dtrace.c revision 268595
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * $FreeBSD: stable/10/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c 268595 2014-07-13 17:50:50Z pfg $ 22 */ 23 24/* 25 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2012 by Delphix. All rights reserved. 28 */ 29 30/* 31 * DTrace - Dynamic Tracing for Solaris 32 * 33 * This is the implementation of the Solaris Dynamic Tracing framework 34 * (DTrace). The user-visible interface to DTrace is described at length in 35 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 36 * library, the in-kernel DTrace framework, and the DTrace providers are 37 * described in the block comments in the <sys/dtrace.h> header file. The 38 * internal architecture of DTrace is described in the block comments in the 39 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 40 * implementation very much assume mastery of all of these sources; if one has 41 * an unanswered question about the implementation, one should consult them 42 * first. 43 * 44 * The functions here are ordered roughly as follows: 45 * 46 * - Probe context functions 47 * - Probe hashing functions 48 * - Non-probe context utility functions 49 * - Matching functions 50 * - Provider-to-Framework API functions 51 * - Probe management functions 52 * - DIF object functions 53 * - Format functions 54 * - Predicate functions 55 * - ECB functions 56 * - Buffer functions 57 * - Enabling functions 58 * - DOF functions 59 * - Anonymous enabling functions 60 * - Consumer state functions 61 * - Helper functions 62 * - Hook functions 63 * - Driver cookbook functions 64 * 65 * Each group of functions begins with a block comment labelled the "DTrace 66 * [Group] Functions", allowing one to find each block by searching forward 67 * on capital-f functions. 68 */ 69#include <sys/errno.h> 70#if !defined(sun) 71#include <sys/time.h> 72#endif 73#include <sys/stat.h> 74#include <sys/modctl.h> 75#include <sys/conf.h> 76#include <sys/systm.h> 77#if defined(sun) 78#include <sys/ddi.h> 79#include <sys/sunddi.h> 80#endif 81#include <sys/cpuvar.h> 82#include <sys/kmem.h> 83#if defined(sun) 84#include <sys/strsubr.h> 85#endif 86#include <sys/sysmacros.h> 87#include <sys/dtrace_impl.h> 88#include <sys/atomic.h> 89#include <sys/cmn_err.h> 90#if defined(sun) 91#include <sys/mutex_impl.h> 92#include <sys/rwlock_impl.h> 93#endif 94#include <sys/ctf_api.h> 95#if defined(sun) 96#include <sys/panic.h> 97#include <sys/priv_impl.h> 98#endif 99#include <sys/policy.h> 100#if defined(sun) 101#include <sys/cred_impl.h> 102#include <sys/procfs_isa.h> 103#endif 104#include <sys/taskq.h> 105#if defined(sun) 106#include <sys/mkdev.h> 107#include <sys/kdi.h> 108#endif 109#include <sys/zone.h> 110#include <sys/socket.h> 111#include <netinet/in.h> 112#include "strtolctype.h" 113 114/* FreeBSD includes: */ 115#if !defined(sun) 116#include <sys/callout.h> 117#include <sys/ctype.h> 118#include <sys/eventhandler.h> 119#include <sys/limits.h> 120#include <sys/kdb.h> 121#include <sys/kernel.h> 122#include <sys/malloc.h> 123#include <sys/sysctl.h> 124#include <sys/lock.h> 125#include <sys/mutex.h> 126#include <sys/rwlock.h> 127#include <sys/sx.h> 128#include <sys/dtrace_bsd.h> 129#include <netinet/in.h> 130#include "dtrace_cddl.h" 131#include "dtrace_debug.c" 132#endif 133 134/* 135 * DTrace Tunable Variables 136 * 137 * The following variables may be tuned by adding a line to /etc/system that 138 * includes both the name of the DTrace module ("dtrace") and the name of the 139 * variable. For example: 140 * 141 * set dtrace:dtrace_destructive_disallow = 1 142 * 143 * In general, the only variables that one should be tuning this way are those 144 * that affect system-wide DTrace behavior, and for which the default behavior 145 * is undesirable. Most of these variables are tunable on a per-consumer 146 * basis using DTrace options, and need not be tuned on a system-wide basis. 147 * When tuning these variables, avoid pathological values; while some attempt 148 * is made to verify the integrity of these variables, they are not considered 149 * part of the supported interface to DTrace, and they are therefore not 150 * checked comprehensively. Further, these variables should not be tuned 151 * dynamically via "mdb -kw" or other means; they should only be tuned via 152 * /etc/system. 153 */ 154int dtrace_destructive_disallow = 0; 155dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 156size_t dtrace_difo_maxsize = (256 * 1024); 157dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 158size_t dtrace_global_maxsize = (16 * 1024); 159size_t dtrace_actions_max = (16 * 1024); 160size_t dtrace_retain_max = 1024; 161dtrace_optval_t dtrace_helper_actions_max = 128; 162dtrace_optval_t dtrace_helper_providers_max = 32; 163dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 164size_t dtrace_strsize_default = 256; 165dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 166dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 167dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 168dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 169dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 170dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 171dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 172dtrace_optval_t dtrace_nspec_default = 1; 173dtrace_optval_t dtrace_specsize_default = 32 * 1024; 174dtrace_optval_t dtrace_stackframes_default = 20; 175dtrace_optval_t dtrace_ustackframes_default = 20; 176dtrace_optval_t dtrace_jstackframes_default = 50; 177dtrace_optval_t dtrace_jstackstrsize_default = 512; 178int dtrace_msgdsize_max = 128; 179hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 180hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 181int dtrace_devdepth_max = 32; 182int dtrace_err_verbose; 183hrtime_t dtrace_deadman_interval = NANOSEC; 184hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 185hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 186hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 187 188/* 189 * DTrace External Variables 190 * 191 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 192 * available to DTrace consumers via the backtick (`) syntax. One of these, 193 * dtrace_zero, is made deliberately so: it is provided as a source of 194 * well-known, zero-filled memory. While this variable is not documented, 195 * it is used by some translators as an implementation detail. 196 */ 197const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 198 199/* 200 * DTrace Internal Variables 201 */ 202#if defined(sun) 203static dev_info_t *dtrace_devi; /* device info */ 204#endif 205#if defined(sun) 206static vmem_t *dtrace_arena; /* probe ID arena */ 207static vmem_t *dtrace_minor; /* minor number arena */ 208#else 209static taskq_t *dtrace_taskq; /* task queue */ 210static struct unrhdr *dtrace_arena; /* Probe ID number. */ 211#endif 212static dtrace_probe_t **dtrace_probes; /* array of all probes */ 213static int dtrace_nprobes; /* number of probes */ 214static dtrace_provider_t *dtrace_provider; /* provider list */ 215static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 216static int dtrace_opens; /* number of opens */ 217static int dtrace_helpers; /* number of helpers */ 218static int dtrace_getf; /* number of unpriv getf()s */ 219#if defined(sun) 220static void *dtrace_softstate; /* softstate pointer */ 221#endif 222static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 223static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 224static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 225static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 226static int dtrace_toxranges; /* number of toxic ranges */ 227static int dtrace_toxranges_max; /* size of toxic range array */ 228static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 229static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 230static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 231static kthread_t *dtrace_panicked; /* panicking thread */ 232static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 233static dtrace_genid_t dtrace_probegen; /* current probe generation */ 234static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 235static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 236static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 237static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 238static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 239#if !defined(sun) 240static struct mtx dtrace_unr_mtx; 241MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 242int dtrace_in_probe; /* non-zero if executing a probe */ 243#if defined(__i386__) || defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 244uintptr_t dtrace_in_probe_addr; /* Address of invop when already in probe */ 245#endif 246static eventhandler_tag dtrace_kld_load_tag; 247static eventhandler_tag dtrace_kld_unload_try_tag; 248#endif 249 250/* 251 * DTrace Locking 252 * DTrace is protected by three (relatively coarse-grained) locks: 253 * 254 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 255 * including enabling state, probes, ECBs, consumer state, helper state, 256 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 257 * probe context is lock-free -- synchronization is handled via the 258 * dtrace_sync() cross call mechanism. 259 * 260 * (2) dtrace_provider_lock is required when manipulating provider state, or 261 * when provider state must be held constant. 262 * 263 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 264 * when meta provider state must be held constant. 265 * 266 * The lock ordering between these three locks is dtrace_meta_lock before 267 * dtrace_provider_lock before dtrace_lock. (In particular, there are 268 * several places where dtrace_provider_lock is held by the framework as it 269 * calls into the providers -- which then call back into the framework, 270 * grabbing dtrace_lock.) 271 * 272 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 273 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 274 * role as a coarse-grained lock; it is acquired before both of these locks. 275 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 276 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 277 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 278 * acquired _between_ dtrace_provider_lock and dtrace_lock. 279 */ 280static kmutex_t dtrace_lock; /* probe state lock */ 281static kmutex_t dtrace_provider_lock; /* provider state lock */ 282static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 283 284#if !defined(sun) 285/* XXX FreeBSD hacks. */ 286#define cr_suid cr_svuid 287#define cr_sgid cr_svgid 288#define ipaddr_t in_addr_t 289#define mod_modname pathname 290#define vuprintf vprintf 291#define ttoproc(_a) ((_a)->td_proc) 292#define crgetzoneid(_a) 0 293#define NCPU MAXCPU 294#define SNOCD 0 295#define CPU_ON_INTR(_a) 0 296 297#define PRIV_EFFECTIVE (1 << 0) 298#define PRIV_DTRACE_KERNEL (1 << 1) 299#define PRIV_DTRACE_PROC (1 << 2) 300#define PRIV_DTRACE_USER (1 << 3) 301#define PRIV_PROC_OWNER (1 << 4) 302#define PRIV_PROC_ZONE (1 << 5) 303#define PRIV_ALL ~0 304 305SYSCTL_DECL(_debug_dtrace); 306SYSCTL_DECL(_kern_dtrace); 307#endif 308 309#if defined(sun) 310#define curcpu CPU->cpu_id 311#endif 312 313 314/* 315 * DTrace Provider Variables 316 * 317 * These are the variables relating to DTrace as a provider (that is, the 318 * provider of the BEGIN, END, and ERROR probes). 319 */ 320static dtrace_pattr_t dtrace_provider_attr = { 321{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 322{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 323{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 324{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 325{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 326}; 327 328static void 329dtrace_nullop(void) 330{} 331 332static dtrace_pops_t dtrace_provider_ops = { 333 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 334 (void (*)(void *, modctl_t *))dtrace_nullop, 335 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 336 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 337 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 338 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 339 NULL, 340 NULL, 341 NULL, 342 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 343}; 344 345static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 346static dtrace_id_t dtrace_probeid_end; /* special END probe */ 347dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 348 349/* 350 * DTrace Helper Tracing Variables 351 */ 352uint32_t dtrace_helptrace_next = 0; 353uint32_t dtrace_helptrace_nlocals; 354char *dtrace_helptrace_buffer; 355int dtrace_helptrace_bufsize = 512 * 1024; 356 357#ifdef DEBUG 358int dtrace_helptrace_enabled = 1; 359#else 360int dtrace_helptrace_enabled = 0; 361#endif 362 363/* 364 * DTrace Error Hashing 365 * 366 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 367 * table. This is very useful for checking coverage of tests that are 368 * expected to induce DIF or DOF processing errors, and may be useful for 369 * debugging problems in the DIF code generator or in DOF generation . The 370 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 371 */ 372#ifdef DEBUG 373static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 374static const char *dtrace_errlast; 375static kthread_t *dtrace_errthread; 376static kmutex_t dtrace_errlock; 377#endif 378 379/* 380 * DTrace Macros and Constants 381 * 382 * These are various macros that are useful in various spots in the 383 * implementation, along with a few random constants that have no meaning 384 * outside of the implementation. There is no real structure to this cpp 385 * mishmash -- but is there ever? 386 */ 387#define DTRACE_HASHSTR(hash, probe) \ 388 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 389 390#define DTRACE_HASHNEXT(hash, probe) \ 391 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 392 393#define DTRACE_HASHPREV(hash, probe) \ 394 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 395 396#define DTRACE_HASHEQ(hash, lhs, rhs) \ 397 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 398 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 399 400#define DTRACE_AGGHASHSIZE_SLEW 17 401 402#define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 403 404/* 405 * The key for a thread-local variable consists of the lower 61 bits of the 406 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 407 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 408 * equal to a variable identifier. This is necessary (but not sufficient) to 409 * assure that global associative arrays never collide with thread-local 410 * variables. To guarantee that they cannot collide, we must also define the 411 * order for keying dynamic variables. That order is: 412 * 413 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 414 * 415 * Because the variable-key and the tls-key are in orthogonal spaces, there is 416 * no way for a global variable key signature to match a thread-local key 417 * signature. 418 */ 419#if defined(sun) 420#define DTRACE_TLS_THRKEY(where) { \ 421 uint_t intr = 0; \ 422 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 423 for (; actv; actv >>= 1) \ 424 intr++; \ 425 ASSERT(intr < (1 << 3)); \ 426 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 427 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 428} 429#else 430#define DTRACE_TLS_THRKEY(where) { \ 431 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 432 uint_t intr = 0; \ 433 uint_t actv = _c->cpu_intr_actv; \ 434 for (; actv; actv >>= 1) \ 435 intr++; \ 436 ASSERT(intr < (1 << 3)); \ 437 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 438 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 439} 440#endif 441 442#define DT_BSWAP_8(x) ((x) & 0xff) 443#define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 444#define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 445#define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 446 447#define DT_MASK_LO 0x00000000FFFFFFFFULL 448 449#define DTRACE_STORE(type, tomax, offset, what) \ 450 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 451 452#ifndef __x86 453#define DTRACE_ALIGNCHECK(addr, size, flags) \ 454 if (addr & (size - 1)) { \ 455 *flags |= CPU_DTRACE_BADALIGN; \ 456 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 457 return (0); \ 458 } 459#else 460#define DTRACE_ALIGNCHECK(addr, size, flags) 461#endif 462 463/* 464 * Test whether a range of memory starting at testaddr of size testsz falls 465 * within the range of memory described by addr, sz. We take care to avoid 466 * problems with overflow and underflow of the unsigned quantities, and 467 * disallow all negative sizes. Ranges of size 0 are allowed. 468 */ 469#define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 470 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 471 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 472 (testaddr) + (testsz) >= (testaddr)) 473 474/* 475 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 476 * alloc_sz on the righthand side of the comparison in order to avoid overflow 477 * or underflow in the comparison with it. This is simpler than the INRANGE 478 * check above, because we know that the dtms_scratch_ptr is valid in the 479 * range. Allocations of size zero are allowed. 480 */ 481#define DTRACE_INSCRATCH(mstate, alloc_sz) \ 482 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 483 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 484 485#define DTRACE_LOADFUNC(bits) \ 486/*CSTYLED*/ \ 487uint##bits##_t \ 488dtrace_load##bits(uintptr_t addr) \ 489{ \ 490 size_t size = bits / NBBY; \ 491 /*CSTYLED*/ \ 492 uint##bits##_t rval; \ 493 int i; \ 494 volatile uint16_t *flags = (volatile uint16_t *) \ 495 &cpu_core[curcpu].cpuc_dtrace_flags; \ 496 \ 497 DTRACE_ALIGNCHECK(addr, size, flags); \ 498 \ 499 for (i = 0; i < dtrace_toxranges; i++) { \ 500 if (addr >= dtrace_toxrange[i].dtt_limit) \ 501 continue; \ 502 \ 503 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 504 continue; \ 505 \ 506 /* \ 507 * This address falls within a toxic region; return 0. \ 508 */ \ 509 *flags |= CPU_DTRACE_BADADDR; \ 510 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 511 return (0); \ 512 } \ 513 \ 514 *flags |= CPU_DTRACE_NOFAULT; \ 515 /*CSTYLED*/ \ 516 rval = *((volatile uint##bits##_t *)addr); \ 517 *flags &= ~CPU_DTRACE_NOFAULT; \ 518 \ 519 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 520} 521 522#ifdef _LP64 523#define dtrace_loadptr dtrace_load64 524#else 525#define dtrace_loadptr dtrace_load32 526#endif 527 528#define DTRACE_DYNHASH_FREE 0 529#define DTRACE_DYNHASH_SINK 1 530#define DTRACE_DYNHASH_VALID 2 531 532#define DTRACE_MATCH_NEXT 0 533#define DTRACE_MATCH_DONE 1 534#define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 535#define DTRACE_STATE_ALIGN 64 536 537#define DTRACE_FLAGS2FLT(flags) \ 538 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 539 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 540 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 541 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 542 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 543 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 544 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 545 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 546 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 547 DTRACEFLT_UNKNOWN) 548 549#define DTRACEACT_ISSTRING(act) \ 550 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 551 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 552 553/* Function prototype definitions: */ 554static size_t dtrace_strlen(const char *, size_t); 555static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 556static void dtrace_enabling_provide(dtrace_provider_t *); 557static int dtrace_enabling_match(dtrace_enabling_t *, int *); 558static void dtrace_enabling_matchall(void); 559static void dtrace_enabling_reap(void); 560static dtrace_state_t *dtrace_anon_grab(void); 561static uint64_t dtrace_helper(int, dtrace_mstate_t *, 562 dtrace_state_t *, uint64_t, uint64_t); 563static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 564static void dtrace_buffer_drop(dtrace_buffer_t *); 565static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 566static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 567 dtrace_state_t *, dtrace_mstate_t *); 568static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 569 dtrace_optval_t); 570static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 571static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 572uint16_t dtrace_load16(uintptr_t); 573uint32_t dtrace_load32(uintptr_t); 574uint64_t dtrace_load64(uintptr_t); 575uint8_t dtrace_load8(uintptr_t); 576void dtrace_dynvar_clean(dtrace_dstate_t *); 577dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 578 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 579uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 580static int dtrace_priv_proc(dtrace_state_t *); 581static void dtrace_getf_barrier(void); 582 583/* 584 * DTrace Probe Context Functions 585 * 586 * These functions are called from probe context. Because probe context is 587 * any context in which C may be called, arbitrarily locks may be held, 588 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 589 * As a result, functions called from probe context may only call other DTrace 590 * support functions -- they may not interact at all with the system at large. 591 * (Note that the ASSERT macro is made probe-context safe by redefining it in 592 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 593 * loads are to be performed from probe context, they _must_ be in terms of 594 * the safe dtrace_load*() variants. 595 * 596 * Some functions in this block are not actually called from probe context; 597 * for these functions, there will be a comment above the function reading 598 * "Note: not called from probe context." 599 */ 600void 601dtrace_panic(const char *format, ...) 602{ 603 va_list alist; 604 605 va_start(alist, format); 606 dtrace_vpanic(format, alist); 607 va_end(alist); 608} 609 610int 611dtrace_assfail(const char *a, const char *f, int l) 612{ 613 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 614 615 /* 616 * We just need something here that even the most clever compiler 617 * cannot optimize away. 618 */ 619 return (a[(uintptr_t)f]); 620} 621 622/* 623 * Atomically increment a specified error counter from probe context. 624 */ 625static void 626dtrace_error(uint32_t *counter) 627{ 628 /* 629 * Most counters stored to in probe context are per-CPU counters. 630 * However, there are some error conditions that are sufficiently 631 * arcane that they don't merit per-CPU storage. If these counters 632 * are incremented concurrently on different CPUs, scalability will be 633 * adversely affected -- but we don't expect them to be white-hot in a 634 * correctly constructed enabling... 635 */ 636 uint32_t oval, nval; 637 638 do { 639 oval = *counter; 640 641 if ((nval = oval + 1) == 0) { 642 /* 643 * If the counter would wrap, set it to 1 -- assuring 644 * that the counter is never zero when we have seen 645 * errors. (The counter must be 32-bits because we 646 * aren't guaranteed a 64-bit compare&swap operation.) 647 * To save this code both the infamy of being fingered 648 * by a priggish news story and the indignity of being 649 * the target of a neo-puritan witch trial, we're 650 * carefully avoiding any colorful description of the 651 * likelihood of this condition -- but suffice it to 652 * say that it is only slightly more likely than the 653 * overflow of predicate cache IDs, as discussed in 654 * dtrace_predicate_create(). 655 */ 656 nval = 1; 657 } 658 } while (dtrace_cas32(counter, oval, nval) != oval); 659} 660 661/* 662 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 663 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 664 */ 665DTRACE_LOADFUNC(8) 666DTRACE_LOADFUNC(16) 667DTRACE_LOADFUNC(32) 668DTRACE_LOADFUNC(64) 669 670static int 671dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 672{ 673 if (dest < mstate->dtms_scratch_base) 674 return (0); 675 676 if (dest + size < dest) 677 return (0); 678 679 if (dest + size > mstate->dtms_scratch_ptr) 680 return (0); 681 682 return (1); 683} 684 685static int 686dtrace_canstore_statvar(uint64_t addr, size_t sz, 687 dtrace_statvar_t **svars, int nsvars) 688{ 689 int i; 690 691 for (i = 0; i < nsvars; i++) { 692 dtrace_statvar_t *svar = svars[i]; 693 694 if (svar == NULL || svar->dtsv_size == 0) 695 continue; 696 697 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 698 return (1); 699 } 700 701 return (0); 702} 703 704/* 705 * Check to see if the address is within a memory region to which a store may 706 * be issued. This includes the DTrace scratch areas, and any DTrace variable 707 * region. The caller of dtrace_canstore() is responsible for performing any 708 * alignment checks that are needed before stores are actually executed. 709 */ 710static int 711dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 712 dtrace_vstate_t *vstate) 713{ 714 /* 715 * First, check to see if the address is in scratch space... 716 */ 717 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 718 mstate->dtms_scratch_size)) 719 return (1); 720 721 /* 722 * Now check to see if it's a dynamic variable. This check will pick 723 * up both thread-local variables and any global dynamically-allocated 724 * variables. 725 */ 726 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 727 vstate->dtvs_dynvars.dtds_size)) { 728 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 729 uintptr_t base = (uintptr_t)dstate->dtds_base + 730 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 731 uintptr_t chunkoffs; 732 733 /* 734 * Before we assume that we can store here, we need to make 735 * sure that it isn't in our metadata -- storing to our 736 * dynamic variable metadata would corrupt our state. For 737 * the range to not include any dynamic variable metadata, 738 * it must: 739 * 740 * (1) Start above the hash table that is at the base of 741 * the dynamic variable space 742 * 743 * (2) Have a starting chunk offset that is beyond the 744 * dtrace_dynvar_t that is at the base of every chunk 745 * 746 * (3) Not span a chunk boundary 747 * 748 */ 749 if (addr < base) 750 return (0); 751 752 chunkoffs = (addr - base) % dstate->dtds_chunksize; 753 754 if (chunkoffs < sizeof (dtrace_dynvar_t)) 755 return (0); 756 757 if (chunkoffs + sz > dstate->dtds_chunksize) 758 return (0); 759 760 return (1); 761 } 762 763 /* 764 * Finally, check the static local and global variables. These checks 765 * take the longest, so we perform them last. 766 */ 767 if (dtrace_canstore_statvar(addr, sz, 768 vstate->dtvs_locals, vstate->dtvs_nlocals)) 769 return (1); 770 771 if (dtrace_canstore_statvar(addr, sz, 772 vstate->dtvs_globals, vstate->dtvs_nglobals)) 773 return (1); 774 775 return (0); 776} 777 778 779/* 780 * Convenience routine to check to see if the address is within a memory 781 * region in which a load may be issued given the user's privilege level; 782 * if not, it sets the appropriate error flags and loads 'addr' into the 783 * illegal value slot. 784 * 785 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 786 * appropriate memory access protection. 787 */ 788static int 789dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 790 dtrace_vstate_t *vstate) 791{ 792 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 793 file_t *fp; 794 795 /* 796 * If we hold the privilege to read from kernel memory, then 797 * everything is readable. 798 */ 799 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 800 return (1); 801 802 /* 803 * You can obviously read that which you can store. 804 */ 805 if (dtrace_canstore(addr, sz, mstate, vstate)) 806 return (1); 807 808 /* 809 * We're allowed to read from our own string table. 810 */ 811 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 812 mstate->dtms_difo->dtdo_strlen)) 813 return (1); 814 815 if (vstate->dtvs_state != NULL && 816 dtrace_priv_proc(vstate->dtvs_state)) { 817 proc_t *p; 818 819 /* 820 * When we have privileges to the current process, there are 821 * several context-related kernel structures that are safe to 822 * read, even absent the privilege to read from kernel memory. 823 * These reads are safe because these structures contain only 824 * state that (1) we're permitted to read, (2) is harmless or 825 * (3) contains pointers to additional kernel state that we're 826 * not permitted to read (and as such, do not present an 827 * opportunity for privilege escalation). Finally (and 828 * critically), because of the nature of their relation with 829 * the current thread context, the memory associated with these 830 * structures cannot change over the duration of probe context, 831 * and it is therefore impossible for this memory to be 832 * deallocated and reallocated as something else while it's 833 * being operated upon. 834 */ 835 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) 836 return (1); 837 838 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 839 sz, curthread->t_procp, sizeof (proc_t))) { 840 return (1); 841 } 842 843 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 844 curthread->t_cred, sizeof (cred_t))) { 845 return (1); 846 } 847 848#if defined(sun) 849 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 850 &(p->p_pidp->pid_id), sizeof (pid_t))) { 851 return (1); 852 } 853 854 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 855 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 856 return (1); 857 } 858#endif 859 } 860 861 if ((fp = mstate->dtms_getf) != NULL) { 862 uintptr_t psz = sizeof (void *); 863 vnode_t *vp; 864 vnodeops_t *op; 865 866 /* 867 * When getf() returns a file_t, the enabling is implicitly 868 * granted the (transient) right to read the returned file_t 869 * as well as the v_path and v_op->vnop_name of the underlying 870 * vnode. These accesses are allowed after a successful 871 * getf() because the members that they refer to cannot change 872 * once set -- and the barrier logic in the kernel's closef() 873 * path assures that the file_t and its referenced vode_t 874 * cannot themselves be stale (that is, it impossible for 875 * either dtms_getf itself or its f_vnode member to reference 876 * freed memory). 877 */ 878 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) 879 return (1); 880 881 if ((vp = fp->f_vnode) != NULL) { 882#if defined(sun) 883 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) 884 return (1); 885 if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz, 886 vp->v_path, strlen(vp->v_path) + 1)) { 887 return (1); 888 } 889#endif 890 891 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) 892 return (1); 893 894#if defined(sun) 895 if ((op = vp->v_op) != NULL && 896 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 897 return (1); 898 } 899 900 if (op != NULL && op->vnop_name != NULL && 901 DTRACE_INRANGE(addr, sz, op->vnop_name, 902 strlen(op->vnop_name) + 1)) { 903 return (1); 904 } 905#endif 906 } 907 } 908 909 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 910 *illval = addr; 911 return (0); 912} 913 914/* 915 * Convenience routine to check to see if a given string is within a memory 916 * region in which a load may be issued given the user's privilege level; 917 * this exists so that we don't need to issue unnecessary dtrace_strlen() 918 * calls in the event that the user has all privileges. 919 */ 920static int 921dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 922 dtrace_vstate_t *vstate) 923{ 924 size_t strsz; 925 926 /* 927 * If we hold the privilege to read from kernel memory, then 928 * everything is readable. 929 */ 930 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 931 return (1); 932 933 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 934 if (dtrace_canload(addr, strsz, mstate, vstate)) 935 return (1); 936 937 return (0); 938} 939 940/* 941 * Convenience routine to check to see if a given variable is within a memory 942 * region in which a load may be issued given the user's privilege level. 943 */ 944static int 945dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 946 dtrace_vstate_t *vstate) 947{ 948 size_t sz; 949 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 950 951 /* 952 * If we hold the privilege to read from kernel memory, then 953 * everything is readable. 954 */ 955 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 956 return (1); 957 958 if (type->dtdt_kind == DIF_TYPE_STRING) 959 sz = dtrace_strlen(src, 960 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 961 else 962 sz = type->dtdt_size; 963 964 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 965} 966 967/* 968 * Convert a string to a signed integer using safe loads. 969 * 970 * NOTE: This function uses various macros from strtolctype.h to manipulate 971 * digit values, etc -- these have all been checked to ensure they make 972 * no additional function calls. 973 */ 974static int64_t 975dtrace_strtoll(char *input, int base, size_t limit) 976{ 977 uintptr_t pos = (uintptr_t)input; 978 int64_t val = 0; 979 int x; 980 boolean_t neg = B_FALSE; 981 char c, cc, ccc; 982 uintptr_t end = pos + limit; 983 984 /* 985 * Consume any whitespace preceding digits. 986 */ 987 while ((c = dtrace_load8(pos)) == ' ' || c == '\t') 988 pos++; 989 990 /* 991 * Handle an explicit sign if one is present. 992 */ 993 if (c == '-' || c == '+') { 994 if (c == '-') 995 neg = B_TRUE; 996 c = dtrace_load8(++pos); 997 } 998 999 /* 1000 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it 1001 * if present. 1002 */ 1003 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' || 1004 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) { 1005 pos += 2; 1006 c = ccc; 1007 } 1008 1009 /* 1010 * Read in contiguous digits until the first non-digit character. 1011 */ 1012 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base; 1013 c = dtrace_load8(++pos)) 1014 val = val * base + x; 1015 1016 return (neg ? -val : val); 1017} 1018 1019/* 1020 * Compare two strings using safe loads. 1021 */ 1022static int 1023dtrace_strncmp(char *s1, char *s2, size_t limit) 1024{ 1025 uint8_t c1, c2; 1026 volatile uint16_t *flags; 1027 1028 if (s1 == s2 || limit == 0) 1029 return (0); 1030 1031 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1032 1033 do { 1034 if (s1 == NULL) { 1035 c1 = '\0'; 1036 } else { 1037 c1 = dtrace_load8((uintptr_t)s1++); 1038 } 1039 1040 if (s2 == NULL) { 1041 c2 = '\0'; 1042 } else { 1043 c2 = dtrace_load8((uintptr_t)s2++); 1044 } 1045 1046 if (c1 != c2) 1047 return (c1 - c2); 1048 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 1049 1050 return (0); 1051} 1052 1053/* 1054 * Compute strlen(s) for a string using safe memory accesses. The additional 1055 * len parameter is used to specify a maximum length to ensure completion. 1056 */ 1057static size_t 1058dtrace_strlen(const char *s, size_t lim) 1059{ 1060 uint_t len; 1061 1062 for (len = 0; len != lim; len++) { 1063 if (dtrace_load8((uintptr_t)s++) == '\0') 1064 break; 1065 } 1066 1067 return (len); 1068} 1069 1070/* 1071 * Check if an address falls within a toxic region. 1072 */ 1073static int 1074dtrace_istoxic(uintptr_t kaddr, size_t size) 1075{ 1076 uintptr_t taddr, tsize; 1077 int i; 1078 1079 for (i = 0; i < dtrace_toxranges; i++) { 1080 taddr = dtrace_toxrange[i].dtt_base; 1081 tsize = dtrace_toxrange[i].dtt_limit - taddr; 1082 1083 if (kaddr - taddr < tsize) { 1084 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1085 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 1086 return (1); 1087 } 1088 1089 if (taddr - kaddr < size) { 1090 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1091 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 1092 return (1); 1093 } 1094 } 1095 1096 return (0); 1097} 1098 1099/* 1100 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 1101 * memory specified by the DIF program. The dst is assumed to be safe memory 1102 * that we can store to directly because it is managed by DTrace. As with 1103 * standard bcopy, overlapping copies are handled properly. 1104 */ 1105static void 1106dtrace_bcopy(const void *src, void *dst, size_t len) 1107{ 1108 if (len != 0) { 1109 uint8_t *s1 = dst; 1110 const uint8_t *s2 = src; 1111 1112 if (s1 <= s2) { 1113 do { 1114 *s1++ = dtrace_load8((uintptr_t)s2++); 1115 } while (--len != 0); 1116 } else { 1117 s2 += len; 1118 s1 += len; 1119 1120 do { 1121 *--s1 = dtrace_load8((uintptr_t)--s2); 1122 } while (--len != 0); 1123 } 1124 } 1125} 1126 1127/* 1128 * Copy src to dst using safe memory accesses, up to either the specified 1129 * length, or the point that a nul byte is encountered. The src is assumed to 1130 * be unsafe memory specified by the DIF program. The dst is assumed to be 1131 * safe memory that we can store to directly because it is managed by DTrace. 1132 * Unlike dtrace_bcopy(), overlapping regions are not handled. 1133 */ 1134static void 1135dtrace_strcpy(const void *src, void *dst, size_t len) 1136{ 1137 if (len != 0) { 1138 uint8_t *s1 = dst, c; 1139 const uint8_t *s2 = src; 1140 1141 do { 1142 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1143 } while (--len != 0 && c != '\0'); 1144 } 1145} 1146 1147/* 1148 * Copy src to dst, deriving the size and type from the specified (BYREF) 1149 * variable type. The src is assumed to be unsafe memory specified by the DIF 1150 * program. The dst is assumed to be DTrace variable memory that is of the 1151 * specified type; we assume that we can store to directly. 1152 */ 1153static void 1154dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 1155{ 1156 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1157 1158 if (type->dtdt_kind == DIF_TYPE_STRING) { 1159 dtrace_strcpy(src, dst, type->dtdt_size); 1160 } else { 1161 dtrace_bcopy(src, dst, type->dtdt_size); 1162 } 1163} 1164 1165/* 1166 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1167 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1168 * safe memory that we can access directly because it is managed by DTrace. 1169 */ 1170static int 1171dtrace_bcmp(const void *s1, const void *s2, size_t len) 1172{ 1173 volatile uint16_t *flags; 1174 1175 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1176 1177 if (s1 == s2) 1178 return (0); 1179 1180 if (s1 == NULL || s2 == NULL) 1181 return (1); 1182 1183 if (s1 != s2 && len != 0) { 1184 const uint8_t *ps1 = s1; 1185 const uint8_t *ps2 = s2; 1186 1187 do { 1188 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1189 return (1); 1190 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1191 } 1192 return (0); 1193} 1194 1195/* 1196 * Zero the specified region using a simple byte-by-byte loop. Note that this 1197 * is for safe DTrace-managed memory only. 1198 */ 1199static void 1200dtrace_bzero(void *dst, size_t len) 1201{ 1202 uchar_t *cp; 1203 1204 for (cp = dst; len != 0; len--) 1205 *cp++ = 0; 1206} 1207 1208static void 1209dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1210{ 1211 uint64_t result[2]; 1212 1213 result[0] = addend1[0] + addend2[0]; 1214 result[1] = addend1[1] + addend2[1] + 1215 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1216 1217 sum[0] = result[0]; 1218 sum[1] = result[1]; 1219} 1220 1221/* 1222 * Shift the 128-bit value in a by b. If b is positive, shift left. 1223 * If b is negative, shift right. 1224 */ 1225static void 1226dtrace_shift_128(uint64_t *a, int b) 1227{ 1228 uint64_t mask; 1229 1230 if (b == 0) 1231 return; 1232 1233 if (b < 0) { 1234 b = -b; 1235 if (b >= 64) { 1236 a[0] = a[1] >> (b - 64); 1237 a[1] = 0; 1238 } else { 1239 a[0] >>= b; 1240 mask = 1LL << (64 - b); 1241 mask -= 1; 1242 a[0] |= ((a[1] & mask) << (64 - b)); 1243 a[1] >>= b; 1244 } 1245 } else { 1246 if (b >= 64) { 1247 a[1] = a[0] << (b - 64); 1248 a[0] = 0; 1249 } else { 1250 a[1] <<= b; 1251 mask = a[0] >> (64 - b); 1252 a[1] |= mask; 1253 a[0] <<= b; 1254 } 1255 } 1256} 1257 1258/* 1259 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1260 * use native multiplication on those, and then re-combine into the 1261 * resulting 128-bit value. 1262 * 1263 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1264 * hi1 * hi2 << 64 + 1265 * hi1 * lo2 << 32 + 1266 * hi2 * lo1 << 32 + 1267 * lo1 * lo2 1268 */ 1269static void 1270dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1271{ 1272 uint64_t hi1, hi2, lo1, lo2; 1273 uint64_t tmp[2]; 1274 1275 hi1 = factor1 >> 32; 1276 hi2 = factor2 >> 32; 1277 1278 lo1 = factor1 & DT_MASK_LO; 1279 lo2 = factor2 & DT_MASK_LO; 1280 1281 product[0] = lo1 * lo2; 1282 product[1] = hi1 * hi2; 1283 1284 tmp[0] = hi1 * lo2; 1285 tmp[1] = 0; 1286 dtrace_shift_128(tmp, 32); 1287 dtrace_add_128(product, tmp, product); 1288 1289 tmp[0] = hi2 * lo1; 1290 tmp[1] = 0; 1291 dtrace_shift_128(tmp, 32); 1292 dtrace_add_128(product, tmp, product); 1293} 1294 1295/* 1296 * This privilege check should be used by actions and subroutines to 1297 * verify that the user credentials of the process that enabled the 1298 * invoking ECB match the target credentials 1299 */ 1300static int 1301dtrace_priv_proc_common_user(dtrace_state_t *state) 1302{ 1303 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1304 1305 /* 1306 * We should always have a non-NULL state cred here, since if cred 1307 * is null (anonymous tracing), we fast-path bypass this routine. 1308 */ 1309 ASSERT(s_cr != NULL); 1310 1311 if ((cr = CRED()) != NULL && 1312 s_cr->cr_uid == cr->cr_uid && 1313 s_cr->cr_uid == cr->cr_ruid && 1314 s_cr->cr_uid == cr->cr_suid && 1315 s_cr->cr_gid == cr->cr_gid && 1316 s_cr->cr_gid == cr->cr_rgid && 1317 s_cr->cr_gid == cr->cr_sgid) 1318 return (1); 1319 1320 return (0); 1321} 1322 1323/* 1324 * This privilege check should be used by actions and subroutines to 1325 * verify that the zone of the process that enabled the invoking ECB 1326 * matches the target credentials 1327 */ 1328static int 1329dtrace_priv_proc_common_zone(dtrace_state_t *state) 1330{ 1331#if defined(sun) 1332 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1333 1334 /* 1335 * We should always have a non-NULL state cred here, since if cred 1336 * is null (anonymous tracing), we fast-path bypass this routine. 1337 */ 1338 ASSERT(s_cr != NULL); 1339 1340 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1341 return (1); 1342 1343 return (0); 1344#else 1345 return (1); 1346#endif 1347} 1348 1349/* 1350 * This privilege check should be used by actions and subroutines to 1351 * verify that the process has not setuid or changed credentials. 1352 */ 1353static int 1354dtrace_priv_proc_common_nocd(void) 1355{ 1356 proc_t *proc; 1357 1358 if ((proc = ttoproc(curthread)) != NULL && 1359 !(proc->p_flag & SNOCD)) 1360 return (1); 1361 1362 return (0); 1363} 1364 1365static int 1366dtrace_priv_proc_destructive(dtrace_state_t *state) 1367{ 1368 int action = state->dts_cred.dcr_action; 1369 1370 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1371 dtrace_priv_proc_common_zone(state) == 0) 1372 goto bad; 1373 1374 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1375 dtrace_priv_proc_common_user(state) == 0) 1376 goto bad; 1377 1378 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1379 dtrace_priv_proc_common_nocd() == 0) 1380 goto bad; 1381 1382 return (1); 1383 1384bad: 1385 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1386 1387 return (0); 1388} 1389 1390static int 1391dtrace_priv_proc_control(dtrace_state_t *state) 1392{ 1393 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1394 return (1); 1395 1396 if (dtrace_priv_proc_common_zone(state) && 1397 dtrace_priv_proc_common_user(state) && 1398 dtrace_priv_proc_common_nocd()) 1399 return (1); 1400 1401 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1402 1403 return (0); 1404} 1405 1406static int 1407dtrace_priv_proc(dtrace_state_t *state) 1408{ 1409 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1410 return (1); 1411 1412 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1413 1414 return (0); 1415} 1416 1417static int 1418dtrace_priv_kernel(dtrace_state_t *state) 1419{ 1420 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1421 return (1); 1422 1423 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1424 1425 return (0); 1426} 1427 1428static int 1429dtrace_priv_kernel_destructive(dtrace_state_t *state) 1430{ 1431 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1432 return (1); 1433 1434 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1435 1436 return (0); 1437} 1438 1439/* 1440 * Determine if the dte_cond of the specified ECB allows for processing of 1441 * the current probe to continue. Note that this routine may allow continued 1442 * processing, but with access(es) stripped from the mstate's dtms_access 1443 * field. 1444 */ 1445static int 1446dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1447 dtrace_ecb_t *ecb) 1448{ 1449 dtrace_probe_t *probe = ecb->dte_probe; 1450 dtrace_provider_t *prov = probe->dtpr_provider; 1451 dtrace_pops_t *pops = &prov->dtpv_pops; 1452 int mode = DTRACE_MODE_NOPRIV_DROP; 1453 1454 ASSERT(ecb->dte_cond); 1455 1456#if defined(sun) 1457 if (pops->dtps_mode != NULL) { 1458 mode = pops->dtps_mode(prov->dtpv_arg, 1459 probe->dtpr_id, probe->dtpr_arg); 1460 1461 ASSERT((mode & DTRACE_MODE_USER) || 1462 (mode & DTRACE_MODE_KERNEL)); 1463 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1464 (mode & DTRACE_MODE_NOPRIV_DROP)); 1465 } 1466 1467 /* 1468 * If the dte_cond bits indicate that this consumer is only allowed to 1469 * see user-mode firings of this probe, call the provider's dtps_mode() 1470 * entry point to check that the probe was fired while in a user 1471 * context. If that's not the case, use the policy specified by the 1472 * provider to determine if we drop the probe or merely restrict 1473 * operation. 1474 */ 1475 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1476 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1477 1478 if (!(mode & DTRACE_MODE_USER)) { 1479 if (mode & DTRACE_MODE_NOPRIV_DROP) 1480 return (0); 1481 1482 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1483 } 1484 } 1485#endif 1486 1487 /* 1488 * This is more subtle than it looks. We have to be absolutely certain 1489 * that CRED() isn't going to change out from under us so it's only 1490 * legit to examine that structure if we're in constrained situations. 1491 * Currently, the only times we'll this check is if a non-super-user 1492 * has enabled the profile or syscall providers -- providers that 1493 * allow visibility of all processes. For the profile case, the check 1494 * above will ensure that we're examining a user context. 1495 */ 1496 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1497 cred_t *cr; 1498 cred_t *s_cr = state->dts_cred.dcr_cred; 1499 proc_t *proc; 1500 1501 ASSERT(s_cr != NULL); 1502 1503 if ((cr = CRED()) == NULL || 1504 s_cr->cr_uid != cr->cr_uid || 1505 s_cr->cr_uid != cr->cr_ruid || 1506 s_cr->cr_uid != cr->cr_suid || 1507 s_cr->cr_gid != cr->cr_gid || 1508 s_cr->cr_gid != cr->cr_rgid || 1509 s_cr->cr_gid != cr->cr_sgid || 1510 (proc = ttoproc(curthread)) == NULL || 1511 (proc->p_flag & SNOCD)) { 1512 if (mode & DTRACE_MODE_NOPRIV_DROP) 1513 return (0); 1514 1515#if defined(sun) 1516 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1517#endif 1518 } 1519 } 1520 1521#if defined(sun) 1522 /* 1523 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1524 * in our zone, check to see if our mode policy is to restrict rather 1525 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1526 * and DTRACE_ACCESS_ARGS 1527 */ 1528 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1529 cred_t *cr; 1530 cred_t *s_cr = state->dts_cred.dcr_cred; 1531 1532 ASSERT(s_cr != NULL); 1533 1534 if ((cr = CRED()) == NULL || 1535 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1536 if (mode & DTRACE_MODE_NOPRIV_DROP) 1537 return (0); 1538 1539 mstate->dtms_access &= 1540 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1541 } 1542 } 1543#endif 1544 1545 return (1); 1546} 1547 1548/* 1549 * Note: not called from probe context. This function is called 1550 * asynchronously (and at a regular interval) from outside of probe context to 1551 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1552 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1553 */ 1554void 1555dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1556{ 1557 dtrace_dynvar_t *dirty; 1558 dtrace_dstate_percpu_t *dcpu; 1559 dtrace_dynvar_t **rinsep; 1560 int i, j, work = 0; 1561 1562 for (i = 0; i < NCPU; i++) { 1563 dcpu = &dstate->dtds_percpu[i]; 1564 rinsep = &dcpu->dtdsc_rinsing; 1565 1566 /* 1567 * If the dirty list is NULL, there is no dirty work to do. 1568 */ 1569 if (dcpu->dtdsc_dirty == NULL) 1570 continue; 1571 1572 if (dcpu->dtdsc_rinsing != NULL) { 1573 /* 1574 * If the rinsing list is non-NULL, then it is because 1575 * this CPU was selected to accept another CPU's 1576 * dirty list -- and since that time, dirty buffers 1577 * have accumulated. This is a highly unlikely 1578 * condition, but we choose to ignore the dirty 1579 * buffers -- they'll be picked up a future cleanse. 1580 */ 1581 continue; 1582 } 1583 1584 if (dcpu->dtdsc_clean != NULL) { 1585 /* 1586 * If the clean list is non-NULL, then we're in a 1587 * situation where a CPU has done deallocations (we 1588 * have a non-NULL dirty list) but no allocations (we 1589 * also have a non-NULL clean list). We can't simply 1590 * move the dirty list into the clean list on this 1591 * CPU, yet we also don't want to allow this condition 1592 * to persist, lest a short clean list prevent a 1593 * massive dirty list from being cleaned (which in 1594 * turn could lead to otherwise avoidable dynamic 1595 * drops). To deal with this, we look for some CPU 1596 * with a NULL clean list, NULL dirty list, and NULL 1597 * rinsing list -- and then we borrow this CPU to 1598 * rinse our dirty list. 1599 */ 1600 for (j = 0; j < NCPU; j++) { 1601 dtrace_dstate_percpu_t *rinser; 1602 1603 rinser = &dstate->dtds_percpu[j]; 1604 1605 if (rinser->dtdsc_rinsing != NULL) 1606 continue; 1607 1608 if (rinser->dtdsc_dirty != NULL) 1609 continue; 1610 1611 if (rinser->dtdsc_clean != NULL) 1612 continue; 1613 1614 rinsep = &rinser->dtdsc_rinsing; 1615 break; 1616 } 1617 1618 if (j == NCPU) { 1619 /* 1620 * We were unable to find another CPU that 1621 * could accept this dirty list -- we are 1622 * therefore unable to clean it now. 1623 */ 1624 dtrace_dynvar_failclean++; 1625 continue; 1626 } 1627 } 1628 1629 work = 1; 1630 1631 /* 1632 * Atomically move the dirty list aside. 1633 */ 1634 do { 1635 dirty = dcpu->dtdsc_dirty; 1636 1637 /* 1638 * Before we zap the dirty list, set the rinsing list. 1639 * (This allows for a potential assertion in 1640 * dtrace_dynvar(): if a free dynamic variable appears 1641 * on a hash chain, either the dirty list or the 1642 * rinsing list for some CPU must be non-NULL.) 1643 */ 1644 *rinsep = dirty; 1645 dtrace_membar_producer(); 1646 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1647 dirty, NULL) != dirty); 1648 } 1649 1650 if (!work) { 1651 /* 1652 * We have no work to do; we can simply return. 1653 */ 1654 return; 1655 } 1656 1657 dtrace_sync(); 1658 1659 for (i = 0; i < NCPU; i++) { 1660 dcpu = &dstate->dtds_percpu[i]; 1661 1662 if (dcpu->dtdsc_rinsing == NULL) 1663 continue; 1664 1665 /* 1666 * We are now guaranteed that no hash chain contains a pointer 1667 * into this dirty list; we can make it clean. 1668 */ 1669 ASSERT(dcpu->dtdsc_clean == NULL); 1670 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1671 dcpu->dtdsc_rinsing = NULL; 1672 } 1673 1674 /* 1675 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1676 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1677 * This prevents a race whereby a CPU incorrectly decides that 1678 * the state should be something other than DTRACE_DSTATE_CLEAN 1679 * after dtrace_dynvar_clean() has completed. 1680 */ 1681 dtrace_sync(); 1682 1683 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1684} 1685 1686/* 1687 * Depending on the value of the op parameter, this function looks-up, 1688 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1689 * allocation is requested, this function will return a pointer to a 1690 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1691 * variable can be allocated. If NULL is returned, the appropriate counter 1692 * will be incremented. 1693 */ 1694dtrace_dynvar_t * 1695dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1696 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1697 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1698{ 1699 uint64_t hashval = DTRACE_DYNHASH_VALID; 1700 dtrace_dynhash_t *hash = dstate->dtds_hash; 1701 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1702 processorid_t me = curcpu, cpu = me; 1703 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1704 size_t bucket, ksize; 1705 size_t chunksize = dstate->dtds_chunksize; 1706 uintptr_t kdata, lock, nstate; 1707 uint_t i; 1708 1709 ASSERT(nkeys != 0); 1710 1711 /* 1712 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1713 * algorithm. For the by-value portions, we perform the algorithm in 1714 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1715 * bit, and seems to have only a minute effect on distribution. For 1716 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1717 * over each referenced byte. It's painful to do this, but it's much 1718 * better than pathological hash distribution. The efficacy of the 1719 * hashing algorithm (and a comparison with other algorithms) may be 1720 * found by running the ::dtrace_dynstat MDB dcmd. 1721 */ 1722 for (i = 0; i < nkeys; i++) { 1723 if (key[i].dttk_size == 0) { 1724 uint64_t val = key[i].dttk_value; 1725 1726 hashval += (val >> 48) & 0xffff; 1727 hashval += (hashval << 10); 1728 hashval ^= (hashval >> 6); 1729 1730 hashval += (val >> 32) & 0xffff; 1731 hashval += (hashval << 10); 1732 hashval ^= (hashval >> 6); 1733 1734 hashval += (val >> 16) & 0xffff; 1735 hashval += (hashval << 10); 1736 hashval ^= (hashval >> 6); 1737 1738 hashval += val & 0xffff; 1739 hashval += (hashval << 10); 1740 hashval ^= (hashval >> 6); 1741 } else { 1742 /* 1743 * This is incredibly painful, but it beats the hell 1744 * out of the alternative. 1745 */ 1746 uint64_t j, size = key[i].dttk_size; 1747 uintptr_t base = (uintptr_t)key[i].dttk_value; 1748 1749 if (!dtrace_canload(base, size, mstate, vstate)) 1750 break; 1751 1752 for (j = 0; j < size; j++) { 1753 hashval += dtrace_load8(base + j); 1754 hashval += (hashval << 10); 1755 hashval ^= (hashval >> 6); 1756 } 1757 } 1758 } 1759 1760 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1761 return (NULL); 1762 1763 hashval += (hashval << 3); 1764 hashval ^= (hashval >> 11); 1765 hashval += (hashval << 15); 1766 1767 /* 1768 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1769 * comes out to be one of our two sentinel hash values. If this 1770 * actually happens, we set the hashval to be a value known to be a 1771 * non-sentinel value. 1772 */ 1773 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1774 hashval = DTRACE_DYNHASH_VALID; 1775 1776 /* 1777 * Yes, it's painful to do a divide here. If the cycle count becomes 1778 * important here, tricks can be pulled to reduce it. (However, it's 1779 * critical that hash collisions be kept to an absolute minimum; 1780 * they're much more painful than a divide.) It's better to have a 1781 * solution that generates few collisions and still keeps things 1782 * relatively simple. 1783 */ 1784 bucket = hashval % dstate->dtds_hashsize; 1785 1786 if (op == DTRACE_DYNVAR_DEALLOC) { 1787 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1788 1789 for (;;) { 1790 while ((lock = *lockp) & 1) 1791 continue; 1792 1793 if (dtrace_casptr((volatile void *)lockp, 1794 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1795 break; 1796 } 1797 1798 dtrace_membar_producer(); 1799 } 1800 1801top: 1802 prev = NULL; 1803 lock = hash[bucket].dtdh_lock; 1804 1805 dtrace_membar_consumer(); 1806 1807 start = hash[bucket].dtdh_chain; 1808 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1809 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1810 op != DTRACE_DYNVAR_DEALLOC)); 1811 1812 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1813 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1814 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1815 1816 if (dvar->dtdv_hashval != hashval) { 1817 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1818 /* 1819 * We've reached the sink, and therefore the 1820 * end of the hash chain; we can kick out of 1821 * the loop knowing that we have seen a valid 1822 * snapshot of state. 1823 */ 1824 ASSERT(dvar->dtdv_next == NULL); 1825 ASSERT(dvar == &dtrace_dynhash_sink); 1826 break; 1827 } 1828 1829 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1830 /* 1831 * We've gone off the rails: somewhere along 1832 * the line, one of the members of this hash 1833 * chain was deleted. Note that we could also 1834 * detect this by simply letting this loop run 1835 * to completion, as we would eventually hit 1836 * the end of the dirty list. However, we 1837 * want to avoid running the length of the 1838 * dirty list unnecessarily (it might be quite 1839 * long), so we catch this as early as 1840 * possible by detecting the hash marker. In 1841 * this case, we simply set dvar to NULL and 1842 * break; the conditional after the loop will 1843 * send us back to top. 1844 */ 1845 dvar = NULL; 1846 break; 1847 } 1848 1849 goto next; 1850 } 1851 1852 if (dtuple->dtt_nkeys != nkeys) 1853 goto next; 1854 1855 for (i = 0; i < nkeys; i++, dkey++) { 1856 if (dkey->dttk_size != key[i].dttk_size) 1857 goto next; /* size or type mismatch */ 1858 1859 if (dkey->dttk_size != 0) { 1860 if (dtrace_bcmp( 1861 (void *)(uintptr_t)key[i].dttk_value, 1862 (void *)(uintptr_t)dkey->dttk_value, 1863 dkey->dttk_size)) 1864 goto next; 1865 } else { 1866 if (dkey->dttk_value != key[i].dttk_value) 1867 goto next; 1868 } 1869 } 1870 1871 if (op != DTRACE_DYNVAR_DEALLOC) 1872 return (dvar); 1873 1874 ASSERT(dvar->dtdv_next == NULL || 1875 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1876 1877 if (prev != NULL) { 1878 ASSERT(hash[bucket].dtdh_chain != dvar); 1879 ASSERT(start != dvar); 1880 ASSERT(prev->dtdv_next == dvar); 1881 prev->dtdv_next = dvar->dtdv_next; 1882 } else { 1883 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1884 start, dvar->dtdv_next) != start) { 1885 /* 1886 * We have failed to atomically swing the 1887 * hash table head pointer, presumably because 1888 * of a conflicting allocation on another CPU. 1889 * We need to reread the hash chain and try 1890 * again. 1891 */ 1892 goto top; 1893 } 1894 } 1895 1896 dtrace_membar_producer(); 1897 1898 /* 1899 * Now set the hash value to indicate that it's free. 1900 */ 1901 ASSERT(hash[bucket].dtdh_chain != dvar); 1902 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1903 1904 dtrace_membar_producer(); 1905 1906 /* 1907 * Set the next pointer to point at the dirty list, and 1908 * atomically swing the dirty pointer to the newly freed dvar. 1909 */ 1910 do { 1911 next = dcpu->dtdsc_dirty; 1912 dvar->dtdv_next = next; 1913 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1914 1915 /* 1916 * Finally, unlock this hash bucket. 1917 */ 1918 ASSERT(hash[bucket].dtdh_lock == lock); 1919 ASSERT(lock & 1); 1920 hash[bucket].dtdh_lock++; 1921 1922 return (NULL); 1923next: 1924 prev = dvar; 1925 continue; 1926 } 1927 1928 if (dvar == NULL) { 1929 /* 1930 * If dvar is NULL, it is because we went off the rails: 1931 * one of the elements that we traversed in the hash chain 1932 * was deleted while we were traversing it. In this case, 1933 * we assert that we aren't doing a dealloc (deallocs lock 1934 * the hash bucket to prevent themselves from racing with 1935 * one another), and retry the hash chain traversal. 1936 */ 1937 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1938 goto top; 1939 } 1940 1941 if (op != DTRACE_DYNVAR_ALLOC) { 1942 /* 1943 * If we are not to allocate a new variable, we want to 1944 * return NULL now. Before we return, check that the value 1945 * of the lock word hasn't changed. If it has, we may have 1946 * seen an inconsistent snapshot. 1947 */ 1948 if (op == DTRACE_DYNVAR_NOALLOC) { 1949 if (hash[bucket].dtdh_lock != lock) 1950 goto top; 1951 } else { 1952 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1953 ASSERT(hash[bucket].dtdh_lock == lock); 1954 ASSERT(lock & 1); 1955 hash[bucket].dtdh_lock++; 1956 } 1957 1958 return (NULL); 1959 } 1960 1961 /* 1962 * We need to allocate a new dynamic variable. The size we need is the 1963 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1964 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1965 * the size of any referred-to data (dsize). We then round the final 1966 * size up to the chunksize for allocation. 1967 */ 1968 for (ksize = 0, i = 0; i < nkeys; i++) 1969 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1970 1971 /* 1972 * This should be pretty much impossible, but could happen if, say, 1973 * strange DIF specified the tuple. Ideally, this should be an 1974 * assertion and not an error condition -- but that requires that the 1975 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1976 * bullet-proof. (That is, it must not be able to be fooled by 1977 * malicious DIF.) Given the lack of backwards branches in DIF, 1978 * solving this would presumably not amount to solving the Halting 1979 * Problem -- but it still seems awfully hard. 1980 */ 1981 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1982 ksize + dsize > chunksize) { 1983 dcpu->dtdsc_drops++; 1984 return (NULL); 1985 } 1986 1987 nstate = DTRACE_DSTATE_EMPTY; 1988 1989 do { 1990retry: 1991 free = dcpu->dtdsc_free; 1992 1993 if (free == NULL) { 1994 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1995 void *rval; 1996 1997 if (clean == NULL) { 1998 /* 1999 * We're out of dynamic variable space on 2000 * this CPU. Unless we have tried all CPUs, 2001 * we'll try to allocate from a different 2002 * CPU. 2003 */ 2004 switch (dstate->dtds_state) { 2005 case DTRACE_DSTATE_CLEAN: { 2006 void *sp = &dstate->dtds_state; 2007 2008 if (++cpu >= NCPU) 2009 cpu = 0; 2010 2011 if (dcpu->dtdsc_dirty != NULL && 2012 nstate == DTRACE_DSTATE_EMPTY) 2013 nstate = DTRACE_DSTATE_DIRTY; 2014 2015 if (dcpu->dtdsc_rinsing != NULL) 2016 nstate = DTRACE_DSTATE_RINSING; 2017 2018 dcpu = &dstate->dtds_percpu[cpu]; 2019 2020 if (cpu != me) 2021 goto retry; 2022 2023 (void) dtrace_cas32(sp, 2024 DTRACE_DSTATE_CLEAN, nstate); 2025 2026 /* 2027 * To increment the correct bean 2028 * counter, take another lap. 2029 */ 2030 goto retry; 2031 } 2032 2033 case DTRACE_DSTATE_DIRTY: 2034 dcpu->dtdsc_dirty_drops++; 2035 break; 2036 2037 case DTRACE_DSTATE_RINSING: 2038 dcpu->dtdsc_rinsing_drops++; 2039 break; 2040 2041 case DTRACE_DSTATE_EMPTY: 2042 dcpu->dtdsc_drops++; 2043 break; 2044 } 2045 2046 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 2047 return (NULL); 2048 } 2049 2050 /* 2051 * The clean list appears to be non-empty. We want to 2052 * move the clean list to the free list; we start by 2053 * moving the clean pointer aside. 2054 */ 2055 if (dtrace_casptr(&dcpu->dtdsc_clean, 2056 clean, NULL) != clean) { 2057 /* 2058 * We are in one of two situations: 2059 * 2060 * (a) The clean list was switched to the 2061 * free list by another CPU. 2062 * 2063 * (b) The clean list was added to by the 2064 * cleansing cyclic. 2065 * 2066 * In either of these situations, we can 2067 * just reattempt the free list allocation. 2068 */ 2069 goto retry; 2070 } 2071 2072 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 2073 2074 /* 2075 * Now we'll move the clean list to our free list. 2076 * It's impossible for this to fail: the only way 2077 * the free list can be updated is through this 2078 * code path, and only one CPU can own the clean list. 2079 * Thus, it would only be possible for this to fail if 2080 * this code were racing with dtrace_dynvar_clean(). 2081 * (That is, if dtrace_dynvar_clean() updated the clean 2082 * list, and we ended up racing to update the free 2083 * list.) This race is prevented by the dtrace_sync() 2084 * in dtrace_dynvar_clean() -- which flushes the 2085 * owners of the clean lists out before resetting 2086 * the clean lists. 2087 */ 2088 dcpu = &dstate->dtds_percpu[me]; 2089 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 2090 ASSERT(rval == NULL); 2091 goto retry; 2092 } 2093 2094 dvar = free; 2095 new_free = dvar->dtdv_next; 2096 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 2097 2098 /* 2099 * We have now allocated a new chunk. We copy the tuple keys into the 2100 * tuple array and copy any referenced key data into the data space 2101 * following the tuple array. As we do this, we relocate dttk_value 2102 * in the final tuple to point to the key data address in the chunk. 2103 */ 2104 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 2105 dvar->dtdv_data = (void *)(kdata + ksize); 2106 dvar->dtdv_tuple.dtt_nkeys = nkeys; 2107 2108 for (i = 0; i < nkeys; i++) { 2109 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 2110 size_t kesize = key[i].dttk_size; 2111 2112 if (kesize != 0) { 2113 dtrace_bcopy( 2114 (const void *)(uintptr_t)key[i].dttk_value, 2115 (void *)kdata, kesize); 2116 dkey->dttk_value = kdata; 2117 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 2118 } else { 2119 dkey->dttk_value = key[i].dttk_value; 2120 } 2121 2122 dkey->dttk_size = kesize; 2123 } 2124 2125 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 2126 dvar->dtdv_hashval = hashval; 2127 dvar->dtdv_next = start; 2128 2129 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 2130 return (dvar); 2131 2132 /* 2133 * The cas has failed. Either another CPU is adding an element to 2134 * this hash chain, or another CPU is deleting an element from this 2135 * hash chain. The simplest way to deal with both of these cases 2136 * (though not necessarily the most efficient) is to free our 2137 * allocated block and tail-call ourselves. Note that the free is 2138 * to the dirty list and _not_ to the free list. This is to prevent 2139 * races with allocators, above. 2140 */ 2141 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2142 2143 dtrace_membar_producer(); 2144 2145 do { 2146 free = dcpu->dtdsc_dirty; 2147 dvar->dtdv_next = free; 2148 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 2149 2150 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 2151} 2152 2153/*ARGSUSED*/ 2154static void 2155dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 2156{ 2157 if ((int64_t)nval < (int64_t)*oval) 2158 *oval = nval; 2159} 2160 2161/*ARGSUSED*/ 2162static void 2163dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2164{ 2165 if ((int64_t)nval > (int64_t)*oval) 2166 *oval = nval; 2167} 2168 2169static void 2170dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2171{ 2172 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2173 int64_t val = (int64_t)nval; 2174 2175 if (val < 0) { 2176 for (i = 0; i < zero; i++) { 2177 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2178 quanta[i] += incr; 2179 return; 2180 } 2181 } 2182 } else { 2183 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2184 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2185 quanta[i - 1] += incr; 2186 return; 2187 } 2188 } 2189 2190 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2191 return; 2192 } 2193 2194 ASSERT(0); 2195} 2196 2197static void 2198dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2199{ 2200 uint64_t arg = *lquanta++; 2201 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2202 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2203 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2204 int32_t val = (int32_t)nval, level; 2205 2206 ASSERT(step != 0); 2207 ASSERT(levels != 0); 2208 2209 if (val < base) { 2210 /* 2211 * This is an underflow. 2212 */ 2213 lquanta[0] += incr; 2214 return; 2215 } 2216 2217 level = (val - base) / step; 2218 2219 if (level < levels) { 2220 lquanta[level + 1] += incr; 2221 return; 2222 } 2223 2224 /* 2225 * This is an overflow. 2226 */ 2227 lquanta[levels + 1] += incr; 2228} 2229 2230static int 2231dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2232 uint16_t high, uint16_t nsteps, int64_t value) 2233{ 2234 int64_t this = 1, last, next; 2235 int base = 1, order; 2236 2237 ASSERT(factor <= nsteps); 2238 ASSERT(nsteps % factor == 0); 2239 2240 for (order = 0; order < low; order++) 2241 this *= factor; 2242 2243 /* 2244 * If our value is less than our factor taken to the power of the 2245 * low order of magnitude, it goes into the zeroth bucket. 2246 */ 2247 if (value < (last = this)) 2248 return (0); 2249 2250 for (this *= factor; order <= high; order++) { 2251 int nbuckets = this > nsteps ? nsteps : this; 2252 2253 if ((next = this * factor) < this) { 2254 /* 2255 * We should not generally get log/linear quantizations 2256 * with a high magnitude that allows 64-bits to 2257 * overflow, but we nonetheless protect against this 2258 * by explicitly checking for overflow, and clamping 2259 * our value accordingly. 2260 */ 2261 value = this - 1; 2262 } 2263 2264 if (value < this) { 2265 /* 2266 * If our value lies within this order of magnitude, 2267 * determine its position by taking the offset within 2268 * the order of magnitude, dividing by the bucket 2269 * width, and adding to our (accumulated) base. 2270 */ 2271 return (base + (value - last) / (this / nbuckets)); 2272 } 2273 2274 base += nbuckets - (nbuckets / factor); 2275 last = this; 2276 this = next; 2277 } 2278 2279 /* 2280 * Our value is greater than or equal to our factor taken to the 2281 * power of one plus the high magnitude -- return the top bucket. 2282 */ 2283 return (base); 2284} 2285 2286static void 2287dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2288{ 2289 uint64_t arg = *llquanta++; 2290 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2291 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2292 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2293 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2294 2295 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2296 low, high, nsteps, nval)] += incr; 2297} 2298 2299/*ARGSUSED*/ 2300static void 2301dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2302{ 2303 data[0]++; 2304 data[1] += nval; 2305} 2306 2307/*ARGSUSED*/ 2308static void 2309dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2310{ 2311 int64_t snval = (int64_t)nval; 2312 uint64_t tmp[2]; 2313 2314 data[0]++; 2315 data[1] += nval; 2316 2317 /* 2318 * What we want to say here is: 2319 * 2320 * data[2] += nval * nval; 2321 * 2322 * But given that nval is 64-bit, we could easily overflow, so 2323 * we do this as 128-bit arithmetic. 2324 */ 2325 if (snval < 0) 2326 snval = -snval; 2327 2328 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2329 dtrace_add_128(data + 2, tmp, data + 2); 2330} 2331 2332/*ARGSUSED*/ 2333static void 2334dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2335{ 2336 *oval = *oval + 1; 2337} 2338 2339/*ARGSUSED*/ 2340static void 2341dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2342{ 2343 *oval += nval; 2344} 2345 2346/* 2347 * Aggregate given the tuple in the principal data buffer, and the aggregating 2348 * action denoted by the specified dtrace_aggregation_t. The aggregation 2349 * buffer is specified as the buf parameter. This routine does not return 2350 * failure; if there is no space in the aggregation buffer, the data will be 2351 * dropped, and a corresponding counter incremented. 2352 */ 2353static void 2354dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2355 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2356{ 2357 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2358 uint32_t i, ndx, size, fsize; 2359 uint32_t align = sizeof (uint64_t) - 1; 2360 dtrace_aggbuffer_t *agb; 2361 dtrace_aggkey_t *key; 2362 uint32_t hashval = 0, limit, isstr; 2363 caddr_t tomax, data, kdata; 2364 dtrace_actkind_t action; 2365 dtrace_action_t *act; 2366 uintptr_t offs; 2367 2368 if (buf == NULL) 2369 return; 2370 2371 if (!agg->dtag_hasarg) { 2372 /* 2373 * Currently, only quantize() and lquantize() take additional 2374 * arguments, and they have the same semantics: an increment 2375 * value that defaults to 1 when not present. If additional 2376 * aggregating actions take arguments, the setting of the 2377 * default argument value will presumably have to become more 2378 * sophisticated... 2379 */ 2380 arg = 1; 2381 } 2382 2383 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2384 size = rec->dtrd_offset - agg->dtag_base; 2385 fsize = size + rec->dtrd_size; 2386 2387 ASSERT(dbuf->dtb_tomax != NULL); 2388 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2389 2390 if ((tomax = buf->dtb_tomax) == NULL) { 2391 dtrace_buffer_drop(buf); 2392 return; 2393 } 2394 2395 /* 2396 * The metastructure is always at the bottom of the buffer. 2397 */ 2398 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2399 sizeof (dtrace_aggbuffer_t)); 2400 2401 if (buf->dtb_offset == 0) { 2402 /* 2403 * We just kludge up approximately 1/8th of the size to be 2404 * buckets. If this guess ends up being routinely 2405 * off-the-mark, we may need to dynamically readjust this 2406 * based on past performance. 2407 */ 2408 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2409 2410 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2411 (uintptr_t)tomax || hashsize == 0) { 2412 /* 2413 * We've been given a ludicrously small buffer; 2414 * increment our drop count and leave. 2415 */ 2416 dtrace_buffer_drop(buf); 2417 return; 2418 } 2419 2420 /* 2421 * And now, a pathetic attempt to try to get a an odd (or 2422 * perchance, a prime) hash size for better hash distribution. 2423 */ 2424 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2425 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2426 2427 agb->dtagb_hashsize = hashsize; 2428 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2429 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2430 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2431 2432 for (i = 0; i < agb->dtagb_hashsize; i++) 2433 agb->dtagb_hash[i] = NULL; 2434 } 2435 2436 ASSERT(agg->dtag_first != NULL); 2437 ASSERT(agg->dtag_first->dta_intuple); 2438 2439 /* 2440 * Calculate the hash value based on the key. Note that we _don't_ 2441 * include the aggid in the hashing (but we will store it as part of 2442 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2443 * algorithm: a simple, quick algorithm that has no known funnels, and 2444 * gets good distribution in practice. The efficacy of the hashing 2445 * algorithm (and a comparison with other algorithms) may be found by 2446 * running the ::dtrace_aggstat MDB dcmd. 2447 */ 2448 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2449 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2450 limit = i + act->dta_rec.dtrd_size; 2451 ASSERT(limit <= size); 2452 isstr = DTRACEACT_ISSTRING(act); 2453 2454 for (; i < limit; i++) { 2455 hashval += data[i]; 2456 hashval += (hashval << 10); 2457 hashval ^= (hashval >> 6); 2458 2459 if (isstr && data[i] == '\0') 2460 break; 2461 } 2462 } 2463 2464 hashval += (hashval << 3); 2465 hashval ^= (hashval >> 11); 2466 hashval += (hashval << 15); 2467 2468 /* 2469 * Yes, the divide here is expensive -- but it's generally the least 2470 * of the performance issues given the amount of data that we iterate 2471 * over to compute hash values, compare data, etc. 2472 */ 2473 ndx = hashval % agb->dtagb_hashsize; 2474 2475 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2476 ASSERT((caddr_t)key >= tomax); 2477 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2478 2479 if (hashval != key->dtak_hashval || key->dtak_size != size) 2480 continue; 2481 2482 kdata = key->dtak_data; 2483 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2484 2485 for (act = agg->dtag_first; act->dta_intuple; 2486 act = act->dta_next) { 2487 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2488 limit = i + act->dta_rec.dtrd_size; 2489 ASSERT(limit <= size); 2490 isstr = DTRACEACT_ISSTRING(act); 2491 2492 for (; i < limit; i++) { 2493 if (kdata[i] != data[i]) 2494 goto next; 2495 2496 if (isstr && data[i] == '\0') 2497 break; 2498 } 2499 } 2500 2501 if (action != key->dtak_action) { 2502 /* 2503 * We are aggregating on the same value in the same 2504 * aggregation with two different aggregating actions. 2505 * (This should have been picked up in the compiler, 2506 * so we may be dealing with errant or devious DIF.) 2507 * This is an error condition; we indicate as much, 2508 * and return. 2509 */ 2510 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2511 return; 2512 } 2513 2514 /* 2515 * This is a hit: we need to apply the aggregator to 2516 * the value at this key. 2517 */ 2518 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2519 return; 2520next: 2521 continue; 2522 } 2523 2524 /* 2525 * We didn't find it. We need to allocate some zero-filled space, 2526 * link it into the hash table appropriately, and apply the aggregator 2527 * to the (zero-filled) value. 2528 */ 2529 offs = buf->dtb_offset; 2530 while (offs & (align - 1)) 2531 offs += sizeof (uint32_t); 2532 2533 /* 2534 * If we don't have enough room to both allocate a new key _and_ 2535 * its associated data, increment the drop count and return. 2536 */ 2537 if ((uintptr_t)tomax + offs + fsize > 2538 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2539 dtrace_buffer_drop(buf); 2540 return; 2541 } 2542 2543 /*CONSTCOND*/ 2544 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2545 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2546 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2547 2548 key->dtak_data = kdata = tomax + offs; 2549 buf->dtb_offset = offs + fsize; 2550 2551 /* 2552 * Now copy the data across. 2553 */ 2554 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2555 2556 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2557 kdata[i] = data[i]; 2558 2559 /* 2560 * Because strings are not zeroed out by default, we need to iterate 2561 * looking for actions that store strings, and we need to explicitly 2562 * pad these strings out with zeroes. 2563 */ 2564 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2565 int nul; 2566 2567 if (!DTRACEACT_ISSTRING(act)) 2568 continue; 2569 2570 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2571 limit = i + act->dta_rec.dtrd_size; 2572 ASSERT(limit <= size); 2573 2574 for (nul = 0; i < limit; i++) { 2575 if (nul) { 2576 kdata[i] = '\0'; 2577 continue; 2578 } 2579 2580 if (data[i] != '\0') 2581 continue; 2582 2583 nul = 1; 2584 } 2585 } 2586 2587 for (i = size; i < fsize; i++) 2588 kdata[i] = 0; 2589 2590 key->dtak_hashval = hashval; 2591 key->dtak_size = size; 2592 key->dtak_action = action; 2593 key->dtak_next = agb->dtagb_hash[ndx]; 2594 agb->dtagb_hash[ndx] = key; 2595 2596 /* 2597 * Finally, apply the aggregator. 2598 */ 2599 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2600 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2601} 2602 2603/* 2604 * Given consumer state, this routine finds a speculation in the INACTIVE 2605 * state and transitions it into the ACTIVE state. If there is no speculation 2606 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2607 * incremented -- it is up to the caller to take appropriate action. 2608 */ 2609static int 2610dtrace_speculation(dtrace_state_t *state) 2611{ 2612 int i = 0; 2613 dtrace_speculation_state_t current; 2614 uint32_t *stat = &state->dts_speculations_unavail, count; 2615 2616 while (i < state->dts_nspeculations) { 2617 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2618 2619 current = spec->dtsp_state; 2620 2621 if (current != DTRACESPEC_INACTIVE) { 2622 if (current == DTRACESPEC_COMMITTINGMANY || 2623 current == DTRACESPEC_COMMITTING || 2624 current == DTRACESPEC_DISCARDING) 2625 stat = &state->dts_speculations_busy; 2626 i++; 2627 continue; 2628 } 2629 2630 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2631 current, DTRACESPEC_ACTIVE) == current) 2632 return (i + 1); 2633 } 2634 2635 /* 2636 * We couldn't find a speculation. If we found as much as a single 2637 * busy speculation buffer, we'll attribute this failure as "busy" 2638 * instead of "unavail". 2639 */ 2640 do { 2641 count = *stat; 2642 } while (dtrace_cas32(stat, count, count + 1) != count); 2643 2644 return (0); 2645} 2646 2647/* 2648 * This routine commits an active speculation. If the specified speculation 2649 * is not in a valid state to perform a commit(), this routine will silently do 2650 * nothing. The state of the specified speculation is transitioned according 2651 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2652 */ 2653static void 2654dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2655 dtrace_specid_t which) 2656{ 2657 dtrace_speculation_t *spec; 2658 dtrace_buffer_t *src, *dest; 2659 uintptr_t daddr, saddr, dlimit, slimit; 2660 dtrace_speculation_state_t current, new = 0; 2661 intptr_t offs; 2662 uint64_t timestamp; 2663 2664 if (which == 0) 2665 return; 2666 2667 if (which > state->dts_nspeculations) { 2668 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2669 return; 2670 } 2671 2672 spec = &state->dts_speculations[which - 1]; 2673 src = &spec->dtsp_buffer[cpu]; 2674 dest = &state->dts_buffer[cpu]; 2675 2676 do { 2677 current = spec->dtsp_state; 2678 2679 if (current == DTRACESPEC_COMMITTINGMANY) 2680 break; 2681 2682 switch (current) { 2683 case DTRACESPEC_INACTIVE: 2684 case DTRACESPEC_DISCARDING: 2685 return; 2686 2687 case DTRACESPEC_COMMITTING: 2688 /* 2689 * This is only possible if we are (a) commit()'ing 2690 * without having done a prior speculate() on this CPU 2691 * and (b) racing with another commit() on a different 2692 * CPU. There's nothing to do -- we just assert that 2693 * our offset is 0. 2694 */ 2695 ASSERT(src->dtb_offset == 0); 2696 return; 2697 2698 case DTRACESPEC_ACTIVE: 2699 new = DTRACESPEC_COMMITTING; 2700 break; 2701 2702 case DTRACESPEC_ACTIVEONE: 2703 /* 2704 * This speculation is active on one CPU. If our 2705 * buffer offset is non-zero, we know that the one CPU 2706 * must be us. Otherwise, we are committing on a 2707 * different CPU from the speculate(), and we must 2708 * rely on being asynchronously cleaned. 2709 */ 2710 if (src->dtb_offset != 0) { 2711 new = DTRACESPEC_COMMITTING; 2712 break; 2713 } 2714 /*FALLTHROUGH*/ 2715 2716 case DTRACESPEC_ACTIVEMANY: 2717 new = DTRACESPEC_COMMITTINGMANY; 2718 break; 2719 2720 default: 2721 ASSERT(0); 2722 } 2723 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2724 current, new) != current); 2725 2726 /* 2727 * We have set the state to indicate that we are committing this 2728 * speculation. Now reserve the necessary space in the destination 2729 * buffer. 2730 */ 2731 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2732 sizeof (uint64_t), state, NULL)) < 0) { 2733 dtrace_buffer_drop(dest); 2734 goto out; 2735 } 2736 2737 /* 2738 * We have sufficient space to copy the speculative buffer into the 2739 * primary buffer. First, modify the speculative buffer, filling 2740 * in the timestamp of all entries with the current time. The data 2741 * must have the commit() time rather than the time it was traced, 2742 * so that all entries in the primary buffer are in timestamp order. 2743 */ 2744 timestamp = dtrace_gethrtime(); 2745 saddr = (uintptr_t)src->dtb_tomax; 2746 slimit = saddr + src->dtb_offset; 2747 while (saddr < slimit) { 2748 size_t size; 2749 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2750 2751 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2752 saddr += sizeof (dtrace_epid_t); 2753 continue; 2754 } 2755 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2756 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2757 2758 ASSERT3U(saddr + size, <=, slimit); 2759 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2760 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2761 2762 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2763 2764 saddr += size; 2765 } 2766 2767 /* 2768 * Copy the buffer across. (Note that this is a 2769 * highly subobtimal bcopy(); in the unlikely event that this becomes 2770 * a serious performance issue, a high-performance DTrace-specific 2771 * bcopy() should obviously be invented.) 2772 */ 2773 daddr = (uintptr_t)dest->dtb_tomax + offs; 2774 dlimit = daddr + src->dtb_offset; 2775 saddr = (uintptr_t)src->dtb_tomax; 2776 2777 /* 2778 * First, the aligned portion. 2779 */ 2780 while (dlimit - daddr >= sizeof (uint64_t)) { 2781 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2782 2783 daddr += sizeof (uint64_t); 2784 saddr += sizeof (uint64_t); 2785 } 2786 2787 /* 2788 * Now any left-over bit... 2789 */ 2790 while (dlimit - daddr) 2791 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2792 2793 /* 2794 * Finally, commit the reserved space in the destination buffer. 2795 */ 2796 dest->dtb_offset = offs + src->dtb_offset; 2797 2798out: 2799 /* 2800 * If we're lucky enough to be the only active CPU on this speculation 2801 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2802 */ 2803 if (current == DTRACESPEC_ACTIVE || 2804 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2805 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2806 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2807 2808 ASSERT(rval == DTRACESPEC_COMMITTING); 2809 } 2810 2811 src->dtb_offset = 0; 2812 src->dtb_xamot_drops += src->dtb_drops; 2813 src->dtb_drops = 0; 2814} 2815 2816/* 2817 * This routine discards an active speculation. If the specified speculation 2818 * is not in a valid state to perform a discard(), this routine will silently 2819 * do nothing. The state of the specified speculation is transitioned 2820 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2821 */ 2822static void 2823dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2824 dtrace_specid_t which) 2825{ 2826 dtrace_speculation_t *spec; 2827 dtrace_speculation_state_t current, new = 0; 2828 dtrace_buffer_t *buf; 2829 2830 if (which == 0) 2831 return; 2832 2833 if (which > state->dts_nspeculations) { 2834 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2835 return; 2836 } 2837 2838 spec = &state->dts_speculations[which - 1]; 2839 buf = &spec->dtsp_buffer[cpu]; 2840 2841 do { 2842 current = spec->dtsp_state; 2843 2844 switch (current) { 2845 case DTRACESPEC_INACTIVE: 2846 case DTRACESPEC_COMMITTINGMANY: 2847 case DTRACESPEC_COMMITTING: 2848 case DTRACESPEC_DISCARDING: 2849 return; 2850 2851 case DTRACESPEC_ACTIVE: 2852 case DTRACESPEC_ACTIVEMANY: 2853 new = DTRACESPEC_DISCARDING; 2854 break; 2855 2856 case DTRACESPEC_ACTIVEONE: 2857 if (buf->dtb_offset != 0) { 2858 new = DTRACESPEC_INACTIVE; 2859 } else { 2860 new = DTRACESPEC_DISCARDING; 2861 } 2862 break; 2863 2864 default: 2865 ASSERT(0); 2866 } 2867 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2868 current, new) != current); 2869 2870 buf->dtb_offset = 0; 2871 buf->dtb_drops = 0; 2872} 2873 2874/* 2875 * Note: not called from probe context. This function is called 2876 * asynchronously from cross call context to clean any speculations that are 2877 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2878 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2879 * speculation. 2880 */ 2881static void 2882dtrace_speculation_clean_here(dtrace_state_t *state) 2883{ 2884 dtrace_icookie_t cookie; 2885 processorid_t cpu = curcpu; 2886 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2887 dtrace_specid_t i; 2888 2889 cookie = dtrace_interrupt_disable(); 2890 2891 if (dest->dtb_tomax == NULL) { 2892 dtrace_interrupt_enable(cookie); 2893 return; 2894 } 2895 2896 for (i = 0; i < state->dts_nspeculations; i++) { 2897 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2898 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2899 2900 if (src->dtb_tomax == NULL) 2901 continue; 2902 2903 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2904 src->dtb_offset = 0; 2905 continue; 2906 } 2907 2908 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2909 continue; 2910 2911 if (src->dtb_offset == 0) 2912 continue; 2913 2914 dtrace_speculation_commit(state, cpu, i + 1); 2915 } 2916 2917 dtrace_interrupt_enable(cookie); 2918} 2919 2920/* 2921 * Note: not called from probe context. This function is called 2922 * asynchronously (and at a regular interval) to clean any speculations that 2923 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2924 * is work to be done, it cross calls all CPUs to perform that work; 2925 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2926 * INACTIVE state until they have been cleaned by all CPUs. 2927 */ 2928static void 2929dtrace_speculation_clean(dtrace_state_t *state) 2930{ 2931 int work = 0, rv; 2932 dtrace_specid_t i; 2933 2934 for (i = 0; i < state->dts_nspeculations; i++) { 2935 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2936 2937 ASSERT(!spec->dtsp_cleaning); 2938 2939 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2940 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2941 continue; 2942 2943 work++; 2944 spec->dtsp_cleaning = 1; 2945 } 2946 2947 if (!work) 2948 return; 2949 2950 dtrace_xcall(DTRACE_CPUALL, 2951 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2952 2953 /* 2954 * We now know that all CPUs have committed or discarded their 2955 * speculation buffers, as appropriate. We can now set the state 2956 * to inactive. 2957 */ 2958 for (i = 0; i < state->dts_nspeculations; i++) { 2959 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2960 dtrace_speculation_state_t current, new; 2961 2962 if (!spec->dtsp_cleaning) 2963 continue; 2964 2965 current = spec->dtsp_state; 2966 ASSERT(current == DTRACESPEC_DISCARDING || 2967 current == DTRACESPEC_COMMITTINGMANY); 2968 2969 new = DTRACESPEC_INACTIVE; 2970 2971 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2972 ASSERT(rv == current); 2973 spec->dtsp_cleaning = 0; 2974 } 2975} 2976 2977/* 2978 * Called as part of a speculate() to get the speculative buffer associated 2979 * with a given speculation. Returns NULL if the specified speculation is not 2980 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2981 * the active CPU is not the specified CPU -- the speculation will be 2982 * atomically transitioned into the ACTIVEMANY state. 2983 */ 2984static dtrace_buffer_t * 2985dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2986 dtrace_specid_t which) 2987{ 2988 dtrace_speculation_t *spec; 2989 dtrace_speculation_state_t current, new = 0; 2990 dtrace_buffer_t *buf; 2991 2992 if (which == 0) 2993 return (NULL); 2994 2995 if (which > state->dts_nspeculations) { 2996 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2997 return (NULL); 2998 } 2999 3000 spec = &state->dts_speculations[which - 1]; 3001 buf = &spec->dtsp_buffer[cpuid]; 3002 3003 do { 3004 current = spec->dtsp_state; 3005 3006 switch (current) { 3007 case DTRACESPEC_INACTIVE: 3008 case DTRACESPEC_COMMITTINGMANY: 3009 case DTRACESPEC_DISCARDING: 3010 return (NULL); 3011 3012 case DTRACESPEC_COMMITTING: 3013 ASSERT(buf->dtb_offset == 0); 3014 return (NULL); 3015 3016 case DTRACESPEC_ACTIVEONE: 3017 /* 3018 * This speculation is currently active on one CPU. 3019 * Check the offset in the buffer; if it's non-zero, 3020 * that CPU must be us (and we leave the state alone). 3021 * If it's zero, assume that we're starting on a new 3022 * CPU -- and change the state to indicate that the 3023 * speculation is active on more than one CPU. 3024 */ 3025 if (buf->dtb_offset != 0) 3026 return (buf); 3027 3028 new = DTRACESPEC_ACTIVEMANY; 3029 break; 3030 3031 case DTRACESPEC_ACTIVEMANY: 3032 return (buf); 3033 3034 case DTRACESPEC_ACTIVE: 3035 new = DTRACESPEC_ACTIVEONE; 3036 break; 3037 3038 default: 3039 ASSERT(0); 3040 } 3041 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3042 current, new) != current); 3043 3044 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 3045 return (buf); 3046} 3047 3048/* 3049 * Return a string. In the event that the user lacks the privilege to access 3050 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3051 * don't fail access checking. 3052 * 3053 * dtrace_dif_variable() uses this routine as a helper for various 3054 * builtin values such as 'execname' and 'probefunc.' 3055 */ 3056uintptr_t 3057dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 3058 dtrace_mstate_t *mstate) 3059{ 3060 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3061 uintptr_t ret; 3062 size_t strsz; 3063 3064 /* 3065 * The easy case: this probe is allowed to read all of memory, so 3066 * we can just return this as a vanilla pointer. 3067 */ 3068 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 3069 return (addr); 3070 3071 /* 3072 * This is the tougher case: we copy the string in question from 3073 * kernel memory into scratch memory and return it that way: this 3074 * ensures that we won't trip up when access checking tests the 3075 * BYREF return value. 3076 */ 3077 strsz = dtrace_strlen((char *)addr, size) + 1; 3078 3079 if (mstate->dtms_scratch_ptr + strsz > 3080 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3081 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3082 return (0); 3083 } 3084 3085 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3086 strsz); 3087 ret = mstate->dtms_scratch_ptr; 3088 mstate->dtms_scratch_ptr += strsz; 3089 return (ret); 3090} 3091 3092/* 3093 * Return a string from a memoy address which is known to have one or 3094 * more concatenated, individually zero terminated, sub-strings. 3095 * In the event that the user lacks the privilege to access 3096 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3097 * don't fail access checking. 3098 * 3099 * dtrace_dif_variable() uses this routine as a helper for various 3100 * builtin values such as 'execargs'. 3101 */ 3102static uintptr_t 3103dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 3104 dtrace_mstate_t *mstate) 3105{ 3106 char *p; 3107 size_t i; 3108 uintptr_t ret; 3109 3110 if (mstate->dtms_scratch_ptr + strsz > 3111 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3112 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3113 return (0); 3114 } 3115 3116 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3117 strsz); 3118 3119 /* Replace sub-string termination characters with a space. */ 3120 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 3121 p++, i++) 3122 if (*p == '\0') 3123 *p = ' '; 3124 3125 ret = mstate->dtms_scratch_ptr; 3126 mstate->dtms_scratch_ptr += strsz; 3127 return (ret); 3128} 3129 3130/* 3131 * This function implements the DIF emulator's variable lookups. The emulator 3132 * passes a reserved variable identifier and optional built-in array index. 3133 */ 3134static uint64_t 3135dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 3136 uint64_t ndx) 3137{ 3138 /* 3139 * If we're accessing one of the uncached arguments, we'll turn this 3140 * into a reference in the args array. 3141 */ 3142 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 3143 ndx = v - DIF_VAR_ARG0; 3144 v = DIF_VAR_ARGS; 3145 } 3146 3147 switch (v) { 3148 case DIF_VAR_ARGS: 3149 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 3150 if (ndx >= sizeof (mstate->dtms_arg) / 3151 sizeof (mstate->dtms_arg[0])) { 3152 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3153 dtrace_provider_t *pv; 3154 uint64_t val; 3155 3156 pv = mstate->dtms_probe->dtpr_provider; 3157 if (pv->dtpv_pops.dtps_getargval != NULL) 3158 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 3159 mstate->dtms_probe->dtpr_id, 3160 mstate->dtms_probe->dtpr_arg, ndx, aframes); 3161 else 3162 val = dtrace_getarg(ndx, aframes); 3163 3164 /* 3165 * This is regrettably required to keep the compiler 3166 * from tail-optimizing the call to dtrace_getarg(). 3167 * The condition always evaluates to true, but the 3168 * compiler has no way of figuring that out a priori. 3169 * (None of this would be necessary if the compiler 3170 * could be relied upon to _always_ tail-optimize 3171 * the call to dtrace_getarg() -- but it can't.) 3172 */ 3173 if (mstate->dtms_probe != NULL) 3174 return (val); 3175 3176 ASSERT(0); 3177 } 3178 3179 return (mstate->dtms_arg[ndx]); 3180 3181#if defined(sun) 3182 case DIF_VAR_UREGS: { 3183 klwp_t *lwp; 3184 3185 if (!dtrace_priv_proc(state)) 3186 return (0); 3187 3188 if ((lwp = curthread->t_lwp) == NULL) { 3189 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3190 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 3191 return (0); 3192 } 3193 3194 return (dtrace_getreg(lwp->lwp_regs, ndx)); 3195 return (0); 3196 } 3197#else 3198 case DIF_VAR_UREGS: { 3199 struct trapframe *tframe; 3200 3201 if (!dtrace_priv_proc(state)) 3202 return (0); 3203 3204 if ((tframe = curthread->td_frame) == NULL) { 3205 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3206 cpu_core[curcpu].cpuc_dtrace_illval = 0; 3207 return (0); 3208 } 3209 3210 return (dtrace_getreg(tframe, ndx)); 3211 } 3212#endif 3213 3214 case DIF_VAR_CURTHREAD: 3215 if (!dtrace_priv_proc(state)) 3216 return (0); 3217 return ((uint64_t)(uintptr_t)curthread); 3218 3219 case DIF_VAR_TIMESTAMP: 3220 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 3221 mstate->dtms_timestamp = dtrace_gethrtime(); 3222 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 3223 } 3224 return (mstate->dtms_timestamp); 3225 3226 case DIF_VAR_VTIMESTAMP: 3227 ASSERT(dtrace_vtime_references != 0); 3228 return (curthread->t_dtrace_vtime); 3229 3230 case DIF_VAR_WALLTIMESTAMP: 3231 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3232 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3233 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3234 } 3235 return (mstate->dtms_walltimestamp); 3236 3237#if defined(sun) 3238 case DIF_VAR_IPL: 3239 if (!dtrace_priv_kernel(state)) 3240 return (0); 3241 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3242 mstate->dtms_ipl = dtrace_getipl(); 3243 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3244 } 3245 return (mstate->dtms_ipl); 3246#endif 3247 3248 case DIF_VAR_EPID: 3249 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3250 return (mstate->dtms_epid); 3251 3252 case DIF_VAR_ID: 3253 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3254 return (mstate->dtms_probe->dtpr_id); 3255 3256 case DIF_VAR_STACKDEPTH: 3257 if (!dtrace_priv_kernel(state)) 3258 return (0); 3259 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3260 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3261 3262 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3263 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3264 } 3265 return (mstate->dtms_stackdepth); 3266 3267 case DIF_VAR_USTACKDEPTH: 3268 if (!dtrace_priv_proc(state)) 3269 return (0); 3270 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3271 /* 3272 * See comment in DIF_VAR_PID. 3273 */ 3274 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3275 CPU_ON_INTR(CPU)) { 3276 mstate->dtms_ustackdepth = 0; 3277 } else { 3278 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3279 mstate->dtms_ustackdepth = 3280 dtrace_getustackdepth(); 3281 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3282 } 3283 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3284 } 3285 return (mstate->dtms_ustackdepth); 3286 3287 case DIF_VAR_CALLER: 3288 if (!dtrace_priv_kernel(state)) 3289 return (0); 3290 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3291 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3292 3293 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3294 /* 3295 * If this is an unanchored probe, we are 3296 * required to go through the slow path: 3297 * dtrace_caller() only guarantees correct 3298 * results for anchored probes. 3299 */ 3300 pc_t caller[2] = {0, 0}; 3301 3302 dtrace_getpcstack(caller, 2, aframes, 3303 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3304 mstate->dtms_caller = caller[1]; 3305 } else if ((mstate->dtms_caller = 3306 dtrace_caller(aframes)) == -1) { 3307 /* 3308 * We have failed to do this the quick way; 3309 * we must resort to the slower approach of 3310 * calling dtrace_getpcstack(). 3311 */ 3312 pc_t caller = 0; 3313 3314 dtrace_getpcstack(&caller, 1, aframes, NULL); 3315 mstate->dtms_caller = caller; 3316 } 3317 3318 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3319 } 3320 return (mstate->dtms_caller); 3321 3322 case DIF_VAR_UCALLER: 3323 if (!dtrace_priv_proc(state)) 3324 return (0); 3325 3326 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3327 uint64_t ustack[3]; 3328 3329 /* 3330 * dtrace_getupcstack() fills in the first uint64_t 3331 * with the current PID. The second uint64_t will 3332 * be the program counter at user-level. The third 3333 * uint64_t will contain the caller, which is what 3334 * we're after. 3335 */ 3336 ustack[2] = 0; 3337 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3338 dtrace_getupcstack(ustack, 3); 3339 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3340 mstate->dtms_ucaller = ustack[2]; 3341 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3342 } 3343 3344 return (mstate->dtms_ucaller); 3345 3346 case DIF_VAR_PROBEPROV: 3347 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3348 return (dtrace_dif_varstr( 3349 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3350 state, mstate)); 3351 3352 case DIF_VAR_PROBEMOD: 3353 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3354 return (dtrace_dif_varstr( 3355 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3356 state, mstate)); 3357 3358 case DIF_VAR_PROBEFUNC: 3359 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3360 return (dtrace_dif_varstr( 3361 (uintptr_t)mstate->dtms_probe->dtpr_func, 3362 state, mstate)); 3363 3364 case DIF_VAR_PROBENAME: 3365 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3366 return (dtrace_dif_varstr( 3367 (uintptr_t)mstate->dtms_probe->dtpr_name, 3368 state, mstate)); 3369 3370 case DIF_VAR_PID: 3371 if (!dtrace_priv_proc(state)) 3372 return (0); 3373 3374#if defined(sun) 3375 /* 3376 * Note that we are assuming that an unanchored probe is 3377 * always due to a high-level interrupt. (And we're assuming 3378 * that there is only a single high level interrupt.) 3379 */ 3380 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3381 return (pid0.pid_id); 3382 3383 /* 3384 * It is always safe to dereference one's own t_procp pointer: 3385 * it always points to a valid, allocated proc structure. 3386 * Further, it is always safe to dereference the p_pidp member 3387 * of one's own proc structure. (These are truisms becuase 3388 * threads and processes don't clean up their own state -- 3389 * they leave that task to whomever reaps them.) 3390 */ 3391 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3392#else 3393 return ((uint64_t)curproc->p_pid); 3394#endif 3395 3396 case DIF_VAR_PPID: 3397 if (!dtrace_priv_proc(state)) 3398 return (0); 3399 3400#if defined(sun) 3401 /* 3402 * See comment in DIF_VAR_PID. 3403 */ 3404 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3405 return (pid0.pid_id); 3406 3407 /* 3408 * It is always safe to dereference one's own t_procp pointer: 3409 * it always points to a valid, allocated proc structure. 3410 * (This is true because threads don't clean up their own 3411 * state -- they leave that task to whomever reaps them.) 3412 */ 3413 return ((uint64_t)curthread->t_procp->p_ppid); 3414#else 3415 return ((uint64_t)curproc->p_pptr->p_pid); 3416#endif 3417 3418 case DIF_VAR_TID: 3419#if defined(sun) 3420 /* 3421 * See comment in DIF_VAR_PID. 3422 */ 3423 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3424 return (0); 3425#endif 3426 3427 return ((uint64_t)curthread->t_tid); 3428 3429 case DIF_VAR_EXECARGS: { 3430 struct pargs *p_args = curthread->td_proc->p_args; 3431 3432 if (p_args == NULL) 3433 return(0); 3434 3435 return (dtrace_dif_varstrz( 3436 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3437 } 3438 3439 case DIF_VAR_EXECNAME: 3440#if defined(sun) 3441 if (!dtrace_priv_proc(state)) 3442 return (0); 3443 3444 /* 3445 * See comment in DIF_VAR_PID. 3446 */ 3447 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3448 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3449 3450 /* 3451 * It is always safe to dereference one's own t_procp pointer: 3452 * it always points to a valid, allocated proc structure. 3453 * (This is true because threads don't clean up their own 3454 * state -- they leave that task to whomever reaps them.) 3455 */ 3456 return (dtrace_dif_varstr( 3457 (uintptr_t)curthread->t_procp->p_user.u_comm, 3458 state, mstate)); 3459#else 3460 return (dtrace_dif_varstr( 3461 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3462#endif 3463 3464 case DIF_VAR_ZONENAME: 3465#if defined(sun) 3466 if (!dtrace_priv_proc(state)) 3467 return (0); 3468 3469 /* 3470 * See comment in DIF_VAR_PID. 3471 */ 3472 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3473 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3474 3475 /* 3476 * It is always safe to dereference one's own t_procp pointer: 3477 * it always points to a valid, allocated proc structure. 3478 * (This is true because threads don't clean up their own 3479 * state -- they leave that task to whomever reaps them.) 3480 */ 3481 return (dtrace_dif_varstr( 3482 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3483 state, mstate)); 3484#else 3485 return (0); 3486#endif 3487 3488 case DIF_VAR_UID: 3489 if (!dtrace_priv_proc(state)) 3490 return (0); 3491 3492#if defined(sun) 3493 /* 3494 * See comment in DIF_VAR_PID. 3495 */ 3496 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3497 return ((uint64_t)p0.p_cred->cr_uid); 3498#endif 3499 3500 /* 3501 * It is always safe to dereference one's own t_procp pointer: 3502 * it always points to a valid, allocated proc structure. 3503 * (This is true because threads don't clean up their own 3504 * state -- they leave that task to whomever reaps them.) 3505 * 3506 * Additionally, it is safe to dereference one's own process 3507 * credential, since this is never NULL after process birth. 3508 */ 3509 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3510 3511 case DIF_VAR_GID: 3512 if (!dtrace_priv_proc(state)) 3513 return (0); 3514 3515#if defined(sun) 3516 /* 3517 * See comment in DIF_VAR_PID. 3518 */ 3519 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3520 return ((uint64_t)p0.p_cred->cr_gid); 3521#endif 3522 3523 /* 3524 * It is always safe to dereference one's own t_procp pointer: 3525 * it always points to a valid, allocated proc structure. 3526 * (This is true because threads don't clean up their own 3527 * state -- they leave that task to whomever reaps them.) 3528 * 3529 * Additionally, it is safe to dereference one's own process 3530 * credential, since this is never NULL after process birth. 3531 */ 3532 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3533 3534 case DIF_VAR_ERRNO: { 3535#if defined(sun) 3536 klwp_t *lwp; 3537 if (!dtrace_priv_proc(state)) 3538 return (0); 3539 3540 /* 3541 * See comment in DIF_VAR_PID. 3542 */ 3543 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3544 return (0); 3545 3546 /* 3547 * It is always safe to dereference one's own t_lwp pointer in 3548 * the event that this pointer is non-NULL. (This is true 3549 * because threads and lwps don't clean up their own state -- 3550 * they leave that task to whomever reaps them.) 3551 */ 3552 if ((lwp = curthread->t_lwp) == NULL) 3553 return (0); 3554 3555 return ((uint64_t)lwp->lwp_errno); 3556#else 3557 return (curthread->td_errno); 3558#endif 3559 } 3560#if !defined(sun) 3561 case DIF_VAR_CPU: { 3562 return curcpu; 3563 } 3564#endif 3565 default: 3566 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3567 return (0); 3568 } 3569} 3570 3571 3572typedef enum dtrace_json_state { 3573 DTRACE_JSON_REST = 1, 3574 DTRACE_JSON_OBJECT, 3575 DTRACE_JSON_STRING, 3576 DTRACE_JSON_STRING_ESCAPE, 3577 DTRACE_JSON_STRING_ESCAPE_UNICODE, 3578 DTRACE_JSON_COLON, 3579 DTRACE_JSON_COMMA, 3580 DTRACE_JSON_VALUE, 3581 DTRACE_JSON_IDENTIFIER, 3582 DTRACE_JSON_NUMBER, 3583 DTRACE_JSON_NUMBER_FRAC, 3584 DTRACE_JSON_NUMBER_EXP, 3585 DTRACE_JSON_COLLECT_OBJECT 3586} dtrace_json_state_t; 3587 3588/* 3589 * This function possesses just enough knowledge about JSON to extract a single 3590 * value from a JSON string and store it in the scratch buffer. It is able 3591 * to extract nested object values, and members of arrays by index. 3592 * 3593 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to 3594 * be looked up as we descend into the object tree. e.g. 3595 * 3596 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL 3597 * with nelems = 5. 3598 * 3599 * The run time of this function must be bounded above by strsize to limit the 3600 * amount of work done in probe context. As such, it is implemented as a 3601 * simple state machine, reading one character at a time using safe loads 3602 * until we find the requested element, hit a parsing error or run off the 3603 * end of the object or string. 3604 * 3605 * As there is no way for a subroutine to return an error without interrupting 3606 * clause execution, we simply return NULL in the event of a missing key or any 3607 * other error condition. Each NULL return in this function is commented with 3608 * the error condition it represents -- parsing or otherwise. 3609 * 3610 * The set of states for the state machine closely matches the JSON 3611 * specification (http://json.org/). Briefly: 3612 * 3613 * DTRACE_JSON_REST: 3614 * Skip whitespace until we find either a top-level Object, moving 3615 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE. 3616 * 3617 * DTRACE_JSON_OBJECT: 3618 * Locate the next key String in an Object. Sets a flag to denote 3619 * the next String as a key string and moves to DTRACE_JSON_STRING. 3620 * 3621 * DTRACE_JSON_COLON: 3622 * Skip whitespace until we find the colon that separates key Strings 3623 * from their values. Once found, move to DTRACE_JSON_VALUE. 3624 * 3625 * DTRACE_JSON_VALUE: 3626 * Detects the type of the next value (String, Number, Identifier, Object 3627 * or Array) and routes to the states that process that type. Here we also 3628 * deal with the element selector list if we are requested to traverse down 3629 * into the object tree. 3630 * 3631 * DTRACE_JSON_COMMA: 3632 * Skip whitespace until we find the comma that separates key-value pairs 3633 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays 3634 * (similarly DTRACE_JSON_VALUE). All following literal value processing 3635 * states return to this state at the end of their value, unless otherwise 3636 * noted. 3637 * 3638 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP: 3639 * Processes a Number literal from the JSON, including any exponent 3640 * component that may be present. Numbers are returned as strings, which 3641 * may be passed to strtoll() if an integer is required. 3642 * 3643 * DTRACE_JSON_IDENTIFIER: 3644 * Processes a "true", "false" or "null" literal in the JSON. 3645 * 3646 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE, 3647 * DTRACE_JSON_STRING_ESCAPE_UNICODE: 3648 * Processes a String literal from the JSON, whether the String denotes 3649 * a key, a value or part of a larger Object. Handles all escape sequences 3650 * present in the specification, including four-digit unicode characters, 3651 * but merely includes the escape sequence without converting it to the 3652 * actual escaped character. If the String is flagged as a key, we 3653 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA. 3654 * 3655 * DTRACE_JSON_COLLECT_OBJECT: 3656 * This state collects an entire Object (or Array), correctly handling 3657 * embedded strings. If the full element selector list matches this nested 3658 * object, we return the Object in full as a string. If not, we use this 3659 * state to skip to the next value at this level and continue processing. 3660 * 3661 * NOTE: This function uses various macros from strtolctype.h to manipulate 3662 * digit values, etc -- these have all been checked to ensure they make 3663 * no additional function calls. 3664 */ 3665static char * 3666dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems, 3667 char *dest) 3668{ 3669 dtrace_json_state_t state = DTRACE_JSON_REST; 3670 int64_t array_elem = INT64_MIN; 3671 int64_t array_pos = 0; 3672 uint8_t escape_unicount = 0; 3673 boolean_t string_is_key = B_FALSE; 3674 boolean_t collect_object = B_FALSE; 3675 boolean_t found_key = B_FALSE; 3676 boolean_t in_array = B_FALSE; 3677 uint32_t braces = 0, brackets = 0; 3678 char *elem = elemlist; 3679 char *dd = dest; 3680 uintptr_t cur; 3681 3682 for (cur = json; cur < json + size; cur++) { 3683 char cc = dtrace_load8(cur); 3684 if (cc == '\0') 3685 return (NULL); 3686 3687 switch (state) { 3688 case DTRACE_JSON_REST: 3689 if (isspace(cc)) 3690 break; 3691 3692 if (cc == '{') { 3693 state = DTRACE_JSON_OBJECT; 3694 break; 3695 } 3696 3697 if (cc == '[') { 3698 in_array = B_TRUE; 3699 array_pos = 0; 3700 array_elem = dtrace_strtoll(elem, 10, size); 3701 found_key = array_elem == 0 ? B_TRUE : B_FALSE; 3702 state = DTRACE_JSON_VALUE; 3703 break; 3704 } 3705 3706 /* 3707 * ERROR: expected to find a top-level object or array. 3708 */ 3709 return (NULL); 3710 case DTRACE_JSON_OBJECT: 3711 if (isspace(cc)) 3712 break; 3713 3714 if (cc == '"') { 3715 state = DTRACE_JSON_STRING; 3716 string_is_key = B_TRUE; 3717 break; 3718 } 3719 3720 /* 3721 * ERROR: either the object did not start with a key 3722 * string, or we've run off the end of the object 3723 * without finding the requested key. 3724 */ 3725 return (NULL); 3726 case DTRACE_JSON_STRING: 3727 if (cc == '\\') { 3728 *dd++ = '\\'; 3729 state = DTRACE_JSON_STRING_ESCAPE; 3730 break; 3731 } 3732 3733 if (cc == '"') { 3734 if (collect_object) { 3735 /* 3736 * We don't reset the dest here, as 3737 * the string is part of a larger 3738 * object being collected. 3739 */ 3740 *dd++ = cc; 3741 collect_object = B_FALSE; 3742 state = DTRACE_JSON_COLLECT_OBJECT; 3743 break; 3744 } 3745 *dd = '\0'; 3746 dd = dest; /* reset string buffer */ 3747 if (string_is_key) { 3748 if (dtrace_strncmp(dest, elem, 3749 size) == 0) 3750 found_key = B_TRUE; 3751 } else if (found_key) { 3752 if (nelems > 1) { 3753 /* 3754 * We expected an object, not 3755 * this string. 3756 */ 3757 return (NULL); 3758 } 3759 return (dest); 3760 } 3761 state = string_is_key ? DTRACE_JSON_COLON : 3762 DTRACE_JSON_COMMA; 3763 string_is_key = B_FALSE; 3764 break; 3765 } 3766 3767 *dd++ = cc; 3768 break; 3769 case DTRACE_JSON_STRING_ESCAPE: 3770 *dd++ = cc; 3771 if (cc == 'u') { 3772 escape_unicount = 0; 3773 state = DTRACE_JSON_STRING_ESCAPE_UNICODE; 3774 } else { 3775 state = DTRACE_JSON_STRING; 3776 } 3777 break; 3778 case DTRACE_JSON_STRING_ESCAPE_UNICODE: 3779 if (!isxdigit(cc)) { 3780 /* 3781 * ERROR: invalid unicode escape, expected 3782 * four valid hexidecimal digits. 3783 */ 3784 return (NULL); 3785 } 3786 3787 *dd++ = cc; 3788 if (++escape_unicount == 4) 3789 state = DTRACE_JSON_STRING; 3790 break; 3791 case DTRACE_JSON_COLON: 3792 if (isspace(cc)) 3793 break; 3794 3795 if (cc == ':') { 3796 state = DTRACE_JSON_VALUE; 3797 break; 3798 } 3799 3800 /* 3801 * ERROR: expected a colon. 3802 */ 3803 return (NULL); 3804 case DTRACE_JSON_COMMA: 3805 if (isspace(cc)) 3806 break; 3807 3808 if (cc == ',') { 3809 if (in_array) { 3810 state = DTRACE_JSON_VALUE; 3811 if (++array_pos == array_elem) 3812 found_key = B_TRUE; 3813 } else { 3814 state = DTRACE_JSON_OBJECT; 3815 } 3816 break; 3817 } 3818 3819 /* 3820 * ERROR: either we hit an unexpected character, or 3821 * we reached the end of the object or array without 3822 * finding the requested key. 3823 */ 3824 return (NULL); 3825 case DTRACE_JSON_IDENTIFIER: 3826 if (islower(cc)) { 3827 *dd++ = cc; 3828 break; 3829 } 3830 3831 *dd = '\0'; 3832 dd = dest; /* reset string buffer */ 3833 3834 if (dtrace_strncmp(dest, "true", 5) == 0 || 3835 dtrace_strncmp(dest, "false", 6) == 0 || 3836 dtrace_strncmp(dest, "null", 5) == 0) { 3837 if (found_key) { 3838 if (nelems > 1) { 3839 /* 3840 * ERROR: We expected an object, 3841 * not this identifier. 3842 */ 3843 return (NULL); 3844 } 3845 return (dest); 3846 } else { 3847 cur--; 3848 state = DTRACE_JSON_COMMA; 3849 break; 3850 } 3851 } 3852 3853 /* 3854 * ERROR: we did not recognise the identifier as one 3855 * of those in the JSON specification. 3856 */ 3857 return (NULL); 3858 case DTRACE_JSON_NUMBER: 3859 if (cc == '.') { 3860 *dd++ = cc; 3861 state = DTRACE_JSON_NUMBER_FRAC; 3862 break; 3863 } 3864 3865 if (cc == 'x' || cc == 'X') { 3866 /* 3867 * ERROR: specification explicitly excludes 3868 * hexidecimal or octal numbers. 3869 */ 3870 return (NULL); 3871 } 3872 3873 /* FALLTHRU */ 3874 case DTRACE_JSON_NUMBER_FRAC: 3875 if (cc == 'e' || cc == 'E') { 3876 *dd++ = cc; 3877 state = DTRACE_JSON_NUMBER_EXP; 3878 break; 3879 } 3880 3881 if (cc == '+' || cc == '-') { 3882 /* 3883 * ERROR: expect sign as part of exponent only. 3884 */ 3885 return (NULL); 3886 } 3887 /* FALLTHRU */ 3888 case DTRACE_JSON_NUMBER_EXP: 3889 if (isdigit(cc) || cc == '+' || cc == '-') { 3890 *dd++ = cc; 3891 break; 3892 } 3893 3894 *dd = '\0'; 3895 dd = dest; /* reset string buffer */ 3896 if (found_key) { 3897 if (nelems > 1) { 3898 /* 3899 * ERROR: We expected an object, not 3900 * this number. 3901 */ 3902 return (NULL); 3903 } 3904 return (dest); 3905 } 3906 3907 cur--; 3908 state = DTRACE_JSON_COMMA; 3909 break; 3910 case DTRACE_JSON_VALUE: 3911 if (isspace(cc)) 3912 break; 3913 3914 if (cc == '{' || cc == '[') { 3915 if (nelems > 1 && found_key) { 3916 in_array = cc == '[' ? B_TRUE : B_FALSE; 3917 /* 3918 * If our element selector directs us 3919 * to descend into this nested object, 3920 * then move to the next selector 3921 * element in the list and restart the 3922 * state machine. 3923 */ 3924 while (*elem != '\0') 3925 elem++; 3926 elem++; /* skip the inter-element NUL */ 3927 nelems--; 3928 dd = dest; 3929 if (in_array) { 3930 state = DTRACE_JSON_VALUE; 3931 array_pos = 0; 3932 array_elem = dtrace_strtoll( 3933 elem, 10, size); 3934 found_key = array_elem == 0 ? 3935 B_TRUE : B_FALSE; 3936 } else { 3937 found_key = B_FALSE; 3938 state = DTRACE_JSON_OBJECT; 3939 } 3940 break; 3941 } 3942 3943 /* 3944 * Otherwise, we wish to either skip this 3945 * nested object or return it in full. 3946 */ 3947 if (cc == '[') 3948 brackets = 1; 3949 else 3950 braces = 1; 3951 *dd++ = cc; 3952 state = DTRACE_JSON_COLLECT_OBJECT; 3953 break; 3954 } 3955 3956 if (cc == '"') { 3957 state = DTRACE_JSON_STRING; 3958 break; 3959 } 3960 3961 if (islower(cc)) { 3962 /* 3963 * Here we deal with true, false and null. 3964 */ 3965 *dd++ = cc; 3966 state = DTRACE_JSON_IDENTIFIER; 3967 break; 3968 } 3969 3970 if (cc == '-' || isdigit(cc)) { 3971 *dd++ = cc; 3972 state = DTRACE_JSON_NUMBER; 3973 break; 3974 } 3975 3976 /* 3977 * ERROR: unexpected character at start of value. 3978 */ 3979 return (NULL); 3980 case DTRACE_JSON_COLLECT_OBJECT: 3981 if (cc == '\0') 3982 /* 3983 * ERROR: unexpected end of input. 3984 */ 3985 return (NULL); 3986 3987 *dd++ = cc; 3988 if (cc == '"') { 3989 collect_object = B_TRUE; 3990 state = DTRACE_JSON_STRING; 3991 break; 3992 } 3993 3994 if (cc == ']') { 3995 if (brackets-- == 0) { 3996 /* 3997 * ERROR: unbalanced brackets. 3998 */ 3999 return (NULL); 4000 } 4001 } else if (cc == '}') { 4002 if (braces-- == 0) { 4003 /* 4004 * ERROR: unbalanced braces. 4005 */ 4006 return (NULL); 4007 } 4008 } else if (cc == '{') { 4009 braces++; 4010 } else if (cc == '[') { 4011 brackets++; 4012 } 4013 4014 if (brackets == 0 && braces == 0) { 4015 if (found_key) { 4016 *dd = '\0'; 4017 return (dest); 4018 } 4019 dd = dest; /* reset string buffer */ 4020 state = DTRACE_JSON_COMMA; 4021 } 4022 break; 4023 } 4024 } 4025 return (NULL); 4026} 4027 4028/* 4029 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 4030 * Notice that we don't bother validating the proper number of arguments or 4031 * their types in the tuple stack. This isn't needed because all argument 4032 * interpretation is safe because of our load safety -- the worst that can 4033 * happen is that a bogus program can obtain bogus results. 4034 */ 4035static void 4036dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 4037 dtrace_key_t *tupregs, int nargs, 4038 dtrace_mstate_t *mstate, dtrace_state_t *state) 4039{ 4040 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4041 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4042 dtrace_vstate_t *vstate = &state->dts_vstate; 4043 4044#if defined(sun) 4045 union { 4046 mutex_impl_t mi; 4047 uint64_t mx; 4048 } m; 4049 4050 union { 4051 krwlock_t ri; 4052 uintptr_t rw; 4053 } r; 4054#else 4055 struct thread *lowner; 4056 union { 4057 struct lock_object *li; 4058 uintptr_t lx; 4059 } l; 4060#endif 4061 4062 switch (subr) { 4063 case DIF_SUBR_RAND: 4064 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 4065 break; 4066 4067#if defined(sun) 4068 case DIF_SUBR_MUTEX_OWNED: 4069 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4070 mstate, vstate)) { 4071 regs[rd] = 0; 4072 break; 4073 } 4074 4075 m.mx = dtrace_load64(tupregs[0].dttk_value); 4076 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 4077 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 4078 else 4079 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 4080 break; 4081 4082 case DIF_SUBR_MUTEX_OWNER: 4083 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4084 mstate, vstate)) { 4085 regs[rd] = 0; 4086 break; 4087 } 4088 4089 m.mx = dtrace_load64(tupregs[0].dttk_value); 4090 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 4091 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 4092 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 4093 else 4094 regs[rd] = 0; 4095 break; 4096 4097 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4098 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4099 mstate, vstate)) { 4100 regs[rd] = 0; 4101 break; 4102 } 4103 4104 m.mx = dtrace_load64(tupregs[0].dttk_value); 4105 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 4106 break; 4107 4108 case DIF_SUBR_MUTEX_TYPE_SPIN: 4109 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4110 mstate, vstate)) { 4111 regs[rd] = 0; 4112 break; 4113 } 4114 4115 m.mx = dtrace_load64(tupregs[0].dttk_value); 4116 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 4117 break; 4118 4119 case DIF_SUBR_RW_READ_HELD: { 4120 uintptr_t tmp; 4121 4122 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4123 mstate, vstate)) { 4124 regs[rd] = 0; 4125 break; 4126 } 4127 4128 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4129 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 4130 break; 4131 } 4132 4133 case DIF_SUBR_RW_WRITE_HELD: 4134 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4135 mstate, vstate)) { 4136 regs[rd] = 0; 4137 break; 4138 } 4139 4140 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4141 regs[rd] = _RW_WRITE_HELD(&r.ri); 4142 break; 4143 4144 case DIF_SUBR_RW_ISWRITER: 4145 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4146 mstate, vstate)) { 4147 regs[rd] = 0; 4148 break; 4149 } 4150 4151 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4152 regs[rd] = _RW_ISWRITER(&r.ri); 4153 break; 4154 4155#else 4156 case DIF_SUBR_MUTEX_OWNED: 4157 if (!dtrace_canload(tupregs[0].dttk_value, 4158 sizeof (struct lock_object), mstate, vstate)) { 4159 regs[rd] = 0; 4160 break; 4161 } 4162 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4163 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4164 break; 4165 4166 case DIF_SUBR_MUTEX_OWNER: 4167 if (!dtrace_canload(tupregs[0].dttk_value, 4168 sizeof (struct lock_object), mstate, vstate)) { 4169 regs[rd] = 0; 4170 break; 4171 } 4172 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4173 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4174 regs[rd] = (uintptr_t)lowner; 4175 break; 4176 4177 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4178 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4179 mstate, vstate)) { 4180 regs[rd] = 0; 4181 break; 4182 } 4183 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4184 /* XXX - should be only LC_SLEEPABLE? */ 4185 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & 4186 (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0; 4187 break; 4188 4189 case DIF_SUBR_MUTEX_TYPE_SPIN: 4190 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4191 mstate, vstate)) { 4192 regs[rd] = 0; 4193 break; 4194 } 4195 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4196 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 4197 break; 4198 4199 case DIF_SUBR_RW_READ_HELD: 4200 case DIF_SUBR_SX_SHARED_HELD: 4201 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4202 mstate, vstate)) { 4203 regs[rd] = 0; 4204 break; 4205 } 4206 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4207 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4208 lowner == NULL; 4209 break; 4210 4211 case DIF_SUBR_RW_WRITE_HELD: 4212 case DIF_SUBR_SX_EXCLUSIVE_HELD: 4213 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4214 mstate, vstate)) { 4215 regs[rd] = 0; 4216 break; 4217 } 4218 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4219 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4220 regs[rd] = (lowner == curthread); 4221 break; 4222 4223 case DIF_SUBR_RW_ISWRITER: 4224 case DIF_SUBR_SX_ISEXCLUSIVE: 4225 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4226 mstate, vstate)) { 4227 regs[rd] = 0; 4228 break; 4229 } 4230 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4231 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4232 lowner != NULL; 4233 break; 4234#endif /* ! defined(sun) */ 4235 4236 case DIF_SUBR_BCOPY: { 4237 /* 4238 * We need to be sure that the destination is in the scratch 4239 * region -- no other region is allowed. 4240 */ 4241 uintptr_t src = tupregs[0].dttk_value; 4242 uintptr_t dest = tupregs[1].dttk_value; 4243 size_t size = tupregs[2].dttk_value; 4244 4245 if (!dtrace_inscratch(dest, size, mstate)) { 4246 *flags |= CPU_DTRACE_BADADDR; 4247 *illval = regs[rd]; 4248 break; 4249 } 4250 4251 if (!dtrace_canload(src, size, mstate, vstate)) { 4252 regs[rd] = 0; 4253 break; 4254 } 4255 4256 dtrace_bcopy((void *)src, (void *)dest, size); 4257 break; 4258 } 4259 4260 case DIF_SUBR_ALLOCA: 4261 case DIF_SUBR_COPYIN: { 4262 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4263 uint64_t size = 4264 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 4265 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 4266 4267 /* 4268 * This action doesn't require any credential checks since 4269 * probes will not activate in user contexts to which the 4270 * enabling user does not have permissions. 4271 */ 4272 4273 /* 4274 * Rounding up the user allocation size could have overflowed 4275 * a large, bogus allocation (like -1ULL) to 0. 4276 */ 4277 if (scratch_size < size || 4278 !DTRACE_INSCRATCH(mstate, scratch_size)) { 4279 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4280 regs[rd] = 0; 4281 break; 4282 } 4283 4284 if (subr == DIF_SUBR_COPYIN) { 4285 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4286 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4287 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4288 } 4289 4290 mstate->dtms_scratch_ptr += scratch_size; 4291 regs[rd] = dest; 4292 break; 4293 } 4294 4295 case DIF_SUBR_COPYINTO: { 4296 uint64_t size = tupregs[1].dttk_value; 4297 uintptr_t dest = tupregs[2].dttk_value; 4298 4299 /* 4300 * This action doesn't require any credential checks since 4301 * probes will not activate in user contexts to which the 4302 * enabling user does not have permissions. 4303 */ 4304 if (!dtrace_inscratch(dest, size, mstate)) { 4305 *flags |= CPU_DTRACE_BADADDR; 4306 *illval = regs[rd]; 4307 break; 4308 } 4309 4310 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4311 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4312 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4313 break; 4314 } 4315 4316 case DIF_SUBR_COPYINSTR: { 4317 uintptr_t dest = mstate->dtms_scratch_ptr; 4318 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4319 4320 if (nargs > 1 && tupregs[1].dttk_value < size) 4321 size = tupregs[1].dttk_value + 1; 4322 4323 /* 4324 * This action doesn't require any credential checks since 4325 * probes will not activate in user contexts to which the 4326 * enabling user does not have permissions. 4327 */ 4328 if (!DTRACE_INSCRATCH(mstate, size)) { 4329 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4330 regs[rd] = 0; 4331 break; 4332 } 4333 4334 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4335 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 4336 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4337 4338 ((char *)dest)[size - 1] = '\0'; 4339 mstate->dtms_scratch_ptr += size; 4340 regs[rd] = dest; 4341 break; 4342 } 4343 4344#if defined(sun) 4345 case DIF_SUBR_MSGSIZE: 4346 case DIF_SUBR_MSGDSIZE: { 4347 uintptr_t baddr = tupregs[0].dttk_value, daddr; 4348 uintptr_t wptr, rptr; 4349 size_t count = 0; 4350 int cont = 0; 4351 4352 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 4353 4354 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 4355 vstate)) { 4356 regs[rd] = 0; 4357 break; 4358 } 4359 4360 wptr = dtrace_loadptr(baddr + 4361 offsetof(mblk_t, b_wptr)); 4362 4363 rptr = dtrace_loadptr(baddr + 4364 offsetof(mblk_t, b_rptr)); 4365 4366 if (wptr < rptr) { 4367 *flags |= CPU_DTRACE_BADADDR; 4368 *illval = tupregs[0].dttk_value; 4369 break; 4370 } 4371 4372 daddr = dtrace_loadptr(baddr + 4373 offsetof(mblk_t, b_datap)); 4374 4375 baddr = dtrace_loadptr(baddr + 4376 offsetof(mblk_t, b_cont)); 4377 4378 /* 4379 * We want to prevent against denial-of-service here, 4380 * so we're only going to search the list for 4381 * dtrace_msgdsize_max mblks. 4382 */ 4383 if (cont++ > dtrace_msgdsize_max) { 4384 *flags |= CPU_DTRACE_ILLOP; 4385 break; 4386 } 4387 4388 if (subr == DIF_SUBR_MSGDSIZE) { 4389 if (dtrace_load8(daddr + 4390 offsetof(dblk_t, db_type)) != M_DATA) 4391 continue; 4392 } 4393 4394 count += wptr - rptr; 4395 } 4396 4397 if (!(*flags & CPU_DTRACE_FAULT)) 4398 regs[rd] = count; 4399 4400 break; 4401 } 4402#endif 4403 4404 case DIF_SUBR_PROGENYOF: { 4405 pid_t pid = tupregs[0].dttk_value; 4406 proc_t *p; 4407 int rval = 0; 4408 4409 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4410 4411 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 4412#if defined(sun) 4413 if (p->p_pidp->pid_id == pid) { 4414#else 4415 if (p->p_pid == pid) { 4416#endif 4417 rval = 1; 4418 break; 4419 } 4420 } 4421 4422 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4423 4424 regs[rd] = rval; 4425 break; 4426 } 4427 4428 case DIF_SUBR_SPECULATION: 4429 regs[rd] = dtrace_speculation(state); 4430 break; 4431 4432 case DIF_SUBR_COPYOUT: { 4433 uintptr_t kaddr = tupregs[0].dttk_value; 4434 uintptr_t uaddr = tupregs[1].dttk_value; 4435 uint64_t size = tupregs[2].dttk_value; 4436 4437 if (!dtrace_destructive_disallow && 4438 dtrace_priv_proc_control(state) && 4439 !dtrace_istoxic(kaddr, size)) { 4440 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4441 dtrace_copyout(kaddr, uaddr, size, flags); 4442 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4443 } 4444 break; 4445 } 4446 4447 case DIF_SUBR_COPYOUTSTR: { 4448 uintptr_t kaddr = tupregs[0].dttk_value; 4449 uintptr_t uaddr = tupregs[1].dttk_value; 4450 uint64_t size = tupregs[2].dttk_value; 4451 4452 if (!dtrace_destructive_disallow && 4453 dtrace_priv_proc_control(state) && 4454 !dtrace_istoxic(kaddr, size)) { 4455 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4456 dtrace_copyoutstr(kaddr, uaddr, size, flags); 4457 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4458 } 4459 break; 4460 } 4461 4462 case DIF_SUBR_STRLEN: { 4463 size_t sz; 4464 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 4465 sz = dtrace_strlen((char *)addr, 4466 state->dts_options[DTRACEOPT_STRSIZE]); 4467 4468 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 4469 regs[rd] = 0; 4470 break; 4471 } 4472 4473 regs[rd] = sz; 4474 4475 break; 4476 } 4477 4478 case DIF_SUBR_STRCHR: 4479 case DIF_SUBR_STRRCHR: { 4480 /* 4481 * We're going to iterate over the string looking for the 4482 * specified character. We will iterate until we have reached 4483 * the string length or we have found the character. If this 4484 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 4485 * of the specified character instead of the first. 4486 */ 4487 uintptr_t saddr = tupregs[0].dttk_value; 4488 uintptr_t addr = tupregs[0].dttk_value; 4489 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 4490 char c, target = (char)tupregs[1].dttk_value; 4491 4492 for (regs[rd] = 0; addr < limit; addr++) { 4493 if ((c = dtrace_load8(addr)) == target) { 4494 regs[rd] = addr; 4495 4496 if (subr == DIF_SUBR_STRCHR) 4497 break; 4498 } 4499 4500 if (c == '\0') 4501 break; 4502 } 4503 4504 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 4505 regs[rd] = 0; 4506 break; 4507 } 4508 4509 break; 4510 } 4511 4512 case DIF_SUBR_STRSTR: 4513 case DIF_SUBR_INDEX: 4514 case DIF_SUBR_RINDEX: { 4515 /* 4516 * We're going to iterate over the string looking for the 4517 * specified string. We will iterate until we have reached 4518 * the string length or we have found the string. (Yes, this 4519 * is done in the most naive way possible -- but considering 4520 * that the string we're searching for is likely to be 4521 * relatively short, the complexity of Rabin-Karp or similar 4522 * hardly seems merited.) 4523 */ 4524 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 4525 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 4526 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4527 size_t len = dtrace_strlen(addr, size); 4528 size_t sublen = dtrace_strlen(substr, size); 4529 char *limit = addr + len, *orig = addr; 4530 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 4531 int inc = 1; 4532 4533 regs[rd] = notfound; 4534 4535 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 4536 regs[rd] = 0; 4537 break; 4538 } 4539 4540 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 4541 vstate)) { 4542 regs[rd] = 0; 4543 break; 4544 } 4545 4546 /* 4547 * strstr() and index()/rindex() have similar semantics if 4548 * both strings are the empty string: strstr() returns a 4549 * pointer to the (empty) string, and index() and rindex() 4550 * both return index 0 (regardless of any position argument). 4551 */ 4552 if (sublen == 0 && len == 0) { 4553 if (subr == DIF_SUBR_STRSTR) 4554 regs[rd] = (uintptr_t)addr; 4555 else 4556 regs[rd] = 0; 4557 break; 4558 } 4559 4560 if (subr != DIF_SUBR_STRSTR) { 4561 if (subr == DIF_SUBR_RINDEX) { 4562 limit = orig - 1; 4563 addr += len; 4564 inc = -1; 4565 } 4566 4567 /* 4568 * Both index() and rindex() take an optional position 4569 * argument that denotes the starting position. 4570 */ 4571 if (nargs == 3) { 4572 int64_t pos = (int64_t)tupregs[2].dttk_value; 4573 4574 /* 4575 * If the position argument to index() is 4576 * negative, Perl implicitly clamps it at 4577 * zero. This semantic is a little surprising 4578 * given the special meaning of negative 4579 * positions to similar Perl functions like 4580 * substr(), but it appears to reflect a 4581 * notion that index() can start from a 4582 * negative index and increment its way up to 4583 * the string. Given this notion, Perl's 4584 * rindex() is at least self-consistent in 4585 * that it implicitly clamps positions greater 4586 * than the string length to be the string 4587 * length. Where Perl completely loses 4588 * coherence, however, is when the specified 4589 * substring is the empty string (""). In 4590 * this case, even if the position is 4591 * negative, rindex() returns 0 -- and even if 4592 * the position is greater than the length, 4593 * index() returns the string length. These 4594 * semantics violate the notion that index() 4595 * should never return a value less than the 4596 * specified position and that rindex() should 4597 * never return a value greater than the 4598 * specified position. (One assumes that 4599 * these semantics are artifacts of Perl's 4600 * implementation and not the results of 4601 * deliberate design -- it beggars belief that 4602 * even Larry Wall could desire such oddness.) 4603 * While in the abstract one would wish for 4604 * consistent position semantics across 4605 * substr(), index() and rindex() -- or at the 4606 * very least self-consistent position 4607 * semantics for index() and rindex() -- we 4608 * instead opt to keep with the extant Perl 4609 * semantics, in all their broken glory. (Do 4610 * we have more desire to maintain Perl's 4611 * semantics than Perl does? Probably.) 4612 */ 4613 if (subr == DIF_SUBR_RINDEX) { 4614 if (pos < 0) { 4615 if (sublen == 0) 4616 regs[rd] = 0; 4617 break; 4618 } 4619 4620 if (pos > len) 4621 pos = len; 4622 } else { 4623 if (pos < 0) 4624 pos = 0; 4625 4626 if (pos >= len) { 4627 if (sublen == 0) 4628 regs[rd] = len; 4629 break; 4630 } 4631 } 4632 4633 addr = orig + pos; 4634 } 4635 } 4636 4637 for (regs[rd] = notfound; addr != limit; addr += inc) { 4638 if (dtrace_strncmp(addr, substr, sublen) == 0) { 4639 if (subr != DIF_SUBR_STRSTR) { 4640 /* 4641 * As D index() and rindex() are 4642 * modeled on Perl (and not on awk), 4643 * we return a zero-based (and not a 4644 * one-based) index. (For you Perl 4645 * weenies: no, we're not going to add 4646 * $[ -- and shouldn't you be at a con 4647 * or something?) 4648 */ 4649 regs[rd] = (uintptr_t)(addr - orig); 4650 break; 4651 } 4652 4653 ASSERT(subr == DIF_SUBR_STRSTR); 4654 regs[rd] = (uintptr_t)addr; 4655 break; 4656 } 4657 } 4658 4659 break; 4660 } 4661 4662 case DIF_SUBR_STRTOK: { 4663 uintptr_t addr = tupregs[0].dttk_value; 4664 uintptr_t tokaddr = tupregs[1].dttk_value; 4665 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4666 uintptr_t limit, toklimit = tokaddr + size; 4667 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 4668 char *dest = (char *)mstate->dtms_scratch_ptr; 4669 int i; 4670 4671 /* 4672 * Check both the token buffer and (later) the input buffer, 4673 * since both could be non-scratch addresses. 4674 */ 4675 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 4676 regs[rd] = 0; 4677 break; 4678 } 4679 4680 if (!DTRACE_INSCRATCH(mstate, size)) { 4681 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4682 regs[rd] = 0; 4683 break; 4684 } 4685 4686 if (addr == 0) { 4687 /* 4688 * If the address specified is NULL, we use our saved 4689 * strtok pointer from the mstate. Note that this 4690 * means that the saved strtok pointer is _only_ 4691 * valid within multiple enablings of the same probe -- 4692 * it behaves like an implicit clause-local variable. 4693 */ 4694 addr = mstate->dtms_strtok; 4695 } else { 4696 /* 4697 * If the user-specified address is non-NULL we must 4698 * access check it. This is the only time we have 4699 * a chance to do so, since this address may reside 4700 * in the string table of this clause-- future calls 4701 * (when we fetch addr from mstate->dtms_strtok) 4702 * would fail this access check. 4703 */ 4704 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 4705 regs[rd] = 0; 4706 break; 4707 } 4708 } 4709 4710 /* 4711 * First, zero the token map, and then process the token 4712 * string -- setting a bit in the map for every character 4713 * found in the token string. 4714 */ 4715 for (i = 0; i < sizeof (tokmap); i++) 4716 tokmap[i] = 0; 4717 4718 for (; tokaddr < toklimit; tokaddr++) { 4719 if ((c = dtrace_load8(tokaddr)) == '\0') 4720 break; 4721 4722 ASSERT((c >> 3) < sizeof (tokmap)); 4723 tokmap[c >> 3] |= (1 << (c & 0x7)); 4724 } 4725 4726 for (limit = addr + size; addr < limit; addr++) { 4727 /* 4728 * We're looking for a character that is _not_ contained 4729 * in the token string. 4730 */ 4731 if ((c = dtrace_load8(addr)) == '\0') 4732 break; 4733 4734 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 4735 break; 4736 } 4737 4738 if (c == '\0') { 4739 /* 4740 * We reached the end of the string without finding 4741 * any character that was not in the token string. 4742 * We return NULL in this case, and we set the saved 4743 * address to NULL as well. 4744 */ 4745 regs[rd] = 0; 4746 mstate->dtms_strtok = 0; 4747 break; 4748 } 4749 4750 /* 4751 * From here on, we're copying into the destination string. 4752 */ 4753 for (i = 0; addr < limit && i < size - 1; addr++) { 4754 if ((c = dtrace_load8(addr)) == '\0') 4755 break; 4756 4757 if (tokmap[c >> 3] & (1 << (c & 0x7))) 4758 break; 4759 4760 ASSERT(i < size); 4761 dest[i++] = c; 4762 } 4763 4764 ASSERT(i < size); 4765 dest[i] = '\0'; 4766 regs[rd] = (uintptr_t)dest; 4767 mstate->dtms_scratch_ptr += size; 4768 mstate->dtms_strtok = addr; 4769 break; 4770 } 4771 4772 case DIF_SUBR_SUBSTR: { 4773 uintptr_t s = tupregs[0].dttk_value; 4774 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4775 char *d = (char *)mstate->dtms_scratch_ptr; 4776 int64_t index = (int64_t)tupregs[1].dttk_value; 4777 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4778 size_t len = dtrace_strlen((char *)s, size); 4779 int64_t i; 4780 4781 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4782 regs[rd] = 0; 4783 break; 4784 } 4785 4786 if (!DTRACE_INSCRATCH(mstate, size)) { 4787 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4788 regs[rd] = 0; 4789 break; 4790 } 4791 4792 if (nargs <= 2) 4793 remaining = (int64_t)size; 4794 4795 if (index < 0) { 4796 index += len; 4797 4798 if (index < 0 && index + remaining > 0) { 4799 remaining += index; 4800 index = 0; 4801 } 4802 } 4803 4804 if (index >= len || index < 0) { 4805 remaining = 0; 4806 } else if (remaining < 0) { 4807 remaining += len - index; 4808 } else if (index + remaining > size) { 4809 remaining = size - index; 4810 } 4811 4812 for (i = 0; i < remaining; i++) { 4813 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4814 break; 4815 } 4816 4817 d[i] = '\0'; 4818 4819 mstate->dtms_scratch_ptr += size; 4820 regs[rd] = (uintptr_t)d; 4821 break; 4822 } 4823 4824 case DIF_SUBR_JSON: { 4825 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4826 uintptr_t json = tupregs[0].dttk_value; 4827 size_t jsonlen = dtrace_strlen((char *)json, size); 4828 uintptr_t elem = tupregs[1].dttk_value; 4829 size_t elemlen = dtrace_strlen((char *)elem, size); 4830 4831 char *dest = (char *)mstate->dtms_scratch_ptr; 4832 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1; 4833 char *ee = elemlist; 4834 int nelems = 1; 4835 uintptr_t cur; 4836 4837 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) || 4838 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) { 4839 regs[rd] = 0; 4840 break; 4841 } 4842 4843 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) { 4844 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4845 regs[rd] = 0; 4846 break; 4847 } 4848 4849 /* 4850 * Read the element selector and split it up into a packed list 4851 * of strings. 4852 */ 4853 for (cur = elem; cur < elem + elemlen; cur++) { 4854 char cc = dtrace_load8(cur); 4855 4856 if (cur == elem && cc == '[') { 4857 /* 4858 * If the first element selector key is 4859 * actually an array index then ignore the 4860 * bracket. 4861 */ 4862 continue; 4863 } 4864 4865 if (cc == ']') 4866 continue; 4867 4868 if (cc == '.' || cc == '[') { 4869 nelems++; 4870 cc = '\0'; 4871 } 4872 4873 *ee++ = cc; 4874 } 4875 *ee++ = '\0'; 4876 4877 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist, 4878 nelems, dest)) != 0) 4879 mstate->dtms_scratch_ptr += jsonlen + 1; 4880 break; 4881 } 4882 4883 case DIF_SUBR_TOUPPER: 4884 case DIF_SUBR_TOLOWER: { 4885 uintptr_t s = tupregs[0].dttk_value; 4886 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4887 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4888 size_t len = dtrace_strlen((char *)s, size); 4889 char lower, upper, convert; 4890 int64_t i; 4891 4892 if (subr == DIF_SUBR_TOUPPER) { 4893 lower = 'a'; 4894 upper = 'z'; 4895 convert = 'A'; 4896 } else { 4897 lower = 'A'; 4898 upper = 'Z'; 4899 convert = 'a'; 4900 } 4901 4902 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4903 regs[rd] = 0; 4904 break; 4905 } 4906 4907 if (!DTRACE_INSCRATCH(mstate, size)) { 4908 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4909 regs[rd] = 0; 4910 break; 4911 } 4912 4913 for (i = 0; i < size - 1; i++) { 4914 if ((c = dtrace_load8(s + i)) == '\0') 4915 break; 4916 4917 if (c >= lower && c <= upper) 4918 c = convert + (c - lower); 4919 4920 dest[i] = c; 4921 } 4922 4923 ASSERT(i < size); 4924 dest[i] = '\0'; 4925 regs[rd] = (uintptr_t)dest; 4926 mstate->dtms_scratch_ptr += size; 4927 break; 4928 } 4929 4930#if defined(sun) 4931 case DIF_SUBR_GETMAJOR: 4932#ifdef _LP64 4933 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 4934#else 4935 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 4936#endif 4937 break; 4938 4939 case DIF_SUBR_GETMINOR: 4940#ifdef _LP64 4941 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 4942#else 4943 regs[rd] = tupregs[0].dttk_value & MAXMIN; 4944#endif 4945 break; 4946 4947 case DIF_SUBR_DDI_PATHNAME: { 4948 /* 4949 * This one is a galactic mess. We are going to roughly 4950 * emulate ddi_pathname(), but it's made more complicated 4951 * by the fact that we (a) want to include the minor name and 4952 * (b) must proceed iteratively instead of recursively. 4953 */ 4954 uintptr_t dest = mstate->dtms_scratch_ptr; 4955 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4956 char *start = (char *)dest, *end = start + size - 1; 4957 uintptr_t daddr = tupregs[0].dttk_value; 4958 int64_t minor = (int64_t)tupregs[1].dttk_value; 4959 char *s; 4960 int i, len, depth = 0; 4961 4962 /* 4963 * Due to all the pointer jumping we do and context we must 4964 * rely upon, we just mandate that the user must have kernel 4965 * read privileges to use this routine. 4966 */ 4967 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 4968 *flags |= CPU_DTRACE_KPRIV; 4969 *illval = daddr; 4970 regs[rd] = 0; 4971 } 4972 4973 if (!DTRACE_INSCRATCH(mstate, size)) { 4974 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4975 regs[rd] = 0; 4976 break; 4977 } 4978 4979 *end = '\0'; 4980 4981 /* 4982 * We want to have a name for the minor. In order to do this, 4983 * we need to walk the minor list from the devinfo. We want 4984 * to be sure that we don't infinitely walk a circular list, 4985 * so we check for circularity by sending a scout pointer 4986 * ahead two elements for every element that we iterate over; 4987 * if the list is circular, these will ultimately point to the 4988 * same element. You may recognize this little trick as the 4989 * answer to a stupid interview question -- one that always 4990 * seems to be asked by those who had to have it laboriously 4991 * explained to them, and who can't even concisely describe 4992 * the conditions under which one would be forced to resort to 4993 * this technique. Needless to say, those conditions are 4994 * found here -- and probably only here. Is this the only use 4995 * of this infamous trick in shipping, production code? If it 4996 * isn't, it probably should be... 4997 */ 4998 if (minor != -1) { 4999 uintptr_t maddr = dtrace_loadptr(daddr + 5000 offsetof(struct dev_info, devi_minor)); 5001 5002 uintptr_t next = offsetof(struct ddi_minor_data, next); 5003 uintptr_t name = offsetof(struct ddi_minor_data, 5004 d_minor) + offsetof(struct ddi_minor, name); 5005 uintptr_t dev = offsetof(struct ddi_minor_data, 5006 d_minor) + offsetof(struct ddi_minor, dev); 5007 uintptr_t scout; 5008 5009 if (maddr != NULL) 5010 scout = dtrace_loadptr(maddr + next); 5011 5012 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5013 uint64_t m; 5014#ifdef _LP64 5015 m = dtrace_load64(maddr + dev) & MAXMIN64; 5016#else 5017 m = dtrace_load32(maddr + dev) & MAXMIN; 5018#endif 5019 if (m != minor) { 5020 maddr = dtrace_loadptr(maddr + next); 5021 5022 if (scout == NULL) 5023 continue; 5024 5025 scout = dtrace_loadptr(scout + next); 5026 5027 if (scout == NULL) 5028 continue; 5029 5030 scout = dtrace_loadptr(scout + next); 5031 5032 if (scout == NULL) 5033 continue; 5034 5035 if (scout == maddr) { 5036 *flags |= CPU_DTRACE_ILLOP; 5037 break; 5038 } 5039 5040 continue; 5041 } 5042 5043 /* 5044 * We have the minor data. Now we need to 5045 * copy the minor's name into the end of the 5046 * pathname. 5047 */ 5048 s = (char *)dtrace_loadptr(maddr + name); 5049 len = dtrace_strlen(s, size); 5050 5051 if (*flags & CPU_DTRACE_FAULT) 5052 break; 5053 5054 if (len != 0) { 5055 if ((end -= (len + 1)) < start) 5056 break; 5057 5058 *end = ':'; 5059 } 5060 5061 for (i = 1; i <= len; i++) 5062 end[i] = dtrace_load8((uintptr_t)s++); 5063 break; 5064 } 5065 } 5066 5067 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5068 ddi_node_state_t devi_state; 5069 5070 devi_state = dtrace_load32(daddr + 5071 offsetof(struct dev_info, devi_node_state)); 5072 5073 if (*flags & CPU_DTRACE_FAULT) 5074 break; 5075 5076 if (devi_state >= DS_INITIALIZED) { 5077 s = (char *)dtrace_loadptr(daddr + 5078 offsetof(struct dev_info, devi_addr)); 5079 len = dtrace_strlen(s, size); 5080 5081 if (*flags & CPU_DTRACE_FAULT) 5082 break; 5083 5084 if (len != 0) { 5085 if ((end -= (len + 1)) < start) 5086 break; 5087 5088 *end = '@'; 5089 } 5090 5091 for (i = 1; i <= len; i++) 5092 end[i] = dtrace_load8((uintptr_t)s++); 5093 } 5094 5095 /* 5096 * Now for the node name... 5097 */ 5098 s = (char *)dtrace_loadptr(daddr + 5099 offsetof(struct dev_info, devi_node_name)); 5100 5101 daddr = dtrace_loadptr(daddr + 5102 offsetof(struct dev_info, devi_parent)); 5103 5104 /* 5105 * If our parent is NULL (that is, if we're the root 5106 * node), we're going to use the special path 5107 * "devices". 5108 */ 5109 if (daddr == 0) 5110 s = "devices"; 5111 5112 len = dtrace_strlen(s, size); 5113 if (*flags & CPU_DTRACE_FAULT) 5114 break; 5115 5116 if ((end -= (len + 1)) < start) 5117 break; 5118 5119 for (i = 1; i <= len; i++) 5120 end[i] = dtrace_load8((uintptr_t)s++); 5121 *end = '/'; 5122 5123 if (depth++ > dtrace_devdepth_max) { 5124 *flags |= CPU_DTRACE_ILLOP; 5125 break; 5126 } 5127 } 5128 5129 if (end < start) 5130 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5131 5132 if (daddr == 0) { 5133 regs[rd] = (uintptr_t)end; 5134 mstate->dtms_scratch_ptr += size; 5135 } 5136 5137 break; 5138 } 5139#endif 5140 5141 case DIF_SUBR_STRJOIN: { 5142 char *d = (char *)mstate->dtms_scratch_ptr; 5143 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5144 uintptr_t s1 = tupregs[0].dttk_value; 5145 uintptr_t s2 = tupregs[1].dttk_value; 5146 int i = 0; 5147 5148 if (!dtrace_strcanload(s1, size, mstate, vstate) || 5149 !dtrace_strcanload(s2, size, mstate, vstate)) { 5150 regs[rd] = 0; 5151 break; 5152 } 5153 5154 if (!DTRACE_INSCRATCH(mstate, size)) { 5155 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5156 regs[rd] = 0; 5157 break; 5158 } 5159 5160 for (;;) { 5161 if (i >= size) { 5162 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5163 regs[rd] = 0; 5164 break; 5165 } 5166 5167 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 5168 i--; 5169 break; 5170 } 5171 } 5172 5173 for (;;) { 5174 if (i >= size) { 5175 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5176 regs[rd] = 0; 5177 break; 5178 } 5179 5180 if ((d[i++] = dtrace_load8(s2++)) == '\0') 5181 break; 5182 } 5183 5184 if (i < size) { 5185 mstate->dtms_scratch_ptr += i; 5186 regs[rd] = (uintptr_t)d; 5187 } 5188 5189 break; 5190 } 5191 5192 case DIF_SUBR_STRTOLL: { 5193 uintptr_t s = tupregs[0].dttk_value; 5194 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5195 int base = 10; 5196 5197 if (nargs > 1) { 5198 if ((base = tupregs[1].dttk_value) <= 1 || 5199 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5200 *flags |= CPU_DTRACE_ILLOP; 5201 break; 5202 } 5203 } 5204 5205 if (!dtrace_strcanload(s, size, mstate, vstate)) { 5206 regs[rd] = INT64_MIN; 5207 break; 5208 } 5209 5210 regs[rd] = dtrace_strtoll((char *)s, base, size); 5211 break; 5212 } 5213 5214 case DIF_SUBR_LLTOSTR: { 5215 int64_t i = (int64_t)tupregs[0].dttk_value; 5216 uint64_t val, digit; 5217 uint64_t size = 65; /* enough room for 2^64 in binary */ 5218 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 5219 int base = 10; 5220 5221 if (nargs > 1) { 5222 if ((base = tupregs[1].dttk_value) <= 1 || 5223 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5224 *flags |= CPU_DTRACE_ILLOP; 5225 break; 5226 } 5227 } 5228 5229 val = (base == 10 && i < 0) ? i * -1 : i; 5230 5231 if (!DTRACE_INSCRATCH(mstate, size)) { 5232 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5233 regs[rd] = 0; 5234 break; 5235 } 5236 5237 for (*end-- = '\0'; val; val /= base) { 5238 if ((digit = val % base) <= '9' - '0') { 5239 *end-- = '0' + digit; 5240 } else { 5241 *end-- = 'a' + (digit - ('9' - '0') - 1); 5242 } 5243 } 5244 5245 if (i == 0 && base == 16) 5246 *end-- = '0'; 5247 5248 if (base == 16) 5249 *end-- = 'x'; 5250 5251 if (i == 0 || base == 8 || base == 16) 5252 *end-- = '0'; 5253 5254 if (i < 0 && base == 10) 5255 *end-- = '-'; 5256 5257 regs[rd] = (uintptr_t)end + 1; 5258 mstate->dtms_scratch_ptr += size; 5259 break; 5260 } 5261 5262 case DIF_SUBR_HTONS: 5263 case DIF_SUBR_NTOHS: 5264#if BYTE_ORDER == BIG_ENDIAN 5265 regs[rd] = (uint16_t)tupregs[0].dttk_value; 5266#else 5267 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 5268#endif 5269 break; 5270 5271 5272 case DIF_SUBR_HTONL: 5273 case DIF_SUBR_NTOHL: 5274#if BYTE_ORDER == BIG_ENDIAN 5275 regs[rd] = (uint32_t)tupregs[0].dttk_value; 5276#else 5277 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 5278#endif 5279 break; 5280 5281 5282 case DIF_SUBR_HTONLL: 5283 case DIF_SUBR_NTOHLL: 5284#if BYTE_ORDER == BIG_ENDIAN 5285 regs[rd] = (uint64_t)tupregs[0].dttk_value; 5286#else 5287 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 5288#endif 5289 break; 5290 5291 5292 case DIF_SUBR_DIRNAME: 5293 case DIF_SUBR_BASENAME: { 5294 char *dest = (char *)mstate->dtms_scratch_ptr; 5295 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5296 uintptr_t src = tupregs[0].dttk_value; 5297 int i, j, len = dtrace_strlen((char *)src, size); 5298 int lastbase = -1, firstbase = -1, lastdir = -1; 5299 int start, end; 5300 5301 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 5302 regs[rd] = 0; 5303 break; 5304 } 5305 5306 if (!DTRACE_INSCRATCH(mstate, size)) { 5307 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5308 regs[rd] = 0; 5309 break; 5310 } 5311 5312 /* 5313 * The basename and dirname for a zero-length string is 5314 * defined to be "." 5315 */ 5316 if (len == 0) { 5317 len = 1; 5318 src = (uintptr_t)"."; 5319 } 5320 5321 /* 5322 * Start from the back of the string, moving back toward the 5323 * front until we see a character that isn't a slash. That 5324 * character is the last character in the basename. 5325 */ 5326 for (i = len - 1; i >= 0; i--) { 5327 if (dtrace_load8(src + i) != '/') 5328 break; 5329 } 5330 5331 if (i >= 0) 5332 lastbase = i; 5333 5334 /* 5335 * Starting from the last character in the basename, move 5336 * towards the front until we find a slash. The character 5337 * that we processed immediately before that is the first 5338 * character in the basename. 5339 */ 5340 for (; i >= 0; i--) { 5341 if (dtrace_load8(src + i) == '/') 5342 break; 5343 } 5344 5345 if (i >= 0) 5346 firstbase = i + 1; 5347 5348 /* 5349 * Now keep going until we find a non-slash character. That 5350 * character is the last character in the dirname. 5351 */ 5352 for (; i >= 0; i--) { 5353 if (dtrace_load8(src + i) != '/') 5354 break; 5355 } 5356 5357 if (i >= 0) 5358 lastdir = i; 5359 5360 ASSERT(!(lastbase == -1 && firstbase != -1)); 5361 ASSERT(!(firstbase == -1 && lastdir != -1)); 5362 5363 if (lastbase == -1) { 5364 /* 5365 * We didn't find a non-slash character. We know that 5366 * the length is non-zero, so the whole string must be 5367 * slashes. In either the dirname or the basename 5368 * case, we return '/'. 5369 */ 5370 ASSERT(firstbase == -1); 5371 firstbase = lastbase = lastdir = 0; 5372 } 5373 5374 if (firstbase == -1) { 5375 /* 5376 * The entire string consists only of a basename 5377 * component. If we're looking for dirname, we need 5378 * to change our string to be just "."; if we're 5379 * looking for a basename, we'll just set the first 5380 * character of the basename to be 0. 5381 */ 5382 if (subr == DIF_SUBR_DIRNAME) { 5383 ASSERT(lastdir == -1); 5384 src = (uintptr_t)"."; 5385 lastdir = 0; 5386 } else { 5387 firstbase = 0; 5388 } 5389 } 5390 5391 if (subr == DIF_SUBR_DIRNAME) { 5392 if (lastdir == -1) { 5393 /* 5394 * We know that we have a slash in the name -- 5395 * or lastdir would be set to 0, above. And 5396 * because lastdir is -1, we know that this 5397 * slash must be the first character. (That 5398 * is, the full string must be of the form 5399 * "/basename".) In this case, the last 5400 * character of the directory name is 0. 5401 */ 5402 lastdir = 0; 5403 } 5404 5405 start = 0; 5406 end = lastdir; 5407 } else { 5408 ASSERT(subr == DIF_SUBR_BASENAME); 5409 ASSERT(firstbase != -1 && lastbase != -1); 5410 start = firstbase; 5411 end = lastbase; 5412 } 5413 5414 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 5415 dest[j] = dtrace_load8(src + i); 5416 5417 dest[j] = '\0'; 5418 regs[rd] = (uintptr_t)dest; 5419 mstate->dtms_scratch_ptr += size; 5420 break; 5421 } 5422 5423 case DIF_SUBR_GETF: { 5424 uintptr_t fd = tupregs[0].dttk_value; 5425 struct filedesc *fdp; 5426 file_t *fp; 5427 5428 if (!dtrace_priv_proc(state)) { 5429 regs[rd] = 0; 5430 break; 5431 } 5432 fdp = curproc->p_fd; 5433 FILEDESC_SLOCK(fdp); 5434 fp = fget_locked(fdp, fd); 5435 mstate->dtms_getf = fp; 5436 regs[rd] = (uintptr_t)fp; 5437 FILEDESC_SUNLOCK(fdp); 5438 break; 5439 } 5440 5441 case DIF_SUBR_CLEANPATH: { 5442 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5443 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5444 uintptr_t src = tupregs[0].dttk_value; 5445 int i = 0, j = 0; 5446#if defined(sun) 5447 zone_t *z; 5448#endif 5449 5450 if (!dtrace_strcanload(src, size, mstate, vstate)) { 5451 regs[rd] = 0; 5452 break; 5453 } 5454 5455 if (!DTRACE_INSCRATCH(mstate, size)) { 5456 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5457 regs[rd] = 0; 5458 break; 5459 } 5460 5461 /* 5462 * Move forward, loading each character. 5463 */ 5464 do { 5465 c = dtrace_load8(src + i++); 5466next: 5467 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 5468 break; 5469 5470 if (c != '/') { 5471 dest[j++] = c; 5472 continue; 5473 } 5474 5475 c = dtrace_load8(src + i++); 5476 5477 if (c == '/') { 5478 /* 5479 * We have two slashes -- we can just advance 5480 * to the next character. 5481 */ 5482 goto next; 5483 } 5484 5485 if (c != '.') { 5486 /* 5487 * This is not "." and it's not ".." -- we can 5488 * just store the "/" and this character and 5489 * drive on. 5490 */ 5491 dest[j++] = '/'; 5492 dest[j++] = c; 5493 continue; 5494 } 5495 5496 c = dtrace_load8(src + i++); 5497 5498 if (c == '/') { 5499 /* 5500 * This is a "/./" component. We're not going 5501 * to store anything in the destination buffer; 5502 * we're just going to go to the next component. 5503 */ 5504 goto next; 5505 } 5506 5507 if (c != '.') { 5508 /* 5509 * This is not ".." -- we can just store the 5510 * "/." and this character and continue 5511 * processing. 5512 */ 5513 dest[j++] = '/'; 5514 dest[j++] = '.'; 5515 dest[j++] = c; 5516 continue; 5517 } 5518 5519 c = dtrace_load8(src + i++); 5520 5521 if (c != '/' && c != '\0') { 5522 /* 5523 * This is not ".." -- it's "..[mumble]". 5524 * We'll store the "/.." and this character 5525 * and continue processing. 5526 */ 5527 dest[j++] = '/'; 5528 dest[j++] = '.'; 5529 dest[j++] = '.'; 5530 dest[j++] = c; 5531 continue; 5532 } 5533 5534 /* 5535 * This is "/../" or "/..\0". We need to back up 5536 * our destination pointer until we find a "/". 5537 */ 5538 i--; 5539 while (j != 0 && dest[--j] != '/') 5540 continue; 5541 5542 if (c == '\0') 5543 dest[++j] = '/'; 5544 } while (c != '\0'); 5545 5546 dest[j] = '\0'; 5547 5548#if defined(sun) 5549 if (mstate->dtms_getf != NULL && 5550 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 5551 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 5552 /* 5553 * If we've done a getf() as a part of this ECB and we 5554 * don't have kernel access (and we're not in the global 5555 * zone), check if the path we cleaned up begins with 5556 * the zone's root path, and trim it off if so. Note 5557 * that this is an output cleanliness issue, not a 5558 * security issue: knowing one's zone root path does 5559 * not enable privilege escalation. 5560 */ 5561 if (strstr(dest, z->zone_rootpath) == dest) 5562 dest += strlen(z->zone_rootpath) - 1; 5563 } 5564#endif 5565 5566 regs[rd] = (uintptr_t)dest; 5567 mstate->dtms_scratch_ptr += size; 5568 break; 5569 } 5570 5571 case DIF_SUBR_INET_NTOA: 5572 case DIF_SUBR_INET_NTOA6: 5573 case DIF_SUBR_INET_NTOP: { 5574 size_t size; 5575 int af, argi, i; 5576 char *base, *end; 5577 5578 if (subr == DIF_SUBR_INET_NTOP) { 5579 af = (int)tupregs[0].dttk_value; 5580 argi = 1; 5581 } else { 5582 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 5583 argi = 0; 5584 } 5585 5586 if (af == AF_INET) { 5587 ipaddr_t ip4; 5588 uint8_t *ptr8, val; 5589 5590 /* 5591 * Safely load the IPv4 address. 5592 */ 5593 ip4 = dtrace_load32(tupregs[argi].dttk_value); 5594 5595 /* 5596 * Check an IPv4 string will fit in scratch. 5597 */ 5598 size = INET_ADDRSTRLEN; 5599 if (!DTRACE_INSCRATCH(mstate, size)) { 5600 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5601 regs[rd] = 0; 5602 break; 5603 } 5604 base = (char *)mstate->dtms_scratch_ptr; 5605 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5606 5607 /* 5608 * Stringify as a dotted decimal quad. 5609 */ 5610 *end-- = '\0'; 5611 ptr8 = (uint8_t *)&ip4; 5612 for (i = 3; i >= 0; i--) { 5613 val = ptr8[i]; 5614 5615 if (val == 0) { 5616 *end-- = '0'; 5617 } else { 5618 for (; val; val /= 10) { 5619 *end-- = '0' + (val % 10); 5620 } 5621 } 5622 5623 if (i > 0) 5624 *end-- = '.'; 5625 } 5626 ASSERT(end + 1 >= base); 5627 5628 } else if (af == AF_INET6) { 5629 struct in6_addr ip6; 5630 int firstzero, tryzero, numzero, v6end; 5631 uint16_t val; 5632 const char digits[] = "0123456789abcdef"; 5633 5634 /* 5635 * Stringify using RFC 1884 convention 2 - 16 bit 5636 * hexadecimal values with a zero-run compression. 5637 * Lower case hexadecimal digits are used. 5638 * eg, fe80::214:4fff:fe0b:76c8. 5639 * The IPv4 embedded form is returned for inet_ntop, 5640 * just the IPv4 string is returned for inet_ntoa6. 5641 */ 5642 5643 /* 5644 * Safely load the IPv6 address. 5645 */ 5646 dtrace_bcopy( 5647 (void *)(uintptr_t)tupregs[argi].dttk_value, 5648 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 5649 5650 /* 5651 * Check an IPv6 string will fit in scratch. 5652 */ 5653 size = INET6_ADDRSTRLEN; 5654 if (!DTRACE_INSCRATCH(mstate, size)) { 5655 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5656 regs[rd] = 0; 5657 break; 5658 } 5659 base = (char *)mstate->dtms_scratch_ptr; 5660 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5661 *end-- = '\0'; 5662 5663 /* 5664 * Find the longest run of 16 bit zero values 5665 * for the single allowed zero compression - "::". 5666 */ 5667 firstzero = -1; 5668 tryzero = -1; 5669 numzero = 1; 5670 for (i = 0; i < sizeof (struct in6_addr); i++) { 5671#if defined(sun) 5672 if (ip6._S6_un._S6_u8[i] == 0 && 5673#else 5674 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5675#endif 5676 tryzero == -1 && i % 2 == 0) { 5677 tryzero = i; 5678 continue; 5679 } 5680 5681 if (tryzero != -1 && 5682#if defined(sun) 5683 (ip6._S6_un._S6_u8[i] != 0 || 5684#else 5685 (ip6.__u6_addr.__u6_addr8[i] != 0 || 5686#endif 5687 i == sizeof (struct in6_addr) - 1)) { 5688 5689 if (i - tryzero <= numzero) { 5690 tryzero = -1; 5691 continue; 5692 } 5693 5694 firstzero = tryzero; 5695 numzero = i - i % 2 - tryzero; 5696 tryzero = -1; 5697 5698#if defined(sun) 5699 if (ip6._S6_un._S6_u8[i] == 0 && 5700#else 5701 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5702#endif 5703 i == sizeof (struct in6_addr) - 1) 5704 numzero += 2; 5705 } 5706 } 5707 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 5708 5709 /* 5710 * Check for an IPv4 embedded address. 5711 */ 5712 v6end = sizeof (struct in6_addr) - 2; 5713 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 5714 IN6_IS_ADDR_V4COMPAT(&ip6)) { 5715 for (i = sizeof (struct in6_addr) - 1; 5716 i >= DTRACE_V4MAPPED_OFFSET; i--) { 5717 ASSERT(end >= base); 5718 5719#if defined(sun) 5720 val = ip6._S6_un._S6_u8[i]; 5721#else 5722 val = ip6.__u6_addr.__u6_addr8[i]; 5723#endif 5724 5725 if (val == 0) { 5726 *end-- = '0'; 5727 } else { 5728 for (; val; val /= 10) { 5729 *end-- = '0' + val % 10; 5730 } 5731 } 5732 5733 if (i > DTRACE_V4MAPPED_OFFSET) 5734 *end-- = '.'; 5735 } 5736 5737 if (subr == DIF_SUBR_INET_NTOA6) 5738 goto inetout; 5739 5740 /* 5741 * Set v6end to skip the IPv4 address that 5742 * we have already stringified. 5743 */ 5744 v6end = 10; 5745 } 5746 5747 /* 5748 * Build the IPv6 string by working through the 5749 * address in reverse. 5750 */ 5751 for (i = v6end; i >= 0; i -= 2) { 5752 ASSERT(end >= base); 5753 5754 if (i == firstzero + numzero - 2) { 5755 *end-- = ':'; 5756 *end-- = ':'; 5757 i -= numzero - 2; 5758 continue; 5759 } 5760 5761 if (i < 14 && i != firstzero - 2) 5762 *end-- = ':'; 5763 5764#if defined(sun) 5765 val = (ip6._S6_un._S6_u8[i] << 8) + 5766 ip6._S6_un._S6_u8[i + 1]; 5767#else 5768 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 5769 ip6.__u6_addr.__u6_addr8[i + 1]; 5770#endif 5771 5772 if (val == 0) { 5773 *end-- = '0'; 5774 } else { 5775 for (; val; val /= 16) { 5776 *end-- = digits[val % 16]; 5777 } 5778 } 5779 } 5780 ASSERT(end + 1 >= base); 5781 5782 } else { 5783 /* 5784 * The user didn't use AH_INET or AH_INET6. 5785 */ 5786 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5787 regs[rd] = 0; 5788 break; 5789 } 5790 5791inetout: regs[rd] = (uintptr_t)end + 1; 5792 mstate->dtms_scratch_ptr += size; 5793 break; 5794 } 5795 5796 case DIF_SUBR_MEMREF: { 5797 uintptr_t size = 2 * sizeof(uintptr_t); 5798 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5799 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 5800 5801 /* address and length */ 5802 memref[0] = tupregs[0].dttk_value; 5803 memref[1] = tupregs[1].dttk_value; 5804 5805 regs[rd] = (uintptr_t) memref; 5806 mstate->dtms_scratch_ptr += scratch_size; 5807 break; 5808 } 5809 5810 case DIF_SUBR_TYPEREF: { 5811 uintptr_t size = 4 * sizeof(uintptr_t); 5812 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5813 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 5814 5815 /* address, num_elements, type_str, type_len */ 5816 typeref[0] = tupregs[0].dttk_value; 5817 typeref[1] = tupregs[1].dttk_value; 5818 typeref[2] = tupregs[2].dttk_value; 5819 typeref[3] = tupregs[3].dttk_value; 5820 5821 regs[rd] = (uintptr_t) typeref; 5822 mstate->dtms_scratch_ptr += scratch_size; 5823 break; 5824 } 5825 } 5826} 5827 5828/* 5829 * Emulate the execution of DTrace IR instructions specified by the given 5830 * DIF object. This function is deliberately void of assertions as all of 5831 * the necessary checks are handled by a call to dtrace_difo_validate(). 5832 */ 5833static uint64_t 5834dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 5835 dtrace_vstate_t *vstate, dtrace_state_t *state) 5836{ 5837 const dif_instr_t *text = difo->dtdo_buf; 5838 const uint_t textlen = difo->dtdo_len; 5839 const char *strtab = difo->dtdo_strtab; 5840 const uint64_t *inttab = difo->dtdo_inttab; 5841 5842 uint64_t rval = 0; 5843 dtrace_statvar_t *svar; 5844 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 5845 dtrace_difv_t *v; 5846 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 5847 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 5848 5849 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 5850 uint64_t regs[DIF_DIR_NREGS]; 5851 uint64_t *tmp; 5852 5853 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 5854 int64_t cc_r; 5855 uint_t pc = 0, id, opc = 0; 5856 uint8_t ttop = 0; 5857 dif_instr_t instr; 5858 uint_t r1, r2, rd; 5859 5860 /* 5861 * We stash the current DIF object into the machine state: we need it 5862 * for subsequent access checking. 5863 */ 5864 mstate->dtms_difo = difo; 5865 5866 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 5867 5868 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 5869 opc = pc; 5870 5871 instr = text[pc++]; 5872 r1 = DIF_INSTR_R1(instr); 5873 r2 = DIF_INSTR_R2(instr); 5874 rd = DIF_INSTR_RD(instr); 5875 5876 switch (DIF_INSTR_OP(instr)) { 5877 case DIF_OP_OR: 5878 regs[rd] = regs[r1] | regs[r2]; 5879 break; 5880 case DIF_OP_XOR: 5881 regs[rd] = regs[r1] ^ regs[r2]; 5882 break; 5883 case DIF_OP_AND: 5884 regs[rd] = regs[r1] & regs[r2]; 5885 break; 5886 case DIF_OP_SLL: 5887 regs[rd] = regs[r1] << regs[r2]; 5888 break; 5889 case DIF_OP_SRL: 5890 regs[rd] = regs[r1] >> regs[r2]; 5891 break; 5892 case DIF_OP_SUB: 5893 regs[rd] = regs[r1] - regs[r2]; 5894 break; 5895 case DIF_OP_ADD: 5896 regs[rd] = regs[r1] + regs[r2]; 5897 break; 5898 case DIF_OP_MUL: 5899 regs[rd] = regs[r1] * regs[r2]; 5900 break; 5901 case DIF_OP_SDIV: 5902 if (regs[r2] == 0) { 5903 regs[rd] = 0; 5904 *flags |= CPU_DTRACE_DIVZERO; 5905 } else { 5906 regs[rd] = (int64_t)regs[r1] / 5907 (int64_t)regs[r2]; 5908 } 5909 break; 5910 5911 case DIF_OP_UDIV: 5912 if (regs[r2] == 0) { 5913 regs[rd] = 0; 5914 *flags |= CPU_DTRACE_DIVZERO; 5915 } else { 5916 regs[rd] = regs[r1] / regs[r2]; 5917 } 5918 break; 5919 5920 case DIF_OP_SREM: 5921 if (regs[r2] == 0) { 5922 regs[rd] = 0; 5923 *flags |= CPU_DTRACE_DIVZERO; 5924 } else { 5925 regs[rd] = (int64_t)regs[r1] % 5926 (int64_t)regs[r2]; 5927 } 5928 break; 5929 5930 case DIF_OP_UREM: 5931 if (regs[r2] == 0) { 5932 regs[rd] = 0; 5933 *flags |= CPU_DTRACE_DIVZERO; 5934 } else { 5935 regs[rd] = regs[r1] % regs[r2]; 5936 } 5937 break; 5938 5939 case DIF_OP_NOT: 5940 regs[rd] = ~regs[r1]; 5941 break; 5942 case DIF_OP_MOV: 5943 regs[rd] = regs[r1]; 5944 break; 5945 case DIF_OP_CMP: 5946 cc_r = regs[r1] - regs[r2]; 5947 cc_n = cc_r < 0; 5948 cc_z = cc_r == 0; 5949 cc_v = 0; 5950 cc_c = regs[r1] < regs[r2]; 5951 break; 5952 case DIF_OP_TST: 5953 cc_n = cc_v = cc_c = 0; 5954 cc_z = regs[r1] == 0; 5955 break; 5956 case DIF_OP_BA: 5957 pc = DIF_INSTR_LABEL(instr); 5958 break; 5959 case DIF_OP_BE: 5960 if (cc_z) 5961 pc = DIF_INSTR_LABEL(instr); 5962 break; 5963 case DIF_OP_BNE: 5964 if (cc_z == 0) 5965 pc = DIF_INSTR_LABEL(instr); 5966 break; 5967 case DIF_OP_BG: 5968 if ((cc_z | (cc_n ^ cc_v)) == 0) 5969 pc = DIF_INSTR_LABEL(instr); 5970 break; 5971 case DIF_OP_BGU: 5972 if ((cc_c | cc_z) == 0) 5973 pc = DIF_INSTR_LABEL(instr); 5974 break; 5975 case DIF_OP_BGE: 5976 if ((cc_n ^ cc_v) == 0) 5977 pc = DIF_INSTR_LABEL(instr); 5978 break; 5979 case DIF_OP_BGEU: 5980 if (cc_c == 0) 5981 pc = DIF_INSTR_LABEL(instr); 5982 break; 5983 case DIF_OP_BL: 5984 if (cc_n ^ cc_v) 5985 pc = DIF_INSTR_LABEL(instr); 5986 break; 5987 case DIF_OP_BLU: 5988 if (cc_c) 5989 pc = DIF_INSTR_LABEL(instr); 5990 break; 5991 case DIF_OP_BLE: 5992 if (cc_z | (cc_n ^ cc_v)) 5993 pc = DIF_INSTR_LABEL(instr); 5994 break; 5995 case DIF_OP_BLEU: 5996 if (cc_c | cc_z) 5997 pc = DIF_INSTR_LABEL(instr); 5998 break; 5999 case DIF_OP_RLDSB: 6000 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6001 break; 6002 /*FALLTHROUGH*/ 6003 case DIF_OP_LDSB: 6004 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 6005 break; 6006 case DIF_OP_RLDSH: 6007 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6008 break; 6009 /*FALLTHROUGH*/ 6010 case DIF_OP_LDSH: 6011 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 6012 break; 6013 case DIF_OP_RLDSW: 6014 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6015 break; 6016 /*FALLTHROUGH*/ 6017 case DIF_OP_LDSW: 6018 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 6019 break; 6020 case DIF_OP_RLDUB: 6021 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6022 break; 6023 /*FALLTHROUGH*/ 6024 case DIF_OP_LDUB: 6025 regs[rd] = dtrace_load8(regs[r1]); 6026 break; 6027 case DIF_OP_RLDUH: 6028 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6029 break; 6030 /*FALLTHROUGH*/ 6031 case DIF_OP_LDUH: 6032 regs[rd] = dtrace_load16(regs[r1]); 6033 break; 6034 case DIF_OP_RLDUW: 6035 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6036 break; 6037 /*FALLTHROUGH*/ 6038 case DIF_OP_LDUW: 6039 regs[rd] = dtrace_load32(regs[r1]); 6040 break; 6041 case DIF_OP_RLDX: 6042 if (!dtrace_canload(regs[r1], 8, mstate, vstate)) 6043 break; 6044 /*FALLTHROUGH*/ 6045 case DIF_OP_LDX: 6046 regs[rd] = dtrace_load64(regs[r1]); 6047 break; 6048 case DIF_OP_ULDSB: 6049 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6050 regs[rd] = (int8_t) 6051 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6052 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6053 break; 6054 case DIF_OP_ULDSH: 6055 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6056 regs[rd] = (int16_t) 6057 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6058 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6059 break; 6060 case DIF_OP_ULDSW: 6061 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6062 regs[rd] = (int32_t) 6063 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6064 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6065 break; 6066 case DIF_OP_ULDUB: 6067 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6068 regs[rd] = 6069 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6070 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6071 break; 6072 case DIF_OP_ULDUH: 6073 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6074 regs[rd] = 6075 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6076 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6077 break; 6078 case DIF_OP_ULDUW: 6079 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6080 regs[rd] = 6081 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6082 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6083 break; 6084 case DIF_OP_ULDX: 6085 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6086 regs[rd] = 6087 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 6088 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6089 break; 6090 case DIF_OP_RET: 6091 rval = regs[rd]; 6092 pc = textlen; 6093 break; 6094 case DIF_OP_NOP: 6095 break; 6096 case DIF_OP_SETX: 6097 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 6098 break; 6099 case DIF_OP_SETS: 6100 regs[rd] = (uint64_t)(uintptr_t) 6101 (strtab + DIF_INSTR_STRING(instr)); 6102 break; 6103 case DIF_OP_SCMP: { 6104 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 6105 uintptr_t s1 = regs[r1]; 6106 uintptr_t s2 = regs[r2]; 6107 6108 if (s1 != 0 && 6109 !dtrace_strcanload(s1, sz, mstate, vstate)) 6110 break; 6111 if (s2 != 0 && 6112 !dtrace_strcanload(s2, sz, mstate, vstate)) 6113 break; 6114 6115 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 6116 6117 cc_n = cc_r < 0; 6118 cc_z = cc_r == 0; 6119 cc_v = cc_c = 0; 6120 break; 6121 } 6122 case DIF_OP_LDGA: 6123 regs[rd] = dtrace_dif_variable(mstate, state, 6124 r1, regs[r2]); 6125 break; 6126 case DIF_OP_LDGS: 6127 id = DIF_INSTR_VAR(instr); 6128 6129 if (id >= DIF_VAR_OTHER_UBASE) { 6130 uintptr_t a; 6131 6132 id -= DIF_VAR_OTHER_UBASE; 6133 svar = vstate->dtvs_globals[id]; 6134 ASSERT(svar != NULL); 6135 v = &svar->dtsv_var; 6136 6137 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 6138 regs[rd] = svar->dtsv_data; 6139 break; 6140 } 6141 6142 a = (uintptr_t)svar->dtsv_data; 6143 6144 if (*(uint8_t *)a == UINT8_MAX) { 6145 /* 6146 * If the 0th byte is set to UINT8_MAX 6147 * then this is to be treated as a 6148 * reference to a NULL variable. 6149 */ 6150 regs[rd] = 0; 6151 } else { 6152 regs[rd] = a + sizeof (uint64_t); 6153 } 6154 6155 break; 6156 } 6157 6158 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 6159 break; 6160 6161 case DIF_OP_STGS: 6162 id = DIF_INSTR_VAR(instr); 6163 6164 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6165 id -= DIF_VAR_OTHER_UBASE; 6166 6167 svar = vstate->dtvs_globals[id]; 6168 ASSERT(svar != NULL); 6169 v = &svar->dtsv_var; 6170 6171 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6172 uintptr_t a = (uintptr_t)svar->dtsv_data; 6173 6174 ASSERT(a != 0); 6175 ASSERT(svar->dtsv_size != 0); 6176 6177 if (regs[rd] == 0) { 6178 *(uint8_t *)a = UINT8_MAX; 6179 break; 6180 } else { 6181 *(uint8_t *)a = 0; 6182 a += sizeof (uint64_t); 6183 } 6184 if (!dtrace_vcanload( 6185 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6186 mstate, vstate)) 6187 break; 6188 6189 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6190 (void *)a, &v->dtdv_type); 6191 break; 6192 } 6193 6194 svar->dtsv_data = regs[rd]; 6195 break; 6196 6197 case DIF_OP_LDTA: 6198 /* 6199 * There are no DTrace built-in thread-local arrays at 6200 * present. This opcode is saved for future work. 6201 */ 6202 *flags |= CPU_DTRACE_ILLOP; 6203 regs[rd] = 0; 6204 break; 6205 6206 case DIF_OP_LDLS: 6207 id = DIF_INSTR_VAR(instr); 6208 6209 if (id < DIF_VAR_OTHER_UBASE) { 6210 /* 6211 * For now, this has no meaning. 6212 */ 6213 regs[rd] = 0; 6214 break; 6215 } 6216 6217 id -= DIF_VAR_OTHER_UBASE; 6218 6219 ASSERT(id < vstate->dtvs_nlocals); 6220 ASSERT(vstate->dtvs_locals != NULL); 6221 6222 svar = vstate->dtvs_locals[id]; 6223 ASSERT(svar != NULL); 6224 v = &svar->dtsv_var; 6225 6226 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6227 uintptr_t a = (uintptr_t)svar->dtsv_data; 6228 size_t sz = v->dtdv_type.dtdt_size; 6229 6230 sz += sizeof (uint64_t); 6231 ASSERT(svar->dtsv_size == NCPU * sz); 6232 a += curcpu * sz; 6233 6234 if (*(uint8_t *)a == UINT8_MAX) { 6235 /* 6236 * If the 0th byte is set to UINT8_MAX 6237 * then this is to be treated as a 6238 * reference to a NULL variable. 6239 */ 6240 regs[rd] = 0; 6241 } else { 6242 regs[rd] = a + sizeof (uint64_t); 6243 } 6244 6245 break; 6246 } 6247 6248 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6249 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6250 regs[rd] = tmp[curcpu]; 6251 break; 6252 6253 case DIF_OP_STLS: 6254 id = DIF_INSTR_VAR(instr); 6255 6256 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6257 id -= DIF_VAR_OTHER_UBASE; 6258 ASSERT(id < vstate->dtvs_nlocals); 6259 6260 ASSERT(vstate->dtvs_locals != NULL); 6261 svar = vstate->dtvs_locals[id]; 6262 ASSERT(svar != NULL); 6263 v = &svar->dtsv_var; 6264 6265 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6266 uintptr_t a = (uintptr_t)svar->dtsv_data; 6267 size_t sz = v->dtdv_type.dtdt_size; 6268 6269 sz += sizeof (uint64_t); 6270 ASSERT(svar->dtsv_size == NCPU * sz); 6271 a += curcpu * sz; 6272 6273 if (regs[rd] == 0) { 6274 *(uint8_t *)a = UINT8_MAX; 6275 break; 6276 } else { 6277 *(uint8_t *)a = 0; 6278 a += sizeof (uint64_t); 6279 } 6280 6281 if (!dtrace_vcanload( 6282 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6283 mstate, vstate)) 6284 break; 6285 6286 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6287 (void *)a, &v->dtdv_type); 6288 break; 6289 } 6290 6291 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6292 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6293 tmp[curcpu] = regs[rd]; 6294 break; 6295 6296 case DIF_OP_LDTS: { 6297 dtrace_dynvar_t *dvar; 6298 dtrace_key_t *key; 6299 6300 id = DIF_INSTR_VAR(instr); 6301 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6302 id -= DIF_VAR_OTHER_UBASE; 6303 v = &vstate->dtvs_tlocals[id]; 6304 6305 key = &tupregs[DIF_DTR_NREGS]; 6306 key[0].dttk_value = (uint64_t)id; 6307 key[0].dttk_size = 0; 6308 DTRACE_TLS_THRKEY(key[1].dttk_value); 6309 key[1].dttk_size = 0; 6310 6311 dvar = dtrace_dynvar(dstate, 2, key, 6312 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 6313 mstate, vstate); 6314 6315 if (dvar == NULL) { 6316 regs[rd] = 0; 6317 break; 6318 } 6319 6320 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6321 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6322 } else { 6323 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6324 } 6325 6326 break; 6327 } 6328 6329 case DIF_OP_STTS: { 6330 dtrace_dynvar_t *dvar; 6331 dtrace_key_t *key; 6332 6333 id = DIF_INSTR_VAR(instr); 6334 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6335 id -= DIF_VAR_OTHER_UBASE; 6336 6337 key = &tupregs[DIF_DTR_NREGS]; 6338 key[0].dttk_value = (uint64_t)id; 6339 key[0].dttk_size = 0; 6340 DTRACE_TLS_THRKEY(key[1].dttk_value); 6341 key[1].dttk_size = 0; 6342 v = &vstate->dtvs_tlocals[id]; 6343 6344 dvar = dtrace_dynvar(dstate, 2, key, 6345 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6346 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6347 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6348 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6349 6350 /* 6351 * Given that we're storing to thread-local data, 6352 * we need to flush our predicate cache. 6353 */ 6354 curthread->t_predcache = 0; 6355 6356 if (dvar == NULL) 6357 break; 6358 6359 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6360 if (!dtrace_vcanload( 6361 (void *)(uintptr_t)regs[rd], 6362 &v->dtdv_type, mstate, vstate)) 6363 break; 6364 6365 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6366 dvar->dtdv_data, &v->dtdv_type); 6367 } else { 6368 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6369 } 6370 6371 break; 6372 } 6373 6374 case DIF_OP_SRA: 6375 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 6376 break; 6377 6378 case DIF_OP_CALL: 6379 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 6380 regs, tupregs, ttop, mstate, state); 6381 break; 6382 6383 case DIF_OP_PUSHTR: 6384 if (ttop == DIF_DTR_NREGS) { 6385 *flags |= CPU_DTRACE_TUPOFLOW; 6386 break; 6387 } 6388 6389 if (r1 == DIF_TYPE_STRING) { 6390 /* 6391 * If this is a string type and the size is 0, 6392 * we'll use the system-wide default string 6393 * size. Note that we are _not_ looking at 6394 * the value of the DTRACEOPT_STRSIZE option; 6395 * had this been set, we would expect to have 6396 * a non-zero size value in the "pushtr". 6397 */ 6398 tupregs[ttop].dttk_size = 6399 dtrace_strlen((char *)(uintptr_t)regs[rd], 6400 regs[r2] ? regs[r2] : 6401 dtrace_strsize_default) + 1; 6402 } else { 6403 tupregs[ttop].dttk_size = regs[r2]; 6404 } 6405 6406 tupregs[ttop++].dttk_value = regs[rd]; 6407 break; 6408 6409 case DIF_OP_PUSHTV: 6410 if (ttop == DIF_DTR_NREGS) { 6411 *flags |= CPU_DTRACE_TUPOFLOW; 6412 break; 6413 } 6414 6415 tupregs[ttop].dttk_value = regs[rd]; 6416 tupregs[ttop++].dttk_size = 0; 6417 break; 6418 6419 case DIF_OP_POPTS: 6420 if (ttop != 0) 6421 ttop--; 6422 break; 6423 6424 case DIF_OP_FLUSHTS: 6425 ttop = 0; 6426 break; 6427 6428 case DIF_OP_LDGAA: 6429 case DIF_OP_LDTAA: { 6430 dtrace_dynvar_t *dvar; 6431 dtrace_key_t *key = tupregs; 6432 uint_t nkeys = ttop; 6433 6434 id = DIF_INSTR_VAR(instr); 6435 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6436 id -= DIF_VAR_OTHER_UBASE; 6437 6438 key[nkeys].dttk_value = (uint64_t)id; 6439 key[nkeys++].dttk_size = 0; 6440 6441 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 6442 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6443 key[nkeys++].dttk_size = 0; 6444 v = &vstate->dtvs_tlocals[id]; 6445 } else { 6446 v = &vstate->dtvs_globals[id]->dtsv_var; 6447 } 6448 6449 dvar = dtrace_dynvar(dstate, nkeys, key, 6450 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6451 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6452 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 6453 6454 if (dvar == NULL) { 6455 regs[rd] = 0; 6456 break; 6457 } 6458 6459 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6460 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6461 } else { 6462 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6463 } 6464 6465 break; 6466 } 6467 6468 case DIF_OP_STGAA: 6469 case DIF_OP_STTAA: { 6470 dtrace_dynvar_t *dvar; 6471 dtrace_key_t *key = tupregs; 6472 uint_t nkeys = ttop; 6473 6474 id = DIF_INSTR_VAR(instr); 6475 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6476 id -= DIF_VAR_OTHER_UBASE; 6477 6478 key[nkeys].dttk_value = (uint64_t)id; 6479 key[nkeys++].dttk_size = 0; 6480 6481 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 6482 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6483 key[nkeys++].dttk_size = 0; 6484 v = &vstate->dtvs_tlocals[id]; 6485 } else { 6486 v = &vstate->dtvs_globals[id]->dtsv_var; 6487 } 6488 6489 dvar = dtrace_dynvar(dstate, nkeys, key, 6490 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6491 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6492 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6493 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6494 6495 if (dvar == NULL) 6496 break; 6497 6498 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6499 if (!dtrace_vcanload( 6500 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6501 mstate, vstate)) 6502 break; 6503 6504 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6505 dvar->dtdv_data, &v->dtdv_type); 6506 } else { 6507 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6508 } 6509 6510 break; 6511 } 6512 6513 case DIF_OP_ALLOCS: { 6514 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6515 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 6516 6517 /* 6518 * Rounding up the user allocation size could have 6519 * overflowed large, bogus allocations (like -1ULL) to 6520 * 0. 6521 */ 6522 if (size < regs[r1] || 6523 !DTRACE_INSCRATCH(mstate, size)) { 6524 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6525 regs[rd] = 0; 6526 break; 6527 } 6528 6529 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 6530 mstate->dtms_scratch_ptr += size; 6531 regs[rd] = ptr; 6532 break; 6533 } 6534 6535 case DIF_OP_COPYS: 6536 if (!dtrace_canstore(regs[rd], regs[r2], 6537 mstate, vstate)) { 6538 *flags |= CPU_DTRACE_BADADDR; 6539 *illval = regs[rd]; 6540 break; 6541 } 6542 6543 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 6544 break; 6545 6546 dtrace_bcopy((void *)(uintptr_t)regs[r1], 6547 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 6548 break; 6549 6550 case DIF_OP_STB: 6551 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 6552 *flags |= CPU_DTRACE_BADADDR; 6553 *illval = regs[rd]; 6554 break; 6555 } 6556 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 6557 break; 6558 6559 case DIF_OP_STH: 6560 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 6561 *flags |= CPU_DTRACE_BADADDR; 6562 *illval = regs[rd]; 6563 break; 6564 } 6565 if (regs[rd] & 1) { 6566 *flags |= CPU_DTRACE_BADALIGN; 6567 *illval = regs[rd]; 6568 break; 6569 } 6570 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 6571 break; 6572 6573 case DIF_OP_STW: 6574 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 6575 *flags |= CPU_DTRACE_BADADDR; 6576 *illval = regs[rd]; 6577 break; 6578 } 6579 if (regs[rd] & 3) { 6580 *flags |= CPU_DTRACE_BADALIGN; 6581 *illval = regs[rd]; 6582 break; 6583 } 6584 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 6585 break; 6586 6587 case DIF_OP_STX: 6588 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 6589 *flags |= CPU_DTRACE_BADADDR; 6590 *illval = regs[rd]; 6591 break; 6592 } 6593 if (regs[rd] & 7) { 6594 *flags |= CPU_DTRACE_BADALIGN; 6595 *illval = regs[rd]; 6596 break; 6597 } 6598 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 6599 break; 6600 } 6601 } 6602 6603 if (!(*flags & CPU_DTRACE_FAULT)) 6604 return (rval); 6605 6606 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 6607 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 6608 6609 return (0); 6610} 6611 6612static void 6613dtrace_action_breakpoint(dtrace_ecb_t *ecb) 6614{ 6615 dtrace_probe_t *probe = ecb->dte_probe; 6616 dtrace_provider_t *prov = probe->dtpr_provider; 6617 char c[DTRACE_FULLNAMELEN + 80], *str; 6618 char *msg = "dtrace: breakpoint action at probe "; 6619 char *ecbmsg = " (ecb "; 6620 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 6621 uintptr_t val = (uintptr_t)ecb; 6622 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 6623 6624 if (dtrace_destructive_disallow) 6625 return; 6626 6627 /* 6628 * It's impossible to be taking action on the NULL probe. 6629 */ 6630 ASSERT(probe != NULL); 6631 6632 /* 6633 * This is a poor man's (destitute man's?) sprintf(): we want to 6634 * print the provider name, module name, function name and name of 6635 * the probe, along with the hex address of the ECB with the breakpoint 6636 * action -- all of which we must place in the character buffer by 6637 * hand. 6638 */ 6639 while (*msg != '\0') 6640 c[i++] = *msg++; 6641 6642 for (str = prov->dtpv_name; *str != '\0'; str++) 6643 c[i++] = *str; 6644 c[i++] = ':'; 6645 6646 for (str = probe->dtpr_mod; *str != '\0'; str++) 6647 c[i++] = *str; 6648 c[i++] = ':'; 6649 6650 for (str = probe->dtpr_func; *str != '\0'; str++) 6651 c[i++] = *str; 6652 c[i++] = ':'; 6653 6654 for (str = probe->dtpr_name; *str != '\0'; str++) 6655 c[i++] = *str; 6656 6657 while (*ecbmsg != '\0') 6658 c[i++] = *ecbmsg++; 6659 6660 while (shift >= 0) { 6661 mask = (uintptr_t)0xf << shift; 6662 6663 if (val >= ((uintptr_t)1 << shift)) 6664 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 6665 shift -= 4; 6666 } 6667 6668 c[i++] = ')'; 6669 c[i] = '\0'; 6670 6671#if defined(sun) 6672 debug_enter(c); 6673#else 6674 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 6675#endif 6676} 6677 6678static void 6679dtrace_action_panic(dtrace_ecb_t *ecb) 6680{ 6681 dtrace_probe_t *probe = ecb->dte_probe; 6682 6683 /* 6684 * It's impossible to be taking action on the NULL probe. 6685 */ 6686 ASSERT(probe != NULL); 6687 6688 if (dtrace_destructive_disallow) 6689 return; 6690 6691 if (dtrace_panicked != NULL) 6692 return; 6693 6694 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 6695 return; 6696 6697 /* 6698 * We won the right to panic. (We want to be sure that only one 6699 * thread calls panic() from dtrace_probe(), and that panic() is 6700 * called exactly once.) 6701 */ 6702 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 6703 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 6704 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 6705} 6706 6707static void 6708dtrace_action_raise(uint64_t sig) 6709{ 6710 if (dtrace_destructive_disallow) 6711 return; 6712 6713 if (sig >= NSIG) { 6714 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 6715 return; 6716 } 6717 6718#if defined(sun) 6719 /* 6720 * raise() has a queue depth of 1 -- we ignore all subsequent 6721 * invocations of the raise() action. 6722 */ 6723 if (curthread->t_dtrace_sig == 0) 6724 curthread->t_dtrace_sig = (uint8_t)sig; 6725 6726 curthread->t_sig_check = 1; 6727 aston(curthread); 6728#else 6729 struct proc *p = curproc; 6730 PROC_LOCK(p); 6731 kern_psignal(p, sig); 6732 PROC_UNLOCK(p); 6733#endif 6734} 6735 6736static void 6737dtrace_action_stop(void) 6738{ 6739 if (dtrace_destructive_disallow) 6740 return; 6741 6742#if defined(sun) 6743 if (!curthread->t_dtrace_stop) { 6744 curthread->t_dtrace_stop = 1; 6745 curthread->t_sig_check = 1; 6746 aston(curthread); 6747 } 6748#else 6749 struct proc *p = curproc; 6750 PROC_LOCK(p); 6751 kern_psignal(p, SIGSTOP); 6752 PROC_UNLOCK(p); 6753#endif 6754} 6755 6756static void 6757dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 6758{ 6759 hrtime_t now; 6760 volatile uint16_t *flags; 6761#if defined(sun) 6762 cpu_t *cpu = CPU; 6763#else 6764 cpu_t *cpu = &solaris_cpu[curcpu]; 6765#endif 6766 6767 if (dtrace_destructive_disallow) 6768 return; 6769 6770 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 6771 6772 now = dtrace_gethrtime(); 6773 6774 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 6775 /* 6776 * We need to advance the mark to the current time. 6777 */ 6778 cpu->cpu_dtrace_chillmark = now; 6779 cpu->cpu_dtrace_chilled = 0; 6780 } 6781 6782 /* 6783 * Now check to see if the requested chill time would take us over 6784 * the maximum amount of time allowed in the chill interval. (Or 6785 * worse, if the calculation itself induces overflow.) 6786 */ 6787 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 6788 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 6789 *flags |= CPU_DTRACE_ILLOP; 6790 return; 6791 } 6792 6793 while (dtrace_gethrtime() - now < val) 6794 continue; 6795 6796 /* 6797 * Normally, we assure that the value of the variable "timestamp" does 6798 * not change within an ECB. The presence of chill() represents an 6799 * exception to this rule, however. 6800 */ 6801 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 6802 cpu->cpu_dtrace_chilled += val; 6803} 6804 6805static void 6806dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 6807 uint64_t *buf, uint64_t arg) 6808{ 6809 int nframes = DTRACE_USTACK_NFRAMES(arg); 6810 int strsize = DTRACE_USTACK_STRSIZE(arg); 6811 uint64_t *pcs = &buf[1], *fps; 6812 char *str = (char *)&pcs[nframes]; 6813 int size, offs = 0, i, j; 6814 uintptr_t old = mstate->dtms_scratch_ptr, saved; 6815 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 6816 char *sym; 6817 6818 /* 6819 * Should be taking a faster path if string space has not been 6820 * allocated. 6821 */ 6822 ASSERT(strsize != 0); 6823 6824 /* 6825 * We will first allocate some temporary space for the frame pointers. 6826 */ 6827 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6828 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 6829 (nframes * sizeof (uint64_t)); 6830 6831 if (!DTRACE_INSCRATCH(mstate, size)) { 6832 /* 6833 * Not enough room for our frame pointers -- need to indicate 6834 * that we ran out of scratch space. 6835 */ 6836 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6837 return; 6838 } 6839 6840 mstate->dtms_scratch_ptr += size; 6841 saved = mstate->dtms_scratch_ptr; 6842 6843 /* 6844 * Now get a stack with both program counters and frame pointers. 6845 */ 6846 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6847 dtrace_getufpstack(buf, fps, nframes + 1); 6848 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6849 6850 /* 6851 * If that faulted, we're cooked. 6852 */ 6853 if (*flags & CPU_DTRACE_FAULT) 6854 goto out; 6855 6856 /* 6857 * Now we want to walk up the stack, calling the USTACK helper. For 6858 * each iteration, we restore the scratch pointer. 6859 */ 6860 for (i = 0; i < nframes; i++) { 6861 mstate->dtms_scratch_ptr = saved; 6862 6863 if (offs >= strsize) 6864 break; 6865 6866 sym = (char *)(uintptr_t)dtrace_helper( 6867 DTRACE_HELPER_ACTION_USTACK, 6868 mstate, state, pcs[i], fps[i]); 6869 6870 /* 6871 * If we faulted while running the helper, we're going to 6872 * clear the fault and null out the corresponding string. 6873 */ 6874 if (*flags & CPU_DTRACE_FAULT) { 6875 *flags &= ~CPU_DTRACE_FAULT; 6876 str[offs++] = '\0'; 6877 continue; 6878 } 6879 6880 if (sym == NULL) { 6881 str[offs++] = '\0'; 6882 continue; 6883 } 6884 6885 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6886 6887 /* 6888 * Now copy in the string that the helper returned to us. 6889 */ 6890 for (j = 0; offs + j < strsize; j++) { 6891 if ((str[offs + j] = sym[j]) == '\0') 6892 break; 6893 } 6894 6895 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6896 6897 offs += j + 1; 6898 } 6899 6900 if (offs >= strsize) { 6901 /* 6902 * If we didn't have room for all of the strings, we don't 6903 * abort processing -- this needn't be a fatal error -- but we 6904 * still want to increment a counter (dts_stkstroverflows) to 6905 * allow this condition to be warned about. (If this is from 6906 * a jstack() action, it is easily tuned via jstackstrsize.) 6907 */ 6908 dtrace_error(&state->dts_stkstroverflows); 6909 } 6910 6911 while (offs < strsize) 6912 str[offs++] = '\0'; 6913 6914out: 6915 mstate->dtms_scratch_ptr = old; 6916} 6917 6918static void 6919dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size, 6920 size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind) 6921{ 6922 volatile uint16_t *flags; 6923 uint64_t val = *valp; 6924 size_t valoffs = *valoffsp; 6925 6926 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 6927 ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF); 6928 6929 /* 6930 * If this is a string, we're going to only load until we find the zero 6931 * byte -- after which we'll store zero bytes. 6932 */ 6933 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 6934 char c = '\0' + 1; 6935 size_t s; 6936 6937 for (s = 0; s < size; s++) { 6938 if (c != '\0' && dtkind == DIF_TF_BYREF) { 6939 c = dtrace_load8(val++); 6940 } else if (c != '\0' && dtkind == DIF_TF_BYUREF) { 6941 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6942 c = dtrace_fuword8((void *)(uintptr_t)val++); 6943 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6944 if (*flags & CPU_DTRACE_FAULT) 6945 break; 6946 } 6947 6948 DTRACE_STORE(uint8_t, tomax, valoffs++, c); 6949 6950 if (c == '\0' && intuple) 6951 break; 6952 } 6953 } else { 6954 uint8_t c; 6955 while (valoffs < end) { 6956 if (dtkind == DIF_TF_BYREF) { 6957 c = dtrace_load8(val++); 6958 } else if (dtkind == DIF_TF_BYUREF) { 6959 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6960 c = dtrace_fuword8((void *)(uintptr_t)val++); 6961 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6962 if (*flags & CPU_DTRACE_FAULT) 6963 break; 6964 } 6965 6966 DTRACE_STORE(uint8_t, tomax, 6967 valoffs++, c); 6968 } 6969 } 6970 6971 *valp = val; 6972 *valoffsp = valoffs; 6973} 6974 6975/* 6976 * If you're looking for the epicenter of DTrace, you just found it. This 6977 * is the function called by the provider to fire a probe -- from which all 6978 * subsequent probe-context DTrace activity emanates. 6979 */ 6980void 6981dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 6982 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 6983{ 6984 processorid_t cpuid; 6985 dtrace_icookie_t cookie; 6986 dtrace_probe_t *probe; 6987 dtrace_mstate_t mstate; 6988 dtrace_ecb_t *ecb; 6989 dtrace_action_t *act; 6990 intptr_t offs; 6991 size_t size; 6992 int vtime, onintr; 6993 volatile uint16_t *flags; 6994 hrtime_t now; 6995 6996 if (panicstr != NULL) 6997 return; 6998 6999#if defined(sun) 7000 /* 7001 * Kick out immediately if this CPU is still being born (in which case 7002 * curthread will be set to -1) or the current thread can't allow 7003 * probes in its current context. 7004 */ 7005 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 7006 return; 7007#endif 7008 7009 cookie = dtrace_interrupt_disable(); 7010 probe = dtrace_probes[id - 1]; 7011 cpuid = curcpu; 7012 onintr = CPU_ON_INTR(CPU); 7013 7014 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 7015 probe->dtpr_predcache == curthread->t_predcache) { 7016 /* 7017 * We have hit in the predicate cache; we know that 7018 * this predicate would evaluate to be false. 7019 */ 7020 dtrace_interrupt_enable(cookie); 7021 return; 7022 } 7023 7024#if defined(sun) 7025 if (panic_quiesce) { 7026#else 7027 if (panicstr != NULL) { 7028#endif 7029 /* 7030 * We don't trace anything if we're panicking. 7031 */ 7032 dtrace_interrupt_enable(cookie); 7033 return; 7034 } 7035 7036 now = dtrace_gethrtime(); 7037 vtime = dtrace_vtime_references != 0; 7038 7039 if (vtime && curthread->t_dtrace_start) 7040 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 7041 7042 mstate.dtms_difo = NULL; 7043 mstate.dtms_probe = probe; 7044 mstate.dtms_strtok = 0; 7045 mstate.dtms_arg[0] = arg0; 7046 mstate.dtms_arg[1] = arg1; 7047 mstate.dtms_arg[2] = arg2; 7048 mstate.dtms_arg[3] = arg3; 7049 mstate.dtms_arg[4] = arg4; 7050 7051 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 7052 7053 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 7054 dtrace_predicate_t *pred = ecb->dte_predicate; 7055 dtrace_state_t *state = ecb->dte_state; 7056 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 7057 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 7058 dtrace_vstate_t *vstate = &state->dts_vstate; 7059 dtrace_provider_t *prov = probe->dtpr_provider; 7060 uint64_t tracememsize = 0; 7061 int committed = 0; 7062 caddr_t tomax; 7063 7064 /* 7065 * A little subtlety with the following (seemingly innocuous) 7066 * declaration of the automatic 'val': by looking at the 7067 * code, you might think that it could be declared in the 7068 * action processing loop, below. (That is, it's only used in 7069 * the action processing loop.) However, it must be declared 7070 * out of that scope because in the case of DIF expression 7071 * arguments to aggregating actions, one iteration of the 7072 * action loop will use the last iteration's value. 7073 */ 7074 uint64_t val = 0; 7075 7076 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 7077 mstate.dtms_getf = NULL; 7078 7079 *flags &= ~CPU_DTRACE_ERROR; 7080 7081 if (prov == dtrace_provider) { 7082 /* 7083 * If dtrace itself is the provider of this probe, 7084 * we're only going to continue processing the ECB if 7085 * arg0 (the dtrace_state_t) is equal to the ECB's 7086 * creating state. (This prevents disjoint consumers 7087 * from seeing one another's metaprobes.) 7088 */ 7089 if (arg0 != (uint64_t)(uintptr_t)state) 7090 continue; 7091 } 7092 7093 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 7094 /* 7095 * We're not currently active. If our provider isn't 7096 * the dtrace pseudo provider, we're not interested. 7097 */ 7098 if (prov != dtrace_provider) 7099 continue; 7100 7101 /* 7102 * Now we must further check if we are in the BEGIN 7103 * probe. If we are, we will only continue processing 7104 * if we're still in WARMUP -- if one BEGIN enabling 7105 * has invoked the exit() action, we don't want to 7106 * evaluate subsequent BEGIN enablings. 7107 */ 7108 if (probe->dtpr_id == dtrace_probeid_begin && 7109 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 7110 ASSERT(state->dts_activity == 7111 DTRACE_ACTIVITY_DRAINING); 7112 continue; 7113 } 7114 } 7115 7116 if (ecb->dte_cond) { 7117 /* 7118 * If the dte_cond bits indicate that this 7119 * consumer is only allowed to see user-mode firings 7120 * of this probe, call the provider's dtps_usermode() 7121 * entry point to check that the probe was fired 7122 * while in a user context. Skip this ECB if that's 7123 * not the case. 7124 */ 7125 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 7126 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 7127 probe->dtpr_id, probe->dtpr_arg) == 0) 7128 continue; 7129 7130#if defined(sun) 7131 /* 7132 * This is more subtle than it looks. We have to be 7133 * absolutely certain that CRED() isn't going to 7134 * change out from under us so it's only legit to 7135 * examine that structure if we're in constrained 7136 * situations. Currently, the only times we'll this 7137 * check is if a non-super-user has enabled the 7138 * profile or syscall providers -- providers that 7139 * allow visibility of all processes. For the 7140 * profile case, the check above will ensure that 7141 * we're examining a user context. 7142 */ 7143 if (ecb->dte_cond & DTRACE_COND_OWNER) { 7144 cred_t *cr; 7145 cred_t *s_cr = 7146 ecb->dte_state->dts_cred.dcr_cred; 7147 proc_t *proc; 7148 7149 ASSERT(s_cr != NULL); 7150 7151 if ((cr = CRED()) == NULL || 7152 s_cr->cr_uid != cr->cr_uid || 7153 s_cr->cr_uid != cr->cr_ruid || 7154 s_cr->cr_uid != cr->cr_suid || 7155 s_cr->cr_gid != cr->cr_gid || 7156 s_cr->cr_gid != cr->cr_rgid || 7157 s_cr->cr_gid != cr->cr_sgid || 7158 (proc = ttoproc(curthread)) == NULL || 7159 (proc->p_flag & SNOCD)) 7160 continue; 7161 } 7162 7163 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 7164 cred_t *cr; 7165 cred_t *s_cr = 7166 ecb->dte_state->dts_cred.dcr_cred; 7167 7168 ASSERT(s_cr != NULL); 7169 7170 if ((cr = CRED()) == NULL || 7171 s_cr->cr_zone->zone_id != 7172 cr->cr_zone->zone_id) 7173 continue; 7174 } 7175#endif 7176 } 7177 7178 if (now - state->dts_alive > dtrace_deadman_timeout) { 7179 /* 7180 * We seem to be dead. Unless we (a) have kernel 7181 * destructive permissions (b) have explicitly enabled 7182 * destructive actions and (c) destructive actions have 7183 * not been disabled, we're going to transition into 7184 * the KILLED state, from which no further processing 7185 * on this state will be performed. 7186 */ 7187 if (!dtrace_priv_kernel_destructive(state) || 7188 !state->dts_cred.dcr_destructive || 7189 dtrace_destructive_disallow) { 7190 void *activity = &state->dts_activity; 7191 dtrace_activity_t current; 7192 7193 do { 7194 current = state->dts_activity; 7195 } while (dtrace_cas32(activity, current, 7196 DTRACE_ACTIVITY_KILLED) != current); 7197 7198 continue; 7199 } 7200 } 7201 7202 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 7203 ecb->dte_alignment, state, &mstate)) < 0) 7204 continue; 7205 7206 tomax = buf->dtb_tomax; 7207 ASSERT(tomax != NULL); 7208 7209 if (ecb->dte_size != 0) { 7210 dtrace_rechdr_t dtrh; 7211 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 7212 mstate.dtms_timestamp = dtrace_gethrtime(); 7213 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7214 } 7215 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 7216 dtrh.dtrh_epid = ecb->dte_epid; 7217 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 7218 mstate.dtms_timestamp); 7219 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 7220 } 7221 7222 mstate.dtms_epid = ecb->dte_epid; 7223 mstate.dtms_present |= DTRACE_MSTATE_EPID; 7224 7225 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 7226 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 7227 else 7228 mstate.dtms_access = 0; 7229 7230 if (pred != NULL) { 7231 dtrace_difo_t *dp = pred->dtp_difo; 7232 int rval; 7233 7234 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 7235 7236 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 7237 dtrace_cacheid_t cid = probe->dtpr_predcache; 7238 7239 if (cid != DTRACE_CACHEIDNONE && !onintr) { 7240 /* 7241 * Update the predicate cache... 7242 */ 7243 ASSERT(cid == pred->dtp_cacheid); 7244 curthread->t_predcache = cid; 7245 } 7246 7247 continue; 7248 } 7249 } 7250 7251 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 7252 act != NULL; act = act->dta_next) { 7253 size_t valoffs; 7254 dtrace_difo_t *dp; 7255 dtrace_recdesc_t *rec = &act->dta_rec; 7256 7257 size = rec->dtrd_size; 7258 valoffs = offs + rec->dtrd_offset; 7259 7260 if (DTRACEACT_ISAGG(act->dta_kind)) { 7261 uint64_t v = 0xbad; 7262 dtrace_aggregation_t *agg; 7263 7264 agg = (dtrace_aggregation_t *)act; 7265 7266 if ((dp = act->dta_difo) != NULL) 7267 v = dtrace_dif_emulate(dp, 7268 &mstate, vstate, state); 7269 7270 if (*flags & CPU_DTRACE_ERROR) 7271 continue; 7272 7273 /* 7274 * Note that we always pass the expression 7275 * value from the previous iteration of the 7276 * action loop. This value will only be used 7277 * if there is an expression argument to the 7278 * aggregating action, denoted by the 7279 * dtag_hasarg field. 7280 */ 7281 dtrace_aggregate(agg, buf, 7282 offs, aggbuf, v, val); 7283 continue; 7284 } 7285 7286 switch (act->dta_kind) { 7287 case DTRACEACT_STOP: 7288 if (dtrace_priv_proc_destructive(state)) 7289 dtrace_action_stop(); 7290 continue; 7291 7292 case DTRACEACT_BREAKPOINT: 7293 if (dtrace_priv_kernel_destructive(state)) 7294 dtrace_action_breakpoint(ecb); 7295 continue; 7296 7297 case DTRACEACT_PANIC: 7298 if (dtrace_priv_kernel_destructive(state)) 7299 dtrace_action_panic(ecb); 7300 continue; 7301 7302 case DTRACEACT_STACK: 7303 if (!dtrace_priv_kernel(state)) 7304 continue; 7305 7306 dtrace_getpcstack((pc_t *)(tomax + valoffs), 7307 size / sizeof (pc_t), probe->dtpr_aframes, 7308 DTRACE_ANCHORED(probe) ? NULL : 7309 (uint32_t *)arg0); 7310 continue; 7311 7312 case DTRACEACT_JSTACK: 7313 case DTRACEACT_USTACK: 7314 if (!dtrace_priv_proc(state)) 7315 continue; 7316 7317 /* 7318 * See comment in DIF_VAR_PID. 7319 */ 7320 if (DTRACE_ANCHORED(mstate.dtms_probe) && 7321 CPU_ON_INTR(CPU)) { 7322 int depth = DTRACE_USTACK_NFRAMES( 7323 rec->dtrd_arg) + 1; 7324 7325 dtrace_bzero((void *)(tomax + valoffs), 7326 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 7327 + depth * sizeof (uint64_t)); 7328 7329 continue; 7330 } 7331 7332 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 7333 curproc->p_dtrace_helpers != NULL) { 7334 /* 7335 * This is the slow path -- we have 7336 * allocated string space, and we're 7337 * getting the stack of a process that 7338 * has helpers. Call into a separate 7339 * routine to perform this processing. 7340 */ 7341 dtrace_action_ustack(&mstate, state, 7342 (uint64_t *)(tomax + valoffs), 7343 rec->dtrd_arg); 7344 continue; 7345 } 7346 7347 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7348 dtrace_getupcstack((uint64_t *) 7349 (tomax + valoffs), 7350 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 7351 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7352 continue; 7353 7354 default: 7355 break; 7356 } 7357 7358 dp = act->dta_difo; 7359 ASSERT(dp != NULL); 7360 7361 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 7362 7363 if (*flags & CPU_DTRACE_ERROR) 7364 continue; 7365 7366 switch (act->dta_kind) { 7367 case DTRACEACT_SPECULATE: { 7368 dtrace_rechdr_t *dtrh; 7369 7370 ASSERT(buf == &state->dts_buffer[cpuid]); 7371 buf = dtrace_speculation_buffer(state, 7372 cpuid, val); 7373 7374 if (buf == NULL) { 7375 *flags |= CPU_DTRACE_DROP; 7376 continue; 7377 } 7378 7379 offs = dtrace_buffer_reserve(buf, 7380 ecb->dte_needed, ecb->dte_alignment, 7381 state, NULL); 7382 7383 if (offs < 0) { 7384 *flags |= CPU_DTRACE_DROP; 7385 continue; 7386 } 7387 7388 tomax = buf->dtb_tomax; 7389 ASSERT(tomax != NULL); 7390 7391 if (ecb->dte_size == 0) 7392 continue; 7393 7394 ASSERT3U(ecb->dte_size, >=, 7395 sizeof (dtrace_rechdr_t)); 7396 dtrh = ((void *)(tomax + offs)); 7397 dtrh->dtrh_epid = ecb->dte_epid; 7398 /* 7399 * When the speculation is committed, all of 7400 * the records in the speculative buffer will 7401 * have their timestamps set to the commit 7402 * time. Until then, it is set to a sentinel 7403 * value, for debugability. 7404 */ 7405 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 7406 continue; 7407 } 7408 7409 case DTRACEACT_PRINTM: { 7410 /* The DIF returns a 'memref'. */ 7411 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 7412 7413 /* Get the size from the memref. */ 7414 size = memref[1]; 7415 7416 /* 7417 * Check if the size exceeds the allocated 7418 * buffer size. 7419 */ 7420 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7421 /* Flag a drop! */ 7422 *flags |= CPU_DTRACE_DROP; 7423 continue; 7424 } 7425 7426 /* Store the size in the buffer first. */ 7427 DTRACE_STORE(uintptr_t, tomax, 7428 valoffs, size); 7429 7430 /* 7431 * Offset the buffer address to the start 7432 * of the data. 7433 */ 7434 valoffs += sizeof(uintptr_t); 7435 7436 /* 7437 * Reset to the memory address rather than 7438 * the memref array, then let the BYREF 7439 * code below do the work to store the 7440 * memory data in the buffer. 7441 */ 7442 val = memref[0]; 7443 break; 7444 } 7445 7446 case DTRACEACT_PRINTT: { 7447 /* The DIF returns a 'typeref'. */ 7448 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 7449 char c = '\0' + 1; 7450 size_t s; 7451 7452 /* 7453 * Get the type string length and round it 7454 * up so that the data that follows is 7455 * aligned for easy access. 7456 */ 7457 size_t typs = strlen((char *) typeref[2]) + 1; 7458 typs = roundup(typs, sizeof(uintptr_t)); 7459 7460 /* 7461 *Get the size from the typeref using the 7462 * number of elements and the type size. 7463 */ 7464 size = typeref[1] * typeref[3]; 7465 7466 /* 7467 * Check if the size exceeds the allocated 7468 * buffer size. 7469 */ 7470 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7471 /* Flag a drop! */ 7472 *flags |= CPU_DTRACE_DROP; 7473 7474 } 7475 7476 /* Store the size in the buffer first. */ 7477 DTRACE_STORE(uintptr_t, tomax, 7478 valoffs, size); 7479 valoffs += sizeof(uintptr_t); 7480 7481 /* Store the type size in the buffer. */ 7482 DTRACE_STORE(uintptr_t, tomax, 7483 valoffs, typeref[3]); 7484 valoffs += sizeof(uintptr_t); 7485 7486 val = typeref[2]; 7487 7488 for (s = 0; s < typs; s++) { 7489 if (c != '\0') 7490 c = dtrace_load8(val++); 7491 7492 DTRACE_STORE(uint8_t, tomax, 7493 valoffs++, c); 7494 } 7495 7496 /* 7497 * Reset to the memory address rather than 7498 * the typeref array, then let the BYREF 7499 * code below do the work to store the 7500 * memory data in the buffer. 7501 */ 7502 val = typeref[0]; 7503 break; 7504 } 7505 7506 case DTRACEACT_CHILL: 7507 if (dtrace_priv_kernel_destructive(state)) 7508 dtrace_action_chill(&mstate, val); 7509 continue; 7510 7511 case DTRACEACT_RAISE: 7512 if (dtrace_priv_proc_destructive(state)) 7513 dtrace_action_raise(val); 7514 continue; 7515 7516 case DTRACEACT_COMMIT: 7517 ASSERT(!committed); 7518 7519 /* 7520 * We need to commit our buffer state. 7521 */ 7522 if (ecb->dte_size) 7523 buf->dtb_offset = offs + ecb->dte_size; 7524 buf = &state->dts_buffer[cpuid]; 7525 dtrace_speculation_commit(state, cpuid, val); 7526 committed = 1; 7527 continue; 7528 7529 case DTRACEACT_DISCARD: 7530 dtrace_speculation_discard(state, cpuid, val); 7531 continue; 7532 7533 case DTRACEACT_DIFEXPR: 7534 case DTRACEACT_LIBACT: 7535 case DTRACEACT_PRINTF: 7536 case DTRACEACT_PRINTA: 7537 case DTRACEACT_SYSTEM: 7538 case DTRACEACT_FREOPEN: 7539 case DTRACEACT_TRACEMEM: 7540 break; 7541 7542 case DTRACEACT_TRACEMEM_DYNSIZE: 7543 tracememsize = val; 7544 break; 7545 7546 case DTRACEACT_SYM: 7547 case DTRACEACT_MOD: 7548 if (!dtrace_priv_kernel(state)) 7549 continue; 7550 break; 7551 7552 case DTRACEACT_USYM: 7553 case DTRACEACT_UMOD: 7554 case DTRACEACT_UADDR: { 7555#if defined(sun) 7556 struct pid *pid = curthread->t_procp->p_pidp; 7557#endif 7558 7559 if (!dtrace_priv_proc(state)) 7560 continue; 7561 7562 DTRACE_STORE(uint64_t, tomax, 7563#if defined(sun) 7564 valoffs, (uint64_t)pid->pid_id); 7565#else 7566 valoffs, (uint64_t) curproc->p_pid); 7567#endif 7568 DTRACE_STORE(uint64_t, tomax, 7569 valoffs + sizeof (uint64_t), val); 7570 7571 continue; 7572 } 7573 7574 case DTRACEACT_EXIT: { 7575 /* 7576 * For the exit action, we are going to attempt 7577 * to atomically set our activity to be 7578 * draining. If this fails (either because 7579 * another CPU has beat us to the exit action, 7580 * or because our current activity is something 7581 * other than ACTIVE or WARMUP), we will 7582 * continue. This assures that the exit action 7583 * can be successfully recorded at most once 7584 * when we're in the ACTIVE state. If we're 7585 * encountering the exit() action while in 7586 * COOLDOWN, however, we want to honor the new 7587 * status code. (We know that we're the only 7588 * thread in COOLDOWN, so there is no race.) 7589 */ 7590 void *activity = &state->dts_activity; 7591 dtrace_activity_t current = state->dts_activity; 7592 7593 if (current == DTRACE_ACTIVITY_COOLDOWN) 7594 break; 7595 7596 if (current != DTRACE_ACTIVITY_WARMUP) 7597 current = DTRACE_ACTIVITY_ACTIVE; 7598 7599 if (dtrace_cas32(activity, current, 7600 DTRACE_ACTIVITY_DRAINING) != current) { 7601 *flags |= CPU_DTRACE_DROP; 7602 continue; 7603 } 7604 7605 break; 7606 } 7607 7608 default: 7609 ASSERT(0); 7610 } 7611 7612 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF || 7613 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) { 7614 uintptr_t end = valoffs + size; 7615 7616 if (tracememsize != 0 && 7617 valoffs + tracememsize < end) { 7618 end = valoffs + tracememsize; 7619 tracememsize = 0; 7620 } 7621 7622 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF && 7623 !dtrace_vcanload((void *)(uintptr_t)val, 7624 &dp->dtdo_rtype, &mstate, vstate)) 7625 continue; 7626 7627 dtrace_store_by_ref(dp, tomax, size, &valoffs, 7628 &val, end, act->dta_intuple, 7629 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ? 7630 DIF_TF_BYREF: DIF_TF_BYUREF); 7631 continue; 7632 } 7633 7634 switch (size) { 7635 case 0: 7636 break; 7637 7638 case sizeof (uint8_t): 7639 DTRACE_STORE(uint8_t, tomax, valoffs, val); 7640 break; 7641 case sizeof (uint16_t): 7642 DTRACE_STORE(uint16_t, tomax, valoffs, val); 7643 break; 7644 case sizeof (uint32_t): 7645 DTRACE_STORE(uint32_t, tomax, valoffs, val); 7646 break; 7647 case sizeof (uint64_t): 7648 DTRACE_STORE(uint64_t, tomax, valoffs, val); 7649 break; 7650 default: 7651 /* 7652 * Any other size should have been returned by 7653 * reference, not by value. 7654 */ 7655 ASSERT(0); 7656 break; 7657 } 7658 } 7659 7660 if (*flags & CPU_DTRACE_DROP) 7661 continue; 7662 7663 if (*flags & CPU_DTRACE_FAULT) { 7664 int ndx; 7665 dtrace_action_t *err; 7666 7667 buf->dtb_errors++; 7668 7669 if (probe->dtpr_id == dtrace_probeid_error) { 7670 /* 7671 * There's nothing we can do -- we had an 7672 * error on the error probe. We bump an 7673 * error counter to at least indicate that 7674 * this condition happened. 7675 */ 7676 dtrace_error(&state->dts_dblerrors); 7677 continue; 7678 } 7679 7680 if (vtime) { 7681 /* 7682 * Before recursing on dtrace_probe(), we 7683 * need to explicitly clear out our start 7684 * time to prevent it from being accumulated 7685 * into t_dtrace_vtime. 7686 */ 7687 curthread->t_dtrace_start = 0; 7688 } 7689 7690 /* 7691 * Iterate over the actions to figure out which action 7692 * we were processing when we experienced the error. 7693 * Note that act points _past_ the faulting action; if 7694 * act is ecb->dte_action, the fault was in the 7695 * predicate, if it's ecb->dte_action->dta_next it's 7696 * in action #1, and so on. 7697 */ 7698 for (err = ecb->dte_action, ndx = 0; 7699 err != act; err = err->dta_next, ndx++) 7700 continue; 7701 7702 dtrace_probe_error(state, ecb->dte_epid, ndx, 7703 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 7704 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 7705 cpu_core[cpuid].cpuc_dtrace_illval); 7706 7707 continue; 7708 } 7709 7710 if (!committed) 7711 buf->dtb_offset = offs + ecb->dte_size; 7712 } 7713 7714 if (vtime) 7715 curthread->t_dtrace_start = dtrace_gethrtime(); 7716 7717 dtrace_interrupt_enable(cookie); 7718} 7719 7720/* 7721 * DTrace Probe Hashing Functions 7722 * 7723 * The functions in this section (and indeed, the functions in remaining 7724 * sections) are not _called_ from probe context. (Any exceptions to this are 7725 * marked with a "Note:".) Rather, they are called from elsewhere in the 7726 * DTrace framework to look-up probes in, add probes to and remove probes from 7727 * the DTrace probe hashes. (Each probe is hashed by each element of the 7728 * probe tuple -- allowing for fast lookups, regardless of what was 7729 * specified.) 7730 */ 7731static uint_t 7732dtrace_hash_str(const char *p) 7733{ 7734 unsigned int g; 7735 uint_t hval = 0; 7736 7737 while (*p) { 7738 hval = (hval << 4) + *p++; 7739 if ((g = (hval & 0xf0000000)) != 0) 7740 hval ^= g >> 24; 7741 hval &= ~g; 7742 } 7743 return (hval); 7744} 7745 7746static dtrace_hash_t * 7747dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 7748{ 7749 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 7750 7751 hash->dth_stroffs = stroffs; 7752 hash->dth_nextoffs = nextoffs; 7753 hash->dth_prevoffs = prevoffs; 7754 7755 hash->dth_size = 1; 7756 hash->dth_mask = hash->dth_size - 1; 7757 7758 hash->dth_tab = kmem_zalloc(hash->dth_size * 7759 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 7760 7761 return (hash); 7762} 7763 7764static void 7765dtrace_hash_destroy(dtrace_hash_t *hash) 7766{ 7767#ifdef DEBUG 7768 int i; 7769 7770 for (i = 0; i < hash->dth_size; i++) 7771 ASSERT(hash->dth_tab[i] == NULL); 7772#endif 7773 7774 kmem_free(hash->dth_tab, 7775 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 7776 kmem_free(hash, sizeof (dtrace_hash_t)); 7777} 7778 7779static void 7780dtrace_hash_resize(dtrace_hash_t *hash) 7781{ 7782 int size = hash->dth_size, i, ndx; 7783 int new_size = hash->dth_size << 1; 7784 int new_mask = new_size - 1; 7785 dtrace_hashbucket_t **new_tab, *bucket, *next; 7786 7787 ASSERT((new_size & new_mask) == 0); 7788 7789 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 7790 7791 for (i = 0; i < size; i++) { 7792 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 7793 dtrace_probe_t *probe = bucket->dthb_chain; 7794 7795 ASSERT(probe != NULL); 7796 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 7797 7798 next = bucket->dthb_next; 7799 bucket->dthb_next = new_tab[ndx]; 7800 new_tab[ndx] = bucket; 7801 } 7802 } 7803 7804 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 7805 hash->dth_tab = new_tab; 7806 hash->dth_size = new_size; 7807 hash->dth_mask = new_mask; 7808} 7809 7810static void 7811dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 7812{ 7813 int hashval = DTRACE_HASHSTR(hash, new); 7814 int ndx = hashval & hash->dth_mask; 7815 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7816 dtrace_probe_t **nextp, **prevp; 7817 7818 for (; bucket != NULL; bucket = bucket->dthb_next) { 7819 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 7820 goto add; 7821 } 7822 7823 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 7824 dtrace_hash_resize(hash); 7825 dtrace_hash_add(hash, new); 7826 return; 7827 } 7828 7829 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 7830 bucket->dthb_next = hash->dth_tab[ndx]; 7831 hash->dth_tab[ndx] = bucket; 7832 hash->dth_nbuckets++; 7833 7834add: 7835 nextp = DTRACE_HASHNEXT(hash, new); 7836 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 7837 *nextp = bucket->dthb_chain; 7838 7839 if (bucket->dthb_chain != NULL) { 7840 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 7841 ASSERT(*prevp == NULL); 7842 *prevp = new; 7843 } 7844 7845 bucket->dthb_chain = new; 7846 bucket->dthb_len++; 7847} 7848 7849static dtrace_probe_t * 7850dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 7851{ 7852 int hashval = DTRACE_HASHSTR(hash, template); 7853 int ndx = hashval & hash->dth_mask; 7854 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7855 7856 for (; bucket != NULL; bucket = bucket->dthb_next) { 7857 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 7858 return (bucket->dthb_chain); 7859 } 7860 7861 return (NULL); 7862} 7863 7864static int 7865dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 7866{ 7867 int hashval = DTRACE_HASHSTR(hash, template); 7868 int ndx = hashval & hash->dth_mask; 7869 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7870 7871 for (; bucket != NULL; bucket = bucket->dthb_next) { 7872 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 7873 return (bucket->dthb_len); 7874 } 7875 7876 return (0); 7877} 7878 7879static void 7880dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 7881{ 7882 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 7883 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7884 7885 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 7886 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 7887 7888 /* 7889 * Find the bucket that we're removing this probe from. 7890 */ 7891 for (; bucket != NULL; bucket = bucket->dthb_next) { 7892 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 7893 break; 7894 } 7895 7896 ASSERT(bucket != NULL); 7897 7898 if (*prevp == NULL) { 7899 if (*nextp == NULL) { 7900 /* 7901 * The removed probe was the only probe on this 7902 * bucket; we need to remove the bucket. 7903 */ 7904 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 7905 7906 ASSERT(bucket->dthb_chain == probe); 7907 ASSERT(b != NULL); 7908 7909 if (b == bucket) { 7910 hash->dth_tab[ndx] = bucket->dthb_next; 7911 } else { 7912 while (b->dthb_next != bucket) 7913 b = b->dthb_next; 7914 b->dthb_next = bucket->dthb_next; 7915 } 7916 7917 ASSERT(hash->dth_nbuckets > 0); 7918 hash->dth_nbuckets--; 7919 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 7920 return; 7921 } 7922 7923 bucket->dthb_chain = *nextp; 7924 } else { 7925 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 7926 } 7927 7928 if (*nextp != NULL) 7929 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 7930} 7931 7932/* 7933 * DTrace Utility Functions 7934 * 7935 * These are random utility functions that are _not_ called from probe context. 7936 */ 7937static int 7938dtrace_badattr(const dtrace_attribute_t *a) 7939{ 7940 return (a->dtat_name > DTRACE_STABILITY_MAX || 7941 a->dtat_data > DTRACE_STABILITY_MAX || 7942 a->dtat_class > DTRACE_CLASS_MAX); 7943} 7944 7945/* 7946 * Return a duplicate copy of a string. If the specified string is NULL, 7947 * this function returns a zero-length string. 7948 */ 7949static char * 7950dtrace_strdup(const char *str) 7951{ 7952 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 7953 7954 if (str != NULL) 7955 (void) strcpy(new, str); 7956 7957 return (new); 7958} 7959 7960#define DTRACE_ISALPHA(c) \ 7961 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 7962 7963static int 7964dtrace_badname(const char *s) 7965{ 7966 char c; 7967 7968 if (s == NULL || (c = *s++) == '\0') 7969 return (0); 7970 7971 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 7972 return (1); 7973 7974 while ((c = *s++) != '\0') { 7975 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 7976 c != '-' && c != '_' && c != '.' && c != '`') 7977 return (1); 7978 } 7979 7980 return (0); 7981} 7982 7983static void 7984dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 7985{ 7986 uint32_t priv; 7987 7988#if defined(sun) 7989 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 7990 /* 7991 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 7992 */ 7993 priv = DTRACE_PRIV_ALL; 7994 } else { 7995 *uidp = crgetuid(cr); 7996 *zoneidp = crgetzoneid(cr); 7997 7998 priv = 0; 7999 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 8000 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 8001 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 8002 priv |= DTRACE_PRIV_USER; 8003 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 8004 priv |= DTRACE_PRIV_PROC; 8005 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 8006 priv |= DTRACE_PRIV_OWNER; 8007 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 8008 priv |= DTRACE_PRIV_ZONEOWNER; 8009 } 8010#else 8011 priv = DTRACE_PRIV_ALL; 8012#endif 8013 8014 *privp = priv; 8015} 8016 8017#ifdef DTRACE_ERRDEBUG 8018static void 8019dtrace_errdebug(const char *str) 8020{ 8021 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 8022 int occupied = 0; 8023 8024 mutex_enter(&dtrace_errlock); 8025 dtrace_errlast = str; 8026 dtrace_errthread = curthread; 8027 8028 while (occupied++ < DTRACE_ERRHASHSZ) { 8029 if (dtrace_errhash[hval].dter_msg == str) { 8030 dtrace_errhash[hval].dter_count++; 8031 goto out; 8032 } 8033 8034 if (dtrace_errhash[hval].dter_msg != NULL) { 8035 hval = (hval + 1) % DTRACE_ERRHASHSZ; 8036 continue; 8037 } 8038 8039 dtrace_errhash[hval].dter_msg = str; 8040 dtrace_errhash[hval].dter_count = 1; 8041 goto out; 8042 } 8043 8044 panic("dtrace: undersized error hash"); 8045out: 8046 mutex_exit(&dtrace_errlock); 8047} 8048#endif 8049 8050/* 8051 * DTrace Matching Functions 8052 * 8053 * These functions are used to match groups of probes, given some elements of 8054 * a probe tuple, or some globbed expressions for elements of a probe tuple. 8055 */ 8056static int 8057dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 8058 zoneid_t zoneid) 8059{ 8060 if (priv != DTRACE_PRIV_ALL) { 8061 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 8062 uint32_t match = priv & ppriv; 8063 8064 /* 8065 * No PRIV_DTRACE_* privileges... 8066 */ 8067 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 8068 DTRACE_PRIV_KERNEL)) == 0) 8069 return (0); 8070 8071 /* 8072 * No matching bits, but there were bits to match... 8073 */ 8074 if (match == 0 && ppriv != 0) 8075 return (0); 8076 8077 /* 8078 * Need to have permissions to the process, but don't... 8079 */ 8080 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 8081 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 8082 return (0); 8083 } 8084 8085 /* 8086 * Need to be in the same zone unless we possess the 8087 * privilege to examine all zones. 8088 */ 8089 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 8090 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 8091 return (0); 8092 } 8093 } 8094 8095 return (1); 8096} 8097 8098/* 8099 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 8100 * consists of input pattern strings and an ops-vector to evaluate them. 8101 * This function returns >0 for match, 0 for no match, and <0 for error. 8102 */ 8103static int 8104dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 8105 uint32_t priv, uid_t uid, zoneid_t zoneid) 8106{ 8107 dtrace_provider_t *pvp = prp->dtpr_provider; 8108 int rv; 8109 8110 if (pvp->dtpv_defunct) 8111 return (0); 8112 8113 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 8114 return (rv); 8115 8116 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 8117 return (rv); 8118 8119 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 8120 return (rv); 8121 8122 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 8123 return (rv); 8124 8125 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 8126 return (0); 8127 8128 return (rv); 8129} 8130 8131/* 8132 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 8133 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 8134 * libc's version, the kernel version only applies to 8-bit ASCII strings. 8135 * In addition, all of the recursion cases except for '*' matching have been 8136 * unwound. For '*', we still implement recursive evaluation, but a depth 8137 * counter is maintained and matching is aborted if we recurse too deep. 8138 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 8139 */ 8140static int 8141dtrace_match_glob(const char *s, const char *p, int depth) 8142{ 8143 const char *olds; 8144 char s1, c; 8145 int gs; 8146 8147 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 8148 return (-1); 8149 8150 if (s == NULL) 8151 s = ""; /* treat NULL as empty string */ 8152 8153top: 8154 olds = s; 8155 s1 = *s++; 8156 8157 if (p == NULL) 8158 return (0); 8159 8160 if ((c = *p++) == '\0') 8161 return (s1 == '\0'); 8162 8163 switch (c) { 8164 case '[': { 8165 int ok = 0, notflag = 0; 8166 char lc = '\0'; 8167 8168 if (s1 == '\0') 8169 return (0); 8170 8171 if (*p == '!') { 8172 notflag = 1; 8173 p++; 8174 } 8175 8176 if ((c = *p++) == '\0') 8177 return (0); 8178 8179 do { 8180 if (c == '-' && lc != '\0' && *p != ']') { 8181 if ((c = *p++) == '\0') 8182 return (0); 8183 if (c == '\\' && (c = *p++) == '\0') 8184 return (0); 8185 8186 if (notflag) { 8187 if (s1 < lc || s1 > c) 8188 ok++; 8189 else 8190 return (0); 8191 } else if (lc <= s1 && s1 <= c) 8192 ok++; 8193 8194 } else if (c == '\\' && (c = *p++) == '\0') 8195 return (0); 8196 8197 lc = c; /* save left-hand 'c' for next iteration */ 8198 8199 if (notflag) { 8200 if (s1 != c) 8201 ok++; 8202 else 8203 return (0); 8204 } else if (s1 == c) 8205 ok++; 8206 8207 if ((c = *p++) == '\0') 8208 return (0); 8209 8210 } while (c != ']'); 8211 8212 if (ok) 8213 goto top; 8214 8215 return (0); 8216 } 8217 8218 case '\\': 8219 if ((c = *p++) == '\0') 8220 return (0); 8221 /*FALLTHRU*/ 8222 8223 default: 8224 if (c != s1) 8225 return (0); 8226 /*FALLTHRU*/ 8227 8228 case '?': 8229 if (s1 != '\0') 8230 goto top; 8231 return (0); 8232 8233 case '*': 8234 while (*p == '*') 8235 p++; /* consecutive *'s are identical to a single one */ 8236 8237 if (*p == '\0') 8238 return (1); 8239 8240 for (s = olds; *s != '\0'; s++) { 8241 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 8242 return (gs); 8243 } 8244 8245 return (0); 8246 } 8247} 8248 8249/*ARGSUSED*/ 8250static int 8251dtrace_match_string(const char *s, const char *p, int depth) 8252{ 8253 return (s != NULL && strcmp(s, p) == 0); 8254} 8255 8256/*ARGSUSED*/ 8257static int 8258dtrace_match_nul(const char *s, const char *p, int depth) 8259{ 8260 return (1); /* always match the empty pattern */ 8261} 8262 8263/*ARGSUSED*/ 8264static int 8265dtrace_match_nonzero(const char *s, const char *p, int depth) 8266{ 8267 return (s != NULL && s[0] != '\0'); 8268} 8269 8270static int 8271dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 8272 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 8273{ 8274 dtrace_probe_t template, *probe; 8275 dtrace_hash_t *hash = NULL; 8276 int len, best = INT_MAX, nmatched = 0; 8277 dtrace_id_t i; 8278 8279 ASSERT(MUTEX_HELD(&dtrace_lock)); 8280 8281 /* 8282 * If the probe ID is specified in the key, just lookup by ID and 8283 * invoke the match callback once if a matching probe is found. 8284 */ 8285 if (pkp->dtpk_id != DTRACE_IDNONE) { 8286 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 8287 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 8288 (void) (*matched)(probe, arg); 8289 nmatched++; 8290 } 8291 return (nmatched); 8292 } 8293 8294 template.dtpr_mod = (char *)pkp->dtpk_mod; 8295 template.dtpr_func = (char *)pkp->dtpk_func; 8296 template.dtpr_name = (char *)pkp->dtpk_name; 8297 8298 /* 8299 * We want to find the most distinct of the module name, function 8300 * name, and name. So for each one that is not a glob pattern or 8301 * empty string, we perform a lookup in the corresponding hash and 8302 * use the hash table with the fewest collisions to do our search. 8303 */ 8304 if (pkp->dtpk_mmatch == &dtrace_match_string && 8305 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 8306 best = len; 8307 hash = dtrace_bymod; 8308 } 8309 8310 if (pkp->dtpk_fmatch == &dtrace_match_string && 8311 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 8312 best = len; 8313 hash = dtrace_byfunc; 8314 } 8315 8316 if (pkp->dtpk_nmatch == &dtrace_match_string && 8317 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 8318 best = len; 8319 hash = dtrace_byname; 8320 } 8321 8322 /* 8323 * If we did not select a hash table, iterate over every probe and 8324 * invoke our callback for each one that matches our input probe key. 8325 */ 8326 if (hash == NULL) { 8327 for (i = 0; i < dtrace_nprobes; i++) { 8328 if ((probe = dtrace_probes[i]) == NULL || 8329 dtrace_match_probe(probe, pkp, priv, uid, 8330 zoneid) <= 0) 8331 continue; 8332 8333 nmatched++; 8334 8335 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8336 break; 8337 } 8338 8339 return (nmatched); 8340 } 8341 8342 /* 8343 * If we selected a hash table, iterate over each probe of the same key 8344 * name and invoke the callback for every probe that matches the other 8345 * attributes of our input probe key. 8346 */ 8347 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 8348 probe = *(DTRACE_HASHNEXT(hash, probe))) { 8349 8350 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 8351 continue; 8352 8353 nmatched++; 8354 8355 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8356 break; 8357 } 8358 8359 return (nmatched); 8360} 8361 8362/* 8363 * Return the function pointer dtrace_probecmp() should use to compare the 8364 * specified pattern with a string. For NULL or empty patterns, we select 8365 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 8366 * For non-empty non-glob strings, we use dtrace_match_string(). 8367 */ 8368static dtrace_probekey_f * 8369dtrace_probekey_func(const char *p) 8370{ 8371 char c; 8372 8373 if (p == NULL || *p == '\0') 8374 return (&dtrace_match_nul); 8375 8376 while ((c = *p++) != '\0') { 8377 if (c == '[' || c == '?' || c == '*' || c == '\\') 8378 return (&dtrace_match_glob); 8379 } 8380 8381 return (&dtrace_match_string); 8382} 8383 8384/* 8385 * Build a probe comparison key for use with dtrace_match_probe() from the 8386 * given probe description. By convention, a null key only matches anchored 8387 * probes: if each field is the empty string, reset dtpk_fmatch to 8388 * dtrace_match_nonzero(). 8389 */ 8390static void 8391dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 8392{ 8393 pkp->dtpk_prov = pdp->dtpd_provider; 8394 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 8395 8396 pkp->dtpk_mod = pdp->dtpd_mod; 8397 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 8398 8399 pkp->dtpk_func = pdp->dtpd_func; 8400 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 8401 8402 pkp->dtpk_name = pdp->dtpd_name; 8403 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 8404 8405 pkp->dtpk_id = pdp->dtpd_id; 8406 8407 if (pkp->dtpk_id == DTRACE_IDNONE && 8408 pkp->dtpk_pmatch == &dtrace_match_nul && 8409 pkp->dtpk_mmatch == &dtrace_match_nul && 8410 pkp->dtpk_fmatch == &dtrace_match_nul && 8411 pkp->dtpk_nmatch == &dtrace_match_nul) 8412 pkp->dtpk_fmatch = &dtrace_match_nonzero; 8413} 8414 8415/* 8416 * DTrace Provider-to-Framework API Functions 8417 * 8418 * These functions implement much of the Provider-to-Framework API, as 8419 * described in <sys/dtrace.h>. The parts of the API not in this section are 8420 * the functions in the API for probe management (found below), and 8421 * dtrace_probe() itself (found above). 8422 */ 8423 8424/* 8425 * Register the calling provider with the DTrace framework. This should 8426 * generally be called by DTrace providers in their attach(9E) entry point. 8427 */ 8428int 8429dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 8430 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 8431{ 8432 dtrace_provider_t *provider; 8433 8434 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 8435 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8436 "arguments", name ? name : "<NULL>"); 8437 return (EINVAL); 8438 } 8439 8440 if (name[0] == '\0' || dtrace_badname(name)) { 8441 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8442 "provider name", name); 8443 return (EINVAL); 8444 } 8445 8446 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 8447 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 8448 pops->dtps_destroy == NULL || 8449 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 8450 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8451 "provider ops", name); 8452 return (EINVAL); 8453 } 8454 8455 if (dtrace_badattr(&pap->dtpa_provider) || 8456 dtrace_badattr(&pap->dtpa_mod) || 8457 dtrace_badattr(&pap->dtpa_func) || 8458 dtrace_badattr(&pap->dtpa_name) || 8459 dtrace_badattr(&pap->dtpa_args)) { 8460 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8461 "provider attributes", name); 8462 return (EINVAL); 8463 } 8464 8465 if (priv & ~DTRACE_PRIV_ALL) { 8466 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8467 "privilege attributes", name); 8468 return (EINVAL); 8469 } 8470 8471 if ((priv & DTRACE_PRIV_KERNEL) && 8472 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 8473 pops->dtps_usermode == NULL) { 8474 cmn_err(CE_WARN, "failed to register provider '%s': need " 8475 "dtps_usermode() op for given privilege attributes", name); 8476 return (EINVAL); 8477 } 8478 8479 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 8480 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8481 (void) strcpy(provider->dtpv_name, name); 8482 8483 provider->dtpv_attr = *pap; 8484 provider->dtpv_priv.dtpp_flags = priv; 8485 if (cr != NULL) { 8486 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 8487 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 8488 } 8489 provider->dtpv_pops = *pops; 8490 8491 if (pops->dtps_provide == NULL) { 8492 ASSERT(pops->dtps_provide_module != NULL); 8493 provider->dtpv_pops.dtps_provide = 8494 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 8495 } 8496 8497 if (pops->dtps_provide_module == NULL) { 8498 ASSERT(pops->dtps_provide != NULL); 8499 provider->dtpv_pops.dtps_provide_module = 8500 (void (*)(void *, modctl_t *))dtrace_nullop; 8501 } 8502 8503 if (pops->dtps_suspend == NULL) { 8504 ASSERT(pops->dtps_resume == NULL); 8505 provider->dtpv_pops.dtps_suspend = 8506 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8507 provider->dtpv_pops.dtps_resume = 8508 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8509 } 8510 8511 provider->dtpv_arg = arg; 8512 *idp = (dtrace_provider_id_t)provider; 8513 8514 if (pops == &dtrace_provider_ops) { 8515 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8516 ASSERT(MUTEX_HELD(&dtrace_lock)); 8517 ASSERT(dtrace_anon.dta_enabling == NULL); 8518 8519 /* 8520 * We make sure that the DTrace provider is at the head of 8521 * the provider chain. 8522 */ 8523 provider->dtpv_next = dtrace_provider; 8524 dtrace_provider = provider; 8525 return (0); 8526 } 8527 8528 mutex_enter(&dtrace_provider_lock); 8529 mutex_enter(&dtrace_lock); 8530 8531 /* 8532 * If there is at least one provider registered, we'll add this 8533 * provider after the first provider. 8534 */ 8535 if (dtrace_provider != NULL) { 8536 provider->dtpv_next = dtrace_provider->dtpv_next; 8537 dtrace_provider->dtpv_next = provider; 8538 } else { 8539 dtrace_provider = provider; 8540 } 8541 8542 if (dtrace_retained != NULL) { 8543 dtrace_enabling_provide(provider); 8544 8545 /* 8546 * Now we need to call dtrace_enabling_matchall() -- which 8547 * will acquire cpu_lock and dtrace_lock. We therefore need 8548 * to drop all of our locks before calling into it... 8549 */ 8550 mutex_exit(&dtrace_lock); 8551 mutex_exit(&dtrace_provider_lock); 8552 dtrace_enabling_matchall(); 8553 8554 return (0); 8555 } 8556 8557 mutex_exit(&dtrace_lock); 8558 mutex_exit(&dtrace_provider_lock); 8559 8560 return (0); 8561} 8562 8563/* 8564 * Unregister the specified provider from the DTrace framework. This should 8565 * generally be called by DTrace providers in their detach(9E) entry point. 8566 */ 8567int 8568dtrace_unregister(dtrace_provider_id_t id) 8569{ 8570 dtrace_provider_t *old = (dtrace_provider_t *)id; 8571 dtrace_provider_t *prev = NULL; 8572 int i, self = 0, noreap = 0; 8573 dtrace_probe_t *probe, *first = NULL; 8574 8575 if (old->dtpv_pops.dtps_enable == 8576 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 8577 /* 8578 * If DTrace itself is the provider, we're called with locks 8579 * already held. 8580 */ 8581 ASSERT(old == dtrace_provider); 8582#if defined(sun) 8583 ASSERT(dtrace_devi != NULL); 8584#endif 8585 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8586 ASSERT(MUTEX_HELD(&dtrace_lock)); 8587 self = 1; 8588 8589 if (dtrace_provider->dtpv_next != NULL) { 8590 /* 8591 * There's another provider here; return failure. 8592 */ 8593 return (EBUSY); 8594 } 8595 } else { 8596 mutex_enter(&dtrace_provider_lock); 8597#if defined(sun) 8598 mutex_enter(&mod_lock); 8599#endif 8600 mutex_enter(&dtrace_lock); 8601 } 8602 8603 /* 8604 * If anyone has /dev/dtrace open, or if there are anonymous enabled 8605 * probes, we refuse to let providers slither away, unless this 8606 * provider has already been explicitly invalidated. 8607 */ 8608 if (!old->dtpv_defunct && 8609 (dtrace_opens || (dtrace_anon.dta_state != NULL && 8610 dtrace_anon.dta_state->dts_necbs > 0))) { 8611 if (!self) { 8612 mutex_exit(&dtrace_lock); 8613#if defined(sun) 8614 mutex_exit(&mod_lock); 8615#endif 8616 mutex_exit(&dtrace_provider_lock); 8617 } 8618 return (EBUSY); 8619 } 8620 8621 /* 8622 * Attempt to destroy the probes associated with this provider. 8623 */ 8624 for (i = 0; i < dtrace_nprobes; i++) { 8625 if ((probe = dtrace_probes[i]) == NULL) 8626 continue; 8627 8628 if (probe->dtpr_provider != old) 8629 continue; 8630 8631 if (probe->dtpr_ecb == NULL) 8632 continue; 8633 8634 /* 8635 * If we are trying to unregister a defunct provider, and the 8636 * provider was made defunct within the interval dictated by 8637 * dtrace_unregister_defunct_reap, we'll (asynchronously) 8638 * attempt to reap our enablings. To denote that the provider 8639 * should reattempt to unregister itself at some point in the 8640 * future, we will return a differentiable error code (EAGAIN 8641 * instead of EBUSY) in this case. 8642 */ 8643 if (dtrace_gethrtime() - old->dtpv_defunct > 8644 dtrace_unregister_defunct_reap) 8645 noreap = 1; 8646 8647 if (!self) { 8648 mutex_exit(&dtrace_lock); 8649#if defined(sun) 8650 mutex_exit(&mod_lock); 8651#endif 8652 mutex_exit(&dtrace_provider_lock); 8653 } 8654 8655 if (noreap) 8656 return (EBUSY); 8657 8658 (void) taskq_dispatch(dtrace_taskq, 8659 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 8660 8661 return (EAGAIN); 8662 } 8663 8664 /* 8665 * All of the probes for this provider are disabled; we can safely 8666 * remove all of them from their hash chains and from the probe array. 8667 */ 8668 for (i = 0; i < dtrace_nprobes; i++) { 8669 if ((probe = dtrace_probes[i]) == NULL) 8670 continue; 8671 8672 if (probe->dtpr_provider != old) 8673 continue; 8674 8675 dtrace_probes[i] = NULL; 8676 8677 dtrace_hash_remove(dtrace_bymod, probe); 8678 dtrace_hash_remove(dtrace_byfunc, probe); 8679 dtrace_hash_remove(dtrace_byname, probe); 8680 8681 if (first == NULL) { 8682 first = probe; 8683 probe->dtpr_nextmod = NULL; 8684 } else { 8685 probe->dtpr_nextmod = first; 8686 first = probe; 8687 } 8688 } 8689 8690 /* 8691 * The provider's probes have been removed from the hash chains and 8692 * from the probe array. Now issue a dtrace_sync() to be sure that 8693 * everyone has cleared out from any probe array processing. 8694 */ 8695 dtrace_sync(); 8696 8697 for (probe = first; probe != NULL; probe = first) { 8698 first = probe->dtpr_nextmod; 8699 8700 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 8701 probe->dtpr_arg); 8702 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8703 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8704 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8705#if defined(sun) 8706 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 8707#else 8708 free_unr(dtrace_arena, probe->dtpr_id); 8709#endif 8710 kmem_free(probe, sizeof (dtrace_probe_t)); 8711 } 8712 8713 if ((prev = dtrace_provider) == old) { 8714#if defined(sun) 8715 ASSERT(self || dtrace_devi == NULL); 8716 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 8717#endif 8718 dtrace_provider = old->dtpv_next; 8719 } else { 8720 while (prev != NULL && prev->dtpv_next != old) 8721 prev = prev->dtpv_next; 8722 8723 if (prev == NULL) { 8724 panic("attempt to unregister non-existent " 8725 "dtrace provider %p\n", (void *)id); 8726 } 8727 8728 prev->dtpv_next = old->dtpv_next; 8729 } 8730 8731 if (!self) { 8732 mutex_exit(&dtrace_lock); 8733#if defined(sun) 8734 mutex_exit(&mod_lock); 8735#endif 8736 mutex_exit(&dtrace_provider_lock); 8737 } 8738 8739 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 8740 kmem_free(old, sizeof (dtrace_provider_t)); 8741 8742 return (0); 8743} 8744 8745/* 8746 * Invalidate the specified provider. All subsequent probe lookups for the 8747 * specified provider will fail, but its probes will not be removed. 8748 */ 8749void 8750dtrace_invalidate(dtrace_provider_id_t id) 8751{ 8752 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 8753 8754 ASSERT(pvp->dtpv_pops.dtps_enable != 8755 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8756 8757 mutex_enter(&dtrace_provider_lock); 8758 mutex_enter(&dtrace_lock); 8759 8760 pvp->dtpv_defunct = dtrace_gethrtime(); 8761 8762 mutex_exit(&dtrace_lock); 8763 mutex_exit(&dtrace_provider_lock); 8764} 8765 8766/* 8767 * Indicate whether or not DTrace has attached. 8768 */ 8769int 8770dtrace_attached(void) 8771{ 8772 /* 8773 * dtrace_provider will be non-NULL iff the DTrace driver has 8774 * attached. (It's non-NULL because DTrace is always itself a 8775 * provider.) 8776 */ 8777 return (dtrace_provider != NULL); 8778} 8779 8780/* 8781 * Remove all the unenabled probes for the given provider. This function is 8782 * not unlike dtrace_unregister(), except that it doesn't remove the provider 8783 * -- just as many of its associated probes as it can. 8784 */ 8785int 8786dtrace_condense(dtrace_provider_id_t id) 8787{ 8788 dtrace_provider_t *prov = (dtrace_provider_t *)id; 8789 int i; 8790 dtrace_probe_t *probe; 8791 8792 /* 8793 * Make sure this isn't the dtrace provider itself. 8794 */ 8795 ASSERT(prov->dtpv_pops.dtps_enable != 8796 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8797 8798 mutex_enter(&dtrace_provider_lock); 8799 mutex_enter(&dtrace_lock); 8800 8801 /* 8802 * Attempt to destroy the probes associated with this provider. 8803 */ 8804 for (i = 0; i < dtrace_nprobes; i++) { 8805 if ((probe = dtrace_probes[i]) == NULL) 8806 continue; 8807 8808 if (probe->dtpr_provider != prov) 8809 continue; 8810 8811 if (probe->dtpr_ecb != NULL) 8812 continue; 8813 8814 dtrace_probes[i] = NULL; 8815 8816 dtrace_hash_remove(dtrace_bymod, probe); 8817 dtrace_hash_remove(dtrace_byfunc, probe); 8818 dtrace_hash_remove(dtrace_byname, probe); 8819 8820 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 8821 probe->dtpr_arg); 8822 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8823 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8824 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8825 kmem_free(probe, sizeof (dtrace_probe_t)); 8826#if defined(sun) 8827 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 8828#else 8829 free_unr(dtrace_arena, i + 1); 8830#endif 8831 } 8832 8833 mutex_exit(&dtrace_lock); 8834 mutex_exit(&dtrace_provider_lock); 8835 8836 return (0); 8837} 8838 8839/* 8840 * DTrace Probe Management Functions 8841 * 8842 * The functions in this section perform the DTrace probe management, 8843 * including functions to create probes, look-up probes, and call into the 8844 * providers to request that probes be provided. Some of these functions are 8845 * in the Provider-to-Framework API; these functions can be identified by the 8846 * fact that they are not declared "static". 8847 */ 8848 8849/* 8850 * Create a probe with the specified module name, function name, and name. 8851 */ 8852dtrace_id_t 8853dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 8854 const char *func, const char *name, int aframes, void *arg) 8855{ 8856 dtrace_probe_t *probe, **probes; 8857 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 8858 dtrace_id_t id; 8859 8860 if (provider == dtrace_provider) { 8861 ASSERT(MUTEX_HELD(&dtrace_lock)); 8862 } else { 8863 mutex_enter(&dtrace_lock); 8864 } 8865 8866#if defined(sun) 8867 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 8868 VM_BESTFIT | VM_SLEEP); 8869#else 8870 id = alloc_unr(dtrace_arena); 8871#endif 8872 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 8873 8874 probe->dtpr_id = id; 8875 probe->dtpr_gen = dtrace_probegen++; 8876 probe->dtpr_mod = dtrace_strdup(mod); 8877 probe->dtpr_func = dtrace_strdup(func); 8878 probe->dtpr_name = dtrace_strdup(name); 8879 probe->dtpr_arg = arg; 8880 probe->dtpr_aframes = aframes; 8881 probe->dtpr_provider = provider; 8882 8883 dtrace_hash_add(dtrace_bymod, probe); 8884 dtrace_hash_add(dtrace_byfunc, probe); 8885 dtrace_hash_add(dtrace_byname, probe); 8886 8887 if (id - 1 >= dtrace_nprobes) { 8888 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 8889 size_t nsize = osize << 1; 8890 8891 if (nsize == 0) { 8892 ASSERT(osize == 0); 8893 ASSERT(dtrace_probes == NULL); 8894 nsize = sizeof (dtrace_probe_t *); 8895 } 8896 8897 probes = kmem_zalloc(nsize, KM_SLEEP); 8898 8899 if (dtrace_probes == NULL) { 8900 ASSERT(osize == 0); 8901 dtrace_probes = probes; 8902 dtrace_nprobes = 1; 8903 } else { 8904 dtrace_probe_t **oprobes = dtrace_probes; 8905 8906 bcopy(oprobes, probes, osize); 8907 dtrace_membar_producer(); 8908 dtrace_probes = probes; 8909 8910 dtrace_sync(); 8911 8912 /* 8913 * All CPUs are now seeing the new probes array; we can 8914 * safely free the old array. 8915 */ 8916 kmem_free(oprobes, osize); 8917 dtrace_nprobes <<= 1; 8918 } 8919 8920 ASSERT(id - 1 < dtrace_nprobes); 8921 } 8922 8923 ASSERT(dtrace_probes[id - 1] == NULL); 8924 dtrace_probes[id - 1] = probe; 8925 8926 if (provider != dtrace_provider) 8927 mutex_exit(&dtrace_lock); 8928 8929 return (id); 8930} 8931 8932static dtrace_probe_t * 8933dtrace_probe_lookup_id(dtrace_id_t id) 8934{ 8935 ASSERT(MUTEX_HELD(&dtrace_lock)); 8936 8937 if (id == 0 || id > dtrace_nprobes) 8938 return (NULL); 8939 8940 return (dtrace_probes[id - 1]); 8941} 8942 8943static int 8944dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 8945{ 8946 *((dtrace_id_t *)arg) = probe->dtpr_id; 8947 8948 return (DTRACE_MATCH_DONE); 8949} 8950 8951/* 8952 * Look up a probe based on provider and one or more of module name, function 8953 * name and probe name. 8954 */ 8955dtrace_id_t 8956dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 8957 char *func, char *name) 8958{ 8959 dtrace_probekey_t pkey; 8960 dtrace_id_t id; 8961 int match; 8962 8963 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 8964 pkey.dtpk_pmatch = &dtrace_match_string; 8965 pkey.dtpk_mod = mod; 8966 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 8967 pkey.dtpk_func = func; 8968 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 8969 pkey.dtpk_name = name; 8970 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 8971 pkey.dtpk_id = DTRACE_IDNONE; 8972 8973 mutex_enter(&dtrace_lock); 8974 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 8975 dtrace_probe_lookup_match, &id); 8976 mutex_exit(&dtrace_lock); 8977 8978 ASSERT(match == 1 || match == 0); 8979 return (match ? id : 0); 8980} 8981 8982/* 8983 * Returns the probe argument associated with the specified probe. 8984 */ 8985void * 8986dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 8987{ 8988 dtrace_probe_t *probe; 8989 void *rval = NULL; 8990 8991 mutex_enter(&dtrace_lock); 8992 8993 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 8994 probe->dtpr_provider == (dtrace_provider_t *)id) 8995 rval = probe->dtpr_arg; 8996 8997 mutex_exit(&dtrace_lock); 8998 8999 return (rval); 9000} 9001 9002/* 9003 * Copy a probe into a probe description. 9004 */ 9005static void 9006dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 9007{ 9008 bzero(pdp, sizeof (dtrace_probedesc_t)); 9009 pdp->dtpd_id = prp->dtpr_id; 9010 9011 (void) strncpy(pdp->dtpd_provider, 9012 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 9013 9014 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 9015 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 9016 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 9017} 9018 9019/* 9020 * Called to indicate that a probe -- or probes -- should be provided by a 9021 * specfied provider. If the specified description is NULL, the provider will 9022 * be told to provide all of its probes. (This is done whenever a new 9023 * consumer comes along, or whenever a retained enabling is to be matched.) If 9024 * the specified description is non-NULL, the provider is given the 9025 * opportunity to dynamically provide the specified probe, allowing providers 9026 * to support the creation of probes on-the-fly. (So-called _autocreated_ 9027 * probes.) If the provider is NULL, the operations will be applied to all 9028 * providers; if the provider is non-NULL the operations will only be applied 9029 * to the specified provider. The dtrace_provider_lock must be held, and the 9030 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 9031 * will need to grab the dtrace_lock when it reenters the framework through 9032 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 9033 */ 9034static void 9035dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 9036{ 9037#if defined(sun) 9038 modctl_t *ctl; 9039#endif 9040 int all = 0; 9041 9042 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9043 9044 if (prv == NULL) { 9045 all = 1; 9046 prv = dtrace_provider; 9047 } 9048 9049 do { 9050 /* 9051 * First, call the blanket provide operation. 9052 */ 9053 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 9054 9055#if defined(sun) 9056 /* 9057 * Now call the per-module provide operation. We will grab 9058 * mod_lock to prevent the list from being modified. Note 9059 * that this also prevents the mod_busy bits from changing. 9060 * (mod_busy can only be changed with mod_lock held.) 9061 */ 9062 mutex_enter(&mod_lock); 9063 9064 ctl = &modules; 9065 do { 9066 if (ctl->mod_busy || ctl->mod_mp == NULL) 9067 continue; 9068 9069 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 9070 9071 } while ((ctl = ctl->mod_next) != &modules); 9072 9073 mutex_exit(&mod_lock); 9074#endif 9075 } while (all && (prv = prv->dtpv_next) != NULL); 9076} 9077 9078#if defined(sun) 9079/* 9080 * Iterate over each probe, and call the Framework-to-Provider API function 9081 * denoted by offs. 9082 */ 9083static void 9084dtrace_probe_foreach(uintptr_t offs) 9085{ 9086 dtrace_provider_t *prov; 9087 void (*func)(void *, dtrace_id_t, void *); 9088 dtrace_probe_t *probe; 9089 dtrace_icookie_t cookie; 9090 int i; 9091 9092 /* 9093 * We disable interrupts to walk through the probe array. This is 9094 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 9095 * won't see stale data. 9096 */ 9097 cookie = dtrace_interrupt_disable(); 9098 9099 for (i = 0; i < dtrace_nprobes; i++) { 9100 if ((probe = dtrace_probes[i]) == NULL) 9101 continue; 9102 9103 if (probe->dtpr_ecb == NULL) { 9104 /* 9105 * This probe isn't enabled -- don't call the function. 9106 */ 9107 continue; 9108 } 9109 9110 prov = probe->dtpr_provider; 9111 func = *((void(**)(void *, dtrace_id_t, void *)) 9112 ((uintptr_t)&prov->dtpv_pops + offs)); 9113 9114 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 9115 } 9116 9117 dtrace_interrupt_enable(cookie); 9118} 9119#endif 9120 9121static int 9122dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 9123{ 9124 dtrace_probekey_t pkey; 9125 uint32_t priv; 9126 uid_t uid; 9127 zoneid_t zoneid; 9128 9129 ASSERT(MUTEX_HELD(&dtrace_lock)); 9130 dtrace_ecb_create_cache = NULL; 9131 9132 if (desc == NULL) { 9133 /* 9134 * If we're passed a NULL description, we're being asked to 9135 * create an ECB with a NULL probe. 9136 */ 9137 (void) dtrace_ecb_create_enable(NULL, enab); 9138 return (0); 9139 } 9140 9141 dtrace_probekey(desc, &pkey); 9142 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 9143 &priv, &uid, &zoneid); 9144 9145 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 9146 enab)); 9147} 9148 9149/* 9150 * DTrace Helper Provider Functions 9151 */ 9152static void 9153dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 9154{ 9155 attr->dtat_name = DOF_ATTR_NAME(dofattr); 9156 attr->dtat_data = DOF_ATTR_DATA(dofattr); 9157 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 9158} 9159 9160static void 9161dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 9162 const dof_provider_t *dofprov, char *strtab) 9163{ 9164 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 9165 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 9166 dofprov->dofpv_provattr); 9167 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 9168 dofprov->dofpv_modattr); 9169 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 9170 dofprov->dofpv_funcattr); 9171 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 9172 dofprov->dofpv_nameattr); 9173 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 9174 dofprov->dofpv_argsattr); 9175} 9176 9177static void 9178dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9179{ 9180 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9181 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9182 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 9183 dof_provider_t *provider; 9184 dof_probe_t *probe; 9185 uint32_t *off, *enoff; 9186 uint8_t *arg; 9187 char *strtab; 9188 uint_t i, nprobes; 9189 dtrace_helper_provdesc_t dhpv; 9190 dtrace_helper_probedesc_t dhpb; 9191 dtrace_meta_t *meta = dtrace_meta_pid; 9192 dtrace_mops_t *mops = &meta->dtm_mops; 9193 void *parg; 9194 9195 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9196 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9197 provider->dofpv_strtab * dof->dofh_secsize); 9198 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9199 provider->dofpv_probes * dof->dofh_secsize); 9200 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9201 provider->dofpv_prargs * dof->dofh_secsize); 9202 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9203 provider->dofpv_proffs * dof->dofh_secsize); 9204 9205 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9206 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 9207 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 9208 enoff = NULL; 9209 9210 /* 9211 * See dtrace_helper_provider_validate(). 9212 */ 9213 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 9214 provider->dofpv_prenoffs != DOF_SECT_NONE) { 9215 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9216 provider->dofpv_prenoffs * dof->dofh_secsize); 9217 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 9218 } 9219 9220 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 9221 9222 /* 9223 * Create the provider. 9224 */ 9225 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9226 9227 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 9228 return; 9229 9230 meta->dtm_count++; 9231 9232 /* 9233 * Create the probes. 9234 */ 9235 for (i = 0; i < nprobes; i++) { 9236 probe = (dof_probe_t *)(uintptr_t)(daddr + 9237 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 9238 9239 dhpb.dthpb_mod = dhp->dofhp_mod; 9240 dhpb.dthpb_func = strtab + probe->dofpr_func; 9241 dhpb.dthpb_name = strtab + probe->dofpr_name; 9242 dhpb.dthpb_base = probe->dofpr_addr; 9243 dhpb.dthpb_offs = off + probe->dofpr_offidx; 9244 dhpb.dthpb_noffs = probe->dofpr_noffs; 9245 if (enoff != NULL) { 9246 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 9247 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 9248 } else { 9249 dhpb.dthpb_enoffs = NULL; 9250 dhpb.dthpb_nenoffs = 0; 9251 } 9252 dhpb.dthpb_args = arg + probe->dofpr_argidx; 9253 dhpb.dthpb_nargc = probe->dofpr_nargc; 9254 dhpb.dthpb_xargc = probe->dofpr_xargc; 9255 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 9256 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 9257 9258 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 9259 } 9260} 9261 9262static void 9263dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 9264{ 9265 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9266 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9267 int i; 9268 9269 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9270 9271 for (i = 0; i < dof->dofh_secnum; i++) { 9272 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9273 dof->dofh_secoff + i * dof->dofh_secsize); 9274 9275 if (sec->dofs_type != DOF_SECT_PROVIDER) 9276 continue; 9277 9278 dtrace_helper_provide_one(dhp, sec, pid); 9279 } 9280 9281 /* 9282 * We may have just created probes, so we must now rematch against 9283 * any retained enablings. Note that this call will acquire both 9284 * cpu_lock and dtrace_lock; the fact that we are holding 9285 * dtrace_meta_lock now is what defines the ordering with respect to 9286 * these three locks. 9287 */ 9288 dtrace_enabling_matchall(); 9289} 9290 9291static void 9292dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9293{ 9294 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9295 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9296 dof_sec_t *str_sec; 9297 dof_provider_t *provider; 9298 char *strtab; 9299 dtrace_helper_provdesc_t dhpv; 9300 dtrace_meta_t *meta = dtrace_meta_pid; 9301 dtrace_mops_t *mops = &meta->dtm_mops; 9302 9303 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9304 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9305 provider->dofpv_strtab * dof->dofh_secsize); 9306 9307 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9308 9309 /* 9310 * Create the provider. 9311 */ 9312 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9313 9314 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 9315 9316 meta->dtm_count--; 9317} 9318 9319static void 9320dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 9321{ 9322 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9323 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9324 int i; 9325 9326 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9327 9328 for (i = 0; i < dof->dofh_secnum; i++) { 9329 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9330 dof->dofh_secoff + i * dof->dofh_secsize); 9331 9332 if (sec->dofs_type != DOF_SECT_PROVIDER) 9333 continue; 9334 9335 dtrace_helper_provider_remove_one(dhp, sec, pid); 9336 } 9337} 9338 9339/* 9340 * DTrace Meta Provider-to-Framework API Functions 9341 * 9342 * These functions implement the Meta Provider-to-Framework API, as described 9343 * in <sys/dtrace.h>. 9344 */ 9345int 9346dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 9347 dtrace_meta_provider_id_t *idp) 9348{ 9349 dtrace_meta_t *meta; 9350 dtrace_helpers_t *help, *next; 9351 int i; 9352 9353 *idp = DTRACE_METAPROVNONE; 9354 9355 /* 9356 * We strictly don't need the name, but we hold onto it for 9357 * debuggability. All hail error queues! 9358 */ 9359 if (name == NULL) { 9360 cmn_err(CE_WARN, "failed to register meta-provider: " 9361 "invalid name"); 9362 return (EINVAL); 9363 } 9364 9365 if (mops == NULL || 9366 mops->dtms_create_probe == NULL || 9367 mops->dtms_provide_pid == NULL || 9368 mops->dtms_remove_pid == NULL) { 9369 cmn_err(CE_WARN, "failed to register meta-register %s: " 9370 "invalid ops", name); 9371 return (EINVAL); 9372 } 9373 9374 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 9375 meta->dtm_mops = *mops; 9376 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 9377 (void) strcpy(meta->dtm_name, name); 9378 meta->dtm_arg = arg; 9379 9380 mutex_enter(&dtrace_meta_lock); 9381 mutex_enter(&dtrace_lock); 9382 9383 if (dtrace_meta_pid != NULL) { 9384 mutex_exit(&dtrace_lock); 9385 mutex_exit(&dtrace_meta_lock); 9386 cmn_err(CE_WARN, "failed to register meta-register %s: " 9387 "user-land meta-provider exists", name); 9388 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 9389 kmem_free(meta, sizeof (dtrace_meta_t)); 9390 return (EINVAL); 9391 } 9392 9393 dtrace_meta_pid = meta; 9394 *idp = (dtrace_meta_provider_id_t)meta; 9395 9396 /* 9397 * If there are providers and probes ready to go, pass them 9398 * off to the new meta provider now. 9399 */ 9400 9401 help = dtrace_deferred_pid; 9402 dtrace_deferred_pid = NULL; 9403 9404 mutex_exit(&dtrace_lock); 9405 9406 while (help != NULL) { 9407 for (i = 0; i < help->dthps_nprovs; i++) { 9408 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 9409 help->dthps_pid); 9410 } 9411 9412 next = help->dthps_next; 9413 help->dthps_next = NULL; 9414 help->dthps_prev = NULL; 9415 help->dthps_deferred = 0; 9416 help = next; 9417 } 9418 9419 mutex_exit(&dtrace_meta_lock); 9420 9421 return (0); 9422} 9423 9424int 9425dtrace_meta_unregister(dtrace_meta_provider_id_t id) 9426{ 9427 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 9428 9429 mutex_enter(&dtrace_meta_lock); 9430 mutex_enter(&dtrace_lock); 9431 9432 if (old == dtrace_meta_pid) { 9433 pp = &dtrace_meta_pid; 9434 } else { 9435 panic("attempt to unregister non-existent " 9436 "dtrace meta-provider %p\n", (void *)old); 9437 } 9438 9439 if (old->dtm_count != 0) { 9440 mutex_exit(&dtrace_lock); 9441 mutex_exit(&dtrace_meta_lock); 9442 return (EBUSY); 9443 } 9444 9445 *pp = NULL; 9446 9447 mutex_exit(&dtrace_lock); 9448 mutex_exit(&dtrace_meta_lock); 9449 9450 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 9451 kmem_free(old, sizeof (dtrace_meta_t)); 9452 9453 return (0); 9454} 9455 9456 9457/* 9458 * DTrace DIF Object Functions 9459 */ 9460static int 9461dtrace_difo_err(uint_t pc, const char *format, ...) 9462{ 9463 if (dtrace_err_verbose) { 9464 va_list alist; 9465 9466 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 9467 va_start(alist, format); 9468 (void) vuprintf(format, alist); 9469 va_end(alist); 9470 } 9471 9472#ifdef DTRACE_ERRDEBUG 9473 dtrace_errdebug(format); 9474#endif 9475 return (1); 9476} 9477 9478/* 9479 * Validate a DTrace DIF object by checking the IR instructions. The following 9480 * rules are currently enforced by dtrace_difo_validate(): 9481 * 9482 * 1. Each instruction must have a valid opcode 9483 * 2. Each register, string, variable, or subroutine reference must be valid 9484 * 3. No instruction can modify register %r0 (must be zero) 9485 * 4. All instruction reserved bits must be set to zero 9486 * 5. The last instruction must be a "ret" instruction 9487 * 6. All branch targets must reference a valid instruction _after_ the branch 9488 */ 9489static int 9490dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 9491 cred_t *cr) 9492{ 9493 int err = 0, i; 9494 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9495 int kcheckload; 9496 uint_t pc; 9497 9498 kcheckload = cr == NULL || 9499 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 9500 9501 dp->dtdo_destructive = 0; 9502 9503 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 9504 dif_instr_t instr = dp->dtdo_buf[pc]; 9505 9506 uint_t r1 = DIF_INSTR_R1(instr); 9507 uint_t r2 = DIF_INSTR_R2(instr); 9508 uint_t rd = DIF_INSTR_RD(instr); 9509 uint_t rs = DIF_INSTR_RS(instr); 9510 uint_t label = DIF_INSTR_LABEL(instr); 9511 uint_t v = DIF_INSTR_VAR(instr); 9512 uint_t subr = DIF_INSTR_SUBR(instr); 9513 uint_t type = DIF_INSTR_TYPE(instr); 9514 uint_t op = DIF_INSTR_OP(instr); 9515 9516 switch (op) { 9517 case DIF_OP_OR: 9518 case DIF_OP_XOR: 9519 case DIF_OP_AND: 9520 case DIF_OP_SLL: 9521 case DIF_OP_SRL: 9522 case DIF_OP_SRA: 9523 case DIF_OP_SUB: 9524 case DIF_OP_ADD: 9525 case DIF_OP_MUL: 9526 case DIF_OP_SDIV: 9527 case DIF_OP_UDIV: 9528 case DIF_OP_SREM: 9529 case DIF_OP_UREM: 9530 case DIF_OP_COPYS: 9531 if (r1 >= nregs) 9532 err += efunc(pc, "invalid register %u\n", r1); 9533 if (r2 >= nregs) 9534 err += efunc(pc, "invalid register %u\n", r2); 9535 if (rd >= nregs) 9536 err += efunc(pc, "invalid register %u\n", rd); 9537 if (rd == 0) 9538 err += efunc(pc, "cannot write to %r0\n"); 9539 break; 9540 case DIF_OP_NOT: 9541 case DIF_OP_MOV: 9542 case DIF_OP_ALLOCS: 9543 if (r1 >= nregs) 9544 err += efunc(pc, "invalid register %u\n", r1); 9545 if (r2 != 0) 9546 err += efunc(pc, "non-zero reserved bits\n"); 9547 if (rd >= nregs) 9548 err += efunc(pc, "invalid register %u\n", rd); 9549 if (rd == 0) 9550 err += efunc(pc, "cannot write to %r0\n"); 9551 break; 9552 case DIF_OP_LDSB: 9553 case DIF_OP_LDSH: 9554 case DIF_OP_LDSW: 9555 case DIF_OP_LDUB: 9556 case DIF_OP_LDUH: 9557 case DIF_OP_LDUW: 9558 case DIF_OP_LDX: 9559 if (r1 >= nregs) 9560 err += efunc(pc, "invalid register %u\n", r1); 9561 if (r2 != 0) 9562 err += efunc(pc, "non-zero reserved bits\n"); 9563 if (rd >= nregs) 9564 err += efunc(pc, "invalid register %u\n", rd); 9565 if (rd == 0) 9566 err += efunc(pc, "cannot write to %r0\n"); 9567 if (kcheckload) 9568 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 9569 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 9570 break; 9571 case DIF_OP_RLDSB: 9572 case DIF_OP_RLDSH: 9573 case DIF_OP_RLDSW: 9574 case DIF_OP_RLDUB: 9575 case DIF_OP_RLDUH: 9576 case DIF_OP_RLDUW: 9577 case DIF_OP_RLDX: 9578 if (r1 >= nregs) 9579 err += efunc(pc, "invalid register %u\n", r1); 9580 if (r2 != 0) 9581 err += efunc(pc, "non-zero reserved bits\n"); 9582 if (rd >= nregs) 9583 err += efunc(pc, "invalid register %u\n", rd); 9584 if (rd == 0) 9585 err += efunc(pc, "cannot write to %r0\n"); 9586 break; 9587 case DIF_OP_ULDSB: 9588 case DIF_OP_ULDSH: 9589 case DIF_OP_ULDSW: 9590 case DIF_OP_ULDUB: 9591 case DIF_OP_ULDUH: 9592 case DIF_OP_ULDUW: 9593 case DIF_OP_ULDX: 9594 if (r1 >= nregs) 9595 err += efunc(pc, "invalid register %u\n", r1); 9596 if (r2 != 0) 9597 err += efunc(pc, "non-zero reserved bits\n"); 9598 if (rd >= nregs) 9599 err += efunc(pc, "invalid register %u\n", rd); 9600 if (rd == 0) 9601 err += efunc(pc, "cannot write to %r0\n"); 9602 break; 9603 case DIF_OP_STB: 9604 case DIF_OP_STH: 9605 case DIF_OP_STW: 9606 case DIF_OP_STX: 9607 if (r1 >= nregs) 9608 err += efunc(pc, "invalid register %u\n", r1); 9609 if (r2 != 0) 9610 err += efunc(pc, "non-zero reserved bits\n"); 9611 if (rd >= nregs) 9612 err += efunc(pc, "invalid register %u\n", rd); 9613 if (rd == 0) 9614 err += efunc(pc, "cannot write to 0 address\n"); 9615 break; 9616 case DIF_OP_CMP: 9617 case DIF_OP_SCMP: 9618 if (r1 >= nregs) 9619 err += efunc(pc, "invalid register %u\n", r1); 9620 if (r2 >= nregs) 9621 err += efunc(pc, "invalid register %u\n", r2); 9622 if (rd != 0) 9623 err += efunc(pc, "non-zero reserved bits\n"); 9624 break; 9625 case DIF_OP_TST: 9626 if (r1 >= nregs) 9627 err += efunc(pc, "invalid register %u\n", r1); 9628 if (r2 != 0 || rd != 0) 9629 err += efunc(pc, "non-zero reserved bits\n"); 9630 break; 9631 case DIF_OP_BA: 9632 case DIF_OP_BE: 9633 case DIF_OP_BNE: 9634 case DIF_OP_BG: 9635 case DIF_OP_BGU: 9636 case DIF_OP_BGE: 9637 case DIF_OP_BGEU: 9638 case DIF_OP_BL: 9639 case DIF_OP_BLU: 9640 case DIF_OP_BLE: 9641 case DIF_OP_BLEU: 9642 if (label >= dp->dtdo_len) { 9643 err += efunc(pc, "invalid branch target %u\n", 9644 label); 9645 } 9646 if (label <= pc) { 9647 err += efunc(pc, "backward branch to %u\n", 9648 label); 9649 } 9650 break; 9651 case DIF_OP_RET: 9652 if (r1 != 0 || r2 != 0) 9653 err += efunc(pc, "non-zero reserved bits\n"); 9654 if (rd >= nregs) 9655 err += efunc(pc, "invalid register %u\n", rd); 9656 break; 9657 case DIF_OP_NOP: 9658 case DIF_OP_POPTS: 9659 case DIF_OP_FLUSHTS: 9660 if (r1 != 0 || r2 != 0 || rd != 0) 9661 err += efunc(pc, "non-zero reserved bits\n"); 9662 break; 9663 case DIF_OP_SETX: 9664 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 9665 err += efunc(pc, "invalid integer ref %u\n", 9666 DIF_INSTR_INTEGER(instr)); 9667 } 9668 if (rd >= nregs) 9669 err += efunc(pc, "invalid register %u\n", rd); 9670 if (rd == 0) 9671 err += efunc(pc, "cannot write to %r0\n"); 9672 break; 9673 case DIF_OP_SETS: 9674 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 9675 err += efunc(pc, "invalid string ref %u\n", 9676 DIF_INSTR_STRING(instr)); 9677 } 9678 if (rd >= nregs) 9679 err += efunc(pc, "invalid register %u\n", rd); 9680 if (rd == 0) 9681 err += efunc(pc, "cannot write to %r0\n"); 9682 break; 9683 case DIF_OP_LDGA: 9684 case DIF_OP_LDTA: 9685 if (r1 > DIF_VAR_ARRAY_MAX) 9686 err += efunc(pc, "invalid array %u\n", r1); 9687 if (r2 >= nregs) 9688 err += efunc(pc, "invalid register %u\n", r2); 9689 if (rd >= nregs) 9690 err += efunc(pc, "invalid register %u\n", rd); 9691 if (rd == 0) 9692 err += efunc(pc, "cannot write to %r0\n"); 9693 break; 9694 case DIF_OP_LDGS: 9695 case DIF_OP_LDTS: 9696 case DIF_OP_LDLS: 9697 case DIF_OP_LDGAA: 9698 case DIF_OP_LDTAA: 9699 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 9700 err += efunc(pc, "invalid variable %u\n", v); 9701 if (rd >= nregs) 9702 err += efunc(pc, "invalid register %u\n", rd); 9703 if (rd == 0) 9704 err += efunc(pc, "cannot write to %r0\n"); 9705 break; 9706 case DIF_OP_STGS: 9707 case DIF_OP_STTS: 9708 case DIF_OP_STLS: 9709 case DIF_OP_STGAA: 9710 case DIF_OP_STTAA: 9711 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 9712 err += efunc(pc, "invalid variable %u\n", v); 9713 if (rs >= nregs) 9714 err += efunc(pc, "invalid register %u\n", rd); 9715 break; 9716 case DIF_OP_CALL: 9717 if (subr > DIF_SUBR_MAX) 9718 err += efunc(pc, "invalid subr %u\n", subr); 9719 if (rd >= nregs) 9720 err += efunc(pc, "invalid register %u\n", rd); 9721 if (rd == 0) 9722 err += efunc(pc, "cannot write to %r0\n"); 9723 9724 if (subr == DIF_SUBR_COPYOUT || 9725 subr == DIF_SUBR_COPYOUTSTR) { 9726 dp->dtdo_destructive = 1; 9727 } 9728 9729 if (subr == DIF_SUBR_GETF) { 9730 /* 9731 * If we have a getf() we need to record that 9732 * in our state. Note that our state can be 9733 * NULL if this is a helper -- but in that 9734 * case, the call to getf() is itself illegal, 9735 * and will be caught (slightly later) when 9736 * the helper is validated. 9737 */ 9738 if (vstate->dtvs_state != NULL) 9739 vstate->dtvs_state->dts_getf++; 9740 } 9741 9742 break; 9743 case DIF_OP_PUSHTR: 9744 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 9745 err += efunc(pc, "invalid ref type %u\n", type); 9746 if (r2 >= nregs) 9747 err += efunc(pc, "invalid register %u\n", r2); 9748 if (rs >= nregs) 9749 err += efunc(pc, "invalid register %u\n", rs); 9750 break; 9751 case DIF_OP_PUSHTV: 9752 if (type != DIF_TYPE_CTF) 9753 err += efunc(pc, "invalid val type %u\n", type); 9754 if (r2 >= nregs) 9755 err += efunc(pc, "invalid register %u\n", r2); 9756 if (rs >= nregs) 9757 err += efunc(pc, "invalid register %u\n", rs); 9758 break; 9759 default: 9760 err += efunc(pc, "invalid opcode %u\n", 9761 DIF_INSTR_OP(instr)); 9762 } 9763 } 9764 9765 if (dp->dtdo_len != 0 && 9766 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 9767 err += efunc(dp->dtdo_len - 1, 9768 "expected 'ret' as last DIF instruction\n"); 9769 } 9770 9771 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) { 9772 /* 9773 * If we're not returning by reference, the size must be either 9774 * 0 or the size of one of the base types. 9775 */ 9776 switch (dp->dtdo_rtype.dtdt_size) { 9777 case 0: 9778 case sizeof (uint8_t): 9779 case sizeof (uint16_t): 9780 case sizeof (uint32_t): 9781 case sizeof (uint64_t): 9782 break; 9783 9784 default: 9785 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 9786 } 9787 } 9788 9789 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 9790 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 9791 dtrace_diftype_t *vt, *et; 9792 uint_t id, ndx; 9793 9794 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 9795 v->dtdv_scope != DIFV_SCOPE_THREAD && 9796 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 9797 err += efunc(i, "unrecognized variable scope %d\n", 9798 v->dtdv_scope); 9799 break; 9800 } 9801 9802 if (v->dtdv_kind != DIFV_KIND_ARRAY && 9803 v->dtdv_kind != DIFV_KIND_SCALAR) { 9804 err += efunc(i, "unrecognized variable type %d\n", 9805 v->dtdv_kind); 9806 break; 9807 } 9808 9809 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 9810 err += efunc(i, "%d exceeds variable id limit\n", id); 9811 break; 9812 } 9813 9814 if (id < DIF_VAR_OTHER_UBASE) 9815 continue; 9816 9817 /* 9818 * For user-defined variables, we need to check that this 9819 * definition is identical to any previous definition that we 9820 * encountered. 9821 */ 9822 ndx = id - DIF_VAR_OTHER_UBASE; 9823 9824 switch (v->dtdv_scope) { 9825 case DIFV_SCOPE_GLOBAL: 9826 if (ndx < vstate->dtvs_nglobals) { 9827 dtrace_statvar_t *svar; 9828 9829 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 9830 existing = &svar->dtsv_var; 9831 } 9832 9833 break; 9834 9835 case DIFV_SCOPE_THREAD: 9836 if (ndx < vstate->dtvs_ntlocals) 9837 existing = &vstate->dtvs_tlocals[ndx]; 9838 break; 9839 9840 case DIFV_SCOPE_LOCAL: 9841 if (ndx < vstate->dtvs_nlocals) { 9842 dtrace_statvar_t *svar; 9843 9844 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 9845 existing = &svar->dtsv_var; 9846 } 9847 9848 break; 9849 } 9850 9851 vt = &v->dtdv_type; 9852 9853 if (vt->dtdt_flags & DIF_TF_BYREF) { 9854 if (vt->dtdt_size == 0) { 9855 err += efunc(i, "zero-sized variable\n"); 9856 break; 9857 } 9858 9859 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 9860 vt->dtdt_size > dtrace_global_maxsize) { 9861 err += efunc(i, "oversized by-ref global\n"); 9862 break; 9863 } 9864 } 9865 9866 if (existing == NULL || existing->dtdv_id == 0) 9867 continue; 9868 9869 ASSERT(existing->dtdv_id == v->dtdv_id); 9870 ASSERT(existing->dtdv_scope == v->dtdv_scope); 9871 9872 if (existing->dtdv_kind != v->dtdv_kind) 9873 err += efunc(i, "%d changed variable kind\n", id); 9874 9875 et = &existing->dtdv_type; 9876 9877 if (vt->dtdt_flags != et->dtdt_flags) { 9878 err += efunc(i, "%d changed variable type flags\n", id); 9879 break; 9880 } 9881 9882 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 9883 err += efunc(i, "%d changed variable type size\n", id); 9884 break; 9885 } 9886 } 9887 9888 return (err); 9889} 9890 9891/* 9892 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 9893 * are much more constrained than normal DIFOs. Specifically, they may 9894 * not: 9895 * 9896 * 1. Make calls to subroutines other than copyin(), copyinstr() or 9897 * miscellaneous string routines 9898 * 2. Access DTrace variables other than the args[] array, and the 9899 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 9900 * 3. Have thread-local variables. 9901 * 4. Have dynamic variables. 9902 */ 9903static int 9904dtrace_difo_validate_helper(dtrace_difo_t *dp) 9905{ 9906 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9907 int err = 0; 9908 uint_t pc; 9909 9910 for (pc = 0; pc < dp->dtdo_len; pc++) { 9911 dif_instr_t instr = dp->dtdo_buf[pc]; 9912 9913 uint_t v = DIF_INSTR_VAR(instr); 9914 uint_t subr = DIF_INSTR_SUBR(instr); 9915 uint_t op = DIF_INSTR_OP(instr); 9916 9917 switch (op) { 9918 case DIF_OP_OR: 9919 case DIF_OP_XOR: 9920 case DIF_OP_AND: 9921 case DIF_OP_SLL: 9922 case DIF_OP_SRL: 9923 case DIF_OP_SRA: 9924 case DIF_OP_SUB: 9925 case DIF_OP_ADD: 9926 case DIF_OP_MUL: 9927 case DIF_OP_SDIV: 9928 case DIF_OP_UDIV: 9929 case DIF_OP_SREM: 9930 case DIF_OP_UREM: 9931 case DIF_OP_COPYS: 9932 case DIF_OP_NOT: 9933 case DIF_OP_MOV: 9934 case DIF_OP_RLDSB: 9935 case DIF_OP_RLDSH: 9936 case DIF_OP_RLDSW: 9937 case DIF_OP_RLDUB: 9938 case DIF_OP_RLDUH: 9939 case DIF_OP_RLDUW: 9940 case DIF_OP_RLDX: 9941 case DIF_OP_ULDSB: 9942 case DIF_OP_ULDSH: 9943 case DIF_OP_ULDSW: 9944 case DIF_OP_ULDUB: 9945 case DIF_OP_ULDUH: 9946 case DIF_OP_ULDUW: 9947 case DIF_OP_ULDX: 9948 case DIF_OP_STB: 9949 case DIF_OP_STH: 9950 case DIF_OP_STW: 9951 case DIF_OP_STX: 9952 case DIF_OP_ALLOCS: 9953 case DIF_OP_CMP: 9954 case DIF_OP_SCMP: 9955 case DIF_OP_TST: 9956 case DIF_OP_BA: 9957 case DIF_OP_BE: 9958 case DIF_OP_BNE: 9959 case DIF_OP_BG: 9960 case DIF_OP_BGU: 9961 case DIF_OP_BGE: 9962 case DIF_OP_BGEU: 9963 case DIF_OP_BL: 9964 case DIF_OP_BLU: 9965 case DIF_OP_BLE: 9966 case DIF_OP_BLEU: 9967 case DIF_OP_RET: 9968 case DIF_OP_NOP: 9969 case DIF_OP_POPTS: 9970 case DIF_OP_FLUSHTS: 9971 case DIF_OP_SETX: 9972 case DIF_OP_SETS: 9973 case DIF_OP_LDGA: 9974 case DIF_OP_LDLS: 9975 case DIF_OP_STGS: 9976 case DIF_OP_STLS: 9977 case DIF_OP_PUSHTR: 9978 case DIF_OP_PUSHTV: 9979 break; 9980 9981 case DIF_OP_LDGS: 9982 if (v >= DIF_VAR_OTHER_UBASE) 9983 break; 9984 9985 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 9986 break; 9987 9988 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 9989 v == DIF_VAR_PPID || v == DIF_VAR_TID || 9990 v == DIF_VAR_EXECARGS || 9991 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 9992 v == DIF_VAR_UID || v == DIF_VAR_GID) 9993 break; 9994 9995 err += efunc(pc, "illegal variable %u\n", v); 9996 break; 9997 9998 case DIF_OP_LDTA: 9999 case DIF_OP_LDTS: 10000 case DIF_OP_LDGAA: 10001 case DIF_OP_LDTAA: 10002 err += efunc(pc, "illegal dynamic variable load\n"); 10003 break; 10004 10005 case DIF_OP_STTS: 10006 case DIF_OP_STGAA: 10007 case DIF_OP_STTAA: 10008 err += efunc(pc, "illegal dynamic variable store\n"); 10009 break; 10010 10011 case DIF_OP_CALL: 10012 if (subr == DIF_SUBR_ALLOCA || 10013 subr == DIF_SUBR_BCOPY || 10014 subr == DIF_SUBR_COPYIN || 10015 subr == DIF_SUBR_COPYINTO || 10016 subr == DIF_SUBR_COPYINSTR || 10017 subr == DIF_SUBR_INDEX || 10018 subr == DIF_SUBR_INET_NTOA || 10019 subr == DIF_SUBR_INET_NTOA6 || 10020 subr == DIF_SUBR_INET_NTOP || 10021 subr == DIF_SUBR_JSON || 10022 subr == DIF_SUBR_LLTOSTR || 10023 subr == DIF_SUBR_STRTOLL || 10024 subr == DIF_SUBR_RINDEX || 10025 subr == DIF_SUBR_STRCHR || 10026 subr == DIF_SUBR_STRJOIN || 10027 subr == DIF_SUBR_STRRCHR || 10028 subr == DIF_SUBR_STRSTR || 10029 subr == DIF_SUBR_HTONS || 10030 subr == DIF_SUBR_HTONL || 10031 subr == DIF_SUBR_HTONLL || 10032 subr == DIF_SUBR_NTOHS || 10033 subr == DIF_SUBR_NTOHL || 10034 subr == DIF_SUBR_NTOHLL || 10035 subr == DIF_SUBR_MEMREF || 10036 subr == DIF_SUBR_TYPEREF) 10037 break; 10038 10039 err += efunc(pc, "invalid subr %u\n", subr); 10040 break; 10041 10042 default: 10043 err += efunc(pc, "invalid opcode %u\n", 10044 DIF_INSTR_OP(instr)); 10045 } 10046 } 10047 10048 return (err); 10049} 10050 10051/* 10052 * Returns 1 if the expression in the DIF object can be cached on a per-thread 10053 * basis; 0 if not. 10054 */ 10055static int 10056dtrace_difo_cacheable(dtrace_difo_t *dp) 10057{ 10058 int i; 10059 10060 if (dp == NULL) 10061 return (0); 10062 10063 for (i = 0; i < dp->dtdo_varlen; i++) { 10064 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10065 10066 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 10067 continue; 10068 10069 switch (v->dtdv_id) { 10070 case DIF_VAR_CURTHREAD: 10071 case DIF_VAR_PID: 10072 case DIF_VAR_TID: 10073 case DIF_VAR_EXECARGS: 10074 case DIF_VAR_EXECNAME: 10075 case DIF_VAR_ZONENAME: 10076 break; 10077 10078 default: 10079 return (0); 10080 } 10081 } 10082 10083 /* 10084 * This DIF object may be cacheable. Now we need to look for any 10085 * array loading instructions, any memory loading instructions, or 10086 * any stores to thread-local variables. 10087 */ 10088 for (i = 0; i < dp->dtdo_len; i++) { 10089 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 10090 10091 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 10092 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 10093 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 10094 op == DIF_OP_LDGA || op == DIF_OP_STTS) 10095 return (0); 10096 } 10097 10098 return (1); 10099} 10100 10101static void 10102dtrace_difo_hold(dtrace_difo_t *dp) 10103{ 10104 int i; 10105 10106 ASSERT(MUTEX_HELD(&dtrace_lock)); 10107 10108 dp->dtdo_refcnt++; 10109 ASSERT(dp->dtdo_refcnt != 0); 10110 10111 /* 10112 * We need to check this DIF object for references to the variable 10113 * DIF_VAR_VTIMESTAMP. 10114 */ 10115 for (i = 0; i < dp->dtdo_varlen; i++) { 10116 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10117 10118 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10119 continue; 10120 10121 if (dtrace_vtime_references++ == 0) 10122 dtrace_vtime_enable(); 10123 } 10124} 10125 10126/* 10127 * This routine calculates the dynamic variable chunksize for a given DIF 10128 * object. The calculation is not fool-proof, and can probably be tricked by 10129 * malicious DIF -- but it works for all compiler-generated DIF. Because this 10130 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 10131 * if a dynamic variable size exceeds the chunksize. 10132 */ 10133static void 10134dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10135{ 10136 uint64_t sval = 0; 10137 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 10138 const dif_instr_t *text = dp->dtdo_buf; 10139 uint_t pc, srd = 0; 10140 uint_t ttop = 0; 10141 size_t size, ksize; 10142 uint_t id, i; 10143 10144 for (pc = 0; pc < dp->dtdo_len; pc++) { 10145 dif_instr_t instr = text[pc]; 10146 uint_t op = DIF_INSTR_OP(instr); 10147 uint_t rd = DIF_INSTR_RD(instr); 10148 uint_t r1 = DIF_INSTR_R1(instr); 10149 uint_t nkeys = 0; 10150 uchar_t scope = 0; 10151 10152 dtrace_key_t *key = tupregs; 10153 10154 switch (op) { 10155 case DIF_OP_SETX: 10156 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 10157 srd = rd; 10158 continue; 10159 10160 case DIF_OP_STTS: 10161 key = &tupregs[DIF_DTR_NREGS]; 10162 key[0].dttk_size = 0; 10163 key[1].dttk_size = 0; 10164 nkeys = 2; 10165 scope = DIFV_SCOPE_THREAD; 10166 break; 10167 10168 case DIF_OP_STGAA: 10169 case DIF_OP_STTAA: 10170 nkeys = ttop; 10171 10172 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 10173 key[nkeys++].dttk_size = 0; 10174 10175 key[nkeys++].dttk_size = 0; 10176 10177 if (op == DIF_OP_STTAA) { 10178 scope = DIFV_SCOPE_THREAD; 10179 } else { 10180 scope = DIFV_SCOPE_GLOBAL; 10181 } 10182 10183 break; 10184 10185 case DIF_OP_PUSHTR: 10186 if (ttop == DIF_DTR_NREGS) 10187 return; 10188 10189 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 10190 /* 10191 * If the register for the size of the "pushtr" 10192 * is %r0 (or the value is 0) and the type is 10193 * a string, we'll use the system-wide default 10194 * string size. 10195 */ 10196 tupregs[ttop++].dttk_size = 10197 dtrace_strsize_default; 10198 } else { 10199 if (srd == 0) 10200 return; 10201 10202 tupregs[ttop++].dttk_size = sval; 10203 } 10204 10205 break; 10206 10207 case DIF_OP_PUSHTV: 10208 if (ttop == DIF_DTR_NREGS) 10209 return; 10210 10211 tupregs[ttop++].dttk_size = 0; 10212 break; 10213 10214 case DIF_OP_FLUSHTS: 10215 ttop = 0; 10216 break; 10217 10218 case DIF_OP_POPTS: 10219 if (ttop != 0) 10220 ttop--; 10221 break; 10222 } 10223 10224 sval = 0; 10225 srd = 0; 10226 10227 if (nkeys == 0) 10228 continue; 10229 10230 /* 10231 * We have a dynamic variable allocation; calculate its size. 10232 */ 10233 for (ksize = 0, i = 0; i < nkeys; i++) 10234 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 10235 10236 size = sizeof (dtrace_dynvar_t); 10237 size += sizeof (dtrace_key_t) * (nkeys - 1); 10238 size += ksize; 10239 10240 /* 10241 * Now we need to determine the size of the stored data. 10242 */ 10243 id = DIF_INSTR_VAR(instr); 10244 10245 for (i = 0; i < dp->dtdo_varlen; i++) { 10246 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10247 10248 if (v->dtdv_id == id && v->dtdv_scope == scope) { 10249 size += v->dtdv_type.dtdt_size; 10250 break; 10251 } 10252 } 10253 10254 if (i == dp->dtdo_varlen) 10255 return; 10256 10257 /* 10258 * We have the size. If this is larger than the chunk size 10259 * for our dynamic variable state, reset the chunk size. 10260 */ 10261 size = P2ROUNDUP(size, sizeof (uint64_t)); 10262 10263 if (size > vstate->dtvs_dynvars.dtds_chunksize) 10264 vstate->dtvs_dynvars.dtds_chunksize = size; 10265 } 10266} 10267 10268static void 10269dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10270{ 10271 int i, oldsvars, osz, nsz, otlocals, ntlocals; 10272 uint_t id; 10273 10274 ASSERT(MUTEX_HELD(&dtrace_lock)); 10275 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 10276 10277 for (i = 0; i < dp->dtdo_varlen; i++) { 10278 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10279 dtrace_statvar_t *svar, ***svarp = NULL; 10280 size_t dsize = 0; 10281 uint8_t scope = v->dtdv_scope; 10282 int *np = NULL; 10283 10284 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10285 continue; 10286 10287 id -= DIF_VAR_OTHER_UBASE; 10288 10289 switch (scope) { 10290 case DIFV_SCOPE_THREAD: 10291 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 10292 dtrace_difv_t *tlocals; 10293 10294 if ((ntlocals = (otlocals << 1)) == 0) 10295 ntlocals = 1; 10296 10297 osz = otlocals * sizeof (dtrace_difv_t); 10298 nsz = ntlocals * sizeof (dtrace_difv_t); 10299 10300 tlocals = kmem_zalloc(nsz, KM_SLEEP); 10301 10302 if (osz != 0) { 10303 bcopy(vstate->dtvs_tlocals, 10304 tlocals, osz); 10305 kmem_free(vstate->dtvs_tlocals, osz); 10306 } 10307 10308 vstate->dtvs_tlocals = tlocals; 10309 vstate->dtvs_ntlocals = ntlocals; 10310 } 10311 10312 vstate->dtvs_tlocals[id] = *v; 10313 continue; 10314 10315 case DIFV_SCOPE_LOCAL: 10316 np = &vstate->dtvs_nlocals; 10317 svarp = &vstate->dtvs_locals; 10318 10319 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10320 dsize = NCPU * (v->dtdv_type.dtdt_size + 10321 sizeof (uint64_t)); 10322 else 10323 dsize = NCPU * sizeof (uint64_t); 10324 10325 break; 10326 10327 case DIFV_SCOPE_GLOBAL: 10328 np = &vstate->dtvs_nglobals; 10329 svarp = &vstate->dtvs_globals; 10330 10331 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10332 dsize = v->dtdv_type.dtdt_size + 10333 sizeof (uint64_t); 10334 10335 break; 10336 10337 default: 10338 ASSERT(0); 10339 } 10340 10341 while (id >= (oldsvars = *np)) { 10342 dtrace_statvar_t **statics; 10343 int newsvars, oldsize, newsize; 10344 10345 if ((newsvars = (oldsvars << 1)) == 0) 10346 newsvars = 1; 10347 10348 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 10349 newsize = newsvars * sizeof (dtrace_statvar_t *); 10350 10351 statics = kmem_zalloc(newsize, KM_SLEEP); 10352 10353 if (oldsize != 0) { 10354 bcopy(*svarp, statics, oldsize); 10355 kmem_free(*svarp, oldsize); 10356 } 10357 10358 *svarp = statics; 10359 *np = newsvars; 10360 } 10361 10362 if ((svar = (*svarp)[id]) == NULL) { 10363 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 10364 svar->dtsv_var = *v; 10365 10366 if ((svar->dtsv_size = dsize) != 0) { 10367 svar->dtsv_data = (uint64_t)(uintptr_t) 10368 kmem_zalloc(dsize, KM_SLEEP); 10369 } 10370 10371 (*svarp)[id] = svar; 10372 } 10373 10374 svar->dtsv_refcnt++; 10375 } 10376 10377 dtrace_difo_chunksize(dp, vstate); 10378 dtrace_difo_hold(dp); 10379} 10380 10381static dtrace_difo_t * 10382dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10383{ 10384 dtrace_difo_t *new; 10385 size_t sz; 10386 10387 ASSERT(dp->dtdo_buf != NULL); 10388 ASSERT(dp->dtdo_refcnt != 0); 10389 10390 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10391 10392 ASSERT(dp->dtdo_buf != NULL); 10393 sz = dp->dtdo_len * sizeof (dif_instr_t); 10394 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 10395 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 10396 new->dtdo_len = dp->dtdo_len; 10397 10398 if (dp->dtdo_strtab != NULL) { 10399 ASSERT(dp->dtdo_strlen != 0); 10400 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 10401 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 10402 new->dtdo_strlen = dp->dtdo_strlen; 10403 } 10404 10405 if (dp->dtdo_inttab != NULL) { 10406 ASSERT(dp->dtdo_intlen != 0); 10407 sz = dp->dtdo_intlen * sizeof (uint64_t); 10408 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 10409 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 10410 new->dtdo_intlen = dp->dtdo_intlen; 10411 } 10412 10413 if (dp->dtdo_vartab != NULL) { 10414 ASSERT(dp->dtdo_varlen != 0); 10415 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 10416 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 10417 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 10418 new->dtdo_varlen = dp->dtdo_varlen; 10419 } 10420 10421 dtrace_difo_init(new, vstate); 10422 return (new); 10423} 10424 10425static void 10426dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10427{ 10428 int i; 10429 10430 ASSERT(dp->dtdo_refcnt == 0); 10431 10432 for (i = 0; i < dp->dtdo_varlen; i++) { 10433 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10434 dtrace_statvar_t *svar, **svarp = NULL; 10435 uint_t id; 10436 uint8_t scope = v->dtdv_scope; 10437 int *np = NULL; 10438 10439 switch (scope) { 10440 case DIFV_SCOPE_THREAD: 10441 continue; 10442 10443 case DIFV_SCOPE_LOCAL: 10444 np = &vstate->dtvs_nlocals; 10445 svarp = vstate->dtvs_locals; 10446 break; 10447 10448 case DIFV_SCOPE_GLOBAL: 10449 np = &vstate->dtvs_nglobals; 10450 svarp = vstate->dtvs_globals; 10451 break; 10452 10453 default: 10454 ASSERT(0); 10455 } 10456 10457 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10458 continue; 10459 10460 id -= DIF_VAR_OTHER_UBASE; 10461 ASSERT(id < *np); 10462 10463 svar = svarp[id]; 10464 ASSERT(svar != NULL); 10465 ASSERT(svar->dtsv_refcnt > 0); 10466 10467 if (--svar->dtsv_refcnt > 0) 10468 continue; 10469 10470 if (svar->dtsv_size != 0) { 10471 ASSERT(svar->dtsv_data != 0); 10472 kmem_free((void *)(uintptr_t)svar->dtsv_data, 10473 svar->dtsv_size); 10474 } 10475 10476 kmem_free(svar, sizeof (dtrace_statvar_t)); 10477 svarp[id] = NULL; 10478 } 10479 10480 if (dp->dtdo_buf != NULL) 10481 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10482 if (dp->dtdo_inttab != NULL) 10483 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10484 if (dp->dtdo_strtab != NULL) 10485 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10486 if (dp->dtdo_vartab != NULL) 10487 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10488 10489 kmem_free(dp, sizeof (dtrace_difo_t)); 10490} 10491 10492static void 10493dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10494{ 10495 int i; 10496 10497 ASSERT(MUTEX_HELD(&dtrace_lock)); 10498 ASSERT(dp->dtdo_refcnt != 0); 10499 10500 for (i = 0; i < dp->dtdo_varlen; i++) { 10501 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10502 10503 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10504 continue; 10505 10506 ASSERT(dtrace_vtime_references > 0); 10507 if (--dtrace_vtime_references == 0) 10508 dtrace_vtime_disable(); 10509 } 10510 10511 if (--dp->dtdo_refcnt == 0) 10512 dtrace_difo_destroy(dp, vstate); 10513} 10514 10515/* 10516 * DTrace Format Functions 10517 */ 10518static uint16_t 10519dtrace_format_add(dtrace_state_t *state, char *str) 10520{ 10521 char *fmt, **new; 10522 uint16_t ndx, len = strlen(str) + 1; 10523 10524 fmt = kmem_zalloc(len, KM_SLEEP); 10525 bcopy(str, fmt, len); 10526 10527 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 10528 if (state->dts_formats[ndx] == NULL) { 10529 state->dts_formats[ndx] = fmt; 10530 return (ndx + 1); 10531 } 10532 } 10533 10534 if (state->dts_nformats == USHRT_MAX) { 10535 /* 10536 * This is only likely if a denial-of-service attack is being 10537 * attempted. As such, it's okay to fail silently here. 10538 */ 10539 kmem_free(fmt, len); 10540 return (0); 10541 } 10542 10543 /* 10544 * For simplicity, we always resize the formats array to be exactly the 10545 * number of formats. 10546 */ 10547 ndx = state->dts_nformats++; 10548 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 10549 10550 if (state->dts_formats != NULL) { 10551 ASSERT(ndx != 0); 10552 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 10553 kmem_free(state->dts_formats, ndx * sizeof (char *)); 10554 } 10555 10556 state->dts_formats = new; 10557 state->dts_formats[ndx] = fmt; 10558 10559 return (ndx + 1); 10560} 10561 10562static void 10563dtrace_format_remove(dtrace_state_t *state, uint16_t format) 10564{ 10565 char *fmt; 10566 10567 ASSERT(state->dts_formats != NULL); 10568 ASSERT(format <= state->dts_nformats); 10569 ASSERT(state->dts_formats[format - 1] != NULL); 10570 10571 fmt = state->dts_formats[format - 1]; 10572 kmem_free(fmt, strlen(fmt) + 1); 10573 state->dts_formats[format - 1] = NULL; 10574} 10575 10576static void 10577dtrace_format_destroy(dtrace_state_t *state) 10578{ 10579 int i; 10580 10581 if (state->dts_nformats == 0) { 10582 ASSERT(state->dts_formats == NULL); 10583 return; 10584 } 10585 10586 ASSERT(state->dts_formats != NULL); 10587 10588 for (i = 0; i < state->dts_nformats; i++) { 10589 char *fmt = state->dts_formats[i]; 10590 10591 if (fmt == NULL) 10592 continue; 10593 10594 kmem_free(fmt, strlen(fmt) + 1); 10595 } 10596 10597 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 10598 state->dts_nformats = 0; 10599 state->dts_formats = NULL; 10600} 10601 10602/* 10603 * DTrace Predicate Functions 10604 */ 10605static dtrace_predicate_t * 10606dtrace_predicate_create(dtrace_difo_t *dp) 10607{ 10608 dtrace_predicate_t *pred; 10609 10610 ASSERT(MUTEX_HELD(&dtrace_lock)); 10611 ASSERT(dp->dtdo_refcnt != 0); 10612 10613 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 10614 pred->dtp_difo = dp; 10615 pred->dtp_refcnt = 1; 10616 10617 if (!dtrace_difo_cacheable(dp)) 10618 return (pred); 10619 10620 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 10621 /* 10622 * This is only theoretically possible -- we have had 2^32 10623 * cacheable predicates on this machine. We cannot allow any 10624 * more predicates to become cacheable: as unlikely as it is, 10625 * there may be a thread caching a (now stale) predicate cache 10626 * ID. (N.B.: the temptation is being successfully resisted to 10627 * have this cmn_err() "Holy shit -- we executed this code!") 10628 */ 10629 return (pred); 10630 } 10631 10632 pred->dtp_cacheid = dtrace_predcache_id++; 10633 10634 return (pred); 10635} 10636 10637static void 10638dtrace_predicate_hold(dtrace_predicate_t *pred) 10639{ 10640 ASSERT(MUTEX_HELD(&dtrace_lock)); 10641 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 10642 ASSERT(pred->dtp_refcnt > 0); 10643 10644 pred->dtp_refcnt++; 10645} 10646 10647static void 10648dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 10649{ 10650 dtrace_difo_t *dp = pred->dtp_difo; 10651 10652 ASSERT(MUTEX_HELD(&dtrace_lock)); 10653 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 10654 ASSERT(pred->dtp_refcnt > 0); 10655 10656 if (--pred->dtp_refcnt == 0) { 10657 dtrace_difo_release(pred->dtp_difo, vstate); 10658 kmem_free(pred, sizeof (dtrace_predicate_t)); 10659 } 10660} 10661 10662/* 10663 * DTrace Action Description Functions 10664 */ 10665static dtrace_actdesc_t * 10666dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 10667 uint64_t uarg, uint64_t arg) 10668{ 10669 dtrace_actdesc_t *act; 10670 10671#if defined(sun) 10672 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 10673 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 10674#endif 10675 10676 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 10677 act->dtad_kind = kind; 10678 act->dtad_ntuple = ntuple; 10679 act->dtad_uarg = uarg; 10680 act->dtad_arg = arg; 10681 act->dtad_refcnt = 1; 10682 10683 return (act); 10684} 10685 10686static void 10687dtrace_actdesc_hold(dtrace_actdesc_t *act) 10688{ 10689 ASSERT(act->dtad_refcnt >= 1); 10690 act->dtad_refcnt++; 10691} 10692 10693static void 10694dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 10695{ 10696 dtrace_actkind_t kind = act->dtad_kind; 10697 dtrace_difo_t *dp; 10698 10699 ASSERT(act->dtad_refcnt >= 1); 10700 10701 if (--act->dtad_refcnt != 0) 10702 return; 10703 10704 if ((dp = act->dtad_difo) != NULL) 10705 dtrace_difo_release(dp, vstate); 10706 10707 if (DTRACEACT_ISPRINTFLIKE(kind)) { 10708 char *str = (char *)(uintptr_t)act->dtad_arg; 10709 10710#if defined(sun) 10711 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 10712 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 10713#endif 10714 10715 if (str != NULL) 10716 kmem_free(str, strlen(str) + 1); 10717 } 10718 10719 kmem_free(act, sizeof (dtrace_actdesc_t)); 10720} 10721 10722/* 10723 * DTrace ECB Functions 10724 */ 10725static dtrace_ecb_t * 10726dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 10727{ 10728 dtrace_ecb_t *ecb; 10729 dtrace_epid_t epid; 10730 10731 ASSERT(MUTEX_HELD(&dtrace_lock)); 10732 10733 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 10734 ecb->dte_predicate = NULL; 10735 ecb->dte_probe = probe; 10736 10737 /* 10738 * The default size is the size of the default action: recording 10739 * the header. 10740 */ 10741 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 10742 ecb->dte_alignment = sizeof (dtrace_epid_t); 10743 10744 epid = state->dts_epid++; 10745 10746 if (epid - 1 >= state->dts_necbs) { 10747 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 10748 int necbs = state->dts_necbs << 1; 10749 10750 ASSERT(epid == state->dts_necbs + 1); 10751 10752 if (necbs == 0) { 10753 ASSERT(oecbs == NULL); 10754 necbs = 1; 10755 } 10756 10757 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 10758 10759 if (oecbs != NULL) 10760 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 10761 10762 dtrace_membar_producer(); 10763 state->dts_ecbs = ecbs; 10764 10765 if (oecbs != NULL) { 10766 /* 10767 * If this state is active, we must dtrace_sync() 10768 * before we can free the old dts_ecbs array: we're 10769 * coming in hot, and there may be active ring 10770 * buffer processing (which indexes into the dts_ecbs 10771 * array) on another CPU. 10772 */ 10773 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 10774 dtrace_sync(); 10775 10776 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 10777 } 10778 10779 dtrace_membar_producer(); 10780 state->dts_necbs = necbs; 10781 } 10782 10783 ecb->dte_state = state; 10784 10785 ASSERT(state->dts_ecbs[epid - 1] == NULL); 10786 dtrace_membar_producer(); 10787 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 10788 10789 return (ecb); 10790} 10791 10792static void 10793dtrace_ecb_enable(dtrace_ecb_t *ecb) 10794{ 10795 dtrace_probe_t *probe = ecb->dte_probe; 10796 10797 ASSERT(MUTEX_HELD(&cpu_lock)); 10798 ASSERT(MUTEX_HELD(&dtrace_lock)); 10799 ASSERT(ecb->dte_next == NULL); 10800 10801 if (probe == NULL) { 10802 /* 10803 * This is the NULL probe -- there's nothing to do. 10804 */ 10805 return; 10806 } 10807 10808 if (probe->dtpr_ecb == NULL) { 10809 dtrace_provider_t *prov = probe->dtpr_provider; 10810 10811 /* 10812 * We're the first ECB on this probe. 10813 */ 10814 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 10815 10816 if (ecb->dte_predicate != NULL) 10817 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 10818 10819 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 10820 probe->dtpr_id, probe->dtpr_arg); 10821 } else { 10822 /* 10823 * This probe is already active. Swing the last pointer to 10824 * point to the new ECB, and issue a dtrace_sync() to assure 10825 * that all CPUs have seen the change. 10826 */ 10827 ASSERT(probe->dtpr_ecb_last != NULL); 10828 probe->dtpr_ecb_last->dte_next = ecb; 10829 probe->dtpr_ecb_last = ecb; 10830 probe->dtpr_predcache = 0; 10831 10832 dtrace_sync(); 10833 } 10834} 10835 10836static void 10837dtrace_ecb_resize(dtrace_ecb_t *ecb) 10838{ 10839 dtrace_action_t *act; 10840 uint32_t curneeded = UINT32_MAX; 10841 uint32_t aggbase = UINT32_MAX; 10842 10843 /* 10844 * If we record anything, we always record the dtrace_rechdr_t. (And 10845 * we always record it first.) 10846 */ 10847 ecb->dte_size = sizeof (dtrace_rechdr_t); 10848 ecb->dte_alignment = sizeof (dtrace_epid_t); 10849 10850 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 10851 dtrace_recdesc_t *rec = &act->dta_rec; 10852 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 10853 10854 ecb->dte_alignment = MAX(ecb->dte_alignment, 10855 rec->dtrd_alignment); 10856 10857 if (DTRACEACT_ISAGG(act->dta_kind)) { 10858 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 10859 10860 ASSERT(rec->dtrd_size != 0); 10861 ASSERT(agg->dtag_first != NULL); 10862 ASSERT(act->dta_prev->dta_intuple); 10863 ASSERT(aggbase != UINT32_MAX); 10864 ASSERT(curneeded != UINT32_MAX); 10865 10866 agg->dtag_base = aggbase; 10867 10868 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 10869 rec->dtrd_offset = curneeded; 10870 curneeded += rec->dtrd_size; 10871 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 10872 10873 aggbase = UINT32_MAX; 10874 curneeded = UINT32_MAX; 10875 } else if (act->dta_intuple) { 10876 if (curneeded == UINT32_MAX) { 10877 /* 10878 * This is the first record in a tuple. Align 10879 * curneeded to be at offset 4 in an 8-byte 10880 * aligned block. 10881 */ 10882 ASSERT(act->dta_prev == NULL || 10883 !act->dta_prev->dta_intuple); 10884 ASSERT3U(aggbase, ==, UINT32_MAX); 10885 curneeded = P2PHASEUP(ecb->dte_size, 10886 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 10887 10888 aggbase = curneeded - sizeof (dtrace_aggid_t); 10889 ASSERT(IS_P2ALIGNED(aggbase, 10890 sizeof (uint64_t))); 10891 } 10892 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 10893 rec->dtrd_offset = curneeded; 10894 curneeded += rec->dtrd_size; 10895 } else { 10896 /* tuples must be followed by an aggregation */ 10897 ASSERT(act->dta_prev == NULL || 10898 !act->dta_prev->dta_intuple); 10899 10900 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 10901 rec->dtrd_alignment); 10902 rec->dtrd_offset = ecb->dte_size; 10903 ecb->dte_size += rec->dtrd_size; 10904 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 10905 } 10906 } 10907 10908 if ((act = ecb->dte_action) != NULL && 10909 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 10910 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 10911 /* 10912 * If the size is still sizeof (dtrace_rechdr_t), then all 10913 * actions store no data; set the size to 0. 10914 */ 10915 ecb->dte_size = 0; 10916 } 10917 10918 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 10919 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 10920 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 10921 ecb->dte_needed); 10922} 10923 10924static dtrace_action_t * 10925dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 10926{ 10927 dtrace_aggregation_t *agg; 10928 size_t size = sizeof (uint64_t); 10929 int ntuple = desc->dtad_ntuple; 10930 dtrace_action_t *act; 10931 dtrace_recdesc_t *frec; 10932 dtrace_aggid_t aggid; 10933 dtrace_state_t *state = ecb->dte_state; 10934 10935 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 10936 agg->dtag_ecb = ecb; 10937 10938 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 10939 10940 switch (desc->dtad_kind) { 10941 case DTRACEAGG_MIN: 10942 agg->dtag_initial = INT64_MAX; 10943 agg->dtag_aggregate = dtrace_aggregate_min; 10944 break; 10945 10946 case DTRACEAGG_MAX: 10947 agg->dtag_initial = INT64_MIN; 10948 agg->dtag_aggregate = dtrace_aggregate_max; 10949 break; 10950 10951 case DTRACEAGG_COUNT: 10952 agg->dtag_aggregate = dtrace_aggregate_count; 10953 break; 10954 10955 case DTRACEAGG_QUANTIZE: 10956 agg->dtag_aggregate = dtrace_aggregate_quantize; 10957 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 10958 sizeof (uint64_t); 10959 break; 10960 10961 case DTRACEAGG_LQUANTIZE: { 10962 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 10963 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 10964 10965 agg->dtag_initial = desc->dtad_arg; 10966 agg->dtag_aggregate = dtrace_aggregate_lquantize; 10967 10968 if (step == 0 || levels == 0) 10969 goto err; 10970 10971 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 10972 break; 10973 } 10974 10975 case DTRACEAGG_LLQUANTIZE: { 10976 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 10977 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 10978 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 10979 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 10980 int64_t v; 10981 10982 agg->dtag_initial = desc->dtad_arg; 10983 agg->dtag_aggregate = dtrace_aggregate_llquantize; 10984 10985 if (factor < 2 || low >= high || nsteps < factor) 10986 goto err; 10987 10988 /* 10989 * Now check that the number of steps evenly divides a power 10990 * of the factor. (This assures both integer bucket size and 10991 * linearity within each magnitude.) 10992 */ 10993 for (v = factor; v < nsteps; v *= factor) 10994 continue; 10995 10996 if ((v % nsteps) || (nsteps % factor)) 10997 goto err; 10998 10999 size = (dtrace_aggregate_llquantize_bucket(factor, 11000 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 11001 break; 11002 } 11003 11004 case DTRACEAGG_AVG: 11005 agg->dtag_aggregate = dtrace_aggregate_avg; 11006 size = sizeof (uint64_t) * 2; 11007 break; 11008 11009 case DTRACEAGG_STDDEV: 11010 agg->dtag_aggregate = dtrace_aggregate_stddev; 11011 size = sizeof (uint64_t) * 4; 11012 break; 11013 11014 case DTRACEAGG_SUM: 11015 agg->dtag_aggregate = dtrace_aggregate_sum; 11016 break; 11017 11018 default: 11019 goto err; 11020 } 11021 11022 agg->dtag_action.dta_rec.dtrd_size = size; 11023 11024 if (ntuple == 0) 11025 goto err; 11026 11027 /* 11028 * We must make sure that we have enough actions for the n-tuple. 11029 */ 11030 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 11031 if (DTRACEACT_ISAGG(act->dta_kind)) 11032 break; 11033 11034 if (--ntuple == 0) { 11035 /* 11036 * This is the action with which our n-tuple begins. 11037 */ 11038 agg->dtag_first = act; 11039 goto success; 11040 } 11041 } 11042 11043 /* 11044 * This n-tuple is short by ntuple elements. Return failure. 11045 */ 11046 ASSERT(ntuple != 0); 11047err: 11048 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11049 return (NULL); 11050 11051success: 11052 /* 11053 * If the last action in the tuple has a size of zero, it's actually 11054 * an expression argument for the aggregating action. 11055 */ 11056 ASSERT(ecb->dte_action_last != NULL); 11057 act = ecb->dte_action_last; 11058 11059 if (act->dta_kind == DTRACEACT_DIFEXPR) { 11060 ASSERT(act->dta_difo != NULL); 11061 11062 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 11063 agg->dtag_hasarg = 1; 11064 } 11065 11066 /* 11067 * We need to allocate an id for this aggregation. 11068 */ 11069#if defined(sun) 11070 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 11071 VM_BESTFIT | VM_SLEEP); 11072#else 11073 aggid = alloc_unr(state->dts_aggid_arena); 11074#endif 11075 11076 if (aggid - 1 >= state->dts_naggregations) { 11077 dtrace_aggregation_t **oaggs = state->dts_aggregations; 11078 dtrace_aggregation_t **aggs; 11079 int naggs = state->dts_naggregations << 1; 11080 int onaggs = state->dts_naggregations; 11081 11082 ASSERT(aggid == state->dts_naggregations + 1); 11083 11084 if (naggs == 0) { 11085 ASSERT(oaggs == NULL); 11086 naggs = 1; 11087 } 11088 11089 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 11090 11091 if (oaggs != NULL) { 11092 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 11093 kmem_free(oaggs, onaggs * sizeof (*aggs)); 11094 } 11095 11096 state->dts_aggregations = aggs; 11097 state->dts_naggregations = naggs; 11098 } 11099 11100 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 11101 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 11102 11103 frec = &agg->dtag_first->dta_rec; 11104 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 11105 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 11106 11107 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 11108 ASSERT(!act->dta_intuple); 11109 act->dta_intuple = 1; 11110 } 11111 11112 return (&agg->dtag_action); 11113} 11114 11115static void 11116dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 11117{ 11118 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11119 dtrace_state_t *state = ecb->dte_state; 11120 dtrace_aggid_t aggid = agg->dtag_id; 11121 11122 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 11123#if defined(sun) 11124 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 11125#else 11126 free_unr(state->dts_aggid_arena, aggid); 11127#endif 11128 11129 ASSERT(state->dts_aggregations[aggid - 1] == agg); 11130 state->dts_aggregations[aggid - 1] = NULL; 11131 11132 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11133} 11134 11135static int 11136dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11137{ 11138 dtrace_action_t *action, *last; 11139 dtrace_difo_t *dp = desc->dtad_difo; 11140 uint32_t size = 0, align = sizeof (uint8_t), mask; 11141 uint16_t format = 0; 11142 dtrace_recdesc_t *rec; 11143 dtrace_state_t *state = ecb->dte_state; 11144 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 11145 uint64_t arg = desc->dtad_arg; 11146 11147 ASSERT(MUTEX_HELD(&dtrace_lock)); 11148 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 11149 11150 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 11151 /* 11152 * If this is an aggregating action, there must be neither 11153 * a speculate nor a commit on the action chain. 11154 */ 11155 dtrace_action_t *act; 11156 11157 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11158 if (act->dta_kind == DTRACEACT_COMMIT) 11159 return (EINVAL); 11160 11161 if (act->dta_kind == DTRACEACT_SPECULATE) 11162 return (EINVAL); 11163 } 11164 11165 action = dtrace_ecb_aggregation_create(ecb, desc); 11166 11167 if (action == NULL) 11168 return (EINVAL); 11169 } else { 11170 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 11171 (desc->dtad_kind == DTRACEACT_DIFEXPR && 11172 dp != NULL && dp->dtdo_destructive)) { 11173 state->dts_destructive = 1; 11174 } 11175 11176 switch (desc->dtad_kind) { 11177 case DTRACEACT_PRINTF: 11178 case DTRACEACT_PRINTA: 11179 case DTRACEACT_SYSTEM: 11180 case DTRACEACT_FREOPEN: 11181 case DTRACEACT_DIFEXPR: 11182 /* 11183 * We know that our arg is a string -- turn it into a 11184 * format. 11185 */ 11186 if (arg == 0) { 11187 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 11188 desc->dtad_kind == DTRACEACT_DIFEXPR); 11189 format = 0; 11190 } else { 11191 ASSERT(arg != 0); 11192#if defined(sun) 11193 ASSERT(arg > KERNELBASE); 11194#endif 11195 format = dtrace_format_add(state, 11196 (char *)(uintptr_t)arg); 11197 } 11198 11199 /*FALLTHROUGH*/ 11200 case DTRACEACT_LIBACT: 11201 case DTRACEACT_TRACEMEM: 11202 case DTRACEACT_TRACEMEM_DYNSIZE: 11203 if (dp == NULL) 11204 return (EINVAL); 11205 11206 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 11207 break; 11208 11209 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 11210 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11211 return (EINVAL); 11212 11213 size = opt[DTRACEOPT_STRSIZE]; 11214 } 11215 11216 break; 11217 11218 case DTRACEACT_STACK: 11219 if ((nframes = arg) == 0) { 11220 nframes = opt[DTRACEOPT_STACKFRAMES]; 11221 ASSERT(nframes > 0); 11222 arg = nframes; 11223 } 11224 11225 size = nframes * sizeof (pc_t); 11226 break; 11227 11228 case DTRACEACT_JSTACK: 11229 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 11230 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 11231 11232 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 11233 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 11234 11235 arg = DTRACE_USTACK_ARG(nframes, strsize); 11236 11237 /*FALLTHROUGH*/ 11238 case DTRACEACT_USTACK: 11239 if (desc->dtad_kind != DTRACEACT_JSTACK && 11240 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 11241 strsize = DTRACE_USTACK_STRSIZE(arg); 11242 nframes = opt[DTRACEOPT_USTACKFRAMES]; 11243 ASSERT(nframes > 0); 11244 arg = DTRACE_USTACK_ARG(nframes, strsize); 11245 } 11246 11247 /* 11248 * Save a slot for the pid. 11249 */ 11250 size = (nframes + 1) * sizeof (uint64_t); 11251 size += DTRACE_USTACK_STRSIZE(arg); 11252 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 11253 11254 break; 11255 11256 case DTRACEACT_SYM: 11257 case DTRACEACT_MOD: 11258 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 11259 sizeof (uint64_t)) || 11260 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11261 return (EINVAL); 11262 break; 11263 11264 case DTRACEACT_USYM: 11265 case DTRACEACT_UMOD: 11266 case DTRACEACT_UADDR: 11267 if (dp == NULL || 11268 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 11269 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11270 return (EINVAL); 11271 11272 /* 11273 * We have a slot for the pid, plus a slot for the 11274 * argument. To keep things simple (aligned with 11275 * bitness-neutral sizing), we store each as a 64-bit 11276 * quantity. 11277 */ 11278 size = 2 * sizeof (uint64_t); 11279 break; 11280 11281 case DTRACEACT_STOP: 11282 case DTRACEACT_BREAKPOINT: 11283 case DTRACEACT_PANIC: 11284 break; 11285 11286 case DTRACEACT_CHILL: 11287 case DTRACEACT_DISCARD: 11288 case DTRACEACT_RAISE: 11289 if (dp == NULL) 11290 return (EINVAL); 11291 break; 11292 11293 case DTRACEACT_EXIT: 11294 if (dp == NULL || 11295 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 11296 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11297 return (EINVAL); 11298 break; 11299 11300 case DTRACEACT_SPECULATE: 11301 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 11302 return (EINVAL); 11303 11304 if (dp == NULL) 11305 return (EINVAL); 11306 11307 state->dts_speculates = 1; 11308 break; 11309 11310 case DTRACEACT_PRINTM: 11311 size = dp->dtdo_rtype.dtdt_size; 11312 break; 11313 11314 case DTRACEACT_PRINTT: 11315 size = dp->dtdo_rtype.dtdt_size; 11316 break; 11317 11318 case DTRACEACT_COMMIT: { 11319 dtrace_action_t *act = ecb->dte_action; 11320 11321 for (; act != NULL; act = act->dta_next) { 11322 if (act->dta_kind == DTRACEACT_COMMIT) 11323 return (EINVAL); 11324 } 11325 11326 if (dp == NULL) 11327 return (EINVAL); 11328 break; 11329 } 11330 11331 default: 11332 return (EINVAL); 11333 } 11334 11335 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 11336 /* 11337 * If this is a data-storing action or a speculate, 11338 * we must be sure that there isn't a commit on the 11339 * action chain. 11340 */ 11341 dtrace_action_t *act = ecb->dte_action; 11342 11343 for (; act != NULL; act = act->dta_next) { 11344 if (act->dta_kind == DTRACEACT_COMMIT) 11345 return (EINVAL); 11346 } 11347 } 11348 11349 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 11350 action->dta_rec.dtrd_size = size; 11351 } 11352 11353 action->dta_refcnt = 1; 11354 rec = &action->dta_rec; 11355 size = rec->dtrd_size; 11356 11357 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 11358 if (!(size & mask)) { 11359 align = mask + 1; 11360 break; 11361 } 11362 } 11363 11364 action->dta_kind = desc->dtad_kind; 11365 11366 if ((action->dta_difo = dp) != NULL) 11367 dtrace_difo_hold(dp); 11368 11369 rec->dtrd_action = action->dta_kind; 11370 rec->dtrd_arg = arg; 11371 rec->dtrd_uarg = desc->dtad_uarg; 11372 rec->dtrd_alignment = (uint16_t)align; 11373 rec->dtrd_format = format; 11374 11375 if ((last = ecb->dte_action_last) != NULL) { 11376 ASSERT(ecb->dte_action != NULL); 11377 action->dta_prev = last; 11378 last->dta_next = action; 11379 } else { 11380 ASSERT(ecb->dte_action == NULL); 11381 ecb->dte_action = action; 11382 } 11383 11384 ecb->dte_action_last = action; 11385 11386 return (0); 11387} 11388 11389static void 11390dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 11391{ 11392 dtrace_action_t *act = ecb->dte_action, *next; 11393 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 11394 dtrace_difo_t *dp; 11395 uint16_t format; 11396 11397 if (act != NULL && act->dta_refcnt > 1) { 11398 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 11399 act->dta_refcnt--; 11400 } else { 11401 for (; act != NULL; act = next) { 11402 next = act->dta_next; 11403 ASSERT(next != NULL || act == ecb->dte_action_last); 11404 ASSERT(act->dta_refcnt == 1); 11405 11406 if ((format = act->dta_rec.dtrd_format) != 0) 11407 dtrace_format_remove(ecb->dte_state, format); 11408 11409 if ((dp = act->dta_difo) != NULL) 11410 dtrace_difo_release(dp, vstate); 11411 11412 if (DTRACEACT_ISAGG(act->dta_kind)) { 11413 dtrace_ecb_aggregation_destroy(ecb, act); 11414 } else { 11415 kmem_free(act, sizeof (dtrace_action_t)); 11416 } 11417 } 11418 } 11419 11420 ecb->dte_action = NULL; 11421 ecb->dte_action_last = NULL; 11422 ecb->dte_size = 0; 11423} 11424 11425static void 11426dtrace_ecb_disable(dtrace_ecb_t *ecb) 11427{ 11428 /* 11429 * We disable the ECB by removing it from its probe. 11430 */ 11431 dtrace_ecb_t *pecb, *prev = NULL; 11432 dtrace_probe_t *probe = ecb->dte_probe; 11433 11434 ASSERT(MUTEX_HELD(&dtrace_lock)); 11435 11436 if (probe == NULL) { 11437 /* 11438 * This is the NULL probe; there is nothing to disable. 11439 */ 11440 return; 11441 } 11442 11443 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 11444 if (pecb == ecb) 11445 break; 11446 prev = pecb; 11447 } 11448 11449 ASSERT(pecb != NULL); 11450 11451 if (prev == NULL) { 11452 probe->dtpr_ecb = ecb->dte_next; 11453 } else { 11454 prev->dte_next = ecb->dte_next; 11455 } 11456 11457 if (ecb == probe->dtpr_ecb_last) { 11458 ASSERT(ecb->dte_next == NULL); 11459 probe->dtpr_ecb_last = prev; 11460 } 11461 11462 /* 11463 * The ECB has been disconnected from the probe; now sync to assure 11464 * that all CPUs have seen the change before returning. 11465 */ 11466 dtrace_sync(); 11467 11468 if (probe->dtpr_ecb == NULL) { 11469 /* 11470 * That was the last ECB on the probe; clear the predicate 11471 * cache ID for the probe, disable it and sync one more time 11472 * to assure that we'll never hit it again. 11473 */ 11474 dtrace_provider_t *prov = probe->dtpr_provider; 11475 11476 ASSERT(ecb->dte_next == NULL); 11477 ASSERT(probe->dtpr_ecb_last == NULL); 11478 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 11479 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 11480 probe->dtpr_id, probe->dtpr_arg); 11481 dtrace_sync(); 11482 } else { 11483 /* 11484 * There is at least one ECB remaining on the probe. If there 11485 * is _exactly_ one, set the probe's predicate cache ID to be 11486 * the predicate cache ID of the remaining ECB. 11487 */ 11488 ASSERT(probe->dtpr_ecb_last != NULL); 11489 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 11490 11491 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 11492 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 11493 11494 ASSERT(probe->dtpr_ecb->dte_next == NULL); 11495 11496 if (p != NULL) 11497 probe->dtpr_predcache = p->dtp_cacheid; 11498 } 11499 11500 ecb->dte_next = NULL; 11501 } 11502} 11503 11504static void 11505dtrace_ecb_destroy(dtrace_ecb_t *ecb) 11506{ 11507 dtrace_state_t *state = ecb->dte_state; 11508 dtrace_vstate_t *vstate = &state->dts_vstate; 11509 dtrace_predicate_t *pred; 11510 dtrace_epid_t epid = ecb->dte_epid; 11511 11512 ASSERT(MUTEX_HELD(&dtrace_lock)); 11513 ASSERT(ecb->dte_next == NULL); 11514 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 11515 11516 if ((pred = ecb->dte_predicate) != NULL) 11517 dtrace_predicate_release(pred, vstate); 11518 11519 dtrace_ecb_action_remove(ecb); 11520 11521 ASSERT(state->dts_ecbs[epid - 1] == ecb); 11522 state->dts_ecbs[epid - 1] = NULL; 11523 11524 kmem_free(ecb, sizeof (dtrace_ecb_t)); 11525} 11526 11527static dtrace_ecb_t * 11528dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 11529 dtrace_enabling_t *enab) 11530{ 11531 dtrace_ecb_t *ecb; 11532 dtrace_predicate_t *pred; 11533 dtrace_actdesc_t *act; 11534 dtrace_provider_t *prov; 11535 dtrace_ecbdesc_t *desc = enab->dten_current; 11536 11537 ASSERT(MUTEX_HELD(&dtrace_lock)); 11538 ASSERT(state != NULL); 11539 11540 ecb = dtrace_ecb_add(state, probe); 11541 ecb->dte_uarg = desc->dted_uarg; 11542 11543 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 11544 dtrace_predicate_hold(pred); 11545 ecb->dte_predicate = pred; 11546 } 11547 11548 if (probe != NULL) { 11549 /* 11550 * If the provider shows more leg than the consumer is old 11551 * enough to see, we need to enable the appropriate implicit 11552 * predicate bits to prevent the ecb from activating at 11553 * revealing times. 11554 * 11555 * Providers specifying DTRACE_PRIV_USER at register time 11556 * are stating that they need the /proc-style privilege 11557 * model to be enforced, and this is what DTRACE_COND_OWNER 11558 * and DTRACE_COND_ZONEOWNER will then do at probe time. 11559 */ 11560 prov = probe->dtpr_provider; 11561 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 11562 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11563 ecb->dte_cond |= DTRACE_COND_OWNER; 11564 11565 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 11566 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11567 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 11568 11569 /* 11570 * If the provider shows us kernel innards and the user 11571 * is lacking sufficient privilege, enable the 11572 * DTRACE_COND_USERMODE implicit predicate. 11573 */ 11574 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 11575 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 11576 ecb->dte_cond |= DTRACE_COND_USERMODE; 11577 } 11578 11579 if (dtrace_ecb_create_cache != NULL) { 11580 /* 11581 * If we have a cached ecb, we'll use its action list instead 11582 * of creating our own (saving both time and space). 11583 */ 11584 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 11585 dtrace_action_t *act = cached->dte_action; 11586 11587 if (act != NULL) { 11588 ASSERT(act->dta_refcnt > 0); 11589 act->dta_refcnt++; 11590 ecb->dte_action = act; 11591 ecb->dte_action_last = cached->dte_action_last; 11592 ecb->dte_needed = cached->dte_needed; 11593 ecb->dte_size = cached->dte_size; 11594 ecb->dte_alignment = cached->dte_alignment; 11595 } 11596 11597 return (ecb); 11598 } 11599 11600 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 11601 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 11602 dtrace_ecb_destroy(ecb); 11603 return (NULL); 11604 } 11605 } 11606 11607 dtrace_ecb_resize(ecb); 11608 11609 return (dtrace_ecb_create_cache = ecb); 11610} 11611 11612static int 11613dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 11614{ 11615 dtrace_ecb_t *ecb; 11616 dtrace_enabling_t *enab = arg; 11617 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 11618 11619 ASSERT(state != NULL); 11620 11621 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 11622 /* 11623 * This probe was created in a generation for which this 11624 * enabling has previously created ECBs; we don't want to 11625 * enable it again, so just kick out. 11626 */ 11627 return (DTRACE_MATCH_NEXT); 11628 } 11629 11630 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 11631 return (DTRACE_MATCH_DONE); 11632 11633 dtrace_ecb_enable(ecb); 11634 return (DTRACE_MATCH_NEXT); 11635} 11636 11637static dtrace_ecb_t * 11638dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 11639{ 11640 dtrace_ecb_t *ecb; 11641 11642 ASSERT(MUTEX_HELD(&dtrace_lock)); 11643 11644 if (id == 0 || id > state->dts_necbs) 11645 return (NULL); 11646 11647 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 11648 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 11649 11650 return (state->dts_ecbs[id - 1]); 11651} 11652 11653static dtrace_aggregation_t * 11654dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 11655{ 11656 dtrace_aggregation_t *agg; 11657 11658 ASSERT(MUTEX_HELD(&dtrace_lock)); 11659 11660 if (id == 0 || id > state->dts_naggregations) 11661 return (NULL); 11662 11663 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 11664 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 11665 agg->dtag_id == id); 11666 11667 return (state->dts_aggregations[id - 1]); 11668} 11669 11670/* 11671 * DTrace Buffer Functions 11672 * 11673 * The following functions manipulate DTrace buffers. Most of these functions 11674 * are called in the context of establishing or processing consumer state; 11675 * exceptions are explicitly noted. 11676 */ 11677 11678/* 11679 * Note: called from cross call context. This function switches the two 11680 * buffers on a given CPU. The atomicity of this operation is assured by 11681 * disabling interrupts while the actual switch takes place; the disabling of 11682 * interrupts serializes the execution with any execution of dtrace_probe() on 11683 * the same CPU. 11684 */ 11685static void 11686dtrace_buffer_switch(dtrace_buffer_t *buf) 11687{ 11688 caddr_t tomax = buf->dtb_tomax; 11689 caddr_t xamot = buf->dtb_xamot; 11690 dtrace_icookie_t cookie; 11691 hrtime_t now; 11692 11693 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11694 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 11695 11696 cookie = dtrace_interrupt_disable(); 11697 now = dtrace_gethrtime(); 11698 buf->dtb_tomax = xamot; 11699 buf->dtb_xamot = tomax; 11700 buf->dtb_xamot_drops = buf->dtb_drops; 11701 buf->dtb_xamot_offset = buf->dtb_offset; 11702 buf->dtb_xamot_errors = buf->dtb_errors; 11703 buf->dtb_xamot_flags = buf->dtb_flags; 11704 buf->dtb_offset = 0; 11705 buf->dtb_drops = 0; 11706 buf->dtb_errors = 0; 11707 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 11708 buf->dtb_interval = now - buf->dtb_switched; 11709 buf->dtb_switched = now; 11710 dtrace_interrupt_enable(cookie); 11711} 11712 11713/* 11714 * Note: called from cross call context. This function activates a buffer 11715 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 11716 * is guaranteed by the disabling of interrupts. 11717 */ 11718static void 11719dtrace_buffer_activate(dtrace_state_t *state) 11720{ 11721 dtrace_buffer_t *buf; 11722 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 11723 11724 buf = &state->dts_buffer[curcpu]; 11725 11726 if (buf->dtb_tomax != NULL) { 11727 /* 11728 * We might like to assert that the buffer is marked inactive, 11729 * but this isn't necessarily true: the buffer for the CPU 11730 * that processes the BEGIN probe has its buffer activated 11731 * manually. In this case, we take the (harmless) action 11732 * re-clearing the bit INACTIVE bit. 11733 */ 11734 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 11735 } 11736 11737 dtrace_interrupt_enable(cookie); 11738} 11739 11740static int 11741dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 11742 processorid_t cpu, int *factor) 11743{ 11744#if defined(sun) 11745 cpu_t *cp; 11746#endif 11747 dtrace_buffer_t *buf; 11748 int allocated = 0, desired = 0; 11749 11750#if defined(sun) 11751 ASSERT(MUTEX_HELD(&cpu_lock)); 11752 ASSERT(MUTEX_HELD(&dtrace_lock)); 11753 11754 *factor = 1; 11755 11756 if (size > dtrace_nonroot_maxsize && 11757 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 11758 return (EFBIG); 11759 11760 cp = cpu_list; 11761 11762 do { 11763 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 11764 continue; 11765 11766 buf = &bufs[cp->cpu_id]; 11767 11768 /* 11769 * If there is already a buffer allocated for this CPU, it 11770 * is only possible that this is a DR event. In this case, 11771 */ 11772 if (buf->dtb_tomax != NULL) { 11773 ASSERT(buf->dtb_size == size); 11774 continue; 11775 } 11776 11777 ASSERT(buf->dtb_xamot == NULL); 11778 11779 if ((buf->dtb_tomax = kmem_zalloc(size, 11780 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11781 goto err; 11782 11783 buf->dtb_size = size; 11784 buf->dtb_flags = flags; 11785 buf->dtb_offset = 0; 11786 buf->dtb_drops = 0; 11787 11788 if (flags & DTRACEBUF_NOSWITCH) 11789 continue; 11790 11791 if ((buf->dtb_xamot = kmem_zalloc(size, 11792 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11793 goto err; 11794 } while ((cp = cp->cpu_next) != cpu_list); 11795 11796 return (0); 11797 11798err: 11799 cp = cpu_list; 11800 11801 do { 11802 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 11803 continue; 11804 11805 buf = &bufs[cp->cpu_id]; 11806 desired += 2; 11807 11808 if (buf->dtb_xamot != NULL) { 11809 ASSERT(buf->dtb_tomax != NULL); 11810 ASSERT(buf->dtb_size == size); 11811 kmem_free(buf->dtb_xamot, size); 11812 allocated++; 11813 } 11814 11815 if (buf->dtb_tomax != NULL) { 11816 ASSERT(buf->dtb_size == size); 11817 kmem_free(buf->dtb_tomax, size); 11818 allocated++; 11819 } 11820 11821 buf->dtb_tomax = NULL; 11822 buf->dtb_xamot = NULL; 11823 buf->dtb_size = 0; 11824 } while ((cp = cp->cpu_next) != cpu_list); 11825#else 11826 int i; 11827 11828 *factor = 1; 11829#if defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 11830 /* 11831 * FreeBSD isn't good at limiting the amount of memory we 11832 * ask to malloc, so let's place a limit here before trying 11833 * to do something that might well end in tears at bedtime. 11834 */ 11835 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 11836 return (ENOMEM); 11837#endif 11838 11839 ASSERT(MUTEX_HELD(&dtrace_lock)); 11840 CPU_FOREACH(i) { 11841 if (cpu != DTRACE_CPUALL && cpu != i) 11842 continue; 11843 11844 buf = &bufs[i]; 11845 11846 /* 11847 * If there is already a buffer allocated for this CPU, it 11848 * is only possible that this is a DR event. In this case, 11849 * the buffer size must match our specified size. 11850 */ 11851 if (buf->dtb_tomax != NULL) { 11852 ASSERT(buf->dtb_size == size); 11853 continue; 11854 } 11855 11856 ASSERT(buf->dtb_xamot == NULL); 11857 11858 if ((buf->dtb_tomax = kmem_zalloc(size, 11859 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11860 goto err; 11861 11862 buf->dtb_size = size; 11863 buf->dtb_flags = flags; 11864 buf->dtb_offset = 0; 11865 buf->dtb_drops = 0; 11866 11867 if (flags & DTRACEBUF_NOSWITCH) 11868 continue; 11869 11870 if ((buf->dtb_xamot = kmem_zalloc(size, 11871 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11872 goto err; 11873 } 11874 11875 return (0); 11876 11877err: 11878 /* 11879 * Error allocating memory, so free the buffers that were 11880 * allocated before the failed allocation. 11881 */ 11882 CPU_FOREACH(i) { 11883 if (cpu != DTRACE_CPUALL && cpu != i) 11884 continue; 11885 11886 buf = &bufs[i]; 11887 desired += 2; 11888 11889 if (buf->dtb_xamot != NULL) { 11890 ASSERT(buf->dtb_tomax != NULL); 11891 ASSERT(buf->dtb_size == size); 11892 kmem_free(buf->dtb_xamot, size); 11893 allocated++; 11894 } 11895 11896 if (buf->dtb_tomax != NULL) { 11897 ASSERT(buf->dtb_size == size); 11898 kmem_free(buf->dtb_tomax, size); 11899 allocated++; 11900 } 11901 11902 buf->dtb_tomax = NULL; 11903 buf->dtb_xamot = NULL; 11904 buf->dtb_size = 0; 11905 11906 } 11907#endif 11908 *factor = desired / (allocated > 0 ? allocated : 1); 11909 11910 return (ENOMEM); 11911} 11912 11913/* 11914 * Note: called from probe context. This function just increments the drop 11915 * count on a buffer. It has been made a function to allow for the 11916 * possibility of understanding the source of mysterious drop counts. (A 11917 * problem for which one may be particularly disappointed that DTrace cannot 11918 * be used to understand DTrace.) 11919 */ 11920static void 11921dtrace_buffer_drop(dtrace_buffer_t *buf) 11922{ 11923 buf->dtb_drops++; 11924} 11925 11926/* 11927 * Note: called from probe context. This function is called to reserve space 11928 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 11929 * mstate. Returns the new offset in the buffer, or a negative value if an 11930 * error has occurred. 11931 */ 11932static intptr_t 11933dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 11934 dtrace_state_t *state, dtrace_mstate_t *mstate) 11935{ 11936 intptr_t offs = buf->dtb_offset, soffs; 11937 intptr_t woffs; 11938 caddr_t tomax; 11939 size_t total; 11940 11941 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 11942 return (-1); 11943 11944 if ((tomax = buf->dtb_tomax) == NULL) { 11945 dtrace_buffer_drop(buf); 11946 return (-1); 11947 } 11948 11949 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 11950 while (offs & (align - 1)) { 11951 /* 11952 * Assert that our alignment is off by a number which 11953 * is itself sizeof (uint32_t) aligned. 11954 */ 11955 ASSERT(!((align - (offs & (align - 1))) & 11956 (sizeof (uint32_t) - 1))); 11957 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 11958 offs += sizeof (uint32_t); 11959 } 11960 11961 if ((soffs = offs + needed) > buf->dtb_size) { 11962 dtrace_buffer_drop(buf); 11963 return (-1); 11964 } 11965 11966 if (mstate == NULL) 11967 return (offs); 11968 11969 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 11970 mstate->dtms_scratch_size = buf->dtb_size - soffs; 11971 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 11972 11973 return (offs); 11974 } 11975 11976 if (buf->dtb_flags & DTRACEBUF_FILL) { 11977 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 11978 (buf->dtb_flags & DTRACEBUF_FULL)) 11979 return (-1); 11980 goto out; 11981 } 11982 11983 total = needed + (offs & (align - 1)); 11984 11985 /* 11986 * For a ring buffer, life is quite a bit more complicated. Before 11987 * we can store any padding, we need to adjust our wrapping offset. 11988 * (If we've never before wrapped or we're not about to, no adjustment 11989 * is required.) 11990 */ 11991 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 11992 offs + total > buf->dtb_size) { 11993 woffs = buf->dtb_xamot_offset; 11994 11995 if (offs + total > buf->dtb_size) { 11996 /* 11997 * We can't fit in the end of the buffer. First, a 11998 * sanity check that we can fit in the buffer at all. 11999 */ 12000 if (total > buf->dtb_size) { 12001 dtrace_buffer_drop(buf); 12002 return (-1); 12003 } 12004 12005 /* 12006 * We're going to be storing at the top of the buffer, 12007 * so now we need to deal with the wrapped offset. We 12008 * only reset our wrapped offset to 0 if it is 12009 * currently greater than the current offset. If it 12010 * is less than the current offset, it is because a 12011 * previous allocation induced a wrap -- but the 12012 * allocation didn't subsequently take the space due 12013 * to an error or false predicate evaluation. In this 12014 * case, we'll just leave the wrapped offset alone: if 12015 * the wrapped offset hasn't been advanced far enough 12016 * for this allocation, it will be adjusted in the 12017 * lower loop. 12018 */ 12019 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 12020 if (woffs >= offs) 12021 woffs = 0; 12022 } else { 12023 woffs = 0; 12024 } 12025 12026 /* 12027 * Now we know that we're going to be storing to the 12028 * top of the buffer and that there is room for us 12029 * there. We need to clear the buffer from the current 12030 * offset to the end (there may be old gunk there). 12031 */ 12032 while (offs < buf->dtb_size) 12033 tomax[offs++] = 0; 12034 12035 /* 12036 * We need to set our offset to zero. And because we 12037 * are wrapping, we need to set the bit indicating as 12038 * much. We can also adjust our needed space back 12039 * down to the space required by the ECB -- we know 12040 * that the top of the buffer is aligned. 12041 */ 12042 offs = 0; 12043 total = needed; 12044 buf->dtb_flags |= DTRACEBUF_WRAPPED; 12045 } else { 12046 /* 12047 * There is room for us in the buffer, so we simply 12048 * need to check the wrapped offset. 12049 */ 12050 if (woffs < offs) { 12051 /* 12052 * The wrapped offset is less than the offset. 12053 * This can happen if we allocated buffer space 12054 * that induced a wrap, but then we didn't 12055 * subsequently take the space due to an error 12056 * or false predicate evaluation. This is 12057 * okay; we know that _this_ allocation isn't 12058 * going to induce a wrap. We still can't 12059 * reset the wrapped offset to be zero, 12060 * however: the space may have been trashed in 12061 * the previous failed probe attempt. But at 12062 * least the wrapped offset doesn't need to 12063 * be adjusted at all... 12064 */ 12065 goto out; 12066 } 12067 } 12068 12069 while (offs + total > woffs) { 12070 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 12071 size_t size; 12072 12073 if (epid == DTRACE_EPIDNONE) { 12074 size = sizeof (uint32_t); 12075 } else { 12076 ASSERT3U(epid, <=, state->dts_necbs); 12077 ASSERT(state->dts_ecbs[epid - 1] != NULL); 12078 12079 size = state->dts_ecbs[epid - 1]->dte_size; 12080 } 12081 12082 ASSERT(woffs + size <= buf->dtb_size); 12083 ASSERT(size != 0); 12084 12085 if (woffs + size == buf->dtb_size) { 12086 /* 12087 * We've reached the end of the buffer; we want 12088 * to set the wrapped offset to 0 and break 12089 * out. However, if the offs is 0, then we're 12090 * in a strange edge-condition: the amount of 12091 * space that we want to reserve plus the size 12092 * of the record that we're overwriting is 12093 * greater than the size of the buffer. This 12094 * is problematic because if we reserve the 12095 * space but subsequently don't consume it (due 12096 * to a failed predicate or error) the wrapped 12097 * offset will be 0 -- yet the EPID at offset 0 12098 * will not be committed. This situation is 12099 * relatively easy to deal with: if we're in 12100 * this case, the buffer is indistinguishable 12101 * from one that hasn't wrapped; we need only 12102 * finish the job by clearing the wrapped bit, 12103 * explicitly setting the offset to be 0, and 12104 * zero'ing out the old data in the buffer. 12105 */ 12106 if (offs == 0) { 12107 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 12108 buf->dtb_offset = 0; 12109 woffs = total; 12110 12111 while (woffs < buf->dtb_size) 12112 tomax[woffs++] = 0; 12113 } 12114 12115 woffs = 0; 12116 break; 12117 } 12118 12119 woffs += size; 12120 } 12121 12122 /* 12123 * We have a wrapped offset. It may be that the wrapped offset 12124 * has become zero -- that's okay. 12125 */ 12126 buf->dtb_xamot_offset = woffs; 12127 } 12128 12129out: 12130 /* 12131 * Now we can plow the buffer with any necessary padding. 12132 */ 12133 while (offs & (align - 1)) { 12134 /* 12135 * Assert that our alignment is off by a number which 12136 * is itself sizeof (uint32_t) aligned. 12137 */ 12138 ASSERT(!((align - (offs & (align - 1))) & 12139 (sizeof (uint32_t) - 1))); 12140 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12141 offs += sizeof (uint32_t); 12142 } 12143 12144 if (buf->dtb_flags & DTRACEBUF_FILL) { 12145 if (offs + needed > buf->dtb_size - state->dts_reserve) { 12146 buf->dtb_flags |= DTRACEBUF_FULL; 12147 return (-1); 12148 } 12149 } 12150 12151 if (mstate == NULL) 12152 return (offs); 12153 12154 /* 12155 * For ring buffers and fill buffers, the scratch space is always 12156 * the inactive buffer. 12157 */ 12158 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 12159 mstate->dtms_scratch_size = buf->dtb_size; 12160 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12161 12162 return (offs); 12163} 12164 12165static void 12166dtrace_buffer_polish(dtrace_buffer_t *buf) 12167{ 12168 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 12169 ASSERT(MUTEX_HELD(&dtrace_lock)); 12170 12171 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 12172 return; 12173 12174 /* 12175 * We need to polish the ring buffer. There are three cases: 12176 * 12177 * - The first (and presumably most common) is that there is no gap 12178 * between the buffer offset and the wrapped offset. In this case, 12179 * there is nothing in the buffer that isn't valid data; we can 12180 * mark the buffer as polished and return. 12181 * 12182 * - The second (less common than the first but still more common 12183 * than the third) is that there is a gap between the buffer offset 12184 * and the wrapped offset, and the wrapped offset is larger than the 12185 * buffer offset. This can happen because of an alignment issue, or 12186 * can happen because of a call to dtrace_buffer_reserve() that 12187 * didn't subsequently consume the buffer space. In this case, 12188 * we need to zero the data from the buffer offset to the wrapped 12189 * offset. 12190 * 12191 * - The third (and least common) is that there is a gap between the 12192 * buffer offset and the wrapped offset, but the wrapped offset is 12193 * _less_ than the buffer offset. This can only happen because a 12194 * call to dtrace_buffer_reserve() induced a wrap, but the space 12195 * was not subsequently consumed. In this case, we need to zero the 12196 * space from the offset to the end of the buffer _and_ from the 12197 * top of the buffer to the wrapped offset. 12198 */ 12199 if (buf->dtb_offset < buf->dtb_xamot_offset) { 12200 bzero(buf->dtb_tomax + buf->dtb_offset, 12201 buf->dtb_xamot_offset - buf->dtb_offset); 12202 } 12203 12204 if (buf->dtb_offset > buf->dtb_xamot_offset) { 12205 bzero(buf->dtb_tomax + buf->dtb_offset, 12206 buf->dtb_size - buf->dtb_offset); 12207 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 12208 } 12209} 12210 12211/* 12212 * This routine determines if data generated at the specified time has likely 12213 * been entirely consumed at user-level. This routine is called to determine 12214 * if an ECB on a defunct probe (but for an active enabling) can be safely 12215 * disabled and destroyed. 12216 */ 12217static int 12218dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 12219{ 12220 int i; 12221 12222 for (i = 0; i < NCPU; i++) { 12223 dtrace_buffer_t *buf = &bufs[i]; 12224 12225 if (buf->dtb_size == 0) 12226 continue; 12227 12228 if (buf->dtb_flags & DTRACEBUF_RING) 12229 return (0); 12230 12231 if (!buf->dtb_switched && buf->dtb_offset != 0) 12232 return (0); 12233 12234 if (buf->dtb_switched - buf->dtb_interval < when) 12235 return (0); 12236 } 12237 12238 return (1); 12239} 12240 12241static void 12242dtrace_buffer_free(dtrace_buffer_t *bufs) 12243{ 12244 int i; 12245 12246 for (i = 0; i < NCPU; i++) { 12247 dtrace_buffer_t *buf = &bufs[i]; 12248 12249 if (buf->dtb_tomax == NULL) { 12250 ASSERT(buf->dtb_xamot == NULL); 12251 ASSERT(buf->dtb_size == 0); 12252 continue; 12253 } 12254 12255 if (buf->dtb_xamot != NULL) { 12256 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 12257 kmem_free(buf->dtb_xamot, buf->dtb_size); 12258 } 12259 12260 kmem_free(buf->dtb_tomax, buf->dtb_size); 12261 buf->dtb_size = 0; 12262 buf->dtb_tomax = NULL; 12263 buf->dtb_xamot = NULL; 12264 } 12265} 12266 12267/* 12268 * DTrace Enabling Functions 12269 */ 12270static dtrace_enabling_t * 12271dtrace_enabling_create(dtrace_vstate_t *vstate) 12272{ 12273 dtrace_enabling_t *enab; 12274 12275 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 12276 enab->dten_vstate = vstate; 12277 12278 return (enab); 12279} 12280 12281static void 12282dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 12283{ 12284 dtrace_ecbdesc_t **ndesc; 12285 size_t osize, nsize; 12286 12287 /* 12288 * We can't add to enablings after we've enabled them, or after we've 12289 * retained them. 12290 */ 12291 ASSERT(enab->dten_probegen == 0); 12292 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12293 12294 if (enab->dten_ndesc < enab->dten_maxdesc) { 12295 enab->dten_desc[enab->dten_ndesc++] = ecb; 12296 return; 12297 } 12298 12299 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12300 12301 if (enab->dten_maxdesc == 0) { 12302 enab->dten_maxdesc = 1; 12303 } else { 12304 enab->dten_maxdesc <<= 1; 12305 } 12306 12307 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 12308 12309 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12310 ndesc = kmem_zalloc(nsize, KM_SLEEP); 12311 bcopy(enab->dten_desc, ndesc, osize); 12312 if (enab->dten_desc != NULL) 12313 kmem_free(enab->dten_desc, osize); 12314 12315 enab->dten_desc = ndesc; 12316 enab->dten_desc[enab->dten_ndesc++] = ecb; 12317} 12318 12319static void 12320dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 12321 dtrace_probedesc_t *pd) 12322{ 12323 dtrace_ecbdesc_t *new; 12324 dtrace_predicate_t *pred; 12325 dtrace_actdesc_t *act; 12326 12327 /* 12328 * We're going to create a new ECB description that matches the 12329 * specified ECB in every way, but has the specified probe description. 12330 */ 12331 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12332 12333 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 12334 dtrace_predicate_hold(pred); 12335 12336 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 12337 dtrace_actdesc_hold(act); 12338 12339 new->dted_action = ecb->dted_action; 12340 new->dted_pred = ecb->dted_pred; 12341 new->dted_probe = *pd; 12342 new->dted_uarg = ecb->dted_uarg; 12343 12344 dtrace_enabling_add(enab, new); 12345} 12346 12347static void 12348dtrace_enabling_dump(dtrace_enabling_t *enab) 12349{ 12350 int i; 12351 12352 for (i = 0; i < enab->dten_ndesc; i++) { 12353 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 12354 12355 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 12356 desc->dtpd_provider, desc->dtpd_mod, 12357 desc->dtpd_func, desc->dtpd_name); 12358 } 12359} 12360 12361static void 12362dtrace_enabling_destroy(dtrace_enabling_t *enab) 12363{ 12364 int i; 12365 dtrace_ecbdesc_t *ep; 12366 dtrace_vstate_t *vstate = enab->dten_vstate; 12367 12368 ASSERT(MUTEX_HELD(&dtrace_lock)); 12369 12370 for (i = 0; i < enab->dten_ndesc; i++) { 12371 dtrace_actdesc_t *act, *next; 12372 dtrace_predicate_t *pred; 12373 12374 ep = enab->dten_desc[i]; 12375 12376 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 12377 dtrace_predicate_release(pred, vstate); 12378 12379 for (act = ep->dted_action; act != NULL; act = next) { 12380 next = act->dtad_next; 12381 dtrace_actdesc_release(act, vstate); 12382 } 12383 12384 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12385 } 12386 12387 if (enab->dten_desc != NULL) 12388 kmem_free(enab->dten_desc, 12389 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 12390 12391 /* 12392 * If this was a retained enabling, decrement the dts_nretained count 12393 * and take it off of the dtrace_retained list. 12394 */ 12395 if (enab->dten_prev != NULL || enab->dten_next != NULL || 12396 dtrace_retained == enab) { 12397 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12398 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 12399 enab->dten_vstate->dtvs_state->dts_nretained--; 12400 dtrace_retained_gen++; 12401 } 12402 12403 if (enab->dten_prev == NULL) { 12404 if (dtrace_retained == enab) { 12405 dtrace_retained = enab->dten_next; 12406 12407 if (dtrace_retained != NULL) 12408 dtrace_retained->dten_prev = NULL; 12409 } 12410 } else { 12411 ASSERT(enab != dtrace_retained); 12412 ASSERT(dtrace_retained != NULL); 12413 enab->dten_prev->dten_next = enab->dten_next; 12414 } 12415 12416 if (enab->dten_next != NULL) { 12417 ASSERT(dtrace_retained != NULL); 12418 enab->dten_next->dten_prev = enab->dten_prev; 12419 } 12420 12421 kmem_free(enab, sizeof (dtrace_enabling_t)); 12422} 12423 12424static int 12425dtrace_enabling_retain(dtrace_enabling_t *enab) 12426{ 12427 dtrace_state_t *state; 12428 12429 ASSERT(MUTEX_HELD(&dtrace_lock)); 12430 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12431 ASSERT(enab->dten_vstate != NULL); 12432 12433 state = enab->dten_vstate->dtvs_state; 12434 ASSERT(state != NULL); 12435 12436 /* 12437 * We only allow each state to retain dtrace_retain_max enablings. 12438 */ 12439 if (state->dts_nretained >= dtrace_retain_max) 12440 return (ENOSPC); 12441 12442 state->dts_nretained++; 12443 dtrace_retained_gen++; 12444 12445 if (dtrace_retained == NULL) { 12446 dtrace_retained = enab; 12447 return (0); 12448 } 12449 12450 enab->dten_next = dtrace_retained; 12451 dtrace_retained->dten_prev = enab; 12452 dtrace_retained = enab; 12453 12454 return (0); 12455} 12456 12457static int 12458dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 12459 dtrace_probedesc_t *create) 12460{ 12461 dtrace_enabling_t *new, *enab; 12462 int found = 0, err = ENOENT; 12463 12464 ASSERT(MUTEX_HELD(&dtrace_lock)); 12465 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 12466 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 12467 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 12468 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 12469 12470 new = dtrace_enabling_create(&state->dts_vstate); 12471 12472 /* 12473 * Iterate over all retained enablings, looking for enablings that 12474 * match the specified state. 12475 */ 12476 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12477 int i; 12478 12479 /* 12480 * dtvs_state can only be NULL for helper enablings -- and 12481 * helper enablings can't be retained. 12482 */ 12483 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12484 12485 if (enab->dten_vstate->dtvs_state != state) 12486 continue; 12487 12488 /* 12489 * Now iterate over each probe description; we're looking for 12490 * an exact match to the specified probe description. 12491 */ 12492 for (i = 0; i < enab->dten_ndesc; i++) { 12493 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12494 dtrace_probedesc_t *pd = &ep->dted_probe; 12495 12496 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 12497 continue; 12498 12499 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 12500 continue; 12501 12502 if (strcmp(pd->dtpd_func, match->dtpd_func)) 12503 continue; 12504 12505 if (strcmp(pd->dtpd_name, match->dtpd_name)) 12506 continue; 12507 12508 /* 12509 * We have a winning probe! Add it to our growing 12510 * enabling. 12511 */ 12512 found = 1; 12513 dtrace_enabling_addlike(new, ep, create); 12514 } 12515 } 12516 12517 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 12518 dtrace_enabling_destroy(new); 12519 return (err); 12520 } 12521 12522 return (0); 12523} 12524 12525static void 12526dtrace_enabling_retract(dtrace_state_t *state) 12527{ 12528 dtrace_enabling_t *enab, *next; 12529 12530 ASSERT(MUTEX_HELD(&dtrace_lock)); 12531 12532 /* 12533 * Iterate over all retained enablings, destroy the enablings retained 12534 * for the specified state. 12535 */ 12536 for (enab = dtrace_retained; enab != NULL; enab = next) { 12537 next = enab->dten_next; 12538 12539 /* 12540 * dtvs_state can only be NULL for helper enablings -- and 12541 * helper enablings can't be retained. 12542 */ 12543 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12544 12545 if (enab->dten_vstate->dtvs_state == state) { 12546 ASSERT(state->dts_nretained > 0); 12547 dtrace_enabling_destroy(enab); 12548 } 12549 } 12550 12551 ASSERT(state->dts_nretained == 0); 12552} 12553 12554static int 12555dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 12556{ 12557 int i = 0; 12558 int matched = 0; 12559 12560 ASSERT(MUTEX_HELD(&cpu_lock)); 12561 ASSERT(MUTEX_HELD(&dtrace_lock)); 12562 12563 for (i = 0; i < enab->dten_ndesc; i++) { 12564 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12565 12566 enab->dten_current = ep; 12567 enab->dten_error = 0; 12568 12569 matched += dtrace_probe_enable(&ep->dted_probe, enab); 12570 12571 if (enab->dten_error != 0) { 12572 /* 12573 * If we get an error half-way through enabling the 12574 * probes, we kick out -- perhaps with some number of 12575 * them enabled. Leaving enabled probes enabled may 12576 * be slightly confusing for user-level, but we expect 12577 * that no one will attempt to actually drive on in 12578 * the face of such errors. If this is an anonymous 12579 * enabling (indicated with a NULL nmatched pointer), 12580 * we cmn_err() a message. We aren't expecting to 12581 * get such an error -- such as it can exist at all, 12582 * it would be a result of corrupted DOF in the driver 12583 * properties. 12584 */ 12585 if (nmatched == NULL) { 12586 cmn_err(CE_WARN, "dtrace_enabling_match() " 12587 "error on %p: %d", (void *)ep, 12588 enab->dten_error); 12589 } 12590 12591 return (enab->dten_error); 12592 } 12593 } 12594 12595 enab->dten_probegen = dtrace_probegen; 12596 if (nmatched != NULL) 12597 *nmatched = matched; 12598 12599 return (0); 12600} 12601 12602static void 12603dtrace_enabling_matchall(void) 12604{ 12605 dtrace_enabling_t *enab; 12606 12607 mutex_enter(&cpu_lock); 12608 mutex_enter(&dtrace_lock); 12609 12610 /* 12611 * Iterate over all retained enablings to see if any probes match 12612 * against them. We only perform this operation on enablings for which 12613 * we have sufficient permissions by virtue of being in the global zone 12614 * or in the same zone as the DTrace client. Because we can be called 12615 * after dtrace_detach() has been called, we cannot assert that there 12616 * are retained enablings. We can safely load from dtrace_retained, 12617 * however: the taskq_destroy() at the end of dtrace_detach() will 12618 * block pending our completion. 12619 */ 12620 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12621#if defined(sun) 12622 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 12623 12624 if (INGLOBALZONE(curproc) || 12625 cr != NULL && getzoneid() == crgetzoneid(cr)) 12626#endif 12627 (void) dtrace_enabling_match(enab, NULL); 12628 } 12629 12630 mutex_exit(&dtrace_lock); 12631 mutex_exit(&cpu_lock); 12632} 12633 12634/* 12635 * If an enabling is to be enabled without having matched probes (that is, if 12636 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 12637 * enabling must be _primed_ by creating an ECB for every ECB description. 12638 * This must be done to assure that we know the number of speculations, the 12639 * number of aggregations, the minimum buffer size needed, etc. before we 12640 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 12641 * enabling any probes, we create ECBs for every ECB decription, but with a 12642 * NULL probe -- which is exactly what this function does. 12643 */ 12644static void 12645dtrace_enabling_prime(dtrace_state_t *state) 12646{ 12647 dtrace_enabling_t *enab; 12648 int i; 12649 12650 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12651 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12652 12653 if (enab->dten_vstate->dtvs_state != state) 12654 continue; 12655 12656 /* 12657 * We don't want to prime an enabling more than once, lest 12658 * we allow a malicious user to induce resource exhaustion. 12659 * (The ECBs that result from priming an enabling aren't 12660 * leaked -- but they also aren't deallocated until the 12661 * consumer state is destroyed.) 12662 */ 12663 if (enab->dten_primed) 12664 continue; 12665 12666 for (i = 0; i < enab->dten_ndesc; i++) { 12667 enab->dten_current = enab->dten_desc[i]; 12668 (void) dtrace_probe_enable(NULL, enab); 12669 } 12670 12671 enab->dten_primed = 1; 12672 } 12673} 12674 12675/* 12676 * Called to indicate that probes should be provided due to retained 12677 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 12678 * must take an initial lap through the enabling calling the dtps_provide() 12679 * entry point explicitly to allow for autocreated probes. 12680 */ 12681static void 12682dtrace_enabling_provide(dtrace_provider_t *prv) 12683{ 12684 int i, all = 0; 12685 dtrace_probedesc_t desc; 12686 dtrace_genid_t gen; 12687 12688 ASSERT(MUTEX_HELD(&dtrace_lock)); 12689 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 12690 12691 if (prv == NULL) { 12692 all = 1; 12693 prv = dtrace_provider; 12694 } 12695 12696 do { 12697 dtrace_enabling_t *enab; 12698 void *parg = prv->dtpv_arg; 12699 12700retry: 12701 gen = dtrace_retained_gen; 12702 for (enab = dtrace_retained; enab != NULL; 12703 enab = enab->dten_next) { 12704 for (i = 0; i < enab->dten_ndesc; i++) { 12705 desc = enab->dten_desc[i]->dted_probe; 12706 mutex_exit(&dtrace_lock); 12707 prv->dtpv_pops.dtps_provide(parg, &desc); 12708 mutex_enter(&dtrace_lock); 12709 /* 12710 * Process the retained enablings again if 12711 * they have changed while we weren't holding 12712 * dtrace_lock. 12713 */ 12714 if (gen != dtrace_retained_gen) 12715 goto retry; 12716 } 12717 } 12718 } while (all && (prv = prv->dtpv_next) != NULL); 12719 12720 mutex_exit(&dtrace_lock); 12721 dtrace_probe_provide(NULL, all ? NULL : prv); 12722 mutex_enter(&dtrace_lock); 12723} 12724 12725/* 12726 * Called to reap ECBs that are attached to probes from defunct providers. 12727 */ 12728static void 12729dtrace_enabling_reap(void) 12730{ 12731 dtrace_provider_t *prov; 12732 dtrace_probe_t *probe; 12733 dtrace_ecb_t *ecb; 12734 hrtime_t when; 12735 int i; 12736 12737 mutex_enter(&cpu_lock); 12738 mutex_enter(&dtrace_lock); 12739 12740 for (i = 0; i < dtrace_nprobes; i++) { 12741 if ((probe = dtrace_probes[i]) == NULL) 12742 continue; 12743 12744 if (probe->dtpr_ecb == NULL) 12745 continue; 12746 12747 prov = probe->dtpr_provider; 12748 12749 if ((when = prov->dtpv_defunct) == 0) 12750 continue; 12751 12752 /* 12753 * We have ECBs on a defunct provider: we want to reap these 12754 * ECBs to allow the provider to unregister. The destruction 12755 * of these ECBs must be done carefully: if we destroy the ECB 12756 * and the consumer later wishes to consume an EPID that 12757 * corresponds to the destroyed ECB (and if the EPID metadata 12758 * has not been previously consumed), the consumer will abort 12759 * processing on the unknown EPID. To reduce (but not, sadly, 12760 * eliminate) the possibility of this, we will only destroy an 12761 * ECB for a defunct provider if, for the state that 12762 * corresponds to the ECB: 12763 * 12764 * (a) There is no speculative tracing (which can effectively 12765 * cache an EPID for an arbitrary amount of time). 12766 * 12767 * (b) The principal buffers have been switched twice since the 12768 * provider became defunct. 12769 * 12770 * (c) The aggregation buffers are of zero size or have been 12771 * switched twice since the provider became defunct. 12772 * 12773 * We use dts_speculates to determine (a) and call a function 12774 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 12775 * that as soon as we've been unable to destroy one of the ECBs 12776 * associated with the probe, we quit trying -- reaping is only 12777 * fruitful in as much as we can destroy all ECBs associated 12778 * with the defunct provider's probes. 12779 */ 12780 while ((ecb = probe->dtpr_ecb) != NULL) { 12781 dtrace_state_t *state = ecb->dte_state; 12782 dtrace_buffer_t *buf = state->dts_buffer; 12783 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 12784 12785 if (state->dts_speculates) 12786 break; 12787 12788 if (!dtrace_buffer_consumed(buf, when)) 12789 break; 12790 12791 if (!dtrace_buffer_consumed(aggbuf, when)) 12792 break; 12793 12794 dtrace_ecb_disable(ecb); 12795 ASSERT(probe->dtpr_ecb != ecb); 12796 dtrace_ecb_destroy(ecb); 12797 } 12798 } 12799 12800 mutex_exit(&dtrace_lock); 12801 mutex_exit(&cpu_lock); 12802} 12803 12804/* 12805 * DTrace DOF Functions 12806 */ 12807/*ARGSUSED*/ 12808static void 12809dtrace_dof_error(dof_hdr_t *dof, const char *str) 12810{ 12811 if (dtrace_err_verbose) 12812 cmn_err(CE_WARN, "failed to process DOF: %s", str); 12813 12814#ifdef DTRACE_ERRDEBUG 12815 dtrace_errdebug(str); 12816#endif 12817} 12818 12819/* 12820 * Create DOF out of a currently enabled state. Right now, we only create 12821 * DOF containing the run-time options -- but this could be expanded to create 12822 * complete DOF representing the enabled state. 12823 */ 12824static dof_hdr_t * 12825dtrace_dof_create(dtrace_state_t *state) 12826{ 12827 dof_hdr_t *dof; 12828 dof_sec_t *sec; 12829 dof_optdesc_t *opt; 12830 int i, len = sizeof (dof_hdr_t) + 12831 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 12832 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 12833 12834 ASSERT(MUTEX_HELD(&dtrace_lock)); 12835 12836 dof = kmem_zalloc(len, KM_SLEEP); 12837 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 12838 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 12839 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 12840 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 12841 12842 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 12843 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 12844 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 12845 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 12846 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 12847 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 12848 12849 dof->dofh_flags = 0; 12850 dof->dofh_hdrsize = sizeof (dof_hdr_t); 12851 dof->dofh_secsize = sizeof (dof_sec_t); 12852 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 12853 dof->dofh_secoff = sizeof (dof_hdr_t); 12854 dof->dofh_loadsz = len; 12855 dof->dofh_filesz = len; 12856 dof->dofh_pad = 0; 12857 12858 /* 12859 * Fill in the option section header... 12860 */ 12861 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 12862 sec->dofs_type = DOF_SECT_OPTDESC; 12863 sec->dofs_align = sizeof (uint64_t); 12864 sec->dofs_flags = DOF_SECF_LOAD; 12865 sec->dofs_entsize = sizeof (dof_optdesc_t); 12866 12867 opt = (dof_optdesc_t *)((uintptr_t)sec + 12868 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 12869 12870 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 12871 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 12872 12873 for (i = 0; i < DTRACEOPT_MAX; i++) { 12874 opt[i].dofo_option = i; 12875 opt[i].dofo_strtab = DOF_SECIDX_NONE; 12876 opt[i].dofo_value = state->dts_options[i]; 12877 } 12878 12879 return (dof); 12880} 12881 12882static dof_hdr_t * 12883dtrace_dof_copyin(uintptr_t uarg, int *errp) 12884{ 12885 dof_hdr_t hdr, *dof; 12886 12887 ASSERT(!MUTEX_HELD(&dtrace_lock)); 12888 12889 /* 12890 * First, we're going to copyin() the sizeof (dof_hdr_t). 12891 */ 12892 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 12893 dtrace_dof_error(NULL, "failed to copyin DOF header"); 12894 *errp = EFAULT; 12895 return (NULL); 12896 } 12897 12898 /* 12899 * Now we'll allocate the entire DOF and copy it in -- provided 12900 * that the length isn't outrageous. 12901 */ 12902 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 12903 dtrace_dof_error(&hdr, "load size exceeds maximum"); 12904 *errp = E2BIG; 12905 return (NULL); 12906 } 12907 12908 if (hdr.dofh_loadsz < sizeof (hdr)) { 12909 dtrace_dof_error(&hdr, "invalid load size"); 12910 *errp = EINVAL; 12911 return (NULL); 12912 } 12913 12914 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 12915 12916 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 12917 dof->dofh_loadsz != hdr.dofh_loadsz) { 12918 kmem_free(dof, hdr.dofh_loadsz); 12919 *errp = EFAULT; 12920 return (NULL); 12921 } 12922 12923 return (dof); 12924} 12925 12926#if !defined(sun) 12927static __inline uchar_t 12928dtrace_dof_char(char c) { 12929 switch (c) { 12930 case '0': 12931 case '1': 12932 case '2': 12933 case '3': 12934 case '4': 12935 case '5': 12936 case '6': 12937 case '7': 12938 case '8': 12939 case '9': 12940 return (c - '0'); 12941 case 'A': 12942 case 'B': 12943 case 'C': 12944 case 'D': 12945 case 'E': 12946 case 'F': 12947 return (c - 'A' + 10); 12948 case 'a': 12949 case 'b': 12950 case 'c': 12951 case 'd': 12952 case 'e': 12953 case 'f': 12954 return (c - 'a' + 10); 12955 } 12956 /* Should not reach here. */ 12957 return (0); 12958} 12959#endif 12960 12961static dof_hdr_t * 12962dtrace_dof_property(const char *name) 12963{ 12964 uchar_t *buf; 12965 uint64_t loadsz; 12966 unsigned int len, i; 12967 dof_hdr_t *dof; 12968 12969#if defined(sun) 12970 /* 12971 * Unfortunately, array of values in .conf files are always (and 12972 * only) interpreted to be integer arrays. We must read our DOF 12973 * as an integer array, and then squeeze it into a byte array. 12974 */ 12975 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 12976 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 12977 return (NULL); 12978 12979 for (i = 0; i < len; i++) 12980 buf[i] = (uchar_t)(((int *)buf)[i]); 12981 12982 if (len < sizeof (dof_hdr_t)) { 12983 ddi_prop_free(buf); 12984 dtrace_dof_error(NULL, "truncated header"); 12985 return (NULL); 12986 } 12987 12988 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 12989 ddi_prop_free(buf); 12990 dtrace_dof_error(NULL, "truncated DOF"); 12991 return (NULL); 12992 } 12993 12994 if (loadsz >= dtrace_dof_maxsize) { 12995 ddi_prop_free(buf); 12996 dtrace_dof_error(NULL, "oversized DOF"); 12997 return (NULL); 12998 } 12999 13000 dof = kmem_alloc(loadsz, KM_SLEEP); 13001 bcopy(buf, dof, loadsz); 13002 ddi_prop_free(buf); 13003#else 13004 char *p; 13005 char *p_env; 13006 13007 if ((p_env = getenv(name)) == NULL) 13008 return (NULL); 13009 13010 len = strlen(p_env) / 2; 13011 13012 buf = kmem_alloc(len, KM_SLEEP); 13013 13014 dof = (dof_hdr_t *) buf; 13015 13016 p = p_env; 13017 13018 for (i = 0; i < len; i++) { 13019 buf[i] = (dtrace_dof_char(p[0]) << 4) | 13020 dtrace_dof_char(p[1]); 13021 p += 2; 13022 } 13023 13024 freeenv(p_env); 13025 13026 if (len < sizeof (dof_hdr_t)) { 13027 kmem_free(buf, 0); 13028 dtrace_dof_error(NULL, "truncated header"); 13029 return (NULL); 13030 } 13031 13032 if (len < (loadsz = dof->dofh_loadsz)) { 13033 kmem_free(buf, 0); 13034 dtrace_dof_error(NULL, "truncated DOF"); 13035 return (NULL); 13036 } 13037 13038 if (loadsz >= dtrace_dof_maxsize) { 13039 kmem_free(buf, 0); 13040 dtrace_dof_error(NULL, "oversized DOF"); 13041 return (NULL); 13042 } 13043#endif 13044 13045 return (dof); 13046} 13047 13048static void 13049dtrace_dof_destroy(dof_hdr_t *dof) 13050{ 13051 kmem_free(dof, dof->dofh_loadsz); 13052} 13053 13054/* 13055 * Return the dof_sec_t pointer corresponding to a given section index. If the 13056 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 13057 * a type other than DOF_SECT_NONE is specified, the header is checked against 13058 * this type and NULL is returned if the types do not match. 13059 */ 13060static dof_sec_t * 13061dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 13062{ 13063 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 13064 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 13065 13066 if (i >= dof->dofh_secnum) { 13067 dtrace_dof_error(dof, "referenced section index is invalid"); 13068 return (NULL); 13069 } 13070 13071 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 13072 dtrace_dof_error(dof, "referenced section is not loadable"); 13073 return (NULL); 13074 } 13075 13076 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 13077 dtrace_dof_error(dof, "referenced section is the wrong type"); 13078 return (NULL); 13079 } 13080 13081 return (sec); 13082} 13083 13084static dtrace_probedesc_t * 13085dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 13086{ 13087 dof_probedesc_t *probe; 13088 dof_sec_t *strtab; 13089 uintptr_t daddr = (uintptr_t)dof; 13090 uintptr_t str; 13091 size_t size; 13092 13093 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 13094 dtrace_dof_error(dof, "invalid probe section"); 13095 return (NULL); 13096 } 13097 13098 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13099 dtrace_dof_error(dof, "bad alignment in probe description"); 13100 return (NULL); 13101 } 13102 13103 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 13104 dtrace_dof_error(dof, "truncated probe description"); 13105 return (NULL); 13106 } 13107 13108 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 13109 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 13110 13111 if (strtab == NULL) 13112 return (NULL); 13113 13114 str = daddr + strtab->dofs_offset; 13115 size = strtab->dofs_size; 13116 13117 if (probe->dofp_provider >= strtab->dofs_size) { 13118 dtrace_dof_error(dof, "corrupt probe provider"); 13119 return (NULL); 13120 } 13121 13122 (void) strncpy(desc->dtpd_provider, 13123 (char *)(str + probe->dofp_provider), 13124 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 13125 13126 if (probe->dofp_mod >= strtab->dofs_size) { 13127 dtrace_dof_error(dof, "corrupt probe module"); 13128 return (NULL); 13129 } 13130 13131 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 13132 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 13133 13134 if (probe->dofp_func >= strtab->dofs_size) { 13135 dtrace_dof_error(dof, "corrupt probe function"); 13136 return (NULL); 13137 } 13138 13139 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 13140 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 13141 13142 if (probe->dofp_name >= strtab->dofs_size) { 13143 dtrace_dof_error(dof, "corrupt probe name"); 13144 return (NULL); 13145 } 13146 13147 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 13148 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 13149 13150 return (desc); 13151} 13152 13153static dtrace_difo_t * 13154dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13155 cred_t *cr) 13156{ 13157 dtrace_difo_t *dp; 13158 size_t ttl = 0; 13159 dof_difohdr_t *dofd; 13160 uintptr_t daddr = (uintptr_t)dof; 13161 size_t max = dtrace_difo_maxsize; 13162 int i, l, n; 13163 13164 static const struct { 13165 int section; 13166 int bufoffs; 13167 int lenoffs; 13168 int entsize; 13169 int align; 13170 const char *msg; 13171 } difo[] = { 13172 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 13173 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 13174 sizeof (dif_instr_t), "multiple DIF sections" }, 13175 13176 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 13177 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 13178 sizeof (uint64_t), "multiple integer tables" }, 13179 13180 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 13181 offsetof(dtrace_difo_t, dtdo_strlen), 0, 13182 sizeof (char), "multiple string tables" }, 13183 13184 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 13185 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 13186 sizeof (uint_t), "multiple variable tables" }, 13187 13188 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 13189 }; 13190 13191 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 13192 dtrace_dof_error(dof, "invalid DIFO header section"); 13193 return (NULL); 13194 } 13195 13196 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13197 dtrace_dof_error(dof, "bad alignment in DIFO header"); 13198 return (NULL); 13199 } 13200 13201 if (sec->dofs_size < sizeof (dof_difohdr_t) || 13202 sec->dofs_size % sizeof (dof_secidx_t)) { 13203 dtrace_dof_error(dof, "bad size in DIFO header"); 13204 return (NULL); 13205 } 13206 13207 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13208 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 13209 13210 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 13211 dp->dtdo_rtype = dofd->dofd_rtype; 13212 13213 for (l = 0; l < n; l++) { 13214 dof_sec_t *subsec; 13215 void **bufp; 13216 uint32_t *lenp; 13217 13218 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 13219 dofd->dofd_links[l])) == NULL) 13220 goto err; /* invalid section link */ 13221 13222 if (ttl + subsec->dofs_size > max) { 13223 dtrace_dof_error(dof, "exceeds maximum size"); 13224 goto err; 13225 } 13226 13227 ttl += subsec->dofs_size; 13228 13229 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 13230 if (subsec->dofs_type != difo[i].section) 13231 continue; 13232 13233 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 13234 dtrace_dof_error(dof, "section not loaded"); 13235 goto err; 13236 } 13237 13238 if (subsec->dofs_align != difo[i].align) { 13239 dtrace_dof_error(dof, "bad alignment"); 13240 goto err; 13241 } 13242 13243 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 13244 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 13245 13246 if (*bufp != NULL) { 13247 dtrace_dof_error(dof, difo[i].msg); 13248 goto err; 13249 } 13250 13251 if (difo[i].entsize != subsec->dofs_entsize) { 13252 dtrace_dof_error(dof, "entry size mismatch"); 13253 goto err; 13254 } 13255 13256 if (subsec->dofs_entsize != 0 && 13257 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 13258 dtrace_dof_error(dof, "corrupt entry size"); 13259 goto err; 13260 } 13261 13262 *lenp = subsec->dofs_size; 13263 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 13264 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 13265 *bufp, subsec->dofs_size); 13266 13267 if (subsec->dofs_entsize != 0) 13268 *lenp /= subsec->dofs_entsize; 13269 13270 break; 13271 } 13272 13273 /* 13274 * If we encounter a loadable DIFO sub-section that is not 13275 * known to us, assume this is a broken program and fail. 13276 */ 13277 if (difo[i].section == DOF_SECT_NONE && 13278 (subsec->dofs_flags & DOF_SECF_LOAD)) { 13279 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 13280 goto err; 13281 } 13282 } 13283 13284 if (dp->dtdo_buf == NULL) { 13285 /* 13286 * We can't have a DIF object without DIF text. 13287 */ 13288 dtrace_dof_error(dof, "missing DIF text"); 13289 goto err; 13290 } 13291 13292 /* 13293 * Before we validate the DIF object, run through the variable table 13294 * looking for the strings -- if any of their size are under, we'll set 13295 * their size to be the system-wide default string size. Note that 13296 * this should _not_ happen if the "strsize" option has been set -- 13297 * in this case, the compiler should have set the size to reflect the 13298 * setting of the option. 13299 */ 13300 for (i = 0; i < dp->dtdo_varlen; i++) { 13301 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 13302 dtrace_diftype_t *t = &v->dtdv_type; 13303 13304 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 13305 continue; 13306 13307 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 13308 t->dtdt_size = dtrace_strsize_default; 13309 } 13310 13311 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 13312 goto err; 13313 13314 dtrace_difo_init(dp, vstate); 13315 return (dp); 13316 13317err: 13318 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 13319 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 13320 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 13321 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 13322 13323 kmem_free(dp, sizeof (dtrace_difo_t)); 13324 return (NULL); 13325} 13326 13327static dtrace_predicate_t * 13328dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13329 cred_t *cr) 13330{ 13331 dtrace_difo_t *dp; 13332 13333 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 13334 return (NULL); 13335 13336 return (dtrace_predicate_create(dp)); 13337} 13338 13339static dtrace_actdesc_t * 13340dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13341 cred_t *cr) 13342{ 13343 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 13344 dof_actdesc_t *desc; 13345 dof_sec_t *difosec; 13346 size_t offs; 13347 uintptr_t daddr = (uintptr_t)dof; 13348 uint64_t arg; 13349 dtrace_actkind_t kind; 13350 13351 if (sec->dofs_type != DOF_SECT_ACTDESC) { 13352 dtrace_dof_error(dof, "invalid action section"); 13353 return (NULL); 13354 } 13355 13356 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 13357 dtrace_dof_error(dof, "truncated action description"); 13358 return (NULL); 13359 } 13360 13361 if (sec->dofs_align != sizeof (uint64_t)) { 13362 dtrace_dof_error(dof, "bad alignment in action description"); 13363 return (NULL); 13364 } 13365 13366 if (sec->dofs_size < sec->dofs_entsize) { 13367 dtrace_dof_error(dof, "section entry size exceeds total size"); 13368 return (NULL); 13369 } 13370 13371 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 13372 dtrace_dof_error(dof, "bad entry size in action description"); 13373 return (NULL); 13374 } 13375 13376 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 13377 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 13378 return (NULL); 13379 } 13380 13381 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 13382 desc = (dof_actdesc_t *)(daddr + 13383 (uintptr_t)sec->dofs_offset + offs); 13384 kind = (dtrace_actkind_t)desc->dofa_kind; 13385 13386 if ((DTRACEACT_ISPRINTFLIKE(kind) && 13387 (kind != DTRACEACT_PRINTA || 13388 desc->dofa_strtab != DOF_SECIDX_NONE)) || 13389 (kind == DTRACEACT_DIFEXPR && 13390 desc->dofa_strtab != DOF_SECIDX_NONE)) { 13391 dof_sec_t *strtab; 13392 char *str, *fmt; 13393 uint64_t i; 13394 13395 /* 13396 * The argument to these actions is an index into the 13397 * DOF string table. For printf()-like actions, this 13398 * is the format string. For print(), this is the 13399 * CTF type of the expression result. 13400 */ 13401 if ((strtab = dtrace_dof_sect(dof, 13402 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 13403 goto err; 13404 13405 str = (char *)((uintptr_t)dof + 13406 (uintptr_t)strtab->dofs_offset); 13407 13408 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 13409 if (str[i] == '\0') 13410 break; 13411 } 13412 13413 if (i >= strtab->dofs_size) { 13414 dtrace_dof_error(dof, "bogus format string"); 13415 goto err; 13416 } 13417 13418 if (i == desc->dofa_arg) { 13419 dtrace_dof_error(dof, "empty format string"); 13420 goto err; 13421 } 13422 13423 i -= desc->dofa_arg; 13424 fmt = kmem_alloc(i + 1, KM_SLEEP); 13425 bcopy(&str[desc->dofa_arg], fmt, i + 1); 13426 arg = (uint64_t)(uintptr_t)fmt; 13427 } else { 13428 if (kind == DTRACEACT_PRINTA) { 13429 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 13430 arg = 0; 13431 } else { 13432 arg = desc->dofa_arg; 13433 } 13434 } 13435 13436 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 13437 desc->dofa_uarg, arg); 13438 13439 if (last != NULL) { 13440 last->dtad_next = act; 13441 } else { 13442 first = act; 13443 } 13444 13445 last = act; 13446 13447 if (desc->dofa_difo == DOF_SECIDX_NONE) 13448 continue; 13449 13450 if ((difosec = dtrace_dof_sect(dof, 13451 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 13452 goto err; 13453 13454 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 13455 13456 if (act->dtad_difo == NULL) 13457 goto err; 13458 } 13459 13460 ASSERT(first != NULL); 13461 return (first); 13462 13463err: 13464 for (act = first; act != NULL; act = next) { 13465 next = act->dtad_next; 13466 dtrace_actdesc_release(act, vstate); 13467 } 13468 13469 return (NULL); 13470} 13471 13472static dtrace_ecbdesc_t * 13473dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13474 cred_t *cr) 13475{ 13476 dtrace_ecbdesc_t *ep; 13477 dof_ecbdesc_t *ecb; 13478 dtrace_probedesc_t *desc; 13479 dtrace_predicate_t *pred = NULL; 13480 13481 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 13482 dtrace_dof_error(dof, "truncated ECB description"); 13483 return (NULL); 13484 } 13485 13486 if (sec->dofs_align != sizeof (uint64_t)) { 13487 dtrace_dof_error(dof, "bad alignment in ECB description"); 13488 return (NULL); 13489 } 13490 13491 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 13492 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 13493 13494 if (sec == NULL) 13495 return (NULL); 13496 13497 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 13498 ep->dted_uarg = ecb->dofe_uarg; 13499 desc = &ep->dted_probe; 13500 13501 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 13502 goto err; 13503 13504 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 13505 if ((sec = dtrace_dof_sect(dof, 13506 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 13507 goto err; 13508 13509 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 13510 goto err; 13511 13512 ep->dted_pred.dtpdd_predicate = pred; 13513 } 13514 13515 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 13516 if ((sec = dtrace_dof_sect(dof, 13517 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 13518 goto err; 13519 13520 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 13521 13522 if (ep->dted_action == NULL) 13523 goto err; 13524 } 13525 13526 return (ep); 13527 13528err: 13529 if (pred != NULL) 13530 dtrace_predicate_release(pred, vstate); 13531 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 13532 return (NULL); 13533} 13534 13535/* 13536 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 13537 * specified DOF. At present, this amounts to simply adding 'ubase' to the 13538 * site of any user SETX relocations to account for load object base address. 13539 * In the future, if we need other relocations, this function can be extended. 13540 */ 13541static int 13542dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 13543{ 13544 uintptr_t daddr = (uintptr_t)dof; 13545 dof_relohdr_t *dofr = 13546 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13547 dof_sec_t *ss, *rs, *ts; 13548 dof_relodesc_t *r; 13549 uint_t i, n; 13550 13551 if (sec->dofs_size < sizeof (dof_relohdr_t) || 13552 sec->dofs_align != sizeof (dof_secidx_t)) { 13553 dtrace_dof_error(dof, "invalid relocation header"); 13554 return (-1); 13555 } 13556 13557 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 13558 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 13559 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 13560 13561 if (ss == NULL || rs == NULL || ts == NULL) 13562 return (-1); /* dtrace_dof_error() has been called already */ 13563 13564 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 13565 rs->dofs_align != sizeof (uint64_t)) { 13566 dtrace_dof_error(dof, "invalid relocation section"); 13567 return (-1); 13568 } 13569 13570 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 13571 n = rs->dofs_size / rs->dofs_entsize; 13572 13573 for (i = 0; i < n; i++) { 13574 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 13575 13576 switch (r->dofr_type) { 13577 case DOF_RELO_NONE: 13578 break; 13579 case DOF_RELO_SETX: 13580 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 13581 sizeof (uint64_t) > ts->dofs_size) { 13582 dtrace_dof_error(dof, "bad relocation offset"); 13583 return (-1); 13584 } 13585 13586 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 13587 dtrace_dof_error(dof, "misaligned setx relo"); 13588 return (-1); 13589 } 13590 13591 *(uint64_t *)taddr += ubase; 13592 break; 13593 default: 13594 dtrace_dof_error(dof, "invalid relocation type"); 13595 return (-1); 13596 } 13597 13598 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 13599 } 13600 13601 return (0); 13602} 13603 13604/* 13605 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 13606 * header: it should be at the front of a memory region that is at least 13607 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 13608 * size. It need not be validated in any other way. 13609 */ 13610static int 13611dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 13612 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 13613{ 13614 uint64_t len = dof->dofh_loadsz, seclen; 13615 uintptr_t daddr = (uintptr_t)dof; 13616 dtrace_ecbdesc_t *ep; 13617 dtrace_enabling_t *enab; 13618 uint_t i; 13619 13620 ASSERT(MUTEX_HELD(&dtrace_lock)); 13621 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 13622 13623 /* 13624 * Check the DOF header identification bytes. In addition to checking 13625 * valid settings, we also verify that unused bits/bytes are zeroed so 13626 * we can use them later without fear of regressing existing binaries. 13627 */ 13628 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 13629 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 13630 dtrace_dof_error(dof, "DOF magic string mismatch"); 13631 return (-1); 13632 } 13633 13634 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 13635 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 13636 dtrace_dof_error(dof, "DOF has invalid data model"); 13637 return (-1); 13638 } 13639 13640 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 13641 dtrace_dof_error(dof, "DOF encoding mismatch"); 13642 return (-1); 13643 } 13644 13645 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 13646 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 13647 dtrace_dof_error(dof, "DOF version mismatch"); 13648 return (-1); 13649 } 13650 13651 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 13652 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 13653 return (-1); 13654 } 13655 13656 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 13657 dtrace_dof_error(dof, "DOF uses too many integer registers"); 13658 return (-1); 13659 } 13660 13661 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 13662 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 13663 return (-1); 13664 } 13665 13666 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 13667 if (dof->dofh_ident[i] != 0) { 13668 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 13669 return (-1); 13670 } 13671 } 13672 13673 if (dof->dofh_flags & ~DOF_FL_VALID) { 13674 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 13675 return (-1); 13676 } 13677 13678 if (dof->dofh_secsize == 0) { 13679 dtrace_dof_error(dof, "zero section header size"); 13680 return (-1); 13681 } 13682 13683 /* 13684 * Check that the section headers don't exceed the amount of DOF 13685 * data. Note that we cast the section size and number of sections 13686 * to uint64_t's to prevent possible overflow in the multiplication. 13687 */ 13688 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 13689 13690 if (dof->dofh_secoff > len || seclen > len || 13691 dof->dofh_secoff + seclen > len) { 13692 dtrace_dof_error(dof, "truncated section headers"); 13693 return (-1); 13694 } 13695 13696 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 13697 dtrace_dof_error(dof, "misaligned section headers"); 13698 return (-1); 13699 } 13700 13701 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 13702 dtrace_dof_error(dof, "misaligned section size"); 13703 return (-1); 13704 } 13705 13706 /* 13707 * Take an initial pass through the section headers to be sure that 13708 * the headers don't have stray offsets. If the 'noprobes' flag is 13709 * set, do not permit sections relating to providers, probes, or args. 13710 */ 13711 for (i = 0; i < dof->dofh_secnum; i++) { 13712 dof_sec_t *sec = (dof_sec_t *)(daddr + 13713 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13714 13715 if (noprobes) { 13716 switch (sec->dofs_type) { 13717 case DOF_SECT_PROVIDER: 13718 case DOF_SECT_PROBES: 13719 case DOF_SECT_PRARGS: 13720 case DOF_SECT_PROFFS: 13721 dtrace_dof_error(dof, "illegal sections " 13722 "for enabling"); 13723 return (-1); 13724 } 13725 } 13726 13727 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 13728 !(sec->dofs_flags & DOF_SECF_LOAD)) { 13729 dtrace_dof_error(dof, "loadable section with load " 13730 "flag unset"); 13731 return (-1); 13732 } 13733 13734 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 13735 continue; /* just ignore non-loadable sections */ 13736 13737 if (sec->dofs_align & (sec->dofs_align - 1)) { 13738 dtrace_dof_error(dof, "bad section alignment"); 13739 return (-1); 13740 } 13741 13742 if (sec->dofs_offset & (sec->dofs_align - 1)) { 13743 dtrace_dof_error(dof, "misaligned section"); 13744 return (-1); 13745 } 13746 13747 if (sec->dofs_offset > len || sec->dofs_size > len || 13748 sec->dofs_offset + sec->dofs_size > len) { 13749 dtrace_dof_error(dof, "corrupt section header"); 13750 return (-1); 13751 } 13752 13753 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 13754 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 13755 dtrace_dof_error(dof, "non-terminating string table"); 13756 return (-1); 13757 } 13758 } 13759 13760 /* 13761 * Take a second pass through the sections and locate and perform any 13762 * relocations that are present. We do this after the first pass to 13763 * be sure that all sections have had their headers validated. 13764 */ 13765 for (i = 0; i < dof->dofh_secnum; i++) { 13766 dof_sec_t *sec = (dof_sec_t *)(daddr + 13767 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13768 13769 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 13770 continue; /* skip sections that are not loadable */ 13771 13772 switch (sec->dofs_type) { 13773 case DOF_SECT_URELHDR: 13774 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 13775 return (-1); 13776 break; 13777 } 13778 } 13779 13780 if ((enab = *enabp) == NULL) 13781 enab = *enabp = dtrace_enabling_create(vstate); 13782 13783 for (i = 0; i < dof->dofh_secnum; i++) { 13784 dof_sec_t *sec = (dof_sec_t *)(daddr + 13785 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13786 13787 if (sec->dofs_type != DOF_SECT_ECBDESC) 13788 continue; 13789 13790 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 13791 dtrace_enabling_destroy(enab); 13792 *enabp = NULL; 13793 return (-1); 13794 } 13795 13796 dtrace_enabling_add(enab, ep); 13797 } 13798 13799 return (0); 13800} 13801 13802/* 13803 * Process DOF for any options. This routine assumes that the DOF has been 13804 * at least processed by dtrace_dof_slurp(). 13805 */ 13806static int 13807dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 13808{ 13809 int i, rval; 13810 uint32_t entsize; 13811 size_t offs; 13812 dof_optdesc_t *desc; 13813 13814 for (i = 0; i < dof->dofh_secnum; i++) { 13815 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 13816 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13817 13818 if (sec->dofs_type != DOF_SECT_OPTDESC) 13819 continue; 13820 13821 if (sec->dofs_align != sizeof (uint64_t)) { 13822 dtrace_dof_error(dof, "bad alignment in " 13823 "option description"); 13824 return (EINVAL); 13825 } 13826 13827 if ((entsize = sec->dofs_entsize) == 0) { 13828 dtrace_dof_error(dof, "zeroed option entry size"); 13829 return (EINVAL); 13830 } 13831 13832 if (entsize < sizeof (dof_optdesc_t)) { 13833 dtrace_dof_error(dof, "bad option entry size"); 13834 return (EINVAL); 13835 } 13836 13837 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 13838 desc = (dof_optdesc_t *)((uintptr_t)dof + 13839 (uintptr_t)sec->dofs_offset + offs); 13840 13841 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 13842 dtrace_dof_error(dof, "non-zero option string"); 13843 return (EINVAL); 13844 } 13845 13846 if (desc->dofo_value == DTRACEOPT_UNSET) { 13847 dtrace_dof_error(dof, "unset option"); 13848 return (EINVAL); 13849 } 13850 13851 if ((rval = dtrace_state_option(state, 13852 desc->dofo_option, desc->dofo_value)) != 0) { 13853 dtrace_dof_error(dof, "rejected option"); 13854 return (rval); 13855 } 13856 } 13857 } 13858 13859 return (0); 13860} 13861 13862/* 13863 * DTrace Consumer State Functions 13864 */ 13865static int 13866dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 13867{ 13868 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 13869 void *base; 13870 uintptr_t limit; 13871 dtrace_dynvar_t *dvar, *next, *start; 13872 int i; 13873 13874 ASSERT(MUTEX_HELD(&dtrace_lock)); 13875 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 13876 13877 bzero(dstate, sizeof (dtrace_dstate_t)); 13878 13879 if ((dstate->dtds_chunksize = chunksize) == 0) 13880 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 13881 13882 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 13883 size = min; 13884 13885 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 13886 return (ENOMEM); 13887 13888 dstate->dtds_size = size; 13889 dstate->dtds_base = base; 13890 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 13891 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 13892 13893 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 13894 13895 if (hashsize != 1 && (hashsize & 1)) 13896 hashsize--; 13897 13898 dstate->dtds_hashsize = hashsize; 13899 dstate->dtds_hash = dstate->dtds_base; 13900 13901 /* 13902 * Set all of our hash buckets to point to the single sink, and (if 13903 * it hasn't already been set), set the sink's hash value to be the 13904 * sink sentinel value. The sink is needed for dynamic variable 13905 * lookups to know that they have iterated over an entire, valid hash 13906 * chain. 13907 */ 13908 for (i = 0; i < hashsize; i++) 13909 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 13910 13911 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 13912 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 13913 13914 /* 13915 * Determine number of active CPUs. Divide free list evenly among 13916 * active CPUs. 13917 */ 13918 start = (dtrace_dynvar_t *) 13919 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 13920 limit = (uintptr_t)base + size; 13921 13922 maxper = (limit - (uintptr_t)start) / NCPU; 13923 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 13924 13925#if !defined(sun) 13926 CPU_FOREACH(i) { 13927#else 13928 for (i = 0; i < NCPU; i++) { 13929#endif 13930 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 13931 13932 /* 13933 * If we don't even have enough chunks to make it once through 13934 * NCPUs, we're just going to allocate everything to the first 13935 * CPU. And if we're on the last CPU, we're going to allocate 13936 * whatever is left over. In either case, we set the limit to 13937 * be the limit of the dynamic variable space. 13938 */ 13939 if (maxper == 0 || i == NCPU - 1) { 13940 limit = (uintptr_t)base + size; 13941 start = NULL; 13942 } else { 13943 limit = (uintptr_t)start + maxper; 13944 start = (dtrace_dynvar_t *)limit; 13945 } 13946 13947 ASSERT(limit <= (uintptr_t)base + size); 13948 13949 for (;;) { 13950 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 13951 dstate->dtds_chunksize); 13952 13953 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 13954 break; 13955 13956 dvar->dtdv_next = next; 13957 dvar = next; 13958 } 13959 13960 if (maxper == 0) 13961 break; 13962 } 13963 13964 return (0); 13965} 13966 13967static void 13968dtrace_dstate_fini(dtrace_dstate_t *dstate) 13969{ 13970 ASSERT(MUTEX_HELD(&cpu_lock)); 13971 13972 if (dstate->dtds_base == NULL) 13973 return; 13974 13975 kmem_free(dstate->dtds_base, dstate->dtds_size); 13976 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 13977} 13978 13979static void 13980dtrace_vstate_fini(dtrace_vstate_t *vstate) 13981{ 13982 /* 13983 * Logical XOR, where are you? 13984 */ 13985 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 13986 13987 if (vstate->dtvs_nglobals > 0) { 13988 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 13989 sizeof (dtrace_statvar_t *)); 13990 } 13991 13992 if (vstate->dtvs_ntlocals > 0) { 13993 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 13994 sizeof (dtrace_difv_t)); 13995 } 13996 13997 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 13998 13999 if (vstate->dtvs_nlocals > 0) { 14000 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 14001 sizeof (dtrace_statvar_t *)); 14002 } 14003} 14004 14005#if defined(sun) 14006static void 14007dtrace_state_clean(dtrace_state_t *state) 14008{ 14009 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14010 return; 14011 14012 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14013 dtrace_speculation_clean(state); 14014} 14015 14016static void 14017dtrace_state_deadman(dtrace_state_t *state) 14018{ 14019 hrtime_t now; 14020 14021 dtrace_sync(); 14022 14023 now = dtrace_gethrtime(); 14024 14025 if (state != dtrace_anon.dta_state && 14026 now - state->dts_laststatus >= dtrace_deadman_user) 14027 return; 14028 14029 /* 14030 * We must be sure that dts_alive never appears to be less than the 14031 * value upon entry to dtrace_state_deadman(), and because we lack a 14032 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14033 * store INT64_MAX to it, followed by a memory barrier, followed by 14034 * the new value. This assures that dts_alive never appears to be 14035 * less than its true value, regardless of the order in which the 14036 * stores to the underlying storage are issued. 14037 */ 14038 state->dts_alive = INT64_MAX; 14039 dtrace_membar_producer(); 14040 state->dts_alive = now; 14041} 14042#else 14043static void 14044dtrace_state_clean(void *arg) 14045{ 14046 dtrace_state_t *state = arg; 14047 dtrace_optval_t *opt = state->dts_options; 14048 14049 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14050 return; 14051 14052 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14053 dtrace_speculation_clean(state); 14054 14055 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14056 dtrace_state_clean, state); 14057} 14058 14059static void 14060dtrace_state_deadman(void *arg) 14061{ 14062 dtrace_state_t *state = arg; 14063 hrtime_t now; 14064 14065 dtrace_sync(); 14066 14067 dtrace_debug_output(); 14068 14069 now = dtrace_gethrtime(); 14070 14071 if (state != dtrace_anon.dta_state && 14072 now - state->dts_laststatus >= dtrace_deadman_user) 14073 return; 14074 14075 /* 14076 * We must be sure that dts_alive never appears to be less than the 14077 * value upon entry to dtrace_state_deadman(), and because we lack a 14078 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14079 * store INT64_MAX to it, followed by a memory barrier, followed by 14080 * the new value. This assures that dts_alive never appears to be 14081 * less than its true value, regardless of the order in which the 14082 * stores to the underlying storage are issued. 14083 */ 14084 state->dts_alive = INT64_MAX; 14085 dtrace_membar_producer(); 14086 state->dts_alive = now; 14087 14088 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14089 dtrace_state_deadman, state); 14090} 14091#endif 14092 14093static dtrace_state_t * 14094#if defined(sun) 14095dtrace_state_create(dev_t *devp, cred_t *cr) 14096#else 14097dtrace_state_create(struct cdev *dev) 14098#endif 14099{ 14100#if defined(sun) 14101 minor_t minor; 14102 major_t major; 14103#else 14104 cred_t *cr = NULL; 14105 int m = 0; 14106#endif 14107 char c[30]; 14108 dtrace_state_t *state; 14109 dtrace_optval_t *opt; 14110 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 14111 14112 ASSERT(MUTEX_HELD(&dtrace_lock)); 14113 ASSERT(MUTEX_HELD(&cpu_lock)); 14114 14115#if defined(sun) 14116 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 14117 VM_BESTFIT | VM_SLEEP); 14118 14119 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 14120 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14121 return (NULL); 14122 } 14123 14124 state = ddi_get_soft_state(dtrace_softstate, minor); 14125#else 14126 if (dev != NULL) { 14127 cr = dev->si_cred; 14128 m = dev2unit(dev); 14129 } 14130 14131 /* Allocate memory for the state. */ 14132 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 14133#endif 14134 14135 state->dts_epid = DTRACE_EPIDNONE + 1; 14136 14137 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 14138#if defined(sun) 14139 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 14140 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14141 14142 if (devp != NULL) { 14143 major = getemajor(*devp); 14144 } else { 14145 major = ddi_driver_major(dtrace_devi); 14146 } 14147 14148 state->dts_dev = makedevice(major, minor); 14149 14150 if (devp != NULL) 14151 *devp = state->dts_dev; 14152#else 14153 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 14154 state->dts_dev = dev; 14155#endif 14156 14157 /* 14158 * We allocate NCPU buffers. On the one hand, this can be quite 14159 * a bit of memory per instance (nearly 36K on a Starcat). On the 14160 * other hand, it saves an additional memory reference in the probe 14161 * path. 14162 */ 14163 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 14164 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 14165 14166#if defined(sun) 14167 state->dts_cleaner = CYCLIC_NONE; 14168 state->dts_deadman = CYCLIC_NONE; 14169#else 14170 callout_init(&state->dts_cleaner, CALLOUT_MPSAFE); 14171 callout_init(&state->dts_deadman, CALLOUT_MPSAFE); 14172#endif 14173 state->dts_vstate.dtvs_state = state; 14174 14175 for (i = 0; i < DTRACEOPT_MAX; i++) 14176 state->dts_options[i] = DTRACEOPT_UNSET; 14177 14178 /* 14179 * Set the default options. 14180 */ 14181 opt = state->dts_options; 14182 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 14183 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 14184 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 14185 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 14186 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 14187 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 14188 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 14189 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 14190 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 14191 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 14192 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 14193 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 14194 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 14195 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 14196 14197 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 14198 14199 /* 14200 * Depending on the user credentials, we set flag bits which alter probe 14201 * visibility or the amount of destructiveness allowed. In the case of 14202 * actual anonymous tracing, or the possession of all privileges, all of 14203 * the normal checks are bypassed. 14204 */ 14205 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 14206 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 14207 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 14208 } else { 14209 /* 14210 * Set up the credentials for this instantiation. We take a 14211 * hold on the credential to prevent it from disappearing on 14212 * us; this in turn prevents the zone_t referenced by this 14213 * credential from disappearing. This means that we can 14214 * examine the credential and the zone from probe context. 14215 */ 14216 crhold(cr); 14217 state->dts_cred.dcr_cred = cr; 14218 14219 /* 14220 * CRA_PROC means "we have *some* privilege for dtrace" and 14221 * unlocks the use of variables like pid, zonename, etc. 14222 */ 14223 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 14224 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14225 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 14226 } 14227 14228 /* 14229 * dtrace_user allows use of syscall and profile providers. 14230 * If the user also has proc_owner and/or proc_zone, we 14231 * extend the scope to include additional visibility and 14232 * destructive power. 14233 */ 14234 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 14235 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 14236 state->dts_cred.dcr_visible |= 14237 DTRACE_CRV_ALLPROC; 14238 14239 state->dts_cred.dcr_action |= 14240 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14241 } 14242 14243 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 14244 state->dts_cred.dcr_visible |= 14245 DTRACE_CRV_ALLZONE; 14246 14247 state->dts_cred.dcr_action |= 14248 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14249 } 14250 14251 /* 14252 * If we have all privs in whatever zone this is, 14253 * we can do destructive things to processes which 14254 * have altered credentials. 14255 */ 14256#if defined(sun) 14257 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14258 cr->cr_zone->zone_privset)) { 14259 state->dts_cred.dcr_action |= 14260 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14261 } 14262#endif 14263 } 14264 14265 /* 14266 * Holding the dtrace_kernel privilege also implies that 14267 * the user has the dtrace_user privilege from a visibility 14268 * perspective. But without further privileges, some 14269 * destructive actions are not available. 14270 */ 14271 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 14272 /* 14273 * Make all probes in all zones visible. However, 14274 * this doesn't mean that all actions become available 14275 * to all zones. 14276 */ 14277 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 14278 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 14279 14280 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 14281 DTRACE_CRA_PROC; 14282 /* 14283 * Holding proc_owner means that destructive actions 14284 * for *this* zone are allowed. 14285 */ 14286 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14287 state->dts_cred.dcr_action |= 14288 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14289 14290 /* 14291 * Holding proc_zone means that destructive actions 14292 * for this user/group ID in all zones is allowed. 14293 */ 14294 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14295 state->dts_cred.dcr_action |= 14296 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14297 14298#if defined(sun) 14299 /* 14300 * If we have all privs in whatever zone this is, 14301 * we can do destructive things to processes which 14302 * have altered credentials. 14303 */ 14304 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14305 cr->cr_zone->zone_privset)) { 14306 state->dts_cred.dcr_action |= 14307 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14308 } 14309#endif 14310 } 14311 14312 /* 14313 * Holding the dtrace_proc privilege gives control over fasttrap 14314 * and pid providers. We need to grant wider destructive 14315 * privileges in the event that the user has proc_owner and/or 14316 * proc_zone. 14317 */ 14318 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14319 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14320 state->dts_cred.dcr_action |= 14321 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14322 14323 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14324 state->dts_cred.dcr_action |= 14325 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14326 } 14327 } 14328 14329 return (state); 14330} 14331 14332static int 14333dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 14334{ 14335 dtrace_optval_t *opt = state->dts_options, size; 14336 processorid_t cpu = 0;; 14337 int flags = 0, rval, factor, divisor = 1; 14338 14339 ASSERT(MUTEX_HELD(&dtrace_lock)); 14340 ASSERT(MUTEX_HELD(&cpu_lock)); 14341 ASSERT(which < DTRACEOPT_MAX); 14342 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 14343 (state == dtrace_anon.dta_state && 14344 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 14345 14346 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 14347 return (0); 14348 14349 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 14350 cpu = opt[DTRACEOPT_CPU]; 14351 14352 if (which == DTRACEOPT_SPECSIZE) 14353 flags |= DTRACEBUF_NOSWITCH; 14354 14355 if (which == DTRACEOPT_BUFSIZE) { 14356 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 14357 flags |= DTRACEBUF_RING; 14358 14359 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 14360 flags |= DTRACEBUF_FILL; 14361 14362 if (state != dtrace_anon.dta_state || 14363 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14364 flags |= DTRACEBUF_INACTIVE; 14365 } 14366 14367 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 14368 /* 14369 * The size must be 8-byte aligned. If the size is not 8-byte 14370 * aligned, drop it down by the difference. 14371 */ 14372 if (size & (sizeof (uint64_t) - 1)) 14373 size -= size & (sizeof (uint64_t) - 1); 14374 14375 if (size < state->dts_reserve) { 14376 /* 14377 * Buffers always must be large enough to accommodate 14378 * their prereserved space. We return E2BIG instead 14379 * of ENOMEM in this case to allow for user-level 14380 * software to differentiate the cases. 14381 */ 14382 return (E2BIG); 14383 } 14384 14385 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 14386 14387 if (rval != ENOMEM) { 14388 opt[which] = size; 14389 return (rval); 14390 } 14391 14392 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14393 return (rval); 14394 14395 for (divisor = 2; divisor < factor; divisor <<= 1) 14396 continue; 14397 } 14398 14399 return (ENOMEM); 14400} 14401 14402static int 14403dtrace_state_buffers(dtrace_state_t *state) 14404{ 14405 dtrace_speculation_t *spec = state->dts_speculations; 14406 int rval, i; 14407 14408 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 14409 DTRACEOPT_BUFSIZE)) != 0) 14410 return (rval); 14411 14412 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 14413 DTRACEOPT_AGGSIZE)) != 0) 14414 return (rval); 14415 14416 for (i = 0; i < state->dts_nspeculations; i++) { 14417 if ((rval = dtrace_state_buffer(state, 14418 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 14419 return (rval); 14420 } 14421 14422 return (0); 14423} 14424 14425static void 14426dtrace_state_prereserve(dtrace_state_t *state) 14427{ 14428 dtrace_ecb_t *ecb; 14429 dtrace_probe_t *probe; 14430 14431 state->dts_reserve = 0; 14432 14433 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 14434 return; 14435 14436 /* 14437 * If our buffer policy is a "fill" buffer policy, we need to set the 14438 * prereserved space to be the space required by the END probes. 14439 */ 14440 probe = dtrace_probes[dtrace_probeid_end - 1]; 14441 ASSERT(probe != NULL); 14442 14443 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 14444 if (ecb->dte_state != state) 14445 continue; 14446 14447 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 14448 } 14449} 14450 14451static int 14452dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 14453{ 14454 dtrace_optval_t *opt = state->dts_options, sz, nspec; 14455 dtrace_speculation_t *spec; 14456 dtrace_buffer_t *buf; 14457#if defined(sun) 14458 cyc_handler_t hdlr; 14459 cyc_time_t when; 14460#endif 14461 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 14462 dtrace_icookie_t cookie; 14463 14464 mutex_enter(&cpu_lock); 14465 mutex_enter(&dtrace_lock); 14466 14467 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 14468 rval = EBUSY; 14469 goto out; 14470 } 14471 14472 /* 14473 * Before we can perform any checks, we must prime all of the 14474 * retained enablings that correspond to this state. 14475 */ 14476 dtrace_enabling_prime(state); 14477 14478 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 14479 rval = EACCES; 14480 goto out; 14481 } 14482 14483 dtrace_state_prereserve(state); 14484 14485 /* 14486 * Now we want to do is try to allocate our speculations. 14487 * We do not automatically resize the number of speculations; if 14488 * this fails, we will fail the operation. 14489 */ 14490 nspec = opt[DTRACEOPT_NSPEC]; 14491 ASSERT(nspec != DTRACEOPT_UNSET); 14492 14493 if (nspec > INT_MAX) { 14494 rval = ENOMEM; 14495 goto out; 14496 } 14497 14498 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 14499 KM_NOSLEEP | KM_NORMALPRI); 14500 14501 if (spec == NULL) { 14502 rval = ENOMEM; 14503 goto out; 14504 } 14505 14506 state->dts_speculations = spec; 14507 state->dts_nspeculations = (int)nspec; 14508 14509 for (i = 0; i < nspec; i++) { 14510 if ((buf = kmem_zalloc(bufsize, 14511 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 14512 rval = ENOMEM; 14513 goto err; 14514 } 14515 14516 spec[i].dtsp_buffer = buf; 14517 } 14518 14519 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 14520 if (dtrace_anon.dta_state == NULL) { 14521 rval = ENOENT; 14522 goto out; 14523 } 14524 14525 if (state->dts_necbs != 0) { 14526 rval = EALREADY; 14527 goto out; 14528 } 14529 14530 state->dts_anon = dtrace_anon_grab(); 14531 ASSERT(state->dts_anon != NULL); 14532 state = state->dts_anon; 14533 14534 /* 14535 * We want "grabanon" to be set in the grabbed state, so we'll 14536 * copy that option value from the grabbing state into the 14537 * grabbed state. 14538 */ 14539 state->dts_options[DTRACEOPT_GRABANON] = 14540 opt[DTRACEOPT_GRABANON]; 14541 14542 *cpu = dtrace_anon.dta_beganon; 14543 14544 /* 14545 * If the anonymous state is active (as it almost certainly 14546 * is if the anonymous enabling ultimately matched anything), 14547 * we don't allow any further option processing -- but we 14548 * don't return failure. 14549 */ 14550 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 14551 goto out; 14552 } 14553 14554 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 14555 opt[DTRACEOPT_AGGSIZE] != 0) { 14556 if (state->dts_aggregations == NULL) { 14557 /* 14558 * We're not going to create an aggregation buffer 14559 * because we don't have any ECBs that contain 14560 * aggregations -- set this option to 0. 14561 */ 14562 opt[DTRACEOPT_AGGSIZE] = 0; 14563 } else { 14564 /* 14565 * If we have an aggregation buffer, we must also have 14566 * a buffer to use as scratch. 14567 */ 14568 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 14569 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 14570 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 14571 } 14572 } 14573 } 14574 14575 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 14576 opt[DTRACEOPT_SPECSIZE] != 0) { 14577 if (!state->dts_speculates) { 14578 /* 14579 * We're not going to create speculation buffers 14580 * because we don't have any ECBs that actually 14581 * speculate -- set the speculation size to 0. 14582 */ 14583 opt[DTRACEOPT_SPECSIZE] = 0; 14584 } 14585 } 14586 14587 /* 14588 * The bare minimum size for any buffer that we're actually going to 14589 * do anything to is sizeof (uint64_t). 14590 */ 14591 sz = sizeof (uint64_t); 14592 14593 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 14594 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 14595 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 14596 /* 14597 * A buffer size has been explicitly set to 0 (or to a size 14598 * that will be adjusted to 0) and we need the space -- we 14599 * need to return failure. We return ENOSPC to differentiate 14600 * it from failing to allocate a buffer due to failure to meet 14601 * the reserve (for which we return E2BIG). 14602 */ 14603 rval = ENOSPC; 14604 goto out; 14605 } 14606 14607 if ((rval = dtrace_state_buffers(state)) != 0) 14608 goto err; 14609 14610 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 14611 sz = dtrace_dstate_defsize; 14612 14613 do { 14614 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 14615 14616 if (rval == 0) 14617 break; 14618 14619 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14620 goto err; 14621 } while (sz >>= 1); 14622 14623 opt[DTRACEOPT_DYNVARSIZE] = sz; 14624 14625 if (rval != 0) 14626 goto err; 14627 14628 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 14629 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 14630 14631 if (opt[DTRACEOPT_CLEANRATE] == 0) 14632 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 14633 14634 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 14635 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 14636 14637 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 14638 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 14639 14640 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 14641#if defined(sun) 14642 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 14643 hdlr.cyh_arg = state; 14644 hdlr.cyh_level = CY_LOW_LEVEL; 14645 14646 when.cyt_when = 0; 14647 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 14648 14649 state->dts_cleaner = cyclic_add(&hdlr, &when); 14650 14651 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 14652 hdlr.cyh_arg = state; 14653 hdlr.cyh_level = CY_LOW_LEVEL; 14654 14655 when.cyt_when = 0; 14656 when.cyt_interval = dtrace_deadman_interval; 14657 14658 state->dts_deadman = cyclic_add(&hdlr, &when); 14659#else 14660 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14661 dtrace_state_clean, state); 14662 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14663 dtrace_state_deadman, state); 14664#endif 14665 14666 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 14667 14668#if defined(sun) 14669 if (state->dts_getf != 0 && 14670 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 14671 /* 14672 * We don't have kernel privs but we have at least one call 14673 * to getf(); we need to bump our zone's count, and (if 14674 * this is the first enabling to have an unprivileged call 14675 * to getf()) we need to hook into closef(). 14676 */ 14677 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 14678 14679 if (dtrace_getf++ == 0) { 14680 ASSERT(dtrace_closef == NULL); 14681 dtrace_closef = dtrace_getf_barrier; 14682 } 14683 } 14684#endif 14685 14686 /* 14687 * Now it's time to actually fire the BEGIN probe. We need to disable 14688 * interrupts here both to record the CPU on which we fired the BEGIN 14689 * probe (the data from this CPU will be processed first at user 14690 * level) and to manually activate the buffer for this CPU. 14691 */ 14692 cookie = dtrace_interrupt_disable(); 14693 *cpu = curcpu; 14694 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 14695 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 14696 14697 dtrace_probe(dtrace_probeid_begin, 14698 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 14699 dtrace_interrupt_enable(cookie); 14700 /* 14701 * We may have had an exit action from a BEGIN probe; only change our 14702 * state to ACTIVE if we're still in WARMUP. 14703 */ 14704 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 14705 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 14706 14707 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 14708 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 14709 14710 /* 14711 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 14712 * want each CPU to transition its principal buffer out of the 14713 * INACTIVE state. Doing this assures that no CPU will suddenly begin 14714 * processing an ECB halfway down a probe's ECB chain; all CPUs will 14715 * atomically transition from processing none of a state's ECBs to 14716 * processing all of them. 14717 */ 14718 dtrace_xcall(DTRACE_CPUALL, 14719 (dtrace_xcall_t)dtrace_buffer_activate, state); 14720 goto out; 14721 14722err: 14723 dtrace_buffer_free(state->dts_buffer); 14724 dtrace_buffer_free(state->dts_aggbuffer); 14725 14726 if ((nspec = state->dts_nspeculations) == 0) { 14727 ASSERT(state->dts_speculations == NULL); 14728 goto out; 14729 } 14730 14731 spec = state->dts_speculations; 14732 ASSERT(spec != NULL); 14733 14734 for (i = 0; i < state->dts_nspeculations; i++) { 14735 if ((buf = spec[i].dtsp_buffer) == NULL) 14736 break; 14737 14738 dtrace_buffer_free(buf); 14739 kmem_free(buf, bufsize); 14740 } 14741 14742 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 14743 state->dts_nspeculations = 0; 14744 state->dts_speculations = NULL; 14745 14746out: 14747 mutex_exit(&dtrace_lock); 14748 mutex_exit(&cpu_lock); 14749 14750 return (rval); 14751} 14752 14753static int 14754dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 14755{ 14756 dtrace_icookie_t cookie; 14757 14758 ASSERT(MUTEX_HELD(&dtrace_lock)); 14759 14760 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 14761 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 14762 return (EINVAL); 14763 14764 /* 14765 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 14766 * to be sure that every CPU has seen it. See below for the details 14767 * on why this is done. 14768 */ 14769 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 14770 dtrace_sync(); 14771 14772 /* 14773 * By this point, it is impossible for any CPU to be still processing 14774 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 14775 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 14776 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 14777 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 14778 * iff we're in the END probe. 14779 */ 14780 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 14781 dtrace_sync(); 14782 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 14783 14784 /* 14785 * Finally, we can release the reserve and call the END probe. We 14786 * disable interrupts across calling the END probe to allow us to 14787 * return the CPU on which we actually called the END probe. This 14788 * allows user-land to be sure that this CPU's principal buffer is 14789 * processed last. 14790 */ 14791 state->dts_reserve = 0; 14792 14793 cookie = dtrace_interrupt_disable(); 14794 *cpu = curcpu; 14795 dtrace_probe(dtrace_probeid_end, 14796 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 14797 dtrace_interrupt_enable(cookie); 14798 14799 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 14800 dtrace_sync(); 14801 14802#if defined(sun) 14803 if (state->dts_getf != 0 && 14804 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 14805 /* 14806 * We don't have kernel privs but we have at least one call 14807 * to getf(); we need to lower our zone's count, and (if 14808 * this is the last enabling to have an unprivileged call 14809 * to getf()) we need to clear the closef() hook. 14810 */ 14811 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 14812 ASSERT(dtrace_closef == dtrace_getf_barrier); 14813 ASSERT(dtrace_getf > 0); 14814 14815 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 14816 14817 if (--dtrace_getf == 0) 14818 dtrace_closef = NULL; 14819 } 14820#endif 14821 14822 return (0); 14823} 14824 14825static int 14826dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 14827 dtrace_optval_t val) 14828{ 14829 ASSERT(MUTEX_HELD(&dtrace_lock)); 14830 14831 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 14832 return (EBUSY); 14833 14834 if (option >= DTRACEOPT_MAX) 14835 return (EINVAL); 14836 14837 if (option != DTRACEOPT_CPU && val < 0) 14838 return (EINVAL); 14839 14840 switch (option) { 14841 case DTRACEOPT_DESTRUCTIVE: 14842 if (dtrace_destructive_disallow) 14843 return (EACCES); 14844 14845 state->dts_cred.dcr_destructive = 1; 14846 break; 14847 14848 case DTRACEOPT_BUFSIZE: 14849 case DTRACEOPT_DYNVARSIZE: 14850 case DTRACEOPT_AGGSIZE: 14851 case DTRACEOPT_SPECSIZE: 14852 case DTRACEOPT_STRSIZE: 14853 if (val < 0) 14854 return (EINVAL); 14855 14856 if (val >= LONG_MAX) { 14857 /* 14858 * If this is an otherwise negative value, set it to 14859 * the highest multiple of 128m less than LONG_MAX. 14860 * Technically, we're adjusting the size without 14861 * regard to the buffer resizing policy, but in fact, 14862 * this has no effect -- if we set the buffer size to 14863 * ~LONG_MAX and the buffer policy is ultimately set to 14864 * be "manual", the buffer allocation is guaranteed to 14865 * fail, if only because the allocation requires two 14866 * buffers. (We set the the size to the highest 14867 * multiple of 128m because it ensures that the size 14868 * will remain a multiple of a megabyte when 14869 * repeatedly halved -- all the way down to 15m.) 14870 */ 14871 val = LONG_MAX - (1 << 27) + 1; 14872 } 14873 } 14874 14875 state->dts_options[option] = val; 14876 14877 return (0); 14878} 14879 14880static void 14881dtrace_state_destroy(dtrace_state_t *state) 14882{ 14883 dtrace_ecb_t *ecb; 14884 dtrace_vstate_t *vstate = &state->dts_vstate; 14885#if defined(sun) 14886 minor_t minor = getminor(state->dts_dev); 14887#endif 14888 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 14889 dtrace_speculation_t *spec = state->dts_speculations; 14890 int nspec = state->dts_nspeculations; 14891 uint32_t match; 14892 14893 ASSERT(MUTEX_HELD(&dtrace_lock)); 14894 ASSERT(MUTEX_HELD(&cpu_lock)); 14895 14896 /* 14897 * First, retract any retained enablings for this state. 14898 */ 14899 dtrace_enabling_retract(state); 14900 ASSERT(state->dts_nretained == 0); 14901 14902 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 14903 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 14904 /* 14905 * We have managed to come into dtrace_state_destroy() on a 14906 * hot enabling -- almost certainly because of a disorderly 14907 * shutdown of a consumer. (That is, a consumer that is 14908 * exiting without having called dtrace_stop().) In this case, 14909 * we're going to set our activity to be KILLED, and then 14910 * issue a sync to be sure that everyone is out of probe 14911 * context before we start blowing away ECBs. 14912 */ 14913 state->dts_activity = DTRACE_ACTIVITY_KILLED; 14914 dtrace_sync(); 14915 } 14916 14917 /* 14918 * Release the credential hold we took in dtrace_state_create(). 14919 */ 14920 if (state->dts_cred.dcr_cred != NULL) 14921 crfree(state->dts_cred.dcr_cred); 14922 14923 /* 14924 * Now we can safely disable and destroy any enabled probes. Because 14925 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 14926 * (especially if they're all enabled), we take two passes through the 14927 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 14928 * in the second we disable whatever is left over. 14929 */ 14930 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 14931 for (i = 0; i < state->dts_necbs; i++) { 14932 if ((ecb = state->dts_ecbs[i]) == NULL) 14933 continue; 14934 14935 if (match && ecb->dte_probe != NULL) { 14936 dtrace_probe_t *probe = ecb->dte_probe; 14937 dtrace_provider_t *prov = probe->dtpr_provider; 14938 14939 if (!(prov->dtpv_priv.dtpp_flags & match)) 14940 continue; 14941 } 14942 14943 dtrace_ecb_disable(ecb); 14944 dtrace_ecb_destroy(ecb); 14945 } 14946 14947 if (!match) 14948 break; 14949 } 14950 14951 /* 14952 * Before we free the buffers, perform one more sync to assure that 14953 * every CPU is out of probe context. 14954 */ 14955 dtrace_sync(); 14956 14957 dtrace_buffer_free(state->dts_buffer); 14958 dtrace_buffer_free(state->dts_aggbuffer); 14959 14960 for (i = 0; i < nspec; i++) 14961 dtrace_buffer_free(spec[i].dtsp_buffer); 14962 14963#if defined(sun) 14964 if (state->dts_cleaner != CYCLIC_NONE) 14965 cyclic_remove(state->dts_cleaner); 14966 14967 if (state->dts_deadman != CYCLIC_NONE) 14968 cyclic_remove(state->dts_deadman); 14969#else 14970 callout_stop(&state->dts_cleaner); 14971 callout_drain(&state->dts_cleaner); 14972 callout_stop(&state->dts_deadman); 14973 callout_drain(&state->dts_deadman); 14974#endif 14975 14976 dtrace_dstate_fini(&vstate->dtvs_dynvars); 14977 dtrace_vstate_fini(vstate); 14978 if (state->dts_ecbs != NULL) 14979 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 14980 14981 if (state->dts_aggregations != NULL) { 14982#ifdef DEBUG 14983 for (i = 0; i < state->dts_naggregations; i++) 14984 ASSERT(state->dts_aggregations[i] == NULL); 14985#endif 14986 ASSERT(state->dts_naggregations > 0); 14987 kmem_free(state->dts_aggregations, 14988 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 14989 } 14990 14991 kmem_free(state->dts_buffer, bufsize); 14992 kmem_free(state->dts_aggbuffer, bufsize); 14993 14994 for (i = 0; i < nspec; i++) 14995 kmem_free(spec[i].dtsp_buffer, bufsize); 14996 14997 if (spec != NULL) 14998 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 14999 15000 dtrace_format_destroy(state); 15001 15002 if (state->dts_aggid_arena != NULL) { 15003#if defined(sun) 15004 vmem_destroy(state->dts_aggid_arena); 15005#else 15006 delete_unrhdr(state->dts_aggid_arena); 15007#endif 15008 state->dts_aggid_arena = NULL; 15009 } 15010#if defined(sun) 15011 ddi_soft_state_free(dtrace_softstate, minor); 15012 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 15013#endif 15014} 15015 15016/* 15017 * DTrace Anonymous Enabling Functions 15018 */ 15019static dtrace_state_t * 15020dtrace_anon_grab(void) 15021{ 15022 dtrace_state_t *state; 15023 15024 ASSERT(MUTEX_HELD(&dtrace_lock)); 15025 15026 if ((state = dtrace_anon.dta_state) == NULL) { 15027 ASSERT(dtrace_anon.dta_enabling == NULL); 15028 return (NULL); 15029 } 15030 15031 ASSERT(dtrace_anon.dta_enabling != NULL); 15032 ASSERT(dtrace_retained != NULL); 15033 15034 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 15035 dtrace_anon.dta_enabling = NULL; 15036 dtrace_anon.dta_state = NULL; 15037 15038 return (state); 15039} 15040 15041static void 15042dtrace_anon_property(void) 15043{ 15044 int i, rv; 15045 dtrace_state_t *state; 15046 dof_hdr_t *dof; 15047 char c[32]; /* enough for "dof-data-" + digits */ 15048 15049 ASSERT(MUTEX_HELD(&dtrace_lock)); 15050 ASSERT(MUTEX_HELD(&cpu_lock)); 15051 15052 for (i = 0; ; i++) { 15053 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 15054 15055 dtrace_err_verbose = 1; 15056 15057 if ((dof = dtrace_dof_property(c)) == NULL) { 15058 dtrace_err_verbose = 0; 15059 break; 15060 } 15061 15062#if defined(sun) 15063 /* 15064 * We want to create anonymous state, so we need to transition 15065 * the kernel debugger to indicate that DTrace is active. If 15066 * this fails (e.g. because the debugger has modified text in 15067 * some way), we won't continue with the processing. 15068 */ 15069 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15070 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 15071 "enabling ignored."); 15072 dtrace_dof_destroy(dof); 15073 break; 15074 } 15075#endif 15076 15077 /* 15078 * If we haven't allocated an anonymous state, we'll do so now. 15079 */ 15080 if ((state = dtrace_anon.dta_state) == NULL) { 15081#if defined(sun) 15082 state = dtrace_state_create(NULL, NULL); 15083#else 15084 state = dtrace_state_create(NULL); 15085#endif 15086 dtrace_anon.dta_state = state; 15087 15088 if (state == NULL) { 15089 /* 15090 * This basically shouldn't happen: the only 15091 * failure mode from dtrace_state_create() is a 15092 * failure of ddi_soft_state_zalloc() that 15093 * itself should never happen. Still, the 15094 * interface allows for a failure mode, and 15095 * we want to fail as gracefully as possible: 15096 * we'll emit an error message and cease 15097 * processing anonymous state in this case. 15098 */ 15099 cmn_err(CE_WARN, "failed to create " 15100 "anonymous state"); 15101 dtrace_dof_destroy(dof); 15102 break; 15103 } 15104 } 15105 15106 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 15107 &dtrace_anon.dta_enabling, 0, B_TRUE); 15108 15109 if (rv == 0) 15110 rv = dtrace_dof_options(dof, state); 15111 15112 dtrace_err_verbose = 0; 15113 dtrace_dof_destroy(dof); 15114 15115 if (rv != 0) { 15116 /* 15117 * This is malformed DOF; chuck any anonymous state 15118 * that we created. 15119 */ 15120 ASSERT(dtrace_anon.dta_enabling == NULL); 15121 dtrace_state_destroy(state); 15122 dtrace_anon.dta_state = NULL; 15123 break; 15124 } 15125 15126 ASSERT(dtrace_anon.dta_enabling != NULL); 15127 } 15128 15129 if (dtrace_anon.dta_enabling != NULL) { 15130 int rval; 15131 15132 /* 15133 * dtrace_enabling_retain() can only fail because we are 15134 * trying to retain more enablings than are allowed -- but 15135 * we only have one anonymous enabling, and we are guaranteed 15136 * to be allowed at least one retained enabling; we assert 15137 * that dtrace_enabling_retain() returns success. 15138 */ 15139 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 15140 ASSERT(rval == 0); 15141 15142 dtrace_enabling_dump(dtrace_anon.dta_enabling); 15143 } 15144} 15145 15146/* 15147 * DTrace Helper Functions 15148 */ 15149static void 15150dtrace_helper_trace(dtrace_helper_action_t *helper, 15151 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 15152{ 15153 uint32_t size, next, nnext, i; 15154 dtrace_helptrace_t *ent; 15155 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 15156 15157 if (!dtrace_helptrace_enabled) 15158 return; 15159 15160 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 15161 15162 /* 15163 * What would a tracing framework be without its own tracing 15164 * framework? (Well, a hell of a lot simpler, for starters...) 15165 */ 15166 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 15167 sizeof (uint64_t) - sizeof (uint64_t); 15168 15169 /* 15170 * Iterate until we can allocate a slot in the trace buffer. 15171 */ 15172 do { 15173 next = dtrace_helptrace_next; 15174 15175 if (next + size < dtrace_helptrace_bufsize) { 15176 nnext = next + size; 15177 } else { 15178 nnext = size; 15179 } 15180 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 15181 15182 /* 15183 * We have our slot; fill it in. 15184 */ 15185 if (nnext == size) 15186 next = 0; 15187 15188 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 15189 ent->dtht_helper = helper; 15190 ent->dtht_where = where; 15191 ent->dtht_nlocals = vstate->dtvs_nlocals; 15192 15193 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 15194 mstate->dtms_fltoffs : -1; 15195 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 15196 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 15197 15198 for (i = 0; i < vstate->dtvs_nlocals; i++) { 15199 dtrace_statvar_t *svar; 15200 15201 if ((svar = vstate->dtvs_locals[i]) == NULL) 15202 continue; 15203 15204 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 15205 ent->dtht_locals[i] = 15206 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 15207 } 15208} 15209 15210static uint64_t 15211dtrace_helper(int which, dtrace_mstate_t *mstate, 15212 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 15213{ 15214 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 15215 uint64_t sarg0 = mstate->dtms_arg[0]; 15216 uint64_t sarg1 = mstate->dtms_arg[1]; 15217 uint64_t rval = 0; 15218 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 15219 dtrace_helper_action_t *helper; 15220 dtrace_vstate_t *vstate; 15221 dtrace_difo_t *pred; 15222 int i, trace = dtrace_helptrace_enabled; 15223 15224 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 15225 15226 if (helpers == NULL) 15227 return (0); 15228 15229 if ((helper = helpers->dthps_actions[which]) == NULL) 15230 return (0); 15231 15232 vstate = &helpers->dthps_vstate; 15233 mstate->dtms_arg[0] = arg0; 15234 mstate->dtms_arg[1] = arg1; 15235 15236 /* 15237 * Now iterate over each helper. If its predicate evaluates to 'true', 15238 * we'll call the corresponding actions. Note that the below calls 15239 * to dtrace_dif_emulate() may set faults in machine state. This is 15240 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 15241 * the stored DIF offset with its own (which is the desired behavior). 15242 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 15243 * from machine state; this is okay, too. 15244 */ 15245 for (; helper != NULL; helper = helper->dtha_next) { 15246 if ((pred = helper->dtha_predicate) != NULL) { 15247 if (trace) 15248 dtrace_helper_trace(helper, mstate, vstate, 0); 15249 15250 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 15251 goto next; 15252 15253 if (*flags & CPU_DTRACE_FAULT) 15254 goto err; 15255 } 15256 15257 for (i = 0; i < helper->dtha_nactions; i++) { 15258 if (trace) 15259 dtrace_helper_trace(helper, 15260 mstate, vstate, i + 1); 15261 15262 rval = dtrace_dif_emulate(helper->dtha_actions[i], 15263 mstate, vstate, state); 15264 15265 if (*flags & CPU_DTRACE_FAULT) 15266 goto err; 15267 } 15268 15269next: 15270 if (trace) 15271 dtrace_helper_trace(helper, mstate, vstate, 15272 DTRACE_HELPTRACE_NEXT); 15273 } 15274 15275 if (trace) 15276 dtrace_helper_trace(helper, mstate, vstate, 15277 DTRACE_HELPTRACE_DONE); 15278 15279 /* 15280 * Restore the arg0 that we saved upon entry. 15281 */ 15282 mstate->dtms_arg[0] = sarg0; 15283 mstate->dtms_arg[1] = sarg1; 15284 15285 return (rval); 15286 15287err: 15288 if (trace) 15289 dtrace_helper_trace(helper, mstate, vstate, 15290 DTRACE_HELPTRACE_ERR); 15291 15292 /* 15293 * Restore the arg0 that we saved upon entry. 15294 */ 15295 mstate->dtms_arg[0] = sarg0; 15296 mstate->dtms_arg[1] = sarg1; 15297 15298 return (0); 15299} 15300 15301static void 15302dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 15303 dtrace_vstate_t *vstate) 15304{ 15305 int i; 15306 15307 if (helper->dtha_predicate != NULL) 15308 dtrace_difo_release(helper->dtha_predicate, vstate); 15309 15310 for (i = 0; i < helper->dtha_nactions; i++) { 15311 ASSERT(helper->dtha_actions[i] != NULL); 15312 dtrace_difo_release(helper->dtha_actions[i], vstate); 15313 } 15314 15315 kmem_free(helper->dtha_actions, 15316 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 15317 kmem_free(helper, sizeof (dtrace_helper_action_t)); 15318} 15319 15320static int 15321dtrace_helper_destroygen(int gen) 15322{ 15323 proc_t *p = curproc; 15324 dtrace_helpers_t *help = p->p_dtrace_helpers; 15325 dtrace_vstate_t *vstate; 15326 int i; 15327 15328 ASSERT(MUTEX_HELD(&dtrace_lock)); 15329 15330 if (help == NULL || gen > help->dthps_generation) 15331 return (EINVAL); 15332 15333 vstate = &help->dthps_vstate; 15334 15335 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15336 dtrace_helper_action_t *last = NULL, *h, *next; 15337 15338 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15339 next = h->dtha_next; 15340 15341 if (h->dtha_generation == gen) { 15342 if (last != NULL) { 15343 last->dtha_next = next; 15344 } else { 15345 help->dthps_actions[i] = next; 15346 } 15347 15348 dtrace_helper_action_destroy(h, vstate); 15349 } else { 15350 last = h; 15351 } 15352 } 15353 } 15354 15355 /* 15356 * Interate until we've cleared out all helper providers with the 15357 * given generation number. 15358 */ 15359 for (;;) { 15360 dtrace_helper_provider_t *prov; 15361 15362 /* 15363 * Look for a helper provider with the right generation. We 15364 * have to start back at the beginning of the list each time 15365 * because we drop dtrace_lock. It's unlikely that we'll make 15366 * more than two passes. 15367 */ 15368 for (i = 0; i < help->dthps_nprovs; i++) { 15369 prov = help->dthps_provs[i]; 15370 15371 if (prov->dthp_generation == gen) 15372 break; 15373 } 15374 15375 /* 15376 * If there were no matches, we're done. 15377 */ 15378 if (i == help->dthps_nprovs) 15379 break; 15380 15381 /* 15382 * Move the last helper provider into this slot. 15383 */ 15384 help->dthps_nprovs--; 15385 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 15386 help->dthps_provs[help->dthps_nprovs] = NULL; 15387 15388 mutex_exit(&dtrace_lock); 15389 15390 /* 15391 * If we have a meta provider, remove this helper provider. 15392 */ 15393 mutex_enter(&dtrace_meta_lock); 15394 if (dtrace_meta_pid != NULL) { 15395 ASSERT(dtrace_deferred_pid == NULL); 15396 dtrace_helper_provider_remove(&prov->dthp_prov, 15397 p->p_pid); 15398 } 15399 mutex_exit(&dtrace_meta_lock); 15400 15401 dtrace_helper_provider_destroy(prov); 15402 15403 mutex_enter(&dtrace_lock); 15404 } 15405 15406 return (0); 15407} 15408 15409static int 15410dtrace_helper_validate(dtrace_helper_action_t *helper) 15411{ 15412 int err = 0, i; 15413 dtrace_difo_t *dp; 15414 15415 if ((dp = helper->dtha_predicate) != NULL) 15416 err += dtrace_difo_validate_helper(dp); 15417 15418 for (i = 0; i < helper->dtha_nactions; i++) 15419 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 15420 15421 return (err == 0); 15422} 15423 15424static int 15425dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 15426{ 15427 dtrace_helpers_t *help; 15428 dtrace_helper_action_t *helper, *last; 15429 dtrace_actdesc_t *act; 15430 dtrace_vstate_t *vstate; 15431 dtrace_predicate_t *pred; 15432 int count = 0, nactions = 0, i; 15433 15434 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 15435 return (EINVAL); 15436 15437 help = curproc->p_dtrace_helpers; 15438 last = help->dthps_actions[which]; 15439 vstate = &help->dthps_vstate; 15440 15441 for (count = 0; last != NULL; last = last->dtha_next) { 15442 count++; 15443 if (last->dtha_next == NULL) 15444 break; 15445 } 15446 15447 /* 15448 * If we already have dtrace_helper_actions_max helper actions for this 15449 * helper action type, we'll refuse to add a new one. 15450 */ 15451 if (count >= dtrace_helper_actions_max) 15452 return (ENOSPC); 15453 15454 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 15455 helper->dtha_generation = help->dthps_generation; 15456 15457 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 15458 ASSERT(pred->dtp_difo != NULL); 15459 dtrace_difo_hold(pred->dtp_difo); 15460 helper->dtha_predicate = pred->dtp_difo; 15461 } 15462 15463 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 15464 if (act->dtad_kind != DTRACEACT_DIFEXPR) 15465 goto err; 15466 15467 if (act->dtad_difo == NULL) 15468 goto err; 15469 15470 nactions++; 15471 } 15472 15473 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 15474 (helper->dtha_nactions = nactions), KM_SLEEP); 15475 15476 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 15477 dtrace_difo_hold(act->dtad_difo); 15478 helper->dtha_actions[i++] = act->dtad_difo; 15479 } 15480 15481 if (!dtrace_helper_validate(helper)) 15482 goto err; 15483 15484 if (last == NULL) { 15485 help->dthps_actions[which] = helper; 15486 } else { 15487 last->dtha_next = helper; 15488 } 15489 15490 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 15491 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 15492 dtrace_helptrace_next = 0; 15493 } 15494 15495 return (0); 15496err: 15497 dtrace_helper_action_destroy(helper, vstate); 15498 return (EINVAL); 15499} 15500 15501static void 15502dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 15503 dof_helper_t *dofhp) 15504{ 15505 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 15506 15507 mutex_enter(&dtrace_meta_lock); 15508 mutex_enter(&dtrace_lock); 15509 15510 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 15511 /* 15512 * If the dtrace module is loaded but not attached, or if 15513 * there aren't isn't a meta provider registered to deal with 15514 * these provider descriptions, we need to postpone creating 15515 * the actual providers until later. 15516 */ 15517 15518 if (help->dthps_next == NULL && help->dthps_prev == NULL && 15519 dtrace_deferred_pid != help) { 15520 help->dthps_deferred = 1; 15521 help->dthps_pid = p->p_pid; 15522 help->dthps_next = dtrace_deferred_pid; 15523 help->dthps_prev = NULL; 15524 if (dtrace_deferred_pid != NULL) 15525 dtrace_deferred_pid->dthps_prev = help; 15526 dtrace_deferred_pid = help; 15527 } 15528 15529 mutex_exit(&dtrace_lock); 15530 15531 } else if (dofhp != NULL) { 15532 /* 15533 * If the dtrace module is loaded and we have a particular 15534 * helper provider description, pass that off to the 15535 * meta provider. 15536 */ 15537 15538 mutex_exit(&dtrace_lock); 15539 15540 dtrace_helper_provide(dofhp, p->p_pid); 15541 15542 } else { 15543 /* 15544 * Otherwise, just pass all the helper provider descriptions 15545 * off to the meta provider. 15546 */ 15547 15548 int i; 15549 mutex_exit(&dtrace_lock); 15550 15551 for (i = 0; i < help->dthps_nprovs; i++) { 15552 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 15553 p->p_pid); 15554 } 15555 } 15556 15557 mutex_exit(&dtrace_meta_lock); 15558} 15559 15560static int 15561dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 15562{ 15563 dtrace_helpers_t *help; 15564 dtrace_helper_provider_t *hprov, **tmp_provs; 15565 uint_t tmp_maxprovs, i; 15566 15567 ASSERT(MUTEX_HELD(&dtrace_lock)); 15568 15569 help = curproc->p_dtrace_helpers; 15570 ASSERT(help != NULL); 15571 15572 /* 15573 * If we already have dtrace_helper_providers_max helper providers, 15574 * we're refuse to add a new one. 15575 */ 15576 if (help->dthps_nprovs >= dtrace_helper_providers_max) 15577 return (ENOSPC); 15578 15579 /* 15580 * Check to make sure this isn't a duplicate. 15581 */ 15582 for (i = 0; i < help->dthps_nprovs; i++) { 15583 if (dofhp->dofhp_dof == 15584 help->dthps_provs[i]->dthp_prov.dofhp_dof) 15585 return (EALREADY); 15586 } 15587 15588 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 15589 hprov->dthp_prov = *dofhp; 15590 hprov->dthp_ref = 1; 15591 hprov->dthp_generation = gen; 15592 15593 /* 15594 * Allocate a bigger table for helper providers if it's already full. 15595 */ 15596 if (help->dthps_maxprovs == help->dthps_nprovs) { 15597 tmp_maxprovs = help->dthps_maxprovs; 15598 tmp_provs = help->dthps_provs; 15599 15600 if (help->dthps_maxprovs == 0) 15601 help->dthps_maxprovs = 2; 15602 else 15603 help->dthps_maxprovs *= 2; 15604 if (help->dthps_maxprovs > dtrace_helper_providers_max) 15605 help->dthps_maxprovs = dtrace_helper_providers_max; 15606 15607 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 15608 15609 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 15610 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 15611 15612 if (tmp_provs != NULL) { 15613 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 15614 sizeof (dtrace_helper_provider_t *)); 15615 kmem_free(tmp_provs, tmp_maxprovs * 15616 sizeof (dtrace_helper_provider_t *)); 15617 } 15618 } 15619 15620 help->dthps_provs[help->dthps_nprovs] = hprov; 15621 help->dthps_nprovs++; 15622 15623 return (0); 15624} 15625 15626static void 15627dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 15628{ 15629 mutex_enter(&dtrace_lock); 15630 15631 if (--hprov->dthp_ref == 0) { 15632 dof_hdr_t *dof; 15633 mutex_exit(&dtrace_lock); 15634 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 15635 dtrace_dof_destroy(dof); 15636 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 15637 } else { 15638 mutex_exit(&dtrace_lock); 15639 } 15640} 15641 15642static int 15643dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 15644{ 15645 uintptr_t daddr = (uintptr_t)dof; 15646 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 15647 dof_provider_t *provider; 15648 dof_probe_t *probe; 15649 uint8_t *arg; 15650 char *strtab, *typestr; 15651 dof_stridx_t typeidx; 15652 size_t typesz; 15653 uint_t nprobes, j, k; 15654 15655 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 15656 15657 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 15658 dtrace_dof_error(dof, "misaligned section offset"); 15659 return (-1); 15660 } 15661 15662 /* 15663 * The section needs to be large enough to contain the DOF provider 15664 * structure appropriate for the given version. 15665 */ 15666 if (sec->dofs_size < 15667 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 15668 offsetof(dof_provider_t, dofpv_prenoffs) : 15669 sizeof (dof_provider_t))) { 15670 dtrace_dof_error(dof, "provider section too small"); 15671 return (-1); 15672 } 15673 15674 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 15675 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 15676 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 15677 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 15678 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 15679 15680 if (str_sec == NULL || prb_sec == NULL || 15681 arg_sec == NULL || off_sec == NULL) 15682 return (-1); 15683 15684 enoff_sec = NULL; 15685 15686 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 15687 provider->dofpv_prenoffs != DOF_SECT_NONE && 15688 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 15689 provider->dofpv_prenoffs)) == NULL) 15690 return (-1); 15691 15692 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 15693 15694 if (provider->dofpv_name >= str_sec->dofs_size || 15695 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 15696 dtrace_dof_error(dof, "invalid provider name"); 15697 return (-1); 15698 } 15699 15700 if (prb_sec->dofs_entsize == 0 || 15701 prb_sec->dofs_entsize > prb_sec->dofs_size) { 15702 dtrace_dof_error(dof, "invalid entry size"); 15703 return (-1); 15704 } 15705 15706 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 15707 dtrace_dof_error(dof, "misaligned entry size"); 15708 return (-1); 15709 } 15710 15711 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 15712 dtrace_dof_error(dof, "invalid entry size"); 15713 return (-1); 15714 } 15715 15716 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 15717 dtrace_dof_error(dof, "misaligned section offset"); 15718 return (-1); 15719 } 15720 15721 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 15722 dtrace_dof_error(dof, "invalid entry size"); 15723 return (-1); 15724 } 15725 15726 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 15727 15728 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 15729 15730 /* 15731 * Take a pass through the probes to check for errors. 15732 */ 15733 for (j = 0; j < nprobes; j++) { 15734 probe = (dof_probe_t *)(uintptr_t)(daddr + 15735 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 15736 15737 if (probe->dofpr_func >= str_sec->dofs_size) { 15738 dtrace_dof_error(dof, "invalid function name"); 15739 return (-1); 15740 } 15741 15742 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 15743 dtrace_dof_error(dof, "function name too long"); 15744 return (-1); 15745 } 15746 15747 if (probe->dofpr_name >= str_sec->dofs_size || 15748 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 15749 dtrace_dof_error(dof, "invalid probe name"); 15750 return (-1); 15751 } 15752 15753 /* 15754 * The offset count must not wrap the index, and the offsets 15755 * must also not overflow the section's data. 15756 */ 15757 if (probe->dofpr_offidx + probe->dofpr_noffs < 15758 probe->dofpr_offidx || 15759 (probe->dofpr_offidx + probe->dofpr_noffs) * 15760 off_sec->dofs_entsize > off_sec->dofs_size) { 15761 dtrace_dof_error(dof, "invalid probe offset"); 15762 return (-1); 15763 } 15764 15765 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 15766 /* 15767 * If there's no is-enabled offset section, make sure 15768 * there aren't any is-enabled offsets. Otherwise 15769 * perform the same checks as for probe offsets 15770 * (immediately above). 15771 */ 15772 if (enoff_sec == NULL) { 15773 if (probe->dofpr_enoffidx != 0 || 15774 probe->dofpr_nenoffs != 0) { 15775 dtrace_dof_error(dof, "is-enabled " 15776 "offsets with null section"); 15777 return (-1); 15778 } 15779 } else if (probe->dofpr_enoffidx + 15780 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 15781 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 15782 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 15783 dtrace_dof_error(dof, "invalid is-enabled " 15784 "offset"); 15785 return (-1); 15786 } 15787 15788 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 15789 dtrace_dof_error(dof, "zero probe and " 15790 "is-enabled offsets"); 15791 return (-1); 15792 } 15793 } else if (probe->dofpr_noffs == 0) { 15794 dtrace_dof_error(dof, "zero probe offsets"); 15795 return (-1); 15796 } 15797 15798 if (probe->dofpr_argidx + probe->dofpr_xargc < 15799 probe->dofpr_argidx || 15800 (probe->dofpr_argidx + probe->dofpr_xargc) * 15801 arg_sec->dofs_entsize > arg_sec->dofs_size) { 15802 dtrace_dof_error(dof, "invalid args"); 15803 return (-1); 15804 } 15805 15806 typeidx = probe->dofpr_nargv; 15807 typestr = strtab + probe->dofpr_nargv; 15808 for (k = 0; k < probe->dofpr_nargc; k++) { 15809 if (typeidx >= str_sec->dofs_size) { 15810 dtrace_dof_error(dof, "bad " 15811 "native argument type"); 15812 return (-1); 15813 } 15814 15815 typesz = strlen(typestr) + 1; 15816 if (typesz > DTRACE_ARGTYPELEN) { 15817 dtrace_dof_error(dof, "native " 15818 "argument type too long"); 15819 return (-1); 15820 } 15821 typeidx += typesz; 15822 typestr += typesz; 15823 } 15824 15825 typeidx = probe->dofpr_xargv; 15826 typestr = strtab + probe->dofpr_xargv; 15827 for (k = 0; k < probe->dofpr_xargc; k++) { 15828 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 15829 dtrace_dof_error(dof, "bad " 15830 "native argument index"); 15831 return (-1); 15832 } 15833 15834 if (typeidx >= str_sec->dofs_size) { 15835 dtrace_dof_error(dof, "bad " 15836 "translated argument type"); 15837 return (-1); 15838 } 15839 15840 typesz = strlen(typestr) + 1; 15841 if (typesz > DTRACE_ARGTYPELEN) { 15842 dtrace_dof_error(dof, "translated argument " 15843 "type too long"); 15844 return (-1); 15845 } 15846 15847 typeidx += typesz; 15848 typestr += typesz; 15849 } 15850 } 15851 15852 return (0); 15853} 15854 15855static int 15856dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 15857{ 15858 dtrace_helpers_t *help; 15859 dtrace_vstate_t *vstate; 15860 dtrace_enabling_t *enab = NULL; 15861 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 15862 uintptr_t daddr = (uintptr_t)dof; 15863 15864 ASSERT(MUTEX_HELD(&dtrace_lock)); 15865 15866 if ((help = curproc->p_dtrace_helpers) == NULL) 15867 help = dtrace_helpers_create(curproc); 15868 15869 vstate = &help->dthps_vstate; 15870 15871 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 15872 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 15873 dtrace_dof_destroy(dof); 15874 return (rv); 15875 } 15876 15877 /* 15878 * Look for helper providers and validate their descriptions. 15879 */ 15880 if (dhp != NULL) { 15881 for (i = 0; i < dof->dofh_secnum; i++) { 15882 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 15883 dof->dofh_secoff + i * dof->dofh_secsize); 15884 15885 if (sec->dofs_type != DOF_SECT_PROVIDER) 15886 continue; 15887 15888 if (dtrace_helper_provider_validate(dof, sec) != 0) { 15889 dtrace_enabling_destroy(enab); 15890 dtrace_dof_destroy(dof); 15891 return (-1); 15892 } 15893 15894 nprovs++; 15895 } 15896 } 15897 15898 /* 15899 * Now we need to walk through the ECB descriptions in the enabling. 15900 */ 15901 for (i = 0; i < enab->dten_ndesc; i++) { 15902 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 15903 dtrace_probedesc_t *desc = &ep->dted_probe; 15904 15905 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 15906 continue; 15907 15908 if (strcmp(desc->dtpd_mod, "helper") != 0) 15909 continue; 15910 15911 if (strcmp(desc->dtpd_func, "ustack") != 0) 15912 continue; 15913 15914 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 15915 ep)) != 0) { 15916 /* 15917 * Adding this helper action failed -- we are now going 15918 * to rip out the entire generation and return failure. 15919 */ 15920 (void) dtrace_helper_destroygen(help->dthps_generation); 15921 dtrace_enabling_destroy(enab); 15922 dtrace_dof_destroy(dof); 15923 return (-1); 15924 } 15925 15926 nhelpers++; 15927 } 15928 15929 if (nhelpers < enab->dten_ndesc) 15930 dtrace_dof_error(dof, "unmatched helpers"); 15931 15932 gen = help->dthps_generation++; 15933 dtrace_enabling_destroy(enab); 15934 15935 if (dhp != NULL && nprovs > 0) { 15936 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 15937 if (dtrace_helper_provider_add(dhp, gen) == 0) { 15938 mutex_exit(&dtrace_lock); 15939 dtrace_helper_provider_register(curproc, help, dhp); 15940 mutex_enter(&dtrace_lock); 15941 15942 destroy = 0; 15943 } 15944 } 15945 15946 if (destroy) 15947 dtrace_dof_destroy(dof); 15948 15949 return (gen); 15950} 15951 15952static dtrace_helpers_t * 15953dtrace_helpers_create(proc_t *p) 15954{ 15955 dtrace_helpers_t *help; 15956 15957 ASSERT(MUTEX_HELD(&dtrace_lock)); 15958 ASSERT(p->p_dtrace_helpers == NULL); 15959 15960 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 15961 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 15962 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 15963 15964 p->p_dtrace_helpers = help; 15965 dtrace_helpers++; 15966 15967 return (help); 15968} 15969 15970#if defined(sun) 15971static 15972#endif 15973void 15974dtrace_helpers_destroy(proc_t *p) 15975{ 15976 dtrace_helpers_t *help; 15977 dtrace_vstate_t *vstate; 15978#if defined(sun) 15979 proc_t *p = curproc; 15980#endif 15981 int i; 15982 15983 mutex_enter(&dtrace_lock); 15984 15985 ASSERT(p->p_dtrace_helpers != NULL); 15986 ASSERT(dtrace_helpers > 0); 15987 15988 help = p->p_dtrace_helpers; 15989 vstate = &help->dthps_vstate; 15990 15991 /* 15992 * We're now going to lose the help from this process. 15993 */ 15994 p->p_dtrace_helpers = NULL; 15995 dtrace_sync(); 15996 15997 /* 15998 * Destory the helper actions. 15999 */ 16000 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16001 dtrace_helper_action_t *h, *next; 16002 16003 for (h = help->dthps_actions[i]; h != NULL; h = next) { 16004 next = h->dtha_next; 16005 dtrace_helper_action_destroy(h, vstate); 16006 h = next; 16007 } 16008 } 16009 16010 mutex_exit(&dtrace_lock); 16011 16012 /* 16013 * Destroy the helper providers. 16014 */ 16015 if (help->dthps_maxprovs > 0) { 16016 mutex_enter(&dtrace_meta_lock); 16017 if (dtrace_meta_pid != NULL) { 16018 ASSERT(dtrace_deferred_pid == NULL); 16019 16020 for (i = 0; i < help->dthps_nprovs; i++) { 16021 dtrace_helper_provider_remove( 16022 &help->dthps_provs[i]->dthp_prov, p->p_pid); 16023 } 16024 } else { 16025 mutex_enter(&dtrace_lock); 16026 ASSERT(help->dthps_deferred == 0 || 16027 help->dthps_next != NULL || 16028 help->dthps_prev != NULL || 16029 help == dtrace_deferred_pid); 16030 16031 /* 16032 * Remove the helper from the deferred list. 16033 */ 16034 if (help->dthps_next != NULL) 16035 help->dthps_next->dthps_prev = help->dthps_prev; 16036 if (help->dthps_prev != NULL) 16037 help->dthps_prev->dthps_next = help->dthps_next; 16038 if (dtrace_deferred_pid == help) { 16039 dtrace_deferred_pid = help->dthps_next; 16040 ASSERT(help->dthps_prev == NULL); 16041 } 16042 16043 mutex_exit(&dtrace_lock); 16044 } 16045 16046 mutex_exit(&dtrace_meta_lock); 16047 16048 for (i = 0; i < help->dthps_nprovs; i++) { 16049 dtrace_helper_provider_destroy(help->dthps_provs[i]); 16050 } 16051 16052 kmem_free(help->dthps_provs, help->dthps_maxprovs * 16053 sizeof (dtrace_helper_provider_t *)); 16054 } 16055 16056 mutex_enter(&dtrace_lock); 16057 16058 dtrace_vstate_fini(&help->dthps_vstate); 16059 kmem_free(help->dthps_actions, 16060 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 16061 kmem_free(help, sizeof (dtrace_helpers_t)); 16062 16063 --dtrace_helpers; 16064 mutex_exit(&dtrace_lock); 16065} 16066 16067#if defined(sun) 16068static 16069#endif 16070void 16071dtrace_helpers_duplicate(proc_t *from, proc_t *to) 16072{ 16073 dtrace_helpers_t *help, *newhelp; 16074 dtrace_helper_action_t *helper, *new, *last; 16075 dtrace_difo_t *dp; 16076 dtrace_vstate_t *vstate; 16077 int i, j, sz, hasprovs = 0; 16078 16079 mutex_enter(&dtrace_lock); 16080 ASSERT(from->p_dtrace_helpers != NULL); 16081 ASSERT(dtrace_helpers > 0); 16082 16083 help = from->p_dtrace_helpers; 16084 newhelp = dtrace_helpers_create(to); 16085 ASSERT(to->p_dtrace_helpers != NULL); 16086 16087 newhelp->dthps_generation = help->dthps_generation; 16088 vstate = &newhelp->dthps_vstate; 16089 16090 /* 16091 * Duplicate the helper actions. 16092 */ 16093 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16094 if ((helper = help->dthps_actions[i]) == NULL) 16095 continue; 16096 16097 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 16098 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 16099 KM_SLEEP); 16100 new->dtha_generation = helper->dtha_generation; 16101 16102 if ((dp = helper->dtha_predicate) != NULL) { 16103 dp = dtrace_difo_duplicate(dp, vstate); 16104 new->dtha_predicate = dp; 16105 } 16106 16107 new->dtha_nactions = helper->dtha_nactions; 16108 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 16109 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 16110 16111 for (j = 0; j < new->dtha_nactions; j++) { 16112 dtrace_difo_t *dp = helper->dtha_actions[j]; 16113 16114 ASSERT(dp != NULL); 16115 dp = dtrace_difo_duplicate(dp, vstate); 16116 new->dtha_actions[j] = dp; 16117 } 16118 16119 if (last != NULL) { 16120 last->dtha_next = new; 16121 } else { 16122 newhelp->dthps_actions[i] = new; 16123 } 16124 16125 last = new; 16126 } 16127 } 16128 16129 /* 16130 * Duplicate the helper providers and register them with the 16131 * DTrace framework. 16132 */ 16133 if (help->dthps_nprovs > 0) { 16134 newhelp->dthps_nprovs = help->dthps_nprovs; 16135 newhelp->dthps_maxprovs = help->dthps_nprovs; 16136 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 16137 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16138 for (i = 0; i < newhelp->dthps_nprovs; i++) { 16139 newhelp->dthps_provs[i] = help->dthps_provs[i]; 16140 newhelp->dthps_provs[i]->dthp_ref++; 16141 } 16142 16143 hasprovs = 1; 16144 } 16145 16146 mutex_exit(&dtrace_lock); 16147 16148 if (hasprovs) 16149 dtrace_helper_provider_register(to, newhelp, NULL); 16150} 16151 16152/* 16153 * DTrace Hook Functions 16154 */ 16155static void 16156dtrace_module_loaded(modctl_t *ctl) 16157{ 16158 dtrace_provider_t *prv; 16159 16160 mutex_enter(&dtrace_provider_lock); 16161#if defined(sun) 16162 mutex_enter(&mod_lock); 16163#endif 16164 16165#if defined(sun) 16166 ASSERT(ctl->mod_busy); 16167#endif 16168 16169 /* 16170 * We're going to call each providers per-module provide operation 16171 * specifying only this module. 16172 */ 16173 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 16174 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 16175 16176#if defined(sun) 16177 mutex_exit(&mod_lock); 16178#endif 16179 mutex_exit(&dtrace_provider_lock); 16180 16181 /* 16182 * If we have any retained enablings, we need to match against them. 16183 * Enabling probes requires that cpu_lock be held, and we cannot hold 16184 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 16185 * module. (In particular, this happens when loading scheduling 16186 * classes.) So if we have any retained enablings, we need to dispatch 16187 * our task queue to do the match for us. 16188 */ 16189 mutex_enter(&dtrace_lock); 16190 16191 if (dtrace_retained == NULL) { 16192 mutex_exit(&dtrace_lock); 16193 return; 16194 } 16195 16196 (void) taskq_dispatch(dtrace_taskq, 16197 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 16198 16199 mutex_exit(&dtrace_lock); 16200 16201 /* 16202 * And now, for a little heuristic sleaze: in general, we want to 16203 * match modules as soon as they load. However, we cannot guarantee 16204 * this, because it would lead us to the lock ordering violation 16205 * outlined above. The common case, of course, is that cpu_lock is 16206 * _not_ held -- so we delay here for a clock tick, hoping that that's 16207 * long enough for the task queue to do its work. If it's not, it's 16208 * not a serious problem -- it just means that the module that we 16209 * just loaded may not be immediately instrumentable. 16210 */ 16211 delay(1); 16212} 16213 16214static void 16215#if defined(sun) 16216dtrace_module_unloaded(modctl_t *ctl) 16217#else 16218dtrace_module_unloaded(modctl_t *ctl, int *error) 16219#endif 16220{ 16221 dtrace_probe_t template, *probe, *first, *next; 16222 dtrace_provider_t *prov; 16223#if !defined(sun) 16224 char modname[DTRACE_MODNAMELEN]; 16225 size_t len; 16226#endif 16227 16228#if defined(sun) 16229 template.dtpr_mod = ctl->mod_modname; 16230#else 16231 /* Handle the fact that ctl->filename may end in ".ko". */ 16232 strlcpy(modname, ctl->filename, sizeof(modname)); 16233 len = strlen(ctl->filename); 16234 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0) 16235 modname[len - 3] = '\0'; 16236 template.dtpr_mod = modname; 16237#endif 16238 16239 mutex_enter(&dtrace_provider_lock); 16240#if defined(sun) 16241 mutex_enter(&mod_lock); 16242#endif 16243 mutex_enter(&dtrace_lock); 16244 16245#if !defined(sun) 16246 if (ctl->nenabled > 0) { 16247 /* Don't allow unloads if a probe is enabled. */ 16248 mutex_exit(&dtrace_provider_lock); 16249 mutex_exit(&dtrace_lock); 16250 *error = -1; 16251 printf( 16252 "kldunload: attempt to unload module that has DTrace probes enabled\n"); 16253 return; 16254 } 16255#endif 16256 16257 if (dtrace_bymod == NULL) { 16258 /* 16259 * The DTrace module is loaded (obviously) but not attached; 16260 * we don't have any work to do. 16261 */ 16262 mutex_exit(&dtrace_provider_lock); 16263#if defined(sun) 16264 mutex_exit(&mod_lock); 16265#endif 16266 mutex_exit(&dtrace_lock); 16267 return; 16268 } 16269 16270 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 16271 probe != NULL; probe = probe->dtpr_nextmod) { 16272 if (probe->dtpr_ecb != NULL) { 16273 mutex_exit(&dtrace_provider_lock); 16274#if defined(sun) 16275 mutex_exit(&mod_lock); 16276#endif 16277 mutex_exit(&dtrace_lock); 16278 16279 /* 16280 * This shouldn't _actually_ be possible -- we're 16281 * unloading a module that has an enabled probe in it. 16282 * (It's normally up to the provider to make sure that 16283 * this can't happen.) However, because dtps_enable() 16284 * doesn't have a failure mode, there can be an 16285 * enable/unload race. Upshot: we don't want to 16286 * assert, but we're not going to disable the 16287 * probe, either. 16288 */ 16289 if (dtrace_err_verbose) { 16290#if defined(sun) 16291 cmn_err(CE_WARN, "unloaded module '%s' had " 16292 "enabled probes", ctl->mod_modname); 16293#else 16294 cmn_err(CE_WARN, "unloaded module '%s' had " 16295 "enabled probes", modname); 16296#endif 16297 } 16298 16299 return; 16300 } 16301 } 16302 16303 probe = first; 16304 16305 for (first = NULL; probe != NULL; probe = next) { 16306 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 16307 16308 dtrace_probes[probe->dtpr_id - 1] = NULL; 16309 16310 next = probe->dtpr_nextmod; 16311 dtrace_hash_remove(dtrace_bymod, probe); 16312 dtrace_hash_remove(dtrace_byfunc, probe); 16313 dtrace_hash_remove(dtrace_byname, probe); 16314 16315 if (first == NULL) { 16316 first = probe; 16317 probe->dtpr_nextmod = NULL; 16318 } else { 16319 probe->dtpr_nextmod = first; 16320 first = probe; 16321 } 16322 } 16323 16324 /* 16325 * We've removed all of the module's probes from the hash chains and 16326 * from the probe array. Now issue a dtrace_sync() to be sure that 16327 * everyone has cleared out from any probe array processing. 16328 */ 16329 dtrace_sync(); 16330 16331 for (probe = first; probe != NULL; probe = first) { 16332 first = probe->dtpr_nextmod; 16333 prov = probe->dtpr_provider; 16334 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 16335 probe->dtpr_arg); 16336 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 16337 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 16338 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 16339#if defined(sun) 16340 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 16341#else 16342 free_unr(dtrace_arena, probe->dtpr_id); 16343#endif 16344 kmem_free(probe, sizeof (dtrace_probe_t)); 16345 } 16346 16347 mutex_exit(&dtrace_lock); 16348#if defined(sun) 16349 mutex_exit(&mod_lock); 16350#endif 16351 mutex_exit(&dtrace_provider_lock); 16352} 16353 16354#if !defined(sun) 16355static void 16356dtrace_kld_load(void *arg __unused, linker_file_t lf) 16357{ 16358 16359 dtrace_module_loaded(lf); 16360} 16361 16362static void 16363dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error) 16364{ 16365 16366 if (*error != 0) 16367 /* We already have an error, so don't do anything. */ 16368 return; 16369 dtrace_module_unloaded(lf, error); 16370} 16371#endif 16372 16373#if defined(sun) 16374static void 16375dtrace_suspend(void) 16376{ 16377 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 16378} 16379 16380static void 16381dtrace_resume(void) 16382{ 16383 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 16384} 16385#endif 16386 16387static int 16388dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 16389{ 16390 ASSERT(MUTEX_HELD(&cpu_lock)); 16391 mutex_enter(&dtrace_lock); 16392 16393 switch (what) { 16394 case CPU_CONFIG: { 16395 dtrace_state_t *state; 16396 dtrace_optval_t *opt, rs, c; 16397 16398 /* 16399 * For now, we only allocate a new buffer for anonymous state. 16400 */ 16401 if ((state = dtrace_anon.dta_state) == NULL) 16402 break; 16403 16404 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 16405 break; 16406 16407 opt = state->dts_options; 16408 c = opt[DTRACEOPT_CPU]; 16409 16410 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 16411 break; 16412 16413 /* 16414 * Regardless of what the actual policy is, we're going to 16415 * temporarily set our resize policy to be manual. We're 16416 * also going to temporarily set our CPU option to denote 16417 * the newly configured CPU. 16418 */ 16419 rs = opt[DTRACEOPT_BUFRESIZE]; 16420 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 16421 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 16422 16423 (void) dtrace_state_buffers(state); 16424 16425 opt[DTRACEOPT_BUFRESIZE] = rs; 16426 opt[DTRACEOPT_CPU] = c; 16427 16428 break; 16429 } 16430 16431 case CPU_UNCONFIG: 16432 /* 16433 * We don't free the buffer in the CPU_UNCONFIG case. (The 16434 * buffer will be freed when the consumer exits.) 16435 */ 16436 break; 16437 16438 default: 16439 break; 16440 } 16441 16442 mutex_exit(&dtrace_lock); 16443 return (0); 16444} 16445 16446#if defined(sun) 16447static void 16448dtrace_cpu_setup_initial(processorid_t cpu) 16449{ 16450 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 16451} 16452#endif 16453 16454static void 16455dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 16456{ 16457 if (dtrace_toxranges >= dtrace_toxranges_max) { 16458 int osize, nsize; 16459 dtrace_toxrange_t *range; 16460 16461 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16462 16463 if (osize == 0) { 16464 ASSERT(dtrace_toxrange == NULL); 16465 ASSERT(dtrace_toxranges_max == 0); 16466 dtrace_toxranges_max = 1; 16467 } else { 16468 dtrace_toxranges_max <<= 1; 16469 } 16470 16471 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16472 range = kmem_zalloc(nsize, KM_SLEEP); 16473 16474 if (dtrace_toxrange != NULL) { 16475 ASSERT(osize != 0); 16476 bcopy(dtrace_toxrange, range, osize); 16477 kmem_free(dtrace_toxrange, osize); 16478 } 16479 16480 dtrace_toxrange = range; 16481 } 16482 16483 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 16484 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 16485 16486 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 16487 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 16488 dtrace_toxranges++; 16489} 16490 16491static void 16492dtrace_getf_barrier() 16493{ 16494#if defined(sun) 16495 /* 16496 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 16497 * that contain calls to getf(), this routine will be called on every 16498 * closef() before either the underlying vnode is released or the 16499 * file_t itself is freed. By the time we are here, it is essential 16500 * that the file_t can no longer be accessed from a call to getf() 16501 * in probe context -- that assures that a dtrace_sync() can be used 16502 * to clear out any enablings referring to the old structures. 16503 */ 16504 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 16505 kcred->cr_zone->zone_dtrace_getf != 0) 16506 dtrace_sync(); 16507#endif 16508} 16509 16510/* 16511 * DTrace Driver Cookbook Functions 16512 */ 16513#if defined(sun) 16514/*ARGSUSED*/ 16515static int 16516dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 16517{ 16518 dtrace_provider_id_t id; 16519 dtrace_state_t *state = NULL; 16520 dtrace_enabling_t *enab; 16521 16522 mutex_enter(&cpu_lock); 16523 mutex_enter(&dtrace_provider_lock); 16524 mutex_enter(&dtrace_lock); 16525 16526 if (ddi_soft_state_init(&dtrace_softstate, 16527 sizeof (dtrace_state_t), 0) != 0) { 16528 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 16529 mutex_exit(&cpu_lock); 16530 mutex_exit(&dtrace_provider_lock); 16531 mutex_exit(&dtrace_lock); 16532 return (DDI_FAILURE); 16533 } 16534 16535 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 16536 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 16537 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 16538 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 16539 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 16540 ddi_remove_minor_node(devi, NULL); 16541 ddi_soft_state_fini(&dtrace_softstate); 16542 mutex_exit(&cpu_lock); 16543 mutex_exit(&dtrace_provider_lock); 16544 mutex_exit(&dtrace_lock); 16545 return (DDI_FAILURE); 16546 } 16547 16548 ddi_report_dev(devi); 16549 dtrace_devi = devi; 16550 16551 dtrace_modload = dtrace_module_loaded; 16552 dtrace_modunload = dtrace_module_unloaded; 16553 dtrace_cpu_init = dtrace_cpu_setup_initial; 16554 dtrace_helpers_cleanup = dtrace_helpers_destroy; 16555 dtrace_helpers_fork = dtrace_helpers_duplicate; 16556 dtrace_cpustart_init = dtrace_suspend; 16557 dtrace_cpustart_fini = dtrace_resume; 16558 dtrace_debugger_init = dtrace_suspend; 16559 dtrace_debugger_fini = dtrace_resume; 16560 16561 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16562 16563 ASSERT(MUTEX_HELD(&cpu_lock)); 16564 16565 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 16566 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 16567 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 16568 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 16569 VM_SLEEP | VMC_IDENTIFIER); 16570 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 16571 1, INT_MAX, 0); 16572 16573 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 16574 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 16575 NULL, NULL, NULL, NULL, NULL, 0); 16576 16577 ASSERT(MUTEX_HELD(&cpu_lock)); 16578 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 16579 offsetof(dtrace_probe_t, dtpr_nextmod), 16580 offsetof(dtrace_probe_t, dtpr_prevmod)); 16581 16582 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 16583 offsetof(dtrace_probe_t, dtpr_nextfunc), 16584 offsetof(dtrace_probe_t, dtpr_prevfunc)); 16585 16586 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 16587 offsetof(dtrace_probe_t, dtpr_nextname), 16588 offsetof(dtrace_probe_t, dtpr_prevname)); 16589 16590 if (dtrace_retain_max < 1) { 16591 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 16592 "setting to 1", dtrace_retain_max); 16593 dtrace_retain_max = 1; 16594 } 16595 16596 /* 16597 * Now discover our toxic ranges. 16598 */ 16599 dtrace_toxic_ranges(dtrace_toxrange_add); 16600 16601 /* 16602 * Before we register ourselves as a provider to our own framework, 16603 * we would like to assert that dtrace_provider is NULL -- but that's 16604 * not true if we were loaded as a dependency of a DTrace provider. 16605 * Once we've registered, we can assert that dtrace_provider is our 16606 * pseudo provider. 16607 */ 16608 (void) dtrace_register("dtrace", &dtrace_provider_attr, 16609 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 16610 16611 ASSERT(dtrace_provider != NULL); 16612 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 16613 16614 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 16615 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 16616 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 16617 dtrace_provider, NULL, NULL, "END", 0, NULL); 16618 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 16619 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 16620 16621 dtrace_anon_property(); 16622 mutex_exit(&cpu_lock); 16623 16624 /* 16625 * If DTrace helper tracing is enabled, we need to allocate the 16626 * trace buffer and initialize the values. 16627 */ 16628 if (dtrace_helptrace_enabled) { 16629 ASSERT(dtrace_helptrace_buffer == NULL); 16630 dtrace_helptrace_buffer = 16631 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 16632 dtrace_helptrace_next = 0; 16633 } 16634 16635 /* 16636 * If there are already providers, we must ask them to provide their 16637 * probes, and then match any anonymous enabling against them. Note 16638 * that there should be no other retained enablings at this time: 16639 * the only retained enablings at this time should be the anonymous 16640 * enabling. 16641 */ 16642 if (dtrace_anon.dta_enabling != NULL) { 16643 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 16644 16645 dtrace_enabling_provide(NULL); 16646 state = dtrace_anon.dta_state; 16647 16648 /* 16649 * We couldn't hold cpu_lock across the above call to 16650 * dtrace_enabling_provide(), but we must hold it to actually 16651 * enable the probes. We have to drop all of our locks, pick 16652 * up cpu_lock, and regain our locks before matching the 16653 * retained anonymous enabling. 16654 */ 16655 mutex_exit(&dtrace_lock); 16656 mutex_exit(&dtrace_provider_lock); 16657 16658 mutex_enter(&cpu_lock); 16659 mutex_enter(&dtrace_provider_lock); 16660 mutex_enter(&dtrace_lock); 16661 16662 if ((enab = dtrace_anon.dta_enabling) != NULL) 16663 (void) dtrace_enabling_match(enab, NULL); 16664 16665 mutex_exit(&cpu_lock); 16666 } 16667 16668 mutex_exit(&dtrace_lock); 16669 mutex_exit(&dtrace_provider_lock); 16670 16671 if (state != NULL) { 16672 /* 16673 * If we created any anonymous state, set it going now. 16674 */ 16675 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 16676 } 16677 16678 return (DDI_SUCCESS); 16679} 16680#endif 16681 16682#if !defined(sun) 16683#if __FreeBSD_version >= 800039 16684static void dtrace_dtr(void *); 16685#endif 16686#endif 16687 16688/*ARGSUSED*/ 16689static int 16690#if defined(sun) 16691dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 16692#else 16693dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 16694#endif 16695{ 16696 dtrace_state_t *state; 16697 uint32_t priv; 16698 uid_t uid; 16699 zoneid_t zoneid; 16700 16701#if defined(sun) 16702 if (getminor(*devp) == DTRACEMNRN_HELPER) 16703 return (0); 16704 16705 /* 16706 * If this wasn't an open with the "helper" minor, then it must be 16707 * the "dtrace" minor. 16708 */ 16709 if (getminor(*devp) == DTRACEMNRN_DTRACE) 16710 return (ENXIO); 16711#else 16712 cred_t *cred_p = NULL; 16713 16714#if __FreeBSD_version < 800039 16715 /* 16716 * The first minor device is the one that is cloned so there is 16717 * nothing more to do here. 16718 */ 16719 if (dev2unit(dev) == 0) 16720 return 0; 16721 16722 /* 16723 * Devices are cloned, so if the DTrace state has already 16724 * been allocated, that means this device belongs to a 16725 * different client. Each client should open '/dev/dtrace' 16726 * to get a cloned device. 16727 */ 16728 if (dev->si_drv1 != NULL) 16729 return (EBUSY); 16730#endif 16731 16732 cred_p = dev->si_cred; 16733#endif 16734 16735 /* 16736 * If no DTRACE_PRIV_* bits are set in the credential, then the 16737 * caller lacks sufficient permission to do anything with DTrace. 16738 */ 16739 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 16740 if (priv == DTRACE_PRIV_NONE) { 16741#if !defined(sun) 16742#if __FreeBSD_version < 800039 16743 /* Destroy the cloned device. */ 16744 destroy_dev(dev); 16745#endif 16746#endif 16747 16748 return (EACCES); 16749 } 16750 16751 /* 16752 * Ask all providers to provide all their probes. 16753 */ 16754 mutex_enter(&dtrace_provider_lock); 16755 dtrace_probe_provide(NULL, NULL); 16756 mutex_exit(&dtrace_provider_lock); 16757 16758 mutex_enter(&cpu_lock); 16759 mutex_enter(&dtrace_lock); 16760 dtrace_opens++; 16761 dtrace_membar_producer(); 16762 16763#if defined(sun) 16764 /* 16765 * If the kernel debugger is active (that is, if the kernel debugger 16766 * modified text in some way), we won't allow the open. 16767 */ 16768 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 16769 dtrace_opens--; 16770 mutex_exit(&cpu_lock); 16771 mutex_exit(&dtrace_lock); 16772 return (EBUSY); 16773 } 16774 16775 state = dtrace_state_create(devp, cred_p); 16776#else 16777 state = dtrace_state_create(dev); 16778#if __FreeBSD_version < 800039 16779 dev->si_drv1 = state; 16780#else 16781 devfs_set_cdevpriv(state, dtrace_dtr); 16782#endif 16783#endif 16784 16785 mutex_exit(&cpu_lock); 16786 16787 if (state == NULL) { 16788#if defined(sun) 16789 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 16790 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16791#else 16792 --dtrace_opens; 16793#endif 16794 mutex_exit(&dtrace_lock); 16795#if !defined(sun) 16796#if __FreeBSD_version < 800039 16797 /* Destroy the cloned device. */ 16798 destroy_dev(dev); 16799#endif 16800#endif 16801 return (EAGAIN); 16802 } 16803 16804 mutex_exit(&dtrace_lock); 16805 16806 return (0); 16807} 16808 16809/*ARGSUSED*/ 16810#if defined(sun) 16811static int 16812dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 16813#elif __FreeBSD_version < 800039 16814static int 16815dtrace_close(struct cdev *dev, int flags, int fmt __unused, struct thread *td) 16816#else 16817static void 16818dtrace_dtr(void *data) 16819#endif 16820{ 16821#if defined(sun) 16822 minor_t minor = getminor(dev); 16823 dtrace_state_t *state; 16824 16825 if (minor == DTRACEMNRN_HELPER) 16826 return (0); 16827 16828 state = ddi_get_soft_state(dtrace_softstate, minor); 16829#else 16830#if __FreeBSD_version < 800039 16831 dtrace_state_t *state = dev->si_drv1; 16832 16833 /* Check if this is not a cloned device. */ 16834 if (dev2unit(dev) == 0) 16835 return (0); 16836#else 16837 dtrace_state_t *state = data; 16838#endif 16839 16840#endif 16841 16842 mutex_enter(&cpu_lock); 16843 mutex_enter(&dtrace_lock); 16844 16845 if (state != NULL) { 16846 if (state->dts_anon) { 16847 /* 16848 * There is anonymous state. Destroy that first. 16849 */ 16850 ASSERT(dtrace_anon.dta_state == NULL); 16851 dtrace_state_destroy(state->dts_anon); 16852 } 16853 16854 dtrace_state_destroy(state); 16855 16856#if !defined(sun) 16857 kmem_free(state, 0); 16858#if __FreeBSD_version < 800039 16859 dev->si_drv1 = NULL; 16860#endif 16861#endif 16862 } 16863 16864 ASSERT(dtrace_opens > 0); 16865#if defined(sun) 16866 /* 16867 * Only relinquish control of the kernel debugger interface when there 16868 * are no consumers and no anonymous enablings. 16869 */ 16870 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 16871 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16872#else 16873 --dtrace_opens; 16874#endif 16875 16876 mutex_exit(&dtrace_lock); 16877 mutex_exit(&cpu_lock); 16878 16879#if __FreeBSD_version < 800039 16880 /* Schedule this cloned device to be destroyed. */ 16881 destroy_dev_sched(dev); 16882#endif 16883 16884#if defined(sun) || __FreeBSD_version < 800039 16885 return (0); 16886#endif 16887} 16888 16889#if defined(sun) 16890/*ARGSUSED*/ 16891static int 16892dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 16893{ 16894 int rval; 16895 dof_helper_t help, *dhp = NULL; 16896 16897 switch (cmd) { 16898 case DTRACEHIOC_ADDDOF: 16899 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 16900 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 16901 return (EFAULT); 16902 } 16903 16904 dhp = &help; 16905 arg = (intptr_t)help.dofhp_dof; 16906 /*FALLTHROUGH*/ 16907 16908 case DTRACEHIOC_ADD: { 16909 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 16910 16911 if (dof == NULL) 16912 return (rval); 16913 16914 mutex_enter(&dtrace_lock); 16915 16916 /* 16917 * dtrace_helper_slurp() takes responsibility for the dof -- 16918 * it may free it now or it may save it and free it later. 16919 */ 16920 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 16921 *rv = rval; 16922 rval = 0; 16923 } else { 16924 rval = EINVAL; 16925 } 16926 16927 mutex_exit(&dtrace_lock); 16928 return (rval); 16929 } 16930 16931 case DTRACEHIOC_REMOVE: { 16932 mutex_enter(&dtrace_lock); 16933 rval = dtrace_helper_destroygen(arg); 16934 mutex_exit(&dtrace_lock); 16935 16936 return (rval); 16937 } 16938 16939 default: 16940 break; 16941 } 16942 16943 return (ENOTTY); 16944} 16945 16946/*ARGSUSED*/ 16947static int 16948dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 16949{ 16950 minor_t minor = getminor(dev); 16951 dtrace_state_t *state; 16952 int rval; 16953 16954 if (minor == DTRACEMNRN_HELPER) 16955 return (dtrace_ioctl_helper(cmd, arg, rv)); 16956 16957 state = ddi_get_soft_state(dtrace_softstate, minor); 16958 16959 if (state->dts_anon) { 16960 ASSERT(dtrace_anon.dta_state == NULL); 16961 state = state->dts_anon; 16962 } 16963 16964 switch (cmd) { 16965 case DTRACEIOC_PROVIDER: { 16966 dtrace_providerdesc_t pvd; 16967 dtrace_provider_t *pvp; 16968 16969 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 16970 return (EFAULT); 16971 16972 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 16973 mutex_enter(&dtrace_provider_lock); 16974 16975 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 16976 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 16977 break; 16978 } 16979 16980 mutex_exit(&dtrace_provider_lock); 16981 16982 if (pvp == NULL) 16983 return (ESRCH); 16984 16985 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 16986 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 16987 16988 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 16989 return (EFAULT); 16990 16991 return (0); 16992 } 16993 16994 case DTRACEIOC_EPROBE: { 16995 dtrace_eprobedesc_t epdesc; 16996 dtrace_ecb_t *ecb; 16997 dtrace_action_t *act; 16998 void *buf; 16999 size_t size; 17000 uintptr_t dest; 17001 int nrecs; 17002 17003 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 17004 return (EFAULT); 17005 17006 mutex_enter(&dtrace_lock); 17007 17008 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 17009 mutex_exit(&dtrace_lock); 17010 return (EINVAL); 17011 } 17012 17013 if (ecb->dte_probe == NULL) { 17014 mutex_exit(&dtrace_lock); 17015 return (EINVAL); 17016 } 17017 17018 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 17019 epdesc.dtepd_uarg = ecb->dte_uarg; 17020 epdesc.dtepd_size = ecb->dte_size; 17021 17022 nrecs = epdesc.dtepd_nrecs; 17023 epdesc.dtepd_nrecs = 0; 17024 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17025 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17026 continue; 17027 17028 epdesc.dtepd_nrecs++; 17029 } 17030 17031 /* 17032 * Now that we have the size, we need to allocate a temporary 17033 * buffer in which to store the complete description. We need 17034 * the temporary buffer to be able to drop dtrace_lock() 17035 * across the copyout(), below. 17036 */ 17037 size = sizeof (dtrace_eprobedesc_t) + 17038 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 17039 17040 buf = kmem_alloc(size, KM_SLEEP); 17041 dest = (uintptr_t)buf; 17042 17043 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 17044 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 17045 17046 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17047 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17048 continue; 17049 17050 if (nrecs-- == 0) 17051 break; 17052 17053 bcopy(&act->dta_rec, (void *)dest, 17054 sizeof (dtrace_recdesc_t)); 17055 dest += sizeof (dtrace_recdesc_t); 17056 } 17057 17058 mutex_exit(&dtrace_lock); 17059 17060 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17061 kmem_free(buf, size); 17062 return (EFAULT); 17063 } 17064 17065 kmem_free(buf, size); 17066 return (0); 17067 } 17068 17069 case DTRACEIOC_AGGDESC: { 17070 dtrace_aggdesc_t aggdesc; 17071 dtrace_action_t *act; 17072 dtrace_aggregation_t *agg; 17073 int nrecs; 17074 uint32_t offs; 17075 dtrace_recdesc_t *lrec; 17076 void *buf; 17077 size_t size; 17078 uintptr_t dest; 17079 17080 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 17081 return (EFAULT); 17082 17083 mutex_enter(&dtrace_lock); 17084 17085 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 17086 mutex_exit(&dtrace_lock); 17087 return (EINVAL); 17088 } 17089 17090 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 17091 17092 nrecs = aggdesc.dtagd_nrecs; 17093 aggdesc.dtagd_nrecs = 0; 17094 17095 offs = agg->dtag_base; 17096 lrec = &agg->dtag_action.dta_rec; 17097 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 17098 17099 for (act = agg->dtag_first; ; act = act->dta_next) { 17100 ASSERT(act->dta_intuple || 17101 DTRACEACT_ISAGG(act->dta_kind)); 17102 17103 /* 17104 * If this action has a record size of zero, it 17105 * denotes an argument to the aggregating action. 17106 * Because the presence of this record doesn't (or 17107 * shouldn't) affect the way the data is interpreted, 17108 * we don't copy it out to save user-level the 17109 * confusion of dealing with a zero-length record. 17110 */ 17111 if (act->dta_rec.dtrd_size == 0) { 17112 ASSERT(agg->dtag_hasarg); 17113 continue; 17114 } 17115 17116 aggdesc.dtagd_nrecs++; 17117 17118 if (act == &agg->dtag_action) 17119 break; 17120 } 17121 17122 /* 17123 * Now that we have the size, we need to allocate a temporary 17124 * buffer in which to store the complete description. We need 17125 * the temporary buffer to be able to drop dtrace_lock() 17126 * across the copyout(), below. 17127 */ 17128 size = sizeof (dtrace_aggdesc_t) + 17129 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 17130 17131 buf = kmem_alloc(size, KM_SLEEP); 17132 dest = (uintptr_t)buf; 17133 17134 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 17135 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 17136 17137 for (act = agg->dtag_first; ; act = act->dta_next) { 17138 dtrace_recdesc_t rec = act->dta_rec; 17139 17140 /* 17141 * See the comment in the above loop for why we pass 17142 * over zero-length records. 17143 */ 17144 if (rec.dtrd_size == 0) { 17145 ASSERT(agg->dtag_hasarg); 17146 continue; 17147 } 17148 17149 if (nrecs-- == 0) 17150 break; 17151 17152 rec.dtrd_offset -= offs; 17153 bcopy(&rec, (void *)dest, sizeof (rec)); 17154 dest += sizeof (dtrace_recdesc_t); 17155 17156 if (act == &agg->dtag_action) 17157 break; 17158 } 17159 17160 mutex_exit(&dtrace_lock); 17161 17162 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17163 kmem_free(buf, size); 17164 return (EFAULT); 17165 } 17166 17167 kmem_free(buf, size); 17168 return (0); 17169 } 17170 17171 case DTRACEIOC_ENABLE: { 17172 dof_hdr_t *dof; 17173 dtrace_enabling_t *enab = NULL; 17174 dtrace_vstate_t *vstate; 17175 int err = 0; 17176 17177 *rv = 0; 17178 17179 /* 17180 * If a NULL argument has been passed, we take this as our 17181 * cue to reevaluate our enablings. 17182 */ 17183 if (arg == NULL) { 17184 dtrace_enabling_matchall(); 17185 17186 return (0); 17187 } 17188 17189 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 17190 return (rval); 17191 17192 mutex_enter(&cpu_lock); 17193 mutex_enter(&dtrace_lock); 17194 vstate = &state->dts_vstate; 17195 17196 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 17197 mutex_exit(&dtrace_lock); 17198 mutex_exit(&cpu_lock); 17199 dtrace_dof_destroy(dof); 17200 return (EBUSY); 17201 } 17202 17203 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 17204 mutex_exit(&dtrace_lock); 17205 mutex_exit(&cpu_lock); 17206 dtrace_dof_destroy(dof); 17207 return (EINVAL); 17208 } 17209 17210 if ((rval = dtrace_dof_options(dof, state)) != 0) { 17211 dtrace_enabling_destroy(enab); 17212 mutex_exit(&dtrace_lock); 17213 mutex_exit(&cpu_lock); 17214 dtrace_dof_destroy(dof); 17215 return (rval); 17216 } 17217 17218 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 17219 err = dtrace_enabling_retain(enab); 17220 } else { 17221 dtrace_enabling_destroy(enab); 17222 } 17223 17224 mutex_exit(&cpu_lock); 17225 mutex_exit(&dtrace_lock); 17226 dtrace_dof_destroy(dof); 17227 17228 return (err); 17229 } 17230 17231 case DTRACEIOC_REPLICATE: { 17232 dtrace_repldesc_t desc; 17233 dtrace_probedesc_t *match = &desc.dtrpd_match; 17234 dtrace_probedesc_t *create = &desc.dtrpd_create; 17235 int err; 17236 17237 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17238 return (EFAULT); 17239 17240 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17241 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17242 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17243 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17244 17245 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17246 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17247 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17248 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17249 17250 mutex_enter(&dtrace_lock); 17251 err = dtrace_enabling_replicate(state, match, create); 17252 mutex_exit(&dtrace_lock); 17253 17254 return (err); 17255 } 17256 17257 case DTRACEIOC_PROBEMATCH: 17258 case DTRACEIOC_PROBES: { 17259 dtrace_probe_t *probe = NULL; 17260 dtrace_probedesc_t desc; 17261 dtrace_probekey_t pkey; 17262 dtrace_id_t i; 17263 int m = 0; 17264 uint32_t priv; 17265 uid_t uid; 17266 zoneid_t zoneid; 17267 17268 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17269 return (EFAULT); 17270 17271 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17272 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17273 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17274 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17275 17276 /* 17277 * Before we attempt to match this probe, we want to give 17278 * all providers the opportunity to provide it. 17279 */ 17280 if (desc.dtpd_id == DTRACE_IDNONE) { 17281 mutex_enter(&dtrace_provider_lock); 17282 dtrace_probe_provide(&desc, NULL); 17283 mutex_exit(&dtrace_provider_lock); 17284 desc.dtpd_id++; 17285 } 17286 17287 if (cmd == DTRACEIOC_PROBEMATCH) { 17288 dtrace_probekey(&desc, &pkey); 17289 pkey.dtpk_id = DTRACE_IDNONE; 17290 } 17291 17292 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 17293 17294 mutex_enter(&dtrace_lock); 17295 17296 if (cmd == DTRACEIOC_PROBEMATCH) { 17297 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17298 if ((probe = dtrace_probes[i - 1]) != NULL && 17299 (m = dtrace_match_probe(probe, &pkey, 17300 priv, uid, zoneid)) != 0) 17301 break; 17302 } 17303 17304 if (m < 0) { 17305 mutex_exit(&dtrace_lock); 17306 return (EINVAL); 17307 } 17308 17309 } else { 17310 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17311 if ((probe = dtrace_probes[i - 1]) != NULL && 17312 dtrace_match_priv(probe, priv, uid, zoneid)) 17313 break; 17314 } 17315 } 17316 17317 if (probe == NULL) { 17318 mutex_exit(&dtrace_lock); 17319 return (ESRCH); 17320 } 17321 17322 dtrace_probe_description(probe, &desc); 17323 mutex_exit(&dtrace_lock); 17324 17325 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17326 return (EFAULT); 17327 17328 return (0); 17329 } 17330 17331 case DTRACEIOC_PROBEARG: { 17332 dtrace_argdesc_t desc; 17333 dtrace_probe_t *probe; 17334 dtrace_provider_t *prov; 17335 17336 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17337 return (EFAULT); 17338 17339 if (desc.dtargd_id == DTRACE_IDNONE) 17340 return (EINVAL); 17341 17342 if (desc.dtargd_ndx == DTRACE_ARGNONE) 17343 return (EINVAL); 17344 17345 mutex_enter(&dtrace_provider_lock); 17346 mutex_enter(&mod_lock); 17347 mutex_enter(&dtrace_lock); 17348 17349 if (desc.dtargd_id > dtrace_nprobes) { 17350 mutex_exit(&dtrace_lock); 17351 mutex_exit(&mod_lock); 17352 mutex_exit(&dtrace_provider_lock); 17353 return (EINVAL); 17354 } 17355 17356 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 17357 mutex_exit(&dtrace_lock); 17358 mutex_exit(&mod_lock); 17359 mutex_exit(&dtrace_provider_lock); 17360 return (EINVAL); 17361 } 17362 17363 mutex_exit(&dtrace_lock); 17364 17365 prov = probe->dtpr_provider; 17366 17367 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 17368 /* 17369 * There isn't any typed information for this probe. 17370 * Set the argument number to DTRACE_ARGNONE. 17371 */ 17372 desc.dtargd_ndx = DTRACE_ARGNONE; 17373 } else { 17374 desc.dtargd_native[0] = '\0'; 17375 desc.dtargd_xlate[0] = '\0'; 17376 desc.dtargd_mapping = desc.dtargd_ndx; 17377 17378 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 17379 probe->dtpr_id, probe->dtpr_arg, &desc); 17380 } 17381 17382 mutex_exit(&mod_lock); 17383 mutex_exit(&dtrace_provider_lock); 17384 17385 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17386 return (EFAULT); 17387 17388 return (0); 17389 } 17390 17391 case DTRACEIOC_GO: { 17392 processorid_t cpuid; 17393 rval = dtrace_state_go(state, &cpuid); 17394 17395 if (rval != 0) 17396 return (rval); 17397 17398 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17399 return (EFAULT); 17400 17401 return (0); 17402 } 17403 17404 case DTRACEIOC_STOP: { 17405 processorid_t cpuid; 17406 17407 mutex_enter(&dtrace_lock); 17408 rval = dtrace_state_stop(state, &cpuid); 17409 mutex_exit(&dtrace_lock); 17410 17411 if (rval != 0) 17412 return (rval); 17413 17414 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17415 return (EFAULT); 17416 17417 return (0); 17418 } 17419 17420 case DTRACEIOC_DOFGET: { 17421 dof_hdr_t hdr, *dof; 17422 uint64_t len; 17423 17424 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 17425 return (EFAULT); 17426 17427 mutex_enter(&dtrace_lock); 17428 dof = dtrace_dof_create(state); 17429 mutex_exit(&dtrace_lock); 17430 17431 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 17432 rval = copyout(dof, (void *)arg, len); 17433 dtrace_dof_destroy(dof); 17434 17435 return (rval == 0 ? 0 : EFAULT); 17436 } 17437 17438 case DTRACEIOC_AGGSNAP: 17439 case DTRACEIOC_BUFSNAP: { 17440 dtrace_bufdesc_t desc; 17441 caddr_t cached; 17442 dtrace_buffer_t *buf; 17443 17444 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17445 return (EFAULT); 17446 17447 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 17448 return (EINVAL); 17449 17450 mutex_enter(&dtrace_lock); 17451 17452 if (cmd == DTRACEIOC_BUFSNAP) { 17453 buf = &state->dts_buffer[desc.dtbd_cpu]; 17454 } else { 17455 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 17456 } 17457 17458 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 17459 size_t sz = buf->dtb_offset; 17460 17461 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 17462 mutex_exit(&dtrace_lock); 17463 return (EBUSY); 17464 } 17465 17466 /* 17467 * If this buffer has already been consumed, we're 17468 * going to indicate that there's nothing left here 17469 * to consume. 17470 */ 17471 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 17472 mutex_exit(&dtrace_lock); 17473 17474 desc.dtbd_size = 0; 17475 desc.dtbd_drops = 0; 17476 desc.dtbd_errors = 0; 17477 desc.dtbd_oldest = 0; 17478 sz = sizeof (desc); 17479 17480 if (copyout(&desc, (void *)arg, sz) != 0) 17481 return (EFAULT); 17482 17483 return (0); 17484 } 17485 17486 /* 17487 * If this is a ring buffer that has wrapped, we want 17488 * to copy the whole thing out. 17489 */ 17490 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 17491 dtrace_buffer_polish(buf); 17492 sz = buf->dtb_size; 17493 } 17494 17495 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 17496 mutex_exit(&dtrace_lock); 17497 return (EFAULT); 17498 } 17499 17500 desc.dtbd_size = sz; 17501 desc.dtbd_drops = buf->dtb_drops; 17502 desc.dtbd_errors = buf->dtb_errors; 17503 desc.dtbd_oldest = buf->dtb_xamot_offset; 17504 desc.dtbd_timestamp = dtrace_gethrtime(); 17505 17506 mutex_exit(&dtrace_lock); 17507 17508 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17509 return (EFAULT); 17510 17511 buf->dtb_flags |= DTRACEBUF_CONSUMED; 17512 17513 return (0); 17514 } 17515 17516 if (buf->dtb_tomax == NULL) { 17517 ASSERT(buf->dtb_xamot == NULL); 17518 mutex_exit(&dtrace_lock); 17519 return (ENOENT); 17520 } 17521 17522 cached = buf->dtb_tomax; 17523 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 17524 17525 dtrace_xcall(desc.dtbd_cpu, 17526 (dtrace_xcall_t)dtrace_buffer_switch, buf); 17527 17528 state->dts_errors += buf->dtb_xamot_errors; 17529 17530 /* 17531 * If the buffers did not actually switch, then the cross call 17532 * did not take place -- presumably because the given CPU is 17533 * not in the ready set. If this is the case, we'll return 17534 * ENOENT. 17535 */ 17536 if (buf->dtb_tomax == cached) { 17537 ASSERT(buf->dtb_xamot != cached); 17538 mutex_exit(&dtrace_lock); 17539 return (ENOENT); 17540 } 17541 17542 ASSERT(cached == buf->dtb_xamot); 17543 17544 /* 17545 * We have our snapshot; now copy it out. 17546 */ 17547 if (copyout(buf->dtb_xamot, desc.dtbd_data, 17548 buf->dtb_xamot_offset) != 0) { 17549 mutex_exit(&dtrace_lock); 17550 return (EFAULT); 17551 } 17552 17553 desc.dtbd_size = buf->dtb_xamot_offset; 17554 desc.dtbd_drops = buf->dtb_xamot_drops; 17555 desc.dtbd_errors = buf->dtb_xamot_errors; 17556 desc.dtbd_oldest = 0; 17557 desc.dtbd_timestamp = buf->dtb_switched; 17558 17559 mutex_exit(&dtrace_lock); 17560 17561 /* 17562 * Finally, copy out the buffer description. 17563 */ 17564 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17565 return (EFAULT); 17566 17567 return (0); 17568 } 17569 17570 case DTRACEIOC_CONF: { 17571 dtrace_conf_t conf; 17572 17573 bzero(&conf, sizeof (conf)); 17574 conf.dtc_difversion = DIF_VERSION; 17575 conf.dtc_difintregs = DIF_DIR_NREGS; 17576 conf.dtc_diftupregs = DIF_DTR_NREGS; 17577 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 17578 17579 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 17580 return (EFAULT); 17581 17582 return (0); 17583 } 17584 17585 case DTRACEIOC_STATUS: { 17586 dtrace_status_t stat; 17587 dtrace_dstate_t *dstate; 17588 int i, j; 17589 uint64_t nerrs; 17590 17591 /* 17592 * See the comment in dtrace_state_deadman() for the reason 17593 * for setting dts_laststatus to INT64_MAX before setting 17594 * it to the correct value. 17595 */ 17596 state->dts_laststatus = INT64_MAX; 17597 dtrace_membar_producer(); 17598 state->dts_laststatus = dtrace_gethrtime(); 17599 17600 bzero(&stat, sizeof (stat)); 17601 17602 mutex_enter(&dtrace_lock); 17603 17604 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 17605 mutex_exit(&dtrace_lock); 17606 return (ENOENT); 17607 } 17608 17609 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 17610 stat.dtst_exiting = 1; 17611 17612 nerrs = state->dts_errors; 17613 dstate = &state->dts_vstate.dtvs_dynvars; 17614 17615 for (i = 0; i < NCPU; i++) { 17616 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 17617 17618 stat.dtst_dyndrops += dcpu->dtdsc_drops; 17619 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 17620 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 17621 17622 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 17623 stat.dtst_filled++; 17624 17625 nerrs += state->dts_buffer[i].dtb_errors; 17626 17627 for (j = 0; j < state->dts_nspeculations; j++) { 17628 dtrace_speculation_t *spec; 17629 dtrace_buffer_t *buf; 17630 17631 spec = &state->dts_speculations[j]; 17632 buf = &spec->dtsp_buffer[i]; 17633 stat.dtst_specdrops += buf->dtb_xamot_drops; 17634 } 17635 } 17636 17637 stat.dtst_specdrops_busy = state->dts_speculations_busy; 17638 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 17639 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 17640 stat.dtst_dblerrors = state->dts_dblerrors; 17641 stat.dtst_killed = 17642 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 17643 stat.dtst_errors = nerrs; 17644 17645 mutex_exit(&dtrace_lock); 17646 17647 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 17648 return (EFAULT); 17649 17650 return (0); 17651 } 17652 17653 case DTRACEIOC_FORMAT: { 17654 dtrace_fmtdesc_t fmt; 17655 char *str; 17656 int len; 17657 17658 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 17659 return (EFAULT); 17660 17661 mutex_enter(&dtrace_lock); 17662 17663 if (fmt.dtfd_format == 0 || 17664 fmt.dtfd_format > state->dts_nformats) { 17665 mutex_exit(&dtrace_lock); 17666 return (EINVAL); 17667 } 17668 17669 /* 17670 * Format strings are allocated contiguously and they are 17671 * never freed; if a format index is less than the number 17672 * of formats, we can assert that the format map is non-NULL 17673 * and that the format for the specified index is non-NULL. 17674 */ 17675 ASSERT(state->dts_formats != NULL); 17676 str = state->dts_formats[fmt.dtfd_format - 1]; 17677 ASSERT(str != NULL); 17678 17679 len = strlen(str) + 1; 17680 17681 if (len > fmt.dtfd_length) { 17682 fmt.dtfd_length = len; 17683 17684 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 17685 mutex_exit(&dtrace_lock); 17686 return (EINVAL); 17687 } 17688 } else { 17689 if (copyout(str, fmt.dtfd_string, len) != 0) { 17690 mutex_exit(&dtrace_lock); 17691 return (EINVAL); 17692 } 17693 } 17694 17695 mutex_exit(&dtrace_lock); 17696 return (0); 17697 } 17698 17699 default: 17700 break; 17701 } 17702 17703 return (ENOTTY); 17704} 17705 17706/*ARGSUSED*/ 17707static int 17708dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 17709{ 17710 dtrace_state_t *state; 17711 17712 switch (cmd) { 17713 case DDI_DETACH: 17714 break; 17715 17716 case DDI_SUSPEND: 17717 return (DDI_SUCCESS); 17718 17719 default: 17720 return (DDI_FAILURE); 17721 } 17722 17723 mutex_enter(&cpu_lock); 17724 mutex_enter(&dtrace_provider_lock); 17725 mutex_enter(&dtrace_lock); 17726 17727 ASSERT(dtrace_opens == 0); 17728 17729 if (dtrace_helpers > 0) { 17730 mutex_exit(&dtrace_provider_lock); 17731 mutex_exit(&dtrace_lock); 17732 mutex_exit(&cpu_lock); 17733 return (DDI_FAILURE); 17734 } 17735 17736 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 17737 mutex_exit(&dtrace_provider_lock); 17738 mutex_exit(&dtrace_lock); 17739 mutex_exit(&cpu_lock); 17740 return (DDI_FAILURE); 17741 } 17742 17743 dtrace_provider = NULL; 17744 17745 if ((state = dtrace_anon_grab()) != NULL) { 17746 /* 17747 * If there were ECBs on this state, the provider should 17748 * have not been allowed to detach; assert that there is 17749 * none. 17750 */ 17751 ASSERT(state->dts_necbs == 0); 17752 dtrace_state_destroy(state); 17753 17754 /* 17755 * If we're being detached with anonymous state, we need to 17756 * indicate to the kernel debugger that DTrace is now inactive. 17757 */ 17758 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17759 } 17760 17761 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 17762 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 17763 dtrace_cpu_init = NULL; 17764 dtrace_helpers_cleanup = NULL; 17765 dtrace_helpers_fork = NULL; 17766 dtrace_cpustart_init = NULL; 17767 dtrace_cpustart_fini = NULL; 17768 dtrace_debugger_init = NULL; 17769 dtrace_debugger_fini = NULL; 17770 dtrace_modload = NULL; 17771 dtrace_modunload = NULL; 17772 17773 ASSERT(dtrace_getf == 0); 17774 ASSERT(dtrace_closef == NULL); 17775 17776 mutex_exit(&cpu_lock); 17777 17778 if (dtrace_helptrace_enabled) { 17779 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 17780 dtrace_helptrace_buffer = NULL; 17781 } 17782 17783 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 17784 dtrace_probes = NULL; 17785 dtrace_nprobes = 0; 17786 17787 dtrace_hash_destroy(dtrace_bymod); 17788 dtrace_hash_destroy(dtrace_byfunc); 17789 dtrace_hash_destroy(dtrace_byname); 17790 dtrace_bymod = NULL; 17791 dtrace_byfunc = NULL; 17792 dtrace_byname = NULL; 17793 17794 kmem_cache_destroy(dtrace_state_cache); 17795 vmem_destroy(dtrace_minor); 17796 vmem_destroy(dtrace_arena); 17797 17798 if (dtrace_toxrange != NULL) { 17799 kmem_free(dtrace_toxrange, 17800 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 17801 dtrace_toxrange = NULL; 17802 dtrace_toxranges = 0; 17803 dtrace_toxranges_max = 0; 17804 } 17805 17806 ddi_remove_minor_node(dtrace_devi, NULL); 17807 dtrace_devi = NULL; 17808 17809 ddi_soft_state_fini(&dtrace_softstate); 17810 17811 ASSERT(dtrace_vtime_references == 0); 17812 ASSERT(dtrace_opens == 0); 17813 ASSERT(dtrace_retained == NULL); 17814 17815 mutex_exit(&dtrace_lock); 17816 mutex_exit(&dtrace_provider_lock); 17817 17818 /* 17819 * We don't destroy the task queue until after we have dropped our 17820 * locks (taskq_destroy() may block on running tasks). To prevent 17821 * attempting to do work after we have effectively detached but before 17822 * the task queue has been destroyed, all tasks dispatched via the 17823 * task queue must check that DTrace is still attached before 17824 * performing any operation. 17825 */ 17826 taskq_destroy(dtrace_taskq); 17827 dtrace_taskq = NULL; 17828 17829 return (DDI_SUCCESS); 17830} 17831#endif 17832 17833#if defined(sun) 17834/*ARGSUSED*/ 17835static int 17836dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 17837{ 17838 int error; 17839 17840 switch (infocmd) { 17841 case DDI_INFO_DEVT2DEVINFO: 17842 *result = (void *)dtrace_devi; 17843 error = DDI_SUCCESS; 17844 break; 17845 case DDI_INFO_DEVT2INSTANCE: 17846 *result = (void *)0; 17847 error = DDI_SUCCESS; 17848 break; 17849 default: 17850 error = DDI_FAILURE; 17851 } 17852 return (error); 17853} 17854#endif 17855 17856#if defined(sun) 17857static struct cb_ops dtrace_cb_ops = { 17858 dtrace_open, /* open */ 17859 dtrace_close, /* close */ 17860 nulldev, /* strategy */ 17861 nulldev, /* print */ 17862 nodev, /* dump */ 17863 nodev, /* read */ 17864 nodev, /* write */ 17865 dtrace_ioctl, /* ioctl */ 17866 nodev, /* devmap */ 17867 nodev, /* mmap */ 17868 nodev, /* segmap */ 17869 nochpoll, /* poll */ 17870 ddi_prop_op, /* cb_prop_op */ 17871 0, /* streamtab */ 17872 D_NEW | D_MP /* Driver compatibility flag */ 17873}; 17874 17875static struct dev_ops dtrace_ops = { 17876 DEVO_REV, /* devo_rev */ 17877 0, /* refcnt */ 17878 dtrace_info, /* get_dev_info */ 17879 nulldev, /* identify */ 17880 nulldev, /* probe */ 17881 dtrace_attach, /* attach */ 17882 dtrace_detach, /* detach */ 17883 nodev, /* reset */ 17884 &dtrace_cb_ops, /* driver operations */ 17885 NULL, /* bus operations */ 17886 nodev /* dev power */ 17887}; 17888 17889static struct modldrv modldrv = { 17890 &mod_driverops, /* module type (this is a pseudo driver) */ 17891 "Dynamic Tracing", /* name of module */ 17892 &dtrace_ops, /* driver ops */ 17893}; 17894 17895static struct modlinkage modlinkage = { 17896 MODREV_1, 17897 (void *)&modldrv, 17898 NULL 17899}; 17900 17901int 17902_init(void) 17903{ 17904 return (mod_install(&modlinkage)); 17905} 17906 17907int 17908_info(struct modinfo *modinfop) 17909{ 17910 return (mod_info(&modlinkage, modinfop)); 17911} 17912 17913int 17914_fini(void) 17915{ 17916 return (mod_remove(&modlinkage)); 17917} 17918#else 17919 17920static d_ioctl_t dtrace_ioctl; 17921static d_ioctl_t dtrace_ioctl_helper; 17922static void dtrace_load(void *); 17923static int dtrace_unload(void); 17924#if __FreeBSD_version < 800039 17925static void dtrace_clone(void *, struct ucred *, char *, int , struct cdev **); 17926static struct clonedevs *dtrace_clones; /* Ptr to the array of cloned devices. */ 17927static eventhandler_tag eh_tag; /* Event handler tag. */ 17928#else 17929static struct cdev *dtrace_dev; 17930static struct cdev *helper_dev; 17931#endif 17932 17933void dtrace_invop_init(void); 17934void dtrace_invop_uninit(void); 17935 17936static struct cdevsw dtrace_cdevsw = { 17937 .d_version = D_VERSION, 17938#if __FreeBSD_version < 800039 17939 .d_flags = D_TRACKCLOSE | D_NEEDMINOR, 17940 .d_close = dtrace_close, 17941#endif 17942 .d_ioctl = dtrace_ioctl, 17943 .d_open = dtrace_open, 17944 .d_name = "dtrace", 17945}; 17946 17947static struct cdevsw helper_cdevsw = { 17948 .d_version = D_VERSION, 17949 .d_ioctl = dtrace_ioctl_helper, 17950 .d_name = "helper", 17951}; 17952 17953#include <dtrace_anon.c> 17954#if __FreeBSD_version < 800039 17955#include <dtrace_clone.c> 17956#endif 17957#include <dtrace_ioctl.c> 17958#include <dtrace_load.c> 17959#include <dtrace_modevent.c> 17960#include <dtrace_sysctl.c> 17961#include <dtrace_unload.c> 17962#include <dtrace_vtime.c> 17963#include <dtrace_hacks.c> 17964#include <dtrace_isa.c> 17965 17966SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 17967SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 17968SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 17969 17970DEV_MODULE(dtrace, dtrace_modevent, NULL); 17971MODULE_VERSION(dtrace, 1); 17972MODULE_DEPEND(dtrace, cyclic, 1, 1, 1); 17973MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 17974#endif 17975