1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 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_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 188 189 /* 190 * DTrace Locking 191 * DTrace is protected by three (relatively coarse-grained) locks: 192 * 193 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 194 * including enabling state, probes, ECBs, consumer state, helper state, 195 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 196 * probe context is lock-free -- synchronization is handled via the 197 * dtrace_sync() cross call mechanism. 198 * 199 * (2) dtrace_provider_lock is required when manipulating provider state, or 200 * when provider state must be held constant. 201 * 202 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 203 * when meta provider state must be held constant. 204 * 205 * The lock ordering between these three locks is dtrace_meta_lock before 206 * dtrace_provider_lock before dtrace_lock. (In particular, there are 207 * several places where dtrace_provider_lock is held by the framework as it 208 * calls into the providers -- which then call back into the framework, 209 * grabbing dtrace_lock.) 210 * 211 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 212 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 213 * role as a coarse-grained lock; it is acquired before both of these locks. 214 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 215 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 216 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 217 * acquired _between_ dtrace_provider_lock and dtrace_lock. 218 */ 219 static kmutex_t dtrace_lock; /* probe state lock */ 220 static kmutex_t dtrace_provider_lock; /* provider state lock */ 221 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 222 223 /* 224 * DTrace Provider Variables 225 * 226 * These are the variables relating to DTrace as a provider (that is, the 227 * provider of the BEGIN, END, and ERROR probes). 228 */ 229 static dtrace_pattr_t dtrace_provider_attr = { 230 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 231 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 232 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 233 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 234 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 235 }; 236 237 static void 238 dtrace_nullop(void) 239 {} 240 241 static dtrace_pops_t dtrace_provider_ops = { 242 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, 243 (void (*)(void *, struct modctl *))dtrace_nullop, 244 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 245 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 246 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 247 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 248 NULL, 249 NULL, 250 NULL, 251 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 252 }; 253 254 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 255 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 256 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 257 258 /* 259 * DTrace Helper Tracing Variables 260 */ 261 uint32_t dtrace_helptrace_next = 0; 262 uint32_t dtrace_helptrace_nlocals; 263 char *dtrace_helptrace_buffer; 264 int dtrace_helptrace_bufsize = 512 * 1024; 265 266 #ifdef DEBUG 267 int dtrace_helptrace_enabled = 1; 268 #else 269 int dtrace_helptrace_enabled = 0; 270 #endif 271 272 /* 273 * DTrace Error Hashing 274 * 275 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 276 * table. This is very useful for checking coverage of tests that are 277 * expected to induce DIF or DOF processing errors, and may be useful for 278 * debugging problems in the DIF code generator or in DOF generation . The 279 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 280 */ 281 #ifdef DEBUG 282 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 283 static const char *dtrace_errlast; 284 static kthread_t *dtrace_errthread; 285 static kmutex_t dtrace_errlock; 286 #endif 287 288 /* 289 * DTrace Macros and Constants 290 * 291 * These are various macros that are useful in various spots in the 292 * implementation, along with a few random constants that have no meaning 293 * outside of the implementation. There is no real structure to this cpp 294 * mishmash -- but is there ever? 295 */ 296 #define DTRACE_HASHSTR(hash, probe) \ 297 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 298 299 #define DTRACE_HASHNEXT(hash, probe) \ 300 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 301 302 #define DTRACE_HASHPREV(hash, probe) \ 303 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 304 305 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 306 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 307 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 308 309 #define DTRACE_AGGHASHSIZE_SLEW 17 310 311 /* 312 * The key for a thread-local variable consists of the lower 61 bits of the 313 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 314 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 315 * equal to a variable identifier. This is necessary (but not sufficient) to 316 * assure that global associative arrays never collide with thread-local 317 * variables. To guarantee that they cannot collide, we must also define the 318 * order for keying dynamic variables. That order is: 319 * 320 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 321 * 322 * Because the variable-key and the tls-key are in orthogonal spaces, there is 323 * no way for a global variable key signature to match a thread-local key 324 * signature. 325 */ 326 #define DTRACE_TLS_THRKEY(where) { \ 327 uint_t intr = 0; \ 328 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 329 for (; actv; actv >>= 1) \ 330 intr++; \ 331 ASSERT(intr < (1 << 3)); \ 332 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 333 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 334 } 335 336 #define DTRACE_STORE(type, tomax, offset, what) \ 337 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 338 339 #ifndef __i386 340 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 341 if (addr & (size - 1)) { \ 342 *flags |= CPU_DTRACE_BADALIGN; \ 343 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 344 return (0); \ 345 } 346 #else 347 #define DTRACE_ALIGNCHECK(addr, size, flags) 348 #endif 349 350 #define DTRACE_LOADFUNC(bits) \ 351 /*CSTYLED*/ \ 352 uint##bits##_t \ 353 dtrace_load##bits(uintptr_t addr) \ 354 { \ 355 size_t size = bits / NBBY; \ 356 /*CSTYLED*/ \ 357 uint##bits##_t rval; \ 358 int i; \ 359 volatile uint16_t *flags = (volatile uint16_t *) \ 360 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ 361 \ 362 DTRACE_ALIGNCHECK(addr, size, flags); \ 363 \ 364 for (i = 0; i < dtrace_toxranges; i++) { \ 365 if (addr >= dtrace_toxrange[i].dtt_limit) \ 366 continue; \ 367 \ 368 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 369 continue; \ 370 \ 371 /* \ 372 * This address falls within a toxic region; return 0. \ 373 */ \ 374 *flags |= CPU_DTRACE_BADADDR; \ 375 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 376 return (0); \ 377 } \ 378 \ 379 *flags |= CPU_DTRACE_NOFAULT; \ 380 /*CSTYLED*/ \ 381 rval = *((volatile uint##bits##_t *)addr); \ 382 *flags &= ~CPU_DTRACE_NOFAULT; \ 383 \ 384 return (rval); \ 385 } 386 387 #ifdef _LP64 388 #define dtrace_loadptr dtrace_load64 389 #else 390 #define dtrace_loadptr dtrace_load32 391 #endif 392 393 #define DTRACE_DYNHASH_FREE 0 394 #define DTRACE_DYNHASH_SINK 1 395 #define DTRACE_DYNHASH_VALID 2 396 397 #define DTRACE_MATCH_NEXT 0 398 #define DTRACE_MATCH_DONE 1 399 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 400 #define DTRACE_STATE_ALIGN 64 401 402 #define DTRACE_FLAGS2FLT(flags) \ 403 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 404 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 405 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 406 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 407 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 408 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 409 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 410 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 411 DTRACEFLT_UNKNOWN) 412 413 #define DTRACEACT_ISSTRING(act) \ 414 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 415 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 416 417 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 418 static void dtrace_enabling_provide(dtrace_provider_t *); 419 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 420 static void dtrace_enabling_matchall(void); 421 static dtrace_state_t *dtrace_anon_grab(void); 422 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 423 dtrace_state_t *, uint64_t, uint64_t); 424 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 425 static void dtrace_buffer_drop(dtrace_buffer_t *); 426 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 427 dtrace_state_t *, dtrace_mstate_t *); 428 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 429 dtrace_optval_t); 430 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 431 432 /* 433 * DTrace Probe Context Functions 434 * 435 * These functions are called from probe context. Because probe context is 436 * any context in which C may be called, arbitrarily locks may be held, 437 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 438 * As a result, functions called from probe context may only call other DTrace 439 * support functions -- they may not interact at all with the system at large. 440 * (Note that the ASSERT macro is made probe-context safe by redefining it in 441 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 442 * loads are to be performed from probe context, they _must_ be in terms of 443 * the safe dtrace_load*() variants. 444 * 445 * Some functions in this block are not actually called from probe context; 446 * for these functions, there will be a comment above the function reading 447 * "Note: not called from probe context." 448 */ 449 void 450 dtrace_panic(const char *format, ...) 451 { 452 va_list alist; 453 454 va_start(alist, format); 455 dtrace_vpanic(format, alist); 456 va_end(alist); 457 } 458 459 int 460 dtrace_assfail(const char *a, const char *f, int l) 461 { 462 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 463 464 /* 465 * We just need something here that even the most clever compiler 466 * cannot optimize away. 467 */ 468 return (a[(uintptr_t)f]); 469 } 470 471 /* 472 * Atomically increment a specified error counter from probe context. 473 */ 474 static void 475 dtrace_error(uint32_t *counter) 476 { 477 /* 478 * Most counters stored to in probe context are per-CPU counters. 479 * However, there are some error conditions that are sufficiently 480 * arcane that they don't merit per-CPU storage. If these counters 481 * are incremented concurrently on different CPUs, scalability will be 482 * adversely affected -- but we don't expect them to be white-hot in a 483 * correctly constructed enabling... 484 */ 485 uint32_t oval, nval; 486 487 do { 488 oval = *counter; 489 490 if ((nval = oval + 1) == 0) { 491 /* 492 * If the counter would wrap, set it to 1 -- assuring 493 * that the counter is never zero when we have seen 494 * errors. (The counter must be 32-bits because we 495 * aren't guaranteed a 64-bit compare&swap operation.) 496 * To save this code both the infamy of being fingered 497 * by a priggish news story and the indignity of being 498 * the target of a neo-puritan witch trial, we're 499 * carefully avoiding any colorful description of the 500 * likelihood of this condition -- but suffice it to 501 * say that it is only slightly more likely than the 502 * overflow of predicate cache IDs, as discussed in 503 * dtrace_predicate_create(). 504 */ 505 nval = 1; 506 } 507 } while (dtrace_cas32(counter, oval, nval) != oval); 508 } 509 510 /* 511 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 512 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 513 */ 514 DTRACE_LOADFUNC(8) 515 DTRACE_LOADFUNC(16) 516 DTRACE_LOADFUNC(32) 517 DTRACE_LOADFUNC(64) 518 519 static int 520 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 521 { 522 if (dest < mstate->dtms_scratch_base) 523 return (0); 524 525 if (dest + size < dest) 526 return (0); 527 528 if (dest + size > mstate->dtms_scratch_ptr) 529 return (0); 530 531 return (1); 532 } 533 534 static int 535 dtrace_canstore_statvar(uint64_t addr, size_t sz, 536 dtrace_statvar_t **svars, int nsvars) 537 { 538 int i; 539 540 for (i = 0; i < nsvars; i++) { 541 dtrace_statvar_t *svar = svars[i]; 542 543 if (svar == NULL || svar->dtsv_size == 0) 544 continue; 545 546 if (addr - svar->dtsv_data < svar->dtsv_size && 547 addr + sz <= svar->dtsv_data + svar->dtsv_size) 548 return (1); 549 } 550 551 return (0); 552 } 553 554 /* 555 * Check to see if the address is within a memory region to which a store may 556 * be issued. This includes the DTrace scratch areas, and any DTrace variable 557 * region. The caller of dtrace_canstore() is responsible for performing any 558 * alignment checks that are needed before stores are actually executed. 559 */ 560 static int 561 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 562 dtrace_vstate_t *vstate) 563 { 564 uintptr_t a; 565 size_t s; 566 567 /* 568 * First, check to see if the address is in scratch space... 569 */ 570 a = mstate->dtms_scratch_base; 571 s = mstate->dtms_scratch_size; 572 573 if (addr - a < s && addr + sz <= a + s) 574 return (1); 575 576 /* 577 * Now check to see if it's a dynamic variable. This check will pick 578 * up both thread-local variables and any global dynamically-allocated 579 * variables. 580 */ 581 a = (uintptr_t)vstate->dtvs_dynvars.dtds_base; 582 s = vstate->dtvs_dynvars.dtds_size; 583 if (addr - a < s && addr + sz <= a + s) 584 return (1); 585 586 /* 587 * Finally, check the static local and global variables. These checks 588 * take the longest, so we perform them last. 589 */ 590 if (dtrace_canstore_statvar(addr, sz, 591 vstate->dtvs_locals, vstate->dtvs_nlocals)) 592 return (1); 593 594 if (dtrace_canstore_statvar(addr, sz, 595 vstate->dtvs_globals, vstate->dtvs_nglobals)) 596 return (1); 597 598 return (0); 599 } 600 601 /* 602 * Compare two strings using safe loads. 603 */ 604 static int 605 dtrace_strncmp(char *s1, char *s2, size_t limit) 606 { 607 uint8_t c1, c2; 608 volatile uint16_t *flags; 609 610 if (s1 == s2 || limit == 0) 611 return (0); 612 613 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 614 615 do { 616 if (s1 == NULL) { 617 c1 = '\0'; 618 } else { 619 c1 = dtrace_load8((uintptr_t)s1++); 620 } 621 622 if (s2 == NULL) { 623 c2 = '\0'; 624 } else { 625 c2 = dtrace_load8((uintptr_t)s2++); 626 } 627 628 if (c1 != c2) 629 return (c1 - c2); 630 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 631 632 return (0); 633 } 634 635 /* 636 * Compute strlen(s) for a string using safe memory accesses. The additional 637 * len parameter is used to specify a maximum length to ensure completion. 638 */ 639 static size_t 640 dtrace_strlen(const char *s, size_t lim) 641 { 642 uint_t len; 643 644 for (len = 0; len != lim; len++) { 645 if (dtrace_load8((uintptr_t)s++) == '\0') 646 break; 647 } 648 649 return (len); 650 } 651 652 /* 653 * Check if an address falls within a toxic region. 654 */ 655 static int 656 dtrace_istoxic(uintptr_t kaddr, size_t size) 657 { 658 uintptr_t taddr, tsize; 659 int i; 660 661 for (i = 0; i < dtrace_toxranges; i++) { 662 taddr = dtrace_toxrange[i].dtt_base; 663 tsize = dtrace_toxrange[i].dtt_limit - taddr; 664 665 if (kaddr - taddr < tsize) { 666 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 667 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr; 668 return (1); 669 } 670 671 if (taddr - kaddr < size) { 672 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 673 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr; 674 return (1); 675 } 676 } 677 678 return (0); 679 } 680 681 /* 682 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 683 * memory specified by the DIF program. The dst is assumed to be safe memory 684 * that we can store to directly because it is managed by DTrace. As with 685 * standard bcopy, overlapping copies are handled properly. 686 */ 687 static void 688 dtrace_bcopy(const void *src, void *dst, size_t len) 689 { 690 if (len != 0) { 691 uint8_t *s1 = dst; 692 const uint8_t *s2 = src; 693 694 if (s1 <= s2) { 695 do { 696 *s1++ = dtrace_load8((uintptr_t)s2++); 697 } while (--len != 0); 698 } else { 699 s2 += len; 700 s1 += len; 701 702 do { 703 *--s1 = dtrace_load8((uintptr_t)--s2); 704 } while (--len != 0); 705 } 706 } 707 } 708 709 /* 710 * Copy src to dst using safe memory accesses, up to either the specified 711 * length, or the point that a nul byte is encountered. The src is assumed to 712 * be unsafe memory specified by the DIF program. The dst is assumed to be 713 * safe memory that we can store to directly because it is managed by DTrace. 714 * Unlike dtrace_bcopy(), overlapping regions are not handled. 715 */ 716 static void 717 dtrace_strcpy(const void *src, void *dst, size_t len) 718 { 719 if (len != 0) { 720 uint8_t *s1 = dst, c; 721 const uint8_t *s2 = src; 722 723 do { 724 *s1++ = c = dtrace_load8((uintptr_t)s2++); 725 } while (--len != 0 && c != '\0'); 726 } 727 } 728 729 /* 730 * Copy src to dst, deriving the size and type from the specified (BYREF) 731 * variable type. The src is assumed to be unsafe memory specified by the DIF 732 * program. The dst is assumed to be DTrace variable memory that is of the 733 * specified type; we assume that we can store to directly. 734 */ 735 static void 736 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 737 { 738 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 739 740 if (type->dtdt_kind == DIF_TYPE_STRING) { 741 dtrace_strcpy(src, dst, type->dtdt_size); 742 } else { 743 dtrace_bcopy(src, dst, type->dtdt_size); 744 } 745 } 746 747 /* 748 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 749 * unsafe memory specified by the DIF program. The s2 data is assumed to be 750 * safe memory that we can access directly because it is managed by DTrace. 751 */ 752 static int 753 dtrace_bcmp(const void *s1, const void *s2, size_t len) 754 { 755 volatile uint16_t *flags; 756 757 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 758 759 if (s1 == s2) 760 return (0); 761 762 if (s1 == NULL || s2 == NULL) 763 return (1); 764 765 if (s1 != s2 && len != 0) { 766 const uint8_t *ps1 = s1; 767 const uint8_t *ps2 = s2; 768 769 do { 770 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 771 return (1); 772 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 773 } 774 return (0); 775 } 776 777 /* 778 * Zero the specified region using a simple byte-by-byte loop. Note that this 779 * is for safe DTrace-managed memory only. 780 */ 781 static void 782 dtrace_bzero(void *dst, size_t len) 783 { 784 uchar_t *cp; 785 786 for (cp = dst; len != 0; len--) 787 *cp++ = 0; 788 } 789 790 /* 791 * This privilege check should be used by actions and subroutines to 792 * verify that the user credentials of the process that enabled the 793 * invoking ECB match the target credentials 794 */ 795 static int 796 dtrace_priv_proc_common_user(dtrace_state_t *state) 797 { 798 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 799 800 /* 801 * We should always have a non-NULL state cred here, since if cred 802 * is null (anonymous tracing), we fast-path bypass this routine. 803 */ 804 ASSERT(s_cr != NULL); 805 806 if ((cr = CRED()) != NULL && 807 s_cr->cr_uid == cr->cr_uid && 808 s_cr->cr_uid == cr->cr_ruid && 809 s_cr->cr_uid == cr->cr_suid && 810 s_cr->cr_gid == cr->cr_gid && 811 s_cr->cr_gid == cr->cr_rgid && 812 s_cr->cr_gid == cr->cr_sgid) 813 return (1); 814 815 return (0); 816 } 817 818 /* 819 * This privilege check should be used by actions and subroutines to 820 * verify that the zone of the process that enabled the invoking ECB 821 * matches the target credentials 822 */ 823 static int 824 dtrace_priv_proc_common_zone(dtrace_state_t *state) 825 { 826 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 827 828 /* 829 * We should always have a non-NULL state cred here, since if cred 830 * is null (anonymous tracing), we fast-path bypass this routine. 831 */ 832 ASSERT(s_cr != NULL); 833 834 if ((cr = CRED()) != NULL && 835 s_cr->cr_zone == cr->cr_zone) 836 return (1); 837 838 return (0); 839 } 840 841 /* 842 * This privilege check should be used by actions and subroutines to 843 * verify that the process has not setuid or changed credentials. 844 */ 845 static int 846 dtrace_priv_proc_common_nocd() 847 { 848 proc_t *proc; 849 850 if ((proc = ttoproc(curthread)) != NULL && 851 !(proc->p_flag & SNOCD)) 852 return (1); 853 854 return (0); 855 } 856 857 static int 858 dtrace_priv_proc_destructive(dtrace_state_t *state) 859 { 860 int action = state->dts_cred.dcr_action; 861 862 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 863 dtrace_priv_proc_common_zone(state) == 0) 864 goto bad; 865 866 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 867 dtrace_priv_proc_common_user(state) == 0) 868 goto bad; 869 870 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 871 dtrace_priv_proc_common_nocd() == 0) 872 goto bad; 873 874 return (1); 875 876 bad: 877 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 878 879 return (0); 880 } 881 882 static int 883 dtrace_priv_proc_control(dtrace_state_t *state) 884 { 885 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 886 return (1); 887 888 if (dtrace_priv_proc_common_zone(state) && 889 dtrace_priv_proc_common_user(state) && 890 dtrace_priv_proc_common_nocd()) 891 return (1); 892 893 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 894 895 return (0); 896 } 897 898 static int 899 dtrace_priv_proc(dtrace_state_t *state) 900 { 901 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 902 return (1); 903 904 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 905 906 return (0); 907 } 908 909 static int 910 dtrace_priv_kernel(dtrace_state_t *state) 911 { 912 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 913 return (1); 914 915 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 916 917 return (0); 918 } 919 920 static int 921 dtrace_priv_kernel_destructive(dtrace_state_t *state) 922 { 923 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 924 return (1); 925 926 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 927 928 return (0); 929 } 930 931 /* 932 * Note: not called from probe context. This function is called 933 * asynchronously (and at a regular interval) from outside of probe context to 934 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 935 * cleaning is explained in detail in <sys/dtrace_impl.h>. 936 */ 937 void 938 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 939 { 940 dtrace_dynvar_t *dirty; 941 dtrace_dstate_percpu_t *dcpu; 942 int i, work = 0; 943 944 for (i = 0; i < NCPU; i++) { 945 dcpu = &dstate->dtds_percpu[i]; 946 947 ASSERT(dcpu->dtdsc_rinsing == NULL); 948 949 /* 950 * If the dirty list is NULL, there is no dirty work to do. 951 */ 952 if (dcpu->dtdsc_dirty == NULL) 953 continue; 954 955 /* 956 * If the clean list is non-NULL, then we're not going to do 957 * any work for this CPU -- it means that there has not been 958 * a dtrace_dynvar() allocation on this CPU (or from this CPU) 959 * since the last time we cleaned house. 960 */ 961 if (dcpu->dtdsc_clean != NULL) 962 continue; 963 964 work = 1; 965 966 /* 967 * Atomically move the dirty list aside. 968 */ 969 do { 970 dirty = dcpu->dtdsc_dirty; 971 972 /* 973 * Before we zap the dirty list, set the rinsing list. 974 * (This allows for a potential assertion in 975 * dtrace_dynvar(): if a free dynamic variable appears 976 * on a hash chain, either the dirty list or the 977 * rinsing list for some CPU must be non-NULL.) 978 */ 979 dcpu->dtdsc_rinsing = dirty; 980 dtrace_membar_producer(); 981 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 982 dirty, NULL) != dirty); 983 } 984 985 if (!work) { 986 /* 987 * We have no work to do; we can simply return. 988 */ 989 return; 990 } 991 992 dtrace_sync(); 993 994 for (i = 0; i < NCPU; i++) { 995 dcpu = &dstate->dtds_percpu[i]; 996 997 if (dcpu->dtdsc_rinsing == NULL) 998 continue; 999 1000 /* 1001 * We are now guaranteed that no hash chain contains a pointer 1002 * into this dirty list; we can make it clean. 1003 */ 1004 ASSERT(dcpu->dtdsc_clean == NULL); 1005 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1006 dcpu->dtdsc_rinsing = NULL; 1007 } 1008 1009 /* 1010 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1011 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1012 * This prevents a race whereby a CPU incorrectly decides that 1013 * the state should be something other than DTRACE_DSTATE_CLEAN 1014 * after dtrace_dynvar_clean() has completed. 1015 */ 1016 dtrace_sync(); 1017 1018 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1019 } 1020 1021 /* 1022 * Depending on the value of the op parameter, this function looks-up, 1023 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1024 * allocation is requested, this function will return a pointer to a 1025 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1026 * variable can be allocated. If NULL is returned, the appropriate counter 1027 * will be incremented. 1028 */ 1029 dtrace_dynvar_t * 1030 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1031 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op) 1032 { 1033 uint64_t hashval = DTRACE_DYNHASH_VALID; 1034 dtrace_dynhash_t *hash = dstate->dtds_hash; 1035 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1036 processorid_t me = CPU->cpu_id, cpu = me; 1037 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1038 size_t bucket, ksize; 1039 size_t chunksize = dstate->dtds_chunksize; 1040 uintptr_t kdata, lock, nstate; 1041 uint_t i; 1042 1043 ASSERT(nkeys != 0); 1044 1045 /* 1046 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1047 * algorithm. For the by-value portions, we perform the algorithm in 1048 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1049 * bit, and seems to have only a minute effect on distribution. For 1050 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1051 * over each referenced byte. It's painful to do this, but it's much 1052 * better than pathological hash distribution. The efficacy of the 1053 * hashing algorithm (and a comparison with other algorithms) may be 1054 * found by running the ::dtrace_dynstat MDB dcmd. 1055 */ 1056 for (i = 0; i < nkeys; i++) { 1057 if (key[i].dttk_size == 0) { 1058 uint64_t val = key[i].dttk_value; 1059 1060 hashval += (val >> 48) & 0xffff; 1061 hashval += (hashval << 10); 1062 hashval ^= (hashval >> 6); 1063 1064 hashval += (val >> 32) & 0xffff; 1065 hashval += (hashval << 10); 1066 hashval ^= (hashval >> 6); 1067 1068 hashval += (val >> 16) & 0xffff; 1069 hashval += (hashval << 10); 1070 hashval ^= (hashval >> 6); 1071 1072 hashval += val & 0xffff; 1073 hashval += (hashval << 10); 1074 hashval ^= (hashval >> 6); 1075 } else { 1076 /* 1077 * This is incredibly painful, but it beats the hell 1078 * out of the alternative. 1079 */ 1080 uint64_t j, size = key[i].dttk_size; 1081 uintptr_t base = (uintptr_t)key[i].dttk_value; 1082 1083 for (j = 0; j < size; j++) { 1084 hashval += dtrace_load8(base + j); 1085 hashval += (hashval << 10); 1086 hashval ^= (hashval >> 6); 1087 } 1088 } 1089 } 1090 1091 hashval += (hashval << 3); 1092 hashval ^= (hashval >> 11); 1093 hashval += (hashval << 15); 1094 1095 /* 1096 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1097 * comes out to be one of our two sentinel hash values. If this 1098 * actually happens, we set the hashval to be a value known to be a 1099 * non-sentinel value. 1100 */ 1101 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1102 hashval = DTRACE_DYNHASH_VALID; 1103 1104 /* 1105 * Yes, it's painful to do a divide here. If the cycle count becomes 1106 * important here, tricks can be pulled to reduce it. (However, it's 1107 * critical that hash collisions be kept to an absolute minimum; 1108 * they're much more painful than a divide.) It's better to have a 1109 * solution that generates few collisions and still keeps things 1110 * relatively simple. 1111 */ 1112 bucket = hashval % dstate->dtds_hashsize; 1113 1114 if (op == DTRACE_DYNVAR_DEALLOC) { 1115 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1116 1117 for (;;) { 1118 while ((lock = *lockp) & 1) 1119 continue; 1120 1121 if (dtrace_casptr((void *)lockp, 1122 (void *)lock, (void *)(lock + 1)) == (void *)lock) 1123 break; 1124 } 1125 1126 dtrace_membar_producer(); 1127 } 1128 1129 top: 1130 prev = NULL; 1131 lock = hash[bucket].dtdh_lock; 1132 1133 dtrace_membar_consumer(); 1134 1135 start = hash[bucket].dtdh_chain; 1136 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1137 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1138 op != DTRACE_DYNVAR_DEALLOC)); 1139 1140 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1141 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1142 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1143 1144 if (dvar->dtdv_hashval != hashval) { 1145 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1146 /* 1147 * We've reached the sink, and therefore the 1148 * end of the hash chain; we can kick out of 1149 * the loop knowing that we have seen a valid 1150 * snapshot of state. 1151 */ 1152 ASSERT(dvar->dtdv_next == NULL); 1153 ASSERT(dvar == &dtrace_dynhash_sink); 1154 break; 1155 } 1156 1157 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1158 /* 1159 * We've gone off the rails: somewhere along 1160 * the line, one of the members of this hash 1161 * chain was deleted. Note that we could also 1162 * detect this by simply letting this loop run 1163 * to completion, as we would eventually hit 1164 * the end of the dirty list. However, we 1165 * want to avoid running the length of the 1166 * dirty list unnecessarily (it might be quite 1167 * long), so we catch this as early as 1168 * possible by detecting the hash marker. In 1169 * this case, we simply set dvar to NULL and 1170 * break; the conditional after the loop will 1171 * send us back to top. 1172 */ 1173 dvar = NULL; 1174 break; 1175 } 1176 1177 goto next; 1178 } 1179 1180 if (dtuple->dtt_nkeys != nkeys) 1181 goto next; 1182 1183 for (i = 0; i < nkeys; i++, dkey++) { 1184 if (dkey->dttk_size != key[i].dttk_size) 1185 goto next; /* size or type mismatch */ 1186 1187 if (dkey->dttk_size != 0) { 1188 if (dtrace_bcmp( 1189 (void *)(uintptr_t)key[i].dttk_value, 1190 (void *)(uintptr_t)dkey->dttk_value, 1191 dkey->dttk_size)) 1192 goto next; 1193 } else { 1194 if (dkey->dttk_value != key[i].dttk_value) 1195 goto next; 1196 } 1197 } 1198 1199 if (op != DTRACE_DYNVAR_DEALLOC) 1200 return (dvar); 1201 1202 ASSERT(dvar->dtdv_next == NULL || 1203 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1204 1205 if (prev != NULL) { 1206 ASSERT(hash[bucket].dtdh_chain != dvar); 1207 ASSERT(start != dvar); 1208 ASSERT(prev->dtdv_next == dvar); 1209 prev->dtdv_next = dvar->dtdv_next; 1210 } else { 1211 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1212 start, dvar->dtdv_next) != start) { 1213 /* 1214 * We have failed to atomically swing the 1215 * hash table head pointer, presumably because 1216 * of a conflicting allocation on another CPU. 1217 * We need to reread the hash chain and try 1218 * again. 1219 */ 1220 goto top; 1221 } 1222 } 1223 1224 dtrace_membar_producer(); 1225 1226 /* 1227 * Now set the hash value to indicate that it's free. 1228 */ 1229 ASSERT(hash[bucket].dtdh_chain != dvar); 1230 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1231 1232 dtrace_membar_producer(); 1233 1234 /* 1235 * Set the next pointer to point at the dirty list, and 1236 * atomically swing the dirty pointer to the newly freed dvar. 1237 */ 1238 do { 1239 next = dcpu->dtdsc_dirty; 1240 dvar->dtdv_next = next; 1241 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1242 1243 /* 1244 * Finally, unlock this hash bucket. 1245 */ 1246 ASSERT(hash[bucket].dtdh_lock == lock); 1247 ASSERT(lock & 1); 1248 hash[bucket].dtdh_lock++; 1249 1250 return (NULL); 1251 next: 1252 prev = dvar; 1253 continue; 1254 } 1255 1256 if (dvar == NULL) { 1257 /* 1258 * If dvar is NULL, it is because we went off the rails: 1259 * one of the elements that we traversed in the hash chain 1260 * was deleted while we were traversing it. In this case, 1261 * we assert that we aren't doing a dealloc (deallocs lock 1262 * the hash bucket to prevent themselves from racing with 1263 * one another), and retry the hash chain traversal. 1264 */ 1265 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1266 goto top; 1267 } 1268 1269 if (op != DTRACE_DYNVAR_ALLOC) { 1270 /* 1271 * If we are not to allocate a new variable, we want to 1272 * return NULL now. Before we return, check that the value 1273 * of the lock word hasn't changed. If it has, we may have 1274 * seen an inconsistent snapshot. 1275 */ 1276 if (op == DTRACE_DYNVAR_NOALLOC) { 1277 if (hash[bucket].dtdh_lock != lock) 1278 goto top; 1279 } else { 1280 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1281 ASSERT(hash[bucket].dtdh_lock == lock); 1282 ASSERT(lock & 1); 1283 hash[bucket].dtdh_lock++; 1284 } 1285 1286 return (NULL); 1287 } 1288 1289 /* 1290 * We need to allocate a new dynamic variable. The size we need is the 1291 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1292 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1293 * the size of any referred-to data (dsize). We then round the final 1294 * size up to the chunksize for allocation. 1295 */ 1296 for (ksize = 0, i = 0; i < nkeys; i++) 1297 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1298 1299 /* 1300 * This should be pretty much impossible, but could happen if, say, 1301 * strange DIF specified the tuple. Ideally, this should be an 1302 * assertion and not an error condition -- but that requires that the 1303 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1304 * bullet-proof. (That is, it must not be able to be fooled by 1305 * malicious DIF.) Given the lack of backwards branches in DIF, 1306 * solving this would presumably not amount to solving the Halting 1307 * Problem -- but it still seems awfully hard. 1308 */ 1309 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1310 ksize + dsize > chunksize) { 1311 dcpu->dtdsc_drops++; 1312 return (NULL); 1313 } 1314 1315 nstate = DTRACE_DSTATE_EMPTY; 1316 1317 do { 1318 retry: 1319 free = dcpu->dtdsc_free; 1320 1321 if (free == NULL) { 1322 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1323 void *rval; 1324 1325 if (clean == NULL) { 1326 /* 1327 * We're out of dynamic variable space on 1328 * this CPU. Unless we have tried all CPUs, 1329 * we'll try to allocate from a different 1330 * CPU. 1331 */ 1332 switch (dstate->dtds_state) { 1333 case DTRACE_DSTATE_CLEAN: { 1334 void *sp = &dstate->dtds_state; 1335 1336 if (++cpu >= NCPU) 1337 cpu = 0; 1338 1339 if (dcpu->dtdsc_dirty != NULL && 1340 nstate == DTRACE_DSTATE_EMPTY) 1341 nstate = DTRACE_DSTATE_DIRTY; 1342 1343 if (dcpu->dtdsc_rinsing != NULL) 1344 nstate = DTRACE_DSTATE_RINSING; 1345 1346 dcpu = &dstate->dtds_percpu[cpu]; 1347 1348 if (cpu != me) 1349 goto retry; 1350 1351 (void) dtrace_cas32(sp, 1352 DTRACE_DSTATE_CLEAN, nstate); 1353 1354 /* 1355 * To increment the correct bean 1356 * counter, take another lap. 1357 */ 1358 goto retry; 1359 } 1360 1361 case DTRACE_DSTATE_DIRTY: 1362 dcpu->dtdsc_dirty_drops++; 1363 break; 1364 1365 case DTRACE_DSTATE_RINSING: 1366 dcpu->dtdsc_rinsing_drops++; 1367 break; 1368 1369 case DTRACE_DSTATE_EMPTY: 1370 dcpu->dtdsc_drops++; 1371 break; 1372 } 1373 1374 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1375 return (NULL); 1376 } 1377 1378 /* 1379 * The clean list appears to be non-empty. We want to 1380 * move the clean list to the free list; we start by 1381 * moving the clean pointer aside. 1382 */ 1383 if (dtrace_casptr(&dcpu->dtdsc_clean, 1384 clean, NULL) != clean) { 1385 /* 1386 * We are in one of two situations: 1387 * 1388 * (a) The clean list was switched to the 1389 * free list by another CPU. 1390 * 1391 * (b) The clean list was added to by the 1392 * cleansing cyclic. 1393 * 1394 * In either of these situations, we can 1395 * just reattempt the free list allocation. 1396 */ 1397 goto retry; 1398 } 1399 1400 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1401 1402 /* 1403 * Now we'll move the clean list to the free list. 1404 * It's impossible for this to fail: the only way 1405 * the free list can be updated is through this 1406 * code path, and only one CPU can own the clean list. 1407 * Thus, it would only be possible for this to fail if 1408 * this code were racing with dtrace_dynvar_clean(). 1409 * (That is, if dtrace_dynvar_clean() updated the clean 1410 * list, and we ended up racing to update the free 1411 * list.) This race is prevented by the dtrace_sync() 1412 * in dtrace_dynvar_clean() -- which flushes the 1413 * owners of the clean lists out before resetting 1414 * the clean lists. 1415 */ 1416 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1417 ASSERT(rval == NULL); 1418 goto retry; 1419 } 1420 1421 dvar = free; 1422 new_free = dvar->dtdv_next; 1423 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1424 1425 /* 1426 * We have now allocated a new chunk. We copy the tuple keys into the 1427 * tuple array and copy any referenced key data into the data space 1428 * following the tuple array. As we do this, we relocate dttk_value 1429 * in the final tuple to point to the key data address in the chunk. 1430 */ 1431 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1432 dvar->dtdv_data = (void *)(kdata + ksize); 1433 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1434 1435 for (i = 0; i < nkeys; i++) { 1436 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1437 size_t kesize = key[i].dttk_size; 1438 1439 if (kesize != 0) { 1440 dtrace_bcopy( 1441 (const void *)(uintptr_t)key[i].dttk_value, 1442 (void *)kdata, kesize); 1443 dkey->dttk_value = kdata; 1444 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1445 } else { 1446 dkey->dttk_value = key[i].dttk_value; 1447 } 1448 1449 dkey->dttk_size = kesize; 1450 } 1451 1452 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1453 dvar->dtdv_hashval = hashval; 1454 dvar->dtdv_next = start; 1455 1456 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1457 return (dvar); 1458 1459 /* 1460 * The cas has failed. Either another CPU is adding an element to 1461 * this hash chain, or another CPU is deleting an element from this 1462 * hash chain. The simplest way to deal with both of these cases 1463 * (though not necessarily the most efficient) is to free our 1464 * allocated block and tail-call ourselves. Note that the free is 1465 * to the dirty list and _not_ to the free list. This is to prevent 1466 * races with allocators, above. 1467 */ 1468 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1469 1470 dtrace_membar_producer(); 1471 1472 do { 1473 free = dcpu->dtdsc_dirty; 1474 dvar->dtdv_next = free; 1475 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1476 1477 return (dtrace_dynvar(dstate, nkeys, key, dsize, op)); 1478 } 1479 1480 /*ARGSUSED*/ 1481 static void 1482 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1483 { 1484 if (nval < *oval) 1485 *oval = nval; 1486 } 1487 1488 /*ARGSUSED*/ 1489 static void 1490 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1491 { 1492 if (nval > *oval) 1493 *oval = nval; 1494 } 1495 1496 static void 1497 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1498 { 1499 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1500 int64_t val = (int64_t)nval; 1501 1502 if (val < 0) { 1503 for (i = 0; i < zero; i++) { 1504 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1505 quanta[i] += incr; 1506 return; 1507 } 1508 } 1509 } else { 1510 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1511 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1512 quanta[i - 1] += incr; 1513 return; 1514 } 1515 } 1516 1517 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1518 return; 1519 } 1520 1521 ASSERT(0); 1522 } 1523 1524 static void 1525 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1526 { 1527 uint64_t arg = *lquanta++; 1528 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1529 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1530 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1531 int32_t val = (int32_t)nval, level; 1532 1533 ASSERT(step != 0); 1534 ASSERT(levels != 0); 1535 1536 if (val < base) { 1537 /* 1538 * This is an underflow. 1539 */ 1540 lquanta[0] += incr; 1541 return; 1542 } 1543 1544 level = (val - base) / step; 1545 1546 if (level < levels) { 1547 lquanta[level + 1] += incr; 1548 return; 1549 } 1550 1551 /* 1552 * This is an overflow. 1553 */ 1554 lquanta[levels + 1] += incr; 1555 } 1556 1557 /*ARGSUSED*/ 1558 static void 1559 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 1560 { 1561 data[0]++; 1562 data[1] += nval; 1563 } 1564 1565 /*ARGSUSED*/ 1566 static void 1567 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 1568 { 1569 *oval = *oval + 1; 1570 } 1571 1572 /*ARGSUSED*/ 1573 static void 1574 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 1575 { 1576 *oval += nval; 1577 } 1578 1579 /* 1580 * Aggregate given the tuple in the principal data buffer, and the aggregating 1581 * action denoted by the specified dtrace_aggregation_t. The aggregation 1582 * buffer is specified as the buf parameter. This routine does not return 1583 * failure; if there is no space in the aggregation buffer, the data will be 1584 * dropped, and a corresponding counter incremented. 1585 */ 1586 static void 1587 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 1588 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 1589 { 1590 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 1591 uint32_t i, ndx, size, fsize; 1592 uint32_t align = sizeof (uint64_t) - 1; 1593 dtrace_aggbuffer_t *agb; 1594 dtrace_aggkey_t *key; 1595 uint32_t hashval = 0, limit, isstr; 1596 caddr_t tomax, data, kdata; 1597 dtrace_actkind_t action; 1598 dtrace_action_t *act; 1599 uintptr_t offs; 1600 1601 if (buf == NULL) 1602 return; 1603 1604 if (!agg->dtag_hasarg) { 1605 /* 1606 * Currently, only quantize() and lquantize() take additional 1607 * arguments, and they have the same semantics: an increment 1608 * value that defaults to 1 when not present. If additional 1609 * aggregating actions take arguments, the setting of the 1610 * default argument value will presumably have to become more 1611 * sophisticated... 1612 */ 1613 arg = 1; 1614 } 1615 1616 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 1617 size = rec->dtrd_offset - agg->dtag_base; 1618 fsize = size + rec->dtrd_size; 1619 1620 ASSERT(dbuf->dtb_tomax != NULL); 1621 data = dbuf->dtb_tomax + offset + agg->dtag_base; 1622 1623 if ((tomax = buf->dtb_tomax) == NULL) { 1624 dtrace_buffer_drop(buf); 1625 return; 1626 } 1627 1628 /* 1629 * The metastructure is always at the bottom of the buffer. 1630 */ 1631 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 1632 sizeof (dtrace_aggbuffer_t)); 1633 1634 if (buf->dtb_offset == 0) { 1635 /* 1636 * We just kludge up approximately 1/8th of the size to be 1637 * buckets. If this guess ends up being routinely 1638 * off-the-mark, we may need to dynamically readjust this 1639 * based on past performance. 1640 */ 1641 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 1642 1643 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 1644 (uintptr_t)tomax || hashsize == 0) { 1645 /* 1646 * We've been given a ludicrously small buffer; 1647 * increment our drop count and leave. 1648 */ 1649 dtrace_buffer_drop(buf); 1650 return; 1651 } 1652 1653 /* 1654 * And now, a pathetic attempt to try to get a an odd (or 1655 * perchance, a prime) hash size for better hash distribution. 1656 */ 1657 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 1658 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 1659 1660 agb->dtagb_hashsize = hashsize; 1661 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 1662 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 1663 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 1664 1665 for (i = 0; i < agb->dtagb_hashsize; i++) 1666 agb->dtagb_hash[i] = NULL; 1667 } 1668 1669 ASSERT(agg->dtag_first != NULL); 1670 ASSERT(agg->dtag_first->dta_intuple); 1671 1672 /* 1673 * Calculate the hash value based on the key. Note that we _don't_ 1674 * include the aggid in the hashing (but we will store it as part of 1675 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 1676 * algorithm: a simple, quick algorithm that has no known funnels, and 1677 * gets good distribution in practice. The efficacy of the hashing 1678 * algorithm (and a comparison with other algorithms) may be found by 1679 * running the ::dtrace_aggstat MDB dcmd. 1680 */ 1681 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 1682 i = act->dta_rec.dtrd_offset - agg->dtag_base; 1683 limit = i + act->dta_rec.dtrd_size; 1684 ASSERT(limit <= size); 1685 isstr = DTRACEACT_ISSTRING(act); 1686 1687 for (; i < limit; i++) { 1688 hashval += data[i]; 1689 hashval += (hashval << 10); 1690 hashval ^= (hashval >> 6); 1691 1692 if (isstr && data[i] == '\0') 1693 break; 1694 } 1695 } 1696 1697 hashval += (hashval << 3); 1698 hashval ^= (hashval >> 11); 1699 hashval += (hashval << 15); 1700 1701 /* 1702 * Yes, the divide here is expensive -- but it's generally the least 1703 * of the performance issues given the amount of data that we iterate 1704 * over to compute hash values, compare data, etc. 1705 */ 1706 ndx = hashval % agb->dtagb_hashsize; 1707 1708 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 1709 ASSERT((caddr_t)key >= tomax); 1710 ASSERT((caddr_t)key < tomax + buf->dtb_size); 1711 1712 if (hashval != key->dtak_hashval || key->dtak_size != size) 1713 continue; 1714 1715 kdata = key->dtak_data; 1716 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 1717 1718 for (act = agg->dtag_first; act->dta_intuple; 1719 act = act->dta_next) { 1720 i = act->dta_rec.dtrd_offset - agg->dtag_base; 1721 limit = i + act->dta_rec.dtrd_size; 1722 ASSERT(limit <= size); 1723 isstr = DTRACEACT_ISSTRING(act); 1724 1725 for (; i < limit; i++) { 1726 if (kdata[i] != data[i]) 1727 goto next; 1728 1729 if (isstr && data[i] == '\0') 1730 break; 1731 } 1732 } 1733 1734 if (action != key->dtak_action) { 1735 /* 1736 * We are aggregating on the same value in the same 1737 * aggregation with two different aggregating actions. 1738 * (This should have been picked up in the compiler, 1739 * so we may be dealing with errant or devious DIF.) 1740 * This is an error condition; we indicate as much, 1741 * and return. 1742 */ 1743 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 1744 return; 1745 } 1746 1747 /* 1748 * This is a hit: we need to apply the aggregator to 1749 * the value at this key. 1750 */ 1751 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 1752 return; 1753 next: 1754 continue; 1755 } 1756 1757 /* 1758 * We didn't find it. We need to allocate some zero-filled space, 1759 * link it into the hash table appropriately, and apply the aggregator 1760 * to the (zero-filled) value. 1761 */ 1762 offs = buf->dtb_offset; 1763 while (offs & (align - 1)) 1764 offs += sizeof (uint32_t); 1765 1766 /* 1767 * If we don't have enough room to both allocate a new key _and_ 1768 * its associated data, increment the drop count and return. 1769 */ 1770 if ((uintptr_t)tomax + offs + fsize > 1771 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 1772 dtrace_buffer_drop(buf); 1773 return; 1774 } 1775 1776 /*CONSTCOND*/ 1777 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 1778 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 1779 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 1780 1781 key->dtak_data = kdata = tomax + offs; 1782 buf->dtb_offset = offs + fsize; 1783 1784 /* 1785 * Now copy the data across. 1786 */ 1787 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 1788 1789 for (i = sizeof (dtrace_aggid_t); i < size; i++) 1790 kdata[i] = data[i]; 1791 1792 /* 1793 * Because strings are not zeroed out by default, we need to iterate 1794 * looking for actions that store strings, and we need to explicitly 1795 * pad these strings out with zeroes. 1796 */ 1797 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 1798 int nul; 1799 1800 if (!DTRACEACT_ISSTRING(act)) 1801 continue; 1802 1803 i = act->dta_rec.dtrd_offset - agg->dtag_base; 1804 limit = i + act->dta_rec.dtrd_size; 1805 ASSERT(limit <= size); 1806 1807 for (nul = 0; i < limit; i++) { 1808 if (nul) { 1809 kdata[i] = '\0'; 1810 continue; 1811 } 1812 1813 if (data[i] != '\0') 1814 continue; 1815 1816 nul = 1; 1817 } 1818 } 1819 1820 for (i = size; i < fsize; i++) 1821 kdata[i] = 0; 1822 1823 key->dtak_hashval = hashval; 1824 key->dtak_size = size; 1825 key->dtak_action = action; 1826 key->dtak_next = agb->dtagb_hash[ndx]; 1827 agb->dtagb_hash[ndx] = key; 1828 1829 /* 1830 * Finally, apply the aggregator. 1831 */ 1832 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 1833 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 1834 } 1835 1836 /* 1837 * Given consumer state, this routine finds a speculation in the INACTIVE 1838 * state and transitions it into the ACTIVE state. If there is no speculation 1839 * in the INACTIVE state, 0 is returned. In this case, no error counter is 1840 * incremented -- it is up to the caller to take appropriate action. 1841 */ 1842 static int 1843 dtrace_speculation(dtrace_state_t *state) 1844 { 1845 int i = 0; 1846 dtrace_speculation_state_t current; 1847 uint32_t *stat = &state->dts_speculations_unavail, count; 1848 1849 while (i < state->dts_nspeculations) { 1850 dtrace_speculation_t *spec = &state->dts_speculations[i]; 1851 1852 current = spec->dtsp_state; 1853 1854 if (current != DTRACESPEC_INACTIVE) { 1855 if (current == DTRACESPEC_COMMITTINGMANY || 1856 current == DTRACESPEC_COMMITTING || 1857 current == DTRACESPEC_DISCARDING) 1858 stat = &state->dts_speculations_busy; 1859 i++; 1860 continue; 1861 } 1862 1863 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 1864 current, DTRACESPEC_ACTIVE) == current) 1865 return (i + 1); 1866 } 1867 1868 /* 1869 * We couldn't find a speculation. If we found as much as a single 1870 * busy speculation buffer, we'll attribute this failure as "busy" 1871 * instead of "unavail". 1872 */ 1873 do { 1874 count = *stat; 1875 } while (dtrace_cas32(stat, count, count + 1) != count); 1876 1877 return (0); 1878 } 1879 1880 /* 1881 * This routine commits an active speculation. If the specified speculation 1882 * is not in a valid state to perform a commit(), this routine will silently do 1883 * nothing. The state of the specified speculation is transitioned according 1884 * to the state transition diagram outlined in <sys/dtrace_impl.h> 1885 */ 1886 static void 1887 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 1888 dtrace_specid_t which) 1889 { 1890 dtrace_speculation_t *spec; 1891 dtrace_buffer_t *src, *dest; 1892 uintptr_t daddr, saddr, dlimit; 1893 dtrace_speculation_state_t current, new; 1894 intptr_t offs; 1895 1896 if (which == 0) 1897 return; 1898 1899 if (which > state->dts_nspeculations) { 1900 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 1901 return; 1902 } 1903 1904 spec = &state->dts_speculations[which - 1]; 1905 src = &spec->dtsp_buffer[cpu]; 1906 dest = &state->dts_buffer[cpu]; 1907 1908 do { 1909 current = spec->dtsp_state; 1910 1911 if (current == DTRACESPEC_COMMITTINGMANY) 1912 break; 1913 1914 switch (current) { 1915 case DTRACESPEC_INACTIVE: 1916 case DTRACESPEC_DISCARDING: 1917 return; 1918 1919 case DTRACESPEC_COMMITTING: 1920 /* 1921 * This is only possible if we are (a) commit()'ing 1922 * without having done a prior speculate() on this CPU 1923 * and (b) racing with another commit() on a different 1924 * CPU. There's nothing to do -- we just assert that 1925 * our offset is 0. 1926 */ 1927 ASSERT(src->dtb_offset == 0); 1928 return; 1929 1930 case DTRACESPEC_ACTIVE: 1931 new = DTRACESPEC_COMMITTING; 1932 break; 1933 1934 case DTRACESPEC_ACTIVEONE: 1935 /* 1936 * This speculation is active on one CPU. If our 1937 * buffer offset is non-zero, we know that the one CPU 1938 * must be us. Otherwise, we are committing on a 1939 * different CPU from the speculate(), and we must 1940 * rely on being asynchronously cleaned. 1941 */ 1942 if (src->dtb_offset != 0) { 1943 new = DTRACESPEC_COMMITTING; 1944 break; 1945 } 1946 /*FALLTHROUGH*/ 1947 1948 case DTRACESPEC_ACTIVEMANY: 1949 new = DTRACESPEC_COMMITTINGMANY; 1950 break; 1951 1952 default: 1953 ASSERT(0); 1954 } 1955 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 1956 current, new) != current); 1957 1958 /* 1959 * We have set the state to indicate that we are committing this 1960 * speculation. Now reserve the necessary space in the destination 1961 * buffer. 1962 */ 1963 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 1964 sizeof (uint64_t), state, NULL)) < 0) { 1965 dtrace_buffer_drop(dest); 1966 goto out; 1967 } 1968 1969 /* 1970 * We have the space; copy the buffer across. (Note that this is a 1971 * highly subobtimal bcopy(); in the unlikely event that this becomes 1972 * a serious performance issue, a high-performance DTrace-specific 1973 * bcopy() should obviously be invented.) 1974 */ 1975 daddr = (uintptr_t)dest->dtb_tomax + offs; 1976 dlimit = daddr + src->dtb_offset; 1977 saddr = (uintptr_t)src->dtb_tomax; 1978 1979 /* 1980 * First, the aligned portion. 1981 */ 1982 while (dlimit - daddr >= sizeof (uint64_t)) { 1983 *((uint64_t *)daddr) = *((uint64_t *)saddr); 1984 1985 daddr += sizeof (uint64_t); 1986 saddr += sizeof (uint64_t); 1987 } 1988 1989 /* 1990 * Now any left-over bit... 1991 */ 1992 while (dlimit - daddr) 1993 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 1994 1995 /* 1996 * Finally, commit the reserved space in the destination buffer. 1997 */ 1998 dest->dtb_offset = offs + src->dtb_offset; 1999 2000 out: 2001 /* 2002 * If we're lucky enough to be the only active CPU on this speculation 2003 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2004 */ 2005 if (current == DTRACESPEC_ACTIVE || 2006 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2007 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2008 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2009 2010 ASSERT(rval == DTRACESPEC_COMMITTING); 2011 } 2012 2013 src->dtb_offset = 0; 2014 src->dtb_xamot_drops += src->dtb_drops; 2015 src->dtb_drops = 0; 2016 } 2017 2018 /* 2019 * This routine discards an active speculation. If the specified speculation 2020 * is not in a valid state to perform a discard(), this routine will silently 2021 * do nothing. The state of the specified speculation is transitioned 2022 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2023 */ 2024 static void 2025 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2026 dtrace_specid_t which) 2027 { 2028 dtrace_speculation_t *spec; 2029 dtrace_speculation_state_t current, new; 2030 dtrace_buffer_t *buf; 2031 2032 if (which == 0) 2033 return; 2034 2035 if (which > state->dts_nspeculations) { 2036 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2037 return; 2038 } 2039 2040 spec = &state->dts_speculations[which - 1]; 2041 buf = &spec->dtsp_buffer[cpu]; 2042 2043 do { 2044 current = spec->dtsp_state; 2045 2046 switch (current) { 2047 case DTRACESPEC_INACTIVE: 2048 case DTRACESPEC_COMMITTINGMANY: 2049 case DTRACESPEC_COMMITTING: 2050 case DTRACESPEC_DISCARDING: 2051 return; 2052 2053 case DTRACESPEC_ACTIVE: 2054 case DTRACESPEC_ACTIVEMANY: 2055 new = DTRACESPEC_DISCARDING; 2056 break; 2057 2058 case DTRACESPEC_ACTIVEONE: 2059 if (buf->dtb_offset != 0) { 2060 new = DTRACESPEC_INACTIVE; 2061 } else { 2062 new = DTRACESPEC_DISCARDING; 2063 } 2064 break; 2065 2066 default: 2067 ASSERT(0); 2068 } 2069 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2070 current, new) != current); 2071 2072 buf->dtb_offset = 0; 2073 buf->dtb_drops = 0; 2074 } 2075 2076 /* 2077 * Note: not called from probe context. This function is called 2078 * asynchronously from cross call context to clean any speculations that are 2079 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2080 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2081 * speculation. 2082 */ 2083 static void 2084 dtrace_speculation_clean_here(dtrace_state_t *state) 2085 { 2086 dtrace_icookie_t cookie; 2087 processorid_t cpu = CPU->cpu_id; 2088 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2089 dtrace_specid_t i; 2090 2091 cookie = dtrace_interrupt_disable(); 2092 2093 if (dest->dtb_tomax == NULL) { 2094 dtrace_interrupt_enable(cookie); 2095 return; 2096 } 2097 2098 for (i = 0; i < state->dts_nspeculations; i++) { 2099 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2100 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2101 2102 if (src->dtb_tomax == NULL) 2103 continue; 2104 2105 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2106 src->dtb_offset = 0; 2107 continue; 2108 } 2109 2110 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2111 continue; 2112 2113 if (src->dtb_offset == 0) 2114 continue; 2115 2116 dtrace_speculation_commit(state, cpu, i + 1); 2117 } 2118 2119 dtrace_interrupt_enable(cookie); 2120 } 2121 2122 /* 2123 * Note: not called from probe context. This function is called 2124 * asynchronously (and at a regular interval) to clean any speculations that 2125 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2126 * is work to be done, it cross calls all CPUs to perform that work; 2127 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2128 * INACTIVE state until they have been cleaned by all CPUs. 2129 */ 2130 static void 2131 dtrace_speculation_clean(dtrace_state_t *state) 2132 { 2133 int work = 0, rv; 2134 dtrace_specid_t i; 2135 2136 for (i = 0; i < state->dts_nspeculations; i++) { 2137 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2138 2139 ASSERT(!spec->dtsp_cleaning); 2140 2141 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2142 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2143 continue; 2144 2145 work++; 2146 spec->dtsp_cleaning = 1; 2147 } 2148 2149 if (!work) 2150 return; 2151 2152 dtrace_xcall(DTRACE_CPUALL, 2153 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2154 2155 /* 2156 * We now know that all CPUs have committed or discarded their 2157 * speculation buffers, as appropriate. We can now set the state 2158 * to inactive. 2159 */ 2160 for (i = 0; i < state->dts_nspeculations; i++) { 2161 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2162 dtrace_speculation_state_t current, new; 2163 2164 if (!spec->dtsp_cleaning) 2165 continue; 2166 2167 current = spec->dtsp_state; 2168 ASSERT(current == DTRACESPEC_DISCARDING || 2169 current == DTRACESPEC_COMMITTINGMANY); 2170 2171 new = DTRACESPEC_INACTIVE; 2172 2173 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2174 ASSERT(rv == current); 2175 spec->dtsp_cleaning = 0; 2176 } 2177 } 2178 2179 /* 2180 * Called as part of a speculate() to get the speculative buffer associated 2181 * with a given speculation. Returns NULL if the specified speculation is not 2182 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2183 * the active CPU is not the specified CPU -- the speculation will be 2184 * atomically transitioned into the ACTIVEMANY state. 2185 */ 2186 static dtrace_buffer_t * 2187 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2188 dtrace_specid_t which) 2189 { 2190 dtrace_speculation_t *spec; 2191 dtrace_speculation_state_t current, new; 2192 dtrace_buffer_t *buf; 2193 2194 if (which == 0) 2195 return (NULL); 2196 2197 if (which > state->dts_nspeculations) { 2198 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2199 return (NULL); 2200 } 2201 2202 spec = &state->dts_speculations[which - 1]; 2203 buf = &spec->dtsp_buffer[cpuid]; 2204 2205 do { 2206 current = spec->dtsp_state; 2207 2208 switch (current) { 2209 case DTRACESPEC_INACTIVE: 2210 case DTRACESPEC_COMMITTINGMANY: 2211 case DTRACESPEC_DISCARDING: 2212 return (NULL); 2213 2214 case DTRACESPEC_COMMITTING: 2215 ASSERT(buf->dtb_offset == 0); 2216 return (NULL); 2217 2218 case DTRACESPEC_ACTIVEONE: 2219 /* 2220 * This speculation is currently active on one CPU. 2221 * Check the offset in the buffer; if it's non-zero, 2222 * that CPU must be us (and we leave the state alone). 2223 * If it's zero, assume that we're starting on a new 2224 * CPU -- and change the state to indicate that the 2225 * speculation is active on more than one CPU. 2226 */ 2227 if (buf->dtb_offset != 0) 2228 return (buf); 2229 2230 new = DTRACESPEC_ACTIVEMANY; 2231 break; 2232 2233 case DTRACESPEC_ACTIVEMANY: 2234 return (buf); 2235 2236 case DTRACESPEC_ACTIVE: 2237 new = DTRACESPEC_ACTIVEONE; 2238 break; 2239 2240 default: 2241 ASSERT(0); 2242 } 2243 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2244 current, new) != current); 2245 2246 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2247 return (buf); 2248 } 2249 2250 /* 2251 * This function implements the DIF emulator's variable lookups. The emulator 2252 * passes a reserved variable identifier and optional built-in array index. 2253 */ 2254 static uint64_t 2255 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2256 uint64_t ndx) 2257 { 2258 /* 2259 * If we're accessing one of the uncached arguments, we'll turn this 2260 * into a reference in the args array. 2261 */ 2262 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2263 ndx = v - DIF_VAR_ARG0; 2264 v = DIF_VAR_ARGS; 2265 } 2266 2267 switch (v) { 2268 case DIF_VAR_ARGS: 2269 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2270 if (ndx >= sizeof (mstate->dtms_arg) / 2271 sizeof (mstate->dtms_arg[0])) { 2272 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2273 dtrace_provider_t *pv; 2274 uint64_t val; 2275 2276 pv = mstate->dtms_probe->dtpr_provider; 2277 if (pv->dtpv_pops.dtps_getargval != NULL) 2278 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2279 mstate->dtms_probe->dtpr_id, 2280 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2281 else 2282 val = dtrace_getarg(ndx, aframes); 2283 2284 /* 2285 * This is regrettably required to keep the compiler 2286 * from tail-optimizing the call to dtrace_getarg(). 2287 * The condition always evaluates to true, but the 2288 * compiler has no way of figuring that out a priori. 2289 * (None of this would be necessary if the compiler 2290 * could be relied upon to _always_ tail-optimize 2291 * the call to dtrace_getarg() -- but it can't.) 2292 */ 2293 if (mstate->dtms_probe != NULL) 2294 return (val); 2295 2296 ASSERT(0); 2297 } 2298 2299 return (mstate->dtms_arg[ndx]); 2300 2301 case DIF_VAR_UREGS: { 2302 klwp_t *lwp; 2303 2304 if (!dtrace_priv_proc(state)) 2305 return (0); 2306 2307 if ((lwp = curthread->t_lwp) == NULL) { 2308 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2309 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL; 2310 return (0); 2311 } 2312 2313 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2314 } 2315 2316 case DIF_VAR_CURTHREAD: 2317 if (!dtrace_priv_kernel(state)) 2318 return (0); 2319 return ((uint64_t)(uintptr_t)curthread); 2320 2321 case DIF_VAR_TIMESTAMP: 2322 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2323 mstate->dtms_timestamp = dtrace_gethrtime(); 2324 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2325 } 2326 return (mstate->dtms_timestamp); 2327 2328 case DIF_VAR_VTIMESTAMP: 2329 ASSERT(dtrace_vtime_references != 0); 2330 return (curthread->t_dtrace_vtime); 2331 2332 case DIF_VAR_WALLTIMESTAMP: 2333 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2334 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2335 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2336 } 2337 return (mstate->dtms_walltimestamp); 2338 2339 case DIF_VAR_IPL: 2340 if (!dtrace_priv_kernel(state)) 2341 return (0); 2342 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2343 mstate->dtms_ipl = dtrace_getipl(); 2344 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2345 } 2346 return (mstate->dtms_ipl); 2347 2348 case DIF_VAR_EPID: 2349 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2350 return (mstate->dtms_epid); 2351 2352 case DIF_VAR_ID: 2353 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2354 return (mstate->dtms_probe->dtpr_id); 2355 2356 case DIF_VAR_STACKDEPTH: 2357 if (!dtrace_priv_kernel(state)) 2358 return (0); 2359 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2360 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2361 2362 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2363 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2364 } 2365 return (mstate->dtms_stackdepth); 2366 2367 case DIF_VAR_USTACKDEPTH: 2368 if (!dtrace_priv_proc(state)) 2369 return (0); 2370 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2371 /* 2372 * See comment in DIF_VAR_PID. 2373 */ 2374 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2375 CPU_ON_INTR(CPU)) { 2376 mstate->dtms_ustackdepth = 0; 2377 } else { 2378 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2379 mstate->dtms_ustackdepth = 2380 dtrace_getustackdepth(); 2381 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2382 } 2383 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2384 } 2385 return (mstate->dtms_ustackdepth); 2386 2387 case DIF_VAR_CALLER: 2388 if (!dtrace_priv_kernel(state)) 2389 return (0); 2390 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2391 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2392 2393 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2394 /* 2395 * If this is an unanchored probe, we are 2396 * required to go through the slow path: 2397 * dtrace_caller() only guarantees correct 2398 * results for anchored probes. 2399 */ 2400 pc_t caller[2]; 2401 2402 dtrace_getpcstack(caller, 2, aframes, 2403 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2404 mstate->dtms_caller = caller[1]; 2405 } else if ((mstate->dtms_caller = 2406 dtrace_caller(aframes)) == -1) { 2407 /* 2408 * We have failed to do this the quick way; 2409 * we must resort to the slower approach of 2410 * calling dtrace_getpcstack(). 2411 */ 2412 pc_t caller; 2413 2414 dtrace_getpcstack(&caller, 1, aframes, NULL); 2415 mstate->dtms_caller = caller; 2416 } 2417 2418 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 2419 } 2420 return (mstate->dtms_caller); 2421 2422 case DIF_VAR_UCALLER: 2423 if (!dtrace_priv_proc(state)) 2424 return (0); 2425 2426 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 2427 uint64_t ustack[3]; 2428 2429 /* 2430 * dtrace_getupcstack() fills in the first uint64_t 2431 * with the current PID. The second uint64_t will 2432 * be the program counter at user-level. The third 2433 * uint64_t will contain the caller, which is what 2434 * we're after. 2435 */ 2436 ustack[2] = NULL; 2437 dtrace_getupcstack(ustack, 3); 2438 mstate->dtms_ucaller = ustack[2]; 2439 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 2440 } 2441 2442 return (mstate->dtms_ucaller); 2443 2444 case DIF_VAR_PROBEPROV: 2445 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2446 return ((uint64_t)(uintptr_t) 2447 mstate->dtms_probe->dtpr_provider->dtpv_name); 2448 2449 case DIF_VAR_PROBEMOD: 2450 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2451 return ((uint64_t)(uintptr_t) 2452 mstate->dtms_probe->dtpr_mod); 2453 2454 case DIF_VAR_PROBEFUNC: 2455 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2456 return ((uint64_t)(uintptr_t) 2457 mstate->dtms_probe->dtpr_func); 2458 2459 case DIF_VAR_PROBENAME: 2460 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2461 return ((uint64_t)(uintptr_t) 2462 mstate->dtms_probe->dtpr_name); 2463 2464 case DIF_VAR_PID: 2465 if (!dtrace_priv_proc(state)) 2466 return (0); 2467 2468 /* 2469 * Note that we are assuming that an unanchored probe is 2470 * always due to a high-level interrupt. (And we're assuming 2471 * that there is only a single high level interrupt.) 2472 */ 2473 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2474 return (pid0.pid_id); 2475 2476 /* 2477 * It is always safe to dereference one's own t_procp pointer: 2478 * it always points to a valid, allocated proc structure. 2479 * Further, it is always safe to dereference the p_pidp member 2480 * of one's own proc structure. (These are truisms becuase 2481 * threads and processes don't clean up their own state -- 2482 * they leave that task to whomever reaps them.) 2483 */ 2484 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 2485 2486 case DIF_VAR_TID: 2487 /* 2488 * See comment in DIF_VAR_PID. 2489 */ 2490 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2491 return (0); 2492 2493 return ((uint64_t)curthread->t_tid); 2494 2495 case DIF_VAR_EXECNAME: 2496 if (!dtrace_priv_proc(state)) 2497 return (0); 2498 2499 /* 2500 * See comment in DIF_VAR_PID. 2501 */ 2502 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2503 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 2504 2505 /* 2506 * It is always safe to dereference one's own t_procp pointer: 2507 * it always points to a valid, allocated proc structure. 2508 * (This is true because threads don't clean up their own 2509 * state -- they leave that task to whomever reaps them.) 2510 */ 2511 return ((uint64_t)(uintptr_t) 2512 curthread->t_procp->p_user.u_comm); 2513 2514 case DIF_VAR_ZONENAME: 2515 if (!dtrace_priv_proc(state)) 2516 return (0); 2517 2518 /* 2519 * See comment in DIF_VAR_PID. 2520 */ 2521 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2522 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 2523 2524 /* 2525 * It is always safe to dereference one's own t_procp pointer: 2526 * it always points to a valid, allocated proc structure. 2527 * (This is true because threads don't clean up their own 2528 * state -- they leave that task to whomever reaps them.) 2529 */ 2530 return ((uint64_t)(uintptr_t) 2531 curthread->t_procp->p_zone->zone_name); 2532 2533 default: 2534 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2535 return (0); 2536 } 2537 } 2538 2539 /* 2540 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 2541 * Notice that we don't bother validating the proper number of arguments or 2542 * their types in the tuple stack. This isn't needed because all argument 2543 * interpretation is safe because of our load safety -- the worst that can 2544 * happen is that a bogus program can obtain bogus results. 2545 */ 2546 static void 2547 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 2548 dtrace_key_t *tupregs, int nargs, 2549 dtrace_mstate_t *mstate, dtrace_state_t *state) 2550 { 2551 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 2552 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 2553 2554 union { 2555 mutex_impl_t mi; 2556 uint64_t mx; 2557 } m; 2558 2559 union { 2560 krwlock_t ri; 2561 uintptr_t rw; 2562 } r; 2563 2564 switch (subr) { 2565 case DIF_SUBR_RAND: 2566 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 2567 break; 2568 2569 case DIF_SUBR_MUTEX_OWNED: 2570 m.mx = dtrace_load64(tupregs[0].dttk_value); 2571 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 2572 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 2573 else 2574 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 2575 break; 2576 2577 case DIF_SUBR_MUTEX_OWNER: 2578 m.mx = dtrace_load64(tupregs[0].dttk_value); 2579 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 2580 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 2581 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 2582 else 2583 regs[rd] = 0; 2584 break; 2585 2586 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 2587 m.mx = dtrace_load64(tupregs[0].dttk_value); 2588 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 2589 break; 2590 2591 case DIF_SUBR_MUTEX_TYPE_SPIN: 2592 m.mx = dtrace_load64(tupregs[0].dttk_value); 2593 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 2594 break; 2595 2596 case DIF_SUBR_RW_READ_HELD: { 2597 uintptr_t tmp; 2598 2599 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2600 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 2601 break; 2602 } 2603 2604 case DIF_SUBR_RW_WRITE_HELD: 2605 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2606 regs[rd] = _RW_WRITE_HELD(&r.ri); 2607 break; 2608 2609 case DIF_SUBR_RW_ISWRITER: 2610 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 2611 regs[rd] = _RW_ISWRITER(&r.ri); 2612 break; 2613 2614 case DIF_SUBR_BCOPY: { 2615 /* 2616 * We need to be sure that the destination is in the scratch 2617 * region -- no other region is allowed. 2618 */ 2619 uintptr_t src = tupregs[0].dttk_value; 2620 uintptr_t dest = tupregs[1].dttk_value; 2621 size_t size = tupregs[2].dttk_value; 2622 2623 if (!dtrace_inscratch(dest, size, mstate)) { 2624 *flags |= CPU_DTRACE_BADADDR; 2625 *illval = regs[rd]; 2626 break; 2627 } 2628 2629 dtrace_bcopy((void *)src, (void *)dest, size); 2630 break; 2631 } 2632 2633 case DIF_SUBR_ALLOCA: 2634 case DIF_SUBR_COPYIN: { 2635 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 2636 uint64_t size = 2637 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 2638 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 2639 2640 /* 2641 * This action doesn't require any credential checks since 2642 * probes will not activate in user contexts to which the 2643 * enabling user does not have permissions. 2644 */ 2645 if (mstate->dtms_scratch_ptr + scratch_size > 2646 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2647 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2648 regs[rd] = NULL; 2649 break; 2650 } 2651 2652 if (subr == DIF_SUBR_COPYIN) { 2653 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2654 dtrace_copyin(tupregs[0].dttk_value, dest, size); 2655 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2656 } 2657 2658 mstate->dtms_scratch_ptr += scratch_size; 2659 regs[rd] = dest; 2660 break; 2661 } 2662 2663 case DIF_SUBR_COPYINTO: { 2664 uint64_t size = tupregs[1].dttk_value; 2665 uintptr_t dest = tupregs[2].dttk_value; 2666 2667 /* 2668 * This action doesn't require any credential checks since 2669 * probes will not activate in user contexts to which the 2670 * enabling user does not have permissions. 2671 */ 2672 if (!dtrace_inscratch(dest, size, mstate)) { 2673 *flags |= CPU_DTRACE_BADADDR; 2674 *illval = regs[rd]; 2675 break; 2676 } 2677 2678 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2679 dtrace_copyin(tupregs[0].dttk_value, dest, size); 2680 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2681 break; 2682 } 2683 2684 case DIF_SUBR_COPYINSTR: { 2685 uintptr_t dest = mstate->dtms_scratch_ptr; 2686 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2687 2688 if (nargs > 1 && tupregs[1].dttk_value < size) 2689 size = tupregs[1].dttk_value + 1; 2690 2691 /* 2692 * This action doesn't require any credential checks since 2693 * probes will not activate in user contexts to which the 2694 * enabling user does not have permissions. 2695 */ 2696 if (mstate->dtms_scratch_ptr + size > 2697 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2698 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2699 regs[rd] = NULL; 2700 break; 2701 } 2702 2703 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2704 dtrace_copyinstr(tupregs[0].dttk_value, dest, size); 2705 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2706 2707 ((char *)dest)[size - 1] = '\0'; 2708 mstate->dtms_scratch_ptr += size; 2709 regs[rd] = dest; 2710 break; 2711 } 2712 2713 case DIF_SUBR_MSGSIZE: 2714 case DIF_SUBR_MSGDSIZE: { 2715 uintptr_t baddr = tupregs[0].dttk_value, daddr; 2716 uintptr_t wptr, rptr; 2717 size_t count = 0; 2718 int cont = 0; 2719 2720 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 2721 wptr = dtrace_loadptr(baddr + 2722 offsetof(mblk_t, b_wptr)); 2723 2724 rptr = dtrace_loadptr(baddr + 2725 offsetof(mblk_t, b_rptr)); 2726 2727 if (wptr < rptr) { 2728 *flags |= CPU_DTRACE_BADADDR; 2729 *illval = tupregs[0].dttk_value; 2730 break; 2731 } 2732 2733 daddr = dtrace_loadptr(baddr + 2734 offsetof(mblk_t, b_datap)); 2735 2736 baddr = dtrace_loadptr(baddr + 2737 offsetof(mblk_t, b_cont)); 2738 2739 /* 2740 * We want to prevent against denial-of-service here, 2741 * so we're only going to search the list for 2742 * dtrace_msgdsize_max mblks. 2743 */ 2744 if (cont++ > dtrace_msgdsize_max) { 2745 *flags |= CPU_DTRACE_ILLOP; 2746 break; 2747 } 2748 2749 if (subr == DIF_SUBR_MSGDSIZE) { 2750 if (dtrace_load8(daddr + 2751 offsetof(dblk_t, db_type)) != M_DATA) 2752 continue; 2753 } 2754 2755 count += wptr - rptr; 2756 } 2757 2758 if (!(*flags & CPU_DTRACE_FAULT)) 2759 regs[rd] = count; 2760 2761 break; 2762 } 2763 2764 case DIF_SUBR_PROGENYOF: { 2765 pid_t pid = tupregs[0].dttk_value; 2766 proc_t *p; 2767 int rval = 0; 2768 2769 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2770 2771 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 2772 if (p->p_pidp->pid_id == pid) { 2773 rval = 1; 2774 break; 2775 } 2776 } 2777 2778 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2779 2780 regs[rd] = rval; 2781 break; 2782 } 2783 2784 case DIF_SUBR_SPECULATION: 2785 regs[rd] = dtrace_speculation(state); 2786 break; 2787 2788 case DIF_SUBR_COPYOUT: { 2789 uintptr_t kaddr = tupregs[0].dttk_value; 2790 uintptr_t uaddr = tupregs[1].dttk_value; 2791 uint64_t size = tupregs[2].dttk_value; 2792 2793 if (!dtrace_destructive_disallow && 2794 dtrace_priv_proc_control(state) && 2795 !dtrace_istoxic(kaddr, size)) { 2796 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2797 dtrace_copyout(kaddr, uaddr, size); 2798 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2799 } 2800 break; 2801 } 2802 2803 case DIF_SUBR_COPYOUTSTR: { 2804 uintptr_t kaddr = tupregs[0].dttk_value; 2805 uintptr_t uaddr = tupregs[1].dttk_value; 2806 uint64_t size = tupregs[2].dttk_value; 2807 2808 if (!dtrace_destructive_disallow && 2809 dtrace_priv_proc_control(state) && 2810 !dtrace_istoxic(kaddr, size)) { 2811 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2812 dtrace_copyoutstr(kaddr, uaddr, size); 2813 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2814 } 2815 break; 2816 } 2817 2818 case DIF_SUBR_STRLEN: 2819 regs[rd] = dtrace_strlen((char *)(uintptr_t) 2820 tupregs[0].dttk_value, 2821 state->dts_options[DTRACEOPT_STRSIZE]); 2822 break; 2823 2824 case DIF_SUBR_STRCHR: 2825 case DIF_SUBR_STRRCHR: { 2826 /* 2827 * We're going to iterate over the string looking for the 2828 * specified character. We will iterate until we have reached 2829 * the string length or we have found the character. If this 2830 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 2831 * of the specified character instead of the first. 2832 */ 2833 uintptr_t addr = tupregs[0].dttk_value; 2834 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 2835 char c, target = (char)tupregs[1].dttk_value; 2836 2837 for (regs[rd] = NULL; addr < limit; addr++) { 2838 if ((c = dtrace_load8(addr)) == target) { 2839 regs[rd] = addr; 2840 2841 if (subr == DIF_SUBR_STRCHR) 2842 break; 2843 } 2844 2845 if (c == '\0') 2846 break; 2847 } 2848 2849 break; 2850 } 2851 2852 case DIF_SUBR_STRSTR: 2853 case DIF_SUBR_INDEX: 2854 case DIF_SUBR_RINDEX: { 2855 /* 2856 * We're going to iterate over the string looking for the 2857 * specified string. We will iterate until we have reached 2858 * the string length or we have found the string. (Yes, this 2859 * is done in the most naive way possible -- but considering 2860 * that the string we're searching for is likely to be 2861 * relatively short, the complexity of Rabin-Karp or similar 2862 * hardly seems merited.) 2863 */ 2864 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 2865 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 2866 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2867 size_t len = dtrace_strlen(addr, size); 2868 size_t sublen = dtrace_strlen(substr, size); 2869 char *limit = addr + len, *orig = addr; 2870 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 2871 int inc = 1; 2872 2873 regs[rd] = notfound; 2874 2875 /* 2876 * strstr() and index()/rindex() have similar semantics if 2877 * both strings are the empty string: strstr() returns a 2878 * pointer to the (empty) string, and index() and rindex() 2879 * both return index 0 (regardless of any position argument). 2880 */ 2881 if (sublen == 0 && len == 0) { 2882 if (subr == DIF_SUBR_STRSTR) 2883 regs[rd] = (uintptr_t)addr; 2884 else 2885 regs[rd] = 0; 2886 break; 2887 } 2888 2889 if (subr != DIF_SUBR_STRSTR) { 2890 if (subr == DIF_SUBR_RINDEX) { 2891 limit = orig - 1; 2892 addr += len; 2893 inc = -1; 2894 } 2895 2896 /* 2897 * Both index() and rindex() take an optional position 2898 * argument that denotes the starting position. 2899 */ 2900 if (nargs == 3) { 2901 int64_t pos = (int64_t)tupregs[2].dttk_value; 2902 2903 /* 2904 * If the position argument to index() is 2905 * negative, Perl implicitly clamps it at 2906 * zero. This semantic is a little surprising 2907 * given the special meaning of negative 2908 * positions to similar Perl functions like 2909 * substr(), but it appears to reflect a 2910 * notion that index() can start from a 2911 * negative index and increment its way up to 2912 * the string. Given this notion, Perl's 2913 * rindex() is at least self-consistent in 2914 * that it implicitly clamps positions greater 2915 * than the string length to be the string 2916 * length. Where Perl completely loses 2917 * coherence, however, is when the specified 2918 * substring is the empty string (""). In 2919 * this case, even if the position is 2920 * negative, rindex() returns 0 -- and even if 2921 * the position is greater than the length, 2922 * index() returns the string length. These 2923 * semantics violate the notion that index() 2924 * should never return a value less than the 2925 * specified position and that rindex() should 2926 * never return a value greater than the 2927 * specified position. (One assumes that 2928 * these semantics are artifacts of Perl's 2929 * implementation and not the results of 2930 * deliberate design -- it beggars belief that 2931 * even Larry Wall could desire such oddness.) 2932 * While in the abstract one would wish for 2933 * consistent position semantics across 2934 * substr(), index() and rindex() -- or at the 2935 * very least self-consistent position 2936 * semantics for index() and rindex() -- we 2937 * instead opt to keep with the extant Perl 2938 * semantics, in all their broken glory. (Do 2939 * we have more desire to maintain Perl's 2940 * semantics than Perl does? Probably.) 2941 */ 2942 if (subr == DIF_SUBR_RINDEX) { 2943 if (pos < 0) { 2944 if (sublen == 0) 2945 regs[rd] = 0; 2946 break; 2947 } 2948 2949 if (pos > len) 2950 pos = len; 2951 } else { 2952 if (pos < 0) 2953 pos = 0; 2954 2955 if (pos >= len) { 2956 if (sublen == 0) 2957 regs[rd] = len; 2958 break; 2959 } 2960 } 2961 2962 addr = orig + pos; 2963 } 2964 } 2965 2966 for (regs[rd] = notfound; addr != limit; addr += inc) { 2967 if (dtrace_strncmp(addr, substr, sublen) == 0) { 2968 if (subr != DIF_SUBR_STRSTR) { 2969 /* 2970 * As D index() and rindex() are 2971 * modeled on Perl (and not on awk), 2972 * we return a zero-based (and not a 2973 * one-based) index. (For you Perl 2974 * weenies: no, we're not going to add 2975 * $[ -- and shouldn't you be at a con 2976 * or something?) 2977 */ 2978 regs[rd] = (uintptr_t)(addr - orig); 2979 break; 2980 } 2981 2982 ASSERT(subr == DIF_SUBR_STRSTR); 2983 regs[rd] = (uintptr_t)addr; 2984 break; 2985 } 2986 } 2987 2988 break; 2989 } 2990 2991 case DIF_SUBR_STRTOK: { 2992 uintptr_t addr = tupregs[0].dttk_value; 2993 uintptr_t tokaddr = tupregs[1].dttk_value; 2994 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2995 uintptr_t limit, toklimit = tokaddr + size; 2996 uint8_t c, tokmap[32]; /* 256 / 8 */ 2997 char *dest = (char *)mstate->dtms_scratch_ptr; 2998 int i; 2999 3000 if (mstate->dtms_scratch_ptr + size > 3001 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3002 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3003 regs[rd] = NULL; 3004 break; 3005 } 3006 3007 if (addr == NULL) { 3008 /* 3009 * If the address specified is NULL, we use our saved 3010 * strtok pointer from the mstate. Note that this 3011 * means that the saved strtok pointer is _only_ 3012 * valid within multiple enablings of the same probe -- 3013 * it behaves like an implicit clause-local variable. 3014 */ 3015 addr = mstate->dtms_strtok; 3016 } 3017 3018 /* 3019 * First, zero the token map, and then process the token 3020 * string -- setting a bit in the map for every character 3021 * found in the token string. 3022 */ 3023 for (i = 0; i < sizeof (tokmap); i++) 3024 tokmap[i] = 0; 3025 3026 for (; tokaddr < toklimit; tokaddr++) { 3027 if ((c = dtrace_load8(tokaddr)) == '\0') 3028 break; 3029 3030 ASSERT((c >> 3) < sizeof (tokmap)); 3031 tokmap[c >> 3] |= (1 << (c & 0x7)); 3032 } 3033 3034 for (limit = addr + size; addr < limit; addr++) { 3035 /* 3036 * We're looking for a character that is _not_ contained 3037 * in the token string. 3038 */ 3039 if ((c = dtrace_load8(addr)) == '\0') 3040 break; 3041 3042 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3043 break; 3044 } 3045 3046 if (c == '\0') { 3047 /* 3048 * We reached the end of the string without finding 3049 * any character that was not in the token string. 3050 * We return NULL in this case, and we set the saved 3051 * address to NULL as well. 3052 */ 3053 regs[rd] = NULL; 3054 mstate->dtms_strtok = NULL; 3055 break; 3056 } 3057 3058 /* 3059 * From here on, we're copying into the destination string. 3060 */ 3061 for (i = 0; addr < limit && i < size - 1; addr++) { 3062 if ((c = dtrace_load8(addr)) == '\0') 3063 break; 3064 3065 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3066 break; 3067 3068 ASSERT(i < size); 3069 dest[i++] = c; 3070 } 3071 3072 ASSERT(i < size); 3073 dest[i] = '\0'; 3074 regs[rd] = (uintptr_t)dest; 3075 mstate->dtms_scratch_ptr += size; 3076 mstate->dtms_strtok = addr; 3077 break; 3078 } 3079 3080 case DIF_SUBR_SUBSTR: { 3081 uintptr_t s = tupregs[0].dttk_value; 3082 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3083 char *d = (char *)mstate->dtms_scratch_ptr; 3084 int64_t index = (int64_t)tupregs[1].dttk_value; 3085 int64_t remaining = (int64_t)tupregs[2].dttk_value; 3086 size_t len = dtrace_strlen((char *)s, size); 3087 int64_t i = 0; 3088 3089 if (nargs <= 2) 3090 remaining = (int64_t)size; 3091 3092 if (mstate->dtms_scratch_ptr + size > 3093 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3094 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3095 regs[rd] = NULL; 3096 break; 3097 } 3098 3099 if (index < 0) { 3100 index += len; 3101 3102 if (index < 0 && index + remaining > 0) { 3103 remaining += index; 3104 index = 0; 3105 } 3106 } 3107 3108 if (index >= len || index < 0) 3109 index = len; 3110 3111 for (d[0] = '\0'; remaining > 0; remaining--) { 3112 if ((d[i++] = dtrace_load8(s++ + index)) == '\0') 3113 break; 3114 3115 if (i == size) { 3116 d[i - 1] = '\0'; 3117 break; 3118 } 3119 } 3120 3121 mstate->dtms_scratch_ptr += size; 3122 regs[rd] = (uintptr_t)d; 3123 break; 3124 } 3125 3126 case DIF_SUBR_GETMAJOR: 3127 #ifdef _LP64 3128 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 3129 #else 3130 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 3131 #endif 3132 break; 3133 3134 case DIF_SUBR_GETMINOR: 3135 #ifdef _LP64 3136 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 3137 #else 3138 regs[rd] = tupregs[0].dttk_value & MAXMIN; 3139 #endif 3140 break; 3141 3142 case DIF_SUBR_DDI_PATHNAME: { 3143 /* 3144 * This one is a galactic mess. We are going to roughly 3145 * emulate ddi_pathname(), but it's made more complicated 3146 * by the fact that we (a) want to include the minor name and 3147 * (b) must proceed iteratively instead of recursively. 3148 */ 3149 uintptr_t dest = mstate->dtms_scratch_ptr; 3150 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3151 char *start = (char *)dest, *end = start + size - 1; 3152 uintptr_t daddr = tupregs[0].dttk_value; 3153 int64_t minor = (int64_t)tupregs[1].dttk_value; 3154 char *s; 3155 int i, len, depth = 0; 3156 3157 if (size == 0 || mstate->dtms_scratch_ptr + size > 3158 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3159 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3160 regs[rd] = NULL; 3161 break; 3162 } 3163 3164 *end = '\0'; 3165 3166 /* 3167 * We want to have a name for the minor. In order to do this, 3168 * we need to walk the minor list from the devinfo. We want 3169 * to be sure that we don't infinitely walk a circular list, 3170 * so we check for circularity by sending a scout pointer 3171 * ahead two elements for every element that we iterate over; 3172 * if the list is circular, these will ultimately point to the 3173 * same element. You may recognize this little trick as the 3174 * answer to a stupid interview question -- one that always 3175 * seems to be asked by those who had to have it laboriously 3176 * explained to them, and who can't even concisely describe 3177 * the conditions under which one would be forced to resort to 3178 * this technique. Needless to say, those conditions are 3179 * found here -- and probably only here. Is this is the only 3180 * use of this infamous trick in shipping, production code? 3181 * If it isn't, it probably should be... 3182 */ 3183 if (minor != -1) { 3184 uintptr_t maddr = dtrace_loadptr(daddr + 3185 offsetof(struct dev_info, devi_minor)); 3186 3187 uintptr_t next = offsetof(struct ddi_minor_data, next); 3188 uintptr_t name = offsetof(struct ddi_minor_data, 3189 d_minor) + offsetof(struct ddi_minor, name); 3190 uintptr_t dev = offsetof(struct ddi_minor_data, 3191 d_minor) + offsetof(struct ddi_minor, dev); 3192 uintptr_t scout; 3193 3194 if (maddr != NULL) 3195 scout = dtrace_loadptr(maddr + next); 3196 3197 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3198 uint64_t m; 3199 #ifdef _LP64 3200 m = dtrace_load64(maddr + dev) & MAXMIN64; 3201 #else 3202 m = dtrace_load32(maddr + dev) & MAXMIN; 3203 #endif 3204 if (m != minor) { 3205 maddr = dtrace_loadptr(maddr + next); 3206 3207 if (scout == NULL) 3208 continue; 3209 3210 scout = dtrace_loadptr(scout + next); 3211 3212 if (scout == NULL) 3213 continue; 3214 3215 scout = dtrace_loadptr(scout + next); 3216 3217 if (scout == NULL) 3218 continue; 3219 3220 if (scout == maddr) { 3221 *flags |= CPU_DTRACE_ILLOP; 3222 break; 3223 } 3224 3225 continue; 3226 } 3227 3228 /* 3229 * We have the minor data. Now we need to 3230 * copy the minor's name into the end of the 3231 * pathname. 3232 */ 3233 s = (char *)dtrace_loadptr(maddr + name); 3234 len = dtrace_strlen(s, size); 3235 3236 if (*flags & CPU_DTRACE_FAULT) 3237 break; 3238 3239 if (len != 0) { 3240 if ((end -= (len + 1)) < start) 3241 break; 3242 3243 *end = ':'; 3244 } 3245 3246 for (i = 1; i <= len; i++) 3247 end[i] = dtrace_load8((uintptr_t)s++); 3248 break; 3249 } 3250 } 3251 3252 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3253 ddi_node_state_t devi_state; 3254 3255 devi_state = dtrace_load32(daddr + 3256 offsetof(struct dev_info, devi_node_state)); 3257 3258 if (*flags & CPU_DTRACE_FAULT) 3259 break; 3260 3261 if (devi_state >= DS_INITIALIZED) { 3262 s = (char *)dtrace_loadptr(daddr + 3263 offsetof(struct dev_info, devi_addr)); 3264 len = dtrace_strlen(s, size); 3265 3266 if (*flags & CPU_DTRACE_FAULT) 3267 break; 3268 3269 if (len != 0) { 3270 if ((end -= (len + 1)) < start) 3271 break; 3272 3273 *end = '@'; 3274 } 3275 3276 for (i = 1; i <= len; i++) 3277 end[i] = dtrace_load8((uintptr_t)s++); 3278 } 3279 3280 /* 3281 * Now for the node name... 3282 */ 3283 s = (char *)dtrace_loadptr(daddr + 3284 offsetof(struct dev_info, devi_node_name)); 3285 3286 daddr = dtrace_loadptr(daddr + 3287 offsetof(struct dev_info, devi_parent)); 3288 3289 /* 3290 * If our parent is NULL (that is, if we're the root 3291 * node), we're going to use the special path 3292 * "devices". 3293 */ 3294 if (daddr == NULL) 3295 s = "devices"; 3296 3297 len = dtrace_strlen(s, size); 3298 if (*flags & CPU_DTRACE_FAULT) 3299 break; 3300 3301 if ((end -= (len + 1)) < start) 3302 break; 3303 3304 for (i = 1; i <= len; i++) 3305 end[i] = dtrace_load8((uintptr_t)s++); 3306 *end = '/'; 3307 3308 if (depth++ > dtrace_devdepth_max) { 3309 *flags |= CPU_DTRACE_ILLOP; 3310 break; 3311 } 3312 } 3313 3314 if (end < start) 3315 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3316 3317 if (daddr == NULL) { 3318 regs[rd] = (uintptr_t)end; 3319 mstate->dtms_scratch_ptr += size; 3320 } 3321 3322 break; 3323 } 3324 3325 case DIF_SUBR_STRJOIN: { 3326 char *d = (char *)mstate->dtms_scratch_ptr; 3327 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3328 uintptr_t s1 = tupregs[0].dttk_value; 3329 uintptr_t s2 = tupregs[1].dttk_value; 3330 int i = 0; 3331 3332 if (mstate->dtms_scratch_ptr + size > 3333 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3334 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3335 regs[rd] = NULL; 3336 break; 3337 } 3338 3339 for (;;) { 3340 if (i >= size) { 3341 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3342 regs[rd] = NULL; 3343 break; 3344 } 3345 3346 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 3347 i--; 3348 break; 3349 } 3350 } 3351 3352 for (;;) { 3353 if (i >= size) { 3354 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3355 regs[rd] = NULL; 3356 break; 3357 } 3358 3359 if ((d[i++] = dtrace_load8(s2++)) == '\0') 3360 break; 3361 } 3362 3363 if (i < size) { 3364 mstate->dtms_scratch_ptr += i; 3365 regs[rd] = (uintptr_t)d; 3366 } 3367 3368 break; 3369 } 3370 3371 case DIF_SUBR_LLTOSTR: { 3372 int64_t i = (int64_t)tupregs[0].dttk_value; 3373 int64_t val = i < 0 ? i * -1 : i; 3374 uint64_t size = 22; /* enough room for 2^64 in decimal */ 3375 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 3376 3377 if (mstate->dtms_scratch_ptr + size > 3378 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3379 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3380 regs[rd] = NULL; 3381 break; 3382 } 3383 3384 for (*end-- = '\0'; val; val /= 10) 3385 *end-- = '0' + (val % 10); 3386 3387 if (i == 0) 3388 *end-- = '0'; 3389 3390 if (i < 0) 3391 *end-- = '-'; 3392 3393 regs[rd] = (uintptr_t)end + 1; 3394 mstate->dtms_scratch_ptr += size; 3395 break; 3396 } 3397 3398 case DIF_SUBR_DIRNAME: 3399 case DIF_SUBR_BASENAME: { 3400 char *dest = (char *)mstate->dtms_scratch_ptr; 3401 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3402 uintptr_t src = tupregs[0].dttk_value; 3403 int i, j, len = dtrace_strlen((char *)src, size); 3404 int lastbase = -1, firstbase = -1, lastdir = -1; 3405 int start, end; 3406 3407 if (mstate->dtms_scratch_ptr + size > 3408 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3409 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3410 regs[rd] = NULL; 3411 break; 3412 } 3413 3414 /* 3415 * The basename and dirname for a zero-length string is 3416 * defined to be "." 3417 */ 3418 if (len == 0) { 3419 len = 1; 3420 src = (uintptr_t)"."; 3421 } 3422 3423 /* 3424 * Start from the back of the string, moving back toward the 3425 * front until we see a character that isn't a slash. That 3426 * character is the last character in the basename. 3427 */ 3428 for (i = len - 1; i >= 0; i--) { 3429 if (dtrace_load8(src + i) != '/') 3430 break; 3431 } 3432 3433 if (i >= 0) 3434 lastbase = i; 3435 3436 /* 3437 * Starting from the last character in the basename, move 3438 * towards the front until we find a slash. The character 3439 * that we processed immediately before that is the first 3440 * character in the basename. 3441 */ 3442 for (; i >= 0; i--) { 3443 if (dtrace_load8(src + i) == '/') 3444 break; 3445 } 3446 3447 if (i >= 0) 3448 firstbase = i + 1; 3449 3450 /* 3451 * Now keep going until we find a non-slash character. That 3452 * character is the last character in the dirname. 3453 */ 3454 for (; i >= 0; i--) { 3455 if (dtrace_load8(src + i) != '/') 3456 break; 3457 } 3458 3459 if (i >= 0) 3460 lastdir = i; 3461 3462 ASSERT(!(lastbase == -1 && firstbase != -1)); 3463 ASSERT(!(firstbase == -1 && lastdir != -1)); 3464 3465 if (lastbase == -1) { 3466 /* 3467 * We didn't find a non-slash character. We know that 3468 * the length is non-zero, so the whole string must be 3469 * slashes. In either the dirname or the basename 3470 * case, we return '/'. 3471 */ 3472 ASSERT(firstbase == -1); 3473 firstbase = lastbase = lastdir = 0; 3474 } 3475 3476 if (firstbase == -1) { 3477 /* 3478 * The entire string consists only of a basename 3479 * component. If we're looking for dirname, we need 3480 * to change our string to be just "."; if we're 3481 * looking for a basename, we'll just set the first 3482 * character of the basename to be 0. 3483 */ 3484 if (subr == DIF_SUBR_DIRNAME) { 3485 ASSERT(lastdir == -1); 3486 src = (uintptr_t)"."; 3487 lastdir = 0; 3488 } else { 3489 firstbase = 0; 3490 } 3491 } 3492 3493 if (subr == DIF_SUBR_DIRNAME) { 3494 if (lastdir == -1) { 3495 /* 3496 * We know that we have a slash in the name -- 3497 * or lastdir would be set to 0, above. And 3498 * because lastdir is -1, we know that this 3499 * slash must be the first character. (That 3500 * is, the full string must be of the form 3501 * "/basename".) In this case, the last 3502 * character of the directory name is 0. 3503 */ 3504 lastdir = 0; 3505 } 3506 3507 start = 0; 3508 end = lastdir; 3509 } else { 3510 ASSERT(subr == DIF_SUBR_BASENAME); 3511 ASSERT(firstbase != -1 && lastbase != -1); 3512 start = firstbase; 3513 end = lastbase; 3514 } 3515 3516 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 3517 dest[j] = dtrace_load8(src + i); 3518 3519 dest[j] = '\0'; 3520 regs[rd] = (uintptr_t)dest; 3521 mstate->dtms_scratch_ptr += size; 3522 break; 3523 } 3524 3525 case DIF_SUBR_CLEANPATH: { 3526 char *dest = (char *)mstate->dtms_scratch_ptr, c; 3527 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3528 uintptr_t src = tupregs[0].dttk_value; 3529 int i = 0, j = 0; 3530 3531 if (mstate->dtms_scratch_ptr + size > 3532 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3533 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3534 regs[rd] = NULL; 3535 break; 3536 } 3537 3538 /* 3539 * Move forward, loading each character. 3540 */ 3541 do { 3542 c = dtrace_load8(src + i++); 3543 next: 3544 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 3545 break; 3546 3547 if (c != '/') { 3548 dest[j++] = c; 3549 continue; 3550 } 3551 3552 c = dtrace_load8(src + i++); 3553 3554 if (c == '/') { 3555 /* 3556 * We have two slashes -- we can just advance 3557 * to the next character. 3558 */ 3559 goto next; 3560 } 3561 3562 if (c != '.') { 3563 /* 3564 * This is not "." and it's not ".." -- we can 3565 * just store the "/" and this character and 3566 * drive on. 3567 */ 3568 dest[j++] = '/'; 3569 dest[j++] = c; 3570 continue; 3571 } 3572 3573 c = dtrace_load8(src + i++); 3574 3575 if (c == '/') { 3576 /* 3577 * This is a "/./" component. We're not going 3578 * to store anything in the destination buffer; 3579 * we're just going to go to the next component. 3580 */ 3581 goto next; 3582 } 3583 3584 if (c != '.') { 3585 /* 3586 * This is not ".." -- we can just store the 3587 * "/." and this character and continue 3588 * processing. 3589 */ 3590 dest[j++] = '/'; 3591 dest[j++] = '.'; 3592 dest[j++] = c; 3593 continue; 3594 } 3595 3596 c = dtrace_load8(src + i++); 3597 3598 if (c != '/' && c != '\0') { 3599 /* 3600 * This is not ".." -- it's "..[mumble]". 3601 * We'll store the "/.." and this character 3602 * and continue processing. 3603 */ 3604 dest[j++] = '/'; 3605 dest[j++] = '.'; 3606 dest[j++] = '.'; 3607 dest[j++] = c; 3608 continue; 3609 } 3610 3611 /* 3612 * This is "/../" or "/..\0". We need to back up 3613 * our destination pointer until we find a "/". 3614 */ 3615 i--; 3616 while (j != 0 && dest[--j] != '/') 3617 continue; 3618 3619 if (c == '\0') 3620 dest[++j] = '/'; 3621 } while (c != '\0'); 3622 3623 dest[j] = '\0'; 3624 regs[rd] = (uintptr_t)dest; 3625 mstate->dtms_scratch_ptr += size; 3626 break; 3627 } 3628 } 3629 } 3630 3631 /* 3632 * Emulate the execution of DTrace IR instructions specified by the given 3633 * DIF object. This function is deliberately void of assertions as all of 3634 * the necessary checks are handled by a call to dtrace_difo_validate(). 3635 */ 3636 static uint64_t 3637 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 3638 dtrace_vstate_t *vstate, dtrace_state_t *state) 3639 { 3640 const dif_instr_t *text = difo->dtdo_buf; 3641 const uint_t textlen = difo->dtdo_len; 3642 const char *strtab = difo->dtdo_strtab; 3643 const uint64_t *inttab = difo->dtdo_inttab; 3644 3645 uint64_t rval = 0; 3646 dtrace_statvar_t *svar; 3647 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 3648 dtrace_difv_t *v; 3649 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 3650 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 3651 3652 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 3653 uint64_t regs[DIF_DIR_NREGS]; 3654 uint64_t *tmp; 3655 3656 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 3657 int64_t cc_r; 3658 uint_t pc = 0, id, opc; 3659 uint8_t ttop = 0; 3660 dif_instr_t instr; 3661 uint_t r1, r2, rd; 3662 3663 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 3664 3665 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 3666 opc = pc; 3667 3668 instr = text[pc++]; 3669 r1 = DIF_INSTR_R1(instr); 3670 r2 = DIF_INSTR_R2(instr); 3671 rd = DIF_INSTR_RD(instr); 3672 3673 switch (DIF_INSTR_OP(instr)) { 3674 case DIF_OP_OR: 3675 regs[rd] = regs[r1] | regs[r2]; 3676 break; 3677 case DIF_OP_XOR: 3678 regs[rd] = regs[r1] ^ regs[r2]; 3679 break; 3680 case DIF_OP_AND: 3681 regs[rd] = regs[r1] & regs[r2]; 3682 break; 3683 case DIF_OP_SLL: 3684 regs[rd] = regs[r1] << regs[r2]; 3685 break; 3686 case DIF_OP_SRL: 3687 regs[rd] = regs[r1] >> regs[r2]; 3688 break; 3689 case DIF_OP_SUB: 3690 regs[rd] = regs[r1] - regs[r2]; 3691 break; 3692 case DIF_OP_ADD: 3693 regs[rd] = regs[r1] + regs[r2]; 3694 break; 3695 case DIF_OP_MUL: 3696 regs[rd] = regs[r1] * regs[r2]; 3697 break; 3698 case DIF_OP_SDIV: 3699 if (regs[r2] == 0) { 3700 regs[rd] = 0; 3701 *flags |= CPU_DTRACE_DIVZERO; 3702 } else { 3703 regs[rd] = (int64_t)regs[r1] / 3704 (int64_t)regs[r2]; 3705 } 3706 break; 3707 3708 case DIF_OP_UDIV: 3709 if (regs[r2] == 0) { 3710 regs[rd] = 0; 3711 *flags |= CPU_DTRACE_DIVZERO; 3712 } else { 3713 regs[rd] = regs[r1] / regs[r2]; 3714 } 3715 break; 3716 3717 case DIF_OP_SREM: 3718 if (regs[r2] == 0) { 3719 regs[rd] = 0; 3720 *flags |= CPU_DTRACE_DIVZERO; 3721 } else { 3722 regs[rd] = (int64_t)regs[r1] % 3723 (int64_t)regs[r2]; 3724 } 3725 break; 3726 3727 case DIF_OP_UREM: 3728 if (regs[r2] == 0) { 3729 regs[rd] = 0; 3730 *flags |= CPU_DTRACE_DIVZERO; 3731 } else { 3732 regs[rd] = regs[r1] % regs[r2]; 3733 } 3734 break; 3735 3736 case DIF_OP_NOT: 3737 regs[rd] = ~regs[r1]; 3738 break; 3739 case DIF_OP_MOV: 3740 regs[rd] = regs[r1]; 3741 break; 3742 case DIF_OP_CMP: 3743 cc_r = regs[r1] - regs[r2]; 3744 cc_n = cc_r < 0; 3745 cc_z = cc_r == 0; 3746 cc_v = 0; 3747 cc_c = regs[r1] < regs[r2]; 3748 break; 3749 case DIF_OP_TST: 3750 cc_n = cc_v = cc_c = 0; 3751 cc_z = regs[r1] == 0; 3752 break; 3753 case DIF_OP_BA: 3754 pc = DIF_INSTR_LABEL(instr); 3755 break; 3756 case DIF_OP_BE: 3757 if (cc_z) 3758 pc = DIF_INSTR_LABEL(instr); 3759 break; 3760 case DIF_OP_BNE: 3761 if (cc_z == 0) 3762 pc = DIF_INSTR_LABEL(instr); 3763 break; 3764 case DIF_OP_BG: 3765 if ((cc_z | (cc_n ^ cc_v)) == 0) 3766 pc = DIF_INSTR_LABEL(instr); 3767 break; 3768 case DIF_OP_BGU: 3769 if ((cc_c | cc_z) == 0) 3770 pc = DIF_INSTR_LABEL(instr); 3771 break; 3772 case DIF_OP_BGE: 3773 if ((cc_n ^ cc_v) == 0) 3774 pc = DIF_INSTR_LABEL(instr); 3775 break; 3776 case DIF_OP_BGEU: 3777 if (cc_c == 0) 3778 pc = DIF_INSTR_LABEL(instr); 3779 break; 3780 case DIF_OP_BL: 3781 if (cc_n ^ cc_v) 3782 pc = DIF_INSTR_LABEL(instr); 3783 break; 3784 case DIF_OP_BLU: 3785 if (cc_c) 3786 pc = DIF_INSTR_LABEL(instr); 3787 break; 3788 case DIF_OP_BLE: 3789 if (cc_z | (cc_n ^ cc_v)) 3790 pc = DIF_INSTR_LABEL(instr); 3791 break; 3792 case DIF_OP_BLEU: 3793 if (cc_c | cc_z) 3794 pc = DIF_INSTR_LABEL(instr); 3795 break; 3796 case DIF_OP_RLDSB: 3797 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 3798 *flags |= CPU_DTRACE_KPRIV; 3799 *illval = regs[r1]; 3800 break; 3801 } 3802 /*FALLTHROUGH*/ 3803 case DIF_OP_LDSB: 3804 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 3805 break; 3806 case DIF_OP_RLDSH: 3807 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 3808 *flags |= CPU_DTRACE_KPRIV; 3809 *illval = regs[r1]; 3810 break; 3811 } 3812 /*FALLTHROUGH*/ 3813 case DIF_OP_LDSH: 3814 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 3815 break; 3816 case DIF_OP_RLDSW: 3817 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 3818 *flags |= CPU_DTRACE_KPRIV; 3819 *illval = regs[r1]; 3820 break; 3821 } 3822 /*FALLTHROUGH*/ 3823 case DIF_OP_LDSW: 3824 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 3825 break; 3826 case DIF_OP_RLDUB: 3827 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 3828 *flags |= CPU_DTRACE_KPRIV; 3829 *illval = regs[r1]; 3830 break; 3831 } 3832 /*FALLTHROUGH*/ 3833 case DIF_OP_LDUB: 3834 regs[rd] = dtrace_load8(regs[r1]); 3835 break; 3836 case DIF_OP_RLDUH: 3837 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 3838 *flags |= CPU_DTRACE_KPRIV; 3839 *illval = regs[r1]; 3840 break; 3841 } 3842 /*FALLTHROUGH*/ 3843 case DIF_OP_LDUH: 3844 regs[rd] = dtrace_load16(regs[r1]); 3845 break; 3846 case DIF_OP_RLDUW: 3847 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 3848 *flags |= CPU_DTRACE_KPRIV; 3849 *illval = regs[r1]; 3850 break; 3851 } 3852 /*FALLTHROUGH*/ 3853 case DIF_OP_LDUW: 3854 regs[rd] = dtrace_load32(regs[r1]); 3855 break; 3856 case DIF_OP_RLDX: 3857 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 3858 *flags |= CPU_DTRACE_KPRIV; 3859 *illval = regs[r1]; 3860 break; 3861 } 3862 /*FALLTHROUGH*/ 3863 case DIF_OP_LDX: 3864 regs[rd] = dtrace_load64(regs[r1]); 3865 break; 3866 case DIF_OP_ULDSB: 3867 regs[rd] = (int8_t) 3868 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 3869 break; 3870 case DIF_OP_ULDSH: 3871 regs[rd] = (int16_t) 3872 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 3873 break; 3874 case DIF_OP_ULDSW: 3875 regs[rd] = (int32_t) 3876 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 3877 break; 3878 case DIF_OP_ULDUB: 3879 regs[rd] = 3880 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 3881 break; 3882 case DIF_OP_ULDUH: 3883 regs[rd] = 3884 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 3885 break; 3886 case DIF_OP_ULDUW: 3887 regs[rd] = 3888 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 3889 break; 3890 case DIF_OP_ULDX: 3891 regs[rd] = 3892 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 3893 break; 3894 case DIF_OP_RET: 3895 rval = regs[rd]; 3896 break; 3897 case DIF_OP_NOP: 3898 break; 3899 case DIF_OP_SETX: 3900 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 3901 break; 3902 case DIF_OP_SETS: 3903 regs[rd] = (uint64_t)(uintptr_t) 3904 (strtab + DIF_INSTR_STRING(instr)); 3905 break; 3906 case DIF_OP_SCMP: 3907 cc_r = dtrace_strncmp((char *)(uintptr_t)regs[r1], 3908 (char *)(uintptr_t)regs[r2], 3909 state->dts_options[DTRACEOPT_STRSIZE]); 3910 3911 cc_n = cc_r < 0; 3912 cc_z = cc_r == 0; 3913 cc_v = cc_c = 0; 3914 break; 3915 case DIF_OP_LDGA: 3916 regs[rd] = dtrace_dif_variable(mstate, state, 3917 r1, regs[r2]); 3918 break; 3919 case DIF_OP_LDGS: 3920 id = DIF_INSTR_VAR(instr); 3921 3922 if (id >= DIF_VAR_OTHER_UBASE) { 3923 uintptr_t a; 3924 3925 id -= DIF_VAR_OTHER_UBASE; 3926 svar = vstate->dtvs_globals[id]; 3927 ASSERT(svar != NULL); 3928 v = &svar->dtsv_var; 3929 3930 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 3931 regs[rd] = svar->dtsv_data; 3932 break; 3933 } 3934 3935 a = (uintptr_t)svar->dtsv_data; 3936 3937 if (*(uint8_t *)a == UINT8_MAX) { 3938 /* 3939 * If the 0th byte is set to UINT8_MAX 3940 * then this is to be treated as a 3941 * reference to a NULL variable. 3942 */ 3943 regs[rd] = NULL; 3944 } else { 3945 regs[rd] = a + sizeof (uint64_t); 3946 } 3947 3948 break; 3949 } 3950 3951 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 3952 break; 3953 3954 case DIF_OP_STGS: 3955 id = DIF_INSTR_VAR(instr); 3956 3957 ASSERT(id >= DIF_VAR_OTHER_UBASE); 3958 id -= DIF_VAR_OTHER_UBASE; 3959 3960 svar = vstate->dtvs_globals[id]; 3961 ASSERT(svar != NULL); 3962 v = &svar->dtsv_var; 3963 3964 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 3965 uintptr_t a = (uintptr_t)svar->dtsv_data; 3966 3967 ASSERT(a != NULL); 3968 ASSERT(svar->dtsv_size != 0); 3969 3970 if (regs[rd] == NULL) { 3971 *(uint8_t *)a = UINT8_MAX; 3972 break; 3973 } else { 3974 *(uint8_t *)a = 0; 3975 a += sizeof (uint64_t); 3976 } 3977 3978 dtrace_vcopy((void *)(uintptr_t)regs[rd], 3979 (void *)a, &v->dtdv_type); 3980 break; 3981 } 3982 3983 svar->dtsv_data = regs[rd]; 3984 break; 3985 3986 case DIF_OP_LDTA: 3987 /* 3988 * There are no DTrace built-in thread-local arrays at 3989 * present. This opcode is saved for future work. 3990 */ 3991 *flags |= CPU_DTRACE_ILLOP; 3992 regs[rd] = 0; 3993 break; 3994 3995 case DIF_OP_LDLS: 3996 id = DIF_INSTR_VAR(instr); 3997 3998 if (id < DIF_VAR_OTHER_UBASE) { 3999 /* 4000 * For now, this has no meaning. 4001 */ 4002 regs[rd] = 0; 4003 break; 4004 } 4005 4006 id -= DIF_VAR_OTHER_UBASE; 4007 4008 ASSERT(id < vstate->dtvs_nlocals); 4009 ASSERT(vstate->dtvs_locals != NULL); 4010 4011 svar = vstate->dtvs_locals[id]; 4012 ASSERT(svar != NULL); 4013 v = &svar->dtsv_var; 4014 4015 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4016 uintptr_t a = (uintptr_t)svar->dtsv_data; 4017 size_t sz = v->dtdv_type.dtdt_size; 4018 4019 sz += sizeof (uint64_t); 4020 ASSERT(svar->dtsv_size == NCPU * sz); 4021 a += CPU->cpu_id * sz; 4022 4023 if (*(uint8_t *)a == UINT8_MAX) { 4024 /* 4025 * If the 0th byte is set to UINT8_MAX 4026 * then this is to be treated as a 4027 * reference to a NULL variable. 4028 */ 4029 regs[rd] = NULL; 4030 } else { 4031 regs[rd] = a + sizeof (uint64_t); 4032 } 4033 4034 break; 4035 } 4036 4037 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 4038 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 4039 regs[rd] = tmp[CPU->cpu_id]; 4040 break; 4041 4042 case DIF_OP_STLS: 4043 id = DIF_INSTR_VAR(instr); 4044 4045 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4046 id -= DIF_VAR_OTHER_UBASE; 4047 ASSERT(id < vstate->dtvs_nlocals); 4048 4049 ASSERT(vstate->dtvs_locals != NULL); 4050 svar = vstate->dtvs_locals[id]; 4051 ASSERT(svar != NULL); 4052 v = &svar->dtsv_var; 4053 4054 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4055 uintptr_t a = (uintptr_t)svar->dtsv_data; 4056 size_t sz = v->dtdv_type.dtdt_size; 4057 4058 sz += sizeof (uint64_t); 4059 ASSERT(svar->dtsv_size == NCPU * sz); 4060 a += CPU->cpu_id * sz; 4061 4062 if (regs[rd] == NULL) { 4063 *(uint8_t *)a = UINT8_MAX; 4064 break; 4065 } else { 4066 *(uint8_t *)a = 0; 4067 a += sizeof (uint64_t); 4068 } 4069 4070 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4071 (void *)a, &v->dtdv_type); 4072 break; 4073 } 4074 4075 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 4076 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 4077 tmp[CPU->cpu_id] = regs[rd]; 4078 break; 4079 4080 case DIF_OP_LDTS: { 4081 dtrace_dynvar_t *dvar; 4082 dtrace_key_t *key; 4083 4084 id = DIF_INSTR_VAR(instr); 4085 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4086 id -= DIF_VAR_OTHER_UBASE; 4087 v = &vstate->dtvs_tlocals[id]; 4088 4089 key = &tupregs[DIF_DTR_NREGS]; 4090 key[0].dttk_value = (uint64_t)id; 4091 key[0].dttk_size = 0; 4092 DTRACE_TLS_THRKEY(key[1].dttk_value); 4093 key[1].dttk_size = 0; 4094 4095 dvar = dtrace_dynvar(dstate, 2, key, 4096 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC); 4097 4098 if (dvar == NULL) { 4099 regs[rd] = 0; 4100 break; 4101 } 4102 4103 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4104 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 4105 } else { 4106 regs[rd] = *((uint64_t *)dvar->dtdv_data); 4107 } 4108 4109 break; 4110 } 4111 4112 case DIF_OP_STTS: { 4113 dtrace_dynvar_t *dvar; 4114 dtrace_key_t *key; 4115 4116 id = DIF_INSTR_VAR(instr); 4117 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4118 id -= DIF_VAR_OTHER_UBASE; 4119 4120 key = &tupregs[DIF_DTR_NREGS]; 4121 key[0].dttk_value = (uint64_t)id; 4122 key[0].dttk_size = 0; 4123 DTRACE_TLS_THRKEY(key[1].dttk_value); 4124 key[1].dttk_size = 0; 4125 v = &vstate->dtvs_tlocals[id]; 4126 4127 dvar = dtrace_dynvar(dstate, 2, key, 4128 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4129 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4130 regs[rd] ? DTRACE_DYNVAR_ALLOC : 4131 DTRACE_DYNVAR_DEALLOC); 4132 4133 /* 4134 * Given that we're storing to thread-local data, 4135 * we need to flush our predicate cache. 4136 */ 4137 curthread->t_predcache = NULL; 4138 4139 if (dvar == NULL) 4140 break; 4141 4142 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4143 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4144 dvar->dtdv_data, &v->dtdv_type); 4145 } else { 4146 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 4147 } 4148 4149 break; 4150 } 4151 4152 case DIF_OP_SRA: 4153 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 4154 break; 4155 4156 case DIF_OP_CALL: 4157 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 4158 regs, tupregs, ttop, mstate, state); 4159 break; 4160 4161 case DIF_OP_PUSHTR: 4162 if (ttop == DIF_DTR_NREGS) { 4163 *flags |= CPU_DTRACE_TUPOFLOW; 4164 break; 4165 } 4166 4167 if (r1 == DIF_TYPE_STRING) { 4168 /* 4169 * If this is a string type and the size is 0, 4170 * we'll use the system-wide default string 4171 * size. Note that we are _not_ looking at 4172 * the value of the DTRACEOPT_STRSIZE option; 4173 * had this been set, we would expect to have 4174 * a non-zero size value in the "pushtr". 4175 */ 4176 tupregs[ttop].dttk_size = 4177 dtrace_strlen((char *)(uintptr_t)regs[rd], 4178 regs[r2] ? regs[r2] : 4179 dtrace_strsize_default) + 1; 4180 } else { 4181 tupregs[ttop].dttk_size = regs[r2]; 4182 } 4183 4184 tupregs[ttop++].dttk_value = regs[rd]; 4185 break; 4186 4187 case DIF_OP_PUSHTV: 4188 if (ttop == DIF_DTR_NREGS) { 4189 *flags |= CPU_DTRACE_TUPOFLOW; 4190 break; 4191 } 4192 4193 tupregs[ttop].dttk_value = regs[rd]; 4194 tupregs[ttop++].dttk_size = 0; 4195 break; 4196 4197 case DIF_OP_POPTS: 4198 if (ttop != 0) 4199 ttop--; 4200 break; 4201 4202 case DIF_OP_FLUSHTS: 4203 ttop = 0; 4204 break; 4205 4206 case DIF_OP_LDGAA: 4207 case DIF_OP_LDTAA: { 4208 dtrace_dynvar_t *dvar; 4209 dtrace_key_t *key = tupregs; 4210 uint_t nkeys = ttop; 4211 4212 id = DIF_INSTR_VAR(instr); 4213 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4214 id -= DIF_VAR_OTHER_UBASE; 4215 4216 key[nkeys].dttk_value = (uint64_t)id; 4217 key[nkeys++].dttk_size = 0; 4218 4219 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 4220 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 4221 key[nkeys++].dttk_size = 0; 4222 v = &vstate->dtvs_tlocals[id]; 4223 } else { 4224 v = &vstate->dtvs_globals[id]->dtsv_var; 4225 } 4226 4227 dvar = dtrace_dynvar(dstate, nkeys, key, 4228 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4229 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4230 DTRACE_DYNVAR_NOALLOC); 4231 4232 if (dvar == NULL) { 4233 regs[rd] = 0; 4234 break; 4235 } 4236 4237 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4238 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 4239 } else { 4240 regs[rd] = *((uint64_t *)dvar->dtdv_data); 4241 } 4242 4243 break; 4244 } 4245 4246 case DIF_OP_STGAA: 4247 case DIF_OP_STTAA: { 4248 dtrace_dynvar_t *dvar; 4249 dtrace_key_t *key = tupregs; 4250 uint_t nkeys = ttop; 4251 4252 id = DIF_INSTR_VAR(instr); 4253 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4254 id -= DIF_VAR_OTHER_UBASE; 4255 4256 key[nkeys].dttk_value = (uint64_t)id; 4257 key[nkeys++].dttk_size = 0; 4258 4259 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 4260 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 4261 key[nkeys++].dttk_size = 0; 4262 v = &vstate->dtvs_tlocals[id]; 4263 } else { 4264 v = &vstate->dtvs_globals[id]->dtsv_var; 4265 } 4266 4267 dvar = dtrace_dynvar(dstate, nkeys, key, 4268 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4269 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4270 regs[rd] ? DTRACE_DYNVAR_ALLOC : 4271 DTRACE_DYNVAR_DEALLOC); 4272 4273 if (dvar == NULL) 4274 break; 4275 4276 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4277 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4278 dvar->dtdv_data, &v->dtdv_type); 4279 } else { 4280 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 4281 } 4282 4283 break; 4284 } 4285 4286 case DIF_OP_ALLOCS: { 4287 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4288 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 4289 4290 if (mstate->dtms_scratch_ptr + size > 4291 mstate->dtms_scratch_base + 4292 mstate->dtms_scratch_size) { 4293 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4294 regs[rd] = NULL; 4295 } else { 4296 dtrace_bzero((void *) 4297 mstate->dtms_scratch_ptr, size); 4298 mstate->dtms_scratch_ptr += size; 4299 regs[rd] = ptr; 4300 } 4301 break; 4302 } 4303 4304 case DIF_OP_COPYS: 4305 if (!dtrace_canstore(regs[rd], regs[r2], 4306 mstate, vstate)) { 4307 *flags |= CPU_DTRACE_BADADDR; 4308 *illval = regs[rd]; 4309 break; 4310 } 4311 4312 dtrace_bcopy((void *)(uintptr_t)regs[r1], 4313 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 4314 break; 4315 4316 case DIF_OP_STB: 4317 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 4318 *flags |= CPU_DTRACE_BADADDR; 4319 *illval = regs[rd]; 4320 break; 4321 } 4322 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 4323 break; 4324 4325 case DIF_OP_STH: 4326 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 4327 *flags |= CPU_DTRACE_BADADDR; 4328 *illval = regs[rd]; 4329 break; 4330 } 4331 if (regs[rd] & 1) { 4332 *flags |= CPU_DTRACE_BADALIGN; 4333 *illval = regs[rd]; 4334 break; 4335 } 4336 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 4337 break; 4338 4339 case DIF_OP_STW: 4340 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 4341 *flags |= CPU_DTRACE_BADADDR; 4342 *illval = regs[rd]; 4343 break; 4344 } 4345 if (regs[rd] & 3) { 4346 *flags |= CPU_DTRACE_BADALIGN; 4347 *illval = regs[rd]; 4348 break; 4349 } 4350 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 4351 break; 4352 4353 case DIF_OP_STX: 4354 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 4355 *flags |= CPU_DTRACE_BADADDR; 4356 *illval = regs[rd]; 4357 break; 4358 } 4359 if (regs[rd] & 7) { 4360 *flags |= CPU_DTRACE_BADALIGN; 4361 *illval = regs[rd]; 4362 break; 4363 } 4364 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 4365 break; 4366 } 4367 } 4368 4369 if (!(*flags & CPU_DTRACE_FAULT)) 4370 return (rval); 4371 4372 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 4373 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 4374 4375 return (0); 4376 } 4377 4378 static void 4379 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 4380 { 4381 dtrace_probe_t *probe = ecb->dte_probe; 4382 dtrace_provider_t *prov = probe->dtpr_provider; 4383 char c[DTRACE_FULLNAMELEN + 80], *str; 4384 char *msg = "dtrace: breakpoint action at probe "; 4385 char *ecbmsg = " (ecb "; 4386 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 4387 uintptr_t val = (uintptr_t)ecb; 4388 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 4389 4390 if (dtrace_destructive_disallow) 4391 return; 4392 4393 /* 4394 * It's impossible to be taking action on the NULL probe. 4395 */ 4396 ASSERT(probe != NULL); 4397 4398 /* 4399 * This is a poor man's (destitute man's?) sprintf(): we want to 4400 * print the provider name, module name, function name and name of 4401 * the probe, along with the hex address of the ECB with the breakpoint 4402 * action -- all of which we must place in the character buffer by 4403 * hand. 4404 */ 4405 while (*msg != '\0') 4406 c[i++] = *msg++; 4407 4408 for (str = prov->dtpv_name; *str != '\0'; str++) 4409 c[i++] = *str; 4410 c[i++] = ':'; 4411 4412 for (str = probe->dtpr_mod; *str != '\0'; str++) 4413 c[i++] = *str; 4414 c[i++] = ':'; 4415 4416 for (str = probe->dtpr_func; *str != '\0'; str++) 4417 c[i++] = *str; 4418 c[i++] = ':'; 4419 4420 for (str = probe->dtpr_name; *str != '\0'; str++) 4421 c[i++] = *str; 4422 4423 while (*ecbmsg != '\0') 4424 c[i++] = *ecbmsg++; 4425 4426 while (shift >= 0) { 4427 mask = (uintptr_t)0xf << shift; 4428 4429 if (val >= ((uintptr_t)1 << shift)) 4430 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 4431 shift -= 4; 4432 } 4433 4434 c[i++] = ')'; 4435 c[i] = '\0'; 4436 4437 debug_enter(c); 4438 } 4439 4440 static void 4441 dtrace_action_panic(dtrace_ecb_t *ecb) 4442 { 4443 dtrace_probe_t *probe = ecb->dte_probe; 4444 4445 /* 4446 * It's impossible to be taking action on the NULL probe. 4447 */ 4448 ASSERT(probe != NULL); 4449 4450 if (dtrace_destructive_disallow) 4451 return; 4452 4453 if (dtrace_panicked != NULL) 4454 return; 4455 4456 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 4457 return; 4458 4459 /* 4460 * We won the right to panic. (We want to be sure that only one 4461 * thread calls panic() from dtrace_probe(), and that panic() is 4462 * called exactly once.) 4463 */ 4464 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 4465 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 4466 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 4467 } 4468 4469 static void 4470 dtrace_action_raise(uint64_t sig) 4471 { 4472 if (dtrace_destructive_disallow) 4473 return; 4474 4475 if (sig >= NSIG) { 4476 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4477 return; 4478 } 4479 4480 /* 4481 * raise() has a queue depth of 1 -- we ignore all subsequent 4482 * invocations of the raise() action. 4483 */ 4484 if (curthread->t_dtrace_sig == 0) 4485 curthread->t_dtrace_sig = (uint8_t)sig; 4486 4487 curthread->t_sig_check = 1; 4488 aston(curthread); 4489 } 4490 4491 static void 4492 dtrace_action_stop(void) 4493 { 4494 if (dtrace_destructive_disallow) 4495 return; 4496 4497 if (!curthread->t_dtrace_stop) { 4498 curthread->t_dtrace_stop = 1; 4499 curthread->t_sig_check = 1; 4500 aston(curthread); 4501 } 4502 } 4503 4504 static void 4505 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 4506 { 4507 hrtime_t now; 4508 volatile uint16_t *flags; 4509 cpu_t *cpu = CPU; 4510 4511 if (dtrace_destructive_disallow) 4512 return; 4513 4514 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 4515 4516 now = dtrace_gethrtime(); 4517 4518 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 4519 /* 4520 * We need to advance the mark to the current time. 4521 */ 4522 cpu->cpu_dtrace_chillmark = now; 4523 cpu->cpu_dtrace_chilled = 0; 4524 } 4525 4526 /* 4527 * Now check to see if the requested chill time would take us over 4528 * the maximum amount of time allowed in the chill interval. (Or 4529 * worse, if the calculation itself induces overflow.) 4530 */ 4531 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 4532 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 4533 *flags |= CPU_DTRACE_ILLOP; 4534 return; 4535 } 4536 4537 while (dtrace_gethrtime() - now < val) 4538 continue; 4539 4540 /* 4541 * Normally, we assure that the value of the variable "timestamp" does 4542 * not change within an ECB. The presence of chill() represents an 4543 * exception to this rule, however. 4544 */ 4545 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 4546 cpu->cpu_dtrace_chilled += val; 4547 } 4548 4549 static void 4550 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 4551 uint64_t *buf, uint64_t arg) 4552 { 4553 int nframes = DTRACE_USTACK_NFRAMES(arg); 4554 int strsize = DTRACE_USTACK_STRSIZE(arg); 4555 uint64_t *pcs = &buf[1], *fps; 4556 char *str = (char *)&pcs[nframes]; 4557 int size, offs = 0, i, j; 4558 uintptr_t old = mstate->dtms_scratch_ptr, saved; 4559 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 4560 char *sym; 4561 4562 /* 4563 * Should be taking a faster path if string space has not been 4564 * allocated. 4565 */ 4566 ASSERT(strsize != 0); 4567 4568 /* 4569 * We will first allocate some temporary space for the frame pointers. 4570 */ 4571 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4572 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 4573 (nframes * sizeof (uint64_t)); 4574 4575 if (mstate->dtms_scratch_ptr + size > 4576 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 4577 /* 4578 * Not enough room for our frame pointers -- need to indicate 4579 * that we ran out of scratch space. 4580 */ 4581 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4582 return; 4583 } 4584 4585 mstate->dtms_scratch_ptr += size; 4586 saved = mstate->dtms_scratch_ptr; 4587 4588 /* 4589 * Now get a stack with both program counters and frame pointers. 4590 */ 4591 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4592 dtrace_getufpstack(buf, fps, nframes + 1); 4593 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4594 4595 /* 4596 * If that faulted, we're cooked. 4597 */ 4598 if (*flags & CPU_DTRACE_FAULT) 4599 goto out; 4600 4601 /* 4602 * Now we want to walk up the stack, calling the USTACK helper. For 4603 * each iteration, we restore the scratch pointer. 4604 */ 4605 for (i = 0; i < nframes; i++) { 4606 mstate->dtms_scratch_ptr = saved; 4607 4608 if (offs >= strsize) 4609 break; 4610 4611 sym = (char *)(uintptr_t)dtrace_helper( 4612 DTRACE_HELPER_ACTION_USTACK, 4613 mstate, state, pcs[i], fps[i]); 4614 4615 /* 4616 * If we faulted while running the helper, we're going to 4617 * clear the fault and null out the corresponding string. 4618 */ 4619 if (*flags & CPU_DTRACE_FAULT) { 4620 *flags &= ~CPU_DTRACE_FAULT; 4621 str[offs++] = '\0'; 4622 continue; 4623 } 4624 4625 if (sym == NULL) { 4626 str[offs++] = '\0'; 4627 continue; 4628 } 4629 4630 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4631 4632 /* 4633 * Now copy in the string that the helper returned to us. 4634 */ 4635 for (j = 0; offs + j < strsize; j++) { 4636 if ((str[offs + j] = sym[j]) == '\0') 4637 break; 4638 } 4639 4640 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4641 4642 offs += j + 1; 4643 } 4644 4645 if (offs >= strsize) { 4646 /* 4647 * If we didn't have room for all of the strings, we don't 4648 * abort processing -- this needn't be a fatal error -- but we 4649 * still want to increment a counter (dts_stkstroverflows) to 4650 * allow this condition to be warned about. (If this is from 4651 * a jstack() action, it is easily tuned via jstackstrsize.) 4652 */ 4653 dtrace_error(&state->dts_stkstroverflows); 4654 } 4655 4656 while (offs < strsize) 4657 str[offs++] = '\0'; 4658 4659 out: 4660 mstate->dtms_scratch_ptr = old; 4661 } 4662 4663 /* 4664 * If you're looking for the epicenter of DTrace, you just found it. This 4665 * is the function called by the provider to fire a probe -- from which all 4666 * subsequent probe-context DTrace activity emanates. 4667 */ 4668 void 4669 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 4670 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 4671 { 4672 processorid_t cpuid; 4673 dtrace_icookie_t cookie; 4674 dtrace_probe_t *probe; 4675 dtrace_mstate_t mstate; 4676 dtrace_ecb_t *ecb; 4677 dtrace_action_t *act; 4678 intptr_t offs; 4679 size_t size; 4680 int vtime, onintr; 4681 volatile uint16_t *flags; 4682 hrtime_t now; 4683 4684 /* 4685 * Kick out immediately if this CPU is still being born (in which case 4686 * curthread will be set to -1) 4687 */ 4688 if ((uintptr_t)curthread & 1) 4689 return; 4690 4691 cookie = dtrace_interrupt_disable(); 4692 probe = dtrace_probes[id - 1]; 4693 cpuid = CPU->cpu_id; 4694 onintr = CPU_ON_INTR(CPU); 4695 4696 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 4697 probe->dtpr_predcache == curthread->t_predcache) { 4698 /* 4699 * We have hit in the predicate cache; we know that 4700 * this predicate would evaluate to be false. 4701 */ 4702 dtrace_interrupt_enable(cookie); 4703 return; 4704 } 4705 4706 if (panic_quiesce) { 4707 /* 4708 * We don't trace anything if we're panicking. 4709 */ 4710 dtrace_interrupt_enable(cookie); 4711 return; 4712 } 4713 4714 now = dtrace_gethrtime(); 4715 vtime = dtrace_vtime_references != 0; 4716 4717 if (vtime && curthread->t_dtrace_start) 4718 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 4719 4720 mstate.dtms_probe = probe; 4721 mstate.dtms_arg[0] = arg0; 4722 mstate.dtms_arg[1] = arg1; 4723 mstate.dtms_arg[2] = arg2; 4724 mstate.dtms_arg[3] = arg3; 4725 mstate.dtms_arg[4] = arg4; 4726 4727 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 4728 4729 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 4730 dtrace_predicate_t *pred = ecb->dte_predicate; 4731 dtrace_state_t *state = ecb->dte_state; 4732 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 4733 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 4734 dtrace_vstate_t *vstate = &state->dts_vstate; 4735 dtrace_provider_t *prov = probe->dtpr_provider; 4736 int committed = 0; 4737 caddr_t tomax; 4738 4739 /* 4740 * A little subtlety with the following (seemingly innocuous) 4741 * declaration of the automatic 'val': by looking at the 4742 * code, you might think that it could be declared in the 4743 * action processing loop, below. (That is, it's only used in 4744 * the action processing loop.) However, it must be declared 4745 * out of that scope because in the case of DIF expression 4746 * arguments to aggregating actions, one iteration of the 4747 * action loop will use the last iteration's value. 4748 */ 4749 #ifdef lint 4750 uint64_t val = 0; 4751 #else 4752 uint64_t val; 4753 #endif 4754 4755 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 4756 *flags &= ~CPU_DTRACE_ERROR; 4757 4758 if (prov == dtrace_provider) { 4759 /* 4760 * If dtrace itself is the provider of this probe, 4761 * we're only going to continue processing the ECB if 4762 * arg0 (the dtrace_state_t) is equal to the ECB's 4763 * creating state. (This prevents disjoint consumers 4764 * from seeing one another's metaprobes.) 4765 */ 4766 if (arg0 != (uint64_t)(uintptr_t)state) 4767 continue; 4768 } 4769 4770 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 4771 /* 4772 * We're not currently active. If our provider isn't 4773 * the dtrace pseudo provider, we're not interested. 4774 */ 4775 if (prov != dtrace_provider) 4776 continue; 4777 4778 /* 4779 * Now we must further check if we are in the BEGIN 4780 * probe. If we are, we will only continue processing 4781 * if we're still in WARMUP -- if one BEGIN enabling 4782 * has invoked the exit() action, we don't want to 4783 * evaluate subsequent BEGIN enablings. 4784 */ 4785 if (probe->dtpr_id == dtrace_probeid_begin && 4786 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 4787 ASSERT(state->dts_activity == 4788 DTRACE_ACTIVITY_DRAINING); 4789 continue; 4790 } 4791 } 4792 4793 if (ecb->dte_cond) { 4794 /* 4795 * If the dte_cond bits indicate that this 4796 * consumer is only allowed to see user-mode firings 4797 * of this probe, call the provider's dtps_usermode() 4798 * entry point to check that the probe was fired 4799 * while in a user context. Skip this ECB if that's 4800 * not the case. 4801 */ 4802 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 4803 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 4804 probe->dtpr_id, probe->dtpr_arg) == 0) 4805 continue; 4806 4807 /* 4808 * This is more subtle than it looks. We have to be 4809 * absolutely certain that CRED() isn't going to 4810 * change out from under us so it's only legit to 4811 * examine that structure if we're in constrained 4812 * situations. Currently, the only times we'll this 4813 * check is if a non-super-user has enabled the 4814 * profile or syscall providers -- providers that 4815 * allow visibility of all processes. For the 4816 * profile case, the check above will ensure that 4817 * we're examining a user context. 4818 */ 4819 if (ecb->dte_cond & DTRACE_COND_OWNER) { 4820 cred_t *cr; 4821 cred_t *s_cr = 4822 ecb->dte_state->dts_cred.dcr_cred; 4823 proc_t *proc; 4824 4825 ASSERT(s_cr != NULL); 4826 4827 if ((cr = CRED()) == NULL || 4828 s_cr->cr_uid != cr->cr_uid || 4829 s_cr->cr_uid != cr->cr_ruid || 4830 s_cr->cr_uid != cr->cr_suid || 4831 s_cr->cr_gid != cr->cr_gid || 4832 s_cr->cr_gid != cr->cr_rgid || 4833 s_cr->cr_gid != cr->cr_sgid || 4834 (proc = ttoproc(curthread)) == NULL || 4835 (proc->p_flag & SNOCD)) 4836 continue; 4837 } 4838 4839 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 4840 cred_t *cr; 4841 cred_t *s_cr = 4842 ecb->dte_state->dts_cred.dcr_cred; 4843 4844 ASSERT(s_cr != NULL); 4845 4846 if ((cr = CRED()) == NULL || 4847 s_cr->cr_zone->zone_id != 4848 cr->cr_zone->zone_id) 4849 continue; 4850 } 4851 } 4852 4853 if (now - state->dts_alive > dtrace_deadman_timeout) { 4854 /* 4855 * We seem to be dead. Unless we (a) have kernel 4856 * destructive permissions (b) have expicitly enabled 4857 * destructive actions and (c) destructive actions have 4858 * not been disabled, we're going to transition into 4859 * the KILLED state, from which no further processing 4860 * on this state will be performed. 4861 */ 4862 if (!dtrace_priv_kernel_destructive(state) || 4863 !state->dts_cred.dcr_destructive || 4864 dtrace_destructive_disallow) { 4865 void *activity = &state->dts_activity; 4866 dtrace_activity_t current; 4867 4868 do { 4869 current = state->dts_activity; 4870 } while (dtrace_cas32(activity, current, 4871 DTRACE_ACTIVITY_KILLED) != current); 4872 4873 continue; 4874 } 4875 } 4876 4877 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 4878 ecb->dte_alignment, state, &mstate)) < 0) 4879 continue; 4880 4881 tomax = buf->dtb_tomax; 4882 ASSERT(tomax != NULL); 4883 4884 if (ecb->dte_size != 0) 4885 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 4886 4887 mstate.dtms_epid = ecb->dte_epid; 4888 mstate.dtms_present |= DTRACE_MSTATE_EPID; 4889 4890 if (pred != NULL) { 4891 dtrace_difo_t *dp = pred->dtp_difo; 4892 int rval; 4893 4894 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 4895 4896 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 4897 dtrace_cacheid_t cid = probe->dtpr_predcache; 4898 4899 if (cid != DTRACE_CACHEIDNONE && !onintr) { 4900 /* 4901 * Update the predicate cache... 4902 */ 4903 ASSERT(cid == pred->dtp_cacheid); 4904 curthread->t_predcache = cid; 4905 } 4906 4907 continue; 4908 } 4909 } 4910 4911 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 4912 act != NULL; act = act->dta_next) { 4913 size_t valoffs; 4914 dtrace_difo_t *dp; 4915 dtrace_recdesc_t *rec = &act->dta_rec; 4916 4917 size = rec->dtrd_size; 4918 valoffs = offs + rec->dtrd_offset; 4919 4920 if (DTRACEACT_ISAGG(act->dta_kind)) { 4921 uint64_t v = 0xbad; 4922 dtrace_aggregation_t *agg; 4923 4924 agg = (dtrace_aggregation_t *)act; 4925 4926 if ((dp = act->dta_difo) != NULL) 4927 v = dtrace_dif_emulate(dp, 4928 &mstate, vstate, state); 4929 4930 if (*flags & CPU_DTRACE_ERROR) 4931 continue; 4932 4933 /* 4934 * Note that we always pass the expression 4935 * value from the previous iteration of the 4936 * action loop. This value will only be used 4937 * if there is an expression argument to the 4938 * aggregating action, denoted by the 4939 * dtag_hasarg field. 4940 */ 4941 dtrace_aggregate(agg, buf, 4942 offs, aggbuf, v, val); 4943 continue; 4944 } 4945 4946 switch (act->dta_kind) { 4947 case DTRACEACT_STOP: 4948 if (dtrace_priv_proc_destructive(state)) 4949 dtrace_action_stop(); 4950 continue; 4951 4952 case DTRACEACT_BREAKPOINT: 4953 if (dtrace_priv_kernel_destructive(state)) 4954 dtrace_action_breakpoint(ecb); 4955 continue; 4956 4957 case DTRACEACT_PANIC: 4958 if (dtrace_priv_kernel_destructive(state)) 4959 dtrace_action_panic(ecb); 4960 continue; 4961 4962 case DTRACEACT_STACK: 4963 if (!dtrace_priv_kernel(state)) 4964 continue; 4965 4966 dtrace_getpcstack((pc_t *)(tomax + valoffs), 4967 size / sizeof (pc_t), probe->dtpr_aframes, 4968 DTRACE_ANCHORED(probe) ? NULL : 4969 (uint32_t *)arg0); 4970 4971 continue; 4972 4973 case DTRACEACT_JSTACK: 4974 case DTRACEACT_USTACK: 4975 if (!dtrace_priv_proc(state)) 4976 continue; 4977 4978 /* 4979 * See comment in DIF_VAR_PID. 4980 */ 4981 if (DTRACE_ANCHORED(mstate.dtms_probe) && 4982 CPU_ON_INTR(CPU)) { 4983 int depth = DTRACE_USTACK_NFRAMES( 4984 rec->dtrd_arg) + 1; 4985 4986 dtrace_bzero((void *)(tomax + valoffs), 4987 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 4988 + depth * sizeof (uint64_t)); 4989 4990 continue; 4991 } 4992 4993 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 4994 curproc->p_dtrace_helpers != NULL) { 4995 /* 4996 * This is the slow path -- we have 4997 * allocated string space, and we're 4998 * getting the stack of a process that 4999 * has helpers. Call into a separate 5000 * routine to perform this processing. 5001 */ 5002 dtrace_action_ustack(&mstate, state, 5003 (uint64_t *)(tomax + valoffs), 5004 rec->dtrd_arg); 5005 continue; 5006 } 5007 5008 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5009 dtrace_getupcstack((uint64_t *) 5010 (tomax + valoffs), 5011 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 5012 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5013 continue; 5014 5015 default: 5016 break; 5017 } 5018 5019 dp = act->dta_difo; 5020 ASSERT(dp != NULL); 5021 5022 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 5023 5024 if (*flags & CPU_DTRACE_ERROR) 5025 continue; 5026 5027 switch (act->dta_kind) { 5028 case DTRACEACT_SPECULATE: 5029 ASSERT(buf == &state->dts_buffer[cpuid]); 5030 buf = dtrace_speculation_buffer(state, 5031 cpuid, val); 5032 5033 if (buf == NULL) { 5034 *flags |= CPU_DTRACE_DROP; 5035 continue; 5036 } 5037 5038 offs = dtrace_buffer_reserve(buf, 5039 ecb->dte_needed, ecb->dte_alignment, 5040 state, NULL); 5041 5042 if (offs < 0) { 5043 *flags |= CPU_DTRACE_DROP; 5044 continue; 5045 } 5046 5047 tomax = buf->dtb_tomax; 5048 ASSERT(tomax != NULL); 5049 5050 if (ecb->dte_size != 0) 5051 DTRACE_STORE(uint32_t, tomax, offs, 5052 ecb->dte_epid); 5053 continue; 5054 5055 case DTRACEACT_CHILL: 5056 if (dtrace_priv_kernel_destructive(state)) 5057 dtrace_action_chill(&mstate, val); 5058 continue; 5059 5060 case DTRACEACT_RAISE: 5061 if (dtrace_priv_proc_destructive(state)) 5062 dtrace_action_raise(val); 5063 continue; 5064 5065 case DTRACEACT_COMMIT: 5066 ASSERT(!committed); 5067 5068 /* 5069 * We need to commit our buffer state. 5070 */ 5071 if (ecb->dte_size) 5072 buf->dtb_offset = offs + ecb->dte_size; 5073 buf = &state->dts_buffer[cpuid]; 5074 dtrace_speculation_commit(state, cpuid, val); 5075 committed = 1; 5076 continue; 5077 5078 case DTRACEACT_DISCARD: 5079 dtrace_speculation_discard(state, cpuid, val); 5080 continue; 5081 5082 case DTRACEACT_DIFEXPR: 5083 case DTRACEACT_LIBACT: 5084 case DTRACEACT_PRINTF: 5085 case DTRACEACT_PRINTA: 5086 case DTRACEACT_SYSTEM: 5087 case DTRACEACT_FREOPEN: 5088 break; 5089 5090 case DTRACEACT_SYM: 5091 case DTRACEACT_MOD: 5092 if (!dtrace_priv_kernel(state)) 5093 continue; 5094 break; 5095 5096 case DTRACEACT_USYM: 5097 case DTRACEACT_UMOD: 5098 case DTRACEACT_UADDR: { 5099 struct pid *pid = curthread->t_procp->p_pidp; 5100 5101 if (!dtrace_priv_proc(state)) 5102 continue; 5103 5104 DTRACE_STORE(uint64_t, tomax, 5105 valoffs, (uint64_t)pid->pid_id); 5106 DTRACE_STORE(uint64_t, tomax, 5107 valoffs + sizeof (uint64_t), val); 5108 5109 continue; 5110 } 5111 5112 case DTRACEACT_EXIT: { 5113 /* 5114 * For the exit action, we are going to attempt 5115 * to atomically set our activity to be 5116 * draining. If this fails (either because 5117 * another CPU has beat us to the exit action, 5118 * or because our current activity is something 5119 * other than ACTIVE or WARMUP), we will 5120 * continue. This assures that the exit action 5121 * can be successfully recorded at most once 5122 * when we're in the ACTIVE state. If we're 5123 * encountering the exit() action while in 5124 * COOLDOWN, however, we want to honor the new 5125 * status code. (We know that we're the only 5126 * thread in COOLDOWN, so there is no race.) 5127 */ 5128 void *activity = &state->dts_activity; 5129 dtrace_activity_t current = state->dts_activity; 5130 5131 if (current == DTRACE_ACTIVITY_COOLDOWN) 5132 break; 5133 5134 if (current != DTRACE_ACTIVITY_WARMUP) 5135 current = DTRACE_ACTIVITY_ACTIVE; 5136 5137 if (dtrace_cas32(activity, current, 5138 DTRACE_ACTIVITY_DRAINING) != current) { 5139 *flags |= CPU_DTRACE_DROP; 5140 continue; 5141 } 5142 5143 break; 5144 } 5145 5146 default: 5147 ASSERT(0); 5148 } 5149 5150 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 5151 uintptr_t end = valoffs + size; 5152 5153 /* 5154 * If this is a string, we're going to only 5155 * load until we find the zero byte -- after 5156 * which we'll store zero bytes. 5157 */ 5158 if (dp->dtdo_rtype.dtdt_kind == 5159 DIF_TYPE_STRING) { 5160 char c = '\0' + 1; 5161 int intuple = act->dta_intuple; 5162 size_t s; 5163 5164 for (s = 0; s < size; s++) { 5165 if (c != '\0') 5166 c = dtrace_load8(val++); 5167 5168 DTRACE_STORE(uint8_t, tomax, 5169 valoffs++, c); 5170 5171 if (c == '\0' && intuple) 5172 break; 5173 } 5174 5175 continue; 5176 } 5177 5178 while (valoffs < end) { 5179 DTRACE_STORE(uint8_t, tomax, valoffs++, 5180 dtrace_load8(val++)); 5181 } 5182 5183 continue; 5184 } 5185 5186 switch (size) { 5187 case 0: 5188 break; 5189 5190 case sizeof (uint8_t): 5191 DTRACE_STORE(uint8_t, tomax, valoffs, val); 5192 break; 5193 case sizeof (uint16_t): 5194 DTRACE_STORE(uint16_t, tomax, valoffs, val); 5195 break; 5196 case sizeof (uint32_t): 5197 DTRACE_STORE(uint32_t, tomax, valoffs, val); 5198 break; 5199 case sizeof (uint64_t): 5200 DTRACE_STORE(uint64_t, tomax, valoffs, val); 5201 break; 5202 default: 5203 /* 5204 * Any other size should have been returned by 5205 * reference, not by value. 5206 */ 5207 ASSERT(0); 5208 break; 5209 } 5210 } 5211 5212 if (*flags & CPU_DTRACE_DROP) 5213 continue; 5214 5215 if (*flags & CPU_DTRACE_FAULT) { 5216 int ndx; 5217 dtrace_action_t *err; 5218 5219 buf->dtb_errors++; 5220 5221 if (probe->dtpr_id == dtrace_probeid_error) { 5222 /* 5223 * There's nothing we can do -- we had an 5224 * error on the error probe. We bump an 5225 * error counter to at least indicate that 5226 * this condition happened. 5227 */ 5228 dtrace_error(&state->dts_dblerrors); 5229 continue; 5230 } 5231 5232 if (vtime) { 5233 /* 5234 * Before recursing on dtrace_probe(), we 5235 * need to explicitly clear out our start 5236 * time to prevent it from being accumulated 5237 * into t_dtrace_vtime. 5238 */ 5239 curthread->t_dtrace_start = 0; 5240 } 5241 5242 /* 5243 * Iterate over the actions to figure out which action 5244 * we were processing when we experienced the error. 5245 * Note that act points _past_ the faulting action; if 5246 * act is ecb->dte_action, the fault was in the 5247 * predicate, if it's ecb->dte_action->dta_next it's 5248 * in action #1, and so on. 5249 */ 5250 for (err = ecb->dte_action, ndx = 0; 5251 err != act; err = err->dta_next, ndx++) 5252 continue; 5253 5254 dtrace_probe_error(state, ecb->dte_epid, ndx, 5255 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 5256 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 5257 cpu_core[cpuid].cpuc_dtrace_illval); 5258 5259 continue; 5260 } 5261 5262 if (!committed) 5263 buf->dtb_offset = offs + ecb->dte_size; 5264 } 5265 5266 if (vtime) 5267 curthread->t_dtrace_start = dtrace_gethrtime(); 5268 5269 dtrace_interrupt_enable(cookie); 5270 } 5271 5272 /* 5273 * DTrace Probe Hashing Functions 5274 * 5275 * The functions in this section (and indeed, the functions in remaining 5276 * sections) are not _called_ from probe context. (Any exceptions to this are 5277 * marked with a "Note:".) Rather, they are called from elsewhere in the 5278 * DTrace framework to look-up probes in, add probes to and remove probes from 5279 * the DTrace probe hashes. (Each probe is hashed by each element of the 5280 * probe tuple -- allowing for fast lookups, regardless of what was 5281 * specified.) 5282 */ 5283 static uint_t 5284 dtrace_hash_str(char *p) 5285 { 5286 unsigned int g; 5287 uint_t hval = 0; 5288 5289 while (*p) { 5290 hval = (hval << 4) + *p++; 5291 if ((g = (hval & 0xf0000000)) != 0) 5292 hval ^= g >> 24; 5293 hval &= ~g; 5294 } 5295 return (hval); 5296 } 5297 5298 static dtrace_hash_t * 5299 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 5300 { 5301 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 5302 5303 hash->dth_stroffs = stroffs; 5304 hash->dth_nextoffs = nextoffs; 5305 hash->dth_prevoffs = prevoffs; 5306 5307 hash->dth_size = 1; 5308 hash->dth_mask = hash->dth_size - 1; 5309 5310 hash->dth_tab = kmem_zalloc(hash->dth_size * 5311 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 5312 5313 return (hash); 5314 } 5315 5316 static void 5317 dtrace_hash_destroy(dtrace_hash_t *hash) 5318 { 5319 #ifdef DEBUG 5320 int i; 5321 5322 for (i = 0; i < hash->dth_size; i++) 5323 ASSERT(hash->dth_tab[i] == NULL); 5324 #endif 5325 5326 kmem_free(hash->dth_tab, 5327 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 5328 kmem_free(hash, sizeof (dtrace_hash_t)); 5329 } 5330 5331 static void 5332 dtrace_hash_resize(dtrace_hash_t *hash) 5333 { 5334 int size = hash->dth_size, i, ndx; 5335 int new_size = hash->dth_size << 1; 5336 int new_mask = new_size - 1; 5337 dtrace_hashbucket_t **new_tab, *bucket, *next; 5338 5339 ASSERT((new_size & new_mask) == 0); 5340 5341 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 5342 5343 for (i = 0; i < size; i++) { 5344 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 5345 dtrace_probe_t *probe = bucket->dthb_chain; 5346 5347 ASSERT(probe != NULL); 5348 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 5349 5350 next = bucket->dthb_next; 5351 bucket->dthb_next = new_tab[ndx]; 5352 new_tab[ndx] = bucket; 5353 } 5354 } 5355 5356 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 5357 hash->dth_tab = new_tab; 5358 hash->dth_size = new_size; 5359 hash->dth_mask = new_mask; 5360 } 5361 5362 static void 5363 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 5364 { 5365 int hashval = DTRACE_HASHSTR(hash, new); 5366 int ndx = hashval & hash->dth_mask; 5367 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5368 dtrace_probe_t **nextp, **prevp; 5369 5370 for (; bucket != NULL; bucket = bucket->dthb_next) { 5371 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 5372 goto add; 5373 } 5374 5375 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 5376 dtrace_hash_resize(hash); 5377 dtrace_hash_add(hash, new); 5378 return; 5379 } 5380 5381 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 5382 bucket->dthb_next = hash->dth_tab[ndx]; 5383 hash->dth_tab[ndx] = bucket; 5384 hash->dth_nbuckets++; 5385 5386 add: 5387 nextp = DTRACE_HASHNEXT(hash, new); 5388 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 5389 *nextp = bucket->dthb_chain; 5390 5391 if (bucket->dthb_chain != NULL) { 5392 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 5393 ASSERT(*prevp == NULL); 5394 *prevp = new; 5395 } 5396 5397 bucket->dthb_chain = new; 5398 bucket->dthb_len++; 5399 } 5400 5401 static dtrace_probe_t * 5402 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 5403 { 5404 int hashval = DTRACE_HASHSTR(hash, template); 5405 int ndx = hashval & hash->dth_mask; 5406 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5407 5408 for (; bucket != NULL; bucket = bucket->dthb_next) { 5409 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 5410 return (bucket->dthb_chain); 5411 } 5412 5413 return (NULL); 5414 } 5415 5416 static int 5417 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 5418 { 5419 int hashval = DTRACE_HASHSTR(hash, template); 5420 int ndx = hashval & hash->dth_mask; 5421 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5422 5423 for (; bucket != NULL; bucket = bucket->dthb_next) { 5424 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 5425 return (bucket->dthb_len); 5426 } 5427 5428 return (NULL); 5429 } 5430 5431 static void 5432 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 5433 { 5434 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 5435 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 5436 5437 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 5438 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 5439 5440 /* 5441 * Find the bucket that we're removing this probe from. 5442 */ 5443 for (; bucket != NULL; bucket = bucket->dthb_next) { 5444 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 5445 break; 5446 } 5447 5448 ASSERT(bucket != NULL); 5449 5450 if (*prevp == NULL) { 5451 if (*nextp == NULL) { 5452 /* 5453 * The removed probe was the only probe on this 5454 * bucket; we need to remove the bucket. 5455 */ 5456 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 5457 5458 ASSERT(bucket->dthb_chain == probe); 5459 ASSERT(b != NULL); 5460 5461 if (b == bucket) { 5462 hash->dth_tab[ndx] = bucket->dthb_next; 5463 } else { 5464 while (b->dthb_next != bucket) 5465 b = b->dthb_next; 5466 b->dthb_next = bucket->dthb_next; 5467 } 5468 5469 ASSERT(hash->dth_nbuckets > 0); 5470 hash->dth_nbuckets--; 5471 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 5472 return; 5473 } 5474 5475 bucket->dthb_chain = *nextp; 5476 } else { 5477 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 5478 } 5479 5480 if (*nextp != NULL) 5481 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 5482 } 5483 5484 /* 5485 * DTrace Utility Functions 5486 * 5487 * These are random utility functions that are _not_ called from probe context. 5488 */ 5489 static int 5490 dtrace_badattr(const dtrace_attribute_t *a) 5491 { 5492 return (a->dtat_name > DTRACE_STABILITY_MAX || 5493 a->dtat_data > DTRACE_STABILITY_MAX || 5494 a->dtat_class > DTRACE_CLASS_MAX); 5495 } 5496 5497 /* 5498 * Return a duplicate copy of a string. If the specified string is NULL, 5499 * this function returns a zero-length string. 5500 */ 5501 static char * 5502 dtrace_strdup(const char *str) 5503 { 5504 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 5505 5506 if (str != NULL) 5507 (void) strcpy(new, str); 5508 5509 return (new); 5510 } 5511 5512 #define DTRACE_ISALPHA(c) \ 5513 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 5514 5515 static int 5516 dtrace_badname(const char *s) 5517 { 5518 char c; 5519 5520 if (s == NULL || (c = *s++) == '\0') 5521 return (0); 5522 5523 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 5524 return (1); 5525 5526 while ((c = *s++) != '\0') { 5527 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 5528 c != '-' && c != '_' && c != '.' && c != '`') 5529 return (1); 5530 } 5531 5532 return (0); 5533 } 5534 5535 static void 5536 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 5537 { 5538 uint32_t priv; 5539 5540 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 5541 /* 5542 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 5543 */ 5544 priv = DTRACE_PRIV_ALL; 5545 } else { 5546 *uidp = crgetuid(cr); 5547 *zoneidp = crgetzoneid(cr); 5548 5549 priv = 0; 5550 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 5551 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 5552 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 5553 priv |= DTRACE_PRIV_USER; 5554 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 5555 priv |= DTRACE_PRIV_PROC; 5556 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 5557 priv |= DTRACE_PRIV_OWNER; 5558 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 5559 priv |= DTRACE_PRIV_ZONEOWNER; 5560 } 5561 5562 *privp = priv; 5563 } 5564 5565 #ifdef DTRACE_ERRDEBUG 5566 static void 5567 dtrace_errdebug(const char *str) 5568 { 5569 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; 5570 int occupied = 0; 5571 5572 mutex_enter(&dtrace_errlock); 5573 dtrace_errlast = str; 5574 dtrace_errthread = curthread; 5575 5576 while (occupied++ < DTRACE_ERRHASHSZ) { 5577 if (dtrace_errhash[hval].dter_msg == str) { 5578 dtrace_errhash[hval].dter_count++; 5579 goto out; 5580 } 5581 5582 if (dtrace_errhash[hval].dter_msg != NULL) { 5583 hval = (hval + 1) % DTRACE_ERRHASHSZ; 5584 continue; 5585 } 5586 5587 dtrace_errhash[hval].dter_msg = str; 5588 dtrace_errhash[hval].dter_count = 1; 5589 goto out; 5590 } 5591 5592 panic("dtrace: undersized error hash"); 5593 out: 5594 mutex_exit(&dtrace_errlock); 5595 } 5596 #endif 5597 5598 /* 5599 * DTrace Matching Functions 5600 * 5601 * These functions are used to match groups of probes, given some elements of 5602 * a probe tuple, or some globbed expressions for elements of a probe tuple. 5603 */ 5604 static int 5605 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 5606 zoneid_t zoneid) 5607 { 5608 if (priv != DTRACE_PRIV_ALL) { 5609 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 5610 uint32_t match = priv & ppriv; 5611 5612 /* 5613 * No PRIV_DTRACE_* privileges... 5614 */ 5615 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 5616 DTRACE_PRIV_KERNEL)) == 0) 5617 return (0); 5618 5619 /* 5620 * No matching bits, but there were bits to match... 5621 */ 5622 if (match == 0 && ppriv != 0) 5623 return (0); 5624 5625 /* 5626 * Need to have permissions to the process, but don't... 5627 */ 5628 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 5629 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 5630 return (0); 5631 } 5632 5633 /* 5634 * Need to be in the same zone unless we possess the 5635 * privilege to examine all zones. 5636 */ 5637 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 5638 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 5639 return (0); 5640 } 5641 } 5642 5643 return (1); 5644 } 5645 5646 /* 5647 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 5648 * consists of input pattern strings and an ops-vector to evaluate them. 5649 * This function returns >0 for match, 0 for no match, and <0 for error. 5650 */ 5651 static int 5652 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 5653 uint32_t priv, uid_t uid, zoneid_t zoneid) 5654 { 5655 dtrace_provider_t *pvp = prp->dtpr_provider; 5656 int rv; 5657 5658 if (pvp->dtpv_defunct) 5659 return (0); 5660 5661 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 5662 return (rv); 5663 5664 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 5665 return (rv); 5666 5667 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 5668 return (rv); 5669 5670 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 5671 return (rv); 5672 5673 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 5674 return (0); 5675 5676 return (rv); 5677 } 5678 5679 /* 5680 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 5681 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 5682 * libc's version, the kernel version only applies to 8-bit ASCII strings. 5683 * In addition, all of the recursion cases except for '*' matching have been 5684 * unwound. For '*', we still implement recursive evaluation, but a depth 5685 * counter is maintained and matching is aborted if we recurse too deep. 5686 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 5687 */ 5688 static int 5689 dtrace_match_glob(const char *s, const char *p, int depth) 5690 { 5691 const char *olds; 5692 char s1, c; 5693 int gs; 5694 5695 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 5696 return (-1); 5697 5698 if (s == NULL) 5699 s = ""; /* treat NULL as empty string */ 5700 5701 top: 5702 olds = s; 5703 s1 = *s++; 5704 5705 if (p == NULL) 5706 return (0); 5707 5708 if ((c = *p++) == '\0') 5709 return (s1 == '\0'); 5710 5711 switch (c) { 5712 case '[': { 5713 int ok = 0, notflag = 0; 5714 char lc = '\0'; 5715 5716 if (s1 == '\0') 5717 return (0); 5718 5719 if (*p == '!') { 5720 notflag = 1; 5721 p++; 5722 } 5723 5724 if ((c = *p++) == '\0') 5725 return (0); 5726 5727 do { 5728 if (c == '-' && lc != '\0' && *p != ']') { 5729 if ((c = *p++) == '\0') 5730 return (0); 5731 if (c == '\\' && (c = *p++) == '\0') 5732 return (0); 5733 5734 if (notflag) { 5735 if (s1 < lc || s1 > c) 5736 ok++; 5737 else 5738 return (0); 5739 } else if (lc <= s1 && s1 <= c) 5740 ok++; 5741 5742 } else if (c == '\\' && (c = *p++) == '\0') 5743 return (0); 5744 5745 lc = c; /* save left-hand 'c' for next iteration */ 5746 5747 if (notflag) { 5748 if (s1 != c) 5749 ok++; 5750 else 5751 return (0); 5752 } else if (s1 == c) 5753 ok++; 5754 5755 if ((c = *p++) == '\0') 5756 return (0); 5757 5758 } while (c != ']'); 5759 5760 if (ok) 5761 goto top; 5762 5763 return (0); 5764 } 5765 5766 case '\\': 5767 if ((c = *p++) == '\0') 5768 return (0); 5769 /*FALLTHRU*/ 5770 5771 default: 5772 if (c != s1) 5773 return (0); 5774 /*FALLTHRU*/ 5775 5776 case '?': 5777 if (s1 != '\0') 5778 goto top; 5779 return (0); 5780 5781 case '*': 5782 while (*p == '*') 5783 p++; /* consecutive *'s are identical to a single one */ 5784 5785 if (*p == '\0') 5786 return (1); 5787 5788 for (s = olds; *s != '\0'; s++) { 5789 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 5790 return (gs); 5791 } 5792 5793 return (0); 5794 } 5795 } 5796 5797 /*ARGSUSED*/ 5798 static int 5799 dtrace_match_string(const char *s, const char *p, int depth) 5800 { 5801 return (s != NULL && strcmp(s, p) == 0); 5802 } 5803 5804 /*ARGSUSED*/ 5805 static int 5806 dtrace_match_nul(const char *s, const char *p, int depth) 5807 { 5808 return (1); /* always match the empty pattern */ 5809 } 5810 5811 /*ARGSUSED*/ 5812 static int 5813 dtrace_match_nonzero(const char *s, const char *p, int depth) 5814 { 5815 return (s != NULL && s[0] != '\0'); 5816 } 5817 5818 static int 5819 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 5820 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 5821 { 5822 dtrace_probe_t template, *probe; 5823 dtrace_hash_t *hash = NULL; 5824 int len, best = INT_MAX, nmatched = 0; 5825 dtrace_id_t i; 5826 5827 ASSERT(MUTEX_HELD(&dtrace_lock)); 5828 5829 /* 5830 * If the probe ID is specified in the key, just lookup by ID and 5831 * invoke the match callback once if a matching probe is found. 5832 */ 5833 if (pkp->dtpk_id != DTRACE_IDNONE) { 5834 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 5835 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 5836 (void) (*matched)(probe, arg); 5837 nmatched++; 5838 } 5839 return (nmatched); 5840 } 5841 5842 template.dtpr_mod = (char *)pkp->dtpk_mod; 5843 template.dtpr_func = (char *)pkp->dtpk_func; 5844 template.dtpr_name = (char *)pkp->dtpk_name; 5845 5846 /* 5847 * We want to find the most distinct of the module name, function 5848 * name, and name. So for each one that is not a glob pattern or 5849 * empty string, we perform a lookup in the corresponding hash and 5850 * use the hash table with the fewest collisions to do our search. 5851 */ 5852 if (pkp->dtpk_mmatch == &dtrace_match_string && 5853 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 5854 best = len; 5855 hash = dtrace_bymod; 5856 } 5857 5858 if (pkp->dtpk_fmatch == &dtrace_match_string && 5859 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 5860 best = len; 5861 hash = dtrace_byfunc; 5862 } 5863 5864 if (pkp->dtpk_nmatch == &dtrace_match_string && 5865 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 5866 best = len; 5867 hash = dtrace_byname; 5868 } 5869 5870 /* 5871 * If we did not select a hash table, iterate over every probe and 5872 * invoke our callback for each one that matches our input probe key. 5873 */ 5874 if (hash == NULL) { 5875 for (i = 0; i < dtrace_nprobes; i++) { 5876 if ((probe = dtrace_probes[i]) == NULL || 5877 dtrace_match_probe(probe, pkp, priv, uid, 5878 zoneid) <= 0) 5879 continue; 5880 5881 nmatched++; 5882 5883 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 5884 break; 5885 } 5886 5887 return (nmatched); 5888 } 5889 5890 /* 5891 * If we selected a hash table, iterate over each probe of the same key 5892 * name and invoke the callback for every probe that matches the other 5893 * attributes of our input probe key. 5894 */ 5895 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 5896 probe = *(DTRACE_HASHNEXT(hash, probe))) { 5897 5898 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 5899 continue; 5900 5901 nmatched++; 5902 5903 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 5904 break; 5905 } 5906 5907 return (nmatched); 5908 } 5909 5910 /* 5911 * Return the function pointer dtrace_probecmp() should use to compare the 5912 * specified pattern with a string. For NULL or empty patterns, we select 5913 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 5914 * For non-empty non-glob strings, we use dtrace_match_string(). 5915 */ 5916 static dtrace_probekey_f * 5917 dtrace_probekey_func(const char *p) 5918 { 5919 char c; 5920 5921 if (p == NULL || *p == '\0') 5922 return (&dtrace_match_nul); 5923 5924 while ((c = *p++) != '\0') { 5925 if (c == '[' || c == '?' || c == '*' || c == '\\') 5926 return (&dtrace_match_glob); 5927 } 5928 5929 return (&dtrace_match_string); 5930 } 5931 5932 /* 5933 * Build a probe comparison key for use with dtrace_match_probe() from the 5934 * given probe description. By convention, a null key only matches anchored 5935 * probes: if each field is the empty string, reset dtpk_fmatch to 5936 * dtrace_match_nonzero(). 5937 */ 5938 static void 5939 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 5940 { 5941 pkp->dtpk_prov = pdp->dtpd_provider; 5942 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 5943 5944 pkp->dtpk_mod = pdp->dtpd_mod; 5945 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 5946 5947 pkp->dtpk_func = pdp->dtpd_func; 5948 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 5949 5950 pkp->dtpk_name = pdp->dtpd_name; 5951 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 5952 5953 pkp->dtpk_id = pdp->dtpd_id; 5954 5955 if (pkp->dtpk_id == DTRACE_IDNONE && 5956 pkp->dtpk_pmatch == &dtrace_match_nul && 5957 pkp->dtpk_mmatch == &dtrace_match_nul && 5958 pkp->dtpk_fmatch == &dtrace_match_nul && 5959 pkp->dtpk_nmatch == &dtrace_match_nul) 5960 pkp->dtpk_fmatch = &dtrace_match_nonzero; 5961 } 5962 5963 /* 5964 * DTrace Provider-to-Framework API Functions 5965 * 5966 * These functions implement much of the Provider-to-Framework API, as 5967 * described in <sys/dtrace.h>. The parts of the API not in this section are 5968 * the functions in the API for probe management (found below), and 5969 * dtrace_probe() itself (found above). 5970 */ 5971 5972 /* 5973 * Register the calling provider with the DTrace framework. This should 5974 * generally be called by DTrace providers in their attach(9E) entry point. 5975 */ 5976 int 5977 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 5978 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 5979 { 5980 dtrace_provider_t *provider; 5981 5982 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 5983 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5984 "arguments", name ? name : "<NULL>"); 5985 return (EINVAL); 5986 } 5987 5988 if (name[0] == '\0' || dtrace_badname(name)) { 5989 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5990 "provider name", name); 5991 return (EINVAL); 5992 } 5993 5994 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 5995 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 5996 pops->dtps_destroy == NULL || 5997 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 5998 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 5999 "provider ops", name); 6000 return (EINVAL); 6001 } 6002 6003 if (dtrace_badattr(&pap->dtpa_provider) || 6004 dtrace_badattr(&pap->dtpa_mod) || 6005 dtrace_badattr(&pap->dtpa_func) || 6006 dtrace_badattr(&pap->dtpa_name) || 6007 dtrace_badattr(&pap->dtpa_args)) { 6008 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 6009 "provider attributes", name); 6010 return (EINVAL); 6011 } 6012 6013 if (priv & ~DTRACE_PRIV_ALL) { 6014 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 6015 "privilege attributes", name); 6016 return (EINVAL); 6017 } 6018 6019 if ((priv & DTRACE_PRIV_KERNEL) && 6020 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 6021 pops->dtps_usermode == NULL) { 6022 cmn_err(CE_WARN, "failed to register provider '%s': need " 6023 "dtps_usermode() op for given privilege attributes", name); 6024 return (EINVAL); 6025 } 6026 6027 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 6028 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 6029 (void) strcpy(provider->dtpv_name, name); 6030 6031 provider->dtpv_attr = *pap; 6032 provider->dtpv_priv.dtpp_flags = priv; 6033 if (cr != NULL) { 6034 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 6035 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 6036 } 6037 provider->dtpv_pops = *pops; 6038 6039 if (pops->dtps_provide == NULL) { 6040 ASSERT(pops->dtps_provide_module != NULL); 6041 provider->dtpv_pops.dtps_provide = 6042 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 6043 } 6044 6045 if (pops->dtps_provide_module == NULL) { 6046 ASSERT(pops->dtps_provide != NULL); 6047 provider->dtpv_pops.dtps_provide_module = 6048 (void (*)(void *, struct modctl *))dtrace_nullop; 6049 } 6050 6051 if (pops->dtps_suspend == NULL) { 6052 ASSERT(pops->dtps_resume == NULL); 6053 provider->dtpv_pops.dtps_suspend = 6054 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 6055 provider->dtpv_pops.dtps_resume = 6056 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 6057 } 6058 6059 provider->dtpv_arg = arg; 6060 *idp = (dtrace_provider_id_t)provider; 6061 6062 if (pops == &dtrace_provider_ops) { 6063 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6064 ASSERT(MUTEX_HELD(&dtrace_lock)); 6065 ASSERT(dtrace_anon.dta_enabling == NULL); 6066 6067 /* 6068 * We make sure that the DTrace provider is at the head of 6069 * the provider chain. 6070 */ 6071 provider->dtpv_next = dtrace_provider; 6072 dtrace_provider = provider; 6073 return (0); 6074 } 6075 6076 mutex_enter(&dtrace_provider_lock); 6077 mutex_enter(&dtrace_lock); 6078 6079 /* 6080 * If there is at least one provider registered, we'll add this 6081 * provider after the first provider. 6082 */ 6083 if (dtrace_provider != NULL) { 6084 provider->dtpv_next = dtrace_provider->dtpv_next; 6085 dtrace_provider->dtpv_next = provider; 6086 } else { 6087 dtrace_provider = provider; 6088 } 6089 6090 if (dtrace_retained != NULL) { 6091 dtrace_enabling_provide(provider); 6092 6093 /* 6094 * Now we need to call dtrace_enabling_matchall() -- which 6095 * will acquire cpu_lock and dtrace_lock. We therefore need 6096 * to drop all of our locks before calling into it... 6097 */ 6098 mutex_exit(&dtrace_lock); 6099 mutex_exit(&dtrace_provider_lock); 6100 dtrace_enabling_matchall(); 6101 6102 return (0); 6103 } 6104 6105 mutex_exit(&dtrace_lock); 6106 mutex_exit(&dtrace_provider_lock); 6107 6108 return (0); 6109 } 6110 6111 /* 6112 * Unregister the specified provider from the DTrace framework. This should 6113 * generally be called by DTrace providers in their detach(9E) entry point. 6114 */ 6115 int 6116 dtrace_unregister(dtrace_provider_id_t id) 6117 { 6118 dtrace_provider_t *old = (dtrace_provider_t *)id; 6119 dtrace_provider_t *prev = NULL; 6120 int i, self = 0; 6121 dtrace_probe_t *probe, *first = NULL; 6122 6123 if (old->dtpv_pops.dtps_enable == 6124 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 6125 /* 6126 * If DTrace itself is the provider, we're called with locks 6127 * already held. 6128 */ 6129 ASSERT(old == dtrace_provider); 6130 ASSERT(dtrace_devi != NULL); 6131 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6132 ASSERT(MUTEX_HELD(&dtrace_lock)); 6133 self = 1; 6134 6135 if (dtrace_provider->dtpv_next != NULL) { 6136 /* 6137 * There's another provider here; return failure. 6138 */ 6139 return (EBUSY); 6140 } 6141 } else { 6142 mutex_enter(&dtrace_provider_lock); 6143 mutex_enter(&mod_lock); 6144 mutex_enter(&dtrace_lock); 6145 } 6146 6147 /* 6148 * If anyone has /dev/dtrace open, or if there are anonymous enabled 6149 * probes, we refuse to let providers slither away, unless this 6150 * provider has already been explicitly invalidated. 6151 */ 6152 if (!old->dtpv_defunct && 6153 (dtrace_opens || (dtrace_anon.dta_state != NULL && 6154 dtrace_anon.dta_state->dts_necbs > 0))) { 6155 if (!self) { 6156 mutex_exit(&dtrace_lock); 6157 mutex_exit(&mod_lock); 6158 mutex_exit(&dtrace_provider_lock); 6159 } 6160 return (EBUSY); 6161 } 6162 6163 /* 6164 * Attempt to destroy the probes associated with this provider. 6165 */ 6166 for (i = 0; i < dtrace_nprobes; i++) { 6167 if ((probe = dtrace_probes[i]) == NULL) 6168 continue; 6169 6170 if (probe->dtpr_provider != old) 6171 continue; 6172 6173 if (probe->dtpr_ecb == NULL) 6174 continue; 6175 6176 /* 6177 * We have at least one ECB; we can't remove this provider. 6178 */ 6179 if (!self) { 6180 mutex_exit(&dtrace_lock); 6181 mutex_exit(&mod_lock); 6182 mutex_exit(&dtrace_provider_lock); 6183 } 6184 return (EBUSY); 6185 } 6186 6187 /* 6188 * All of the probes for this provider are disabled; we can safely 6189 * remove all of them from their hash chains and from the probe array. 6190 */ 6191 for (i = 0; i < dtrace_nprobes; i++) { 6192 if ((probe = dtrace_probes[i]) == NULL) 6193 continue; 6194 6195 if (probe->dtpr_provider != old) 6196 continue; 6197 6198 dtrace_probes[i] = NULL; 6199 6200 dtrace_hash_remove(dtrace_bymod, probe); 6201 dtrace_hash_remove(dtrace_byfunc, probe); 6202 dtrace_hash_remove(dtrace_byname, probe); 6203 6204 if (first == NULL) { 6205 first = probe; 6206 probe->dtpr_nextmod = NULL; 6207 } else { 6208 probe->dtpr_nextmod = first; 6209 first = probe; 6210 } 6211 } 6212 6213 /* 6214 * The provider's probes have been removed from the hash chains and 6215 * from the probe array. Now issue a dtrace_sync() to be sure that 6216 * everyone has cleared out from any probe array processing. 6217 */ 6218 dtrace_sync(); 6219 6220 for (probe = first; probe != NULL; probe = first) { 6221 first = probe->dtpr_nextmod; 6222 6223 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 6224 probe->dtpr_arg); 6225 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 6226 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 6227 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 6228 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 6229 kmem_free(probe, sizeof (dtrace_probe_t)); 6230 } 6231 6232 if ((prev = dtrace_provider) == old) { 6233 ASSERT(self || dtrace_devi == NULL); 6234 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 6235 dtrace_provider = old->dtpv_next; 6236 } else { 6237 while (prev != NULL && prev->dtpv_next != old) 6238 prev = prev->dtpv_next; 6239 6240 if (prev == NULL) { 6241 panic("attempt to unregister non-existent " 6242 "dtrace provider %p\n", (void *)id); 6243 } 6244 6245 prev->dtpv_next = old->dtpv_next; 6246 } 6247 6248 if (!self) { 6249 mutex_exit(&dtrace_lock); 6250 mutex_exit(&mod_lock); 6251 mutex_exit(&dtrace_provider_lock); 6252 } 6253 6254 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 6255 kmem_free(old, sizeof (dtrace_provider_t)); 6256 6257 return (0); 6258 } 6259 6260 /* 6261 * Invalidate the specified provider. All subsequent probe lookups for the 6262 * specified provider will fail, but its probes will not be removed. 6263 */ 6264 void 6265 dtrace_invalidate(dtrace_provider_id_t id) 6266 { 6267 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 6268 6269 ASSERT(pvp->dtpv_pops.dtps_enable != 6270 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 6271 6272 mutex_enter(&dtrace_provider_lock); 6273 mutex_enter(&dtrace_lock); 6274 6275 pvp->dtpv_defunct = 1; 6276 6277 mutex_exit(&dtrace_lock); 6278 mutex_exit(&dtrace_provider_lock); 6279 } 6280 6281 /* 6282 * Indicate whether or not DTrace has attached. 6283 */ 6284 int 6285 dtrace_attached(void) 6286 { 6287 /* 6288 * dtrace_provider will be non-NULL iff the DTrace driver has 6289 * attached. (It's non-NULL because DTrace is always itself a 6290 * provider.) 6291 */ 6292 return (dtrace_provider != NULL); 6293 } 6294 6295 /* 6296 * Remove all the unenabled probes for the given provider. This function is 6297 * not unlike dtrace_unregister(), except that it doesn't remove the provider 6298 * -- just as many of its associated probes as it can. 6299 */ 6300 int 6301 dtrace_condense(dtrace_provider_id_t id) 6302 { 6303 dtrace_provider_t *prov = (dtrace_provider_t *)id; 6304 int i; 6305 dtrace_probe_t *probe; 6306 6307 /* 6308 * Make sure this isn't the dtrace provider itself. 6309 */ 6310 ASSERT(prov->dtpv_pops.dtps_enable != 6311 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 6312 6313 mutex_enter(&dtrace_provider_lock); 6314 mutex_enter(&dtrace_lock); 6315 6316 /* 6317 * Attempt to destroy the probes associated with this provider. 6318 */ 6319 for (i = 0; i < dtrace_nprobes; i++) { 6320 if ((probe = dtrace_probes[i]) == NULL) 6321 continue; 6322 6323 if (probe->dtpr_provider != prov) 6324 continue; 6325 6326 if (probe->dtpr_ecb != NULL) 6327 continue; 6328 6329 dtrace_probes[i] = NULL; 6330 6331 dtrace_hash_remove(dtrace_bymod, probe); 6332 dtrace_hash_remove(dtrace_byfunc, probe); 6333 dtrace_hash_remove(dtrace_byname, probe); 6334 6335 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 6336 probe->dtpr_arg); 6337 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 6338 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 6339 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 6340 kmem_free(probe, sizeof (dtrace_probe_t)); 6341 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 6342 } 6343 6344 mutex_exit(&dtrace_lock); 6345 mutex_exit(&dtrace_provider_lock); 6346 6347 return (0); 6348 } 6349 6350 /* 6351 * DTrace Probe Management Functions 6352 * 6353 * The functions in this section perform the DTrace probe management, 6354 * including functions to create probes, look-up probes, and call into the 6355 * providers to request that probes be provided. Some of these functions are 6356 * in the Provider-to-Framework API; these functions can be identified by the 6357 * fact that they are not declared "static". 6358 */ 6359 6360 /* 6361 * Create a probe with the specified module name, function name, and name. 6362 */ 6363 dtrace_id_t 6364 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 6365 const char *func, const char *name, int aframes, void *arg) 6366 { 6367 dtrace_probe_t *probe, **probes; 6368 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 6369 dtrace_id_t id; 6370 6371 if (provider == dtrace_provider) { 6372 ASSERT(MUTEX_HELD(&dtrace_lock)); 6373 } else { 6374 mutex_enter(&dtrace_lock); 6375 } 6376 6377 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 6378 VM_BESTFIT | VM_SLEEP); 6379 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 6380 6381 probe->dtpr_id = id; 6382 probe->dtpr_gen = dtrace_probegen++; 6383 probe->dtpr_mod = dtrace_strdup(mod); 6384 probe->dtpr_func = dtrace_strdup(func); 6385 probe->dtpr_name = dtrace_strdup(name); 6386 probe->dtpr_arg = arg; 6387 probe->dtpr_aframes = aframes; 6388 probe->dtpr_provider = provider; 6389 6390 dtrace_hash_add(dtrace_bymod, probe); 6391 dtrace_hash_add(dtrace_byfunc, probe); 6392 dtrace_hash_add(dtrace_byname, probe); 6393 6394 if (id - 1 >= dtrace_nprobes) { 6395 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 6396 size_t nsize = osize << 1; 6397 6398 if (nsize == 0) { 6399 ASSERT(osize == 0); 6400 ASSERT(dtrace_probes == NULL); 6401 nsize = sizeof (dtrace_probe_t *); 6402 } 6403 6404 probes = kmem_zalloc(nsize, KM_SLEEP); 6405 6406 if (dtrace_probes == NULL) { 6407 ASSERT(osize == 0); 6408 dtrace_probes = probes; 6409 dtrace_nprobes = 1; 6410 } else { 6411 dtrace_probe_t **oprobes = dtrace_probes; 6412 6413 bcopy(oprobes, probes, osize); 6414 dtrace_membar_producer(); 6415 dtrace_probes = probes; 6416 6417 dtrace_sync(); 6418 6419 /* 6420 * All CPUs are now seeing the new probes array; we can 6421 * safely free the old array. 6422 */ 6423 kmem_free(oprobes, osize); 6424 dtrace_nprobes <<= 1; 6425 } 6426 6427 ASSERT(id - 1 < dtrace_nprobes); 6428 } 6429 6430 ASSERT(dtrace_probes[id - 1] == NULL); 6431 dtrace_probes[id - 1] = probe; 6432 6433 if (provider != dtrace_provider) 6434 mutex_exit(&dtrace_lock); 6435 6436 return (id); 6437 } 6438 6439 static dtrace_probe_t * 6440 dtrace_probe_lookup_id(dtrace_id_t id) 6441 { 6442 ASSERT(MUTEX_HELD(&dtrace_lock)); 6443 6444 if (id == 0 || id > dtrace_nprobes) 6445 return (NULL); 6446 6447 return (dtrace_probes[id - 1]); 6448 } 6449 6450 static int 6451 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 6452 { 6453 *((dtrace_id_t *)arg) = probe->dtpr_id; 6454 6455 return (DTRACE_MATCH_DONE); 6456 } 6457 6458 /* 6459 * Look up a probe based on provider and one or more of module name, function 6460 * name and probe name. 6461 */ 6462 dtrace_id_t 6463 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, 6464 const char *func, const char *name) 6465 { 6466 dtrace_probekey_t pkey; 6467 dtrace_id_t id; 6468 int match; 6469 6470 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 6471 pkey.dtpk_pmatch = &dtrace_match_string; 6472 pkey.dtpk_mod = mod; 6473 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 6474 pkey.dtpk_func = func; 6475 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 6476 pkey.dtpk_name = name; 6477 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 6478 pkey.dtpk_id = DTRACE_IDNONE; 6479 6480 mutex_enter(&dtrace_lock); 6481 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 6482 dtrace_probe_lookup_match, &id); 6483 mutex_exit(&dtrace_lock); 6484 6485 ASSERT(match == 1 || match == 0); 6486 return (match ? id : 0); 6487 } 6488 6489 /* 6490 * Returns the probe argument associated with the specified probe. 6491 */ 6492 void * 6493 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 6494 { 6495 dtrace_probe_t *probe; 6496 void *rval = NULL; 6497 6498 mutex_enter(&dtrace_lock); 6499 6500 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 6501 probe->dtpr_provider == (dtrace_provider_t *)id) 6502 rval = probe->dtpr_arg; 6503 6504 mutex_exit(&dtrace_lock); 6505 6506 return (rval); 6507 } 6508 6509 /* 6510 * Copy a probe into a probe description. 6511 */ 6512 static void 6513 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 6514 { 6515 bzero(pdp, sizeof (dtrace_probedesc_t)); 6516 pdp->dtpd_id = prp->dtpr_id; 6517 6518 (void) strncpy(pdp->dtpd_provider, 6519 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 6520 6521 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 6522 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 6523 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 6524 } 6525 6526 /* 6527 * Called to indicate that a probe -- or probes -- should be provided by a 6528 * specfied provider. If the specified description is NULL, the provider will 6529 * be told to provide all of its probes. (This is done whenever a new 6530 * consumer comes along, or whenever a retained enabling is to be matched.) If 6531 * the specified description is non-NULL, the provider is given the 6532 * opportunity to dynamically provide the specified probe, allowing providers 6533 * to support the creation of probes on-the-fly. (So-called _autocreated_ 6534 * probes.) If the provider is NULL, the operations will be applied to all 6535 * providers; if the provider is non-NULL the operations will only be applied 6536 * to the specified provider. The dtrace_provider_lock must be held, and the 6537 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 6538 * will need to grab the dtrace_lock when it reenters the framework through 6539 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 6540 */ 6541 static void 6542 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 6543 { 6544 struct modctl *ctl; 6545 int all = 0; 6546 6547 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6548 6549 if (prv == NULL) { 6550 all = 1; 6551 prv = dtrace_provider; 6552 } 6553 6554 do { 6555 /* 6556 * First, call the blanket provide operation. 6557 */ 6558 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 6559 6560 /* 6561 * Now call the per-module provide operation. We will grab 6562 * mod_lock to prevent the list from being modified. Note 6563 * that this also prevents the mod_busy bits from changing. 6564 * (mod_busy can only be changed with mod_lock held.) 6565 */ 6566 mutex_enter(&mod_lock); 6567 6568 ctl = &modules; 6569 do { 6570 if (ctl->mod_busy || ctl->mod_mp == NULL) 6571 continue; 6572 6573 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 6574 6575 } while ((ctl = ctl->mod_next) != &modules); 6576 6577 mutex_exit(&mod_lock); 6578 } while (all && (prv = prv->dtpv_next) != NULL); 6579 } 6580 6581 /* 6582 * Iterate over each probe, and call the Framework-to-Provider API function 6583 * denoted by offs. 6584 */ 6585 static void 6586 dtrace_probe_foreach(uintptr_t offs) 6587 { 6588 dtrace_provider_t *prov; 6589 void (*func)(void *, dtrace_id_t, void *); 6590 dtrace_probe_t *probe; 6591 dtrace_icookie_t cookie; 6592 int i; 6593 6594 /* 6595 * We disable interrupts to walk through the probe array. This is 6596 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 6597 * won't see stale data. 6598 */ 6599 cookie = dtrace_interrupt_disable(); 6600 6601 for (i = 0; i < dtrace_nprobes; i++) { 6602 if ((probe = dtrace_probes[i]) == NULL) 6603 continue; 6604 6605 if (probe->dtpr_ecb == NULL) { 6606 /* 6607 * This probe isn't enabled -- don't call the function. 6608 */ 6609 continue; 6610 } 6611 6612 prov = probe->dtpr_provider; 6613 func = *((void(**)(void *, dtrace_id_t, void *)) 6614 ((uintptr_t)&prov->dtpv_pops + offs)); 6615 6616 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 6617 } 6618 6619 dtrace_interrupt_enable(cookie); 6620 } 6621 6622 static int 6623 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 6624 { 6625 dtrace_probekey_t pkey; 6626 uint32_t priv; 6627 uid_t uid; 6628 zoneid_t zoneid; 6629 6630 ASSERT(MUTEX_HELD(&dtrace_lock)); 6631 dtrace_ecb_create_cache = NULL; 6632 6633 if (desc == NULL) { 6634 /* 6635 * If we're passed a NULL description, we're being asked to 6636 * create an ECB with a NULL probe. 6637 */ 6638 (void) dtrace_ecb_create_enable(NULL, enab); 6639 return (0); 6640 } 6641 6642 dtrace_probekey(desc, &pkey); 6643 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 6644 &priv, &uid, &zoneid); 6645 6646 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 6647 enab)); 6648 } 6649 6650 /* 6651 * DTrace Helper Provider Functions 6652 */ 6653 static void 6654 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 6655 { 6656 attr->dtat_name = DOF_ATTR_NAME(dofattr); 6657 attr->dtat_data = DOF_ATTR_DATA(dofattr); 6658 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 6659 } 6660 6661 static void 6662 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 6663 const dof_provider_t *dofprov, char *strtab) 6664 { 6665 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 6666 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 6667 dofprov->dofpv_provattr); 6668 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 6669 dofprov->dofpv_modattr); 6670 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 6671 dofprov->dofpv_funcattr); 6672 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 6673 dofprov->dofpv_nameattr); 6674 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 6675 dofprov->dofpv_argsattr); 6676 } 6677 6678 static void 6679 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 6680 { 6681 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6682 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6683 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 6684 dof_provider_t *provider; 6685 dof_probe_t *probe; 6686 uint32_t *off, *enoff; 6687 uint8_t *arg; 6688 char *strtab; 6689 uint_t i, nprobes; 6690 dtrace_helper_provdesc_t dhpv; 6691 dtrace_helper_probedesc_t dhpb; 6692 dtrace_meta_t *meta = dtrace_meta_pid; 6693 dtrace_mops_t *mops = &meta->dtm_mops; 6694 void *parg; 6695 6696 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 6697 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6698 provider->dofpv_strtab * dof->dofh_secsize); 6699 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6700 provider->dofpv_probes * dof->dofh_secsize); 6701 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6702 provider->dofpv_prargs * dof->dofh_secsize); 6703 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6704 provider->dofpv_proffs * dof->dofh_secsize); 6705 6706 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 6707 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 6708 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 6709 enoff = NULL; 6710 6711 /* 6712 * See dtrace_helper_provider_validate(). 6713 */ 6714 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 6715 provider->dofpv_prenoffs != DOF_SECT_NONE) { 6716 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6717 provider->dofpv_prenoffs * dof->dofh_secsize); 6718 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 6719 } 6720 6721 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 6722 6723 /* 6724 * Create the provider. 6725 */ 6726 dtrace_dofprov2hprov(&dhpv, provider, strtab); 6727 6728 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 6729 return; 6730 6731 meta->dtm_count++; 6732 6733 /* 6734 * Create the probes. 6735 */ 6736 for (i = 0; i < nprobes; i++) { 6737 probe = (dof_probe_t *)(uintptr_t)(daddr + 6738 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 6739 6740 dhpb.dthpb_mod = dhp->dofhp_mod; 6741 dhpb.dthpb_func = strtab + probe->dofpr_func; 6742 dhpb.dthpb_name = strtab + probe->dofpr_name; 6743 dhpb.dthpb_base = probe->dofpr_addr; 6744 dhpb.dthpb_offs = off + probe->dofpr_offidx; 6745 dhpb.dthpb_noffs = probe->dofpr_noffs; 6746 if (enoff != NULL) { 6747 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 6748 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 6749 } else { 6750 dhpb.dthpb_enoffs = NULL; 6751 dhpb.dthpb_nenoffs = 0; 6752 } 6753 dhpb.dthpb_args = arg + probe->dofpr_argidx; 6754 dhpb.dthpb_nargc = probe->dofpr_nargc; 6755 dhpb.dthpb_xargc = probe->dofpr_xargc; 6756 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 6757 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 6758 6759 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 6760 } 6761 } 6762 6763 static void 6764 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 6765 { 6766 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6767 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6768 int i; 6769 6770 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 6771 6772 for (i = 0; i < dof->dofh_secnum; i++) { 6773 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 6774 dof->dofh_secoff + i * dof->dofh_secsize); 6775 6776 if (sec->dofs_type != DOF_SECT_PROVIDER) 6777 continue; 6778 6779 dtrace_helper_provide_one(dhp, sec, pid); 6780 } 6781 6782 /* 6783 * We may have just created probes, so we must now rematch against 6784 * any retained enablings. Note that this call will acquire both 6785 * cpu_lock and dtrace_lock; the fact that we are holding 6786 * dtrace_meta_lock now is what defines the ordering with respect to 6787 * these three locks. 6788 */ 6789 dtrace_enabling_matchall(); 6790 } 6791 6792 static void 6793 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 6794 { 6795 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6796 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6797 dof_sec_t *str_sec; 6798 dof_provider_t *provider; 6799 char *strtab; 6800 dtrace_helper_provdesc_t dhpv; 6801 dtrace_meta_t *meta = dtrace_meta_pid; 6802 dtrace_mops_t *mops = &meta->dtm_mops; 6803 6804 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 6805 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 6806 provider->dofpv_strtab * dof->dofh_secsize); 6807 6808 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 6809 6810 /* 6811 * Create the provider. 6812 */ 6813 dtrace_dofprov2hprov(&dhpv, provider, strtab); 6814 6815 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 6816 6817 meta->dtm_count--; 6818 } 6819 6820 static void 6821 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 6822 { 6823 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 6824 dof_hdr_t *dof = (dof_hdr_t *)daddr; 6825 int i; 6826 6827 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 6828 6829 for (i = 0; i < dof->dofh_secnum; i++) { 6830 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 6831 dof->dofh_secoff + i * dof->dofh_secsize); 6832 6833 if (sec->dofs_type != DOF_SECT_PROVIDER) 6834 continue; 6835 6836 dtrace_helper_provider_remove_one(dhp, sec, pid); 6837 } 6838 } 6839 6840 /* 6841 * DTrace Meta Provider-to-Framework API Functions 6842 * 6843 * These functions implement the Meta Provider-to-Framework API, as described 6844 * in <sys/dtrace.h>. 6845 */ 6846 int 6847 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 6848 dtrace_meta_provider_id_t *idp) 6849 { 6850 dtrace_meta_t *meta; 6851 dtrace_helpers_t *help, *next; 6852 int i; 6853 6854 *idp = DTRACE_METAPROVNONE; 6855 6856 /* 6857 * We strictly don't need the name, but we hold onto it for 6858 * debuggability. All hail error queues! 6859 */ 6860 if (name == NULL) { 6861 cmn_err(CE_WARN, "failed to register meta-provider: " 6862 "invalid name"); 6863 return (EINVAL); 6864 } 6865 6866 if (mops == NULL || 6867 mops->dtms_create_probe == NULL || 6868 mops->dtms_provide_pid == NULL || 6869 mops->dtms_remove_pid == NULL) { 6870 cmn_err(CE_WARN, "failed to register meta-register %s: " 6871 "invalid ops", name); 6872 return (EINVAL); 6873 } 6874 6875 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 6876 meta->dtm_mops = *mops; 6877 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 6878 (void) strcpy(meta->dtm_name, name); 6879 meta->dtm_arg = arg; 6880 6881 mutex_enter(&dtrace_meta_lock); 6882 mutex_enter(&dtrace_lock); 6883 6884 if (dtrace_meta_pid != NULL) { 6885 mutex_exit(&dtrace_lock); 6886 mutex_exit(&dtrace_meta_lock); 6887 cmn_err(CE_WARN, "failed to register meta-register %s: " 6888 "user-land meta-provider exists", name); 6889 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 6890 kmem_free(meta, sizeof (dtrace_meta_t)); 6891 return (EINVAL); 6892 } 6893 6894 dtrace_meta_pid = meta; 6895 *idp = (dtrace_meta_provider_id_t)meta; 6896 6897 /* 6898 * If there are providers and probes ready to go, pass them 6899 * off to the new meta provider now. 6900 */ 6901 6902 help = dtrace_deferred_pid; 6903 dtrace_deferred_pid = NULL; 6904 6905 mutex_exit(&dtrace_lock); 6906 6907 while (help != NULL) { 6908 for (i = 0; i < help->dthps_nprovs; i++) { 6909 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 6910 help->dthps_pid); 6911 } 6912 6913 next = help->dthps_next; 6914 help->dthps_next = NULL; 6915 help->dthps_prev = NULL; 6916 help->dthps_deferred = 0; 6917 help = next; 6918 } 6919 6920 mutex_exit(&dtrace_meta_lock); 6921 6922 return (0); 6923 } 6924 6925 int 6926 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 6927 { 6928 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 6929 6930 mutex_enter(&dtrace_meta_lock); 6931 mutex_enter(&dtrace_lock); 6932 6933 if (old == dtrace_meta_pid) { 6934 pp = &dtrace_meta_pid; 6935 } else { 6936 panic("attempt to unregister non-existent " 6937 "dtrace meta-provider %p\n", (void *)old); 6938 } 6939 6940 if (old->dtm_count != 0) { 6941 mutex_exit(&dtrace_lock); 6942 mutex_exit(&dtrace_meta_lock); 6943 return (EBUSY); 6944 } 6945 6946 *pp = NULL; 6947 6948 mutex_exit(&dtrace_lock); 6949 mutex_exit(&dtrace_meta_lock); 6950 6951 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 6952 kmem_free(old, sizeof (dtrace_meta_t)); 6953 6954 return (0); 6955 } 6956 6957 6958 /* 6959 * DTrace DIF Object Functions 6960 */ 6961 static int 6962 dtrace_difo_err(uint_t pc, const char *format, ...) 6963 { 6964 if (dtrace_err_verbose) { 6965 va_list alist; 6966 6967 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 6968 va_start(alist, format); 6969 (void) vuprintf(format, alist); 6970 va_end(alist); 6971 } 6972 6973 #ifdef DTRACE_ERRDEBUG 6974 dtrace_errdebug(format); 6975 #endif 6976 return (1); 6977 } 6978 6979 /* 6980 * Validate a DTrace DIF object by checking the IR instructions. The following 6981 * rules are currently enforced by dtrace_difo_validate(): 6982 * 6983 * 1. Each instruction must have a valid opcode 6984 * 2. Each register, string, variable, or subroutine reference must be valid 6985 * 3. No instruction can modify register %r0 (must be zero) 6986 * 4. All instruction reserved bits must be set to zero 6987 * 5. The last instruction must be a "ret" instruction 6988 * 6. All branch targets must reference a valid instruction _after_ the branch 6989 */ 6990 static int 6991 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 6992 cred_t *cr) 6993 { 6994 int err = 0, i; 6995 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 6996 int kcheck; 6997 uint_t pc; 6998 6999 kcheck = cr == NULL || 7000 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE) == 0; 7001 7002 dp->dtdo_destructive = 0; 7003 7004 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 7005 dif_instr_t instr = dp->dtdo_buf[pc]; 7006 7007 uint_t r1 = DIF_INSTR_R1(instr); 7008 uint_t r2 = DIF_INSTR_R2(instr); 7009 uint_t rd = DIF_INSTR_RD(instr); 7010 uint_t rs = DIF_INSTR_RS(instr); 7011 uint_t label = DIF_INSTR_LABEL(instr); 7012 uint_t v = DIF_INSTR_VAR(instr); 7013 uint_t subr = DIF_INSTR_SUBR(instr); 7014 uint_t type = DIF_INSTR_TYPE(instr); 7015 uint_t op = DIF_INSTR_OP(instr); 7016 7017 switch (op) { 7018 case DIF_OP_OR: 7019 case DIF_OP_XOR: 7020 case DIF_OP_AND: 7021 case DIF_OP_SLL: 7022 case DIF_OP_SRL: 7023 case DIF_OP_SRA: 7024 case DIF_OP_SUB: 7025 case DIF_OP_ADD: 7026 case DIF_OP_MUL: 7027 case DIF_OP_SDIV: 7028 case DIF_OP_UDIV: 7029 case DIF_OP_SREM: 7030 case DIF_OP_UREM: 7031 case DIF_OP_COPYS: 7032 if (r1 >= nregs) 7033 err += efunc(pc, "invalid register %u\n", r1); 7034 if (r2 >= nregs) 7035 err += efunc(pc, "invalid register %u\n", r2); 7036 if (rd >= nregs) 7037 err += efunc(pc, "invalid register %u\n", rd); 7038 if (rd == 0) 7039 err += efunc(pc, "cannot write to %r0\n"); 7040 break; 7041 case DIF_OP_NOT: 7042 case DIF_OP_MOV: 7043 case DIF_OP_ALLOCS: 7044 if (r1 >= nregs) 7045 err += efunc(pc, "invalid register %u\n", r1); 7046 if (r2 != 0) 7047 err += efunc(pc, "non-zero reserved bits\n"); 7048 if (rd >= nregs) 7049 err += efunc(pc, "invalid register %u\n", rd); 7050 if (rd == 0) 7051 err += efunc(pc, "cannot write to %r0\n"); 7052 break; 7053 case DIF_OP_LDSB: 7054 case DIF_OP_LDSH: 7055 case DIF_OP_LDSW: 7056 case DIF_OP_LDUB: 7057 case DIF_OP_LDUH: 7058 case DIF_OP_LDUW: 7059 case DIF_OP_LDX: 7060 if (r1 >= nregs) 7061 err += efunc(pc, "invalid register %u\n", r1); 7062 if (r2 != 0) 7063 err += efunc(pc, "non-zero reserved bits\n"); 7064 if (rd >= nregs) 7065 err += efunc(pc, "invalid register %u\n", rd); 7066 if (rd == 0) 7067 err += efunc(pc, "cannot write to %r0\n"); 7068 if (kcheck) 7069 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 7070 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 7071 break; 7072 case DIF_OP_RLDSB: 7073 case DIF_OP_RLDSH: 7074 case DIF_OP_RLDSW: 7075 case DIF_OP_RLDUB: 7076 case DIF_OP_RLDUH: 7077 case DIF_OP_RLDUW: 7078 case DIF_OP_RLDX: 7079 if (r1 >= nregs) 7080 err += efunc(pc, "invalid register %u\n", r1); 7081 if (r2 != 0) 7082 err += efunc(pc, "non-zero reserved bits\n"); 7083 if (rd >= nregs) 7084 err += efunc(pc, "invalid register %u\n", rd); 7085 if (rd == 0) 7086 err += efunc(pc, "cannot write to %r0\n"); 7087 break; 7088 case DIF_OP_ULDSB: 7089 case DIF_OP_ULDSH: 7090 case DIF_OP_ULDSW: 7091 case DIF_OP_ULDUB: 7092 case DIF_OP_ULDUH: 7093 case DIF_OP_ULDUW: 7094 case DIF_OP_ULDX: 7095 if (r1 >= nregs) 7096 err += efunc(pc, "invalid register %u\n", r1); 7097 if (r2 != 0) 7098 err += efunc(pc, "non-zero reserved bits\n"); 7099 if (rd >= nregs) 7100 err += efunc(pc, "invalid register %u\n", rd); 7101 if (rd == 0) 7102 err += efunc(pc, "cannot write to %r0\n"); 7103 break; 7104 case DIF_OP_STB: 7105 case DIF_OP_STH: 7106 case DIF_OP_STW: 7107 case DIF_OP_STX: 7108 if (r1 >= nregs) 7109 err += efunc(pc, "invalid register %u\n", r1); 7110 if (r2 != 0) 7111 err += efunc(pc, "non-zero reserved bits\n"); 7112 if (rd >= nregs) 7113 err += efunc(pc, "invalid register %u\n", rd); 7114 if (rd == 0) 7115 err += efunc(pc, "cannot write to 0 address\n"); 7116 break; 7117 case DIF_OP_CMP: 7118 case DIF_OP_SCMP: 7119 if (r1 >= nregs) 7120 err += efunc(pc, "invalid register %u\n", r1); 7121 if (r2 >= nregs) 7122 err += efunc(pc, "invalid register %u\n", r2); 7123 if (rd != 0) 7124 err += efunc(pc, "non-zero reserved bits\n"); 7125 break; 7126 case DIF_OP_TST: 7127 if (r1 >= nregs) 7128 err += efunc(pc, "invalid register %u\n", r1); 7129 if (r2 != 0 || rd != 0) 7130 err += efunc(pc, "non-zero reserved bits\n"); 7131 break; 7132 case DIF_OP_BA: 7133 case DIF_OP_BE: 7134 case DIF_OP_BNE: 7135 case DIF_OP_BG: 7136 case DIF_OP_BGU: 7137 case DIF_OP_BGE: 7138 case DIF_OP_BGEU: 7139 case DIF_OP_BL: 7140 case DIF_OP_BLU: 7141 case DIF_OP_BLE: 7142 case DIF_OP_BLEU: 7143 if (label >= dp->dtdo_len) { 7144 err += efunc(pc, "invalid branch target %u\n", 7145 label); 7146 } 7147 if (label <= pc) { 7148 err += efunc(pc, "backward branch to %u\n", 7149 label); 7150 } 7151 break; 7152 case DIF_OP_RET: 7153 if (r1 != 0 || r2 != 0) 7154 err += efunc(pc, "non-zero reserved bits\n"); 7155 if (rd >= nregs) 7156 err += efunc(pc, "invalid register %u\n", rd); 7157 break; 7158 case DIF_OP_NOP: 7159 case DIF_OP_POPTS: 7160 case DIF_OP_FLUSHTS: 7161 if (r1 != 0 || r2 != 0 || rd != 0) 7162 err += efunc(pc, "non-zero reserved bits\n"); 7163 break; 7164 case DIF_OP_SETX: 7165 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 7166 err += efunc(pc, "invalid integer ref %u\n", 7167 DIF_INSTR_INTEGER(instr)); 7168 } 7169 if (rd >= nregs) 7170 err += efunc(pc, "invalid register %u\n", rd); 7171 if (rd == 0) 7172 err += efunc(pc, "cannot write to %r0\n"); 7173 break; 7174 case DIF_OP_SETS: 7175 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 7176 err += efunc(pc, "invalid string ref %u\n", 7177 DIF_INSTR_STRING(instr)); 7178 } 7179 if (rd >= nregs) 7180 err += efunc(pc, "invalid register %u\n", rd); 7181 if (rd == 0) 7182 err += efunc(pc, "cannot write to %r0\n"); 7183 break; 7184 case DIF_OP_LDGA: 7185 case DIF_OP_LDTA: 7186 if (r1 > DIF_VAR_ARRAY_MAX) 7187 err += efunc(pc, "invalid array %u\n", r1); 7188 if (r2 >= nregs) 7189 err += efunc(pc, "invalid register %u\n", r2); 7190 if (rd >= nregs) 7191 err += efunc(pc, "invalid register %u\n", rd); 7192 if (rd == 0) 7193 err += efunc(pc, "cannot write to %r0\n"); 7194 break; 7195 case DIF_OP_LDGS: 7196 case DIF_OP_LDTS: 7197 case DIF_OP_LDLS: 7198 case DIF_OP_LDGAA: 7199 case DIF_OP_LDTAA: 7200 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 7201 err += efunc(pc, "invalid variable %u\n", v); 7202 if (rd >= nregs) 7203 err += efunc(pc, "invalid register %u\n", rd); 7204 if (rd == 0) 7205 err += efunc(pc, "cannot write to %r0\n"); 7206 break; 7207 case DIF_OP_STGS: 7208 case DIF_OP_STTS: 7209 case DIF_OP_STLS: 7210 case DIF_OP_STGAA: 7211 case DIF_OP_STTAA: 7212 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 7213 err += efunc(pc, "invalid variable %u\n", v); 7214 if (rs >= nregs) 7215 err += efunc(pc, "invalid register %u\n", rd); 7216 break; 7217 case DIF_OP_CALL: 7218 if (subr > DIF_SUBR_MAX) 7219 err += efunc(pc, "invalid subr %u\n", subr); 7220 if (rd >= nregs) 7221 err += efunc(pc, "invalid register %u\n", rd); 7222 if (rd == 0) 7223 err += efunc(pc, "cannot write to %r0\n"); 7224 7225 if (subr == DIF_SUBR_COPYOUT || 7226 subr == DIF_SUBR_COPYOUTSTR) { 7227 dp->dtdo_destructive = 1; 7228 } 7229 break; 7230 case DIF_OP_PUSHTR: 7231 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 7232 err += efunc(pc, "invalid ref type %u\n", type); 7233 if (r2 >= nregs) 7234 err += efunc(pc, "invalid register %u\n", r2); 7235 if (rs >= nregs) 7236 err += efunc(pc, "invalid register %u\n", rs); 7237 break; 7238 case DIF_OP_PUSHTV: 7239 if (type != DIF_TYPE_CTF) 7240 err += efunc(pc, "invalid val type %u\n", type); 7241 if (r2 >= nregs) 7242 err += efunc(pc, "invalid register %u\n", r2); 7243 if (rs >= nregs) 7244 err += efunc(pc, "invalid register %u\n", rs); 7245 break; 7246 default: 7247 err += efunc(pc, "invalid opcode %u\n", 7248 DIF_INSTR_OP(instr)); 7249 } 7250 } 7251 7252 if (dp->dtdo_len != 0 && 7253 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 7254 err += efunc(dp->dtdo_len - 1, 7255 "expected 'ret' as last DIF instruction\n"); 7256 } 7257 7258 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 7259 /* 7260 * If we're not returning by reference, the size must be either 7261 * 0 or the size of one of the base types. 7262 */ 7263 switch (dp->dtdo_rtype.dtdt_size) { 7264 case 0: 7265 case sizeof (uint8_t): 7266 case sizeof (uint16_t): 7267 case sizeof (uint32_t): 7268 case sizeof (uint64_t): 7269 break; 7270 7271 default: 7272 err += efunc(dp->dtdo_len - 1, "bad return size"); 7273 } 7274 } 7275 7276 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 7277 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 7278 dtrace_diftype_t *vt, *et; 7279 uint_t id, ndx; 7280 7281 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 7282 v->dtdv_scope != DIFV_SCOPE_THREAD && 7283 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 7284 err += efunc(i, "unrecognized variable scope %d\n", 7285 v->dtdv_scope); 7286 break; 7287 } 7288 7289 if (v->dtdv_kind != DIFV_KIND_ARRAY && 7290 v->dtdv_kind != DIFV_KIND_SCALAR) { 7291 err += efunc(i, "unrecognized variable type %d\n", 7292 v->dtdv_kind); 7293 break; 7294 } 7295 7296 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 7297 err += efunc(i, "%d exceeds variable id limit\n", id); 7298 break; 7299 } 7300 7301 if (id < DIF_VAR_OTHER_UBASE) 7302 continue; 7303 7304 /* 7305 * For user-defined variables, we need to check that this 7306 * definition is identical to any previous definition that we 7307 * encountered. 7308 */ 7309 ndx = id - DIF_VAR_OTHER_UBASE; 7310 7311 switch (v->dtdv_scope) { 7312 case DIFV_SCOPE_GLOBAL: 7313 if (ndx < vstate->dtvs_nglobals) { 7314 dtrace_statvar_t *svar; 7315 7316 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 7317 existing = &svar->dtsv_var; 7318 } 7319 7320 break; 7321 7322 case DIFV_SCOPE_THREAD: 7323 if (ndx < vstate->dtvs_ntlocals) 7324 existing = &vstate->dtvs_tlocals[ndx]; 7325 break; 7326 7327 case DIFV_SCOPE_LOCAL: 7328 if (ndx < vstate->dtvs_nlocals) { 7329 dtrace_statvar_t *svar; 7330 7331 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 7332 existing = &svar->dtsv_var; 7333 } 7334 7335 break; 7336 } 7337 7338 vt = &v->dtdv_type; 7339 7340 if (vt->dtdt_flags & DIF_TF_BYREF) { 7341 if (vt->dtdt_size == 0) { 7342 err += efunc(i, "zero-sized variable\n"); 7343 break; 7344 } 7345 7346 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 7347 vt->dtdt_size > dtrace_global_maxsize) { 7348 err += efunc(i, "oversized by-ref global\n"); 7349 break; 7350 } 7351 } 7352 7353 if (existing == NULL || existing->dtdv_id == 0) 7354 continue; 7355 7356 ASSERT(existing->dtdv_id == v->dtdv_id); 7357 ASSERT(existing->dtdv_scope == v->dtdv_scope); 7358 7359 if (existing->dtdv_kind != v->dtdv_kind) 7360 err += efunc(i, "%d changed variable kind\n", id); 7361 7362 et = &existing->dtdv_type; 7363 7364 if (vt->dtdt_flags != et->dtdt_flags) { 7365 err += efunc(i, "%d changed variable type flags\n", id); 7366 break; 7367 } 7368 7369 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 7370 err += efunc(i, "%d changed variable type size\n", id); 7371 break; 7372 } 7373 } 7374 7375 return (err); 7376 } 7377 7378 /* 7379 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 7380 * are much more constrained than normal DIFOs. Specifically, they may 7381 * not: 7382 * 7383 * 1. Make calls to subroutines other than copyin(), copyinstr() or 7384 * miscellaneous string routines 7385 * 2. Access DTrace variables other than the args[] array, and the 7386 * curthread, pid, tid and execname variables. 7387 * 3. Have thread-local variables. 7388 * 4. Have dynamic variables. 7389 */ 7390 static int 7391 dtrace_difo_validate_helper(dtrace_difo_t *dp) 7392 { 7393 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 7394 int err = 0; 7395 uint_t pc; 7396 7397 for (pc = 0; pc < dp->dtdo_len; pc++) { 7398 dif_instr_t instr = dp->dtdo_buf[pc]; 7399 7400 uint_t v = DIF_INSTR_VAR(instr); 7401 uint_t subr = DIF_INSTR_SUBR(instr); 7402 uint_t op = DIF_INSTR_OP(instr); 7403 7404 switch (op) { 7405 case DIF_OP_OR: 7406 case DIF_OP_XOR: 7407 case DIF_OP_AND: 7408 case DIF_OP_SLL: 7409 case DIF_OP_SRL: 7410 case DIF_OP_SRA: 7411 case DIF_OP_SUB: 7412 case DIF_OP_ADD: 7413 case DIF_OP_MUL: 7414 case DIF_OP_SDIV: 7415 case DIF_OP_UDIV: 7416 case DIF_OP_SREM: 7417 case DIF_OP_UREM: 7418 case DIF_OP_COPYS: 7419 case DIF_OP_NOT: 7420 case DIF_OP_MOV: 7421 case DIF_OP_RLDSB: 7422 case DIF_OP_RLDSH: 7423 case DIF_OP_RLDSW: 7424 case DIF_OP_RLDUB: 7425 case DIF_OP_RLDUH: 7426 case DIF_OP_RLDUW: 7427 case DIF_OP_RLDX: 7428 case DIF_OP_ULDSB: 7429 case DIF_OP_ULDSH: 7430 case DIF_OP_ULDSW: 7431 case DIF_OP_ULDUB: 7432 case DIF_OP_ULDUH: 7433 case DIF_OP_ULDUW: 7434 case DIF_OP_ULDX: 7435 case DIF_OP_STB: 7436 case DIF_OP_STH: 7437 case DIF_OP_STW: 7438 case DIF_OP_STX: 7439 case DIF_OP_ALLOCS: 7440 case DIF_OP_CMP: 7441 case DIF_OP_SCMP: 7442 case DIF_OP_TST: 7443 case DIF_OP_BA: 7444 case DIF_OP_BE: 7445 case DIF_OP_BNE: 7446 case DIF_OP_BG: 7447 case DIF_OP_BGU: 7448 case DIF_OP_BGE: 7449 case DIF_OP_BGEU: 7450 case DIF_OP_BL: 7451 case DIF_OP_BLU: 7452 case DIF_OP_BLE: 7453 case DIF_OP_BLEU: 7454 case DIF_OP_RET: 7455 case DIF_OP_NOP: 7456 case DIF_OP_POPTS: 7457 case DIF_OP_FLUSHTS: 7458 case DIF_OP_SETX: 7459 case DIF_OP_SETS: 7460 case DIF_OP_LDGA: 7461 case DIF_OP_LDLS: 7462 case DIF_OP_STGS: 7463 case DIF_OP_STLS: 7464 case DIF_OP_PUSHTR: 7465 case DIF_OP_PUSHTV: 7466 break; 7467 7468 case DIF_OP_LDGS: 7469 if (v >= DIF_VAR_OTHER_UBASE) 7470 break; 7471 7472 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 7473 break; 7474 7475 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 7476 v == DIF_VAR_TID || v == DIF_VAR_EXECNAME || 7477 v == DIF_VAR_ZONENAME) 7478 break; 7479 7480 err += efunc(pc, "illegal variable %u\n", v); 7481 break; 7482 7483 case DIF_OP_LDTA: 7484 case DIF_OP_LDTS: 7485 case DIF_OP_LDGAA: 7486 case DIF_OP_LDTAA: 7487 err += efunc(pc, "illegal dynamic variable load\n"); 7488 break; 7489 7490 case DIF_OP_STTS: 7491 case DIF_OP_STGAA: 7492 case DIF_OP_STTAA: 7493 err += efunc(pc, "illegal dynamic variable store\n"); 7494 break; 7495 7496 case DIF_OP_CALL: 7497 if (subr == DIF_SUBR_ALLOCA || 7498 subr == DIF_SUBR_BCOPY || 7499 subr == DIF_SUBR_COPYIN || 7500 subr == DIF_SUBR_COPYINTO || 7501 subr == DIF_SUBR_COPYINSTR || 7502 subr == DIF_SUBR_INDEX || 7503 subr == DIF_SUBR_LLTOSTR || 7504 subr == DIF_SUBR_RINDEX || 7505 subr == DIF_SUBR_STRCHR || 7506 subr == DIF_SUBR_STRJOIN || 7507 subr == DIF_SUBR_STRRCHR || 7508 subr == DIF_SUBR_STRSTR) 7509 break; 7510 7511 err += efunc(pc, "invalid subr %u\n", subr); 7512 break; 7513 7514 default: 7515 err += efunc(pc, "invalid opcode %u\n", 7516 DIF_INSTR_OP(instr)); 7517 } 7518 } 7519 7520 return (err); 7521 } 7522 7523 /* 7524 * Returns 1 if the expression in the DIF object can be cached on a per-thread 7525 * basis; 0 if not. 7526 */ 7527 static int 7528 dtrace_difo_cacheable(dtrace_difo_t *dp) 7529 { 7530 int i; 7531 7532 if (dp == NULL) 7533 return (0); 7534 7535 for (i = 0; i < dp->dtdo_varlen; i++) { 7536 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7537 7538 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 7539 continue; 7540 7541 switch (v->dtdv_id) { 7542 case DIF_VAR_CURTHREAD: 7543 case DIF_VAR_PID: 7544 case DIF_VAR_TID: 7545 case DIF_VAR_EXECNAME: 7546 case DIF_VAR_ZONENAME: 7547 break; 7548 7549 default: 7550 return (0); 7551 } 7552 } 7553 7554 /* 7555 * This DIF object may be cacheable. Now we need to look for any 7556 * array loading instructions, any memory loading instructions, or 7557 * any stores to thread-local variables. 7558 */ 7559 for (i = 0; i < dp->dtdo_len; i++) { 7560 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 7561 7562 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 7563 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 7564 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 7565 op == DIF_OP_LDGA || op == DIF_OP_STTS) 7566 return (0); 7567 } 7568 7569 return (1); 7570 } 7571 7572 static void 7573 dtrace_difo_hold(dtrace_difo_t *dp) 7574 { 7575 int i; 7576 7577 ASSERT(MUTEX_HELD(&dtrace_lock)); 7578 7579 dp->dtdo_refcnt++; 7580 ASSERT(dp->dtdo_refcnt != 0); 7581 7582 /* 7583 * We need to check this DIF object for references to the variable 7584 * DIF_VAR_VTIMESTAMP. 7585 */ 7586 for (i = 0; i < dp->dtdo_varlen; i++) { 7587 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7588 7589 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 7590 continue; 7591 7592 if (dtrace_vtime_references++ == 0) 7593 dtrace_vtime_enable(); 7594 } 7595 } 7596 7597 /* 7598 * This routine calculates the dynamic variable chunksize for a given DIF 7599 * object. The calculation is not fool-proof, and can probably be tricked by 7600 * malicious DIF -- but it works for all compiler-generated DIF. Because this 7601 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 7602 * if a dynamic variable size exceeds the chunksize. 7603 */ 7604 static void 7605 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7606 { 7607 uint64_t sval; 7608 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 7609 const dif_instr_t *text = dp->dtdo_buf; 7610 uint_t pc, srd = 0; 7611 uint_t ttop = 0; 7612 size_t size, ksize; 7613 uint_t id, i; 7614 7615 for (pc = 0; pc < dp->dtdo_len; pc++) { 7616 dif_instr_t instr = text[pc]; 7617 uint_t op = DIF_INSTR_OP(instr); 7618 uint_t rd = DIF_INSTR_RD(instr); 7619 uint_t r1 = DIF_INSTR_R1(instr); 7620 uint_t nkeys = 0; 7621 uchar_t scope; 7622 7623 dtrace_key_t *key = tupregs; 7624 7625 switch (op) { 7626 case DIF_OP_SETX: 7627 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 7628 srd = rd; 7629 continue; 7630 7631 case DIF_OP_STTS: 7632 key = &tupregs[DIF_DTR_NREGS]; 7633 key[0].dttk_size = 0; 7634 key[1].dttk_size = 0; 7635 nkeys = 2; 7636 scope = DIFV_SCOPE_THREAD; 7637 break; 7638 7639 case DIF_OP_STGAA: 7640 case DIF_OP_STTAA: 7641 nkeys = ttop; 7642 7643 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 7644 key[nkeys++].dttk_size = 0; 7645 7646 key[nkeys++].dttk_size = 0; 7647 7648 if (op == DIF_OP_STTAA) { 7649 scope = DIFV_SCOPE_THREAD; 7650 } else { 7651 scope = DIFV_SCOPE_GLOBAL; 7652 } 7653 7654 break; 7655 7656 case DIF_OP_PUSHTR: 7657 if (ttop == DIF_DTR_NREGS) 7658 return; 7659 7660 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 7661 /* 7662 * If the register for the size of the "pushtr" 7663 * is %r0 (or the value is 0) and the type is 7664 * a string, we'll use the system-wide default 7665 * string size. 7666 */ 7667 tupregs[ttop++].dttk_size = 7668 dtrace_strsize_default; 7669 } else { 7670 if (srd == 0) 7671 return; 7672 7673 tupregs[ttop++].dttk_size = sval; 7674 } 7675 7676 break; 7677 7678 case DIF_OP_PUSHTV: 7679 if (ttop == DIF_DTR_NREGS) 7680 return; 7681 7682 tupregs[ttop++].dttk_size = 0; 7683 break; 7684 7685 case DIF_OP_FLUSHTS: 7686 ttop = 0; 7687 break; 7688 7689 case DIF_OP_POPTS: 7690 if (ttop != 0) 7691 ttop--; 7692 break; 7693 } 7694 7695 sval = 0; 7696 srd = 0; 7697 7698 if (nkeys == 0) 7699 continue; 7700 7701 /* 7702 * We have a dynamic variable allocation; calculate its size. 7703 */ 7704 for (ksize = 0, i = 0; i < nkeys; i++) 7705 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 7706 7707 size = sizeof (dtrace_dynvar_t); 7708 size += sizeof (dtrace_key_t) * (nkeys - 1); 7709 size += ksize; 7710 7711 /* 7712 * Now we need to determine the size of the stored data. 7713 */ 7714 id = DIF_INSTR_VAR(instr); 7715 7716 for (i = 0; i < dp->dtdo_varlen; i++) { 7717 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7718 7719 if (v->dtdv_id == id && v->dtdv_scope == scope) { 7720 size += v->dtdv_type.dtdt_size; 7721 break; 7722 } 7723 } 7724 7725 if (i == dp->dtdo_varlen) 7726 return; 7727 7728 /* 7729 * We have the size. If this is larger than the chunk size 7730 * for our dynamic variable state, reset the chunk size. 7731 */ 7732 size = P2ROUNDUP(size, sizeof (uint64_t)); 7733 7734 if (size > vstate->dtvs_dynvars.dtds_chunksize) 7735 vstate->dtvs_dynvars.dtds_chunksize = size; 7736 } 7737 } 7738 7739 static void 7740 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7741 { 7742 int i, oldsvars, osz, nsz, otlocals, ntlocals; 7743 uint_t id; 7744 7745 ASSERT(MUTEX_HELD(&dtrace_lock)); 7746 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 7747 7748 for (i = 0; i < dp->dtdo_varlen; i++) { 7749 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7750 dtrace_statvar_t *svar, ***svarp; 7751 size_t dsize = 0; 7752 uint8_t scope = v->dtdv_scope; 7753 int *np; 7754 7755 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 7756 continue; 7757 7758 id -= DIF_VAR_OTHER_UBASE; 7759 7760 switch (scope) { 7761 case DIFV_SCOPE_THREAD: 7762 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 7763 dtrace_difv_t *tlocals; 7764 7765 if ((ntlocals = (otlocals << 1)) == 0) 7766 ntlocals = 1; 7767 7768 osz = otlocals * sizeof (dtrace_difv_t); 7769 nsz = ntlocals * sizeof (dtrace_difv_t); 7770 7771 tlocals = kmem_zalloc(nsz, KM_SLEEP); 7772 7773 if (osz != 0) { 7774 bcopy(vstate->dtvs_tlocals, 7775 tlocals, osz); 7776 kmem_free(vstate->dtvs_tlocals, osz); 7777 } 7778 7779 vstate->dtvs_tlocals = tlocals; 7780 vstate->dtvs_ntlocals = ntlocals; 7781 } 7782 7783 vstate->dtvs_tlocals[id] = *v; 7784 continue; 7785 7786 case DIFV_SCOPE_LOCAL: 7787 np = &vstate->dtvs_nlocals; 7788 svarp = &vstate->dtvs_locals; 7789 7790 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 7791 dsize = NCPU * (v->dtdv_type.dtdt_size + 7792 sizeof (uint64_t)); 7793 else 7794 dsize = NCPU * sizeof (uint64_t); 7795 7796 break; 7797 7798 case DIFV_SCOPE_GLOBAL: 7799 np = &vstate->dtvs_nglobals; 7800 svarp = &vstate->dtvs_globals; 7801 7802 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 7803 dsize = v->dtdv_type.dtdt_size + 7804 sizeof (uint64_t); 7805 7806 break; 7807 7808 default: 7809 ASSERT(0); 7810 } 7811 7812 while (id >= (oldsvars = *np)) { 7813 dtrace_statvar_t **statics; 7814 int newsvars, oldsize, newsize; 7815 7816 if ((newsvars = (oldsvars << 1)) == 0) 7817 newsvars = 1; 7818 7819 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 7820 newsize = newsvars * sizeof (dtrace_statvar_t *); 7821 7822 statics = kmem_zalloc(newsize, KM_SLEEP); 7823 7824 if (oldsize != 0) { 7825 bcopy(*svarp, statics, oldsize); 7826 kmem_free(*svarp, oldsize); 7827 } 7828 7829 *svarp = statics; 7830 *np = newsvars; 7831 } 7832 7833 if ((svar = (*svarp)[id]) == NULL) { 7834 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 7835 svar->dtsv_var = *v; 7836 7837 if ((svar->dtsv_size = dsize) != 0) { 7838 svar->dtsv_data = (uint64_t)(uintptr_t) 7839 kmem_zalloc(dsize, KM_SLEEP); 7840 } 7841 7842 (*svarp)[id] = svar; 7843 } 7844 7845 svar->dtsv_refcnt++; 7846 } 7847 7848 dtrace_difo_chunksize(dp, vstate); 7849 dtrace_difo_hold(dp); 7850 } 7851 7852 static dtrace_difo_t * 7853 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7854 { 7855 dtrace_difo_t *new; 7856 size_t sz; 7857 7858 ASSERT(dp->dtdo_buf != NULL); 7859 ASSERT(dp->dtdo_refcnt != 0); 7860 7861 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 7862 7863 ASSERT(dp->dtdo_buf != NULL); 7864 sz = dp->dtdo_len * sizeof (dif_instr_t); 7865 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 7866 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 7867 new->dtdo_len = dp->dtdo_len; 7868 7869 if (dp->dtdo_strtab != NULL) { 7870 ASSERT(dp->dtdo_strlen != 0); 7871 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 7872 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 7873 new->dtdo_strlen = dp->dtdo_strlen; 7874 } 7875 7876 if (dp->dtdo_inttab != NULL) { 7877 ASSERT(dp->dtdo_intlen != 0); 7878 sz = dp->dtdo_intlen * sizeof (uint64_t); 7879 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 7880 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 7881 new->dtdo_intlen = dp->dtdo_intlen; 7882 } 7883 7884 if (dp->dtdo_vartab != NULL) { 7885 ASSERT(dp->dtdo_varlen != 0); 7886 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 7887 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 7888 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 7889 new->dtdo_varlen = dp->dtdo_varlen; 7890 } 7891 7892 dtrace_difo_init(new, vstate); 7893 return (new); 7894 } 7895 7896 static void 7897 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7898 { 7899 int i; 7900 7901 ASSERT(dp->dtdo_refcnt == 0); 7902 7903 for (i = 0; i < dp->dtdo_varlen; i++) { 7904 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7905 dtrace_statvar_t *svar, **svarp; 7906 uint_t id; 7907 uint8_t scope = v->dtdv_scope; 7908 int *np; 7909 7910 switch (scope) { 7911 case DIFV_SCOPE_THREAD: 7912 continue; 7913 7914 case DIFV_SCOPE_LOCAL: 7915 np = &vstate->dtvs_nlocals; 7916 svarp = vstate->dtvs_locals; 7917 break; 7918 7919 case DIFV_SCOPE_GLOBAL: 7920 np = &vstate->dtvs_nglobals; 7921 svarp = vstate->dtvs_globals; 7922 break; 7923 7924 default: 7925 ASSERT(0); 7926 } 7927 7928 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 7929 continue; 7930 7931 id -= DIF_VAR_OTHER_UBASE; 7932 ASSERT(id < *np); 7933 7934 svar = svarp[id]; 7935 ASSERT(svar != NULL); 7936 ASSERT(svar->dtsv_refcnt > 0); 7937 7938 if (--svar->dtsv_refcnt > 0) 7939 continue; 7940 7941 if (svar->dtsv_size != 0) { 7942 ASSERT(svar->dtsv_data != NULL); 7943 kmem_free((void *)(uintptr_t)svar->dtsv_data, 7944 svar->dtsv_size); 7945 } 7946 7947 kmem_free(svar, sizeof (dtrace_statvar_t)); 7948 svarp[id] = NULL; 7949 } 7950 7951 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 7952 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 7953 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 7954 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 7955 7956 kmem_free(dp, sizeof (dtrace_difo_t)); 7957 } 7958 7959 static void 7960 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 7961 { 7962 int i; 7963 7964 ASSERT(MUTEX_HELD(&dtrace_lock)); 7965 ASSERT(dp->dtdo_refcnt != 0); 7966 7967 for (i = 0; i < dp->dtdo_varlen; i++) { 7968 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 7969 7970 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 7971 continue; 7972 7973 ASSERT(dtrace_vtime_references > 0); 7974 if (--dtrace_vtime_references == 0) 7975 dtrace_vtime_disable(); 7976 } 7977 7978 if (--dp->dtdo_refcnt == 0) 7979 dtrace_difo_destroy(dp, vstate); 7980 } 7981 7982 /* 7983 * DTrace Format Functions 7984 */ 7985 static uint16_t 7986 dtrace_format_add(dtrace_state_t *state, char *str) 7987 { 7988 char *fmt, **new; 7989 uint16_t ndx, len = strlen(str) + 1; 7990 7991 fmt = kmem_zalloc(len, KM_SLEEP); 7992 bcopy(str, fmt, len); 7993 7994 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 7995 if (state->dts_formats[ndx] == NULL) { 7996 state->dts_formats[ndx] = fmt; 7997 return (ndx + 1); 7998 } 7999 } 8000 8001 if (state->dts_nformats == USHRT_MAX) { 8002 /* 8003 * This is only likely if a denial-of-service attack is being 8004 * attempted. As such, it's okay to fail silently here. 8005 */ 8006 kmem_free(fmt, len); 8007 return (0); 8008 } 8009 8010 /* 8011 * For simplicity, we always resize the formats array to be exactly the 8012 * number of formats. 8013 */ 8014 ndx = state->dts_nformats++; 8015 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 8016 8017 if (state->dts_formats != NULL) { 8018 ASSERT(ndx != 0); 8019 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 8020 kmem_free(state->dts_formats, ndx * sizeof (char *)); 8021 } 8022 8023 state->dts_formats = new; 8024 state->dts_formats[ndx] = fmt; 8025 8026 return (ndx + 1); 8027 } 8028 8029 static void 8030 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 8031 { 8032 char *fmt; 8033 8034 ASSERT(state->dts_formats != NULL); 8035 ASSERT(format <= state->dts_nformats); 8036 ASSERT(state->dts_formats[format - 1] != NULL); 8037 8038 fmt = state->dts_formats[format - 1]; 8039 kmem_free(fmt, strlen(fmt) + 1); 8040 state->dts_formats[format - 1] = NULL; 8041 } 8042 8043 static void 8044 dtrace_format_destroy(dtrace_state_t *state) 8045 { 8046 int i; 8047 8048 if (state->dts_nformats == 0) { 8049 ASSERT(state->dts_formats == NULL); 8050 return; 8051 } 8052 8053 ASSERT(state->dts_formats != NULL); 8054 8055 for (i = 0; i < state->dts_nformats; i++) { 8056 char *fmt = state->dts_formats[i]; 8057 8058 if (fmt == NULL) 8059 continue; 8060 8061 kmem_free(fmt, strlen(fmt) + 1); 8062 } 8063 8064 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 8065 state->dts_nformats = 0; 8066 state->dts_formats = NULL; 8067 } 8068 8069 /* 8070 * DTrace Predicate Functions 8071 */ 8072 static dtrace_predicate_t * 8073 dtrace_predicate_create(dtrace_difo_t *dp) 8074 { 8075 dtrace_predicate_t *pred; 8076 8077 ASSERT(MUTEX_HELD(&dtrace_lock)); 8078 ASSERT(dp->dtdo_refcnt != 0); 8079 8080 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 8081 pred->dtp_difo = dp; 8082 pred->dtp_refcnt = 1; 8083 8084 if (!dtrace_difo_cacheable(dp)) 8085 return (pred); 8086 8087 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 8088 /* 8089 * This is only theoretically possible -- we have had 2^32 8090 * cacheable predicates on this machine. We cannot allow any 8091 * more predicates to become cacheable: as unlikely as it is, 8092 * there may be a thread caching a (now stale) predicate cache 8093 * ID. (N.B.: the temptation is being successfully resisted to 8094 * have this cmn_err() "Holy shit -- we executed this code!") 8095 */ 8096 return (pred); 8097 } 8098 8099 pred->dtp_cacheid = dtrace_predcache_id++; 8100 8101 return (pred); 8102 } 8103 8104 static void 8105 dtrace_predicate_hold(dtrace_predicate_t *pred) 8106 { 8107 ASSERT(MUTEX_HELD(&dtrace_lock)); 8108 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 8109 ASSERT(pred->dtp_refcnt > 0); 8110 8111 pred->dtp_refcnt++; 8112 } 8113 8114 static void 8115 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 8116 { 8117 dtrace_difo_t *dp = pred->dtp_difo; 8118 8119 ASSERT(MUTEX_HELD(&dtrace_lock)); 8120 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 8121 ASSERT(pred->dtp_refcnt > 0); 8122 8123 if (--pred->dtp_refcnt == 0) { 8124 dtrace_difo_release(pred->dtp_difo, vstate); 8125 kmem_free(pred, sizeof (dtrace_predicate_t)); 8126 } 8127 } 8128 8129 /* 8130 * DTrace Action Description Functions 8131 */ 8132 static dtrace_actdesc_t * 8133 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 8134 uint64_t uarg, uint64_t arg) 8135 { 8136 dtrace_actdesc_t *act; 8137 8138 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 8139 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 8140 8141 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 8142 act->dtad_kind = kind; 8143 act->dtad_ntuple = ntuple; 8144 act->dtad_uarg = uarg; 8145 act->dtad_arg = arg; 8146 act->dtad_refcnt = 1; 8147 8148 return (act); 8149 } 8150 8151 static void 8152 dtrace_actdesc_hold(dtrace_actdesc_t *act) 8153 { 8154 ASSERT(act->dtad_refcnt >= 1); 8155 act->dtad_refcnt++; 8156 } 8157 8158 static void 8159 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 8160 { 8161 dtrace_actkind_t kind = act->dtad_kind; 8162 dtrace_difo_t *dp; 8163 8164 ASSERT(act->dtad_refcnt >= 1); 8165 8166 if (--act->dtad_refcnt != 0) 8167 return; 8168 8169 if ((dp = act->dtad_difo) != NULL) 8170 dtrace_difo_release(dp, vstate); 8171 8172 if (DTRACEACT_ISPRINTFLIKE(kind)) { 8173 char *str = (char *)(uintptr_t)act->dtad_arg; 8174 8175 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 8176 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 8177 8178 if (str != NULL) 8179 kmem_free(str, strlen(str) + 1); 8180 } 8181 8182 kmem_free(act, sizeof (dtrace_actdesc_t)); 8183 } 8184 8185 /* 8186 * DTrace ECB Functions 8187 */ 8188 static dtrace_ecb_t * 8189 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 8190 { 8191 dtrace_ecb_t *ecb; 8192 dtrace_epid_t epid; 8193 8194 ASSERT(MUTEX_HELD(&dtrace_lock)); 8195 8196 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 8197 ecb->dte_predicate = NULL; 8198 ecb->dte_probe = probe; 8199 8200 /* 8201 * The default size is the size of the default action: recording 8202 * the epid. 8203 */ 8204 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 8205 ecb->dte_alignment = sizeof (dtrace_epid_t); 8206 8207 epid = state->dts_epid++; 8208 8209 if (epid - 1 >= state->dts_necbs) { 8210 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 8211 int necbs = state->dts_necbs << 1; 8212 8213 ASSERT(epid == state->dts_necbs + 1); 8214 8215 if (necbs == 0) { 8216 ASSERT(oecbs == NULL); 8217 necbs = 1; 8218 } 8219 8220 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 8221 8222 if (oecbs != NULL) 8223 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 8224 8225 dtrace_membar_producer(); 8226 state->dts_ecbs = ecbs; 8227 8228 if (oecbs != NULL) { 8229 /* 8230 * If this state is active, we must dtrace_sync() 8231 * before we can free the old dts_ecbs array: we're 8232 * coming in hot, and there may be active ring 8233 * buffer processing (which indexes into the dts_ecbs 8234 * array) on another CPU. 8235 */ 8236 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 8237 dtrace_sync(); 8238 8239 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 8240 } 8241 8242 dtrace_membar_producer(); 8243 state->dts_necbs = necbs; 8244 } 8245 8246 ecb->dte_state = state; 8247 8248 ASSERT(state->dts_ecbs[epid - 1] == NULL); 8249 dtrace_membar_producer(); 8250 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 8251 8252 return (ecb); 8253 } 8254 8255 static void 8256 dtrace_ecb_enable(dtrace_ecb_t *ecb) 8257 { 8258 dtrace_probe_t *probe = ecb->dte_probe; 8259 8260 ASSERT(MUTEX_HELD(&cpu_lock)); 8261 ASSERT(MUTEX_HELD(&dtrace_lock)); 8262 ASSERT(ecb->dte_next == NULL); 8263 8264 if (probe == NULL) { 8265 /* 8266 * This is the NULL probe -- there's nothing to do. 8267 */ 8268 return; 8269 } 8270 8271 if (probe->dtpr_ecb == NULL) { 8272 dtrace_provider_t *prov = probe->dtpr_provider; 8273 8274 /* 8275 * We're the first ECB on this probe. 8276 */ 8277 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 8278 8279 if (ecb->dte_predicate != NULL) 8280 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 8281 8282 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 8283 probe->dtpr_id, probe->dtpr_arg); 8284 } else { 8285 /* 8286 * This probe is already active. Swing the last pointer to 8287 * point to the new ECB, and issue a dtrace_sync() to assure 8288 * that all CPUs have seen the change. 8289 */ 8290 ASSERT(probe->dtpr_ecb_last != NULL); 8291 probe->dtpr_ecb_last->dte_next = ecb; 8292 probe->dtpr_ecb_last = ecb; 8293 probe->dtpr_predcache = 0; 8294 8295 dtrace_sync(); 8296 } 8297 } 8298 8299 static void 8300 dtrace_ecb_resize(dtrace_ecb_t *ecb) 8301 { 8302 uint32_t maxalign = sizeof (dtrace_epid_t); 8303 uint32_t align = sizeof (uint8_t), offs, diff; 8304 dtrace_action_t *act; 8305 int wastuple = 0; 8306 uint32_t aggbase = UINT32_MAX; 8307 dtrace_state_t *state = ecb->dte_state; 8308 8309 /* 8310 * If we record anything, we always record the epid. (And we always 8311 * record it first.) 8312 */ 8313 offs = sizeof (dtrace_epid_t); 8314 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 8315 8316 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 8317 dtrace_recdesc_t *rec = &act->dta_rec; 8318 8319 if ((align = rec->dtrd_alignment) > maxalign) 8320 maxalign = align; 8321 8322 if (!wastuple && act->dta_intuple) { 8323 /* 8324 * This is the first record in a tuple. Align the 8325 * offset to be at offset 4 in an 8-byte aligned 8326 * block. 8327 */ 8328 diff = offs + sizeof (dtrace_aggid_t); 8329 8330 if (diff = (diff & (sizeof (uint64_t) - 1))) 8331 offs += sizeof (uint64_t) - diff; 8332 8333 aggbase = offs - sizeof (dtrace_aggid_t); 8334 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 8335 } 8336 8337 /*LINTED*/ 8338 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 8339 /* 8340 * The current offset is not properly aligned; align it. 8341 */ 8342 offs += align - diff; 8343 } 8344 8345 rec->dtrd_offset = offs; 8346 8347 if (offs + rec->dtrd_size > ecb->dte_needed) { 8348 ecb->dte_needed = offs + rec->dtrd_size; 8349 8350 if (ecb->dte_needed > state->dts_needed) 8351 state->dts_needed = ecb->dte_needed; 8352 } 8353 8354 if (DTRACEACT_ISAGG(act->dta_kind)) { 8355 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 8356 dtrace_action_t *first = agg->dtag_first, *prev; 8357 8358 ASSERT(rec->dtrd_size != 0 && first != NULL); 8359 ASSERT(wastuple); 8360 ASSERT(aggbase != UINT32_MAX); 8361 8362 agg->dtag_base = aggbase; 8363 8364 while ((prev = first->dta_prev) != NULL && 8365 DTRACEACT_ISAGG(prev->dta_kind)) { 8366 agg = (dtrace_aggregation_t *)prev; 8367 first = agg->dtag_first; 8368 } 8369 8370 if (prev != NULL) { 8371 offs = prev->dta_rec.dtrd_offset + 8372 prev->dta_rec.dtrd_size; 8373 } else { 8374 offs = sizeof (dtrace_epid_t); 8375 } 8376 wastuple = 0; 8377 } else { 8378 if (!act->dta_intuple) 8379 ecb->dte_size = offs + rec->dtrd_size; 8380 8381 offs += rec->dtrd_size; 8382 } 8383 8384 wastuple = act->dta_intuple; 8385 } 8386 8387 if ((act = ecb->dte_action) != NULL && 8388 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 8389 ecb->dte_size == sizeof (dtrace_epid_t)) { 8390 /* 8391 * If the size is still sizeof (dtrace_epid_t), then all 8392 * actions store no data; set the size to 0. 8393 */ 8394 ecb->dte_alignment = maxalign; 8395 ecb->dte_size = 0; 8396 8397 /* 8398 * If the needed space is still sizeof (dtrace_epid_t), then 8399 * all actions need no additional space; set the needed 8400 * size to 0. 8401 */ 8402 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 8403 ecb->dte_needed = 0; 8404 8405 return; 8406 } 8407 8408 /* 8409 * Set our alignment, and make sure that the dte_size and dte_needed 8410 * are aligned to the size of an EPID. 8411 */ 8412 ecb->dte_alignment = maxalign; 8413 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 8414 ~(sizeof (dtrace_epid_t) - 1); 8415 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 8416 ~(sizeof (dtrace_epid_t) - 1); 8417 ASSERT(ecb->dte_size <= ecb->dte_needed); 8418 } 8419 8420 static dtrace_action_t * 8421 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 8422 { 8423 dtrace_aggregation_t *agg; 8424 size_t size = sizeof (uint64_t); 8425 int ntuple = desc->dtad_ntuple; 8426 dtrace_action_t *act; 8427 dtrace_recdesc_t *frec; 8428 dtrace_aggid_t aggid; 8429 dtrace_state_t *state = ecb->dte_state; 8430 8431 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 8432 agg->dtag_ecb = ecb; 8433 8434 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 8435 8436 switch (desc->dtad_kind) { 8437 case DTRACEAGG_MIN: 8438 agg->dtag_initial = UINT64_MAX; 8439 agg->dtag_aggregate = dtrace_aggregate_min; 8440 break; 8441 8442 case DTRACEAGG_MAX: 8443 agg->dtag_aggregate = dtrace_aggregate_max; 8444 break; 8445 8446 case DTRACEAGG_COUNT: 8447 agg->dtag_aggregate = dtrace_aggregate_count; 8448 break; 8449 8450 case DTRACEAGG_QUANTIZE: 8451 agg->dtag_aggregate = dtrace_aggregate_quantize; 8452 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 8453 sizeof (uint64_t); 8454 break; 8455 8456 case DTRACEAGG_LQUANTIZE: { 8457 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 8458 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 8459 8460 agg->dtag_initial = desc->dtad_arg; 8461 agg->dtag_aggregate = dtrace_aggregate_lquantize; 8462 8463 if (step == 0 || levels == 0) 8464 goto err; 8465 8466 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 8467 break; 8468 } 8469 8470 case DTRACEAGG_AVG: 8471 agg->dtag_aggregate = dtrace_aggregate_avg; 8472 size = sizeof (uint64_t) * 2; 8473 break; 8474 8475 case DTRACEAGG_SUM: 8476 agg->dtag_aggregate = dtrace_aggregate_sum; 8477 break; 8478 8479 default: 8480 goto err; 8481 } 8482 8483 agg->dtag_action.dta_rec.dtrd_size = size; 8484 8485 if (ntuple == 0) 8486 goto err; 8487 8488 /* 8489 * We must make sure that we have enough actions for the n-tuple. 8490 */ 8491 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 8492 if (DTRACEACT_ISAGG(act->dta_kind)) 8493 break; 8494 8495 if (--ntuple == 0) { 8496 /* 8497 * This is the action with which our n-tuple begins. 8498 */ 8499 agg->dtag_first = act; 8500 goto success; 8501 } 8502 } 8503 8504 /* 8505 * This n-tuple is short by ntuple elements. Return failure. 8506 */ 8507 ASSERT(ntuple != 0); 8508 err: 8509 kmem_free(agg, sizeof (dtrace_aggregation_t)); 8510 return (NULL); 8511 8512 success: 8513 /* 8514 * If the last action in the tuple has a size of zero, it's actually 8515 * an expression argument for the aggregating action. 8516 */ 8517 ASSERT(ecb->dte_action_last != NULL); 8518 act = ecb->dte_action_last; 8519 8520 if (act->dta_kind == DTRACEACT_DIFEXPR) { 8521 ASSERT(act->dta_difo != NULL); 8522 8523 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 8524 agg->dtag_hasarg = 1; 8525 } 8526 8527 /* 8528 * We need to allocate an id for this aggregation. 8529 */ 8530 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 8531 VM_BESTFIT | VM_SLEEP); 8532 8533 if (aggid - 1 >= state->dts_naggregations) { 8534 dtrace_aggregation_t **oaggs = state->dts_aggregations; 8535 dtrace_aggregation_t **aggs; 8536 int naggs = state->dts_naggregations << 1; 8537 int onaggs = state->dts_naggregations; 8538 8539 ASSERT(aggid == state->dts_naggregations + 1); 8540 8541 if (naggs == 0) { 8542 ASSERT(oaggs == NULL); 8543 naggs = 1; 8544 } 8545 8546 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 8547 8548 if (oaggs != NULL) { 8549 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 8550 kmem_free(oaggs, onaggs * sizeof (*aggs)); 8551 } 8552 8553 state->dts_aggregations = aggs; 8554 state->dts_naggregations = naggs; 8555 } 8556 8557 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 8558 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 8559 8560 frec = &agg->dtag_first->dta_rec; 8561 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 8562 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 8563 8564 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 8565 ASSERT(!act->dta_intuple); 8566 act->dta_intuple = 1; 8567 } 8568 8569 return (&agg->dtag_action); 8570 } 8571 8572 static void 8573 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 8574 { 8575 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 8576 dtrace_state_t *state = ecb->dte_state; 8577 dtrace_aggid_t aggid = agg->dtag_id; 8578 8579 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 8580 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 8581 8582 ASSERT(state->dts_aggregations[aggid - 1] == agg); 8583 state->dts_aggregations[aggid - 1] = NULL; 8584 8585 kmem_free(agg, sizeof (dtrace_aggregation_t)); 8586 } 8587 8588 static int 8589 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 8590 { 8591 dtrace_action_t *action, *last; 8592 dtrace_difo_t *dp = desc->dtad_difo; 8593 uint32_t size = 0, align = sizeof (uint8_t), mask; 8594 uint16_t format = 0; 8595 dtrace_recdesc_t *rec; 8596 dtrace_state_t *state = ecb->dte_state; 8597 dtrace_optval_t *opt = state->dts_options, nframes, strsize; 8598 uint64_t arg = desc->dtad_arg; 8599 8600 ASSERT(MUTEX_HELD(&dtrace_lock)); 8601 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 8602 8603 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 8604 /* 8605 * If this is an aggregating action, there must be neither 8606 * a speculate nor a commit on the action chain. 8607 */ 8608 dtrace_action_t *act; 8609 8610 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 8611 if (act->dta_kind == DTRACEACT_COMMIT) 8612 return (EINVAL); 8613 8614 if (act->dta_kind == DTRACEACT_SPECULATE) 8615 return (EINVAL); 8616 } 8617 8618 action = dtrace_ecb_aggregation_create(ecb, desc); 8619 8620 if (action == NULL) 8621 return (EINVAL); 8622 } else { 8623 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 8624 (desc->dtad_kind == DTRACEACT_DIFEXPR && 8625 dp != NULL && dp->dtdo_destructive)) { 8626 state->dts_destructive = 1; 8627 } 8628 8629 switch (desc->dtad_kind) { 8630 case DTRACEACT_PRINTF: 8631 case DTRACEACT_PRINTA: 8632 case DTRACEACT_SYSTEM: 8633 case DTRACEACT_FREOPEN: 8634 /* 8635 * We know that our arg is a string -- turn it into a 8636 * format. 8637 */ 8638 if (arg == NULL) { 8639 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); 8640 format = 0; 8641 } else { 8642 ASSERT(arg != NULL); 8643 ASSERT(arg > KERNELBASE); 8644 format = dtrace_format_add(state, 8645 (char *)(uintptr_t)arg); 8646 } 8647 8648 /*FALLTHROUGH*/ 8649 case DTRACEACT_LIBACT: 8650 case DTRACEACT_DIFEXPR: 8651 if (dp == NULL) 8652 return (EINVAL); 8653 8654 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 8655 break; 8656 8657 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 8658 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8659 return (EINVAL); 8660 8661 size = opt[DTRACEOPT_STRSIZE]; 8662 } 8663 8664 break; 8665 8666 case DTRACEACT_STACK: 8667 if ((nframes = arg) == 0) { 8668 nframes = opt[DTRACEOPT_STACKFRAMES]; 8669 ASSERT(nframes > 0); 8670 arg = nframes; 8671 } 8672 8673 size = nframes * sizeof (pc_t); 8674 break; 8675 8676 case DTRACEACT_JSTACK: 8677 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 8678 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 8679 8680 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 8681 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 8682 8683 arg = DTRACE_USTACK_ARG(nframes, strsize); 8684 8685 /*FALLTHROUGH*/ 8686 case DTRACEACT_USTACK: 8687 if (desc->dtad_kind != DTRACEACT_JSTACK && 8688 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 8689 strsize = DTRACE_USTACK_STRSIZE(arg); 8690 nframes = opt[DTRACEOPT_USTACKFRAMES]; 8691 ASSERT(nframes > 0); 8692 arg = DTRACE_USTACK_ARG(nframes, strsize); 8693 } 8694 8695 /* 8696 * Save a slot for the pid. 8697 */ 8698 size = (nframes + 1) * sizeof (uint64_t); 8699 size += DTRACE_USTACK_STRSIZE(arg); 8700 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 8701 8702 break; 8703 8704 case DTRACEACT_SYM: 8705 case DTRACEACT_MOD: 8706 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 8707 sizeof (uint64_t)) || 8708 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8709 return (EINVAL); 8710 break; 8711 8712 case DTRACEACT_USYM: 8713 case DTRACEACT_UMOD: 8714 case DTRACEACT_UADDR: 8715 if (dp == NULL || 8716 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 8717 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8718 return (EINVAL); 8719 8720 /* 8721 * We have a slot for the pid, plus a slot for the 8722 * argument. To keep things simple (aligned with 8723 * bitness-neutral sizing), we store each as a 64-bit 8724 * quantity. 8725 */ 8726 size = 2 * sizeof (uint64_t); 8727 break; 8728 8729 case DTRACEACT_STOP: 8730 case DTRACEACT_BREAKPOINT: 8731 case DTRACEACT_PANIC: 8732 break; 8733 8734 case DTRACEACT_CHILL: 8735 case DTRACEACT_DISCARD: 8736 case DTRACEACT_RAISE: 8737 if (dp == NULL) 8738 return (EINVAL); 8739 break; 8740 8741 case DTRACEACT_EXIT: 8742 if (dp == NULL || 8743 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 8744 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 8745 return (EINVAL); 8746 break; 8747 8748 case DTRACEACT_SPECULATE: 8749 if (ecb->dte_size > sizeof (dtrace_epid_t)) 8750 return (EINVAL); 8751 8752 if (dp == NULL) 8753 return (EINVAL); 8754 8755 state->dts_speculates = 1; 8756 break; 8757 8758 case DTRACEACT_COMMIT: { 8759 dtrace_action_t *act = ecb->dte_action; 8760 8761 for (; act != NULL; act = act->dta_next) { 8762 if (act->dta_kind == DTRACEACT_COMMIT) 8763 return (EINVAL); 8764 } 8765 8766 if (dp == NULL) 8767 return (EINVAL); 8768 break; 8769 } 8770 8771 default: 8772 return (EINVAL); 8773 } 8774 8775 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 8776 /* 8777 * If this is a data-storing action or a speculate, 8778 * we must be sure that there isn't a commit on the 8779 * action chain. 8780 */ 8781 dtrace_action_t *act = ecb->dte_action; 8782 8783 for (; act != NULL; act = act->dta_next) { 8784 if (act->dta_kind == DTRACEACT_COMMIT) 8785 return (EINVAL); 8786 } 8787 } 8788 8789 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 8790 action->dta_rec.dtrd_size = size; 8791 } 8792 8793 action->dta_refcnt = 1; 8794 rec = &action->dta_rec; 8795 size = rec->dtrd_size; 8796 8797 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 8798 if (!(size & mask)) { 8799 align = mask + 1; 8800 break; 8801 } 8802 } 8803 8804 action->dta_kind = desc->dtad_kind; 8805 8806 if ((action->dta_difo = dp) != NULL) 8807 dtrace_difo_hold(dp); 8808 8809 rec->dtrd_action = action->dta_kind; 8810 rec->dtrd_arg = arg; 8811 rec->dtrd_uarg = desc->dtad_uarg; 8812 rec->dtrd_alignment = (uint16_t)align; 8813 rec->dtrd_format = format; 8814 8815 if ((last = ecb->dte_action_last) != NULL) { 8816 ASSERT(ecb->dte_action != NULL); 8817 action->dta_prev = last; 8818 last->dta_next = action; 8819 } else { 8820 ASSERT(ecb->dte_action == NULL); 8821 ecb->dte_action = action; 8822 } 8823 8824 ecb->dte_action_last = action; 8825 8826 return (0); 8827 } 8828 8829 static void 8830 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 8831 { 8832 dtrace_action_t *act = ecb->dte_action, *next; 8833 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 8834 dtrace_difo_t *dp; 8835 uint16_t format; 8836 8837 if (act != NULL && act->dta_refcnt > 1) { 8838 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 8839 act->dta_refcnt--; 8840 } else { 8841 for (; act != NULL; act = next) { 8842 next = act->dta_next; 8843 ASSERT(next != NULL || act == ecb->dte_action_last); 8844 ASSERT(act->dta_refcnt == 1); 8845 8846 if ((format = act->dta_rec.dtrd_format) != 0) 8847 dtrace_format_remove(ecb->dte_state, format); 8848 8849 if ((dp = act->dta_difo) != NULL) 8850 dtrace_difo_release(dp, vstate); 8851 8852 if (DTRACEACT_ISAGG(act->dta_kind)) { 8853 dtrace_ecb_aggregation_destroy(ecb, act); 8854 } else { 8855 kmem_free(act, sizeof (dtrace_action_t)); 8856 } 8857 } 8858 } 8859 8860 ecb->dte_action = NULL; 8861 ecb->dte_action_last = NULL; 8862 ecb->dte_size = sizeof (dtrace_epid_t); 8863 } 8864 8865 static void 8866 dtrace_ecb_disable(dtrace_ecb_t *ecb) 8867 { 8868 /* 8869 * We disable the ECB by removing it from its probe. 8870 */ 8871 dtrace_ecb_t *pecb, *prev = NULL; 8872 dtrace_probe_t *probe = ecb->dte_probe; 8873 8874 ASSERT(MUTEX_HELD(&dtrace_lock)); 8875 8876 if (probe == NULL) { 8877 /* 8878 * This is the NULL probe; there is nothing to disable. 8879 */ 8880 return; 8881 } 8882 8883 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 8884 if (pecb == ecb) 8885 break; 8886 prev = pecb; 8887 } 8888 8889 ASSERT(pecb != NULL); 8890 8891 if (prev == NULL) { 8892 probe->dtpr_ecb = ecb->dte_next; 8893 } else { 8894 prev->dte_next = ecb->dte_next; 8895 } 8896 8897 if (ecb == probe->dtpr_ecb_last) { 8898 ASSERT(ecb->dte_next == NULL); 8899 probe->dtpr_ecb_last = prev; 8900 } 8901 8902 /* 8903 * The ECB has been disconnected from the probe; now sync to assure 8904 * that all CPUs have seen the change before returning. 8905 */ 8906 dtrace_sync(); 8907 8908 if (probe->dtpr_ecb == NULL) { 8909 /* 8910 * That was the last ECB on the probe; clear the predicate 8911 * cache ID for the probe, disable it and sync one more time 8912 * to assure that we'll never hit it again. 8913 */ 8914 dtrace_provider_t *prov = probe->dtpr_provider; 8915 8916 ASSERT(ecb->dte_next == NULL); 8917 ASSERT(probe->dtpr_ecb_last == NULL); 8918 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 8919 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 8920 probe->dtpr_id, probe->dtpr_arg); 8921 dtrace_sync(); 8922 } else { 8923 /* 8924 * There is at least one ECB remaining on the probe. If there 8925 * is _exactly_ one, set the probe's predicate cache ID to be 8926 * the predicate cache ID of the remaining ECB. 8927 */ 8928 ASSERT(probe->dtpr_ecb_last != NULL); 8929 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 8930 8931 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 8932 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 8933 8934 ASSERT(probe->dtpr_ecb->dte_next == NULL); 8935 8936 if (p != NULL) 8937 probe->dtpr_predcache = p->dtp_cacheid; 8938 } 8939 8940 ecb->dte_next = NULL; 8941 } 8942 } 8943 8944 static void 8945 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 8946 { 8947 dtrace_state_t *state = ecb->dte_state; 8948 dtrace_vstate_t *vstate = &state->dts_vstate; 8949 dtrace_predicate_t *pred; 8950 dtrace_epid_t epid = ecb->dte_epid; 8951 8952 ASSERT(MUTEX_HELD(&dtrace_lock)); 8953 ASSERT(ecb->dte_next == NULL); 8954 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 8955 8956 if ((pred = ecb->dte_predicate) != NULL) 8957 dtrace_predicate_release(pred, vstate); 8958 8959 dtrace_ecb_action_remove(ecb); 8960 8961 ASSERT(state->dts_ecbs[epid - 1] == ecb); 8962 state->dts_ecbs[epid - 1] = NULL; 8963 8964 kmem_free(ecb, sizeof (dtrace_ecb_t)); 8965 } 8966 8967 static dtrace_ecb_t * 8968 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 8969 dtrace_enabling_t *enab) 8970 { 8971 dtrace_ecb_t *ecb; 8972 dtrace_predicate_t *pred; 8973 dtrace_actdesc_t *act; 8974 dtrace_provider_t *prov; 8975 dtrace_ecbdesc_t *desc = enab->dten_current; 8976 8977 ASSERT(MUTEX_HELD(&dtrace_lock)); 8978 ASSERT(state != NULL); 8979 8980 ecb = dtrace_ecb_add(state, probe); 8981 ecb->dte_uarg = desc->dted_uarg; 8982 8983 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 8984 dtrace_predicate_hold(pred); 8985 ecb->dte_predicate = pred; 8986 } 8987 8988 if (probe != NULL) { 8989 /* 8990 * If the provider shows more leg than the consumer is old 8991 * enough to see, we need to enable the appropriate implicit 8992 * predicate bits to prevent the ecb from activating at 8993 * revealing times. 8994 * 8995 * Providers specifying DTRACE_PRIV_USER at register time 8996 * are stating that they need the /proc-style privilege 8997 * model to be enforced, and this is what DTRACE_COND_OWNER 8998 * and DTRACE_COND_ZONEOWNER will then do at probe time. 8999 */ 9000 prov = probe->dtpr_provider; 9001 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 9002 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 9003 ecb->dte_cond |= DTRACE_COND_OWNER; 9004 9005 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 9006 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 9007 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 9008 9009 /* 9010 * If the provider shows us kernel innards and the user 9011 * is lacking sufficient privilege, enable the 9012 * DTRACE_COND_USERMODE implicit predicate. 9013 */ 9014 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 9015 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 9016 ecb->dte_cond |= DTRACE_COND_USERMODE; 9017 } 9018 9019 if (dtrace_ecb_create_cache != NULL) { 9020 /* 9021 * If we have a cached ecb, we'll use its action list instead 9022 * of creating our own (saving both time and space). 9023 */ 9024 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 9025 dtrace_action_t *act = cached->dte_action; 9026 9027 if (act != NULL) { 9028 ASSERT(act->dta_refcnt > 0); 9029 act->dta_refcnt++; 9030 ecb->dte_action = act; 9031 ecb->dte_action_last = cached->dte_action_last; 9032 ecb->dte_needed = cached->dte_needed; 9033 ecb->dte_size = cached->dte_size; 9034 ecb->dte_alignment = cached->dte_alignment; 9035 } 9036 9037 return (ecb); 9038 } 9039 9040 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 9041 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 9042 dtrace_ecb_destroy(ecb); 9043 return (NULL); 9044 } 9045 } 9046 9047 dtrace_ecb_resize(ecb); 9048 9049 return (dtrace_ecb_create_cache = ecb); 9050 } 9051 9052 static int 9053 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 9054 { 9055 dtrace_ecb_t *ecb; 9056 dtrace_enabling_t *enab = arg; 9057 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 9058 9059 ASSERT(state != NULL); 9060 9061 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 9062 /* 9063 * This probe was created in a generation for which this 9064 * enabling has previously created ECBs; we don't want to 9065 * enable it again, so just kick out. 9066 */ 9067 return (DTRACE_MATCH_NEXT); 9068 } 9069 9070 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 9071 return (DTRACE_MATCH_DONE); 9072 9073 dtrace_ecb_enable(ecb); 9074 return (DTRACE_MATCH_NEXT); 9075 } 9076 9077 static dtrace_ecb_t * 9078 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 9079 { 9080 dtrace_ecb_t *ecb; 9081 9082 ASSERT(MUTEX_HELD(&dtrace_lock)); 9083 9084 if (id == 0 || id > state->dts_necbs) 9085 return (NULL); 9086 9087 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 9088 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 9089 9090 return (state->dts_ecbs[id - 1]); 9091 } 9092 9093 static dtrace_aggregation_t * 9094 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 9095 { 9096 dtrace_aggregation_t *agg; 9097 9098 ASSERT(MUTEX_HELD(&dtrace_lock)); 9099 9100 if (id == 0 || id > state->dts_naggregations) 9101 return (NULL); 9102 9103 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 9104 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 9105 agg->dtag_id == id); 9106 9107 return (state->dts_aggregations[id - 1]); 9108 } 9109 9110 /* 9111 * DTrace Buffer Functions 9112 * 9113 * The following functions manipulate DTrace buffers. Most of these functions 9114 * are called in the context of establishing or processing consumer state; 9115 * exceptions are explicitly noted. 9116 */ 9117 9118 /* 9119 * Note: called from cross call context. This function switches the two 9120 * buffers on a given CPU. The atomicity of this operation is assured by 9121 * disabling interrupts while the actual switch takes place; the disabling of 9122 * interrupts serializes the execution with any execution of dtrace_probe() on 9123 * the same CPU. 9124 */ 9125 static void 9126 dtrace_buffer_switch(dtrace_buffer_t *buf) 9127 { 9128 caddr_t tomax = buf->dtb_tomax; 9129 caddr_t xamot = buf->dtb_xamot; 9130 dtrace_icookie_t cookie; 9131 9132 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 9133 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 9134 9135 cookie = dtrace_interrupt_disable(); 9136 buf->dtb_tomax = xamot; 9137 buf->dtb_xamot = tomax; 9138 buf->dtb_xamot_drops = buf->dtb_drops; 9139 buf->dtb_xamot_offset = buf->dtb_offset; 9140 buf->dtb_xamot_errors = buf->dtb_errors; 9141 buf->dtb_xamot_flags = buf->dtb_flags; 9142 buf->dtb_offset = 0; 9143 buf->dtb_drops = 0; 9144 buf->dtb_errors = 0; 9145 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 9146 dtrace_interrupt_enable(cookie); 9147 } 9148 9149 /* 9150 * Note: called from cross call context. This function activates a buffer 9151 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 9152 * is guaranteed by the disabling of interrupts. 9153 */ 9154 static void 9155 dtrace_buffer_activate(dtrace_state_t *state) 9156 { 9157 dtrace_buffer_t *buf; 9158 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 9159 9160 buf = &state->dts_buffer[CPU->cpu_id]; 9161 9162 if (buf->dtb_tomax != NULL) { 9163 /* 9164 * We might like to assert that the buffer is marked inactive, 9165 * but this isn't necessarily true: the buffer for the CPU 9166 * that processes the BEGIN probe has its buffer activated 9167 * manually. In this case, we take the (harmless) action 9168 * re-clearing the bit INACTIVE bit. 9169 */ 9170 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 9171 } 9172 9173 dtrace_interrupt_enable(cookie); 9174 } 9175 9176 static int 9177 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 9178 processorid_t cpu) 9179 { 9180 cpu_t *cp; 9181 dtrace_buffer_t *buf; 9182 9183 ASSERT(MUTEX_HELD(&cpu_lock)); 9184 ASSERT(MUTEX_HELD(&dtrace_lock)); 9185 9186 if (size > dtrace_nonroot_maxsize && 9187 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 9188 return (EFBIG); 9189 9190 cp = cpu_list; 9191 9192 do { 9193 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 9194 continue; 9195 9196 buf = &bufs[cp->cpu_id]; 9197 9198 /* 9199 * If there is already a buffer allocated for this CPU, it 9200 * is only possible that this is a DR event. In this case, 9201 * the buffer size must match our specified size. 9202 */ 9203 if (buf->dtb_tomax != NULL) { 9204 ASSERT(buf->dtb_size == size); 9205 continue; 9206 } 9207 9208 ASSERT(buf->dtb_xamot == NULL); 9209 9210 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 9211 goto err; 9212 9213 buf->dtb_size = size; 9214 buf->dtb_flags = flags; 9215 buf->dtb_offset = 0; 9216 buf->dtb_drops = 0; 9217 9218 if (flags & DTRACEBUF_NOSWITCH) 9219 continue; 9220 9221 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 9222 goto err; 9223 } while ((cp = cp->cpu_next) != cpu_list); 9224 9225 return (0); 9226 9227 err: 9228 cp = cpu_list; 9229 9230 do { 9231 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 9232 continue; 9233 9234 buf = &bufs[cp->cpu_id]; 9235 9236 if (buf->dtb_xamot != NULL) { 9237 ASSERT(buf->dtb_tomax != NULL); 9238 ASSERT(buf->dtb_size == size); 9239 kmem_free(buf->dtb_xamot, size); 9240 } 9241 9242 if (buf->dtb_tomax != NULL) { 9243 ASSERT(buf->dtb_size == size); 9244 kmem_free(buf->dtb_tomax, size); 9245 } 9246 9247 buf->dtb_tomax = NULL; 9248 buf->dtb_xamot = NULL; 9249 buf->dtb_size = 0; 9250 } while ((cp = cp->cpu_next) != cpu_list); 9251 9252 return (ENOMEM); 9253 } 9254 9255 /* 9256 * Note: called from probe context. This function just increments the drop 9257 * count on a buffer. It has been made a function to allow for the 9258 * possibility of understanding the source of mysterious drop counts. (A 9259 * problem for which one may be particularly disappointed that DTrace cannot 9260 * be used to understand DTrace.) 9261 */ 9262 static void 9263 dtrace_buffer_drop(dtrace_buffer_t *buf) 9264 { 9265 buf->dtb_drops++; 9266 } 9267 9268 /* 9269 * Note: called from probe context. This function is called to reserve space 9270 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 9271 * mstate. Returns the new offset in the buffer, or a negative value if an 9272 * error has occurred. 9273 */ 9274 static intptr_t 9275 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 9276 dtrace_state_t *state, dtrace_mstate_t *mstate) 9277 { 9278 intptr_t offs = buf->dtb_offset, soffs; 9279 intptr_t woffs; 9280 caddr_t tomax; 9281 size_t total; 9282 9283 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 9284 return (-1); 9285 9286 if ((tomax = buf->dtb_tomax) == NULL) { 9287 dtrace_buffer_drop(buf); 9288 return (-1); 9289 } 9290 9291 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 9292 while (offs & (align - 1)) { 9293 /* 9294 * Assert that our alignment is off by a number which 9295 * is itself sizeof (uint32_t) aligned. 9296 */ 9297 ASSERT(!((align - (offs & (align - 1))) & 9298 (sizeof (uint32_t) - 1))); 9299 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 9300 offs += sizeof (uint32_t); 9301 } 9302 9303 if ((soffs = offs + needed) > buf->dtb_size) { 9304 dtrace_buffer_drop(buf); 9305 return (-1); 9306 } 9307 9308 if (mstate == NULL) 9309 return (offs); 9310 9311 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 9312 mstate->dtms_scratch_size = buf->dtb_size - soffs; 9313 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 9314 9315 return (offs); 9316 } 9317 9318 if (buf->dtb_flags & DTRACEBUF_FILL) { 9319 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 9320 (buf->dtb_flags & DTRACEBUF_FULL)) 9321 return (-1); 9322 goto out; 9323 } 9324 9325 total = needed + (offs & (align - 1)); 9326 9327 /* 9328 * For a ring buffer, life is quite a bit more complicated. Before 9329 * we can store any padding, we need to adjust our wrapping offset. 9330 * (If we've never before wrapped or we're not about to, no adjustment 9331 * is required.) 9332 */ 9333 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 9334 offs + total > buf->dtb_size) { 9335 woffs = buf->dtb_xamot_offset; 9336 9337 if (offs + total > buf->dtb_size) { 9338 /* 9339 * We can't fit in the end of the buffer. First, a 9340 * sanity check that we can fit in the buffer at all. 9341 */ 9342 if (total > buf->dtb_size) { 9343 dtrace_buffer_drop(buf); 9344 return (-1); 9345 } 9346 9347 /* 9348 * We're going to be storing at the top of the buffer, 9349 * so now we need to deal with the wrapped offset. We 9350 * only reset our wrapped offset to 0 if it is 9351 * currently greater than the current offset. If it 9352 * is less than the current offset, it is because a 9353 * previous allocation induced a wrap -- but the 9354 * allocation didn't subsequently take the space due 9355 * to an error or false predicate evaluation. In this 9356 * case, we'll just leave the wrapped offset alone: if 9357 * the wrapped offset hasn't been advanced far enough 9358 * for this allocation, it will be adjusted in the 9359 * lower loop. 9360 */ 9361 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 9362 if (woffs >= offs) 9363 woffs = 0; 9364 } else { 9365 woffs = 0; 9366 } 9367 9368 /* 9369 * Now we know that we're going to be storing to the 9370 * top of the buffer and that there is room for us 9371 * there. We need to clear the buffer from the current 9372 * offset to the end (there may be old gunk there). 9373 */ 9374 while (offs < buf->dtb_size) 9375 tomax[offs++] = 0; 9376 9377 /* 9378 * We need to set our offset to zero. And because we 9379 * are wrapping, we need to set the bit indicating as 9380 * much. We can also adjust our needed space back 9381 * down to the space required by the ECB -- we know 9382 * that the top of the buffer is aligned. 9383 */ 9384 offs = 0; 9385 total = needed; 9386 buf->dtb_flags |= DTRACEBUF_WRAPPED; 9387 } else { 9388 /* 9389 * There is room for us in the buffer, so we simply 9390 * need to check the wrapped offset. 9391 */ 9392 if (woffs < offs) { 9393 /* 9394 * The wrapped offset is less than the offset. 9395 * This can happen if we allocated buffer space 9396 * that induced a wrap, but then we didn't 9397 * subsequently take the space due to an error 9398 * or false predicate evaluation. This is 9399 * okay; we know that _this_ allocation isn't 9400 * going to induce a wrap. We still can't 9401 * reset the wrapped offset to be zero, 9402 * however: the space may have been trashed in 9403 * the previous failed probe attempt. But at 9404 * least the wrapped offset doesn't need to 9405 * be adjusted at all... 9406 */ 9407 goto out; 9408 } 9409 } 9410 9411 while (offs + total > woffs) { 9412 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 9413 size_t size; 9414 9415 if (epid == DTRACE_EPIDNONE) { 9416 size = sizeof (uint32_t); 9417 } else { 9418 ASSERT(epid <= state->dts_necbs); 9419 ASSERT(state->dts_ecbs[epid - 1] != NULL); 9420 9421 size = state->dts_ecbs[epid - 1]->dte_size; 9422 } 9423 9424 ASSERT(woffs + size <= buf->dtb_size); 9425 ASSERT(size != 0); 9426 9427 if (woffs + size == buf->dtb_size) { 9428 /* 9429 * We've reached the end of the buffer; we want 9430 * to set the wrapped offset to 0 and break 9431 * out. However, if the offs is 0, then we're 9432 * in a strange edge-condition: the amount of 9433 * space that we want to reserve plus the size 9434 * of the record that we're overwriting is 9435 * greater than the size of the buffer. This 9436 * is problematic because if we reserve the 9437 * space but subsequently don't consume it (due 9438 * to a failed predicate or error) the wrapped 9439 * offset will be 0 -- yet the EPID at offset 0 9440 * will not be committed. This situation is 9441 * relatively easy to deal with: if we're in 9442 * this case, the buffer is indistinguishable 9443 * from one that hasn't wrapped; we need only 9444 * finish the job by clearing the wrapped bit, 9445 * explicitly setting the offset to be 0, and 9446 * zero'ing out the old data in the buffer. 9447 */ 9448 if (offs == 0) { 9449 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 9450 buf->dtb_offset = 0; 9451 woffs = total; 9452 9453 while (woffs < buf->dtb_size) 9454 tomax[woffs++] = 0; 9455 } 9456 9457 woffs = 0; 9458 break; 9459 } 9460 9461 woffs += size; 9462 } 9463 9464 /* 9465 * We have a wrapped offset. It may be that the wrapped offset 9466 * has become zero -- that's okay. 9467 */ 9468 buf->dtb_xamot_offset = woffs; 9469 } 9470 9471 out: 9472 /* 9473 * Now we can plow the buffer with any necessary padding. 9474 */ 9475 while (offs & (align - 1)) { 9476 /* 9477 * Assert that our alignment is off by a number which 9478 * is itself sizeof (uint32_t) aligned. 9479 */ 9480 ASSERT(!((align - (offs & (align - 1))) & 9481 (sizeof (uint32_t) - 1))); 9482 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 9483 offs += sizeof (uint32_t); 9484 } 9485 9486 if (buf->dtb_flags & DTRACEBUF_FILL) { 9487 if (offs + needed > buf->dtb_size - state->dts_reserve) { 9488 buf->dtb_flags |= DTRACEBUF_FULL; 9489 return (-1); 9490 } 9491 } 9492 9493 if (mstate == NULL) 9494 return (offs); 9495 9496 /* 9497 * For ring buffers and fill buffers, the scratch space is always 9498 * the inactive buffer. 9499 */ 9500 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 9501 mstate->dtms_scratch_size = buf->dtb_size; 9502 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 9503 9504 return (offs); 9505 } 9506 9507 static void 9508 dtrace_buffer_polish(dtrace_buffer_t *buf) 9509 { 9510 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 9511 ASSERT(MUTEX_HELD(&dtrace_lock)); 9512 9513 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 9514 return; 9515 9516 /* 9517 * We need to polish the ring buffer. There are three cases: 9518 * 9519 * - The first (and presumably most common) is that there is no gap 9520 * between the buffer offset and the wrapped offset. In this case, 9521 * there is nothing in the buffer that isn't valid data; we can 9522 * mark the buffer as polished and return. 9523 * 9524 * - The second (less common than the first but still more common 9525 * than the third) is that there is a gap between the buffer offset 9526 * and the wrapped offset, and the wrapped offset is larger than the 9527 * buffer offset. This can happen because of an alignment issue, or 9528 * can happen because of a call to dtrace_buffer_reserve() that 9529 * didn't subsequently consume the buffer space. In this case, 9530 * we need to zero the data from the buffer offset to the wrapped 9531 * offset. 9532 * 9533 * - The third (and least common) is that there is a gap between the 9534 * buffer offset and the wrapped offset, but the wrapped offset is 9535 * _less_ than the buffer offset. This can only happen because a 9536 * call to dtrace_buffer_reserve() induced a wrap, but the space 9537 * was not subsequently consumed. In this case, we need to zero the 9538 * space from the offset to the end of the buffer _and_ from the 9539 * top of the buffer to the wrapped offset. 9540 */ 9541 if (buf->dtb_offset < buf->dtb_xamot_offset) { 9542 bzero(buf->dtb_tomax + buf->dtb_offset, 9543 buf->dtb_xamot_offset - buf->dtb_offset); 9544 } 9545 9546 if (buf->dtb_offset > buf->dtb_xamot_offset) { 9547 bzero(buf->dtb_tomax + buf->dtb_offset, 9548 buf->dtb_size - buf->dtb_offset); 9549 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 9550 } 9551 } 9552 9553 static void 9554 dtrace_buffer_free(dtrace_buffer_t *bufs) 9555 { 9556 int i; 9557 9558 for (i = 0; i < NCPU; i++) { 9559 dtrace_buffer_t *buf = &bufs[i]; 9560 9561 if (buf->dtb_tomax == NULL) { 9562 ASSERT(buf->dtb_xamot == NULL); 9563 ASSERT(buf->dtb_size == 0); 9564 continue; 9565 } 9566 9567 if (buf->dtb_xamot != NULL) { 9568 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 9569 kmem_free(buf->dtb_xamot, buf->dtb_size); 9570 } 9571 9572 kmem_free(buf->dtb_tomax, buf->dtb_size); 9573 buf->dtb_size = 0; 9574 buf->dtb_tomax = NULL; 9575 buf->dtb_xamot = NULL; 9576 } 9577 } 9578 9579 /* 9580 * DTrace Enabling Functions 9581 */ 9582 static dtrace_enabling_t * 9583 dtrace_enabling_create(dtrace_vstate_t *vstate) 9584 { 9585 dtrace_enabling_t *enab; 9586 9587 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 9588 enab->dten_vstate = vstate; 9589 9590 return (enab); 9591 } 9592 9593 static void 9594 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 9595 { 9596 dtrace_ecbdesc_t **ndesc; 9597 size_t osize, nsize; 9598 9599 /* 9600 * We can't add to enablings after we've enabled them, or after we've 9601 * retained them. 9602 */ 9603 ASSERT(enab->dten_probegen == 0); 9604 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 9605 9606 if (enab->dten_ndesc < enab->dten_maxdesc) { 9607 enab->dten_desc[enab->dten_ndesc++] = ecb; 9608 return; 9609 } 9610 9611 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 9612 9613 if (enab->dten_maxdesc == 0) { 9614 enab->dten_maxdesc = 1; 9615 } else { 9616 enab->dten_maxdesc <<= 1; 9617 } 9618 9619 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 9620 9621 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 9622 ndesc = kmem_zalloc(nsize, KM_SLEEP); 9623 bcopy(enab->dten_desc, ndesc, osize); 9624 kmem_free(enab->dten_desc, osize); 9625 9626 enab->dten_desc = ndesc; 9627 enab->dten_desc[enab->dten_ndesc++] = ecb; 9628 } 9629 9630 static void 9631 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 9632 dtrace_probedesc_t *pd) 9633 { 9634 dtrace_ecbdesc_t *new; 9635 dtrace_predicate_t *pred; 9636 dtrace_actdesc_t *act; 9637 9638 /* 9639 * We're going to create a new ECB description that matches the 9640 * specified ECB in every way, but has the specified probe description. 9641 */ 9642 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 9643 9644 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 9645 dtrace_predicate_hold(pred); 9646 9647 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 9648 dtrace_actdesc_hold(act); 9649 9650 new->dted_action = ecb->dted_action; 9651 new->dted_pred = ecb->dted_pred; 9652 new->dted_probe = *pd; 9653 new->dted_uarg = ecb->dted_uarg; 9654 9655 dtrace_enabling_add(enab, new); 9656 } 9657 9658 static void 9659 dtrace_enabling_dump(dtrace_enabling_t *enab) 9660 { 9661 int i; 9662 9663 for (i = 0; i < enab->dten_ndesc; i++) { 9664 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 9665 9666 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 9667 desc->dtpd_provider, desc->dtpd_mod, 9668 desc->dtpd_func, desc->dtpd_name); 9669 } 9670 } 9671 9672 static void 9673 dtrace_enabling_destroy(dtrace_enabling_t *enab) 9674 { 9675 int i; 9676 dtrace_ecbdesc_t *ep; 9677 dtrace_vstate_t *vstate = enab->dten_vstate; 9678 9679 ASSERT(MUTEX_HELD(&dtrace_lock)); 9680 9681 for (i = 0; i < enab->dten_ndesc; i++) { 9682 dtrace_actdesc_t *act, *next; 9683 dtrace_predicate_t *pred; 9684 9685 ep = enab->dten_desc[i]; 9686 9687 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 9688 dtrace_predicate_release(pred, vstate); 9689 9690 for (act = ep->dted_action; act != NULL; act = next) { 9691 next = act->dtad_next; 9692 dtrace_actdesc_release(act, vstate); 9693 } 9694 9695 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 9696 } 9697 9698 kmem_free(enab->dten_desc, 9699 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 9700 9701 /* 9702 * If this was a retained enabling, decrement the dts_nretained count 9703 * and take it off of the dtrace_retained list. 9704 */ 9705 if (enab->dten_prev != NULL || enab->dten_next != NULL || 9706 dtrace_retained == enab) { 9707 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9708 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 9709 enab->dten_vstate->dtvs_state->dts_nretained--; 9710 } 9711 9712 if (enab->dten_prev == NULL) { 9713 if (dtrace_retained == enab) { 9714 dtrace_retained = enab->dten_next; 9715 9716 if (dtrace_retained != NULL) 9717 dtrace_retained->dten_prev = NULL; 9718 } 9719 } else { 9720 ASSERT(enab != dtrace_retained); 9721 ASSERT(dtrace_retained != NULL); 9722 enab->dten_prev->dten_next = enab->dten_next; 9723 } 9724 9725 if (enab->dten_next != NULL) { 9726 ASSERT(dtrace_retained != NULL); 9727 enab->dten_next->dten_prev = enab->dten_prev; 9728 } 9729 9730 kmem_free(enab, sizeof (dtrace_enabling_t)); 9731 } 9732 9733 static int 9734 dtrace_enabling_retain(dtrace_enabling_t *enab) 9735 { 9736 dtrace_state_t *state; 9737 9738 ASSERT(MUTEX_HELD(&dtrace_lock)); 9739 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 9740 ASSERT(enab->dten_vstate != NULL); 9741 9742 state = enab->dten_vstate->dtvs_state; 9743 ASSERT(state != NULL); 9744 9745 /* 9746 * We only allow each state to retain dtrace_retain_max enablings. 9747 */ 9748 if (state->dts_nretained >= dtrace_retain_max) 9749 return (ENOSPC); 9750 9751 state->dts_nretained++; 9752 9753 if (dtrace_retained == NULL) { 9754 dtrace_retained = enab; 9755 return (0); 9756 } 9757 9758 enab->dten_next = dtrace_retained; 9759 dtrace_retained->dten_prev = enab; 9760 dtrace_retained = enab; 9761 9762 return (0); 9763 } 9764 9765 static int 9766 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 9767 dtrace_probedesc_t *create) 9768 { 9769 dtrace_enabling_t *new, *enab; 9770 int found = 0, err = ENOENT; 9771 9772 ASSERT(MUTEX_HELD(&dtrace_lock)); 9773 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 9774 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 9775 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 9776 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 9777 9778 new = dtrace_enabling_create(&state->dts_vstate); 9779 9780 /* 9781 * Iterate over all retained enablings, looking for enablings that 9782 * match the specified state. 9783 */ 9784 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9785 int i; 9786 9787 /* 9788 * dtvs_state can only be NULL for helper enablings -- and 9789 * helper enablings can't be retained. 9790 */ 9791 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9792 9793 if (enab->dten_vstate->dtvs_state != state) 9794 continue; 9795 9796 /* 9797 * Now iterate over each probe description; we're looking for 9798 * an exact match to the specified probe description. 9799 */ 9800 for (i = 0; i < enab->dten_ndesc; i++) { 9801 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 9802 dtrace_probedesc_t *pd = &ep->dted_probe; 9803 9804 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 9805 continue; 9806 9807 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 9808 continue; 9809 9810 if (strcmp(pd->dtpd_func, match->dtpd_func)) 9811 continue; 9812 9813 if (strcmp(pd->dtpd_name, match->dtpd_name)) 9814 continue; 9815 9816 /* 9817 * We have a winning probe! Add it to our growing 9818 * enabling. 9819 */ 9820 found = 1; 9821 dtrace_enabling_addlike(new, ep, create); 9822 } 9823 } 9824 9825 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 9826 dtrace_enabling_destroy(new); 9827 return (err); 9828 } 9829 9830 return (0); 9831 } 9832 9833 static void 9834 dtrace_enabling_retract(dtrace_state_t *state) 9835 { 9836 dtrace_enabling_t *enab, *next; 9837 9838 ASSERT(MUTEX_HELD(&dtrace_lock)); 9839 9840 /* 9841 * Iterate over all retained enablings, destroy the enablings retained 9842 * for the specified state. 9843 */ 9844 for (enab = dtrace_retained; enab != NULL; enab = next) { 9845 next = enab->dten_next; 9846 9847 /* 9848 * dtvs_state can only be NULL for helper enablings -- and 9849 * helper enablings can't be retained. 9850 */ 9851 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9852 9853 if (enab->dten_vstate->dtvs_state == state) { 9854 ASSERT(state->dts_nretained > 0); 9855 dtrace_enabling_destroy(enab); 9856 } 9857 } 9858 9859 ASSERT(state->dts_nretained == 0); 9860 } 9861 9862 static int 9863 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 9864 { 9865 int i = 0; 9866 int matched = 0; 9867 9868 ASSERT(MUTEX_HELD(&cpu_lock)); 9869 ASSERT(MUTEX_HELD(&dtrace_lock)); 9870 9871 for (i = 0; i < enab->dten_ndesc; i++) { 9872 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 9873 9874 enab->dten_current = ep; 9875 enab->dten_error = 0; 9876 9877 matched += dtrace_probe_enable(&ep->dted_probe, enab); 9878 9879 if (enab->dten_error != 0) { 9880 /* 9881 * If we get an error half-way through enabling the 9882 * probes, we kick out -- perhaps with some number of 9883 * them enabled. Leaving enabled probes enabled may 9884 * be slightly confusing for user-level, but we expect 9885 * that no one will attempt to actually drive on in 9886 * the face of such errors. If this is an anonymous 9887 * enabling (indicated with a NULL nmatched pointer), 9888 * we cmn_err() a message. We aren't expecting to 9889 * get such an error -- such as it can exist at all, 9890 * it would be a result of corrupted DOF in the driver 9891 * properties. 9892 */ 9893 if (nmatched == NULL) { 9894 cmn_err(CE_WARN, "dtrace_enabling_match() " 9895 "error on %p: %d", (void *)ep, 9896 enab->dten_error); 9897 } 9898 9899 return (enab->dten_error); 9900 } 9901 } 9902 9903 enab->dten_probegen = dtrace_probegen; 9904 if (nmatched != NULL) 9905 *nmatched = matched; 9906 9907 return (0); 9908 } 9909 9910 static void 9911 dtrace_enabling_matchall(void) 9912 { 9913 dtrace_enabling_t *enab; 9914 9915 mutex_enter(&cpu_lock); 9916 mutex_enter(&dtrace_lock); 9917 9918 /* 9919 * Because we can be called after dtrace_detach() has been called, we 9920 * cannot assert that there are retained enablings. We can safely 9921 * load from dtrace_retained, however: the taskq_destroy() at the 9922 * end of dtrace_detach() will block pending our completion. 9923 */ 9924 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) 9925 (void) dtrace_enabling_match(enab, NULL); 9926 9927 mutex_exit(&dtrace_lock); 9928 mutex_exit(&cpu_lock); 9929 } 9930 9931 static int 9932 dtrace_enabling_matchstate(dtrace_state_t *state, int *nmatched) 9933 { 9934 dtrace_enabling_t *enab; 9935 int matched, total = 0, err; 9936 9937 ASSERT(MUTEX_HELD(&cpu_lock)); 9938 ASSERT(MUTEX_HELD(&dtrace_lock)); 9939 9940 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9941 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9942 9943 if (enab->dten_vstate->dtvs_state != state) 9944 continue; 9945 9946 if ((err = dtrace_enabling_match(enab, &matched)) != 0) 9947 return (err); 9948 9949 total += matched; 9950 } 9951 9952 if (nmatched != NULL) 9953 *nmatched = total; 9954 9955 return (0); 9956 } 9957 9958 /* 9959 * If an enabling is to be enabled without having matched probes (that is, if 9960 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 9961 * enabling must be _primed_ by creating an ECB for every ECB description. 9962 * This must be done to assure that we know the number of speculations, the 9963 * number of aggregations, the minimum buffer size needed, etc. before we 9964 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 9965 * enabling any probes, we create ECBs for every ECB decription, but with a 9966 * NULL probe -- which is exactly what this function does. 9967 */ 9968 static void 9969 dtrace_enabling_prime(dtrace_state_t *state) 9970 { 9971 dtrace_enabling_t *enab; 9972 int i; 9973 9974 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 9975 ASSERT(enab->dten_vstate->dtvs_state != NULL); 9976 9977 if (enab->dten_vstate->dtvs_state != state) 9978 continue; 9979 9980 /* 9981 * We don't want to prime an enabling more than once, lest 9982 * we allow a malicious user to induce resource exhaustion. 9983 * (The ECBs that result from priming an enabling aren't 9984 * leaked -- but they also aren't deallocated until the 9985 * consumer state is destroyed.) 9986 */ 9987 if (enab->dten_primed) 9988 continue; 9989 9990 for (i = 0; i < enab->dten_ndesc; i++) { 9991 enab->dten_current = enab->dten_desc[i]; 9992 (void) dtrace_probe_enable(NULL, enab); 9993 } 9994 9995 enab->dten_primed = 1; 9996 } 9997 } 9998 9999 /* 10000 * Called to indicate that probes should be provided due to retained 10001 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 10002 * must take an initial lap through the enabling calling the dtps_provide() 10003 * entry point explicitly to allow for autocreated probes. 10004 */ 10005 static void 10006 dtrace_enabling_provide(dtrace_provider_t *prv) 10007 { 10008 int i, all = 0; 10009 dtrace_probedesc_t desc; 10010 10011 ASSERT(MUTEX_HELD(&dtrace_lock)); 10012 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 10013 10014 if (prv == NULL) { 10015 all = 1; 10016 prv = dtrace_provider; 10017 } 10018 10019 do { 10020 dtrace_enabling_t *enab = dtrace_retained; 10021 void *parg = prv->dtpv_arg; 10022 10023 for (; enab != NULL; enab = enab->dten_next) { 10024 for (i = 0; i < enab->dten_ndesc; i++) { 10025 desc = enab->dten_desc[i]->dted_probe; 10026 mutex_exit(&dtrace_lock); 10027 prv->dtpv_pops.dtps_provide(parg, &desc); 10028 mutex_enter(&dtrace_lock); 10029 } 10030 } 10031 } while (all && (prv = prv->dtpv_next) != NULL); 10032 10033 mutex_exit(&dtrace_lock); 10034 dtrace_probe_provide(NULL, all ? NULL : prv); 10035 mutex_enter(&dtrace_lock); 10036 } 10037 10038 /* 10039 * DTrace DOF Functions 10040 */ 10041 /*ARGSUSED*/ 10042 static void 10043 dtrace_dof_error(dof_hdr_t *dof, const char *str) 10044 { 10045 if (dtrace_err_verbose) 10046 cmn_err(CE_WARN, "failed to process DOF: %s", str); 10047 10048 #ifdef DTRACE_ERRDEBUG 10049 dtrace_errdebug(str); 10050 #endif 10051 } 10052 10053 /* 10054 * Create DOF out of a currently enabled state. Right now, we only create 10055 * DOF containing the run-time options -- but this could be expanded to create 10056 * complete DOF representing the enabled state. 10057 */ 10058 static dof_hdr_t * 10059 dtrace_dof_create(dtrace_state_t *state) 10060 { 10061 dof_hdr_t *dof; 10062 dof_sec_t *sec; 10063 dof_optdesc_t *opt; 10064 int i, len = sizeof (dof_hdr_t) + 10065 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 10066 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 10067 10068 ASSERT(MUTEX_HELD(&dtrace_lock)); 10069 10070 dof = kmem_zalloc(len, KM_SLEEP); 10071 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 10072 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 10073 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 10074 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 10075 10076 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 10077 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 10078 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 10079 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 10080 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 10081 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 10082 10083 dof->dofh_flags = 0; 10084 dof->dofh_hdrsize = sizeof (dof_hdr_t); 10085 dof->dofh_secsize = sizeof (dof_sec_t); 10086 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 10087 dof->dofh_secoff = sizeof (dof_hdr_t); 10088 dof->dofh_loadsz = len; 10089 dof->dofh_filesz = len; 10090 dof->dofh_pad = 0; 10091 10092 /* 10093 * Fill in the option section header... 10094 */ 10095 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 10096 sec->dofs_type = DOF_SECT_OPTDESC; 10097 sec->dofs_align = sizeof (uint64_t); 10098 sec->dofs_flags = DOF_SECF_LOAD; 10099 sec->dofs_entsize = sizeof (dof_optdesc_t); 10100 10101 opt = (dof_optdesc_t *)((uintptr_t)sec + 10102 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 10103 10104 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 10105 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 10106 10107 for (i = 0; i < DTRACEOPT_MAX; i++) { 10108 opt[i].dofo_option = i; 10109 opt[i].dofo_strtab = DOF_SECIDX_NONE; 10110 opt[i].dofo_value = state->dts_options[i]; 10111 } 10112 10113 return (dof); 10114 } 10115 10116 static dof_hdr_t * 10117 dtrace_dof_copyin(uintptr_t uarg, int *errp) 10118 { 10119 dof_hdr_t hdr, *dof; 10120 10121 ASSERT(!MUTEX_HELD(&dtrace_lock)); 10122 10123 /* 10124 * First, we're going to copyin() the sizeof (dof_hdr_t). 10125 */ 10126 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 10127 dtrace_dof_error(NULL, "failed to copyin DOF header"); 10128 *errp = EFAULT; 10129 return (NULL); 10130 } 10131 10132 /* 10133 * Now we'll allocate the entire DOF and copy it in -- provided 10134 * that the length isn't outrageous. 10135 */ 10136 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 10137 dtrace_dof_error(&hdr, "load size exceeds maximum"); 10138 *errp = E2BIG; 10139 return (NULL); 10140 } 10141 10142 if (hdr.dofh_loadsz < sizeof (hdr)) { 10143 dtrace_dof_error(&hdr, "invalid load size"); 10144 *errp = EINVAL; 10145 return (NULL); 10146 } 10147 10148 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 10149 10150 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) { 10151 kmem_free(dof, hdr.dofh_loadsz); 10152 *errp = EFAULT; 10153 return (NULL); 10154 } 10155 10156 return (dof); 10157 } 10158 10159 static dof_hdr_t * 10160 dtrace_dof_property(const char *name) 10161 { 10162 uchar_t *buf; 10163 uint64_t loadsz; 10164 unsigned int len, i; 10165 dof_hdr_t *dof; 10166 10167 /* 10168 * Unfortunately, array of values in .conf files are always (and 10169 * only) interpreted to be integer arrays. We must read our DOF 10170 * as an integer array, and then squeeze it into a byte array. 10171 */ 10172 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 10173 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 10174 return (NULL); 10175 10176 for (i = 0; i < len; i++) 10177 buf[i] = (uchar_t)(((int *)buf)[i]); 10178 10179 if (len < sizeof (dof_hdr_t)) { 10180 ddi_prop_free(buf); 10181 dtrace_dof_error(NULL, "truncated header"); 10182 return (NULL); 10183 } 10184 10185 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 10186 ddi_prop_free(buf); 10187 dtrace_dof_error(NULL, "truncated DOF"); 10188 return (NULL); 10189 } 10190 10191 if (loadsz >= dtrace_dof_maxsize) { 10192 ddi_prop_free(buf); 10193 dtrace_dof_error(NULL, "oversized DOF"); 10194 return (NULL); 10195 } 10196 10197 dof = kmem_alloc(loadsz, KM_SLEEP); 10198 bcopy(buf, dof, loadsz); 10199 ddi_prop_free(buf); 10200 10201 return (dof); 10202 } 10203 10204 static void 10205 dtrace_dof_destroy(dof_hdr_t *dof) 10206 { 10207 kmem_free(dof, dof->dofh_loadsz); 10208 } 10209 10210 /* 10211 * Return the dof_sec_t pointer corresponding to a given section index. If the 10212 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 10213 * a type other than DOF_SECT_NONE is specified, the header is checked against 10214 * this type and NULL is returned if the types do not match. 10215 */ 10216 static dof_sec_t * 10217 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 10218 { 10219 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 10220 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 10221 10222 if (i >= dof->dofh_secnum) { 10223 dtrace_dof_error(dof, "referenced section index is invalid"); 10224 return (NULL); 10225 } 10226 10227 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 10228 dtrace_dof_error(dof, "referenced section is not loadable"); 10229 return (NULL); 10230 } 10231 10232 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 10233 dtrace_dof_error(dof, "referenced section is the wrong type"); 10234 return (NULL); 10235 } 10236 10237 return (sec); 10238 } 10239 10240 static dtrace_probedesc_t * 10241 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 10242 { 10243 dof_probedesc_t *probe; 10244 dof_sec_t *strtab; 10245 uintptr_t daddr = (uintptr_t)dof; 10246 uintptr_t str; 10247 size_t size; 10248 10249 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 10250 dtrace_dof_error(dof, "invalid probe section"); 10251 return (NULL); 10252 } 10253 10254 if (sec->dofs_align != sizeof (dof_secidx_t)) { 10255 dtrace_dof_error(dof, "bad alignment in probe description"); 10256 return (NULL); 10257 } 10258 10259 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 10260 dtrace_dof_error(dof, "truncated probe description"); 10261 return (NULL); 10262 } 10263 10264 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 10265 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 10266 10267 if (strtab == NULL) 10268 return (NULL); 10269 10270 str = daddr + strtab->dofs_offset; 10271 size = strtab->dofs_size; 10272 10273 if (probe->dofp_provider >= strtab->dofs_size) { 10274 dtrace_dof_error(dof, "corrupt probe provider"); 10275 return (NULL); 10276 } 10277 10278 (void) strncpy(desc->dtpd_provider, 10279 (char *)(str + probe->dofp_provider), 10280 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 10281 10282 if (probe->dofp_mod >= strtab->dofs_size) { 10283 dtrace_dof_error(dof, "corrupt probe module"); 10284 return (NULL); 10285 } 10286 10287 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 10288 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 10289 10290 if (probe->dofp_func >= strtab->dofs_size) { 10291 dtrace_dof_error(dof, "corrupt probe function"); 10292 return (NULL); 10293 } 10294 10295 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 10296 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 10297 10298 if (probe->dofp_name >= strtab->dofs_size) { 10299 dtrace_dof_error(dof, "corrupt probe name"); 10300 return (NULL); 10301 } 10302 10303 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 10304 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 10305 10306 return (desc); 10307 } 10308 10309 static dtrace_difo_t * 10310 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10311 cred_t *cr) 10312 { 10313 dtrace_difo_t *dp; 10314 size_t ttl = 0; 10315 dof_difohdr_t *dofd; 10316 uintptr_t daddr = (uintptr_t)dof; 10317 size_t max = dtrace_difo_maxsize; 10318 int i, l, n; 10319 10320 static const struct { 10321 int section; 10322 int bufoffs; 10323 int lenoffs; 10324 int entsize; 10325 int align; 10326 const char *msg; 10327 } difo[] = { 10328 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 10329 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 10330 sizeof (dif_instr_t), "multiple DIF sections" }, 10331 10332 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 10333 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 10334 sizeof (uint64_t), "multiple integer tables" }, 10335 10336 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 10337 offsetof(dtrace_difo_t, dtdo_strlen), 0, 10338 sizeof (char), "multiple string tables" }, 10339 10340 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 10341 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 10342 sizeof (uint_t), "multiple variable tables" }, 10343 10344 { DOF_SECT_NONE, 0, 0, 0, NULL } 10345 }; 10346 10347 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 10348 dtrace_dof_error(dof, "invalid DIFO header section"); 10349 return (NULL); 10350 } 10351 10352 if (sec->dofs_align != sizeof (dof_secidx_t)) { 10353 dtrace_dof_error(dof, "bad alignment in DIFO header"); 10354 return (NULL); 10355 } 10356 10357 if (sec->dofs_size < sizeof (dof_difohdr_t) || 10358 sec->dofs_size % sizeof (dof_secidx_t)) { 10359 dtrace_dof_error(dof, "bad size in DIFO header"); 10360 return (NULL); 10361 } 10362 10363 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 10364 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 10365 10366 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10367 dp->dtdo_rtype = dofd->dofd_rtype; 10368 10369 for (l = 0; l < n; l++) { 10370 dof_sec_t *subsec; 10371 void **bufp; 10372 uint32_t *lenp; 10373 10374 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 10375 dofd->dofd_links[l])) == NULL) 10376 goto err; /* invalid section link */ 10377 10378 if (ttl + subsec->dofs_size > max) { 10379 dtrace_dof_error(dof, "exceeds maximum size"); 10380 goto err; 10381 } 10382 10383 ttl += subsec->dofs_size; 10384 10385 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 10386 if (subsec->dofs_type != difo[i].section) 10387 continue; 10388 10389 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 10390 dtrace_dof_error(dof, "section not loaded"); 10391 goto err; 10392 } 10393 10394 if (subsec->dofs_align != difo[i].align) { 10395 dtrace_dof_error(dof, "bad alignment"); 10396 goto err; 10397 } 10398 10399 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 10400 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 10401 10402 if (*bufp != NULL) { 10403 dtrace_dof_error(dof, difo[i].msg); 10404 goto err; 10405 } 10406 10407 if (difo[i].entsize != subsec->dofs_entsize) { 10408 dtrace_dof_error(dof, "entry size mismatch"); 10409 goto err; 10410 } 10411 10412 if (subsec->dofs_entsize != 0 && 10413 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 10414 dtrace_dof_error(dof, "corrupt entry size"); 10415 goto err; 10416 } 10417 10418 *lenp = subsec->dofs_size; 10419 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 10420 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 10421 *bufp, subsec->dofs_size); 10422 10423 if (subsec->dofs_entsize != 0) 10424 *lenp /= subsec->dofs_entsize; 10425 10426 break; 10427 } 10428 10429 /* 10430 * If we encounter a loadable DIFO sub-section that is not 10431 * known to us, assume this is a broken program and fail. 10432 */ 10433 if (difo[i].section == DOF_SECT_NONE && 10434 (subsec->dofs_flags & DOF_SECF_LOAD)) { 10435 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 10436 goto err; 10437 } 10438 } 10439 10440 if (dp->dtdo_buf == NULL) { 10441 /* 10442 * We can't have a DIF object without DIF text. 10443 */ 10444 dtrace_dof_error(dof, "missing DIF text"); 10445 goto err; 10446 } 10447 10448 /* 10449 * Before we validate the DIF object, run through the variable table 10450 * looking for the strings -- if any of their size are under, we'll set 10451 * their size to be the system-wide default string size. Note that 10452 * this should _not_ happen if the "strsize" option has been set -- 10453 * in this case, the compiler should have set the size to reflect the 10454 * setting of the option. 10455 */ 10456 for (i = 0; i < dp->dtdo_varlen; i++) { 10457 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10458 dtrace_diftype_t *t = &v->dtdv_type; 10459 10460 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 10461 continue; 10462 10463 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 10464 t->dtdt_size = dtrace_strsize_default; 10465 } 10466 10467 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 10468 goto err; 10469 10470 dtrace_difo_init(dp, vstate); 10471 return (dp); 10472 10473 err: 10474 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10475 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10476 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10477 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10478 10479 kmem_free(dp, sizeof (dtrace_difo_t)); 10480 return (NULL); 10481 } 10482 10483 static dtrace_predicate_t * 10484 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10485 cred_t *cr) 10486 { 10487 dtrace_difo_t *dp; 10488 10489 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 10490 return (NULL); 10491 10492 return (dtrace_predicate_create(dp)); 10493 } 10494 10495 static dtrace_actdesc_t * 10496 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10497 cred_t *cr) 10498 { 10499 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 10500 dof_actdesc_t *desc; 10501 dof_sec_t *difosec; 10502 size_t offs; 10503 uintptr_t daddr = (uintptr_t)dof; 10504 uint64_t arg; 10505 dtrace_actkind_t kind; 10506 10507 if (sec->dofs_type != DOF_SECT_ACTDESC) { 10508 dtrace_dof_error(dof, "invalid action section"); 10509 return (NULL); 10510 } 10511 10512 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 10513 dtrace_dof_error(dof, "truncated action description"); 10514 return (NULL); 10515 } 10516 10517 if (sec->dofs_align != sizeof (uint64_t)) { 10518 dtrace_dof_error(dof, "bad alignment in action description"); 10519 return (NULL); 10520 } 10521 10522 if (sec->dofs_size < sec->dofs_entsize) { 10523 dtrace_dof_error(dof, "section entry size exceeds total size"); 10524 return (NULL); 10525 } 10526 10527 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 10528 dtrace_dof_error(dof, "bad entry size in action description"); 10529 return (NULL); 10530 } 10531 10532 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 10533 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 10534 return (NULL); 10535 } 10536 10537 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 10538 desc = (dof_actdesc_t *)(daddr + 10539 (uintptr_t)sec->dofs_offset + offs); 10540 kind = (dtrace_actkind_t)desc->dofa_kind; 10541 10542 if (DTRACEACT_ISPRINTFLIKE(kind) && 10543 (kind != DTRACEACT_PRINTA || 10544 desc->dofa_strtab != DOF_SECIDX_NONE)) { 10545 dof_sec_t *strtab; 10546 char *str, *fmt; 10547 uint64_t i; 10548 10549 /* 10550 * printf()-like actions must have a format string. 10551 */ 10552 if ((strtab = dtrace_dof_sect(dof, 10553 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 10554 goto err; 10555 10556 str = (char *)((uintptr_t)dof + 10557 (uintptr_t)strtab->dofs_offset); 10558 10559 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 10560 if (str[i] == '\0') 10561 break; 10562 } 10563 10564 if (i >= strtab->dofs_size) { 10565 dtrace_dof_error(dof, "bogus format string"); 10566 goto err; 10567 } 10568 10569 if (i == desc->dofa_arg) { 10570 dtrace_dof_error(dof, "empty format string"); 10571 goto err; 10572 } 10573 10574 i -= desc->dofa_arg; 10575 fmt = kmem_alloc(i + 1, KM_SLEEP); 10576 bcopy(&str[desc->dofa_arg], fmt, i + 1); 10577 arg = (uint64_t)(uintptr_t)fmt; 10578 } else { 10579 if (kind == DTRACEACT_PRINTA) { 10580 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 10581 arg = 0; 10582 } else { 10583 arg = desc->dofa_arg; 10584 } 10585 } 10586 10587 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 10588 desc->dofa_uarg, arg); 10589 10590 if (last != NULL) { 10591 last->dtad_next = act; 10592 } else { 10593 first = act; 10594 } 10595 10596 last = act; 10597 10598 if (desc->dofa_difo == DOF_SECIDX_NONE) 10599 continue; 10600 10601 if ((difosec = dtrace_dof_sect(dof, 10602 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 10603 goto err; 10604 10605 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 10606 10607 if (act->dtad_difo == NULL) 10608 goto err; 10609 } 10610 10611 ASSERT(first != NULL); 10612 return (first); 10613 10614 err: 10615 for (act = first; act != NULL; act = next) { 10616 next = act->dtad_next; 10617 dtrace_actdesc_release(act, vstate); 10618 } 10619 10620 return (NULL); 10621 } 10622 10623 static dtrace_ecbdesc_t * 10624 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 10625 cred_t *cr) 10626 { 10627 dtrace_ecbdesc_t *ep; 10628 dof_ecbdesc_t *ecb; 10629 dtrace_probedesc_t *desc; 10630 dtrace_predicate_t *pred = NULL; 10631 10632 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 10633 dtrace_dof_error(dof, "truncated ECB description"); 10634 return (NULL); 10635 } 10636 10637 if (sec->dofs_align != sizeof (uint64_t)) { 10638 dtrace_dof_error(dof, "bad alignment in ECB description"); 10639 return (NULL); 10640 } 10641 10642 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 10643 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 10644 10645 if (sec == NULL) 10646 return (NULL); 10647 10648 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 10649 ep->dted_uarg = ecb->dofe_uarg; 10650 desc = &ep->dted_probe; 10651 10652 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 10653 goto err; 10654 10655 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 10656 if ((sec = dtrace_dof_sect(dof, 10657 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 10658 goto err; 10659 10660 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 10661 goto err; 10662 10663 ep->dted_pred.dtpdd_predicate = pred; 10664 } 10665 10666 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 10667 if ((sec = dtrace_dof_sect(dof, 10668 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 10669 goto err; 10670 10671 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 10672 10673 if (ep->dted_action == NULL) 10674 goto err; 10675 } 10676 10677 return (ep); 10678 10679 err: 10680 if (pred != NULL) 10681 dtrace_predicate_release(pred, vstate); 10682 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 10683 return (NULL); 10684 } 10685 10686 /* 10687 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 10688 * specified DOF. At present, this amounts to simply adding 'ubase' to the 10689 * site of any user SETX relocations to account for load object base address. 10690 * In the future, if we need other relocations, this function can be extended. 10691 */ 10692 static int 10693 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 10694 { 10695 uintptr_t daddr = (uintptr_t)dof; 10696 dof_relohdr_t *dofr = 10697 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 10698 dof_sec_t *ss, *rs, *ts; 10699 dof_relodesc_t *r; 10700 uint_t i, n; 10701 10702 if (sec->dofs_size < sizeof (dof_relohdr_t) || 10703 sec->dofs_align != sizeof (dof_secidx_t)) { 10704 dtrace_dof_error(dof, "invalid relocation header"); 10705 return (-1); 10706 } 10707 10708 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 10709 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 10710 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 10711 10712 if (ss == NULL || rs == NULL || ts == NULL) 10713 return (-1); /* dtrace_dof_error() has been called already */ 10714 10715 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 10716 rs->dofs_align != sizeof (uint64_t)) { 10717 dtrace_dof_error(dof, "invalid relocation section"); 10718 return (-1); 10719 } 10720 10721 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 10722 n = rs->dofs_size / rs->dofs_entsize; 10723 10724 for (i = 0; i < n; i++) { 10725 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 10726 10727 switch (r->dofr_type) { 10728 case DOF_RELO_NONE: 10729 break; 10730 case DOF_RELO_SETX: 10731 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 10732 sizeof (uint64_t) > ts->dofs_size) { 10733 dtrace_dof_error(dof, "bad relocation offset"); 10734 return (-1); 10735 } 10736 10737 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 10738 dtrace_dof_error(dof, "misaligned setx relo"); 10739 return (-1); 10740 } 10741 10742 *(uint64_t *)taddr += ubase; 10743 break; 10744 default: 10745 dtrace_dof_error(dof, "invalid relocation type"); 10746 return (-1); 10747 } 10748 10749 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 10750 } 10751 10752 return (0); 10753 } 10754 10755 /* 10756 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 10757 * header: it should be at the front of a memory region that is at least 10758 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 10759 * size. It need not be validated in any other way. 10760 */ 10761 static int 10762 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 10763 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 10764 { 10765 uint64_t len = dof->dofh_loadsz, seclen; 10766 uintptr_t daddr = (uintptr_t)dof; 10767 dtrace_ecbdesc_t *ep; 10768 dtrace_enabling_t *enab; 10769 uint_t i; 10770 10771 ASSERT(MUTEX_HELD(&dtrace_lock)); 10772 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 10773 10774 /* 10775 * Check the DOF header identification bytes. In addition to checking 10776 * valid settings, we also verify that unused bits/bytes are zeroed so 10777 * we can use them later without fear of regressing existing binaries. 10778 */ 10779 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 10780 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 10781 dtrace_dof_error(dof, "DOF magic string mismatch"); 10782 return (-1); 10783 } 10784 10785 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 10786 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 10787 dtrace_dof_error(dof, "DOF has invalid data model"); 10788 return (-1); 10789 } 10790 10791 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 10792 dtrace_dof_error(dof, "DOF encoding mismatch"); 10793 return (-1); 10794 } 10795 10796 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 10797 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 10798 dtrace_dof_error(dof, "DOF version mismatch"); 10799 return (-1); 10800 } 10801 10802 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 10803 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 10804 return (-1); 10805 } 10806 10807 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 10808 dtrace_dof_error(dof, "DOF uses too many integer registers"); 10809 return (-1); 10810 } 10811 10812 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 10813 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 10814 return (-1); 10815 } 10816 10817 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 10818 if (dof->dofh_ident[i] != 0) { 10819 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 10820 return (-1); 10821 } 10822 } 10823 10824 if (dof->dofh_flags & ~DOF_FL_VALID) { 10825 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 10826 return (-1); 10827 } 10828 10829 if (dof->dofh_secsize == 0) { 10830 dtrace_dof_error(dof, "zero section header size"); 10831 return (-1); 10832 } 10833 10834 /* 10835 * Check that the section headers don't exceed the amount of DOF 10836 * data. Note that we cast the section size and number of sections 10837 * to uint64_t's to prevent possible overflow in the multiplication. 10838 */ 10839 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 10840 10841 if (dof->dofh_secoff > len || seclen > len || 10842 dof->dofh_secoff + seclen > len) { 10843 dtrace_dof_error(dof, "truncated section headers"); 10844 return (-1); 10845 } 10846 10847 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 10848 dtrace_dof_error(dof, "misaligned section headers"); 10849 return (-1); 10850 } 10851 10852 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 10853 dtrace_dof_error(dof, "misaligned section size"); 10854 return (-1); 10855 } 10856 10857 /* 10858 * Take an initial pass through the section headers to be sure that 10859 * the headers don't have stray offsets. If the 'noprobes' flag is 10860 * set, do not permit sections relating to providers, probes, or args. 10861 */ 10862 for (i = 0; i < dof->dofh_secnum; i++) { 10863 dof_sec_t *sec = (dof_sec_t *)(daddr + 10864 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10865 10866 if (noprobes) { 10867 switch (sec->dofs_type) { 10868 case DOF_SECT_PROVIDER: 10869 case DOF_SECT_PROBES: 10870 case DOF_SECT_PRARGS: 10871 case DOF_SECT_PROFFS: 10872 dtrace_dof_error(dof, "illegal sections " 10873 "for enabling"); 10874 return (-1); 10875 } 10876 } 10877 10878 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 10879 continue; /* just ignore non-loadable sections */ 10880 10881 if (sec->dofs_align & (sec->dofs_align - 1)) { 10882 dtrace_dof_error(dof, "bad section alignment"); 10883 return (-1); 10884 } 10885 10886 if (sec->dofs_offset & (sec->dofs_align - 1)) { 10887 dtrace_dof_error(dof, "misaligned section"); 10888 return (-1); 10889 } 10890 10891 if (sec->dofs_offset > len || sec->dofs_size > len || 10892 sec->dofs_offset + sec->dofs_size > len) { 10893 dtrace_dof_error(dof, "corrupt section header"); 10894 return (-1); 10895 } 10896 10897 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 10898 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 10899 dtrace_dof_error(dof, "non-terminating string table"); 10900 return (-1); 10901 } 10902 } 10903 10904 /* 10905 * Take a second pass through the sections and locate and perform any 10906 * relocations that are present. We do this after the first pass to 10907 * be sure that all sections have had their headers validated. 10908 */ 10909 for (i = 0; i < dof->dofh_secnum; i++) { 10910 dof_sec_t *sec = (dof_sec_t *)(daddr + 10911 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10912 10913 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 10914 continue; /* skip sections that are not loadable */ 10915 10916 switch (sec->dofs_type) { 10917 case DOF_SECT_URELHDR: 10918 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 10919 return (-1); 10920 break; 10921 } 10922 } 10923 10924 if ((enab = *enabp) == NULL) 10925 enab = *enabp = dtrace_enabling_create(vstate); 10926 10927 for (i = 0; i < dof->dofh_secnum; i++) { 10928 dof_sec_t *sec = (dof_sec_t *)(daddr + 10929 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10930 10931 if (sec->dofs_type != DOF_SECT_ECBDESC) 10932 continue; 10933 10934 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 10935 dtrace_enabling_destroy(enab); 10936 *enabp = NULL; 10937 return (-1); 10938 } 10939 10940 dtrace_enabling_add(enab, ep); 10941 } 10942 10943 return (0); 10944 } 10945 10946 /* 10947 * Process DOF for any options. This routine assumes that the DOF has been 10948 * at least processed by dtrace_dof_slurp(). 10949 */ 10950 static int 10951 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 10952 { 10953 int i, rval; 10954 uint32_t entsize; 10955 size_t offs; 10956 dof_optdesc_t *desc; 10957 10958 for (i = 0; i < dof->dofh_secnum; i++) { 10959 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 10960 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 10961 10962 if (sec->dofs_type != DOF_SECT_OPTDESC) 10963 continue; 10964 10965 if (sec->dofs_align != sizeof (uint64_t)) { 10966 dtrace_dof_error(dof, "bad alignment in " 10967 "option description"); 10968 return (EINVAL); 10969 } 10970 10971 if ((entsize = sec->dofs_entsize) == 0) { 10972 dtrace_dof_error(dof, "zeroed option entry size"); 10973 return (EINVAL); 10974 } 10975 10976 if (entsize < sizeof (dof_optdesc_t)) { 10977 dtrace_dof_error(dof, "bad option entry size"); 10978 return (EINVAL); 10979 } 10980 10981 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 10982 desc = (dof_optdesc_t *)((uintptr_t)dof + 10983 (uintptr_t)sec->dofs_offset + offs); 10984 10985 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 10986 dtrace_dof_error(dof, "non-zero option string"); 10987 return (EINVAL); 10988 } 10989 10990 if (desc->dofo_value == DTRACEOPT_UNSET) { 10991 dtrace_dof_error(dof, "unset option"); 10992 return (EINVAL); 10993 } 10994 10995 if ((rval = dtrace_state_option(state, 10996 desc->dofo_option, desc->dofo_value)) != 0) { 10997 dtrace_dof_error(dof, "rejected option"); 10998 return (rval); 10999 } 11000 } 11001 } 11002 11003 return (0); 11004 } 11005 11006 /* 11007 * DTrace Consumer State Functions 11008 */ 11009 int 11010 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 11011 { 11012 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 11013 void *base; 11014 uintptr_t limit; 11015 dtrace_dynvar_t *dvar, *next, *start; 11016 int i; 11017 11018 ASSERT(MUTEX_HELD(&dtrace_lock)); 11019 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 11020 11021 bzero(dstate, sizeof (dtrace_dstate_t)); 11022 11023 if ((dstate->dtds_chunksize = chunksize) == 0) 11024 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 11025 11026 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 11027 size = min; 11028 11029 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 11030 return (ENOMEM); 11031 11032 dstate->dtds_size = size; 11033 dstate->dtds_base = base; 11034 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 11035 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 11036 11037 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 11038 11039 if (hashsize != 1 && (hashsize & 1)) 11040 hashsize--; 11041 11042 dstate->dtds_hashsize = hashsize; 11043 dstate->dtds_hash = dstate->dtds_base; 11044 11045 /* 11046 * Set all of our hash buckets to point to the single sink, and (if 11047 * it hasn't already been set), set the sink's hash value to be the 11048 * sink sentinel value. The sink is needed for dynamic variable 11049 * lookups to know that they have iterated over an entire, valid hash 11050 * chain. 11051 */ 11052 for (i = 0; i < hashsize; i++) 11053 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 11054 11055 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 11056 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 11057 11058 /* 11059 * Determine number of active CPUs. Divide free list evenly among 11060 * active CPUs. 11061 */ 11062 start = (dtrace_dynvar_t *) 11063 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 11064 limit = (uintptr_t)base + size; 11065 11066 maxper = (limit - (uintptr_t)start) / NCPU; 11067 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 11068 11069 for (i = 0; i < NCPU; i++) { 11070 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 11071 11072 /* 11073 * If we don't even have enough chunks to make it once through 11074 * NCPUs, we're just going to allocate everything to the first 11075 * CPU. And if we're on the last CPU, we're going to allocate 11076 * whatever is left over. In either case, we set the limit to 11077 * be the limit of the dynamic variable space. 11078 */ 11079 if (maxper == 0 || i == NCPU - 1) { 11080 limit = (uintptr_t)base + size; 11081 start = NULL; 11082 } else { 11083 limit = (uintptr_t)start + maxper; 11084 start = (dtrace_dynvar_t *)limit; 11085 } 11086 11087 ASSERT(limit <= (uintptr_t)base + size); 11088 11089 for (;;) { 11090 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 11091 dstate->dtds_chunksize); 11092 11093 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 11094 break; 11095 11096 dvar->dtdv_next = next; 11097 dvar = next; 11098 } 11099 11100 if (maxper == 0) 11101 break; 11102 } 11103 11104 return (0); 11105 } 11106 11107 void 11108 dtrace_dstate_fini(dtrace_dstate_t *dstate) 11109 { 11110 ASSERT(MUTEX_HELD(&cpu_lock)); 11111 11112 if (dstate->dtds_base == NULL) 11113 return; 11114 11115 kmem_free(dstate->dtds_base, dstate->dtds_size); 11116 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 11117 } 11118 11119 static void 11120 dtrace_vstate_fini(dtrace_vstate_t *vstate) 11121 { 11122 /* 11123 * Logical XOR, where are you? 11124 */ 11125 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 11126 11127 if (vstate->dtvs_nglobals > 0) { 11128 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 11129 sizeof (dtrace_statvar_t *)); 11130 } 11131 11132 if (vstate->dtvs_ntlocals > 0) { 11133 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 11134 sizeof (dtrace_difv_t)); 11135 } 11136 11137 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 11138 11139 if (vstate->dtvs_nlocals > 0) { 11140 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 11141 sizeof (dtrace_statvar_t *)); 11142 } 11143 } 11144 11145 static void 11146 dtrace_state_clean(dtrace_state_t *state) 11147 { 11148 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 11149 return; 11150 11151 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 11152 dtrace_speculation_clean(state); 11153 } 11154 11155 static void 11156 dtrace_state_deadman(dtrace_state_t *state) 11157 { 11158 hrtime_t now; 11159 11160 dtrace_sync(); 11161 11162 now = dtrace_gethrtime(); 11163 11164 if (state != dtrace_anon.dta_state && 11165 now - state->dts_laststatus >= dtrace_deadman_user) 11166 return; 11167 11168 /* 11169 * We must be sure that dts_alive never appears to be less than the 11170 * value upon entry to dtrace_state_deadman(), and because we lack a 11171 * dtrace_cas64(), we cannot store to it atomically. We thus instead 11172 * store INT64_MAX to it, followed by a memory barrier, followed by 11173 * the new value. This assures that dts_alive never appears to be 11174 * less than its true value, regardless of the order in which the 11175 * stores to the underlying storage are issued. 11176 */ 11177 state->dts_alive = INT64_MAX; 11178 dtrace_membar_producer(); 11179 state->dts_alive = now; 11180 } 11181 11182 dtrace_state_t * 11183 dtrace_state_create(dev_t *devp, cred_t *cr) 11184 { 11185 minor_t minor; 11186 major_t major; 11187 char c[30]; 11188 dtrace_state_t *state; 11189 dtrace_optval_t *opt; 11190 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 11191 11192 ASSERT(MUTEX_HELD(&dtrace_lock)); 11193 ASSERT(MUTEX_HELD(&cpu_lock)); 11194 11195 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 11196 VM_BESTFIT | VM_SLEEP); 11197 11198 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 11199 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 11200 return (NULL); 11201 } 11202 11203 state = ddi_get_soft_state(dtrace_softstate, minor); 11204 state->dts_epid = DTRACE_EPIDNONE + 1; 11205 11206 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); 11207 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 11208 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 11209 11210 if (devp != NULL) { 11211 major = getemajor(*devp); 11212 } else { 11213 major = ddi_driver_major(dtrace_devi); 11214 } 11215 11216 state->dts_dev = makedevice(major, minor); 11217 11218 if (devp != NULL) 11219 *devp = state->dts_dev; 11220 11221 /* 11222 * We allocate NCPU buffers. On the one hand, this can be quite 11223 * a bit of memory per instance (nearly 36K on a Starcat). On the 11224 * other hand, it saves an additional memory reference in the probe 11225 * path. 11226 */ 11227 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 11228 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 11229 state->dts_cleaner = CYCLIC_NONE; 11230 state->dts_deadman = CYCLIC_NONE; 11231 state->dts_vstate.dtvs_state = state; 11232 11233 for (i = 0; i < DTRACEOPT_MAX; i++) 11234 state->dts_options[i] = DTRACEOPT_UNSET; 11235 11236 /* 11237 * Set the default options. 11238 */ 11239 opt = state->dts_options; 11240 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 11241 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 11242 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 11243 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 11244 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 11245 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 11246 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 11247 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 11248 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 11249 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 11250 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 11251 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 11252 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 11253 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 11254 11255 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 11256 11257 /* 11258 * Depending on the user credentials, we set flag bits which alter probe 11259 * visibility or the amount of destructiveness allowed. In the case of 11260 * actual anonymous tracing, or the possession of all privileges, all of 11261 * the normal checks are bypassed. 11262 */ 11263 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 11264 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 11265 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 11266 } else { 11267 /* 11268 * Set up the credentials for this instantiation. We take a 11269 * hold on the credential to prevent it from disappearing on 11270 * us; this in turn prevents the zone_t referenced by this 11271 * credential from disappearing. This means that we can 11272 * examine the credential and the zone from probe context. 11273 */ 11274 crhold(cr); 11275 state->dts_cred.dcr_cred = cr; 11276 11277 /* 11278 * CRA_PROC means "we have *some* privilege for dtrace" and 11279 * unlocks the use of variables like pid, zonename, etc. 11280 */ 11281 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 11282 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 11283 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 11284 } 11285 11286 /* 11287 * dtrace_user allows use of syscall and profile providers. 11288 * If the user also has proc_owner and/or proc_zone, we 11289 * extend the scope to include additional visibility and 11290 * destructive power. 11291 */ 11292 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 11293 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 11294 state->dts_cred.dcr_visible |= 11295 DTRACE_CRV_ALLPROC; 11296 11297 state->dts_cred.dcr_action |= 11298 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 11299 } 11300 11301 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 11302 state->dts_cred.dcr_visible |= 11303 DTRACE_CRV_ALLZONE; 11304 11305 state->dts_cred.dcr_action |= 11306 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 11307 } 11308 11309 /* 11310 * If we have all privs in whatever zone this is, 11311 * we can do destructive things to processes which 11312 * have altered credentials. 11313 */ 11314 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 11315 cr->cr_zone->zone_privset)) { 11316 state->dts_cred.dcr_action |= 11317 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 11318 } 11319 } 11320 11321 /* 11322 * Holding the dtrace_kernel privilege also implies that 11323 * the user has the dtrace_user privilege from a visibility 11324 * perspective. But without further privileges, some 11325 * destructive actions are not available. 11326 */ 11327 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 11328 /* 11329 * Make all probes in all zones visible. However, 11330 * this doesn't mean that all actions become available 11331 * to all zones. 11332 */ 11333 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 11334 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 11335 11336 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 11337 DTRACE_CRA_PROC; 11338 /* 11339 * Holding proc_owner means that destructive actions 11340 * for *this* zone are allowed. 11341 */ 11342 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 11343 state->dts_cred.dcr_action |= 11344 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 11345 11346 /* 11347 * Holding proc_zone means that destructive actions 11348 * for this user/group ID in all zones is allowed. 11349 */ 11350 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 11351 state->dts_cred.dcr_action |= 11352 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 11353 11354 /* 11355 * If we have all privs in whatever zone this is, 11356 * we can do destructive things to processes which 11357 * have altered credentials. 11358 */ 11359 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 11360 cr->cr_zone->zone_privset)) { 11361 state->dts_cred.dcr_action |= 11362 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 11363 } 11364 } 11365 11366 /* 11367 * Holding the dtrace_proc privilege gives control over fasttrap 11368 * and pid providers. We need to grant wider destructive 11369 * privileges in the event that the user has proc_owner and/or 11370 * proc_zone. 11371 */ 11372 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 11373 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 11374 state->dts_cred.dcr_action |= 11375 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 11376 11377 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 11378 state->dts_cred.dcr_action |= 11379 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 11380 } 11381 } 11382 11383 return (state); 11384 } 11385 11386 static int 11387 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 11388 { 11389 dtrace_optval_t *opt = state->dts_options, size; 11390 processorid_t cpu; 11391 int flags = 0, rval; 11392 11393 ASSERT(MUTEX_HELD(&dtrace_lock)); 11394 ASSERT(MUTEX_HELD(&cpu_lock)); 11395 ASSERT(which < DTRACEOPT_MAX); 11396 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 11397 (state == dtrace_anon.dta_state && 11398 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 11399 11400 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 11401 return (0); 11402 11403 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 11404 cpu = opt[DTRACEOPT_CPU]; 11405 11406 if (which == DTRACEOPT_SPECSIZE) 11407 flags |= DTRACEBUF_NOSWITCH; 11408 11409 if (which == DTRACEOPT_BUFSIZE) { 11410 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 11411 flags |= DTRACEBUF_RING; 11412 11413 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 11414 flags |= DTRACEBUF_FILL; 11415 11416 flags |= DTRACEBUF_INACTIVE; 11417 } 11418 11419 for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { 11420 /* 11421 * The size must be 8-byte aligned. If the size is not 8-byte 11422 * aligned, drop it down by the difference. 11423 */ 11424 if (size & (sizeof (uint64_t) - 1)) 11425 size -= size & (sizeof (uint64_t) - 1); 11426 11427 if (size < state->dts_reserve) { 11428 /* 11429 * Buffers always must be large enough to accommodate 11430 * their prereserved space. We return E2BIG instead 11431 * of ENOMEM in this case to allow for user-level 11432 * software to differentiate the cases. 11433 */ 11434 return (E2BIG); 11435 } 11436 11437 rval = dtrace_buffer_alloc(buf, size, flags, cpu); 11438 11439 if (rval != ENOMEM) { 11440 opt[which] = size; 11441 return (rval); 11442 } 11443 11444 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 11445 return (rval); 11446 } 11447 11448 return (ENOMEM); 11449 } 11450 11451 static int 11452 dtrace_state_buffers(dtrace_state_t *state) 11453 { 11454 dtrace_speculation_t *spec = state->dts_speculations; 11455 int rval, i; 11456 11457 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 11458 DTRACEOPT_BUFSIZE)) != 0) 11459 return (rval); 11460 11461 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 11462 DTRACEOPT_AGGSIZE)) != 0) 11463 return (rval); 11464 11465 for (i = 0; i < state->dts_nspeculations; i++) { 11466 if ((rval = dtrace_state_buffer(state, 11467 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 11468 return (rval); 11469 } 11470 11471 return (0); 11472 } 11473 11474 static void 11475 dtrace_state_prereserve(dtrace_state_t *state) 11476 { 11477 dtrace_ecb_t *ecb; 11478 dtrace_probe_t *probe; 11479 11480 state->dts_reserve = 0; 11481 11482 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 11483 return; 11484 11485 /* 11486 * If our buffer policy is a "fill" buffer policy, we need to set the 11487 * prereserved space to be the space required by the END probes. 11488 */ 11489 probe = dtrace_probes[dtrace_probeid_end - 1]; 11490 ASSERT(probe != NULL); 11491 11492 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 11493 if (ecb->dte_state != state) 11494 continue; 11495 11496 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 11497 } 11498 } 11499 11500 static int 11501 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 11502 { 11503 dtrace_optval_t *opt = state->dts_options, sz, nspec; 11504 dtrace_speculation_t *spec; 11505 dtrace_buffer_t *buf; 11506 cyc_handler_t hdlr; 11507 cyc_time_t when; 11508 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 11509 dtrace_icookie_t cookie; 11510 11511 mutex_enter(&cpu_lock); 11512 mutex_enter(&dtrace_lock); 11513 11514 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 11515 rval = EBUSY; 11516 goto out; 11517 } 11518 11519 /* 11520 * Before we can perform any checks, we must prime all of the 11521 * retained enablings that correspond to this state. 11522 */ 11523 dtrace_enabling_prime(state); 11524 11525 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 11526 rval = EACCES; 11527 goto out; 11528 } 11529 11530 dtrace_state_prereserve(state); 11531 11532 /* 11533 * Now we want to do is try to allocate our speculations. 11534 * We do not automatically resize the number of speculations; if 11535 * this fails, we will fail the operation. 11536 */ 11537 nspec = opt[DTRACEOPT_NSPEC]; 11538 ASSERT(nspec != DTRACEOPT_UNSET); 11539 11540 if (nspec > INT_MAX) { 11541 rval = ENOMEM; 11542 goto out; 11543 } 11544 11545 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); 11546 11547 if (spec == NULL) { 11548 rval = ENOMEM; 11549 goto out; 11550 } 11551 11552 state->dts_speculations = spec; 11553 state->dts_nspeculations = (int)nspec; 11554 11555 for (i = 0; i < nspec; i++) { 11556 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { 11557 rval = ENOMEM; 11558 goto err; 11559 } 11560 11561 spec[i].dtsp_buffer = buf; 11562 } 11563 11564 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 11565 if (dtrace_anon.dta_state == NULL) { 11566 rval = ENOENT; 11567 goto out; 11568 } 11569 11570 if (state->dts_necbs != 0) { 11571 rval = EALREADY; 11572 goto out; 11573 } 11574 11575 state->dts_anon = dtrace_anon_grab(); 11576 ASSERT(state->dts_anon != NULL); 11577 state = state->dts_anon; 11578 11579 /* 11580 * We want "grabanon" to be set in the grabbed state, so we'll 11581 * copy that option value from the grabbing state into the 11582 * grabbed state. 11583 */ 11584 state->dts_options[DTRACEOPT_GRABANON] = 11585 opt[DTRACEOPT_GRABANON]; 11586 11587 *cpu = dtrace_anon.dta_beganon; 11588 11589 /* 11590 * If the anonymous state is active (as it almost certainly 11591 * is if the anonymous enabling ultimately matched anything), 11592 * we don't allow any further option processing -- but we 11593 * don't return failure. 11594 */ 11595 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11596 goto out; 11597 } 11598 11599 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 11600 opt[DTRACEOPT_AGGSIZE] != 0) { 11601 if (state->dts_aggregations == NULL) { 11602 /* 11603 * We're not going to create an aggregation buffer 11604 * because we don't have any ECBs that contain 11605 * aggregations -- set this option to 0. 11606 */ 11607 opt[DTRACEOPT_AGGSIZE] = 0; 11608 } else { 11609 /* 11610 * If we have an aggregation buffer, we must also have 11611 * a buffer to use as scratch. 11612 */ 11613 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 11614 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 11615 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 11616 } 11617 } 11618 } 11619 11620 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 11621 opt[DTRACEOPT_SPECSIZE] != 0) { 11622 if (!state->dts_speculates) { 11623 /* 11624 * We're not going to create speculation buffers 11625 * because we don't have any ECBs that actually 11626 * speculate -- set the speculation size to 0. 11627 */ 11628 opt[DTRACEOPT_SPECSIZE] = 0; 11629 } 11630 } 11631 11632 /* 11633 * The bare minimum size for any buffer that we're actually going to 11634 * do anything to is sizeof (uint64_t). 11635 */ 11636 sz = sizeof (uint64_t); 11637 11638 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 11639 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 11640 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 11641 /* 11642 * A buffer size has been explicitly set to 0 (or to a size 11643 * that will be adjusted to 0) and we need the space -- we 11644 * need to return failure. We return ENOSPC to differentiate 11645 * it from failing to allocate a buffer due to failure to meet 11646 * the reserve (for which we return E2BIG). 11647 */ 11648 rval = ENOSPC; 11649 goto out; 11650 } 11651 11652 if ((rval = dtrace_state_buffers(state)) != 0) 11653 goto err; 11654 11655 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 11656 sz = dtrace_dstate_defsize; 11657 11658 do { 11659 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 11660 11661 if (rval == 0) 11662 break; 11663 11664 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 11665 goto err; 11666 } while (sz >>= 1); 11667 11668 opt[DTRACEOPT_DYNVARSIZE] = sz; 11669 11670 if (rval != 0) 11671 goto err; 11672 11673 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 11674 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 11675 11676 if (opt[DTRACEOPT_CLEANRATE] == 0) 11677 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 11678 11679 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 11680 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 11681 11682 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 11683 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 11684 11685 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 11686 hdlr.cyh_arg = state; 11687 hdlr.cyh_level = CY_LOW_LEVEL; 11688 11689 when.cyt_when = 0; 11690 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 11691 11692 state->dts_cleaner = cyclic_add(&hdlr, &when); 11693 11694 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 11695 hdlr.cyh_arg = state; 11696 hdlr.cyh_level = CY_LOW_LEVEL; 11697 11698 when.cyt_when = 0; 11699 when.cyt_interval = dtrace_deadman_interval; 11700 11701 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 11702 state->dts_deadman = cyclic_add(&hdlr, &when); 11703 11704 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 11705 11706 /* 11707 * Now it's time to actually fire the BEGIN probe. We need to disable 11708 * interrupts here both to record the CPU on which we fired the BEGIN 11709 * probe (the data from this CPU will be processed first at user 11710 * level) and to manually activate the buffer for this CPU. 11711 */ 11712 cookie = dtrace_interrupt_disable(); 11713 *cpu = CPU->cpu_id; 11714 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 11715 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 11716 11717 dtrace_probe(dtrace_probeid_begin, 11718 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 11719 dtrace_interrupt_enable(cookie); 11720 /* 11721 * We may have had an exit action from a BEGIN probe; only change our 11722 * state to ACTIVE if we're still in WARMUP. 11723 */ 11724 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 11725 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 11726 11727 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 11728 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 11729 11730 /* 11731 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 11732 * want each CPU to transition its principal buffer out of the 11733 * INACTIVE state. Doing this assures that no CPU will suddenly begin 11734 * processing an ECB halfway down a probe's ECB chain; all CPUs will 11735 * atomically transition from processing none of a state's ECBs to 11736 * processing all of them. 11737 */ 11738 dtrace_xcall(DTRACE_CPUALL, 11739 (dtrace_xcall_t)dtrace_buffer_activate, state); 11740 goto out; 11741 11742 err: 11743 dtrace_buffer_free(state->dts_buffer); 11744 dtrace_buffer_free(state->dts_aggbuffer); 11745 11746 if ((nspec = state->dts_nspeculations) == 0) { 11747 ASSERT(state->dts_speculations == NULL); 11748 goto out; 11749 } 11750 11751 spec = state->dts_speculations; 11752 ASSERT(spec != NULL); 11753 11754 for (i = 0; i < state->dts_nspeculations; i++) { 11755 if ((buf = spec[i].dtsp_buffer) == NULL) 11756 break; 11757 11758 dtrace_buffer_free(buf); 11759 kmem_free(buf, bufsize); 11760 } 11761 11762 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 11763 state->dts_nspeculations = 0; 11764 state->dts_speculations = NULL; 11765 11766 out: 11767 mutex_exit(&dtrace_lock); 11768 mutex_exit(&cpu_lock); 11769 11770 return (rval); 11771 } 11772 11773 static int 11774 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 11775 { 11776 dtrace_icookie_t cookie; 11777 11778 ASSERT(MUTEX_HELD(&dtrace_lock)); 11779 11780 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 11781 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 11782 return (EINVAL); 11783 11784 /* 11785 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 11786 * to be sure that every CPU has seen it. See below for the details 11787 * on why this is done. 11788 */ 11789 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 11790 dtrace_sync(); 11791 11792 /* 11793 * By this point, it is impossible for any CPU to be still processing 11794 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 11795 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 11796 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 11797 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 11798 * iff we're in the END probe. 11799 */ 11800 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 11801 dtrace_sync(); 11802 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 11803 11804 /* 11805 * Finally, we can release the reserve and call the END probe. We 11806 * disable interrupts across calling the END probe to allow us to 11807 * return the CPU on which we actually called the END probe. This 11808 * allows user-land to be sure that this CPU's principal buffer is 11809 * processed last. 11810 */ 11811 state->dts_reserve = 0; 11812 11813 cookie = dtrace_interrupt_disable(); 11814 *cpu = CPU->cpu_id; 11815 dtrace_probe(dtrace_probeid_end, 11816 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 11817 dtrace_interrupt_enable(cookie); 11818 11819 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 11820 dtrace_sync(); 11821 11822 return (0); 11823 } 11824 11825 static int 11826 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 11827 dtrace_optval_t val) 11828 { 11829 ASSERT(MUTEX_HELD(&dtrace_lock)); 11830 11831 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11832 return (EBUSY); 11833 11834 if (option >= DTRACEOPT_MAX) 11835 return (EINVAL); 11836 11837 if (option != DTRACEOPT_CPU && val < 0) 11838 return (EINVAL); 11839 11840 switch (option) { 11841 case DTRACEOPT_DESTRUCTIVE: 11842 if (dtrace_destructive_disallow) 11843 return (EACCES); 11844 11845 state->dts_cred.dcr_destructive = 1; 11846 break; 11847 11848 case DTRACEOPT_BUFSIZE: 11849 case DTRACEOPT_DYNVARSIZE: 11850 case DTRACEOPT_AGGSIZE: 11851 case DTRACEOPT_SPECSIZE: 11852 case DTRACEOPT_STRSIZE: 11853 if (val < 0) 11854 return (EINVAL); 11855 11856 if (val >= LONG_MAX) { 11857 /* 11858 * If this is an otherwise negative value, set it to 11859 * the highest multiple of 128m less than LONG_MAX. 11860 * Technically, we're adjusting the size without 11861 * regard to the buffer resizing policy, but in fact, 11862 * this has no effect -- if we set the buffer size to 11863 * ~LONG_MAX and the buffer policy is ultimately set to 11864 * be "manual", the buffer allocation is guaranteed to 11865 * fail, if only because the allocation requires two 11866 * buffers. (We set the the size to the highest 11867 * multiple of 128m because it ensures that the size 11868 * will remain a multiple of a megabyte when 11869 * repeatedly halved -- all the way down to 15m.) 11870 */ 11871 val = LONG_MAX - (1 << 27) + 1; 11872 } 11873 } 11874 11875 state->dts_options[option] = val; 11876 11877 return (0); 11878 } 11879 11880 static void 11881 dtrace_state_destroy(dtrace_state_t *state) 11882 { 11883 dtrace_ecb_t *ecb; 11884 dtrace_vstate_t *vstate = &state->dts_vstate; 11885 minor_t minor = getminor(state->dts_dev); 11886 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 11887 dtrace_speculation_t *spec = state->dts_speculations; 11888 int nspec = state->dts_nspeculations; 11889 uint32_t match; 11890 11891 ASSERT(MUTEX_HELD(&dtrace_lock)); 11892 ASSERT(MUTEX_HELD(&cpu_lock)); 11893 11894 /* 11895 * First, retract any retained enablings for this state. 11896 */ 11897 dtrace_enabling_retract(state); 11898 ASSERT(state->dts_nretained == 0); 11899 11900 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 11901 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 11902 /* 11903 * We have managed to come into dtrace_state_destroy() on a 11904 * hot enabling -- almost certainly because of a disorderly 11905 * shutdown of a consumer. (That is, a consumer that is 11906 * exiting without having called dtrace_stop().) In this case, 11907 * we're going to set our activity to be KILLED, and then 11908 * issue a sync to be sure that everyone is out of probe 11909 * context before we start blowing away ECBs. 11910 */ 11911 state->dts_activity = DTRACE_ACTIVITY_KILLED; 11912 dtrace_sync(); 11913 } 11914 11915 /* 11916 * Release the credential hold we took in dtrace_state_create(). 11917 */ 11918 if (state->dts_cred.dcr_cred != NULL) 11919 crfree(state->dts_cred.dcr_cred); 11920 11921 /* 11922 * Now we can safely disable and destroy any enabled probes. Because 11923 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 11924 * (especially if they're all enabled), we take two passes through the 11925 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 11926 * in the second we disable whatever is left over. 11927 */ 11928 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 11929 for (i = 0; i < state->dts_necbs; i++) { 11930 if ((ecb = state->dts_ecbs[i]) == NULL) 11931 continue; 11932 11933 if (match && ecb->dte_probe != NULL) { 11934 dtrace_probe_t *probe = ecb->dte_probe; 11935 dtrace_provider_t *prov = probe->dtpr_provider; 11936 11937 if (!(prov->dtpv_priv.dtpp_flags & match)) 11938 continue; 11939 } 11940 11941 dtrace_ecb_disable(ecb); 11942 dtrace_ecb_destroy(ecb); 11943 } 11944 11945 if (!match) 11946 break; 11947 } 11948 11949 /* 11950 * Before we free the buffers, perform one more sync to assure that 11951 * every CPU is out of probe context. 11952 */ 11953 dtrace_sync(); 11954 11955 dtrace_buffer_free(state->dts_buffer); 11956 dtrace_buffer_free(state->dts_aggbuffer); 11957 11958 for (i = 0; i < nspec; i++) 11959 dtrace_buffer_free(spec[i].dtsp_buffer); 11960 11961 if (state->dts_cleaner != CYCLIC_NONE) 11962 cyclic_remove(state->dts_cleaner); 11963 11964 if (state->dts_deadman != CYCLIC_NONE) 11965 cyclic_remove(state->dts_deadman); 11966 11967 dtrace_dstate_fini(&vstate->dtvs_dynvars); 11968 dtrace_vstate_fini(vstate); 11969 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 11970 11971 if (state->dts_aggregations != NULL) { 11972 #ifdef DEBUG 11973 for (i = 0; i < state->dts_naggregations; i++) 11974 ASSERT(state->dts_aggregations[i] == NULL); 11975 #endif 11976 ASSERT(state->dts_naggregations > 0); 11977 kmem_free(state->dts_aggregations, 11978 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 11979 } 11980 11981 kmem_free(state->dts_buffer, bufsize); 11982 kmem_free(state->dts_aggbuffer, bufsize); 11983 11984 for (i = 0; i < nspec; i++) 11985 kmem_free(spec[i].dtsp_buffer, bufsize); 11986 11987 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 11988 11989 dtrace_format_destroy(state); 11990 11991 vmem_destroy(state->dts_aggid_arena); 11992 ddi_soft_state_free(dtrace_softstate, minor); 11993 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 11994 } 11995 11996 /* 11997 * DTrace Anonymous Enabling Functions 11998 */ 11999 static dtrace_state_t * 12000 dtrace_anon_grab(void) 12001 { 12002 dtrace_state_t *state; 12003 12004 ASSERT(MUTEX_HELD(&dtrace_lock)); 12005 12006 if ((state = dtrace_anon.dta_state) == NULL) { 12007 ASSERT(dtrace_anon.dta_enabling == NULL); 12008 return (NULL); 12009 } 12010 12011 ASSERT(dtrace_anon.dta_enabling != NULL); 12012 ASSERT(dtrace_retained != NULL); 12013 12014 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 12015 dtrace_anon.dta_enabling = NULL; 12016 dtrace_anon.dta_state = NULL; 12017 12018 return (state); 12019 } 12020 12021 static void 12022 dtrace_anon_property(void) 12023 { 12024 int i, rv; 12025 dtrace_state_t *state; 12026 dof_hdr_t *dof; 12027 char c[32]; /* enough for "dof-data-" + digits */ 12028 12029 ASSERT(MUTEX_HELD(&dtrace_lock)); 12030 ASSERT(MUTEX_HELD(&cpu_lock)); 12031 12032 for (i = 0; ; i++) { 12033 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 12034 12035 dtrace_err_verbose = 1; 12036 12037 if ((dof = dtrace_dof_property(c)) == NULL) { 12038 dtrace_err_verbose = 0; 12039 break; 12040 } 12041 12042 /* 12043 * We want to create anonymous state, so we need to transition 12044 * the kernel debugger to indicate that DTrace is active. If 12045 * this fails (e.g. because the debugger has modified text in 12046 * some way), we won't continue with the processing. 12047 */ 12048 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 12049 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 12050 "enabling ignored."); 12051 dtrace_dof_destroy(dof); 12052 break; 12053 } 12054 12055 /* 12056 * If we haven't allocated an anonymous state, we'll do so now. 12057 */ 12058 if ((state = dtrace_anon.dta_state) == NULL) { 12059 state = dtrace_state_create(NULL, NULL); 12060 dtrace_anon.dta_state = state; 12061 12062 if (state == NULL) { 12063 /* 12064 * This basically shouldn't happen: the only 12065 * failure mode from dtrace_state_create() is a 12066 * failure of ddi_soft_state_zalloc() that 12067 * itself should never happen. Still, the 12068 * interface allows for a failure mode, and 12069 * we want to fail as gracefully as possible: 12070 * we'll emit an error message and cease 12071 * processing anonymous state in this case. 12072 */ 12073 cmn_err(CE_WARN, "failed to create " 12074 "anonymous state"); 12075 dtrace_dof_destroy(dof); 12076 break; 12077 } 12078 } 12079 12080 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 12081 &dtrace_anon.dta_enabling, 0, B_TRUE); 12082 12083 if (rv == 0) 12084 rv = dtrace_dof_options(dof, state); 12085 12086 dtrace_err_verbose = 0; 12087 dtrace_dof_destroy(dof); 12088 12089 if (rv != 0) { 12090 /* 12091 * This is malformed DOF; chuck any anonymous state 12092 * that we created. 12093 */ 12094 ASSERT(dtrace_anon.dta_enabling == NULL); 12095 dtrace_state_destroy(state); 12096 dtrace_anon.dta_state = NULL; 12097 break; 12098 } 12099 12100 ASSERT(dtrace_anon.dta_enabling != NULL); 12101 } 12102 12103 if (dtrace_anon.dta_enabling != NULL) { 12104 int rval; 12105 12106 /* 12107 * dtrace_enabling_retain() can only fail because we are 12108 * trying to retain more enablings than are allowed -- but 12109 * we only have one anonymous enabling, and we are guaranteed 12110 * to be allowed at least one retained enabling; we assert 12111 * that dtrace_enabling_retain() returns success. 12112 */ 12113 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 12114 ASSERT(rval == 0); 12115 12116 dtrace_enabling_dump(dtrace_anon.dta_enabling); 12117 } 12118 } 12119 12120 /* 12121 * DTrace Helper Functions 12122 */ 12123 static void 12124 dtrace_helper_trace(dtrace_helper_action_t *helper, 12125 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 12126 { 12127 uint32_t size, next, nnext, i; 12128 dtrace_helptrace_t *ent; 12129 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 12130 12131 if (!dtrace_helptrace_enabled) 12132 return; 12133 12134 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 12135 12136 /* 12137 * What would a tracing framework be without its own tracing 12138 * framework? (Well, a hell of a lot simpler, for starters...) 12139 */ 12140 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 12141 sizeof (uint64_t) - sizeof (uint64_t); 12142 12143 /* 12144 * Iterate until we can allocate a slot in the trace buffer. 12145 */ 12146 do { 12147 next = dtrace_helptrace_next; 12148 12149 if (next + size < dtrace_helptrace_bufsize) { 12150 nnext = next + size; 12151 } else { 12152 nnext = size; 12153 } 12154 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 12155 12156 /* 12157 * We have our slot; fill it in. 12158 */ 12159 if (nnext == size) 12160 next = 0; 12161 12162 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 12163 ent->dtht_helper = helper; 12164 ent->dtht_where = where; 12165 ent->dtht_nlocals = vstate->dtvs_nlocals; 12166 12167 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 12168 mstate->dtms_fltoffs : -1; 12169 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 12170 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 12171 12172 for (i = 0; i < vstate->dtvs_nlocals; i++) { 12173 dtrace_statvar_t *svar; 12174 12175 if ((svar = vstate->dtvs_locals[i]) == NULL) 12176 continue; 12177 12178 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 12179 ent->dtht_locals[i] = 12180 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; 12181 } 12182 } 12183 12184 static uint64_t 12185 dtrace_helper(int which, dtrace_mstate_t *mstate, 12186 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 12187 { 12188 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 12189 uint64_t sarg0 = mstate->dtms_arg[0]; 12190 uint64_t sarg1 = mstate->dtms_arg[1]; 12191 uint64_t rval; 12192 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 12193 dtrace_helper_action_t *helper; 12194 dtrace_vstate_t *vstate; 12195 dtrace_difo_t *pred; 12196 int i, trace = dtrace_helptrace_enabled; 12197 12198 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 12199 12200 if (helpers == NULL) 12201 return (0); 12202 12203 if ((helper = helpers->dthps_actions[which]) == NULL) 12204 return (0); 12205 12206 vstate = &helpers->dthps_vstate; 12207 mstate->dtms_arg[0] = arg0; 12208 mstate->dtms_arg[1] = arg1; 12209 12210 /* 12211 * Now iterate over each helper. If its predicate evaluates to 'true', 12212 * we'll call the corresponding actions. Note that the below calls 12213 * to dtrace_dif_emulate() may set faults in machine state. This is 12214 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 12215 * the stored DIF offset with its own (which is the desired behavior). 12216 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 12217 * from machine state; this is okay, too. 12218 */ 12219 for (; helper != NULL; helper = helper->dtha_next) { 12220 if ((pred = helper->dtha_predicate) != NULL) { 12221 if (trace) 12222 dtrace_helper_trace(helper, mstate, vstate, 0); 12223 12224 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 12225 goto next; 12226 12227 if (*flags & CPU_DTRACE_FAULT) 12228 goto err; 12229 } 12230 12231 for (i = 0; i < helper->dtha_nactions; i++) { 12232 if (trace) 12233 dtrace_helper_trace(helper, 12234 mstate, vstate, i + 1); 12235 12236 rval = dtrace_dif_emulate(helper->dtha_actions[i], 12237 mstate, vstate, state); 12238 12239 if (*flags & CPU_DTRACE_FAULT) 12240 goto err; 12241 } 12242 12243 next: 12244 if (trace) 12245 dtrace_helper_trace(helper, mstate, vstate, 12246 DTRACE_HELPTRACE_NEXT); 12247 } 12248 12249 if (trace) 12250 dtrace_helper_trace(helper, mstate, vstate, 12251 DTRACE_HELPTRACE_DONE); 12252 12253 /* 12254 * Restore the arg0 that we saved upon entry. 12255 */ 12256 mstate->dtms_arg[0] = sarg0; 12257 mstate->dtms_arg[1] = sarg1; 12258 12259 return (rval); 12260 12261 err: 12262 if (trace) 12263 dtrace_helper_trace(helper, mstate, vstate, 12264 DTRACE_HELPTRACE_ERR); 12265 12266 /* 12267 * Restore the arg0 that we saved upon entry. 12268 */ 12269 mstate->dtms_arg[0] = sarg0; 12270 mstate->dtms_arg[1] = sarg1; 12271 12272 return (NULL); 12273 } 12274 12275 static void 12276 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 12277 dtrace_vstate_t *vstate) 12278 { 12279 int i; 12280 12281 if (helper->dtha_predicate != NULL) 12282 dtrace_difo_release(helper->dtha_predicate, vstate); 12283 12284 for (i = 0; i < helper->dtha_nactions; i++) { 12285 ASSERT(helper->dtha_actions[i] != NULL); 12286 dtrace_difo_release(helper->dtha_actions[i], vstate); 12287 } 12288 12289 kmem_free(helper->dtha_actions, 12290 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 12291 kmem_free(helper, sizeof (dtrace_helper_action_t)); 12292 } 12293 12294 static int 12295 dtrace_helper_destroygen(int gen) 12296 { 12297 proc_t *p = curproc; 12298 dtrace_helpers_t *help = p->p_dtrace_helpers; 12299 dtrace_vstate_t *vstate; 12300 int i; 12301 12302 ASSERT(MUTEX_HELD(&dtrace_lock)); 12303 12304 if (help == NULL || gen > help->dthps_generation) 12305 return (EINVAL); 12306 12307 vstate = &help->dthps_vstate; 12308 12309 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12310 dtrace_helper_action_t *last = NULL, *h, *next; 12311 12312 for (h = help->dthps_actions[i]; h != NULL; h = next) { 12313 next = h->dtha_next; 12314 12315 if (h->dtha_generation == gen) { 12316 if (last != NULL) { 12317 last->dtha_next = next; 12318 } else { 12319 help->dthps_actions[i] = next; 12320 } 12321 12322 dtrace_helper_action_destroy(h, vstate); 12323 } else { 12324 last = h; 12325 } 12326 } 12327 } 12328 12329 /* 12330 * Interate until we've cleared out all helper providers with the 12331 * given generation number. 12332 */ 12333 for (;;) { 12334 dtrace_helper_provider_t *prov; 12335 12336 /* 12337 * Look for a helper provider with the right generation. 12338 */ 12339 for (i = 0; i < help->dthps_nprovs; i++) { 12340 prov = help->dthps_provs[i]; 12341 12342 if (prov->dthp_generation == gen) 12343 break; 12344 } 12345 12346 /* 12347 * If there were no matches, we're done. 12348 */ 12349 if (i == help->dthps_nprovs) 12350 break; 12351 12352 /* 12353 * Move the last helper provider into this slot. 12354 */ 12355 help->dthps_nprovs--; 12356 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 12357 help->dthps_provs[help->dthps_nprovs] = NULL; 12358 12359 mutex_exit(&dtrace_lock); 12360 12361 /* 12362 * If we have a meta provider, remove this helper provider. 12363 */ 12364 mutex_enter(&dtrace_meta_lock); 12365 if (dtrace_meta_pid != NULL) { 12366 ASSERT(dtrace_deferred_pid == NULL); 12367 dtrace_helper_provider_remove(&prov->dthp_prov, 12368 p->p_pid); 12369 } 12370 mutex_exit(&dtrace_meta_lock); 12371 12372 mutex_enter(&dtrace_lock); 12373 } 12374 12375 return (0); 12376 } 12377 12378 static int 12379 dtrace_helper_validate(dtrace_helper_action_t *helper) 12380 { 12381 int err = 0, i; 12382 dtrace_difo_t *dp; 12383 12384 if ((dp = helper->dtha_predicate) != NULL) 12385 err += dtrace_difo_validate_helper(dp); 12386 12387 for (i = 0; i < helper->dtha_nactions; i++) 12388 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 12389 12390 return (err == 0); 12391 } 12392 12393 static int 12394 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 12395 { 12396 dtrace_helpers_t *help; 12397 dtrace_helper_action_t *helper, *last; 12398 dtrace_actdesc_t *act; 12399 dtrace_vstate_t *vstate; 12400 dtrace_predicate_t *pred; 12401 int count = 0, nactions = 0, i; 12402 12403 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 12404 return (EINVAL); 12405 12406 help = curproc->p_dtrace_helpers; 12407 last = help->dthps_actions[which]; 12408 vstate = &help->dthps_vstate; 12409 12410 for (count = 0; last != NULL; last = last->dtha_next) { 12411 count++; 12412 if (last->dtha_next == NULL) 12413 break; 12414 } 12415 12416 /* 12417 * If we already have dtrace_helper_actions_max helper actions for this 12418 * helper action type, we'll refuse to add a new one. 12419 */ 12420 if (count >= dtrace_helper_actions_max) 12421 return (ENOSPC); 12422 12423 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 12424 helper->dtha_generation = help->dthps_generation; 12425 12426 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 12427 ASSERT(pred->dtp_difo != NULL); 12428 dtrace_difo_hold(pred->dtp_difo); 12429 helper->dtha_predicate = pred->dtp_difo; 12430 } 12431 12432 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 12433 if (act->dtad_kind != DTRACEACT_DIFEXPR) 12434 goto err; 12435 12436 if (act->dtad_difo == NULL) 12437 goto err; 12438 12439 nactions++; 12440 } 12441 12442 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 12443 (helper->dtha_nactions = nactions), KM_SLEEP); 12444 12445 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 12446 dtrace_difo_hold(act->dtad_difo); 12447 helper->dtha_actions[i++] = act->dtad_difo; 12448 } 12449 12450 if (!dtrace_helper_validate(helper)) 12451 goto err; 12452 12453 if (last == NULL) { 12454 help->dthps_actions[which] = helper; 12455 } else { 12456 last->dtha_next = helper; 12457 } 12458 12459 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 12460 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 12461 dtrace_helptrace_next = 0; 12462 } 12463 12464 return (0); 12465 err: 12466 dtrace_helper_action_destroy(helper, vstate); 12467 return (EINVAL); 12468 } 12469 12470 static void 12471 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 12472 dof_helper_t *dofhp) 12473 { 12474 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 12475 12476 mutex_enter(&dtrace_meta_lock); 12477 mutex_enter(&dtrace_lock); 12478 12479 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 12480 /* 12481 * If the dtrace module is loaded but not attached, or if 12482 * there aren't isn't a meta provider registered to deal with 12483 * these provider descriptions, we need to postpone creating 12484 * the actual providers until later. 12485 */ 12486 12487 if (help->dthps_next == NULL && help->dthps_prev == NULL && 12488 dtrace_deferred_pid != help) { 12489 help->dthps_deferred = 1; 12490 help->dthps_pid = p->p_pid; 12491 help->dthps_next = dtrace_deferred_pid; 12492 help->dthps_prev = NULL; 12493 if (dtrace_deferred_pid != NULL) 12494 dtrace_deferred_pid->dthps_prev = help; 12495 dtrace_deferred_pid = help; 12496 } 12497 12498 mutex_exit(&dtrace_lock); 12499 12500 } else if (dofhp != NULL) { 12501 /* 12502 * If the dtrace module is loaded and we have a particular 12503 * helper provider description, pass that off to the 12504 * meta provider. 12505 */ 12506 12507 mutex_exit(&dtrace_lock); 12508 12509 dtrace_helper_provide(dofhp, p->p_pid); 12510 12511 } else { 12512 /* 12513 * Otherwise, just pass all the helper provider descriptions 12514 * off to the meta provider. 12515 */ 12516 12517 int i; 12518 mutex_exit(&dtrace_lock); 12519 12520 for (i = 0; i < help->dthps_nprovs; i++) { 12521 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 12522 p->p_pid); 12523 } 12524 } 12525 12526 mutex_exit(&dtrace_meta_lock); 12527 } 12528 12529 static int 12530 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 12531 { 12532 dtrace_helpers_t *help; 12533 dtrace_helper_provider_t *hprov, **tmp_provs; 12534 uint_t tmp_maxprovs, i; 12535 12536 ASSERT(MUTEX_HELD(&dtrace_lock)); 12537 12538 help = curproc->p_dtrace_helpers; 12539 ASSERT(help != NULL); 12540 12541 /* 12542 * If we already have dtrace_helper_providers_max helper providers, 12543 * we're refuse to add a new one. 12544 */ 12545 if (help->dthps_nprovs >= dtrace_helper_providers_max) 12546 return (ENOSPC); 12547 12548 /* 12549 * Check to make sure this isn't a duplicate. 12550 */ 12551 for (i = 0; i < help->dthps_nprovs; i++) { 12552 if (dofhp->dofhp_addr == 12553 help->dthps_provs[i]->dthp_prov.dofhp_addr) 12554 return (EALREADY); 12555 } 12556 12557 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 12558 hprov->dthp_prov = *dofhp; 12559 hprov->dthp_ref = 1; 12560 hprov->dthp_generation = gen; 12561 12562 /* 12563 * Allocate a bigger table for helper providers if it's already full. 12564 */ 12565 if (help->dthps_maxprovs == help->dthps_nprovs) { 12566 tmp_maxprovs = help->dthps_maxprovs; 12567 tmp_provs = help->dthps_provs; 12568 12569 if (help->dthps_maxprovs == 0) 12570 help->dthps_maxprovs = 2; 12571 else 12572 help->dthps_maxprovs *= 2; 12573 if (help->dthps_maxprovs > dtrace_helper_providers_max) 12574 help->dthps_maxprovs = dtrace_helper_providers_max; 12575 12576 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 12577 12578 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 12579 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 12580 12581 if (tmp_provs != NULL) { 12582 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 12583 sizeof (dtrace_helper_provider_t *)); 12584 kmem_free(tmp_provs, tmp_maxprovs * 12585 sizeof (dtrace_helper_provider_t *)); 12586 } 12587 } 12588 12589 help->dthps_provs[help->dthps_nprovs] = hprov; 12590 help->dthps_nprovs++; 12591 12592 return (0); 12593 } 12594 12595 static void 12596 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 12597 { 12598 mutex_enter(&dtrace_lock); 12599 12600 if (--hprov->dthp_ref == 0) { 12601 dof_hdr_t *dof; 12602 mutex_exit(&dtrace_lock); 12603 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 12604 dtrace_dof_destroy(dof); 12605 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 12606 } else { 12607 mutex_exit(&dtrace_lock); 12608 } 12609 } 12610 12611 static int 12612 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 12613 { 12614 uintptr_t daddr = (uintptr_t)dof; 12615 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 12616 dof_provider_t *provider; 12617 dof_probe_t *probe; 12618 uint8_t *arg; 12619 char *strtab, *typestr; 12620 dof_stridx_t typeidx; 12621 size_t typesz; 12622 uint_t nprobes, j, k; 12623 12624 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 12625 12626 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 12627 dtrace_dof_error(dof, "misaligned section offset"); 12628 return (-1); 12629 } 12630 12631 /* 12632 * The section needs to be large enough to contain the DOF provider 12633 * structure appropriate for the given version. 12634 */ 12635 if (sec->dofs_size < 12636 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 12637 offsetof(dof_provider_t, dofpv_prenoffs) : 12638 sizeof (dof_provider_t))) { 12639 dtrace_dof_error(dof, "provider section too small"); 12640 return (-1); 12641 } 12642 12643 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 12644 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 12645 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 12646 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 12647 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 12648 12649 if (str_sec == NULL || prb_sec == NULL || 12650 arg_sec == NULL || off_sec == NULL) 12651 return (-1); 12652 12653 enoff_sec = NULL; 12654 12655 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 12656 provider->dofpv_prenoffs != DOF_SECT_NONE && 12657 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 12658 provider->dofpv_prenoffs)) == NULL) 12659 return (-1); 12660 12661 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 12662 12663 if (provider->dofpv_name >= str_sec->dofs_size || 12664 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 12665 dtrace_dof_error(dof, "invalid provider name"); 12666 return (-1); 12667 } 12668 12669 if (prb_sec->dofs_entsize == 0 || 12670 prb_sec->dofs_entsize > prb_sec->dofs_size) { 12671 dtrace_dof_error(dof, "invalid entry size"); 12672 return (-1); 12673 } 12674 12675 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 12676 dtrace_dof_error(dof, "misaligned entry size"); 12677 return (-1); 12678 } 12679 12680 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 12681 dtrace_dof_error(dof, "invalid entry size"); 12682 return (-1); 12683 } 12684 12685 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 12686 dtrace_dof_error(dof, "misaligned section offset"); 12687 return (-1); 12688 } 12689 12690 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 12691 dtrace_dof_error(dof, "invalid entry size"); 12692 return (-1); 12693 } 12694 12695 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 12696 12697 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 12698 12699 /* 12700 * Take a pass through the probes to check for errors. 12701 */ 12702 for (j = 0; j < nprobes; j++) { 12703 probe = (dof_probe_t *)(uintptr_t)(daddr + 12704 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 12705 12706 if (probe->dofpr_func >= str_sec->dofs_size) { 12707 dtrace_dof_error(dof, "invalid function name"); 12708 return (-1); 12709 } 12710 12711 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 12712 dtrace_dof_error(dof, "function name too long"); 12713 return (-1); 12714 } 12715 12716 if (probe->dofpr_name >= str_sec->dofs_size || 12717 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 12718 dtrace_dof_error(dof, "invalid probe name"); 12719 return (-1); 12720 } 12721 12722 /* 12723 * The offset count must not wrap the index, and the offsets 12724 * must also not overflow the section's data. 12725 */ 12726 if (probe->dofpr_offidx + probe->dofpr_noffs < 12727 probe->dofpr_offidx || 12728 (probe->dofpr_offidx + probe->dofpr_noffs) * 12729 off_sec->dofs_entsize > off_sec->dofs_size) { 12730 dtrace_dof_error(dof, "invalid probe offset"); 12731 return (-1); 12732 } 12733 12734 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 12735 /* 12736 * If there's no is-enabled offset section, make sure 12737 * there aren't any is-enabled offsets. Otherwise 12738 * perform the same checks as for probe offsets 12739 * (immediately above). 12740 */ 12741 if (enoff_sec == NULL) { 12742 if (probe->dofpr_enoffidx != 0 || 12743 probe->dofpr_nenoffs != 0) { 12744 dtrace_dof_error(dof, "is-enabled " 12745 "offsets with null section"); 12746 return (-1); 12747 } 12748 } else if (probe->dofpr_enoffidx + 12749 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 12750 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 12751 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 12752 dtrace_dof_error(dof, "invalid is-enabled " 12753 "offset"); 12754 return (-1); 12755 } 12756 12757 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 12758 dtrace_dof_error(dof, "zero probe and " 12759 "is-enabled offsets"); 12760 return (-1); 12761 } 12762 } else if (probe->dofpr_noffs == 0) { 12763 dtrace_dof_error(dof, "zero probe offsets"); 12764 return (-1); 12765 } 12766 12767 if (probe->dofpr_argidx + probe->dofpr_xargc < 12768 probe->dofpr_argidx || 12769 (probe->dofpr_argidx + probe->dofpr_xargc) * 12770 arg_sec->dofs_entsize > arg_sec->dofs_size) { 12771 dtrace_dof_error(dof, "invalid args"); 12772 return (-1); 12773 } 12774 12775 typeidx = probe->dofpr_nargv; 12776 typestr = strtab + probe->dofpr_nargv; 12777 for (k = 0; k < probe->dofpr_nargc; k++) { 12778 if (typeidx >= str_sec->dofs_size) { 12779 dtrace_dof_error(dof, "bad " 12780 "native argument type"); 12781 return (-1); 12782 } 12783 12784 typesz = strlen(typestr) + 1; 12785 if (typesz > DTRACE_ARGTYPELEN) { 12786 dtrace_dof_error(dof, "native " 12787 "argument type too long"); 12788 return (-1); 12789 } 12790 typeidx += typesz; 12791 typestr += typesz; 12792 } 12793 12794 typeidx = probe->dofpr_xargv; 12795 typestr = strtab + probe->dofpr_xargv; 12796 for (k = 0; k < probe->dofpr_xargc; k++) { 12797 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 12798 dtrace_dof_error(dof, "bad " 12799 "native argument index"); 12800 return (-1); 12801 } 12802 12803 if (typeidx >= str_sec->dofs_size) { 12804 dtrace_dof_error(dof, "bad " 12805 "translated argument type"); 12806 return (-1); 12807 } 12808 12809 typesz = strlen(typestr) + 1; 12810 if (typesz > DTRACE_ARGTYPELEN) { 12811 dtrace_dof_error(dof, "translated argument " 12812 "type too long"); 12813 return (-1); 12814 } 12815 12816 typeidx += typesz; 12817 typestr += typesz; 12818 } 12819 } 12820 12821 return (0); 12822 } 12823 12824 static int 12825 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 12826 { 12827 dtrace_helpers_t *help; 12828 dtrace_vstate_t *vstate; 12829 dtrace_enabling_t *enab = NULL; 12830 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 12831 uintptr_t daddr = (uintptr_t)dof; 12832 12833 ASSERT(MUTEX_HELD(&dtrace_lock)); 12834 12835 if ((help = curproc->p_dtrace_helpers) == NULL) 12836 help = dtrace_helpers_create(curproc); 12837 12838 vstate = &help->dthps_vstate; 12839 12840 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 12841 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 12842 dtrace_dof_destroy(dof); 12843 return (rv); 12844 } 12845 12846 /* 12847 * Look for helper providers and validate their descriptions. 12848 */ 12849 if (dhp != NULL) { 12850 for (i = 0; i < dof->dofh_secnum; i++) { 12851 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 12852 dof->dofh_secoff + i * dof->dofh_secsize); 12853 12854 if (sec->dofs_type != DOF_SECT_PROVIDER) 12855 continue; 12856 12857 if (dtrace_helper_provider_validate(dof, sec) != 0) { 12858 dtrace_enabling_destroy(enab); 12859 dtrace_dof_destroy(dof); 12860 return (-1); 12861 } 12862 12863 nprovs++; 12864 } 12865 } 12866 12867 /* 12868 * Now we need to walk through the ECB descriptions in the enabling. 12869 */ 12870 for (i = 0; i < enab->dten_ndesc; i++) { 12871 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12872 dtrace_probedesc_t *desc = &ep->dted_probe; 12873 12874 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 12875 continue; 12876 12877 if (strcmp(desc->dtpd_mod, "helper") != 0) 12878 continue; 12879 12880 if (strcmp(desc->dtpd_func, "ustack") != 0) 12881 continue; 12882 12883 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 12884 ep)) != 0) { 12885 /* 12886 * Adding this helper action failed -- we are now going 12887 * to rip out the entire generation and return failure. 12888 */ 12889 (void) dtrace_helper_destroygen(help->dthps_generation); 12890 dtrace_enabling_destroy(enab); 12891 dtrace_dof_destroy(dof); 12892 return (-1); 12893 } 12894 12895 nhelpers++; 12896 } 12897 12898 if (nhelpers < enab->dten_ndesc) 12899 dtrace_dof_error(dof, "unmatched helpers"); 12900 12901 gen = help->dthps_generation++; 12902 dtrace_enabling_destroy(enab); 12903 12904 if (dhp != NULL && nprovs > 0) { 12905 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 12906 if (dtrace_helper_provider_add(dhp, gen) == 0) { 12907 mutex_exit(&dtrace_lock); 12908 dtrace_helper_provider_register(curproc, help, dhp); 12909 mutex_enter(&dtrace_lock); 12910 12911 destroy = 0; 12912 } 12913 } 12914 12915 if (destroy) 12916 dtrace_dof_destroy(dof); 12917 12918 return (gen); 12919 } 12920 12921 static dtrace_helpers_t * 12922 dtrace_helpers_create(proc_t *p) 12923 { 12924 dtrace_helpers_t *help; 12925 12926 ASSERT(MUTEX_HELD(&dtrace_lock)); 12927 ASSERT(p->p_dtrace_helpers == NULL); 12928 12929 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 12930 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 12931 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 12932 12933 p->p_dtrace_helpers = help; 12934 dtrace_helpers++; 12935 12936 return (help); 12937 } 12938 12939 static void 12940 dtrace_helpers_destroy(void) 12941 { 12942 dtrace_helpers_t *help; 12943 dtrace_vstate_t *vstate; 12944 proc_t *p = curproc; 12945 int i; 12946 12947 mutex_enter(&dtrace_lock); 12948 12949 ASSERT(p->p_dtrace_helpers != NULL); 12950 ASSERT(dtrace_helpers > 0); 12951 12952 help = p->p_dtrace_helpers; 12953 vstate = &help->dthps_vstate; 12954 12955 /* 12956 * We're now going to lose the help from this process. 12957 */ 12958 p->p_dtrace_helpers = NULL; 12959 dtrace_sync(); 12960 12961 /* 12962 * Destory the helper actions. 12963 */ 12964 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 12965 dtrace_helper_action_t *h, *next; 12966 12967 for (h = help->dthps_actions[i]; h != NULL; h = next) { 12968 next = h->dtha_next; 12969 dtrace_helper_action_destroy(h, vstate); 12970 h = next; 12971 } 12972 } 12973 12974 mutex_exit(&dtrace_lock); 12975 12976 /* 12977 * Destroy the helper providers. 12978 */ 12979 if (help->dthps_maxprovs > 0) { 12980 mutex_enter(&dtrace_meta_lock); 12981 if (dtrace_meta_pid != NULL) { 12982 ASSERT(dtrace_deferred_pid == NULL); 12983 12984 for (i = 0; i < help->dthps_nprovs; i++) { 12985 dtrace_helper_provider_remove( 12986 &help->dthps_provs[i]->dthp_prov, p->p_pid); 12987 } 12988 } else { 12989 mutex_enter(&dtrace_lock); 12990 ASSERT(help->dthps_deferred == 0 || 12991 help->dthps_next != NULL || 12992 help->dthps_prev != NULL || 12993 help == dtrace_deferred_pid); 12994 12995 /* 12996 * Remove the helper from the deferred list. 12997 */ 12998 if (help->dthps_next != NULL) 12999 help->dthps_next->dthps_prev = help->dthps_prev; 13000 if (help->dthps_prev != NULL) 13001 help->dthps_prev->dthps_next = help->dthps_next; 13002 if (dtrace_deferred_pid == help) { 13003 dtrace_deferred_pid = help->dthps_next; 13004 ASSERT(help->dthps_prev == NULL); 13005 } 13006 13007 mutex_exit(&dtrace_lock); 13008 } 13009 13010 mutex_exit(&dtrace_meta_lock); 13011 13012 for (i = 0; i < help->dthps_nprovs; i++) { 13013 dtrace_helper_provider_destroy(help->dthps_provs[i]); 13014 } 13015 13016 kmem_free(help->dthps_provs, help->dthps_maxprovs * 13017 sizeof (dtrace_helper_provider_t *)); 13018 } 13019 13020 mutex_enter(&dtrace_lock); 13021 13022 dtrace_vstate_fini(&help->dthps_vstate); 13023 kmem_free(help->dthps_actions, 13024 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 13025 kmem_free(help, sizeof (dtrace_helpers_t)); 13026 13027 --dtrace_helpers; 13028 mutex_exit(&dtrace_lock); 13029 } 13030 13031 static void 13032 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 13033 { 13034 dtrace_helpers_t *help, *newhelp; 13035 dtrace_helper_action_t *helper, *new, *last; 13036 dtrace_difo_t *dp; 13037 dtrace_vstate_t *vstate; 13038 int i, j, sz, hasprovs = 0; 13039 13040 mutex_enter(&dtrace_lock); 13041 ASSERT(from->p_dtrace_helpers != NULL); 13042 ASSERT(dtrace_helpers > 0); 13043 13044 help = from->p_dtrace_helpers; 13045 newhelp = dtrace_helpers_create(to); 13046 ASSERT(to->p_dtrace_helpers != NULL); 13047 13048 newhelp->dthps_generation = help->dthps_generation; 13049 vstate = &newhelp->dthps_vstate; 13050 13051 /* 13052 * Duplicate the helper actions. 13053 */ 13054 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 13055 if ((helper = help->dthps_actions[i]) == NULL) 13056 continue; 13057 13058 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 13059 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 13060 KM_SLEEP); 13061 new->dtha_generation = helper->dtha_generation; 13062 13063 if ((dp = helper->dtha_predicate) != NULL) { 13064 dp = dtrace_difo_duplicate(dp, vstate); 13065 new->dtha_predicate = dp; 13066 } 13067 13068 new->dtha_nactions = helper->dtha_nactions; 13069 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 13070 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 13071 13072 for (j = 0; j < new->dtha_nactions; j++) { 13073 dtrace_difo_t *dp = helper->dtha_actions[j]; 13074 13075 ASSERT(dp != NULL); 13076 dp = dtrace_difo_duplicate(dp, vstate); 13077 new->dtha_actions[j] = dp; 13078 } 13079 13080 if (last != NULL) { 13081 last->dtha_next = new; 13082 } else { 13083 newhelp->dthps_actions[i] = new; 13084 } 13085 13086 last = new; 13087 } 13088 } 13089 13090 /* 13091 * Duplicate the helper providers and register them with the 13092 * DTrace framework. 13093 */ 13094 if (help->dthps_nprovs > 0) { 13095 newhelp->dthps_nprovs = help->dthps_nprovs; 13096 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 13097 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 13098 for (i = 0; i < newhelp->dthps_nprovs; i++) { 13099 newhelp->dthps_provs[i] = help->dthps_provs[i]; 13100 newhelp->dthps_provs[i]->dthp_ref++; 13101 } 13102 13103 hasprovs = 1; 13104 } 13105 13106 mutex_exit(&dtrace_lock); 13107 13108 if (hasprovs) 13109 dtrace_helper_provider_register(to, newhelp, NULL); 13110 } 13111 13112 /* 13113 * DTrace Hook Functions 13114 */ 13115 static void 13116 dtrace_module_loaded(struct modctl *ctl) 13117 { 13118 dtrace_provider_t *prv; 13119 13120 mutex_enter(&dtrace_provider_lock); 13121 mutex_enter(&mod_lock); 13122 13123 ASSERT(ctl->mod_busy); 13124 13125 /* 13126 * We're going to call each providers per-module provide operation 13127 * specifying only this module. 13128 */ 13129 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 13130 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 13131 13132 mutex_exit(&mod_lock); 13133 mutex_exit(&dtrace_provider_lock); 13134 13135 /* 13136 * If we have any retained enablings, we need to match against them. 13137 * Enabling probes requires that cpu_lock be held, and we cannot hold 13138 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 13139 * module. (In particular, this happens when loading scheduling 13140 * classes.) So if we have any retained enablings, we need to dispatch 13141 * our task queue to do the match for us. 13142 */ 13143 mutex_enter(&dtrace_lock); 13144 13145 if (dtrace_retained == NULL) { 13146 mutex_exit(&dtrace_lock); 13147 return; 13148 } 13149 13150 (void) taskq_dispatch(dtrace_taskq, 13151 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 13152 13153 mutex_exit(&dtrace_lock); 13154 13155 /* 13156 * And now, for a little heuristic sleaze: in general, we want to 13157 * match modules as soon as they load. However, we cannot guarantee 13158 * this, because it would lead us to the lock ordering violation 13159 * outlined above. The common case, of course, is that cpu_lock is 13160 * _not_ held -- so we delay here for a clock tick, hoping that that's 13161 * long enough for the task queue to do its work. If it's not, it's 13162 * not a serious problem -- it just means that the module that we 13163 * just loaded may not be immediately instrumentable. 13164 */ 13165 delay(1); 13166 } 13167 13168 static void 13169 dtrace_module_unloaded(struct modctl *ctl) 13170 { 13171 dtrace_probe_t template, *probe, *first, *next; 13172 dtrace_provider_t *prov; 13173 13174 template.dtpr_mod = ctl->mod_modname; 13175 13176 mutex_enter(&dtrace_provider_lock); 13177 mutex_enter(&mod_lock); 13178 mutex_enter(&dtrace_lock); 13179 13180 if (dtrace_bymod == NULL) { 13181 /* 13182 * The DTrace module is loaded (obviously) but not attached; 13183 * we don't have any work to do. 13184 */ 13185 mutex_exit(&dtrace_provider_lock); 13186 mutex_exit(&mod_lock); 13187 mutex_exit(&dtrace_lock); 13188 return; 13189 } 13190 13191 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 13192 probe != NULL; probe = probe->dtpr_nextmod) { 13193 if (probe->dtpr_ecb != NULL) { 13194 mutex_exit(&dtrace_provider_lock); 13195 mutex_exit(&mod_lock); 13196 mutex_exit(&dtrace_lock); 13197 13198 /* 13199 * This shouldn't _actually_ be possible -- we're 13200 * unloading a module that has an enabled probe in it. 13201 * (It's normally up to the provider to make sure that 13202 * this can't happen.) However, because dtps_enable() 13203 * doesn't have a failure mode, there can be an 13204 * enable/unload race. Upshot: we don't want to 13205 * assert, but we're not going to disable the 13206 * probe, either. 13207 */ 13208 if (dtrace_err_verbose) { 13209 cmn_err(CE_WARN, "unloaded module '%s' had " 13210 "enabled probes", ctl->mod_modname); 13211 } 13212 13213 return; 13214 } 13215 } 13216 13217 probe = first; 13218 13219 for (first = NULL; probe != NULL; probe = next) { 13220 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 13221 13222 dtrace_probes[probe->dtpr_id - 1] = NULL; 13223 13224 next = probe->dtpr_nextmod; 13225 dtrace_hash_remove(dtrace_bymod, probe); 13226 dtrace_hash_remove(dtrace_byfunc, probe); 13227 dtrace_hash_remove(dtrace_byname, probe); 13228 13229 if (first == NULL) { 13230 first = probe; 13231 probe->dtpr_nextmod = NULL; 13232 } else { 13233 probe->dtpr_nextmod = first; 13234 first = probe; 13235 } 13236 } 13237 13238 /* 13239 * We've removed all of the module's probes from the hash chains and 13240 * from the probe array. Now issue a dtrace_sync() to be sure that 13241 * everyone has cleared out from any probe array processing. 13242 */ 13243 dtrace_sync(); 13244 13245 for (probe = first; probe != NULL; probe = first) { 13246 first = probe->dtpr_nextmod; 13247 prov = probe->dtpr_provider; 13248 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 13249 probe->dtpr_arg); 13250 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 13251 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 13252 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 13253 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 13254 kmem_free(probe, sizeof (dtrace_probe_t)); 13255 } 13256 13257 mutex_exit(&dtrace_lock); 13258 mutex_exit(&mod_lock); 13259 mutex_exit(&dtrace_provider_lock); 13260 } 13261 13262 void 13263 dtrace_suspend(void) 13264 { 13265 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 13266 } 13267 13268 void 13269 dtrace_resume(void) 13270 { 13271 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 13272 } 13273 13274 static int 13275 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 13276 { 13277 ASSERT(MUTEX_HELD(&cpu_lock)); 13278 mutex_enter(&dtrace_lock); 13279 13280 switch (what) { 13281 case CPU_CONFIG: { 13282 dtrace_state_t *state; 13283 dtrace_optval_t *opt, rs, c; 13284 13285 /* 13286 * For now, we only allocate a new buffer for anonymous state. 13287 */ 13288 if ((state = dtrace_anon.dta_state) == NULL) 13289 break; 13290 13291 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 13292 break; 13293 13294 opt = state->dts_options; 13295 c = opt[DTRACEOPT_CPU]; 13296 13297 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 13298 break; 13299 13300 /* 13301 * Regardless of what the actual policy is, we're going to 13302 * temporarily set our resize policy to be manual. We're 13303 * also going to temporarily set our CPU option to denote 13304 * the newly configured CPU. 13305 */ 13306 rs = opt[DTRACEOPT_BUFRESIZE]; 13307 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 13308 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 13309 13310 (void) dtrace_state_buffers(state); 13311 13312 opt[DTRACEOPT_BUFRESIZE] = rs; 13313 opt[DTRACEOPT_CPU] = c; 13314 13315 break; 13316 } 13317 13318 case CPU_UNCONFIG: 13319 /* 13320 * We don't free the buffer in the CPU_UNCONFIG case. (The 13321 * buffer will be freed when the consumer exits.) 13322 */ 13323 break; 13324 13325 default: 13326 break; 13327 } 13328 13329 mutex_exit(&dtrace_lock); 13330 return (0); 13331 } 13332 13333 static void 13334 dtrace_cpu_setup_initial(processorid_t cpu) 13335 { 13336 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 13337 } 13338 13339 static void 13340 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 13341 { 13342 if (dtrace_toxranges >= dtrace_toxranges_max) { 13343 int osize, nsize; 13344 dtrace_toxrange_t *range; 13345 13346 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 13347 13348 if (osize == 0) { 13349 ASSERT(dtrace_toxrange == NULL); 13350 ASSERT(dtrace_toxranges_max == 0); 13351 dtrace_toxranges_max = 1; 13352 } else { 13353 dtrace_toxranges_max <<= 1; 13354 } 13355 13356 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 13357 range = kmem_zalloc(nsize, KM_SLEEP); 13358 13359 if (dtrace_toxrange != NULL) { 13360 ASSERT(osize != 0); 13361 bcopy(dtrace_toxrange, range, osize); 13362 kmem_free(dtrace_toxrange, osize); 13363 } 13364 13365 dtrace_toxrange = range; 13366 } 13367 13368 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); 13369 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); 13370 13371 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 13372 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 13373 dtrace_toxranges++; 13374 } 13375 13376 /* 13377 * DTrace Driver Cookbook Functions 13378 */ 13379 /*ARGSUSED*/ 13380 static int 13381 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 13382 { 13383 dtrace_provider_id_t id; 13384 dtrace_state_t *state = NULL; 13385 dtrace_enabling_t *enab; 13386 13387 mutex_enter(&cpu_lock); 13388 mutex_enter(&dtrace_provider_lock); 13389 mutex_enter(&dtrace_lock); 13390 13391 if (ddi_soft_state_init(&dtrace_softstate, 13392 sizeof (dtrace_state_t), 0) != 0) { 13393 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 13394 mutex_exit(&cpu_lock); 13395 mutex_exit(&dtrace_provider_lock); 13396 mutex_exit(&dtrace_lock); 13397 return (DDI_FAILURE); 13398 } 13399 13400 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 13401 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 13402 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 13403 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 13404 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 13405 ddi_remove_minor_node(devi, NULL); 13406 ddi_soft_state_fini(&dtrace_softstate); 13407 mutex_exit(&cpu_lock); 13408 mutex_exit(&dtrace_provider_lock); 13409 mutex_exit(&dtrace_lock); 13410 return (DDI_FAILURE); 13411 } 13412 13413 ddi_report_dev(devi); 13414 dtrace_devi = devi; 13415 13416 dtrace_modload = dtrace_module_loaded; 13417 dtrace_modunload = dtrace_module_unloaded; 13418 dtrace_cpu_init = dtrace_cpu_setup_initial; 13419 dtrace_helpers_cleanup = dtrace_helpers_destroy; 13420 dtrace_helpers_fork = dtrace_helpers_duplicate; 13421 dtrace_cpustart_init = dtrace_suspend; 13422 dtrace_cpustart_fini = dtrace_resume; 13423 dtrace_debugger_init = dtrace_suspend; 13424 dtrace_debugger_fini = dtrace_resume; 13425 dtrace_kreloc_init = dtrace_suspend; 13426 dtrace_kreloc_fini = dtrace_resume; 13427 13428 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 13429 13430 ASSERT(MUTEX_HELD(&cpu_lock)); 13431 13432 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 13433 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 13434 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 13435 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 13436 VM_SLEEP | VMC_IDENTIFIER); 13437 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 13438 1, INT_MAX, 0); 13439 13440 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 13441 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 13442 NULL, NULL, NULL, NULL, NULL, 0); 13443 13444 ASSERT(MUTEX_HELD(&cpu_lock)); 13445 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 13446 offsetof(dtrace_probe_t, dtpr_nextmod), 13447 offsetof(dtrace_probe_t, dtpr_prevmod)); 13448 13449 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 13450 offsetof(dtrace_probe_t, dtpr_nextfunc), 13451 offsetof(dtrace_probe_t, dtpr_prevfunc)); 13452 13453 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 13454 offsetof(dtrace_probe_t, dtpr_nextname), 13455 offsetof(dtrace_probe_t, dtpr_prevname)); 13456 13457 if (dtrace_retain_max < 1) { 13458 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 13459 "setting to 1", dtrace_retain_max); 13460 dtrace_retain_max = 1; 13461 } 13462 13463 /* 13464 * Now discover our toxic ranges. 13465 */ 13466 dtrace_toxic_ranges(dtrace_toxrange_add); 13467 13468 /* 13469 * Before we register ourselves as a provider to our own framework, 13470 * we would like to assert that dtrace_provider is NULL -- but that's 13471 * not true if we were loaded as a dependency of a DTrace provider. 13472 * Once we've registered, we can assert that dtrace_provider is our 13473 * pseudo provider. 13474 */ 13475 (void) dtrace_register("dtrace", &dtrace_provider_attr, 13476 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 13477 13478 ASSERT(dtrace_provider != NULL); 13479 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 13480 13481 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 13482 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 13483 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 13484 dtrace_provider, NULL, NULL, "END", 0, NULL); 13485 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 13486 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 13487 13488 dtrace_anon_property(); 13489 mutex_exit(&cpu_lock); 13490 13491 /* 13492 * If DTrace helper tracing is enabled, we need to allocate the 13493 * trace buffer and initialize the values. 13494 */ 13495 if (dtrace_helptrace_enabled) { 13496 ASSERT(dtrace_helptrace_buffer == NULL); 13497 dtrace_helptrace_buffer = 13498 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 13499 dtrace_helptrace_next = 0; 13500 } 13501 13502 /* 13503 * If there are already providers, we must ask them to provide their 13504 * probes, and then match any anonymous enabling against them. Note 13505 * that there should be no other retained enablings at this time: 13506 * the only retained enablings at this time should be the anonymous 13507 * enabling. 13508 */ 13509 if (dtrace_anon.dta_enabling != NULL) { 13510 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 13511 13512 dtrace_enabling_provide(NULL); 13513 state = dtrace_anon.dta_state; 13514 13515 /* 13516 * We couldn't hold cpu_lock across the above call to 13517 * dtrace_enabling_provide(), but we must hold it to actually 13518 * enable the probes. We have to drop all of our locks, pick 13519 * up cpu_lock, and regain our locks before matching the 13520 * retained anonymous enabling. 13521 */ 13522 mutex_exit(&dtrace_lock); 13523 mutex_exit(&dtrace_provider_lock); 13524 13525 mutex_enter(&cpu_lock); 13526 mutex_enter(&dtrace_provider_lock); 13527 mutex_enter(&dtrace_lock); 13528 13529 if ((enab = dtrace_anon.dta_enabling) != NULL) 13530 (void) dtrace_enabling_match(enab, NULL); 13531 13532 mutex_exit(&cpu_lock); 13533 } 13534 13535 mutex_exit(&dtrace_lock); 13536 mutex_exit(&dtrace_provider_lock); 13537 13538 if (state != NULL) { 13539 /* 13540 * If we created any anonymous state, set it going now. 13541 */ 13542 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 13543 } 13544 13545 return (DDI_SUCCESS); 13546 } 13547 13548 /*ARGSUSED*/ 13549 static int 13550 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 13551 { 13552 dtrace_state_t *state; 13553 uint32_t priv; 13554 uid_t uid; 13555 zoneid_t zoneid; 13556 13557 if (getminor(*devp) == DTRACEMNRN_HELPER) 13558 return (0); 13559 13560 /* 13561 * If this wasn't an open with the "helper" minor, then it must be 13562 * the "dtrace" minor. 13563 */ 13564 ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE); 13565 13566 /* 13567 * If no DTRACE_PRIV_* bits are set in the credential, then the 13568 * caller lacks sufficient permission to do anything with DTrace. 13569 */ 13570 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 13571 if (priv == DTRACE_PRIV_NONE) 13572 return (EACCES); 13573 13574 /* 13575 * Ask all providers to provide all their probes. 13576 */ 13577 mutex_enter(&dtrace_provider_lock); 13578 dtrace_probe_provide(NULL, NULL); 13579 mutex_exit(&dtrace_provider_lock); 13580 13581 mutex_enter(&cpu_lock); 13582 mutex_enter(&dtrace_lock); 13583 dtrace_opens++; 13584 dtrace_membar_producer(); 13585 13586 /* 13587 * If the kernel debugger is active (that is, if the kernel debugger 13588 * modified text in some way), we won't allow the open. 13589 */ 13590 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13591 dtrace_opens--; 13592 mutex_exit(&cpu_lock); 13593 mutex_exit(&dtrace_lock); 13594 return (EBUSY); 13595 } 13596 13597 state = dtrace_state_create(devp, cred_p); 13598 mutex_exit(&cpu_lock); 13599 13600 if (state == NULL) { 13601 if (--dtrace_opens == 0) 13602 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13603 mutex_exit(&dtrace_lock); 13604 return (EAGAIN); 13605 } 13606 13607 mutex_exit(&dtrace_lock); 13608 13609 return (0); 13610 } 13611 13612 /*ARGSUSED*/ 13613 static int 13614 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 13615 { 13616 minor_t minor = getminor(dev); 13617 dtrace_state_t *state; 13618 13619 if (minor == DTRACEMNRN_HELPER) 13620 return (0); 13621 13622 state = ddi_get_soft_state(dtrace_softstate, minor); 13623 13624 mutex_enter(&cpu_lock); 13625 mutex_enter(&dtrace_lock); 13626 13627 if (state->dts_anon) { 13628 /* 13629 * There is anonymous state. Destroy that first. 13630 */ 13631 ASSERT(dtrace_anon.dta_state == NULL); 13632 dtrace_state_destroy(state->dts_anon); 13633 } 13634 13635 dtrace_state_destroy(state); 13636 ASSERT(dtrace_opens > 0); 13637 if (--dtrace_opens == 0) 13638 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 13639 13640 mutex_exit(&dtrace_lock); 13641 mutex_exit(&cpu_lock); 13642 13643 return (0); 13644 } 13645 13646 /*ARGSUSED*/ 13647 static int 13648 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 13649 { 13650 int rval; 13651 dof_helper_t help, *dhp = NULL; 13652 13653 switch (cmd) { 13654 case DTRACEHIOC_ADDDOF: 13655 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 13656 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 13657 return (EFAULT); 13658 } 13659 13660 dhp = &help; 13661 arg = (intptr_t)help.dofhp_dof; 13662 /*FALLTHROUGH*/ 13663 13664 case DTRACEHIOC_ADD: { 13665 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 13666 13667 if (dof == NULL) 13668 return (rval); 13669 13670 mutex_enter(&dtrace_lock); 13671 13672 /* 13673 * dtrace_helper_slurp() takes responsibility for the dof -- 13674 * it may free it now or it may save it and free it later. 13675 */ 13676 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 13677 *rv = rval; 13678 rval = 0; 13679 } else { 13680 rval = EINVAL; 13681 } 13682 13683 mutex_exit(&dtrace_lock); 13684 return (rval); 13685 } 13686 13687 case DTRACEHIOC_REMOVE: { 13688 mutex_enter(&dtrace_lock); 13689 rval = dtrace_helper_destroygen(arg); 13690 mutex_exit(&dtrace_lock); 13691 13692 return (rval); 13693 } 13694 13695 default: 13696 break; 13697 } 13698 13699 return (ENOTTY); 13700 } 13701 13702 /*ARGSUSED*/ 13703 static int 13704 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 13705 { 13706 minor_t minor = getminor(dev); 13707 dtrace_state_t *state; 13708 int rval; 13709 13710 if (minor == DTRACEMNRN_HELPER) 13711 return (dtrace_ioctl_helper(cmd, arg, rv)); 13712 13713 state = ddi_get_soft_state(dtrace_softstate, minor); 13714 13715 if (state->dts_anon) { 13716 ASSERT(dtrace_anon.dta_state == NULL); 13717 state = state->dts_anon; 13718 } 13719 13720 switch (cmd) { 13721 case DTRACEIOC_PROVIDER: { 13722 dtrace_providerdesc_t pvd; 13723 dtrace_provider_t *pvp; 13724 13725 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 13726 return (EFAULT); 13727 13728 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 13729 mutex_enter(&dtrace_provider_lock); 13730 13731 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 13732 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 13733 break; 13734 } 13735 13736 mutex_exit(&dtrace_provider_lock); 13737 13738 if (pvp == NULL) 13739 return (ESRCH); 13740 13741 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 13742 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 13743 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 13744 return (EFAULT); 13745 13746 return (0); 13747 } 13748 13749 case DTRACEIOC_EPROBE: { 13750 dtrace_eprobedesc_t epdesc; 13751 dtrace_ecb_t *ecb; 13752 dtrace_action_t *act; 13753 void *buf; 13754 size_t size; 13755 uintptr_t dest; 13756 int nrecs; 13757 13758 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 13759 return (EFAULT); 13760 13761 mutex_enter(&dtrace_lock); 13762 13763 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 13764 mutex_exit(&dtrace_lock); 13765 return (EINVAL); 13766 } 13767 13768 if (ecb->dte_probe == NULL) { 13769 mutex_exit(&dtrace_lock); 13770 return (EINVAL); 13771 } 13772 13773 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 13774 epdesc.dtepd_uarg = ecb->dte_uarg; 13775 epdesc.dtepd_size = ecb->dte_size; 13776 13777 nrecs = epdesc.dtepd_nrecs; 13778 epdesc.dtepd_nrecs = 0; 13779 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 13780 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 13781 continue; 13782 13783 epdesc.dtepd_nrecs++; 13784 } 13785 13786 /* 13787 * Now that we have the size, we need to allocate a temporary 13788 * buffer in which to store the complete description. We need 13789 * the temporary buffer to be able to drop dtrace_lock() 13790 * across the copyout(), below. 13791 */ 13792 size = sizeof (dtrace_eprobedesc_t) + 13793 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 13794 13795 buf = kmem_alloc(size, KM_SLEEP); 13796 dest = (uintptr_t)buf; 13797 13798 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 13799 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 13800 13801 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 13802 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 13803 continue; 13804 13805 if (nrecs-- == 0) 13806 break; 13807 13808 bcopy(&act->dta_rec, (void *)dest, 13809 sizeof (dtrace_recdesc_t)); 13810 dest += sizeof (dtrace_recdesc_t); 13811 } 13812 13813 mutex_exit(&dtrace_lock); 13814 13815 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 13816 kmem_free(buf, size); 13817 return (EFAULT); 13818 } 13819 13820 kmem_free(buf, size); 13821 return (0); 13822 } 13823 13824 case DTRACEIOC_AGGDESC: { 13825 dtrace_aggdesc_t aggdesc; 13826 dtrace_action_t *act; 13827 dtrace_aggregation_t *agg; 13828 int nrecs; 13829 uint32_t offs; 13830 dtrace_recdesc_t *lrec; 13831 void *buf; 13832 size_t size; 13833 uintptr_t dest; 13834 13835 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 13836 return (EFAULT); 13837 13838 mutex_enter(&dtrace_lock); 13839 13840 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 13841 mutex_exit(&dtrace_lock); 13842 return (EINVAL); 13843 } 13844 13845 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 13846 13847 nrecs = aggdesc.dtagd_nrecs; 13848 aggdesc.dtagd_nrecs = 0; 13849 13850 offs = agg->dtag_base; 13851 lrec = &agg->dtag_action.dta_rec; 13852 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 13853 13854 for (act = agg->dtag_first; ; act = act->dta_next) { 13855 ASSERT(act->dta_intuple || 13856 DTRACEACT_ISAGG(act->dta_kind)); 13857 13858 /* 13859 * If this action has a record size of zero, it 13860 * denotes an argument to the aggregating action. 13861 * Because the presence of this record doesn't (or 13862 * shouldn't) affect the way the data is interpreted, 13863 * we don't copy it out to save user-level the 13864 * confusion of dealing with a zero-length record. 13865 */ 13866 if (act->dta_rec.dtrd_size == 0) { 13867 ASSERT(agg->dtag_hasarg); 13868 continue; 13869 } 13870 13871 aggdesc.dtagd_nrecs++; 13872 13873 if (act == &agg->dtag_action) 13874 break; 13875 } 13876 13877 /* 13878 * Now that we have the size, we need to allocate a temporary 13879 * buffer in which to store the complete description. We need 13880 * the temporary buffer to be able to drop dtrace_lock() 13881 * across the copyout(), below. 13882 */ 13883 size = sizeof (dtrace_aggdesc_t) + 13884 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 13885 13886 buf = kmem_alloc(size, KM_SLEEP); 13887 dest = (uintptr_t)buf; 13888 13889 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 13890 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 13891 13892 for (act = agg->dtag_first; ; act = act->dta_next) { 13893 dtrace_recdesc_t rec = act->dta_rec; 13894 13895 /* 13896 * See the comment in the above loop for why we pass 13897 * over zero-length records. 13898 */ 13899 if (rec.dtrd_size == 0) { 13900 ASSERT(agg->dtag_hasarg); 13901 continue; 13902 } 13903 13904 if (nrecs-- == 0) 13905 break; 13906 13907 rec.dtrd_offset -= offs; 13908 bcopy(&rec, (void *)dest, sizeof (rec)); 13909 dest += sizeof (dtrace_recdesc_t); 13910 13911 if (act == &agg->dtag_action) 13912 break; 13913 } 13914 13915 mutex_exit(&dtrace_lock); 13916 13917 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 13918 kmem_free(buf, size); 13919 return (EFAULT); 13920 } 13921 13922 kmem_free(buf, size); 13923 return (0); 13924 } 13925 13926 case DTRACEIOC_ENABLE: { 13927 dof_hdr_t *dof; 13928 dtrace_enabling_t *enab = NULL; 13929 dtrace_vstate_t *vstate; 13930 int err = 0; 13931 13932 *rv = 0; 13933 13934 /* 13935 * If a NULL argument has been passed, we take this as our 13936 * cue to reevaluate our enablings. 13937 */ 13938 if (arg == NULL) { 13939 mutex_enter(&cpu_lock); 13940 mutex_enter(&dtrace_lock); 13941 err = dtrace_enabling_matchstate(state, rv); 13942 mutex_exit(&dtrace_lock); 13943 mutex_exit(&cpu_lock); 13944 13945 return (err); 13946 } 13947 13948 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 13949 return (rval); 13950 13951 mutex_enter(&cpu_lock); 13952 mutex_enter(&dtrace_lock); 13953 vstate = &state->dts_vstate; 13954 13955 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13956 mutex_exit(&dtrace_lock); 13957 mutex_exit(&cpu_lock); 13958 dtrace_dof_destroy(dof); 13959 return (EBUSY); 13960 } 13961 13962 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 13963 mutex_exit(&dtrace_lock); 13964 mutex_exit(&cpu_lock); 13965 dtrace_dof_destroy(dof); 13966 return (EINVAL); 13967 } 13968 13969 if ((rval = dtrace_dof_options(dof, state)) != 0) { 13970 dtrace_enabling_destroy(enab); 13971 mutex_exit(&dtrace_lock); 13972 mutex_exit(&cpu_lock); 13973 dtrace_dof_destroy(dof); 13974 return (rval); 13975 } 13976 13977 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 13978 err = dtrace_enabling_retain(enab); 13979 } else { 13980 dtrace_enabling_destroy(enab); 13981 } 13982 13983 mutex_exit(&cpu_lock); 13984 mutex_exit(&dtrace_lock); 13985 dtrace_dof_destroy(dof); 13986 13987 return (err); 13988 } 13989 13990 case DTRACEIOC_REPLICATE: { 13991 dtrace_repldesc_t desc; 13992 dtrace_probedesc_t *match = &desc.dtrpd_match; 13993 dtrace_probedesc_t *create = &desc.dtrpd_create; 13994 int err; 13995 13996 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 13997 return (EFAULT); 13998 13999 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 14000 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 14001 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 14002 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 14003 14004 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 14005 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 14006 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 14007 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 14008 14009 mutex_enter(&dtrace_lock); 14010 err = dtrace_enabling_replicate(state, match, create); 14011 mutex_exit(&dtrace_lock); 14012 14013 return (err); 14014 } 14015 14016 case DTRACEIOC_PROBEMATCH: 14017 case DTRACEIOC_PROBES: { 14018 dtrace_probe_t *probe = NULL; 14019 dtrace_probedesc_t desc; 14020 dtrace_probekey_t pkey; 14021 dtrace_id_t i; 14022 int m = 0; 14023 uint32_t priv; 14024 uid_t uid; 14025 zoneid_t zoneid; 14026 14027 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 14028 return (EFAULT); 14029 14030 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 14031 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 14032 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 14033 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 14034 14035 /* 14036 * Before we attempt to match this probe, we want to give 14037 * all providers the opportunity to provide it. 14038 */ 14039 if (desc.dtpd_id == DTRACE_IDNONE) { 14040 mutex_enter(&dtrace_provider_lock); 14041 dtrace_probe_provide(&desc, NULL); 14042 mutex_exit(&dtrace_provider_lock); 14043 desc.dtpd_id++; 14044 } 14045 14046 if (cmd == DTRACEIOC_PROBEMATCH) { 14047 dtrace_probekey(&desc, &pkey); 14048 pkey.dtpk_id = DTRACE_IDNONE; 14049 } 14050 14051 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 14052 14053 mutex_enter(&dtrace_lock); 14054 14055 if (cmd == DTRACEIOC_PROBEMATCH) { 14056 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 14057 if ((probe = dtrace_probes[i - 1]) != NULL && 14058 (m = dtrace_match_probe(probe, &pkey, 14059 priv, uid, zoneid)) != 0) 14060 break; 14061 } 14062 14063 if (m < 0) { 14064 mutex_exit(&dtrace_lock); 14065 return (EINVAL); 14066 } 14067 14068 } else { 14069 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 14070 if ((probe = dtrace_probes[i - 1]) != NULL && 14071 dtrace_match_priv(probe, priv, uid, zoneid)) 14072 break; 14073 } 14074 } 14075 14076 if (probe == NULL) { 14077 mutex_exit(&dtrace_lock); 14078 return (ESRCH); 14079 } 14080 14081 dtrace_probe_description(probe, &desc); 14082 mutex_exit(&dtrace_lock); 14083 14084 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 14085 return (EFAULT); 14086 14087 return (0); 14088 } 14089 14090 case DTRACEIOC_PROBEARG: { 14091 dtrace_argdesc_t desc; 14092 dtrace_probe_t *probe; 14093 dtrace_provider_t *prov; 14094 14095 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 14096 return (EFAULT); 14097 14098 if (desc.dtargd_id == DTRACE_IDNONE) 14099 return (EINVAL); 14100 14101 if (desc.dtargd_ndx == DTRACE_ARGNONE) 14102 return (EINVAL); 14103 14104 mutex_enter(&dtrace_provider_lock); 14105 mutex_enter(&mod_lock); 14106 mutex_enter(&dtrace_lock); 14107 14108 if (desc.dtargd_id > dtrace_nprobes) { 14109 mutex_exit(&dtrace_lock); 14110 mutex_exit(&mod_lock); 14111 mutex_exit(&dtrace_provider_lock); 14112 return (EINVAL); 14113 } 14114 14115 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 14116 mutex_exit(&dtrace_lock); 14117 mutex_exit(&mod_lock); 14118 mutex_exit(&dtrace_provider_lock); 14119 return (EINVAL); 14120 } 14121 14122 mutex_exit(&dtrace_lock); 14123 14124 prov = probe->dtpr_provider; 14125 14126 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 14127 /* 14128 * There isn't any typed information for this probe. 14129 * Set the argument number to DTRACE_ARGNONE. 14130 */ 14131 desc.dtargd_ndx = DTRACE_ARGNONE; 14132 } else { 14133 desc.dtargd_native[0] = '\0'; 14134 desc.dtargd_xlate[0] = '\0'; 14135 desc.dtargd_mapping = desc.dtargd_ndx; 14136 14137 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 14138 probe->dtpr_id, probe->dtpr_arg, &desc); 14139 } 14140 14141 mutex_exit(&mod_lock); 14142 mutex_exit(&dtrace_provider_lock); 14143 14144 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 14145 return (EFAULT); 14146 14147 return (0); 14148 } 14149 14150 case DTRACEIOC_GO: { 14151 processorid_t cpuid; 14152 rval = dtrace_state_go(state, &cpuid); 14153 14154 if (rval != 0) 14155 return (rval); 14156 14157 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 14158 return (EFAULT); 14159 14160 return (0); 14161 } 14162 14163 case DTRACEIOC_STOP: { 14164 processorid_t cpuid; 14165 14166 mutex_enter(&dtrace_lock); 14167 rval = dtrace_state_stop(state, &cpuid); 14168 mutex_exit(&dtrace_lock); 14169 14170 if (rval != 0) 14171 return (rval); 14172 14173 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 14174 return (EFAULT); 14175 14176 return (0); 14177 } 14178 14179 case DTRACEIOC_DOFGET: { 14180 dof_hdr_t hdr, *dof; 14181 uint64_t len; 14182 14183 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 14184 return (EFAULT); 14185 14186 mutex_enter(&dtrace_lock); 14187 dof = dtrace_dof_create(state); 14188 mutex_exit(&dtrace_lock); 14189 14190 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 14191 rval = copyout(dof, (void *)arg, len); 14192 dtrace_dof_destroy(dof); 14193 14194 return (rval == 0 ? 0 : EFAULT); 14195 } 14196 14197 case DTRACEIOC_AGGSNAP: 14198 case DTRACEIOC_BUFSNAP: { 14199 dtrace_bufdesc_t desc; 14200 caddr_t cached; 14201 dtrace_buffer_t *buf; 14202 14203 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 14204 return (EFAULT); 14205 14206 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 14207 return (EINVAL); 14208 14209 mutex_enter(&dtrace_lock); 14210 14211 if (cmd == DTRACEIOC_BUFSNAP) { 14212 buf = &state->dts_buffer[desc.dtbd_cpu]; 14213 } else { 14214 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 14215 } 14216 14217 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 14218 size_t sz = buf->dtb_offset; 14219 14220 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 14221 mutex_exit(&dtrace_lock); 14222 return (EBUSY); 14223 } 14224 14225 /* 14226 * If this buffer has already been consumed, we're 14227 * going to indicate that there's nothing left here 14228 * to consume. 14229 */ 14230 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 14231 mutex_exit(&dtrace_lock); 14232 14233 desc.dtbd_size = 0; 14234 desc.dtbd_drops = 0; 14235 desc.dtbd_errors = 0; 14236 desc.dtbd_oldest = 0; 14237 sz = sizeof (desc); 14238 14239 if (copyout(&desc, (void *)arg, sz) != 0) 14240 return (EFAULT); 14241 14242 return (0); 14243 } 14244 14245 /* 14246 * If this is a ring buffer that has wrapped, we want 14247 * to copy the whole thing out. 14248 */ 14249 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 14250 dtrace_buffer_polish(buf); 14251 sz = buf->dtb_size; 14252 } 14253 14254 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 14255 mutex_exit(&dtrace_lock); 14256 return (EFAULT); 14257 } 14258 14259 desc.dtbd_size = sz; 14260 desc.dtbd_drops = buf->dtb_drops; 14261 desc.dtbd_errors = buf->dtb_errors; 14262 desc.dtbd_oldest = buf->dtb_xamot_offset; 14263 14264 mutex_exit(&dtrace_lock); 14265 14266 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 14267 return (EFAULT); 14268 14269 buf->dtb_flags |= DTRACEBUF_CONSUMED; 14270 14271 return (0); 14272 } 14273 14274 if (buf->dtb_tomax == NULL) { 14275 ASSERT(buf->dtb_xamot == NULL); 14276 mutex_exit(&dtrace_lock); 14277 return (ENOENT); 14278 } 14279 14280 cached = buf->dtb_tomax; 14281 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 14282 14283 dtrace_xcall(desc.dtbd_cpu, 14284 (dtrace_xcall_t)dtrace_buffer_switch, buf); 14285 14286 state->dts_errors += buf->dtb_xamot_errors; 14287 14288 /* 14289 * If the buffers did not actually switch, then the cross call 14290 * did not take place -- presumably because the given CPU is 14291 * not in the ready set. If this is the case, we'll return 14292 * ENOENT. 14293 */ 14294 if (buf->dtb_tomax == cached) { 14295 ASSERT(buf->dtb_xamot != cached); 14296 mutex_exit(&dtrace_lock); 14297 return (ENOENT); 14298 } 14299 14300 ASSERT(cached == buf->dtb_xamot); 14301 14302 /* 14303 * We have our snapshot; now copy it out. 14304 */ 14305 if (copyout(buf->dtb_xamot, desc.dtbd_data, 14306 buf->dtb_xamot_offset) != 0) { 14307 mutex_exit(&dtrace_lock); 14308 return (EFAULT); 14309 } 14310 14311 desc.dtbd_size = buf->dtb_xamot_offset; 14312 desc.dtbd_drops = buf->dtb_xamot_drops; 14313 desc.dtbd_errors = buf->dtb_xamot_errors; 14314 desc.dtbd_oldest = 0; 14315 14316 mutex_exit(&dtrace_lock); 14317 14318 /* 14319 * Finally, copy out the buffer description. 14320 */ 14321 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 14322 return (EFAULT); 14323 14324 return (0); 14325 } 14326 14327 case DTRACEIOC_CONF: { 14328 dtrace_conf_t conf; 14329 14330 bzero(&conf, sizeof (conf)); 14331 conf.dtc_difversion = DIF_VERSION; 14332 conf.dtc_difintregs = DIF_DIR_NREGS; 14333 conf.dtc_diftupregs = DIF_DTR_NREGS; 14334 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 14335 14336 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 14337 return (EFAULT); 14338 14339 return (0); 14340 } 14341 14342 case DTRACEIOC_STATUS: { 14343 dtrace_status_t stat; 14344 dtrace_dstate_t *dstate; 14345 int i, j; 14346 uint64_t nerrs; 14347 14348 /* 14349 * See the comment in dtrace_state_deadman() for the reason 14350 * for setting dts_laststatus to INT64_MAX before setting 14351 * it to the correct value. 14352 */ 14353 state->dts_laststatus = INT64_MAX; 14354 dtrace_membar_producer(); 14355 state->dts_laststatus = dtrace_gethrtime(); 14356 14357 bzero(&stat, sizeof (stat)); 14358 14359 mutex_enter(&dtrace_lock); 14360 14361 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 14362 mutex_exit(&dtrace_lock); 14363 return (ENOENT); 14364 } 14365 14366 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 14367 stat.dtst_exiting = 1; 14368 14369 nerrs = state->dts_errors; 14370 dstate = &state->dts_vstate.dtvs_dynvars; 14371 14372 for (i = 0; i < NCPU; i++) { 14373 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 14374 14375 stat.dtst_dyndrops += dcpu->dtdsc_drops; 14376 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 14377 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 14378 14379 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 14380 stat.dtst_filled++; 14381 14382 nerrs += state->dts_buffer[i].dtb_errors; 14383 14384 for (j = 0; j < state->dts_nspeculations; j++) { 14385 dtrace_speculation_t *spec; 14386 dtrace_buffer_t *buf; 14387 14388 spec = &state->dts_speculations[j]; 14389 buf = &spec->dtsp_buffer[i]; 14390 stat.dtst_specdrops += buf->dtb_xamot_drops; 14391 } 14392 } 14393 14394 stat.dtst_specdrops_busy = state->dts_speculations_busy; 14395 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 14396 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 14397 stat.dtst_dblerrors = state->dts_dblerrors; 14398 stat.dtst_killed = 14399 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 14400 stat.dtst_errors = nerrs; 14401 14402 mutex_exit(&dtrace_lock); 14403 14404 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 14405 return (EFAULT); 14406 14407 return (0); 14408 } 14409 14410 case DTRACEIOC_FORMAT: { 14411 dtrace_fmtdesc_t fmt; 14412 char *str; 14413 int len; 14414 14415 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 14416 return (EFAULT); 14417 14418 mutex_enter(&dtrace_lock); 14419 14420 if (fmt.dtfd_format == 0 || 14421 fmt.dtfd_format > state->dts_nformats) { 14422 mutex_exit(&dtrace_lock); 14423 return (EINVAL); 14424 } 14425 14426 /* 14427 * Format strings are allocated contiguously and they are 14428 * never freed; if a format index is less than the number 14429 * of formats, we can assert that the format map is non-NULL 14430 * and that the format for the specified index is non-NULL. 14431 */ 14432 ASSERT(state->dts_formats != NULL); 14433 str = state->dts_formats[fmt.dtfd_format - 1]; 14434 ASSERT(str != NULL); 14435 14436 len = strlen(str) + 1; 14437 14438 if (len > fmt.dtfd_length) { 14439 fmt.dtfd_length = len; 14440 14441 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 14442 mutex_exit(&dtrace_lock); 14443 return (EINVAL); 14444 } 14445 } else { 14446 if (copyout(str, fmt.dtfd_string, len) != 0) { 14447 mutex_exit(&dtrace_lock); 14448 return (EINVAL); 14449 } 14450 } 14451 14452 mutex_exit(&dtrace_lock); 14453 return (0); 14454 } 14455 14456 default: 14457 break; 14458 } 14459 14460 return (ENOTTY); 14461 } 14462 14463 /*ARGSUSED*/ 14464 static int 14465 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 14466 { 14467 dtrace_state_t *state; 14468 14469 switch (cmd) { 14470 case DDI_DETACH: 14471 break; 14472 14473 case DDI_SUSPEND: 14474 return (DDI_SUCCESS); 14475 14476 default: 14477 return (DDI_FAILURE); 14478 } 14479 14480 mutex_enter(&cpu_lock); 14481 mutex_enter(&dtrace_provider_lock); 14482 mutex_enter(&dtrace_lock); 14483 14484 ASSERT(dtrace_opens == 0); 14485 14486 if (dtrace_helpers > 0) { 14487 mutex_exit(&dtrace_provider_lock); 14488 mutex_exit(&dtrace_lock); 14489 mutex_exit(&cpu_lock); 14490 return (DDI_FAILURE); 14491 } 14492 14493 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 14494 mutex_exit(&dtrace_provider_lock); 14495 mutex_exit(&dtrace_lock); 14496 mutex_exit(&cpu_lock); 14497 return (DDI_FAILURE); 14498 } 14499 14500 dtrace_provider = NULL; 14501 14502 if ((state = dtrace_anon_grab()) != NULL) { 14503 /* 14504 * If there were ECBs on this state, the provider should 14505 * have not been allowed to detach; assert that there is 14506 * none. 14507 */ 14508 ASSERT(state->dts_necbs == 0); 14509 dtrace_state_destroy(state); 14510 14511 /* 14512 * If we're being detached with anonymous state, we need to 14513 * indicate to the kernel debugger that DTrace is now inactive. 14514 */ 14515 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 14516 } 14517 14518 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 14519 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 14520 dtrace_cpu_init = NULL; 14521 dtrace_helpers_cleanup = NULL; 14522 dtrace_helpers_fork = NULL; 14523 dtrace_cpustart_init = NULL; 14524 dtrace_cpustart_fini = NULL; 14525 dtrace_debugger_init = NULL; 14526 dtrace_debugger_fini = NULL; 14527 dtrace_kreloc_init = NULL; 14528 dtrace_kreloc_fini = NULL; 14529 dtrace_modload = NULL; 14530 dtrace_modunload = NULL; 14531 14532 mutex_exit(&cpu_lock); 14533 14534 if (dtrace_helptrace_enabled) { 14535 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 14536 dtrace_helptrace_buffer = NULL; 14537 } 14538 14539 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 14540 dtrace_probes = NULL; 14541 dtrace_nprobes = 0; 14542 14543 dtrace_hash_destroy(dtrace_bymod); 14544 dtrace_hash_destroy(dtrace_byfunc); 14545 dtrace_hash_destroy(dtrace_byname); 14546 dtrace_bymod = NULL; 14547 dtrace_byfunc = NULL; 14548 dtrace_byname = NULL; 14549 14550 kmem_cache_destroy(dtrace_state_cache); 14551 vmem_destroy(dtrace_minor); 14552 vmem_destroy(dtrace_arena); 14553 14554 if (dtrace_toxrange != NULL) { 14555 kmem_free(dtrace_toxrange, 14556 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 14557 dtrace_toxrange = NULL; 14558 dtrace_toxranges = 0; 14559 dtrace_toxranges_max = 0; 14560 } 14561 14562 ddi_remove_minor_node(dtrace_devi, NULL); 14563 dtrace_devi = NULL; 14564 14565 ddi_soft_state_fini(&dtrace_softstate); 14566 14567 ASSERT(dtrace_vtime_references == 0); 14568 ASSERT(dtrace_opens == 0); 14569 ASSERT(dtrace_retained == NULL); 14570 14571 mutex_exit(&dtrace_lock); 14572 mutex_exit(&dtrace_provider_lock); 14573 14574 /* 14575 * We don't destroy the task queue until after we have dropped our 14576 * locks (taskq_destroy() may block on running tasks). To prevent 14577 * attempting to do work after we have effectively detached but before 14578 * the task queue has been destroyed, all tasks dispatched via the 14579 * task queue must check that DTrace is still attached before 14580 * performing any operation. 14581 */ 14582 taskq_destroy(dtrace_taskq); 14583 dtrace_taskq = NULL; 14584 14585 return (DDI_SUCCESS); 14586 } 14587 14588 /*ARGSUSED*/ 14589 static int 14590 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 14591 { 14592 int error; 14593 14594 switch (infocmd) { 14595 case DDI_INFO_DEVT2DEVINFO: 14596 *result = (void *)dtrace_devi; 14597 error = DDI_SUCCESS; 14598 break; 14599 case DDI_INFO_DEVT2INSTANCE: 14600 *result = (void *)0; 14601 error = DDI_SUCCESS; 14602 break; 14603 default: 14604 error = DDI_FAILURE; 14605 } 14606 return (error); 14607 } 14608 14609 static struct cb_ops dtrace_cb_ops = { 14610 dtrace_open, /* open */ 14611 dtrace_close, /* close */ 14612 nulldev, /* strategy */ 14613 nulldev, /* print */ 14614 nodev, /* dump */ 14615 nodev, /* read */ 14616 nodev, /* write */ 14617 dtrace_ioctl, /* ioctl */ 14618 nodev, /* devmap */ 14619 nodev, /* mmap */ 14620 nodev, /* segmap */ 14621 nochpoll, /* poll */ 14622 ddi_prop_op, /* cb_prop_op */ 14623 0, /* streamtab */ 14624 D_NEW | D_MP /* Driver compatibility flag */ 14625 }; 14626 14627 static struct dev_ops dtrace_ops = { 14628 DEVO_REV, /* devo_rev */ 14629 0, /* refcnt */ 14630 dtrace_info, /* get_dev_info */ 14631 nulldev, /* identify */ 14632 nulldev, /* probe */ 14633 dtrace_attach, /* attach */ 14634 dtrace_detach, /* detach */ 14635 nodev, /* reset */ 14636 &dtrace_cb_ops, /* driver operations */ 14637 NULL, /* bus operations */ 14638 nodev /* dev power */ 14639 }; 14640 14641 static struct modldrv modldrv = { 14642 &mod_driverops, /* module type (this is a pseudo driver) */ 14643 "Dynamic Tracing", /* name of module */ 14644 &dtrace_ops, /* driver ops */ 14645 }; 14646 14647 static struct modlinkage modlinkage = { 14648 MODREV_1, 14649 (void *)&modldrv, 14650 NULL 14651 }; 14652 14653 int 14654 _init(void) 14655 { 14656 return (mod_install(&modlinkage)); 14657 } 14658 14659 int 14660 _info(struct modinfo *modinfop) 14661 { 14662 return (mod_info(&modlinkage, modinfop)); 14663 } 14664 14665 int 14666 _fini(void) 14667 { 14668 return (mod_remove(&modlinkage)); 14669 } 14670