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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 25 */ 26 27 /* 28 * DTrace - Dynamic Tracing for Solaris 29 * 30 * This is the implementation of the Solaris Dynamic Tracing framework 31 * (DTrace). The user-visible interface to DTrace is described at length in 32 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 33 * library, the in-kernel DTrace framework, and the DTrace providers are 34 * described in the block comments in the <sys/dtrace.h> header file. The 35 * internal architecture of DTrace is described in the block comments in the 36 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 37 * implementation very much assume mastery of all of these sources; if one has 38 * an unanswered question about the implementation, one should consult them 39 * first. 40 * 41 * The functions here are ordered roughly as follows: 42 * 43 * - Probe context functions 44 * - Probe hashing functions 45 * - Non-probe context utility functions 46 * - Matching functions 47 * - Provider-to-Framework API functions 48 * - Probe management functions 49 * - DIF object functions 50 * - Format functions 51 * - Predicate functions 52 * - ECB functions 53 * - Buffer functions 54 * - Enabling functions 55 * - DOF functions 56 * - Anonymous enabling functions 57 * - Consumer state functions 58 * - Helper functions 59 * - Hook functions 60 * - Driver cookbook functions 61 * 62 * Each group of functions begins with a block comment labelled the "DTrace 63 * [Group] Functions", allowing one to find each block by searching forward 64 * on capital-f functions. 65 */ 66 #include <sys/errno.h> 67 #include <sys/stat.h> 68 #include <sys/modctl.h> 69 #include <sys/conf.h> 70 #include <sys/systm.h> 71 #include <sys/ddi.h> 72 #include <sys/sunddi.h> 73 #include <sys/cpuvar.h> 74 #include <sys/kmem.h> 75 #include <sys/strsubr.h> 76 #include <sys/sysmacros.h> 77 #include <sys/dtrace_impl.h> 78 #include <sys/atomic.h> 79 #include <sys/cmn_err.h> 80 #include <sys/mutex_impl.h> 81 #include <sys/rwlock_impl.h> 82 #include <sys/ctf_api.h> 83 #include <sys/panic.h> 84 #include <sys/priv_impl.h> 85 #include <sys/policy.h> 86 #include <sys/cred_impl.h> 87 #include <sys/procfs_isa.h> 88 #include <sys/taskq.h> 89 #include <sys/mkdev.h> 90 #include <sys/kdi.h> 91 #include <sys/zone.h> 92 #include <sys/socket.h> 93 #include <netinet/in.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 = 1024; 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 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 148 149 /* 150 * DTrace External Variables 151 * 152 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 153 * available to DTrace consumers via the backtick (`) syntax. One of these, 154 * dtrace_zero, is made deliberately so: it is provided as a source of 155 * well-known, zero-filled memory. While this variable is not documented, 156 * it is used by some translators as an implementation detail. 157 */ 158 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 159 160 /* 161 * DTrace Internal Variables 162 */ 163 static dev_info_t *dtrace_devi; /* device info */ 164 static vmem_t *dtrace_arena; /* probe ID arena */ 165 static vmem_t *dtrace_minor; /* minor number arena */ 166 static taskq_t *dtrace_taskq; /* task queue */ 167 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 168 static int dtrace_nprobes; /* number of probes */ 169 static dtrace_provider_t *dtrace_provider; /* provider list */ 170 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 171 static int dtrace_opens; /* number of opens */ 172 static int dtrace_helpers; /* number of helpers */ 173 static int dtrace_getf; /* number of unpriv getf()s */ 174 static void *dtrace_softstate; /* softstate pointer */ 175 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 176 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 177 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 178 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 179 static int dtrace_toxranges; /* number of toxic ranges */ 180 static int dtrace_toxranges_max; /* size of toxic range array */ 181 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 182 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 183 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 184 static kthread_t *dtrace_panicked; /* panicking thread */ 185 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 186 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 187 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 188 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 189 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 190 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 191 static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 192 193 /* 194 * DTrace Locking 195 * DTrace is protected by three (relatively coarse-grained) locks: 196 * 197 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 198 * including enabling state, probes, ECBs, consumer state, helper state, 199 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 200 * probe context is lock-free -- synchronization is handled via the 201 * dtrace_sync() cross call mechanism. 202 * 203 * (2) dtrace_provider_lock is required when manipulating provider state, or 204 * when provider state must be held constant. 205 * 206 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 207 * when meta provider state must be held constant. 208 * 209 * The lock ordering between these three locks is dtrace_meta_lock before 210 * dtrace_provider_lock before dtrace_lock. (In particular, there are 211 * several places where dtrace_provider_lock is held by the framework as it 212 * calls into the providers -- which then call back into the framework, 213 * grabbing dtrace_lock.) 214 * 215 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 216 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 217 * role as a coarse-grained lock; it is acquired before both of these locks. 218 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 219 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 220 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 221 * acquired _between_ dtrace_provider_lock and dtrace_lock. 222 */ 223 static kmutex_t dtrace_lock; /* probe state lock */ 224 static kmutex_t dtrace_provider_lock; /* provider state lock */ 225 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 226 227 /* 228 * DTrace Provider Variables 229 * 230 * These are the variables relating to DTrace as a provider (that is, the 231 * provider of the BEGIN, END, and ERROR probes). 232 */ 233 static dtrace_pattr_t dtrace_provider_attr = { 234 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 235 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 236 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 237 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 238 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 239 }; 240 241 static void 242 dtrace_nullop(void) 243 {} 244 245 static int 246 dtrace_enable_nullop(void) 247 { 248 return (0); 249 } 250 251 static dtrace_pops_t dtrace_provider_ops = { 252 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, 253 (void (*)(void *, struct modctl *))dtrace_nullop, 254 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop, 255 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 256 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 257 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 258 NULL, 259 NULL, 260 NULL, 261 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 262 }; 263 264 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 265 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 266 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 267 268 /* 269 * DTrace Helper Tracing Variables 270 */ 271 uint32_t dtrace_helptrace_next = 0; 272 uint32_t dtrace_helptrace_nlocals; 273 char *dtrace_helptrace_buffer; 274 int dtrace_helptrace_bufsize = 512 * 1024; 275 276 #ifdef DEBUG 277 int dtrace_helptrace_enabled = 1; 278 #else 279 int dtrace_helptrace_enabled = 0; 280 #endif 281 282 /* 283 * DTrace Error Hashing 284 * 285 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 286 * table. This is very useful for checking coverage of tests that are 287 * expected to induce DIF or DOF processing errors, and may be useful for 288 * debugging problems in the DIF code generator or in DOF generation . The 289 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 290 */ 291 #ifdef DEBUG 292 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 293 static const char *dtrace_errlast; 294 static kthread_t *dtrace_errthread; 295 static kmutex_t dtrace_errlock; 296 #endif 297 298 /* 299 * DTrace Macros and Constants 300 * 301 * These are various macros that are useful in various spots in the 302 * implementation, along with a few random constants that have no meaning 303 * outside of the implementation. There is no real structure to this cpp 304 * mishmash -- but is there ever? 305 */ 306 #define DTRACE_HASHSTR(hash, probe) \ 307 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 308 309 #define DTRACE_HASHNEXT(hash, probe) \ 310 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 311 312 #define DTRACE_HASHPREV(hash, probe) \ 313 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 314 315 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 316 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 317 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 318 319 #define DTRACE_AGGHASHSIZE_SLEW 17 320 321 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 322 323 /* 324 * The key for a thread-local variable consists of the lower 61 bits of the 325 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 326 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 327 * equal to a variable identifier. This is necessary (but not sufficient) to 328 * assure that global associative arrays never collide with thread-local 329 * variables. To guarantee that they cannot collide, we must also define the 330 * order for keying dynamic variables. That order is: 331 * 332 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 333 * 334 * Because the variable-key and the tls-key are in orthogonal spaces, there is 335 * no way for a global variable key signature to match a thread-local key 336 * signature. 337 */ 338 #define DTRACE_TLS_THRKEY(where) { \ 339 uint_t intr = 0; \ 340 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 341 for (; actv; actv >>= 1) \ 342 intr++; \ 343 ASSERT(intr < (1 << 3)); \ 344 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 345 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 346 } 347 348 #define DT_BSWAP_8(x) ((x) & 0xff) 349 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 350 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 351 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 352 353 #define DT_MASK_LO 0x00000000FFFFFFFFULL 354 355 #define DTRACE_STORE(type, tomax, offset, what) \ 356 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 357 358 #ifndef __i386 359 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 360 if (addr & (size - 1)) { \ 361 *flags |= CPU_DTRACE_BADALIGN; \ 362 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 363 return (0); \ 364 } 365 #else 366 #define DTRACE_ALIGNCHECK(addr, size, flags) 367 #endif 368 369 /* 370 * Test whether a range of memory starting at testaddr of size testsz falls 371 * within the range of memory described by addr, sz. We take care to avoid 372 * problems with overflow and underflow of the unsigned quantities, and 373 * disallow all negative sizes. Ranges of size 0 are allowed. 374 */ 375 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 376 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 377 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 378 (testaddr) + (testsz) >= (testaddr)) 379 380 /* 381 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 382 * alloc_sz on the righthand side of the comparison in order to avoid overflow 383 * or underflow in the comparison with it. This is simpler than the INRANGE 384 * check above, because we know that the dtms_scratch_ptr is valid in the 385 * range. Allocations of size zero are allowed. 386 */ 387 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 388 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 389 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 390 391 #define DTRACE_LOADFUNC(bits) \ 392 /*CSTYLED*/ \ 393 uint##bits##_t \ 394 dtrace_load##bits(uintptr_t addr) \ 395 { \ 396 size_t size = bits / NBBY; \ 397 /*CSTYLED*/ \ 398 uint##bits##_t rval; \ 399 int i; \ 400 volatile uint16_t *flags = (volatile uint16_t *) \ 401 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ 402 \ 403 DTRACE_ALIGNCHECK(addr, size, flags); \ 404 \ 405 for (i = 0; i < dtrace_toxranges; i++) { \ 406 if (addr >= dtrace_toxrange[i].dtt_limit) \ 407 continue; \ 408 \ 409 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 410 continue; \ 411 \ 412 /* \ 413 * This address falls within a toxic region; return 0. \ 414 */ \ 415 *flags |= CPU_DTRACE_BADADDR; \ 416 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 417 return (0); \ 418 } \ 419 \ 420 *flags |= CPU_DTRACE_NOFAULT; \ 421 /*CSTYLED*/ \ 422 rval = *((volatile uint##bits##_t *)addr); \ 423 *flags &= ~CPU_DTRACE_NOFAULT; \ 424 \ 425 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 426 } 427 428 #ifdef _LP64 429 #define dtrace_loadptr dtrace_load64 430 #else 431 #define dtrace_loadptr dtrace_load32 432 #endif 433 434 #define DTRACE_DYNHASH_FREE 0 435 #define DTRACE_DYNHASH_SINK 1 436 #define DTRACE_DYNHASH_VALID 2 437 438 #define DTRACE_MATCH_FAIL -1 439 #define DTRACE_MATCH_NEXT 0 440 #define DTRACE_MATCH_DONE 1 441 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 442 #define DTRACE_STATE_ALIGN 64 443 444 #define DTRACE_FLAGS2FLT(flags) \ 445 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 446 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 447 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 448 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 449 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 450 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 451 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 452 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 453 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 454 DTRACEFLT_UNKNOWN) 455 456 #define DTRACEACT_ISSTRING(act) \ 457 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 458 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 459 460 static size_t dtrace_strlen(const char *, size_t); 461 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 462 static void dtrace_enabling_provide(dtrace_provider_t *); 463 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 464 static void dtrace_enabling_matchall(void); 465 static void dtrace_enabling_reap(void); 466 static dtrace_state_t *dtrace_anon_grab(void); 467 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 468 dtrace_state_t *, uint64_t, uint64_t); 469 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 470 static void dtrace_buffer_drop(dtrace_buffer_t *); 471 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 472 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 473 dtrace_state_t *, dtrace_mstate_t *); 474 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 475 dtrace_optval_t); 476 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 477 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 478 static int dtrace_priv_proc(dtrace_state_t *, dtrace_mstate_t *); 479 static void dtrace_getf_barrier(void); 480 481 /* 482 * DTrace Probe Context Functions 483 * 484 * These functions are called from probe context. Because probe context is 485 * any context in which C may be called, arbitrarily locks may be held, 486 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 487 * As a result, functions called from probe context may only call other DTrace 488 * support functions -- they may not interact at all with the system at large. 489 * (Note that the ASSERT macro is made probe-context safe by redefining it in 490 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 491 * loads are to be performed from probe context, they _must_ be in terms of 492 * the safe dtrace_load*() variants. 493 * 494 * Some functions in this block are not actually called from probe context; 495 * for these functions, there will be a comment above the function reading 496 * "Note: not called from probe context." 497 */ 498 void 499 dtrace_panic(const char *format, ...) 500 { 501 va_list alist; 502 503 va_start(alist, format); 504 dtrace_vpanic(format, alist); 505 va_end(alist); 506 } 507 508 int 509 dtrace_assfail(const char *a, const char *f, int l) 510 { 511 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 512 513 /* 514 * We just need something here that even the most clever compiler 515 * cannot optimize away. 516 */ 517 return (a[(uintptr_t)f]); 518 } 519 520 /* 521 * Atomically increment a specified error counter from probe context. 522 */ 523 static void 524 dtrace_error(uint32_t *counter) 525 { 526 /* 527 * Most counters stored to in probe context are per-CPU counters. 528 * However, there are some error conditions that are sufficiently 529 * arcane that they don't merit per-CPU storage. If these counters 530 * are incremented concurrently on different CPUs, scalability will be 531 * adversely affected -- but we don't expect them to be white-hot in a 532 * correctly constructed enabling... 533 */ 534 uint32_t oval, nval; 535 536 do { 537 oval = *counter; 538 539 if ((nval = oval + 1) == 0) { 540 /* 541 * If the counter would wrap, set it to 1 -- assuring 542 * that the counter is never zero when we have seen 543 * errors. (The counter must be 32-bits because we 544 * aren't guaranteed a 64-bit compare&swap operation.) 545 * To save this code both the infamy of being fingered 546 * by a priggish news story and the indignity of being 547 * the target of a neo-puritan witch trial, we're 548 * carefully avoiding any colorful description of the 549 * likelihood of this condition -- but suffice it to 550 * say that it is only slightly more likely than the 551 * overflow of predicate cache IDs, as discussed in 552 * dtrace_predicate_create(). 553 */ 554 nval = 1; 555 } 556 } while (dtrace_cas32(counter, oval, nval) != oval); 557 } 558 559 /* 560 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 561 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 562 */ 563 DTRACE_LOADFUNC(8) 564 DTRACE_LOADFUNC(16) 565 DTRACE_LOADFUNC(32) 566 DTRACE_LOADFUNC(64) 567 568 static int 569 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 570 { 571 if (dest < mstate->dtms_scratch_base) 572 return (0); 573 574 if (dest + size < dest) 575 return (0); 576 577 if (dest + size > mstate->dtms_scratch_ptr) 578 return (0); 579 580 return (1); 581 } 582 583 static int 584 dtrace_canstore_statvar(uint64_t addr, size_t sz, 585 dtrace_statvar_t **svars, int nsvars) 586 { 587 int i; 588 589 for (i = 0; i < nsvars; i++) { 590 dtrace_statvar_t *svar = svars[i]; 591 592 if (svar == NULL || svar->dtsv_size == 0) 593 continue; 594 595 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 596 return (1); 597 } 598 599 return (0); 600 } 601 602 /* 603 * Check to see if the address is within a memory region to which a store may 604 * be issued. This includes the DTrace scratch areas, and any DTrace variable 605 * region. The caller of dtrace_canstore() is responsible for performing any 606 * alignment checks that are needed before stores are actually executed. 607 */ 608 static int 609 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 610 dtrace_vstate_t *vstate) 611 { 612 /* 613 * First, check to see if the address is in scratch space... 614 */ 615 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 616 mstate->dtms_scratch_size)) 617 return (1); 618 619 /* 620 * Now check to see if it's a dynamic variable. This check will pick 621 * up both thread-local variables and any global dynamically-allocated 622 * variables. 623 */ 624 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 625 vstate->dtvs_dynvars.dtds_size)) { 626 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 627 uintptr_t base = (uintptr_t)dstate->dtds_base + 628 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 629 uintptr_t chunkoffs; 630 631 /* 632 * Before we assume that we can store here, we need to make 633 * sure that it isn't in our metadata -- storing to our 634 * dynamic variable metadata would corrupt our state. For 635 * the range to not include any dynamic variable metadata, 636 * it must: 637 * 638 * (1) Start above the hash table that is at the base of 639 * the dynamic variable space 640 * 641 * (2) Have a starting chunk offset that is beyond the 642 * dtrace_dynvar_t that is at the base of every chunk 643 * 644 * (3) Not span a chunk boundary 645 * 646 */ 647 if (addr < base) 648 return (0); 649 650 chunkoffs = (addr - base) % dstate->dtds_chunksize; 651 652 if (chunkoffs < sizeof (dtrace_dynvar_t)) 653 return (0); 654 655 if (chunkoffs + sz > dstate->dtds_chunksize) 656 return (0); 657 658 return (1); 659 } 660 661 /* 662 * Finally, check the static local and global variables. These checks 663 * take the longest, so we perform them last. 664 */ 665 if (dtrace_canstore_statvar(addr, sz, 666 vstate->dtvs_locals, vstate->dtvs_nlocals)) 667 return (1); 668 669 if (dtrace_canstore_statvar(addr, sz, 670 vstate->dtvs_globals, vstate->dtvs_nglobals)) 671 return (1); 672 673 return (0); 674 } 675 676 677 /* 678 * Convenience routine to check to see if the address is within a memory 679 * region in which a load may be issued given the user's privilege level; 680 * if not, it sets the appropriate error flags and loads 'addr' into the 681 * illegal value slot. 682 * 683 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 684 * appropriate memory access protection. 685 */ 686 static int 687 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 688 dtrace_vstate_t *vstate) 689 { 690 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 691 file_t *fp; 692 693 /* 694 * If we hold the privilege to read from kernel memory, then 695 * everything is readable. 696 */ 697 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 698 return (1); 699 700 /* 701 * You can obviously read that which you can store. 702 */ 703 if (dtrace_canstore(addr, sz, mstate, vstate)) 704 return (1); 705 706 /* 707 * We're allowed to read from our own string table. 708 */ 709 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 710 mstate->dtms_difo->dtdo_strlen)) 711 return (1); 712 713 if (vstate->dtvs_state != NULL && 714 dtrace_priv_proc(vstate->dtvs_state, mstate)) { 715 proc_t *p; 716 717 /* 718 * When we have privileges to the current process, there are 719 * several context-related kernel structures that are safe to 720 * read, even absent the privilege to read from kernel memory. 721 * These reads are safe because these structures contain only 722 * state that (1) we're permitted to read, (2) is harmless or 723 * (3) contains pointers to additional kernel state that we're 724 * not permitted to read (and as such, do not present an 725 * opportunity for privilege escalation). Finally (and 726 * critically), because of the nature of their relation with 727 * the current thread context, the memory associated with these 728 * structures cannot change over the duration of probe context, 729 * and it is therefore impossible for this memory to be 730 * deallocated and reallocated as something else while it's 731 * being operated upon. 732 */ 733 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) 734 return (1); 735 736 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 737 sz, curthread->t_procp, sizeof (proc_t))) { 738 return (1); 739 } 740 741 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 742 curthread->t_cred, sizeof (cred_t))) { 743 return (1); 744 } 745 746 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 747 &(p->p_pidp->pid_id), sizeof (pid_t))) { 748 return (1); 749 } 750 751 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 752 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 753 return (1); 754 } 755 } 756 757 if ((fp = mstate->dtms_getf) != NULL) { 758 uintptr_t psz = sizeof (void *); 759 vnode_t *vp; 760 vnodeops_t *op; 761 762 /* 763 * When getf() returns a file_t, the enabling is implicitly 764 * granted the (transient) right to read the returned file_t 765 * as well as the v_path and v_op->vnop_name of the underlying 766 * vnode. These accesses are allowed after a successful 767 * getf() because the members that they refer to cannot change 768 * once set -- and the barrier logic in the kernel's closef() 769 * path assures that the file_t and its referenced vode_t 770 * cannot themselves be stale (that is, it impossible for 771 * either dtms_getf itself or its f_vnode member to reference 772 * freed memory). 773 */ 774 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) 775 return (1); 776 777 if ((vp = fp->f_vnode) != NULL) { 778 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) 779 return (1); 780 781 if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz, 782 vp->v_path, strlen(vp->v_path) + 1)) { 783 return (1); 784 } 785 786 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) 787 return (1); 788 789 if ((op = vp->v_op) != NULL && 790 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 791 return (1); 792 } 793 794 if (op != NULL && op->vnop_name != NULL && 795 DTRACE_INRANGE(addr, sz, op->vnop_name, 796 strlen(op->vnop_name) + 1)) { 797 return (1); 798 } 799 } 800 } 801 802 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 803 *illval = addr; 804 return (0); 805 } 806 807 /* 808 * Convenience routine to check to see if a given string is within a memory 809 * region in which a load may be issued given the user's privilege level; 810 * this exists so that we don't need to issue unnecessary dtrace_strlen() 811 * calls in the event that the user has all privileges. 812 */ 813 static int 814 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 815 dtrace_vstate_t *vstate) 816 { 817 size_t strsz; 818 819 /* 820 * If we hold the privilege to read from kernel memory, then 821 * everything is readable. 822 */ 823 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 824 return (1); 825 826 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 827 if (dtrace_canload(addr, strsz, mstate, vstate)) 828 return (1); 829 830 return (0); 831 } 832 833 /* 834 * Convenience routine to check to see if a given variable is within a memory 835 * region in which a load may be issued given the user's privilege level. 836 */ 837 static int 838 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 839 dtrace_vstate_t *vstate) 840 { 841 size_t sz; 842 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 843 844 /* 845 * If we hold the privilege to read from kernel memory, then 846 * everything is readable. 847 */ 848 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 849 return (1); 850 851 if (type->dtdt_kind == DIF_TYPE_STRING) 852 sz = dtrace_strlen(src, 853 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 854 else 855 sz = type->dtdt_size; 856 857 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 858 } 859 860 /* 861 * Compare two strings using safe loads. 862 */ 863 static int 864 dtrace_strncmp(char *s1, char *s2, size_t limit) 865 { 866 uint8_t c1, c2; 867 volatile uint16_t *flags; 868 869 if (s1 == s2 || limit == 0) 870 return (0); 871 872 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 873 874 do { 875 if (s1 == NULL) { 876 c1 = '\0'; 877 } else { 878 c1 = dtrace_load8((uintptr_t)s1++); 879 } 880 881 if (s2 == NULL) { 882 c2 = '\0'; 883 } else { 884 c2 = dtrace_load8((uintptr_t)s2++); 885 } 886 887 if (c1 != c2) 888 return (c1 - c2); 889 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 890 891 return (0); 892 } 893 894 /* 895 * Compute strlen(s) for a string using safe memory accesses. The additional 896 * len parameter is used to specify a maximum length to ensure completion. 897 */ 898 static size_t 899 dtrace_strlen(const char *s, size_t lim) 900 { 901 uint_t len; 902 903 for (len = 0; len != lim; len++) { 904 if (dtrace_load8((uintptr_t)s++) == '\0') 905 break; 906 } 907 908 return (len); 909 } 910 911 /* 912 * Check if an address falls within a toxic region. 913 */ 914 static int 915 dtrace_istoxic(uintptr_t kaddr, size_t size) 916 { 917 uintptr_t taddr, tsize; 918 int i; 919 920 for (i = 0; i < dtrace_toxranges; i++) { 921 taddr = dtrace_toxrange[i].dtt_base; 922 tsize = dtrace_toxrange[i].dtt_limit - taddr; 923 924 if (kaddr - taddr < tsize) { 925 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 926 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr; 927 return (1); 928 } 929 930 if (taddr - kaddr < size) { 931 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 932 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr; 933 return (1); 934 } 935 } 936 937 return (0); 938 } 939 940 /* 941 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 942 * memory specified by the DIF program. The dst is assumed to be safe memory 943 * that we can store to directly because it is managed by DTrace. As with 944 * standard bcopy, overlapping copies are handled properly. 945 */ 946 static void 947 dtrace_bcopy(const void *src, void *dst, size_t len) 948 { 949 if (len != 0) { 950 uint8_t *s1 = dst; 951 const uint8_t *s2 = src; 952 953 if (s1 <= s2) { 954 do { 955 *s1++ = dtrace_load8((uintptr_t)s2++); 956 } while (--len != 0); 957 } else { 958 s2 += len; 959 s1 += len; 960 961 do { 962 *--s1 = dtrace_load8((uintptr_t)--s2); 963 } while (--len != 0); 964 } 965 } 966 } 967 968 /* 969 * Copy src to dst using safe memory accesses, up to either the specified 970 * length, or the point that a nul byte is encountered. The src is assumed to 971 * be unsafe memory specified by the DIF program. The dst is assumed to be 972 * safe memory that we can store to directly because it is managed by DTrace. 973 * Unlike dtrace_bcopy(), overlapping regions are not handled. 974 */ 975 static void 976 dtrace_strcpy(const void *src, void *dst, size_t len) 977 { 978 if (len != 0) { 979 uint8_t *s1 = dst, c; 980 const uint8_t *s2 = src; 981 982 do { 983 *s1++ = c = dtrace_load8((uintptr_t)s2++); 984 } while (--len != 0 && c != '\0'); 985 } 986 } 987 988 /* 989 * Copy src to dst, deriving the size and type from the specified (BYREF) 990 * variable type. The src is assumed to be unsafe memory specified by the DIF 991 * program. The dst is assumed to be DTrace variable memory that is of the 992 * specified type; we assume that we can store to directly. 993 */ 994 static void 995 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 996 { 997 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 998 999 if (type->dtdt_kind == DIF_TYPE_STRING) { 1000 dtrace_strcpy(src, dst, type->dtdt_size); 1001 } else { 1002 dtrace_bcopy(src, dst, type->dtdt_size); 1003 } 1004 } 1005 1006 /* 1007 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1008 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1009 * safe memory that we can access directly because it is managed by DTrace. 1010 */ 1011 static int 1012 dtrace_bcmp(const void *s1, const void *s2, size_t len) 1013 { 1014 volatile uint16_t *flags; 1015 1016 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 1017 1018 if (s1 == s2) 1019 return (0); 1020 1021 if (s1 == NULL || s2 == NULL) 1022 return (1); 1023 1024 if (s1 != s2 && len != 0) { 1025 const uint8_t *ps1 = s1; 1026 const uint8_t *ps2 = s2; 1027 1028 do { 1029 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1030 return (1); 1031 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1032 } 1033 return (0); 1034 } 1035 1036 /* 1037 * Zero the specified region using a simple byte-by-byte loop. Note that this 1038 * is for safe DTrace-managed memory only. 1039 */ 1040 static void 1041 dtrace_bzero(void *dst, size_t len) 1042 { 1043 uchar_t *cp; 1044 1045 for (cp = dst; len != 0; len--) 1046 *cp++ = 0; 1047 } 1048 1049 static void 1050 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1051 { 1052 uint64_t result[2]; 1053 1054 result[0] = addend1[0] + addend2[0]; 1055 result[1] = addend1[1] + addend2[1] + 1056 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1057 1058 sum[0] = result[0]; 1059 sum[1] = result[1]; 1060 } 1061 1062 /* 1063 * Shift the 128-bit value in a by b. If b is positive, shift left. 1064 * If b is negative, shift right. 1065 */ 1066 static void 1067 dtrace_shift_128(uint64_t *a, int b) 1068 { 1069 uint64_t mask; 1070 1071 if (b == 0) 1072 return; 1073 1074 if (b < 0) { 1075 b = -b; 1076 if (b >= 64) { 1077 a[0] = a[1] >> (b - 64); 1078 a[1] = 0; 1079 } else { 1080 a[0] >>= b; 1081 mask = 1LL << (64 - b); 1082 mask -= 1; 1083 a[0] |= ((a[1] & mask) << (64 - b)); 1084 a[1] >>= b; 1085 } 1086 } else { 1087 if (b >= 64) { 1088 a[1] = a[0] << (b - 64); 1089 a[0] = 0; 1090 } else { 1091 a[1] <<= b; 1092 mask = a[0] >> (64 - b); 1093 a[1] |= mask; 1094 a[0] <<= b; 1095 } 1096 } 1097 } 1098 1099 /* 1100 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1101 * use native multiplication on those, and then re-combine into the 1102 * resulting 128-bit value. 1103 * 1104 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1105 * hi1 * hi2 << 64 + 1106 * hi1 * lo2 << 32 + 1107 * hi2 * lo1 << 32 + 1108 * lo1 * lo2 1109 */ 1110 static void 1111 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1112 { 1113 uint64_t hi1, hi2, lo1, lo2; 1114 uint64_t tmp[2]; 1115 1116 hi1 = factor1 >> 32; 1117 hi2 = factor2 >> 32; 1118 1119 lo1 = factor1 & DT_MASK_LO; 1120 lo2 = factor2 & DT_MASK_LO; 1121 1122 product[0] = lo1 * lo2; 1123 product[1] = hi1 * hi2; 1124 1125 tmp[0] = hi1 * lo2; 1126 tmp[1] = 0; 1127 dtrace_shift_128(tmp, 32); 1128 dtrace_add_128(product, tmp, product); 1129 1130 tmp[0] = hi2 * lo1; 1131 tmp[1] = 0; 1132 dtrace_shift_128(tmp, 32); 1133 dtrace_add_128(product, tmp, product); 1134 } 1135 1136 /* 1137 * This privilege check should be used by actions and subroutines to 1138 * verify that the user credentials of the process that enabled the 1139 * invoking ECB match the target credentials 1140 */ 1141 static int 1142 dtrace_priv_proc_common_user(dtrace_state_t *state) 1143 { 1144 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1145 1146 /* 1147 * We should always have a non-NULL state cred here, since if cred 1148 * is null (anonymous tracing), we fast-path bypass this routine. 1149 */ 1150 ASSERT(s_cr != NULL); 1151 1152 if ((cr = CRED()) != NULL && 1153 s_cr->cr_uid == cr->cr_uid && 1154 s_cr->cr_uid == cr->cr_ruid && 1155 s_cr->cr_uid == cr->cr_suid && 1156 s_cr->cr_gid == cr->cr_gid && 1157 s_cr->cr_gid == cr->cr_rgid && 1158 s_cr->cr_gid == cr->cr_sgid) 1159 return (1); 1160 1161 return (0); 1162 } 1163 1164 /* 1165 * This privilege check should be used by actions and subroutines to 1166 * verify that the zone of the process that enabled the invoking ECB 1167 * matches the target credentials 1168 */ 1169 static int 1170 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1171 { 1172 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1173 1174 /* 1175 * We should always have a non-NULL state cred here, since if cred 1176 * is null (anonymous tracing), we fast-path bypass this routine. 1177 */ 1178 ASSERT(s_cr != NULL); 1179 1180 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1181 return (1); 1182 1183 return (0); 1184 } 1185 1186 /* 1187 * This privilege check should be used by actions and subroutines to 1188 * verify that the process has not setuid or changed credentials. 1189 */ 1190 static int 1191 dtrace_priv_proc_common_nocd() 1192 { 1193 proc_t *proc; 1194 1195 if ((proc = ttoproc(curthread)) != NULL && 1196 !(proc->p_flag & SNOCD)) 1197 return (1); 1198 1199 return (0); 1200 } 1201 1202 static int 1203 dtrace_priv_proc_destructive(dtrace_state_t *state, dtrace_mstate_t *mstate) 1204 { 1205 int action = state->dts_cred.dcr_action; 1206 1207 if (!(mstate->dtms_access & DTRACE_ACCESS_PROC)) 1208 goto bad; 1209 1210 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1211 dtrace_priv_proc_common_zone(state) == 0) 1212 goto bad; 1213 1214 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1215 dtrace_priv_proc_common_user(state) == 0) 1216 goto bad; 1217 1218 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1219 dtrace_priv_proc_common_nocd() == 0) 1220 goto bad; 1221 1222 return (1); 1223 1224 bad: 1225 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1226 1227 return (0); 1228 } 1229 1230 static int 1231 dtrace_priv_proc_control(dtrace_state_t *state, dtrace_mstate_t *mstate) 1232 { 1233 if (mstate->dtms_access & DTRACE_ACCESS_PROC) { 1234 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1235 return (1); 1236 1237 if (dtrace_priv_proc_common_zone(state) && 1238 dtrace_priv_proc_common_user(state) && 1239 dtrace_priv_proc_common_nocd()) 1240 return (1); 1241 } 1242 1243 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1244 1245 return (0); 1246 } 1247 1248 static int 1249 dtrace_priv_proc(dtrace_state_t *state, dtrace_mstate_t *mstate) 1250 { 1251 if ((mstate->dtms_access & DTRACE_ACCESS_PROC) && 1252 (state->dts_cred.dcr_action & DTRACE_CRA_PROC)) 1253 return (1); 1254 1255 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1256 1257 return (0); 1258 } 1259 1260 static int 1261 dtrace_priv_kernel(dtrace_state_t *state) 1262 { 1263 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1264 return (1); 1265 1266 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1267 1268 return (0); 1269 } 1270 1271 static int 1272 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1273 { 1274 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1275 return (1); 1276 1277 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1278 1279 return (0); 1280 } 1281 1282 /* 1283 * Determine if the dte_cond of the specified ECB allows for processing of 1284 * the current probe to continue. Note that this routine may allow continued 1285 * processing, but with access(es) stripped from the mstate's dtms_access 1286 * field. 1287 */ 1288 static int 1289 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1290 dtrace_ecb_t *ecb) 1291 { 1292 dtrace_probe_t *probe = ecb->dte_probe; 1293 dtrace_provider_t *prov = probe->dtpr_provider; 1294 dtrace_pops_t *pops = &prov->dtpv_pops; 1295 int mode = DTRACE_MODE_NOPRIV_DROP; 1296 1297 ASSERT(ecb->dte_cond); 1298 1299 if (pops->dtps_mode != NULL) { 1300 mode = pops->dtps_mode(prov->dtpv_arg, 1301 probe->dtpr_id, probe->dtpr_arg); 1302 1303 ASSERT((mode & DTRACE_MODE_USER) || 1304 (mode & DTRACE_MODE_KERNEL)); 1305 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1306 (mode & DTRACE_MODE_NOPRIV_DROP)); 1307 } 1308 1309 /* 1310 * If the dte_cond bits indicate that this consumer is only allowed to 1311 * see user-mode firings of this probe, call the provider's dtps_mode() 1312 * entry point to check that the probe was fired while in a user 1313 * context. If that's not the case, use the policy specified by the 1314 * provider to determine if we drop the probe or merely restrict 1315 * operation. 1316 */ 1317 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1318 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1319 1320 if (!(mode & DTRACE_MODE_USER)) { 1321 if (mode & DTRACE_MODE_NOPRIV_DROP) 1322 return (0); 1323 1324 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1325 } 1326 } 1327 1328 /* 1329 * This is more subtle than it looks. We have to be absolutely certain 1330 * that CRED() isn't going to change out from under us so it's only 1331 * legit to examine that structure if we're in constrained situations. 1332 * Currently, the only times we'll this check is if a non-super-user 1333 * has enabled the profile or syscall providers -- providers that 1334 * allow visibility of all processes. For the profile case, the check 1335 * above will ensure that we're examining a user context. 1336 */ 1337 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1338 cred_t *cr; 1339 cred_t *s_cr = state->dts_cred.dcr_cred; 1340 proc_t *proc; 1341 1342 ASSERT(s_cr != NULL); 1343 1344 if ((cr = CRED()) == NULL || 1345 s_cr->cr_uid != cr->cr_uid || 1346 s_cr->cr_uid != cr->cr_ruid || 1347 s_cr->cr_uid != cr->cr_suid || 1348 s_cr->cr_gid != cr->cr_gid || 1349 s_cr->cr_gid != cr->cr_rgid || 1350 s_cr->cr_gid != cr->cr_sgid || 1351 (proc = ttoproc(curthread)) == NULL || 1352 (proc->p_flag & SNOCD)) { 1353 if (mode & DTRACE_MODE_NOPRIV_DROP) 1354 return (0); 1355 1356 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1357 } 1358 } 1359 1360 /* 1361 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1362 * in our zone, check to see if our mode policy is to restrict rather 1363 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1364 * and DTRACE_ACCESS_ARGS 1365 */ 1366 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1367 cred_t *cr; 1368 cred_t *s_cr = state->dts_cred.dcr_cred; 1369 1370 ASSERT(s_cr != NULL); 1371 1372 if ((cr = CRED()) == NULL || 1373 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1374 if (mode & DTRACE_MODE_NOPRIV_DROP) 1375 return (0); 1376 1377 mstate->dtms_access &= 1378 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1379 } 1380 } 1381 1382 return (1); 1383 } 1384 1385 /* 1386 * Note: not called from probe context. This function is called 1387 * asynchronously (and at a regular interval) from outside of probe context to 1388 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1389 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1390 */ 1391 void 1392 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1393 { 1394 dtrace_dynvar_t *dirty; 1395 dtrace_dstate_percpu_t *dcpu; 1396 dtrace_dynvar_t **rinsep; 1397 int i, j, work = 0; 1398 1399 for (i = 0; i < NCPU; i++) { 1400 dcpu = &dstate->dtds_percpu[i]; 1401 rinsep = &dcpu->dtdsc_rinsing; 1402 1403 /* 1404 * If the dirty list is NULL, there is no dirty work to do. 1405 */ 1406 if (dcpu->dtdsc_dirty == NULL) 1407 continue; 1408 1409 if (dcpu->dtdsc_rinsing != NULL) { 1410 /* 1411 * If the rinsing list is non-NULL, then it is because 1412 * this CPU was selected to accept another CPU's 1413 * dirty list -- and since that time, dirty buffers 1414 * have accumulated. This is a highly unlikely 1415 * condition, but we choose to ignore the dirty 1416 * buffers -- they'll be picked up a future cleanse. 1417 */ 1418 continue; 1419 } 1420 1421 if (dcpu->dtdsc_clean != NULL) { 1422 /* 1423 * If the clean list is non-NULL, then we're in a 1424 * situation where a CPU has done deallocations (we 1425 * have a non-NULL dirty list) but no allocations (we 1426 * also have a non-NULL clean list). We can't simply 1427 * move the dirty list into the clean list on this 1428 * CPU, yet we also don't want to allow this condition 1429 * to persist, lest a short clean list prevent a 1430 * massive dirty list from being cleaned (which in 1431 * turn could lead to otherwise avoidable dynamic 1432 * drops). To deal with this, we look for some CPU 1433 * with a NULL clean list, NULL dirty list, and NULL 1434 * rinsing list -- and then we borrow this CPU to 1435 * rinse our dirty list. 1436 */ 1437 for (j = 0; j < NCPU; j++) { 1438 dtrace_dstate_percpu_t *rinser; 1439 1440 rinser = &dstate->dtds_percpu[j]; 1441 1442 if (rinser->dtdsc_rinsing != NULL) 1443 continue; 1444 1445 if (rinser->dtdsc_dirty != NULL) 1446 continue; 1447 1448 if (rinser->dtdsc_clean != NULL) 1449 continue; 1450 1451 rinsep = &rinser->dtdsc_rinsing; 1452 break; 1453 } 1454 1455 if (j == NCPU) { 1456 /* 1457 * We were unable to find another CPU that 1458 * could accept this dirty list -- we are 1459 * therefore unable to clean it now. 1460 */ 1461 dtrace_dynvar_failclean++; 1462 continue; 1463 } 1464 } 1465 1466 work = 1; 1467 1468 /* 1469 * Atomically move the dirty list aside. 1470 */ 1471 do { 1472 dirty = dcpu->dtdsc_dirty; 1473 1474 /* 1475 * Before we zap the dirty list, set the rinsing list. 1476 * (This allows for a potential assertion in 1477 * dtrace_dynvar(): if a free dynamic variable appears 1478 * on a hash chain, either the dirty list or the 1479 * rinsing list for some CPU must be non-NULL.) 1480 */ 1481 *rinsep = dirty; 1482 dtrace_membar_producer(); 1483 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1484 dirty, NULL) != dirty); 1485 } 1486 1487 if (!work) { 1488 /* 1489 * We have no work to do; we can simply return. 1490 */ 1491 return; 1492 } 1493 1494 dtrace_sync(); 1495 1496 for (i = 0; i < NCPU; i++) { 1497 dcpu = &dstate->dtds_percpu[i]; 1498 1499 if (dcpu->dtdsc_rinsing == NULL) 1500 continue; 1501 1502 /* 1503 * We are now guaranteed that no hash chain contains a pointer 1504 * into this dirty list; we can make it clean. 1505 */ 1506 ASSERT(dcpu->dtdsc_clean == NULL); 1507 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1508 dcpu->dtdsc_rinsing = NULL; 1509 } 1510 1511 /* 1512 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1513 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1514 * This prevents a race whereby a CPU incorrectly decides that 1515 * the state should be something other than DTRACE_DSTATE_CLEAN 1516 * after dtrace_dynvar_clean() has completed. 1517 */ 1518 dtrace_sync(); 1519 1520 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1521 } 1522 1523 /* 1524 * Depending on the value of the op parameter, this function looks-up, 1525 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1526 * allocation is requested, this function will return a pointer to a 1527 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1528 * variable can be allocated. If NULL is returned, the appropriate counter 1529 * will be incremented. 1530 */ 1531 dtrace_dynvar_t * 1532 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1533 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1534 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1535 { 1536 uint64_t hashval = DTRACE_DYNHASH_VALID; 1537 dtrace_dynhash_t *hash = dstate->dtds_hash; 1538 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1539 processorid_t me = CPU->cpu_id, cpu = me; 1540 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1541 size_t bucket, ksize; 1542 size_t chunksize = dstate->dtds_chunksize; 1543 uintptr_t kdata, lock, nstate; 1544 uint_t i; 1545 1546 ASSERT(nkeys != 0); 1547 1548 /* 1549 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1550 * algorithm. For the by-value portions, we perform the algorithm in 1551 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1552 * bit, and seems to have only a minute effect on distribution. For 1553 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1554 * over each referenced byte. It's painful to do this, but it's much 1555 * better than pathological hash distribution. The efficacy of the 1556 * hashing algorithm (and a comparison with other algorithms) may be 1557 * found by running the ::dtrace_dynstat MDB dcmd. 1558 */ 1559 for (i = 0; i < nkeys; i++) { 1560 if (key[i].dttk_size == 0) { 1561 uint64_t val = key[i].dttk_value; 1562 1563 hashval += (val >> 48) & 0xffff; 1564 hashval += (hashval << 10); 1565 hashval ^= (hashval >> 6); 1566 1567 hashval += (val >> 32) & 0xffff; 1568 hashval += (hashval << 10); 1569 hashval ^= (hashval >> 6); 1570 1571 hashval += (val >> 16) & 0xffff; 1572 hashval += (hashval << 10); 1573 hashval ^= (hashval >> 6); 1574 1575 hashval += val & 0xffff; 1576 hashval += (hashval << 10); 1577 hashval ^= (hashval >> 6); 1578 } else { 1579 /* 1580 * This is incredibly painful, but it beats the hell 1581 * out of the alternative. 1582 */ 1583 uint64_t j, size = key[i].dttk_size; 1584 uintptr_t base = (uintptr_t)key[i].dttk_value; 1585 1586 if (!dtrace_canload(base, size, mstate, vstate)) 1587 break; 1588 1589 for (j = 0; j < size; j++) { 1590 hashval += dtrace_load8(base + j); 1591 hashval += (hashval << 10); 1592 hashval ^= (hashval >> 6); 1593 } 1594 } 1595 } 1596 1597 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1598 return (NULL); 1599 1600 hashval += (hashval << 3); 1601 hashval ^= (hashval >> 11); 1602 hashval += (hashval << 15); 1603 1604 /* 1605 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1606 * comes out to be one of our two sentinel hash values. If this 1607 * actually happens, we set the hashval to be a value known to be a 1608 * non-sentinel value. 1609 */ 1610 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1611 hashval = DTRACE_DYNHASH_VALID; 1612 1613 /* 1614 * Yes, it's painful to do a divide here. If the cycle count becomes 1615 * important here, tricks can be pulled to reduce it. (However, it's 1616 * critical that hash collisions be kept to an absolute minimum; 1617 * they're much more painful than a divide.) It's better to have a 1618 * solution that generates few collisions and still keeps things 1619 * relatively simple. 1620 */ 1621 bucket = hashval % dstate->dtds_hashsize; 1622 1623 if (op == DTRACE_DYNVAR_DEALLOC) { 1624 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1625 1626 for (;;) { 1627 while ((lock = *lockp) & 1) 1628 continue; 1629 1630 if (dtrace_casptr((void *)lockp, 1631 (void *)lock, (void *)(lock + 1)) == (void *)lock) 1632 break; 1633 } 1634 1635 dtrace_membar_producer(); 1636 } 1637 1638 top: 1639 prev = NULL; 1640 lock = hash[bucket].dtdh_lock; 1641 1642 dtrace_membar_consumer(); 1643 1644 start = hash[bucket].dtdh_chain; 1645 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1646 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1647 op != DTRACE_DYNVAR_DEALLOC)); 1648 1649 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1650 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1651 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1652 1653 if (dvar->dtdv_hashval != hashval) { 1654 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1655 /* 1656 * We've reached the sink, and therefore the 1657 * end of the hash chain; we can kick out of 1658 * the loop knowing that we have seen a valid 1659 * snapshot of state. 1660 */ 1661 ASSERT(dvar->dtdv_next == NULL); 1662 ASSERT(dvar == &dtrace_dynhash_sink); 1663 break; 1664 } 1665 1666 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1667 /* 1668 * We've gone off the rails: somewhere along 1669 * the line, one of the members of this hash 1670 * chain was deleted. Note that we could also 1671 * detect this by simply letting this loop run 1672 * to completion, as we would eventually hit 1673 * the end of the dirty list. However, we 1674 * want to avoid running the length of the 1675 * dirty list unnecessarily (it might be quite 1676 * long), so we catch this as early as 1677 * possible by detecting the hash marker. In 1678 * this case, we simply set dvar to NULL and 1679 * break; the conditional after the loop will 1680 * send us back to top. 1681 */ 1682 dvar = NULL; 1683 break; 1684 } 1685 1686 goto next; 1687 } 1688 1689 if (dtuple->dtt_nkeys != nkeys) 1690 goto next; 1691 1692 for (i = 0; i < nkeys; i++, dkey++) { 1693 if (dkey->dttk_size != key[i].dttk_size) 1694 goto next; /* size or type mismatch */ 1695 1696 if (dkey->dttk_size != 0) { 1697 if (dtrace_bcmp( 1698 (void *)(uintptr_t)key[i].dttk_value, 1699 (void *)(uintptr_t)dkey->dttk_value, 1700 dkey->dttk_size)) 1701 goto next; 1702 } else { 1703 if (dkey->dttk_value != key[i].dttk_value) 1704 goto next; 1705 } 1706 } 1707 1708 if (op != DTRACE_DYNVAR_DEALLOC) 1709 return (dvar); 1710 1711 ASSERT(dvar->dtdv_next == NULL || 1712 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1713 1714 if (prev != NULL) { 1715 ASSERT(hash[bucket].dtdh_chain != dvar); 1716 ASSERT(start != dvar); 1717 ASSERT(prev->dtdv_next == dvar); 1718 prev->dtdv_next = dvar->dtdv_next; 1719 } else { 1720 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1721 start, dvar->dtdv_next) != start) { 1722 /* 1723 * We have failed to atomically swing the 1724 * hash table head pointer, presumably because 1725 * of a conflicting allocation on another CPU. 1726 * We need to reread the hash chain and try 1727 * again. 1728 */ 1729 goto top; 1730 } 1731 } 1732 1733 dtrace_membar_producer(); 1734 1735 /* 1736 * Now set the hash value to indicate that it's free. 1737 */ 1738 ASSERT(hash[bucket].dtdh_chain != dvar); 1739 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1740 1741 dtrace_membar_producer(); 1742 1743 /* 1744 * Set the next pointer to point at the dirty list, and 1745 * atomically swing the dirty pointer to the newly freed dvar. 1746 */ 1747 do { 1748 next = dcpu->dtdsc_dirty; 1749 dvar->dtdv_next = next; 1750 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1751 1752 /* 1753 * Finally, unlock this hash bucket. 1754 */ 1755 ASSERT(hash[bucket].dtdh_lock == lock); 1756 ASSERT(lock & 1); 1757 hash[bucket].dtdh_lock++; 1758 1759 return (NULL); 1760 next: 1761 prev = dvar; 1762 continue; 1763 } 1764 1765 if (dvar == NULL) { 1766 /* 1767 * If dvar is NULL, it is because we went off the rails: 1768 * one of the elements that we traversed in the hash chain 1769 * was deleted while we were traversing it. In this case, 1770 * we assert that we aren't doing a dealloc (deallocs lock 1771 * the hash bucket to prevent themselves from racing with 1772 * one another), and retry the hash chain traversal. 1773 */ 1774 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1775 goto top; 1776 } 1777 1778 if (op != DTRACE_DYNVAR_ALLOC) { 1779 /* 1780 * If we are not to allocate a new variable, we want to 1781 * return NULL now. Before we return, check that the value 1782 * of the lock word hasn't changed. If it has, we may have 1783 * seen an inconsistent snapshot. 1784 */ 1785 if (op == DTRACE_DYNVAR_NOALLOC) { 1786 if (hash[bucket].dtdh_lock != lock) 1787 goto top; 1788 } else { 1789 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1790 ASSERT(hash[bucket].dtdh_lock == lock); 1791 ASSERT(lock & 1); 1792 hash[bucket].dtdh_lock++; 1793 } 1794 1795 return (NULL); 1796 } 1797 1798 /* 1799 * We need to allocate a new dynamic variable. The size we need is the 1800 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1801 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1802 * the size of any referred-to data (dsize). We then round the final 1803 * size up to the chunksize for allocation. 1804 */ 1805 for (ksize = 0, i = 0; i < nkeys; i++) 1806 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1807 1808 /* 1809 * This should be pretty much impossible, but could happen if, say, 1810 * strange DIF specified the tuple. Ideally, this should be an 1811 * assertion and not an error condition -- but that requires that the 1812 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1813 * bullet-proof. (That is, it must not be able to be fooled by 1814 * malicious DIF.) Given the lack of backwards branches in DIF, 1815 * solving this would presumably not amount to solving the Halting 1816 * Problem -- but it still seems awfully hard. 1817 */ 1818 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1819 ksize + dsize > chunksize) { 1820 dcpu->dtdsc_drops++; 1821 return (NULL); 1822 } 1823 1824 nstate = DTRACE_DSTATE_EMPTY; 1825 1826 do { 1827 retry: 1828 free = dcpu->dtdsc_free; 1829 1830 if (free == NULL) { 1831 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1832 void *rval; 1833 1834 if (clean == NULL) { 1835 /* 1836 * We're out of dynamic variable space on 1837 * this CPU. Unless we have tried all CPUs, 1838 * we'll try to allocate from a different 1839 * CPU. 1840 */ 1841 switch (dstate->dtds_state) { 1842 case DTRACE_DSTATE_CLEAN: { 1843 void *sp = &dstate->dtds_state; 1844 1845 if (++cpu >= NCPU) 1846 cpu = 0; 1847 1848 if (dcpu->dtdsc_dirty != NULL && 1849 nstate == DTRACE_DSTATE_EMPTY) 1850 nstate = DTRACE_DSTATE_DIRTY; 1851 1852 if (dcpu->dtdsc_rinsing != NULL) 1853 nstate = DTRACE_DSTATE_RINSING; 1854 1855 dcpu = &dstate->dtds_percpu[cpu]; 1856 1857 if (cpu != me) 1858 goto retry; 1859 1860 (void) dtrace_cas32(sp, 1861 DTRACE_DSTATE_CLEAN, nstate); 1862 1863 /* 1864 * To increment the correct bean 1865 * counter, take another lap. 1866 */ 1867 goto retry; 1868 } 1869 1870 case DTRACE_DSTATE_DIRTY: 1871 dcpu->dtdsc_dirty_drops++; 1872 break; 1873 1874 case DTRACE_DSTATE_RINSING: 1875 dcpu->dtdsc_rinsing_drops++; 1876 break; 1877 1878 case DTRACE_DSTATE_EMPTY: 1879 dcpu->dtdsc_drops++; 1880 break; 1881 } 1882 1883 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1884 return (NULL); 1885 } 1886 1887 /* 1888 * The clean list appears to be non-empty. We want to 1889 * move the clean list to the free list; we start by 1890 * moving the clean pointer aside. 1891 */ 1892 if (dtrace_casptr(&dcpu->dtdsc_clean, 1893 clean, NULL) != clean) { 1894 /* 1895 * We are in one of two situations: 1896 * 1897 * (a) The clean list was switched to the 1898 * free list by another CPU. 1899 * 1900 * (b) The clean list was added to by the 1901 * cleansing cyclic. 1902 * 1903 * In either of these situations, we can 1904 * just reattempt the free list allocation. 1905 */ 1906 goto retry; 1907 } 1908 1909 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1910 1911 /* 1912 * Now we'll move the clean list to our free list. 1913 * It's impossible for this to fail: the only way 1914 * the free list can be updated is through this 1915 * code path, and only one CPU can own the clean list. 1916 * Thus, it would only be possible for this to fail if 1917 * this code were racing with dtrace_dynvar_clean(). 1918 * (That is, if dtrace_dynvar_clean() updated the clean 1919 * list, and we ended up racing to update the free 1920 * list.) This race is prevented by the dtrace_sync() 1921 * in dtrace_dynvar_clean() -- which flushes the 1922 * owners of the clean lists out before resetting 1923 * the clean lists. 1924 */ 1925 dcpu = &dstate->dtds_percpu[me]; 1926 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1927 ASSERT(rval == NULL); 1928 goto retry; 1929 } 1930 1931 dvar = free; 1932 new_free = dvar->dtdv_next; 1933 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1934 1935 /* 1936 * We have now allocated a new chunk. We copy the tuple keys into the 1937 * tuple array and copy any referenced key data into the data space 1938 * following the tuple array. As we do this, we relocate dttk_value 1939 * in the final tuple to point to the key data address in the chunk. 1940 */ 1941 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1942 dvar->dtdv_data = (void *)(kdata + ksize); 1943 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1944 1945 for (i = 0; i < nkeys; i++) { 1946 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1947 size_t kesize = key[i].dttk_size; 1948 1949 if (kesize != 0) { 1950 dtrace_bcopy( 1951 (const void *)(uintptr_t)key[i].dttk_value, 1952 (void *)kdata, kesize); 1953 dkey->dttk_value = kdata; 1954 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1955 } else { 1956 dkey->dttk_value = key[i].dttk_value; 1957 } 1958 1959 dkey->dttk_size = kesize; 1960 } 1961 1962 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1963 dvar->dtdv_hashval = hashval; 1964 dvar->dtdv_next = start; 1965 1966 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1967 return (dvar); 1968 1969 /* 1970 * The cas has failed. Either another CPU is adding an element to 1971 * this hash chain, or another CPU is deleting an element from this 1972 * hash chain. The simplest way to deal with both of these cases 1973 * (though not necessarily the most efficient) is to free our 1974 * allocated block and tail-call ourselves. Note that the free is 1975 * to the dirty list and _not_ to the free list. This is to prevent 1976 * races with allocators, above. 1977 */ 1978 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1979 1980 dtrace_membar_producer(); 1981 1982 do { 1983 free = dcpu->dtdsc_dirty; 1984 dvar->dtdv_next = free; 1985 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1986 1987 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 1988 } 1989 1990 /*ARGSUSED*/ 1991 static void 1992 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1993 { 1994 if ((int64_t)nval < (int64_t)*oval) 1995 *oval = nval; 1996 } 1997 1998 /*ARGSUSED*/ 1999 static void 2000 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2001 { 2002 if ((int64_t)nval > (int64_t)*oval) 2003 *oval = nval; 2004 } 2005 2006 static void 2007 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2008 { 2009 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2010 int64_t val = (int64_t)nval; 2011 2012 if (val < 0) { 2013 for (i = 0; i < zero; i++) { 2014 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2015 quanta[i] += incr; 2016 return; 2017 } 2018 } 2019 } else { 2020 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2021 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2022 quanta[i - 1] += incr; 2023 return; 2024 } 2025 } 2026 2027 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2028 return; 2029 } 2030 2031 ASSERT(0); 2032 } 2033 2034 static void 2035 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2036 { 2037 uint64_t arg = *lquanta++; 2038 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2039 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2040 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2041 int32_t val = (int32_t)nval, level; 2042 2043 ASSERT(step != 0); 2044 ASSERT(levels != 0); 2045 2046 if (val < base) { 2047 /* 2048 * This is an underflow. 2049 */ 2050 lquanta[0] += incr; 2051 return; 2052 } 2053 2054 level = (val - base) / step; 2055 2056 if (level < levels) { 2057 lquanta[level + 1] += incr; 2058 return; 2059 } 2060 2061 /* 2062 * This is an overflow. 2063 */ 2064 lquanta[levels + 1] += incr; 2065 } 2066 2067 static int 2068 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2069 uint16_t high, uint16_t nsteps, int64_t value) 2070 { 2071 int64_t this = 1, last, next; 2072 int base = 1, order; 2073 2074 ASSERT(factor <= nsteps); 2075 ASSERT(nsteps % factor == 0); 2076 2077 for (order = 0; order < low; order++) 2078 this *= factor; 2079 2080 /* 2081 * If our value is less than our factor taken to the power of the 2082 * low order of magnitude, it goes into the zeroth bucket. 2083 */ 2084 if (value < (last = this)) 2085 return (0); 2086 2087 for (this *= factor; order <= high; order++) { 2088 int nbuckets = this > nsteps ? nsteps : this; 2089 2090 if ((next = this * factor) < this) { 2091 /* 2092 * We should not generally get log/linear quantizations 2093 * with a high magnitude that allows 64-bits to 2094 * overflow, but we nonetheless protect against this 2095 * by explicitly checking for overflow, and clamping 2096 * our value accordingly. 2097 */ 2098 value = this - 1; 2099 } 2100 2101 if (value < this) { 2102 /* 2103 * If our value lies within this order of magnitude, 2104 * determine its position by taking the offset within 2105 * the order of magnitude, dividing by the bucket 2106 * width, and adding to our (accumulated) base. 2107 */ 2108 return (base + (value - last) / (this / nbuckets)); 2109 } 2110 2111 base += nbuckets - (nbuckets / factor); 2112 last = this; 2113 this = next; 2114 } 2115 2116 /* 2117 * Our value is greater than or equal to our factor taken to the 2118 * power of one plus the high magnitude -- return the top bucket. 2119 */ 2120 return (base); 2121 } 2122 2123 static void 2124 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2125 { 2126 uint64_t arg = *llquanta++; 2127 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2128 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2129 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2130 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2131 2132 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2133 low, high, nsteps, nval)] += incr; 2134 } 2135 2136 /*ARGSUSED*/ 2137 static void 2138 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2139 { 2140 data[0]++; 2141 data[1] += nval; 2142 } 2143 2144 /*ARGSUSED*/ 2145 static void 2146 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2147 { 2148 int64_t snval = (int64_t)nval; 2149 uint64_t tmp[2]; 2150 2151 data[0]++; 2152 data[1] += nval; 2153 2154 /* 2155 * What we want to say here is: 2156 * 2157 * data[2] += nval * nval; 2158 * 2159 * But given that nval is 64-bit, we could easily overflow, so 2160 * we do this as 128-bit arithmetic. 2161 */ 2162 if (snval < 0) 2163 snval = -snval; 2164 2165 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2166 dtrace_add_128(data + 2, tmp, data + 2); 2167 } 2168 2169 /*ARGSUSED*/ 2170 static void 2171 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2172 { 2173 *oval = *oval + 1; 2174 } 2175 2176 /*ARGSUSED*/ 2177 static void 2178 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2179 { 2180 *oval += nval; 2181 } 2182 2183 /* 2184 * Aggregate given the tuple in the principal data buffer, and the aggregating 2185 * action denoted by the specified dtrace_aggregation_t. The aggregation 2186 * buffer is specified as the buf parameter. This routine does not return 2187 * failure; if there is no space in the aggregation buffer, the data will be 2188 * dropped, and a corresponding counter incremented. 2189 */ 2190 static void 2191 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2192 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2193 { 2194 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2195 uint32_t i, ndx, size, fsize; 2196 uint32_t align = sizeof (uint64_t) - 1; 2197 dtrace_aggbuffer_t *agb; 2198 dtrace_aggkey_t *key; 2199 uint32_t hashval = 0, limit, isstr; 2200 caddr_t tomax, data, kdata; 2201 dtrace_actkind_t action; 2202 dtrace_action_t *act; 2203 uintptr_t offs; 2204 2205 if (buf == NULL) 2206 return; 2207 2208 if (!agg->dtag_hasarg) { 2209 /* 2210 * Currently, only quantize() and lquantize() take additional 2211 * arguments, and they have the same semantics: an increment 2212 * value that defaults to 1 when not present. If additional 2213 * aggregating actions take arguments, the setting of the 2214 * default argument value will presumably have to become more 2215 * sophisticated... 2216 */ 2217 arg = 1; 2218 } 2219 2220 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2221 size = rec->dtrd_offset - agg->dtag_base; 2222 fsize = size + rec->dtrd_size; 2223 2224 ASSERT(dbuf->dtb_tomax != NULL); 2225 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2226 2227 if ((tomax = buf->dtb_tomax) == NULL) { 2228 dtrace_buffer_drop(buf); 2229 return; 2230 } 2231 2232 /* 2233 * The metastructure is always at the bottom of the buffer. 2234 */ 2235 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2236 sizeof (dtrace_aggbuffer_t)); 2237 2238 if (buf->dtb_offset == 0) { 2239 /* 2240 * We just kludge up approximately 1/8th of the size to be 2241 * buckets. If this guess ends up being routinely 2242 * off-the-mark, we may need to dynamically readjust this 2243 * based on past performance. 2244 */ 2245 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2246 2247 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2248 (uintptr_t)tomax || hashsize == 0) { 2249 /* 2250 * We've been given a ludicrously small buffer; 2251 * increment our drop count and leave. 2252 */ 2253 dtrace_buffer_drop(buf); 2254 return; 2255 } 2256 2257 /* 2258 * And now, a pathetic attempt to try to get a an odd (or 2259 * perchance, a prime) hash size for better hash distribution. 2260 */ 2261 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2262 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2263 2264 agb->dtagb_hashsize = hashsize; 2265 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2266 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2267 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2268 2269 for (i = 0; i < agb->dtagb_hashsize; i++) 2270 agb->dtagb_hash[i] = NULL; 2271 } 2272 2273 ASSERT(agg->dtag_first != NULL); 2274 ASSERT(agg->dtag_first->dta_intuple); 2275 2276 /* 2277 * Calculate the hash value based on the key. Note that we _don't_ 2278 * include the aggid in the hashing (but we will store it as part of 2279 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2280 * algorithm: a simple, quick algorithm that has no known funnels, and 2281 * gets good distribution in practice. The efficacy of the hashing 2282 * algorithm (and a comparison with other algorithms) may be found by 2283 * running the ::dtrace_aggstat MDB dcmd. 2284 */ 2285 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2286 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2287 limit = i + act->dta_rec.dtrd_size; 2288 ASSERT(limit <= size); 2289 isstr = DTRACEACT_ISSTRING(act); 2290 2291 for (; i < limit; i++) { 2292 hashval += data[i]; 2293 hashval += (hashval << 10); 2294 hashval ^= (hashval >> 6); 2295 2296 if (isstr && data[i] == '\0') 2297 break; 2298 } 2299 } 2300 2301 hashval += (hashval << 3); 2302 hashval ^= (hashval >> 11); 2303 hashval += (hashval << 15); 2304 2305 /* 2306 * Yes, the divide here is expensive -- but it's generally the least 2307 * of the performance issues given the amount of data that we iterate 2308 * over to compute hash values, compare data, etc. 2309 */ 2310 ndx = hashval % agb->dtagb_hashsize; 2311 2312 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2313 ASSERT((caddr_t)key >= tomax); 2314 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2315 2316 if (hashval != key->dtak_hashval || key->dtak_size != size) 2317 continue; 2318 2319 kdata = key->dtak_data; 2320 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2321 2322 for (act = agg->dtag_first; act->dta_intuple; 2323 act = act->dta_next) { 2324 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2325 limit = i + act->dta_rec.dtrd_size; 2326 ASSERT(limit <= size); 2327 isstr = DTRACEACT_ISSTRING(act); 2328 2329 for (; i < limit; i++) { 2330 if (kdata[i] != data[i]) 2331 goto next; 2332 2333 if (isstr && data[i] == '\0') 2334 break; 2335 } 2336 } 2337 2338 if (action != key->dtak_action) { 2339 /* 2340 * We are aggregating on the same value in the same 2341 * aggregation with two different aggregating actions. 2342 * (This should have been picked up in the compiler, 2343 * so we may be dealing with errant or devious DIF.) 2344 * This is an error condition; we indicate as much, 2345 * and return. 2346 */ 2347 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2348 return; 2349 } 2350 2351 /* 2352 * This is a hit: we need to apply the aggregator to 2353 * the value at this key. 2354 */ 2355 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2356 return; 2357 next: 2358 continue; 2359 } 2360 2361 /* 2362 * We didn't find it. We need to allocate some zero-filled space, 2363 * link it into the hash table appropriately, and apply the aggregator 2364 * to the (zero-filled) value. 2365 */ 2366 offs = buf->dtb_offset; 2367 while (offs & (align - 1)) 2368 offs += sizeof (uint32_t); 2369 2370 /* 2371 * If we don't have enough room to both allocate a new key _and_ 2372 * its associated data, increment the drop count and return. 2373 */ 2374 if ((uintptr_t)tomax + offs + fsize > 2375 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2376 dtrace_buffer_drop(buf); 2377 return; 2378 } 2379 2380 /*CONSTCOND*/ 2381 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2382 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2383 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2384 2385 key->dtak_data = kdata = tomax + offs; 2386 buf->dtb_offset = offs + fsize; 2387 2388 /* 2389 * Now copy the data across. 2390 */ 2391 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2392 2393 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2394 kdata[i] = data[i]; 2395 2396 /* 2397 * Because strings are not zeroed out by default, we need to iterate 2398 * looking for actions that store strings, and we need to explicitly 2399 * pad these strings out with zeroes. 2400 */ 2401 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2402 int nul; 2403 2404 if (!DTRACEACT_ISSTRING(act)) 2405 continue; 2406 2407 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2408 limit = i + act->dta_rec.dtrd_size; 2409 ASSERT(limit <= size); 2410 2411 for (nul = 0; i < limit; i++) { 2412 if (nul) { 2413 kdata[i] = '\0'; 2414 continue; 2415 } 2416 2417 if (data[i] != '\0') 2418 continue; 2419 2420 nul = 1; 2421 } 2422 } 2423 2424 for (i = size; i < fsize; i++) 2425 kdata[i] = 0; 2426 2427 key->dtak_hashval = hashval; 2428 key->dtak_size = size; 2429 key->dtak_action = action; 2430 key->dtak_next = agb->dtagb_hash[ndx]; 2431 agb->dtagb_hash[ndx] = key; 2432 2433 /* 2434 * Finally, apply the aggregator. 2435 */ 2436 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2437 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2438 } 2439 2440 /* 2441 * Given consumer state, this routine finds a speculation in the INACTIVE 2442 * state and transitions it into the ACTIVE state. If there is no speculation 2443 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2444 * incremented -- it is up to the caller to take appropriate action. 2445 */ 2446 static int 2447 dtrace_speculation(dtrace_state_t *state) 2448 { 2449 int i = 0; 2450 dtrace_speculation_state_t current; 2451 uint32_t *stat = &state->dts_speculations_unavail, count; 2452 2453 while (i < state->dts_nspeculations) { 2454 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2455 2456 current = spec->dtsp_state; 2457 2458 if (current != DTRACESPEC_INACTIVE) { 2459 if (current == DTRACESPEC_COMMITTINGMANY || 2460 current == DTRACESPEC_COMMITTING || 2461 current == DTRACESPEC_DISCARDING) 2462 stat = &state->dts_speculations_busy; 2463 i++; 2464 continue; 2465 } 2466 2467 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2468 current, DTRACESPEC_ACTIVE) == current) 2469 return (i + 1); 2470 } 2471 2472 /* 2473 * We couldn't find a speculation. If we found as much as a single 2474 * busy speculation buffer, we'll attribute this failure as "busy" 2475 * instead of "unavail". 2476 */ 2477 do { 2478 count = *stat; 2479 } while (dtrace_cas32(stat, count, count + 1) != count); 2480 2481 return (0); 2482 } 2483 2484 /* 2485 * This routine commits an active speculation. If the specified speculation 2486 * is not in a valid state to perform a commit(), this routine will silently do 2487 * nothing. The state of the specified speculation is transitioned according 2488 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2489 */ 2490 static void 2491 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2492 dtrace_specid_t which) 2493 { 2494 dtrace_speculation_t *spec; 2495 dtrace_buffer_t *src, *dest; 2496 uintptr_t daddr, saddr, dlimit; 2497 dtrace_speculation_state_t current, new; 2498 intptr_t offs; 2499 2500 if (which == 0) 2501 return; 2502 2503 if (which > state->dts_nspeculations) { 2504 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2505 return; 2506 } 2507 2508 spec = &state->dts_speculations[which - 1]; 2509 src = &spec->dtsp_buffer[cpu]; 2510 dest = &state->dts_buffer[cpu]; 2511 2512 do { 2513 current = spec->dtsp_state; 2514 2515 if (current == DTRACESPEC_COMMITTINGMANY) 2516 break; 2517 2518 switch (current) { 2519 case DTRACESPEC_INACTIVE: 2520 case DTRACESPEC_DISCARDING: 2521 return; 2522 2523 case DTRACESPEC_COMMITTING: 2524 /* 2525 * This is only possible if we are (a) commit()'ing 2526 * without having done a prior speculate() on this CPU 2527 * and (b) racing with another commit() on a different 2528 * CPU. There's nothing to do -- we just assert that 2529 * our offset is 0. 2530 */ 2531 ASSERT(src->dtb_offset == 0); 2532 return; 2533 2534 case DTRACESPEC_ACTIVE: 2535 new = DTRACESPEC_COMMITTING; 2536 break; 2537 2538 case DTRACESPEC_ACTIVEONE: 2539 /* 2540 * This speculation is active on one CPU. If our 2541 * buffer offset is non-zero, we know that the one CPU 2542 * must be us. Otherwise, we are committing on a 2543 * different CPU from the speculate(), and we must 2544 * rely on being asynchronously cleaned. 2545 */ 2546 if (src->dtb_offset != 0) { 2547 new = DTRACESPEC_COMMITTING; 2548 break; 2549 } 2550 /*FALLTHROUGH*/ 2551 2552 case DTRACESPEC_ACTIVEMANY: 2553 new = DTRACESPEC_COMMITTINGMANY; 2554 break; 2555 2556 default: 2557 ASSERT(0); 2558 } 2559 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2560 current, new) != current); 2561 2562 /* 2563 * We have set the state to indicate that we are committing this 2564 * speculation. Now reserve the necessary space in the destination 2565 * buffer. 2566 */ 2567 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2568 sizeof (uint64_t), state, NULL)) < 0) { 2569 dtrace_buffer_drop(dest); 2570 goto out; 2571 } 2572 2573 /* 2574 * We have the space; copy the buffer across. (Note that this is a 2575 * highly subobtimal bcopy(); in the unlikely event that this becomes 2576 * a serious performance issue, a high-performance DTrace-specific 2577 * bcopy() should obviously be invented.) 2578 */ 2579 daddr = (uintptr_t)dest->dtb_tomax + offs; 2580 dlimit = daddr + src->dtb_offset; 2581 saddr = (uintptr_t)src->dtb_tomax; 2582 2583 /* 2584 * First, the aligned portion. 2585 */ 2586 while (dlimit - daddr >= sizeof (uint64_t)) { 2587 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2588 2589 daddr += sizeof (uint64_t); 2590 saddr += sizeof (uint64_t); 2591 } 2592 2593 /* 2594 * Now any left-over bit... 2595 */ 2596 while (dlimit - daddr) 2597 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2598 2599 /* 2600 * Finally, commit the reserved space in the destination buffer. 2601 */ 2602 dest->dtb_offset = offs + src->dtb_offset; 2603 2604 out: 2605 /* 2606 * If we're lucky enough to be the only active CPU on this speculation 2607 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2608 */ 2609 if (current == DTRACESPEC_ACTIVE || 2610 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2611 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2612 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2613 2614 ASSERT(rval == DTRACESPEC_COMMITTING); 2615 } 2616 2617 src->dtb_offset = 0; 2618 src->dtb_xamot_drops += src->dtb_drops; 2619 src->dtb_drops = 0; 2620 } 2621 2622 /* 2623 * This routine discards an active speculation. If the specified speculation 2624 * is not in a valid state to perform a discard(), this routine will silently 2625 * do nothing. The state of the specified speculation is transitioned 2626 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2627 */ 2628 static void 2629 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2630 dtrace_specid_t which) 2631 { 2632 dtrace_speculation_t *spec; 2633 dtrace_speculation_state_t current, new; 2634 dtrace_buffer_t *buf; 2635 2636 if (which == 0) 2637 return; 2638 2639 if (which > state->dts_nspeculations) { 2640 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2641 return; 2642 } 2643 2644 spec = &state->dts_speculations[which - 1]; 2645 buf = &spec->dtsp_buffer[cpu]; 2646 2647 do { 2648 current = spec->dtsp_state; 2649 2650 switch (current) { 2651 case DTRACESPEC_INACTIVE: 2652 case DTRACESPEC_COMMITTINGMANY: 2653 case DTRACESPEC_COMMITTING: 2654 case DTRACESPEC_DISCARDING: 2655 return; 2656 2657 case DTRACESPEC_ACTIVE: 2658 case DTRACESPEC_ACTIVEMANY: 2659 new = DTRACESPEC_DISCARDING; 2660 break; 2661 2662 case DTRACESPEC_ACTIVEONE: 2663 if (buf->dtb_offset != 0) { 2664 new = DTRACESPEC_INACTIVE; 2665 } else { 2666 new = DTRACESPEC_DISCARDING; 2667 } 2668 break; 2669 2670 default: 2671 ASSERT(0); 2672 } 2673 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2674 current, new) != current); 2675 2676 buf->dtb_offset = 0; 2677 buf->dtb_drops = 0; 2678 } 2679 2680 /* 2681 * Note: not called from probe context. This function is called 2682 * asynchronously from cross call context to clean any speculations that are 2683 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2684 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2685 * speculation. 2686 */ 2687 static void 2688 dtrace_speculation_clean_here(dtrace_state_t *state) 2689 { 2690 dtrace_icookie_t cookie; 2691 processorid_t cpu = CPU->cpu_id; 2692 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2693 dtrace_specid_t i; 2694 2695 cookie = dtrace_interrupt_disable(); 2696 2697 if (dest->dtb_tomax == NULL) { 2698 dtrace_interrupt_enable(cookie); 2699 return; 2700 } 2701 2702 for (i = 0; i < state->dts_nspeculations; i++) { 2703 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2704 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2705 2706 if (src->dtb_tomax == NULL) 2707 continue; 2708 2709 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2710 src->dtb_offset = 0; 2711 continue; 2712 } 2713 2714 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2715 continue; 2716 2717 if (src->dtb_offset == 0) 2718 continue; 2719 2720 dtrace_speculation_commit(state, cpu, i + 1); 2721 } 2722 2723 dtrace_interrupt_enable(cookie); 2724 } 2725 2726 /* 2727 * Note: not called from probe context. This function is called 2728 * asynchronously (and at a regular interval) to clean any speculations that 2729 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2730 * is work to be done, it cross calls all CPUs to perform that work; 2731 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2732 * INACTIVE state until they have been cleaned by all CPUs. 2733 */ 2734 static void 2735 dtrace_speculation_clean(dtrace_state_t *state) 2736 { 2737 int work = 0, rv; 2738 dtrace_specid_t i; 2739 2740 for (i = 0; i < state->dts_nspeculations; i++) { 2741 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2742 2743 ASSERT(!spec->dtsp_cleaning); 2744 2745 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2746 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2747 continue; 2748 2749 work++; 2750 spec->dtsp_cleaning = 1; 2751 } 2752 2753 if (!work) 2754 return; 2755 2756 dtrace_xcall(DTRACE_CPUALL, 2757 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2758 2759 /* 2760 * We now know that all CPUs have committed or discarded their 2761 * speculation buffers, as appropriate. We can now set the state 2762 * to inactive. 2763 */ 2764 for (i = 0; i < state->dts_nspeculations; i++) { 2765 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2766 dtrace_speculation_state_t current, new; 2767 2768 if (!spec->dtsp_cleaning) 2769 continue; 2770 2771 current = spec->dtsp_state; 2772 ASSERT(current == DTRACESPEC_DISCARDING || 2773 current == DTRACESPEC_COMMITTINGMANY); 2774 2775 new = DTRACESPEC_INACTIVE; 2776 2777 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2778 ASSERT(rv == current); 2779 spec->dtsp_cleaning = 0; 2780 } 2781 } 2782 2783 /* 2784 * Called as part of a speculate() to get the speculative buffer associated 2785 * with a given speculation. Returns NULL if the specified speculation is not 2786 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2787 * the active CPU is not the specified CPU -- the speculation will be 2788 * atomically transitioned into the ACTIVEMANY state. 2789 */ 2790 static dtrace_buffer_t * 2791 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2792 dtrace_specid_t which) 2793 { 2794 dtrace_speculation_t *spec; 2795 dtrace_speculation_state_t current, new; 2796 dtrace_buffer_t *buf; 2797 2798 if (which == 0) 2799 return (NULL); 2800 2801 if (which > state->dts_nspeculations) { 2802 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2803 return (NULL); 2804 } 2805 2806 spec = &state->dts_speculations[which - 1]; 2807 buf = &spec->dtsp_buffer[cpuid]; 2808 2809 do { 2810 current = spec->dtsp_state; 2811 2812 switch (current) { 2813 case DTRACESPEC_INACTIVE: 2814 case DTRACESPEC_COMMITTINGMANY: 2815 case DTRACESPEC_DISCARDING: 2816 return (NULL); 2817 2818 case DTRACESPEC_COMMITTING: 2819 ASSERT(buf->dtb_offset == 0); 2820 return (NULL); 2821 2822 case DTRACESPEC_ACTIVEONE: 2823 /* 2824 * This speculation is currently active on one CPU. 2825 * Check the offset in the buffer; if it's non-zero, 2826 * that CPU must be us (and we leave the state alone). 2827 * If it's zero, assume that we're starting on a new 2828 * CPU -- and change the state to indicate that the 2829 * speculation is active on more than one CPU. 2830 */ 2831 if (buf->dtb_offset != 0) 2832 return (buf); 2833 2834 new = DTRACESPEC_ACTIVEMANY; 2835 break; 2836 2837 case DTRACESPEC_ACTIVEMANY: 2838 return (buf); 2839 2840 case DTRACESPEC_ACTIVE: 2841 new = DTRACESPEC_ACTIVEONE; 2842 break; 2843 2844 default: 2845 ASSERT(0); 2846 } 2847 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2848 current, new) != current); 2849 2850 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2851 return (buf); 2852 } 2853 2854 /* 2855 * Return a string. In the event that the user lacks the privilege to access 2856 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2857 * don't fail access checking. 2858 * 2859 * dtrace_dif_variable() uses this routine as a helper for various 2860 * builtin values such as 'execname' and 'probefunc.' 2861 */ 2862 uintptr_t 2863 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 2864 dtrace_mstate_t *mstate) 2865 { 2866 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2867 uintptr_t ret; 2868 size_t strsz; 2869 2870 /* 2871 * The easy case: this probe is allowed to read all of memory, so 2872 * we can just return this as a vanilla pointer. 2873 */ 2874 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 2875 return (addr); 2876 2877 /* 2878 * This is the tougher case: we copy the string in question from 2879 * kernel memory into scratch memory and return it that way: this 2880 * ensures that we won't trip up when access checking tests the 2881 * BYREF return value. 2882 */ 2883 strsz = dtrace_strlen((char *)addr, size) + 1; 2884 2885 if (mstate->dtms_scratch_ptr + strsz > 2886 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2887 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2888 return (NULL); 2889 } 2890 2891 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2892 strsz); 2893 ret = mstate->dtms_scratch_ptr; 2894 mstate->dtms_scratch_ptr += strsz; 2895 return (ret); 2896 } 2897 2898 /* 2899 * This function implements the DIF emulator's variable lookups. The emulator 2900 * passes a reserved variable identifier and optional built-in array index. 2901 */ 2902 static uint64_t 2903 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2904 uint64_t ndx) 2905 { 2906 /* 2907 * If we're accessing one of the uncached arguments, we'll turn this 2908 * into a reference in the args array. 2909 */ 2910 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2911 ndx = v - DIF_VAR_ARG0; 2912 v = DIF_VAR_ARGS; 2913 } 2914 2915 switch (v) { 2916 case DIF_VAR_ARGS: 2917 if (!(mstate->dtms_access & DTRACE_ACCESS_ARGS)) { 2918 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= 2919 CPU_DTRACE_KPRIV; 2920 return (0); 2921 } 2922 2923 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2924 if (ndx >= sizeof (mstate->dtms_arg) / 2925 sizeof (mstate->dtms_arg[0])) { 2926 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2927 dtrace_provider_t *pv; 2928 uint64_t val; 2929 2930 pv = mstate->dtms_probe->dtpr_provider; 2931 if (pv->dtpv_pops.dtps_getargval != NULL) 2932 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2933 mstate->dtms_probe->dtpr_id, 2934 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2935 else 2936 val = dtrace_getarg(ndx, aframes); 2937 2938 /* 2939 * This is regrettably required to keep the compiler 2940 * from tail-optimizing the call to dtrace_getarg(). 2941 * The condition always evaluates to true, but the 2942 * compiler has no way of figuring that out a priori. 2943 * (None of this would be necessary if the compiler 2944 * could be relied upon to _always_ tail-optimize 2945 * the call to dtrace_getarg() -- but it can't.) 2946 */ 2947 if (mstate->dtms_probe != NULL) 2948 return (val); 2949 2950 ASSERT(0); 2951 } 2952 2953 return (mstate->dtms_arg[ndx]); 2954 2955 case DIF_VAR_UREGS: { 2956 klwp_t *lwp; 2957 2958 if (!dtrace_priv_proc(state, mstate)) 2959 return (0); 2960 2961 if ((lwp = curthread->t_lwp) == NULL) { 2962 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2963 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL; 2964 return (0); 2965 } 2966 2967 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2968 } 2969 2970 case DIF_VAR_VMREGS: { 2971 uint64_t rval; 2972 2973 if (!dtrace_priv_kernel(state)) 2974 return (0); 2975 2976 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2977 2978 rval = dtrace_getvmreg(ndx, 2979 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags); 2980 2981 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2982 2983 return (rval); 2984 } 2985 2986 case DIF_VAR_CURTHREAD: 2987 if (!dtrace_priv_proc(state, mstate)) 2988 return (0); 2989 return ((uint64_t)(uintptr_t)curthread); 2990 2991 case DIF_VAR_TIMESTAMP: 2992 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2993 mstate->dtms_timestamp = dtrace_gethrtime(); 2994 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2995 } 2996 return (mstate->dtms_timestamp); 2997 2998 case DIF_VAR_VTIMESTAMP: 2999 ASSERT(dtrace_vtime_references != 0); 3000 return (curthread->t_dtrace_vtime); 3001 3002 case DIF_VAR_WALLTIMESTAMP: 3003 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3004 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3005 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3006 } 3007 return (mstate->dtms_walltimestamp); 3008 3009 case DIF_VAR_IPL: 3010 if (!dtrace_priv_kernel(state)) 3011 return (0); 3012 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3013 mstate->dtms_ipl = dtrace_getipl(); 3014 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3015 } 3016 return (mstate->dtms_ipl); 3017 3018 case DIF_VAR_EPID: 3019 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3020 return (mstate->dtms_epid); 3021 3022 case DIF_VAR_ID: 3023 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3024 return (mstate->dtms_probe->dtpr_id); 3025 3026 case DIF_VAR_STACKDEPTH: 3027 if (!dtrace_priv_kernel(state)) 3028 return (0); 3029 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3030 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3031 3032 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3033 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3034 } 3035 return (mstate->dtms_stackdepth); 3036 3037 case DIF_VAR_USTACKDEPTH: 3038 if (!dtrace_priv_proc(state, mstate)) 3039 return (0); 3040 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3041 /* 3042 * See comment in DIF_VAR_PID. 3043 */ 3044 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3045 CPU_ON_INTR(CPU)) { 3046 mstate->dtms_ustackdepth = 0; 3047 } else { 3048 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3049 mstate->dtms_ustackdepth = 3050 dtrace_getustackdepth(); 3051 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3052 } 3053 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3054 } 3055 return (mstate->dtms_ustackdepth); 3056 3057 case DIF_VAR_CALLER: 3058 if (!dtrace_priv_kernel(state)) 3059 return (0); 3060 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3061 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3062 3063 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3064 /* 3065 * If this is an unanchored probe, we are 3066 * required to go through the slow path: 3067 * dtrace_caller() only guarantees correct 3068 * results for anchored probes. 3069 */ 3070 pc_t caller[2]; 3071 3072 dtrace_getpcstack(caller, 2, aframes, 3073 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3074 mstate->dtms_caller = caller[1]; 3075 } else if ((mstate->dtms_caller = 3076 dtrace_caller(aframes)) == -1) { 3077 /* 3078 * We have failed to do this the quick way; 3079 * we must resort to the slower approach of 3080 * calling dtrace_getpcstack(). 3081 */ 3082 pc_t caller; 3083 3084 dtrace_getpcstack(&caller, 1, aframes, NULL); 3085 mstate->dtms_caller = caller; 3086 } 3087 3088 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3089 } 3090 return (mstate->dtms_caller); 3091 3092 case DIF_VAR_UCALLER: 3093 if (!dtrace_priv_proc(state, mstate)) 3094 return (0); 3095 3096 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3097 uint64_t ustack[3]; 3098 3099 /* 3100 * dtrace_getupcstack() fills in the first uint64_t 3101 * with the current PID. The second uint64_t will 3102 * be the program counter at user-level. The third 3103 * uint64_t will contain the caller, which is what 3104 * we're after. 3105 */ 3106 ustack[2] = NULL; 3107 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3108 dtrace_getupcstack(ustack, 3); 3109 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3110 mstate->dtms_ucaller = ustack[2]; 3111 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3112 } 3113 3114 return (mstate->dtms_ucaller); 3115 3116 case DIF_VAR_PROBEPROV: 3117 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3118 return (dtrace_dif_varstr( 3119 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3120 state, mstate)); 3121 3122 case DIF_VAR_PROBEMOD: 3123 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3124 return (dtrace_dif_varstr( 3125 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3126 state, mstate)); 3127 3128 case DIF_VAR_PROBEFUNC: 3129 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3130 return (dtrace_dif_varstr( 3131 (uintptr_t)mstate->dtms_probe->dtpr_func, 3132 state, mstate)); 3133 3134 case DIF_VAR_PROBENAME: 3135 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3136 return (dtrace_dif_varstr( 3137 (uintptr_t)mstate->dtms_probe->dtpr_name, 3138 state, mstate)); 3139 3140 case DIF_VAR_PID: 3141 if (!dtrace_priv_proc(state, mstate)) 3142 return (0); 3143 3144 /* 3145 * Note that we are assuming that an unanchored probe is 3146 * always due to a high-level interrupt. (And we're assuming 3147 * that there is only a single high level interrupt.) 3148 */ 3149 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3150 return (pid0.pid_id); 3151 3152 /* 3153 * It is always safe to dereference one's own t_procp pointer: 3154 * it always points to a valid, allocated proc structure. 3155 * Further, it is always safe to dereference the p_pidp member 3156 * of one's own proc structure. (These are truisms becuase 3157 * threads and processes don't clean up their own state -- 3158 * they leave that task to whomever reaps them.) 3159 */ 3160 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3161 3162 case DIF_VAR_PPID: 3163 if (!dtrace_priv_proc(state, mstate)) 3164 return (0); 3165 3166 /* 3167 * See comment in DIF_VAR_PID. 3168 */ 3169 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3170 return (pid0.pid_id); 3171 3172 /* 3173 * It is always safe to dereference one's own t_procp pointer: 3174 * it always points to a valid, allocated proc structure. 3175 * (This is true because threads don't clean up their own 3176 * state -- they leave that task to whomever reaps them.) 3177 */ 3178 return ((uint64_t)curthread->t_procp->p_ppid); 3179 3180 case DIF_VAR_TID: 3181 /* 3182 * See comment in DIF_VAR_PID. 3183 */ 3184 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3185 return (0); 3186 3187 return ((uint64_t)curthread->t_tid); 3188 3189 case DIF_VAR_EXECNAME: 3190 if (!dtrace_priv_proc(state, mstate)) 3191 return (0); 3192 3193 /* 3194 * See comment in DIF_VAR_PID. 3195 */ 3196 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3197 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3198 3199 /* 3200 * It is always safe to dereference one's own t_procp pointer: 3201 * it always points to a valid, allocated proc structure. 3202 * (This is true because threads don't clean up their own 3203 * state -- they leave that task to whomever reaps them.) 3204 */ 3205 return (dtrace_dif_varstr( 3206 (uintptr_t)curthread->t_procp->p_user.u_comm, 3207 state, mstate)); 3208 3209 case DIF_VAR_ZONENAME: 3210 if (!dtrace_priv_proc(state, mstate)) 3211 return (0); 3212 3213 /* 3214 * See comment in DIF_VAR_PID. 3215 */ 3216 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3217 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3218 3219 /* 3220 * It is always safe to dereference one's own t_procp pointer: 3221 * it always points to a valid, allocated proc structure. 3222 * (This is true because threads don't clean up their own 3223 * state -- they leave that task to whomever reaps them.) 3224 */ 3225 return (dtrace_dif_varstr( 3226 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3227 state, mstate)); 3228 3229 case DIF_VAR_UID: 3230 if (!dtrace_priv_proc(state, mstate)) 3231 return (0); 3232 3233 /* 3234 * See comment in DIF_VAR_PID. 3235 */ 3236 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3237 return ((uint64_t)p0.p_cred->cr_uid); 3238 3239 /* 3240 * It is always safe to dereference one's own t_procp pointer: 3241 * it always points to a valid, allocated proc structure. 3242 * (This is true because threads don't clean up their own 3243 * state -- they leave that task to whomever reaps them.) 3244 * 3245 * Additionally, it is safe to dereference one's own process 3246 * credential, since this is never NULL after process birth. 3247 */ 3248 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3249 3250 case DIF_VAR_GID: 3251 if (!dtrace_priv_proc(state, mstate)) 3252 return (0); 3253 3254 /* 3255 * See comment in DIF_VAR_PID. 3256 */ 3257 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3258 return ((uint64_t)p0.p_cred->cr_gid); 3259 3260 /* 3261 * It is always safe to dereference one's own t_procp pointer: 3262 * it always points to a valid, allocated proc structure. 3263 * (This is true because threads don't clean up their own 3264 * state -- they leave that task to whomever reaps them.) 3265 * 3266 * Additionally, it is safe to dereference one's own process 3267 * credential, since this is never NULL after process birth. 3268 */ 3269 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3270 3271 case DIF_VAR_ERRNO: { 3272 klwp_t *lwp; 3273 if (!dtrace_priv_proc(state, mstate)) 3274 return (0); 3275 3276 /* 3277 * See comment in DIF_VAR_PID. 3278 */ 3279 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3280 return (0); 3281 3282 /* 3283 * It is always safe to dereference one's own t_lwp pointer in 3284 * the event that this pointer is non-NULL. (This is true 3285 * because threads and lwps don't clean up their own state -- 3286 * they leave that task to whomever reaps them.) 3287 */ 3288 if ((lwp = curthread->t_lwp) == NULL) 3289 return (0); 3290 3291 return ((uint64_t)lwp->lwp_errno); 3292 } 3293 default: 3294 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3295 return (0); 3296 } 3297 } 3298 3299 /* 3300 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 3301 * Notice that we don't bother validating the proper number of arguments or 3302 * their types in the tuple stack. This isn't needed because all argument 3303 * interpretation is safe because of our load safety -- the worst that can 3304 * happen is that a bogus program can obtain bogus results. 3305 */ 3306 static void 3307 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 3308 dtrace_key_t *tupregs, int nargs, 3309 dtrace_mstate_t *mstate, dtrace_state_t *state) 3310 { 3311 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 3312 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 3313 dtrace_vstate_t *vstate = &state->dts_vstate; 3314 3315 union { 3316 mutex_impl_t mi; 3317 uint64_t mx; 3318 } m; 3319 3320 union { 3321 krwlock_t ri; 3322 uintptr_t rw; 3323 } r; 3324 3325 switch (subr) { 3326 case DIF_SUBR_RAND: 3327 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 3328 break; 3329 3330 case DIF_SUBR_MUTEX_OWNED: 3331 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3332 mstate, vstate)) { 3333 regs[rd] = NULL; 3334 break; 3335 } 3336 3337 m.mx = dtrace_load64(tupregs[0].dttk_value); 3338 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3339 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3340 else 3341 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 3342 break; 3343 3344 case DIF_SUBR_MUTEX_OWNER: 3345 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3346 mstate, vstate)) { 3347 regs[rd] = NULL; 3348 break; 3349 } 3350 3351 m.mx = dtrace_load64(tupregs[0].dttk_value); 3352 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3353 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3354 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3355 else 3356 regs[rd] = 0; 3357 break; 3358 3359 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3360 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3361 mstate, vstate)) { 3362 regs[rd] = NULL; 3363 break; 3364 } 3365 3366 m.mx = dtrace_load64(tupregs[0].dttk_value); 3367 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3368 break; 3369 3370 case DIF_SUBR_MUTEX_TYPE_SPIN: 3371 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3372 mstate, vstate)) { 3373 regs[rd] = NULL; 3374 break; 3375 } 3376 3377 m.mx = dtrace_load64(tupregs[0].dttk_value); 3378 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3379 break; 3380 3381 case DIF_SUBR_RW_READ_HELD: { 3382 uintptr_t tmp; 3383 3384 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3385 mstate, vstate)) { 3386 regs[rd] = NULL; 3387 break; 3388 } 3389 3390 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3391 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 3392 break; 3393 } 3394 3395 case DIF_SUBR_RW_WRITE_HELD: 3396 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3397 mstate, vstate)) { 3398 regs[rd] = NULL; 3399 break; 3400 } 3401 3402 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3403 regs[rd] = _RW_WRITE_HELD(&r.ri); 3404 break; 3405 3406 case DIF_SUBR_RW_ISWRITER: 3407 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3408 mstate, vstate)) { 3409 regs[rd] = NULL; 3410 break; 3411 } 3412 3413 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3414 regs[rd] = _RW_ISWRITER(&r.ri); 3415 break; 3416 3417 case DIF_SUBR_BCOPY: { 3418 /* 3419 * We need to be sure that the destination is in the scratch 3420 * region -- no other region is allowed. 3421 */ 3422 uintptr_t src = tupregs[0].dttk_value; 3423 uintptr_t dest = tupregs[1].dttk_value; 3424 size_t size = tupregs[2].dttk_value; 3425 3426 if (!dtrace_inscratch(dest, size, mstate)) { 3427 *flags |= CPU_DTRACE_BADADDR; 3428 *illval = regs[rd]; 3429 break; 3430 } 3431 3432 if (!dtrace_canload(src, size, mstate, vstate)) { 3433 regs[rd] = NULL; 3434 break; 3435 } 3436 3437 dtrace_bcopy((void *)src, (void *)dest, size); 3438 break; 3439 } 3440 3441 case DIF_SUBR_ALLOCA: 3442 case DIF_SUBR_COPYIN: { 3443 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 3444 uint64_t size = 3445 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 3446 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 3447 3448 /* 3449 * This action doesn't require any credential checks since 3450 * probes will not activate in user contexts to which the 3451 * enabling user does not have permissions. 3452 */ 3453 3454 /* 3455 * Rounding up the user allocation size could have overflowed 3456 * a large, bogus allocation (like -1ULL) to 0. 3457 */ 3458 if (scratch_size < size || 3459 !DTRACE_INSCRATCH(mstate, scratch_size)) { 3460 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3461 regs[rd] = NULL; 3462 break; 3463 } 3464 3465 if (subr == DIF_SUBR_COPYIN) { 3466 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3467 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3468 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3469 } 3470 3471 mstate->dtms_scratch_ptr += scratch_size; 3472 regs[rd] = dest; 3473 break; 3474 } 3475 3476 case DIF_SUBR_COPYINTO: { 3477 uint64_t size = tupregs[1].dttk_value; 3478 uintptr_t dest = tupregs[2].dttk_value; 3479 3480 /* 3481 * This action doesn't require any credential checks since 3482 * probes will not activate in user contexts to which the 3483 * enabling user does not have permissions. 3484 */ 3485 if (!dtrace_inscratch(dest, size, mstate)) { 3486 *flags |= CPU_DTRACE_BADADDR; 3487 *illval = regs[rd]; 3488 break; 3489 } 3490 3491 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3492 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3493 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3494 break; 3495 } 3496 3497 case DIF_SUBR_COPYINSTR: { 3498 uintptr_t dest = mstate->dtms_scratch_ptr; 3499 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3500 3501 if (nargs > 1 && tupregs[1].dttk_value < size) 3502 size = tupregs[1].dttk_value + 1; 3503 3504 /* 3505 * This action doesn't require any credential checks since 3506 * probes will not activate in user contexts to which the 3507 * enabling user does not have permissions. 3508 */ 3509 if (!DTRACE_INSCRATCH(mstate, size)) { 3510 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3511 regs[rd] = NULL; 3512 break; 3513 } 3514 3515 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3516 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 3517 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3518 3519 ((char *)dest)[size - 1] = '\0'; 3520 mstate->dtms_scratch_ptr += size; 3521 regs[rd] = dest; 3522 break; 3523 } 3524 3525 case DIF_SUBR_MSGSIZE: 3526 case DIF_SUBR_MSGDSIZE: { 3527 uintptr_t baddr = tupregs[0].dttk_value, daddr; 3528 uintptr_t wptr, rptr; 3529 size_t count = 0; 3530 int cont = 0; 3531 3532 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3533 3534 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 3535 vstate)) { 3536 regs[rd] = NULL; 3537 break; 3538 } 3539 3540 wptr = dtrace_loadptr(baddr + 3541 offsetof(mblk_t, b_wptr)); 3542 3543 rptr = dtrace_loadptr(baddr + 3544 offsetof(mblk_t, b_rptr)); 3545 3546 if (wptr < rptr) { 3547 *flags |= CPU_DTRACE_BADADDR; 3548 *illval = tupregs[0].dttk_value; 3549 break; 3550 } 3551 3552 daddr = dtrace_loadptr(baddr + 3553 offsetof(mblk_t, b_datap)); 3554 3555 baddr = dtrace_loadptr(baddr + 3556 offsetof(mblk_t, b_cont)); 3557 3558 /* 3559 * We want to prevent against denial-of-service here, 3560 * so we're only going to search the list for 3561 * dtrace_msgdsize_max mblks. 3562 */ 3563 if (cont++ > dtrace_msgdsize_max) { 3564 *flags |= CPU_DTRACE_ILLOP; 3565 break; 3566 } 3567 3568 if (subr == DIF_SUBR_MSGDSIZE) { 3569 if (dtrace_load8(daddr + 3570 offsetof(dblk_t, db_type)) != M_DATA) 3571 continue; 3572 } 3573 3574 count += wptr - rptr; 3575 } 3576 3577 if (!(*flags & CPU_DTRACE_FAULT)) 3578 regs[rd] = count; 3579 3580 break; 3581 } 3582 3583 case DIF_SUBR_PROGENYOF: { 3584 pid_t pid = tupregs[0].dttk_value; 3585 proc_t *p; 3586 int rval = 0; 3587 3588 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3589 3590 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 3591 if (p->p_pidp->pid_id == pid) { 3592 rval = 1; 3593 break; 3594 } 3595 } 3596 3597 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3598 3599 regs[rd] = rval; 3600 break; 3601 } 3602 3603 case DIF_SUBR_SPECULATION: 3604 regs[rd] = dtrace_speculation(state); 3605 break; 3606 3607 case DIF_SUBR_COPYOUT: { 3608 uintptr_t kaddr = tupregs[0].dttk_value; 3609 uintptr_t uaddr = tupregs[1].dttk_value; 3610 uint64_t size = tupregs[2].dttk_value; 3611 3612 if (!dtrace_destructive_disallow && 3613 dtrace_priv_proc_control(state, mstate) && 3614 !dtrace_istoxic(kaddr, size)) { 3615 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3616 dtrace_copyout(kaddr, uaddr, size, flags); 3617 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3618 } 3619 break; 3620 } 3621 3622 case DIF_SUBR_COPYOUTSTR: { 3623 uintptr_t kaddr = tupregs[0].dttk_value; 3624 uintptr_t uaddr = tupregs[1].dttk_value; 3625 uint64_t size = tupregs[2].dttk_value; 3626 3627 if (!dtrace_destructive_disallow && 3628 dtrace_priv_proc_control(state, mstate) && 3629 !dtrace_istoxic(kaddr, size)) { 3630 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3631 dtrace_copyoutstr(kaddr, uaddr, size, flags); 3632 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3633 } 3634 break; 3635 } 3636 3637 case DIF_SUBR_STRLEN: { 3638 size_t sz; 3639 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 3640 sz = dtrace_strlen((char *)addr, 3641 state->dts_options[DTRACEOPT_STRSIZE]); 3642 3643 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 3644 regs[rd] = NULL; 3645 break; 3646 } 3647 3648 regs[rd] = sz; 3649 3650 break; 3651 } 3652 3653 case DIF_SUBR_STRCHR: 3654 case DIF_SUBR_STRRCHR: { 3655 /* 3656 * We're going to iterate over the string looking for the 3657 * specified character. We will iterate until we have reached 3658 * the string length or we have found the character. If this 3659 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 3660 * of the specified character instead of the first. 3661 */ 3662 uintptr_t saddr = tupregs[0].dttk_value; 3663 uintptr_t addr = tupregs[0].dttk_value; 3664 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 3665 char c, target = (char)tupregs[1].dttk_value; 3666 3667 for (regs[rd] = NULL; addr < limit; addr++) { 3668 if ((c = dtrace_load8(addr)) == target) { 3669 regs[rd] = addr; 3670 3671 if (subr == DIF_SUBR_STRCHR) 3672 break; 3673 } 3674 3675 if (c == '\0') 3676 break; 3677 } 3678 3679 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 3680 regs[rd] = NULL; 3681 break; 3682 } 3683 3684 break; 3685 } 3686 3687 case DIF_SUBR_STRSTR: 3688 case DIF_SUBR_INDEX: 3689 case DIF_SUBR_RINDEX: { 3690 /* 3691 * We're going to iterate over the string looking for the 3692 * specified string. We will iterate until we have reached 3693 * the string length or we have found the string. (Yes, this 3694 * is done in the most naive way possible -- but considering 3695 * that the string we're searching for is likely to be 3696 * relatively short, the complexity of Rabin-Karp or similar 3697 * hardly seems merited.) 3698 */ 3699 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 3700 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 3701 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3702 size_t len = dtrace_strlen(addr, size); 3703 size_t sublen = dtrace_strlen(substr, size); 3704 char *limit = addr + len, *orig = addr; 3705 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 3706 int inc = 1; 3707 3708 regs[rd] = notfound; 3709 3710 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 3711 regs[rd] = NULL; 3712 break; 3713 } 3714 3715 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 3716 vstate)) { 3717 regs[rd] = NULL; 3718 break; 3719 } 3720 3721 /* 3722 * strstr() and index()/rindex() have similar semantics if 3723 * both strings are the empty string: strstr() returns a 3724 * pointer to the (empty) string, and index() and rindex() 3725 * both return index 0 (regardless of any position argument). 3726 */ 3727 if (sublen == 0 && len == 0) { 3728 if (subr == DIF_SUBR_STRSTR) 3729 regs[rd] = (uintptr_t)addr; 3730 else 3731 regs[rd] = 0; 3732 break; 3733 } 3734 3735 if (subr != DIF_SUBR_STRSTR) { 3736 if (subr == DIF_SUBR_RINDEX) { 3737 limit = orig - 1; 3738 addr += len; 3739 inc = -1; 3740 } 3741 3742 /* 3743 * Both index() and rindex() take an optional position 3744 * argument that denotes the starting position. 3745 */ 3746 if (nargs == 3) { 3747 int64_t pos = (int64_t)tupregs[2].dttk_value; 3748 3749 /* 3750 * If the position argument to index() is 3751 * negative, Perl implicitly clamps it at 3752 * zero. This semantic is a little surprising 3753 * given the special meaning of negative 3754 * positions to similar Perl functions like 3755 * substr(), but it appears to reflect a 3756 * notion that index() can start from a 3757 * negative index and increment its way up to 3758 * the string. Given this notion, Perl's 3759 * rindex() is at least self-consistent in 3760 * that it implicitly clamps positions greater 3761 * than the string length to be the string 3762 * length. Where Perl completely loses 3763 * coherence, however, is when the specified 3764 * substring is the empty string (""). In 3765 * this case, even if the position is 3766 * negative, rindex() returns 0 -- and even if 3767 * the position is greater than the length, 3768 * index() returns the string length. These 3769 * semantics violate the notion that index() 3770 * should never return a value less than the 3771 * specified position and that rindex() should 3772 * never return a value greater than the 3773 * specified position. (One assumes that 3774 * these semantics are artifacts of Perl's 3775 * implementation and not the results of 3776 * deliberate design -- it beggars belief that 3777 * even Larry Wall could desire such oddness.) 3778 * While in the abstract one would wish for 3779 * consistent position semantics across 3780 * substr(), index() and rindex() -- or at the 3781 * very least self-consistent position 3782 * semantics for index() and rindex() -- we 3783 * instead opt to keep with the extant Perl 3784 * semantics, in all their broken glory. (Do 3785 * we have more desire to maintain Perl's 3786 * semantics than Perl does? Probably.) 3787 */ 3788 if (subr == DIF_SUBR_RINDEX) { 3789 if (pos < 0) { 3790 if (sublen == 0) 3791 regs[rd] = 0; 3792 break; 3793 } 3794 3795 if (pos > len) 3796 pos = len; 3797 } else { 3798 if (pos < 0) 3799 pos = 0; 3800 3801 if (pos >= len) { 3802 if (sublen == 0) 3803 regs[rd] = len; 3804 break; 3805 } 3806 } 3807 3808 addr = orig + pos; 3809 } 3810 } 3811 3812 for (regs[rd] = notfound; addr != limit; addr += inc) { 3813 if (dtrace_strncmp(addr, substr, sublen) == 0) { 3814 if (subr != DIF_SUBR_STRSTR) { 3815 /* 3816 * As D index() and rindex() are 3817 * modeled on Perl (and not on awk), 3818 * we return a zero-based (and not a 3819 * one-based) index. (For you Perl 3820 * weenies: no, we're not going to add 3821 * $[ -- and shouldn't you be at a con 3822 * or something?) 3823 */ 3824 regs[rd] = (uintptr_t)(addr - orig); 3825 break; 3826 } 3827 3828 ASSERT(subr == DIF_SUBR_STRSTR); 3829 regs[rd] = (uintptr_t)addr; 3830 break; 3831 } 3832 } 3833 3834 break; 3835 } 3836 3837 case DIF_SUBR_STRTOK: { 3838 uintptr_t addr = tupregs[0].dttk_value; 3839 uintptr_t tokaddr = tupregs[1].dttk_value; 3840 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3841 uintptr_t limit, toklimit = tokaddr + size; 3842 uint8_t c, tokmap[32]; /* 256 / 8 */ 3843 char *dest = (char *)mstate->dtms_scratch_ptr; 3844 int i; 3845 3846 /* 3847 * Check both the token buffer and (later) the input buffer, 3848 * since both could be non-scratch addresses. 3849 */ 3850 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 3851 regs[rd] = NULL; 3852 break; 3853 } 3854 3855 if (!DTRACE_INSCRATCH(mstate, size)) { 3856 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3857 regs[rd] = NULL; 3858 break; 3859 } 3860 3861 if (addr == NULL) { 3862 /* 3863 * If the address specified is NULL, we use our saved 3864 * strtok pointer from the mstate. Note that this 3865 * means that the saved strtok pointer is _only_ 3866 * valid within multiple enablings of the same probe -- 3867 * it behaves like an implicit clause-local variable. 3868 */ 3869 addr = mstate->dtms_strtok; 3870 } else { 3871 /* 3872 * If the user-specified address is non-NULL we must 3873 * access check it. This is the only time we have 3874 * a chance to do so, since this address may reside 3875 * in the string table of this clause-- future calls 3876 * (when we fetch addr from mstate->dtms_strtok) 3877 * would fail this access check. 3878 */ 3879 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 3880 regs[rd] = NULL; 3881 break; 3882 } 3883 } 3884 3885 /* 3886 * First, zero the token map, and then process the token 3887 * string -- setting a bit in the map for every character 3888 * found in the token string. 3889 */ 3890 for (i = 0; i < sizeof (tokmap); i++) 3891 tokmap[i] = 0; 3892 3893 for (; tokaddr < toklimit; tokaddr++) { 3894 if ((c = dtrace_load8(tokaddr)) == '\0') 3895 break; 3896 3897 ASSERT((c >> 3) < sizeof (tokmap)); 3898 tokmap[c >> 3] |= (1 << (c & 0x7)); 3899 } 3900 3901 for (limit = addr + size; addr < limit; addr++) { 3902 /* 3903 * We're looking for a character that is _not_ contained 3904 * in the token string. 3905 */ 3906 if ((c = dtrace_load8(addr)) == '\0') 3907 break; 3908 3909 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3910 break; 3911 } 3912 3913 if (c == '\0') { 3914 /* 3915 * We reached the end of the string without finding 3916 * any character that was not in the token string. 3917 * We return NULL in this case, and we set the saved 3918 * address to NULL as well. 3919 */ 3920 regs[rd] = NULL; 3921 mstate->dtms_strtok = NULL; 3922 break; 3923 } 3924 3925 /* 3926 * From here on, we're copying into the destination string. 3927 */ 3928 for (i = 0; addr < limit && i < size - 1; addr++) { 3929 if ((c = dtrace_load8(addr)) == '\0') 3930 break; 3931 3932 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3933 break; 3934 3935 ASSERT(i < size); 3936 dest[i++] = c; 3937 } 3938 3939 ASSERT(i < size); 3940 dest[i] = '\0'; 3941 regs[rd] = (uintptr_t)dest; 3942 mstate->dtms_scratch_ptr += size; 3943 mstate->dtms_strtok = addr; 3944 break; 3945 } 3946 3947 case DIF_SUBR_SUBSTR: { 3948 uintptr_t s = tupregs[0].dttk_value; 3949 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3950 char *d = (char *)mstate->dtms_scratch_ptr; 3951 int64_t index = (int64_t)tupregs[1].dttk_value; 3952 int64_t remaining = (int64_t)tupregs[2].dttk_value; 3953 size_t len = dtrace_strlen((char *)s, size); 3954 int64_t i; 3955 3956 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 3957 regs[rd] = NULL; 3958 break; 3959 } 3960 3961 if (!DTRACE_INSCRATCH(mstate, size)) { 3962 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3963 regs[rd] = NULL; 3964 break; 3965 } 3966 3967 if (nargs <= 2) 3968 remaining = (int64_t)size; 3969 3970 if (index < 0) { 3971 index += len; 3972 3973 if (index < 0 && index + remaining > 0) { 3974 remaining += index; 3975 index = 0; 3976 } 3977 } 3978 3979 if (index >= len || index < 0) { 3980 remaining = 0; 3981 } else if (remaining < 0) { 3982 remaining += len - index; 3983 } else if (index + remaining > size) { 3984 remaining = size - index; 3985 } 3986 3987 for (i = 0; i < remaining; i++) { 3988 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 3989 break; 3990 } 3991 3992 d[i] = '\0'; 3993 3994 mstate->dtms_scratch_ptr += size; 3995 regs[rd] = (uintptr_t)d; 3996 break; 3997 } 3998 3999 case DIF_SUBR_TOUPPER: 4000 case DIF_SUBR_TOLOWER: { 4001 uintptr_t s = tupregs[0].dttk_value; 4002 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4003 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4004 size_t len = dtrace_strlen((char *)s, size); 4005 char lower, upper, convert; 4006 int64_t i; 4007 4008 if (subr == DIF_SUBR_TOUPPER) { 4009 lower = 'a'; 4010 upper = 'z'; 4011 convert = 'A'; 4012 } else { 4013 lower = 'A'; 4014 upper = 'Z'; 4015 convert = 'a'; 4016 } 4017 4018 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4019 regs[rd] = NULL; 4020 break; 4021 } 4022 4023 if (!DTRACE_INSCRATCH(mstate, size)) { 4024 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4025 regs[rd] = NULL; 4026 break; 4027 } 4028 4029 for (i = 0; i < size - 1; i++) { 4030 if ((c = dtrace_load8(s + i)) == '\0') 4031 break; 4032 4033 if (c >= lower && c <= upper) 4034 c = convert + (c - lower); 4035 4036 dest[i] = c; 4037 } 4038 4039 ASSERT(i < size); 4040 dest[i] = '\0'; 4041 regs[rd] = (uintptr_t)dest; 4042 mstate->dtms_scratch_ptr += size; 4043 break; 4044 } 4045 4046 case DIF_SUBR_GETMAJOR: 4047 #ifdef _LP64 4048 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 4049 #else 4050 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 4051 #endif 4052 break; 4053 4054 case DIF_SUBR_GETMINOR: 4055 #ifdef _LP64 4056 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 4057 #else 4058 regs[rd] = tupregs[0].dttk_value & MAXMIN; 4059 #endif 4060 break; 4061 4062 case DIF_SUBR_DDI_PATHNAME: { 4063 /* 4064 * This one is a galactic mess. We are going to roughly 4065 * emulate ddi_pathname(), but it's made more complicated 4066 * by the fact that we (a) want to include the minor name and 4067 * (b) must proceed iteratively instead of recursively. 4068 */ 4069 uintptr_t dest = mstate->dtms_scratch_ptr; 4070 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4071 char *start = (char *)dest, *end = start + size - 1; 4072 uintptr_t daddr = tupregs[0].dttk_value; 4073 int64_t minor = (int64_t)tupregs[1].dttk_value; 4074 char *s; 4075 int i, len, depth = 0; 4076 4077 /* 4078 * Due to all the pointer jumping we do and context we must 4079 * rely upon, we just mandate that the user must have kernel 4080 * read privileges to use this routine. 4081 */ 4082 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 4083 *flags |= CPU_DTRACE_KPRIV; 4084 *illval = daddr; 4085 regs[rd] = NULL; 4086 } 4087 4088 if (!DTRACE_INSCRATCH(mstate, size)) { 4089 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4090 regs[rd] = NULL; 4091 break; 4092 } 4093 4094 *end = '\0'; 4095 4096 /* 4097 * We want to have a name for the minor. In order to do this, 4098 * we need to walk the minor list from the devinfo. We want 4099 * to be sure that we don't infinitely walk a circular list, 4100 * so we check for circularity by sending a scout pointer 4101 * ahead two elements for every element that we iterate over; 4102 * if the list is circular, these will ultimately point to the 4103 * same element. You may recognize this little trick as the 4104 * answer to a stupid interview question -- one that always 4105 * seems to be asked by those who had to have it laboriously 4106 * explained to them, and who can't even concisely describe 4107 * the conditions under which one would be forced to resort to 4108 * this technique. Needless to say, those conditions are 4109 * found here -- and probably only here. Is this the only use 4110 * of this infamous trick in shipping, production code? If it 4111 * isn't, it probably should be... 4112 */ 4113 if (minor != -1) { 4114 uintptr_t maddr = dtrace_loadptr(daddr + 4115 offsetof(struct dev_info, devi_minor)); 4116 4117 uintptr_t next = offsetof(struct ddi_minor_data, next); 4118 uintptr_t name = offsetof(struct ddi_minor_data, 4119 d_minor) + offsetof(struct ddi_minor, name); 4120 uintptr_t dev = offsetof(struct ddi_minor_data, 4121 d_minor) + offsetof(struct ddi_minor, dev); 4122 uintptr_t scout; 4123 4124 if (maddr != NULL) 4125 scout = dtrace_loadptr(maddr + next); 4126 4127 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4128 uint64_t m; 4129 #ifdef _LP64 4130 m = dtrace_load64(maddr + dev) & MAXMIN64; 4131 #else 4132 m = dtrace_load32(maddr + dev) & MAXMIN; 4133 #endif 4134 if (m != minor) { 4135 maddr = dtrace_loadptr(maddr + next); 4136 4137 if (scout == NULL) 4138 continue; 4139 4140 scout = dtrace_loadptr(scout + next); 4141 4142 if (scout == NULL) 4143 continue; 4144 4145 scout = dtrace_loadptr(scout + next); 4146 4147 if (scout == NULL) 4148 continue; 4149 4150 if (scout == maddr) { 4151 *flags |= CPU_DTRACE_ILLOP; 4152 break; 4153 } 4154 4155 continue; 4156 } 4157 4158 /* 4159 * We have the minor data. Now we need to 4160 * copy the minor's name into the end of the 4161 * pathname. 4162 */ 4163 s = (char *)dtrace_loadptr(maddr + name); 4164 len = dtrace_strlen(s, size); 4165 4166 if (*flags & CPU_DTRACE_FAULT) 4167 break; 4168 4169 if (len != 0) { 4170 if ((end -= (len + 1)) < start) 4171 break; 4172 4173 *end = ':'; 4174 } 4175 4176 for (i = 1; i <= len; i++) 4177 end[i] = dtrace_load8((uintptr_t)s++); 4178 break; 4179 } 4180 } 4181 4182 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4183 ddi_node_state_t devi_state; 4184 4185 devi_state = dtrace_load32(daddr + 4186 offsetof(struct dev_info, devi_node_state)); 4187 4188 if (*flags & CPU_DTRACE_FAULT) 4189 break; 4190 4191 if (devi_state >= DS_INITIALIZED) { 4192 s = (char *)dtrace_loadptr(daddr + 4193 offsetof(struct dev_info, devi_addr)); 4194 len = dtrace_strlen(s, size); 4195 4196 if (*flags & CPU_DTRACE_FAULT) 4197 break; 4198 4199 if (len != 0) { 4200 if ((end -= (len + 1)) < start) 4201 break; 4202 4203 *end = '@'; 4204 } 4205 4206 for (i = 1; i <= len; i++) 4207 end[i] = dtrace_load8((uintptr_t)s++); 4208 } 4209 4210 /* 4211 * Now for the node name... 4212 */ 4213 s = (char *)dtrace_loadptr(daddr + 4214 offsetof(struct dev_info, devi_node_name)); 4215 4216 daddr = dtrace_loadptr(daddr + 4217 offsetof(struct dev_info, devi_parent)); 4218 4219 /* 4220 * If our parent is NULL (that is, if we're the root 4221 * node), we're going to use the special path 4222 * "devices". 4223 */ 4224 if (daddr == NULL) 4225 s = "devices"; 4226 4227 len = dtrace_strlen(s, size); 4228 if (*flags & CPU_DTRACE_FAULT) 4229 break; 4230 4231 if ((end -= (len + 1)) < start) 4232 break; 4233 4234 for (i = 1; i <= len; i++) 4235 end[i] = dtrace_load8((uintptr_t)s++); 4236 *end = '/'; 4237 4238 if (depth++ > dtrace_devdepth_max) { 4239 *flags |= CPU_DTRACE_ILLOP; 4240 break; 4241 } 4242 } 4243 4244 if (end < start) 4245 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4246 4247 if (daddr == NULL) { 4248 regs[rd] = (uintptr_t)end; 4249 mstate->dtms_scratch_ptr += size; 4250 } 4251 4252 break; 4253 } 4254 4255 case DIF_SUBR_STRJOIN: { 4256 char *d = (char *)mstate->dtms_scratch_ptr; 4257 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4258 uintptr_t s1 = tupregs[0].dttk_value; 4259 uintptr_t s2 = tupregs[1].dttk_value; 4260 int i = 0; 4261 4262 if (!dtrace_strcanload(s1, size, mstate, vstate) || 4263 !dtrace_strcanload(s2, size, mstate, vstate)) { 4264 regs[rd] = NULL; 4265 break; 4266 } 4267 4268 if (!DTRACE_INSCRATCH(mstate, size)) { 4269 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4270 regs[rd] = NULL; 4271 break; 4272 } 4273 4274 for (;;) { 4275 if (i >= size) { 4276 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4277 regs[rd] = NULL; 4278 break; 4279 } 4280 4281 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 4282 i--; 4283 break; 4284 } 4285 } 4286 4287 for (;;) { 4288 if (i >= size) { 4289 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4290 regs[rd] = NULL; 4291 break; 4292 } 4293 4294 if ((d[i++] = dtrace_load8(s2++)) == '\0') 4295 break; 4296 } 4297 4298 if (i < size) { 4299 mstate->dtms_scratch_ptr += i; 4300 regs[rd] = (uintptr_t)d; 4301 } 4302 4303 break; 4304 } 4305 4306 case DIF_SUBR_LLTOSTR: { 4307 int64_t i = (int64_t)tupregs[0].dttk_value; 4308 uint64_t val, digit; 4309 uint64_t size = 65; /* enough room for 2^64 in binary */ 4310 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 4311 int base = 10; 4312 4313 if (nargs > 1) { 4314 if ((base = tupregs[1].dttk_value) <= 1 || 4315 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 4316 *flags |= CPU_DTRACE_ILLOP; 4317 break; 4318 } 4319 } 4320 4321 val = (base == 10 && i < 0) ? i * -1 : i; 4322 4323 if (!DTRACE_INSCRATCH(mstate, size)) { 4324 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4325 regs[rd] = NULL; 4326 break; 4327 } 4328 4329 for (*end-- = '\0'; val; val /= base) { 4330 if ((digit = val % base) <= '9' - '0') { 4331 *end-- = '0' + digit; 4332 } else { 4333 *end-- = 'a' + (digit - ('9' - '0') - 1); 4334 } 4335 } 4336 4337 if (i == 0 && base == 16) 4338 *end-- = '0'; 4339 4340 if (base == 16) 4341 *end-- = 'x'; 4342 4343 if (i == 0 || base == 8 || base == 16) 4344 *end-- = '0'; 4345 4346 if (i < 0 && base == 10) 4347 *end-- = '-'; 4348 4349 regs[rd] = (uintptr_t)end + 1; 4350 mstate->dtms_scratch_ptr += size; 4351 break; 4352 } 4353 4354 case DIF_SUBR_HTONS: 4355 case DIF_SUBR_NTOHS: 4356 #ifdef _BIG_ENDIAN 4357 regs[rd] = (uint16_t)tupregs[0].dttk_value; 4358 #else 4359 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 4360 #endif 4361 break; 4362 4363 4364 case DIF_SUBR_HTONL: 4365 case DIF_SUBR_NTOHL: 4366 #ifdef _BIG_ENDIAN 4367 regs[rd] = (uint32_t)tupregs[0].dttk_value; 4368 #else 4369 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 4370 #endif 4371 break; 4372 4373 4374 case DIF_SUBR_HTONLL: 4375 case DIF_SUBR_NTOHLL: 4376 #ifdef _BIG_ENDIAN 4377 regs[rd] = (uint64_t)tupregs[0].dttk_value; 4378 #else 4379 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 4380 #endif 4381 break; 4382 4383 4384 case DIF_SUBR_DIRNAME: 4385 case DIF_SUBR_BASENAME: { 4386 char *dest = (char *)mstate->dtms_scratch_ptr; 4387 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4388 uintptr_t src = tupregs[0].dttk_value; 4389 int i, j, len = dtrace_strlen((char *)src, size); 4390 int lastbase = -1, firstbase = -1, lastdir = -1; 4391 int start, end; 4392 4393 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 4394 regs[rd] = NULL; 4395 break; 4396 } 4397 4398 if (!DTRACE_INSCRATCH(mstate, size)) { 4399 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4400 regs[rd] = NULL; 4401 break; 4402 } 4403 4404 /* 4405 * The basename and dirname for a zero-length string is 4406 * defined to be "." 4407 */ 4408 if (len == 0) { 4409 len = 1; 4410 src = (uintptr_t)"."; 4411 } 4412 4413 /* 4414 * Start from the back of the string, moving back toward the 4415 * front until we see a character that isn't a slash. That 4416 * character is the last character in the basename. 4417 */ 4418 for (i = len - 1; i >= 0; i--) { 4419 if (dtrace_load8(src + i) != '/') 4420 break; 4421 } 4422 4423 if (i >= 0) 4424 lastbase = i; 4425 4426 /* 4427 * Starting from the last character in the basename, move 4428 * towards the front until we find a slash. The character 4429 * that we processed immediately before that is the first 4430 * character in the basename. 4431 */ 4432 for (; i >= 0; i--) { 4433 if (dtrace_load8(src + i) == '/') 4434 break; 4435 } 4436 4437 if (i >= 0) 4438 firstbase = i + 1; 4439 4440 /* 4441 * Now keep going until we find a non-slash character. That 4442 * character is the last character in the dirname. 4443 */ 4444 for (; i >= 0; i--) { 4445 if (dtrace_load8(src + i) != '/') 4446 break; 4447 } 4448 4449 if (i >= 0) 4450 lastdir = i; 4451 4452 ASSERT(!(lastbase == -1 && firstbase != -1)); 4453 ASSERT(!(firstbase == -1 && lastdir != -1)); 4454 4455 if (lastbase == -1) { 4456 /* 4457 * We didn't find a non-slash character. We know that 4458 * the length is non-zero, so the whole string must be 4459 * slashes. In either the dirname or the basename 4460 * case, we return '/'. 4461 */ 4462 ASSERT(firstbase == -1); 4463 firstbase = lastbase = lastdir = 0; 4464 } 4465 4466 if (firstbase == -1) { 4467 /* 4468 * The entire string consists only of a basename 4469 * component. If we're looking for dirname, we need 4470 * to change our string to be just "."; if we're 4471 * looking for a basename, we'll just set the first 4472 * character of the basename to be 0. 4473 */ 4474 if (subr == DIF_SUBR_DIRNAME) { 4475 ASSERT(lastdir == -1); 4476 src = (uintptr_t)"."; 4477 lastdir = 0; 4478 } else { 4479 firstbase = 0; 4480 } 4481 } 4482 4483 if (subr == DIF_SUBR_DIRNAME) { 4484 if (lastdir == -1) { 4485 /* 4486 * We know that we have a slash in the name -- 4487 * or lastdir would be set to 0, above. And 4488 * because lastdir is -1, we know that this 4489 * slash must be the first character. (That 4490 * is, the full string must be of the form 4491 * "/basename".) In this case, the last 4492 * character of the directory name is 0. 4493 */ 4494 lastdir = 0; 4495 } 4496 4497 start = 0; 4498 end = lastdir; 4499 } else { 4500 ASSERT(subr == DIF_SUBR_BASENAME); 4501 ASSERT(firstbase != -1 && lastbase != -1); 4502 start = firstbase; 4503 end = lastbase; 4504 } 4505 4506 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 4507 dest[j] = dtrace_load8(src + i); 4508 4509 dest[j] = '\0'; 4510 regs[rd] = (uintptr_t)dest; 4511 mstate->dtms_scratch_ptr += size; 4512 break; 4513 } 4514 4515 case DIF_SUBR_GETF: { 4516 uintptr_t fd = tupregs[0].dttk_value; 4517 uf_info_t *finfo = &curthread->t_procp->p_user.u_finfo; 4518 file_t *fp; 4519 4520 if (!dtrace_priv_proc(state, mstate)) { 4521 regs[rd] = NULL; 4522 break; 4523 } 4524 4525 /* 4526 * This is safe because fi_nfiles only increases, and the 4527 * fi_list array is not freed when the array size doubles. 4528 * (See the comment in flist_grow() for details on the 4529 * management of the u_finfo structure.) 4530 */ 4531 fp = fd < finfo->fi_nfiles ? finfo->fi_list[fd].uf_file : NULL; 4532 4533 mstate->dtms_getf = fp; 4534 regs[rd] = (uintptr_t)fp; 4535 break; 4536 } 4537 4538 case DIF_SUBR_CLEANPATH: { 4539 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4540 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4541 uintptr_t src = tupregs[0].dttk_value; 4542 int i = 0, j = 0; 4543 zone_t *z; 4544 4545 if (!dtrace_strcanload(src, size, mstate, vstate)) { 4546 regs[rd] = NULL; 4547 break; 4548 } 4549 4550 if (!DTRACE_INSCRATCH(mstate, size)) { 4551 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4552 regs[rd] = NULL; 4553 break; 4554 } 4555 4556 /* 4557 * Move forward, loading each character. 4558 */ 4559 do { 4560 c = dtrace_load8(src + i++); 4561 next: 4562 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 4563 break; 4564 4565 if (c != '/') { 4566 dest[j++] = c; 4567 continue; 4568 } 4569 4570 c = dtrace_load8(src + i++); 4571 4572 if (c == '/') { 4573 /* 4574 * We have two slashes -- we can just advance 4575 * to the next character. 4576 */ 4577 goto next; 4578 } 4579 4580 if (c != '.') { 4581 /* 4582 * This is not "." and it's not ".." -- we can 4583 * just store the "/" and this character and 4584 * drive on. 4585 */ 4586 dest[j++] = '/'; 4587 dest[j++] = c; 4588 continue; 4589 } 4590 4591 c = dtrace_load8(src + i++); 4592 4593 if (c == '/') { 4594 /* 4595 * This is a "/./" component. We're not going 4596 * to store anything in the destination buffer; 4597 * we're just going to go to the next component. 4598 */ 4599 goto next; 4600 } 4601 4602 if (c != '.') { 4603 /* 4604 * This is not ".." -- we can just store the 4605 * "/." and this character and continue 4606 * processing. 4607 */ 4608 dest[j++] = '/'; 4609 dest[j++] = '.'; 4610 dest[j++] = c; 4611 continue; 4612 } 4613 4614 c = dtrace_load8(src + i++); 4615 4616 if (c != '/' && c != '\0') { 4617 /* 4618 * This is not ".." -- it's "..[mumble]". 4619 * We'll store the "/.." and this character 4620 * and continue processing. 4621 */ 4622 dest[j++] = '/'; 4623 dest[j++] = '.'; 4624 dest[j++] = '.'; 4625 dest[j++] = c; 4626 continue; 4627 } 4628 4629 /* 4630 * This is "/../" or "/..\0". We need to back up 4631 * our destination pointer until we find a "/". 4632 */ 4633 i--; 4634 while (j != 0 && dest[--j] != '/') 4635 continue; 4636 4637 if (c == '\0') 4638 dest[++j] = '/'; 4639 } while (c != '\0'); 4640 4641 dest[j] = '\0'; 4642 4643 if (mstate->dtms_getf != NULL && 4644 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 4645 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 4646 /* 4647 * If we've done a getf() as a part of this ECB and we 4648 * don't have kernel access (and we're not in the global 4649 * zone), check if the path we cleaned up begins with 4650 * the zone's root path, and trim it off if so. Note 4651 * that this is an output cleanliness issue, not a 4652 * security issue: knowing one's zone root path does 4653 * not enable privilege escalation. 4654 */ 4655 if (strstr(dest, z->zone_rootpath) == dest) 4656 dest += strlen(z->zone_rootpath) - 1; 4657 } 4658 4659 regs[rd] = (uintptr_t)dest; 4660 mstate->dtms_scratch_ptr += size; 4661 break; 4662 } 4663 4664 case DIF_SUBR_INET_NTOA: 4665 case DIF_SUBR_INET_NTOA6: 4666 case DIF_SUBR_INET_NTOP: { 4667 size_t size; 4668 int af, argi, i; 4669 char *base, *end; 4670 4671 if (subr == DIF_SUBR_INET_NTOP) { 4672 af = (int)tupregs[0].dttk_value; 4673 argi = 1; 4674 } else { 4675 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 4676 argi = 0; 4677 } 4678 4679 if (af == AF_INET) { 4680 ipaddr_t ip4; 4681 uint8_t *ptr8, val; 4682 4683 /* 4684 * Safely load the IPv4 address. 4685 */ 4686 ip4 = dtrace_load32(tupregs[argi].dttk_value); 4687 4688 /* 4689 * Check an IPv4 string will fit in scratch. 4690 */ 4691 size = INET_ADDRSTRLEN; 4692 if (!DTRACE_INSCRATCH(mstate, size)) { 4693 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4694 regs[rd] = NULL; 4695 break; 4696 } 4697 base = (char *)mstate->dtms_scratch_ptr; 4698 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4699 4700 /* 4701 * Stringify as a dotted decimal quad. 4702 */ 4703 *end-- = '\0'; 4704 ptr8 = (uint8_t *)&ip4; 4705 for (i = 3; i >= 0; i--) { 4706 val = ptr8[i]; 4707 4708 if (val == 0) { 4709 *end-- = '0'; 4710 } else { 4711 for (; val; val /= 10) { 4712 *end-- = '0' + (val % 10); 4713 } 4714 } 4715 4716 if (i > 0) 4717 *end-- = '.'; 4718 } 4719 ASSERT(end + 1 >= base); 4720 4721 } else if (af == AF_INET6) { 4722 struct in6_addr ip6; 4723 int firstzero, tryzero, numzero, v6end; 4724 uint16_t val; 4725 const char digits[] = "0123456789abcdef"; 4726 4727 /* 4728 * Stringify using RFC 1884 convention 2 - 16 bit 4729 * hexadecimal values with a zero-run compression. 4730 * Lower case hexadecimal digits are used. 4731 * eg, fe80::214:4fff:fe0b:76c8. 4732 * The IPv4 embedded form is returned for inet_ntop, 4733 * just the IPv4 string is returned for inet_ntoa6. 4734 */ 4735 4736 /* 4737 * Safely load the IPv6 address. 4738 */ 4739 dtrace_bcopy( 4740 (void *)(uintptr_t)tupregs[argi].dttk_value, 4741 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 4742 4743 /* 4744 * Check an IPv6 string will fit in scratch. 4745 */ 4746 size = INET6_ADDRSTRLEN; 4747 if (!DTRACE_INSCRATCH(mstate, size)) { 4748 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4749 regs[rd] = NULL; 4750 break; 4751 } 4752 base = (char *)mstate->dtms_scratch_ptr; 4753 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4754 *end-- = '\0'; 4755 4756 /* 4757 * Find the longest run of 16 bit zero values 4758 * for the single allowed zero compression - "::". 4759 */ 4760 firstzero = -1; 4761 tryzero = -1; 4762 numzero = 1; 4763 for (i = 0; i < sizeof (struct in6_addr); i++) { 4764 if (ip6._S6_un._S6_u8[i] == 0 && 4765 tryzero == -1 && i % 2 == 0) { 4766 tryzero = i; 4767 continue; 4768 } 4769 4770 if (tryzero != -1 && 4771 (ip6._S6_un._S6_u8[i] != 0 || 4772 i == sizeof (struct in6_addr) - 1)) { 4773 4774 if (i - tryzero <= numzero) { 4775 tryzero = -1; 4776 continue; 4777 } 4778 4779 firstzero = tryzero; 4780 numzero = i - i % 2 - tryzero; 4781 tryzero = -1; 4782 4783 if (ip6._S6_un._S6_u8[i] == 0 && 4784 i == sizeof (struct in6_addr) - 1) 4785 numzero += 2; 4786 } 4787 } 4788 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 4789 4790 /* 4791 * Check for an IPv4 embedded address. 4792 */ 4793 v6end = sizeof (struct in6_addr) - 2; 4794 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 4795 IN6_IS_ADDR_V4COMPAT(&ip6)) { 4796 for (i = sizeof (struct in6_addr) - 1; 4797 i >= DTRACE_V4MAPPED_OFFSET; i--) { 4798 ASSERT(end >= base); 4799 4800 val = ip6._S6_un._S6_u8[i]; 4801 4802 if (val == 0) { 4803 *end-- = '0'; 4804 } else { 4805 for (; val; val /= 10) { 4806 *end-- = '0' + val % 10; 4807 } 4808 } 4809 4810 if (i > DTRACE_V4MAPPED_OFFSET) 4811 *end-- = '.'; 4812 } 4813 4814 if (subr == DIF_SUBR_INET_NTOA6) 4815 goto inetout; 4816 4817 /* 4818 * Set v6end to skip the IPv4 address that 4819 * we have already stringified. 4820 */ 4821 v6end = 10; 4822 } 4823 4824 /* 4825 * Build the IPv6 string by working through the 4826 * address in reverse. 4827 */ 4828 for (i = v6end; i >= 0; i -= 2) { 4829 ASSERT(end >= base); 4830 4831 if (i == firstzero + numzero - 2) { 4832 *end-- = ':'; 4833 *end-- = ':'; 4834 i -= numzero - 2; 4835 continue; 4836 } 4837 4838 if (i < 14 && i != firstzero - 2) 4839 *end-- = ':'; 4840 4841 val = (ip6._S6_un._S6_u8[i] << 8) + 4842 ip6._S6_un._S6_u8[i + 1]; 4843 4844 if (val == 0) { 4845 *end-- = '0'; 4846 } else { 4847 for (; val; val /= 16) { 4848 *end-- = digits[val % 16]; 4849 } 4850 } 4851 } 4852 ASSERT(end + 1 >= base); 4853 4854 } else { 4855 /* 4856 * The user didn't use AH_INET or AH_INET6. 4857 */ 4858 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4859 regs[rd] = NULL; 4860 break; 4861 } 4862 4863 inetout: regs[rd] = (uintptr_t)end + 1; 4864 mstate->dtms_scratch_ptr += size; 4865 break; 4866 } 4867 4868 } 4869 } 4870 4871 /* 4872 * Emulate the execution of DTrace IR instructions specified by the given 4873 * DIF object. This function is deliberately void of assertions as all of 4874 * the necessary checks are handled by a call to dtrace_difo_validate(). 4875 */ 4876 static uint64_t 4877 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 4878 dtrace_vstate_t *vstate, dtrace_state_t *state) 4879 { 4880 const dif_instr_t *text = difo->dtdo_buf; 4881 const uint_t textlen = difo->dtdo_len; 4882 const char *strtab = difo->dtdo_strtab; 4883 const uint64_t *inttab = difo->dtdo_inttab; 4884 4885 uint64_t rval = 0; 4886 dtrace_statvar_t *svar; 4887 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 4888 dtrace_difv_t *v; 4889 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 4890 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 4891 4892 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 4893 uint64_t regs[DIF_DIR_NREGS]; 4894 uint64_t *tmp; 4895 4896 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 4897 int64_t cc_r; 4898 uint_t pc = 0, id, opc; 4899 uint8_t ttop = 0; 4900 dif_instr_t instr; 4901 uint_t r1, r2, rd; 4902 4903 /* 4904 * We stash the current DIF object into the machine state: we need it 4905 * for subsequent access checking. 4906 */ 4907 mstate->dtms_difo = difo; 4908 4909 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 4910 4911 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 4912 opc = pc; 4913 4914 instr = text[pc++]; 4915 r1 = DIF_INSTR_R1(instr); 4916 r2 = DIF_INSTR_R2(instr); 4917 rd = DIF_INSTR_RD(instr); 4918 4919 switch (DIF_INSTR_OP(instr)) { 4920 case DIF_OP_OR: 4921 regs[rd] = regs[r1] | regs[r2]; 4922 break; 4923 case DIF_OP_XOR: 4924 regs[rd] = regs[r1] ^ regs[r2]; 4925 break; 4926 case DIF_OP_AND: 4927 regs[rd] = regs[r1] & regs[r2]; 4928 break; 4929 case DIF_OP_SLL: 4930 regs[rd] = regs[r1] << regs[r2]; 4931 break; 4932 case DIF_OP_SRL: 4933 regs[rd] = regs[r1] >> regs[r2]; 4934 break; 4935 case DIF_OP_SUB: 4936 regs[rd] = regs[r1] - regs[r2]; 4937 break; 4938 case DIF_OP_ADD: 4939 regs[rd] = regs[r1] + regs[r2]; 4940 break; 4941 case DIF_OP_MUL: 4942 regs[rd] = regs[r1] * regs[r2]; 4943 break; 4944 case DIF_OP_SDIV: 4945 if (regs[r2] == 0) { 4946 regs[rd] = 0; 4947 *flags |= CPU_DTRACE_DIVZERO; 4948 } else { 4949 regs[rd] = (int64_t)regs[r1] / 4950 (int64_t)regs[r2]; 4951 } 4952 break; 4953 4954 case DIF_OP_UDIV: 4955 if (regs[r2] == 0) { 4956 regs[rd] = 0; 4957 *flags |= CPU_DTRACE_DIVZERO; 4958 } else { 4959 regs[rd] = regs[r1] / regs[r2]; 4960 } 4961 break; 4962 4963 case DIF_OP_SREM: 4964 if (regs[r2] == 0) { 4965 regs[rd] = 0; 4966 *flags |= CPU_DTRACE_DIVZERO; 4967 } else { 4968 regs[rd] = (int64_t)regs[r1] % 4969 (int64_t)regs[r2]; 4970 } 4971 break; 4972 4973 case DIF_OP_UREM: 4974 if (regs[r2] == 0) { 4975 regs[rd] = 0; 4976 *flags |= CPU_DTRACE_DIVZERO; 4977 } else { 4978 regs[rd] = regs[r1] % regs[r2]; 4979 } 4980 break; 4981 4982 case DIF_OP_NOT: 4983 regs[rd] = ~regs[r1]; 4984 break; 4985 case DIF_OP_MOV: 4986 regs[rd] = regs[r1]; 4987 break; 4988 case DIF_OP_CMP: 4989 cc_r = regs[r1] - regs[r2]; 4990 cc_n = cc_r < 0; 4991 cc_z = cc_r == 0; 4992 cc_v = 0; 4993 cc_c = regs[r1] < regs[r2]; 4994 break; 4995 case DIF_OP_TST: 4996 cc_n = cc_v = cc_c = 0; 4997 cc_z = regs[r1] == 0; 4998 break; 4999 case DIF_OP_BA: 5000 pc = DIF_INSTR_LABEL(instr); 5001 break; 5002 case DIF_OP_BE: 5003 if (cc_z) 5004 pc = DIF_INSTR_LABEL(instr); 5005 break; 5006 case DIF_OP_BNE: 5007 if (cc_z == 0) 5008 pc = DIF_INSTR_LABEL(instr); 5009 break; 5010 case DIF_OP_BG: 5011 if ((cc_z | (cc_n ^ cc_v)) == 0) 5012 pc = DIF_INSTR_LABEL(instr); 5013 break; 5014 case DIF_OP_BGU: 5015 if ((cc_c | cc_z) == 0) 5016 pc = DIF_INSTR_LABEL(instr); 5017 break; 5018 case DIF_OP_BGE: 5019 if ((cc_n ^ cc_v) == 0) 5020 pc = DIF_INSTR_LABEL(instr); 5021 break; 5022 case DIF_OP_BGEU: 5023 if (cc_c == 0) 5024 pc = DIF_INSTR_LABEL(instr); 5025 break; 5026 case DIF_OP_BL: 5027 if (cc_n ^ cc_v) 5028 pc = DIF_INSTR_LABEL(instr); 5029 break; 5030 case DIF_OP_BLU: 5031 if (cc_c) 5032 pc = DIF_INSTR_LABEL(instr); 5033 break; 5034 case DIF_OP_BLE: 5035 if (cc_z | (cc_n ^ cc_v)) 5036 pc = DIF_INSTR_LABEL(instr); 5037 break; 5038 case DIF_OP_BLEU: 5039 if (cc_c | cc_z) 5040 pc = DIF_INSTR_LABEL(instr); 5041 break; 5042 case DIF_OP_RLDSB: 5043 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 5044 break; 5045 /*FALLTHROUGH*/ 5046 case DIF_OP_LDSB: 5047 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 5048 break; 5049 case DIF_OP_RLDSH: 5050 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 5051 break; 5052 /*FALLTHROUGH*/ 5053 case DIF_OP_LDSH: 5054 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 5055 break; 5056 case DIF_OP_RLDSW: 5057 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 5058 break; 5059 /*FALLTHROUGH*/ 5060 case DIF_OP_LDSW: 5061 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 5062 break; 5063 case DIF_OP_RLDUB: 5064 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 5065 break; 5066 /*FALLTHROUGH*/ 5067 case DIF_OP_LDUB: 5068 regs[rd] = dtrace_load8(regs[r1]); 5069 break; 5070 case DIF_OP_RLDUH: 5071 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 5072 break; 5073 /*FALLTHROUGH*/ 5074 case DIF_OP_LDUH: 5075 regs[rd] = dtrace_load16(regs[r1]); 5076 break; 5077 case DIF_OP_RLDUW: 5078 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 5079 break; 5080 /*FALLTHROUGH*/ 5081 case DIF_OP_LDUW: 5082 regs[rd] = dtrace_load32(regs[r1]); 5083 break; 5084 case DIF_OP_RLDX: 5085 if (!dtrace_canload(regs[r1], 8, mstate, vstate)) 5086 break; 5087 /*FALLTHROUGH*/ 5088 case DIF_OP_LDX: 5089 regs[rd] = dtrace_load64(regs[r1]); 5090 break; 5091 case DIF_OP_ULDSB: 5092 regs[rd] = (int8_t) 5093 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5094 break; 5095 case DIF_OP_ULDSH: 5096 regs[rd] = (int16_t) 5097 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5098 break; 5099 case DIF_OP_ULDSW: 5100 regs[rd] = (int32_t) 5101 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5102 break; 5103 case DIF_OP_ULDUB: 5104 regs[rd] = 5105 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5106 break; 5107 case DIF_OP_ULDUH: 5108 regs[rd] = 5109 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5110 break; 5111 case DIF_OP_ULDUW: 5112 regs[rd] = 5113 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5114 break; 5115 case DIF_OP_ULDX: 5116 regs[rd] = 5117 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 5118 break; 5119 case DIF_OP_RET: 5120 rval = regs[rd]; 5121 pc = textlen; 5122 break; 5123 case DIF_OP_NOP: 5124 break; 5125 case DIF_OP_SETX: 5126 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 5127 break; 5128 case DIF_OP_SETS: 5129 regs[rd] = (uint64_t)(uintptr_t) 5130 (strtab + DIF_INSTR_STRING(instr)); 5131 break; 5132 case DIF_OP_SCMP: { 5133 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 5134 uintptr_t s1 = regs[r1]; 5135 uintptr_t s2 = regs[r2]; 5136 5137 if (s1 != NULL && 5138 !dtrace_strcanload(s1, sz, mstate, vstate)) 5139 break; 5140 if (s2 != NULL && 5141 !dtrace_strcanload(s2, sz, mstate, vstate)) 5142 break; 5143 5144 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 5145 5146 cc_n = cc_r < 0; 5147 cc_z = cc_r == 0; 5148 cc_v = cc_c = 0; 5149 break; 5150 } 5151 case DIF_OP_LDGA: 5152 regs[rd] = dtrace_dif_variable(mstate, state, 5153 r1, regs[r2]); 5154 break; 5155 case DIF_OP_LDGS: 5156 id = DIF_INSTR_VAR(instr); 5157 5158 if (id >= DIF_VAR_OTHER_UBASE) { 5159 uintptr_t a; 5160 5161 id -= DIF_VAR_OTHER_UBASE; 5162 svar = vstate->dtvs_globals[id]; 5163 ASSERT(svar != NULL); 5164 v = &svar->dtsv_var; 5165 5166 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 5167 regs[rd] = svar->dtsv_data; 5168 break; 5169 } 5170 5171 a = (uintptr_t)svar->dtsv_data; 5172 5173 if (*(uint8_t *)a == UINT8_MAX) { 5174 /* 5175 * If the 0th byte is set to UINT8_MAX 5176 * then this is to be treated as a 5177 * reference to a NULL variable. 5178 */ 5179 regs[rd] = NULL; 5180 } else { 5181 regs[rd] = a + sizeof (uint64_t); 5182 } 5183 5184 break; 5185 } 5186 5187 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 5188 break; 5189 5190 case DIF_OP_STGS: 5191 id = DIF_INSTR_VAR(instr); 5192 5193 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5194 id -= DIF_VAR_OTHER_UBASE; 5195 5196 svar = vstate->dtvs_globals[id]; 5197 ASSERT(svar != NULL); 5198 v = &svar->dtsv_var; 5199 5200 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5201 uintptr_t a = (uintptr_t)svar->dtsv_data; 5202 5203 ASSERT(a != NULL); 5204 ASSERT(svar->dtsv_size != 0); 5205 5206 if (regs[rd] == NULL) { 5207 *(uint8_t *)a = UINT8_MAX; 5208 break; 5209 } else { 5210 *(uint8_t *)a = 0; 5211 a += sizeof (uint64_t); 5212 } 5213 if (!dtrace_vcanload( 5214 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5215 mstate, vstate)) 5216 break; 5217 5218 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5219 (void *)a, &v->dtdv_type); 5220 break; 5221 } 5222 5223 svar->dtsv_data = regs[rd]; 5224 break; 5225 5226 case DIF_OP_LDTA: 5227 /* 5228 * There are no DTrace built-in thread-local arrays at 5229 * present. This opcode is saved for future work. 5230 */ 5231 *flags |= CPU_DTRACE_ILLOP; 5232 regs[rd] = 0; 5233 break; 5234 5235 case DIF_OP_LDLS: 5236 id = DIF_INSTR_VAR(instr); 5237 5238 if (id < DIF_VAR_OTHER_UBASE) { 5239 /* 5240 * For now, this has no meaning. 5241 */ 5242 regs[rd] = 0; 5243 break; 5244 } 5245 5246 id -= DIF_VAR_OTHER_UBASE; 5247 5248 ASSERT(id < vstate->dtvs_nlocals); 5249 ASSERT(vstate->dtvs_locals != NULL); 5250 5251 svar = vstate->dtvs_locals[id]; 5252 ASSERT(svar != NULL); 5253 v = &svar->dtsv_var; 5254 5255 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5256 uintptr_t a = (uintptr_t)svar->dtsv_data; 5257 size_t sz = v->dtdv_type.dtdt_size; 5258 5259 sz += sizeof (uint64_t); 5260 ASSERT(svar->dtsv_size == NCPU * sz); 5261 a += CPU->cpu_id * sz; 5262 5263 if (*(uint8_t *)a == UINT8_MAX) { 5264 /* 5265 * If the 0th byte is set to UINT8_MAX 5266 * then this is to be treated as a 5267 * reference to a NULL variable. 5268 */ 5269 regs[rd] = NULL; 5270 } else { 5271 regs[rd] = a + sizeof (uint64_t); 5272 } 5273 5274 break; 5275 } 5276 5277 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5278 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5279 regs[rd] = tmp[CPU->cpu_id]; 5280 break; 5281 5282 case DIF_OP_STLS: 5283 id = DIF_INSTR_VAR(instr); 5284 5285 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5286 id -= DIF_VAR_OTHER_UBASE; 5287 ASSERT(id < vstate->dtvs_nlocals); 5288 5289 ASSERT(vstate->dtvs_locals != NULL); 5290 svar = vstate->dtvs_locals[id]; 5291 ASSERT(svar != NULL); 5292 v = &svar->dtsv_var; 5293 5294 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5295 uintptr_t a = (uintptr_t)svar->dtsv_data; 5296 size_t sz = v->dtdv_type.dtdt_size; 5297 5298 sz += sizeof (uint64_t); 5299 ASSERT(svar->dtsv_size == NCPU * sz); 5300 a += CPU->cpu_id * sz; 5301 5302 if (regs[rd] == NULL) { 5303 *(uint8_t *)a = UINT8_MAX; 5304 break; 5305 } else { 5306 *(uint8_t *)a = 0; 5307 a += sizeof (uint64_t); 5308 } 5309 5310 if (!dtrace_vcanload( 5311 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5312 mstate, vstate)) 5313 break; 5314 5315 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5316 (void *)a, &v->dtdv_type); 5317 break; 5318 } 5319 5320 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5321 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5322 tmp[CPU->cpu_id] = regs[rd]; 5323 break; 5324 5325 case DIF_OP_LDTS: { 5326 dtrace_dynvar_t *dvar; 5327 dtrace_key_t *key; 5328 5329 id = DIF_INSTR_VAR(instr); 5330 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5331 id -= DIF_VAR_OTHER_UBASE; 5332 v = &vstate->dtvs_tlocals[id]; 5333 5334 key = &tupregs[DIF_DTR_NREGS]; 5335 key[0].dttk_value = (uint64_t)id; 5336 key[0].dttk_size = 0; 5337 DTRACE_TLS_THRKEY(key[1].dttk_value); 5338 key[1].dttk_size = 0; 5339 5340 dvar = dtrace_dynvar(dstate, 2, key, 5341 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 5342 mstate, vstate); 5343 5344 if (dvar == NULL) { 5345 regs[rd] = 0; 5346 break; 5347 } 5348 5349 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5350 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5351 } else { 5352 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5353 } 5354 5355 break; 5356 } 5357 5358 case DIF_OP_STTS: { 5359 dtrace_dynvar_t *dvar; 5360 dtrace_key_t *key; 5361 5362 id = DIF_INSTR_VAR(instr); 5363 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5364 id -= DIF_VAR_OTHER_UBASE; 5365 5366 key = &tupregs[DIF_DTR_NREGS]; 5367 key[0].dttk_value = (uint64_t)id; 5368 key[0].dttk_size = 0; 5369 DTRACE_TLS_THRKEY(key[1].dttk_value); 5370 key[1].dttk_size = 0; 5371 v = &vstate->dtvs_tlocals[id]; 5372 5373 dvar = dtrace_dynvar(dstate, 2, key, 5374 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5375 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5376 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5377 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5378 5379 /* 5380 * Given that we're storing to thread-local data, 5381 * we need to flush our predicate cache. 5382 */ 5383 curthread->t_predcache = NULL; 5384 5385 if (dvar == NULL) 5386 break; 5387 5388 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5389 if (!dtrace_vcanload( 5390 (void *)(uintptr_t)regs[rd], 5391 &v->dtdv_type, mstate, vstate)) 5392 break; 5393 5394 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5395 dvar->dtdv_data, &v->dtdv_type); 5396 } else { 5397 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5398 } 5399 5400 break; 5401 } 5402 5403 case DIF_OP_SRA: 5404 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 5405 break; 5406 5407 case DIF_OP_CALL: 5408 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 5409 regs, tupregs, ttop, mstate, state); 5410 break; 5411 5412 case DIF_OP_PUSHTR: 5413 if (ttop == DIF_DTR_NREGS) { 5414 *flags |= CPU_DTRACE_TUPOFLOW; 5415 break; 5416 } 5417 5418 if (r1 == DIF_TYPE_STRING) { 5419 /* 5420 * If this is a string type and the size is 0, 5421 * we'll use the system-wide default string 5422 * size. Note that we are _not_ looking at 5423 * the value of the DTRACEOPT_STRSIZE option; 5424 * had this been set, we would expect to have 5425 * a non-zero size value in the "pushtr". 5426 */ 5427 tupregs[ttop].dttk_size = 5428 dtrace_strlen((char *)(uintptr_t)regs[rd], 5429 regs[r2] ? regs[r2] : 5430 dtrace_strsize_default) + 1; 5431 } else { 5432 tupregs[ttop].dttk_size = regs[r2]; 5433 } 5434 5435 tupregs[ttop++].dttk_value = regs[rd]; 5436 break; 5437 5438 case DIF_OP_PUSHTV: 5439 if (ttop == DIF_DTR_NREGS) { 5440 *flags |= CPU_DTRACE_TUPOFLOW; 5441 break; 5442 } 5443 5444 tupregs[ttop].dttk_value = regs[rd]; 5445 tupregs[ttop++].dttk_size = 0; 5446 break; 5447 5448 case DIF_OP_POPTS: 5449 if (ttop != 0) 5450 ttop--; 5451 break; 5452 5453 case DIF_OP_FLUSHTS: 5454 ttop = 0; 5455 break; 5456 5457 case DIF_OP_LDGAA: 5458 case DIF_OP_LDTAA: { 5459 dtrace_dynvar_t *dvar; 5460 dtrace_key_t *key = tupregs; 5461 uint_t nkeys = ttop; 5462 5463 id = DIF_INSTR_VAR(instr); 5464 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5465 id -= DIF_VAR_OTHER_UBASE; 5466 5467 key[nkeys].dttk_value = (uint64_t)id; 5468 key[nkeys++].dttk_size = 0; 5469 5470 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 5471 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5472 key[nkeys++].dttk_size = 0; 5473 v = &vstate->dtvs_tlocals[id]; 5474 } else { 5475 v = &vstate->dtvs_globals[id]->dtsv_var; 5476 } 5477 5478 dvar = dtrace_dynvar(dstate, nkeys, key, 5479 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5480 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5481 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 5482 5483 if (dvar == NULL) { 5484 regs[rd] = 0; 5485 break; 5486 } 5487 5488 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5489 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5490 } else { 5491 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5492 } 5493 5494 break; 5495 } 5496 5497 case DIF_OP_STGAA: 5498 case DIF_OP_STTAA: { 5499 dtrace_dynvar_t *dvar; 5500 dtrace_key_t *key = tupregs; 5501 uint_t nkeys = ttop; 5502 5503 id = DIF_INSTR_VAR(instr); 5504 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5505 id -= DIF_VAR_OTHER_UBASE; 5506 5507 key[nkeys].dttk_value = (uint64_t)id; 5508 key[nkeys++].dttk_size = 0; 5509 5510 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 5511 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5512 key[nkeys++].dttk_size = 0; 5513 v = &vstate->dtvs_tlocals[id]; 5514 } else { 5515 v = &vstate->dtvs_globals[id]->dtsv_var; 5516 } 5517 5518 dvar = dtrace_dynvar(dstate, nkeys, key, 5519 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5520 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5521 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5522 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5523 5524 if (dvar == NULL) 5525 break; 5526 5527 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5528 if (!dtrace_vcanload( 5529 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5530 mstate, vstate)) 5531 break; 5532 5533 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5534 dvar->dtdv_data, &v->dtdv_type); 5535 } else { 5536 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5537 } 5538 5539 break; 5540 } 5541 5542 case DIF_OP_ALLOCS: { 5543 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5544 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 5545 5546 /* 5547 * Rounding up the user allocation size could have 5548 * overflowed large, bogus allocations (like -1ULL) to 5549 * 0. 5550 */ 5551 if (size < regs[r1] || 5552 !DTRACE_INSCRATCH(mstate, size)) { 5553 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5554 regs[rd] = NULL; 5555 break; 5556 } 5557 5558 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 5559 mstate->dtms_scratch_ptr += size; 5560 regs[rd] = ptr; 5561 break; 5562 } 5563 5564 case DIF_OP_COPYS: 5565 if (!dtrace_canstore(regs[rd], regs[r2], 5566 mstate, vstate)) { 5567 *flags |= CPU_DTRACE_BADADDR; 5568 *illval = regs[rd]; 5569 break; 5570 } 5571 5572 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 5573 break; 5574 5575 dtrace_bcopy((void *)(uintptr_t)regs[r1], 5576 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 5577 break; 5578 5579 case DIF_OP_STB: 5580 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 5581 *flags |= CPU_DTRACE_BADADDR; 5582 *illval = regs[rd]; 5583 break; 5584 } 5585 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 5586 break; 5587 5588 case DIF_OP_STH: 5589 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 5590 *flags |= CPU_DTRACE_BADADDR; 5591 *illval = regs[rd]; 5592 break; 5593 } 5594 if (regs[rd] & 1) { 5595 *flags |= CPU_DTRACE_BADALIGN; 5596 *illval = regs[rd]; 5597 break; 5598 } 5599 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 5600 break; 5601 5602 case DIF_OP_STW: 5603 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 5604 *flags |= CPU_DTRACE_BADADDR; 5605 *illval = regs[rd]; 5606 break; 5607 } 5608 if (regs[rd] & 3) { 5609 *flags |= CPU_DTRACE_BADALIGN; 5610 *illval = regs[rd]; 5611 break; 5612 } 5613 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 5614 break; 5615 5616 case DIF_OP_STX: 5617 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 5618 *flags |= CPU_DTRACE_BADADDR; 5619 *illval = regs[rd]; 5620 break; 5621 } 5622 if (regs[rd] & 7) { 5623 *flags |= CPU_DTRACE_BADALIGN; 5624 *illval = regs[rd]; 5625 break; 5626 } 5627 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 5628 break; 5629 } 5630 } 5631 5632 if (!(*flags & CPU_DTRACE_FAULT)) 5633 return (rval); 5634 5635 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 5636 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 5637 5638 return (0); 5639 } 5640 5641 static void 5642 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 5643 { 5644 dtrace_probe_t *probe = ecb->dte_probe; 5645 dtrace_provider_t *prov = probe->dtpr_provider; 5646 char c[DTRACE_FULLNAMELEN + 80], *str; 5647 char *msg = "dtrace: breakpoint action at probe "; 5648 char *ecbmsg = " (ecb "; 5649 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 5650 uintptr_t val = (uintptr_t)ecb; 5651 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 5652 5653 if (dtrace_destructive_disallow) 5654 return; 5655 5656 /* 5657 * It's impossible to be taking action on the NULL probe. 5658 */ 5659 ASSERT(probe != NULL); 5660 5661 /* 5662 * This is a poor man's (destitute man's?) sprintf(): we want to 5663 * print the provider name, module name, function name and name of 5664 * the probe, along with the hex address of the ECB with the breakpoint 5665 * action -- all of which we must place in the character buffer by 5666 * hand. 5667 */ 5668 while (*msg != '\0') 5669 c[i++] = *msg++; 5670 5671 for (str = prov->dtpv_name; *str != '\0'; str++) 5672 c[i++] = *str; 5673 c[i++] = ':'; 5674 5675 for (str = probe->dtpr_mod; *str != '\0'; str++) 5676 c[i++] = *str; 5677 c[i++] = ':'; 5678 5679 for (str = probe->dtpr_func; *str != '\0'; str++) 5680 c[i++] = *str; 5681 c[i++] = ':'; 5682 5683 for (str = probe->dtpr_name; *str != '\0'; str++) 5684 c[i++] = *str; 5685 5686 while (*ecbmsg != '\0') 5687 c[i++] = *ecbmsg++; 5688 5689 while (shift >= 0) { 5690 mask = (uintptr_t)0xf << shift; 5691 5692 if (val >= ((uintptr_t)1 << shift)) 5693 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 5694 shift -= 4; 5695 } 5696 5697 c[i++] = ')'; 5698 c[i] = '\0'; 5699 5700 debug_enter(c); 5701 } 5702 5703 static void 5704 dtrace_action_panic(dtrace_ecb_t *ecb) 5705 { 5706 dtrace_probe_t *probe = ecb->dte_probe; 5707 5708 /* 5709 * It's impossible to be taking action on the NULL probe. 5710 */ 5711 ASSERT(probe != NULL); 5712 5713 if (dtrace_destructive_disallow) 5714 return; 5715 5716 if (dtrace_panicked != NULL) 5717 return; 5718 5719 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 5720 return; 5721 5722 /* 5723 * We won the right to panic. (We want to be sure that only one 5724 * thread calls panic() from dtrace_probe(), and that panic() is 5725 * called exactly once.) 5726 */ 5727 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 5728 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 5729 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 5730 } 5731 5732 static void 5733 dtrace_action_raise(uint64_t sig) 5734 { 5735 if (dtrace_destructive_disallow) 5736 return; 5737 5738 if (sig >= NSIG) { 5739 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5740 return; 5741 } 5742 5743 /* 5744 * raise() has a queue depth of 1 -- we ignore all subsequent 5745 * invocations of the raise() action. 5746 */ 5747 if (curthread->t_dtrace_sig == 0) 5748 curthread->t_dtrace_sig = (uint8_t)sig; 5749 5750 curthread->t_sig_check = 1; 5751 aston(curthread); 5752 } 5753 5754 static void 5755 dtrace_action_stop(void) 5756 { 5757 if (dtrace_destructive_disallow) 5758 return; 5759 5760 if (!curthread->t_dtrace_stop) { 5761 curthread->t_dtrace_stop = 1; 5762 curthread->t_sig_check = 1; 5763 aston(curthread); 5764 } 5765 } 5766 5767 static void 5768 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 5769 { 5770 hrtime_t now; 5771 volatile uint16_t *flags; 5772 cpu_t *cpu = CPU; 5773 5774 if (dtrace_destructive_disallow) 5775 return; 5776 5777 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 5778 5779 now = dtrace_gethrtime(); 5780 5781 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 5782 /* 5783 * We need to advance the mark to the current time. 5784 */ 5785 cpu->cpu_dtrace_chillmark = now; 5786 cpu->cpu_dtrace_chilled = 0; 5787 } 5788 5789 /* 5790 * Now check to see if the requested chill time would take us over 5791 * the maximum amount of time allowed in the chill interval. (Or 5792 * worse, if the calculation itself induces overflow.) 5793 */ 5794 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 5795 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 5796 *flags |= CPU_DTRACE_ILLOP; 5797 return; 5798 } 5799 5800 while (dtrace_gethrtime() - now < val) 5801 continue; 5802 5803 /* 5804 * Normally, we assure that the value of the variable "timestamp" does 5805 * not change within an ECB. The presence of chill() represents an 5806 * exception to this rule, however. 5807 */ 5808 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 5809 cpu->cpu_dtrace_chilled += val; 5810 } 5811 5812 static void 5813 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 5814 uint64_t *buf, uint64_t arg) 5815 { 5816 int nframes = DTRACE_USTACK_NFRAMES(arg); 5817 int strsize = DTRACE_USTACK_STRSIZE(arg); 5818 uint64_t *pcs = &buf[1], *fps; 5819 char *str = (char *)&pcs[nframes]; 5820 int size, offs = 0, i, j; 5821 uintptr_t old = mstate->dtms_scratch_ptr, saved; 5822 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 5823 char *sym; 5824 5825 /* 5826 * Should be taking a faster path if string space has not been 5827 * allocated. 5828 */ 5829 ASSERT(strsize != 0); 5830 5831 /* 5832 * We will first allocate some temporary space for the frame pointers. 5833 */ 5834 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5835 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 5836 (nframes * sizeof (uint64_t)); 5837 5838 if (!DTRACE_INSCRATCH(mstate, size)) { 5839 /* 5840 * Not enough room for our frame pointers -- need to indicate 5841 * that we ran out of scratch space. 5842 */ 5843 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5844 return; 5845 } 5846 5847 mstate->dtms_scratch_ptr += size; 5848 saved = mstate->dtms_scratch_ptr; 5849 5850 /* 5851 * Now get a stack with both program counters and frame pointers. 5852 */ 5853 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5854 dtrace_getufpstack(buf, fps, nframes + 1); 5855 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5856 5857 /* 5858 * If that faulted, we're cooked. 5859 */ 5860 if (*flags & CPU_DTRACE_FAULT) 5861 goto out; 5862 5863 /* 5864 * Now we want to walk up the stack, calling the USTACK helper. For 5865 * each iteration, we restore the scratch pointer. 5866 */ 5867 for (i = 0; i < nframes; i++) { 5868 mstate->dtms_scratch_ptr = saved; 5869 5870 if (offs >= strsize) 5871 break; 5872 5873 sym = (char *)(uintptr_t)dtrace_helper( 5874 DTRACE_HELPER_ACTION_USTACK, 5875 mstate, state, pcs[i], fps[i]); 5876 5877 /* 5878 * If we faulted while running the helper, we're going to 5879 * clear the fault and null out the corresponding string. 5880 */ 5881 if (*flags & CPU_DTRACE_FAULT) { 5882 *flags &= ~CPU_DTRACE_FAULT; 5883 str[offs++] = '\0'; 5884 continue; 5885 } 5886 5887 if (sym == NULL) { 5888 str[offs++] = '\0'; 5889 continue; 5890 } 5891 5892 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5893 5894 /* 5895 * Now copy in the string that the helper returned to us. 5896 */ 5897 for (j = 0; offs + j < strsize; j++) { 5898 if ((str[offs + j] = sym[j]) == '\0') 5899 break; 5900 } 5901 5902 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5903 5904 offs += j + 1; 5905 } 5906 5907 if (offs >= strsize) { 5908 /* 5909 * If we didn't have room for all of the strings, we don't 5910 * abort processing -- this needn't be a fatal error -- but we 5911 * still want to increment a counter (dts_stkstroverflows) to 5912 * allow this condition to be warned about. (If this is from 5913 * a jstack() action, it is easily tuned via jstackstrsize.) 5914 */ 5915 dtrace_error(&state->dts_stkstroverflows); 5916 } 5917 5918 while (offs < strsize) 5919 str[offs++] = '\0'; 5920 5921 out: 5922 mstate->dtms_scratch_ptr = old; 5923 } 5924 5925 /* 5926 * If you're looking for the epicenter of DTrace, you just found it. This 5927 * is the function called by the provider to fire a probe -- from which all 5928 * subsequent probe-context DTrace activity emanates. 5929 */ 5930 void 5931 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 5932 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 5933 { 5934 processorid_t cpuid; 5935 dtrace_icookie_t cookie; 5936 dtrace_probe_t *probe; 5937 dtrace_mstate_t mstate; 5938 dtrace_ecb_t *ecb; 5939 dtrace_action_t *act; 5940 intptr_t offs; 5941 size_t size; 5942 int vtime, onintr; 5943 volatile uint16_t *flags; 5944 hrtime_t now; 5945 5946 /* 5947 * Kick out immediately if this CPU is still being born (in which case 5948 * curthread will be set to -1) or the current thread can't allow 5949 * probes in its current context. 5950 */ 5951 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 5952 return; 5953 5954 cookie = dtrace_interrupt_disable(); 5955 probe = dtrace_probes[id - 1]; 5956 cpuid = CPU->cpu_id; 5957 onintr = CPU_ON_INTR(CPU); 5958 5959 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 5960 probe->dtpr_predcache == curthread->t_predcache) { 5961 /* 5962 * We have hit in the predicate cache; we know that 5963 * this predicate would evaluate to be false. 5964 */ 5965 dtrace_interrupt_enable(cookie); 5966 return; 5967 } 5968 5969 if (panic_quiesce) { 5970 /* 5971 * We don't trace anything if we're panicking. 5972 */ 5973 dtrace_interrupt_enable(cookie); 5974 return; 5975 } 5976 5977 now = dtrace_gethrtime(); 5978 vtime = dtrace_vtime_references != 0; 5979 5980 if (vtime && curthread->t_dtrace_start) 5981 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 5982 5983 mstate.dtms_difo = NULL; 5984 mstate.dtms_probe = probe; 5985 mstate.dtms_strtok = NULL; 5986 mstate.dtms_arg[0] = arg0; 5987 mstate.dtms_arg[1] = arg1; 5988 mstate.dtms_arg[2] = arg2; 5989 mstate.dtms_arg[3] = arg3; 5990 mstate.dtms_arg[4] = arg4; 5991 5992 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 5993 5994 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 5995 dtrace_predicate_t *pred = ecb->dte_predicate; 5996 dtrace_state_t *state = ecb->dte_state; 5997 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 5998 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 5999 dtrace_vstate_t *vstate = &state->dts_vstate; 6000 dtrace_provider_t *prov = probe->dtpr_provider; 6001 uint64_t tracememsize = 0; 6002 int committed = 0; 6003 caddr_t tomax; 6004 6005 /* 6006 * A little subtlety with the following (seemingly innocuous) 6007 * declaration of the automatic 'val': by looking at the 6008 * code, you might think that it could be declared in the 6009 * action processing loop, below. (That is, it's only used in 6010 * the action processing loop.) However, it must be declared 6011 * out of that scope because in the case of DIF expression 6012 * arguments to aggregating actions, one iteration of the 6013 * action loop will use the last iteration's value. 6014 */ 6015 #ifdef lint 6016 uint64_t val = 0; 6017 #else 6018 uint64_t val; 6019 #endif 6020 6021 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 6022 mstate.dtms_access = DTRACE_ACCESS_ARGS | DTRACE_ACCESS_PROC; 6023 mstate.dtms_getf = NULL; 6024 6025 *flags &= ~CPU_DTRACE_ERROR; 6026 6027 if (prov == dtrace_provider) { 6028 /* 6029 * If dtrace itself is the provider of this probe, 6030 * we're only going to continue processing the ECB if 6031 * arg0 (the dtrace_state_t) is equal to the ECB's 6032 * creating state. (This prevents disjoint consumers 6033 * from seeing one another's metaprobes.) 6034 */ 6035 if (arg0 != (uint64_t)(uintptr_t)state) 6036 continue; 6037 } 6038 6039 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 6040 /* 6041 * We're not currently active. If our provider isn't 6042 * the dtrace pseudo provider, we're not interested. 6043 */ 6044 if (prov != dtrace_provider) 6045 continue; 6046 6047 /* 6048 * Now we must further check if we are in the BEGIN 6049 * probe. If we are, we will only continue processing 6050 * if we're still in WARMUP -- if one BEGIN enabling 6051 * has invoked the exit() action, we don't want to 6052 * evaluate subsequent BEGIN enablings. 6053 */ 6054 if (probe->dtpr_id == dtrace_probeid_begin && 6055 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 6056 ASSERT(state->dts_activity == 6057 DTRACE_ACTIVITY_DRAINING); 6058 continue; 6059 } 6060 } 6061 6062 if (ecb->dte_cond && !dtrace_priv_probe(state, &mstate, ecb)) 6063 continue; 6064 6065 if (now - state->dts_alive > dtrace_deadman_timeout) { 6066 /* 6067 * We seem to be dead. Unless we (a) have kernel 6068 * destructive permissions (b) have expicitly enabled 6069 * destructive actions and (c) destructive actions have 6070 * not been disabled, we're going to transition into 6071 * the KILLED state, from which no further processing 6072 * on this state will be performed. 6073 */ 6074 if (!dtrace_priv_kernel_destructive(state) || 6075 !state->dts_cred.dcr_destructive || 6076 dtrace_destructive_disallow) { 6077 void *activity = &state->dts_activity; 6078 dtrace_activity_t current; 6079 6080 do { 6081 current = state->dts_activity; 6082 } while (dtrace_cas32(activity, current, 6083 DTRACE_ACTIVITY_KILLED) != current); 6084 6085 continue; 6086 } 6087 } 6088 6089 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 6090 ecb->dte_alignment, state, &mstate)) < 0) 6091 continue; 6092 6093 tomax = buf->dtb_tomax; 6094 ASSERT(tomax != NULL); 6095 6096 if (ecb->dte_size != 0) 6097 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 6098 6099 mstate.dtms_epid = ecb->dte_epid; 6100 mstate.dtms_present |= DTRACE_MSTATE_EPID; 6101 6102 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 6103 mstate.dtms_access |= DTRACE_ACCESS_KERNEL; 6104 6105 if (pred != NULL) { 6106 dtrace_difo_t *dp = pred->dtp_difo; 6107 int rval; 6108 6109 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 6110 6111 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 6112 dtrace_cacheid_t cid = probe->dtpr_predcache; 6113 6114 if (cid != DTRACE_CACHEIDNONE && !onintr) { 6115 /* 6116 * Update the predicate cache... 6117 */ 6118 ASSERT(cid == pred->dtp_cacheid); 6119 curthread->t_predcache = cid; 6120 } 6121 6122 continue; 6123 } 6124 } 6125 6126 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 6127 act != NULL; act = act->dta_next) { 6128 size_t valoffs; 6129 dtrace_difo_t *dp; 6130 dtrace_recdesc_t *rec = &act->dta_rec; 6131 6132 size = rec->dtrd_size; 6133 valoffs = offs + rec->dtrd_offset; 6134 6135 if (DTRACEACT_ISAGG(act->dta_kind)) { 6136 uint64_t v = 0xbad; 6137 dtrace_aggregation_t *agg; 6138 6139 agg = (dtrace_aggregation_t *)act; 6140 6141 if ((dp = act->dta_difo) != NULL) 6142 v = dtrace_dif_emulate(dp, 6143 &mstate, vstate, state); 6144 6145 if (*flags & CPU_DTRACE_ERROR) 6146 continue; 6147 6148 /* 6149 * Note that we always pass the expression 6150 * value from the previous iteration of the 6151 * action loop. This value will only be used 6152 * if there is an expression argument to the 6153 * aggregating action, denoted by the 6154 * dtag_hasarg field. 6155 */ 6156 dtrace_aggregate(agg, buf, 6157 offs, aggbuf, v, val); 6158 continue; 6159 } 6160 6161 switch (act->dta_kind) { 6162 case DTRACEACT_STOP: 6163 if (dtrace_priv_proc_destructive(state, 6164 &mstate)) 6165 dtrace_action_stop(); 6166 continue; 6167 6168 case DTRACEACT_BREAKPOINT: 6169 if (dtrace_priv_kernel_destructive(state)) 6170 dtrace_action_breakpoint(ecb); 6171 continue; 6172 6173 case DTRACEACT_PANIC: 6174 if (dtrace_priv_kernel_destructive(state)) 6175 dtrace_action_panic(ecb); 6176 continue; 6177 6178 case DTRACEACT_STACK: 6179 if (!dtrace_priv_kernel(state)) 6180 continue; 6181 6182 dtrace_getpcstack((pc_t *)(tomax + valoffs), 6183 size / sizeof (pc_t), probe->dtpr_aframes, 6184 DTRACE_ANCHORED(probe) ? NULL : 6185 (uint32_t *)arg0); 6186 6187 continue; 6188 6189 case DTRACEACT_JSTACK: 6190 case DTRACEACT_USTACK: 6191 if (!dtrace_priv_proc(state, &mstate)) 6192 continue; 6193 6194 /* 6195 * See comment in DIF_VAR_PID. 6196 */ 6197 if (DTRACE_ANCHORED(mstate.dtms_probe) && 6198 CPU_ON_INTR(CPU)) { 6199 int depth = DTRACE_USTACK_NFRAMES( 6200 rec->dtrd_arg) + 1; 6201 6202 dtrace_bzero((void *)(tomax + valoffs), 6203 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 6204 + depth * sizeof (uint64_t)); 6205 6206 continue; 6207 } 6208 6209 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 6210 curproc->p_dtrace_helpers != NULL) { 6211 /* 6212 * This is the slow path -- we have 6213 * allocated string space, and we're 6214 * getting the stack of a process that 6215 * has helpers. Call into a separate 6216 * routine to perform this processing. 6217 */ 6218 dtrace_action_ustack(&mstate, state, 6219 (uint64_t *)(tomax + valoffs), 6220 rec->dtrd_arg); 6221 continue; 6222 } 6223 6224 /* 6225 * Clear the string space, since there's no 6226 * helper to do it for us. 6227 */ 6228 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0) { 6229 int depth = DTRACE_USTACK_NFRAMES( 6230 rec->dtrd_arg); 6231 size_t strsize = DTRACE_USTACK_STRSIZE( 6232 rec->dtrd_arg); 6233 uint64_t *buf = (uint64_t *)(tomax + 6234 valoffs); 6235 void *strspace = &buf[depth + 1]; 6236 6237 dtrace_bzero(strspace, 6238 MIN(depth, strsize)); 6239 } 6240 6241 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6242 dtrace_getupcstack((uint64_t *) 6243 (tomax + valoffs), 6244 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 6245 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6246 continue; 6247 6248 default: 6249 break; 6250 } 6251 6252 dp = act->dta_difo; 6253 ASSERT(dp != NULL); 6254 6255 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 6256 6257 if (*flags & CPU_DTRACE_ERROR) 6258 continue; 6259 6260 switch (act->dta_kind) { 6261 case DTRACEACT_SPECULATE: 6262 ASSERT(buf == &state->dts_buffer[cpuid]); 6263 buf = dtrace_speculation_buffer(state, 6264 cpuid, val); 6265 6266 if (buf == NULL) { 6267 *flags |= CPU_DTRACE_DROP; 6268 continue; 6269 } 6270 6271 offs = dtrace_buffer_reserve(buf, 6272 ecb->dte_needed, ecb->dte_alignment, 6273 state, NULL); 6274 6275 if (offs < 0) { 6276 *flags |= CPU_DTRACE_DROP; 6277 continue; 6278 } 6279 6280 tomax = buf->dtb_tomax; 6281 ASSERT(tomax != NULL); 6282 6283 if (ecb->dte_size != 0) 6284 DTRACE_STORE(uint32_t, tomax, offs, 6285 ecb->dte_epid); 6286 continue; 6287 6288 case DTRACEACT_CHILL: 6289 if (dtrace_priv_kernel_destructive(state)) 6290 dtrace_action_chill(&mstate, val); 6291 continue; 6292 6293 case DTRACEACT_RAISE: 6294 if (dtrace_priv_proc_destructive(state, 6295 &mstate)) 6296 dtrace_action_raise(val); 6297 continue; 6298 6299 case DTRACEACT_COMMIT: 6300 ASSERT(!committed); 6301 6302 /* 6303 * We need to commit our buffer state. 6304 */ 6305 if (ecb->dte_size) 6306 buf->dtb_offset = offs + ecb->dte_size; 6307 buf = &state->dts_buffer[cpuid]; 6308 dtrace_speculation_commit(state, cpuid, val); 6309 committed = 1; 6310 continue; 6311 6312 case DTRACEACT_DISCARD: 6313 dtrace_speculation_discard(state, cpuid, val); 6314 continue; 6315 6316 case DTRACEACT_DIFEXPR: 6317 case DTRACEACT_LIBACT: 6318 case DTRACEACT_PRINTF: 6319 case DTRACEACT_PRINTA: 6320 case DTRACEACT_SYSTEM: 6321 case DTRACEACT_FREOPEN: 6322 case DTRACEACT_TRACEMEM: 6323 break; 6324 6325 case DTRACEACT_TRACEMEM_DYNSIZE: 6326 tracememsize = val; 6327 break; 6328 6329 case DTRACEACT_SYM: 6330 case DTRACEACT_MOD: 6331 if (!dtrace_priv_kernel(state)) 6332 continue; 6333 break; 6334 6335 case DTRACEACT_USYM: 6336 case DTRACEACT_UMOD: 6337 case DTRACEACT_UADDR: { 6338 struct pid *pid = curthread->t_procp->p_pidp; 6339 6340 if (!dtrace_priv_proc(state, &mstate)) 6341 continue; 6342 6343 DTRACE_STORE(uint64_t, tomax, 6344 valoffs, (uint64_t)pid->pid_id); 6345 DTRACE_STORE(uint64_t, tomax, 6346 valoffs + sizeof (uint64_t), val); 6347 6348 continue; 6349 } 6350 6351 case DTRACEACT_EXIT: { 6352 /* 6353 * For the exit action, we are going to attempt 6354 * to atomically set our activity to be 6355 * draining. If this fails (either because 6356 * another CPU has beat us to the exit action, 6357 * or because our current activity is something 6358 * other than ACTIVE or WARMUP), we will 6359 * continue. This assures that the exit action 6360 * can be successfully recorded at most once 6361 * when we're in the ACTIVE state. If we're 6362 * encountering the exit() action while in 6363 * COOLDOWN, however, we want to honor the new 6364 * status code. (We know that we're the only 6365 * thread in COOLDOWN, so there is no race.) 6366 */ 6367 void *activity = &state->dts_activity; 6368 dtrace_activity_t current = state->dts_activity; 6369 6370 if (current == DTRACE_ACTIVITY_COOLDOWN) 6371 break; 6372 6373 if (current != DTRACE_ACTIVITY_WARMUP) 6374 current = DTRACE_ACTIVITY_ACTIVE; 6375 6376 if (dtrace_cas32(activity, current, 6377 DTRACE_ACTIVITY_DRAINING) != current) { 6378 *flags |= CPU_DTRACE_DROP; 6379 continue; 6380 } 6381 6382 break; 6383 } 6384 6385 default: 6386 ASSERT(0); 6387 } 6388 6389 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 6390 uintptr_t end = valoffs + size; 6391 6392 if (tracememsize != 0 && 6393 valoffs + tracememsize < end) { 6394 end = valoffs + tracememsize; 6395 tracememsize = 0; 6396 } 6397 6398 if (!dtrace_vcanload((void *)(uintptr_t)val, 6399 &dp->dtdo_rtype, &mstate, vstate)) 6400 continue; 6401 6402 /* 6403 * If this is a string, we're going to only 6404 * load until we find the zero byte -- after 6405 * which we'll store zero bytes. 6406 */ 6407 if (dp->dtdo_rtype.dtdt_kind == 6408 DIF_TYPE_STRING) { 6409 char c = '\0' + 1; 6410 int intuple = act->dta_intuple; 6411 size_t s; 6412 6413 for (s = 0; s < size; s++) { 6414 if (c != '\0') 6415 c = dtrace_load8(val++); 6416 6417 DTRACE_STORE(uint8_t, tomax, 6418 valoffs++, c); 6419 6420 if (c == '\0' && intuple) 6421 break; 6422 } 6423 6424 continue; 6425 } 6426 6427 while (valoffs < end) { 6428 DTRACE_STORE(uint8_t, tomax, valoffs++, 6429 dtrace_load8(val++)); 6430 } 6431 6432 continue; 6433 } 6434 6435 switch (size) { 6436 case 0: 6437 break; 6438 6439 case sizeof (uint8_t): 6440 DTRACE_STORE(uint8_t, tomax, valoffs, val); 6441 break; 6442 case sizeof (uint16_t): 6443 DTRACE_STORE(uint16_t, tomax, valoffs, val); 6444 break; 6445 case sizeof (uint32_t): 6446 DTRACE_STORE(uint32_t, tomax, valoffs, val); 6447 break; 6448 case sizeof (uint64_t): 6449 DTRACE_STORE(uint64_t, tomax, valoffs, val); 6450 break; 6451 default: 6452 /* 6453 * Any other size should have been returned by 6454 * reference, not by value. 6455 */ 6456 ASSERT(0); 6457 break; 6458 } 6459 } 6460 6461 if (*flags & CPU_DTRACE_DROP) 6462 continue; 6463 6464 if (*flags & CPU_DTRACE_FAULT) { 6465 int ndx; 6466 dtrace_action_t *err; 6467 6468 buf->dtb_errors++; 6469 6470 if (probe->dtpr_id == dtrace_probeid_error) { 6471 /* 6472 * There's nothing we can do -- we had an 6473 * error on the error probe. We bump an 6474 * error counter to at least indicate that 6475 * this condition happened. 6476 */ 6477 dtrace_error(&state->dts_dblerrors); 6478 continue; 6479 } 6480 6481 if (vtime) { 6482 /* 6483 * Before recursing on dtrace_probe(), we 6484 * need to explicitly clear out our start 6485 * time to prevent it from being accumulated 6486 * into t_dtrace_vtime. 6487 */ 6488 curthread->t_dtrace_start = 0; 6489 } 6490 6491 /* 6492 * Iterate over the actions to figure out which action 6493 * we were processing when we experienced the error. 6494 * Note that act points _past_ the faulting action; if 6495 * act is ecb->dte_action, the fault was in the 6496 * predicate, if it's ecb->dte_action->dta_next it's 6497 * in action #1, and so on. 6498 */ 6499 for (err = ecb->dte_action, ndx = 0; 6500 err != act; err = err->dta_next, ndx++) 6501 continue; 6502 6503 dtrace_probe_error(state, ecb->dte_epid, ndx, 6504 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 6505 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 6506 cpu_core[cpuid].cpuc_dtrace_illval); 6507 6508 continue; 6509 } 6510 6511 if (!committed) 6512 buf->dtb_offset = offs + ecb->dte_size; 6513 } 6514 6515 if (vtime) 6516 curthread->t_dtrace_start = dtrace_gethrtime(); 6517 6518 dtrace_interrupt_enable(cookie); 6519 } 6520 6521 /* 6522 * DTrace Probe Hashing Functions 6523 * 6524 * The functions in this section (and indeed, the functions in remaining 6525 * sections) are not _called_ from probe context. (Any exceptions to this are 6526 * marked with a "Note:".) Rather, they are called from elsewhere in the 6527 * DTrace framework to look-up probes in, add probes to and remove probes from 6528 * the DTrace probe hashes. (Each probe is hashed by each element of the 6529 * probe tuple -- allowing for fast lookups, regardless of what was 6530 * specified.) 6531 */ 6532 static uint_t 6533 dtrace_hash_str(char *p) 6534 { 6535 unsigned int g; 6536 uint_t hval = 0; 6537 6538 while (*p) { 6539 hval = (hval << 4) + *p++; 6540 if ((g = (hval & 0xf0000000)) != 0) 6541 hval ^= g >> 24; 6542 hval &= ~g; 6543 } 6544 return (hval); 6545 } 6546 6547 static dtrace_hash_t * 6548 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 6549 { 6550 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 6551 6552 hash->dth_stroffs = stroffs; 6553 hash->dth_nextoffs = nextoffs; 6554 hash->dth_prevoffs = prevoffs; 6555 6556 hash->dth_size = 1; 6557 hash->dth_mask = hash->dth_size - 1; 6558 6559 hash->dth_tab = kmem_zalloc(hash->dth_size * 6560 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 6561 6562 return (hash); 6563 } 6564 6565 static void 6566 dtrace_hash_destroy(dtrace_hash_t *hash) 6567 { 6568 #ifdef DEBUG 6569 int i; 6570 6571 for (i = 0; i < hash->dth_size; i++) 6572 ASSERT(hash->dth_tab[i] == NULL); 6573 #endif 6574 6575 kmem_free(hash->dth_tab, 6576 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 6577 kmem_free(hash, sizeof (dtrace_hash_t)); 6578 } 6579 6580 static void 6581 dtrace_hash_resize(dtrace_hash_t *hash) 6582 { 6583 int size = hash->dth_size, i, ndx; 6584 int new_size = hash->dth_size << 1; 6585 int new_mask = new_size - 1; 6586 dtrace_hashbucket_t **new_tab, *bucket, *next; 6587 6588 ASSERT((new_size & new_mask) == 0); 6589 6590 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 6591 6592 for (i = 0; i < size; i++) { 6593 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 6594 dtrace_probe_t *probe = bucket->dthb_chain; 6595 6596 ASSERT(probe != NULL); 6597 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 6598 6599 next = bucket->dthb_next; 6600 bucket->dthb_next = new_tab[ndx]; 6601 new_tab[ndx] = bucket; 6602 } 6603 } 6604 6605 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 6606 hash->dth_tab = new_tab; 6607 hash->dth_size = new_size; 6608 hash->dth_mask = new_mask; 6609 } 6610 6611 static void 6612 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 6613 { 6614 int hashval = DTRACE_HASHSTR(hash, new); 6615 int ndx = hashval & hash->dth_mask; 6616 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6617 dtrace_probe_t **nextp, **prevp; 6618 6619 for (; bucket != NULL; bucket = bucket->dthb_next) { 6620 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 6621 goto add; 6622 } 6623 6624 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 6625 dtrace_hash_resize(hash); 6626 dtrace_hash_add(hash, new); 6627 return; 6628 } 6629 6630 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 6631 bucket->dthb_next = hash->dth_tab[ndx]; 6632 hash->dth_tab[ndx] = bucket; 6633 hash->dth_nbuckets++; 6634 6635 add: 6636 nextp = DTRACE_HASHNEXT(hash, new); 6637 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 6638 *nextp = bucket->dthb_chain; 6639 6640 if (bucket->dthb_chain != NULL) { 6641 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 6642 ASSERT(*prevp == NULL); 6643 *prevp = new; 6644 } 6645 6646 bucket->dthb_chain = new; 6647 bucket->dthb_len++; 6648 } 6649 6650 static dtrace_probe_t * 6651 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 6652 { 6653 int hashval = DTRACE_HASHSTR(hash, template); 6654 int ndx = hashval & hash->dth_mask; 6655 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6656 6657 for (; bucket != NULL; bucket = bucket->dthb_next) { 6658 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6659 return (bucket->dthb_chain); 6660 } 6661 6662 return (NULL); 6663 } 6664 6665 static int 6666 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 6667 { 6668 int hashval = DTRACE_HASHSTR(hash, template); 6669 int ndx = hashval & hash->dth_mask; 6670 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6671 6672 for (; bucket != NULL; bucket = bucket->dthb_next) { 6673 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6674 return (bucket->dthb_len); 6675 } 6676 6677 return (NULL); 6678 } 6679 6680 static void 6681 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 6682 { 6683 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 6684 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6685 6686 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 6687 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 6688 6689 /* 6690 * Find the bucket that we're removing this probe from. 6691 */ 6692 for (; bucket != NULL; bucket = bucket->dthb_next) { 6693 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 6694 break; 6695 } 6696 6697 ASSERT(bucket != NULL); 6698 6699 if (*prevp == NULL) { 6700 if (*nextp == NULL) { 6701 /* 6702 * The removed probe was the only probe on this 6703 * bucket; we need to remove the bucket. 6704 */ 6705 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 6706 6707 ASSERT(bucket->dthb_chain == probe); 6708 ASSERT(b != NULL); 6709 6710 if (b == bucket) { 6711 hash->dth_tab[ndx] = bucket->dthb_next; 6712 } else { 6713 while (b->dthb_next != bucket) 6714 b = b->dthb_next; 6715 b->dthb_next = bucket->dthb_next; 6716 } 6717 6718 ASSERT(hash->dth_nbuckets > 0); 6719 hash->dth_nbuckets--; 6720 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 6721 return; 6722 } 6723 6724 bucket->dthb_chain = *nextp; 6725 } else { 6726 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 6727 } 6728 6729 if (*nextp != NULL) 6730 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 6731 } 6732 6733 /* 6734 * DTrace Utility Functions 6735 * 6736 * These are random utility functions that are _not_ called from probe context. 6737 */ 6738 static int 6739 dtrace_badattr(const dtrace_attribute_t *a) 6740 { 6741 return (a->dtat_name > DTRACE_STABILITY_MAX || 6742 a->dtat_data > DTRACE_STABILITY_MAX || 6743 a->dtat_class > DTRACE_CLASS_MAX); 6744 } 6745 6746 /* 6747 * Return a duplicate copy of a string. If the specified string is NULL, 6748 * this function returns a zero-length string. 6749 */ 6750 static char * 6751 dtrace_strdup(const char *str) 6752 { 6753 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 6754 6755 if (str != NULL) 6756 (void) strcpy(new, str); 6757 6758 return (new); 6759 } 6760 6761 #define DTRACE_ISALPHA(c) \ 6762 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 6763 6764 static int 6765 dtrace_badname(const char *s) 6766 { 6767 char c; 6768 6769 if (s == NULL || (c = *s++) == '\0') 6770 return (0); 6771 6772 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 6773 return (1); 6774 6775 while ((c = *s++) != '\0') { 6776 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 6777 c != '-' && c != '_' && c != '.' && c != '`') 6778 return (1); 6779 } 6780 6781 return (0); 6782 } 6783 6784 static void 6785 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 6786 { 6787 uint32_t priv; 6788 6789 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 6790 /* 6791 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 6792 */ 6793 priv = DTRACE_PRIV_ALL; 6794 } else { 6795 *uidp = crgetuid(cr); 6796 *zoneidp = crgetzoneid(cr); 6797 6798 priv = 0; 6799 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 6800 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 6801 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 6802 priv |= DTRACE_PRIV_USER; 6803 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 6804 priv |= DTRACE_PRIV_PROC; 6805 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 6806 priv |= DTRACE_PRIV_OWNER; 6807 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 6808 priv |= DTRACE_PRIV_ZONEOWNER; 6809 } 6810 6811 *privp = priv; 6812 } 6813 6814 #ifdef DTRACE_ERRDEBUG 6815 static void 6816 dtrace_errdebug(const char *str) 6817 { 6818 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; 6819 int occupied = 0; 6820 6821 mutex_enter(&dtrace_errlock); 6822 dtrace_errlast = str; 6823 dtrace_errthread = curthread; 6824 6825 while (occupied++ < DTRACE_ERRHASHSZ) { 6826 if (dtrace_errhash[hval].dter_msg == str) { 6827 dtrace_errhash[hval].dter_count++; 6828 goto out; 6829 } 6830 6831 if (dtrace_errhash[hval].dter_msg != NULL) { 6832 hval = (hval + 1) % DTRACE_ERRHASHSZ; 6833 continue; 6834 } 6835 6836 dtrace_errhash[hval].dter_msg = str; 6837 dtrace_errhash[hval].dter_count = 1; 6838 goto out; 6839 } 6840 6841 panic("dtrace: undersized error hash"); 6842 out: 6843 mutex_exit(&dtrace_errlock); 6844 } 6845 #endif 6846 6847 /* 6848 * DTrace Matching Functions 6849 * 6850 * These functions are used to match groups of probes, given some elements of 6851 * a probe tuple, or some globbed expressions for elements of a probe tuple. 6852 */ 6853 static int 6854 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 6855 zoneid_t zoneid) 6856 { 6857 if (priv != DTRACE_PRIV_ALL) { 6858 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 6859 uint32_t match = priv & ppriv; 6860 6861 /* 6862 * No PRIV_DTRACE_* privileges... 6863 */ 6864 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 6865 DTRACE_PRIV_KERNEL)) == 0) 6866 return (0); 6867 6868 /* 6869 * No matching bits, but there were bits to match... 6870 */ 6871 if (match == 0 && ppriv != 0) 6872 return (0); 6873 6874 /* 6875 * Need to have permissions to the process, but don't... 6876 */ 6877 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 6878 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 6879 return (0); 6880 } 6881 6882 /* 6883 * Need to be in the same zone unless we possess the 6884 * privilege to examine all zones. 6885 */ 6886 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 6887 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 6888 return (0); 6889 } 6890 } 6891 6892 return (1); 6893 } 6894 6895 /* 6896 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 6897 * consists of input pattern strings and an ops-vector to evaluate them. 6898 * This function returns >0 for match, 0 for no match, and <0 for error. 6899 */ 6900 static int 6901 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 6902 uint32_t priv, uid_t uid, zoneid_t zoneid) 6903 { 6904 dtrace_provider_t *pvp = prp->dtpr_provider; 6905 int rv; 6906 6907 if (pvp->dtpv_defunct) 6908 return (0); 6909 6910 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 6911 return (rv); 6912 6913 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 6914 return (rv); 6915 6916 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 6917 return (rv); 6918 6919 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 6920 return (rv); 6921 6922 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 6923 return (0); 6924 6925 return (rv); 6926 } 6927 6928 /* 6929 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 6930 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 6931 * libc's version, the kernel version only applies to 8-bit ASCII strings. 6932 * In addition, all of the recursion cases except for '*' matching have been 6933 * unwound. For '*', we still implement recursive evaluation, but a depth 6934 * counter is maintained and matching is aborted if we recurse too deep. 6935 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 6936 */ 6937 static int 6938 dtrace_match_glob(const char *s, const char *p, int depth) 6939 { 6940 const char *olds; 6941 char s1, c; 6942 int gs; 6943 6944 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 6945 return (-1); 6946 6947 if (s == NULL) 6948 s = ""; /* treat NULL as empty string */ 6949 6950 top: 6951 olds = s; 6952 s1 = *s++; 6953 6954 if (p == NULL) 6955 return (0); 6956 6957 if ((c = *p++) == '\0') 6958 return (s1 == '\0'); 6959 6960 switch (c) { 6961 case '[': { 6962 int ok = 0, notflag = 0; 6963 char lc = '\0'; 6964 6965 if (s1 == '\0') 6966 return (0); 6967 6968 if (*p == '!') { 6969 notflag = 1; 6970 p++; 6971 } 6972 6973 if ((c = *p++) == '\0') 6974 return (0); 6975 6976 do { 6977 if (c == '-' && lc != '\0' && *p != ']') { 6978 if ((c = *p++) == '\0') 6979 return (0); 6980 if (c == '\\' && (c = *p++) == '\0') 6981 return (0); 6982 6983 if (notflag) { 6984 if (s1 < lc || s1 > c) 6985 ok++; 6986 else 6987 return (0); 6988 } else if (lc <= s1 && s1 <= c) 6989 ok++; 6990 6991 } else if (c == '\\' && (c = *p++) == '\0') 6992 return (0); 6993 6994 lc = c; /* save left-hand 'c' for next iteration */ 6995 6996 if (notflag) { 6997 if (s1 != c) 6998 ok++; 6999 else 7000 return (0); 7001 } else if (s1 == c) 7002 ok++; 7003 7004 if ((c = *p++) == '\0') 7005 return (0); 7006 7007 } while (c != ']'); 7008 7009 if (ok) 7010 goto top; 7011 7012 return (0); 7013 } 7014 7015 case '\\': 7016 if ((c = *p++) == '\0') 7017 return (0); 7018 /*FALLTHRU*/ 7019 7020 default: 7021 if (c != s1) 7022 return (0); 7023 /*FALLTHRU*/ 7024 7025 case '?': 7026 if (s1 != '\0') 7027 goto top; 7028 return (0); 7029 7030 case '*': 7031 while (*p == '*') 7032 p++; /* consecutive *'s are identical to a single one */ 7033 7034 if (*p == '\0') 7035 return (1); 7036 7037 for (s = olds; *s != '\0'; s++) { 7038 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 7039 return (gs); 7040 } 7041 7042 return (0); 7043 } 7044 } 7045 7046 /*ARGSUSED*/ 7047 static int 7048 dtrace_match_string(const char *s, const char *p, int depth) 7049 { 7050 return (s != NULL && strcmp(s, p) == 0); 7051 } 7052 7053 /*ARGSUSED*/ 7054 static int 7055 dtrace_match_nul(const char *s, const char *p, int depth) 7056 { 7057 return (1); /* always match the empty pattern */ 7058 } 7059 7060 /*ARGSUSED*/ 7061 static int 7062 dtrace_match_nonzero(const char *s, const char *p, int depth) 7063 { 7064 return (s != NULL && s[0] != '\0'); 7065 } 7066 7067 static int 7068 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 7069 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 7070 { 7071 dtrace_probe_t template, *probe; 7072 dtrace_hash_t *hash = NULL; 7073 int len, rc, best = INT_MAX, nmatched = 0; 7074 dtrace_id_t i; 7075 7076 ASSERT(MUTEX_HELD(&dtrace_lock)); 7077 7078 /* 7079 * If the probe ID is specified in the key, just lookup by ID and 7080 * invoke the match callback once if a matching probe is found. 7081 */ 7082 if (pkp->dtpk_id != DTRACE_IDNONE) { 7083 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 7084 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 7085 if ((*matched)(probe, arg) == DTRACE_MATCH_FAIL) 7086 return (DTRACE_MATCH_FAIL); 7087 nmatched++; 7088 } 7089 return (nmatched); 7090 } 7091 7092 template.dtpr_mod = (char *)pkp->dtpk_mod; 7093 template.dtpr_func = (char *)pkp->dtpk_func; 7094 template.dtpr_name = (char *)pkp->dtpk_name; 7095 7096 /* 7097 * We want to find the most distinct of the module name, function 7098 * name, and name. So for each one that is not a glob pattern or 7099 * empty string, we perform a lookup in the corresponding hash and 7100 * use the hash table with the fewest collisions to do our search. 7101 */ 7102 if (pkp->dtpk_mmatch == &dtrace_match_string && 7103 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 7104 best = len; 7105 hash = dtrace_bymod; 7106 } 7107 7108 if (pkp->dtpk_fmatch == &dtrace_match_string && 7109 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 7110 best = len; 7111 hash = dtrace_byfunc; 7112 } 7113 7114 if (pkp->dtpk_nmatch == &dtrace_match_string && 7115 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 7116 best = len; 7117 hash = dtrace_byname; 7118 } 7119 7120 /* 7121 * If we did not select a hash table, iterate over every probe and 7122 * invoke our callback for each one that matches our input probe key. 7123 */ 7124 if (hash == NULL) { 7125 for (i = 0; i < dtrace_nprobes; i++) { 7126 if ((probe = dtrace_probes[i]) == NULL || 7127 dtrace_match_probe(probe, pkp, priv, uid, 7128 zoneid) <= 0) 7129 continue; 7130 7131 nmatched++; 7132 7133 if ((rc = (*matched)(probe, arg)) != 7134 DTRACE_MATCH_NEXT) { 7135 if (rc == DTRACE_MATCH_FAIL) 7136 return (DTRACE_MATCH_FAIL); 7137 break; 7138 } 7139 } 7140 7141 return (nmatched); 7142 } 7143 7144 /* 7145 * If we selected a hash table, iterate over each probe of the same key 7146 * name and invoke the callback for every probe that matches the other 7147 * attributes of our input probe key. 7148 */ 7149 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 7150 probe = *(DTRACE_HASHNEXT(hash, probe))) { 7151 7152 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 7153 continue; 7154 7155 nmatched++; 7156 7157 if ((rc = (*matched)(probe, arg)) != DTRACE_MATCH_NEXT) { 7158 if (rc == DTRACE_MATCH_FAIL) 7159 return (DTRACE_MATCH_FAIL); 7160 break; 7161 } 7162 } 7163 7164 return (nmatched); 7165 } 7166 7167 /* 7168 * Return the function pointer dtrace_probecmp() should use to compare the 7169 * specified pattern with a string. For NULL or empty patterns, we select 7170 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 7171 * For non-empty non-glob strings, we use dtrace_match_string(). 7172 */ 7173 static dtrace_probekey_f * 7174 dtrace_probekey_func(const char *p) 7175 { 7176 char c; 7177 7178 if (p == NULL || *p == '\0') 7179 return (&dtrace_match_nul); 7180 7181 while ((c = *p++) != '\0') { 7182 if (c == '[' || c == '?' || c == '*' || c == '\\') 7183 return (&dtrace_match_glob); 7184 } 7185 7186 return (&dtrace_match_string); 7187 } 7188 7189 /* 7190 * Build a probe comparison key for use with dtrace_match_probe() from the 7191 * given probe description. By convention, a null key only matches anchored 7192 * probes: if each field is the empty string, reset dtpk_fmatch to 7193 * dtrace_match_nonzero(). 7194 */ 7195 static void 7196 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 7197 { 7198 pkp->dtpk_prov = pdp->dtpd_provider; 7199 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 7200 7201 pkp->dtpk_mod = pdp->dtpd_mod; 7202 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 7203 7204 pkp->dtpk_func = pdp->dtpd_func; 7205 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 7206 7207 pkp->dtpk_name = pdp->dtpd_name; 7208 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 7209 7210 pkp->dtpk_id = pdp->dtpd_id; 7211 7212 if (pkp->dtpk_id == DTRACE_IDNONE && 7213 pkp->dtpk_pmatch == &dtrace_match_nul && 7214 pkp->dtpk_mmatch == &dtrace_match_nul && 7215 pkp->dtpk_fmatch == &dtrace_match_nul && 7216 pkp->dtpk_nmatch == &dtrace_match_nul) 7217 pkp->dtpk_fmatch = &dtrace_match_nonzero; 7218 } 7219 7220 /* 7221 * DTrace Provider-to-Framework API Functions 7222 * 7223 * These functions implement much of the Provider-to-Framework API, as 7224 * described in <sys/dtrace.h>. The parts of the API not in this section are 7225 * the functions in the API for probe management (found below), and 7226 * dtrace_probe() itself (found above). 7227 */ 7228 7229 /* 7230 * Register the calling provider with the DTrace framework. This should 7231 * generally be called by DTrace providers in their attach(9E) entry point. 7232 */ 7233 int 7234 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 7235 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 7236 { 7237 dtrace_provider_t *provider; 7238 7239 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 7240 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7241 "arguments", name ? name : "<NULL>"); 7242 return (EINVAL); 7243 } 7244 7245 if (name[0] == '\0' || dtrace_badname(name)) { 7246 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7247 "provider name", name); 7248 return (EINVAL); 7249 } 7250 7251 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 7252 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 7253 pops->dtps_destroy == NULL || 7254 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 7255 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7256 "provider ops", name); 7257 return (EINVAL); 7258 } 7259 7260 if (dtrace_badattr(&pap->dtpa_provider) || 7261 dtrace_badattr(&pap->dtpa_mod) || 7262 dtrace_badattr(&pap->dtpa_func) || 7263 dtrace_badattr(&pap->dtpa_name) || 7264 dtrace_badattr(&pap->dtpa_args)) { 7265 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7266 "provider attributes", name); 7267 return (EINVAL); 7268 } 7269 7270 if (priv & ~DTRACE_PRIV_ALL) { 7271 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7272 "privilege attributes", name); 7273 return (EINVAL); 7274 } 7275 7276 if ((priv & DTRACE_PRIV_KERNEL) && 7277 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 7278 pops->dtps_mode == NULL) { 7279 cmn_err(CE_WARN, "failed to register provider '%s': need " 7280 "dtps_mode() op for given privilege attributes", name); 7281 return (EINVAL); 7282 } 7283 7284 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 7285 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7286 (void) strcpy(provider->dtpv_name, name); 7287 7288 provider->dtpv_attr = *pap; 7289 provider->dtpv_priv.dtpp_flags = priv; 7290 if (cr != NULL) { 7291 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 7292 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 7293 } 7294 provider->dtpv_pops = *pops; 7295 7296 if (pops->dtps_provide == NULL) { 7297 ASSERT(pops->dtps_provide_module != NULL); 7298 provider->dtpv_pops.dtps_provide = 7299 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 7300 } 7301 7302 if (pops->dtps_provide_module == NULL) { 7303 ASSERT(pops->dtps_provide != NULL); 7304 provider->dtpv_pops.dtps_provide_module = 7305 (void (*)(void *, struct modctl *))dtrace_nullop; 7306 } 7307 7308 if (pops->dtps_suspend == NULL) { 7309 ASSERT(pops->dtps_resume == NULL); 7310 provider->dtpv_pops.dtps_suspend = 7311 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7312 provider->dtpv_pops.dtps_resume = 7313 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7314 } 7315 7316 provider->dtpv_arg = arg; 7317 *idp = (dtrace_provider_id_t)provider; 7318 7319 if (pops == &dtrace_provider_ops) { 7320 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7321 ASSERT(MUTEX_HELD(&dtrace_lock)); 7322 ASSERT(dtrace_anon.dta_enabling == NULL); 7323 7324 /* 7325 * We make sure that the DTrace provider is at the head of 7326 * the provider chain. 7327 */ 7328 provider->dtpv_next = dtrace_provider; 7329 dtrace_provider = provider; 7330 return (0); 7331 } 7332 7333 mutex_enter(&dtrace_provider_lock); 7334 mutex_enter(&dtrace_lock); 7335 7336 /* 7337 * If there is at least one provider registered, we'll add this 7338 * provider after the first provider. 7339 */ 7340 if (dtrace_provider != NULL) { 7341 provider->dtpv_next = dtrace_provider->dtpv_next; 7342 dtrace_provider->dtpv_next = provider; 7343 } else { 7344 dtrace_provider = provider; 7345 } 7346 7347 if (dtrace_retained != NULL) { 7348 dtrace_enabling_provide(provider); 7349 7350 /* 7351 * Now we need to call dtrace_enabling_matchall() -- which 7352 * will acquire cpu_lock and dtrace_lock. We therefore need 7353 * to drop all of our locks before calling into it... 7354 */ 7355 mutex_exit(&dtrace_lock); 7356 mutex_exit(&dtrace_provider_lock); 7357 dtrace_enabling_matchall(); 7358 7359 return (0); 7360 } 7361 7362 mutex_exit(&dtrace_lock); 7363 mutex_exit(&dtrace_provider_lock); 7364 7365 return (0); 7366 } 7367 7368 /* 7369 * Unregister the specified provider from the DTrace framework. This should 7370 * generally be called by DTrace providers in their detach(9E) entry point. 7371 */ 7372 int 7373 dtrace_unregister(dtrace_provider_id_t id) 7374 { 7375 dtrace_provider_t *old = (dtrace_provider_t *)id; 7376 dtrace_provider_t *prev = NULL; 7377 int i, self = 0, noreap = 0; 7378 dtrace_probe_t *probe, *first = NULL; 7379 7380 if (old->dtpv_pops.dtps_enable == 7381 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop) { 7382 /* 7383 * If DTrace itself is the provider, we're called with locks 7384 * already held. 7385 */ 7386 ASSERT(old == dtrace_provider); 7387 ASSERT(dtrace_devi != NULL); 7388 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7389 ASSERT(MUTEX_HELD(&dtrace_lock)); 7390 self = 1; 7391 7392 if (dtrace_provider->dtpv_next != NULL) { 7393 /* 7394 * There's another provider here; return failure. 7395 */ 7396 return (EBUSY); 7397 } 7398 } else { 7399 mutex_enter(&dtrace_provider_lock); 7400 mutex_enter(&mod_lock); 7401 mutex_enter(&dtrace_lock); 7402 } 7403 7404 /* 7405 * If anyone has /dev/dtrace open, or if there are anonymous enabled 7406 * probes, we refuse to let providers slither away, unless this 7407 * provider has already been explicitly invalidated. 7408 */ 7409 if (!old->dtpv_defunct && 7410 (dtrace_opens || (dtrace_anon.dta_state != NULL && 7411 dtrace_anon.dta_state->dts_necbs > 0))) { 7412 if (!self) { 7413 mutex_exit(&dtrace_lock); 7414 mutex_exit(&mod_lock); 7415 mutex_exit(&dtrace_provider_lock); 7416 } 7417 return (EBUSY); 7418 } 7419 7420 /* 7421 * Attempt to destroy the probes associated with this provider. 7422 */ 7423 for (i = 0; i < dtrace_nprobes; i++) { 7424 if ((probe = dtrace_probes[i]) == NULL) 7425 continue; 7426 7427 if (probe->dtpr_provider != old) 7428 continue; 7429 7430 if (probe->dtpr_ecb == NULL) 7431 continue; 7432 7433 /* 7434 * If we are trying to unregister a defunct provider, and the 7435 * provider was made defunct within the interval dictated by 7436 * dtrace_unregister_defunct_reap, we'll (asynchronously) 7437 * attempt to reap our enablings. To denote that the provider 7438 * should reattempt to unregister itself at some point in the 7439 * future, we will return a differentiable error code (EAGAIN 7440 * instead of EBUSY) in this case. 7441 */ 7442 if (dtrace_gethrtime() - old->dtpv_defunct > 7443 dtrace_unregister_defunct_reap) 7444 noreap = 1; 7445 7446 if (!self) { 7447 mutex_exit(&dtrace_lock); 7448 mutex_exit(&mod_lock); 7449 mutex_exit(&dtrace_provider_lock); 7450 } 7451 7452 if (noreap) 7453 return (EBUSY); 7454 7455 (void) taskq_dispatch(dtrace_taskq, 7456 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 7457 7458 return (EAGAIN); 7459 } 7460 7461 /* 7462 * All of the probes for this provider are disabled; we can safely 7463 * remove all of them from their hash chains and from the probe array. 7464 */ 7465 for (i = 0; i < dtrace_nprobes; i++) { 7466 if ((probe = dtrace_probes[i]) == NULL) 7467 continue; 7468 7469 if (probe->dtpr_provider != old) 7470 continue; 7471 7472 dtrace_probes[i] = NULL; 7473 7474 dtrace_hash_remove(dtrace_bymod, probe); 7475 dtrace_hash_remove(dtrace_byfunc, probe); 7476 dtrace_hash_remove(dtrace_byname, probe); 7477 7478 if (first == NULL) { 7479 first = probe; 7480 probe->dtpr_nextmod = NULL; 7481 } else { 7482 probe->dtpr_nextmod = first; 7483 first = probe; 7484 } 7485 } 7486 7487 /* 7488 * The provider's probes have been removed from the hash chains and 7489 * from the probe array. Now issue a dtrace_sync() to be sure that 7490 * everyone has cleared out from any probe array processing. 7491 */ 7492 dtrace_sync(); 7493 7494 for (probe = first; probe != NULL; probe = first) { 7495 first = probe->dtpr_nextmod; 7496 7497 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 7498 probe->dtpr_arg); 7499 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7500 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7501 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7502 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 7503 kmem_free(probe, sizeof (dtrace_probe_t)); 7504 } 7505 7506 if ((prev = dtrace_provider) == old) { 7507 ASSERT(self || dtrace_devi == NULL); 7508 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 7509 dtrace_provider = old->dtpv_next; 7510 } else { 7511 while (prev != NULL && prev->dtpv_next != old) 7512 prev = prev->dtpv_next; 7513 7514 if (prev == NULL) { 7515 panic("attempt to unregister non-existent " 7516 "dtrace provider %p\n", (void *)id); 7517 } 7518 7519 prev->dtpv_next = old->dtpv_next; 7520 } 7521 7522 if (!self) { 7523 mutex_exit(&dtrace_lock); 7524 mutex_exit(&mod_lock); 7525 mutex_exit(&dtrace_provider_lock); 7526 } 7527 7528 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 7529 kmem_free(old, sizeof (dtrace_provider_t)); 7530 7531 return (0); 7532 } 7533 7534 /* 7535 * Invalidate the specified provider. All subsequent probe lookups for the 7536 * specified provider will fail, but its probes will not be removed. 7537 */ 7538 void 7539 dtrace_invalidate(dtrace_provider_id_t id) 7540 { 7541 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 7542 7543 ASSERT(pvp->dtpv_pops.dtps_enable != 7544 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7545 7546 mutex_enter(&dtrace_provider_lock); 7547 mutex_enter(&dtrace_lock); 7548 7549 pvp->dtpv_defunct = dtrace_gethrtime(); 7550 7551 mutex_exit(&dtrace_lock); 7552 mutex_exit(&dtrace_provider_lock); 7553 } 7554 7555 /* 7556 * Indicate whether or not DTrace has attached. 7557 */ 7558 int 7559 dtrace_attached(void) 7560 { 7561 /* 7562 * dtrace_provider will be non-NULL iff the DTrace driver has 7563 * attached. (It's non-NULL because DTrace is always itself a 7564 * provider.) 7565 */ 7566 return (dtrace_provider != NULL); 7567 } 7568 7569 /* 7570 * Remove all the unenabled probes for the given provider. This function is 7571 * not unlike dtrace_unregister(), except that it doesn't remove the provider 7572 * -- just as many of its associated probes as it can. 7573 */ 7574 int 7575 dtrace_condense(dtrace_provider_id_t id) 7576 { 7577 dtrace_provider_t *prov = (dtrace_provider_t *)id; 7578 int i; 7579 dtrace_probe_t *probe; 7580 7581 /* 7582 * Make sure this isn't the dtrace provider itself. 7583 */ 7584 ASSERT(prov->dtpv_pops.dtps_enable != 7585 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7586 7587 mutex_enter(&dtrace_provider_lock); 7588 mutex_enter(&dtrace_lock); 7589 7590 /* 7591 * Attempt to destroy the probes associated with this provider. 7592 */ 7593 for (i = 0; i < dtrace_nprobes; i++) { 7594 if ((probe = dtrace_probes[i]) == NULL) 7595 continue; 7596 7597 if (probe->dtpr_provider != prov) 7598 continue; 7599 7600 if (probe->dtpr_ecb != NULL) 7601 continue; 7602 7603 dtrace_probes[i] = NULL; 7604 7605 dtrace_hash_remove(dtrace_bymod, probe); 7606 dtrace_hash_remove(dtrace_byfunc, probe); 7607 dtrace_hash_remove(dtrace_byname, probe); 7608 7609 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 7610 probe->dtpr_arg); 7611 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7612 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7613 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7614 kmem_free(probe, sizeof (dtrace_probe_t)); 7615 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 7616 } 7617 7618 mutex_exit(&dtrace_lock); 7619 mutex_exit(&dtrace_provider_lock); 7620 7621 return (0); 7622 } 7623 7624 /* 7625 * DTrace Probe Management Functions 7626 * 7627 * The functions in this section perform the DTrace probe management, 7628 * including functions to create probes, look-up probes, and call into the 7629 * providers to request that probes be provided. Some of these functions are 7630 * in the Provider-to-Framework API; these functions can be identified by the 7631 * fact that they are not declared "static". 7632 */ 7633 7634 /* 7635 * Create a probe with the specified module name, function name, and name. 7636 */ 7637 dtrace_id_t 7638 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 7639 const char *func, const char *name, int aframes, void *arg) 7640 { 7641 dtrace_probe_t *probe, **probes; 7642 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 7643 dtrace_id_t id; 7644 7645 if (provider == dtrace_provider) { 7646 ASSERT(MUTEX_HELD(&dtrace_lock)); 7647 } else { 7648 mutex_enter(&dtrace_lock); 7649 } 7650 7651 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 7652 VM_BESTFIT | VM_SLEEP); 7653 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 7654 7655 probe->dtpr_id = id; 7656 probe->dtpr_gen = dtrace_probegen++; 7657 probe->dtpr_mod = dtrace_strdup(mod); 7658 probe->dtpr_func = dtrace_strdup(func); 7659 probe->dtpr_name = dtrace_strdup(name); 7660 probe->dtpr_arg = arg; 7661 probe->dtpr_aframes = aframes; 7662 probe->dtpr_provider = provider; 7663 7664 dtrace_hash_add(dtrace_bymod, probe); 7665 dtrace_hash_add(dtrace_byfunc, probe); 7666 dtrace_hash_add(dtrace_byname, probe); 7667 7668 if (id - 1 >= dtrace_nprobes) { 7669 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 7670 size_t nsize = osize << 1; 7671 7672 if (nsize == 0) { 7673 ASSERT(osize == 0); 7674 ASSERT(dtrace_probes == NULL); 7675 nsize = sizeof (dtrace_probe_t *); 7676 } 7677 7678 probes = kmem_zalloc(nsize, KM_SLEEP); 7679 7680 if (dtrace_probes == NULL) { 7681 ASSERT(osize == 0); 7682 dtrace_probes = probes; 7683 dtrace_nprobes = 1; 7684 } else { 7685 dtrace_probe_t **oprobes = dtrace_probes; 7686 7687 bcopy(oprobes, probes, osize); 7688 dtrace_membar_producer(); 7689 dtrace_probes = probes; 7690 7691 dtrace_sync(); 7692 7693 /* 7694 * All CPUs are now seeing the new probes array; we can 7695 * safely free the old array. 7696 */ 7697 kmem_free(oprobes, osize); 7698 dtrace_nprobes <<= 1; 7699 } 7700 7701 ASSERT(id - 1 < dtrace_nprobes); 7702 } 7703 7704 ASSERT(dtrace_probes[id - 1] == NULL); 7705 dtrace_probes[id - 1] = probe; 7706 7707 if (provider != dtrace_provider) 7708 mutex_exit(&dtrace_lock); 7709 7710 return (id); 7711 } 7712 7713 static dtrace_probe_t * 7714 dtrace_probe_lookup_id(dtrace_id_t id) 7715 { 7716 ASSERT(MUTEX_HELD(&dtrace_lock)); 7717 7718 if (id == 0 || id > dtrace_nprobes) 7719 return (NULL); 7720 7721 return (dtrace_probes[id - 1]); 7722 } 7723 7724 static int 7725 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 7726 { 7727 *((dtrace_id_t *)arg) = probe->dtpr_id; 7728 7729 return (DTRACE_MATCH_DONE); 7730 } 7731 7732 /* 7733 * Look up a probe based on provider and one or more of module name, function 7734 * name and probe name. 7735 */ 7736 dtrace_id_t 7737 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, 7738 const char *func, const char *name) 7739 { 7740 dtrace_probekey_t pkey; 7741 dtrace_id_t id; 7742 int match; 7743 7744 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 7745 pkey.dtpk_pmatch = &dtrace_match_string; 7746 pkey.dtpk_mod = mod; 7747 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 7748 pkey.dtpk_func = func; 7749 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 7750 pkey.dtpk_name = name; 7751 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 7752 pkey.dtpk_id = DTRACE_IDNONE; 7753 7754 mutex_enter(&dtrace_lock); 7755 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 7756 dtrace_probe_lookup_match, &id); 7757 mutex_exit(&dtrace_lock); 7758 7759 ASSERT(match == 1 || match == 0); 7760 return (match ? id : 0); 7761 } 7762 7763 /* 7764 * Returns the probe argument associated with the specified probe. 7765 */ 7766 void * 7767 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 7768 { 7769 dtrace_probe_t *probe; 7770 void *rval = NULL; 7771 7772 mutex_enter(&dtrace_lock); 7773 7774 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 7775 probe->dtpr_provider == (dtrace_provider_t *)id) 7776 rval = probe->dtpr_arg; 7777 7778 mutex_exit(&dtrace_lock); 7779 7780 return (rval); 7781 } 7782 7783 /* 7784 * Copy a probe into a probe description. 7785 */ 7786 static void 7787 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 7788 { 7789 bzero(pdp, sizeof (dtrace_probedesc_t)); 7790 pdp->dtpd_id = prp->dtpr_id; 7791 7792 (void) strncpy(pdp->dtpd_provider, 7793 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 7794 7795 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 7796 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 7797 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 7798 } 7799 7800 /* 7801 * Called to indicate that a probe -- or probes -- should be provided by a 7802 * specfied provider. If the specified description is NULL, the provider will 7803 * be told to provide all of its probes. (This is done whenever a new 7804 * consumer comes along, or whenever a retained enabling is to be matched.) If 7805 * the specified description is non-NULL, the provider is given the 7806 * opportunity to dynamically provide the specified probe, allowing providers 7807 * to support the creation of probes on-the-fly. (So-called _autocreated_ 7808 * probes.) If the provider is NULL, the operations will be applied to all 7809 * providers; if the provider is non-NULL the operations will only be applied 7810 * to the specified provider. The dtrace_provider_lock must be held, and the 7811 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 7812 * will need to grab the dtrace_lock when it reenters the framework through 7813 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 7814 */ 7815 static void 7816 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 7817 { 7818 struct modctl *ctl; 7819 int all = 0; 7820 7821 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7822 7823 if (prv == NULL) { 7824 all = 1; 7825 prv = dtrace_provider; 7826 } 7827 7828 do { 7829 /* 7830 * First, call the blanket provide operation. 7831 */ 7832 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 7833 7834 /* 7835 * Now call the per-module provide operation. We will grab 7836 * mod_lock to prevent the list from being modified. Note 7837 * that this also prevents the mod_busy bits from changing. 7838 * (mod_busy can only be changed with mod_lock held.) 7839 */ 7840 mutex_enter(&mod_lock); 7841 7842 ctl = &modules; 7843 do { 7844 if (ctl->mod_busy || ctl->mod_mp == NULL) 7845 continue; 7846 7847 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 7848 7849 } while ((ctl = ctl->mod_next) != &modules); 7850 7851 mutex_exit(&mod_lock); 7852 } while (all && (prv = prv->dtpv_next) != NULL); 7853 } 7854 7855 /* 7856 * Iterate over each probe, and call the Framework-to-Provider API function 7857 * denoted by offs. 7858 */ 7859 static void 7860 dtrace_probe_foreach(uintptr_t offs) 7861 { 7862 dtrace_provider_t *prov; 7863 void (*func)(void *, dtrace_id_t, void *); 7864 dtrace_probe_t *probe; 7865 dtrace_icookie_t cookie; 7866 int i; 7867 7868 /* 7869 * We disable interrupts to walk through the probe array. This is 7870 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 7871 * won't see stale data. 7872 */ 7873 cookie = dtrace_interrupt_disable(); 7874 7875 for (i = 0; i < dtrace_nprobes; i++) { 7876 if ((probe = dtrace_probes[i]) == NULL) 7877 continue; 7878 7879 if (probe->dtpr_ecb == NULL) { 7880 /* 7881 * This probe isn't enabled -- don't call the function. 7882 */ 7883 continue; 7884 } 7885 7886 prov = probe->dtpr_provider; 7887 func = *((void(**)(void *, dtrace_id_t, void *)) 7888 ((uintptr_t)&prov->dtpv_pops + offs)); 7889 7890 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 7891 } 7892 7893 dtrace_interrupt_enable(cookie); 7894 } 7895 7896 static int 7897 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 7898 { 7899 dtrace_probekey_t pkey; 7900 uint32_t priv; 7901 uid_t uid; 7902 zoneid_t zoneid; 7903 7904 ASSERT(MUTEX_HELD(&dtrace_lock)); 7905 dtrace_ecb_create_cache = NULL; 7906 7907 if (desc == NULL) { 7908 /* 7909 * If we're passed a NULL description, we're being asked to 7910 * create an ECB with a NULL probe. 7911 */ 7912 (void) dtrace_ecb_create_enable(NULL, enab); 7913 return (0); 7914 } 7915 7916 dtrace_probekey(desc, &pkey); 7917 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 7918 &priv, &uid, &zoneid); 7919 7920 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 7921 enab)); 7922 } 7923 7924 /* 7925 * DTrace Helper Provider Functions 7926 */ 7927 static void 7928 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 7929 { 7930 attr->dtat_name = DOF_ATTR_NAME(dofattr); 7931 attr->dtat_data = DOF_ATTR_DATA(dofattr); 7932 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 7933 } 7934 7935 static void 7936 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 7937 const dof_provider_t *dofprov, char *strtab) 7938 { 7939 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 7940 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 7941 dofprov->dofpv_provattr); 7942 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 7943 dofprov->dofpv_modattr); 7944 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 7945 dofprov->dofpv_funcattr); 7946 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 7947 dofprov->dofpv_nameattr); 7948 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 7949 dofprov->dofpv_argsattr); 7950 } 7951 7952 static void 7953 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 7954 { 7955 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7956 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7957 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 7958 dof_provider_t *provider; 7959 dof_probe_t *probe; 7960 uint32_t *off, *enoff; 7961 uint8_t *arg; 7962 char *strtab; 7963 uint_t i, nprobes; 7964 dtrace_helper_provdesc_t dhpv; 7965 dtrace_helper_probedesc_t dhpb; 7966 dtrace_meta_t *meta = dtrace_meta_pid; 7967 dtrace_mops_t *mops = &meta->dtm_mops; 7968 void *parg; 7969 7970 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 7971 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7972 provider->dofpv_strtab * dof->dofh_secsize); 7973 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7974 provider->dofpv_probes * dof->dofh_secsize); 7975 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7976 provider->dofpv_prargs * dof->dofh_secsize); 7977 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7978 provider->dofpv_proffs * dof->dofh_secsize); 7979 7980 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 7981 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 7982 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 7983 enoff = NULL; 7984 7985 /* 7986 * See dtrace_helper_provider_validate(). 7987 */ 7988 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 7989 provider->dofpv_prenoffs != DOF_SECT_NONE) { 7990 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7991 provider->dofpv_prenoffs * dof->dofh_secsize); 7992 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 7993 } 7994 7995 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 7996 7997 /* 7998 * Create the provider. 7999 */ 8000 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8001 8002 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 8003 return; 8004 8005 meta->dtm_count++; 8006 8007 /* 8008 * Create the probes. 8009 */ 8010 for (i = 0; i < nprobes; i++) { 8011 probe = (dof_probe_t *)(uintptr_t)(daddr + 8012 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 8013 8014 dhpb.dthpb_mod = dhp->dofhp_mod; 8015 dhpb.dthpb_func = strtab + probe->dofpr_func; 8016 dhpb.dthpb_name = strtab + probe->dofpr_name; 8017 dhpb.dthpb_base = probe->dofpr_addr; 8018 dhpb.dthpb_offs = off + probe->dofpr_offidx; 8019 dhpb.dthpb_noffs = probe->dofpr_noffs; 8020 if (enoff != NULL) { 8021 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 8022 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 8023 } else { 8024 dhpb.dthpb_enoffs = NULL; 8025 dhpb.dthpb_nenoffs = 0; 8026 } 8027 dhpb.dthpb_args = arg + probe->dofpr_argidx; 8028 dhpb.dthpb_nargc = probe->dofpr_nargc; 8029 dhpb.dthpb_xargc = probe->dofpr_xargc; 8030 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 8031 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 8032 8033 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 8034 } 8035 } 8036 8037 static void 8038 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 8039 { 8040 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8041 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8042 int i; 8043 8044 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8045 8046 for (i = 0; i < dof->dofh_secnum; i++) { 8047 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8048 dof->dofh_secoff + i * dof->dofh_secsize); 8049 8050 if (sec->dofs_type != DOF_SECT_PROVIDER) 8051 continue; 8052 8053 dtrace_helper_provide_one(dhp, sec, pid); 8054 } 8055 8056 /* 8057 * We may have just created probes, so we must now rematch against 8058 * any retained enablings. Note that this call will acquire both 8059 * cpu_lock and dtrace_lock; the fact that we are holding 8060 * dtrace_meta_lock now is what defines the ordering with respect to 8061 * these three locks. 8062 */ 8063 dtrace_enabling_matchall(); 8064 } 8065 8066 static void 8067 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8068 { 8069 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8070 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8071 dof_sec_t *str_sec; 8072 dof_provider_t *provider; 8073 char *strtab; 8074 dtrace_helper_provdesc_t dhpv; 8075 dtrace_meta_t *meta = dtrace_meta_pid; 8076 dtrace_mops_t *mops = &meta->dtm_mops; 8077 8078 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8079 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8080 provider->dofpv_strtab * dof->dofh_secsize); 8081 8082 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8083 8084 /* 8085 * Create the provider. 8086 */ 8087 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8088 8089 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 8090 8091 meta->dtm_count--; 8092 } 8093 8094 static void 8095 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 8096 { 8097 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8098 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8099 int i; 8100 8101 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8102 8103 for (i = 0; i < dof->dofh_secnum; i++) { 8104 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8105 dof->dofh_secoff + i * dof->dofh_secsize); 8106 8107 if (sec->dofs_type != DOF_SECT_PROVIDER) 8108 continue; 8109 8110 dtrace_helper_provider_remove_one(dhp, sec, pid); 8111 } 8112 } 8113 8114 /* 8115 * DTrace Meta Provider-to-Framework API Functions 8116 * 8117 * These functions implement the Meta Provider-to-Framework API, as described 8118 * in <sys/dtrace.h>. 8119 */ 8120 int 8121 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 8122 dtrace_meta_provider_id_t *idp) 8123 { 8124 dtrace_meta_t *meta; 8125 dtrace_helpers_t *help, *next; 8126 int i; 8127 8128 *idp = DTRACE_METAPROVNONE; 8129 8130 /* 8131 * We strictly don't need the name, but we hold onto it for 8132 * debuggability. All hail error queues! 8133 */ 8134 if (name == NULL) { 8135 cmn_err(CE_WARN, "failed to register meta-provider: " 8136 "invalid name"); 8137 return (EINVAL); 8138 } 8139 8140 if (mops == NULL || 8141 mops->dtms_create_probe == NULL || 8142 mops->dtms_provide_pid == NULL || 8143 mops->dtms_remove_pid == NULL) { 8144 cmn_err(CE_WARN, "failed to register meta-register %s: " 8145 "invalid ops", name); 8146 return (EINVAL); 8147 } 8148 8149 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 8150 meta->dtm_mops = *mops; 8151 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8152 (void) strcpy(meta->dtm_name, name); 8153 meta->dtm_arg = arg; 8154 8155 mutex_enter(&dtrace_meta_lock); 8156 mutex_enter(&dtrace_lock); 8157 8158 if (dtrace_meta_pid != NULL) { 8159 mutex_exit(&dtrace_lock); 8160 mutex_exit(&dtrace_meta_lock); 8161 cmn_err(CE_WARN, "failed to register meta-register %s: " 8162 "user-land meta-provider exists", name); 8163 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 8164 kmem_free(meta, sizeof (dtrace_meta_t)); 8165 return (EINVAL); 8166 } 8167 8168 dtrace_meta_pid = meta; 8169 *idp = (dtrace_meta_provider_id_t)meta; 8170 8171 /* 8172 * If there are providers and probes ready to go, pass them 8173 * off to the new meta provider now. 8174 */ 8175 8176 help = dtrace_deferred_pid; 8177 dtrace_deferred_pid = NULL; 8178 8179 mutex_exit(&dtrace_lock); 8180 8181 while (help != NULL) { 8182 for (i = 0; i < help->dthps_nprovs; i++) { 8183 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 8184 help->dthps_pid); 8185 } 8186 8187 next = help->dthps_next; 8188 help->dthps_next = NULL; 8189 help->dthps_prev = NULL; 8190 help->dthps_deferred = 0; 8191 help = next; 8192 } 8193 8194 mutex_exit(&dtrace_meta_lock); 8195 8196 return (0); 8197 } 8198 8199 int 8200 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 8201 { 8202 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 8203 8204 mutex_enter(&dtrace_meta_lock); 8205 mutex_enter(&dtrace_lock); 8206 8207 if (old == dtrace_meta_pid) { 8208 pp = &dtrace_meta_pid; 8209 } else { 8210 panic("attempt to unregister non-existent " 8211 "dtrace meta-provider %p\n", (void *)old); 8212 } 8213 8214 if (old->dtm_count != 0) { 8215 mutex_exit(&dtrace_lock); 8216 mutex_exit(&dtrace_meta_lock); 8217 return (EBUSY); 8218 } 8219 8220 *pp = NULL; 8221 8222 mutex_exit(&dtrace_lock); 8223 mutex_exit(&dtrace_meta_lock); 8224 8225 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 8226 kmem_free(old, sizeof (dtrace_meta_t)); 8227 8228 return (0); 8229 } 8230 8231 8232 /* 8233 * DTrace DIF Object Functions 8234 */ 8235 static int 8236 dtrace_difo_err(uint_t pc, const char *format, ...) 8237 { 8238 if (dtrace_err_verbose) { 8239 va_list alist; 8240 8241 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 8242 va_start(alist, format); 8243 (void) vuprintf(format, alist); 8244 va_end(alist); 8245 } 8246 8247 #ifdef DTRACE_ERRDEBUG 8248 dtrace_errdebug(format); 8249 #endif 8250 return (1); 8251 } 8252 8253 /* 8254 * Validate a DTrace DIF object by checking the IR instructions. The following 8255 * rules are currently enforced by dtrace_difo_validate(): 8256 * 8257 * 1. Each instruction must have a valid opcode 8258 * 2. Each register, string, variable, or subroutine reference must be valid 8259 * 3. No instruction can modify register %r0 (must be zero) 8260 * 4. All instruction reserved bits must be set to zero 8261 * 5. The last instruction must be a "ret" instruction 8262 * 6. All branch targets must reference a valid instruction _after_ the branch 8263 */ 8264 static int 8265 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 8266 cred_t *cr) 8267 { 8268 int err = 0, i; 8269 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8270 int kcheckload; 8271 uint_t pc; 8272 8273 kcheckload = cr == NULL || 8274 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 8275 8276 dp->dtdo_destructive = 0; 8277 8278 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 8279 dif_instr_t instr = dp->dtdo_buf[pc]; 8280 8281 uint_t r1 = DIF_INSTR_R1(instr); 8282 uint_t r2 = DIF_INSTR_R2(instr); 8283 uint_t rd = DIF_INSTR_RD(instr); 8284 uint_t rs = DIF_INSTR_RS(instr); 8285 uint_t label = DIF_INSTR_LABEL(instr); 8286 uint_t v = DIF_INSTR_VAR(instr); 8287 uint_t subr = DIF_INSTR_SUBR(instr); 8288 uint_t type = DIF_INSTR_TYPE(instr); 8289 uint_t op = DIF_INSTR_OP(instr); 8290 8291 switch (op) { 8292 case DIF_OP_OR: 8293 case DIF_OP_XOR: 8294 case DIF_OP_AND: 8295 case DIF_OP_SLL: 8296 case DIF_OP_SRL: 8297 case DIF_OP_SRA: 8298 case DIF_OP_SUB: 8299 case DIF_OP_ADD: 8300 case DIF_OP_MUL: 8301 case DIF_OP_SDIV: 8302 case DIF_OP_UDIV: 8303 case DIF_OP_SREM: 8304 case DIF_OP_UREM: 8305 case DIF_OP_COPYS: 8306 if (r1 >= nregs) 8307 err += efunc(pc, "invalid register %u\n", r1); 8308 if (r2 >= nregs) 8309 err += efunc(pc, "invalid register %u\n", r2); 8310 if (rd >= nregs) 8311 err += efunc(pc, "invalid register %u\n", rd); 8312 if (rd == 0) 8313 err += efunc(pc, "cannot write to %r0\n"); 8314 break; 8315 case DIF_OP_NOT: 8316 case DIF_OP_MOV: 8317 case DIF_OP_ALLOCS: 8318 if (r1 >= nregs) 8319 err += efunc(pc, "invalid register %u\n", r1); 8320 if (r2 != 0) 8321 err += efunc(pc, "non-zero reserved bits\n"); 8322 if (rd >= nregs) 8323 err += efunc(pc, "invalid register %u\n", rd); 8324 if (rd == 0) 8325 err += efunc(pc, "cannot write to %r0\n"); 8326 break; 8327 case DIF_OP_LDSB: 8328 case DIF_OP_LDSH: 8329 case DIF_OP_LDSW: 8330 case DIF_OP_LDUB: 8331 case DIF_OP_LDUH: 8332 case DIF_OP_LDUW: 8333 case DIF_OP_LDX: 8334 if (r1 >= nregs) 8335 err += efunc(pc, "invalid register %u\n", r1); 8336 if (r2 != 0) 8337 err += efunc(pc, "non-zero reserved bits\n"); 8338 if (rd >= nregs) 8339 err += efunc(pc, "invalid register %u\n", rd); 8340 if (rd == 0) 8341 err += efunc(pc, "cannot write to %r0\n"); 8342 if (kcheckload) 8343 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 8344 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 8345 break; 8346 case DIF_OP_RLDSB: 8347 case DIF_OP_RLDSH: 8348 case DIF_OP_RLDSW: 8349 case DIF_OP_RLDUB: 8350 case DIF_OP_RLDUH: 8351 case DIF_OP_RLDUW: 8352 case DIF_OP_RLDX: 8353 if (r1 >= nregs) 8354 err += efunc(pc, "invalid register %u\n", r1); 8355 if (r2 != 0) 8356 err += efunc(pc, "non-zero reserved bits\n"); 8357 if (rd >= nregs) 8358 err += efunc(pc, "invalid register %u\n", rd); 8359 if (rd == 0) 8360 err += efunc(pc, "cannot write to %r0\n"); 8361 break; 8362 case DIF_OP_ULDSB: 8363 case DIF_OP_ULDSH: 8364 case DIF_OP_ULDSW: 8365 case DIF_OP_ULDUB: 8366 case DIF_OP_ULDUH: 8367 case DIF_OP_ULDUW: 8368 case DIF_OP_ULDX: 8369 if (r1 >= nregs) 8370 err += efunc(pc, "invalid register %u\n", r1); 8371 if (r2 != 0) 8372 err += efunc(pc, "non-zero reserved bits\n"); 8373 if (rd >= nregs) 8374 err += efunc(pc, "invalid register %u\n", rd); 8375 if (rd == 0) 8376 err += efunc(pc, "cannot write to %r0\n"); 8377 break; 8378 case DIF_OP_STB: 8379 case DIF_OP_STH: 8380 case DIF_OP_STW: 8381 case DIF_OP_STX: 8382 if (r1 >= nregs) 8383 err += efunc(pc, "invalid register %u\n", r1); 8384 if (r2 != 0) 8385 err += efunc(pc, "non-zero reserved bits\n"); 8386 if (rd >= nregs) 8387 err += efunc(pc, "invalid register %u\n", rd); 8388 if (rd == 0) 8389 err += efunc(pc, "cannot write to 0 address\n"); 8390 break; 8391 case DIF_OP_CMP: 8392 case DIF_OP_SCMP: 8393 if (r1 >= nregs) 8394 err += efunc(pc, "invalid register %u\n", r1); 8395 if (r2 >= nregs) 8396 err += efunc(pc, "invalid register %u\n", r2); 8397 if (rd != 0) 8398 err += efunc(pc, "non-zero reserved bits\n"); 8399 break; 8400 case DIF_OP_TST: 8401 if (r1 >= nregs) 8402 err += efunc(pc, "invalid register %u\n", r1); 8403 if (r2 != 0 || rd != 0) 8404 err += efunc(pc, "non-zero reserved bits\n"); 8405 break; 8406 case DIF_OP_BA: 8407 case DIF_OP_BE: 8408 case DIF_OP_BNE: 8409 case DIF_OP_BG: 8410 case DIF_OP_BGU: 8411 case DIF_OP_BGE: 8412 case DIF_OP_BGEU: 8413 case DIF_OP_BL: 8414 case DIF_OP_BLU: 8415 case DIF_OP_BLE: 8416 case DIF_OP_BLEU: 8417 if (label >= dp->dtdo_len) { 8418 err += efunc(pc, "invalid branch target %u\n", 8419 label); 8420 } 8421 if (label <= pc) { 8422 err += efunc(pc, "backward branch to %u\n", 8423 label); 8424 } 8425 break; 8426 case DIF_OP_RET: 8427 if (r1 != 0 || r2 != 0) 8428 err += efunc(pc, "non-zero reserved bits\n"); 8429 if (rd >= nregs) 8430 err += efunc(pc, "invalid register %u\n", rd); 8431 break; 8432 case DIF_OP_NOP: 8433 case DIF_OP_POPTS: 8434 case DIF_OP_FLUSHTS: 8435 if (r1 != 0 || r2 != 0 || rd != 0) 8436 err += efunc(pc, "non-zero reserved bits\n"); 8437 break; 8438 case DIF_OP_SETX: 8439 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 8440 err += efunc(pc, "invalid integer ref %u\n", 8441 DIF_INSTR_INTEGER(instr)); 8442 } 8443 if (rd >= nregs) 8444 err += efunc(pc, "invalid register %u\n", rd); 8445 if (rd == 0) 8446 err += efunc(pc, "cannot write to %r0\n"); 8447 break; 8448 case DIF_OP_SETS: 8449 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 8450 err += efunc(pc, "invalid string ref %u\n", 8451 DIF_INSTR_STRING(instr)); 8452 } 8453 if (rd >= nregs) 8454 err += efunc(pc, "invalid register %u\n", rd); 8455 if (rd == 0) 8456 err += efunc(pc, "cannot write to %r0\n"); 8457 break; 8458 case DIF_OP_LDGA: 8459 case DIF_OP_LDTA: 8460 if (r1 > DIF_VAR_ARRAY_MAX) 8461 err += efunc(pc, "invalid array %u\n", r1); 8462 if (r2 >= nregs) 8463 err += efunc(pc, "invalid register %u\n", r2); 8464 if (rd >= nregs) 8465 err += efunc(pc, "invalid register %u\n", rd); 8466 if (rd == 0) 8467 err += efunc(pc, "cannot write to %r0\n"); 8468 break; 8469 case DIF_OP_LDGS: 8470 case DIF_OP_LDTS: 8471 case DIF_OP_LDLS: 8472 case DIF_OP_LDGAA: 8473 case DIF_OP_LDTAA: 8474 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 8475 err += efunc(pc, "invalid variable %u\n", v); 8476 if (rd >= nregs) 8477 err += efunc(pc, "invalid register %u\n", rd); 8478 if (rd == 0) 8479 err += efunc(pc, "cannot write to %r0\n"); 8480 break; 8481 case DIF_OP_STGS: 8482 case DIF_OP_STTS: 8483 case DIF_OP_STLS: 8484 case DIF_OP_STGAA: 8485 case DIF_OP_STTAA: 8486 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 8487 err += efunc(pc, "invalid variable %u\n", v); 8488 if (rs >= nregs) 8489 err += efunc(pc, "invalid register %u\n", rd); 8490 break; 8491 case DIF_OP_CALL: 8492 if (subr > DIF_SUBR_MAX) 8493 err += efunc(pc, "invalid subr %u\n", subr); 8494 if (rd >= nregs) 8495 err += efunc(pc, "invalid register %u\n", rd); 8496 if (rd == 0) 8497 err += efunc(pc, "cannot write to %r0\n"); 8498 8499 if (subr == DIF_SUBR_COPYOUT || 8500 subr == DIF_SUBR_COPYOUTSTR) { 8501 dp->dtdo_destructive = 1; 8502 } 8503 8504 if (subr == DIF_SUBR_GETF) { 8505 /* 8506 * If we have a getf() we need to record that 8507 * in our state. Note that our state can be 8508 * NULL if this is a helper -- but in that 8509 * case, the call to getf() is itself illegal, 8510 * and will be caught (slightly later) when 8511 * the helper is validated. 8512 */ 8513 if (vstate->dtvs_state != NULL) 8514 vstate->dtvs_state->dts_getf++; 8515 } 8516 8517 break; 8518 case DIF_OP_PUSHTR: 8519 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 8520 err += efunc(pc, "invalid ref type %u\n", type); 8521 if (r2 >= nregs) 8522 err += efunc(pc, "invalid register %u\n", r2); 8523 if (rs >= nregs) 8524 err += efunc(pc, "invalid register %u\n", rs); 8525 break; 8526 case DIF_OP_PUSHTV: 8527 if (type != DIF_TYPE_CTF) 8528 err += efunc(pc, "invalid val type %u\n", type); 8529 if (r2 >= nregs) 8530 err += efunc(pc, "invalid register %u\n", r2); 8531 if (rs >= nregs) 8532 err += efunc(pc, "invalid register %u\n", rs); 8533 break; 8534 default: 8535 err += efunc(pc, "invalid opcode %u\n", 8536 DIF_INSTR_OP(instr)); 8537 } 8538 } 8539 8540 if (dp->dtdo_len != 0 && 8541 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 8542 err += efunc(dp->dtdo_len - 1, 8543 "expected 'ret' as last DIF instruction\n"); 8544 } 8545 8546 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 8547 /* 8548 * If we're not returning by reference, the size must be either 8549 * 0 or the size of one of the base types. 8550 */ 8551 switch (dp->dtdo_rtype.dtdt_size) { 8552 case 0: 8553 case sizeof (uint8_t): 8554 case sizeof (uint16_t): 8555 case sizeof (uint32_t): 8556 case sizeof (uint64_t): 8557 break; 8558 8559 default: 8560 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 8561 } 8562 } 8563 8564 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 8565 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 8566 dtrace_diftype_t *vt, *et; 8567 uint_t id, ndx; 8568 8569 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 8570 v->dtdv_scope != DIFV_SCOPE_THREAD && 8571 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 8572 err += efunc(i, "unrecognized variable scope %d\n", 8573 v->dtdv_scope); 8574 break; 8575 } 8576 8577 if (v->dtdv_kind != DIFV_KIND_ARRAY && 8578 v->dtdv_kind != DIFV_KIND_SCALAR) { 8579 err += efunc(i, "unrecognized variable type %d\n", 8580 v->dtdv_kind); 8581 break; 8582 } 8583 8584 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 8585 err += efunc(i, "%d exceeds variable id limit\n", id); 8586 break; 8587 } 8588 8589 if (id < DIF_VAR_OTHER_UBASE) 8590 continue; 8591 8592 /* 8593 * For user-defined variables, we need to check that this 8594 * definition is identical to any previous definition that we 8595 * encountered. 8596 */ 8597 ndx = id - DIF_VAR_OTHER_UBASE; 8598 8599 switch (v->dtdv_scope) { 8600 case DIFV_SCOPE_GLOBAL: 8601 if (ndx < vstate->dtvs_nglobals) { 8602 dtrace_statvar_t *svar; 8603 8604 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 8605 existing = &svar->dtsv_var; 8606 } 8607 8608 break; 8609 8610 case DIFV_SCOPE_THREAD: 8611 if (ndx < vstate->dtvs_ntlocals) 8612 existing = &vstate->dtvs_tlocals[ndx]; 8613 break; 8614 8615 case DIFV_SCOPE_LOCAL: 8616 if (ndx < vstate->dtvs_nlocals) { 8617 dtrace_statvar_t *svar; 8618 8619 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 8620 existing = &svar->dtsv_var; 8621 } 8622 8623 break; 8624 } 8625 8626 vt = &v->dtdv_type; 8627 8628 if (vt->dtdt_flags & DIF_TF_BYREF) { 8629 if (vt->dtdt_size == 0) { 8630 err += efunc(i, "zero-sized variable\n"); 8631 break; 8632 } 8633 8634 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 8635 vt->dtdt_size > dtrace_global_maxsize) { 8636 err += efunc(i, "oversized by-ref global\n"); 8637 break; 8638 } 8639 } 8640 8641 if (existing == NULL || existing->dtdv_id == 0) 8642 continue; 8643 8644 ASSERT(existing->dtdv_id == v->dtdv_id); 8645 ASSERT(existing->dtdv_scope == v->dtdv_scope); 8646 8647 if (existing->dtdv_kind != v->dtdv_kind) 8648 err += efunc(i, "%d changed variable kind\n", id); 8649 8650 et = &existing->dtdv_type; 8651 8652 if (vt->dtdt_flags != et->dtdt_flags) { 8653 err += efunc(i, "%d changed variable type flags\n", id); 8654 break; 8655 } 8656 8657 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 8658 err += efunc(i, "%d changed variable type size\n", id); 8659 break; 8660 } 8661 } 8662 8663 return (err); 8664 } 8665 8666 /* 8667 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 8668 * are much more constrained than normal DIFOs. Specifically, they may 8669 * not: 8670 * 8671 * 1. Make calls to subroutines other than copyin(), copyinstr() or 8672 * miscellaneous string routines 8673 * 2. Access DTrace variables other than the args[] array, and the 8674 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 8675 * 3. Have thread-local variables. 8676 * 4. Have dynamic variables. 8677 */ 8678 static int 8679 dtrace_difo_validate_helper(dtrace_difo_t *dp) 8680 { 8681 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8682 int err = 0; 8683 uint_t pc; 8684 8685 for (pc = 0; pc < dp->dtdo_len; pc++) { 8686 dif_instr_t instr = dp->dtdo_buf[pc]; 8687 8688 uint_t v = DIF_INSTR_VAR(instr); 8689 uint_t subr = DIF_INSTR_SUBR(instr); 8690 uint_t op = DIF_INSTR_OP(instr); 8691 8692 switch (op) { 8693 case DIF_OP_OR: 8694 case DIF_OP_XOR: 8695 case DIF_OP_AND: 8696 case DIF_OP_SLL: 8697 case DIF_OP_SRL: 8698 case DIF_OP_SRA: 8699 case DIF_OP_SUB: 8700 case DIF_OP_ADD: 8701 case DIF_OP_MUL: 8702 case DIF_OP_SDIV: 8703 case DIF_OP_UDIV: 8704 case DIF_OP_SREM: 8705 case DIF_OP_UREM: 8706 case DIF_OP_COPYS: 8707 case DIF_OP_NOT: 8708 case DIF_OP_MOV: 8709 case DIF_OP_RLDSB: 8710 case DIF_OP_RLDSH: 8711 case DIF_OP_RLDSW: 8712 case DIF_OP_RLDUB: 8713 case DIF_OP_RLDUH: 8714 case DIF_OP_RLDUW: 8715 case DIF_OP_RLDX: 8716 case DIF_OP_ULDSB: 8717 case DIF_OP_ULDSH: 8718 case DIF_OP_ULDSW: 8719 case DIF_OP_ULDUB: 8720 case DIF_OP_ULDUH: 8721 case DIF_OP_ULDUW: 8722 case DIF_OP_ULDX: 8723 case DIF_OP_STB: 8724 case DIF_OP_STH: 8725 case DIF_OP_STW: 8726 case DIF_OP_STX: 8727 case DIF_OP_ALLOCS: 8728 case DIF_OP_CMP: 8729 case DIF_OP_SCMP: 8730 case DIF_OP_TST: 8731 case DIF_OP_BA: 8732 case DIF_OP_BE: 8733 case DIF_OP_BNE: 8734 case DIF_OP_BG: 8735 case DIF_OP_BGU: 8736 case DIF_OP_BGE: 8737 case DIF_OP_BGEU: 8738 case DIF_OP_BL: 8739 case DIF_OP_BLU: 8740 case DIF_OP_BLE: 8741 case DIF_OP_BLEU: 8742 case DIF_OP_RET: 8743 case DIF_OP_NOP: 8744 case DIF_OP_POPTS: 8745 case DIF_OP_FLUSHTS: 8746 case DIF_OP_SETX: 8747 case DIF_OP_SETS: 8748 case DIF_OP_LDGA: 8749 case DIF_OP_LDLS: 8750 case DIF_OP_STGS: 8751 case DIF_OP_STLS: 8752 case DIF_OP_PUSHTR: 8753 case DIF_OP_PUSHTV: 8754 break; 8755 8756 case DIF_OP_LDGS: 8757 if (v >= DIF_VAR_OTHER_UBASE) 8758 break; 8759 8760 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 8761 break; 8762 8763 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 8764 v == DIF_VAR_PPID || v == DIF_VAR_TID || 8765 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 8766 v == DIF_VAR_UID || v == DIF_VAR_GID) 8767 break; 8768 8769 err += efunc(pc, "illegal variable %u\n", v); 8770 break; 8771 8772 case DIF_OP_LDTA: 8773 case DIF_OP_LDTS: 8774 case DIF_OP_LDGAA: 8775 case DIF_OP_LDTAA: 8776 err += efunc(pc, "illegal dynamic variable load\n"); 8777 break; 8778 8779 case DIF_OP_STTS: 8780 case DIF_OP_STGAA: 8781 case DIF_OP_STTAA: 8782 err += efunc(pc, "illegal dynamic variable store\n"); 8783 break; 8784 8785 case DIF_OP_CALL: 8786 if (subr == DIF_SUBR_ALLOCA || 8787 subr == DIF_SUBR_BCOPY || 8788 subr == DIF_SUBR_COPYIN || 8789 subr == DIF_SUBR_COPYINTO || 8790 subr == DIF_SUBR_COPYINSTR || 8791 subr == DIF_SUBR_INDEX || 8792 subr == DIF_SUBR_INET_NTOA || 8793 subr == DIF_SUBR_INET_NTOA6 || 8794 subr == DIF_SUBR_INET_NTOP || 8795 subr == DIF_SUBR_LLTOSTR || 8796 subr == DIF_SUBR_RINDEX || 8797 subr == DIF_SUBR_STRCHR || 8798 subr == DIF_SUBR_STRJOIN || 8799 subr == DIF_SUBR_STRRCHR || 8800 subr == DIF_SUBR_STRSTR || 8801 subr == DIF_SUBR_HTONS || 8802 subr == DIF_SUBR_HTONL || 8803 subr == DIF_SUBR_HTONLL || 8804 subr == DIF_SUBR_NTOHS || 8805 subr == DIF_SUBR_NTOHL || 8806 subr == DIF_SUBR_NTOHLL) 8807 break; 8808 8809 err += efunc(pc, "invalid subr %u\n", subr); 8810 break; 8811 8812 default: 8813 err += efunc(pc, "invalid opcode %u\n", 8814 DIF_INSTR_OP(instr)); 8815 } 8816 } 8817 8818 return (err); 8819 } 8820 8821 /* 8822 * Returns 1 if the expression in the DIF object can be cached on a per-thread 8823 * basis; 0 if not. 8824 */ 8825 static int 8826 dtrace_difo_cacheable(dtrace_difo_t *dp) 8827 { 8828 int i; 8829 8830 if (dp == NULL) 8831 return (0); 8832 8833 for (i = 0; i < dp->dtdo_varlen; i++) { 8834 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8835 8836 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 8837 continue; 8838 8839 switch (v->dtdv_id) { 8840 case DIF_VAR_CURTHREAD: 8841 case DIF_VAR_PID: 8842 case DIF_VAR_TID: 8843 case DIF_VAR_EXECNAME: 8844 case DIF_VAR_ZONENAME: 8845 break; 8846 8847 default: 8848 return (0); 8849 } 8850 } 8851 8852 /* 8853 * This DIF object may be cacheable. Now we need to look for any 8854 * array loading instructions, any memory loading instructions, or 8855 * any stores to thread-local variables. 8856 */ 8857 for (i = 0; i < dp->dtdo_len; i++) { 8858 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 8859 8860 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 8861 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 8862 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 8863 op == DIF_OP_LDGA || op == DIF_OP_STTS) 8864 return (0); 8865 } 8866 8867 return (1); 8868 } 8869 8870 static void 8871 dtrace_difo_hold(dtrace_difo_t *dp) 8872 { 8873 int i; 8874 8875 ASSERT(MUTEX_HELD(&dtrace_lock)); 8876 8877 dp->dtdo_refcnt++; 8878 ASSERT(dp->dtdo_refcnt != 0); 8879 8880 /* 8881 * We need to check this DIF object for references to the variable 8882 * DIF_VAR_VTIMESTAMP. 8883 */ 8884 for (i = 0; i < dp->dtdo_varlen; i++) { 8885 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8886 8887 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 8888 continue; 8889 8890 if (dtrace_vtime_references++ == 0) 8891 dtrace_vtime_enable(); 8892 } 8893 } 8894 8895 /* 8896 * This routine calculates the dynamic variable chunksize for a given DIF 8897 * object. The calculation is not fool-proof, and can probably be tricked by 8898 * malicious DIF -- but it works for all compiler-generated DIF. Because this 8899 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 8900 * if a dynamic variable size exceeds the chunksize. 8901 */ 8902 static void 8903 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8904 { 8905 uint64_t sval; 8906 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 8907 const dif_instr_t *text = dp->dtdo_buf; 8908 uint_t pc, srd = 0; 8909 uint_t ttop = 0; 8910 size_t size, ksize; 8911 uint_t id, i; 8912 8913 for (pc = 0; pc < dp->dtdo_len; pc++) { 8914 dif_instr_t instr = text[pc]; 8915 uint_t op = DIF_INSTR_OP(instr); 8916 uint_t rd = DIF_INSTR_RD(instr); 8917 uint_t r1 = DIF_INSTR_R1(instr); 8918 uint_t nkeys = 0; 8919 uchar_t scope; 8920 8921 dtrace_key_t *key = tupregs; 8922 8923 switch (op) { 8924 case DIF_OP_SETX: 8925 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 8926 srd = rd; 8927 continue; 8928 8929 case DIF_OP_STTS: 8930 key = &tupregs[DIF_DTR_NREGS]; 8931 key[0].dttk_size = 0; 8932 key[1].dttk_size = 0; 8933 nkeys = 2; 8934 scope = DIFV_SCOPE_THREAD; 8935 break; 8936 8937 case DIF_OP_STGAA: 8938 case DIF_OP_STTAA: 8939 nkeys = ttop; 8940 8941 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 8942 key[nkeys++].dttk_size = 0; 8943 8944 key[nkeys++].dttk_size = 0; 8945 8946 if (op == DIF_OP_STTAA) { 8947 scope = DIFV_SCOPE_THREAD; 8948 } else { 8949 scope = DIFV_SCOPE_GLOBAL; 8950 } 8951 8952 break; 8953 8954 case DIF_OP_PUSHTR: 8955 if (ttop == DIF_DTR_NREGS) 8956 return; 8957 8958 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 8959 /* 8960 * If the register for the size of the "pushtr" 8961 * is %r0 (or the value is 0) and the type is 8962 * a string, we'll use the system-wide default 8963 * string size. 8964 */ 8965 tupregs[ttop++].dttk_size = 8966 dtrace_strsize_default; 8967 } else { 8968 if (srd == 0) 8969 return; 8970 8971 tupregs[ttop++].dttk_size = sval; 8972 } 8973 8974 break; 8975 8976 case DIF_OP_PUSHTV: 8977 if (ttop == DIF_DTR_NREGS) 8978 return; 8979 8980 tupregs[ttop++].dttk_size = 0; 8981 break; 8982 8983 case DIF_OP_FLUSHTS: 8984 ttop = 0; 8985 break; 8986 8987 case DIF_OP_POPTS: 8988 if (ttop != 0) 8989 ttop--; 8990 break; 8991 } 8992 8993 sval = 0; 8994 srd = 0; 8995 8996 if (nkeys == 0) 8997 continue; 8998 8999 /* 9000 * We have a dynamic variable allocation; calculate its size. 9001 */ 9002 for (ksize = 0, i = 0; i < nkeys; i++) 9003 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 9004 9005 size = sizeof (dtrace_dynvar_t); 9006 size += sizeof (dtrace_key_t) * (nkeys - 1); 9007 size += ksize; 9008 9009 /* 9010 * Now we need to determine the size of the stored data. 9011 */ 9012 id = DIF_INSTR_VAR(instr); 9013 9014 for (i = 0; i < dp->dtdo_varlen; i++) { 9015 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9016 9017 if (v->dtdv_id == id && v->dtdv_scope == scope) { 9018 size += v->dtdv_type.dtdt_size; 9019 break; 9020 } 9021 } 9022 9023 if (i == dp->dtdo_varlen) 9024 return; 9025 9026 /* 9027 * We have the size. If this is larger than the chunk size 9028 * for our dynamic variable state, reset the chunk size. 9029 */ 9030 size = P2ROUNDUP(size, sizeof (uint64_t)); 9031 9032 if (size > vstate->dtvs_dynvars.dtds_chunksize) 9033 vstate->dtvs_dynvars.dtds_chunksize = size; 9034 } 9035 } 9036 9037 static void 9038 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9039 { 9040 int i, oldsvars, osz, nsz, otlocals, ntlocals; 9041 uint_t id; 9042 9043 ASSERT(MUTEX_HELD(&dtrace_lock)); 9044 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 9045 9046 for (i = 0; i < dp->dtdo_varlen; i++) { 9047 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9048 dtrace_statvar_t *svar, ***svarp; 9049 size_t dsize = 0; 9050 uint8_t scope = v->dtdv_scope; 9051 int *np; 9052 9053 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9054 continue; 9055 9056 id -= DIF_VAR_OTHER_UBASE; 9057 9058 switch (scope) { 9059 case DIFV_SCOPE_THREAD: 9060 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 9061 dtrace_difv_t *tlocals; 9062 9063 if ((ntlocals = (otlocals << 1)) == 0) 9064 ntlocals = 1; 9065 9066 osz = otlocals * sizeof (dtrace_difv_t); 9067 nsz = ntlocals * sizeof (dtrace_difv_t); 9068 9069 tlocals = kmem_zalloc(nsz, KM_SLEEP); 9070 9071 if (osz != 0) { 9072 bcopy(vstate->dtvs_tlocals, 9073 tlocals, osz); 9074 kmem_free(vstate->dtvs_tlocals, osz); 9075 } 9076 9077 vstate->dtvs_tlocals = tlocals; 9078 vstate->dtvs_ntlocals = ntlocals; 9079 } 9080 9081 vstate->dtvs_tlocals[id] = *v; 9082 continue; 9083 9084 case DIFV_SCOPE_LOCAL: 9085 np = &vstate->dtvs_nlocals; 9086 svarp = &vstate->dtvs_locals; 9087 9088 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9089 dsize = NCPU * (v->dtdv_type.dtdt_size + 9090 sizeof (uint64_t)); 9091 else 9092 dsize = NCPU * sizeof (uint64_t); 9093 9094 break; 9095 9096 case DIFV_SCOPE_GLOBAL: 9097 np = &vstate->dtvs_nglobals; 9098 svarp = &vstate->dtvs_globals; 9099 9100 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9101 dsize = v->dtdv_type.dtdt_size + 9102 sizeof (uint64_t); 9103 9104 break; 9105 9106 default: 9107 ASSERT(0); 9108 } 9109 9110 while (id >= (oldsvars = *np)) { 9111 dtrace_statvar_t **statics; 9112 int newsvars, oldsize, newsize; 9113 9114 if ((newsvars = (oldsvars << 1)) == 0) 9115 newsvars = 1; 9116 9117 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 9118 newsize = newsvars * sizeof (dtrace_statvar_t *); 9119 9120 statics = kmem_zalloc(newsize, KM_SLEEP); 9121 9122 if (oldsize != 0) { 9123 bcopy(*svarp, statics, oldsize); 9124 kmem_free(*svarp, oldsize); 9125 } 9126 9127 *svarp = statics; 9128 *np = newsvars; 9129 } 9130 9131 if ((svar = (*svarp)[id]) == NULL) { 9132 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 9133 svar->dtsv_var = *v; 9134 9135 if ((svar->dtsv_size = dsize) != 0) { 9136 svar->dtsv_data = (uint64_t)(uintptr_t) 9137 kmem_zalloc(dsize, KM_SLEEP); 9138 } 9139 9140 (*svarp)[id] = svar; 9141 } 9142 9143 svar->dtsv_refcnt++; 9144 } 9145 9146 dtrace_difo_chunksize(dp, vstate); 9147 dtrace_difo_hold(dp); 9148 } 9149 9150 static dtrace_difo_t * 9151 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9152 { 9153 dtrace_difo_t *new; 9154 size_t sz; 9155 9156 ASSERT(dp->dtdo_buf != NULL); 9157 ASSERT(dp->dtdo_refcnt != 0); 9158 9159 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 9160 9161 ASSERT(dp->dtdo_buf != NULL); 9162 sz = dp->dtdo_len * sizeof (dif_instr_t); 9163 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 9164 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 9165 new->dtdo_len = dp->dtdo_len; 9166 9167 if (dp->dtdo_strtab != NULL) { 9168 ASSERT(dp->dtdo_strlen != 0); 9169 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 9170 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 9171 new->dtdo_strlen = dp->dtdo_strlen; 9172 } 9173 9174 if (dp->dtdo_inttab != NULL) { 9175 ASSERT(dp->dtdo_intlen != 0); 9176 sz = dp->dtdo_intlen * sizeof (uint64_t); 9177 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 9178 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 9179 new->dtdo_intlen = dp->dtdo_intlen; 9180 } 9181 9182 if (dp->dtdo_vartab != NULL) { 9183 ASSERT(dp->dtdo_varlen != 0); 9184 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 9185 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 9186 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 9187 new->dtdo_varlen = dp->dtdo_varlen; 9188 } 9189 9190 dtrace_difo_init(new, vstate); 9191 return (new); 9192 } 9193 9194 static void 9195 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9196 { 9197 int i; 9198 9199 ASSERT(dp->dtdo_refcnt == 0); 9200 9201 for (i = 0; i < dp->dtdo_varlen; i++) { 9202 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9203 dtrace_statvar_t *svar, **svarp; 9204 uint_t id; 9205 uint8_t scope = v->dtdv_scope; 9206 int *np; 9207 9208 switch (scope) { 9209 case DIFV_SCOPE_THREAD: 9210 continue; 9211 9212 case DIFV_SCOPE_LOCAL: 9213 np = &vstate->dtvs_nlocals; 9214 svarp = vstate->dtvs_locals; 9215 break; 9216 9217 case DIFV_SCOPE_GLOBAL: 9218 np = &vstate->dtvs_nglobals; 9219 svarp = vstate->dtvs_globals; 9220 break; 9221 9222 default: 9223 ASSERT(0); 9224 } 9225 9226 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9227 continue; 9228 9229 id -= DIF_VAR_OTHER_UBASE; 9230 ASSERT(id < *np); 9231 9232 svar = svarp[id]; 9233 ASSERT(svar != NULL); 9234 ASSERT(svar->dtsv_refcnt > 0); 9235 9236 if (--svar->dtsv_refcnt > 0) 9237 continue; 9238 9239 if (svar->dtsv_size != 0) { 9240 ASSERT(svar->dtsv_data != NULL); 9241 kmem_free((void *)(uintptr_t)svar->dtsv_data, 9242 svar->dtsv_size); 9243 } 9244 9245 kmem_free(svar, sizeof (dtrace_statvar_t)); 9246 svarp[id] = NULL; 9247 } 9248 9249 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 9250 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 9251 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 9252 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 9253 9254 kmem_free(dp, sizeof (dtrace_difo_t)); 9255 } 9256 9257 static void 9258 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9259 { 9260 int i; 9261 9262 ASSERT(MUTEX_HELD(&dtrace_lock)); 9263 ASSERT(dp->dtdo_refcnt != 0); 9264 9265 for (i = 0; i < dp->dtdo_varlen; i++) { 9266 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9267 9268 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9269 continue; 9270 9271 ASSERT(dtrace_vtime_references > 0); 9272 if (--dtrace_vtime_references == 0) 9273 dtrace_vtime_disable(); 9274 } 9275 9276 if (--dp->dtdo_refcnt == 0) 9277 dtrace_difo_destroy(dp, vstate); 9278 } 9279 9280 /* 9281 * DTrace Format Functions 9282 */ 9283 static uint16_t 9284 dtrace_format_add(dtrace_state_t *state, char *str) 9285 { 9286 char *fmt, **new; 9287 uint16_t ndx, len = strlen(str) + 1; 9288 9289 fmt = kmem_zalloc(len, KM_SLEEP); 9290 bcopy(str, fmt, len); 9291 9292 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 9293 if (state->dts_formats[ndx] == NULL) { 9294 state->dts_formats[ndx] = fmt; 9295 return (ndx + 1); 9296 } 9297 } 9298 9299 if (state->dts_nformats == USHRT_MAX) { 9300 /* 9301 * This is only likely if a denial-of-service attack is being 9302 * attempted. As such, it's okay to fail silently here. 9303 */ 9304 kmem_free(fmt, len); 9305 return (0); 9306 } 9307 9308 /* 9309 * For simplicity, we always resize the formats array to be exactly the 9310 * number of formats. 9311 */ 9312 ndx = state->dts_nformats++; 9313 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 9314 9315 if (state->dts_formats != NULL) { 9316 ASSERT(ndx != 0); 9317 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 9318 kmem_free(state->dts_formats, ndx * sizeof (char *)); 9319 } 9320 9321 state->dts_formats = new; 9322 state->dts_formats[ndx] = fmt; 9323 9324 return (ndx + 1); 9325 } 9326 9327 static void 9328 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 9329 { 9330 char *fmt; 9331 9332 ASSERT(state->dts_formats != NULL); 9333 ASSERT(format <= state->dts_nformats); 9334 ASSERT(state->dts_formats[format - 1] != NULL); 9335 9336 fmt = state->dts_formats[format - 1]; 9337 kmem_free(fmt, strlen(fmt) + 1); 9338 state->dts_formats[format - 1] = NULL; 9339 } 9340 9341 static void 9342 dtrace_format_destroy(dtrace_state_t *state) 9343 { 9344 int i; 9345 9346 if (state->dts_nformats == 0) { 9347 ASSERT(state->dts_formats == NULL); 9348 return; 9349 } 9350 9351 ASSERT(state->dts_formats != NULL); 9352 9353 for (i = 0; i < state->dts_nformats; i++) { 9354 char *fmt = state->dts_formats[i]; 9355 9356 if (fmt == NULL) 9357 continue; 9358 9359 kmem_free(fmt, strlen(fmt) + 1); 9360 } 9361 9362 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 9363 state->dts_nformats = 0; 9364 state->dts_formats = NULL; 9365 } 9366 9367 /* 9368 * DTrace Predicate Functions 9369 */ 9370 static dtrace_predicate_t * 9371 dtrace_predicate_create(dtrace_difo_t *dp) 9372 { 9373 dtrace_predicate_t *pred; 9374 9375 ASSERT(MUTEX_HELD(&dtrace_lock)); 9376 ASSERT(dp->dtdo_refcnt != 0); 9377 9378 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 9379 pred->dtp_difo = dp; 9380 pred->dtp_refcnt = 1; 9381 9382 if (!dtrace_difo_cacheable(dp)) 9383 return (pred); 9384 9385 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 9386 /* 9387 * This is only theoretically possible -- we have had 2^32 9388 * cacheable predicates on this machine. We cannot allow any 9389 * more predicates to become cacheable: as unlikely as it is, 9390 * there may be a thread caching a (now stale) predicate cache 9391 * ID. (N.B.: the temptation is being successfully resisted to 9392 * have this cmn_err() "Holy shit -- we executed this code!") 9393 */ 9394 return (pred); 9395 } 9396 9397 pred->dtp_cacheid = dtrace_predcache_id++; 9398 9399 return (pred); 9400 } 9401 9402 static void 9403 dtrace_predicate_hold(dtrace_predicate_t *pred) 9404 { 9405 ASSERT(MUTEX_HELD(&dtrace_lock)); 9406 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 9407 ASSERT(pred->dtp_refcnt > 0); 9408 9409 pred->dtp_refcnt++; 9410 } 9411 9412 static void 9413 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 9414 { 9415 dtrace_difo_t *dp = pred->dtp_difo; 9416 9417 ASSERT(MUTEX_HELD(&dtrace_lock)); 9418 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 9419 ASSERT(pred->dtp_refcnt > 0); 9420 9421 if (--pred->dtp_refcnt == 0) { 9422 dtrace_difo_release(pred->dtp_difo, vstate); 9423 kmem_free(pred, sizeof (dtrace_predicate_t)); 9424 } 9425 } 9426 9427 /* 9428 * DTrace Action Description Functions 9429 */ 9430 static dtrace_actdesc_t * 9431 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 9432 uint64_t uarg, uint64_t arg) 9433 { 9434 dtrace_actdesc_t *act; 9435 9436 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 9437 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 9438 9439 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 9440 act->dtad_kind = kind; 9441 act->dtad_ntuple = ntuple; 9442 act->dtad_uarg = uarg; 9443 act->dtad_arg = arg; 9444 act->dtad_refcnt = 1; 9445 9446 return (act); 9447 } 9448 9449 static void 9450 dtrace_actdesc_hold(dtrace_actdesc_t *act) 9451 { 9452 ASSERT(act->dtad_refcnt >= 1); 9453 act->dtad_refcnt++; 9454 } 9455 9456 static void 9457 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 9458 { 9459 dtrace_actkind_t kind = act->dtad_kind; 9460 dtrace_difo_t *dp; 9461 9462 ASSERT(act->dtad_refcnt >= 1); 9463 9464 if (--act->dtad_refcnt != 0) 9465 return; 9466 9467 if ((dp = act->dtad_difo) != NULL) 9468 dtrace_difo_release(dp, vstate); 9469 9470 if (DTRACEACT_ISPRINTFLIKE(kind)) { 9471 char *str = (char *)(uintptr_t)act->dtad_arg; 9472 9473 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 9474 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 9475 9476 if (str != NULL) 9477 kmem_free(str, strlen(str) + 1); 9478 } 9479 9480 kmem_free(act, sizeof (dtrace_actdesc_t)); 9481 } 9482 9483 /* 9484 * DTrace ECB Functions 9485 */ 9486 static dtrace_ecb_t * 9487 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 9488 { 9489 dtrace_ecb_t *ecb; 9490 dtrace_epid_t epid; 9491 9492 ASSERT(MUTEX_HELD(&dtrace_lock)); 9493 9494 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 9495 ecb->dte_predicate = NULL; 9496 ecb->dte_probe = probe; 9497 9498 /* 9499 * The default size is the size of the default action: recording 9500 * the epid. 9501 */ 9502 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9503 ecb->dte_alignment = sizeof (dtrace_epid_t); 9504 9505 epid = state->dts_epid++; 9506 9507 if (epid - 1 >= state->dts_necbs) { 9508 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 9509 int necbs = state->dts_necbs << 1; 9510 9511 ASSERT(epid == state->dts_necbs + 1); 9512 9513 if (necbs == 0) { 9514 ASSERT(oecbs == NULL); 9515 necbs = 1; 9516 } 9517 9518 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 9519 9520 if (oecbs != NULL) 9521 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 9522 9523 dtrace_membar_producer(); 9524 state->dts_ecbs = ecbs; 9525 9526 if (oecbs != NULL) { 9527 /* 9528 * If this state is active, we must dtrace_sync() 9529 * before we can free the old dts_ecbs array: we're 9530 * coming in hot, and there may be active ring 9531 * buffer processing (which indexes into the dts_ecbs 9532 * array) on another CPU. 9533 */ 9534 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 9535 dtrace_sync(); 9536 9537 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 9538 } 9539 9540 dtrace_membar_producer(); 9541 state->dts_necbs = necbs; 9542 } 9543 9544 ecb->dte_state = state; 9545 9546 ASSERT(state->dts_ecbs[epid - 1] == NULL); 9547 dtrace_membar_producer(); 9548 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 9549 9550 return (ecb); 9551 } 9552 9553 static int 9554 dtrace_ecb_enable(dtrace_ecb_t *ecb) 9555 { 9556 dtrace_probe_t *probe = ecb->dte_probe; 9557 9558 ASSERT(MUTEX_HELD(&cpu_lock)); 9559 ASSERT(MUTEX_HELD(&dtrace_lock)); 9560 ASSERT(ecb->dte_next == NULL); 9561 9562 if (probe == NULL) { 9563 /* 9564 * This is the NULL probe -- there's nothing to do. 9565 */ 9566 return (0); 9567 } 9568 9569 if (probe->dtpr_ecb == NULL) { 9570 dtrace_provider_t *prov = probe->dtpr_provider; 9571 9572 /* 9573 * We're the first ECB on this probe. 9574 */ 9575 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 9576 9577 if (ecb->dte_predicate != NULL) 9578 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 9579 9580 return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 9581 probe->dtpr_id, probe->dtpr_arg)); 9582 } else { 9583 /* 9584 * This probe is already active. Swing the last pointer to 9585 * point to the new ECB, and issue a dtrace_sync() to assure 9586 * that all CPUs have seen the change. 9587 */ 9588 ASSERT(probe->dtpr_ecb_last != NULL); 9589 probe->dtpr_ecb_last->dte_next = ecb; 9590 probe->dtpr_ecb_last = ecb; 9591 probe->dtpr_predcache = 0; 9592 9593 dtrace_sync(); 9594 return (0); 9595 } 9596 } 9597 9598 static void 9599 dtrace_ecb_resize(dtrace_ecb_t *ecb) 9600 { 9601 uint32_t maxalign = sizeof (dtrace_epid_t); 9602 uint32_t align = sizeof (uint8_t), offs, diff; 9603 dtrace_action_t *act; 9604 int wastuple = 0; 9605 uint32_t aggbase = UINT32_MAX; 9606 dtrace_state_t *state = ecb->dte_state; 9607 9608 /* 9609 * If we record anything, we always record the epid. (And we always 9610 * record it first.) 9611 */ 9612 offs = sizeof (dtrace_epid_t); 9613 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9614 9615 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9616 dtrace_recdesc_t *rec = &act->dta_rec; 9617 9618 if ((align = rec->dtrd_alignment) > maxalign) 9619 maxalign = align; 9620 9621 if (!wastuple && act->dta_intuple) { 9622 /* 9623 * This is the first record in a tuple. Align the 9624 * offset to be at offset 4 in an 8-byte aligned 9625 * block. 9626 */ 9627 diff = offs + sizeof (dtrace_aggid_t); 9628 9629 if (diff = (diff & (sizeof (uint64_t) - 1))) 9630 offs += sizeof (uint64_t) - diff; 9631 9632 aggbase = offs - sizeof (dtrace_aggid_t); 9633 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 9634 } 9635 9636 /*LINTED*/ 9637 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 9638 /* 9639 * The current offset is not properly aligned; align it. 9640 */ 9641 offs += align - diff; 9642 } 9643 9644 rec->dtrd_offset = offs; 9645 9646 if (offs + rec->dtrd_size > ecb->dte_needed) { 9647 ecb->dte_needed = offs + rec->dtrd_size; 9648 9649 if (ecb->dte_needed > state->dts_needed) 9650 state->dts_needed = ecb->dte_needed; 9651 } 9652 9653 if (DTRACEACT_ISAGG(act->dta_kind)) { 9654 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9655 dtrace_action_t *first = agg->dtag_first, *prev; 9656 9657 ASSERT(rec->dtrd_size != 0 && first != NULL); 9658 ASSERT(wastuple); 9659 ASSERT(aggbase != UINT32_MAX); 9660 9661 agg->dtag_base = aggbase; 9662 9663 while ((prev = first->dta_prev) != NULL && 9664 DTRACEACT_ISAGG(prev->dta_kind)) { 9665 agg = (dtrace_aggregation_t *)prev; 9666 first = agg->dtag_first; 9667 } 9668 9669 if (prev != NULL) { 9670 offs = prev->dta_rec.dtrd_offset + 9671 prev->dta_rec.dtrd_size; 9672 } else { 9673 offs = sizeof (dtrace_epid_t); 9674 } 9675 wastuple = 0; 9676 } else { 9677 if (!act->dta_intuple) 9678 ecb->dte_size = offs + rec->dtrd_size; 9679 9680 offs += rec->dtrd_size; 9681 } 9682 9683 wastuple = act->dta_intuple; 9684 } 9685 9686 if ((act = ecb->dte_action) != NULL && 9687 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 9688 ecb->dte_size == sizeof (dtrace_epid_t)) { 9689 /* 9690 * If the size is still sizeof (dtrace_epid_t), then all 9691 * actions store no data; set the size to 0. 9692 */ 9693 ecb->dte_alignment = maxalign; 9694 ecb->dte_size = 0; 9695 9696 /* 9697 * If the needed space is still sizeof (dtrace_epid_t), then 9698 * all actions need no additional space; set the needed 9699 * size to 0. 9700 */ 9701 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 9702 ecb->dte_needed = 0; 9703 9704 return; 9705 } 9706 9707 /* 9708 * Set our alignment, and make sure that the dte_size and dte_needed 9709 * are aligned to the size of an EPID. 9710 */ 9711 ecb->dte_alignment = maxalign; 9712 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 9713 ~(sizeof (dtrace_epid_t) - 1); 9714 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 9715 ~(sizeof (dtrace_epid_t) - 1); 9716 ASSERT(ecb->dte_size <= ecb->dte_needed); 9717 } 9718 9719 static dtrace_action_t * 9720 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9721 { 9722 dtrace_aggregation_t *agg; 9723 size_t size = sizeof (uint64_t); 9724 int ntuple = desc->dtad_ntuple; 9725 dtrace_action_t *act; 9726 dtrace_recdesc_t *frec; 9727 dtrace_aggid_t aggid; 9728 dtrace_state_t *state = ecb->dte_state; 9729 9730 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 9731 agg->dtag_ecb = ecb; 9732 9733 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 9734 9735 switch (desc->dtad_kind) { 9736 case DTRACEAGG_MIN: 9737 agg->dtag_initial = INT64_MAX; 9738 agg->dtag_aggregate = dtrace_aggregate_min; 9739 break; 9740 9741 case DTRACEAGG_MAX: 9742 agg->dtag_initial = INT64_MIN; 9743 agg->dtag_aggregate = dtrace_aggregate_max; 9744 break; 9745 9746 case DTRACEAGG_COUNT: 9747 agg->dtag_aggregate = dtrace_aggregate_count; 9748 break; 9749 9750 case DTRACEAGG_QUANTIZE: 9751 agg->dtag_aggregate = dtrace_aggregate_quantize; 9752 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 9753 sizeof (uint64_t); 9754 break; 9755 9756 case DTRACEAGG_LQUANTIZE: { 9757 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 9758 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 9759 9760 agg->dtag_initial = desc->dtad_arg; 9761 agg->dtag_aggregate = dtrace_aggregate_lquantize; 9762 9763 if (step == 0 || levels == 0) 9764 goto err; 9765 9766 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 9767 break; 9768 } 9769 9770 case DTRACEAGG_LLQUANTIZE: { 9771 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 9772 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 9773 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 9774 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 9775 int64_t v; 9776 9777 agg->dtag_initial = desc->dtad_arg; 9778 agg->dtag_aggregate = dtrace_aggregate_llquantize; 9779 9780 if (factor < 2 || low >= high || nsteps < factor) 9781 goto err; 9782 9783 /* 9784 * Now check that the number of steps evenly divides a power 9785 * of the factor. (This assures both integer bucket size and 9786 * linearity within each magnitude.) 9787 */ 9788 for (v = factor; v < nsteps; v *= factor) 9789 continue; 9790 9791 if ((v % nsteps) || (nsteps % factor)) 9792 goto err; 9793 9794 size = (dtrace_aggregate_llquantize_bucket(factor, 9795 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 9796 break; 9797 } 9798 9799 case DTRACEAGG_AVG: 9800 agg->dtag_aggregate = dtrace_aggregate_avg; 9801 size = sizeof (uint64_t) * 2; 9802 break; 9803 9804 case DTRACEAGG_STDDEV: 9805 agg->dtag_aggregate = dtrace_aggregate_stddev; 9806 size = sizeof (uint64_t) * 4; 9807 break; 9808 9809 case DTRACEAGG_SUM: 9810 agg->dtag_aggregate = dtrace_aggregate_sum; 9811 break; 9812 9813 default: 9814 goto err; 9815 } 9816 9817 agg->dtag_action.dta_rec.dtrd_size = size; 9818 9819 if (ntuple == 0) 9820 goto err; 9821 9822 /* 9823 * We must make sure that we have enough actions for the n-tuple. 9824 */ 9825 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 9826 if (DTRACEACT_ISAGG(act->dta_kind)) 9827 break; 9828 9829 if (--ntuple == 0) { 9830 /* 9831 * This is the action with which our n-tuple begins. 9832 */ 9833 agg->dtag_first = act; 9834 goto success; 9835 } 9836 } 9837 9838 /* 9839 * This n-tuple is short by ntuple elements. Return failure. 9840 */ 9841 ASSERT(ntuple != 0); 9842 err: 9843 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9844 return (NULL); 9845 9846 success: 9847 /* 9848 * If the last action in the tuple has a size of zero, it's actually 9849 * an expression argument for the aggregating action. 9850 */ 9851 ASSERT(ecb->dte_action_last != NULL); 9852 act = ecb->dte_action_last; 9853 9854 if (act->dta_kind == DTRACEACT_DIFEXPR) { 9855 ASSERT(act->dta_difo != NULL); 9856 9857 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 9858 agg->dtag_hasarg = 1; 9859 } 9860 9861 /* 9862 * We need to allocate an id for this aggregation. 9863 */ 9864 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 9865 VM_BESTFIT | VM_SLEEP); 9866 9867 if (aggid - 1 >= state->dts_naggregations) { 9868 dtrace_aggregation_t **oaggs = state->dts_aggregations; 9869 dtrace_aggregation_t **aggs; 9870 int naggs = state->dts_naggregations << 1; 9871 int onaggs = state->dts_naggregations; 9872 9873 ASSERT(aggid == state->dts_naggregations + 1); 9874 9875 if (naggs == 0) { 9876 ASSERT(oaggs == NULL); 9877 naggs = 1; 9878 } 9879 9880 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 9881 9882 if (oaggs != NULL) { 9883 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 9884 kmem_free(oaggs, onaggs * sizeof (*aggs)); 9885 } 9886 9887 state->dts_aggregations = aggs; 9888 state->dts_naggregations = naggs; 9889 } 9890 9891 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 9892 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 9893 9894 frec = &agg->dtag_first->dta_rec; 9895 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 9896 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 9897 9898 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 9899 ASSERT(!act->dta_intuple); 9900 act->dta_intuple = 1; 9901 } 9902 9903 return (&agg->dtag_action); 9904 } 9905 9906 static void 9907 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 9908 { 9909 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9910 dtrace_state_t *state = ecb->dte_state; 9911 dtrace_aggid_t aggid = agg->dtag_id; 9912 9913 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 9914 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 9915 9916 ASSERT(state->dts_aggregations[aggid - 1] == agg); 9917 state->dts_aggregations[aggid - 1] = NULL; 9918 9919 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9920 } 9921 9922 static int 9923 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9924 { 9925 dtrace_action_t *action, *last; 9926 dtrace_difo_t *dp = desc->dtad_difo; 9927 uint32_t size = 0, align = sizeof (uint8_t), mask; 9928 uint16_t format = 0; 9929 dtrace_recdesc_t *rec; 9930 dtrace_state_t *state = ecb->dte_state; 9931 dtrace_optval_t *opt = state->dts_options, nframes, strsize; 9932 uint64_t arg = desc->dtad_arg; 9933 9934 ASSERT(MUTEX_HELD(&dtrace_lock)); 9935 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 9936 9937 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 9938 /* 9939 * If this is an aggregating action, there must be neither 9940 * a speculate nor a commit on the action chain. 9941 */ 9942 dtrace_action_t *act; 9943 9944 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9945 if (act->dta_kind == DTRACEACT_COMMIT) 9946 return (EINVAL); 9947 9948 if (act->dta_kind == DTRACEACT_SPECULATE) 9949 return (EINVAL); 9950 } 9951 9952 action = dtrace_ecb_aggregation_create(ecb, desc); 9953 9954 if (action == NULL) 9955 return (EINVAL); 9956 } else { 9957 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 9958 (desc->dtad_kind == DTRACEACT_DIFEXPR && 9959 dp != NULL && dp->dtdo_destructive)) { 9960 state->dts_destructive = 1; 9961 } 9962 9963 switch (desc->dtad_kind) { 9964 case DTRACEACT_PRINTF: 9965 case DTRACEACT_PRINTA: 9966 case DTRACEACT_SYSTEM: 9967 case DTRACEACT_FREOPEN: 9968 case DTRACEACT_DIFEXPR: 9969 /* 9970 * We know that our arg is a string -- turn it into a 9971 * format. 9972 */ 9973 if (arg == NULL) { 9974 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 9975 desc->dtad_kind == DTRACEACT_DIFEXPR); 9976 format = 0; 9977 } else { 9978 ASSERT(arg != NULL); 9979 ASSERT(arg > KERNELBASE); 9980 format = dtrace_format_add(state, 9981 (char *)(uintptr_t)arg); 9982 } 9983 9984 /*FALLTHROUGH*/ 9985 case DTRACEACT_LIBACT: 9986 case DTRACEACT_TRACEMEM: 9987 case DTRACEACT_TRACEMEM_DYNSIZE: 9988 if (dp == NULL) 9989 return (EINVAL); 9990 9991 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 9992 break; 9993 9994 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 9995 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9996 return (EINVAL); 9997 9998 size = opt[DTRACEOPT_STRSIZE]; 9999 } 10000 10001 break; 10002 10003 case DTRACEACT_STACK: 10004 if ((nframes = arg) == 0) { 10005 nframes = opt[DTRACEOPT_STACKFRAMES]; 10006 ASSERT(nframes > 0); 10007 arg = nframes; 10008 } 10009 10010 size = nframes * sizeof (pc_t); 10011 break; 10012 10013 case DTRACEACT_JSTACK: 10014 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 10015 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 10016 10017 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 10018 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 10019 10020 arg = DTRACE_USTACK_ARG(nframes, strsize); 10021 10022 /*FALLTHROUGH*/ 10023 case DTRACEACT_USTACK: 10024 if (desc->dtad_kind != DTRACEACT_JSTACK && 10025 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 10026 strsize = DTRACE_USTACK_STRSIZE(arg); 10027 nframes = opt[DTRACEOPT_USTACKFRAMES]; 10028 ASSERT(nframes > 0); 10029 arg = DTRACE_USTACK_ARG(nframes, strsize); 10030 } 10031 10032 /* 10033 * Save a slot for the pid. 10034 */ 10035 size = (nframes + 1) * sizeof (uint64_t); 10036 size += DTRACE_USTACK_STRSIZE(arg); 10037 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 10038 10039 break; 10040 10041 case DTRACEACT_SYM: 10042 case DTRACEACT_MOD: 10043 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 10044 sizeof (uint64_t)) || 10045 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10046 return (EINVAL); 10047 break; 10048 10049 case DTRACEACT_USYM: 10050 case DTRACEACT_UMOD: 10051 case DTRACEACT_UADDR: 10052 if (dp == NULL || 10053 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 10054 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10055 return (EINVAL); 10056 10057 /* 10058 * We have a slot for the pid, plus a slot for the 10059 * argument. To keep things simple (aligned with 10060 * bitness-neutral sizing), we store each as a 64-bit 10061 * quantity. 10062 */ 10063 size = 2 * sizeof (uint64_t); 10064 break; 10065 10066 case DTRACEACT_STOP: 10067 case DTRACEACT_BREAKPOINT: 10068 case DTRACEACT_PANIC: 10069 break; 10070 10071 case DTRACEACT_CHILL: 10072 case DTRACEACT_DISCARD: 10073 case DTRACEACT_RAISE: 10074 if (dp == NULL) 10075 return (EINVAL); 10076 break; 10077 10078 case DTRACEACT_EXIT: 10079 if (dp == NULL || 10080 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 10081 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10082 return (EINVAL); 10083 break; 10084 10085 case DTRACEACT_SPECULATE: 10086 if (ecb->dte_size > sizeof (dtrace_epid_t)) 10087 return (EINVAL); 10088 10089 if (dp == NULL) 10090 return (EINVAL); 10091 10092 state->dts_speculates = 1; 10093 break; 10094 10095 case DTRACEACT_COMMIT: { 10096 dtrace_action_t *act = ecb->dte_action; 10097 10098 for (; act != NULL; act = act->dta_next) { 10099 if (act->dta_kind == DTRACEACT_COMMIT) 10100 return (EINVAL); 10101 } 10102 10103 if (dp == NULL) 10104 return (EINVAL); 10105 break; 10106 } 10107 10108 default: 10109 return (EINVAL); 10110 } 10111 10112 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 10113 /* 10114 * If this is a data-storing action or a speculate, 10115 * we must be sure that there isn't a commit on the 10116 * action chain. 10117 */ 10118 dtrace_action_t *act = ecb->dte_action; 10119 10120 for (; act != NULL; act = act->dta_next) { 10121 if (act->dta_kind == DTRACEACT_COMMIT) 10122 return (EINVAL); 10123 } 10124 } 10125 10126 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 10127 action->dta_rec.dtrd_size = size; 10128 } 10129 10130 action->dta_refcnt = 1; 10131 rec = &action->dta_rec; 10132 size = rec->dtrd_size; 10133 10134 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 10135 if (!(size & mask)) { 10136 align = mask + 1; 10137 break; 10138 } 10139 } 10140 10141 action->dta_kind = desc->dtad_kind; 10142 10143 if ((action->dta_difo = dp) != NULL) 10144 dtrace_difo_hold(dp); 10145 10146 rec->dtrd_action = action->dta_kind; 10147 rec->dtrd_arg = arg; 10148 rec->dtrd_uarg = desc->dtad_uarg; 10149 rec->dtrd_alignment = (uint16_t)align; 10150 rec->dtrd_format = format; 10151 10152 if ((last = ecb->dte_action_last) != NULL) { 10153 ASSERT(ecb->dte_action != NULL); 10154 action->dta_prev = last; 10155 last->dta_next = action; 10156 } else { 10157 ASSERT(ecb->dte_action == NULL); 10158 ecb->dte_action = action; 10159 } 10160 10161 ecb->dte_action_last = action; 10162 10163 return (0); 10164 } 10165 10166 static void 10167 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 10168 { 10169 dtrace_action_t *act = ecb->dte_action, *next; 10170 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 10171 dtrace_difo_t *dp; 10172 uint16_t format; 10173 10174 if (act != NULL && act->dta_refcnt > 1) { 10175 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 10176 act->dta_refcnt--; 10177 } else { 10178 for (; act != NULL; act = next) { 10179 next = act->dta_next; 10180 ASSERT(next != NULL || act == ecb->dte_action_last); 10181 ASSERT(act->dta_refcnt == 1); 10182 10183 if ((format = act->dta_rec.dtrd_format) != 0) 10184 dtrace_format_remove(ecb->dte_state, format); 10185 10186 if ((dp = act->dta_difo) != NULL) 10187 dtrace_difo_release(dp, vstate); 10188 10189 if (DTRACEACT_ISAGG(act->dta_kind)) { 10190 dtrace_ecb_aggregation_destroy(ecb, act); 10191 } else { 10192 kmem_free(act, sizeof (dtrace_action_t)); 10193 } 10194 } 10195 } 10196 10197 ecb->dte_action = NULL; 10198 ecb->dte_action_last = NULL; 10199 ecb->dte_size = sizeof (dtrace_epid_t); 10200 } 10201 10202 static void 10203 dtrace_ecb_disable(dtrace_ecb_t *ecb) 10204 { 10205 /* 10206 * We disable the ECB by removing it from its probe. 10207 */ 10208 dtrace_ecb_t *pecb, *prev = NULL; 10209 dtrace_probe_t *probe = ecb->dte_probe; 10210 10211 ASSERT(MUTEX_HELD(&dtrace_lock)); 10212 10213 if (probe == NULL) { 10214 /* 10215 * This is the NULL probe; there is nothing to disable. 10216 */ 10217 return; 10218 } 10219 10220 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 10221 if (pecb == ecb) 10222 break; 10223 prev = pecb; 10224 } 10225 10226 ASSERT(pecb != NULL); 10227 10228 if (prev == NULL) { 10229 probe->dtpr_ecb = ecb->dte_next; 10230 } else { 10231 prev->dte_next = ecb->dte_next; 10232 } 10233 10234 if (ecb == probe->dtpr_ecb_last) { 10235 ASSERT(ecb->dte_next == NULL); 10236 probe->dtpr_ecb_last = prev; 10237 } 10238 10239 /* 10240 * The ECB has been disconnected from the probe; now sync to assure 10241 * that all CPUs have seen the change before returning. 10242 */ 10243 dtrace_sync(); 10244 10245 if (probe->dtpr_ecb == NULL) { 10246 /* 10247 * That was the last ECB on the probe; clear the predicate 10248 * cache ID for the probe, disable it and sync one more time 10249 * to assure that we'll never hit it again. 10250 */ 10251 dtrace_provider_t *prov = probe->dtpr_provider; 10252 10253 ASSERT(ecb->dte_next == NULL); 10254 ASSERT(probe->dtpr_ecb_last == NULL); 10255 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 10256 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 10257 probe->dtpr_id, probe->dtpr_arg); 10258 dtrace_sync(); 10259 } else { 10260 /* 10261 * There is at least one ECB remaining on the probe. If there 10262 * is _exactly_ one, set the probe's predicate cache ID to be 10263 * the predicate cache ID of the remaining ECB. 10264 */ 10265 ASSERT(probe->dtpr_ecb_last != NULL); 10266 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 10267 10268 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 10269 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 10270 10271 ASSERT(probe->dtpr_ecb->dte_next == NULL); 10272 10273 if (p != NULL) 10274 probe->dtpr_predcache = p->dtp_cacheid; 10275 } 10276 10277 ecb->dte_next = NULL; 10278 } 10279 } 10280 10281 static void 10282 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 10283 { 10284 dtrace_state_t *state = ecb->dte_state; 10285 dtrace_vstate_t *vstate = &state->dts_vstate; 10286 dtrace_predicate_t *pred; 10287 dtrace_epid_t epid = ecb->dte_epid; 10288 10289 ASSERT(MUTEX_HELD(&dtrace_lock)); 10290 ASSERT(ecb->dte_next == NULL); 10291 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 10292 10293 if ((pred = ecb->dte_predicate) != NULL) 10294 dtrace_predicate_release(pred, vstate); 10295 10296 dtrace_ecb_action_remove(ecb); 10297 10298 ASSERT(state->dts_ecbs[epid - 1] == ecb); 10299 state->dts_ecbs[epid - 1] = NULL; 10300 10301 kmem_free(ecb, sizeof (dtrace_ecb_t)); 10302 } 10303 10304 static dtrace_ecb_t * 10305 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 10306 dtrace_enabling_t *enab) 10307 { 10308 dtrace_ecb_t *ecb; 10309 dtrace_predicate_t *pred; 10310 dtrace_actdesc_t *act; 10311 dtrace_provider_t *prov; 10312 dtrace_ecbdesc_t *desc = enab->dten_current; 10313 10314 ASSERT(MUTEX_HELD(&dtrace_lock)); 10315 ASSERT(state != NULL); 10316 10317 ecb = dtrace_ecb_add(state, probe); 10318 ecb->dte_uarg = desc->dted_uarg; 10319 10320 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 10321 dtrace_predicate_hold(pred); 10322 ecb->dte_predicate = pred; 10323 } 10324 10325 if (probe != NULL) { 10326 /* 10327 * If the provider shows more leg than the consumer is old 10328 * enough to see, we need to enable the appropriate implicit 10329 * predicate bits to prevent the ecb from activating at 10330 * revealing times. 10331 * 10332 * Providers specifying DTRACE_PRIV_USER at register time 10333 * are stating that they need the /proc-style privilege 10334 * model to be enforced, and this is what DTRACE_COND_OWNER 10335 * and DTRACE_COND_ZONEOWNER will then do at probe time. 10336 */ 10337 prov = probe->dtpr_provider; 10338 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 10339 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10340 ecb->dte_cond |= DTRACE_COND_OWNER; 10341 10342 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 10343 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10344 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 10345 10346 /* 10347 * If the provider shows us kernel innards and the user 10348 * is lacking sufficient privilege, enable the 10349 * DTRACE_COND_USERMODE implicit predicate. 10350 */ 10351 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 10352 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 10353 ecb->dte_cond |= DTRACE_COND_USERMODE; 10354 } 10355 10356 if (dtrace_ecb_create_cache != NULL) { 10357 /* 10358 * If we have a cached ecb, we'll use its action list instead 10359 * of creating our own (saving both time and space). 10360 */ 10361 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 10362 dtrace_action_t *act = cached->dte_action; 10363 10364 if (act != NULL) { 10365 ASSERT(act->dta_refcnt > 0); 10366 act->dta_refcnt++; 10367 ecb->dte_action = act; 10368 ecb->dte_action_last = cached->dte_action_last; 10369 ecb->dte_needed = cached->dte_needed; 10370 ecb->dte_size = cached->dte_size; 10371 ecb->dte_alignment = cached->dte_alignment; 10372 } 10373 10374 return (ecb); 10375 } 10376 10377 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 10378 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 10379 dtrace_ecb_destroy(ecb); 10380 return (NULL); 10381 } 10382 } 10383 10384 dtrace_ecb_resize(ecb); 10385 10386 return (dtrace_ecb_create_cache = ecb); 10387 } 10388 10389 static int 10390 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 10391 { 10392 dtrace_ecb_t *ecb; 10393 dtrace_enabling_t *enab = arg; 10394 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 10395 10396 ASSERT(state != NULL); 10397 10398 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 10399 /* 10400 * This probe was created in a generation for which this 10401 * enabling has previously created ECBs; we don't want to 10402 * enable it again, so just kick out. 10403 */ 10404 return (DTRACE_MATCH_NEXT); 10405 } 10406 10407 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 10408 return (DTRACE_MATCH_DONE); 10409 10410 if (dtrace_ecb_enable(ecb) < 0) 10411 return (DTRACE_MATCH_FAIL); 10412 10413 return (DTRACE_MATCH_NEXT); 10414 } 10415 10416 static dtrace_ecb_t * 10417 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 10418 { 10419 dtrace_ecb_t *ecb; 10420 10421 ASSERT(MUTEX_HELD(&dtrace_lock)); 10422 10423 if (id == 0 || id > state->dts_necbs) 10424 return (NULL); 10425 10426 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 10427 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 10428 10429 return (state->dts_ecbs[id - 1]); 10430 } 10431 10432 static dtrace_aggregation_t * 10433 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 10434 { 10435 dtrace_aggregation_t *agg; 10436 10437 ASSERT(MUTEX_HELD(&dtrace_lock)); 10438 10439 if (id == 0 || id > state->dts_naggregations) 10440 return (NULL); 10441 10442 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 10443 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 10444 agg->dtag_id == id); 10445 10446 return (state->dts_aggregations[id - 1]); 10447 } 10448 10449 /* 10450 * DTrace Buffer Functions 10451 * 10452 * The following functions manipulate DTrace buffers. Most of these functions 10453 * are called in the context of establishing or processing consumer state; 10454 * exceptions are explicitly noted. 10455 */ 10456 10457 /* 10458 * Note: called from cross call context. This function switches the two 10459 * buffers on a given CPU. The atomicity of this operation is assured by 10460 * disabling interrupts while the actual switch takes place; the disabling of 10461 * interrupts serializes the execution with any execution of dtrace_probe() on 10462 * the same CPU. 10463 */ 10464 static void 10465 dtrace_buffer_switch(dtrace_buffer_t *buf) 10466 { 10467 caddr_t tomax = buf->dtb_tomax; 10468 caddr_t xamot = buf->dtb_xamot; 10469 dtrace_icookie_t cookie; 10470 hrtime_t now = dtrace_gethrtime(); 10471 10472 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10473 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 10474 10475 cookie = dtrace_interrupt_disable(); 10476 buf->dtb_tomax = xamot; 10477 buf->dtb_xamot = tomax; 10478 buf->dtb_xamot_drops = buf->dtb_drops; 10479 buf->dtb_xamot_offset = buf->dtb_offset; 10480 buf->dtb_xamot_errors = buf->dtb_errors; 10481 buf->dtb_xamot_flags = buf->dtb_flags; 10482 buf->dtb_offset = 0; 10483 buf->dtb_drops = 0; 10484 buf->dtb_errors = 0; 10485 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 10486 buf->dtb_interval = now - buf->dtb_switched; 10487 buf->dtb_switched = now; 10488 dtrace_interrupt_enable(cookie); 10489 } 10490 10491 /* 10492 * Note: called from cross call context. This function activates a buffer 10493 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 10494 * is guaranteed by the disabling of interrupts. 10495 */ 10496 static void 10497 dtrace_buffer_activate(dtrace_state_t *state) 10498 { 10499 dtrace_buffer_t *buf; 10500 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 10501 10502 buf = &state->dts_buffer[CPU->cpu_id]; 10503 10504 if (buf->dtb_tomax != NULL) { 10505 /* 10506 * We might like to assert that the buffer is marked inactive, 10507 * but this isn't necessarily true: the buffer for the CPU 10508 * that processes the BEGIN probe has its buffer activated 10509 * manually. In this case, we take the (harmless) action 10510 * re-clearing the bit INACTIVE bit. 10511 */ 10512 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 10513 } 10514 10515 dtrace_interrupt_enable(cookie); 10516 } 10517 10518 static int 10519 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 10520 processorid_t cpu, int *factor) 10521 { 10522 cpu_t *cp; 10523 dtrace_buffer_t *buf; 10524 int allocated = 0, desired = 0; 10525 10526 ASSERT(MUTEX_HELD(&cpu_lock)); 10527 ASSERT(MUTEX_HELD(&dtrace_lock)); 10528 10529 *factor = 1; 10530 10531 if (size > dtrace_nonroot_maxsize && 10532 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 10533 return (EFBIG); 10534 10535 cp = cpu_list; 10536 10537 do { 10538 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10539 continue; 10540 10541 buf = &bufs[cp->cpu_id]; 10542 10543 /* 10544 * If there is already a buffer allocated for this CPU, it 10545 * is only possible that this is a DR event. In this case, 10546 * the buffer size must match our specified size. 10547 */ 10548 if (buf->dtb_tomax != NULL) { 10549 ASSERT(buf->dtb_size == size); 10550 continue; 10551 } 10552 10553 ASSERT(buf->dtb_xamot == NULL); 10554 10555 if ((buf->dtb_tomax = kmem_zalloc(size, 10556 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10557 goto err; 10558 10559 buf->dtb_size = size; 10560 buf->dtb_flags = flags; 10561 buf->dtb_offset = 0; 10562 buf->dtb_drops = 0; 10563 10564 if (flags & DTRACEBUF_NOSWITCH) 10565 continue; 10566 10567 if ((buf->dtb_xamot = kmem_zalloc(size, 10568 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10569 goto err; 10570 } while ((cp = cp->cpu_next) != cpu_list); 10571 10572 return (0); 10573 10574 err: 10575 cp = cpu_list; 10576 10577 do { 10578 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10579 continue; 10580 10581 buf = &bufs[cp->cpu_id]; 10582 desired += 2; 10583 10584 if (buf->dtb_xamot != NULL) { 10585 ASSERT(buf->dtb_tomax != NULL); 10586 ASSERT(buf->dtb_size == size); 10587 kmem_free(buf->dtb_xamot, size); 10588 allocated++; 10589 } 10590 10591 if (buf->dtb_tomax != NULL) { 10592 ASSERT(buf->dtb_size == size); 10593 kmem_free(buf->dtb_tomax, size); 10594 allocated++; 10595 } 10596 10597 buf->dtb_tomax = NULL; 10598 buf->dtb_xamot = NULL; 10599 buf->dtb_size = 0; 10600 } while ((cp = cp->cpu_next) != cpu_list); 10601 10602 *factor = desired / (allocated > 0 ? allocated : 1); 10603 10604 return (ENOMEM); 10605 } 10606 10607 /* 10608 * Note: called from probe context. This function just increments the drop 10609 * count on a buffer. It has been made a function to allow for the 10610 * possibility of understanding the source of mysterious drop counts. (A 10611 * problem for which one may be particularly disappointed that DTrace cannot 10612 * be used to understand DTrace.) 10613 */ 10614 static void 10615 dtrace_buffer_drop(dtrace_buffer_t *buf) 10616 { 10617 buf->dtb_drops++; 10618 } 10619 10620 /* 10621 * Note: called from probe context. This function is called to reserve space 10622 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 10623 * mstate. Returns the new offset in the buffer, or a negative value if an 10624 * error has occurred. 10625 */ 10626 static intptr_t 10627 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 10628 dtrace_state_t *state, dtrace_mstate_t *mstate) 10629 { 10630 intptr_t offs = buf->dtb_offset, soffs; 10631 intptr_t woffs; 10632 caddr_t tomax; 10633 size_t total; 10634 10635 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 10636 return (-1); 10637 10638 if ((tomax = buf->dtb_tomax) == NULL) { 10639 dtrace_buffer_drop(buf); 10640 return (-1); 10641 } 10642 10643 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 10644 while (offs & (align - 1)) { 10645 /* 10646 * Assert that our alignment is off by a number which 10647 * is itself sizeof (uint32_t) aligned. 10648 */ 10649 ASSERT(!((align - (offs & (align - 1))) & 10650 (sizeof (uint32_t) - 1))); 10651 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10652 offs += sizeof (uint32_t); 10653 } 10654 10655 if ((soffs = offs + needed) > buf->dtb_size) { 10656 dtrace_buffer_drop(buf); 10657 return (-1); 10658 } 10659 10660 if (mstate == NULL) 10661 return (offs); 10662 10663 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 10664 mstate->dtms_scratch_size = buf->dtb_size - soffs; 10665 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10666 10667 return (offs); 10668 } 10669 10670 if (buf->dtb_flags & DTRACEBUF_FILL) { 10671 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 10672 (buf->dtb_flags & DTRACEBUF_FULL)) 10673 return (-1); 10674 goto out; 10675 } 10676 10677 total = needed + (offs & (align - 1)); 10678 10679 /* 10680 * For a ring buffer, life is quite a bit more complicated. Before 10681 * we can store any padding, we need to adjust our wrapping offset. 10682 * (If we've never before wrapped or we're not about to, no adjustment 10683 * is required.) 10684 */ 10685 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 10686 offs + total > buf->dtb_size) { 10687 woffs = buf->dtb_xamot_offset; 10688 10689 if (offs + total > buf->dtb_size) { 10690 /* 10691 * We can't fit in the end of the buffer. First, a 10692 * sanity check that we can fit in the buffer at all. 10693 */ 10694 if (total > buf->dtb_size) { 10695 dtrace_buffer_drop(buf); 10696 return (-1); 10697 } 10698 10699 /* 10700 * We're going to be storing at the top of the buffer, 10701 * so now we need to deal with the wrapped offset. We 10702 * only reset our wrapped offset to 0 if it is 10703 * currently greater than the current offset. If it 10704 * is less than the current offset, it is because a 10705 * previous allocation induced a wrap -- but the 10706 * allocation didn't subsequently take the space due 10707 * to an error or false predicate evaluation. In this 10708 * case, we'll just leave the wrapped offset alone: if 10709 * the wrapped offset hasn't been advanced far enough 10710 * for this allocation, it will be adjusted in the 10711 * lower loop. 10712 */ 10713 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 10714 if (woffs >= offs) 10715 woffs = 0; 10716 } else { 10717 woffs = 0; 10718 } 10719 10720 /* 10721 * Now we know that we're going to be storing to the 10722 * top of the buffer and that there is room for us 10723 * there. We need to clear the buffer from the current 10724 * offset to the end (there may be old gunk there). 10725 */ 10726 while (offs < buf->dtb_size) 10727 tomax[offs++] = 0; 10728 10729 /* 10730 * We need to set our offset to zero. And because we 10731 * are wrapping, we need to set the bit indicating as 10732 * much. We can also adjust our needed space back 10733 * down to the space required by the ECB -- we know 10734 * that the top of the buffer is aligned. 10735 */ 10736 offs = 0; 10737 total = needed; 10738 buf->dtb_flags |= DTRACEBUF_WRAPPED; 10739 } else { 10740 /* 10741 * There is room for us in the buffer, so we simply 10742 * need to check the wrapped offset. 10743 */ 10744 if (woffs < offs) { 10745 /* 10746 * The wrapped offset is less than the offset. 10747 * This can happen if we allocated buffer space 10748 * that induced a wrap, but then we didn't 10749 * subsequently take the space due to an error 10750 * or false predicate evaluation. This is 10751 * okay; we know that _this_ allocation isn't 10752 * going to induce a wrap. We still can't 10753 * reset the wrapped offset to be zero, 10754 * however: the space may have been trashed in 10755 * the previous failed probe attempt. But at 10756 * least the wrapped offset doesn't need to 10757 * be adjusted at all... 10758 */ 10759 goto out; 10760 } 10761 } 10762 10763 while (offs + total > woffs) { 10764 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 10765 size_t size; 10766 10767 if (epid == DTRACE_EPIDNONE) { 10768 size = sizeof (uint32_t); 10769 } else { 10770 ASSERT(epid <= state->dts_necbs); 10771 ASSERT(state->dts_ecbs[epid - 1] != NULL); 10772 10773 size = state->dts_ecbs[epid - 1]->dte_size; 10774 } 10775 10776 ASSERT(woffs + size <= buf->dtb_size); 10777 ASSERT(size != 0); 10778 10779 if (woffs + size == buf->dtb_size) { 10780 /* 10781 * We've reached the end of the buffer; we want 10782 * to set the wrapped offset to 0 and break 10783 * out. However, if the offs is 0, then we're 10784 * in a strange edge-condition: the amount of 10785 * space that we want to reserve plus the size 10786 * of the record that we're overwriting is 10787 * greater than the size of the buffer. This 10788 * is problematic because if we reserve the 10789 * space but subsequently don't consume it (due 10790 * to a failed predicate or error) the wrapped 10791 * offset will be 0 -- yet the EPID at offset 0 10792 * will not be committed. This situation is 10793 * relatively easy to deal with: if we're in 10794 * this case, the buffer is indistinguishable 10795 * from one that hasn't wrapped; we need only 10796 * finish the job by clearing the wrapped bit, 10797 * explicitly setting the offset to be 0, and 10798 * zero'ing out the old data in the buffer. 10799 */ 10800 if (offs == 0) { 10801 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 10802 buf->dtb_offset = 0; 10803 woffs = total; 10804 10805 while (woffs < buf->dtb_size) 10806 tomax[woffs++] = 0; 10807 } 10808 10809 woffs = 0; 10810 break; 10811 } 10812 10813 woffs += size; 10814 } 10815 10816 /* 10817 * We have a wrapped offset. It may be that the wrapped offset 10818 * has become zero -- that's okay. 10819 */ 10820 buf->dtb_xamot_offset = woffs; 10821 } 10822 10823 out: 10824 /* 10825 * Now we can plow the buffer with any necessary padding. 10826 */ 10827 while (offs & (align - 1)) { 10828 /* 10829 * Assert that our alignment is off by a number which 10830 * is itself sizeof (uint32_t) aligned. 10831 */ 10832 ASSERT(!((align - (offs & (align - 1))) & 10833 (sizeof (uint32_t) - 1))); 10834 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10835 offs += sizeof (uint32_t); 10836 } 10837 10838 if (buf->dtb_flags & DTRACEBUF_FILL) { 10839 if (offs + needed > buf->dtb_size - state->dts_reserve) { 10840 buf->dtb_flags |= DTRACEBUF_FULL; 10841 return (-1); 10842 } 10843 } 10844 10845 if (mstate == NULL) 10846 return (offs); 10847 10848 /* 10849 * For ring buffers and fill buffers, the scratch space is always 10850 * the inactive buffer. 10851 */ 10852 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 10853 mstate->dtms_scratch_size = buf->dtb_size; 10854 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10855 10856 return (offs); 10857 } 10858 10859 static void 10860 dtrace_buffer_polish(dtrace_buffer_t *buf) 10861 { 10862 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 10863 ASSERT(MUTEX_HELD(&dtrace_lock)); 10864 10865 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 10866 return; 10867 10868 /* 10869 * We need to polish the ring buffer. There are three cases: 10870 * 10871 * - The first (and presumably most common) is that there is no gap 10872 * between the buffer offset and the wrapped offset. In this case, 10873 * there is nothing in the buffer that isn't valid data; we can 10874 * mark the buffer as polished and return. 10875 * 10876 * - The second (less common than the first but still more common 10877 * than the third) is that there is a gap between the buffer offset 10878 * and the wrapped offset, and the wrapped offset is larger than the 10879 * buffer offset. This can happen because of an alignment issue, or 10880 * can happen because of a call to dtrace_buffer_reserve() that 10881 * didn't subsequently consume the buffer space. In this case, 10882 * we need to zero the data from the buffer offset to the wrapped 10883 * offset. 10884 * 10885 * - The third (and least common) is that there is a gap between the 10886 * buffer offset and the wrapped offset, but the wrapped offset is 10887 * _less_ than the buffer offset. This can only happen because a 10888 * call to dtrace_buffer_reserve() induced a wrap, but the space 10889 * was not subsequently consumed. In this case, we need to zero the 10890 * space from the offset to the end of the buffer _and_ from the 10891 * top of the buffer to the wrapped offset. 10892 */ 10893 if (buf->dtb_offset < buf->dtb_xamot_offset) { 10894 bzero(buf->dtb_tomax + buf->dtb_offset, 10895 buf->dtb_xamot_offset - buf->dtb_offset); 10896 } 10897 10898 if (buf->dtb_offset > buf->dtb_xamot_offset) { 10899 bzero(buf->dtb_tomax + buf->dtb_offset, 10900 buf->dtb_size - buf->dtb_offset); 10901 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 10902 } 10903 } 10904 10905 /* 10906 * This routine determines if data generated at the specified time has likely 10907 * been entirely consumed at user-level. This routine is called to determine 10908 * if an ECB on a defunct probe (but for an active enabling) can be safely 10909 * disabled and destroyed. 10910 */ 10911 static int 10912 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 10913 { 10914 int i; 10915 10916 for (i = 0; i < NCPU; i++) { 10917 dtrace_buffer_t *buf = &bufs[i]; 10918 10919 if (buf->dtb_size == 0) 10920 continue; 10921 10922 if (buf->dtb_flags & DTRACEBUF_RING) 10923 return (0); 10924 10925 if (!buf->dtb_switched && buf->dtb_offset != 0) 10926 return (0); 10927 10928 if (buf->dtb_switched - buf->dtb_interval < when) 10929 return (0); 10930 } 10931 10932 return (1); 10933 } 10934 10935 static void 10936 dtrace_buffer_free(dtrace_buffer_t *bufs) 10937 { 10938 int i; 10939 10940 for (i = 0; i < NCPU; i++) { 10941 dtrace_buffer_t *buf = &bufs[i]; 10942 10943 if (buf->dtb_tomax == NULL) { 10944 ASSERT(buf->dtb_xamot == NULL); 10945 ASSERT(buf->dtb_size == 0); 10946 continue; 10947 } 10948 10949 if (buf->dtb_xamot != NULL) { 10950 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10951 kmem_free(buf->dtb_xamot, buf->dtb_size); 10952 } 10953 10954 kmem_free(buf->dtb_tomax, buf->dtb_size); 10955 buf->dtb_size = 0; 10956 buf->dtb_tomax = NULL; 10957 buf->dtb_xamot = NULL; 10958 } 10959 } 10960 10961 /* 10962 * DTrace Enabling Functions 10963 */ 10964 static dtrace_enabling_t * 10965 dtrace_enabling_create(dtrace_vstate_t *vstate) 10966 { 10967 dtrace_enabling_t *enab; 10968 10969 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 10970 enab->dten_vstate = vstate; 10971 10972 return (enab); 10973 } 10974 10975 static void 10976 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 10977 { 10978 dtrace_ecbdesc_t **ndesc; 10979 size_t osize, nsize; 10980 10981 /* 10982 * We can't add to enablings after we've enabled them, or after we've 10983 * retained them. 10984 */ 10985 ASSERT(enab->dten_probegen == 0); 10986 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 10987 10988 if (enab->dten_ndesc < enab->dten_maxdesc) { 10989 enab->dten_desc[enab->dten_ndesc++] = ecb; 10990 return; 10991 } 10992 10993 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 10994 10995 if (enab->dten_maxdesc == 0) { 10996 enab->dten_maxdesc = 1; 10997 } else { 10998 enab->dten_maxdesc <<= 1; 10999 } 11000 11001 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 11002 11003 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11004 ndesc = kmem_zalloc(nsize, KM_SLEEP); 11005 bcopy(enab->dten_desc, ndesc, osize); 11006 kmem_free(enab->dten_desc, osize); 11007 11008 enab->dten_desc = ndesc; 11009 enab->dten_desc[enab->dten_ndesc++] = ecb; 11010 } 11011 11012 static void 11013 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 11014 dtrace_probedesc_t *pd) 11015 { 11016 dtrace_ecbdesc_t *new; 11017 dtrace_predicate_t *pred; 11018 dtrace_actdesc_t *act; 11019 11020 /* 11021 * We're going to create a new ECB description that matches the 11022 * specified ECB in every way, but has the specified probe description. 11023 */ 11024 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 11025 11026 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 11027 dtrace_predicate_hold(pred); 11028 11029 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 11030 dtrace_actdesc_hold(act); 11031 11032 new->dted_action = ecb->dted_action; 11033 new->dted_pred = ecb->dted_pred; 11034 new->dted_probe = *pd; 11035 new->dted_uarg = ecb->dted_uarg; 11036 11037 dtrace_enabling_add(enab, new); 11038 } 11039 11040 static void 11041 dtrace_enabling_dump(dtrace_enabling_t *enab) 11042 { 11043 int i; 11044 11045 for (i = 0; i < enab->dten_ndesc; i++) { 11046 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 11047 11048 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 11049 desc->dtpd_provider, desc->dtpd_mod, 11050 desc->dtpd_func, desc->dtpd_name); 11051 } 11052 } 11053 11054 static void 11055 dtrace_enabling_destroy(dtrace_enabling_t *enab) 11056 { 11057 int i; 11058 dtrace_ecbdesc_t *ep; 11059 dtrace_vstate_t *vstate = enab->dten_vstate; 11060 11061 ASSERT(MUTEX_HELD(&dtrace_lock)); 11062 11063 for (i = 0; i < enab->dten_ndesc; i++) { 11064 dtrace_actdesc_t *act, *next; 11065 dtrace_predicate_t *pred; 11066 11067 ep = enab->dten_desc[i]; 11068 11069 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 11070 dtrace_predicate_release(pred, vstate); 11071 11072 for (act = ep->dted_action; act != NULL; act = next) { 11073 next = act->dtad_next; 11074 dtrace_actdesc_release(act, vstate); 11075 } 11076 11077 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 11078 } 11079 11080 kmem_free(enab->dten_desc, 11081 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 11082 11083 /* 11084 * If this was a retained enabling, decrement the dts_nretained count 11085 * and take it off of the dtrace_retained list. 11086 */ 11087 if (enab->dten_prev != NULL || enab->dten_next != NULL || 11088 dtrace_retained == enab) { 11089 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11090 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 11091 enab->dten_vstate->dtvs_state->dts_nretained--; 11092 dtrace_retained_gen++; 11093 } 11094 11095 if (enab->dten_prev == NULL) { 11096 if (dtrace_retained == enab) { 11097 dtrace_retained = enab->dten_next; 11098 11099 if (dtrace_retained != NULL) 11100 dtrace_retained->dten_prev = NULL; 11101 } 11102 } else { 11103 ASSERT(enab != dtrace_retained); 11104 ASSERT(dtrace_retained != NULL); 11105 enab->dten_prev->dten_next = enab->dten_next; 11106 } 11107 11108 if (enab->dten_next != NULL) { 11109 ASSERT(dtrace_retained != NULL); 11110 enab->dten_next->dten_prev = enab->dten_prev; 11111 } 11112 11113 kmem_free(enab, sizeof (dtrace_enabling_t)); 11114 } 11115 11116 static int 11117 dtrace_enabling_retain(dtrace_enabling_t *enab) 11118 { 11119 dtrace_state_t *state; 11120 11121 ASSERT(MUTEX_HELD(&dtrace_lock)); 11122 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11123 ASSERT(enab->dten_vstate != NULL); 11124 11125 state = enab->dten_vstate->dtvs_state; 11126 ASSERT(state != NULL); 11127 11128 /* 11129 * We only allow each state to retain dtrace_retain_max enablings. 11130 */ 11131 if (state->dts_nretained >= dtrace_retain_max) 11132 return (ENOSPC); 11133 11134 state->dts_nretained++; 11135 dtrace_retained_gen++; 11136 11137 if (dtrace_retained == NULL) { 11138 dtrace_retained = enab; 11139 return (0); 11140 } 11141 11142 enab->dten_next = dtrace_retained; 11143 dtrace_retained->dten_prev = enab; 11144 dtrace_retained = enab; 11145 11146 return (0); 11147 } 11148 11149 static int 11150 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 11151 dtrace_probedesc_t *create) 11152 { 11153 dtrace_enabling_t *new, *enab; 11154 int found = 0, err = ENOENT; 11155 11156 ASSERT(MUTEX_HELD(&dtrace_lock)); 11157 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 11158 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 11159 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 11160 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 11161 11162 new = dtrace_enabling_create(&state->dts_vstate); 11163 11164 /* 11165 * Iterate over all retained enablings, looking for enablings that 11166 * match the specified state. 11167 */ 11168 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11169 int i; 11170 11171 /* 11172 * dtvs_state can only be NULL for helper enablings -- and 11173 * helper enablings can't be retained. 11174 */ 11175 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11176 11177 if (enab->dten_vstate->dtvs_state != state) 11178 continue; 11179 11180 /* 11181 * Now iterate over each probe description; we're looking for 11182 * an exact match to the specified probe description. 11183 */ 11184 for (i = 0; i < enab->dten_ndesc; i++) { 11185 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11186 dtrace_probedesc_t *pd = &ep->dted_probe; 11187 11188 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 11189 continue; 11190 11191 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 11192 continue; 11193 11194 if (strcmp(pd->dtpd_func, match->dtpd_func)) 11195 continue; 11196 11197 if (strcmp(pd->dtpd_name, match->dtpd_name)) 11198 continue; 11199 11200 /* 11201 * We have a winning probe! Add it to our growing 11202 * enabling. 11203 */ 11204 found = 1; 11205 dtrace_enabling_addlike(new, ep, create); 11206 } 11207 } 11208 11209 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 11210 dtrace_enabling_destroy(new); 11211 return (err); 11212 } 11213 11214 return (0); 11215 } 11216 11217 static void 11218 dtrace_enabling_retract(dtrace_state_t *state) 11219 { 11220 dtrace_enabling_t *enab, *next; 11221 11222 ASSERT(MUTEX_HELD(&dtrace_lock)); 11223 11224 /* 11225 * Iterate over all retained enablings, destroy the enablings retained 11226 * for the specified state. 11227 */ 11228 for (enab = dtrace_retained; enab != NULL; enab = next) { 11229 next = enab->dten_next; 11230 11231 /* 11232 * dtvs_state can only be NULL for helper enablings -- and 11233 * helper enablings can't be retained. 11234 */ 11235 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11236 11237 if (enab->dten_vstate->dtvs_state == state) { 11238 ASSERT(state->dts_nretained > 0); 11239 dtrace_enabling_destroy(enab); 11240 } 11241 } 11242 11243 ASSERT(state->dts_nretained == 0); 11244 } 11245 11246 static int 11247 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 11248 { 11249 int i = 0; 11250 int total_matched = 0, matched = 0; 11251 11252 ASSERT(MUTEX_HELD(&cpu_lock)); 11253 ASSERT(MUTEX_HELD(&dtrace_lock)); 11254 11255 for (i = 0; i < enab->dten_ndesc; i++) { 11256 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11257 11258 enab->dten_current = ep; 11259 enab->dten_error = 0; 11260 11261 /* 11262 * If a provider failed to enable a probe then get out and 11263 * let the consumer know we failed. 11264 */ 11265 if ((matched = dtrace_probe_enable(&ep->dted_probe, enab)) < 0) 11266 return (EBUSY); 11267 11268 total_matched += matched; 11269 11270 if (enab->dten_error != 0) { 11271 /* 11272 * If we get an error half-way through enabling the 11273 * probes, we kick out -- perhaps with some number of 11274 * them enabled. Leaving enabled probes enabled may 11275 * be slightly confusing for user-level, but we expect 11276 * that no one will attempt to actually drive on in 11277 * the face of such errors. If this is an anonymous 11278 * enabling (indicated with a NULL nmatched pointer), 11279 * we cmn_err() a message. We aren't expecting to 11280 * get such an error -- such as it can exist at all, 11281 * it would be a result of corrupted DOF in the driver 11282 * properties. 11283 */ 11284 if (nmatched == NULL) { 11285 cmn_err(CE_WARN, "dtrace_enabling_match() " 11286 "error on %p: %d", (void *)ep, 11287 enab->dten_error); 11288 } 11289 11290 return (enab->dten_error); 11291 } 11292 } 11293 11294 enab->dten_probegen = dtrace_probegen; 11295 if (nmatched != NULL) 11296 *nmatched = total_matched; 11297 11298 return (0); 11299 } 11300 11301 static void 11302 dtrace_enabling_matchall(void) 11303 { 11304 dtrace_enabling_t *enab; 11305 11306 mutex_enter(&cpu_lock); 11307 mutex_enter(&dtrace_lock); 11308 11309 /* 11310 * Iterate over all retained enablings to see if any probes match 11311 * against them. We only perform this operation on enablings for which 11312 * we have sufficient permissions by virtue of being in the global zone 11313 * or in the same zone as the DTrace client. Because we can be called 11314 * after dtrace_detach() has been called, we cannot assert that there 11315 * are retained enablings. We can safely load from dtrace_retained, 11316 * however: the taskq_destroy() at the end of dtrace_detach() will 11317 * block pending our completion. 11318 */ 11319 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11320 dtrace_cred_t *dcr = &enab->dten_vstate->dtvs_state->dts_cred; 11321 cred_t *cr = dcr->dcr_cred; 11322 zoneid_t zone = cr != NULL ? crgetzoneid(cr) : 0; 11323 11324 if ((dcr->dcr_visible & DTRACE_CRV_ALLZONE) || (cr != NULL && 11325 (zone == GLOBAL_ZONEID || getzoneid() == zone))) 11326 (void) dtrace_enabling_match(enab, NULL); 11327 } 11328 11329 mutex_exit(&dtrace_lock); 11330 mutex_exit(&cpu_lock); 11331 } 11332 11333 /* 11334 * If an enabling is to be enabled without having matched probes (that is, if 11335 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 11336 * enabling must be _primed_ by creating an ECB for every ECB description. 11337 * This must be done to assure that we know the number of speculations, the 11338 * number of aggregations, the minimum buffer size needed, etc. before we 11339 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 11340 * enabling any probes, we create ECBs for every ECB decription, but with a 11341 * NULL probe -- which is exactly what this function does. 11342 */ 11343 static void 11344 dtrace_enabling_prime(dtrace_state_t *state) 11345 { 11346 dtrace_enabling_t *enab; 11347 int i; 11348 11349 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11350 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11351 11352 if (enab->dten_vstate->dtvs_state != state) 11353 continue; 11354 11355 /* 11356 * We don't want to prime an enabling more than once, lest 11357 * we allow a malicious user to induce resource exhaustion. 11358 * (The ECBs that result from priming an enabling aren't 11359 * leaked -- but they also aren't deallocated until the 11360 * consumer state is destroyed.) 11361 */ 11362 if (enab->dten_primed) 11363 continue; 11364 11365 for (i = 0; i < enab->dten_ndesc; i++) { 11366 enab->dten_current = enab->dten_desc[i]; 11367 (void) dtrace_probe_enable(NULL, enab); 11368 } 11369 11370 enab->dten_primed = 1; 11371 } 11372 } 11373 11374 /* 11375 * Called to indicate that probes should be provided due to retained 11376 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 11377 * must take an initial lap through the enabling calling the dtps_provide() 11378 * entry point explicitly to allow for autocreated probes. 11379 */ 11380 static void 11381 dtrace_enabling_provide(dtrace_provider_t *prv) 11382 { 11383 int i, all = 0; 11384 dtrace_probedesc_t desc; 11385 dtrace_genid_t gen; 11386 11387 ASSERT(MUTEX_HELD(&dtrace_lock)); 11388 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 11389 11390 if (prv == NULL) { 11391 all = 1; 11392 prv = dtrace_provider; 11393 } 11394 11395 do { 11396 dtrace_enabling_t *enab; 11397 void *parg = prv->dtpv_arg; 11398 11399 retry: 11400 gen = dtrace_retained_gen; 11401 for (enab = dtrace_retained; enab != NULL; 11402 enab = enab->dten_next) { 11403 for (i = 0; i < enab->dten_ndesc; i++) { 11404 desc = enab->dten_desc[i]->dted_probe; 11405 mutex_exit(&dtrace_lock); 11406 prv->dtpv_pops.dtps_provide(parg, &desc); 11407 mutex_enter(&dtrace_lock); 11408 /* 11409 * Process the retained enablings again if 11410 * they have changed while we weren't holding 11411 * dtrace_lock. 11412 */ 11413 if (gen != dtrace_retained_gen) 11414 goto retry; 11415 } 11416 } 11417 } while (all && (prv = prv->dtpv_next) != NULL); 11418 11419 mutex_exit(&dtrace_lock); 11420 dtrace_probe_provide(NULL, all ? NULL : prv); 11421 mutex_enter(&dtrace_lock); 11422 } 11423 11424 /* 11425 * Called to reap ECBs that are attached to probes from defunct providers. 11426 */ 11427 static void 11428 dtrace_enabling_reap(void) 11429 { 11430 dtrace_provider_t *prov; 11431 dtrace_probe_t *probe; 11432 dtrace_ecb_t *ecb; 11433 hrtime_t when; 11434 int i; 11435 11436 mutex_enter(&cpu_lock); 11437 mutex_enter(&dtrace_lock); 11438 11439 for (i = 0; i < dtrace_nprobes; i++) { 11440 if ((probe = dtrace_probes[i]) == NULL) 11441 continue; 11442 11443 if (probe->dtpr_ecb == NULL) 11444 continue; 11445 11446 prov = probe->dtpr_provider; 11447 11448 if ((when = prov->dtpv_defunct) == 0) 11449 continue; 11450 11451 /* 11452 * We have ECBs on a defunct provider: we want to reap these 11453 * ECBs to allow the provider to unregister. The destruction 11454 * of these ECBs must be done carefully: if we destroy the ECB 11455 * and the consumer later wishes to consume an EPID that 11456 * corresponds to the destroyed ECB (and if the EPID metadata 11457 * has not been previously consumed), the consumer will abort 11458 * processing on the unknown EPID. To reduce (but not, sadly, 11459 * eliminate) the possibility of this, we will only destroy an 11460 * ECB for a defunct provider if, for the state that 11461 * corresponds to the ECB: 11462 * 11463 * (a) There is no speculative tracing (which can effectively 11464 * cache an EPID for an arbitrary amount of time). 11465 * 11466 * (b) The principal buffers have been switched twice since the 11467 * provider became defunct. 11468 * 11469 * (c) The aggregation buffers are of zero size or have been 11470 * switched twice since the provider became defunct. 11471 * 11472 * We use dts_speculates to determine (a) and call a function 11473 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 11474 * that as soon as we've been unable to destroy one of the ECBs 11475 * associated with the probe, we quit trying -- reaping is only 11476 * fruitful in as much as we can destroy all ECBs associated 11477 * with the defunct provider's probes. 11478 */ 11479 while ((ecb = probe->dtpr_ecb) != NULL) { 11480 dtrace_state_t *state = ecb->dte_state; 11481 dtrace_buffer_t *buf = state->dts_buffer; 11482 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 11483 11484 if (state->dts_speculates) 11485 break; 11486 11487 if (!dtrace_buffer_consumed(buf, when)) 11488 break; 11489 11490 if (!dtrace_buffer_consumed(aggbuf, when)) 11491 break; 11492 11493 dtrace_ecb_disable(ecb); 11494 ASSERT(probe->dtpr_ecb != ecb); 11495 dtrace_ecb_destroy(ecb); 11496 } 11497 } 11498 11499 mutex_exit(&dtrace_lock); 11500 mutex_exit(&cpu_lock); 11501 } 11502 11503 /* 11504 * DTrace DOF Functions 11505 */ 11506 /*ARGSUSED*/ 11507 static void 11508 dtrace_dof_error(dof_hdr_t *dof, const char *str) 11509 { 11510 if (dtrace_err_verbose) 11511 cmn_err(CE_WARN, "failed to process DOF: %s", str); 11512 11513 #ifdef DTRACE_ERRDEBUG 11514 dtrace_errdebug(str); 11515 #endif 11516 } 11517 11518 /* 11519 * Create DOF out of a currently enabled state. Right now, we only create 11520 * DOF containing the run-time options -- but this could be expanded to create 11521 * complete DOF representing the enabled state. 11522 */ 11523 static dof_hdr_t * 11524 dtrace_dof_create(dtrace_state_t *state) 11525 { 11526 dof_hdr_t *dof; 11527 dof_sec_t *sec; 11528 dof_optdesc_t *opt; 11529 int i, len = sizeof (dof_hdr_t) + 11530 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 11531 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11532 11533 ASSERT(MUTEX_HELD(&dtrace_lock)); 11534 11535 dof = kmem_zalloc(len, KM_SLEEP); 11536 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 11537 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 11538 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 11539 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 11540 11541 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 11542 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 11543 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 11544 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 11545 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 11546 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 11547 11548 dof->dofh_flags = 0; 11549 dof->dofh_hdrsize = sizeof (dof_hdr_t); 11550 dof->dofh_secsize = sizeof (dof_sec_t); 11551 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 11552 dof->dofh_secoff = sizeof (dof_hdr_t); 11553 dof->dofh_loadsz = len; 11554 dof->dofh_filesz = len; 11555 dof->dofh_pad = 0; 11556 11557 /* 11558 * Fill in the option section header... 11559 */ 11560 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 11561 sec->dofs_type = DOF_SECT_OPTDESC; 11562 sec->dofs_align = sizeof (uint64_t); 11563 sec->dofs_flags = DOF_SECF_LOAD; 11564 sec->dofs_entsize = sizeof (dof_optdesc_t); 11565 11566 opt = (dof_optdesc_t *)((uintptr_t)sec + 11567 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 11568 11569 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 11570 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11571 11572 for (i = 0; i < DTRACEOPT_MAX; i++) { 11573 opt[i].dofo_option = i; 11574 opt[i].dofo_strtab = DOF_SECIDX_NONE; 11575 opt[i].dofo_value = state->dts_options[i]; 11576 } 11577 11578 return (dof); 11579 } 11580 11581 static dof_hdr_t * 11582 dtrace_dof_copyin(uintptr_t uarg, int *errp) 11583 { 11584 dof_hdr_t hdr, *dof; 11585 11586 ASSERT(!MUTEX_HELD(&dtrace_lock)); 11587 11588 /* 11589 * First, we're going to copyin() the sizeof (dof_hdr_t). 11590 */ 11591 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 11592 dtrace_dof_error(NULL, "failed to copyin DOF header"); 11593 *errp = EFAULT; 11594 return (NULL); 11595 } 11596 11597 /* 11598 * Now we'll allocate the entire DOF and copy it in -- provided 11599 * that the length isn't outrageous. 11600 */ 11601 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 11602 dtrace_dof_error(&hdr, "load size exceeds maximum"); 11603 *errp = E2BIG; 11604 return (NULL); 11605 } 11606 11607 if (hdr.dofh_loadsz < sizeof (hdr)) { 11608 dtrace_dof_error(&hdr, "invalid load size"); 11609 *errp = EINVAL; 11610 return (NULL); 11611 } 11612 11613 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 11614 11615 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 11616 dof->dofh_loadsz != hdr.dofh_loadsz) { 11617 kmem_free(dof, hdr.dofh_loadsz); 11618 *errp = EFAULT; 11619 return (NULL); 11620 } 11621 11622 return (dof); 11623 } 11624 11625 static dof_hdr_t * 11626 dtrace_dof_property(const char *name) 11627 { 11628 uchar_t *buf; 11629 uint64_t loadsz; 11630 unsigned int len, i; 11631 dof_hdr_t *dof; 11632 11633 /* 11634 * Unfortunately, array of values in .conf files are always (and 11635 * only) interpreted to be integer arrays. We must read our DOF 11636 * as an integer array, and then squeeze it into a byte array. 11637 */ 11638 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 11639 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 11640 return (NULL); 11641 11642 for (i = 0; i < len; i++) 11643 buf[i] = (uchar_t)(((int *)buf)[i]); 11644 11645 if (len < sizeof (dof_hdr_t)) { 11646 ddi_prop_free(buf); 11647 dtrace_dof_error(NULL, "truncated header"); 11648 return (NULL); 11649 } 11650 11651 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 11652 ddi_prop_free(buf); 11653 dtrace_dof_error(NULL, "truncated DOF"); 11654 return (NULL); 11655 } 11656 11657 if (loadsz >= dtrace_dof_maxsize) { 11658 ddi_prop_free(buf); 11659 dtrace_dof_error(NULL, "oversized DOF"); 11660 return (NULL); 11661 } 11662 11663 dof = kmem_alloc(loadsz, KM_SLEEP); 11664 bcopy(buf, dof, loadsz); 11665 ddi_prop_free(buf); 11666 11667 return (dof); 11668 } 11669 11670 static void 11671 dtrace_dof_destroy(dof_hdr_t *dof) 11672 { 11673 kmem_free(dof, dof->dofh_loadsz); 11674 } 11675 11676 /* 11677 * Return the dof_sec_t pointer corresponding to a given section index. If the 11678 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 11679 * a type other than DOF_SECT_NONE is specified, the header is checked against 11680 * this type and NULL is returned if the types do not match. 11681 */ 11682 static dof_sec_t * 11683 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 11684 { 11685 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 11686 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 11687 11688 if (i >= dof->dofh_secnum) { 11689 dtrace_dof_error(dof, "referenced section index is invalid"); 11690 return (NULL); 11691 } 11692 11693 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 11694 dtrace_dof_error(dof, "referenced section is not loadable"); 11695 return (NULL); 11696 } 11697 11698 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 11699 dtrace_dof_error(dof, "referenced section is the wrong type"); 11700 return (NULL); 11701 } 11702 11703 return (sec); 11704 } 11705 11706 static dtrace_probedesc_t * 11707 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 11708 { 11709 dof_probedesc_t *probe; 11710 dof_sec_t *strtab; 11711 uintptr_t daddr = (uintptr_t)dof; 11712 uintptr_t str; 11713 size_t size; 11714 11715 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 11716 dtrace_dof_error(dof, "invalid probe section"); 11717 return (NULL); 11718 } 11719 11720 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11721 dtrace_dof_error(dof, "bad alignment in probe description"); 11722 return (NULL); 11723 } 11724 11725 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 11726 dtrace_dof_error(dof, "truncated probe description"); 11727 return (NULL); 11728 } 11729 11730 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 11731 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 11732 11733 if (strtab == NULL) 11734 return (NULL); 11735 11736 str = daddr + strtab->dofs_offset; 11737 size = strtab->dofs_size; 11738 11739 if (probe->dofp_provider >= strtab->dofs_size) { 11740 dtrace_dof_error(dof, "corrupt probe provider"); 11741 return (NULL); 11742 } 11743 11744 (void) strncpy(desc->dtpd_provider, 11745 (char *)(str + probe->dofp_provider), 11746 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 11747 11748 if (probe->dofp_mod >= strtab->dofs_size) { 11749 dtrace_dof_error(dof, "corrupt probe module"); 11750 return (NULL); 11751 } 11752 11753 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 11754 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 11755 11756 if (probe->dofp_func >= strtab->dofs_size) { 11757 dtrace_dof_error(dof, "corrupt probe function"); 11758 return (NULL); 11759 } 11760 11761 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 11762 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 11763 11764 if (probe->dofp_name >= strtab->dofs_size) { 11765 dtrace_dof_error(dof, "corrupt probe name"); 11766 return (NULL); 11767 } 11768 11769 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 11770 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 11771 11772 return (desc); 11773 } 11774 11775 static dtrace_difo_t * 11776 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11777 cred_t *cr) 11778 { 11779 dtrace_difo_t *dp; 11780 size_t ttl = 0; 11781 dof_difohdr_t *dofd; 11782 uintptr_t daddr = (uintptr_t)dof; 11783 size_t max = dtrace_difo_maxsize; 11784 int i, l, n; 11785 11786 static const struct { 11787 int section; 11788 int bufoffs; 11789 int lenoffs; 11790 int entsize; 11791 int align; 11792 const char *msg; 11793 } difo[] = { 11794 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 11795 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 11796 sizeof (dif_instr_t), "multiple DIF sections" }, 11797 11798 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 11799 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 11800 sizeof (uint64_t), "multiple integer tables" }, 11801 11802 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 11803 offsetof(dtrace_difo_t, dtdo_strlen), 0, 11804 sizeof (char), "multiple string tables" }, 11805 11806 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 11807 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 11808 sizeof (uint_t), "multiple variable tables" }, 11809 11810 { DOF_SECT_NONE, 0, 0, 0, NULL } 11811 }; 11812 11813 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 11814 dtrace_dof_error(dof, "invalid DIFO header section"); 11815 return (NULL); 11816 } 11817 11818 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11819 dtrace_dof_error(dof, "bad alignment in DIFO header"); 11820 return (NULL); 11821 } 11822 11823 if (sec->dofs_size < sizeof (dof_difohdr_t) || 11824 sec->dofs_size % sizeof (dof_secidx_t)) { 11825 dtrace_dof_error(dof, "bad size in DIFO header"); 11826 return (NULL); 11827 } 11828 11829 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 11830 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 11831 11832 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 11833 dp->dtdo_rtype = dofd->dofd_rtype; 11834 11835 for (l = 0; l < n; l++) { 11836 dof_sec_t *subsec; 11837 void **bufp; 11838 uint32_t *lenp; 11839 11840 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 11841 dofd->dofd_links[l])) == NULL) 11842 goto err; /* invalid section link */ 11843 11844 if (ttl + subsec->dofs_size > max) { 11845 dtrace_dof_error(dof, "exceeds maximum size"); 11846 goto err; 11847 } 11848 11849 ttl += subsec->dofs_size; 11850 11851 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 11852 if (subsec->dofs_type != difo[i].section) 11853 continue; 11854 11855 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 11856 dtrace_dof_error(dof, "section not loaded"); 11857 goto err; 11858 } 11859 11860 if (subsec->dofs_align != difo[i].align) { 11861 dtrace_dof_error(dof, "bad alignment"); 11862 goto err; 11863 } 11864 11865 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 11866 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 11867 11868 if (*bufp != NULL) { 11869 dtrace_dof_error(dof, difo[i].msg); 11870 goto err; 11871 } 11872 11873 if (difo[i].entsize != subsec->dofs_entsize) { 11874 dtrace_dof_error(dof, "entry size mismatch"); 11875 goto err; 11876 } 11877 11878 if (subsec->dofs_entsize != 0 && 11879 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 11880 dtrace_dof_error(dof, "corrupt entry size"); 11881 goto err; 11882 } 11883 11884 *lenp = subsec->dofs_size; 11885 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 11886 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 11887 *bufp, subsec->dofs_size); 11888 11889 if (subsec->dofs_entsize != 0) 11890 *lenp /= subsec->dofs_entsize; 11891 11892 break; 11893 } 11894 11895 /* 11896 * If we encounter a loadable DIFO sub-section that is not 11897 * known to us, assume this is a broken program and fail. 11898 */ 11899 if (difo[i].section == DOF_SECT_NONE && 11900 (subsec->dofs_flags & DOF_SECF_LOAD)) { 11901 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 11902 goto err; 11903 } 11904 } 11905 11906 if (dp->dtdo_buf == NULL) { 11907 /* 11908 * We can't have a DIF object without DIF text. 11909 */ 11910 dtrace_dof_error(dof, "missing DIF text"); 11911 goto err; 11912 } 11913 11914 /* 11915 * Before we validate the DIF object, run through the variable table 11916 * looking for the strings -- if any of their size are under, we'll set 11917 * their size to be the system-wide default string size. Note that 11918 * this should _not_ happen if the "strsize" option has been set -- 11919 * in this case, the compiler should have set the size to reflect the 11920 * setting of the option. 11921 */ 11922 for (i = 0; i < dp->dtdo_varlen; i++) { 11923 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 11924 dtrace_diftype_t *t = &v->dtdv_type; 11925 11926 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 11927 continue; 11928 11929 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 11930 t->dtdt_size = dtrace_strsize_default; 11931 } 11932 11933 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 11934 goto err; 11935 11936 dtrace_difo_init(dp, vstate); 11937 return (dp); 11938 11939 err: 11940 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 11941 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 11942 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 11943 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 11944 11945 kmem_free(dp, sizeof (dtrace_difo_t)); 11946 return (NULL); 11947 } 11948 11949 static dtrace_predicate_t * 11950 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11951 cred_t *cr) 11952 { 11953 dtrace_difo_t *dp; 11954 11955 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 11956 return (NULL); 11957 11958 return (dtrace_predicate_create(dp)); 11959 } 11960 11961 static dtrace_actdesc_t * 11962 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11963 cred_t *cr) 11964 { 11965 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 11966 dof_actdesc_t *desc; 11967 dof_sec_t *difosec; 11968 size_t offs; 11969 uintptr_t daddr = (uintptr_t)dof; 11970 uint64_t arg; 11971 dtrace_actkind_t kind; 11972 11973 if (sec->dofs_type != DOF_SECT_ACTDESC) { 11974 dtrace_dof_error(dof, "invalid action section"); 11975 return (NULL); 11976 } 11977 11978 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 11979 dtrace_dof_error(dof, "truncated action description"); 11980 return (NULL); 11981 } 11982 11983 if (sec->dofs_align != sizeof (uint64_t)) { 11984 dtrace_dof_error(dof, "bad alignment in action description"); 11985 return (NULL); 11986 } 11987 11988 if (sec->dofs_size < sec->dofs_entsize) { 11989 dtrace_dof_error(dof, "section entry size exceeds total size"); 11990 return (NULL); 11991 } 11992 11993 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 11994 dtrace_dof_error(dof, "bad entry size in action description"); 11995 return (NULL); 11996 } 11997 11998 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 11999 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 12000 return (NULL); 12001 } 12002 12003 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 12004 desc = (dof_actdesc_t *)(daddr + 12005 (uintptr_t)sec->dofs_offset + offs); 12006 kind = (dtrace_actkind_t)desc->dofa_kind; 12007 12008 if ((DTRACEACT_ISPRINTFLIKE(kind) && 12009 (kind != DTRACEACT_PRINTA || 12010 desc->dofa_strtab != DOF_SECIDX_NONE)) || 12011 (kind == DTRACEACT_DIFEXPR && 12012 desc->dofa_strtab != DOF_SECIDX_NONE)) { 12013 dof_sec_t *strtab; 12014 char *str, *fmt; 12015 uint64_t i; 12016 12017 /* 12018 * The argument to these actions is an index into the 12019 * DOF string table. For printf()-like actions, this 12020 * is the format string. For print(), this is the 12021 * CTF type of the expression result. 12022 */ 12023 if ((strtab = dtrace_dof_sect(dof, 12024 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 12025 goto err; 12026 12027 str = (char *)((uintptr_t)dof + 12028 (uintptr_t)strtab->dofs_offset); 12029 12030 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 12031 if (str[i] == '\0') 12032 break; 12033 } 12034 12035 if (i >= strtab->dofs_size) { 12036 dtrace_dof_error(dof, "bogus format string"); 12037 goto err; 12038 } 12039 12040 if (i == desc->dofa_arg) { 12041 dtrace_dof_error(dof, "empty format string"); 12042 goto err; 12043 } 12044 12045 i -= desc->dofa_arg; 12046 fmt = kmem_alloc(i + 1, KM_SLEEP); 12047 bcopy(&str[desc->dofa_arg], fmt, i + 1); 12048 arg = (uint64_t)(uintptr_t)fmt; 12049 } else { 12050 if (kind == DTRACEACT_PRINTA) { 12051 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 12052 arg = 0; 12053 } else { 12054 arg = desc->dofa_arg; 12055 } 12056 } 12057 12058 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 12059 desc->dofa_uarg, arg); 12060 12061 if (last != NULL) { 12062 last->dtad_next = act; 12063 } else { 12064 first = act; 12065 } 12066 12067 last = act; 12068 12069 if (desc->dofa_difo == DOF_SECIDX_NONE) 12070 continue; 12071 12072 if ((difosec = dtrace_dof_sect(dof, 12073 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 12074 goto err; 12075 12076 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 12077 12078 if (act->dtad_difo == NULL) 12079 goto err; 12080 } 12081 12082 ASSERT(first != NULL); 12083 return (first); 12084 12085 err: 12086 for (act = first; act != NULL; act = next) { 12087 next = act->dtad_next; 12088 dtrace_actdesc_release(act, vstate); 12089 } 12090 12091 return (NULL); 12092 } 12093 12094 static dtrace_ecbdesc_t * 12095 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12096 cred_t *cr) 12097 { 12098 dtrace_ecbdesc_t *ep; 12099 dof_ecbdesc_t *ecb; 12100 dtrace_probedesc_t *desc; 12101 dtrace_predicate_t *pred = NULL; 12102 12103 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 12104 dtrace_dof_error(dof, "truncated ECB description"); 12105 return (NULL); 12106 } 12107 12108 if (sec->dofs_align != sizeof (uint64_t)) { 12109 dtrace_dof_error(dof, "bad alignment in ECB description"); 12110 return (NULL); 12111 } 12112 12113 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 12114 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 12115 12116 if (sec == NULL) 12117 return (NULL); 12118 12119 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12120 ep->dted_uarg = ecb->dofe_uarg; 12121 desc = &ep->dted_probe; 12122 12123 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 12124 goto err; 12125 12126 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 12127 if ((sec = dtrace_dof_sect(dof, 12128 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 12129 goto err; 12130 12131 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 12132 goto err; 12133 12134 ep->dted_pred.dtpdd_predicate = pred; 12135 } 12136 12137 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 12138 if ((sec = dtrace_dof_sect(dof, 12139 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 12140 goto err; 12141 12142 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 12143 12144 if (ep->dted_action == NULL) 12145 goto err; 12146 } 12147 12148 return (ep); 12149 12150 err: 12151 if (pred != NULL) 12152 dtrace_predicate_release(pred, vstate); 12153 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12154 return (NULL); 12155 } 12156 12157 /* 12158 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 12159 * specified DOF. At present, this amounts to simply adding 'ubase' to the 12160 * site of any user SETX relocations to account for load object base address. 12161 * In the future, if we need other relocations, this function can be extended. 12162 */ 12163 static int 12164 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 12165 { 12166 uintptr_t daddr = (uintptr_t)dof; 12167 dof_relohdr_t *dofr = 12168 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12169 dof_sec_t *ss, *rs, *ts; 12170 dof_relodesc_t *r; 12171 uint_t i, n; 12172 12173 if (sec->dofs_size < sizeof (dof_relohdr_t) || 12174 sec->dofs_align != sizeof (dof_secidx_t)) { 12175 dtrace_dof_error(dof, "invalid relocation header"); 12176 return (-1); 12177 } 12178 12179 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 12180 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 12181 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 12182 12183 if (ss == NULL || rs == NULL || ts == NULL) 12184 return (-1); /* dtrace_dof_error() has been called already */ 12185 12186 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 12187 rs->dofs_align != sizeof (uint64_t)) { 12188 dtrace_dof_error(dof, "invalid relocation section"); 12189 return (-1); 12190 } 12191 12192 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 12193 n = rs->dofs_size / rs->dofs_entsize; 12194 12195 for (i = 0; i < n; i++) { 12196 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 12197 12198 switch (r->dofr_type) { 12199 case DOF_RELO_NONE: 12200 break; 12201 case DOF_RELO_SETX: 12202 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 12203 sizeof (uint64_t) > ts->dofs_size) { 12204 dtrace_dof_error(dof, "bad relocation offset"); 12205 return (-1); 12206 } 12207 12208 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 12209 dtrace_dof_error(dof, "misaligned setx relo"); 12210 return (-1); 12211 } 12212 12213 *(uint64_t *)taddr += ubase; 12214 break; 12215 default: 12216 dtrace_dof_error(dof, "invalid relocation type"); 12217 return (-1); 12218 } 12219 12220 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 12221 } 12222 12223 return (0); 12224 } 12225 12226 /* 12227 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 12228 * header: it should be at the front of a memory region that is at least 12229 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 12230 * size. It need not be validated in any other way. 12231 */ 12232 static int 12233 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 12234 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 12235 { 12236 uint64_t len = dof->dofh_loadsz, seclen; 12237 uintptr_t daddr = (uintptr_t)dof; 12238 dtrace_ecbdesc_t *ep; 12239 dtrace_enabling_t *enab; 12240 uint_t i; 12241 12242 ASSERT(MUTEX_HELD(&dtrace_lock)); 12243 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 12244 12245 /* 12246 * Check the DOF header identification bytes. In addition to checking 12247 * valid settings, we also verify that unused bits/bytes are zeroed so 12248 * we can use them later without fear of regressing existing binaries. 12249 */ 12250 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 12251 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 12252 dtrace_dof_error(dof, "DOF magic string mismatch"); 12253 return (-1); 12254 } 12255 12256 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 12257 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 12258 dtrace_dof_error(dof, "DOF has invalid data model"); 12259 return (-1); 12260 } 12261 12262 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 12263 dtrace_dof_error(dof, "DOF encoding mismatch"); 12264 return (-1); 12265 } 12266 12267 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 12268 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 12269 dtrace_dof_error(dof, "DOF version mismatch"); 12270 return (-1); 12271 } 12272 12273 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 12274 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 12275 return (-1); 12276 } 12277 12278 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 12279 dtrace_dof_error(dof, "DOF uses too many integer registers"); 12280 return (-1); 12281 } 12282 12283 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 12284 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 12285 return (-1); 12286 } 12287 12288 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 12289 if (dof->dofh_ident[i] != 0) { 12290 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 12291 return (-1); 12292 } 12293 } 12294 12295 if (dof->dofh_flags & ~DOF_FL_VALID) { 12296 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 12297 return (-1); 12298 } 12299 12300 if (dof->dofh_secsize == 0) { 12301 dtrace_dof_error(dof, "zero section header size"); 12302 return (-1); 12303 } 12304 12305 /* 12306 * Check that the section headers don't exceed the amount of DOF 12307 * data. Note that we cast the section size and number of sections 12308 * to uint64_t's to prevent possible overflow in the multiplication. 12309 */ 12310 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 12311 12312 if (dof->dofh_secoff > len || seclen > len || 12313 dof->dofh_secoff + seclen > len) { 12314 dtrace_dof_error(dof, "truncated section headers"); 12315 return (-1); 12316 } 12317 12318 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 12319 dtrace_dof_error(dof, "misaligned section headers"); 12320 return (-1); 12321 } 12322 12323 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 12324 dtrace_dof_error(dof, "misaligned section size"); 12325 return (-1); 12326 } 12327 12328 /* 12329 * Take an initial pass through the section headers to be sure that 12330 * the headers don't have stray offsets. If the 'noprobes' flag is 12331 * set, do not permit sections relating to providers, probes, or args. 12332 */ 12333 for (i = 0; i < dof->dofh_secnum; i++) { 12334 dof_sec_t *sec = (dof_sec_t *)(daddr + 12335 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12336 12337 if (noprobes) { 12338 switch (sec->dofs_type) { 12339 case DOF_SECT_PROVIDER: 12340 case DOF_SECT_PROBES: 12341 case DOF_SECT_PRARGS: 12342 case DOF_SECT_PROFFS: 12343 dtrace_dof_error(dof, "illegal sections " 12344 "for enabling"); 12345 return (-1); 12346 } 12347 } 12348 12349 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 12350 !(sec->dofs_flags & DOF_SECF_LOAD)) { 12351 dtrace_dof_error(dof, "loadable section with load " 12352 "flag unset"); 12353 return (-1); 12354 } 12355 12356 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12357 continue; /* just ignore non-loadable sections */ 12358 12359 if (sec->dofs_align & (sec->dofs_align - 1)) { 12360 dtrace_dof_error(dof, "bad section alignment"); 12361 return (-1); 12362 } 12363 12364 if (sec->dofs_offset & (sec->dofs_align - 1)) { 12365 dtrace_dof_error(dof, "misaligned section"); 12366 return (-1); 12367 } 12368 12369 if (sec->dofs_offset > len || sec->dofs_size > len || 12370 sec->dofs_offset + sec->dofs_size > len) { 12371 dtrace_dof_error(dof, "corrupt section header"); 12372 return (-1); 12373 } 12374 12375 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 12376 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 12377 dtrace_dof_error(dof, "non-terminating string table"); 12378 return (-1); 12379 } 12380 } 12381 12382 /* 12383 * Take a second pass through the sections and locate and perform any 12384 * relocations that are present. We do this after the first pass to 12385 * be sure that all sections have had their headers validated. 12386 */ 12387 for (i = 0; i < dof->dofh_secnum; i++) { 12388 dof_sec_t *sec = (dof_sec_t *)(daddr + 12389 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12390 12391 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12392 continue; /* skip sections that are not loadable */ 12393 12394 switch (sec->dofs_type) { 12395 case DOF_SECT_URELHDR: 12396 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 12397 return (-1); 12398 break; 12399 } 12400 } 12401 12402 if ((enab = *enabp) == NULL) 12403 enab = *enabp = dtrace_enabling_create(vstate); 12404 12405 for (i = 0; i < dof->dofh_secnum; i++) { 12406 dof_sec_t *sec = (dof_sec_t *)(daddr + 12407 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12408 12409 if (sec->dofs_type != DOF_SECT_ECBDESC) 12410 continue; 12411 12412 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 12413 dtrace_enabling_destroy(enab); 12414 *enabp = NULL; 12415 return (-1); 12416 } 12417 12418 dtrace_enabling_add(enab, ep); 12419 } 12420 12421 return (0); 12422 } 12423 12424 /* 12425 * Process DOF for any options. This routine assumes that the DOF has been 12426 * at least processed by dtrace_dof_slurp(). 12427 */ 12428 static int 12429 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 12430 { 12431 int i, rval; 12432 uint32_t entsize; 12433 size_t offs; 12434 dof_optdesc_t *desc; 12435 12436 for (i = 0; i < dof->dofh_secnum; i++) { 12437 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 12438 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12439 12440 if (sec->dofs_type != DOF_SECT_OPTDESC) 12441 continue; 12442 12443 if (sec->dofs_align != sizeof (uint64_t)) { 12444 dtrace_dof_error(dof, "bad alignment in " 12445 "option description"); 12446 return (EINVAL); 12447 } 12448 12449 if ((entsize = sec->dofs_entsize) == 0) { 12450 dtrace_dof_error(dof, "zeroed option entry size"); 12451 return (EINVAL); 12452 } 12453 12454 if (entsize < sizeof (dof_optdesc_t)) { 12455 dtrace_dof_error(dof, "bad option entry size"); 12456 return (EINVAL); 12457 } 12458 12459 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 12460 desc = (dof_optdesc_t *)((uintptr_t)dof + 12461 (uintptr_t)sec->dofs_offset + offs); 12462 12463 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 12464 dtrace_dof_error(dof, "non-zero option string"); 12465 return (EINVAL); 12466 } 12467 12468 if (desc->dofo_value == DTRACEOPT_UNSET) { 12469 dtrace_dof_error(dof, "unset option"); 12470 return (EINVAL); 12471 } 12472 12473 if ((rval = dtrace_state_option(state, 12474 desc->dofo_option, desc->dofo_value)) != 0) { 12475 dtrace_dof_error(dof, "rejected option"); 12476 return (rval); 12477 } 12478 } 12479 } 12480 12481 return (0); 12482 } 12483 12484 /* 12485 * DTrace Consumer State Functions 12486 */ 12487 int 12488 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 12489 { 12490 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 12491 void *base; 12492 uintptr_t limit; 12493 dtrace_dynvar_t *dvar, *next, *start; 12494 int i; 12495 12496 ASSERT(MUTEX_HELD(&dtrace_lock)); 12497 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 12498 12499 bzero(dstate, sizeof (dtrace_dstate_t)); 12500 12501 if ((dstate->dtds_chunksize = chunksize) == 0) 12502 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 12503 12504 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 12505 size = min; 12506 12507 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12508 return (ENOMEM); 12509 12510 dstate->dtds_size = size; 12511 dstate->dtds_base = base; 12512 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 12513 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 12514 12515 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 12516 12517 if (hashsize != 1 && (hashsize & 1)) 12518 hashsize--; 12519 12520 dstate->dtds_hashsize = hashsize; 12521 dstate->dtds_hash = dstate->dtds_base; 12522 12523 /* 12524 * Set all of our hash buckets to point to the single sink, and (if 12525 * it hasn't already been set), set the sink's hash value to be the 12526 * sink sentinel value. The sink is needed for dynamic variable 12527 * lookups to know that they have iterated over an entire, valid hash 12528 * chain. 12529 */ 12530 for (i = 0; i < hashsize; i++) 12531 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 12532 12533 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 12534 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 12535 12536 /* 12537 * Determine number of active CPUs. Divide free list evenly among 12538 * active CPUs. 12539 */ 12540 start = (dtrace_dynvar_t *) 12541 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 12542 limit = (uintptr_t)base + size; 12543 12544 maxper = (limit - (uintptr_t)start) / NCPU; 12545 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 12546 12547 for (i = 0; i < NCPU; i++) { 12548 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 12549 12550 /* 12551 * If we don't even have enough chunks to make it once through 12552 * NCPUs, we're just going to allocate everything to the first 12553 * CPU. And if we're on the last CPU, we're going to allocate 12554 * whatever is left over. In either case, we set the limit to 12555 * be the limit of the dynamic variable space. 12556 */ 12557 if (maxper == 0 || i == NCPU - 1) { 12558 limit = (uintptr_t)base + size; 12559 start = NULL; 12560 } else { 12561 limit = (uintptr_t)start + maxper; 12562 start = (dtrace_dynvar_t *)limit; 12563 } 12564 12565 ASSERT(limit <= (uintptr_t)base + size); 12566 12567 for (;;) { 12568 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 12569 dstate->dtds_chunksize); 12570 12571 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 12572 break; 12573 12574 dvar->dtdv_next = next; 12575 dvar = next; 12576 } 12577 12578 if (maxper == 0) 12579 break; 12580 } 12581 12582 return (0); 12583 } 12584 12585 void 12586 dtrace_dstate_fini(dtrace_dstate_t *dstate) 12587 { 12588 ASSERT(MUTEX_HELD(&cpu_lock)); 12589 12590 if (dstate->dtds_base == NULL) 12591 return; 12592 12593 kmem_free(dstate->dtds_base, dstate->dtds_size); 12594 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 12595 } 12596 12597 static void 12598 dtrace_vstate_fini(dtrace_vstate_t *vstate) 12599 { 12600 /* 12601 * Logical XOR, where are you? 12602 */ 12603 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 12604 12605 if (vstate->dtvs_nglobals > 0) { 12606 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 12607 sizeof (dtrace_statvar_t *)); 12608 } 12609 12610 if (vstate->dtvs_ntlocals > 0) { 12611 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 12612 sizeof (dtrace_difv_t)); 12613 } 12614 12615 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 12616 12617 if (vstate->dtvs_nlocals > 0) { 12618 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 12619 sizeof (dtrace_statvar_t *)); 12620 } 12621 } 12622 12623 static void 12624 dtrace_state_clean(dtrace_state_t *state) 12625 { 12626 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 12627 return; 12628 12629 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 12630 dtrace_speculation_clean(state); 12631 } 12632 12633 static void 12634 dtrace_state_deadman(dtrace_state_t *state) 12635 { 12636 hrtime_t now; 12637 12638 dtrace_sync(); 12639 12640 now = dtrace_gethrtime(); 12641 12642 if (state != dtrace_anon.dta_state && 12643 now - state->dts_laststatus >= dtrace_deadman_user) 12644 return; 12645 12646 /* 12647 * We must be sure that dts_alive never appears to be less than the 12648 * value upon entry to dtrace_state_deadman(), and because we lack a 12649 * dtrace_cas64(), we cannot store to it atomically. We thus instead 12650 * store INT64_MAX to it, followed by a memory barrier, followed by 12651 * the new value. This assures that dts_alive never appears to be 12652 * less than its true value, regardless of the order in which the 12653 * stores to the underlying storage are issued. 12654 */ 12655 state->dts_alive = INT64_MAX; 12656 dtrace_membar_producer(); 12657 state->dts_alive = now; 12658 } 12659 12660 dtrace_state_t * 12661 dtrace_state_create(dev_t *devp, cred_t *cr) 12662 { 12663 minor_t minor; 12664 major_t major; 12665 char c[30]; 12666 dtrace_state_t *state; 12667 dtrace_optval_t *opt; 12668 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 12669 12670 ASSERT(MUTEX_HELD(&dtrace_lock)); 12671 ASSERT(MUTEX_HELD(&cpu_lock)); 12672 12673 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 12674 VM_BESTFIT | VM_SLEEP); 12675 12676 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 12677 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 12678 return (NULL); 12679 } 12680 12681 state = ddi_get_soft_state(dtrace_softstate, minor); 12682 state->dts_epid = DTRACE_EPIDNONE + 1; 12683 12684 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); 12685 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 12686 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 12687 12688 if (devp != NULL) { 12689 major = getemajor(*devp); 12690 } else { 12691 major = ddi_driver_major(dtrace_devi); 12692 } 12693 12694 state->dts_dev = makedevice(major, minor); 12695 12696 if (devp != NULL) 12697 *devp = state->dts_dev; 12698 12699 /* 12700 * We allocate NCPU buffers. On the one hand, this can be quite 12701 * a bit of memory per instance (nearly 36K on a Starcat). On the 12702 * other hand, it saves an additional memory reference in the probe 12703 * path. 12704 */ 12705 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 12706 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 12707 state->dts_cleaner = CYCLIC_NONE; 12708 state->dts_deadman = CYCLIC_NONE; 12709 state->dts_vstate.dtvs_state = state; 12710 12711 for (i = 0; i < DTRACEOPT_MAX; i++) 12712 state->dts_options[i] = DTRACEOPT_UNSET; 12713 12714 /* 12715 * Set the default options. 12716 */ 12717 opt = state->dts_options; 12718 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 12719 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 12720 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 12721 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 12722 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 12723 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 12724 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 12725 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 12726 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 12727 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 12728 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 12729 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 12730 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 12731 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 12732 12733 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 12734 12735 /* 12736 * Depending on the user credentials, we set flag bits which alter probe 12737 * visibility or the amount of destructiveness allowed. In the case of 12738 * actual anonymous tracing, or the possession of all privileges, all of 12739 * the normal checks are bypassed. 12740 */ 12741 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 12742 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 12743 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 12744 } else { 12745 /* 12746 * Set up the credentials for this instantiation. We take a 12747 * hold on the credential to prevent it from disappearing on 12748 * us; this in turn prevents the zone_t referenced by this 12749 * credential from disappearing. This means that we can 12750 * examine the credential and the zone from probe context. 12751 */ 12752 crhold(cr); 12753 state->dts_cred.dcr_cred = cr; 12754 12755 /* 12756 * CRA_PROC means "we have *some* privilege for dtrace" and 12757 * unlocks the use of variables like pid, zonename, etc. 12758 */ 12759 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 12760 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12761 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 12762 } 12763 12764 /* 12765 * dtrace_user allows use of syscall and profile providers. 12766 * If the user also has proc_owner and/or proc_zone, we 12767 * extend the scope to include additional visibility and 12768 * destructive power. 12769 */ 12770 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 12771 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 12772 state->dts_cred.dcr_visible |= 12773 DTRACE_CRV_ALLPROC; 12774 12775 state->dts_cred.dcr_action |= 12776 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12777 } 12778 12779 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 12780 state->dts_cred.dcr_visible |= 12781 DTRACE_CRV_ALLZONE; 12782 12783 state->dts_cred.dcr_action |= 12784 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12785 } 12786 12787 /* 12788 * If we have all privs in whatever zone this is, 12789 * we can do destructive things to processes which 12790 * have altered credentials. 12791 */ 12792 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12793 cr->cr_zone->zone_privset)) { 12794 state->dts_cred.dcr_action |= 12795 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12796 } 12797 } 12798 12799 /* 12800 * Holding the dtrace_kernel privilege also implies that 12801 * the user has the dtrace_user privilege from a visibility 12802 * perspective. But without further privileges, some 12803 * destructive actions are not available. 12804 */ 12805 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 12806 /* 12807 * Make all probes in all zones visible. However, 12808 * this doesn't mean that all actions become available 12809 * to all zones. 12810 */ 12811 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 12812 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 12813 12814 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 12815 DTRACE_CRA_PROC; 12816 /* 12817 * Holding proc_owner means that destructive actions 12818 * for *this* zone are allowed. 12819 */ 12820 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12821 state->dts_cred.dcr_action |= 12822 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12823 12824 /* 12825 * Holding proc_zone means that destructive actions 12826 * for this user/group ID in all zones is allowed. 12827 */ 12828 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12829 state->dts_cred.dcr_action |= 12830 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12831 12832 /* 12833 * If we have all privs in whatever zone this is, 12834 * we can do destructive things to processes which 12835 * have altered credentials. 12836 */ 12837 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12838 cr->cr_zone->zone_privset)) { 12839 state->dts_cred.dcr_action |= 12840 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12841 } 12842 } 12843 12844 /* 12845 * Holding the dtrace_proc privilege gives control over fasttrap 12846 * and pid providers. We need to grant wider destructive 12847 * privileges in the event that the user has proc_owner and/or 12848 * proc_zone. 12849 */ 12850 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12851 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12852 state->dts_cred.dcr_action |= 12853 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12854 12855 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12856 state->dts_cred.dcr_action |= 12857 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12858 } 12859 } 12860 12861 return (state); 12862 } 12863 12864 static int 12865 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 12866 { 12867 dtrace_optval_t *opt = state->dts_options, size; 12868 processorid_t cpu; 12869 int flags = 0, rval, factor, divisor = 1; 12870 12871 ASSERT(MUTEX_HELD(&dtrace_lock)); 12872 ASSERT(MUTEX_HELD(&cpu_lock)); 12873 ASSERT(which < DTRACEOPT_MAX); 12874 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 12875 (state == dtrace_anon.dta_state && 12876 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 12877 12878 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 12879 return (0); 12880 12881 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 12882 cpu = opt[DTRACEOPT_CPU]; 12883 12884 if (which == DTRACEOPT_SPECSIZE) 12885 flags |= DTRACEBUF_NOSWITCH; 12886 12887 if (which == DTRACEOPT_BUFSIZE) { 12888 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 12889 flags |= DTRACEBUF_RING; 12890 12891 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 12892 flags |= DTRACEBUF_FILL; 12893 12894 if (state != dtrace_anon.dta_state || 12895 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 12896 flags |= DTRACEBUF_INACTIVE; 12897 } 12898 12899 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 12900 /* 12901 * The size must be 8-byte aligned. If the size is not 8-byte 12902 * aligned, drop it down by the difference. 12903 */ 12904 if (size & (sizeof (uint64_t) - 1)) 12905 size -= size & (sizeof (uint64_t) - 1); 12906 12907 if (size < state->dts_reserve) { 12908 /* 12909 * Buffers always must be large enough to accommodate 12910 * their prereserved space. We return E2BIG instead 12911 * of ENOMEM in this case to allow for user-level 12912 * software to differentiate the cases. 12913 */ 12914 return (E2BIG); 12915 } 12916 12917 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 12918 12919 if (rval != ENOMEM) { 12920 opt[which] = size; 12921 return (rval); 12922 } 12923 12924 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 12925 return (rval); 12926 12927 for (divisor = 2; divisor < factor; divisor <<= 1) 12928 continue; 12929 } 12930 12931 return (ENOMEM); 12932 } 12933 12934 static int 12935 dtrace_state_buffers(dtrace_state_t *state) 12936 { 12937 dtrace_speculation_t *spec = state->dts_speculations; 12938 int rval, i; 12939 12940 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 12941 DTRACEOPT_BUFSIZE)) != 0) 12942 return (rval); 12943 12944 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 12945 DTRACEOPT_AGGSIZE)) != 0) 12946 return (rval); 12947 12948 for (i = 0; i < state->dts_nspeculations; i++) { 12949 if ((rval = dtrace_state_buffer(state, 12950 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 12951 return (rval); 12952 } 12953 12954 return (0); 12955 } 12956 12957 static void 12958 dtrace_state_prereserve(dtrace_state_t *state) 12959 { 12960 dtrace_ecb_t *ecb; 12961 dtrace_probe_t *probe; 12962 12963 state->dts_reserve = 0; 12964 12965 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 12966 return; 12967 12968 /* 12969 * If our buffer policy is a "fill" buffer policy, we need to set the 12970 * prereserved space to be the space required by the END probes. 12971 */ 12972 probe = dtrace_probes[dtrace_probeid_end - 1]; 12973 ASSERT(probe != NULL); 12974 12975 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 12976 if (ecb->dte_state != state) 12977 continue; 12978 12979 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 12980 } 12981 } 12982 12983 static int 12984 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 12985 { 12986 dtrace_optval_t *opt = state->dts_options, sz, nspec; 12987 dtrace_speculation_t *spec; 12988 dtrace_buffer_t *buf; 12989 cyc_handler_t hdlr; 12990 cyc_time_t when; 12991 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 12992 dtrace_icookie_t cookie; 12993 12994 mutex_enter(&cpu_lock); 12995 mutex_enter(&dtrace_lock); 12996 12997 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 12998 rval = EBUSY; 12999 goto out; 13000 } 13001 13002 /* 13003 * Before we can perform any checks, we must prime all of the 13004 * retained enablings that correspond to this state. 13005 */ 13006 dtrace_enabling_prime(state); 13007 13008 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 13009 rval = EACCES; 13010 goto out; 13011 } 13012 13013 dtrace_state_prereserve(state); 13014 13015 /* 13016 * Now we want to do is try to allocate our speculations. 13017 * We do not automatically resize the number of speculations; if 13018 * this fails, we will fail the operation. 13019 */ 13020 nspec = opt[DTRACEOPT_NSPEC]; 13021 ASSERT(nspec != DTRACEOPT_UNSET); 13022 13023 if (nspec > INT_MAX) { 13024 rval = ENOMEM; 13025 goto out; 13026 } 13027 13028 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 13029 KM_NOSLEEP | KM_NORMALPRI); 13030 13031 if (spec == NULL) { 13032 rval = ENOMEM; 13033 goto out; 13034 } 13035 13036 state->dts_speculations = spec; 13037 state->dts_nspeculations = (int)nspec; 13038 13039 for (i = 0; i < nspec; i++) { 13040 if ((buf = kmem_zalloc(bufsize, 13041 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 13042 rval = ENOMEM; 13043 goto err; 13044 } 13045 13046 spec[i].dtsp_buffer = buf; 13047 } 13048 13049 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 13050 if (dtrace_anon.dta_state == NULL) { 13051 rval = ENOENT; 13052 goto out; 13053 } 13054 13055 if (state->dts_necbs != 0) { 13056 rval = EALREADY; 13057 goto out; 13058 } 13059 13060 state->dts_anon = dtrace_anon_grab(); 13061 ASSERT(state->dts_anon != NULL); 13062 state = state->dts_anon; 13063 13064 /* 13065 * We want "grabanon" to be set in the grabbed state, so we'll 13066 * copy that option value from the grabbing state into the 13067 * grabbed state. 13068 */ 13069 state->dts_options[DTRACEOPT_GRABANON] = 13070 opt[DTRACEOPT_GRABANON]; 13071 13072 *cpu = dtrace_anon.dta_beganon; 13073 13074 /* 13075 * If the anonymous state is active (as it almost certainly 13076 * is if the anonymous enabling ultimately matched anything), 13077 * we don't allow any further option processing -- but we 13078 * don't return failure. 13079 */ 13080 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13081 goto out; 13082 } 13083 13084 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 13085 opt[DTRACEOPT_AGGSIZE] != 0) { 13086 if (state->dts_aggregations == NULL) { 13087 /* 13088 * We're not going to create an aggregation buffer 13089 * because we don't have any ECBs that contain 13090 * aggregations -- set this option to 0. 13091 */ 13092 opt[DTRACEOPT_AGGSIZE] = 0; 13093 } else { 13094 /* 13095 * If we have an aggregation buffer, we must also have 13096 * a buffer to use as scratch. 13097 */ 13098 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 13099 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 13100 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 13101 } 13102 } 13103 } 13104 13105 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 13106 opt[DTRACEOPT_SPECSIZE] != 0) { 13107 if (!state->dts_speculates) { 13108 /* 13109 * We're not going to create speculation buffers 13110 * because we don't have any ECBs that actually 13111 * speculate -- set the speculation size to 0. 13112 */ 13113 opt[DTRACEOPT_SPECSIZE] = 0; 13114 } 13115 } 13116 13117 /* 13118 * The bare minimum size for any buffer that we're actually going to 13119 * do anything to is sizeof (uint64_t). 13120 */ 13121 sz = sizeof (uint64_t); 13122 13123 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 13124 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 13125 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 13126 /* 13127 * A buffer size has been explicitly set to 0 (or to a size 13128 * that will be adjusted to 0) and we need the space -- we 13129 * need to return failure. We return ENOSPC to differentiate 13130 * it from failing to allocate a buffer due to failure to meet 13131 * the reserve (for which we return E2BIG). 13132 */ 13133 rval = ENOSPC; 13134 goto out; 13135 } 13136 13137 if ((rval = dtrace_state_buffers(state)) != 0) 13138 goto err; 13139 13140 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 13141 sz = dtrace_dstate_defsize; 13142 13143 do { 13144 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 13145 13146 if (rval == 0) 13147 break; 13148 13149 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13150 goto err; 13151 } while (sz >>= 1); 13152 13153 opt[DTRACEOPT_DYNVARSIZE] = sz; 13154 13155 if (rval != 0) 13156 goto err; 13157 13158 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 13159 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 13160 13161 if (opt[DTRACEOPT_CLEANRATE] == 0) 13162 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13163 13164 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 13165 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 13166 13167 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 13168 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13169 13170 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 13171 hdlr.cyh_arg = state; 13172 hdlr.cyh_level = CY_LOW_LEVEL; 13173 13174 when.cyt_when = 0; 13175 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 13176 13177 state->dts_cleaner = cyclic_add(&hdlr, &when); 13178 13179 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 13180 hdlr.cyh_arg = state; 13181 hdlr.cyh_level = CY_LOW_LEVEL; 13182 13183 when.cyt_when = 0; 13184 when.cyt_interval = dtrace_deadman_interval; 13185 13186 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 13187 state->dts_deadman = cyclic_add(&hdlr, &when); 13188 13189 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 13190 13191 if (state->dts_getf != 0 && 13192 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 13193 /* 13194 * We don't have kernel privs but we have at least one call 13195 * to getf(); we need to bump our zone's count, and (if 13196 * this is the first enabling to have an unprivileged call 13197 * to getf()) we need to hook into closef(). 13198 */ 13199 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 13200 13201 if (dtrace_getf++ == 0) { 13202 ASSERT(dtrace_closef == NULL); 13203 dtrace_closef = dtrace_getf_barrier; 13204 } 13205 } 13206 13207 /* 13208 * Now it's time to actually fire the BEGIN probe. We need to disable 13209 * interrupts here both to record the CPU on which we fired the BEGIN 13210 * probe (the data from this CPU will be processed first at user 13211 * level) and to manually activate the buffer for this CPU. 13212 */ 13213 cookie = dtrace_interrupt_disable(); 13214 *cpu = CPU->cpu_id; 13215 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 13216 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 13217 13218 dtrace_probe(dtrace_probeid_begin, 13219 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13220 dtrace_interrupt_enable(cookie); 13221 /* 13222 * We may have had an exit action from a BEGIN probe; only change our 13223 * state to ACTIVE if we're still in WARMUP. 13224 */ 13225 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 13226 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 13227 13228 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 13229 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 13230 13231 /* 13232 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 13233 * want each CPU to transition its principal buffer out of the 13234 * INACTIVE state. Doing this assures that no CPU will suddenly begin 13235 * processing an ECB halfway down a probe's ECB chain; all CPUs will 13236 * atomically transition from processing none of a state's ECBs to 13237 * processing all of them. 13238 */ 13239 dtrace_xcall(DTRACE_CPUALL, 13240 (dtrace_xcall_t)dtrace_buffer_activate, state); 13241 goto out; 13242 13243 err: 13244 dtrace_buffer_free(state->dts_buffer); 13245 dtrace_buffer_free(state->dts_aggbuffer); 13246 13247 if ((nspec = state->dts_nspeculations) == 0) { 13248 ASSERT(state->dts_speculations == NULL); 13249 goto out; 13250 } 13251 13252 spec = state->dts_speculations; 13253 ASSERT(spec != NULL); 13254 13255 for (i = 0; i < state->dts_nspeculations; i++) { 13256 if ((buf = spec[i].dtsp_buffer) == NULL) 13257 break; 13258 13259 dtrace_buffer_free(buf); 13260 kmem_free(buf, bufsize); 13261 } 13262 13263 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13264 state->dts_nspeculations = 0; 13265 state->dts_speculations = NULL; 13266 13267 out: 13268 mutex_exit(&dtrace_lock); 13269 mutex_exit(&cpu_lock); 13270 13271 return (rval); 13272 } 13273 13274 static int 13275 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 13276 { 13277 dtrace_icookie_t cookie; 13278 13279 ASSERT(MUTEX_HELD(&dtrace_lock)); 13280 13281 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 13282 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 13283 return (EINVAL); 13284 13285 /* 13286 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 13287 * to be sure that every CPU has seen it. See below for the details 13288 * on why this is done. 13289 */ 13290 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 13291 dtrace_sync(); 13292 13293 /* 13294 * By this point, it is impossible for any CPU to be still processing 13295 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 13296 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 13297 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 13298 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 13299 * iff we're in the END probe. 13300 */ 13301 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 13302 dtrace_sync(); 13303 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 13304 13305 /* 13306 * Finally, we can release the reserve and call the END probe. We 13307 * disable interrupts across calling the END probe to allow us to 13308 * return the CPU on which we actually called the END probe. This 13309 * allows user-land to be sure that this CPU's principal buffer is 13310 * processed last. 13311 */ 13312 state->dts_reserve = 0; 13313 13314 cookie = dtrace_interrupt_disable(); 13315 *cpu = CPU->cpu_id; 13316 dtrace_probe(dtrace_probeid_end, 13317 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13318 dtrace_interrupt_enable(cookie); 13319 13320 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 13321 dtrace_sync(); 13322 13323 if (state->dts_getf != 0 && 13324 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 13325 /* 13326 * We don't have kernel privs but we have at least one call 13327 * to getf(); we need to lower our zone's count, and (if 13328 * this is the last enabling to have an unprivileged call 13329 * to getf()) we need to clear the closef() hook. 13330 */ 13331 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 13332 ASSERT(dtrace_closef == dtrace_getf_barrier); 13333 ASSERT(dtrace_getf > 0); 13334 13335 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 13336 13337 if (--dtrace_getf == 0) 13338 dtrace_closef = NULL; 13339 } 13340 13341 return (0); 13342 } 13343 13344 static int 13345 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 13346 dtrace_optval_t val) 13347 { 13348 ASSERT(MUTEX_HELD(&dtrace_lock)); 13349 13350 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13351 return (EBUSY); 13352 13353 if (option >= DTRACEOPT_MAX) 13354 return (EINVAL); 13355 13356 if (option != DTRACEOPT_CPU && val < 0) 13357 return (EINVAL); 13358 13359 switch (option) { 13360 case DTRACEOPT_DESTRUCTIVE: 13361 if (dtrace_destructive_disallow) 13362 return (EACCES); 13363 13364 state->dts_cred.dcr_destructive = 1; 13365 break; 13366 13367 case DTRACEOPT_BUFSIZE: 13368 case DTRACEOPT_DYNVARSIZE: 13369 case DTRACEOPT_AGGSIZE: 13370 case DTRACEOPT_SPECSIZE: 13371 case DTRACEOPT_STRSIZE: 13372 if (val < 0) 13373 return (EINVAL); 13374 13375 if (val >= LONG_MAX) { 13376 /* 13377 * If this is an otherwise negative value, set it to 13378 * the highest multiple of 128m less than LONG_MAX. 13379 * Technically, we're adjusting the size without 13380 * regard to the buffer resizing policy, but in fact, 13381 * this has no effect -- if we set the buffer size to 13382 * ~LONG_MAX and the buffer policy is ultimately set to 13383 * be "manual", the buffer allocation is guaranteed to 13384 * fail, if only because the allocation requires two 13385 * buffers. (We set the the size to the highest 13386 * multiple of 128m because it ensures that the size 13387 * will remain a multiple of a megabyte when 13388 * repeatedly halved -- all the way down to 15m.) 13389 */ 13390 val = LONG_MAX - (1 << 27) + 1; 13391 } 13392 } 13393 13394 state->dts_options[option] = val; 13395 13396 return (0); 13397 } 13398 13399 static void 13400 dtrace_state_destroy(dtrace_state_t *state) 13401 { 13402 dtrace_ecb_t *ecb; 13403 dtrace_vstate_t *vstate = &state->dts_vstate; 13404 minor_t minor = getminor(state->dts_dev); 13405 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13406 dtrace_speculation_t *spec = state->dts_speculations; 13407 int nspec = state->dts_nspeculations; 13408 uint32_t match; 13409 13410 ASSERT(MUTEX_HELD(&dtrace_lock)); 13411 ASSERT(MUTEX_HELD(&cpu_lock)); 13412 13413 /* 13414 * First, retract any retained enablings for this state. 13415 */ 13416 dtrace_enabling_retract(state); 13417 ASSERT(state->dts_nretained == 0); 13418 13419 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 13420 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 13421 /* 13422 * We have managed to come into dtrace_state_destroy() on a 13423 * hot enabling -- almost certainly because of a disorderly 13424 * shutdown of a consumer. (That is, a consumer that is 13425 * exiting without having called dtrace_stop().) In this case, 13426 * we're going to set our activity to be KILLED, and then 13427 * issue a sync to be sure that everyone is out of probe 13428 * context before we start blowing away ECBs. 13429 */ 13430 state->dts_activity = DTRACE_ACTIVITY_KILLED; 13431 dtrace_sync(); 13432 } 13433 13434 /* 13435 * Release the credential hold we took in dtrace_state_create(). 13436 */ 13437 if (state->dts_cred.dcr_cred != NULL) 13438 crfree(state->dts_cred.dcr_cred); 13439 13440 /* 13441 * Now we can safely disable and destroy any enabled probes. Because 13442 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 13443 * (especially if they're all enabled), we take two passes through the 13444 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 13445 * in the second we disable whatever is left over. 13446 */ 13447 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 13448 for (i = 0; i < state->dts_necbs; i++) { 13449 if ((ecb = state->dts_ecbs[i]) == NULL) 13450 continue; 13451 13452 if (match && ecb->dte_probe != NULL) { 13453 dtrace_probe_t *probe = ecb->dte_probe; 13454 dtrace_provider_t *prov = probe->dtpr_provider; 13455 13456 if (!(prov->dtpv_priv.dtpp_flags & match)) 13457 continue; 13458 } 13459 13460 dtrace_ecb_disable(ecb); 13461 dtrace_ecb_destroy(ecb); 13462 } 13463 13464 if (!match) 13465 break; 13466 } 13467 13468 /* 13469 * Before we free the buffers, perform one more sync to assure that 13470 * every CPU is out of probe context. 13471 */ 13472 dtrace_sync(); 13473 13474 dtrace_buffer_free(state->dts_buffer); 13475 dtrace_buffer_free(state->dts_aggbuffer); 13476 13477 for (i = 0; i < nspec; i++) 13478 dtrace_buffer_free(spec[i].dtsp_buffer); 13479 13480 if (state->dts_cleaner != CYCLIC_NONE) 13481 cyclic_remove(state->dts_cleaner); 13482 13483 if (state->dts_deadman != CYCLIC_NONE) 13484 cyclic_remove(state->dts_deadman); 13485 13486 dtrace_dstate_fini(&vstate->dtvs_dynvars); 13487 dtrace_vstate_fini(vstate); 13488 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 13489 13490 if (state->dts_aggregations != NULL) { 13491 #ifdef DEBUG 13492 for (i = 0; i < state->dts_naggregations; i++) 13493 ASSERT(state->dts_aggregations[i] == NULL); 13494 #endif 13495 ASSERT(state->dts_naggregations > 0); 13496 kmem_free(state->dts_aggregations, 13497 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 13498 } 13499 13500 kmem_free(state->dts_buffer, bufsize); 13501 kmem_free(state->dts_aggbuffer, bufsize); 13502 13503 for (i = 0; i < nspec; i++) 13504 kmem_free(spec[i].dtsp_buffer, bufsize); 13505 13506 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13507 13508 dtrace_format_destroy(state); 13509 13510 vmem_destroy(state->dts_aggid_arena); 13511 ddi_soft_state_free(dtrace_softstate, minor); 13512 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 13513 } 13514 13515 /* 13516 * DTrace Anonymous Enabling Functions 13517 */ 13518 static dtrace_state_t * 13519 dtrace_anon_grab(void) 13520 { 13521 dtrace_state_t *state; 13522 13523 ASSERT(MUTEX_HELD(&dtrace_lock)); 13524 13525 if ((state = dtrace_anon.dta_state) == NULL) { 13526 ASSERT(dtrace_anon.dta_enabling == NULL); 13527 return (NULL); 13528 } 13529 13530 ASSERT(dtrace_anon.dta_enabling != NULL); 13531 ASSERT(dtrace_retained != NULL); 13532 13533 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 13534 dtrace_anon.dta_enabling = NULL; 13535 dtrace_anon.dta_state = NULL; 13536 13537 return (state); 13538 } 13539 13540 static void 13541 dtrace_anon_property(void) 13542 { 13543 int i, rv; 13544 dtrace_state_t *state; 13545 dof_hdr_t *dof; 13546 char c[32]; /* enough for "dof-data-" + digits */ 13547 13548 ASSERT(MUTEX_HELD(&dtrace_lock)); 13549 ASSERT(MUTEX_HELD(&cpu_lock)); 13550 13551 for (i = 0; ; i++) { 13552 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 13553 13554 dtrace_err_verbose = 1; 13555 13556 if ((dof = dtrace_dof_property(c)) == NULL) { 13557 dtrace_err_verbose = 0; 13558 break; 13559 } 13560 13561 /* 13562 * We want to create anonymous state, so we need to transition 13563 * the kernel debugger to indicate that DTrace is active. If 13564 * this fails (e.g. because the debugger has modified text in 13565 * some way), we won't continue with the processing. 13566 */ 13567 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13568 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 13569 "enabling ignored."); 13570 dtrace_dof_destroy(dof); 13571 break; 13572 } 13573 13574 /* 13575 * If we haven't allocated an anonymous state, we'll do so now. 13576 */ 13577 if ((state = dtrace_anon.dta_state) == NULL) { 13578 state = dtrace_state_create(NULL, NULL); 13579 dtrace_anon.dta_state = state; 13580 13581 if (state == NULL) { 13582 /* 13583 * This basically shouldn't happen: the only 13584 * failure mode from dtrace_state_create() is a 13585 * failure of ddi_soft_state_zalloc() that 13586 * itself should never happen. Still, the 13587 * interface allows for a failure mode, and 13588 * we want to fail as gracefully as possible: 13589 * we'll emit an error message and cease 13590 * processing anonymous state in this case. 13591 */ 13592 cmn_err(CE_WARN, "failed to create " 13593 "anonymous state"); 13594 dtrace_dof_destroy(dof); 13595 break; 13596 } 13597 } 13598 13599 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 13600 &dtrace_anon.dta_enabling, 0, B_TRUE); 13601 13602 if (rv == 0) 13603 rv = dtrace_dof_options(dof, state); 13604 13605 dtrace_err_verbose = 0; 13606 dtrace_dof_destroy(dof); 13607 13608 if (rv != 0) { 13609 /* 13610 * This is malformed DOF; chuck any anonymous state 13611 * that we created. 13612 */ 13613 ASSERT(dtrace_anon.dta_enabling == NULL); 13614 dtrace_state_destroy(state); 13615 dtrace_anon.dta_state = NULL; 13616 break; 13617 } 13618 13619 ASSERT(dtrace_anon.dta_enabling != NULL); 13620 } 13621 13622 if (dtrace_anon.dta_enabling != NULL) { 13623 int rval; 13624 13625 /* 13626 * dtrace_enabling_retain() can only fail because we are 13627 * trying to retain more enablings than are allowed -- but 13628 * we only have one anonymous enabling, and we are guaranteed 13629 * to be allowed at least one retained enabling; we assert 13630 * that dtrace_enabling_retain() returns success. 13631 */ 13632 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 13633 ASSERT(rval == 0); 13634 13635 dtrace_enabling_dump(dtrace_anon.dta_enabling); 13636 } 13637 } 13638 13639 /* 13640 * DTrace Helper Functions 13641 */ 13642 static void 13643 dtrace_helper_trace(dtrace_helper_action_t *helper, 13644 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 13645 { 13646 uint32_t size, next, nnext, i; 13647 dtrace_helptrace_t *ent; 13648 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 13649 13650 if (!dtrace_helptrace_enabled) 13651 return; 13652 13653 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 13654 13655 /* 13656 * What would a tracing framework be without its own tracing 13657 * framework? (Well, a hell of a lot simpler, for starters...) 13658 */ 13659 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 13660 sizeof (uint64_t) - sizeof (uint64_t); 13661 13662 /* 13663 * Iterate until we can allocate a slot in the trace buffer. 13664 */ 13665 do { 13666 next = dtrace_helptrace_next; 13667 13668 if (next + size < dtrace_helptrace_bufsize) { 13669 nnext = next + size; 13670 } else { 13671 nnext = size; 13672 } 13673 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 13674 13675 /* 13676 * We have our slot; fill it in. 13677 */ 13678 if (nnext == size) 13679 next = 0; 13680 13681 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 13682 ent->dtht_helper = helper; 13683 ent->dtht_where = where; 13684 ent->dtht_nlocals = vstate->dtvs_nlocals; 13685 13686 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 13687 mstate->dtms_fltoffs : -1; 13688 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 13689 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 13690 13691 for (i = 0; i < vstate->dtvs_nlocals; i++) { 13692 dtrace_statvar_t *svar; 13693 13694 if ((svar = vstate->dtvs_locals[i]) == NULL) 13695 continue; 13696 13697 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 13698 ent->dtht_locals[i] = 13699 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; 13700 } 13701 } 13702 13703 static uint64_t 13704 dtrace_helper(int which, dtrace_mstate_t *mstate, 13705 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 13706 { 13707 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 13708 uint64_t sarg0 = mstate->dtms_arg[0]; 13709 uint64_t sarg1 = mstate->dtms_arg[1]; 13710 uint64_t rval; 13711 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 13712 dtrace_helper_action_t *helper; 13713 dtrace_vstate_t *vstate; 13714 dtrace_difo_t *pred; 13715 int i, trace = dtrace_helptrace_enabled; 13716 13717 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 13718 13719 if (helpers == NULL) 13720 return (0); 13721 13722 if ((helper = helpers->dthps_actions[which]) == NULL) 13723 return (0); 13724 13725 vstate = &helpers->dthps_vstate; 13726 mstate->dtms_arg[0] = arg0; 13727 mstate->dtms_arg[1] = arg1; 13728 13729 /* 13730 * Now iterate over each helper. If its predicate evaluates to 'true', 13731 * we'll call the corresponding actions. Note that the below calls 13732 * to dtrace_dif_emulate() may set faults in machine state. This is 13733 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 13734 * the stored DIF offset with its own (which is the desired behavior). 13735 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 13736 * from machine state; this is okay, too. 13737 */ 13738 for (; helper != NULL; helper = helper->dtha_next) { 13739 if ((pred = helper->dtha_predicate) != NULL) { 13740 if (trace) 13741 dtrace_helper_trace(helper, mstate, vstate, 0); 13742 13743 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 13744 goto next; 13745 13746 if (*flags & CPU_DTRACE_FAULT) 13747 goto err; 13748 } 13749 13750 for (i = 0; i < helper->dtha_nactions; i++) { 13751 if (trace) 13752 dtrace_helper_trace(helper, 13753 mstate, vstate, i + 1); 13754 13755 rval = dtrace_dif_emulate(helper->dtha_actions[i], 13756 mstate, vstate, state); 13757 13758 if (*flags & CPU_DTRACE_FAULT) 13759 goto err; 13760 } 13761 13762 next: 13763 if (trace) 13764 dtrace_helper_trace(helper, mstate, vstate, 13765 DTRACE_HELPTRACE_NEXT); 13766 } 13767 13768 if (trace) 13769 dtrace_helper_trace(helper, mstate, vstate, 13770 DTRACE_HELPTRACE_DONE); 13771 13772 /* 13773 * Restore the arg0 that we saved upon entry. 13774 */ 13775 mstate->dtms_arg[0] = sarg0; 13776 mstate->dtms_arg[1] = sarg1; 13777 13778 return (rval); 13779 13780 err: 13781 if (trace) 13782 dtrace_helper_trace(helper, mstate, vstate, 13783 DTRACE_HELPTRACE_ERR); 13784 13785 /* 13786 * Restore the arg0 that we saved upon entry. 13787 */ 13788 mstate->dtms_arg[0] = sarg0; 13789 mstate->dtms_arg[1] = sarg1; 13790 13791 return (NULL); 13792 } 13793 13794 static void 13795 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 13796 dtrace_vstate_t *vstate) 13797 { 13798 int i; 13799 13800 if (helper->dtha_predicate != NULL) 13801 dtrace_difo_release(helper->dtha_predicate, vstate); 13802 13803 for (i = 0; i < helper->dtha_nactions; i++) { 13804 ASSERT(helper->dtha_actions[i] != NULL); 13805 dtrace_difo_release(helper->dtha_actions[i], vstate); 13806 } 13807 13808 kmem_free(helper->dtha_actions, 13809 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 13810 kmem_free(helper, sizeof (dtrace_helper_action_t)); 13811 } 13812 13813 static int 13814 dtrace_helper_destroygen(int gen) 13815 { 13816 proc_t *p = curproc; 13817 dtrace_helpers_t *help = p->p_dtrace_helpers; 13818 dtrace_vstate_t *vstate; 13819 int i; 13820 13821 ASSERT(MUTEX_HELD(&dtrace_lock)); 13822 13823 if (help == NULL || gen > help->dthps_generation) 13824 return (EINVAL); 13825 13826 vstate = &help->dthps_vstate; 13827 13828 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 13829 dtrace_helper_action_t *last = NULL, *h, *next; 13830 13831 for (h = help->dthps_actions[i]; h != NULL; h = next) { 13832 next = h->dtha_next; 13833 13834 if (h->dtha_generation == gen) { 13835 if (last != NULL) { 13836 last->dtha_next = next; 13837 } else { 13838 help->dthps_actions[i] = next; 13839 } 13840 13841 dtrace_helper_action_destroy(h, vstate); 13842 } else { 13843 last = h; 13844 } 13845 } 13846 } 13847 13848 /* 13849 * Interate until we've cleared out all helper providers with the 13850 * given generation number. 13851 */ 13852 for (;;) { 13853 dtrace_helper_provider_t *prov; 13854 13855 /* 13856 * Look for a helper provider with the right generation. We 13857 * have to start back at the beginning of the list each time 13858 * because we drop dtrace_lock. It's unlikely that we'll make 13859 * more than two passes. 13860 */ 13861 for (i = 0; i < help->dthps_nprovs; i++) { 13862 prov = help->dthps_provs[i]; 13863 13864 if (prov->dthp_generation == gen) 13865 break; 13866 } 13867 13868 /* 13869 * If there were no matches, we're done. 13870 */ 13871 if (i == help->dthps_nprovs) 13872 break; 13873 13874 /* 13875 * Move the last helper provider into this slot. 13876 */ 13877 help->dthps_nprovs--; 13878 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 13879 help->dthps_provs[help->dthps_nprovs] = NULL; 13880 13881 mutex_exit(&dtrace_lock); 13882 13883 /* 13884 * If we have a meta provider, remove this helper provider. 13885 */ 13886 mutex_enter(&dtrace_meta_lock); 13887 if (dtrace_meta_pid != NULL) { 13888 ASSERT(dtrace_deferred_pid == NULL); 13889 dtrace_helper_provider_remove(&prov->dthp_prov, 13890 p->p_pid); 13891 } 13892 mutex_exit(&dtrace_meta_lock); 13893 13894 dtrace_helper_provider_destroy(prov); 13895 13896 mutex_enter(&dtrace_lock); 13897 } 13898 13899 return (0); 13900 } 13901 13902 static int 13903 dtrace_helper_validate(dtrace_helper_action_t *helper) 13904 { 13905 int err = 0, i; 13906 dtrace_difo_t *dp; 13907 13908 if ((dp = helper->dtha_predicate) != NULL) 13909 err += dtrace_difo_validate_helper(dp); 13910 13911 for (i = 0; i < helper->dtha_nactions; i++) 13912 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 13913 13914 return (err == 0); 13915 } 13916 13917 static int 13918 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 13919 { 13920 dtrace_helpers_t *help; 13921 dtrace_helper_action_t *helper, *last; 13922 dtrace_actdesc_t *act; 13923 dtrace_vstate_t *vstate; 13924 dtrace_predicate_t *pred; 13925 int count = 0, nactions = 0, i; 13926 13927 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 13928 return (EINVAL); 13929 13930 help = curproc->p_dtrace_helpers; 13931 last = help->dthps_actions[which]; 13932 vstate = &help->dthps_vstate; 13933 13934 for (count = 0; last != NULL; last = last->dtha_next) { 13935 count++; 13936 if (last->dtha_next == NULL) 13937 break; 13938 } 13939 13940 /* 13941 * If we already have dtrace_helper_actions_max helper actions for this 13942 * helper action type, we'll refuse to add a new one. 13943 */ 13944 if (count >= dtrace_helper_actions_max) 13945 return (ENOSPC); 13946 13947 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 13948 helper->dtha_generation = help->dthps_generation; 13949 13950 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 13951 ASSERT(pred->dtp_difo != NULL); 13952 dtrace_difo_hold(pred->dtp_difo); 13953 helper->dtha_predicate = pred->dtp_difo; 13954 } 13955 13956 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 13957 if (act->dtad_kind != DTRACEACT_DIFEXPR) 13958 goto err; 13959 13960 if (act->dtad_difo == NULL) 13961 goto err; 13962 13963 nactions++; 13964 } 13965 13966 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 13967 (helper->dtha_nactions = nactions), KM_SLEEP); 13968 13969 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 13970 dtrace_difo_hold(act->dtad_difo); 13971 helper->dtha_actions[i++] = act->dtad_difo; 13972 } 13973 13974 if (!dtrace_helper_validate(helper)) 13975 goto err; 13976 13977 if (last == NULL) { 13978 help->dthps_actions[which] = helper; 13979 } else { 13980 last->dtha_next = helper; 13981 } 13982 13983 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 13984 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 13985 dtrace_helptrace_next = 0; 13986 } 13987 13988 return (0); 13989 err: 13990 dtrace_helper_action_destroy(helper, vstate); 13991 return (EINVAL); 13992 } 13993 13994 static void 13995 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 13996 dof_helper_t *dofhp) 13997 { 13998 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 13999 14000 mutex_enter(&dtrace_meta_lock); 14001 mutex_enter(&dtrace_lock); 14002 14003 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 14004 /* 14005 * If the dtrace module is loaded but not attached, or if 14006 * there aren't isn't a meta provider registered to deal with 14007 * these provider descriptions, we need to postpone creating 14008 * the actual providers until later. 14009 */ 14010 14011 if (help->dthps_next == NULL && help->dthps_prev == NULL && 14012 dtrace_deferred_pid != help) { 14013 help->dthps_deferred = 1; 14014 help->dthps_pid = p->p_pid; 14015 help->dthps_next = dtrace_deferred_pid; 14016 help->dthps_prev = NULL; 14017 if (dtrace_deferred_pid != NULL) 14018 dtrace_deferred_pid->dthps_prev = help; 14019 dtrace_deferred_pid = help; 14020 } 14021 14022 mutex_exit(&dtrace_lock); 14023 14024 } else if (dofhp != NULL) { 14025 /* 14026 * If the dtrace module is loaded and we have a particular 14027 * helper provider description, pass that off to the 14028 * meta provider. 14029 */ 14030 14031 mutex_exit(&dtrace_lock); 14032 14033 dtrace_helper_provide(dofhp, p->p_pid); 14034 14035 } else { 14036 /* 14037 * Otherwise, just pass all the helper provider descriptions 14038 * off to the meta provider. 14039 */ 14040 14041 int i; 14042 mutex_exit(&dtrace_lock); 14043 14044 for (i = 0; i < help->dthps_nprovs; i++) { 14045 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 14046 p->p_pid); 14047 } 14048 } 14049 14050 mutex_exit(&dtrace_meta_lock); 14051 } 14052 14053 static int 14054 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 14055 { 14056 dtrace_helpers_t *help; 14057 dtrace_helper_provider_t *hprov, **tmp_provs; 14058 uint_t tmp_maxprovs, i; 14059 14060 ASSERT(MUTEX_HELD(&dtrace_lock)); 14061 14062 help = curproc->p_dtrace_helpers; 14063 ASSERT(help != NULL); 14064 14065 /* 14066 * If we already have dtrace_helper_providers_max helper providers, 14067 * we're refuse to add a new one. 14068 */ 14069 if (help->dthps_nprovs >= dtrace_helper_providers_max) 14070 return (ENOSPC); 14071 14072 /* 14073 * Check to make sure this isn't a duplicate. 14074 */ 14075 for (i = 0; i < help->dthps_nprovs; i++) { 14076 if (dofhp->dofhp_addr == 14077 help->dthps_provs[i]->dthp_prov.dofhp_addr) 14078 return (EALREADY); 14079 } 14080 14081 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 14082 hprov->dthp_prov = *dofhp; 14083 hprov->dthp_ref = 1; 14084 hprov->dthp_generation = gen; 14085 14086 /* 14087 * Allocate a bigger table for helper providers if it's already full. 14088 */ 14089 if (help->dthps_maxprovs == help->dthps_nprovs) { 14090 tmp_maxprovs = help->dthps_maxprovs; 14091 tmp_provs = help->dthps_provs; 14092 14093 if (help->dthps_maxprovs == 0) 14094 help->dthps_maxprovs = 2; 14095 else 14096 help->dthps_maxprovs *= 2; 14097 if (help->dthps_maxprovs > dtrace_helper_providers_max) 14098 help->dthps_maxprovs = dtrace_helper_providers_max; 14099 14100 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 14101 14102 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 14103 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14104 14105 if (tmp_provs != NULL) { 14106 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 14107 sizeof (dtrace_helper_provider_t *)); 14108 kmem_free(tmp_provs, tmp_maxprovs * 14109 sizeof (dtrace_helper_provider_t *)); 14110 } 14111 } 14112 14113 help->dthps_provs[help->dthps_nprovs] = hprov; 14114 help->dthps_nprovs++; 14115 14116 return (0); 14117 } 14118 14119 static void 14120 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 14121 { 14122 mutex_enter(&dtrace_lock); 14123 14124 if (--hprov->dthp_ref == 0) { 14125 dof_hdr_t *dof; 14126 mutex_exit(&dtrace_lock); 14127 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 14128 dtrace_dof_destroy(dof); 14129 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 14130 } else { 14131 mutex_exit(&dtrace_lock); 14132 } 14133 } 14134 14135 static int 14136 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 14137 { 14138 uintptr_t daddr = (uintptr_t)dof; 14139 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 14140 dof_provider_t *provider; 14141 dof_probe_t *probe; 14142 uint8_t *arg; 14143 char *strtab, *typestr; 14144 dof_stridx_t typeidx; 14145 size_t typesz; 14146 uint_t nprobes, j, k; 14147 14148 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 14149 14150 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 14151 dtrace_dof_error(dof, "misaligned section offset"); 14152 return (-1); 14153 } 14154 14155 /* 14156 * The section needs to be large enough to contain the DOF provider 14157 * structure appropriate for the given version. 14158 */ 14159 if (sec->dofs_size < 14160 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 14161 offsetof(dof_provider_t, dofpv_prenoffs) : 14162 sizeof (dof_provider_t))) { 14163 dtrace_dof_error(dof, "provider section too small"); 14164 return (-1); 14165 } 14166 14167 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 14168 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 14169 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 14170 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 14171 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 14172 14173 if (str_sec == NULL || prb_sec == NULL || 14174 arg_sec == NULL || off_sec == NULL) 14175 return (-1); 14176 14177 enoff_sec = NULL; 14178 14179 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14180 provider->dofpv_prenoffs != DOF_SECT_NONE && 14181 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 14182 provider->dofpv_prenoffs)) == NULL) 14183 return (-1); 14184 14185 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 14186 14187 if (provider->dofpv_name >= str_sec->dofs_size || 14188 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 14189 dtrace_dof_error(dof, "invalid provider name"); 14190 return (-1); 14191 } 14192 14193 if (prb_sec->dofs_entsize == 0 || 14194 prb_sec->dofs_entsize > prb_sec->dofs_size) { 14195 dtrace_dof_error(dof, "invalid entry size"); 14196 return (-1); 14197 } 14198 14199 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 14200 dtrace_dof_error(dof, "misaligned entry size"); 14201 return (-1); 14202 } 14203 14204 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 14205 dtrace_dof_error(dof, "invalid entry size"); 14206 return (-1); 14207 } 14208 14209 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 14210 dtrace_dof_error(dof, "misaligned section offset"); 14211 return (-1); 14212 } 14213 14214 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 14215 dtrace_dof_error(dof, "invalid entry size"); 14216 return (-1); 14217 } 14218 14219 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 14220 14221 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 14222 14223 /* 14224 * Take a pass through the probes to check for errors. 14225 */ 14226 for (j = 0; j < nprobes; j++) { 14227 probe = (dof_probe_t *)(uintptr_t)(daddr + 14228 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 14229 14230 if (probe->dofpr_func >= str_sec->dofs_size) { 14231 dtrace_dof_error(dof, "invalid function name"); 14232 return (-1); 14233 } 14234 14235 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 14236 dtrace_dof_error(dof, "function name too long"); 14237 return (-1); 14238 } 14239 14240 if (probe->dofpr_name >= str_sec->dofs_size || 14241 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 14242 dtrace_dof_error(dof, "invalid probe name"); 14243 return (-1); 14244 } 14245 14246 /* 14247 * The offset count must not wrap the index, and the offsets 14248 * must also not overflow the section's data. 14249 */ 14250 if (probe->dofpr_offidx + probe->dofpr_noffs < 14251 probe->dofpr_offidx || 14252 (probe->dofpr_offidx + probe->dofpr_noffs) * 14253 off_sec->dofs_entsize > off_sec->dofs_size) { 14254 dtrace_dof_error(dof, "invalid probe offset"); 14255 return (-1); 14256 } 14257 14258 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 14259 /* 14260 * If there's no is-enabled offset section, make sure 14261 * there aren't any is-enabled offsets. Otherwise 14262 * perform the same checks as for probe offsets 14263 * (immediately above). 14264 */ 14265 if (enoff_sec == NULL) { 14266 if (probe->dofpr_enoffidx != 0 || 14267 probe->dofpr_nenoffs != 0) { 14268 dtrace_dof_error(dof, "is-enabled " 14269 "offsets with null section"); 14270 return (-1); 14271 } 14272 } else if (probe->dofpr_enoffidx + 14273 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 14274 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 14275 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 14276 dtrace_dof_error(dof, "invalid is-enabled " 14277 "offset"); 14278 return (-1); 14279 } 14280 14281 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 14282 dtrace_dof_error(dof, "zero probe and " 14283 "is-enabled offsets"); 14284 return (-1); 14285 } 14286 } else if (probe->dofpr_noffs == 0) { 14287 dtrace_dof_error(dof, "zero probe offsets"); 14288 return (-1); 14289 } 14290 14291 if (probe->dofpr_argidx + probe->dofpr_xargc < 14292 probe->dofpr_argidx || 14293 (probe->dofpr_argidx + probe->dofpr_xargc) * 14294 arg_sec->dofs_entsize > arg_sec->dofs_size) { 14295 dtrace_dof_error(dof, "invalid args"); 14296 return (-1); 14297 } 14298 14299 typeidx = probe->dofpr_nargv; 14300 typestr = strtab + probe->dofpr_nargv; 14301 for (k = 0; k < probe->dofpr_nargc; k++) { 14302 if (typeidx >= str_sec->dofs_size) { 14303 dtrace_dof_error(dof, "bad " 14304 "native argument type"); 14305 return (-1); 14306 } 14307 14308 typesz = strlen(typestr) + 1; 14309 if (typesz > DTRACE_ARGTYPELEN) { 14310 dtrace_dof_error(dof, "native " 14311 "argument type too long"); 14312 return (-1); 14313 } 14314 typeidx += typesz; 14315 typestr += typesz; 14316 } 14317 14318 typeidx = probe->dofpr_xargv; 14319 typestr = strtab + probe->dofpr_xargv; 14320 for (k = 0; k < probe->dofpr_xargc; k++) { 14321 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 14322 dtrace_dof_error(dof, "bad " 14323 "native argument index"); 14324 return (-1); 14325 } 14326 14327 if (typeidx >= str_sec->dofs_size) { 14328 dtrace_dof_error(dof, "bad " 14329 "translated argument type"); 14330 return (-1); 14331 } 14332 14333 typesz = strlen(typestr) + 1; 14334 if (typesz > DTRACE_ARGTYPELEN) { 14335 dtrace_dof_error(dof, "translated argument " 14336 "type too long"); 14337 return (-1); 14338 } 14339 14340 typeidx += typesz; 14341 typestr += typesz; 14342 } 14343 } 14344 14345 return (0); 14346 } 14347 14348 static int 14349 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 14350 { 14351 dtrace_helpers_t *help; 14352 dtrace_vstate_t *vstate; 14353 dtrace_enabling_t *enab = NULL; 14354 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 14355 uintptr_t daddr = (uintptr_t)dof; 14356 14357 ASSERT(MUTEX_HELD(&dtrace_lock)); 14358 14359 if ((help = curproc->p_dtrace_helpers) == NULL) 14360 help = dtrace_helpers_create(curproc); 14361 14362 vstate = &help->dthps_vstate; 14363 14364 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 14365 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 14366 dtrace_dof_destroy(dof); 14367 return (rv); 14368 } 14369 14370 /* 14371 * Look for helper providers and validate their descriptions. 14372 */ 14373 if (dhp != NULL) { 14374 for (i = 0; i < dof->dofh_secnum; i++) { 14375 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 14376 dof->dofh_secoff + i * dof->dofh_secsize); 14377 14378 if (sec->dofs_type != DOF_SECT_PROVIDER) 14379 continue; 14380 14381 if (dtrace_helper_provider_validate(dof, sec) != 0) { 14382 dtrace_enabling_destroy(enab); 14383 dtrace_dof_destroy(dof); 14384 return (-1); 14385 } 14386 14387 nprovs++; 14388 } 14389 } 14390 14391 /* 14392 * Now we need to walk through the ECB descriptions in the enabling. 14393 */ 14394 for (i = 0; i < enab->dten_ndesc; i++) { 14395 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 14396 dtrace_probedesc_t *desc = &ep->dted_probe; 14397 14398 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 14399 continue; 14400 14401 if (strcmp(desc->dtpd_mod, "helper") != 0) 14402 continue; 14403 14404 if (strcmp(desc->dtpd_func, "ustack") != 0) 14405 continue; 14406 14407 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 14408 ep)) != 0) { 14409 /* 14410 * Adding this helper action failed -- we are now going 14411 * to rip out the entire generation and return failure. 14412 */ 14413 (void) dtrace_helper_destroygen(help->dthps_generation); 14414 dtrace_enabling_destroy(enab); 14415 dtrace_dof_destroy(dof); 14416 return (-1); 14417 } 14418 14419 nhelpers++; 14420 } 14421 14422 if (nhelpers < enab->dten_ndesc) 14423 dtrace_dof_error(dof, "unmatched helpers"); 14424 14425 gen = help->dthps_generation++; 14426 dtrace_enabling_destroy(enab); 14427 14428 if (dhp != NULL && nprovs > 0) { 14429 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 14430 if (dtrace_helper_provider_add(dhp, gen) == 0) { 14431 mutex_exit(&dtrace_lock); 14432 dtrace_helper_provider_register(curproc, help, dhp); 14433 mutex_enter(&dtrace_lock); 14434 14435 destroy = 0; 14436 } 14437 } 14438 14439 if (destroy) 14440 dtrace_dof_destroy(dof); 14441 14442 return (gen); 14443 } 14444 14445 static dtrace_helpers_t * 14446 dtrace_helpers_create(proc_t *p) 14447 { 14448 dtrace_helpers_t *help; 14449 14450 ASSERT(MUTEX_HELD(&dtrace_lock)); 14451 ASSERT(p->p_dtrace_helpers == NULL); 14452 14453 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 14454 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 14455 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 14456 14457 p->p_dtrace_helpers = help; 14458 dtrace_helpers++; 14459 14460 return (help); 14461 } 14462 14463 static void 14464 dtrace_helpers_destroy(void) 14465 { 14466 dtrace_helpers_t *help; 14467 dtrace_vstate_t *vstate; 14468 proc_t *p = curproc; 14469 int i; 14470 14471 mutex_enter(&dtrace_lock); 14472 14473 ASSERT(p->p_dtrace_helpers != NULL); 14474 ASSERT(dtrace_helpers > 0); 14475 14476 help = p->p_dtrace_helpers; 14477 vstate = &help->dthps_vstate; 14478 14479 /* 14480 * We're now going to lose the help from this process. 14481 */ 14482 p->p_dtrace_helpers = NULL; 14483 dtrace_sync(); 14484 14485 /* 14486 * Destory the helper actions. 14487 */ 14488 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14489 dtrace_helper_action_t *h, *next; 14490 14491 for (h = help->dthps_actions[i]; h != NULL; h = next) { 14492 next = h->dtha_next; 14493 dtrace_helper_action_destroy(h, vstate); 14494 h = next; 14495 } 14496 } 14497 14498 mutex_exit(&dtrace_lock); 14499 14500 /* 14501 * Destroy the helper providers. 14502 */ 14503 if (help->dthps_maxprovs > 0) { 14504 mutex_enter(&dtrace_meta_lock); 14505 if (dtrace_meta_pid != NULL) { 14506 ASSERT(dtrace_deferred_pid == NULL); 14507 14508 for (i = 0; i < help->dthps_nprovs; i++) { 14509 dtrace_helper_provider_remove( 14510 &help->dthps_provs[i]->dthp_prov, p->p_pid); 14511 } 14512 } else { 14513 mutex_enter(&dtrace_lock); 14514 ASSERT(help->dthps_deferred == 0 || 14515 help->dthps_next != NULL || 14516 help->dthps_prev != NULL || 14517 help == dtrace_deferred_pid); 14518 14519 /* 14520 * Remove the helper from the deferred list. 14521 */ 14522 if (help->dthps_next != NULL) 14523 help->dthps_next->dthps_prev = help->dthps_prev; 14524 if (help->dthps_prev != NULL) 14525 help->dthps_prev->dthps_next = help->dthps_next; 14526 if (dtrace_deferred_pid == help) { 14527 dtrace_deferred_pid = help->dthps_next; 14528 ASSERT(help->dthps_prev == NULL); 14529 } 14530 14531 mutex_exit(&dtrace_lock); 14532 } 14533 14534 mutex_exit(&dtrace_meta_lock); 14535 14536 for (i = 0; i < help->dthps_nprovs; i++) { 14537 dtrace_helper_provider_destroy(help->dthps_provs[i]); 14538 } 14539 14540 kmem_free(help->dthps_provs, help->dthps_maxprovs * 14541 sizeof (dtrace_helper_provider_t *)); 14542 } 14543 14544 mutex_enter(&dtrace_lock); 14545 14546 dtrace_vstate_fini(&help->dthps_vstate); 14547 kmem_free(help->dthps_actions, 14548 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 14549 kmem_free(help, sizeof (dtrace_helpers_t)); 14550 14551 --dtrace_helpers; 14552 mutex_exit(&dtrace_lock); 14553 } 14554 14555 static void 14556 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 14557 { 14558 dtrace_helpers_t *help, *newhelp; 14559 dtrace_helper_action_t *helper, *new, *last; 14560 dtrace_difo_t *dp; 14561 dtrace_vstate_t *vstate; 14562 int i, j, sz, hasprovs = 0; 14563 14564 mutex_enter(&dtrace_lock); 14565 ASSERT(from->p_dtrace_helpers != NULL); 14566 ASSERT(dtrace_helpers > 0); 14567 14568 help = from->p_dtrace_helpers; 14569 newhelp = dtrace_helpers_create(to); 14570 ASSERT(to->p_dtrace_helpers != NULL); 14571 14572 newhelp->dthps_generation = help->dthps_generation; 14573 vstate = &newhelp->dthps_vstate; 14574 14575 /* 14576 * Duplicate the helper actions. 14577 */ 14578 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14579 if ((helper = help->dthps_actions[i]) == NULL) 14580 continue; 14581 14582 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 14583 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 14584 KM_SLEEP); 14585 new->dtha_generation = helper->dtha_generation; 14586 14587 if ((dp = helper->dtha_predicate) != NULL) { 14588 dp = dtrace_difo_duplicate(dp, vstate); 14589 new->dtha_predicate = dp; 14590 } 14591 14592 new->dtha_nactions = helper->dtha_nactions; 14593 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 14594 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 14595 14596 for (j = 0; j < new->dtha_nactions; j++) { 14597 dtrace_difo_t *dp = helper->dtha_actions[j]; 14598 14599 ASSERT(dp != NULL); 14600 dp = dtrace_difo_duplicate(dp, vstate); 14601 new->dtha_actions[j] = dp; 14602 } 14603 14604 if (last != NULL) { 14605 last->dtha_next = new; 14606 } else { 14607 newhelp->dthps_actions[i] = new; 14608 } 14609 14610 last = new; 14611 } 14612 } 14613 14614 /* 14615 * Duplicate the helper providers and register them with the 14616 * DTrace framework. 14617 */ 14618 if (help->dthps_nprovs > 0) { 14619 newhelp->dthps_nprovs = help->dthps_nprovs; 14620 newhelp->dthps_maxprovs = help->dthps_nprovs; 14621 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 14622 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14623 for (i = 0; i < newhelp->dthps_nprovs; i++) { 14624 newhelp->dthps_provs[i] = help->dthps_provs[i]; 14625 newhelp->dthps_provs[i]->dthp_ref++; 14626 } 14627 14628 hasprovs = 1; 14629 } 14630 14631 mutex_exit(&dtrace_lock); 14632 14633 if (hasprovs) 14634 dtrace_helper_provider_register(to, newhelp, NULL); 14635 } 14636 14637 /* 14638 * DTrace Hook Functions 14639 */ 14640 static void 14641 dtrace_module_loaded(struct modctl *ctl) 14642 { 14643 dtrace_provider_t *prv; 14644 14645 mutex_enter(&dtrace_provider_lock); 14646 mutex_enter(&mod_lock); 14647 14648 ASSERT(ctl->mod_busy); 14649 14650 /* 14651 * We're going to call each providers per-module provide operation 14652 * specifying only this module. 14653 */ 14654 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 14655 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 14656 14657 mutex_exit(&mod_lock); 14658 mutex_exit(&dtrace_provider_lock); 14659 14660 /* 14661 * If we have any retained enablings, we need to match against them. 14662 * Enabling probes requires that cpu_lock be held, and we cannot hold 14663 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 14664 * module. (In particular, this happens when loading scheduling 14665 * classes.) So if we have any retained enablings, we need to dispatch 14666 * our task queue to do the match for us. 14667 */ 14668 mutex_enter(&dtrace_lock); 14669 14670 if (dtrace_retained == NULL) { 14671 mutex_exit(&dtrace_lock); 14672 return; 14673 } 14674 14675 (void) taskq_dispatch(dtrace_taskq, 14676 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 14677 14678 mutex_exit(&dtrace_lock); 14679 14680 /* 14681 * And now, for a little heuristic sleaze: in general, we want to 14682 * match modules as soon as they load. However, we cannot guarantee 14683 * this, because it would lead us to the lock ordering violation 14684 * outlined above. The common case, of course, is that cpu_lock is 14685 * _not_ held -- so we delay here for a clock tick, hoping that that's 14686 * long enough for the task queue to do its work. If it's not, it's 14687 * not a serious problem -- it just means that the module that we 14688 * just loaded may not be immediately instrumentable. 14689 */ 14690 delay(1); 14691 } 14692 14693 static void 14694 dtrace_module_unloaded(struct modctl *ctl) 14695 { 14696 dtrace_probe_t template, *probe, *first, *next; 14697 dtrace_provider_t *prov; 14698 14699 template.dtpr_mod = ctl->mod_modname; 14700 14701 mutex_enter(&dtrace_provider_lock); 14702 mutex_enter(&mod_lock); 14703 mutex_enter(&dtrace_lock); 14704 14705 if (dtrace_bymod == NULL) { 14706 /* 14707 * The DTrace module is loaded (obviously) but not attached; 14708 * we don't have any work to do. 14709 */ 14710 mutex_exit(&dtrace_provider_lock); 14711 mutex_exit(&mod_lock); 14712 mutex_exit(&dtrace_lock); 14713 return; 14714 } 14715 14716 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 14717 probe != NULL; probe = probe->dtpr_nextmod) { 14718 if (probe->dtpr_ecb != NULL) { 14719 mutex_exit(&dtrace_provider_lock); 14720 mutex_exit(&mod_lock); 14721 mutex_exit(&dtrace_lock); 14722 14723 /* 14724 * This shouldn't _actually_ be possible -- we're 14725 * unloading a module that has an enabled probe in it. 14726 * (It's normally up to the provider to make sure that 14727 * this can't happen.) However, because dtps_enable() 14728 * doesn't have a failure mode, there can be an 14729 * enable/unload race. Upshot: we don't want to 14730 * assert, but we're not going to disable the 14731 * probe, either. 14732 */ 14733 if (dtrace_err_verbose) { 14734 cmn_err(CE_WARN, "unloaded module '%s' had " 14735 "enabled probes", ctl->mod_modname); 14736 } 14737 14738 return; 14739 } 14740 } 14741 14742 probe = first; 14743 14744 for (first = NULL; probe != NULL; probe = next) { 14745 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 14746 14747 dtrace_probes[probe->dtpr_id - 1] = NULL; 14748 14749 next = probe->dtpr_nextmod; 14750 dtrace_hash_remove(dtrace_bymod, probe); 14751 dtrace_hash_remove(dtrace_byfunc, probe); 14752 dtrace_hash_remove(dtrace_byname, probe); 14753 14754 if (first == NULL) { 14755 first = probe; 14756 probe->dtpr_nextmod = NULL; 14757 } else { 14758 probe->dtpr_nextmod = first; 14759 first = probe; 14760 } 14761 } 14762 14763 /* 14764 * We've removed all of the module's probes from the hash chains and 14765 * from the probe array. Now issue a dtrace_sync() to be sure that 14766 * everyone has cleared out from any probe array processing. 14767 */ 14768 dtrace_sync(); 14769 14770 for (probe = first; probe != NULL; probe = first) { 14771 first = probe->dtpr_nextmod; 14772 prov = probe->dtpr_provider; 14773 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 14774 probe->dtpr_arg); 14775 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 14776 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 14777 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 14778 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 14779 kmem_free(probe, sizeof (dtrace_probe_t)); 14780 } 14781 14782 mutex_exit(&dtrace_lock); 14783 mutex_exit(&mod_lock); 14784 mutex_exit(&dtrace_provider_lock); 14785 } 14786 14787 void 14788 dtrace_suspend(void) 14789 { 14790 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 14791 } 14792 14793 void 14794 dtrace_resume(void) 14795 { 14796 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 14797 } 14798 14799 static int 14800 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 14801 { 14802 ASSERT(MUTEX_HELD(&cpu_lock)); 14803 mutex_enter(&dtrace_lock); 14804 14805 switch (what) { 14806 case CPU_CONFIG: { 14807 dtrace_state_t *state; 14808 dtrace_optval_t *opt, rs, c; 14809 14810 /* 14811 * For now, we only allocate a new buffer for anonymous state. 14812 */ 14813 if ((state = dtrace_anon.dta_state) == NULL) 14814 break; 14815 14816 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14817 break; 14818 14819 opt = state->dts_options; 14820 c = opt[DTRACEOPT_CPU]; 14821 14822 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 14823 break; 14824 14825 /* 14826 * Regardless of what the actual policy is, we're going to 14827 * temporarily set our resize policy to be manual. We're 14828 * also going to temporarily set our CPU option to denote 14829 * the newly configured CPU. 14830 */ 14831 rs = opt[DTRACEOPT_BUFRESIZE]; 14832 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 14833 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 14834 14835 (void) dtrace_state_buffers(state); 14836 14837 opt[DTRACEOPT_BUFRESIZE] = rs; 14838 opt[DTRACEOPT_CPU] = c; 14839 14840 break; 14841 } 14842 14843 case CPU_UNCONFIG: 14844 /* 14845 * We don't free the buffer in the CPU_UNCONFIG case. (The 14846 * buffer will be freed when the consumer exits.) 14847 */ 14848 break; 14849 14850 default: 14851 break; 14852 } 14853 14854 mutex_exit(&dtrace_lock); 14855 return (0); 14856 } 14857 14858 static void 14859 dtrace_cpu_setup_initial(processorid_t cpu) 14860 { 14861 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 14862 } 14863 14864 static void 14865 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 14866 { 14867 if (dtrace_toxranges >= dtrace_toxranges_max) { 14868 int osize, nsize; 14869 dtrace_toxrange_t *range; 14870 14871 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14872 14873 if (osize == 0) { 14874 ASSERT(dtrace_toxrange == NULL); 14875 ASSERT(dtrace_toxranges_max == 0); 14876 dtrace_toxranges_max = 1; 14877 } else { 14878 dtrace_toxranges_max <<= 1; 14879 } 14880 14881 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14882 range = kmem_zalloc(nsize, KM_SLEEP); 14883 14884 if (dtrace_toxrange != NULL) { 14885 ASSERT(osize != 0); 14886 bcopy(dtrace_toxrange, range, osize); 14887 kmem_free(dtrace_toxrange, osize); 14888 } 14889 14890 dtrace_toxrange = range; 14891 } 14892 14893 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); 14894 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); 14895 14896 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 14897 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 14898 dtrace_toxranges++; 14899 } 14900 14901 static void 14902 dtrace_getf_barrier() 14903 { 14904 /* 14905 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 14906 * that contain calls to getf(), this routine will be called on every 14907 * closef() before either the underlying vnode is released or the 14908 * file_t itself is freed. By the time we are here, it is essential 14909 * that the file_t can no longer be accessed from a call to getf() 14910 * in probe context -- that assures that a dtrace_sync() can be used 14911 * to clear out any enablings referring to the old structures. 14912 */ 14913 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 14914 kcred->cr_zone->zone_dtrace_getf != 0) 14915 dtrace_sync(); 14916 } 14917 14918 /* 14919 * DTrace Driver Cookbook Functions 14920 */ 14921 /*ARGSUSED*/ 14922 static int 14923 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 14924 { 14925 dtrace_provider_id_t id; 14926 dtrace_state_t *state = NULL; 14927 dtrace_enabling_t *enab; 14928 14929 mutex_enter(&cpu_lock); 14930 mutex_enter(&dtrace_provider_lock); 14931 mutex_enter(&dtrace_lock); 14932 14933 if (ddi_soft_state_init(&dtrace_softstate, 14934 sizeof (dtrace_state_t), 0) != 0) { 14935 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 14936 mutex_exit(&cpu_lock); 14937 mutex_exit(&dtrace_provider_lock); 14938 mutex_exit(&dtrace_lock); 14939 return (DDI_FAILURE); 14940 } 14941 14942 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 14943 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 14944 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 14945 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 14946 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 14947 ddi_remove_minor_node(devi, NULL); 14948 ddi_soft_state_fini(&dtrace_softstate); 14949 mutex_exit(&cpu_lock); 14950 mutex_exit(&dtrace_provider_lock); 14951 mutex_exit(&dtrace_lock); 14952 return (DDI_FAILURE); 14953 } 14954 14955 ddi_report_dev(devi); 14956 dtrace_devi = devi; 14957 14958 dtrace_modload = dtrace_module_loaded; 14959 dtrace_modunload = dtrace_module_unloaded; 14960 dtrace_cpu_init = dtrace_cpu_setup_initial; 14961 dtrace_helpers_cleanup = dtrace_helpers_destroy; 14962 dtrace_helpers_fork = dtrace_helpers_duplicate; 14963 dtrace_cpustart_init = dtrace_suspend; 14964 dtrace_cpustart_fini = dtrace_resume; 14965 dtrace_debugger_init = dtrace_suspend; 14966 dtrace_debugger_fini = dtrace_resume; 14967 14968 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 14969 14970 ASSERT(MUTEX_HELD(&cpu_lock)); 14971 14972 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 14973 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14974 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 14975 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 14976 VM_SLEEP | VMC_IDENTIFIER); 14977 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 14978 1, INT_MAX, 0); 14979 14980 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 14981 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 14982 NULL, NULL, NULL, NULL, NULL, 0); 14983 14984 ASSERT(MUTEX_HELD(&cpu_lock)); 14985 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 14986 offsetof(dtrace_probe_t, dtpr_nextmod), 14987 offsetof(dtrace_probe_t, dtpr_prevmod)); 14988 14989 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 14990 offsetof(dtrace_probe_t, dtpr_nextfunc), 14991 offsetof(dtrace_probe_t, dtpr_prevfunc)); 14992 14993 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 14994 offsetof(dtrace_probe_t, dtpr_nextname), 14995 offsetof(dtrace_probe_t, dtpr_prevname)); 14996 14997 if (dtrace_retain_max < 1) { 14998 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 14999 "setting to 1", dtrace_retain_max); 15000 dtrace_retain_max = 1; 15001 } 15002 15003 /* 15004 * Now discover our toxic ranges. 15005 */ 15006 dtrace_toxic_ranges(dtrace_toxrange_add); 15007 15008 /* 15009 * Before we register ourselves as a provider to our own framework, 15010 * we would like to assert that dtrace_provider is NULL -- but that's 15011 * not true if we were loaded as a dependency of a DTrace provider. 15012 * Once we've registered, we can assert that dtrace_provider is our 15013 * pseudo provider. 15014 */ 15015 (void) dtrace_register("dtrace", &dtrace_provider_attr, 15016 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 15017 15018 ASSERT(dtrace_provider != NULL); 15019 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 15020 15021 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 15022 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 15023 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 15024 dtrace_provider, NULL, NULL, "END", 0, NULL); 15025 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 15026 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 15027 15028 dtrace_anon_property(); 15029 mutex_exit(&cpu_lock); 15030 15031 /* 15032 * If DTrace helper tracing is enabled, we need to allocate the 15033 * trace buffer and initialize the values. 15034 */ 15035 if (dtrace_helptrace_enabled) { 15036 ASSERT(dtrace_helptrace_buffer == NULL); 15037 dtrace_helptrace_buffer = 15038 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 15039 dtrace_helptrace_next = 0; 15040 } 15041 15042 /* 15043 * If there are already providers, we must ask them to provide their 15044 * probes, and then match any anonymous enabling against them. Note 15045 * that there should be no other retained enablings at this time: 15046 * the only retained enablings at this time should be the anonymous 15047 * enabling. 15048 */ 15049 if (dtrace_anon.dta_enabling != NULL) { 15050 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 15051 15052 dtrace_enabling_provide(NULL); 15053 state = dtrace_anon.dta_state; 15054 15055 /* 15056 * We couldn't hold cpu_lock across the above call to 15057 * dtrace_enabling_provide(), but we must hold it to actually 15058 * enable the probes. We have to drop all of our locks, pick 15059 * up cpu_lock, and regain our locks before matching the 15060 * retained anonymous enabling. 15061 */ 15062 mutex_exit(&dtrace_lock); 15063 mutex_exit(&dtrace_provider_lock); 15064 15065 mutex_enter(&cpu_lock); 15066 mutex_enter(&dtrace_provider_lock); 15067 mutex_enter(&dtrace_lock); 15068 15069 if ((enab = dtrace_anon.dta_enabling) != NULL) 15070 (void) dtrace_enabling_match(enab, NULL); 15071 15072 mutex_exit(&cpu_lock); 15073 } 15074 15075 mutex_exit(&dtrace_lock); 15076 mutex_exit(&dtrace_provider_lock); 15077 15078 if (state != NULL) { 15079 /* 15080 * If we created any anonymous state, set it going now. 15081 */ 15082 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 15083 } 15084 15085 return (DDI_SUCCESS); 15086 } 15087 15088 /*ARGSUSED*/ 15089 static int 15090 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 15091 { 15092 dtrace_state_t *state; 15093 uint32_t priv; 15094 uid_t uid; 15095 zoneid_t zoneid; 15096 15097 if (getminor(*devp) == DTRACEMNRN_HELPER) 15098 return (0); 15099 15100 /* 15101 * If this wasn't an open with the "helper" minor, then it must be 15102 * the "dtrace" minor. 15103 */ 15104 if (getminor(*devp) != DTRACEMNRN_DTRACE) 15105 return (ENXIO); 15106 15107 /* 15108 * If no DTRACE_PRIV_* bits are set in the credential, then the 15109 * caller lacks sufficient permission to do anything with DTrace. 15110 */ 15111 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 15112 if (priv == DTRACE_PRIV_NONE) 15113 return (EACCES); 15114 15115 /* 15116 * Ask all providers to provide all their probes. 15117 */ 15118 mutex_enter(&dtrace_provider_lock); 15119 dtrace_probe_provide(NULL, NULL); 15120 mutex_exit(&dtrace_provider_lock); 15121 15122 mutex_enter(&cpu_lock); 15123 mutex_enter(&dtrace_lock); 15124 dtrace_opens++; 15125 dtrace_membar_producer(); 15126 15127 /* 15128 * If the kernel debugger is active (that is, if the kernel debugger 15129 * modified text in some way), we won't allow the open. 15130 */ 15131 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15132 dtrace_opens--; 15133 mutex_exit(&cpu_lock); 15134 mutex_exit(&dtrace_lock); 15135 return (EBUSY); 15136 } 15137 15138 state = dtrace_state_create(devp, cred_p); 15139 mutex_exit(&cpu_lock); 15140 15141 if (state == NULL) { 15142 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 15143 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15144 mutex_exit(&dtrace_lock); 15145 return (EAGAIN); 15146 } 15147 15148 mutex_exit(&dtrace_lock); 15149 15150 return (0); 15151 } 15152 15153 /*ARGSUSED*/ 15154 static int 15155 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 15156 { 15157 minor_t minor = getminor(dev); 15158 dtrace_state_t *state; 15159 15160 if (minor == DTRACEMNRN_HELPER) 15161 return (0); 15162 15163 state = ddi_get_soft_state(dtrace_softstate, minor); 15164 15165 mutex_enter(&cpu_lock); 15166 mutex_enter(&dtrace_lock); 15167 15168 if (state->dts_anon) { 15169 /* 15170 * There is anonymous state. Destroy that first. 15171 */ 15172 ASSERT(dtrace_anon.dta_state == NULL); 15173 dtrace_state_destroy(state->dts_anon); 15174 } 15175 15176 dtrace_state_destroy(state); 15177 ASSERT(dtrace_opens > 0); 15178 15179 /* 15180 * Only relinquish control of the kernel debugger interface when there 15181 * are no consumers and no anonymous enablings. 15182 */ 15183 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 15184 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15185 15186 mutex_exit(&dtrace_lock); 15187 mutex_exit(&cpu_lock); 15188 15189 return (0); 15190 } 15191 15192 /*ARGSUSED*/ 15193 static int 15194 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 15195 { 15196 int rval; 15197 dof_helper_t help, *dhp = NULL; 15198 15199 switch (cmd) { 15200 case DTRACEHIOC_ADDDOF: 15201 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 15202 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 15203 return (EFAULT); 15204 } 15205 15206 dhp = &help; 15207 arg = (intptr_t)help.dofhp_dof; 15208 /*FALLTHROUGH*/ 15209 15210 case DTRACEHIOC_ADD: { 15211 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 15212 15213 if (dof == NULL) 15214 return (rval); 15215 15216 mutex_enter(&dtrace_lock); 15217 15218 /* 15219 * dtrace_helper_slurp() takes responsibility for the dof -- 15220 * it may free it now or it may save it and free it later. 15221 */ 15222 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 15223 *rv = rval; 15224 rval = 0; 15225 } else { 15226 rval = EINVAL; 15227 } 15228 15229 mutex_exit(&dtrace_lock); 15230 return (rval); 15231 } 15232 15233 case DTRACEHIOC_REMOVE: { 15234 mutex_enter(&dtrace_lock); 15235 rval = dtrace_helper_destroygen(arg); 15236 mutex_exit(&dtrace_lock); 15237 15238 return (rval); 15239 } 15240 15241 default: 15242 break; 15243 } 15244 15245 return (ENOTTY); 15246 } 15247 15248 /*ARGSUSED*/ 15249 static int 15250 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 15251 { 15252 minor_t minor = getminor(dev); 15253 dtrace_state_t *state; 15254 int rval; 15255 15256 if (minor == DTRACEMNRN_HELPER) 15257 return (dtrace_ioctl_helper(cmd, arg, rv)); 15258 15259 state = ddi_get_soft_state(dtrace_softstate, minor); 15260 15261 if (state->dts_anon) { 15262 ASSERT(dtrace_anon.dta_state == NULL); 15263 state = state->dts_anon; 15264 } 15265 15266 switch (cmd) { 15267 case DTRACEIOC_PROVIDER: { 15268 dtrace_providerdesc_t pvd; 15269 dtrace_provider_t *pvp; 15270 15271 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 15272 return (EFAULT); 15273 15274 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 15275 mutex_enter(&dtrace_provider_lock); 15276 15277 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 15278 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 15279 break; 15280 } 15281 15282 mutex_exit(&dtrace_provider_lock); 15283 15284 if (pvp == NULL) 15285 return (ESRCH); 15286 15287 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 15288 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 15289 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 15290 return (EFAULT); 15291 15292 return (0); 15293 } 15294 15295 case DTRACEIOC_EPROBE: { 15296 dtrace_eprobedesc_t epdesc; 15297 dtrace_ecb_t *ecb; 15298 dtrace_action_t *act; 15299 void *buf; 15300 size_t size; 15301 uintptr_t dest; 15302 int nrecs; 15303 15304 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 15305 return (EFAULT); 15306 15307 mutex_enter(&dtrace_lock); 15308 15309 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 15310 mutex_exit(&dtrace_lock); 15311 return (EINVAL); 15312 } 15313 15314 if (ecb->dte_probe == NULL) { 15315 mutex_exit(&dtrace_lock); 15316 return (EINVAL); 15317 } 15318 15319 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 15320 epdesc.dtepd_uarg = ecb->dte_uarg; 15321 epdesc.dtepd_size = ecb->dte_size; 15322 15323 nrecs = epdesc.dtepd_nrecs; 15324 epdesc.dtepd_nrecs = 0; 15325 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15326 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15327 continue; 15328 15329 epdesc.dtepd_nrecs++; 15330 } 15331 15332 /* 15333 * Now that we have the size, we need to allocate a temporary 15334 * buffer in which to store the complete description. We need 15335 * the temporary buffer to be able to drop dtrace_lock() 15336 * across the copyout(), below. 15337 */ 15338 size = sizeof (dtrace_eprobedesc_t) + 15339 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 15340 15341 buf = kmem_alloc(size, KM_SLEEP); 15342 dest = (uintptr_t)buf; 15343 15344 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 15345 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 15346 15347 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15348 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15349 continue; 15350 15351 if (nrecs-- == 0) 15352 break; 15353 15354 bcopy(&act->dta_rec, (void *)dest, 15355 sizeof (dtrace_recdesc_t)); 15356 dest += sizeof (dtrace_recdesc_t); 15357 } 15358 15359 mutex_exit(&dtrace_lock); 15360 15361 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15362 kmem_free(buf, size); 15363 return (EFAULT); 15364 } 15365 15366 kmem_free(buf, size); 15367 return (0); 15368 } 15369 15370 case DTRACEIOC_AGGDESC: { 15371 dtrace_aggdesc_t aggdesc; 15372 dtrace_action_t *act; 15373 dtrace_aggregation_t *agg; 15374 int nrecs; 15375 uint32_t offs; 15376 dtrace_recdesc_t *lrec; 15377 void *buf; 15378 size_t size; 15379 uintptr_t dest; 15380 15381 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 15382 return (EFAULT); 15383 15384 mutex_enter(&dtrace_lock); 15385 15386 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 15387 mutex_exit(&dtrace_lock); 15388 return (EINVAL); 15389 } 15390 15391 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 15392 15393 nrecs = aggdesc.dtagd_nrecs; 15394 aggdesc.dtagd_nrecs = 0; 15395 15396 offs = agg->dtag_base; 15397 lrec = &agg->dtag_action.dta_rec; 15398 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 15399 15400 for (act = agg->dtag_first; ; act = act->dta_next) { 15401 ASSERT(act->dta_intuple || 15402 DTRACEACT_ISAGG(act->dta_kind)); 15403 15404 /* 15405 * If this action has a record size of zero, it 15406 * denotes an argument to the aggregating action. 15407 * Because the presence of this record doesn't (or 15408 * shouldn't) affect the way the data is interpreted, 15409 * we don't copy it out to save user-level the 15410 * confusion of dealing with a zero-length record. 15411 */ 15412 if (act->dta_rec.dtrd_size == 0) { 15413 ASSERT(agg->dtag_hasarg); 15414 continue; 15415 } 15416 15417 aggdesc.dtagd_nrecs++; 15418 15419 if (act == &agg->dtag_action) 15420 break; 15421 } 15422 15423 /* 15424 * Now that we have the size, we need to allocate a temporary 15425 * buffer in which to store the complete description. We need 15426 * the temporary buffer to be able to drop dtrace_lock() 15427 * across the copyout(), below. 15428 */ 15429 size = sizeof (dtrace_aggdesc_t) + 15430 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 15431 15432 buf = kmem_alloc(size, KM_SLEEP); 15433 dest = (uintptr_t)buf; 15434 15435 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 15436 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 15437 15438 for (act = agg->dtag_first; ; act = act->dta_next) { 15439 dtrace_recdesc_t rec = act->dta_rec; 15440 15441 /* 15442 * See the comment in the above loop for why we pass 15443 * over zero-length records. 15444 */ 15445 if (rec.dtrd_size == 0) { 15446 ASSERT(agg->dtag_hasarg); 15447 continue; 15448 } 15449 15450 if (nrecs-- == 0) 15451 break; 15452 15453 rec.dtrd_offset -= offs; 15454 bcopy(&rec, (void *)dest, sizeof (rec)); 15455 dest += sizeof (dtrace_recdesc_t); 15456 15457 if (act == &agg->dtag_action) 15458 break; 15459 } 15460 15461 mutex_exit(&dtrace_lock); 15462 15463 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15464 kmem_free(buf, size); 15465 return (EFAULT); 15466 } 15467 15468 kmem_free(buf, size); 15469 return (0); 15470 } 15471 15472 case DTRACEIOC_ENABLE: { 15473 dof_hdr_t *dof; 15474 dtrace_enabling_t *enab = NULL; 15475 dtrace_vstate_t *vstate; 15476 int err = 0; 15477 15478 *rv = 0; 15479 15480 /* 15481 * If a NULL argument has been passed, we take this as our 15482 * cue to reevaluate our enablings. 15483 */ 15484 if (arg == NULL) { 15485 dtrace_enabling_matchall(); 15486 15487 return (0); 15488 } 15489 15490 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 15491 return (rval); 15492 15493 mutex_enter(&cpu_lock); 15494 mutex_enter(&dtrace_lock); 15495 vstate = &state->dts_vstate; 15496 15497 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 15498 mutex_exit(&dtrace_lock); 15499 mutex_exit(&cpu_lock); 15500 dtrace_dof_destroy(dof); 15501 return (EBUSY); 15502 } 15503 15504 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 15505 mutex_exit(&dtrace_lock); 15506 mutex_exit(&cpu_lock); 15507 dtrace_dof_destroy(dof); 15508 return (EINVAL); 15509 } 15510 15511 if ((rval = dtrace_dof_options(dof, state)) != 0) { 15512 dtrace_enabling_destroy(enab); 15513 mutex_exit(&dtrace_lock); 15514 mutex_exit(&cpu_lock); 15515 dtrace_dof_destroy(dof); 15516 return (rval); 15517 } 15518 15519 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 15520 err = dtrace_enabling_retain(enab); 15521 } else { 15522 dtrace_enabling_destroy(enab); 15523 } 15524 15525 mutex_exit(&cpu_lock); 15526 mutex_exit(&dtrace_lock); 15527 dtrace_dof_destroy(dof); 15528 15529 return (err); 15530 } 15531 15532 case DTRACEIOC_REPLICATE: { 15533 dtrace_repldesc_t desc; 15534 dtrace_probedesc_t *match = &desc.dtrpd_match; 15535 dtrace_probedesc_t *create = &desc.dtrpd_create; 15536 int err; 15537 15538 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15539 return (EFAULT); 15540 15541 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15542 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15543 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15544 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15545 15546 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15547 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15548 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15549 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15550 15551 mutex_enter(&dtrace_lock); 15552 err = dtrace_enabling_replicate(state, match, create); 15553 mutex_exit(&dtrace_lock); 15554 15555 return (err); 15556 } 15557 15558 case DTRACEIOC_PROBEMATCH: 15559 case DTRACEIOC_PROBES: { 15560 dtrace_probe_t *probe = NULL; 15561 dtrace_probedesc_t desc; 15562 dtrace_probekey_t pkey; 15563 dtrace_id_t i; 15564 int m = 0; 15565 uint32_t priv; 15566 uid_t uid; 15567 zoneid_t zoneid; 15568 15569 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15570 return (EFAULT); 15571 15572 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15573 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15574 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15575 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15576 15577 /* 15578 * Before we attempt to match this probe, we want to give 15579 * all providers the opportunity to provide it. 15580 */ 15581 if (desc.dtpd_id == DTRACE_IDNONE) { 15582 mutex_enter(&dtrace_provider_lock); 15583 dtrace_probe_provide(&desc, NULL); 15584 mutex_exit(&dtrace_provider_lock); 15585 desc.dtpd_id++; 15586 } 15587 15588 if (cmd == DTRACEIOC_PROBEMATCH) { 15589 dtrace_probekey(&desc, &pkey); 15590 pkey.dtpk_id = DTRACE_IDNONE; 15591 } 15592 15593 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 15594 15595 mutex_enter(&dtrace_lock); 15596 15597 if (cmd == DTRACEIOC_PROBEMATCH) { 15598 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15599 if ((probe = dtrace_probes[i - 1]) != NULL && 15600 (m = dtrace_match_probe(probe, &pkey, 15601 priv, uid, zoneid)) != 0) 15602 break; 15603 } 15604 15605 if (m < 0) { 15606 mutex_exit(&dtrace_lock); 15607 return (EINVAL); 15608 } 15609 15610 } else { 15611 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15612 if ((probe = dtrace_probes[i - 1]) != NULL && 15613 dtrace_match_priv(probe, priv, uid, zoneid)) 15614 break; 15615 } 15616 } 15617 15618 if (probe == NULL) { 15619 mutex_exit(&dtrace_lock); 15620 return (ESRCH); 15621 } 15622 15623 dtrace_probe_description(probe, &desc); 15624 mutex_exit(&dtrace_lock); 15625 15626 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15627 return (EFAULT); 15628 15629 return (0); 15630 } 15631 15632 case DTRACEIOC_PROBEARG: { 15633 dtrace_argdesc_t desc; 15634 dtrace_probe_t *probe; 15635 dtrace_provider_t *prov; 15636 15637 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15638 return (EFAULT); 15639 15640 if (desc.dtargd_id == DTRACE_IDNONE) 15641 return (EINVAL); 15642 15643 if (desc.dtargd_ndx == DTRACE_ARGNONE) 15644 return (EINVAL); 15645 15646 mutex_enter(&dtrace_provider_lock); 15647 mutex_enter(&mod_lock); 15648 mutex_enter(&dtrace_lock); 15649 15650 if (desc.dtargd_id > dtrace_nprobes) { 15651 mutex_exit(&dtrace_lock); 15652 mutex_exit(&mod_lock); 15653 mutex_exit(&dtrace_provider_lock); 15654 return (EINVAL); 15655 } 15656 15657 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 15658 mutex_exit(&dtrace_lock); 15659 mutex_exit(&mod_lock); 15660 mutex_exit(&dtrace_provider_lock); 15661 return (EINVAL); 15662 } 15663 15664 mutex_exit(&dtrace_lock); 15665 15666 prov = probe->dtpr_provider; 15667 15668 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 15669 /* 15670 * There isn't any typed information for this probe. 15671 * Set the argument number to DTRACE_ARGNONE. 15672 */ 15673 desc.dtargd_ndx = DTRACE_ARGNONE; 15674 } else { 15675 desc.dtargd_native[0] = '\0'; 15676 desc.dtargd_xlate[0] = '\0'; 15677 desc.dtargd_mapping = desc.dtargd_ndx; 15678 15679 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 15680 probe->dtpr_id, probe->dtpr_arg, &desc); 15681 } 15682 15683 mutex_exit(&mod_lock); 15684 mutex_exit(&dtrace_provider_lock); 15685 15686 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15687 return (EFAULT); 15688 15689 return (0); 15690 } 15691 15692 case DTRACEIOC_GO: { 15693 processorid_t cpuid; 15694 rval = dtrace_state_go(state, &cpuid); 15695 15696 if (rval != 0) 15697 return (rval); 15698 15699 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15700 return (EFAULT); 15701 15702 return (0); 15703 } 15704 15705 case DTRACEIOC_STOP: { 15706 processorid_t cpuid; 15707 15708 mutex_enter(&dtrace_lock); 15709 rval = dtrace_state_stop(state, &cpuid); 15710 mutex_exit(&dtrace_lock); 15711 15712 if (rval != 0) 15713 return (rval); 15714 15715 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15716 return (EFAULT); 15717 15718 return (0); 15719 } 15720 15721 case DTRACEIOC_DOFGET: { 15722 dof_hdr_t hdr, *dof; 15723 uint64_t len; 15724 15725 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 15726 return (EFAULT); 15727 15728 mutex_enter(&dtrace_lock); 15729 dof = dtrace_dof_create(state); 15730 mutex_exit(&dtrace_lock); 15731 15732 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 15733 rval = copyout(dof, (void *)arg, len); 15734 dtrace_dof_destroy(dof); 15735 15736 return (rval == 0 ? 0 : EFAULT); 15737 } 15738 15739 case DTRACEIOC_AGGSNAP: 15740 case DTRACEIOC_BUFSNAP: { 15741 dtrace_bufdesc_t desc; 15742 caddr_t cached; 15743 dtrace_buffer_t *buf; 15744 15745 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15746 return (EFAULT); 15747 15748 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 15749 return (EINVAL); 15750 15751 mutex_enter(&dtrace_lock); 15752 15753 if (cmd == DTRACEIOC_BUFSNAP) { 15754 buf = &state->dts_buffer[desc.dtbd_cpu]; 15755 } else { 15756 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 15757 } 15758 15759 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 15760 size_t sz = buf->dtb_offset; 15761 15762 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 15763 mutex_exit(&dtrace_lock); 15764 return (EBUSY); 15765 } 15766 15767 /* 15768 * If this buffer has already been consumed, we're 15769 * going to indicate that there's nothing left here 15770 * to consume. 15771 */ 15772 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 15773 mutex_exit(&dtrace_lock); 15774 15775 desc.dtbd_size = 0; 15776 desc.dtbd_drops = 0; 15777 desc.dtbd_errors = 0; 15778 desc.dtbd_oldest = 0; 15779 sz = sizeof (desc); 15780 15781 if (copyout(&desc, (void *)arg, sz) != 0) 15782 return (EFAULT); 15783 15784 return (0); 15785 } 15786 15787 /* 15788 * If this is a ring buffer that has wrapped, we want 15789 * to copy the whole thing out. 15790 */ 15791 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 15792 dtrace_buffer_polish(buf); 15793 sz = buf->dtb_size; 15794 } 15795 15796 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 15797 mutex_exit(&dtrace_lock); 15798 return (EFAULT); 15799 } 15800 15801 desc.dtbd_size = sz; 15802 desc.dtbd_drops = buf->dtb_drops; 15803 desc.dtbd_errors = buf->dtb_errors; 15804 desc.dtbd_oldest = buf->dtb_xamot_offset; 15805 15806 mutex_exit(&dtrace_lock); 15807 15808 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15809 return (EFAULT); 15810 15811 buf->dtb_flags |= DTRACEBUF_CONSUMED; 15812 15813 return (0); 15814 } 15815 15816 if (buf->dtb_tomax == NULL) { 15817 ASSERT(buf->dtb_xamot == NULL); 15818 mutex_exit(&dtrace_lock); 15819 return (ENOENT); 15820 } 15821 15822 cached = buf->dtb_tomax; 15823 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 15824 15825 dtrace_xcall(desc.dtbd_cpu, 15826 (dtrace_xcall_t)dtrace_buffer_switch, buf); 15827 15828 state->dts_errors += buf->dtb_xamot_errors; 15829 15830 /* 15831 * If the buffers did not actually switch, then the cross call 15832 * did not take place -- presumably because the given CPU is 15833 * not in the ready set. If this is the case, we'll return 15834 * ENOENT. 15835 */ 15836 if (buf->dtb_tomax == cached) { 15837 ASSERT(buf->dtb_xamot != cached); 15838 mutex_exit(&dtrace_lock); 15839 return (ENOENT); 15840 } 15841 15842 ASSERT(cached == buf->dtb_xamot); 15843 15844 /* 15845 * We have our snapshot; now copy it out. 15846 */ 15847 if (copyout(buf->dtb_xamot, desc.dtbd_data, 15848 buf->dtb_xamot_offset) != 0) { 15849 mutex_exit(&dtrace_lock); 15850 return (EFAULT); 15851 } 15852 15853 desc.dtbd_size = buf->dtb_xamot_offset; 15854 desc.dtbd_drops = buf->dtb_xamot_drops; 15855 desc.dtbd_errors = buf->dtb_xamot_errors; 15856 desc.dtbd_oldest = 0; 15857 15858 mutex_exit(&dtrace_lock); 15859 15860 /* 15861 * Finally, copy out the buffer description. 15862 */ 15863 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15864 return (EFAULT); 15865 15866 return (0); 15867 } 15868 15869 case DTRACEIOC_CONF: { 15870 dtrace_conf_t conf; 15871 15872 bzero(&conf, sizeof (conf)); 15873 conf.dtc_difversion = DIF_VERSION; 15874 conf.dtc_difintregs = DIF_DIR_NREGS; 15875 conf.dtc_diftupregs = DIF_DTR_NREGS; 15876 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 15877 15878 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 15879 return (EFAULT); 15880 15881 return (0); 15882 } 15883 15884 case DTRACEIOC_STATUS: { 15885 dtrace_status_t stat; 15886 dtrace_dstate_t *dstate; 15887 int i, j; 15888 uint64_t nerrs; 15889 15890 /* 15891 * See the comment in dtrace_state_deadman() for the reason 15892 * for setting dts_laststatus to INT64_MAX before setting 15893 * it to the correct value. 15894 */ 15895 state->dts_laststatus = INT64_MAX; 15896 dtrace_membar_producer(); 15897 state->dts_laststatus = dtrace_gethrtime(); 15898 15899 bzero(&stat, sizeof (stat)); 15900 15901 mutex_enter(&dtrace_lock); 15902 15903 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 15904 mutex_exit(&dtrace_lock); 15905 return (ENOENT); 15906 } 15907 15908 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 15909 stat.dtst_exiting = 1; 15910 15911 nerrs = state->dts_errors; 15912 dstate = &state->dts_vstate.dtvs_dynvars; 15913 15914 for (i = 0; i < NCPU; i++) { 15915 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 15916 15917 stat.dtst_dyndrops += dcpu->dtdsc_drops; 15918 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 15919 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 15920 15921 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 15922 stat.dtst_filled++; 15923 15924 nerrs += state->dts_buffer[i].dtb_errors; 15925 15926 for (j = 0; j < state->dts_nspeculations; j++) { 15927 dtrace_speculation_t *spec; 15928 dtrace_buffer_t *buf; 15929 15930 spec = &state->dts_speculations[j]; 15931 buf = &spec->dtsp_buffer[i]; 15932 stat.dtst_specdrops += buf->dtb_xamot_drops; 15933 } 15934 } 15935 15936 stat.dtst_specdrops_busy = state->dts_speculations_busy; 15937 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 15938 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 15939 stat.dtst_dblerrors = state->dts_dblerrors; 15940 stat.dtst_killed = 15941 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 15942 stat.dtst_errors = nerrs; 15943 15944 mutex_exit(&dtrace_lock); 15945 15946 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 15947 return (EFAULT); 15948 15949 return (0); 15950 } 15951 15952 case DTRACEIOC_FORMAT: { 15953 dtrace_fmtdesc_t fmt; 15954 char *str; 15955 int len; 15956 15957 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 15958 return (EFAULT); 15959 15960 mutex_enter(&dtrace_lock); 15961 15962 if (fmt.dtfd_format == 0 || 15963 fmt.dtfd_format > state->dts_nformats) { 15964 mutex_exit(&dtrace_lock); 15965 return (EINVAL); 15966 } 15967 15968 /* 15969 * Format strings are allocated contiguously and they are 15970 * never freed; if a format index is less than the number 15971 * of formats, we can assert that the format map is non-NULL 15972 * and that the format for the specified index is non-NULL. 15973 */ 15974 ASSERT(state->dts_formats != NULL); 15975 str = state->dts_formats[fmt.dtfd_format - 1]; 15976 ASSERT(str != NULL); 15977 15978 len = strlen(str) + 1; 15979 15980 if (len > fmt.dtfd_length) { 15981 fmt.dtfd_length = len; 15982 15983 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 15984 mutex_exit(&dtrace_lock); 15985 return (EINVAL); 15986 } 15987 } else { 15988 if (copyout(str, fmt.dtfd_string, len) != 0) { 15989 mutex_exit(&dtrace_lock); 15990 return (EINVAL); 15991 } 15992 } 15993 15994 mutex_exit(&dtrace_lock); 15995 return (0); 15996 } 15997 15998 default: 15999 break; 16000 } 16001 16002 return (ENOTTY); 16003 } 16004 16005 /*ARGSUSED*/ 16006 static int 16007 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 16008 { 16009 dtrace_state_t *state; 16010 16011 switch (cmd) { 16012 case DDI_DETACH: 16013 break; 16014 16015 case DDI_SUSPEND: 16016 return (DDI_SUCCESS); 16017 16018 default: 16019 return (DDI_FAILURE); 16020 } 16021 16022 mutex_enter(&cpu_lock); 16023 mutex_enter(&dtrace_provider_lock); 16024 mutex_enter(&dtrace_lock); 16025 16026 ASSERT(dtrace_opens == 0); 16027 16028 if (dtrace_helpers > 0) { 16029 mutex_exit(&dtrace_provider_lock); 16030 mutex_exit(&dtrace_lock); 16031 mutex_exit(&cpu_lock); 16032 return (DDI_FAILURE); 16033 } 16034 16035 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 16036 mutex_exit(&dtrace_provider_lock); 16037 mutex_exit(&dtrace_lock); 16038 mutex_exit(&cpu_lock); 16039 return (DDI_FAILURE); 16040 } 16041 16042 dtrace_provider = NULL; 16043 16044 if ((state = dtrace_anon_grab()) != NULL) { 16045 /* 16046 * If there were ECBs on this state, the provider should 16047 * have not been allowed to detach; assert that there is 16048 * none. 16049 */ 16050 ASSERT(state->dts_necbs == 0); 16051 dtrace_state_destroy(state); 16052 16053 /* 16054 * If we're being detached with anonymous state, we need to 16055 * indicate to the kernel debugger that DTrace is now inactive. 16056 */ 16057 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16058 } 16059 16060 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 16061 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16062 dtrace_cpu_init = NULL; 16063 dtrace_helpers_cleanup = NULL; 16064 dtrace_helpers_fork = NULL; 16065 dtrace_cpustart_init = NULL; 16066 dtrace_cpustart_fini = NULL; 16067 dtrace_debugger_init = NULL; 16068 dtrace_debugger_fini = NULL; 16069 dtrace_modload = NULL; 16070 dtrace_modunload = NULL; 16071 16072 ASSERT(dtrace_getf == 0); 16073 ASSERT(dtrace_closef == NULL); 16074 16075 mutex_exit(&cpu_lock); 16076 16077 if (dtrace_helptrace_enabled) { 16078 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 16079 dtrace_helptrace_buffer = NULL; 16080 } 16081 16082 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 16083 dtrace_probes = NULL; 16084 dtrace_nprobes = 0; 16085 16086 dtrace_hash_destroy(dtrace_bymod); 16087 dtrace_hash_destroy(dtrace_byfunc); 16088 dtrace_hash_destroy(dtrace_byname); 16089 dtrace_bymod = NULL; 16090 dtrace_byfunc = NULL; 16091 dtrace_byname = NULL; 16092 16093 kmem_cache_destroy(dtrace_state_cache); 16094 vmem_destroy(dtrace_minor); 16095 vmem_destroy(dtrace_arena); 16096 16097 if (dtrace_toxrange != NULL) { 16098 kmem_free(dtrace_toxrange, 16099 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 16100 dtrace_toxrange = NULL; 16101 dtrace_toxranges = 0; 16102 dtrace_toxranges_max = 0; 16103 } 16104 16105 ddi_remove_minor_node(dtrace_devi, NULL); 16106 dtrace_devi = NULL; 16107 16108 ddi_soft_state_fini(&dtrace_softstate); 16109 16110 ASSERT(dtrace_vtime_references == 0); 16111 ASSERT(dtrace_opens == 0); 16112 ASSERT(dtrace_retained == NULL); 16113 16114 mutex_exit(&dtrace_lock); 16115 mutex_exit(&dtrace_provider_lock); 16116 16117 /* 16118 * We don't destroy the task queue until after we have dropped our 16119 * locks (taskq_destroy() may block on running tasks). To prevent 16120 * attempting to do work after we have effectively detached but before 16121 * the task queue has been destroyed, all tasks dispatched via the 16122 * task queue must check that DTrace is still attached before 16123 * performing any operation. 16124 */ 16125 taskq_destroy(dtrace_taskq); 16126 dtrace_taskq = NULL; 16127 16128 return (DDI_SUCCESS); 16129 } 16130 16131 /*ARGSUSED*/ 16132 static int 16133 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 16134 { 16135 int error; 16136 16137 switch (infocmd) { 16138 case DDI_INFO_DEVT2DEVINFO: 16139 *result = (void *)dtrace_devi; 16140 error = DDI_SUCCESS; 16141 break; 16142 case DDI_INFO_DEVT2INSTANCE: 16143 *result = (void *)0; 16144 error = DDI_SUCCESS; 16145 break; 16146 default: 16147 error = DDI_FAILURE; 16148 } 16149 return (error); 16150 } 16151 16152 static struct cb_ops dtrace_cb_ops = { 16153 dtrace_open, /* open */ 16154 dtrace_close, /* close */ 16155 nulldev, /* strategy */ 16156 nulldev, /* print */ 16157 nodev, /* dump */ 16158 nodev, /* read */ 16159 nodev, /* write */ 16160 dtrace_ioctl, /* ioctl */ 16161 nodev, /* devmap */ 16162 nodev, /* mmap */ 16163 nodev, /* segmap */ 16164 nochpoll, /* poll */ 16165 ddi_prop_op, /* cb_prop_op */ 16166 0, /* streamtab */ 16167 D_NEW | D_MP /* Driver compatibility flag */ 16168 }; 16169 16170 static struct dev_ops dtrace_ops = { 16171 DEVO_REV, /* devo_rev */ 16172 0, /* refcnt */ 16173 dtrace_info, /* get_dev_info */ 16174 nulldev, /* identify */ 16175 nulldev, /* probe */ 16176 dtrace_attach, /* attach */ 16177 dtrace_detach, /* detach */ 16178 nodev, /* reset */ 16179 &dtrace_cb_ops, /* driver operations */ 16180 NULL, /* bus operations */ 16181 nodev, /* dev power */ 16182 ddi_quiesce_not_needed, /* quiesce */ 16183 }; 16184 16185 static struct modldrv modldrv = { 16186 &mod_driverops, /* module type (this is a pseudo driver) */ 16187 "Dynamic Tracing", /* name of module */ 16188 &dtrace_ops, /* driver ops */ 16189 }; 16190 16191 static struct modlinkage modlinkage = { 16192 MODREV_1, 16193 (void *)&modldrv, 16194 NULL 16195 }; 16196 16197 int 16198 _init(void) 16199 { 16200 return (mod_install(&modlinkage)); 16201 } 16202 16203 int 16204 _info(struct modinfo *modinfop) 16205 { 16206 return (mod_info(&modlinkage, modinfop)); 16207 } 16208 16209 int 16210 _fini(void) 16211 { 16212 return (mod_remove(&modlinkage)); 16213 }