1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * DTrace - Dynamic Tracing for Solaris 30 * 31 * This is the implementation of the Solaris Dynamic Tracing framework 32 * (DTrace). The user-visible interface to DTrace is described at length in 33 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 34 * library, the in-kernel DTrace framework, and the DTrace providers are 35 * described in the block comments in the <sys/dtrace.h> header file. The 36 * internal architecture of DTrace is described in the block comments in the 37 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 38 * implementation very much assume mastery of all of these sources; if one has 39 * an unanswered question about the implementation, one should consult them 40 * first. 41 * 42 * The functions here are ordered roughly as follows: 43 * 44 * - Probe context functions 45 * - Probe hashing functions 46 * - Non-probe context utility functions 47 * - Matching functions 48 * - Provider-to-Framework API functions 49 * - Probe management functions 50 * - DIF object functions 51 * - Format functions 52 * - Predicate functions 53 * - ECB functions 54 * - Buffer functions 55 * - Enabling functions 56 * - DOF functions 57 * - Anonymous enabling functions 58 * - Consumer state functions 59 * - Helper functions 60 * - Hook functions 61 * - Driver cookbook functions 62 * 63 * Each group of functions begins with a block comment labelled the "DTrace 64 * [Group] Functions", allowing one to find each block by searching forward 65 * on capital-f functions. 66 */ 67 #include <sys/errno.h> 68 #include <sys/stat.h> 69 #include <sys/modctl.h> 70 #include <sys/conf.h> 71 #include <sys/systm.h> 72 #include <sys/ddi.h> 73 #include <sys/sunddi.h> 74 #include <sys/cpuvar.h> 75 #include <sys/kmem.h> 76 #include <sys/strsubr.h> 77 #include <sys/sysmacros.h> 78 #include <sys/dtrace_impl.h> 79 #include <sys/atomic.h> 80 #include <sys/cmn_err.h> 81 #include <sys/mutex_impl.h> 82 #include <sys/rwlock_impl.h> 83 #include <sys/ctf_api.h> 84 #include <sys/panic.h> 85 #include <sys/priv_impl.h> 86 #include <sys/policy.h> 87 #include <sys/cred_impl.h> 88 #include <sys/procfs_isa.h> 89 #include <sys/taskq.h> 90 #include <sys/mkdev.h> 91 #include <sys/kdi.h> 92 #include <sys/zone.h> 93 94 /* 95 * DTrace Tunable Variables 96 * 97 * The following variables may be tuned by adding a line to /etc/system that 98 * includes both the name of the DTrace module ("dtrace") and the name of the 99 * variable. For example: 100 * 101 * set dtrace:dtrace_destructive_disallow = 1 102 * 103 * In general, the only variables that one should be tuning this way are those 104 * that affect system-wide DTrace behavior, and for which the default behavior 105 * is undesirable. Most of these variables are tunable on a per-consumer 106 * basis using DTrace options, and need not be tuned on a system-wide basis. 107 * When tuning these variables, avoid pathological values; while some attempt 108 * is made to verify the integrity of these variables, they are not considered 109 * part of the supported interface to DTrace, and they are therefore not 110 * checked comprehensively. Further, these variables should not be tuned 111 * dynamically via "mdb -kw" or other means; they should only be tuned via 112 * /etc/system. 113 */ 114 int dtrace_destructive_disallow = 0; 115 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 116 size_t dtrace_difo_maxsize = (256 * 1024); 117 dtrace_optval_t dtrace_dof_maxsize = (256 * 1024); 118 size_t dtrace_global_maxsize = (16 * 1024); 119 size_t dtrace_actions_max = (16 * 1024); 120 size_t dtrace_retain_max = 1024; 121 dtrace_optval_t dtrace_helper_actions_max = 32; 122 dtrace_optval_t dtrace_helper_providers_max = 32; 123 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 124 size_t dtrace_strsize_default = 256; 125 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 126 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 127 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 128 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 129 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 130 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 131 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 132 dtrace_optval_t dtrace_nspec_default = 1; 133 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 134 dtrace_optval_t dtrace_stackframes_default = 20; 135 dtrace_optval_t dtrace_ustackframes_default = 20; 136 dtrace_optval_t dtrace_jstackframes_default = 50; 137 dtrace_optval_t dtrace_jstackstrsize_default = 512; 138 int dtrace_msgdsize_max = 128; 139 hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 140 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 141 int dtrace_devdepth_max = 32; 142 int dtrace_err_verbose; 143 hrtime_t dtrace_deadman_interval = NANOSEC; 144 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 145 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 146 147 /* 148 * DTrace External Variables 149 * 150 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 151 * available to DTrace consumers via the backtick (`) syntax. One of these, 152 * dtrace_zero, is made deliberately so: it is provided as a source of 153 * well-known, zero-filled memory. While this variable is not documented, 154 * it is used by some translators as an implementation detail. 155 */ 156 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 157 158 /* 159 * DTrace Internal Variables 160 */ 161 static dev_info_t *dtrace_devi; /* device info */ 162 static vmem_t *dtrace_arena; /* probe ID arena */ 163 static vmem_t *dtrace_minor; /* minor number arena */ 164 static taskq_t *dtrace_taskq; /* task queue */ 165 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 166 static int dtrace_nprobes; /* number of probes */ 167 static dtrace_provider_t *dtrace_provider; /* provider list */ 168 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 169 static int dtrace_opens; /* number of opens */ 170 static int dtrace_helpers; /* number of helpers */ 171 static void *dtrace_softstate; /* softstate pointer */ 172 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 173 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 174 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 175 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 176 static int dtrace_toxranges; /* number of toxic ranges */ 177 static int dtrace_toxranges_max; /* size of toxic range array */ 178 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 179 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 180 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 181 static kthread_t *dtrace_panicked; /* panicking thread */ 182 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 183 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 184 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 185 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 186 static dtrace_state_t *dtrace_state; /* temporary variable */ 187 static int dtrace_err; /* temporary variable */ 188 189 /* 190 * DTrace Locking 191 * DTrace is protected by three (relatively coarse-grained) locks: 192 * 193 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 194 * including enabling state, probes, ECBs, consumer state, helper state, 195 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 196 * probe context is lock-free -- synchronization is handled via the 197 * dtrace_sync() cross call mechanism. 198 * 199 * (2) dtrace_provider_lock is required when manipulating provider state, or 200 * when provider state must be held constant. 201 * 202 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 203 * when meta provider state must be held constant. 204 * 205 * The lock ordering between these three locks is dtrace_meta_lock before 206 * dtrace_provider_lock before dtrace_lock. (In particular, there are 207 * several places where dtrace_provider_lock is held by the framework as it 208 * calls into the providers -- which then call back into the framework, 209 * grabbing dtrace_lock.) 210 * 211 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 212 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 213 * role as a coarse-grained lock; it is acquired before both of these locks. 214 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 215 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 216 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 217 * acquired _between_ dtrace_provider_lock and dtrace_lock. 218 */ 219 static kmutex_t dtrace_lock; /* probe state lock */ 220 static kmutex_t dtrace_provider_lock; /* provider state lock */ 221 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 222 223 /* 224 * DTrace Provider Variables 225 * 226 * These are the variables relating to DTrace as a provider (that is, the 227 * provider of the BEGIN, END, and ERROR probes). 228 */ 229 static dtrace_pattr_t dtrace_provider_attr = { 230 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 231 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 232 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 233 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 234 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 235 }; 236 237 static void 238 dtrace_nullop(void) 239 {} 240 241 static dtrace_pops_t dtrace_provider_ops = { 242 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, 243 (void (*)(void *, struct modctl *))dtrace_nullop, 244 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 245 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 246 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 247 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 248 NULL, 249 NULL, 250 NULL, 251 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 252 }; 253 254 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 255 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 256 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 257 258 /* 259 * DTrace Helper Tracing Variables 260 */ 261 uint32_t dtrace_helptrace_next = 0; 262 uint32_t dtrace_helptrace_nlocals; 263 char *dtrace_helptrace_buffer; 264 int dtrace_helptrace_bufsize = 512 * 1024; 265 266 #ifdef DEBUG 267 int dtrace_helptrace_enabled = 1; 268 #else 269 int dtrace_helptrace_enabled = 0; 270 #endif 271 272 /* 273 * DTrace Error Hashing 274 * 275 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 276 * table. This is very useful for checking coverage of tests that are 277 * expected to induce DIF or DOF processing errors, and may be useful for 278 * debugging problems in the DIF code generator or in DOF generation . The 279 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 280 */ 281 #ifdef DEBUG 282 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 283 static const char *dtrace_errlast; 284 static kthread_t *dtrace_errthread; 285 static kmutex_t dtrace_errlock; 286 #endif 287 288 /* 289 * DTrace Macros and Constants 290 * 291 * These are various macros that are useful in various spots in the 292 * implementation, along with a few random constants that have no meaning 293 * outside of the implementation. There is no real structure to this cpp 294 * mishmash -- but is there ever? 295 */ 296 #define DTRACE_HASHSTR(hash, probe) \ 297 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 298 299 #define DTRACE_HASHNEXT(hash, probe) \ 300 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 301 302 #define DTRACE_HASHPREV(hash, probe) \ 303 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 304 305 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 306 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 307 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 308 309 #define DTRACE_AGGHASHSIZE_SLEW 17 310 311 /* 312 * The key for a thread-local variable consists of the lower 61 bits of the 313 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 314 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 315 * equal to a variable identifier. This is necessary (but not sufficient) to 316 * assure that global associative arrays never collide with thread-local 317 * variables. To guarantee that they cannot collide, we must also define the 318 * order for keying dynamic variables. That order is: 319 * 320 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 321 * 322 * Because the variable-key and the tls-key are in orthogonal spaces, there is 323 * no way for a global variable key signature to match a thread-local key 324 * signature. 325 */ 326 #define DTRACE_TLS_THRKEY(where) { \ 327 uint_t intr = 0; \ 328 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 329 for (; actv; actv >>= 1) \ 330 intr++; \ 331 ASSERT(intr < (1 << 3)); \ 332 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 333 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 334 } 335 336 #define DTRACE_STORE(type, tomax, offset, what) \ 337 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 338 339 #ifndef __i386 340 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 341 if (addr & (size - 1)) { \ 342 *flags |= CPU_DTRACE_BADALIGN; \ 343 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 344 return (0); \ 345 } 346 #else 347 #define DTRACE_ALIGNCHECK(addr, size, flags) 348 #endif 349 350 #define DTRACE_LOADFUNC(bits) \ 351 /*CSTYLED*/ \ 352 uint##bits##_t \ 353 dtrace_load##bits(uintptr_t addr) \ 354 { \ 355 size_t size = bits / NBBY; \ 356 /*CSTYLED*/ \ 357 uint##bits##_t rval; \ 358 int i; \ 359 volatile uint16_t *flags = (volatile uint16_t *) \ 360 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ 361 \ 362 DTRACE_ALIGNCHECK(addr, size, flags); \ 363 \ 364 for (i = 0; i < dtrace_toxranges; i++) { \ 365 if (addr >= dtrace_toxrange[i].dtt_limit) \ 366 continue; \ 367 \ 368 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 369 continue; \ 370 \ 371 /* \ 372 * This address falls within a toxic region; return 0. \ 373 */ \ 374 *flags |= CPU_DTRACE_BADADDR; \ 375 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 376 return (0); \ 377 } \ 378 \ 379 *flags |= CPU_DTRACE_NOFAULT; \ 380 /*CSTYLED*/ \ 381 rval = *((volatile uint##bits##_t *)addr); \ 382 *flags &= ~CPU_DTRACE_NOFAULT; \ 383 \ 384 return (rval); \ 385 } 386 387 #ifdef _LP64 388 #define dtrace_loadptr dtrace_load64 389 #else 390 #define dtrace_loadptr dtrace_load32 391 #endif 392 393 #define DTRACE_MATCH_NEXT 0 394 #define DTRACE_MATCH_DONE 1 395 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 396 #define DTRACE_STATE_ALIGN 64 397 398 #define DTRACE_FLAGS2FLT(flags) \ 399 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 400 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 401 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 402 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 403 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 404 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 405 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 406 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 407 DTRACEFLT_UNKNOWN) 408 409 #define DTRACEACT_ISSTRING(act) \ 410 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 411 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 412 413 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 414 static void dtrace_enabling_provide(dtrace_provider_t *); 415 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 416 static void dtrace_enabling_matchall(void); 417 static dtrace_state_t *dtrace_anon_grab(void); 418 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 419 dtrace_state_t *, uint64_t, uint64_t); 420 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 421 static void dtrace_buffer_drop(dtrace_buffer_t *); 422 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 423 dtrace_state_t *, dtrace_mstate_t *); 424 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 425 dtrace_optval_t); 426 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 427 428 /* 429 * DTrace Probe Context Functions 430 * 431 * These functions are called from probe context. Because probe context is 432 * any context in which C may be called, arbitrarily locks may be held, 433 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 434 * As a result, functions called from probe context may only call other DTrace 435 * support functions -- they may not interact at all with the system at large. 436 * (Note that the ASSERT macro is made probe-context safe by redefining it in 437 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 438 * loads are to be performed from probe context, they _must_ be in terms of 439 * the safe dtrace_load*() variants. 440 * 441 * Some functions in this block are not actually called from probe context; 442 * for these functions, there will be a comment above the function reading 443 * "Note: not called from probe context." 444 */ 445 void 446 dtrace_panic(const char *format, ...) 447 { 448 va_list alist; 449 450 va_start(alist, format); 451 dtrace_vpanic(format, alist); 452 va_end(alist); 453 } 454 455 int 456 dtrace_assfail(const char *a, const char *f, int l) 457 { 458 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 459 460 /* 461 * We just need something here that even the most clever compiler 462 * cannot optimize away. 463 */ 464 return (a[(uintptr_t)f]); 465 } 466 467 /* 468 * Atomically increment a specified error counter from probe context. 469 */ 470 static void 471 dtrace_error(uint32_t *counter) 472 { 473 /* 474 * Most counters stored to in probe context are per-CPU counters. 475 * However, there are some error conditions that are sufficiently 476 * arcane that they don't merit per-CPU storage. If these counters 477 * are incremented concurrently on different CPUs, scalability will be 478 * adversely affected -- but we don't expect them to be white-hot in a 479 * correctly constructed enabling... 480 */ 481 uint32_t oval, nval; 482 483 do { 484 oval = *counter; 485 486 if ((nval = oval + 1) == 0) { 487 /* 488 * If the counter would wrap, set it to 1 -- assuring 489 * that the counter is never zero when we have seen 490 * errors. (The counter must be 32-bits because we 491 * aren't guaranteed a 64-bit compare&swap operation.) 492 * To save this code both the infamy of being fingered 493 * by a priggish news story and the indignity of being 494 * the target of a neo-puritan witch trial, we're 495 * carefully avoiding any colorful description of the 496 * likelihood of this condition -- but suffice it to 497 * say that it is only slightly more likely than the 498 * overflow of predicate cache IDs, as discussed in 499 * dtrace_predicate_create(). 500 */ 501 nval = 1; 502 } 503 } while (dtrace_cas32(counter, oval, nval) != oval); 504 } 505 506 /* 507 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 508 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 509 */ 510 DTRACE_LOADFUNC(8) 511 DTRACE_LOADFUNC(16) 512 DTRACE_LOADFUNC(32) 513 DTRACE_LOADFUNC(64) 514 515 static int 516 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 517 { 518 if (dest < mstate->dtms_scratch_base) 519 return (0); 520 521 if (dest + size < dest) 522 return (0); 523 524 if (dest + size > mstate->dtms_scratch_ptr) 525 return (0); 526 527 return (1); 528 } 529 530 static int 531 dtrace_canstore_statvar(uint64_t addr, size_t sz, 532 dtrace_statvar_t **svars, int nsvars) 533 { 534 int i; 535 536 for (i = 0; i < nsvars; i++) { 537 dtrace_statvar_t *svar = svars[i]; 538 539 if (svar == NULL || svar->dtsv_size == 0) 540 continue; 541 542 if (addr - svar->dtsv_data < svar->dtsv_size && 543 addr + sz <= svar->dtsv_data + svar->dtsv_size) 544 return (1); 545 } 546 547 return (0); 548 } 549 550 /* 551 * Check to see if the address is within a memory region to which a store may 552 * be issued. This includes the DTrace scratch areas, and any DTrace variable 553 * region. The caller of dtrace_canstore() is responsible for performing any 554 * alignment checks that are needed before stores are actually executed. 555 */ 556 static int 557 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 558 dtrace_vstate_t *vstate) 559 { 560 uintptr_t a; 561 size_t s; 562 563 /* 564 * First, check to see if the address is in scratch space... 565 */ 566 a = mstate->dtms_scratch_base; 567 s = mstate->dtms_scratch_size; 568 569 if (addr - a < s && addr + sz <= a + s) 570 return (1); 571 572 /* 573 * Now check to see if it's a dynamic variable. This check will pick 574 * up both thread-local variables and any global dynamically-allocated 575 * variables. 576 */ 577 a = (uintptr_t)vstate->dtvs_dynvars.dtds_base; 578 s = vstate->dtvs_dynvars.dtds_size; 579 if (addr - a < s && addr + sz <= a + s) 580 return (1); 581 582 /* 583 * Finally, check the static local and global variables. These checks 584 * take the longest, so we perform them last. 585 */ 586 if (dtrace_canstore_statvar(addr, sz, 587 vstate->dtvs_locals, vstate->dtvs_nlocals)) 588 return (1); 589 590 if (dtrace_canstore_statvar(addr, sz, 591 vstate->dtvs_globals, vstate->dtvs_nglobals)) 592 return (1); 593 594 return (0); 595 } 596 597 /* 598 * Compare two strings using safe loads. 599 */ 600 static int 601 dtrace_strncmp(char *s1, char *s2, size_t limit) 602 { 603 uint8_t c1, c2; 604 volatile uint16_t *flags; 605 606 if (s1 == s2 || limit == 0) 607 return (0); 608 609 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 610 611 do { 612 if (s1 == NULL) { 613 c1 = '\0'; 614 } else { 615 c1 = dtrace_load8((uintptr_t)s1++); 616 } 617 618 if (s2 == NULL) { 619 c2 = '\0'; 620 } else { 621 c2 = dtrace_load8((uintptr_t)s2++); 622 } 623 624 if (c1 != c2) 625 return (c1 - c2); 626 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 627 628 return (0); 629 } 630 631 /* 632 * Compute strlen(s) for a string using safe memory accesses. The additional 633 * len parameter is used to specify a maximum length to ensure completion. 634 */ 635 static size_t 636 dtrace_strlen(const char *s, size_t lim) 637 { 638 uint_t len; 639 640 for (len = 0; len != lim; len++) { 641 if (dtrace_load8((uintptr_t)s++) == '\0') 642 break; 643 } 644 645 return (len); 646 } 647 648 /* 649 * Check if an address falls within a toxic region. 650 */ 651 static int 652 dtrace_istoxic(uintptr_t kaddr, size_t size) 653 { 654 uintptr_t taddr, tsize; 655 int i; 656 657 for (i = 0; i < dtrace_toxranges; i++) { 658 taddr = dtrace_toxrange[i].dtt_base; 659 tsize = dtrace_toxrange[i].dtt_limit - taddr; 660 661 if (kaddr - taddr < tsize) { 662 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 663 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr; 664 return (1); 665 } 666 667 if (taddr - kaddr < size) { 668 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 669 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr; 670 return (1); 671 } 672 } 673 674 return (0); 675 } 676 677 /* 678 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 679 * memory specified by the DIF program. The dst is assumed to be safe memory 680 * that we can store to directly because it is managed by DTrace. As with 681 * standard bcopy, overlapping copies are handled properly. 682 */ 683 static void 684 dtrace_bcopy(const void *src, void *dst, size_t len) 685 { 686 if (len != 0) { 687 uint8_t *s1 = dst; 688 const uint8_t *s2 = src; 689 690 if (s1 <= s2) { 691 do { 692 *s1++ = dtrace_load8((uintptr_t)s2++); 693 } while (--len != 0); 694 } else { 695 s2 += len; 696 s1 += len; 697 698 do { 699 *--s1 = dtrace_load8((uintptr_t)--s2); 700 } while (--len != 0); 701 } 702 } 703 } 704 705 /* 706 * Copy src to dst using safe memory accesses, up to either the specified 707 * length, or the point that a nul byte is encountered. The src is assumed to 708 * be unsafe memory specified by the DIF program. The dst is assumed to be 709 * safe memory that we can store to directly because it is managed by DTrace. 710 * Unlike dtrace_bcopy(), overlapping regions are not handled. 711 */ 712 static void 713 dtrace_strcpy(const void *src, void *dst, size_t len) 714 { 715 if (len != 0) { 716 uint8_t *s1 = dst, c; 717 const uint8_t *s2 = src; 718 719 do { 720 *s1++ = c = dtrace_load8((uintptr_t)s2++); 721 } while (--len != 0 && c != '\0'); 722 } 723 } 724 725 /* 726 * Copy src to dst, deriving the size and type from the specified (BYREF) 727 * variable type. The src is assumed to be unsafe memory specified by the DIF 728 * program. The dst is assumed to be DTrace variable memory that is of the 729 * specified type; we assume that we can store to directly. 730 */ 731 static void 732 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 733 { 734 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 735 736 if (type->dtdt_kind == DIF_TYPE_STRING) { 737 dtrace_strcpy(src, dst, type->dtdt_size); 738 } else { 739 dtrace_bcopy(src, dst, type->dtdt_size); 740 } 741 } 742 743 /* 744 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 745 * unsafe memory specified by the DIF program. The s2 data is assumed to be 746 * safe memory that we can access directly because it is managed by DTrace. 747 */ 748 static int 749 dtrace_bcmp(const void *s1, const void *s2, size_t len) 750 { 751 volatile uint16_t *flags; 752 753 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 754 755 if (s1 == s2) 756 return (0); 757 758 if (s1 == NULL || s2 == NULL) 759 return (1); 760 761 if (s1 != s2 && len != 0) { 762 const uint8_t *ps1 = s1; 763 const uint8_t *ps2 = s2; 764 765 do { 766 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 767 return (1); 768 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 769 } 770 return (0); 771 } 772 773 /* 774 * Zero the specified region using a simple byte-by-byte loop. Note that this 775 * is for safe DTrace-managed memory only. 776 */ 777 static void 778 dtrace_bzero(void *dst, size_t len) 779 { 780 uchar_t *cp; 781 782 for (cp = dst; len != 0; len--) 783 *cp++ = 0; 784 } 785 786 /* 787 * This privilege check should be used by actions and subroutines to 788 * verify that the user credentials of the process that enabled the 789 * invoking ECB match the target credentials 790 */ 791 static int 792 dtrace_priv_proc_common_user(dtrace_state_t *state) 793 { 794 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 795 796 /* 797 * We should always have a non-NULL state cred here, since if cred 798 * is null (anonymous tracing), we fast-path bypass this routine. 799 */ 800 ASSERT(s_cr != NULL); 801 802 if ((cr = CRED()) != NULL && 803 s_cr->cr_uid == cr->cr_uid && 804 s_cr->cr_uid == cr->cr_ruid && 805 s_cr->cr_uid == cr->cr_suid && 806 s_cr->cr_gid == cr->cr_gid && 807 s_cr->cr_gid == cr->cr_rgid && 808 s_cr->cr_gid == cr->cr_sgid) 809 return (1); 810 811 return (0); 812 } 813 814 /* 815 * This privilege check should be used by actions and subroutines to 816 * verify that the zone of the process that enabled the invoking ECB 817 * matches the target credentials 818 */ 819 static int 820 dtrace_priv_proc_common_zone(dtrace_state_t *state) 821 { 822 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 823 824 /* 825 * We should always have a non-NULL state cred here, since if cred 826 * is null (anonymous tracing), we fast-path bypass this routine. 827 */ 828 ASSERT(s_cr != NULL); 829 830 if ((cr = CRED()) != NULL && 831 s_cr->cr_zone == cr->cr_zone) 832 return (1); 833 834 return (0); 835 } 836 837 /* 838 * This privilege check should be used by actions and subroutines to 839 * verify that the process has not setuid or changed credentials. 840 */ 841 static int 842 dtrace_priv_proc_common_nocd() 843 { 844 proc_t *proc; 845 846 if ((proc = ttoproc(curthread)) != NULL && 847 !(proc->p_flag & SNOCD)) 848 return (1); 849 850 return (0); 851 } 852 853 static int 854 dtrace_priv_proc_destructive(dtrace_state_t *state) 855 { 856 int action = state->dts_cred.dcr_action; 857 858 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 859 dtrace_priv_proc_common_zone(state) == 0) 860 goto bad; 861 862 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 863 dtrace_priv_proc_common_user(state) == 0) 864 goto bad; 865 866 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 867 dtrace_priv_proc_common_nocd() == 0) 868 goto bad; 869 870 return (1); 871 872 bad: 873 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 874 875 return (0); 876 } 877 878 static int 879 dtrace_priv_proc_control(dtrace_state_t *state) 880 { 881 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 882 return (1); 883 884 if (dtrace_priv_proc_common_zone(state) && 885 dtrace_priv_proc_common_user(state) && 886 dtrace_priv_proc_common_nocd()) 887 return (1); 888 889 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 890 891 return (0); 892 } 893 894 static int 895 dtrace_priv_proc(dtrace_state_t *state) 896 { 897 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 898 return (1); 899 900 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 901 902 return (0); 903 } 904 905 static int 906 dtrace_priv_kernel(dtrace_state_t *state) 907 { 908 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 909 return (1); 910 911 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 912 913 return (0); 914 } 915 916 static int 917 dtrace_priv_kernel_destructive(dtrace_state_t *state) 918 { 919 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 920 return (1); 921 922 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 923 924 return (0); 925 } 926 927 /* 928 * Note: not called from probe context. This function is called 929 * asynchronously (and at a regular interval) from outside of probe context to 930 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 931 * cleaning is explained in detail in <sys/dtrace_impl.h>. 932 */ 933 void 934 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 935 { 936 dtrace_dynvar_t *dirty; 937 dtrace_dstate_percpu_t *dcpu; 938 int i, work = 0; 939 940 for (i = 0; i < NCPU; i++) { 941 dcpu = &dstate->dtds_percpu[i]; 942 943 ASSERT(dcpu->dtdsc_rinsing == NULL); 944 945 /* 946 * If the dirty list is NULL, there is no dirty work to do. 947 */ 948 if (dcpu->dtdsc_dirty == NULL) 949 continue; 950 951 /* 952 * If the clean list is non-NULL, then we're not going to do 953 * any work for this CPU -- it means that there has not been 954 * a dtrace_dynvar() allocation on this CPU (or from this CPU) 955 * since the last time we cleaned house. 956 */ 957 if (dcpu->dtdsc_clean != NULL) 958 continue; 959 960 work = 1; 961 962 /* 963 * Atomically move the dirty list aside. 964 */ 965 do { 966 dirty = dcpu->dtdsc_dirty; 967 968 /* 969 * Before we zap the dirty list, set the rinsing list. 970 * (This allows for a potential assertion in 971 * dtrace_dynvar(): if a free dynamic variable appears 972 * on a hash chain, either the dirty list or the 973 * rinsing list for some CPU must be non-NULL.) 974 */ 975 dcpu->dtdsc_rinsing = dirty; 976 dtrace_membar_producer(); 977 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 978 dirty, NULL) != dirty); 979 } 980 981 if (!work) { 982 /* 983 * We have no work to do; we can simply return. 984 */ 985 return; 986 } 987 988 dtrace_sync(); 989 990 for (i = 0; i < NCPU; i++) { 991 dcpu = &dstate->dtds_percpu[i]; 992 993 if (dcpu->dtdsc_rinsing == NULL) 994 continue; 995 996 /* 997 * We are now guaranteed that no hash chain contains a pointer 998 * into this dirty list; we can make it clean. 999 */ 1000 ASSERT(dcpu->dtdsc_clean == NULL); 1001 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1002 dcpu->dtdsc_rinsing = NULL; 1003 } 1004 1005 /* 1006 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1007 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1008 * This prevents a race whereby a CPU incorrectly decides that 1009 * the state should be something other than DTRACE_DSTATE_CLEAN 1010 * after dtrace_dynvar_clean() has completed. 1011 */ 1012 dtrace_sync(); 1013 1014 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1015 } 1016 1017 /* 1018 * Depending on the value of the op parameter, this function looks-up, 1019 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1020 * allocation is requested, this function will return a pointer to a 1021 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1022 * variable can be allocated. If NULL is returned, the appropriate counter 1023 * will be incremented. 1024 */ 1025 dtrace_dynvar_t * 1026 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1027 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op) 1028 { 1029 uint64_t hashval = 1; 1030 dtrace_dynhash_t *hash = dstate->dtds_hash; 1031 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1032 processorid_t me = CPU->cpu_id, cpu = me; 1033 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1034 size_t bucket, ksize; 1035 size_t chunksize = dstate->dtds_chunksize; 1036 uintptr_t kdata, lock, nstate; 1037 uint_t i; 1038 1039 ASSERT(nkeys != 0); 1040 1041 /* 1042 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1043 * algorithm. For the by-value portions, we perform the algorithm in 1044 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1045 * bit, and seems to have only a minute effect on distribution. For 1046 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1047 * over each referenced byte. It's painful to do this, but it's much 1048 * better than pathological hash distribution. The efficacy of the 1049 * hashing algorithm (and a comparison with other algorithms) may be 1050 * found by running the ::dtrace_dynstat MDB dcmd. 1051 */ 1052 for (i = 0; i < nkeys; i++) { 1053 if (key[i].dttk_size == 0) { 1054 uint64_t val = key[i].dttk_value; 1055 1056 hashval += (val >> 48) & 0xffff; 1057 hashval += (hashval << 10); 1058 hashval ^= (hashval >> 6); 1059 1060 hashval += (val >> 32) & 0xffff; 1061 hashval += (hashval << 10); 1062 hashval ^= (hashval >> 6); 1063 1064 hashval += (val >> 16) & 0xffff; 1065 hashval += (hashval << 10); 1066 hashval ^= (hashval >> 6); 1067 1068 hashval += val & 0xffff; 1069 hashval += (hashval << 10); 1070 hashval ^= (hashval >> 6); 1071 } else { 1072 /* 1073 * This is incredibly painful, but it beats the hell 1074 * out of the alternative. 1075 */ 1076 uint64_t j, size = key[i].dttk_size; 1077 uintptr_t base = (uintptr_t)key[i].dttk_value; 1078 1079 for (j = 0; j < size; j++) { 1080 hashval += dtrace_load8(base + j); 1081 hashval += (hashval << 10); 1082 hashval ^= (hashval >> 6); 1083 } 1084 } 1085 } 1086 1087 hashval += (hashval << 3); 1088 hashval ^= (hashval >> 11); 1089 hashval += (hashval << 15); 1090 1091 /* 1092 * There is a remote chance (ideally, 1 in 2^32) that our hashval 1093 * comes out to be 0. We rely on a zero hashval denoting a free 1094 * element; if this actually happens, we set the hashval to 1. 1095 */ 1096 if (hashval == 0) 1097 hashval = 1; 1098 1099 /* 1100 * Yes, it's painful to do a divide here. If the cycle count becomes 1101 * important here, tricks can be pulled to reduce it. (However, it's 1102 * critical that hash collisions be kept to an absolute minimum; 1103 * they're much more painful than a divide.) It's better to have a 1104 * solution that generates few collisions and still keeps things 1105 * relatively simple. 1106 */ 1107 bucket = hashval % dstate->dtds_hashsize; 1108 1109 if (op == DTRACE_DYNVAR_DEALLOC) { 1110 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1111 1112 for (;;) { 1113 while ((lock = *lockp) & 1) 1114 continue; 1115 1116 if (dtrace_casptr((void *)lockp, 1117 (void *)lock, (void *)(lock + 1)) == (void *)lock) 1118 break; 1119 } 1120 1121 dtrace_membar_producer(); 1122 } 1123 1124 top: 1125 prev = NULL; 1126 lock = hash[bucket].dtdh_lock; 1127 1128 dtrace_membar_consumer(); 1129 1130 start = hash[bucket].dtdh_chain; 1131 ASSERT(start == NULL || start->dtdv_hashval != 0 || 1132 op != DTRACE_DYNVAR_DEALLOC); 1133 1134 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1135 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1136 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1137 1138 if (dvar->dtdv_hashval != hashval) { 1139 if (dvar->dtdv_hashval == 0) { 1140 /* 1141 * We've gone off the rails. Somewhere 1142 * along the line, one of the members of this 1143 * hash chain was deleted. We could assert 1144 * that either the dirty list or the rinsing 1145 * list is non-NULL. (The dtrace_sync() in 1146 * dtrace_dynvar_clean() would validate this 1147 * assertion.) 1148 */ 1149 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1150 goto top; 1151 } 1152 1153 goto next; 1154 } 1155 1156 if (dtuple->dtt_nkeys != nkeys) 1157 goto next; 1158 1159 for (i = 0; i < nkeys; i++, dkey++) { 1160 if (dkey->dttk_size != key[i].dttk_size) 1161 goto next; /* size or type mismatch */ 1162 1163 if (dkey->dttk_size != 0) { 1164 if (dtrace_bcmp( 1165 (void *)(uintptr_t)key[i].dttk_value, 1166 (void *)(uintptr_t)dkey->dttk_value, 1167 dkey->dttk_size)) 1168 goto next; 1169 } else { 1170 if (dkey->dttk_value != key[i].dttk_value) 1171 goto next; 1172 } 1173 } 1174 1175 if (op != DTRACE_DYNVAR_DEALLOC) 1176 return (dvar); 1177 1178 ASSERT(dvar->dtdv_next == NULL || 1179 dvar->dtdv_next->dtdv_hashval != 0); 1180 1181 if (prev != NULL) { 1182 ASSERT(hash[bucket].dtdh_chain != dvar); 1183 ASSERT(start != dvar); 1184 ASSERT(prev->dtdv_next == dvar); 1185 prev->dtdv_next = dvar->dtdv_next; 1186 } else { 1187 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1188 start, dvar->dtdv_next) != start) { 1189 /* 1190 * We have failed to atomically swing the 1191 * hash table head pointer, presumably because 1192 * of a conflicting allocation on another CPU. 1193 * We need to reread the hash chain and try 1194 * again. 1195 */ 1196 goto top; 1197 } 1198 } 1199 1200 dtrace_membar_producer(); 1201 1202 /* 1203 * Now clear the hash value to indicate that it's free. 1204 */ 1205 ASSERT(hash[bucket].dtdh_chain != dvar); 1206 dvar->dtdv_hashval = 0; 1207 1208 dtrace_membar_producer(); 1209 1210 /* 1211 * Set the next pointer to point at the dirty list, and 1212 * atomically swing the dirty pointer to the newly freed dvar. 1213 */ 1214 do { 1215 next = dcpu->dtdsc_dirty; 1216 dvar->dtdv_next = next; 1217 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1218 1219 /* 1220 * Finally, unlock this hash bucket. 1221 */ 1222 ASSERT(hash[bucket].dtdh_lock == lock); 1223 ASSERT(lock & 1); 1224 hash[bucket].dtdh_lock++; 1225 1226 return (NULL); 1227 next: 1228 prev = dvar; 1229 continue; 1230 } 1231 1232 if (op != DTRACE_DYNVAR_ALLOC) { 1233 /* 1234 * If we are not to allocate a new variable, we want to 1235 * return NULL now. Before we return, check that the value 1236 * of the lock word hasn't changed. If it has, we may have 1237 * seen an inconsistent snapshot. 1238 */ 1239 if (op == DTRACE_DYNVAR_NOALLOC) { 1240 if (hash[bucket].dtdh_lock != lock) 1241 goto top; 1242 } else { 1243 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1244 ASSERT(hash[bucket].dtdh_lock == lock); 1245 ASSERT(lock & 1); 1246 hash[bucket].dtdh_lock++; 1247 } 1248 1249 return (NULL); 1250 } 1251 1252 /* 1253 * We need to allocate a new dynamic variable. The size we need is the 1254 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1255 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1256 * the size of any referred-to data (dsize). We then round the final 1257 * size up to the chunksize for allocation. 1258 */ 1259 for (ksize = 0, i = 0; i < nkeys; i++) 1260 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1261 1262 /* 1263 * This should be pretty much impossible, but could happen if, say, 1264 * strange DIF specified the tuple. Ideally, this should be an 1265 * assertion and not an error condition -- but that requires that the 1266 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1267 * bullet-proof. (That is, it must not be able to be fooled by 1268 * malicious DIF.) Given the lack of backwards branches in DIF, 1269 * solving this would presumably not amount to solving the Halting 1270 * Problem -- but it still seems awfully hard. 1271 */ 1272 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1273 ksize + dsize > chunksize) { 1274 dcpu->dtdsc_drops++; 1275 return (NULL); 1276 } 1277 1278 nstate = DTRACE_DSTATE_EMPTY; 1279 1280 do { 1281 retry: 1282 free = dcpu->dtdsc_free; 1283 1284 if (free == NULL) { 1285 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1286 void *rval; 1287 1288 if (clean == NULL) { 1289 /* 1290 * We're out of dynamic variable space on 1291 * this CPU. Unless we have tried all CPUs, 1292 * we'll try to allocate from a different 1293 * CPU. 1294 */ 1295 switch (dstate->dtds_state) { 1296 case DTRACE_DSTATE_CLEAN: { 1297 void *sp = &dstate->dtds_state; 1298 1299 if (++cpu >= NCPU) 1300 cpu = 0; 1301 1302 if (dcpu->dtdsc_dirty != NULL && 1303 nstate == DTRACE_DSTATE_EMPTY) 1304 nstate = DTRACE_DSTATE_DIRTY; 1305 1306 if (dcpu->dtdsc_rinsing != NULL) 1307 nstate = DTRACE_DSTATE_RINSING; 1308 1309 dcpu = &dstate->dtds_percpu[cpu]; 1310 1311 if (cpu != me) 1312 goto retry; 1313 1314 (void) dtrace_cas32(sp, 1315 DTRACE_DSTATE_CLEAN, nstate); 1316 1317 /* 1318 * To increment the correct bean 1319 * counter, take another lap. 1320 */ 1321 goto retry; 1322 } 1323 1324 case DTRACE_DSTATE_DIRTY: 1325 dcpu->dtdsc_dirty_drops++; 1326 break; 1327 1328 case DTRACE_DSTATE_RINSING: 1329 dcpu->dtdsc_rinsing_drops++; 1330 break; 1331 1332 case DTRACE_DSTATE_EMPTY: 1333 dcpu->dtdsc_drops++; 1334 break; 1335 } 1336 1337 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1338 return (NULL); 1339 } 1340 1341 /* 1342 * The clean list appears to be non-empty. We want to 1343 * move the clean list to the free list; we start by 1344 * moving the clean pointer aside. 1345 */ 1346 if (dtrace_casptr(&dcpu->dtdsc_clean, 1347 clean, NULL) != clean) { 1348 /* 1349 * We are in one of two situations: 1350 * 1351 * (a) The clean list was switched to the 1352 * free list by another CPU. 1353 * 1354 * (b) The clean list was added to by the 1355 * cleansing cyclic. 1356 * 1357 * In either of these situations, we can 1358 * just reattempt the free list allocation. 1359 */ 1360 goto retry; 1361 } 1362 1363 ASSERT(clean->dtdv_hashval == 0); 1364 1365 /* 1366 * Now we'll move the clean list to the free list. 1367 * It's impossible for this to fail: the only way 1368 * the free list can be updated is through this 1369 * code path, and only one CPU can own the clean list. 1370 * Thus, it would only be possible for this to fail if 1371 * this code were racing with dtrace_dynvar_clean(). 1372 * (That is, if dtrace_dynvar_clean() updated the clean 1373 * list, and we ended up racing to update the free 1374 * list.) This race is prevented by the dtrace_sync() 1375 * in dtrace_dynvar_clean() -- which flushes the 1376 * owners of the clean lists out before resetting 1377 * the clean lists. 1378 */ 1379 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1380 ASSERT(rval == NULL); 1381 goto retry; 1382 } 1383 1384 dvar = free; 1385 new_free = dvar->dtdv_next; 1386 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1387 1388 /* 1389 * We have now allocated a new chunk. We copy the tuple keys into the 1390 * tuple array and copy any referenced key data into the data space 1391 * following the tuple array. As we do this, we relocate dttk_value 1392 * in the final tuple to point to the key data address in the chunk. 1393 */ 1394 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1395 dvar->dtdv_data = (void *)(kdata + ksize); 1396 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1397 1398 for (i = 0; i < nkeys; i++) { 1399 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1400 size_t kesize = key[i].dttk_size; 1401 1402 if (kesize != 0) { 1403 dtrace_bcopy( 1404 (const void *)(uintptr_t)key[i].dttk_value, 1405 (void *)kdata, kesize); 1406 dkey->dttk_value = kdata; 1407 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1408 } else { 1409 dkey->dttk_value = key[i].dttk_value; 1410 } 1411 1412 dkey->dttk_size = kesize; 1413 } 1414 1415 ASSERT(dvar->dtdv_hashval == 0); 1416 dvar->dtdv_hashval = hashval; 1417 dvar->dtdv_next = start; 1418 1419 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1420 return (dvar); 1421 1422 /* 1423 * The cas has failed. Either another CPU is adding an element to 1424 * this hash chain, or another CPU is deleting an element from this 1425 * hash chain. The simplest way to deal with both of these cases 1426 * (though not necessarily the most efficient) is to free our 1427 * allocated block and tail-call ourselves. Note that the free is 1428 * to the dirty list and _not_ to the free list. This is to prevent 1429 * races with allocators, above. 1430 */ 1431 dvar->dtdv_hashval = 0; 1432 1433 dtrace_membar_producer(); 1434 1435 do { 1436 free = dcpu->dtdsc_dirty; 1437 dvar->dtdv_next = free; 1438 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1439 1440 return (dtrace_dynvar(dstate, nkeys, key, dsize, op)); 1441 } 1442 1443 /*ARGSUSED*/ 1444 static void 1445 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1446 { 1447 if (nval < *oval) 1448 *oval = nval; 1449 } 1450 1451 /*ARGSUSED*/ 1452 static void 1453 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1454 { 1455 if (nval > *oval) 1456 *oval = nval; 1457 } 1458 1459 static void 1460 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1461 { 1462 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1463 int64_t val = (int64_t)nval; 1464 1465 if (val < 0) { 1466 for (i = 0; i < zero; i++) { 1467 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1468 quanta[i] += incr; 1469 return; 1470 } 1471 } 1472 } else { 1473 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1474 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1475 quanta[i - 1] += incr; 1476 return; 1477 } 1478 } 1479 1480 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1481 return; 1482 } 1483 1484 ASSERT(0); 1485 } 1486 1487 static void 1488 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1489 { 1490 uint64_t arg = *lquanta++; 1491 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1492 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1493 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1494 int32_t val = (int32_t)nval, level; 1495 1496 ASSERT(step != 0); 1497 ASSERT(levels != 0); 1498 1499 if (val < base) { 1500 /* 1501 * This is an underflow. 1502 */ 1503 lquanta[0] += incr; 1504 return; 1505 } 1506 1507 level = (val - base) / step; 1508 1509 if (level < levels) { 1510 lquanta[level + 1] += incr; 1511 return; 1512 } 1513 1514 /* 1515 * This is an overflow. 1516 */ 1517 lquanta[levels + 1] += incr; 1518 } 1519 1520 /*ARGSUSED*/ 1521 static void 1522 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 1523 { 1524 data[0]++; 1525 data[1] += nval; 1526 } 1527 1528 /*ARGSUSED*/ 1529 static void 1530 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 1531 { 1532 *oval = *oval + 1; 1533 } 1534 1535 /*ARGSUSED*/ 1536 static void 1537 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 1538 { 1539 *oval += nval; 1540 } 1541 1542 /* 1543 * Aggregate given the tuple in the principal data buffer, and the aggregating 1544 * action denoted by the specified dtrace_aggregation_t. The aggregation 1545 * buffer is specified as the buf parameter. This routine does not return 1546 * failure; if there is no space in the aggregation buffer, the data will be 1547 * dropped, and a corresponding counter incremented. 1548 */ 1549 static void 1550 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 1551 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 1552 { 1553 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 1554 uint32_t i, ndx, size, fsize; 1555 uint32_t align = sizeof (uint64_t) - 1; 1556 dtrace_aggbuffer_t *agb; 1557 dtrace_aggkey_t *key; 1558 uint32_t hashval = 0, limit, isstr; 1559 caddr_t tomax, data, kdata; 1560 dtrace_actkind_t action; 1561 dtrace_action_t *act; 1562 uintptr_t offs; 1563 1564 if (buf == NULL) 1565 return; 1566 1567 if (!agg->dtag_hasarg) { 1568 /* 1569 * Currently, only quantize() and lquantize() take additional 1570 * arguments, and they have the same semantics: an increment 1571 * value that defaults to 1 when not present. If additional 1572 * aggregating actions take arguments, the setting of the 1573 * default argument value will presumably have to become more 1574 * sophisticated... 1575 */ 1576 arg = 1; 1577 } 1578 1579 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 1580 size = rec->dtrd_offset - agg->dtag_base; 1581 fsize = size + rec->dtrd_size; 1582 1583 ASSERT(dbuf->dtb_tomax != NULL); 1584 data = dbuf->dtb_tomax + offset + agg->dtag_base; 1585 1586 if ((tomax = buf->dtb_tomax) == NULL) { 1587 dtrace_buffer_drop(buf); 1588 return; 1589 } 1590 1591 /* 1592 * The metastructure is always at the bottom of the buffer. 1593 */ 1594 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 1595 sizeof (dtrace_aggbuffer_t)); 1596 1597 if (buf->dtb_offset == 0) { 1598 /* 1599 * We just kludge up approximately 1/8th of the size to be 1600 * buckets. If this guess ends up being routinely 1601 * off-the-mark, we may need to dynamically readjust this 1602 * based on past performance. 1603 */ 1604 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 1605 1606 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 1607 (uintptr_t)tomax || hashsize == 0) { 1608 /* 1609 * We've been given a ludicrously small buffer; 1610 * increment our drop count and leave. 1611 */ 1612 dtrace_buffer_drop(buf); 1613 return; 1614 } 1615 1616 /* 1617 * And now, a pathetic attempt to try to get a an odd (or 1618 * perchance, a prime) hash size for better hash distribution. 1619 */ 1620 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 1621 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 1622 1623 agb->dtagb_hashsize = hashsize; 1624 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 1625 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 1626 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 1627 1628 for (i = 0; i < agb->dtagb_hashsize; i++) 1629 agb->dtagb_hash[i] = NULL; 1630 } 1631 1632 ASSERT(agg->dtag_first != NULL); 1633 ASSERT(agg->dtag_first->dta_intuple); 1634 1635 /* 1636 * Calculate the hash value based on the key. Note that we _don't_ 1637 * include the aggid in the hashing (but we will store it as part of 1638 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 1639 * algorithm: a simple, quick algorithm that has no known funnels, and 1640 * gets good distribution in practice. The efficacy of the hashing 1641 * algorithm (and a comparison with other algorithms) may be found by 1642 * running the ::dtrace_aggstat MDB dcmd. 1643 */ 1644 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 1645 i = act->dta_rec.dtrd_offset - agg->dtag_base; 1646 limit = i + act->dta_rec.dtrd_size; 1647 ASSERT(limit <= size); 1648 isstr = DTRACEACT_ISSTRING(act); 1649 1650 for (; i < limit; i++) { 1651 hashval += data[i]; 1652 hashval += (hashval << 10); 1653 hashval ^= (hashval >> 6); 1654 1655 if (isstr && data[i] == '\0') 1656 break; 1657 } 1658 } 1659 1660 hashval += (hashval << 3); 1661 hashval ^= (hashval >> 11); 1662 hashval += (hashval << 15); 1663 1664 /* 1665 * Yes, the divide here is expensive -- but it's generally the least 1666 * of the performance issues given the amount of data that we iterate 1667 * over to compute hash values, compare data, etc. 1668 */ 1669 ndx = hashval % agb->dtagb_hashsize; 1670 1671 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 1672 ASSERT((caddr_t)key >= tomax); 1673 ASSERT((caddr_t)key < tomax + buf->dtb_size); 1674 1675 if (hashval != key->dtak_hashval || key->dtak_size != size) 1676 continue; 1677 1678 kdata = key->dtak_data; 1679 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 1680 1681 for (act = agg->dtag_first; act->dta_intuple; 1682 act = act->dta_next) { 1683 i = act->dta_rec.dtrd_offset - agg->dtag_base; 1684 limit = i + act->dta_rec.dtrd_size; 1685 ASSERT(limit <= size); 1686 isstr = DTRACEACT_ISSTRING(act); 1687 1688 for (; i < limit; i++) { 1689 if (kdata[i] != data[i]) 1690 goto next; 1691 1692 if (isstr && data[i] == '\0') 1693 break; 1694 } 1695 } 1696 1697 if (action != key->dtak_action) { 1698 /* 1699 * We are aggregating on the same value in the same 1700 * aggregation with two different aggregating actions. 1701 * (This should have been picked up in the compiler, 1702 * so we may be dealing with errant or devious DIF.) 1703 * This is an error condition; we indicate as much, 1704 * and return. 1705 */ 1706 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 1707 return; 1708 } 1709 1710 /* 1711 * This is a hit: we need to apply the aggregator to 1712 * the value at this key. 1713 */ 1714 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 1715 return; 1716 next: 1717 continue; 1718 } 1719 1720 /* 1721 * We didn't find it. We need to allocate some zero-filled space, 1722 * link it into the hash table appropriately, and apply the aggregator 1723 * to the (zero-filled) value. 1724 */ 1725 offs = buf->dtb_offset; 1726 while (offs & (align - 1)) 1727 offs += sizeof (uint32_t); 1728 1729 /* 1730 * If we don't have enough room to both allocate a new key _and_ 1731 * its associated data, increment the drop count and return. 1732 */ 1733 if ((uintptr_t)tomax + offs + fsize > 1734 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 1735 dtrace_buffer_drop(buf); 1736 return; 1737 } 1738 1739 /*CONSTCOND*/ 1740 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 1741 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 1742 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 1743 1744 key->dtak_data = kdata = tomax + offs; 1745 buf->dtb_offset = offs + fsize; 1746 1747 /* 1748 * Now copy the data across. 1749 */ 1750 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 1751 1752 for (i = sizeof (dtrace_aggid_t); i < size; i++) 1753 kdata[i] = data[i]; 1754 1755 /* 1756 * Because strings are not zeroed out by default, we need to iterate 1757 * looking for actions that store strings, and we need to explicitly 1758 * pad these strings out with zeroes. 1759 */ 1760 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 1761 int nul; 1762 1763 if (!DTRACEACT_ISSTRING(act)) 1764 continue; 1765 1766 i = act->dta_rec.dtrd_offset - agg->dtag_base; 1767 limit = i + act->dta_rec.dtrd_size; 1768 ASSERT(limit <= size); 1769 1770 for (nul = 0; i < limit; i++) { 1771 if (nul) { 1772 kdata[i] = '\0'; 1773 continue; 1774 } 1775 1776 if (data[i] != '\0') 1777 continue; 1778 1779 nul = 1; 1780 } 1781 } 1782 1783 for (i = size; i < fsize; i++) 1784 kdata[i] = 0; 1785 1786 key->dtak_hashval = hashval; 1787 key->dtak_size = size; 1788 key->dtak_action = action; 1789 key->dtak_next = agb->dtagb_hash[ndx]; 1790 agb->dtagb_hash[ndx] = key; 1791 1792 /* 1793 * Finally, apply the aggregator. 1794 */ 1795 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 1796 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 1797 } 1798 1799 /* 1800 * Given consumer state, this routine finds a speculation in the INACTIVE 1801 * state and transitions it into the ACTIVE state. If there is no speculation 1802 * in the INACTIVE state, 0 is returned. In this case, no error counter is 1803 * incremented -- it is up to the caller to take appropriate action. 1804 */ 1805 static int 1806 dtrace_speculation(dtrace_state_t *state) 1807 { 1808 int i = 0; 1809 dtrace_speculation_state_t current; 1810 uint32_t *stat = &state->dts_speculations_unavail, count; 1811 1812 while (i < state->dts_nspeculations) { 1813 dtrace_speculation_t *spec = &state->dts_speculations[i]; 1814 1815 current = spec->dtsp_state; 1816 1817 if (current != DTRACESPEC_INACTIVE) { 1818 if (current == DTRACESPEC_COMMITTINGMANY || 1819 current == DTRACESPEC_COMMITTING || 1820 current == DTRACESPEC_DISCARDING) 1821 stat = &state->dts_speculations_busy; 1822 i++; 1823 continue; 1824 } 1825 1826 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 1827 current, DTRACESPEC_ACTIVE) == current) 1828 return (i + 1); 1829 } 1830 1831 /* 1832 * We couldn't find a speculation. If we found as much as a single 1833 * busy speculation buffer, we'll attribute this failure as "busy" 1834 * instead of "unavail". 1835 */ 1836 do { 1837 count = *stat; 1838 } while (dtrace_cas32(stat, count, count + 1) != count); 1839 1840 return (0); 1841 } 1842 1843 /* 1844 * This routine commits an active speculation. If the specified speculation 1845 * is not in a valid state to perform a commit(), this routine will silently do 1846 * nothing. The state of the specified speculation is transitioned according 1847 * to the state transition diagram outlined in <sys/dtrace_impl.h> 1848 */ 1849 static void 1850 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 1851 dtrace_specid_t which) 1852 { 1853 dtrace_speculation_t *spec; 1854 dtrace_buffer_t *src, *dest; 1855 uintptr_t daddr, saddr, dlimit; 1856 dtrace_speculation_state_t current, new; 1857 intptr_t offs; 1858 1859 if (which == 0) 1860 return; 1861 1862 if (which > state->dts_nspeculations) { 1863 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 1864 return; 1865 } 1866 1867 spec = &state->dts_speculations[which - 1]; 1868 src = &spec->dtsp_buffer[cpu]; 1869 dest = &state->dts_buffer[cpu]; 1870 1871 do { 1872 current = spec->dtsp_state; 1873 1874 if (current == DTRACESPEC_COMMITTINGMANY) 1875 break; 1876 1877 switch (current) { 1878 case DTRACESPEC_INACTIVE: 1879 case DTRACESPEC_DISCARDING: 1880 return; 1881 1882 case DTRACESPEC_COMMITTING: 1883 /* 1884 * This is only possible if we are (a) commit()'ing 1885 * without having done a prior speculate() on this CPU 1886 * and (b) racing with another commit() on a different 1887 * CPU. There's nothing to do -- we just assert that 1888 * our offset is 0. 1889 */ 1890 ASSERT(src->dtb_offset == 0); 1891 return; 1892 1893 case DTRACESPEC_ACTIVE: 1894 new = DTRACESPEC_COMMITTING; 1895 break; 1896 1897 case DTRACESPEC_ACTIVEONE: 1898 /* 1899 * This speculation is active on one CPU. If our 1900 * buffer offset is non-zero, we know that the one CPU 1901 * must be us. Otherwise, we are committing on a 1902 * different CPU from the speculate(), and we must 1903 * rely on being asynchronously cleaned. 1904 */ 1905 if (src->dtb_offset != 0) { 1906 new = DTRACESPEC_COMMITTING; 1907 break; 1908 } 1909 /*FALLTHROUGH*/ 1910 1911 case DTRACESPEC_ACTIVEMANY: 1912 new = DTRACESPEC_COMMITTINGMANY; 1913 break; 1914 1915 default: 1916 ASSERT(0); 1917 } 1918 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 1919 current, new) != current); 1920 1921 /* 1922 * We have set the state to indicate that we are committing this 1923 * speculation. Now reserve the necessary space in the destination 1924 * buffer. 1925 */ 1926 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 1927 sizeof (uint64_t), state, NULL)) < 0) { 1928 dtrace_buffer_drop(dest); 1929 goto out; 1930 } 1931 1932 /* 1933 * We have the space; copy the buffer across. (Note that this is a 1934 * highly subobtimal bcopy(); in the unlikely event that this becomes 1935 * a serious performance issue, a high-performance DTrace-specific 1936 * bcopy() should obviously be invented.) 1937 */ 1938 daddr = (uintptr_t)dest->dtb_tomax + offs; 1939 dlimit = daddr + src->dtb_offset; 1940 saddr = (uintptr_t)src->dtb_tomax; 1941 1942 /* 1943 * First, the aligned portion. 1944 */ 1945 while (dlimit - daddr >= sizeof (uint64_t)) { 1946 *((uint64_t *)daddr) = *((uint64_t *)saddr); 1947 1948 daddr += sizeof (uint64_t); 1949 saddr += sizeof (uint64_t); 1950 } 1951 1952 /* 1953 * Now any left-over bit... 1954 */ 1955 while (dlimit - daddr) 1956 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 1957 1958 /* 1959 * Finally, commit the reserved space in the destination buffer. 1960 */ 1961 dest->dtb_offset = offs + src->dtb_offset; 1962 1963 out: 1964 /* 1965 * If we're lucky enough to be the only active CPU on this speculation 1966 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 1967 */ 1968 if (current == DTRACESPEC_ACTIVE || 1969 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 1970 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 1971 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 1972 1973 ASSERT(rval == DTRACESPEC_COMMITTING); 1974 } 1975 1976 src->dtb_offset = 0; 1977 src->dtb_xamot_drops += src->dtb_drops; 1978 src->dtb_drops = 0; 1979 } 1980 1981 /* 1982 * This routine discards an active speculation. If the specified speculation 1983 * is not in a valid state to perform a discard(), this routine will silently 1984 * do nothing. The state of the specified speculation is transitioned 1985 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 1986 */ 1987 static void 1988 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 1989 dtrace_specid_t which) 1990 { 1991 dtrace_speculation_t *spec; 1992 dtrace_speculation_state_t current, new; 1993 dtrace_buffer_t *buf; 1994 1995 if (which == 0) 1996 return; 1997 1998 if (which > state->dts_nspeculations) { 1999 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2000 return; 2001 } 2002 2003 spec = &state->dts_speculations[which - 1]; 2004 buf = &spec->dtsp_buffer[cpu]; 2005 2006 do { 2007 current = spec->dtsp_state; 2008 2009 switch (current) { 2010 case DTRACESPEC_INACTIVE: 2011 case DTRACESPEC_COMMITTINGMANY: 2012 case DTRACESPEC_COMMITTING: 2013 case DTRACESPEC_DISCARDING: 2014 return; 2015 2016 case DTRACESPEC_ACTIVE: 2017 case DTRACESPEC_ACTIVEMANY: 2018 new = DTRACESPEC_DISCARDING; 2019 break; 2020 2021 case DTRACESPEC_ACTIVEONE: 2022 if (buf->dtb_offset != 0) { 2023 new = DTRACESPEC_INACTIVE; 2024 } else { 2025 new = DTRACESPEC_DISCARDING; 2026 } 2027 break; 2028 2029 default: 2030 ASSERT(0); 2031 } 2032 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2033 current, new) != current); 2034 2035 buf->dtb_offset = 0; 2036 buf->dtb_drops = 0; 2037 } 2038 2039 /* 2040 * Note: not called from probe context. This function is called 2041 * asynchronously from cross call context to clean any speculations that are 2042 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2043 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2044 * speculation. 2045 */ 2046 static void 2047 dtrace_speculation_clean_here(dtrace_state_t *state) 2048 { 2049 dtrace_icookie_t cookie; 2050 processorid_t cpu = CPU->cpu_id; 2051 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2052 dtrace_specid_t i; 2053 2054 cookie = dtrace_interrupt_disable(); 2055 2056 if (dest->dtb_tomax == NULL) { 2057 dtrace_interrupt_enable(cookie); 2058 return; 2059 } 2060 2061 for (i = 0; i < state->dts_nspeculations; i++) { 2062 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2063 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2064 2065 if (src->dtb_tomax == NULL) 2066 continue; 2067 2068 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2069 src->dtb_offset = 0; 2070 continue; 2071 } 2072 2073 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2074 continue; 2075 2076 if (src->dtb_offset == 0) 2077 continue; 2078 2079 dtrace_speculation_commit(state, cpu, i + 1); 2080 } 2081 2082 dtrace_interrupt_enable(cookie); 2083 } 2084 2085 /* 2086 * Note: not called from probe context. This function is called 2087 * asynchronously (and at a regular interval) to clean any speculations that 2088 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2089 * is work to be done, it cross calls all CPUs to perform that work; 2090 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2091 * INACTIVE state until they have been cleaned by all CPUs. 2092 */ 2093 static void 2094 dtrace_speculation_clean(dtrace_state_t *state) 2095 { 2096 int work = 0, rv; 2097 dtrace_specid_t i; 2098 2099 for (i = 0; i < state->dts_nspeculations; i++) { 2100 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2101 2102 ASSERT(!spec->dtsp_cleaning); 2103 2104 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2105 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2106 continue; 2107 2108 work++; 2109 spec->dtsp_cleaning = 1; 2110 } 2111 2112 if (!work) 2113 return; 2114 2115 dtrace_xcall(DTRACE_CPUALL, 2116 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2117 2118 /* 2119 * We now know that all CPUs have committed or discarded their 2120 * speculation buffers, as appropriate. We can now set the state 2121 * to inactive. 2122 */ 2123 for (i = 0; i < state->dts_nspeculations; i++) { 2124 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2125 dtrace_speculation_state_t current, new; 2126 2127 if (!spec->dtsp_cleaning) 2128 continue; 2129 2130 current = spec->dtsp_state; 2131 ASSERT(current == DTRACESPEC_DISCARDING || 2132 current == DTRACESPEC_COMMITTINGMANY); 2133 2134 new = DTRACESPEC_INACTIVE; 2135 2136 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2137 ASSERT(rv == current); 2138 spec->dtsp_cleaning = 0; 2139 } 2140 } 2141 2142 /* 2143 * Called as part of a speculate() to get the speculative buffer associated 2144 * with a given speculation. Returns NULL if the specified speculation is not 2145 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2146 * the active CPU is not the specified CPU -- the speculation will be 2147 * atomically transitioned into the ACTIVEMANY state. 2148 */ 2149 static dtrace_buffer_t * 2150 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2151 dtrace_specid_t which) 2152 { 2153 dtrace_speculation_t *spec; 2154 dtrace_speculation_state_t current, new; 2155 dtrace_buffer_t *buf; 2156 2157 if (which == 0) 2158 return (NULL); 2159 2160 if (which > state->dts_nspeculations) { 2161 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2162 return (NULL); 2163 } 2164 2165 spec = &state->dts_speculations[which - 1]; 2166 buf = &spec->dtsp_buffer[cpuid]; 2167 2168 do { 2169 current = spec->dtsp_state; 2170 2171 switch (current) { 2172 case DTRACESPEC_INACTIVE: 2173 case DTRACESPEC_COMMITTINGMANY: 2174 case DTRACESPEC_DISCARDING: 2175 return (NULL); 2176 2177 case DTRACESPEC_COMMITTING: 2178 ASSERT(buf->dtb_offset == 0); 2179 return (NULL); 2180 2181 case DTRACESPEC_ACTIVEONE: 2182 /* 2183 * This speculation is currently active on one CPU. 2184 * Check the offset in the buffer; if it's non-zero, 2185 * that CPU must be us (and we leave the state alone). 2186 * If it's zero, assume that we're starting on a new 2187 * CPU -- and change the state to indicate that the 2188 * speculation is active on more than one CPU. 2189 */ 2190 if (buf->dtb_offset != 0) 2191 return (buf); 2192 2193 new = DTRACESPEC_ACTIVEMANY; 2194 break; 2195 2196 case DTRACESPEC_ACTIVEMANY: 2197 return (buf); 2198 2199 case DTRACESPEC_ACTIVE: 2200 new = DTRACESPEC_ACTIVEONE; 2201 break; 2202 2203 default: 2204 ASSERT(0); 2205 } 2206 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2207 current, new) != current); 2208 2209 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2210 return (buf); 2211 } 2212 2213 /* 2214 * This function implements the DIF emulator's variable lookups. The emulator 2215 * passes a reserved variable identifier and optional built-in array index. 2216 */ 2217 static uint64_t 2218 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2219 uint64_t ndx) 2220 { 2221 /* 2222 * If we're accessing one of the uncached arguments, we'll turn this 2223 * into a reference in the args array. 2224 */ 2225 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2226 ndx = v - DIF_VAR_ARG0; 2227 v = DIF_VAR_ARGS; 2228 } 2229 2230 switch (v) { 2231 case DIF_VAR_ARGS: 2232 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2233 if (ndx >= sizeof (mstate->dtms_arg) / 2234 sizeof (mstate->dtms_arg[0])) { 2235 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2236 dtrace_provider_t *pv; 2237 uint64_t val; 2238 2239 pv = mstate->dtms_probe->dtpr_provider; 2240 if (pv->dtpv_pops.dtps_getargval != NULL) 2241 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2242 mstate->dtms_probe->dtpr_id, 2243 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2244 else 2245 val = dtrace_getarg(ndx, aframes); 2246 2247 /* 2248 * This is regrettably required to keep the compiler 2249 * from tail-optimizing the call to dtrace_getarg(). 2250 * The condition always evaluates to true, but the 2251 * compiler has no way of figuring that out a priori. 2252 * (None of this would be necessary if the compiler 2253 * could be relied upon to _always_ tail-optimize 2254 * the call to dtrace_getarg() -- but it can't.) 2255 */ 2256 if (mstate->dtms_probe != NULL) 2257 return (val); 2258 2259 ASSERT(0); 2260 } 2261 2262 return (mstate->dtms_arg[ndx]); 2263 2264 case DIF_VAR_UREGS: { 2265 klwp_t *lwp; 2266 2267 if (!dtrace_priv_proc(state)) 2268 return (0); 2269 2270 if ((lwp = curthread->t_lwp) == NULL) { 2271 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2272 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL; 2273 return (0); 2274 } 2275 2276 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2277 } 2278 2279 case DIF_VAR_CURTHREAD: 2280 if (!dtrace_priv_kernel(state)) 2281 return (0); 2282 return ((uint64_t)(uintptr_t)curthread); 2283 2284 case DIF_VAR_TIMESTAMP: 2285 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2286 mstate->dtms_timestamp = dtrace_gethrtime(); 2287 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2288 } 2289 return (mstate->dtms_timestamp); 2290 2291 case DIF_VAR_VTIMESTAMP: 2292 ASSERT(dtrace_vtime_references != 0); 2293 return (curthread->t_dtrace_vtime); 2294 2295 case DIF_VAR_WALLTIMESTAMP: 2296 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2297 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2298 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2299 } 2300 return (mstate->dtms_walltimestamp); 2301 2302 case DIF_VAR_IPL: 2303 if (!dtrace_priv_kernel(state)) 2304 return (0); 2305 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2306 mstate->dtms_ipl = dtrace_getipl(); 2307 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2308 } 2309 return (mstate->dtms_ipl); 2310 2311 case DIF_VAR_EPID: 2312 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2313 return (mstate->dtms_epid); 2314 2315 case DIF_VAR_ID: 2316 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2317 return (mstate->dtms_probe->dtpr_id); 2318 2319 case DIF_VAR_STACKDEPTH: 2320 if (!dtrace_priv_kernel(state)) 2321 return (0); 2322 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2323 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2324 2325 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2326 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2327 } 2328 return (mstate->dtms_stackdepth); 2329 2330 case DIF_VAR_USTACKDEPTH: 2331 if (!dtrace_priv_proc(state)) 2332 return (0); 2333 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2334 /* 2335 * See comment in DIF_VAR_PID. 2336 */ 2337 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2338 CPU_ON_INTR(CPU)) { 2339 mstate->dtms_ustackdepth = 0; 2340 } else { 2341 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2342 mstate->dtms_ustackdepth = 2343 dtrace_getustackdepth(); 2344 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2345 } 2346 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2347 } 2348 return (mstate->dtms_ustackdepth); 2349 2350 case DIF_VAR_CALLER: 2351 if (!dtrace_priv_kernel(state)) 2352 return (0); 2353 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2354 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2355 2356 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2357 /* 2358 * If this is an unanchored probe, we are 2359 * required to go through the slow path: 2360 * dtrace_caller() only guarantees correct 2361 * results for anchored probes. 2362 */ 2363 pc_t caller[2]; 2364 2365 dtrace_getpcstack(caller, 2, aframes, 2366 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2367 mstate->dtms_caller = caller[1]; 2368 } else if ((mstate->dtms_caller = 2369 dtrace_caller(aframes)) == -1) { 2370 /* 2371 * We have failed to do this the quick way; 2372 * we must resort to the slower approach of 2373 * calling dtrace_getpcstack(). 2374 */ 2375 pc_t caller; 2376 2377 dtrace_getpcstack(&caller, 1, aframes, NULL); 2378 mstate->dtms_caller = caller; 2379 } 2380 2381 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 2382 } 2383 return (mstate->dtms_caller); 2384 2385 case DIF_VAR_UCALLER: 2386 if (!dtrace_priv_proc(state)) 2387 return (0); 2388 2389 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 2390 uint64_t ustack[3]; 2391 2392 /* 2393 * dtrace_getupcstack() fills in the first uint64_t 2394 * with the current PID. The second uint64_t will 2395 * be the program counter at user-level. The third 2396 * uint64_t will contain the caller, which is what 2397 * we're after. 2398 */ 2399 ustack[2] = NULL; 2400 dtrace_getupcstack(ustack, 3); 2401 mstate->dtms_ucaller = ustack[2]; 2402 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 2403 } 2404 2405 return (mstate->dtms_ucaller); 2406 2407 case DIF_VAR_PROBEPROV: 2408 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2409 return ((uint64_t)(uintptr_t) 2410 mstate->dtms_probe->dtpr_provider->dtpv_name); 2411 2412 case DIF_VAR_PROBEMOD: 2413 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2414 return ((uint64_t)(uintptr_t) 2415 mstate->dtms_probe->dtpr_mod); 2416 2417 case DIF_VAR_PROBEFUNC: 2418 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2419 return ((uint64_t)(uintptr_t) 2420 mstate->dtms_probe->dtpr_func); 2421 2422 case DIF_VAR_PROBENAME: 2423 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2424 return ((uint64_t)(uintptr_t) 2425 mstate->dtms_probe->dtpr_name); 2426 2427 case DIF_VAR_PID: 2428 if (!dtrace_priv_proc(state)) 2429 return (0); 2430 2431 /* 2432 * Note that we are assuming that an unanchored probe is 2433 * always due to a high-level interrupt. (And we're assuming 2434 * that there is only a single high level interrupt.) 2435 */ 2436 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2437 return (pid0.pid_id); 2438 2439 /* 2440 * It is always safe to dereference one's own t_procp pointer: 2441 * it always points to a valid, allocated proc structure. 2442 * Further, it is always safe to dereference the p_pidp member 2443 * of one's own proc structure. (These are truisms becuase 2444 * threads and processes don't clean up their own state -- 2445 * they leave that task to whomever reaps them.) 2446 */ 2447 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 2448 2449 case DIF_VAR_TID: 2450 /* 2451 * See comment in DIF_VAR_PID. 2452 */ 2453 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2454 return (0); 2455 2456 return ((uint64_t)curthread->t_tid); 2457 2458 case DIF_VAR_EXECNAME: 2459 if (!dtrace_priv_proc(state)) 2460 return (0); 2461 2462 /* 2463 * See comment in DIF_VAR_PID. 2464 */ 2465 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2466 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 2467 2468 /* 2469 * It is always safe to dereference one's own t_procp pointer: 2470 * it always points to a valid, allocated proc structure. 2471 * (This is true because threads don't clean up their own 2472 * state -- they leave that task to whomever reaps them.) 2473 */ 2474 return ((uint64_t)(uintptr_t) 2475 curthread->t_procp->p_user.u_comm); 2476 2477 case DIF_VAR_ZONENAME: 2478 if (!dtrace_priv_proc(state)) 2479 return (0); 2480 2481 /* 2482 * See comment in DIF_VAR_PID. 2483 */ 2484 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2485 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 2486 2487 /* 2488 * It is always safe to dereference one's own t_procp pointer: 2489 * it always points to a valid, allocated proc structure. 2490 * (This is true because threads don't clean up their own 2491 * state -- they leave that task to whomever reaps them.) 2492 */ 2493 return ((uint64_t)(uintptr_t) 2494 curthread->t_procp->p_zone->zone_name); 2495 2496 default: 2497 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2498 return (0); 2499 } 2500 } 2501 2502 /* 2503 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 2504 * Notice that we don't bother validating the proper number of arguments or 2505 * their types in the tuple stack. This isn't needed because all argument 2506 * interpretation is safe because of our load safety -- the worst that can 2507 * happen is that a bogus program can obtain bogus results. 2508 */ 2509 static void 2510 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 2511 dtrace_key_t *tupregs, int nargs, 2512 dtrace_mstate_t *mstate, dtrace_state_t *state) 2513 { 2514 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 2515 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 2516 2517 union { 2518 mutex_impl_t mi; 2519 uint64_t mx; 2520 } m; 2521 2522 union { 2523 krwlock_t ri; 2524 uintptr_t rw; 2525 } r; 2526 2527 switch (subr) { 2528 case DIF_SUBR_RAND: 2529 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 2530 break; 2531 2532 case DIF_SUBR_MUTEX_OWNED: 2533 m.mx = dtrace_load64(tupregs[0].dttk_value); 2534 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 2535 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 2536 else 2537 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 2538 break; 2539 2540 case DIF_SUBR_MUTEX_OWNER: 2541 m.mx = dtrace_load64(tupregs[0].dttk_value); 2542 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 2543 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 2544 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 2545 else 2546 regs[rd] = 0; 2547 break; 2548 2549 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 2550 m.mx = dtrace_load64(tupregs[0].dttk_value); 2551 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 2552 break; 2553 2554 case DIF_SUBR_MUTEX_TYPE_SPIN: 2555 m.mx = dtrace_load64(tupregs[0].dttk_value); 2556 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 2557 break; 2558 2559 case DIF_SUBR_RW_READ_HELD: { 2560 uintptr_t tmp; 2561 2562 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2563 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 2564 break; 2565 } 2566 2567 case DIF_SUBR_RW_WRITE_HELD: 2568 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2569 regs[rd] = _RW_WRITE_HELD(&r.ri); 2570 break; 2571 2572 case DIF_SUBR_RW_ISWRITER: 2573 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2574 regs[rd] = _RW_ISWRITER(&r.ri); 2575 break; 2576 2577 case DIF_SUBR_BCOPY: { 2578 /* 2579 * We need to be sure that the destination is in the scratch 2580 * region -- no other region is allowed. 2581 */ 2582 uintptr_t src = tupregs[0].dttk_value; 2583 uintptr_t dest = tupregs[1].dttk_value; 2584 size_t size = tupregs[2].dttk_value; 2585 2586 if (!dtrace_inscratch(dest, size, mstate)) { 2587 *flags |= CPU_DTRACE_BADADDR; 2588 *illval = regs[rd]; 2589 break; 2590 } 2591 2592 dtrace_bcopy((void *)src, (void *)dest, size); 2593 break; 2594 } 2595 2596 case DIF_SUBR_ALLOCA: 2597 case DIF_SUBR_COPYIN: { 2598 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 2599 uint64_t size = 2600 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 2601 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 2602 2603 /* 2604 * This action doesn't require any credential checks since 2605 * probes will not activate in user contexts to which the 2606 * enabling user does not have permissions. 2607 */ 2608 if (mstate->dtms_scratch_ptr + scratch_size > 2609 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2610 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2611 regs[rd] = NULL; 2612 break; 2613 } 2614 2615 if (subr == DIF_SUBR_COPYIN) { 2616 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2617 dtrace_copyin(tupregs[0].dttk_value, dest, size); 2618 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2619 } 2620 2621 mstate->dtms_scratch_ptr += scratch_size; 2622 regs[rd] = dest; 2623 break; 2624 } 2625 2626 case DIF_SUBR_COPYINTO: { 2627 uint64_t size = tupregs[1].dttk_value; 2628 uintptr_t dest = tupregs[2].dttk_value; 2629 2630 /* 2631 * This action doesn't require any credential checks since 2632 * probes will not activate in user contexts to which the 2633 * enabling user does not have permissions. 2634 */ 2635 if (!dtrace_inscratch(dest, size, mstate)) { 2636 *flags |= CPU_DTRACE_BADADDR; 2637 *illval = regs[rd]; 2638 break; 2639 } 2640 2641 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2642 dtrace_copyin(tupregs[0].dttk_value, dest, size); 2643 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2644 break; 2645 } 2646 2647 case DIF_SUBR_COPYINSTR: { 2648 uintptr_t dest = mstate->dtms_scratch_ptr; 2649 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2650 2651 if (nargs > 1 && tupregs[1].dttk_value < size) 2652 size = tupregs[1].dttk_value + 1; 2653 2654 /* 2655 * This action doesn't require any credential checks since 2656 * probes will not activate in user contexts to which the 2657 * enabling user does not have permissions. 2658 */ 2659 if (mstate->dtms_scratch_ptr + size > 2660 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2661 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2662 regs[rd] = NULL; 2663 break; 2664 } 2665 2666 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2667 dtrace_copyinstr(tupregs[0].dttk_value, dest, size); 2668 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2669 2670 ((char *)dest)[size - 1] = '\0'; 2671 mstate->dtms_scratch_ptr += size; 2672 regs[rd] = dest; 2673 break; 2674 } 2675 2676 case DIF_SUBR_MSGSIZE: 2677 case DIF_SUBR_MSGDSIZE: { 2678 uintptr_t baddr = tupregs[0].dttk_value, daddr; 2679 uintptr_t wptr, rptr; 2680 size_t count = 0; 2681 int cont = 0; 2682 2683 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 2684 wptr = dtrace_loadptr(baddr + 2685 offsetof(mblk_t, b_wptr)); 2686 2687 rptr = dtrace_loadptr(baddr + 2688 offsetof(mblk_t, b_rptr)); 2689 2690 if (wptr < rptr) { 2691 *flags |= CPU_DTRACE_BADADDR; 2692 *illval = tupregs[0].dttk_value; 2693 break; 2694 } 2695 2696 daddr = dtrace_loadptr(baddr + 2697 offsetof(mblk_t, b_datap)); 2698 2699 baddr = dtrace_loadptr(baddr + 2700 offsetof(mblk_t, b_cont)); 2701 2702 /* 2703 * We want to prevent against denial-of-service here, 2704 * so we're only going to search the list for 2705 * dtrace_msgdsize_max mblks. 2706 */ 2707 if (cont++ > dtrace_msgdsize_max) { 2708 *flags |= CPU_DTRACE_ILLOP; 2709 break; 2710 } 2711 2712 if (subr == DIF_SUBR_MSGDSIZE) { 2713 if (dtrace_load8(daddr + 2714 offsetof(dblk_t, db_type)) != M_DATA) 2715 continue; 2716 } 2717 2718 count += wptr - rptr; 2719 } 2720 2721 if (!(*flags & CPU_DTRACE_FAULT)) 2722 regs[rd] = count; 2723 2724 break; 2725 } 2726 2727 case DIF_SUBR_PROGENYOF: { 2728 pid_t pid = tupregs[0].dttk_value; 2729 proc_t *p; 2730 int rval = 0; 2731 2732 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2733 2734 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 2735 if (p->p_pidp->pid_id == pid) { 2736 rval = 1; 2737 break; 2738 } 2739 } 2740 2741 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2742 2743 regs[rd] = rval; 2744 break; 2745 } 2746 2747 case DIF_SUBR_SPECULATION: 2748 regs[rd] = dtrace_speculation(state); 2749 break; 2750 2751 case DIF_SUBR_COPYOUT: { 2752 uintptr_t kaddr = tupregs[0].dttk_value; 2753 uintptr_t uaddr = tupregs[1].dttk_value; 2754 uint64_t size = tupregs[2].dttk_value; 2755 2756 if (!dtrace_destructive_disallow && 2757 dtrace_priv_proc_control(state) && 2758 !dtrace_istoxic(kaddr, size)) { 2759 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2760 dtrace_copyout(kaddr, uaddr, size); 2761 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2762 } 2763 break; 2764 } 2765 2766 case DIF_SUBR_COPYOUTSTR: { 2767 uintptr_t kaddr = tupregs[0].dttk_value; 2768 uintptr_t uaddr = tupregs[1].dttk_value; 2769 uint64_t size = tupregs[2].dttk_value; 2770 2771 if (!dtrace_destructive_disallow && 2772 dtrace_priv_proc_control(state) && 2773 !dtrace_istoxic(kaddr, size)) { 2774 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2775 dtrace_copyoutstr(kaddr, uaddr, size); 2776 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2777 } 2778 break; 2779 } 2780 2781 case DIF_SUBR_STRLEN: 2782 regs[rd] = dtrace_strlen((char *)(uintptr_t) 2783 tupregs[0].dttk_value, 2784 state->dts_options[DTRACEOPT_STRSIZE]); 2785 break; 2786 2787 case DIF_SUBR_STRCHR: 2788 case DIF_SUBR_STRRCHR: { 2789 /* 2790 * We're going to iterate over the string looking for the 2791 * specified character. We will iterate until we have reached 2792 * the string length or we have found the character. If this 2793 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 2794 * of the specified character instead of the first. 2795 */ 2796 uintptr_t addr = tupregs[0].dttk_value; 2797 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 2798 char c, target = (char)tupregs[1].dttk_value; 2799 2800 for (regs[rd] = NULL; addr < limit; addr++) { 2801 if ((c = dtrace_load8(addr)) == target) { 2802 regs[rd] = addr; 2803 2804 if (subr == DIF_SUBR_STRCHR) 2805 break; 2806 } 2807 2808 if (c == '\0') 2809 break; 2810 } 2811 2812 break; 2813 } 2814 2815 case DIF_SUBR_STRSTR: 2816 case DIF_SUBR_INDEX: 2817 case DIF_SUBR_RINDEX: { 2818 /* 2819 * We're going to iterate over the string looking for the 2820 * specified string. We will iterate until we have reached 2821 * the string length or we have found the string. (Yes, this 2822 * is done in the most naive way possible -- but considering 2823 * that the string we're searching for is likely to be 2824 * relatively short, the complexity of Rabin-Karp or similar 2825 * hardly seems merited.) 2826 */ 2827 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 2828 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 2829 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2830 size_t len = dtrace_strlen(addr, size); 2831 size_t sublen = dtrace_strlen(substr, size); 2832 char *limit = addr + len, *orig = addr; 2833 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 2834 int inc = 1; 2835 2836 regs[rd] = notfound; 2837 2838 /* 2839 * strstr() and index()/rindex() have similar semantics if 2840 * both strings are the empty string: strstr() returns a 2841 * pointer to the (empty) string, and index() and rindex() 2842 * both return index 0 (regardless of any position argument). 2843 */ 2844 if (sublen == 0 && len == 0) { 2845 if (subr == DIF_SUBR_STRSTR) 2846 regs[rd] = (uintptr_t)addr; 2847 else 2848 regs[rd] = 0; 2849 break; 2850 } 2851 2852 if (subr != DIF_SUBR_STRSTR) { 2853 if (subr == DIF_SUBR_RINDEX) { 2854 limit = orig - 1; 2855 addr += len; 2856 inc = -1; 2857 } 2858 2859 /* 2860 * Both index() and rindex() take an optional position 2861 * argument that denotes the starting position. 2862 */ 2863 if (nargs == 3) { 2864 int64_t pos = (int64_t)tupregs[2].dttk_value; 2865 2866 /* 2867 * If the position argument to index() is 2868 * negative, Perl implicitly clamps it at 2869 * zero. This semantic is a little surprising 2870 * given the special meaning of negative 2871 * positions to similar Perl functions like 2872 * substr(), but it appears to reflect a 2873 * notion that index() can start from a 2874 * negative index and increment its way up to 2875 * the string. Given this notion, Perl's 2876 * rindex() is at least self-consistent in 2877 * that it implicitly clamps positions greater 2878 * than the string length to be the string 2879 * length. Where Perl completely loses 2880 * coherence, however, is when the specified 2881 * substring is the empty string (""). In 2882 * this case, even if the position is 2883 * negative, rindex() returns 0 -- and even if 2884 * the position is greater than the length, 2885 * index() returns the string length. These 2886 * semantics violate the notion that index() 2887 * should never return a value less than the 2888 * specified position and that rindex() should 2889 * never return a value greater than the 2890 * specified position. (One assumes that 2891 * these semantics are artifacts of Perl's 2892 * implementation and not the results of 2893 * deliberate design -- it beggars belief that 2894 * even Larry Wall could desire such oddness.) 2895 * While in the abstract one would wish for 2896 * consistent position semantics across 2897 * substr(), index() and rindex() -- or at the 2898 * very least self-consistent position 2899 * semantics for index() and rindex() -- we 2900 * instead opt to keep with the extant Perl 2901 * semantics, in all their broken glory. (Do 2902 * we have more desire to maintain Perl's 2903 * semantics than Perl does? Probably.) 2904 */ 2905 if (subr == DIF_SUBR_RINDEX) { 2906 if (pos < 0) { 2907 if (sublen == 0) 2908 regs[rd] = 0; 2909 break; 2910 } 2911 2912 if (pos > len) 2913 pos = len; 2914 } else { 2915 if (pos < 0) 2916 pos = 0; 2917 2918 if (pos >= len) { 2919 if (sublen == 0) 2920 regs[rd] = len; 2921 break; 2922 } 2923 } 2924 2925 addr = orig + pos; 2926 } 2927 } 2928 2929 for (regs[rd] = notfound; addr != limit; addr += inc) { 2930 if (dtrace_strncmp(addr, substr, sublen) == 0) { 2931 if (subr != DIF_SUBR_STRSTR) { 2932 /* 2933 * As D index() and rindex() are 2934 * modeled on Perl (and not on awk), 2935 * we return a zero-based (and not a 2936 * one-based) index. (For you Perl 2937 * weenies: no, we're not going to add 2938 * $[ -- and shouldn't you be at a con 2939 * or something?) 2940 */ 2941 regs[rd] = (uintptr_t)(addr - orig); 2942 break; 2943 } 2944 2945 ASSERT(subr == DIF_SUBR_STRSTR); 2946 regs[rd] = (uintptr_t)addr; 2947 break; 2948 } 2949 } 2950 2951 break; 2952 } 2953 2954 case DIF_SUBR_STRTOK: { 2955 uintptr_t addr = tupregs[0].dttk_value; 2956 uintptr_t tokaddr = tupregs[1].dttk_value; 2957 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2958 uintptr_t limit, toklimit = tokaddr + size; 2959 uint8_t c, tokmap[32]; /* 256 / 8 */ 2960 char *dest = (char *)mstate->dtms_scratch_ptr; 2961 int i; 2962 2963 if (mstate->dtms_scratch_ptr + size > 2964 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2965 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2966 regs[rd] = NULL; 2967 break; 2968 } 2969 2970 if (addr == NULL) { 2971 /* 2972 * If the address specified is NULL, we use our saved 2973 * strtok pointer from the mstate. Note that this 2974 * means that the saved strtok pointer is _only_ 2975 * valid within multiple enablings of the same probe -- 2976 * it behaves like an implicit clause-local variable. 2977 */ 2978 addr = mstate->dtms_strtok; 2979 } 2980 2981 /* 2982 * First, zero the token map, and then process the token 2983 * string -- setting a bit in the map for every character 2984 * found in the token string. 2985 */ 2986 for (i = 0; i < sizeof (tokmap); i++) 2987 tokmap[i] = 0; 2988 2989 for (; tokaddr < toklimit; tokaddr++) { 2990 if ((c = dtrace_load8(tokaddr)) == '\0') 2991 break; 2992 2993 ASSERT((c >> 3) < sizeof (tokmap)); 2994 tokmap[c >> 3] |= (1 << (c & 0x7)); 2995 } 2996 2997 for (limit = addr + size; addr < limit; addr++) { 2998 /* 2999 * We're looking for a character that is _not_ contained 3000 * in the token string. 3001 */ 3002 if ((c = dtrace_load8(addr)) == '\0') 3003 break; 3004 3005 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3006 break; 3007 } 3008 3009 if (c == '\0') { 3010 /* 3011 * We reached the end of the string without finding 3012 * any character that was not in the token string. 3013 * We return NULL in this case, and we set the saved 3014 * address to NULL as well. 3015 */ 3016 regs[rd] = NULL; 3017 mstate->dtms_strtok = NULL; 3018 break; 3019 } 3020 3021 /* 3022 * From here on, we're copying into the destination string. 3023 */ 3024 for (i = 0; addr < limit && i < size - 1; addr++) { 3025 if ((c = dtrace_load8(addr)) == '\0') 3026 break; 3027 3028 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3029 break; 3030 3031 ASSERT(i < size); 3032 dest[i++] = c; 3033 } 3034 3035 ASSERT(i < size); 3036 dest[i] = '\0'; 3037 regs[rd] = (uintptr_t)dest; 3038 mstate->dtms_scratch_ptr += size; 3039 mstate->dtms_strtok = addr; 3040 break; 3041 } 3042 3043 case DIF_SUBR_SUBSTR: { 3044 uintptr_t s = tupregs[0].dttk_value; 3045 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3046 char *d = (char *)mstate->dtms_scratch_ptr; 3047 int64_t index = (int64_t)tupregs[1].dttk_value; 3048 int64_t remaining = (int64_t)tupregs[2].dttk_value; 3049 size_t len = dtrace_strlen((char *)s, size); 3050 int64_t i = 0; 3051 3052 if (nargs <= 2) 3053 remaining = (int64_t)size; 3054 3055 if (mstate->dtms_scratch_ptr + size > 3056 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3057 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3058 regs[rd] = NULL; 3059 break; 3060 } 3061 3062 if (index < 0) { 3063 index += len; 3064 3065 if (index < 0 && index + remaining > 0) { 3066 remaining += index; 3067 index = 0; 3068 } 3069 } 3070 3071 if (index >= len || index < 0) 3072 index = len; 3073 3074 for (d[0] = '\0'; remaining > 0; remaining--) { 3075 if ((d[i++] = dtrace_load8(s++ + index)) == '\0') 3076 break; 3077 3078 if (i == size) { 3079 d[i - 1] = '\0'; 3080 break; 3081 } 3082 } 3083 3084 mstate->dtms_scratch_ptr += size; 3085 regs[rd] = (uintptr_t)d; 3086 break; 3087 } 3088 3089 case DIF_SUBR_GETMAJOR: 3090 #ifdef _LP64 3091 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 3092 #else 3093 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 3094 #endif 3095 break; 3096 3097 case DIF_SUBR_GETMINOR: 3098 #ifdef _LP64 3099 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 3100 #else 3101 regs[rd] = tupregs[0].dttk_value & MAXMIN; 3102 #endif 3103 break; 3104 3105 case DIF_SUBR_DDI_PATHNAME: { 3106 /* 3107 * This one is a galactic mess. We are going to roughly 3108 * emulate ddi_pathname(), but it's made more complicated 3109 * by the fact that we (a) want to include the minor name and 3110 * (b) must proceed iteratively instead of recursively. 3111 */ 3112 uintptr_t dest = mstate->dtms_scratch_ptr; 3113 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3114 char *start = (char *)dest, *end = start + size - 1; 3115 uintptr_t daddr = tupregs[0].dttk_value; 3116 int64_t minor = (int64_t)tupregs[1].dttk_value; 3117 char *s; 3118 int i, len, depth = 0; 3119 3120 if (size == 0 || mstate->dtms_scratch_ptr + size > 3121 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3122 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3123 regs[rd] = NULL; 3124 break; 3125 } 3126 3127 *end = '\0'; 3128 3129 /* 3130 * We want to have a name for the minor. In order to do this, 3131 * we need to walk the minor list from the devinfo. We want 3132 * to be sure that we don't infinitely walk a circular list, 3133 * so we check for circularity by sending a scout pointer 3134 * ahead two elements for every element that we iterate over; 3135 * if the list is circular, these will ultimately point to the 3136 * same element. You may recognize this little trick as the 3137 * answer to a stupid interview question -- one that always 3138 * seems to be asked by those who had to have it laboriously 3139 * explained to them, and who can't even concisely describe 3140 * the conditions under which one would be forced to resort to 3141 * this technique. Needless to say, those conditions are 3142 * found here -- and probably only here. Is this is the only 3143 * use of this infamous trick in shipping, production code? 3144 * If it isn't, it probably should be... 3145 */ 3146 if (minor != -1) { 3147 uintptr_t maddr = dtrace_loadptr(daddr + 3148 offsetof(struct dev_info, devi_minor)); 3149 3150 uintptr_t next = offsetof(struct ddi_minor_data, next); 3151 uintptr_t name = offsetof(struct ddi_minor_data, 3152 d_minor) + offsetof(struct ddi_minor, name); 3153 uintptr_t dev = offsetof(struct ddi_minor_data, 3154 d_minor) + offsetof(struct ddi_minor, dev); 3155 uintptr_t scout; 3156 3157 if (maddr != NULL) 3158 scout = dtrace_loadptr(maddr + next); 3159 3160 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3161 uint64_t m; 3162 #ifdef _LP64 3163 m = dtrace_load64(maddr + dev) & MAXMIN64; 3164 #else 3165 m = dtrace_load32(maddr + dev) & MAXMIN; 3166 #endif 3167 if (m != minor) { 3168 maddr = dtrace_loadptr(maddr + next); 3169 3170 if (scout == NULL) 3171 continue; 3172 3173 scout = dtrace_loadptr(scout + next); 3174 3175 if (scout == NULL) 3176 continue; 3177 3178 scout = dtrace_loadptr(scout + next); 3179 3180 if (scout == NULL) 3181 continue; 3182 3183 if (scout == maddr) { 3184 *flags |= CPU_DTRACE_ILLOP; 3185 break; 3186 } 3187 3188 continue; 3189 } 3190 3191 /* 3192 * We have the minor data. Now we need to 3193 * copy the minor's name into the end of the 3194 * pathname. 3195 */ 3196 s = (char *)dtrace_loadptr(maddr + name); 3197 len = dtrace_strlen(s, size); 3198 3199 if (*flags & CPU_DTRACE_FAULT) 3200 break; 3201 3202 if (len != 0) { 3203 if ((end -= (len + 1)) < start) 3204 break; 3205 3206 *end = ':'; 3207 } 3208 3209 for (i = 1; i <= len; i++) 3210 end[i] = dtrace_load8((uintptr_t)s++); 3211 break; 3212 } 3213 } 3214 3215 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3216 ddi_node_state_t devi_state; 3217 3218 devi_state = dtrace_load32(daddr + 3219 offsetof(struct dev_info, devi_node_state)); 3220 3221 if (*flags & CPU_DTRACE_FAULT) 3222 break; 3223 3224 if (devi_state >= DS_INITIALIZED) { 3225 s = (char *)dtrace_loadptr(daddr + 3226 offsetof(struct dev_info, devi_addr)); 3227 len = dtrace_strlen(s, size); 3228 3229 if (*flags & CPU_DTRACE_FAULT) 3230 break; 3231 3232 if (len != 0) { 3233 if ((end -= (len + 1)) < start) 3234 break; 3235 3236 *end = '@'; 3237 } 3238 3239 for (i = 1; i <= len; i++) 3240 end[i] = dtrace_load8((uintptr_t)s++); 3241 } 3242 3243 /* 3244 * Now for the node name... 3245 */ 3246 s = (char *)dtrace_loadptr(daddr + 3247 offsetof(struct dev_info, devi_node_name)); 3248 3249 daddr = dtrace_loadptr(daddr + 3250 offsetof(struct dev_info, devi_parent)); 3251 3252 /* 3253 * If our parent is NULL (that is, if we're the root 3254 * node), we're going to use the special path 3255 * "devices". 3256 */ 3257 if (daddr == NULL) 3258 s = "devices"; 3259 3260 len = dtrace_strlen(s, size); 3261 if (*flags & CPU_DTRACE_FAULT) 3262 break; 3263 3264 if ((end -= (len + 1)) < start) 3265 break; 3266 3267 for (i = 1; i <= len; i++) 3268 end[i] = dtrace_load8((uintptr_t)s++); 3269 *end = '/'; 3270 3271 if (depth++ > dtrace_devdepth_max) { 3272 *flags |= CPU_DTRACE_ILLOP; 3273 break; 3274 } 3275 } 3276 3277 if (end < start) 3278 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3279 3280 if (daddr == NULL) { 3281 regs[rd] = (uintptr_t)end; 3282 mstate->dtms_scratch_ptr += size; 3283 } 3284 3285 break; 3286 } 3287 3288 case DIF_SUBR_STRJOIN: { 3289 char *d = (char *)mstate->dtms_scratch_ptr; 3290 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3291 uintptr_t s1 = tupregs[0].dttk_value; 3292 uintptr_t s2 = tupregs[1].dttk_value; 3293 int i = 0; 3294 3295 if (mstate->dtms_scratch_ptr + size > 3296 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3297 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3298 regs[rd] = NULL; 3299 break; 3300 } 3301 3302 for (;;) { 3303 if (i >= size) { 3304 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3305 regs[rd] = NULL; 3306 break; 3307 } 3308 3309 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 3310 i--; 3311 break; 3312 } 3313 } 3314 3315 for (;;) { 3316 if (i >= size) { 3317 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3318 regs[rd] = NULL; 3319 break; 3320 } 3321 3322 if ((d[i++] = dtrace_load8(s2++)) == '\0') 3323 break; 3324 } 3325 3326 if (i < size) { 3327 mstate->dtms_scratch_ptr += i; 3328 regs[rd] = (uintptr_t)d; 3329 } 3330 3331 break; 3332 } 3333 3334 case DIF_SUBR_LLTOSTR: { 3335 int64_t i = (int64_t)tupregs[0].dttk_value; 3336 int64_t val = i < 0 ? i * -1 : i; 3337 uint64_t size = 22; /* enough room for 2^64 in decimal */ 3338 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 3339 3340 if (mstate->dtms_scratch_ptr + size > 3341 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3342 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3343 regs[rd] = NULL; 3344 break; 3345 } 3346 3347 for (*end-- = '\0'; val; val /= 10) 3348 *end-- = '0' + (val % 10); 3349 3350 if (i == 0) 3351 *end-- = '0'; 3352 3353 if (i < 0) 3354 *end-- = '-'; 3355 3356 regs[rd] = (uintptr_t)end + 1; 3357 mstate->dtms_scratch_ptr += size; 3358 break; 3359 } 3360 3361 case DIF_SUBR_DIRNAME: 3362 case DIF_SUBR_BASENAME: { 3363 char *dest = (char *)mstate->dtms_scratch_ptr; 3364 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3365 uintptr_t src = tupregs[0].dttk_value; 3366 int i, j, len = dtrace_strlen((char *)src, size); 3367 int lastbase = -1, firstbase = -1, lastdir = -1; 3368 int start, end; 3369 3370 if (mstate->dtms_scratch_ptr + size > 3371 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3372 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3373 regs[rd] = NULL; 3374 break; 3375 } 3376 3377 /* 3378 * The basename and dirname for a zero-length string is 3379 * defined to be "." 3380 */ 3381 if (len == 0) { 3382 len = 1; 3383 src = (uintptr_t)"."; 3384 } 3385 3386 /* 3387 * Start from the back of the string, moving back toward the 3388 * front until we see a character that isn't a slash. That 3389 * character is the last character in the basename. 3390 */ 3391 for (i = len - 1; i >= 0; i--) { 3392 if (dtrace_load8(src + i) != '/') 3393 break; 3394 } 3395 3396 if (i >= 0) 3397 lastbase = i; 3398 3399 /* 3400 * Starting from the last character in the basename, move 3401 * towards the front until we find a slash. The character 3402 * that we processed immediately before that is the first 3403 * character in the basename. 3404 */ 3405 for (; i >= 0; i--) { 3406 if (dtrace_load8(src + i) == '/') 3407 break; 3408 } 3409 3410 if (i >= 0) 3411 firstbase = i + 1; 3412 3413 /* 3414 * Now keep going until we find a non-slash character. That 3415 * character is the last character in the dirname. 3416 */ 3417 for (; i >= 0; i--) { 3418 if (dtrace_load8(src + i) != '/') 3419 break; 3420 } 3421 3422 if (i >= 0) 3423 lastdir = i; 3424 3425 ASSERT(!(lastbase == -1 && firstbase != -1)); 3426 ASSERT(!(firstbase == -1 && lastdir != -1)); 3427 3428 if (lastbase == -1) { 3429 /* 3430 * We didn't find a non-slash character. We know that 3431 * the length is non-zero, so the whole string must be 3432 * slashes. In either the dirname or the basename 3433 * case, we return '/'. 3434 */ 3435 ASSERT(firstbase == -1); 3436 firstbase = lastbase = lastdir = 0; 3437 } 3438 3439 if (firstbase == -1) { 3440 /* 3441 * The entire string consists only of a basename 3442 * component. If we're looking for dirname, we need 3443 * to change our string to be just "."; if we're 3444 * looking for a basename, we'll just set the first 3445 * character of the basename to be 0. 3446 */ 3447 if (subr == DIF_SUBR_DIRNAME) { 3448 ASSERT(lastdir == -1); 3449 src = (uintptr_t)"."; 3450 lastdir = 0; 3451 } else { 3452 firstbase = 0; 3453 } 3454 } 3455 3456 if (subr == DIF_SUBR_DIRNAME) { 3457 if (lastdir == -1) { 3458 /* 3459 * We know that we have a slash in the name -- 3460 * or lastdir would be set to 0, above. And 3461 * because lastdir is -1, we know that this 3462 * slash must be the first character. (That 3463 * is, the full string must be of the form 3464 * "/basename".) In this case, the last 3465 * character of the directory name is 0. 3466 */ 3467 lastdir = 0; 3468 } 3469 3470 start = 0; 3471 end = lastdir; 3472 } else { 3473 ASSERT(subr == DIF_SUBR_BASENAME); 3474 ASSERT(firstbase != -1 && lastbase != -1); 3475 start = firstbase; 3476 end = lastbase; 3477 } 3478 3479 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 3480 dest[j] = dtrace_load8(src + i); 3481 3482 dest[j] = '\0'; 3483 regs[rd] = (uintptr_t)dest; 3484 mstate->dtms_scratch_ptr += size; 3485 break; 3486 } 3487 3488 case DIF_SUBR_CLEANPATH: { 3489 char *dest = (char *)mstate->dtms_scratch_ptr, c; 3490 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3491 uintptr_t src = tupregs[0].dttk_value; 3492 int i = 0, j = 0; 3493 3494 if (mstate->dtms_scratch_ptr + size > 3495 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3496 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3497 regs[rd] = NULL; 3498 break; 3499 } 3500 3501 /* 3502 * Move forward, loading each character. 3503 */ 3504 do { 3505 c = dtrace_load8(src + i++); 3506 next: 3507 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 3508 break; 3509 3510 if (c != '/') { 3511 dest[j++] = c; 3512 continue; 3513 } 3514 3515 c = dtrace_load8(src + i++); 3516 3517 if (c == '/') { 3518 /* 3519 * We have two slashes -- we can just advance 3520 * to the next character. 3521 */ 3522 goto next; 3523 } 3524 3525 if (c != '.') { 3526 /* 3527 * This is not "." and it's not ".." -- we can 3528 * just store the "/" and this character and 3529 * drive on. 3530 */ 3531 dest[j++] = '/'; 3532 dest[j++] = c; 3533 continue; 3534 } 3535 3536 c = dtrace_load8(src + i++); 3537 3538 if (c == '/') { 3539 /* 3540 * This is a "/./" component. We're not going 3541 * to store anything in the destination buffer; 3542 * we're just going to go to the next component. 3543 */ 3544 goto next; 3545 } 3546 3547 if (c != '.') { 3548 /* 3549 * This is not ".." -- we can just store the 3550 * "/." and this character and continue 3551 * processing. 3552 */ 3553 dest[j++] = '/'; 3554 dest[j++] = '.'; 3555 dest[j++] = c; 3556 continue; 3557 } 3558 3559 c = dtrace_load8(src + i++); 3560 3561 if (c != '/' && c != '\0') { 3562 /* 3563 * This is not ".." -- it's "..[mumble]". 3564 * We'll store the "/.." and this character 3565 * and continue processing. 3566 */ 3567 dest[j++] = '/'; 3568 dest[j++] = '.'; 3569 dest[j++] = '.'; 3570 dest[j++] = c; 3571 continue; 3572 } 3573 3574 /* 3575 * This is "/../" or "/..\0". We need to back up 3576 * our destination pointer until we find a "/". 3577 */ 3578 i--; 3579 while (j != 0 && dest[--j] != '/') 3580 continue; 3581 3582 if (c == '\0') 3583 dest[++j] = '/'; 3584 } while (c != '\0'); 3585 3586 dest[j] = '\0'; 3587 regs[rd] = (uintptr_t)dest; 3588 mstate->dtms_scratch_ptr += size; 3589 break; 3590 } 3591 } 3592 } 3593 3594 /* 3595 * Emulate the execution of DTrace IR instructions specified by the given 3596 * DIF object. This function is deliberately void of assertions as all of 3597 * the necessary checks are handled by a call to dtrace_difo_validate(). 3598 */ 3599 static uint64_t 3600 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 3601 dtrace_vstate_t *vstate, dtrace_state_t *state) 3602 { 3603 const dif_instr_t *text = difo->dtdo_buf; 3604 const uint_t textlen = difo->dtdo_len; 3605 const char *strtab = difo->dtdo_strtab; 3606 const uint64_t *inttab = difo->dtdo_inttab; 3607 3608 uint64_t rval = 0; 3609 dtrace_statvar_t *svar; 3610 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 3611 dtrace_difv_t *v; 3612 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 3613 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 3614 3615 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 3616 uint64_t regs[DIF_DIR_NREGS]; 3617 uint64_t *tmp; 3618 3619 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 3620 int64_t cc_r; 3621 uint_t pc = 0, id, opc; 3622 uint8_t ttop = 0; 3623 dif_instr_t instr; 3624 uint_t r1, r2, rd; 3625 3626 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 3627 3628 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 3629 opc = pc; 3630 3631 instr = text[pc++]; 3632 r1 = DIF_INSTR_R1(instr); 3633 r2 = DIF_INSTR_R2(instr); 3634 rd = DIF_INSTR_RD(instr); 3635 3636 switch (DIF_INSTR_OP(instr)) { 3637 case DIF_OP_OR: 3638 regs[rd] = regs[r1] | regs[r2]; 3639 break; 3640 case DIF_OP_XOR: 3641 regs[rd] = regs[r1] ^ regs[r2]; 3642 break; 3643 case DIF_OP_AND: 3644 regs[rd] = regs[r1] & regs[r2]; 3645 break; 3646 case DIF_OP_SLL: 3647 regs[rd] = regs[r1] << regs[r2]; 3648 break; 3649 case DIF_OP_SRL: 3650 regs[rd] = regs[r1] >> regs[r2]; 3651 break; 3652 case DIF_OP_SUB: 3653 regs[rd] = regs[r1] - regs[r2]; 3654 break; 3655 case DIF_OP_ADD: 3656 regs[rd] = regs[r1] + regs[r2]; 3657 break; 3658 case DIF_OP_MUL: 3659 regs[rd] = regs[r1] * regs[r2]; 3660 break; 3661 case DIF_OP_SDIV: 3662 if (regs[r2] == 0) { 3663 regs[rd] = 0; 3664 *flags |= CPU_DTRACE_DIVZERO; 3665 } else { 3666 regs[rd] = (int64_t)regs[r1] / 3667 (int64_t)regs[r2]; 3668 } 3669 break; 3670 3671 case DIF_OP_UDIV: 3672 if (regs[r2] == 0) { 3673 regs[rd] = 0; 3674 *flags |= CPU_DTRACE_DIVZERO; 3675 } else { 3676 regs[rd] = regs[r1] / regs[r2]; 3677 } 3678 break; 3679 3680 case DIF_OP_SREM: 3681 if (regs[r2] == 0) { 3682 regs[rd] = 0; 3683 *flags |= CPU_DTRACE_DIVZERO; 3684 } else { 3685 regs[rd] = (int64_t)regs[r1] % 3686 (int64_t)regs[r2]; 3687 } 3688 break; 3689 3690 case DIF_OP_UREM: 3691 if (regs[r2] == 0) { 3692 regs[rd] = 0; 3693 *flags |= CPU_DTRACE_DIVZERO; 3694 } else { 3695 regs[rd] = regs[r1] % regs[r2]; 3696 } 3697 break; 3698 3699 case DIF_OP_NOT: 3700 regs[rd] = ~regs[r1]; 3701 break; 3702 case DIF_OP_MOV: 3703 regs[rd] = regs[r1]; 3704 break; 3705 case DIF_OP_CMP: 3706 cc_r = regs[r1] - regs[r2]; 3707 cc_n = cc_r < 0; 3708 cc_z = cc_r == 0; 3709 cc_v = 0; 3710 cc_c = regs[r1] < regs[r2]; 3711 break; 3712 case DIF_OP_TST: 3713 cc_n = cc_v = cc_c = 0; 3714 cc_z = regs[r1] == 0; 3715 break; 3716 case DIF_OP_BA: 3717 pc = DIF_INSTR_LABEL(instr); 3718 break; 3719 case DIF_OP_BE: 3720 if (cc_z) 3721 pc = DIF_INSTR_LABEL(instr); 3722 break; 3723 case DIF_OP_BNE: 3724 if (cc_z == 0) 3725 pc = DIF_INSTR_LABEL(instr); 3726 break; 3727 case DIF_OP_BG: 3728 if ((cc_z | (cc_n ^ cc_v)) == 0) 3729 pc = DIF_INSTR_LABEL(instr); 3730 break; 3731 case DIF_OP_BGU: 3732 if ((cc_c | cc_z) == 0) 3733 pc = DIF_INSTR_LABEL(instr); 3734 break; 3735 case DIF_OP_BGE: 3736 if ((cc_n ^ cc_v) == 0) 3737 pc = DIF_INSTR_LABEL(instr); 3738 break; 3739 case DIF_OP_BGEU: 3740 if (cc_c == 0) 3741 pc = DIF_INSTR_LABEL(instr); 3742 break; 3743 case DIF_OP_BL: 3744 if (cc_n ^ cc_v) 3745 pc = DIF_INSTR_LABEL(instr); 3746 break; 3747 case DIF_OP_BLU: 3748 if (cc_c) 3749 pc = DIF_INSTR_LABEL(instr); 3750 break; 3751 case DIF_OP_BLE: 3752 if (cc_z | (cc_n ^ cc_v)) 3753 pc = DIF_INSTR_LABEL(instr); 3754 break; 3755 case DIF_OP_BLEU: 3756 if (cc_c | cc_z) 3757 pc = DIF_INSTR_LABEL(instr); 3758 break; 3759 case DIF_OP_RLDSB: 3760 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 3761 *flags |= CPU_DTRACE_KPRIV; 3762 *illval = regs[r1]; 3763 break; 3764 } 3765 /*FALLTHROUGH*/ 3766 case DIF_OP_LDSB: 3767 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 3768 break; 3769 case DIF_OP_RLDSH: 3770 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 3771 *flags |= CPU_DTRACE_KPRIV; 3772 *illval = regs[r1]; 3773 break; 3774 } 3775 /*FALLTHROUGH*/ 3776 case DIF_OP_LDSH: 3777 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 3778 break; 3779 case DIF_OP_RLDSW: 3780 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 3781 *flags |= CPU_DTRACE_KPRIV; 3782 *illval = regs[r1]; 3783 break; 3784 } 3785 /*FALLTHROUGH*/ 3786 case DIF_OP_LDSW: 3787 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 3788 break; 3789 case DIF_OP_RLDUB: 3790 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 3791 *flags |= CPU_DTRACE_KPRIV; 3792 *illval = regs[r1]; 3793 break; 3794 } 3795 /*FALLTHROUGH*/ 3796 case DIF_OP_LDUB: 3797 regs[rd] = dtrace_load8(regs[r1]); 3798 break; 3799 case DIF_OP_RLDUH: 3800 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 3801 *flags |= CPU_DTRACE_KPRIV; 3802 *illval = regs[r1]; 3803 break; 3804 } 3805 /*FALLTHROUGH*/ 3806 case DIF_OP_LDUH: 3807 regs[rd] = dtrace_load16(regs[r1]); 3808 break; 3809 case DIF_OP_RLDUW: 3810 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 3811 *flags |= CPU_DTRACE_KPRIV; 3812 *illval = regs[r1]; 3813 break; 3814 } 3815 /*FALLTHROUGH*/ 3816 case DIF_OP_LDUW: 3817 regs[rd] = dtrace_load32(regs[r1]); 3818 break; 3819 case DIF_OP_RLDX: 3820 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 3821 *flags |= CPU_DTRACE_KPRIV; 3822 *illval = regs[r1]; 3823 break; 3824 } 3825 /*FALLTHROUGH*/ 3826 case DIF_OP_LDX: 3827 regs[rd] = dtrace_load64(regs[r1]); 3828 break; 3829 case DIF_OP_ULDSB: 3830 regs[rd] = (int8_t) 3831 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 3832 break; 3833 case DIF_OP_ULDSH: 3834 regs[rd] = (int16_t) 3835 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 3836 break; 3837 case DIF_OP_ULDSW: 3838 regs[rd] = (int32_t) 3839 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 3840 break; 3841 case DIF_OP_ULDUB: 3842 regs[rd] = 3843 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 3844 break; 3845 case DIF_OP_ULDUH: 3846 regs[rd] = 3847 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 3848 break; 3849 case DIF_OP_ULDUW: 3850 regs[rd] = 3851 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 3852 break; 3853 case DIF_OP_ULDX: 3854 regs[rd] = 3855 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 3856 break; 3857 case DIF_OP_RET: 3858 rval = regs[rd]; 3859 break; 3860 case DIF_OP_NOP: 3861 break; 3862 case DIF_OP_SETX: 3863 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 3864 break; 3865 case DIF_OP_SETS: 3866 regs[rd] = (uint64_t)(uintptr_t) 3867 (strtab + DIF_INSTR_STRING(instr)); 3868 break; 3869 case DIF_OP_SCMP: 3870 cc_r = dtrace_strncmp((char *)(uintptr_t)regs[r1], 3871 (char *)(uintptr_t)regs[r2], 3872 state->dts_options[DTRACEOPT_STRSIZE]); 3873 3874 cc_n = cc_r < 0; 3875 cc_z = cc_r == 0; 3876 cc_v = cc_c = 0; 3877 break; 3878 case DIF_OP_LDGA: 3879 regs[rd] = dtrace_dif_variable(mstate, state, 3880 r1, regs[r2]); 3881 break; 3882 case DIF_OP_LDGS: 3883 id = DIF_INSTR_VAR(instr); 3884 3885 if (id >= DIF_VAR_OTHER_UBASE) { 3886 uintptr_t a; 3887 3888 id -= DIF_VAR_OTHER_UBASE; 3889 svar = vstate->dtvs_globals[id]; 3890 ASSERT(svar != NULL); 3891 v = &svar->dtsv_var; 3892 3893 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 3894 regs[rd] = svar->dtsv_data; 3895 break; 3896 } 3897 3898 a = (uintptr_t)svar->dtsv_data; 3899 3900 if (*(uint8_t *)a == UINT8_MAX) { 3901 /* 3902 * If the 0th byte is set to UINT8_MAX 3903 * then this is to be treated as a 3904 * reference to a NULL variable. 3905 */ 3906 regs[rd] = NULL; 3907 } else { 3908 regs[rd] = a + sizeof (uint64_t); 3909 } 3910 3911 break; 3912 } 3913 3914 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 3915 break; 3916 3917 case DIF_OP_STGS: 3918 id = DIF_INSTR_VAR(instr); 3919 3920 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3921 id -= DIF_VAR_OTHER_UBASE; 3922 3923 svar = vstate->dtvs_globals[id]; 3924 ASSERT(svar != NULL); 3925 v = &svar->dtsv_var; 3926 3927 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3928 uintptr_t a = (uintptr_t)svar->dtsv_data; 3929 3930 ASSERT(a != NULL); 3931 ASSERT(svar->dtsv_size != 0); 3932 3933 if (regs[rd] == NULL) { 3934 *(uint8_t *)a = UINT8_MAX; 3935 break; 3936 } else { 3937 *(uint8_t *)a = 0; 3938 a += sizeof (uint64_t); 3939 } 3940 3941 dtrace_vcopy((void *)(uintptr_t)regs[rd], 3942 (void *)a, &v->dtdv_type); 3943 break; 3944 } 3945 3946 svar->dtsv_data = regs[rd]; 3947 break; 3948 3949 case DIF_OP_LDTA: 3950 /* 3951 * There are no DTrace built-in thread-local arrays at 3952 * present. This opcode is saved for future work. 3953 */ 3954 *flags |= CPU_DTRACE_ILLOP; 3955 regs[rd] = 0; 3956 break; 3957 3958 case DIF_OP_LDLS: 3959 id = DIF_INSTR_VAR(instr); 3960 3961 if (id < DIF_VAR_OTHER_UBASE) { 3962 /* 3963 * For now, this has no meaning. 3964 */ 3965 regs[rd] = 0; 3966 break; 3967 } 3968 3969 id -= DIF_VAR_OTHER_UBASE; 3970 3971 ASSERT(id < vstate->dtvs_nlocals); 3972 ASSERT(vstate->dtvs_locals != NULL); 3973 3974 svar = vstate->dtvs_locals[id]; 3975 ASSERT(svar != NULL); 3976 v = &svar->dtsv_var; 3977 3978 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3979 uintptr_t a = (uintptr_t)svar->dtsv_data; 3980 size_t sz = v->dtdv_type.dtdt_size; 3981 3982 sz += sizeof (uint64_t); 3983 ASSERT(svar->dtsv_size == NCPU * sz); 3984 a += CPU->cpu_id * sz; 3985 3986 if (*(uint8_t *)a == UINT8_MAX) { 3987 /* 3988 * If the 0th byte is set to UINT8_MAX 3989 * then this is to be treated as a 3990 * reference to a NULL variable. 3991 */ 3992 regs[rd] = NULL; 3993 } else { 3994 regs[rd] = a + sizeof (uint64_t); 3995 } 3996 3997 break; 3998 } 3999 4000 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 4001 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 4002 regs[rd] = tmp[CPU->cpu_id]; 4003 break; 4004 4005 case DIF_OP_STLS: 4006 id = DIF_INSTR_VAR(instr); 4007 4008 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4009 id -= DIF_VAR_OTHER_UBASE; 4010 ASSERT(id < vstate->dtvs_nlocals); 4011 4012 ASSERT(vstate->dtvs_locals != NULL); 4013 svar = vstate->dtvs_locals[id]; 4014 ASSERT(svar != NULL); 4015 v = &svar->dtsv_var; 4016 4017 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4018 uintptr_t a = (uintptr_t)svar->dtsv_data; 4019 size_t sz = v->dtdv_type.dtdt_size; 4020 4021 sz += sizeof (uint64_t); 4022 ASSERT(svar->dtsv_size == NCPU * sz); 4023 a += CPU->cpu_id * sz; 4024 4025 if (regs[rd] == NULL) { 4026 *(uint8_t *)a = UINT8_MAX; 4027 break; 4028 } else { 4029 *(uint8_t *)a = 0; 4030 a += sizeof (uint64_t); 4031 } 4032 4033 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4034 (void *)a, &v->dtdv_type); 4035 break; 4036 } 4037 4038 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 4039 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 4040 tmp[CPU->cpu_id] = regs[rd]; 4041 break; 4042 4043 case DIF_OP_LDTS: { 4044 dtrace_dynvar_t *dvar; 4045 dtrace_key_t *key; 4046 4047 id = DIF_INSTR_VAR(instr); 4048 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4049 id -= DIF_VAR_OTHER_UBASE; 4050 v = &vstate->dtvs_tlocals[id]; 4051 4052 key = &tupregs[DIF_DTR_NREGS]; 4053 key[0].dttk_value = (uint64_t)id; 4054 key[0].dttk_size = 0; 4055 DTRACE_TLS_THRKEY(key[1].dttk_value); 4056 key[1].dttk_size = 0; 4057 4058 dvar = dtrace_dynvar(dstate, 2, key, 4059 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC); 4060 4061 if (dvar == NULL) { 4062 regs[rd] = 0; 4063 break; 4064 } 4065 4066 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4067 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 4068 } else { 4069 regs[rd] = *((uint64_t *)dvar->dtdv_data); 4070 } 4071 4072 break; 4073 } 4074 4075 case DIF_OP_STTS: { 4076 dtrace_dynvar_t *dvar; 4077 dtrace_key_t *key; 4078 4079 id = DIF_INSTR_VAR(instr); 4080 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4081 id -= DIF_VAR_OTHER_UBASE; 4082 4083 key = &tupregs[DIF_DTR_NREGS]; 4084 key[0].dttk_value = (uint64_t)id; 4085 key[0].dttk_size = 0; 4086 DTRACE_TLS_THRKEY(key[1].dttk_value); 4087 key[1].dttk_size = 0; 4088 v = &vstate->dtvs_tlocals[id]; 4089 4090 dvar = dtrace_dynvar(dstate, 2, key, 4091 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4092 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4093 regs[rd] ? DTRACE_DYNVAR_ALLOC : 4094 DTRACE_DYNVAR_DEALLOC); 4095 4096 /* 4097 * Given that we're storing to thread-local data, 4098 * we need to flush our predicate cache. 4099 */ 4100 curthread->t_predcache = NULL; 4101 4102 if (dvar == NULL) 4103 break; 4104 4105 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4106 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4107 dvar->dtdv_data, &v->dtdv_type); 4108 } else { 4109 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 4110 } 4111 4112 break; 4113 } 4114 4115 case DIF_OP_SRA: 4116 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 4117 break; 4118 4119 case DIF_OP_CALL: 4120 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 4121 regs, tupregs, ttop, mstate, state); 4122 break; 4123 4124 case DIF_OP_PUSHTR: 4125 if (ttop == DIF_DTR_NREGS) { 4126 *flags |= CPU_DTRACE_TUPOFLOW; 4127 break; 4128 } 4129 4130 if (r1 == DIF_TYPE_STRING) { 4131 /* 4132 * If this is a string type and the size is 0, 4133 * we'll use the system-wide default string 4134 * size. Note that we are _not_ looking at 4135 * the value of the DTRACEOPT_STRSIZE option; 4136 * had this been set, we would expect to have 4137 * a non-zero size value in the "pushtr". 4138 */ 4139 tupregs[ttop].dttk_size = 4140 dtrace_strlen((char *)(uintptr_t)regs[rd], 4141 regs[r2] ? regs[r2] : 4142 dtrace_strsize_default) + 1; 4143 } else { 4144 tupregs[ttop].dttk_size = regs[r2]; 4145 } 4146 4147 tupregs[ttop++].dttk_value = regs[rd]; 4148 break; 4149 4150 case DIF_OP_PUSHTV: 4151 if (ttop == DIF_DTR_NREGS) { 4152 *flags |= CPU_DTRACE_TUPOFLOW; 4153 break; 4154 } 4155 4156 tupregs[ttop].dttk_value = regs[rd]; 4157 tupregs[ttop++].dttk_size = 0; 4158 break; 4159 4160 case DIF_OP_POPTS: 4161 if (ttop != 0) 4162 ttop--; 4163 break; 4164 4165 case DIF_OP_FLUSHTS: 4166 ttop = 0; 4167 break; 4168 4169 case DIF_OP_LDGAA: 4170 case DIF_OP_LDTAA: { 4171 dtrace_dynvar_t *dvar; 4172 dtrace_key_t *key = tupregs; 4173 uint_t nkeys = ttop; 4174 4175 id = DIF_INSTR_VAR(instr); 4176 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4177 id -= DIF_VAR_OTHER_UBASE; 4178 4179 key[nkeys].dttk_value = (uint64_t)id; 4180 key[nkeys++].dttk_size = 0; 4181 4182 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 4183 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 4184 key[nkeys++].dttk_size = 0; 4185 v = &vstate->dtvs_tlocals[id]; 4186 } else { 4187 v = &vstate->dtvs_globals[id]->dtsv_var; 4188 } 4189 4190 dvar = dtrace_dynvar(dstate, nkeys, key, 4191 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4192 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4193 DTRACE_DYNVAR_NOALLOC); 4194 4195 if (dvar == NULL) { 4196 regs[rd] = 0; 4197 break; 4198 } 4199 4200 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4201 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 4202 } else { 4203 regs[rd] = *((uint64_t *)dvar->dtdv_data); 4204 } 4205 4206 break; 4207 } 4208 4209 case DIF_OP_STGAA: 4210 case DIF_OP_STTAA: { 4211 dtrace_dynvar_t *dvar; 4212 dtrace_key_t *key = tupregs; 4213 uint_t nkeys = ttop; 4214 4215 id = DIF_INSTR_VAR(instr); 4216 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4217 id -= DIF_VAR_OTHER_UBASE; 4218 4219 key[nkeys].dttk_value = (uint64_t)id; 4220 key[nkeys++].dttk_size = 0; 4221 4222 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 4223 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 4224 key[nkeys++].dttk_size = 0; 4225 v = &vstate->dtvs_tlocals[id]; 4226 } else { 4227 v = &vstate->dtvs_globals[id]->dtsv_var; 4228 } 4229 4230 dvar = dtrace_dynvar(dstate, nkeys, key, 4231 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4232 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4233 regs[rd] ? DTRACE_DYNVAR_ALLOC : 4234 DTRACE_DYNVAR_DEALLOC); 4235 4236 if (dvar == NULL) 4237 break; 4238 4239 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4240 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4241 dvar->dtdv_data, &v->dtdv_type); 4242 } else { 4243 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 4244 } 4245 4246 break; 4247 } 4248 4249 case DIF_OP_ALLOCS: { 4250 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4251 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 4252 4253 if (mstate->dtms_scratch_ptr + size > 4254 mstate->dtms_scratch_base + 4255 mstate->dtms_scratch_size) { 4256 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4257 regs[rd] = NULL; 4258 } else { 4259 dtrace_bzero((void *) 4260 mstate->dtms_scratch_ptr, size); 4261 mstate->dtms_scratch_ptr += size; 4262 regs[rd] = ptr; 4263 } 4264 break; 4265 } 4266 4267 case DIF_OP_COPYS: 4268 if (!dtrace_canstore(regs[rd], regs[r2], 4269 mstate, vstate)) { 4270 *flags |= CPU_DTRACE_BADADDR; 4271 *illval = regs[rd]; 4272 break; 4273 } 4274 4275 dtrace_bcopy((void *)(uintptr_t)regs[r1], 4276 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 4277 break; 4278 4279 case DIF_OP_STB: 4280 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 4281 *flags |= CPU_DTRACE_BADADDR; 4282 *illval = regs[rd]; 4283 break; 4284 } 4285 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 4286 break; 4287 4288 case DIF_OP_STH: 4289 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 4290 *flags |= CPU_DTRACE_BADADDR; 4291 *illval = regs[rd]; 4292 break; 4293 } 4294 if (regs[rd] & 1) { 4295 *flags |= CPU_DTRACE_BADALIGN; 4296 *illval = regs[rd]; 4297 break; 4298 } 4299 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 4300 break; 4301 4302 case DIF_OP_STW: 4303 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 4304 *flags |= CPU_DTRACE_BADADDR; 4305 *illval = regs[rd]; 4306 break; 4307 } 4308 if (regs[rd] & 3) { 4309 *flags |= CPU_DTRACE_BADALIGN; 4310 *illval = regs[rd]; 4311 break; 4312 } 4313 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 4314 break; 4315 4316 case DIF_OP_STX: 4317 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 4318 *flags |= CPU_DTRACE_BADADDR; 4319 *illval = regs[rd]; 4320 break; 4321 } 4322 if (regs[rd] & 7) { 4323 *flags |= CPU_DTRACE_BADALIGN; 4324 *illval = regs[rd]; 4325 break; 4326 } 4327 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 4328 break; 4329 } 4330 } 4331 4332 if (!(*flags & CPU_DTRACE_FAULT)) 4333 return (rval); 4334 4335 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 4336 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 4337 4338 return (0); 4339 } 4340 4341 static void 4342 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 4343 { 4344 dtrace_probe_t *probe = ecb->dte_probe; 4345 dtrace_provider_t *prov = probe->dtpr_provider; 4346 char c[DTRACE_FULLNAMELEN + 80], *str; 4347 char *msg = "dtrace: breakpoint action at probe "; 4348 char *ecbmsg = " (ecb "; 4349 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 4350 uintptr_t val = (uintptr_t)ecb; 4351 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 4352 4353 if (dtrace_destructive_disallow) 4354 return; 4355 4356 /* 4357 * It's impossible to be taking action on the NULL probe. 4358 */ 4359 ASSERT(probe != NULL); 4360 4361 /* 4362 * This is a poor man's (destitute man's?) sprintf(): we want to 4363 * print the provider name, module name, function name and name of 4364 * the probe, along with the hex address of the ECB with the breakpoint 4365 * action -- all of which we must place in the character buffer by 4366 * hand. 4367 */ 4368 while (*msg != '\0') 4369 c[i++] = *msg++; 4370 4371 for (str = prov->dtpv_name; *str != '\0'; str++) 4372 c[i++] = *str; 4373 c[i++] = ':'; 4374 4375 for (str = probe->dtpr_mod; *str != '\0'; str++) 4376 c[i++] = *str; 4377 c[i++] = ':'; 4378 4379 for (str = probe->dtpr_func; *str != '\0'; str++) 4380 c[i++] = *str; 4381 c[i++] = ':'; 4382 4383 for (str = probe->dtpr_name; *str != '\0'; str++) 4384 c[i++] = *str; 4385 4386 while (*ecbmsg != '\0') 4387 c[i++] = *ecbmsg++; 4388 4389 while (shift >= 0) { 4390 mask = (uintptr_t)0xf << shift; 4391 4392 if (val >= ((uintptr_t)1 << shift)) 4393 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 4394 shift -= 4; 4395 } 4396 4397 c[i++] = ')'; 4398 c[i] = '\0'; 4399 4400 debug_enter(c); 4401 } 4402 4403 static void 4404 dtrace_action_panic(dtrace_ecb_t *ecb) 4405 { 4406 dtrace_probe_t *probe = ecb->dte_probe; 4407 4408 /* 4409 * It's impossible to be taking action on the NULL probe. 4410 */ 4411 ASSERT(probe != NULL); 4412 4413 if (dtrace_destructive_disallow) 4414 return; 4415 4416 if (dtrace_panicked != NULL) 4417 return; 4418 4419 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 4420 return; 4421 4422 /* 4423 * We won the right to panic. (We want to be sure that only one 4424 * thread calls panic() from dtrace_probe(), and that panic() is 4425 * called exactly once.) 4426 */ 4427 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 4428 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 4429 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 4430 } 4431 4432 static void 4433 dtrace_action_raise(uint64_t sig) 4434 { 4435 if (dtrace_destructive_disallow) 4436 return; 4437 4438 if (sig >= NSIG) { 4439 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4440 return; 4441 } 4442 4443 /* 4444 * raise() has a queue depth of 1 -- we ignore all subsequent 4445 * invocations of the raise() action. 4446 */ 4447 if (curthread->t_dtrace_sig == 0) 4448 curthread->t_dtrace_sig = (uint8_t)sig; 4449 4450 curthread->t_sig_check = 1; 4451 aston(curthread); 4452 } 4453 4454 static void 4455 dtrace_action_stop(void) 4456 { 4457 if (dtrace_destructive_disallow) 4458 return; 4459 4460 if (!curthread->t_dtrace_stop) { 4461 curthread->t_dtrace_stop = 1; 4462 curthread->t_sig_check = 1; 4463 aston(curthread); 4464 } 4465 } 4466 4467 static void 4468 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 4469 { 4470 hrtime_t now; 4471 volatile uint16_t *flags; 4472 cpu_t *cpu = CPU; 4473 4474 if (dtrace_destructive_disallow) 4475 return; 4476 4477 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 4478 4479 now = dtrace_gethrtime(); 4480 4481 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 4482 /* 4483 * We need to advance the mark to the current time. 4484 */ 4485 cpu->cpu_dtrace_chillmark = now; 4486 cpu->cpu_dtrace_chilled = 0; 4487 } 4488 4489 /* 4490 * Now check to see if the requested chill time would take us over 4491 * the maximum amount of time allowed in the chill interval. (Or 4492 * worse, if the calculation itself induces overflow.) 4493 */ 4494 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 4495 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 4496 *flags |= CPU_DTRACE_ILLOP; 4497 return; 4498 } 4499 4500 while (dtrace_gethrtime() - now < val) 4501 continue; 4502 4503 /* 4504 * Normally, we assure that the value of the variable "timestamp" does 4505 * not change within an ECB. The presence of chill() represents an 4506 * exception to this rule, however. 4507 */ 4508 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 4509 cpu->cpu_dtrace_chilled += val; 4510 } 4511 4512 static void 4513 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 4514 uint64_t *buf, uint64_t arg) 4515 { 4516 int nframes = DTRACE_USTACK_NFRAMES(arg); 4517 int strsize = DTRACE_USTACK_STRSIZE(arg); 4518 uint64_t *pcs = &buf[1], *fps; 4519 char *str = (char *)&pcs[nframes]; 4520 int size, offs = 0, i, j; 4521 uintptr_t old = mstate->dtms_scratch_ptr, saved; 4522 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 4523 char *sym; 4524 4525 /* 4526 * Should be taking a faster path if string space has not been 4527 * allocated. 4528 */ 4529 ASSERT(strsize != 0); 4530 4531 /* 4532 * We will first allocate some temporary space for the frame pointers. 4533 */ 4534 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4535 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 4536 (nframes * sizeof (uint64_t)); 4537 4538 if (mstate->dtms_scratch_ptr + size > 4539 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 4540 /* 4541 * Not enough room for our frame pointers -- need to indicate 4542 * that we ran out of scratch space. 4543 */ 4544 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4545 return; 4546 } 4547 4548 mstate->dtms_scratch_ptr += size; 4549 saved = mstate->dtms_scratch_ptr; 4550 4551 /* 4552 * Now get a stack with both program counters and frame pointers. 4553 */ 4554 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4555 dtrace_getufpstack(buf, fps, nframes + 1); 4556 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4557 4558 /* 4559 * If that faulted, we're cooked. 4560 */ 4561 if (*flags & CPU_DTRACE_FAULT) 4562 goto out; 4563 4564 /* 4565 * Now we want to walk up the stack, calling the USTACK helper. For 4566 * each iteration, we restore the scratch pointer. 4567 */ 4568 for (i = 0; i < nframes; i++) { 4569 mstate->dtms_scratch_ptr = saved; 4570 4571 if (offs >= strsize) 4572 break; 4573 4574 sym = (char *)(uintptr_t)dtrace_helper( 4575 DTRACE_HELPER_ACTION_USTACK, 4576 mstate, state, pcs[i], fps[i]); 4577 4578 /* 4579 * If we faulted while running the helper, we're going to 4580 * clear the fault and null out the corresponding string. 4581 */ 4582 if (*flags & CPU_DTRACE_FAULT) { 4583 *flags &= ~CPU_DTRACE_FAULT; 4584 str[offs++] = '\0'; 4585 continue; 4586 } 4587 4588 if (sym == NULL) { 4589 str[offs++] = '\0'; 4590 continue; 4591 } 4592 4593 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4594 4595 /* 4596 * Now copy in the string that the helper returned to us. 4597 */ 4598 for (j = 0; offs + j < strsize; j++) { 4599 if ((str[offs + j] = sym[j]) == '\0') 4600 break; 4601 } 4602 4603 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4604 4605 offs += j + 1; 4606 } 4607 4608 if (offs >= strsize) { 4609 /* 4610 * If we didn't have room for all of the strings, we don't 4611 * abort processing -- this needn't be a fatal error -- but we 4612 * still want to increment a counter (dts_stkstroverflows) to 4613 * allow this condition to be warned about. (If this is from 4614 * a jstack() action, it is easily tuned via jstackstrsize.) 4615 */ 4616 dtrace_error(&state->dts_stkstroverflows); 4617 } 4618 4619 while (offs < strsize) 4620 str[offs++] = '\0'; 4621 4622 out: 4623 mstate->dtms_scratch_ptr = old; 4624 } 4625 4626 /* 4627 * If you're looking for the epicenter of DTrace, you just found it. This 4628 * is the function called by the provider to fire a probe -- from which all 4629 * subsequent probe-context DTrace activity emanates. 4630 */ 4631 void 4632 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 4633 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 4634 { 4635 processorid_t cpuid; 4636 dtrace_icookie_t cookie; 4637 dtrace_probe_t *probe; 4638 dtrace_mstate_t mstate; 4639 dtrace_ecb_t *ecb; 4640 dtrace_action_t *act; 4641 intptr_t offs; 4642 size_t size; 4643 int vtime, onintr; 4644 volatile uint16_t *flags; 4645 hrtime_t now; 4646 4647 /* 4648 * Kick out immediately if this CPU is still being born (in which case 4649 * curthread will be set to -1) 4650 */ 4651 if ((uintptr_t)curthread & 1) 4652 return; 4653 4654 cookie = dtrace_interrupt_disable(); 4655 probe = dtrace_probes[id - 1]; 4656 cpuid = CPU->cpu_id; 4657 onintr = CPU_ON_INTR(CPU); 4658 4659 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 4660 probe->dtpr_predcache == curthread->t_predcache) { 4661 /* 4662 * We have hit in the predicate cache; we know that 4663 * this predicate would evaluate to be false. 4664 */ 4665 dtrace_interrupt_enable(cookie); 4666 return; 4667 } 4668 4669 if (panic_quiesce) { 4670 /* 4671 * We don't trace anything if we're panicking. 4672 */ 4673 dtrace_interrupt_enable(cookie); 4674 return; 4675 } 4676 4677 now = dtrace_gethrtime(); 4678 vtime = dtrace_vtime_references != 0; 4679 4680 if (vtime && curthread->t_dtrace_start) 4681 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 4682 4683 mstate.dtms_probe = probe; 4684 mstate.dtms_arg[0] = arg0; 4685 mstate.dtms_arg[1] = arg1; 4686 mstate.dtms_arg[2] = arg2; 4687 mstate.dtms_arg[3] = arg3; 4688 mstate.dtms_arg[4] = arg4; 4689 4690 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 4691 4692 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 4693 dtrace_predicate_t *pred = ecb->dte_predicate; 4694 dtrace_state_t *state = ecb->dte_state; 4695 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 4696 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 4697 dtrace_vstate_t *vstate = &state->dts_vstate; 4698 dtrace_provider_t *prov = probe->dtpr_provider; 4699 int committed = 0; 4700 caddr_t tomax; 4701 4702 /* 4703 * A little subtlety with the following (seemingly innocuous) 4704 * declaration of the automatic 'val': by looking at the 4705 * code, you might think that it could be declared in the 4706 * action processing loop, below. (That is, it's only used in 4707 * the action processing loop.) However, it must be declared 4708 * out of that scope because in the case of DIF expression 4709 * arguments to aggregating actions, one iteration of the 4710 * action loop will use the last iteration's value. 4711 */ 4712 #ifdef lint 4713 uint64_t val = 0; 4714 #else 4715 uint64_t val; 4716 #endif 4717 4718 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 4719 *flags &= ~CPU_DTRACE_ERROR; 4720 4721 if (prov == dtrace_provider) { 4722 /* 4723 * If dtrace itself is the provider of this probe, 4724 * we're only going to continue processing the ECB if 4725 * arg0 (the dtrace_state_t) is equal to the ECB's 4726 * creating state. (This prevents disjoint consumers 4727 * from seeing one another's metaprobes.) 4728 */ 4729 if (arg0 != (uint64_t)(uintptr_t)state) 4730 continue; 4731 } 4732 4733 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 4734 /* 4735 * We're not currently active. If our provider isn't 4736 * the dtrace pseudo provider, we're not interested. 4737 */ 4738 if (prov != dtrace_provider) 4739 continue; 4740 4741 /* 4742 * Now we must further check if we are in the BEGIN 4743 * probe. If we are, we will only continue processing 4744 * if we're still in WARMUP -- if one BEGIN enabling 4745 * has invoked the exit() action, we don't want to 4746 * evaluate subsequent BEGIN enablings. 4747 */ 4748 if (probe->dtpr_id == dtrace_probeid_begin && 4749 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 4750 ASSERT(state->dts_activity == 4751 DTRACE_ACTIVITY_DRAINING); 4752 continue; 4753 } 4754 } 4755 4756 if (ecb->dte_cond) { 4757 /* 4758 * If the dte_cond bits indicate that this 4759 * consumer is only allowed to see user-mode firings 4760 * of this probe, call the provider's dtps_usermode() 4761 * entry point to check that the probe was fired 4762 * while in a user context. Skip this ECB if that's 4763 * not the case. 4764 */ 4765 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 4766 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 4767 probe->dtpr_id, probe->dtpr_arg) == 0) 4768 continue; 4769 4770 /* 4771 * This is more subtle than it looks. We have to be 4772 * absolutely certain that CRED() isn't going to 4773 * change out from under us so it's only legit to 4774 * examine that structure if we're in constrained 4775 * situations. Currently, the only times we'll this 4776 * check is if a non-super-user has enabled the 4777 * profile or syscall providers -- providers that 4778 * allow visibility of all processes. For the 4779 * profile case, the check above will ensure that 4780 * we're examining a user context. 4781 */ 4782 if (ecb->dte_cond & DTRACE_COND_OWNER) { 4783 cred_t *cr; 4784 cred_t *s_cr = 4785 ecb->dte_state->dts_cred.dcr_cred; 4786 proc_t *proc; 4787 4788 ASSERT(s_cr != NULL); 4789 4790 if ((cr = CRED()) == NULL || 4791 s_cr->cr_uid != cr->cr_uid || 4792 s_cr->cr_uid != cr->cr_ruid || 4793 s_cr->cr_uid != cr->cr_suid || 4794 s_cr->cr_gid != cr->cr_gid || 4795 s_cr->cr_gid != cr->cr_rgid || 4796 s_cr->cr_gid != cr->cr_sgid || 4797 (proc = ttoproc(curthread)) == NULL || 4798 (proc->p_flag & SNOCD)) 4799 continue; 4800 } 4801 4802 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 4803 cred_t *cr; 4804 cred_t *s_cr = 4805 ecb->dte_state->dts_cred.dcr_cred; 4806 4807 ASSERT(s_cr != NULL); 4808 4809 if ((cr = CRED()) == NULL || 4810 s_cr->cr_zone->zone_id != 4811 cr->cr_zone->zone_id) 4812 continue; 4813 } 4814 } 4815 4816 if (now - state->dts_alive > dtrace_deadman_timeout) { 4817 /* 4818 * We seem to be dead. Unless we (a) have kernel 4819 * destructive permissions (b) have expicitly enabled 4820 * destructive actions and (c) destructive actions have 4821 * not been disabled, we're going to transition into 4822 * the KILLED state, from which no further processing 4823 * on this state will be performed. 4824 */ 4825 if (!dtrace_priv_kernel_destructive(state) || 4826 !state->dts_cred.dcr_destructive || 4827 dtrace_destructive_disallow) { 4828 void *activity = &state->dts_activity; 4829 dtrace_activity_t current; 4830 4831 do { 4832 current = state->dts_activity; 4833 } while (dtrace_cas32(activity, current, 4834 DTRACE_ACTIVITY_KILLED) != current); 4835 4836 continue; 4837 } 4838 } 4839 4840 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 4841 ecb->dte_alignment, state, &mstate)) < 0) 4842 continue; 4843 4844 tomax = buf->dtb_tomax; 4845 ASSERT(tomax != NULL); 4846 4847 if (ecb->dte_size != 0) 4848 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 4849 4850 mstate.dtms_epid = ecb->dte_epid; 4851 mstate.dtms_present |= DTRACE_MSTATE_EPID; 4852 4853 if (pred != NULL) { 4854 dtrace_difo_t *dp = pred->dtp_difo; 4855 int rval; 4856 4857 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 4858 4859 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 4860 dtrace_cacheid_t cid = probe->dtpr_predcache; 4861 4862 if (cid != DTRACE_CACHEIDNONE && !onintr) { 4863 /* 4864 * Update the predicate cache... 4865 */ 4866 ASSERT(cid == pred->dtp_cacheid); 4867 curthread->t_predcache = cid; 4868 } 4869 4870 continue; 4871 } 4872 } 4873 4874 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 4875 act != NULL; act = act->dta_next) { 4876 size_t valoffs; 4877 dtrace_difo_t *dp; 4878 dtrace_recdesc_t *rec = &act->dta_rec; 4879 4880 size = rec->dtrd_size; 4881 valoffs = offs + rec->dtrd_offset; 4882 4883 if (DTRACEACT_ISAGG(act->dta_kind)) { 4884 uint64_t v = 0xbad; 4885 dtrace_aggregation_t *agg; 4886 4887 agg = (dtrace_aggregation_t *)act; 4888 4889 if ((dp = act->dta_difo) != NULL) 4890 v = dtrace_dif_emulate(dp, 4891 &mstate, vstate, state); 4892 4893 if (*flags & CPU_DTRACE_ERROR) 4894 continue; 4895 4896 /* 4897 * Note that we always pass the expression 4898 * value from the previous iteration of the 4899 * action loop. This value will only be used 4900 * if there is an expression argument to the 4901 * aggregating action, denoted by the 4902 * dtag_hasarg field. 4903 */ 4904 dtrace_aggregate(agg, buf, 4905 offs, aggbuf, v, val); 4906 continue; 4907 } 4908 4909 switch (act->dta_kind) { 4910 case DTRACEACT_STOP: 4911 if (dtrace_priv_proc_destructive(state)) 4912 dtrace_action_stop(); 4913 continue; 4914 4915 case DTRACEACT_BREAKPOINT: 4916 if (dtrace_priv_kernel_destructive(state)) 4917 dtrace_action_breakpoint(ecb); 4918 continue; 4919 4920 case DTRACEACT_PANIC: 4921 if (dtrace_priv_kernel_destructive(state)) 4922 dtrace_action_panic(ecb); 4923 continue; 4924 4925 case DTRACEACT_STACK: 4926 if (!dtrace_priv_kernel(state)) 4927 continue; 4928 4929 dtrace_getpcstack((pc_t *)(tomax + valoffs), 4930 size / sizeof (pc_t), probe->dtpr_aframes, 4931 DTRACE_ANCHORED(probe) ? NULL : 4932 (uint32_t *)arg0); 4933 4934 continue; 4935 4936 case DTRACEACT_JSTACK: 4937 case DTRACEACT_USTACK: 4938 if (!dtrace_priv_proc(state)) 4939 continue; 4940 4941 /* 4942 * See comment in DIF_VAR_PID. 4943 */ 4944 if (DTRACE_ANCHORED(mstate.dtms_probe) && 4945 CPU_ON_INTR(CPU)) { 4946 int depth = DTRACE_USTACK_NFRAMES( 4947 rec->dtrd_arg) + 1; 4948 4949 dtrace_bzero((void *)(tomax + valoffs), 4950 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 4951 + depth * sizeof (uint64_t)); 4952 4953 continue; 4954 } 4955 4956 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 4957 curproc->p_dtrace_helpers != NULL) { 4958 /* 4959 * This is the slow path -- we have 4960 * allocated string space, and we're 4961 * getting the stack of a process that 4962 * has helpers. Call into a separate 4963 * routine to perform this processing. 4964 */ 4965 dtrace_action_ustack(&mstate, state, 4966 (uint64_t *)(tomax + valoffs), 4967 rec->dtrd_arg); 4968 continue; 4969 } 4970 4971 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4972 dtrace_getupcstack((uint64_t *) 4973 (tomax + valoffs), 4974 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 4975 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4976 continue; 4977 4978 default: 4979 break; 4980 } 4981 4982 dp = act->dta_difo; 4983 ASSERT(dp != NULL); 4984 4985 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 4986 4987 if (*flags & CPU_DTRACE_ERROR) 4988 continue; 4989 4990 switch (act->dta_kind) { 4991 case DTRACEACT_SPECULATE: 4992 ASSERT(buf == &state->dts_buffer[cpuid]); 4993 buf = dtrace_speculation_buffer(state, 4994 cpuid, val); 4995 4996 if (buf == NULL) { 4997 *flags |= CPU_DTRACE_DROP; 4998 continue; 4999 } 5000 5001 offs = dtrace_buffer_reserve(buf, 5002 ecb->dte_needed, ecb->dte_alignment, 5003 state, NULL); 5004 5005 if (offs < 0) { 5006 *flags |= CPU_DTRACE_DROP; 5007 continue; 5008 } 5009 5010 tomax = buf->dtb_tomax; 5011 ASSERT(tomax != NULL); 5012 5013 if (ecb->dte_size != 0) 5014 DTRACE_STORE(uint32_t, tomax, offs, 5015 ecb->dte_epid); 5016 continue; 5017 5018 case DTRACEACT_CHILL: 5019 if (dtrace_priv_kernel_destructive(state)) 5020 dtrace_action_chill(&mstate, val); 5021 continue; 5022 5023 case DTRACEACT_RAISE: 5024 if (dtrace_priv_proc_destructive(state)) 5025 dtrace_action_raise(val); 5026 continue; 5027 5028 case DTRACEACT_COMMIT: 5029 ASSERT(!committed); 5030 5031 /* 5032 * We need to commit our buffer state. 5033 */ 5034 if (ecb->dte_size) 5035 buf->dtb_offset = offs + ecb->dte_size; 5036 buf = &state->dts_buffer[cpuid]; 5037 dtrace_speculation_commit(state, cpuid, val); 5038 committed = 1; 5039 continue; 5040 5041 case DTRACEACT_DISCARD: 5042 dtrace_speculation_discard(state, cpuid, val); 5043 continue; 5044 5045 case DTRACEACT_DIFEXPR: 5046 case DTRACEACT_LIBACT: 5047 case DTRACEACT_PRINTF: 5048 case DTRACEACT_PRINTA: 5049 case DTRACEACT_SYSTEM: 5050 case DTRACEACT_FREOPEN: 5051 break; 5052 5053 case DTRACEACT_SYM: 5054 case DTRACEACT_MOD: 5055 if (!dtrace_priv_kernel(state)) 5056 continue; 5057 break; 5058 5059 case DTRACEACT_USYM: 5060 case DTRACEACT_UMOD: 5061 case DTRACEACT_UADDR: { 5062 struct pid *pid = curthread->t_procp->p_pidp; 5063 5064 if (!dtrace_priv_proc(state)) 5065 continue; 5066 5067 DTRACE_STORE(uint64_t, tomax, 5068 valoffs, (uint64_t)pid->pid_id); 5069 DTRACE_STORE(uint64_t, tomax, 5070 valoffs + sizeof (uint64_t), val); 5071 5072 continue; 5073 } 5074 5075 case DTRACEACT_EXIT: { 5076 /* 5077 * For the exit action, we are going to attempt 5078 * to atomically set our activity to be 5079 * draining. If this fails (either because 5080 * another CPU has beat us to the exit action, 5081 * or because our current activity is something 5082 * other than ACTIVE or WARMUP), we will 5083 * continue. This assures that the exit action 5084 * can be successfully recorded at most once 5085 * when we're in the ACTIVE state. If we're 5086 * encountering the exit() action while in 5087 * COOLDOWN, however, we want to honor the new 5088 * status code. (We know that we're the only 5089 * thread in COOLDOWN, so there is no race.) 5090 */ 5091 void *activity = &state->dts_activity; 5092 dtrace_activity_t current = state->dts_activity; 5093 5094 if (current == DTRACE_ACTIVITY_COOLDOWN) 5095 break; 5096 5097 if (current != DTRACE_ACTIVITY_WARMUP) 5098 current = DTRACE_ACTIVITY_ACTIVE; 5099 5100 if (dtrace_cas32(activity, current, 5101 DTRACE_ACTIVITY_DRAINING) != current) { 5102 *flags |= CPU_DTRACE_DROP; 5103 continue; 5104 } 5105 5106 break; 5107 } 5108 5109 default: 5110 ASSERT(0); 5111 } 5112 5113 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 5114 uintptr_t end = valoffs + size; 5115 5116 /* 5117 * If this is a string, we're going to only 5118 * load until we find the zero byte -- after 5119 * which we'll store zero bytes. 5120 */ 5121 if (dp->dtdo_rtype.dtdt_kind == 5122 DIF_TYPE_STRING) { 5123 char c = '\0' + 1; 5124 int intuple = act->dta_intuple; 5125 size_t s; 5126 5127 for (s = 0; s < size; s++) { 5128 if (c != '\0') 5129 c = dtrace_load8(val++); 5130 5131 DTRACE_STORE(uint8_t, tomax, 5132 valoffs++, c); 5133 5134 if (c == '\0' && intuple) 5135 break; 5136 } 5137 5138 continue; 5139 } 5140 5141 while (valoffs < end) { 5142 DTRACE_STORE(uint8_t, tomax, valoffs++, 5143 dtrace_load8(val++)); 5144 } 5145 5146 continue; 5147 } 5148 5149 switch (size) { 5150 case 0: 5151 break; 5152 5153 case sizeof (uint8_t): 5154 DTRACE_STORE(uint8_t, tomax, valoffs, val); 5155 break; 5156 case sizeof (uint16_t): 5157 DTRACE_STORE(uint16_t, tomax, valoffs, val); 5158 break; 5159 case sizeof (uint32_t): 5160 DTRACE_STORE(uint32_t, tomax, valoffs, val); 5161 break; 5162 case sizeof (uint64_t): 5163 DTRACE_STORE(uint64_t, tomax, valoffs, val); 5164 break; 5165 default: 5166 /* 5167 * Any other size should have been returned by 5168 * reference, not by value. 5169 */ 5170 ASSERT(0); 5171 break; 5172 } 5173 } 5174 5175 if (*flags & CPU_DTRACE_DROP) 5176 continue; 5177 5178 if (*flags & CPU_DTRACE_FAULT) { 5179 int ndx; 5180 dtrace_action_t *err; 5181 5182 buf->dtb_errors++; 5183 5184 if (probe->dtpr_id == dtrace_probeid_error) { 5185 /* 5186 * There's nothing we can do -- we had an 5187 * error on the error probe. We bump an 5188 * error counter to at least indicate that 5189 * this condition happened. 5190 */ 5191 dtrace_error(&state->dts_dblerrors); 5192 continue; 5193 } 5194 5195 if (vtime) { 5196 /* 5197 * Before recursing on dtrace_probe(), we 5198 * need to explicitly clear out our start 5199 * time to prevent it from being accumulated 5200 * into t_dtrace_vtime. 5201 */ 5202 curthread->t_dtrace_start = 0; 5203 } 5204 5205 /* 5206 * Iterate over the actions to figure out which action 5207 * we were processing when we experienced the error. 5208 * Note that act points _past_ the faulting action; if 5209 * act is ecb->dte_action, the fault was in the 5210 * predicate, if it's ecb->dte_action->dta_next it's 5211 * in action #1, and so on. 5212 */ 5213 for (err = ecb->dte_action, ndx = 0; 5214 err != act; err = err->dta_next, ndx++) 5215 continue; 5216 5217 dtrace_probe_error(state, ecb->dte_epid, ndx, 5218 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 5219 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 5220 cpu_core[cpuid].cpuc_dtrace_illval); 5221 5222 continue; 5223 } 5224 5225 if (!committed) 5226 buf->dtb_offset = offs + ecb->dte_size; 5227 } 5228 5229 if (vtime) 5230 curthread->t_dtrace_start = dtrace_gethrtime(); 5231 5232 dtrace_interrupt_enable(cookie); 5233 } 5234 5235 /* 5236 * DTrace Probe Hashing Functions 5237 * 5238 * The functions in this section (and indeed, the functions in remaining 5239 * sections) are not _called_ from probe context. (Any exceptions to this are 5240 * marked with a "Note:".) Rather, they are called from elsewhere in the 5241 * DTrace framework to look-up probes in, add probes to and remove probes from 5242 * the DTrace probe hashes. (Each probe is hashed by each element of the 5243 * probe tuple -- allowing for fast lookups, regardless of what was 5244 * specified.) 5245 */ 5246 static uint_t 5247 dtrace_hash_str(char *p) 5248 { 5249 unsigned int g; 5250 uint_t hval = 0; 5251 5252 while (*p) { 5253 hval = (hval << 4) + *p++; 5254 if ((g = (hval & 0xf0000000)) != 0) 5255 hval ^= g >> 24; 5256 hval &= ~g; 5257 } 5258 return (hval); 5259 } 5260 5261 static dtrace_hash_t * 5262 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 5263 { 5264 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 5265 5266 hash->dth_stroffs = stroffs; 5267 hash->dth_nextoffs = nextoffs; 5268 hash->dth_prevoffs = prevoffs; 5269 5270 hash->dth_size = 1; 5271 hash->dth_mask = hash->dth_size - 1; 5272 5273 hash->dth_tab = kmem_zalloc(hash->dth_size * 5274 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 5275 5276 return (hash); 5277 } 5278 5279 static void 5280 dtrace_hash_destroy(dtrace_hash_t *hash) 5281 { 5282 #ifdef DEBUG 5283 int i; 5284 5285 for (i = 0; i < hash->dth_size; i++) 5286 ASSERT(hash->dth_tab[i] == NULL); 5287 #endif 5288 5289 kmem_free(hash->dth_tab, 5290 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 5291 kmem_free(hash, sizeof (dtrace_hash_t)); 5292 } 5293 5294 static void 5295 dtrace_hash_resize(dtrace_hash_t *hash) 5296 { 5297 int size = hash->dth_size, i, ndx; 5298 int new_size = hash->dth_size << 1; 5299 int new_mask = new_size - 1; 5300 dtrace_hashbucket_t **new_tab, *bucket, *next; 5301 5302 ASSERT((new_size & new_mask) == 0); 5303 5304 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 5305 5306 for (i = 0; i < size; i++) { 5307 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 5308 dtrace_probe_t *probe = bucket->dthb_chain; 5309 5310 ASSERT(probe != NULL); 5311 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 5312 5313 next = bucket->dthb_next; 5314 bucket->dthb_next = new_tab[ndx]; 5315 new_tab[ndx] = bucket; 5316 } 5317 } 5318 5319 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 5320 hash->dth_tab = new_tab; 5321 hash->dth_size = new_size; 5322 hash->dth_mask = new_mask; 5323 } 5324 5325 static void 5326 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 5327 { 5328 int hashval = DTRACE_HASHSTR(hash, new); 5329 int ndx = hashval & hash->dth_mask; 5330 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5331 dtrace_probe_t **nextp, **prevp; 5332 5333 for (; bucket != NULL; bucket = bucket->dthb_next) { 5334 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 5335 goto add; 5336 } 5337 5338 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 5339 dtrace_hash_resize(hash); 5340 dtrace_hash_add(hash, new); 5341 return; 5342 } 5343 5344 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 5345 bucket->dthb_next = hash->dth_tab[ndx]; 5346 hash->dth_tab[ndx] = bucket; 5347 hash->dth_nbuckets++; 5348 5349 add: 5350 nextp = DTRACE_HASHNEXT(hash, new); 5351 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 5352 *nextp = bucket->dthb_chain; 5353 5354 if (bucket->dthb_chain != NULL) { 5355 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 5356 ASSERT(*prevp == NULL); 5357 *prevp = new; 5358 } 5359 5360 bucket->dthb_chain = new; 5361 bucket->dthb_len++; 5362 } 5363 5364 static dtrace_probe_t * 5365 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 5366 { 5367 int hashval = DTRACE_HASHSTR(hash, template); 5368 int ndx = hashval & hash->dth_mask; 5369 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5370 5371 for (; bucket != NULL; bucket = bucket->dthb_next) { 5372 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 5373 return (bucket->dthb_chain); 5374 } 5375 5376 return (NULL); 5377 } 5378 5379 static int 5380 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 5381 { 5382 int hashval = DTRACE_HASHSTR(hash, template); 5383 int ndx = hashval & hash->dth_mask; 5384 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5385 5386 for (; bucket != NULL; bucket = bucket->dthb_next) { 5387 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 5388 return (bucket->dthb_len); 5389 } 5390 5391 return (NULL); 5392 } 5393 5394 static void 5395 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 5396 { 5397 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 5398 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5399 5400 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 5401 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 5402 5403 /* 5404 * Find the bucket that we're removing this probe from. 5405 */ 5406 for (; bucket != NULL; bucket = bucket->dthb_next) { 5407 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 5408 break; 5409 } 5410 5411 ASSERT(bucket != NULL); 5412 5413 if (*prevp == NULL) { 5414 if (*nextp == NULL) { 5415 /* 5416 * The removed probe was the only probe on this 5417 * bucket; we need to remove the bucket. 5418 */ 5419 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 5420 5421 ASSERT(bucket->dthb_chain == probe); 5422 ASSERT(b != NULL); 5423 5424 if (b == bucket) { 5425 hash->dth_tab[ndx] = bucket->dthb_next; 5426 } else { 5427 while (b->dthb_next != bucket) 5428 b = b->dthb_next; 5429 b->dthb_next = bucket->dthb_next; 5430 } 5431 5432 ASSERT(hash->dth_nbuckets > 0); 5433 hash->dth_nbuckets--; 5434 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 5435 return; 5436 } 5437 5438 bucket->dthb_chain = *nextp; 5439 } else { 5440 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 5441 } 5442 5443 if (*nextp != NULL) 5444 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 5445 } 5446 5447 /* 5448 * DTrace Utility Functions 5449 * 5450 * These are random utility functions that are _not_ called from probe context. 5451 */ 5452 static int 5453 dtrace_badattr(const dtrace_attribute_t *a) 5454 { 5455 return (a->dtat_name > DTRACE_STABILITY_MAX || 5456 a->dtat_data > DTRACE_STABILITY_MAX || 5457 a->dtat_class > DTRACE_CLASS_MAX); 5458 } 5459 5460 /* 5461 * Return a duplicate copy of a string. If the specified string is NULL, 5462 * this function returns a zero-length string. 5463 */ 5464 static char * 5465 dtrace_strdup(const char *str) 5466 { 5467 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 5468 5469 if (str != NULL) 5470 (void) strcpy(new, str); 5471 5472 return (new); 5473 } 5474 5475 #define DTRACE_ISALPHA(c) \ 5476 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 5477 5478 static int 5479 dtrace_badname(const char *s) 5480 { 5481 char c; 5482 5483 if (s == NULL || (c = *s++) == '\0') 5484 return (0); 5485 5486 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 5487 return (1); 5488 5489 while ((c = *s++) != '\0') { 5490 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 5491 c != '-' && c != '_' && c != '.' && c != '`') 5492 return (1); 5493 } 5494 5495 return (0); 5496 } 5497 5498 static void 5499 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 5500 { 5501 uint32_t priv; 5502 5503 *uidp = crgetuid(cr); 5504 *zoneidp = crgetzoneid(cr); 5505 if (PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 5506 priv = DTRACE_PRIV_ALL; 5507 } else { 5508 priv = 0; 5509 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 5510 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 5511 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 5512 priv |= DTRACE_PRIV_USER; 5513 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 5514 priv |= DTRACE_PRIV_PROC; 5515 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 5516 priv |= DTRACE_PRIV_OWNER; 5517 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 5518 priv |= DTRACE_PRIV_ZONEOWNER; 5519 } 5520 5521 *privp = priv; 5522 } 5523 5524 #ifdef DTRACE_ERRDEBUG 5525 static void 5526 dtrace_errdebug(const char *str) 5527 { 5528 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; 5529 int occupied = 0; 5530 5531 mutex_enter(&dtrace_errlock); 5532 dtrace_errlast = str; 5533 dtrace_errthread = curthread; 5534 5535 while (occupied++ < DTRACE_ERRHASHSZ) { 5536 if (dtrace_errhash[hval].dter_msg == str) { 5537 dtrace_errhash[hval].dter_count++; 5538 goto out; 5539 } 5540 5541 if (dtrace_errhash[hval].dter_msg != NULL) { 5542 hval = (hval + 1) % DTRACE_ERRHASHSZ; 5543 continue; 5544 } 5545 5546 dtrace_errhash[hval].dter_msg = str; 5547 dtrace_errhash[hval].dter_count = 1; 5548 goto out; 5549 } 5550 5551 panic("dtrace: undersized error hash"); 5552 out: 5553 mutex_exit(&dtrace_errlock); 5554 } 5555 #endif 5556 5557 /* 5558 * DTrace Matching Functions 5559 * 5560 * These functions are used to match groups of probes, given some elements of 5561 * a probe tuple, or some globbed expressions for elements of a probe tuple. 5562 */ 5563 static int 5564 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 5565 zoneid_t zoneid) 5566 { 5567 if (priv != DTRACE_PRIV_ALL) { 5568 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 5569 uint32_t match = priv & ppriv; 5570 5571 /* 5572 * No PRIV_DTRACE_* privileges... 5573 */ 5574 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 5575 DTRACE_PRIV_KERNEL)) == 0) 5576 return (0); 5577 5578 /* 5579 * No matching bits, but there were bits to match... 5580 */ 5581 if (match == 0 && ppriv != 0) 5582 return (0); 5583 5584 /* 5585 * Need to have permissions to the process, but don't... 5586 */ 5587 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 5588 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 5589 return (0); 5590 } 5591 5592 /* 5593 * Need to be in the same zone unless we possess the 5594 * privilege to examine all zones. 5595 */ 5596 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 5597 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 5598 return (0); 5599 } 5600 } 5601 5602 return (1); 5603 } 5604 5605 /* 5606 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 5607 * consists of input pattern strings and an ops-vector to evaluate them. 5608 * This function returns >0 for match, 0 for no match, and <0 for error. 5609 */ 5610 static int 5611 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 5612 uint32_t priv, uid_t uid, zoneid_t zoneid) 5613 { 5614 dtrace_provider_t *pvp = prp->dtpr_provider; 5615 int rv; 5616 5617 if (pvp->dtpv_defunct) 5618 return (0); 5619 5620 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 5621 return (rv); 5622 5623 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 5624 return (rv); 5625 5626 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 5627 return (rv); 5628 5629 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 5630 return (rv); 5631 5632 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 5633 return (0); 5634 5635 return (rv); 5636 } 5637 5638 /* 5639 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 5640 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 5641 * libc's version, the kernel version only applies to 8-bit ASCII strings. 5642 * In addition, all of the recursion cases except for '*' matching have been 5643 * unwound. For '*', we still implement recursive evaluation, but a depth 5644 * counter is maintained and matching is aborted if we recurse too deep. 5645 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 5646 */ 5647 static int 5648 dtrace_match_glob(const char *s, const char *p, int depth) 5649 { 5650 const char *olds; 5651 char s1, c; 5652 int gs; 5653 5654 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 5655 return (-1); 5656 5657 if (s == NULL) 5658 s = ""; /* treat NULL as empty string */ 5659 5660 top: 5661 olds = s; 5662 s1 = *s++; 5663 5664 if (p == NULL) 5665 return (0); 5666 5667 if ((c = *p++) == '\0') 5668 return (s1 == '\0'); 5669 5670 switch (c) { 5671 case '[': { 5672 int ok = 0, notflag = 0; 5673 char lc = '\0'; 5674 5675 if (s1 == '\0') 5676 return (0); 5677 5678 if (*p == '!') { 5679 notflag = 1; 5680 p++; 5681 } 5682 5683 if ((c = *p++) == '\0') 5684 return (0); 5685 5686 do { 5687 if (c == '-' && lc != '\0' && *p != ']') { 5688 if ((c = *p++) == '\0') 5689 return (0); 5690 if (c == '\\' && (c = *p++) == '\0') 5691 return (0); 5692 5693 if (notflag) { 5694 if (s1 < lc || s1 > c) 5695 ok++; 5696 else 5697 return (0); 5698 } else if (lc <= s1 && s1 <= c) 5699 ok++; 5700 5701 } else if (c == '\\' && (c = *p++) == '\0') 5702 return (0); 5703 5704 lc = c; /* save left-hand 'c' for next iteration */ 5705 5706 if (notflag) { 5707 if (s1 != c) 5708 ok++; 5709 else 5710 return (0); 5711 } else if (s1 == c) 5712 ok++; 5713 5714 if ((c = *p++) == '\0') 5715 return (0); 5716 5717 } while (c != ']'); 5718 5719 if (ok) 5720 goto top; 5721 5722 return (0); 5723 } 5724 5725 case '\\': 5726 if ((c = *p++) == '\0') 5727 return (0); 5728 /*FALLTHRU*/ 5729 5730 default: 5731 if (c != s1) 5732 return (0); 5733 /*FALLTHRU*/ 5734 5735 case '?': 5736 if (s1 != '\0') 5737 goto top; 5738 return (0); 5739 5740 case '*': 5741 while (*p == '*') 5742 p++; /* consecutive *'s are identical to a single one */ 5743 5744 if (*p == '\0') 5745 return (1); 5746 5747 for (s = olds; *s != '\0'; s++) { 5748 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 5749 return (gs); 5750 } 5751 5752 return (0); 5753 } 5754 } 5755 5756 /*ARGSUSED*/ 5757 static int 5758 dtrace_match_string(const char *s, const char *p, int depth) 5759 { 5760 return (s != NULL && strcmp(s, p) == 0); 5761 } 5762 5763 /*ARGSUSED*/ 5764 static int 5765 dtrace_match_nul(const char *s, const char *p, int depth) 5766 { 5767 return (1); /* always match the empty pattern */ 5768 } 5769 5770 /*ARGSUSED*/ 5771 static int 5772 dtrace_match_nonzero(const char *s, const char *p, int depth) 5773 { 5774 return (s != NULL && s[0] != '\0'); 5775 } 5776 5777 static int 5778 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 5779 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 5780 { 5781 dtrace_probe_t template, *probe; 5782 dtrace_hash_t *hash = NULL; 5783 int len, best = INT_MAX, nmatched = 0; 5784 dtrace_id_t i; 5785 5786 ASSERT(MUTEX_HELD(&dtrace_lock)); 5787 5788 /* 5789 * If the probe ID is specified in the key, just lookup by ID and 5790 * invoke the match callback once if a matching probe is found. 5791 */ 5792 if (pkp->dtpk_id != DTRACE_IDNONE) { 5793 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 5794 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 5795 (void) (*matched)(probe, arg); 5796 nmatched++; 5797 } 5798 return (nmatched); 5799 } 5800 5801 template.dtpr_mod = (char *)pkp->dtpk_mod; 5802 template.dtpr_func = (char *)pkp->dtpk_func; 5803 template.dtpr_name = (char *)pkp->dtpk_name; 5804 5805 /* 5806 * We want to find the most distinct of the module name, function 5807 * name, and name. So for each one that is not a glob pattern or 5808 * empty string, we perform a lookup in the corresponding hash and 5809 * use the hash table with the fewest collisions to do our search. 5810 */ 5811 if (pkp->dtpk_mmatch == &dtrace_match_string && 5812 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 5813 best = len; 5814 hash = dtrace_bymod; 5815 } 5816 5817 if (pkp->dtpk_fmatch == &dtrace_match_string && 5818 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 5819 best = len; 5820 hash = dtrace_byfunc; 5821 } 5822 5823 if (pkp->dtpk_nmatch == &dtrace_match_string && 5824 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 5825 best = len; 5826 hash = dtrace_byname; 5827 } 5828 5829 /* 5830 * If we did not select a hash table, iterate over every probe and 5831 * invoke our callback for each one that matches our input probe key. 5832 */ 5833 if (hash == NULL) { 5834 for (i = 0; i < dtrace_nprobes; i++) { 5835 if ((probe = dtrace_probes[i]) == NULL || 5836 dtrace_match_probe(probe, pkp, priv, uid, 5837 zoneid) <= 0) 5838 continue; 5839 5840 nmatched++; 5841 5842 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 5843 break; 5844 } 5845 5846 return (nmatched); 5847 } 5848 5849 /* 5850 * If we selected a hash table, iterate over each probe of the same key 5851 * name and invoke the callback for every probe that matches the other 5852 * attributes of our input probe key. 5853 */ 5854 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 5855 probe = *(DTRACE_HASHNEXT(hash, probe))) { 5856 5857 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 5858 continue; 5859 5860 nmatched++; 5861 5862 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 5863 break; 5864 } 5865 5866 return (nmatched); 5867 } 5868 5869 /* 5870 * Return the function pointer dtrace_probecmp() should use to compare the 5871 * specified pattern with a string. For NULL or empty patterns, we select 5872 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 5873 * For non-empty non-glob strings, we use dtrace_match_string(). 5874 */ 5875 static dtrace_probekey_f * 5876 dtrace_probekey_func(const char *p) 5877 { 5878 char c; 5879 5880 if (p == NULL || *p == '\0') 5881 return (&dtrace_match_nul); 5882 5883 while ((c = *p++) != '\0') { 5884 if (c == '[' || c == '?' || c == '*' || c == '\\') 5885 return (&dtrace_match_glob); 5886 } 5887 5888 return (&dtrace_match_string); 5889 } 5890 5891 /* 5892 * Build a probe comparison key for use with dtrace_match_probe() from the 5893 * given probe description. By convention, a null key only matches anchored 5894 * probes: if each field is the empty string, reset dtpk_fmatch to 5895 * dtrace_match_nonzero(). 5896 */ 5897 static void 5898 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 5899 { 5900 pkp->dtpk_prov = pdp->dtpd_provider; 5901 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 5902 5903 pkp->dtpk_mod = pdp->dtpd_mod; 5904 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 5905 5906 pkp->dtpk_func = pdp->dtpd_func; 5907 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 5908 5909 pkp->dtpk_name = pdp->dtpd_name; 5910 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 5911 5912 pkp->dtpk_id = pdp->dtpd_id; 5913 5914 if (pkp->dtpk_id == DTRACE_IDNONE && 5915 pkp->dtpk_pmatch == &dtrace_match_nul && 5916 pkp->dtpk_mmatch == &dtrace_match_nul && 5917 pkp->dtpk_fmatch == &dtrace_match_nul && 5918 pkp->dtpk_nmatch == &dtrace_match_nul) 5919 pkp->dtpk_fmatch = &dtrace_match_nonzero; 5920 } 5921 5922 /* 5923 * DTrace Provider-to-Framework API Functions 5924 * 5925 * These functions implement much of the Provider-to-Framework API, as 5926 * described in <sys/dtrace.h>. The parts of the API not in this section are 5927 * the functions in the API for probe management (found below), and 5928 * dtrace_probe() itself (found above). 5929 */ 5930 5931 /* 5932 * Register the calling provider with the DTrace framework. This should 5933 * generally be called by DTrace providers in their attach(9E) entry point. 5934 */ 5935 int 5936 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 5937 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 5938 { 5939 dtrace_provider_t *provider; 5940 5941 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 5942 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5943 "arguments", name ? name : "<NULL>"); 5944 return (EINVAL); 5945 } 5946 5947 if (name[0] == '\0' || dtrace_badname(name)) { 5948 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5949 "provider name", name); 5950 return (EINVAL); 5951 } 5952 5953 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 5954 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 5955 pops->dtps_destroy == NULL || 5956 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 5957 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5958 "provider ops", name); 5959 return (EINVAL); 5960 } 5961 5962 if (dtrace_badattr(&pap->dtpa_provider) || 5963 dtrace_badattr(&pap->dtpa_mod) || 5964 dtrace_badattr(&pap->dtpa_func) || 5965 dtrace_badattr(&pap->dtpa_name) || 5966 dtrace_badattr(&pap->dtpa_args)) { 5967 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5968 "provider attributes", name); 5969 return (EINVAL); 5970 } 5971 5972 if (priv & ~DTRACE_PRIV_ALL) { 5973 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5974 "privilege attributes", name); 5975 return (EINVAL); 5976 } 5977 5978 if ((priv & DTRACE_PRIV_KERNEL) && 5979 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 5980 pops->dtps_usermode == NULL) { 5981 cmn_err(CE_WARN, "failed to register provider '%s': need " 5982 "dtps_usermode() op for given privilege attributes", name); 5983 return (EINVAL); 5984 } 5985 5986 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 5987 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 5988 (void) strcpy(provider->dtpv_name, name); 5989 5990 provider->dtpv_attr = *pap; 5991 provider->dtpv_priv.dtpp_flags = priv; 5992 if (cr != NULL) { 5993 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 5994 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 5995 } 5996 provider->dtpv_pops = *pops; 5997 5998 if (pops->dtps_provide == NULL) { 5999 ASSERT(pops->dtps_provide_module != NULL); 6000 provider->dtpv_pops.dtps_provide = 6001 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 6002 } 6003 6004 if (pops->dtps_provide_module == NULL) { 6005 ASSERT(pops->dtps_provide != NULL); 6006 provider->dtpv_pops.dtps_provide_module = 6007 (void (*)(void *, struct modctl *))dtrace_nullop; 6008 } 6009 6010 if (pops->dtps_suspend == NULL) { 6011 ASSERT(pops->dtps_resume == NULL); 6012 provider->dtpv_pops.dtps_suspend = 6013 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 6014 provider->dtpv_pops.dtps_resume = 6015 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 6016 } 6017 6018 provider->dtpv_arg = arg; 6019 *idp = (dtrace_provider_id_t)provider; 6020 6021 if (pops == &dtrace_provider_ops) { 6022 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6023 ASSERT(MUTEX_HELD(&dtrace_lock)); 6024 ASSERT(dtrace_anon.dta_enabling == NULL); 6025 6026 /* 6027 * We make sure that the DTrace provider is at the head of 6028 * the provider chain. 6029 */ 6030 provider->dtpv_next = dtrace_provider; 6031 dtrace_provider = provider; 6032 return (0); 6033 } 6034 6035 mutex_enter(&dtrace_provider_lock); 6036 mutex_enter(&dtrace_lock); 6037 6038 /* 6039 * If there is at least one provider registered, we'll add this 6040 * provider after the first provider. 6041 */ 6042 if (dtrace_provider != NULL) { 6043 provider->dtpv_next = dtrace_provider->dtpv_next; 6044 dtrace_provider->dtpv_next = provider; 6045 } else { 6046 dtrace_provider = provider; 6047 } 6048 6049 if (dtrace_retained != NULL) { 6050 dtrace_enabling_provide(provider); 6051 6052 /* 6053 * Now we need to call dtrace_enabling_matchall() -- which 6054 * will acquire cpu_lock and dtrace_lock. We therefore need 6055 * to drop all of our locks before calling into it... 6056 */ 6057 mutex_exit(&dtrace_lock); 6058 mutex_exit(&dtrace_provider_lock); 6059 dtrace_enabling_matchall(); 6060 6061 return (0); 6062 } 6063 6064 mutex_exit(&dtrace_lock); 6065 mutex_exit(&dtrace_provider_lock); 6066 6067 return (0); 6068 } 6069 6070 /* 6071 * Unregister the specified provider from the DTrace framework. This should 6072 * generally be called by DTrace providers in their detach(9E) entry point. 6073 */ 6074 int 6075 dtrace_unregister(dtrace_provider_id_t id) 6076 { 6077 dtrace_provider_t *old = (dtrace_provider_t *)id; 6078 dtrace_provider_t *prev = NULL; 6079 int i, self = 0; 6080 dtrace_probe_t *probe, *first = NULL; 6081 6082 if (old->dtpv_pops.dtps_enable == 6083 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 6084 /* 6085 * If DTrace itself is the provider, we're called with locks 6086 * already held. 6087 */ 6088 ASSERT(old == dtrace_provider); 6089 ASSERT(dtrace_devi != NULL); 6090 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6091 ASSERT(MUTEX_HELD(&dtrace_lock)); 6092 self = 1; 6093 6094 if (dtrace_provider->dtpv_next != NULL) { 6095 /* 6096 * There's another provider here; return failure. 6097 */ 6098 return (EBUSY); 6099 } 6100 } else { 6101 mutex_enter(&dtrace_provider_lock); 6102 mutex_enter(&mod_lock); 6103 mutex_enter(&dtrace_lock); 6104 } 6105 6106 /* 6107 * If anyone has /dev/dtrace open, or if there are anonymous enabled 6108 * probes, we refuse to let providers slither away, unless this 6109 * provider has already been explicitly invalidated. 6110 */ 6111 if (!old->dtpv_defunct && 6112 (dtrace_opens || (dtrace_anon.dta_state != NULL && 6113 dtrace_anon.dta_state->dts_necbs > 0))) { 6114 if (!self) { 6115 mutex_exit(&dtrace_lock); 6116 mutex_exit(&mod_lock); 6117 mutex_exit(&dtrace_provider_lock); 6118 } 6119 return (EBUSY); 6120 } 6121 6122 /* 6123 * Attempt to destroy the probes associated with this provider. 6124 */ 6125 for (i = 0; i < dtrace_nprobes; i++) { 6126 if ((probe = dtrace_probes[i]) == NULL) 6127 continue; 6128 6129 if (probe->dtpr_provider != old) 6130 continue; 6131 6132 if (probe->dtpr_ecb == NULL) 6133 continue; 6134 6135 /* 6136 * We have at least one ECB; we can't remove this provider. 6137 */ 6138 if (!self) { 6139 mutex_exit(&dtrace_lock); 6140 mutex_exit(&mod_lock); 6141 mutex_exit(&dtrace_provider_lock); 6142 } 6143 return (EBUSY); 6144 } 6145 6146 /* 6147 * All of the probes for this provider are disabled; we can safely 6148 * remove all of them from their hash chains and from the probe array. 6149 */ 6150 for (i = 0; i < dtrace_nprobes; i++) { 6151 if ((probe = dtrace_probes[i]) == NULL) 6152 continue; 6153 6154 if (probe->dtpr_provider != old) 6155 continue; 6156 6157 dtrace_probes[i] = NULL; 6158 6159 dtrace_hash_remove(dtrace_bymod, probe); 6160 dtrace_hash_remove(dtrace_byfunc, probe); 6161 dtrace_hash_remove(dtrace_byname, probe); 6162 6163 if (first == NULL) { 6164 first = probe; 6165 probe->dtpr_nextmod = NULL; 6166 } else { 6167 probe->dtpr_nextmod = first; 6168 first = probe; 6169 } 6170 } 6171 6172 /* 6173 * The provider's probes have been removed from the hash chains and 6174 * from the probe array. Now issue a dtrace_sync() to be sure that 6175 * everyone has cleared out from any probe array processing. 6176 */ 6177 dtrace_sync(); 6178 6179 for (probe = first; probe != NULL; probe = first) { 6180 first = probe->dtpr_nextmod; 6181 6182 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 6183 probe->dtpr_arg); 6184 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 6185 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 6186 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 6187 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 6188 kmem_free(probe, sizeof (dtrace_probe_t)); 6189 } 6190 6191 if ((prev = dtrace_provider) == old) { 6192 ASSERT(self || dtrace_devi == NULL); 6193 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 6194 dtrace_provider = old->dtpv_next; 6195 } else { 6196 while (prev != NULL && prev->dtpv_next != old) 6197 prev = prev->dtpv_next; 6198 6199 if (prev == NULL) { 6200 panic("attempt to unregister non-existent " 6201 "dtrace provider %p\n", (void *)id); 6202 } 6203 6204 prev->dtpv_next = old->dtpv_next; 6205 } 6206 6207 if (!self) { 6208 mutex_exit(&dtrace_lock); 6209 mutex_exit(&mod_lock); 6210 mutex_exit(&dtrace_provider_lock); 6211 } 6212 6213 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 6214 kmem_free(old, sizeof (dtrace_provider_t)); 6215 6216 return (0); 6217 } 6218 6219 /* 6220 * Invalidate the specified provider. All subsequent probe lookups for the 6221 * specified provider will fail, but its probes will not be removed. 6222 */ 6223 void 6224 dtrace_invalidate(dtrace_provider_id_t id) 6225 { 6226 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 6227 6228 ASSERT(pvp->dtpv_pops.dtps_enable != 6229 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 6230 6231 mutex_enter(&dtrace_provider_lock); 6232 mutex_enter(&dtrace_lock); 6233 6234 pvp->dtpv_defunct = 1; 6235 6236 mutex_exit(&dtrace_lock); 6237 mutex_exit(&dtrace_provider_lock); 6238 } 6239 6240 /* 6241 * Indicate whether or not DTrace has attached. 6242 */ 6243 int 6244 dtrace_attached(void) 6245 { 6246 /* 6247 * dtrace_provider will be non-NULL iff the DTrace driver has 6248 * attached. (It's non-NULL because DTrace is always itself a 6249 * provider.) 6250 */ 6251 return (dtrace_provider != NULL); 6252 } 6253 6254 /* 6255 * Remove all the unenabled probes for the given provider. This function is 6256 * not unlike dtrace_unregister(), except that it doesn't remove the provider 6257 * -- just as many of its associated probes as it can. 6258 */ 6259 int 6260 dtrace_condense(dtrace_provider_id_t id) 6261 { 6262 dtrace_provider_t *prov = (dtrace_provider_t *)id; 6263 int i; 6264 dtrace_probe_t *probe; 6265 6266 /* 6267 * Make sure this isn't the dtrace provider itself. 6268 */ 6269 ASSERT(prov->dtpv_pops.dtps_enable != 6270 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 6271 6272 mutex_enter(&dtrace_provider_lock); 6273 mutex_enter(&dtrace_lock); 6274 6275 /* 6276 * Attempt to destroy the probes associated with this provider. 6277 */ 6278 for (i = 0; i < dtrace_nprobes; i++) { 6279 if ((probe = dtrace_probes[i]) == NULL) 6280 continue; 6281 6282 if (probe->dtpr_provider != prov) 6283 continue; 6284 6285 if (probe->dtpr_ecb != NULL) 6286 continue; 6287 6288 dtrace_probes[i] = NULL; 6289 6290 dtrace_hash_remove(dtrace_bymod, probe); 6291 dtrace_hash_remove(dtrace_byfunc, probe); 6292 dtrace_hash_remove(dtrace_byname, probe); 6293 6294 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 6295 probe->dtpr_arg); 6296 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 6297 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 6298 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 6299 kmem_free(probe, sizeof (dtrace_probe_t)); 6300 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 6301 } 6302 6303 mutex_exit(&dtrace_lock); 6304 mutex_exit(&dtrace_provider_lock); 6305 6306 return (0); 6307 } 6308 6309 /* 6310 * DTrace Probe Management Functions 6311 * 6312 * The functions in this section perform the DTrace probe management, 6313 * including functions to create probes, look-up probes, and call into the 6314 * providers to request that probes be provided. Some of these functions are 6315 * in the Provider-to-Framework API; these functions can be identified by the 6316 * fact that they are not declared "static". 6317 */ 6318 6319 /* 6320 * Create a probe with the specified module name, function name, and name. 6321 */ 6322 dtrace_id_t 6323 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 6324 const char *func, const char *name, int aframes, void *arg) 6325 { 6326 dtrace_probe_t *probe, **probes; 6327 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 6328 dtrace_id_t id; 6329 6330 if (provider == dtrace_provider) { 6331 ASSERT(MUTEX_HELD(&dtrace_lock)); 6332 } else { 6333 mutex_enter(&dtrace_lock); 6334 } 6335 6336 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 6337 VM_BESTFIT | VM_SLEEP); 6338 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 6339 6340 probe->dtpr_id = id; 6341 probe->dtpr_gen = dtrace_probegen++; 6342 probe->dtpr_mod = dtrace_strdup(mod); 6343 probe->dtpr_func = dtrace_strdup(func); 6344 probe->dtpr_name = dtrace_strdup(name); 6345 probe->dtpr_arg = arg; 6346 probe->dtpr_aframes = aframes; 6347 probe->dtpr_provider = provider; 6348 6349 dtrace_hash_add(dtrace_bymod, probe); 6350 dtrace_hash_add(dtrace_byfunc, probe); 6351 dtrace_hash_add(dtrace_byname, probe); 6352 6353 if (id - 1 >= dtrace_nprobes) { 6354 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 6355 size_t nsize = osize << 1; 6356 6357 if (nsize == 0) { 6358 ASSERT(osize == 0); 6359 ASSERT(dtrace_probes == NULL); 6360 nsize = sizeof (dtrace_probe_t *); 6361 } 6362 6363 probes = kmem_zalloc(nsize, KM_SLEEP); 6364 6365 if (dtrace_probes == NULL) { 6366 ASSERT(osize == 0); 6367 dtrace_probes = probes; 6368 dtrace_nprobes = 1; 6369 } else { 6370 dtrace_probe_t **oprobes = dtrace_probes; 6371 6372 bcopy(oprobes, probes, osize); 6373 dtrace_membar_producer(); 6374 dtrace_probes = probes; 6375 6376 dtrace_sync(); 6377 6378 /* 6379 * All CPUs are now seeing the new probes array; we can 6380 * safely free the old array. 6381 */ 6382 kmem_free(oprobes, osize); 6383 dtrace_nprobes <<= 1; 6384 } 6385 6386 ASSERT(id - 1 < dtrace_nprobes); 6387 } 6388 6389 ASSERT(dtrace_probes[id - 1] == NULL); 6390 dtrace_probes[id - 1] = probe; 6391 6392 if (provider != dtrace_provider) 6393 mutex_exit(&dtrace_lock); 6394 6395 return (id); 6396 } 6397 6398 static dtrace_probe_t * 6399 dtrace_probe_lookup_id(dtrace_id_t id) 6400 { 6401 ASSERT(MUTEX_HELD(&dtrace_lock)); 6402 6403 if (id == 0 || id > dtrace_nprobes) 6404 return (NULL); 6405 6406 return (dtrace_probes[id - 1]); 6407 } 6408 6409 static int 6410 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 6411 { 6412 *((dtrace_id_t *)arg) = probe->dtpr_id; 6413 6414 return (DTRACE_MATCH_DONE); 6415 } 6416 6417 /* 6418 * Look up a probe based on provider and one or more of module name, function 6419 * name and probe name. 6420 */ 6421 dtrace_id_t 6422 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, 6423 const char *func, const char *name) 6424 { 6425 dtrace_probekey_t pkey; 6426 dtrace_id_t id; 6427 int match; 6428 6429 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 6430 pkey.dtpk_pmatch = &dtrace_match_string; 6431 pkey.dtpk_mod = mod; 6432 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 6433 pkey.dtpk_func = func; 6434 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 6435 pkey.dtpk_name = name; 6436 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 6437 pkey.dtpk_id = DTRACE_IDNONE; 6438 6439 mutex_enter(&dtrace_lock); 6440 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 6441 dtrace_probe_lookup_match, &id); 6442 mutex_exit(&dtrace_lock); 6443 6444 ASSERT(match == 1 || match == 0); 6445 return (match ? id : 0); 6446 } 6447 6448 /* 6449 * Returns the probe argument associated with the specified probe. 6450 */ 6451 void * 6452 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 6453 { 6454 dtrace_probe_t *probe; 6455 void *rval = NULL; 6456 6457 mutex_enter(&dtrace_lock); 6458 6459 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 6460 probe->dtpr_provider == (dtrace_provider_t *)id) 6461 rval = probe->dtpr_arg; 6462 6463 mutex_exit(&dtrace_lock); 6464 6465 return (rval); 6466 } 6467 6468 /* 6469 * Copy a probe into a probe description. 6470 */ 6471 static void 6472 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 6473 { 6474 bzero(pdp, sizeof (dtrace_probedesc_t)); 6475 pdp->dtpd_id = prp->dtpr_id; 6476 6477 (void) strncpy(pdp->dtpd_provider, 6478 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 6479 6480 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 6481 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 6482 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 6483 } 6484 6485 /* 6486 * Called to indicate that a probe -- or probes -- should be provided by a 6487 * specfied provider. If the specified description is NULL, the provider will 6488 * be told to provide all of its probes. (This is done whenever a new 6489 * consumer comes along, or whenever a retained enabling is to be matched.) If 6490 * the specified description is non-NULL, the provider is given the 6491 * opportunity to dynamically provide the specified probe, allowing providers 6492 * to support the creation of probes on-the-fly. (So-called _autocreated_ 6493 * probes.) If the provider is NULL, the operations will be applied to all 6494 * providers; if the provider is non-NULL the operations will only be applied 6495 * to the specified provider. The dtrace_provider_lock must be held, and the 6496 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 6497 * will need to grab the dtrace_lock when it reenters the framework through 6498 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 6499 */ 6500 static void 6501 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 6502 { 6503 struct modctl *ctl; 6504 int all = 0; 6505 6506 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6507 6508 if (prv == NULL) { 6509 all = 1; 6510 prv = dtrace_provider; 6511 } 6512 6513 do { 6514 /* 6515 * First, call the blanket provide operation. 6516 */ 6517 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 6518 6519 /* 6520 * Now call the per-module provide operation. We will grab 6521 * mod_lock to prevent the list from being modified. Note 6522 * that this also prevents the mod_busy bits from changing. 6523 * (mod_busy can only be changed with mod_lock held.) 6524 */ 6525 mutex_enter(&mod_lock); 6526 6527 ctl = &modules; 6528 do { 6529 if (ctl->mod_busy || ctl->mod_mp == NULL) 6530 continue; 6531 6532 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 6533 6534 } while ((ctl = ctl->mod_next) != &modules); 6535 6536 mutex_exit(&mod_lock); 6537 } while (all && (prv = prv->dtpv_next) != NULL); 6538 } 6539 6540 /* 6541 * Iterate over each probe, and call the Framework-to-Provider API function 6542 * denoted by offs. 6543 */ 6544 static void 6545 dtrace_probe_foreach(uintptr_t offs) 6546 { 6547 dtrace_provider_t *prov; 6548 void (*func)(void *, dtrace_id_t, void *); 6549 dtrace_probe_t *probe; 6550 dtrace_icookie_t cookie; 6551 int i; 6552 6553 /* 6554 * We disable interrupts to walk through the probe array. This is 6555 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 6556 * won't see stale data. 6557 */ 6558 cookie = dtrace_interrupt_disable(); 6559 6560 for (i = 0; i < dtrace_nprobes; i++) { 6561 if ((probe = dtrace_probes[i]) == NULL) 6562 continue; 6563 6564 if (probe->dtpr_ecb == NULL) { 6565 /* 6566 * This probe isn't enabled -- don't call the function. 6567 */ 6568 continue; 6569 } 6570 6571 prov = probe->dtpr_provider; 6572 func = *((void(**)(void *, dtrace_id_t, void *)) 6573 ((uintptr_t)&prov->dtpv_pops + offs)); 6574 6575 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 6576 } 6577 6578 dtrace_interrupt_enable(cookie); 6579 } 6580 6581 static int 6582 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 6583 { 6584 dtrace_probekey_t pkey; 6585 uint32_t priv; 6586 uid_t uid; 6587 zoneid_t zoneid; 6588 6589 ASSERT(MUTEX_HELD(&dtrace_lock)); 6590 dtrace_ecb_create_cache = NULL; 6591 6592 if (desc == NULL) { 6593 /* 6594 * If we're passed a NULL description, we're being asked to 6595 * create an ECB with a NULL probe. 6596 */ 6597 (void) dtrace_ecb_create_enable(NULL, enab); 6598 return (0); 6599 } 6600 6601 dtrace_probekey(desc, &pkey); 6602 dtrace_cred2priv(CRED(), &priv, &uid, &zoneid); 6603 6604 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 6605 enab)); 6606 } 6607 6608 /* 6609 * DTrace Helper Provider Functions 6610 */ 6611 static void 6612 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 6613 { 6614 attr->dtat_name = DOF_ATTR_NAME(dofattr); 6615 attr->dtat_data = DOF_ATTR_DATA(dofattr); 6616 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 6617 } 6618 6619 static void 6620 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 6621 const dof_provider_t *dofprov, char *strtab) 6622 { 6623 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 6624 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 6625 dofprov->dofpv_provattr); 6626 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 6627 dofprov->dofpv_modattr); 6628 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 6629 dofprov->dofpv_funcattr); 6630 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 6631 dofprov->dofpv_nameattr); 6632 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 6633 dofprov->dofpv_argsattr); 6634 } 6635 6636 static void 6637 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 6638 { 6639 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6640 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6641 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec; 6642 dof_provider_t *provider; 6643 dof_probe_t *probe; 6644 uint32_t *off; 6645 uint8_t *arg; 6646 char *strtab; 6647 uint_t i, nprobes; 6648 dtrace_helper_provdesc_t dhpv; 6649 dtrace_helper_probedesc_t dhpb; 6650 dtrace_meta_t *meta = dtrace_meta_pid; 6651 dtrace_mops_t *mops = &meta->dtm_mops; 6652 void *parg; 6653 6654 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 6655 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6656 provider->dofpv_strtab * dof->dofh_secsize); 6657 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6658 provider->dofpv_probes * dof->dofh_secsize); 6659 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6660 provider->dofpv_prargs * dof->dofh_secsize); 6661 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6662 provider->dofpv_proffs * dof->dofh_secsize); 6663 6664 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 6665 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 6666 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 6667 6668 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 6669 6670 /* 6671 * Create the provider. 6672 */ 6673 dtrace_dofprov2hprov(&dhpv, provider, strtab); 6674 6675 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 6676 return; 6677 6678 meta->dtm_count++; 6679 6680 /* 6681 * Create the probes. 6682 */ 6683 for (i = 0; i < nprobes; i++) { 6684 probe = (dof_probe_t *)(uintptr_t)(daddr + 6685 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 6686 6687 dhpb.dthpb_mod = dhp->dofhp_mod; 6688 dhpb.dthpb_func = strtab + probe->dofpr_func; 6689 dhpb.dthpb_name = strtab + probe->dofpr_name; 6690 dhpb.dthpb_base = probe->dofpr_addr; 6691 dhpb.dthpb_offs = off + probe->dofpr_offidx; 6692 dhpb.dthpb_noffs = probe->dofpr_noffs; 6693 dhpb.dthpb_args = arg + probe->dofpr_argidx; 6694 dhpb.dthpb_nargc = probe->dofpr_nargc; 6695 dhpb.dthpb_xargc = probe->dofpr_xargc; 6696 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 6697 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 6698 6699 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 6700 } 6701 } 6702 6703 static void 6704 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 6705 { 6706 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6707 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6708 int i; 6709 6710 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 6711 6712 for (i = 0; i < dof->dofh_secnum; i++) { 6713 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 6714 dof->dofh_secoff + i * dof->dofh_secsize); 6715 6716 if (sec->dofs_type != DOF_SECT_PROVIDER) 6717 continue; 6718 6719 dtrace_helper_provide_one(dhp, sec, pid); 6720 } 6721 6722 /* 6723 * We may have just created probes, so we must now rematch against 6724 * any retained enablings. Note that this call will acquire both 6725 * cpu_lock and dtrace_lock; the fact that we are holding 6726 * dtrace_meta_lock now is what defines the ordering with respect to 6727 * these three locks. 6728 */ 6729 dtrace_enabling_matchall(); 6730 } 6731 6732 static void 6733 dtrace_helper_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 6734 { 6735 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6736 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6737 dof_sec_t *str_sec; 6738 dof_provider_t *provider; 6739 char *strtab; 6740 dtrace_helper_provdesc_t dhpv; 6741 dtrace_meta_t *meta = dtrace_meta_pid; 6742 dtrace_mops_t *mops = &meta->dtm_mops; 6743 6744 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 6745 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6746 provider->dofpv_strtab * dof->dofh_secsize); 6747 6748 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 6749 6750 /* 6751 * Create the provider. 6752 */ 6753 dtrace_dofprov2hprov(&dhpv, provider, strtab); 6754 6755 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 6756 6757 meta->dtm_count--; 6758 } 6759 6760 static void 6761 dtrace_helper_remove(dof_helper_t *dhp, pid_t pid) 6762 { 6763 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6764 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6765 int i; 6766 6767 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 6768 6769 for (i = 0; i < dof->dofh_secnum; i++) { 6770 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 6771 dof->dofh_secoff + i * dof->dofh_secsize); 6772 6773 if (sec->dofs_type != DOF_SECT_PROVIDER) 6774 continue; 6775 6776 dtrace_helper_remove_one(dhp, sec, pid); 6777 } 6778 } 6779 6780 /* 6781 * DTrace Meta Provider-to-Framework API Functions 6782 * 6783 * These functions implement the Meta Provider-to-Framework API, as described 6784 * in <sys/dtrace.h>. 6785 */ 6786 int 6787 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 6788 dtrace_meta_provider_id_t *idp) 6789 { 6790 dtrace_meta_t *meta; 6791 dtrace_helpers_t *help, *next; 6792 int i; 6793 6794 *idp = DTRACE_METAPROVNONE; 6795 6796 /* 6797 * We strictly don't need the name, but we hold onto it for 6798 * debuggability. All hail error queues! 6799 */ 6800 if (name == NULL) { 6801 cmn_err(CE_WARN, "failed to register meta-provider: " 6802 "invalid name"); 6803 return (EINVAL); 6804 } 6805 6806 if (mops == NULL || 6807 mops->dtms_create_probe == NULL || 6808 mops->dtms_provide_pid == NULL || 6809 mops->dtms_remove_pid == NULL) { 6810 cmn_err(CE_WARN, "failed to register meta-register %s: " 6811 "invalid ops", name); 6812 return (EINVAL); 6813 } 6814 6815 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 6816 meta->dtm_mops = *mops; 6817 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 6818 (void) strcpy(meta->dtm_name, name); 6819 meta->dtm_arg = arg; 6820 6821 mutex_enter(&dtrace_meta_lock); 6822 mutex_enter(&dtrace_lock); 6823 6824 if (dtrace_meta_pid != NULL) { 6825 mutex_exit(&dtrace_lock); 6826 mutex_exit(&dtrace_meta_lock); 6827 cmn_err(CE_WARN, "failed to register meta-register %s: " 6828 "user-land meta-provider exists", name); 6829 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 6830 kmem_free(meta, sizeof (dtrace_meta_t)); 6831 return (EINVAL); 6832 } 6833 6834 dtrace_meta_pid = meta; 6835 *idp = (dtrace_meta_provider_id_t)meta; 6836 6837 /* 6838 * If there are providers and probes ready to go, pass them 6839 * off to the new meta provider now. 6840 */ 6841 6842 help = dtrace_deferred_pid; 6843 dtrace_deferred_pid = NULL; 6844 6845 mutex_exit(&dtrace_lock); 6846 6847 while (help != NULL) { 6848 for (i = 0; i < help->dthps_nprovs; i++) { 6849 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 6850 help->dthps_pid); 6851 } 6852 6853 next = help->dthps_next; 6854 help->dthps_next = NULL; 6855 help->dthps_prev = NULL; 6856 help->dthps_deferred = 0; 6857 help = next; 6858 } 6859 6860 mutex_exit(&dtrace_meta_lock); 6861 6862 return (0); 6863 } 6864 6865 int 6866 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 6867 { 6868 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 6869 6870 mutex_enter(&dtrace_meta_lock); 6871 mutex_enter(&dtrace_lock); 6872 6873 if (old == dtrace_meta_pid) { 6874 pp = &dtrace_meta_pid; 6875 } else { 6876 panic("attempt to unregister non-existent " 6877 "dtrace meta-provider %p\n", (void *)old); 6878 } 6879 6880 if (old->dtm_count != 0) { 6881 mutex_exit(&dtrace_lock); 6882 mutex_exit(&dtrace_meta_lock); 6883 return (EBUSY); 6884 } 6885 6886 *pp = NULL; 6887 6888 mutex_exit(&dtrace_lock); 6889 mutex_exit(&dtrace_meta_lock); 6890 6891 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 6892 kmem_free(old, sizeof (dtrace_meta_t)); 6893 6894 return (0); 6895 } 6896 6897 6898 /* 6899 * DTrace DIF Object Functions 6900 */ 6901 static int 6902 dtrace_difo_err(uint_t pc, const char *format, ...) 6903 { 6904 if (dtrace_err_verbose) { 6905 va_list alist; 6906 6907 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 6908 va_start(alist, format); 6909 (void) vuprintf(format, alist); 6910 va_end(alist); 6911 } 6912 6913 #ifdef DTRACE_ERRDEBUG 6914 dtrace_errdebug(format); 6915 #endif 6916 return (1); 6917 } 6918 6919 /* 6920 * Validate a DTrace DIF object by checking the IR instructions. The following 6921 * rules are currently enforced by dtrace_difo_validate(): 6922 * 6923 * 1. Each instruction must have a valid opcode 6924 * 2. Each register, string, variable, or subroutine reference must be valid 6925 * 3. No instruction can modify register %r0 (must be zero) 6926 * 4. All instruction reserved bits must be set to zero 6927 * 5. The last instruction must be a "ret" instruction 6928 * 6. All branch targets must reference a valid instruction _after_ the branch 6929 */ 6930 static int 6931 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 6932 cred_t *cr) 6933 { 6934 int err = 0, i; 6935 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 6936 int kcheck; 6937 uint_t pc; 6938 6939 kcheck = cr == NULL || 6940 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE) == 0; 6941 6942 dp->dtdo_destructive = 0; 6943 6944 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 6945 dif_instr_t instr = dp->dtdo_buf[pc]; 6946 6947 uint_t r1 = DIF_INSTR_R1(instr); 6948 uint_t r2 = DIF_INSTR_R2(instr); 6949 uint_t rd = DIF_INSTR_RD(instr); 6950 uint_t rs = DIF_INSTR_RS(instr); 6951 uint_t label = DIF_INSTR_LABEL(instr); 6952 uint_t v = DIF_INSTR_VAR(instr); 6953 uint_t subr = DIF_INSTR_SUBR(instr); 6954 uint_t type = DIF_INSTR_TYPE(instr); 6955 uint_t op = DIF_INSTR_OP(instr); 6956 6957 switch (op) { 6958 case DIF_OP_OR: 6959 case DIF_OP_XOR: 6960 case DIF_OP_AND: 6961 case DIF_OP_SLL: 6962 case DIF_OP_SRL: 6963 case DIF_OP_SRA: 6964 case DIF_OP_SUB: 6965 case DIF_OP_ADD: 6966 case DIF_OP_MUL: 6967 case DIF_OP_SDIV: 6968 case DIF_OP_UDIV: 6969 case DIF_OP_SREM: 6970 case DIF_OP_UREM: 6971 case DIF_OP_COPYS: 6972 if (r1 >= nregs) 6973 err += efunc(pc, "invalid register %u\n", r1); 6974 if (r2 >= nregs) 6975 err += efunc(pc, "invalid register %u\n", r2); 6976 if (rd >= nregs) 6977 err += efunc(pc, "invalid register %u\n", rd); 6978 if (rd == 0) 6979 err += efunc(pc, "cannot write to %r0\n"); 6980 break; 6981 case DIF_OP_NOT: 6982 case DIF_OP_MOV: 6983 case DIF_OP_ALLOCS: 6984 if (r1 >= nregs) 6985 err += efunc(pc, "invalid register %u\n", r1); 6986 if (r2 != 0) 6987 err += efunc(pc, "non-zero reserved bits\n"); 6988 if (rd >= nregs) 6989 err += efunc(pc, "invalid register %u\n", rd); 6990 if (rd == 0) 6991 err += efunc(pc, "cannot write to %r0\n"); 6992 break; 6993 case DIF_OP_LDSB: 6994 case DIF_OP_LDSH: 6995 case DIF_OP_LDSW: 6996 case DIF_OP_LDUB: 6997 case DIF_OP_LDUH: 6998 case DIF_OP_LDUW: 6999 case DIF_OP_LDX: 7000 if (r1 >= nregs) 7001 err += efunc(pc, "invalid register %u\n", r1); 7002 if (r2 != 0) 7003 err += efunc(pc, "non-zero reserved bits\n"); 7004 if (rd >= nregs) 7005 err += efunc(pc, "invalid register %u\n", rd); 7006 if (rd == 0) 7007 err += efunc(pc, "cannot write to %r0\n"); 7008 if (kcheck) 7009 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 7010 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 7011 break; 7012 case DIF_OP_RLDSB: 7013 case DIF_OP_RLDSH: 7014 case DIF_OP_RLDSW: 7015 case DIF_OP_RLDUB: 7016 case DIF_OP_RLDUH: 7017 case DIF_OP_RLDUW: 7018 case DIF_OP_RLDX: 7019 if (r1 >= nregs) 7020 err += efunc(pc, "invalid register %u\n", r1); 7021 if (r2 != 0) 7022 err += efunc(pc, "non-zero reserved bits\n"); 7023 if (rd >= nregs) 7024 err += efunc(pc, "invalid register %u\n", rd); 7025 if (rd == 0) 7026 err += efunc(pc, "cannot write to %r0\n"); 7027 break; 7028 case DIF_OP_ULDSB: 7029 case DIF_OP_ULDSH: 7030 case DIF_OP_ULDSW: 7031 case DIF_OP_ULDUB: 7032 case DIF_OP_ULDUH: 7033 case DIF_OP_ULDUW: 7034 case DIF_OP_ULDX: 7035 if (r1 >= nregs) 7036 err += efunc(pc, "invalid register %u\n", r1); 7037 if (r2 != 0) 7038 err += efunc(pc, "non-zero reserved bits\n"); 7039 if (rd >= nregs) 7040 err += efunc(pc, "invalid register %u\n", rd); 7041 if (rd == 0) 7042 err += efunc(pc, "cannot write to %r0\n"); 7043 break; 7044 case DIF_OP_STB: 7045 case DIF_OP_STH: 7046 case DIF_OP_STW: 7047 case DIF_OP_STX: 7048 if (r1 >= nregs) 7049 err += efunc(pc, "invalid register %u\n", r1); 7050 if (r2 != 0) 7051 err += efunc(pc, "non-zero reserved bits\n"); 7052 if (rd >= nregs) 7053 err += efunc(pc, "invalid register %u\n", rd); 7054 if (rd == 0) 7055 err += efunc(pc, "cannot write to 0 address\n"); 7056 break; 7057 case DIF_OP_CMP: 7058 case DIF_OP_SCMP: 7059 if (r1 >= nregs) 7060 err += efunc(pc, "invalid register %u\n", r1); 7061 if (r2 >= nregs) 7062 err += efunc(pc, "invalid register %u\n", r2); 7063 if (rd != 0) 7064 err += efunc(pc, "non-zero reserved bits\n"); 7065 break; 7066 case DIF_OP_TST: 7067 if (r1 >= nregs) 7068 err += efunc(pc, "invalid register %u\n", r1); 7069 if (r2 != 0 || rd != 0) 7070 err += efunc(pc, "non-zero reserved bits\n"); 7071 break; 7072 case DIF_OP_BA: 7073 case DIF_OP_BE: 7074 case DIF_OP_BNE: 7075 case DIF_OP_BG: 7076 case DIF_OP_BGU: 7077 case DIF_OP_BGE: 7078 case DIF_OP_BGEU: 7079 case DIF_OP_BL: 7080 case DIF_OP_BLU: 7081 case DIF_OP_BLE: 7082 case DIF_OP_BLEU: 7083 if (label >= dp->dtdo_len) { 7084 err += efunc(pc, "invalid branch target %u\n", 7085 label); 7086 } 7087 if (label <= pc) { 7088 err += efunc(pc, "backward branch to %u\n", 7089 label); 7090 } 7091 break; 7092 case DIF_OP_RET: 7093 if (r1 != 0 || r2 != 0) 7094 err += efunc(pc, "non-zero reserved bits\n"); 7095 if (rd >= nregs) 7096 err += efunc(pc, "invalid register %u\n", rd); 7097 break; 7098 case DIF_OP_NOP: 7099 case DIF_OP_POPTS: 7100 case DIF_OP_FLUSHTS: 7101 if (r1 != 0 || r2 != 0 || rd != 0) 7102 err += efunc(pc, "non-zero reserved bits\n"); 7103 break; 7104 case DIF_OP_SETX: 7105 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 7106 err += efunc(pc, "invalid integer ref %u\n", 7107 DIF_INSTR_INTEGER(instr)); 7108 } 7109 if (rd >= nregs) 7110 err += efunc(pc, "invalid register %u\n", rd); 7111 if (rd == 0) 7112 err += efunc(pc, "cannot write to %r0\n"); 7113 break; 7114 case DIF_OP_SETS: 7115 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 7116 err += efunc(pc, "invalid string ref %u\n", 7117 DIF_INSTR_STRING(instr)); 7118 } 7119 if (rd >= nregs) 7120 err += efunc(pc, "invalid register %u\n", rd); 7121 if (rd == 0) 7122 err += efunc(pc, "cannot write to %r0\n"); 7123 break; 7124 case DIF_OP_LDGA: 7125 case DIF_OP_LDTA: 7126 if (r1 > DIF_VAR_ARRAY_MAX) 7127 err += efunc(pc, "invalid array %u\n", r1); 7128 if (r2 >= nregs) 7129 err += efunc(pc, "invalid register %u\n", r2); 7130 if (rd >= nregs) 7131 err += efunc(pc, "invalid register %u\n", rd); 7132 if (rd == 0) 7133 err += efunc(pc, "cannot write to %r0\n"); 7134 break; 7135 case DIF_OP_LDGS: 7136 case DIF_OP_LDTS: 7137 case DIF_OP_LDLS: 7138 case DIF_OP_LDGAA: 7139 case DIF_OP_LDTAA: 7140 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 7141 err += efunc(pc, "invalid variable %u\n", v); 7142 if (rd >= nregs) 7143 err += efunc(pc, "invalid register %u\n", rd); 7144 if (rd == 0) 7145 err += efunc(pc, "cannot write to %r0\n"); 7146 break; 7147 case DIF_OP_STGS: 7148 case DIF_OP_STTS: 7149 case DIF_OP_STLS: 7150 case DIF_OP_STGAA: 7151 case DIF_OP_STTAA: 7152 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 7153 err += efunc(pc, "invalid variable %u\n", v); 7154 if (rs >= nregs) 7155 err += efunc(pc, "invalid register %u\n", rd); 7156 break; 7157 case DIF_OP_CALL: 7158 if (subr > DIF_SUBR_MAX) 7159 err += efunc(pc, "invalid subr %u\n", subr); 7160 if (rd >= nregs) 7161 err += efunc(pc, "invalid register %u\n", rd); 7162 if (rd == 0) 7163 err += efunc(pc, "cannot write to %r0\n"); 7164 7165 if (subr == DIF_SUBR_COPYOUT || 7166 subr == DIF_SUBR_COPYOUTSTR) { 7167 dp->dtdo_destructive = 1; 7168 } 7169 break; 7170 case DIF_OP_PUSHTR: 7171 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 7172 err += efunc(pc, "invalid ref type %u\n", type); 7173 if (r2 >= nregs) 7174 err += efunc(pc, "invalid register %u\n", r2); 7175 if (rs >= nregs) 7176 err += efunc(pc, "invalid register %u\n", rs); 7177 break; 7178 case DIF_OP_PUSHTV: 7179 if (type != DIF_TYPE_CTF) 7180 err += efunc(pc, "invalid val type %u\n", type); 7181 if (r2 >= nregs) 7182 err += efunc(pc, "invalid register %u\n", r2); 7183 if (rs >= nregs) 7184 err += efunc(pc, "invalid register %u\n", rs); 7185 break; 7186 default: 7187 err += efunc(pc, "invalid opcode %u\n", 7188 DIF_INSTR_OP(instr)); 7189 } 7190 } 7191 7192 if (dp->dtdo_len != 0 && 7193 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 7194 err += efunc(dp->dtdo_len - 1, 7195 "expected 'ret' as last DIF instruction\n"); 7196 } 7197 7198 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 7199 /* 7200 * If we're not returning by reference, the size must be either 7201 * 0 or the size of one of the base types. 7202 */ 7203 switch (dp->dtdo_rtype.dtdt_size) { 7204 case 0: 7205 case sizeof (uint8_t): 7206 case sizeof (uint16_t): 7207 case sizeof (uint32_t): 7208 case sizeof (uint64_t): 7209 break; 7210 7211 default: 7212 err += efunc(dp->dtdo_len - 1, "bad return size"); 7213 } 7214 } 7215 7216 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 7217 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 7218 dtrace_diftype_t *vt, *et; 7219 uint_t id, ndx; 7220 7221 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 7222 v->dtdv_scope != DIFV_SCOPE_THREAD && 7223 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 7224 err += efunc(i, "unrecognized variable scope %d\n", 7225 v->dtdv_scope); 7226 break; 7227 } 7228 7229 if (v->dtdv_kind != DIFV_KIND_ARRAY && 7230 v->dtdv_kind != DIFV_KIND_SCALAR) { 7231 err += efunc(i, "unrecognized variable type %d\n", 7232 v->dtdv_kind); 7233 break; 7234 } 7235 7236 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 7237 err += efunc(i, "%d exceeds variable id limit\n", id); 7238 break; 7239 } 7240 7241 if (id < DIF_VAR_OTHER_UBASE) 7242 continue; 7243 7244 /* 7245 * For user-defined variables, we need to check that this 7246 * definition is identical to any previous definition that we 7247 * encountered. 7248 */ 7249 ndx = id - DIF_VAR_OTHER_UBASE; 7250 7251 switch (v->dtdv_scope) { 7252 case DIFV_SCOPE_GLOBAL: 7253 if (ndx < vstate->dtvs_nglobals) { 7254 dtrace_statvar_t *svar; 7255 7256 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 7257 existing = &svar->dtsv_var; 7258 } 7259 7260 break; 7261 7262 case DIFV_SCOPE_THREAD: 7263 if (ndx < vstate->dtvs_ntlocals) 7264 existing = &vstate->dtvs_tlocals[ndx]; 7265 break; 7266 7267 case DIFV_SCOPE_LOCAL: 7268 if (ndx < vstate->dtvs_nlocals) { 7269 dtrace_statvar_t *svar; 7270 7271 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 7272 existing = &svar->dtsv_var; 7273 } 7274 7275 break; 7276 } 7277 7278 vt = &v->dtdv_type; 7279 7280 if (vt->dtdt_flags & DIF_TF_BYREF) { 7281 if (vt->dtdt_size == 0) { 7282 err += efunc(i, "zero-sized variable\n"); 7283 break; 7284 } 7285 7286 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 7287 vt->dtdt_size > dtrace_global_maxsize) { 7288 err += efunc(i, "oversized by-ref global\n"); 7289 break; 7290 } 7291 } 7292 7293 if (existing == NULL || existing->dtdv_id == 0) 7294 continue; 7295 7296 ASSERT(existing->dtdv_id == v->dtdv_id); 7297 ASSERT(existing->dtdv_scope == v->dtdv_scope); 7298 7299 if (existing->dtdv_kind != v->dtdv_kind) 7300 err += efunc(i, "%d changed variable kind\n", id); 7301 7302 et = &existing->dtdv_type; 7303 7304 if (vt->dtdt_flags != et->dtdt_flags) { 7305 err += efunc(i, "%d changed variable type flags\n", id); 7306 break; 7307 } 7308 7309 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 7310 err += efunc(i, "%d changed variable type size\n", id); 7311 break; 7312 } 7313 } 7314 7315 return (err); 7316 } 7317 7318 /* 7319 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 7320 * are much more constrained than normal DIFOs. Specifically, they may 7321 * not: 7322 * 7323 * 1. Make calls to subroutines other than copyin(), copyinstr() or 7324 * miscellaneous string routines 7325 * 2. Access DTrace variables other than the args[] array, and the 7326 * curthread, pid, tid and execname variables. 7327 * 3. Have thread-local variables. 7328 * 4. Have dynamic variables. 7329 */ 7330 static int 7331 dtrace_difo_validate_helper(dtrace_difo_t *dp) 7332 { 7333 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 7334 int err = 0; 7335 uint_t pc; 7336 7337 for (pc = 0; pc < dp->dtdo_len; pc++) { 7338 dif_instr_t instr = dp->dtdo_buf[pc]; 7339 7340 uint_t v = DIF_INSTR_VAR(instr); 7341 uint_t subr = DIF_INSTR_SUBR(instr); 7342 uint_t op = DIF_INSTR_OP(instr); 7343 7344 switch (op) { 7345 case DIF_OP_OR: 7346 case DIF_OP_XOR: 7347 case DIF_OP_AND: 7348 case DIF_OP_SLL: 7349 case DIF_OP_SRL: 7350 case DIF_OP_SRA: 7351 case DIF_OP_SUB: 7352 case DIF_OP_ADD: 7353 case DIF_OP_MUL: 7354 case DIF_OP_SDIV: 7355 case DIF_OP_UDIV: 7356 case DIF_OP_SREM: 7357 case DIF_OP_UREM: 7358 case DIF_OP_COPYS: 7359 case DIF_OP_NOT: 7360 case DIF_OP_MOV: 7361 case DIF_OP_RLDSB: 7362 case DIF_OP_RLDSH: 7363 case DIF_OP_RLDSW: 7364 case DIF_OP_RLDUB: 7365 case DIF_OP_RLDUH: 7366 case DIF_OP_RLDUW: 7367 case DIF_OP_RLDX: 7368 case DIF_OP_ULDSB: 7369 case DIF_OP_ULDSH: 7370 case DIF_OP_ULDSW: 7371 case DIF_OP_ULDUB: 7372 case DIF_OP_ULDUH: 7373 case DIF_OP_ULDUW: 7374 case DIF_OP_ULDX: 7375 case DIF_OP_STB: 7376 case DIF_OP_STH: 7377 case DIF_OP_STW: 7378 case DIF_OP_STX: 7379 case DIF_OP_ALLOCS: 7380 case DIF_OP_CMP: 7381 case DIF_OP_SCMP: 7382 case DIF_OP_TST: 7383 case DIF_OP_BA: 7384 case DIF_OP_BE: 7385 case DIF_OP_BNE: 7386 case DIF_OP_BG: 7387 case DIF_OP_BGU: 7388 case DIF_OP_BGE: 7389 case DIF_OP_BGEU: 7390 case DIF_OP_BL: 7391 case DIF_OP_BLU: 7392 case DIF_OP_BLE: 7393 case DIF_OP_BLEU: 7394 case DIF_OP_RET: 7395 case DIF_OP_NOP: 7396 case DIF_OP_POPTS: 7397 case DIF_OP_FLUSHTS: 7398 case DIF_OP_SETX: 7399 case DIF_OP_SETS: 7400 case DIF_OP_LDGA: 7401 case DIF_OP_LDLS: 7402 case DIF_OP_STGS: 7403 case DIF_OP_STLS: 7404 case DIF_OP_PUSHTR: 7405 case DIF_OP_PUSHTV: 7406 break; 7407 7408 case DIF_OP_LDGS: 7409 if (v >= DIF_VAR_OTHER_UBASE) 7410 break; 7411 7412 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 7413 break; 7414 7415 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 7416 v == DIF_VAR_TID || v == DIF_VAR_EXECNAME || 7417 v == DIF_VAR_ZONENAME) 7418 break; 7419 7420 err += efunc(pc, "illegal variable %u\n", v); 7421 break; 7422 7423 case DIF_OP_LDTA: 7424 case DIF_OP_LDTS: 7425 case DIF_OP_LDGAA: 7426 case DIF_OP_LDTAA: 7427 err += efunc(pc, "illegal dynamic variable load\n"); 7428 break; 7429 7430 case DIF_OP_STTS: 7431 case DIF_OP_STGAA: 7432 case DIF_OP_STTAA: 7433 err += efunc(pc, "illegal dynamic variable store\n"); 7434 break; 7435 7436 case DIF_OP_CALL: 7437 if (subr == DIF_SUBR_ALLOCA || 7438 subr == DIF_SUBR_BCOPY || 7439 subr == DIF_SUBR_COPYIN || 7440 subr == DIF_SUBR_COPYINTO || 7441 subr == DIF_SUBR_COPYINSTR || 7442 subr == DIF_SUBR_INDEX || 7443 subr == DIF_SUBR_LLTOSTR || 7444 subr == DIF_SUBR_RINDEX || 7445 subr == DIF_SUBR_STRCHR || 7446 subr == DIF_SUBR_STRJOIN || 7447 subr == DIF_SUBR_STRRCHR || 7448 subr == DIF_SUBR_STRSTR) 7449 break; 7450 7451 err += efunc(pc, "invalid subr %u\n", subr); 7452 break; 7453 7454 default: 7455 err += efunc(pc, "invalid opcode %u\n", 7456 DIF_INSTR_OP(instr)); 7457 } 7458 } 7459 7460 return (err); 7461 } 7462 7463 /* 7464 * Returns 1 if the expression in the DIF object can be cached on a per-thread 7465 * basis; 0 if not. 7466 */ 7467 static int 7468 dtrace_difo_cacheable(dtrace_difo_t *dp) 7469 { 7470 int i; 7471 7472 if (dp == NULL) 7473 return (0); 7474 7475 for (i = 0; i < dp->dtdo_varlen; i++) { 7476 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7477 7478 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 7479 continue; 7480 7481 switch (v->dtdv_id) { 7482 case DIF_VAR_CURTHREAD: 7483 case DIF_VAR_PID: 7484 case DIF_VAR_TID: 7485 case DIF_VAR_EXECNAME: 7486 case DIF_VAR_ZONENAME: 7487 break; 7488 7489 default: 7490 return (0); 7491 } 7492 } 7493 7494 /* 7495 * This DIF object may be cacheable. Now we need to look for any 7496 * array loading instructions, any memory loading instructions, or 7497 * any stores to thread-local variables. 7498 */ 7499 for (i = 0; i < dp->dtdo_len; i++) { 7500 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 7501 7502 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 7503 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 7504 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 7505 op == DIF_OP_LDGA || op == DIF_OP_STTS) 7506 return (0); 7507 } 7508 7509 return (1); 7510 } 7511 7512 static void 7513 dtrace_difo_hold(dtrace_difo_t *dp) 7514 { 7515 int i; 7516 7517 ASSERT(MUTEX_HELD(&dtrace_lock)); 7518 7519 dp->dtdo_refcnt++; 7520 ASSERT(dp->dtdo_refcnt != 0); 7521 7522 /* 7523 * We need to check this DIF object for references to the variable 7524 * DIF_VAR_VTIMESTAMP. 7525 */ 7526 for (i = 0; i < dp->dtdo_varlen; i++) { 7527 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7528 7529 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 7530 continue; 7531 7532 if (dtrace_vtime_references++ == 0) 7533 dtrace_vtime_enable(); 7534 } 7535 } 7536 7537 /* 7538 * This routine calculates the dynamic variable chunksize for a given DIF 7539 * object. The calculation is not fool-proof, and can probably be tricked by 7540 * malicious DIF -- but it works for all compiler-generated DIF. Because this 7541 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 7542 * if a dynamic variable size exceeds the chunksize. 7543 */ 7544 static void 7545 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7546 { 7547 uint64_t sval; 7548 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 7549 const dif_instr_t *text = dp->dtdo_buf; 7550 uint_t pc, srd = 0; 7551 uint_t ttop = 0; 7552 size_t size, ksize; 7553 uint_t id, i; 7554 7555 for (pc = 0; pc < dp->dtdo_len; pc++) { 7556 dif_instr_t instr = text[pc]; 7557 uint_t op = DIF_INSTR_OP(instr); 7558 uint_t rd = DIF_INSTR_RD(instr); 7559 uint_t r1 = DIF_INSTR_R1(instr); 7560 uint_t nkeys = 0; 7561 uchar_t scope; 7562 7563 dtrace_key_t *key = tupregs; 7564 7565 switch (op) { 7566 case DIF_OP_SETX: 7567 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 7568 srd = rd; 7569 continue; 7570 7571 case DIF_OP_STTS: 7572 key = &tupregs[DIF_DTR_NREGS]; 7573 key[0].dttk_size = 0; 7574 key[1].dttk_size = 0; 7575 nkeys = 2; 7576 scope = DIFV_SCOPE_THREAD; 7577 break; 7578 7579 case DIF_OP_STGAA: 7580 case DIF_OP_STTAA: 7581 nkeys = ttop; 7582 7583 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 7584 key[nkeys++].dttk_size = 0; 7585 7586 key[nkeys++].dttk_size = 0; 7587 7588 if (op == DIF_OP_STTAA) { 7589 scope = DIFV_SCOPE_THREAD; 7590 } else { 7591 scope = DIFV_SCOPE_GLOBAL; 7592 } 7593 7594 break; 7595 7596 case DIF_OP_PUSHTR: 7597 if (ttop == DIF_DTR_NREGS) 7598 return; 7599 7600 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 7601 /* 7602 * If the register for the size of the "pushtr" 7603 * is %r0 (or the value is 0) and the type is 7604 * a string, we'll use the system-wide default 7605 * string size. 7606 */ 7607 tupregs[ttop++].dttk_size = 7608 dtrace_strsize_default; 7609 } else { 7610 if (srd == 0) 7611 return; 7612 7613 tupregs[ttop++].dttk_size = sval; 7614 } 7615 7616 break; 7617 7618 case DIF_OP_PUSHTV: 7619 if (ttop == DIF_DTR_NREGS) 7620 return; 7621 7622 tupregs[ttop++].dttk_size = 0; 7623 break; 7624 7625 case DIF_OP_FLUSHTS: 7626 ttop = 0; 7627 break; 7628 7629 case DIF_OP_POPTS: 7630 if (ttop != 0) 7631 ttop--; 7632 break; 7633 } 7634 7635 sval = 0; 7636 srd = 0; 7637 7638 if (nkeys == 0) 7639 continue; 7640 7641 /* 7642 * We have a dynamic variable allocation; calculate its size. 7643 */ 7644 for (ksize = 0, i = 0; i < nkeys; i++) 7645 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 7646 7647 size = sizeof (dtrace_dynvar_t); 7648 size += sizeof (dtrace_key_t) * (nkeys - 1); 7649 size += ksize; 7650 7651 /* 7652 * Now we need to determine the size of the stored data. 7653 */ 7654 id = DIF_INSTR_VAR(instr); 7655 7656 for (i = 0; i < dp->dtdo_varlen; i++) { 7657 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7658 7659 if (v->dtdv_id == id && v->dtdv_scope == scope) { 7660 size += v->dtdv_type.dtdt_size; 7661 break; 7662 } 7663 } 7664 7665 if (i == dp->dtdo_varlen) 7666 return; 7667 7668 /* 7669 * We have the size. If this is larger than the chunk size 7670 * for our dynamic variable state, reset the chunk size. 7671 */ 7672 size = P2ROUNDUP(size, sizeof (uint64_t)); 7673 7674 if (size > vstate->dtvs_dynvars.dtds_chunksize) 7675 vstate->dtvs_dynvars.dtds_chunksize = size; 7676 } 7677 } 7678 7679 static void 7680 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7681 { 7682 int i, oldsvars, osz, nsz, otlocals, ntlocals; 7683 uint_t id; 7684 7685 ASSERT(MUTEX_HELD(&dtrace_lock)); 7686 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 7687 7688 for (i = 0; i < dp->dtdo_varlen; i++) { 7689 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7690 dtrace_statvar_t *svar, ***svarp; 7691 size_t dsize = 0; 7692 uint8_t scope = v->dtdv_scope; 7693 int *np; 7694 7695 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 7696 continue; 7697 7698 id -= DIF_VAR_OTHER_UBASE; 7699 7700 switch (scope) { 7701 case DIFV_SCOPE_THREAD: 7702 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 7703 dtrace_difv_t *tlocals; 7704 7705 if ((ntlocals = (otlocals << 1)) == 0) 7706 ntlocals = 1; 7707 7708 osz = otlocals * sizeof (dtrace_difv_t); 7709 nsz = ntlocals * sizeof (dtrace_difv_t); 7710 7711 tlocals = kmem_zalloc(nsz, KM_SLEEP); 7712 7713 if (osz != 0) { 7714 bcopy(vstate->dtvs_tlocals, 7715 tlocals, osz); 7716 kmem_free(vstate->dtvs_tlocals, osz); 7717 } 7718 7719 vstate->dtvs_tlocals = tlocals; 7720 vstate->dtvs_ntlocals = ntlocals; 7721 } 7722 7723 vstate->dtvs_tlocals[id] = *v; 7724 continue; 7725 7726 case DIFV_SCOPE_LOCAL: 7727 np = &vstate->dtvs_nlocals; 7728 svarp = &vstate->dtvs_locals; 7729 7730 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 7731 dsize = NCPU * (v->dtdv_type.dtdt_size + 7732 sizeof (uint64_t)); 7733 else 7734 dsize = NCPU * sizeof (uint64_t); 7735 7736 break; 7737 7738 case DIFV_SCOPE_GLOBAL: 7739 np = &vstate->dtvs_nglobals; 7740 svarp = &vstate->dtvs_globals; 7741 7742 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 7743 dsize = v->dtdv_type.dtdt_size + 7744 sizeof (uint64_t); 7745 7746 break; 7747 7748 default: 7749 ASSERT(0); 7750 } 7751 7752 while (id >= (oldsvars = *np)) { 7753 dtrace_statvar_t **statics; 7754 int newsvars, oldsize, newsize; 7755 7756 if ((newsvars = (oldsvars << 1)) == 0) 7757 newsvars = 1; 7758 7759 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 7760 newsize = newsvars * sizeof (dtrace_statvar_t *); 7761 7762 statics = kmem_zalloc(newsize, KM_SLEEP); 7763 7764 if (oldsize != 0) { 7765 bcopy(*svarp, statics, oldsize); 7766 kmem_free(*svarp, oldsize); 7767 } 7768 7769 *svarp = statics; 7770 *np = newsvars; 7771 } 7772 7773 if ((svar = (*svarp)[id]) == NULL) { 7774 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 7775 svar->dtsv_var = *v; 7776 7777 if ((svar->dtsv_size = dsize) != 0) { 7778 svar->dtsv_data = (uint64_t)(uintptr_t) 7779 kmem_zalloc(dsize, KM_SLEEP); 7780 } 7781 7782 (*svarp)[id] = svar; 7783 } 7784 7785 svar->dtsv_refcnt++; 7786 } 7787 7788 dtrace_difo_chunksize(dp, vstate); 7789 dtrace_difo_hold(dp); 7790 } 7791 7792 static dtrace_difo_t * 7793 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7794 { 7795 dtrace_difo_t *new; 7796 size_t sz; 7797 7798 ASSERT(dp->dtdo_buf != NULL); 7799 ASSERT(dp->dtdo_refcnt != 0); 7800 7801 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 7802 7803 ASSERT(dp->dtdo_buf != NULL); 7804 sz = dp->dtdo_len * sizeof (dif_instr_t); 7805 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 7806 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 7807 new->dtdo_len = dp->dtdo_len; 7808 7809 if (dp->dtdo_strtab != NULL) { 7810 ASSERT(dp->dtdo_strlen != 0); 7811 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 7812 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 7813 new->dtdo_strlen = dp->dtdo_strlen; 7814 } 7815 7816 if (dp->dtdo_inttab != NULL) { 7817 ASSERT(dp->dtdo_intlen != 0); 7818 sz = dp->dtdo_intlen * sizeof (uint64_t); 7819 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 7820 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 7821 new->dtdo_intlen = dp->dtdo_intlen; 7822 } 7823 7824 if (dp->dtdo_vartab != NULL) { 7825 ASSERT(dp->dtdo_varlen != 0); 7826 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 7827 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 7828 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 7829 new->dtdo_varlen = dp->dtdo_varlen; 7830 } 7831 7832 dtrace_difo_init(new, vstate); 7833 return (new); 7834 } 7835 7836 static void 7837 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7838 { 7839 int i; 7840 7841 ASSERT(dp->dtdo_refcnt == 0); 7842 7843 for (i = 0; i < dp->dtdo_varlen; i++) { 7844 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7845 dtrace_statvar_t *svar, **svarp; 7846 uint_t id; 7847 uint8_t scope = v->dtdv_scope; 7848 int *np; 7849 7850 switch (scope) { 7851 case DIFV_SCOPE_THREAD: 7852 continue; 7853 7854 case DIFV_SCOPE_LOCAL: 7855 np = &vstate->dtvs_nlocals; 7856 svarp = vstate->dtvs_locals; 7857 break; 7858 7859 case DIFV_SCOPE_GLOBAL: 7860 np = &vstate->dtvs_nglobals; 7861 svarp = vstate->dtvs_globals; 7862 break; 7863 7864 default: 7865 ASSERT(0); 7866 } 7867 7868 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 7869 continue; 7870 7871 id -= DIF_VAR_OTHER_UBASE; 7872 ASSERT(id < *np); 7873 7874 svar = svarp[id]; 7875 ASSERT(svar != NULL); 7876 ASSERT(svar->dtsv_refcnt > 0); 7877 7878 if (--svar->dtsv_refcnt > 0) 7879 continue; 7880 7881 if (svar->dtsv_size != 0) { 7882 ASSERT(svar->dtsv_data != NULL); 7883 kmem_free((void *)(uintptr_t)svar->dtsv_data, 7884 svar->dtsv_size); 7885 } 7886 7887 kmem_free(svar, sizeof (dtrace_statvar_t)); 7888 svarp[id] = NULL; 7889 } 7890 7891 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 7892 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 7893 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 7894 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 7895 7896 kmem_free(dp, sizeof (dtrace_difo_t)); 7897 } 7898 7899 static void 7900 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7901 { 7902 int i; 7903 7904 ASSERT(MUTEX_HELD(&dtrace_lock)); 7905 ASSERT(dp->dtdo_refcnt != 0); 7906 7907 for (i = 0; i < dp->dtdo_varlen; i++) { 7908 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7909 7910 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 7911 continue; 7912 7913 ASSERT(dtrace_vtime_references > 0); 7914 if (--dtrace_vtime_references == 0) 7915 dtrace_vtime_disable(); 7916 } 7917 7918 if (--dp->dtdo_refcnt == 0) 7919 dtrace_difo_destroy(dp, vstate); 7920 } 7921 7922 /* 7923 * DTrace Format Functions 7924 */ 7925 static uint16_t 7926 dtrace_format_add(dtrace_state_t *state, char *str) 7927 { 7928 char *fmt, **new; 7929 uint16_t ndx, len = strlen(str) + 1; 7930 7931 fmt = kmem_zalloc(len, KM_SLEEP); 7932 bcopy(str, fmt, len); 7933 7934 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 7935 if (state->dts_formats[ndx] == NULL) { 7936 state->dts_formats[ndx] = fmt; 7937 return (ndx + 1); 7938 } 7939 } 7940 7941 if (state->dts_nformats == USHRT_MAX) { 7942 /* 7943 * This is only likely if a denial-of-service attack is being 7944 * attempted. As such, it's okay to fail silently here. 7945 */ 7946 kmem_free(fmt, len); 7947 return (0); 7948 } 7949 7950 /* 7951 * For simplicity, we always resize the formats array to be exactly the 7952 * number of formats. 7953 */ 7954 ndx = state->dts_nformats++; 7955 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 7956 7957 if (state->dts_formats != NULL) { 7958 ASSERT(ndx != 0); 7959 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 7960 kmem_free(state->dts_formats, ndx * sizeof (char *)); 7961 } 7962 7963 state->dts_formats = new; 7964 state->dts_formats[ndx] = fmt; 7965 7966 return (ndx + 1); 7967 } 7968 7969 static void 7970 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 7971 { 7972 char *fmt; 7973 7974 ASSERT(state->dts_formats != NULL); 7975 ASSERT(format <= state->dts_nformats); 7976 ASSERT(state->dts_formats[format - 1] != NULL); 7977 7978 fmt = state->dts_formats[format - 1]; 7979 kmem_free(fmt, strlen(fmt) + 1); 7980 state->dts_formats[format - 1] = NULL; 7981 } 7982 7983 static void 7984 dtrace_format_destroy(dtrace_state_t *state) 7985 { 7986 int i; 7987 7988 if (state->dts_nformats == 0) { 7989 ASSERT(state->dts_formats == NULL); 7990 return; 7991 } 7992 7993 ASSERT(state->dts_formats != NULL); 7994 7995 for (i = 0; i < state->dts_nformats; i++) { 7996 char *fmt = state->dts_formats[i]; 7997 7998 if (fmt == NULL) 7999 continue; 8000 8001 kmem_free(fmt, strlen(fmt) + 1); 8002 } 8003 8004 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 8005 state->dts_nformats = 0; 8006 state->dts_formats = NULL; 8007 } 8008 8009 /* 8010 * DTrace Predicate Functions 8011 */ 8012 static dtrace_predicate_t * 8013 dtrace_predicate_create(dtrace_difo_t *dp) 8014 { 8015 dtrace_predicate_t *pred; 8016 8017 ASSERT(MUTEX_HELD(&dtrace_lock)); 8018 ASSERT(dp->dtdo_refcnt != 0); 8019 8020 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 8021 pred->dtp_difo = dp; 8022 pred->dtp_refcnt = 1; 8023 8024 if (!dtrace_difo_cacheable(dp)) 8025 return (pred); 8026 8027 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 8028 /* 8029 * This is only theoretically possible -- we have had 2^32 8030 * cacheable predicates on this machine. We cannot allow any 8031 * more predicates to become cacheable: as unlikely as it is, 8032 * there may be a thread caching a (now stale) predicate cache 8033 * ID. (N.B.: the temptation is being successfully resisted to 8034 * have this cmn_err() "Holy shit -- we executed this code!") 8035 */ 8036 return (pred); 8037 } 8038 8039 pred->dtp_cacheid = dtrace_predcache_id++; 8040 8041 return (pred); 8042 } 8043 8044 static void 8045 dtrace_predicate_hold(dtrace_predicate_t *pred) 8046 { 8047 ASSERT(MUTEX_HELD(&dtrace_lock)); 8048 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 8049 ASSERT(pred->dtp_refcnt > 0); 8050 8051 pred->dtp_refcnt++; 8052 } 8053 8054 static void 8055 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 8056 { 8057 dtrace_difo_t *dp = pred->dtp_difo; 8058 8059 ASSERT(MUTEX_HELD(&dtrace_lock)); 8060 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 8061 ASSERT(pred->dtp_refcnt > 0); 8062 8063 if (--pred->dtp_refcnt == 0) { 8064 dtrace_difo_release(pred->dtp_difo, vstate); 8065 kmem_free(pred, sizeof (dtrace_predicate_t)); 8066 } 8067 } 8068 8069 /* 8070 * DTrace Action Description Functions 8071 */ 8072 static dtrace_actdesc_t * 8073 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 8074 uint64_t uarg, uint64_t arg) 8075 { 8076 dtrace_actdesc_t *act; 8077 8078 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 8079 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 8080 8081 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 8082 act->dtad_kind = kind; 8083 act->dtad_ntuple = ntuple; 8084 act->dtad_uarg = uarg; 8085 act->dtad_arg = arg; 8086 act->dtad_refcnt = 1; 8087 8088 return (act); 8089 } 8090 8091 static void 8092 dtrace_actdesc_hold(dtrace_actdesc_t *act) 8093 { 8094 ASSERT(act->dtad_refcnt >= 1); 8095 act->dtad_refcnt++; 8096 } 8097 8098 static void 8099 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 8100 { 8101 dtrace_actkind_t kind = act->dtad_kind; 8102 dtrace_difo_t *dp; 8103 8104 ASSERT(act->dtad_refcnt >= 1); 8105 8106 if (--act->dtad_refcnt != 0) 8107 return; 8108 8109 if ((dp = act->dtad_difo) != NULL) 8110 dtrace_difo_release(dp, vstate); 8111 8112 if (DTRACEACT_ISPRINTFLIKE(kind)) { 8113 char *str = (char *)(uintptr_t)act->dtad_arg; 8114 8115 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 8116 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 8117 8118 if (str != NULL) 8119 kmem_free(str, strlen(str) + 1); 8120 } 8121 8122 kmem_free(act, sizeof (dtrace_actdesc_t)); 8123 } 8124 8125 /* 8126 * DTrace ECB Functions 8127 */ 8128 static dtrace_ecb_t * 8129 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 8130 { 8131 dtrace_ecb_t *ecb; 8132 dtrace_epid_t epid; 8133 8134 ASSERT(MUTEX_HELD(&dtrace_lock)); 8135 8136 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 8137 ecb->dte_predicate = NULL; 8138 ecb->dte_probe = probe; 8139 8140 /* 8141 * The default size is the size of the default action: recording 8142 * the epid. 8143 */ 8144 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 8145 ecb->dte_alignment = sizeof (dtrace_epid_t); 8146 8147 epid = state->dts_epid++; 8148 8149 if (epid - 1 >= state->dts_necbs) { 8150 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 8151 int necbs = state->dts_necbs << 1; 8152 8153 ASSERT(epid == state->dts_necbs + 1); 8154 8155 if (necbs == 0) { 8156 ASSERT(oecbs == NULL); 8157 necbs = 1; 8158 } 8159 8160 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 8161 8162 if (oecbs != NULL) 8163 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 8164 8165 dtrace_membar_producer(); 8166 state->dts_ecbs = ecbs; 8167 8168 if (oecbs != NULL) { 8169 /* 8170 * If this state is active, we must dtrace_sync() 8171 * before we can free the old dts_ecbs array: we're 8172 * coming in hot, and there may be active ring 8173 * buffer processing (which indexes into the dts_ecbs 8174 * array) on another CPU. 8175 */ 8176 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 8177 dtrace_sync(); 8178 8179 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 8180 } 8181 8182 dtrace_membar_producer(); 8183 state->dts_necbs = necbs; 8184 } 8185 8186 ecb->dte_state = state; 8187 8188 ASSERT(state->dts_ecbs[epid - 1] == NULL); 8189 dtrace_membar_producer(); 8190 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 8191 8192 return (ecb); 8193 } 8194 8195 static void 8196 dtrace_ecb_enable(dtrace_ecb_t *ecb) 8197 { 8198 dtrace_probe_t *probe = ecb->dte_probe; 8199 8200 ASSERT(MUTEX_HELD(&cpu_lock)); 8201 ASSERT(MUTEX_HELD(&dtrace_lock)); 8202 ASSERT(ecb->dte_next == NULL); 8203 8204 if (probe == NULL) { 8205 /* 8206 * This is the NULL probe -- there's nothing to do. 8207 */ 8208 return; 8209 } 8210 8211 if (probe->dtpr_ecb == NULL) { 8212 dtrace_provider_t *prov = probe->dtpr_provider; 8213 8214 /* 8215 * We're the first ECB on this probe. 8216 */ 8217 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 8218 8219 if (ecb->dte_predicate != NULL) 8220 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 8221 8222 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 8223 probe->dtpr_id, probe->dtpr_arg); 8224 } else { 8225 /* 8226 * This probe is already active. Swing the last pointer to 8227 * point to the new ECB, and issue a dtrace_sync() to assure 8228 * that all CPUs have seen the change. 8229 */ 8230 ASSERT(probe->dtpr_ecb_last != NULL); 8231 probe->dtpr_ecb_last->dte_next = ecb; 8232 probe->dtpr_ecb_last = ecb; 8233 probe->dtpr_predcache = 0; 8234 8235 dtrace_sync(); 8236 } 8237 } 8238 8239 static void 8240 dtrace_ecb_resize(dtrace_ecb_t *ecb) 8241 { 8242 uint32_t maxalign = sizeof (dtrace_epid_t); 8243 uint32_t align = sizeof (uint8_t), offs, diff; 8244 dtrace_action_t *act; 8245 int wastuple = 0; 8246 uint32_t aggbase = UINT32_MAX; 8247 dtrace_state_t *state = ecb->dte_state; 8248 8249 /* 8250 * If we record anything, we always record the epid. (And we always 8251 * record it first.) 8252 */ 8253 offs = sizeof (dtrace_epid_t); 8254 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 8255 8256 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 8257 dtrace_recdesc_t *rec = &act->dta_rec; 8258 8259 if ((align = rec->dtrd_alignment) > maxalign) 8260 maxalign = align; 8261 8262 if (!wastuple && act->dta_intuple) { 8263 /* 8264 * This is the first record in a tuple. Align the 8265 * offset to be at offset 4 in an 8-byte aligned 8266 * block. 8267 */ 8268 diff = offs + sizeof (dtrace_aggid_t); 8269 8270 if (diff = (diff & (sizeof (uint64_t) - 1))) 8271 offs += sizeof (uint64_t) - diff; 8272 8273 aggbase = offs - sizeof (dtrace_aggid_t); 8274 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 8275 } 8276 8277 /*LINTED*/ 8278 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 8279 /* 8280 * The current offset is not properly aligned; align it. 8281 */ 8282 offs += align - diff; 8283 } 8284 8285 rec->dtrd_offset = offs; 8286 8287 if (offs + rec->dtrd_size > ecb->dte_needed) { 8288 ecb->dte_needed = offs + rec->dtrd_size; 8289 8290 if (ecb->dte_needed > state->dts_needed) 8291 state->dts_needed = ecb->dte_needed; 8292 } 8293 8294 if (DTRACEACT_ISAGG(act->dta_kind)) { 8295 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 8296 dtrace_action_t *first = agg->dtag_first, *prev; 8297 8298 ASSERT(rec->dtrd_size != 0 && first != NULL); 8299 ASSERT(wastuple); 8300 ASSERT(aggbase != UINT32_MAX); 8301 8302 agg->dtag_base = aggbase; 8303 8304 while ((prev = first->dta_prev) != NULL && 8305 DTRACEACT_ISAGG(prev->dta_kind)) { 8306 agg = (dtrace_aggregation_t *)prev; 8307 first = agg->dtag_first; 8308 } 8309 8310 if (prev != NULL) { 8311 offs = prev->dta_rec.dtrd_offset + 8312 prev->dta_rec.dtrd_size; 8313 } else { 8314 offs = sizeof (dtrace_epid_t); 8315 } 8316 wastuple = 0; 8317 } else { 8318 if (!act->dta_intuple) 8319 ecb->dte_size = offs + rec->dtrd_size; 8320 8321 offs += rec->dtrd_size; 8322 } 8323 8324 wastuple = act->dta_intuple; 8325 } 8326 8327 if ((act = ecb->dte_action) != NULL && 8328 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 8329 ecb->dte_size == sizeof (dtrace_epid_t)) { 8330 /* 8331 * If the size is still sizeof (dtrace_epid_t), then all 8332 * actions store no data; set the size to 0. 8333 */ 8334 ecb->dte_alignment = maxalign; 8335 ecb->dte_size = 0; 8336 8337 /* 8338 * If the needed space is still sizeof (dtrace_epid_t), then 8339 * all actions need no additional space; set the needed 8340 * size to 0. 8341 */ 8342 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 8343 ecb->dte_needed = 0; 8344 8345 return; 8346 } 8347 8348 /* 8349 * Set our alignment, and make sure that the dte_size and dte_needed 8350 * are aligned to the size of an EPID. 8351 */ 8352 ecb->dte_alignment = maxalign; 8353 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 8354 ~(sizeof (dtrace_epid_t) - 1); 8355 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 8356 ~(sizeof (dtrace_epid_t) - 1); 8357 ASSERT(ecb->dte_size <= ecb->dte_needed); 8358 } 8359 8360 static dtrace_action_t * 8361 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 8362 { 8363 dtrace_aggregation_t *agg; 8364 size_t size = sizeof (uint64_t); 8365 int ntuple = desc->dtad_ntuple; 8366 dtrace_action_t *act; 8367 dtrace_recdesc_t *frec; 8368 dtrace_aggid_t aggid; 8369 dtrace_state_t *state = ecb->dte_state; 8370 8371 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 8372 agg->dtag_ecb = ecb; 8373 8374 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 8375 8376 switch (desc->dtad_kind) { 8377 case DTRACEAGG_MIN: 8378 agg->dtag_initial = UINT64_MAX; 8379 agg->dtag_aggregate = dtrace_aggregate_min; 8380 break; 8381 8382 case DTRACEAGG_MAX: 8383 agg->dtag_aggregate = dtrace_aggregate_max; 8384 break; 8385 8386 case DTRACEAGG_COUNT: 8387 agg->dtag_aggregate = dtrace_aggregate_count; 8388 break; 8389 8390 case DTRACEAGG_QUANTIZE: 8391 agg->dtag_aggregate = dtrace_aggregate_quantize; 8392 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 8393 sizeof (uint64_t); 8394 break; 8395 8396 case DTRACEAGG_LQUANTIZE: { 8397 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 8398 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 8399 8400 agg->dtag_initial = desc->dtad_arg; 8401 agg->dtag_aggregate = dtrace_aggregate_lquantize; 8402 8403 if (step == 0 || levels == 0) 8404 goto err; 8405 8406 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 8407 break; 8408 } 8409 8410 case DTRACEAGG_AVG: 8411 agg->dtag_aggregate = dtrace_aggregate_avg; 8412 size = sizeof (uint64_t) * 2; 8413 break; 8414 8415 case DTRACEAGG_SUM: 8416 agg->dtag_aggregate = dtrace_aggregate_sum; 8417 break; 8418 8419 default: 8420 goto err; 8421 } 8422 8423 agg->dtag_action.dta_rec.dtrd_size = size; 8424 8425 if (ntuple == 0) 8426 goto err; 8427 8428 /* 8429 * We must make sure that we have enough actions for the n-tuple. 8430 */ 8431 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 8432 if (DTRACEACT_ISAGG(act->dta_kind)) 8433 break; 8434 8435 if (--ntuple == 0) { 8436 /* 8437 * This is the action with which our n-tuple begins. 8438 */ 8439 agg->dtag_first = act; 8440 goto success; 8441 } 8442 } 8443 8444 /* 8445 * This n-tuple is short by ntuple elements. Return failure. 8446 */ 8447 ASSERT(ntuple != 0); 8448 err: 8449 kmem_free(agg, sizeof (dtrace_aggregation_t)); 8450 return (NULL); 8451 8452 success: 8453 /* 8454 * If the last action in the tuple has a size of zero, it's actually 8455 * an expression argument for the aggregating action. 8456 */ 8457 ASSERT(ecb->dte_action_last != NULL); 8458 act = ecb->dte_action_last; 8459 8460 if (act->dta_kind == DTRACEACT_DIFEXPR) { 8461 ASSERT(act->dta_difo != NULL); 8462 8463 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 8464 agg->dtag_hasarg = 1; 8465 } 8466 8467 /* 8468 * We need to allocate an id for this aggregation. 8469 */ 8470 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 8471 VM_BESTFIT | VM_SLEEP); 8472 8473 if (aggid - 1 >= state->dts_naggregations) { 8474 dtrace_aggregation_t **oaggs = state->dts_aggregations; 8475 dtrace_aggregation_t **aggs; 8476 int naggs = state->dts_naggregations << 1; 8477 int onaggs = state->dts_naggregations; 8478 8479 ASSERT(aggid == state->dts_naggregations + 1); 8480 8481 if (naggs == 0) { 8482 ASSERT(oaggs == NULL); 8483 naggs = 1; 8484 } 8485 8486 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 8487 8488 if (oaggs != NULL) { 8489 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 8490 kmem_free(oaggs, onaggs * sizeof (*aggs)); 8491 } 8492 8493 state->dts_aggregations = aggs; 8494 state->dts_naggregations = naggs; 8495 } 8496 8497 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 8498 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 8499 8500 frec = &agg->dtag_first->dta_rec; 8501 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 8502 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 8503 8504 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 8505 ASSERT(!act->dta_intuple); 8506 act->dta_intuple = 1; 8507 } 8508 8509 return (&agg->dtag_action); 8510 } 8511 8512 static void 8513 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 8514 { 8515 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 8516 dtrace_state_t *state = ecb->dte_state; 8517 dtrace_aggid_t aggid = agg->dtag_id; 8518 8519 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 8520 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 8521 8522 ASSERT(state->dts_aggregations[aggid - 1] == agg); 8523 state->dts_aggregations[aggid - 1] = NULL; 8524 8525 kmem_free(agg, sizeof (dtrace_aggregation_t)); 8526 } 8527 8528 static int 8529 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 8530 { 8531 dtrace_action_t *action, *last; 8532 dtrace_difo_t *dp = desc->dtad_difo; 8533 uint32_t size = 0, align = sizeof (uint8_t), mask; 8534 uint16_t format = 0; 8535 dtrace_recdesc_t *rec; 8536 dtrace_state_t *state = ecb->dte_state; 8537 dtrace_optval_t *opt = state->dts_options, nframes, strsize; 8538 uint64_t arg = desc->dtad_arg; 8539 8540 ASSERT(MUTEX_HELD(&dtrace_lock)); 8541 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 8542 8543 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 8544 /* 8545 * If this is an aggregating action, there must be neither 8546 * a speculate nor a commit on the action chain. 8547 */ 8548 dtrace_action_t *act; 8549 8550 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 8551 if (act->dta_kind == DTRACEACT_COMMIT) 8552 return (EINVAL); 8553 8554 if (act->dta_kind == DTRACEACT_SPECULATE) 8555 return (EINVAL); 8556 } 8557 8558 action = dtrace_ecb_aggregation_create(ecb, desc); 8559 8560 if (action == NULL) 8561 return (EINVAL); 8562 } else { 8563 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 8564 (desc->dtad_kind == DTRACEACT_DIFEXPR && 8565 dp != NULL && dp->dtdo_destructive)) { 8566 state->dts_destructive = 1; 8567 } 8568 8569 switch (desc->dtad_kind) { 8570 case DTRACEACT_PRINTF: 8571 case DTRACEACT_PRINTA: 8572 case DTRACEACT_SYSTEM: 8573 case DTRACEACT_FREOPEN: 8574 /* 8575 * We know that our arg is a string -- turn it into a 8576 * format. 8577 */ 8578 if (arg == NULL) { 8579 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); 8580 format = 0; 8581 } else { 8582 ASSERT(arg != NULL); 8583 ASSERT(arg > KERNELBASE); 8584 format = dtrace_format_add(state, 8585 (char *)(uintptr_t)arg); 8586 } 8587 8588 /*FALLTHROUGH*/ 8589 case DTRACEACT_LIBACT: 8590 case DTRACEACT_DIFEXPR: 8591 if (dp == NULL) 8592 return (EINVAL); 8593 8594 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 8595 break; 8596 8597 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 8598 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8599 return (EINVAL); 8600 8601 size = opt[DTRACEOPT_STRSIZE]; 8602 } 8603 8604 break; 8605 8606 case DTRACEACT_STACK: 8607 if ((nframes = arg) == 0) { 8608 nframes = opt[DTRACEOPT_STACKFRAMES]; 8609 ASSERT(nframes > 0); 8610 arg = nframes; 8611 } 8612 8613 size = nframes * sizeof (pc_t); 8614 break; 8615 8616 case DTRACEACT_JSTACK: 8617 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 8618 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 8619 8620 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 8621 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 8622 8623 arg = DTRACE_USTACK_ARG(nframes, strsize); 8624 8625 /*FALLTHROUGH*/ 8626 case DTRACEACT_USTACK: 8627 if (desc->dtad_kind != DTRACEACT_JSTACK && 8628 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 8629 strsize = DTRACE_USTACK_STRSIZE(arg); 8630 nframes = opt[DTRACEOPT_USTACKFRAMES]; 8631 ASSERT(nframes > 0); 8632 arg = DTRACE_USTACK_ARG(nframes, strsize); 8633 } 8634 8635 /* 8636 * Save a slot for the pid. 8637 */ 8638 size = (nframes + 1) * sizeof (uint64_t); 8639 size += DTRACE_USTACK_STRSIZE(arg); 8640 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 8641 8642 break; 8643 8644 case DTRACEACT_SYM: 8645 case DTRACEACT_MOD: 8646 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 8647 sizeof (uint64_t)) || 8648 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8649 return (EINVAL); 8650 break; 8651 8652 case DTRACEACT_USYM: 8653 case DTRACEACT_UMOD: 8654 case DTRACEACT_UADDR: 8655 if (dp == NULL || 8656 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 8657 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8658 return (EINVAL); 8659 8660 /* 8661 * We have a slot for the pid, plus a slot for the 8662 * argument. To keep things simple (aligned with 8663 * bitness-neutral sizing), we store each as a 64-bit 8664 * quantity. 8665 */ 8666 size = 2 * sizeof (uint64_t); 8667 break; 8668 8669 case DTRACEACT_STOP: 8670 case DTRACEACT_BREAKPOINT: 8671 case DTRACEACT_PANIC: 8672 break; 8673 8674 case DTRACEACT_CHILL: 8675 case DTRACEACT_DISCARD: 8676 case DTRACEACT_RAISE: 8677 if (dp == NULL) 8678 return (EINVAL); 8679 break; 8680 8681 case DTRACEACT_EXIT: 8682 if (dp == NULL || 8683 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 8684 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8685 return (EINVAL); 8686 break; 8687 8688 case DTRACEACT_SPECULATE: 8689 if (ecb->dte_size > sizeof (dtrace_epid_t)) 8690 return (EINVAL); 8691 8692 if (dp == NULL) 8693 return (EINVAL); 8694 8695 state->dts_speculates = 1; 8696 break; 8697 8698 case DTRACEACT_COMMIT: { 8699 dtrace_action_t *act = ecb->dte_action; 8700 8701 for (; act != NULL; act = act->dta_next) { 8702 if (act->dta_kind == DTRACEACT_COMMIT) 8703 return (EINVAL); 8704 } 8705 8706 if (dp == NULL) 8707 return (EINVAL); 8708 break; 8709 } 8710 8711 default: 8712 return (EINVAL); 8713 } 8714 8715 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 8716 /* 8717 * If this is a data-storing action or a speculate, 8718 * we must be sure that there isn't a commit on the 8719 * action chain. 8720 */ 8721 dtrace_action_t *act = ecb->dte_action; 8722 8723 for (; act != NULL; act = act->dta_next) { 8724 if (act->dta_kind == DTRACEACT_COMMIT) 8725 return (EINVAL); 8726 } 8727 } 8728 8729 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 8730 action->dta_rec.dtrd_size = size; 8731 } 8732 8733 action->dta_refcnt = 1; 8734 rec = &action->dta_rec; 8735 size = rec->dtrd_size; 8736 8737 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 8738 if (!(size & mask)) { 8739 align = mask + 1; 8740 break; 8741 } 8742 } 8743 8744 action->dta_kind = desc->dtad_kind; 8745 8746 if ((action->dta_difo = dp) != NULL) 8747 dtrace_difo_hold(dp); 8748 8749 rec->dtrd_action = action->dta_kind; 8750 rec->dtrd_arg = arg; 8751 rec->dtrd_uarg = desc->dtad_uarg; 8752 rec->dtrd_alignment = (uint16_t)align; 8753 rec->dtrd_format = format; 8754 8755 if ((last = ecb->dte_action_last) != NULL) { 8756 ASSERT(ecb->dte_action != NULL); 8757 action->dta_prev = last; 8758 last->dta_next = action; 8759 } else { 8760 ASSERT(ecb->dte_action == NULL); 8761 ecb->dte_action = action; 8762 } 8763 8764 ecb->dte_action_last = action; 8765 8766 return (0); 8767 } 8768 8769 static void 8770 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 8771 { 8772 dtrace_action_t *act = ecb->dte_action, *next; 8773 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 8774 dtrace_difo_t *dp; 8775 uint16_t format; 8776 8777 if (act != NULL && act->dta_refcnt > 1) { 8778 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 8779 act->dta_refcnt--; 8780 } else { 8781 for (; act != NULL; act = next) { 8782 next = act->dta_next; 8783 ASSERT(next != NULL || act == ecb->dte_action_last); 8784 ASSERT(act->dta_refcnt == 1); 8785 8786 if ((format = act->dta_rec.dtrd_format) != 0) 8787 dtrace_format_remove(ecb->dte_state, format); 8788 8789 if ((dp = act->dta_difo) != NULL) 8790 dtrace_difo_release(dp, vstate); 8791 8792 if (DTRACEACT_ISAGG(act->dta_kind)) { 8793 dtrace_ecb_aggregation_destroy(ecb, act); 8794 } else { 8795 kmem_free(act, sizeof (dtrace_action_t)); 8796 } 8797 } 8798 } 8799 8800 ecb->dte_action = NULL; 8801 ecb->dte_action_last = NULL; 8802 ecb->dte_size = sizeof (dtrace_epid_t); 8803 } 8804 8805 static void 8806 dtrace_ecb_disable(dtrace_ecb_t *ecb) 8807 { 8808 /* 8809 * We disable the ECB by removing it from its probe. 8810 */ 8811 dtrace_ecb_t *pecb, *prev = NULL; 8812 dtrace_probe_t *probe = ecb->dte_probe; 8813 8814 ASSERT(MUTEX_HELD(&dtrace_lock)); 8815 8816 if (probe == NULL) { 8817 /* 8818 * This is the NULL probe; there is nothing to disable. 8819 */ 8820 return; 8821 } 8822 8823 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 8824 if (pecb == ecb) 8825 break; 8826 prev = pecb; 8827 } 8828 8829 ASSERT(pecb != NULL); 8830 8831 if (prev == NULL) { 8832 probe->dtpr_ecb = ecb->dte_next; 8833 } else { 8834 prev->dte_next = ecb->dte_next; 8835 } 8836 8837 if (ecb == probe->dtpr_ecb_last) { 8838 ASSERT(ecb->dte_next == NULL); 8839 probe->dtpr_ecb_last = prev; 8840 } 8841 8842 /* 8843 * The ECB has been disconnected from the probe; now sync to assure 8844 * that all CPUs have seen the change before returning. 8845 */ 8846 dtrace_sync(); 8847 8848 if (probe->dtpr_ecb == NULL) { 8849 /* 8850 * That was the last ECB on the probe; clear the predicate 8851 * cache ID for the probe, disable it and sync one more time 8852 * to assure that we'll never hit it again. 8853 */ 8854 dtrace_provider_t *prov = probe->dtpr_provider; 8855 8856 ASSERT(ecb->dte_next == NULL); 8857 ASSERT(probe->dtpr_ecb_last == NULL); 8858 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 8859 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 8860 probe->dtpr_id, probe->dtpr_arg); 8861 dtrace_sync(); 8862 } else { 8863 /* 8864 * There is at least one ECB remaining on the probe. If there 8865 * is _exactly_ one, set the probe's predicate cache ID to be 8866 * the predicate cache ID of the remaining ECB. 8867 */ 8868 ASSERT(probe->dtpr_ecb_last != NULL); 8869 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 8870 8871 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 8872 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 8873 8874 ASSERT(probe->dtpr_ecb->dte_next == NULL); 8875 8876 if (p != NULL) 8877 probe->dtpr_predcache = p->dtp_cacheid; 8878 } 8879 8880 ecb->dte_next = NULL; 8881 } 8882 } 8883 8884 static void 8885 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 8886 { 8887 dtrace_state_t *state = ecb->dte_state; 8888 dtrace_vstate_t *vstate = &state->dts_vstate; 8889 dtrace_predicate_t *pred; 8890 dtrace_epid_t epid = ecb->dte_epid; 8891 8892 ASSERT(MUTEX_HELD(&dtrace_lock)); 8893 ASSERT(ecb->dte_next == NULL); 8894 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 8895 8896 if ((pred = ecb->dte_predicate) != NULL) 8897 dtrace_predicate_release(pred, vstate); 8898 8899 dtrace_ecb_action_remove(ecb); 8900 8901 ASSERT(state->dts_ecbs[epid - 1] == ecb); 8902 state->dts_ecbs[epid - 1] = NULL; 8903 8904 kmem_free(ecb, sizeof (dtrace_ecb_t)); 8905 } 8906 8907 static dtrace_ecb_t * 8908 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 8909 dtrace_enabling_t *enab) 8910 { 8911 dtrace_ecb_t *ecb; 8912 dtrace_predicate_t *pred; 8913 dtrace_actdesc_t *act; 8914 dtrace_provider_t *prov; 8915 dtrace_ecbdesc_t *desc = enab->dten_current; 8916 8917 ASSERT(MUTEX_HELD(&dtrace_lock)); 8918 ASSERT(state != NULL); 8919 8920 ecb = dtrace_ecb_add(state, probe); 8921 ecb->dte_uarg = desc->dted_uarg; 8922 8923 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 8924 dtrace_predicate_hold(pred); 8925 ecb->dte_predicate = pred; 8926 } 8927 8928 if (probe != NULL) { 8929 /* 8930 * If the provider shows more leg than the consumer is old 8931 * enough to see, we need to enable the appropriate implicit 8932 * predicate bits to prevent the ecb from activating at 8933 * revealing times. 8934 * 8935 * Providers specifying DTRACE_PRIV_USER at register time 8936 * are stating that they need the /proc-style privilege 8937 * model to be enforced, and this is what DTRACE_COND_OWNER 8938 * and DTRACE_COND_ZONEOWNER will then do at probe time. 8939 */ 8940 prov = probe->dtpr_provider; 8941 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 8942 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 8943 ecb->dte_cond |= DTRACE_COND_OWNER; 8944 8945 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 8946 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 8947 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 8948 8949 /* 8950 * If the provider shows us kernel innards and the user 8951 * is lacking sufficient privilege, enable the 8952 * DTRACE_COND_USERMODE implicit predicate. 8953 */ 8954 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 8955 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 8956 ecb->dte_cond |= DTRACE_COND_USERMODE; 8957 } 8958 8959 if (dtrace_ecb_create_cache != NULL) { 8960 /* 8961 * If we have a cached ecb, we'll use its action list instead 8962 * of creating our own (saving both time and space). 8963 */ 8964 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 8965 dtrace_action_t *act = cached->dte_action; 8966 8967 if (act != NULL) { 8968 ASSERT(act->dta_refcnt > 0); 8969 act->dta_refcnt++; 8970 ecb->dte_action = act; 8971 ecb->dte_action_last = cached->dte_action_last; 8972 ecb->dte_needed = cached->dte_needed; 8973 ecb->dte_size = cached->dte_size; 8974 ecb->dte_alignment = cached->dte_alignment; 8975 } 8976 8977 return (ecb); 8978 } 8979 8980 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 8981 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 8982 dtrace_ecb_destroy(ecb); 8983 return (NULL); 8984 } 8985 } 8986 8987 dtrace_ecb_resize(ecb); 8988 8989 return (dtrace_ecb_create_cache = ecb); 8990 } 8991 8992 static int 8993 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 8994 { 8995 dtrace_ecb_t *ecb; 8996 dtrace_enabling_t *enab = arg; 8997 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 8998 8999 ASSERT(state != NULL); 9000 9001 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 9002 /* 9003 * This probe was created in a generation for which this 9004 * enabling has previously created ECBs; we don't want to 9005 * enable it again, so just kick out. 9006 */ 9007 return (DTRACE_MATCH_NEXT); 9008 } 9009 9010 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 9011 return (DTRACE_MATCH_DONE); 9012 9013 dtrace_ecb_enable(ecb); 9014 return (DTRACE_MATCH_NEXT); 9015 } 9016 9017 static dtrace_ecb_t * 9018 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 9019 { 9020 dtrace_ecb_t *ecb; 9021 9022 ASSERT(MUTEX_HELD(&dtrace_lock)); 9023 9024 if (id == 0 || id > state->dts_necbs) 9025 return (NULL); 9026 9027 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 9028 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 9029 9030 return (state->dts_ecbs[id - 1]); 9031 } 9032 9033 static dtrace_aggregation_t * 9034 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 9035 { 9036 dtrace_aggregation_t *agg; 9037 9038 ASSERT(MUTEX_HELD(&dtrace_lock)); 9039 9040 if (id == 0 || id > state->dts_naggregations) 9041 return (NULL); 9042 9043 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 9044 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 9045 agg->dtag_id == id); 9046 9047 return (state->dts_aggregations[id - 1]); 9048 } 9049 9050 /* 9051 * DTrace Buffer Functions 9052 * 9053 * The following functions manipulate DTrace buffers. Most of these functions 9054 * are called in the context of establishing or processing consumer state; 9055 * exceptions are explicitly noted. 9056 */ 9057 9058 /* 9059 * Note: called from cross call context. This function switches the two 9060 * buffers on a given CPU. The atomicity of this operation is assured by 9061 * disabling interrupts while the actual switch takes place; the disabling of 9062 * interrupts serializes the execution with any execution of dtrace_probe() on 9063 * the same CPU. 9064 */ 9065 static void 9066 dtrace_buffer_switch(dtrace_buffer_t *buf) 9067 { 9068 caddr_t tomax = buf->dtb_tomax; 9069 caddr_t xamot = buf->dtb_xamot; 9070 dtrace_icookie_t cookie; 9071 9072 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 9073 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 9074 9075 cookie = dtrace_interrupt_disable(); 9076 buf->dtb_tomax = xamot; 9077 buf->dtb_xamot = tomax; 9078 buf->dtb_xamot_drops = buf->dtb_drops; 9079 buf->dtb_xamot_offset = buf->dtb_offset; 9080 buf->dtb_xamot_errors = buf->dtb_errors; 9081 buf->dtb_xamot_flags = buf->dtb_flags; 9082 buf->dtb_offset = 0; 9083 buf->dtb_drops = 0; 9084 buf->dtb_errors = 0; 9085 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 9086 dtrace_interrupt_enable(cookie); 9087 } 9088 9089 /* 9090 * Note: called from cross call context. This function activates a buffer 9091 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 9092 * is guaranteed by the disabling of interrupts. 9093 */ 9094 static void 9095 dtrace_buffer_activate(dtrace_state_t *state) 9096 { 9097 dtrace_buffer_t *buf; 9098 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 9099 9100 buf = &state->dts_buffer[CPU->cpu_id]; 9101 9102 if (buf->dtb_tomax != NULL) { 9103 /* 9104 * We might like to assert that the buffer is marked inactive, 9105 * but this isn't necessarily true: the buffer for the CPU 9106 * that processes the BEGIN probe has its buffer activated 9107 * manually. In this case, we take the (harmless) action 9108 * re-clearing the bit INACTIVE bit. 9109 */ 9110 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 9111 } 9112 9113 dtrace_interrupt_enable(cookie); 9114 } 9115 9116 static int 9117 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 9118 processorid_t cpu) 9119 { 9120 cpu_t *cp; 9121 dtrace_buffer_t *buf; 9122 9123 ASSERT(MUTEX_HELD(&cpu_lock)); 9124 ASSERT(MUTEX_HELD(&dtrace_lock)); 9125 9126 if (size > dtrace_nonroot_maxsize && 9127 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 9128 return (EFBIG); 9129 9130 cp = cpu_list; 9131 9132 do { 9133 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 9134 continue; 9135 9136 buf = &bufs[cp->cpu_id]; 9137 9138 /* 9139 * If there is already a buffer allocated for this CPU, it 9140 * is only possible that this is a DR event. In this case, 9141 * the buffer size must match our specified size. 9142 */ 9143 if (buf->dtb_tomax != NULL) { 9144 ASSERT(buf->dtb_size == size); 9145 continue; 9146 } 9147 9148 ASSERT(buf->dtb_xamot == NULL); 9149 9150 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 9151 goto err; 9152 9153 buf->dtb_size = size; 9154 buf->dtb_flags = flags; 9155 buf->dtb_offset = 0; 9156 buf->dtb_drops = 0; 9157 9158 if (flags & DTRACEBUF_NOSWITCH) 9159 continue; 9160 9161 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 9162 goto err; 9163 } while ((cp = cp->cpu_next) != cpu_list); 9164 9165 return (0); 9166 9167 err: 9168 cp = cpu_list; 9169 9170 do { 9171 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 9172 continue; 9173 9174 buf = &bufs[cp->cpu_id]; 9175 9176 if (buf->dtb_xamot != NULL) { 9177 ASSERT(buf->dtb_tomax != NULL); 9178 ASSERT(buf->dtb_size == size); 9179 kmem_free(buf->dtb_xamot, size); 9180 } 9181 9182 if (buf->dtb_tomax != NULL) { 9183 ASSERT(buf->dtb_size == size); 9184 kmem_free(buf->dtb_tomax, size); 9185 } 9186 9187 buf->dtb_tomax = NULL; 9188 buf->dtb_xamot = NULL; 9189 buf->dtb_size = 0; 9190 } while ((cp = cp->cpu_next) != cpu_list); 9191 9192 return (ENOMEM); 9193 } 9194 9195 /* 9196 * Note: called from probe context. This function just increments the drop 9197 * count on a buffer. It has been made a function to allow for the 9198 * possibility of understanding the source of mysterious drop counts. (A 9199 * problem for which one may be particularly disappointed that DTrace cannot 9200 * be used to understand DTrace.) 9201 */ 9202 static void 9203 dtrace_buffer_drop(dtrace_buffer_t *buf) 9204 { 9205 buf->dtb_drops++; 9206 } 9207 9208 /* 9209 * Note: called from probe context. This function is called to reserve space 9210 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 9211 * mstate. Returns the new offset in the buffer, or a negative value if an 9212 * error has occurred. 9213 */ 9214 static intptr_t 9215 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 9216 dtrace_state_t *state, dtrace_mstate_t *mstate) 9217 { 9218 intptr_t offs = buf->dtb_offset, soffs; 9219 intptr_t woffs; 9220 caddr_t tomax; 9221 size_t total; 9222 9223 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 9224 return (-1); 9225 9226 if ((tomax = buf->dtb_tomax) == NULL) { 9227 dtrace_buffer_drop(buf); 9228 return (-1); 9229 } 9230 9231 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 9232 while (offs & (align - 1)) { 9233 /* 9234 * Assert that our alignment is off by a number which 9235 * is itself sizeof (uint32_t) aligned. 9236 */ 9237 ASSERT(!((align - (offs & (align - 1))) & 9238 (sizeof (uint32_t) - 1))); 9239 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 9240 offs += sizeof (uint32_t); 9241 } 9242 9243 if ((soffs = offs + needed) > buf->dtb_size) { 9244 dtrace_buffer_drop(buf); 9245 return (-1); 9246 } 9247 9248 if (mstate == NULL) 9249 return (offs); 9250 9251 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 9252 mstate->dtms_scratch_size = buf->dtb_size - soffs; 9253 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 9254 9255 return (offs); 9256 } 9257 9258 if (buf->dtb_flags & DTRACEBUF_FILL) { 9259 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 9260 (buf->dtb_flags & DTRACEBUF_FULL)) 9261 return (-1); 9262 goto out; 9263 } 9264 9265 total = needed + (offs & (align - 1)); 9266 9267 /* 9268 * For a ring buffer, life is quite a bit more complicated. Before 9269 * we can store any padding, we need to adjust our wrapping offset. 9270 * (If we've never before wrapped or we're not about to, no adjustment 9271 * is required.) 9272 */ 9273 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 9274 offs + total > buf->dtb_size) { 9275 woffs = buf->dtb_xamot_offset; 9276 9277 if (offs + total > buf->dtb_size) { 9278 /* 9279 * We can't fit in the end of the buffer. First, a 9280 * sanity check that we can fit in the buffer at all. 9281 */ 9282 if (total > buf->dtb_size) { 9283 dtrace_buffer_drop(buf); 9284 return (-1); 9285 } 9286 9287 /* 9288 * We're going to be storing at the top of the buffer, 9289 * so now we need to deal with the wrapped offset. We 9290 * only reset our wrapped offset to 0 if it is 9291 * currently greater than the current offset. If it 9292 * is less than the current offset, it is because a 9293 * previous allocation induced a wrap -- but the 9294 * allocation didn't subsequently take the space due 9295 * to an error or false predicate evaluation. In this 9296 * case, we'll just leave the wrapped offset alone: if 9297 * the wrapped offset hasn't been advanced far enough 9298 * for this allocation, it will be adjusted in the 9299 * lower loop. 9300 */ 9301 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 9302 if (woffs >= offs) 9303 woffs = 0; 9304 } else { 9305 woffs = 0; 9306 } 9307 9308 /* 9309 * Now we know that we're going to be storing to the 9310 * top of the buffer and that there is room for us 9311 * there. We need to clear the buffer from the current 9312 * offset to the end (there may be old gunk there). 9313 */ 9314 while (offs < buf->dtb_size) 9315 tomax[offs++] = 0; 9316 9317 /* 9318 * We need to set our offset to zero. And because we 9319 * are wrapping, we need to set the bit indicating as 9320 * much. We can also adjust our needed space back 9321 * down to the space required by the ECB -- we know 9322 * that the top of the buffer is aligned. 9323 */ 9324 offs = 0; 9325 total = needed; 9326 buf->dtb_flags |= DTRACEBUF_WRAPPED; 9327 } else { 9328 /* 9329 * There is room for us in the buffer, so we simply 9330 * need to check the wrapped offset. 9331 */ 9332 if (woffs < offs) { 9333 /* 9334 * The wrapped offset is less than the offset. 9335 * This can happen if we allocated buffer space 9336 * that induced a wrap, but then we didn't 9337 * subsequently take the space due to an error 9338 * or false predicate evaluation. This is 9339 * okay; we know that _this_ allocation isn't 9340 * going to induce a wrap. We still can't 9341 * reset the wrapped offset to be zero, 9342 * however: the space may have been trashed in 9343 * the previous failed probe attempt. But at 9344 * least the wrapped offset doesn't need to 9345 * be adjusted at all... 9346 */ 9347 goto out; 9348 } 9349 } 9350 9351 while (offs + total > woffs) { 9352 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 9353 size_t size; 9354 9355 if (epid == DTRACE_EPIDNONE) { 9356 size = sizeof (uint32_t); 9357 } else { 9358 ASSERT(epid <= state->dts_necbs); 9359 ASSERT(state->dts_ecbs[epid - 1] != NULL); 9360 9361 size = state->dts_ecbs[epid - 1]->dte_size; 9362 } 9363 9364 ASSERT(woffs + size <= buf->dtb_size); 9365 ASSERT(size != 0); 9366 9367 if (woffs + size == buf->dtb_size) { 9368 /* 9369 * We've reached the end of the buffer; we want 9370 * to set the wrapped offset to 0 and break 9371 * out. However, if the offs is 0, then we're 9372 * in a strange edge-condition: the amount of 9373 * space that we want to reserve plus the size 9374 * of the record that we're overwriting is 9375 * greater than the size of the buffer. This 9376 * is problematic because if we reserve the 9377 * space but subsequently don't consume it (due 9378 * to a failed predicate or error) the wrapped 9379 * offset will be 0 -- yet the EPID at offset 0 9380 * will not be committed. This situation is 9381 * relatively easy to deal with: if we're in 9382 * this case, the buffer is indistinguishable 9383 * from one that hasn't wrapped; we need only 9384 * finish the job by clearing the wrapped bit, 9385 * explicitly setting the offset to be 0, and 9386 * zero'ing out the old data in the buffer. 9387 */ 9388 if (offs == 0) { 9389 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 9390 buf->dtb_offset = 0; 9391 woffs = total; 9392 9393 while (woffs < buf->dtb_size) 9394 tomax[woffs++] = 0; 9395 } 9396 9397 woffs = 0; 9398 break; 9399 } 9400 9401 woffs += size; 9402 } 9403 9404 /* 9405 * We have a wrapped offset. It may be that the wrapped offset 9406 * has become zero -- that's okay. 9407 */ 9408 buf->dtb_xamot_offset = woffs; 9409 } 9410 9411 out: 9412 /* 9413 * Now we can plow the buffer with any necessary padding. 9414 */ 9415 while (offs & (align - 1)) { 9416 /* 9417 * Assert that our alignment is off by a number which 9418 * is itself sizeof (uint32_t) aligned. 9419 */ 9420 ASSERT(!((align - (offs & (align - 1))) & 9421 (sizeof (uint32_t) - 1))); 9422 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 9423 offs += sizeof (uint32_t); 9424 } 9425 9426 if (buf->dtb_flags & DTRACEBUF_FILL) { 9427 if (offs + needed > buf->dtb_size - state->dts_reserve) { 9428 buf->dtb_flags |= DTRACEBUF_FULL; 9429 return (-1); 9430 } 9431 } 9432 9433 if (mstate == NULL) 9434 return (offs); 9435 9436 /* 9437 * For ring buffers and fill buffers, the scratch space is always 9438 * the inactive buffer. 9439 */ 9440 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 9441 mstate->dtms_scratch_size = buf->dtb_size; 9442 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 9443 9444 return (offs); 9445 } 9446 9447 static void 9448 dtrace_buffer_polish(dtrace_buffer_t *buf) 9449 { 9450 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 9451 ASSERT(MUTEX_HELD(&dtrace_lock)); 9452 9453 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 9454 return; 9455 9456 /* 9457 * We need to polish the ring buffer. There are three cases: 9458 * 9459 * - The first (and presumably most common) is that there is no gap 9460 * between the buffer offset and the wrapped offset. In this case, 9461 * there is nothing in the buffer that isn't valid data; we can 9462 * mark the buffer as polished and return. 9463 * 9464 * - The second (less common than the first but still more common 9465 * than the third) is that there is a gap between the buffer offset 9466 * and the wrapped offset, and the wrapped offset is larger than the 9467 * buffer offset. This can happen because of an alignment issue, or 9468 * can happen because of a call to dtrace_buffer_reserve() that 9469 * didn't subsequently consume the buffer space. In this case, 9470 * we need to zero the data from the buffer offset to the wrapped 9471 * offset. 9472 * 9473 * - The third (and least common) is that there is a gap between the 9474 * buffer offset and the wrapped offset, but the wrapped offset is 9475 * _less_ than the buffer offset. This can only happen because a 9476 * call to dtrace_buffer_reserve() induced a wrap, but the space 9477 * was not subsequently consumed. In this case, we need to zero the 9478 * space from the offset to the end of the buffer _and_ from the 9479 * top of the buffer to the wrapped offset. 9480 */ 9481 if (buf->dtb_offset < buf->dtb_xamot_offset) { 9482 bzero(buf->dtb_tomax + buf->dtb_offset, 9483 buf->dtb_xamot_offset - buf->dtb_offset); 9484 } 9485 9486 if (buf->dtb_offset > buf->dtb_xamot_offset) { 9487 bzero(buf->dtb_tomax + buf->dtb_offset, 9488 buf->dtb_size - buf->dtb_offset); 9489 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 9490 } 9491 } 9492 9493 static void 9494 dtrace_buffer_free(dtrace_buffer_t *bufs) 9495 { 9496 int i; 9497 9498 for (i = 0; i < NCPU; i++) { 9499 dtrace_buffer_t *buf = &bufs[i]; 9500 9501 if (buf->dtb_tomax == NULL) { 9502 ASSERT(buf->dtb_xamot == NULL); 9503 ASSERT(buf->dtb_size == 0); 9504 continue; 9505 } 9506 9507 if (buf->dtb_xamot != NULL) { 9508 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 9509 kmem_free(buf->dtb_xamot, buf->dtb_size); 9510 } 9511 9512 kmem_free(buf->dtb_tomax, buf->dtb_size); 9513 buf->dtb_size = 0; 9514 buf->dtb_tomax = NULL; 9515 buf->dtb_xamot = NULL; 9516 } 9517 } 9518 9519 /* 9520 * DTrace Enabling Functions 9521 */ 9522 static dtrace_enabling_t * 9523 dtrace_enabling_create(dtrace_vstate_t *vstate) 9524 { 9525 dtrace_enabling_t *enab; 9526 9527 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 9528 enab->dten_vstate = vstate; 9529 9530 return (enab); 9531 } 9532 9533 static void 9534 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 9535 { 9536 dtrace_ecbdesc_t **ndesc; 9537 size_t osize, nsize; 9538 9539 /* 9540 * We can't add to enablings after we've enabled them, or after we've 9541 * retained them. 9542 */ 9543 ASSERT(enab->dten_probegen == 0); 9544 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 9545 9546 if (enab->dten_ndesc < enab->dten_maxdesc) { 9547 enab->dten_desc[enab->dten_ndesc++] = ecb; 9548 return; 9549 } 9550 9551 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 9552 9553 if (enab->dten_maxdesc == 0) { 9554 enab->dten_maxdesc = 1; 9555 } else { 9556 enab->dten_maxdesc <<= 1; 9557 } 9558 9559 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 9560 9561 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 9562 ndesc = kmem_zalloc(nsize, KM_SLEEP); 9563 bcopy(enab->dten_desc, ndesc, osize); 9564 kmem_free(enab->dten_desc, osize); 9565 9566 enab->dten_desc = ndesc; 9567 enab->dten_desc[enab->dten_ndesc++] = ecb; 9568 } 9569 9570 static void 9571 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 9572 dtrace_probedesc_t *pd) 9573 { 9574 dtrace_ecbdesc_t *new; 9575 dtrace_predicate_t *pred; 9576 dtrace_actdesc_t *act; 9577 9578 /* 9579 * We're going to create a new ECB description that matches the 9580 * specified ECB in every way, but has the specified probe description. 9581 */ 9582 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 9583 9584 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 9585 dtrace_predicate_hold(pred); 9586 9587 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 9588 dtrace_actdesc_hold(act); 9589 9590 new->dted_action = ecb->dted_action; 9591 new->dted_pred = ecb->dted_pred; 9592 new->dted_probe = *pd; 9593 new->dted_uarg = ecb->dted_uarg; 9594 9595 dtrace_enabling_add(enab, new); 9596 } 9597 9598 static void 9599 dtrace_enabling_dump(dtrace_enabling_t *enab) 9600 { 9601 int i; 9602 9603 for (i = 0; i < enab->dten_ndesc; i++) { 9604 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 9605 9606 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 9607 desc->dtpd_provider, desc->dtpd_mod, 9608 desc->dtpd_func, desc->dtpd_name); 9609 } 9610 } 9611 9612 static void 9613 dtrace_enabling_destroy(dtrace_enabling_t *enab) 9614 { 9615 int i; 9616 dtrace_ecbdesc_t *ep; 9617 dtrace_vstate_t *vstate = enab->dten_vstate; 9618 9619 ASSERT(MUTEX_HELD(&dtrace_lock)); 9620 9621 for (i = 0; i < enab->dten_ndesc; i++) { 9622 dtrace_actdesc_t *act, *next; 9623 dtrace_predicate_t *pred; 9624 9625 ep = enab->dten_desc[i]; 9626 9627 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 9628 dtrace_predicate_release(pred, vstate); 9629 9630 for (act = ep->dted_action; act != NULL; act = next) { 9631 next = act->dtad_next; 9632 dtrace_actdesc_release(act, vstate); 9633 } 9634 9635 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 9636 } 9637 9638 kmem_free(enab->dten_desc, 9639 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 9640 9641 /* 9642 * If this was a retained enabling, decrement the dts_nretained count 9643 * and take it off of the dtrace_retained list. 9644 */ 9645 if (enab->dten_prev != NULL || enab->dten_next != NULL || 9646 dtrace_retained == enab) { 9647 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9648 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 9649 enab->dten_vstate->dtvs_state->dts_nretained--; 9650 } 9651 9652 if (enab->dten_prev == NULL) { 9653 if (dtrace_retained == enab) { 9654 dtrace_retained = enab->dten_next; 9655 9656 if (dtrace_retained != NULL) 9657 dtrace_retained->dten_prev = NULL; 9658 } 9659 } else { 9660 ASSERT(enab != dtrace_retained); 9661 ASSERT(dtrace_retained != NULL); 9662 enab->dten_prev->dten_next = enab->dten_next; 9663 } 9664 9665 if (enab->dten_next != NULL) { 9666 ASSERT(dtrace_retained != NULL); 9667 enab->dten_next->dten_prev = enab->dten_prev; 9668 } 9669 9670 kmem_free(enab, sizeof (dtrace_enabling_t)); 9671 } 9672 9673 static int 9674 dtrace_enabling_retain(dtrace_enabling_t *enab) 9675 { 9676 dtrace_state_t *state; 9677 9678 ASSERT(MUTEX_HELD(&dtrace_lock)); 9679 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 9680 ASSERT(enab->dten_vstate != NULL); 9681 9682 state = enab->dten_vstate->dtvs_state; 9683 ASSERT(state != NULL); 9684 9685 /* 9686 * We only allow each state to retain dtrace_retain_max enablings. 9687 */ 9688 if (state->dts_nretained >= dtrace_retain_max) 9689 return (ENOSPC); 9690 9691 state->dts_nretained++; 9692 9693 if (dtrace_retained == NULL) { 9694 dtrace_retained = enab; 9695 return (0); 9696 } 9697 9698 enab->dten_next = dtrace_retained; 9699 dtrace_retained->dten_prev = enab; 9700 dtrace_retained = enab; 9701 9702 return (0); 9703 } 9704 9705 static int 9706 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 9707 dtrace_probedesc_t *create) 9708 { 9709 dtrace_enabling_t *new, *enab; 9710 int found = 0, err = ENOENT; 9711 9712 ASSERT(MUTEX_HELD(&dtrace_lock)); 9713 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 9714 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 9715 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 9716 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 9717 9718 new = dtrace_enabling_create(&state->dts_vstate); 9719 9720 /* 9721 * Iterate over all retained enablings, looking for enablings that 9722 * match the specified state. 9723 */ 9724 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9725 int i; 9726 9727 /* 9728 * dtvs_state can only be NULL for helper enablings -- and 9729 * helper enablings can't be retained. 9730 */ 9731 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9732 9733 if (enab->dten_vstate->dtvs_state != state) 9734 continue; 9735 9736 /* 9737 * Now iterate over each probe description; we're looking for 9738 * an exact match to the specified probe description. 9739 */ 9740 for (i = 0; i < enab->dten_ndesc; i++) { 9741 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 9742 dtrace_probedesc_t *pd = &ep->dted_probe; 9743 9744 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 9745 continue; 9746 9747 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 9748 continue; 9749 9750 if (strcmp(pd->dtpd_func, match->dtpd_func)) 9751 continue; 9752 9753 if (strcmp(pd->dtpd_name, match->dtpd_name)) 9754 continue; 9755 9756 /* 9757 * We have a winning probe! Add it to our growing 9758 * enabling. 9759 */ 9760 found = 1; 9761 dtrace_enabling_addlike(new, ep, create); 9762 } 9763 } 9764 9765 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 9766 dtrace_enabling_destroy(new); 9767 return (err); 9768 } 9769 9770 return (0); 9771 } 9772 9773 static void 9774 dtrace_enabling_retract(dtrace_state_t *state) 9775 { 9776 dtrace_enabling_t *enab, *next; 9777 9778 ASSERT(MUTEX_HELD(&dtrace_lock)); 9779 9780 /* 9781 * Iterate over all retained enablings, destroy the enablings retained 9782 * for the specified state. 9783 */ 9784 for (enab = dtrace_retained; enab != NULL; enab = next) { 9785 next = enab->dten_next; 9786 9787 /* 9788 * dtvs_state can only be NULL for helper enablings -- and 9789 * helper enablings can't be retained. 9790 */ 9791 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9792 9793 if (enab->dten_vstate->dtvs_state == state) { 9794 ASSERT(state->dts_nretained > 0); 9795 dtrace_enabling_destroy(enab); 9796 } 9797 } 9798 9799 ASSERT(state->dts_nretained == 0); 9800 } 9801 9802 static int 9803 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 9804 { 9805 int i = 0; 9806 int matched = 0; 9807 9808 ASSERT(MUTEX_HELD(&cpu_lock)); 9809 ASSERT(MUTEX_HELD(&dtrace_lock)); 9810 9811 for (i = 0; i < enab->dten_ndesc; i++) { 9812 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 9813 9814 enab->dten_current = ep; 9815 enab->dten_error = 0; 9816 9817 matched += dtrace_probe_enable(&ep->dted_probe, enab); 9818 9819 if (enab->dten_error != 0) { 9820 /* 9821 * If we get an error half-way through enabling the 9822 * probes, we kick out -- perhaps with some number of 9823 * them enabled. Leaving enabled probes enabled may 9824 * be slightly confusing for user-level, but we expect 9825 * that no one will attempt to actually drive on in 9826 * the face of such errors. If this is an anonymous 9827 * enabling (indicated with a NULL nmatched pointer), 9828 * we cmn_err() a message. We aren't expecting to 9829 * get such an error -- such as it can exist at all, 9830 * it would be a result of corrupted DOF in the driver 9831 * properties. 9832 */ 9833 if (nmatched == NULL) { 9834 cmn_err(CE_WARN, "dtrace_enabling_match() " 9835 "error on %p: %d", (void *)ep, 9836 enab->dten_error); 9837 } 9838 9839 return (enab->dten_error); 9840 } 9841 } 9842 9843 enab->dten_probegen = dtrace_probegen; 9844 if (nmatched != NULL) 9845 *nmatched = matched; 9846 9847 return (0); 9848 } 9849 9850 static void 9851 dtrace_enabling_matchall(void) 9852 { 9853 dtrace_enabling_t *enab; 9854 9855 mutex_enter(&cpu_lock); 9856 mutex_enter(&dtrace_lock); 9857 9858 /* 9859 * Because we can be called after dtrace_detach() has been called, we 9860 * cannot assert that there are retained enablings. We can safely 9861 * load from dtrace_retained, however: the taskq_destroy() at the 9862 * end of dtrace_detach() will block pending our completion. 9863 */ 9864 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) 9865 (void) dtrace_enabling_match(enab, NULL); 9866 9867 mutex_exit(&dtrace_lock); 9868 mutex_exit(&cpu_lock); 9869 } 9870 9871 static int 9872 dtrace_enabling_matchstate(dtrace_state_t *state, int *nmatched) 9873 { 9874 dtrace_enabling_t *enab; 9875 int matched, total = 0, err; 9876 9877 ASSERT(MUTEX_HELD(&cpu_lock)); 9878 ASSERT(MUTEX_HELD(&dtrace_lock)); 9879 9880 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9881 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9882 9883 if (enab->dten_vstate->dtvs_state != state) 9884 continue; 9885 9886 if ((err = dtrace_enabling_match(enab, &matched)) != 0) 9887 return (err); 9888 9889 total += matched; 9890 } 9891 9892 if (nmatched != NULL) 9893 *nmatched = total; 9894 9895 return (0); 9896 } 9897 9898 /* 9899 * If an enabling is to be enabled without having matched probes (that is, if 9900 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 9901 * enabling must be _primed_ by creating an ECB for every ECB description. 9902 * This must be done to assure that we know the number of speculations, the 9903 * number of aggregations, the minimum buffer size needed, etc. before we 9904 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 9905 * enabling any probes, we create ECBs for every ECB decription, but with a 9906 * NULL probe -- which is exactly what this function does. 9907 */ 9908 static void 9909 dtrace_enabling_prime(dtrace_state_t *state) 9910 { 9911 dtrace_enabling_t *enab; 9912 int i; 9913 9914 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9915 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9916 9917 if (enab->dten_vstate->dtvs_state != state) 9918 continue; 9919 9920 /* 9921 * We don't want to prime an enabling more than once, lest 9922 * we allow a malicious user to induce resource exhaustion. 9923 * (The ECBs that result from priming an enabling aren't 9924 * leaked -- but they also aren't deallocated until the 9925 * consumer state is destroyed.) 9926 */ 9927 if (enab->dten_primed) 9928 continue; 9929 9930 for (i = 0; i < enab->dten_ndesc; i++) { 9931 enab->dten_current = enab->dten_desc[i]; 9932 (void) dtrace_probe_enable(NULL, enab); 9933 } 9934 9935 enab->dten_primed = 1; 9936 } 9937 } 9938 9939 /* 9940 * Called to indicate that probes should be provided due to retained 9941 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 9942 * must take an initial lap through the enabling calling the dtps_provide() 9943 * entry point explicitly to allow for autocreated probes. 9944 */ 9945 static void 9946 dtrace_enabling_provide(dtrace_provider_t *prv) 9947 { 9948 int i, all = 0; 9949 dtrace_probedesc_t desc; 9950 9951 ASSERT(MUTEX_HELD(&dtrace_lock)); 9952 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9953 9954 if (prv == NULL) { 9955 all = 1; 9956 prv = dtrace_provider; 9957 } 9958 9959 do { 9960 dtrace_enabling_t *enab = dtrace_retained; 9961 void *parg = prv->dtpv_arg; 9962 9963 for (; enab != NULL; enab = enab->dten_next) { 9964 for (i = 0; i < enab->dten_ndesc; i++) { 9965 desc = enab->dten_desc[i]->dted_probe; 9966 mutex_exit(&dtrace_lock); 9967 prv->dtpv_pops.dtps_provide(parg, &desc); 9968 mutex_enter(&dtrace_lock); 9969 } 9970 } 9971 } while (all && (prv = prv->dtpv_next) != NULL); 9972 9973 mutex_exit(&dtrace_lock); 9974 dtrace_probe_provide(NULL, all ? NULL : prv); 9975 mutex_enter(&dtrace_lock); 9976 } 9977 9978 /* 9979 * DTrace DOF Functions 9980 */ 9981 /*ARGSUSED*/ 9982 static void 9983 dtrace_dof_error(dof_hdr_t *dof, const char *str) 9984 { 9985 if (dtrace_err_verbose) 9986 cmn_err(CE_WARN, "failed to process DOF: %s", str); 9987 9988 #ifdef DTRACE_ERRDEBUG 9989 dtrace_errdebug(str); 9990 #endif 9991 } 9992 9993 /* 9994 * Create DOF out of a currently enabled state. Right now, we only create 9995 * DOF containing the run-time options -- but this could be expanded to create 9996 * complete DOF representing the enabled state. 9997 */ 9998 static dof_hdr_t * 9999 dtrace_dof_create(dtrace_state_t *state) 10000 { 10001 dof_hdr_t *dof; 10002 dof_sec_t *sec; 10003 dof_optdesc_t *opt; 10004 int i, len = sizeof (dof_hdr_t) + 10005 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 10006 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 10007 10008 ASSERT(MUTEX_HELD(&dtrace_lock)); 10009 10010 dof = kmem_zalloc(len, KM_SLEEP); 10011 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 10012 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 10013 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 10014 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 10015 10016 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 10017 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 10018 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION_1; 10019 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 10020 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 10021 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 10022 10023 dof->dofh_flags = 0; 10024 dof->dofh_hdrsize = sizeof (dof_hdr_t); 10025 dof->dofh_secsize = sizeof (dof_sec_t); 10026 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 10027 dof->dofh_secoff = sizeof (dof_hdr_t); 10028 dof->dofh_loadsz = len; 10029 dof->dofh_filesz = len; 10030 dof->dofh_pad = 0; 10031 10032 /* 10033 * Fill in the option section header... 10034 */ 10035 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 10036 sec->dofs_type = DOF_SECT_OPTDESC; 10037 sec->dofs_align = sizeof (uint64_t); 10038 sec->dofs_flags = DOF_SECF_LOAD; 10039 sec->dofs_entsize = sizeof (dof_optdesc_t); 10040 10041 opt = (dof_optdesc_t *)((uintptr_t)sec + 10042 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 10043 10044 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 10045 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 10046 10047 for (i = 0; i < DTRACEOPT_MAX; i++) { 10048 opt[i].dofo_option = i; 10049 opt[i].dofo_strtab = DOF_SECIDX_NONE; 10050 opt[i].dofo_value = state->dts_options[i]; 10051 } 10052 10053 return (dof); 10054 } 10055 10056 static dof_hdr_t * 10057 dtrace_dof_copyin(uintptr_t uarg, int *errp) 10058 { 10059 dof_hdr_t hdr, *dof; 10060 10061 ASSERT(!MUTEX_HELD(&dtrace_lock)); 10062 10063 /* 10064 * First, we're going to copyin() the sizeof (dof_hdr_t). 10065 */ 10066 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 10067 dtrace_dof_error(NULL, "failed to copyin DOF header"); 10068 *errp = EFAULT; 10069 return (NULL); 10070 } 10071 10072 /* 10073 * Now we'll allocate the entire DOF and copy it in -- provided 10074 * that the length isn't outrageous. 10075 */ 10076 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 10077 dtrace_dof_error(&hdr, "load size exceeds maximum"); 10078 *errp = E2BIG; 10079 return (NULL); 10080 } 10081 10082 if (hdr.dofh_loadsz < sizeof (hdr)) { 10083 dtrace_dof_error(&hdr, "invalid load size"); 10084 *errp = EINVAL; 10085 return (NULL); 10086 } 10087 10088 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 10089 10090 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) { 10091 kmem_free(dof, hdr.dofh_loadsz); 10092 *errp = EFAULT; 10093 return (NULL); 10094 } 10095 10096 return (dof); 10097 } 10098 10099 static dof_hdr_t * 10100 dtrace_dof_property(const char *name) 10101 { 10102 uchar_t *buf; 10103 uint64_t loadsz; 10104 unsigned int len, i; 10105 dof_hdr_t *dof; 10106 10107 /* 10108 * Unfortunately, array of values in .conf files are always (and 10109 * only) interpreted to be integer arrays. We must read our DOF 10110 * as an integer array, and then squeeze it into a byte array. 10111 */ 10112 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 10113 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 10114 return (NULL); 10115 10116 for (i = 0; i < len; i++) 10117 buf[i] = (uchar_t)(((int *)buf)[i]); 10118 10119 if (len < sizeof (dof_hdr_t)) { 10120 ddi_prop_free(buf); 10121 dtrace_dof_error(NULL, "truncated header"); 10122 return (NULL); 10123 } 10124 10125 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 10126 ddi_prop_free(buf); 10127 dtrace_dof_error(NULL, "truncated DOF"); 10128 return (NULL); 10129 } 10130 10131 if (loadsz >= dtrace_dof_maxsize) { 10132 ddi_prop_free(buf); 10133 dtrace_dof_error(NULL, "oversized DOF"); 10134 return (NULL); 10135 } 10136 10137 dof = kmem_alloc(loadsz, KM_SLEEP); 10138 bcopy(buf, dof, loadsz); 10139 ddi_prop_free(buf); 10140 10141 return (dof); 10142 } 10143 10144 static void 10145 dtrace_dof_destroy(dof_hdr_t *dof) 10146 { 10147 kmem_free(dof, dof->dofh_loadsz); 10148 } 10149 10150 /* 10151 * Return the dof_sec_t pointer corresponding to a given section index. If the 10152 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 10153 * a type other than DOF_SECT_NONE is specified, the header is checked against 10154 * this type and NULL is returned if the types do not match. 10155 */ 10156 static dof_sec_t * 10157 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 10158 { 10159 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 10160 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 10161 10162 if (i >= dof->dofh_secnum) { 10163 dtrace_dof_error(dof, "referenced section index is invalid"); 10164 return (NULL); 10165 } 10166 10167 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 10168 dtrace_dof_error(dof, "referenced section is not loadable"); 10169 return (NULL); 10170 } 10171 10172 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 10173 dtrace_dof_error(dof, "referenced section is the wrong type"); 10174 return (NULL); 10175 } 10176 10177 return (sec); 10178 } 10179 10180 static dtrace_probedesc_t * 10181 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 10182 { 10183 dof_probedesc_t *probe; 10184 dof_sec_t *strtab; 10185 uintptr_t daddr = (uintptr_t)dof; 10186 uintptr_t str; 10187 size_t size; 10188 10189 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 10190 dtrace_dof_error(dof, "invalid probe section"); 10191 return (NULL); 10192 } 10193 10194 if (sec->dofs_align != sizeof (dof_secidx_t)) { 10195 dtrace_dof_error(dof, "bad alignment in probe description"); 10196 return (NULL); 10197 } 10198 10199 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 10200 dtrace_dof_error(dof, "truncated probe description"); 10201 return (NULL); 10202 } 10203 10204 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 10205 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 10206 10207 if (strtab == NULL) 10208 return (NULL); 10209 10210 str = daddr + strtab->dofs_offset; 10211 size = strtab->dofs_size; 10212 10213 if (probe->dofp_provider >= strtab->dofs_size) { 10214 dtrace_dof_error(dof, "corrupt probe provider"); 10215 return (NULL); 10216 } 10217 10218 (void) strncpy(desc->dtpd_provider, 10219 (char *)(str + probe->dofp_provider), 10220 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 10221 10222 if (probe->dofp_mod >= strtab->dofs_size) { 10223 dtrace_dof_error(dof, "corrupt probe module"); 10224 return (NULL); 10225 } 10226 10227 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 10228 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 10229 10230 if (probe->dofp_func >= strtab->dofs_size) { 10231 dtrace_dof_error(dof, "corrupt probe function"); 10232 return (NULL); 10233 } 10234 10235 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 10236 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 10237 10238 if (probe->dofp_name >= strtab->dofs_size) { 10239 dtrace_dof_error(dof, "corrupt probe name"); 10240 return (NULL); 10241 } 10242 10243 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 10244 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 10245 10246 return (desc); 10247 } 10248 10249 static dtrace_difo_t * 10250 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10251 cred_t *cr) 10252 { 10253 dtrace_difo_t *dp; 10254 size_t ttl = 0; 10255 dof_difohdr_t *dofd; 10256 uintptr_t daddr = (uintptr_t)dof; 10257 size_t max = dtrace_difo_maxsize; 10258 int i, l, n; 10259 10260 static const struct { 10261 int section; 10262 int bufoffs; 10263 int lenoffs; 10264 int entsize; 10265 int align; 10266 const char *msg; 10267 } difo[] = { 10268 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 10269 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 10270 sizeof (dif_instr_t), "multiple DIF sections" }, 10271 10272 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 10273 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 10274 sizeof (uint64_t), "multiple integer tables" }, 10275 10276 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 10277 offsetof(dtrace_difo_t, dtdo_strlen), 0, 10278 sizeof (char), "multiple string tables" }, 10279 10280 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 10281 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 10282 sizeof (uint_t), "multiple variable tables" }, 10283 10284 { DOF_SECT_NONE, 0, 0, 0, NULL } 10285 }; 10286 10287 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 10288 dtrace_dof_error(dof, "invalid DIFO header section"); 10289 return (NULL); 10290 } 10291 10292 if (sec->dofs_align != sizeof (dof_secidx_t)) { 10293 dtrace_dof_error(dof, "bad alignment in DIFO header"); 10294 return (NULL); 10295 } 10296 10297 if (sec->dofs_size < sizeof (dof_difohdr_t) || 10298 sec->dofs_size % sizeof (dof_secidx_t)) { 10299 dtrace_dof_error(dof, "bad size in DIFO header"); 10300 return (NULL); 10301 } 10302 10303 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 10304 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 10305 10306 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10307 dp->dtdo_rtype = dofd->dofd_rtype; 10308 10309 for (l = 0; l < n; l++) { 10310 dof_sec_t *subsec; 10311 void **bufp; 10312 uint32_t *lenp; 10313 10314 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 10315 dofd->dofd_links[l])) == NULL) 10316 goto err; /* invalid section link */ 10317 10318 if (ttl + subsec->dofs_size > max) { 10319 dtrace_dof_error(dof, "exceeds maximum size"); 10320 goto err; 10321 } 10322 10323 ttl += subsec->dofs_size; 10324 10325 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 10326 if (subsec->dofs_type != difo[i].section) 10327 continue; 10328 10329 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 10330 dtrace_dof_error(dof, "section not loaded"); 10331 goto err; 10332 } 10333 10334 if (subsec->dofs_align != difo[i].align) { 10335 dtrace_dof_error(dof, "bad alignment"); 10336 goto err; 10337 } 10338 10339 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 10340 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 10341 10342 if (*bufp != NULL) { 10343 dtrace_dof_error(dof, difo[i].msg); 10344 goto err; 10345 } 10346 10347 if (difo[i].entsize != subsec->dofs_entsize) { 10348 dtrace_dof_error(dof, "entry size mismatch"); 10349 goto err; 10350 } 10351 10352 if (subsec->dofs_entsize != 0 && 10353 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 10354 dtrace_dof_error(dof, "corrupt entry size"); 10355 goto err; 10356 } 10357 10358 *lenp = subsec->dofs_size; 10359 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 10360 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 10361 *bufp, subsec->dofs_size); 10362 10363 if (subsec->dofs_entsize != 0) 10364 *lenp /= subsec->dofs_entsize; 10365 10366 break; 10367 } 10368 10369 /* 10370 * If we encounter a loadable DIFO sub-section that is not 10371 * known to us, assume this is a broken program and fail. 10372 */ 10373 if (difo[i].section == DOF_SECT_NONE && 10374 (subsec->dofs_flags & DOF_SECF_LOAD)) { 10375 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 10376 goto err; 10377 } 10378 } 10379 10380 if (dp->dtdo_buf == NULL) { 10381 /* 10382 * We can't have a DIF object without DIF text. 10383 */ 10384 dtrace_dof_error(dof, "missing DIF text"); 10385 goto err; 10386 } 10387 10388 /* 10389 * Before we validate the DIF object, run through the variable table 10390 * looking for the strings -- if any of their size are under, we'll set 10391 * their size to be the system-wide default string size. Note that 10392 * this should _not_ happen if the "strsize" option has been set -- 10393 * in this case, the compiler should have set the size to reflect the 10394 * setting of the option. 10395 */ 10396 for (i = 0; i < dp->dtdo_varlen; i++) { 10397 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10398 dtrace_diftype_t *t = &v->dtdv_type; 10399 10400 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 10401 continue; 10402 10403 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 10404 t->dtdt_size = dtrace_strsize_default; 10405 } 10406 10407 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 10408 goto err; 10409 10410 dtrace_difo_init(dp, vstate); 10411 return (dp); 10412 10413 err: 10414 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10415 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10416 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10417 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10418 10419 kmem_free(dp, sizeof (dtrace_difo_t)); 10420 return (NULL); 10421 } 10422 10423 static dtrace_predicate_t * 10424 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10425 cred_t *cr) 10426 { 10427 dtrace_difo_t *dp; 10428 10429 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 10430 return (NULL); 10431 10432 return (dtrace_predicate_create(dp)); 10433 } 10434 10435 static dtrace_actdesc_t * 10436 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10437 cred_t *cr) 10438 { 10439 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 10440 dof_actdesc_t *desc; 10441 dof_sec_t *difosec; 10442 size_t offs; 10443 uintptr_t daddr = (uintptr_t)dof; 10444 uint64_t arg; 10445 dtrace_actkind_t kind; 10446 10447 if (sec->dofs_type != DOF_SECT_ACTDESC) { 10448 dtrace_dof_error(dof, "invalid action section"); 10449 return (NULL); 10450 } 10451 10452 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 10453 dtrace_dof_error(dof, "truncated action description"); 10454 return (NULL); 10455 } 10456 10457 if (sec->dofs_align != sizeof (uint64_t)) { 10458 dtrace_dof_error(dof, "bad alignment in action description"); 10459 return (NULL); 10460 } 10461 10462 if (sec->dofs_size < sec->dofs_entsize) { 10463 dtrace_dof_error(dof, "section entry size exceeds total size"); 10464 return (NULL); 10465 } 10466 10467 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 10468 dtrace_dof_error(dof, "bad entry size in action description"); 10469 return (NULL); 10470 } 10471 10472 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 10473 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 10474 return (NULL); 10475 } 10476 10477 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 10478 desc = (dof_actdesc_t *)(daddr + 10479 (uintptr_t)sec->dofs_offset + offs); 10480 kind = (dtrace_actkind_t)desc->dofa_kind; 10481 10482 if (DTRACEACT_ISPRINTFLIKE(kind) && 10483 (kind != DTRACEACT_PRINTA || 10484 desc->dofa_strtab != DOF_SECIDX_NONE)) { 10485 dof_sec_t *strtab; 10486 char *str, *fmt; 10487 uint64_t i; 10488 10489 /* 10490 * printf()-like actions must have a format string. 10491 */ 10492 if ((strtab = dtrace_dof_sect(dof, 10493 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 10494 goto err; 10495 10496 str = (char *)((uintptr_t)dof + 10497 (uintptr_t)strtab->dofs_offset); 10498 10499 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 10500 if (str[i] == '\0') 10501 break; 10502 } 10503 10504 if (i >= strtab->dofs_size) { 10505 dtrace_dof_error(dof, "bogus format string"); 10506 goto err; 10507 } 10508 10509 if (i == desc->dofa_arg) { 10510 dtrace_dof_error(dof, "empty format string"); 10511 goto err; 10512 } 10513 10514 i -= desc->dofa_arg; 10515 fmt = kmem_alloc(i + 1, KM_SLEEP); 10516 bcopy(&str[desc->dofa_arg], fmt, i + 1); 10517 arg = (uint64_t)(uintptr_t)fmt; 10518 } else { 10519 if (kind == DTRACEACT_PRINTA) { 10520 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 10521 arg = 0; 10522 } else { 10523 arg = desc->dofa_arg; 10524 } 10525 } 10526 10527 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 10528 desc->dofa_uarg, arg); 10529 10530 if (last != NULL) { 10531 last->dtad_next = act; 10532 } else { 10533 first = act; 10534 } 10535 10536 last = act; 10537 10538 if (desc->dofa_difo == DOF_SECIDX_NONE) 10539 continue; 10540 10541 if ((difosec = dtrace_dof_sect(dof, 10542 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 10543 goto err; 10544 10545 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 10546 10547 if (act->dtad_difo == NULL) 10548 goto err; 10549 } 10550 10551 ASSERT(first != NULL); 10552 return (first); 10553 10554 err: 10555 for (act = first; act != NULL; act = next) { 10556 next = act->dtad_next; 10557 dtrace_actdesc_release(act, vstate); 10558 } 10559 10560 return (NULL); 10561 } 10562 10563 static dtrace_ecbdesc_t * 10564 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10565 cred_t *cr) 10566 { 10567 dtrace_ecbdesc_t *ep; 10568 dof_ecbdesc_t *ecb; 10569 dtrace_probedesc_t *desc; 10570 dtrace_predicate_t *pred = NULL; 10571 10572 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 10573 dtrace_dof_error(dof, "truncated ECB description"); 10574 return (NULL); 10575 } 10576 10577 if (sec->dofs_align != sizeof (uint64_t)) { 10578 dtrace_dof_error(dof, "bad alignment in ECB description"); 10579 return (NULL); 10580 } 10581 10582 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 10583 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 10584 10585 if (sec == NULL) 10586 return (NULL); 10587 10588 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 10589 ep->dted_uarg = ecb->dofe_uarg; 10590 desc = &ep->dted_probe; 10591 10592 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 10593 goto err; 10594 10595 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 10596 if ((sec = dtrace_dof_sect(dof, 10597 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 10598 goto err; 10599 10600 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 10601 goto err; 10602 10603 ep->dted_pred.dtpdd_predicate = pred; 10604 } 10605 10606 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 10607 if ((sec = dtrace_dof_sect(dof, 10608 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 10609 goto err; 10610 10611 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 10612 10613 if (ep->dted_action == NULL) 10614 goto err; 10615 } 10616 10617 return (ep); 10618 10619 err: 10620 if (pred != NULL) 10621 dtrace_predicate_release(pred, vstate); 10622 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 10623 return (NULL); 10624 } 10625 10626 /* 10627 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 10628 * specified DOF. At present, this amounts to simply adding 'ubase' to the 10629 * site of any user SETX relocations to account for load object base address. 10630 * In the future, if we need other relocations, this function can be extended. 10631 */ 10632 static int 10633 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 10634 { 10635 uintptr_t daddr = (uintptr_t)dof; 10636 dof_relohdr_t *dofr = 10637 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 10638 dof_sec_t *ss, *rs, *ts; 10639 dof_relodesc_t *r; 10640 uint_t i, n; 10641 10642 if (sec->dofs_size < sizeof (dof_relohdr_t) || 10643 sec->dofs_align != sizeof (dof_secidx_t)) { 10644 dtrace_dof_error(dof, "invalid relocation header"); 10645 return (-1); 10646 } 10647 10648 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 10649 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 10650 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 10651 10652 if (ss == NULL || rs == NULL || ts == NULL) 10653 return (-1); /* dtrace_dof_error() has been called already */ 10654 10655 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 10656 rs->dofs_align != sizeof (uint64_t)) { 10657 dtrace_dof_error(dof, "invalid relocation section"); 10658 return (-1); 10659 } 10660 10661 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 10662 n = rs->dofs_size / rs->dofs_entsize; 10663 10664 for (i = 0; i < n; i++) { 10665 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 10666 10667 switch (r->dofr_type) { 10668 case DOF_RELO_NONE: 10669 break; 10670 case DOF_RELO_SETX: 10671 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 10672 sizeof (uint64_t) > ts->dofs_size) { 10673 dtrace_dof_error(dof, "bad relocation offset"); 10674 return (-1); 10675 } 10676 10677 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 10678 dtrace_dof_error(dof, "misaligned setx relo"); 10679 return (-1); 10680 } 10681 10682 *(uint64_t *)taddr += ubase; 10683 break; 10684 default: 10685 dtrace_dof_error(dof, "invalid relocation type"); 10686 return (-1); 10687 } 10688 10689 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 10690 } 10691 10692 return (0); 10693 } 10694 10695 /* 10696 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 10697 * header: it should be at the front of a memory region that is at least 10698 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 10699 * size. It need not be validated in any other way. 10700 */ 10701 static int 10702 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 10703 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 10704 { 10705 uint64_t len = dof->dofh_loadsz, seclen; 10706 uintptr_t daddr = (uintptr_t)dof; 10707 dtrace_ecbdesc_t *ep; 10708 dtrace_enabling_t *enab; 10709 uint_t i; 10710 10711 ASSERT(MUTEX_HELD(&dtrace_lock)); 10712 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 10713 10714 /* 10715 * Check the DOF header identification bytes. In addition to checking 10716 * valid settings, we also verify that unused bits/bytes are zeroed so 10717 * we can use them later without fear of regressing existing binaries. 10718 */ 10719 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 10720 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 10721 dtrace_dof_error(dof, "DOF magic string mismatch"); 10722 return (-1); 10723 } 10724 10725 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 10726 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 10727 dtrace_dof_error(dof, "DOF has invalid data model"); 10728 return (-1); 10729 } 10730 10731 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 10732 dtrace_dof_error(dof, "DOF encoding mismatch"); 10733 return (-1); 10734 } 10735 10736 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 10737 dtrace_dof_error(dof, "DOF version mismatch"); 10738 return (-1); 10739 } 10740 10741 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 10742 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 10743 return (-1); 10744 } 10745 10746 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 10747 dtrace_dof_error(dof, "DOF uses too many integer registers"); 10748 return (-1); 10749 } 10750 10751 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 10752 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 10753 return (-1); 10754 } 10755 10756 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 10757 if (dof->dofh_ident[i] != 0) { 10758 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 10759 return (-1); 10760 } 10761 } 10762 10763 if (dof->dofh_flags & ~DOF_FL_VALID) { 10764 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 10765 return (-1); 10766 } 10767 10768 if (dof->dofh_secsize == 0) { 10769 dtrace_dof_error(dof, "zero section header size"); 10770 return (-1); 10771 } 10772 10773 /* 10774 * Check that the section headers don't exceed the amount of DOF 10775 * data. Note that we cast the section size and number of sections 10776 * to uint64_t's to prevent possible overflow in the multiplication. 10777 */ 10778 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 10779 10780 if (dof->dofh_secoff > len || seclen > len || 10781 dof->dofh_secoff + seclen > len) { 10782 dtrace_dof_error(dof, "truncated section headers"); 10783 return (-1); 10784 } 10785 10786 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 10787 dtrace_dof_error(dof, "misaligned section headers"); 10788 return (-1); 10789 } 10790 10791 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 10792 dtrace_dof_error(dof, "misaligned section size"); 10793 return (-1); 10794 } 10795 10796 /* 10797 * Take an initial pass through the section headers to be sure that 10798 * the headers don't have stray offsets. If the 'noprobes' flag is 10799 * set, do not permit sections relating to providers, probes, or args. 10800 */ 10801 for (i = 0; i < dof->dofh_secnum; i++) { 10802 dof_sec_t *sec = (dof_sec_t *)(daddr + 10803 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10804 10805 if (noprobes) { 10806 switch (sec->dofs_type) { 10807 case DOF_SECT_PROVIDER: 10808 case DOF_SECT_PROBES: 10809 case DOF_SECT_PRARGS: 10810 case DOF_SECT_PROFFS: 10811 dtrace_dof_error(dof, "illegal sections " 10812 "for enabling"); 10813 return (-1); 10814 } 10815 } 10816 10817 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 10818 continue; /* just ignore non-loadable sections */ 10819 10820 if (sec->dofs_align & (sec->dofs_align - 1)) { 10821 dtrace_dof_error(dof, "bad section alignment"); 10822 return (-1); 10823 } 10824 10825 if (sec->dofs_offset & (sec->dofs_align - 1)) { 10826 dtrace_dof_error(dof, "misaligned section"); 10827 return (-1); 10828 } 10829 10830 if (sec->dofs_offset > len || sec->dofs_size > len || 10831 sec->dofs_offset + sec->dofs_size > len) { 10832 dtrace_dof_error(dof, "corrupt section header"); 10833 return (-1); 10834 } 10835 10836 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 10837 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 10838 dtrace_dof_error(dof, "non-terminating string table"); 10839 return (-1); 10840 } 10841 } 10842 10843 /* 10844 * Take a second pass through the sections and locate and perform any 10845 * relocations that are present. We do this after the first pass to 10846 * be sure that all sections have had their headers validated. 10847 */ 10848 for (i = 0; i < dof->dofh_secnum; i++) { 10849 dof_sec_t *sec = (dof_sec_t *)(daddr + 10850 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10851 10852 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 10853 continue; /* skip sections that are not loadable */ 10854 10855 switch (sec->dofs_type) { 10856 case DOF_SECT_URELHDR: 10857 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 10858 return (-1); 10859 break; 10860 } 10861 } 10862 10863 if ((enab = *enabp) == NULL) 10864 enab = *enabp = dtrace_enabling_create(vstate); 10865 10866 for (i = 0; i < dof->dofh_secnum; i++) { 10867 dof_sec_t *sec = (dof_sec_t *)(daddr + 10868 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10869 10870 if (sec->dofs_type != DOF_SECT_ECBDESC) 10871 continue; 10872 10873 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 10874 dtrace_enabling_destroy(enab); 10875 *enabp = NULL; 10876 return (-1); 10877 } 10878 10879 dtrace_enabling_add(enab, ep); 10880 } 10881 10882 return (0); 10883 } 10884 10885 /* 10886 * Process DOF for any options. This routine assumes that the DOF has been 10887 * at least processed by dtrace_dof_slurp(). 10888 */ 10889 static int 10890 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 10891 { 10892 int i, rval; 10893 uint32_t entsize; 10894 size_t offs; 10895 dof_optdesc_t *desc; 10896 10897 for (i = 0; i < dof->dofh_secnum; i++) { 10898 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 10899 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10900 10901 if (sec->dofs_type != DOF_SECT_OPTDESC) 10902 continue; 10903 10904 if (sec->dofs_align != sizeof (uint64_t)) { 10905 dtrace_dof_error(dof, "bad alignment in " 10906 "option description"); 10907 return (EINVAL); 10908 } 10909 10910 if ((entsize = sec->dofs_entsize) == 0) { 10911 dtrace_dof_error(dof, "zeroed option entry size"); 10912 return (EINVAL); 10913 } 10914 10915 if (entsize < sizeof (dof_optdesc_t)) { 10916 dtrace_dof_error(dof, "bad option entry size"); 10917 return (EINVAL); 10918 } 10919 10920 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 10921 desc = (dof_optdesc_t *)((uintptr_t)dof + 10922 (uintptr_t)sec->dofs_offset + offs); 10923 10924 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 10925 dtrace_dof_error(dof, "non-zero option string"); 10926 return (EINVAL); 10927 } 10928 10929 if (desc->dofo_value == DTRACEOPT_UNSET) { 10930 dtrace_dof_error(dof, "unset option"); 10931 return (EINVAL); 10932 } 10933 10934 if ((rval = dtrace_state_option(state, 10935 desc->dofo_option, desc->dofo_value)) != 0) { 10936 dtrace_dof_error(dof, "rejected option"); 10937 return (rval); 10938 } 10939 } 10940 } 10941 10942 return (0); 10943 } 10944 10945 /* 10946 * DTrace Consumer State Functions 10947 */ 10948 int 10949 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 10950 { 10951 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 10952 void *base; 10953 uintptr_t limit; 10954 dtrace_dynvar_t *dvar, *next, *start; 10955 int i; 10956 10957 ASSERT(MUTEX_HELD(&dtrace_lock)); 10958 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 10959 10960 bzero(dstate, sizeof (dtrace_dstate_t)); 10961 10962 if ((dstate->dtds_chunksize = chunksize) == 0) 10963 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 10964 10965 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 10966 size = min; 10967 10968 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10969 return (ENOMEM); 10970 10971 dstate->dtds_size = size; 10972 dstate->dtds_base = base; 10973 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 10974 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 10975 10976 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 10977 10978 if (hashsize != 1 && (hashsize & 1)) 10979 hashsize--; 10980 10981 dstate->dtds_hashsize = hashsize; 10982 dstate->dtds_hash = dstate->dtds_base; 10983 10984 /* 10985 * Determine number of active CPUs. Divide free list evenly among 10986 * active CPUs. 10987 */ 10988 start = (dtrace_dynvar_t *) 10989 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 10990 limit = (uintptr_t)base + size; 10991 10992 maxper = (limit - (uintptr_t)start) / NCPU; 10993 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 10994 10995 for (i = 0; i < NCPU; i++) { 10996 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 10997 10998 /* 10999 * If we don't even have enough chunks to make it once through 11000 * NCPUs, we're just going to allocate everything to the first 11001 * CPU. And if we're on the last CPU, we're going to allocate 11002 * whatever is left over. In either case, we set the limit to 11003 * be the limit of the dynamic variable space. 11004 */ 11005 if (maxper == 0 || i == NCPU - 1) { 11006 limit = (uintptr_t)base + size; 11007 start = NULL; 11008 } else { 11009 limit = (uintptr_t)start + maxper; 11010 start = (dtrace_dynvar_t *)limit; 11011 } 11012 11013 ASSERT(limit <= (uintptr_t)base + size); 11014 11015 for (;;) { 11016 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 11017 dstate->dtds_chunksize); 11018 11019 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 11020 break; 11021 11022 dvar->dtdv_next = next; 11023 dvar = next; 11024 } 11025 11026 if (maxper == 0) 11027 break; 11028 } 11029 11030 return (0); 11031 } 11032 11033 void 11034 dtrace_dstate_fini(dtrace_dstate_t *dstate) 11035 { 11036 ASSERT(MUTEX_HELD(&cpu_lock)); 11037 11038 if (dstate->dtds_base == NULL) 11039 return; 11040 11041 kmem_free(dstate->dtds_base, dstate->dtds_size); 11042 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 11043 } 11044 11045 static void 11046 dtrace_vstate_fini(dtrace_vstate_t *vstate) 11047 { 11048 /* 11049 * Logical XOR, where are you? 11050 */ 11051 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 11052 11053 if (vstate->dtvs_nglobals > 0) { 11054 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 11055 sizeof (dtrace_statvar_t *)); 11056 } 11057 11058 if (vstate->dtvs_ntlocals > 0) { 11059 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 11060 sizeof (dtrace_difv_t)); 11061 } 11062 11063 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 11064 11065 if (vstate->dtvs_nlocals > 0) { 11066 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 11067 sizeof (dtrace_statvar_t *)); 11068 } 11069 } 11070 11071 static void 11072 dtrace_state_clean(dtrace_state_t *state) 11073 { 11074 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 11075 return; 11076 11077 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 11078 dtrace_speculation_clean(state); 11079 } 11080 11081 static void 11082 dtrace_state_deadman(dtrace_state_t *state) 11083 { 11084 hrtime_t now; 11085 11086 dtrace_sync(); 11087 11088 now = dtrace_gethrtime(); 11089 11090 if (state != dtrace_anon.dta_state && 11091 now - state->dts_laststatus >= dtrace_deadman_user) 11092 return; 11093 11094 /* 11095 * We must be sure that dts_alive never appears to be less than the 11096 * value upon entry to dtrace_state_deadman(), and because we lack a 11097 * dtrace_cas64(), we cannot store to it atomically. We thus instead 11098 * store INT64_MAX to it, followed by a memory barrier, followed by 11099 * the new value. This assures that dts_alive never appears to be 11100 * less than its true value, regardless of the order in which the 11101 * stores to the underlying storage are issued. 11102 */ 11103 state->dts_alive = INT64_MAX; 11104 dtrace_membar_producer(); 11105 state->dts_alive = now; 11106 } 11107 11108 dtrace_state_t * 11109 dtrace_state_create(dev_t *devp, cred_t *cr) 11110 { 11111 minor_t minor; 11112 major_t major; 11113 char c[30]; 11114 dtrace_state_t *state; 11115 dtrace_optval_t *opt; 11116 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 11117 11118 ASSERT(MUTEX_HELD(&dtrace_lock)); 11119 ASSERT(MUTEX_HELD(&cpu_lock)); 11120 11121 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 11122 VM_BESTFIT | VM_SLEEP); 11123 11124 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 11125 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 11126 return (NULL); 11127 } 11128 11129 state = ddi_get_soft_state(dtrace_softstate, minor); 11130 state->dts_epid = DTRACE_EPIDNONE + 1; 11131 11132 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); 11133 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 11134 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 11135 11136 if (devp != NULL) { 11137 major = getemajor(*devp); 11138 } else { 11139 major = ddi_driver_major(dtrace_devi); 11140 } 11141 11142 state->dts_dev = makedevice(major, minor); 11143 11144 if (devp != NULL) 11145 *devp = state->dts_dev; 11146 11147 /* 11148 * We allocate NCPU buffers. On the one hand, this can be quite 11149 * a bit of memory per instance (nearly 36K on a Starcat). On the 11150 * other hand, it saves an additional memory reference in the probe 11151 * path. 11152 */ 11153 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 11154 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 11155 state->dts_cleaner = CYCLIC_NONE; 11156 state->dts_deadman = CYCLIC_NONE; 11157 state->dts_vstate.dtvs_state = state; 11158 11159 for (i = 0; i < DTRACEOPT_MAX; i++) 11160 state->dts_options[i] = DTRACEOPT_UNSET; 11161 11162 /* 11163 * Set the default options. 11164 */ 11165 opt = state->dts_options; 11166 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 11167 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 11168 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 11169 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 11170 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 11171 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 11172 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 11173 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 11174 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 11175 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 11176 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 11177 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 11178 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 11179 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 11180 11181 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 11182 11183 /* 11184 * Depending on the user credentials, we set flag bits which alter probe 11185 * visibility or the amount of destructiveness allowed. In the case of 11186 * actual anonymous tracing, or the possession of all privileges, all of 11187 * the normal checks are bypassed. 11188 */ 11189 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 11190 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 11191 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 11192 } else { 11193 /* 11194 * Set up the credentials for this instantiation. We take a 11195 * hold on the credential to prevent it from disappearing on 11196 * us; this in turn prevents the zone_t referenced by this 11197 * credential from disappearing. This means that we can 11198 * examine the credential and the zone from probe context. 11199 */ 11200 crhold(cr); 11201 state->dts_cred.dcr_cred = cr; 11202 11203 /* 11204 * CRA_PROC means "we have *some* privilege for dtrace" and 11205 * unlocks the use of variables like pid, zonename, etc. 11206 */ 11207 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 11208 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 11209 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 11210 } 11211 11212 /* 11213 * dtrace_user allows use of syscall and profile providers. 11214 * If the user also has proc_owner and/or proc_zone, we 11215 * extend the scope to include additional visibility and 11216 * destructive power. 11217 */ 11218 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 11219 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 11220 state->dts_cred.dcr_visible |= 11221 DTRACE_CRV_ALLPROC; 11222 11223 state->dts_cred.dcr_action |= 11224 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 11225 } 11226 11227 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 11228 state->dts_cred.dcr_visible |= 11229 DTRACE_CRV_ALLZONE; 11230 11231 state->dts_cred.dcr_action |= 11232 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 11233 } 11234 11235 /* 11236 * If we have all privs in whatever zone this is, 11237 * we can do destructive things to processes which 11238 * have altered credentials. 11239 */ 11240 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 11241 cr->cr_zone->zone_privset)) { 11242 state->dts_cred.dcr_action |= 11243 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 11244 } 11245 } 11246 11247 /* 11248 * Holding the dtrace_kernel privilege also implies that 11249 * the user has the dtrace_user privilege from a visibility 11250 * perspective. But without further privileges, some 11251 * destructive actions are not available. 11252 */ 11253 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 11254 /* 11255 * Make all probes in all zones visible. However, 11256 * this doesn't mean that all actions become available 11257 * to all zones. 11258 */ 11259 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 11260 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 11261 11262 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 11263 DTRACE_CRA_PROC; 11264 /* 11265 * Holding proc_owner means that destructive actions 11266 * for *this* zone are allowed. 11267 */ 11268 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 11269 state->dts_cred.dcr_action |= 11270 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 11271 11272 /* 11273 * Holding proc_zone means that destructive actions 11274 * for this user/group ID in all zones is allowed. 11275 */ 11276 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 11277 state->dts_cred.dcr_action |= 11278 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 11279 11280 /* 11281 * If we have all privs in whatever zone this is, 11282 * we can do destructive things to processes which 11283 * have altered credentials. 11284 */ 11285 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 11286 cr->cr_zone->zone_privset)) { 11287 state->dts_cred.dcr_action |= 11288 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 11289 } 11290 } 11291 11292 /* 11293 * Holding the dtrace_proc privilege gives control over fasttrap 11294 * and pid providers. We need to grant wider destructive 11295 * privileges in the event that the user has proc_owner and/or 11296 * proc_zone. 11297 */ 11298 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 11299 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 11300 state->dts_cred.dcr_action |= 11301 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 11302 11303 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 11304 state->dts_cred.dcr_action |= 11305 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 11306 } 11307 } 11308 11309 return (state); 11310 } 11311 11312 static int 11313 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 11314 { 11315 dtrace_optval_t *opt = state->dts_options, size; 11316 processorid_t cpu; 11317 int flags = 0, rval; 11318 11319 ASSERT(MUTEX_HELD(&dtrace_lock)); 11320 ASSERT(MUTEX_HELD(&cpu_lock)); 11321 ASSERT(which < DTRACEOPT_MAX); 11322 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 11323 (state == dtrace_anon.dta_state && 11324 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 11325 11326 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 11327 return (0); 11328 11329 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 11330 cpu = opt[DTRACEOPT_CPU]; 11331 11332 if (which == DTRACEOPT_SPECSIZE) 11333 flags |= DTRACEBUF_NOSWITCH; 11334 11335 if (which == DTRACEOPT_BUFSIZE) { 11336 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 11337 flags |= DTRACEBUF_RING; 11338 11339 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 11340 flags |= DTRACEBUF_FILL; 11341 11342 flags |= DTRACEBUF_INACTIVE; 11343 } 11344 11345 for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { 11346 /* 11347 * The size must be 8-byte aligned. If the size is not 8-byte 11348 * aligned, drop it down by the difference. 11349 */ 11350 if (size & (sizeof (uint64_t) - 1)) 11351 size -= size & (sizeof (uint64_t) - 1); 11352 11353 if (size < state->dts_reserve) { 11354 /* 11355 * Buffers always must be large enough to accommodate 11356 * their prereserved space. We return E2BIG instead 11357 * of ENOMEM in this case to allow for user-level 11358 * software to differentiate the cases. 11359 */ 11360 return (E2BIG); 11361 } 11362 11363 rval = dtrace_buffer_alloc(buf, size, flags, cpu); 11364 11365 if (rval != ENOMEM) { 11366 opt[which] = size; 11367 return (rval); 11368 } 11369 11370 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 11371 return (rval); 11372 } 11373 11374 return (ENOMEM); 11375 } 11376 11377 static int 11378 dtrace_state_buffers(dtrace_state_t *state) 11379 { 11380 dtrace_speculation_t *spec = state->dts_speculations; 11381 int rval, i; 11382 11383 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 11384 DTRACEOPT_BUFSIZE)) != 0) 11385 return (rval); 11386 11387 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 11388 DTRACEOPT_AGGSIZE)) != 0) 11389 return (rval); 11390 11391 for (i = 0; i < state->dts_nspeculations; i++) { 11392 if ((rval = dtrace_state_buffer(state, 11393 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 11394 return (rval); 11395 } 11396 11397 return (0); 11398 } 11399 11400 static void 11401 dtrace_state_prereserve(dtrace_state_t *state) 11402 { 11403 dtrace_ecb_t *ecb; 11404 dtrace_probe_t *probe; 11405 11406 state->dts_reserve = 0; 11407 11408 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 11409 return; 11410 11411 /* 11412 * If our buffer policy is a "fill" buffer policy, we need to set the 11413 * prereserved space to be the space required by the END probes. 11414 */ 11415 probe = dtrace_probes[dtrace_probeid_end - 1]; 11416 ASSERT(probe != NULL); 11417 11418 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 11419 if (ecb->dte_state != state) 11420 continue; 11421 11422 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 11423 } 11424 } 11425 11426 static int 11427 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 11428 { 11429 dtrace_optval_t *opt = state->dts_options, sz, nspec; 11430 dtrace_speculation_t *spec; 11431 dtrace_buffer_t *buf; 11432 cyc_handler_t hdlr; 11433 cyc_time_t when; 11434 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 11435 dtrace_icookie_t cookie; 11436 11437 mutex_enter(&cpu_lock); 11438 mutex_enter(&dtrace_lock); 11439 11440 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 11441 rval = EBUSY; 11442 goto out; 11443 } 11444 11445 /* 11446 * Before we can perform any checks, we must prime all of the 11447 * retained enablings that correspond to this state. 11448 */ 11449 dtrace_enabling_prime(state); 11450 11451 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 11452 rval = EACCES; 11453 goto out; 11454 } 11455 11456 dtrace_state_prereserve(state); 11457 11458 /* 11459 * Now we want to do is try to allocate our speculations. 11460 * We do not automatically resize the number of speculations; if 11461 * this fails, we will fail the operation. 11462 */ 11463 nspec = opt[DTRACEOPT_NSPEC]; 11464 ASSERT(nspec != DTRACEOPT_UNSET); 11465 11466 if (nspec > INT_MAX) { 11467 rval = ENOMEM; 11468 goto out; 11469 } 11470 11471 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); 11472 11473 if (spec == NULL) { 11474 rval = ENOMEM; 11475 goto out; 11476 } 11477 11478 state->dts_speculations = spec; 11479 state->dts_nspeculations = (int)nspec; 11480 11481 for (i = 0; i < nspec; i++) { 11482 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { 11483 rval = ENOMEM; 11484 goto err; 11485 } 11486 11487 spec[i].dtsp_buffer = buf; 11488 } 11489 11490 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 11491 if (dtrace_anon.dta_state == NULL) { 11492 rval = ENOENT; 11493 goto out; 11494 } 11495 11496 if (state->dts_necbs != 0) { 11497 rval = EALREADY; 11498 goto out; 11499 } 11500 11501 state->dts_anon = dtrace_anon_grab(); 11502 ASSERT(state->dts_anon != NULL); 11503 state = state->dts_anon; 11504 11505 /* 11506 * We want "grabanon" to be set in the grabbed state, so we'll 11507 * copy that option value from the grabbing state into the 11508 * grabbed state. 11509 */ 11510 state->dts_options[DTRACEOPT_GRABANON] = 11511 opt[DTRACEOPT_GRABANON]; 11512 11513 *cpu = dtrace_anon.dta_beganon; 11514 11515 /* 11516 * If the anonymous state is active (as it almost certainly 11517 * is if the anonymous enabling ultimately matched anything), 11518 * we don't allow any further option processing -- but we 11519 * don't return failure. 11520 */ 11521 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11522 goto out; 11523 } 11524 11525 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 11526 opt[DTRACEOPT_AGGSIZE] != 0) { 11527 if (state->dts_aggregations == NULL) { 11528 /* 11529 * We're not going to create an aggregation buffer 11530 * because we don't have any ECBs that contain 11531 * aggregations -- set this option to 0. 11532 */ 11533 opt[DTRACEOPT_AGGSIZE] = 0; 11534 } else { 11535 /* 11536 * If we have an aggregation buffer, we must also have 11537 * a buffer to use as scratch. 11538 */ 11539 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 11540 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 11541 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 11542 } 11543 } 11544 } 11545 11546 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 11547 opt[DTRACEOPT_SPECSIZE] != 0) { 11548 if (!state->dts_speculates) { 11549 /* 11550 * We're not going to create speculation buffers 11551 * because we don't have any ECBs that actually 11552 * speculate -- set the speculation size to 0. 11553 */ 11554 opt[DTRACEOPT_SPECSIZE] = 0; 11555 } 11556 } 11557 11558 /* 11559 * The bare minimum size for any buffer that we're actually going to 11560 * do anything to is sizeof (uint64_t). 11561 */ 11562 sz = sizeof (uint64_t); 11563 11564 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 11565 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 11566 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 11567 /* 11568 * A buffer size has been explicitly set to 0 (or to a size 11569 * that will be adjusted to 0) and we need the space -- we 11570 * need to return failure. We return ENOSPC to differentiate 11571 * it from failing to allocate a buffer due to failure to meet 11572 * the reserve (for which we return E2BIG). 11573 */ 11574 rval = ENOSPC; 11575 goto out; 11576 } 11577 11578 if ((rval = dtrace_state_buffers(state)) != 0) 11579 goto err; 11580 11581 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 11582 sz = dtrace_dstate_defsize; 11583 11584 do { 11585 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 11586 11587 if (rval == 0) 11588 break; 11589 11590 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 11591 goto err; 11592 } while (sz >>= 1); 11593 11594 opt[DTRACEOPT_DYNVARSIZE] = sz; 11595 11596 if (rval != 0) 11597 goto err; 11598 11599 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 11600 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 11601 11602 if (opt[DTRACEOPT_CLEANRATE] == 0) 11603 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 11604 11605 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 11606 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 11607 11608 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 11609 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 11610 11611 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 11612 hdlr.cyh_arg = state; 11613 hdlr.cyh_level = CY_LOW_LEVEL; 11614 11615 when.cyt_when = 0; 11616 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 11617 11618 state->dts_cleaner = cyclic_add(&hdlr, &when); 11619 11620 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 11621 hdlr.cyh_arg = state; 11622 hdlr.cyh_level = CY_LOW_LEVEL; 11623 11624 when.cyt_when = 0; 11625 when.cyt_interval = dtrace_deadman_interval; 11626 11627 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 11628 state->dts_deadman = cyclic_add(&hdlr, &when); 11629 11630 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 11631 11632 /* 11633 * Now it's time to actually fire the BEGIN probe. We need to disable 11634 * interrupts here both to record the CPU on which we fired the BEGIN 11635 * probe (the data from this CPU will be processed first at user 11636 * level) and to manually activate the buffer for this CPU. 11637 */ 11638 cookie = dtrace_interrupt_disable(); 11639 *cpu = CPU->cpu_id; 11640 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 11641 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 11642 11643 dtrace_probe(dtrace_probeid_begin, 11644 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 11645 dtrace_interrupt_enable(cookie); 11646 /* 11647 * We may have had an exit action from a BEGIN probe; only change our 11648 * state to ACTIVE if we're still in WARMUP. 11649 */ 11650 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 11651 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 11652 11653 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 11654 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 11655 11656 /* 11657 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 11658 * want each CPU to transition its principal buffer out of the 11659 * INACTIVE state. Doing this assures that no CPU will suddenly begin 11660 * processing an ECB halfway down a probe's ECB chain; all CPUs will 11661 * atomically transition from processing none of a state's ECBs to 11662 * processing all of them. 11663 */ 11664 dtrace_xcall(DTRACE_CPUALL, 11665 (dtrace_xcall_t)dtrace_buffer_activate, state); 11666 goto out; 11667 11668 err: 11669 dtrace_buffer_free(state->dts_buffer); 11670 dtrace_buffer_free(state->dts_aggbuffer); 11671 11672 if ((nspec = state->dts_nspeculations) == 0) { 11673 ASSERT(state->dts_speculations == NULL); 11674 goto out; 11675 } 11676 11677 spec = state->dts_speculations; 11678 ASSERT(spec != NULL); 11679 11680 for (i = 0; i < state->dts_nspeculations; i++) { 11681 if ((buf = spec[i].dtsp_buffer) == NULL) 11682 break; 11683 11684 dtrace_buffer_free(buf); 11685 kmem_free(buf, bufsize); 11686 } 11687 11688 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 11689 state->dts_nspeculations = 0; 11690 state->dts_speculations = NULL; 11691 11692 out: 11693 mutex_exit(&dtrace_lock); 11694 mutex_exit(&cpu_lock); 11695 11696 return (rval); 11697 } 11698 11699 static int 11700 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 11701 { 11702 dtrace_icookie_t cookie; 11703 11704 ASSERT(MUTEX_HELD(&dtrace_lock)); 11705 11706 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 11707 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 11708 return (EINVAL); 11709 11710 /* 11711 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 11712 * to be sure that every CPU has seen it. See below for the details 11713 * on why this is done. 11714 */ 11715 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 11716 dtrace_sync(); 11717 11718 /* 11719 * By this point, it is impossible for any CPU to be still processing 11720 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 11721 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 11722 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 11723 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 11724 * iff we're in the END probe. 11725 */ 11726 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 11727 dtrace_sync(); 11728 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 11729 11730 /* 11731 * Finally, we can release the reserve and call the END probe. We 11732 * disable interrupts across calling the END probe to allow us to 11733 * return the CPU on which we actually called the END probe. This 11734 * allows user-land to be sure that this CPU's principal buffer is 11735 * processed last. 11736 */ 11737 state->dts_reserve = 0; 11738 11739 cookie = dtrace_interrupt_disable(); 11740 *cpu = CPU->cpu_id; 11741 dtrace_probe(dtrace_probeid_end, 11742 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 11743 dtrace_interrupt_enable(cookie); 11744 11745 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 11746 dtrace_sync(); 11747 11748 return (0); 11749 } 11750 11751 static int 11752 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 11753 dtrace_optval_t val) 11754 { 11755 ASSERT(MUTEX_HELD(&dtrace_lock)); 11756 11757 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11758 return (EBUSY); 11759 11760 if (option >= DTRACEOPT_MAX) 11761 return (EINVAL); 11762 11763 if (option != DTRACEOPT_CPU && val < 0) 11764 return (EINVAL); 11765 11766 switch (option) { 11767 case DTRACEOPT_DESTRUCTIVE: 11768 if (dtrace_destructive_disallow) 11769 return (EACCES); 11770 11771 state->dts_cred.dcr_destructive = 1; 11772 break; 11773 11774 case DTRACEOPT_BUFSIZE: 11775 case DTRACEOPT_DYNVARSIZE: 11776 case DTRACEOPT_AGGSIZE: 11777 case DTRACEOPT_SPECSIZE: 11778 case DTRACEOPT_STRSIZE: 11779 if (val < 0) 11780 return (EINVAL); 11781 11782 if (val >= LONG_MAX) { 11783 /* 11784 * If this is an otherwise negative value, set it to 11785 * the highest multiple of 128m less than LONG_MAX. 11786 * Technically, we're adjusting the size without 11787 * regard to the buffer resizing policy, but in fact, 11788 * this has no effect -- if we set the buffer size to 11789 * ~LONG_MAX and the buffer policy is ultimately set to 11790 * be "manual", the buffer allocation is guaranteed to 11791 * fail, if only because the allocation requires two 11792 * buffers. (We set the the size to the highest 11793 * multiple of 128m because it ensures that the size 11794 * will remain a multiple of a megabyte when 11795 * repeatedly halved -- all the way down to 15m.) 11796 */ 11797 val = LONG_MAX - (1 << 27) + 1; 11798 } 11799 } 11800 11801 state->dts_options[option] = val; 11802 11803 return (0); 11804 } 11805 11806 static void 11807 dtrace_state_destroy(dtrace_state_t *state) 11808 { 11809 dtrace_ecb_t *ecb; 11810 dtrace_vstate_t *vstate = &state->dts_vstate; 11811 minor_t minor = getminor(state->dts_dev); 11812 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 11813 dtrace_speculation_t *spec = state->dts_speculations; 11814 int nspec = state->dts_nspeculations; 11815 uint32_t match; 11816 11817 ASSERT(MUTEX_HELD(&dtrace_lock)); 11818 ASSERT(MUTEX_HELD(&cpu_lock)); 11819 11820 /* 11821 * First, retract any retained enablings for this state. 11822 */ 11823 dtrace_enabling_retract(state); 11824 ASSERT(state->dts_nretained == 0); 11825 11826 /* 11827 * Release the credential hold we took in dtrace_state_create(). 11828 */ 11829 if (state->dts_cred.dcr_cred != NULL) 11830 crfree(state->dts_cred.dcr_cred); 11831 11832 /* 11833 * Now we need to disable and destroy any enabled probes. Because any 11834 * DTRACE_PRIV_KERNEL probes may actually be slowing our progress 11835 * (especially if they're all enabled), we take two passes through 11836 * the ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, 11837 * and in the second we disable whatever is left over. 11838 */ 11839 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 11840 for (i = 0; i < state->dts_necbs; i++) { 11841 if ((ecb = state->dts_ecbs[i]) == NULL) 11842 continue; 11843 11844 if (match && ecb->dte_probe != NULL) { 11845 dtrace_probe_t *probe = ecb->dte_probe; 11846 dtrace_provider_t *prov = probe->dtpr_provider; 11847 11848 if (!(prov->dtpv_priv.dtpp_flags & match)) 11849 continue; 11850 } 11851 11852 dtrace_ecb_disable(ecb); 11853 dtrace_ecb_destroy(ecb); 11854 } 11855 11856 if (!match) 11857 break; 11858 } 11859 11860 /* 11861 * Before we free the buffers, perform one more sync to assure that 11862 * every CPU is out of probe context. 11863 */ 11864 dtrace_sync(); 11865 11866 dtrace_buffer_free(state->dts_buffer); 11867 dtrace_buffer_free(state->dts_aggbuffer); 11868 11869 for (i = 0; i < nspec; i++) 11870 dtrace_buffer_free(spec[i].dtsp_buffer); 11871 11872 if (state->dts_cleaner != CYCLIC_NONE) 11873 cyclic_remove(state->dts_cleaner); 11874 11875 if (state->dts_deadman != CYCLIC_NONE) 11876 cyclic_remove(state->dts_deadman); 11877 11878 dtrace_dstate_fini(&vstate->dtvs_dynvars); 11879 dtrace_vstate_fini(vstate); 11880 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 11881 11882 if (state->dts_aggregations != NULL) { 11883 #ifdef DEBUG 11884 for (i = 0; i < state->dts_naggregations; i++) 11885 ASSERT(state->dts_aggregations[i] == NULL); 11886 #endif 11887 ASSERT(state->dts_naggregations > 0); 11888 kmem_free(state->dts_aggregations, 11889 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 11890 } 11891 11892 kmem_free(state->dts_buffer, bufsize); 11893 kmem_free(state->dts_aggbuffer, bufsize); 11894 11895 for (i = 0; i < nspec; i++) 11896 kmem_free(spec[i].dtsp_buffer, bufsize); 11897 11898 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 11899 11900 dtrace_format_destroy(state); 11901 11902 vmem_destroy(state->dts_aggid_arena); 11903 ddi_soft_state_free(dtrace_softstate, minor); 11904 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 11905 } 11906 11907 /* 11908 * DTrace Anonymous Enabling Functions 11909 */ 11910 static dtrace_state_t * 11911 dtrace_anon_grab(void) 11912 { 11913 dtrace_state_t *state; 11914 11915 ASSERT(MUTEX_HELD(&dtrace_lock)); 11916 11917 if ((state = dtrace_anon.dta_state) == NULL) { 11918 ASSERT(dtrace_anon.dta_enabling == NULL); 11919 return (NULL); 11920 } 11921 11922 ASSERT(dtrace_anon.dta_enabling != NULL); 11923 ASSERT(dtrace_retained != NULL); 11924 11925 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 11926 dtrace_anon.dta_enabling = NULL; 11927 dtrace_anon.dta_state = NULL; 11928 11929 return (state); 11930 } 11931 11932 static void 11933 dtrace_anon_property(void) 11934 { 11935 int i, rv; 11936 dtrace_state_t *state; 11937 dof_hdr_t *dof; 11938 char c[32]; /* enough for "dof-data-" + digits */ 11939 11940 ASSERT(MUTEX_HELD(&dtrace_lock)); 11941 ASSERT(MUTEX_HELD(&cpu_lock)); 11942 11943 for (i = 0; ; i++) { 11944 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 11945 11946 dtrace_err_verbose = 1; 11947 11948 if ((dof = dtrace_dof_property(c)) == NULL) { 11949 dtrace_err_verbose = 0; 11950 break; 11951 } 11952 11953 /* 11954 * We want to create anonymous state, so we need to transition 11955 * the kernel debugger to indicate that DTrace is active. If 11956 * this fails (e.g. because the debugger has modified text in 11957 * some way), we won't continue with the processing. 11958 */ 11959 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 11960 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 11961 "enabling ignored."); 11962 dtrace_dof_destroy(dof); 11963 break; 11964 } 11965 11966 /* 11967 * If we haven't allocated an anonymous state, we'll do so now. 11968 */ 11969 if ((state = dtrace_anon.dta_state) == NULL) { 11970 state = dtrace_state_create(NULL, NULL); 11971 dtrace_anon.dta_state = state; 11972 11973 if (state == NULL) { 11974 /* 11975 * This basically shouldn't happen: the only 11976 * failure mode from dtrace_state_create() is a 11977 * failure of ddi_soft_state_zalloc() that 11978 * itself should never happen. Still, the 11979 * interface allows for a failure mode, and 11980 * we want to fail as gracefully as possible: 11981 * we'll emit an error message and cease 11982 * processing anonymous state in this case. 11983 */ 11984 cmn_err(CE_WARN, "failed to create " 11985 "anonymous state"); 11986 dtrace_dof_destroy(dof); 11987 break; 11988 } 11989 } 11990 11991 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 11992 &dtrace_anon.dta_enabling, 0, B_TRUE); 11993 11994 if (rv == 0) 11995 rv = dtrace_dof_options(dof, state); 11996 11997 dtrace_err_verbose = 0; 11998 dtrace_dof_destroy(dof); 11999 12000 if (rv != 0) { 12001 /* 12002 * This is malformed DOF; chuck any anonymous state 12003 * that we created. 12004 */ 12005 ASSERT(dtrace_anon.dta_enabling == NULL); 12006 dtrace_state_destroy(state); 12007 dtrace_anon.dta_state = NULL; 12008 break; 12009 } 12010 12011 ASSERT(dtrace_anon.dta_enabling != NULL); 12012 } 12013 12014 if (dtrace_anon.dta_enabling != NULL) { 12015 int rval; 12016 12017 /* 12018 * dtrace_enabling_retain() can only fail because we are 12019 * trying to retain more enablings than are allowed -- but 12020 * we only have one anonymous enabling, and we are guaranteed 12021 * to be allowed at least one retained enabling; we assert 12022 * that dtrace_enabling_retain() returns success. 12023 */ 12024 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 12025 ASSERT(rval == 0); 12026 12027 dtrace_enabling_dump(dtrace_anon.dta_enabling); 12028 } 12029 } 12030 12031 /* 12032 * DTrace Helper Functions 12033 */ 12034 static void 12035 dtrace_helper_trace(dtrace_helper_action_t *helper, 12036 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 12037 { 12038 uint32_t size, next, nnext, i; 12039 dtrace_helptrace_t *ent; 12040 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 12041 12042 if (!dtrace_helptrace_enabled) 12043 return; 12044 12045 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 12046 12047 /* 12048 * What would a tracing framework be without its own tracing 12049 * framework? (Well, a hell of a lot simpler, for starters...) 12050 */ 12051 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 12052 sizeof (uint64_t) - sizeof (uint64_t); 12053 12054 /* 12055 * Iterate until we can allocate a slot in the trace buffer. 12056 */ 12057 do { 12058 next = dtrace_helptrace_next; 12059 12060 if (next + size < dtrace_helptrace_bufsize) { 12061 nnext = next + size; 12062 } else { 12063 nnext = size; 12064 } 12065 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 12066 12067 /* 12068 * We have our slot; fill it in. 12069 */ 12070 if (nnext == size) 12071 next = 0; 12072 12073 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 12074 ent->dtht_helper = helper; 12075 ent->dtht_where = where; 12076 ent->dtht_nlocals = vstate->dtvs_nlocals; 12077 12078 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 12079 mstate->dtms_fltoffs : -1; 12080 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 12081 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 12082 12083 for (i = 0; i < vstate->dtvs_nlocals; i++) { 12084 dtrace_statvar_t *svar; 12085 12086 if ((svar = vstate->dtvs_locals[i]) == NULL) 12087 continue; 12088 12089 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 12090 ent->dtht_locals[i] = 12091 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; 12092 } 12093 } 12094 12095 static uint64_t 12096 dtrace_helper(int which, dtrace_mstate_t *mstate, 12097 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 12098 { 12099 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 12100 uint64_t sarg0 = mstate->dtms_arg[0]; 12101 uint64_t sarg1 = mstate->dtms_arg[1]; 12102 uint64_t rval; 12103 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 12104 dtrace_helper_action_t *helper; 12105 dtrace_vstate_t *vstate; 12106 dtrace_difo_t *pred; 12107 int i, trace = dtrace_helptrace_enabled; 12108 12109 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 12110 12111 if (helpers == NULL) 12112 return (0); 12113 12114 if ((helper = helpers->dthps_actions[which]) == NULL) 12115 return (0); 12116 12117 vstate = &helpers->dthps_vstate; 12118 mstate->dtms_arg[0] = arg0; 12119 mstate->dtms_arg[1] = arg1; 12120 12121 /* 12122 * Now iterate over each helper. If its predicate evaluates to 'true', 12123 * we'll call the corresponding actions. Note that the below calls 12124 * to dtrace_dif_emulate() may set faults in machine state. This is 12125 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 12126 * the stored DIF offset with its own (which is the desired behavior). 12127 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 12128 * from machine state; this is okay, too. 12129 */ 12130 for (; helper != NULL; helper = helper->dthp_next) { 12131 if ((pred = helper->dthp_predicate) != NULL) { 12132 if (trace) 12133 dtrace_helper_trace(helper, mstate, vstate, 0); 12134 12135 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 12136 goto next; 12137 12138 if (*flags & CPU_DTRACE_FAULT) 12139 goto err; 12140 } 12141 12142 for (i = 0; i < helper->dthp_nactions; i++) { 12143 if (trace) 12144 dtrace_helper_trace(helper, 12145 mstate, vstate, i + 1); 12146 12147 rval = dtrace_dif_emulate(helper->dthp_actions[i], 12148 mstate, vstate, state); 12149 12150 if (*flags & CPU_DTRACE_FAULT) 12151 goto err; 12152 } 12153 12154 next: 12155 if (trace) 12156 dtrace_helper_trace(helper, mstate, vstate, 12157 DTRACE_HELPTRACE_NEXT); 12158 } 12159 12160 if (trace) 12161 dtrace_helper_trace(helper, mstate, vstate, 12162 DTRACE_HELPTRACE_DONE); 12163 12164 /* 12165 * Restore the arg0 that we saved upon entry. 12166 */ 12167 mstate->dtms_arg[0] = sarg0; 12168 mstate->dtms_arg[1] = sarg1; 12169 12170 return (rval); 12171 12172 err: 12173 if (trace) 12174 dtrace_helper_trace(helper, mstate, vstate, 12175 DTRACE_HELPTRACE_ERR); 12176 12177 /* 12178 * Restore the arg0 that we saved upon entry. 12179 */ 12180 mstate->dtms_arg[0] = sarg0; 12181 mstate->dtms_arg[1] = sarg1; 12182 12183 return (NULL); 12184 } 12185 12186 static void 12187 dtrace_helper_destroy(dtrace_helper_action_t *helper, dtrace_vstate_t *vstate) 12188 { 12189 int i; 12190 12191 if (helper->dthp_predicate != NULL) 12192 dtrace_difo_release(helper->dthp_predicate, vstate); 12193 12194 for (i = 0; i < helper->dthp_nactions; i++) { 12195 ASSERT(helper->dthp_actions[i] != NULL); 12196 dtrace_difo_release(helper->dthp_actions[i], vstate); 12197 } 12198 12199 kmem_free(helper->dthp_actions, 12200 helper->dthp_nactions * sizeof (dtrace_difo_t *)); 12201 kmem_free(helper, sizeof (dtrace_helper_action_t)); 12202 } 12203 12204 static int 12205 dtrace_helper_destroygen(int gen) 12206 { 12207 dtrace_helpers_t *help = curproc->p_dtrace_helpers; 12208 dtrace_vstate_t *vstate; 12209 int i; 12210 12211 ASSERT(MUTEX_HELD(&dtrace_lock)); 12212 12213 if (help == NULL || gen > help->dthps_generation) 12214 return (EINVAL); 12215 12216 vstate = &help->dthps_vstate; 12217 12218 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12219 dtrace_helper_action_t *last = NULL, *h, *next; 12220 12221 for (h = help->dthps_actions[i]; h != NULL; h = next) { 12222 next = h->dthp_next; 12223 12224 if (h->dthp_generation == gen) { 12225 if (last != NULL) { 12226 last->dthp_next = next; 12227 } else { 12228 help->dthps_actions[i] = next; 12229 } 12230 12231 dtrace_helper_destroy(h, vstate); 12232 } else { 12233 last = h; 12234 } 12235 } 12236 } 12237 12238 return (0); 12239 } 12240 12241 static int 12242 dtrace_helper_validate(dtrace_helper_action_t *helper) 12243 { 12244 int err = 0, i; 12245 dtrace_difo_t *dp; 12246 12247 if ((dp = helper->dthp_predicate) != NULL) 12248 err += dtrace_difo_validate_helper(dp); 12249 12250 for (i = 0; i < helper->dthp_nactions; i++) 12251 err += dtrace_difo_validate_helper(helper->dthp_actions[i]); 12252 12253 return (err == 0); 12254 } 12255 12256 static int 12257 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 12258 { 12259 dtrace_helpers_t *help; 12260 dtrace_helper_action_t *helper, *last; 12261 dtrace_actdesc_t *act; 12262 dtrace_vstate_t *vstate; 12263 dtrace_predicate_t *pred; 12264 int count = 0, nactions = 0, i; 12265 12266 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 12267 return (EINVAL); 12268 12269 help = curproc->p_dtrace_helpers; 12270 last = help->dthps_actions[which]; 12271 vstate = &help->dthps_vstate; 12272 12273 for (count = 0; last != NULL; last = last->dthp_next) { 12274 count++; 12275 if (last->dthp_next == NULL) 12276 break; 12277 } 12278 12279 /* 12280 * If we already have dtrace_helper_actions_max helper actions for this 12281 * helper action type, we'll refuse to add a new one. 12282 */ 12283 if (count >= dtrace_helper_actions_max) 12284 return (ENOSPC); 12285 12286 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 12287 helper->dthp_generation = help->dthps_generation; 12288 12289 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 12290 ASSERT(pred->dtp_difo != NULL); 12291 dtrace_difo_hold(pred->dtp_difo); 12292 helper->dthp_predicate = pred->dtp_difo; 12293 } 12294 12295 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 12296 if (act->dtad_kind != DTRACEACT_DIFEXPR) 12297 goto err; 12298 12299 if (act->dtad_difo == NULL) 12300 goto err; 12301 12302 nactions++; 12303 } 12304 12305 helper->dthp_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 12306 (helper->dthp_nactions = nactions), KM_SLEEP); 12307 12308 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 12309 dtrace_difo_hold(act->dtad_difo); 12310 helper->dthp_actions[i++] = act->dtad_difo; 12311 } 12312 12313 if (!dtrace_helper_validate(helper)) 12314 goto err; 12315 12316 if (last == NULL) { 12317 help->dthps_actions[which] = helper; 12318 } else { 12319 last->dthp_next = helper; 12320 } 12321 12322 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 12323 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 12324 dtrace_helptrace_next = 0; 12325 } 12326 12327 return (0); 12328 err: 12329 dtrace_helper_destroy(helper, vstate); 12330 return (EINVAL); 12331 } 12332 12333 static void 12334 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 12335 dof_helper_t *dofhp) 12336 { 12337 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 12338 12339 mutex_enter(&dtrace_meta_lock); 12340 mutex_enter(&dtrace_lock); 12341 12342 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 12343 /* 12344 * If the dtrace module is loaded but not attached, or if 12345 * there aren't isn't a meta provider registered to deal with 12346 * these provider descriptions, we need to postpone creating 12347 * the actual providers until later. 12348 */ 12349 12350 if (help->dthps_next == NULL && help->dthps_prev == NULL && 12351 dtrace_deferred_pid != help) { 12352 help->dthps_deferred = 1; 12353 help->dthps_pid = p->p_pid; 12354 help->dthps_next = dtrace_deferred_pid; 12355 help->dthps_prev = NULL; 12356 if (dtrace_deferred_pid != NULL) 12357 dtrace_deferred_pid->dthps_prev = help; 12358 dtrace_deferred_pid = help; 12359 } 12360 12361 mutex_exit(&dtrace_lock); 12362 12363 } else if (dofhp != NULL) { 12364 /* 12365 * If the dtrace module is loaded and we have a particular 12366 * helper provider description, pass that off to the 12367 * meta provider. 12368 */ 12369 12370 mutex_exit(&dtrace_lock); 12371 12372 dtrace_helper_provide(dofhp, p->p_pid); 12373 12374 } else { 12375 /* 12376 * Otherwise, just pass all the helper provider descriptions 12377 * off to the meta provider. 12378 */ 12379 12380 int i; 12381 mutex_exit(&dtrace_lock); 12382 12383 for (i = 0; i < help->dthps_nprovs; i++) { 12384 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 12385 p->p_pid); 12386 } 12387 } 12388 12389 mutex_exit(&dtrace_meta_lock); 12390 } 12391 12392 static int 12393 dtrace_helper_provider_add(dof_helper_t *dofhp) 12394 { 12395 dtrace_helpers_t *help; 12396 dtrace_helper_provider_t *hprov, **tmp_provs; 12397 uint_t tmp_nprovs, i; 12398 12399 help = curproc->p_dtrace_helpers; 12400 ASSERT(help != NULL); 12401 12402 /* 12403 * If we already have dtrace_helper_providers_max helper providers, 12404 * we're refuse to add a new one. 12405 */ 12406 if (help->dthps_nprovs >= dtrace_helper_providers_max) 12407 return (ENOSPC); 12408 12409 /* 12410 * Check to make sure this isn't a duplicate. 12411 */ 12412 for (i = 0; i < help->dthps_nprovs; i++) { 12413 if (dofhp->dofhp_addr == 12414 help->dthps_provs[i]->dthp_prov.dofhp_addr) 12415 return (EALREADY); 12416 } 12417 12418 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 12419 hprov->dthp_prov = *dofhp; 12420 hprov->dthp_ref = 1; 12421 12422 tmp_nprovs = help->dthps_nprovs; 12423 tmp_provs = help->dthps_provs; 12424 help->dthps_nprovs++; 12425 help->dthps_provs = kmem_zalloc(help->dthps_nprovs * 12426 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 12427 12428 help->dthps_provs[tmp_nprovs] = hprov; 12429 if (tmp_provs != NULL) { 12430 bcopy(tmp_provs, help->dthps_provs, tmp_nprovs * 12431 sizeof (dtrace_helper_provider_t *)); 12432 kmem_free(tmp_provs, tmp_nprovs * 12433 sizeof (dtrace_helper_provider_t *)); 12434 } 12435 12436 return (0); 12437 } 12438 12439 static void 12440 dtrace_helper_provider_remove(dtrace_helper_provider_t *hprov) 12441 { 12442 mutex_enter(&dtrace_lock); 12443 12444 if (--hprov->dthp_ref == 0) { 12445 dof_hdr_t *dof; 12446 mutex_exit(&dtrace_lock); 12447 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 12448 dtrace_dof_destroy(dof); 12449 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 12450 } else { 12451 mutex_exit(&dtrace_lock); 12452 } 12453 } 12454 12455 static int 12456 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 12457 { 12458 uintptr_t daddr = (uintptr_t)dof; 12459 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec; 12460 dof_provider_t *provider; 12461 dof_probe_t *probe; 12462 uint8_t *arg; 12463 char *strtab, *typestr; 12464 dof_stridx_t typeidx; 12465 size_t typesz; 12466 uint_t nprobes, j, k; 12467 12468 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 12469 12470 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 12471 dtrace_dof_error(dof, "misaligned section offset"); 12472 return (-1); 12473 } 12474 12475 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 12476 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 12477 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 12478 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 12479 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 12480 12481 if (str_sec == NULL || prb_sec == NULL || 12482 arg_sec == NULL || off_sec == NULL) 12483 return (-1); 12484 12485 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 12486 12487 if (provider->dofpv_name >= str_sec->dofs_size || 12488 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 12489 dtrace_dof_error(dof, "invalid provider name"); 12490 return (-1); 12491 } 12492 12493 if (prb_sec->dofs_entsize == 0 || 12494 prb_sec->dofs_entsize > prb_sec->dofs_size) { 12495 dtrace_dof_error(dof, "invalid entry size"); 12496 return (-1); 12497 } 12498 12499 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 12500 dtrace_dof_error(dof, "misaligned entry size"); 12501 return (-1); 12502 } 12503 12504 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 12505 dtrace_dof_error(dof, "invalid entry size"); 12506 return (-1); 12507 } 12508 12509 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 12510 dtrace_dof_error(dof, "misaligned section offset"); 12511 return (-1); 12512 } 12513 12514 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 12515 dtrace_dof_error(dof, "invalid entry size"); 12516 return (-1); 12517 } 12518 12519 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 12520 12521 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 12522 12523 /* 12524 * Take a pass through the probes to check for errors. 12525 */ 12526 for (j = 0; j < nprobes; j++) { 12527 probe = (dof_probe_t *)(uintptr_t)(daddr + 12528 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 12529 12530 if (probe->dofpr_func >= str_sec->dofs_size) { 12531 dtrace_dof_error(dof, "invalid function name"); 12532 return (-1); 12533 } 12534 12535 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 12536 dtrace_dof_error(dof, "function name too long"); 12537 return (-1); 12538 } 12539 12540 if (probe->dofpr_name >= str_sec->dofs_size || 12541 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 12542 dtrace_dof_error(dof, "invalid probe name"); 12543 return (-1); 12544 } 12545 12546 12547 if (probe->dofpr_offidx + probe->dofpr_noffs < 12548 probe->dofpr_offidx || 12549 (probe->dofpr_offidx + probe->dofpr_noffs) * 12550 off_sec->dofs_entsize > off_sec->dofs_size) { 12551 dtrace_dof_error(dof, "invalid probe offset"); 12552 return (-1); 12553 } 12554 12555 if (probe->dofpr_argidx + probe->dofpr_xargc < 12556 probe->dofpr_argidx || 12557 (probe->dofpr_argidx + probe->dofpr_xargc) * 12558 arg_sec->dofs_entsize > arg_sec->dofs_size) { 12559 dtrace_dof_error(dof, "invalid args"); 12560 return (-1); 12561 } 12562 12563 typeidx = probe->dofpr_nargv; 12564 typestr = strtab + probe->dofpr_nargv; 12565 for (k = 0; k < probe->dofpr_nargc; k++) { 12566 if (typeidx >= str_sec->dofs_size) { 12567 dtrace_dof_error(dof, "bad " 12568 "native argument type"); 12569 return (-1); 12570 } 12571 12572 typesz = strlen(typestr) + 1; 12573 if (typesz > DTRACE_ARGTYPELEN) { 12574 dtrace_dof_error(dof, "native " 12575 "argument type too long"); 12576 return (-1); 12577 } 12578 typeidx += typesz; 12579 typestr += typesz; 12580 } 12581 12582 typeidx = probe->dofpr_xargv; 12583 typestr = strtab + probe->dofpr_xargv; 12584 for (k = 0; k < probe->dofpr_xargc; k++) { 12585 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 12586 dtrace_dof_error(dof, "bad " 12587 "native argument index"); 12588 return (-1); 12589 } 12590 12591 if (typeidx >= str_sec->dofs_size) { 12592 dtrace_dof_error(dof, "bad " 12593 "translated argument type"); 12594 return (-1); 12595 } 12596 12597 typesz = strlen(typestr) + 1; 12598 if (typesz > DTRACE_ARGTYPELEN) { 12599 dtrace_dof_error(dof, "translated argument " 12600 "type too long"); 12601 return (-1); 12602 } 12603 12604 typeidx += typesz; 12605 typestr += typesz; 12606 } 12607 } 12608 12609 return (0); 12610 } 12611 12612 static int 12613 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 12614 { 12615 dtrace_helpers_t *help; 12616 dtrace_vstate_t *vstate; 12617 dtrace_enabling_t *enab = NULL; 12618 int i, gen, rv, nhelpers = 0, destroy = 1; 12619 12620 ASSERT(MUTEX_HELD(&dtrace_lock)); 12621 12622 if ((help = curproc->p_dtrace_helpers) == NULL) 12623 help = dtrace_helpers_create(curproc); 12624 12625 vstate = &help->dthps_vstate; 12626 12627 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 12628 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 12629 dtrace_dof_destroy(dof); 12630 return (rv); 12631 } 12632 12633 /* 12634 * Now we need to walk through the ECB descriptions in the enabling. 12635 */ 12636 for (i = 0; i < enab->dten_ndesc; i++) { 12637 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12638 dtrace_probedesc_t *desc = &ep->dted_probe; 12639 12640 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 12641 continue; 12642 12643 if (strcmp(desc->dtpd_mod, "helper") != 0) 12644 continue; 12645 12646 if (strcmp(desc->dtpd_func, "ustack") != 0) 12647 continue; 12648 12649 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 12650 ep)) != 0) { 12651 /* 12652 * Adding this helper action failed -- we are now going 12653 * to rip out the entire generation and return failure. 12654 */ 12655 (void) dtrace_helper_destroygen(help->dthps_generation); 12656 dtrace_enabling_destroy(enab); 12657 dtrace_dof_destroy(dof); 12658 dtrace_err = rv; 12659 return (-1); 12660 } 12661 12662 nhelpers++; 12663 } 12664 12665 if (nhelpers < enab->dten_ndesc) 12666 dtrace_dof_error(dof, "unmatched helpers"); 12667 12668 if (dhp != NULL) { 12669 uintptr_t daddr = (uintptr_t)dof; 12670 int err = 0, count = 0; 12671 12672 /* 12673 * Look for helper probes. 12674 */ 12675 for (i = 0; i < dof->dofh_secnum; i++) { 12676 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 12677 dof->dofh_secoff + i * dof->dofh_secsize); 12678 12679 if (sec->dofs_type != DOF_SECT_PROVIDER) 12680 continue; 12681 12682 if (dtrace_helper_provider_validate(dof, sec) != 0) { 12683 err = 1; 12684 break; 12685 } 12686 12687 count++; 12688 } 12689 12690 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 12691 if (err == 0 && count > 0 && 12692 dtrace_helper_provider_add(dhp) == 0) 12693 destroy = 0; 12694 else 12695 dhp = NULL; 12696 } 12697 12698 gen = help->dthps_generation++; 12699 dtrace_enabling_destroy(enab); 12700 12701 if (dhp != NULL) { 12702 mutex_exit(&dtrace_lock); 12703 dtrace_helper_provider_register(curproc, help, dhp); 12704 mutex_enter(&dtrace_lock); 12705 } 12706 12707 if (destroy) 12708 dtrace_dof_destroy(dof); 12709 12710 return (gen); 12711 } 12712 12713 static dtrace_helpers_t * 12714 dtrace_helpers_create(proc_t *p) 12715 { 12716 dtrace_helpers_t *help; 12717 12718 ASSERT(MUTEX_HELD(&dtrace_lock)); 12719 ASSERT(p->p_dtrace_helpers == NULL); 12720 12721 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 12722 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 12723 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 12724 12725 p->p_dtrace_helpers = help; 12726 dtrace_helpers++; 12727 12728 return (help); 12729 } 12730 12731 static void 12732 dtrace_helpers_destroy(void) 12733 { 12734 dtrace_helpers_t *help; 12735 dtrace_vstate_t *vstate; 12736 proc_t *p = curproc; 12737 int i; 12738 12739 mutex_enter(&dtrace_lock); 12740 12741 ASSERT(p->p_dtrace_helpers != NULL); 12742 ASSERT(dtrace_helpers > 0); 12743 12744 help = p->p_dtrace_helpers; 12745 vstate = &help->dthps_vstate; 12746 12747 /* 12748 * We're now going to lose the help from this process. 12749 */ 12750 p->p_dtrace_helpers = NULL; 12751 dtrace_sync(); 12752 12753 /* 12754 * Destory the helper actions. 12755 */ 12756 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12757 dtrace_helper_action_t *h, *next; 12758 12759 for (h = help->dthps_actions[i]; h != NULL; h = next) { 12760 next = h->dthp_next; 12761 dtrace_helper_destroy(h, vstate); 12762 h = next; 12763 } 12764 } 12765 12766 mutex_exit(&dtrace_lock); 12767 12768 /* 12769 * Destroy the helper providers. 12770 */ 12771 if (help->dthps_nprovs > 0) { 12772 mutex_enter(&dtrace_meta_lock); 12773 if (dtrace_meta_pid != NULL) { 12774 ASSERT(dtrace_deferred_pid == NULL); 12775 12776 for (i = 0; i < help->dthps_nprovs; i++) { 12777 dtrace_helper_remove( 12778 &help->dthps_provs[i]->dthp_prov, p->p_pid); 12779 } 12780 } else { 12781 mutex_enter(&dtrace_lock); 12782 ASSERT(help->dthps_deferred == 0 || 12783 help->dthps_next != NULL || 12784 help->dthps_prev != NULL || 12785 help == dtrace_deferred_pid); 12786 12787 /* 12788 * Remove the helper from the deferred list. 12789 */ 12790 if (help->dthps_next != NULL) 12791 help->dthps_next->dthps_prev = help->dthps_prev; 12792 if (help->dthps_prev != NULL) 12793 help->dthps_prev->dthps_next = help->dthps_next; 12794 if (dtrace_deferred_pid == help) { 12795 dtrace_deferred_pid = help->dthps_next; 12796 ASSERT(help->dthps_prev == NULL); 12797 } 12798 12799 mutex_exit(&dtrace_lock); 12800 } 12801 12802 mutex_exit(&dtrace_meta_lock); 12803 12804 for (i = 0; i < help->dthps_nprovs; i++) { 12805 dtrace_helper_provider_remove(help->dthps_provs[i]); 12806 } 12807 12808 kmem_free(help->dthps_provs, help->dthps_nprovs * 12809 sizeof (dtrace_helper_provider_t *)); 12810 } 12811 12812 mutex_enter(&dtrace_lock); 12813 12814 dtrace_vstate_fini(&help->dthps_vstate); 12815 kmem_free(help->dthps_actions, 12816 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 12817 kmem_free(help, sizeof (dtrace_helpers_t)); 12818 12819 --dtrace_helpers; 12820 mutex_exit(&dtrace_lock); 12821 } 12822 12823 static void 12824 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 12825 { 12826 dtrace_helpers_t *help, *newhelp; 12827 dtrace_helper_action_t *helper, *new, *last; 12828 dtrace_difo_t *dp; 12829 dtrace_vstate_t *vstate; 12830 int i, j, sz, hasprovs = 0; 12831 12832 mutex_enter(&dtrace_lock); 12833 ASSERT(from->p_dtrace_helpers != NULL); 12834 ASSERT(dtrace_helpers > 0); 12835 12836 help = from->p_dtrace_helpers; 12837 newhelp = dtrace_helpers_create(to); 12838 ASSERT(to->p_dtrace_helpers != NULL); 12839 12840 newhelp->dthps_generation = help->dthps_generation; 12841 vstate = &newhelp->dthps_vstate; 12842 12843 /* 12844 * Duplicate the helper actions. 12845 */ 12846 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12847 if ((helper = help->dthps_actions[i]) == NULL) 12848 continue; 12849 12850 for (last = NULL; helper != NULL; helper = helper->dthp_next) { 12851 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 12852 KM_SLEEP); 12853 new->dthp_generation = helper->dthp_generation; 12854 12855 if ((dp = helper->dthp_predicate) != NULL) { 12856 dp = dtrace_difo_duplicate(dp, vstate); 12857 new->dthp_predicate = dp; 12858 } 12859 12860 new->dthp_nactions = helper->dthp_nactions; 12861 sz = sizeof (dtrace_difo_t *) * new->dthp_nactions; 12862 new->dthp_actions = kmem_alloc(sz, KM_SLEEP); 12863 12864 for (j = 0; j < new->dthp_nactions; j++) { 12865 dtrace_difo_t *dp = helper->dthp_actions[j]; 12866 12867 ASSERT(dp != NULL); 12868 dp = dtrace_difo_duplicate(dp, vstate); 12869 new->dthp_actions[j] = dp; 12870 } 12871 12872 if (last != NULL) { 12873 last->dthp_next = new; 12874 } else { 12875 newhelp->dthps_actions[i] = new; 12876 } 12877 12878 last = new; 12879 } 12880 } 12881 12882 /* 12883 * Duplicate the helper providers and register them with the 12884 * DTrace framework. 12885 */ 12886 if (help->dthps_nprovs > 0) { 12887 newhelp->dthps_nprovs = help->dthps_nprovs; 12888 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 12889 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 12890 for (i = 0; i < newhelp->dthps_nprovs; i++) { 12891 newhelp->dthps_provs[i] = help->dthps_provs[i]; 12892 newhelp->dthps_provs[i]->dthp_ref++; 12893 } 12894 12895 hasprovs = 1; 12896 } 12897 12898 mutex_exit(&dtrace_lock); 12899 12900 if (hasprovs) 12901 dtrace_helper_provider_register(to, newhelp, NULL); 12902 } 12903 12904 /* 12905 * DTrace Hook Functions 12906 */ 12907 static void 12908 dtrace_module_loaded(struct modctl *ctl) 12909 { 12910 dtrace_provider_t *prv; 12911 12912 mutex_enter(&dtrace_provider_lock); 12913 mutex_enter(&mod_lock); 12914 12915 ASSERT(ctl->mod_busy); 12916 12917 /* 12918 * We're going to call each providers per-module provide operation 12919 * specifying only this module. 12920 */ 12921 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 12922 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 12923 12924 mutex_exit(&mod_lock); 12925 mutex_exit(&dtrace_provider_lock); 12926 12927 /* 12928 * If we have any retained enablings, we need to match against them. 12929 * Enabling probes requires that cpu_lock be held, and we cannot hold 12930 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 12931 * module. (In particular, this happens when loading scheduling 12932 * classes.) So if we have any retained enablings, we need to dispatch 12933 * our task queue to do the match for us. 12934 */ 12935 mutex_enter(&dtrace_lock); 12936 12937 if (dtrace_retained == NULL) { 12938 mutex_exit(&dtrace_lock); 12939 return; 12940 } 12941 12942 (void) taskq_dispatch(dtrace_taskq, 12943 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 12944 12945 mutex_exit(&dtrace_lock); 12946 12947 /* 12948 * And now, for a little heuristic sleaze: in general, we want to 12949 * match modules as soon as they load. However, we cannot guarantee 12950 * this, because it would lead us to the lock ordering violation 12951 * outlined above. The common case, of course, is that cpu_lock is 12952 * _not_ held -- so we delay here for a clock tick, hoping that that's 12953 * long enough for the task queue to do its work. If it's not, it's 12954 * not a serious problem -- it just means that the module that we 12955 * just loaded may not be immediately instrumentable. 12956 */ 12957 delay(1); 12958 } 12959 12960 static void 12961 dtrace_module_unloaded(struct modctl *ctl) 12962 { 12963 dtrace_probe_t template, *probe, *first, *next; 12964 dtrace_provider_t *prov; 12965 12966 template.dtpr_mod = ctl->mod_modname; 12967 12968 mutex_enter(&dtrace_provider_lock); 12969 mutex_enter(&mod_lock); 12970 mutex_enter(&dtrace_lock); 12971 12972 if (dtrace_bymod == NULL) { 12973 /* 12974 * The DTrace module is loaded (obviously) but not attached; 12975 * we don't have any work to do. 12976 */ 12977 mutex_exit(&dtrace_provider_lock); 12978 mutex_exit(&mod_lock); 12979 mutex_exit(&dtrace_lock); 12980 return; 12981 } 12982 12983 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 12984 probe != NULL; probe = probe->dtpr_nextmod) { 12985 if (probe->dtpr_ecb != NULL) { 12986 mutex_exit(&dtrace_provider_lock); 12987 mutex_exit(&mod_lock); 12988 mutex_exit(&dtrace_lock); 12989 12990 /* 12991 * This shouldn't _actually_ be possible -- we're 12992 * unloading a module that has an enabled probe in it. 12993 * (It's normally up to the provider to make sure that 12994 * this can't happen.) However, because dtps_enable() 12995 * doesn't have a failure mode, there can be an 12996 * enable/unload race. Upshot: we don't want to 12997 * assert, but we're not going to disable the 12998 * probe, either. 12999 */ 13000 if (dtrace_err_verbose) { 13001 cmn_err(CE_WARN, "unloaded module '%s' had " 13002 "enabled probes", ctl->mod_modname); 13003 } 13004 13005 return; 13006 } 13007 } 13008 13009 probe = first; 13010 13011 for (first = NULL; probe != NULL; probe = next) { 13012 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 13013 13014 dtrace_probes[probe->dtpr_id - 1] = NULL; 13015 13016 next = probe->dtpr_nextmod; 13017 dtrace_hash_remove(dtrace_bymod, probe); 13018 dtrace_hash_remove(dtrace_byfunc, probe); 13019 dtrace_hash_remove(dtrace_byname, probe); 13020 13021 if (first == NULL) { 13022 first = probe; 13023 probe->dtpr_nextmod = NULL; 13024 } else { 13025 probe->dtpr_nextmod = first; 13026 first = probe; 13027 } 13028 } 13029 13030 /* 13031 * We've removed all of the module's probes from the hash chains and 13032 * from the probe array. Now issue a dtrace_sync() to be sure that 13033 * everyone has cleared out from any probe array processing. 13034 */ 13035 dtrace_sync(); 13036 13037 for (probe = first; probe != NULL; probe = first) { 13038 first = probe->dtpr_nextmod; 13039 prov = probe->dtpr_provider; 13040 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 13041 probe->dtpr_arg); 13042 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 13043 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 13044 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 13045 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 13046 kmem_free(probe, sizeof (dtrace_probe_t)); 13047 } 13048 13049 mutex_exit(&dtrace_lock); 13050 mutex_exit(&mod_lock); 13051 mutex_exit(&dtrace_provider_lock); 13052 } 13053 13054 void 13055 dtrace_suspend(void) 13056 { 13057 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 13058 } 13059 13060 void 13061 dtrace_resume(void) 13062 { 13063 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 13064 } 13065 13066 static int 13067 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 13068 { 13069 ASSERT(MUTEX_HELD(&cpu_lock)); 13070 mutex_enter(&dtrace_lock); 13071 13072 switch (what) { 13073 case CPU_CONFIG: { 13074 dtrace_state_t *state; 13075 dtrace_optval_t *opt, rs, c; 13076 13077 /* 13078 * For now, we only allocate a new buffer for anonymous state. 13079 */ 13080 if ((state = dtrace_anon.dta_state) == NULL) 13081 break; 13082 13083 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 13084 break; 13085 13086 opt = state->dts_options; 13087 c = opt[DTRACEOPT_CPU]; 13088 13089 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 13090 break; 13091 13092 /* 13093 * Regardless of what the actual policy is, we're going to 13094 * temporarily set our resize policy to be manual. We're 13095 * also going to temporarily set our CPU option to denote 13096 * the newly configured CPU. 13097 */ 13098 rs = opt[DTRACEOPT_BUFRESIZE]; 13099 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 13100 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 13101 13102 (void) dtrace_state_buffers(state); 13103 13104 opt[DTRACEOPT_BUFRESIZE] = rs; 13105 opt[DTRACEOPT_CPU] = c; 13106 13107 break; 13108 } 13109 13110 case CPU_UNCONFIG: 13111 /* 13112 * We don't free the buffer in the CPU_UNCONFIG case. (The 13113 * buffer will be freed when the consumer exits.) 13114 */ 13115 break; 13116 13117 default: 13118 break; 13119 } 13120 13121 mutex_exit(&dtrace_lock); 13122 return (0); 13123 } 13124 13125 static void 13126 dtrace_cpu_setup_initial(processorid_t cpu) 13127 { 13128 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 13129 } 13130 13131 static void 13132 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 13133 { 13134 if (dtrace_toxranges >= dtrace_toxranges_max) { 13135 int osize, nsize; 13136 dtrace_toxrange_t *range; 13137 13138 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 13139 13140 if (osize == 0) { 13141 ASSERT(dtrace_toxrange == NULL); 13142 ASSERT(dtrace_toxranges_max == 0); 13143 dtrace_toxranges_max = 1; 13144 } else { 13145 dtrace_toxranges_max <<= 1; 13146 } 13147 13148 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 13149 range = kmem_zalloc(nsize, KM_SLEEP); 13150 13151 if (dtrace_toxrange != NULL) { 13152 ASSERT(osize != 0); 13153 bcopy(dtrace_toxrange, range, osize); 13154 kmem_free(dtrace_toxrange, osize); 13155 } 13156 13157 dtrace_toxrange = range; 13158 } 13159 13160 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); 13161 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); 13162 13163 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 13164 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 13165 dtrace_toxranges++; 13166 } 13167 13168 /* 13169 * DTrace Driver Cookbook Functions 13170 */ 13171 /*ARGSUSED*/ 13172 static int 13173 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 13174 { 13175 dtrace_provider_id_t id; 13176 dtrace_state_t *state = NULL; 13177 dtrace_enabling_t *enab; 13178 13179 mutex_enter(&cpu_lock); 13180 mutex_enter(&dtrace_provider_lock); 13181 mutex_enter(&dtrace_lock); 13182 13183 if (ddi_soft_state_init(&dtrace_softstate, sizeof (dtrace_state_t) + 13184 NCPU * sizeof (dtrace_buffer_t), 0) != 0) { 13185 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 13186 mutex_exit(&cpu_lock); 13187 mutex_exit(&dtrace_provider_lock); 13188 mutex_exit(&dtrace_lock); 13189 return (DDI_FAILURE); 13190 } 13191 13192 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 13193 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 13194 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 13195 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 13196 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 13197 ddi_remove_minor_node(devi, NULL); 13198 ddi_soft_state_fini(&dtrace_softstate); 13199 mutex_exit(&cpu_lock); 13200 mutex_exit(&dtrace_provider_lock); 13201 mutex_exit(&dtrace_lock); 13202 return (DDI_FAILURE); 13203 } 13204 13205 ddi_report_dev(devi); 13206 dtrace_devi = devi; 13207 13208 dtrace_modload = dtrace_module_loaded; 13209 dtrace_modunload = dtrace_module_unloaded; 13210 dtrace_cpu_init = dtrace_cpu_setup_initial; 13211 dtrace_helpers_cleanup = dtrace_helpers_destroy; 13212 dtrace_helpers_fork = dtrace_helpers_duplicate; 13213 dtrace_cpustart_init = dtrace_suspend; 13214 dtrace_cpustart_fini = dtrace_resume; 13215 dtrace_debugger_init = dtrace_suspend; 13216 dtrace_debugger_fini = dtrace_resume; 13217 dtrace_kreloc_init = dtrace_suspend; 13218 dtrace_kreloc_fini = dtrace_resume; 13219 13220 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 13221 13222 ASSERT(MUTEX_HELD(&cpu_lock)); 13223 13224 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 13225 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 13226 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 13227 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 13228 VM_SLEEP | VMC_IDENTIFIER); 13229 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 13230 1, INT_MAX, 0); 13231 13232 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 13233 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 13234 NULL, NULL, NULL, NULL, NULL, 0); 13235 13236 ASSERT(MUTEX_HELD(&cpu_lock)); 13237 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 13238 offsetof(dtrace_probe_t, dtpr_nextmod), 13239 offsetof(dtrace_probe_t, dtpr_prevmod)); 13240 13241 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 13242 offsetof(dtrace_probe_t, dtpr_nextfunc), 13243 offsetof(dtrace_probe_t, dtpr_prevfunc)); 13244 13245 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 13246 offsetof(dtrace_probe_t, dtpr_nextname), 13247 offsetof(dtrace_probe_t, dtpr_prevname)); 13248 13249 if (dtrace_retain_max < 1) { 13250 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 13251 "setting to 1", dtrace_retain_max); 13252 dtrace_retain_max = 1; 13253 } 13254 13255 /* 13256 * Now discover our toxic ranges. 13257 */ 13258 dtrace_toxic_ranges(dtrace_toxrange_add); 13259 13260 /* 13261 * Before we register ourselves as a provider to our own framework, 13262 * we would like to assert that dtrace_provider is NULL -- but that's 13263 * not true if we were loaded as a dependency of a DTrace provider. 13264 * Once we've registered, we can assert that dtrace_provider is our 13265 * pseudo provider. 13266 */ 13267 (void) dtrace_register("dtrace", &dtrace_provider_attr, 13268 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 13269 13270 ASSERT(dtrace_provider != NULL); 13271 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 13272 13273 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 13274 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 13275 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 13276 dtrace_provider, NULL, NULL, "END", 0, NULL); 13277 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 13278 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 13279 13280 dtrace_anon_property(); 13281 mutex_exit(&cpu_lock); 13282 13283 /* 13284 * If DTrace helper tracing is enabled, we need to allocate the 13285 * trace buffer and initialize the values. 13286 */ 13287 if (dtrace_helptrace_enabled) { 13288 ASSERT(dtrace_helptrace_buffer == NULL); 13289 dtrace_helptrace_buffer = 13290 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 13291 dtrace_helptrace_next = 0; 13292 } 13293 13294 /* 13295 * If there are already providers, we must ask them to provide their 13296 * probes, and then match any anonymous enabling against them. Note 13297 * that there should be no other retained enablings at this time: 13298 * the only retained enablings at this time should be the anonymous 13299 * enabling. 13300 */ 13301 if (dtrace_anon.dta_enabling != NULL) { 13302 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 13303 13304 dtrace_enabling_provide(NULL); 13305 state = dtrace_anon.dta_state; 13306 13307 /* 13308 * We couldn't hold cpu_lock across the above call to 13309 * dtrace_enabling_provide(), but we must hold it to actually 13310 * enable the probes. We have to drop all of our locks, pick 13311 * up cpu_lock, and regain our locks before matching the 13312 * retained anonymous enabling. 13313 */ 13314 mutex_exit(&dtrace_lock); 13315 mutex_exit(&dtrace_provider_lock); 13316 13317 mutex_enter(&cpu_lock); 13318 mutex_enter(&dtrace_provider_lock); 13319 mutex_enter(&dtrace_lock); 13320 13321 if ((enab = dtrace_anon.dta_enabling) != NULL) 13322 (void) dtrace_enabling_match(enab, NULL); 13323 13324 mutex_exit(&cpu_lock); 13325 } 13326 13327 mutex_exit(&dtrace_lock); 13328 mutex_exit(&dtrace_provider_lock); 13329 13330 if (state != NULL) { 13331 /* 13332 * If we created any anonymous state, set it going now. 13333 */ 13334 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 13335 } 13336 13337 return (DDI_SUCCESS); 13338 } 13339 13340 /*ARGSUSED*/ 13341 static int 13342 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 13343 { 13344 dtrace_state_t *state; 13345 uint32_t priv; 13346 uid_t uid; 13347 zoneid_t zoneid; 13348 13349 if (getminor(*devp) == DTRACEMNRN_HELPER) 13350 return (0); 13351 13352 /* 13353 * If this wasn't an open with the "helper" minor, then it must be 13354 * the "dtrace" minor. 13355 */ 13356 ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE); 13357 13358 /* 13359 * If no DTRACE_PRIV_* bits are set in the credential, then the 13360 * caller lacks sufficient permission to do anything with DTrace. 13361 */ 13362 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 13363 if (priv == DTRACE_PRIV_NONE) 13364 return (EACCES); 13365 13366 /* 13367 * Ask all providers to provide all their probes. 13368 */ 13369 mutex_enter(&dtrace_provider_lock); 13370 dtrace_probe_provide(NULL, NULL); 13371 mutex_exit(&dtrace_provider_lock); 13372 13373 mutex_enter(&cpu_lock); 13374 mutex_enter(&dtrace_lock); 13375 dtrace_opens++; 13376 dtrace_membar_producer(); 13377 13378 /* 13379 * If the kernel debugger is active (that is, if the kernel debugger 13380 * modified text in some way), we won't allow the open. 13381 */ 13382 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13383 dtrace_opens--; 13384 mutex_exit(&cpu_lock); 13385 mutex_exit(&dtrace_lock); 13386 return (EBUSY); 13387 } 13388 13389 state = dtrace_state_create(devp, cred_p); 13390 mutex_exit(&cpu_lock); 13391 13392 if (state == NULL) { 13393 if (--dtrace_opens == 0) 13394 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13395 mutex_exit(&dtrace_lock); 13396 return (EAGAIN); 13397 } 13398 13399 mutex_exit(&dtrace_lock); 13400 13401 return (0); 13402 } 13403 13404 /*ARGSUSED*/ 13405 static int 13406 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 13407 { 13408 minor_t minor = getminor(dev); 13409 dtrace_state_t *state; 13410 13411 if (minor == DTRACEMNRN_HELPER) 13412 return (0); 13413 13414 state = ddi_get_soft_state(dtrace_softstate, minor); 13415 13416 mutex_enter(&cpu_lock); 13417 mutex_enter(&dtrace_lock); 13418 13419 if (state->dts_anon) { 13420 /* 13421 * There is anonymous state. Destroy that first. 13422 */ 13423 ASSERT(dtrace_anon.dta_state == NULL); 13424 dtrace_state_destroy(state->dts_anon); 13425 } 13426 13427 dtrace_state_destroy(state); 13428 ASSERT(dtrace_opens > 0); 13429 if (--dtrace_opens == 0) 13430 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13431 13432 mutex_exit(&dtrace_lock); 13433 mutex_exit(&cpu_lock); 13434 13435 return (0); 13436 } 13437 13438 /*ARGSUSED*/ 13439 static int 13440 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 13441 { 13442 int rval; 13443 dof_helper_t help, *dhp = NULL; 13444 13445 switch (cmd) { 13446 case DTRACEHIOC_ADDDOF: 13447 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 13448 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 13449 return (EFAULT); 13450 } 13451 13452 dhp = &help; 13453 arg = (intptr_t)help.dofhp_dof; 13454 /*FALLTHROUGH*/ 13455 13456 case DTRACEHIOC_ADD: { 13457 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 13458 13459 if (dof == NULL) 13460 return (rval); 13461 13462 mutex_enter(&dtrace_lock); 13463 dtrace_err = 0; 13464 13465 /* 13466 * dtrace_helper_slurp() takes responsibility for the dof -- 13467 * it may free it now or it may save it and free it later. 13468 */ 13469 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 13470 *rv = rval; 13471 rval = 0; 13472 } else { 13473 rval = EINVAL; 13474 } 13475 13476 mutex_exit(&dtrace_lock); 13477 return (rval); 13478 } 13479 13480 case DTRACEHIOC_REMOVE: { 13481 mutex_enter(&dtrace_lock); 13482 rval = dtrace_helper_destroygen(arg); 13483 mutex_exit(&dtrace_lock); 13484 13485 return (rval); 13486 } 13487 13488 default: 13489 break; 13490 } 13491 13492 return (ENOTTY); 13493 } 13494 13495 /*ARGSUSED*/ 13496 static int 13497 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 13498 { 13499 minor_t minor = getminor(dev); 13500 dtrace_state_t *state; 13501 int rval; 13502 13503 if (minor == DTRACEMNRN_HELPER) 13504 return (dtrace_ioctl_helper(cmd, arg, rv)); 13505 13506 state = ddi_get_soft_state(dtrace_softstate, minor); 13507 13508 if (state->dts_anon) { 13509 ASSERT(dtrace_anon.dta_state == NULL); 13510 state = state->dts_anon; 13511 } 13512 13513 switch (cmd) { 13514 case DTRACEIOC_PROVIDER: { 13515 dtrace_providerdesc_t pvd; 13516 dtrace_provider_t *pvp; 13517 13518 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 13519 return (EFAULT); 13520 13521 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 13522 mutex_enter(&dtrace_provider_lock); 13523 13524 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 13525 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 13526 break; 13527 } 13528 13529 mutex_exit(&dtrace_provider_lock); 13530 13531 if (pvp == NULL) 13532 return (ESRCH); 13533 13534 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 13535 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 13536 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 13537 return (EFAULT); 13538 13539 return (0); 13540 } 13541 13542 case DTRACEIOC_EPROBE: { 13543 dtrace_eprobedesc_t epdesc; 13544 dtrace_ecb_t *ecb; 13545 dtrace_action_t *act; 13546 void *buf; 13547 size_t size; 13548 uintptr_t dest; 13549 int nrecs; 13550 13551 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 13552 return (EFAULT); 13553 13554 mutex_enter(&dtrace_lock); 13555 13556 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 13557 mutex_exit(&dtrace_lock); 13558 return (EINVAL); 13559 } 13560 13561 if (ecb->dte_probe == NULL) { 13562 mutex_exit(&dtrace_lock); 13563 return (EINVAL); 13564 } 13565 13566 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 13567 epdesc.dtepd_uarg = ecb->dte_uarg; 13568 epdesc.dtepd_size = ecb->dte_size; 13569 13570 nrecs = epdesc.dtepd_nrecs; 13571 epdesc.dtepd_nrecs = 0; 13572 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 13573 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 13574 continue; 13575 13576 epdesc.dtepd_nrecs++; 13577 } 13578 13579 /* 13580 * Now that we have the size, we need to allocate a temporary 13581 * buffer in which to store the complete description. We need 13582 * the temporary buffer to be able to drop dtrace_lock() 13583 * across the copyout(), below. 13584 */ 13585 size = sizeof (dtrace_eprobedesc_t) + 13586 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 13587 13588 buf = kmem_alloc(size, KM_SLEEP); 13589 dest = (uintptr_t)buf; 13590 13591 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 13592 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 13593 13594 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 13595 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 13596 continue; 13597 13598 if (nrecs-- == 0) 13599 break; 13600 13601 bcopy(&act->dta_rec, (void *)dest, 13602 sizeof (dtrace_recdesc_t)); 13603 dest += sizeof (dtrace_recdesc_t); 13604 } 13605 13606 mutex_exit(&dtrace_lock); 13607 13608 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 13609 kmem_free(buf, size); 13610 return (EFAULT); 13611 } 13612 13613 kmem_free(buf, size); 13614 return (0); 13615 } 13616 13617 case DTRACEIOC_AGGDESC: { 13618 dtrace_aggdesc_t aggdesc; 13619 dtrace_action_t *act; 13620 dtrace_aggregation_t *agg; 13621 int nrecs; 13622 uint32_t offs; 13623 dtrace_recdesc_t *lrec; 13624 void *buf; 13625 size_t size; 13626 uintptr_t dest; 13627 13628 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 13629 return (EFAULT); 13630 13631 mutex_enter(&dtrace_lock); 13632 13633 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 13634 mutex_exit(&dtrace_lock); 13635 return (EINVAL); 13636 } 13637 13638 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 13639 13640 nrecs = aggdesc.dtagd_nrecs; 13641 aggdesc.dtagd_nrecs = 0; 13642 13643 offs = agg->dtag_base; 13644 lrec = &agg->dtag_action.dta_rec; 13645 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 13646 13647 for (act = agg->dtag_first; ; act = act->dta_next) { 13648 ASSERT(act->dta_intuple || 13649 DTRACEACT_ISAGG(act->dta_kind)); 13650 13651 /* 13652 * If this action has a record size of zero, it 13653 * denotes an argument to the aggregating action. 13654 * Because the presence of this record doesn't (or 13655 * shouldn't) affect the way the data is interpreted, 13656 * we don't copy it out to save user-level the 13657 * confusion of dealing with a zero-length record. 13658 */ 13659 if (act->dta_rec.dtrd_size == 0) { 13660 ASSERT(agg->dtag_hasarg); 13661 continue; 13662 } 13663 13664 aggdesc.dtagd_nrecs++; 13665 13666 if (act == &agg->dtag_action) 13667 break; 13668 } 13669 13670 /* 13671 * Now that we have the size, we need to allocate a temporary 13672 * buffer in which to store the complete description. We need 13673 * the temporary buffer to be able to drop dtrace_lock() 13674 * across the copyout(), below. 13675 */ 13676 size = sizeof (dtrace_aggdesc_t) + 13677 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 13678 13679 buf = kmem_alloc(size, KM_SLEEP); 13680 dest = (uintptr_t)buf; 13681 13682 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 13683 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 13684 13685 for (act = agg->dtag_first; ; act = act->dta_next) { 13686 dtrace_recdesc_t rec = act->dta_rec; 13687 13688 /* 13689 * See the comment in the above loop for why we pass 13690 * over zero-length records. 13691 */ 13692 if (rec.dtrd_size == 0) { 13693 ASSERT(agg->dtag_hasarg); 13694 continue; 13695 } 13696 13697 if (nrecs-- == 0) 13698 break; 13699 13700 rec.dtrd_offset -= offs; 13701 bcopy(&rec, (void *)dest, sizeof (rec)); 13702 dest += sizeof (dtrace_recdesc_t); 13703 13704 if (act == &agg->dtag_action) 13705 break; 13706 } 13707 13708 mutex_exit(&dtrace_lock); 13709 13710 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 13711 kmem_free(buf, size); 13712 return (EFAULT); 13713 } 13714 13715 kmem_free(buf, size); 13716 return (0); 13717 } 13718 13719 case DTRACEIOC_ENABLE: { 13720 dof_hdr_t *dof; 13721 dtrace_enabling_t *enab = NULL; 13722 dtrace_vstate_t *vstate; 13723 int err = 0; 13724 13725 *rv = 0; 13726 13727 /* 13728 * If a NULL argument has been passed, we take this as our 13729 * cue to reevaluate our enablings. 13730 */ 13731 if (arg == NULL) { 13732 mutex_enter(&cpu_lock); 13733 mutex_enter(&dtrace_lock); 13734 err = dtrace_enabling_matchstate(state, rv); 13735 mutex_exit(&dtrace_lock); 13736 mutex_exit(&cpu_lock); 13737 13738 return (err); 13739 } 13740 13741 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 13742 return (rval); 13743 13744 mutex_enter(&cpu_lock); 13745 mutex_enter(&dtrace_lock); 13746 vstate = &state->dts_vstate; 13747 13748 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13749 mutex_exit(&dtrace_lock); 13750 mutex_exit(&cpu_lock); 13751 dtrace_dof_destroy(dof); 13752 return (EBUSY); 13753 } 13754 13755 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 13756 mutex_exit(&dtrace_lock); 13757 mutex_exit(&cpu_lock); 13758 dtrace_dof_destroy(dof); 13759 return (EINVAL); 13760 } 13761 13762 if ((rval = dtrace_dof_options(dof, state)) != 0) { 13763 dtrace_enabling_destroy(enab); 13764 mutex_exit(&dtrace_lock); 13765 mutex_exit(&cpu_lock); 13766 dtrace_dof_destroy(dof); 13767 return (rval); 13768 } 13769 13770 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 13771 err = dtrace_enabling_retain(enab); 13772 } else { 13773 dtrace_enabling_destroy(enab); 13774 } 13775 13776 mutex_exit(&cpu_lock); 13777 mutex_exit(&dtrace_lock); 13778 dtrace_dof_destroy(dof); 13779 13780 return (err); 13781 } 13782 13783 case DTRACEIOC_REPLICATE: { 13784 dtrace_repldesc_t desc; 13785 dtrace_probedesc_t *match = &desc.dtrpd_match; 13786 dtrace_probedesc_t *create = &desc.dtrpd_create; 13787 int err; 13788 13789 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13790 return (EFAULT); 13791 13792 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13793 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13794 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13795 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13796 13797 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13798 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13799 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13800 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13801 13802 mutex_enter(&dtrace_lock); 13803 err = dtrace_enabling_replicate(state, match, create); 13804 mutex_exit(&dtrace_lock); 13805 13806 return (err); 13807 } 13808 13809 case DTRACEIOC_PROBEMATCH: 13810 case DTRACEIOC_PROBES: { 13811 dtrace_probe_t *probe = NULL; 13812 dtrace_probedesc_t desc; 13813 dtrace_probekey_t pkey; 13814 dtrace_id_t i; 13815 int m = 0; 13816 uint32_t priv; 13817 uid_t uid; 13818 zoneid_t zoneid; 13819 13820 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13821 return (EFAULT); 13822 13823 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13824 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13825 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13826 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13827 13828 /* 13829 * Before we attempt to match this probe, we want to give 13830 * all providers the opportunity to provide it. 13831 */ 13832 if (desc.dtpd_id == DTRACE_IDNONE) { 13833 mutex_enter(&dtrace_provider_lock); 13834 dtrace_probe_provide(&desc, NULL); 13835 mutex_exit(&dtrace_provider_lock); 13836 desc.dtpd_id++; 13837 } 13838 13839 if (cmd == DTRACEIOC_PROBEMATCH) { 13840 dtrace_probekey(&desc, &pkey); 13841 pkey.dtpk_id = DTRACE_IDNONE; 13842 } 13843 13844 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 13845 13846 mutex_enter(&dtrace_lock); 13847 13848 if (cmd == DTRACEIOC_PROBEMATCH) { 13849 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 13850 if ((probe = dtrace_probes[i - 1]) != NULL && 13851 (m = dtrace_match_probe(probe, &pkey, 13852 priv, uid, zoneid)) != 0) 13853 break; 13854 } 13855 13856 if (m < 0) { 13857 mutex_exit(&dtrace_lock); 13858 return (EINVAL); 13859 } 13860 13861 } else { 13862 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 13863 if ((probe = dtrace_probes[i - 1]) != NULL && 13864 dtrace_match_priv(probe, priv, uid, zoneid)) 13865 break; 13866 } 13867 } 13868 13869 if (probe == NULL) { 13870 mutex_exit(&dtrace_lock); 13871 return (ESRCH); 13872 } 13873 13874 dtrace_probe_description(probe, &desc); 13875 mutex_exit(&dtrace_lock); 13876 13877 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13878 return (EFAULT); 13879 13880 return (0); 13881 } 13882 13883 case DTRACEIOC_PROBEARG: { 13884 dtrace_argdesc_t desc; 13885 dtrace_probe_t *probe; 13886 dtrace_provider_t *prov; 13887 13888 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13889 return (EFAULT); 13890 13891 if (desc.dtargd_id == DTRACE_IDNONE) 13892 return (EINVAL); 13893 13894 if (desc.dtargd_ndx == DTRACE_ARGNONE) 13895 return (EINVAL); 13896 13897 mutex_enter(&dtrace_provider_lock); 13898 mutex_enter(&mod_lock); 13899 mutex_enter(&dtrace_lock); 13900 13901 if (desc.dtargd_id > dtrace_nprobes) { 13902 mutex_exit(&dtrace_lock); 13903 mutex_exit(&mod_lock); 13904 mutex_exit(&dtrace_provider_lock); 13905 return (EINVAL); 13906 } 13907 13908 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 13909 mutex_exit(&dtrace_lock); 13910 mutex_exit(&mod_lock); 13911 mutex_exit(&dtrace_provider_lock); 13912 return (EINVAL); 13913 } 13914 13915 mutex_exit(&dtrace_lock); 13916 13917 prov = probe->dtpr_provider; 13918 13919 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 13920 /* 13921 * There isn't any typed information for this probe. 13922 * Set the argument number to DTRACE_ARGNONE. 13923 */ 13924 desc.dtargd_ndx = DTRACE_ARGNONE; 13925 } else { 13926 desc.dtargd_native[0] = '\0'; 13927 desc.dtargd_xlate[0] = '\0'; 13928 desc.dtargd_mapping = desc.dtargd_ndx; 13929 13930 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 13931 probe->dtpr_id, probe->dtpr_arg, &desc); 13932 } 13933 13934 mutex_exit(&mod_lock); 13935 mutex_exit(&dtrace_provider_lock); 13936 13937 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13938 return (EFAULT); 13939 13940 return (0); 13941 } 13942 13943 case DTRACEIOC_GO: { 13944 processorid_t cpuid; 13945 rval = dtrace_state_go(state, &cpuid); 13946 13947 if (rval != 0) 13948 return (rval); 13949 13950 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 13951 return (EFAULT); 13952 13953 return (0); 13954 } 13955 13956 case DTRACEIOC_STOP: { 13957 processorid_t cpuid; 13958 13959 mutex_enter(&dtrace_lock); 13960 rval = dtrace_state_stop(state, &cpuid); 13961 mutex_exit(&dtrace_lock); 13962 13963 if (rval != 0) 13964 return (rval); 13965 13966 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 13967 return (EFAULT); 13968 13969 return (0); 13970 } 13971 13972 case DTRACEIOC_DOFGET: { 13973 dof_hdr_t hdr, *dof; 13974 uint64_t len; 13975 13976 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 13977 return (EFAULT); 13978 13979 mutex_enter(&dtrace_lock); 13980 dof = dtrace_dof_create(state); 13981 mutex_exit(&dtrace_lock); 13982 13983 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 13984 rval = copyout(dof, (void *)arg, len); 13985 dtrace_dof_destroy(dof); 13986 13987 return (rval == 0 ? 0 : EFAULT); 13988 } 13989 13990 case DTRACEIOC_AGGSNAP: 13991 case DTRACEIOC_BUFSNAP: { 13992 dtrace_bufdesc_t desc; 13993 caddr_t cached; 13994 dtrace_buffer_t *buf; 13995 13996 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13997 return (EFAULT); 13998 13999 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 14000 return (EINVAL); 14001 14002 mutex_enter(&dtrace_lock); 14003 14004 if (cmd == DTRACEIOC_BUFSNAP) { 14005 buf = &state->dts_buffer[desc.dtbd_cpu]; 14006 } else { 14007 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 14008 } 14009 14010 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 14011 size_t sz = buf->dtb_offset; 14012 14013 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 14014 mutex_exit(&dtrace_lock); 14015 return (EBUSY); 14016 } 14017 14018 /* 14019 * If this buffer has already been consumed, we're 14020 * going to indicate that there's nothing left here 14021 * to consume. 14022 */ 14023 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 14024 mutex_exit(&dtrace_lock); 14025 14026 desc.dtbd_size = 0; 14027 desc.dtbd_drops = 0; 14028 desc.dtbd_errors = 0; 14029 desc.dtbd_oldest = 0; 14030 sz = sizeof (desc); 14031 14032 if (copyout(&desc, (void *)arg, sz) != 0) 14033 return (EFAULT); 14034 14035 return (0); 14036 } 14037 14038 /* 14039 * If this is a ring buffer that has wrapped, we want 14040 * to copy the whole thing out. 14041 */ 14042 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 14043 dtrace_buffer_polish(buf); 14044 sz = buf->dtb_size; 14045 } 14046 14047 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 14048 mutex_exit(&dtrace_lock); 14049 return (EFAULT); 14050 } 14051 14052 desc.dtbd_size = sz; 14053 desc.dtbd_drops = buf->dtb_drops; 14054 desc.dtbd_errors = buf->dtb_errors; 14055 desc.dtbd_oldest = buf->dtb_xamot_offset; 14056 14057 mutex_exit(&dtrace_lock); 14058 14059 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 14060 return (EFAULT); 14061 14062 buf->dtb_flags |= DTRACEBUF_CONSUMED; 14063 14064 return (0); 14065 } 14066 14067 if (buf->dtb_tomax == NULL) { 14068 ASSERT(buf->dtb_xamot == NULL); 14069 mutex_exit(&dtrace_lock); 14070 return (ENOENT); 14071 } 14072 14073 cached = buf->dtb_tomax; 14074 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 14075 14076 dtrace_xcall(desc.dtbd_cpu, 14077 (dtrace_xcall_t)dtrace_buffer_switch, buf); 14078 14079 state->dts_errors += buf->dtb_xamot_errors; 14080 14081 /* 14082 * If the buffers did not actually switch, then the cross call 14083 * did not take place -- presumably because the given CPU is 14084 * not in the ready set. If this is the case, we'll return 14085 * ENOENT. 14086 */ 14087 if (buf->dtb_tomax == cached) { 14088 ASSERT(buf->dtb_xamot != cached); 14089 mutex_exit(&dtrace_lock); 14090 return (ENOENT); 14091 } 14092 14093 ASSERT(cached == buf->dtb_xamot); 14094 14095 /* 14096 * We have our snapshot; now copy it out. 14097 */ 14098 if (copyout(buf->dtb_xamot, desc.dtbd_data, 14099 buf->dtb_xamot_offset) != 0) { 14100 mutex_exit(&dtrace_lock); 14101 return (EFAULT); 14102 } 14103 14104 desc.dtbd_size = buf->dtb_xamot_offset; 14105 desc.dtbd_drops = buf->dtb_xamot_drops; 14106 desc.dtbd_errors = buf->dtb_xamot_errors; 14107 desc.dtbd_oldest = 0; 14108 14109 mutex_exit(&dtrace_lock); 14110 14111 /* 14112 * Finally, copy out the buffer description. 14113 */ 14114 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 14115 return (EFAULT); 14116 14117 return (0); 14118 } 14119 14120 case DTRACEIOC_CONF: { 14121 dtrace_conf_t conf; 14122 14123 bzero(&conf, sizeof (conf)); 14124 conf.dtc_difversion = DIF_VERSION; 14125 conf.dtc_difintregs = DIF_DIR_NREGS; 14126 conf.dtc_diftupregs = DIF_DTR_NREGS; 14127 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 14128 14129 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 14130 return (EFAULT); 14131 14132 return (0); 14133 } 14134 14135 case DTRACEIOC_STATUS: { 14136 dtrace_status_t stat; 14137 dtrace_dstate_t *dstate; 14138 int i, j; 14139 uint64_t nerrs; 14140 14141 /* 14142 * See the comment in dtrace_state_deadman() for the reason 14143 * for setting dts_laststatus to INT64_MAX before setting 14144 * it to the correct value. 14145 */ 14146 state->dts_laststatus = INT64_MAX; 14147 dtrace_membar_producer(); 14148 state->dts_laststatus = dtrace_gethrtime(); 14149 14150 bzero(&stat, sizeof (stat)); 14151 14152 mutex_enter(&dtrace_lock); 14153 14154 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 14155 mutex_exit(&dtrace_lock); 14156 return (ENOENT); 14157 } 14158 14159 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 14160 stat.dtst_exiting = 1; 14161 14162 nerrs = state->dts_errors; 14163 dstate = &state->dts_vstate.dtvs_dynvars; 14164 14165 for (i = 0; i < NCPU; i++) { 14166 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 14167 14168 stat.dtst_dyndrops += dcpu->dtdsc_drops; 14169 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 14170 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 14171 14172 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 14173 stat.dtst_filled++; 14174 14175 nerrs += state->dts_buffer[i].dtb_errors; 14176 14177 for (j = 0; j < state->dts_nspeculations; j++) { 14178 dtrace_speculation_t *spec; 14179 dtrace_buffer_t *buf; 14180 14181 spec = &state->dts_speculations[j]; 14182 buf = &spec->dtsp_buffer[i]; 14183 stat.dtst_specdrops += buf->dtb_xamot_drops; 14184 } 14185 } 14186 14187 stat.dtst_specdrops_busy = state->dts_speculations_busy; 14188 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 14189 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 14190 stat.dtst_dblerrors = state->dts_dblerrors; 14191 stat.dtst_killed = 14192 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 14193 stat.dtst_errors = nerrs; 14194 14195 mutex_exit(&dtrace_lock); 14196 14197 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 14198 return (EFAULT); 14199 14200 return (0); 14201 } 14202 14203 case DTRACEIOC_FORMAT: { 14204 dtrace_fmtdesc_t fmt; 14205 char *str; 14206 int len; 14207 14208 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 14209 return (EFAULT); 14210 14211 mutex_enter(&dtrace_lock); 14212 14213 if (fmt.dtfd_format == 0 || 14214 fmt.dtfd_format > state->dts_nformats) { 14215 mutex_exit(&dtrace_lock); 14216 return (EINVAL); 14217 } 14218 14219 /* 14220 * Format strings are allocated contiguously and they are 14221 * never freed; if a format index is less than the number 14222 * of formats, we can assert that the format map is non-NULL 14223 * and that the format for the specified index is non-NULL. 14224 */ 14225 ASSERT(state->dts_formats != NULL); 14226 str = state->dts_formats[fmt.dtfd_format - 1]; 14227 ASSERT(str != NULL); 14228 14229 len = strlen(str) + 1; 14230 14231 if (len > fmt.dtfd_length) { 14232 fmt.dtfd_length = len; 14233 14234 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 14235 mutex_exit(&dtrace_lock); 14236 return (EINVAL); 14237 } 14238 } else { 14239 if (copyout(str, fmt.dtfd_string, len) != 0) { 14240 mutex_exit(&dtrace_lock); 14241 return (EINVAL); 14242 } 14243 } 14244 14245 mutex_exit(&dtrace_lock); 14246 return (0); 14247 } 14248 14249 default: 14250 break; 14251 } 14252 14253 return (ENOTTY); 14254 } 14255 14256 /*ARGSUSED*/ 14257 static int 14258 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 14259 { 14260 dtrace_state_t *state; 14261 14262 switch (cmd) { 14263 case DDI_DETACH: 14264 break; 14265 14266 case DDI_SUSPEND: 14267 return (DDI_SUCCESS); 14268 14269 default: 14270 return (DDI_FAILURE); 14271 } 14272 14273 mutex_enter(&cpu_lock); 14274 mutex_enter(&dtrace_provider_lock); 14275 mutex_enter(&dtrace_lock); 14276 14277 ASSERT(dtrace_opens == 0); 14278 14279 if (dtrace_helpers > 0) { 14280 mutex_exit(&dtrace_provider_lock); 14281 mutex_exit(&dtrace_lock); 14282 mutex_exit(&cpu_lock); 14283 return (DDI_FAILURE); 14284 } 14285 14286 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 14287 mutex_exit(&dtrace_provider_lock); 14288 mutex_exit(&dtrace_lock); 14289 mutex_exit(&cpu_lock); 14290 return (DDI_FAILURE); 14291 } 14292 14293 dtrace_provider = NULL; 14294 14295 if ((state = dtrace_anon_grab()) != NULL) { 14296 /* 14297 * If there were ECBs on this state, the provider should 14298 * have not been allowed to detach; assert that there is 14299 * none. 14300 */ 14301 ASSERT(state->dts_necbs == 0); 14302 dtrace_state_destroy(state); 14303 14304 /* 14305 * If we're being detached with anonymous state, we need to 14306 * indicate to the kernel debugger that DTrace is now inactive. 14307 */ 14308 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 14309 } 14310 14311 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 14312 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 14313 dtrace_cpu_init = NULL; 14314 dtrace_helpers_cleanup = NULL; 14315 dtrace_helpers_fork = NULL; 14316 dtrace_cpustart_init = NULL; 14317 dtrace_cpustart_fini = NULL; 14318 dtrace_debugger_init = NULL; 14319 dtrace_debugger_fini = NULL; 14320 dtrace_kreloc_init = NULL; 14321 dtrace_kreloc_fini = NULL; 14322 dtrace_modload = NULL; 14323 dtrace_modunload = NULL; 14324 14325 mutex_exit(&cpu_lock); 14326 14327 if (dtrace_helptrace_enabled) { 14328 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 14329 dtrace_helptrace_buffer = NULL; 14330 } 14331 14332 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 14333 dtrace_probes = NULL; 14334 dtrace_nprobes = 0; 14335 14336 dtrace_hash_destroy(dtrace_bymod); 14337 dtrace_hash_destroy(dtrace_byfunc); 14338 dtrace_hash_destroy(dtrace_byname); 14339 dtrace_bymod = NULL; 14340 dtrace_byfunc = NULL; 14341 dtrace_byname = NULL; 14342 14343 kmem_cache_destroy(dtrace_state_cache); 14344 vmem_destroy(dtrace_minor); 14345 vmem_destroy(dtrace_arena); 14346 14347 if (dtrace_toxrange != NULL) { 14348 kmem_free(dtrace_toxrange, 14349 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 14350 dtrace_toxrange = NULL; 14351 dtrace_toxranges = 0; 14352 dtrace_toxranges_max = 0; 14353 } 14354 14355 ddi_remove_minor_node(dtrace_devi, NULL); 14356 dtrace_devi = NULL; 14357 14358 ddi_soft_state_fini(&dtrace_softstate); 14359 14360 ASSERT(dtrace_vtime_references == 0); 14361 ASSERT(dtrace_opens == 0); 14362 ASSERT(dtrace_retained == NULL); 14363 14364 mutex_exit(&dtrace_lock); 14365 mutex_exit(&dtrace_provider_lock); 14366 14367 /* 14368 * We don't destroy the task queue until after we have dropped our 14369 * locks (taskq_destroy() may block on running tasks). To prevent 14370 * attempting to do work after we have effectively detached but before 14371 * the task queue has been destroyed, all tasks dispatched via the 14372 * task queue must check that DTrace is still attached before 14373 * performing any operation. 14374 */ 14375 taskq_destroy(dtrace_taskq); 14376 dtrace_taskq = NULL; 14377 14378 return (DDI_SUCCESS); 14379 } 14380 14381 /*ARGSUSED*/ 14382 static int 14383 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 14384 { 14385 int error; 14386 14387 switch (infocmd) { 14388 case DDI_INFO_DEVT2DEVINFO: 14389 *result = (void *)dtrace_devi; 14390 error = DDI_SUCCESS; 14391 break; 14392 case DDI_INFO_DEVT2INSTANCE: 14393 *result = (void *)0; 14394 error = DDI_SUCCESS; 14395 break; 14396 default: 14397 error = DDI_FAILURE; 14398 } 14399 return (error); 14400 } 14401 14402 static struct cb_ops dtrace_cb_ops = { 14403 dtrace_open, /* open */ 14404 dtrace_close, /* close */ 14405 nulldev, /* strategy */ 14406 nulldev, /* print */ 14407 nodev, /* dump */ 14408 nodev, /* read */ 14409 nodev, /* write */ 14410 dtrace_ioctl, /* ioctl */ 14411 nodev, /* devmap */ 14412 nodev, /* mmap */ 14413 nodev, /* segmap */ 14414 nochpoll, /* poll */ 14415 ddi_prop_op, /* cb_prop_op */ 14416 0, /* streamtab */ 14417 D_NEW | D_MP /* Driver compatibility flag */ 14418 }; 14419 14420 static struct dev_ops dtrace_ops = { 14421 DEVO_REV, /* devo_rev */ 14422 0, /* refcnt */ 14423 dtrace_info, /* get_dev_info */ 14424 nulldev, /* identify */ 14425 nulldev, /* probe */ 14426 dtrace_attach, /* attach */ 14427 dtrace_detach, /* detach */ 14428 nodev, /* reset */ 14429 &dtrace_cb_ops, /* driver operations */ 14430 NULL, /* bus operations */ 14431 nodev /* dev power */ 14432 }; 14433 14434 static struct modldrv modldrv = { 14435 &mod_driverops, /* module type (this is a pseudo driver) */ 14436 "Dynamic Tracing", /* name of module */ 14437 &dtrace_ops, /* driver ops */ 14438 }; 14439 14440 static struct modlinkage modlinkage = { 14441 MODREV_1, 14442 (void *)&modldrv, 14443 NULL 14444 }; 14445 14446 int 14447 _init(void) 14448 { 14449 return (mod_install(&modlinkage)); 14450 } 14451 14452 int 14453 _info(struct modinfo *modinfop) 14454 { 14455 return (mod_info(&modlinkage, modinfop)); 14456 } 14457 14458 int 14459 _fini(void) 14460 { 14461 return (mod_remove(&modlinkage)); 14462 } 14463