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 /* 2216 * See comment in DIF_VAR_PID. 2217 */ 2218 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2219 CPU_ON_INTR(CPU)) { 2220 mstate->dtms_ustackdepth = 0; 2221 } else { 2222 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2223 mstate->dtms_ustackdepth = 2224 dtrace_getustackdepth(); 2225 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2226 } 2227 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2228 } 2229 return (mstate->dtms_ustackdepth); 2230 2231 case DIF_VAR_CALLER: 2232 if (!dtrace_priv_kernel(state)) 2233 return (0); 2234 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2235 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2236 2237 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2238 /* 2239 * If this is an unanchored probe, we are 2240 * required to go through the slow path: 2241 * dtrace_caller() only guarantees correct 2242 * results for anchored probes. 2243 */ 2244 pc_t caller[2]; 2245 2246 dtrace_getpcstack(caller, 2, aframes, 2247 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2248 mstate->dtms_caller = caller[1]; 2249 } else if ((mstate->dtms_caller = 2250 dtrace_caller(aframes)) == -1) { 2251 /* 2252 * We have failed to do this the quick way; 2253 * we must resort to the slower approach of 2254 * calling dtrace_getpcstack(). 2255 */ 2256 pc_t caller; 2257 2258 dtrace_getpcstack(&caller, 1, aframes, NULL); 2259 mstate->dtms_caller = caller; 2260 } 2261 2262 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 2263 } 2264 return (mstate->dtms_caller); 2265 2266 case DIF_VAR_UCALLER: 2267 if (!dtrace_priv_proc(state)) 2268 return (0); 2269 2270 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 2271 uint64_t ustack[3]; 2272 2273 /* 2274 * dtrace_getupcstack() fills in the first uint64_t 2275 * with the current PID. The second uint64_t will 2276 * be the program counter at user-level. The third 2277 * uint64_t will contain the caller, which is what 2278 * we're after. 2279 */ 2280 ustack[2] = NULL; 2281 dtrace_getupcstack(ustack, 3); 2282 mstate->dtms_ucaller = ustack[2]; 2283 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 2284 } 2285 2286 return (mstate->dtms_ucaller); 2287 2288 case DIF_VAR_PROBEPROV: 2289 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2290 return ((uint64_t)(uintptr_t) 2291 mstate->dtms_probe->dtpr_provider->dtpv_name); 2292 2293 case DIF_VAR_PROBEMOD: 2294 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2295 return ((uint64_t)(uintptr_t) 2296 mstate->dtms_probe->dtpr_mod); 2297 2298 case DIF_VAR_PROBEFUNC: 2299 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2300 return ((uint64_t)(uintptr_t) 2301 mstate->dtms_probe->dtpr_func); 2302 2303 case DIF_VAR_PROBENAME: 2304 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2305 return ((uint64_t)(uintptr_t) 2306 mstate->dtms_probe->dtpr_name); 2307 2308 case DIF_VAR_PID: 2309 if (!dtrace_priv_proc(state)) 2310 return (0); 2311 2312 /* 2313 * Note that we are assuming that an unanchored probe is 2314 * always due to a high-level interrupt. (And we're assuming 2315 * that there is only a single high level interrupt.) 2316 */ 2317 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2318 return (pid0.pid_id); 2319 2320 /* 2321 * It is always safe to dereference one's own t_procp pointer: 2322 * it always points to a valid, allocated proc structure. 2323 * Further, it is always safe to dereference the p_pidp member 2324 * of one's own proc structure. (These are truisms becuase 2325 * threads and processes don't clean up their own state -- 2326 * they leave that task to whomever reaps them.) 2327 */ 2328 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 2329 2330 case DIF_VAR_TID: 2331 /* 2332 * See comment in DIF_VAR_PID. 2333 */ 2334 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2335 return (0); 2336 2337 return ((uint64_t)curthread->t_tid); 2338 2339 case DIF_VAR_EXECNAME: 2340 if (!dtrace_priv_proc(state)) 2341 return (0); 2342 2343 /* 2344 * See comment in DIF_VAR_PID. 2345 */ 2346 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2347 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 2348 2349 /* 2350 * It is always safe to dereference one's own t_procp pointer: 2351 * it always points to a valid, allocated proc structure. 2352 * (This is true because threads don't clean up their own 2353 * state -- they leave that task to whomever reaps them.) 2354 */ 2355 return ((uint64_t)(uintptr_t) 2356 curthread->t_procp->p_user.u_comm); 2357 2358 case DIF_VAR_ZONENAME: 2359 if (!dtrace_priv_proc(state)) 2360 return (0); 2361 2362 /* 2363 * See comment in DIF_VAR_PID. 2364 */ 2365 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2366 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 2367 2368 /* 2369 * It is always safe to dereference one's own t_procp pointer: 2370 * it always points to a valid, allocated proc structure. 2371 * (This is true because threads don't clean up their own 2372 * state -- they leave that task to whomever reaps them.) 2373 */ 2374 return ((uint64_t)(uintptr_t) 2375 curthread->t_procp->p_zone->zone_name); 2376 2377 default: 2378 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2379 return (0); 2380 } 2381 } 2382 2383 /* 2384 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 2385 * Notice that we don't bother validating the proper number of arguments or 2386 * their types in the tuple stack. This isn't needed because all argument 2387 * interpretation is safe because of our load safety -- the worst that can 2388 * happen is that a bogus program can obtain bogus results. 2389 */ 2390 static void 2391 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 2392 dtrace_key_t *tupregs, int nargs, 2393 dtrace_mstate_t *mstate, dtrace_state_t *state) 2394 { 2395 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 2396 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 2397 2398 union { 2399 mutex_impl_t mi; 2400 uint64_t mx; 2401 } m; 2402 2403 union { 2404 krwlock_t ri; 2405 uintptr_t rw; 2406 } r; 2407 2408 switch (subr) { 2409 case DIF_SUBR_RAND: 2410 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 2411 break; 2412 2413 case DIF_SUBR_MUTEX_OWNED: 2414 m.mx = dtrace_load64(tupregs[0].dttk_value); 2415 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 2416 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 2417 else 2418 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 2419 break; 2420 2421 case DIF_SUBR_MUTEX_OWNER: 2422 m.mx = dtrace_load64(tupregs[0].dttk_value); 2423 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 2424 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 2425 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 2426 else 2427 regs[rd] = 0; 2428 break; 2429 2430 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 2431 m.mx = dtrace_load64(tupregs[0].dttk_value); 2432 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 2433 break; 2434 2435 case DIF_SUBR_MUTEX_TYPE_SPIN: 2436 m.mx = dtrace_load64(tupregs[0].dttk_value); 2437 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 2438 break; 2439 2440 case DIF_SUBR_RW_READ_HELD: { 2441 uintptr_t tmp; 2442 2443 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2444 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 2445 break; 2446 } 2447 2448 case DIF_SUBR_RW_WRITE_HELD: 2449 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2450 regs[rd] = _RW_WRITE_HELD(&r.ri); 2451 break; 2452 2453 case DIF_SUBR_RW_ISWRITER: 2454 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2455 regs[rd] = _RW_ISWRITER(&r.ri); 2456 break; 2457 2458 case DIF_SUBR_BCOPY: { 2459 /* 2460 * We need to be sure that the destination is in the scratch 2461 * region -- no other region is allowed. 2462 */ 2463 uintptr_t src = tupregs[0].dttk_value; 2464 uintptr_t dest = tupregs[1].dttk_value; 2465 size_t size = tupregs[2].dttk_value; 2466 2467 if (!dtrace_inscratch(dest, size, mstate)) { 2468 *flags |= CPU_DTRACE_BADADDR; 2469 *illval = regs[rd]; 2470 break; 2471 } 2472 2473 dtrace_bcopy((void *)src, (void *)dest, size); 2474 break; 2475 } 2476 2477 case DIF_SUBR_ALLOCA: 2478 case DIF_SUBR_COPYIN: { 2479 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 2480 uint64_t size = 2481 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 2482 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 2483 2484 /* 2485 * This action doesn't require any credential checks since 2486 * probes will not activate in user contexts to which the 2487 * enabling user does not have permissions. 2488 */ 2489 if (mstate->dtms_scratch_ptr + scratch_size > 2490 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2491 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2492 regs[rd] = NULL; 2493 break; 2494 } 2495 2496 if (subr == DIF_SUBR_COPYIN) { 2497 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2498 dtrace_copyin(tupregs[0].dttk_value, dest, size); 2499 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2500 } 2501 2502 mstate->dtms_scratch_ptr += scratch_size; 2503 regs[rd] = dest; 2504 break; 2505 } 2506 2507 case DIF_SUBR_COPYINTO: { 2508 uint64_t size = tupregs[1].dttk_value; 2509 uintptr_t dest = tupregs[2].dttk_value; 2510 2511 /* 2512 * This action doesn't require any credential checks since 2513 * probes will not activate in user contexts to which the 2514 * enabling user does not have permissions. 2515 */ 2516 if (!dtrace_inscratch(dest, size, mstate)) { 2517 *flags |= CPU_DTRACE_BADADDR; 2518 *illval = regs[rd]; 2519 break; 2520 } 2521 2522 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2523 dtrace_copyin(tupregs[0].dttk_value, dest, size); 2524 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2525 break; 2526 } 2527 2528 case DIF_SUBR_COPYINSTR: { 2529 uintptr_t dest = mstate->dtms_scratch_ptr; 2530 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2531 2532 if (nargs > 1 && tupregs[1].dttk_value < size) 2533 size = tupregs[1].dttk_value + 1; 2534 2535 /* 2536 * This action doesn't require any credential checks since 2537 * probes will not activate in user contexts to which the 2538 * enabling user does not have permissions. 2539 */ 2540 if (mstate->dtms_scratch_ptr + size > 2541 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2542 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2543 regs[rd] = NULL; 2544 break; 2545 } 2546 2547 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2548 dtrace_copyinstr(tupregs[0].dttk_value, dest, size); 2549 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2550 2551 ((char *)dest)[size - 1] = '\0'; 2552 mstate->dtms_scratch_ptr += size; 2553 regs[rd] = dest; 2554 break; 2555 } 2556 2557 case DIF_SUBR_MSGSIZE: 2558 case DIF_SUBR_MSGDSIZE: { 2559 uintptr_t baddr = tupregs[0].dttk_value, daddr; 2560 uintptr_t wptr, rptr; 2561 size_t count = 0; 2562 int cont = 0; 2563 2564 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 2565 wptr = dtrace_loadptr(baddr + 2566 offsetof(mblk_t, b_wptr)); 2567 2568 rptr = dtrace_loadptr(baddr + 2569 offsetof(mblk_t, b_rptr)); 2570 2571 if (wptr < rptr) { 2572 *flags |= CPU_DTRACE_BADADDR; 2573 *illval = tupregs[0].dttk_value; 2574 break; 2575 } 2576 2577 daddr = dtrace_loadptr(baddr + 2578 offsetof(mblk_t, b_datap)); 2579 2580 baddr = dtrace_loadptr(baddr + 2581 offsetof(mblk_t, b_cont)); 2582 2583 /* 2584 * We want to prevent against denial-of-service here, 2585 * so we're only going to search the list for 2586 * dtrace_msgdsize_max mblks. 2587 */ 2588 if (cont++ > dtrace_msgdsize_max) { 2589 *flags |= CPU_DTRACE_ILLOP; 2590 break; 2591 } 2592 2593 if (subr == DIF_SUBR_MSGDSIZE) { 2594 if (dtrace_load8(daddr + 2595 offsetof(dblk_t, db_type)) != M_DATA) 2596 continue; 2597 } 2598 2599 count += wptr - rptr; 2600 } 2601 2602 if (!(*flags & CPU_DTRACE_FAULT)) 2603 regs[rd] = count; 2604 2605 break; 2606 } 2607 2608 case DIF_SUBR_PROGENYOF: { 2609 pid_t pid = tupregs[0].dttk_value; 2610 proc_t *p; 2611 int rval = 0; 2612 2613 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2614 2615 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 2616 if (p->p_pidp->pid_id == pid) { 2617 rval = 1; 2618 break; 2619 } 2620 } 2621 2622 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2623 2624 regs[rd] = rval; 2625 break; 2626 } 2627 2628 case DIF_SUBR_SPECULATION: 2629 regs[rd] = dtrace_speculation(state); 2630 break; 2631 2632 case DIF_SUBR_COPYOUT: { 2633 uintptr_t kaddr = tupregs[0].dttk_value; 2634 uintptr_t uaddr = tupregs[1].dttk_value; 2635 uint64_t size = tupregs[2].dttk_value; 2636 2637 if (!dtrace_destructive_disallow && 2638 dtrace_priv_proc_control(state) && 2639 !dtrace_istoxic(kaddr, size)) { 2640 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2641 dtrace_copyout(kaddr, uaddr, size); 2642 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2643 } 2644 break; 2645 } 2646 2647 case DIF_SUBR_COPYOUTSTR: { 2648 uintptr_t kaddr = tupregs[0].dttk_value; 2649 uintptr_t uaddr = tupregs[1].dttk_value; 2650 uint64_t size = tupregs[2].dttk_value; 2651 2652 if (!dtrace_destructive_disallow && 2653 dtrace_priv_proc_control(state) && 2654 !dtrace_istoxic(kaddr, size)) { 2655 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2656 dtrace_copyoutstr(kaddr, uaddr, size); 2657 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2658 } 2659 break; 2660 } 2661 2662 case DIF_SUBR_STRLEN: 2663 regs[rd] = dtrace_strlen((char *)(uintptr_t) 2664 tupregs[0].dttk_value, 2665 state->dts_options[DTRACEOPT_STRSIZE]); 2666 break; 2667 2668 case DIF_SUBR_STRCHR: 2669 case DIF_SUBR_STRRCHR: { 2670 /* 2671 * We're going to iterate over the string looking for the 2672 * specified character. We will iterate until we have reached 2673 * the string length or we have found the character. If this 2674 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 2675 * of the specified character instead of the first. 2676 */ 2677 uintptr_t addr = tupregs[0].dttk_value; 2678 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 2679 char c, target = (char)tupregs[1].dttk_value; 2680 2681 for (regs[rd] = NULL; addr < limit; addr++) { 2682 if ((c = dtrace_load8(addr)) == target) { 2683 regs[rd] = addr; 2684 2685 if (subr == DIF_SUBR_STRCHR) 2686 break; 2687 } 2688 2689 if (c == '\0') 2690 break; 2691 } 2692 2693 break; 2694 } 2695 2696 case DIF_SUBR_STRSTR: 2697 case DIF_SUBR_INDEX: 2698 case DIF_SUBR_RINDEX: { 2699 /* 2700 * We're going to iterate over the string looking for the 2701 * specified string. We will iterate until we have reached 2702 * the string length or we have found the string. (Yes, this 2703 * is done in the most naive way possible -- but considering 2704 * that the string we're searching for is likely to be 2705 * relatively short, the complexity of Rabin-Karp or similar 2706 * hardly seems merited.) 2707 */ 2708 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 2709 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 2710 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2711 size_t len = dtrace_strlen(addr, size); 2712 size_t sublen = dtrace_strlen(substr, size); 2713 char *limit = addr + len, *orig = addr; 2714 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 2715 int inc = 1; 2716 2717 regs[rd] = notfound; 2718 2719 /* 2720 * strstr() and index()/rindex() have similar semantics if 2721 * both strings are the empty string: strstr() returns a 2722 * pointer to the (empty) string, and index() and rindex() 2723 * both return index 0 (regardless of any position argument). 2724 */ 2725 if (sublen == 0 && len == 0) { 2726 if (subr == DIF_SUBR_STRSTR) 2727 regs[rd] = (uintptr_t)addr; 2728 else 2729 regs[rd] = 0; 2730 break; 2731 } 2732 2733 if (subr != DIF_SUBR_STRSTR) { 2734 if (subr == DIF_SUBR_RINDEX) { 2735 limit = orig - 1; 2736 addr += len; 2737 inc = -1; 2738 } 2739 2740 /* 2741 * Both index() and rindex() take an optional position 2742 * argument that denotes the starting position. 2743 */ 2744 if (nargs == 3) { 2745 int64_t pos = (int64_t)tupregs[2].dttk_value; 2746 2747 /* 2748 * If the position argument to index() is 2749 * negative, Perl implicitly clamps it at 2750 * zero. This semantic is a little surprising 2751 * given the special meaning of negative 2752 * positions to similar Perl functions like 2753 * substr(), but it appears to reflect a 2754 * notion that index() can start from a 2755 * negative index and increment its way up to 2756 * the string. Given this notion, Perl's 2757 * rindex() is at least self-consistent in 2758 * that it implicitly clamps positions greater 2759 * than the string length to be the string 2760 * length. Where Perl completely loses 2761 * coherence, however, is when the specified 2762 * substring is the empty string (""). In 2763 * this case, even if the position is 2764 * negative, rindex() returns 0 -- and even if 2765 * the position is greater than the length, 2766 * index() returns the string length. These 2767 * semantics violate the notion that index() 2768 * should never return a value less than the 2769 * specified position and that rindex() should 2770 * never return a value greater than the 2771 * specified position. (One assumes that 2772 * these semantics are artifacts of Perl's 2773 * implementation and not the results of 2774 * deliberate design -- it beggars belief that 2775 * even Larry Wall could desire such oddness.) 2776 * While in the abstract one would wish for 2777 * consistent position semantics across 2778 * substr(), index() and rindex() -- or at the 2779 * very least self-consistent position 2780 * semantics for index() and rindex() -- we 2781 * instead opt to keep with the extant Perl 2782 * semantics, in all their broken glory. (Do 2783 * we have more desire to maintain Perl's 2784 * semantics than Perl does? Probably.) 2785 */ 2786 if (subr == DIF_SUBR_RINDEX) { 2787 if (pos < 0) { 2788 if (sublen == 0) 2789 regs[rd] = 0; 2790 break; 2791 } 2792 2793 if (pos > len) 2794 pos = len; 2795 } else { 2796 if (pos < 0) 2797 pos = 0; 2798 2799 if (pos >= len) { 2800 if (sublen == 0) 2801 regs[rd] = len; 2802 break; 2803 } 2804 } 2805 2806 addr = orig + pos; 2807 } 2808 } 2809 2810 for (regs[rd] = notfound; addr != limit; addr += inc) { 2811 if (dtrace_strncmp(addr, substr, sublen) == 0) { 2812 if (subr != DIF_SUBR_STRSTR) { 2813 /* 2814 * As D index() and rindex() are 2815 * modeled on Perl (and not on awk), 2816 * we return a zero-based (and not a 2817 * one-based) index. (For you Perl 2818 * weenies: no, we're not going to add 2819 * $[ -- and shouldn't you be at a con 2820 * or something?) 2821 */ 2822 regs[rd] = (uintptr_t)(addr - orig); 2823 break; 2824 } 2825 2826 ASSERT(subr == DIF_SUBR_STRSTR); 2827 regs[rd] = (uintptr_t)addr; 2828 break; 2829 } 2830 } 2831 2832 break; 2833 } 2834 2835 case DIF_SUBR_STRTOK: { 2836 uintptr_t addr = tupregs[0].dttk_value; 2837 uintptr_t tokaddr = tupregs[1].dttk_value; 2838 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2839 uintptr_t limit, toklimit = tokaddr + size; 2840 uint8_t c, tokmap[32]; /* 256 / 8 */ 2841 char *dest = (char *)mstate->dtms_scratch_ptr; 2842 int i; 2843 2844 if (mstate->dtms_scratch_ptr + size > 2845 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2846 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2847 regs[rd] = NULL; 2848 break; 2849 } 2850 2851 if (addr == NULL) { 2852 /* 2853 * If the address specified is NULL, we use our saved 2854 * strtok pointer from the mstate. Note that this 2855 * means that the saved strtok pointer is _only_ 2856 * valid within multiple enablings of the same probe -- 2857 * it behaves like an implicit clause-local variable. 2858 */ 2859 addr = mstate->dtms_strtok; 2860 } 2861 2862 /* 2863 * First, zero the token map, and then process the token 2864 * string -- setting a bit in the map for every character 2865 * found in the token string. 2866 */ 2867 for (i = 0; i < sizeof (tokmap); i++) 2868 tokmap[i] = 0; 2869 2870 for (; tokaddr < toklimit; tokaddr++) { 2871 if ((c = dtrace_load8(tokaddr)) == '\0') 2872 break; 2873 2874 ASSERT((c >> 3) < sizeof (tokmap)); 2875 tokmap[c >> 3] |= (1 << (c & 0x7)); 2876 } 2877 2878 for (limit = addr + size; addr < limit; addr++) { 2879 /* 2880 * We're looking for a character that is _not_ contained 2881 * in the token string. 2882 */ 2883 if ((c = dtrace_load8(addr)) == '\0') 2884 break; 2885 2886 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 2887 break; 2888 } 2889 2890 if (c == '\0') { 2891 /* 2892 * We reached the end of the string without finding 2893 * any character that was not in the token string. 2894 * We return NULL in this case, and we set the saved 2895 * address to NULL as well. 2896 */ 2897 regs[rd] = NULL; 2898 mstate->dtms_strtok = NULL; 2899 break; 2900 } 2901 2902 /* 2903 * From here on, we're copying into the destination string. 2904 */ 2905 for (i = 0; addr < limit && i < size - 1; addr++) { 2906 if ((c = dtrace_load8(addr)) == '\0') 2907 break; 2908 2909 if (tokmap[c >> 3] & (1 << (c & 0x7))) 2910 break; 2911 2912 ASSERT(i < size); 2913 dest[i++] = c; 2914 } 2915 2916 ASSERT(i < size); 2917 dest[i] = '\0'; 2918 regs[rd] = (uintptr_t)dest; 2919 mstate->dtms_scratch_ptr += size; 2920 mstate->dtms_strtok = addr; 2921 break; 2922 } 2923 2924 case DIF_SUBR_SUBSTR: { 2925 uintptr_t s = tupregs[0].dttk_value; 2926 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2927 char *d = (char *)mstate->dtms_scratch_ptr; 2928 int64_t index = (int64_t)tupregs[1].dttk_value; 2929 int64_t remaining = (int64_t)tupregs[2].dttk_value; 2930 size_t len = dtrace_strlen((char *)s, size); 2931 int64_t i = 0; 2932 2933 if (nargs <= 2) 2934 remaining = (int64_t)size; 2935 2936 if (mstate->dtms_scratch_ptr + size > 2937 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2938 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2939 regs[rd] = NULL; 2940 break; 2941 } 2942 2943 if (index < 0) { 2944 index += len; 2945 2946 if (index < 0 && index + remaining > 0) { 2947 remaining += index; 2948 index = 0; 2949 } 2950 } 2951 2952 if (index >= len || index < 0) 2953 index = len; 2954 2955 for (d[0] = '\0'; remaining > 0; remaining--) { 2956 if ((d[i++] = dtrace_load8(s++ + index)) == '\0') 2957 break; 2958 2959 if (i == size) { 2960 d[i - 1] = '\0'; 2961 break; 2962 } 2963 } 2964 2965 mstate->dtms_scratch_ptr += size; 2966 regs[rd] = (uintptr_t)d; 2967 break; 2968 } 2969 2970 case DIF_SUBR_GETMAJOR: 2971 #ifdef _LP64 2972 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 2973 #else 2974 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 2975 #endif 2976 break; 2977 2978 case DIF_SUBR_GETMINOR: 2979 #ifdef _LP64 2980 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 2981 #else 2982 regs[rd] = tupregs[0].dttk_value & MAXMIN; 2983 #endif 2984 break; 2985 2986 case DIF_SUBR_DDI_PATHNAME: { 2987 /* 2988 * This one is a galactic mess. We are going to roughly 2989 * emulate ddi_pathname(), but it's made more complicated 2990 * by the fact that we (a) want to include the minor name and 2991 * (b) must proceed iteratively instead of recursively. 2992 */ 2993 uintptr_t dest = mstate->dtms_scratch_ptr; 2994 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2995 char *start = (char *)dest, *end = start + size - 1; 2996 uintptr_t daddr = tupregs[0].dttk_value; 2997 int64_t minor = (int64_t)tupregs[1].dttk_value; 2998 char *s; 2999 int i, len, depth = 0; 3000 3001 if (size == 0 || mstate->dtms_scratch_ptr + size > 3002 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3003 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3004 regs[rd] = NULL; 3005 break; 3006 } 3007 3008 *end = '\0'; 3009 3010 /* 3011 * We want to have a name for the minor. In order to do this, 3012 * we need to walk the minor list from the devinfo. We want 3013 * to be sure that we don't infinitely walk a circular list, 3014 * so we check for circularity by sending a scout pointer 3015 * ahead two elements for every element that we iterate over; 3016 * if the list is circular, these will ultimately point to the 3017 * same element. You may recognize this little trick as the 3018 * answer to a stupid interview question -- one that always 3019 * seems to be asked by those who had to have it laboriously 3020 * explained to them, and who can't even concisely describe 3021 * the conditions under which one would be forced to resort to 3022 * this technique. Needless to say, those conditions are 3023 * found here -- and probably only here. Is this is the only 3024 * use of this infamous trick in shipping, production code? 3025 * If it isn't, it probably should be... 3026 */ 3027 if (minor != -1) { 3028 uintptr_t maddr = dtrace_loadptr(daddr + 3029 offsetof(struct dev_info, devi_minor)); 3030 3031 uintptr_t next = offsetof(struct ddi_minor_data, next); 3032 uintptr_t name = offsetof(struct ddi_minor_data, 3033 d_minor) + offsetof(struct ddi_minor, name); 3034 uintptr_t dev = offsetof(struct ddi_minor_data, 3035 d_minor) + offsetof(struct ddi_minor, dev); 3036 uintptr_t scout; 3037 3038 if (maddr != NULL) 3039 scout = dtrace_loadptr(maddr + next); 3040 3041 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3042 uint64_t m; 3043 #ifdef _LP64 3044 m = dtrace_load64(maddr + dev) & MAXMIN64; 3045 #else 3046 m = dtrace_load32(maddr + dev) & MAXMIN; 3047 #endif 3048 if (m != minor) { 3049 maddr = dtrace_loadptr(maddr + next); 3050 3051 if (scout == NULL) 3052 continue; 3053 3054 scout = dtrace_loadptr(scout + next); 3055 3056 if (scout == NULL) 3057 continue; 3058 3059 scout = dtrace_loadptr(scout + next); 3060 3061 if (scout == NULL) 3062 continue; 3063 3064 if (scout == maddr) { 3065 *flags |= CPU_DTRACE_ILLOP; 3066 break; 3067 } 3068 3069 continue; 3070 } 3071 3072 /* 3073 * We have the minor data. Now we need to 3074 * copy the minor's name into the end of the 3075 * pathname. 3076 */ 3077 s = (char *)dtrace_loadptr(maddr + name); 3078 len = dtrace_strlen(s, size); 3079 3080 if (*flags & CPU_DTRACE_FAULT) 3081 break; 3082 3083 if (len != 0) { 3084 if ((end -= (len + 1)) < start) 3085 break; 3086 3087 *end = ':'; 3088 } 3089 3090 for (i = 1; i <= len; i++) 3091 end[i] = dtrace_load8((uintptr_t)s++); 3092 break; 3093 } 3094 } 3095 3096 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3097 ddi_node_state_t devi_state; 3098 3099 devi_state = dtrace_load32(daddr + 3100 offsetof(struct dev_info, devi_node_state)); 3101 3102 if (*flags & CPU_DTRACE_FAULT) 3103 break; 3104 3105 if (devi_state >= DS_INITIALIZED) { 3106 s = (char *)dtrace_loadptr(daddr + 3107 offsetof(struct dev_info, devi_addr)); 3108 len = dtrace_strlen(s, size); 3109 3110 if (*flags & CPU_DTRACE_FAULT) 3111 break; 3112 3113 if (len != 0) { 3114 if ((end -= (len + 1)) < start) 3115 break; 3116 3117 *end = '@'; 3118 } 3119 3120 for (i = 1; i <= len; i++) 3121 end[i] = dtrace_load8((uintptr_t)s++); 3122 } 3123 3124 /* 3125 * Now for the node name... 3126 */ 3127 s = (char *)dtrace_loadptr(daddr + 3128 offsetof(struct dev_info, devi_node_name)); 3129 3130 daddr = dtrace_loadptr(daddr + 3131 offsetof(struct dev_info, devi_parent)); 3132 3133 /* 3134 * If our parent is NULL (that is, if we're the root 3135 * node), we're going to use the special path 3136 * "devices". 3137 */ 3138 if (daddr == NULL) 3139 s = "devices"; 3140 3141 len = dtrace_strlen(s, size); 3142 if (*flags & CPU_DTRACE_FAULT) 3143 break; 3144 3145 if ((end -= (len + 1)) < start) 3146 break; 3147 3148 for (i = 1; i <= len; i++) 3149 end[i] = dtrace_load8((uintptr_t)s++); 3150 *end = '/'; 3151 3152 if (depth++ > dtrace_devdepth_max) { 3153 *flags |= CPU_DTRACE_ILLOP; 3154 break; 3155 } 3156 } 3157 3158 if (end < start) 3159 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3160 3161 if (daddr == NULL) { 3162 regs[rd] = (uintptr_t)end; 3163 mstate->dtms_scratch_ptr += size; 3164 } 3165 3166 break; 3167 } 3168 3169 case DIF_SUBR_STRJOIN: { 3170 char *d = (char *)mstate->dtms_scratch_ptr; 3171 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3172 uintptr_t s1 = tupregs[0].dttk_value; 3173 uintptr_t s2 = tupregs[1].dttk_value; 3174 int i = 0; 3175 3176 if (mstate->dtms_scratch_ptr + size > 3177 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3178 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3179 regs[rd] = NULL; 3180 break; 3181 } 3182 3183 for (;;) { 3184 if (i >= size) { 3185 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3186 regs[rd] = NULL; 3187 break; 3188 } 3189 3190 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 3191 i--; 3192 break; 3193 } 3194 } 3195 3196 for (;;) { 3197 if (i >= size) { 3198 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3199 regs[rd] = NULL; 3200 break; 3201 } 3202 3203 if ((d[i++] = dtrace_load8(s2++)) == '\0') 3204 break; 3205 } 3206 3207 if (i < size) { 3208 mstate->dtms_scratch_ptr += i; 3209 regs[rd] = (uintptr_t)d; 3210 } 3211 3212 break; 3213 } 3214 3215 case DIF_SUBR_LLTOSTR: { 3216 int64_t i = (int64_t)tupregs[0].dttk_value; 3217 int64_t val = i < 0 ? i * -1 : i; 3218 uint64_t size = 22; /* enough room for 2^64 in decimal */ 3219 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 3220 3221 if (mstate->dtms_scratch_ptr + size > 3222 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3223 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3224 regs[rd] = NULL; 3225 break; 3226 } 3227 3228 for (*end-- = '\0'; val; val /= 10) 3229 *end-- = '0' + (val % 10); 3230 3231 if (i == 0) 3232 *end-- = '0'; 3233 3234 if (i < 0) 3235 *end-- = '-'; 3236 3237 regs[rd] = (uintptr_t)end + 1; 3238 mstate->dtms_scratch_ptr += size; 3239 break; 3240 } 3241 3242 case DIF_SUBR_DIRNAME: 3243 case DIF_SUBR_BASENAME: { 3244 char *dest = (char *)mstate->dtms_scratch_ptr; 3245 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3246 uintptr_t src = tupregs[0].dttk_value; 3247 int i, j, len = dtrace_strlen((char *)src, size); 3248 int lastbase = -1, firstbase = -1, lastdir = -1; 3249 int start, end; 3250 3251 if (mstate->dtms_scratch_ptr + size > 3252 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3253 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3254 regs[rd] = NULL; 3255 break; 3256 } 3257 3258 /* 3259 * The basename and dirname for a zero-length string is 3260 * defined to be "." 3261 */ 3262 if (len == 0) { 3263 len = 1; 3264 src = (uintptr_t)"."; 3265 } 3266 3267 /* 3268 * Start from the back of the string, moving back toward the 3269 * front until we see a character that isn't a slash. That 3270 * character is the last character in the basename. 3271 */ 3272 for (i = len - 1; i >= 0; i--) { 3273 if (dtrace_load8(src + i) != '/') 3274 break; 3275 } 3276 3277 if (i >= 0) 3278 lastbase = i; 3279 3280 /* 3281 * Starting from the last character in the basename, move 3282 * towards the front until we find a slash. The character 3283 * that we processed immediately before that is the first 3284 * character in the basename. 3285 */ 3286 for (; i >= 0; i--) { 3287 if (dtrace_load8(src + i) == '/') 3288 break; 3289 } 3290 3291 if (i >= 0) 3292 firstbase = i + 1; 3293 3294 /* 3295 * Now keep going until we find a non-slash character. That 3296 * character is the last character in the dirname. 3297 */ 3298 for (; i >= 0; i--) { 3299 if (dtrace_load8(src + i) != '/') 3300 break; 3301 } 3302 3303 if (i >= 0) 3304 lastdir = i; 3305 3306 ASSERT(!(lastbase == -1 && firstbase != -1)); 3307 ASSERT(!(firstbase == -1 && lastdir != -1)); 3308 3309 if (lastbase == -1) { 3310 /* 3311 * We didn't find a non-slash character. We know that 3312 * the length is non-zero, so the whole string must be 3313 * slashes. In either the dirname or the basename 3314 * case, we return '/'. 3315 */ 3316 ASSERT(firstbase == -1); 3317 firstbase = lastbase = lastdir = 0; 3318 } 3319 3320 if (firstbase == -1) { 3321 /* 3322 * The entire string consists only of a basename 3323 * component. If we're looking for dirname, we need 3324 * to change our string to be just "."; if we're 3325 * looking for a basename, we'll just set the first 3326 * character of the basename to be 0. 3327 */ 3328 if (subr == DIF_SUBR_DIRNAME) { 3329 ASSERT(lastdir == -1); 3330 src = (uintptr_t)"."; 3331 lastdir = 0; 3332 } else { 3333 firstbase = 0; 3334 } 3335 } 3336 3337 if (subr == DIF_SUBR_DIRNAME) { 3338 if (lastdir == -1) { 3339 /* 3340 * We know that we have a slash in the name -- 3341 * or lastdir would be set to 0, above. And 3342 * because lastdir is -1, we know that this 3343 * slash must be the first character. (That 3344 * is, the full string must be of the form 3345 * "/basename".) In this case, the last 3346 * character of the directory name is 0. 3347 */ 3348 lastdir = 0; 3349 } 3350 3351 start = 0; 3352 end = lastdir; 3353 } else { 3354 ASSERT(subr == DIF_SUBR_BASENAME); 3355 ASSERT(firstbase != -1 && lastbase != -1); 3356 start = firstbase; 3357 end = lastbase; 3358 } 3359 3360 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 3361 dest[j] = dtrace_load8(src + i); 3362 3363 dest[j] = '\0'; 3364 regs[rd] = (uintptr_t)dest; 3365 mstate->dtms_scratch_ptr += size; 3366 break; 3367 } 3368 3369 case DIF_SUBR_CLEANPATH: { 3370 char *dest = (char *)mstate->dtms_scratch_ptr, c; 3371 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3372 uintptr_t src = tupregs[0].dttk_value; 3373 int i = 0, j = 0; 3374 3375 if (mstate->dtms_scratch_ptr + size > 3376 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3377 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3378 regs[rd] = NULL; 3379 break; 3380 } 3381 3382 /* 3383 * Move forward, loading each character. 3384 */ 3385 do { 3386 c = dtrace_load8(src + i++); 3387 next: 3388 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 3389 break; 3390 3391 if (c != '/') { 3392 dest[j++] = c; 3393 continue; 3394 } 3395 3396 c = dtrace_load8(src + i++); 3397 3398 if (c == '/') { 3399 /* 3400 * We have two slashes -- we can just advance 3401 * to the next character. 3402 */ 3403 goto next; 3404 } 3405 3406 if (c != '.') { 3407 /* 3408 * This is not "." and it's not ".." -- we can 3409 * just store the "/" and this character and 3410 * drive on. 3411 */ 3412 dest[j++] = '/'; 3413 dest[j++] = c; 3414 continue; 3415 } 3416 3417 c = dtrace_load8(src + i++); 3418 3419 if (c == '/') { 3420 /* 3421 * This is a "/./" component. We're not going 3422 * to store anything in the destination buffer; 3423 * we're just going to go to the next component. 3424 */ 3425 goto next; 3426 } 3427 3428 if (c != '.') { 3429 /* 3430 * This is not ".." -- we can just store the 3431 * "/." and this character and continue 3432 * processing. 3433 */ 3434 dest[j++] = '/'; 3435 dest[j++] = '.'; 3436 dest[j++] = c; 3437 continue; 3438 } 3439 3440 c = dtrace_load8(src + i++); 3441 3442 if (c != '/' && c != '\0') { 3443 /* 3444 * This is not ".." -- it's "..[mumble]". 3445 * We'll store the "/.." and this character 3446 * and continue processing. 3447 */ 3448 dest[j++] = '/'; 3449 dest[j++] = '.'; 3450 dest[j++] = '.'; 3451 dest[j++] = c; 3452 continue; 3453 } 3454 3455 /* 3456 * This is "/../" or "/..\0". We need to back up 3457 * our destination pointer until we find a "/". 3458 */ 3459 i--; 3460 while (j != 0 && dest[--j] != '/') 3461 continue; 3462 3463 if (c == '\0') 3464 dest[++j] = '/'; 3465 } while (c != '\0'); 3466 3467 dest[j] = '\0'; 3468 regs[rd] = (uintptr_t)dest; 3469 mstate->dtms_scratch_ptr += size; 3470 break; 3471 } 3472 } 3473 } 3474 3475 /* 3476 * Emulate the execution of DTrace IR instructions specified by the given 3477 * DIF object. This function is deliberately void of assertions as all of 3478 * the necessary checks are handled by a call to dtrace_difo_validate(). 3479 */ 3480 static uint64_t 3481 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 3482 dtrace_vstate_t *vstate, dtrace_state_t *state) 3483 { 3484 const dif_instr_t *text = difo->dtdo_buf; 3485 const uint_t textlen = difo->dtdo_len; 3486 const char *strtab = difo->dtdo_strtab; 3487 const uint64_t *inttab = difo->dtdo_inttab; 3488 3489 uint64_t rval = 0; 3490 dtrace_statvar_t *svar; 3491 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 3492 dtrace_difv_t *v; 3493 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 3494 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 3495 3496 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 3497 uint64_t regs[DIF_DIR_NREGS]; 3498 uint64_t *tmp; 3499 3500 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 3501 int64_t cc_r; 3502 uint_t pc = 0, id, opc; 3503 uint8_t ttop = 0; 3504 dif_instr_t instr; 3505 uint_t r1, r2, rd; 3506 3507 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 3508 3509 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 3510 opc = pc; 3511 3512 instr = text[pc++]; 3513 r1 = DIF_INSTR_R1(instr); 3514 r2 = DIF_INSTR_R2(instr); 3515 rd = DIF_INSTR_RD(instr); 3516 3517 switch (DIF_INSTR_OP(instr)) { 3518 case DIF_OP_OR: 3519 regs[rd] = regs[r1] | regs[r2]; 3520 break; 3521 case DIF_OP_XOR: 3522 regs[rd] = regs[r1] ^ regs[r2]; 3523 break; 3524 case DIF_OP_AND: 3525 regs[rd] = regs[r1] & regs[r2]; 3526 break; 3527 case DIF_OP_SLL: 3528 regs[rd] = regs[r1] << regs[r2]; 3529 break; 3530 case DIF_OP_SRL: 3531 regs[rd] = regs[r1] >> regs[r2]; 3532 break; 3533 case DIF_OP_SUB: 3534 regs[rd] = regs[r1] - regs[r2]; 3535 break; 3536 case DIF_OP_ADD: 3537 regs[rd] = regs[r1] + regs[r2]; 3538 break; 3539 case DIF_OP_MUL: 3540 regs[rd] = regs[r1] * regs[r2]; 3541 break; 3542 case DIF_OP_SDIV: 3543 if (regs[r2] == 0) { 3544 regs[rd] = 0; 3545 *flags |= CPU_DTRACE_DIVZERO; 3546 } else { 3547 regs[rd] = (int64_t)regs[r1] / 3548 (int64_t)regs[r2]; 3549 } 3550 break; 3551 3552 case DIF_OP_UDIV: 3553 if (regs[r2] == 0) { 3554 regs[rd] = 0; 3555 *flags |= CPU_DTRACE_DIVZERO; 3556 } else { 3557 regs[rd] = regs[r1] / regs[r2]; 3558 } 3559 break; 3560 3561 case DIF_OP_SREM: 3562 if (regs[r2] == 0) { 3563 regs[rd] = 0; 3564 *flags |= CPU_DTRACE_DIVZERO; 3565 } else { 3566 regs[rd] = (int64_t)regs[r1] % 3567 (int64_t)regs[r2]; 3568 } 3569 break; 3570 3571 case DIF_OP_UREM: 3572 if (regs[r2] == 0) { 3573 regs[rd] = 0; 3574 *flags |= CPU_DTRACE_DIVZERO; 3575 } else { 3576 regs[rd] = regs[r1] % regs[r2]; 3577 } 3578 break; 3579 3580 case DIF_OP_NOT: 3581 regs[rd] = ~regs[r1]; 3582 break; 3583 case DIF_OP_MOV: 3584 regs[rd] = regs[r1]; 3585 break; 3586 case DIF_OP_CMP: 3587 cc_r = regs[r1] - regs[r2]; 3588 cc_n = cc_r < 0; 3589 cc_z = cc_r == 0; 3590 cc_v = 0; 3591 cc_c = regs[r1] < regs[r2]; 3592 break; 3593 case DIF_OP_TST: 3594 cc_n = cc_v = cc_c = 0; 3595 cc_z = regs[r1] == 0; 3596 break; 3597 case DIF_OP_BA: 3598 pc = DIF_INSTR_LABEL(instr); 3599 break; 3600 case DIF_OP_BE: 3601 if (cc_z) 3602 pc = DIF_INSTR_LABEL(instr); 3603 break; 3604 case DIF_OP_BNE: 3605 if (cc_z == 0) 3606 pc = DIF_INSTR_LABEL(instr); 3607 break; 3608 case DIF_OP_BG: 3609 if ((cc_z | (cc_n ^ cc_v)) == 0) 3610 pc = DIF_INSTR_LABEL(instr); 3611 break; 3612 case DIF_OP_BGU: 3613 if ((cc_c | cc_z) == 0) 3614 pc = DIF_INSTR_LABEL(instr); 3615 break; 3616 case DIF_OP_BGE: 3617 if ((cc_n ^ cc_v) == 0) 3618 pc = DIF_INSTR_LABEL(instr); 3619 break; 3620 case DIF_OP_BGEU: 3621 if (cc_c == 0) 3622 pc = DIF_INSTR_LABEL(instr); 3623 break; 3624 case DIF_OP_BL: 3625 if (cc_n ^ cc_v) 3626 pc = DIF_INSTR_LABEL(instr); 3627 break; 3628 case DIF_OP_BLU: 3629 if (cc_c) 3630 pc = DIF_INSTR_LABEL(instr); 3631 break; 3632 case DIF_OP_BLE: 3633 if (cc_z | (cc_n ^ cc_v)) 3634 pc = DIF_INSTR_LABEL(instr); 3635 break; 3636 case DIF_OP_BLEU: 3637 if (cc_c | cc_z) 3638 pc = DIF_INSTR_LABEL(instr); 3639 break; 3640 case DIF_OP_RLDSB: 3641 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 3642 *flags |= CPU_DTRACE_KPRIV; 3643 *illval = regs[r1]; 3644 break; 3645 } 3646 /*FALLTHROUGH*/ 3647 case DIF_OP_LDSB: 3648 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 3649 break; 3650 case DIF_OP_RLDSH: 3651 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 3652 *flags |= CPU_DTRACE_KPRIV; 3653 *illval = regs[r1]; 3654 break; 3655 } 3656 /*FALLTHROUGH*/ 3657 case DIF_OP_LDSH: 3658 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 3659 break; 3660 case DIF_OP_RLDSW: 3661 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 3662 *flags |= CPU_DTRACE_KPRIV; 3663 *illval = regs[r1]; 3664 break; 3665 } 3666 /*FALLTHROUGH*/ 3667 case DIF_OP_LDSW: 3668 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 3669 break; 3670 case DIF_OP_RLDUB: 3671 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 3672 *flags |= CPU_DTRACE_KPRIV; 3673 *illval = regs[r1]; 3674 break; 3675 } 3676 /*FALLTHROUGH*/ 3677 case DIF_OP_LDUB: 3678 regs[rd] = dtrace_load8(regs[r1]); 3679 break; 3680 case DIF_OP_RLDUH: 3681 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 3682 *flags |= CPU_DTRACE_KPRIV; 3683 *illval = regs[r1]; 3684 break; 3685 } 3686 /*FALLTHROUGH*/ 3687 case DIF_OP_LDUH: 3688 regs[rd] = dtrace_load16(regs[r1]); 3689 break; 3690 case DIF_OP_RLDUW: 3691 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 3692 *flags |= CPU_DTRACE_KPRIV; 3693 *illval = regs[r1]; 3694 break; 3695 } 3696 /*FALLTHROUGH*/ 3697 case DIF_OP_LDUW: 3698 regs[rd] = dtrace_load32(regs[r1]); 3699 break; 3700 case DIF_OP_RLDX: 3701 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 3702 *flags |= CPU_DTRACE_KPRIV; 3703 *illval = regs[r1]; 3704 break; 3705 } 3706 /*FALLTHROUGH*/ 3707 case DIF_OP_LDX: 3708 regs[rd] = dtrace_load64(regs[r1]); 3709 break; 3710 case DIF_OP_ULDSB: 3711 regs[rd] = (int8_t) 3712 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 3713 break; 3714 case DIF_OP_ULDSH: 3715 regs[rd] = (int16_t) 3716 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 3717 break; 3718 case DIF_OP_ULDSW: 3719 regs[rd] = (int32_t) 3720 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 3721 break; 3722 case DIF_OP_ULDUB: 3723 regs[rd] = 3724 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 3725 break; 3726 case DIF_OP_ULDUH: 3727 regs[rd] = 3728 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 3729 break; 3730 case DIF_OP_ULDUW: 3731 regs[rd] = 3732 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 3733 break; 3734 case DIF_OP_ULDX: 3735 regs[rd] = 3736 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 3737 break; 3738 case DIF_OP_RET: 3739 rval = regs[rd]; 3740 break; 3741 case DIF_OP_NOP: 3742 break; 3743 case DIF_OP_SETX: 3744 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 3745 break; 3746 case DIF_OP_SETS: 3747 regs[rd] = (uint64_t)(uintptr_t) 3748 (strtab + DIF_INSTR_STRING(instr)); 3749 break; 3750 case DIF_OP_SCMP: 3751 cc_r = dtrace_strncmp((char *)(uintptr_t)regs[r1], 3752 (char *)(uintptr_t)regs[r2], 3753 state->dts_options[DTRACEOPT_STRSIZE]); 3754 3755 cc_n = cc_r < 0; 3756 cc_z = cc_r == 0; 3757 cc_v = cc_c = 0; 3758 break; 3759 case DIF_OP_LDGA: 3760 regs[rd] = dtrace_dif_variable(mstate, state, 3761 r1, regs[r2]); 3762 break; 3763 case DIF_OP_LDGS: 3764 id = DIF_INSTR_VAR(instr); 3765 3766 if (id >= DIF_VAR_OTHER_UBASE) { 3767 uintptr_t a; 3768 3769 id -= DIF_VAR_OTHER_UBASE; 3770 svar = vstate->dtvs_globals[id]; 3771 ASSERT(svar != NULL); 3772 v = &svar->dtsv_var; 3773 3774 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 3775 regs[rd] = svar->dtsv_data; 3776 break; 3777 } 3778 3779 a = (uintptr_t)svar->dtsv_data; 3780 3781 if (*(uint8_t *)a == UINT8_MAX) { 3782 /* 3783 * If the 0th byte is set to UINT8_MAX 3784 * then this is to be treated as a 3785 * reference to a NULL variable. 3786 */ 3787 regs[rd] = NULL; 3788 } else { 3789 regs[rd] = a + sizeof (uint64_t); 3790 } 3791 3792 break; 3793 } 3794 3795 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 3796 break; 3797 3798 case DIF_OP_STGS: 3799 id = DIF_INSTR_VAR(instr); 3800 3801 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3802 id -= DIF_VAR_OTHER_UBASE; 3803 3804 svar = vstate->dtvs_globals[id]; 3805 ASSERT(svar != NULL); 3806 v = &svar->dtsv_var; 3807 3808 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3809 uintptr_t a = (uintptr_t)svar->dtsv_data; 3810 3811 ASSERT(a != NULL); 3812 ASSERT(svar->dtsv_size != 0); 3813 3814 if (regs[rd] == NULL) { 3815 *(uint8_t *)a = UINT8_MAX; 3816 break; 3817 } else { 3818 *(uint8_t *)a = 0; 3819 a += sizeof (uint64_t); 3820 } 3821 3822 dtrace_vcopy((void *)(uintptr_t)regs[rd], 3823 (void *)a, &v->dtdv_type); 3824 break; 3825 } 3826 3827 svar->dtsv_data = regs[rd]; 3828 break; 3829 3830 case DIF_OP_LDTA: 3831 /* 3832 * There are no DTrace built-in thread-local arrays at 3833 * present. This opcode is saved for future work. 3834 */ 3835 *flags |= CPU_DTRACE_ILLOP; 3836 regs[rd] = 0; 3837 break; 3838 3839 case DIF_OP_LDLS: 3840 id = DIF_INSTR_VAR(instr); 3841 3842 if (id < DIF_VAR_OTHER_UBASE) { 3843 /* 3844 * For now, this has no meaning. 3845 */ 3846 regs[rd] = 0; 3847 break; 3848 } 3849 3850 id -= DIF_VAR_OTHER_UBASE; 3851 3852 ASSERT(id < vstate->dtvs_nlocals); 3853 ASSERT(vstate->dtvs_locals != NULL); 3854 3855 svar = vstate->dtvs_locals[id]; 3856 ASSERT(svar != NULL); 3857 v = &svar->dtsv_var; 3858 3859 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3860 uintptr_t a = (uintptr_t)svar->dtsv_data; 3861 size_t sz = v->dtdv_type.dtdt_size; 3862 3863 sz += sizeof (uint64_t); 3864 ASSERT(svar->dtsv_size == NCPU * sz); 3865 a += CPU->cpu_id * sz; 3866 3867 if (*(uint8_t *)a == UINT8_MAX) { 3868 /* 3869 * If the 0th byte is set to UINT8_MAX 3870 * then this is to be treated as a 3871 * reference to a NULL variable. 3872 */ 3873 regs[rd] = NULL; 3874 } else { 3875 regs[rd] = a + sizeof (uint64_t); 3876 } 3877 3878 break; 3879 } 3880 3881 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 3882 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 3883 regs[rd] = tmp[CPU->cpu_id]; 3884 break; 3885 3886 case DIF_OP_STLS: 3887 id = DIF_INSTR_VAR(instr); 3888 3889 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3890 id -= DIF_VAR_OTHER_UBASE; 3891 ASSERT(id < vstate->dtvs_nlocals); 3892 3893 ASSERT(vstate->dtvs_locals != NULL); 3894 svar = vstate->dtvs_locals[id]; 3895 ASSERT(svar != NULL); 3896 v = &svar->dtsv_var; 3897 3898 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3899 uintptr_t a = (uintptr_t)svar->dtsv_data; 3900 size_t sz = v->dtdv_type.dtdt_size; 3901 3902 sz += sizeof (uint64_t); 3903 ASSERT(svar->dtsv_size == NCPU * sz); 3904 a += CPU->cpu_id * sz; 3905 3906 if (regs[rd] == NULL) { 3907 *(uint8_t *)a = UINT8_MAX; 3908 break; 3909 } else { 3910 *(uint8_t *)a = 0; 3911 a += sizeof (uint64_t); 3912 } 3913 3914 dtrace_vcopy((void *)(uintptr_t)regs[rd], 3915 (void *)a, &v->dtdv_type); 3916 break; 3917 } 3918 3919 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 3920 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 3921 tmp[CPU->cpu_id] = regs[rd]; 3922 break; 3923 3924 case DIF_OP_LDTS: { 3925 dtrace_dynvar_t *dvar; 3926 dtrace_key_t *key; 3927 3928 id = DIF_INSTR_VAR(instr); 3929 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3930 id -= DIF_VAR_OTHER_UBASE; 3931 v = &vstate->dtvs_tlocals[id]; 3932 3933 key = &tupregs[DIF_DTR_NREGS]; 3934 key[0].dttk_value = (uint64_t)id; 3935 key[0].dttk_size = 0; 3936 DTRACE_TLS_THRKEY(key[1].dttk_value); 3937 key[1].dttk_size = 0; 3938 3939 dvar = dtrace_dynvar(dstate, 2, key, 3940 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC); 3941 3942 if (dvar == NULL) { 3943 regs[rd] = 0; 3944 break; 3945 } 3946 3947 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3948 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 3949 } else { 3950 regs[rd] = *((uint64_t *)dvar->dtdv_data); 3951 } 3952 3953 break; 3954 } 3955 3956 case DIF_OP_STTS: { 3957 dtrace_dynvar_t *dvar; 3958 dtrace_key_t *key; 3959 3960 id = DIF_INSTR_VAR(instr); 3961 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3962 id -= DIF_VAR_OTHER_UBASE; 3963 3964 key = &tupregs[DIF_DTR_NREGS]; 3965 key[0].dttk_value = (uint64_t)id; 3966 key[0].dttk_size = 0; 3967 DTRACE_TLS_THRKEY(key[1].dttk_value); 3968 key[1].dttk_size = 0; 3969 v = &vstate->dtvs_tlocals[id]; 3970 3971 dvar = dtrace_dynvar(dstate, 2, key, 3972 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 3973 v->dtdv_type.dtdt_size : sizeof (uint64_t), 3974 regs[rd] ? DTRACE_DYNVAR_ALLOC : 3975 DTRACE_DYNVAR_DEALLOC); 3976 3977 /* 3978 * Given that we're storing to thread-local data, 3979 * we need to flush our predicate cache. 3980 */ 3981 curthread->t_predcache = NULL; 3982 3983 if (dvar == NULL) 3984 break; 3985 3986 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3987 dtrace_vcopy((void *)(uintptr_t)regs[rd], 3988 dvar->dtdv_data, &v->dtdv_type); 3989 } else { 3990 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 3991 } 3992 3993 break; 3994 } 3995 3996 case DIF_OP_SRA: 3997 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 3998 break; 3999 4000 case DIF_OP_CALL: 4001 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 4002 regs, tupregs, ttop, mstate, state); 4003 break; 4004 4005 case DIF_OP_PUSHTR: 4006 if (ttop == DIF_DTR_NREGS) { 4007 *flags |= CPU_DTRACE_TUPOFLOW; 4008 break; 4009 } 4010 4011 if (r1 == DIF_TYPE_STRING) { 4012 /* 4013 * If this is a string type and the size is 0, 4014 * we'll use the system-wide default string 4015 * size. Note that we are _not_ looking at 4016 * the value of the DTRACEOPT_STRSIZE option; 4017 * had this been set, we would expect to have 4018 * a non-zero size value in the "pushtr". 4019 */ 4020 tupregs[ttop].dttk_size = 4021 dtrace_strlen((char *)(uintptr_t)regs[rd], 4022 regs[r2] ? regs[r2] : 4023 dtrace_strsize_default) + 1; 4024 } else { 4025 tupregs[ttop].dttk_size = regs[r2]; 4026 } 4027 4028 tupregs[ttop++].dttk_value = regs[rd]; 4029 break; 4030 4031 case DIF_OP_PUSHTV: 4032 if (ttop == DIF_DTR_NREGS) { 4033 *flags |= CPU_DTRACE_TUPOFLOW; 4034 break; 4035 } 4036 4037 tupregs[ttop].dttk_value = regs[rd]; 4038 tupregs[ttop++].dttk_size = 0; 4039 break; 4040 4041 case DIF_OP_POPTS: 4042 if (ttop != 0) 4043 ttop--; 4044 break; 4045 4046 case DIF_OP_FLUSHTS: 4047 ttop = 0; 4048 break; 4049 4050 case DIF_OP_LDGAA: 4051 case DIF_OP_LDTAA: { 4052 dtrace_dynvar_t *dvar; 4053 dtrace_key_t *key = tupregs; 4054 uint_t nkeys = ttop; 4055 4056 id = DIF_INSTR_VAR(instr); 4057 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4058 id -= DIF_VAR_OTHER_UBASE; 4059 4060 key[nkeys].dttk_value = (uint64_t)id; 4061 key[nkeys++].dttk_size = 0; 4062 4063 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 4064 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 4065 key[nkeys++].dttk_size = 0; 4066 v = &vstate->dtvs_tlocals[id]; 4067 } else { 4068 v = &vstate->dtvs_globals[id]->dtsv_var; 4069 } 4070 4071 dvar = dtrace_dynvar(dstate, nkeys, key, 4072 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4073 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4074 DTRACE_DYNVAR_NOALLOC); 4075 4076 if (dvar == NULL) { 4077 regs[rd] = 0; 4078 break; 4079 } 4080 4081 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4082 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 4083 } else { 4084 regs[rd] = *((uint64_t *)dvar->dtdv_data); 4085 } 4086 4087 break; 4088 } 4089 4090 case DIF_OP_STGAA: 4091 case DIF_OP_STTAA: { 4092 dtrace_dynvar_t *dvar; 4093 dtrace_key_t *key = tupregs; 4094 uint_t nkeys = ttop; 4095 4096 id = DIF_INSTR_VAR(instr); 4097 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4098 id -= DIF_VAR_OTHER_UBASE; 4099 4100 key[nkeys].dttk_value = (uint64_t)id; 4101 key[nkeys++].dttk_size = 0; 4102 4103 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 4104 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 4105 key[nkeys++].dttk_size = 0; 4106 v = &vstate->dtvs_tlocals[id]; 4107 } else { 4108 v = &vstate->dtvs_globals[id]->dtsv_var; 4109 } 4110 4111 dvar = dtrace_dynvar(dstate, nkeys, key, 4112 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4113 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4114 regs[rd] ? DTRACE_DYNVAR_ALLOC : 4115 DTRACE_DYNVAR_DEALLOC); 4116 4117 if (dvar == NULL) 4118 break; 4119 4120 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4121 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4122 dvar->dtdv_data, &v->dtdv_type); 4123 } else { 4124 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 4125 } 4126 4127 break; 4128 } 4129 4130 case DIF_OP_ALLOCS: { 4131 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4132 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 4133 4134 if (mstate->dtms_scratch_ptr + size > 4135 mstate->dtms_scratch_base + 4136 mstate->dtms_scratch_size) { 4137 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4138 regs[rd] = NULL; 4139 } else { 4140 dtrace_bzero((void *) 4141 mstate->dtms_scratch_ptr, size); 4142 mstate->dtms_scratch_ptr += size; 4143 regs[rd] = ptr; 4144 } 4145 break; 4146 } 4147 4148 case DIF_OP_COPYS: 4149 if (!dtrace_canstore(regs[rd], regs[r2], 4150 mstate, vstate)) { 4151 *flags |= CPU_DTRACE_BADADDR; 4152 *illval = regs[rd]; 4153 break; 4154 } 4155 4156 dtrace_bcopy((void *)(uintptr_t)regs[r1], 4157 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 4158 break; 4159 4160 case DIF_OP_STB: 4161 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 4162 *flags |= CPU_DTRACE_BADADDR; 4163 *illval = regs[rd]; 4164 break; 4165 } 4166 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 4167 break; 4168 4169 case DIF_OP_STH: 4170 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 4171 *flags |= CPU_DTRACE_BADADDR; 4172 *illval = regs[rd]; 4173 break; 4174 } 4175 if (regs[rd] & 1) { 4176 *flags |= CPU_DTRACE_BADALIGN; 4177 *illval = regs[rd]; 4178 break; 4179 } 4180 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 4181 break; 4182 4183 case DIF_OP_STW: 4184 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 4185 *flags |= CPU_DTRACE_BADADDR; 4186 *illval = regs[rd]; 4187 break; 4188 } 4189 if (regs[rd] & 3) { 4190 *flags |= CPU_DTRACE_BADALIGN; 4191 *illval = regs[rd]; 4192 break; 4193 } 4194 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 4195 break; 4196 4197 case DIF_OP_STX: 4198 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 4199 *flags |= CPU_DTRACE_BADADDR; 4200 *illval = regs[rd]; 4201 break; 4202 } 4203 if (regs[rd] & 7) { 4204 *flags |= CPU_DTRACE_BADALIGN; 4205 *illval = regs[rd]; 4206 break; 4207 } 4208 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 4209 break; 4210 } 4211 } 4212 4213 if (!(*flags & CPU_DTRACE_FAULT)) 4214 return (rval); 4215 4216 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 4217 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 4218 4219 return (0); 4220 } 4221 4222 static void 4223 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 4224 { 4225 dtrace_probe_t *probe = ecb->dte_probe; 4226 dtrace_provider_t *prov = probe->dtpr_provider; 4227 char c[DTRACE_FULLNAMELEN + 80], *str; 4228 char *msg = "dtrace: breakpoint action at probe "; 4229 char *ecbmsg = " (ecb "; 4230 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 4231 uintptr_t val = (uintptr_t)ecb; 4232 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 4233 4234 if (dtrace_destructive_disallow) 4235 return; 4236 4237 /* 4238 * It's impossible to be taking action on the NULL probe. 4239 */ 4240 ASSERT(probe != NULL); 4241 4242 /* 4243 * This is a poor man's (destitute man's?) sprintf(): we want to 4244 * print the provider name, module name, function name and name of 4245 * the probe, along with the hex address of the ECB with the breakpoint 4246 * action -- all of which we must place in the character buffer by 4247 * hand. 4248 */ 4249 while (*msg != '\0') 4250 c[i++] = *msg++; 4251 4252 for (str = prov->dtpv_name; *str != '\0'; str++) 4253 c[i++] = *str; 4254 c[i++] = ':'; 4255 4256 for (str = probe->dtpr_mod; *str != '\0'; str++) 4257 c[i++] = *str; 4258 c[i++] = ':'; 4259 4260 for (str = probe->dtpr_func; *str != '\0'; str++) 4261 c[i++] = *str; 4262 c[i++] = ':'; 4263 4264 for (str = probe->dtpr_name; *str != '\0'; str++) 4265 c[i++] = *str; 4266 4267 while (*ecbmsg != '\0') 4268 c[i++] = *ecbmsg++; 4269 4270 while (shift >= 0) { 4271 mask = (uintptr_t)0xf << shift; 4272 4273 if (val >= ((uintptr_t)1 << shift)) 4274 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 4275 shift -= 4; 4276 } 4277 4278 c[i++] = ')'; 4279 c[i] = '\0'; 4280 4281 debug_enter(c); 4282 } 4283 4284 static void 4285 dtrace_action_panic(dtrace_ecb_t *ecb) 4286 { 4287 dtrace_probe_t *probe = ecb->dte_probe; 4288 4289 /* 4290 * It's impossible to be taking action on the NULL probe. 4291 */ 4292 ASSERT(probe != NULL); 4293 4294 if (dtrace_destructive_disallow) 4295 return; 4296 4297 if (dtrace_panicked != NULL) 4298 return; 4299 4300 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 4301 return; 4302 4303 /* 4304 * We won the right to panic. (We want to be sure that only one 4305 * thread calls panic() from dtrace_probe(), and that panic() is 4306 * called exactly once.) 4307 */ 4308 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 4309 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 4310 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 4311 } 4312 4313 static void 4314 dtrace_action_raise(uint64_t sig) 4315 { 4316 if (dtrace_destructive_disallow) 4317 return; 4318 4319 if (sig >= NSIG) { 4320 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4321 return; 4322 } 4323 4324 /* 4325 * raise() has a queue depth of 1 -- we ignore all subsequent 4326 * invocations of the raise() action. 4327 */ 4328 if (curthread->t_dtrace_sig == 0) 4329 curthread->t_dtrace_sig = (uint8_t)sig; 4330 4331 curthread->t_sig_check = 1; 4332 aston(curthread); 4333 } 4334 4335 static void 4336 dtrace_action_stop(void) 4337 { 4338 if (dtrace_destructive_disallow) 4339 return; 4340 4341 if (!curthread->t_dtrace_stop) { 4342 curthread->t_dtrace_stop = 1; 4343 curthread->t_sig_check = 1; 4344 aston(curthread); 4345 } 4346 } 4347 4348 static void 4349 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 4350 { 4351 hrtime_t now; 4352 volatile uint16_t *flags; 4353 cpu_t *cpu = CPU; 4354 4355 if (dtrace_destructive_disallow) 4356 return; 4357 4358 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 4359 4360 now = dtrace_gethrtime(); 4361 4362 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 4363 /* 4364 * We need to advance the mark to the current time. 4365 */ 4366 cpu->cpu_dtrace_chillmark = now; 4367 cpu->cpu_dtrace_chilled = 0; 4368 } 4369 4370 /* 4371 * Now check to see if the requested chill time would take us over 4372 * the maximum amount of time allowed in the chill interval. (Or 4373 * worse, if the calculation itself induces overflow.) 4374 */ 4375 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 4376 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 4377 *flags |= CPU_DTRACE_ILLOP; 4378 return; 4379 } 4380 4381 while (dtrace_gethrtime() - now < val) 4382 continue; 4383 4384 /* 4385 * Normally, we assure that the value of the variable "timestamp" does 4386 * not change within an ECB. The presence of chill() represents an 4387 * exception to this rule, however. 4388 */ 4389 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 4390 cpu->cpu_dtrace_chilled += val; 4391 } 4392 4393 static void 4394 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 4395 uint64_t *buf, uint64_t arg) 4396 { 4397 int nframes = DTRACE_USTACK_NFRAMES(arg); 4398 int strsize = DTRACE_USTACK_STRSIZE(arg); 4399 uint64_t *pcs = &buf[1], *fps; 4400 char *str = (char *)&pcs[nframes]; 4401 int size, offs = 0, i, j; 4402 uintptr_t old = mstate->dtms_scratch_ptr, saved; 4403 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 4404 char *sym; 4405 4406 /* 4407 * Should be taking a faster path if string space has not been 4408 * allocated. 4409 */ 4410 ASSERT(strsize != 0); 4411 4412 /* 4413 * We will first allocate some temporary space for the frame pointers. 4414 */ 4415 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4416 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 4417 (nframes * sizeof (uint64_t)); 4418 4419 if (mstate->dtms_scratch_ptr + size > 4420 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 4421 /* 4422 * Not enough room for our frame pointers -- need to indicate 4423 * that we ran out of scratch space. 4424 */ 4425 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4426 return; 4427 } 4428 4429 mstate->dtms_scratch_ptr += size; 4430 saved = mstate->dtms_scratch_ptr; 4431 4432 /* 4433 * Now get a stack with both program counters and frame pointers. 4434 */ 4435 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4436 dtrace_getufpstack(buf, fps, nframes + 1); 4437 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4438 4439 /* 4440 * If that faulted, we're cooked. 4441 */ 4442 if (*flags & CPU_DTRACE_FAULT) 4443 goto out; 4444 4445 /* 4446 * Now we want to walk up the stack, calling the USTACK helper. For 4447 * each iteration, we restore the scratch pointer. 4448 */ 4449 for (i = 0; i < nframes; i++) { 4450 mstate->dtms_scratch_ptr = saved; 4451 4452 if (offs >= strsize) 4453 break; 4454 4455 sym = (char *)(uintptr_t)dtrace_helper( 4456 DTRACE_HELPER_ACTION_USTACK, 4457 mstate, state, pcs[i], fps[i]); 4458 4459 /* 4460 * If we faulted while running the helper, we're going to 4461 * clear the fault and null out the corresponding string. 4462 */ 4463 if (*flags & CPU_DTRACE_FAULT) { 4464 *flags &= ~CPU_DTRACE_FAULT; 4465 str[offs++] = '\0'; 4466 continue; 4467 } 4468 4469 if (sym == NULL) { 4470 str[offs++] = '\0'; 4471 continue; 4472 } 4473 4474 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4475 4476 /* 4477 * Now copy in the string that the helper returned to us. 4478 */ 4479 for (j = 0; offs + j < strsize; j++) { 4480 if ((str[offs + j] = sym[j]) == '\0') 4481 break; 4482 } 4483 4484 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4485 4486 offs += j + 1; 4487 } 4488 4489 if (offs >= strsize) { 4490 /* 4491 * If we didn't have room for all of the strings, we don't 4492 * abort processing -- this needn't be a fatal error -- but we 4493 * still want to increment a counter (dts_stkstroverflows) to 4494 * allow this condition to be warned about. (If this is from 4495 * a jstack() action, it is easily tuned via jstackstrsize.) 4496 */ 4497 dtrace_error(&state->dts_stkstroverflows); 4498 } 4499 4500 while (offs < strsize) 4501 str[offs++] = '\0'; 4502 4503 out: 4504 mstate->dtms_scratch_ptr = old; 4505 } 4506 4507 /* 4508 * If you're looking for the epicenter of DTrace, you just found it. This 4509 * is the function called by the provider to fire a probe -- from which all 4510 * subsequent probe-context DTrace activity emanates. 4511 */ 4512 void 4513 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 4514 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 4515 { 4516 processorid_t cpuid; 4517 dtrace_icookie_t cookie; 4518 dtrace_probe_t *probe; 4519 dtrace_mstate_t mstate; 4520 dtrace_ecb_t *ecb; 4521 dtrace_action_t *act; 4522 intptr_t offs; 4523 size_t size; 4524 int vtime, onintr; 4525 volatile uint16_t *flags; 4526 hrtime_t now; 4527 4528 /* 4529 * Kick out immediately if this CPU is still being born (in which case 4530 * curthread will be set to -1) 4531 */ 4532 if ((uintptr_t)curthread & 1) 4533 return; 4534 4535 cookie = dtrace_interrupt_disable(); 4536 probe = dtrace_probes[id - 1]; 4537 cpuid = CPU->cpu_id; 4538 onintr = CPU_ON_INTR(CPU); 4539 4540 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 4541 probe->dtpr_predcache == curthread->t_predcache) { 4542 /* 4543 * We have hit in the predicate cache; we know that 4544 * this predicate would evaluate to be false. 4545 */ 4546 dtrace_interrupt_enable(cookie); 4547 return; 4548 } 4549 4550 if (panic_quiesce) { 4551 /* 4552 * We don't trace anything if we're panicking. 4553 */ 4554 dtrace_interrupt_enable(cookie); 4555 return; 4556 } 4557 4558 now = dtrace_gethrtime(); 4559 vtime = dtrace_vtime_references != 0; 4560 4561 if (vtime && curthread->t_dtrace_start) 4562 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 4563 4564 mstate.dtms_probe = probe; 4565 mstate.dtms_arg[0] = arg0; 4566 mstate.dtms_arg[1] = arg1; 4567 mstate.dtms_arg[2] = arg2; 4568 mstate.dtms_arg[3] = arg3; 4569 mstate.dtms_arg[4] = arg4; 4570 4571 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 4572 4573 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 4574 dtrace_predicate_t *pred = ecb->dte_predicate; 4575 dtrace_state_t *state = ecb->dte_state; 4576 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 4577 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 4578 dtrace_vstate_t *vstate = &state->dts_vstate; 4579 dtrace_provider_t *prov = probe->dtpr_provider; 4580 int committed = 0; 4581 caddr_t tomax; 4582 4583 /* 4584 * A little subtlety with the following (seemingly innocuous) 4585 * declaration of the automatic 'val': by looking at the 4586 * code, you might think that it could be declared in the 4587 * action processing loop, below. (That is, it's only used in 4588 * the action processing loop.) However, it must be declared 4589 * out of that scope because in the case of DIF expression 4590 * arguments to aggregating actions, one iteration of the 4591 * action loop will use the last iteration's value. 4592 */ 4593 #ifdef lint 4594 uint64_t val = 0; 4595 #else 4596 uint64_t val; 4597 #endif 4598 4599 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 4600 *flags &= ~CPU_DTRACE_ERROR; 4601 4602 if (prov == dtrace_provider) { 4603 /* 4604 * If dtrace itself is the provider of this probe, 4605 * we're only going to continue processing the ECB if 4606 * arg0 (the dtrace_state_t) is equal to the ECB's 4607 * creating state. (This prevents disjoint consumers 4608 * from seeing one another's metaprobes.) 4609 */ 4610 if (arg0 != (uint64_t)(uintptr_t)state) 4611 continue; 4612 } 4613 4614 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 4615 /* 4616 * We're not currently active. If our provider isn't 4617 * the dtrace pseudo provider, we're not interested. 4618 */ 4619 if (prov != dtrace_provider) 4620 continue; 4621 4622 /* 4623 * Now we must further check if we are in the BEGIN 4624 * probe. If we are, we will only continue processing 4625 * if we're still in WARMUP -- if one BEGIN enabling 4626 * has invoked the exit() action, we don't want to 4627 * evaluate subsequent BEGIN enablings. 4628 */ 4629 if (probe->dtpr_id == dtrace_probeid_begin && 4630 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 4631 ASSERT(state->dts_activity == 4632 DTRACE_ACTIVITY_DRAINING); 4633 continue; 4634 } 4635 } 4636 4637 if (ecb->dte_cond) { 4638 /* 4639 * If the dte_cond bits indicate that this 4640 * consumer is only allowed to see user-mode firings 4641 * of this probe, call the provider's dtps_usermode() 4642 * entry point to check that the probe was fired 4643 * while in a user context. Skip this ECB if that's 4644 * not the case. 4645 */ 4646 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 4647 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 4648 probe->dtpr_id, probe->dtpr_arg) == 0) 4649 continue; 4650 4651 /* 4652 * This is more subtle than it looks. We have to be 4653 * absolutely certain that CRED() isn't going to 4654 * change out from under us so it's only legit to 4655 * examine that structure if we're in constrained 4656 * situations. Currently, the only times we'll this 4657 * check is if a non-super-user has enabled the 4658 * profile or syscall providers -- providers that 4659 * allow visibility of all processes. For the 4660 * profile case, the check above will ensure that 4661 * we're examining a user context. 4662 */ 4663 if (ecb->dte_cond & DTRACE_COND_OWNER) { 4664 uid_t uid = ecb->dte_state->dts_cred.dcr_uid; 4665 gid_t gid = ecb->dte_state->dts_cred.dcr_gid; 4666 cred_t *cr; 4667 proc_t *proc; 4668 4669 if ((cr = CRED()) == NULL || 4670 uid != cr->cr_uid || 4671 uid != cr->cr_ruid || 4672 uid != cr->cr_suid || 4673 gid != cr->cr_gid || 4674 gid != cr->cr_rgid || 4675 gid != cr->cr_sgid || 4676 (proc = ttoproc(curthread)) == NULL || 4677 (proc->p_flag & SNOCD)) 4678 continue; 4679 4680 } 4681 } 4682 4683 if (now - state->dts_alive > dtrace_deadman_timeout) { 4684 /* 4685 * We seem to be dead. Unless we (a) have kernel 4686 * destructive permissions (b) have expicitly enabled 4687 * destructive actions and (c) destructive actions have 4688 * not been disabled, we're going to transition into 4689 * the KILLED state, from which no further processing 4690 * on this state will be performed. 4691 */ 4692 if (!dtrace_priv_kernel_destructive(state) || 4693 !state->dts_cred.dcr_destructive || 4694 dtrace_destructive_disallow) { 4695 void *activity = &state->dts_activity; 4696 dtrace_activity_t current; 4697 4698 do { 4699 current = state->dts_activity; 4700 } while (dtrace_cas32(activity, current, 4701 DTRACE_ACTIVITY_KILLED) != current); 4702 4703 continue; 4704 } 4705 } 4706 4707 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 4708 ecb->dte_alignment, state, &mstate)) < 0) 4709 continue; 4710 4711 tomax = buf->dtb_tomax; 4712 ASSERT(tomax != NULL); 4713 4714 if (ecb->dte_size != 0) 4715 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 4716 4717 mstate.dtms_epid = ecb->dte_epid; 4718 mstate.dtms_present |= DTRACE_MSTATE_EPID; 4719 4720 if (pred != NULL) { 4721 dtrace_difo_t *dp = pred->dtp_difo; 4722 int rval; 4723 4724 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 4725 4726 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 4727 dtrace_cacheid_t cid = probe->dtpr_predcache; 4728 4729 if (cid != DTRACE_CACHEIDNONE && !onintr) { 4730 /* 4731 * Update the predicate cache... 4732 */ 4733 ASSERT(cid == pred->dtp_cacheid); 4734 curthread->t_predcache = cid; 4735 } 4736 4737 continue; 4738 } 4739 } 4740 4741 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 4742 act != NULL; act = act->dta_next) { 4743 size_t valoffs; 4744 dtrace_difo_t *dp; 4745 dtrace_recdesc_t *rec = &act->dta_rec; 4746 4747 size = rec->dtrd_size; 4748 valoffs = offs + rec->dtrd_offset; 4749 4750 if (DTRACEACT_ISAGG(act->dta_kind)) { 4751 uint64_t v = 0xbad; 4752 dtrace_aggregation_t *agg; 4753 4754 agg = (dtrace_aggregation_t *)act; 4755 4756 if ((dp = act->dta_difo) != NULL) 4757 v = dtrace_dif_emulate(dp, 4758 &mstate, vstate, state); 4759 4760 if (*flags & CPU_DTRACE_ERROR) 4761 continue; 4762 4763 /* 4764 * Note that we always pass the expression 4765 * value from the previous iteration of the 4766 * action loop. This value will only be used 4767 * if there is an expression argument to the 4768 * aggregating action, denoted by the 4769 * dtag_hasarg field. 4770 */ 4771 dtrace_aggregate(agg, buf, 4772 offs, aggbuf, v, val); 4773 continue; 4774 } 4775 4776 switch (act->dta_kind) { 4777 case DTRACEACT_STOP: 4778 if (dtrace_priv_proc_destructive(state)) 4779 dtrace_action_stop(); 4780 continue; 4781 4782 case DTRACEACT_BREAKPOINT: 4783 if (dtrace_priv_kernel_destructive(state)) 4784 dtrace_action_breakpoint(ecb); 4785 continue; 4786 4787 case DTRACEACT_PANIC: 4788 if (dtrace_priv_kernel_destructive(state)) 4789 dtrace_action_panic(ecb); 4790 continue; 4791 4792 case DTRACEACT_STACK: 4793 if (!dtrace_priv_kernel(state)) 4794 continue; 4795 4796 dtrace_getpcstack((pc_t *)(tomax + valoffs), 4797 size / sizeof (pc_t), probe->dtpr_aframes, 4798 DTRACE_ANCHORED(probe) ? NULL : 4799 (uint32_t *)arg0); 4800 4801 continue; 4802 4803 case DTRACEACT_JSTACK: 4804 case DTRACEACT_USTACK: 4805 if (!dtrace_priv_proc(state)) 4806 continue; 4807 4808 /* 4809 * See comment in DIF_VAR_PID. 4810 */ 4811 if (DTRACE_ANCHORED(mstate.dtms_probe) && 4812 CPU_ON_INTR(CPU)) { 4813 int depth = DTRACE_USTACK_NFRAMES( 4814 rec->dtrd_arg) + 1; 4815 4816 dtrace_bzero((void *)(tomax + valoffs), 4817 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 4818 + depth * sizeof (uint64_t)); 4819 4820 continue; 4821 } 4822 4823 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 4824 curproc->p_dtrace_helpers != NULL) { 4825 /* 4826 * This is the slow path -- we have 4827 * allocated string space, and we're 4828 * getting the stack of a process that 4829 * has helpers. Call into a separate 4830 * routine to perform this processing. 4831 */ 4832 dtrace_action_ustack(&mstate, state, 4833 (uint64_t *)(tomax + valoffs), 4834 rec->dtrd_arg); 4835 continue; 4836 } 4837 4838 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4839 dtrace_getupcstack((uint64_t *) 4840 (tomax + valoffs), 4841 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 4842 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4843 continue; 4844 4845 default: 4846 break; 4847 } 4848 4849 dp = act->dta_difo; 4850 ASSERT(dp != NULL); 4851 4852 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 4853 4854 if (*flags & CPU_DTRACE_ERROR) 4855 continue; 4856 4857 switch (act->dta_kind) { 4858 case DTRACEACT_SPECULATE: 4859 ASSERT(buf == &state->dts_buffer[cpuid]); 4860 buf = dtrace_speculation_buffer(state, 4861 cpuid, val); 4862 4863 if (buf == NULL) { 4864 *flags |= CPU_DTRACE_DROP; 4865 continue; 4866 } 4867 4868 offs = dtrace_buffer_reserve(buf, 4869 ecb->dte_needed, ecb->dte_alignment, 4870 state, NULL); 4871 4872 if (offs < 0) { 4873 *flags |= CPU_DTRACE_DROP; 4874 continue; 4875 } 4876 4877 tomax = buf->dtb_tomax; 4878 ASSERT(tomax != NULL); 4879 4880 if (ecb->dte_size != 0) 4881 DTRACE_STORE(uint32_t, tomax, offs, 4882 ecb->dte_epid); 4883 continue; 4884 4885 case DTRACEACT_CHILL: 4886 if (dtrace_priv_kernel_destructive(state)) 4887 dtrace_action_chill(&mstate, val); 4888 continue; 4889 4890 case DTRACEACT_RAISE: 4891 if (dtrace_priv_proc_destructive(state)) 4892 dtrace_action_raise(val); 4893 continue; 4894 4895 case DTRACEACT_COMMIT: 4896 ASSERT(!committed); 4897 4898 /* 4899 * We need to commit our buffer state. 4900 */ 4901 if (ecb->dte_size) 4902 buf->dtb_offset = offs + ecb->dte_size; 4903 buf = &state->dts_buffer[cpuid]; 4904 dtrace_speculation_commit(state, cpuid, val); 4905 committed = 1; 4906 continue; 4907 4908 case DTRACEACT_DISCARD: 4909 dtrace_speculation_discard(state, cpuid, val); 4910 continue; 4911 4912 case DTRACEACT_DIFEXPR: 4913 case DTRACEACT_LIBACT: 4914 case DTRACEACT_PRINTF: 4915 case DTRACEACT_PRINTA: 4916 case DTRACEACT_SYSTEM: 4917 case DTRACEACT_FREOPEN: 4918 break; 4919 4920 case DTRACEACT_SYM: 4921 case DTRACEACT_MOD: 4922 if (!dtrace_priv_kernel(state)) 4923 continue; 4924 break; 4925 4926 case DTRACEACT_USYM: 4927 case DTRACEACT_UMOD: 4928 case DTRACEACT_UADDR: { 4929 struct pid *pid = curthread->t_procp->p_pidp; 4930 4931 if (!dtrace_priv_proc(state)) 4932 continue; 4933 4934 DTRACE_STORE(uint64_t, tomax, 4935 valoffs, (uint64_t)pid->pid_id); 4936 DTRACE_STORE(uint64_t, tomax, 4937 valoffs + sizeof (uint64_t), val); 4938 4939 continue; 4940 } 4941 4942 case DTRACEACT_EXIT: { 4943 /* 4944 * For the exit action, we are going to attempt 4945 * to atomically set our activity to be 4946 * draining. If this fails (either because 4947 * another CPU has beat us to the exit action, 4948 * or because our current activity is something 4949 * other than ACTIVE or WARMUP), we will 4950 * continue. This assures that the exit action 4951 * can be successfully recorded at most once 4952 * when we're in the ACTIVE state. If we're 4953 * encountering the exit() action while in 4954 * COOLDOWN, however, we want to honor the new 4955 * status code. (We know that we're the only 4956 * thread in COOLDOWN, so there is no race.) 4957 */ 4958 void *activity = &state->dts_activity; 4959 dtrace_activity_t current = state->dts_activity; 4960 4961 if (current == DTRACE_ACTIVITY_COOLDOWN) 4962 break; 4963 4964 if (current != DTRACE_ACTIVITY_WARMUP) 4965 current = DTRACE_ACTIVITY_ACTIVE; 4966 4967 if (dtrace_cas32(activity, current, 4968 DTRACE_ACTIVITY_DRAINING) != current) { 4969 *flags |= CPU_DTRACE_DROP; 4970 continue; 4971 } 4972 4973 break; 4974 } 4975 4976 default: 4977 ASSERT(0); 4978 } 4979 4980 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 4981 uintptr_t end = valoffs + size; 4982 4983 /* 4984 * If this is a string, we're going to only 4985 * load until we find the zero byte -- after 4986 * which we'll store zero bytes. 4987 */ 4988 if (dp->dtdo_rtype.dtdt_kind == 4989 DIF_TYPE_STRING) { 4990 char c = '\0' + 1; 4991 size_t s; 4992 4993 for (s = 0; s < size; s++) { 4994 if (c != '\0') 4995 c = dtrace_load8(val++); 4996 4997 DTRACE_STORE(uint8_t, tomax, 4998 valoffs++, c); 4999 } 5000 5001 continue; 5002 } 5003 5004 while (valoffs < end) { 5005 DTRACE_STORE(uint8_t, tomax, valoffs++, 5006 dtrace_load8(val++)); 5007 } 5008 5009 continue; 5010 } 5011 5012 switch (size) { 5013 case 0: 5014 break; 5015 5016 case sizeof (uint8_t): 5017 DTRACE_STORE(uint8_t, tomax, valoffs, val); 5018 break; 5019 case sizeof (uint16_t): 5020 DTRACE_STORE(uint16_t, tomax, valoffs, val); 5021 break; 5022 case sizeof (uint32_t): 5023 DTRACE_STORE(uint32_t, tomax, valoffs, val); 5024 break; 5025 case sizeof (uint64_t): 5026 DTRACE_STORE(uint64_t, tomax, valoffs, val); 5027 break; 5028 default: 5029 /* 5030 * Any other size should have been returned by 5031 * reference, not by value. 5032 */ 5033 ASSERT(0); 5034 break; 5035 } 5036 } 5037 5038 if (*flags & CPU_DTRACE_DROP) 5039 continue; 5040 5041 if (*flags & CPU_DTRACE_FAULT) { 5042 int ndx; 5043 dtrace_action_t *err; 5044 5045 buf->dtb_errors++; 5046 5047 if (probe->dtpr_id == dtrace_probeid_error) { 5048 /* 5049 * There's nothing we can do -- we had an 5050 * error on the error probe. We bump an 5051 * error counter to at least indicate that 5052 * this condition happened. 5053 */ 5054 dtrace_error(&state->dts_dblerrors); 5055 continue; 5056 } 5057 5058 if (vtime) { 5059 /* 5060 * Before recursing on dtrace_probe(), we 5061 * need to explicitly clear out our start 5062 * time to prevent it from being accumulated 5063 * into t_dtrace_vtime. 5064 */ 5065 curthread->t_dtrace_start = 0; 5066 } 5067 5068 /* 5069 * Iterate over the actions to figure out which action 5070 * we were processing when we experienced the error. 5071 * Note that act points _past_ the faulting action; if 5072 * act is ecb->dte_action, the fault was in the 5073 * predicate, if it's ecb->dte_action->dta_next it's 5074 * in action #1, and so on. 5075 */ 5076 for (err = ecb->dte_action, ndx = 0; 5077 err != act; err = err->dta_next, ndx++) 5078 continue; 5079 5080 dtrace_probe_error(state, ecb->dte_epid, ndx, 5081 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 5082 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 5083 cpu_core[cpuid].cpuc_dtrace_illval); 5084 5085 continue; 5086 } 5087 5088 if (!committed) 5089 buf->dtb_offset = offs + ecb->dte_size; 5090 } 5091 5092 if (vtime) 5093 curthread->t_dtrace_start = dtrace_gethrtime(); 5094 5095 dtrace_interrupt_enable(cookie); 5096 } 5097 5098 /* 5099 * DTrace Probe Hashing Functions 5100 * 5101 * The functions in this section (and indeed, the functions in remaining 5102 * sections) are not _called_ from probe context. (Any exceptions to this are 5103 * marked with a "Note:".) Rather, they are called from elsewhere in the 5104 * DTrace framework to look-up probes in, add probes to and remove probes from 5105 * the DTrace probe hashes. (Each probe is hashed by each element of the 5106 * probe tuple -- allowing for fast lookups, regardless of what was 5107 * specified.) 5108 */ 5109 static uint_t 5110 dtrace_hash_str(char *p) 5111 { 5112 unsigned int g; 5113 uint_t hval = 0; 5114 5115 while (*p) { 5116 hval = (hval << 4) + *p++; 5117 if ((g = (hval & 0xf0000000)) != 0) 5118 hval ^= g >> 24; 5119 hval &= ~g; 5120 } 5121 return (hval); 5122 } 5123 5124 static dtrace_hash_t * 5125 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 5126 { 5127 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 5128 5129 hash->dth_stroffs = stroffs; 5130 hash->dth_nextoffs = nextoffs; 5131 hash->dth_prevoffs = prevoffs; 5132 5133 hash->dth_size = 1; 5134 hash->dth_mask = hash->dth_size - 1; 5135 5136 hash->dth_tab = kmem_zalloc(hash->dth_size * 5137 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 5138 5139 return (hash); 5140 } 5141 5142 static void 5143 dtrace_hash_destroy(dtrace_hash_t *hash) 5144 { 5145 #ifdef DEBUG 5146 int i; 5147 5148 for (i = 0; i < hash->dth_size; i++) 5149 ASSERT(hash->dth_tab[i] == NULL); 5150 #endif 5151 5152 kmem_free(hash->dth_tab, 5153 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 5154 kmem_free(hash, sizeof (dtrace_hash_t)); 5155 } 5156 5157 static void 5158 dtrace_hash_resize(dtrace_hash_t *hash) 5159 { 5160 int size = hash->dth_size, i, ndx; 5161 int new_size = hash->dth_size << 1; 5162 int new_mask = new_size - 1; 5163 dtrace_hashbucket_t **new_tab, *bucket, *next; 5164 5165 ASSERT((new_size & new_mask) == 0); 5166 5167 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 5168 5169 for (i = 0; i < size; i++) { 5170 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 5171 dtrace_probe_t *probe = bucket->dthb_chain; 5172 5173 ASSERT(probe != NULL); 5174 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 5175 5176 next = bucket->dthb_next; 5177 bucket->dthb_next = new_tab[ndx]; 5178 new_tab[ndx] = bucket; 5179 } 5180 } 5181 5182 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 5183 hash->dth_tab = new_tab; 5184 hash->dth_size = new_size; 5185 hash->dth_mask = new_mask; 5186 } 5187 5188 static void 5189 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 5190 { 5191 int hashval = DTRACE_HASHSTR(hash, new); 5192 int ndx = hashval & hash->dth_mask; 5193 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5194 dtrace_probe_t **nextp, **prevp; 5195 5196 for (; bucket != NULL; bucket = bucket->dthb_next) { 5197 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 5198 goto add; 5199 } 5200 5201 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 5202 dtrace_hash_resize(hash); 5203 dtrace_hash_add(hash, new); 5204 return; 5205 } 5206 5207 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 5208 bucket->dthb_next = hash->dth_tab[ndx]; 5209 hash->dth_tab[ndx] = bucket; 5210 hash->dth_nbuckets++; 5211 5212 add: 5213 nextp = DTRACE_HASHNEXT(hash, new); 5214 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 5215 *nextp = bucket->dthb_chain; 5216 5217 if (bucket->dthb_chain != NULL) { 5218 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 5219 ASSERT(*prevp == NULL); 5220 *prevp = new; 5221 } 5222 5223 bucket->dthb_chain = new; 5224 bucket->dthb_len++; 5225 } 5226 5227 static dtrace_probe_t * 5228 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 5229 { 5230 int hashval = DTRACE_HASHSTR(hash, template); 5231 int ndx = hashval & hash->dth_mask; 5232 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5233 5234 for (; bucket != NULL; bucket = bucket->dthb_next) { 5235 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 5236 return (bucket->dthb_chain); 5237 } 5238 5239 return (NULL); 5240 } 5241 5242 static int 5243 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 5244 { 5245 int hashval = DTRACE_HASHSTR(hash, template); 5246 int ndx = hashval & hash->dth_mask; 5247 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5248 5249 for (; bucket != NULL; bucket = bucket->dthb_next) { 5250 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 5251 return (bucket->dthb_len); 5252 } 5253 5254 return (NULL); 5255 } 5256 5257 static void 5258 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 5259 { 5260 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 5261 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5262 5263 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 5264 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 5265 5266 /* 5267 * Find the bucket that we're removing this probe from. 5268 */ 5269 for (; bucket != NULL; bucket = bucket->dthb_next) { 5270 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 5271 break; 5272 } 5273 5274 ASSERT(bucket != NULL); 5275 5276 if (*prevp == NULL) { 5277 if (*nextp == NULL) { 5278 /* 5279 * The removed probe was the only probe on this 5280 * bucket; we need to remove the bucket. 5281 */ 5282 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 5283 5284 ASSERT(bucket->dthb_chain == probe); 5285 ASSERT(b != NULL); 5286 5287 if (b == bucket) { 5288 hash->dth_tab[ndx] = bucket->dthb_next; 5289 } else { 5290 while (b->dthb_next != bucket) 5291 b = b->dthb_next; 5292 b->dthb_next = bucket->dthb_next; 5293 } 5294 5295 ASSERT(hash->dth_nbuckets > 0); 5296 hash->dth_nbuckets--; 5297 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 5298 return; 5299 } 5300 5301 bucket->dthb_chain = *nextp; 5302 } else { 5303 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 5304 } 5305 5306 if (*nextp != NULL) 5307 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 5308 } 5309 5310 /* 5311 * DTrace Utility Functions 5312 * 5313 * These are random utility functions that are _not_ called from probe context. 5314 */ 5315 static int 5316 dtrace_badattr(const dtrace_attribute_t *a) 5317 { 5318 return (a->dtat_name > DTRACE_STABILITY_MAX || 5319 a->dtat_data > DTRACE_STABILITY_MAX || 5320 a->dtat_class > DTRACE_CLASS_MAX); 5321 } 5322 5323 /* 5324 * Return a duplicate copy of a string. If the specified string is NULL, 5325 * this function returns a zero-length string. 5326 */ 5327 static char * 5328 dtrace_strdup(const char *str) 5329 { 5330 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 5331 5332 if (str != NULL) 5333 (void) strcpy(new, str); 5334 5335 return (new); 5336 } 5337 5338 #define DTRACE_ISALPHA(c) \ 5339 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 5340 5341 static int 5342 dtrace_badname(const char *s) 5343 { 5344 char c; 5345 5346 if (s == NULL || (c = *s++) == '\0') 5347 return (0); 5348 5349 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 5350 return (1); 5351 5352 while ((c = *s++) != '\0') { 5353 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 5354 c != '-' && c != '_' && c != '.' && c != '`') 5355 return (1); 5356 } 5357 5358 return (0); 5359 } 5360 5361 static void 5362 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp) 5363 { 5364 uint32_t priv; 5365 5366 *uidp = crgetuid(cr); 5367 if (PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 5368 priv = DTRACE_PRIV_ALL; 5369 } else { 5370 priv = 0; 5371 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 5372 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 5373 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 5374 priv |= DTRACE_PRIV_USER; 5375 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 5376 priv |= DTRACE_PRIV_PROC; 5377 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 5378 priv |= DTRACE_PRIV_OWNER; 5379 } 5380 5381 *privp = priv; 5382 } 5383 5384 #ifdef DTRACE_ERRDEBUG 5385 static void 5386 dtrace_errdebug(const char *str) 5387 { 5388 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; 5389 int occupied = 0; 5390 5391 mutex_enter(&dtrace_errlock); 5392 dtrace_errlast = str; 5393 dtrace_errthread = curthread; 5394 5395 while (occupied++ < DTRACE_ERRHASHSZ) { 5396 if (dtrace_errhash[hval].dter_msg == str) { 5397 dtrace_errhash[hval].dter_count++; 5398 goto out; 5399 } 5400 5401 if (dtrace_errhash[hval].dter_msg != NULL) { 5402 hval = (hval + 1) % DTRACE_ERRHASHSZ; 5403 continue; 5404 } 5405 5406 dtrace_errhash[hval].dter_msg = str; 5407 dtrace_errhash[hval].dter_count = 1; 5408 goto out; 5409 } 5410 5411 panic("dtrace: undersized error hash"); 5412 out: 5413 mutex_exit(&dtrace_errlock); 5414 } 5415 #endif 5416 5417 /* 5418 * DTrace Matching Functions 5419 * 5420 * These functions are used to match groups of probes, given some elements of 5421 * a probe tuple, or some globbed expressions for elements of a probe tuple. 5422 */ 5423 static int 5424 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid) 5425 { 5426 if (priv != DTRACE_PRIV_ALL) { 5427 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 5428 uint32_t match = priv & ppriv; 5429 5430 /* 5431 * No PRIV_DTRACE_* privileges... 5432 */ 5433 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 5434 DTRACE_PRIV_KERNEL)) == 0) 5435 return (0); 5436 5437 /* 5438 * No matching bits, but there were bits to match... 5439 */ 5440 if (match == 0 && ppriv != 0) 5441 return (0); 5442 5443 /* 5444 * Need to have permissions to the process, but don't... 5445 */ 5446 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 5447 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) 5448 return (0); 5449 } 5450 5451 return (1); 5452 } 5453 5454 /* 5455 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 5456 * consists of input pattern strings and an ops-vector to evaluate them. 5457 * This function returns >0 for match, 0 for no match, and <0 for error. 5458 */ 5459 static int 5460 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 5461 uint32_t priv, uid_t uid) 5462 { 5463 dtrace_provider_t *pvp = prp->dtpr_provider; 5464 int rv; 5465 5466 if (pvp->dtpv_defunct) 5467 return (0); 5468 5469 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 5470 return (rv); 5471 5472 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 5473 return (rv); 5474 5475 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 5476 return (rv); 5477 5478 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 5479 return (rv); 5480 5481 if (dtrace_match_priv(prp, priv, uid) == 0) 5482 return (0); 5483 5484 return (rv); 5485 } 5486 5487 /* 5488 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 5489 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 5490 * libc's version, the kernel version only applies to 8-bit ASCII strings. 5491 * In addition, all of the recursion cases except for '*' matching have been 5492 * unwound. For '*', we still implement recursive evaluation, but a depth 5493 * counter is maintained and matching is aborted if we recurse too deep. 5494 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 5495 */ 5496 static int 5497 dtrace_match_glob(const char *s, const char *p, int depth) 5498 { 5499 const char *olds; 5500 char s1, c; 5501 int gs; 5502 5503 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 5504 return (-1); 5505 5506 if (s == NULL) 5507 s = ""; /* treat NULL as empty string */ 5508 5509 top: 5510 olds = s; 5511 s1 = *s++; 5512 5513 if (p == NULL) 5514 return (0); 5515 5516 if ((c = *p++) == '\0') 5517 return (s1 == '\0'); 5518 5519 switch (c) { 5520 case '[': { 5521 int ok = 0, notflag = 0; 5522 char lc = '\0'; 5523 5524 if (s1 == '\0') 5525 return (0); 5526 5527 if (*p == '!') { 5528 notflag = 1; 5529 p++; 5530 } 5531 5532 if ((c = *p++) == '\0') 5533 return (0); 5534 5535 do { 5536 if (c == '-' && lc != '\0' && *p != ']') { 5537 if ((c = *p++) == '\0') 5538 return (0); 5539 if (c == '\\' && (c = *p++) == '\0') 5540 return (0); 5541 5542 if (notflag) { 5543 if (s1 < lc || s1 > c) 5544 ok++; 5545 else 5546 return (0); 5547 } else if (lc <= s1 && s1 <= c) 5548 ok++; 5549 5550 } else if (c == '\\' && (c = *p++) == '\0') 5551 return (0); 5552 5553 lc = c; /* save left-hand 'c' for next iteration */ 5554 5555 if (notflag) { 5556 if (s1 != c) 5557 ok++; 5558 else 5559 return (0); 5560 } else if (s1 == c) 5561 ok++; 5562 5563 if ((c = *p++) == '\0') 5564 return (0); 5565 5566 } while (c != ']'); 5567 5568 if (ok) 5569 goto top; 5570 5571 return (0); 5572 } 5573 5574 case '\\': 5575 if ((c = *p++) == '\0') 5576 return (0); 5577 /*FALLTHRU*/ 5578 5579 default: 5580 if (c != s1) 5581 return (0); 5582 /*FALLTHRU*/ 5583 5584 case '?': 5585 if (s1 != '\0') 5586 goto top; 5587 return (0); 5588 5589 case '*': 5590 while (*p == '*') 5591 p++; /* consecutive *'s are identical to a single one */ 5592 5593 if (*p == '\0') 5594 return (1); 5595 5596 for (s = olds; *s != '\0'; s++) { 5597 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 5598 return (gs); 5599 } 5600 5601 return (0); 5602 } 5603 } 5604 5605 /*ARGSUSED*/ 5606 static int 5607 dtrace_match_string(const char *s, const char *p, int depth) 5608 { 5609 return (s != NULL && strcmp(s, p) == 0); 5610 } 5611 5612 /*ARGSUSED*/ 5613 static int 5614 dtrace_match_nul(const char *s, const char *p, int depth) 5615 { 5616 return (1); /* always match the empty pattern */ 5617 } 5618 5619 /*ARGSUSED*/ 5620 static int 5621 dtrace_match_nonzero(const char *s, const char *p, int depth) 5622 { 5623 return (s != NULL && s[0] != '\0'); 5624 } 5625 5626 static int 5627 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 5628 int (*matched)(dtrace_probe_t *, void *), void *arg) 5629 { 5630 dtrace_probe_t template, *probe; 5631 dtrace_hash_t *hash = NULL; 5632 int len, best = INT_MAX, nmatched = 0; 5633 dtrace_id_t i; 5634 5635 ASSERT(MUTEX_HELD(&dtrace_lock)); 5636 5637 /* 5638 * If the probe ID is specified in the key, just lookup by ID and 5639 * invoke the match callback once if a matching probe is found. 5640 */ 5641 if (pkp->dtpk_id != DTRACE_IDNONE) { 5642 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 5643 dtrace_match_probe(probe, pkp, priv, uid) > 0) { 5644 (void) (*matched)(probe, arg); 5645 nmatched++; 5646 } 5647 return (nmatched); 5648 } 5649 5650 template.dtpr_mod = (char *)pkp->dtpk_mod; 5651 template.dtpr_func = (char *)pkp->dtpk_func; 5652 template.dtpr_name = (char *)pkp->dtpk_name; 5653 5654 /* 5655 * We want to find the most distinct of the module name, function 5656 * name, and name. So for each one that is not a glob pattern or 5657 * empty string, we perform a lookup in the corresponding hash and 5658 * use the hash table with the fewest collisions to do our search. 5659 */ 5660 if (pkp->dtpk_mmatch == &dtrace_match_string && 5661 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 5662 best = len; 5663 hash = dtrace_bymod; 5664 } 5665 5666 if (pkp->dtpk_fmatch == &dtrace_match_string && 5667 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 5668 best = len; 5669 hash = dtrace_byfunc; 5670 } 5671 5672 if (pkp->dtpk_nmatch == &dtrace_match_string && 5673 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 5674 best = len; 5675 hash = dtrace_byname; 5676 } 5677 5678 /* 5679 * If we did not select a hash table, iterate over every probe and 5680 * invoke our callback for each one that matches our input probe key. 5681 */ 5682 if (hash == NULL) { 5683 for (i = 0; i < dtrace_nprobes; i++) { 5684 if ((probe = dtrace_probes[i]) == NULL || 5685 dtrace_match_probe(probe, pkp, priv, uid) <= 0) 5686 continue; 5687 5688 nmatched++; 5689 5690 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 5691 break; 5692 } 5693 5694 return (nmatched); 5695 } 5696 5697 /* 5698 * If we selected a hash table, iterate over each probe of the same key 5699 * name and invoke the callback for every probe that matches the other 5700 * attributes of our input probe key. 5701 */ 5702 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 5703 probe = *(DTRACE_HASHNEXT(hash, probe))) { 5704 5705 if (dtrace_match_probe(probe, pkp, priv, uid) <= 0) 5706 continue; 5707 5708 nmatched++; 5709 5710 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 5711 break; 5712 } 5713 5714 return (nmatched); 5715 } 5716 5717 /* 5718 * Return the function pointer dtrace_probecmp() should use to compare the 5719 * specified pattern with a string. For NULL or empty patterns, we select 5720 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 5721 * For non-empty non-glob strings, we use dtrace_match_string(). 5722 */ 5723 static dtrace_probekey_f * 5724 dtrace_probekey_func(const char *p) 5725 { 5726 char c; 5727 5728 if (p == NULL || *p == '\0') 5729 return (&dtrace_match_nul); 5730 5731 while ((c = *p++) != '\0') { 5732 if (c == '[' || c == '?' || c == '*' || c == '\\') 5733 return (&dtrace_match_glob); 5734 } 5735 5736 return (&dtrace_match_string); 5737 } 5738 5739 /* 5740 * Build a probe comparison key for use with dtrace_match_probe() from the 5741 * given probe description. By convention, a null key only matches anchored 5742 * probes: if each field is the empty string, reset dtpk_fmatch to 5743 * dtrace_match_nonzero(). 5744 */ 5745 static void 5746 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 5747 { 5748 pkp->dtpk_prov = pdp->dtpd_provider; 5749 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 5750 5751 pkp->dtpk_mod = pdp->dtpd_mod; 5752 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 5753 5754 pkp->dtpk_func = pdp->dtpd_func; 5755 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 5756 5757 pkp->dtpk_name = pdp->dtpd_name; 5758 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 5759 5760 pkp->dtpk_id = pdp->dtpd_id; 5761 5762 if (pkp->dtpk_id == DTRACE_IDNONE && 5763 pkp->dtpk_pmatch == &dtrace_match_nul && 5764 pkp->dtpk_mmatch == &dtrace_match_nul && 5765 pkp->dtpk_fmatch == &dtrace_match_nul && 5766 pkp->dtpk_nmatch == &dtrace_match_nul) 5767 pkp->dtpk_fmatch = &dtrace_match_nonzero; 5768 } 5769 5770 /* 5771 * DTrace Provider-to-Framework API Functions 5772 * 5773 * These functions implement much of the Provider-to-Framework API, as 5774 * described in <sys/dtrace.h>. The parts of the API not in this section are 5775 * the functions in the API for probe management (found below), and 5776 * dtrace_probe() itself (found above). 5777 */ 5778 5779 /* 5780 * Register the calling provider with the DTrace framework. This should 5781 * generally be called by DTrace providers in their attach(9E) entry point. 5782 */ 5783 int 5784 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 5785 uid_t uid, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 5786 { 5787 dtrace_provider_t *provider; 5788 5789 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 5790 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5791 "arguments", name ? name : "<NULL>"); 5792 return (EINVAL); 5793 } 5794 5795 if (name[0] == '\0' || dtrace_badname(name)) { 5796 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5797 "provider name", name); 5798 return (EINVAL); 5799 } 5800 5801 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 5802 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 5803 pops->dtps_destroy == NULL || 5804 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 5805 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5806 "provider ops", name); 5807 return (EINVAL); 5808 } 5809 5810 if (dtrace_badattr(&pap->dtpa_provider) || 5811 dtrace_badattr(&pap->dtpa_mod) || 5812 dtrace_badattr(&pap->dtpa_func) || 5813 dtrace_badattr(&pap->dtpa_name) || 5814 dtrace_badattr(&pap->dtpa_args)) { 5815 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5816 "provider attributes", name); 5817 return (EINVAL); 5818 } 5819 5820 if (priv & ~DTRACE_PRIV_ALL) { 5821 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5822 "privilege attributes", name); 5823 return (EINVAL); 5824 } 5825 5826 if ((priv & DTRACE_PRIV_KERNEL) && 5827 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 5828 pops->dtps_usermode == NULL) { 5829 cmn_err(CE_WARN, "failed to register provider '%s': need " 5830 "dtps_usermode() op for given privilege attributes", name); 5831 return (EINVAL); 5832 } 5833 5834 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 5835 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 5836 (void) strcpy(provider->dtpv_name, name); 5837 5838 provider->dtpv_attr = *pap; 5839 provider->dtpv_priv.dtpp_flags = priv; 5840 provider->dtpv_priv.dtpp_uid = uid; 5841 provider->dtpv_pops = *pops; 5842 5843 if (pops->dtps_provide == NULL) { 5844 ASSERT(pops->dtps_provide_module != NULL); 5845 provider->dtpv_pops.dtps_provide = 5846 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 5847 } 5848 5849 if (pops->dtps_provide_module == NULL) { 5850 ASSERT(pops->dtps_provide != NULL); 5851 provider->dtpv_pops.dtps_provide_module = 5852 (void (*)(void *, struct modctl *))dtrace_nullop; 5853 } 5854 5855 if (pops->dtps_suspend == NULL) { 5856 ASSERT(pops->dtps_resume == NULL); 5857 provider->dtpv_pops.dtps_suspend = 5858 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 5859 provider->dtpv_pops.dtps_resume = 5860 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 5861 } 5862 5863 provider->dtpv_arg = arg; 5864 *idp = (dtrace_provider_id_t)provider; 5865 5866 if (pops == &dtrace_provider_ops) { 5867 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 5868 ASSERT(MUTEX_HELD(&dtrace_lock)); 5869 ASSERT(dtrace_anon.dta_enabling == NULL); 5870 5871 /* 5872 * We make sure that the DTrace provider is at the head of 5873 * the provider chain. 5874 */ 5875 provider->dtpv_next = dtrace_provider; 5876 dtrace_provider = provider; 5877 return (0); 5878 } 5879 5880 mutex_enter(&dtrace_provider_lock); 5881 mutex_enter(&dtrace_lock); 5882 5883 /* 5884 * If there is at least one provider registered, we'll add this 5885 * provider after the first provider. 5886 */ 5887 if (dtrace_provider != NULL) { 5888 provider->dtpv_next = dtrace_provider->dtpv_next; 5889 dtrace_provider->dtpv_next = provider; 5890 } else { 5891 dtrace_provider = provider; 5892 } 5893 5894 if (dtrace_retained != NULL) { 5895 dtrace_enabling_provide(provider); 5896 5897 /* 5898 * Now we need to call dtrace_enabling_matchall() -- which 5899 * will acquire cpu_lock and dtrace_lock. We therefore need 5900 * to drop all of our locks before calling into it... 5901 */ 5902 mutex_exit(&dtrace_lock); 5903 mutex_exit(&dtrace_provider_lock); 5904 dtrace_enabling_matchall(); 5905 5906 return (0); 5907 } 5908 5909 mutex_exit(&dtrace_lock); 5910 mutex_exit(&dtrace_provider_lock); 5911 5912 return (0); 5913 } 5914 5915 /* 5916 * Unregister the specified provider from the DTrace framework. This should 5917 * generally be called by DTrace providers in their detach(9E) entry point. 5918 */ 5919 int 5920 dtrace_unregister(dtrace_provider_id_t id) 5921 { 5922 dtrace_provider_t *old = (dtrace_provider_t *)id; 5923 dtrace_provider_t *prev = NULL; 5924 int i, self = 0; 5925 dtrace_probe_t *probe, *first = NULL; 5926 5927 if (old->dtpv_pops.dtps_enable == 5928 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 5929 /* 5930 * If DTrace itself is the provider, we're called with locks 5931 * already held. 5932 */ 5933 ASSERT(old == dtrace_provider); 5934 ASSERT(dtrace_devi != NULL); 5935 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 5936 ASSERT(MUTEX_HELD(&dtrace_lock)); 5937 self = 1; 5938 5939 if (dtrace_provider->dtpv_next != NULL) { 5940 /* 5941 * There's another provider here; return failure. 5942 */ 5943 return (EBUSY); 5944 } 5945 } else { 5946 mutex_enter(&dtrace_provider_lock); 5947 mutex_enter(&mod_lock); 5948 mutex_enter(&dtrace_lock); 5949 } 5950 5951 /* 5952 * If anyone has /dev/dtrace open, or if there are anonymous enabled 5953 * probes, we refuse to let providers slither away, unless this 5954 * provider has already been explicitly invalidated. 5955 */ 5956 if (!old->dtpv_defunct && 5957 (dtrace_opens || (dtrace_anon.dta_state != NULL && 5958 dtrace_anon.dta_state->dts_necbs > 0))) { 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 * Attempt to destroy the probes associated with this provider. 5969 */ 5970 for (i = 0; i < dtrace_nprobes; i++) { 5971 if ((probe = dtrace_probes[i]) == NULL) 5972 continue; 5973 5974 if (probe->dtpr_provider != old) 5975 continue; 5976 5977 if (probe->dtpr_ecb == NULL) 5978 continue; 5979 5980 /* 5981 * We have at least one ECB; we can't remove this provider. 5982 */ 5983 if (!self) { 5984 mutex_exit(&dtrace_lock); 5985 mutex_exit(&mod_lock); 5986 mutex_exit(&dtrace_provider_lock); 5987 } 5988 return (EBUSY); 5989 } 5990 5991 /* 5992 * All of the probes for this provider are disabled; we can safely 5993 * remove all of them from their hash chains and from the probe array. 5994 */ 5995 for (i = 0; i < dtrace_nprobes; i++) { 5996 if ((probe = dtrace_probes[i]) == NULL) 5997 continue; 5998 5999 if (probe->dtpr_provider != old) 6000 continue; 6001 6002 dtrace_probes[i] = NULL; 6003 6004 dtrace_hash_remove(dtrace_bymod, probe); 6005 dtrace_hash_remove(dtrace_byfunc, probe); 6006 dtrace_hash_remove(dtrace_byname, probe); 6007 6008 if (first == NULL) { 6009 first = probe; 6010 probe->dtpr_nextmod = NULL; 6011 } else { 6012 probe->dtpr_nextmod = first; 6013 first = probe; 6014 } 6015 } 6016 6017 /* 6018 * The provider's probes have been removed from the hash chains and 6019 * from the probe array. Now issue a dtrace_sync() to be sure that 6020 * everyone has cleared out from any probe array processing. 6021 */ 6022 dtrace_sync(); 6023 6024 for (probe = first; probe != NULL; probe = first) { 6025 first = probe->dtpr_nextmod; 6026 6027 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 6028 probe->dtpr_arg); 6029 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 6030 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 6031 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 6032 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 6033 kmem_free(probe, sizeof (dtrace_probe_t)); 6034 } 6035 6036 if ((prev = dtrace_provider) == old) { 6037 ASSERT(self || dtrace_devi == NULL); 6038 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 6039 dtrace_provider = old->dtpv_next; 6040 } else { 6041 while (prev != NULL && prev->dtpv_next != old) 6042 prev = prev->dtpv_next; 6043 6044 if (prev == NULL) { 6045 panic("attempt to unregister non-existent " 6046 "dtrace provider %p\n", (void *)id); 6047 } 6048 6049 prev->dtpv_next = old->dtpv_next; 6050 } 6051 6052 if (!self) { 6053 mutex_exit(&dtrace_lock); 6054 mutex_exit(&mod_lock); 6055 mutex_exit(&dtrace_provider_lock); 6056 } 6057 6058 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 6059 kmem_free(old, sizeof (dtrace_provider_t)); 6060 6061 return (0); 6062 } 6063 6064 /* 6065 * Invalidate the specified provider. All subsequent probe lookups for the 6066 * specified provider will fail, but its probes will not be removed. 6067 */ 6068 void 6069 dtrace_invalidate(dtrace_provider_id_t id) 6070 { 6071 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 6072 6073 ASSERT(pvp->dtpv_pops.dtps_enable != 6074 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 6075 6076 mutex_enter(&dtrace_provider_lock); 6077 mutex_enter(&dtrace_lock); 6078 6079 pvp->dtpv_defunct = 1; 6080 6081 mutex_exit(&dtrace_lock); 6082 mutex_exit(&dtrace_provider_lock); 6083 } 6084 6085 /* 6086 * Indicate whether or not DTrace has attached. 6087 */ 6088 int 6089 dtrace_attached(void) 6090 { 6091 /* 6092 * dtrace_provider will be non-NULL iff the DTrace driver has 6093 * attached. (It's non-NULL because DTrace is always itself a 6094 * provider.) 6095 */ 6096 return (dtrace_provider != NULL); 6097 } 6098 6099 /* 6100 * Remove all the unenabled probes for the given provider. This function is 6101 * not unlike dtrace_unregister(), except that it doesn't remove the provider 6102 * -- just as many of its associated probes as it can. 6103 */ 6104 int 6105 dtrace_condense(dtrace_provider_id_t id) 6106 { 6107 dtrace_provider_t *prov = (dtrace_provider_t *)id; 6108 int i; 6109 dtrace_probe_t *probe; 6110 6111 /* 6112 * Make sure this isn't the dtrace provider itself. 6113 */ 6114 ASSERT(prov->dtpv_pops.dtps_enable != 6115 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 6116 6117 mutex_enter(&dtrace_provider_lock); 6118 mutex_enter(&dtrace_lock); 6119 6120 /* 6121 * Attempt to destroy the probes associated with this provider. 6122 */ 6123 for (i = 0; i < dtrace_nprobes; i++) { 6124 if ((probe = dtrace_probes[i]) == NULL) 6125 continue; 6126 6127 if (probe->dtpr_provider != prov) 6128 continue; 6129 6130 if (probe->dtpr_ecb != NULL) 6131 continue; 6132 6133 dtrace_probes[i] = NULL; 6134 6135 dtrace_hash_remove(dtrace_bymod, probe); 6136 dtrace_hash_remove(dtrace_byfunc, probe); 6137 dtrace_hash_remove(dtrace_byname, probe); 6138 6139 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 6140 probe->dtpr_arg); 6141 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 6142 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 6143 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 6144 kmem_free(probe, sizeof (dtrace_probe_t)); 6145 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 6146 } 6147 6148 mutex_exit(&dtrace_lock); 6149 mutex_exit(&dtrace_provider_lock); 6150 6151 return (0); 6152 } 6153 6154 /* 6155 * DTrace Probe Management Functions 6156 * 6157 * The functions in this section perform the DTrace probe management, 6158 * including functions to create probes, look-up probes, and call into the 6159 * providers to request that probes be provided. Some of these functions are 6160 * in the Provider-to-Framework API; these functions can be identified by the 6161 * fact that they are not declared "static". 6162 */ 6163 6164 /* 6165 * Create a probe with the specified module name, function name, and name. 6166 */ 6167 dtrace_id_t 6168 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 6169 const char *func, const char *name, int aframes, void *arg) 6170 { 6171 dtrace_probe_t *probe, **probes; 6172 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 6173 dtrace_id_t id; 6174 6175 if (provider == dtrace_provider) { 6176 ASSERT(MUTEX_HELD(&dtrace_lock)); 6177 } else { 6178 mutex_enter(&dtrace_lock); 6179 } 6180 6181 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 6182 VM_BESTFIT | VM_SLEEP); 6183 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 6184 6185 probe->dtpr_id = id; 6186 probe->dtpr_gen = dtrace_probegen++; 6187 probe->dtpr_mod = dtrace_strdup(mod); 6188 probe->dtpr_func = dtrace_strdup(func); 6189 probe->dtpr_name = dtrace_strdup(name); 6190 probe->dtpr_arg = arg; 6191 probe->dtpr_aframes = aframes; 6192 probe->dtpr_provider = provider; 6193 6194 dtrace_hash_add(dtrace_bymod, probe); 6195 dtrace_hash_add(dtrace_byfunc, probe); 6196 dtrace_hash_add(dtrace_byname, probe); 6197 6198 if (id - 1 >= dtrace_nprobes) { 6199 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 6200 size_t nsize = osize << 1; 6201 6202 if (nsize == 0) { 6203 ASSERT(osize == 0); 6204 ASSERT(dtrace_probes == NULL); 6205 nsize = sizeof (dtrace_probe_t *); 6206 } 6207 6208 probes = kmem_zalloc(nsize, KM_SLEEP); 6209 6210 if (dtrace_probes == NULL) { 6211 ASSERT(osize == 0); 6212 dtrace_probes = probes; 6213 dtrace_nprobes = 1; 6214 } else { 6215 dtrace_probe_t **oprobes = dtrace_probes; 6216 6217 bcopy(oprobes, probes, osize); 6218 dtrace_membar_producer(); 6219 dtrace_probes = probes; 6220 6221 dtrace_sync(); 6222 6223 /* 6224 * All CPUs are now seeing the new probes array; we can 6225 * safely free the old array. 6226 */ 6227 kmem_free(oprobes, osize); 6228 dtrace_nprobes <<= 1; 6229 } 6230 6231 ASSERT(id - 1 < dtrace_nprobes); 6232 } 6233 6234 ASSERT(dtrace_probes[id - 1] == NULL); 6235 dtrace_probes[id - 1] = probe; 6236 6237 if (provider != dtrace_provider) 6238 mutex_exit(&dtrace_lock); 6239 6240 return (id); 6241 } 6242 6243 static dtrace_probe_t * 6244 dtrace_probe_lookup_id(dtrace_id_t id) 6245 { 6246 ASSERT(MUTEX_HELD(&dtrace_lock)); 6247 6248 if (id == 0 || id > dtrace_nprobes) 6249 return (NULL); 6250 6251 return (dtrace_probes[id - 1]); 6252 } 6253 6254 static int 6255 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 6256 { 6257 *((dtrace_id_t *)arg) = probe->dtpr_id; 6258 6259 return (DTRACE_MATCH_DONE); 6260 } 6261 6262 /* 6263 * Look up a probe based on provider and one or more of module name, function 6264 * name and probe name. 6265 */ 6266 dtrace_id_t 6267 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, 6268 const char *func, const char *name) 6269 { 6270 dtrace_probekey_t pkey; 6271 dtrace_id_t id; 6272 int match; 6273 6274 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 6275 pkey.dtpk_pmatch = &dtrace_match_string; 6276 pkey.dtpk_mod = mod; 6277 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 6278 pkey.dtpk_func = func; 6279 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 6280 pkey.dtpk_name = name; 6281 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 6282 pkey.dtpk_id = DTRACE_IDNONE; 6283 6284 mutex_enter(&dtrace_lock); 6285 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 6286 dtrace_probe_lookup_match, &id); 6287 mutex_exit(&dtrace_lock); 6288 6289 ASSERT(match == 1 || match == 0); 6290 return (match ? id : 0); 6291 } 6292 6293 /* 6294 * Returns the probe argument associated with the specified probe. 6295 */ 6296 void * 6297 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 6298 { 6299 dtrace_probe_t *probe; 6300 void *rval = NULL; 6301 6302 mutex_enter(&dtrace_lock); 6303 6304 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 6305 probe->dtpr_provider == (dtrace_provider_t *)id) 6306 rval = probe->dtpr_arg; 6307 6308 mutex_exit(&dtrace_lock); 6309 6310 return (rval); 6311 } 6312 6313 /* 6314 * Copy a probe into a probe description. 6315 */ 6316 static void 6317 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 6318 { 6319 bzero(pdp, sizeof (dtrace_probedesc_t)); 6320 pdp->dtpd_id = prp->dtpr_id; 6321 6322 (void) strncpy(pdp->dtpd_provider, 6323 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 6324 6325 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 6326 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 6327 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 6328 } 6329 6330 /* 6331 * Called to indicate that a probe -- or probes -- should be provided by a 6332 * specfied provider. If the specified description is NULL, the provider will 6333 * be told to provide all of its probes. (This is done whenever a new 6334 * consumer comes along, or whenever a retained enabling is to be matched.) If 6335 * the specified description is non-NULL, the provider is given the 6336 * opportunity to dynamically provide the specified probe, allowing providers 6337 * to support the creation of probes on-the-fly. (So-called _autocreated_ 6338 * probes.) If the provider is NULL, the operations will be applied to all 6339 * providers; if the provider is non-NULL the operations will only be applied 6340 * to the specified provider. The dtrace_provider_lock must be held, and the 6341 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 6342 * will need to grab the dtrace_lock when it reenters the framework through 6343 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 6344 */ 6345 static void 6346 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 6347 { 6348 struct modctl *ctl; 6349 int all = 0; 6350 6351 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6352 6353 if (prv == NULL) { 6354 all = 1; 6355 prv = dtrace_provider; 6356 } 6357 6358 do { 6359 /* 6360 * First, call the blanket provide operation. 6361 */ 6362 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 6363 6364 /* 6365 * Now call the per-module provide operation. We will grab 6366 * mod_lock to prevent the list from being modified. Note 6367 * that this also prevents the mod_busy bits from changing. 6368 * (mod_busy can only be changed with mod_lock held.) 6369 */ 6370 mutex_enter(&mod_lock); 6371 6372 ctl = &modules; 6373 do { 6374 if (ctl->mod_busy || ctl->mod_mp == NULL) 6375 continue; 6376 6377 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 6378 6379 } while ((ctl = ctl->mod_next) != &modules); 6380 6381 mutex_exit(&mod_lock); 6382 } while (all && (prv = prv->dtpv_next) != NULL); 6383 } 6384 6385 /* 6386 * Iterate over each probe, and call the Framework-to-Provider API function 6387 * denoted by offs. 6388 */ 6389 static void 6390 dtrace_probe_foreach(uintptr_t offs) 6391 { 6392 dtrace_provider_t *prov; 6393 void (*func)(void *, dtrace_id_t, void *); 6394 dtrace_probe_t *probe; 6395 dtrace_icookie_t cookie; 6396 int i; 6397 6398 /* 6399 * We disable interrupts to walk through the probe array. This is 6400 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 6401 * won't see stale data. 6402 */ 6403 cookie = dtrace_interrupt_disable(); 6404 6405 for (i = 0; i < dtrace_nprobes; i++) { 6406 if ((probe = dtrace_probes[i]) == NULL) 6407 continue; 6408 6409 if (probe->dtpr_ecb == NULL) { 6410 /* 6411 * This probe isn't enabled -- don't call the function. 6412 */ 6413 continue; 6414 } 6415 6416 prov = probe->dtpr_provider; 6417 func = *((void(**)(void *, dtrace_id_t, void *)) 6418 ((uintptr_t)&prov->dtpv_pops + offs)); 6419 6420 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 6421 } 6422 6423 dtrace_interrupt_enable(cookie); 6424 } 6425 6426 static int 6427 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 6428 { 6429 dtrace_probekey_t pkey; 6430 uint32_t priv; 6431 uid_t uid; 6432 6433 ASSERT(MUTEX_HELD(&dtrace_lock)); 6434 dtrace_ecb_create_cache = NULL; 6435 6436 if (desc == NULL) { 6437 /* 6438 * If we're passed a NULL description, we're being asked to 6439 * create an ECB with a NULL probe. 6440 */ 6441 (void) dtrace_ecb_create_enable(NULL, enab); 6442 return (0); 6443 } 6444 6445 dtrace_probekey(desc, &pkey); 6446 dtrace_cred2priv(CRED(), &priv, &uid); 6447 6448 return (dtrace_match(&pkey, priv, uid, dtrace_ecb_create_enable, enab)); 6449 } 6450 6451 /* 6452 * DTrace Helper Provider Functions 6453 */ 6454 static void 6455 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 6456 { 6457 attr->dtat_name = DOF_ATTR_NAME(dofattr); 6458 attr->dtat_data = DOF_ATTR_DATA(dofattr); 6459 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 6460 } 6461 6462 static void 6463 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 6464 const dof_provider_t *dofprov, char *strtab) 6465 { 6466 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 6467 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 6468 dofprov->dofpv_provattr); 6469 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 6470 dofprov->dofpv_modattr); 6471 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 6472 dofprov->dofpv_funcattr); 6473 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 6474 dofprov->dofpv_nameattr); 6475 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 6476 dofprov->dofpv_argsattr); 6477 } 6478 6479 static void 6480 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 6481 { 6482 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6483 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6484 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec; 6485 dof_provider_t *provider; 6486 dof_probe_t *probe; 6487 uint32_t *off; 6488 uint8_t *arg; 6489 char *strtab; 6490 uint_t i, nprobes; 6491 dtrace_helper_provdesc_t dhpv; 6492 dtrace_helper_probedesc_t dhpb; 6493 dtrace_meta_t *meta = dtrace_meta_pid; 6494 dtrace_mops_t *mops = &meta->dtm_mops; 6495 void *parg; 6496 6497 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 6498 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6499 provider->dofpv_strtab * dof->dofh_secsize); 6500 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6501 provider->dofpv_probes * dof->dofh_secsize); 6502 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6503 provider->dofpv_prargs * dof->dofh_secsize); 6504 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6505 provider->dofpv_proffs * dof->dofh_secsize); 6506 6507 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 6508 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 6509 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 6510 6511 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 6512 6513 /* 6514 * Create the provider. 6515 */ 6516 dtrace_dofprov2hprov(&dhpv, provider, strtab); 6517 6518 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 6519 return; 6520 6521 meta->dtm_count++; 6522 6523 /* 6524 * Create the probes. 6525 */ 6526 for (i = 0; i < nprobes; i++) { 6527 probe = (dof_probe_t *)(uintptr_t)(daddr + 6528 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 6529 6530 dhpb.dthpb_mod = dhp->dofhp_mod; 6531 dhpb.dthpb_func = strtab + probe->dofpr_func; 6532 dhpb.dthpb_name = strtab + probe->dofpr_name; 6533 dhpb.dthpb_base = probe->dofpr_addr; 6534 dhpb.dthpb_offs = off + probe->dofpr_offidx; 6535 dhpb.dthpb_noffs = probe->dofpr_noffs; 6536 dhpb.dthpb_args = arg + probe->dofpr_argidx; 6537 dhpb.dthpb_nargc = probe->dofpr_nargc; 6538 dhpb.dthpb_xargc = probe->dofpr_xargc; 6539 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 6540 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 6541 6542 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 6543 } 6544 } 6545 6546 static void 6547 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 6548 { 6549 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6550 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6551 int i; 6552 6553 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 6554 6555 for (i = 0; i < dof->dofh_secnum; i++) { 6556 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 6557 dof->dofh_secoff + i * dof->dofh_secsize); 6558 6559 if (sec->dofs_type != DOF_SECT_PROVIDER) 6560 continue; 6561 6562 dtrace_helper_provide_one(dhp, sec, pid); 6563 } 6564 6565 /* 6566 * We may have just created probes, so we must now rematch against 6567 * any retained enablings. Note that this call will acquire both 6568 * cpu_lock and dtrace_lock; the fact that we are holding 6569 * dtrace_meta_lock now is what defines the ordering with respect to 6570 * these three locks. 6571 */ 6572 dtrace_enabling_matchall(); 6573 } 6574 6575 static void 6576 dtrace_helper_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 6577 { 6578 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6579 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6580 dof_sec_t *str_sec; 6581 dof_provider_t *provider; 6582 char *strtab; 6583 dtrace_helper_provdesc_t dhpv; 6584 dtrace_meta_t *meta = dtrace_meta_pid; 6585 dtrace_mops_t *mops = &meta->dtm_mops; 6586 6587 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 6588 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6589 provider->dofpv_strtab * dof->dofh_secsize); 6590 6591 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 6592 6593 /* 6594 * Create the provider. 6595 */ 6596 dtrace_dofprov2hprov(&dhpv, provider, strtab); 6597 6598 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 6599 6600 meta->dtm_count--; 6601 } 6602 6603 static void 6604 dtrace_helper_remove(dof_helper_t *dhp, pid_t pid) 6605 { 6606 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6607 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6608 int i; 6609 6610 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 6611 6612 for (i = 0; i < dof->dofh_secnum; i++) { 6613 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 6614 dof->dofh_secoff + i * dof->dofh_secsize); 6615 6616 if (sec->dofs_type != DOF_SECT_PROVIDER) 6617 continue; 6618 6619 dtrace_helper_remove_one(dhp, sec, pid); 6620 } 6621 } 6622 6623 /* 6624 * DTrace Meta Provider-to-Framework API Functions 6625 * 6626 * These functions implement the Meta Provider-to-Framework API, as described 6627 * in <sys/dtrace.h>. 6628 */ 6629 int 6630 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 6631 dtrace_meta_provider_id_t *idp) 6632 { 6633 dtrace_meta_t *meta; 6634 dtrace_helpers_t *help, *next; 6635 int i; 6636 6637 *idp = DTRACE_METAPROVNONE; 6638 6639 /* 6640 * We strictly don't need the name, but we hold onto it for 6641 * debuggability. All hail error queues! 6642 */ 6643 if (name == NULL) { 6644 cmn_err(CE_WARN, "failed to register meta-provider: " 6645 "invalid name"); 6646 return (EINVAL); 6647 } 6648 6649 if (mops == NULL || 6650 mops->dtms_create_probe == NULL || 6651 mops->dtms_provide_pid == NULL || 6652 mops->dtms_remove_pid == NULL) { 6653 cmn_err(CE_WARN, "failed to register meta-register %s: " 6654 "invalid ops", name); 6655 return (EINVAL); 6656 } 6657 6658 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 6659 meta->dtm_mops = *mops; 6660 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 6661 (void) strcpy(meta->dtm_name, name); 6662 meta->dtm_arg = arg; 6663 6664 mutex_enter(&dtrace_meta_lock); 6665 mutex_enter(&dtrace_lock); 6666 6667 if (dtrace_meta_pid != NULL) { 6668 mutex_exit(&dtrace_lock); 6669 mutex_exit(&dtrace_meta_lock); 6670 cmn_err(CE_WARN, "failed to register meta-register %s: " 6671 "user-land meta-provider exists", name); 6672 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 6673 kmem_free(meta, sizeof (dtrace_meta_t)); 6674 return (EINVAL); 6675 } 6676 6677 dtrace_meta_pid = meta; 6678 *idp = (dtrace_meta_provider_id_t)meta; 6679 6680 /* 6681 * If there are providers and probes ready to go, pass them 6682 * off to the new meta provider now. 6683 */ 6684 6685 help = dtrace_deferred_pid; 6686 dtrace_deferred_pid = NULL; 6687 6688 mutex_exit(&dtrace_lock); 6689 6690 while (help != NULL) { 6691 for (i = 0; i < help->dthps_nprovs; i++) { 6692 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 6693 help->dthps_pid); 6694 } 6695 6696 next = help->dthps_next; 6697 help->dthps_next = NULL; 6698 help->dthps_prev = NULL; 6699 help->dthps_deferred = 0; 6700 help = next; 6701 } 6702 6703 mutex_exit(&dtrace_meta_lock); 6704 6705 return (0); 6706 } 6707 6708 int 6709 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 6710 { 6711 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 6712 6713 mutex_enter(&dtrace_meta_lock); 6714 mutex_enter(&dtrace_lock); 6715 6716 if (old == dtrace_meta_pid) { 6717 pp = &dtrace_meta_pid; 6718 } else { 6719 panic("attempt to unregister non-existent " 6720 "dtrace meta-provider %p\n", (void *)old); 6721 } 6722 6723 if (old->dtm_count != 0) { 6724 mutex_exit(&dtrace_lock); 6725 mutex_exit(&dtrace_meta_lock); 6726 return (EBUSY); 6727 } 6728 6729 *pp = NULL; 6730 6731 mutex_exit(&dtrace_lock); 6732 mutex_exit(&dtrace_meta_lock); 6733 6734 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 6735 kmem_free(old, sizeof (dtrace_meta_t)); 6736 6737 return (0); 6738 } 6739 6740 6741 /* 6742 * DTrace DIF Object Functions 6743 */ 6744 static int 6745 dtrace_difo_err(uint_t pc, const char *format, ...) 6746 { 6747 if (dtrace_err_verbose) { 6748 va_list alist; 6749 6750 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 6751 va_start(alist, format); 6752 (void) vuprintf(format, alist); 6753 va_end(alist); 6754 } 6755 6756 #ifdef DTRACE_ERRDEBUG 6757 dtrace_errdebug(format); 6758 #endif 6759 return (1); 6760 } 6761 6762 /* 6763 * Validate a DTrace DIF object by checking the IR instructions. The following 6764 * rules are currently enforced by dtrace_difo_validate(): 6765 * 6766 * 1. Each instruction must have a valid opcode 6767 * 2. Each register, string, variable, or subroutine reference must be valid 6768 * 3. No instruction can modify register %r0 (must be zero) 6769 * 4. All instruction reserved bits must be set to zero 6770 * 5. The last instruction must be a "ret" instruction 6771 * 6. All branch targets must reference a valid instruction _after_ the branch 6772 */ 6773 static int 6774 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 6775 cred_t *cr) 6776 { 6777 int err = 0, i; 6778 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 6779 int kcheck; 6780 uint_t pc; 6781 6782 kcheck = cr == NULL || 6783 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE) == 0; 6784 6785 dp->dtdo_destructive = 0; 6786 6787 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 6788 dif_instr_t instr = dp->dtdo_buf[pc]; 6789 6790 uint_t r1 = DIF_INSTR_R1(instr); 6791 uint_t r2 = DIF_INSTR_R2(instr); 6792 uint_t rd = DIF_INSTR_RD(instr); 6793 uint_t rs = DIF_INSTR_RS(instr); 6794 uint_t label = DIF_INSTR_LABEL(instr); 6795 uint_t v = DIF_INSTR_VAR(instr); 6796 uint_t subr = DIF_INSTR_SUBR(instr); 6797 uint_t type = DIF_INSTR_TYPE(instr); 6798 uint_t op = DIF_INSTR_OP(instr); 6799 6800 switch (op) { 6801 case DIF_OP_OR: 6802 case DIF_OP_XOR: 6803 case DIF_OP_AND: 6804 case DIF_OP_SLL: 6805 case DIF_OP_SRL: 6806 case DIF_OP_SRA: 6807 case DIF_OP_SUB: 6808 case DIF_OP_ADD: 6809 case DIF_OP_MUL: 6810 case DIF_OP_SDIV: 6811 case DIF_OP_UDIV: 6812 case DIF_OP_SREM: 6813 case DIF_OP_UREM: 6814 case DIF_OP_COPYS: 6815 if (r1 >= nregs) 6816 err += efunc(pc, "invalid register %u\n", r1); 6817 if (r2 >= nregs) 6818 err += efunc(pc, "invalid register %u\n", r2); 6819 if (rd >= nregs) 6820 err += efunc(pc, "invalid register %u\n", rd); 6821 if (rd == 0) 6822 err += efunc(pc, "cannot write to %r0\n"); 6823 break; 6824 case DIF_OP_NOT: 6825 case DIF_OP_MOV: 6826 case DIF_OP_ALLOCS: 6827 if (r1 >= nregs) 6828 err += efunc(pc, "invalid register %u\n", r1); 6829 if (r2 != 0) 6830 err += efunc(pc, "non-zero reserved bits\n"); 6831 if (rd >= nregs) 6832 err += efunc(pc, "invalid register %u\n", rd); 6833 if (rd == 0) 6834 err += efunc(pc, "cannot write to %r0\n"); 6835 break; 6836 case DIF_OP_LDSB: 6837 case DIF_OP_LDSH: 6838 case DIF_OP_LDSW: 6839 case DIF_OP_LDUB: 6840 case DIF_OP_LDUH: 6841 case DIF_OP_LDUW: 6842 case DIF_OP_LDX: 6843 if (r1 >= nregs) 6844 err += efunc(pc, "invalid register %u\n", r1); 6845 if (r2 != 0) 6846 err += efunc(pc, "non-zero reserved bits\n"); 6847 if (rd >= nregs) 6848 err += efunc(pc, "invalid register %u\n", rd); 6849 if (rd == 0) 6850 err += efunc(pc, "cannot write to %r0\n"); 6851 if (kcheck) 6852 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 6853 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 6854 break; 6855 case DIF_OP_RLDSB: 6856 case DIF_OP_RLDSH: 6857 case DIF_OP_RLDSW: 6858 case DIF_OP_RLDUB: 6859 case DIF_OP_RLDUH: 6860 case DIF_OP_RLDUW: 6861 case DIF_OP_RLDX: 6862 if (r1 >= nregs) 6863 err += efunc(pc, "invalid register %u\n", r1); 6864 if (r2 != 0) 6865 err += efunc(pc, "non-zero reserved bits\n"); 6866 if (rd >= nregs) 6867 err += efunc(pc, "invalid register %u\n", rd); 6868 if (rd == 0) 6869 err += efunc(pc, "cannot write to %r0\n"); 6870 break; 6871 case DIF_OP_ULDSB: 6872 case DIF_OP_ULDSH: 6873 case DIF_OP_ULDSW: 6874 case DIF_OP_ULDUB: 6875 case DIF_OP_ULDUH: 6876 case DIF_OP_ULDUW: 6877 case DIF_OP_ULDX: 6878 if (r1 >= nregs) 6879 err += efunc(pc, "invalid register %u\n", r1); 6880 if (r2 != 0) 6881 err += efunc(pc, "non-zero reserved bits\n"); 6882 if (rd >= nregs) 6883 err += efunc(pc, "invalid register %u\n", rd); 6884 if (rd == 0) 6885 err += efunc(pc, "cannot write to %r0\n"); 6886 break; 6887 case DIF_OP_STB: 6888 case DIF_OP_STH: 6889 case DIF_OP_STW: 6890 case DIF_OP_STX: 6891 if (r1 >= nregs) 6892 err += efunc(pc, "invalid register %u\n", r1); 6893 if (r2 != 0) 6894 err += efunc(pc, "non-zero reserved bits\n"); 6895 if (rd >= nregs) 6896 err += efunc(pc, "invalid register %u\n", rd); 6897 if (rd == 0) 6898 err += efunc(pc, "cannot write to 0 address\n"); 6899 break; 6900 case DIF_OP_CMP: 6901 case DIF_OP_SCMP: 6902 if (r1 >= nregs) 6903 err += efunc(pc, "invalid register %u\n", r1); 6904 if (r2 >= nregs) 6905 err += efunc(pc, "invalid register %u\n", r2); 6906 if (rd != 0) 6907 err += efunc(pc, "non-zero reserved bits\n"); 6908 break; 6909 case DIF_OP_TST: 6910 if (r1 >= nregs) 6911 err += efunc(pc, "invalid register %u\n", r1); 6912 if (r2 != 0 || rd != 0) 6913 err += efunc(pc, "non-zero reserved bits\n"); 6914 break; 6915 case DIF_OP_BA: 6916 case DIF_OP_BE: 6917 case DIF_OP_BNE: 6918 case DIF_OP_BG: 6919 case DIF_OP_BGU: 6920 case DIF_OP_BGE: 6921 case DIF_OP_BGEU: 6922 case DIF_OP_BL: 6923 case DIF_OP_BLU: 6924 case DIF_OP_BLE: 6925 case DIF_OP_BLEU: 6926 if (label >= dp->dtdo_len) { 6927 err += efunc(pc, "invalid branch target %u\n", 6928 label); 6929 } 6930 if (label <= pc) { 6931 err += efunc(pc, "backward branch to %u\n", 6932 label); 6933 } 6934 break; 6935 case DIF_OP_RET: 6936 if (r1 != 0 || r2 != 0) 6937 err += efunc(pc, "non-zero reserved bits\n"); 6938 if (rd >= nregs) 6939 err += efunc(pc, "invalid register %u\n", rd); 6940 break; 6941 case DIF_OP_NOP: 6942 case DIF_OP_POPTS: 6943 case DIF_OP_FLUSHTS: 6944 if (r1 != 0 || r2 != 0 || rd != 0) 6945 err += efunc(pc, "non-zero reserved bits\n"); 6946 break; 6947 case DIF_OP_SETX: 6948 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 6949 err += efunc(pc, "invalid integer ref %u\n", 6950 DIF_INSTR_INTEGER(instr)); 6951 } 6952 if (rd >= nregs) 6953 err += efunc(pc, "invalid register %u\n", rd); 6954 if (rd == 0) 6955 err += efunc(pc, "cannot write to %r0\n"); 6956 break; 6957 case DIF_OP_SETS: 6958 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 6959 err += efunc(pc, "invalid string ref %u\n", 6960 DIF_INSTR_STRING(instr)); 6961 } 6962 if (rd >= nregs) 6963 err += efunc(pc, "invalid register %u\n", rd); 6964 if (rd == 0) 6965 err += efunc(pc, "cannot write to %r0\n"); 6966 break; 6967 case DIF_OP_LDGA: 6968 case DIF_OP_LDTA: 6969 if (r1 > DIF_VAR_ARRAY_MAX) 6970 err += efunc(pc, "invalid array %u\n", r1); 6971 if (r2 >= nregs) 6972 err += efunc(pc, "invalid register %u\n", r2); 6973 if (rd >= nregs) 6974 err += efunc(pc, "invalid register %u\n", rd); 6975 if (rd == 0) 6976 err += efunc(pc, "cannot write to %r0\n"); 6977 break; 6978 case DIF_OP_LDGS: 6979 case DIF_OP_LDTS: 6980 case DIF_OP_LDLS: 6981 case DIF_OP_LDGAA: 6982 case DIF_OP_LDTAA: 6983 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 6984 err += efunc(pc, "invalid variable %u\n", v); 6985 if (rd >= nregs) 6986 err += efunc(pc, "invalid register %u\n", rd); 6987 if (rd == 0) 6988 err += efunc(pc, "cannot write to %r0\n"); 6989 break; 6990 case DIF_OP_STGS: 6991 case DIF_OP_STTS: 6992 case DIF_OP_STLS: 6993 case DIF_OP_STGAA: 6994 case DIF_OP_STTAA: 6995 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 6996 err += efunc(pc, "invalid variable %u\n", v); 6997 if (rs >= nregs) 6998 err += efunc(pc, "invalid register %u\n", rd); 6999 break; 7000 case DIF_OP_CALL: 7001 if (subr > DIF_SUBR_MAX) 7002 err += efunc(pc, "invalid subr %u\n", subr); 7003 if (rd >= nregs) 7004 err += efunc(pc, "invalid register %u\n", rd); 7005 if (rd == 0) 7006 err += efunc(pc, "cannot write to %r0\n"); 7007 7008 if (subr == DIF_SUBR_COPYOUT || 7009 subr == DIF_SUBR_COPYOUTSTR) { 7010 dp->dtdo_destructive = 1; 7011 } 7012 break; 7013 case DIF_OP_PUSHTR: 7014 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 7015 err += efunc(pc, "invalid ref type %u\n", type); 7016 if (r2 >= nregs) 7017 err += efunc(pc, "invalid register %u\n", r2); 7018 if (rs >= nregs) 7019 err += efunc(pc, "invalid register %u\n", rs); 7020 break; 7021 case DIF_OP_PUSHTV: 7022 if (type != DIF_TYPE_CTF) 7023 err += efunc(pc, "invalid val type %u\n", type); 7024 if (r2 >= nregs) 7025 err += efunc(pc, "invalid register %u\n", r2); 7026 if (rs >= nregs) 7027 err += efunc(pc, "invalid register %u\n", rs); 7028 break; 7029 default: 7030 err += efunc(pc, "invalid opcode %u\n", 7031 DIF_INSTR_OP(instr)); 7032 } 7033 } 7034 7035 if (dp->dtdo_len != 0 && 7036 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 7037 err += efunc(dp->dtdo_len - 1, 7038 "expected 'ret' as last DIF instruction\n"); 7039 } 7040 7041 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 7042 /* 7043 * If we're not returning by reference, the size must be either 7044 * 0 or the size of one of the base types. 7045 */ 7046 switch (dp->dtdo_rtype.dtdt_size) { 7047 case 0: 7048 case sizeof (uint8_t): 7049 case sizeof (uint16_t): 7050 case sizeof (uint32_t): 7051 case sizeof (uint64_t): 7052 break; 7053 7054 default: 7055 err += efunc(dp->dtdo_len - 1, "bad return size"); 7056 } 7057 } 7058 7059 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 7060 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 7061 dtrace_diftype_t *vt, *et; 7062 uint_t id, ndx; 7063 7064 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 7065 v->dtdv_scope != DIFV_SCOPE_THREAD && 7066 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 7067 err += efunc(i, "unrecognized variable scope %d\n", 7068 v->dtdv_scope); 7069 break; 7070 } 7071 7072 if (v->dtdv_kind != DIFV_KIND_ARRAY && 7073 v->dtdv_kind != DIFV_KIND_SCALAR) { 7074 err += efunc(i, "unrecognized variable type %d\n", 7075 v->dtdv_kind); 7076 break; 7077 } 7078 7079 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 7080 err += efunc(i, "%d exceeds variable id limit\n", id); 7081 break; 7082 } 7083 7084 if (id < DIF_VAR_OTHER_UBASE) 7085 continue; 7086 7087 /* 7088 * For user-defined variables, we need to check that this 7089 * definition is identical to any previous definition that we 7090 * encountered. 7091 */ 7092 ndx = id - DIF_VAR_OTHER_UBASE; 7093 7094 switch (v->dtdv_scope) { 7095 case DIFV_SCOPE_GLOBAL: 7096 if (ndx < vstate->dtvs_nglobals) { 7097 dtrace_statvar_t *svar; 7098 7099 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 7100 existing = &svar->dtsv_var; 7101 } 7102 7103 break; 7104 7105 case DIFV_SCOPE_THREAD: 7106 if (ndx < vstate->dtvs_ntlocals) 7107 existing = &vstate->dtvs_tlocals[ndx]; 7108 break; 7109 7110 case DIFV_SCOPE_LOCAL: 7111 if (ndx < vstate->dtvs_nlocals) { 7112 dtrace_statvar_t *svar; 7113 7114 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 7115 existing = &svar->dtsv_var; 7116 } 7117 7118 break; 7119 } 7120 7121 vt = &v->dtdv_type; 7122 7123 if (vt->dtdt_flags & DIF_TF_BYREF) { 7124 if (vt->dtdt_size == 0) { 7125 err += efunc(i, "zero-sized variable\n"); 7126 break; 7127 } 7128 7129 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 7130 vt->dtdt_size > dtrace_global_maxsize) { 7131 err += efunc(i, "oversized by-ref global\n"); 7132 break; 7133 } 7134 } 7135 7136 if (existing == NULL || existing->dtdv_id == 0) 7137 continue; 7138 7139 ASSERT(existing->dtdv_id == v->dtdv_id); 7140 ASSERT(existing->dtdv_scope == v->dtdv_scope); 7141 7142 if (existing->dtdv_kind != v->dtdv_kind) 7143 err += efunc(i, "%d changed variable kind\n", id); 7144 7145 et = &existing->dtdv_type; 7146 7147 if (vt->dtdt_flags != et->dtdt_flags) { 7148 err += efunc(i, "%d changed variable type flags\n", id); 7149 break; 7150 } 7151 7152 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 7153 err += efunc(i, "%d changed variable type size\n", id); 7154 break; 7155 } 7156 } 7157 7158 return (err); 7159 } 7160 7161 /* 7162 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 7163 * are much more constrained than normal DIFOs. Specifically, they may 7164 * not: 7165 * 7166 * 1. Make calls to subroutines other than copyin(), copyinstr() or 7167 * miscellaneous string routines 7168 * 2. Access DTrace variables other than the args[] array, and the 7169 * curthread, pid, tid and execname variables. 7170 * 3. Have thread-local variables. 7171 * 4. Have dynamic variables. 7172 */ 7173 static int 7174 dtrace_difo_validate_helper(dtrace_difo_t *dp) 7175 { 7176 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 7177 int err = 0; 7178 uint_t pc; 7179 7180 for (pc = 0; pc < dp->dtdo_len; pc++) { 7181 dif_instr_t instr = dp->dtdo_buf[pc]; 7182 7183 uint_t v = DIF_INSTR_VAR(instr); 7184 uint_t subr = DIF_INSTR_SUBR(instr); 7185 uint_t op = DIF_INSTR_OP(instr); 7186 7187 switch (op) { 7188 case DIF_OP_OR: 7189 case DIF_OP_XOR: 7190 case DIF_OP_AND: 7191 case DIF_OP_SLL: 7192 case DIF_OP_SRL: 7193 case DIF_OP_SRA: 7194 case DIF_OP_SUB: 7195 case DIF_OP_ADD: 7196 case DIF_OP_MUL: 7197 case DIF_OP_SDIV: 7198 case DIF_OP_UDIV: 7199 case DIF_OP_SREM: 7200 case DIF_OP_UREM: 7201 case DIF_OP_COPYS: 7202 case DIF_OP_NOT: 7203 case DIF_OP_MOV: 7204 case DIF_OP_RLDSB: 7205 case DIF_OP_RLDSH: 7206 case DIF_OP_RLDSW: 7207 case DIF_OP_RLDUB: 7208 case DIF_OP_RLDUH: 7209 case DIF_OP_RLDUW: 7210 case DIF_OP_RLDX: 7211 case DIF_OP_ULDSB: 7212 case DIF_OP_ULDSH: 7213 case DIF_OP_ULDSW: 7214 case DIF_OP_ULDUB: 7215 case DIF_OP_ULDUH: 7216 case DIF_OP_ULDUW: 7217 case DIF_OP_ULDX: 7218 case DIF_OP_STB: 7219 case DIF_OP_STH: 7220 case DIF_OP_STW: 7221 case DIF_OP_STX: 7222 case DIF_OP_ALLOCS: 7223 case DIF_OP_CMP: 7224 case DIF_OP_SCMP: 7225 case DIF_OP_TST: 7226 case DIF_OP_BA: 7227 case DIF_OP_BE: 7228 case DIF_OP_BNE: 7229 case DIF_OP_BG: 7230 case DIF_OP_BGU: 7231 case DIF_OP_BGE: 7232 case DIF_OP_BGEU: 7233 case DIF_OP_BL: 7234 case DIF_OP_BLU: 7235 case DIF_OP_BLE: 7236 case DIF_OP_BLEU: 7237 case DIF_OP_RET: 7238 case DIF_OP_NOP: 7239 case DIF_OP_POPTS: 7240 case DIF_OP_FLUSHTS: 7241 case DIF_OP_SETX: 7242 case DIF_OP_SETS: 7243 case DIF_OP_LDGA: 7244 case DIF_OP_LDLS: 7245 case DIF_OP_STGS: 7246 case DIF_OP_STLS: 7247 case DIF_OP_PUSHTR: 7248 case DIF_OP_PUSHTV: 7249 break; 7250 7251 case DIF_OP_LDGS: 7252 if (v >= DIF_VAR_OTHER_UBASE) 7253 break; 7254 7255 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 7256 break; 7257 7258 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 7259 v == DIF_VAR_TID || v == DIF_VAR_EXECNAME || 7260 v == DIF_VAR_ZONENAME) 7261 break; 7262 7263 err += efunc(pc, "illegal variable %u\n", v); 7264 break; 7265 7266 case DIF_OP_LDTA: 7267 case DIF_OP_LDTS: 7268 case DIF_OP_LDGAA: 7269 case DIF_OP_LDTAA: 7270 err += efunc(pc, "illegal dynamic variable load\n"); 7271 break; 7272 7273 case DIF_OP_STTS: 7274 case DIF_OP_STGAA: 7275 case DIF_OP_STTAA: 7276 err += efunc(pc, "illegal dynamic variable store\n"); 7277 break; 7278 7279 case DIF_OP_CALL: 7280 if (subr == DIF_SUBR_ALLOCA || 7281 subr == DIF_SUBR_BCOPY || 7282 subr == DIF_SUBR_COPYIN || 7283 subr == DIF_SUBR_COPYINTO || 7284 subr == DIF_SUBR_COPYINSTR || 7285 subr == DIF_SUBR_INDEX || 7286 subr == DIF_SUBR_LLTOSTR || 7287 subr == DIF_SUBR_RINDEX || 7288 subr == DIF_SUBR_STRCHR || 7289 subr == DIF_SUBR_STRJOIN || 7290 subr == DIF_SUBR_STRRCHR || 7291 subr == DIF_SUBR_STRSTR) 7292 break; 7293 7294 err += efunc(pc, "invalid subr %u\n", subr); 7295 break; 7296 7297 default: 7298 err += efunc(pc, "invalid opcode %u\n", 7299 DIF_INSTR_OP(instr)); 7300 } 7301 } 7302 7303 return (err); 7304 } 7305 7306 /* 7307 * Returns 1 if the expression in the DIF object can be cached on a per-thread 7308 * basis; 0 if not. 7309 */ 7310 static int 7311 dtrace_difo_cacheable(dtrace_difo_t *dp) 7312 { 7313 int i; 7314 7315 if (dp == NULL) 7316 return (0); 7317 7318 for (i = 0; i < dp->dtdo_varlen; i++) { 7319 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7320 7321 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 7322 continue; 7323 7324 switch (v->dtdv_id) { 7325 case DIF_VAR_CURTHREAD: 7326 case DIF_VAR_PID: 7327 case DIF_VAR_TID: 7328 case DIF_VAR_EXECNAME: 7329 case DIF_VAR_ZONENAME: 7330 break; 7331 7332 default: 7333 return (0); 7334 } 7335 } 7336 7337 /* 7338 * This DIF object may be cacheable. Now we need to look for any 7339 * array loading instructions, any memory loading instructions, or 7340 * any stores to thread-local variables. 7341 */ 7342 for (i = 0; i < dp->dtdo_len; i++) { 7343 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 7344 7345 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 7346 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 7347 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 7348 op == DIF_OP_LDGA || op == DIF_OP_STTS) 7349 return (0); 7350 } 7351 7352 return (1); 7353 } 7354 7355 static void 7356 dtrace_difo_hold(dtrace_difo_t *dp) 7357 { 7358 int i; 7359 7360 ASSERT(MUTEX_HELD(&dtrace_lock)); 7361 7362 dp->dtdo_refcnt++; 7363 ASSERT(dp->dtdo_refcnt != 0); 7364 7365 /* 7366 * We need to check this DIF object for references to the variable 7367 * DIF_VAR_VTIMESTAMP. 7368 */ 7369 for (i = 0; i < dp->dtdo_varlen; i++) { 7370 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7371 7372 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 7373 continue; 7374 7375 if (dtrace_vtime_references++ == 0) 7376 dtrace_vtime_enable(); 7377 } 7378 } 7379 7380 /* 7381 * This routine calculates the dynamic variable chunksize for a given DIF 7382 * object. The calculation is not fool-proof, and can probably be tricked by 7383 * malicious DIF -- but it works for all compiler-generated DIF. Because this 7384 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 7385 * if a dynamic variable size exceeds the chunksize. 7386 */ 7387 static void 7388 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7389 { 7390 uint64_t sval; 7391 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 7392 const dif_instr_t *text = dp->dtdo_buf; 7393 uint_t pc, srd = 0; 7394 uint_t ttop = 0; 7395 size_t size, ksize; 7396 uint_t id, i; 7397 7398 for (pc = 0; pc < dp->dtdo_len; pc++) { 7399 dif_instr_t instr = text[pc]; 7400 uint_t op = DIF_INSTR_OP(instr); 7401 uint_t rd = DIF_INSTR_RD(instr); 7402 uint_t r1 = DIF_INSTR_R1(instr); 7403 uint_t nkeys = 0; 7404 uchar_t scope; 7405 7406 dtrace_key_t *key = tupregs; 7407 7408 switch (op) { 7409 case DIF_OP_SETX: 7410 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 7411 srd = rd; 7412 continue; 7413 7414 case DIF_OP_STTS: 7415 key = &tupregs[DIF_DTR_NREGS]; 7416 key[0].dttk_size = 0; 7417 key[1].dttk_size = 0; 7418 nkeys = 2; 7419 scope = DIFV_SCOPE_THREAD; 7420 break; 7421 7422 case DIF_OP_STGAA: 7423 case DIF_OP_STTAA: 7424 nkeys = ttop; 7425 7426 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 7427 key[nkeys++].dttk_size = 0; 7428 7429 key[nkeys++].dttk_size = 0; 7430 7431 if (op == DIF_OP_STTAA) { 7432 scope = DIFV_SCOPE_THREAD; 7433 } else { 7434 scope = DIFV_SCOPE_GLOBAL; 7435 } 7436 7437 break; 7438 7439 case DIF_OP_PUSHTR: 7440 if (ttop == DIF_DTR_NREGS) 7441 return; 7442 7443 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 7444 /* 7445 * If the register for the size of the "pushtr" 7446 * is %r0 (or the value is 0) and the type is 7447 * a string, we'll use the system-wide default 7448 * string size. 7449 */ 7450 tupregs[ttop++].dttk_size = 7451 dtrace_strsize_default; 7452 } else { 7453 if (srd == 0) 7454 return; 7455 7456 tupregs[ttop++].dttk_size = sval; 7457 } 7458 7459 break; 7460 7461 case DIF_OP_PUSHTV: 7462 if (ttop == DIF_DTR_NREGS) 7463 return; 7464 7465 tupregs[ttop++].dttk_size = 0; 7466 break; 7467 7468 case DIF_OP_FLUSHTS: 7469 ttop = 0; 7470 break; 7471 7472 case DIF_OP_POPTS: 7473 if (ttop != 0) 7474 ttop--; 7475 break; 7476 } 7477 7478 sval = 0; 7479 srd = 0; 7480 7481 if (nkeys == 0) 7482 continue; 7483 7484 /* 7485 * We have a dynamic variable allocation; calculate its size. 7486 */ 7487 for (ksize = 0, i = 0; i < nkeys; i++) 7488 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 7489 7490 size = sizeof (dtrace_dynvar_t); 7491 size += sizeof (dtrace_key_t) * (nkeys - 1); 7492 size += ksize; 7493 7494 /* 7495 * Now we need to determine the size of the stored data. 7496 */ 7497 id = DIF_INSTR_VAR(instr); 7498 7499 for (i = 0; i < dp->dtdo_varlen; i++) { 7500 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7501 7502 if (v->dtdv_id == id && v->dtdv_scope == scope) { 7503 size += v->dtdv_type.dtdt_size; 7504 break; 7505 } 7506 } 7507 7508 if (i == dp->dtdo_varlen) 7509 return; 7510 7511 /* 7512 * We have the size. If this is larger than the chunk size 7513 * for our dynamic variable state, reset the chunk size. 7514 */ 7515 size = P2ROUNDUP(size, sizeof (uint64_t)); 7516 7517 if (size > vstate->dtvs_dynvars.dtds_chunksize) 7518 vstate->dtvs_dynvars.dtds_chunksize = size; 7519 } 7520 } 7521 7522 static void 7523 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7524 { 7525 int i, oldsvars, osz, nsz, otlocals, ntlocals; 7526 uint_t id; 7527 7528 ASSERT(MUTEX_HELD(&dtrace_lock)); 7529 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 7530 7531 for (i = 0; i < dp->dtdo_varlen; i++) { 7532 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7533 dtrace_statvar_t *svar, ***svarp; 7534 size_t dsize = 0; 7535 uint8_t scope = v->dtdv_scope; 7536 int *np; 7537 7538 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 7539 continue; 7540 7541 id -= DIF_VAR_OTHER_UBASE; 7542 7543 switch (scope) { 7544 case DIFV_SCOPE_THREAD: 7545 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 7546 dtrace_difv_t *tlocals; 7547 7548 if ((ntlocals = (otlocals << 1)) == 0) 7549 ntlocals = 1; 7550 7551 osz = otlocals * sizeof (dtrace_difv_t); 7552 nsz = ntlocals * sizeof (dtrace_difv_t); 7553 7554 tlocals = kmem_zalloc(nsz, KM_SLEEP); 7555 7556 if (osz != 0) { 7557 bcopy(vstate->dtvs_tlocals, 7558 tlocals, osz); 7559 kmem_free(vstate->dtvs_tlocals, osz); 7560 } 7561 7562 vstate->dtvs_tlocals = tlocals; 7563 vstate->dtvs_ntlocals = ntlocals; 7564 } 7565 7566 vstate->dtvs_tlocals[id] = *v; 7567 continue; 7568 7569 case DIFV_SCOPE_LOCAL: 7570 np = &vstate->dtvs_nlocals; 7571 svarp = &vstate->dtvs_locals; 7572 7573 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 7574 dsize = NCPU * (v->dtdv_type.dtdt_size + 7575 sizeof (uint64_t)); 7576 else 7577 dsize = NCPU * sizeof (uint64_t); 7578 7579 break; 7580 7581 case DIFV_SCOPE_GLOBAL: 7582 np = &vstate->dtvs_nglobals; 7583 svarp = &vstate->dtvs_globals; 7584 7585 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 7586 dsize = v->dtdv_type.dtdt_size + 7587 sizeof (uint64_t); 7588 7589 break; 7590 7591 default: 7592 ASSERT(0); 7593 } 7594 7595 while (id >= (oldsvars = *np)) { 7596 dtrace_statvar_t **statics; 7597 int newsvars, oldsize, newsize; 7598 7599 if ((newsvars = (oldsvars << 1)) == 0) 7600 newsvars = 1; 7601 7602 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 7603 newsize = newsvars * sizeof (dtrace_statvar_t *); 7604 7605 statics = kmem_zalloc(newsize, KM_SLEEP); 7606 7607 if (oldsize != 0) { 7608 bcopy(*svarp, statics, oldsize); 7609 kmem_free(*svarp, oldsize); 7610 } 7611 7612 *svarp = statics; 7613 *np = newsvars; 7614 } 7615 7616 if ((svar = (*svarp)[id]) == NULL) { 7617 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 7618 svar->dtsv_var = *v; 7619 7620 if ((svar->dtsv_size = dsize) != 0) { 7621 svar->dtsv_data = (uint64_t)(uintptr_t) 7622 kmem_zalloc(dsize, KM_SLEEP); 7623 } 7624 7625 (*svarp)[id] = svar; 7626 } 7627 7628 svar->dtsv_refcnt++; 7629 } 7630 7631 dtrace_difo_chunksize(dp, vstate); 7632 dtrace_difo_hold(dp); 7633 } 7634 7635 static dtrace_difo_t * 7636 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7637 { 7638 dtrace_difo_t *new; 7639 size_t sz; 7640 7641 ASSERT(dp->dtdo_buf != NULL); 7642 ASSERT(dp->dtdo_refcnt != 0); 7643 7644 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 7645 7646 ASSERT(dp->dtdo_buf != NULL); 7647 sz = dp->dtdo_len * sizeof (dif_instr_t); 7648 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 7649 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 7650 new->dtdo_len = dp->dtdo_len; 7651 7652 if (dp->dtdo_strtab != NULL) { 7653 ASSERT(dp->dtdo_strlen != 0); 7654 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 7655 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 7656 new->dtdo_strlen = dp->dtdo_strlen; 7657 } 7658 7659 if (dp->dtdo_inttab != NULL) { 7660 ASSERT(dp->dtdo_intlen != 0); 7661 sz = dp->dtdo_intlen * sizeof (uint64_t); 7662 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 7663 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 7664 new->dtdo_intlen = dp->dtdo_intlen; 7665 } 7666 7667 if (dp->dtdo_vartab != NULL) { 7668 ASSERT(dp->dtdo_varlen != 0); 7669 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 7670 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 7671 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 7672 new->dtdo_varlen = dp->dtdo_varlen; 7673 } 7674 7675 dtrace_difo_init(new, vstate); 7676 return (new); 7677 } 7678 7679 static void 7680 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7681 { 7682 int i; 7683 7684 ASSERT(dp->dtdo_refcnt == 0); 7685 7686 for (i = 0; i < dp->dtdo_varlen; i++) { 7687 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7688 dtrace_statvar_t *svar, **svarp; 7689 uint_t id; 7690 uint8_t scope = v->dtdv_scope; 7691 int *np; 7692 7693 switch (scope) { 7694 case DIFV_SCOPE_THREAD: 7695 continue; 7696 7697 case DIFV_SCOPE_LOCAL: 7698 np = &vstate->dtvs_nlocals; 7699 svarp = vstate->dtvs_locals; 7700 break; 7701 7702 case DIFV_SCOPE_GLOBAL: 7703 np = &vstate->dtvs_nglobals; 7704 svarp = vstate->dtvs_globals; 7705 break; 7706 7707 default: 7708 ASSERT(0); 7709 } 7710 7711 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 7712 continue; 7713 7714 id -= DIF_VAR_OTHER_UBASE; 7715 ASSERT(id < *np); 7716 7717 svar = svarp[id]; 7718 ASSERT(svar != NULL); 7719 ASSERT(svar->dtsv_refcnt > 0); 7720 7721 if (--svar->dtsv_refcnt > 0) 7722 continue; 7723 7724 if (svar->dtsv_size != 0) { 7725 ASSERT(svar->dtsv_data != NULL); 7726 kmem_free((void *)(uintptr_t)svar->dtsv_data, 7727 svar->dtsv_size); 7728 } 7729 7730 kmem_free(svar, sizeof (dtrace_statvar_t)); 7731 svarp[id] = NULL; 7732 } 7733 7734 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 7735 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 7736 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 7737 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 7738 7739 kmem_free(dp, sizeof (dtrace_difo_t)); 7740 } 7741 7742 static void 7743 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7744 { 7745 int i; 7746 7747 ASSERT(MUTEX_HELD(&dtrace_lock)); 7748 ASSERT(dp->dtdo_refcnt != 0); 7749 7750 for (i = 0; i < dp->dtdo_varlen; i++) { 7751 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7752 7753 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 7754 continue; 7755 7756 ASSERT(dtrace_vtime_references > 0); 7757 if (--dtrace_vtime_references == 0) 7758 dtrace_vtime_disable(); 7759 } 7760 7761 if (--dp->dtdo_refcnt == 0) 7762 dtrace_difo_destroy(dp, vstate); 7763 } 7764 7765 /* 7766 * DTrace Format Functions 7767 */ 7768 static uint16_t 7769 dtrace_format_add(dtrace_state_t *state, char *str) 7770 { 7771 char *fmt, **new; 7772 uint16_t ndx, len = strlen(str) + 1; 7773 7774 fmt = kmem_zalloc(len, KM_SLEEP); 7775 bcopy(str, fmt, len); 7776 7777 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 7778 if (state->dts_formats[ndx] == NULL) { 7779 state->dts_formats[ndx] = fmt; 7780 return (ndx + 1); 7781 } 7782 } 7783 7784 if (state->dts_nformats == USHRT_MAX) { 7785 /* 7786 * This is only likely if a denial-of-service attack is being 7787 * attempted. As such, it's okay to fail silently here. 7788 */ 7789 kmem_free(fmt, len); 7790 return (0); 7791 } 7792 7793 /* 7794 * For simplicity, we always resize the formats array to be exactly the 7795 * number of formats. 7796 */ 7797 ndx = state->dts_nformats++; 7798 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 7799 7800 if (state->dts_formats != NULL) { 7801 ASSERT(ndx != 0); 7802 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 7803 kmem_free(state->dts_formats, ndx * sizeof (char *)); 7804 } 7805 7806 state->dts_formats = new; 7807 state->dts_formats[ndx] = fmt; 7808 7809 return (ndx + 1); 7810 } 7811 7812 static void 7813 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 7814 { 7815 char *fmt; 7816 7817 ASSERT(state->dts_formats != NULL); 7818 ASSERT(format <= state->dts_nformats); 7819 ASSERT(state->dts_formats[format - 1] != NULL); 7820 7821 fmt = state->dts_formats[format - 1]; 7822 kmem_free(fmt, strlen(fmt) + 1); 7823 state->dts_formats[format - 1] = NULL; 7824 } 7825 7826 static void 7827 dtrace_format_destroy(dtrace_state_t *state) 7828 { 7829 int i; 7830 7831 if (state->dts_nformats == 0) { 7832 ASSERT(state->dts_formats == NULL); 7833 return; 7834 } 7835 7836 ASSERT(state->dts_formats != NULL); 7837 7838 for (i = 0; i < state->dts_nformats; i++) { 7839 char *fmt = state->dts_formats[i]; 7840 7841 if (fmt == NULL) 7842 continue; 7843 7844 kmem_free(fmt, strlen(fmt) + 1); 7845 } 7846 7847 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 7848 state->dts_nformats = 0; 7849 state->dts_formats = NULL; 7850 } 7851 7852 /* 7853 * DTrace Predicate Functions 7854 */ 7855 static dtrace_predicate_t * 7856 dtrace_predicate_create(dtrace_difo_t *dp) 7857 { 7858 dtrace_predicate_t *pred; 7859 7860 ASSERT(MUTEX_HELD(&dtrace_lock)); 7861 ASSERT(dp->dtdo_refcnt != 0); 7862 7863 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 7864 pred->dtp_difo = dp; 7865 pred->dtp_refcnt = 1; 7866 7867 if (!dtrace_difo_cacheable(dp)) 7868 return (pred); 7869 7870 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 7871 /* 7872 * This is only theoretically possible -- we have had 2^32 7873 * cacheable predicates on this machine. We cannot allow any 7874 * more predicates to become cacheable: as unlikely as it is, 7875 * there may be a thread caching a (now stale) predicate cache 7876 * ID. (N.B.: the temptation is being successfully resisted to 7877 * have this cmn_err() "Holy shit -- we executed this code!") 7878 */ 7879 return (pred); 7880 } 7881 7882 pred->dtp_cacheid = dtrace_predcache_id++; 7883 7884 return (pred); 7885 } 7886 7887 static void 7888 dtrace_predicate_hold(dtrace_predicate_t *pred) 7889 { 7890 ASSERT(MUTEX_HELD(&dtrace_lock)); 7891 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 7892 ASSERT(pred->dtp_refcnt > 0); 7893 7894 pred->dtp_refcnt++; 7895 } 7896 7897 static void 7898 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 7899 { 7900 dtrace_difo_t *dp = pred->dtp_difo; 7901 7902 ASSERT(MUTEX_HELD(&dtrace_lock)); 7903 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 7904 ASSERT(pred->dtp_refcnt > 0); 7905 7906 if (--pred->dtp_refcnt == 0) { 7907 dtrace_difo_release(pred->dtp_difo, vstate); 7908 kmem_free(pred, sizeof (dtrace_predicate_t)); 7909 } 7910 } 7911 7912 /* 7913 * DTrace Action Description Functions 7914 */ 7915 static dtrace_actdesc_t * 7916 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 7917 uint64_t uarg, uint64_t arg) 7918 { 7919 dtrace_actdesc_t *act; 7920 7921 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 7922 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 7923 7924 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 7925 act->dtad_kind = kind; 7926 act->dtad_ntuple = ntuple; 7927 act->dtad_uarg = uarg; 7928 act->dtad_arg = arg; 7929 act->dtad_refcnt = 1; 7930 7931 return (act); 7932 } 7933 7934 static void 7935 dtrace_actdesc_hold(dtrace_actdesc_t *act) 7936 { 7937 ASSERT(act->dtad_refcnt >= 1); 7938 act->dtad_refcnt++; 7939 } 7940 7941 static void 7942 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 7943 { 7944 dtrace_actkind_t kind = act->dtad_kind; 7945 dtrace_difo_t *dp; 7946 7947 ASSERT(act->dtad_refcnt >= 1); 7948 7949 if (--act->dtad_refcnt != 0) 7950 return; 7951 7952 if ((dp = act->dtad_difo) != NULL) 7953 dtrace_difo_release(dp, vstate); 7954 7955 if (DTRACEACT_ISPRINTFLIKE(kind)) { 7956 char *str = (char *)(uintptr_t)act->dtad_arg; 7957 7958 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 7959 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 7960 7961 if (str != NULL) 7962 kmem_free(str, strlen(str) + 1); 7963 } 7964 7965 kmem_free(act, sizeof (dtrace_actdesc_t)); 7966 } 7967 7968 /* 7969 * DTrace ECB Functions 7970 */ 7971 static dtrace_ecb_t * 7972 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 7973 { 7974 dtrace_ecb_t *ecb; 7975 dtrace_epid_t epid; 7976 7977 ASSERT(MUTEX_HELD(&dtrace_lock)); 7978 7979 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 7980 ecb->dte_predicate = NULL; 7981 ecb->dte_probe = probe; 7982 7983 /* 7984 * The default size is the size of the default action: recording 7985 * the epid. 7986 */ 7987 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 7988 ecb->dte_alignment = sizeof (dtrace_epid_t); 7989 7990 epid = state->dts_epid++; 7991 7992 if (epid - 1 >= state->dts_necbs) { 7993 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 7994 int necbs = state->dts_necbs << 1; 7995 7996 ASSERT(epid == state->dts_necbs + 1); 7997 7998 if (necbs == 0) { 7999 ASSERT(oecbs == NULL); 8000 necbs = 1; 8001 } 8002 8003 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 8004 8005 if (oecbs != NULL) 8006 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 8007 8008 dtrace_membar_producer(); 8009 state->dts_ecbs = ecbs; 8010 8011 if (oecbs != NULL) { 8012 /* 8013 * If this state is active, we must dtrace_sync() 8014 * before we can free the old dts_ecbs array: we're 8015 * coming in hot, and there may be active ring 8016 * buffer processing (which indexes into the dts_ecbs 8017 * array) on another CPU. 8018 */ 8019 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 8020 dtrace_sync(); 8021 8022 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 8023 } 8024 8025 dtrace_membar_producer(); 8026 state->dts_necbs = necbs; 8027 } 8028 8029 ecb->dte_state = state; 8030 8031 ASSERT(state->dts_ecbs[epid - 1] == NULL); 8032 dtrace_membar_producer(); 8033 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 8034 8035 return (ecb); 8036 } 8037 8038 static void 8039 dtrace_ecb_enable(dtrace_ecb_t *ecb) 8040 { 8041 dtrace_probe_t *probe = ecb->dte_probe; 8042 8043 ASSERT(MUTEX_HELD(&cpu_lock)); 8044 ASSERT(MUTEX_HELD(&dtrace_lock)); 8045 ASSERT(ecb->dte_next == NULL); 8046 8047 if (probe == NULL) { 8048 /* 8049 * This is the NULL probe -- there's nothing to do. 8050 */ 8051 return; 8052 } 8053 8054 if (probe->dtpr_ecb == NULL) { 8055 dtrace_provider_t *prov = probe->dtpr_provider; 8056 8057 /* 8058 * We're the first ECB on this probe. 8059 */ 8060 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 8061 8062 if (ecb->dte_predicate != NULL) 8063 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 8064 8065 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 8066 probe->dtpr_id, probe->dtpr_arg); 8067 } else { 8068 /* 8069 * This probe is already active. Swing the last pointer to 8070 * point to the new ECB, and issue a dtrace_sync() to assure 8071 * that all CPUs have seen the change. 8072 */ 8073 ASSERT(probe->dtpr_ecb_last != NULL); 8074 probe->dtpr_ecb_last->dte_next = ecb; 8075 probe->dtpr_ecb_last = ecb; 8076 probe->dtpr_predcache = 0; 8077 8078 dtrace_sync(); 8079 } 8080 } 8081 8082 static void 8083 dtrace_ecb_resize(dtrace_ecb_t *ecb) 8084 { 8085 uint32_t maxalign = sizeof (dtrace_epid_t); 8086 uint32_t align = sizeof (uint8_t), offs, diff; 8087 dtrace_action_t *act; 8088 int wastuple = 0; 8089 uint32_t aggbase = UINT32_MAX; 8090 dtrace_state_t *state = ecb->dte_state; 8091 8092 /* 8093 * If we record anything, we always record the epid. (And we always 8094 * record it first.) 8095 */ 8096 offs = sizeof (dtrace_epid_t); 8097 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 8098 8099 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 8100 dtrace_recdesc_t *rec = &act->dta_rec; 8101 8102 if ((align = rec->dtrd_alignment) > maxalign) 8103 maxalign = align; 8104 8105 if (!wastuple && act->dta_intuple) { 8106 /* 8107 * This is the first record in a tuple. Align the 8108 * offset to be at offset 4 in an 8-byte aligned 8109 * block. 8110 */ 8111 diff = offs + sizeof (dtrace_aggid_t); 8112 8113 if (diff = (diff & (sizeof (uint64_t) - 1))) 8114 offs += sizeof (uint64_t) - diff; 8115 8116 aggbase = offs - sizeof (dtrace_aggid_t); 8117 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 8118 } 8119 8120 /*LINTED*/ 8121 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 8122 /* 8123 * The current offset is not properly aligned; align it. 8124 */ 8125 offs += align - diff; 8126 } 8127 8128 rec->dtrd_offset = offs; 8129 8130 if (offs + rec->dtrd_size > ecb->dte_needed) { 8131 ecb->dte_needed = offs + rec->dtrd_size; 8132 8133 if (ecb->dte_needed > state->dts_needed) 8134 state->dts_needed = ecb->dte_needed; 8135 } 8136 8137 if (DTRACEACT_ISAGG(act->dta_kind)) { 8138 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 8139 dtrace_action_t *first = agg->dtag_first, *prev; 8140 8141 ASSERT(rec->dtrd_size != 0 && first != NULL); 8142 ASSERT(wastuple); 8143 ASSERT(aggbase != UINT32_MAX); 8144 8145 agg->dtag_base = aggbase; 8146 8147 while ((prev = first->dta_prev) != NULL && 8148 DTRACEACT_ISAGG(prev->dta_kind)) { 8149 agg = (dtrace_aggregation_t *)prev; 8150 first = agg->dtag_first; 8151 } 8152 8153 if (prev != NULL) { 8154 offs = prev->dta_rec.dtrd_offset + 8155 prev->dta_rec.dtrd_size; 8156 } else { 8157 offs = sizeof (dtrace_epid_t); 8158 } 8159 wastuple = 0; 8160 } else { 8161 if (!act->dta_intuple) 8162 ecb->dte_size = offs + rec->dtrd_size; 8163 8164 offs += rec->dtrd_size; 8165 } 8166 8167 wastuple = act->dta_intuple; 8168 } 8169 8170 if ((act = ecb->dte_action) != NULL && 8171 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 8172 ecb->dte_size == sizeof (dtrace_epid_t)) { 8173 /* 8174 * If the size is still sizeof (dtrace_epid_t), then all 8175 * actions store no data; set the size to 0. 8176 */ 8177 ecb->dte_alignment = maxalign; 8178 ecb->dte_size = 0; 8179 8180 /* 8181 * If the needed space is still sizeof (dtrace_epid_t), then 8182 * all actions need no additional space; set the needed 8183 * size to 0. 8184 */ 8185 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 8186 ecb->dte_needed = 0; 8187 8188 return; 8189 } 8190 8191 /* 8192 * Set our alignment, and make sure that the dte_size and dte_needed 8193 * are aligned to the size of an EPID. 8194 */ 8195 ecb->dte_alignment = maxalign; 8196 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 8197 ~(sizeof (dtrace_epid_t) - 1); 8198 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 8199 ~(sizeof (dtrace_epid_t) - 1); 8200 ASSERT(ecb->dte_size <= ecb->dte_needed); 8201 } 8202 8203 static dtrace_action_t * 8204 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 8205 { 8206 dtrace_aggregation_t *agg; 8207 size_t size = sizeof (uint64_t); 8208 int ntuple = desc->dtad_ntuple; 8209 dtrace_action_t *act; 8210 dtrace_recdesc_t *frec; 8211 dtrace_aggid_t aggid; 8212 dtrace_state_t *state = ecb->dte_state; 8213 8214 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 8215 agg->dtag_ecb = ecb; 8216 8217 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 8218 8219 switch (desc->dtad_kind) { 8220 case DTRACEAGG_MIN: 8221 agg->dtag_initial = UINT64_MAX; 8222 agg->dtag_aggregate = dtrace_aggregate_min; 8223 break; 8224 8225 case DTRACEAGG_MAX: 8226 agg->dtag_aggregate = dtrace_aggregate_max; 8227 break; 8228 8229 case DTRACEAGG_COUNT: 8230 agg->dtag_aggregate = dtrace_aggregate_count; 8231 break; 8232 8233 case DTRACEAGG_QUANTIZE: 8234 agg->dtag_aggregate = dtrace_aggregate_quantize; 8235 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 8236 sizeof (uint64_t); 8237 break; 8238 8239 case DTRACEAGG_LQUANTIZE: { 8240 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 8241 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 8242 8243 agg->dtag_initial = desc->dtad_arg; 8244 agg->dtag_aggregate = dtrace_aggregate_lquantize; 8245 8246 if (step == 0 || levels == 0) 8247 goto err; 8248 8249 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 8250 break; 8251 } 8252 8253 case DTRACEAGG_AVG: 8254 agg->dtag_aggregate = dtrace_aggregate_avg; 8255 size = sizeof (uint64_t) * 2; 8256 break; 8257 8258 case DTRACEAGG_SUM: 8259 agg->dtag_aggregate = dtrace_aggregate_sum; 8260 break; 8261 8262 default: 8263 goto err; 8264 } 8265 8266 agg->dtag_action.dta_rec.dtrd_size = size; 8267 8268 if (ntuple == 0) 8269 goto err; 8270 8271 /* 8272 * We must make sure that we have enough actions for the n-tuple. 8273 */ 8274 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 8275 if (DTRACEACT_ISAGG(act->dta_kind)) 8276 break; 8277 8278 if (--ntuple == 0) { 8279 /* 8280 * This is the action with which our n-tuple begins. 8281 */ 8282 agg->dtag_first = act; 8283 goto success; 8284 } 8285 } 8286 8287 /* 8288 * This n-tuple is short by ntuple elements. Return failure. 8289 */ 8290 ASSERT(ntuple != 0); 8291 err: 8292 kmem_free(agg, sizeof (dtrace_aggregation_t)); 8293 return (NULL); 8294 8295 success: 8296 /* 8297 * If the last action in the tuple has a size of zero, it's actually 8298 * an expression argument for the aggregating action. 8299 */ 8300 ASSERT(ecb->dte_action_last != NULL); 8301 act = ecb->dte_action_last; 8302 8303 if (act->dta_kind == DTRACEACT_DIFEXPR) { 8304 ASSERT(act->dta_difo != NULL); 8305 8306 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 8307 agg->dtag_hasarg = 1; 8308 } 8309 8310 /* 8311 * We need to allocate an id for this aggregation. 8312 */ 8313 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 8314 VM_BESTFIT | VM_SLEEP); 8315 8316 if (aggid - 1 >= state->dts_naggregations) { 8317 dtrace_aggregation_t **oaggs = state->dts_aggregations; 8318 dtrace_aggregation_t **aggs; 8319 int naggs = state->dts_naggregations << 1; 8320 int onaggs = state->dts_naggregations; 8321 8322 ASSERT(aggid == state->dts_naggregations + 1); 8323 8324 if (naggs == 0) { 8325 ASSERT(oaggs == NULL); 8326 naggs = 1; 8327 } 8328 8329 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 8330 8331 if (oaggs != NULL) { 8332 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 8333 kmem_free(oaggs, onaggs * sizeof (*aggs)); 8334 } 8335 8336 state->dts_aggregations = aggs; 8337 state->dts_naggregations = naggs; 8338 } 8339 8340 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 8341 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 8342 8343 frec = &agg->dtag_first->dta_rec; 8344 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 8345 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 8346 8347 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 8348 ASSERT(!act->dta_intuple); 8349 act->dta_intuple = 1; 8350 } 8351 8352 return (&agg->dtag_action); 8353 } 8354 8355 static void 8356 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 8357 { 8358 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 8359 dtrace_state_t *state = ecb->dte_state; 8360 dtrace_aggid_t aggid = agg->dtag_id; 8361 8362 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 8363 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 8364 8365 ASSERT(state->dts_aggregations[aggid - 1] == agg); 8366 state->dts_aggregations[aggid - 1] = NULL; 8367 8368 kmem_free(agg, sizeof (dtrace_aggregation_t)); 8369 } 8370 8371 static int 8372 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 8373 { 8374 dtrace_action_t *action, *last; 8375 dtrace_difo_t *dp = desc->dtad_difo; 8376 uint32_t size = 0, align = sizeof (uint8_t), mask; 8377 uint16_t format = 0; 8378 dtrace_recdesc_t *rec; 8379 dtrace_state_t *state = ecb->dte_state; 8380 dtrace_optval_t *opt = state->dts_options, nframes, strsize; 8381 uint64_t arg = desc->dtad_arg; 8382 8383 ASSERT(MUTEX_HELD(&dtrace_lock)); 8384 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 8385 8386 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 8387 /* 8388 * If this is an aggregating action, there must be neither 8389 * a speculate nor a commit on the action chain. 8390 */ 8391 dtrace_action_t *act; 8392 8393 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 8394 if (act->dta_kind == DTRACEACT_COMMIT) 8395 return (EINVAL); 8396 8397 if (act->dta_kind == DTRACEACT_SPECULATE) 8398 return (EINVAL); 8399 } 8400 8401 action = dtrace_ecb_aggregation_create(ecb, desc); 8402 8403 if (action == NULL) 8404 return (EINVAL); 8405 } else { 8406 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 8407 (desc->dtad_kind == DTRACEACT_DIFEXPR && 8408 dp != NULL && dp->dtdo_destructive)) { 8409 state->dts_destructive = 1; 8410 } 8411 8412 switch (desc->dtad_kind) { 8413 case DTRACEACT_PRINTF: 8414 case DTRACEACT_PRINTA: 8415 case DTRACEACT_SYSTEM: 8416 case DTRACEACT_FREOPEN: 8417 /* 8418 * We know that our arg is a string -- turn it into a 8419 * format. 8420 */ 8421 if (arg == NULL) { 8422 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); 8423 format = 0; 8424 } else { 8425 ASSERT(arg != NULL); 8426 ASSERT(arg > KERNELBASE); 8427 format = dtrace_format_add(state, 8428 (char *)(uintptr_t)arg); 8429 } 8430 8431 /*FALLTHROUGH*/ 8432 case DTRACEACT_LIBACT: 8433 case DTRACEACT_DIFEXPR: 8434 if (dp == NULL) 8435 return (EINVAL); 8436 8437 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 8438 break; 8439 8440 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 8441 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8442 return (EINVAL); 8443 8444 size = opt[DTRACEOPT_STRSIZE]; 8445 } 8446 8447 break; 8448 8449 case DTRACEACT_STACK: 8450 if ((nframes = arg) == 0) { 8451 nframes = opt[DTRACEOPT_STACKFRAMES]; 8452 ASSERT(nframes > 0); 8453 arg = nframes; 8454 } 8455 8456 size = nframes * sizeof (pc_t); 8457 break; 8458 8459 case DTRACEACT_JSTACK: 8460 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 8461 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 8462 8463 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 8464 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 8465 8466 arg = DTRACE_USTACK_ARG(nframes, strsize); 8467 8468 /*FALLTHROUGH*/ 8469 case DTRACEACT_USTACK: 8470 if (desc->dtad_kind != DTRACEACT_JSTACK && 8471 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 8472 strsize = DTRACE_USTACK_STRSIZE(arg); 8473 nframes = opt[DTRACEOPT_USTACKFRAMES]; 8474 ASSERT(nframes > 0); 8475 arg = DTRACE_USTACK_ARG(nframes, strsize); 8476 } 8477 8478 /* 8479 * Save a slot for the pid. 8480 */ 8481 size = (nframes + 1) * sizeof (uint64_t); 8482 size += DTRACE_USTACK_STRSIZE(arg); 8483 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 8484 8485 break; 8486 8487 case DTRACEACT_SYM: 8488 case DTRACEACT_MOD: 8489 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 8490 sizeof (uint64_t)) || 8491 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8492 return (EINVAL); 8493 break; 8494 8495 case DTRACEACT_USYM: 8496 case DTRACEACT_UMOD: 8497 case DTRACEACT_UADDR: 8498 if (dp == NULL || 8499 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 8500 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8501 return (EINVAL); 8502 8503 /* 8504 * We have a slot for the pid, plus a slot for the 8505 * argument. To keep things simple (aligned with 8506 * bitness-neutral sizing), we store each as a 64-bit 8507 * quantity. 8508 */ 8509 size = 2 * sizeof (uint64_t); 8510 break; 8511 8512 case DTRACEACT_STOP: 8513 case DTRACEACT_BREAKPOINT: 8514 case DTRACEACT_PANIC: 8515 break; 8516 8517 case DTRACEACT_CHILL: 8518 case DTRACEACT_DISCARD: 8519 case DTRACEACT_RAISE: 8520 if (dp == NULL) 8521 return (EINVAL); 8522 break; 8523 8524 case DTRACEACT_EXIT: 8525 if (dp == NULL || 8526 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 8527 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8528 return (EINVAL); 8529 break; 8530 8531 case DTRACEACT_SPECULATE: 8532 if (ecb->dte_size > sizeof (dtrace_epid_t)) 8533 return (EINVAL); 8534 8535 if (dp == NULL) 8536 return (EINVAL); 8537 8538 state->dts_speculates = 1; 8539 break; 8540 8541 case DTRACEACT_COMMIT: { 8542 dtrace_action_t *act = ecb->dte_action; 8543 8544 for (; act != NULL; act = act->dta_next) { 8545 if (act->dta_kind == DTRACEACT_COMMIT) 8546 return (EINVAL); 8547 } 8548 8549 if (dp == NULL) 8550 return (EINVAL); 8551 break; 8552 } 8553 8554 default: 8555 return (EINVAL); 8556 } 8557 8558 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 8559 /* 8560 * If this is a data-storing action or a speculate, 8561 * we must be sure that there isn't a commit on the 8562 * action chain. 8563 */ 8564 dtrace_action_t *act = ecb->dte_action; 8565 8566 for (; act != NULL; act = act->dta_next) { 8567 if (act->dta_kind == DTRACEACT_COMMIT) 8568 return (EINVAL); 8569 } 8570 } 8571 8572 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 8573 action->dta_rec.dtrd_size = size; 8574 } 8575 8576 action->dta_refcnt = 1; 8577 rec = &action->dta_rec; 8578 size = rec->dtrd_size; 8579 8580 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 8581 if (!(size & mask)) { 8582 align = mask + 1; 8583 break; 8584 } 8585 } 8586 8587 action->dta_kind = desc->dtad_kind; 8588 8589 if ((action->dta_difo = dp) != NULL) 8590 dtrace_difo_hold(dp); 8591 8592 rec->dtrd_action = action->dta_kind; 8593 rec->dtrd_arg = arg; 8594 8595 if (ecb->dte_state == dtrace_anon.dta_state) { 8596 /* 8597 * If this is an anonymous enabling, explicitly clear the uarg. 8598 */ 8599 rec->dtrd_uarg = 0; 8600 } else { 8601 rec->dtrd_uarg = desc->dtad_uarg; 8602 } 8603 8604 rec->dtrd_alignment = (uint16_t)align; 8605 rec->dtrd_format = format; 8606 8607 if ((last = ecb->dte_action_last) != NULL) { 8608 ASSERT(ecb->dte_action != NULL); 8609 action->dta_prev = last; 8610 last->dta_next = action; 8611 } else { 8612 ASSERT(ecb->dte_action == NULL); 8613 ecb->dte_action = action; 8614 } 8615 8616 ecb->dte_action_last = action; 8617 8618 return (0); 8619 } 8620 8621 static void 8622 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 8623 { 8624 dtrace_action_t *act = ecb->dte_action, *next; 8625 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 8626 dtrace_difo_t *dp; 8627 uint16_t format; 8628 8629 if (act != NULL && act->dta_refcnt > 1) { 8630 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 8631 act->dta_refcnt--; 8632 } else { 8633 for (; act != NULL; act = next) { 8634 next = act->dta_next; 8635 ASSERT(next != NULL || act == ecb->dte_action_last); 8636 ASSERT(act->dta_refcnt == 1); 8637 8638 if ((format = act->dta_rec.dtrd_format) != 0) 8639 dtrace_format_remove(ecb->dte_state, format); 8640 8641 if ((dp = act->dta_difo) != NULL) 8642 dtrace_difo_release(dp, vstate); 8643 8644 if (DTRACEACT_ISAGG(act->dta_kind)) { 8645 dtrace_ecb_aggregation_destroy(ecb, act); 8646 } else { 8647 kmem_free(act, sizeof (dtrace_action_t)); 8648 } 8649 } 8650 } 8651 8652 ecb->dte_action = NULL; 8653 ecb->dte_action_last = NULL; 8654 ecb->dte_size = sizeof (dtrace_epid_t); 8655 } 8656 8657 static void 8658 dtrace_ecb_disable(dtrace_ecb_t *ecb) 8659 { 8660 /* 8661 * We disable the ECB by removing it from its probe. 8662 */ 8663 dtrace_ecb_t *pecb, *prev = NULL; 8664 dtrace_probe_t *probe = ecb->dte_probe; 8665 8666 ASSERT(MUTEX_HELD(&dtrace_lock)); 8667 8668 if (probe == NULL) { 8669 /* 8670 * This is the NULL probe; there is nothing to disable. 8671 */ 8672 return; 8673 } 8674 8675 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 8676 if (pecb == ecb) 8677 break; 8678 prev = pecb; 8679 } 8680 8681 ASSERT(pecb != NULL); 8682 8683 if (prev == NULL) { 8684 probe->dtpr_ecb = ecb->dte_next; 8685 } else { 8686 prev->dte_next = ecb->dte_next; 8687 } 8688 8689 if (ecb == probe->dtpr_ecb_last) { 8690 ASSERT(ecb->dte_next == NULL); 8691 probe->dtpr_ecb_last = prev; 8692 } 8693 8694 /* 8695 * The ECB has been disconnected from the probe; now sync to assure 8696 * that all CPUs have seen the change before returning. 8697 */ 8698 dtrace_sync(); 8699 8700 if (probe->dtpr_ecb == NULL) { 8701 /* 8702 * That was the last ECB on the probe; clear the predicate 8703 * cache ID for the probe, disable it and sync one more time 8704 * to assure that we'll never hit it again. 8705 */ 8706 dtrace_provider_t *prov = probe->dtpr_provider; 8707 8708 ASSERT(ecb->dte_next == NULL); 8709 ASSERT(probe->dtpr_ecb_last == NULL); 8710 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 8711 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 8712 probe->dtpr_id, probe->dtpr_arg); 8713 dtrace_sync(); 8714 } else { 8715 /* 8716 * There is at least one ECB remaining on the probe. If there 8717 * is _exactly_ one, set the probe's predicate cache ID to be 8718 * the predicate cache ID of the remaining ECB. 8719 */ 8720 ASSERT(probe->dtpr_ecb_last != NULL); 8721 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 8722 8723 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 8724 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 8725 8726 ASSERT(probe->dtpr_ecb->dte_next == NULL); 8727 8728 if (p != NULL) 8729 probe->dtpr_predcache = p->dtp_cacheid; 8730 } 8731 8732 ecb->dte_next = NULL; 8733 } 8734 } 8735 8736 static void 8737 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 8738 { 8739 dtrace_state_t *state = ecb->dte_state; 8740 dtrace_vstate_t *vstate = &state->dts_vstate; 8741 dtrace_predicate_t *pred; 8742 dtrace_epid_t epid = ecb->dte_epid; 8743 8744 ASSERT(MUTEX_HELD(&dtrace_lock)); 8745 ASSERT(ecb->dte_next == NULL); 8746 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 8747 8748 if ((pred = ecb->dte_predicate) != NULL) 8749 dtrace_predicate_release(pred, vstate); 8750 8751 dtrace_ecb_action_remove(ecb); 8752 8753 ASSERT(state->dts_ecbs[epid - 1] == ecb); 8754 state->dts_ecbs[epid - 1] = NULL; 8755 8756 kmem_free(ecb, sizeof (dtrace_ecb_t)); 8757 } 8758 8759 static dtrace_ecb_t * 8760 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 8761 dtrace_enabling_t *enab) 8762 { 8763 dtrace_ecb_t *ecb; 8764 dtrace_predicate_t *pred; 8765 dtrace_actdesc_t *act; 8766 dtrace_provider_t *prov; 8767 dtrace_ecbdesc_t *desc = enab->dten_current; 8768 8769 ASSERT(MUTEX_HELD(&dtrace_lock)); 8770 ASSERT(state != NULL); 8771 8772 ecb = dtrace_ecb_add(state, probe); 8773 ecb->dte_uarg = desc->dted_uarg; 8774 8775 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 8776 dtrace_predicate_hold(pred); 8777 ecb->dte_predicate = pred; 8778 } 8779 8780 if (probe != NULL) { 8781 /* 8782 * If the provider shows more leg than the consumer is old 8783 * enough to see, we need to enable the appropriate implicit 8784 * predicate bits to prevent the ecb from activating at 8785 * revealing times. 8786 */ 8787 prov = probe->dtpr_provider; 8788 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 8789 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 8790 ecb->dte_cond |= DTRACE_COND_OWNER; 8791 8792 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 8793 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 8794 ecb->dte_cond |= DTRACE_COND_USERMODE; 8795 } 8796 8797 if (dtrace_ecb_create_cache != NULL) { 8798 /* 8799 * If we have a cached ecb, we'll use its action list instead 8800 * of creating our own (saving both time and space). 8801 */ 8802 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 8803 dtrace_action_t *act = cached->dte_action; 8804 8805 if (act != NULL) { 8806 ASSERT(act->dta_refcnt > 0); 8807 act->dta_refcnt++; 8808 ecb->dte_action = act; 8809 ecb->dte_action_last = cached->dte_action_last; 8810 ecb->dte_needed = cached->dte_needed; 8811 ecb->dte_size = cached->dte_size; 8812 ecb->dte_alignment = cached->dte_alignment; 8813 } 8814 8815 return (ecb); 8816 } 8817 8818 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 8819 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 8820 dtrace_ecb_destroy(ecb); 8821 return (NULL); 8822 } 8823 } 8824 8825 dtrace_ecb_resize(ecb); 8826 8827 return (dtrace_ecb_create_cache = ecb); 8828 } 8829 8830 static int 8831 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 8832 { 8833 dtrace_ecb_t *ecb; 8834 dtrace_enabling_t *enab = arg; 8835 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 8836 8837 ASSERT(state != NULL); 8838 8839 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 8840 /* 8841 * This probe was created in a generation for which this 8842 * enabling has previously created ECBs; we don't want to 8843 * enable it again, so just kick out. 8844 */ 8845 return (DTRACE_MATCH_NEXT); 8846 } 8847 8848 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 8849 return (DTRACE_MATCH_DONE); 8850 8851 dtrace_ecb_enable(ecb); 8852 return (DTRACE_MATCH_NEXT); 8853 } 8854 8855 static dtrace_ecb_t * 8856 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 8857 { 8858 dtrace_ecb_t *ecb; 8859 8860 ASSERT(MUTEX_HELD(&dtrace_lock)); 8861 8862 if (id == 0 || id > state->dts_necbs) 8863 return (NULL); 8864 8865 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 8866 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 8867 8868 return (state->dts_ecbs[id - 1]); 8869 } 8870 8871 static dtrace_aggregation_t * 8872 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 8873 { 8874 dtrace_aggregation_t *agg; 8875 8876 ASSERT(MUTEX_HELD(&dtrace_lock)); 8877 8878 if (id == 0 || id > state->dts_naggregations) 8879 return (NULL); 8880 8881 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 8882 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 8883 agg->dtag_id == id); 8884 8885 return (state->dts_aggregations[id - 1]); 8886 } 8887 8888 /* 8889 * DTrace Buffer Functions 8890 * 8891 * The following functions manipulate DTrace buffers. Most of these functions 8892 * are called in the context of establishing or processing consumer state; 8893 * exceptions are explicitly noted. 8894 */ 8895 8896 /* 8897 * Note: called from cross call context. This function switches the two 8898 * buffers on a given CPU. The atomicity of this operation is assured by 8899 * disabling interrupts while the actual switch takes place; the disabling of 8900 * interrupts serializes the execution with any execution of dtrace_probe() on 8901 * the same CPU. 8902 */ 8903 static void 8904 dtrace_buffer_switch(dtrace_buffer_t *buf) 8905 { 8906 caddr_t tomax = buf->dtb_tomax; 8907 caddr_t xamot = buf->dtb_xamot; 8908 dtrace_icookie_t cookie; 8909 8910 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 8911 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 8912 8913 cookie = dtrace_interrupt_disable(); 8914 buf->dtb_tomax = xamot; 8915 buf->dtb_xamot = tomax; 8916 buf->dtb_xamot_drops = buf->dtb_drops; 8917 buf->dtb_xamot_offset = buf->dtb_offset; 8918 buf->dtb_xamot_errors = buf->dtb_errors; 8919 buf->dtb_xamot_flags = buf->dtb_flags; 8920 buf->dtb_offset = 0; 8921 buf->dtb_drops = 0; 8922 buf->dtb_errors = 0; 8923 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 8924 dtrace_interrupt_enable(cookie); 8925 } 8926 8927 /* 8928 * Note: called from cross call context. This function activates a buffer 8929 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 8930 * is guaranteed by the disabling of interrupts. 8931 */ 8932 static void 8933 dtrace_buffer_activate(dtrace_state_t *state) 8934 { 8935 dtrace_buffer_t *buf; 8936 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 8937 8938 buf = &state->dts_buffer[CPU->cpu_id]; 8939 8940 if (buf->dtb_tomax != NULL) { 8941 /* 8942 * We might like to assert that the buffer is marked inactive, 8943 * but this isn't necessarily true: the buffer for the CPU 8944 * that processes the BEGIN probe has its buffer activated 8945 * manually. In this case, we take the (harmless) action 8946 * re-clearing the bit INACTIVE bit. 8947 */ 8948 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 8949 } 8950 8951 dtrace_interrupt_enable(cookie); 8952 } 8953 8954 static int 8955 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 8956 processorid_t cpu) 8957 { 8958 cpu_t *cp; 8959 dtrace_buffer_t *buf; 8960 8961 ASSERT(MUTEX_HELD(&cpu_lock)); 8962 ASSERT(MUTEX_HELD(&dtrace_lock)); 8963 8964 if (crgetuid(CRED()) != 0 && size > dtrace_nonroot_maxsize) 8965 return (EFBIG); 8966 8967 cp = cpu_list; 8968 8969 do { 8970 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 8971 continue; 8972 8973 buf = &bufs[cp->cpu_id]; 8974 8975 /* 8976 * If there is already a buffer allocated for this CPU, it 8977 * is only possible that this is a DR event. In this case, 8978 * the buffer size must match our specified size. 8979 */ 8980 if (buf->dtb_tomax != NULL) { 8981 ASSERT(buf->dtb_size == size); 8982 continue; 8983 } 8984 8985 ASSERT(buf->dtb_xamot == NULL); 8986 8987 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 8988 goto err; 8989 8990 buf->dtb_size = size; 8991 buf->dtb_flags = flags; 8992 buf->dtb_offset = 0; 8993 buf->dtb_drops = 0; 8994 8995 if (flags & DTRACEBUF_NOSWITCH) 8996 continue; 8997 8998 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 8999 goto err; 9000 } while ((cp = cp->cpu_next) != cpu_list); 9001 9002 return (0); 9003 9004 err: 9005 cp = cpu_list; 9006 9007 do { 9008 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 9009 continue; 9010 9011 buf = &bufs[cp->cpu_id]; 9012 9013 if (buf->dtb_xamot != NULL) { 9014 ASSERT(buf->dtb_tomax != NULL); 9015 ASSERT(buf->dtb_size == size); 9016 kmem_free(buf->dtb_xamot, size); 9017 } 9018 9019 if (buf->dtb_tomax != NULL) { 9020 ASSERT(buf->dtb_size == size); 9021 kmem_free(buf->dtb_tomax, size); 9022 } 9023 9024 buf->dtb_tomax = NULL; 9025 buf->dtb_xamot = NULL; 9026 buf->dtb_size = 0; 9027 } while ((cp = cp->cpu_next) != cpu_list); 9028 9029 return (ENOMEM); 9030 } 9031 9032 /* 9033 * Note: called from probe context. This function just increments the drop 9034 * count on a buffer. It has been made a function to allow for the 9035 * possibility of understanding the source of mysterious drop counts. (A 9036 * problem for which one may be particularly disappointed that DTrace cannot 9037 * be used to understand DTrace.) 9038 */ 9039 static void 9040 dtrace_buffer_drop(dtrace_buffer_t *buf) 9041 { 9042 buf->dtb_drops++; 9043 } 9044 9045 /* 9046 * Note: called from probe context. This function is called to reserve space 9047 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 9048 * mstate. Returns the new offset in the buffer, or a negative value if an 9049 * error has occurred. 9050 */ 9051 static intptr_t 9052 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 9053 dtrace_state_t *state, dtrace_mstate_t *mstate) 9054 { 9055 intptr_t offs = buf->dtb_offset, soffs; 9056 intptr_t woffs; 9057 caddr_t tomax; 9058 size_t total; 9059 9060 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 9061 return (-1); 9062 9063 if ((tomax = buf->dtb_tomax) == NULL) { 9064 dtrace_buffer_drop(buf); 9065 return (-1); 9066 } 9067 9068 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 9069 while (offs & (align - 1)) { 9070 /* 9071 * Assert that our alignment is off by a number which 9072 * is itself sizeof (uint32_t) aligned. 9073 */ 9074 ASSERT(!((align - (offs & (align - 1))) & 9075 (sizeof (uint32_t) - 1))); 9076 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 9077 offs += sizeof (uint32_t); 9078 } 9079 9080 if ((soffs = offs + needed) > buf->dtb_size) { 9081 dtrace_buffer_drop(buf); 9082 return (-1); 9083 } 9084 9085 if (mstate == NULL) 9086 return (offs); 9087 9088 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 9089 mstate->dtms_scratch_size = buf->dtb_size - soffs; 9090 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 9091 9092 return (offs); 9093 } 9094 9095 if (buf->dtb_flags & DTRACEBUF_FILL) { 9096 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 9097 (buf->dtb_flags & DTRACEBUF_FULL)) 9098 return (-1); 9099 goto out; 9100 } 9101 9102 total = needed + (offs & (align - 1)); 9103 9104 /* 9105 * For a ring buffer, life is quite a bit more complicated. Before 9106 * we can store any padding, we need to adjust our wrapping offset. 9107 * (If we've never before wrapped or we're not about to, no adjustment 9108 * is required.) 9109 */ 9110 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 9111 offs + total > buf->dtb_size) { 9112 woffs = buf->dtb_xamot_offset; 9113 9114 if (offs + total > buf->dtb_size) { 9115 /* 9116 * We can't fit in the end of the buffer. First, a 9117 * sanity check that we can fit in the buffer at all. 9118 */ 9119 if (total > buf->dtb_size) { 9120 dtrace_buffer_drop(buf); 9121 return (-1); 9122 } 9123 9124 /* 9125 * We're going to be storing at the top of the buffer, 9126 * so now we need to deal with the wrapped offset. We 9127 * only reset our wrapped offset to 0 if it is 9128 * currently greater than the current offset. If it 9129 * is less than the current offset, it is because a 9130 * previous allocation induced a wrap -- but the 9131 * allocation didn't subsequently take the space due 9132 * to an error or false predicate evaluation. In this 9133 * case, we'll just leave the wrapped offset alone: if 9134 * the wrapped offset hasn't been advanced far enough 9135 * for this allocation, it will be adjusted in the 9136 * lower loop. 9137 */ 9138 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 9139 if (woffs >= offs) 9140 woffs = 0; 9141 } else { 9142 woffs = 0; 9143 } 9144 9145 /* 9146 * Now we know that we're going to be storing to the 9147 * top of the buffer and that there is room for us 9148 * there. We need to clear the buffer from the current 9149 * offset to the end (there may be old gunk there). 9150 */ 9151 while (offs < buf->dtb_size) 9152 tomax[offs++] = 0; 9153 9154 /* 9155 * We need to set our offset to zero. And because we 9156 * are wrapping, we need to set the bit indicating as 9157 * much. We can also adjust our needed space back 9158 * down to the space required by the ECB -- we know 9159 * that the top of the buffer is aligned. 9160 */ 9161 offs = 0; 9162 total = needed; 9163 buf->dtb_flags |= DTRACEBUF_WRAPPED; 9164 } else { 9165 /* 9166 * There is room for us in the buffer, so we simply 9167 * need to check the wrapped offset. 9168 */ 9169 if (woffs < offs) { 9170 /* 9171 * The wrapped offset is less than the offset. 9172 * This can happen if we allocated buffer space 9173 * that induced a wrap, but then we didn't 9174 * subsequently take the space due to an error 9175 * or false predicate evaluation. This is 9176 * okay; we know that _this_ allocation isn't 9177 * going to induce a wrap. We still can't 9178 * reset the wrapped offset to be zero, 9179 * however: the space may have been trashed in 9180 * the previous failed probe attempt. But at 9181 * least the wrapped offset doesn't need to 9182 * be adjusted at all... 9183 */ 9184 goto out; 9185 } 9186 } 9187 9188 while (offs + total > woffs) { 9189 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 9190 size_t size; 9191 9192 if (epid == DTRACE_EPIDNONE) { 9193 size = sizeof (uint32_t); 9194 } else { 9195 ASSERT(epid <= state->dts_necbs); 9196 ASSERT(state->dts_ecbs[epid - 1] != NULL); 9197 9198 size = state->dts_ecbs[epid - 1]->dte_size; 9199 } 9200 9201 ASSERT(woffs + size <= buf->dtb_size); 9202 ASSERT(size != 0); 9203 9204 if (woffs + size == buf->dtb_size) { 9205 /* 9206 * We've reached the end of the buffer; we want 9207 * to set the wrapped offset to 0 and break 9208 * out. However, if the offs is 0, then we're 9209 * in a strange edge-condition: the amount of 9210 * space that we want to reserve plus the size 9211 * of the record that we're overwriting is 9212 * greater than the size of the buffer. This 9213 * is problematic because if we reserve the 9214 * space but subsequently don't consume it (due 9215 * to a failed predicate or error) the wrapped 9216 * offset will be 0 -- yet the EPID at offset 0 9217 * will not be committed. This situation is 9218 * relatively easy to deal with: if we're in 9219 * this case, the buffer is indistinguishable 9220 * from one that hasn't wrapped; we need only 9221 * finish the job by clearing the wrapped bit, 9222 * explicitly setting the offset to be 0, and 9223 * zero'ing out the old data in the buffer. 9224 */ 9225 if (offs == 0) { 9226 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 9227 buf->dtb_offset = 0; 9228 woffs = total; 9229 9230 while (woffs < buf->dtb_size) 9231 tomax[woffs++] = 0; 9232 } 9233 9234 woffs = 0; 9235 break; 9236 } 9237 9238 woffs += size; 9239 } 9240 9241 /* 9242 * We have a wrapped offset. It may be that the wrapped offset 9243 * has become zero -- that's okay. 9244 */ 9245 buf->dtb_xamot_offset = woffs; 9246 } 9247 9248 out: 9249 /* 9250 * Now we can plow the buffer with any necessary padding. 9251 */ 9252 while (offs & (align - 1)) { 9253 /* 9254 * Assert that our alignment is off by a number which 9255 * is itself sizeof (uint32_t) aligned. 9256 */ 9257 ASSERT(!((align - (offs & (align - 1))) & 9258 (sizeof (uint32_t) - 1))); 9259 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 9260 offs += sizeof (uint32_t); 9261 } 9262 9263 if (buf->dtb_flags & DTRACEBUF_FILL) { 9264 if (offs + needed > buf->dtb_size - state->dts_reserve) { 9265 buf->dtb_flags |= DTRACEBUF_FULL; 9266 return (-1); 9267 } 9268 } 9269 9270 if (mstate == NULL) 9271 return (offs); 9272 9273 /* 9274 * For ring buffers and fill buffers, the scratch space is always 9275 * the inactive buffer. 9276 */ 9277 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 9278 mstate->dtms_scratch_size = buf->dtb_size; 9279 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 9280 9281 return (offs); 9282 } 9283 9284 static void 9285 dtrace_buffer_polish(dtrace_buffer_t *buf) 9286 { 9287 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 9288 ASSERT(MUTEX_HELD(&dtrace_lock)); 9289 9290 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 9291 return; 9292 9293 /* 9294 * We need to polish the ring buffer. There are three cases: 9295 * 9296 * - The first (and presumably most common) is that there is no gap 9297 * between the buffer offset and the wrapped offset. In this case, 9298 * there is nothing in the buffer that isn't valid data; we can 9299 * mark the buffer as polished and return. 9300 * 9301 * - The second (less common than the first but still more common 9302 * than the third) is that there is a gap between the buffer offset 9303 * and the wrapped offset, and the wrapped offset is larger than the 9304 * buffer offset. This can happen because of an alignment issue, or 9305 * can happen because of a call to dtrace_buffer_reserve() that 9306 * didn't subsequently consume the buffer space. In this case, 9307 * we need to zero the data from the buffer offset to the wrapped 9308 * offset. 9309 * 9310 * - The third (and least common) is that there is a gap between the 9311 * buffer offset and the wrapped offset, but the wrapped offset is 9312 * _less_ than the buffer offset. This can only happen because a 9313 * call to dtrace_buffer_reserve() induced a wrap, but the space 9314 * was not subsequently consumed. In this case, we need to zero the 9315 * space from the offset to the end of the buffer _and_ from the 9316 * top of the buffer to the wrapped offset. 9317 */ 9318 if (buf->dtb_offset < buf->dtb_xamot_offset) { 9319 bzero(buf->dtb_tomax + buf->dtb_offset, 9320 buf->dtb_xamot_offset - buf->dtb_offset); 9321 } 9322 9323 if (buf->dtb_offset > buf->dtb_xamot_offset) { 9324 bzero(buf->dtb_tomax + buf->dtb_offset, 9325 buf->dtb_size - buf->dtb_offset); 9326 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 9327 } 9328 } 9329 9330 static void 9331 dtrace_buffer_free(dtrace_buffer_t *bufs) 9332 { 9333 int i; 9334 9335 for (i = 0; i < NCPU; i++) { 9336 dtrace_buffer_t *buf = &bufs[i]; 9337 9338 if (buf->dtb_tomax == NULL) { 9339 ASSERT(buf->dtb_xamot == NULL); 9340 ASSERT(buf->dtb_size == 0); 9341 continue; 9342 } 9343 9344 if (buf->dtb_xamot != NULL) { 9345 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 9346 kmem_free(buf->dtb_xamot, buf->dtb_size); 9347 } 9348 9349 kmem_free(buf->dtb_tomax, buf->dtb_size); 9350 buf->dtb_size = 0; 9351 buf->dtb_tomax = NULL; 9352 buf->dtb_xamot = NULL; 9353 } 9354 } 9355 9356 /* 9357 * DTrace Enabling Functions 9358 */ 9359 static dtrace_enabling_t * 9360 dtrace_enabling_create(dtrace_vstate_t *vstate) 9361 { 9362 dtrace_enabling_t *enab; 9363 9364 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 9365 enab->dten_vstate = vstate; 9366 9367 return (enab); 9368 } 9369 9370 static void 9371 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 9372 { 9373 dtrace_ecbdesc_t **ndesc; 9374 size_t osize, nsize; 9375 9376 /* 9377 * We can't add to enablings after we've enabled them, or after we've 9378 * retained them. 9379 */ 9380 ASSERT(enab->dten_probegen == 0); 9381 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 9382 9383 if (enab->dten_ndesc < enab->dten_maxdesc) { 9384 enab->dten_desc[enab->dten_ndesc++] = ecb; 9385 return; 9386 } 9387 9388 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 9389 9390 if (enab->dten_maxdesc == 0) { 9391 enab->dten_maxdesc = 1; 9392 } else { 9393 enab->dten_maxdesc <<= 1; 9394 } 9395 9396 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 9397 9398 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 9399 ndesc = kmem_zalloc(nsize, KM_SLEEP); 9400 bcopy(enab->dten_desc, ndesc, osize); 9401 kmem_free(enab->dten_desc, osize); 9402 9403 enab->dten_desc = ndesc; 9404 enab->dten_desc[enab->dten_ndesc++] = ecb; 9405 } 9406 9407 static void 9408 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 9409 dtrace_probedesc_t *pd) 9410 { 9411 dtrace_ecbdesc_t *new; 9412 dtrace_predicate_t *pred; 9413 dtrace_actdesc_t *act; 9414 9415 /* 9416 * We're going to create a new ECB description that matches the 9417 * specified ECB in every way, but has the specified probe description. 9418 */ 9419 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 9420 9421 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 9422 dtrace_predicate_hold(pred); 9423 9424 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 9425 dtrace_actdesc_hold(act); 9426 9427 new->dted_action = ecb->dted_action; 9428 new->dted_pred = ecb->dted_pred; 9429 new->dted_probe = *pd; 9430 new->dted_uarg = ecb->dted_uarg; 9431 9432 dtrace_enabling_add(enab, new); 9433 } 9434 9435 static void 9436 dtrace_enabling_dump(dtrace_enabling_t *enab) 9437 { 9438 int i; 9439 9440 for (i = 0; i < enab->dten_ndesc; i++) { 9441 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 9442 9443 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 9444 desc->dtpd_provider, desc->dtpd_mod, 9445 desc->dtpd_func, desc->dtpd_name); 9446 } 9447 } 9448 9449 static void 9450 dtrace_enabling_destroy(dtrace_enabling_t *enab) 9451 { 9452 int i; 9453 dtrace_ecbdesc_t *ep; 9454 dtrace_vstate_t *vstate = enab->dten_vstate; 9455 9456 ASSERT(MUTEX_HELD(&dtrace_lock)); 9457 9458 for (i = 0; i < enab->dten_ndesc; i++) { 9459 dtrace_actdesc_t *act, *next; 9460 dtrace_predicate_t *pred; 9461 9462 ep = enab->dten_desc[i]; 9463 9464 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 9465 dtrace_predicate_release(pred, vstate); 9466 9467 for (act = ep->dted_action; act != NULL; act = next) { 9468 next = act->dtad_next; 9469 dtrace_actdesc_release(act, vstate); 9470 } 9471 9472 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 9473 } 9474 9475 kmem_free(enab->dten_desc, 9476 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 9477 9478 /* 9479 * If this was a retained enabling, decrement the dts_nretained count 9480 * and take it off of the dtrace_retained list. 9481 */ 9482 if (enab->dten_prev != NULL || enab->dten_next != NULL || 9483 dtrace_retained == enab) { 9484 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9485 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 9486 enab->dten_vstate->dtvs_state->dts_nretained--; 9487 } 9488 9489 if (enab->dten_prev == NULL) { 9490 if (dtrace_retained == enab) { 9491 dtrace_retained = enab->dten_next; 9492 9493 if (dtrace_retained != NULL) 9494 dtrace_retained->dten_prev = NULL; 9495 } 9496 } else { 9497 ASSERT(enab != dtrace_retained); 9498 ASSERT(dtrace_retained != NULL); 9499 enab->dten_prev->dten_next = enab->dten_next; 9500 } 9501 9502 if (enab->dten_next != NULL) { 9503 ASSERT(dtrace_retained != NULL); 9504 enab->dten_next->dten_prev = enab->dten_prev; 9505 } 9506 9507 kmem_free(enab, sizeof (dtrace_enabling_t)); 9508 } 9509 9510 static int 9511 dtrace_enabling_retain(dtrace_enabling_t *enab) 9512 { 9513 dtrace_state_t *state; 9514 9515 ASSERT(MUTEX_HELD(&dtrace_lock)); 9516 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 9517 ASSERT(enab->dten_vstate != NULL); 9518 9519 state = enab->dten_vstate->dtvs_state; 9520 ASSERT(state != NULL); 9521 9522 /* 9523 * We only allow each state to retain dtrace_retain_max enablings. 9524 */ 9525 if (state->dts_nretained >= dtrace_retain_max) 9526 return (ENOSPC); 9527 9528 state->dts_nretained++; 9529 9530 if (dtrace_retained == NULL) { 9531 dtrace_retained = enab; 9532 return (0); 9533 } 9534 9535 enab->dten_next = dtrace_retained; 9536 dtrace_retained->dten_prev = enab; 9537 dtrace_retained = enab; 9538 9539 return (0); 9540 } 9541 9542 static int 9543 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 9544 dtrace_probedesc_t *create) 9545 { 9546 dtrace_enabling_t *new, *enab; 9547 int found = 0, err = ENOENT; 9548 9549 ASSERT(MUTEX_HELD(&dtrace_lock)); 9550 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 9551 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 9552 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 9553 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 9554 9555 new = dtrace_enabling_create(&state->dts_vstate); 9556 9557 /* 9558 * Iterate over all retained enablings, looking for enablings that 9559 * match the specified state. 9560 */ 9561 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9562 int i; 9563 9564 /* 9565 * dtvs_state can only be NULL for helper enablings -- and 9566 * helper enablings can't be retained. 9567 */ 9568 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9569 9570 if (enab->dten_vstate->dtvs_state != state) 9571 continue; 9572 9573 /* 9574 * Now iterate over each probe description; we're looking for 9575 * an exact match to the specified probe description. 9576 */ 9577 for (i = 0; i < enab->dten_ndesc; i++) { 9578 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 9579 dtrace_probedesc_t *pd = &ep->dted_probe; 9580 9581 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 9582 continue; 9583 9584 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 9585 continue; 9586 9587 if (strcmp(pd->dtpd_func, match->dtpd_func)) 9588 continue; 9589 9590 if (strcmp(pd->dtpd_name, match->dtpd_name)) 9591 continue; 9592 9593 /* 9594 * We have a winning probe! Add it to our growing 9595 * enabling. 9596 */ 9597 found = 1; 9598 dtrace_enabling_addlike(new, ep, create); 9599 } 9600 } 9601 9602 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 9603 dtrace_enabling_destroy(new); 9604 return (err); 9605 } 9606 9607 return (0); 9608 } 9609 9610 static void 9611 dtrace_enabling_retract(dtrace_state_t *state) 9612 { 9613 dtrace_enabling_t *enab, *next; 9614 9615 ASSERT(MUTEX_HELD(&dtrace_lock)); 9616 9617 /* 9618 * Iterate over all retained enablings, destroy the enablings retained 9619 * for the specified state. 9620 */ 9621 for (enab = dtrace_retained; enab != NULL; enab = next) { 9622 next = enab->dten_next; 9623 9624 /* 9625 * dtvs_state can only be NULL for helper enablings -- and 9626 * helper enablings can't be retained. 9627 */ 9628 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9629 9630 if (enab->dten_vstate->dtvs_state == state) { 9631 ASSERT(state->dts_nretained > 0); 9632 dtrace_enabling_destroy(enab); 9633 } 9634 } 9635 9636 ASSERT(state->dts_nretained == 0); 9637 } 9638 9639 static int 9640 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 9641 { 9642 int i = 0; 9643 int matched = 0; 9644 9645 ASSERT(MUTEX_HELD(&cpu_lock)); 9646 ASSERT(MUTEX_HELD(&dtrace_lock)); 9647 9648 for (i = 0; i < enab->dten_ndesc; i++) { 9649 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 9650 9651 enab->dten_current = ep; 9652 enab->dten_error = 0; 9653 9654 matched += dtrace_probe_enable(&ep->dted_probe, enab); 9655 9656 if (enab->dten_error != 0) { 9657 /* 9658 * If we get an error half-way through enabling the 9659 * probes, we kick out -- perhaps with some number of 9660 * them enabled. Leaving enabled probes enabled may 9661 * be slightly confusing for user-level, but we expect 9662 * that no one will attempt to actually drive on in 9663 * the face of such errors. If this is an anonymous 9664 * enabling (indicated with a NULL nmatched pointer), 9665 * we cmn_err() a message. We aren't expecting to 9666 * get such an error -- such as it can exist at all, 9667 * it would be a result of corrupted DOF in the driver 9668 * properties. 9669 */ 9670 if (nmatched == NULL) { 9671 cmn_err(CE_WARN, "dtrace_enabling_match() " 9672 "error on %p: %d", (void *)ep, 9673 enab->dten_error); 9674 } 9675 9676 return (enab->dten_error); 9677 } 9678 } 9679 9680 enab->dten_probegen = dtrace_probegen; 9681 if (nmatched != NULL) 9682 *nmatched = matched; 9683 9684 return (0); 9685 } 9686 9687 static void 9688 dtrace_enabling_matchall(void) 9689 { 9690 dtrace_enabling_t *enab; 9691 9692 mutex_enter(&cpu_lock); 9693 mutex_enter(&dtrace_lock); 9694 9695 /* 9696 * Because we can be called after dtrace_detach() has been called, we 9697 * cannot assert that there are retained enablings. We can safely 9698 * load from dtrace_retained, however: the taskq_destroy() at the 9699 * end of dtrace_detach() will block pending our completion. 9700 */ 9701 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) 9702 (void) dtrace_enabling_match(enab, NULL); 9703 9704 mutex_exit(&dtrace_lock); 9705 mutex_exit(&cpu_lock); 9706 } 9707 9708 static int 9709 dtrace_enabling_matchstate(dtrace_state_t *state, int *nmatched) 9710 { 9711 dtrace_enabling_t *enab; 9712 int matched, total = 0, err; 9713 9714 ASSERT(MUTEX_HELD(&cpu_lock)); 9715 ASSERT(MUTEX_HELD(&dtrace_lock)); 9716 9717 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9718 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9719 9720 if (enab->dten_vstate->dtvs_state != state) 9721 continue; 9722 9723 if ((err = dtrace_enabling_match(enab, &matched)) != 0) 9724 return (err); 9725 9726 total += matched; 9727 } 9728 9729 if (nmatched != NULL) 9730 *nmatched = total; 9731 9732 return (0); 9733 } 9734 9735 /* 9736 * If an enabling is to be enabled without having matched probes (that is, if 9737 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 9738 * enabling must be _primed_ by creating an ECB for every ECB description. 9739 * This must be done to assure that we know the number of speculations, the 9740 * number of aggregations, the minimum buffer size needed, etc. before we 9741 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 9742 * enabling any probes, we create ECBs for every ECB decription, but with a 9743 * NULL probe -- which is exactly what this function does. 9744 */ 9745 static void 9746 dtrace_enabling_prime(dtrace_state_t *state) 9747 { 9748 dtrace_enabling_t *enab; 9749 int i; 9750 9751 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9752 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9753 9754 if (enab->dten_vstate->dtvs_state != state) 9755 continue; 9756 9757 /* 9758 * We don't want to prime an enabling more than once, lest 9759 * we allow a malicious user to induce resource exhaustion. 9760 * (The ECBs that result from priming an enabling aren't 9761 * leaked -- but they also aren't deallocated until the 9762 * consumer state is destroyed.) 9763 */ 9764 if (enab->dten_primed) 9765 continue; 9766 9767 for (i = 0; i < enab->dten_ndesc; i++) { 9768 enab->dten_current = enab->dten_desc[i]; 9769 (void) dtrace_probe_enable(NULL, enab); 9770 } 9771 9772 enab->dten_primed = 1; 9773 } 9774 } 9775 9776 /* 9777 * Called to indicate that probes should be provided due to retained 9778 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 9779 * must take an initial lap through the enabling calling the dtps_provide() 9780 * entry point explicitly to allow for autocreated probes. 9781 */ 9782 static void 9783 dtrace_enabling_provide(dtrace_provider_t *prv) 9784 { 9785 int i, all = 0; 9786 dtrace_probedesc_t desc; 9787 9788 ASSERT(MUTEX_HELD(&dtrace_lock)); 9789 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9790 9791 if (prv == NULL) { 9792 all = 1; 9793 prv = dtrace_provider; 9794 } 9795 9796 do { 9797 dtrace_enabling_t *enab = dtrace_retained; 9798 void *parg = prv->dtpv_arg; 9799 9800 for (; enab != NULL; enab = enab->dten_next) { 9801 for (i = 0; i < enab->dten_ndesc; i++) { 9802 desc = enab->dten_desc[i]->dted_probe; 9803 mutex_exit(&dtrace_lock); 9804 prv->dtpv_pops.dtps_provide(parg, &desc); 9805 mutex_enter(&dtrace_lock); 9806 } 9807 } 9808 } while (all && (prv = prv->dtpv_next) != NULL); 9809 9810 mutex_exit(&dtrace_lock); 9811 dtrace_probe_provide(NULL, all ? NULL : prv); 9812 mutex_enter(&dtrace_lock); 9813 } 9814 9815 /* 9816 * DTrace DOF Functions 9817 */ 9818 /*ARGSUSED*/ 9819 static void 9820 dtrace_dof_error(dof_hdr_t *dof, const char *str) 9821 { 9822 if (dtrace_err_verbose) 9823 cmn_err(CE_WARN, "failed to process DOF: %s", str); 9824 9825 #ifdef DTRACE_ERRDEBUG 9826 dtrace_errdebug(str); 9827 #endif 9828 } 9829 9830 /* 9831 * Create DOF out of a currently enabled state. Right now, we only create 9832 * DOF containing the run-time options -- but this could be expanded to create 9833 * complete DOF representing the enabled state. 9834 */ 9835 static dof_hdr_t * 9836 dtrace_dof_create(dtrace_state_t *state) 9837 { 9838 dof_hdr_t *dof; 9839 dof_sec_t *sec; 9840 dof_optdesc_t *opt; 9841 int i, len = sizeof (dof_hdr_t) + 9842 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 9843 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 9844 9845 ASSERT(MUTEX_HELD(&dtrace_lock)); 9846 9847 dof = kmem_zalloc(len, KM_SLEEP); 9848 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 9849 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 9850 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 9851 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 9852 9853 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 9854 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 9855 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION_1; 9856 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 9857 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 9858 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 9859 9860 dof->dofh_flags = 0; 9861 dof->dofh_hdrsize = sizeof (dof_hdr_t); 9862 dof->dofh_secsize = sizeof (dof_sec_t); 9863 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 9864 dof->dofh_secoff = sizeof (dof_hdr_t); 9865 dof->dofh_loadsz = len; 9866 dof->dofh_filesz = len; 9867 dof->dofh_pad = 0; 9868 9869 /* 9870 * Fill in the option section header... 9871 */ 9872 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 9873 sec->dofs_type = DOF_SECT_OPTDESC; 9874 sec->dofs_align = sizeof (uint64_t); 9875 sec->dofs_flags = DOF_SECF_LOAD; 9876 sec->dofs_entsize = sizeof (dof_optdesc_t); 9877 9878 opt = (dof_optdesc_t *)((uintptr_t)sec + 9879 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 9880 9881 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 9882 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 9883 9884 for (i = 0; i < DTRACEOPT_MAX; i++) { 9885 opt[i].dofo_option = i; 9886 opt[i].dofo_strtab = DOF_SECIDX_NONE; 9887 opt[i].dofo_value = state->dts_options[i]; 9888 } 9889 9890 return (dof); 9891 } 9892 9893 static dof_hdr_t * 9894 dtrace_dof_copyin(uintptr_t uarg, int *errp) 9895 { 9896 dof_hdr_t hdr, *dof; 9897 9898 ASSERT(!MUTEX_HELD(&dtrace_lock)); 9899 9900 /* 9901 * First, we're going to copyin() the sizeof (dof_hdr_t). 9902 */ 9903 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 9904 dtrace_dof_error(NULL, "failed to copyin DOF header"); 9905 *errp = EFAULT; 9906 return (NULL); 9907 } 9908 9909 /* 9910 * Now we'll allocate the entire DOF and copy it in -- provided 9911 * that the length isn't outrageous. 9912 */ 9913 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 9914 dtrace_dof_error(&hdr, "load size exceeds maximum"); 9915 *errp = E2BIG; 9916 return (NULL); 9917 } 9918 9919 if (hdr.dofh_loadsz < sizeof (hdr)) { 9920 dtrace_dof_error(&hdr, "invalid load size"); 9921 *errp = EINVAL; 9922 return (NULL); 9923 } 9924 9925 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 9926 9927 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) { 9928 kmem_free(dof, hdr.dofh_loadsz); 9929 *errp = EFAULT; 9930 return (NULL); 9931 } 9932 9933 return (dof); 9934 } 9935 9936 static dof_hdr_t * 9937 dtrace_dof_property(const char *name) 9938 { 9939 uchar_t *buf; 9940 uint64_t loadsz; 9941 unsigned int len, i; 9942 dof_hdr_t *dof; 9943 9944 /* 9945 * Unfortunately, array of values in .conf files are always (and 9946 * only) interpreted to be integer arrays. We must read our DOF 9947 * as an integer array, and then squeeze it into a byte array. 9948 */ 9949 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 9950 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 9951 return (NULL); 9952 9953 for (i = 0; i < len; i++) 9954 buf[i] = (uchar_t)(((int *)buf)[i]); 9955 9956 if (len < sizeof (dof_hdr_t)) { 9957 ddi_prop_free(buf); 9958 dtrace_dof_error(NULL, "truncated header"); 9959 return (NULL); 9960 } 9961 9962 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 9963 ddi_prop_free(buf); 9964 dtrace_dof_error(NULL, "truncated DOF"); 9965 return (NULL); 9966 } 9967 9968 if (loadsz >= dtrace_dof_maxsize) { 9969 ddi_prop_free(buf); 9970 dtrace_dof_error(NULL, "oversized DOF"); 9971 return (NULL); 9972 } 9973 9974 dof = kmem_alloc(loadsz, KM_SLEEP); 9975 bcopy(buf, dof, loadsz); 9976 ddi_prop_free(buf); 9977 9978 return (dof); 9979 } 9980 9981 static void 9982 dtrace_dof_destroy(dof_hdr_t *dof) 9983 { 9984 kmem_free(dof, dof->dofh_loadsz); 9985 } 9986 9987 /* 9988 * Return the dof_sec_t pointer corresponding to a given section index. If the 9989 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 9990 * a type other than DOF_SECT_NONE is specified, the header is checked against 9991 * this type and NULL is returned if the types do not match. 9992 */ 9993 static dof_sec_t * 9994 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 9995 { 9996 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 9997 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 9998 9999 if (i >= dof->dofh_secnum) { 10000 dtrace_dof_error(dof, "referenced section index is invalid"); 10001 return (NULL); 10002 } 10003 10004 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 10005 dtrace_dof_error(dof, "referenced section is not loadable"); 10006 return (NULL); 10007 } 10008 10009 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 10010 dtrace_dof_error(dof, "referenced section is the wrong type"); 10011 return (NULL); 10012 } 10013 10014 return (sec); 10015 } 10016 10017 static dtrace_probedesc_t * 10018 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 10019 { 10020 dof_probedesc_t *probe; 10021 dof_sec_t *strtab; 10022 uintptr_t daddr = (uintptr_t)dof; 10023 uintptr_t str; 10024 size_t size; 10025 10026 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 10027 dtrace_dof_error(dof, "invalid probe section"); 10028 return (NULL); 10029 } 10030 10031 if (sec->dofs_align != sizeof (dof_secidx_t)) { 10032 dtrace_dof_error(dof, "bad alignment in probe description"); 10033 return (NULL); 10034 } 10035 10036 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 10037 dtrace_dof_error(dof, "truncated probe description"); 10038 return (NULL); 10039 } 10040 10041 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 10042 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 10043 10044 if (strtab == NULL) 10045 return (NULL); 10046 10047 str = daddr + strtab->dofs_offset; 10048 size = strtab->dofs_size; 10049 10050 if (probe->dofp_provider >= strtab->dofs_size) { 10051 dtrace_dof_error(dof, "corrupt probe provider"); 10052 return (NULL); 10053 } 10054 10055 (void) strncpy(desc->dtpd_provider, 10056 (char *)(str + probe->dofp_provider), 10057 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 10058 10059 if (probe->dofp_mod >= strtab->dofs_size) { 10060 dtrace_dof_error(dof, "corrupt probe module"); 10061 return (NULL); 10062 } 10063 10064 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 10065 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 10066 10067 if (probe->dofp_func >= strtab->dofs_size) { 10068 dtrace_dof_error(dof, "corrupt probe function"); 10069 return (NULL); 10070 } 10071 10072 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 10073 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 10074 10075 if (probe->dofp_name >= strtab->dofs_size) { 10076 dtrace_dof_error(dof, "corrupt probe name"); 10077 return (NULL); 10078 } 10079 10080 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 10081 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 10082 10083 return (desc); 10084 } 10085 10086 static dtrace_difo_t * 10087 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10088 cred_t *cr) 10089 { 10090 dtrace_difo_t *dp; 10091 size_t ttl = 0; 10092 dof_difohdr_t *dofd; 10093 uintptr_t daddr = (uintptr_t)dof; 10094 size_t max = dtrace_difo_maxsize; 10095 int i, l, n; 10096 10097 static const struct { 10098 int section; 10099 int bufoffs; 10100 int lenoffs; 10101 int entsize; 10102 int align; 10103 const char *msg; 10104 } difo[] = { 10105 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 10106 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 10107 sizeof (dif_instr_t), "multiple DIF sections" }, 10108 10109 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 10110 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 10111 sizeof (uint64_t), "multiple integer tables" }, 10112 10113 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 10114 offsetof(dtrace_difo_t, dtdo_strlen), 0, 10115 sizeof (char), "multiple string tables" }, 10116 10117 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 10118 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 10119 sizeof (uint_t), "multiple variable tables" }, 10120 10121 { DOF_SECT_NONE, 0, 0, 0, NULL } 10122 }; 10123 10124 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 10125 dtrace_dof_error(dof, "invalid DIFO header section"); 10126 return (NULL); 10127 } 10128 10129 if (sec->dofs_align != sizeof (dof_secidx_t)) { 10130 dtrace_dof_error(dof, "bad alignment in DIFO header"); 10131 return (NULL); 10132 } 10133 10134 if (sec->dofs_size < sizeof (dof_difohdr_t) || 10135 sec->dofs_size % sizeof (dof_secidx_t)) { 10136 dtrace_dof_error(dof, "bad size in DIFO header"); 10137 return (NULL); 10138 } 10139 10140 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 10141 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 10142 10143 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10144 dp->dtdo_rtype = dofd->dofd_rtype; 10145 10146 for (l = 0; l < n; l++) { 10147 dof_sec_t *subsec; 10148 void **bufp; 10149 uint32_t *lenp; 10150 10151 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 10152 dofd->dofd_links[l])) == NULL) 10153 goto err; /* invalid section link */ 10154 10155 if (ttl + subsec->dofs_size > max) { 10156 dtrace_dof_error(dof, "exceeds maximum size"); 10157 goto err; 10158 } 10159 10160 ttl += subsec->dofs_size; 10161 10162 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 10163 if (subsec->dofs_type != difo[i].section) 10164 continue; 10165 10166 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 10167 dtrace_dof_error(dof, "section not loaded"); 10168 goto err; 10169 } 10170 10171 if (subsec->dofs_align != difo[i].align) { 10172 dtrace_dof_error(dof, "bad alignment"); 10173 goto err; 10174 } 10175 10176 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 10177 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 10178 10179 if (*bufp != NULL) { 10180 dtrace_dof_error(dof, difo[i].msg); 10181 goto err; 10182 } 10183 10184 if (difo[i].entsize != subsec->dofs_entsize) { 10185 dtrace_dof_error(dof, "entry size mismatch"); 10186 goto err; 10187 } 10188 10189 if (subsec->dofs_entsize != 0 && 10190 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 10191 dtrace_dof_error(dof, "corrupt entry size"); 10192 goto err; 10193 } 10194 10195 *lenp = subsec->dofs_size; 10196 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 10197 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 10198 *bufp, subsec->dofs_size); 10199 10200 if (subsec->dofs_entsize != 0) 10201 *lenp /= subsec->dofs_entsize; 10202 10203 break; 10204 } 10205 10206 /* 10207 * If we encounter a loadable DIFO sub-section that is not 10208 * known to us, assume this is a broken program and fail. 10209 */ 10210 if (difo[i].section == DOF_SECT_NONE && 10211 (subsec->dofs_flags & DOF_SECF_LOAD)) { 10212 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 10213 goto err; 10214 } 10215 } 10216 10217 if (dp->dtdo_buf == NULL) { 10218 /* 10219 * We can't have a DIF object without DIF text. 10220 */ 10221 dtrace_dof_error(dof, "missing DIF text"); 10222 goto err; 10223 } 10224 10225 /* 10226 * Before we validate the DIF object, run through the variable table 10227 * looking for the strings -- if any of their size are under, we'll set 10228 * their size to be the system-wide default string size. Note that 10229 * this should _not_ happen if the "strsize" option has been set -- 10230 * in this case, the compiler should have set the size to reflect the 10231 * setting of the option. 10232 */ 10233 for (i = 0; i < dp->dtdo_varlen; i++) { 10234 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10235 dtrace_diftype_t *t = &v->dtdv_type; 10236 10237 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 10238 continue; 10239 10240 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 10241 t->dtdt_size = dtrace_strsize_default; 10242 } 10243 10244 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 10245 goto err; 10246 10247 dtrace_difo_init(dp, vstate); 10248 return (dp); 10249 10250 err: 10251 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10252 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10253 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10254 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10255 10256 kmem_free(dp, sizeof (dtrace_difo_t)); 10257 return (NULL); 10258 } 10259 10260 static dtrace_predicate_t * 10261 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10262 cred_t *cr) 10263 { 10264 dtrace_difo_t *dp; 10265 10266 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 10267 return (NULL); 10268 10269 return (dtrace_predicate_create(dp)); 10270 } 10271 10272 static dtrace_actdesc_t * 10273 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10274 cred_t *cr) 10275 { 10276 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 10277 dof_actdesc_t *desc; 10278 dof_sec_t *difosec; 10279 size_t offs; 10280 uintptr_t daddr = (uintptr_t)dof; 10281 uint64_t arg; 10282 dtrace_actkind_t kind; 10283 10284 if (sec->dofs_type != DOF_SECT_ACTDESC) { 10285 dtrace_dof_error(dof, "invalid action section"); 10286 return (NULL); 10287 } 10288 10289 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 10290 dtrace_dof_error(dof, "truncated action description"); 10291 return (NULL); 10292 } 10293 10294 if (sec->dofs_align != sizeof (uint64_t)) { 10295 dtrace_dof_error(dof, "bad alignment in action description"); 10296 return (NULL); 10297 } 10298 10299 if (sec->dofs_size < sec->dofs_entsize) { 10300 dtrace_dof_error(dof, "section entry size exceeds total size"); 10301 return (NULL); 10302 } 10303 10304 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 10305 dtrace_dof_error(dof, "bad entry size in action description"); 10306 return (NULL); 10307 } 10308 10309 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 10310 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 10311 return (NULL); 10312 } 10313 10314 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 10315 desc = (dof_actdesc_t *)(daddr + 10316 (uintptr_t)sec->dofs_offset + offs); 10317 kind = (dtrace_actkind_t)desc->dofa_kind; 10318 10319 if (DTRACEACT_ISPRINTFLIKE(kind) && 10320 (kind != DTRACEACT_PRINTA || 10321 desc->dofa_strtab != DOF_SECIDX_NONE)) { 10322 dof_sec_t *strtab; 10323 char *str, *fmt; 10324 uint64_t i; 10325 10326 /* 10327 * printf()-like actions must have a format string. 10328 */ 10329 if ((strtab = dtrace_dof_sect(dof, 10330 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 10331 goto err; 10332 10333 str = (char *)((uintptr_t)dof + 10334 (uintptr_t)strtab->dofs_offset); 10335 10336 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 10337 if (str[i] == '\0') 10338 break; 10339 } 10340 10341 if (i >= strtab->dofs_size) { 10342 dtrace_dof_error(dof, "bogus format string"); 10343 goto err; 10344 } 10345 10346 if (i == desc->dofa_arg) { 10347 dtrace_dof_error(dof, "empty format string"); 10348 goto err; 10349 } 10350 10351 i -= desc->dofa_arg; 10352 fmt = kmem_alloc(i + 1, KM_SLEEP); 10353 bcopy(&str[desc->dofa_arg], fmt, i + 1); 10354 arg = (uint64_t)(uintptr_t)fmt; 10355 } else { 10356 if (kind == DTRACEACT_PRINTA) { 10357 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 10358 arg = 0; 10359 } else { 10360 arg = desc->dofa_arg; 10361 } 10362 } 10363 10364 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 10365 desc->dofa_uarg, arg); 10366 10367 if (last != NULL) { 10368 last->dtad_next = act; 10369 } else { 10370 first = act; 10371 } 10372 10373 last = act; 10374 10375 if (desc->dofa_difo == DOF_SECIDX_NONE) 10376 continue; 10377 10378 if ((difosec = dtrace_dof_sect(dof, 10379 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 10380 goto err; 10381 10382 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 10383 10384 if (act->dtad_difo == NULL) 10385 goto err; 10386 } 10387 10388 ASSERT(first != NULL); 10389 return (first); 10390 10391 err: 10392 for (act = first; act != NULL; act = next) { 10393 next = act->dtad_next; 10394 dtrace_actdesc_release(act, vstate); 10395 } 10396 10397 return (NULL); 10398 } 10399 10400 static dtrace_ecbdesc_t * 10401 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10402 cred_t *cr) 10403 { 10404 dtrace_ecbdesc_t *ep; 10405 dof_ecbdesc_t *ecb; 10406 dtrace_probedesc_t *desc; 10407 dtrace_predicate_t *pred = NULL; 10408 10409 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 10410 dtrace_dof_error(dof, "truncated ECB description"); 10411 return (NULL); 10412 } 10413 10414 if (sec->dofs_align != sizeof (uint64_t)) { 10415 dtrace_dof_error(dof, "bad alignment in ECB description"); 10416 return (NULL); 10417 } 10418 10419 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 10420 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 10421 10422 if (sec == NULL) 10423 return (NULL); 10424 10425 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 10426 ep->dted_uarg = ecb->dofe_uarg; 10427 desc = &ep->dted_probe; 10428 10429 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 10430 goto err; 10431 10432 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 10433 if ((sec = dtrace_dof_sect(dof, 10434 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 10435 goto err; 10436 10437 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 10438 goto err; 10439 10440 ep->dted_pred.dtpdd_predicate = pred; 10441 } 10442 10443 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 10444 if ((sec = dtrace_dof_sect(dof, 10445 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 10446 goto err; 10447 10448 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 10449 10450 if (ep->dted_action == NULL) 10451 goto err; 10452 } 10453 10454 return (ep); 10455 10456 err: 10457 if (pred != NULL) 10458 dtrace_predicate_release(pred, vstate); 10459 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 10460 return (NULL); 10461 } 10462 10463 /* 10464 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 10465 * specified DOF. At present, this amounts to simply adding 'ubase' to the 10466 * site of any user SETX relocations to account for load object base address. 10467 * In the future, if we need other relocations, this function can be extended. 10468 */ 10469 static int 10470 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 10471 { 10472 uintptr_t daddr = (uintptr_t)dof; 10473 dof_relohdr_t *dofr = 10474 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 10475 dof_sec_t *ss, *rs, *ts; 10476 dof_relodesc_t *r; 10477 uint_t i, n; 10478 10479 if (sec->dofs_size < sizeof (dof_relohdr_t) || 10480 sec->dofs_align != sizeof (dof_secidx_t)) { 10481 dtrace_dof_error(dof, "invalid relocation header"); 10482 return (-1); 10483 } 10484 10485 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 10486 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 10487 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 10488 10489 if (ss == NULL || rs == NULL || ts == NULL) 10490 return (-1); /* dtrace_dof_error() has been called already */ 10491 10492 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 10493 rs->dofs_align != sizeof (uint64_t)) { 10494 dtrace_dof_error(dof, "invalid relocation section"); 10495 return (-1); 10496 } 10497 10498 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 10499 n = rs->dofs_size / rs->dofs_entsize; 10500 10501 for (i = 0; i < n; i++) { 10502 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 10503 10504 switch (r->dofr_type) { 10505 case DOF_RELO_NONE: 10506 break; 10507 case DOF_RELO_SETX: 10508 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 10509 sizeof (uint64_t) > ts->dofs_size) { 10510 dtrace_dof_error(dof, "bad relocation offset"); 10511 return (-1); 10512 } 10513 10514 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 10515 dtrace_dof_error(dof, "misaligned setx relo"); 10516 return (-1); 10517 } 10518 10519 *(uint64_t *)taddr += ubase; 10520 break; 10521 default: 10522 dtrace_dof_error(dof, "invalid relocation type"); 10523 return (-1); 10524 } 10525 10526 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 10527 } 10528 10529 return (0); 10530 } 10531 10532 /* 10533 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 10534 * header: it should be at the front of a memory region that is at least 10535 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 10536 * size. It need not be validated in any other way. 10537 */ 10538 static int 10539 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 10540 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 10541 { 10542 uint64_t len = dof->dofh_loadsz, seclen; 10543 uintptr_t daddr = (uintptr_t)dof; 10544 dtrace_ecbdesc_t *ep; 10545 dtrace_enabling_t *enab; 10546 uint_t i; 10547 10548 ASSERT(MUTEX_HELD(&dtrace_lock)); 10549 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 10550 10551 /* 10552 * Check the DOF header identification bytes. In addition to checking 10553 * valid settings, we also verify that unused bits/bytes are zeroed so 10554 * we can use them later without fear of regressing existing binaries. 10555 */ 10556 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 10557 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 10558 dtrace_dof_error(dof, "DOF magic string mismatch"); 10559 return (-1); 10560 } 10561 10562 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 10563 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 10564 dtrace_dof_error(dof, "DOF has invalid data model"); 10565 return (-1); 10566 } 10567 10568 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 10569 dtrace_dof_error(dof, "DOF encoding mismatch"); 10570 return (-1); 10571 } 10572 10573 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 10574 dtrace_dof_error(dof, "DOF version mismatch"); 10575 return (-1); 10576 } 10577 10578 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 10579 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 10580 return (-1); 10581 } 10582 10583 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 10584 dtrace_dof_error(dof, "DOF uses too many integer registers"); 10585 return (-1); 10586 } 10587 10588 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 10589 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 10590 return (-1); 10591 } 10592 10593 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 10594 if (dof->dofh_ident[i] != 0) { 10595 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 10596 return (-1); 10597 } 10598 } 10599 10600 if (dof->dofh_flags & ~DOF_FL_VALID) { 10601 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 10602 return (-1); 10603 } 10604 10605 if (dof->dofh_secsize == 0) { 10606 dtrace_dof_error(dof, "zero section header size"); 10607 return (-1); 10608 } 10609 10610 /* 10611 * Check that the section headers don't exceed the amount of DOF 10612 * data. Note that we cast the section size and number of sections 10613 * to uint64_t's to prevent possible overflow in the multiplication. 10614 */ 10615 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 10616 10617 if (dof->dofh_secoff > len || seclen > len || 10618 dof->dofh_secoff + seclen > len) { 10619 dtrace_dof_error(dof, "truncated section headers"); 10620 return (-1); 10621 } 10622 10623 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 10624 dtrace_dof_error(dof, "misaligned section headers"); 10625 return (-1); 10626 } 10627 10628 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 10629 dtrace_dof_error(dof, "misaligned section size"); 10630 return (-1); 10631 } 10632 10633 /* 10634 * Take an initial pass through the section headers to be sure that 10635 * the headers don't have stray offsets. If the 'noprobes' flag is 10636 * set, do not permit sections relating to providers, probes, or args. 10637 */ 10638 for (i = 0; i < dof->dofh_secnum; i++) { 10639 dof_sec_t *sec = (dof_sec_t *)(daddr + 10640 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10641 10642 if (noprobes) { 10643 switch (sec->dofs_type) { 10644 case DOF_SECT_PROVIDER: 10645 case DOF_SECT_PROBES: 10646 case DOF_SECT_PRARGS: 10647 case DOF_SECT_PROFFS: 10648 dtrace_dof_error(dof, "illegal sections " 10649 "for enabling"); 10650 return (-1); 10651 } 10652 } 10653 10654 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 10655 continue; /* just ignore non-loadable sections */ 10656 10657 if (sec->dofs_align & (sec->dofs_align - 1)) { 10658 dtrace_dof_error(dof, "bad section alignment"); 10659 return (-1); 10660 } 10661 10662 if (sec->dofs_offset & (sec->dofs_align - 1)) { 10663 dtrace_dof_error(dof, "misaligned section"); 10664 return (-1); 10665 } 10666 10667 if (sec->dofs_offset > len || sec->dofs_size > len || 10668 sec->dofs_offset + sec->dofs_size > len) { 10669 dtrace_dof_error(dof, "corrupt section header"); 10670 return (-1); 10671 } 10672 10673 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 10674 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 10675 dtrace_dof_error(dof, "non-terminating string table"); 10676 return (-1); 10677 } 10678 } 10679 10680 /* 10681 * Take a second pass through the sections and locate and perform any 10682 * relocations that are present. We do this after the first pass to 10683 * be sure that all sections have had their headers validated. 10684 */ 10685 for (i = 0; i < dof->dofh_secnum; i++) { 10686 dof_sec_t *sec = (dof_sec_t *)(daddr + 10687 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10688 10689 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 10690 continue; /* skip sections that are not loadable */ 10691 10692 switch (sec->dofs_type) { 10693 case DOF_SECT_URELHDR: 10694 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 10695 return (-1); 10696 break; 10697 } 10698 } 10699 10700 if ((enab = *enabp) == NULL) 10701 enab = *enabp = dtrace_enabling_create(vstate); 10702 10703 for (i = 0; i < dof->dofh_secnum; i++) { 10704 dof_sec_t *sec = (dof_sec_t *)(daddr + 10705 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10706 10707 if (sec->dofs_type != DOF_SECT_ECBDESC) 10708 continue; 10709 10710 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 10711 dtrace_enabling_destroy(enab); 10712 *enabp = NULL; 10713 return (-1); 10714 } 10715 10716 dtrace_enabling_add(enab, ep); 10717 } 10718 10719 return (0); 10720 } 10721 10722 /* 10723 * Process DOF for any options. This routine assumes that the DOF has been 10724 * at least processed by dtrace_dof_slurp(). 10725 */ 10726 static int 10727 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 10728 { 10729 int i, rval; 10730 uint32_t entsize; 10731 size_t offs; 10732 dof_optdesc_t *desc; 10733 10734 for (i = 0; i < dof->dofh_secnum; i++) { 10735 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 10736 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10737 10738 if (sec->dofs_type != DOF_SECT_OPTDESC) 10739 continue; 10740 10741 if (sec->dofs_align != sizeof (uint64_t)) { 10742 dtrace_dof_error(dof, "bad alignment in " 10743 "option description"); 10744 return (EINVAL); 10745 } 10746 10747 if ((entsize = sec->dofs_entsize) == 0) { 10748 dtrace_dof_error(dof, "zeroed option entry size"); 10749 return (EINVAL); 10750 } 10751 10752 if (entsize < sizeof (dof_optdesc_t)) { 10753 dtrace_dof_error(dof, "bad option entry size"); 10754 return (EINVAL); 10755 } 10756 10757 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 10758 desc = (dof_optdesc_t *)((uintptr_t)dof + 10759 (uintptr_t)sec->dofs_offset + offs); 10760 10761 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 10762 dtrace_dof_error(dof, "non-zero option string"); 10763 return (EINVAL); 10764 } 10765 10766 if (desc->dofo_value == DTRACEOPT_UNSET) { 10767 dtrace_dof_error(dof, "unset option"); 10768 return (EINVAL); 10769 } 10770 10771 if ((rval = dtrace_state_option(state, 10772 desc->dofo_option, desc->dofo_value)) != 0) { 10773 dtrace_dof_error(dof, "rejected option"); 10774 return (rval); 10775 } 10776 } 10777 } 10778 10779 return (0); 10780 } 10781 10782 /* 10783 * DTrace Consumer State Functions 10784 */ 10785 int 10786 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 10787 { 10788 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 10789 void *base; 10790 uintptr_t limit; 10791 dtrace_dynvar_t *dvar, *next, *start; 10792 int i; 10793 10794 ASSERT(MUTEX_HELD(&dtrace_lock)); 10795 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 10796 10797 bzero(dstate, sizeof (dtrace_dstate_t)); 10798 10799 if ((dstate->dtds_chunksize = chunksize) == 0) 10800 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 10801 10802 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 10803 size = min; 10804 10805 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10806 return (ENOMEM); 10807 10808 dstate->dtds_size = size; 10809 dstate->dtds_base = base; 10810 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 10811 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 10812 10813 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 10814 10815 if (hashsize != 1 && (hashsize & 1)) 10816 hashsize--; 10817 10818 dstate->dtds_hashsize = hashsize; 10819 dstate->dtds_hash = dstate->dtds_base; 10820 10821 /* 10822 * Determine number of active CPUs. Divide free list evenly among 10823 * active CPUs. 10824 */ 10825 start = (dtrace_dynvar_t *) 10826 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 10827 limit = (uintptr_t)base + size; 10828 10829 maxper = (limit - (uintptr_t)start) / NCPU; 10830 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 10831 10832 for (i = 0; i < NCPU; i++) { 10833 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 10834 10835 /* 10836 * If we don't even have enough chunks to make it once through 10837 * NCPUs, we're just going to allocate everything to the first 10838 * CPU. And if we're on the last CPU, we're going to allocate 10839 * whatever is left over. In either case, we set the limit to 10840 * be the limit of the dynamic variable space. 10841 */ 10842 if (maxper == 0 || i == NCPU - 1) { 10843 limit = (uintptr_t)base + size; 10844 start = NULL; 10845 } else { 10846 limit = (uintptr_t)start + maxper; 10847 start = (dtrace_dynvar_t *)limit; 10848 } 10849 10850 ASSERT(limit <= (uintptr_t)base + size); 10851 10852 for (;;) { 10853 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 10854 dstate->dtds_chunksize); 10855 10856 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 10857 break; 10858 10859 dvar->dtdv_next = next; 10860 dvar = next; 10861 } 10862 10863 if (maxper == 0) 10864 break; 10865 } 10866 10867 return (0); 10868 } 10869 10870 void 10871 dtrace_dstate_fini(dtrace_dstate_t *dstate) 10872 { 10873 ASSERT(MUTEX_HELD(&cpu_lock)); 10874 10875 if (dstate->dtds_base == NULL) 10876 return; 10877 10878 kmem_free(dstate->dtds_base, dstate->dtds_size); 10879 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 10880 } 10881 10882 static void 10883 dtrace_vstate_fini(dtrace_vstate_t *vstate) 10884 { 10885 /* 10886 * Logical XOR, where are you? 10887 */ 10888 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 10889 10890 if (vstate->dtvs_nglobals > 0) { 10891 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 10892 sizeof (dtrace_statvar_t *)); 10893 } 10894 10895 if (vstate->dtvs_ntlocals > 0) { 10896 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 10897 sizeof (dtrace_difv_t)); 10898 } 10899 10900 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 10901 10902 if (vstate->dtvs_nlocals > 0) { 10903 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 10904 sizeof (dtrace_statvar_t *)); 10905 } 10906 } 10907 10908 static void 10909 dtrace_state_clean(dtrace_state_t *state) 10910 { 10911 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 10912 return; 10913 10914 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 10915 dtrace_speculation_clean(state); 10916 } 10917 10918 static void 10919 dtrace_state_deadman(dtrace_state_t *state) 10920 { 10921 hrtime_t now; 10922 10923 dtrace_sync(); 10924 10925 now = dtrace_gethrtime(); 10926 10927 if (state != dtrace_anon.dta_state && 10928 now - state->dts_laststatus >= dtrace_deadman_user) 10929 return; 10930 10931 /* 10932 * We must be sure that dts_alive never appears to be less than the 10933 * value upon entry to dtrace_state_deadman(), and because we lack a 10934 * dtrace_cas64(), we cannot store to it atomically. We thus instead 10935 * store INT64_MAX to it, followed by a memory barrier, followed by 10936 * the new value. This assures that dts_alive never appears to be 10937 * less than its true value, regardless of the order in which the 10938 * stores to the underlying storage are issued. 10939 */ 10940 state->dts_alive = INT64_MAX; 10941 dtrace_membar_producer(); 10942 state->dts_alive = now; 10943 } 10944 10945 dtrace_state_t * 10946 dtrace_state_create(dev_t *devp, cred_t *cr) 10947 { 10948 minor_t minor; 10949 major_t major; 10950 char c[30]; 10951 dtrace_state_t *state; 10952 dtrace_optval_t *opt; 10953 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 10954 10955 ASSERT(MUTEX_HELD(&dtrace_lock)); 10956 ASSERT(MUTEX_HELD(&cpu_lock)); 10957 10958 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 10959 VM_BESTFIT | VM_SLEEP); 10960 10961 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 10962 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 10963 return (NULL); 10964 } 10965 10966 state = ddi_get_soft_state(dtrace_softstate, minor); 10967 state->dts_epid = DTRACE_EPIDNONE + 1; 10968 10969 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); 10970 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 10971 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 10972 10973 if (devp != NULL) { 10974 major = getemajor(*devp); 10975 } else { 10976 major = ddi_driver_major(dtrace_devi); 10977 } 10978 10979 state->dts_dev = makedevice(major, minor); 10980 10981 if (devp != NULL) 10982 *devp = state->dts_dev; 10983 10984 /* 10985 * We allocate NCPU buffers. On the one hand, this can be quite 10986 * a bit of memory per instance (nearly 36K on a Starcat). On the 10987 * other hand, it saves an additional memory reference in the probe 10988 * path. 10989 */ 10990 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 10991 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 10992 state->dts_cleaner = CYCLIC_NONE; 10993 state->dts_deadman = CYCLIC_NONE; 10994 state->dts_vstate.dtvs_state = state; 10995 10996 for (i = 0; i < DTRACEOPT_MAX; i++) 10997 state->dts_options[i] = DTRACEOPT_UNSET; 10998 10999 /* 11000 * Set the default options. 11001 */ 11002 opt = state->dts_options; 11003 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 11004 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 11005 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 11006 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 11007 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 11008 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 11009 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 11010 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 11011 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 11012 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 11013 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 11014 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 11015 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 11016 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 11017 11018 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 11019 11020 /* 11021 * Set up the credentials for this instantiation. 11022 */ 11023 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 11024 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 11025 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 11026 } else { 11027 state->dts_cred.dcr_uid = crgetuid(cr); 11028 state->dts_cred.dcr_gid = crgetgid(cr); 11029 11030 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 11031 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 11032 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 11033 } 11034 11035 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) && 11036 PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 11037 state->dts_cred.dcr_visible |= DTRACE_CRV_ALLPROC; 11038 state->dts_cred.dcr_action |= 11039 DTRACE_CRA_PROC_DESTRUCTIVE; 11040 } 11041 11042 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 11043 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 11044 DTRACE_CRV_ALLPROC; 11045 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 11046 DTRACE_CRA_PROC; 11047 11048 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 11049 state->dts_cred.dcr_action |= 11050 DTRACE_CRA_PROC_DESTRUCTIVE; 11051 } 11052 } 11053 11054 return (state); 11055 } 11056 11057 static int 11058 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 11059 { 11060 dtrace_optval_t *opt = state->dts_options, size; 11061 processorid_t cpu; 11062 int flags = 0, rval; 11063 11064 ASSERT(MUTEX_HELD(&dtrace_lock)); 11065 ASSERT(MUTEX_HELD(&cpu_lock)); 11066 ASSERT(which < DTRACEOPT_MAX); 11067 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 11068 (state == dtrace_anon.dta_state && 11069 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 11070 11071 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 11072 return (0); 11073 11074 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 11075 cpu = opt[DTRACEOPT_CPU]; 11076 11077 if (which == DTRACEOPT_SPECSIZE) 11078 flags |= DTRACEBUF_NOSWITCH; 11079 11080 if (which == DTRACEOPT_BUFSIZE) { 11081 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 11082 flags |= DTRACEBUF_RING; 11083 11084 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 11085 flags |= DTRACEBUF_FILL; 11086 11087 flags |= DTRACEBUF_INACTIVE; 11088 } 11089 11090 for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { 11091 /* 11092 * The size must be 8-byte aligned. If the size is not 8-byte 11093 * aligned, drop it down by the difference. 11094 */ 11095 if (size & (sizeof (uint64_t) - 1)) 11096 size -= size & (sizeof (uint64_t) - 1); 11097 11098 if (size < state->dts_reserve) { 11099 /* 11100 * Buffers always must be large enough to accommodate 11101 * their prereserved space. We return E2BIG instead 11102 * of ENOMEM in this case to allow for user-level 11103 * software to differentiate the cases. 11104 */ 11105 return (E2BIG); 11106 } 11107 11108 rval = dtrace_buffer_alloc(buf, size, flags, cpu); 11109 11110 if (rval != ENOMEM) { 11111 opt[which] = size; 11112 return (rval); 11113 } 11114 11115 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 11116 return (rval); 11117 } 11118 11119 return (ENOMEM); 11120 } 11121 11122 static int 11123 dtrace_state_buffers(dtrace_state_t *state) 11124 { 11125 dtrace_speculation_t *spec = state->dts_speculations; 11126 int rval, i; 11127 11128 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 11129 DTRACEOPT_BUFSIZE)) != 0) 11130 return (rval); 11131 11132 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 11133 DTRACEOPT_AGGSIZE)) != 0) 11134 return (rval); 11135 11136 for (i = 0; i < state->dts_nspeculations; i++) { 11137 if ((rval = dtrace_state_buffer(state, 11138 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 11139 return (rval); 11140 } 11141 11142 return (0); 11143 } 11144 11145 static void 11146 dtrace_state_prereserve(dtrace_state_t *state) 11147 { 11148 dtrace_ecb_t *ecb; 11149 dtrace_probe_t *probe; 11150 11151 state->dts_reserve = 0; 11152 11153 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 11154 return; 11155 11156 /* 11157 * If our buffer policy is a "fill" buffer policy, we need to set the 11158 * prereserved space to be the space required by the END probes. 11159 */ 11160 probe = dtrace_probes[dtrace_probeid_end - 1]; 11161 ASSERT(probe != NULL); 11162 11163 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 11164 if (ecb->dte_state != state) 11165 continue; 11166 11167 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 11168 } 11169 } 11170 11171 static int 11172 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 11173 { 11174 dtrace_optval_t *opt = state->dts_options, sz, nspec; 11175 dtrace_speculation_t *spec; 11176 dtrace_buffer_t *buf; 11177 cyc_handler_t hdlr; 11178 cyc_time_t when; 11179 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 11180 dtrace_icookie_t cookie; 11181 11182 mutex_enter(&cpu_lock); 11183 mutex_enter(&dtrace_lock); 11184 11185 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 11186 rval = EBUSY; 11187 goto out; 11188 } 11189 11190 /* 11191 * Before we can perform any checks, we must prime all of the 11192 * retained enablings that correspond to this state. 11193 */ 11194 dtrace_enabling_prime(state); 11195 11196 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 11197 rval = EACCES; 11198 goto out; 11199 } 11200 11201 dtrace_state_prereserve(state); 11202 11203 /* 11204 * Now we want to do is try to allocate our speculations. 11205 * We do not automatically resize the number of speculations; if 11206 * this fails, we will fail the operation. 11207 */ 11208 nspec = opt[DTRACEOPT_NSPEC]; 11209 ASSERT(nspec != DTRACEOPT_UNSET); 11210 11211 if (nspec > INT_MAX) { 11212 rval = ENOMEM; 11213 goto out; 11214 } 11215 11216 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); 11217 11218 if (spec == NULL) { 11219 rval = ENOMEM; 11220 goto out; 11221 } 11222 11223 state->dts_speculations = spec; 11224 state->dts_nspeculations = (int)nspec; 11225 11226 for (i = 0; i < nspec; i++) { 11227 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { 11228 rval = ENOMEM; 11229 goto err; 11230 } 11231 11232 spec[i].dtsp_buffer = buf; 11233 } 11234 11235 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 11236 if (dtrace_anon.dta_state == NULL) { 11237 rval = ENOENT; 11238 goto out; 11239 } 11240 11241 if (state->dts_necbs != 0) { 11242 rval = EALREADY; 11243 goto out; 11244 } 11245 11246 state->dts_anon = dtrace_anon_grab(); 11247 ASSERT(state->dts_anon != NULL); 11248 11249 *cpu = dtrace_anon.dta_beganon; 11250 11251 /* 11252 * If the anonymous state is active (as it almost certainly 11253 * is if the anonymous enabling ultimately matched anything), 11254 * we don't allow any further option processing -- but we 11255 * don't return failure. 11256 */ 11257 state = state->dts_anon; 11258 11259 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11260 goto out; 11261 } 11262 11263 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 11264 opt[DTRACEOPT_AGGSIZE] != 0) { 11265 if (state->dts_aggregations == NULL) { 11266 /* 11267 * We're not going to create an aggregation buffer 11268 * because we don't have any ECBs that contain 11269 * aggregations -- set this option to 0. 11270 */ 11271 opt[DTRACEOPT_AGGSIZE] = 0; 11272 } else { 11273 /* 11274 * If we have an aggregation buffer, we must also have 11275 * a buffer to use as scratch. 11276 */ 11277 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 11278 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 11279 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 11280 } 11281 } 11282 } 11283 11284 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 11285 opt[DTRACEOPT_SPECSIZE] != 0) { 11286 if (!state->dts_speculates) { 11287 /* 11288 * We're not going to create speculation buffers 11289 * because we don't have any ECBs that actually 11290 * speculate -- set the speculation size to 0. 11291 */ 11292 opt[DTRACEOPT_SPECSIZE] = 0; 11293 } 11294 } 11295 11296 /* 11297 * The bare minimum size for any buffer that we're actually going to 11298 * do anything to is sizeof (uint64_t). 11299 */ 11300 sz = sizeof (uint64_t); 11301 11302 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 11303 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 11304 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 11305 /* 11306 * A buffer size has been explicitly set to 0 (or to a size 11307 * that will be adjusted to 0) and we need the space -- we 11308 * need to return failure. We return ENOSPC to differentiate 11309 * it from failing to allocate a buffer due to failure to meet 11310 * the reserve (for which we return E2BIG). 11311 */ 11312 rval = ENOSPC; 11313 goto out; 11314 } 11315 11316 if ((rval = dtrace_state_buffers(state)) != 0) 11317 goto err; 11318 11319 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 11320 sz = dtrace_dstate_defsize; 11321 11322 do { 11323 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 11324 11325 if (rval == 0) 11326 break; 11327 11328 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 11329 goto err; 11330 } while (sz >>= 1); 11331 11332 opt[DTRACEOPT_DYNVARSIZE] = sz; 11333 11334 if (rval != 0) 11335 goto err; 11336 11337 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 11338 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 11339 11340 if (opt[DTRACEOPT_CLEANRATE] == 0) 11341 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 11342 11343 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 11344 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 11345 11346 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 11347 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 11348 11349 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 11350 hdlr.cyh_arg = state; 11351 hdlr.cyh_level = CY_LOW_LEVEL; 11352 11353 when.cyt_when = 0; 11354 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 11355 11356 state->dts_cleaner = cyclic_add(&hdlr, &when); 11357 11358 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 11359 hdlr.cyh_arg = state; 11360 hdlr.cyh_level = CY_LOW_LEVEL; 11361 11362 when.cyt_when = 0; 11363 when.cyt_interval = dtrace_deadman_interval; 11364 11365 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 11366 state->dts_deadman = cyclic_add(&hdlr, &when); 11367 11368 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 11369 11370 /* 11371 * Now it's time to actually fire the BEGIN probe. We need to disable 11372 * interrupts here both to record the CPU on which we fired the BEGIN 11373 * probe (the data from this CPU will be processed first at user 11374 * level) and to manually activate the buffer for this CPU. 11375 */ 11376 cookie = dtrace_interrupt_disable(); 11377 *cpu = CPU->cpu_id; 11378 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 11379 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 11380 11381 dtrace_probe(dtrace_probeid_begin, 11382 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 11383 dtrace_interrupt_enable(cookie); 11384 /* 11385 * We may have had an exit action from a BEGIN probe; only change our 11386 * state to ACTIVE if we're still in WARMUP. 11387 */ 11388 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 11389 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 11390 11391 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 11392 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 11393 11394 /* 11395 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 11396 * want each CPU to transition its principal buffer out of the 11397 * INACTIVE state. Doing this assures that no CPU will suddenly begin 11398 * processing an ECB halfway down a probe's ECB chain; all CPUs will 11399 * atomically transition from processing none of a state's ECBs to 11400 * processing all of them. 11401 */ 11402 dtrace_xcall(DTRACE_CPUALL, 11403 (dtrace_xcall_t)dtrace_buffer_activate, state); 11404 goto out; 11405 11406 err: 11407 dtrace_buffer_free(state->dts_buffer); 11408 dtrace_buffer_free(state->dts_aggbuffer); 11409 11410 if ((nspec = state->dts_nspeculations) == 0) { 11411 ASSERT(state->dts_speculations == NULL); 11412 goto out; 11413 } 11414 11415 spec = state->dts_speculations; 11416 ASSERT(spec != NULL); 11417 11418 for (i = 0; i < state->dts_nspeculations; i++) { 11419 if ((buf = spec[i].dtsp_buffer) == NULL) 11420 break; 11421 11422 dtrace_buffer_free(buf); 11423 kmem_free(buf, bufsize); 11424 } 11425 11426 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 11427 state->dts_nspeculations = 0; 11428 state->dts_speculations = NULL; 11429 11430 out: 11431 mutex_exit(&dtrace_lock); 11432 mutex_exit(&cpu_lock); 11433 11434 return (rval); 11435 } 11436 11437 static int 11438 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 11439 { 11440 dtrace_icookie_t cookie; 11441 11442 ASSERT(MUTEX_HELD(&dtrace_lock)); 11443 11444 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 11445 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 11446 return (EINVAL); 11447 11448 /* 11449 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 11450 * to be sure that every CPU has seen it. See below for the details 11451 * on why this is done. 11452 */ 11453 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 11454 dtrace_sync(); 11455 11456 /* 11457 * By this point, it is impossible for any CPU to be still processing 11458 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 11459 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 11460 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 11461 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 11462 * iff we're in the END probe. 11463 */ 11464 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 11465 dtrace_sync(); 11466 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 11467 11468 /* 11469 * Finally, we can release the reserve and call the END probe. We 11470 * disable interrupts across calling the END probe to allow us to 11471 * return the CPU on which we actually called the END probe. This 11472 * allows user-land to be sure that this CPU's principal buffer is 11473 * processed last. 11474 */ 11475 state->dts_reserve = 0; 11476 11477 cookie = dtrace_interrupt_disable(); 11478 *cpu = CPU->cpu_id; 11479 dtrace_probe(dtrace_probeid_end, 11480 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 11481 dtrace_interrupt_enable(cookie); 11482 11483 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 11484 dtrace_sync(); 11485 11486 return (0); 11487 } 11488 11489 static int 11490 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 11491 dtrace_optval_t val) 11492 { 11493 ASSERT(MUTEX_HELD(&dtrace_lock)); 11494 11495 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11496 return (EBUSY); 11497 11498 if (option >= DTRACEOPT_MAX) 11499 return (EINVAL); 11500 11501 if (option != DTRACEOPT_CPU && val < 0) 11502 return (EINVAL); 11503 11504 switch (option) { 11505 case DTRACEOPT_DESTRUCTIVE: 11506 if (dtrace_destructive_disallow) 11507 return (EACCES); 11508 11509 state->dts_cred.dcr_destructive = 1; 11510 break; 11511 11512 case DTRACEOPT_BUFSIZE: 11513 case DTRACEOPT_DYNVARSIZE: 11514 case DTRACEOPT_AGGSIZE: 11515 case DTRACEOPT_SPECSIZE: 11516 case DTRACEOPT_STRSIZE: 11517 if (val < 0) 11518 return (EINVAL); 11519 11520 if (val >= LONG_MAX) { 11521 /* 11522 * If this is an otherwise negative value, set it to 11523 * the highest multiple of 128m less than LONG_MAX. 11524 * Technically, we're adjusting the size without 11525 * regard to the buffer resizing policy, but in fact, 11526 * this has no effect -- if we set the buffer size to 11527 * ~LONG_MAX and the buffer policy is ultimately set to 11528 * be "manual", the buffer allocation is guaranteed to 11529 * fail, if only because the allocation requires two 11530 * buffers. (We set the the size to the highest 11531 * multiple of 128m because it ensures that the size 11532 * will remain a multiple of a megabyte when 11533 * repeatedly halved -- all the way down to 15m.) 11534 */ 11535 val = LONG_MAX - (1 << 27) + 1; 11536 } 11537 } 11538 11539 state->dts_options[option] = val; 11540 11541 return (0); 11542 } 11543 11544 static void 11545 dtrace_state_destroy(dtrace_state_t *state) 11546 { 11547 dtrace_ecb_t *ecb; 11548 dtrace_vstate_t *vstate = &state->dts_vstate; 11549 minor_t minor = getminor(state->dts_dev); 11550 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 11551 dtrace_speculation_t *spec = state->dts_speculations; 11552 int nspec = state->dts_nspeculations; 11553 uint32_t match; 11554 11555 ASSERT(MUTEX_HELD(&dtrace_lock)); 11556 ASSERT(MUTEX_HELD(&cpu_lock)); 11557 11558 /* 11559 * First, retract any retained enablings for this state. 11560 */ 11561 dtrace_enabling_retract(state); 11562 ASSERT(state->dts_nretained == 0); 11563 11564 /* 11565 * Now we need to disable and destroy any enabled probes. Because any 11566 * DTRACE_PRIV_KERNEL probes may actually be slowing our progress 11567 * (especially if they're all enabled), we take two passes through 11568 * the ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, 11569 * and in the second we disable whatever is left over. 11570 */ 11571 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 11572 for (i = 0; i < state->dts_necbs; i++) { 11573 if ((ecb = state->dts_ecbs[i]) == NULL) 11574 continue; 11575 11576 if (match && ecb->dte_probe != NULL) { 11577 dtrace_probe_t *probe = ecb->dte_probe; 11578 dtrace_provider_t *prov = probe->dtpr_provider; 11579 11580 if (!(prov->dtpv_priv.dtpp_flags & match)) 11581 continue; 11582 } 11583 11584 dtrace_ecb_disable(ecb); 11585 dtrace_ecb_destroy(ecb); 11586 } 11587 11588 if (!match) 11589 break; 11590 } 11591 11592 /* 11593 * Before we free the buffers, perform one more sync to assure that 11594 * every CPU is out of probe context. 11595 */ 11596 dtrace_sync(); 11597 11598 dtrace_buffer_free(state->dts_buffer); 11599 dtrace_buffer_free(state->dts_aggbuffer); 11600 11601 for (i = 0; i < nspec; i++) 11602 dtrace_buffer_free(spec[i].dtsp_buffer); 11603 11604 if (state->dts_cleaner != CYCLIC_NONE) 11605 cyclic_remove(state->dts_cleaner); 11606 11607 if (state->dts_deadman != CYCLIC_NONE) 11608 cyclic_remove(state->dts_deadman); 11609 11610 dtrace_dstate_fini(&vstate->dtvs_dynvars); 11611 dtrace_vstate_fini(vstate); 11612 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 11613 11614 if (state->dts_aggregations != NULL) { 11615 #ifdef DEBUG 11616 for (i = 0; i < state->dts_naggregations; i++) 11617 ASSERT(state->dts_aggregations[i] == NULL); 11618 #endif 11619 ASSERT(state->dts_naggregations > 0); 11620 kmem_free(state->dts_aggregations, 11621 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 11622 } 11623 11624 kmem_free(state->dts_buffer, bufsize); 11625 kmem_free(state->dts_aggbuffer, bufsize); 11626 11627 for (i = 0; i < nspec; i++) 11628 kmem_free(spec[i].dtsp_buffer, bufsize); 11629 11630 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 11631 11632 dtrace_format_destroy(state); 11633 11634 vmem_destroy(state->dts_aggid_arena); 11635 ddi_soft_state_free(dtrace_softstate, minor); 11636 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 11637 } 11638 11639 /* 11640 * DTrace Anonymous Enabling Functions 11641 */ 11642 static dtrace_state_t * 11643 dtrace_anon_grab(void) 11644 { 11645 dtrace_state_t *state; 11646 11647 ASSERT(MUTEX_HELD(&dtrace_lock)); 11648 11649 if ((state = dtrace_anon.dta_state) == NULL) { 11650 ASSERT(dtrace_anon.dta_enabling == NULL); 11651 return (NULL); 11652 } 11653 11654 ASSERT(dtrace_anon.dta_enabling != NULL); 11655 ASSERT(dtrace_retained != NULL); 11656 11657 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 11658 dtrace_anon.dta_enabling = NULL; 11659 dtrace_anon.dta_state = NULL; 11660 11661 return (state); 11662 } 11663 11664 static void 11665 dtrace_anon_property(void) 11666 { 11667 int i, rv; 11668 dtrace_state_t *state; 11669 dof_hdr_t *dof; 11670 char c[32]; /* enough for "dof-data-" + digits */ 11671 11672 ASSERT(MUTEX_HELD(&dtrace_lock)); 11673 ASSERT(MUTEX_HELD(&cpu_lock)); 11674 11675 for (i = 0; ; i++) { 11676 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 11677 11678 dtrace_err_verbose = 1; 11679 11680 if ((dof = dtrace_dof_property(c)) == NULL) { 11681 dtrace_err_verbose = 0; 11682 break; 11683 } 11684 11685 /* 11686 * We want to create anonymous state, so we need to transition 11687 * the kernel debugger to indicate that DTrace is active. If 11688 * this fails (e.g. because the debugger has modified text in 11689 * some way), we won't continue with the processing. 11690 */ 11691 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 11692 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 11693 "enabling ignored."); 11694 dtrace_dof_destroy(dof); 11695 break; 11696 } 11697 11698 /* 11699 * If we haven't allocated an anonymous state, we'll do so now. 11700 */ 11701 if ((state = dtrace_anon.dta_state) == NULL) { 11702 state = dtrace_state_create(NULL, NULL); 11703 dtrace_anon.dta_state = state; 11704 11705 if (state == NULL) { 11706 /* 11707 * This basically shouldn't happen: the only 11708 * failure mode from dtrace_state_create() is a 11709 * failure of ddi_soft_state_zalloc() that 11710 * itself should never happen. Still, the 11711 * interface allows for a failure mode, and 11712 * we want to fail as gracefully as possible: 11713 * we'll emit an error message and cease 11714 * processing anonymous state in this case. 11715 */ 11716 cmn_err(CE_WARN, "failed to create " 11717 "anonymous state"); 11718 dtrace_dof_destroy(dof); 11719 break; 11720 } 11721 } 11722 11723 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 11724 &dtrace_anon.dta_enabling, 0, B_TRUE); 11725 11726 if (rv == 0) 11727 rv = dtrace_dof_options(dof, state); 11728 11729 dtrace_err_verbose = 0; 11730 dtrace_dof_destroy(dof); 11731 11732 if (rv != 0) { 11733 /* 11734 * This is malformed DOF; chuck any anonymous state 11735 * that we created. 11736 */ 11737 ASSERT(dtrace_anon.dta_enabling == NULL); 11738 dtrace_state_destroy(state); 11739 dtrace_anon.dta_state = NULL; 11740 break; 11741 } 11742 11743 ASSERT(dtrace_anon.dta_enabling != NULL); 11744 } 11745 11746 if (dtrace_anon.dta_enabling != NULL) { 11747 int rval; 11748 11749 /* 11750 * dtrace_enabling_retain() can only fail because we are 11751 * trying to retain more enablings than are allowed -- but 11752 * we only have one anonymous enabling, and we are guaranteed 11753 * to be allowed at least one retained enabling; we assert 11754 * that dtrace_enabling_retain() returns success. 11755 */ 11756 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 11757 ASSERT(rval == 0); 11758 11759 dtrace_enabling_dump(dtrace_anon.dta_enabling); 11760 } 11761 } 11762 11763 /* 11764 * DTrace Helper Functions 11765 */ 11766 static void 11767 dtrace_helper_trace(dtrace_helper_action_t *helper, 11768 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 11769 { 11770 uint32_t size, next, nnext, i; 11771 dtrace_helptrace_t *ent; 11772 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 11773 11774 if (!dtrace_helptrace_enabled) 11775 return; 11776 11777 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 11778 11779 /* 11780 * What would a tracing framework be without its own tracing 11781 * framework? (Well, a hell of a lot simpler, for starters...) 11782 */ 11783 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 11784 sizeof (uint64_t) - sizeof (uint64_t); 11785 11786 /* 11787 * Iterate until we can allocate a slot in the trace buffer. 11788 */ 11789 do { 11790 next = dtrace_helptrace_next; 11791 11792 if (next + size < dtrace_helptrace_bufsize) { 11793 nnext = next + size; 11794 } else { 11795 nnext = size; 11796 } 11797 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 11798 11799 /* 11800 * We have our slot; fill it in. 11801 */ 11802 if (nnext == size) 11803 next = 0; 11804 11805 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 11806 ent->dtht_helper = helper; 11807 ent->dtht_where = where; 11808 ent->dtht_nlocals = vstate->dtvs_nlocals; 11809 11810 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 11811 mstate->dtms_fltoffs : -1; 11812 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 11813 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 11814 11815 for (i = 0; i < vstate->dtvs_nlocals; i++) { 11816 dtrace_statvar_t *svar; 11817 11818 if ((svar = vstate->dtvs_locals[i]) == NULL) 11819 continue; 11820 11821 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 11822 ent->dtht_locals[i] = 11823 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; 11824 } 11825 } 11826 11827 static uint64_t 11828 dtrace_helper(int which, dtrace_mstate_t *mstate, 11829 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 11830 { 11831 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 11832 uint64_t sarg0 = mstate->dtms_arg[0]; 11833 uint64_t sarg1 = mstate->dtms_arg[1]; 11834 uint64_t rval; 11835 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 11836 dtrace_helper_action_t *helper; 11837 dtrace_vstate_t *vstate; 11838 dtrace_difo_t *pred; 11839 int i, trace = dtrace_helptrace_enabled; 11840 11841 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 11842 11843 if (helpers == NULL) 11844 return (0); 11845 11846 if ((helper = helpers->dthps_actions[which]) == NULL) 11847 return (0); 11848 11849 vstate = &helpers->dthps_vstate; 11850 mstate->dtms_arg[0] = arg0; 11851 mstate->dtms_arg[1] = arg1; 11852 11853 /* 11854 * Now iterate over each helper. If its predicate evaluates to 'true', 11855 * we'll call the corresponding actions. Note that the below calls 11856 * to dtrace_dif_emulate() may set faults in machine state. This is 11857 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 11858 * the stored DIF offset with its own (which is the desired behavior). 11859 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 11860 * from machine state; this is okay, too. 11861 */ 11862 for (; helper != NULL; helper = helper->dthp_next) { 11863 if ((pred = helper->dthp_predicate) != NULL) { 11864 if (trace) 11865 dtrace_helper_trace(helper, mstate, vstate, 0); 11866 11867 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 11868 goto next; 11869 11870 if (*flags & CPU_DTRACE_FAULT) 11871 goto err; 11872 } 11873 11874 for (i = 0; i < helper->dthp_nactions; i++) { 11875 if (trace) 11876 dtrace_helper_trace(helper, 11877 mstate, vstate, i + 1); 11878 11879 rval = dtrace_dif_emulate(helper->dthp_actions[i], 11880 mstate, vstate, state); 11881 11882 if (*flags & CPU_DTRACE_FAULT) 11883 goto err; 11884 } 11885 11886 next: 11887 if (trace) 11888 dtrace_helper_trace(helper, mstate, vstate, 11889 DTRACE_HELPTRACE_NEXT); 11890 } 11891 11892 if (trace) 11893 dtrace_helper_trace(helper, mstate, vstate, 11894 DTRACE_HELPTRACE_DONE); 11895 11896 /* 11897 * Restore the arg0 that we saved upon entry. 11898 */ 11899 mstate->dtms_arg[0] = sarg0; 11900 mstate->dtms_arg[1] = sarg1; 11901 11902 return (rval); 11903 11904 err: 11905 if (trace) 11906 dtrace_helper_trace(helper, mstate, vstate, 11907 DTRACE_HELPTRACE_ERR); 11908 11909 /* 11910 * Restore the arg0 that we saved upon entry. 11911 */ 11912 mstate->dtms_arg[0] = sarg0; 11913 mstate->dtms_arg[1] = sarg1; 11914 11915 return (NULL); 11916 } 11917 11918 static void 11919 dtrace_helper_destroy(dtrace_helper_action_t *helper, dtrace_vstate_t *vstate) 11920 { 11921 int i; 11922 11923 if (helper->dthp_predicate != NULL) 11924 dtrace_difo_release(helper->dthp_predicate, vstate); 11925 11926 for (i = 0; i < helper->dthp_nactions; i++) { 11927 ASSERT(helper->dthp_actions[i] != NULL); 11928 dtrace_difo_release(helper->dthp_actions[i], vstate); 11929 } 11930 11931 kmem_free(helper->dthp_actions, 11932 helper->dthp_nactions * sizeof (dtrace_difo_t *)); 11933 kmem_free(helper, sizeof (dtrace_helper_action_t)); 11934 } 11935 11936 static int 11937 dtrace_helper_destroygen(int gen) 11938 { 11939 dtrace_helpers_t *help = curproc->p_dtrace_helpers; 11940 dtrace_vstate_t *vstate; 11941 int i; 11942 11943 ASSERT(MUTEX_HELD(&dtrace_lock)); 11944 11945 if (help == NULL || gen > help->dthps_generation) 11946 return (EINVAL); 11947 11948 vstate = &help->dthps_vstate; 11949 11950 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 11951 dtrace_helper_action_t *last = NULL, *h, *next; 11952 11953 for (h = help->dthps_actions[i]; h != NULL; h = next) { 11954 next = h->dthp_next; 11955 11956 if (h->dthp_generation == gen) { 11957 if (last != NULL) { 11958 last->dthp_next = next; 11959 } else { 11960 help->dthps_actions[i] = next; 11961 } 11962 11963 dtrace_helper_destroy(h, vstate); 11964 } else { 11965 last = h; 11966 } 11967 } 11968 } 11969 11970 return (0); 11971 } 11972 11973 static int 11974 dtrace_helper_validate(dtrace_helper_action_t *helper) 11975 { 11976 int err = 0, i; 11977 dtrace_difo_t *dp; 11978 11979 if ((dp = helper->dthp_predicate) != NULL) 11980 err += dtrace_difo_validate_helper(dp); 11981 11982 for (i = 0; i < helper->dthp_nactions; i++) 11983 err += dtrace_difo_validate_helper(helper->dthp_actions[i]); 11984 11985 return (err == 0); 11986 } 11987 11988 static int 11989 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 11990 { 11991 dtrace_helpers_t *help; 11992 dtrace_helper_action_t *helper, *last; 11993 dtrace_actdesc_t *act; 11994 dtrace_vstate_t *vstate; 11995 dtrace_predicate_t *pred; 11996 int count = 0, nactions = 0, i; 11997 11998 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 11999 return (EINVAL); 12000 12001 help = curproc->p_dtrace_helpers; 12002 last = help->dthps_actions[which]; 12003 vstate = &help->dthps_vstate; 12004 12005 for (count = 0; last != NULL; last = last->dthp_next) { 12006 count++; 12007 if (last->dthp_next == NULL) 12008 break; 12009 } 12010 12011 /* 12012 * If we already have dtrace_helper_actions_max helper actions for this 12013 * helper action type, we'll refuse to add a new one. 12014 */ 12015 if (count >= dtrace_helper_actions_max) 12016 return (ENOSPC); 12017 12018 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 12019 helper->dthp_generation = help->dthps_generation; 12020 12021 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 12022 ASSERT(pred->dtp_difo != NULL); 12023 dtrace_difo_hold(pred->dtp_difo); 12024 helper->dthp_predicate = pred->dtp_difo; 12025 } 12026 12027 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 12028 if (act->dtad_kind != DTRACEACT_DIFEXPR) 12029 goto err; 12030 12031 if (act->dtad_difo == NULL) 12032 goto err; 12033 12034 nactions++; 12035 } 12036 12037 helper->dthp_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 12038 (helper->dthp_nactions = nactions), KM_SLEEP); 12039 12040 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 12041 dtrace_difo_hold(act->dtad_difo); 12042 helper->dthp_actions[i++] = act->dtad_difo; 12043 } 12044 12045 if (!dtrace_helper_validate(helper)) 12046 goto err; 12047 12048 if (last == NULL) { 12049 help->dthps_actions[which] = helper; 12050 } else { 12051 last->dthp_next = helper; 12052 } 12053 12054 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 12055 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 12056 dtrace_helptrace_next = 0; 12057 } 12058 12059 return (0); 12060 err: 12061 dtrace_helper_destroy(helper, vstate); 12062 return (EINVAL); 12063 } 12064 12065 static void 12066 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 12067 dof_helper_t *dofhp) 12068 { 12069 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 12070 12071 mutex_enter(&dtrace_meta_lock); 12072 mutex_enter(&dtrace_lock); 12073 12074 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 12075 /* 12076 * If the dtrace module is loaded but not attached, or if 12077 * there aren't isn't a meta provider registered to deal with 12078 * these provider descriptions, we need to postpone creating 12079 * the actual providers until later. 12080 */ 12081 12082 if (help->dthps_next == NULL && help->dthps_prev == NULL && 12083 dtrace_deferred_pid != help) { 12084 help->dthps_deferred = 1; 12085 help->dthps_pid = p->p_pid; 12086 help->dthps_next = dtrace_deferred_pid; 12087 help->dthps_prev = NULL; 12088 if (dtrace_deferred_pid != NULL) 12089 dtrace_deferred_pid->dthps_prev = help; 12090 dtrace_deferred_pid = help; 12091 } 12092 12093 mutex_exit(&dtrace_lock); 12094 12095 } else if (dofhp != NULL) { 12096 /* 12097 * If the dtrace module is loaded and we have a particular 12098 * helper provider description, pass that off to the 12099 * meta provider. 12100 */ 12101 12102 mutex_exit(&dtrace_lock); 12103 12104 dtrace_helper_provide(dofhp, p->p_pid); 12105 12106 } else { 12107 /* 12108 * Otherwise, just pass all the helper provider descriptions 12109 * off to the meta provider. 12110 */ 12111 12112 int i; 12113 mutex_exit(&dtrace_lock); 12114 12115 for (i = 0; i < help->dthps_nprovs; i++) { 12116 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 12117 p->p_pid); 12118 } 12119 } 12120 12121 mutex_exit(&dtrace_meta_lock); 12122 } 12123 12124 static int 12125 dtrace_helper_provider_add(dof_helper_t *dofhp) 12126 { 12127 dtrace_helpers_t *help; 12128 dtrace_helper_provider_t *hprov, **tmp_provs; 12129 uint_t tmp_nprovs, i; 12130 12131 help = curproc->p_dtrace_helpers; 12132 ASSERT(help != NULL); 12133 12134 /* 12135 * If we already have dtrace_helper_providers_max helper providers, 12136 * we're refuse to add a new one. 12137 */ 12138 if (help->dthps_nprovs >= dtrace_helper_providers_max) 12139 return (ENOSPC); 12140 12141 /* 12142 * Check to make sure this isn't a duplicate. 12143 */ 12144 for (i = 0; i < help->dthps_nprovs; i++) { 12145 if (dofhp->dofhp_addr == 12146 help->dthps_provs[i]->dthp_prov.dofhp_addr) 12147 return (EALREADY); 12148 } 12149 12150 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 12151 hprov->dthp_prov = *dofhp; 12152 hprov->dthp_ref = 1; 12153 12154 tmp_nprovs = help->dthps_nprovs; 12155 tmp_provs = help->dthps_provs; 12156 help->dthps_nprovs++; 12157 help->dthps_provs = kmem_zalloc(help->dthps_nprovs * 12158 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 12159 12160 help->dthps_provs[tmp_nprovs] = hprov; 12161 if (tmp_provs != NULL) { 12162 bcopy(tmp_provs, help->dthps_provs, tmp_nprovs * 12163 sizeof (dtrace_helper_provider_t *)); 12164 kmem_free(tmp_provs, tmp_nprovs * 12165 sizeof (dtrace_helper_provider_t *)); 12166 } 12167 12168 return (0); 12169 } 12170 12171 static void 12172 dtrace_helper_provider_remove(dtrace_helper_provider_t *hprov) 12173 { 12174 mutex_enter(&dtrace_lock); 12175 12176 if (--hprov->dthp_ref == 0) { 12177 dof_hdr_t *dof; 12178 mutex_exit(&dtrace_lock); 12179 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 12180 dtrace_dof_destroy(dof); 12181 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 12182 } else { 12183 mutex_exit(&dtrace_lock); 12184 } 12185 } 12186 12187 static int 12188 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 12189 { 12190 uintptr_t daddr = (uintptr_t)dof; 12191 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec; 12192 dof_provider_t *provider; 12193 dof_probe_t *probe; 12194 uint8_t *arg; 12195 char *strtab, *typestr; 12196 dof_stridx_t typeidx; 12197 size_t typesz; 12198 uint_t nprobes, j, k; 12199 12200 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 12201 12202 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 12203 dtrace_dof_error(dof, "misaligned section offset"); 12204 return (-1); 12205 } 12206 12207 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 12208 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 12209 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 12210 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 12211 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 12212 12213 if (str_sec == NULL || prb_sec == NULL || 12214 arg_sec == NULL || off_sec == NULL) 12215 return (-1); 12216 12217 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 12218 12219 if (provider->dofpv_name >= str_sec->dofs_size || 12220 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 12221 dtrace_dof_error(dof, "invalid provider name"); 12222 return (-1); 12223 } 12224 12225 if (prb_sec->dofs_entsize == 0 || 12226 prb_sec->dofs_entsize > prb_sec->dofs_size) { 12227 dtrace_dof_error(dof, "invalid entry size"); 12228 return (-1); 12229 } 12230 12231 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 12232 dtrace_dof_error(dof, "misaligned entry size"); 12233 return (-1); 12234 } 12235 12236 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 12237 dtrace_dof_error(dof, "invalid entry size"); 12238 return (-1); 12239 } 12240 12241 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 12242 dtrace_dof_error(dof, "misaligned section offset"); 12243 return (-1); 12244 } 12245 12246 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 12247 dtrace_dof_error(dof, "invalid entry size"); 12248 return (-1); 12249 } 12250 12251 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 12252 12253 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 12254 12255 /* 12256 * Take a pass through the probes to check for errors. 12257 */ 12258 for (j = 0; j < nprobes; j++) { 12259 probe = (dof_probe_t *)(uintptr_t)(daddr + 12260 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 12261 12262 if (probe->dofpr_func >= str_sec->dofs_size) { 12263 dtrace_dof_error(dof, "invalid function name"); 12264 return (-1); 12265 } 12266 12267 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 12268 dtrace_dof_error(dof, "function name too long"); 12269 return (-1); 12270 } 12271 12272 if (probe->dofpr_name >= str_sec->dofs_size || 12273 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 12274 dtrace_dof_error(dof, "invalid probe name"); 12275 return (-1); 12276 } 12277 12278 12279 if (probe->dofpr_offidx + probe->dofpr_noffs < 12280 probe->dofpr_offidx || 12281 (probe->dofpr_offidx + probe->dofpr_noffs) * 12282 off_sec->dofs_entsize > off_sec->dofs_size) { 12283 dtrace_dof_error(dof, "invalid probe offset"); 12284 return (-1); 12285 } 12286 12287 if (probe->dofpr_argidx + probe->dofpr_xargc < 12288 probe->dofpr_argidx || 12289 (probe->dofpr_argidx + probe->dofpr_xargc) * 12290 arg_sec->dofs_entsize > arg_sec->dofs_size) { 12291 dtrace_dof_error(dof, "invalid args"); 12292 return (-1); 12293 } 12294 12295 typeidx = probe->dofpr_nargv; 12296 typestr = strtab + probe->dofpr_nargv; 12297 for (k = 0; k < probe->dofpr_nargc; k++) { 12298 if (typeidx >= str_sec->dofs_size) { 12299 dtrace_dof_error(dof, "bad " 12300 "native argument type"); 12301 return (-1); 12302 } 12303 12304 typesz = strlen(typestr) + 1; 12305 if (typesz > DTRACE_ARGTYPELEN) { 12306 dtrace_dof_error(dof, "native " 12307 "argument type too long"); 12308 return (-1); 12309 } 12310 typeidx += typesz; 12311 typestr += typesz; 12312 } 12313 12314 typeidx = probe->dofpr_xargv; 12315 typestr = strtab + probe->dofpr_xargv; 12316 for (k = 0; k < probe->dofpr_xargc; k++) { 12317 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 12318 dtrace_dof_error(dof, "bad " 12319 "native argument index"); 12320 return (-1); 12321 } 12322 12323 if (typeidx >= str_sec->dofs_size) { 12324 dtrace_dof_error(dof, "bad " 12325 "translated argument type"); 12326 return (-1); 12327 } 12328 12329 typesz = strlen(typestr) + 1; 12330 if (typesz > DTRACE_ARGTYPELEN) { 12331 dtrace_dof_error(dof, "translated argument " 12332 "type too long"); 12333 return (-1); 12334 } 12335 12336 typeidx += typesz; 12337 typestr += typesz; 12338 } 12339 } 12340 12341 return (0); 12342 } 12343 12344 static int 12345 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 12346 { 12347 dtrace_helpers_t *help; 12348 dtrace_vstate_t *vstate; 12349 dtrace_enabling_t *enab = NULL; 12350 int i, gen, rv, nhelpers = 0, destroy = 1; 12351 12352 ASSERT(MUTEX_HELD(&dtrace_lock)); 12353 12354 if ((help = curproc->p_dtrace_helpers) == NULL) 12355 help = dtrace_helpers_create(curproc); 12356 12357 vstate = &help->dthps_vstate; 12358 12359 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 12360 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 12361 dtrace_dof_destroy(dof); 12362 return (rv); 12363 } 12364 12365 /* 12366 * Now we need to walk through the ECB descriptions in the enabling. 12367 */ 12368 for (i = 0; i < enab->dten_ndesc; i++) { 12369 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12370 dtrace_probedesc_t *desc = &ep->dted_probe; 12371 12372 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 12373 continue; 12374 12375 if (strcmp(desc->dtpd_mod, "helper") != 0) 12376 continue; 12377 12378 if (strcmp(desc->dtpd_func, "ustack") != 0) 12379 continue; 12380 12381 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 12382 ep)) != 0) { 12383 /* 12384 * Adding this helper action failed -- we are now going 12385 * to rip out the entire generation and return failure. 12386 */ 12387 (void) dtrace_helper_destroygen(help->dthps_generation); 12388 dtrace_enabling_destroy(enab); 12389 dtrace_dof_destroy(dof); 12390 dtrace_err = rv; 12391 return (-1); 12392 } 12393 12394 nhelpers++; 12395 } 12396 12397 if (nhelpers < enab->dten_ndesc) 12398 dtrace_dof_error(dof, "unmatched helpers"); 12399 12400 if (dhp != NULL) { 12401 uintptr_t daddr = (uintptr_t)dof; 12402 int err = 0, count = 0; 12403 12404 /* 12405 * Look for helper probes. 12406 */ 12407 for (i = 0; i < dof->dofh_secnum; i++) { 12408 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 12409 dof->dofh_secoff + i * dof->dofh_secsize); 12410 12411 if (sec->dofs_type != DOF_SECT_PROVIDER) 12412 continue; 12413 12414 if (dtrace_helper_provider_validate(dof, sec) != 0) { 12415 err = 1; 12416 break; 12417 } 12418 12419 count++; 12420 } 12421 12422 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 12423 if (err == 0 && count > 0 && 12424 dtrace_helper_provider_add(dhp) == 0) 12425 destroy = 0; 12426 else 12427 dhp = NULL; 12428 } 12429 12430 gen = help->dthps_generation++; 12431 dtrace_enabling_destroy(enab); 12432 12433 if (dhp != NULL) { 12434 mutex_exit(&dtrace_lock); 12435 dtrace_helper_provider_register(curproc, help, dhp); 12436 mutex_enter(&dtrace_lock); 12437 } 12438 12439 if (destroy) 12440 dtrace_dof_destroy(dof); 12441 12442 return (gen); 12443 } 12444 12445 static dtrace_helpers_t * 12446 dtrace_helpers_create(proc_t *p) 12447 { 12448 dtrace_helpers_t *help; 12449 12450 ASSERT(MUTEX_HELD(&dtrace_lock)); 12451 ASSERT(p->p_dtrace_helpers == NULL); 12452 12453 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 12454 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 12455 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 12456 12457 p->p_dtrace_helpers = help; 12458 dtrace_helpers++; 12459 12460 return (help); 12461 } 12462 12463 static void 12464 dtrace_helpers_destroy(void) 12465 { 12466 dtrace_helpers_t *help; 12467 dtrace_vstate_t *vstate; 12468 proc_t *p = curproc; 12469 int i; 12470 12471 mutex_enter(&dtrace_lock); 12472 12473 ASSERT(p->p_dtrace_helpers != NULL); 12474 ASSERT(dtrace_helpers > 0); 12475 12476 help = p->p_dtrace_helpers; 12477 vstate = &help->dthps_vstate; 12478 12479 /* 12480 * We're now going to lose the help from this process. 12481 */ 12482 p->p_dtrace_helpers = NULL; 12483 dtrace_sync(); 12484 12485 /* 12486 * Destory the helper actions. 12487 */ 12488 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12489 dtrace_helper_action_t *h, *next; 12490 12491 for (h = help->dthps_actions[i]; h != NULL; h = next) { 12492 next = h->dthp_next; 12493 dtrace_helper_destroy(h, vstate); 12494 h = next; 12495 } 12496 } 12497 12498 mutex_exit(&dtrace_lock); 12499 12500 /* 12501 * Destroy the helper providers. 12502 */ 12503 if (help->dthps_nprovs > 0) { 12504 mutex_enter(&dtrace_meta_lock); 12505 if (dtrace_meta_pid != NULL) { 12506 ASSERT(dtrace_deferred_pid == NULL); 12507 12508 for (i = 0; i < help->dthps_nprovs; i++) { 12509 dtrace_helper_remove( 12510 &help->dthps_provs[i]->dthp_prov, p->p_pid); 12511 } 12512 } else { 12513 mutex_enter(&dtrace_lock); 12514 ASSERT(help->dthps_deferred == 0 || 12515 help->dthps_next != NULL || 12516 help->dthps_prev != NULL || 12517 help == dtrace_deferred_pid); 12518 12519 /* 12520 * Remove the helper from the deferred list. 12521 */ 12522 if (help->dthps_next != NULL) 12523 help->dthps_next->dthps_prev = help->dthps_prev; 12524 if (help->dthps_prev != NULL) 12525 help->dthps_prev->dthps_next = help->dthps_next; 12526 if (dtrace_deferred_pid == help) { 12527 dtrace_deferred_pid = help->dthps_next; 12528 ASSERT(help->dthps_prev == NULL); 12529 } 12530 12531 mutex_exit(&dtrace_lock); 12532 } 12533 12534 mutex_exit(&dtrace_meta_lock); 12535 12536 for (i = 0; i < help->dthps_nprovs; i++) { 12537 dtrace_helper_provider_remove(help->dthps_provs[i]); 12538 } 12539 12540 kmem_free(help->dthps_provs, help->dthps_nprovs * 12541 sizeof (dtrace_helper_provider_t *)); 12542 } 12543 12544 mutex_enter(&dtrace_lock); 12545 12546 dtrace_vstate_fini(&help->dthps_vstate); 12547 kmem_free(help->dthps_actions, 12548 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 12549 kmem_free(help, sizeof (dtrace_helpers_t)); 12550 12551 --dtrace_helpers; 12552 mutex_exit(&dtrace_lock); 12553 } 12554 12555 static void 12556 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 12557 { 12558 dtrace_helpers_t *help, *newhelp; 12559 dtrace_helper_action_t *helper, *new, *last; 12560 dtrace_difo_t *dp; 12561 dtrace_vstate_t *vstate; 12562 int i, j, sz, hasprovs = 0; 12563 12564 mutex_enter(&dtrace_lock); 12565 ASSERT(from->p_dtrace_helpers != NULL); 12566 ASSERT(dtrace_helpers > 0); 12567 12568 help = from->p_dtrace_helpers; 12569 newhelp = dtrace_helpers_create(to); 12570 ASSERT(to->p_dtrace_helpers != NULL); 12571 12572 newhelp->dthps_generation = help->dthps_generation; 12573 vstate = &newhelp->dthps_vstate; 12574 12575 /* 12576 * Duplicate the helper actions. 12577 */ 12578 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12579 if ((helper = help->dthps_actions[i]) == NULL) 12580 continue; 12581 12582 for (last = NULL; helper != NULL; helper = helper->dthp_next) { 12583 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 12584 KM_SLEEP); 12585 new->dthp_generation = helper->dthp_generation; 12586 12587 if ((dp = helper->dthp_predicate) != NULL) { 12588 dp = dtrace_difo_duplicate(dp, vstate); 12589 new->dthp_predicate = dp; 12590 } 12591 12592 new->dthp_nactions = helper->dthp_nactions; 12593 sz = sizeof (dtrace_difo_t *) * new->dthp_nactions; 12594 new->dthp_actions = kmem_alloc(sz, KM_SLEEP); 12595 12596 for (j = 0; j < new->dthp_nactions; j++) { 12597 dtrace_difo_t *dp = helper->dthp_actions[j]; 12598 12599 ASSERT(dp != NULL); 12600 dp = dtrace_difo_duplicate(dp, vstate); 12601 new->dthp_actions[j] = dp; 12602 } 12603 12604 if (last != NULL) { 12605 last->dthp_next = new; 12606 } else { 12607 newhelp->dthps_actions[i] = new; 12608 } 12609 12610 last = new; 12611 } 12612 } 12613 12614 /* 12615 * Duplicate the helper providers and register them with the 12616 * DTrace framework. 12617 */ 12618 if (help->dthps_nprovs > 0) { 12619 newhelp->dthps_nprovs = help->dthps_nprovs; 12620 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 12621 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 12622 for (i = 0; i < newhelp->dthps_nprovs; i++) { 12623 newhelp->dthps_provs[i] = help->dthps_provs[i]; 12624 newhelp->dthps_provs[i]->dthp_ref++; 12625 } 12626 12627 hasprovs = 1; 12628 } 12629 12630 mutex_exit(&dtrace_lock); 12631 12632 if (hasprovs) 12633 dtrace_helper_provider_register(to, newhelp, NULL); 12634 } 12635 12636 /* 12637 * DTrace Hook Functions 12638 */ 12639 static void 12640 dtrace_module_loaded(struct modctl *ctl) 12641 { 12642 dtrace_provider_t *prv; 12643 12644 mutex_enter(&dtrace_provider_lock); 12645 mutex_enter(&mod_lock); 12646 12647 ASSERT(ctl->mod_busy); 12648 12649 /* 12650 * We're going to call each providers per-module provide operation 12651 * specifying only this module. 12652 */ 12653 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 12654 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 12655 12656 mutex_exit(&mod_lock); 12657 mutex_exit(&dtrace_provider_lock); 12658 12659 /* 12660 * If we have any retained enablings, we need to match against them. 12661 * Enabling probes requires that cpu_lock be held, and we cannot hold 12662 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 12663 * module. (In particular, this happens when loading scheduling 12664 * classes.) So if we have any retained enablings, we need to dispatch 12665 * our task queue to do the match for us. 12666 */ 12667 mutex_enter(&dtrace_lock); 12668 12669 if (dtrace_retained == NULL) { 12670 mutex_exit(&dtrace_lock); 12671 return; 12672 } 12673 12674 (void) taskq_dispatch(dtrace_taskq, 12675 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 12676 12677 mutex_exit(&dtrace_lock); 12678 12679 /* 12680 * And now, for a little heuristic sleaze: in general, we want to 12681 * match modules as soon as they load. However, we cannot guarantee 12682 * this, because it would lead us to the lock ordering violation 12683 * outlined above. The common case, of course, is that cpu_lock is 12684 * _not_ held -- so we delay here for a clock tick, hoping that that's 12685 * long enough for the task queue to do its work. If it's not, it's 12686 * not a serious problem -- it just means that the module that we 12687 * just loaded may not be immediately instrumentable. 12688 */ 12689 delay(1); 12690 } 12691 12692 static void 12693 dtrace_module_unloaded(struct modctl *ctl) 12694 { 12695 dtrace_probe_t template, *probe, *first, *next; 12696 dtrace_provider_t *prov; 12697 12698 template.dtpr_mod = ctl->mod_modname; 12699 12700 mutex_enter(&dtrace_provider_lock); 12701 mutex_enter(&mod_lock); 12702 mutex_enter(&dtrace_lock); 12703 12704 if (dtrace_bymod == NULL) { 12705 /* 12706 * The DTrace module is loaded (obviously) but not attached; 12707 * we don't have any work to do. 12708 */ 12709 mutex_exit(&dtrace_provider_lock); 12710 mutex_exit(&mod_lock); 12711 mutex_exit(&dtrace_lock); 12712 return; 12713 } 12714 12715 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 12716 probe != NULL; probe = probe->dtpr_nextmod) { 12717 if (probe->dtpr_ecb != NULL) { 12718 mutex_exit(&dtrace_provider_lock); 12719 mutex_exit(&mod_lock); 12720 mutex_exit(&dtrace_lock); 12721 12722 /* 12723 * This shouldn't _actually_ be possible -- we're 12724 * unloading a module that has an enabled probe in it. 12725 * (It's normally up to the provider to make sure that 12726 * this can't happen.) However, because dtps_enable() 12727 * doesn't have a failure mode, there can be an 12728 * enable/unload race. Upshot: we don't want to 12729 * assert, but we're not going to disable the 12730 * probe, either. 12731 */ 12732 if (dtrace_err_verbose) { 12733 cmn_err(CE_WARN, "unloaded module '%s' had " 12734 "enabled probes", ctl->mod_modname); 12735 } 12736 12737 return; 12738 } 12739 } 12740 12741 probe = first; 12742 12743 for (first = NULL; probe != NULL; probe = next) { 12744 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 12745 12746 dtrace_probes[probe->dtpr_id - 1] = NULL; 12747 12748 next = probe->dtpr_nextmod; 12749 dtrace_hash_remove(dtrace_bymod, probe); 12750 dtrace_hash_remove(dtrace_byfunc, probe); 12751 dtrace_hash_remove(dtrace_byname, probe); 12752 12753 if (first == NULL) { 12754 first = probe; 12755 probe->dtpr_nextmod = NULL; 12756 } else { 12757 probe->dtpr_nextmod = first; 12758 first = probe; 12759 } 12760 } 12761 12762 /* 12763 * We've removed all of the module's probes from the hash chains and 12764 * from the probe array. Now issue a dtrace_sync() to be sure that 12765 * everyone has cleared out from any probe array processing. 12766 */ 12767 dtrace_sync(); 12768 12769 for (probe = first; probe != NULL; probe = first) { 12770 first = probe->dtpr_nextmod; 12771 prov = probe->dtpr_provider; 12772 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 12773 probe->dtpr_arg); 12774 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 12775 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 12776 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 12777 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 12778 kmem_free(probe, sizeof (dtrace_probe_t)); 12779 } 12780 12781 mutex_exit(&dtrace_lock); 12782 mutex_exit(&mod_lock); 12783 mutex_exit(&dtrace_provider_lock); 12784 } 12785 12786 void 12787 dtrace_suspend(void) 12788 { 12789 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 12790 } 12791 12792 void 12793 dtrace_resume(void) 12794 { 12795 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 12796 } 12797 12798 static int 12799 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 12800 { 12801 ASSERT(MUTEX_HELD(&cpu_lock)); 12802 mutex_enter(&dtrace_lock); 12803 12804 switch (what) { 12805 case CPU_CONFIG: { 12806 dtrace_state_t *state; 12807 dtrace_optval_t *opt, rs, c; 12808 12809 /* 12810 * For now, we only allocate a new buffer for anonymous state. 12811 */ 12812 if ((state = dtrace_anon.dta_state) == NULL) 12813 break; 12814 12815 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 12816 break; 12817 12818 opt = state->dts_options; 12819 c = opt[DTRACEOPT_CPU]; 12820 12821 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 12822 break; 12823 12824 /* 12825 * Regardless of what the actual policy is, we're going to 12826 * temporarily set our resize policy to be manual. We're 12827 * also going to temporarily set our CPU option to denote 12828 * the newly configured CPU. 12829 */ 12830 rs = opt[DTRACEOPT_BUFRESIZE]; 12831 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 12832 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 12833 12834 (void) dtrace_state_buffers(state); 12835 12836 opt[DTRACEOPT_BUFRESIZE] = rs; 12837 opt[DTRACEOPT_CPU] = c; 12838 12839 break; 12840 } 12841 12842 case CPU_UNCONFIG: 12843 /* 12844 * We don't free the buffer in the CPU_UNCONFIG case. (The 12845 * buffer will be freed when the consumer exits.) 12846 */ 12847 break; 12848 12849 default: 12850 break; 12851 } 12852 12853 mutex_exit(&dtrace_lock); 12854 return (0); 12855 } 12856 12857 static void 12858 dtrace_cpu_setup_initial(processorid_t cpu) 12859 { 12860 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 12861 } 12862 12863 static void 12864 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 12865 { 12866 if (dtrace_toxranges >= dtrace_toxranges_max) { 12867 int osize, nsize; 12868 dtrace_toxrange_t *range; 12869 12870 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 12871 12872 if (osize == 0) { 12873 ASSERT(dtrace_toxrange == NULL); 12874 ASSERT(dtrace_toxranges_max == 0); 12875 dtrace_toxranges_max = 1; 12876 } else { 12877 dtrace_toxranges_max <<= 1; 12878 } 12879 12880 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 12881 range = kmem_zalloc(nsize, KM_SLEEP); 12882 12883 if (dtrace_toxrange != NULL) { 12884 ASSERT(osize != 0); 12885 bcopy(dtrace_toxrange, range, osize); 12886 kmem_free(dtrace_toxrange, osize); 12887 } 12888 12889 dtrace_toxrange = range; 12890 } 12891 12892 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); 12893 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); 12894 12895 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 12896 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 12897 dtrace_toxranges++; 12898 } 12899 12900 /* 12901 * DTrace Driver Cookbook Functions 12902 */ 12903 /*ARGSUSED*/ 12904 static int 12905 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 12906 { 12907 dtrace_provider_id_t id; 12908 dtrace_state_t *state = NULL; 12909 dtrace_enabling_t *enab; 12910 12911 mutex_enter(&cpu_lock); 12912 mutex_enter(&dtrace_provider_lock); 12913 mutex_enter(&dtrace_lock); 12914 12915 if (ddi_soft_state_init(&dtrace_softstate, sizeof (dtrace_state_t) + 12916 NCPU * sizeof (dtrace_buffer_t), 0) != 0) { 12917 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 12918 mutex_exit(&cpu_lock); 12919 mutex_exit(&dtrace_provider_lock); 12920 mutex_exit(&dtrace_lock); 12921 return (DDI_FAILURE); 12922 } 12923 12924 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 12925 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 12926 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 12927 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 12928 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 12929 ddi_remove_minor_node(devi, NULL); 12930 ddi_soft_state_fini(&dtrace_softstate); 12931 mutex_exit(&cpu_lock); 12932 mutex_exit(&dtrace_provider_lock); 12933 mutex_exit(&dtrace_lock); 12934 return (DDI_FAILURE); 12935 } 12936 12937 ddi_report_dev(devi); 12938 dtrace_devi = devi; 12939 12940 dtrace_modload = dtrace_module_loaded; 12941 dtrace_modunload = dtrace_module_unloaded; 12942 dtrace_cpu_init = dtrace_cpu_setup_initial; 12943 dtrace_helpers_cleanup = dtrace_helpers_destroy; 12944 dtrace_helpers_fork = dtrace_helpers_duplicate; 12945 dtrace_cpustart_init = dtrace_suspend; 12946 dtrace_cpustart_fini = dtrace_resume; 12947 dtrace_debugger_init = dtrace_suspend; 12948 dtrace_debugger_fini = dtrace_resume; 12949 dtrace_kreloc_init = dtrace_suspend; 12950 dtrace_kreloc_fini = dtrace_resume; 12951 12952 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 12953 12954 ASSERT(MUTEX_HELD(&cpu_lock)); 12955 12956 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 12957 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 12958 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 12959 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 12960 VM_SLEEP | VMC_IDENTIFIER); 12961 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 12962 1, INT_MAX, 0); 12963 12964 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 12965 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 12966 NULL, NULL, NULL, NULL, NULL, 0); 12967 12968 ASSERT(MUTEX_HELD(&cpu_lock)); 12969 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 12970 offsetof(dtrace_probe_t, dtpr_nextmod), 12971 offsetof(dtrace_probe_t, dtpr_prevmod)); 12972 12973 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 12974 offsetof(dtrace_probe_t, dtpr_nextfunc), 12975 offsetof(dtrace_probe_t, dtpr_prevfunc)); 12976 12977 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 12978 offsetof(dtrace_probe_t, dtpr_nextname), 12979 offsetof(dtrace_probe_t, dtpr_prevname)); 12980 12981 if (dtrace_retain_max < 1) { 12982 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 12983 "setting to 1", dtrace_retain_max); 12984 dtrace_retain_max = 1; 12985 } 12986 12987 /* 12988 * Now discover our toxic ranges. 12989 */ 12990 dtrace_toxic_ranges(dtrace_toxrange_add); 12991 12992 /* 12993 * Before we register ourselves as a provider to our own framework, 12994 * we would like to assert that dtrace_provider is NULL -- but that's 12995 * not true if we were loaded as a dependency of a DTrace provider. 12996 * Once we've registered, we can assert that dtrace_provider is our 12997 * pseudo provider. 12998 */ 12999 (void) dtrace_register("dtrace", &dtrace_provider_attr, 13000 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 13001 13002 ASSERT(dtrace_provider != NULL); 13003 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 13004 13005 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 13006 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 13007 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 13008 dtrace_provider, NULL, NULL, "END", 0, NULL); 13009 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 13010 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 13011 13012 dtrace_anon_property(); 13013 mutex_exit(&cpu_lock); 13014 13015 /* 13016 * If DTrace helper tracing is enabled, we need to allocate the 13017 * trace buffer and initialize the values. 13018 */ 13019 if (dtrace_helptrace_enabled) { 13020 ASSERT(dtrace_helptrace_buffer == NULL); 13021 dtrace_helptrace_buffer = 13022 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 13023 dtrace_helptrace_next = 0; 13024 } 13025 13026 /* 13027 * If there are already providers, we must ask them to provide their 13028 * probes, and then match any anonymous enabling against them. Note 13029 * that there should be no other retained enablings at this time: 13030 * the only retained enablings at this time should be the anonymous 13031 * enabling. 13032 */ 13033 if (dtrace_anon.dta_enabling != NULL) { 13034 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 13035 13036 dtrace_enabling_provide(NULL); 13037 state = dtrace_anon.dta_state; 13038 13039 /* 13040 * We couldn't hold cpu_lock across the above call to 13041 * dtrace_enabling_provide(), but we must hold it to actually 13042 * enable the probes. We have to drop all of our locks, pick 13043 * up cpu_lock, and regain our locks before matching the 13044 * retained anonymous enabling. 13045 */ 13046 mutex_exit(&dtrace_lock); 13047 mutex_exit(&dtrace_provider_lock); 13048 13049 mutex_enter(&cpu_lock); 13050 mutex_enter(&dtrace_provider_lock); 13051 mutex_enter(&dtrace_lock); 13052 13053 if ((enab = dtrace_anon.dta_enabling) != NULL) 13054 (void) dtrace_enabling_match(enab, NULL); 13055 13056 mutex_exit(&cpu_lock); 13057 } 13058 13059 mutex_exit(&dtrace_lock); 13060 mutex_exit(&dtrace_provider_lock); 13061 13062 if (state != NULL) { 13063 /* 13064 * If we created any anonymous state, set it going now. 13065 */ 13066 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 13067 } 13068 13069 return (DDI_SUCCESS); 13070 } 13071 13072 /*ARGSUSED*/ 13073 static int 13074 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 13075 { 13076 dtrace_state_t *state; 13077 uint32_t priv; 13078 uid_t uid; 13079 13080 if (getminor(*devp) == DTRACEMNRN_HELPER) 13081 return (0); 13082 13083 /* 13084 * If this wasn't an open with the "helper" minor, then it must be 13085 * the "dtrace" minor. 13086 */ 13087 ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE); 13088 13089 /* 13090 * If no DTRACE_PRIV_* bits are set in the credential, then the 13091 * caller lacks sufficient permission to do anything with DTrace. 13092 */ 13093 dtrace_cred2priv(cred_p, &priv, &uid); 13094 if (priv == DTRACE_PRIV_NONE) 13095 return (EACCES); 13096 13097 /* 13098 * Ask all providers to provide all their probes. 13099 */ 13100 mutex_enter(&dtrace_provider_lock); 13101 dtrace_probe_provide(NULL, NULL); 13102 mutex_exit(&dtrace_provider_lock); 13103 13104 mutex_enter(&cpu_lock); 13105 mutex_enter(&dtrace_lock); 13106 dtrace_opens++; 13107 dtrace_membar_producer(); 13108 13109 /* 13110 * If the kernel debugger is active (that is, if the kernel debugger 13111 * modified text in some way), we won't allow the open. 13112 */ 13113 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13114 dtrace_opens--; 13115 mutex_exit(&cpu_lock); 13116 mutex_exit(&dtrace_lock); 13117 return (EBUSY); 13118 } 13119 13120 state = dtrace_state_create(devp, cred_p); 13121 mutex_exit(&cpu_lock); 13122 13123 if (state == NULL) { 13124 if (--dtrace_opens == 0) 13125 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13126 mutex_exit(&dtrace_lock); 13127 return (EAGAIN); 13128 } 13129 13130 mutex_exit(&dtrace_lock); 13131 13132 return (0); 13133 } 13134 13135 /*ARGSUSED*/ 13136 static int 13137 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 13138 { 13139 minor_t minor = getminor(dev); 13140 dtrace_state_t *state; 13141 13142 if (minor == DTRACEMNRN_HELPER) 13143 return (0); 13144 13145 state = ddi_get_soft_state(dtrace_softstate, minor); 13146 13147 mutex_enter(&cpu_lock); 13148 mutex_enter(&dtrace_lock); 13149 13150 if (state->dts_anon) { 13151 /* 13152 * There is anonymous state. Destroy that first. 13153 */ 13154 ASSERT(dtrace_anon.dta_state == NULL); 13155 dtrace_state_destroy(state->dts_anon); 13156 } 13157 13158 dtrace_state_destroy(state); 13159 ASSERT(dtrace_opens > 0); 13160 if (--dtrace_opens == 0) 13161 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13162 13163 mutex_exit(&dtrace_lock); 13164 mutex_exit(&cpu_lock); 13165 13166 return (0); 13167 } 13168 13169 /*ARGSUSED*/ 13170 static int 13171 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 13172 { 13173 int rval; 13174 dof_helper_t help, *dhp = NULL; 13175 13176 switch (cmd) { 13177 case DTRACEHIOC_ADDDOF: 13178 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 13179 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 13180 return (EFAULT); 13181 } 13182 13183 dhp = &help; 13184 arg = (intptr_t)help.dofhp_dof; 13185 /*FALLTHROUGH*/ 13186 13187 case DTRACEHIOC_ADD: { 13188 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 13189 13190 if (dof == NULL) 13191 return (rval); 13192 13193 mutex_enter(&dtrace_lock); 13194 dtrace_err = 0; 13195 13196 /* 13197 * dtrace_helper_slurp() takes responsibility for the dof -- 13198 * it may free it now or it may save it and free it later. 13199 */ 13200 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 13201 *rv = rval; 13202 rval = 0; 13203 } else { 13204 rval = EINVAL; 13205 } 13206 13207 mutex_exit(&dtrace_lock); 13208 return (rval); 13209 } 13210 13211 case DTRACEHIOC_REMOVE: { 13212 mutex_enter(&dtrace_lock); 13213 rval = dtrace_helper_destroygen(arg); 13214 mutex_exit(&dtrace_lock); 13215 13216 return (rval); 13217 } 13218 13219 default: 13220 break; 13221 } 13222 13223 return (ENOTTY); 13224 } 13225 13226 /*ARGSUSED*/ 13227 static int 13228 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 13229 { 13230 minor_t minor = getminor(dev); 13231 dtrace_state_t *state; 13232 int rval; 13233 13234 if (minor == DTRACEMNRN_HELPER) 13235 return (dtrace_ioctl_helper(cmd, arg, rv)); 13236 13237 state = ddi_get_soft_state(dtrace_softstate, minor); 13238 13239 if (state->dts_anon) { 13240 ASSERT(dtrace_anon.dta_state == NULL); 13241 state = state->dts_anon; 13242 } 13243 13244 switch (cmd) { 13245 case DTRACEIOC_PROVIDER: { 13246 dtrace_providerdesc_t pvd; 13247 dtrace_provider_t *pvp; 13248 13249 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 13250 return (EFAULT); 13251 13252 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 13253 mutex_enter(&dtrace_provider_lock); 13254 13255 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 13256 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 13257 break; 13258 } 13259 13260 mutex_exit(&dtrace_provider_lock); 13261 13262 if (pvp == NULL) 13263 return (ESRCH); 13264 13265 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 13266 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 13267 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 13268 return (EFAULT); 13269 13270 return (0); 13271 } 13272 13273 case DTRACEIOC_EPROBE: { 13274 dtrace_eprobedesc_t epdesc; 13275 dtrace_ecb_t *ecb; 13276 dtrace_action_t *act; 13277 void *buf; 13278 size_t size; 13279 uintptr_t dest; 13280 int nrecs; 13281 13282 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 13283 return (EFAULT); 13284 13285 mutex_enter(&dtrace_lock); 13286 13287 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 13288 mutex_exit(&dtrace_lock); 13289 return (EINVAL); 13290 } 13291 13292 if (ecb->dte_probe == NULL) { 13293 mutex_exit(&dtrace_lock); 13294 return (EINVAL); 13295 } 13296 13297 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 13298 epdesc.dtepd_uarg = ecb->dte_uarg; 13299 epdesc.dtepd_size = ecb->dte_size; 13300 13301 nrecs = epdesc.dtepd_nrecs; 13302 epdesc.dtepd_nrecs = 0; 13303 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 13304 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 13305 continue; 13306 13307 epdesc.dtepd_nrecs++; 13308 } 13309 13310 /* 13311 * Now that we have the size, we need to allocate a temporary 13312 * buffer in which to store the complete description. We need 13313 * the temporary buffer to be able to drop dtrace_lock() 13314 * across the copyout(), below. 13315 */ 13316 size = sizeof (dtrace_eprobedesc_t) + 13317 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 13318 13319 buf = kmem_alloc(size, KM_SLEEP); 13320 dest = (uintptr_t)buf; 13321 13322 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 13323 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 13324 13325 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 13326 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 13327 continue; 13328 13329 if (nrecs-- == 0) 13330 break; 13331 13332 bcopy(&act->dta_rec, (void *)dest, 13333 sizeof (dtrace_recdesc_t)); 13334 dest += sizeof (dtrace_recdesc_t); 13335 } 13336 13337 mutex_exit(&dtrace_lock); 13338 13339 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 13340 kmem_free(buf, size); 13341 return (EFAULT); 13342 } 13343 13344 kmem_free(buf, size); 13345 return (0); 13346 } 13347 13348 case DTRACEIOC_AGGDESC: { 13349 dtrace_aggdesc_t aggdesc; 13350 dtrace_action_t *act; 13351 dtrace_aggregation_t *agg; 13352 int nrecs; 13353 uint32_t offs; 13354 dtrace_recdesc_t *lrec; 13355 void *buf; 13356 size_t size; 13357 uintptr_t dest; 13358 13359 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 13360 return (EFAULT); 13361 13362 mutex_enter(&dtrace_lock); 13363 13364 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 13365 mutex_exit(&dtrace_lock); 13366 return (EINVAL); 13367 } 13368 13369 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 13370 13371 nrecs = aggdesc.dtagd_nrecs; 13372 aggdesc.dtagd_nrecs = 0; 13373 13374 offs = agg->dtag_base; 13375 lrec = &agg->dtag_action.dta_rec; 13376 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 13377 13378 for (act = agg->dtag_first; ; act = act->dta_next) { 13379 ASSERT(act->dta_intuple || 13380 DTRACEACT_ISAGG(act->dta_kind)); 13381 aggdesc.dtagd_nrecs++; 13382 13383 if (act == &agg->dtag_action) 13384 break; 13385 } 13386 13387 /* 13388 * Now that we have the size, we need to allocate a temporary 13389 * buffer in which to store the complete description. We need 13390 * the temporary buffer to be able to drop dtrace_lock() 13391 * across the copyout(), below. 13392 */ 13393 size = sizeof (dtrace_aggdesc_t) + 13394 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 13395 13396 buf = kmem_alloc(size, KM_SLEEP); 13397 dest = (uintptr_t)buf; 13398 13399 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 13400 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 13401 13402 for (act = agg->dtag_first; ; act = act->dta_next) { 13403 dtrace_recdesc_t rec = act->dta_rec; 13404 13405 if (nrecs-- == 0) 13406 break; 13407 13408 rec.dtrd_offset -= offs; 13409 bcopy(&rec, (void *)dest, sizeof (rec)); 13410 dest += sizeof (dtrace_recdesc_t); 13411 13412 if (act == &agg->dtag_action) 13413 break; 13414 } 13415 13416 mutex_exit(&dtrace_lock); 13417 13418 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 13419 kmem_free(buf, size); 13420 return (EFAULT); 13421 } 13422 13423 kmem_free(buf, size); 13424 return (0); 13425 } 13426 13427 case DTRACEIOC_ENABLE: { 13428 dof_hdr_t *dof; 13429 dtrace_enabling_t *enab = NULL; 13430 dtrace_vstate_t *vstate; 13431 int err = 0; 13432 13433 *rv = 0; 13434 13435 /* 13436 * If a NULL argument has been passed, we take this as our 13437 * cue to reevaluate our enablings. 13438 */ 13439 if (arg == NULL) { 13440 mutex_enter(&cpu_lock); 13441 mutex_enter(&dtrace_lock); 13442 err = dtrace_enabling_matchstate(state, rv); 13443 mutex_exit(&dtrace_lock); 13444 mutex_exit(&cpu_lock); 13445 13446 return (err); 13447 } 13448 13449 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 13450 return (rval); 13451 13452 mutex_enter(&cpu_lock); 13453 mutex_enter(&dtrace_lock); 13454 vstate = &state->dts_vstate; 13455 13456 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13457 mutex_exit(&dtrace_lock); 13458 mutex_exit(&cpu_lock); 13459 dtrace_dof_destroy(dof); 13460 return (EBUSY); 13461 } 13462 13463 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 13464 mutex_exit(&dtrace_lock); 13465 mutex_exit(&cpu_lock); 13466 dtrace_dof_destroy(dof); 13467 return (EINVAL); 13468 } 13469 13470 if ((rval = dtrace_dof_options(dof, state)) != 0) { 13471 dtrace_enabling_destroy(enab); 13472 mutex_exit(&dtrace_lock); 13473 mutex_exit(&cpu_lock); 13474 dtrace_dof_destroy(dof); 13475 return (rval); 13476 } 13477 13478 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 13479 err = dtrace_enabling_retain(enab); 13480 } else { 13481 dtrace_enabling_destroy(enab); 13482 } 13483 13484 mutex_exit(&cpu_lock); 13485 mutex_exit(&dtrace_lock); 13486 dtrace_dof_destroy(dof); 13487 13488 return (err); 13489 } 13490 13491 case DTRACEIOC_REPLICATE: { 13492 dtrace_repldesc_t desc; 13493 dtrace_probedesc_t *match = &desc.dtrpd_match; 13494 dtrace_probedesc_t *create = &desc.dtrpd_create; 13495 int err; 13496 13497 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13498 return (EFAULT); 13499 13500 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13501 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13502 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13503 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13504 13505 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13506 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13507 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13508 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13509 13510 mutex_enter(&dtrace_lock); 13511 err = dtrace_enabling_replicate(state, match, create); 13512 mutex_exit(&dtrace_lock); 13513 13514 return (err); 13515 } 13516 13517 case DTRACEIOC_PROBEMATCH: 13518 case DTRACEIOC_PROBES: { 13519 dtrace_probe_t *probe = NULL; 13520 dtrace_probedesc_t desc; 13521 dtrace_probekey_t pkey; 13522 dtrace_id_t i; 13523 int m = 0; 13524 uint32_t priv; 13525 uid_t uid; 13526 13527 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13528 return (EFAULT); 13529 13530 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 13531 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 13532 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 13533 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 13534 13535 /* 13536 * Before we attempt to match this probe, we want to give 13537 * all providers the opportunity to provide it. 13538 */ 13539 if (desc.dtpd_id == DTRACE_IDNONE) { 13540 mutex_enter(&dtrace_provider_lock); 13541 dtrace_probe_provide(&desc, NULL); 13542 mutex_exit(&dtrace_provider_lock); 13543 desc.dtpd_id++; 13544 } 13545 13546 if (cmd == DTRACEIOC_PROBEMATCH) { 13547 dtrace_probekey(&desc, &pkey); 13548 pkey.dtpk_id = DTRACE_IDNONE; 13549 } 13550 13551 uid = crgetuid(cr); 13552 dtrace_cred2priv(cr, &priv, &uid); 13553 13554 mutex_enter(&dtrace_lock); 13555 13556 if (cmd == DTRACEIOC_PROBEMATCH) { 13557 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 13558 if ((probe = dtrace_probes[i - 1]) != NULL && 13559 (m = dtrace_match_probe(probe, &pkey, 13560 priv, uid)) != 0) 13561 break; 13562 } 13563 13564 if (m < 0) { 13565 mutex_exit(&dtrace_lock); 13566 return (EINVAL); 13567 } 13568 13569 } else { 13570 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 13571 if ((probe = dtrace_probes[i - 1]) != NULL && 13572 dtrace_match_priv(probe, priv, uid)) 13573 break; 13574 } 13575 } 13576 13577 if (probe == NULL) { 13578 mutex_exit(&dtrace_lock); 13579 return (ESRCH); 13580 } 13581 13582 dtrace_probe_description(probe, &desc); 13583 mutex_exit(&dtrace_lock); 13584 13585 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13586 return (EFAULT); 13587 13588 return (0); 13589 } 13590 13591 case DTRACEIOC_PROBEARG: { 13592 dtrace_argdesc_t desc; 13593 dtrace_probe_t *probe; 13594 dtrace_provider_t *prov; 13595 13596 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13597 return (EFAULT); 13598 13599 if (desc.dtargd_id == DTRACE_IDNONE) 13600 return (EINVAL); 13601 13602 if (desc.dtargd_ndx == DTRACE_ARGNONE) 13603 return (EINVAL); 13604 13605 mutex_enter(&dtrace_provider_lock); 13606 mutex_enter(&mod_lock); 13607 mutex_enter(&dtrace_lock); 13608 13609 if (desc.dtargd_id > dtrace_nprobes) { 13610 mutex_exit(&dtrace_lock); 13611 mutex_exit(&mod_lock); 13612 mutex_exit(&dtrace_provider_lock); 13613 return (EINVAL); 13614 } 13615 13616 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 13617 mutex_exit(&dtrace_lock); 13618 mutex_exit(&mod_lock); 13619 mutex_exit(&dtrace_provider_lock); 13620 return (EINVAL); 13621 } 13622 13623 mutex_exit(&dtrace_lock); 13624 13625 prov = probe->dtpr_provider; 13626 13627 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 13628 /* 13629 * There isn't any typed information for this probe. 13630 * Set the argument number to DTRACE_ARGNONE. 13631 */ 13632 desc.dtargd_ndx = DTRACE_ARGNONE; 13633 } else { 13634 desc.dtargd_native[0] = '\0'; 13635 desc.dtargd_xlate[0] = '\0'; 13636 desc.dtargd_mapping = desc.dtargd_ndx; 13637 13638 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 13639 probe->dtpr_id, probe->dtpr_arg, &desc); 13640 } 13641 13642 mutex_exit(&mod_lock); 13643 mutex_exit(&dtrace_provider_lock); 13644 13645 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13646 return (EFAULT); 13647 13648 return (0); 13649 } 13650 13651 case DTRACEIOC_GO: { 13652 processorid_t cpuid; 13653 rval = dtrace_state_go(state, &cpuid); 13654 13655 if (rval != 0) 13656 return (rval); 13657 13658 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 13659 return (EFAULT); 13660 13661 return (0); 13662 } 13663 13664 case DTRACEIOC_STOP: { 13665 processorid_t cpuid; 13666 13667 mutex_enter(&dtrace_lock); 13668 rval = dtrace_state_stop(state, &cpuid); 13669 mutex_exit(&dtrace_lock); 13670 13671 if (rval != 0) 13672 return (rval); 13673 13674 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 13675 return (EFAULT); 13676 13677 return (0); 13678 } 13679 13680 case DTRACEIOC_DOFGET: { 13681 dof_hdr_t hdr, *dof; 13682 uint64_t len; 13683 13684 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 13685 return (EFAULT); 13686 13687 mutex_enter(&dtrace_lock); 13688 dof = dtrace_dof_create(state); 13689 mutex_exit(&dtrace_lock); 13690 13691 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 13692 rval = copyout(dof, (void *)arg, len); 13693 dtrace_dof_destroy(dof); 13694 13695 return (rval == 0 ? 0 : EFAULT); 13696 } 13697 13698 case DTRACEIOC_AGGSNAP: 13699 case DTRACEIOC_BUFSNAP: { 13700 dtrace_bufdesc_t desc; 13701 caddr_t cached; 13702 dtrace_buffer_t *buf; 13703 13704 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13705 return (EFAULT); 13706 13707 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 13708 return (EINVAL); 13709 13710 mutex_enter(&dtrace_lock); 13711 13712 if (cmd == DTRACEIOC_BUFSNAP) { 13713 buf = &state->dts_buffer[desc.dtbd_cpu]; 13714 } else { 13715 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 13716 } 13717 13718 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 13719 size_t sz = buf->dtb_offset; 13720 13721 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 13722 mutex_exit(&dtrace_lock); 13723 return (EBUSY); 13724 } 13725 13726 /* 13727 * If this buffer has already been consumed, we're 13728 * going to indicate that there's nothing left here 13729 * to consume. 13730 */ 13731 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 13732 mutex_exit(&dtrace_lock); 13733 13734 desc.dtbd_size = 0; 13735 desc.dtbd_drops = 0; 13736 desc.dtbd_errors = 0; 13737 desc.dtbd_oldest = 0; 13738 sz = sizeof (desc); 13739 13740 if (copyout(&desc, (void *)arg, sz) != 0) 13741 return (EFAULT); 13742 13743 return (0); 13744 } 13745 13746 /* 13747 * If this is a ring buffer that has wrapped, we want 13748 * to copy the whole thing out. 13749 */ 13750 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 13751 dtrace_buffer_polish(buf); 13752 sz = buf->dtb_size; 13753 } 13754 13755 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 13756 mutex_exit(&dtrace_lock); 13757 return (EFAULT); 13758 } 13759 13760 desc.dtbd_size = sz; 13761 desc.dtbd_drops = buf->dtb_drops; 13762 desc.dtbd_errors = buf->dtb_errors; 13763 desc.dtbd_oldest = buf->dtb_xamot_offset; 13764 13765 mutex_exit(&dtrace_lock); 13766 13767 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13768 return (EFAULT); 13769 13770 buf->dtb_flags |= DTRACEBUF_CONSUMED; 13771 13772 return (0); 13773 } 13774 13775 if (buf->dtb_tomax == NULL) { 13776 ASSERT(buf->dtb_xamot == NULL); 13777 mutex_exit(&dtrace_lock); 13778 return (ENOENT); 13779 } 13780 13781 cached = buf->dtb_tomax; 13782 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 13783 13784 dtrace_xcall(desc.dtbd_cpu, 13785 (dtrace_xcall_t)dtrace_buffer_switch, buf); 13786 13787 state->dts_errors += buf->dtb_xamot_errors; 13788 13789 /* 13790 * If the buffers did not actually switch, then the cross call 13791 * did not take place -- presumably because the given CPU is 13792 * not in the ready set. If this is the case, we'll return 13793 * ENOENT. 13794 */ 13795 if (buf->dtb_tomax == cached) { 13796 ASSERT(buf->dtb_xamot != cached); 13797 mutex_exit(&dtrace_lock); 13798 return (ENOENT); 13799 } 13800 13801 ASSERT(cached == buf->dtb_xamot); 13802 13803 /* 13804 * We have our snapshot; now copy it out. 13805 */ 13806 if (copyout(buf->dtb_xamot, desc.dtbd_data, 13807 buf->dtb_xamot_offset) != 0) { 13808 mutex_exit(&dtrace_lock); 13809 return (EFAULT); 13810 } 13811 13812 desc.dtbd_size = buf->dtb_xamot_offset; 13813 desc.dtbd_drops = buf->dtb_xamot_drops; 13814 desc.dtbd_errors = buf->dtb_xamot_errors; 13815 desc.dtbd_oldest = 0; 13816 13817 mutex_exit(&dtrace_lock); 13818 13819 /* 13820 * Finally, copy out the buffer description. 13821 */ 13822 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 13823 return (EFAULT); 13824 13825 return (0); 13826 } 13827 13828 case DTRACEIOC_CONF: { 13829 dtrace_conf_t conf; 13830 13831 bzero(&conf, sizeof (conf)); 13832 conf.dtc_difversion = DIF_VERSION; 13833 conf.dtc_difintregs = DIF_DIR_NREGS; 13834 conf.dtc_diftupregs = DIF_DTR_NREGS; 13835 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 13836 13837 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 13838 return (EFAULT); 13839 13840 return (0); 13841 } 13842 13843 case DTRACEIOC_STATUS: { 13844 dtrace_status_t stat; 13845 dtrace_dstate_t *dstate; 13846 int i, j; 13847 uint64_t nerrs; 13848 13849 /* 13850 * See the comment in dtrace_state_deadman() for the reason 13851 * for setting dts_laststatus to INT64_MAX before setting 13852 * it to the correct value. 13853 */ 13854 state->dts_laststatus = INT64_MAX; 13855 dtrace_membar_producer(); 13856 state->dts_laststatus = dtrace_gethrtime(); 13857 13858 bzero(&stat, sizeof (stat)); 13859 13860 mutex_enter(&dtrace_lock); 13861 13862 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 13863 mutex_exit(&dtrace_lock); 13864 return (ENOENT); 13865 } 13866 13867 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 13868 stat.dtst_exiting = 1; 13869 13870 nerrs = state->dts_errors; 13871 dstate = &state->dts_vstate.dtvs_dynvars; 13872 13873 for (i = 0; i < NCPU; i++) { 13874 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 13875 13876 stat.dtst_dyndrops += dcpu->dtdsc_drops; 13877 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 13878 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 13879 13880 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 13881 stat.dtst_filled++; 13882 13883 nerrs += state->dts_buffer[i].dtb_errors; 13884 13885 for (j = 0; j < state->dts_nspeculations; j++) { 13886 dtrace_speculation_t *spec; 13887 dtrace_buffer_t *buf; 13888 13889 spec = &state->dts_speculations[j]; 13890 buf = &spec->dtsp_buffer[i]; 13891 stat.dtst_specdrops += buf->dtb_xamot_drops; 13892 } 13893 } 13894 13895 stat.dtst_specdrops_busy = state->dts_speculations_busy; 13896 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 13897 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 13898 stat.dtst_dblerrors = state->dts_dblerrors; 13899 stat.dtst_killed = 13900 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 13901 stat.dtst_errors = nerrs; 13902 13903 mutex_exit(&dtrace_lock); 13904 13905 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 13906 return (EFAULT); 13907 13908 return (0); 13909 } 13910 13911 case DTRACEIOC_FORMAT: { 13912 dtrace_fmtdesc_t fmt; 13913 char *str; 13914 int len; 13915 13916 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 13917 return (EFAULT); 13918 13919 mutex_enter(&dtrace_lock); 13920 13921 if (fmt.dtfd_format == 0 || 13922 fmt.dtfd_format > state->dts_nformats) { 13923 mutex_exit(&dtrace_lock); 13924 return (EINVAL); 13925 } 13926 13927 /* 13928 * Format strings are allocated contiguously and they are 13929 * never freed; if a format index is less than the number 13930 * of formats, we can assert that the format map is non-NULL 13931 * and that the format for the specified index is non-NULL. 13932 */ 13933 ASSERT(state->dts_formats != NULL); 13934 str = state->dts_formats[fmt.dtfd_format - 1]; 13935 ASSERT(str != NULL); 13936 13937 len = strlen(str) + 1; 13938 13939 if (len > fmt.dtfd_length) { 13940 fmt.dtfd_length = len; 13941 13942 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 13943 mutex_exit(&dtrace_lock); 13944 return (EINVAL); 13945 } 13946 } else { 13947 if (copyout(str, fmt.dtfd_string, len) != 0) { 13948 mutex_exit(&dtrace_lock); 13949 return (EINVAL); 13950 } 13951 } 13952 13953 mutex_exit(&dtrace_lock); 13954 return (0); 13955 } 13956 13957 default: 13958 break; 13959 } 13960 13961 return (ENOTTY); 13962 } 13963 13964 /*ARGSUSED*/ 13965 static int 13966 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 13967 { 13968 dtrace_state_t *state; 13969 13970 switch (cmd) { 13971 case DDI_DETACH: 13972 break; 13973 13974 case DDI_SUSPEND: 13975 return (DDI_SUCCESS); 13976 13977 default: 13978 return (DDI_FAILURE); 13979 } 13980 13981 mutex_enter(&cpu_lock); 13982 mutex_enter(&dtrace_provider_lock); 13983 mutex_enter(&dtrace_lock); 13984 13985 ASSERT(dtrace_opens == 0); 13986 13987 if (dtrace_helpers > 0) { 13988 mutex_exit(&dtrace_provider_lock); 13989 mutex_exit(&dtrace_lock); 13990 mutex_exit(&cpu_lock); 13991 return (DDI_FAILURE); 13992 } 13993 13994 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 13995 mutex_exit(&dtrace_provider_lock); 13996 mutex_exit(&dtrace_lock); 13997 mutex_exit(&cpu_lock); 13998 return (DDI_FAILURE); 13999 } 14000 14001 dtrace_provider = NULL; 14002 14003 if ((state = dtrace_anon_grab()) != NULL) { 14004 /* 14005 * If there were ECBs on this state, the provider should 14006 * have not been allowed to detach; assert that there is 14007 * none. 14008 */ 14009 ASSERT(state->dts_necbs == 0); 14010 dtrace_state_destroy(state); 14011 14012 /* 14013 * If we're being detached with anonymous state, we need to 14014 * indicate to the kernel debugger that DTrace is now inactive. 14015 */ 14016 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 14017 } 14018 14019 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 14020 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 14021 dtrace_cpu_init = NULL; 14022 dtrace_helpers_cleanup = NULL; 14023 dtrace_helpers_fork = NULL; 14024 dtrace_cpustart_init = NULL; 14025 dtrace_cpustart_fini = NULL; 14026 dtrace_debugger_init = NULL; 14027 dtrace_debugger_fini = NULL; 14028 dtrace_kreloc_init = NULL; 14029 dtrace_kreloc_fini = NULL; 14030 dtrace_modload = NULL; 14031 dtrace_modunload = NULL; 14032 14033 mutex_exit(&cpu_lock); 14034 14035 if (dtrace_helptrace_enabled) { 14036 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 14037 dtrace_helptrace_buffer = NULL; 14038 } 14039 14040 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 14041 dtrace_probes = NULL; 14042 dtrace_nprobes = 0; 14043 14044 dtrace_hash_destroy(dtrace_bymod); 14045 dtrace_hash_destroy(dtrace_byfunc); 14046 dtrace_hash_destroy(dtrace_byname); 14047 dtrace_bymod = NULL; 14048 dtrace_byfunc = NULL; 14049 dtrace_byname = NULL; 14050 14051 kmem_cache_destroy(dtrace_state_cache); 14052 vmem_destroy(dtrace_minor); 14053 vmem_destroy(dtrace_arena); 14054 14055 if (dtrace_toxrange != NULL) { 14056 kmem_free(dtrace_toxrange, 14057 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 14058 dtrace_toxrange = NULL; 14059 dtrace_toxranges = 0; 14060 dtrace_toxranges_max = 0; 14061 } 14062 14063 ddi_remove_minor_node(dtrace_devi, NULL); 14064 dtrace_devi = NULL; 14065 14066 ddi_soft_state_fini(&dtrace_softstate); 14067 14068 ASSERT(dtrace_vtime_references == 0); 14069 ASSERT(dtrace_opens == 0); 14070 ASSERT(dtrace_retained == NULL); 14071 14072 mutex_exit(&dtrace_lock); 14073 mutex_exit(&dtrace_provider_lock); 14074 14075 /* 14076 * We don't destroy the task queue until after we have dropped our 14077 * locks (taskq_destroy() may block on running tasks). To prevent 14078 * attempting to do work after we have effectively detached but before 14079 * the task queue has been destroyed, all tasks dispatched via the 14080 * task queue must check that DTrace is still attached before 14081 * performing any operation. 14082 */ 14083 taskq_destroy(dtrace_taskq); 14084 dtrace_taskq = NULL; 14085 14086 return (DDI_SUCCESS); 14087 } 14088 14089 /*ARGSUSED*/ 14090 static int 14091 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 14092 { 14093 int error; 14094 14095 switch (infocmd) { 14096 case DDI_INFO_DEVT2DEVINFO: 14097 *result = (void *)dtrace_devi; 14098 error = DDI_SUCCESS; 14099 break; 14100 case DDI_INFO_DEVT2INSTANCE: 14101 *result = (void *)0; 14102 error = DDI_SUCCESS; 14103 break; 14104 default: 14105 error = DDI_FAILURE; 14106 } 14107 return (error); 14108 } 14109 14110 static struct cb_ops dtrace_cb_ops = { 14111 dtrace_open, /* open */ 14112 dtrace_close, /* close */ 14113 nulldev, /* strategy */ 14114 nulldev, /* print */ 14115 nodev, /* dump */ 14116 nodev, /* read */ 14117 nodev, /* write */ 14118 dtrace_ioctl, /* ioctl */ 14119 nodev, /* devmap */ 14120 nodev, /* mmap */ 14121 nodev, /* segmap */ 14122 nochpoll, /* poll */ 14123 ddi_prop_op, /* cb_prop_op */ 14124 0, /* streamtab */ 14125 D_NEW | D_MP /* Driver compatibility flag */ 14126 }; 14127 14128 static struct dev_ops dtrace_ops = { 14129 DEVO_REV, /* devo_rev */ 14130 0, /* refcnt */ 14131 dtrace_info, /* get_dev_info */ 14132 nulldev, /* identify */ 14133 nulldev, /* probe */ 14134 dtrace_attach, /* attach */ 14135 dtrace_detach, /* detach */ 14136 nodev, /* reset */ 14137 &dtrace_cb_ops, /* driver operations */ 14138 NULL, /* bus operations */ 14139 nodev /* dev power */ 14140 }; 14141 14142 static struct modldrv modldrv = { 14143 &mod_driverops, /* module type (this is a pseudo driver) */ 14144 "Dynamic Tracing", /* name of module */ 14145 &dtrace_ops, /* driver ops */ 14146 }; 14147 14148 static struct modlinkage modlinkage = { 14149 MODREV_1, 14150 (void *)&modldrv, 14151 NULL 14152 }; 14153 14154 int 14155 _init(void) 14156 { 14157 return (mod_install(&modlinkage)); 14158 } 14159 14160 int 14161 _info(struct modinfo *modinfop) 14162 { 14163 return (mod_info(&modlinkage, modinfop)); 14164 } 14165 14166 int 14167 _fini(void) 14168 { 14169 return (mod_remove(&modlinkage)); 14170 } 14171