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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * DTrace - Dynamic Tracing for Solaris 31 * 32 * This is the implementation of the Solaris Dynamic Tracing framework 33 * (DTrace). The user-visible interface to DTrace is described at length in 34 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 35 * library, the in-kernel DTrace framework, and the DTrace providers are 36 * described in the block comments in the <sys/dtrace.h> header file. The 37 * internal architecture of DTrace is described in the block comments in the 38 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 39 * implementation very much assume mastery of all of these sources; if one has 40 * an unanswered question about the implementation, one should consult them 41 * first. 42 * 43 * The functions here are ordered roughly as follows: 44 * 45 * - Probe context functions 46 * - Probe hashing functions 47 * - Non-probe context utility functions 48 * - Matching functions 49 * - Provider-to-Framework API functions 50 * - Probe management functions 51 * - DIF object functions 52 * - Format functions 53 * - Predicate functions 54 * - ECB functions 55 * - Buffer functions 56 * - Enabling functions 57 * - DOF functions 58 * - Anonymous enabling functions 59 * - Consumer state functions 60 * - Helper functions 61 * - Hook functions 62 * - Driver cookbook functions 63 * 64 * Each group of functions begins with a block comment labelled the "DTrace 65 * [Group] Functions", allowing one to find each block by searching forward 66 * on capital-f functions. 67 */ 68 #include <sys/errno.h> 69 #include <sys/stat.h> 70 #include <sys/modctl.h> 71 #include <sys/conf.h> 72 #include <sys/systm.h> 73 #include <sys/ddi.h> 74 #include <sys/sunddi.h> 75 #include <sys/cpuvar.h> 76 #include <sys/kmem.h> 77 #include <sys/strsubr.h> 78 #include <sys/sysmacros.h> 79 #include <sys/dtrace_impl.h> 80 #include <sys/atomic.h> 81 #include <sys/cmn_err.h> 82 #include <sys/mutex_impl.h> 83 #include <sys/rwlock_impl.h> 84 #include <sys/ctf_api.h> 85 #include <sys/panic.h> 86 #include <sys/priv_impl.h> 87 #include <sys/policy.h> 88 #include <sys/cred_impl.h> 89 #include <sys/procfs_isa.h> 90 #include <sys/taskq.h> 91 #include <sys/mkdev.h> 92 #include <sys/kdi.h> 93 #include <sys/zone.h> 94 95 /* 96 * DTrace Tunable Variables 97 * 98 * The following variables may be tuned by adding a line to /etc/system that 99 * includes both the name of the DTrace module ("dtrace") and the name of the 100 * variable. For example: 101 * 102 * set dtrace:dtrace_destructive_disallow = 1 103 * 104 * In general, the only variables that one should be tuning this way are those 105 * that affect system-wide DTrace behavior, and for which the default behavior 106 * is undesirable. Most of these variables are tunable on a per-consumer 107 * basis using DTrace options, and need not be tuned on a system-wide basis. 108 * When tuning these variables, avoid pathological values; while some attempt 109 * is made to verify the integrity of these variables, they are not considered 110 * part of the supported interface to DTrace, and they are therefore not 111 * checked comprehensively. Further, these variables should not be tuned 112 * dynamically via "mdb -kw" or other means; they should only be tuned via 113 * /etc/system. 114 */ 115 int dtrace_destructive_disallow = 0; 116 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 117 size_t dtrace_difo_maxsize = (256 * 1024); 118 dtrace_optval_t dtrace_dof_maxsize = (256 * 1024); 119 size_t dtrace_global_maxsize = (16 * 1024); 120 size_t dtrace_actions_max = (16 * 1024); 121 size_t dtrace_retain_max = 1024; 122 dtrace_optval_t dtrace_helper_actions_max = 32; 123 dtrace_optval_t dtrace_helper_providers_max = 32; 124 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 125 size_t dtrace_strsize_default = 256; 126 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 127 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 128 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 129 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 130 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 131 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 132 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 133 dtrace_optval_t dtrace_nspec_default = 1; 134 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 135 dtrace_optval_t dtrace_stackframes_default = 20; 136 dtrace_optval_t dtrace_ustackframes_default = 20; 137 dtrace_optval_t dtrace_jstackframes_default = 50; 138 dtrace_optval_t dtrace_jstackstrsize_default = 512; 139 int dtrace_msgdsize_max = 128; 140 hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 141 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 142 int dtrace_devdepth_max = 32; 143 int dtrace_err_verbose; 144 hrtime_t dtrace_deadman_interval = NANOSEC; 145 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 146 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 147 148 /* 149 * DTrace External Variables 150 * 151 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 152 * available to DTrace consumers via the backtick (`) syntax. One of these, 153 * dtrace_zero, is made deliberately so: it is provided as a source of 154 * well-known, zero-filled memory. While this variable is not documented, 155 * it is used by some translators as an implementation detail. 156 */ 157 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 158 159 /* 160 * DTrace Internal Variables 161 */ 162 static dev_info_t *dtrace_devi; /* device info */ 163 static vmem_t *dtrace_arena; /* probe ID arena */ 164 static vmem_t *dtrace_minor; /* minor number arena */ 165 static taskq_t *dtrace_taskq; /* task queue */ 166 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 167 static int dtrace_nprobes; /* number of probes */ 168 static dtrace_provider_t *dtrace_provider; /* provider list */ 169 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 170 static int dtrace_opens; /* number of opens */ 171 static int dtrace_helpers; /* number of helpers */ 172 static void *dtrace_softstate; /* softstate pointer */ 173 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 174 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 175 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 176 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 177 static int dtrace_toxranges; /* number of toxic ranges */ 178 static int dtrace_toxranges_max; /* size of toxic range array */ 179 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 180 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 181 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 182 static kthread_t *dtrace_panicked; /* panicking thread */ 183 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 184 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 185 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 186 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 187 static dtrace_state_t *dtrace_state; /* temporary variable */ 188 static int dtrace_err; /* temporary variable */ 189 190 /* 191 * DTrace Locking 192 * DTrace is protected by three (relatively coarse-grained) locks: 193 * 194 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 195 * including enabling state, probes, ECBs, consumer state, helper state, 196 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 197 * probe context is lock-free -- synchronization is handled via the 198 * dtrace_sync() cross call mechanism. 199 * 200 * (2) dtrace_provider_lock is required when manipulating provider state, or 201 * when provider state must be held constant. 202 * 203 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 204 * when meta provider state must be held constant. 205 * 206 * The lock ordering between these three locks is dtrace_meta_lock before 207 * dtrace_provider_lock before dtrace_lock. (In particular, there are 208 * several places where dtrace_provider_lock is held by the framework as it 209 * calls into the providers -- which then call back into the framework, 210 * grabbing dtrace_lock.) 211 * 212 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 213 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 214 * role as a coarse-grained lock; it is acquired before both of these locks. 215 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 216 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 217 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 218 * acquired _between_ dtrace_provider_lock and dtrace_lock. 219 */ 220 static kmutex_t dtrace_lock; /* probe state lock */ 221 static kmutex_t dtrace_provider_lock; /* provider state lock */ 222 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 223 224 /* 225 * DTrace Provider Variables 226 * 227 * These are the variables relating to DTrace as a provider (that is, the 228 * provider of the BEGIN, END, and ERROR probes). 229 */ 230 static dtrace_pattr_t dtrace_provider_attr = { 231 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 232 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 233 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 234 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 235 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 236 }; 237 238 static void 239 dtrace_nullop(void) 240 {} 241 242 static dtrace_pops_t dtrace_provider_ops = { 243 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, 244 (void (*)(void *, struct modctl *))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 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 249 NULL, 250 NULL, 251 NULL, 252 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 253 }; 254 255 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 256 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 257 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 258 259 /* 260 * DTrace Helper Tracing Variables 261 */ 262 uint32_t dtrace_helptrace_next = 0; 263 uint32_t dtrace_helptrace_nlocals; 264 char *dtrace_helptrace_buffer; 265 int dtrace_helptrace_bufsize = 512 * 1024; 266 267 #ifdef DEBUG 268 int dtrace_helptrace_enabled = 1; 269 #else 270 int dtrace_helptrace_enabled = 0; 271 #endif 272 273 /* 274 * DTrace Error Hashing 275 * 276 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 277 * table. This is very useful for checking coverage of tests that are 278 * expected to induce DIF or DOF processing errors, and may be useful for 279 * debugging problems in the DIF code generator or in DOF generation . The 280 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 281 */ 282 #ifdef DEBUG 283 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 284 static const char *dtrace_errlast; 285 static kthread_t *dtrace_errthread; 286 static kmutex_t dtrace_errlock; 287 #endif 288 289 /* 290 * DTrace Macros and Constants 291 * 292 * These are various macros that are useful in various spots in the 293 * implementation, along with a few random constants that have no meaning 294 * outside of the implementation. There is no real structure to this cpp 295 * mishmash -- but is there ever? 296 */ 297 #define DTRACE_HASHSTR(hash, probe) \ 298 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 299 300 #define DTRACE_HASHNEXT(hash, probe) \ 301 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 302 303 #define DTRACE_HASHPREV(hash, probe) \ 304 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 305 306 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 307 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 308 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 309 310 #define DTRACE_AGGHASHSIZE_SLEW 17 311 312 /* 313 * The key for a thread-local variable consists of the lower 61 bits of the 314 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 315 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 316 * equal to a variable identifier. This is necessary (but not sufficient) to 317 * assure that global associative arrays never collide with thread-local 318 * variables. To guarantee that they cannot collide, we must also define the 319 * order for keying dynamic variables. That order is: 320 * 321 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 322 * 323 * Because the variable-key and the tls-key are in orthogonal spaces, there is 324 * no way for a global variable key signature to match a thread-local key 325 * signature. 326 */ 327 #define DTRACE_TLS_THRKEY(where) { \ 328 uint_t intr = 0; \ 329 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 330 for (; actv; actv >>= 1) \ 331 intr++; \ 332 ASSERT(intr < (1 << 3)); \ 333 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 334 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 335 } 336 337 #define DTRACE_STORE(type, tomax, offset, what) \ 338 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 339 340 #ifndef __i386 341 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 342 if (addr & (size - 1)) { \ 343 *flags |= CPU_DTRACE_BADALIGN; \ 344 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 345 return (0); \ 346 } 347 #else 348 #define DTRACE_ALIGNCHECK(addr, size, flags) 349 #endif 350 351 #define DTRACE_LOADFUNC(bits) \ 352 /*CSTYLED*/ \ 353 uint##bits##_t \ 354 dtrace_load##bits(uintptr_t addr) \ 355 { \ 356 size_t size = bits / NBBY; \ 357 /*CSTYLED*/ \ 358 uint##bits##_t rval; \ 359 int i; \ 360 volatile uint16_t *flags = (volatile uint16_t *) \ 361 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ 362 \ 363 DTRACE_ALIGNCHECK(addr, size, flags); \ 364 \ 365 for (i = 0; i < dtrace_toxranges; i++) { \ 366 if (addr >= dtrace_toxrange[i].dtt_limit) \ 367 continue; \ 368 \ 369 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 370 continue; \ 371 \ 372 /* \ 373 * This address falls within a toxic region; return 0. \ 374 */ \ 375 *flags |= CPU_DTRACE_BADADDR; \ 376 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 377 return (0); \ 378 } \ 379 \ 380 *flags |= CPU_DTRACE_NOFAULT; \ 381 /*CSTYLED*/ \ 382 rval = *((volatile uint##bits##_t *)addr); \ 383 *flags &= ~CPU_DTRACE_NOFAULT; \ 384 \ 385 return (rval); \ 386 } 387 388 #ifdef _LP64 389 #define dtrace_loadptr dtrace_load64 390 #else 391 #define dtrace_loadptr dtrace_load32 392 #endif 393 394 #define DTRACE_MATCH_NEXT 0 395 #define DTRACE_MATCH_DONE 1 396 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 397 #define DTRACE_STATE_ALIGN 64 398 399 #define DTRACE_FLAGS2FLT(flags) \ 400 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 401 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 402 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 403 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 404 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 405 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 406 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 407 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 408 DTRACEFLT_UNKNOWN) 409 410 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 411 static void dtrace_enabling_provide(dtrace_provider_t *); 412 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 413 static void dtrace_enabling_matchall(void); 414 static dtrace_state_t *dtrace_anon_grab(void); 415 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 416 dtrace_state_t *, uint64_t, uint64_t); 417 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 418 static void dtrace_buffer_drop(dtrace_buffer_t *); 419 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 420 dtrace_state_t *, dtrace_mstate_t *); 421 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 422 dtrace_optval_t); 423 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 424 425 /* 426 * DTrace Probe Context Functions 427 * 428 * These functions are called from probe context. Because probe context is 429 * any context in which C may be called, arbitrarily locks may be held, 430 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 431 * As a result, functions called from probe context may only call other DTrace 432 * support functions -- they may not interact at all with the system at large. 433 * (Note that the ASSERT macro is made probe-context safe by redefining it in 434 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 435 * loads are to be performed from probe context, they _must_ be in terms of 436 * the safe dtrace_load*() variants. 437 * 438 * Some functions in this block are not actually called from probe context; 439 * for these functions, there will be a comment above the function reading 440 * "Note: not called from probe context." 441 */ 442 void 443 dtrace_panic(const char *format, ...) 444 { 445 va_list alist; 446 447 va_start(alist, format); 448 dtrace_vpanic(format, alist); 449 va_end(alist); 450 } 451 452 int 453 dtrace_assfail(const char *a, const char *f, int l) 454 { 455 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 456 457 /* 458 * We just need something here that even the most clever compiler 459 * cannot optimize away. 460 */ 461 return (a[(uintptr_t)f]); 462 } 463 464 /* 465 * Atomically increment a specified error counter from probe context. 466 */ 467 static void 468 dtrace_error(uint32_t *counter) 469 { 470 /* 471 * Most counters stored to in probe context are per-CPU counters. 472 * However, there are some error conditions that are sufficiently 473 * arcane that they don't merit per-CPU storage. If these counters 474 * are incremented concurrently on different CPUs, scalability will be 475 * adversely affected -- but we don't expect them to be white-hot in a 476 * correctly constructed enabling... 477 */ 478 uint32_t oval, nval; 479 480 do { 481 oval = *counter; 482 483 if ((nval = oval + 1) == 0) { 484 /* 485 * If the counter would wrap, set it to 1 -- assuring 486 * that the counter is never zero when we have seen 487 * errors. (The counter must be 32-bits because we 488 * aren't guaranteed a 64-bit compare&swap operation.) 489 * To save this code both the infamy of being fingered 490 * by a priggish news story and the indignity of being 491 * the target of a neo-puritan witch trial, we're 492 * carefully avoiding any colorful description of the 493 * likelihood of this condition -- but suffice it to 494 * say that it is only slightly more likely than the 495 * overflow of predicate cache IDs, as discussed in 496 * dtrace_predicate_create(). 497 */ 498 nval = 1; 499 } 500 } while (dtrace_cas32(counter, oval, nval) != oval); 501 } 502 503 /* 504 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 505 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 506 */ 507 DTRACE_LOADFUNC(8) 508 DTRACE_LOADFUNC(16) 509 DTRACE_LOADFUNC(32) 510 DTRACE_LOADFUNC(64) 511 512 static int 513 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 514 { 515 if (dest < mstate->dtms_scratch_base) 516 return (0); 517 518 if (dest + size < dest) 519 return (0); 520 521 if (dest + size > mstate->dtms_scratch_ptr) 522 return (0); 523 524 return (1); 525 } 526 527 static int 528 dtrace_canstore_statvar(uint64_t addr, size_t sz, 529 dtrace_statvar_t **svars, int nsvars) 530 { 531 int i; 532 533 for (i = 0; i < nsvars; i++) { 534 dtrace_statvar_t *svar = svars[i]; 535 536 if (svar == NULL || svar->dtsv_size == 0) 537 continue; 538 539 if (addr - svar->dtsv_data < svar->dtsv_size && 540 addr + sz <= svar->dtsv_data + svar->dtsv_size) 541 return (1); 542 } 543 544 return (0); 545 } 546 547 /* 548 * Check to see if the address is within a memory region to which a store may 549 * be issued. This includes the DTrace scratch areas, and any DTrace variable 550 * region. The caller of dtrace_canstore() is responsible for performing any 551 * alignment checks that are needed before stores are actually executed. 552 */ 553 static int 554 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 555 dtrace_vstate_t *vstate) 556 { 557 uintptr_t a; 558 size_t s; 559 560 /* 561 * First, check to see if the address is in scratch space... 562 */ 563 a = mstate->dtms_scratch_base; 564 s = mstate->dtms_scratch_size; 565 566 if (addr - a < s && addr + sz <= a + s) 567 return (1); 568 569 /* 570 * Now check to see if it's a dynamic variable. This check will pick 571 * up both thread-local variables and any global dynamically-allocated 572 * variables. 573 */ 574 a = (uintptr_t)vstate->dtvs_dynvars.dtds_base; 575 s = vstate->dtvs_dynvars.dtds_size; 576 if (addr - a < s && addr + sz <= a + s) 577 return (1); 578 579 /* 580 * Finally, check the static local and global variables. These checks 581 * take the longest, so we perform them last. 582 */ 583 if (dtrace_canstore_statvar(addr, sz, 584 vstate->dtvs_locals, vstate->dtvs_nlocals)) 585 return (1); 586 587 if (dtrace_canstore_statvar(addr, sz, 588 vstate->dtvs_globals, vstate->dtvs_nglobals)) 589 return (1); 590 591 return (0); 592 } 593 594 /* 595 * Compare two strings using safe loads. 596 */ 597 static int 598 dtrace_strncmp(char *s1, char *s2, size_t limit) 599 { 600 uint8_t c1, c2; 601 volatile uint16_t *flags; 602 603 if (s1 == s2 || limit == 0) 604 return (0); 605 606 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 607 608 do { 609 if (s1 == NULL) { 610 c1 = '\0'; 611 } else { 612 c1 = dtrace_load8((uintptr_t)s1++); 613 } 614 615 if (s2 == NULL) { 616 c2 = '\0'; 617 } else { 618 c2 = dtrace_load8((uintptr_t)s2++); 619 } 620 621 if (c1 != c2) 622 return (c1 - c2); 623 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 624 625 return (0); 626 } 627 628 /* 629 * Compute strlen(s) for a string using safe memory accesses. The additional 630 * len parameter is used to specify a maximum length to ensure completion. 631 */ 632 static size_t 633 dtrace_strlen(const char *s, size_t lim) 634 { 635 uint_t len; 636 637 for (len = 0; len != lim; len++) { 638 if (dtrace_load8((uintptr_t)s++) == '\0') 639 break; 640 } 641 642 return (len); 643 } 644 645 /* 646 * Check if an address falls within a toxic region. 647 */ 648 static int 649 dtrace_istoxic(uintptr_t kaddr, size_t size) 650 { 651 uintptr_t taddr, tsize; 652 int i; 653 654 for (i = 0; i < dtrace_toxranges; i++) { 655 taddr = dtrace_toxrange[i].dtt_base; 656 tsize = dtrace_toxrange[i].dtt_limit - taddr; 657 658 if (kaddr - taddr < tsize) { 659 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 660 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr; 661 return (1); 662 } 663 664 if (taddr - kaddr < size) { 665 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 666 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr; 667 return (1); 668 } 669 } 670 671 return (0); 672 } 673 674 /* 675 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 676 * memory specified by the DIF program. The dst is assumed to be safe memory 677 * that we can store to directly because it is managed by DTrace. As with 678 * standard bcopy, overlapping copies are handled properly. 679 */ 680 static void 681 dtrace_bcopy(const void *src, void *dst, size_t len) 682 { 683 if (len != 0) { 684 uint8_t *s1 = dst; 685 const uint8_t *s2 = src; 686 687 if (s1 <= s2) { 688 do { 689 *s1++ = dtrace_load8((uintptr_t)s2++); 690 } while (--len != 0); 691 } else { 692 s2 += len; 693 s1 += len; 694 695 do { 696 *--s1 = dtrace_load8((uintptr_t)--s2); 697 } while (--len != 0); 698 } 699 } 700 } 701 702 /* 703 * Copy src to dst using safe memory accesses, up to either the specified 704 * length, or the point that a nul byte is encountered. The src is assumed to 705 * be unsafe memory specified by the DIF program. The dst is assumed to be 706 * safe memory that we can store to directly because it is managed by DTrace. 707 * Unlike dtrace_bcopy(), overlapping regions are not handled. 708 */ 709 static void 710 dtrace_strcpy(const void *src, void *dst, size_t len) 711 { 712 if (len != 0) { 713 uint8_t *s1 = dst, c; 714 const uint8_t *s2 = src; 715 716 do { 717 *s1++ = c = dtrace_load8((uintptr_t)s2++); 718 } while (--len != 0 && c != '\0'); 719 } 720 } 721 722 /* 723 * Copy src to dst, deriving the size and type from the specified (BYREF) 724 * variable type. The src is assumed to be unsafe memory specified by the DIF 725 * program. The dst is assumed to be DTrace variable memory that is of the 726 * specified type; we assume that we can store to directly. 727 */ 728 static void 729 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 730 { 731 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 732 733 if (type->dtdt_kind == DIF_TYPE_STRING) { 734 dtrace_strcpy(src, dst, type->dtdt_size); 735 } else { 736 dtrace_bcopy(src, dst, type->dtdt_size); 737 } 738 } 739 740 /* 741 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 742 * unsafe memory specified by the DIF program. The s2 data is assumed to be 743 * safe memory that we can access directly because it is managed by DTrace. 744 */ 745 static int 746 dtrace_bcmp(const void *s1, const void *s2, size_t len) 747 { 748 volatile uint16_t *flags; 749 750 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 751 752 if (s1 == s2) 753 return (0); 754 755 if (s1 == NULL || s2 == NULL) 756 return (1); 757 758 if (s1 != s2 && len != 0) { 759 const uint8_t *ps1 = s1; 760 const uint8_t *ps2 = s2; 761 762 do { 763 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 764 return (1); 765 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 766 } 767 return (0); 768 } 769 770 /* 771 * Zero the specified region using a simple byte-by-byte loop. Note that this 772 * is for safe DTrace-managed memory only. 773 */ 774 static void 775 dtrace_bzero(void *dst, size_t len) 776 { 777 uchar_t *cp; 778 779 for (cp = dst; len != 0; len--) 780 *cp++ = 0; 781 } 782 783 /* 784 * This privilege checks should be used by actions and subroutines to 785 * verify the credentials of the process that enabled the invoking ECB. 786 */ 787 static int 788 dtrace_priv_proc_common(dtrace_state_t *state) 789 { 790 uid_t uid = state->dts_cred.dcr_uid; 791 gid_t gid = state->dts_cred.dcr_gid; 792 cred_t *cr; 793 proc_t *proc; 794 795 if ((cr = CRED()) != NULL && 796 uid == cr->cr_uid && 797 uid == cr->cr_ruid && 798 uid == cr->cr_suid && 799 gid == cr->cr_gid && 800 gid == cr->cr_rgid && 801 gid == cr->cr_sgid && 802 (proc = ttoproc(curthread)) != NULL && 803 !(proc->p_flag & SNOCD)) 804 return (1); 805 806 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 807 808 return (0); 809 } 810 811 static int 812 dtrace_priv_proc_destructive(dtrace_state_t *state) 813 { 814 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_DESTRUCTIVE) 815 return (1); 816 817 return (dtrace_priv_proc_common(state)); 818 } 819 820 static int 821 dtrace_priv_proc_control(dtrace_state_t *state) 822 { 823 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 824 return (1); 825 826 return (dtrace_priv_proc_common(state)); 827 } 828 829 static int 830 dtrace_priv_proc(dtrace_state_t *state) 831 { 832 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 833 return (1); 834 835 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 836 837 return (0); 838 } 839 840 static int 841 dtrace_priv_kernel(dtrace_state_t *state) 842 { 843 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 844 return (1); 845 846 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 847 848 return (0); 849 } 850 851 static int 852 dtrace_priv_kernel_destructive(dtrace_state_t *state) 853 { 854 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 855 return (1); 856 857 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 858 859 return (0); 860 } 861 862 /* 863 * Note: not called from probe context. This function is called 864 * asynchronously (and at a regular interval) from outside of probe context to 865 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 866 * cleaning is explained in detail in <sys/dtrace_impl.h>. 867 */ 868 void 869 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 870 { 871 dtrace_dynvar_t *dirty; 872 dtrace_dstate_percpu_t *dcpu; 873 int i, work = 0; 874 875 for (i = 0; i < NCPU; i++) { 876 dcpu = &dstate->dtds_percpu[i]; 877 878 ASSERT(dcpu->dtdsc_rinsing == NULL); 879 880 /* 881 * If the dirty list is NULL, there is no dirty work to do. 882 */ 883 if (dcpu->dtdsc_dirty == NULL) 884 continue; 885 886 /* 887 * If the clean list is non-NULL, then we're not going to do 888 * any work for this CPU -- it means that there has not been 889 * a dtrace_dynvar() allocation on this CPU (or from this CPU) 890 * since the last time we cleaned house. 891 */ 892 if (dcpu->dtdsc_clean != NULL) 893 continue; 894 895 work = 1; 896 897 /* 898 * Atomically move the dirty list aside. 899 */ 900 do { 901 dirty = dcpu->dtdsc_dirty; 902 903 /* 904 * Before we zap the dirty list, set the rinsing list. 905 * (This allows for a potential assertion in 906 * dtrace_dynvar(): if a free dynamic variable appears 907 * on a hash chain, either the dirty list or the 908 * rinsing list for some CPU must be non-NULL.) 909 */ 910 dcpu->dtdsc_rinsing = dirty; 911 dtrace_membar_producer(); 912 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 913 dirty, NULL) != dirty); 914 } 915 916 if (!work) { 917 /* 918 * We have no work to do; we can simply return. 919 */ 920 return; 921 } 922 923 dtrace_sync(); 924 925 for (i = 0; i < NCPU; i++) { 926 dcpu = &dstate->dtds_percpu[i]; 927 928 if (dcpu->dtdsc_rinsing == NULL) 929 continue; 930 931 /* 932 * We are now guaranteed that no hash chain contains a pointer 933 * into this dirty list; we can make it clean. 934 */ 935 ASSERT(dcpu->dtdsc_clean == NULL); 936 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 937 dcpu->dtdsc_rinsing = NULL; 938 } 939 940 /* 941 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 942 * sure that all CPUs have seen all of the dtdsc_clean pointers. 943 * This prevents a race whereby a CPU incorrectly decides that 944 * the state should be something other than DTRACE_DSTATE_CLEAN 945 * after dtrace_dynvar_clean() has completed. 946 */ 947 dtrace_sync(); 948 949 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 950 } 951 952 /* 953 * Depending on the value of the op parameter, this function looks-up, 954 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 955 * allocation is requested, this function will return a pointer to a 956 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 957 * variable can be allocated. If NULL is returned, the appropriate counter 958 * will be incremented. 959 */ 960 dtrace_dynvar_t * 961 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 962 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op) 963 { 964 uint64_t hashval = 1; 965 dtrace_dynhash_t *hash = dstate->dtds_hash; 966 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 967 processorid_t me = CPU->cpu_id, cpu = me; 968 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 969 size_t bucket, ksize; 970 size_t chunksize = dstate->dtds_chunksize; 971 uintptr_t kdata, lock, nstate; 972 uint_t i; 973 974 ASSERT(nkeys != 0); 975 976 /* 977 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 978 * algorithm. For the by-value portions, we perform the algorithm in 979 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 980 * bit, and seems to have only a minute effect on distribution. For 981 * the by-reference data, we perform "One-at-a-time" iterating (safely) 982 * over each referenced byte. It's painful to do this, but it's much 983 * better than pathological hash distribution. The efficacy of the 984 * hashing algorithm (and a comparison with other algorithms) may be 985 * found by running the ::dtrace_dynstat MDB dcmd. 986 */ 987 for (i = 0; i < nkeys; i++) { 988 if (key[i].dttk_size == 0) { 989 uint64_t val = key[i].dttk_value; 990 991 hashval += (val >> 48) & 0xffff; 992 hashval += (hashval << 10); 993 hashval ^= (hashval >> 6); 994 995 hashval += (val >> 32) & 0xffff; 996 hashval += (hashval << 10); 997 hashval ^= (hashval >> 6); 998 999 hashval += (val >> 16) & 0xffff; 1000 hashval += (hashval << 10); 1001 hashval ^= (hashval >> 6); 1002 1003 hashval += val & 0xffff; 1004 hashval += (hashval << 10); 1005 hashval ^= (hashval >> 6); 1006 } else { 1007 /* 1008 * This is incredibly painful, but it beats the hell 1009 * out of the alternative. 1010 */ 1011 uint64_t j, size = key[i].dttk_size; 1012 uintptr_t base = (uintptr_t)key[i].dttk_value; 1013 1014 for (j = 0; j < size; j++) { 1015 hashval += dtrace_load8(base + j); 1016 hashval += (hashval << 10); 1017 hashval ^= (hashval >> 6); 1018 } 1019 } 1020 } 1021 1022 hashval += (hashval << 3); 1023 hashval ^= (hashval >> 11); 1024 hashval += (hashval << 15); 1025 1026 /* 1027 * There is a remote chance (ideally, 1 in 2^32) that our hashval 1028 * comes out to be 0. We rely on a zero hashval denoting a free 1029 * element; if this actually happens, we set the hashval to 1. 1030 */ 1031 if (hashval == 0) 1032 hashval = 1; 1033 1034 /* 1035 * Yes, it's painful to do a divide here. If the cycle count becomes 1036 * important here, tricks can be pulled to reduce it. (However, it's 1037 * critical that hash collisions be kept to an absolute minimum; 1038 * they're much more painful than a divide.) It's better to have a 1039 * solution that generates few collisions and still keeps things 1040 * relatively simple. 1041 */ 1042 bucket = hashval % dstate->dtds_hashsize; 1043 1044 if (op == DTRACE_DYNVAR_DEALLOC) { 1045 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1046 1047 for (;;) { 1048 while ((lock = *lockp) & 1) 1049 continue; 1050 1051 if (dtrace_casptr((void *)lockp, 1052 (void *)lock, (void *)(lock + 1)) == (void *)lock) 1053 break; 1054 } 1055 1056 dtrace_membar_producer(); 1057 } 1058 1059 top: 1060 prev = NULL; 1061 lock = hash[bucket].dtdh_lock; 1062 1063 dtrace_membar_consumer(); 1064 1065 start = hash[bucket].dtdh_chain; 1066 ASSERT(start == NULL || start->dtdv_hashval != 0 || 1067 op != DTRACE_DYNVAR_DEALLOC); 1068 1069 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1070 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1071 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1072 1073 if (dvar->dtdv_hashval != hashval) { 1074 if (dvar->dtdv_hashval == 0) { 1075 /* 1076 * We've gone off the rails. Somewhere 1077 * along the line, one of the members of this 1078 * hash chain was deleted. We could assert 1079 * that either the dirty list or the rinsing 1080 * list is non-NULL. (The dtrace_sync() in 1081 * dtrace_dynvar_clean() would validate this 1082 * assertion.) 1083 */ 1084 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1085 goto top; 1086 } 1087 1088 goto next; 1089 } 1090 1091 if (dtuple->dtt_nkeys != nkeys) 1092 goto next; 1093 1094 for (i = 0; i < nkeys; i++, dkey++) { 1095 if (dkey->dttk_size != key[i].dttk_size) 1096 goto next; /* size or type mismatch */ 1097 1098 if (dkey->dttk_size != 0) { 1099 if (dtrace_bcmp( 1100 (void *)(uintptr_t)key[i].dttk_value, 1101 (void *)(uintptr_t)dkey->dttk_value, 1102 dkey->dttk_size)) 1103 goto next; 1104 } else { 1105 if (dkey->dttk_value != key[i].dttk_value) 1106 goto next; 1107 } 1108 } 1109 1110 if (op != DTRACE_DYNVAR_DEALLOC) 1111 return (dvar); 1112 1113 ASSERT(dvar->dtdv_next == NULL || 1114 dvar->dtdv_next->dtdv_hashval != 0); 1115 1116 if (prev != NULL) { 1117 ASSERT(hash[bucket].dtdh_chain != dvar); 1118 ASSERT(start != dvar); 1119 ASSERT(prev->dtdv_next == dvar); 1120 prev->dtdv_next = dvar->dtdv_next; 1121 } else { 1122 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1123 start, dvar->dtdv_next) != start) { 1124 /* 1125 * We have failed to atomically swing the 1126 * hash table head pointer, presumably because 1127 * of a conflicting allocation on another CPU. 1128 * We need to reread the hash chain and try 1129 * again. 1130 */ 1131 goto top; 1132 } 1133 } 1134 1135 dtrace_membar_producer(); 1136 1137 /* 1138 * Now clear the hash value to indicate that it's free. 1139 */ 1140 ASSERT(hash[bucket].dtdh_chain != dvar); 1141 dvar->dtdv_hashval = 0; 1142 1143 dtrace_membar_producer(); 1144 1145 /* 1146 * Set the next pointer to point at the dirty list, and 1147 * atomically swing the dirty pointer to the newly freed dvar. 1148 */ 1149 do { 1150 next = dcpu->dtdsc_dirty; 1151 dvar->dtdv_next = next; 1152 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1153 1154 /* 1155 * Finally, unlock this hash bucket. 1156 */ 1157 ASSERT(hash[bucket].dtdh_lock == lock); 1158 ASSERT(lock & 1); 1159 hash[bucket].dtdh_lock++; 1160 1161 return (NULL); 1162 next: 1163 prev = dvar; 1164 continue; 1165 } 1166 1167 if (op != DTRACE_DYNVAR_ALLOC) { 1168 /* 1169 * If we are not to allocate a new variable, we want to 1170 * return NULL now. Before we return, check that the value 1171 * of the lock word hasn't changed. If it has, we may have 1172 * seen an inconsistent snapshot. 1173 */ 1174 if (op == DTRACE_DYNVAR_NOALLOC) { 1175 if (hash[bucket].dtdh_lock != lock) 1176 goto top; 1177 } else { 1178 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1179 ASSERT(hash[bucket].dtdh_lock == lock); 1180 ASSERT(lock & 1); 1181 hash[bucket].dtdh_lock++; 1182 } 1183 1184 return (NULL); 1185 } 1186 1187 /* 1188 * We need to allocate a new dynamic variable. The size we need is the 1189 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1190 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1191 * the size of any referred-to data (dsize). We then round the final 1192 * size up to the chunksize for allocation. 1193 */ 1194 for (ksize = 0, i = 0; i < nkeys; i++) 1195 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1196 1197 /* 1198 * This should be pretty much impossible, but could happen if, say, 1199 * strange DIF specified the tuple. Ideally, this should be an 1200 * assertion and not an error condition -- but that requires that the 1201 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1202 * bullet-proof. (That is, it must not be able to be fooled by 1203 * malicious DIF.) Given the lack of backwards branches in DIF, 1204 * solving this would presumably not amount to solving the Halting 1205 * Problem -- but it still seems awfully hard. 1206 */ 1207 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1208 ksize + dsize > chunksize) { 1209 dcpu->dtdsc_drops++; 1210 return (NULL); 1211 } 1212 1213 nstate = DTRACE_DSTATE_EMPTY; 1214 1215 do { 1216 retry: 1217 free = dcpu->dtdsc_free; 1218 1219 if (free == NULL) { 1220 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1221 void *rval; 1222 1223 if (clean == NULL) { 1224 /* 1225 * We're out of dynamic variable space on 1226 * this CPU. Unless we have tried all CPUs, 1227 * we'll try to allocate from a different 1228 * CPU. 1229 */ 1230 switch (dstate->dtds_state) { 1231 case DTRACE_DSTATE_CLEAN: { 1232 void *sp = &dstate->dtds_state; 1233 1234 if (++cpu >= NCPU) 1235 cpu = 0; 1236 1237 if (dcpu->dtdsc_dirty != NULL && 1238 nstate == DTRACE_DSTATE_EMPTY) 1239 nstate = DTRACE_DSTATE_DIRTY; 1240 1241 if (dcpu->dtdsc_rinsing != NULL) 1242 nstate = DTRACE_DSTATE_RINSING; 1243 1244 dcpu = &dstate->dtds_percpu[cpu]; 1245 1246 if (cpu != me) 1247 goto retry; 1248 1249 (void) dtrace_cas32(sp, 1250 DTRACE_DSTATE_CLEAN, nstate); 1251 1252 /* 1253 * To increment the correct bean 1254 * counter, take another lap. 1255 */ 1256 goto retry; 1257 } 1258 1259 case DTRACE_DSTATE_DIRTY: 1260 dcpu->dtdsc_dirty_drops++; 1261 break; 1262 1263 case DTRACE_DSTATE_RINSING: 1264 dcpu->dtdsc_rinsing_drops++; 1265 break; 1266 1267 case DTRACE_DSTATE_EMPTY: 1268 dcpu->dtdsc_drops++; 1269 break; 1270 } 1271 1272 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1273 return (NULL); 1274 } 1275 1276 /* 1277 * The clean list appears to be non-empty. We want to 1278 * move the clean list to the free list; we start by 1279 * moving the clean pointer aside. 1280 */ 1281 if (dtrace_casptr(&dcpu->dtdsc_clean, 1282 clean, NULL) != clean) { 1283 /* 1284 * We are in one of two situations: 1285 * 1286 * (a) The clean list was switched to the 1287 * free list by another CPU. 1288 * 1289 * (b) The clean list was added to by the 1290 * cleansing cyclic. 1291 * 1292 * In either of these situations, we can 1293 * just reattempt the free list allocation. 1294 */ 1295 goto retry; 1296 } 1297 1298 ASSERT(clean->dtdv_hashval == 0); 1299 1300 /* 1301 * Now we'll move the clean list to the free list. 1302 * It's impossible for this to fail: the only way 1303 * the free list can be updated is through this 1304 * code path, and only one CPU can own the clean list. 1305 * Thus, it would only be possible for this to fail if 1306 * this code were racing with dtrace_dynvar_clean(). 1307 * (That is, if dtrace_dynvar_clean() updated the clean 1308 * list, and we ended up racing to update the free 1309 * list.) This race is prevented by the dtrace_sync() 1310 * in dtrace_dynvar_clean() -- which flushes the 1311 * owners of the clean lists out before resetting 1312 * the clean lists. 1313 */ 1314 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1315 ASSERT(rval == NULL); 1316 goto retry; 1317 } 1318 1319 dvar = free; 1320 new_free = dvar->dtdv_next; 1321 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1322 1323 /* 1324 * We have now allocated a new chunk. We copy the tuple keys into the 1325 * tuple array and copy any referenced key data into the data space 1326 * following the tuple array. As we do this, we relocate dttk_value 1327 * in the final tuple to point to the key data address in the chunk. 1328 */ 1329 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1330 dvar->dtdv_data = (void *)(kdata + ksize); 1331 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1332 1333 for (i = 0; i < nkeys; i++) { 1334 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1335 size_t kesize = key[i].dttk_size; 1336 1337 if (kesize != 0) { 1338 dtrace_bcopy( 1339 (const void *)(uintptr_t)key[i].dttk_value, 1340 (void *)kdata, kesize); 1341 dkey->dttk_value = kdata; 1342 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1343 } else { 1344 dkey->dttk_value = key[i].dttk_value; 1345 } 1346 1347 dkey->dttk_size = kesize; 1348 } 1349 1350 ASSERT(dvar->dtdv_hashval == 0); 1351 dvar->dtdv_hashval = hashval; 1352 dvar->dtdv_next = start; 1353 1354 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1355 return (dvar); 1356 1357 /* 1358 * The cas has failed. Either another CPU is adding an element to 1359 * this hash chain, or another CPU is deleting an element from this 1360 * hash chain. The simplest way to deal with both of these cases 1361 * (though not necessarily the most efficient) is to free our 1362 * allocated block and tail-call ourselves. Note that the free is 1363 * to the dirty list and _not_ to the free list. This is to prevent 1364 * races with allocators, above. 1365 */ 1366 dvar->dtdv_hashval = 0; 1367 1368 dtrace_membar_producer(); 1369 1370 do { 1371 free = dcpu->dtdsc_dirty; 1372 dvar->dtdv_next = free; 1373 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1374 1375 return (dtrace_dynvar(dstate, nkeys, key, dsize, op)); 1376 } 1377 1378 /*ARGSUSED*/ 1379 static void 1380 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1381 { 1382 if (nval < *oval) 1383 *oval = nval; 1384 } 1385 1386 /*ARGSUSED*/ 1387 static void 1388 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1389 { 1390 if (nval > *oval) 1391 *oval = nval; 1392 } 1393 1394 static void 1395 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1396 { 1397 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1398 int64_t val = (int64_t)nval; 1399 1400 if (val < 0) { 1401 for (i = 0; i < zero; i++) { 1402 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1403 quanta[i] += incr; 1404 return; 1405 } 1406 } 1407 } else { 1408 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1409 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1410 quanta[i - 1] += incr; 1411 return; 1412 } 1413 } 1414 1415 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1416 return; 1417 } 1418 1419 ASSERT(0); 1420 } 1421 1422 static void 1423 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1424 { 1425 uint64_t arg = *lquanta++; 1426 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1427 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1428 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1429 int32_t val = (int32_t)nval, level; 1430 1431 ASSERT(step != 0); 1432 ASSERT(levels != 0); 1433 1434 if (val < base) { 1435 /* 1436 * This is an underflow. 1437 */ 1438 lquanta[0] += incr; 1439 return; 1440 } 1441 1442 level = (val - base) / step; 1443 1444 if (level < levels) { 1445 lquanta[level + 1] += incr; 1446 return; 1447 } 1448 1449 /* 1450 * This is an overflow. 1451 */ 1452 lquanta[levels + 1] += incr; 1453 } 1454 1455 /*ARGSUSED*/ 1456 static void 1457 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 1458 { 1459 data[0]++; 1460 data[1] += nval; 1461 } 1462 1463 /*ARGSUSED*/ 1464 static void 1465 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 1466 { 1467 *oval = *oval + 1; 1468 } 1469 1470 /*ARGSUSED*/ 1471 static void 1472 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 1473 { 1474 *oval += nval; 1475 } 1476 1477 /* 1478 * Aggregate given the tuple in the principal data buffer, and the aggregating 1479 * action denoted by the specified dtrace_aggregation_t. The aggregation 1480 * buffer is specified as the buf parameter. This routine does not return 1481 * failure; if there is no space in the aggregation buffer, the data will be 1482 * dropped, and a corresponding counter incremented. 1483 */ 1484 static void 1485 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 1486 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 1487 { 1488 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 1489 uint32_t i, ndx, size, fsize; 1490 uint32_t align = sizeof (uint64_t) - 1; 1491 dtrace_aggbuffer_t *agb; 1492 dtrace_aggkey_t *key; 1493 uint32_t hashval = 0; 1494 caddr_t tomax, data, kdata; 1495 dtrace_actkind_t action; 1496 uintptr_t offs; 1497 1498 if (buf == NULL) 1499 return; 1500 1501 if (!agg->dtag_hasarg) { 1502 /* 1503 * Currently, only quantize() and lquantize() take additional 1504 * arguments, and they have the same semantics: an increment 1505 * value that defaults to 1 when not present. If additional 1506 * aggregating actions take arguments, the setting of the 1507 * default argument value will presumably have to become more 1508 * sophisticated... 1509 */ 1510 arg = 1; 1511 } 1512 1513 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 1514 size = rec->dtrd_offset - agg->dtag_base; 1515 fsize = size + rec->dtrd_size; 1516 1517 ASSERT(dbuf->dtb_tomax != NULL); 1518 data = dbuf->dtb_tomax + offset + agg->dtag_base; 1519 1520 if ((tomax = buf->dtb_tomax) == NULL) { 1521 dtrace_buffer_drop(buf); 1522 return; 1523 } 1524 1525 /* 1526 * The metastructure is always at the bottom of the buffer. 1527 */ 1528 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 1529 sizeof (dtrace_aggbuffer_t)); 1530 1531 if (buf->dtb_offset == 0) { 1532 /* 1533 * We just kludge up approximately 1/8th of the size to be 1534 * buckets. If this guess ends up being routinely 1535 * off-the-mark, we may need to dynamically readjust this 1536 * based on past performance. 1537 */ 1538 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 1539 1540 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 1541 (uintptr_t)tomax || hashsize == 0) { 1542 /* 1543 * We've been given a ludicrously small buffer; 1544 * increment our drop count and leave. 1545 */ 1546 dtrace_buffer_drop(buf); 1547 return; 1548 } 1549 1550 /* 1551 * And now, a pathetic attempt to try to get a an odd (or 1552 * perchance, a prime) hash size for better hash distribution. 1553 */ 1554 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 1555 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 1556 1557 agb->dtagb_hashsize = hashsize; 1558 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 1559 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 1560 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 1561 1562 for (i = 0; i < agb->dtagb_hashsize; i++) 1563 agb->dtagb_hash[i] = NULL; 1564 } 1565 1566 /* 1567 * Calculate the hash value based on the key. Note that we _don't_ 1568 * include the aggid in the hashing (but we will store it as part of 1569 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 1570 * algorithm: a simple, quick algorithm that has no known funnels, and 1571 * gets good distribution in practice. The efficacy of the hashing 1572 * algorithm (and a comparison with other algorithms) may be found by 1573 * running the ::dtrace_aggstat MDB dcmd. 1574 */ 1575 for (i = sizeof (dtrace_aggid_t); i < size; i++) { 1576 hashval += data[i]; 1577 hashval += (hashval << 10); 1578 hashval ^= (hashval >> 6); 1579 } 1580 1581 hashval += (hashval << 3); 1582 hashval ^= (hashval >> 11); 1583 hashval += (hashval << 15); 1584 1585 /* 1586 * Yes, the divide here is expensive. If the cycle count here becomes 1587 * prohibitive, we can do tricks to eliminate it. 1588 */ 1589 ndx = hashval % agb->dtagb_hashsize; 1590 1591 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 1592 ASSERT((caddr_t)key >= tomax); 1593 ASSERT((caddr_t)key < tomax + buf->dtb_size); 1594 1595 if (hashval != key->dtak_hashval || key->dtak_size != size) 1596 continue; 1597 1598 kdata = key->dtak_data; 1599 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 1600 1601 for (i = sizeof (dtrace_aggid_t); i < size; i++) { 1602 if (kdata[i] != data[i]) 1603 goto next; 1604 } 1605 1606 if (action != key->dtak_action) { 1607 /* 1608 * We are aggregating on the same value in the same 1609 * aggregation with two different aggregating actions. 1610 * (This should have been picked up in the compiler, 1611 * so we may be dealing with errant or devious DIF.) 1612 * This is an error condition; we indicate as much, 1613 * and return. 1614 */ 1615 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 1616 return; 1617 } 1618 1619 /* 1620 * This is a hit: we need to apply the aggregator to 1621 * the value at this key. 1622 */ 1623 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 1624 return; 1625 next: 1626 continue; 1627 } 1628 1629 /* 1630 * We didn't find it. We need to allocate some zero-filled space, 1631 * link it into the hash table appropriately, and apply the aggregator 1632 * to the (zero-filled) value. 1633 */ 1634 offs = buf->dtb_offset; 1635 while (offs & (align - 1)) 1636 offs += sizeof (uint32_t); 1637 1638 /* 1639 * If we don't have enough room to both allocate a new key _and_ 1640 * its associated data, increment the drop count and return. 1641 */ 1642 if ((uintptr_t)tomax + offs + fsize > 1643 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 1644 dtrace_buffer_drop(buf); 1645 return; 1646 } 1647 1648 /*CONSTCOND*/ 1649 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 1650 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 1651 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 1652 1653 key->dtak_data = kdata = tomax + offs; 1654 buf->dtb_offset = offs + fsize; 1655 1656 /* 1657 * Now copy the data across. 1658 */ 1659 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 1660 1661 for (i = sizeof (dtrace_aggid_t); i < size; i++) 1662 kdata[i] = data[i]; 1663 1664 for (i = size; i < fsize; i++) 1665 kdata[i] = 0; 1666 1667 key->dtak_hashval = hashval; 1668 key->dtak_size = size; 1669 key->dtak_action = action; 1670 key->dtak_next = agb->dtagb_hash[ndx]; 1671 agb->dtagb_hash[ndx] = key; 1672 1673 /* 1674 * Finally, apply the aggregator. 1675 */ 1676 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 1677 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 1678 } 1679 1680 /* 1681 * Given consumer state, this routine finds a speculation in the INACTIVE 1682 * state and transitions it into the ACTIVE state. If there is no speculation 1683 * in the INACTIVE state, 0 is returned. In this case, no error counter is 1684 * incremented -- it is up to the caller to take appropriate action. 1685 */ 1686 static int 1687 dtrace_speculation(dtrace_state_t *state) 1688 { 1689 int i = 0; 1690 dtrace_speculation_state_t current; 1691 uint32_t *stat = &state->dts_speculations_unavail, count; 1692 1693 while (i < state->dts_nspeculations) { 1694 dtrace_speculation_t *spec = &state->dts_speculations[i]; 1695 1696 current = spec->dtsp_state; 1697 1698 if (current != DTRACESPEC_INACTIVE) { 1699 if (current == DTRACESPEC_COMMITTINGMANY || 1700 current == DTRACESPEC_COMMITTING || 1701 current == DTRACESPEC_DISCARDING) 1702 stat = &state->dts_speculations_busy; 1703 i++; 1704 continue; 1705 } 1706 1707 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 1708 current, DTRACESPEC_ACTIVE) == current) 1709 return (i + 1); 1710 } 1711 1712 /* 1713 * We couldn't find a speculation. If we found as much as a single 1714 * busy speculation buffer, we'll attribute this failure as "busy" 1715 * instead of "unavail". 1716 */ 1717 do { 1718 count = *stat; 1719 } while (dtrace_cas32(stat, count, count + 1) != count); 1720 1721 return (0); 1722 } 1723 1724 /* 1725 * This routine commits an active speculation. If the specified speculation 1726 * is not in a valid state to perform a commit(), this routine will silently do 1727 * nothing. The state of the specified speculation is transitioned according 1728 * to the state transition diagram outlined in <sys/dtrace_impl.h> 1729 */ 1730 static void 1731 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 1732 dtrace_specid_t which) 1733 { 1734 dtrace_speculation_t *spec; 1735 dtrace_buffer_t *src, *dest; 1736 uintptr_t daddr, saddr, dlimit; 1737 dtrace_speculation_state_t current, new; 1738 intptr_t offs; 1739 1740 if (which == 0) 1741 return; 1742 1743 if (which > state->dts_nspeculations) { 1744 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 1745 return; 1746 } 1747 1748 spec = &state->dts_speculations[which - 1]; 1749 src = &spec->dtsp_buffer[cpu]; 1750 dest = &state->dts_buffer[cpu]; 1751 1752 do { 1753 current = spec->dtsp_state; 1754 1755 if (current == DTRACESPEC_COMMITTINGMANY) 1756 break; 1757 1758 switch (current) { 1759 case DTRACESPEC_INACTIVE: 1760 case DTRACESPEC_DISCARDING: 1761 return; 1762 1763 case DTRACESPEC_COMMITTING: 1764 /* 1765 * This is only possible if we are (a) commit()'ing 1766 * without having done a prior speculate() on this CPU 1767 * and (b) racing with another commit() on a different 1768 * CPU. There's nothing to do -- we just assert that 1769 * our offset is 0. 1770 */ 1771 ASSERT(src->dtb_offset == 0); 1772 return; 1773 1774 case DTRACESPEC_ACTIVE: 1775 new = DTRACESPEC_COMMITTING; 1776 break; 1777 1778 case DTRACESPEC_ACTIVEONE: 1779 /* 1780 * This speculation is active on one CPU. If our 1781 * buffer offset is non-zero, we know that the one CPU 1782 * must be us. Otherwise, we are committing on a 1783 * different CPU from the speculate(), and we must 1784 * rely on being asynchronously cleaned. 1785 */ 1786 if (src->dtb_offset != 0) { 1787 new = DTRACESPEC_COMMITTING; 1788 break; 1789 } 1790 /*FALLTHROUGH*/ 1791 1792 case DTRACESPEC_ACTIVEMANY: 1793 new = DTRACESPEC_COMMITTINGMANY; 1794 break; 1795 1796 default: 1797 ASSERT(0); 1798 } 1799 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 1800 current, new) != current); 1801 1802 /* 1803 * We have set the state to indicate that we are committing this 1804 * speculation. Now reserve the necessary space in the destination 1805 * buffer. 1806 */ 1807 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 1808 sizeof (uint64_t), state, NULL)) < 0) { 1809 dtrace_buffer_drop(dest); 1810 goto out; 1811 } 1812 1813 /* 1814 * We have the space; copy the buffer across. (Note that this is a 1815 * highly subobtimal bcopy(); in the unlikely event that this becomes 1816 * a serious performance issue, a high-performance DTrace-specific 1817 * bcopy() should obviously be invented.) 1818 */ 1819 daddr = (uintptr_t)dest->dtb_tomax + offs; 1820 dlimit = daddr + src->dtb_offset; 1821 saddr = (uintptr_t)src->dtb_tomax; 1822 1823 /* 1824 * First, the aligned portion. 1825 */ 1826 while (dlimit - daddr >= sizeof (uint64_t)) { 1827 *((uint64_t *)daddr) = *((uint64_t *)saddr); 1828 1829 daddr += sizeof (uint64_t); 1830 saddr += sizeof (uint64_t); 1831 } 1832 1833 /* 1834 * Now any left-over bit... 1835 */ 1836 while (dlimit - daddr) 1837 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 1838 1839 /* 1840 * Finally, commit the reserved space in the destination buffer. 1841 */ 1842 dest->dtb_offset = offs + src->dtb_offset; 1843 1844 out: 1845 /* 1846 * If we're lucky enough to be the only active CPU on this speculation 1847 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 1848 */ 1849 if (current == DTRACESPEC_ACTIVE || 1850 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 1851 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 1852 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 1853 1854 ASSERT(rval == DTRACESPEC_COMMITTING); 1855 } 1856 1857 src->dtb_offset = 0; 1858 src->dtb_xamot_drops += src->dtb_drops; 1859 src->dtb_drops = 0; 1860 } 1861 1862 /* 1863 * This routine discards an active speculation. If the specified speculation 1864 * is not in a valid state to perform a discard(), this routine will silently 1865 * do nothing. The state of the specified speculation is transitioned 1866 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 1867 */ 1868 static void 1869 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 1870 dtrace_specid_t which) 1871 { 1872 dtrace_speculation_t *spec; 1873 dtrace_speculation_state_t current, new; 1874 dtrace_buffer_t *buf; 1875 1876 if (which == 0) 1877 return; 1878 1879 if (which > state->dts_nspeculations) { 1880 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 1881 return; 1882 } 1883 1884 spec = &state->dts_speculations[which - 1]; 1885 buf = &spec->dtsp_buffer[cpu]; 1886 1887 do { 1888 current = spec->dtsp_state; 1889 1890 switch (current) { 1891 case DTRACESPEC_INACTIVE: 1892 case DTRACESPEC_COMMITTINGMANY: 1893 case DTRACESPEC_COMMITTING: 1894 case DTRACESPEC_DISCARDING: 1895 return; 1896 1897 case DTRACESPEC_ACTIVE: 1898 case DTRACESPEC_ACTIVEMANY: 1899 new = DTRACESPEC_DISCARDING; 1900 break; 1901 1902 case DTRACESPEC_ACTIVEONE: 1903 if (buf->dtb_offset != 0) { 1904 new = DTRACESPEC_INACTIVE; 1905 } else { 1906 new = DTRACESPEC_DISCARDING; 1907 } 1908 break; 1909 1910 default: 1911 ASSERT(0); 1912 } 1913 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 1914 current, new) != current); 1915 1916 buf->dtb_offset = 0; 1917 buf->dtb_drops = 0; 1918 } 1919 1920 /* 1921 * Note: not called from probe context. This function is called 1922 * asynchronously from cross call context to clean any speculations that are 1923 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 1924 * transitioned back to the INACTIVE state until all CPUs have cleaned the 1925 * speculation. 1926 */ 1927 static void 1928 dtrace_speculation_clean_here(dtrace_state_t *state) 1929 { 1930 dtrace_icookie_t cookie; 1931 processorid_t cpu = CPU->cpu_id; 1932 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 1933 dtrace_specid_t i; 1934 1935 cookie = dtrace_interrupt_disable(); 1936 1937 if (dest->dtb_tomax == NULL) { 1938 dtrace_interrupt_enable(cookie); 1939 return; 1940 } 1941 1942 for (i = 0; i < state->dts_nspeculations; i++) { 1943 dtrace_speculation_t *spec = &state->dts_speculations[i]; 1944 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 1945 1946 if (src->dtb_tomax == NULL) 1947 continue; 1948 1949 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 1950 src->dtb_offset = 0; 1951 continue; 1952 } 1953 1954 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 1955 continue; 1956 1957 if (src->dtb_offset == 0) 1958 continue; 1959 1960 dtrace_speculation_commit(state, cpu, i + 1); 1961 } 1962 1963 dtrace_interrupt_enable(cookie); 1964 } 1965 1966 /* 1967 * Note: not called from probe context. This function is called 1968 * asynchronously (and at a regular interval) to clean any speculations that 1969 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 1970 * is work to be done, it cross calls all CPUs to perform that work; 1971 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 1972 * INACTIVE state until they have been cleaned by all CPUs. 1973 */ 1974 static void 1975 dtrace_speculation_clean(dtrace_state_t *state) 1976 { 1977 int work = 0, rv; 1978 dtrace_specid_t i; 1979 1980 for (i = 0; i < state->dts_nspeculations; i++) { 1981 dtrace_speculation_t *spec = &state->dts_speculations[i]; 1982 1983 ASSERT(!spec->dtsp_cleaning); 1984 1985 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 1986 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 1987 continue; 1988 1989 work++; 1990 spec->dtsp_cleaning = 1; 1991 } 1992 1993 if (!work) 1994 return; 1995 1996 dtrace_xcall(DTRACE_CPUALL, 1997 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 1998 1999 /* 2000 * We now know that all CPUs have committed or discarded their 2001 * speculation buffers, as appropriate. We can now set the state 2002 * to inactive. 2003 */ 2004 for (i = 0; i < state->dts_nspeculations; i++) { 2005 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2006 dtrace_speculation_state_t current, new; 2007 2008 if (!spec->dtsp_cleaning) 2009 continue; 2010 2011 current = spec->dtsp_state; 2012 ASSERT(current == DTRACESPEC_DISCARDING || 2013 current == DTRACESPEC_COMMITTINGMANY); 2014 2015 new = DTRACESPEC_INACTIVE; 2016 2017 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2018 ASSERT(rv == current); 2019 spec->dtsp_cleaning = 0; 2020 } 2021 } 2022 2023 /* 2024 * Called as part of a speculate() to get the speculative buffer associated 2025 * with a given speculation. Returns NULL if the specified speculation is not 2026 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2027 * the active CPU is not the specified CPU -- the speculation will be 2028 * atomically transitioned into the ACTIVEMANY state. 2029 */ 2030 static dtrace_buffer_t * 2031 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2032 dtrace_specid_t which) 2033 { 2034 dtrace_speculation_t *spec; 2035 dtrace_speculation_state_t current, new; 2036 dtrace_buffer_t *buf; 2037 2038 if (which == 0) 2039 return (NULL); 2040 2041 if (which > state->dts_nspeculations) { 2042 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2043 return (NULL); 2044 } 2045 2046 spec = &state->dts_speculations[which - 1]; 2047 buf = &spec->dtsp_buffer[cpuid]; 2048 2049 do { 2050 current = spec->dtsp_state; 2051 2052 switch (current) { 2053 case DTRACESPEC_INACTIVE: 2054 case DTRACESPEC_COMMITTINGMANY: 2055 case DTRACESPEC_DISCARDING: 2056 return (NULL); 2057 2058 case DTRACESPEC_COMMITTING: 2059 ASSERT(buf->dtb_offset == 0); 2060 return (NULL); 2061 2062 case DTRACESPEC_ACTIVEONE: 2063 /* 2064 * This speculation is currently active on one CPU. 2065 * Check the offset in the buffer; if it's non-zero, 2066 * that CPU must be us (and we leave the state alone). 2067 * If it's zero, assume that we're starting on a new 2068 * CPU -- and change the state to indicate that the 2069 * speculation is active on more than one CPU. 2070 */ 2071 if (buf->dtb_offset != 0) 2072 return (buf); 2073 2074 new = DTRACESPEC_ACTIVEMANY; 2075 break; 2076 2077 case DTRACESPEC_ACTIVEMANY: 2078 return (buf); 2079 2080 case DTRACESPEC_ACTIVE: 2081 new = DTRACESPEC_ACTIVEONE; 2082 break; 2083 2084 default: 2085 ASSERT(0); 2086 } 2087 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2088 current, new) != current); 2089 2090 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2091 return (buf); 2092 } 2093 2094 /* 2095 * This function implements the DIF emulator's variable lookups. The emulator 2096 * passes a reserved variable identifier and optional built-in array index. 2097 */ 2098 static uint64_t 2099 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2100 uint64_t ndx) 2101 { 2102 /* 2103 * If we're accessing one of the uncached arguments, we'll turn this 2104 * into a reference in the args array. 2105 */ 2106 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2107 ndx = v - DIF_VAR_ARG0; 2108 v = DIF_VAR_ARGS; 2109 } 2110 2111 switch (v) { 2112 case DIF_VAR_ARGS: 2113 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2114 if (ndx >= sizeof (mstate->dtms_arg) / 2115 sizeof (mstate->dtms_arg[0])) { 2116 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2117 dtrace_provider_t *pv; 2118 uint64_t val; 2119 2120 pv = mstate->dtms_probe->dtpr_provider; 2121 if (pv->dtpv_pops.dtps_getargval != NULL) 2122 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2123 mstate->dtms_probe->dtpr_id, 2124 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2125 else 2126 val = dtrace_getarg(ndx, aframes); 2127 2128 /* 2129 * This is regrettably required to keep the compiler 2130 * from tail-optimizing the call to dtrace_getarg(). 2131 * The condition always evaluates to true, but the 2132 * compiler has no way of figuring that out a priori. 2133 * (None of this would be necessary if the compiler 2134 * could be relied upon to _always_ tail-optimize 2135 * the call to dtrace_getarg() -- but it can't.) 2136 */ 2137 if (mstate->dtms_probe != NULL) 2138 return (val); 2139 2140 ASSERT(0); 2141 } 2142 2143 return (mstate->dtms_arg[ndx]); 2144 2145 case DIF_VAR_UREGS: { 2146 klwp_t *lwp; 2147 2148 if (!dtrace_priv_proc(state)) 2149 return (0); 2150 2151 if ((lwp = curthread->t_lwp) == NULL) { 2152 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2153 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL; 2154 return (0); 2155 } 2156 2157 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2158 } 2159 2160 case DIF_VAR_CURTHREAD: 2161 if (!dtrace_priv_kernel(state)) 2162 return (0); 2163 return ((uint64_t)(uintptr_t)curthread); 2164 2165 case DIF_VAR_TIMESTAMP: 2166 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2167 mstate->dtms_timestamp = dtrace_gethrtime(); 2168 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2169 } 2170 return (mstate->dtms_timestamp); 2171 2172 case DIF_VAR_VTIMESTAMP: 2173 ASSERT(dtrace_vtime_references != 0); 2174 return (curthread->t_dtrace_vtime); 2175 2176 case DIF_VAR_WALLTIMESTAMP: 2177 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2178 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2179 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2180 } 2181 return (mstate->dtms_walltimestamp); 2182 2183 case DIF_VAR_IPL: 2184 if (!dtrace_priv_kernel(state)) 2185 return (0); 2186 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2187 mstate->dtms_ipl = dtrace_getipl(); 2188 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2189 } 2190 return (mstate->dtms_ipl); 2191 2192 case DIF_VAR_EPID: 2193 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2194 return (mstate->dtms_epid); 2195 2196 case DIF_VAR_ID: 2197 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2198 return (mstate->dtms_probe->dtpr_id); 2199 2200 case DIF_VAR_STACKDEPTH: 2201 if (!dtrace_priv_kernel(state)) 2202 return (0); 2203 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2204 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2205 2206 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2207 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2208 } 2209 return (mstate->dtms_stackdepth); 2210 2211 case DIF_VAR_USTACKDEPTH: 2212 if (!dtrace_priv_proc(state)) 2213 return (0); 2214 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2215 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2216 mstate->dtms_ustackdepth = dtrace_getustackdepth(); 2217 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2218 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2219 } 2220 return (mstate->dtms_ustackdepth); 2221 2222 case DIF_VAR_CALLER: 2223 if (!dtrace_priv_kernel(state)) 2224 return (0); 2225 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2226 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2227 2228 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2229 /* 2230 * If this is an unanchored probe, we are 2231 * required to go through the slow path: 2232 * dtrace_caller() only guarantees correct 2233 * results for anchored probes. 2234 */ 2235 pc_t caller[2]; 2236 2237 dtrace_getpcstack(caller, 2, aframes, 2238 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2239 mstate->dtms_caller = caller[1]; 2240 } else if ((mstate->dtms_caller = 2241 dtrace_caller(aframes)) == -1) { 2242 /* 2243 * We have failed to do this the quick way; 2244 * we must resort to the slower approach of 2245 * calling dtrace_getpcstack(). 2246 */ 2247 pc_t caller; 2248 2249 dtrace_getpcstack(&caller, 1, aframes, NULL); 2250 mstate->dtms_caller = caller; 2251 } 2252 2253 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 2254 } 2255 return (mstate->dtms_caller); 2256 2257 case DIF_VAR_UCALLER: 2258 if (!dtrace_priv_proc(state)) 2259 return (0); 2260 2261 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 2262 uint64_t ustack[3]; 2263 2264 /* 2265 * dtrace_getupcstack() fills in the first uint64_t 2266 * with the current PID. The second uint64_t will 2267 * be the program counter at user-level. The third 2268 * uint64_t will contain the caller, which is what 2269 * we're after. 2270 */ 2271 ustack[2] = NULL; 2272 dtrace_getupcstack(ustack, 3); 2273 mstate->dtms_ucaller = ustack[2]; 2274 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 2275 } 2276 2277 return (mstate->dtms_ucaller); 2278 2279 case DIF_VAR_PROBEPROV: 2280 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2281 return ((uint64_t)(uintptr_t) 2282 mstate->dtms_probe->dtpr_provider->dtpv_name); 2283 2284 case DIF_VAR_PROBEMOD: 2285 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2286 return ((uint64_t)(uintptr_t) 2287 mstate->dtms_probe->dtpr_mod); 2288 2289 case DIF_VAR_PROBEFUNC: 2290 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2291 return ((uint64_t)(uintptr_t) 2292 mstate->dtms_probe->dtpr_func); 2293 2294 case DIF_VAR_PROBENAME: 2295 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2296 return ((uint64_t)(uintptr_t) 2297 mstate->dtms_probe->dtpr_name); 2298 2299 case DIF_VAR_PID: 2300 if (!dtrace_priv_proc(state)) 2301 return (0); 2302 2303 /* 2304 * Note that we are assuming that an unanchored probe is 2305 * always due to a high-level interrupt. (And we're assuming 2306 * that there is only a single high level interrupt.) 2307 */ 2308 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2309 return (pid0.pid_id); 2310 2311 /* 2312 * It is always safe to dereference one's own t_procp pointer: 2313 * it always points to a valid, allocated proc structure. 2314 * Further, it is always safe to dereference the p_pidp member 2315 * of one's own proc structure. (These are truisms becuase 2316 * threads and processes don't clean up their own state -- 2317 * they leave that task to whomever reaps them.) 2318 */ 2319 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 2320 2321 case DIF_VAR_TID: 2322 /* 2323 * See comment in DIF_VAR_PID. 2324 */ 2325 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2326 return (0); 2327 2328 return ((uint64_t)curthread->t_tid); 2329 2330 case DIF_VAR_EXECNAME: 2331 if (!dtrace_priv_proc(state)) 2332 return (0); 2333 2334 /* 2335 * See comment in DIF_VAR_PID. 2336 */ 2337 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2338 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 2339 2340 /* 2341 * It is always safe to dereference one's own t_procp pointer: 2342 * it always points to a valid, allocated proc structure. 2343 * (This is true because threads don't clean up their own 2344 * state -- they leave that task to whomever reaps them.) 2345 */ 2346 return ((uint64_t)(uintptr_t) 2347 curthread->t_procp->p_user.u_comm); 2348 2349 case DIF_VAR_ZONENAME: 2350 if (!dtrace_priv_proc(state)) 2351 return (0); 2352 2353 /* 2354 * See comment in DIF_VAR_PID. 2355 */ 2356 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2357 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 2358 2359 /* 2360 * It is always safe to dereference one's own t_procp pointer: 2361 * it always points to a valid, allocated proc structure. 2362 * (This is true because threads don't clean up their own 2363 * state -- they leave that task to whomever reaps them.) 2364 */ 2365 return ((uint64_t)(uintptr_t) 2366 curthread->t_procp->p_zone->zone_name); 2367 2368 default: 2369 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2370 return (0); 2371 } 2372 } 2373 2374 /* 2375 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 2376 * Notice that we don't bother validating the proper number of arguments or 2377 * their types in the tuple stack. This isn't needed because all argument 2378 * interpretation is safe because of our load safety -- the worst that can 2379 * happen is that a bogus program can obtain bogus results. 2380 */ 2381 static void 2382 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 2383 dtrace_key_t *tupregs, int nargs, 2384 dtrace_mstate_t *mstate, dtrace_state_t *state) 2385 { 2386 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 2387 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 2388 2389 union { 2390 mutex_impl_t mi; 2391 uint64_t mx; 2392 } m; 2393 2394 union { 2395 krwlock_t ri; 2396 uintptr_t rw; 2397 } r; 2398 2399 switch (subr) { 2400 case DIF_SUBR_RAND: 2401 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 2402 break; 2403 2404 case DIF_SUBR_MUTEX_OWNED: 2405 m.mx = dtrace_load64(tupregs[0].dttk_value); 2406 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 2407 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 2408 else 2409 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 2410 break; 2411 2412 case DIF_SUBR_MUTEX_OWNER: 2413 m.mx = dtrace_load64(tupregs[0].dttk_value); 2414 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 2415 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 2416 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 2417 else 2418 regs[rd] = 0; 2419 break; 2420 2421 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 2422 m.mx = dtrace_load64(tupregs[0].dttk_value); 2423 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 2424 break; 2425 2426 case DIF_SUBR_MUTEX_TYPE_SPIN: 2427 m.mx = dtrace_load64(tupregs[0].dttk_value); 2428 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 2429 break; 2430 2431 case DIF_SUBR_RW_READ_HELD: { 2432 uintptr_t tmp; 2433 2434 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2435 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 2436 break; 2437 } 2438 2439 case DIF_SUBR_RW_WRITE_HELD: 2440 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2441 regs[rd] = _RW_WRITE_HELD(&r.ri); 2442 break; 2443 2444 case DIF_SUBR_RW_ISWRITER: 2445 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2446 regs[rd] = _RW_ISWRITER(&r.ri); 2447 break; 2448 2449 case DIF_SUBR_BCOPY: { 2450 /* 2451 * We need to be sure that the destination is in the scratch 2452 * region -- no other region is allowed. 2453 */ 2454 uintptr_t src = tupregs[0].dttk_value; 2455 uintptr_t dest = tupregs[1].dttk_value; 2456 size_t size = tupregs[2].dttk_value; 2457 2458 if (!dtrace_inscratch(dest, size, mstate)) { 2459 *flags |= CPU_DTRACE_BADADDR; 2460 *illval = regs[rd]; 2461 break; 2462 } 2463 2464 dtrace_bcopy((void *)src, (void *)dest, size); 2465 break; 2466 } 2467 2468 case DIF_SUBR_ALLOCA: 2469 case DIF_SUBR_COPYIN: { 2470 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 2471 uint64_t size = 2472 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 2473 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 2474 2475 /* 2476 * This action doesn't require any credential checks since 2477 * probes will not activate in user contexts to which the 2478 * enabling user does not have permissions. 2479 */ 2480 if (mstate->dtms_scratch_ptr + scratch_size > 2481 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2482 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2483 regs[rd] = NULL; 2484 break; 2485 } 2486 2487 if (subr == DIF_SUBR_COPYIN) { 2488 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2489 dtrace_copyin(tupregs[0].dttk_value, dest, size); 2490 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2491 } 2492 2493 mstate->dtms_scratch_ptr += scratch_size; 2494 regs[rd] = dest; 2495 break; 2496 } 2497 2498 case DIF_SUBR_COPYINTO: { 2499 uint64_t size = tupregs[1].dttk_value; 2500 uintptr_t dest = tupregs[2].dttk_value; 2501 2502 /* 2503 * This action doesn't require any credential checks since 2504 * probes will not activate in user contexts to which the 2505 * enabling user does not have permissions. 2506 */ 2507 if (!dtrace_inscratch(dest, size, mstate)) { 2508 *flags |= CPU_DTRACE_BADADDR; 2509 *illval = regs[rd]; 2510 break; 2511 } 2512 2513 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2514 dtrace_copyin(tupregs[0].dttk_value, dest, size); 2515 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2516 break; 2517 } 2518 2519 case DIF_SUBR_COPYINSTR: { 2520 uintptr_t dest = mstate->dtms_scratch_ptr; 2521 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2522 2523 if (nargs > 1 && tupregs[1].dttk_value < size) 2524 size = tupregs[1].dttk_value + 1; 2525 2526 /* 2527 * This action doesn't require any credential checks since 2528 * probes will not activate in user contexts to which the 2529 * enabling user does not have permissions. 2530 */ 2531 if (mstate->dtms_scratch_ptr + size > 2532 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2533 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2534 regs[rd] = NULL; 2535 break; 2536 } 2537 2538 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2539 dtrace_copyinstr(tupregs[0].dttk_value, dest, size); 2540 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2541 2542 ((char *)dest)[size - 1] = '\0'; 2543 mstate->dtms_scratch_ptr += size; 2544 regs[rd] = dest; 2545 break; 2546 } 2547 2548 case DIF_SUBR_MSGSIZE: 2549 case DIF_SUBR_MSGDSIZE: { 2550 uintptr_t baddr = tupregs[0].dttk_value, daddr; 2551 uintptr_t wptr, rptr; 2552 size_t count = 0; 2553 int cont = 0; 2554 2555 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 2556 wptr = dtrace_loadptr(baddr + 2557 offsetof(mblk_t, b_wptr)); 2558 2559 rptr = dtrace_loadptr(baddr + 2560 offsetof(mblk_t, b_rptr)); 2561 2562 if (wptr < rptr) { 2563 *flags |= CPU_DTRACE_BADADDR; 2564 *illval = tupregs[0].dttk_value; 2565 break; 2566 } 2567 2568 daddr = dtrace_loadptr(baddr + 2569 offsetof(mblk_t, b_datap)); 2570 2571 baddr = dtrace_loadptr(baddr + 2572 offsetof(mblk_t, b_cont)); 2573 2574 /* 2575 * We want to prevent against denial-of-service here, 2576 * so we're only going to search the list for 2577 * dtrace_msgdsize_max mblks. 2578 */ 2579 if (cont++ > dtrace_msgdsize_max) { 2580 *flags |= CPU_DTRACE_ILLOP; 2581 break; 2582 } 2583 2584 if (subr == DIF_SUBR_MSGDSIZE) { 2585 if (dtrace_load8(daddr + 2586 offsetof(dblk_t, db_type)) != M_DATA) 2587 continue; 2588 } 2589 2590 count += wptr - rptr; 2591 } 2592 2593 if (!(*flags & CPU_DTRACE_FAULT)) 2594 regs[rd] = count; 2595 2596 break; 2597 } 2598 2599 case DIF_SUBR_PROGENYOF: { 2600 pid_t pid = tupregs[0].dttk_value; 2601 proc_t *p; 2602 int rval = 0; 2603 2604 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2605 2606 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 2607 if (p->p_pidp->pid_id == pid) { 2608 rval = 1; 2609 break; 2610 } 2611 } 2612 2613 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2614 2615 regs[rd] = rval; 2616 break; 2617 } 2618 2619 case DIF_SUBR_SPECULATION: 2620 regs[rd] = dtrace_speculation(state); 2621 break; 2622 2623 case DIF_SUBR_COPYOUT: { 2624 uintptr_t kaddr = tupregs[0].dttk_value; 2625 uintptr_t uaddr = tupregs[1].dttk_value; 2626 uint64_t size = tupregs[2].dttk_value; 2627 2628 if (!dtrace_destructive_disallow && 2629 dtrace_priv_proc_control(state) && 2630 !dtrace_istoxic(kaddr, size)) { 2631 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2632 dtrace_copyout(kaddr, uaddr, size); 2633 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2634 } 2635 break; 2636 } 2637 2638 case DIF_SUBR_COPYOUTSTR: { 2639 uintptr_t kaddr = tupregs[0].dttk_value; 2640 uintptr_t uaddr = tupregs[1].dttk_value; 2641 uint64_t size = tupregs[2].dttk_value; 2642 2643 if (!dtrace_destructive_disallow && 2644 dtrace_priv_proc_control(state) && 2645 !dtrace_istoxic(kaddr, size)) { 2646 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2647 dtrace_copyoutstr(kaddr, uaddr, size); 2648 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2649 } 2650 break; 2651 } 2652 2653 case DIF_SUBR_STRLEN: 2654 regs[rd] = dtrace_strlen((char *)(uintptr_t) 2655 tupregs[0].dttk_value, 2656 state->dts_options[DTRACEOPT_STRSIZE]); 2657 break; 2658 2659 case DIF_SUBR_STRCHR: 2660 case DIF_SUBR_STRRCHR: { 2661 /* 2662 * We're going to iterate over the string looking for the 2663 * specified character. We will iterate until we have reached 2664 * the string length or we have found the character. If this 2665 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 2666 * of the specified character instead of the first. 2667 */ 2668 uintptr_t addr = tupregs[0].dttk_value; 2669 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 2670 char c, target = (char)tupregs[1].dttk_value; 2671 2672 for (regs[rd] = NULL; addr < limit; addr++) { 2673 if ((c = dtrace_load8(addr)) == target) { 2674 regs[rd] = addr; 2675 2676 if (subr == DIF_SUBR_STRCHR) 2677 break; 2678 } 2679 2680 if (c == '\0') 2681 break; 2682 } 2683 2684 break; 2685 } 2686 2687 case DIF_SUBR_STRSTR: 2688 case DIF_SUBR_INDEX: 2689 case DIF_SUBR_RINDEX: { 2690 /* 2691 * We're going to iterate over the string looking for the 2692 * specified string. We will iterate until we have reached 2693 * the string length or we have found the string. (Yes, this 2694 * is done in the most naive way possible -- but considering 2695 * that the string we're searching for is likely to be 2696 * relatively short, the complexity of Rabin-Karp or similar 2697 * hardly seems merited.) 2698 */ 2699 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 2700 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 2701 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2702 size_t len = dtrace_strlen(addr, size); 2703 size_t sublen = dtrace_strlen(substr, size); 2704 char *limit = addr + len, *orig = addr; 2705 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 2706 int inc = 1; 2707 2708 regs[rd] = notfound; 2709 2710 /* 2711 * strstr() and index()/rindex() have similar semantics if 2712 * both strings are the empty string: strstr() returns a 2713 * pointer to the (empty) string, and index() and rindex() 2714 * both return index 0 (regardless of any position argument). 2715 */ 2716 if (sublen == 0 && len == 0) { 2717 if (subr == DIF_SUBR_STRSTR) 2718 regs[rd] = (uintptr_t)addr; 2719 else 2720 regs[rd] = 0; 2721 break; 2722 } 2723 2724 if (subr != DIF_SUBR_STRSTR) { 2725 if (subr == DIF_SUBR_RINDEX) { 2726 limit = orig - 1; 2727 addr += len; 2728 inc = -1; 2729 } 2730 2731 /* 2732 * Both index() and rindex() take an optional position 2733 * argument that denotes the starting position. 2734 */ 2735 if (nargs == 3) { 2736 int64_t pos = (int64_t)tupregs[2].dttk_value; 2737 2738 /* 2739 * If the position argument to index() is 2740 * negative, Perl implicitly clamps it at 2741 * zero. This semantic is a little surprising 2742 * given the special meaning of negative 2743 * positions to similar Perl functions like 2744 * substr(), but it appears to reflect a 2745 * notion that index() can start from a 2746 * negative index and increment its way up to 2747 * the string. Given this notion, Perl's 2748 * rindex() is at least self-consistent in 2749 * that it implicitly clamps positions greater 2750 * than the string length to be the string 2751 * length. Where Perl completely loses 2752 * coherence, however, is when the specified 2753 * substring is the empty string (""). In 2754 * this case, even if the position is 2755 * negative, rindex() returns 0 -- and even if 2756 * the position is greater than the length, 2757 * index() returns the string length. These 2758 * semantics violate the notion that index() 2759 * should never return a value less than the 2760 * specified position and that rindex() should 2761 * never return a value greater than the 2762 * specified position. (One assumes that 2763 * these semantics are artifacts of Perl's 2764 * implementation and not the results of 2765 * deliberate design -- it beggars belief that 2766 * even Larry Wall could desire such oddness.) 2767 * While in the abstract one would wish for 2768 * consistent position semantics across 2769 * substr(), index() and rindex() -- or at the 2770 * very least self-consistent position 2771 * semantics for index() and rindex() -- we 2772 * instead opt to keep with the extant Perl 2773 * semantics, in all their broken glory. (Do 2774 * we have more desire to maintain Perl's 2775 * semantics than Perl does? Probably.) 2776 */ 2777 if (subr == DIF_SUBR_RINDEX) { 2778 if (pos < 0) { 2779 if (sublen == 0) 2780 regs[rd] = 0; 2781 break; 2782 } 2783 2784 if (pos > len) 2785 pos = len; 2786 } else { 2787 if (pos < 0) 2788 pos = 0; 2789 2790 if (pos >= len) { 2791 if (sublen == 0) 2792 regs[rd] = len; 2793 break; 2794 } 2795 } 2796 2797 addr = orig + pos; 2798 } 2799 } 2800 2801 for (regs[rd] = notfound; addr != limit; addr += inc) { 2802 if (dtrace_strncmp(addr, substr, sublen) == 0) { 2803 if (subr != DIF_SUBR_STRSTR) { 2804 /* 2805 * As D index() and rindex() are 2806 * modeled on Perl (and not on awk), 2807 * we return a zero-based (and not a 2808 * one-based) index. (For you Perl 2809 * weenies: no, we're not going to add 2810 * $[ -- and shouldn't you be at a con 2811 * or something?) 2812 */ 2813 regs[rd] = (uintptr_t)(addr - orig); 2814 break; 2815 } 2816 2817 ASSERT(subr == DIF_SUBR_STRSTR); 2818 regs[rd] = (uintptr_t)addr; 2819 break; 2820 } 2821 } 2822 2823 break; 2824 } 2825 2826 case DIF_SUBR_STRTOK: { 2827 uintptr_t addr = tupregs[0].dttk_value; 2828 uintptr_t tokaddr = tupregs[1].dttk_value; 2829 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2830 uintptr_t limit, toklimit = tokaddr + size; 2831 uint8_t c, tokmap[32]; /* 256 / 8 */ 2832 char *dest = (char *)mstate->dtms_scratch_ptr; 2833 int i; 2834 2835 if (mstate->dtms_scratch_ptr + size > 2836 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2837 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2838 regs[rd] = NULL; 2839 break; 2840 } 2841 2842 if (addr == NULL) { 2843 /* 2844 * If the address specified is NULL, we use our saved 2845 * strtok pointer from the mstate. Note that this 2846 * means that the saved strtok pointer is _only_ 2847 * valid within multiple enablings of the same probe -- 2848 * it behaves like an implicit clause-local variable. 2849 */ 2850 addr = mstate->dtms_strtok; 2851 } 2852 2853 /* 2854 * First, zero the token map, and then process the token 2855 * string -- setting a bit in the map for every character 2856 * found in the token string. 2857 */ 2858 for (i = 0; i < sizeof (tokmap); i++) 2859 tokmap[i] = 0; 2860 2861 for (; tokaddr < toklimit; tokaddr++) { 2862 if ((c = dtrace_load8(tokaddr)) == '\0') 2863 break; 2864 2865 ASSERT((c >> 3) < sizeof (tokmap)); 2866 tokmap[c >> 3] |= (1 << (c & 0x7)); 2867 } 2868 2869 for (limit = addr + size; addr < limit; addr++) { 2870 /* 2871 * We're looking for a character that is _not_ contained 2872 * in the token string. 2873 */ 2874 if ((c = dtrace_load8(addr)) == '\0') 2875 break; 2876 2877 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 2878 break; 2879 } 2880 2881 if (c == '\0') { 2882 /* 2883 * We reached the end of the string without finding 2884 * any character that was not in the token string. 2885 * We return NULL in this case, and we set the saved 2886 * address to NULL as well. 2887 */ 2888 regs[rd] = NULL; 2889 mstate->dtms_strtok = NULL; 2890 break; 2891 } 2892 2893 /* 2894 * From here on, we're copying into the destination string. 2895 */ 2896 for (i = 0; addr < limit && i < size - 1; addr++) { 2897 if ((c = dtrace_load8(addr)) == '\0') 2898 break; 2899 2900 if (tokmap[c >> 3] & (1 << (c & 0x7))) 2901 break; 2902 2903 ASSERT(i < size); 2904 dest[i++] = c; 2905 } 2906 2907 ASSERT(i < size); 2908 dest[i] = '\0'; 2909 regs[rd] = (uintptr_t)dest; 2910 mstate->dtms_scratch_ptr += size; 2911 mstate->dtms_strtok = addr; 2912 break; 2913 } 2914 2915 case DIF_SUBR_SUBSTR: { 2916 uintptr_t s = tupregs[0].dttk_value; 2917 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2918 char *d = (char *)mstate->dtms_scratch_ptr; 2919 int64_t index = (int64_t)tupregs[1].dttk_value; 2920 int64_t remaining = (int64_t)tupregs[2].dttk_value; 2921 size_t len = dtrace_strlen((char *)s, size); 2922 int64_t i = 0; 2923 2924 if (nargs <= 2) 2925 remaining = (int64_t)size; 2926 2927 if (mstate->dtms_scratch_ptr + size > 2928 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2929 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2930 regs[rd] = NULL; 2931 break; 2932 } 2933 2934 if (index < 0) { 2935 index += len; 2936 2937 if (index < 0 && index + remaining > 0) { 2938 remaining += index; 2939 index = 0; 2940 } 2941 } 2942 2943 if (index >= len || index < 0) 2944 index = len; 2945 2946 for (d[0] = '\0'; remaining > 0; remaining--) { 2947 if ((d[i++] = dtrace_load8(s++ + index)) == '\0') 2948 break; 2949 2950 if (i == size) { 2951 d[i - 1] = '\0'; 2952 break; 2953 } 2954 } 2955 2956 mstate->dtms_scratch_ptr += size; 2957 regs[rd] = (uintptr_t)d; 2958 break; 2959 } 2960 2961 case DIF_SUBR_GETMAJOR: 2962 #ifdef _LP64 2963 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 2964 #else 2965 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 2966 #endif 2967 break; 2968 2969 case DIF_SUBR_GETMINOR: 2970 #ifdef _LP64 2971 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 2972 #else 2973 regs[rd] = tupregs[0].dttk_value & MAXMIN; 2974 #endif 2975 break; 2976 2977 case DIF_SUBR_DDI_PATHNAME: { 2978 /* 2979 * This one is a galactic mess. We are going to roughly 2980 * emulate ddi_pathname(), but it's made more complicated 2981 * by the fact that we (a) want to include the minor name and 2982 * (b) must proceed iteratively instead of recursively. 2983 */ 2984 uintptr_t dest = mstate->dtms_scratch_ptr; 2985 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2986 char *start = (char *)dest, *end = start + size - 1; 2987 uintptr_t daddr = tupregs[0].dttk_value; 2988 int64_t minor = (int64_t)tupregs[1].dttk_value; 2989 char *s; 2990 int i, len, depth = 0; 2991 2992 if (size == 0 || mstate->dtms_scratch_ptr + size > 2993 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2994 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2995 regs[rd] = NULL; 2996 break; 2997 } 2998 2999 *end = '\0'; 3000 3001 /* 3002 * We want to have a name for the minor. In order to do this, 3003 * we need to walk the minor list from the devinfo. We want 3004 * to be sure that we don't infinitely walk a circular list, 3005 * so we check for circularity by sending a scout pointer 3006 * ahead two elements for every element that we iterate over; 3007 * if the list is circular, these will ultimately point to the 3008 * same element. You may recognize this little trick as the 3009 * answer to a stupid interview question -- one that always 3010 * seems to be asked by those who had to have it laboriously 3011 * explained to them, and who can't even concisely describe 3012 * the conditions under which one would be forced to resort to 3013 * this technique. Needless to say, those conditions are 3014 * found here -- and probably only here. Is this is the only 3015 * use of this infamous trick in shipping, production code? 3016 * If it isn't, it probably should be... 3017 */ 3018 if (minor != -1) { 3019 uintptr_t maddr = dtrace_loadptr(daddr + 3020 offsetof(struct dev_info, devi_minor)); 3021 3022 uintptr_t next = offsetof(struct ddi_minor_data, next); 3023 uintptr_t name = offsetof(struct ddi_minor_data, 3024 d_minor) + offsetof(struct ddi_minor, name); 3025 uintptr_t dev = offsetof(struct ddi_minor_data, 3026 d_minor) + offsetof(struct ddi_minor, dev); 3027 uintptr_t scout; 3028 3029 if (maddr != NULL) 3030 scout = dtrace_loadptr(maddr + next); 3031 3032 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3033 uint64_t m; 3034 #ifdef _LP64 3035 m = dtrace_load64(maddr + dev) & MAXMIN64; 3036 #else 3037 m = dtrace_load32(maddr + dev) & MAXMIN; 3038 #endif 3039 if (m != minor) { 3040 maddr = dtrace_loadptr(maddr + next); 3041 3042 if (scout == NULL) 3043 continue; 3044 3045 scout = dtrace_loadptr(scout + next); 3046 3047 if (scout == NULL) 3048 continue; 3049 3050 scout = dtrace_loadptr(scout + next); 3051 3052 if (scout == NULL) 3053 continue; 3054 3055 if (scout == maddr) { 3056 *flags |= CPU_DTRACE_ILLOP; 3057 break; 3058 } 3059 3060 continue; 3061 } 3062 3063 /* 3064 * We have the minor data. Now we need to 3065 * copy the minor's name into the end of the 3066 * pathname. 3067 */ 3068 s = (char *)dtrace_loadptr(maddr + name); 3069 len = dtrace_strlen(s, size); 3070 3071 if (*flags & CPU_DTRACE_FAULT) 3072 break; 3073 3074 if (len != 0) { 3075 if ((end -= (len + 1)) < start) 3076 break; 3077 3078 *end = ':'; 3079 } 3080 3081 for (i = 1; i <= len; i++) 3082 end[i] = dtrace_load8((uintptr_t)s++); 3083 break; 3084 } 3085 } 3086 3087 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3088 ddi_node_state_t devi_state; 3089 3090 devi_state = dtrace_load32(daddr + 3091 offsetof(struct dev_info, devi_node_state)); 3092 3093 if (*flags & CPU_DTRACE_FAULT) 3094 break; 3095 3096 if (devi_state >= DS_INITIALIZED) { 3097 s = (char *)dtrace_loadptr(daddr + 3098 offsetof(struct dev_info, devi_addr)); 3099 len = dtrace_strlen(s, size); 3100 3101 if (*flags & CPU_DTRACE_FAULT) 3102 break; 3103 3104 if (len != 0) { 3105 if ((end -= (len + 1)) < start) 3106 break; 3107 3108 *end = '@'; 3109 } 3110 3111 for (i = 1; i <= len; i++) 3112 end[i] = dtrace_load8((uintptr_t)s++); 3113 } 3114 3115 /* 3116 * Now for the node name... 3117 */ 3118 s = (char *)dtrace_loadptr(daddr + 3119 offsetof(struct dev_info, devi_node_name)); 3120 3121 daddr = dtrace_loadptr(daddr + 3122 offsetof(struct dev_info, devi_parent)); 3123 3124 /* 3125 * If our parent is NULL (that is, if we're the root 3126 * node), we're going to use the special path 3127 * "devices". 3128 */ 3129 if (daddr == NULL) 3130 s = "devices"; 3131 3132 len = dtrace_strlen(s, size); 3133 if (*flags & CPU_DTRACE_FAULT) 3134 break; 3135 3136 if ((end -= (len + 1)) < start) 3137 break; 3138 3139 for (i = 1; i <= len; i++) 3140 end[i] = dtrace_load8((uintptr_t)s++); 3141 *end = '/'; 3142 3143 if (depth++ > dtrace_devdepth_max) { 3144 *flags |= CPU_DTRACE_ILLOP; 3145 break; 3146 } 3147 } 3148 3149 if (end < start) 3150 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3151 3152 if (daddr == NULL) { 3153 regs[rd] = (uintptr_t)end; 3154 mstate->dtms_scratch_ptr += size; 3155 } 3156 3157 break; 3158 } 3159 3160 case DIF_SUBR_STRJOIN: { 3161 char *d = (char *)mstate->dtms_scratch_ptr; 3162 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3163 uintptr_t s1 = tupregs[0].dttk_value; 3164 uintptr_t s2 = tupregs[1].dttk_value; 3165 int i = 0; 3166 3167 if (mstate->dtms_scratch_ptr + size > 3168 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3169 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3170 regs[rd] = NULL; 3171 break; 3172 } 3173 3174 for (;;) { 3175 if (i >= size) { 3176 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3177 regs[rd] = NULL; 3178 break; 3179 } 3180 3181 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 3182 i--; 3183 break; 3184 } 3185 } 3186 3187 for (;;) { 3188 if (i >= size) { 3189 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3190 regs[rd] = NULL; 3191 break; 3192 } 3193 3194 if ((d[i++] = dtrace_load8(s2++)) == '\0') 3195 break; 3196 } 3197 3198 if (i < size) { 3199 mstate->dtms_scratch_ptr += i; 3200 regs[rd] = (uintptr_t)d; 3201 } 3202 3203 break; 3204 } 3205 3206 case DIF_SUBR_LLTOSTR: { 3207 int64_t i = (int64_t)tupregs[0].dttk_value; 3208 int64_t val = i < 0 ? i * -1 : i; 3209 uint64_t size = 22; /* enough room for 2^64 in decimal */ 3210 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 3211 3212 if (mstate->dtms_scratch_ptr + size > 3213 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3214 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3215 regs[rd] = NULL; 3216 break; 3217 } 3218 3219 for (*end-- = '\0'; val; val /= 10) 3220 *end-- = '0' + (val % 10); 3221 3222 if (i == 0) 3223 *end-- = '0'; 3224 3225 if (i < 0) 3226 *end-- = '-'; 3227 3228 regs[rd] = (uintptr_t)end + 1; 3229 mstate->dtms_scratch_ptr += size; 3230 break; 3231 } 3232 3233 case DIF_SUBR_DIRNAME: 3234 case DIF_SUBR_BASENAME: { 3235 char *dest = (char *)mstate->dtms_scratch_ptr; 3236 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3237 uintptr_t src = tupregs[0].dttk_value; 3238 int i, j, len = dtrace_strlen((char *)src, size); 3239 int lastbase = -1, firstbase = -1, lastdir = -1; 3240 int start, end; 3241 3242 if (mstate->dtms_scratch_ptr + size > 3243 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3244 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3245 regs[rd] = NULL; 3246 break; 3247 } 3248 3249 /* 3250 * The basename and dirname for a zero-length string is 3251 * defined to be "." 3252 */ 3253 if (len == 0) { 3254 len = 1; 3255 src = (uintptr_t)"."; 3256 } 3257 3258 /* 3259 * Start from the back of the string, moving back toward the 3260 * front until we see a character that isn't a slash. That 3261 * character is the last character in the basename. 3262 */ 3263 for (i = len - 1; i >= 0; i--) { 3264 if (dtrace_load8(src + i) != '/') 3265 break; 3266 } 3267 3268 if (i >= 0) 3269 lastbase = i; 3270 3271 /* 3272 * Starting from the last character in the basename, move 3273 * towards the front until we find a slash. The character 3274 * that we processed immediately before that is the first 3275 * character in the basename. 3276 */ 3277 for (; i >= 0; i--) { 3278 if (dtrace_load8(src + i) == '/') 3279 break; 3280 } 3281 3282 if (i >= 0) 3283 firstbase = i + 1; 3284 3285 /* 3286 * Now keep going until we find a non-slash character. That 3287 * character is the last character in the dirname. 3288 */ 3289 for (; i >= 0; i--) { 3290 if (dtrace_load8(src + i) != '/') 3291 break; 3292 } 3293 3294 if (i >= 0) 3295 lastdir = i; 3296 3297 ASSERT(!(lastbase == -1 && firstbase != -1)); 3298 ASSERT(!(firstbase == -1 && lastdir != -1)); 3299 3300 if (lastbase == -1) { 3301 /* 3302 * We didn't find a non-slash character. We know that 3303 * the length is non-zero, so the whole string must be 3304 * slashes. In either the dirname or the basename 3305 * case, we return '/'. 3306 */ 3307 ASSERT(firstbase == -1); 3308 firstbase = lastbase = lastdir = 0; 3309 } 3310 3311 if (firstbase == -1) { 3312 /* 3313 * The entire string consists only of a basename 3314 * component. If we're looking for dirname, we need 3315 * to change our string to be just "."; if we're 3316 * looking for a basename, we'll just set the first 3317 * character of the basename to be 0. 3318 */ 3319 if (subr == DIF_SUBR_DIRNAME) { 3320 ASSERT(lastdir == -1); 3321 src = (uintptr_t)"."; 3322 lastdir = 0; 3323 } else { 3324 firstbase = 0; 3325 } 3326 } 3327 3328 if (subr == DIF_SUBR_DIRNAME) { 3329 if (lastdir == -1) { 3330 /* 3331 * We know that we have a slash in the name -- 3332 * or lastdir would be set to 0, above. And 3333 * because lastdir is -1, we know that this 3334 * slash must be the first character. (That 3335 * is, the full string must be of the form 3336 * "/basename".) In this case, the last 3337 * character of the directory name is 0. 3338 */ 3339 lastdir = 0; 3340 } 3341 3342 start = 0; 3343 end = lastdir; 3344 } else { 3345 ASSERT(subr == DIF_SUBR_BASENAME); 3346 ASSERT(firstbase != -1 && lastbase != -1); 3347 start = firstbase; 3348 end = lastbase; 3349 } 3350 3351 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 3352 dest[j] = dtrace_load8(src + i); 3353 3354 dest[j] = '\0'; 3355 regs[rd] = (uintptr_t)dest; 3356 mstate->dtms_scratch_ptr += size; 3357 break; 3358 } 3359 3360 case DIF_SUBR_CLEANPATH: { 3361 char *dest = (char *)mstate->dtms_scratch_ptr, c; 3362 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3363 uintptr_t src = tupregs[0].dttk_value; 3364 int i = 0, j = 0; 3365 3366 if (mstate->dtms_scratch_ptr + size > 3367 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3368 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3369 regs[rd] = NULL; 3370 break; 3371 } 3372 3373 /* 3374 * Move forward, loading each character. 3375 */ 3376 do { 3377 c = dtrace_load8(src + i++); 3378 next: 3379 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 3380 break; 3381 3382 if (c != '/') { 3383 dest[j++] = c; 3384 continue; 3385 } 3386 3387 c = dtrace_load8(src + i++); 3388 3389 if (c == '/') { 3390 /* 3391 * We have two slashes -- we can just advance 3392 * to the next character. 3393 */ 3394 goto next; 3395 } 3396 3397 if (c != '.') { 3398 /* 3399 * This is not "." and it's not ".." -- we can 3400 * just store the "/" and this character and 3401 * drive on. 3402 */ 3403 dest[j++] = '/'; 3404 dest[j++] = c; 3405 continue; 3406 } 3407 3408 c = dtrace_load8(src + i++); 3409 3410 if (c == '/') { 3411 /* 3412 * This is a "/./" component. We're not going 3413 * to store anything in the destination buffer; 3414 * we're just going to go to the next component. 3415 */ 3416 goto next; 3417 } 3418 3419 if (c != '.') { 3420 /* 3421 * This is not ".." -- we can just store the 3422 * "/." and this character and continue 3423 * processing. 3424 */ 3425 dest[j++] = '/'; 3426 dest[j++] = '.'; 3427 dest[j++] = c; 3428 continue; 3429 } 3430 3431 c = dtrace_load8(src + i++); 3432 3433 if (c != '/' && c != '\0') { 3434 /* 3435 * This is not ".." -- it's "..[mumble]". 3436 * We'll store the "/.." and this character 3437 * and continue processing. 3438 */ 3439 dest[j++] = '/'; 3440 dest[j++] = '.'; 3441 dest[j++] = '.'; 3442 dest[j++] = c; 3443 continue; 3444 } 3445 3446 /* 3447 * This is "/../" or "/..\0". We need to back up 3448 * our destination pointer until we find a "/". 3449 */ 3450 i--; 3451 while (j != 0 && dest[--j] != '/') 3452 continue; 3453 3454 if (c == '\0') 3455 dest[++j] = '/'; 3456 } while (c != '\0'); 3457 3458 dest[j] = '\0'; 3459 regs[rd] = (uintptr_t)dest; 3460 mstate->dtms_scratch_ptr += size; 3461 break; 3462 } 3463 } 3464 } 3465 3466 /* 3467 * Emulate the execution of DTrace IR instructions specified by the given 3468 * DIF object. This function is deliberately void of assertions as all of 3469 * the necessary checks are handled by a call to dtrace_difo_validate(). 3470 */ 3471 static uint64_t 3472 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 3473 dtrace_vstate_t *vstate, dtrace_state_t *state) 3474 { 3475 const dif_instr_t *text = difo->dtdo_buf; 3476 const uint_t textlen = difo->dtdo_len; 3477 const char *strtab = difo->dtdo_strtab; 3478 const uint64_t *inttab = difo->dtdo_inttab; 3479 3480 uint64_t rval = 0; 3481 dtrace_statvar_t *svar; 3482 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 3483 dtrace_difv_t *v; 3484 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 3485 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 3486 3487 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 3488 uint64_t regs[DIF_DIR_NREGS]; 3489 uint64_t *tmp; 3490 3491 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 3492 int64_t cc_r; 3493 uint_t pc = 0, id, opc; 3494 uint8_t ttop = 0; 3495 dif_instr_t instr; 3496 uint_t r1, r2, rd; 3497 3498 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 3499 3500 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 3501 opc = pc; 3502 3503 instr = text[pc++]; 3504 r1 = DIF_INSTR_R1(instr); 3505 r2 = DIF_INSTR_R2(instr); 3506 rd = DIF_INSTR_RD(instr); 3507 3508 switch (DIF_INSTR_OP(instr)) { 3509 case DIF_OP_OR: 3510 regs[rd] = regs[r1] | regs[r2]; 3511 break; 3512 case DIF_OP_XOR: 3513 regs[rd] = regs[r1] ^ regs[r2]; 3514 break; 3515 case DIF_OP_AND: 3516 regs[rd] = regs[r1] & regs[r2]; 3517 break; 3518 case DIF_OP_SLL: 3519 regs[rd] = regs[r1] << regs[r2]; 3520 break; 3521 case DIF_OP_SRL: 3522 regs[rd] = regs[r1] >> regs[r2]; 3523 break; 3524 case DIF_OP_SUB: 3525 regs[rd] = regs[r1] - regs[r2]; 3526 break; 3527 case DIF_OP_ADD: 3528 regs[rd] = regs[r1] + regs[r2]; 3529 break; 3530 case DIF_OP_MUL: 3531 regs[rd] = regs[r1] * regs[r2]; 3532 break; 3533 case DIF_OP_SDIV: 3534 if (regs[r2] == 0) { 3535 regs[rd] = 0; 3536 *flags |= CPU_DTRACE_DIVZERO; 3537 } else { 3538 regs[rd] = (int64_t)regs[r1] / 3539 (int64_t)regs[r2]; 3540 } 3541 break; 3542 3543 case DIF_OP_UDIV: 3544 if (regs[r2] == 0) { 3545 regs[rd] = 0; 3546 *flags |= CPU_DTRACE_DIVZERO; 3547 } else { 3548 regs[rd] = regs[r1] / regs[r2]; 3549 } 3550 break; 3551 3552 case DIF_OP_SREM: 3553 if (regs[r2] == 0) { 3554 regs[rd] = 0; 3555 *flags |= CPU_DTRACE_DIVZERO; 3556 } else { 3557 regs[rd] = (int64_t)regs[r1] % 3558 (int64_t)regs[r2]; 3559 } 3560 break; 3561 3562 case DIF_OP_UREM: 3563 if (regs[r2] == 0) { 3564 regs[rd] = 0; 3565 *flags |= CPU_DTRACE_DIVZERO; 3566 } else { 3567 regs[rd] = regs[r1] % regs[r2]; 3568 } 3569 break; 3570 3571 case DIF_OP_NOT: 3572 regs[rd] = ~regs[r1]; 3573 break; 3574 case DIF_OP_MOV: 3575 regs[rd] = regs[r1]; 3576 break; 3577 case DIF_OP_CMP: 3578 cc_r = regs[r1] - regs[r2]; 3579 cc_n = cc_r < 0; 3580 cc_z = cc_r == 0; 3581 cc_v = 0; 3582 cc_c = regs[r1] < regs[r2]; 3583 break; 3584 case DIF_OP_TST: 3585 cc_n = cc_v = cc_c = 0; 3586 cc_z = regs[r1] == 0; 3587 break; 3588 case DIF_OP_BA: 3589 pc = DIF_INSTR_LABEL(instr); 3590 break; 3591 case DIF_OP_BE: 3592 if (cc_z) 3593 pc = DIF_INSTR_LABEL(instr); 3594 break; 3595 case DIF_OP_BNE: 3596 if (cc_z == 0) 3597 pc = DIF_INSTR_LABEL(instr); 3598 break; 3599 case DIF_OP_BG: 3600 if ((cc_z | (cc_n ^ cc_v)) == 0) 3601 pc = DIF_INSTR_LABEL(instr); 3602 break; 3603 case DIF_OP_BGU: 3604 if ((cc_c | cc_z) == 0) 3605 pc = DIF_INSTR_LABEL(instr); 3606 break; 3607 case DIF_OP_BGE: 3608 if ((cc_n ^ cc_v) == 0) 3609 pc = DIF_INSTR_LABEL(instr); 3610 break; 3611 case DIF_OP_BGEU: 3612 if (cc_c == 0) 3613 pc = DIF_INSTR_LABEL(instr); 3614 break; 3615 case DIF_OP_BL: 3616 if (cc_n ^ cc_v) 3617 pc = DIF_INSTR_LABEL(instr); 3618 break; 3619 case DIF_OP_BLU: 3620 if (cc_c) 3621 pc = DIF_INSTR_LABEL(instr); 3622 break; 3623 case DIF_OP_BLE: 3624 if (cc_z | (cc_n ^ cc_v)) 3625 pc = DIF_INSTR_LABEL(instr); 3626 break; 3627 case DIF_OP_BLEU: 3628 if (cc_c | cc_z) 3629 pc = DIF_INSTR_LABEL(instr); 3630 break; 3631 case DIF_OP_RLDSB: 3632 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 3633 *flags |= CPU_DTRACE_KPRIV; 3634 *illval = regs[r1]; 3635 break; 3636 } 3637 /*FALLTHROUGH*/ 3638 case DIF_OP_LDSB: 3639 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 3640 break; 3641 case DIF_OP_RLDSH: 3642 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 3643 *flags |= CPU_DTRACE_KPRIV; 3644 *illval = regs[r1]; 3645 break; 3646 } 3647 /*FALLTHROUGH*/ 3648 case DIF_OP_LDSH: 3649 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 3650 break; 3651 case DIF_OP_RLDSW: 3652 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 3653 *flags |= CPU_DTRACE_KPRIV; 3654 *illval = regs[r1]; 3655 break; 3656 } 3657 /*FALLTHROUGH*/ 3658 case DIF_OP_LDSW: 3659 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 3660 break; 3661 case DIF_OP_RLDUB: 3662 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 3663 *flags |= CPU_DTRACE_KPRIV; 3664 *illval = regs[r1]; 3665 break; 3666 } 3667 /*FALLTHROUGH*/ 3668 case DIF_OP_LDUB: 3669 regs[rd] = dtrace_load8(regs[r1]); 3670 break; 3671 case DIF_OP_RLDUH: 3672 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 3673 *flags |= CPU_DTRACE_KPRIV; 3674 *illval = regs[r1]; 3675 break; 3676 } 3677 /*FALLTHROUGH*/ 3678 case DIF_OP_LDUH: 3679 regs[rd] = dtrace_load16(regs[r1]); 3680 break; 3681 case DIF_OP_RLDUW: 3682 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 3683 *flags |= CPU_DTRACE_KPRIV; 3684 *illval = regs[r1]; 3685 break; 3686 } 3687 /*FALLTHROUGH*/ 3688 case DIF_OP_LDUW: 3689 regs[rd] = dtrace_load32(regs[r1]); 3690 break; 3691 case DIF_OP_RLDX: 3692 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 3693 *flags |= CPU_DTRACE_KPRIV; 3694 *illval = regs[r1]; 3695 break; 3696 } 3697 /*FALLTHROUGH*/ 3698 case DIF_OP_LDX: 3699 regs[rd] = dtrace_load64(regs[r1]); 3700 break; 3701 case DIF_OP_ULDSB: 3702 regs[rd] = (int8_t) 3703 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 3704 break; 3705 case DIF_OP_ULDSH: 3706 regs[rd] = (int16_t) 3707 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 3708 break; 3709 case DIF_OP_ULDSW: 3710 regs[rd] = (int32_t) 3711 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 3712 break; 3713 case DIF_OP_ULDUB: 3714 regs[rd] = 3715 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 3716 break; 3717 case DIF_OP_ULDUH: 3718 regs[rd] = 3719 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 3720 break; 3721 case DIF_OP_ULDUW: 3722 regs[rd] = 3723 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 3724 break; 3725 case DIF_OP_ULDX: 3726 regs[rd] = 3727 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 3728 break; 3729 case DIF_OP_RET: 3730 rval = regs[rd]; 3731 break; 3732 case DIF_OP_NOP: 3733 break; 3734 case DIF_OP_SETX: 3735 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 3736 break; 3737 case DIF_OP_SETS: 3738 regs[rd] = (uint64_t)(uintptr_t) 3739 (strtab + DIF_INSTR_STRING(instr)); 3740 break; 3741 case DIF_OP_SCMP: 3742 cc_r = dtrace_strncmp((char *)(uintptr_t)regs[r1], 3743 (char *)(uintptr_t)regs[r2], 3744 state->dts_options[DTRACEOPT_STRSIZE]); 3745 3746 cc_n = cc_r < 0; 3747 cc_z = cc_r == 0; 3748 cc_v = cc_c = 0; 3749 break; 3750 case DIF_OP_LDGA: 3751 regs[rd] = dtrace_dif_variable(mstate, state, 3752 r1, regs[r2]); 3753 break; 3754 case DIF_OP_LDGS: 3755 id = DIF_INSTR_VAR(instr); 3756 3757 if (id >= DIF_VAR_OTHER_UBASE) { 3758 uintptr_t a; 3759 3760 id -= DIF_VAR_OTHER_UBASE; 3761 svar = vstate->dtvs_globals[id]; 3762 ASSERT(svar != NULL); 3763 v = &svar->dtsv_var; 3764 3765 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 3766 regs[rd] = svar->dtsv_data; 3767 break; 3768 } 3769 3770 a = (uintptr_t)svar->dtsv_data; 3771 3772 if (*(uint8_t *)a == UINT8_MAX) { 3773 /* 3774 * If the 0th byte is set to UINT8_MAX 3775 * then this is to be treated as a 3776 * reference to a NULL variable. 3777 */ 3778 regs[rd] = NULL; 3779 } else { 3780 regs[rd] = a + sizeof (uint64_t); 3781 } 3782 3783 break; 3784 } 3785 3786 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 3787 break; 3788 3789 case DIF_OP_STGS: 3790 id = DIF_INSTR_VAR(instr); 3791 3792 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3793 id -= DIF_VAR_OTHER_UBASE; 3794 3795 svar = vstate->dtvs_globals[id]; 3796 ASSERT(svar != NULL); 3797 v = &svar->dtsv_var; 3798 3799 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3800 uintptr_t a = (uintptr_t)svar->dtsv_data; 3801 3802 ASSERT(a != NULL); 3803 ASSERT(svar->dtsv_size != 0); 3804 3805 if (regs[rd] == NULL) { 3806 *(uint8_t *)a = UINT8_MAX; 3807 break; 3808 } else { 3809 *(uint8_t *)a = 0; 3810 a += sizeof (uint64_t); 3811 } 3812 3813 dtrace_vcopy((void *)(uintptr_t)regs[rd], 3814 (void *)a, &v->dtdv_type); 3815 break; 3816 } 3817 3818 svar->dtsv_data = regs[rd]; 3819 break; 3820 3821 case DIF_OP_LDTA: 3822 /* 3823 * There are no DTrace built-in thread-local arrays at 3824 * present. This opcode is saved for future work. 3825 */ 3826 *flags |= CPU_DTRACE_ILLOP; 3827 regs[rd] = 0; 3828 break; 3829 3830 case DIF_OP_LDLS: 3831 id = DIF_INSTR_VAR(instr); 3832 3833 if (id < DIF_VAR_OTHER_UBASE) { 3834 /* 3835 * For now, this has no meaning. 3836 */ 3837 regs[rd] = 0; 3838 break; 3839 } 3840 3841 id -= DIF_VAR_OTHER_UBASE; 3842 3843 ASSERT(id < vstate->dtvs_nlocals); 3844 ASSERT(vstate->dtvs_locals != NULL); 3845 3846 svar = vstate->dtvs_locals[id]; 3847 ASSERT(svar != NULL); 3848 v = &svar->dtsv_var; 3849 3850 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3851 uintptr_t a = (uintptr_t)svar->dtsv_data; 3852 size_t sz = v->dtdv_type.dtdt_size; 3853 3854 sz += sizeof (uint64_t); 3855 ASSERT(svar->dtsv_size == NCPU * sz); 3856 a += CPU->cpu_id * sz; 3857 3858 if (*(uint8_t *)a == UINT8_MAX) { 3859 /* 3860 * If the 0th byte is set to UINT8_MAX 3861 * then this is to be treated as a 3862 * reference to a NULL variable. 3863 */ 3864 regs[rd] = NULL; 3865 } else { 3866 regs[rd] = a + sizeof (uint64_t); 3867 } 3868 3869 break; 3870 } 3871 3872 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 3873 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 3874 regs[rd] = tmp[CPU->cpu_id]; 3875 break; 3876 3877 case DIF_OP_STLS: 3878 id = DIF_INSTR_VAR(instr); 3879 3880 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3881 id -= DIF_VAR_OTHER_UBASE; 3882 ASSERT(id < vstate->dtvs_nlocals); 3883 3884 ASSERT(vstate->dtvs_locals != NULL); 3885 svar = vstate->dtvs_locals[id]; 3886 ASSERT(svar != NULL); 3887 v = &svar->dtsv_var; 3888 3889 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3890 uintptr_t a = (uintptr_t)svar->dtsv_data; 3891 size_t sz = v->dtdv_type.dtdt_size; 3892 3893 sz += sizeof (uint64_t); 3894 ASSERT(svar->dtsv_size == NCPU * sz); 3895 a += CPU->cpu_id * sz; 3896 3897 if (regs[rd] == NULL) { 3898 *(uint8_t *)a = UINT8_MAX; 3899 break; 3900 } else { 3901 *(uint8_t *)a = 0; 3902 a += sizeof (uint64_t); 3903 } 3904 3905 dtrace_vcopy((void *)(uintptr_t)regs[rd], 3906 (void *)a, &v->dtdv_type); 3907 break; 3908 } 3909 3910 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 3911 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 3912 tmp[CPU->cpu_id] = regs[rd]; 3913 break; 3914 3915 case DIF_OP_LDTS: { 3916 dtrace_dynvar_t *dvar; 3917 dtrace_key_t *key; 3918 3919 id = DIF_INSTR_VAR(instr); 3920 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3921 id -= DIF_VAR_OTHER_UBASE; 3922 v = &vstate->dtvs_tlocals[id]; 3923 3924 key = &tupregs[DIF_DTR_NREGS]; 3925 key[0].dttk_value = (uint64_t)id; 3926 key[0].dttk_size = 0; 3927 DTRACE_TLS_THRKEY(key[1].dttk_value); 3928 key[1].dttk_size = 0; 3929 3930 dvar = dtrace_dynvar(dstate, 2, key, 3931 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC); 3932 3933 if (dvar == NULL) { 3934 regs[rd] = 0; 3935 break; 3936 } 3937 3938 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3939 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 3940 } else { 3941 regs[rd] = *((uint64_t *)dvar->dtdv_data); 3942 } 3943 3944 break; 3945 } 3946 3947 case DIF_OP_STTS: { 3948 dtrace_dynvar_t *dvar; 3949 dtrace_key_t *key; 3950 3951 id = DIF_INSTR_VAR(instr); 3952 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3953 id -= DIF_VAR_OTHER_UBASE; 3954 3955 key = &tupregs[DIF_DTR_NREGS]; 3956 key[0].dttk_value = (uint64_t)id; 3957 key[0].dttk_size = 0; 3958 DTRACE_TLS_THRKEY(key[1].dttk_value); 3959 key[1].dttk_size = 0; 3960 v = &vstate->dtvs_tlocals[id]; 3961 3962 dvar = dtrace_dynvar(dstate, 2, key, 3963 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 3964 v->dtdv_type.dtdt_size : sizeof (uint64_t), 3965 regs[rd] ? DTRACE_DYNVAR_ALLOC : 3966 DTRACE_DYNVAR_DEALLOC); 3967 3968 /* 3969 * Given that we're storing to thread-local data, 3970 * we need to flush our predicate cache. 3971 */ 3972 curthread->t_predcache = NULL; 3973 3974 if (dvar == NULL) 3975 break; 3976 3977 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3978 dtrace_vcopy((void *)(uintptr_t)regs[rd], 3979 dvar->dtdv_data, &v->dtdv_type); 3980 } else { 3981 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 3982 } 3983 3984 break; 3985 } 3986 3987 case DIF_OP_SRA: 3988 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 3989 break; 3990 3991 case DIF_OP_CALL: 3992 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 3993 regs, tupregs, ttop, mstate, state); 3994 break; 3995 3996 case DIF_OP_PUSHTR: 3997 if (ttop == DIF_DTR_NREGS) { 3998 *flags |= CPU_DTRACE_TUPOFLOW; 3999 break; 4000 } 4001 4002 if (r1 == DIF_TYPE_STRING) { 4003 /* 4004 * If this is a string type and the size is 0, 4005 * we'll use the system-wide default string 4006 * size. Note that we are _not_ looking at 4007 * the value of the DTRACEOPT_STRSIZE option; 4008 * had this been set, we would expect to have 4009 * a non-zero size value in the "pushtr". 4010 */ 4011 tupregs[ttop].dttk_size = 4012 dtrace_strlen((char *)(uintptr_t)regs[rd], 4013 regs[r2] ? regs[r2] : 4014 dtrace_strsize_default) + 1; 4015 } else { 4016 tupregs[ttop].dttk_size = regs[r2]; 4017 } 4018 4019 tupregs[ttop++].dttk_value = regs[rd]; 4020 break; 4021 4022 case DIF_OP_PUSHTV: 4023 if (ttop == DIF_DTR_NREGS) { 4024 *flags |= CPU_DTRACE_TUPOFLOW; 4025 break; 4026 } 4027 4028 tupregs[ttop].dttk_value = regs[rd]; 4029 tupregs[ttop++].dttk_size = 0; 4030 break; 4031 4032 case DIF_OP_POPTS: 4033 if (ttop != 0) 4034 ttop--; 4035 break; 4036 4037 case DIF_OP_FLUSHTS: 4038 ttop = 0; 4039 break; 4040 4041 case DIF_OP_LDGAA: 4042 case DIF_OP_LDTAA: { 4043 dtrace_dynvar_t *dvar; 4044 dtrace_key_t *key = tupregs; 4045 uint_t nkeys = ttop; 4046 4047 id = DIF_INSTR_VAR(instr); 4048 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4049 id -= DIF_VAR_OTHER_UBASE; 4050 4051 key[nkeys].dttk_value = (uint64_t)id; 4052 key[nkeys++].dttk_size = 0; 4053 4054 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 4055 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 4056 key[nkeys++].dttk_size = 0; 4057 v = &vstate->dtvs_tlocals[id]; 4058 } else { 4059 v = &vstate->dtvs_globals[id]->dtsv_var; 4060 } 4061 4062 dvar = dtrace_dynvar(dstate, nkeys, key, 4063 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4064 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4065 DTRACE_DYNVAR_NOALLOC); 4066 4067 if (dvar == NULL) { 4068 regs[rd] = 0; 4069 break; 4070 } 4071 4072 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4073 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 4074 } else { 4075 regs[rd] = *((uint64_t *)dvar->dtdv_data); 4076 } 4077 4078 break; 4079 } 4080 4081 case DIF_OP_STGAA: 4082 case DIF_OP_STTAA: { 4083 dtrace_dynvar_t *dvar; 4084 dtrace_key_t *key = tupregs; 4085 uint_t nkeys = ttop; 4086 4087 id = DIF_INSTR_VAR(instr); 4088 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4089 id -= DIF_VAR_OTHER_UBASE; 4090 4091 key[nkeys].dttk_value = (uint64_t)id; 4092 key[nkeys++].dttk_size = 0; 4093 4094 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 4095 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 4096 key[nkeys++].dttk_size = 0; 4097 v = &vstate->dtvs_tlocals[id]; 4098 } else { 4099 v = &vstate->dtvs_globals[id]->dtsv_var; 4100 } 4101 4102 dvar = dtrace_dynvar(dstate, nkeys, key, 4103 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4104 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4105 regs[rd] ? DTRACE_DYNVAR_ALLOC : 4106 DTRACE_DYNVAR_DEALLOC); 4107 4108 if (dvar == NULL) 4109 break; 4110 4111 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4112 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4113 dvar->dtdv_data, &v->dtdv_type); 4114 } else { 4115 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 4116 } 4117 4118 break; 4119 } 4120 4121 case DIF_OP_ALLOCS: { 4122 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4123 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 4124 4125 if (mstate->dtms_scratch_ptr + size > 4126 mstate->dtms_scratch_base + 4127 mstate->dtms_scratch_size) { 4128 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4129 regs[rd] = NULL; 4130 } else { 4131 dtrace_bzero((void *) 4132 mstate->dtms_scratch_ptr, size); 4133 mstate->dtms_scratch_ptr += size; 4134 regs[rd] = ptr; 4135 } 4136 break; 4137 } 4138 4139 case DIF_OP_COPYS: 4140 if (!dtrace_canstore(regs[rd], regs[r2], 4141 mstate, vstate)) { 4142 *flags |= CPU_DTRACE_BADADDR; 4143 *illval = regs[rd]; 4144 break; 4145 } 4146 4147 dtrace_bcopy((void *)(uintptr_t)regs[r1], 4148 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 4149 break; 4150 4151 case DIF_OP_STB: 4152 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 4153 *flags |= CPU_DTRACE_BADADDR; 4154 *illval = regs[rd]; 4155 break; 4156 } 4157 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 4158 break; 4159 4160 case DIF_OP_STH: 4161 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 4162 *flags |= CPU_DTRACE_BADADDR; 4163 *illval = regs[rd]; 4164 break; 4165 } 4166 if (regs[rd] & 1) { 4167 *flags |= CPU_DTRACE_BADALIGN; 4168 *illval = regs[rd]; 4169 break; 4170 } 4171 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 4172 break; 4173 4174 case DIF_OP_STW: 4175 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 4176 *flags |= CPU_DTRACE_BADADDR; 4177 *illval = regs[rd]; 4178 break; 4179 } 4180 if (regs[rd] & 3) { 4181 *flags |= CPU_DTRACE_BADALIGN; 4182 *illval = regs[rd]; 4183 break; 4184 } 4185 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 4186 break; 4187 4188 case DIF_OP_STX: 4189 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 4190 *flags |= CPU_DTRACE_BADADDR; 4191 *illval = regs[rd]; 4192 break; 4193 } 4194 if (regs[rd] & 7) { 4195 *flags |= CPU_DTRACE_BADALIGN; 4196 *illval = regs[rd]; 4197 break; 4198 } 4199 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 4200 break; 4201 } 4202 } 4203 4204 if (!(*flags & CPU_DTRACE_FAULT)) 4205 return (rval); 4206 4207 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 4208 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 4209 4210 return (0); 4211 } 4212 4213 static void 4214 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 4215 { 4216 dtrace_probe_t *probe = ecb->dte_probe; 4217 dtrace_provider_t *prov = probe->dtpr_provider; 4218 char c[DTRACE_FULLNAMELEN + 80], *str; 4219 char *msg = "dtrace: breakpoint action at probe "; 4220 char *ecbmsg = " (ecb "; 4221 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 4222 uintptr_t val = (uintptr_t)ecb; 4223 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 4224 4225 if (dtrace_destructive_disallow) 4226 return; 4227 4228 /* 4229 * It's impossible to be taking action on the NULL probe. 4230 */ 4231 ASSERT(probe != NULL); 4232 4233 /* 4234 * This is a poor man's (destitute man's?) sprintf(): we want to 4235 * print the provider name, module name, function name and name of 4236 * the probe, along with the hex address of the ECB with the breakpoint 4237 * action -- all of which we must place in the character buffer by 4238 * hand. 4239 */ 4240 while (*msg != '\0') 4241 c[i++] = *msg++; 4242 4243 for (str = prov->dtpv_name; *str != '\0'; str++) 4244 c[i++] = *str; 4245 c[i++] = ':'; 4246 4247 for (str = probe->dtpr_mod; *str != '\0'; str++) 4248 c[i++] = *str; 4249 c[i++] = ':'; 4250 4251 for (str = probe->dtpr_func; *str != '\0'; str++) 4252 c[i++] = *str; 4253 c[i++] = ':'; 4254 4255 for (str = probe->dtpr_name; *str != '\0'; str++) 4256 c[i++] = *str; 4257 4258 while (*ecbmsg != '\0') 4259 c[i++] = *ecbmsg++; 4260 4261 while (shift >= 0) { 4262 mask = (uintptr_t)0xf << shift; 4263 4264 if (val >= ((uintptr_t)1 << shift)) 4265 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 4266 shift -= 4; 4267 } 4268 4269 c[i++] = ')'; 4270 c[i] = '\0'; 4271 4272 debug_enter(c); 4273 } 4274 4275 static void 4276 dtrace_action_panic(dtrace_ecb_t *ecb) 4277 { 4278 dtrace_probe_t *probe = ecb->dte_probe; 4279 4280 /* 4281 * It's impossible to be taking action on the NULL probe. 4282 */ 4283 ASSERT(probe != NULL); 4284 4285 if (dtrace_destructive_disallow) 4286 return; 4287 4288 if (dtrace_panicked != NULL) 4289 return; 4290 4291 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 4292 return; 4293 4294 /* 4295 * We won the right to panic. (We want to be sure that only one 4296 * thread calls panic() from dtrace_probe(), and that panic() is 4297 * called exactly once.) 4298 */ 4299 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 4300 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 4301 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 4302 } 4303 4304 static void 4305 dtrace_action_raise(uint64_t sig) 4306 { 4307 if (dtrace_destructive_disallow) 4308 return; 4309 4310 if (sig >= NSIG) { 4311 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4312 return; 4313 } 4314 4315 /* 4316 * raise() has a queue depth of 1 -- we ignore all subsequent 4317 * invocations of the raise() action. 4318 */ 4319 if (curthread->t_dtrace_sig == 0) 4320 curthread->t_dtrace_sig = (uint8_t)sig; 4321 4322 curthread->t_sig_check = 1; 4323 aston(curthread); 4324 } 4325 4326 static void 4327 dtrace_action_stop(void) 4328 { 4329 if (dtrace_destructive_disallow) 4330 return; 4331 4332 if (!curthread->t_dtrace_stop) { 4333 curthread->t_dtrace_stop = 1; 4334 curthread->t_sig_check = 1; 4335 aston(curthread); 4336 } 4337 } 4338 4339 static void 4340 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 4341 { 4342 hrtime_t now; 4343 volatile uint16_t *flags; 4344 cpu_t *cpu = CPU; 4345 4346 if (dtrace_destructive_disallow) 4347 return; 4348 4349 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 4350 4351 now = dtrace_gethrtime(); 4352 4353 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 4354 /* 4355 * We need to advance the mark to the current time. 4356 */ 4357 cpu->cpu_dtrace_chillmark = now; 4358 cpu->cpu_dtrace_chilled = 0; 4359 } 4360 4361 /* 4362 * Now check to see if the requested chill time would take us over 4363 * the maximum amount of time allowed in the chill interval. (Or 4364 * worse, if the calculation itself induces overflow.) 4365 */ 4366 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 4367 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 4368 *flags |= CPU_DTRACE_ILLOP; 4369 return; 4370 } 4371 4372 while (dtrace_gethrtime() - now < val) 4373 continue; 4374 4375 /* 4376 * Normally, we assure that the value of the variable "timestamp" does 4377 * not change within an ECB. The presence of chill() represents an 4378 * exception to this rule, however. 4379 */ 4380 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 4381 cpu->cpu_dtrace_chilled += val; 4382 } 4383 4384 static void 4385 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 4386 uint64_t *buf, uint64_t arg) 4387 { 4388 int nframes = DTRACE_USTACK_NFRAMES(arg); 4389 int strsize = DTRACE_USTACK_STRSIZE(arg); 4390 uint64_t *pcs = &buf[1], *fps; 4391 char *str = (char *)&pcs[nframes]; 4392 int size, offs = 0, i, j; 4393 uintptr_t old = mstate->dtms_scratch_ptr, saved; 4394 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 4395 char *sym; 4396 4397 /* 4398 * Should be taking a faster path if string space has not been 4399 * allocated. 4400 */ 4401 ASSERT(strsize != 0); 4402 4403 /* 4404 * We will first allocate some temporary space for the frame pointers. 4405 */ 4406 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4407 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 4408 (nframes * sizeof (uint64_t)); 4409 4410 if (mstate->dtms_scratch_ptr + size > 4411 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 4412 /* 4413 * Not enough room for our frame pointers -- need to indicate 4414 * that we ran out of scratch space. 4415 */ 4416 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4417 return; 4418 } 4419 4420 mstate->dtms_scratch_ptr += size; 4421 saved = mstate->dtms_scratch_ptr; 4422 4423 /* 4424 * Now get a stack with both program counters and frame pointers. 4425 */ 4426 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4427 dtrace_getufpstack(buf, fps, nframes + 1); 4428 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4429 4430 /* 4431 * If that faulted, we're cooked. 4432 */ 4433 if (*flags & CPU_DTRACE_FAULT) 4434 goto out; 4435 4436 /* 4437 * Now we want to walk up the stack, calling the USTACK helper. For 4438 * each iteration, we restore the scratch pointer. 4439 */ 4440 for (i = 0; i < nframes; i++) { 4441 mstate->dtms_scratch_ptr = saved; 4442 4443 if (offs >= strsize) 4444 break; 4445 4446 sym = (char *)(uintptr_t)dtrace_helper( 4447 DTRACE_HELPER_ACTION_USTACK, 4448 mstate, state, pcs[i], fps[i]); 4449 4450 /* 4451 * If we faulted while running the helper, we're going to 4452 * clear the fault and null out the corresponding string. 4453 */ 4454 if (*flags & CPU_DTRACE_FAULT) { 4455 *flags &= ~CPU_DTRACE_FAULT; 4456 str[offs++] = '\0'; 4457 continue; 4458 } 4459 4460 if (sym == NULL) { 4461 str[offs++] = '\0'; 4462 continue; 4463 } 4464 4465 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4466 4467 /* 4468 * Now copy in the string that the helper returned to us. 4469 */ 4470 for (j = 0; offs + j < strsize; j++) { 4471 if ((str[offs + j] = sym[j]) == '\0') 4472 break; 4473 } 4474 4475 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4476 4477 offs += j + 1; 4478 } 4479 4480 if (offs >= strsize) { 4481 /* 4482 * If we didn't have room for all of the strings, we don't 4483 * abort processing -- this needn't be a fatal error -- but we 4484 * still want to increment a counter (dts_stkstroverflows) to 4485 * allow this condition to be warned about. (If this is from 4486 * a jstack() action, it is easily tuned via jstackstrsize.) 4487 */ 4488 dtrace_error(&state->dts_stkstroverflows); 4489 } 4490 4491 while (offs < strsize) 4492 str[offs++] = '\0'; 4493 4494 out: 4495 mstate->dtms_scratch_ptr = old; 4496 } 4497 4498 /* 4499 * If you're looking for the epicenter of DTrace, you just found it. This 4500 * is the function called by the provider to fire a probe -- from which all 4501 * subsequent probe-context DTrace activity emanates. 4502 */ 4503 void 4504 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 4505 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 4506 { 4507 processorid_t cpuid; 4508 dtrace_icookie_t cookie; 4509 dtrace_probe_t *probe; 4510 dtrace_mstate_t mstate; 4511 dtrace_ecb_t *ecb; 4512 dtrace_action_t *act; 4513 intptr_t offs; 4514 size_t size; 4515 int vtime, onintr; 4516 volatile uint16_t *flags; 4517 hrtime_t now; 4518 4519 /* 4520 * Kick out immediately if this CPU is still being born (in which case 4521 * curthread will be set to -1) 4522 */ 4523 if ((uintptr_t)curthread & 1) 4524 return; 4525 4526 cookie = dtrace_interrupt_disable(); 4527 probe = dtrace_probes[id - 1]; 4528 cpuid = CPU->cpu_id; 4529 onintr = CPU_ON_INTR(CPU); 4530 4531 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 4532 probe->dtpr_predcache == curthread->t_predcache) { 4533 /* 4534 * We have hit in the predicate cache; we know that 4535 * this predicate would evaluate to be false. 4536 */ 4537 dtrace_interrupt_enable(cookie); 4538 return; 4539 } 4540 4541 if (panic_quiesce) { 4542 /* 4543 * We don't trace anything if we're panicking. 4544 */ 4545 dtrace_interrupt_enable(cookie); 4546 return; 4547 } 4548 4549 now = dtrace_gethrtime(); 4550 vtime = dtrace_vtime_references != 0; 4551 4552 if (vtime && curthread->t_dtrace_start) 4553 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 4554 4555 mstate.dtms_probe = probe; 4556 mstate.dtms_arg[0] = arg0; 4557 mstate.dtms_arg[1] = arg1; 4558 mstate.dtms_arg[2] = arg2; 4559 mstate.dtms_arg[3] = arg3; 4560 mstate.dtms_arg[4] = arg4; 4561 4562 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 4563 4564 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 4565 dtrace_predicate_t *pred = ecb->dte_predicate; 4566 dtrace_state_t *state = ecb->dte_state; 4567 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 4568 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 4569 dtrace_vstate_t *vstate = &state->dts_vstate; 4570 dtrace_provider_t *prov = probe->dtpr_provider; 4571 int committed = 0; 4572 caddr_t tomax; 4573 4574 /* 4575 * A little subtlety with the following (seemingly innocuous) 4576 * declaration of the automatic 'val': by looking at the 4577 * code, you might think that it could be declared in the 4578 * action processing loop, below. (That is, it's only used in 4579 * the action processing loop.) However, it must be declared 4580 * out of that scope because in the case of DIF expression 4581 * arguments to aggregating actions, one iteration of the 4582 * action loop will use the last iteration's value. 4583 */ 4584 #ifdef lint 4585 uint64_t val = 0; 4586 #else 4587 uint64_t val; 4588 #endif 4589 4590 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 4591 *flags &= ~CPU_DTRACE_ERROR; 4592 4593 if (prov == dtrace_provider) { 4594 /* 4595 * If dtrace itself is the provider of this probe, 4596 * we're only going to continue processing the ECB if 4597 * arg0 (the dtrace_state_t) is equal to the ECB's 4598 * creating state. (This prevents disjoint consumers 4599 * from seeing one another's metaprobes.) 4600 */ 4601 if (arg0 != (uint64_t)(uintptr_t)state) 4602 continue; 4603 } 4604 4605 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 4606 /* 4607 * We're not currently active. If our provider isn't 4608 * the dtrace pseudo provider, we're not interested. 4609 */ 4610 if (prov != dtrace_provider) 4611 continue; 4612 4613 /* 4614 * Now we must further check if we are in the BEGIN 4615 * probe. If we are, we will only continue processing 4616 * if we're still in WARMUP -- if one BEGIN enabling 4617 * has invoked the exit() action, we don't want to 4618 * evaluate subsequent BEGIN enablings. 4619 */ 4620 if (probe->dtpr_id == dtrace_probeid_begin && 4621 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 4622 ASSERT(state->dts_activity == 4623 DTRACE_ACTIVITY_DRAINING); 4624 continue; 4625 } 4626 } 4627 4628 if (ecb->dte_cond) { 4629 /* 4630 * If the dte_cond bits indicate that this 4631 * consumer is only allowed to see user-mode firings 4632 * of this probe, call the provider's dtps_usermode() 4633 * entry point to check that the probe was fired 4634 * while in a user context. Skip this ECB if that's 4635 * not the case. 4636 */ 4637 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 4638 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 4639 probe->dtpr_id, probe->dtpr_arg) == 0) 4640 continue; 4641 4642 /* 4643 * This is more subtle than it looks. We have to be 4644 * absolutely certain that CRED() isn't going to 4645 * change out from under us so it's only legit to 4646 * examine that structure if we're in constrained 4647 * situations. Currently, the only times we'll this 4648 * check is if a non-super-user has enabled the 4649 * profile or syscall providers -- providers that 4650 * allow visibility of all processes. For the 4651 * profile case, the check above will ensure that 4652 * we're examining a user context. 4653 */ 4654 if (ecb->dte_cond & DTRACE_COND_OWNER) { 4655 uid_t uid = ecb->dte_state->dts_cred.dcr_uid; 4656 gid_t gid = ecb->dte_state->dts_cred.dcr_gid; 4657 cred_t *cr; 4658 proc_t *proc; 4659 4660 if ((cr = CRED()) == NULL || 4661 uid != cr->cr_uid || 4662 uid != cr->cr_ruid || 4663 uid != cr->cr_suid || 4664 gid != cr->cr_gid || 4665 gid != cr->cr_rgid || 4666 gid != cr->cr_sgid || 4667 (proc = ttoproc(curthread)) == NULL || 4668 (proc->p_flag & SNOCD)) 4669 continue; 4670 4671 } 4672 } 4673 4674 if (now - state->dts_alive > dtrace_deadman_timeout) { 4675 /* 4676 * We seem to be dead. Unless we (a) have kernel 4677 * destructive permissions (b) have expicitly enabled 4678 * destructive actions and (c) destructive actions have 4679 * not been disabled, we're going to transition into 4680 * the KILLED state, from which no further processing 4681 * on this state will be performed. 4682 */ 4683 if (!dtrace_priv_kernel_destructive(state) || 4684 !state->dts_cred.dcr_destructive || 4685 dtrace_destructive_disallow) { 4686 void *activity = &state->dts_activity; 4687 dtrace_activity_t current; 4688 4689 do { 4690 current = state->dts_activity; 4691 } while (dtrace_cas32(activity, current, 4692 DTRACE_ACTIVITY_KILLED) != current); 4693 4694 continue; 4695 } 4696 } 4697 4698 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 4699 ecb->dte_alignment, state, &mstate)) < 0) 4700 continue; 4701 4702 tomax = buf->dtb_tomax; 4703 ASSERT(tomax != NULL); 4704 4705 if (ecb->dte_size != 0) 4706 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 4707 4708 mstate.dtms_epid = ecb->dte_epid; 4709 mstate.dtms_present |= DTRACE_MSTATE_EPID; 4710 4711 if (pred != NULL) { 4712 dtrace_difo_t *dp = pred->dtp_difo; 4713 int rval; 4714 4715 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 4716 4717 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 4718 dtrace_cacheid_t cid = probe->dtpr_predcache; 4719 4720 if (cid != DTRACE_CACHEIDNONE && !onintr) { 4721 /* 4722 * Update the predicate cache... 4723 */ 4724 ASSERT(cid == pred->dtp_cacheid); 4725 curthread->t_predcache = cid; 4726 } 4727 4728 continue; 4729 } 4730 } 4731 4732 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 4733 act != NULL; act = act->dta_next) { 4734 size_t valoffs; 4735 dtrace_difo_t *dp; 4736 dtrace_recdesc_t *rec = &act->dta_rec; 4737 4738 size = rec->dtrd_size; 4739 valoffs = offs + rec->dtrd_offset; 4740 4741 if (DTRACEACT_ISAGG(act->dta_kind)) { 4742 uint64_t v = 0xbad; 4743 dtrace_aggregation_t *agg; 4744 4745 agg = (dtrace_aggregation_t *)act; 4746 4747 if ((dp = act->dta_difo) != NULL) 4748 v = dtrace_dif_emulate(dp, 4749 &mstate, vstate, state); 4750 4751 if (*flags & CPU_DTRACE_ERROR) 4752 continue; 4753 4754 /* 4755 * Note that we always pass the expression 4756 * value from the previous iteration of the 4757 * action loop. This value will only be used 4758 * if there is an expression argument to the 4759 * aggregating action, denoted by the 4760 * dtag_hasarg field. 4761 */ 4762 dtrace_aggregate(agg, buf, 4763 offs, aggbuf, v, val); 4764 continue; 4765 } 4766 4767 switch (act->dta_kind) { 4768 case DTRACEACT_STOP: 4769 if (dtrace_priv_proc_destructive(state)) 4770 dtrace_action_stop(); 4771 continue; 4772 4773 case DTRACEACT_BREAKPOINT: 4774 if (dtrace_priv_kernel_destructive(state)) 4775 dtrace_action_breakpoint(ecb); 4776 continue; 4777 4778 case DTRACEACT_PANIC: 4779 if (dtrace_priv_kernel_destructive(state)) 4780 dtrace_action_panic(ecb); 4781 continue; 4782 4783 case DTRACEACT_STACK: 4784 if (!dtrace_priv_kernel(state)) 4785 continue; 4786 4787 dtrace_getpcstack((pc_t *)(tomax + valoffs), 4788 size / sizeof (pc_t), probe->dtpr_aframes, 4789 DTRACE_ANCHORED(probe) ? NULL : 4790 (uint32_t *)arg0); 4791 4792 continue; 4793 4794 case DTRACEACT_JSTACK: 4795 case DTRACEACT_USTACK: 4796 if (!dtrace_priv_proc(state)) 4797 continue; 4798 4799 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 4800 curproc->p_dtrace_helpers != NULL) { 4801 /* 4802 * This is the slow path -- we have 4803 * allocated string space, and we're 4804 * getting the stack of a process that 4805 * has helpers. Call into a separate 4806 * routine to perform this processing. 4807 */ 4808 dtrace_action_ustack(&mstate, state, 4809 (uint64_t *)(tomax + valoffs), 4810 rec->dtrd_arg); 4811 continue; 4812 } 4813 4814 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4815 dtrace_getupcstack((uint64_t *) 4816 (tomax + valoffs), 4817 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 4818 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4819 continue; 4820 4821 default: 4822 break; 4823 } 4824 4825 dp = act->dta_difo; 4826 ASSERT(dp != NULL); 4827 4828 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 4829 4830 if (*flags & CPU_DTRACE_ERROR) 4831 continue; 4832 4833 switch (act->dta_kind) { 4834 case DTRACEACT_SPECULATE: 4835 ASSERT(buf == &state->dts_buffer[cpuid]); 4836 buf = dtrace_speculation_buffer(state, 4837 cpuid, val); 4838 4839 if (buf == NULL) { 4840 *flags |= CPU_DTRACE_DROP; 4841 continue; 4842 } 4843 4844 offs = dtrace_buffer_reserve(buf, 4845 ecb->dte_needed, ecb->dte_alignment, 4846 state, NULL); 4847 4848 if (offs < 0) { 4849 *flags |= CPU_DTRACE_DROP; 4850 continue; 4851 } 4852 4853 tomax = buf->dtb_tomax; 4854 ASSERT(tomax != NULL); 4855 4856 if (ecb->dte_size != 0) 4857 DTRACE_STORE(uint32_t, tomax, offs, 4858 ecb->dte_epid); 4859 continue; 4860 4861 case DTRACEACT_CHILL: 4862 if (dtrace_priv_kernel_destructive(state)) 4863 dtrace_action_chill(&mstate, val); 4864 continue; 4865 4866 case DTRACEACT_RAISE: 4867 if (dtrace_priv_proc_destructive(state)) 4868 dtrace_action_raise(val); 4869 continue; 4870 4871 case DTRACEACT_COMMIT: 4872 ASSERT(!committed); 4873 4874 /* 4875 * We need to commit our buffer state. 4876 */ 4877 if (ecb->dte_size) 4878 buf->dtb_offset = offs + ecb->dte_size; 4879 buf = &state->dts_buffer[cpuid]; 4880 dtrace_speculation_commit(state, cpuid, val); 4881 committed = 1; 4882 continue; 4883 4884 case DTRACEACT_DISCARD: 4885 dtrace_speculation_discard(state, cpuid, val); 4886 continue; 4887 4888 case DTRACEACT_DIFEXPR: 4889 case DTRACEACT_LIBACT: 4890 case DTRACEACT_PRINTF: 4891 case DTRACEACT_PRINTA: 4892 case DTRACEACT_SYSTEM: 4893 case DTRACEACT_FREOPEN: 4894 break; 4895 4896 case DTRACEACT_SYM: 4897 case DTRACEACT_MOD: 4898 if (!dtrace_priv_kernel(state)) 4899 continue; 4900 break; 4901 4902 case DTRACEACT_USYM: 4903 case DTRACEACT_UMOD: 4904 case DTRACEACT_UADDR: { 4905 struct pid *pid = curthread->t_procp->p_pidp; 4906 4907 if (!dtrace_priv_proc(state)) 4908 continue; 4909 4910 DTRACE_STORE(uint64_t, tomax, 4911 valoffs, (uint64_t)pid->pid_id); 4912 DTRACE_STORE(uint64_t, tomax, 4913 valoffs + sizeof (uint64_t), val); 4914 4915 continue; 4916 } 4917 4918 case DTRACEACT_EXIT: { 4919 /* 4920 * For the exit action, we are going to attempt 4921 * to atomically set our activity to be 4922 * draining. If this fails (either because 4923 * another CPU has beat us to the exit action, 4924 * or because our current activity is something 4925 * other than ACTIVE or WARMUP), we will 4926 * continue. This assures that the exit action 4927 * can be successfully recorded at most once 4928 * when we're in the ACTIVE state. If we're 4929 * encountering the exit() action while in 4930 * COOLDOWN, however, we want to honor the new 4931 * status code. (We know that we're the only 4932 * thread in COOLDOWN, so there is no race.) 4933 */ 4934 void *activity = &state->dts_activity; 4935 dtrace_activity_t current = state->dts_activity; 4936 4937 if (current == DTRACE_ACTIVITY_COOLDOWN) 4938 break; 4939 4940 if (current != DTRACE_ACTIVITY_WARMUP) 4941 current = DTRACE_ACTIVITY_ACTIVE; 4942 4943 if (dtrace_cas32(activity, current, 4944 DTRACE_ACTIVITY_DRAINING) != current) { 4945 *flags |= CPU_DTRACE_DROP; 4946 continue; 4947 } 4948 4949 break; 4950 } 4951 4952 default: 4953 ASSERT(0); 4954 } 4955 4956 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 4957 uintptr_t end = valoffs + size; 4958 4959 /* 4960 * If this is a string, we're going to only 4961 * load until we find the zero byte -- after 4962 * which we'll store zero bytes. 4963 */ 4964 if (dp->dtdo_rtype.dtdt_kind == 4965 DIF_TYPE_STRING) { 4966 char c = '\0' + 1; 4967 size_t s; 4968 4969 for (s = 0; s < size; s++) { 4970 if (c != '\0') 4971 c = dtrace_load8(val++); 4972 4973 DTRACE_STORE(uint8_t, tomax, 4974 valoffs++, c); 4975 } 4976 4977 continue; 4978 } 4979 4980 while (valoffs < end) { 4981 DTRACE_STORE(uint8_t, tomax, valoffs++, 4982 dtrace_load8(val++)); 4983 } 4984 4985 continue; 4986 } 4987 4988 switch (size) { 4989 case 0: 4990 break; 4991 4992 case sizeof (uint8_t): 4993 DTRACE_STORE(uint8_t, tomax, valoffs, val); 4994 break; 4995 case sizeof (uint16_t): 4996 DTRACE_STORE(uint16_t, tomax, valoffs, val); 4997 break; 4998 case sizeof (uint32_t): 4999 DTRACE_STORE(uint32_t, tomax, valoffs, val); 5000 break; 5001 case sizeof (uint64_t): 5002 DTRACE_STORE(uint64_t, tomax, valoffs, val); 5003 break; 5004 default: 5005 /* 5006 * Any other size should have been returned by 5007 * reference, not by value. 5008 */ 5009 ASSERT(0); 5010 break; 5011 } 5012 } 5013 5014 if (*flags & CPU_DTRACE_DROP) 5015 continue; 5016 5017 if (*flags & CPU_DTRACE_FAULT) { 5018 int ndx; 5019 dtrace_action_t *err; 5020 5021 buf->dtb_errors++; 5022 5023 if (probe->dtpr_id == dtrace_probeid_error) { 5024 /* 5025 * There's nothing we can do -- we had an 5026 * error on the error probe. We bump an 5027 * error counter to at least indicate that 5028 * this condition happened. 5029 */ 5030 dtrace_error(&state->dts_dblerrors); 5031 continue; 5032 } 5033 5034 if (vtime) { 5035 /* 5036 * Before recursing on dtrace_probe(), we 5037 * need to explicitly clear out our start 5038 * time to prevent it from being accumulated 5039 * into t_dtrace_vtime. 5040 */ 5041 curthread->t_dtrace_start = 0; 5042 } 5043 5044 /* 5045 * Iterate over the actions to figure out which action 5046 * we were processing when we experienced the error. 5047 * Note that act points _past_ the faulting action; if 5048 * act is ecb->dte_action, the fault was in the 5049 * predicate, if it's ecb->dte_action->dta_next it's 5050 * in action #1, and so on. 5051 */ 5052 for (err = ecb->dte_action, ndx = 0; 5053 err != act; err = err->dta_next, ndx++) 5054 continue; 5055 5056 dtrace_probe_error(state, ecb->dte_epid, ndx, 5057 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 5058 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 5059 cpu_core[cpuid].cpuc_dtrace_illval); 5060 5061 continue; 5062 } 5063 5064 if (!committed) 5065 buf->dtb_offset = offs + ecb->dte_size; 5066 } 5067 5068 if (vtime) 5069 curthread->t_dtrace_start = dtrace_gethrtime(); 5070 5071 dtrace_interrupt_enable(cookie); 5072 } 5073 5074 /* 5075 * DTrace Probe Hashing Functions 5076 * 5077 * The functions in this section (and indeed, the functions in remaining 5078 * sections) are not _called_ from probe context. (Any exceptions to this are 5079 * marked with a "Note:".) Rather, they are called from elsewhere in the 5080 * DTrace framework to look-up probes in, add probes to and remove probes from 5081 * the DTrace probe hashes. (Each probe is hashed by each element of the 5082 * probe tuple -- allowing for fast lookups, regardless of what was 5083 * specified.) 5084 */ 5085 static uint_t 5086 dtrace_hash_str(char *p) 5087 { 5088 unsigned int g; 5089 uint_t hval = 0; 5090 5091 while (*p) { 5092 hval = (hval << 4) + *p++; 5093 if ((g = (hval & 0xf0000000)) != 0) 5094 hval ^= g >> 24; 5095 hval &= ~g; 5096 } 5097 return (hval); 5098 } 5099 5100 static dtrace_hash_t * 5101 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 5102 { 5103 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 5104 5105 hash->dth_stroffs = stroffs; 5106 hash->dth_nextoffs = nextoffs; 5107 hash->dth_prevoffs = prevoffs; 5108 5109 hash->dth_size = 1; 5110 hash->dth_mask = hash->dth_size - 1; 5111 5112 hash->dth_tab = kmem_zalloc(hash->dth_size * 5113 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 5114 5115 return (hash); 5116 } 5117 5118 static void 5119 dtrace_hash_destroy(dtrace_hash_t *hash) 5120 { 5121 #ifdef DEBUG 5122 int i; 5123 5124 for (i = 0; i < hash->dth_size; i++) 5125 ASSERT(hash->dth_tab[i] == NULL); 5126 #endif 5127 5128 kmem_free(hash->dth_tab, 5129 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 5130 kmem_free(hash, sizeof (dtrace_hash_t)); 5131 } 5132 5133 static void 5134 dtrace_hash_resize(dtrace_hash_t *hash) 5135 { 5136 int size = hash->dth_size, i, ndx; 5137 int new_size = hash->dth_size << 1; 5138 int new_mask = new_size - 1; 5139 dtrace_hashbucket_t **new_tab, *bucket, *next; 5140 5141 ASSERT((new_size & new_mask) == 0); 5142 5143 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 5144 5145 for (i = 0; i < size; i++) { 5146 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 5147 dtrace_probe_t *probe = bucket->dthb_chain; 5148 5149 ASSERT(probe != NULL); 5150 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 5151 5152 next = bucket->dthb_next; 5153 bucket->dthb_next = new_tab[ndx]; 5154 new_tab[ndx] = bucket; 5155 } 5156 } 5157 5158 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 5159 hash->dth_tab = new_tab; 5160 hash->dth_size = new_size; 5161 hash->dth_mask = new_mask; 5162 } 5163 5164 static void 5165 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 5166 { 5167 int hashval = DTRACE_HASHSTR(hash, new); 5168 int ndx = hashval & hash->dth_mask; 5169 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5170 dtrace_probe_t **nextp, **prevp; 5171 5172 for (; bucket != NULL; bucket = bucket->dthb_next) { 5173 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 5174 goto add; 5175 } 5176 5177 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 5178 dtrace_hash_resize(hash); 5179 dtrace_hash_add(hash, new); 5180 return; 5181 } 5182 5183 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 5184 bucket->dthb_next = hash->dth_tab[ndx]; 5185 hash->dth_tab[ndx] = bucket; 5186 hash->dth_nbuckets++; 5187 5188 add: 5189 nextp = DTRACE_HASHNEXT(hash, new); 5190 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 5191 *nextp = bucket->dthb_chain; 5192 5193 if (bucket->dthb_chain != NULL) { 5194 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 5195 ASSERT(*prevp == NULL); 5196 *prevp = new; 5197 } 5198 5199 bucket->dthb_chain = new; 5200 bucket->dthb_len++; 5201 } 5202 5203 static dtrace_probe_t * 5204 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 5205 { 5206 int hashval = DTRACE_HASHSTR(hash, template); 5207 int ndx = hashval & hash->dth_mask; 5208 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5209 5210 for (; bucket != NULL; bucket = bucket->dthb_next) { 5211 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 5212 return (bucket->dthb_chain); 5213 } 5214 5215 return (NULL); 5216 } 5217 5218 static int 5219 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 5220 { 5221 int hashval = DTRACE_HASHSTR(hash, template); 5222 int ndx = hashval & hash->dth_mask; 5223 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5224 5225 for (; bucket != NULL; bucket = bucket->dthb_next) { 5226 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 5227 return (bucket->dthb_len); 5228 } 5229 5230 return (NULL); 5231 } 5232 5233 static void 5234 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 5235 { 5236 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 5237 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5238 5239 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 5240 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 5241 5242 /* 5243 * Find the bucket that we're removing this probe from. 5244 */ 5245 for (; bucket != NULL; bucket = bucket->dthb_next) { 5246 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 5247 break; 5248 } 5249 5250 ASSERT(bucket != NULL); 5251 5252 if (*prevp == NULL) { 5253 if (*nextp == NULL) { 5254 /* 5255 * The removed probe was the only probe on this 5256 * bucket; we need to remove the bucket. 5257 */ 5258 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 5259 5260 ASSERT(bucket->dthb_chain == probe); 5261 ASSERT(b != NULL); 5262 5263 if (b == bucket) { 5264 hash->dth_tab[ndx] = bucket->dthb_next; 5265 } else { 5266 while (b->dthb_next != bucket) 5267 b = b->dthb_next; 5268 b->dthb_next = bucket->dthb_next; 5269 } 5270 5271 ASSERT(hash->dth_nbuckets > 0); 5272 hash->dth_nbuckets--; 5273 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 5274 return; 5275 } 5276 5277 bucket->dthb_chain = *nextp; 5278 } else { 5279 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 5280 } 5281 5282 if (*nextp != NULL) 5283 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 5284 } 5285 5286 /* 5287 * DTrace Utility Functions 5288 * 5289 * These are random utility functions that are _not_ called from probe context. 5290 */ 5291 static int 5292 dtrace_badattr(const dtrace_attribute_t *a) 5293 { 5294 return (a->dtat_name > DTRACE_STABILITY_MAX || 5295 a->dtat_data > DTRACE_STABILITY_MAX || 5296 a->dtat_class > DTRACE_CLASS_MAX); 5297 } 5298 5299 /* 5300 * Return a duplicate copy of a string. If the specified string is NULL, 5301 * this function returns a zero-length string. 5302 */ 5303 static char * 5304 dtrace_strdup(const char *str) 5305 { 5306 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 5307 5308 if (str != NULL) 5309 (void) strcpy(new, str); 5310 5311 return (new); 5312 } 5313 5314 #define DTRACE_ISALPHA(c) \ 5315 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 5316 5317 static int 5318 dtrace_badname(const char *s) 5319 { 5320 char c; 5321 5322 if (s == NULL || (c = *s++) == '\0') 5323 return (0); 5324 5325 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 5326 return (1); 5327 5328 while ((c = *s++) != '\0') { 5329 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 5330 c != '-' && c != '_' && c != '.' && c != '`') 5331 return (1); 5332 } 5333 5334 return (0); 5335 } 5336 5337 static void 5338 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp) 5339 { 5340 uint32_t priv; 5341 5342 *uidp = crgetuid(cr); 5343 if (PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 5344 priv = DTRACE_PRIV_ALL; 5345 } else { 5346 priv = 0; 5347 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 5348 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 5349 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 5350 priv |= DTRACE_PRIV_USER; 5351 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 5352 priv |= DTRACE_PRIV_PROC; 5353 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 5354 priv |= DTRACE_PRIV_OWNER; 5355 } 5356 5357 *privp = priv; 5358 } 5359 5360 #ifdef DTRACE_ERRDEBUG 5361 static void 5362 dtrace_errdebug(const char *str) 5363 { 5364 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; 5365 int occupied = 0; 5366 5367 mutex_enter(&dtrace_errlock); 5368 dtrace_errlast = str; 5369 dtrace_errthread = curthread; 5370 5371 while (occupied++ < DTRACE_ERRHASHSZ) { 5372 if (dtrace_errhash[hval].dter_msg == str) { 5373 dtrace_errhash[hval].dter_count++; 5374 goto out; 5375 } 5376 5377 if (dtrace_errhash[hval].dter_msg != NULL) { 5378 hval = (hval + 1) % DTRACE_ERRHASHSZ; 5379 continue; 5380 } 5381 5382 dtrace_errhash[hval].dter_msg = str; 5383 dtrace_errhash[hval].dter_count = 1; 5384 goto out; 5385 } 5386 5387 panic("dtrace: undersized error hash"); 5388 out: 5389 mutex_exit(&dtrace_errlock); 5390 } 5391 #endif 5392 5393 /* 5394 * DTrace Matching Functions 5395 * 5396 * These functions are used to match groups of probes, given some elements of 5397 * a probe tuple, or some globbed expressions for elements of a probe tuple. 5398 */ 5399 static int 5400 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid) 5401 { 5402 if (priv != DTRACE_PRIV_ALL) { 5403 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 5404 uint32_t match = priv & ppriv; 5405 5406 /* 5407 * No PRIV_DTRACE_* privileges... 5408 */ 5409 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 5410 DTRACE_PRIV_KERNEL)) == 0) 5411 return (0); 5412 5413 /* 5414 * No matching bits, but there were bits to match... 5415 */ 5416 if (match == 0 && ppriv != 0) 5417 return (0); 5418 5419 /* 5420 * Need to have permissions to the process, but don't... 5421 */ 5422 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 5423 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) 5424 return (0); 5425 } 5426 5427 return (1); 5428 } 5429 5430 /* 5431 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 5432 * consists of input pattern strings and an ops-vector to evaluate them. 5433 * This function returns >0 for match, 0 for no match, and <0 for error. 5434 */ 5435 static int 5436 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 5437 uint32_t priv, uid_t uid) 5438 { 5439 dtrace_provider_t *pvp = prp->dtpr_provider; 5440 int rv; 5441 5442 if (pvp->dtpv_defunct) 5443 return (0); 5444 5445 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 5446 return (rv); 5447 5448 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 5449 return (rv); 5450 5451 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 5452 return (rv); 5453 5454 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 5455 return (rv); 5456 5457 if (dtrace_match_priv(prp, priv, uid) == 0) 5458 return (0); 5459 5460 return (rv); 5461 } 5462 5463 /* 5464 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 5465 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 5466 * libc's version, the kernel version only applies to 8-bit ASCII strings. 5467 * In addition, all of the recursion cases except for '*' matching have been 5468 * unwound. For '*', we still implement recursive evaluation, but a depth 5469 * counter is maintained and matching is aborted if we recurse too deep. 5470 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 5471 */ 5472 static int 5473 dtrace_match_glob(const char *s, const char *p, int depth) 5474 { 5475 const char *olds; 5476 char s1, c; 5477 int gs; 5478 5479 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 5480 return (-1); 5481 5482 if (s == NULL) 5483 s = ""; /* treat NULL as empty string */ 5484 5485 top: 5486 olds = s; 5487 s1 = *s++; 5488 5489 if (p == NULL) 5490 return (0); 5491 5492 if ((c = *p++) == '\0') 5493 return (s1 == '\0'); 5494 5495 switch (c) { 5496 case '[': { 5497 int ok = 0, notflag = 0; 5498 char lc = '\0'; 5499 5500 if (s1 == '\0') 5501 return (0); 5502 5503 if (*p == '!') { 5504 notflag = 1; 5505 p++; 5506 } 5507 5508 if ((c = *p++) == '\0') 5509 return (0); 5510 5511 do { 5512 if (c == '-' && lc != '\0' && *p != ']') { 5513 if ((c = *p++) == '\0') 5514 return (0); 5515 if (c == '\\' && (c = *p++) == '\0') 5516 return (0); 5517 5518 if (notflag) { 5519 if (s1 < lc || s1 > c) 5520 ok++; 5521 else 5522 return (0); 5523 } else if (lc <= s1 && s1 <= c) 5524 ok++; 5525 5526 } else if (c == '\\' && (c = *p++) == '\0') 5527 return (0); 5528 5529 lc = c; /* save left-hand 'c' for next iteration */ 5530 5531 if (notflag) { 5532 if (s1 != c) 5533 ok++; 5534 else 5535 return (0); 5536 } else if (s1 == c) 5537 ok++; 5538 5539 if ((c = *p++) == '\0') 5540 return (0); 5541 5542 } while (c != ']'); 5543 5544 if (ok) 5545 goto top; 5546 5547 return (0); 5548 } 5549 5550 case '\\': 5551 if ((c = *p++) == '\0') 5552 return (0); 5553 /*FALLTHRU*/ 5554 5555 default: 5556 if (c != s1) 5557 return (0); 5558 /*FALLTHRU*/ 5559 5560 case '?': 5561 if (s1 != '\0') 5562 goto top; 5563 return (0); 5564 5565 case '*': 5566 while (*p == '*') 5567 p++; /* consecutive *'s are identical to a single one */ 5568 5569 if (*p == '\0') 5570 return (1); 5571 5572 for (s = olds; *s != '\0'; s++) { 5573 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 5574 return (gs); 5575 } 5576 5577 return (0); 5578 } 5579 } 5580 5581 /*ARGSUSED*/ 5582 static int 5583 dtrace_match_string(const char *s, const char *p, int depth) 5584 { 5585 return (s != NULL && strcmp(s, p) == 0); 5586 } 5587 5588 /*ARGSUSED*/ 5589 static int 5590 dtrace_match_nul(const char *s, const char *p, int depth) 5591 { 5592 return (1); /* always match the empty pattern */ 5593 } 5594 5595 /*ARGSUSED*/ 5596 static int 5597 dtrace_match_nonzero(const char *s, const char *p, int depth) 5598 { 5599 return (s != NULL && s[0] != '\0'); 5600 } 5601 5602 static int 5603 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 5604 int (*matched)(dtrace_probe_t *, void *), void *arg) 5605 { 5606 dtrace_probe_t template, *probe; 5607 dtrace_hash_t *hash = NULL; 5608 int len, best = INT_MAX, nmatched = 0; 5609 dtrace_id_t i; 5610 5611 ASSERT(MUTEX_HELD(&dtrace_lock)); 5612 5613 /* 5614 * If the probe ID is specified in the key, just lookup by ID and 5615 * invoke the match callback once if a matching probe is found. 5616 */ 5617 if (pkp->dtpk_id != DTRACE_IDNONE) { 5618 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 5619 dtrace_match_probe(probe, pkp, priv, uid) > 0) { 5620 (void) (*matched)(probe, arg); 5621 nmatched++; 5622 } 5623 return (nmatched); 5624 } 5625 5626 template.dtpr_mod = (char *)pkp->dtpk_mod; 5627 template.dtpr_func = (char *)pkp->dtpk_func; 5628 template.dtpr_name = (char *)pkp->dtpk_name; 5629 5630 /* 5631 * We want to find the most distinct of the module name, function 5632 * name, and name. So for each one that is not a glob pattern or 5633 * empty string, we perform a lookup in the corresponding hash and 5634 * use the hash table with the fewest collisions to do our search. 5635 */ 5636 if (pkp->dtpk_mmatch == &dtrace_match_string && 5637 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 5638 best = len; 5639 hash = dtrace_bymod; 5640 } 5641 5642 if (pkp->dtpk_fmatch == &dtrace_match_string && 5643 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 5644 best = len; 5645 hash = dtrace_byfunc; 5646 } 5647 5648 if (pkp->dtpk_nmatch == &dtrace_match_string && 5649 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 5650 best = len; 5651 hash = dtrace_byname; 5652 } 5653 5654 /* 5655 * If we did not select a hash table, iterate over every probe and 5656 * invoke our callback for each one that matches our input probe key. 5657 */ 5658 if (hash == NULL) { 5659 for (i = 0; i < dtrace_nprobes; i++) { 5660 if ((probe = dtrace_probes[i]) == NULL || 5661 dtrace_match_probe(probe, pkp, priv, uid) <= 0) 5662 continue; 5663 5664 nmatched++; 5665 5666 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 5667 break; 5668 } 5669 5670 return (nmatched); 5671 } 5672 5673 /* 5674 * If we selected a hash table, iterate over each probe of the same key 5675 * name and invoke the callback for every probe that matches the other 5676 * attributes of our input probe key. 5677 */ 5678 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 5679 probe = *(DTRACE_HASHNEXT(hash, probe))) { 5680 5681 if (dtrace_match_probe(probe, pkp, priv, uid) <= 0) 5682 continue; 5683 5684 nmatched++; 5685 5686 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 5687 break; 5688 } 5689 5690 return (nmatched); 5691 } 5692 5693 /* 5694 * Return the function pointer dtrace_probecmp() should use to compare the 5695 * specified pattern with a string. For NULL or empty patterns, we select 5696 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 5697 * For non-empty non-glob strings, we use dtrace_match_string(). 5698 */ 5699 static dtrace_probekey_f * 5700 dtrace_probekey_func(const char *p) 5701 { 5702 char c; 5703 5704 if (p == NULL || *p == '\0') 5705 return (&dtrace_match_nul); 5706 5707 while ((c = *p++) != '\0') { 5708 if (c == '[' || c == '?' || c == '*' || c == '\\') 5709 return (&dtrace_match_glob); 5710 } 5711 5712 return (&dtrace_match_string); 5713 } 5714 5715 /* 5716 * Build a probe comparison key for use with dtrace_match_probe() from the 5717 * given probe description. By convention, a null key only matches anchored 5718 * probes: if each field is the empty string, reset dtpk_fmatch to 5719 * dtrace_match_nonzero(). 5720 */ 5721 static void 5722 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 5723 { 5724 pkp->dtpk_prov = pdp->dtpd_provider; 5725 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 5726 5727 pkp->dtpk_mod = pdp->dtpd_mod; 5728 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 5729 5730 pkp->dtpk_func = pdp->dtpd_func; 5731 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 5732 5733 pkp->dtpk_name = pdp->dtpd_name; 5734 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 5735 5736 pkp->dtpk_id = pdp->dtpd_id; 5737 5738 if (pkp->dtpk_id == DTRACE_IDNONE && 5739 pkp->dtpk_pmatch == &dtrace_match_nul && 5740 pkp->dtpk_mmatch == &dtrace_match_nul && 5741 pkp->dtpk_fmatch == &dtrace_match_nul && 5742 pkp->dtpk_nmatch == &dtrace_match_nul) 5743 pkp->dtpk_fmatch = &dtrace_match_nonzero; 5744 } 5745 5746 /* 5747 * DTrace Provider-to-Framework API Functions 5748 * 5749 * These functions implement much of the Provider-to-Framework API, as 5750 * described in <sys/dtrace.h>. The parts of the API not in this section are 5751 * the functions in the API for probe management (found below), and 5752 * dtrace_probe() itself (found above). 5753 */ 5754 5755 /* 5756 * Register the calling provider with the DTrace framework. This should 5757 * generally be called by DTrace providers in their attach(9E) entry point. 5758 */ 5759 int 5760 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 5761 uid_t uid, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 5762 { 5763 dtrace_provider_t *provider; 5764 5765 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 5766 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5767 "arguments", name ? name : "<NULL>"); 5768 return (EINVAL); 5769 } 5770 5771 if (name[0] == '\0' || dtrace_badname(name)) { 5772 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5773 "provider name", name); 5774 return (EINVAL); 5775 } 5776 5777 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 5778 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 5779 pops->dtps_destroy == NULL || 5780 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 5781 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5782 "provider ops", name); 5783 return (EINVAL); 5784 } 5785 5786 if (dtrace_badattr(&pap->dtpa_provider) || 5787 dtrace_badattr(&pap->dtpa_mod) || 5788 dtrace_badattr(&pap->dtpa_func) || 5789 dtrace_badattr(&pap->dtpa_name) || 5790 dtrace_badattr(&pap->dtpa_args)) { 5791 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5792 "provider attributes", name); 5793 return (EINVAL); 5794 } 5795 5796 if (priv & ~DTRACE_PRIV_ALL) { 5797 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5798 "privilege attributes", name); 5799 return (EINVAL); 5800 } 5801 5802 if ((priv & DTRACE_PRIV_KERNEL) && 5803 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 5804 pops->dtps_usermode == NULL) { 5805 cmn_err(CE_WARN, "failed to register provider '%s': need " 5806 "dtps_usermode() op for given privilege attributes", name); 5807 return (EINVAL); 5808 } 5809 5810 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 5811 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 5812 (void) strcpy(provider->dtpv_name, name); 5813 5814 provider->dtpv_attr = *pap; 5815 provider->dtpv_priv.dtpp_flags = priv; 5816 provider->dtpv_priv.dtpp_uid = uid; 5817 provider->dtpv_pops = *pops; 5818 5819 if (pops->dtps_provide == NULL) { 5820 ASSERT(pops->dtps_provide_module != NULL); 5821 provider->dtpv_pops.dtps_provide = 5822 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 5823 } 5824 5825 if (pops->dtps_provide_module == NULL) { 5826 ASSERT(pops->dtps_provide != NULL); 5827 provider->dtpv_pops.dtps_provide_module = 5828 (void (*)(void *, struct modctl *))dtrace_nullop; 5829 } 5830 5831 if (pops->dtps_suspend == NULL) { 5832 ASSERT(pops->dtps_resume == NULL); 5833 provider->dtpv_pops.dtps_suspend = 5834 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 5835 provider->dtpv_pops.dtps_resume = 5836 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 5837 } 5838 5839 provider->dtpv_arg = arg; 5840 *idp = (dtrace_provider_id_t)provider; 5841 5842 if (pops == &dtrace_provider_ops) { 5843 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 5844 ASSERT(MUTEX_HELD(&dtrace_lock)); 5845 ASSERT(dtrace_anon.dta_enabling == NULL); 5846 5847 /* 5848 * We make sure that the DTrace provider is at the head of 5849 * the provider chain. 5850 */ 5851 provider->dtpv_next = dtrace_provider; 5852 dtrace_provider = provider; 5853 return (0); 5854 } 5855 5856 mutex_enter(&dtrace_provider_lock); 5857 mutex_enter(&dtrace_lock); 5858 5859 /* 5860 * If there is at least one provider registered, we'll add this 5861 * provider after the first provider. 5862 */ 5863 if (dtrace_provider != NULL) { 5864 provider->dtpv_next = dtrace_provider->dtpv_next; 5865 dtrace_provider->dtpv_next = provider; 5866 } else { 5867 dtrace_provider = provider; 5868 } 5869 5870 if (dtrace_retained != NULL) { 5871 dtrace_enabling_provide(provider); 5872 5873 /* 5874 * Now we need to call dtrace_enabling_matchall() -- which 5875 * will acquire cpu_lock and dtrace_lock. We therefore need 5876 * to drop all of our locks before calling into it... 5877 */ 5878 mutex_exit(&dtrace_lock); 5879 mutex_exit(&dtrace_provider_lock); 5880 dtrace_enabling_matchall(); 5881 5882 return (0); 5883 } 5884 5885 mutex_exit(&dtrace_lock); 5886 mutex_exit(&dtrace_provider_lock); 5887 5888 return (0); 5889 } 5890 5891 /* 5892 * Unregister the specified provider from the DTrace framework. This should 5893 * generally be called by DTrace providers in their detach(9E) entry point. 5894 */ 5895 int 5896 dtrace_unregister(dtrace_provider_id_t id) 5897 { 5898 dtrace_provider_t *old = (dtrace_provider_t *)id; 5899 dtrace_provider_t *prev = NULL; 5900 int i, self = 0; 5901 dtrace_probe_t *probe, *first = NULL; 5902 5903 if (old->dtpv_pops.dtps_enable == 5904 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 5905 /* 5906 * If DTrace itself is the provider, we're called with locks 5907 * already held. 5908 */ 5909 ASSERT(old == dtrace_provider); 5910 ASSERT(dtrace_devi != NULL); 5911 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 5912 ASSERT(MUTEX_HELD(&dtrace_lock)); 5913 self = 1; 5914 5915 if (dtrace_provider->dtpv_next != NULL) { 5916 /* 5917 * There's another provider here; return failure. 5918 */ 5919 return (EBUSY); 5920 } 5921 } else { 5922 mutex_enter(&dtrace_provider_lock); 5923 mutex_enter(&mod_lock); 5924 mutex_enter(&dtrace_lock); 5925 } 5926 5927 /* 5928 * If anyone has /dev/dtrace open, or if there are anonymous enabled 5929 * probes, we refuse to let providers slither away, unless this 5930 * provider has already been explicitly invalidated. 5931 */ 5932 if (!old->dtpv_defunct && 5933 (dtrace_opens || (dtrace_anon.dta_state != NULL && 5934 dtrace_anon.dta_state->dts_necbs > 0))) { 5935 if (!self) { 5936 mutex_exit(&dtrace_lock); 5937 mutex_exit(&mod_lock); 5938 mutex_exit(&dtrace_provider_lock); 5939 } 5940 return (EBUSY); 5941 } 5942 5943 /* 5944 * Attempt to destroy the probes associated with this provider. 5945 */ 5946 for (i = 0; i < dtrace_nprobes; i++) { 5947 if ((probe = dtrace_probes[i]) == NULL) 5948 continue; 5949 5950 if (probe->dtpr_provider != old) 5951 continue; 5952 5953 if (probe->dtpr_ecb == NULL) 5954 continue; 5955 5956 /* 5957 * We have at least one ECB; we can't remove this provider. 5958 */ 5959 if (!self) { 5960 mutex_exit(&dtrace_lock); 5961 mutex_exit(&mod_lock); 5962 mutex_exit(&dtrace_provider_lock); 5963 } 5964 return (EBUSY); 5965 } 5966 5967 /* 5968 * All of the probes for this provider are disabled; we can safely 5969 * remove all of them from their hash chains and from the probe array. 5970 */ 5971 for (i = 0; i < dtrace_nprobes; i++) { 5972 if ((probe = dtrace_probes[i]) == NULL) 5973 continue; 5974 5975 if (probe->dtpr_provider != old) 5976 continue; 5977 5978 dtrace_probes[i] = NULL; 5979 5980 dtrace_hash_remove(dtrace_bymod, probe); 5981 dtrace_hash_remove(dtrace_byfunc, probe); 5982 dtrace_hash_remove(dtrace_byname, probe); 5983 5984 if (first == NULL) { 5985 first = probe; 5986 probe->dtpr_nextmod = NULL; 5987 } else { 5988 probe->dtpr_nextmod = first; 5989 first = probe; 5990 } 5991 } 5992 5993 /* 5994 * The provider's probes have been removed from the hash chains and 5995 * from the probe array. Now issue a dtrace_sync() to be sure that 5996 * everyone has cleared out from any probe array processing. 5997 */ 5998 dtrace_sync(); 5999 6000 for (probe = first; probe != NULL; probe = first) { 6001 first = probe->dtpr_nextmod; 6002 6003 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 6004 probe->dtpr_arg); 6005 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 6006 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 6007 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 6008 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 6009 kmem_free(probe, sizeof (dtrace_probe_t)); 6010 } 6011 6012 if ((prev = dtrace_provider) == old) { 6013 ASSERT(self || dtrace_devi == NULL); 6014 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 6015 dtrace_provider = old->dtpv_next; 6016 } else { 6017 while (prev != NULL && prev->dtpv_next != old) 6018 prev = prev->dtpv_next; 6019 6020 if (prev == NULL) { 6021 panic("attempt to unregister non-existent " 6022 "dtrace provider %p\n", (void *)id); 6023 } 6024 6025 prev->dtpv_next = old->dtpv_next; 6026 } 6027 6028 if (!self) { 6029 mutex_exit(&dtrace_lock); 6030 mutex_exit(&mod_lock); 6031 mutex_exit(&dtrace_provider_lock); 6032 } 6033 6034 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 6035 kmem_free(old, sizeof (dtrace_provider_t)); 6036 6037 return (0); 6038 } 6039 6040 /* 6041 * Invalidate the specified provider. All subsequent probe lookups for the 6042 * specified provider will fail, but its probes will not be removed. 6043 */ 6044 void 6045 dtrace_invalidate(dtrace_provider_id_t id) 6046 { 6047 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 6048 6049 ASSERT(pvp->dtpv_pops.dtps_enable != 6050 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 6051 6052 mutex_enter(&dtrace_provider_lock); 6053 mutex_enter(&dtrace_lock); 6054 6055 pvp->dtpv_defunct = 1; 6056 6057 mutex_exit(&dtrace_lock); 6058 mutex_exit(&dtrace_provider_lock); 6059 } 6060 6061 /* 6062 * Indicate whether or not DTrace has attached. 6063 */ 6064 int 6065 dtrace_attached(void) 6066 { 6067 /* 6068 * dtrace_provider will be non-NULL iff the DTrace driver has 6069 * attached. (It's non-NULL because DTrace is always itself a 6070 * provider.) 6071 */ 6072 return (dtrace_provider != NULL); 6073 } 6074 6075 /* 6076 * Remove all the unenabled probes for the given provider. This function is 6077 * not unlike dtrace_unregister(), except that it doesn't remove the provider 6078 * -- just as many of its associated probes as it can. 6079 */ 6080 int 6081 dtrace_condense(dtrace_provider_id_t id) 6082 { 6083 dtrace_provider_t *prov = (dtrace_provider_t *)id; 6084 int i; 6085 dtrace_probe_t *probe; 6086 6087 /* 6088 * Make sure this isn't the dtrace provider itself. 6089 */ 6090 ASSERT(prov->dtpv_pops.dtps_enable != 6091 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 6092 6093 mutex_enter(&dtrace_provider_lock); 6094 mutex_enter(&dtrace_lock); 6095 6096 /* 6097 * Attempt to destroy the probes associated with this provider. 6098 */ 6099 for (i = 0; i < dtrace_nprobes; i++) { 6100 if ((probe = dtrace_probes[i]) == NULL) 6101 continue; 6102 6103 if (probe->dtpr_provider != prov) 6104 continue; 6105 6106 if (probe->dtpr_ecb != NULL) 6107 continue; 6108 6109 dtrace_probes[i] = NULL; 6110 6111 dtrace_hash_remove(dtrace_bymod, probe); 6112 dtrace_hash_remove(dtrace_byfunc, probe); 6113 dtrace_hash_remove(dtrace_byname, probe); 6114 6115 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 6116 probe->dtpr_arg); 6117 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 6118 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 6119 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 6120 kmem_free(probe, sizeof (dtrace_probe_t)); 6121 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 6122 } 6123 6124 mutex_exit(&dtrace_lock); 6125 mutex_exit(&dtrace_provider_lock); 6126 6127 return (0); 6128 } 6129 6130 /* 6131 * DTrace Probe Management Functions 6132 * 6133 * The functions in this section perform the DTrace probe management, 6134 * including functions to create probes, look-up probes, and call into the 6135 * providers to request that probes be provided. Some of these functions are 6136 * in the Provider-to-Framework API; these functions can be identified by the 6137 * fact that they are not declared "static". 6138 */ 6139 6140 /* 6141 * Create a probe with the specified module name, function name, and name. 6142 */ 6143 dtrace_id_t 6144 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 6145 const char *func, const char *name, int aframes, void *arg) 6146 { 6147 dtrace_probe_t *probe, **probes; 6148 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 6149 dtrace_id_t id; 6150 6151 if (provider == dtrace_provider) { 6152 ASSERT(MUTEX_HELD(&dtrace_lock)); 6153 } else { 6154 mutex_enter(&dtrace_lock); 6155 } 6156 6157 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 6158 VM_BESTFIT | VM_SLEEP); 6159 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 6160 6161 probe->dtpr_id = id; 6162 probe->dtpr_gen = dtrace_probegen++; 6163 probe->dtpr_mod = dtrace_strdup(mod); 6164 probe->dtpr_func = dtrace_strdup(func); 6165 probe->dtpr_name = dtrace_strdup(name); 6166 probe->dtpr_arg = arg; 6167 probe->dtpr_aframes = aframes; 6168 probe->dtpr_provider = provider; 6169 6170 dtrace_hash_add(dtrace_bymod, probe); 6171 dtrace_hash_add(dtrace_byfunc, probe); 6172 dtrace_hash_add(dtrace_byname, probe); 6173 6174 if (id - 1 >= dtrace_nprobes) { 6175 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 6176 size_t nsize = osize << 1; 6177 6178 if (nsize == 0) { 6179 ASSERT(osize == 0); 6180 ASSERT(dtrace_probes == NULL); 6181 nsize = sizeof (dtrace_probe_t *); 6182 } 6183 6184 probes = kmem_zalloc(nsize, KM_SLEEP); 6185 6186 if (dtrace_probes == NULL) { 6187 ASSERT(osize == 0); 6188 dtrace_probes = probes; 6189 dtrace_nprobes = 1; 6190 } else { 6191 dtrace_probe_t **oprobes = dtrace_probes; 6192 6193 bcopy(oprobes, probes, osize); 6194 dtrace_membar_producer(); 6195 dtrace_probes = probes; 6196 6197 dtrace_sync(); 6198 6199 /* 6200 * All CPUs are now seeing the new probes array; we can 6201 * safely free the old array. 6202 */ 6203 kmem_free(oprobes, osize); 6204 dtrace_nprobes <<= 1; 6205 } 6206 6207 ASSERT(id - 1 < dtrace_nprobes); 6208 } 6209 6210 ASSERT(dtrace_probes[id - 1] == NULL); 6211 dtrace_probes[id - 1] = probe; 6212 6213 if (provider != dtrace_provider) 6214 mutex_exit(&dtrace_lock); 6215 6216 return (id); 6217 } 6218 6219 static dtrace_probe_t * 6220 dtrace_probe_lookup_id(dtrace_id_t id) 6221 { 6222 ASSERT(MUTEX_HELD(&dtrace_lock)); 6223 6224 if (id == 0 || id > dtrace_nprobes) 6225 return (NULL); 6226 6227 return (dtrace_probes[id - 1]); 6228 } 6229 6230 static int 6231 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 6232 { 6233 *((dtrace_id_t *)arg) = probe->dtpr_id; 6234 6235 return (DTRACE_MATCH_DONE); 6236 } 6237 6238 /* 6239 * Look up a probe based on provider and one or more of module name, function 6240 * name and probe name. 6241 */ 6242 dtrace_id_t 6243 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, 6244 const char *func, const char *name) 6245 { 6246 dtrace_probekey_t pkey; 6247 dtrace_id_t id; 6248 int match; 6249 6250 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 6251 pkey.dtpk_pmatch = &dtrace_match_string; 6252 pkey.dtpk_mod = mod; 6253 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 6254 pkey.dtpk_func = func; 6255 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 6256 pkey.dtpk_name = name; 6257 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 6258 pkey.dtpk_id = DTRACE_IDNONE; 6259 6260 mutex_enter(&dtrace_lock); 6261 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 6262 dtrace_probe_lookup_match, &id); 6263 mutex_exit(&dtrace_lock); 6264 6265 ASSERT(match == 1 || match == 0); 6266 return (match ? id : 0); 6267 } 6268 6269 /* 6270 * Returns the probe argument associated with the specified probe. 6271 */ 6272 void * 6273 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 6274 { 6275 dtrace_probe_t *probe; 6276 void *rval = NULL; 6277 6278 mutex_enter(&dtrace_lock); 6279 6280 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 6281 probe->dtpr_provider == (dtrace_provider_t *)id) 6282 rval = probe->dtpr_arg; 6283 6284 mutex_exit(&dtrace_lock); 6285 6286 return (rval); 6287 } 6288 6289 /* 6290 * Copy a probe into a probe description. 6291 */ 6292 static void 6293 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 6294 { 6295 bzero(pdp, sizeof (dtrace_probedesc_t)); 6296 pdp->dtpd_id = prp->dtpr_id; 6297 6298 (void) strncpy(pdp->dtpd_provider, 6299 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 6300 6301 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 6302 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 6303 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 6304 } 6305 6306 /* 6307 * Called to indicate that a probe -- or probes -- should be provided by a 6308 * specfied provider. If the specified description is NULL, the provider will 6309 * be told to provide all of its probes. (This is done whenever a new 6310 * consumer comes along, or whenever a retained enabling is to be matched.) If 6311 * the specified description is non-NULL, the provider is given the 6312 * opportunity to dynamically provide the specified probe, allowing providers 6313 * to support the creation of probes on-the-fly. (So-called _autocreated_ 6314 * probes.) If the provider is NULL, the operations will be applied to all 6315 * providers; if the provider is non-NULL the operations will only be applied 6316 * to the specified provider. The dtrace_provider_lock must be held, and the 6317 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 6318 * will need to grab the dtrace_lock when it reenters the framework through 6319 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 6320 */ 6321 static void 6322 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 6323 { 6324 struct modctl *ctl; 6325 int all = 0; 6326 6327 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6328 6329 if (prv == NULL) { 6330 all = 1; 6331 prv = dtrace_provider; 6332 } 6333 6334 do { 6335 /* 6336 * First, call the blanket provide operation. 6337 */ 6338 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 6339 6340 /* 6341 * Now call the per-module provide operation. We will grab 6342 * mod_lock to prevent the list from being modified. Note 6343 * that this also prevents the mod_busy bits from changing. 6344 * (mod_busy can only be changed with mod_lock held.) 6345 */ 6346 mutex_enter(&mod_lock); 6347 6348 ctl = &modules; 6349 do { 6350 if (ctl->mod_busy || ctl->mod_mp == NULL) 6351 continue; 6352 6353 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 6354 6355 } while ((ctl = ctl->mod_next) != &modules); 6356 6357 mutex_exit(&mod_lock); 6358 } while (all && (prv = prv->dtpv_next) != NULL); 6359 } 6360 6361 /* 6362 * Iterate over each probe, and call the Framework-to-Provider API function 6363 * denoted by offs. 6364 */ 6365 static void 6366 dtrace_probe_foreach(uintptr_t offs) 6367 { 6368 dtrace_provider_t *prov; 6369 void (*func)(void *, dtrace_id_t, void *); 6370 dtrace_probe_t *probe; 6371 dtrace_icookie_t cookie; 6372 int i; 6373 6374 /* 6375 * We disable interrupts to walk through the probe array. This is 6376 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 6377 * won't see stale data. 6378 */ 6379 cookie = dtrace_interrupt_disable(); 6380 6381 for (i = 0; i < dtrace_nprobes; i++) { 6382 if ((probe = dtrace_probes[i]) == NULL) 6383 continue; 6384 6385 if (probe->dtpr_ecb == NULL) { 6386 /* 6387 * This probe isn't enabled -- don't call the function. 6388 */ 6389 continue; 6390 } 6391 6392 prov = probe->dtpr_provider; 6393 func = *((void(**)(void *, dtrace_id_t, void *)) 6394 ((uintptr_t)&prov->dtpv_pops + offs)); 6395 6396 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 6397 } 6398 6399 dtrace_interrupt_enable(cookie); 6400 } 6401 6402 static int 6403 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 6404 { 6405 dtrace_probekey_t pkey; 6406 uint32_t priv; 6407 uid_t uid; 6408 6409 ASSERT(MUTEX_HELD(&dtrace_lock)); 6410 dtrace_ecb_create_cache = NULL; 6411 6412 if (desc == NULL) { 6413 /* 6414 * If we're passed a NULL description, we're being asked to 6415 * create an ECB with a NULL probe. 6416 */ 6417 (void) dtrace_ecb_create_enable(NULL, enab); 6418 return (0); 6419 } 6420 6421 dtrace_probekey(desc, &pkey); 6422 dtrace_cred2priv(CRED(), &priv, &uid); 6423 6424 return (dtrace_match(&pkey, priv, uid, dtrace_ecb_create_enable, enab)); 6425 } 6426 6427 /* 6428 * DTrace Helper Provider Functions 6429 */ 6430 static void 6431 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 6432 { 6433 attr->dtat_name = DOF_ATTR_NAME(dofattr); 6434 attr->dtat_data = DOF_ATTR_DATA(dofattr); 6435 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 6436 } 6437 6438 static void 6439 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 6440 const dof_provider_t *dofprov, char *strtab) 6441 { 6442 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 6443 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 6444 dofprov->dofpv_provattr); 6445 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 6446 dofprov->dofpv_modattr); 6447 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 6448 dofprov->dofpv_funcattr); 6449 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 6450 dofprov->dofpv_nameattr); 6451 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 6452 dofprov->dofpv_argsattr); 6453 } 6454 6455 static void 6456 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 6457 { 6458 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6459 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6460 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec; 6461 dof_provider_t *provider; 6462 dof_probe_t *probe; 6463 uint32_t *off; 6464 uint8_t *arg; 6465 char *strtab; 6466 uint_t i, nprobes; 6467 dtrace_helper_provdesc_t dhpv; 6468 dtrace_helper_probedesc_t dhpb; 6469 dtrace_meta_t *meta = dtrace_meta_pid; 6470 dtrace_mops_t *mops = &meta->dtm_mops; 6471 void *parg; 6472 6473 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 6474 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6475 provider->dofpv_strtab * dof->dofh_secsize); 6476 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6477 provider->dofpv_probes * dof->dofh_secsize); 6478 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6479 provider->dofpv_prargs * dof->dofh_secsize); 6480 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6481 provider->dofpv_proffs * dof->dofh_secsize); 6482 6483 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 6484 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 6485 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 6486 6487 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 6488 6489 /* 6490 * Create the provider. 6491 */ 6492 dtrace_dofprov2hprov(&dhpv, provider, strtab); 6493 6494 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 6495 return; 6496 6497 meta->dtm_count++; 6498 6499 /* 6500 * Create the probes. 6501 */ 6502 for (i = 0; i < nprobes; i++) { 6503 probe = (dof_probe_t *)(uintptr_t)(daddr + 6504 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 6505 6506 dhpb.dthpb_mod = dhp->dofhp_mod; 6507 dhpb.dthpb_func = strtab + probe->dofpr_func; 6508 dhpb.dthpb_name = strtab + probe->dofpr_name; 6509 dhpb.dthpb_base = probe->dofpr_addr; 6510 dhpb.dthpb_offs = off + probe->dofpr_offidx; 6511 dhpb.dthpb_noffs = probe->dofpr_noffs; 6512 dhpb.dthpb_args = arg + probe->dofpr_argidx; 6513 dhpb.dthpb_nargc = probe->dofpr_nargc; 6514 dhpb.dthpb_xargc = probe->dofpr_xargc; 6515 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 6516 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 6517 6518 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 6519 } 6520 } 6521 6522 static void 6523 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 6524 { 6525 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6526 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6527 int i; 6528 6529 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 6530 6531 for (i = 0; i < dof->dofh_secnum; i++) { 6532 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 6533 dof->dofh_secoff + i * dof->dofh_secsize); 6534 6535 if (sec->dofs_type != DOF_SECT_PROVIDER) 6536 continue; 6537 6538 dtrace_helper_provide_one(dhp, sec, pid); 6539 } 6540 6541 /* 6542 * We may have just created probes, so we must now rematch against 6543 * any retained enablings. Note that this call will acquire both 6544 * cpu_lock and dtrace_lock; the fact that we are holding 6545 * dtrace_meta_lock now is what defines the ordering with respect to 6546 * these three locks. 6547 */ 6548 dtrace_enabling_matchall(); 6549 } 6550 6551 static void 6552 dtrace_helper_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 6553 { 6554 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6555 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6556 dof_sec_t *str_sec; 6557 dof_provider_t *provider; 6558 char *strtab; 6559 dtrace_helper_provdesc_t dhpv; 6560 dtrace_meta_t *meta = dtrace_meta_pid; 6561 dtrace_mops_t *mops = &meta->dtm_mops; 6562 6563 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 6564 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6565 provider->dofpv_strtab * dof->dofh_secsize); 6566 6567 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 6568 6569 /* 6570 * Create the provider. 6571 */ 6572 dtrace_dofprov2hprov(&dhpv, provider, strtab); 6573 6574 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 6575 6576 meta->dtm_count--; 6577 } 6578 6579 static void 6580 dtrace_helper_remove(dof_helper_t *dhp, pid_t pid) 6581 { 6582 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6583 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6584 int i; 6585 6586 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 6587 6588 for (i = 0; i < dof->dofh_secnum; i++) { 6589 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 6590 dof->dofh_secoff + i * dof->dofh_secsize); 6591 6592 if (sec->dofs_type != DOF_SECT_PROVIDER) 6593 continue; 6594 6595 dtrace_helper_remove_one(dhp, sec, pid); 6596 } 6597 } 6598 6599 /* 6600 * DTrace Meta Provider-to-Framework API Functions 6601 * 6602 * These functions implement the Meta Provider-to-Framework API, as described 6603 * in <sys/dtrace.h>. 6604 */ 6605 int 6606 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 6607 dtrace_meta_provider_id_t *idp) 6608 { 6609 dtrace_meta_t *meta; 6610 dtrace_helpers_t *help, *next; 6611 int i; 6612 6613 *idp = DTRACE_METAPROVNONE; 6614 6615 /* 6616 * We strictly don't need the name, but we hold onto it for 6617 * debuggability. All hail error queues! 6618 */ 6619 if (name == NULL) { 6620 cmn_err(CE_WARN, "failed to register meta-provider: " 6621 "invalid name"); 6622 return (EINVAL); 6623 } 6624 6625 if (mops == NULL || 6626 mops->dtms_create_probe == NULL || 6627 mops->dtms_provide_pid == NULL || 6628 mops->dtms_remove_pid == NULL) { 6629 cmn_err(CE_WARN, "failed to register meta-register %s: " 6630 "invalid ops", name); 6631 return (EINVAL); 6632 } 6633 6634 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 6635 meta->dtm_mops = *mops; 6636 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 6637 (void) strcpy(meta->dtm_name, name); 6638 meta->dtm_arg = arg; 6639 6640 mutex_enter(&dtrace_meta_lock); 6641 mutex_enter(&dtrace_lock); 6642 6643 if (dtrace_meta_pid != NULL) { 6644 mutex_exit(&dtrace_lock); 6645 mutex_exit(&dtrace_meta_lock); 6646 cmn_err(CE_WARN, "failed to register meta-register %s: " 6647 "user-land meta-provider exists", name); 6648 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 6649 kmem_free(meta, sizeof (dtrace_meta_t)); 6650 return (EINVAL); 6651 } 6652 6653 dtrace_meta_pid = meta; 6654 *idp = (dtrace_meta_provider_id_t)meta; 6655 6656 /* 6657 * If there are providers and probes ready to go, pass them 6658 * off to the new meta provider now. 6659 */ 6660 6661 help = dtrace_deferred_pid; 6662 dtrace_deferred_pid = NULL; 6663 6664 mutex_exit(&dtrace_lock); 6665 6666 while (help != NULL) { 6667 for (i = 0; i < help->dthps_nprovs; i++) { 6668 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 6669 help->dthps_pid); 6670 } 6671 6672 next = help->dthps_next; 6673 help->dthps_next = NULL; 6674 help->dthps_prev = NULL; 6675 help = next; 6676 } 6677 6678 mutex_exit(&dtrace_meta_lock); 6679 6680 return (0); 6681 } 6682 6683 int 6684 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 6685 { 6686 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 6687 6688 mutex_enter(&dtrace_meta_lock); 6689 mutex_enter(&dtrace_lock); 6690 6691 if (old == dtrace_meta_pid) { 6692 pp = &dtrace_meta_pid; 6693 } else { 6694 panic("attempt to unregister non-existent " 6695 "dtrace meta-provider %p\n", (void *)old); 6696 } 6697 6698 if (old->dtm_count != 0) { 6699 mutex_exit(&dtrace_lock); 6700 mutex_exit(&dtrace_meta_lock); 6701 return (EBUSY); 6702 } 6703 6704 *pp = NULL; 6705 6706 mutex_exit(&dtrace_lock); 6707 mutex_exit(&dtrace_meta_lock); 6708 6709 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 6710 kmem_free(old, sizeof (dtrace_meta_t)); 6711 6712 return (0); 6713 } 6714 6715 6716 /* 6717 * DTrace DIF Object Functions 6718 */ 6719 static int 6720 dtrace_difo_err(uint_t pc, const char *format, ...) 6721 { 6722 if (dtrace_err_verbose) { 6723 va_list alist; 6724 6725 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 6726 va_start(alist, format); 6727 (void) vuprintf(format, alist); 6728 va_end(alist); 6729 } 6730 6731 #ifdef DTRACE_ERRDEBUG 6732 dtrace_errdebug(format); 6733 #endif 6734 return (1); 6735 } 6736 6737 /* 6738 * Validate a DTrace DIF object by checking the IR instructions. The following 6739 * rules are currently enforced by dtrace_difo_validate(): 6740 * 6741 * 1. Each instruction must have a valid opcode 6742 * 2. Each register, string, variable, or subroutine reference must be valid 6743 * 3. No instruction can modify register %r0 (must be zero) 6744 * 4. All instruction reserved bits must be set to zero 6745 * 5. The last instruction must be a "ret" instruction 6746 * 6. All branch targets must reference a valid instruction _after_ the branch 6747 */ 6748 static int 6749 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 6750 cred_t *cr) 6751 { 6752 int err = 0, i; 6753 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 6754 int kcheck; 6755 uint_t pc; 6756 6757 kcheck = cr == NULL || 6758 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE) == 0; 6759 6760 dp->dtdo_destructive = 0; 6761 6762 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 6763 dif_instr_t instr = dp->dtdo_buf[pc]; 6764 6765 uint_t r1 = DIF_INSTR_R1(instr); 6766 uint_t r2 = DIF_INSTR_R2(instr); 6767 uint_t rd = DIF_INSTR_RD(instr); 6768 uint_t rs = DIF_INSTR_RS(instr); 6769 uint_t label = DIF_INSTR_LABEL(instr); 6770 uint_t v = DIF_INSTR_VAR(instr); 6771 uint_t subr = DIF_INSTR_SUBR(instr); 6772 uint_t type = DIF_INSTR_TYPE(instr); 6773 uint_t op = DIF_INSTR_OP(instr); 6774 6775 switch (op) { 6776 case DIF_OP_OR: 6777 case DIF_OP_XOR: 6778 case DIF_OP_AND: 6779 case DIF_OP_SLL: 6780 case DIF_OP_SRL: 6781 case DIF_OP_SRA: 6782 case DIF_OP_SUB: 6783 case DIF_OP_ADD: 6784 case DIF_OP_MUL: 6785 case DIF_OP_SDIV: 6786 case DIF_OP_UDIV: 6787 case DIF_OP_SREM: 6788 case DIF_OP_UREM: 6789 case DIF_OP_COPYS: 6790 if (r1 >= nregs) 6791 err += efunc(pc, "invalid register %u\n", r1); 6792 if (r2 >= nregs) 6793 err += efunc(pc, "invalid register %u\n", r2); 6794 if (rd >= nregs) 6795 err += efunc(pc, "invalid register %u\n", rd); 6796 if (rd == 0) 6797 err += efunc(pc, "cannot write to %r0\n"); 6798 break; 6799 case DIF_OP_NOT: 6800 case DIF_OP_MOV: 6801 case DIF_OP_ALLOCS: 6802 if (r1 >= nregs) 6803 err += efunc(pc, "invalid register %u\n", r1); 6804 if (r2 != 0) 6805 err += efunc(pc, "non-zero reserved bits\n"); 6806 if (rd >= nregs) 6807 err += efunc(pc, "invalid register %u\n", rd); 6808 if (rd == 0) 6809 err += efunc(pc, "cannot write to %r0\n"); 6810 break; 6811 case DIF_OP_LDSB: 6812 case DIF_OP_LDSH: 6813 case DIF_OP_LDSW: 6814 case DIF_OP_LDUB: 6815 case DIF_OP_LDUH: 6816 case DIF_OP_LDUW: 6817 case DIF_OP_LDX: 6818 if (r1 >= nregs) 6819 err += efunc(pc, "invalid register %u\n", r1); 6820 if (r2 != 0) 6821 err += efunc(pc, "non-zero reserved bits\n"); 6822 if (rd >= nregs) 6823 err += efunc(pc, "invalid register %u\n", rd); 6824 if (rd == 0) 6825 err += efunc(pc, "cannot write to %r0\n"); 6826 if (kcheck) 6827 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 6828 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 6829 break; 6830 case DIF_OP_RLDSB: 6831 case DIF_OP_RLDSH: 6832 case DIF_OP_RLDSW: 6833 case DIF_OP_RLDUB: 6834 case DIF_OP_RLDUH: 6835 case DIF_OP_RLDUW: 6836 case DIF_OP_RLDX: 6837 if (r1 >= nregs) 6838 err += efunc(pc, "invalid register %u\n", r1); 6839 if (r2 != 0) 6840 err += efunc(pc, "non-zero reserved bits\n"); 6841 if (rd >= nregs) 6842 err += efunc(pc, "invalid register %u\n", rd); 6843 if (rd == 0) 6844 err += efunc(pc, "cannot write to %r0\n"); 6845 break; 6846 case DIF_OP_ULDSB: 6847 case DIF_OP_ULDSH: 6848 case DIF_OP_ULDSW: 6849 case DIF_OP_ULDUB: 6850 case DIF_OP_ULDUH: 6851 case DIF_OP_ULDUW: 6852 case DIF_OP_ULDX: 6853 if (r1 >= nregs) 6854 err += efunc(pc, "invalid register %u\n", r1); 6855 if (r2 != 0) 6856 err += efunc(pc, "non-zero reserved bits\n"); 6857 if (rd >= nregs) 6858 err += efunc(pc, "invalid register %u\n", rd); 6859 if (rd == 0) 6860 err += efunc(pc, "cannot write to %r0\n"); 6861 break; 6862 case DIF_OP_STB: 6863 case DIF_OP_STH: 6864 case DIF_OP_STW: 6865 case DIF_OP_STX: 6866 if (r1 >= nregs) 6867 err += efunc(pc, "invalid register %u\n", r1); 6868 if (r2 != 0) 6869 err += efunc(pc, "non-zero reserved bits\n"); 6870 if (rd >= nregs) 6871 err += efunc(pc, "invalid register %u\n", rd); 6872 if (rd == 0) 6873 err += efunc(pc, "cannot write to 0 address\n"); 6874 break; 6875 case DIF_OP_CMP: 6876 case DIF_OP_SCMP: 6877 if (r1 >= nregs) 6878 err += efunc(pc, "invalid register %u\n", r1); 6879 if (r2 >= nregs) 6880 err += efunc(pc, "invalid register %u\n", r2); 6881 if (rd != 0) 6882 err += efunc(pc, "non-zero reserved bits\n"); 6883 break; 6884 case DIF_OP_TST: 6885 if (r1 >= nregs) 6886 err += efunc(pc, "invalid register %u\n", r1); 6887 if (r2 != 0 || rd != 0) 6888 err += efunc(pc, "non-zero reserved bits\n"); 6889 break; 6890 case DIF_OP_BA: 6891 case DIF_OP_BE: 6892 case DIF_OP_BNE: 6893 case DIF_OP_BG: 6894 case DIF_OP_BGU: 6895 case DIF_OP_BGE: 6896 case DIF_OP_BGEU: 6897 case DIF_OP_BL: 6898 case DIF_OP_BLU: 6899 case DIF_OP_BLE: 6900 case DIF_OP_BLEU: 6901 if (label >= dp->dtdo_len) { 6902 err += efunc(pc, "invalid branch target %u\n", 6903 label); 6904 } 6905 if (label <= pc) { 6906 err += efunc(pc, "backward branch to %u\n", 6907 label); 6908 } 6909 break; 6910 case DIF_OP_RET: 6911 if (r1 != 0 || r2 != 0) 6912 err += efunc(pc, "non-zero reserved bits\n"); 6913 if (rd >= nregs) 6914 err += efunc(pc, "invalid register %u\n", rd); 6915 break; 6916 case DIF_OP_NOP: 6917 case DIF_OP_POPTS: 6918 case DIF_OP_FLUSHTS: 6919 if (r1 != 0 || r2 != 0 || rd != 0) 6920 err += efunc(pc, "non-zero reserved bits\n"); 6921 break; 6922 case DIF_OP_SETX: 6923 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 6924 err += efunc(pc, "invalid integer ref %u\n", 6925 DIF_INSTR_INTEGER(instr)); 6926 } 6927 if (rd >= nregs) 6928 err += efunc(pc, "invalid register %u\n", rd); 6929 if (rd == 0) 6930 err += efunc(pc, "cannot write to %r0\n"); 6931 break; 6932 case DIF_OP_SETS: 6933 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 6934 err += efunc(pc, "invalid string ref %u\n", 6935 DIF_INSTR_STRING(instr)); 6936 } 6937 if (rd >= nregs) 6938 err += efunc(pc, "invalid register %u\n", rd); 6939 if (rd == 0) 6940 err += efunc(pc, "cannot write to %r0\n"); 6941 break; 6942 case DIF_OP_LDGA: 6943 case DIF_OP_LDTA: 6944 if (r1 > DIF_VAR_ARRAY_MAX) 6945 err += efunc(pc, "invalid array %u\n", r1); 6946 if (r2 >= nregs) 6947 err += efunc(pc, "invalid register %u\n", r2); 6948 if (rd >= nregs) 6949 err += efunc(pc, "invalid register %u\n", rd); 6950 if (rd == 0) 6951 err += efunc(pc, "cannot write to %r0\n"); 6952 break; 6953 case DIF_OP_LDGS: 6954 case DIF_OP_LDTS: 6955 case DIF_OP_LDLS: 6956 case DIF_OP_LDGAA: 6957 case DIF_OP_LDTAA: 6958 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 6959 err += efunc(pc, "invalid variable %u\n", v); 6960 if (rd >= nregs) 6961 err += efunc(pc, "invalid register %u\n", rd); 6962 if (rd == 0) 6963 err += efunc(pc, "cannot write to %r0\n"); 6964 break; 6965 case DIF_OP_STGS: 6966 case DIF_OP_STTS: 6967 case DIF_OP_STLS: 6968 case DIF_OP_STGAA: 6969 case DIF_OP_STTAA: 6970 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 6971 err += efunc(pc, "invalid variable %u\n", v); 6972 if (rs >= nregs) 6973 err += efunc(pc, "invalid register %u\n", rd); 6974 break; 6975 case DIF_OP_CALL: 6976 if (subr > DIF_SUBR_MAX) 6977 err += efunc(pc, "invalid subr %u\n", subr); 6978 if (rd >= nregs) 6979 err += efunc(pc, "invalid register %u\n", rd); 6980 if (rd == 0) 6981 err += efunc(pc, "cannot write to %r0\n"); 6982 6983 if (subr == DIF_SUBR_COPYOUT || 6984 subr == DIF_SUBR_COPYOUTSTR) { 6985 dp->dtdo_destructive = 1; 6986 } 6987 break; 6988 case DIF_OP_PUSHTR: 6989 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 6990 err += efunc(pc, "invalid ref type %u\n", type); 6991 if (r2 >= nregs) 6992 err += efunc(pc, "invalid register %u\n", r2); 6993 if (rs >= nregs) 6994 err += efunc(pc, "invalid register %u\n", rs); 6995 break; 6996 case DIF_OP_PUSHTV: 6997 if (type != DIF_TYPE_CTF) 6998 err += efunc(pc, "invalid val type %u\n", type); 6999 if (r2 >= nregs) 7000 err += efunc(pc, "invalid register %u\n", r2); 7001 if (rs >= nregs) 7002 err += efunc(pc, "invalid register %u\n", rs); 7003 break; 7004 default: 7005 err += efunc(pc, "invalid opcode %u\n", 7006 DIF_INSTR_OP(instr)); 7007 } 7008 } 7009 7010 if (dp->dtdo_len != 0 && 7011 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 7012 err += efunc(dp->dtdo_len - 1, 7013 "expected 'ret' as last DIF instruction\n"); 7014 } 7015 7016 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 7017 /* 7018 * If we're not returning by reference, the size must be either 7019 * 0 or the size of one of the base types. 7020 */ 7021 switch (dp->dtdo_rtype.dtdt_size) { 7022 case 0: 7023 case sizeof (uint8_t): 7024 case sizeof (uint16_t): 7025 case sizeof (uint32_t): 7026 case sizeof (uint64_t): 7027 break; 7028 7029 default: 7030 err += efunc(dp->dtdo_len - 1, "bad return size"); 7031 } 7032 } 7033 7034 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 7035 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 7036 dtrace_diftype_t *vt, *et; 7037 uint_t id, ndx; 7038 7039 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 7040 v->dtdv_scope != DIFV_SCOPE_THREAD && 7041 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 7042 err += efunc(i, "unrecognized variable scope %d\n", 7043 v->dtdv_scope); 7044 break; 7045 } 7046 7047 if (v->dtdv_kind != DIFV_KIND_ARRAY && 7048 v->dtdv_kind != DIFV_KIND_SCALAR) { 7049 err += efunc(i, "unrecognized variable type %d\n", 7050 v->dtdv_kind); 7051 break; 7052 } 7053 7054 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 7055 err += efunc(i, "%d exceeds variable id limit\n", id); 7056 break; 7057 } 7058 7059 if (id < DIF_VAR_OTHER_UBASE) 7060 continue; 7061 7062 /* 7063 * For user-defined variables, we need to check that this 7064 * definition is identical to any previous definition that we 7065 * encountered. 7066 */ 7067 ndx = id - DIF_VAR_OTHER_UBASE; 7068 7069 switch (v->dtdv_scope) { 7070 case DIFV_SCOPE_GLOBAL: 7071 if (ndx < vstate->dtvs_nglobals) { 7072 dtrace_statvar_t *svar; 7073 7074 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 7075 existing = &svar->dtsv_var; 7076 } 7077 7078 break; 7079 7080 case DIFV_SCOPE_THREAD: 7081 if (ndx < vstate->dtvs_ntlocals) 7082 existing = &vstate->dtvs_tlocals[ndx]; 7083 break; 7084 7085 case DIFV_SCOPE_LOCAL: 7086 if (ndx < vstate->dtvs_nlocals) { 7087 dtrace_statvar_t *svar; 7088 7089 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 7090 existing = &svar->dtsv_var; 7091 } 7092 7093 break; 7094 } 7095 7096 vt = &v->dtdv_type; 7097 7098 if (vt->dtdt_flags & DIF_TF_BYREF) { 7099 if (vt->dtdt_size == 0) { 7100 err += efunc(i, "zero-sized variable\n"); 7101 break; 7102 } 7103 7104 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 7105 vt->dtdt_size > dtrace_global_maxsize) { 7106 err += efunc(i, "oversized by-ref global\n"); 7107 break; 7108 } 7109 } 7110 7111 if (existing == NULL || existing->dtdv_id == 0) 7112 continue; 7113 7114 ASSERT(existing->dtdv_id == v->dtdv_id); 7115 ASSERT(existing->dtdv_scope == v->dtdv_scope); 7116 7117 if (existing->dtdv_kind != v->dtdv_kind) 7118 err += efunc(i, "%d changed variable kind\n", id); 7119 7120 et = &existing->dtdv_type; 7121 7122 if (vt->dtdt_flags != et->dtdt_flags) { 7123 err += efunc(i, "%d changed variable type flags\n", id); 7124 break; 7125 } 7126 7127 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 7128 err += efunc(i, "%d changed variable type size\n", id); 7129 break; 7130 } 7131 } 7132 7133 return (err); 7134 } 7135 7136 /* 7137 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 7138 * are much more constrained than normal DIFOs. Specifically, they may 7139 * not: 7140 * 7141 * 1. Make calls to subroutines other than copyin(), copyinstr() or 7142 * miscellaneous string routines 7143 * 2. Access DTrace variables other than the args[] array, and the 7144 * curthread, pid, tid and execname variables. 7145 * 3. Have thread-local variables. 7146 * 4. Have dynamic variables. 7147 */ 7148 static int 7149 dtrace_difo_validate_helper(dtrace_difo_t *dp) 7150 { 7151 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 7152 int err = 0; 7153 uint_t pc; 7154 7155 for (pc = 0; pc < dp->dtdo_len; pc++) { 7156 dif_instr_t instr = dp->dtdo_buf[pc]; 7157 7158 uint_t v = DIF_INSTR_VAR(instr); 7159 uint_t subr = DIF_INSTR_SUBR(instr); 7160 uint_t op = DIF_INSTR_OP(instr); 7161 7162 switch (op) { 7163 case DIF_OP_OR: 7164 case DIF_OP_XOR: 7165 case DIF_OP_AND: 7166 case DIF_OP_SLL: 7167 case DIF_OP_SRL: 7168 case DIF_OP_SRA: 7169 case DIF_OP_SUB: 7170 case DIF_OP_ADD: 7171 case DIF_OP_MUL: 7172 case DIF_OP_SDIV: 7173 case DIF_OP_UDIV: 7174 case DIF_OP_SREM: 7175 case DIF_OP_UREM: 7176 case DIF_OP_COPYS: 7177 case DIF_OP_NOT: 7178 case DIF_OP_MOV: 7179 case DIF_OP_RLDSB: 7180 case DIF_OP_RLDSH: 7181 case DIF_OP_RLDSW: 7182 case DIF_OP_RLDUB: 7183 case DIF_OP_RLDUH: 7184 case DIF_OP_RLDUW: 7185 case DIF_OP_RLDX: 7186 case DIF_OP_ULDSB: 7187 case DIF_OP_ULDSH: 7188 case DIF_OP_ULDSW: 7189 case DIF_OP_ULDUB: 7190 case DIF_OP_ULDUH: 7191 case DIF_OP_ULDUW: 7192 case DIF_OP_ULDX: 7193 case DIF_OP_STB: 7194 case DIF_OP_STH: 7195 case DIF_OP_STW: 7196 case DIF_OP_STX: 7197 case DIF_OP_ALLOCS: 7198 case DIF_OP_CMP: 7199 case DIF_OP_SCMP: 7200 case DIF_OP_TST: 7201 case DIF_OP_BA: 7202 case DIF_OP_BE: 7203 case DIF_OP_BNE: 7204 case DIF_OP_BG: 7205 case DIF_OP_BGU: 7206 case DIF_OP_BGE: 7207 case DIF_OP_BGEU: 7208 case DIF_OP_BL: 7209 case DIF_OP_BLU: 7210 case DIF_OP_BLE: 7211 case DIF_OP_BLEU: 7212 case DIF_OP_RET: 7213 case DIF_OP_NOP: 7214 case DIF_OP_POPTS: 7215 case DIF_OP_FLUSHTS: 7216 case DIF_OP_SETX: 7217 case DIF_OP_SETS: 7218 case DIF_OP_LDGA: 7219 case DIF_OP_LDLS: 7220 case DIF_OP_STGS: 7221 case DIF_OP_STLS: 7222 case DIF_OP_PUSHTR: 7223 case DIF_OP_PUSHTV: 7224 break; 7225 7226 case DIF_OP_LDGS: 7227 if (v >= DIF_VAR_OTHER_UBASE) 7228 break; 7229 7230 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 7231 break; 7232 7233 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 7234 v == DIF_VAR_TID || v == DIF_VAR_EXECNAME || 7235 v == DIF_VAR_ZONENAME) 7236 break; 7237 7238 err += efunc(pc, "illegal variable %u\n", v); 7239 break; 7240 7241 case DIF_OP_LDTA: 7242 case DIF_OP_LDTS: 7243 case DIF_OP_LDGAA: 7244 case DIF_OP_LDTAA: 7245 err += efunc(pc, "illegal dynamic variable load\n"); 7246 break; 7247 7248 case DIF_OP_STTS: 7249 case DIF_OP_STGAA: 7250 case DIF_OP_STTAA: 7251 err += efunc(pc, "illegal dynamic variable store\n"); 7252 break; 7253 7254 case DIF_OP_CALL: 7255 if (subr == DIF_SUBR_ALLOCA || 7256 subr == DIF_SUBR_BCOPY || 7257 subr == DIF_SUBR_COPYIN || 7258 subr == DIF_SUBR_COPYINTO || 7259 subr == DIF_SUBR_COPYINSTR || 7260 subr == DIF_SUBR_INDEX || 7261 subr == DIF_SUBR_LLTOSTR || 7262 subr == DIF_SUBR_RINDEX || 7263 subr == DIF_SUBR_STRCHR || 7264 subr == DIF_SUBR_STRJOIN || 7265 subr == DIF_SUBR_STRRCHR || 7266 subr == DIF_SUBR_STRSTR) 7267 break; 7268 7269 err += efunc(pc, "invalid subr %u\n", subr); 7270 break; 7271 7272 default: 7273 err += efunc(pc, "invalid opcode %u\n", 7274 DIF_INSTR_OP(instr)); 7275 } 7276 } 7277 7278 return (err); 7279 } 7280 7281 /* 7282 * Returns 1 if the expression in the DIF object can be cached on a per-thread 7283 * basis; 0 if not. 7284 */ 7285 static int 7286 dtrace_difo_cacheable(dtrace_difo_t *dp) 7287 { 7288 int i; 7289 7290 if (dp == NULL) 7291 return (0); 7292 7293 for (i = 0; i < dp->dtdo_varlen; i++) { 7294 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7295 7296 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 7297 continue; 7298 7299 switch (v->dtdv_id) { 7300 case DIF_VAR_CURTHREAD: 7301 case DIF_VAR_PID: 7302 case DIF_VAR_TID: 7303 case DIF_VAR_EXECNAME: 7304 case DIF_VAR_ZONENAME: 7305 break; 7306 7307 default: 7308 return (0); 7309 } 7310 } 7311 7312 /* 7313 * This DIF object may be cacheable. Now we need to look for any 7314 * array loading instructions, any memory loading instructions, or 7315 * any stores to thread-local variables. 7316 */ 7317 for (i = 0; i < dp->dtdo_len; i++) { 7318 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 7319 7320 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 7321 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 7322 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 7323 op == DIF_OP_LDGA || op == DIF_OP_STTS) 7324 return (0); 7325 } 7326 7327 return (1); 7328 } 7329 7330 static void 7331 dtrace_difo_hold(dtrace_difo_t *dp) 7332 { 7333 int i; 7334 7335 ASSERT(MUTEX_HELD(&dtrace_lock)); 7336 7337 dp->dtdo_refcnt++; 7338 ASSERT(dp->dtdo_refcnt != 0); 7339 7340 /* 7341 * We need to check this DIF object for references to the variable 7342 * DIF_VAR_VTIMESTAMP. 7343 */ 7344 for (i = 0; i < dp->dtdo_varlen; i++) { 7345 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7346 7347 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 7348 continue; 7349 7350 if (dtrace_vtime_references++ == 0) 7351 dtrace_vtime_enable(); 7352 } 7353 } 7354 7355 /* 7356 * This routine calculates the dynamic variable chunksize for a given DIF 7357 * object. The calculation is not fool-proof, and can probably be tricked by 7358 * malicious DIF -- but it works for all compiler-generated DIF. Because this 7359 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 7360 * if a dynamic variable size exceeds the chunksize. 7361 */ 7362 static void 7363 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7364 { 7365 uint64_t sval; 7366 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 7367 const dif_instr_t *text = dp->dtdo_buf; 7368 uint_t pc, srd = 0; 7369 uint_t ttop = 0; 7370 size_t size, ksize; 7371 uint_t id, i; 7372 7373 for (pc = 0; pc < dp->dtdo_len; pc++) { 7374 dif_instr_t instr = text[pc]; 7375 uint_t op = DIF_INSTR_OP(instr); 7376 uint_t rd = DIF_INSTR_RD(instr); 7377 uint_t r1 = DIF_INSTR_R1(instr); 7378 uint_t nkeys = 0; 7379 uchar_t scope; 7380 7381 dtrace_key_t *key = tupregs; 7382 7383 switch (op) { 7384 case DIF_OP_SETX: 7385 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 7386 srd = rd; 7387 continue; 7388 7389 case DIF_OP_STTS: 7390 key = &tupregs[DIF_DTR_NREGS]; 7391 key[0].dttk_size = 0; 7392 key[1].dttk_size = 0; 7393 nkeys = 2; 7394 scope = DIFV_SCOPE_THREAD; 7395 break; 7396 7397 case DIF_OP_STGAA: 7398 case DIF_OP_STTAA: 7399 nkeys = ttop; 7400 7401 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 7402 key[nkeys++].dttk_size = 0; 7403 7404 key[nkeys++].dttk_size = 0; 7405 7406 if (op == DIF_OP_STTAA) { 7407 scope = DIFV_SCOPE_THREAD; 7408 } else { 7409 scope = DIFV_SCOPE_GLOBAL; 7410 } 7411 7412 break; 7413 7414 case DIF_OP_PUSHTR: 7415 if (ttop == DIF_DTR_NREGS) 7416 return; 7417 7418 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 7419 /* 7420 * If the register for the size of the "pushtr" 7421 * is %r0 (or the value is 0) and the type is 7422 * a string, we'll use the system-wide default 7423 * string size. 7424 */ 7425 tupregs[ttop++].dttk_size = 7426 dtrace_strsize_default; 7427 } else { 7428 if (srd == 0) 7429 return; 7430 7431 tupregs[ttop++].dttk_size = sval; 7432 } 7433 7434 break; 7435 7436 case DIF_OP_PUSHTV: 7437 if (ttop == DIF_DTR_NREGS) 7438 return; 7439 7440 tupregs[ttop++].dttk_size = 0; 7441 break; 7442 7443 case DIF_OP_FLUSHTS: 7444 ttop = 0; 7445 break; 7446 7447 case DIF_OP_POPTS: 7448 if (ttop != 0) 7449 ttop--; 7450 break; 7451 } 7452 7453 sval = 0; 7454 srd = 0; 7455 7456 if (nkeys == 0) 7457 continue; 7458 7459 /* 7460 * We have a dynamic variable allocation; calculate its size. 7461 */ 7462 for (ksize = 0, i = 0; i < nkeys; i++) 7463 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 7464 7465 size = sizeof (dtrace_dynvar_t); 7466 size += sizeof (dtrace_key_t) * (nkeys - 1); 7467 size += ksize; 7468 7469 /* 7470 * Now we need to determine the size of the stored data. 7471 */ 7472 id = DIF_INSTR_VAR(instr); 7473 7474 for (i = 0; i < dp->dtdo_varlen; i++) { 7475 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7476 7477 if (v->dtdv_id == id && v->dtdv_scope == scope) { 7478 size += v->dtdv_type.dtdt_size; 7479 break; 7480 } 7481 } 7482 7483 if (i == dp->dtdo_varlen) 7484 return; 7485 7486 /* 7487 * We have the size. If this is larger than the chunk size 7488 * for our dynamic variable state, reset the chunk size. 7489 */ 7490 size = P2ROUNDUP(size, sizeof (uint64_t)); 7491 7492 if (size > vstate->dtvs_dynvars.dtds_chunksize) 7493 vstate->dtvs_dynvars.dtds_chunksize = size; 7494 } 7495 } 7496 7497 static void 7498 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7499 { 7500 int i, oldsvars, osz, nsz, otlocals, ntlocals; 7501 uint_t id; 7502 7503 ASSERT(MUTEX_HELD(&dtrace_lock)); 7504 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 7505 7506 for (i = 0; i < dp->dtdo_varlen; i++) { 7507 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7508 dtrace_statvar_t *svar, ***svarp; 7509 size_t dsize = 0; 7510 uint8_t scope = v->dtdv_scope; 7511 int *np; 7512 7513 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 7514 continue; 7515 7516 id -= DIF_VAR_OTHER_UBASE; 7517 7518 switch (scope) { 7519 case DIFV_SCOPE_THREAD: 7520 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 7521 dtrace_difv_t *tlocals; 7522 7523 if ((ntlocals = (otlocals << 1)) == 0) 7524 ntlocals = 1; 7525 7526 osz = otlocals * sizeof (dtrace_difv_t); 7527 nsz = ntlocals * sizeof (dtrace_difv_t); 7528 7529 tlocals = kmem_zalloc(nsz, KM_SLEEP); 7530 7531 if (osz != 0) { 7532 bcopy(vstate->dtvs_tlocals, 7533 tlocals, osz); 7534 kmem_free(vstate->dtvs_tlocals, osz); 7535 } 7536 7537 vstate->dtvs_tlocals = tlocals; 7538 vstate->dtvs_ntlocals = ntlocals; 7539 } 7540 7541 vstate->dtvs_tlocals[id] = *v; 7542 continue; 7543 7544 case DIFV_SCOPE_LOCAL: 7545 np = &vstate->dtvs_nlocals; 7546 svarp = &vstate->dtvs_locals; 7547 7548 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 7549 dsize = NCPU * (v->dtdv_type.dtdt_size + 7550 sizeof (uint64_t)); 7551 else 7552 dsize = NCPU * sizeof (uint64_t); 7553 7554 break; 7555 7556 case DIFV_SCOPE_GLOBAL: 7557 np = &vstate->dtvs_nglobals; 7558 svarp = &vstate->dtvs_globals; 7559 7560 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 7561 dsize = v->dtdv_type.dtdt_size + 7562 sizeof (uint64_t); 7563 7564 break; 7565 7566 default: 7567 ASSERT(0); 7568 } 7569 7570 while (id >= (oldsvars = *np)) { 7571 dtrace_statvar_t **statics; 7572 int newsvars, oldsize, newsize; 7573 7574 if ((newsvars = (oldsvars << 1)) == 0) 7575 newsvars = 1; 7576 7577 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 7578 newsize = newsvars * sizeof (dtrace_statvar_t *); 7579 7580 statics = kmem_zalloc(newsize, KM_SLEEP); 7581 7582 if (oldsize != 0) { 7583 bcopy(*svarp, statics, oldsize); 7584 kmem_free(*svarp, oldsize); 7585 } 7586 7587 *svarp = statics; 7588 *np = newsvars; 7589 } 7590 7591 if ((svar = (*svarp)[id]) == NULL) { 7592 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 7593 svar->dtsv_var = *v; 7594 7595 if ((svar->dtsv_size = dsize) != 0) { 7596 svar->dtsv_data = (uint64_t)(uintptr_t) 7597 kmem_zalloc(dsize, KM_SLEEP); 7598 } 7599 7600 (*svarp)[id] = svar; 7601 } 7602 7603 svar->dtsv_refcnt++; 7604 } 7605 7606 dtrace_difo_chunksize(dp, vstate); 7607 dtrace_difo_hold(dp); 7608 } 7609 7610 static dtrace_difo_t * 7611 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7612 { 7613 dtrace_difo_t *new; 7614 size_t sz; 7615 7616 ASSERT(dp->dtdo_buf != NULL); 7617 ASSERT(dp->dtdo_refcnt != 0); 7618 7619 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 7620 7621 ASSERT(dp->dtdo_buf != NULL); 7622 sz = dp->dtdo_len * sizeof (dif_instr_t); 7623 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 7624 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 7625 new->dtdo_len = dp->dtdo_len; 7626 7627 if (dp->dtdo_strtab != NULL) { 7628 ASSERT(dp->dtdo_strlen != 0); 7629 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 7630 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 7631 new->dtdo_strlen = dp->dtdo_strlen; 7632 } 7633 7634 if (dp->dtdo_inttab != NULL) { 7635 ASSERT(dp->dtdo_intlen != 0); 7636 sz = dp->dtdo_intlen * sizeof (uint64_t); 7637 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 7638 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 7639 new->dtdo_intlen = dp->dtdo_intlen; 7640 } 7641 7642 if (dp->dtdo_vartab != NULL) { 7643 ASSERT(dp->dtdo_varlen != 0); 7644 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 7645 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 7646 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 7647 new->dtdo_varlen = dp->dtdo_varlen; 7648 } 7649 7650 dtrace_difo_init(new, vstate); 7651 return (new); 7652 } 7653 7654 static void 7655 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7656 { 7657 int i; 7658 7659 ASSERT(dp->dtdo_refcnt == 0); 7660 7661 for (i = 0; i < dp->dtdo_varlen; i++) { 7662 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7663 dtrace_statvar_t *svar, **svarp; 7664 uint_t id; 7665 uint8_t scope = v->dtdv_scope; 7666 int *np; 7667 7668 switch (scope) { 7669 case DIFV_SCOPE_THREAD: 7670 continue; 7671 7672 case DIFV_SCOPE_LOCAL: 7673 np = &vstate->dtvs_nlocals; 7674 svarp = vstate->dtvs_locals; 7675 break; 7676 7677 case DIFV_SCOPE_GLOBAL: 7678 np = &vstate->dtvs_nglobals; 7679 svarp = vstate->dtvs_globals; 7680 break; 7681 7682 default: 7683 ASSERT(0); 7684 } 7685 7686 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 7687 continue; 7688 7689 id -= DIF_VAR_OTHER_UBASE; 7690 ASSERT(id < *np); 7691 7692 svar = svarp[id]; 7693 ASSERT(svar != NULL); 7694 ASSERT(svar->dtsv_refcnt > 0); 7695 7696 if (--svar->dtsv_refcnt > 0) 7697 continue; 7698 7699 if (svar->dtsv_size != 0) { 7700 ASSERT(svar->dtsv_data != NULL); 7701 kmem_free((void *)(uintptr_t)svar->dtsv_data, 7702 svar->dtsv_size); 7703 } 7704 7705 kmem_free(svar, sizeof (dtrace_statvar_t)); 7706 svarp[id] = NULL; 7707 } 7708 7709 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 7710 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 7711 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 7712 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 7713 7714 kmem_free(dp, sizeof (dtrace_difo_t)); 7715 } 7716 7717 static void 7718 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7719 { 7720 int i; 7721 7722 ASSERT(MUTEX_HELD(&dtrace_lock)); 7723 ASSERT(dp->dtdo_refcnt != 0); 7724 7725 for (i = 0; i < dp->dtdo_varlen; i++) { 7726 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7727 7728 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 7729 continue; 7730 7731 ASSERT(dtrace_vtime_references > 0); 7732 if (--dtrace_vtime_references == 0) 7733 dtrace_vtime_disable(); 7734 } 7735 7736 if (--dp->dtdo_refcnt == 0) 7737 dtrace_difo_destroy(dp, vstate); 7738 } 7739 7740 /* 7741 * DTrace Format Functions 7742 */ 7743 static uint16_t 7744 dtrace_format_add(dtrace_state_t *state, char *str) 7745 { 7746 char *fmt, **new; 7747 uint16_t ndx, len = strlen(str) + 1; 7748 7749 fmt = kmem_zalloc(len, KM_SLEEP); 7750 bcopy(str, fmt, len); 7751 7752 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 7753 if (state->dts_formats[ndx] == NULL) { 7754 state->dts_formats[ndx] = fmt; 7755 return (ndx + 1); 7756 } 7757 } 7758 7759 if (state->dts_nformats == USHRT_MAX) { 7760 /* 7761 * This is only likely if a denial-of-service attack is being 7762 * attempted. As such, it's okay to fail silently here. 7763 */ 7764 kmem_free(fmt, len); 7765 return (0); 7766 } 7767 7768 /* 7769 * For simplicity, we always resize the formats array to be exactly the 7770 * number of formats. 7771 */ 7772 ndx = state->dts_nformats++; 7773 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 7774 7775 if (state->dts_formats != NULL) { 7776 ASSERT(ndx != 0); 7777 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 7778 kmem_free(state->dts_formats, ndx * sizeof (char *)); 7779 } 7780 7781 state->dts_formats = new; 7782 state->dts_formats[ndx] = fmt; 7783 7784 return (ndx + 1); 7785 } 7786 7787 static void 7788 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 7789 { 7790 char *fmt; 7791 7792 ASSERT(state->dts_formats != NULL); 7793 ASSERT(format <= state->dts_nformats); 7794 ASSERT(state->dts_formats[format - 1] != NULL); 7795 7796 fmt = state->dts_formats[format - 1]; 7797 kmem_free(fmt, strlen(fmt) + 1); 7798 state->dts_formats[format - 1] = NULL; 7799 } 7800 7801 static void 7802 dtrace_format_destroy(dtrace_state_t *state) 7803 { 7804 int i; 7805 7806 if (state->dts_nformats == 0) { 7807 ASSERT(state->dts_formats == NULL); 7808 return; 7809 } 7810 7811 ASSERT(state->dts_formats != NULL); 7812 7813 for (i = 0; i < state->dts_nformats; i++) { 7814 char *fmt = state->dts_formats[i]; 7815 7816 if (fmt == NULL) 7817 continue; 7818 7819 kmem_free(fmt, strlen(fmt) + 1); 7820 } 7821 7822 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 7823 state->dts_nformats = 0; 7824 state->dts_formats = NULL; 7825 } 7826 7827 /* 7828 * DTrace Predicate Functions 7829 */ 7830 static dtrace_predicate_t * 7831 dtrace_predicate_create(dtrace_difo_t *dp) 7832 { 7833 dtrace_predicate_t *pred; 7834 7835 ASSERT(MUTEX_HELD(&dtrace_lock)); 7836 ASSERT(dp->dtdo_refcnt != 0); 7837 7838 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 7839 pred->dtp_difo = dp; 7840 pred->dtp_refcnt = 1; 7841 7842 if (!dtrace_difo_cacheable(dp)) 7843 return (pred); 7844 7845 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 7846 /* 7847 * This is only theoretically possible -- we have had 2^32 7848 * cacheable predicates on this machine. We cannot allow any 7849 * more predicates to become cacheable: as unlikely as it is, 7850 * there may be a thread caching a (now stale) predicate cache 7851 * ID. (N.B.: the temptation is being successfully resisted to 7852 * have this cmn_err() "Holy shit -- we executed this code!") 7853 */ 7854 return (pred); 7855 } 7856 7857 pred->dtp_cacheid = dtrace_predcache_id++; 7858 7859 return (pred); 7860 } 7861 7862 static void 7863 dtrace_predicate_hold(dtrace_predicate_t *pred) 7864 { 7865 ASSERT(MUTEX_HELD(&dtrace_lock)); 7866 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 7867 ASSERT(pred->dtp_refcnt > 0); 7868 7869 pred->dtp_refcnt++; 7870 } 7871 7872 static void 7873 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 7874 { 7875 dtrace_difo_t *dp = pred->dtp_difo; 7876 7877 ASSERT(MUTEX_HELD(&dtrace_lock)); 7878 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 7879 ASSERT(pred->dtp_refcnt > 0); 7880 7881 if (--pred->dtp_refcnt == 0) { 7882 dtrace_difo_release(pred->dtp_difo, vstate); 7883 kmem_free(pred, sizeof (dtrace_predicate_t)); 7884 } 7885 } 7886 7887 /* 7888 * DTrace Action Description Functions 7889 */ 7890 static dtrace_actdesc_t * 7891 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 7892 uint64_t uarg, uint64_t arg) 7893 { 7894 dtrace_actdesc_t *act; 7895 7896 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 7897 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 7898 7899 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 7900 act->dtad_kind = kind; 7901 act->dtad_ntuple = ntuple; 7902 act->dtad_uarg = uarg; 7903 act->dtad_arg = arg; 7904 act->dtad_refcnt = 1; 7905 7906 return (act); 7907 } 7908 7909 static void 7910 dtrace_actdesc_hold(dtrace_actdesc_t *act) 7911 { 7912 ASSERT(act->dtad_refcnt >= 1); 7913 act->dtad_refcnt++; 7914 } 7915 7916 static void 7917 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 7918 { 7919 dtrace_actkind_t kind = act->dtad_kind; 7920 dtrace_difo_t *dp; 7921 7922 ASSERT(act->dtad_refcnt >= 1); 7923 7924 if (--act->dtad_refcnt != 0) 7925 return; 7926 7927 if ((dp = act->dtad_difo) != NULL) 7928 dtrace_difo_release(dp, vstate); 7929 7930 if (DTRACEACT_ISPRINTFLIKE(kind)) { 7931 char *str = (char *)(uintptr_t)act->dtad_arg; 7932 7933 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 7934 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 7935 7936 if (str != NULL) 7937 kmem_free(str, strlen(str) + 1); 7938 } 7939 7940 kmem_free(act, sizeof (dtrace_actdesc_t)); 7941 } 7942 7943 /* 7944 * DTrace ECB Functions 7945 */ 7946 static dtrace_ecb_t * 7947 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 7948 { 7949 dtrace_ecb_t *ecb; 7950 dtrace_epid_t epid; 7951 7952 ASSERT(MUTEX_HELD(&dtrace_lock)); 7953 7954 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 7955 ecb->dte_predicate = NULL; 7956 ecb->dte_probe = probe; 7957 7958 /* 7959 * The default size is the size of the default action: recording 7960 * the epid. 7961 */ 7962 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 7963 ecb->dte_alignment = sizeof (dtrace_epid_t); 7964 7965 epid = state->dts_epid++; 7966 7967 if (epid - 1 >= state->dts_necbs) { 7968 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 7969 int necbs = state->dts_necbs << 1; 7970 7971 ASSERT(epid == state->dts_necbs + 1); 7972 7973 if (necbs == 0) { 7974 ASSERT(oecbs == NULL); 7975 necbs = 1; 7976 } 7977 7978 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 7979 7980 if (oecbs != NULL) 7981 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 7982 7983 dtrace_membar_producer(); 7984 state->dts_ecbs = ecbs; 7985 7986 if (oecbs != NULL) { 7987 /* 7988 * If this state is active, we must dtrace_sync() 7989 * before we can free the old dts_ecbs array: we're 7990 * coming in hot, and there may be active ring 7991 * buffer processing (which indexes into the dts_ecbs 7992 * array) on another CPU. 7993 */ 7994 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 7995 dtrace_sync(); 7996 7997 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 7998 } 7999 8000 dtrace_membar_producer(); 8001 state->dts_necbs = necbs; 8002 } 8003 8004 ecb->dte_state = state; 8005 8006 ASSERT(state->dts_ecbs[epid - 1] == NULL); 8007 dtrace_membar_producer(); 8008 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 8009 8010 return (ecb); 8011 } 8012 8013 static void 8014 dtrace_ecb_enable(dtrace_ecb_t *ecb) 8015 { 8016 dtrace_probe_t *probe = ecb->dte_probe; 8017 8018 ASSERT(MUTEX_HELD(&cpu_lock)); 8019 ASSERT(MUTEX_HELD(&dtrace_lock)); 8020 ASSERT(ecb->dte_next == NULL); 8021 8022 if (probe == NULL) { 8023 /* 8024 * This is the NULL probe -- there's nothing to do. 8025 */ 8026 return; 8027 } 8028 8029 if (probe->dtpr_ecb == NULL) { 8030 dtrace_provider_t *prov = probe->dtpr_provider; 8031 8032 /* 8033 * We're the first ECB on this probe. 8034 */ 8035 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 8036 8037 if (ecb->dte_predicate != NULL) 8038 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 8039 8040 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 8041 probe->dtpr_id, probe->dtpr_arg); 8042 } else { 8043 /* 8044 * This probe is already active. Swing the last pointer to 8045 * point to the new ECB, and issue a dtrace_sync() to assure 8046 * that all CPUs have seen the change. 8047 */ 8048 ASSERT(probe->dtpr_ecb_last != NULL); 8049 probe->dtpr_ecb_last->dte_next = ecb; 8050 probe->dtpr_ecb_last = ecb; 8051 probe->dtpr_predcache = 0; 8052 8053 dtrace_sync(); 8054 } 8055 } 8056 8057 static void 8058 dtrace_ecb_resize(dtrace_ecb_t *ecb) 8059 { 8060 uint32_t maxalign = sizeof (dtrace_epid_t); 8061 uint32_t align = sizeof (uint8_t), offs, diff; 8062 dtrace_action_t *act; 8063 int wastuple = 0; 8064 uint32_t aggbase = UINT32_MAX; 8065 dtrace_state_t *state = ecb->dte_state; 8066 8067 /* 8068 * If we record anything, we always record the epid. (And we always 8069 * record it first.) 8070 */ 8071 offs = sizeof (dtrace_epid_t); 8072 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 8073 8074 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 8075 dtrace_recdesc_t *rec = &act->dta_rec; 8076 8077 if ((align = rec->dtrd_alignment) > maxalign) 8078 maxalign = align; 8079 8080 if (!wastuple && act->dta_intuple) { 8081 /* 8082 * This is the first record in a tuple. Align the 8083 * offset to be at offset 4 in an 8-byte aligned 8084 * block. 8085 */ 8086 diff = offs + sizeof (dtrace_aggid_t); 8087 8088 if (diff = (diff & (sizeof (uint64_t) - 1))) 8089 offs += sizeof (uint64_t) - diff; 8090 8091 aggbase = offs - sizeof (dtrace_aggid_t); 8092 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 8093 } 8094 8095 /*LINTED*/ 8096 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 8097 /* 8098 * The current offset is not properly aligned; align it. 8099 */ 8100 offs += align - diff; 8101 } 8102 8103 rec->dtrd_offset = offs; 8104 8105 if (offs + rec->dtrd_size > ecb->dte_needed) { 8106 ecb->dte_needed = offs + rec->dtrd_size; 8107 8108 if (ecb->dte_needed > state->dts_needed) 8109 state->dts_needed = ecb->dte_needed; 8110 } 8111 8112 if (DTRACEACT_ISAGG(act->dta_kind)) { 8113 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 8114 dtrace_action_t *first = agg->dtag_first, *prev; 8115 8116 ASSERT(rec->dtrd_size != 0 && first != NULL); 8117 ASSERT(wastuple); 8118 ASSERT(aggbase != UINT32_MAX); 8119 8120 agg->dtag_base = aggbase; 8121 8122 while ((prev = first->dta_prev) != NULL && 8123 DTRACEACT_ISAGG(prev->dta_kind)) { 8124 agg = (dtrace_aggregation_t *)prev; 8125 first = agg->dtag_first; 8126 } 8127 8128 if (prev != NULL) { 8129 offs = prev->dta_rec.dtrd_offset + 8130 prev->dta_rec.dtrd_size; 8131 } else { 8132 offs = sizeof (dtrace_epid_t); 8133 } 8134 wastuple = 0; 8135 } else { 8136 if (!act->dta_intuple) 8137 ecb->dte_size = offs + rec->dtrd_size; 8138 8139 offs += rec->dtrd_size; 8140 } 8141 8142 wastuple = act->dta_intuple; 8143 } 8144 8145 if ((act = ecb->dte_action) != NULL && 8146 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 8147 ecb->dte_size == sizeof (dtrace_epid_t)) { 8148 /* 8149 * If the size is still sizeof (dtrace_epid_t), then all 8150 * actions store no data; set the size to 0. 8151 */ 8152 ecb->dte_alignment = maxalign; 8153 ecb->dte_size = 0; 8154 8155 /* 8156 * If the needed space is still sizeof (dtrace_epid_t), then 8157 * all actions need no additional space; set the needed 8158 * size to 0. 8159 */ 8160 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 8161 ecb->dte_needed = 0; 8162 8163 return; 8164 } 8165 8166 /* 8167 * Set our alignment, and make sure that the dte_size and dte_needed 8168 * are aligned to the size of an EPID. 8169 */ 8170 ecb->dte_alignment = maxalign; 8171 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 8172 ~(sizeof (dtrace_epid_t) - 1); 8173 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 8174 ~(sizeof (dtrace_epid_t) - 1); 8175 ASSERT(ecb->dte_size <= ecb->dte_needed); 8176 } 8177 8178 static dtrace_action_t * 8179 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 8180 { 8181 dtrace_aggregation_t *agg; 8182 size_t size = sizeof (uint64_t); 8183 int ntuple = desc->dtad_ntuple; 8184 dtrace_action_t *act; 8185 dtrace_recdesc_t *frec; 8186 dtrace_aggid_t aggid; 8187 dtrace_state_t *state = ecb->dte_state; 8188 8189 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 8190 agg->dtag_ecb = ecb; 8191 8192 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 8193 8194 switch (desc->dtad_kind) { 8195 case DTRACEAGG_MIN: 8196 agg->dtag_initial = UINT64_MAX; 8197 agg->dtag_aggregate = dtrace_aggregate_min; 8198 break; 8199 8200 case DTRACEAGG_MAX: 8201 agg->dtag_aggregate = dtrace_aggregate_max; 8202 break; 8203 8204 case DTRACEAGG_COUNT: 8205 agg->dtag_aggregate = dtrace_aggregate_count; 8206 break; 8207 8208 case DTRACEAGG_QUANTIZE: 8209 agg->dtag_aggregate = dtrace_aggregate_quantize; 8210 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 8211 sizeof (uint64_t); 8212 break; 8213 8214 case DTRACEAGG_LQUANTIZE: { 8215 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 8216 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 8217 8218 agg->dtag_initial = desc->dtad_arg; 8219 agg->dtag_aggregate = dtrace_aggregate_lquantize; 8220 8221 if (step == 0 || levels == 0) 8222 goto err; 8223 8224 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 8225 break; 8226 } 8227 8228 case DTRACEAGG_AVG: 8229 agg->dtag_aggregate = dtrace_aggregate_avg; 8230 size = sizeof (uint64_t) * 2; 8231 break; 8232 8233 case DTRACEAGG_SUM: 8234 agg->dtag_aggregate = dtrace_aggregate_sum; 8235 break; 8236 8237 default: 8238 goto err; 8239 } 8240 8241 agg->dtag_action.dta_rec.dtrd_size = size; 8242 8243 if (ntuple == 0) 8244 goto err; 8245 8246 /* 8247 * We must make sure that we have enough actions for the n-tuple. 8248 */ 8249 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 8250 if (DTRACEACT_ISAGG(act->dta_kind)) 8251 break; 8252 8253 if (--ntuple == 0) { 8254 /* 8255 * This is the action with which our n-tuple begins. 8256 */ 8257 agg->dtag_first = act; 8258 goto success; 8259 } 8260 } 8261 8262 /* 8263 * This n-tuple is short by ntuple elements. Return failure. 8264 */ 8265 ASSERT(ntuple != 0); 8266 err: 8267 kmem_free(agg, sizeof (dtrace_aggregation_t)); 8268 return (NULL); 8269 8270 success: 8271 /* 8272 * If the last action in the tuple has a size of zero, it's actually 8273 * an expression argument for the aggregating action. 8274 */ 8275 ASSERT(ecb->dte_action_last != NULL); 8276 act = ecb->dte_action_last; 8277 8278 if (act->dta_kind == DTRACEACT_DIFEXPR) { 8279 ASSERT(act->dta_difo != NULL); 8280 8281 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 8282 agg->dtag_hasarg = 1; 8283 } 8284 8285 /* 8286 * We need to allocate an id for this aggregation. 8287 */ 8288 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 8289 VM_BESTFIT | VM_SLEEP); 8290 8291 if (aggid - 1 >= state->dts_naggregations) { 8292 dtrace_aggregation_t **oaggs = state->dts_aggregations; 8293 dtrace_aggregation_t **aggs; 8294 int naggs = state->dts_naggregations << 1; 8295 int onaggs = state->dts_naggregations; 8296 8297 ASSERT(aggid == state->dts_naggregations + 1); 8298 8299 if (naggs == 0) { 8300 ASSERT(oaggs == NULL); 8301 naggs = 1; 8302 } 8303 8304 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 8305 8306 if (oaggs != NULL) { 8307 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 8308 kmem_free(oaggs, onaggs * sizeof (*aggs)); 8309 } 8310 8311 state->dts_aggregations = aggs; 8312 state->dts_naggregations = naggs; 8313 } 8314 8315 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 8316 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 8317 8318 frec = &agg->dtag_first->dta_rec; 8319 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 8320 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 8321 8322 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 8323 ASSERT(!act->dta_intuple); 8324 act->dta_intuple = 1; 8325 } 8326 8327 return (&agg->dtag_action); 8328 } 8329 8330 static void 8331 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 8332 { 8333 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 8334 dtrace_state_t *state = ecb->dte_state; 8335 dtrace_aggid_t aggid = agg->dtag_id; 8336 8337 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 8338 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 8339 8340 ASSERT(state->dts_aggregations[aggid - 1] == agg); 8341 state->dts_aggregations[aggid - 1] = NULL; 8342 8343 kmem_free(agg, sizeof (dtrace_aggregation_t)); 8344 } 8345 8346 static int 8347 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 8348 { 8349 dtrace_action_t *action, *last; 8350 dtrace_difo_t *dp = desc->dtad_difo; 8351 uint32_t size = 0, align = sizeof (uint8_t), mask; 8352 uint16_t format = 0; 8353 dtrace_recdesc_t *rec; 8354 dtrace_state_t *state = ecb->dte_state; 8355 dtrace_optval_t *opt = state->dts_options, nframes, strsize; 8356 uint64_t arg = desc->dtad_arg; 8357 8358 ASSERT(MUTEX_HELD(&dtrace_lock)); 8359 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 8360 8361 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 8362 /* 8363 * If this is an aggregating action, there must be neither 8364 * a speculate nor a commit on the action chain. 8365 */ 8366 dtrace_action_t *act; 8367 8368 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 8369 if (act->dta_kind == DTRACEACT_COMMIT) 8370 return (EINVAL); 8371 8372 if (act->dta_kind == DTRACEACT_SPECULATE) 8373 return (EINVAL); 8374 } 8375 8376 action = dtrace_ecb_aggregation_create(ecb, desc); 8377 8378 if (action == NULL) 8379 return (EINVAL); 8380 } else { 8381 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 8382 (desc->dtad_kind == DTRACEACT_DIFEXPR && 8383 dp != NULL && dp->dtdo_destructive)) { 8384 state->dts_destructive = 1; 8385 } 8386 8387 switch (desc->dtad_kind) { 8388 case DTRACEACT_PRINTF: 8389 case DTRACEACT_PRINTA: 8390 case DTRACEACT_SYSTEM: 8391 case DTRACEACT_FREOPEN: 8392 /* 8393 * We know that our arg is a string -- turn it into a 8394 * format. 8395 */ 8396 if (arg == NULL) { 8397 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); 8398 format = 0; 8399 } else { 8400 ASSERT(arg != NULL); 8401 ASSERT(arg > KERNELBASE); 8402 format = dtrace_format_add(state, 8403 (char *)(uintptr_t)arg); 8404 } 8405 8406 /*FALLTHROUGH*/ 8407 case DTRACEACT_LIBACT: 8408 case DTRACEACT_DIFEXPR: 8409 if (dp == NULL) 8410 return (EINVAL); 8411 8412 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 8413 break; 8414 8415 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 8416 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8417 return (EINVAL); 8418 8419 size = opt[DTRACEOPT_STRSIZE]; 8420 } 8421 8422 break; 8423 8424 case DTRACEACT_STACK: 8425 if ((nframes = arg) == 0) { 8426 nframes = opt[DTRACEOPT_STACKFRAMES]; 8427 ASSERT(nframes > 0); 8428 arg = nframes; 8429 } 8430 8431 size = nframes * sizeof (pc_t); 8432 break; 8433 8434 case DTRACEACT_JSTACK: 8435 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 8436 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 8437 8438 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 8439 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 8440 8441 arg = DTRACE_USTACK_ARG(nframes, strsize); 8442 8443 /*FALLTHROUGH*/ 8444 case DTRACEACT_USTACK: 8445 if (desc->dtad_kind != DTRACEACT_JSTACK && 8446 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 8447 strsize = DTRACE_USTACK_STRSIZE(arg); 8448 nframes = opt[DTRACEOPT_USTACKFRAMES]; 8449 ASSERT(nframes > 0); 8450 arg = DTRACE_USTACK_ARG(nframes, strsize); 8451 } 8452 8453 /* 8454 * Save a slot for the pid. 8455 */ 8456 size = (nframes + 1) * sizeof (uint64_t); 8457 size += DTRACE_USTACK_STRSIZE(arg); 8458 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 8459 8460 break; 8461 8462 case DTRACEACT_SYM: 8463 case DTRACEACT_MOD: 8464 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 8465 sizeof (uint64_t)) || 8466 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8467 return (EINVAL); 8468 break; 8469 8470 case DTRACEACT_USYM: 8471 case DTRACEACT_UMOD: 8472 case DTRACEACT_UADDR: 8473 if (dp == NULL || 8474 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 8475 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8476 return (EINVAL); 8477 8478 /* 8479 * We have a slot for the pid, plus a slot for the 8480 * argument. To keep things simple (aligned with 8481 * bitness-neutral sizing), we store each as a 64-bit 8482 * quantity. 8483 */ 8484 size = 2 * sizeof (uint64_t); 8485 break; 8486 8487 case DTRACEACT_STOP: 8488 case DTRACEACT_BREAKPOINT: 8489 case DTRACEACT_PANIC: 8490 break; 8491 8492 case DTRACEACT_CHILL: 8493 case DTRACEACT_DISCARD: 8494 case DTRACEACT_RAISE: 8495 if (dp == NULL) 8496 return (EINVAL); 8497 break; 8498 8499 case DTRACEACT_EXIT: 8500 if (dp == NULL || 8501 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 8502 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8503 return (EINVAL); 8504 break; 8505 8506 case DTRACEACT_SPECULATE: 8507 if (ecb->dte_size > sizeof (dtrace_epid_t)) 8508 return (EINVAL); 8509 8510 if (dp == NULL) 8511 return (EINVAL); 8512 8513 state->dts_speculates = 1; 8514 break; 8515 8516 case DTRACEACT_COMMIT: { 8517 dtrace_action_t *act = ecb->dte_action; 8518 8519 for (; act != NULL; act = act->dta_next) { 8520 if (act->dta_kind == DTRACEACT_COMMIT) 8521 return (EINVAL); 8522 } 8523 8524 if (dp == NULL) 8525 return (EINVAL); 8526 break; 8527 } 8528 8529 default: 8530 return (EINVAL); 8531 } 8532 8533 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 8534 /* 8535 * If this is a data-storing action or a speculate, 8536 * we must be sure that there isn't a commit on the 8537 * action chain. 8538 */ 8539 dtrace_action_t *act = ecb->dte_action; 8540 8541 for (; act != NULL; act = act->dta_next) { 8542 if (act->dta_kind == DTRACEACT_COMMIT) 8543 return (EINVAL); 8544 } 8545 } 8546 8547 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 8548 action->dta_rec.dtrd_size = size; 8549 } 8550 8551 action->dta_refcnt = 1; 8552 rec = &action->dta_rec; 8553 size = rec->dtrd_size; 8554 8555 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 8556 if (!(size & mask)) { 8557 align = mask + 1; 8558 break; 8559 } 8560 } 8561 8562 action->dta_kind = desc->dtad_kind; 8563 8564 if ((action->dta_difo = dp) != NULL) 8565 dtrace_difo_hold(dp); 8566 8567 rec->dtrd_action = action->dta_kind; 8568 rec->dtrd_arg = arg; 8569 8570 if (ecb->dte_state == dtrace_anon.dta_state) { 8571 /* 8572 * If this is an anonymous enabling, explicitly clear the uarg. 8573 */ 8574 rec->dtrd_uarg = 0; 8575 } else { 8576 rec->dtrd_uarg = desc->dtad_uarg; 8577 } 8578 8579 rec->dtrd_alignment = (uint16_t)align; 8580 rec->dtrd_format = format; 8581 8582 if ((last = ecb->dte_action_last) != NULL) { 8583 ASSERT(ecb->dte_action != NULL); 8584 action->dta_prev = last; 8585 last->dta_next = action; 8586 } else { 8587 ASSERT(ecb->dte_action == NULL); 8588 ecb->dte_action = action; 8589 } 8590 8591 ecb->dte_action_last = action; 8592 8593 return (0); 8594 } 8595 8596 static void 8597 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 8598 { 8599 dtrace_action_t *act = ecb->dte_action, *next; 8600 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 8601 dtrace_difo_t *dp; 8602 uint16_t format; 8603 8604 if (act != NULL && act->dta_refcnt > 1) { 8605 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 8606 act->dta_refcnt--; 8607 } else { 8608 for (; act != NULL; act = next) { 8609 next = act->dta_next; 8610 ASSERT(next != NULL || act == ecb->dte_action_last); 8611 ASSERT(act->dta_refcnt == 1); 8612 8613 if ((format = act->dta_rec.dtrd_format) != 0) 8614 dtrace_format_remove(ecb->dte_state, format); 8615 8616 if ((dp = act->dta_difo) != NULL) 8617 dtrace_difo_release(dp, vstate); 8618 8619 if (DTRACEACT_ISAGG(act->dta_kind)) { 8620 dtrace_ecb_aggregation_destroy(ecb, act); 8621 } else { 8622 kmem_free(act, sizeof (dtrace_action_t)); 8623 } 8624 } 8625 } 8626 8627 ecb->dte_action = NULL; 8628 ecb->dte_action_last = NULL; 8629 ecb->dte_size = sizeof (dtrace_epid_t); 8630 } 8631 8632 static void 8633 dtrace_ecb_disable(dtrace_ecb_t *ecb) 8634 { 8635 /* 8636 * We disable the ECB by removing it from its probe. 8637 */ 8638 dtrace_ecb_t *pecb, *prev = NULL; 8639 dtrace_probe_t *probe = ecb->dte_probe; 8640 8641 ASSERT(MUTEX_HELD(&dtrace_lock)); 8642 8643 if (probe == NULL) { 8644 /* 8645 * This is the NULL probe; there is nothing to disable. 8646 */ 8647 return; 8648 } 8649 8650 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 8651 if (pecb == ecb) 8652 break; 8653 prev = pecb; 8654 } 8655 8656 ASSERT(pecb != NULL); 8657 8658 if (prev == NULL) { 8659 probe->dtpr_ecb = ecb->dte_next; 8660 } else { 8661 prev->dte_next = ecb->dte_next; 8662 } 8663 8664 if (ecb == probe->dtpr_ecb_last) { 8665 ASSERT(ecb->dte_next == NULL); 8666 probe->dtpr_ecb_last = prev; 8667 } 8668 8669 /* 8670 * The ECB has been disconnected from the probe; now sync to assure 8671 * that all CPUs have seen the change before returning. 8672 */ 8673 dtrace_sync(); 8674 8675 if (probe->dtpr_ecb == NULL) { 8676 /* 8677 * That was the last ECB on the probe; clear the predicate 8678 * cache ID for the probe, disable it and sync one more time 8679 * to assure that we'll never hit it again. 8680 */ 8681 dtrace_provider_t *prov = probe->dtpr_provider; 8682 8683 ASSERT(ecb->dte_next == NULL); 8684 ASSERT(probe->dtpr_ecb_last == NULL); 8685 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 8686 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 8687 probe->dtpr_id, probe->dtpr_arg); 8688 dtrace_sync(); 8689 } else { 8690 /* 8691 * There is at least one ECB remaining on the probe. If there 8692 * is _exactly_ one, set the probe's predicate cache ID to be 8693 * the predicate cache ID of the remaining ECB. 8694 */ 8695 ASSERT(probe->dtpr_ecb_last != NULL); 8696 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 8697 8698 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 8699 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 8700 8701 ASSERT(probe->dtpr_ecb->dte_next == NULL); 8702 8703 if (p != NULL) 8704 probe->dtpr_predcache = p->dtp_cacheid; 8705 } 8706 8707 ecb->dte_next = NULL; 8708 } 8709 } 8710 8711 static void 8712 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 8713 { 8714 dtrace_state_t *state = ecb->dte_state; 8715 dtrace_vstate_t *vstate = &state->dts_vstate; 8716 dtrace_predicate_t *pred; 8717 dtrace_epid_t epid = ecb->dte_epid; 8718 8719 ASSERT(MUTEX_HELD(&dtrace_lock)); 8720 ASSERT(ecb->dte_next == NULL); 8721 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 8722 8723 if ((pred = ecb->dte_predicate) != NULL) 8724 dtrace_predicate_release(pred, vstate); 8725 8726 dtrace_ecb_action_remove(ecb); 8727 8728 ASSERT(state->dts_ecbs[epid - 1] == ecb); 8729 state->dts_ecbs[epid - 1] = NULL; 8730 8731 kmem_free(ecb, sizeof (dtrace_ecb_t)); 8732 } 8733 8734 static dtrace_ecb_t * 8735 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 8736 dtrace_enabling_t *enab) 8737 { 8738 dtrace_ecb_t *ecb; 8739 dtrace_predicate_t *pred; 8740 dtrace_actdesc_t *act; 8741 dtrace_provider_t *prov; 8742 dtrace_ecbdesc_t *desc = enab->dten_current; 8743 8744 ASSERT(MUTEX_HELD(&dtrace_lock)); 8745 ASSERT(state != NULL); 8746 8747 ecb = dtrace_ecb_add(state, probe); 8748 ecb->dte_uarg = desc->dted_uarg; 8749 8750 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 8751 dtrace_predicate_hold(pred); 8752 ecb->dte_predicate = pred; 8753 } 8754 8755 if (probe != NULL) { 8756 /* 8757 * If the provider shows more leg than the consumer is old 8758 * enough to see, we need to enable the appropriate implicit 8759 * predicate bits to prevent the ecb from activating at 8760 * revealing times. 8761 */ 8762 prov = probe->dtpr_provider; 8763 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 8764 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 8765 ecb->dte_cond |= DTRACE_COND_OWNER; 8766 8767 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 8768 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 8769 ecb->dte_cond |= DTRACE_COND_USERMODE; 8770 } 8771 8772 if (dtrace_ecb_create_cache != NULL) { 8773 /* 8774 * If we have a cached ecb, we'll use its action list instead 8775 * of creating our own (saving both time and space). 8776 */ 8777 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 8778 dtrace_action_t *act = cached->dte_action; 8779 8780 if (act != NULL) { 8781 ASSERT(act->dta_refcnt > 0); 8782 act->dta_refcnt++; 8783 ecb->dte_action = act; 8784 ecb->dte_action_last = cached->dte_action_last; 8785 ecb->dte_needed = cached->dte_needed; 8786 ecb->dte_size = cached->dte_size; 8787 ecb->dte_alignment = cached->dte_alignment; 8788 } 8789 8790 return (ecb); 8791 } 8792 8793 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 8794 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 8795 dtrace_ecb_destroy(ecb); 8796 return (NULL); 8797 } 8798 } 8799 8800 dtrace_ecb_resize(ecb); 8801 8802 return (dtrace_ecb_create_cache = ecb); 8803 } 8804 8805 static int 8806 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 8807 { 8808 dtrace_ecb_t *ecb; 8809 dtrace_enabling_t *enab = arg; 8810 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 8811 8812 ASSERT(state != NULL); 8813 8814 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 8815 /* 8816 * This probe was created in a generation for which this 8817 * enabling has previously created ECBs; we don't want to 8818 * enable it again, so just kick out. 8819 */ 8820 return (DTRACE_MATCH_NEXT); 8821 } 8822 8823 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 8824 return (DTRACE_MATCH_DONE); 8825 8826 dtrace_ecb_enable(ecb); 8827 return (DTRACE_MATCH_NEXT); 8828 } 8829 8830 static dtrace_ecb_t * 8831 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 8832 { 8833 dtrace_ecb_t *ecb; 8834 8835 ASSERT(MUTEX_HELD(&dtrace_lock)); 8836 8837 if (id == 0 || id > state->dts_necbs) 8838 return (NULL); 8839 8840 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 8841 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 8842 8843 return (state->dts_ecbs[id - 1]); 8844 } 8845 8846 static dtrace_aggregation_t * 8847 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 8848 { 8849 dtrace_aggregation_t *agg; 8850 8851 ASSERT(MUTEX_HELD(&dtrace_lock)); 8852 8853 if (id == 0 || id > state->dts_naggregations) 8854 return (NULL); 8855 8856 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 8857 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 8858 agg->dtag_id == id); 8859 8860 return (state->dts_aggregations[id - 1]); 8861 } 8862 8863 /* 8864 * DTrace Buffer Functions 8865 * 8866 * The following functions manipulate DTrace buffers. Most of these functions 8867 * are called in the context of establishing or processing consumer state; 8868 * exceptions are explicitly noted. 8869 */ 8870 8871 /* 8872 * Note: called from cross call context. This function switches the two 8873 * buffers on a given CPU. The atomicity of this operation is assured by 8874 * disabling interrupts while the actual switch takes place; the disabling of 8875 * interrupts serializes the execution with any execution of dtrace_probe() on 8876 * the same CPU. 8877 */ 8878 static void 8879 dtrace_buffer_switch(dtrace_buffer_t *buf) 8880 { 8881 caddr_t tomax = buf->dtb_tomax; 8882 caddr_t xamot = buf->dtb_xamot; 8883 dtrace_icookie_t cookie; 8884 8885 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 8886 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 8887 8888 cookie = dtrace_interrupt_disable(); 8889 buf->dtb_tomax = xamot; 8890 buf->dtb_xamot = tomax; 8891 buf->dtb_xamot_drops = buf->dtb_drops; 8892 buf->dtb_xamot_offset = buf->dtb_offset; 8893 buf->dtb_xamot_errors = buf->dtb_errors; 8894 buf->dtb_xamot_flags = buf->dtb_flags; 8895 buf->dtb_offset = 0; 8896 buf->dtb_drops = 0; 8897 buf->dtb_errors = 0; 8898 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 8899 dtrace_interrupt_enable(cookie); 8900 } 8901 8902 /* 8903 * Note: called from cross call context. This function activates a buffer 8904 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 8905 * is guaranteed by the disabling of interrupts. 8906 */ 8907 static void 8908 dtrace_buffer_activate(dtrace_state_t *state) 8909 { 8910 dtrace_buffer_t *buf; 8911 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 8912 8913 buf = &state->dts_buffer[CPU->cpu_id]; 8914 8915 if (buf->dtb_tomax != NULL) { 8916 /* 8917 * We might like to assert that the buffer is marked inactive, 8918 * but this isn't necessarily true: the buffer for the CPU 8919 * that processes the BEGIN probe has its buffer activated 8920 * manually. In this case, we take the (harmless) action 8921 * re-clearing the bit INACTIVE bit. 8922 */ 8923 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 8924 } 8925 8926 dtrace_interrupt_enable(cookie); 8927 } 8928 8929 static int 8930 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 8931 processorid_t cpu) 8932 { 8933 cpu_t *cp; 8934 dtrace_buffer_t *buf; 8935 8936 ASSERT(MUTEX_HELD(&cpu_lock)); 8937 ASSERT(MUTEX_HELD(&dtrace_lock)); 8938 8939 if (crgetuid(CRED()) != 0 && size > dtrace_nonroot_maxsize) 8940 return (EFBIG); 8941 8942 cp = cpu_list; 8943 8944 do { 8945 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 8946 continue; 8947 8948 buf = &bufs[cp->cpu_id]; 8949 8950 /* 8951 * If there is already a buffer allocated for this CPU, it 8952 * is only possible that this is a DR event. In this case, 8953 * the buffer size must match our specified size. 8954 */ 8955 if (buf->dtb_tomax != NULL) { 8956 ASSERT(buf->dtb_size == size); 8957 continue; 8958 } 8959 8960 ASSERT(buf->dtb_xamot == NULL); 8961 8962 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 8963 goto err; 8964 8965 buf->dtb_size = size; 8966 buf->dtb_flags = flags; 8967 buf->dtb_offset = 0; 8968 buf->dtb_drops = 0; 8969 8970 if (flags & DTRACEBUF_NOSWITCH) 8971 continue; 8972 8973 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 8974 goto err; 8975 } while ((cp = cp->cpu_next) != cpu_list); 8976 8977 return (0); 8978 8979 err: 8980 cp = cpu_list; 8981 8982 do { 8983 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 8984 continue; 8985 8986 buf = &bufs[cp->cpu_id]; 8987 8988 if (buf->dtb_xamot != NULL) { 8989 ASSERT(buf->dtb_tomax != NULL); 8990 ASSERT(buf->dtb_size == size); 8991 kmem_free(buf->dtb_xamot, size); 8992 } 8993 8994 if (buf->dtb_tomax != NULL) { 8995 ASSERT(buf->dtb_size == size); 8996 kmem_free(buf->dtb_tomax, size); 8997 } 8998 8999 buf->dtb_tomax = NULL; 9000 buf->dtb_xamot = NULL; 9001 buf->dtb_size = 0; 9002 } while ((cp = cp->cpu_next) != cpu_list); 9003 9004 return (ENOMEM); 9005 } 9006 9007 /* 9008 * Note: called from probe context. This function just increments the drop 9009 * count on a buffer. It has been made a function to allow for the 9010 * possibility of understanding the source of mysterious drop counts. (A 9011 * problem for which one may be particularly disappointed that DTrace cannot 9012 * be used to understand DTrace.) 9013 */ 9014 static void 9015 dtrace_buffer_drop(dtrace_buffer_t *buf) 9016 { 9017 buf->dtb_drops++; 9018 } 9019 9020 /* 9021 * Note: called from probe context. This function is called to reserve space 9022 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 9023 * mstate. Returns the new offset in the buffer, or a negative value if an 9024 * error has occurred. 9025 */ 9026 static intptr_t 9027 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 9028 dtrace_state_t *state, dtrace_mstate_t *mstate) 9029 { 9030 intptr_t offs = buf->dtb_offset, soffs; 9031 intptr_t woffs; 9032 caddr_t tomax; 9033 size_t total; 9034 9035 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 9036 return (-1); 9037 9038 if ((tomax = buf->dtb_tomax) == NULL) { 9039 dtrace_buffer_drop(buf); 9040 return (-1); 9041 } 9042 9043 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 9044 while (offs & (align - 1)) { 9045 /* 9046 * Assert that our alignment is off by a number which 9047 * is itself sizeof (uint32_t) aligned. 9048 */ 9049 ASSERT(!((align - (offs & (align - 1))) & 9050 (sizeof (uint32_t) - 1))); 9051 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 9052 offs += sizeof (uint32_t); 9053 } 9054 9055 if ((soffs = offs + needed) > buf->dtb_size) { 9056 dtrace_buffer_drop(buf); 9057 return (-1); 9058 } 9059 9060 if (mstate == NULL) 9061 return (offs); 9062 9063 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 9064 mstate->dtms_scratch_size = buf->dtb_size - soffs; 9065 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 9066 9067 return (offs); 9068 } 9069 9070 if (buf->dtb_flags & DTRACEBUF_FILL) { 9071 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 9072 (buf->dtb_flags & DTRACEBUF_FULL)) 9073 return (-1); 9074 goto out; 9075 } 9076 9077 total = needed + (offs & (align - 1)); 9078 9079 /* 9080 * For a ring buffer, life is quite a bit more complicated. Before 9081 * we can store any padding, we need to adjust our wrapping offset. 9082 * (If we've never before wrapped or we're not about to, no adjustment 9083 * is required.) 9084 */ 9085 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 9086 offs + total > buf->dtb_size) { 9087 woffs = buf->dtb_xamot_offset; 9088 9089 if (offs + total > buf->dtb_size) { 9090 /* 9091 * We can't fit in the end of the buffer. First, a 9092 * sanity check that we can fit in the buffer at all. 9093 */ 9094 if (total > buf->dtb_size) { 9095 dtrace_buffer_drop(buf); 9096 return (-1); 9097 } 9098 9099 /* 9100 * We're going to be storing at the top of the buffer, 9101 * so now we need to deal with the wrapped offset. We 9102 * only reset our wrapped offset to 0 if it is 9103 * currently greater than the current offset. If it 9104 * is less than the current offset, it is because a 9105 * previous allocation induced a wrap -- but the 9106 * allocation didn't subsequently take the space due 9107 * to an error or false predicate evaluation. In this 9108 * case, we'll just leave the wrapped offset alone: if 9109 * the wrapped offset hasn't been advanced far enough 9110 * for this allocation, it will be adjusted in the 9111 * lower loop. 9112 */ 9113 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 9114 if (woffs >= offs) 9115 woffs = 0; 9116 } else { 9117 woffs = 0; 9118 } 9119 9120 /* 9121 * Now we know that we're going to be storing to the 9122 * top of the buffer and that there is room for us 9123 * there. We need to clear the buffer from the current 9124 * offset to the end (there may be old gunk there). 9125 */ 9126 while (offs < buf->dtb_size) 9127 tomax[offs++] = 0; 9128 9129 /* 9130 * We need to set our offset to zero. And because we 9131 * are wrapping, we need to set the bit indicating as 9132 * much. We can also adjust our needed space back 9133 * down to the space required by the ECB -- we know 9134 * that the top of the buffer is aligned. 9135 */ 9136 offs = 0; 9137 total = needed; 9138 buf->dtb_flags |= DTRACEBUF_WRAPPED; 9139 } else { 9140 /* 9141 * There is room for us in the buffer, so we simply 9142 * need to check the wrapped offset. 9143 */ 9144 if (woffs < offs) { 9145 /* 9146 * The wrapped offset is less than the offset. 9147 * This can happen if we allocated buffer space 9148 * that induced a wrap, but then we didn't 9149 * subsequently take the space due to an error 9150 * or false predicate evaluation. This is 9151 * okay; we know that _this_ allocation isn't 9152 * going to induce a wrap. We still can't 9153 * reset the wrapped offset to be zero, 9154 * however: the space may have been trashed in 9155 * the previous failed probe attempt. But at 9156 * least the wrapped offset doesn't need to 9157 * be adjusted at all... 9158 */ 9159 goto out; 9160 } 9161 } 9162 9163 while (offs + total > woffs) { 9164 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 9165 size_t size; 9166 9167 if (epid == DTRACE_EPIDNONE) { 9168 size = sizeof (uint32_t); 9169 } else { 9170 ASSERT(epid <= state->dts_necbs); 9171 ASSERT(state->dts_ecbs[epid - 1] != NULL); 9172 9173 size = state->dts_ecbs[epid - 1]->dte_size; 9174 } 9175 9176 ASSERT(woffs + size <= buf->dtb_size); 9177 ASSERT(size != 0); 9178 9179 if (woffs + size == buf->dtb_size) { 9180 /* 9181 * We've reached the end of the buffer; we want 9182 * to set the wrapped offset to 0 and break 9183 * out. However, if the offs is 0, then we're 9184 * in a strange edge-condition: the amount of 9185 * space that we want to reserve plus the size 9186 * of the record that we're overwriting is 9187 * greater than the size of the buffer. This 9188 * is problematic because if we reserve the 9189 * space but subsequently don't consume it (due 9190 * to a failed predicate or error) the wrapped 9191 * offset will be 0 -- yet the EPID at offset 0 9192 * will not be committed. This situation is 9193 * relatively easy to deal with: if we're in 9194 * this case, the buffer is indistinguishable 9195 * from one that hasn't wrapped; we need only 9196 * finish the job by clearing the wrapped bit, 9197 * explicitly setting the offset to be 0, and 9198 * zero'ing out the old data in the buffer. 9199 */ 9200 if (offs == 0) { 9201 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 9202 buf->dtb_offset = 0; 9203 woffs = total; 9204 9205 while (woffs < buf->dtb_size) 9206 tomax[woffs++] = 0; 9207 } 9208 9209 woffs = 0; 9210 break; 9211 } 9212 9213 woffs += size; 9214 } 9215 9216 /* 9217 * We have a wrapped offset. It may be that the wrapped offset 9218 * has become zero -- that's okay. 9219 */ 9220 buf->dtb_xamot_offset = woffs; 9221 } 9222 9223 out: 9224 /* 9225 * Now we can plow the buffer with any necessary padding. 9226 */ 9227 while (offs & (align - 1)) { 9228 /* 9229 * Assert that our alignment is off by a number which 9230 * is itself sizeof (uint32_t) aligned. 9231 */ 9232 ASSERT(!((align - (offs & (align - 1))) & 9233 (sizeof (uint32_t) - 1))); 9234 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 9235 offs += sizeof (uint32_t); 9236 } 9237 9238 if (buf->dtb_flags & DTRACEBUF_FILL) { 9239 if (offs + needed > buf->dtb_size - state->dts_reserve) { 9240 buf->dtb_flags |= DTRACEBUF_FULL; 9241 return (-1); 9242 } 9243 } 9244 9245 if (mstate == NULL) 9246 return (offs); 9247 9248 /* 9249 * For ring buffers and fill buffers, the scratch space is always 9250 * the inactive buffer. 9251 */ 9252 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 9253 mstate->dtms_scratch_size = buf->dtb_size; 9254 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 9255 9256 return (offs); 9257 } 9258 9259 static void 9260 dtrace_buffer_polish(dtrace_buffer_t *buf) 9261 { 9262 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 9263 ASSERT(MUTEX_HELD(&dtrace_lock)); 9264 9265 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 9266 return; 9267 9268 /* 9269 * We need to polish the ring buffer. There are three cases: 9270 * 9271 * - The first (and presumably most common) is that there is no gap 9272 * between the buffer offset and the wrapped offset. In this case, 9273 * there is nothing in the buffer that isn't valid data; we can 9274 * mark the buffer as polished and return. 9275 * 9276 * - The second (less common than the first but still more common 9277 * than the third) is that there is a gap between the buffer offset 9278 * and the wrapped offset, and the wrapped offset is larger than the 9279 * buffer offset. This can happen because of an alignment issue, or 9280 * can happen because of a call to dtrace_buffer_reserve() that 9281 * didn't subsequently consume the buffer space. In this case, 9282 * we need to zero the data from the buffer offset to the wrapped 9283 * offset. 9284 * 9285 * - The third (and least common) is that there is a gap between the 9286 * buffer offset and the wrapped offset, but the wrapped offset is 9287 * _less_ than the buffer offset. This can only happen because a 9288 * call to dtrace_buffer_reserve() induced a wrap, but the space 9289 * was not subsequently consumed. In this case, we need to zero the 9290 * space from the offset to the end of the buffer _and_ from the 9291 * top of the buffer to the wrapped offset. 9292 */ 9293 if (buf->dtb_offset < buf->dtb_xamot_offset) { 9294 bzero(buf->dtb_tomax + buf->dtb_offset, 9295 buf->dtb_xamot_offset - buf->dtb_offset); 9296 } 9297 9298 if (buf->dtb_offset > buf->dtb_xamot_offset) { 9299 bzero(buf->dtb_tomax + buf->dtb_offset, 9300 buf->dtb_size - buf->dtb_offset); 9301 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 9302 } 9303 } 9304 9305 static void 9306 dtrace_buffer_free(dtrace_buffer_t *bufs) 9307 { 9308 int i; 9309 9310 for (i = 0; i < NCPU; i++) { 9311 dtrace_buffer_t *buf = &bufs[i]; 9312 9313 if (buf->dtb_tomax == NULL) { 9314 ASSERT(buf->dtb_xamot == NULL); 9315 ASSERT(buf->dtb_size == 0); 9316 continue; 9317 } 9318 9319 if (buf->dtb_xamot != NULL) { 9320 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 9321 kmem_free(buf->dtb_xamot, buf->dtb_size); 9322 } 9323 9324 kmem_free(buf->dtb_tomax, buf->dtb_size); 9325 buf->dtb_size = 0; 9326 buf->dtb_tomax = NULL; 9327 buf->dtb_xamot = NULL; 9328 } 9329 } 9330 9331 /* 9332 * DTrace Enabling Functions 9333 */ 9334 static dtrace_enabling_t * 9335 dtrace_enabling_create(dtrace_vstate_t *vstate) 9336 { 9337 dtrace_enabling_t *enab; 9338 9339 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 9340 enab->dten_vstate = vstate; 9341 9342 return (enab); 9343 } 9344 9345 static void 9346 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 9347 { 9348 dtrace_ecbdesc_t **ndesc; 9349 size_t osize, nsize; 9350 9351 /* 9352 * We can't add to enablings after we've enabled them, or after we've 9353 * retained them. 9354 */ 9355 ASSERT(enab->dten_probegen == 0); 9356 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 9357 9358 if (enab->dten_ndesc < enab->dten_maxdesc) { 9359 enab->dten_desc[enab->dten_ndesc++] = ecb; 9360 return; 9361 } 9362 9363 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 9364 9365 if (enab->dten_maxdesc == 0) { 9366 enab->dten_maxdesc = 1; 9367 } else { 9368 enab->dten_maxdesc <<= 1; 9369 } 9370 9371 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 9372 9373 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 9374 ndesc = kmem_zalloc(nsize, KM_SLEEP); 9375 bcopy(enab->dten_desc, ndesc, osize); 9376 kmem_free(enab->dten_desc, osize); 9377 9378 enab->dten_desc = ndesc; 9379 enab->dten_desc[enab->dten_ndesc++] = ecb; 9380 } 9381 9382 static void 9383 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 9384 dtrace_probedesc_t *pd) 9385 { 9386 dtrace_ecbdesc_t *new; 9387 dtrace_predicate_t *pred; 9388 dtrace_actdesc_t *act; 9389 9390 /* 9391 * We're going to create a new ECB description that matches the 9392 * specified ECB in every way, but has the specified probe description. 9393 */ 9394 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 9395 9396 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 9397 dtrace_predicate_hold(pred); 9398 9399 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 9400 dtrace_actdesc_hold(act); 9401 9402 new->dted_action = ecb->dted_action; 9403 new->dted_pred = ecb->dted_pred; 9404 new->dted_probe = *pd; 9405 new->dted_uarg = ecb->dted_uarg; 9406 9407 dtrace_enabling_add(enab, new); 9408 } 9409 9410 static void 9411 dtrace_enabling_dump(dtrace_enabling_t *enab) 9412 { 9413 int i; 9414 9415 for (i = 0; i < enab->dten_ndesc; i++) { 9416 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 9417 9418 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 9419 desc->dtpd_provider, desc->dtpd_mod, 9420 desc->dtpd_func, desc->dtpd_name); 9421 } 9422 } 9423 9424 static void 9425 dtrace_enabling_destroy(dtrace_enabling_t *enab) 9426 { 9427 int i; 9428 dtrace_ecbdesc_t *ep; 9429 dtrace_vstate_t *vstate = enab->dten_vstate; 9430 9431 ASSERT(MUTEX_HELD(&dtrace_lock)); 9432 9433 for (i = 0; i < enab->dten_ndesc; i++) { 9434 dtrace_actdesc_t *act, *next; 9435 dtrace_predicate_t *pred; 9436 9437 ep = enab->dten_desc[i]; 9438 9439 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 9440 dtrace_predicate_release(pred, vstate); 9441 9442 for (act = ep->dted_action; act != NULL; act = next) { 9443 next = act->dtad_next; 9444 dtrace_actdesc_release(act, vstate); 9445 } 9446 9447 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 9448 } 9449 9450 kmem_free(enab->dten_desc, 9451 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 9452 9453 /* 9454 * If this was a retained enabling, decrement the dts_nretained count 9455 * and take it off of the dtrace_retained list. 9456 */ 9457 if (enab->dten_prev != NULL || enab->dten_next != NULL || 9458 dtrace_retained == enab) { 9459 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9460 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 9461 enab->dten_vstate->dtvs_state->dts_nretained--; 9462 } 9463 9464 if (enab->dten_prev == NULL) { 9465 if (dtrace_retained == enab) { 9466 dtrace_retained = enab->dten_next; 9467 9468 if (dtrace_retained != NULL) 9469 dtrace_retained->dten_prev = NULL; 9470 } 9471 } else { 9472 ASSERT(enab != dtrace_retained); 9473 ASSERT(dtrace_retained != NULL); 9474 enab->dten_prev->dten_next = enab->dten_next; 9475 } 9476 9477 if (enab->dten_next != NULL) { 9478 ASSERT(dtrace_retained != NULL); 9479 enab->dten_next->dten_prev = enab->dten_prev; 9480 } 9481 9482 kmem_free(enab, sizeof (dtrace_enabling_t)); 9483 } 9484 9485 static int 9486 dtrace_enabling_retain(dtrace_enabling_t *enab) 9487 { 9488 dtrace_state_t *state; 9489 9490 ASSERT(MUTEX_HELD(&dtrace_lock)); 9491 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 9492 ASSERT(enab->dten_vstate != NULL); 9493 9494 state = enab->dten_vstate->dtvs_state; 9495 ASSERT(state != NULL); 9496 9497 /* 9498 * We only allow each state to retain dtrace_retain_max enablings. 9499 */ 9500 if (state->dts_nretained >= dtrace_retain_max) 9501 return (ENOSPC); 9502 9503 state->dts_nretained++; 9504 9505 if (dtrace_retained == NULL) { 9506 dtrace_retained = enab; 9507 return (0); 9508 } 9509 9510 enab->dten_next = dtrace_retained; 9511 dtrace_retained->dten_prev = enab; 9512 dtrace_retained = enab; 9513 9514 return (0); 9515 } 9516 9517 static int 9518 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 9519 dtrace_probedesc_t *create) 9520 { 9521 dtrace_enabling_t *new, *enab; 9522 int found = 0, err = ENOENT; 9523 9524 ASSERT(MUTEX_HELD(&dtrace_lock)); 9525 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 9526 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 9527 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 9528 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 9529 9530 new = dtrace_enabling_create(&state->dts_vstate); 9531 9532 /* 9533 * Iterate over all retained enablings, looking for enablings that 9534 * match the specified state. 9535 */ 9536 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9537 int i; 9538 9539 /* 9540 * dtvs_state can only be NULL for helper enablings -- and 9541 * helper enablings can't be retained. 9542 */ 9543 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9544 9545 if (enab->dten_vstate->dtvs_state != state) 9546 continue; 9547 9548 /* 9549 * Now iterate over each probe description; we're looking for 9550 * an exact match to the specified probe description. 9551 */ 9552 for (i = 0; i < enab->dten_ndesc; i++) { 9553 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 9554 dtrace_probedesc_t *pd = &ep->dted_probe; 9555 9556 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 9557 continue; 9558 9559 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 9560 continue; 9561 9562 if (strcmp(pd->dtpd_func, match->dtpd_func)) 9563 continue; 9564 9565 if (strcmp(pd->dtpd_name, match->dtpd_name)) 9566 continue; 9567 9568 /* 9569 * We have a winning probe! Add it to our growing 9570 * enabling. 9571 */ 9572 found = 1; 9573 dtrace_enabling_addlike(new, ep, create); 9574 } 9575 } 9576 9577 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 9578 dtrace_enabling_destroy(new); 9579 return (err); 9580 } 9581 9582 return (0); 9583 } 9584 9585 static void 9586 dtrace_enabling_retract(dtrace_state_t *state) 9587 { 9588 dtrace_enabling_t *enab, *next; 9589 9590 ASSERT(MUTEX_HELD(&dtrace_lock)); 9591 9592 /* 9593 * Iterate over all retained enablings, destroy the enablings retained 9594 * for the specified state. 9595 */ 9596 for (enab = dtrace_retained; enab != NULL; enab = next) { 9597 next = enab->dten_next; 9598 9599 /* 9600 * dtvs_state can only be NULL for helper enablings -- and 9601 * helper enablings can't be retained. 9602 */ 9603 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9604 9605 if (enab->dten_vstate->dtvs_state == state) { 9606 ASSERT(state->dts_nretained > 0); 9607 dtrace_enabling_destroy(enab); 9608 } 9609 } 9610 9611 ASSERT(state->dts_nretained == 0); 9612 } 9613 9614 static int 9615 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 9616 { 9617 int i = 0; 9618 int matched = 0; 9619 9620 ASSERT(MUTEX_HELD(&cpu_lock)); 9621 ASSERT(MUTEX_HELD(&dtrace_lock)); 9622 9623 for (i = 0; i < enab->dten_ndesc; i++) { 9624 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 9625 9626 enab->dten_current = ep; 9627 enab->dten_error = 0; 9628 9629 matched += dtrace_probe_enable(&ep->dted_probe, enab); 9630 9631 if (enab->dten_error != 0) { 9632 /* 9633 * If we get an error half-way through enabling the 9634 * probes, we kick out -- perhaps with some number of 9635 * them enabled. Leaving enabled probes enabled may 9636 * be slightly confusing for user-level, but we expect 9637 * that no one will attempt to actually drive on in 9638 * the face of such errors. If this is an anonymous 9639 * enabling (indicated with a NULL nmatched pointer), 9640 * we cmn_err() a message. We aren't expecting to 9641 * get such an error -- such as it can exist at all, 9642 * it would be a result of corrupted DOF in the driver 9643 * properties. 9644 */ 9645 if (nmatched == NULL) { 9646 cmn_err(CE_WARN, "dtrace_enabling_match() " 9647 "error on %p: %d", (void *)ep, 9648 enab->dten_error); 9649 } 9650 9651 return (enab->dten_error); 9652 } 9653 } 9654 9655 enab->dten_probegen = dtrace_probegen; 9656 if (nmatched != NULL) 9657 *nmatched = matched; 9658 9659 return (0); 9660 } 9661 9662 static void 9663 dtrace_enabling_matchall(void) 9664 { 9665 dtrace_enabling_t *enab; 9666 9667 mutex_enter(&cpu_lock); 9668 mutex_enter(&dtrace_lock); 9669 9670 /* 9671 * Because we can be called after dtrace_detach() has been called, we 9672 * cannot assert that there are retained enablings. We can safely 9673 * load from dtrace_retained, however: the taskq_destroy() at the 9674 * end of dtrace_detach() will block pending our completion. 9675 */ 9676 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) 9677 (void) dtrace_enabling_match(enab, NULL); 9678 9679 mutex_exit(&dtrace_lock); 9680 mutex_exit(&cpu_lock); 9681 } 9682 9683 static int 9684 dtrace_enabling_matchstate(dtrace_state_t *state, int *nmatched) 9685 { 9686 dtrace_enabling_t *enab; 9687 int matched, total = 0, err; 9688 9689 ASSERT(MUTEX_HELD(&cpu_lock)); 9690 ASSERT(MUTEX_HELD(&dtrace_lock)); 9691 9692 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9693 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9694 9695 if (enab->dten_vstate->dtvs_state != state) 9696 continue; 9697 9698 if ((err = dtrace_enabling_match(enab, &matched)) != 0) 9699 return (err); 9700 9701 total += matched; 9702 } 9703 9704 if (nmatched != NULL) 9705 *nmatched = total; 9706 9707 return (0); 9708 } 9709 9710 /* 9711 * If an enabling is to be enabled without having matched probes (that is, if 9712 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 9713 * enabling must be _primed_ by creating an ECB for every ECB description. 9714 * This must be done to assure that we know the number of speculations, the 9715 * number of aggregations, the minimum buffer size needed, etc. before we 9716 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 9717 * enabling any probes, we create ECBs for every ECB decription, but with a 9718 * NULL probe -- which is exactly what this function does. 9719 */ 9720 static void 9721 dtrace_enabling_prime(dtrace_state_t *state) 9722 { 9723 dtrace_enabling_t *enab; 9724 int i; 9725 9726 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9727 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9728 9729 if (enab->dten_vstate->dtvs_state != state) 9730 continue; 9731 9732 /* 9733 * We don't want to prime an enabling more than once, lest 9734 * we allow a malicious user to induce resource exhaustion. 9735 * (The ECBs that result from priming an enabling aren't 9736 * leaked -- but they also aren't deallocated until the 9737 * consumer state is destroyed.) 9738 */ 9739 if (enab->dten_primed) 9740 continue; 9741 9742 for (i = 0; i < enab->dten_ndesc; i++) { 9743 enab->dten_current = enab->dten_desc[i]; 9744 (void) dtrace_probe_enable(NULL, enab); 9745 } 9746 9747 enab->dten_primed = 1; 9748 } 9749 } 9750 9751 /* 9752 * Called to indicate that probes should be provided due to retained 9753 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 9754 * must take an initial lap through the enabling calling the dtps_provide() 9755 * entry point explicitly to allow for autocreated probes. 9756 */ 9757 static void 9758 dtrace_enabling_provide(dtrace_provider_t *prv) 9759 { 9760 int i, all = 0; 9761 dtrace_probedesc_t desc; 9762 9763 ASSERT(MUTEX_HELD(&dtrace_lock)); 9764 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9765 9766 if (prv == NULL) { 9767 all = 1; 9768 prv = dtrace_provider; 9769 } 9770 9771 do { 9772 dtrace_enabling_t *enab = dtrace_retained; 9773 void *parg = prv->dtpv_arg; 9774 9775 for (; enab != NULL; enab = enab->dten_next) { 9776 for (i = 0; i < enab->dten_ndesc; i++) { 9777 desc = enab->dten_desc[i]->dted_probe; 9778 mutex_exit(&dtrace_lock); 9779 prv->dtpv_pops.dtps_provide(parg, &desc); 9780 mutex_enter(&dtrace_lock); 9781 } 9782 } 9783 } while (all && (prv = prv->dtpv_next) != NULL); 9784 9785 mutex_exit(&dtrace_lock); 9786 dtrace_probe_provide(NULL, all ? NULL : prv); 9787 mutex_enter(&dtrace_lock); 9788 } 9789 9790 /* 9791 * DTrace DOF Functions 9792 */ 9793 /*ARGSUSED*/ 9794 static void 9795 dtrace_dof_error(dof_hdr_t *dof, const char *str) 9796 { 9797 if (dtrace_err_verbose) 9798 cmn_err(CE_WARN, "failed to process DOF: %s", str); 9799 9800 #ifdef DTRACE_ERRDEBUG 9801 dtrace_errdebug(str); 9802 #endif 9803 } 9804 9805 /* 9806 * Create DOF out of a currently enabled state. Right now, we only create 9807 * DOF containing the run-time options -- but this could be expanded to create 9808 * complete DOF representing the enabled state. 9809 */ 9810 static dof_hdr_t * 9811 dtrace_dof_create(dtrace_state_t *state) 9812 { 9813 dof_hdr_t *dof; 9814 dof_sec_t *sec; 9815 dof_optdesc_t *opt; 9816 int i, len = sizeof (dof_hdr_t) + 9817 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 9818 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 9819 9820 ASSERT(MUTEX_HELD(&dtrace_lock)); 9821 9822 dof = kmem_zalloc(len, KM_SLEEP); 9823 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 9824 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 9825 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 9826 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 9827 9828 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 9829 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 9830 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION_1; 9831 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 9832 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 9833 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 9834 9835 dof->dofh_flags = 0; 9836 dof->dofh_hdrsize = sizeof (dof_hdr_t); 9837 dof->dofh_secsize = sizeof (dof_sec_t); 9838 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 9839 dof->dofh_secoff = sizeof (dof_hdr_t); 9840 dof->dofh_loadsz = len; 9841 dof->dofh_filesz = len; 9842 dof->dofh_pad = 0; 9843 9844 /* 9845 * Fill in the option section header... 9846 */ 9847 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 9848 sec->dofs_type = DOF_SECT_OPTDESC; 9849 sec->dofs_align = sizeof (uint64_t); 9850 sec->dofs_flags = DOF_SECF_LOAD; 9851 sec->dofs_entsize = sizeof (dof_optdesc_t); 9852 9853 opt = (dof_optdesc_t *)((uintptr_t)sec + 9854 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 9855 9856 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 9857 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 9858 9859 for (i = 0; i < DTRACEOPT_MAX; i++) { 9860 opt[i].dofo_option = i; 9861 opt[i].dofo_strtab = DOF_SECIDX_NONE; 9862 opt[i].dofo_value = state->dts_options[i]; 9863 } 9864 9865 return (dof); 9866 } 9867 9868 static dof_hdr_t * 9869 dtrace_dof_copyin(uintptr_t uarg, int *errp) 9870 { 9871 dof_hdr_t hdr, *dof; 9872 9873 ASSERT(!MUTEX_HELD(&dtrace_lock)); 9874 9875 /* 9876 * First, we're going to copyin() the sizeof (dof_hdr_t). 9877 */ 9878 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 9879 dtrace_dof_error(NULL, "failed to copyin DOF header"); 9880 *errp = EFAULT; 9881 return (NULL); 9882 } 9883 9884 /* 9885 * Now we'll allocate the entire DOF and copy it in -- provided 9886 * that the length isn't outrageous. 9887 */ 9888 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 9889 dtrace_dof_error(&hdr, "load size exceeds maximum"); 9890 *errp = E2BIG; 9891 return (NULL); 9892 } 9893 9894 if (hdr.dofh_loadsz < sizeof (hdr)) { 9895 dtrace_dof_error(&hdr, "invalid load size"); 9896 *errp = EINVAL; 9897 return (NULL); 9898 } 9899 9900 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 9901 9902 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) { 9903 kmem_free(dof, hdr.dofh_loadsz); 9904 *errp = EFAULT; 9905 return (NULL); 9906 } 9907 9908 return (dof); 9909 } 9910 9911 static dof_hdr_t * 9912 dtrace_dof_property(const char *name) 9913 { 9914 uchar_t *buf; 9915 uint64_t loadsz; 9916 unsigned int len, i; 9917 dof_hdr_t *dof; 9918 9919 /* 9920 * Unfortunately, array of values in .conf files are always (and 9921 * only) interpreted to be integer arrays. We must read our DOF 9922 * as an integer array, and then squeeze it into a byte array. 9923 */ 9924 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 9925 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 9926 return (NULL); 9927 9928 for (i = 0; i < len; i++) 9929 buf[i] = (uchar_t)(((int *)buf)[i]); 9930 9931 if (len < sizeof (dof_hdr_t)) { 9932 ddi_prop_free(buf); 9933 dtrace_dof_error(NULL, "truncated header"); 9934 return (NULL); 9935 } 9936 9937 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 9938 ddi_prop_free(buf); 9939 dtrace_dof_error(NULL, "truncated DOF"); 9940 return (NULL); 9941 } 9942 9943 if (loadsz >= dtrace_dof_maxsize) { 9944 ddi_prop_free(buf); 9945 dtrace_dof_error(NULL, "oversized DOF"); 9946 return (NULL); 9947 } 9948 9949 dof = kmem_alloc(loadsz, KM_SLEEP); 9950 bcopy(buf, dof, loadsz); 9951 ddi_prop_free(buf); 9952 9953 return (dof); 9954 } 9955 9956 static void 9957 dtrace_dof_destroy(dof_hdr_t *dof) 9958 { 9959 kmem_free(dof, dof->dofh_loadsz); 9960 } 9961 9962 /* 9963 * Return the dof_sec_t pointer corresponding to a given section index. If the 9964 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 9965 * a type other than DOF_SECT_NONE is specified, the header is checked against 9966 * this type and NULL is returned if the types do not match. 9967 */ 9968 static dof_sec_t * 9969 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 9970 { 9971 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 9972 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 9973 9974 if (i >= dof->dofh_secnum) { 9975 dtrace_dof_error(dof, "referenced section index is invalid"); 9976 return (NULL); 9977 } 9978 9979 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 9980 dtrace_dof_error(dof, "referenced section is not loadable"); 9981 return (NULL); 9982 } 9983 9984 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 9985 dtrace_dof_error(dof, "referenced section is the wrong type"); 9986 return (NULL); 9987 } 9988 9989 return (sec); 9990 } 9991 9992 static dtrace_probedesc_t * 9993 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 9994 { 9995 dof_probedesc_t *probe; 9996 dof_sec_t *strtab; 9997 uintptr_t daddr = (uintptr_t)dof; 9998 uintptr_t str; 9999 size_t size; 10000 10001 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 10002 dtrace_dof_error(dof, "invalid probe section"); 10003 return (NULL); 10004 } 10005 10006 if (sec->dofs_align != sizeof (dof_secidx_t)) { 10007 dtrace_dof_error(dof, "bad alignment in probe description"); 10008 return (NULL); 10009 } 10010 10011 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 10012 dtrace_dof_error(dof, "truncated probe description"); 10013 return (NULL); 10014 } 10015 10016 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 10017 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 10018 10019 if (strtab == NULL) 10020 return (NULL); 10021 10022 str = daddr + strtab->dofs_offset; 10023 size = strtab->dofs_size; 10024 10025 if (probe->dofp_provider >= strtab->dofs_size) { 10026 dtrace_dof_error(dof, "corrupt probe provider"); 10027 return (NULL); 10028 } 10029 10030 (void) strncpy(desc->dtpd_provider, 10031 (char *)(str + probe->dofp_provider), 10032 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 10033 10034 if (probe->dofp_mod >= strtab->dofs_size) { 10035 dtrace_dof_error(dof, "corrupt probe module"); 10036 return (NULL); 10037 } 10038 10039 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 10040 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 10041 10042 if (probe->dofp_func >= strtab->dofs_size) { 10043 dtrace_dof_error(dof, "corrupt probe function"); 10044 return (NULL); 10045 } 10046 10047 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 10048 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 10049 10050 if (probe->dofp_name >= strtab->dofs_size) { 10051 dtrace_dof_error(dof, "corrupt probe name"); 10052 return (NULL); 10053 } 10054 10055 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 10056 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 10057 10058 return (desc); 10059 } 10060 10061 static dtrace_difo_t * 10062 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10063 cred_t *cr) 10064 { 10065 dtrace_difo_t *dp; 10066 size_t ttl = 0; 10067 dof_difohdr_t *dofd; 10068 uintptr_t daddr = (uintptr_t)dof; 10069 size_t max = dtrace_difo_maxsize; 10070 int i, l, n; 10071 10072 static const struct { 10073 int section; 10074 int bufoffs; 10075 int lenoffs; 10076 int entsize; 10077 int align; 10078 const char *msg; 10079 } difo[] = { 10080 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 10081 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 10082 sizeof (dif_instr_t), "multiple DIF sections" }, 10083 10084 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 10085 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 10086 sizeof (uint64_t), "multiple integer tables" }, 10087 10088 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 10089 offsetof(dtrace_difo_t, dtdo_strlen), 0, 10090 sizeof (char), "multiple string tables" }, 10091 10092 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 10093 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 10094 sizeof (uint_t), "multiple variable tables" }, 10095 10096 { DOF_SECT_NONE, 0, 0, 0, NULL } 10097 }; 10098 10099 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 10100 dtrace_dof_error(dof, "invalid DIFO header section"); 10101 return (NULL); 10102 } 10103 10104 if (sec->dofs_align != sizeof (dof_secidx_t)) { 10105 dtrace_dof_error(dof, "bad alignment in DIFO header"); 10106 return (NULL); 10107 } 10108 10109 if (sec->dofs_size < sizeof (dof_difohdr_t) || 10110 sec->dofs_size % sizeof (dof_secidx_t)) { 10111 dtrace_dof_error(dof, "bad size in DIFO header"); 10112 return (NULL); 10113 } 10114 10115 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 10116 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 10117 10118 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10119 dp->dtdo_rtype = dofd->dofd_rtype; 10120 10121 for (l = 0; l < n; l++) { 10122 dof_sec_t *subsec; 10123 void **bufp; 10124 uint32_t *lenp; 10125 10126 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 10127 dofd->dofd_links[l])) == NULL) 10128 goto err; /* invalid section link */ 10129 10130 if (ttl + subsec->dofs_size > max) { 10131 dtrace_dof_error(dof, "exceeds maximum size"); 10132 goto err; 10133 } 10134 10135 ttl += subsec->dofs_size; 10136 10137 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 10138 if (subsec->dofs_type != difo[i].section) 10139 continue; 10140 10141 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 10142 dtrace_dof_error(dof, "section not loaded"); 10143 goto err; 10144 } 10145 10146 if (subsec->dofs_align != difo[i].align) { 10147 dtrace_dof_error(dof, "bad alignment"); 10148 goto err; 10149 } 10150 10151 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 10152 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 10153 10154 if (*bufp != NULL) { 10155 dtrace_dof_error(dof, difo[i].msg); 10156 goto err; 10157 } 10158 10159 if (difo[i].entsize != subsec->dofs_entsize) { 10160 dtrace_dof_error(dof, "entry size mismatch"); 10161 goto err; 10162 } 10163 10164 if (subsec->dofs_entsize != 0 && 10165 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 10166 dtrace_dof_error(dof, "corrupt entry size"); 10167 goto err; 10168 } 10169 10170 *lenp = subsec->dofs_size; 10171 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 10172 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 10173 *bufp, subsec->dofs_size); 10174 10175 if (subsec->dofs_entsize != 0) 10176 *lenp /= subsec->dofs_entsize; 10177 10178 break; 10179 } 10180 10181 /* 10182 * If we encounter a loadable DIFO sub-section that is not 10183 * known to us, assume this is a broken program and fail. 10184 */ 10185 if (difo[i].section == DOF_SECT_NONE && 10186 (subsec->dofs_flags & DOF_SECF_LOAD)) { 10187 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 10188 goto err; 10189 } 10190 } 10191 10192 if (dp->dtdo_buf == NULL) { 10193 /* 10194 * We can't have a DIF object without DIF text. 10195 */ 10196 dtrace_dof_error(dof, "missing DIF text"); 10197 goto err; 10198 } 10199 10200 /* 10201 * Before we validate the DIF object, run through the variable table 10202 * looking for the strings -- if any of their size are under, we'll set 10203 * their size to be the system-wide default string size. Note that 10204 * this should _not_ happen if the "strsize" option has been set -- 10205 * in this case, the compiler should have set the size to reflect the 10206 * setting of the option. 10207 */ 10208 for (i = 0; i < dp->dtdo_varlen; i++) { 10209 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10210 dtrace_diftype_t *t = &v->dtdv_type; 10211 10212 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 10213 continue; 10214 10215 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 10216 t->dtdt_size = dtrace_strsize_default; 10217 } 10218 10219 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 10220 goto err; 10221 10222 dtrace_difo_init(dp, vstate); 10223 return (dp); 10224 10225 err: 10226 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10227 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10228 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10229 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10230 10231 kmem_free(dp, sizeof (dtrace_difo_t)); 10232 return (NULL); 10233 } 10234 10235 static dtrace_predicate_t * 10236 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10237 cred_t *cr) 10238 { 10239 dtrace_difo_t *dp; 10240 10241 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 10242 return (NULL); 10243 10244 return (dtrace_predicate_create(dp)); 10245 } 10246 10247 static dtrace_actdesc_t * 10248 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10249 cred_t *cr) 10250 { 10251 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 10252 dof_actdesc_t *desc; 10253 dof_sec_t *difosec; 10254 size_t offs; 10255 uintptr_t daddr = (uintptr_t)dof; 10256 uint64_t arg; 10257 dtrace_actkind_t kind; 10258 10259 if (sec->dofs_type != DOF_SECT_ACTDESC) { 10260 dtrace_dof_error(dof, "invalid action section"); 10261 return (NULL); 10262 } 10263 10264 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 10265 dtrace_dof_error(dof, "truncated action description"); 10266 return (NULL); 10267 } 10268 10269 if (sec->dofs_align != sizeof (uint64_t)) { 10270 dtrace_dof_error(dof, "bad alignment in action description"); 10271 return (NULL); 10272 } 10273 10274 if (sec->dofs_size < sec->dofs_entsize) { 10275 dtrace_dof_error(dof, "section entry size exceeds total size"); 10276 return (NULL); 10277 } 10278 10279 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 10280 dtrace_dof_error(dof, "bad entry size in action description"); 10281 return (NULL); 10282 } 10283 10284 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 10285 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 10286 return (NULL); 10287 } 10288 10289 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 10290 desc = (dof_actdesc_t *)(daddr + 10291 (uintptr_t)sec->dofs_offset + offs); 10292 kind = (dtrace_actkind_t)desc->dofa_kind; 10293 10294 if (DTRACEACT_ISPRINTFLIKE(kind) && 10295 (kind != DTRACEACT_PRINTA || 10296 desc->dofa_strtab != DOF_SECIDX_NONE)) { 10297 dof_sec_t *strtab; 10298 char *str, *fmt; 10299 uint64_t i; 10300 10301 /* 10302 * printf()-like actions must have a format string. 10303 */ 10304 if ((strtab = dtrace_dof_sect(dof, 10305 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 10306 goto err; 10307 10308 str = (char *)((uintptr_t)dof + 10309 (uintptr_t)strtab->dofs_offset); 10310 10311 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 10312 if (str[i] == '\0') 10313 break; 10314 } 10315 10316 if (i >= strtab->dofs_size) { 10317 dtrace_dof_error(dof, "bogus format string"); 10318 goto err; 10319 } 10320 10321 if (i == desc->dofa_arg) { 10322 dtrace_dof_error(dof, "empty format string"); 10323 goto err; 10324 } 10325 10326 i -= desc->dofa_arg; 10327 fmt = kmem_alloc(i + 1, KM_SLEEP); 10328 bcopy(&str[desc->dofa_arg], fmt, i + 1); 10329 arg = (uint64_t)(uintptr_t)fmt; 10330 } else { 10331 if (kind == DTRACEACT_PRINTA) { 10332 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 10333 arg = 0; 10334 } else { 10335 arg = desc->dofa_arg; 10336 } 10337 } 10338 10339 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 10340 desc->dofa_uarg, arg); 10341 10342 if (last != NULL) { 10343 last->dtad_next = act; 10344 } else { 10345 first = act; 10346 } 10347 10348 last = act; 10349 10350 if (desc->dofa_difo == DOF_SECIDX_NONE) 10351 continue; 10352 10353 if ((difosec = dtrace_dof_sect(dof, 10354 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 10355 goto err; 10356 10357 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 10358 10359 if (act->dtad_difo == NULL) 10360 goto err; 10361 } 10362 10363 ASSERT(first != NULL); 10364 return (first); 10365 10366 err: 10367 for (act = first; act != NULL; act = next) { 10368 next = act->dtad_next; 10369 dtrace_actdesc_release(act, vstate); 10370 } 10371 10372 return (NULL); 10373 } 10374 10375 static dtrace_ecbdesc_t * 10376 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10377 cred_t *cr) 10378 { 10379 dtrace_ecbdesc_t *ep; 10380 dof_ecbdesc_t *ecb; 10381 dtrace_probedesc_t *desc; 10382 dtrace_predicate_t *pred = NULL; 10383 10384 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 10385 dtrace_dof_error(dof, "truncated ECB description"); 10386 return (NULL); 10387 } 10388 10389 if (sec->dofs_align != sizeof (uint64_t)) { 10390 dtrace_dof_error(dof, "bad alignment in ECB description"); 10391 return (NULL); 10392 } 10393 10394 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 10395 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 10396 10397 if (sec == NULL) 10398 return (NULL); 10399 10400 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 10401 ep->dted_uarg = ecb->dofe_uarg; 10402 desc = &ep->dted_probe; 10403 10404 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 10405 goto err; 10406 10407 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 10408 if ((sec = dtrace_dof_sect(dof, 10409 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 10410 goto err; 10411 10412 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 10413 goto err; 10414 10415 ep->dted_pred.dtpdd_predicate = pred; 10416 } 10417 10418 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 10419 if ((sec = dtrace_dof_sect(dof, 10420 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 10421 goto err; 10422 10423 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 10424 10425 if (ep->dted_action == NULL) 10426 goto err; 10427 } 10428 10429 return (ep); 10430 10431 err: 10432 if (pred != NULL) 10433 dtrace_predicate_release(pred, vstate); 10434 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 10435 return (NULL); 10436 } 10437 10438 /* 10439 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 10440 * specified DOF. At present, this amounts to simply adding 'ubase' to the 10441 * site of any user SETX relocations to account for load object base address. 10442 * In the future, if we need other relocations, this function can be extended. 10443 */ 10444 static int 10445 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 10446 { 10447 uintptr_t daddr = (uintptr_t)dof; 10448 dof_relohdr_t *dofr = 10449 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 10450 dof_sec_t *ss, *rs, *ts; 10451 dof_relodesc_t *r; 10452 uint_t i, n; 10453 10454 if (sec->dofs_size < sizeof (dof_relohdr_t) || 10455 sec->dofs_align != sizeof (dof_secidx_t)) { 10456 dtrace_dof_error(dof, "invalid relocation header"); 10457 return (-1); 10458 } 10459 10460 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 10461 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 10462 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 10463 10464 if (ss == NULL || rs == NULL || ts == NULL) 10465 return (-1); /* dtrace_dof_error() has been called already */ 10466 10467 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 10468 rs->dofs_align != sizeof (uint64_t)) { 10469 dtrace_dof_error(dof, "invalid relocation section"); 10470 return (-1); 10471 } 10472 10473 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 10474 n = rs->dofs_size / rs->dofs_entsize; 10475 10476 for (i = 0; i < n; i++) { 10477 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 10478 10479 switch (r->dofr_type) { 10480 case DOF_RELO_NONE: 10481 break; 10482 case DOF_RELO_SETX: 10483 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 10484 sizeof (uint64_t) > ts->dofs_size) { 10485 dtrace_dof_error(dof, "bad relocation offset"); 10486 return (-1); 10487 } 10488 10489 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 10490 dtrace_dof_error(dof, "misaligned setx relo"); 10491 return (-1); 10492 } 10493 10494 *(uint64_t *)taddr += ubase; 10495 break; 10496 default: 10497 dtrace_dof_error(dof, "invalid relocation type"); 10498 return (-1); 10499 } 10500 10501 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 10502 } 10503 10504 return (0); 10505 } 10506 10507 /* 10508 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 10509 * header: it should be at the front of a memory region that is at least 10510 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 10511 * size. It need not be validated in any other way. 10512 */ 10513 static int 10514 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 10515 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 10516 { 10517 uint64_t len = dof->dofh_loadsz, seclen; 10518 uintptr_t daddr = (uintptr_t)dof; 10519 dtrace_ecbdesc_t *ep; 10520 dtrace_enabling_t *enab; 10521 uint_t i; 10522 10523 ASSERT(MUTEX_HELD(&dtrace_lock)); 10524 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 10525 10526 /* 10527 * Check the DOF header identification bytes. In addition to checking 10528 * valid settings, we also verify that unused bits/bytes are zeroed so 10529 * we can use them later without fear of regressing existing binaries. 10530 */ 10531 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 10532 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 10533 dtrace_dof_error(dof, "DOF magic string mismatch"); 10534 return (-1); 10535 } 10536 10537 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 10538 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 10539 dtrace_dof_error(dof, "DOF has invalid data model"); 10540 return (-1); 10541 } 10542 10543 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 10544 dtrace_dof_error(dof, "DOF encoding mismatch"); 10545 return (-1); 10546 } 10547 10548 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 10549 dtrace_dof_error(dof, "DOF version mismatch"); 10550 return (-1); 10551 } 10552 10553 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 10554 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 10555 return (-1); 10556 } 10557 10558 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 10559 dtrace_dof_error(dof, "DOF uses too many integer registers"); 10560 return (-1); 10561 } 10562 10563 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 10564 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 10565 return (-1); 10566 } 10567 10568 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 10569 if (dof->dofh_ident[i] != 0) { 10570 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 10571 return (-1); 10572 } 10573 } 10574 10575 if (dof->dofh_flags & ~DOF_FL_VALID) { 10576 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 10577 return (-1); 10578 } 10579 10580 if (dof->dofh_secsize == 0) { 10581 dtrace_dof_error(dof, "zero section header size"); 10582 return (-1); 10583 } 10584 10585 /* 10586 * Check that the section headers don't exceed the amount of DOF 10587 * data. Note that we cast the section size and number of sections 10588 * to uint64_t's to prevent possible overflow in the multiplication. 10589 */ 10590 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 10591 10592 if (dof->dofh_secoff > len || seclen > len || 10593 dof->dofh_secoff + seclen > len) { 10594 dtrace_dof_error(dof, "truncated section headers"); 10595 return (-1); 10596 } 10597 10598 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 10599 dtrace_dof_error(dof, "misaligned section headers"); 10600 return (-1); 10601 } 10602 10603 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 10604 dtrace_dof_error(dof, "misaligned section size"); 10605 return (-1); 10606 } 10607 10608 /* 10609 * Take an initial pass through the section headers to be sure that 10610 * the headers don't have stray offsets. If the 'noprobes' flag is 10611 * set, do not permit sections relating to providers, probes, or args. 10612 */ 10613 for (i = 0; i < dof->dofh_secnum; i++) { 10614 dof_sec_t *sec = (dof_sec_t *)(daddr + 10615 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10616 10617 if (noprobes) { 10618 switch (sec->dofs_type) { 10619 case DOF_SECT_PROVIDER: 10620 case DOF_SECT_PROBES: 10621 case DOF_SECT_PRARGS: 10622 case DOF_SECT_PROFFS: 10623 dtrace_dof_error(dof, "illegal sections " 10624 "for enabling"); 10625 return (-1); 10626 } 10627 } 10628 10629 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 10630 continue; /* just ignore non-loadable sections */ 10631 10632 if (sec->dofs_align & (sec->dofs_align - 1)) { 10633 dtrace_dof_error(dof, "bad section alignment"); 10634 return (-1); 10635 } 10636 10637 if (sec->dofs_offset & (sec->dofs_align - 1)) { 10638 dtrace_dof_error(dof, "misaligned section"); 10639 return (-1); 10640 } 10641 10642 if (sec->dofs_offset > len || sec->dofs_size > len || 10643 sec->dofs_offset + sec->dofs_size > len) { 10644 dtrace_dof_error(dof, "corrupt section header"); 10645 return (-1); 10646 } 10647 10648 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 10649 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 10650 dtrace_dof_error(dof, "non-terminating string table"); 10651 return (-1); 10652 } 10653 } 10654 10655 /* 10656 * Take a second pass through the sections and locate and perform any 10657 * relocations that are present. We do this after the first pass to 10658 * be sure that all sections have had their headers validated. 10659 */ 10660 for (i = 0; i < dof->dofh_secnum; i++) { 10661 dof_sec_t *sec = (dof_sec_t *)(daddr + 10662 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10663 10664 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 10665 continue; /* skip sections that are not loadable */ 10666 10667 switch (sec->dofs_type) { 10668 case DOF_SECT_URELHDR: 10669 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 10670 return (-1); 10671 break; 10672 } 10673 } 10674 10675 if ((enab = *enabp) == NULL) 10676 enab = *enabp = dtrace_enabling_create(vstate); 10677 10678 for (i = 0; i < dof->dofh_secnum; i++) { 10679 dof_sec_t *sec = (dof_sec_t *)(daddr + 10680 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10681 10682 if (sec->dofs_type != DOF_SECT_ECBDESC) 10683 continue; 10684 10685 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 10686 dtrace_enabling_destroy(enab); 10687 *enabp = NULL; 10688 return (-1); 10689 } 10690 10691 dtrace_enabling_add(enab, ep); 10692 } 10693 10694 return (0); 10695 } 10696 10697 /* 10698 * Process DOF for any options. This routine assumes that the DOF has been 10699 * at least processed by dtrace_dof_slurp(). 10700 */ 10701 static int 10702 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 10703 { 10704 int i, rval; 10705 uint32_t entsize; 10706 size_t offs; 10707 dof_optdesc_t *desc; 10708 10709 for (i = 0; i < dof->dofh_secnum; i++) { 10710 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 10711 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10712 10713 if (sec->dofs_type != DOF_SECT_OPTDESC) 10714 continue; 10715 10716 if (sec->dofs_align != sizeof (uint64_t)) { 10717 dtrace_dof_error(dof, "bad alignment in " 10718 "option description"); 10719 return (EINVAL); 10720 } 10721 10722 if ((entsize = sec->dofs_entsize) == 0) { 10723 dtrace_dof_error(dof, "zeroed option entry size"); 10724 return (EINVAL); 10725 } 10726 10727 if (entsize < sizeof (dof_optdesc_t)) { 10728 dtrace_dof_error(dof, "bad option entry size"); 10729 return (EINVAL); 10730 } 10731 10732 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 10733 desc = (dof_optdesc_t *)((uintptr_t)dof + 10734 (uintptr_t)sec->dofs_offset + offs); 10735 10736 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 10737 dtrace_dof_error(dof, "non-zero option string"); 10738 return (EINVAL); 10739 } 10740 10741 if (desc->dofo_value == DTRACEOPT_UNSET) { 10742 dtrace_dof_error(dof, "unset option"); 10743 return (EINVAL); 10744 } 10745 10746 if ((rval = dtrace_state_option(state, 10747 desc->dofo_option, desc->dofo_value)) != 0) { 10748 dtrace_dof_error(dof, "rejected option"); 10749 return (rval); 10750 } 10751 } 10752 } 10753 10754 return (0); 10755 } 10756 10757 /* 10758 * DTrace Consumer State Functions 10759 */ 10760 int 10761 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 10762 { 10763 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 10764 void *base; 10765 uintptr_t limit; 10766 dtrace_dynvar_t *dvar, *next, *start; 10767 int i; 10768 10769 ASSERT(MUTEX_HELD(&dtrace_lock)); 10770 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 10771 10772 bzero(dstate, sizeof (dtrace_dstate_t)); 10773 10774 if ((dstate->dtds_chunksize = chunksize) == 0) 10775 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 10776 10777 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 10778 size = min; 10779 10780 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10781 return (ENOMEM); 10782 10783 dstate->dtds_size = size; 10784 dstate->dtds_base = base; 10785 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 10786 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 10787 10788 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 10789 10790 if (hashsize != 1 && (hashsize & 1)) 10791 hashsize--; 10792 10793 dstate->dtds_hashsize = hashsize; 10794 dstate->dtds_hash = dstate->dtds_base; 10795 10796 /* 10797 * Determine number of active CPUs. Divide free list evenly among 10798 * active CPUs. 10799 */ 10800 start = (dtrace_dynvar_t *) 10801 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 10802 limit = (uintptr_t)base + size; 10803 10804 maxper = (limit - (uintptr_t)start) / NCPU; 10805 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 10806 10807 for (i = 0; i < NCPU; i++) { 10808 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 10809 10810 /* 10811 * If we don't even have enough chunks to make it once through 10812 * NCPUs, we're just going to allocate everything to the first 10813 * CPU. And if we're on the last CPU, we're going to allocate 10814 * whatever is left over. In either case, we set the limit to 10815 * be the limit of the dynamic variable space. 10816 */ 10817 if (maxper == 0 || i == NCPU - 1) { 10818 limit = (uintptr_t)base + size; 10819 start = NULL; 10820 } else { 10821 limit = (uintptr_t)start + maxper; 10822 start = (dtrace_dynvar_t *)limit; 10823 } 10824 10825 ASSERT(limit <= (uintptr_t)base + size); 10826 10827 for (;;) { 10828 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 10829 dstate->dtds_chunksize); 10830 10831 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 10832 break; 10833 10834 dvar->dtdv_next = next; 10835 dvar = next; 10836 } 10837 10838 if (maxper == 0) 10839 break; 10840 } 10841 10842 return (0); 10843 } 10844 10845 void 10846 dtrace_dstate_fini(dtrace_dstate_t *dstate) 10847 { 10848 ASSERT(MUTEX_HELD(&cpu_lock)); 10849 10850 if (dstate->dtds_base == NULL) 10851 return; 10852 10853 kmem_free(dstate->dtds_base, dstate->dtds_size); 10854 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 10855 } 10856 10857 static void 10858 dtrace_vstate_fini(dtrace_vstate_t *vstate) 10859 { 10860 /* 10861 * Logical XOR, where are you? 10862 */ 10863 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 10864 10865 if (vstate->dtvs_nglobals > 0) { 10866 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 10867 sizeof (dtrace_statvar_t *)); 10868 } 10869 10870 if (vstate->dtvs_ntlocals > 0) { 10871 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 10872 sizeof (dtrace_difv_t)); 10873 } 10874 10875 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 10876 10877 if (vstate->dtvs_nlocals > 0) { 10878 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 10879 sizeof (dtrace_statvar_t *)); 10880 } 10881 } 10882 10883 static void 10884 dtrace_state_clean(dtrace_state_t *state) 10885 { 10886 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 10887 return; 10888 10889 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 10890 dtrace_speculation_clean(state); 10891 } 10892 10893 static void 10894 dtrace_state_deadman(dtrace_state_t *state) 10895 { 10896 hrtime_t now; 10897 10898 dtrace_sync(); 10899 10900 now = dtrace_gethrtime(); 10901 10902 if (state != dtrace_anon.dta_state && 10903 now - state->dts_laststatus >= dtrace_deadman_user) 10904 return; 10905 10906 /* 10907 * We must be sure that dts_alive never appears to be less than the 10908 * value upon entry to dtrace_state_deadman(), and because we lack a 10909 * dtrace_cas64(), we cannot store to it atomically. We thus instead 10910 * store INT64_MAX to it, followed by a memory barrier, followed by 10911 * the new value. This assures that dts_alive never appears to be 10912 * less than its true value, regardless of the order in which the 10913 * stores to the underlying storage are issued. 10914 */ 10915 state->dts_alive = INT64_MAX; 10916 dtrace_membar_producer(); 10917 state->dts_alive = now; 10918 } 10919 10920 dtrace_state_t * 10921 dtrace_state_create(dev_t *devp, cred_t *cr) 10922 { 10923 minor_t minor; 10924 major_t major; 10925 char c[30]; 10926 dtrace_state_t *state; 10927 dtrace_optval_t *opt; 10928 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 10929 10930 ASSERT(MUTEX_HELD(&dtrace_lock)); 10931 ASSERT(MUTEX_HELD(&cpu_lock)); 10932 10933 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 10934 VM_BESTFIT | VM_SLEEP); 10935 10936 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 10937 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 10938 return (NULL); 10939 } 10940 10941 state = ddi_get_soft_state(dtrace_softstate, minor); 10942 state->dts_epid = DTRACE_EPIDNONE + 1; 10943 10944 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); 10945 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 10946 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 10947 10948 if (devp != NULL) { 10949 major = getemajor(*devp); 10950 } else { 10951 major = ddi_driver_major(dtrace_devi); 10952 } 10953 10954 state->dts_dev = makedevice(major, minor); 10955 10956 if (devp != NULL) 10957 *devp = state->dts_dev; 10958 10959 /* 10960 * We allocate NCPU buffers. On the one hand, this can be quite 10961 * a bit of memory per instance (nearly 36K on a Starcat). On the 10962 * other hand, it saves an additional memory reference in the probe 10963 * path. 10964 */ 10965 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 10966 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 10967 state->dts_cleaner = CYCLIC_NONE; 10968 state->dts_deadman = CYCLIC_NONE; 10969 state->dts_vstate.dtvs_state = state; 10970 10971 for (i = 0; i < DTRACEOPT_MAX; i++) 10972 state->dts_options[i] = DTRACEOPT_UNSET; 10973 10974 /* 10975 * Set the default options. 10976 */ 10977 opt = state->dts_options; 10978 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 10979 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 10980 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 10981 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 10982 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 10983 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 10984 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 10985 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 10986 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 10987 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 10988 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 10989 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 10990 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 10991 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 10992 10993 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 10994 10995 /* 10996 * Set up the credentials for this instantiation. 10997 */ 10998 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 10999 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 11000 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 11001 } else { 11002 state->dts_cred.dcr_uid = crgetuid(cr); 11003 state->dts_cred.dcr_gid = crgetgid(cr); 11004 11005 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 11006 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 11007 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 11008 } 11009 11010 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) && 11011 PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 11012 state->dts_cred.dcr_visible |= DTRACE_CRV_ALLPROC; 11013 state->dts_cred.dcr_action |= 11014 DTRACE_CRA_PROC_DESTRUCTIVE; 11015 } 11016 11017 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 11018 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 11019 DTRACE_CRV_ALLPROC; 11020 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 11021 DTRACE_CRA_PROC; 11022 11023 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 11024 state->dts_cred.dcr_action |= 11025 DTRACE_CRA_PROC_DESTRUCTIVE; 11026 } 11027 } 11028 11029 return (state); 11030 } 11031 11032 static int 11033 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 11034 { 11035 dtrace_optval_t *opt = state->dts_options, size; 11036 processorid_t cpu; 11037 int flags = 0, rval; 11038 11039 ASSERT(MUTEX_HELD(&dtrace_lock)); 11040 ASSERT(MUTEX_HELD(&cpu_lock)); 11041 ASSERT(which < DTRACEOPT_MAX); 11042 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 11043 (state == dtrace_anon.dta_state && 11044 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 11045 11046 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 11047 return (0); 11048 11049 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 11050 cpu = opt[DTRACEOPT_CPU]; 11051 11052 if (which == DTRACEOPT_SPECSIZE) 11053 flags |= DTRACEBUF_NOSWITCH; 11054 11055 if (which == DTRACEOPT_BUFSIZE) { 11056 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 11057 flags |= DTRACEBUF_RING; 11058 11059 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 11060 flags |= DTRACEBUF_FILL; 11061 11062 flags |= DTRACEBUF_INACTIVE; 11063 } 11064 11065 for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { 11066 /* 11067 * The size must be 8-byte aligned. If the size is not 8-byte 11068 * aligned, drop it down by the difference. 11069 */ 11070 if (size & (sizeof (uint64_t) - 1)) 11071 size -= size & (sizeof (uint64_t) - 1); 11072 11073 if (size < state->dts_reserve) { 11074 /* 11075 * Buffers always must be large enough to accommodate 11076 * their prereserved space. We return E2BIG instead 11077 * of ENOMEM in this case to allow for user-level 11078 * software to differentiate the cases. 11079 */ 11080 return (E2BIG); 11081 } 11082 11083 rval = dtrace_buffer_alloc(buf, size, flags, cpu); 11084 11085 if (rval != ENOMEM) { 11086 opt[which] = size; 11087 return (rval); 11088 } 11089 11090 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 11091 return (rval); 11092 } 11093 11094 return (ENOMEM); 11095 } 11096 11097 static int 11098 dtrace_state_buffers(dtrace_state_t *state) 11099 { 11100 dtrace_speculation_t *spec = state->dts_speculations; 11101 int rval, i; 11102 11103 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 11104 DTRACEOPT_BUFSIZE)) != 0) 11105 return (rval); 11106 11107 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 11108 DTRACEOPT_AGGSIZE)) != 0) 11109 return (rval); 11110 11111 for (i = 0; i < state->dts_nspeculations; i++) { 11112 if ((rval = dtrace_state_buffer(state, 11113 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 11114 return (rval); 11115 } 11116 11117 return (0); 11118 } 11119 11120 static void 11121 dtrace_state_prereserve(dtrace_state_t *state) 11122 { 11123 dtrace_ecb_t *ecb; 11124 dtrace_probe_t *probe; 11125 11126 state->dts_reserve = 0; 11127 11128 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 11129 return; 11130 11131 /* 11132 * If our buffer policy is a "fill" buffer policy, we need to set the 11133 * prereserved space to be the space required by the END probes. 11134 */ 11135 probe = dtrace_probes[dtrace_probeid_end - 1]; 11136 ASSERT(probe != NULL); 11137 11138 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 11139 if (ecb->dte_state != state) 11140 continue; 11141 11142 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 11143 } 11144 } 11145 11146 static int 11147 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 11148 { 11149 dtrace_optval_t *opt = state->dts_options, sz, nspec; 11150 dtrace_speculation_t *spec; 11151 dtrace_buffer_t *buf; 11152 cyc_handler_t hdlr; 11153 cyc_time_t when; 11154 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 11155 dtrace_icookie_t cookie; 11156 11157 mutex_enter(&cpu_lock); 11158 mutex_enter(&dtrace_lock); 11159 11160 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 11161 rval = EBUSY; 11162 goto out; 11163 } 11164 11165 /* 11166 * Before we can perform any checks, we must prime all of the 11167 * retained enablings that correspond to this state. 11168 */ 11169 dtrace_enabling_prime(state); 11170 11171 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 11172 rval = EACCES; 11173 goto out; 11174 } 11175 11176 dtrace_state_prereserve(state); 11177 11178 /* 11179 * Now we want to do is try to allocate our speculations. 11180 * We do not automatically resize the number of speculations; if 11181 * this fails, we will fail the operation. 11182 */ 11183 nspec = opt[DTRACEOPT_NSPEC]; 11184 ASSERT(nspec != DTRACEOPT_UNSET); 11185 11186 if (nspec > INT_MAX) { 11187 rval = ENOMEM; 11188 goto out; 11189 } 11190 11191 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); 11192 11193 if (spec == NULL) { 11194 rval = ENOMEM; 11195 goto out; 11196 } 11197 11198 state->dts_speculations = spec; 11199 state->dts_nspeculations = (int)nspec; 11200 11201 for (i = 0; i < nspec; i++) { 11202 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { 11203 rval = ENOMEM; 11204 goto err; 11205 } 11206 11207 spec[i].dtsp_buffer = buf; 11208 } 11209 11210 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 11211 if (dtrace_anon.dta_state == NULL) { 11212 rval = ENOENT; 11213 goto out; 11214 } 11215 11216 if (state->dts_necbs != 0) { 11217 rval = EALREADY; 11218 goto out; 11219 } 11220 11221 state->dts_anon = dtrace_anon_grab(); 11222 ASSERT(state->dts_anon != NULL); 11223 11224 *cpu = dtrace_anon.dta_beganon; 11225 11226 /* 11227 * If the anonymous state is active (as it almost certainly 11228 * is if the anonymous enabling ultimately matched anything), 11229 * we don't allow any further option processing -- but we 11230 * don't return failure. 11231 */ 11232 state = state->dts_anon; 11233 11234 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11235 goto out; 11236 } 11237 11238 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 11239 opt[DTRACEOPT_AGGSIZE] != 0) { 11240 if (state->dts_aggregations == NULL) { 11241 /* 11242 * We're not going to create an aggregation buffer 11243 * because we don't have any ECBs that contain 11244 * aggregations -- set this option to 0. 11245 */ 11246 opt[DTRACEOPT_AGGSIZE] = 0; 11247 } else { 11248 /* 11249 * If we have an aggregation buffer, we must also have 11250 * a buffer to use as scratch. 11251 */ 11252 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 11253 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 11254 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 11255 } 11256 } 11257 } 11258 11259 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 11260 opt[DTRACEOPT_SPECSIZE] != 0) { 11261 if (!state->dts_speculates) { 11262 /* 11263 * We're not going to create speculation buffers 11264 * because we don't have any ECBs that actually 11265 * speculate -- set the speculation size to 0. 11266 */ 11267 opt[DTRACEOPT_SPECSIZE] = 0; 11268 } 11269 } 11270 11271 /* 11272 * The bare minimum size for any buffer that we're actually going to 11273 * do anything to is sizeof (uint64_t). 11274 */ 11275 sz = sizeof (uint64_t); 11276 11277 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 11278 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 11279 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 11280 /* 11281 * A buffer size has been explicitly set to 0 (or to a size 11282 * that will be adjusted to 0) and we need the space -- we 11283 * need to return failure. We return ENOSPC to differentiate 11284 * it from failing to allocate a buffer due to failure to meet 11285 * the reserve (for which we return E2BIG). 11286 */ 11287 rval = ENOSPC; 11288 goto out; 11289 } 11290 11291 if ((rval = dtrace_state_buffers(state)) != 0) 11292 goto err; 11293 11294 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 11295 sz = dtrace_dstate_defsize; 11296 11297 do { 11298 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 11299 11300 if (rval == 0) 11301 break; 11302 11303 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 11304 goto err; 11305 } while (sz >>= 1); 11306 11307 opt[DTRACEOPT_DYNVARSIZE] = sz; 11308 11309 if (rval != 0) 11310 goto err; 11311 11312 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 11313 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 11314 11315 if (opt[DTRACEOPT_CLEANRATE] == 0) 11316 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 11317 11318 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 11319 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 11320 11321 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 11322 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 11323 11324 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 11325 hdlr.cyh_arg = state; 11326 hdlr.cyh_level = CY_LOW_LEVEL; 11327 11328 when.cyt_when = 0; 11329 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 11330 11331 state->dts_cleaner = cyclic_add(&hdlr, &when); 11332 11333 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 11334 hdlr.cyh_arg = state; 11335 hdlr.cyh_level = CY_LOW_LEVEL; 11336 11337 when.cyt_when = 0; 11338 when.cyt_interval = dtrace_deadman_interval; 11339 11340 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 11341 state->dts_deadman = cyclic_add(&hdlr, &when); 11342 11343 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 11344 11345 /* 11346 * Now it's time to actually fire the BEGIN probe. We need to disable 11347 * interrupts here both to record the CPU on which we fired the BEGIN 11348 * probe (the data from this CPU will be processed first at user 11349 * level) and to manually activate the buffer for this CPU. 11350 */ 11351 cookie = dtrace_interrupt_disable(); 11352 *cpu = CPU->cpu_id; 11353 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 11354 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 11355 11356 dtrace_probe(dtrace_probeid_begin, 11357 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 11358 dtrace_interrupt_enable(cookie); 11359 /* 11360 * We may have had an exit action from a BEGIN probe; only change our 11361 * state to ACTIVE if we're still in WARMUP. 11362 */ 11363 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 11364 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 11365 11366 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 11367 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 11368 11369 /* 11370 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 11371 * want each CPU to transition its principal buffer out of the 11372 * INACTIVE state. Doing this assures that no CPU will suddenly begin 11373 * processing an ECB halfway down a probe's ECB chain; all CPUs will 11374 * atomically transition from processing none of a state's ECBs to 11375 * processing all of them. 11376 */ 11377 dtrace_xcall(DTRACE_CPUALL, 11378 (dtrace_xcall_t)dtrace_buffer_activate, state); 11379 goto out; 11380 11381 err: 11382 dtrace_buffer_free(state->dts_buffer); 11383 dtrace_buffer_free(state->dts_aggbuffer); 11384 11385 if ((nspec = state->dts_nspeculations) == 0) { 11386 ASSERT(state->dts_speculations == NULL); 11387 goto out; 11388 } 11389 11390 spec = state->dts_speculations; 11391 ASSERT(spec != NULL); 11392 11393 for (i = 0; i < state->dts_nspeculations; i++) { 11394 if ((buf = spec[i].dtsp_buffer) == NULL) 11395 break; 11396 11397 dtrace_buffer_free(buf); 11398 kmem_free(buf, bufsize); 11399 } 11400 11401 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 11402 state->dts_nspeculations = 0; 11403 state->dts_speculations = NULL; 11404 11405 out: 11406 mutex_exit(&dtrace_lock); 11407 mutex_exit(&cpu_lock); 11408 11409 return (rval); 11410 } 11411 11412 static int 11413 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 11414 { 11415 dtrace_icookie_t cookie; 11416 11417 ASSERT(MUTEX_HELD(&dtrace_lock)); 11418 11419 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 11420 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 11421 return (EINVAL); 11422 11423 /* 11424 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 11425 * to be sure that every CPU has seen it. See below for the details 11426 * on why this is done. 11427 */ 11428 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 11429 dtrace_sync(); 11430 11431 /* 11432 * By this point, it is impossible for any CPU to be still processing 11433 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 11434 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 11435 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 11436 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 11437 * iff we're in the END probe. 11438 */ 11439 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 11440 dtrace_sync(); 11441 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 11442 11443 /* 11444 * Finally, we can release the reserve and call the END probe. We 11445 * disable interrupts across calling the END probe to allow us to 11446 * return the CPU on which we actually called the END probe. This 11447 * allows user-land to be sure that this CPU's principal buffer is 11448 * processed last. 11449 */ 11450 state->dts_reserve = 0; 11451 11452 cookie = dtrace_interrupt_disable(); 11453 *cpu = CPU->cpu_id; 11454 dtrace_probe(dtrace_probeid_end, 11455 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 11456 dtrace_interrupt_enable(cookie); 11457 11458 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 11459 dtrace_sync(); 11460 11461 return (0); 11462 } 11463 11464 static int 11465 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 11466 dtrace_optval_t val) 11467 { 11468 ASSERT(MUTEX_HELD(&dtrace_lock)); 11469 11470 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11471 return (EBUSY); 11472 11473 if (option >= DTRACEOPT_MAX) 11474 return (EINVAL); 11475 11476 if (option != DTRACEOPT_CPU && val < 0) 11477 return (EINVAL); 11478 11479 switch (option) { 11480 case DTRACEOPT_DESTRUCTIVE: 11481 if (dtrace_destructive_disallow) 11482 return (EACCES); 11483 11484 state->dts_cred.dcr_destructive = 1; 11485 break; 11486 11487 case DTRACEOPT_BUFSIZE: 11488 case DTRACEOPT_DYNVARSIZE: 11489 case DTRACEOPT_AGGSIZE: 11490 case DTRACEOPT_SPECSIZE: 11491 case DTRACEOPT_STRSIZE: 11492 if (val < 0) 11493 return (EINVAL); 11494 11495 if (val >= LONG_MAX) { 11496 /* 11497 * If this is an otherwise negative value, set it to 11498 * the highest multiple of 128m less than LONG_MAX. 11499 * Technically, we're adjusting the size without 11500 * regard to the buffer resizing policy, but in fact, 11501 * this has no effect -- if we set the buffer size to 11502 * ~LONG_MAX and the buffer policy is ultimately set to 11503 * be "manual", the buffer allocation is guaranteed to 11504 * fail, if only because the allocation requires two 11505 * buffers. (We set the the size to the highest 11506 * multiple of 128m because it ensures that the size 11507 * will remain a multiple of a megabyte when 11508 * repeatedly halved -- all the way down to 15m.) 11509 */ 11510 val = LONG_MAX - (1 << 27) + 1; 11511 } 11512 } 11513 11514 state->dts_options[option] = val; 11515 11516 return (0); 11517 } 11518 11519 static void 11520 dtrace_state_destroy(dtrace_state_t *state) 11521 { 11522 dtrace_ecb_t *ecb; 11523 dtrace_vstate_t *vstate = &state->dts_vstate; 11524 minor_t minor = getminor(state->dts_dev); 11525 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 11526 dtrace_speculation_t *spec = state->dts_speculations; 11527 int nspec = state->dts_nspeculations; 11528 uint32_t match; 11529 11530 ASSERT(MUTEX_HELD(&dtrace_lock)); 11531 ASSERT(MUTEX_HELD(&cpu_lock)); 11532 11533 /* 11534 * First, retract any retained enablings for this state. 11535 */ 11536 dtrace_enabling_retract(state); 11537 ASSERT(state->dts_nretained == 0); 11538 11539 /* 11540 * Now we need to disable and destroy any enabled probes. Because any 11541 * DTRACE_PRIV_KERNEL probes may actually be slowing our progress 11542 * (especially if they're all enabled), we take two passes through 11543 * the ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, 11544 * and in the second we disable whatever is left over. 11545 */ 11546 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 11547 for (i = 0; i < state->dts_necbs; i++) { 11548 if ((ecb = state->dts_ecbs[i]) == NULL) 11549 continue; 11550 11551 if (match && ecb->dte_probe != NULL) { 11552 dtrace_probe_t *probe = ecb->dte_probe; 11553 dtrace_provider_t *prov = probe->dtpr_provider; 11554 11555 if (!(prov->dtpv_priv.dtpp_flags & match)) 11556 continue; 11557 } 11558 11559 dtrace_ecb_disable(ecb); 11560 dtrace_ecb_destroy(ecb); 11561 } 11562 11563 if (!match) 11564 break; 11565 } 11566 11567 /* 11568 * Before we free the buffers, perform one more sync to assure that 11569 * every CPU is out of probe context. 11570 */ 11571 dtrace_sync(); 11572 11573 dtrace_buffer_free(state->dts_buffer); 11574 dtrace_buffer_free(state->dts_aggbuffer); 11575 11576 for (i = 0; i < nspec; i++) 11577 dtrace_buffer_free(spec[i].dtsp_buffer); 11578 11579 if (state->dts_cleaner != CYCLIC_NONE) 11580 cyclic_remove(state->dts_cleaner); 11581 11582 if (state->dts_deadman != CYCLIC_NONE) 11583 cyclic_remove(state->dts_deadman); 11584 11585 dtrace_dstate_fini(&vstate->dtvs_dynvars); 11586 dtrace_vstate_fini(vstate); 11587 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 11588 11589 if (state->dts_aggregations != NULL) { 11590 #ifdef DEBUG 11591 for (i = 0; i < state->dts_naggregations; i++) 11592 ASSERT(state->dts_aggregations[i] == NULL); 11593 #endif 11594 ASSERT(state->dts_naggregations > 0); 11595 kmem_free(state->dts_aggregations, 11596 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 11597 } 11598 11599 kmem_free(state->dts_buffer, bufsize); 11600 kmem_free(state->dts_aggbuffer, bufsize); 11601 11602 for (i = 0; i < nspec; i++) 11603 kmem_free(spec[i].dtsp_buffer, bufsize); 11604 11605 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 11606 11607 dtrace_format_destroy(state); 11608 11609 vmem_destroy(state->dts_aggid_arena); 11610 ddi_soft_state_free(dtrace_softstate, minor); 11611 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 11612 } 11613 11614 /* 11615 * DTrace Anonymous Enabling Functions 11616 */ 11617 static dtrace_state_t * 11618 dtrace_anon_grab(void) 11619 { 11620 dtrace_state_t *state; 11621 11622 ASSERT(MUTEX_HELD(&dtrace_lock)); 11623 11624 if ((state = dtrace_anon.dta_state) == NULL) { 11625 ASSERT(dtrace_anon.dta_enabling == NULL); 11626 return (NULL); 11627 } 11628 11629 ASSERT(dtrace_anon.dta_enabling != NULL); 11630 ASSERT(dtrace_retained != NULL); 11631 11632 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 11633 dtrace_anon.dta_enabling = NULL; 11634 dtrace_anon.dta_state = NULL; 11635 11636 return (state); 11637 } 11638 11639 static void 11640 dtrace_anon_property(void) 11641 { 11642 int i, rv; 11643 dtrace_state_t *state; 11644 dof_hdr_t *dof; 11645 char c[32]; /* enough for "dof-data-" + digits */ 11646 11647 ASSERT(MUTEX_HELD(&dtrace_lock)); 11648 ASSERT(MUTEX_HELD(&cpu_lock)); 11649 11650 for (i = 0; ; i++) { 11651 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 11652 11653 dtrace_err_verbose = 1; 11654 11655 if ((dof = dtrace_dof_property(c)) == NULL) { 11656 dtrace_err_verbose = 0; 11657 break; 11658 } 11659 11660 /* 11661 * We want to create anonymous state, so we need to transition 11662 * the kernel debugger to indicate that DTrace is active. If 11663 * this fails (e.g. because the debugger has modified text in 11664 * some way), we won't continue with the processing. 11665 */ 11666 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 11667 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 11668 "enabling ignored."); 11669 dtrace_dof_destroy(dof); 11670 break; 11671 } 11672 11673 /* 11674 * If we haven't allocated an anonymous state, we'll do so now. 11675 */ 11676 if ((state = dtrace_anon.dta_state) == NULL) { 11677 state = dtrace_state_create(NULL, NULL); 11678 dtrace_anon.dta_state = state; 11679 11680 if (state == NULL) { 11681 /* 11682 * This basically shouldn't happen: the only 11683 * failure mode from dtrace_state_create() is a 11684 * failure of ddi_soft_state_zalloc() that 11685 * itself should never happen. Still, the 11686 * interface allows for a failure mode, and 11687 * we want to fail as gracefully as possible: 11688 * we'll emit an error message and cease 11689 * processing anonymous state in this case. 11690 */ 11691 cmn_err(CE_WARN, "failed to create " 11692 "anonymous state"); 11693 dtrace_dof_destroy(dof); 11694 break; 11695 } 11696 } 11697 11698 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 11699 &dtrace_anon.dta_enabling, 0, B_TRUE); 11700 11701 if (rv == 0) 11702 rv = dtrace_dof_options(dof, state); 11703 11704 dtrace_err_verbose = 0; 11705 dtrace_dof_destroy(dof); 11706 11707 if (rv != 0) { 11708 /* 11709 * This is malformed DOF; chuck any anonymous state 11710 * that we created. 11711 */ 11712 ASSERT(dtrace_anon.dta_enabling == NULL); 11713 dtrace_state_destroy(state); 11714 dtrace_anon.dta_state = NULL; 11715 break; 11716 } 11717 11718 ASSERT(dtrace_anon.dta_enabling != NULL); 11719 } 11720 11721 if (dtrace_anon.dta_enabling != NULL) { 11722 int rval; 11723 11724 /* 11725 * dtrace_enabling_retain() can only fail because we are 11726 * trying to retain more enablings than are allowed -- but 11727 * we only have one anonymous enabling, and we are guaranteed 11728 * to be allowed at least one retained enabling; we assert 11729 * that dtrace_enabling_retain() returns success. 11730 */ 11731 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 11732 ASSERT(rval == 0); 11733 11734 dtrace_enabling_dump(dtrace_anon.dta_enabling); 11735 } 11736 } 11737 11738 /* 11739 * DTrace Helper Functions 11740 */ 11741 static void 11742 dtrace_helper_trace(dtrace_helper_action_t *helper, 11743 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 11744 { 11745 uint32_t size, next, nnext, i; 11746 dtrace_helptrace_t *ent; 11747 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 11748 11749 if (!dtrace_helptrace_enabled) 11750 return; 11751 11752 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 11753 11754 /* 11755 * What would a tracing framework be without its own tracing 11756 * framework? (Well, a hell of a lot simpler, for starters...) 11757 */ 11758 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 11759 sizeof (uint64_t) - sizeof (uint64_t); 11760 11761 /* 11762 * Iterate until we can allocate a slot in the trace buffer. 11763 */ 11764 do { 11765 next = dtrace_helptrace_next; 11766 11767 if (next + size < dtrace_helptrace_bufsize) { 11768 nnext = next + size; 11769 } else { 11770 nnext = size; 11771 } 11772 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 11773 11774 /* 11775 * We have our slot; fill it in. 11776 */ 11777 if (nnext == size) 11778 next = 0; 11779 11780 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 11781 ent->dtht_helper = helper; 11782 ent->dtht_where = where; 11783 ent->dtht_nlocals = vstate->dtvs_nlocals; 11784 11785 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 11786 mstate->dtms_fltoffs : -1; 11787 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 11788 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 11789 11790 for (i = 0; i < vstate->dtvs_nlocals; i++) { 11791 dtrace_statvar_t *svar; 11792 11793 if ((svar = vstate->dtvs_locals[i]) == NULL) 11794 continue; 11795 11796 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 11797 ent->dtht_locals[i] = 11798 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; 11799 } 11800 } 11801 11802 static uint64_t 11803 dtrace_helper(int which, dtrace_mstate_t *mstate, 11804 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 11805 { 11806 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 11807 uint64_t sarg0 = mstate->dtms_arg[0]; 11808 uint64_t sarg1 = mstate->dtms_arg[1]; 11809 uint64_t rval; 11810 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 11811 dtrace_helper_action_t *helper; 11812 dtrace_vstate_t *vstate; 11813 dtrace_difo_t *pred; 11814 int i, trace = dtrace_helptrace_enabled; 11815 11816 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 11817 11818 if (helpers == NULL) 11819 return (0); 11820 11821 if ((helper = helpers->dthps_actions[which]) == NULL) 11822 return (0); 11823 11824 vstate = &helpers->dthps_vstate; 11825 mstate->dtms_arg[0] = arg0; 11826 mstate->dtms_arg[1] = arg1; 11827 11828 /* 11829 * Now iterate over each helper. If its predicate evaluates to 'true', 11830 * we'll call the corresponding actions. Note that the below calls 11831 * to dtrace_dif_emulate() may set faults in machine state. This is 11832 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 11833 * the stored DIF offset with its own (which is the desired behavior). 11834 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 11835 * from machine state; this is okay, too. 11836 */ 11837 for (; helper != NULL; helper = helper->dthp_next) { 11838 if ((pred = helper->dthp_predicate) != NULL) { 11839 if (trace) 11840 dtrace_helper_trace(helper, mstate, vstate, 0); 11841 11842 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 11843 goto next; 11844 11845 if (*flags & CPU_DTRACE_FAULT) 11846 goto err; 11847 } 11848 11849 for (i = 0; i < helper->dthp_nactions; i++) { 11850 if (trace) 11851 dtrace_helper_trace(helper, 11852 mstate, vstate, i + 1); 11853 11854 rval = dtrace_dif_emulate(helper->dthp_actions[i], 11855 mstate, vstate, state); 11856 11857 if (*flags & CPU_DTRACE_FAULT) 11858 goto err; 11859 } 11860 11861 next: 11862 if (trace) 11863 dtrace_helper_trace(helper, mstate, vstate, 11864 DTRACE_HELPTRACE_NEXT); 11865 } 11866 11867 if (trace) 11868 dtrace_helper_trace(helper, mstate, vstate, 11869 DTRACE_HELPTRACE_DONE); 11870 11871 /* 11872 * Restore the arg0 that we saved upon entry. 11873 */ 11874 mstate->dtms_arg[0] = sarg0; 11875 mstate->dtms_arg[1] = sarg1; 11876 11877 return (rval); 11878 11879 err: 11880 if (trace) 11881 dtrace_helper_trace(helper, mstate, vstate, 11882 DTRACE_HELPTRACE_ERR); 11883 11884 /* 11885 * Restore the arg0 that we saved upon entry. 11886 */ 11887 mstate->dtms_arg[0] = sarg0; 11888 mstate->dtms_arg[1] = sarg1; 11889 11890 return (NULL); 11891 } 11892 11893 static void 11894 dtrace_helper_destroy(dtrace_helper_action_t *helper, dtrace_vstate_t *vstate) 11895 { 11896 int i; 11897 11898 if (helper->dthp_predicate != NULL) 11899 dtrace_difo_release(helper->dthp_predicate, vstate); 11900 11901 for (i = 0; i < helper->dthp_nactions; i++) { 11902 ASSERT(helper->dthp_actions[i] != NULL); 11903 dtrace_difo_release(helper->dthp_actions[i], vstate); 11904 } 11905 11906 kmem_free(helper->dthp_actions, 11907 helper->dthp_nactions * sizeof (dtrace_difo_t *)); 11908 kmem_free(helper, sizeof (dtrace_helper_action_t)); 11909 } 11910 11911 static int 11912 dtrace_helper_destroygen(int gen) 11913 { 11914 dtrace_helpers_t *help = curproc->p_dtrace_helpers; 11915 dtrace_vstate_t *vstate; 11916 int i; 11917 11918 ASSERT(MUTEX_HELD(&dtrace_lock)); 11919 11920 if (help == NULL || gen > help->dthps_generation) 11921 return (EINVAL); 11922 11923 vstate = &help->dthps_vstate; 11924 11925 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 11926 dtrace_helper_action_t *last = NULL, *h, *next; 11927 11928 for (h = help->dthps_actions[i]; h != NULL; h = next) { 11929 next = h->dthp_next; 11930 11931 if (h->dthp_generation == gen) { 11932 if (last != NULL) { 11933 last->dthp_next = next; 11934 } else { 11935 help->dthps_actions[i] = next; 11936 } 11937 11938 dtrace_helper_destroy(h, vstate); 11939 } else { 11940 last = h; 11941 } 11942 } 11943 } 11944 11945 return (0); 11946 } 11947 11948 static int 11949 dtrace_helper_validate(dtrace_helper_action_t *helper) 11950 { 11951 int err = 0, i; 11952 dtrace_difo_t *dp; 11953 11954 if ((dp = helper->dthp_predicate) != NULL) 11955 err += dtrace_difo_validate_helper(dp); 11956 11957 for (i = 0; i < helper->dthp_nactions; i++) 11958 err += dtrace_difo_validate_helper(helper->dthp_actions[i]); 11959 11960 return (err == 0); 11961 } 11962 11963 static int 11964 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 11965 { 11966 dtrace_helpers_t *help; 11967 dtrace_helper_action_t *helper, *last; 11968 dtrace_actdesc_t *act; 11969 dtrace_vstate_t *vstate; 11970 dtrace_predicate_t *pred; 11971 int count = 0, nactions = 0, i; 11972 11973 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 11974 return (EINVAL); 11975 11976 help = curproc->p_dtrace_helpers; 11977 last = help->dthps_actions[which]; 11978 vstate = &help->dthps_vstate; 11979 11980 for (count = 0; last != NULL; last = last->dthp_next) { 11981 count++; 11982 if (last->dthp_next == NULL) 11983 break; 11984 } 11985 11986 /* 11987 * If we already have dtrace_helper_actions_max helper actions for this 11988 * helper action type, we'll refuse to add a new one. 11989 */ 11990 if (count >= dtrace_helper_actions_max) 11991 return (ENOSPC); 11992 11993 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 11994 helper->dthp_generation = help->dthps_generation; 11995 11996 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 11997 ASSERT(pred->dtp_difo != NULL); 11998 dtrace_difo_hold(pred->dtp_difo); 11999 helper->dthp_predicate = pred->dtp_difo; 12000 } 12001 12002 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 12003 if (act->dtad_kind != DTRACEACT_DIFEXPR) 12004 goto err; 12005 12006 if (act->dtad_difo == NULL) 12007 goto err; 12008 12009 nactions++; 12010 } 12011 12012 helper->dthp_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 12013 (helper->dthp_nactions = nactions), KM_SLEEP); 12014 12015 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 12016 dtrace_difo_hold(act->dtad_difo); 12017 helper->dthp_actions[i++] = act->dtad_difo; 12018 } 12019 12020 if (!dtrace_helper_validate(helper)) 12021 goto err; 12022 12023 if (last == NULL) { 12024 help->dthps_actions[which] = helper; 12025 } else { 12026 last->dthp_next = helper; 12027 } 12028 12029 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 12030 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 12031 dtrace_helptrace_next = 0; 12032 } 12033 12034 return (0); 12035 err: 12036 dtrace_helper_destroy(helper, vstate); 12037 return (EINVAL); 12038 } 12039 12040 static void 12041 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 12042 dof_helper_t *dofhp) 12043 { 12044 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 12045 12046 mutex_enter(&dtrace_meta_lock); 12047 mutex_enter(&dtrace_lock); 12048 12049 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 12050 /* 12051 * If the dtrace module is loaded but not attached, or if 12052 * there aren't isn't a meta provider registered to deal with 12053 * these provider descriptions, we need to postpone creating 12054 * the actual providers until later. 12055 */ 12056 12057 if (help->dthps_next == NULL && help->dthps_prev == NULL && 12058 dtrace_deferred_pid != help) { 12059 help->dthps_pid = p->p_pid; 12060 help->dthps_next = dtrace_deferred_pid; 12061 help->dthps_prev = NULL; 12062 if (dtrace_deferred_pid != NULL) 12063 dtrace_deferred_pid->dthps_prev = help; 12064 dtrace_deferred_pid = help; 12065 } 12066 12067 mutex_exit(&dtrace_lock); 12068 12069 } else if (dofhp != NULL) { 12070 /* 12071 * If the dtrace module is loaded and we have a particular 12072 * helper provider description, pass that off to the 12073 * meta provider. 12074 */ 12075 12076 mutex_exit(&dtrace_lock); 12077 12078 dtrace_helper_provide(dofhp, p->p_pid); 12079 12080 } else { 12081 /* 12082 * Otherwise, just pass all the helper provider descriptions 12083 * off to the meta provider. 12084 */ 12085 12086 int i; 12087 mutex_exit(&dtrace_lock); 12088 12089 for (i = 0; i < help->dthps_nprovs; i++) { 12090 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 12091 p->p_pid); 12092 } 12093 } 12094 12095 mutex_exit(&dtrace_meta_lock); 12096 } 12097 12098 static int 12099 dtrace_helper_provider_add(dof_helper_t *dofhp) 12100 { 12101 dtrace_helpers_t *help; 12102 dtrace_helper_provider_t *hprov, **tmp_provs; 12103 uint_t tmp_nprovs, i; 12104 12105 help = curproc->p_dtrace_helpers; 12106 ASSERT(help != NULL); 12107 12108 /* 12109 * If we already have dtrace_helper_providers_max helper providers, 12110 * we're refuse to add a new one. 12111 */ 12112 if (help->dthps_nprovs >= dtrace_helper_providers_max) 12113 return (ENOSPC); 12114 12115 /* 12116 * Check to make sure this isn't a duplicate. 12117 */ 12118 for (i = 0; i < help->dthps_nprovs; i++) { 12119 if (dofhp->dofhp_addr == 12120 help->dthps_provs[i]->dthp_prov.dofhp_addr) 12121 return (EALREADY); 12122 } 12123 12124 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 12125 hprov->dthp_prov = *dofhp; 12126 hprov->dthp_ref = 1; 12127 12128 tmp_nprovs = help->dthps_nprovs; 12129 tmp_provs = help->dthps_provs; 12130 help->dthps_nprovs++; 12131 help->dthps_provs = kmem_zalloc(help->dthps_nprovs * 12132 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 12133 12134 help->dthps_provs[tmp_nprovs] = hprov; 12135 if (tmp_provs != NULL) { 12136 bcopy(tmp_provs, help->dthps_provs, tmp_nprovs * 12137 sizeof (dtrace_helper_provider_t *)); 12138 kmem_free(tmp_provs, tmp_nprovs * 12139 sizeof (dtrace_helper_provider_t *)); 12140 } 12141 12142 return (0); 12143 } 12144 12145 static void 12146 dtrace_helper_provider_remove(dtrace_helper_provider_t *hprov) 12147 { 12148 mutex_enter(&dtrace_lock); 12149 12150 if (--hprov->dthp_ref == 0) { 12151 dof_hdr_t *dof; 12152 mutex_exit(&dtrace_lock); 12153 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 12154 dtrace_dof_destroy(dof); 12155 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 12156 } else { 12157 mutex_exit(&dtrace_lock); 12158 } 12159 } 12160 12161 static int 12162 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 12163 { 12164 uintptr_t daddr = (uintptr_t)dof; 12165 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec; 12166 dof_provider_t *provider; 12167 dof_probe_t *probe; 12168 uint8_t *arg; 12169 char *strtab, *typestr; 12170 dof_stridx_t typeidx; 12171 size_t typesz; 12172 uint_t nprobes, j, k; 12173 12174 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 12175 12176 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 12177 dtrace_dof_error(dof, "misaligned section offset"); 12178 return (-1); 12179 } 12180 12181 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 12182 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 12183 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 12184 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 12185 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 12186 12187 if (str_sec == NULL || prb_sec == NULL || 12188 arg_sec == NULL || off_sec == NULL) 12189 return (-1); 12190 12191 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 12192 12193 if (provider->dofpv_name >= str_sec->dofs_size || 12194 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 12195 dtrace_dof_error(dof, "invalid provider name"); 12196 return (-1); 12197 } 12198 12199 if (prb_sec->dofs_entsize == 0 || 12200 prb_sec->dofs_entsize > prb_sec->dofs_size) { 12201 dtrace_dof_error(dof, "invalid entry size"); 12202 return (-1); 12203 } 12204 12205 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 12206 dtrace_dof_error(dof, "misaligned entry size"); 12207 return (-1); 12208 } 12209 12210 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 12211 dtrace_dof_error(dof, "invalid entry size"); 12212 return (-1); 12213 } 12214 12215 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 12216 dtrace_dof_error(dof, "misaligned section offset"); 12217 return (-1); 12218 } 12219 12220 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 12221 dtrace_dof_error(dof, "invalid entry size"); 12222 return (-1); 12223 } 12224 12225 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 12226 12227 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 12228 12229 /* 12230 * Take a pass through the probes to check for errors. 12231 */ 12232 for (j = 0; j < nprobes; j++) { 12233 probe = (dof_probe_t *)(uintptr_t)(daddr + 12234 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 12235 12236 if (probe->dofpr_func >= str_sec->dofs_size) { 12237 dtrace_dof_error(dof, "invalid function name"); 12238 return (-1); 12239 } 12240 12241 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 12242 dtrace_dof_error(dof, "function name too long"); 12243 return (-1); 12244 } 12245 12246 if (probe->dofpr_name >= str_sec->dofs_size || 12247 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 12248 dtrace_dof_error(dof, "invalid probe name"); 12249 return (-1); 12250 } 12251 12252 12253 if (probe->dofpr_offidx + probe->dofpr_noffs < 12254 probe->dofpr_offidx || 12255 (probe->dofpr_offidx + probe->dofpr_noffs) * 12256 off_sec->dofs_entsize > off_sec->dofs_size) { 12257 dtrace_dof_error(dof, "invalid probe offset"); 12258 return (-1); 12259 } 12260 12261 if (probe->dofpr_argidx + probe->dofpr_xargc < 12262 probe->dofpr_argidx || 12263 (probe->dofpr_argidx + probe->dofpr_xargc) * 12264 arg_sec->dofs_entsize > arg_sec->dofs_size) { 12265 dtrace_dof_error(dof, "invalid args"); 12266 return (-1); 12267 } 12268 12269 typeidx = probe->dofpr_nargv; 12270 typestr = strtab + probe->dofpr_nargv; 12271 for (k = 0; k < probe->dofpr_nargc; k++) { 12272 if (typeidx >= str_sec->dofs_size) { 12273 dtrace_dof_error(dof, "bad " 12274 "native argument type"); 12275 return (-1); 12276 } 12277 12278 typesz = strlen(typestr) + 1; 12279 if (typesz > DTRACE_ARGTYPELEN) { 12280 dtrace_dof_error(dof, "native " 12281 "argument type too long"); 12282 return (-1); 12283 } 12284 typeidx += typesz; 12285 typestr += typesz; 12286 } 12287 12288 typeidx = probe->dofpr_xargv; 12289 typestr = strtab + probe->dofpr_xargv; 12290 for (k = 0; k < probe->dofpr_xargc; k++) { 12291 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 12292 dtrace_dof_error(dof, "bad " 12293 "native argument index"); 12294 return (-1); 12295 } 12296 12297 if (typeidx >= str_sec->dofs_size) { 12298 dtrace_dof_error(dof, "bad " 12299 "translated argument type"); 12300 return (-1); 12301 } 12302 12303 typesz = strlen(typestr) + 1; 12304 if (typesz > DTRACE_ARGTYPELEN) { 12305 dtrace_dof_error(dof, "translated argument " 12306 "type too long"); 12307 return (-1); 12308 } 12309 12310 typeidx += typesz; 12311 typestr += typesz; 12312 } 12313 } 12314 12315 return (0); 12316 } 12317 12318 static int 12319 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 12320 { 12321 dtrace_helpers_t *help; 12322 dtrace_vstate_t *vstate; 12323 dtrace_enabling_t *enab = NULL; 12324 int i, gen, rv, nhelpers = 0, destroy = 1; 12325 12326 ASSERT(MUTEX_HELD(&dtrace_lock)); 12327 12328 if ((help = curproc->p_dtrace_helpers) == NULL) 12329 help = dtrace_helpers_create(curproc); 12330 12331 vstate = &help->dthps_vstate; 12332 12333 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 12334 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 12335 dtrace_dof_destroy(dof); 12336 return (rv); 12337 } 12338 12339 /* 12340 * Now we need to walk through the ECB descriptions in the enabling. 12341 */ 12342 for (i = 0; i < enab->dten_ndesc; i++) { 12343 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12344 dtrace_probedesc_t *desc = &ep->dted_probe; 12345 12346 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 12347 continue; 12348 12349 if (strcmp(desc->dtpd_mod, "helper") != 0) 12350 continue; 12351 12352 if (strcmp(desc->dtpd_func, "ustack") != 0) 12353 continue; 12354 12355 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 12356 ep)) != 0) { 12357 /* 12358 * Adding this helper action failed -- we are now going 12359 * to rip out the entire generation and return failure. 12360 */ 12361 (void) dtrace_helper_destroygen(help->dthps_generation); 12362 dtrace_enabling_destroy(enab); 12363 dtrace_dof_destroy(dof); 12364 dtrace_err = rv; 12365 return (-1); 12366 } 12367 12368 nhelpers++; 12369 } 12370 12371 if (nhelpers < enab->dten_ndesc) 12372 dtrace_dof_error(dof, "unmatched helpers"); 12373 12374 if (dhp != NULL) { 12375 uintptr_t daddr = (uintptr_t)dof; 12376 int err = 0; 12377 12378 /* 12379 * Look for helper probes. 12380 */ 12381 for (i = 0; i < dof->dofh_secnum; i++) { 12382 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 12383 dof->dofh_secoff + i * dof->dofh_secsize); 12384 12385 if (sec->dofs_type != DOF_SECT_PROVIDER) 12386 continue; 12387 12388 if (dtrace_helper_provider_validate(dof, sec) != 0) { 12389 err = 1; 12390 break; 12391 } 12392 } 12393 12394 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 12395 if (err == 0 && dtrace_helper_provider_add(dhp) == 0) 12396 destroy = 0; 12397 else 12398 dhp = NULL; 12399 } 12400 12401 gen = help->dthps_generation++; 12402 dtrace_enabling_destroy(enab); 12403 12404 if (dhp != NULL) { 12405 mutex_exit(&dtrace_lock); 12406 dtrace_helper_provider_register(curproc, help, dhp); 12407 mutex_enter(&dtrace_lock); 12408 } 12409 12410 if (destroy) 12411 dtrace_dof_destroy(dof); 12412 12413 return (gen); 12414 } 12415 12416 static dtrace_helpers_t * 12417 dtrace_helpers_create(proc_t *p) 12418 { 12419 dtrace_helpers_t *help; 12420 12421 ASSERT(MUTEX_HELD(&dtrace_lock)); 12422 ASSERT(p->p_dtrace_helpers == NULL); 12423 12424 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 12425 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 12426 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 12427 12428 p->p_dtrace_helpers = help; 12429 dtrace_helpers++; 12430 12431 return (help); 12432 } 12433 12434 static void 12435 dtrace_helpers_destroy(void) 12436 { 12437 dtrace_helpers_t *help; 12438 dtrace_vstate_t *vstate; 12439 proc_t *p = curproc; 12440 int i; 12441 12442 mutex_enter(&dtrace_lock); 12443 12444 ASSERT(p->p_dtrace_helpers != NULL); 12445 ASSERT(dtrace_helpers > 0); 12446 12447 help = p->p_dtrace_helpers; 12448 vstate = &help->dthps_vstate; 12449 12450 /* 12451 * We're now going to lose the help from this process. 12452 */ 12453 p->p_dtrace_helpers = NULL; 12454 dtrace_sync(); 12455 12456 /* 12457 * Destory the helper actions. 12458 */ 12459 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12460 dtrace_helper_action_t *h, *next; 12461 12462 for (h = help->dthps_actions[i]; h != NULL; h = next) { 12463 next = h->dthp_next; 12464 dtrace_helper_destroy(h, vstate); 12465 h = next; 12466 } 12467 } 12468 12469 mutex_exit(&dtrace_lock); 12470 12471 /* 12472 * Destroy the helper providers. 12473 */ 12474 if (help->dthps_nprovs > 0) { 12475 mutex_enter(&dtrace_meta_lock); 12476 if (dtrace_meta_pid != NULL) { 12477 ASSERT(dtrace_deferred_pid == NULL); 12478 12479 for (i = 0; i < help->dthps_nprovs; i++) { 12480 dtrace_helper_remove( 12481 &help->dthps_provs[i]->dthp_prov, p->p_pid); 12482 } 12483 } else { 12484 mutex_enter(&dtrace_lock); 12485 ASSERT(dtrace_deferred_pid != NULL); 12486 12487 /* 12488 * Remove the helper from the deferred list. 12489 */ 12490 if (help->dthps_next != NULL) 12491 help->dthps_next->dthps_prev = help->dthps_prev; 12492 if (help->dthps_prev != NULL) 12493 help->dthps_prev->dthps_next = help->dthps_next; 12494 if (dtrace_deferred_pid == help) { 12495 dtrace_deferred_pid = help->dthps_next; 12496 ASSERT(help->dthps_prev == NULL); 12497 } 12498 12499 mutex_exit(&dtrace_lock); 12500 } 12501 12502 mutex_exit(&dtrace_meta_lock); 12503 12504 for (i = 0; i < help->dthps_nprovs; i++) { 12505 dtrace_helper_provider_remove(help->dthps_provs[i]); 12506 } 12507 12508 kmem_free(help->dthps_provs, help->dthps_nprovs * 12509 sizeof (dtrace_helper_provider_t *)); 12510 } 12511 12512 mutex_enter(&dtrace_lock); 12513 12514 dtrace_vstate_fini(&help->dthps_vstate); 12515 kmem_free(help->dthps_actions, 12516 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 12517 kmem_free(help, sizeof (dtrace_helpers_t)); 12518 12519 --dtrace_helpers; 12520 mutex_exit(&dtrace_lock); 12521 } 12522 12523 static void 12524 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 12525 { 12526 dtrace_helpers_t *help, *newhelp; 12527 dtrace_helper_action_t *helper, *new, *last; 12528 dtrace_difo_t *dp; 12529 dtrace_vstate_t *vstate; 12530 int i, j, sz, hasprovs = 0; 12531 12532 mutex_enter(&dtrace_lock); 12533 ASSERT(from->p_dtrace_helpers != NULL); 12534 ASSERT(dtrace_helpers > 0); 12535 12536 help = from->p_dtrace_helpers; 12537 newhelp = dtrace_helpers_create(to); 12538 ASSERT(to->p_dtrace_helpers != NULL); 12539 12540 newhelp->dthps_generation = help->dthps_generation; 12541 vstate = &newhelp->dthps_vstate; 12542 12543 /* 12544 * Duplicate the helper actions. 12545 */ 12546 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12547 if ((helper = help->dthps_actions[i]) == NULL) 12548 continue; 12549 12550 for (last = NULL; helper != NULL; helper = helper->dthp_next) { 12551 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 12552 KM_SLEEP); 12553 new->dthp_generation = helper->dthp_generation; 12554 12555 if ((dp = helper->dthp_predicate) != NULL) { 12556 dp = dtrace_difo_duplicate(dp, vstate); 12557 new->dthp_predicate = dp; 12558 } 12559 12560 new->dthp_nactions = helper->dthp_nactions; 12561 sz = sizeof (dtrace_difo_t *) * new->dthp_nactions; 12562 new->dthp_actions = kmem_alloc(sz, KM_SLEEP); 12563 12564 for (j = 0; j < new->dthp_nactions; j++) { 12565 dtrace_difo_t *dp = helper->dthp_actions[j]; 12566 12567 ASSERT(dp != NULL); 12568 dp = dtrace_difo_duplicate(dp, vstate); 12569 new->dthp_actions[j] = dp; 12570 } 12571 12572 if (last != NULL) { 12573 last->dthp_next = new; 12574 } else { 12575 newhelp->dthps_actions[i] = new; 12576 } 12577 12578 last = new; 12579 } 12580 } 12581 12582 /* 12583 * Duplicate the helper providers and register them with the 12584 * DTrace framework. 12585 */ 12586 if (help->dthps_nprovs > 0) { 12587 newhelp->dthps_nprovs = help->dthps_nprovs; 12588 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 12589 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 12590 for (i = 0; i < newhelp->dthps_nprovs; i++) { 12591 newhelp->dthps_provs[i] = help->dthps_provs[i]; 12592 newhelp->dthps_provs[i]->dthp_ref++; 12593 } 12594 12595 hasprovs = 1; 12596 } 12597 12598 mutex_exit(&dtrace_lock); 12599 12600 if (hasprovs) 12601 dtrace_helper_provider_register(to, newhelp, NULL); 12602 } 12603 12604 /* 12605 * DTrace Hook Functions 12606 */ 12607 static void 12608 dtrace_module_loaded(struct modctl *ctl) 12609 { 12610 dtrace_provider_t *prv; 12611 12612 mutex_enter(&dtrace_provider_lock); 12613 mutex_enter(&mod_lock); 12614 12615 ASSERT(ctl->mod_busy); 12616 12617 /* 12618 * We're going to call each providers per-module provide operation 12619 * specifying only this module. 12620 */ 12621 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 12622 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 12623 12624 mutex_exit(&mod_lock); 12625 mutex_exit(&dtrace_provider_lock); 12626 12627 /* 12628 * If we have any retained enablings, we need to match against them. 12629 * Enabling probes requires that cpu_lock be held, and we cannot hold 12630 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 12631 * module. (In particular, this happens when loading scheduling 12632 * classes.) So if we have any retained enablings, we need to dispatch 12633 * our task queue to do the match for us. 12634 */ 12635 mutex_enter(&dtrace_lock); 12636 12637 if (dtrace_retained == NULL) { 12638 mutex_exit(&dtrace_lock); 12639 return; 12640 } 12641 12642 (void) taskq_dispatch(dtrace_taskq, 12643 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 12644 12645 mutex_exit(&dtrace_lock); 12646 12647 /* 12648 * And now, for a little heuristic sleaze: in general, we want to 12649 * match modules as soon as they load. However, we cannot guarantee 12650 * this, because it would lead us to the lock ordering violation 12651 * outlined above. The common case, of course, is that cpu_lock is 12652 * _not_ held -- so we delay here for a clock tick, hoping that that's 12653 * long enough for the task queue to do its work. If it's not, it's 12654 * not a serious problem -- it just means that the module that we 12655 * just loaded may not be immediately instrumentable. 12656 */ 12657 delay(1); 12658 } 12659 12660 static void 12661 dtrace_module_unloaded(struct modctl *ctl) 12662 { 12663 dtrace_probe_t template, *probe, *first, *next; 12664 dtrace_provider_t *prov; 12665 12666 template.dtpr_mod = ctl->mod_modname; 12667 12668 mutex_enter(&dtrace_provider_lock); 12669 mutex_enter(&mod_lock); 12670 mutex_enter(&dtrace_lock); 12671 12672 if (dtrace_bymod == NULL) { 12673 /* 12674 * The DTrace module is loaded (obviously) but not attached; 12675 * we don't have any work to do. 12676 */ 12677 mutex_exit(&dtrace_provider_lock); 12678 mutex_exit(&mod_lock); 12679 mutex_exit(&dtrace_lock); 12680 return; 12681 } 12682 12683 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 12684 probe != NULL; probe = probe->dtpr_nextmod) { 12685 if (probe->dtpr_ecb != NULL) { 12686 mutex_exit(&dtrace_provider_lock); 12687 mutex_exit(&mod_lock); 12688 mutex_exit(&dtrace_lock); 12689 12690 /* 12691 * This shouldn't _actually_ be possible -- we're 12692 * unloading a module that has an enabled probe in it. 12693 * (It's normally up to the provider to make sure that 12694 * this can't happen.) However, because dtps_enable() 12695 * doesn't have a failure mode, there can be an 12696 * enable/unload race. Upshot: we don't want to 12697 * assert, but we're not going to disable the 12698 * probe, either. 12699 */ 12700 if (dtrace_err_verbose) { 12701 cmn_err(CE_WARN, "unloaded module '%s' had " 12702 "enabled probes", ctl->mod_modname); 12703 } 12704 12705 return; 12706 } 12707 } 12708 12709 probe = first; 12710 12711 for (first = NULL; probe != NULL; probe = next) { 12712 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 12713 12714 dtrace_probes[probe->dtpr_id - 1] = NULL; 12715 12716 next = probe->dtpr_nextmod; 12717 dtrace_hash_remove(dtrace_bymod, probe); 12718 dtrace_hash_remove(dtrace_byfunc, probe); 12719 dtrace_hash_remove(dtrace_byname, probe); 12720 12721 if (first == NULL) { 12722 first = probe; 12723 probe->dtpr_nextmod = NULL; 12724 } else { 12725 probe->dtpr_nextmod = first; 12726 first = probe; 12727 } 12728 } 12729 12730 /* 12731 * We've removed all of the module's probes from the hash chains and 12732 * from the probe array. Now issue a dtrace_sync() to be sure that 12733 * everyone has cleared out from any probe array processing. 12734 */ 12735 dtrace_sync(); 12736 12737 for (probe = first; probe != NULL; probe = first) { 12738 first = probe->dtpr_nextmod; 12739 prov = probe->dtpr_provider; 12740 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 12741 probe->dtpr_arg); 12742 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 12743 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 12744 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 12745 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 12746 kmem_free(probe, sizeof (dtrace_probe_t)); 12747 } 12748 12749 mutex_exit(&dtrace_lock); 12750 mutex_exit(&mod_lock); 12751 mutex_exit(&dtrace_provider_lock); 12752 } 12753 12754 void 12755 dtrace_suspend(void) 12756 { 12757 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 12758 } 12759 12760 void 12761 dtrace_resume(void) 12762 { 12763 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 12764 } 12765 12766 static int 12767 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 12768 { 12769 ASSERT(MUTEX_HELD(&cpu_lock)); 12770 mutex_enter(&dtrace_lock); 12771 12772 switch (what) { 12773 case CPU_CONFIG: { 12774 dtrace_state_t *state; 12775 dtrace_optval_t *opt, rs, c; 12776 12777 /* 12778 * For now, we only allocate a new buffer for anonymous state. 12779 */ 12780 if ((state = dtrace_anon.dta_state) == NULL) 12781 break; 12782 12783 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 12784 break; 12785 12786 opt = state->dts_options; 12787 c = opt[DTRACEOPT_CPU]; 12788 12789 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 12790 break; 12791 12792 /* 12793 * Regardless of what the actual policy is, we're going to 12794 * temporarily set our resize policy to be manual. We're 12795 * also going to temporarily set our CPU option to denote 12796 * the newly configured CPU. 12797 */ 12798 rs = opt[DTRACEOPT_BUFRESIZE]; 12799 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 12800 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 12801 12802 (void) dtrace_state_buffers(state); 12803 12804 opt[DTRACEOPT_BUFRESIZE] = rs; 12805 opt[DTRACEOPT_CPU] = c; 12806 12807 break; 12808 } 12809 12810 case CPU_UNCONFIG: 12811 /* 12812 * We don't free the buffer in the CPU_UNCONFIG case. (The 12813 * buffer will be freed when the consumer exits.) 12814 */ 12815 break; 12816 12817 default: 12818 break; 12819 } 12820 12821 mutex_exit(&dtrace_lock); 12822 return (0); 12823 } 12824 12825 static void 12826 dtrace_cpu_setup_initial(processorid_t cpu) 12827 { 12828 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 12829 } 12830 12831 static void 12832 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 12833 { 12834 if (dtrace_toxranges >= dtrace_toxranges_max) { 12835 int osize, nsize; 12836 dtrace_toxrange_t *range; 12837 12838 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 12839 12840 if (osize == 0) { 12841 ASSERT(dtrace_toxrange == NULL); 12842 ASSERT(dtrace_toxranges_max == 0); 12843 dtrace_toxranges_max = 1; 12844 } else { 12845 dtrace_toxranges_max <<= 1; 12846 } 12847 12848 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 12849 range = kmem_zalloc(nsize, KM_SLEEP); 12850 12851 if (dtrace_toxrange != NULL) { 12852 ASSERT(osize != 0); 12853 bcopy(dtrace_toxrange, range, osize); 12854 kmem_free(dtrace_toxrange, osize); 12855 } 12856 12857 dtrace_toxrange = range; 12858 } 12859 12860 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); 12861 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); 12862 12863 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 12864 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 12865 dtrace_toxranges++; 12866 } 12867 12868 /* 12869 * DTrace Driver Cookbook Functions 12870 */ 12871 /*ARGSUSED*/ 12872 static int 12873 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 12874 { 12875 dtrace_provider_id_t id; 12876 dtrace_state_t *state = NULL; 12877 dtrace_enabling_t *enab; 12878 12879 mutex_enter(&cpu_lock); 12880 mutex_enter(&dtrace_provider_lock); 12881 mutex_enter(&dtrace_lock); 12882 12883 if (ddi_soft_state_init(&dtrace_softstate, sizeof (dtrace_state_t) + 12884 NCPU * sizeof (dtrace_buffer_t), 0) != 0) { 12885 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 12886 mutex_exit(&cpu_lock); 12887 mutex_exit(&dtrace_provider_lock); 12888 mutex_exit(&dtrace_lock); 12889 return (DDI_FAILURE); 12890 } 12891 12892 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 12893 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 12894 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 12895 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 12896 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 12897 ddi_remove_minor_node(devi, NULL); 12898 ddi_soft_state_fini(&dtrace_softstate); 12899 mutex_exit(&cpu_lock); 12900 mutex_exit(&dtrace_provider_lock); 12901 mutex_exit(&dtrace_lock); 12902 return (DDI_FAILURE); 12903 } 12904 12905 ddi_report_dev(devi); 12906 dtrace_devi = devi; 12907 12908 dtrace_modload = dtrace_module_loaded; 12909 dtrace_modunload = dtrace_module_unloaded; 12910 dtrace_cpu_init = dtrace_cpu_setup_initial; 12911 dtrace_helpers_cleanup = dtrace_helpers_destroy; 12912 dtrace_helpers_fork = dtrace_helpers_duplicate; 12913 dtrace_cpustart_init = dtrace_suspend; 12914 dtrace_cpustart_fini = dtrace_resume; 12915 dtrace_debugger_init = dtrace_suspend; 12916 dtrace_debugger_fini = dtrace_resume; 12917 dtrace_kreloc_init = dtrace_suspend; 12918 dtrace_kreloc_fini = dtrace_resume; 12919 12920 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 12921 12922 ASSERT(MUTEX_HELD(&cpu_lock)); 12923 12924 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 12925 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 12926 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 12927 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 12928 VM_SLEEP | VMC_IDENTIFIER); 12929 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 12930 1, INT_MAX, 0); 12931 12932 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 12933 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 12934 NULL, NULL, NULL, NULL, NULL, 0); 12935 12936 ASSERT(MUTEX_HELD(&cpu_lock)); 12937 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 12938 offsetof(dtrace_probe_t, dtpr_nextmod), 12939 offsetof(dtrace_probe_t, dtpr_prevmod)); 12940 12941 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 12942 offsetof(dtrace_probe_t, dtpr_nextfunc), 12943 offsetof(dtrace_probe_t, dtpr_prevfunc)); 12944 12945 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 12946 offsetof(dtrace_probe_t, dtpr_nextname), 12947 offsetof(dtrace_probe_t, dtpr_prevname)); 12948 12949 if (dtrace_retain_max < 1) { 12950 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 12951 "setting to 1", dtrace_retain_max); 12952 dtrace_retain_max = 1; 12953 } 12954 12955 /* 12956 * Now discover our toxic ranges. 12957 */ 12958 dtrace_toxic_ranges(dtrace_toxrange_add); 12959 12960 /* 12961 * Before we register ourselves as a provider to our own framework, 12962 * we would like to assert that dtrace_provider is NULL -- but that's 12963 * not true if we were loaded as a dependency of a DTrace provider. 12964 * Once we've registered, we can assert that dtrace_provider is our 12965 * pseudo provider. 12966 */ 12967 (void) dtrace_register("dtrace", &dtrace_provider_attr, 12968 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 12969 12970 ASSERT(dtrace_provider != NULL); 12971 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 12972 12973 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 12974 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 12975 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 12976 dtrace_provider, NULL, NULL, "END", 0, NULL); 12977 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 12978 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 12979 12980 dtrace_anon_property(); 12981 mutex_exit(&cpu_lock); 12982 12983 /* 12984 * If DTrace helper tracing is enabled, we need to allocate the 12985 * trace buffer and initialize the values. 12986 */ 12987 if (dtrace_helptrace_enabled) { 12988 ASSERT(dtrace_helptrace_buffer == NULL); 12989 dtrace_helptrace_buffer = 12990 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 12991 dtrace_helptrace_next = 0; 12992 } 12993 12994 /* 12995 * If there are already providers, we must ask them to provide their 12996 * probes, and then match any anonymous enabling against them. Note 12997 * that there should be no other retained enablings at this time: 12998 * the only retained enablings at this time should be the anonymous 12999 * enabling. 13000 */ 13001 if (dtrace_anon.dta_enabling != NULL) { 13002 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 13003 13004 dtrace_enabling_provide(NULL); 13005 state = dtrace_anon.dta_state; 13006 13007 /* 13008 * We couldn't hold cpu_lock across the above call to 13009 * dtrace_enabling_provide(), but we must hold it to actually 13010 * enable the probes. We have to drop all of our locks, pick 13011 * up cpu_lock, and regain our locks before matching the 13012 * retained anonymous enabling. 13013 */ 13014 mutex_exit(&dtrace_lock); 13015 mutex_exit(&dtrace_provider_lock); 13016 13017 mutex_enter(&cpu_lock); 13018 mutex_enter(&dtrace_provider_lock); 13019 mutex_enter(&dtrace_lock); 13020 13021 if ((enab = dtrace_anon.dta_enabling) != NULL) 13022 (void) dtrace_enabling_match(enab, NULL); 13023 13024 mutex_exit(&cpu_lock); 13025 } 13026 13027 mutex_exit(&dtrace_lock); 13028 mutex_exit(&dtrace_provider_lock); 13029 13030 if (state != NULL) { 13031 /* 13032 * If we created any anonymous state, set it going now. 13033 */ 13034 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 13035 } 13036 13037 return (DDI_SUCCESS); 13038 } 13039 13040 /*ARGSUSED*/ 13041 static int 13042 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 13043 { 13044 dtrace_state_t *state; 13045 uint32_t priv; 13046 uid_t uid; 13047 13048 if (getminor(*devp) == DTRACEMNRN_HELPER) 13049 return (0); 13050 13051 /* 13052 * If this wasn't an open with the "helper" minor, then it must be 13053 * the "dtrace" minor. 13054 */ 13055 ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE); 13056 13057 /* 13058 * If no DTRACE_PRIV_* bits are set in the credential, then the 13059 * caller lacks sufficient permission to do anything with DTrace. 13060 */ 13061 dtrace_cred2priv(cred_p, &priv, &uid); 13062 if (priv == DTRACE_PRIV_NONE) 13063 return (EACCES); 13064 13065 /* 13066 * Ask all providers to provide all their probes. 13067 */ 13068 mutex_enter(&dtrace_provider_lock); 13069 dtrace_probe_provide(NULL, NULL); 13070 mutex_exit(&dtrace_provider_lock); 13071 13072 mutex_enter(&cpu_lock); 13073 mutex_enter(&dtrace_lock); 13074 dtrace_opens++; 13075 dtrace_membar_producer(); 13076 13077 /* 13078 * If the kernel debugger is active (that is, if the kernel debugger 13079 * modified text in some way), we won't allow the open. 13080 */ 13081 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13082 dtrace_opens--; 13083 mutex_exit(&cpu_lock); 13084 mutex_exit(&dtrace_lock); 13085 return (EBUSY); 13086 } 13087 13088 state = dtrace_state_create(devp, cred_p); 13089 mutex_exit(&cpu_lock); 13090 13091 if (state == NULL) { 13092 if (--dtrace_opens == 0) 13093 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13094 mutex_exit(&dtrace_lock); 13095 return (EAGAIN); 13096 } 13097 13098 mutex_exit(&dtrace_lock); 13099 13100 return (0); 13101 } 13102 13103 /*ARGSUSED*/ 13104 static int 13105 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 13106 { 13107 minor_t minor = getminor(dev); 13108 dtrace_state_t *state; 13109 13110 if (minor == DTRACEMNRN_HELPER) 13111 return (0); 13112 13113 state = ddi_get_soft_state(dtrace_softstate, minor); 13114 13115 mutex_enter(&cpu_lock); 13116 mutex_enter(&dtrace_lock); 13117 13118 if (state->dts_anon) { 13119 /* 13120 * There is anonymous state. Destroy that first. 13121 */ 13122 ASSERT(dtrace_anon.dta_state == NULL); 13123 dtrace_state_destroy(state->dts_anon); 13124 } 13125 13126 dtrace_state_destroy(state); 13127 ASSERT(dtrace_opens > 0); 13128 if (--dtrace_opens == 0) 13129 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13130 13131 mutex_exit(&dtrace_lock); 13132 mutex_exit(&cpu_lock); 13133 13134 return (0); 13135 } 13136 13137 /*ARGSUSED*/ 13138 static int 13139 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 13140 { 13141 int rval; 13142 dof_helper_t help, *dhp = NULL; 13143 13144 switch (cmd) { 13145 case DTRACEHIOC_ADDDOF: 13146 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 13147 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 13148 return (EFAULT); 13149 } 13150 13151 dhp = &help; 13152 arg = (intptr_t)help.dofhp_dof; 13153 /*FALLTHROUGH*/ 13154 13155 case DTRACEHIOC_ADD: { 13156 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 13157 13158 if (dof == NULL) 13159 return (rval); 13160 13161 mutex_enter(&dtrace_lock); 13162 dtrace_err = 0; 13163 13164 /* 13165 * dtrace_helper_slurp() takes responsibility for the dof -- 13166 * it may free it now or it may save it and free it later. 13167 */ 13168 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 13169 *rv = rval; 13170 rval = 0; 13171 } else { 13172 rval = EINVAL; 13173 } 13174 13175 mutex_exit(&dtrace_lock); 13176 return (rval); 13177 } 13178 13179 case DTRACEHIOC_REMOVE: { 13180 mutex_enter(&dtrace_lock); 13181 rval = dtrace_helper_destroygen(arg); 13182 mutex_exit(&dtrace_lock); 13183 13184 return (rval); 13185 } 13186 13187 default: 13188 break; 13189 } 13190 13191 return (ENOTTY); 13192 } 13193 13194 /*ARGSUSED*/ 13195 static int 13196 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 13197 { 13198 minor_t minor = getminor(dev); 13199 dtrace_state_t *state; 13200 int rval; 13201 13202 if (minor == DTRACEMNRN_HELPER) 13203 return (dtrace_ioctl_helper(cmd, arg, rv)); 13204 13205 state = ddi_get_soft_state(dtrace_softstate, minor); 13206 13207 if (state->dts_anon) { 13208 ASSERT(dtrace_anon.dta_state == NULL); 13209 state = state->dts_anon; 13210 } 13211 13212 switch (cmd) { 13213 case DTRACEIOC_PROVIDER: { 13214 dtrace_providerdesc_t pvd; 13215 dtrace_provider_t *pvp; 13216 13217 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 13218 return (EFAULT); 13219 13220 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 13221 mutex_enter(&dtrace_provider_lock); 13222 13223 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 13224 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 13225 break; 13226 } 13227 13228 mutex_exit(&dtrace_provider_lock); 13229 13230 if (pvp == NULL) 13231 return (ESRCH); 13232 13233 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 13234 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 13235 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 13236 return (EFAULT); 13237 13238 return (0); 13239 } 13240 13241 case DTRACEIOC_EPROBE: { 13242 dtrace_eprobedesc_t epdesc; 13243 dtrace_ecb_t *ecb; 13244 dtrace_action_t *act; 13245 void *buf; 13246 size_t size; 13247 uintptr_t dest; 13248 int nrecs; 13249 13250 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 13251 return (EFAULT); 13252 13253 mutex_enter(&dtrace_lock); 13254 13255 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 13256 mutex_exit(&dtrace_lock); 13257 return (EINVAL); 13258 } 13259 13260 if (ecb->dte_probe == NULL) { 13261 mutex_exit(&dtrace_lock); 13262 return (EINVAL); 13263 } 13264 13265 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 13266 epdesc.dtepd_uarg = ecb->dte_uarg; 13267 epdesc.dtepd_size = ecb->dte_size; 13268 13269 nrecs = epdesc.dtepd_nrecs; 13270 epdesc.dtepd_nrecs = 0; 13271 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 13272 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 13273 continue; 13274 13275 epdesc.dtepd_nrecs++; 13276 } 13277 13278 /* 13279 * Now that we have the size, we need to allocate a temporary 13280 * buffer in which to store the complete description. We need 13281 * the temporary buffer to be able to drop dtrace_lock() 13282 * across the copyout(), below. 13283 */ 13284 size = sizeof (dtrace_eprobedesc_t) + 13285 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 13286 13287 buf = kmem_alloc(size, KM_SLEEP); 13288 dest = (uintptr_t)buf; 13289 13290 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 13291 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 13292 13293 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 13294 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 13295 continue; 13296 13297 if (nrecs-- == 0) 13298 break; 13299 13300 bcopy(&act->dta_rec, (void *)dest, 13301 sizeof (dtrace_recdesc_t)); 13302 dest += sizeof (dtrace_recdesc_t); 13303 } 13304 13305 mutex_exit(&dtrace_lock); 13306 13307 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 13308 kmem_free(buf, size); 13309 return (EFAULT); 13310 } 13311 13312 kmem_free(buf, size); 13313 return (0); 13314 } 13315 13316 case DTRACEIOC_AGGDESC: { 13317 dtrace_aggdesc_t aggdesc; 13318 dtrace_action_t *act; 13319 dtrace_aggregation_t *agg; 13320 int nrecs; 13321 uint32_t offs; 13322 dtrace_recdesc_t *lrec; 13323 void *buf; 13324 size_t size; 13325 uintptr_t dest; 13326 13327 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 13328 return (EFAULT); 13329 13330 mutex_enter(&dtrace_lock); 13331 13332 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 13333 mutex_exit(&dtrace_lock); 13334 return (EINVAL); 13335 } 13336 13337 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 13338 13339 nrecs = aggdesc.dtagd_nrecs; 13340 aggdesc.dtagd_nrecs = 0; 13341 13342 offs = agg->dtag_base; 13343 lrec = &agg->dtag_action.dta_rec; 13344 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 13345 13346 for (act = agg->dtag_first; ; act = act->dta_next) { 13347 ASSERT(act->dta_intuple || 13348 DTRACEACT_ISAGG(act->dta_kind)); 13349 aggdesc.dtagd_nrecs++; 13350 13351 if (act == &agg->dtag_action) 13352 break; 13353 } 13354 13355 /* 13356 * Now that we have the size, we need to allocate a temporary 13357 * buffer in which to store the complete description. We need 13358 * the temporary buffer to be able to drop dtrace_lock() 13359 * across the copyout(), below. 13360 */ 13361 size = sizeof (dtrace_aggdesc_t) + 13362 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 13363 13364 buf = kmem_alloc(size, KM_SLEEP); 13365 dest = (uintptr_t)buf; 13366 13367 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 13368 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 13369 13370 for (act = agg->dtag_first; ; act = act->dta_next) { 13371 dtrace_recdesc_t rec = act->dta_rec; 13372 13373 if (nrecs-- == 0) 13374 break; 13375 13376 rec.dtrd_offset -= offs; 13377 bcopy(&rec, (void *)dest, sizeof (rec)); 13378 dest += sizeof (dtrace_recdesc_t); 13379 13380 if (act == &agg->dtag_action) 13381 break; 13382 } 13383 13384 mutex_exit(&dtrace_lock); 13385 13386 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 13387 kmem_free(buf, size); 13388 return (EFAULT); 13389 } 13390 13391 kmem_free(buf, size); 13392 return (0); 13393 } 13394 13395 case DTRACEIOC_ENABLE: { 13396 dof_hdr_t *dof; 13397 dtrace_enabling_t *enab = NULL; 13398 dtrace_vstate_t *vstate; 13399 int err = 0; 13400 13401 *rv = 0; 13402 13403 /* 13404 * If a NULL argument has been passed, we take this as our 13405 * cue to reevaluate our enablings. 13406 */ 13407 if (arg == NULL) { 13408 mutex_enter(&cpu_lock); 13409 mutex_enter(&dtrace_lock); 13410 err = dtrace_enabling_matchstate(state, rv); 13411 mutex_exit(&dtrace_lock); 13412 mutex_exit(&cpu_lock); 13413 13414 return (err); 13415 } 13416 13417 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 13418 return (rval); 13419 13420 mutex_enter(&cpu_lock); 13421 mutex_enter(&dtrace_lock); 13422 vstate = &state->dts_vstate; 13423 13424 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13425 mutex_exit(&dtrace_lock); 13426 mutex_exit(&cpu_lock); 13427 dtrace_dof_destroy(dof); 13428 return (EBUSY); 13429 } 13430 13431 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 13432 mutex_exit(&dtrace_lock); 13433 mutex_exit(&cpu_lock); 13434 dtrace_dof_destroy(dof); 13435 return (EINVAL); 13436 } 13437 13438 if ((rval = dtrace_dof_options(dof, state)) != 0) { 13439 dtrace_enabling_destroy(enab); 13440 mutex_exit(&dtrace_lock); 13441 mutex_exit(&cpu_lock); 13442 dtrace_dof_destroy(dof); 13443 return (rval); 13444 } 13445 13446 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 13447 err = dtrace_enabling_retain(enab); 13448 } else { 13449 dtrace_enabling_destroy(enab); 13450 } 13451 13452 mutex_exit(&cpu_lock); 13453 mutex_exit(&dtrace_lock); 13454 dtrace_dof_destroy(dof); 13455 13456 return (err); 13457 } 13458 13459 case DTRACEIOC_REPLICATE: { 13460 dtrace_repldesc_t desc; 13461 dtrace_probedesc_t *match = &desc.dtrpd_match; 13462 dtrace_probedesc_t *create = &desc.dtrpd_create; 13463 int err; 13464 13465 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13466 return (EFAULT); 13467 13468 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13469 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13470 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13471 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13472 13473 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13474 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13475 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13476 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13477 13478 mutex_enter(&dtrace_lock); 13479 err = dtrace_enabling_replicate(state, match, create); 13480 mutex_exit(&dtrace_lock); 13481 13482 return (err); 13483 } 13484 13485 case DTRACEIOC_PROBEMATCH: 13486 case DTRACEIOC_PROBES: { 13487 dtrace_probe_t *probe = NULL; 13488 dtrace_probedesc_t desc; 13489 dtrace_probekey_t pkey; 13490 dtrace_id_t i; 13491 int m = 0; 13492 uint32_t priv; 13493 uid_t uid; 13494 13495 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13496 return (EFAULT); 13497 13498 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13499 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13500 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13501 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13502 13503 /* 13504 * Before we attempt to match this probe, we want to give 13505 * all providers the opportunity to provide it. 13506 */ 13507 if (desc.dtpd_id == DTRACE_IDNONE) { 13508 mutex_enter(&dtrace_provider_lock); 13509 dtrace_probe_provide(&desc, NULL); 13510 mutex_exit(&dtrace_provider_lock); 13511 desc.dtpd_id++; 13512 } 13513 13514 if (cmd == DTRACEIOC_PROBEMATCH) { 13515 dtrace_probekey(&desc, &pkey); 13516 pkey.dtpk_id = DTRACE_IDNONE; 13517 } 13518 13519 uid = crgetuid(cr); 13520 dtrace_cred2priv(cr, &priv, &uid); 13521 13522 mutex_enter(&dtrace_lock); 13523 13524 if (cmd == DTRACEIOC_PROBEMATCH) { 13525 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 13526 if ((probe = dtrace_probes[i - 1]) != NULL && 13527 (m = dtrace_match_probe(probe, &pkey, 13528 priv, uid)) != 0) 13529 break; 13530 } 13531 13532 if (m < 0) { 13533 mutex_exit(&dtrace_lock); 13534 return (EINVAL); 13535 } 13536 13537 } else { 13538 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 13539 if ((probe = dtrace_probes[i - 1]) != NULL && 13540 dtrace_match_priv(probe, priv, uid)) 13541 break; 13542 } 13543 } 13544 13545 if (probe == NULL) { 13546 mutex_exit(&dtrace_lock); 13547 return (ESRCH); 13548 } 13549 13550 dtrace_probe_description(probe, &desc); 13551 mutex_exit(&dtrace_lock); 13552 13553 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13554 return (EFAULT); 13555 13556 return (0); 13557 } 13558 13559 case DTRACEIOC_PROBEARG: { 13560 dtrace_argdesc_t desc; 13561 dtrace_probe_t *probe; 13562 dtrace_provider_t *prov; 13563 13564 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13565 return (EFAULT); 13566 13567 if (desc.dtargd_id == DTRACE_IDNONE) 13568 return (EINVAL); 13569 13570 if (desc.dtargd_ndx == DTRACE_ARGNONE) 13571 return (EINVAL); 13572 13573 mutex_enter(&dtrace_provider_lock); 13574 mutex_enter(&mod_lock); 13575 mutex_enter(&dtrace_lock); 13576 13577 if (desc.dtargd_id > dtrace_nprobes) { 13578 mutex_exit(&dtrace_lock); 13579 mutex_exit(&mod_lock); 13580 mutex_exit(&dtrace_provider_lock); 13581 return (EINVAL); 13582 } 13583 13584 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 13585 mutex_exit(&dtrace_lock); 13586 mutex_exit(&mod_lock); 13587 mutex_exit(&dtrace_provider_lock); 13588 return (EINVAL); 13589 } 13590 13591 mutex_exit(&dtrace_lock); 13592 13593 prov = probe->dtpr_provider; 13594 13595 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 13596 /* 13597 * There isn't any typed information for this probe. 13598 * Set the argument number to DTRACE_ARGNONE. 13599 */ 13600 desc.dtargd_ndx = DTRACE_ARGNONE; 13601 } else { 13602 desc.dtargd_native[0] = '\0'; 13603 desc.dtargd_xlate[0] = '\0'; 13604 desc.dtargd_mapping = desc.dtargd_ndx; 13605 13606 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 13607 probe->dtpr_id, probe->dtpr_arg, &desc); 13608 } 13609 13610 mutex_exit(&mod_lock); 13611 mutex_exit(&dtrace_provider_lock); 13612 13613 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13614 return (EFAULT); 13615 13616 return (0); 13617 } 13618 13619 case DTRACEIOC_GO: { 13620 processorid_t cpuid; 13621 rval = dtrace_state_go(state, &cpuid); 13622 13623 if (rval != 0) 13624 return (rval); 13625 13626 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 13627 return (EFAULT); 13628 13629 return (0); 13630 } 13631 13632 case DTRACEIOC_STOP: { 13633 processorid_t cpuid; 13634 13635 mutex_enter(&dtrace_lock); 13636 rval = dtrace_state_stop(state, &cpuid); 13637 mutex_exit(&dtrace_lock); 13638 13639 if (rval != 0) 13640 return (rval); 13641 13642 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 13643 return (EFAULT); 13644 13645 return (0); 13646 } 13647 13648 case DTRACEIOC_DOFGET: { 13649 dof_hdr_t hdr, *dof; 13650 uint64_t len; 13651 13652 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 13653 return (EFAULT); 13654 13655 mutex_enter(&dtrace_lock); 13656 dof = dtrace_dof_create(state); 13657 mutex_exit(&dtrace_lock); 13658 13659 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 13660 rval = copyout(dof, (void *)arg, len); 13661 dtrace_dof_destroy(dof); 13662 13663 return (rval == 0 ? 0 : EFAULT); 13664 } 13665 13666 case DTRACEIOC_AGGSNAP: 13667 case DTRACEIOC_BUFSNAP: { 13668 dtrace_bufdesc_t desc; 13669 caddr_t cached; 13670 dtrace_buffer_t *buf; 13671 13672 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13673 return (EFAULT); 13674 13675 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 13676 return (EINVAL); 13677 13678 mutex_enter(&dtrace_lock); 13679 13680 if (cmd == DTRACEIOC_BUFSNAP) { 13681 buf = &state->dts_buffer[desc.dtbd_cpu]; 13682 } else { 13683 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 13684 } 13685 13686 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 13687 size_t sz = buf->dtb_offset; 13688 13689 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 13690 mutex_exit(&dtrace_lock); 13691 return (EBUSY); 13692 } 13693 13694 /* 13695 * If this buffer has already been consumed, we're 13696 * going to indicate that there's nothing left here 13697 * to consume. 13698 */ 13699 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 13700 mutex_exit(&dtrace_lock); 13701 13702 desc.dtbd_size = 0; 13703 desc.dtbd_drops = 0; 13704 desc.dtbd_errors = 0; 13705 desc.dtbd_oldest = 0; 13706 sz = sizeof (desc); 13707 13708 if (copyout(&desc, (void *)arg, sz) != 0) 13709 return (EFAULT); 13710 13711 return (0); 13712 } 13713 13714 /* 13715 * If this is a ring buffer that has wrapped, we want 13716 * to copy the whole thing out. 13717 */ 13718 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 13719 dtrace_buffer_polish(buf); 13720 sz = buf->dtb_size; 13721 } 13722 13723 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 13724 mutex_exit(&dtrace_lock); 13725 return (EFAULT); 13726 } 13727 13728 desc.dtbd_size = sz; 13729 desc.dtbd_drops = buf->dtb_drops; 13730 desc.dtbd_errors = buf->dtb_errors; 13731 desc.dtbd_oldest = buf->dtb_xamot_offset; 13732 13733 mutex_exit(&dtrace_lock); 13734 13735 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13736 return (EFAULT); 13737 13738 buf->dtb_flags |= DTRACEBUF_CONSUMED; 13739 13740 return (0); 13741 } 13742 13743 if (buf->dtb_tomax == NULL) { 13744 ASSERT(buf->dtb_xamot == NULL); 13745 mutex_exit(&dtrace_lock); 13746 return (ENOENT); 13747 } 13748 13749 cached = buf->dtb_tomax; 13750 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 13751 13752 dtrace_xcall(desc.dtbd_cpu, 13753 (dtrace_xcall_t)dtrace_buffer_switch, buf); 13754 13755 state->dts_errors += buf->dtb_xamot_errors; 13756 13757 /* 13758 * If the buffers did not actually switch, then the cross call 13759 * did not take place -- presumably because the given CPU is 13760 * not in the ready set. If this is the case, we'll return 13761 * ENOENT. 13762 */ 13763 if (buf->dtb_tomax == cached) { 13764 ASSERT(buf->dtb_xamot != cached); 13765 mutex_exit(&dtrace_lock); 13766 return (ENOENT); 13767 } 13768 13769 ASSERT(cached == buf->dtb_xamot); 13770 13771 /* 13772 * We have our snapshot; now copy it out. 13773 */ 13774 if (copyout(buf->dtb_xamot, desc.dtbd_data, 13775 buf->dtb_xamot_offset) != 0) { 13776 mutex_exit(&dtrace_lock); 13777 return (EFAULT); 13778 } 13779 13780 desc.dtbd_size = buf->dtb_xamot_offset; 13781 desc.dtbd_drops = buf->dtb_xamot_drops; 13782 desc.dtbd_errors = buf->dtb_xamot_errors; 13783 desc.dtbd_oldest = 0; 13784 13785 mutex_exit(&dtrace_lock); 13786 13787 /* 13788 * Finally, copy out the buffer description. 13789 */ 13790 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13791 return (EFAULT); 13792 13793 return (0); 13794 } 13795 13796 case DTRACEIOC_CONF: { 13797 dtrace_conf_t conf; 13798 13799 bzero(&conf, sizeof (conf)); 13800 conf.dtc_difversion = DIF_VERSION; 13801 conf.dtc_difintregs = DIF_DIR_NREGS; 13802 conf.dtc_diftupregs = DIF_DTR_NREGS; 13803 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 13804 13805 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 13806 return (EFAULT); 13807 13808 return (0); 13809 } 13810 13811 case DTRACEIOC_STATUS: { 13812 dtrace_status_t stat; 13813 dtrace_dstate_t *dstate; 13814 int i, j; 13815 uint64_t nerrs; 13816 13817 /* 13818 * See the comment in dtrace_state_deadman() for the reason 13819 * for setting dts_laststatus to INT64_MAX before setting 13820 * it to the correct value. 13821 */ 13822 state->dts_laststatus = INT64_MAX; 13823 dtrace_membar_producer(); 13824 state->dts_laststatus = dtrace_gethrtime(); 13825 13826 bzero(&stat, sizeof (stat)); 13827 13828 mutex_enter(&dtrace_lock); 13829 13830 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 13831 mutex_exit(&dtrace_lock); 13832 return (ENOENT); 13833 } 13834 13835 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 13836 stat.dtst_exiting = 1; 13837 13838 nerrs = state->dts_errors; 13839 dstate = &state->dts_vstate.dtvs_dynvars; 13840 13841 for (i = 0; i < NCPU; i++) { 13842 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 13843 13844 stat.dtst_dyndrops += dcpu->dtdsc_drops; 13845 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 13846 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 13847 13848 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 13849 stat.dtst_filled++; 13850 13851 nerrs += state->dts_buffer[i].dtb_errors; 13852 13853 for (j = 0; j < state->dts_nspeculations; j++) { 13854 dtrace_speculation_t *spec; 13855 dtrace_buffer_t *buf; 13856 13857 spec = &state->dts_speculations[j]; 13858 buf = &spec->dtsp_buffer[i]; 13859 stat.dtst_specdrops += buf->dtb_xamot_drops; 13860 } 13861 } 13862 13863 stat.dtst_specdrops_busy = state->dts_speculations_busy; 13864 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 13865 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 13866 stat.dtst_dblerrors = state->dts_dblerrors; 13867 stat.dtst_killed = 13868 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 13869 stat.dtst_errors = nerrs; 13870 13871 mutex_exit(&dtrace_lock); 13872 13873 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 13874 return (EFAULT); 13875 13876 return (0); 13877 } 13878 13879 case DTRACEIOC_FORMAT: { 13880 dtrace_fmtdesc_t fmt; 13881 char *str; 13882 int len; 13883 13884 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 13885 return (EFAULT); 13886 13887 mutex_enter(&dtrace_lock); 13888 13889 if (fmt.dtfd_format == 0 || 13890 fmt.dtfd_format > state->dts_nformats) { 13891 mutex_exit(&dtrace_lock); 13892 return (EINVAL); 13893 } 13894 13895 /* 13896 * Format strings are allocated contiguously and they are 13897 * never freed; if a format index is less than the number 13898 * of formats, we can assert that the format map is non-NULL 13899 * and that the format for the specified index is non-NULL. 13900 */ 13901 ASSERT(state->dts_formats != NULL); 13902 str = state->dts_formats[fmt.dtfd_format - 1]; 13903 ASSERT(str != NULL); 13904 13905 len = strlen(str) + 1; 13906 13907 if (len > fmt.dtfd_length) { 13908 fmt.dtfd_length = len; 13909 13910 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 13911 mutex_exit(&dtrace_lock); 13912 return (EINVAL); 13913 } 13914 } else { 13915 if (copyout(str, fmt.dtfd_string, len) != 0) { 13916 mutex_exit(&dtrace_lock); 13917 return (EINVAL); 13918 } 13919 } 13920 13921 mutex_exit(&dtrace_lock); 13922 return (0); 13923 } 13924 13925 default: 13926 break; 13927 } 13928 13929 return (ENOTTY); 13930 } 13931 13932 /*ARGSUSED*/ 13933 static int 13934 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 13935 { 13936 dtrace_state_t *state; 13937 13938 switch (cmd) { 13939 case DDI_DETACH: 13940 break; 13941 13942 case DDI_SUSPEND: 13943 return (DDI_SUCCESS); 13944 13945 default: 13946 return (DDI_FAILURE); 13947 } 13948 13949 mutex_enter(&cpu_lock); 13950 mutex_enter(&dtrace_provider_lock); 13951 mutex_enter(&dtrace_lock); 13952 13953 ASSERT(dtrace_opens == 0); 13954 13955 if (dtrace_helpers > 0) { 13956 mutex_exit(&dtrace_provider_lock); 13957 mutex_exit(&dtrace_lock); 13958 mutex_exit(&cpu_lock); 13959 return (DDI_FAILURE); 13960 } 13961 13962 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 13963 mutex_exit(&dtrace_provider_lock); 13964 mutex_exit(&dtrace_lock); 13965 mutex_exit(&cpu_lock); 13966 return (DDI_FAILURE); 13967 } 13968 13969 dtrace_provider = NULL; 13970 13971 if ((state = dtrace_anon_grab()) != NULL) { 13972 /* 13973 * If there were ECBs on this state, the provider should 13974 * have not been allowed to detach; assert that there is 13975 * none. 13976 */ 13977 ASSERT(state->dts_necbs == 0); 13978 dtrace_state_destroy(state); 13979 13980 /* 13981 * If we're being detached with anonymous state, we need to 13982 * indicate to the kernel debugger that DTrace is now inactive. 13983 */ 13984 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13985 } 13986 13987 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 13988 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 13989 dtrace_cpu_init = NULL; 13990 dtrace_helpers_cleanup = NULL; 13991 dtrace_helpers_fork = NULL; 13992 dtrace_cpustart_init = NULL; 13993 dtrace_cpustart_fini = NULL; 13994 dtrace_debugger_init = NULL; 13995 dtrace_debugger_fini = NULL; 13996 dtrace_kreloc_init = NULL; 13997 dtrace_kreloc_fini = NULL; 13998 dtrace_modload = NULL; 13999 dtrace_modunload = NULL; 14000 14001 mutex_exit(&cpu_lock); 14002 14003 if (dtrace_helptrace_enabled) { 14004 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 14005 dtrace_helptrace_buffer = NULL; 14006 } 14007 14008 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 14009 dtrace_probes = NULL; 14010 dtrace_nprobes = 0; 14011 14012 dtrace_hash_destroy(dtrace_bymod); 14013 dtrace_hash_destroy(dtrace_byfunc); 14014 dtrace_hash_destroy(dtrace_byname); 14015 dtrace_bymod = NULL; 14016 dtrace_byfunc = NULL; 14017 dtrace_byname = NULL; 14018 14019 kmem_cache_destroy(dtrace_state_cache); 14020 vmem_destroy(dtrace_minor); 14021 vmem_destroy(dtrace_arena); 14022 14023 if (dtrace_toxrange != NULL) { 14024 kmem_free(dtrace_toxrange, 14025 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 14026 dtrace_toxrange = NULL; 14027 dtrace_toxranges = 0; 14028 dtrace_toxranges_max = 0; 14029 } 14030 14031 ddi_remove_minor_node(dtrace_devi, NULL); 14032 dtrace_devi = NULL; 14033 14034 ddi_soft_state_fini(&dtrace_softstate); 14035 14036 ASSERT(dtrace_vtime_references == 0); 14037 ASSERT(dtrace_opens == 0); 14038 ASSERT(dtrace_retained == NULL); 14039 14040 mutex_exit(&dtrace_lock); 14041 mutex_exit(&dtrace_provider_lock); 14042 14043 /* 14044 * We don't destroy the task queue until after we have dropped our 14045 * locks (taskq_destroy() may block on running tasks). To prevent 14046 * attempting to do work after we have effectively detached but before 14047 * the task queue has been destroyed, all tasks dispatched via the 14048 * task queue must check that DTrace is still attached before 14049 * performing any operation. 14050 */ 14051 taskq_destroy(dtrace_taskq); 14052 dtrace_taskq = NULL; 14053 14054 return (DDI_SUCCESS); 14055 } 14056 14057 /*ARGSUSED*/ 14058 static int 14059 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 14060 { 14061 int error; 14062 14063 switch (infocmd) { 14064 case DDI_INFO_DEVT2DEVINFO: 14065 *result = (void *)dtrace_devi; 14066 error = DDI_SUCCESS; 14067 break; 14068 case DDI_INFO_DEVT2INSTANCE: 14069 *result = (void *)0; 14070 error = DDI_SUCCESS; 14071 break; 14072 default: 14073 error = DDI_FAILURE; 14074 } 14075 return (error); 14076 } 14077 14078 static struct cb_ops dtrace_cb_ops = { 14079 dtrace_open, /* open */ 14080 dtrace_close, /* close */ 14081 nulldev, /* strategy */ 14082 nulldev, /* print */ 14083 nodev, /* dump */ 14084 nodev, /* read */ 14085 nodev, /* write */ 14086 dtrace_ioctl, /* ioctl */ 14087 nodev, /* devmap */ 14088 nodev, /* mmap */ 14089 nodev, /* segmap */ 14090 nochpoll, /* poll */ 14091 ddi_prop_op, /* cb_prop_op */ 14092 0, /* streamtab */ 14093 D_NEW | D_MP /* Driver compatibility flag */ 14094 }; 14095 14096 static struct dev_ops dtrace_ops = { 14097 DEVO_REV, /* devo_rev */ 14098 0, /* refcnt */ 14099 dtrace_info, /* get_dev_info */ 14100 nulldev, /* identify */ 14101 nulldev, /* probe */ 14102 dtrace_attach, /* attach */ 14103 dtrace_detach, /* detach */ 14104 nodev, /* reset */ 14105 &dtrace_cb_ops, /* driver operations */ 14106 NULL, /* bus operations */ 14107 nodev /* dev power */ 14108 }; 14109 14110 static struct modldrv modldrv = { 14111 &mod_driverops, /* module type (this is a pseudo driver) */ 14112 "Dynamic Tracing", /* name of module */ 14113 &dtrace_ops, /* driver ops */ 14114 }; 14115 14116 static struct modlinkage modlinkage = { 14117 MODREV_1, 14118 (void *)&modldrv, 14119 NULL 14120 }; 14121 14122 int 14123 _init(void) 14124 { 14125 return (mod_install(&modlinkage)); 14126 } 14127 14128 int 14129 _info(struct modinfo *modinfop) 14130 { 14131 return (mod_info(&modlinkage, modinfop)); 14132 } 14133 14134 int 14135 _fini(void) 14136 { 14137 return (mod_remove(&modlinkage)); 14138 } 14139