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) 2011, 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 = 32; 123 dtrace_optval_t dtrace_helper_providers_max = 32; 124 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 125 size_t dtrace_strsize_default = 256; 126 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 127 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 128 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 129 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 130 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 131 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 132 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 133 dtrace_optval_t dtrace_nspec_default = 1; 134 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 135 dtrace_optval_t dtrace_stackframes_default = 20; 136 dtrace_optval_t dtrace_ustackframes_default = 20; 137 dtrace_optval_t dtrace_jstackframes_default = 50; 138 dtrace_optval_t dtrace_jstackstrsize_default = 512; 139 int dtrace_msgdsize_max = 128; 140 hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 141 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 142 int dtrace_devdepth_max = 32; 143 int dtrace_err_verbose; 144 hrtime_t dtrace_deadman_interval = NANOSEC; 145 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 146 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 147 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 void *dtrace_softstate; /* softstate pointer */ 174 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 175 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 176 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 177 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 178 static int dtrace_toxranges; /* number of toxic ranges */ 179 static int dtrace_toxranges_max; /* size of toxic range array */ 180 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 181 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 182 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 183 static kthread_t *dtrace_panicked; /* panicking thread */ 184 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 185 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 186 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 187 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 188 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 189 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 190 static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 191 192 /* 193 * DTrace Locking 194 * DTrace is protected by three (relatively coarse-grained) locks: 195 * 196 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 197 * including enabling state, probes, ECBs, consumer state, helper state, 198 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 199 * probe context is lock-free -- synchronization is handled via the 200 * dtrace_sync() cross call mechanism. 201 * 202 * (2) dtrace_provider_lock is required when manipulating provider state, or 203 * when provider state must be held constant. 204 * 205 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 206 * when meta provider state must be held constant. 207 * 208 * The lock ordering between these three locks is dtrace_meta_lock before 209 * dtrace_provider_lock before dtrace_lock. (In particular, there are 210 * several places where dtrace_provider_lock is held by the framework as it 211 * calls into the providers -- which then call back into the framework, 212 * grabbing dtrace_lock.) 213 * 214 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 215 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 216 * role as a coarse-grained lock; it is acquired before both of these locks. 217 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 218 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 219 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 220 * acquired _between_ dtrace_provider_lock and dtrace_lock. 221 */ 222 static kmutex_t dtrace_lock; /* probe state lock */ 223 static kmutex_t dtrace_provider_lock; /* provider state lock */ 224 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 225 226 /* 227 * DTrace Provider Variables 228 * 229 * These are the variables relating to DTrace as a provider (that is, the 230 * provider of the BEGIN, END, and ERROR probes). 231 */ 232 static dtrace_pattr_t dtrace_provider_attr = { 233 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 234 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 235 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 236 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 237 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 238 }; 239 240 static void 241 dtrace_nullop(void) 242 {} 243 244 static int 245 dtrace_enable_nullop(void) 246 { 247 return (0); 248 } 249 250 static dtrace_pops_t dtrace_provider_ops = { 251 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, 252 (void (*)(void *, struct modctl *))dtrace_nullop, 253 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop, 254 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 255 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 256 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 257 NULL, 258 NULL, 259 NULL, 260 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 261 }; 262 263 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 264 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 265 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 266 267 /* 268 * DTrace Helper Tracing Variables 269 */ 270 uint32_t dtrace_helptrace_next = 0; 271 uint32_t dtrace_helptrace_nlocals; 272 char *dtrace_helptrace_buffer; 273 int dtrace_helptrace_bufsize = 512 * 1024; 274 275 #ifdef DEBUG 276 int dtrace_helptrace_enabled = 1; 277 #else 278 int dtrace_helptrace_enabled = 0; 279 #endif 280 281 /* 282 * DTrace Error Hashing 283 * 284 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 285 * table. This is very useful for checking coverage of tests that are 286 * expected to induce DIF or DOF processing errors, and may be useful for 287 * debugging problems in the DIF code generator or in DOF generation . The 288 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 289 */ 290 #ifdef DEBUG 291 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 292 static const char *dtrace_errlast; 293 static kthread_t *dtrace_errthread; 294 static kmutex_t dtrace_errlock; 295 #endif 296 297 /* 298 * DTrace Macros and Constants 299 * 300 * These are various macros that are useful in various spots in the 301 * implementation, along with a few random constants that have no meaning 302 * outside of the implementation. There is no real structure to this cpp 303 * mishmash -- but is there ever? 304 */ 305 #define DTRACE_HASHSTR(hash, probe) \ 306 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 307 308 #define DTRACE_HASHNEXT(hash, probe) \ 309 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 310 311 #define DTRACE_HASHPREV(hash, probe) \ 312 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 313 314 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 315 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 316 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 317 318 #define DTRACE_AGGHASHSIZE_SLEW 17 319 320 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 321 322 /* 323 * The key for a thread-local variable consists of the lower 61 bits of the 324 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 325 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 326 * equal to a variable identifier. This is necessary (but not sufficient) to 327 * assure that global associative arrays never collide with thread-local 328 * variables. To guarantee that they cannot collide, we must also define the 329 * order for keying dynamic variables. That order is: 330 * 331 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 332 * 333 * Because the variable-key and the tls-key are in orthogonal spaces, there is 334 * no way for a global variable key signature to match a thread-local key 335 * signature. 336 */ 337 #define DTRACE_TLS_THRKEY(where) { \ 338 uint_t intr = 0; \ 339 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 340 for (; actv; actv >>= 1) \ 341 intr++; \ 342 ASSERT(intr < (1 << 3)); \ 343 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 344 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 345 } 346 347 #define DT_BSWAP_8(x) ((x) & 0xff) 348 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 349 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 350 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 351 352 #define DT_MASK_LO 0x00000000FFFFFFFFULL 353 354 #define DTRACE_STORE(type, tomax, offset, what) \ 355 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 356 357 #ifndef __i386 358 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 359 if (addr & (size - 1)) { \ 360 *flags |= CPU_DTRACE_BADALIGN; \ 361 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 362 return (0); \ 363 } 364 #else 365 #define DTRACE_ALIGNCHECK(addr, size, flags) 366 #endif 367 368 /* 369 * Test whether a range of memory starting at testaddr of size testsz falls 370 * within the range of memory described by addr, sz. We take care to avoid 371 * problems with overflow and underflow of the unsigned quantities, and 372 * disallow all negative sizes. Ranges of size 0 are allowed. 373 */ 374 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 375 ((testaddr) - (baseaddr) < (basesz) && \ 376 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \ 377 (testaddr) + (testsz) >= (testaddr)) 378 379 /* 380 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 381 * alloc_sz on the righthand side of the comparison in order to avoid overflow 382 * or underflow in the comparison with it. This is simpler than the INRANGE 383 * check above, because we know that the dtms_scratch_ptr is valid in the 384 * range. Allocations of size zero are allowed. 385 */ 386 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 387 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 388 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 389 390 #define DTRACE_LOADFUNC(bits) \ 391 /*CSTYLED*/ \ 392 uint##bits##_t \ 393 dtrace_load##bits(uintptr_t addr) \ 394 { \ 395 size_t size = bits / NBBY; \ 396 /*CSTYLED*/ \ 397 uint##bits##_t rval; \ 398 int i; \ 399 volatile uint16_t *flags = (volatile uint16_t *) \ 400 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ 401 \ 402 DTRACE_ALIGNCHECK(addr, size, flags); \ 403 \ 404 for (i = 0; i < dtrace_toxranges; i++) { \ 405 if (addr >= dtrace_toxrange[i].dtt_limit) \ 406 continue; \ 407 \ 408 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 409 continue; \ 410 \ 411 /* \ 412 * This address falls within a toxic region; return 0. \ 413 */ \ 414 *flags |= CPU_DTRACE_BADADDR; \ 415 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 416 return (0); \ 417 } \ 418 \ 419 *flags |= CPU_DTRACE_NOFAULT; \ 420 /*CSTYLED*/ \ 421 rval = *((volatile uint##bits##_t *)addr); \ 422 *flags &= ~CPU_DTRACE_NOFAULT; \ 423 \ 424 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 425 } 426 427 #ifdef _LP64 428 #define dtrace_loadptr dtrace_load64 429 #else 430 #define dtrace_loadptr dtrace_load32 431 #endif 432 433 #define DTRACE_DYNHASH_FREE 0 434 #define DTRACE_DYNHASH_SINK 1 435 #define DTRACE_DYNHASH_VALID 2 436 437 #define DTRACE_MATCH_FAIL -1 438 #define DTRACE_MATCH_NEXT 0 439 #define DTRACE_MATCH_DONE 1 440 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 441 #define DTRACE_STATE_ALIGN 64 442 443 #define DTRACE_FLAGS2FLT(flags) \ 444 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 445 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 446 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 447 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 448 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 449 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 450 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 451 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 452 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 453 DTRACEFLT_UNKNOWN) 454 455 #define DTRACEACT_ISSTRING(act) \ 456 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 457 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 458 459 static size_t dtrace_strlen(const char *, size_t); 460 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 461 static void dtrace_enabling_provide(dtrace_provider_t *); 462 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 463 static void dtrace_enabling_matchall(void); 464 static void dtrace_enabling_reap(void); 465 static dtrace_state_t *dtrace_anon_grab(void); 466 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 467 dtrace_state_t *, uint64_t, uint64_t); 468 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 469 static void dtrace_buffer_drop(dtrace_buffer_t *); 470 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 471 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 472 dtrace_state_t *, dtrace_mstate_t *); 473 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 474 dtrace_optval_t); 475 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 476 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 477 478 /* 479 * DTrace Probe Context Functions 480 * 481 * These functions are called from probe context. Because probe context is 482 * any context in which C may be called, arbitrarily locks may be held, 483 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 484 * As a result, functions called from probe context may only call other DTrace 485 * support functions -- they may not interact at all with the system at large. 486 * (Note that the ASSERT macro is made probe-context safe by redefining it in 487 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 488 * loads are to be performed from probe context, they _must_ be in terms of 489 * the safe dtrace_load*() variants. 490 * 491 * Some functions in this block are not actually called from probe context; 492 * for these functions, there will be a comment above the function reading 493 * "Note: not called from probe context." 494 */ 495 void 496 dtrace_panic(const char *format, ...) 497 { 498 va_list alist; 499 500 va_start(alist, format); 501 dtrace_vpanic(format, alist); 502 va_end(alist); 503 } 504 505 int 506 dtrace_assfail(const char *a, const char *f, int l) 507 { 508 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 509 510 /* 511 * We just need something here that even the most clever compiler 512 * cannot optimize away. 513 */ 514 return (a[(uintptr_t)f]); 515 } 516 517 /* 518 * Atomically increment a specified error counter from probe context. 519 */ 520 static void 521 dtrace_error(uint32_t *counter) 522 { 523 /* 524 * Most counters stored to in probe context are per-CPU counters. 525 * However, there are some error conditions that are sufficiently 526 * arcane that they don't merit per-CPU storage. If these counters 527 * are incremented concurrently on different CPUs, scalability will be 528 * adversely affected -- but we don't expect them to be white-hot in a 529 * correctly constructed enabling... 530 */ 531 uint32_t oval, nval; 532 533 do { 534 oval = *counter; 535 536 if ((nval = oval + 1) == 0) { 537 /* 538 * If the counter would wrap, set it to 1 -- assuring 539 * that the counter is never zero when we have seen 540 * errors. (The counter must be 32-bits because we 541 * aren't guaranteed a 64-bit compare&swap operation.) 542 * To save this code both the infamy of being fingered 543 * by a priggish news story and the indignity of being 544 * the target of a neo-puritan witch trial, we're 545 * carefully avoiding any colorful description of the 546 * likelihood of this condition -- but suffice it to 547 * say that it is only slightly more likely than the 548 * overflow of predicate cache IDs, as discussed in 549 * dtrace_predicate_create(). 550 */ 551 nval = 1; 552 } 553 } while (dtrace_cas32(counter, oval, nval) != oval); 554 } 555 556 /* 557 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 558 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 559 */ 560 DTRACE_LOADFUNC(8) 561 DTRACE_LOADFUNC(16) 562 DTRACE_LOADFUNC(32) 563 DTRACE_LOADFUNC(64) 564 565 static int 566 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 567 { 568 if (dest < mstate->dtms_scratch_base) 569 return (0); 570 571 if (dest + size < dest) 572 return (0); 573 574 if (dest + size > mstate->dtms_scratch_ptr) 575 return (0); 576 577 return (1); 578 } 579 580 static int 581 dtrace_canstore_statvar(uint64_t addr, size_t sz, 582 dtrace_statvar_t **svars, int nsvars) 583 { 584 int i; 585 586 for (i = 0; i < nsvars; i++) { 587 dtrace_statvar_t *svar = svars[i]; 588 589 if (svar == NULL || svar->dtsv_size == 0) 590 continue; 591 592 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 593 return (1); 594 } 595 596 return (0); 597 } 598 599 /* 600 * Check to see if the address is within a memory region to which a store may 601 * be issued. This includes the DTrace scratch areas, and any DTrace variable 602 * region. The caller of dtrace_canstore() is responsible for performing any 603 * alignment checks that are needed before stores are actually executed. 604 */ 605 static int 606 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 607 dtrace_vstate_t *vstate) 608 { 609 /* 610 * First, check to see if the address is in scratch space... 611 */ 612 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 613 mstate->dtms_scratch_size)) 614 return (1); 615 616 /* 617 * Now check to see if it's a dynamic variable. This check will pick 618 * up both thread-local variables and any global dynamically-allocated 619 * variables. 620 */ 621 if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base, 622 vstate->dtvs_dynvars.dtds_size)) { 623 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 624 uintptr_t base = (uintptr_t)dstate->dtds_base + 625 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 626 uintptr_t chunkoffs; 627 628 /* 629 * Before we assume that we can store here, we need to make 630 * sure that it isn't in our metadata -- storing to our 631 * dynamic variable metadata would corrupt our state. For 632 * the range to not include any dynamic variable metadata, 633 * it must: 634 * 635 * (1) Start above the hash table that is at the base of 636 * the dynamic variable space 637 * 638 * (2) Have a starting chunk offset that is beyond the 639 * dtrace_dynvar_t that is at the base of every chunk 640 * 641 * (3) Not span a chunk boundary 642 * 643 */ 644 if (addr < base) 645 return (0); 646 647 chunkoffs = (addr - base) % dstate->dtds_chunksize; 648 649 if (chunkoffs < sizeof (dtrace_dynvar_t)) 650 return (0); 651 652 if (chunkoffs + sz > dstate->dtds_chunksize) 653 return (0); 654 655 return (1); 656 } 657 658 /* 659 * Finally, check the static local and global variables. These checks 660 * take the longest, so we perform them last. 661 */ 662 if (dtrace_canstore_statvar(addr, sz, 663 vstate->dtvs_locals, vstate->dtvs_nlocals)) 664 return (1); 665 666 if (dtrace_canstore_statvar(addr, sz, 667 vstate->dtvs_globals, vstate->dtvs_nglobals)) 668 return (1); 669 670 return (0); 671 } 672 673 674 /* 675 * Convenience routine to check to see if the address is within a memory 676 * region in which a load may be issued given the user's privilege level; 677 * if not, it sets the appropriate error flags and loads 'addr' into the 678 * illegal value slot. 679 * 680 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 681 * appropriate memory access protection. 682 */ 683 static int 684 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 685 dtrace_vstate_t *vstate) 686 { 687 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 688 689 /* 690 * If we hold the privilege to read from kernel memory, then 691 * everything is readable. 692 */ 693 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 694 return (1); 695 696 /* 697 * You can obviously read that which you can store. 698 */ 699 if (dtrace_canstore(addr, sz, mstate, vstate)) 700 return (1); 701 702 /* 703 * We're allowed to read from our own string table. 704 */ 705 if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab, 706 mstate->dtms_difo->dtdo_strlen)) 707 return (1); 708 709 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 710 *illval = addr; 711 return (0); 712 } 713 714 /* 715 * Convenience routine to check to see if a given string is within a memory 716 * region in which a load may be issued given the user's privilege level; 717 * this exists so that we don't need to issue unnecessary dtrace_strlen() 718 * calls in the event that the user has all privileges. 719 */ 720 static int 721 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 722 dtrace_vstate_t *vstate) 723 { 724 size_t strsz; 725 726 /* 727 * If we hold the privilege to read from kernel memory, then 728 * everything is readable. 729 */ 730 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 731 return (1); 732 733 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 734 if (dtrace_canload(addr, strsz, mstate, vstate)) 735 return (1); 736 737 return (0); 738 } 739 740 /* 741 * Convenience routine to check to see if a given variable is within a memory 742 * region in which a load may be issued given the user's privilege level. 743 */ 744 static int 745 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 746 dtrace_vstate_t *vstate) 747 { 748 size_t sz; 749 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 750 751 /* 752 * If we hold the privilege to read from kernel memory, then 753 * everything is readable. 754 */ 755 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 756 return (1); 757 758 if (type->dtdt_kind == DIF_TYPE_STRING) 759 sz = dtrace_strlen(src, 760 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 761 else 762 sz = type->dtdt_size; 763 764 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 765 } 766 767 /* 768 * Compare two strings using safe loads. 769 */ 770 static int 771 dtrace_strncmp(char *s1, char *s2, size_t limit) 772 { 773 uint8_t c1, c2; 774 volatile uint16_t *flags; 775 776 if (s1 == s2 || limit == 0) 777 return (0); 778 779 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 780 781 do { 782 if (s1 == NULL) { 783 c1 = '\0'; 784 } else { 785 c1 = dtrace_load8((uintptr_t)s1++); 786 } 787 788 if (s2 == NULL) { 789 c2 = '\0'; 790 } else { 791 c2 = dtrace_load8((uintptr_t)s2++); 792 } 793 794 if (c1 != c2) 795 return (c1 - c2); 796 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 797 798 return (0); 799 } 800 801 /* 802 * Compute strlen(s) for a string using safe memory accesses. The additional 803 * len parameter is used to specify a maximum length to ensure completion. 804 */ 805 static size_t 806 dtrace_strlen(const char *s, size_t lim) 807 { 808 uint_t len; 809 810 for (len = 0; len != lim; len++) { 811 if (dtrace_load8((uintptr_t)s++) == '\0') 812 break; 813 } 814 815 return (len); 816 } 817 818 /* 819 * Check if an address falls within a toxic region. 820 */ 821 static int 822 dtrace_istoxic(uintptr_t kaddr, size_t size) 823 { 824 uintptr_t taddr, tsize; 825 int i; 826 827 for (i = 0; i < dtrace_toxranges; i++) { 828 taddr = dtrace_toxrange[i].dtt_base; 829 tsize = dtrace_toxrange[i].dtt_limit - taddr; 830 831 if (kaddr - taddr < tsize) { 832 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 833 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr; 834 return (1); 835 } 836 837 if (taddr - kaddr < size) { 838 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 839 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr; 840 return (1); 841 } 842 } 843 844 return (0); 845 } 846 847 /* 848 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 849 * memory specified by the DIF program. The dst is assumed to be safe memory 850 * that we can store to directly because it is managed by DTrace. As with 851 * standard bcopy, overlapping copies are handled properly. 852 */ 853 static void 854 dtrace_bcopy(const void *src, void *dst, size_t len) 855 { 856 if (len != 0) { 857 uint8_t *s1 = dst; 858 const uint8_t *s2 = src; 859 860 if (s1 <= s2) { 861 do { 862 *s1++ = dtrace_load8((uintptr_t)s2++); 863 } while (--len != 0); 864 } else { 865 s2 += len; 866 s1 += len; 867 868 do { 869 *--s1 = dtrace_load8((uintptr_t)--s2); 870 } while (--len != 0); 871 } 872 } 873 } 874 875 /* 876 * Copy src to dst using safe memory accesses, up to either the specified 877 * length, or the point that a nul byte is encountered. The src is assumed to 878 * be unsafe memory specified by the DIF program. The dst is assumed to be 879 * safe memory that we can store to directly because it is managed by DTrace. 880 * Unlike dtrace_bcopy(), overlapping regions are not handled. 881 */ 882 static void 883 dtrace_strcpy(const void *src, void *dst, size_t len) 884 { 885 if (len != 0) { 886 uint8_t *s1 = dst, c; 887 const uint8_t *s2 = src; 888 889 do { 890 *s1++ = c = dtrace_load8((uintptr_t)s2++); 891 } while (--len != 0 && c != '\0'); 892 } 893 } 894 895 /* 896 * Copy src to dst, deriving the size and type from the specified (BYREF) 897 * variable type. The src is assumed to be unsafe memory specified by the DIF 898 * program. The dst is assumed to be DTrace variable memory that is of the 899 * specified type; we assume that we can store to directly. 900 */ 901 static void 902 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 903 { 904 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 905 906 if (type->dtdt_kind == DIF_TYPE_STRING) { 907 dtrace_strcpy(src, dst, type->dtdt_size); 908 } else { 909 dtrace_bcopy(src, dst, type->dtdt_size); 910 } 911 } 912 913 /* 914 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 915 * unsafe memory specified by the DIF program. The s2 data is assumed to be 916 * safe memory that we can access directly because it is managed by DTrace. 917 */ 918 static int 919 dtrace_bcmp(const void *s1, const void *s2, size_t len) 920 { 921 volatile uint16_t *flags; 922 923 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 924 925 if (s1 == s2) 926 return (0); 927 928 if (s1 == NULL || s2 == NULL) 929 return (1); 930 931 if (s1 != s2 && len != 0) { 932 const uint8_t *ps1 = s1; 933 const uint8_t *ps2 = s2; 934 935 do { 936 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 937 return (1); 938 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 939 } 940 return (0); 941 } 942 943 /* 944 * Zero the specified region using a simple byte-by-byte loop. Note that this 945 * is for safe DTrace-managed memory only. 946 */ 947 static void 948 dtrace_bzero(void *dst, size_t len) 949 { 950 uchar_t *cp; 951 952 for (cp = dst; len != 0; len--) 953 *cp++ = 0; 954 } 955 956 static void 957 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 958 { 959 uint64_t result[2]; 960 961 result[0] = addend1[0] + addend2[0]; 962 result[1] = addend1[1] + addend2[1] + 963 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 964 965 sum[0] = result[0]; 966 sum[1] = result[1]; 967 } 968 969 /* 970 * Shift the 128-bit value in a by b. If b is positive, shift left. 971 * If b is negative, shift right. 972 */ 973 static void 974 dtrace_shift_128(uint64_t *a, int b) 975 { 976 uint64_t mask; 977 978 if (b == 0) 979 return; 980 981 if (b < 0) { 982 b = -b; 983 if (b >= 64) { 984 a[0] = a[1] >> (b - 64); 985 a[1] = 0; 986 } else { 987 a[0] >>= b; 988 mask = 1LL << (64 - b); 989 mask -= 1; 990 a[0] |= ((a[1] & mask) << (64 - b)); 991 a[1] >>= b; 992 } 993 } else { 994 if (b >= 64) { 995 a[1] = a[0] << (b - 64); 996 a[0] = 0; 997 } else { 998 a[1] <<= b; 999 mask = a[0] >> (64 - b); 1000 a[1] |= mask; 1001 a[0] <<= b; 1002 } 1003 } 1004 } 1005 1006 /* 1007 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1008 * use native multiplication on those, and then re-combine into the 1009 * resulting 128-bit value. 1010 * 1011 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1012 * hi1 * hi2 << 64 + 1013 * hi1 * lo2 << 32 + 1014 * hi2 * lo1 << 32 + 1015 * lo1 * lo2 1016 */ 1017 static void 1018 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1019 { 1020 uint64_t hi1, hi2, lo1, lo2; 1021 uint64_t tmp[2]; 1022 1023 hi1 = factor1 >> 32; 1024 hi2 = factor2 >> 32; 1025 1026 lo1 = factor1 & DT_MASK_LO; 1027 lo2 = factor2 & DT_MASK_LO; 1028 1029 product[0] = lo1 * lo2; 1030 product[1] = hi1 * hi2; 1031 1032 tmp[0] = hi1 * lo2; 1033 tmp[1] = 0; 1034 dtrace_shift_128(tmp, 32); 1035 dtrace_add_128(product, tmp, product); 1036 1037 tmp[0] = hi2 * lo1; 1038 tmp[1] = 0; 1039 dtrace_shift_128(tmp, 32); 1040 dtrace_add_128(product, tmp, product); 1041 } 1042 1043 /* 1044 * This privilege check should be used by actions and subroutines to 1045 * verify that the user credentials of the process that enabled the 1046 * invoking ECB match the target credentials 1047 */ 1048 static int 1049 dtrace_priv_proc_common_user(dtrace_state_t *state) 1050 { 1051 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1052 1053 /* 1054 * We should always have a non-NULL state cred here, since if cred 1055 * is null (anonymous tracing), we fast-path bypass this routine. 1056 */ 1057 ASSERT(s_cr != NULL); 1058 1059 if ((cr = CRED()) != NULL && 1060 s_cr->cr_uid == cr->cr_uid && 1061 s_cr->cr_uid == cr->cr_ruid && 1062 s_cr->cr_uid == cr->cr_suid && 1063 s_cr->cr_gid == cr->cr_gid && 1064 s_cr->cr_gid == cr->cr_rgid && 1065 s_cr->cr_gid == cr->cr_sgid) 1066 return (1); 1067 1068 return (0); 1069 } 1070 1071 /* 1072 * This privilege check should be used by actions and subroutines to 1073 * verify that the zone of the process that enabled the invoking ECB 1074 * matches the target credentials 1075 */ 1076 static int 1077 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1078 { 1079 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1080 1081 /* 1082 * We should always have a non-NULL state cred here, since if cred 1083 * is null (anonymous tracing), we fast-path bypass this routine. 1084 */ 1085 ASSERT(s_cr != NULL); 1086 1087 if ((cr = CRED()) != NULL && 1088 s_cr->cr_zone == cr->cr_zone) 1089 return (1); 1090 1091 return (0); 1092 } 1093 1094 /* 1095 * This privilege check should be used by actions and subroutines to 1096 * verify that the process has not setuid or changed credentials. 1097 */ 1098 static int 1099 dtrace_priv_proc_common_nocd() 1100 { 1101 proc_t *proc; 1102 1103 if ((proc = ttoproc(curthread)) != NULL && 1104 !(proc->p_flag & SNOCD)) 1105 return (1); 1106 1107 return (0); 1108 } 1109 1110 static int 1111 dtrace_priv_proc_destructive(dtrace_state_t *state, dtrace_mstate_t *mstate) 1112 { 1113 int action = state->dts_cred.dcr_action; 1114 1115 if (!(mstate->dtms_access & DTRACE_ACCESS_PROC)) 1116 goto bad; 1117 1118 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1119 dtrace_priv_proc_common_zone(state) == 0) 1120 goto bad; 1121 1122 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1123 dtrace_priv_proc_common_user(state) == 0) 1124 goto bad; 1125 1126 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1127 dtrace_priv_proc_common_nocd() == 0) 1128 goto bad; 1129 1130 return (1); 1131 1132 bad: 1133 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1134 1135 return (0); 1136 } 1137 1138 static int 1139 dtrace_priv_proc_control(dtrace_state_t *state, dtrace_mstate_t *mstate) 1140 { 1141 if (mstate->dtms_access & DTRACE_ACCESS_PROC) { 1142 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1143 return (1); 1144 1145 if (dtrace_priv_proc_common_zone(state) && 1146 dtrace_priv_proc_common_user(state) && 1147 dtrace_priv_proc_common_nocd()) 1148 return (1); 1149 } 1150 1151 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1152 1153 return (0); 1154 } 1155 1156 static int 1157 dtrace_priv_proc(dtrace_state_t *state, dtrace_mstate_t *mstate) 1158 { 1159 if ((mstate->dtms_access & DTRACE_ACCESS_PROC) && 1160 (state->dts_cred.dcr_action & DTRACE_CRA_PROC)) 1161 return (1); 1162 1163 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1164 1165 return (0); 1166 } 1167 1168 static int 1169 dtrace_priv_kernel(dtrace_state_t *state) 1170 { 1171 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1172 return (1); 1173 1174 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1175 1176 return (0); 1177 } 1178 1179 static int 1180 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1181 { 1182 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1183 return (1); 1184 1185 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1186 1187 return (0); 1188 } 1189 1190 /* 1191 * Determine if the dte_cond of the specified ECB allows for processing of 1192 * the current probe to continue. Note that this routine may allow continued 1193 * processing, but with access(es) stripped from the mstate's dtms_access 1194 * field. 1195 */ 1196 static int 1197 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1198 dtrace_ecb_t *ecb) 1199 { 1200 dtrace_probe_t *probe = ecb->dte_probe; 1201 dtrace_provider_t *prov = probe->dtpr_provider; 1202 dtrace_pops_t *pops = &prov->dtpv_pops; 1203 int mode = DTRACE_MODE_NOPRIV_DROP; 1204 1205 ASSERT(ecb->dte_cond); 1206 1207 if (pops->dtps_mode != NULL) { 1208 mode = pops->dtps_mode(prov->dtpv_arg, 1209 probe->dtpr_id, probe->dtpr_arg); 1210 1211 ASSERT((mode & DTRACE_MODE_USER) || 1212 (mode & DTRACE_MODE_KERNEL)); 1213 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1214 (mode & DTRACE_MODE_NOPRIV_DROP)); 1215 } 1216 1217 /* 1218 * If the dte_cond bits indicate that this consumer is only allowed to 1219 * see user-mode firings of this probe, call the provider's dtps_mode() 1220 * entry point to check that the probe was fired while in a user 1221 * context. If that's not the case, use the policy specified by the 1222 * provider to determine if we drop the probe or merely restrict 1223 * operation. 1224 */ 1225 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1226 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1227 1228 if (!(mode & DTRACE_MODE_USER)) { 1229 if (mode & DTRACE_MODE_NOPRIV_DROP) 1230 return (0); 1231 1232 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1233 } 1234 } 1235 1236 /* 1237 * This is more subtle than it looks. We have to be absolutely certain 1238 * that CRED() isn't going to change out from under us so it's only 1239 * legit to examine that structure if we're in constrained situations. 1240 * Currently, the only times we'll this check is if a non-super-user 1241 * has enabled the profile or syscall providers -- providers that 1242 * allow visibility of all processes. For the profile case, the check 1243 * above will ensure that we're examining a user context. 1244 */ 1245 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1246 cred_t *cr; 1247 cred_t *s_cr = state->dts_cred.dcr_cred; 1248 proc_t *proc; 1249 1250 ASSERT(s_cr != NULL); 1251 1252 if ((cr = CRED()) == NULL || 1253 s_cr->cr_uid != cr->cr_uid || 1254 s_cr->cr_uid != cr->cr_ruid || 1255 s_cr->cr_uid != cr->cr_suid || 1256 s_cr->cr_gid != cr->cr_gid || 1257 s_cr->cr_gid != cr->cr_rgid || 1258 s_cr->cr_gid != cr->cr_sgid || 1259 (proc = ttoproc(curthread)) == NULL || 1260 (proc->p_flag & SNOCD)) { 1261 if (mode & DTRACE_MODE_NOPRIV_DROP) 1262 return (0); 1263 1264 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1265 } 1266 } 1267 1268 /* 1269 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1270 * in our zone, check to see if our mode policy is to restrict rather 1271 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1272 * and DTRACE_ACCESS_ARGS 1273 */ 1274 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1275 cred_t *cr; 1276 cred_t *s_cr = state->dts_cred.dcr_cred; 1277 1278 ASSERT(s_cr != NULL); 1279 1280 if ((cr = CRED()) == NULL || 1281 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1282 if (mode & DTRACE_MODE_NOPRIV_DROP) 1283 return (0); 1284 1285 mstate->dtms_access &= 1286 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1287 } 1288 } 1289 1290 return (1); 1291 } 1292 1293 /* 1294 * Note: not called from probe context. This function is called 1295 * asynchronously (and at a regular interval) from outside of probe context to 1296 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1297 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1298 */ 1299 void 1300 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1301 { 1302 dtrace_dynvar_t *dirty; 1303 dtrace_dstate_percpu_t *dcpu; 1304 dtrace_dynvar_t **rinsep; 1305 int i, j, work = 0; 1306 1307 for (i = 0; i < NCPU; i++) { 1308 dcpu = &dstate->dtds_percpu[i]; 1309 rinsep = &dcpu->dtdsc_rinsing; 1310 1311 /* 1312 * If the dirty list is NULL, there is no dirty work to do. 1313 */ 1314 if (dcpu->dtdsc_dirty == NULL) 1315 continue; 1316 1317 if (dcpu->dtdsc_rinsing != NULL) { 1318 /* 1319 * If the rinsing list is non-NULL, then it is because 1320 * this CPU was selected to accept another CPU's 1321 * dirty list -- and since that time, dirty buffers 1322 * have accumulated. This is a highly unlikely 1323 * condition, but we choose to ignore the dirty 1324 * buffers -- they'll be picked up a future cleanse. 1325 */ 1326 continue; 1327 } 1328 1329 if (dcpu->dtdsc_clean != NULL) { 1330 /* 1331 * If the clean list is non-NULL, then we're in a 1332 * situation where a CPU has done deallocations (we 1333 * have a non-NULL dirty list) but no allocations (we 1334 * also have a non-NULL clean list). We can't simply 1335 * move the dirty list into the clean list on this 1336 * CPU, yet we also don't want to allow this condition 1337 * to persist, lest a short clean list prevent a 1338 * massive dirty list from being cleaned (which in 1339 * turn could lead to otherwise avoidable dynamic 1340 * drops). To deal with this, we look for some CPU 1341 * with a NULL clean list, NULL dirty list, and NULL 1342 * rinsing list -- and then we borrow this CPU to 1343 * rinse our dirty list. 1344 */ 1345 for (j = 0; j < NCPU; j++) { 1346 dtrace_dstate_percpu_t *rinser; 1347 1348 rinser = &dstate->dtds_percpu[j]; 1349 1350 if (rinser->dtdsc_rinsing != NULL) 1351 continue; 1352 1353 if (rinser->dtdsc_dirty != NULL) 1354 continue; 1355 1356 if (rinser->dtdsc_clean != NULL) 1357 continue; 1358 1359 rinsep = &rinser->dtdsc_rinsing; 1360 break; 1361 } 1362 1363 if (j == NCPU) { 1364 /* 1365 * We were unable to find another CPU that 1366 * could accept this dirty list -- we are 1367 * therefore unable to clean it now. 1368 */ 1369 dtrace_dynvar_failclean++; 1370 continue; 1371 } 1372 } 1373 1374 work = 1; 1375 1376 /* 1377 * Atomically move the dirty list aside. 1378 */ 1379 do { 1380 dirty = dcpu->dtdsc_dirty; 1381 1382 /* 1383 * Before we zap the dirty list, set the rinsing list. 1384 * (This allows for a potential assertion in 1385 * dtrace_dynvar(): if a free dynamic variable appears 1386 * on a hash chain, either the dirty list or the 1387 * rinsing list for some CPU must be non-NULL.) 1388 */ 1389 *rinsep = dirty; 1390 dtrace_membar_producer(); 1391 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1392 dirty, NULL) != dirty); 1393 } 1394 1395 if (!work) { 1396 /* 1397 * We have no work to do; we can simply return. 1398 */ 1399 return; 1400 } 1401 1402 dtrace_sync(); 1403 1404 for (i = 0; i < NCPU; i++) { 1405 dcpu = &dstate->dtds_percpu[i]; 1406 1407 if (dcpu->dtdsc_rinsing == NULL) 1408 continue; 1409 1410 /* 1411 * We are now guaranteed that no hash chain contains a pointer 1412 * into this dirty list; we can make it clean. 1413 */ 1414 ASSERT(dcpu->dtdsc_clean == NULL); 1415 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1416 dcpu->dtdsc_rinsing = NULL; 1417 } 1418 1419 /* 1420 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1421 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1422 * This prevents a race whereby a CPU incorrectly decides that 1423 * the state should be something other than DTRACE_DSTATE_CLEAN 1424 * after dtrace_dynvar_clean() has completed. 1425 */ 1426 dtrace_sync(); 1427 1428 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1429 } 1430 1431 /* 1432 * Depending on the value of the op parameter, this function looks-up, 1433 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1434 * allocation is requested, this function will return a pointer to a 1435 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1436 * variable can be allocated. If NULL is returned, the appropriate counter 1437 * will be incremented. 1438 */ 1439 dtrace_dynvar_t * 1440 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1441 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1442 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1443 { 1444 uint64_t hashval = DTRACE_DYNHASH_VALID; 1445 dtrace_dynhash_t *hash = dstate->dtds_hash; 1446 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1447 processorid_t me = CPU->cpu_id, cpu = me; 1448 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1449 size_t bucket, ksize; 1450 size_t chunksize = dstate->dtds_chunksize; 1451 uintptr_t kdata, lock, nstate; 1452 uint_t i; 1453 1454 ASSERT(nkeys != 0); 1455 1456 /* 1457 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1458 * algorithm. For the by-value portions, we perform the algorithm in 1459 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1460 * bit, and seems to have only a minute effect on distribution. For 1461 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1462 * over each referenced byte. It's painful to do this, but it's much 1463 * better than pathological hash distribution. The efficacy of the 1464 * hashing algorithm (and a comparison with other algorithms) may be 1465 * found by running the ::dtrace_dynstat MDB dcmd. 1466 */ 1467 for (i = 0; i < nkeys; i++) { 1468 if (key[i].dttk_size == 0) { 1469 uint64_t val = key[i].dttk_value; 1470 1471 hashval += (val >> 48) & 0xffff; 1472 hashval += (hashval << 10); 1473 hashval ^= (hashval >> 6); 1474 1475 hashval += (val >> 32) & 0xffff; 1476 hashval += (hashval << 10); 1477 hashval ^= (hashval >> 6); 1478 1479 hashval += (val >> 16) & 0xffff; 1480 hashval += (hashval << 10); 1481 hashval ^= (hashval >> 6); 1482 1483 hashval += val & 0xffff; 1484 hashval += (hashval << 10); 1485 hashval ^= (hashval >> 6); 1486 } else { 1487 /* 1488 * This is incredibly painful, but it beats the hell 1489 * out of the alternative. 1490 */ 1491 uint64_t j, size = key[i].dttk_size; 1492 uintptr_t base = (uintptr_t)key[i].dttk_value; 1493 1494 if (!dtrace_canload(base, size, mstate, vstate)) 1495 break; 1496 1497 for (j = 0; j < size; j++) { 1498 hashval += dtrace_load8(base + j); 1499 hashval += (hashval << 10); 1500 hashval ^= (hashval >> 6); 1501 } 1502 } 1503 } 1504 1505 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1506 return (NULL); 1507 1508 hashval += (hashval << 3); 1509 hashval ^= (hashval >> 11); 1510 hashval += (hashval << 15); 1511 1512 /* 1513 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1514 * comes out to be one of our two sentinel hash values. If this 1515 * actually happens, we set the hashval to be a value known to be a 1516 * non-sentinel value. 1517 */ 1518 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1519 hashval = DTRACE_DYNHASH_VALID; 1520 1521 /* 1522 * Yes, it's painful to do a divide here. If the cycle count becomes 1523 * important here, tricks can be pulled to reduce it. (However, it's 1524 * critical that hash collisions be kept to an absolute minimum; 1525 * they're much more painful than a divide.) It's better to have a 1526 * solution that generates few collisions and still keeps things 1527 * relatively simple. 1528 */ 1529 bucket = hashval % dstate->dtds_hashsize; 1530 1531 if (op == DTRACE_DYNVAR_DEALLOC) { 1532 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1533 1534 for (;;) { 1535 while ((lock = *lockp) & 1) 1536 continue; 1537 1538 if (dtrace_casptr((void *)lockp, 1539 (void *)lock, (void *)(lock + 1)) == (void *)lock) 1540 break; 1541 } 1542 1543 dtrace_membar_producer(); 1544 } 1545 1546 top: 1547 prev = NULL; 1548 lock = hash[bucket].dtdh_lock; 1549 1550 dtrace_membar_consumer(); 1551 1552 start = hash[bucket].dtdh_chain; 1553 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1554 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1555 op != DTRACE_DYNVAR_DEALLOC)); 1556 1557 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1558 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1559 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1560 1561 if (dvar->dtdv_hashval != hashval) { 1562 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1563 /* 1564 * We've reached the sink, and therefore the 1565 * end of the hash chain; we can kick out of 1566 * the loop knowing that we have seen a valid 1567 * snapshot of state. 1568 */ 1569 ASSERT(dvar->dtdv_next == NULL); 1570 ASSERT(dvar == &dtrace_dynhash_sink); 1571 break; 1572 } 1573 1574 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1575 /* 1576 * We've gone off the rails: somewhere along 1577 * the line, one of the members of this hash 1578 * chain was deleted. Note that we could also 1579 * detect this by simply letting this loop run 1580 * to completion, as we would eventually hit 1581 * the end of the dirty list. However, we 1582 * want to avoid running the length of the 1583 * dirty list unnecessarily (it might be quite 1584 * long), so we catch this as early as 1585 * possible by detecting the hash marker. In 1586 * this case, we simply set dvar to NULL and 1587 * break; the conditional after the loop will 1588 * send us back to top. 1589 */ 1590 dvar = NULL; 1591 break; 1592 } 1593 1594 goto next; 1595 } 1596 1597 if (dtuple->dtt_nkeys != nkeys) 1598 goto next; 1599 1600 for (i = 0; i < nkeys; i++, dkey++) { 1601 if (dkey->dttk_size != key[i].dttk_size) 1602 goto next; /* size or type mismatch */ 1603 1604 if (dkey->dttk_size != 0) { 1605 if (dtrace_bcmp( 1606 (void *)(uintptr_t)key[i].dttk_value, 1607 (void *)(uintptr_t)dkey->dttk_value, 1608 dkey->dttk_size)) 1609 goto next; 1610 } else { 1611 if (dkey->dttk_value != key[i].dttk_value) 1612 goto next; 1613 } 1614 } 1615 1616 if (op != DTRACE_DYNVAR_DEALLOC) 1617 return (dvar); 1618 1619 ASSERT(dvar->dtdv_next == NULL || 1620 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1621 1622 if (prev != NULL) { 1623 ASSERT(hash[bucket].dtdh_chain != dvar); 1624 ASSERT(start != dvar); 1625 ASSERT(prev->dtdv_next == dvar); 1626 prev->dtdv_next = dvar->dtdv_next; 1627 } else { 1628 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1629 start, dvar->dtdv_next) != start) { 1630 /* 1631 * We have failed to atomically swing the 1632 * hash table head pointer, presumably because 1633 * of a conflicting allocation on another CPU. 1634 * We need to reread the hash chain and try 1635 * again. 1636 */ 1637 goto top; 1638 } 1639 } 1640 1641 dtrace_membar_producer(); 1642 1643 /* 1644 * Now set the hash value to indicate that it's free. 1645 */ 1646 ASSERT(hash[bucket].dtdh_chain != dvar); 1647 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1648 1649 dtrace_membar_producer(); 1650 1651 /* 1652 * Set the next pointer to point at the dirty list, and 1653 * atomically swing the dirty pointer to the newly freed dvar. 1654 */ 1655 do { 1656 next = dcpu->dtdsc_dirty; 1657 dvar->dtdv_next = next; 1658 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1659 1660 /* 1661 * Finally, unlock this hash bucket. 1662 */ 1663 ASSERT(hash[bucket].dtdh_lock == lock); 1664 ASSERT(lock & 1); 1665 hash[bucket].dtdh_lock++; 1666 1667 return (NULL); 1668 next: 1669 prev = dvar; 1670 continue; 1671 } 1672 1673 if (dvar == NULL) { 1674 /* 1675 * If dvar is NULL, it is because we went off the rails: 1676 * one of the elements that we traversed in the hash chain 1677 * was deleted while we were traversing it. In this case, 1678 * we assert that we aren't doing a dealloc (deallocs lock 1679 * the hash bucket to prevent themselves from racing with 1680 * one another), and retry the hash chain traversal. 1681 */ 1682 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1683 goto top; 1684 } 1685 1686 if (op != DTRACE_DYNVAR_ALLOC) { 1687 /* 1688 * If we are not to allocate a new variable, we want to 1689 * return NULL now. Before we return, check that the value 1690 * of the lock word hasn't changed. If it has, we may have 1691 * seen an inconsistent snapshot. 1692 */ 1693 if (op == DTRACE_DYNVAR_NOALLOC) { 1694 if (hash[bucket].dtdh_lock != lock) 1695 goto top; 1696 } else { 1697 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1698 ASSERT(hash[bucket].dtdh_lock == lock); 1699 ASSERT(lock & 1); 1700 hash[bucket].dtdh_lock++; 1701 } 1702 1703 return (NULL); 1704 } 1705 1706 /* 1707 * We need to allocate a new dynamic variable. The size we need is the 1708 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1709 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1710 * the size of any referred-to data (dsize). We then round the final 1711 * size up to the chunksize for allocation. 1712 */ 1713 for (ksize = 0, i = 0; i < nkeys; i++) 1714 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1715 1716 /* 1717 * This should be pretty much impossible, but could happen if, say, 1718 * strange DIF specified the tuple. Ideally, this should be an 1719 * assertion and not an error condition -- but that requires that the 1720 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1721 * bullet-proof. (That is, it must not be able to be fooled by 1722 * malicious DIF.) Given the lack of backwards branches in DIF, 1723 * solving this would presumably not amount to solving the Halting 1724 * Problem -- but it still seems awfully hard. 1725 */ 1726 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1727 ksize + dsize > chunksize) { 1728 dcpu->dtdsc_drops++; 1729 return (NULL); 1730 } 1731 1732 nstate = DTRACE_DSTATE_EMPTY; 1733 1734 do { 1735 retry: 1736 free = dcpu->dtdsc_free; 1737 1738 if (free == NULL) { 1739 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1740 void *rval; 1741 1742 if (clean == NULL) { 1743 /* 1744 * We're out of dynamic variable space on 1745 * this CPU. Unless we have tried all CPUs, 1746 * we'll try to allocate from a different 1747 * CPU. 1748 */ 1749 switch (dstate->dtds_state) { 1750 case DTRACE_DSTATE_CLEAN: { 1751 void *sp = &dstate->dtds_state; 1752 1753 if (++cpu >= NCPU) 1754 cpu = 0; 1755 1756 if (dcpu->dtdsc_dirty != NULL && 1757 nstate == DTRACE_DSTATE_EMPTY) 1758 nstate = DTRACE_DSTATE_DIRTY; 1759 1760 if (dcpu->dtdsc_rinsing != NULL) 1761 nstate = DTRACE_DSTATE_RINSING; 1762 1763 dcpu = &dstate->dtds_percpu[cpu]; 1764 1765 if (cpu != me) 1766 goto retry; 1767 1768 (void) dtrace_cas32(sp, 1769 DTRACE_DSTATE_CLEAN, nstate); 1770 1771 /* 1772 * To increment the correct bean 1773 * counter, take another lap. 1774 */ 1775 goto retry; 1776 } 1777 1778 case DTRACE_DSTATE_DIRTY: 1779 dcpu->dtdsc_dirty_drops++; 1780 break; 1781 1782 case DTRACE_DSTATE_RINSING: 1783 dcpu->dtdsc_rinsing_drops++; 1784 break; 1785 1786 case DTRACE_DSTATE_EMPTY: 1787 dcpu->dtdsc_drops++; 1788 break; 1789 } 1790 1791 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1792 return (NULL); 1793 } 1794 1795 /* 1796 * The clean list appears to be non-empty. We want to 1797 * move the clean list to the free list; we start by 1798 * moving the clean pointer aside. 1799 */ 1800 if (dtrace_casptr(&dcpu->dtdsc_clean, 1801 clean, NULL) != clean) { 1802 /* 1803 * We are in one of two situations: 1804 * 1805 * (a) The clean list was switched to the 1806 * free list by another CPU. 1807 * 1808 * (b) The clean list was added to by the 1809 * cleansing cyclic. 1810 * 1811 * In either of these situations, we can 1812 * just reattempt the free list allocation. 1813 */ 1814 goto retry; 1815 } 1816 1817 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1818 1819 /* 1820 * Now we'll move the clean list to our free list. 1821 * It's impossible for this to fail: the only way 1822 * the free list can be updated is through this 1823 * code path, and only one CPU can own the clean list. 1824 * Thus, it would only be possible for this to fail if 1825 * this code were racing with dtrace_dynvar_clean(). 1826 * (That is, if dtrace_dynvar_clean() updated the clean 1827 * list, and we ended up racing to update the free 1828 * list.) This race is prevented by the dtrace_sync() 1829 * in dtrace_dynvar_clean() -- which flushes the 1830 * owners of the clean lists out before resetting 1831 * the clean lists. 1832 */ 1833 dcpu = &dstate->dtds_percpu[me]; 1834 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1835 ASSERT(rval == NULL); 1836 goto retry; 1837 } 1838 1839 dvar = free; 1840 new_free = dvar->dtdv_next; 1841 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1842 1843 /* 1844 * We have now allocated a new chunk. We copy the tuple keys into the 1845 * tuple array and copy any referenced key data into the data space 1846 * following the tuple array. As we do this, we relocate dttk_value 1847 * in the final tuple to point to the key data address in the chunk. 1848 */ 1849 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1850 dvar->dtdv_data = (void *)(kdata + ksize); 1851 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1852 1853 for (i = 0; i < nkeys; i++) { 1854 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1855 size_t kesize = key[i].dttk_size; 1856 1857 if (kesize != 0) { 1858 dtrace_bcopy( 1859 (const void *)(uintptr_t)key[i].dttk_value, 1860 (void *)kdata, kesize); 1861 dkey->dttk_value = kdata; 1862 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1863 } else { 1864 dkey->dttk_value = key[i].dttk_value; 1865 } 1866 1867 dkey->dttk_size = kesize; 1868 } 1869 1870 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1871 dvar->dtdv_hashval = hashval; 1872 dvar->dtdv_next = start; 1873 1874 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1875 return (dvar); 1876 1877 /* 1878 * The cas has failed. Either another CPU is adding an element to 1879 * this hash chain, or another CPU is deleting an element from this 1880 * hash chain. The simplest way to deal with both of these cases 1881 * (though not necessarily the most efficient) is to free our 1882 * allocated block and tail-call ourselves. Note that the free is 1883 * to the dirty list and _not_ to the free list. This is to prevent 1884 * races with allocators, above. 1885 */ 1886 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1887 1888 dtrace_membar_producer(); 1889 1890 do { 1891 free = dcpu->dtdsc_dirty; 1892 dvar->dtdv_next = free; 1893 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1894 1895 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 1896 } 1897 1898 /*ARGSUSED*/ 1899 static void 1900 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1901 { 1902 if ((int64_t)nval < (int64_t)*oval) 1903 *oval = nval; 1904 } 1905 1906 /*ARGSUSED*/ 1907 static void 1908 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1909 { 1910 if ((int64_t)nval > (int64_t)*oval) 1911 *oval = nval; 1912 } 1913 1914 static void 1915 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1916 { 1917 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1918 int64_t val = (int64_t)nval; 1919 1920 if (val < 0) { 1921 for (i = 0; i < zero; i++) { 1922 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1923 quanta[i] += incr; 1924 return; 1925 } 1926 } 1927 } else { 1928 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1929 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1930 quanta[i - 1] += incr; 1931 return; 1932 } 1933 } 1934 1935 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1936 return; 1937 } 1938 1939 ASSERT(0); 1940 } 1941 1942 static void 1943 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1944 { 1945 uint64_t arg = *lquanta++; 1946 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1947 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1948 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1949 int32_t val = (int32_t)nval, level; 1950 1951 ASSERT(step != 0); 1952 ASSERT(levels != 0); 1953 1954 if (val < base) { 1955 /* 1956 * This is an underflow. 1957 */ 1958 lquanta[0] += incr; 1959 return; 1960 } 1961 1962 level = (val - base) / step; 1963 1964 if (level < levels) { 1965 lquanta[level + 1] += incr; 1966 return; 1967 } 1968 1969 /* 1970 * This is an overflow. 1971 */ 1972 lquanta[levels + 1] += incr; 1973 } 1974 1975 static int 1976 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 1977 uint16_t high, uint16_t nsteps, int64_t value) 1978 { 1979 int64_t this = 1, last, next; 1980 int base = 1, order; 1981 1982 ASSERT(factor <= nsteps); 1983 ASSERT(nsteps % factor == 0); 1984 1985 for (order = 0; order < low; order++) 1986 this *= factor; 1987 1988 /* 1989 * If our value is less than our factor taken to the power of the 1990 * low order of magnitude, it goes into the zeroth bucket. 1991 */ 1992 if (value < (last = this)) 1993 return (0); 1994 1995 for (this *= factor; order <= high; order++) { 1996 int nbuckets = this > nsteps ? nsteps : this; 1997 1998 if ((next = this * factor) < this) { 1999 /* 2000 * We should not generally get log/linear quantizations 2001 * with a high magnitude that allows 64-bits to 2002 * overflow, but we nonetheless protect against this 2003 * by explicitly checking for overflow, and clamping 2004 * our value accordingly. 2005 */ 2006 value = this - 1; 2007 } 2008 2009 if (value < this) { 2010 /* 2011 * If our value lies within this order of magnitude, 2012 * determine its position by taking the offset within 2013 * the order of magnitude, dividing by the bucket 2014 * width, and adding to our (accumulated) base. 2015 */ 2016 return (base + (value - last) / (this / nbuckets)); 2017 } 2018 2019 base += nbuckets - (nbuckets / factor); 2020 last = this; 2021 this = next; 2022 } 2023 2024 /* 2025 * Our value is greater than or equal to our factor taken to the 2026 * power of one plus the high magnitude -- return the top bucket. 2027 */ 2028 return (base); 2029 } 2030 2031 static void 2032 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2033 { 2034 uint64_t arg = *llquanta++; 2035 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2036 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2037 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2038 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2039 2040 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2041 low, high, nsteps, nval)] += incr; 2042 } 2043 2044 /*ARGSUSED*/ 2045 static void 2046 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2047 { 2048 data[0]++; 2049 data[1] += nval; 2050 } 2051 2052 /*ARGSUSED*/ 2053 static void 2054 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2055 { 2056 int64_t snval = (int64_t)nval; 2057 uint64_t tmp[2]; 2058 2059 data[0]++; 2060 data[1] += nval; 2061 2062 /* 2063 * What we want to say here is: 2064 * 2065 * data[2] += nval * nval; 2066 * 2067 * But given that nval is 64-bit, we could easily overflow, so 2068 * we do this as 128-bit arithmetic. 2069 */ 2070 if (snval < 0) 2071 snval = -snval; 2072 2073 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2074 dtrace_add_128(data + 2, tmp, data + 2); 2075 } 2076 2077 /*ARGSUSED*/ 2078 static void 2079 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2080 { 2081 *oval = *oval + 1; 2082 } 2083 2084 /*ARGSUSED*/ 2085 static void 2086 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2087 { 2088 *oval += nval; 2089 } 2090 2091 /* 2092 * Aggregate given the tuple in the principal data buffer, and the aggregating 2093 * action denoted by the specified dtrace_aggregation_t. The aggregation 2094 * buffer is specified as the buf parameter. This routine does not return 2095 * failure; if there is no space in the aggregation buffer, the data will be 2096 * dropped, and a corresponding counter incremented. 2097 */ 2098 static void 2099 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2100 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2101 { 2102 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2103 uint32_t i, ndx, size, fsize; 2104 uint32_t align = sizeof (uint64_t) - 1; 2105 dtrace_aggbuffer_t *agb; 2106 dtrace_aggkey_t *key; 2107 uint32_t hashval = 0, limit, isstr; 2108 caddr_t tomax, data, kdata; 2109 dtrace_actkind_t action; 2110 dtrace_action_t *act; 2111 uintptr_t offs; 2112 2113 if (buf == NULL) 2114 return; 2115 2116 if (!agg->dtag_hasarg) { 2117 /* 2118 * Currently, only quantize() and lquantize() take additional 2119 * arguments, and they have the same semantics: an increment 2120 * value that defaults to 1 when not present. If additional 2121 * aggregating actions take arguments, the setting of the 2122 * default argument value will presumably have to become more 2123 * sophisticated... 2124 */ 2125 arg = 1; 2126 } 2127 2128 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2129 size = rec->dtrd_offset - agg->dtag_base; 2130 fsize = size + rec->dtrd_size; 2131 2132 ASSERT(dbuf->dtb_tomax != NULL); 2133 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2134 2135 if ((tomax = buf->dtb_tomax) == NULL) { 2136 dtrace_buffer_drop(buf); 2137 return; 2138 } 2139 2140 /* 2141 * The metastructure is always at the bottom of the buffer. 2142 */ 2143 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2144 sizeof (dtrace_aggbuffer_t)); 2145 2146 if (buf->dtb_offset == 0) { 2147 /* 2148 * We just kludge up approximately 1/8th of the size to be 2149 * buckets. If this guess ends up being routinely 2150 * off-the-mark, we may need to dynamically readjust this 2151 * based on past performance. 2152 */ 2153 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2154 2155 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2156 (uintptr_t)tomax || hashsize == 0) { 2157 /* 2158 * We've been given a ludicrously small buffer; 2159 * increment our drop count and leave. 2160 */ 2161 dtrace_buffer_drop(buf); 2162 return; 2163 } 2164 2165 /* 2166 * And now, a pathetic attempt to try to get a an odd (or 2167 * perchance, a prime) hash size for better hash distribution. 2168 */ 2169 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2170 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2171 2172 agb->dtagb_hashsize = hashsize; 2173 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2174 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2175 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2176 2177 for (i = 0; i < agb->dtagb_hashsize; i++) 2178 agb->dtagb_hash[i] = NULL; 2179 } 2180 2181 ASSERT(agg->dtag_first != NULL); 2182 ASSERT(agg->dtag_first->dta_intuple); 2183 2184 /* 2185 * Calculate the hash value based on the key. Note that we _don't_ 2186 * include the aggid in the hashing (but we will store it as part of 2187 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2188 * algorithm: a simple, quick algorithm that has no known funnels, and 2189 * gets good distribution in practice. The efficacy of the hashing 2190 * algorithm (and a comparison with other algorithms) may be found by 2191 * running the ::dtrace_aggstat MDB dcmd. 2192 */ 2193 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2194 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2195 limit = i + act->dta_rec.dtrd_size; 2196 ASSERT(limit <= size); 2197 isstr = DTRACEACT_ISSTRING(act); 2198 2199 for (; i < limit; i++) { 2200 hashval += data[i]; 2201 hashval += (hashval << 10); 2202 hashval ^= (hashval >> 6); 2203 2204 if (isstr && data[i] == '\0') 2205 break; 2206 } 2207 } 2208 2209 hashval += (hashval << 3); 2210 hashval ^= (hashval >> 11); 2211 hashval += (hashval << 15); 2212 2213 /* 2214 * Yes, the divide here is expensive -- but it's generally the least 2215 * of the performance issues given the amount of data that we iterate 2216 * over to compute hash values, compare data, etc. 2217 */ 2218 ndx = hashval % agb->dtagb_hashsize; 2219 2220 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2221 ASSERT((caddr_t)key >= tomax); 2222 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2223 2224 if (hashval != key->dtak_hashval || key->dtak_size != size) 2225 continue; 2226 2227 kdata = key->dtak_data; 2228 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2229 2230 for (act = agg->dtag_first; act->dta_intuple; 2231 act = act->dta_next) { 2232 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2233 limit = i + act->dta_rec.dtrd_size; 2234 ASSERT(limit <= size); 2235 isstr = DTRACEACT_ISSTRING(act); 2236 2237 for (; i < limit; i++) { 2238 if (kdata[i] != data[i]) 2239 goto next; 2240 2241 if (isstr && data[i] == '\0') 2242 break; 2243 } 2244 } 2245 2246 if (action != key->dtak_action) { 2247 /* 2248 * We are aggregating on the same value in the same 2249 * aggregation with two different aggregating actions. 2250 * (This should have been picked up in the compiler, 2251 * so we may be dealing with errant or devious DIF.) 2252 * This is an error condition; we indicate as much, 2253 * and return. 2254 */ 2255 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2256 return; 2257 } 2258 2259 /* 2260 * This is a hit: we need to apply the aggregator to 2261 * the value at this key. 2262 */ 2263 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2264 return; 2265 next: 2266 continue; 2267 } 2268 2269 /* 2270 * We didn't find it. We need to allocate some zero-filled space, 2271 * link it into the hash table appropriately, and apply the aggregator 2272 * to the (zero-filled) value. 2273 */ 2274 offs = buf->dtb_offset; 2275 while (offs & (align - 1)) 2276 offs += sizeof (uint32_t); 2277 2278 /* 2279 * If we don't have enough room to both allocate a new key _and_ 2280 * its associated data, increment the drop count and return. 2281 */ 2282 if ((uintptr_t)tomax + offs + fsize > 2283 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2284 dtrace_buffer_drop(buf); 2285 return; 2286 } 2287 2288 /*CONSTCOND*/ 2289 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2290 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2291 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2292 2293 key->dtak_data = kdata = tomax + offs; 2294 buf->dtb_offset = offs + fsize; 2295 2296 /* 2297 * Now copy the data across. 2298 */ 2299 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2300 2301 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2302 kdata[i] = data[i]; 2303 2304 /* 2305 * Because strings are not zeroed out by default, we need to iterate 2306 * looking for actions that store strings, and we need to explicitly 2307 * pad these strings out with zeroes. 2308 */ 2309 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2310 int nul; 2311 2312 if (!DTRACEACT_ISSTRING(act)) 2313 continue; 2314 2315 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2316 limit = i + act->dta_rec.dtrd_size; 2317 ASSERT(limit <= size); 2318 2319 for (nul = 0; i < limit; i++) { 2320 if (nul) { 2321 kdata[i] = '\0'; 2322 continue; 2323 } 2324 2325 if (data[i] != '\0') 2326 continue; 2327 2328 nul = 1; 2329 } 2330 } 2331 2332 for (i = size; i < fsize; i++) 2333 kdata[i] = 0; 2334 2335 key->dtak_hashval = hashval; 2336 key->dtak_size = size; 2337 key->dtak_action = action; 2338 key->dtak_next = agb->dtagb_hash[ndx]; 2339 agb->dtagb_hash[ndx] = key; 2340 2341 /* 2342 * Finally, apply the aggregator. 2343 */ 2344 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2345 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2346 } 2347 2348 /* 2349 * Given consumer state, this routine finds a speculation in the INACTIVE 2350 * state and transitions it into the ACTIVE state. If there is no speculation 2351 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2352 * incremented -- it is up to the caller to take appropriate action. 2353 */ 2354 static int 2355 dtrace_speculation(dtrace_state_t *state) 2356 { 2357 int i = 0; 2358 dtrace_speculation_state_t current; 2359 uint32_t *stat = &state->dts_speculations_unavail, count; 2360 2361 while (i < state->dts_nspeculations) { 2362 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2363 2364 current = spec->dtsp_state; 2365 2366 if (current != DTRACESPEC_INACTIVE) { 2367 if (current == DTRACESPEC_COMMITTINGMANY || 2368 current == DTRACESPEC_COMMITTING || 2369 current == DTRACESPEC_DISCARDING) 2370 stat = &state->dts_speculations_busy; 2371 i++; 2372 continue; 2373 } 2374 2375 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2376 current, DTRACESPEC_ACTIVE) == current) 2377 return (i + 1); 2378 } 2379 2380 /* 2381 * We couldn't find a speculation. If we found as much as a single 2382 * busy speculation buffer, we'll attribute this failure as "busy" 2383 * instead of "unavail". 2384 */ 2385 do { 2386 count = *stat; 2387 } while (dtrace_cas32(stat, count, count + 1) != count); 2388 2389 return (0); 2390 } 2391 2392 /* 2393 * This routine commits an active speculation. If the specified speculation 2394 * is not in a valid state to perform a commit(), this routine will silently do 2395 * nothing. The state of the specified speculation is transitioned according 2396 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2397 */ 2398 static void 2399 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2400 dtrace_specid_t which) 2401 { 2402 dtrace_speculation_t *spec; 2403 dtrace_buffer_t *src, *dest; 2404 uintptr_t daddr, saddr, dlimit; 2405 dtrace_speculation_state_t current, new; 2406 intptr_t offs; 2407 2408 if (which == 0) 2409 return; 2410 2411 if (which > state->dts_nspeculations) { 2412 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2413 return; 2414 } 2415 2416 spec = &state->dts_speculations[which - 1]; 2417 src = &spec->dtsp_buffer[cpu]; 2418 dest = &state->dts_buffer[cpu]; 2419 2420 do { 2421 current = spec->dtsp_state; 2422 2423 if (current == DTRACESPEC_COMMITTINGMANY) 2424 break; 2425 2426 switch (current) { 2427 case DTRACESPEC_INACTIVE: 2428 case DTRACESPEC_DISCARDING: 2429 return; 2430 2431 case DTRACESPEC_COMMITTING: 2432 /* 2433 * This is only possible if we are (a) commit()'ing 2434 * without having done a prior speculate() on this CPU 2435 * and (b) racing with another commit() on a different 2436 * CPU. There's nothing to do -- we just assert that 2437 * our offset is 0. 2438 */ 2439 ASSERT(src->dtb_offset == 0); 2440 return; 2441 2442 case DTRACESPEC_ACTIVE: 2443 new = DTRACESPEC_COMMITTING; 2444 break; 2445 2446 case DTRACESPEC_ACTIVEONE: 2447 /* 2448 * This speculation is active on one CPU. If our 2449 * buffer offset is non-zero, we know that the one CPU 2450 * must be us. Otherwise, we are committing on a 2451 * different CPU from the speculate(), and we must 2452 * rely on being asynchronously cleaned. 2453 */ 2454 if (src->dtb_offset != 0) { 2455 new = DTRACESPEC_COMMITTING; 2456 break; 2457 } 2458 /*FALLTHROUGH*/ 2459 2460 case DTRACESPEC_ACTIVEMANY: 2461 new = DTRACESPEC_COMMITTINGMANY; 2462 break; 2463 2464 default: 2465 ASSERT(0); 2466 } 2467 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2468 current, new) != current); 2469 2470 /* 2471 * We have set the state to indicate that we are committing this 2472 * speculation. Now reserve the necessary space in the destination 2473 * buffer. 2474 */ 2475 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2476 sizeof (uint64_t), state, NULL)) < 0) { 2477 dtrace_buffer_drop(dest); 2478 goto out; 2479 } 2480 2481 /* 2482 * We have the space; copy the buffer across. (Note that this is a 2483 * highly subobtimal bcopy(); in the unlikely event that this becomes 2484 * a serious performance issue, a high-performance DTrace-specific 2485 * bcopy() should obviously be invented.) 2486 */ 2487 daddr = (uintptr_t)dest->dtb_tomax + offs; 2488 dlimit = daddr + src->dtb_offset; 2489 saddr = (uintptr_t)src->dtb_tomax; 2490 2491 /* 2492 * First, the aligned portion. 2493 */ 2494 while (dlimit - daddr >= sizeof (uint64_t)) { 2495 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2496 2497 daddr += sizeof (uint64_t); 2498 saddr += sizeof (uint64_t); 2499 } 2500 2501 /* 2502 * Now any left-over bit... 2503 */ 2504 while (dlimit - daddr) 2505 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2506 2507 /* 2508 * Finally, commit the reserved space in the destination buffer. 2509 */ 2510 dest->dtb_offset = offs + src->dtb_offset; 2511 2512 out: 2513 /* 2514 * If we're lucky enough to be the only active CPU on this speculation 2515 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2516 */ 2517 if (current == DTRACESPEC_ACTIVE || 2518 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2519 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2520 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2521 2522 ASSERT(rval == DTRACESPEC_COMMITTING); 2523 } 2524 2525 src->dtb_offset = 0; 2526 src->dtb_xamot_drops += src->dtb_drops; 2527 src->dtb_drops = 0; 2528 } 2529 2530 /* 2531 * This routine discards an active speculation. If the specified speculation 2532 * is not in a valid state to perform a discard(), this routine will silently 2533 * do nothing. The state of the specified speculation is transitioned 2534 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2535 */ 2536 static void 2537 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2538 dtrace_specid_t which) 2539 { 2540 dtrace_speculation_t *spec; 2541 dtrace_speculation_state_t current, new; 2542 dtrace_buffer_t *buf; 2543 2544 if (which == 0) 2545 return; 2546 2547 if (which > state->dts_nspeculations) { 2548 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2549 return; 2550 } 2551 2552 spec = &state->dts_speculations[which - 1]; 2553 buf = &spec->dtsp_buffer[cpu]; 2554 2555 do { 2556 current = spec->dtsp_state; 2557 2558 switch (current) { 2559 case DTRACESPEC_INACTIVE: 2560 case DTRACESPEC_COMMITTINGMANY: 2561 case DTRACESPEC_COMMITTING: 2562 case DTRACESPEC_DISCARDING: 2563 return; 2564 2565 case DTRACESPEC_ACTIVE: 2566 case DTRACESPEC_ACTIVEMANY: 2567 new = DTRACESPEC_DISCARDING; 2568 break; 2569 2570 case DTRACESPEC_ACTIVEONE: 2571 if (buf->dtb_offset != 0) { 2572 new = DTRACESPEC_INACTIVE; 2573 } else { 2574 new = DTRACESPEC_DISCARDING; 2575 } 2576 break; 2577 2578 default: 2579 ASSERT(0); 2580 } 2581 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2582 current, new) != current); 2583 2584 buf->dtb_offset = 0; 2585 buf->dtb_drops = 0; 2586 } 2587 2588 /* 2589 * Note: not called from probe context. This function is called 2590 * asynchronously from cross call context to clean any speculations that are 2591 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2592 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2593 * speculation. 2594 */ 2595 static void 2596 dtrace_speculation_clean_here(dtrace_state_t *state) 2597 { 2598 dtrace_icookie_t cookie; 2599 processorid_t cpu = CPU->cpu_id; 2600 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2601 dtrace_specid_t i; 2602 2603 cookie = dtrace_interrupt_disable(); 2604 2605 if (dest->dtb_tomax == NULL) { 2606 dtrace_interrupt_enable(cookie); 2607 return; 2608 } 2609 2610 for (i = 0; i < state->dts_nspeculations; i++) { 2611 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2612 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2613 2614 if (src->dtb_tomax == NULL) 2615 continue; 2616 2617 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2618 src->dtb_offset = 0; 2619 continue; 2620 } 2621 2622 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2623 continue; 2624 2625 if (src->dtb_offset == 0) 2626 continue; 2627 2628 dtrace_speculation_commit(state, cpu, i + 1); 2629 } 2630 2631 dtrace_interrupt_enable(cookie); 2632 } 2633 2634 /* 2635 * Note: not called from probe context. This function is called 2636 * asynchronously (and at a regular interval) to clean any speculations that 2637 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2638 * is work to be done, it cross calls all CPUs to perform that work; 2639 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2640 * INACTIVE state until they have been cleaned by all CPUs. 2641 */ 2642 static void 2643 dtrace_speculation_clean(dtrace_state_t *state) 2644 { 2645 int work = 0, rv; 2646 dtrace_specid_t i; 2647 2648 for (i = 0; i < state->dts_nspeculations; i++) { 2649 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2650 2651 ASSERT(!spec->dtsp_cleaning); 2652 2653 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2654 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2655 continue; 2656 2657 work++; 2658 spec->dtsp_cleaning = 1; 2659 } 2660 2661 if (!work) 2662 return; 2663 2664 dtrace_xcall(DTRACE_CPUALL, 2665 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2666 2667 /* 2668 * We now know that all CPUs have committed or discarded their 2669 * speculation buffers, as appropriate. We can now set the state 2670 * to inactive. 2671 */ 2672 for (i = 0; i < state->dts_nspeculations; i++) { 2673 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2674 dtrace_speculation_state_t current, new; 2675 2676 if (!spec->dtsp_cleaning) 2677 continue; 2678 2679 current = spec->dtsp_state; 2680 ASSERT(current == DTRACESPEC_DISCARDING || 2681 current == DTRACESPEC_COMMITTINGMANY); 2682 2683 new = DTRACESPEC_INACTIVE; 2684 2685 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2686 ASSERT(rv == current); 2687 spec->dtsp_cleaning = 0; 2688 } 2689 } 2690 2691 /* 2692 * Called as part of a speculate() to get the speculative buffer associated 2693 * with a given speculation. Returns NULL if the specified speculation is not 2694 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2695 * the active CPU is not the specified CPU -- the speculation will be 2696 * atomically transitioned into the ACTIVEMANY state. 2697 */ 2698 static dtrace_buffer_t * 2699 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2700 dtrace_specid_t which) 2701 { 2702 dtrace_speculation_t *spec; 2703 dtrace_speculation_state_t current, new; 2704 dtrace_buffer_t *buf; 2705 2706 if (which == 0) 2707 return (NULL); 2708 2709 if (which > state->dts_nspeculations) { 2710 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2711 return (NULL); 2712 } 2713 2714 spec = &state->dts_speculations[which - 1]; 2715 buf = &spec->dtsp_buffer[cpuid]; 2716 2717 do { 2718 current = spec->dtsp_state; 2719 2720 switch (current) { 2721 case DTRACESPEC_INACTIVE: 2722 case DTRACESPEC_COMMITTINGMANY: 2723 case DTRACESPEC_DISCARDING: 2724 return (NULL); 2725 2726 case DTRACESPEC_COMMITTING: 2727 ASSERT(buf->dtb_offset == 0); 2728 return (NULL); 2729 2730 case DTRACESPEC_ACTIVEONE: 2731 /* 2732 * This speculation is currently active on one CPU. 2733 * Check the offset in the buffer; if it's non-zero, 2734 * that CPU must be us (and we leave the state alone). 2735 * If it's zero, assume that we're starting on a new 2736 * CPU -- and change the state to indicate that the 2737 * speculation is active on more than one CPU. 2738 */ 2739 if (buf->dtb_offset != 0) 2740 return (buf); 2741 2742 new = DTRACESPEC_ACTIVEMANY; 2743 break; 2744 2745 case DTRACESPEC_ACTIVEMANY: 2746 return (buf); 2747 2748 case DTRACESPEC_ACTIVE: 2749 new = DTRACESPEC_ACTIVEONE; 2750 break; 2751 2752 default: 2753 ASSERT(0); 2754 } 2755 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2756 current, new) != current); 2757 2758 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2759 return (buf); 2760 } 2761 2762 /* 2763 * Return a string. In the event that the user lacks the privilege to access 2764 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2765 * don't fail access checking. 2766 * 2767 * dtrace_dif_variable() uses this routine as a helper for various 2768 * builtin values such as 'execname' and 'probefunc.' 2769 */ 2770 uintptr_t 2771 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 2772 dtrace_mstate_t *mstate) 2773 { 2774 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2775 uintptr_t ret; 2776 size_t strsz; 2777 2778 /* 2779 * The easy case: this probe is allowed to read all of memory, so 2780 * we can just return this as a vanilla pointer. 2781 */ 2782 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 2783 return (addr); 2784 2785 /* 2786 * This is the tougher case: we copy the string in question from 2787 * kernel memory into scratch memory and return it that way: this 2788 * ensures that we won't trip up when access checking tests the 2789 * BYREF return value. 2790 */ 2791 strsz = dtrace_strlen((char *)addr, size) + 1; 2792 2793 if (mstate->dtms_scratch_ptr + strsz > 2794 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2795 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2796 return (NULL); 2797 } 2798 2799 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2800 strsz); 2801 ret = mstate->dtms_scratch_ptr; 2802 mstate->dtms_scratch_ptr += strsz; 2803 return (ret); 2804 } 2805 2806 /* 2807 * This function implements the DIF emulator's variable lookups. The emulator 2808 * passes a reserved variable identifier and optional built-in array index. 2809 */ 2810 static uint64_t 2811 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2812 uint64_t ndx) 2813 { 2814 /* 2815 * If we're accessing one of the uncached arguments, we'll turn this 2816 * into a reference in the args array. 2817 */ 2818 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2819 ndx = v - DIF_VAR_ARG0; 2820 v = DIF_VAR_ARGS; 2821 } 2822 2823 switch (v) { 2824 case DIF_VAR_ARGS: 2825 if (!(mstate->dtms_access & DTRACE_ACCESS_ARGS)) { 2826 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= 2827 CPU_DTRACE_KPRIV; 2828 return (0); 2829 } 2830 2831 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2832 if (ndx >= sizeof (mstate->dtms_arg) / 2833 sizeof (mstate->dtms_arg[0])) { 2834 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2835 dtrace_provider_t *pv; 2836 uint64_t val; 2837 2838 pv = mstate->dtms_probe->dtpr_provider; 2839 if (pv->dtpv_pops.dtps_getargval != NULL) 2840 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2841 mstate->dtms_probe->dtpr_id, 2842 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2843 else 2844 val = dtrace_getarg(ndx, aframes); 2845 2846 /* 2847 * This is regrettably required to keep the compiler 2848 * from tail-optimizing the call to dtrace_getarg(). 2849 * The condition always evaluates to true, but the 2850 * compiler has no way of figuring that out a priori. 2851 * (None of this would be necessary if the compiler 2852 * could be relied upon to _always_ tail-optimize 2853 * the call to dtrace_getarg() -- but it can't.) 2854 */ 2855 if (mstate->dtms_probe != NULL) 2856 return (val); 2857 2858 ASSERT(0); 2859 } 2860 2861 return (mstate->dtms_arg[ndx]); 2862 2863 case DIF_VAR_UREGS: { 2864 klwp_t *lwp; 2865 2866 if (!dtrace_priv_proc(state, mstate)) 2867 return (0); 2868 2869 if ((lwp = curthread->t_lwp) == NULL) { 2870 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2871 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL; 2872 return (0); 2873 } 2874 2875 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2876 } 2877 2878 case DIF_VAR_VMREGS: { 2879 uint64_t rval; 2880 2881 if (!dtrace_priv_kernel(state)) 2882 return (0); 2883 2884 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2885 2886 rval = dtrace_getvmreg(ndx, 2887 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags); 2888 2889 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2890 2891 return (rval); 2892 } 2893 2894 case DIF_VAR_CURTHREAD: 2895 if (!dtrace_priv_kernel(state)) 2896 return (0); 2897 return ((uint64_t)(uintptr_t)curthread); 2898 2899 case DIF_VAR_TIMESTAMP: 2900 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2901 mstate->dtms_timestamp = dtrace_gethrtime(); 2902 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2903 } 2904 return (mstate->dtms_timestamp); 2905 2906 case DIF_VAR_VTIMESTAMP: 2907 ASSERT(dtrace_vtime_references != 0); 2908 return (curthread->t_dtrace_vtime); 2909 2910 case DIF_VAR_WALLTIMESTAMP: 2911 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2912 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2913 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2914 } 2915 return (mstate->dtms_walltimestamp); 2916 2917 case DIF_VAR_IPL: 2918 if (!dtrace_priv_kernel(state)) 2919 return (0); 2920 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2921 mstate->dtms_ipl = dtrace_getipl(); 2922 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2923 } 2924 return (mstate->dtms_ipl); 2925 2926 case DIF_VAR_EPID: 2927 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2928 return (mstate->dtms_epid); 2929 2930 case DIF_VAR_ID: 2931 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2932 return (mstate->dtms_probe->dtpr_id); 2933 2934 case DIF_VAR_STACKDEPTH: 2935 if (!dtrace_priv_kernel(state)) 2936 return (0); 2937 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2938 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2939 2940 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2941 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2942 } 2943 return (mstate->dtms_stackdepth); 2944 2945 case DIF_VAR_USTACKDEPTH: 2946 if (!dtrace_priv_proc(state, mstate)) 2947 return (0); 2948 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2949 /* 2950 * See comment in DIF_VAR_PID. 2951 */ 2952 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2953 CPU_ON_INTR(CPU)) { 2954 mstate->dtms_ustackdepth = 0; 2955 } else { 2956 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2957 mstate->dtms_ustackdepth = 2958 dtrace_getustackdepth(); 2959 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2960 } 2961 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2962 } 2963 return (mstate->dtms_ustackdepth); 2964 2965 case DIF_VAR_CALLER: 2966 if (!dtrace_priv_kernel(state)) 2967 return (0); 2968 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2969 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2970 2971 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2972 /* 2973 * If this is an unanchored probe, we are 2974 * required to go through the slow path: 2975 * dtrace_caller() only guarantees correct 2976 * results for anchored probes. 2977 */ 2978 pc_t caller[2]; 2979 2980 dtrace_getpcstack(caller, 2, aframes, 2981 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2982 mstate->dtms_caller = caller[1]; 2983 } else if ((mstate->dtms_caller = 2984 dtrace_caller(aframes)) == -1) { 2985 /* 2986 * We have failed to do this the quick way; 2987 * we must resort to the slower approach of 2988 * calling dtrace_getpcstack(). 2989 */ 2990 pc_t caller; 2991 2992 dtrace_getpcstack(&caller, 1, aframes, NULL); 2993 mstate->dtms_caller = caller; 2994 } 2995 2996 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 2997 } 2998 return (mstate->dtms_caller); 2999 3000 case DIF_VAR_UCALLER: 3001 if (!dtrace_priv_proc(state, mstate)) 3002 return (0); 3003 3004 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3005 uint64_t ustack[3]; 3006 3007 /* 3008 * dtrace_getupcstack() fills in the first uint64_t 3009 * with the current PID. The second uint64_t will 3010 * be the program counter at user-level. The third 3011 * uint64_t will contain the caller, which is what 3012 * we're after. 3013 */ 3014 ustack[2] = NULL; 3015 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3016 dtrace_getupcstack(ustack, 3); 3017 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3018 mstate->dtms_ucaller = ustack[2]; 3019 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3020 } 3021 3022 return (mstate->dtms_ucaller); 3023 3024 case DIF_VAR_PROBEPROV: 3025 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3026 return (dtrace_dif_varstr( 3027 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3028 state, mstate)); 3029 3030 case DIF_VAR_PROBEMOD: 3031 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3032 return (dtrace_dif_varstr( 3033 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3034 state, mstate)); 3035 3036 case DIF_VAR_PROBEFUNC: 3037 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3038 return (dtrace_dif_varstr( 3039 (uintptr_t)mstate->dtms_probe->dtpr_func, 3040 state, mstate)); 3041 3042 case DIF_VAR_PROBENAME: 3043 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3044 return (dtrace_dif_varstr( 3045 (uintptr_t)mstate->dtms_probe->dtpr_name, 3046 state, mstate)); 3047 3048 case DIF_VAR_PID: 3049 if (!dtrace_priv_proc(state, mstate)) 3050 return (0); 3051 3052 /* 3053 * Note that we are assuming that an unanchored probe is 3054 * always due to a high-level interrupt. (And we're assuming 3055 * that there is only a single high level interrupt.) 3056 */ 3057 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3058 return (pid0.pid_id); 3059 3060 /* 3061 * It is always safe to dereference one's own t_procp pointer: 3062 * it always points to a valid, allocated proc structure. 3063 * Further, it is always safe to dereference the p_pidp member 3064 * of one's own proc structure. (These are truisms becuase 3065 * threads and processes don't clean up their own state -- 3066 * they leave that task to whomever reaps them.) 3067 */ 3068 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3069 3070 case DIF_VAR_PPID: 3071 if (!dtrace_priv_proc(state, mstate)) 3072 return (0); 3073 3074 /* 3075 * See comment in DIF_VAR_PID. 3076 */ 3077 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3078 return (pid0.pid_id); 3079 3080 /* 3081 * It is always safe to dereference one's own t_procp pointer: 3082 * it always points to a valid, allocated proc structure. 3083 * (This is true because threads don't clean up their own 3084 * state -- they leave that task to whomever reaps them.) 3085 */ 3086 return ((uint64_t)curthread->t_procp->p_ppid); 3087 3088 case DIF_VAR_TID: 3089 /* 3090 * See comment in DIF_VAR_PID. 3091 */ 3092 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3093 return (0); 3094 3095 return ((uint64_t)curthread->t_tid); 3096 3097 case DIF_VAR_EXECNAME: 3098 if (!dtrace_priv_proc(state, mstate)) 3099 return (0); 3100 3101 /* 3102 * See comment in DIF_VAR_PID. 3103 */ 3104 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3105 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3106 3107 /* 3108 * It is always safe to dereference one's own t_procp pointer: 3109 * it always points to a valid, allocated proc structure. 3110 * (This is true because threads don't clean up their own 3111 * state -- they leave that task to whomever reaps them.) 3112 */ 3113 return (dtrace_dif_varstr( 3114 (uintptr_t)curthread->t_procp->p_user.u_comm, 3115 state, mstate)); 3116 3117 case DIF_VAR_ZONENAME: 3118 if (!dtrace_priv_proc(state, mstate)) 3119 return (0); 3120 3121 /* 3122 * See comment in DIF_VAR_PID. 3123 */ 3124 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3125 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3126 3127 /* 3128 * It is always safe to dereference one's own t_procp pointer: 3129 * it always points to a valid, allocated proc structure. 3130 * (This is true because threads don't clean up their own 3131 * state -- they leave that task to whomever reaps them.) 3132 */ 3133 return (dtrace_dif_varstr( 3134 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3135 state, mstate)); 3136 3137 case DIF_VAR_UID: 3138 if (!dtrace_priv_proc(state, mstate)) 3139 return (0); 3140 3141 /* 3142 * See comment in DIF_VAR_PID. 3143 */ 3144 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3145 return ((uint64_t)p0.p_cred->cr_uid); 3146 3147 /* 3148 * It is always safe to dereference one's own t_procp pointer: 3149 * it always points to a valid, allocated proc structure. 3150 * (This is true because threads don't clean up their own 3151 * state -- they leave that task to whomever reaps them.) 3152 * 3153 * Additionally, it is safe to dereference one's own process 3154 * credential, since this is never NULL after process birth. 3155 */ 3156 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3157 3158 case DIF_VAR_GID: 3159 if (!dtrace_priv_proc(state, mstate)) 3160 return (0); 3161 3162 /* 3163 * See comment in DIF_VAR_PID. 3164 */ 3165 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3166 return ((uint64_t)p0.p_cred->cr_gid); 3167 3168 /* 3169 * It is always safe to dereference one's own t_procp pointer: 3170 * it always points to a valid, allocated proc structure. 3171 * (This is true because threads don't clean up their own 3172 * state -- they leave that task to whomever reaps them.) 3173 * 3174 * Additionally, it is safe to dereference one's own process 3175 * credential, since this is never NULL after process birth. 3176 */ 3177 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3178 3179 case DIF_VAR_ERRNO: { 3180 klwp_t *lwp; 3181 if (!dtrace_priv_proc(state, mstate)) 3182 return (0); 3183 3184 /* 3185 * See comment in DIF_VAR_PID. 3186 */ 3187 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3188 return (0); 3189 3190 /* 3191 * It is always safe to dereference one's own t_lwp pointer in 3192 * the event that this pointer is non-NULL. (This is true 3193 * because threads and lwps don't clean up their own state -- 3194 * they leave that task to whomever reaps them.) 3195 */ 3196 if ((lwp = curthread->t_lwp) == NULL) 3197 return (0); 3198 3199 return ((uint64_t)lwp->lwp_errno); 3200 } 3201 default: 3202 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3203 return (0); 3204 } 3205 } 3206 3207 /* 3208 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 3209 * Notice that we don't bother validating the proper number of arguments or 3210 * their types in the tuple stack. This isn't needed because all argument 3211 * interpretation is safe because of our load safety -- the worst that can 3212 * happen is that a bogus program can obtain bogus results. 3213 */ 3214 static void 3215 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 3216 dtrace_key_t *tupregs, int nargs, 3217 dtrace_mstate_t *mstate, dtrace_state_t *state) 3218 { 3219 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 3220 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 3221 dtrace_vstate_t *vstate = &state->dts_vstate; 3222 3223 union { 3224 mutex_impl_t mi; 3225 uint64_t mx; 3226 } m; 3227 3228 union { 3229 krwlock_t ri; 3230 uintptr_t rw; 3231 } r; 3232 3233 switch (subr) { 3234 case DIF_SUBR_RAND: 3235 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 3236 break; 3237 3238 case DIF_SUBR_MUTEX_OWNED: 3239 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3240 mstate, vstate)) { 3241 regs[rd] = NULL; 3242 break; 3243 } 3244 3245 m.mx = dtrace_load64(tupregs[0].dttk_value); 3246 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3247 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3248 else 3249 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 3250 break; 3251 3252 case DIF_SUBR_MUTEX_OWNER: 3253 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3254 mstate, vstate)) { 3255 regs[rd] = NULL; 3256 break; 3257 } 3258 3259 m.mx = dtrace_load64(tupregs[0].dttk_value); 3260 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3261 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3262 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3263 else 3264 regs[rd] = 0; 3265 break; 3266 3267 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3268 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3269 mstate, vstate)) { 3270 regs[rd] = NULL; 3271 break; 3272 } 3273 3274 m.mx = dtrace_load64(tupregs[0].dttk_value); 3275 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3276 break; 3277 3278 case DIF_SUBR_MUTEX_TYPE_SPIN: 3279 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3280 mstate, vstate)) { 3281 regs[rd] = NULL; 3282 break; 3283 } 3284 3285 m.mx = dtrace_load64(tupregs[0].dttk_value); 3286 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3287 break; 3288 3289 case DIF_SUBR_RW_READ_HELD: { 3290 uintptr_t tmp; 3291 3292 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3293 mstate, vstate)) { 3294 regs[rd] = NULL; 3295 break; 3296 } 3297 3298 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3299 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 3300 break; 3301 } 3302 3303 case DIF_SUBR_RW_WRITE_HELD: 3304 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3305 mstate, vstate)) { 3306 regs[rd] = NULL; 3307 break; 3308 } 3309 3310 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3311 regs[rd] = _RW_WRITE_HELD(&r.ri); 3312 break; 3313 3314 case DIF_SUBR_RW_ISWRITER: 3315 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3316 mstate, vstate)) { 3317 regs[rd] = NULL; 3318 break; 3319 } 3320 3321 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3322 regs[rd] = _RW_ISWRITER(&r.ri); 3323 break; 3324 3325 case DIF_SUBR_BCOPY: { 3326 /* 3327 * We need to be sure that the destination is in the scratch 3328 * region -- no other region is allowed. 3329 */ 3330 uintptr_t src = tupregs[0].dttk_value; 3331 uintptr_t dest = tupregs[1].dttk_value; 3332 size_t size = tupregs[2].dttk_value; 3333 3334 if (!dtrace_inscratch(dest, size, mstate)) { 3335 *flags |= CPU_DTRACE_BADADDR; 3336 *illval = regs[rd]; 3337 break; 3338 } 3339 3340 if (!dtrace_canload(src, size, mstate, vstate)) { 3341 regs[rd] = NULL; 3342 break; 3343 } 3344 3345 dtrace_bcopy((void *)src, (void *)dest, size); 3346 break; 3347 } 3348 3349 case DIF_SUBR_ALLOCA: 3350 case DIF_SUBR_COPYIN: { 3351 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 3352 uint64_t size = 3353 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 3354 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 3355 3356 /* 3357 * This action doesn't require any credential checks since 3358 * probes will not activate in user contexts to which the 3359 * enabling user does not have permissions. 3360 */ 3361 3362 /* 3363 * Rounding up the user allocation size could have overflowed 3364 * a large, bogus allocation (like -1ULL) to 0. 3365 */ 3366 if (scratch_size < size || 3367 !DTRACE_INSCRATCH(mstate, scratch_size)) { 3368 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3369 regs[rd] = NULL; 3370 break; 3371 } 3372 3373 if (subr == DIF_SUBR_COPYIN) { 3374 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3375 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3376 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3377 } 3378 3379 mstate->dtms_scratch_ptr += scratch_size; 3380 regs[rd] = dest; 3381 break; 3382 } 3383 3384 case DIF_SUBR_COPYINTO: { 3385 uint64_t size = tupregs[1].dttk_value; 3386 uintptr_t dest = tupregs[2].dttk_value; 3387 3388 /* 3389 * This action doesn't require any credential checks since 3390 * probes will not activate in user contexts to which the 3391 * enabling user does not have permissions. 3392 */ 3393 if (!dtrace_inscratch(dest, size, mstate)) { 3394 *flags |= CPU_DTRACE_BADADDR; 3395 *illval = regs[rd]; 3396 break; 3397 } 3398 3399 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3400 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3401 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3402 break; 3403 } 3404 3405 case DIF_SUBR_COPYINSTR: { 3406 uintptr_t dest = mstate->dtms_scratch_ptr; 3407 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3408 3409 if (nargs > 1 && tupregs[1].dttk_value < size) 3410 size = tupregs[1].dttk_value + 1; 3411 3412 /* 3413 * This action doesn't require any credential checks since 3414 * probes will not activate in user contexts to which the 3415 * enabling user does not have permissions. 3416 */ 3417 if (!DTRACE_INSCRATCH(mstate, size)) { 3418 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3419 regs[rd] = NULL; 3420 break; 3421 } 3422 3423 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3424 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 3425 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3426 3427 ((char *)dest)[size - 1] = '\0'; 3428 mstate->dtms_scratch_ptr += size; 3429 regs[rd] = dest; 3430 break; 3431 } 3432 3433 case DIF_SUBR_MSGSIZE: 3434 case DIF_SUBR_MSGDSIZE: { 3435 uintptr_t baddr = tupregs[0].dttk_value, daddr; 3436 uintptr_t wptr, rptr; 3437 size_t count = 0; 3438 int cont = 0; 3439 3440 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3441 3442 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 3443 vstate)) { 3444 regs[rd] = NULL; 3445 break; 3446 } 3447 3448 wptr = dtrace_loadptr(baddr + 3449 offsetof(mblk_t, b_wptr)); 3450 3451 rptr = dtrace_loadptr(baddr + 3452 offsetof(mblk_t, b_rptr)); 3453 3454 if (wptr < rptr) { 3455 *flags |= CPU_DTRACE_BADADDR; 3456 *illval = tupregs[0].dttk_value; 3457 break; 3458 } 3459 3460 daddr = dtrace_loadptr(baddr + 3461 offsetof(mblk_t, b_datap)); 3462 3463 baddr = dtrace_loadptr(baddr + 3464 offsetof(mblk_t, b_cont)); 3465 3466 /* 3467 * We want to prevent against denial-of-service here, 3468 * so we're only going to search the list for 3469 * dtrace_msgdsize_max mblks. 3470 */ 3471 if (cont++ > dtrace_msgdsize_max) { 3472 *flags |= CPU_DTRACE_ILLOP; 3473 break; 3474 } 3475 3476 if (subr == DIF_SUBR_MSGDSIZE) { 3477 if (dtrace_load8(daddr + 3478 offsetof(dblk_t, db_type)) != M_DATA) 3479 continue; 3480 } 3481 3482 count += wptr - rptr; 3483 } 3484 3485 if (!(*flags & CPU_DTRACE_FAULT)) 3486 regs[rd] = count; 3487 3488 break; 3489 } 3490 3491 case DIF_SUBR_PROGENYOF: { 3492 pid_t pid = tupregs[0].dttk_value; 3493 proc_t *p; 3494 int rval = 0; 3495 3496 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3497 3498 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 3499 if (p->p_pidp->pid_id == pid) { 3500 rval = 1; 3501 break; 3502 } 3503 } 3504 3505 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3506 3507 regs[rd] = rval; 3508 break; 3509 } 3510 3511 case DIF_SUBR_SPECULATION: 3512 regs[rd] = dtrace_speculation(state); 3513 break; 3514 3515 case DIF_SUBR_COPYOUT: { 3516 uintptr_t kaddr = tupregs[0].dttk_value; 3517 uintptr_t uaddr = tupregs[1].dttk_value; 3518 uint64_t size = tupregs[2].dttk_value; 3519 3520 if (!dtrace_destructive_disallow && 3521 dtrace_priv_proc_control(state, mstate) && 3522 !dtrace_istoxic(kaddr, size)) { 3523 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3524 dtrace_copyout(kaddr, uaddr, size, flags); 3525 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3526 } 3527 break; 3528 } 3529 3530 case DIF_SUBR_COPYOUTSTR: { 3531 uintptr_t kaddr = tupregs[0].dttk_value; 3532 uintptr_t uaddr = tupregs[1].dttk_value; 3533 uint64_t size = tupregs[2].dttk_value; 3534 3535 if (!dtrace_destructive_disallow && 3536 dtrace_priv_proc_control(state, mstate) && 3537 !dtrace_istoxic(kaddr, size)) { 3538 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3539 dtrace_copyoutstr(kaddr, uaddr, size, flags); 3540 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3541 } 3542 break; 3543 } 3544 3545 case DIF_SUBR_STRLEN: { 3546 size_t sz; 3547 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 3548 sz = dtrace_strlen((char *)addr, 3549 state->dts_options[DTRACEOPT_STRSIZE]); 3550 3551 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 3552 regs[rd] = NULL; 3553 break; 3554 } 3555 3556 regs[rd] = sz; 3557 3558 break; 3559 } 3560 3561 case DIF_SUBR_STRCHR: 3562 case DIF_SUBR_STRRCHR: { 3563 /* 3564 * We're going to iterate over the string looking for the 3565 * specified character. We will iterate until we have reached 3566 * the string length or we have found the character. If this 3567 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 3568 * of the specified character instead of the first. 3569 */ 3570 uintptr_t saddr = tupregs[0].dttk_value; 3571 uintptr_t addr = tupregs[0].dttk_value; 3572 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 3573 char c, target = (char)tupregs[1].dttk_value; 3574 3575 for (regs[rd] = NULL; addr < limit; addr++) { 3576 if ((c = dtrace_load8(addr)) == target) { 3577 regs[rd] = addr; 3578 3579 if (subr == DIF_SUBR_STRCHR) 3580 break; 3581 } 3582 3583 if (c == '\0') 3584 break; 3585 } 3586 3587 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 3588 regs[rd] = NULL; 3589 break; 3590 } 3591 3592 break; 3593 } 3594 3595 case DIF_SUBR_STRSTR: 3596 case DIF_SUBR_INDEX: 3597 case DIF_SUBR_RINDEX: { 3598 /* 3599 * We're going to iterate over the string looking for the 3600 * specified string. We will iterate until we have reached 3601 * the string length or we have found the string. (Yes, this 3602 * is done in the most naive way possible -- but considering 3603 * that the string we're searching for is likely to be 3604 * relatively short, the complexity of Rabin-Karp or similar 3605 * hardly seems merited.) 3606 */ 3607 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 3608 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 3609 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3610 size_t len = dtrace_strlen(addr, size); 3611 size_t sublen = dtrace_strlen(substr, size); 3612 char *limit = addr + len, *orig = addr; 3613 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 3614 int inc = 1; 3615 3616 regs[rd] = notfound; 3617 3618 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 3619 regs[rd] = NULL; 3620 break; 3621 } 3622 3623 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 3624 vstate)) { 3625 regs[rd] = NULL; 3626 break; 3627 } 3628 3629 /* 3630 * strstr() and index()/rindex() have similar semantics if 3631 * both strings are the empty string: strstr() returns a 3632 * pointer to the (empty) string, and index() and rindex() 3633 * both return index 0 (regardless of any position argument). 3634 */ 3635 if (sublen == 0 && len == 0) { 3636 if (subr == DIF_SUBR_STRSTR) 3637 regs[rd] = (uintptr_t)addr; 3638 else 3639 regs[rd] = 0; 3640 break; 3641 } 3642 3643 if (subr != DIF_SUBR_STRSTR) { 3644 if (subr == DIF_SUBR_RINDEX) { 3645 limit = orig - 1; 3646 addr += len; 3647 inc = -1; 3648 } 3649 3650 /* 3651 * Both index() and rindex() take an optional position 3652 * argument that denotes the starting position. 3653 */ 3654 if (nargs == 3) { 3655 int64_t pos = (int64_t)tupregs[2].dttk_value; 3656 3657 /* 3658 * If the position argument to index() is 3659 * negative, Perl implicitly clamps it at 3660 * zero. This semantic is a little surprising 3661 * given the special meaning of negative 3662 * positions to similar Perl functions like 3663 * substr(), but it appears to reflect a 3664 * notion that index() can start from a 3665 * negative index and increment its way up to 3666 * the string. Given this notion, Perl's 3667 * rindex() is at least self-consistent in 3668 * that it implicitly clamps positions greater 3669 * than the string length to be the string 3670 * length. Where Perl completely loses 3671 * coherence, however, is when the specified 3672 * substring is the empty string (""). In 3673 * this case, even if the position is 3674 * negative, rindex() returns 0 -- and even if 3675 * the position is greater than the length, 3676 * index() returns the string length. These 3677 * semantics violate the notion that index() 3678 * should never return a value less than the 3679 * specified position and that rindex() should 3680 * never return a value greater than the 3681 * specified position. (One assumes that 3682 * these semantics are artifacts of Perl's 3683 * implementation and not the results of 3684 * deliberate design -- it beggars belief that 3685 * even Larry Wall could desire such oddness.) 3686 * While in the abstract one would wish for 3687 * consistent position semantics across 3688 * substr(), index() and rindex() -- or at the 3689 * very least self-consistent position 3690 * semantics for index() and rindex() -- we 3691 * instead opt to keep with the extant Perl 3692 * semantics, in all their broken glory. (Do 3693 * we have more desire to maintain Perl's 3694 * semantics than Perl does? Probably.) 3695 */ 3696 if (subr == DIF_SUBR_RINDEX) { 3697 if (pos < 0) { 3698 if (sublen == 0) 3699 regs[rd] = 0; 3700 break; 3701 } 3702 3703 if (pos > len) 3704 pos = len; 3705 } else { 3706 if (pos < 0) 3707 pos = 0; 3708 3709 if (pos >= len) { 3710 if (sublen == 0) 3711 regs[rd] = len; 3712 break; 3713 } 3714 } 3715 3716 addr = orig + pos; 3717 } 3718 } 3719 3720 for (regs[rd] = notfound; addr != limit; addr += inc) { 3721 if (dtrace_strncmp(addr, substr, sublen) == 0) { 3722 if (subr != DIF_SUBR_STRSTR) { 3723 /* 3724 * As D index() and rindex() are 3725 * modeled on Perl (and not on awk), 3726 * we return a zero-based (and not a 3727 * one-based) index. (For you Perl 3728 * weenies: no, we're not going to add 3729 * $[ -- and shouldn't you be at a con 3730 * or something?) 3731 */ 3732 regs[rd] = (uintptr_t)(addr - orig); 3733 break; 3734 } 3735 3736 ASSERT(subr == DIF_SUBR_STRSTR); 3737 regs[rd] = (uintptr_t)addr; 3738 break; 3739 } 3740 } 3741 3742 break; 3743 } 3744 3745 case DIF_SUBR_STRTOK: { 3746 uintptr_t addr = tupregs[0].dttk_value; 3747 uintptr_t tokaddr = tupregs[1].dttk_value; 3748 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3749 uintptr_t limit, toklimit = tokaddr + size; 3750 uint8_t c, tokmap[32]; /* 256 / 8 */ 3751 char *dest = (char *)mstate->dtms_scratch_ptr; 3752 int i; 3753 3754 /* 3755 * Check both the token buffer and (later) the input buffer, 3756 * since both could be non-scratch addresses. 3757 */ 3758 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 3759 regs[rd] = NULL; 3760 break; 3761 } 3762 3763 if (!DTRACE_INSCRATCH(mstate, size)) { 3764 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3765 regs[rd] = NULL; 3766 break; 3767 } 3768 3769 if (addr == NULL) { 3770 /* 3771 * If the address specified is NULL, we use our saved 3772 * strtok pointer from the mstate. Note that this 3773 * means that the saved strtok pointer is _only_ 3774 * valid within multiple enablings of the same probe -- 3775 * it behaves like an implicit clause-local variable. 3776 */ 3777 addr = mstate->dtms_strtok; 3778 } else { 3779 /* 3780 * If the user-specified address is non-NULL we must 3781 * access check it. This is the only time we have 3782 * a chance to do so, since this address may reside 3783 * in the string table of this clause-- future calls 3784 * (when we fetch addr from mstate->dtms_strtok) 3785 * would fail this access check. 3786 */ 3787 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 3788 regs[rd] = NULL; 3789 break; 3790 } 3791 } 3792 3793 /* 3794 * First, zero the token map, and then process the token 3795 * string -- setting a bit in the map for every character 3796 * found in the token string. 3797 */ 3798 for (i = 0; i < sizeof (tokmap); i++) 3799 tokmap[i] = 0; 3800 3801 for (; tokaddr < toklimit; tokaddr++) { 3802 if ((c = dtrace_load8(tokaddr)) == '\0') 3803 break; 3804 3805 ASSERT((c >> 3) < sizeof (tokmap)); 3806 tokmap[c >> 3] |= (1 << (c & 0x7)); 3807 } 3808 3809 for (limit = addr + size; addr < limit; addr++) { 3810 /* 3811 * We're looking for a character that is _not_ contained 3812 * in the token string. 3813 */ 3814 if ((c = dtrace_load8(addr)) == '\0') 3815 break; 3816 3817 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3818 break; 3819 } 3820 3821 if (c == '\0') { 3822 /* 3823 * We reached the end of the string without finding 3824 * any character that was not in the token string. 3825 * We return NULL in this case, and we set the saved 3826 * address to NULL as well. 3827 */ 3828 regs[rd] = NULL; 3829 mstate->dtms_strtok = NULL; 3830 break; 3831 } 3832 3833 /* 3834 * From here on, we're copying into the destination string. 3835 */ 3836 for (i = 0; addr < limit && i < size - 1; addr++) { 3837 if ((c = dtrace_load8(addr)) == '\0') 3838 break; 3839 3840 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3841 break; 3842 3843 ASSERT(i < size); 3844 dest[i++] = c; 3845 } 3846 3847 ASSERT(i < size); 3848 dest[i] = '\0'; 3849 regs[rd] = (uintptr_t)dest; 3850 mstate->dtms_scratch_ptr += size; 3851 mstate->dtms_strtok = addr; 3852 break; 3853 } 3854 3855 case DIF_SUBR_SUBSTR: { 3856 uintptr_t s = tupregs[0].dttk_value; 3857 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3858 char *d = (char *)mstate->dtms_scratch_ptr; 3859 int64_t index = (int64_t)tupregs[1].dttk_value; 3860 int64_t remaining = (int64_t)tupregs[2].dttk_value; 3861 size_t len = dtrace_strlen((char *)s, size); 3862 int64_t i; 3863 3864 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 3865 regs[rd] = NULL; 3866 break; 3867 } 3868 3869 if (!DTRACE_INSCRATCH(mstate, size)) { 3870 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3871 regs[rd] = NULL; 3872 break; 3873 } 3874 3875 if (nargs <= 2) 3876 remaining = (int64_t)size; 3877 3878 if (index < 0) { 3879 index += len; 3880 3881 if (index < 0 && index + remaining > 0) { 3882 remaining += index; 3883 index = 0; 3884 } 3885 } 3886 3887 if (index >= len || index < 0) { 3888 remaining = 0; 3889 } else if (remaining < 0) { 3890 remaining += len - index; 3891 } else if (index + remaining > size) { 3892 remaining = size - index; 3893 } 3894 3895 for (i = 0; i < remaining; i++) { 3896 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 3897 break; 3898 } 3899 3900 d[i] = '\0'; 3901 3902 mstate->dtms_scratch_ptr += size; 3903 regs[rd] = (uintptr_t)d; 3904 break; 3905 } 3906 3907 case DIF_SUBR_TOUPPER: 3908 case DIF_SUBR_TOLOWER: { 3909 uintptr_t s = tupregs[0].dttk_value; 3910 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3911 char *dest = (char *)mstate->dtms_scratch_ptr, c; 3912 size_t len = dtrace_strlen((char *)s, size); 3913 char lower, upper, convert; 3914 int64_t i; 3915 3916 if (subr == DIF_SUBR_TOUPPER) { 3917 lower = 'a'; 3918 upper = 'z'; 3919 convert = 'A'; 3920 } else { 3921 lower = 'A'; 3922 upper = 'Z'; 3923 convert = 'a'; 3924 } 3925 3926 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 3927 regs[rd] = NULL; 3928 break; 3929 } 3930 3931 if (!DTRACE_INSCRATCH(mstate, size)) { 3932 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3933 regs[rd] = NULL; 3934 break; 3935 } 3936 3937 for (i = 0; i < size - 1; i++) { 3938 if ((c = dtrace_load8(s + i)) == '\0') 3939 break; 3940 3941 if (c >= lower && c <= upper) 3942 c = convert + (c - lower); 3943 3944 dest[i] = c; 3945 } 3946 3947 ASSERT(i < size); 3948 dest[i] = '\0'; 3949 regs[rd] = (uintptr_t)dest; 3950 mstate->dtms_scratch_ptr += size; 3951 break; 3952 } 3953 3954 case DIF_SUBR_GETMAJOR: 3955 #ifdef _LP64 3956 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 3957 #else 3958 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 3959 #endif 3960 break; 3961 3962 case DIF_SUBR_GETMINOR: 3963 #ifdef _LP64 3964 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 3965 #else 3966 regs[rd] = tupregs[0].dttk_value & MAXMIN; 3967 #endif 3968 break; 3969 3970 case DIF_SUBR_DDI_PATHNAME: { 3971 /* 3972 * This one is a galactic mess. We are going to roughly 3973 * emulate ddi_pathname(), but it's made more complicated 3974 * by the fact that we (a) want to include the minor name and 3975 * (b) must proceed iteratively instead of recursively. 3976 */ 3977 uintptr_t dest = mstate->dtms_scratch_ptr; 3978 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3979 char *start = (char *)dest, *end = start + size - 1; 3980 uintptr_t daddr = tupregs[0].dttk_value; 3981 int64_t minor = (int64_t)tupregs[1].dttk_value; 3982 char *s; 3983 int i, len, depth = 0; 3984 3985 /* 3986 * Due to all the pointer jumping we do and context we must 3987 * rely upon, we just mandate that the user must have kernel 3988 * read privileges to use this routine. 3989 */ 3990 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 3991 *flags |= CPU_DTRACE_KPRIV; 3992 *illval = daddr; 3993 regs[rd] = NULL; 3994 } 3995 3996 if (!DTRACE_INSCRATCH(mstate, size)) { 3997 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3998 regs[rd] = NULL; 3999 break; 4000 } 4001 4002 *end = '\0'; 4003 4004 /* 4005 * We want to have a name for the minor. In order to do this, 4006 * we need to walk the minor list from the devinfo. We want 4007 * to be sure that we don't infinitely walk a circular list, 4008 * so we check for circularity by sending a scout pointer 4009 * ahead two elements for every element that we iterate over; 4010 * if the list is circular, these will ultimately point to the 4011 * same element. You may recognize this little trick as the 4012 * answer to a stupid interview question -- one that always 4013 * seems to be asked by those who had to have it laboriously 4014 * explained to them, and who can't even concisely describe 4015 * the conditions under which one would be forced to resort to 4016 * this technique. Needless to say, those conditions are 4017 * found here -- and probably only here. Is this the only use 4018 * of this infamous trick in shipping, production code? If it 4019 * isn't, it probably should be... 4020 */ 4021 if (minor != -1) { 4022 uintptr_t maddr = dtrace_loadptr(daddr + 4023 offsetof(struct dev_info, devi_minor)); 4024 4025 uintptr_t next = offsetof(struct ddi_minor_data, next); 4026 uintptr_t name = offsetof(struct ddi_minor_data, 4027 d_minor) + offsetof(struct ddi_minor, name); 4028 uintptr_t dev = offsetof(struct ddi_minor_data, 4029 d_minor) + offsetof(struct ddi_minor, dev); 4030 uintptr_t scout; 4031 4032 if (maddr != NULL) 4033 scout = dtrace_loadptr(maddr + next); 4034 4035 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4036 uint64_t m; 4037 #ifdef _LP64 4038 m = dtrace_load64(maddr + dev) & MAXMIN64; 4039 #else 4040 m = dtrace_load32(maddr + dev) & MAXMIN; 4041 #endif 4042 if (m != minor) { 4043 maddr = dtrace_loadptr(maddr + next); 4044 4045 if (scout == NULL) 4046 continue; 4047 4048 scout = dtrace_loadptr(scout + next); 4049 4050 if (scout == NULL) 4051 continue; 4052 4053 scout = dtrace_loadptr(scout + next); 4054 4055 if (scout == NULL) 4056 continue; 4057 4058 if (scout == maddr) { 4059 *flags |= CPU_DTRACE_ILLOP; 4060 break; 4061 } 4062 4063 continue; 4064 } 4065 4066 /* 4067 * We have the minor data. Now we need to 4068 * copy the minor's name into the end of the 4069 * pathname. 4070 */ 4071 s = (char *)dtrace_loadptr(maddr + name); 4072 len = dtrace_strlen(s, size); 4073 4074 if (*flags & CPU_DTRACE_FAULT) 4075 break; 4076 4077 if (len != 0) { 4078 if ((end -= (len + 1)) < start) 4079 break; 4080 4081 *end = ':'; 4082 } 4083 4084 for (i = 1; i <= len; i++) 4085 end[i] = dtrace_load8((uintptr_t)s++); 4086 break; 4087 } 4088 } 4089 4090 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4091 ddi_node_state_t devi_state; 4092 4093 devi_state = dtrace_load32(daddr + 4094 offsetof(struct dev_info, devi_node_state)); 4095 4096 if (*flags & CPU_DTRACE_FAULT) 4097 break; 4098 4099 if (devi_state >= DS_INITIALIZED) { 4100 s = (char *)dtrace_loadptr(daddr + 4101 offsetof(struct dev_info, devi_addr)); 4102 len = dtrace_strlen(s, size); 4103 4104 if (*flags & CPU_DTRACE_FAULT) 4105 break; 4106 4107 if (len != 0) { 4108 if ((end -= (len + 1)) < start) 4109 break; 4110 4111 *end = '@'; 4112 } 4113 4114 for (i = 1; i <= len; i++) 4115 end[i] = dtrace_load8((uintptr_t)s++); 4116 } 4117 4118 /* 4119 * Now for the node name... 4120 */ 4121 s = (char *)dtrace_loadptr(daddr + 4122 offsetof(struct dev_info, devi_node_name)); 4123 4124 daddr = dtrace_loadptr(daddr + 4125 offsetof(struct dev_info, devi_parent)); 4126 4127 /* 4128 * If our parent is NULL (that is, if we're the root 4129 * node), we're going to use the special path 4130 * "devices". 4131 */ 4132 if (daddr == NULL) 4133 s = "devices"; 4134 4135 len = dtrace_strlen(s, size); 4136 if (*flags & CPU_DTRACE_FAULT) 4137 break; 4138 4139 if ((end -= (len + 1)) < start) 4140 break; 4141 4142 for (i = 1; i <= len; i++) 4143 end[i] = dtrace_load8((uintptr_t)s++); 4144 *end = '/'; 4145 4146 if (depth++ > dtrace_devdepth_max) { 4147 *flags |= CPU_DTRACE_ILLOP; 4148 break; 4149 } 4150 } 4151 4152 if (end < start) 4153 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4154 4155 if (daddr == NULL) { 4156 regs[rd] = (uintptr_t)end; 4157 mstate->dtms_scratch_ptr += size; 4158 } 4159 4160 break; 4161 } 4162 4163 case DIF_SUBR_STRJOIN: { 4164 char *d = (char *)mstate->dtms_scratch_ptr; 4165 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4166 uintptr_t s1 = tupregs[0].dttk_value; 4167 uintptr_t s2 = tupregs[1].dttk_value; 4168 int i = 0; 4169 4170 if (!dtrace_strcanload(s1, size, mstate, vstate) || 4171 !dtrace_strcanload(s2, size, mstate, vstate)) { 4172 regs[rd] = NULL; 4173 break; 4174 } 4175 4176 if (!DTRACE_INSCRATCH(mstate, size)) { 4177 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4178 regs[rd] = NULL; 4179 break; 4180 } 4181 4182 for (;;) { 4183 if (i >= size) { 4184 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4185 regs[rd] = NULL; 4186 break; 4187 } 4188 4189 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 4190 i--; 4191 break; 4192 } 4193 } 4194 4195 for (;;) { 4196 if (i >= size) { 4197 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4198 regs[rd] = NULL; 4199 break; 4200 } 4201 4202 if ((d[i++] = dtrace_load8(s2++)) == '\0') 4203 break; 4204 } 4205 4206 if (i < size) { 4207 mstate->dtms_scratch_ptr += i; 4208 regs[rd] = (uintptr_t)d; 4209 } 4210 4211 break; 4212 } 4213 4214 case DIF_SUBR_LLTOSTR: { 4215 int64_t i = (int64_t)tupregs[0].dttk_value; 4216 int64_t val = i < 0 ? i * -1 : i; 4217 uint64_t size = 22; /* enough room for 2^64 in decimal */ 4218 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 4219 4220 if (!DTRACE_INSCRATCH(mstate, size)) { 4221 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4222 regs[rd] = NULL; 4223 break; 4224 } 4225 4226 for (*end-- = '\0'; val; val /= 10) 4227 *end-- = '0' + (val % 10); 4228 4229 if (i == 0) 4230 *end-- = '0'; 4231 4232 if (i < 0) 4233 *end-- = '-'; 4234 4235 regs[rd] = (uintptr_t)end + 1; 4236 mstate->dtms_scratch_ptr += size; 4237 break; 4238 } 4239 4240 case DIF_SUBR_HTONS: 4241 case DIF_SUBR_NTOHS: 4242 #ifdef _BIG_ENDIAN 4243 regs[rd] = (uint16_t)tupregs[0].dttk_value; 4244 #else 4245 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 4246 #endif 4247 break; 4248 4249 4250 case DIF_SUBR_HTONL: 4251 case DIF_SUBR_NTOHL: 4252 #ifdef _BIG_ENDIAN 4253 regs[rd] = (uint32_t)tupregs[0].dttk_value; 4254 #else 4255 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 4256 #endif 4257 break; 4258 4259 4260 case DIF_SUBR_HTONLL: 4261 case DIF_SUBR_NTOHLL: 4262 #ifdef _BIG_ENDIAN 4263 regs[rd] = (uint64_t)tupregs[0].dttk_value; 4264 #else 4265 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 4266 #endif 4267 break; 4268 4269 4270 case DIF_SUBR_DIRNAME: 4271 case DIF_SUBR_BASENAME: { 4272 char *dest = (char *)mstate->dtms_scratch_ptr; 4273 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4274 uintptr_t src = tupregs[0].dttk_value; 4275 int i, j, len = dtrace_strlen((char *)src, size); 4276 int lastbase = -1, firstbase = -1, lastdir = -1; 4277 int start, end; 4278 4279 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 4280 regs[rd] = NULL; 4281 break; 4282 } 4283 4284 if (!DTRACE_INSCRATCH(mstate, size)) { 4285 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4286 regs[rd] = NULL; 4287 break; 4288 } 4289 4290 /* 4291 * The basename and dirname for a zero-length string is 4292 * defined to be "." 4293 */ 4294 if (len == 0) { 4295 len = 1; 4296 src = (uintptr_t)"."; 4297 } 4298 4299 /* 4300 * Start from the back of the string, moving back toward the 4301 * front until we see a character that isn't a slash. That 4302 * character is the last character in the basename. 4303 */ 4304 for (i = len - 1; i >= 0; i--) { 4305 if (dtrace_load8(src + i) != '/') 4306 break; 4307 } 4308 4309 if (i >= 0) 4310 lastbase = i; 4311 4312 /* 4313 * Starting from the last character in the basename, move 4314 * towards the front until we find a slash. The character 4315 * that we processed immediately before that is the first 4316 * character in the basename. 4317 */ 4318 for (; i >= 0; i--) { 4319 if (dtrace_load8(src + i) == '/') 4320 break; 4321 } 4322 4323 if (i >= 0) 4324 firstbase = i + 1; 4325 4326 /* 4327 * Now keep going until we find a non-slash character. That 4328 * character is the last character in the dirname. 4329 */ 4330 for (; i >= 0; i--) { 4331 if (dtrace_load8(src + i) != '/') 4332 break; 4333 } 4334 4335 if (i >= 0) 4336 lastdir = i; 4337 4338 ASSERT(!(lastbase == -1 && firstbase != -1)); 4339 ASSERT(!(firstbase == -1 && lastdir != -1)); 4340 4341 if (lastbase == -1) { 4342 /* 4343 * We didn't find a non-slash character. We know that 4344 * the length is non-zero, so the whole string must be 4345 * slashes. In either the dirname or the basename 4346 * case, we return '/'. 4347 */ 4348 ASSERT(firstbase == -1); 4349 firstbase = lastbase = lastdir = 0; 4350 } 4351 4352 if (firstbase == -1) { 4353 /* 4354 * The entire string consists only of a basename 4355 * component. If we're looking for dirname, we need 4356 * to change our string to be just "."; if we're 4357 * looking for a basename, we'll just set the first 4358 * character of the basename to be 0. 4359 */ 4360 if (subr == DIF_SUBR_DIRNAME) { 4361 ASSERT(lastdir == -1); 4362 src = (uintptr_t)"."; 4363 lastdir = 0; 4364 } else { 4365 firstbase = 0; 4366 } 4367 } 4368 4369 if (subr == DIF_SUBR_DIRNAME) { 4370 if (lastdir == -1) { 4371 /* 4372 * We know that we have a slash in the name -- 4373 * or lastdir would be set to 0, above. And 4374 * because lastdir is -1, we know that this 4375 * slash must be the first character. (That 4376 * is, the full string must be of the form 4377 * "/basename".) In this case, the last 4378 * character of the directory name is 0. 4379 */ 4380 lastdir = 0; 4381 } 4382 4383 start = 0; 4384 end = lastdir; 4385 } else { 4386 ASSERT(subr == DIF_SUBR_BASENAME); 4387 ASSERT(firstbase != -1 && lastbase != -1); 4388 start = firstbase; 4389 end = lastbase; 4390 } 4391 4392 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 4393 dest[j] = dtrace_load8(src + i); 4394 4395 dest[j] = '\0'; 4396 regs[rd] = (uintptr_t)dest; 4397 mstate->dtms_scratch_ptr += size; 4398 break; 4399 } 4400 4401 case DIF_SUBR_CLEANPATH: { 4402 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4403 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4404 uintptr_t src = tupregs[0].dttk_value; 4405 int i = 0, j = 0; 4406 4407 if (!dtrace_strcanload(src, size, mstate, vstate)) { 4408 regs[rd] = NULL; 4409 break; 4410 } 4411 4412 if (!DTRACE_INSCRATCH(mstate, size)) { 4413 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4414 regs[rd] = NULL; 4415 break; 4416 } 4417 4418 /* 4419 * Move forward, loading each character. 4420 */ 4421 do { 4422 c = dtrace_load8(src + i++); 4423 next: 4424 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 4425 break; 4426 4427 if (c != '/') { 4428 dest[j++] = c; 4429 continue; 4430 } 4431 4432 c = dtrace_load8(src + i++); 4433 4434 if (c == '/') { 4435 /* 4436 * We have two slashes -- we can just advance 4437 * to the next character. 4438 */ 4439 goto next; 4440 } 4441 4442 if (c != '.') { 4443 /* 4444 * This is not "." and it's not ".." -- we can 4445 * just store the "/" and this character and 4446 * drive on. 4447 */ 4448 dest[j++] = '/'; 4449 dest[j++] = c; 4450 continue; 4451 } 4452 4453 c = dtrace_load8(src + i++); 4454 4455 if (c == '/') { 4456 /* 4457 * This is a "/./" component. We're not going 4458 * to store anything in the destination buffer; 4459 * we're just going to go to the next component. 4460 */ 4461 goto next; 4462 } 4463 4464 if (c != '.') { 4465 /* 4466 * This is not ".." -- we can just store the 4467 * "/." and this character and continue 4468 * processing. 4469 */ 4470 dest[j++] = '/'; 4471 dest[j++] = '.'; 4472 dest[j++] = c; 4473 continue; 4474 } 4475 4476 c = dtrace_load8(src + i++); 4477 4478 if (c != '/' && c != '\0') { 4479 /* 4480 * This is not ".." -- it's "..[mumble]". 4481 * We'll store the "/.." and this character 4482 * and continue processing. 4483 */ 4484 dest[j++] = '/'; 4485 dest[j++] = '.'; 4486 dest[j++] = '.'; 4487 dest[j++] = c; 4488 continue; 4489 } 4490 4491 /* 4492 * This is "/../" or "/..\0". We need to back up 4493 * our destination pointer until we find a "/". 4494 */ 4495 i--; 4496 while (j != 0 && dest[--j] != '/') 4497 continue; 4498 4499 if (c == '\0') 4500 dest[++j] = '/'; 4501 } while (c != '\0'); 4502 4503 dest[j] = '\0'; 4504 regs[rd] = (uintptr_t)dest; 4505 mstate->dtms_scratch_ptr += size; 4506 break; 4507 } 4508 4509 case DIF_SUBR_INET_NTOA: 4510 case DIF_SUBR_INET_NTOA6: 4511 case DIF_SUBR_INET_NTOP: { 4512 size_t size; 4513 int af, argi, i; 4514 char *base, *end; 4515 4516 if (subr == DIF_SUBR_INET_NTOP) { 4517 af = (int)tupregs[0].dttk_value; 4518 argi = 1; 4519 } else { 4520 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 4521 argi = 0; 4522 } 4523 4524 if (af == AF_INET) { 4525 ipaddr_t ip4; 4526 uint8_t *ptr8, val; 4527 4528 /* 4529 * Safely load the IPv4 address. 4530 */ 4531 ip4 = dtrace_load32(tupregs[argi].dttk_value); 4532 4533 /* 4534 * Check an IPv4 string will fit in scratch. 4535 */ 4536 size = INET_ADDRSTRLEN; 4537 if (!DTRACE_INSCRATCH(mstate, size)) { 4538 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4539 regs[rd] = NULL; 4540 break; 4541 } 4542 base = (char *)mstate->dtms_scratch_ptr; 4543 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4544 4545 /* 4546 * Stringify as a dotted decimal quad. 4547 */ 4548 *end-- = '\0'; 4549 ptr8 = (uint8_t *)&ip4; 4550 for (i = 3; i >= 0; i--) { 4551 val = ptr8[i]; 4552 4553 if (val == 0) { 4554 *end-- = '0'; 4555 } else { 4556 for (; val; val /= 10) { 4557 *end-- = '0' + (val % 10); 4558 } 4559 } 4560 4561 if (i > 0) 4562 *end-- = '.'; 4563 } 4564 ASSERT(end + 1 >= base); 4565 4566 } else if (af == AF_INET6) { 4567 struct in6_addr ip6; 4568 int firstzero, tryzero, numzero, v6end; 4569 uint16_t val; 4570 const char digits[] = "0123456789abcdef"; 4571 4572 /* 4573 * Stringify using RFC 1884 convention 2 - 16 bit 4574 * hexadecimal values with a zero-run compression. 4575 * Lower case hexadecimal digits are used. 4576 * eg, fe80::214:4fff:fe0b:76c8. 4577 * The IPv4 embedded form is returned for inet_ntop, 4578 * just the IPv4 string is returned for inet_ntoa6. 4579 */ 4580 4581 /* 4582 * Safely load the IPv6 address. 4583 */ 4584 dtrace_bcopy( 4585 (void *)(uintptr_t)tupregs[argi].dttk_value, 4586 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 4587 4588 /* 4589 * Check an IPv6 string will fit in scratch. 4590 */ 4591 size = INET6_ADDRSTRLEN; 4592 if (!DTRACE_INSCRATCH(mstate, size)) { 4593 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4594 regs[rd] = NULL; 4595 break; 4596 } 4597 base = (char *)mstate->dtms_scratch_ptr; 4598 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4599 *end-- = '\0'; 4600 4601 /* 4602 * Find the longest run of 16 bit zero values 4603 * for the single allowed zero compression - "::". 4604 */ 4605 firstzero = -1; 4606 tryzero = -1; 4607 numzero = 1; 4608 for (i = 0; i < sizeof (struct in6_addr); i++) { 4609 if (ip6._S6_un._S6_u8[i] == 0 && 4610 tryzero == -1 && i % 2 == 0) { 4611 tryzero = i; 4612 continue; 4613 } 4614 4615 if (tryzero != -1 && 4616 (ip6._S6_un._S6_u8[i] != 0 || 4617 i == sizeof (struct in6_addr) - 1)) { 4618 4619 if (i - tryzero <= numzero) { 4620 tryzero = -1; 4621 continue; 4622 } 4623 4624 firstzero = tryzero; 4625 numzero = i - i % 2 - tryzero; 4626 tryzero = -1; 4627 4628 if (ip6._S6_un._S6_u8[i] == 0 && 4629 i == sizeof (struct in6_addr) - 1) 4630 numzero += 2; 4631 } 4632 } 4633 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 4634 4635 /* 4636 * Check for an IPv4 embedded address. 4637 */ 4638 v6end = sizeof (struct in6_addr) - 2; 4639 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 4640 IN6_IS_ADDR_V4COMPAT(&ip6)) { 4641 for (i = sizeof (struct in6_addr) - 1; 4642 i >= DTRACE_V4MAPPED_OFFSET; i--) { 4643 ASSERT(end >= base); 4644 4645 val = ip6._S6_un._S6_u8[i]; 4646 4647 if (val == 0) { 4648 *end-- = '0'; 4649 } else { 4650 for (; val; val /= 10) { 4651 *end-- = '0' + val % 10; 4652 } 4653 } 4654 4655 if (i > DTRACE_V4MAPPED_OFFSET) 4656 *end-- = '.'; 4657 } 4658 4659 if (subr == DIF_SUBR_INET_NTOA6) 4660 goto inetout; 4661 4662 /* 4663 * Set v6end to skip the IPv4 address that 4664 * we have already stringified. 4665 */ 4666 v6end = 10; 4667 } 4668 4669 /* 4670 * Build the IPv6 string by working through the 4671 * address in reverse. 4672 */ 4673 for (i = v6end; i >= 0; i -= 2) { 4674 ASSERT(end >= base); 4675 4676 if (i == firstzero + numzero - 2) { 4677 *end-- = ':'; 4678 *end-- = ':'; 4679 i -= numzero - 2; 4680 continue; 4681 } 4682 4683 if (i < 14 && i != firstzero - 2) 4684 *end-- = ':'; 4685 4686 val = (ip6._S6_un._S6_u8[i] << 8) + 4687 ip6._S6_un._S6_u8[i + 1]; 4688 4689 if (val == 0) { 4690 *end-- = '0'; 4691 } else { 4692 for (; val; val /= 16) { 4693 *end-- = digits[val % 16]; 4694 } 4695 } 4696 } 4697 ASSERT(end + 1 >= base); 4698 4699 } else { 4700 /* 4701 * The user didn't use AH_INET or AH_INET6. 4702 */ 4703 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4704 regs[rd] = NULL; 4705 break; 4706 } 4707 4708 inetout: regs[rd] = (uintptr_t)end + 1; 4709 mstate->dtms_scratch_ptr += size; 4710 break; 4711 } 4712 4713 } 4714 } 4715 4716 /* 4717 * Emulate the execution of DTrace IR instructions specified by the given 4718 * DIF object. This function is deliberately void of assertions as all of 4719 * the necessary checks are handled by a call to dtrace_difo_validate(). 4720 */ 4721 static uint64_t 4722 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 4723 dtrace_vstate_t *vstate, dtrace_state_t *state) 4724 { 4725 const dif_instr_t *text = difo->dtdo_buf; 4726 const uint_t textlen = difo->dtdo_len; 4727 const char *strtab = difo->dtdo_strtab; 4728 const uint64_t *inttab = difo->dtdo_inttab; 4729 4730 uint64_t rval = 0; 4731 dtrace_statvar_t *svar; 4732 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 4733 dtrace_difv_t *v; 4734 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 4735 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 4736 4737 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 4738 uint64_t regs[DIF_DIR_NREGS]; 4739 uint64_t *tmp; 4740 4741 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 4742 int64_t cc_r; 4743 uint_t pc = 0, id, opc; 4744 uint8_t ttop = 0; 4745 dif_instr_t instr; 4746 uint_t r1, r2, rd; 4747 4748 /* 4749 * We stash the current DIF object into the machine state: we need it 4750 * for subsequent access checking. 4751 */ 4752 mstate->dtms_difo = difo; 4753 4754 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 4755 4756 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 4757 opc = pc; 4758 4759 instr = text[pc++]; 4760 r1 = DIF_INSTR_R1(instr); 4761 r2 = DIF_INSTR_R2(instr); 4762 rd = DIF_INSTR_RD(instr); 4763 4764 switch (DIF_INSTR_OP(instr)) { 4765 case DIF_OP_OR: 4766 regs[rd] = regs[r1] | regs[r2]; 4767 break; 4768 case DIF_OP_XOR: 4769 regs[rd] = regs[r1] ^ regs[r2]; 4770 break; 4771 case DIF_OP_AND: 4772 regs[rd] = regs[r1] & regs[r2]; 4773 break; 4774 case DIF_OP_SLL: 4775 regs[rd] = regs[r1] << regs[r2]; 4776 break; 4777 case DIF_OP_SRL: 4778 regs[rd] = regs[r1] >> regs[r2]; 4779 break; 4780 case DIF_OP_SUB: 4781 regs[rd] = regs[r1] - regs[r2]; 4782 break; 4783 case DIF_OP_ADD: 4784 regs[rd] = regs[r1] + regs[r2]; 4785 break; 4786 case DIF_OP_MUL: 4787 regs[rd] = regs[r1] * regs[r2]; 4788 break; 4789 case DIF_OP_SDIV: 4790 if (regs[r2] == 0) { 4791 regs[rd] = 0; 4792 *flags |= CPU_DTRACE_DIVZERO; 4793 } else { 4794 regs[rd] = (int64_t)regs[r1] / 4795 (int64_t)regs[r2]; 4796 } 4797 break; 4798 4799 case DIF_OP_UDIV: 4800 if (regs[r2] == 0) { 4801 regs[rd] = 0; 4802 *flags |= CPU_DTRACE_DIVZERO; 4803 } else { 4804 regs[rd] = regs[r1] / regs[r2]; 4805 } 4806 break; 4807 4808 case DIF_OP_SREM: 4809 if (regs[r2] == 0) { 4810 regs[rd] = 0; 4811 *flags |= CPU_DTRACE_DIVZERO; 4812 } else { 4813 regs[rd] = (int64_t)regs[r1] % 4814 (int64_t)regs[r2]; 4815 } 4816 break; 4817 4818 case DIF_OP_UREM: 4819 if (regs[r2] == 0) { 4820 regs[rd] = 0; 4821 *flags |= CPU_DTRACE_DIVZERO; 4822 } else { 4823 regs[rd] = regs[r1] % regs[r2]; 4824 } 4825 break; 4826 4827 case DIF_OP_NOT: 4828 regs[rd] = ~regs[r1]; 4829 break; 4830 case DIF_OP_MOV: 4831 regs[rd] = regs[r1]; 4832 break; 4833 case DIF_OP_CMP: 4834 cc_r = regs[r1] - regs[r2]; 4835 cc_n = cc_r < 0; 4836 cc_z = cc_r == 0; 4837 cc_v = 0; 4838 cc_c = regs[r1] < regs[r2]; 4839 break; 4840 case DIF_OP_TST: 4841 cc_n = cc_v = cc_c = 0; 4842 cc_z = regs[r1] == 0; 4843 break; 4844 case DIF_OP_BA: 4845 pc = DIF_INSTR_LABEL(instr); 4846 break; 4847 case DIF_OP_BE: 4848 if (cc_z) 4849 pc = DIF_INSTR_LABEL(instr); 4850 break; 4851 case DIF_OP_BNE: 4852 if (cc_z == 0) 4853 pc = DIF_INSTR_LABEL(instr); 4854 break; 4855 case DIF_OP_BG: 4856 if ((cc_z | (cc_n ^ cc_v)) == 0) 4857 pc = DIF_INSTR_LABEL(instr); 4858 break; 4859 case DIF_OP_BGU: 4860 if ((cc_c | cc_z) == 0) 4861 pc = DIF_INSTR_LABEL(instr); 4862 break; 4863 case DIF_OP_BGE: 4864 if ((cc_n ^ cc_v) == 0) 4865 pc = DIF_INSTR_LABEL(instr); 4866 break; 4867 case DIF_OP_BGEU: 4868 if (cc_c == 0) 4869 pc = DIF_INSTR_LABEL(instr); 4870 break; 4871 case DIF_OP_BL: 4872 if (cc_n ^ cc_v) 4873 pc = DIF_INSTR_LABEL(instr); 4874 break; 4875 case DIF_OP_BLU: 4876 if (cc_c) 4877 pc = DIF_INSTR_LABEL(instr); 4878 break; 4879 case DIF_OP_BLE: 4880 if (cc_z | (cc_n ^ cc_v)) 4881 pc = DIF_INSTR_LABEL(instr); 4882 break; 4883 case DIF_OP_BLEU: 4884 if (cc_c | cc_z) 4885 pc = DIF_INSTR_LABEL(instr); 4886 break; 4887 case DIF_OP_RLDSB: 4888 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 4889 *flags |= CPU_DTRACE_KPRIV; 4890 *illval = regs[r1]; 4891 break; 4892 } 4893 /*FALLTHROUGH*/ 4894 case DIF_OP_LDSB: 4895 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 4896 break; 4897 case DIF_OP_RLDSH: 4898 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 4899 *flags |= CPU_DTRACE_KPRIV; 4900 *illval = regs[r1]; 4901 break; 4902 } 4903 /*FALLTHROUGH*/ 4904 case DIF_OP_LDSH: 4905 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 4906 break; 4907 case DIF_OP_RLDSW: 4908 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 4909 *flags |= CPU_DTRACE_KPRIV; 4910 *illval = regs[r1]; 4911 break; 4912 } 4913 /*FALLTHROUGH*/ 4914 case DIF_OP_LDSW: 4915 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 4916 break; 4917 case DIF_OP_RLDUB: 4918 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 4919 *flags |= CPU_DTRACE_KPRIV; 4920 *illval = regs[r1]; 4921 break; 4922 } 4923 /*FALLTHROUGH*/ 4924 case DIF_OP_LDUB: 4925 regs[rd] = dtrace_load8(regs[r1]); 4926 break; 4927 case DIF_OP_RLDUH: 4928 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 4929 *flags |= CPU_DTRACE_KPRIV; 4930 *illval = regs[r1]; 4931 break; 4932 } 4933 /*FALLTHROUGH*/ 4934 case DIF_OP_LDUH: 4935 regs[rd] = dtrace_load16(regs[r1]); 4936 break; 4937 case DIF_OP_RLDUW: 4938 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 4939 *flags |= CPU_DTRACE_KPRIV; 4940 *illval = regs[r1]; 4941 break; 4942 } 4943 /*FALLTHROUGH*/ 4944 case DIF_OP_LDUW: 4945 regs[rd] = dtrace_load32(regs[r1]); 4946 break; 4947 case DIF_OP_RLDX: 4948 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 4949 *flags |= CPU_DTRACE_KPRIV; 4950 *illval = regs[r1]; 4951 break; 4952 } 4953 /*FALLTHROUGH*/ 4954 case DIF_OP_LDX: 4955 regs[rd] = dtrace_load64(regs[r1]); 4956 break; 4957 case DIF_OP_ULDSB: 4958 regs[rd] = (int8_t) 4959 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 4960 break; 4961 case DIF_OP_ULDSH: 4962 regs[rd] = (int16_t) 4963 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 4964 break; 4965 case DIF_OP_ULDSW: 4966 regs[rd] = (int32_t) 4967 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 4968 break; 4969 case DIF_OP_ULDUB: 4970 regs[rd] = 4971 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 4972 break; 4973 case DIF_OP_ULDUH: 4974 regs[rd] = 4975 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 4976 break; 4977 case DIF_OP_ULDUW: 4978 regs[rd] = 4979 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 4980 break; 4981 case DIF_OP_ULDX: 4982 regs[rd] = 4983 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 4984 break; 4985 case DIF_OP_RET: 4986 rval = regs[rd]; 4987 pc = textlen; 4988 break; 4989 case DIF_OP_NOP: 4990 break; 4991 case DIF_OP_SETX: 4992 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 4993 break; 4994 case DIF_OP_SETS: 4995 regs[rd] = (uint64_t)(uintptr_t) 4996 (strtab + DIF_INSTR_STRING(instr)); 4997 break; 4998 case DIF_OP_SCMP: { 4999 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 5000 uintptr_t s1 = regs[r1]; 5001 uintptr_t s2 = regs[r2]; 5002 5003 if (s1 != NULL && 5004 !dtrace_strcanload(s1, sz, mstate, vstate)) 5005 break; 5006 if (s2 != NULL && 5007 !dtrace_strcanload(s2, sz, mstate, vstate)) 5008 break; 5009 5010 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 5011 5012 cc_n = cc_r < 0; 5013 cc_z = cc_r == 0; 5014 cc_v = cc_c = 0; 5015 break; 5016 } 5017 case DIF_OP_LDGA: 5018 regs[rd] = dtrace_dif_variable(mstate, state, 5019 r1, regs[r2]); 5020 break; 5021 case DIF_OP_LDGS: 5022 id = DIF_INSTR_VAR(instr); 5023 5024 if (id >= DIF_VAR_OTHER_UBASE) { 5025 uintptr_t a; 5026 5027 id -= DIF_VAR_OTHER_UBASE; 5028 svar = vstate->dtvs_globals[id]; 5029 ASSERT(svar != NULL); 5030 v = &svar->dtsv_var; 5031 5032 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 5033 regs[rd] = svar->dtsv_data; 5034 break; 5035 } 5036 5037 a = (uintptr_t)svar->dtsv_data; 5038 5039 if (*(uint8_t *)a == UINT8_MAX) { 5040 /* 5041 * If the 0th byte is set to UINT8_MAX 5042 * then this is to be treated as a 5043 * reference to a NULL variable. 5044 */ 5045 regs[rd] = NULL; 5046 } else { 5047 regs[rd] = a + sizeof (uint64_t); 5048 } 5049 5050 break; 5051 } 5052 5053 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 5054 break; 5055 5056 case DIF_OP_STGS: 5057 id = DIF_INSTR_VAR(instr); 5058 5059 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5060 id -= DIF_VAR_OTHER_UBASE; 5061 5062 svar = vstate->dtvs_globals[id]; 5063 ASSERT(svar != NULL); 5064 v = &svar->dtsv_var; 5065 5066 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5067 uintptr_t a = (uintptr_t)svar->dtsv_data; 5068 5069 ASSERT(a != NULL); 5070 ASSERT(svar->dtsv_size != 0); 5071 5072 if (regs[rd] == NULL) { 5073 *(uint8_t *)a = UINT8_MAX; 5074 break; 5075 } else { 5076 *(uint8_t *)a = 0; 5077 a += sizeof (uint64_t); 5078 } 5079 if (!dtrace_vcanload( 5080 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5081 mstate, vstate)) 5082 break; 5083 5084 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5085 (void *)a, &v->dtdv_type); 5086 break; 5087 } 5088 5089 svar->dtsv_data = regs[rd]; 5090 break; 5091 5092 case DIF_OP_LDTA: 5093 /* 5094 * There are no DTrace built-in thread-local arrays at 5095 * present. This opcode is saved for future work. 5096 */ 5097 *flags |= CPU_DTRACE_ILLOP; 5098 regs[rd] = 0; 5099 break; 5100 5101 case DIF_OP_LDLS: 5102 id = DIF_INSTR_VAR(instr); 5103 5104 if (id < DIF_VAR_OTHER_UBASE) { 5105 /* 5106 * For now, this has no meaning. 5107 */ 5108 regs[rd] = 0; 5109 break; 5110 } 5111 5112 id -= DIF_VAR_OTHER_UBASE; 5113 5114 ASSERT(id < vstate->dtvs_nlocals); 5115 ASSERT(vstate->dtvs_locals != NULL); 5116 5117 svar = vstate->dtvs_locals[id]; 5118 ASSERT(svar != NULL); 5119 v = &svar->dtsv_var; 5120 5121 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5122 uintptr_t a = (uintptr_t)svar->dtsv_data; 5123 size_t sz = v->dtdv_type.dtdt_size; 5124 5125 sz += sizeof (uint64_t); 5126 ASSERT(svar->dtsv_size == NCPU * sz); 5127 a += CPU->cpu_id * sz; 5128 5129 if (*(uint8_t *)a == UINT8_MAX) { 5130 /* 5131 * If the 0th byte is set to UINT8_MAX 5132 * then this is to be treated as a 5133 * reference to a NULL variable. 5134 */ 5135 regs[rd] = NULL; 5136 } else { 5137 regs[rd] = a + sizeof (uint64_t); 5138 } 5139 5140 break; 5141 } 5142 5143 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5144 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5145 regs[rd] = tmp[CPU->cpu_id]; 5146 break; 5147 5148 case DIF_OP_STLS: 5149 id = DIF_INSTR_VAR(instr); 5150 5151 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5152 id -= DIF_VAR_OTHER_UBASE; 5153 ASSERT(id < vstate->dtvs_nlocals); 5154 5155 ASSERT(vstate->dtvs_locals != NULL); 5156 svar = vstate->dtvs_locals[id]; 5157 ASSERT(svar != NULL); 5158 v = &svar->dtsv_var; 5159 5160 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5161 uintptr_t a = (uintptr_t)svar->dtsv_data; 5162 size_t sz = v->dtdv_type.dtdt_size; 5163 5164 sz += sizeof (uint64_t); 5165 ASSERT(svar->dtsv_size == NCPU * sz); 5166 a += CPU->cpu_id * sz; 5167 5168 if (regs[rd] == NULL) { 5169 *(uint8_t *)a = UINT8_MAX; 5170 break; 5171 } else { 5172 *(uint8_t *)a = 0; 5173 a += sizeof (uint64_t); 5174 } 5175 5176 if (!dtrace_vcanload( 5177 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5178 mstate, vstate)) 5179 break; 5180 5181 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5182 (void *)a, &v->dtdv_type); 5183 break; 5184 } 5185 5186 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5187 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5188 tmp[CPU->cpu_id] = regs[rd]; 5189 break; 5190 5191 case DIF_OP_LDTS: { 5192 dtrace_dynvar_t *dvar; 5193 dtrace_key_t *key; 5194 5195 id = DIF_INSTR_VAR(instr); 5196 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5197 id -= DIF_VAR_OTHER_UBASE; 5198 v = &vstate->dtvs_tlocals[id]; 5199 5200 key = &tupregs[DIF_DTR_NREGS]; 5201 key[0].dttk_value = (uint64_t)id; 5202 key[0].dttk_size = 0; 5203 DTRACE_TLS_THRKEY(key[1].dttk_value); 5204 key[1].dttk_size = 0; 5205 5206 dvar = dtrace_dynvar(dstate, 2, key, 5207 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 5208 mstate, vstate); 5209 5210 if (dvar == NULL) { 5211 regs[rd] = 0; 5212 break; 5213 } 5214 5215 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5216 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5217 } else { 5218 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5219 } 5220 5221 break; 5222 } 5223 5224 case DIF_OP_STTS: { 5225 dtrace_dynvar_t *dvar; 5226 dtrace_key_t *key; 5227 5228 id = DIF_INSTR_VAR(instr); 5229 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5230 id -= DIF_VAR_OTHER_UBASE; 5231 5232 key = &tupregs[DIF_DTR_NREGS]; 5233 key[0].dttk_value = (uint64_t)id; 5234 key[0].dttk_size = 0; 5235 DTRACE_TLS_THRKEY(key[1].dttk_value); 5236 key[1].dttk_size = 0; 5237 v = &vstate->dtvs_tlocals[id]; 5238 5239 dvar = dtrace_dynvar(dstate, 2, key, 5240 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5241 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5242 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5243 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5244 5245 /* 5246 * Given that we're storing to thread-local data, 5247 * we need to flush our predicate cache. 5248 */ 5249 curthread->t_predcache = NULL; 5250 5251 if (dvar == NULL) 5252 break; 5253 5254 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5255 if (!dtrace_vcanload( 5256 (void *)(uintptr_t)regs[rd], 5257 &v->dtdv_type, mstate, vstate)) 5258 break; 5259 5260 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5261 dvar->dtdv_data, &v->dtdv_type); 5262 } else { 5263 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5264 } 5265 5266 break; 5267 } 5268 5269 case DIF_OP_SRA: 5270 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 5271 break; 5272 5273 case DIF_OP_CALL: 5274 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 5275 regs, tupregs, ttop, mstate, state); 5276 break; 5277 5278 case DIF_OP_PUSHTR: 5279 if (ttop == DIF_DTR_NREGS) { 5280 *flags |= CPU_DTRACE_TUPOFLOW; 5281 break; 5282 } 5283 5284 if (r1 == DIF_TYPE_STRING) { 5285 /* 5286 * If this is a string type and the size is 0, 5287 * we'll use the system-wide default string 5288 * size. Note that we are _not_ looking at 5289 * the value of the DTRACEOPT_STRSIZE option; 5290 * had this been set, we would expect to have 5291 * a non-zero size value in the "pushtr". 5292 */ 5293 tupregs[ttop].dttk_size = 5294 dtrace_strlen((char *)(uintptr_t)regs[rd], 5295 regs[r2] ? regs[r2] : 5296 dtrace_strsize_default) + 1; 5297 } else { 5298 tupregs[ttop].dttk_size = regs[r2]; 5299 } 5300 5301 tupregs[ttop++].dttk_value = regs[rd]; 5302 break; 5303 5304 case DIF_OP_PUSHTV: 5305 if (ttop == DIF_DTR_NREGS) { 5306 *flags |= CPU_DTRACE_TUPOFLOW; 5307 break; 5308 } 5309 5310 tupregs[ttop].dttk_value = regs[rd]; 5311 tupregs[ttop++].dttk_size = 0; 5312 break; 5313 5314 case DIF_OP_POPTS: 5315 if (ttop != 0) 5316 ttop--; 5317 break; 5318 5319 case DIF_OP_FLUSHTS: 5320 ttop = 0; 5321 break; 5322 5323 case DIF_OP_LDGAA: 5324 case DIF_OP_LDTAA: { 5325 dtrace_dynvar_t *dvar; 5326 dtrace_key_t *key = tupregs; 5327 uint_t nkeys = ttop; 5328 5329 id = DIF_INSTR_VAR(instr); 5330 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5331 id -= DIF_VAR_OTHER_UBASE; 5332 5333 key[nkeys].dttk_value = (uint64_t)id; 5334 key[nkeys++].dttk_size = 0; 5335 5336 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 5337 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5338 key[nkeys++].dttk_size = 0; 5339 v = &vstate->dtvs_tlocals[id]; 5340 } else { 5341 v = &vstate->dtvs_globals[id]->dtsv_var; 5342 } 5343 5344 dvar = dtrace_dynvar(dstate, nkeys, key, 5345 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5346 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5347 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 5348 5349 if (dvar == NULL) { 5350 regs[rd] = 0; 5351 break; 5352 } 5353 5354 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5355 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5356 } else { 5357 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5358 } 5359 5360 break; 5361 } 5362 5363 case DIF_OP_STGAA: 5364 case DIF_OP_STTAA: { 5365 dtrace_dynvar_t *dvar; 5366 dtrace_key_t *key = tupregs; 5367 uint_t nkeys = ttop; 5368 5369 id = DIF_INSTR_VAR(instr); 5370 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5371 id -= DIF_VAR_OTHER_UBASE; 5372 5373 key[nkeys].dttk_value = (uint64_t)id; 5374 key[nkeys++].dttk_size = 0; 5375 5376 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 5377 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5378 key[nkeys++].dttk_size = 0; 5379 v = &vstate->dtvs_tlocals[id]; 5380 } else { 5381 v = &vstate->dtvs_globals[id]->dtsv_var; 5382 } 5383 5384 dvar = dtrace_dynvar(dstate, nkeys, key, 5385 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5386 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5387 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5388 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5389 5390 if (dvar == NULL) 5391 break; 5392 5393 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5394 if (!dtrace_vcanload( 5395 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5396 mstate, vstate)) 5397 break; 5398 5399 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5400 dvar->dtdv_data, &v->dtdv_type); 5401 } else { 5402 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5403 } 5404 5405 break; 5406 } 5407 5408 case DIF_OP_ALLOCS: { 5409 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5410 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 5411 5412 /* 5413 * Rounding up the user allocation size could have 5414 * overflowed large, bogus allocations (like -1ULL) to 5415 * 0. 5416 */ 5417 if (size < regs[r1] || 5418 !DTRACE_INSCRATCH(mstate, size)) { 5419 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5420 regs[rd] = NULL; 5421 break; 5422 } 5423 5424 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 5425 mstate->dtms_scratch_ptr += size; 5426 regs[rd] = ptr; 5427 break; 5428 } 5429 5430 case DIF_OP_COPYS: 5431 if (!dtrace_canstore(regs[rd], regs[r2], 5432 mstate, vstate)) { 5433 *flags |= CPU_DTRACE_BADADDR; 5434 *illval = regs[rd]; 5435 break; 5436 } 5437 5438 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 5439 break; 5440 5441 dtrace_bcopy((void *)(uintptr_t)regs[r1], 5442 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 5443 break; 5444 5445 case DIF_OP_STB: 5446 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 5447 *flags |= CPU_DTRACE_BADADDR; 5448 *illval = regs[rd]; 5449 break; 5450 } 5451 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 5452 break; 5453 5454 case DIF_OP_STH: 5455 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 5456 *flags |= CPU_DTRACE_BADADDR; 5457 *illval = regs[rd]; 5458 break; 5459 } 5460 if (regs[rd] & 1) { 5461 *flags |= CPU_DTRACE_BADALIGN; 5462 *illval = regs[rd]; 5463 break; 5464 } 5465 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 5466 break; 5467 5468 case DIF_OP_STW: 5469 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 5470 *flags |= CPU_DTRACE_BADADDR; 5471 *illval = regs[rd]; 5472 break; 5473 } 5474 if (regs[rd] & 3) { 5475 *flags |= CPU_DTRACE_BADALIGN; 5476 *illval = regs[rd]; 5477 break; 5478 } 5479 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 5480 break; 5481 5482 case DIF_OP_STX: 5483 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 5484 *flags |= CPU_DTRACE_BADADDR; 5485 *illval = regs[rd]; 5486 break; 5487 } 5488 if (regs[rd] & 7) { 5489 *flags |= CPU_DTRACE_BADALIGN; 5490 *illval = regs[rd]; 5491 break; 5492 } 5493 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 5494 break; 5495 } 5496 } 5497 5498 if (!(*flags & CPU_DTRACE_FAULT)) 5499 return (rval); 5500 5501 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 5502 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 5503 5504 return (0); 5505 } 5506 5507 static void 5508 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 5509 { 5510 dtrace_probe_t *probe = ecb->dte_probe; 5511 dtrace_provider_t *prov = probe->dtpr_provider; 5512 char c[DTRACE_FULLNAMELEN + 80], *str; 5513 char *msg = "dtrace: breakpoint action at probe "; 5514 char *ecbmsg = " (ecb "; 5515 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 5516 uintptr_t val = (uintptr_t)ecb; 5517 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 5518 5519 if (dtrace_destructive_disallow) 5520 return; 5521 5522 /* 5523 * It's impossible to be taking action on the NULL probe. 5524 */ 5525 ASSERT(probe != NULL); 5526 5527 /* 5528 * This is a poor man's (destitute man's?) sprintf(): we want to 5529 * print the provider name, module name, function name and name of 5530 * the probe, along with the hex address of the ECB with the breakpoint 5531 * action -- all of which we must place in the character buffer by 5532 * hand. 5533 */ 5534 while (*msg != '\0') 5535 c[i++] = *msg++; 5536 5537 for (str = prov->dtpv_name; *str != '\0'; str++) 5538 c[i++] = *str; 5539 c[i++] = ':'; 5540 5541 for (str = probe->dtpr_mod; *str != '\0'; str++) 5542 c[i++] = *str; 5543 c[i++] = ':'; 5544 5545 for (str = probe->dtpr_func; *str != '\0'; str++) 5546 c[i++] = *str; 5547 c[i++] = ':'; 5548 5549 for (str = probe->dtpr_name; *str != '\0'; str++) 5550 c[i++] = *str; 5551 5552 while (*ecbmsg != '\0') 5553 c[i++] = *ecbmsg++; 5554 5555 while (shift >= 0) { 5556 mask = (uintptr_t)0xf << shift; 5557 5558 if (val >= ((uintptr_t)1 << shift)) 5559 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 5560 shift -= 4; 5561 } 5562 5563 c[i++] = ')'; 5564 c[i] = '\0'; 5565 5566 debug_enter(c); 5567 } 5568 5569 static void 5570 dtrace_action_panic(dtrace_ecb_t *ecb) 5571 { 5572 dtrace_probe_t *probe = ecb->dte_probe; 5573 5574 /* 5575 * It's impossible to be taking action on the NULL probe. 5576 */ 5577 ASSERT(probe != NULL); 5578 5579 if (dtrace_destructive_disallow) 5580 return; 5581 5582 if (dtrace_panicked != NULL) 5583 return; 5584 5585 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 5586 return; 5587 5588 /* 5589 * We won the right to panic. (We want to be sure that only one 5590 * thread calls panic() from dtrace_probe(), and that panic() is 5591 * called exactly once.) 5592 */ 5593 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 5594 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 5595 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 5596 } 5597 5598 static void 5599 dtrace_action_raise(uint64_t sig) 5600 { 5601 if (dtrace_destructive_disallow) 5602 return; 5603 5604 if (sig >= NSIG) { 5605 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5606 return; 5607 } 5608 5609 /* 5610 * raise() has a queue depth of 1 -- we ignore all subsequent 5611 * invocations of the raise() action. 5612 */ 5613 if (curthread->t_dtrace_sig == 0) 5614 curthread->t_dtrace_sig = (uint8_t)sig; 5615 5616 curthread->t_sig_check = 1; 5617 aston(curthread); 5618 } 5619 5620 static void 5621 dtrace_action_stop(void) 5622 { 5623 if (dtrace_destructive_disallow) 5624 return; 5625 5626 if (!curthread->t_dtrace_stop) { 5627 curthread->t_dtrace_stop = 1; 5628 curthread->t_sig_check = 1; 5629 aston(curthread); 5630 } 5631 } 5632 5633 static void 5634 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 5635 { 5636 hrtime_t now; 5637 volatile uint16_t *flags; 5638 cpu_t *cpu = CPU; 5639 5640 if (dtrace_destructive_disallow) 5641 return; 5642 5643 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 5644 5645 now = dtrace_gethrtime(); 5646 5647 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 5648 /* 5649 * We need to advance the mark to the current time. 5650 */ 5651 cpu->cpu_dtrace_chillmark = now; 5652 cpu->cpu_dtrace_chilled = 0; 5653 } 5654 5655 /* 5656 * Now check to see if the requested chill time would take us over 5657 * the maximum amount of time allowed in the chill interval. (Or 5658 * worse, if the calculation itself induces overflow.) 5659 */ 5660 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 5661 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 5662 *flags |= CPU_DTRACE_ILLOP; 5663 return; 5664 } 5665 5666 while (dtrace_gethrtime() - now < val) 5667 continue; 5668 5669 /* 5670 * Normally, we assure that the value of the variable "timestamp" does 5671 * not change within an ECB. The presence of chill() represents an 5672 * exception to this rule, however. 5673 */ 5674 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 5675 cpu->cpu_dtrace_chilled += val; 5676 } 5677 5678 static void 5679 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 5680 uint64_t *buf, uint64_t arg) 5681 { 5682 int nframes = DTRACE_USTACK_NFRAMES(arg); 5683 int strsize = DTRACE_USTACK_STRSIZE(arg); 5684 uint64_t *pcs = &buf[1], *fps; 5685 char *str = (char *)&pcs[nframes]; 5686 int size, offs = 0, i, j; 5687 uintptr_t old = mstate->dtms_scratch_ptr, saved; 5688 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 5689 char *sym; 5690 5691 /* 5692 * Should be taking a faster path if string space has not been 5693 * allocated. 5694 */ 5695 ASSERT(strsize != 0); 5696 5697 /* 5698 * We will first allocate some temporary space for the frame pointers. 5699 */ 5700 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5701 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 5702 (nframes * sizeof (uint64_t)); 5703 5704 if (!DTRACE_INSCRATCH(mstate, size)) { 5705 /* 5706 * Not enough room for our frame pointers -- need to indicate 5707 * that we ran out of scratch space. 5708 */ 5709 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5710 return; 5711 } 5712 5713 mstate->dtms_scratch_ptr += size; 5714 saved = mstate->dtms_scratch_ptr; 5715 5716 /* 5717 * Now get a stack with both program counters and frame pointers. 5718 */ 5719 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5720 dtrace_getufpstack(buf, fps, nframes + 1); 5721 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5722 5723 /* 5724 * If that faulted, we're cooked. 5725 */ 5726 if (*flags & CPU_DTRACE_FAULT) 5727 goto out; 5728 5729 /* 5730 * Now we want to walk up the stack, calling the USTACK helper. For 5731 * each iteration, we restore the scratch pointer. 5732 */ 5733 for (i = 0; i < nframes; i++) { 5734 mstate->dtms_scratch_ptr = saved; 5735 5736 if (offs >= strsize) 5737 break; 5738 5739 sym = (char *)(uintptr_t)dtrace_helper( 5740 DTRACE_HELPER_ACTION_USTACK, 5741 mstate, state, pcs[i], fps[i]); 5742 5743 /* 5744 * If we faulted while running the helper, we're going to 5745 * clear the fault and null out the corresponding string. 5746 */ 5747 if (*flags & CPU_DTRACE_FAULT) { 5748 *flags &= ~CPU_DTRACE_FAULT; 5749 str[offs++] = '\0'; 5750 continue; 5751 } 5752 5753 if (sym == NULL) { 5754 str[offs++] = '\0'; 5755 continue; 5756 } 5757 5758 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5759 5760 /* 5761 * Now copy in the string that the helper returned to us. 5762 */ 5763 for (j = 0; offs + j < strsize; j++) { 5764 if ((str[offs + j] = sym[j]) == '\0') 5765 break; 5766 } 5767 5768 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5769 5770 offs += j + 1; 5771 } 5772 5773 if (offs >= strsize) { 5774 /* 5775 * If we didn't have room for all of the strings, we don't 5776 * abort processing -- this needn't be a fatal error -- but we 5777 * still want to increment a counter (dts_stkstroverflows) to 5778 * allow this condition to be warned about. (If this is from 5779 * a jstack() action, it is easily tuned via jstackstrsize.) 5780 */ 5781 dtrace_error(&state->dts_stkstroverflows); 5782 } 5783 5784 while (offs < strsize) 5785 str[offs++] = '\0'; 5786 5787 out: 5788 mstate->dtms_scratch_ptr = old; 5789 } 5790 5791 /* 5792 * If you're looking for the epicenter of DTrace, you just found it. This 5793 * is the function called by the provider to fire a probe -- from which all 5794 * subsequent probe-context DTrace activity emanates. 5795 */ 5796 void 5797 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 5798 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 5799 { 5800 processorid_t cpuid; 5801 dtrace_icookie_t cookie; 5802 dtrace_probe_t *probe; 5803 dtrace_mstate_t mstate; 5804 dtrace_ecb_t *ecb; 5805 dtrace_action_t *act; 5806 intptr_t offs; 5807 size_t size; 5808 int vtime, onintr; 5809 volatile uint16_t *flags; 5810 hrtime_t now; 5811 5812 /* 5813 * Kick out immediately if this CPU is still being born (in which case 5814 * curthread will be set to -1) or the current thread can't allow 5815 * probes in its current context. 5816 */ 5817 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 5818 return; 5819 5820 cookie = dtrace_interrupt_disable(); 5821 probe = dtrace_probes[id - 1]; 5822 cpuid = CPU->cpu_id; 5823 onintr = CPU_ON_INTR(CPU); 5824 5825 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 5826 probe->dtpr_predcache == curthread->t_predcache) { 5827 /* 5828 * We have hit in the predicate cache; we know that 5829 * this predicate would evaluate to be false. 5830 */ 5831 dtrace_interrupt_enable(cookie); 5832 return; 5833 } 5834 5835 if (panic_quiesce) { 5836 /* 5837 * We don't trace anything if we're panicking. 5838 */ 5839 dtrace_interrupt_enable(cookie); 5840 return; 5841 } 5842 5843 now = dtrace_gethrtime(); 5844 vtime = dtrace_vtime_references != 0; 5845 5846 if (vtime && curthread->t_dtrace_start) 5847 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 5848 5849 mstate.dtms_difo = NULL; 5850 mstate.dtms_probe = probe; 5851 mstate.dtms_strtok = NULL; 5852 mstate.dtms_arg[0] = arg0; 5853 mstate.dtms_arg[1] = arg1; 5854 mstate.dtms_arg[2] = arg2; 5855 mstate.dtms_arg[3] = arg3; 5856 mstate.dtms_arg[4] = arg4; 5857 5858 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 5859 5860 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 5861 dtrace_predicate_t *pred = ecb->dte_predicate; 5862 dtrace_state_t *state = ecb->dte_state; 5863 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 5864 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 5865 dtrace_vstate_t *vstate = &state->dts_vstate; 5866 dtrace_provider_t *prov = probe->dtpr_provider; 5867 uint64_t tracememsize = 0; 5868 int committed = 0; 5869 caddr_t tomax; 5870 5871 /* 5872 * A little subtlety with the following (seemingly innocuous) 5873 * declaration of the automatic 'val': by looking at the 5874 * code, you might think that it could be declared in the 5875 * action processing loop, below. (That is, it's only used in 5876 * the action processing loop.) However, it must be declared 5877 * out of that scope because in the case of DIF expression 5878 * arguments to aggregating actions, one iteration of the 5879 * action loop will use the last iteration's value. 5880 */ 5881 #ifdef lint 5882 uint64_t val = 0; 5883 #else 5884 uint64_t val; 5885 #endif 5886 5887 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 5888 mstate.dtms_access = DTRACE_ACCESS_ARGS | DTRACE_ACCESS_PROC; 5889 *flags &= ~CPU_DTRACE_ERROR; 5890 5891 if (prov == dtrace_provider) { 5892 /* 5893 * If dtrace itself is the provider of this probe, 5894 * we're only going to continue processing the ECB if 5895 * arg0 (the dtrace_state_t) is equal to the ECB's 5896 * creating state. (This prevents disjoint consumers 5897 * from seeing one another's metaprobes.) 5898 */ 5899 if (arg0 != (uint64_t)(uintptr_t)state) 5900 continue; 5901 } 5902 5903 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 5904 /* 5905 * We're not currently active. If our provider isn't 5906 * the dtrace pseudo provider, we're not interested. 5907 */ 5908 if (prov != dtrace_provider) 5909 continue; 5910 5911 /* 5912 * Now we must further check if we are in the BEGIN 5913 * probe. If we are, we will only continue processing 5914 * if we're still in WARMUP -- if one BEGIN enabling 5915 * has invoked the exit() action, we don't want to 5916 * evaluate subsequent BEGIN enablings. 5917 */ 5918 if (probe->dtpr_id == dtrace_probeid_begin && 5919 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 5920 ASSERT(state->dts_activity == 5921 DTRACE_ACTIVITY_DRAINING); 5922 continue; 5923 } 5924 } 5925 5926 if (ecb->dte_cond && !dtrace_priv_probe(state, &mstate, ecb)) 5927 continue; 5928 5929 if (now - state->dts_alive > dtrace_deadman_timeout) { 5930 /* 5931 * We seem to be dead. Unless we (a) have kernel 5932 * destructive permissions (b) have expicitly enabled 5933 * destructive actions and (c) destructive actions have 5934 * not been disabled, we're going to transition into 5935 * the KILLED state, from which no further processing 5936 * on this state will be performed. 5937 */ 5938 if (!dtrace_priv_kernel_destructive(state) || 5939 !state->dts_cred.dcr_destructive || 5940 dtrace_destructive_disallow) { 5941 void *activity = &state->dts_activity; 5942 dtrace_activity_t current; 5943 5944 do { 5945 current = state->dts_activity; 5946 } while (dtrace_cas32(activity, current, 5947 DTRACE_ACTIVITY_KILLED) != current); 5948 5949 continue; 5950 } 5951 } 5952 5953 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 5954 ecb->dte_alignment, state, &mstate)) < 0) 5955 continue; 5956 5957 tomax = buf->dtb_tomax; 5958 ASSERT(tomax != NULL); 5959 5960 if (ecb->dte_size != 0) 5961 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 5962 5963 mstate.dtms_epid = ecb->dte_epid; 5964 mstate.dtms_present |= DTRACE_MSTATE_EPID; 5965 5966 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 5967 mstate.dtms_access |= DTRACE_ACCESS_KERNEL; 5968 5969 if (pred != NULL) { 5970 dtrace_difo_t *dp = pred->dtp_difo; 5971 int rval; 5972 5973 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 5974 5975 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 5976 dtrace_cacheid_t cid = probe->dtpr_predcache; 5977 5978 if (cid != DTRACE_CACHEIDNONE && !onintr) { 5979 /* 5980 * Update the predicate cache... 5981 */ 5982 ASSERT(cid == pred->dtp_cacheid); 5983 curthread->t_predcache = cid; 5984 } 5985 5986 continue; 5987 } 5988 } 5989 5990 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 5991 act != NULL; act = act->dta_next) { 5992 size_t valoffs; 5993 dtrace_difo_t *dp; 5994 dtrace_recdesc_t *rec = &act->dta_rec; 5995 5996 size = rec->dtrd_size; 5997 valoffs = offs + rec->dtrd_offset; 5998 5999 if (DTRACEACT_ISAGG(act->dta_kind)) { 6000 uint64_t v = 0xbad; 6001 dtrace_aggregation_t *agg; 6002 6003 agg = (dtrace_aggregation_t *)act; 6004 6005 if ((dp = act->dta_difo) != NULL) 6006 v = dtrace_dif_emulate(dp, 6007 &mstate, vstate, state); 6008 6009 if (*flags & CPU_DTRACE_ERROR) 6010 continue; 6011 6012 /* 6013 * Note that we always pass the expression 6014 * value from the previous iteration of the 6015 * action loop. This value will only be used 6016 * if there is an expression argument to the 6017 * aggregating action, denoted by the 6018 * dtag_hasarg field. 6019 */ 6020 dtrace_aggregate(agg, buf, 6021 offs, aggbuf, v, val); 6022 continue; 6023 } 6024 6025 switch (act->dta_kind) { 6026 case DTRACEACT_STOP: 6027 if (dtrace_priv_proc_destructive(state, 6028 &mstate)) 6029 dtrace_action_stop(); 6030 continue; 6031 6032 case DTRACEACT_BREAKPOINT: 6033 if (dtrace_priv_kernel_destructive(state)) 6034 dtrace_action_breakpoint(ecb); 6035 continue; 6036 6037 case DTRACEACT_PANIC: 6038 if (dtrace_priv_kernel_destructive(state)) 6039 dtrace_action_panic(ecb); 6040 continue; 6041 6042 case DTRACEACT_STACK: 6043 if (!dtrace_priv_kernel(state)) 6044 continue; 6045 6046 dtrace_getpcstack((pc_t *)(tomax + valoffs), 6047 size / sizeof (pc_t), probe->dtpr_aframes, 6048 DTRACE_ANCHORED(probe) ? NULL : 6049 (uint32_t *)arg0); 6050 6051 continue; 6052 6053 case DTRACEACT_JSTACK: 6054 case DTRACEACT_USTACK: 6055 if (!dtrace_priv_proc(state, &mstate)) 6056 continue; 6057 6058 /* 6059 * See comment in DIF_VAR_PID. 6060 */ 6061 if (DTRACE_ANCHORED(mstate.dtms_probe) && 6062 CPU_ON_INTR(CPU)) { 6063 int depth = DTRACE_USTACK_NFRAMES( 6064 rec->dtrd_arg) + 1; 6065 6066 dtrace_bzero((void *)(tomax + valoffs), 6067 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 6068 + depth * sizeof (uint64_t)); 6069 6070 continue; 6071 } 6072 6073 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 6074 curproc->p_dtrace_helpers != NULL) { 6075 /* 6076 * This is the slow path -- we have 6077 * allocated string space, and we're 6078 * getting the stack of a process that 6079 * has helpers. Call into a separate 6080 * routine to perform this processing. 6081 */ 6082 dtrace_action_ustack(&mstate, state, 6083 (uint64_t *)(tomax + valoffs), 6084 rec->dtrd_arg); 6085 continue; 6086 } 6087 6088 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6089 dtrace_getupcstack((uint64_t *) 6090 (tomax + valoffs), 6091 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 6092 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6093 continue; 6094 6095 default: 6096 break; 6097 } 6098 6099 dp = act->dta_difo; 6100 ASSERT(dp != NULL); 6101 6102 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 6103 6104 if (*flags & CPU_DTRACE_ERROR) 6105 continue; 6106 6107 switch (act->dta_kind) { 6108 case DTRACEACT_SPECULATE: 6109 ASSERT(buf == &state->dts_buffer[cpuid]); 6110 buf = dtrace_speculation_buffer(state, 6111 cpuid, val); 6112 6113 if (buf == NULL) { 6114 *flags |= CPU_DTRACE_DROP; 6115 continue; 6116 } 6117 6118 offs = dtrace_buffer_reserve(buf, 6119 ecb->dte_needed, ecb->dte_alignment, 6120 state, NULL); 6121 6122 if (offs < 0) { 6123 *flags |= CPU_DTRACE_DROP; 6124 continue; 6125 } 6126 6127 tomax = buf->dtb_tomax; 6128 ASSERT(tomax != NULL); 6129 6130 if (ecb->dte_size != 0) 6131 DTRACE_STORE(uint32_t, tomax, offs, 6132 ecb->dte_epid); 6133 continue; 6134 6135 case DTRACEACT_CHILL: 6136 if (dtrace_priv_kernel_destructive(state)) 6137 dtrace_action_chill(&mstate, val); 6138 continue; 6139 6140 case DTRACEACT_RAISE: 6141 if (dtrace_priv_proc_destructive(state, 6142 &mstate)) 6143 dtrace_action_raise(val); 6144 continue; 6145 6146 case DTRACEACT_COMMIT: 6147 ASSERT(!committed); 6148 6149 /* 6150 * We need to commit our buffer state. 6151 */ 6152 if (ecb->dte_size) 6153 buf->dtb_offset = offs + ecb->dte_size; 6154 buf = &state->dts_buffer[cpuid]; 6155 dtrace_speculation_commit(state, cpuid, val); 6156 committed = 1; 6157 continue; 6158 6159 case DTRACEACT_DISCARD: 6160 dtrace_speculation_discard(state, cpuid, val); 6161 continue; 6162 6163 case DTRACEACT_DIFEXPR: 6164 case DTRACEACT_LIBACT: 6165 case DTRACEACT_PRINTF: 6166 case DTRACEACT_PRINTA: 6167 case DTRACEACT_SYSTEM: 6168 case DTRACEACT_FREOPEN: 6169 case DTRACEACT_TRACEMEM: 6170 break; 6171 6172 case DTRACEACT_TRACEMEM_DYNSIZE: 6173 tracememsize = val; 6174 break; 6175 6176 case DTRACEACT_SYM: 6177 case DTRACEACT_MOD: 6178 if (!dtrace_priv_kernel(state)) 6179 continue; 6180 break; 6181 6182 case DTRACEACT_USYM: 6183 case DTRACEACT_UMOD: 6184 case DTRACEACT_UADDR: { 6185 struct pid *pid = curthread->t_procp->p_pidp; 6186 6187 if (!dtrace_priv_proc(state, &mstate)) 6188 continue; 6189 6190 DTRACE_STORE(uint64_t, tomax, 6191 valoffs, (uint64_t)pid->pid_id); 6192 DTRACE_STORE(uint64_t, tomax, 6193 valoffs + sizeof (uint64_t), val); 6194 6195 continue; 6196 } 6197 6198 case DTRACEACT_EXIT: { 6199 /* 6200 * For the exit action, we are going to attempt 6201 * to atomically set our activity to be 6202 * draining. If this fails (either because 6203 * another CPU has beat us to the exit action, 6204 * or because our current activity is something 6205 * other than ACTIVE or WARMUP), we will 6206 * continue. This assures that the exit action 6207 * can be successfully recorded at most once 6208 * when we're in the ACTIVE state. If we're 6209 * encountering the exit() action while in 6210 * COOLDOWN, however, we want to honor the new 6211 * status code. (We know that we're the only 6212 * thread in COOLDOWN, so there is no race.) 6213 */ 6214 void *activity = &state->dts_activity; 6215 dtrace_activity_t current = state->dts_activity; 6216 6217 if (current == DTRACE_ACTIVITY_COOLDOWN) 6218 break; 6219 6220 if (current != DTRACE_ACTIVITY_WARMUP) 6221 current = DTRACE_ACTIVITY_ACTIVE; 6222 6223 if (dtrace_cas32(activity, current, 6224 DTRACE_ACTIVITY_DRAINING) != current) { 6225 *flags |= CPU_DTRACE_DROP; 6226 continue; 6227 } 6228 6229 break; 6230 } 6231 6232 default: 6233 ASSERT(0); 6234 } 6235 6236 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 6237 uintptr_t end = valoffs + size; 6238 6239 if (tracememsize != 0 && 6240 valoffs + tracememsize < end) { 6241 end = valoffs + tracememsize; 6242 tracememsize = 0; 6243 } 6244 6245 if (!dtrace_vcanload((void *)(uintptr_t)val, 6246 &dp->dtdo_rtype, &mstate, vstate)) 6247 continue; 6248 6249 /* 6250 * If this is a string, we're going to only 6251 * load until we find the zero byte -- after 6252 * which we'll store zero bytes. 6253 */ 6254 if (dp->dtdo_rtype.dtdt_kind == 6255 DIF_TYPE_STRING) { 6256 char c = '\0' + 1; 6257 int intuple = act->dta_intuple; 6258 size_t s; 6259 6260 for (s = 0; s < size; s++) { 6261 if (c != '\0') 6262 c = dtrace_load8(val++); 6263 6264 DTRACE_STORE(uint8_t, tomax, 6265 valoffs++, c); 6266 6267 if (c == '\0' && intuple) 6268 break; 6269 } 6270 6271 continue; 6272 } 6273 6274 while (valoffs < end) { 6275 DTRACE_STORE(uint8_t, tomax, valoffs++, 6276 dtrace_load8(val++)); 6277 } 6278 6279 continue; 6280 } 6281 6282 switch (size) { 6283 case 0: 6284 break; 6285 6286 case sizeof (uint8_t): 6287 DTRACE_STORE(uint8_t, tomax, valoffs, val); 6288 break; 6289 case sizeof (uint16_t): 6290 DTRACE_STORE(uint16_t, tomax, valoffs, val); 6291 break; 6292 case sizeof (uint32_t): 6293 DTRACE_STORE(uint32_t, tomax, valoffs, val); 6294 break; 6295 case sizeof (uint64_t): 6296 DTRACE_STORE(uint64_t, tomax, valoffs, val); 6297 break; 6298 default: 6299 /* 6300 * Any other size should have been returned by 6301 * reference, not by value. 6302 */ 6303 ASSERT(0); 6304 break; 6305 } 6306 } 6307 6308 if (*flags & CPU_DTRACE_DROP) 6309 continue; 6310 6311 if (*flags & CPU_DTRACE_FAULT) { 6312 int ndx; 6313 dtrace_action_t *err; 6314 6315 buf->dtb_errors++; 6316 6317 if (probe->dtpr_id == dtrace_probeid_error) { 6318 /* 6319 * There's nothing we can do -- we had an 6320 * error on the error probe. We bump an 6321 * error counter to at least indicate that 6322 * this condition happened. 6323 */ 6324 dtrace_error(&state->dts_dblerrors); 6325 continue; 6326 } 6327 6328 if (vtime) { 6329 /* 6330 * Before recursing on dtrace_probe(), we 6331 * need to explicitly clear out our start 6332 * time to prevent it from being accumulated 6333 * into t_dtrace_vtime. 6334 */ 6335 curthread->t_dtrace_start = 0; 6336 } 6337 6338 /* 6339 * Iterate over the actions to figure out which action 6340 * we were processing when we experienced the error. 6341 * Note that act points _past_ the faulting action; if 6342 * act is ecb->dte_action, the fault was in the 6343 * predicate, if it's ecb->dte_action->dta_next it's 6344 * in action #1, and so on. 6345 */ 6346 for (err = ecb->dte_action, ndx = 0; 6347 err != act; err = err->dta_next, ndx++) 6348 continue; 6349 6350 dtrace_probe_error(state, ecb->dte_epid, ndx, 6351 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 6352 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 6353 cpu_core[cpuid].cpuc_dtrace_illval); 6354 6355 continue; 6356 } 6357 6358 if (!committed) 6359 buf->dtb_offset = offs + ecb->dte_size; 6360 } 6361 6362 if (vtime) 6363 curthread->t_dtrace_start = dtrace_gethrtime(); 6364 6365 dtrace_interrupt_enable(cookie); 6366 } 6367 6368 /* 6369 * DTrace Probe Hashing Functions 6370 * 6371 * The functions in this section (and indeed, the functions in remaining 6372 * sections) are not _called_ from probe context. (Any exceptions to this are 6373 * marked with a "Note:".) Rather, they are called from elsewhere in the 6374 * DTrace framework to look-up probes in, add probes to and remove probes from 6375 * the DTrace probe hashes. (Each probe is hashed by each element of the 6376 * probe tuple -- allowing for fast lookups, regardless of what was 6377 * specified.) 6378 */ 6379 static uint_t 6380 dtrace_hash_str(char *p) 6381 { 6382 unsigned int g; 6383 uint_t hval = 0; 6384 6385 while (*p) { 6386 hval = (hval << 4) + *p++; 6387 if ((g = (hval & 0xf0000000)) != 0) 6388 hval ^= g >> 24; 6389 hval &= ~g; 6390 } 6391 return (hval); 6392 } 6393 6394 static dtrace_hash_t * 6395 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 6396 { 6397 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 6398 6399 hash->dth_stroffs = stroffs; 6400 hash->dth_nextoffs = nextoffs; 6401 hash->dth_prevoffs = prevoffs; 6402 6403 hash->dth_size = 1; 6404 hash->dth_mask = hash->dth_size - 1; 6405 6406 hash->dth_tab = kmem_zalloc(hash->dth_size * 6407 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 6408 6409 return (hash); 6410 } 6411 6412 static void 6413 dtrace_hash_destroy(dtrace_hash_t *hash) 6414 { 6415 #ifdef DEBUG 6416 int i; 6417 6418 for (i = 0; i < hash->dth_size; i++) 6419 ASSERT(hash->dth_tab[i] == NULL); 6420 #endif 6421 6422 kmem_free(hash->dth_tab, 6423 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 6424 kmem_free(hash, sizeof (dtrace_hash_t)); 6425 } 6426 6427 static void 6428 dtrace_hash_resize(dtrace_hash_t *hash) 6429 { 6430 int size = hash->dth_size, i, ndx; 6431 int new_size = hash->dth_size << 1; 6432 int new_mask = new_size - 1; 6433 dtrace_hashbucket_t **new_tab, *bucket, *next; 6434 6435 ASSERT((new_size & new_mask) == 0); 6436 6437 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 6438 6439 for (i = 0; i < size; i++) { 6440 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 6441 dtrace_probe_t *probe = bucket->dthb_chain; 6442 6443 ASSERT(probe != NULL); 6444 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 6445 6446 next = bucket->dthb_next; 6447 bucket->dthb_next = new_tab[ndx]; 6448 new_tab[ndx] = bucket; 6449 } 6450 } 6451 6452 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 6453 hash->dth_tab = new_tab; 6454 hash->dth_size = new_size; 6455 hash->dth_mask = new_mask; 6456 } 6457 6458 static void 6459 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 6460 { 6461 int hashval = DTRACE_HASHSTR(hash, new); 6462 int ndx = hashval & hash->dth_mask; 6463 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6464 dtrace_probe_t **nextp, **prevp; 6465 6466 for (; bucket != NULL; bucket = bucket->dthb_next) { 6467 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 6468 goto add; 6469 } 6470 6471 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 6472 dtrace_hash_resize(hash); 6473 dtrace_hash_add(hash, new); 6474 return; 6475 } 6476 6477 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 6478 bucket->dthb_next = hash->dth_tab[ndx]; 6479 hash->dth_tab[ndx] = bucket; 6480 hash->dth_nbuckets++; 6481 6482 add: 6483 nextp = DTRACE_HASHNEXT(hash, new); 6484 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 6485 *nextp = bucket->dthb_chain; 6486 6487 if (bucket->dthb_chain != NULL) { 6488 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 6489 ASSERT(*prevp == NULL); 6490 *prevp = new; 6491 } 6492 6493 bucket->dthb_chain = new; 6494 bucket->dthb_len++; 6495 } 6496 6497 static dtrace_probe_t * 6498 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 6499 { 6500 int hashval = DTRACE_HASHSTR(hash, template); 6501 int ndx = hashval & hash->dth_mask; 6502 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6503 6504 for (; bucket != NULL; bucket = bucket->dthb_next) { 6505 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6506 return (bucket->dthb_chain); 6507 } 6508 6509 return (NULL); 6510 } 6511 6512 static int 6513 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 6514 { 6515 int hashval = DTRACE_HASHSTR(hash, template); 6516 int ndx = hashval & hash->dth_mask; 6517 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6518 6519 for (; bucket != NULL; bucket = bucket->dthb_next) { 6520 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6521 return (bucket->dthb_len); 6522 } 6523 6524 return (NULL); 6525 } 6526 6527 static void 6528 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 6529 { 6530 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 6531 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6532 6533 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 6534 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 6535 6536 /* 6537 * Find the bucket that we're removing this probe from. 6538 */ 6539 for (; bucket != NULL; bucket = bucket->dthb_next) { 6540 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 6541 break; 6542 } 6543 6544 ASSERT(bucket != NULL); 6545 6546 if (*prevp == NULL) { 6547 if (*nextp == NULL) { 6548 /* 6549 * The removed probe was the only probe on this 6550 * bucket; we need to remove the bucket. 6551 */ 6552 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 6553 6554 ASSERT(bucket->dthb_chain == probe); 6555 ASSERT(b != NULL); 6556 6557 if (b == bucket) { 6558 hash->dth_tab[ndx] = bucket->dthb_next; 6559 } else { 6560 while (b->dthb_next != bucket) 6561 b = b->dthb_next; 6562 b->dthb_next = bucket->dthb_next; 6563 } 6564 6565 ASSERT(hash->dth_nbuckets > 0); 6566 hash->dth_nbuckets--; 6567 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 6568 return; 6569 } 6570 6571 bucket->dthb_chain = *nextp; 6572 } else { 6573 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 6574 } 6575 6576 if (*nextp != NULL) 6577 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 6578 } 6579 6580 /* 6581 * DTrace Utility Functions 6582 * 6583 * These are random utility functions that are _not_ called from probe context. 6584 */ 6585 static int 6586 dtrace_badattr(const dtrace_attribute_t *a) 6587 { 6588 return (a->dtat_name > DTRACE_STABILITY_MAX || 6589 a->dtat_data > DTRACE_STABILITY_MAX || 6590 a->dtat_class > DTRACE_CLASS_MAX); 6591 } 6592 6593 /* 6594 * Return a duplicate copy of a string. If the specified string is NULL, 6595 * this function returns a zero-length string. 6596 */ 6597 static char * 6598 dtrace_strdup(const char *str) 6599 { 6600 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 6601 6602 if (str != NULL) 6603 (void) strcpy(new, str); 6604 6605 return (new); 6606 } 6607 6608 #define DTRACE_ISALPHA(c) \ 6609 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 6610 6611 static int 6612 dtrace_badname(const char *s) 6613 { 6614 char c; 6615 6616 if (s == NULL || (c = *s++) == '\0') 6617 return (0); 6618 6619 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 6620 return (1); 6621 6622 while ((c = *s++) != '\0') { 6623 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 6624 c != '-' && c != '_' && c != '.' && c != '`') 6625 return (1); 6626 } 6627 6628 return (0); 6629 } 6630 6631 static void 6632 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 6633 { 6634 uint32_t priv; 6635 6636 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 6637 /* 6638 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 6639 */ 6640 priv = DTRACE_PRIV_ALL; 6641 } else { 6642 *uidp = crgetuid(cr); 6643 *zoneidp = crgetzoneid(cr); 6644 6645 priv = 0; 6646 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 6647 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 6648 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 6649 priv |= DTRACE_PRIV_USER; 6650 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 6651 priv |= DTRACE_PRIV_PROC; 6652 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 6653 priv |= DTRACE_PRIV_OWNER; 6654 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 6655 priv |= DTRACE_PRIV_ZONEOWNER; 6656 } 6657 6658 *privp = priv; 6659 } 6660 6661 #ifdef DTRACE_ERRDEBUG 6662 static void 6663 dtrace_errdebug(const char *str) 6664 { 6665 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; 6666 int occupied = 0; 6667 6668 mutex_enter(&dtrace_errlock); 6669 dtrace_errlast = str; 6670 dtrace_errthread = curthread; 6671 6672 while (occupied++ < DTRACE_ERRHASHSZ) { 6673 if (dtrace_errhash[hval].dter_msg == str) { 6674 dtrace_errhash[hval].dter_count++; 6675 goto out; 6676 } 6677 6678 if (dtrace_errhash[hval].dter_msg != NULL) { 6679 hval = (hval + 1) % DTRACE_ERRHASHSZ; 6680 continue; 6681 } 6682 6683 dtrace_errhash[hval].dter_msg = str; 6684 dtrace_errhash[hval].dter_count = 1; 6685 goto out; 6686 } 6687 6688 panic("dtrace: undersized error hash"); 6689 out: 6690 mutex_exit(&dtrace_errlock); 6691 } 6692 #endif 6693 6694 /* 6695 * DTrace Matching Functions 6696 * 6697 * These functions are used to match groups of probes, given some elements of 6698 * a probe tuple, or some globbed expressions for elements of a probe tuple. 6699 */ 6700 static int 6701 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 6702 zoneid_t zoneid) 6703 { 6704 if (priv != DTRACE_PRIV_ALL) { 6705 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 6706 uint32_t match = priv & ppriv; 6707 6708 /* 6709 * No PRIV_DTRACE_* privileges... 6710 */ 6711 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 6712 DTRACE_PRIV_KERNEL)) == 0) 6713 return (0); 6714 6715 /* 6716 * No matching bits, but there were bits to match... 6717 */ 6718 if (match == 0 && ppriv != 0) 6719 return (0); 6720 6721 /* 6722 * Need to have permissions to the process, but don't... 6723 */ 6724 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 6725 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 6726 return (0); 6727 } 6728 6729 /* 6730 * Need to be in the same zone unless we possess the 6731 * privilege to examine all zones. 6732 */ 6733 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 6734 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 6735 return (0); 6736 } 6737 } 6738 6739 return (1); 6740 } 6741 6742 /* 6743 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 6744 * consists of input pattern strings and an ops-vector to evaluate them. 6745 * This function returns >0 for match, 0 for no match, and <0 for error. 6746 */ 6747 static int 6748 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 6749 uint32_t priv, uid_t uid, zoneid_t zoneid) 6750 { 6751 dtrace_provider_t *pvp = prp->dtpr_provider; 6752 int rv; 6753 6754 if (pvp->dtpv_defunct) 6755 return (0); 6756 6757 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 6758 return (rv); 6759 6760 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 6761 return (rv); 6762 6763 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 6764 return (rv); 6765 6766 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 6767 return (rv); 6768 6769 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 6770 return (0); 6771 6772 return (rv); 6773 } 6774 6775 /* 6776 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 6777 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 6778 * libc's version, the kernel version only applies to 8-bit ASCII strings. 6779 * In addition, all of the recursion cases except for '*' matching have been 6780 * unwound. For '*', we still implement recursive evaluation, but a depth 6781 * counter is maintained and matching is aborted if we recurse too deep. 6782 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 6783 */ 6784 static int 6785 dtrace_match_glob(const char *s, const char *p, int depth) 6786 { 6787 const char *olds; 6788 char s1, c; 6789 int gs; 6790 6791 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 6792 return (-1); 6793 6794 if (s == NULL) 6795 s = ""; /* treat NULL as empty string */ 6796 6797 top: 6798 olds = s; 6799 s1 = *s++; 6800 6801 if (p == NULL) 6802 return (0); 6803 6804 if ((c = *p++) == '\0') 6805 return (s1 == '\0'); 6806 6807 switch (c) { 6808 case '[': { 6809 int ok = 0, notflag = 0; 6810 char lc = '\0'; 6811 6812 if (s1 == '\0') 6813 return (0); 6814 6815 if (*p == '!') { 6816 notflag = 1; 6817 p++; 6818 } 6819 6820 if ((c = *p++) == '\0') 6821 return (0); 6822 6823 do { 6824 if (c == '-' && lc != '\0' && *p != ']') { 6825 if ((c = *p++) == '\0') 6826 return (0); 6827 if (c == '\\' && (c = *p++) == '\0') 6828 return (0); 6829 6830 if (notflag) { 6831 if (s1 < lc || s1 > c) 6832 ok++; 6833 else 6834 return (0); 6835 } else if (lc <= s1 && s1 <= c) 6836 ok++; 6837 6838 } else if (c == '\\' && (c = *p++) == '\0') 6839 return (0); 6840 6841 lc = c; /* save left-hand 'c' for next iteration */ 6842 6843 if (notflag) { 6844 if (s1 != c) 6845 ok++; 6846 else 6847 return (0); 6848 } else if (s1 == c) 6849 ok++; 6850 6851 if ((c = *p++) == '\0') 6852 return (0); 6853 6854 } while (c != ']'); 6855 6856 if (ok) 6857 goto top; 6858 6859 return (0); 6860 } 6861 6862 case '\\': 6863 if ((c = *p++) == '\0') 6864 return (0); 6865 /*FALLTHRU*/ 6866 6867 default: 6868 if (c != s1) 6869 return (0); 6870 /*FALLTHRU*/ 6871 6872 case '?': 6873 if (s1 != '\0') 6874 goto top; 6875 return (0); 6876 6877 case '*': 6878 while (*p == '*') 6879 p++; /* consecutive *'s are identical to a single one */ 6880 6881 if (*p == '\0') 6882 return (1); 6883 6884 for (s = olds; *s != '\0'; s++) { 6885 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 6886 return (gs); 6887 } 6888 6889 return (0); 6890 } 6891 } 6892 6893 /*ARGSUSED*/ 6894 static int 6895 dtrace_match_string(const char *s, const char *p, int depth) 6896 { 6897 return (s != NULL && strcmp(s, p) == 0); 6898 } 6899 6900 /*ARGSUSED*/ 6901 static int 6902 dtrace_match_nul(const char *s, const char *p, int depth) 6903 { 6904 return (1); /* always match the empty pattern */ 6905 } 6906 6907 /*ARGSUSED*/ 6908 static int 6909 dtrace_match_nonzero(const char *s, const char *p, int depth) 6910 { 6911 return (s != NULL && s[0] != '\0'); 6912 } 6913 6914 static int 6915 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 6916 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 6917 { 6918 dtrace_probe_t template, *probe; 6919 dtrace_hash_t *hash = NULL; 6920 int len, rc, best = INT_MAX, nmatched = 0; 6921 dtrace_id_t i; 6922 6923 ASSERT(MUTEX_HELD(&dtrace_lock)); 6924 6925 /* 6926 * If the probe ID is specified in the key, just lookup by ID and 6927 * invoke the match callback once if a matching probe is found. 6928 */ 6929 if (pkp->dtpk_id != DTRACE_IDNONE) { 6930 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 6931 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 6932 if ((*matched)(probe, arg) == DTRACE_MATCH_FAIL) 6933 return (DTRACE_MATCH_FAIL); 6934 nmatched++; 6935 } 6936 return (nmatched); 6937 } 6938 6939 template.dtpr_mod = (char *)pkp->dtpk_mod; 6940 template.dtpr_func = (char *)pkp->dtpk_func; 6941 template.dtpr_name = (char *)pkp->dtpk_name; 6942 6943 /* 6944 * We want to find the most distinct of the module name, function 6945 * name, and name. So for each one that is not a glob pattern or 6946 * empty string, we perform a lookup in the corresponding hash and 6947 * use the hash table with the fewest collisions to do our search. 6948 */ 6949 if (pkp->dtpk_mmatch == &dtrace_match_string && 6950 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 6951 best = len; 6952 hash = dtrace_bymod; 6953 } 6954 6955 if (pkp->dtpk_fmatch == &dtrace_match_string && 6956 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 6957 best = len; 6958 hash = dtrace_byfunc; 6959 } 6960 6961 if (pkp->dtpk_nmatch == &dtrace_match_string && 6962 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 6963 best = len; 6964 hash = dtrace_byname; 6965 } 6966 6967 /* 6968 * If we did not select a hash table, iterate over every probe and 6969 * invoke our callback for each one that matches our input probe key. 6970 */ 6971 if (hash == NULL) { 6972 for (i = 0; i < dtrace_nprobes; i++) { 6973 if ((probe = dtrace_probes[i]) == NULL || 6974 dtrace_match_probe(probe, pkp, priv, uid, 6975 zoneid) <= 0) 6976 continue; 6977 6978 nmatched++; 6979 6980 if ((rc = (*matched)(probe, arg)) != 6981 DTRACE_MATCH_NEXT) { 6982 if (rc == DTRACE_MATCH_FAIL) 6983 return (DTRACE_MATCH_FAIL); 6984 break; 6985 } 6986 } 6987 6988 return (nmatched); 6989 } 6990 6991 /* 6992 * If we selected a hash table, iterate over each probe of the same key 6993 * name and invoke the callback for every probe that matches the other 6994 * attributes of our input probe key. 6995 */ 6996 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 6997 probe = *(DTRACE_HASHNEXT(hash, probe))) { 6998 6999 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 7000 continue; 7001 7002 nmatched++; 7003 7004 if ((rc = (*matched)(probe, arg)) != DTRACE_MATCH_NEXT) { 7005 if (rc == DTRACE_MATCH_FAIL) 7006 return (DTRACE_MATCH_FAIL); 7007 break; 7008 } 7009 } 7010 7011 return (nmatched); 7012 } 7013 7014 /* 7015 * Return the function pointer dtrace_probecmp() should use to compare the 7016 * specified pattern with a string. For NULL or empty patterns, we select 7017 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 7018 * For non-empty non-glob strings, we use dtrace_match_string(). 7019 */ 7020 static dtrace_probekey_f * 7021 dtrace_probekey_func(const char *p) 7022 { 7023 char c; 7024 7025 if (p == NULL || *p == '\0') 7026 return (&dtrace_match_nul); 7027 7028 while ((c = *p++) != '\0') { 7029 if (c == '[' || c == '?' || c == '*' || c == '\\') 7030 return (&dtrace_match_glob); 7031 } 7032 7033 return (&dtrace_match_string); 7034 } 7035 7036 /* 7037 * Build a probe comparison key for use with dtrace_match_probe() from the 7038 * given probe description. By convention, a null key only matches anchored 7039 * probes: if each field is the empty string, reset dtpk_fmatch to 7040 * dtrace_match_nonzero(). 7041 */ 7042 static void 7043 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 7044 { 7045 pkp->dtpk_prov = pdp->dtpd_provider; 7046 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 7047 7048 pkp->dtpk_mod = pdp->dtpd_mod; 7049 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 7050 7051 pkp->dtpk_func = pdp->dtpd_func; 7052 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 7053 7054 pkp->dtpk_name = pdp->dtpd_name; 7055 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 7056 7057 pkp->dtpk_id = pdp->dtpd_id; 7058 7059 if (pkp->dtpk_id == DTRACE_IDNONE && 7060 pkp->dtpk_pmatch == &dtrace_match_nul && 7061 pkp->dtpk_mmatch == &dtrace_match_nul && 7062 pkp->dtpk_fmatch == &dtrace_match_nul && 7063 pkp->dtpk_nmatch == &dtrace_match_nul) 7064 pkp->dtpk_fmatch = &dtrace_match_nonzero; 7065 } 7066 7067 /* 7068 * DTrace Provider-to-Framework API Functions 7069 * 7070 * These functions implement much of the Provider-to-Framework API, as 7071 * described in <sys/dtrace.h>. The parts of the API not in this section are 7072 * the functions in the API for probe management (found below), and 7073 * dtrace_probe() itself (found above). 7074 */ 7075 7076 /* 7077 * Register the calling provider with the DTrace framework. This should 7078 * generally be called by DTrace providers in their attach(9E) entry point. 7079 */ 7080 int 7081 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 7082 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 7083 { 7084 dtrace_provider_t *provider; 7085 7086 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 7087 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7088 "arguments", name ? name : "<NULL>"); 7089 return (EINVAL); 7090 } 7091 7092 if (name[0] == '\0' || dtrace_badname(name)) { 7093 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7094 "provider name", name); 7095 return (EINVAL); 7096 } 7097 7098 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 7099 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 7100 pops->dtps_destroy == NULL || 7101 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 7102 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7103 "provider ops", name); 7104 return (EINVAL); 7105 } 7106 7107 if (dtrace_badattr(&pap->dtpa_provider) || 7108 dtrace_badattr(&pap->dtpa_mod) || 7109 dtrace_badattr(&pap->dtpa_func) || 7110 dtrace_badattr(&pap->dtpa_name) || 7111 dtrace_badattr(&pap->dtpa_args)) { 7112 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7113 "provider attributes", name); 7114 return (EINVAL); 7115 } 7116 7117 if (priv & ~DTRACE_PRIV_ALL) { 7118 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7119 "privilege attributes", name); 7120 return (EINVAL); 7121 } 7122 7123 if ((priv & DTRACE_PRIV_KERNEL) && 7124 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 7125 pops->dtps_mode == NULL) { 7126 cmn_err(CE_WARN, "failed to register provider '%s': need " 7127 "dtps_mode() op for given privilege attributes", name); 7128 return (EINVAL); 7129 } 7130 7131 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 7132 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7133 (void) strcpy(provider->dtpv_name, name); 7134 7135 provider->dtpv_attr = *pap; 7136 provider->dtpv_priv.dtpp_flags = priv; 7137 if (cr != NULL) { 7138 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 7139 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 7140 } 7141 provider->dtpv_pops = *pops; 7142 7143 if (pops->dtps_provide == NULL) { 7144 ASSERT(pops->dtps_provide_module != NULL); 7145 provider->dtpv_pops.dtps_provide = 7146 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 7147 } 7148 7149 if (pops->dtps_provide_module == NULL) { 7150 ASSERT(pops->dtps_provide != NULL); 7151 provider->dtpv_pops.dtps_provide_module = 7152 (void (*)(void *, struct modctl *))dtrace_nullop; 7153 } 7154 7155 if (pops->dtps_suspend == NULL) { 7156 ASSERT(pops->dtps_resume == NULL); 7157 provider->dtpv_pops.dtps_suspend = 7158 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7159 provider->dtpv_pops.dtps_resume = 7160 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7161 } 7162 7163 provider->dtpv_arg = arg; 7164 *idp = (dtrace_provider_id_t)provider; 7165 7166 if (pops == &dtrace_provider_ops) { 7167 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7168 ASSERT(MUTEX_HELD(&dtrace_lock)); 7169 ASSERT(dtrace_anon.dta_enabling == NULL); 7170 7171 /* 7172 * We make sure that the DTrace provider is at the head of 7173 * the provider chain. 7174 */ 7175 provider->dtpv_next = dtrace_provider; 7176 dtrace_provider = provider; 7177 return (0); 7178 } 7179 7180 mutex_enter(&dtrace_provider_lock); 7181 mutex_enter(&dtrace_lock); 7182 7183 /* 7184 * If there is at least one provider registered, we'll add this 7185 * provider after the first provider. 7186 */ 7187 if (dtrace_provider != NULL) { 7188 provider->dtpv_next = dtrace_provider->dtpv_next; 7189 dtrace_provider->dtpv_next = provider; 7190 } else { 7191 dtrace_provider = provider; 7192 } 7193 7194 if (dtrace_retained != NULL) { 7195 dtrace_enabling_provide(provider); 7196 7197 /* 7198 * Now we need to call dtrace_enabling_matchall() -- which 7199 * will acquire cpu_lock and dtrace_lock. We therefore need 7200 * to drop all of our locks before calling into it... 7201 */ 7202 mutex_exit(&dtrace_lock); 7203 mutex_exit(&dtrace_provider_lock); 7204 dtrace_enabling_matchall(); 7205 7206 return (0); 7207 } 7208 7209 mutex_exit(&dtrace_lock); 7210 mutex_exit(&dtrace_provider_lock); 7211 7212 return (0); 7213 } 7214 7215 /* 7216 * Unregister the specified provider from the DTrace framework. This should 7217 * generally be called by DTrace providers in their detach(9E) entry point. 7218 */ 7219 int 7220 dtrace_unregister(dtrace_provider_id_t id) 7221 { 7222 dtrace_provider_t *old = (dtrace_provider_t *)id; 7223 dtrace_provider_t *prev = NULL; 7224 int i, self = 0, noreap = 0; 7225 dtrace_probe_t *probe, *first = NULL; 7226 7227 if (old->dtpv_pops.dtps_enable == 7228 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop) { 7229 /* 7230 * If DTrace itself is the provider, we're called with locks 7231 * already held. 7232 */ 7233 ASSERT(old == dtrace_provider); 7234 ASSERT(dtrace_devi != NULL); 7235 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7236 ASSERT(MUTEX_HELD(&dtrace_lock)); 7237 self = 1; 7238 7239 if (dtrace_provider->dtpv_next != NULL) { 7240 /* 7241 * There's another provider here; return failure. 7242 */ 7243 return (EBUSY); 7244 } 7245 } else { 7246 mutex_enter(&dtrace_provider_lock); 7247 mutex_enter(&mod_lock); 7248 mutex_enter(&dtrace_lock); 7249 } 7250 7251 /* 7252 * If anyone has /dev/dtrace open, or if there are anonymous enabled 7253 * probes, we refuse to let providers slither away, unless this 7254 * provider has already been explicitly invalidated. 7255 */ 7256 if (!old->dtpv_defunct && 7257 (dtrace_opens || (dtrace_anon.dta_state != NULL && 7258 dtrace_anon.dta_state->dts_necbs > 0))) { 7259 if (!self) { 7260 mutex_exit(&dtrace_lock); 7261 mutex_exit(&mod_lock); 7262 mutex_exit(&dtrace_provider_lock); 7263 } 7264 return (EBUSY); 7265 } 7266 7267 /* 7268 * Attempt to destroy the probes associated with this provider. 7269 */ 7270 for (i = 0; i < dtrace_nprobes; i++) { 7271 if ((probe = dtrace_probes[i]) == NULL) 7272 continue; 7273 7274 if (probe->dtpr_provider != old) 7275 continue; 7276 7277 if (probe->dtpr_ecb == NULL) 7278 continue; 7279 7280 /* 7281 * If we are trying to unregister a defunct provider, and the 7282 * provider was made defunct within the interval dictated by 7283 * dtrace_unregister_defunct_reap, we'll (asynchronously) 7284 * attempt to reap our enablings. To denote that the provider 7285 * should reattempt to unregister itself at some point in the 7286 * future, we will return a differentiable error code (EAGAIN 7287 * instead of EBUSY) in this case. 7288 */ 7289 if (dtrace_gethrtime() - old->dtpv_defunct > 7290 dtrace_unregister_defunct_reap) 7291 noreap = 1; 7292 7293 if (!self) { 7294 mutex_exit(&dtrace_lock); 7295 mutex_exit(&mod_lock); 7296 mutex_exit(&dtrace_provider_lock); 7297 } 7298 7299 if (noreap) 7300 return (EBUSY); 7301 7302 (void) taskq_dispatch(dtrace_taskq, 7303 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 7304 7305 return (EAGAIN); 7306 } 7307 7308 /* 7309 * All of the probes for this provider are disabled; we can safely 7310 * remove all of them from their hash chains and from the probe array. 7311 */ 7312 for (i = 0; i < dtrace_nprobes; i++) { 7313 if ((probe = dtrace_probes[i]) == NULL) 7314 continue; 7315 7316 if (probe->dtpr_provider != old) 7317 continue; 7318 7319 dtrace_probes[i] = NULL; 7320 7321 dtrace_hash_remove(dtrace_bymod, probe); 7322 dtrace_hash_remove(dtrace_byfunc, probe); 7323 dtrace_hash_remove(dtrace_byname, probe); 7324 7325 if (first == NULL) { 7326 first = probe; 7327 probe->dtpr_nextmod = NULL; 7328 } else { 7329 probe->dtpr_nextmod = first; 7330 first = probe; 7331 } 7332 } 7333 7334 /* 7335 * The provider's probes have been removed from the hash chains and 7336 * from the probe array. Now issue a dtrace_sync() to be sure that 7337 * everyone has cleared out from any probe array processing. 7338 */ 7339 dtrace_sync(); 7340 7341 for (probe = first; probe != NULL; probe = first) { 7342 first = probe->dtpr_nextmod; 7343 7344 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 7345 probe->dtpr_arg); 7346 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7347 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7348 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7349 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 7350 kmem_free(probe, sizeof (dtrace_probe_t)); 7351 } 7352 7353 if ((prev = dtrace_provider) == old) { 7354 ASSERT(self || dtrace_devi == NULL); 7355 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 7356 dtrace_provider = old->dtpv_next; 7357 } else { 7358 while (prev != NULL && prev->dtpv_next != old) 7359 prev = prev->dtpv_next; 7360 7361 if (prev == NULL) { 7362 panic("attempt to unregister non-existent " 7363 "dtrace provider %p\n", (void *)id); 7364 } 7365 7366 prev->dtpv_next = old->dtpv_next; 7367 } 7368 7369 if (!self) { 7370 mutex_exit(&dtrace_lock); 7371 mutex_exit(&mod_lock); 7372 mutex_exit(&dtrace_provider_lock); 7373 } 7374 7375 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 7376 kmem_free(old, sizeof (dtrace_provider_t)); 7377 7378 return (0); 7379 } 7380 7381 /* 7382 * Invalidate the specified provider. All subsequent probe lookups for the 7383 * specified provider will fail, but its probes will not be removed. 7384 */ 7385 void 7386 dtrace_invalidate(dtrace_provider_id_t id) 7387 { 7388 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 7389 7390 ASSERT(pvp->dtpv_pops.dtps_enable != 7391 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7392 7393 mutex_enter(&dtrace_provider_lock); 7394 mutex_enter(&dtrace_lock); 7395 7396 pvp->dtpv_defunct = dtrace_gethrtime(); 7397 7398 mutex_exit(&dtrace_lock); 7399 mutex_exit(&dtrace_provider_lock); 7400 } 7401 7402 /* 7403 * Indicate whether or not DTrace has attached. 7404 */ 7405 int 7406 dtrace_attached(void) 7407 { 7408 /* 7409 * dtrace_provider will be non-NULL iff the DTrace driver has 7410 * attached. (It's non-NULL because DTrace is always itself a 7411 * provider.) 7412 */ 7413 return (dtrace_provider != NULL); 7414 } 7415 7416 /* 7417 * Remove all the unenabled probes for the given provider. This function is 7418 * not unlike dtrace_unregister(), except that it doesn't remove the provider 7419 * -- just as many of its associated probes as it can. 7420 */ 7421 int 7422 dtrace_condense(dtrace_provider_id_t id) 7423 { 7424 dtrace_provider_t *prov = (dtrace_provider_t *)id; 7425 int i; 7426 dtrace_probe_t *probe; 7427 7428 /* 7429 * Make sure this isn't the dtrace provider itself. 7430 */ 7431 ASSERT(prov->dtpv_pops.dtps_enable != 7432 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7433 7434 mutex_enter(&dtrace_provider_lock); 7435 mutex_enter(&dtrace_lock); 7436 7437 /* 7438 * Attempt to destroy the probes associated with this provider. 7439 */ 7440 for (i = 0; i < dtrace_nprobes; i++) { 7441 if ((probe = dtrace_probes[i]) == NULL) 7442 continue; 7443 7444 if (probe->dtpr_provider != prov) 7445 continue; 7446 7447 if (probe->dtpr_ecb != NULL) 7448 continue; 7449 7450 dtrace_probes[i] = NULL; 7451 7452 dtrace_hash_remove(dtrace_bymod, probe); 7453 dtrace_hash_remove(dtrace_byfunc, probe); 7454 dtrace_hash_remove(dtrace_byname, probe); 7455 7456 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 7457 probe->dtpr_arg); 7458 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7459 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7460 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7461 kmem_free(probe, sizeof (dtrace_probe_t)); 7462 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 7463 } 7464 7465 mutex_exit(&dtrace_lock); 7466 mutex_exit(&dtrace_provider_lock); 7467 7468 return (0); 7469 } 7470 7471 /* 7472 * DTrace Probe Management Functions 7473 * 7474 * The functions in this section perform the DTrace probe management, 7475 * including functions to create probes, look-up probes, and call into the 7476 * providers to request that probes be provided. Some of these functions are 7477 * in the Provider-to-Framework API; these functions can be identified by the 7478 * fact that they are not declared "static". 7479 */ 7480 7481 /* 7482 * Create a probe with the specified module name, function name, and name. 7483 */ 7484 dtrace_id_t 7485 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 7486 const char *func, const char *name, int aframes, void *arg) 7487 { 7488 dtrace_probe_t *probe, **probes; 7489 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 7490 dtrace_id_t id; 7491 7492 if (provider == dtrace_provider) { 7493 ASSERT(MUTEX_HELD(&dtrace_lock)); 7494 } else { 7495 mutex_enter(&dtrace_lock); 7496 } 7497 7498 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 7499 VM_BESTFIT | VM_SLEEP); 7500 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 7501 7502 probe->dtpr_id = id; 7503 probe->dtpr_gen = dtrace_probegen++; 7504 probe->dtpr_mod = dtrace_strdup(mod); 7505 probe->dtpr_func = dtrace_strdup(func); 7506 probe->dtpr_name = dtrace_strdup(name); 7507 probe->dtpr_arg = arg; 7508 probe->dtpr_aframes = aframes; 7509 probe->dtpr_provider = provider; 7510 7511 dtrace_hash_add(dtrace_bymod, probe); 7512 dtrace_hash_add(dtrace_byfunc, probe); 7513 dtrace_hash_add(dtrace_byname, probe); 7514 7515 if (id - 1 >= dtrace_nprobes) { 7516 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 7517 size_t nsize = osize << 1; 7518 7519 if (nsize == 0) { 7520 ASSERT(osize == 0); 7521 ASSERT(dtrace_probes == NULL); 7522 nsize = sizeof (dtrace_probe_t *); 7523 } 7524 7525 probes = kmem_zalloc(nsize, KM_SLEEP); 7526 7527 if (dtrace_probes == NULL) { 7528 ASSERT(osize == 0); 7529 dtrace_probes = probes; 7530 dtrace_nprobes = 1; 7531 } else { 7532 dtrace_probe_t **oprobes = dtrace_probes; 7533 7534 bcopy(oprobes, probes, osize); 7535 dtrace_membar_producer(); 7536 dtrace_probes = probes; 7537 7538 dtrace_sync(); 7539 7540 /* 7541 * All CPUs are now seeing the new probes array; we can 7542 * safely free the old array. 7543 */ 7544 kmem_free(oprobes, osize); 7545 dtrace_nprobes <<= 1; 7546 } 7547 7548 ASSERT(id - 1 < dtrace_nprobes); 7549 } 7550 7551 ASSERT(dtrace_probes[id - 1] == NULL); 7552 dtrace_probes[id - 1] = probe; 7553 7554 if (provider != dtrace_provider) 7555 mutex_exit(&dtrace_lock); 7556 7557 return (id); 7558 } 7559 7560 static dtrace_probe_t * 7561 dtrace_probe_lookup_id(dtrace_id_t id) 7562 { 7563 ASSERT(MUTEX_HELD(&dtrace_lock)); 7564 7565 if (id == 0 || id > dtrace_nprobes) 7566 return (NULL); 7567 7568 return (dtrace_probes[id - 1]); 7569 } 7570 7571 static int 7572 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 7573 { 7574 *((dtrace_id_t *)arg) = probe->dtpr_id; 7575 7576 return (DTRACE_MATCH_DONE); 7577 } 7578 7579 /* 7580 * Look up a probe based on provider and one or more of module name, function 7581 * name and probe name. 7582 */ 7583 dtrace_id_t 7584 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, 7585 const char *func, const char *name) 7586 { 7587 dtrace_probekey_t pkey; 7588 dtrace_id_t id; 7589 int match; 7590 7591 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 7592 pkey.dtpk_pmatch = &dtrace_match_string; 7593 pkey.dtpk_mod = mod; 7594 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 7595 pkey.dtpk_func = func; 7596 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 7597 pkey.dtpk_name = name; 7598 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 7599 pkey.dtpk_id = DTRACE_IDNONE; 7600 7601 mutex_enter(&dtrace_lock); 7602 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 7603 dtrace_probe_lookup_match, &id); 7604 mutex_exit(&dtrace_lock); 7605 7606 ASSERT(match == 1 || match == 0); 7607 return (match ? id : 0); 7608 } 7609 7610 /* 7611 * Returns the probe argument associated with the specified probe. 7612 */ 7613 void * 7614 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 7615 { 7616 dtrace_probe_t *probe; 7617 void *rval = NULL; 7618 7619 mutex_enter(&dtrace_lock); 7620 7621 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 7622 probe->dtpr_provider == (dtrace_provider_t *)id) 7623 rval = probe->dtpr_arg; 7624 7625 mutex_exit(&dtrace_lock); 7626 7627 return (rval); 7628 } 7629 7630 /* 7631 * Copy a probe into a probe description. 7632 */ 7633 static void 7634 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 7635 { 7636 bzero(pdp, sizeof (dtrace_probedesc_t)); 7637 pdp->dtpd_id = prp->dtpr_id; 7638 7639 (void) strncpy(pdp->dtpd_provider, 7640 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 7641 7642 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 7643 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 7644 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 7645 } 7646 7647 /* 7648 * Called to indicate that a probe -- or probes -- should be provided by a 7649 * specfied provider. If the specified description is NULL, the provider will 7650 * be told to provide all of its probes. (This is done whenever a new 7651 * consumer comes along, or whenever a retained enabling is to be matched.) If 7652 * the specified description is non-NULL, the provider is given the 7653 * opportunity to dynamically provide the specified probe, allowing providers 7654 * to support the creation of probes on-the-fly. (So-called _autocreated_ 7655 * probes.) If the provider is NULL, the operations will be applied to all 7656 * providers; if the provider is non-NULL the operations will only be applied 7657 * to the specified provider. The dtrace_provider_lock must be held, and the 7658 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 7659 * will need to grab the dtrace_lock when it reenters the framework through 7660 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 7661 */ 7662 static void 7663 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 7664 { 7665 struct modctl *ctl; 7666 int all = 0; 7667 7668 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7669 7670 if (prv == NULL) { 7671 all = 1; 7672 prv = dtrace_provider; 7673 } 7674 7675 do { 7676 /* 7677 * First, call the blanket provide operation. 7678 */ 7679 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 7680 7681 /* 7682 * Now call the per-module provide operation. We will grab 7683 * mod_lock to prevent the list from being modified. Note 7684 * that this also prevents the mod_busy bits from changing. 7685 * (mod_busy can only be changed with mod_lock held.) 7686 */ 7687 mutex_enter(&mod_lock); 7688 7689 ctl = &modules; 7690 do { 7691 if (ctl->mod_busy || ctl->mod_mp == NULL) 7692 continue; 7693 7694 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 7695 7696 } while ((ctl = ctl->mod_next) != &modules); 7697 7698 mutex_exit(&mod_lock); 7699 } while (all && (prv = prv->dtpv_next) != NULL); 7700 } 7701 7702 /* 7703 * Iterate over each probe, and call the Framework-to-Provider API function 7704 * denoted by offs. 7705 */ 7706 static void 7707 dtrace_probe_foreach(uintptr_t offs) 7708 { 7709 dtrace_provider_t *prov; 7710 void (*func)(void *, dtrace_id_t, void *); 7711 dtrace_probe_t *probe; 7712 dtrace_icookie_t cookie; 7713 int i; 7714 7715 /* 7716 * We disable interrupts to walk through the probe array. This is 7717 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 7718 * won't see stale data. 7719 */ 7720 cookie = dtrace_interrupt_disable(); 7721 7722 for (i = 0; i < dtrace_nprobes; i++) { 7723 if ((probe = dtrace_probes[i]) == NULL) 7724 continue; 7725 7726 if (probe->dtpr_ecb == NULL) { 7727 /* 7728 * This probe isn't enabled -- don't call the function. 7729 */ 7730 continue; 7731 } 7732 7733 prov = probe->dtpr_provider; 7734 func = *((void(**)(void *, dtrace_id_t, void *)) 7735 ((uintptr_t)&prov->dtpv_pops + offs)); 7736 7737 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 7738 } 7739 7740 dtrace_interrupt_enable(cookie); 7741 } 7742 7743 static int 7744 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 7745 { 7746 dtrace_probekey_t pkey; 7747 uint32_t priv; 7748 uid_t uid; 7749 zoneid_t zoneid; 7750 7751 ASSERT(MUTEX_HELD(&dtrace_lock)); 7752 dtrace_ecb_create_cache = NULL; 7753 7754 if (desc == NULL) { 7755 /* 7756 * If we're passed a NULL description, we're being asked to 7757 * create an ECB with a NULL probe. 7758 */ 7759 (void) dtrace_ecb_create_enable(NULL, enab); 7760 return (0); 7761 } 7762 7763 dtrace_probekey(desc, &pkey); 7764 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 7765 &priv, &uid, &zoneid); 7766 7767 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 7768 enab)); 7769 } 7770 7771 /* 7772 * DTrace Helper Provider Functions 7773 */ 7774 static void 7775 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 7776 { 7777 attr->dtat_name = DOF_ATTR_NAME(dofattr); 7778 attr->dtat_data = DOF_ATTR_DATA(dofattr); 7779 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 7780 } 7781 7782 static void 7783 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 7784 const dof_provider_t *dofprov, char *strtab) 7785 { 7786 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 7787 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 7788 dofprov->dofpv_provattr); 7789 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 7790 dofprov->dofpv_modattr); 7791 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 7792 dofprov->dofpv_funcattr); 7793 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 7794 dofprov->dofpv_nameattr); 7795 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 7796 dofprov->dofpv_argsattr); 7797 } 7798 7799 static void 7800 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 7801 { 7802 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7803 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7804 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 7805 dof_provider_t *provider; 7806 dof_probe_t *probe; 7807 uint32_t *off, *enoff; 7808 uint8_t *arg; 7809 char *strtab; 7810 uint_t i, nprobes; 7811 dtrace_helper_provdesc_t dhpv; 7812 dtrace_helper_probedesc_t dhpb; 7813 dtrace_meta_t *meta = dtrace_meta_pid; 7814 dtrace_mops_t *mops = &meta->dtm_mops; 7815 void *parg; 7816 7817 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 7818 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7819 provider->dofpv_strtab * dof->dofh_secsize); 7820 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7821 provider->dofpv_probes * dof->dofh_secsize); 7822 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7823 provider->dofpv_prargs * dof->dofh_secsize); 7824 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7825 provider->dofpv_proffs * dof->dofh_secsize); 7826 7827 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 7828 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 7829 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 7830 enoff = NULL; 7831 7832 /* 7833 * See dtrace_helper_provider_validate(). 7834 */ 7835 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 7836 provider->dofpv_prenoffs != DOF_SECT_NONE) { 7837 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7838 provider->dofpv_prenoffs * dof->dofh_secsize); 7839 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 7840 } 7841 7842 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 7843 7844 /* 7845 * Create the provider. 7846 */ 7847 dtrace_dofprov2hprov(&dhpv, provider, strtab); 7848 7849 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 7850 return; 7851 7852 meta->dtm_count++; 7853 7854 /* 7855 * Create the probes. 7856 */ 7857 for (i = 0; i < nprobes; i++) { 7858 probe = (dof_probe_t *)(uintptr_t)(daddr + 7859 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 7860 7861 dhpb.dthpb_mod = dhp->dofhp_mod; 7862 dhpb.dthpb_func = strtab + probe->dofpr_func; 7863 dhpb.dthpb_name = strtab + probe->dofpr_name; 7864 dhpb.dthpb_base = probe->dofpr_addr; 7865 dhpb.dthpb_offs = off + probe->dofpr_offidx; 7866 dhpb.dthpb_noffs = probe->dofpr_noffs; 7867 if (enoff != NULL) { 7868 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 7869 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 7870 } else { 7871 dhpb.dthpb_enoffs = NULL; 7872 dhpb.dthpb_nenoffs = 0; 7873 } 7874 dhpb.dthpb_args = arg + probe->dofpr_argidx; 7875 dhpb.dthpb_nargc = probe->dofpr_nargc; 7876 dhpb.dthpb_xargc = probe->dofpr_xargc; 7877 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 7878 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 7879 7880 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 7881 } 7882 } 7883 7884 static void 7885 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 7886 { 7887 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7888 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7889 int i; 7890 7891 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 7892 7893 for (i = 0; i < dof->dofh_secnum; i++) { 7894 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 7895 dof->dofh_secoff + i * dof->dofh_secsize); 7896 7897 if (sec->dofs_type != DOF_SECT_PROVIDER) 7898 continue; 7899 7900 dtrace_helper_provide_one(dhp, sec, pid); 7901 } 7902 7903 /* 7904 * We may have just created probes, so we must now rematch against 7905 * any retained enablings. Note that this call will acquire both 7906 * cpu_lock and dtrace_lock; the fact that we are holding 7907 * dtrace_meta_lock now is what defines the ordering with respect to 7908 * these three locks. 7909 */ 7910 dtrace_enabling_matchall(); 7911 } 7912 7913 static void 7914 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 7915 { 7916 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7917 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7918 dof_sec_t *str_sec; 7919 dof_provider_t *provider; 7920 char *strtab; 7921 dtrace_helper_provdesc_t dhpv; 7922 dtrace_meta_t *meta = dtrace_meta_pid; 7923 dtrace_mops_t *mops = &meta->dtm_mops; 7924 7925 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 7926 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7927 provider->dofpv_strtab * dof->dofh_secsize); 7928 7929 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 7930 7931 /* 7932 * Create the provider. 7933 */ 7934 dtrace_dofprov2hprov(&dhpv, provider, strtab); 7935 7936 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 7937 7938 meta->dtm_count--; 7939 } 7940 7941 static void 7942 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 7943 { 7944 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7945 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7946 int i; 7947 7948 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 7949 7950 for (i = 0; i < dof->dofh_secnum; i++) { 7951 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 7952 dof->dofh_secoff + i * dof->dofh_secsize); 7953 7954 if (sec->dofs_type != DOF_SECT_PROVIDER) 7955 continue; 7956 7957 dtrace_helper_provider_remove_one(dhp, sec, pid); 7958 } 7959 } 7960 7961 /* 7962 * DTrace Meta Provider-to-Framework API Functions 7963 * 7964 * These functions implement the Meta Provider-to-Framework API, as described 7965 * in <sys/dtrace.h>. 7966 */ 7967 int 7968 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 7969 dtrace_meta_provider_id_t *idp) 7970 { 7971 dtrace_meta_t *meta; 7972 dtrace_helpers_t *help, *next; 7973 int i; 7974 7975 *idp = DTRACE_METAPROVNONE; 7976 7977 /* 7978 * We strictly don't need the name, but we hold onto it for 7979 * debuggability. All hail error queues! 7980 */ 7981 if (name == NULL) { 7982 cmn_err(CE_WARN, "failed to register meta-provider: " 7983 "invalid name"); 7984 return (EINVAL); 7985 } 7986 7987 if (mops == NULL || 7988 mops->dtms_create_probe == NULL || 7989 mops->dtms_provide_pid == NULL || 7990 mops->dtms_remove_pid == NULL) { 7991 cmn_err(CE_WARN, "failed to register meta-register %s: " 7992 "invalid ops", name); 7993 return (EINVAL); 7994 } 7995 7996 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 7997 meta->dtm_mops = *mops; 7998 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7999 (void) strcpy(meta->dtm_name, name); 8000 meta->dtm_arg = arg; 8001 8002 mutex_enter(&dtrace_meta_lock); 8003 mutex_enter(&dtrace_lock); 8004 8005 if (dtrace_meta_pid != NULL) { 8006 mutex_exit(&dtrace_lock); 8007 mutex_exit(&dtrace_meta_lock); 8008 cmn_err(CE_WARN, "failed to register meta-register %s: " 8009 "user-land meta-provider exists", name); 8010 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 8011 kmem_free(meta, sizeof (dtrace_meta_t)); 8012 return (EINVAL); 8013 } 8014 8015 dtrace_meta_pid = meta; 8016 *idp = (dtrace_meta_provider_id_t)meta; 8017 8018 /* 8019 * If there are providers and probes ready to go, pass them 8020 * off to the new meta provider now. 8021 */ 8022 8023 help = dtrace_deferred_pid; 8024 dtrace_deferred_pid = NULL; 8025 8026 mutex_exit(&dtrace_lock); 8027 8028 while (help != NULL) { 8029 for (i = 0; i < help->dthps_nprovs; i++) { 8030 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 8031 help->dthps_pid); 8032 } 8033 8034 next = help->dthps_next; 8035 help->dthps_next = NULL; 8036 help->dthps_prev = NULL; 8037 help->dthps_deferred = 0; 8038 help = next; 8039 } 8040 8041 mutex_exit(&dtrace_meta_lock); 8042 8043 return (0); 8044 } 8045 8046 int 8047 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 8048 { 8049 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 8050 8051 mutex_enter(&dtrace_meta_lock); 8052 mutex_enter(&dtrace_lock); 8053 8054 if (old == dtrace_meta_pid) { 8055 pp = &dtrace_meta_pid; 8056 } else { 8057 panic("attempt to unregister non-existent " 8058 "dtrace meta-provider %p\n", (void *)old); 8059 } 8060 8061 if (old->dtm_count != 0) { 8062 mutex_exit(&dtrace_lock); 8063 mutex_exit(&dtrace_meta_lock); 8064 return (EBUSY); 8065 } 8066 8067 *pp = NULL; 8068 8069 mutex_exit(&dtrace_lock); 8070 mutex_exit(&dtrace_meta_lock); 8071 8072 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 8073 kmem_free(old, sizeof (dtrace_meta_t)); 8074 8075 return (0); 8076 } 8077 8078 8079 /* 8080 * DTrace DIF Object Functions 8081 */ 8082 static int 8083 dtrace_difo_err(uint_t pc, const char *format, ...) 8084 { 8085 if (dtrace_err_verbose) { 8086 va_list alist; 8087 8088 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 8089 va_start(alist, format); 8090 (void) vuprintf(format, alist); 8091 va_end(alist); 8092 } 8093 8094 #ifdef DTRACE_ERRDEBUG 8095 dtrace_errdebug(format); 8096 #endif 8097 return (1); 8098 } 8099 8100 /* 8101 * Validate a DTrace DIF object by checking the IR instructions. The following 8102 * rules are currently enforced by dtrace_difo_validate(): 8103 * 8104 * 1. Each instruction must have a valid opcode 8105 * 2. Each register, string, variable, or subroutine reference must be valid 8106 * 3. No instruction can modify register %r0 (must be zero) 8107 * 4. All instruction reserved bits must be set to zero 8108 * 5. The last instruction must be a "ret" instruction 8109 * 6. All branch targets must reference a valid instruction _after_ the branch 8110 */ 8111 static int 8112 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 8113 cred_t *cr) 8114 { 8115 int err = 0, i; 8116 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8117 int kcheckload; 8118 uint_t pc; 8119 8120 kcheckload = cr == NULL || 8121 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 8122 8123 dp->dtdo_destructive = 0; 8124 8125 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 8126 dif_instr_t instr = dp->dtdo_buf[pc]; 8127 8128 uint_t r1 = DIF_INSTR_R1(instr); 8129 uint_t r2 = DIF_INSTR_R2(instr); 8130 uint_t rd = DIF_INSTR_RD(instr); 8131 uint_t rs = DIF_INSTR_RS(instr); 8132 uint_t label = DIF_INSTR_LABEL(instr); 8133 uint_t v = DIF_INSTR_VAR(instr); 8134 uint_t subr = DIF_INSTR_SUBR(instr); 8135 uint_t type = DIF_INSTR_TYPE(instr); 8136 uint_t op = DIF_INSTR_OP(instr); 8137 8138 switch (op) { 8139 case DIF_OP_OR: 8140 case DIF_OP_XOR: 8141 case DIF_OP_AND: 8142 case DIF_OP_SLL: 8143 case DIF_OP_SRL: 8144 case DIF_OP_SRA: 8145 case DIF_OP_SUB: 8146 case DIF_OP_ADD: 8147 case DIF_OP_MUL: 8148 case DIF_OP_SDIV: 8149 case DIF_OP_UDIV: 8150 case DIF_OP_SREM: 8151 case DIF_OP_UREM: 8152 case DIF_OP_COPYS: 8153 if (r1 >= nregs) 8154 err += efunc(pc, "invalid register %u\n", r1); 8155 if (r2 >= nregs) 8156 err += efunc(pc, "invalid register %u\n", r2); 8157 if (rd >= nregs) 8158 err += efunc(pc, "invalid register %u\n", rd); 8159 if (rd == 0) 8160 err += efunc(pc, "cannot write to %r0\n"); 8161 break; 8162 case DIF_OP_NOT: 8163 case DIF_OP_MOV: 8164 case DIF_OP_ALLOCS: 8165 if (r1 >= nregs) 8166 err += efunc(pc, "invalid register %u\n", r1); 8167 if (r2 != 0) 8168 err += efunc(pc, "non-zero reserved bits\n"); 8169 if (rd >= nregs) 8170 err += efunc(pc, "invalid register %u\n", rd); 8171 if (rd == 0) 8172 err += efunc(pc, "cannot write to %r0\n"); 8173 break; 8174 case DIF_OP_LDSB: 8175 case DIF_OP_LDSH: 8176 case DIF_OP_LDSW: 8177 case DIF_OP_LDUB: 8178 case DIF_OP_LDUH: 8179 case DIF_OP_LDUW: 8180 case DIF_OP_LDX: 8181 if (r1 >= nregs) 8182 err += efunc(pc, "invalid register %u\n", r1); 8183 if (r2 != 0) 8184 err += efunc(pc, "non-zero reserved bits\n"); 8185 if (rd >= nregs) 8186 err += efunc(pc, "invalid register %u\n", rd); 8187 if (rd == 0) 8188 err += efunc(pc, "cannot write to %r0\n"); 8189 if (kcheckload) 8190 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 8191 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 8192 break; 8193 case DIF_OP_RLDSB: 8194 case DIF_OP_RLDSH: 8195 case DIF_OP_RLDSW: 8196 case DIF_OP_RLDUB: 8197 case DIF_OP_RLDUH: 8198 case DIF_OP_RLDUW: 8199 case DIF_OP_RLDX: 8200 if (r1 >= nregs) 8201 err += efunc(pc, "invalid register %u\n", r1); 8202 if (r2 != 0) 8203 err += efunc(pc, "non-zero reserved bits\n"); 8204 if (rd >= nregs) 8205 err += efunc(pc, "invalid register %u\n", rd); 8206 if (rd == 0) 8207 err += efunc(pc, "cannot write to %r0\n"); 8208 break; 8209 case DIF_OP_ULDSB: 8210 case DIF_OP_ULDSH: 8211 case DIF_OP_ULDSW: 8212 case DIF_OP_ULDUB: 8213 case DIF_OP_ULDUH: 8214 case DIF_OP_ULDUW: 8215 case DIF_OP_ULDX: 8216 if (r1 >= nregs) 8217 err += efunc(pc, "invalid register %u\n", r1); 8218 if (r2 != 0) 8219 err += efunc(pc, "non-zero reserved bits\n"); 8220 if (rd >= nregs) 8221 err += efunc(pc, "invalid register %u\n", rd); 8222 if (rd == 0) 8223 err += efunc(pc, "cannot write to %r0\n"); 8224 break; 8225 case DIF_OP_STB: 8226 case DIF_OP_STH: 8227 case DIF_OP_STW: 8228 case DIF_OP_STX: 8229 if (r1 >= nregs) 8230 err += efunc(pc, "invalid register %u\n", r1); 8231 if (r2 != 0) 8232 err += efunc(pc, "non-zero reserved bits\n"); 8233 if (rd >= nregs) 8234 err += efunc(pc, "invalid register %u\n", rd); 8235 if (rd == 0) 8236 err += efunc(pc, "cannot write to 0 address\n"); 8237 break; 8238 case DIF_OP_CMP: 8239 case DIF_OP_SCMP: 8240 if (r1 >= nregs) 8241 err += efunc(pc, "invalid register %u\n", r1); 8242 if (r2 >= nregs) 8243 err += efunc(pc, "invalid register %u\n", r2); 8244 if (rd != 0) 8245 err += efunc(pc, "non-zero reserved bits\n"); 8246 break; 8247 case DIF_OP_TST: 8248 if (r1 >= nregs) 8249 err += efunc(pc, "invalid register %u\n", r1); 8250 if (r2 != 0 || rd != 0) 8251 err += efunc(pc, "non-zero reserved bits\n"); 8252 break; 8253 case DIF_OP_BA: 8254 case DIF_OP_BE: 8255 case DIF_OP_BNE: 8256 case DIF_OP_BG: 8257 case DIF_OP_BGU: 8258 case DIF_OP_BGE: 8259 case DIF_OP_BGEU: 8260 case DIF_OP_BL: 8261 case DIF_OP_BLU: 8262 case DIF_OP_BLE: 8263 case DIF_OP_BLEU: 8264 if (label >= dp->dtdo_len) { 8265 err += efunc(pc, "invalid branch target %u\n", 8266 label); 8267 } 8268 if (label <= pc) { 8269 err += efunc(pc, "backward branch to %u\n", 8270 label); 8271 } 8272 break; 8273 case DIF_OP_RET: 8274 if (r1 != 0 || r2 != 0) 8275 err += efunc(pc, "non-zero reserved bits\n"); 8276 if (rd >= nregs) 8277 err += efunc(pc, "invalid register %u\n", rd); 8278 break; 8279 case DIF_OP_NOP: 8280 case DIF_OP_POPTS: 8281 case DIF_OP_FLUSHTS: 8282 if (r1 != 0 || r2 != 0 || rd != 0) 8283 err += efunc(pc, "non-zero reserved bits\n"); 8284 break; 8285 case DIF_OP_SETX: 8286 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 8287 err += efunc(pc, "invalid integer ref %u\n", 8288 DIF_INSTR_INTEGER(instr)); 8289 } 8290 if (rd >= nregs) 8291 err += efunc(pc, "invalid register %u\n", rd); 8292 if (rd == 0) 8293 err += efunc(pc, "cannot write to %r0\n"); 8294 break; 8295 case DIF_OP_SETS: 8296 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 8297 err += efunc(pc, "invalid string ref %u\n", 8298 DIF_INSTR_STRING(instr)); 8299 } 8300 if (rd >= nregs) 8301 err += efunc(pc, "invalid register %u\n", rd); 8302 if (rd == 0) 8303 err += efunc(pc, "cannot write to %r0\n"); 8304 break; 8305 case DIF_OP_LDGA: 8306 case DIF_OP_LDTA: 8307 if (r1 > DIF_VAR_ARRAY_MAX) 8308 err += efunc(pc, "invalid array %u\n", r1); 8309 if (r2 >= nregs) 8310 err += efunc(pc, "invalid register %u\n", r2); 8311 if (rd >= nregs) 8312 err += efunc(pc, "invalid register %u\n", rd); 8313 if (rd == 0) 8314 err += efunc(pc, "cannot write to %r0\n"); 8315 break; 8316 case DIF_OP_LDGS: 8317 case DIF_OP_LDTS: 8318 case DIF_OP_LDLS: 8319 case DIF_OP_LDGAA: 8320 case DIF_OP_LDTAA: 8321 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 8322 err += efunc(pc, "invalid variable %u\n", v); 8323 if (rd >= nregs) 8324 err += efunc(pc, "invalid register %u\n", rd); 8325 if (rd == 0) 8326 err += efunc(pc, "cannot write to %r0\n"); 8327 break; 8328 case DIF_OP_STGS: 8329 case DIF_OP_STTS: 8330 case DIF_OP_STLS: 8331 case DIF_OP_STGAA: 8332 case DIF_OP_STTAA: 8333 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 8334 err += efunc(pc, "invalid variable %u\n", v); 8335 if (rs >= nregs) 8336 err += efunc(pc, "invalid register %u\n", rd); 8337 break; 8338 case DIF_OP_CALL: 8339 if (subr > DIF_SUBR_MAX) 8340 err += efunc(pc, "invalid subr %u\n", subr); 8341 if (rd >= nregs) 8342 err += efunc(pc, "invalid register %u\n", rd); 8343 if (rd == 0) 8344 err += efunc(pc, "cannot write to %r0\n"); 8345 8346 if (subr == DIF_SUBR_COPYOUT || 8347 subr == DIF_SUBR_COPYOUTSTR) { 8348 dp->dtdo_destructive = 1; 8349 } 8350 break; 8351 case DIF_OP_PUSHTR: 8352 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 8353 err += efunc(pc, "invalid ref type %u\n", type); 8354 if (r2 >= nregs) 8355 err += efunc(pc, "invalid register %u\n", r2); 8356 if (rs >= nregs) 8357 err += efunc(pc, "invalid register %u\n", rs); 8358 break; 8359 case DIF_OP_PUSHTV: 8360 if (type != DIF_TYPE_CTF) 8361 err += efunc(pc, "invalid val type %u\n", type); 8362 if (r2 >= nregs) 8363 err += efunc(pc, "invalid register %u\n", r2); 8364 if (rs >= nregs) 8365 err += efunc(pc, "invalid register %u\n", rs); 8366 break; 8367 default: 8368 err += efunc(pc, "invalid opcode %u\n", 8369 DIF_INSTR_OP(instr)); 8370 } 8371 } 8372 8373 if (dp->dtdo_len != 0 && 8374 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 8375 err += efunc(dp->dtdo_len - 1, 8376 "expected 'ret' as last DIF instruction\n"); 8377 } 8378 8379 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 8380 /* 8381 * If we're not returning by reference, the size must be either 8382 * 0 or the size of one of the base types. 8383 */ 8384 switch (dp->dtdo_rtype.dtdt_size) { 8385 case 0: 8386 case sizeof (uint8_t): 8387 case sizeof (uint16_t): 8388 case sizeof (uint32_t): 8389 case sizeof (uint64_t): 8390 break; 8391 8392 default: 8393 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 8394 } 8395 } 8396 8397 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 8398 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 8399 dtrace_diftype_t *vt, *et; 8400 uint_t id, ndx; 8401 8402 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 8403 v->dtdv_scope != DIFV_SCOPE_THREAD && 8404 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 8405 err += efunc(i, "unrecognized variable scope %d\n", 8406 v->dtdv_scope); 8407 break; 8408 } 8409 8410 if (v->dtdv_kind != DIFV_KIND_ARRAY && 8411 v->dtdv_kind != DIFV_KIND_SCALAR) { 8412 err += efunc(i, "unrecognized variable type %d\n", 8413 v->dtdv_kind); 8414 break; 8415 } 8416 8417 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 8418 err += efunc(i, "%d exceeds variable id limit\n", id); 8419 break; 8420 } 8421 8422 if (id < DIF_VAR_OTHER_UBASE) 8423 continue; 8424 8425 /* 8426 * For user-defined variables, we need to check that this 8427 * definition is identical to any previous definition that we 8428 * encountered. 8429 */ 8430 ndx = id - DIF_VAR_OTHER_UBASE; 8431 8432 switch (v->dtdv_scope) { 8433 case DIFV_SCOPE_GLOBAL: 8434 if (ndx < vstate->dtvs_nglobals) { 8435 dtrace_statvar_t *svar; 8436 8437 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 8438 existing = &svar->dtsv_var; 8439 } 8440 8441 break; 8442 8443 case DIFV_SCOPE_THREAD: 8444 if (ndx < vstate->dtvs_ntlocals) 8445 existing = &vstate->dtvs_tlocals[ndx]; 8446 break; 8447 8448 case DIFV_SCOPE_LOCAL: 8449 if (ndx < vstate->dtvs_nlocals) { 8450 dtrace_statvar_t *svar; 8451 8452 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 8453 existing = &svar->dtsv_var; 8454 } 8455 8456 break; 8457 } 8458 8459 vt = &v->dtdv_type; 8460 8461 if (vt->dtdt_flags & DIF_TF_BYREF) { 8462 if (vt->dtdt_size == 0) { 8463 err += efunc(i, "zero-sized variable\n"); 8464 break; 8465 } 8466 8467 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 8468 vt->dtdt_size > dtrace_global_maxsize) { 8469 err += efunc(i, "oversized by-ref global\n"); 8470 break; 8471 } 8472 } 8473 8474 if (existing == NULL || existing->dtdv_id == 0) 8475 continue; 8476 8477 ASSERT(existing->dtdv_id == v->dtdv_id); 8478 ASSERT(existing->dtdv_scope == v->dtdv_scope); 8479 8480 if (existing->dtdv_kind != v->dtdv_kind) 8481 err += efunc(i, "%d changed variable kind\n", id); 8482 8483 et = &existing->dtdv_type; 8484 8485 if (vt->dtdt_flags != et->dtdt_flags) { 8486 err += efunc(i, "%d changed variable type flags\n", id); 8487 break; 8488 } 8489 8490 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 8491 err += efunc(i, "%d changed variable type size\n", id); 8492 break; 8493 } 8494 } 8495 8496 return (err); 8497 } 8498 8499 /* 8500 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 8501 * are much more constrained than normal DIFOs. Specifically, they may 8502 * not: 8503 * 8504 * 1. Make calls to subroutines other than copyin(), copyinstr() or 8505 * miscellaneous string routines 8506 * 2. Access DTrace variables other than the args[] array, and the 8507 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 8508 * 3. Have thread-local variables. 8509 * 4. Have dynamic variables. 8510 */ 8511 static int 8512 dtrace_difo_validate_helper(dtrace_difo_t *dp) 8513 { 8514 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8515 int err = 0; 8516 uint_t pc; 8517 8518 for (pc = 0; pc < dp->dtdo_len; pc++) { 8519 dif_instr_t instr = dp->dtdo_buf[pc]; 8520 8521 uint_t v = DIF_INSTR_VAR(instr); 8522 uint_t subr = DIF_INSTR_SUBR(instr); 8523 uint_t op = DIF_INSTR_OP(instr); 8524 8525 switch (op) { 8526 case DIF_OP_OR: 8527 case DIF_OP_XOR: 8528 case DIF_OP_AND: 8529 case DIF_OP_SLL: 8530 case DIF_OP_SRL: 8531 case DIF_OP_SRA: 8532 case DIF_OP_SUB: 8533 case DIF_OP_ADD: 8534 case DIF_OP_MUL: 8535 case DIF_OP_SDIV: 8536 case DIF_OP_UDIV: 8537 case DIF_OP_SREM: 8538 case DIF_OP_UREM: 8539 case DIF_OP_COPYS: 8540 case DIF_OP_NOT: 8541 case DIF_OP_MOV: 8542 case DIF_OP_RLDSB: 8543 case DIF_OP_RLDSH: 8544 case DIF_OP_RLDSW: 8545 case DIF_OP_RLDUB: 8546 case DIF_OP_RLDUH: 8547 case DIF_OP_RLDUW: 8548 case DIF_OP_RLDX: 8549 case DIF_OP_ULDSB: 8550 case DIF_OP_ULDSH: 8551 case DIF_OP_ULDSW: 8552 case DIF_OP_ULDUB: 8553 case DIF_OP_ULDUH: 8554 case DIF_OP_ULDUW: 8555 case DIF_OP_ULDX: 8556 case DIF_OP_STB: 8557 case DIF_OP_STH: 8558 case DIF_OP_STW: 8559 case DIF_OP_STX: 8560 case DIF_OP_ALLOCS: 8561 case DIF_OP_CMP: 8562 case DIF_OP_SCMP: 8563 case DIF_OP_TST: 8564 case DIF_OP_BA: 8565 case DIF_OP_BE: 8566 case DIF_OP_BNE: 8567 case DIF_OP_BG: 8568 case DIF_OP_BGU: 8569 case DIF_OP_BGE: 8570 case DIF_OP_BGEU: 8571 case DIF_OP_BL: 8572 case DIF_OP_BLU: 8573 case DIF_OP_BLE: 8574 case DIF_OP_BLEU: 8575 case DIF_OP_RET: 8576 case DIF_OP_NOP: 8577 case DIF_OP_POPTS: 8578 case DIF_OP_FLUSHTS: 8579 case DIF_OP_SETX: 8580 case DIF_OP_SETS: 8581 case DIF_OP_LDGA: 8582 case DIF_OP_LDLS: 8583 case DIF_OP_STGS: 8584 case DIF_OP_STLS: 8585 case DIF_OP_PUSHTR: 8586 case DIF_OP_PUSHTV: 8587 break; 8588 8589 case DIF_OP_LDGS: 8590 if (v >= DIF_VAR_OTHER_UBASE) 8591 break; 8592 8593 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 8594 break; 8595 8596 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 8597 v == DIF_VAR_PPID || v == DIF_VAR_TID || 8598 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 8599 v == DIF_VAR_UID || v == DIF_VAR_GID) 8600 break; 8601 8602 err += efunc(pc, "illegal variable %u\n", v); 8603 break; 8604 8605 case DIF_OP_LDTA: 8606 case DIF_OP_LDTS: 8607 case DIF_OP_LDGAA: 8608 case DIF_OP_LDTAA: 8609 err += efunc(pc, "illegal dynamic variable load\n"); 8610 break; 8611 8612 case DIF_OP_STTS: 8613 case DIF_OP_STGAA: 8614 case DIF_OP_STTAA: 8615 err += efunc(pc, "illegal dynamic variable store\n"); 8616 break; 8617 8618 case DIF_OP_CALL: 8619 if (subr == DIF_SUBR_ALLOCA || 8620 subr == DIF_SUBR_BCOPY || 8621 subr == DIF_SUBR_COPYIN || 8622 subr == DIF_SUBR_COPYINTO || 8623 subr == DIF_SUBR_COPYINSTR || 8624 subr == DIF_SUBR_INDEX || 8625 subr == DIF_SUBR_INET_NTOA || 8626 subr == DIF_SUBR_INET_NTOA6 || 8627 subr == DIF_SUBR_INET_NTOP || 8628 subr == DIF_SUBR_LLTOSTR || 8629 subr == DIF_SUBR_RINDEX || 8630 subr == DIF_SUBR_STRCHR || 8631 subr == DIF_SUBR_STRJOIN || 8632 subr == DIF_SUBR_STRRCHR || 8633 subr == DIF_SUBR_STRSTR || 8634 subr == DIF_SUBR_HTONS || 8635 subr == DIF_SUBR_HTONL || 8636 subr == DIF_SUBR_HTONLL || 8637 subr == DIF_SUBR_NTOHS || 8638 subr == DIF_SUBR_NTOHL || 8639 subr == DIF_SUBR_NTOHLL) 8640 break; 8641 8642 err += efunc(pc, "invalid subr %u\n", subr); 8643 break; 8644 8645 default: 8646 err += efunc(pc, "invalid opcode %u\n", 8647 DIF_INSTR_OP(instr)); 8648 } 8649 } 8650 8651 return (err); 8652 } 8653 8654 /* 8655 * Returns 1 if the expression in the DIF object can be cached on a per-thread 8656 * basis; 0 if not. 8657 */ 8658 static int 8659 dtrace_difo_cacheable(dtrace_difo_t *dp) 8660 { 8661 int i; 8662 8663 if (dp == NULL) 8664 return (0); 8665 8666 for (i = 0; i < dp->dtdo_varlen; i++) { 8667 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8668 8669 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 8670 continue; 8671 8672 switch (v->dtdv_id) { 8673 case DIF_VAR_CURTHREAD: 8674 case DIF_VAR_PID: 8675 case DIF_VAR_TID: 8676 case DIF_VAR_EXECNAME: 8677 case DIF_VAR_ZONENAME: 8678 break; 8679 8680 default: 8681 return (0); 8682 } 8683 } 8684 8685 /* 8686 * This DIF object may be cacheable. Now we need to look for any 8687 * array loading instructions, any memory loading instructions, or 8688 * any stores to thread-local variables. 8689 */ 8690 for (i = 0; i < dp->dtdo_len; i++) { 8691 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 8692 8693 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 8694 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 8695 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 8696 op == DIF_OP_LDGA || op == DIF_OP_STTS) 8697 return (0); 8698 } 8699 8700 return (1); 8701 } 8702 8703 static void 8704 dtrace_difo_hold(dtrace_difo_t *dp) 8705 { 8706 int i; 8707 8708 ASSERT(MUTEX_HELD(&dtrace_lock)); 8709 8710 dp->dtdo_refcnt++; 8711 ASSERT(dp->dtdo_refcnt != 0); 8712 8713 /* 8714 * We need to check this DIF object for references to the variable 8715 * DIF_VAR_VTIMESTAMP. 8716 */ 8717 for (i = 0; i < dp->dtdo_varlen; i++) { 8718 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8719 8720 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 8721 continue; 8722 8723 if (dtrace_vtime_references++ == 0) 8724 dtrace_vtime_enable(); 8725 } 8726 } 8727 8728 /* 8729 * This routine calculates the dynamic variable chunksize for a given DIF 8730 * object. The calculation is not fool-proof, and can probably be tricked by 8731 * malicious DIF -- but it works for all compiler-generated DIF. Because this 8732 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 8733 * if a dynamic variable size exceeds the chunksize. 8734 */ 8735 static void 8736 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8737 { 8738 uint64_t sval; 8739 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 8740 const dif_instr_t *text = dp->dtdo_buf; 8741 uint_t pc, srd = 0; 8742 uint_t ttop = 0; 8743 size_t size, ksize; 8744 uint_t id, i; 8745 8746 for (pc = 0; pc < dp->dtdo_len; pc++) { 8747 dif_instr_t instr = text[pc]; 8748 uint_t op = DIF_INSTR_OP(instr); 8749 uint_t rd = DIF_INSTR_RD(instr); 8750 uint_t r1 = DIF_INSTR_R1(instr); 8751 uint_t nkeys = 0; 8752 uchar_t scope; 8753 8754 dtrace_key_t *key = tupregs; 8755 8756 switch (op) { 8757 case DIF_OP_SETX: 8758 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 8759 srd = rd; 8760 continue; 8761 8762 case DIF_OP_STTS: 8763 key = &tupregs[DIF_DTR_NREGS]; 8764 key[0].dttk_size = 0; 8765 key[1].dttk_size = 0; 8766 nkeys = 2; 8767 scope = DIFV_SCOPE_THREAD; 8768 break; 8769 8770 case DIF_OP_STGAA: 8771 case DIF_OP_STTAA: 8772 nkeys = ttop; 8773 8774 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 8775 key[nkeys++].dttk_size = 0; 8776 8777 key[nkeys++].dttk_size = 0; 8778 8779 if (op == DIF_OP_STTAA) { 8780 scope = DIFV_SCOPE_THREAD; 8781 } else { 8782 scope = DIFV_SCOPE_GLOBAL; 8783 } 8784 8785 break; 8786 8787 case DIF_OP_PUSHTR: 8788 if (ttop == DIF_DTR_NREGS) 8789 return; 8790 8791 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 8792 /* 8793 * If the register for the size of the "pushtr" 8794 * is %r0 (or the value is 0) and the type is 8795 * a string, we'll use the system-wide default 8796 * string size. 8797 */ 8798 tupregs[ttop++].dttk_size = 8799 dtrace_strsize_default; 8800 } else { 8801 if (srd == 0) 8802 return; 8803 8804 tupregs[ttop++].dttk_size = sval; 8805 } 8806 8807 break; 8808 8809 case DIF_OP_PUSHTV: 8810 if (ttop == DIF_DTR_NREGS) 8811 return; 8812 8813 tupregs[ttop++].dttk_size = 0; 8814 break; 8815 8816 case DIF_OP_FLUSHTS: 8817 ttop = 0; 8818 break; 8819 8820 case DIF_OP_POPTS: 8821 if (ttop != 0) 8822 ttop--; 8823 break; 8824 } 8825 8826 sval = 0; 8827 srd = 0; 8828 8829 if (nkeys == 0) 8830 continue; 8831 8832 /* 8833 * We have a dynamic variable allocation; calculate its size. 8834 */ 8835 for (ksize = 0, i = 0; i < nkeys; i++) 8836 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 8837 8838 size = sizeof (dtrace_dynvar_t); 8839 size += sizeof (dtrace_key_t) * (nkeys - 1); 8840 size += ksize; 8841 8842 /* 8843 * Now we need to determine the size of the stored data. 8844 */ 8845 id = DIF_INSTR_VAR(instr); 8846 8847 for (i = 0; i < dp->dtdo_varlen; i++) { 8848 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8849 8850 if (v->dtdv_id == id && v->dtdv_scope == scope) { 8851 size += v->dtdv_type.dtdt_size; 8852 break; 8853 } 8854 } 8855 8856 if (i == dp->dtdo_varlen) 8857 return; 8858 8859 /* 8860 * We have the size. If this is larger than the chunk size 8861 * for our dynamic variable state, reset the chunk size. 8862 */ 8863 size = P2ROUNDUP(size, sizeof (uint64_t)); 8864 8865 if (size > vstate->dtvs_dynvars.dtds_chunksize) 8866 vstate->dtvs_dynvars.dtds_chunksize = size; 8867 } 8868 } 8869 8870 static void 8871 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8872 { 8873 int i, oldsvars, osz, nsz, otlocals, ntlocals; 8874 uint_t id; 8875 8876 ASSERT(MUTEX_HELD(&dtrace_lock)); 8877 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 8878 8879 for (i = 0; i < dp->dtdo_varlen; i++) { 8880 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8881 dtrace_statvar_t *svar, ***svarp; 8882 size_t dsize = 0; 8883 uint8_t scope = v->dtdv_scope; 8884 int *np; 8885 8886 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 8887 continue; 8888 8889 id -= DIF_VAR_OTHER_UBASE; 8890 8891 switch (scope) { 8892 case DIFV_SCOPE_THREAD: 8893 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 8894 dtrace_difv_t *tlocals; 8895 8896 if ((ntlocals = (otlocals << 1)) == 0) 8897 ntlocals = 1; 8898 8899 osz = otlocals * sizeof (dtrace_difv_t); 8900 nsz = ntlocals * sizeof (dtrace_difv_t); 8901 8902 tlocals = kmem_zalloc(nsz, KM_SLEEP); 8903 8904 if (osz != 0) { 8905 bcopy(vstate->dtvs_tlocals, 8906 tlocals, osz); 8907 kmem_free(vstate->dtvs_tlocals, osz); 8908 } 8909 8910 vstate->dtvs_tlocals = tlocals; 8911 vstate->dtvs_ntlocals = ntlocals; 8912 } 8913 8914 vstate->dtvs_tlocals[id] = *v; 8915 continue; 8916 8917 case DIFV_SCOPE_LOCAL: 8918 np = &vstate->dtvs_nlocals; 8919 svarp = &vstate->dtvs_locals; 8920 8921 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 8922 dsize = NCPU * (v->dtdv_type.dtdt_size + 8923 sizeof (uint64_t)); 8924 else 8925 dsize = NCPU * sizeof (uint64_t); 8926 8927 break; 8928 8929 case DIFV_SCOPE_GLOBAL: 8930 np = &vstate->dtvs_nglobals; 8931 svarp = &vstate->dtvs_globals; 8932 8933 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 8934 dsize = v->dtdv_type.dtdt_size + 8935 sizeof (uint64_t); 8936 8937 break; 8938 8939 default: 8940 ASSERT(0); 8941 } 8942 8943 while (id >= (oldsvars = *np)) { 8944 dtrace_statvar_t **statics; 8945 int newsvars, oldsize, newsize; 8946 8947 if ((newsvars = (oldsvars << 1)) == 0) 8948 newsvars = 1; 8949 8950 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 8951 newsize = newsvars * sizeof (dtrace_statvar_t *); 8952 8953 statics = kmem_zalloc(newsize, KM_SLEEP); 8954 8955 if (oldsize != 0) { 8956 bcopy(*svarp, statics, oldsize); 8957 kmem_free(*svarp, oldsize); 8958 } 8959 8960 *svarp = statics; 8961 *np = newsvars; 8962 } 8963 8964 if ((svar = (*svarp)[id]) == NULL) { 8965 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 8966 svar->dtsv_var = *v; 8967 8968 if ((svar->dtsv_size = dsize) != 0) { 8969 svar->dtsv_data = (uint64_t)(uintptr_t) 8970 kmem_zalloc(dsize, KM_SLEEP); 8971 } 8972 8973 (*svarp)[id] = svar; 8974 } 8975 8976 svar->dtsv_refcnt++; 8977 } 8978 8979 dtrace_difo_chunksize(dp, vstate); 8980 dtrace_difo_hold(dp); 8981 } 8982 8983 static dtrace_difo_t * 8984 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8985 { 8986 dtrace_difo_t *new; 8987 size_t sz; 8988 8989 ASSERT(dp->dtdo_buf != NULL); 8990 ASSERT(dp->dtdo_refcnt != 0); 8991 8992 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 8993 8994 ASSERT(dp->dtdo_buf != NULL); 8995 sz = dp->dtdo_len * sizeof (dif_instr_t); 8996 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 8997 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 8998 new->dtdo_len = dp->dtdo_len; 8999 9000 if (dp->dtdo_strtab != NULL) { 9001 ASSERT(dp->dtdo_strlen != 0); 9002 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 9003 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 9004 new->dtdo_strlen = dp->dtdo_strlen; 9005 } 9006 9007 if (dp->dtdo_inttab != NULL) { 9008 ASSERT(dp->dtdo_intlen != 0); 9009 sz = dp->dtdo_intlen * sizeof (uint64_t); 9010 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 9011 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 9012 new->dtdo_intlen = dp->dtdo_intlen; 9013 } 9014 9015 if (dp->dtdo_vartab != NULL) { 9016 ASSERT(dp->dtdo_varlen != 0); 9017 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 9018 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 9019 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 9020 new->dtdo_varlen = dp->dtdo_varlen; 9021 } 9022 9023 dtrace_difo_init(new, vstate); 9024 return (new); 9025 } 9026 9027 static void 9028 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9029 { 9030 int i; 9031 9032 ASSERT(dp->dtdo_refcnt == 0); 9033 9034 for (i = 0; i < dp->dtdo_varlen; i++) { 9035 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9036 dtrace_statvar_t *svar, **svarp; 9037 uint_t id; 9038 uint8_t scope = v->dtdv_scope; 9039 int *np; 9040 9041 switch (scope) { 9042 case DIFV_SCOPE_THREAD: 9043 continue; 9044 9045 case DIFV_SCOPE_LOCAL: 9046 np = &vstate->dtvs_nlocals; 9047 svarp = vstate->dtvs_locals; 9048 break; 9049 9050 case DIFV_SCOPE_GLOBAL: 9051 np = &vstate->dtvs_nglobals; 9052 svarp = vstate->dtvs_globals; 9053 break; 9054 9055 default: 9056 ASSERT(0); 9057 } 9058 9059 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9060 continue; 9061 9062 id -= DIF_VAR_OTHER_UBASE; 9063 ASSERT(id < *np); 9064 9065 svar = svarp[id]; 9066 ASSERT(svar != NULL); 9067 ASSERT(svar->dtsv_refcnt > 0); 9068 9069 if (--svar->dtsv_refcnt > 0) 9070 continue; 9071 9072 if (svar->dtsv_size != 0) { 9073 ASSERT(svar->dtsv_data != NULL); 9074 kmem_free((void *)(uintptr_t)svar->dtsv_data, 9075 svar->dtsv_size); 9076 } 9077 9078 kmem_free(svar, sizeof (dtrace_statvar_t)); 9079 svarp[id] = NULL; 9080 } 9081 9082 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 9083 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 9084 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 9085 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 9086 9087 kmem_free(dp, sizeof (dtrace_difo_t)); 9088 } 9089 9090 static void 9091 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9092 { 9093 int i; 9094 9095 ASSERT(MUTEX_HELD(&dtrace_lock)); 9096 ASSERT(dp->dtdo_refcnt != 0); 9097 9098 for (i = 0; i < dp->dtdo_varlen; i++) { 9099 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9100 9101 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9102 continue; 9103 9104 ASSERT(dtrace_vtime_references > 0); 9105 if (--dtrace_vtime_references == 0) 9106 dtrace_vtime_disable(); 9107 } 9108 9109 if (--dp->dtdo_refcnt == 0) 9110 dtrace_difo_destroy(dp, vstate); 9111 } 9112 9113 /* 9114 * DTrace Format Functions 9115 */ 9116 static uint16_t 9117 dtrace_format_add(dtrace_state_t *state, char *str) 9118 { 9119 char *fmt, **new; 9120 uint16_t ndx, len = strlen(str) + 1; 9121 9122 fmt = kmem_zalloc(len, KM_SLEEP); 9123 bcopy(str, fmt, len); 9124 9125 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 9126 if (state->dts_formats[ndx] == NULL) { 9127 state->dts_formats[ndx] = fmt; 9128 return (ndx + 1); 9129 } 9130 } 9131 9132 if (state->dts_nformats == USHRT_MAX) { 9133 /* 9134 * This is only likely if a denial-of-service attack is being 9135 * attempted. As such, it's okay to fail silently here. 9136 */ 9137 kmem_free(fmt, len); 9138 return (0); 9139 } 9140 9141 /* 9142 * For simplicity, we always resize the formats array to be exactly the 9143 * number of formats. 9144 */ 9145 ndx = state->dts_nformats++; 9146 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 9147 9148 if (state->dts_formats != NULL) { 9149 ASSERT(ndx != 0); 9150 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 9151 kmem_free(state->dts_formats, ndx * sizeof (char *)); 9152 } 9153 9154 state->dts_formats = new; 9155 state->dts_formats[ndx] = fmt; 9156 9157 return (ndx + 1); 9158 } 9159 9160 static void 9161 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 9162 { 9163 char *fmt; 9164 9165 ASSERT(state->dts_formats != NULL); 9166 ASSERT(format <= state->dts_nformats); 9167 ASSERT(state->dts_formats[format - 1] != NULL); 9168 9169 fmt = state->dts_formats[format - 1]; 9170 kmem_free(fmt, strlen(fmt) + 1); 9171 state->dts_formats[format - 1] = NULL; 9172 } 9173 9174 static void 9175 dtrace_format_destroy(dtrace_state_t *state) 9176 { 9177 int i; 9178 9179 if (state->dts_nformats == 0) { 9180 ASSERT(state->dts_formats == NULL); 9181 return; 9182 } 9183 9184 ASSERT(state->dts_formats != NULL); 9185 9186 for (i = 0; i < state->dts_nformats; i++) { 9187 char *fmt = state->dts_formats[i]; 9188 9189 if (fmt == NULL) 9190 continue; 9191 9192 kmem_free(fmt, strlen(fmt) + 1); 9193 } 9194 9195 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 9196 state->dts_nformats = 0; 9197 state->dts_formats = NULL; 9198 } 9199 9200 /* 9201 * DTrace Predicate Functions 9202 */ 9203 static dtrace_predicate_t * 9204 dtrace_predicate_create(dtrace_difo_t *dp) 9205 { 9206 dtrace_predicate_t *pred; 9207 9208 ASSERT(MUTEX_HELD(&dtrace_lock)); 9209 ASSERT(dp->dtdo_refcnt != 0); 9210 9211 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 9212 pred->dtp_difo = dp; 9213 pred->dtp_refcnt = 1; 9214 9215 if (!dtrace_difo_cacheable(dp)) 9216 return (pred); 9217 9218 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 9219 /* 9220 * This is only theoretically possible -- we have had 2^32 9221 * cacheable predicates on this machine. We cannot allow any 9222 * more predicates to become cacheable: as unlikely as it is, 9223 * there may be a thread caching a (now stale) predicate cache 9224 * ID. (N.B.: the temptation is being successfully resisted to 9225 * have this cmn_err() "Holy shit -- we executed this code!") 9226 */ 9227 return (pred); 9228 } 9229 9230 pred->dtp_cacheid = dtrace_predcache_id++; 9231 9232 return (pred); 9233 } 9234 9235 static void 9236 dtrace_predicate_hold(dtrace_predicate_t *pred) 9237 { 9238 ASSERT(MUTEX_HELD(&dtrace_lock)); 9239 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 9240 ASSERT(pred->dtp_refcnt > 0); 9241 9242 pred->dtp_refcnt++; 9243 } 9244 9245 static void 9246 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 9247 { 9248 dtrace_difo_t *dp = pred->dtp_difo; 9249 9250 ASSERT(MUTEX_HELD(&dtrace_lock)); 9251 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 9252 ASSERT(pred->dtp_refcnt > 0); 9253 9254 if (--pred->dtp_refcnt == 0) { 9255 dtrace_difo_release(pred->dtp_difo, vstate); 9256 kmem_free(pred, sizeof (dtrace_predicate_t)); 9257 } 9258 } 9259 9260 /* 9261 * DTrace Action Description Functions 9262 */ 9263 static dtrace_actdesc_t * 9264 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 9265 uint64_t uarg, uint64_t arg) 9266 { 9267 dtrace_actdesc_t *act; 9268 9269 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 9270 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 9271 9272 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 9273 act->dtad_kind = kind; 9274 act->dtad_ntuple = ntuple; 9275 act->dtad_uarg = uarg; 9276 act->dtad_arg = arg; 9277 act->dtad_refcnt = 1; 9278 9279 return (act); 9280 } 9281 9282 static void 9283 dtrace_actdesc_hold(dtrace_actdesc_t *act) 9284 { 9285 ASSERT(act->dtad_refcnt >= 1); 9286 act->dtad_refcnt++; 9287 } 9288 9289 static void 9290 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 9291 { 9292 dtrace_actkind_t kind = act->dtad_kind; 9293 dtrace_difo_t *dp; 9294 9295 ASSERT(act->dtad_refcnt >= 1); 9296 9297 if (--act->dtad_refcnt != 0) 9298 return; 9299 9300 if ((dp = act->dtad_difo) != NULL) 9301 dtrace_difo_release(dp, vstate); 9302 9303 if (DTRACEACT_ISPRINTFLIKE(kind)) { 9304 char *str = (char *)(uintptr_t)act->dtad_arg; 9305 9306 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 9307 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 9308 9309 if (str != NULL) 9310 kmem_free(str, strlen(str) + 1); 9311 } 9312 9313 kmem_free(act, sizeof (dtrace_actdesc_t)); 9314 } 9315 9316 /* 9317 * DTrace ECB Functions 9318 */ 9319 static dtrace_ecb_t * 9320 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 9321 { 9322 dtrace_ecb_t *ecb; 9323 dtrace_epid_t epid; 9324 9325 ASSERT(MUTEX_HELD(&dtrace_lock)); 9326 9327 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 9328 ecb->dte_predicate = NULL; 9329 ecb->dte_probe = probe; 9330 9331 /* 9332 * The default size is the size of the default action: recording 9333 * the epid. 9334 */ 9335 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9336 ecb->dte_alignment = sizeof (dtrace_epid_t); 9337 9338 epid = state->dts_epid++; 9339 9340 if (epid - 1 >= state->dts_necbs) { 9341 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 9342 int necbs = state->dts_necbs << 1; 9343 9344 ASSERT(epid == state->dts_necbs + 1); 9345 9346 if (necbs == 0) { 9347 ASSERT(oecbs == NULL); 9348 necbs = 1; 9349 } 9350 9351 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 9352 9353 if (oecbs != NULL) 9354 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 9355 9356 dtrace_membar_producer(); 9357 state->dts_ecbs = ecbs; 9358 9359 if (oecbs != NULL) { 9360 /* 9361 * If this state is active, we must dtrace_sync() 9362 * before we can free the old dts_ecbs array: we're 9363 * coming in hot, and there may be active ring 9364 * buffer processing (which indexes into the dts_ecbs 9365 * array) on another CPU. 9366 */ 9367 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 9368 dtrace_sync(); 9369 9370 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 9371 } 9372 9373 dtrace_membar_producer(); 9374 state->dts_necbs = necbs; 9375 } 9376 9377 ecb->dte_state = state; 9378 9379 ASSERT(state->dts_ecbs[epid - 1] == NULL); 9380 dtrace_membar_producer(); 9381 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 9382 9383 return (ecb); 9384 } 9385 9386 static int 9387 dtrace_ecb_enable(dtrace_ecb_t *ecb) 9388 { 9389 dtrace_probe_t *probe = ecb->dte_probe; 9390 9391 ASSERT(MUTEX_HELD(&cpu_lock)); 9392 ASSERT(MUTEX_HELD(&dtrace_lock)); 9393 ASSERT(ecb->dte_next == NULL); 9394 9395 if (probe == NULL) { 9396 /* 9397 * This is the NULL probe -- there's nothing to do. 9398 */ 9399 return (0); 9400 } 9401 9402 if (probe->dtpr_ecb == NULL) { 9403 dtrace_provider_t *prov = probe->dtpr_provider; 9404 9405 /* 9406 * We're the first ECB on this probe. 9407 */ 9408 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 9409 9410 if (ecb->dte_predicate != NULL) 9411 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 9412 9413 return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 9414 probe->dtpr_id, probe->dtpr_arg)); 9415 } else { 9416 /* 9417 * This probe is already active. Swing the last pointer to 9418 * point to the new ECB, and issue a dtrace_sync() to assure 9419 * that all CPUs have seen the change. 9420 */ 9421 ASSERT(probe->dtpr_ecb_last != NULL); 9422 probe->dtpr_ecb_last->dte_next = ecb; 9423 probe->dtpr_ecb_last = ecb; 9424 probe->dtpr_predcache = 0; 9425 9426 dtrace_sync(); 9427 return (0); 9428 } 9429 } 9430 9431 static void 9432 dtrace_ecb_resize(dtrace_ecb_t *ecb) 9433 { 9434 uint32_t maxalign = sizeof (dtrace_epid_t); 9435 uint32_t align = sizeof (uint8_t), offs, diff; 9436 dtrace_action_t *act; 9437 int wastuple = 0; 9438 uint32_t aggbase = UINT32_MAX; 9439 dtrace_state_t *state = ecb->dte_state; 9440 9441 /* 9442 * If we record anything, we always record the epid. (And we always 9443 * record it first.) 9444 */ 9445 offs = sizeof (dtrace_epid_t); 9446 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9447 9448 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9449 dtrace_recdesc_t *rec = &act->dta_rec; 9450 9451 if ((align = rec->dtrd_alignment) > maxalign) 9452 maxalign = align; 9453 9454 if (!wastuple && act->dta_intuple) { 9455 /* 9456 * This is the first record in a tuple. Align the 9457 * offset to be at offset 4 in an 8-byte aligned 9458 * block. 9459 */ 9460 diff = offs + sizeof (dtrace_aggid_t); 9461 9462 if (diff = (diff & (sizeof (uint64_t) - 1))) 9463 offs += sizeof (uint64_t) - diff; 9464 9465 aggbase = offs - sizeof (dtrace_aggid_t); 9466 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 9467 } 9468 9469 /*LINTED*/ 9470 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 9471 /* 9472 * The current offset is not properly aligned; align it. 9473 */ 9474 offs += align - diff; 9475 } 9476 9477 rec->dtrd_offset = offs; 9478 9479 if (offs + rec->dtrd_size > ecb->dte_needed) { 9480 ecb->dte_needed = offs + rec->dtrd_size; 9481 9482 if (ecb->dte_needed > state->dts_needed) 9483 state->dts_needed = ecb->dte_needed; 9484 } 9485 9486 if (DTRACEACT_ISAGG(act->dta_kind)) { 9487 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9488 dtrace_action_t *first = agg->dtag_first, *prev; 9489 9490 ASSERT(rec->dtrd_size != 0 && first != NULL); 9491 ASSERT(wastuple); 9492 ASSERT(aggbase != UINT32_MAX); 9493 9494 agg->dtag_base = aggbase; 9495 9496 while ((prev = first->dta_prev) != NULL && 9497 DTRACEACT_ISAGG(prev->dta_kind)) { 9498 agg = (dtrace_aggregation_t *)prev; 9499 first = agg->dtag_first; 9500 } 9501 9502 if (prev != NULL) { 9503 offs = prev->dta_rec.dtrd_offset + 9504 prev->dta_rec.dtrd_size; 9505 } else { 9506 offs = sizeof (dtrace_epid_t); 9507 } 9508 wastuple = 0; 9509 } else { 9510 if (!act->dta_intuple) 9511 ecb->dte_size = offs + rec->dtrd_size; 9512 9513 offs += rec->dtrd_size; 9514 } 9515 9516 wastuple = act->dta_intuple; 9517 } 9518 9519 if ((act = ecb->dte_action) != NULL && 9520 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 9521 ecb->dte_size == sizeof (dtrace_epid_t)) { 9522 /* 9523 * If the size is still sizeof (dtrace_epid_t), then all 9524 * actions store no data; set the size to 0. 9525 */ 9526 ecb->dte_alignment = maxalign; 9527 ecb->dte_size = 0; 9528 9529 /* 9530 * If the needed space is still sizeof (dtrace_epid_t), then 9531 * all actions need no additional space; set the needed 9532 * size to 0. 9533 */ 9534 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 9535 ecb->dte_needed = 0; 9536 9537 return; 9538 } 9539 9540 /* 9541 * Set our alignment, and make sure that the dte_size and dte_needed 9542 * are aligned to the size of an EPID. 9543 */ 9544 ecb->dte_alignment = maxalign; 9545 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 9546 ~(sizeof (dtrace_epid_t) - 1); 9547 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 9548 ~(sizeof (dtrace_epid_t) - 1); 9549 ASSERT(ecb->dte_size <= ecb->dte_needed); 9550 } 9551 9552 static dtrace_action_t * 9553 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9554 { 9555 dtrace_aggregation_t *agg; 9556 size_t size = sizeof (uint64_t); 9557 int ntuple = desc->dtad_ntuple; 9558 dtrace_action_t *act; 9559 dtrace_recdesc_t *frec; 9560 dtrace_aggid_t aggid; 9561 dtrace_state_t *state = ecb->dte_state; 9562 9563 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 9564 agg->dtag_ecb = ecb; 9565 9566 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 9567 9568 switch (desc->dtad_kind) { 9569 case DTRACEAGG_MIN: 9570 agg->dtag_initial = INT64_MAX; 9571 agg->dtag_aggregate = dtrace_aggregate_min; 9572 break; 9573 9574 case DTRACEAGG_MAX: 9575 agg->dtag_initial = INT64_MIN; 9576 agg->dtag_aggregate = dtrace_aggregate_max; 9577 break; 9578 9579 case DTRACEAGG_COUNT: 9580 agg->dtag_aggregate = dtrace_aggregate_count; 9581 break; 9582 9583 case DTRACEAGG_QUANTIZE: 9584 agg->dtag_aggregate = dtrace_aggregate_quantize; 9585 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 9586 sizeof (uint64_t); 9587 break; 9588 9589 case DTRACEAGG_LQUANTIZE: { 9590 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 9591 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 9592 9593 agg->dtag_initial = desc->dtad_arg; 9594 agg->dtag_aggregate = dtrace_aggregate_lquantize; 9595 9596 if (step == 0 || levels == 0) 9597 goto err; 9598 9599 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 9600 break; 9601 } 9602 9603 case DTRACEAGG_LLQUANTIZE: { 9604 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 9605 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 9606 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 9607 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 9608 int64_t v; 9609 9610 agg->dtag_initial = desc->dtad_arg; 9611 agg->dtag_aggregate = dtrace_aggregate_llquantize; 9612 9613 if (factor < 2 || low >= high || nsteps < factor) 9614 goto err; 9615 9616 /* 9617 * Now check that the number of steps evenly divides a power 9618 * of the factor. (This assures both integer bucket size and 9619 * linearity within each magnitude.) 9620 */ 9621 for (v = factor; v < nsteps; v *= factor) 9622 continue; 9623 9624 if ((v % nsteps) || (nsteps % factor)) 9625 goto err; 9626 9627 size = (dtrace_aggregate_llquantize_bucket(factor, 9628 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 9629 break; 9630 } 9631 9632 case DTRACEAGG_AVG: 9633 agg->dtag_aggregate = dtrace_aggregate_avg; 9634 size = sizeof (uint64_t) * 2; 9635 break; 9636 9637 case DTRACEAGG_STDDEV: 9638 agg->dtag_aggregate = dtrace_aggregate_stddev; 9639 size = sizeof (uint64_t) * 4; 9640 break; 9641 9642 case DTRACEAGG_SUM: 9643 agg->dtag_aggregate = dtrace_aggregate_sum; 9644 break; 9645 9646 default: 9647 goto err; 9648 } 9649 9650 agg->dtag_action.dta_rec.dtrd_size = size; 9651 9652 if (ntuple == 0) 9653 goto err; 9654 9655 /* 9656 * We must make sure that we have enough actions for the n-tuple. 9657 */ 9658 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 9659 if (DTRACEACT_ISAGG(act->dta_kind)) 9660 break; 9661 9662 if (--ntuple == 0) { 9663 /* 9664 * This is the action with which our n-tuple begins. 9665 */ 9666 agg->dtag_first = act; 9667 goto success; 9668 } 9669 } 9670 9671 /* 9672 * This n-tuple is short by ntuple elements. Return failure. 9673 */ 9674 ASSERT(ntuple != 0); 9675 err: 9676 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9677 return (NULL); 9678 9679 success: 9680 /* 9681 * If the last action in the tuple has a size of zero, it's actually 9682 * an expression argument for the aggregating action. 9683 */ 9684 ASSERT(ecb->dte_action_last != NULL); 9685 act = ecb->dte_action_last; 9686 9687 if (act->dta_kind == DTRACEACT_DIFEXPR) { 9688 ASSERT(act->dta_difo != NULL); 9689 9690 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 9691 agg->dtag_hasarg = 1; 9692 } 9693 9694 /* 9695 * We need to allocate an id for this aggregation. 9696 */ 9697 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 9698 VM_BESTFIT | VM_SLEEP); 9699 9700 if (aggid - 1 >= state->dts_naggregations) { 9701 dtrace_aggregation_t **oaggs = state->dts_aggregations; 9702 dtrace_aggregation_t **aggs; 9703 int naggs = state->dts_naggregations << 1; 9704 int onaggs = state->dts_naggregations; 9705 9706 ASSERT(aggid == state->dts_naggregations + 1); 9707 9708 if (naggs == 0) { 9709 ASSERT(oaggs == NULL); 9710 naggs = 1; 9711 } 9712 9713 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 9714 9715 if (oaggs != NULL) { 9716 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 9717 kmem_free(oaggs, onaggs * sizeof (*aggs)); 9718 } 9719 9720 state->dts_aggregations = aggs; 9721 state->dts_naggregations = naggs; 9722 } 9723 9724 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 9725 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 9726 9727 frec = &agg->dtag_first->dta_rec; 9728 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 9729 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 9730 9731 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 9732 ASSERT(!act->dta_intuple); 9733 act->dta_intuple = 1; 9734 } 9735 9736 return (&agg->dtag_action); 9737 } 9738 9739 static void 9740 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 9741 { 9742 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9743 dtrace_state_t *state = ecb->dte_state; 9744 dtrace_aggid_t aggid = agg->dtag_id; 9745 9746 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 9747 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 9748 9749 ASSERT(state->dts_aggregations[aggid - 1] == agg); 9750 state->dts_aggregations[aggid - 1] = NULL; 9751 9752 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9753 } 9754 9755 static int 9756 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9757 { 9758 dtrace_action_t *action, *last; 9759 dtrace_difo_t *dp = desc->dtad_difo; 9760 uint32_t size = 0, align = sizeof (uint8_t), mask; 9761 uint16_t format = 0; 9762 dtrace_recdesc_t *rec; 9763 dtrace_state_t *state = ecb->dte_state; 9764 dtrace_optval_t *opt = state->dts_options, nframes, strsize; 9765 uint64_t arg = desc->dtad_arg; 9766 9767 ASSERT(MUTEX_HELD(&dtrace_lock)); 9768 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 9769 9770 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 9771 /* 9772 * If this is an aggregating action, there must be neither 9773 * a speculate nor a commit on the action chain. 9774 */ 9775 dtrace_action_t *act; 9776 9777 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9778 if (act->dta_kind == DTRACEACT_COMMIT) 9779 return (EINVAL); 9780 9781 if (act->dta_kind == DTRACEACT_SPECULATE) 9782 return (EINVAL); 9783 } 9784 9785 action = dtrace_ecb_aggregation_create(ecb, desc); 9786 9787 if (action == NULL) 9788 return (EINVAL); 9789 } else { 9790 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 9791 (desc->dtad_kind == DTRACEACT_DIFEXPR && 9792 dp != NULL && dp->dtdo_destructive)) { 9793 state->dts_destructive = 1; 9794 } 9795 9796 switch (desc->dtad_kind) { 9797 case DTRACEACT_PRINTF: 9798 case DTRACEACT_PRINTA: 9799 case DTRACEACT_SYSTEM: 9800 case DTRACEACT_FREOPEN: 9801 /* 9802 * We know that our arg is a string -- turn it into a 9803 * format. 9804 */ 9805 if (arg == NULL) { 9806 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); 9807 format = 0; 9808 } else { 9809 ASSERT(arg != NULL); 9810 ASSERT(arg > KERNELBASE); 9811 format = dtrace_format_add(state, 9812 (char *)(uintptr_t)arg); 9813 } 9814 9815 /*FALLTHROUGH*/ 9816 case DTRACEACT_LIBACT: 9817 case DTRACEACT_DIFEXPR: 9818 case DTRACEACT_TRACEMEM: 9819 case DTRACEACT_TRACEMEM_DYNSIZE: 9820 if (dp == NULL) 9821 return (EINVAL); 9822 9823 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 9824 break; 9825 9826 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 9827 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9828 return (EINVAL); 9829 9830 size = opt[DTRACEOPT_STRSIZE]; 9831 } 9832 9833 break; 9834 9835 case DTRACEACT_STACK: 9836 if ((nframes = arg) == 0) { 9837 nframes = opt[DTRACEOPT_STACKFRAMES]; 9838 ASSERT(nframes > 0); 9839 arg = nframes; 9840 } 9841 9842 size = nframes * sizeof (pc_t); 9843 break; 9844 9845 case DTRACEACT_JSTACK: 9846 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 9847 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 9848 9849 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 9850 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 9851 9852 arg = DTRACE_USTACK_ARG(nframes, strsize); 9853 9854 /*FALLTHROUGH*/ 9855 case DTRACEACT_USTACK: 9856 if (desc->dtad_kind != DTRACEACT_JSTACK && 9857 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 9858 strsize = DTRACE_USTACK_STRSIZE(arg); 9859 nframes = opt[DTRACEOPT_USTACKFRAMES]; 9860 ASSERT(nframes > 0); 9861 arg = DTRACE_USTACK_ARG(nframes, strsize); 9862 } 9863 9864 /* 9865 * Save a slot for the pid. 9866 */ 9867 size = (nframes + 1) * sizeof (uint64_t); 9868 size += DTRACE_USTACK_STRSIZE(arg); 9869 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 9870 9871 break; 9872 9873 case DTRACEACT_SYM: 9874 case DTRACEACT_MOD: 9875 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 9876 sizeof (uint64_t)) || 9877 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9878 return (EINVAL); 9879 break; 9880 9881 case DTRACEACT_USYM: 9882 case DTRACEACT_UMOD: 9883 case DTRACEACT_UADDR: 9884 if (dp == NULL || 9885 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 9886 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9887 return (EINVAL); 9888 9889 /* 9890 * We have a slot for the pid, plus a slot for the 9891 * argument. To keep things simple (aligned with 9892 * bitness-neutral sizing), we store each as a 64-bit 9893 * quantity. 9894 */ 9895 size = 2 * sizeof (uint64_t); 9896 break; 9897 9898 case DTRACEACT_STOP: 9899 case DTRACEACT_BREAKPOINT: 9900 case DTRACEACT_PANIC: 9901 break; 9902 9903 case DTRACEACT_CHILL: 9904 case DTRACEACT_DISCARD: 9905 case DTRACEACT_RAISE: 9906 if (dp == NULL) 9907 return (EINVAL); 9908 break; 9909 9910 case DTRACEACT_EXIT: 9911 if (dp == NULL || 9912 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 9913 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9914 return (EINVAL); 9915 break; 9916 9917 case DTRACEACT_SPECULATE: 9918 if (ecb->dte_size > sizeof (dtrace_epid_t)) 9919 return (EINVAL); 9920 9921 if (dp == NULL) 9922 return (EINVAL); 9923 9924 state->dts_speculates = 1; 9925 break; 9926 9927 case DTRACEACT_COMMIT: { 9928 dtrace_action_t *act = ecb->dte_action; 9929 9930 for (; act != NULL; act = act->dta_next) { 9931 if (act->dta_kind == DTRACEACT_COMMIT) 9932 return (EINVAL); 9933 } 9934 9935 if (dp == NULL) 9936 return (EINVAL); 9937 break; 9938 } 9939 9940 default: 9941 return (EINVAL); 9942 } 9943 9944 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 9945 /* 9946 * If this is a data-storing action or a speculate, 9947 * we must be sure that there isn't a commit on the 9948 * action chain. 9949 */ 9950 dtrace_action_t *act = ecb->dte_action; 9951 9952 for (; act != NULL; act = act->dta_next) { 9953 if (act->dta_kind == DTRACEACT_COMMIT) 9954 return (EINVAL); 9955 } 9956 } 9957 9958 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 9959 action->dta_rec.dtrd_size = size; 9960 } 9961 9962 action->dta_refcnt = 1; 9963 rec = &action->dta_rec; 9964 size = rec->dtrd_size; 9965 9966 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 9967 if (!(size & mask)) { 9968 align = mask + 1; 9969 break; 9970 } 9971 } 9972 9973 action->dta_kind = desc->dtad_kind; 9974 9975 if ((action->dta_difo = dp) != NULL) 9976 dtrace_difo_hold(dp); 9977 9978 rec->dtrd_action = action->dta_kind; 9979 rec->dtrd_arg = arg; 9980 rec->dtrd_uarg = desc->dtad_uarg; 9981 rec->dtrd_alignment = (uint16_t)align; 9982 rec->dtrd_format = format; 9983 9984 if ((last = ecb->dte_action_last) != NULL) { 9985 ASSERT(ecb->dte_action != NULL); 9986 action->dta_prev = last; 9987 last->dta_next = action; 9988 } else { 9989 ASSERT(ecb->dte_action == NULL); 9990 ecb->dte_action = action; 9991 } 9992 9993 ecb->dte_action_last = action; 9994 9995 return (0); 9996 } 9997 9998 static void 9999 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 10000 { 10001 dtrace_action_t *act = ecb->dte_action, *next; 10002 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 10003 dtrace_difo_t *dp; 10004 uint16_t format; 10005 10006 if (act != NULL && act->dta_refcnt > 1) { 10007 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 10008 act->dta_refcnt--; 10009 } else { 10010 for (; act != NULL; act = next) { 10011 next = act->dta_next; 10012 ASSERT(next != NULL || act == ecb->dte_action_last); 10013 ASSERT(act->dta_refcnt == 1); 10014 10015 if ((format = act->dta_rec.dtrd_format) != 0) 10016 dtrace_format_remove(ecb->dte_state, format); 10017 10018 if ((dp = act->dta_difo) != NULL) 10019 dtrace_difo_release(dp, vstate); 10020 10021 if (DTRACEACT_ISAGG(act->dta_kind)) { 10022 dtrace_ecb_aggregation_destroy(ecb, act); 10023 } else { 10024 kmem_free(act, sizeof (dtrace_action_t)); 10025 } 10026 } 10027 } 10028 10029 ecb->dte_action = NULL; 10030 ecb->dte_action_last = NULL; 10031 ecb->dte_size = sizeof (dtrace_epid_t); 10032 } 10033 10034 static void 10035 dtrace_ecb_disable(dtrace_ecb_t *ecb) 10036 { 10037 /* 10038 * We disable the ECB by removing it from its probe. 10039 */ 10040 dtrace_ecb_t *pecb, *prev = NULL; 10041 dtrace_probe_t *probe = ecb->dte_probe; 10042 10043 ASSERT(MUTEX_HELD(&dtrace_lock)); 10044 10045 if (probe == NULL) { 10046 /* 10047 * This is the NULL probe; there is nothing to disable. 10048 */ 10049 return; 10050 } 10051 10052 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 10053 if (pecb == ecb) 10054 break; 10055 prev = pecb; 10056 } 10057 10058 ASSERT(pecb != NULL); 10059 10060 if (prev == NULL) { 10061 probe->dtpr_ecb = ecb->dte_next; 10062 } else { 10063 prev->dte_next = ecb->dte_next; 10064 } 10065 10066 if (ecb == probe->dtpr_ecb_last) { 10067 ASSERT(ecb->dte_next == NULL); 10068 probe->dtpr_ecb_last = prev; 10069 } 10070 10071 /* 10072 * The ECB has been disconnected from the probe; now sync to assure 10073 * that all CPUs have seen the change before returning. 10074 */ 10075 dtrace_sync(); 10076 10077 if (probe->dtpr_ecb == NULL) { 10078 /* 10079 * That was the last ECB on the probe; clear the predicate 10080 * cache ID for the probe, disable it and sync one more time 10081 * to assure that we'll never hit it again. 10082 */ 10083 dtrace_provider_t *prov = probe->dtpr_provider; 10084 10085 ASSERT(ecb->dte_next == NULL); 10086 ASSERT(probe->dtpr_ecb_last == NULL); 10087 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 10088 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 10089 probe->dtpr_id, probe->dtpr_arg); 10090 dtrace_sync(); 10091 } else { 10092 /* 10093 * There is at least one ECB remaining on the probe. If there 10094 * is _exactly_ one, set the probe's predicate cache ID to be 10095 * the predicate cache ID of the remaining ECB. 10096 */ 10097 ASSERT(probe->dtpr_ecb_last != NULL); 10098 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 10099 10100 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 10101 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 10102 10103 ASSERT(probe->dtpr_ecb->dte_next == NULL); 10104 10105 if (p != NULL) 10106 probe->dtpr_predcache = p->dtp_cacheid; 10107 } 10108 10109 ecb->dte_next = NULL; 10110 } 10111 } 10112 10113 static void 10114 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 10115 { 10116 dtrace_state_t *state = ecb->dte_state; 10117 dtrace_vstate_t *vstate = &state->dts_vstate; 10118 dtrace_predicate_t *pred; 10119 dtrace_epid_t epid = ecb->dte_epid; 10120 10121 ASSERT(MUTEX_HELD(&dtrace_lock)); 10122 ASSERT(ecb->dte_next == NULL); 10123 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 10124 10125 if ((pred = ecb->dte_predicate) != NULL) 10126 dtrace_predicate_release(pred, vstate); 10127 10128 dtrace_ecb_action_remove(ecb); 10129 10130 ASSERT(state->dts_ecbs[epid - 1] == ecb); 10131 state->dts_ecbs[epid - 1] = NULL; 10132 10133 kmem_free(ecb, sizeof (dtrace_ecb_t)); 10134 } 10135 10136 static dtrace_ecb_t * 10137 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 10138 dtrace_enabling_t *enab) 10139 { 10140 dtrace_ecb_t *ecb; 10141 dtrace_predicate_t *pred; 10142 dtrace_actdesc_t *act; 10143 dtrace_provider_t *prov; 10144 dtrace_ecbdesc_t *desc = enab->dten_current; 10145 10146 ASSERT(MUTEX_HELD(&dtrace_lock)); 10147 ASSERT(state != NULL); 10148 10149 ecb = dtrace_ecb_add(state, probe); 10150 ecb->dte_uarg = desc->dted_uarg; 10151 10152 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 10153 dtrace_predicate_hold(pred); 10154 ecb->dte_predicate = pred; 10155 } 10156 10157 if (probe != NULL) { 10158 /* 10159 * If the provider shows more leg than the consumer is old 10160 * enough to see, we need to enable the appropriate implicit 10161 * predicate bits to prevent the ecb from activating at 10162 * revealing times. 10163 * 10164 * Providers specifying DTRACE_PRIV_USER at register time 10165 * are stating that they need the /proc-style privilege 10166 * model to be enforced, and this is what DTRACE_COND_OWNER 10167 * and DTRACE_COND_ZONEOWNER will then do at probe time. 10168 */ 10169 prov = probe->dtpr_provider; 10170 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 10171 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10172 ecb->dte_cond |= DTRACE_COND_OWNER; 10173 10174 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 10175 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10176 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 10177 10178 /* 10179 * If the provider shows us kernel innards and the user 10180 * is lacking sufficient privilege, enable the 10181 * DTRACE_COND_USERMODE implicit predicate. 10182 */ 10183 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 10184 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 10185 ecb->dte_cond |= DTRACE_COND_USERMODE; 10186 } 10187 10188 if (dtrace_ecb_create_cache != NULL) { 10189 /* 10190 * If we have a cached ecb, we'll use its action list instead 10191 * of creating our own (saving both time and space). 10192 */ 10193 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 10194 dtrace_action_t *act = cached->dte_action; 10195 10196 if (act != NULL) { 10197 ASSERT(act->dta_refcnt > 0); 10198 act->dta_refcnt++; 10199 ecb->dte_action = act; 10200 ecb->dte_action_last = cached->dte_action_last; 10201 ecb->dte_needed = cached->dte_needed; 10202 ecb->dte_size = cached->dte_size; 10203 ecb->dte_alignment = cached->dte_alignment; 10204 } 10205 10206 return (ecb); 10207 } 10208 10209 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 10210 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 10211 dtrace_ecb_destroy(ecb); 10212 return (NULL); 10213 } 10214 } 10215 10216 dtrace_ecb_resize(ecb); 10217 10218 return (dtrace_ecb_create_cache = ecb); 10219 } 10220 10221 static int 10222 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 10223 { 10224 dtrace_ecb_t *ecb; 10225 dtrace_enabling_t *enab = arg; 10226 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 10227 10228 ASSERT(state != NULL); 10229 10230 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 10231 /* 10232 * This probe was created in a generation for which this 10233 * enabling has previously created ECBs; we don't want to 10234 * enable it again, so just kick out. 10235 */ 10236 return (DTRACE_MATCH_NEXT); 10237 } 10238 10239 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 10240 return (DTRACE_MATCH_DONE); 10241 10242 if (dtrace_ecb_enable(ecb) < 0) 10243 return (DTRACE_MATCH_FAIL); 10244 10245 return (DTRACE_MATCH_NEXT); 10246 } 10247 10248 static dtrace_ecb_t * 10249 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 10250 { 10251 dtrace_ecb_t *ecb; 10252 10253 ASSERT(MUTEX_HELD(&dtrace_lock)); 10254 10255 if (id == 0 || id > state->dts_necbs) 10256 return (NULL); 10257 10258 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 10259 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 10260 10261 return (state->dts_ecbs[id - 1]); 10262 } 10263 10264 static dtrace_aggregation_t * 10265 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 10266 { 10267 dtrace_aggregation_t *agg; 10268 10269 ASSERT(MUTEX_HELD(&dtrace_lock)); 10270 10271 if (id == 0 || id > state->dts_naggregations) 10272 return (NULL); 10273 10274 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 10275 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 10276 agg->dtag_id == id); 10277 10278 return (state->dts_aggregations[id - 1]); 10279 } 10280 10281 /* 10282 * DTrace Buffer Functions 10283 * 10284 * The following functions manipulate DTrace buffers. Most of these functions 10285 * are called in the context of establishing or processing consumer state; 10286 * exceptions are explicitly noted. 10287 */ 10288 10289 /* 10290 * Note: called from cross call context. This function switches the two 10291 * buffers on a given CPU. The atomicity of this operation is assured by 10292 * disabling interrupts while the actual switch takes place; the disabling of 10293 * interrupts serializes the execution with any execution of dtrace_probe() on 10294 * the same CPU. 10295 */ 10296 static void 10297 dtrace_buffer_switch(dtrace_buffer_t *buf) 10298 { 10299 caddr_t tomax = buf->dtb_tomax; 10300 caddr_t xamot = buf->dtb_xamot; 10301 dtrace_icookie_t cookie; 10302 hrtime_t now = dtrace_gethrtime(); 10303 10304 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10305 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 10306 10307 cookie = dtrace_interrupt_disable(); 10308 buf->dtb_tomax = xamot; 10309 buf->dtb_xamot = tomax; 10310 buf->dtb_xamot_drops = buf->dtb_drops; 10311 buf->dtb_xamot_offset = buf->dtb_offset; 10312 buf->dtb_xamot_errors = buf->dtb_errors; 10313 buf->dtb_xamot_flags = buf->dtb_flags; 10314 buf->dtb_offset = 0; 10315 buf->dtb_drops = 0; 10316 buf->dtb_errors = 0; 10317 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 10318 buf->dtb_interval = now - buf->dtb_switched; 10319 buf->dtb_switched = now; 10320 dtrace_interrupt_enable(cookie); 10321 } 10322 10323 /* 10324 * Note: called from cross call context. This function activates a buffer 10325 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 10326 * is guaranteed by the disabling of interrupts. 10327 */ 10328 static void 10329 dtrace_buffer_activate(dtrace_state_t *state) 10330 { 10331 dtrace_buffer_t *buf; 10332 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 10333 10334 buf = &state->dts_buffer[CPU->cpu_id]; 10335 10336 if (buf->dtb_tomax != NULL) { 10337 /* 10338 * We might like to assert that the buffer is marked inactive, 10339 * but this isn't necessarily true: the buffer for the CPU 10340 * that processes the BEGIN probe has its buffer activated 10341 * manually. In this case, we take the (harmless) action 10342 * re-clearing the bit INACTIVE bit. 10343 */ 10344 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 10345 } 10346 10347 dtrace_interrupt_enable(cookie); 10348 } 10349 10350 static int 10351 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 10352 processorid_t cpu, int *factor) 10353 { 10354 cpu_t *cp; 10355 dtrace_buffer_t *buf; 10356 int allocated = 0, desired = 0; 10357 10358 ASSERT(MUTEX_HELD(&cpu_lock)); 10359 ASSERT(MUTEX_HELD(&dtrace_lock)); 10360 10361 *factor = 1; 10362 10363 if (size > dtrace_nonroot_maxsize && 10364 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 10365 return (EFBIG); 10366 10367 cp = cpu_list; 10368 10369 do { 10370 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10371 continue; 10372 10373 buf = &bufs[cp->cpu_id]; 10374 10375 /* 10376 * If there is already a buffer allocated for this CPU, it 10377 * is only possible that this is a DR event. In this case, 10378 * the buffer size must match our specified size. 10379 */ 10380 if (buf->dtb_tomax != NULL) { 10381 ASSERT(buf->dtb_size == size); 10382 continue; 10383 } 10384 10385 ASSERT(buf->dtb_xamot == NULL); 10386 10387 if ((buf->dtb_tomax = kmem_zalloc(size, 10388 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10389 goto err; 10390 10391 buf->dtb_size = size; 10392 buf->dtb_flags = flags; 10393 buf->dtb_offset = 0; 10394 buf->dtb_drops = 0; 10395 10396 if (flags & DTRACEBUF_NOSWITCH) 10397 continue; 10398 10399 if ((buf->dtb_xamot = kmem_zalloc(size, 10400 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10401 goto err; 10402 } while ((cp = cp->cpu_next) != cpu_list); 10403 10404 return (0); 10405 10406 err: 10407 cp = cpu_list; 10408 10409 do { 10410 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10411 continue; 10412 10413 buf = &bufs[cp->cpu_id]; 10414 desired += 2; 10415 10416 if (buf->dtb_xamot != NULL) { 10417 ASSERT(buf->dtb_tomax != NULL); 10418 ASSERT(buf->dtb_size == size); 10419 kmem_free(buf->dtb_xamot, size); 10420 allocated++; 10421 } 10422 10423 if (buf->dtb_tomax != NULL) { 10424 ASSERT(buf->dtb_size == size); 10425 kmem_free(buf->dtb_tomax, size); 10426 allocated++; 10427 } 10428 10429 buf->dtb_tomax = NULL; 10430 buf->dtb_xamot = NULL; 10431 buf->dtb_size = 0; 10432 } while ((cp = cp->cpu_next) != cpu_list); 10433 10434 *factor = desired / (allocated > 0 ? allocated : 1); 10435 10436 return (ENOMEM); 10437 } 10438 10439 /* 10440 * Note: called from probe context. This function just increments the drop 10441 * count on a buffer. It has been made a function to allow for the 10442 * possibility of understanding the source of mysterious drop counts. (A 10443 * problem for which one may be particularly disappointed that DTrace cannot 10444 * be used to understand DTrace.) 10445 */ 10446 static void 10447 dtrace_buffer_drop(dtrace_buffer_t *buf) 10448 { 10449 buf->dtb_drops++; 10450 } 10451 10452 /* 10453 * Note: called from probe context. This function is called to reserve space 10454 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 10455 * mstate. Returns the new offset in the buffer, or a negative value if an 10456 * error has occurred. 10457 */ 10458 static intptr_t 10459 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 10460 dtrace_state_t *state, dtrace_mstate_t *mstate) 10461 { 10462 intptr_t offs = buf->dtb_offset, soffs; 10463 intptr_t woffs; 10464 caddr_t tomax; 10465 size_t total; 10466 10467 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 10468 return (-1); 10469 10470 if ((tomax = buf->dtb_tomax) == NULL) { 10471 dtrace_buffer_drop(buf); 10472 return (-1); 10473 } 10474 10475 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 10476 while (offs & (align - 1)) { 10477 /* 10478 * Assert that our alignment is off by a number which 10479 * is itself sizeof (uint32_t) aligned. 10480 */ 10481 ASSERT(!((align - (offs & (align - 1))) & 10482 (sizeof (uint32_t) - 1))); 10483 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10484 offs += sizeof (uint32_t); 10485 } 10486 10487 if ((soffs = offs + needed) > buf->dtb_size) { 10488 dtrace_buffer_drop(buf); 10489 return (-1); 10490 } 10491 10492 if (mstate == NULL) 10493 return (offs); 10494 10495 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 10496 mstate->dtms_scratch_size = buf->dtb_size - soffs; 10497 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10498 10499 return (offs); 10500 } 10501 10502 if (buf->dtb_flags & DTRACEBUF_FILL) { 10503 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 10504 (buf->dtb_flags & DTRACEBUF_FULL)) 10505 return (-1); 10506 goto out; 10507 } 10508 10509 total = needed + (offs & (align - 1)); 10510 10511 /* 10512 * For a ring buffer, life is quite a bit more complicated. Before 10513 * we can store any padding, we need to adjust our wrapping offset. 10514 * (If we've never before wrapped or we're not about to, no adjustment 10515 * is required.) 10516 */ 10517 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 10518 offs + total > buf->dtb_size) { 10519 woffs = buf->dtb_xamot_offset; 10520 10521 if (offs + total > buf->dtb_size) { 10522 /* 10523 * We can't fit in the end of the buffer. First, a 10524 * sanity check that we can fit in the buffer at all. 10525 */ 10526 if (total > buf->dtb_size) { 10527 dtrace_buffer_drop(buf); 10528 return (-1); 10529 } 10530 10531 /* 10532 * We're going to be storing at the top of the buffer, 10533 * so now we need to deal with the wrapped offset. We 10534 * only reset our wrapped offset to 0 if it is 10535 * currently greater than the current offset. If it 10536 * is less than the current offset, it is because a 10537 * previous allocation induced a wrap -- but the 10538 * allocation didn't subsequently take the space due 10539 * to an error or false predicate evaluation. In this 10540 * case, we'll just leave the wrapped offset alone: if 10541 * the wrapped offset hasn't been advanced far enough 10542 * for this allocation, it will be adjusted in the 10543 * lower loop. 10544 */ 10545 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 10546 if (woffs >= offs) 10547 woffs = 0; 10548 } else { 10549 woffs = 0; 10550 } 10551 10552 /* 10553 * Now we know that we're going to be storing to the 10554 * top of the buffer and that there is room for us 10555 * there. We need to clear the buffer from the current 10556 * offset to the end (there may be old gunk there). 10557 */ 10558 while (offs < buf->dtb_size) 10559 tomax[offs++] = 0; 10560 10561 /* 10562 * We need to set our offset to zero. And because we 10563 * are wrapping, we need to set the bit indicating as 10564 * much. We can also adjust our needed space back 10565 * down to the space required by the ECB -- we know 10566 * that the top of the buffer is aligned. 10567 */ 10568 offs = 0; 10569 total = needed; 10570 buf->dtb_flags |= DTRACEBUF_WRAPPED; 10571 } else { 10572 /* 10573 * There is room for us in the buffer, so we simply 10574 * need to check the wrapped offset. 10575 */ 10576 if (woffs < offs) { 10577 /* 10578 * The wrapped offset is less than the offset. 10579 * This can happen if we allocated buffer space 10580 * that induced a wrap, but then we didn't 10581 * subsequently take the space due to an error 10582 * or false predicate evaluation. This is 10583 * okay; we know that _this_ allocation isn't 10584 * going to induce a wrap. We still can't 10585 * reset the wrapped offset to be zero, 10586 * however: the space may have been trashed in 10587 * the previous failed probe attempt. But at 10588 * least the wrapped offset doesn't need to 10589 * be adjusted at all... 10590 */ 10591 goto out; 10592 } 10593 } 10594 10595 while (offs + total > woffs) { 10596 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 10597 size_t size; 10598 10599 if (epid == DTRACE_EPIDNONE) { 10600 size = sizeof (uint32_t); 10601 } else { 10602 ASSERT(epid <= state->dts_necbs); 10603 ASSERT(state->dts_ecbs[epid - 1] != NULL); 10604 10605 size = state->dts_ecbs[epid - 1]->dte_size; 10606 } 10607 10608 ASSERT(woffs + size <= buf->dtb_size); 10609 ASSERT(size != 0); 10610 10611 if (woffs + size == buf->dtb_size) { 10612 /* 10613 * We've reached the end of the buffer; we want 10614 * to set the wrapped offset to 0 and break 10615 * out. However, if the offs is 0, then we're 10616 * in a strange edge-condition: the amount of 10617 * space that we want to reserve plus the size 10618 * of the record that we're overwriting is 10619 * greater than the size of the buffer. This 10620 * is problematic because if we reserve the 10621 * space but subsequently don't consume it (due 10622 * to a failed predicate or error) the wrapped 10623 * offset will be 0 -- yet the EPID at offset 0 10624 * will not be committed. This situation is 10625 * relatively easy to deal with: if we're in 10626 * this case, the buffer is indistinguishable 10627 * from one that hasn't wrapped; we need only 10628 * finish the job by clearing the wrapped bit, 10629 * explicitly setting the offset to be 0, and 10630 * zero'ing out the old data in the buffer. 10631 */ 10632 if (offs == 0) { 10633 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 10634 buf->dtb_offset = 0; 10635 woffs = total; 10636 10637 while (woffs < buf->dtb_size) 10638 tomax[woffs++] = 0; 10639 } 10640 10641 woffs = 0; 10642 break; 10643 } 10644 10645 woffs += size; 10646 } 10647 10648 /* 10649 * We have a wrapped offset. It may be that the wrapped offset 10650 * has become zero -- that's okay. 10651 */ 10652 buf->dtb_xamot_offset = woffs; 10653 } 10654 10655 out: 10656 /* 10657 * Now we can plow the buffer with any necessary padding. 10658 */ 10659 while (offs & (align - 1)) { 10660 /* 10661 * Assert that our alignment is off by a number which 10662 * is itself sizeof (uint32_t) aligned. 10663 */ 10664 ASSERT(!((align - (offs & (align - 1))) & 10665 (sizeof (uint32_t) - 1))); 10666 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10667 offs += sizeof (uint32_t); 10668 } 10669 10670 if (buf->dtb_flags & DTRACEBUF_FILL) { 10671 if (offs + needed > buf->dtb_size - state->dts_reserve) { 10672 buf->dtb_flags |= DTRACEBUF_FULL; 10673 return (-1); 10674 } 10675 } 10676 10677 if (mstate == NULL) 10678 return (offs); 10679 10680 /* 10681 * For ring buffers and fill buffers, the scratch space is always 10682 * the inactive buffer. 10683 */ 10684 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 10685 mstate->dtms_scratch_size = buf->dtb_size; 10686 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10687 10688 return (offs); 10689 } 10690 10691 static void 10692 dtrace_buffer_polish(dtrace_buffer_t *buf) 10693 { 10694 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 10695 ASSERT(MUTEX_HELD(&dtrace_lock)); 10696 10697 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 10698 return; 10699 10700 /* 10701 * We need to polish the ring buffer. There are three cases: 10702 * 10703 * - The first (and presumably most common) is that there is no gap 10704 * between the buffer offset and the wrapped offset. In this case, 10705 * there is nothing in the buffer that isn't valid data; we can 10706 * mark the buffer as polished and return. 10707 * 10708 * - The second (less common than the first but still more common 10709 * than the third) is that there is a gap between the buffer offset 10710 * and the wrapped offset, and the wrapped offset is larger than the 10711 * buffer offset. This can happen because of an alignment issue, or 10712 * can happen because of a call to dtrace_buffer_reserve() that 10713 * didn't subsequently consume the buffer space. In this case, 10714 * we need to zero the data from the buffer offset to the wrapped 10715 * offset. 10716 * 10717 * - The third (and least common) is that there is a gap between the 10718 * buffer offset and the wrapped offset, but the wrapped offset is 10719 * _less_ than the buffer offset. This can only happen because a 10720 * call to dtrace_buffer_reserve() induced a wrap, but the space 10721 * was not subsequently consumed. In this case, we need to zero the 10722 * space from the offset to the end of the buffer _and_ from the 10723 * top of the buffer to the wrapped offset. 10724 */ 10725 if (buf->dtb_offset < buf->dtb_xamot_offset) { 10726 bzero(buf->dtb_tomax + buf->dtb_offset, 10727 buf->dtb_xamot_offset - buf->dtb_offset); 10728 } 10729 10730 if (buf->dtb_offset > buf->dtb_xamot_offset) { 10731 bzero(buf->dtb_tomax + buf->dtb_offset, 10732 buf->dtb_size - buf->dtb_offset); 10733 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 10734 } 10735 } 10736 10737 /* 10738 * This routine determines if data generated at the specified time has likely 10739 * been entirely consumed at user-level. This routine is called to determine 10740 * if an ECB on a defunct probe (but for an active enabling) can be safely 10741 * disabled and destroyed. 10742 */ 10743 static int 10744 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 10745 { 10746 int i; 10747 10748 for (i = 0; i < NCPU; i++) { 10749 dtrace_buffer_t *buf = &bufs[i]; 10750 10751 if (buf->dtb_size == 0) 10752 continue; 10753 10754 if (buf->dtb_flags & DTRACEBUF_RING) 10755 return (0); 10756 10757 if (!buf->dtb_switched && buf->dtb_offset != 0) 10758 return (0); 10759 10760 if (buf->dtb_switched - buf->dtb_interval < when) 10761 return (0); 10762 } 10763 10764 return (1); 10765 } 10766 10767 static void 10768 dtrace_buffer_free(dtrace_buffer_t *bufs) 10769 { 10770 int i; 10771 10772 for (i = 0; i < NCPU; i++) { 10773 dtrace_buffer_t *buf = &bufs[i]; 10774 10775 if (buf->dtb_tomax == NULL) { 10776 ASSERT(buf->dtb_xamot == NULL); 10777 ASSERT(buf->dtb_size == 0); 10778 continue; 10779 } 10780 10781 if (buf->dtb_xamot != NULL) { 10782 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10783 kmem_free(buf->dtb_xamot, buf->dtb_size); 10784 } 10785 10786 kmem_free(buf->dtb_tomax, buf->dtb_size); 10787 buf->dtb_size = 0; 10788 buf->dtb_tomax = NULL; 10789 buf->dtb_xamot = NULL; 10790 } 10791 } 10792 10793 /* 10794 * DTrace Enabling Functions 10795 */ 10796 static dtrace_enabling_t * 10797 dtrace_enabling_create(dtrace_vstate_t *vstate) 10798 { 10799 dtrace_enabling_t *enab; 10800 10801 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 10802 enab->dten_vstate = vstate; 10803 10804 return (enab); 10805 } 10806 10807 static void 10808 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 10809 { 10810 dtrace_ecbdesc_t **ndesc; 10811 size_t osize, nsize; 10812 10813 /* 10814 * We can't add to enablings after we've enabled them, or after we've 10815 * retained them. 10816 */ 10817 ASSERT(enab->dten_probegen == 0); 10818 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 10819 10820 if (enab->dten_ndesc < enab->dten_maxdesc) { 10821 enab->dten_desc[enab->dten_ndesc++] = ecb; 10822 return; 10823 } 10824 10825 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 10826 10827 if (enab->dten_maxdesc == 0) { 10828 enab->dten_maxdesc = 1; 10829 } else { 10830 enab->dten_maxdesc <<= 1; 10831 } 10832 10833 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 10834 10835 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 10836 ndesc = kmem_zalloc(nsize, KM_SLEEP); 10837 bcopy(enab->dten_desc, ndesc, osize); 10838 kmem_free(enab->dten_desc, osize); 10839 10840 enab->dten_desc = ndesc; 10841 enab->dten_desc[enab->dten_ndesc++] = ecb; 10842 } 10843 10844 static void 10845 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 10846 dtrace_probedesc_t *pd) 10847 { 10848 dtrace_ecbdesc_t *new; 10849 dtrace_predicate_t *pred; 10850 dtrace_actdesc_t *act; 10851 10852 /* 10853 * We're going to create a new ECB description that matches the 10854 * specified ECB in every way, but has the specified probe description. 10855 */ 10856 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 10857 10858 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 10859 dtrace_predicate_hold(pred); 10860 10861 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 10862 dtrace_actdesc_hold(act); 10863 10864 new->dted_action = ecb->dted_action; 10865 new->dted_pred = ecb->dted_pred; 10866 new->dted_probe = *pd; 10867 new->dted_uarg = ecb->dted_uarg; 10868 10869 dtrace_enabling_add(enab, new); 10870 } 10871 10872 static void 10873 dtrace_enabling_dump(dtrace_enabling_t *enab) 10874 { 10875 int i; 10876 10877 for (i = 0; i < enab->dten_ndesc; i++) { 10878 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 10879 10880 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 10881 desc->dtpd_provider, desc->dtpd_mod, 10882 desc->dtpd_func, desc->dtpd_name); 10883 } 10884 } 10885 10886 static void 10887 dtrace_enabling_destroy(dtrace_enabling_t *enab) 10888 { 10889 int i; 10890 dtrace_ecbdesc_t *ep; 10891 dtrace_vstate_t *vstate = enab->dten_vstate; 10892 10893 ASSERT(MUTEX_HELD(&dtrace_lock)); 10894 10895 for (i = 0; i < enab->dten_ndesc; i++) { 10896 dtrace_actdesc_t *act, *next; 10897 dtrace_predicate_t *pred; 10898 10899 ep = enab->dten_desc[i]; 10900 10901 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 10902 dtrace_predicate_release(pred, vstate); 10903 10904 for (act = ep->dted_action; act != NULL; act = next) { 10905 next = act->dtad_next; 10906 dtrace_actdesc_release(act, vstate); 10907 } 10908 10909 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 10910 } 10911 10912 kmem_free(enab->dten_desc, 10913 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 10914 10915 /* 10916 * If this was a retained enabling, decrement the dts_nretained count 10917 * and take it off of the dtrace_retained list. 10918 */ 10919 if (enab->dten_prev != NULL || enab->dten_next != NULL || 10920 dtrace_retained == enab) { 10921 ASSERT(enab->dten_vstate->dtvs_state != NULL); 10922 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 10923 enab->dten_vstate->dtvs_state->dts_nretained--; 10924 dtrace_retained_gen++; 10925 } 10926 10927 if (enab->dten_prev == NULL) { 10928 if (dtrace_retained == enab) { 10929 dtrace_retained = enab->dten_next; 10930 10931 if (dtrace_retained != NULL) 10932 dtrace_retained->dten_prev = NULL; 10933 } 10934 } else { 10935 ASSERT(enab != dtrace_retained); 10936 ASSERT(dtrace_retained != NULL); 10937 enab->dten_prev->dten_next = enab->dten_next; 10938 } 10939 10940 if (enab->dten_next != NULL) { 10941 ASSERT(dtrace_retained != NULL); 10942 enab->dten_next->dten_prev = enab->dten_prev; 10943 } 10944 10945 kmem_free(enab, sizeof (dtrace_enabling_t)); 10946 } 10947 10948 static int 10949 dtrace_enabling_retain(dtrace_enabling_t *enab) 10950 { 10951 dtrace_state_t *state; 10952 10953 ASSERT(MUTEX_HELD(&dtrace_lock)); 10954 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 10955 ASSERT(enab->dten_vstate != NULL); 10956 10957 state = enab->dten_vstate->dtvs_state; 10958 ASSERT(state != NULL); 10959 10960 /* 10961 * We only allow each state to retain dtrace_retain_max enablings. 10962 */ 10963 if (state->dts_nretained >= dtrace_retain_max) 10964 return (ENOSPC); 10965 10966 state->dts_nretained++; 10967 dtrace_retained_gen++; 10968 10969 if (dtrace_retained == NULL) { 10970 dtrace_retained = enab; 10971 return (0); 10972 } 10973 10974 enab->dten_next = dtrace_retained; 10975 dtrace_retained->dten_prev = enab; 10976 dtrace_retained = enab; 10977 10978 return (0); 10979 } 10980 10981 static int 10982 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 10983 dtrace_probedesc_t *create) 10984 { 10985 dtrace_enabling_t *new, *enab; 10986 int found = 0, err = ENOENT; 10987 10988 ASSERT(MUTEX_HELD(&dtrace_lock)); 10989 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 10990 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 10991 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 10992 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 10993 10994 new = dtrace_enabling_create(&state->dts_vstate); 10995 10996 /* 10997 * Iterate over all retained enablings, looking for enablings that 10998 * match the specified state. 10999 */ 11000 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11001 int i; 11002 11003 /* 11004 * dtvs_state can only be NULL for helper enablings -- and 11005 * helper enablings can't be retained. 11006 */ 11007 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11008 11009 if (enab->dten_vstate->dtvs_state != state) 11010 continue; 11011 11012 /* 11013 * Now iterate over each probe description; we're looking for 11014 * an exact match to the specified probe description. 11015 */ 11016 for (i = 0; i < enab->dten_ndesc; i++) { 11017 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11018 dtrace_probedesc_t *pd = &ep->dted_probe; 11019 11020 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 11021 continue; 11022 11023 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 11024 continue; 11025 11026 if (strcmp(pd->dtpd_func, match->dtpd_func)) 11027 continue; 11028 11029 if (strcmp(pd->dtpd_name, match->dtpd_name)) 11030 continue; 11031 11032 /* 11033 * We have a winning probe! Add it to our growing 11034 * enabling. 11035 */ 11036 found = 1; 11037 dtrace_enabling_addlike(new, ep, create); 11038 } 11039 } 11040 11041 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 11042 dtrace_enabling_destroy(new); 11043 return (err); 11044 } 11045 11046 return (0); 11047 } 11048 11049 static void 11050 dtrace_enabling_retract(dtrace_state_t *state) 11051 { 11052 dtrace_enabling_t *enab, *next; 11053 11054 ASSERT(MUTEX_HELD(&dtrace_lock)); 11055 11056 /* 11057 * Iterate over all retained enablings, destroy the enablings retained 11058 * for the specified state. 11059 */ 11060 for (enab = dtrace_retained; enab != NULL; enab = next) { 11061 next = enab->dten_next; 11062 11063 /* 11064 * dtvs_state can only be NULL for helper enablings -- and 11065 * helper enablings can't be retained. 11066 */ 11067 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11068 11069 if (enab->dten_vstate->dtvs_state == state) { 11070 ASSERT(state->dts_nretained > 0); 11071 dtrace_enabling_destroy(enab); 11072 } 11073 } 11074 11075 ASSERT(state->dts_nretained == 0); 11076 } 11077 11078 static int 11079 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 11080 { 11081 int i = 0; 11082 int total_matched = 0, matched = 0; 11083 11084 ASSERT(MUTEX_HELD(&cpu_lock)); 11085 ASSERT(MUTEX_HELD(&dtrace_lock)); 11086 11087 for (i = 0; i < enab->dten_ndesc; i++) { 11088 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11089 11090 enab->dten_current = ep; 11091 enab->dten_error = 0; 11092 11093 /* 11094 * If a provider failed to enable a probe then get out and 11095 * let the consumer know we failed. 11096 */ 11097 if ((matched = dtrace_probe_enable(&ep->dted_probe, enab)) < 0) 11098 return (EBUSY); 11099 11100 total_matched += matched; 11101 11102 if (enab->dten_error != 0) { 11103 /* 11104 * If we get an error half-way through enabling the 11105 * probes, we kick out -- perhaps with some number of 11106 * them enabled. Leaving enabled probes enabled may 11107 * be slightly confusing for user-level, but we expect 11108 * that no one will attempt to actually drive on in 11109 * the face of such errors. If this is an anonymous 11110 * enabling (indicated with a NULL nmatched pointer), 11111 * we cmn_err() a message. We aren't expecting to 11112 * get such an error -- such as it can exist at all, 11113 * it would be a result of corrupted DOF in the driver 11114 * properties. 11115 */ 11116 if (nmatched == NULL) { 11117 cmn_err(CE_WARN, "dtrace_enabling_match() " 11118 "error on %p: %d", (void *)ep, 11119 enab->dten_error); 11120 } 11121 11122 return (enab->dten_error); 11123 } 11124 } 11125 11126 enab->dten_probegen = dtrace_probegen; 11127 if (nmatched != NULL) 11128 *nmatched = total_matched; 11129 11130 return (0); 11131 } 11132 11133 static void 11134 dtrace_enabling_matchall(void) 11135 { 11136 dtrace_enabling_t *enab; 11137 11138 mutex_enter(&cpu_lock); 11139 mutex_enter(&dtrace_lock); 11140 11141 /* 11142 * Iterate over all retained enablings to see if any probes match 11143 * against them. We only perform this operation on enablings for which 11144 * we have sufficient permissions by virtue of being in the global zone 11145 * or in the same zone as the DTrace client. Because we can be called 11146 * after dtrace_detach() has been called, we cannot assert that there 11147 * are retained enablings. We can safely load from dtrace_retained, 11148 * however: the taskq_destroy() at the end of dtrace_detach() will 11149 * block pending our completion. 11150 */ 11151 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11152 dtrace_cred_t *dcr = &enab->dten_vstate->dtvs_state->dts_cred; 11153 cred_t *cr = dcr->dcr_cred; 11154 zoneid_t zone = cr != NULL ? crgetzoneid(cr) : 0; 11155 11156 if ((dcr->dcr_visible & DTRACE_CRV_ALLZONE) || (cr != NULL && 11157 (zone == GLOBAL_ZONEID || getzoneid() == zone))) 11158 (void) dtrace_enabling_match(enab, NULL); 11159 } 11160 11161 mutex_exit(&dtrace_lock); 11162 mutex_exit(&cpu_lock); 11163 } 11164 11165 /* 11166 * If an enabling is to be enabled without having matched probes (that is, if 11167 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 11168 * enabling must be _primed_ by creating an ECB for every ECB description. 11169 * This must be done to assure that we know the number of speculations, the 11170 * number of aggregations, the minimum buffer size needed, etc. before we 11171 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 11172 * enabling any probes, we create ECBs for every ECB decription, but with a 11173 * NULL probe -- which is exactly what this function does. 11174 */ 11175 static void 11176 dtrace_enabling_prime(dtrace_state_t *state) 11177 { 11178 dtrace_enabling_t *enab; 11179 int i; 11180 11181 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11182 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11183 11184 if (enab->dten_vstate->dtvs_state != state) 11185 continue; 11186 11187 /* 11188 * We don't want to prime an enabling more than once, lest 11189 * we allow a malicious user to induce resource exhaustion. 11190 * (The ECBs that result from priming an enabling aren't 11191 * leaked -- but they also aren't deallocated until the 11192 * consumer state is destroyed.) 11193 */ 11194 if (enab->dten_primed) 11195 continue; 11196 11197 for (i = 0; i < enab->dten_ndesc; i++) { 11198 enab->dten_current = enab->dten_desc[i]; 11199 (void) dtrace_probe_enable(NULL, enab); 11200 } 11201 11202 enab->dten_primed = 1; 11203 } 11204 } 11205 11206 /* 11207 * Called to indicate that probes should be provided due to retained 11208 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 11209 * must take an initial lap through the enabling calling the dtps_provide() 11210 * entry point explicitly to allow for autocreated probes. 11211 */ 11212 static void 11213 dtrace_enabling_provide(dtrace_provider_t *prv) 11214 { 11215 int i, all = 0; 11216 dtrace_probedesc_t desc; 11217 dtrace_genid_t gen; 11218 11219 ASSERT(MUTEX_HELD(&dtrace_lock)); 11220 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 11221 11222 if (prv == NULL) { 11223 all = 1; 11224 prv = dtrace_provider; 11225 } 11226 11227 do { 11228 dtrace_enabling_t *enab; 11229 void *parg = prv->dtpv_arg; 11230 11231 retry: 11232 gen = dtrace_retained_gen; 11233 for (enab = dtrace_retained; enab != NULL; 11234 enab = enab->dten_next) { 11235 for (i = 0; i < enab->dten_ndesc; i++) { 11236 desc = enab->dten_desc[i]->dted_probe; 11237 mutex_exit(&dtrace_lock); 11238 prv->dtpv_pops.dtps_provide(parg, &desc); 11239 mutex_enter(&dtrace_lock); 11240 /* 11241 * Process the retained enablings again if 11242 * they have changed while we weren't holding 11243 * dtrace_lock. 11244 */ 11245 if (gen != dtrace_retained_gen) 11246 goto retry; 11247 } 11248 } 11249 } while (all && (prv = prv->dtpv_next) != NULL); 11250 11251 mutex_exit(&dtrace_lock); 11252 dtrace_probe_provide(NULL, all ? NULL : prv); 11253 mutex_enter(&dtrace_lock); 11254 } 11255 11256 /* 11257 * Called to reap ECBs that are attached to probes from defunct providers. 11258 */ 11259 static void 11260 dtrace_enabling_reap(void) 11261 { 11262 dtrace_provider_t *prov; 11263 dtrace_probe_t *probe; 11264 dtrace_ecb_t *ecb; 11265 hrtime_t when; 11266 int i; 11267 11268 mutex_enter(&cpu_lock); 11269 mutex_enter(&dtrace_lock); 11270 11271 for (i = 0; i < dtrace_nprobes; i++) { 11272 if ((probe = dtrace_probes[i]) == NULL) 11273 continue; 11274 11275 if (probe->dtpr_ecb == NULL) 11276 continue; 11277 11278 prov = probe->dtpr_provider; 11279 11280 if ((when = prov->dtpv_defunct) == 0) 11281 continue; 11282 11283 /* 11284 * We have ECBs on a defunct provider: we want to reap these 11285 * ECBs to allow the provider to unregister. The destruction 11286 * of these ECBs must be done carefully: if we destroy the ECB 11287 * and the consumer later wishes to consume an EPID that 11288 * corresponds to the destroyed ECB (and if the EPID metadata 11289 * has not been previously consumed), the consumer will abort 11290 * processing on the unknown EPID. To reduce (but not, sadly, 11291 * eliminate) the possibility of this, we will only destroy an 11292 * ECB for a defunct provider if, for the state that 11293 * corresponds to the ECB: 11294 * 11295 * (a) There is no speculative tracing (which can effectively 11296 * cache an EPID for an arbitrary amount of time). 11297 * 11298 * (b) The principal buffers have been switched twice since the 11299 * provider became defunct. 11300 * 11301 * (c) The aggregation buffers are of zero size or have been 11302 * switched twice since the provider became defunct. 11303 * 11304 * We use dts_speculates to determine (a) and call a function 11305 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 11306 * that as soon as we've been unable to destroy one of the ECBs 11307 * associated with the probe, we quit trying -- reaping is only 11308 * fruitful in as much as we can destroy all ECBs associated 11309 * with the defunct provider's probes. 11310 */ 11311 while ((ecb = probe->dtpr_ecb) != NULL) { 11312 dtrace_state_t *state = ecb->dte_state; 11313 dtrace_buffer_t *buf = state->dts_buffer; 11314 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 11315 11316 if (state->dts_speculates) 11317 break; 11318 11319 if (!dtrace_buffer_consumed(buf, when)) 11320 break; 11321 11322 if (!dtrace_buffer_consumed(aggbuf, when)) 11323 break; 11324 11325 dtrace_ecb_disable(ecb); 11326 ASSERT(probe->dtpr_ecb != ecb); 11327 dtrace_ecb_destroy(ecb); 11328 } 11329 } 11330 11331 mutex_exit(&dtrace_lock); 11332 mutex_exit(&cpu_lock); 11333 } 11334 11335 /* 11336 * DTrace DOF Functions 11337 */ 11338 /*ARGSUSED*/ 11339 static void 11340 dtrace_dof_error(dof_hdr_t *dof, const char *str) 11341 { 11342 if (dtrace_err_verbose) 11343 cmn_err(CE_WARN, "failed to process DOF: %s", str); 11344 11345 #ifdef DTRACE_ERRDEBUG 11346 dtrace_errdebug(str); 11347 #endif 11348 } 11349 11350 /* 11351 * Create DOF out of a currently enabled state. Right now, we only create 11352 * DOF containing the run-time options -- but this could be expanded to create 11353 * complete DOF representing the enabled state. 11354 */ 11355 static dof_hdr_t * 11356 dtrace_dof_create(dtrace_state_t *state) 11357 { 11358 dof_hdr_t *dof; 11359 dof_sec_t *sec; 11360 dof_optdesc_t *opt; 11361 int i, len = sizeof (dof_hdr_t) + 11362 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 11363 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11364 11365 ASSERT(MUTEX_HELD(&dtrace_lock)); 11366 11367 dof = kmem_zalloc(len, KM_SLEEP); 11368 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 11369 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 11370 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 11371 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 11372 11373 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 11374 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 11375 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 11376 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 11377 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 11378 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 11379 11380 dof->dofh_flags = 0; 11381 dof->dofh_hdrsize = sizeof (dof_hdr_t); 11382 dof->dofh_secsize = sizeof (dof_sec_t); 11383 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 11384 dof->dofh_secoff = sizeof (dof_hdr_t); 11385 dof->dofh_loadsz = len; 11386 dof->dofh_filesz = len; 11387 dof->dofh_pad = 0; 11388 11389 /* 11390 * Fill in the option section header... 11391 */ 11392 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 11393 sec->dofs_type = DOF_SECT_OPTDESC; 11394 sec->dofs_align = sizeof (uint64_t); 11395 sec->dofs_flags = DOF_SECF_LOAD; 11396 sec->dofs_entsize = sizeof (dof_optdesc_t); 11397 11398 opt = (dof_optdesc_t *)((uintptr_t)sec + 11399 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 11400 11401 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 11402 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11403 11404 for (i = 0; i < DTRACEOPT_MAX; i++) { 11405 opt[i].dofo_option = i; 11406 opt[i].dofo_strtab = DOF_SECIDX_NONE; 11407 opt[i].dofo_value = state->dts_options[i]; 11408 } 11409 11410 return (dof); 11411 } 11412 11413 static dof_hdr_t * 11414 dtrace_dof_copyin(uintptr_t uarg, int *errp) 11415 { 11416 dof_hdr_t hdr, *dof; 11417 11418 ASSERT(!MUTEX_HELD(&dtrace_lock)); 11419 11420 /* 11421 * First, we're going to copyin() the sizeof (dof_hdr_t). 11422 */ 11423 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 11424 dtrace_dof_error(NULL, "failed to copyin DOF header"); 11425 *errp = EFAULT; 11426 return (NULL); 11427 } 11428 11429 /* 11430 * Now we'll allocate the entire DOF and copy it in -- provided 11431 * that the length isn't outrageous. 11432 */ 11433 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 11434 dtrace_dof_error(&hdr, "load size exceeds maximum"); 11435 *errp = E2BIG; 11436 return (NULL); 11437 } 11438 11439 if (hdr.dofh_loadsz < sizeof (hdr)) { 11440 dtrace_dof_error(&hdr, "invalid load size"); 11441 *errp = EINVAL; 11442 return (NULL); 11443 } 11444 11445 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 11446 11447 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 11448 dof->dofh_loadsz != hdr.dofh_loadsz) { 11449 kmem_free(dof, hdr.dofh_loadsz); 11450 *errp = EFAULT; 11451 return (NULL); 11452 } 11453 11454 return (dof); 11455 } 11456 11457 static dof_hdr_t * 11458 dtrace_dof_property(const char *name) 11459 { 11460 uchar_t *buf; 11461 uint64_t loadsz; 11462 unsigned int len, i; 11463 dof_hdr_t *dof; 11464 11465 /* 11466 * Unfortunately, array of values in .conf files are always (and 11467 * only) interpreted to be integer arrays. We must read our DOF 11468 * as an integer array, and then squeeze it into a byte array. 11469 */ 11470 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 11471 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 11472 return (NULL); 11473 11474 for (i = 0; i < len; i++) 11475 buf[i] = (uchar_t)(((int *)buf)[i]); 11476 11477 if (len < sizeof (dof_hdr_t)) { 11478 ddi_prop_free(buf); 11479 dtrace_dof_error(NULL, "truncated header"); 11480 return (NULL); 11481 } 11482 11483 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 11484 ddi_prop_free(buf); 11485 dtrace_dof_error(NULL, "truncated DOF"); 11486 return (NULL); 11487 } 11488 11489 if (loadsz >= dtrace_dof_maxsize) { 11490 ddi_prop_free(buf); 11491 dtrace_dof_error(NULL, "oversized DOF"); 11492 return (NULL); 11493 } 11494 11495 dof = kmem_alloc(loadsz, KM_SLEEP); 11496 bcopy(buf, dof, loadsz); 11497 ddi_prop_free(buf); 11498 11499 return (dof); 11500 } 11501 11502 static void 11503 dtrace_dof_destroy(dof_hdr_t *dof) 11504 { 11505 kmem_free(dof, dof->dofh_loadsz); 11506 } 11507 11508 /* 11509 * Return the dof_sec_t pointer corresponding to a given section index. If the 11510 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 11511 * a type other than DOF_SECT_NONE is specified, the header is checked against 11512 * this type and NULL is returned if the types do not match. 11513 */ 11514 static dof_sec_t * 11515 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 11516 { 11517 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 11518 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 11519 11520 if (i >= dof->dofh_secnum) { 11521 dtrace_dof_error(dof, "referenced section index is invalid"); 11522 return (NULL); 11523 } 11524 11525 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 11526 dtrace_dof_error(dof, "referenced section is not loadable"); 11527 return (NULL); 11528 } 11529 11530 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 11531 dtrace_dof_error(dof, "referenced section is the wrong type"); 11532 return (NULL); 11533 } 11534 11535 return (sec); 11536 } 11537 11538 static dtrace_probedesc_t * 11539 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 11540 { 11541 dof_probedesc_t *probe; 11542 dof_sec_t *strtab; 11543 uintptr_t daddr = (uintptr_t)dof; 11544 uintptr_t str; 11545 size_t size; 11546 11547 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 11548 dtrace_dof_error(dof, "invalid probe section"); 11549 return (NULL); 11550 } 11551 11552 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11553 dtrace_dof_error(dof, "bad alignment in probe description"); 11554 return (NULL); 11555 } 11556 11557 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 11558 dtrace_dof_error(dof, "truncated probe description"); 11559 return (NULL); 11560 } 11561 11562 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 11563 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 11564 11565 if (strtab == NULL) 11566 return (NULL); 11567 11568 str = daddr + strtab->dofs_offset; 11569 size = strtab->dofs_size; 11570 11571 if (probe->dofp_provider >= strtab->dofs_size) { 11572 dtrace_dof_error(dof, "corrupt probe provider"); 11573 return (NULL); 11574 } 11575 11576 (void) strncpy(desc->dtpd_provider, 11577 (char *)(str + probe->dofp_provider), 11578 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 11579 11580 if (probe->dofp_mod >= strtab->dofs_size) { 11581 dtrace_dof_error(dof, "corrupt probe module"); 11582 return (NULL); 11583 } 11584 11585 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 11586 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 11587 11588 if (probe->dofp_func >= strtab->dofs_size) { 11589 dtrace_dof_error(dof, "corrupt probe function"); 11590 return (NULL); 11591 } 11592 11593 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 11594 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 11595 11596 if (probe->dofp_name >= strtab->dofs_size) { 11597 dtrace_dof_error(dof, "corrupt probe name"); 11598 return (NULL); 11599 } 11600 11601 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 11602 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 11603 11604 return (desc); 11605 } 11606 11607 static dtrace_difo_t * 11608 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11609 cred_t *cr) 11610 { 11611 dtrace_difo_t *dp; 11612 size_t ttl = 0; 11613 dof_difohdr_t *dofd; 11614 uintptr_t daddr = (uintptr_t)dof; 11615 size_t max = dtrace_difo_maxsize; 11616 int i, l, n; 11617 11618 static const struct { 11619 int section; 11620 int bufoffs; 11621 int lenoffs; 11622 int entsize; 11623 int align; 11624 const char *msg; 11625 } difo[] = { 11626 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 11627 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 11628 sizeof (dif_instr_t), "multiple DIF sections" }, 11629 11630 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 11631 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 11632 sizeof (uint64_t), "multiple integer tables" }, 11633 11634 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 11635 offsetof(dtrace_difo_t, dtdo_strlen), 0, 11636 sizeof (char), "multiple string tables" }, 11637 11638 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 11639 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 11640 sizeof (uint_t), "multiple variable tables" }, 11641 11642 { DOF_SECT_NONE, 0, 0, 0, NULL } 11643 }; 11644 11645 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 11646 dtrace_dof_error(dof, "invalid DIFO header section"); 11647 return (NULL); 11648 } 11649 11650 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11651 dtrace_dof_error(dof, "bad alignment in DIFO header"); 11652 return (NULL); 11653 } 11654 11655 if (sec->dofs_size < sizeof (dof_difohdr_t) || 11656 sec->dofs_size % sizeof (dof_secidx_t)) { 11657 dtrace_dof_error(dof, "bad size in DIFO header"); 11658 return (NULL); 11659 } 11660 11661 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 11662 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 11663 11664 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 11665 dp->dtdo_rtype = dofd->dofd_rtype; 11666 11667 for (l = 0; l < n; l++) { 11668 dof_sec_t *subsec; 11669 void **bufp; 11670 uint32_t *lenp; 11671 11672 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 11673 dofd->dofd_links[l])) == NULL) 11674 goto err; /* invalid section link */ 11675 11676 if (ttl + subsec->dofs_size > max) { 11677 dtrace_dof_error(dof, "exceeds maximum size"); 11678 goto err; 11679 } 11680 11681 ttl += subsec->dofs_size; 11682 11683 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 11684 if (subsec->dofs_type != difo[i].section) 11685 continue; 11686 11687 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 11688 dtrace_dof_error(dof, "section not loaded"); 11689 goto err; 11690 } 11691 11692 if (subsec->dofs_align != difo[i].align) { 11693 dtrace_dof_error(dof, "bad alignment"); 11694 goto err; 11695 } 11696 11697 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 11698 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 11699 11700 if (*bufp != NULL) { 11701 dtrace_dof_error(dof, difo[i].msg); 11702 goto err; 11703 } 11704 11705 if (difo[i].entsize != subsec->dofs_entsize) { 11706 dtrace_dof_error(dof, "entry size mismatch"); 11707 goto err; 11708 } 11709 11710 if (subsec->dofs_entsize != 0 && 11711 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 11712 dtrace_dof_error(dof, "corrupt entry size"); 11713 goto err; 11714 } 11715 11716 *lenp = subsec->dofs_size; 11717 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 11718 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 11719 *bufp, subsec->dofs_size); 11720 11721 if (subsec->dofs_entsize != 0) 11722 *lenp /= subsec->dofs_entsize; 11723 11724 break; 11725 } 11726 11727 /* 11728 * If we encounter a loadable DIFO sub-section that is not 11729 * known to us, assume this is a broken program and fail. 11730 */ 11731 if (difo[i].section == DOF_SECT_NONE && 11732 (subsec->dofs_flags & DOF_SECF_LOAD)) { 11733 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 11734 goto err; 11735 } 11736 } 11737 11738 if (dp->dtdo_buf == NULL) { 11739 /* 11740 * We can't have a DIF object without DIF text. 11741 */ 11742 dtrace_dof_error(dof, "missing DIF text"); 11743 goto err; 11744 } 11745 11746 /* 11747 * Before we validate the DIF object, run through the variable table 11748 * looking for the strings -- if any of their size are under, we'll set 11749 * their size to be the system-wide default string size. Note that 11750 * this should _not_ happen if the "strsize" option has been set -- 11751 * in this case, the compiler should have set the size to reflect the 11752 * setting of the option. 11753 */ 11754 for (i = 0; i < dp->dtdo_varlen; i++) { 11755 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 11756 dtrace_diftype_t *t = &v->dtdv_type; 11757 11758 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 11759 continue; 11760 11761 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 11762 t->dtdt_size = dtrace_strsize_default; 11763 } 11764 11765 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 11766 goto err; 11767 11768 dtrace_difo_init(dp, vstate); 11769 return (dp); 11770 11771 err: 11772 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 11773 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 11774 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 11775 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 11776 11777 kmem_free(dp, sizeof (dtrace_difo_t)); 11778 return (NULL); 11779 } 11780 11781 static dtrace_predicate_t * 11782 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11783 cred_t *cr) 11784 { 11785 dtrace_difo_t *dp; 11786 11787 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 11788 return (NULL); 11789 11790 return (dtrace_predicate_create(dp)); 11791 } 11792 11793 static dtrace_actdesc_t * 11794 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11795 cred_t *cr) 11796 { 11797 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 11798 dof_actdesc_t *desc; 11799 dof_sec_t *difosec; 11800 size_t offs; 11801 uintptr_t daddr = (uintptr_t)dof; 11802 uint64_t arg; 11803 dtrace_actkind_t kind; 11804 11805 if (sec->dofs_type != DOF_SECT_ACTDESC) { 11806 dtrace_dof_error(dof, "invalid action section"); 11807 return (NULL); 11808 } 11809 11810 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 11811 dtrace_dof_error(dof, "truncated action description"); 11812 return (NULL); 11813 } 11814 11815 if (sec->dofs_align != sizeof (uint64_t)) { 11816 dtrace_dof_error(dof, "bad alignment in action description"); 11817 return (NULL); 11818 } 11819 11820 if (sec->dofs_size < sec->dofs_entsize) { 11821 dtrace_dof_error(dof, "section entry size exceeds total size"); 11822 return (NULL); 11823 } 11824 11825 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 11826 dtrace_dof_error(dof, "bad entry size in action description"); 11827 return (NULL); 11828 } 11829 11830 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 11831 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 11832 return (NULL); 11833 } 11834 11835 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 11836 desc = (dof_actdesc_t *)(daddr + 11837 (uintptr_t)sec->dofs_offset + offs); 11838 kind = (dtrace_actkind_t)desc->dofa_kind; 11839 11840 if (DTRACEACT_ISPRINTFLIKE(kind) && 11841 (kind != DTRACEACT_PRINTA || 11842 desc->dofa_strtab != DOF_SECIDX_NONE)) { 11843 dof_sec_t *strtab; 11844 char *str, *fmt; 11845 uint64_t i; 11846 11847 /* 11848 * printf()-like actions must have a format string. 11849 */ 11850 if ((strtab = dtrace_dof_sect(dof, 11851 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 11852 goto err; 11853 11854 str = (char *)((uintptr_t)dof + 11855 (uintptr_t)strtab->dofs_offset); 11856 11857 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 11858 if (str[i] == '\0') 11859 break; 11860 } 11861 11862 if (i >= strtab->dofs_size) { 11863 dtrace_dof_error(dof, "bogus format string"); 11864 goto err; 11865 } 11866 11867 if (i == desc->dofa_arg) { 11868 dtrace_dof_error(dof, "empty format string"); 11869 goto err; 11870 } 11871 11872 i -= desc->dofa_arg; 11873 fmt = kmem_alloc(i + 1, KM_SLEEP); 11874 bcopy(&str[desc->dofa_arg], fmt, i + 1); 11875 arg = (uint64_t)(uintptr_t)fmt; 11876 } else { 11877 if (kind == DTRACEACT_PRINTA) { 11878 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 11879 arg = 0; 11880 } else { 11881 arg = desc->dofa_arg; 11882 } 11883 } 11884 11885 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 11886 desc->dofa_uarg, arg); 11887 11888 if (last != NULL) { 11889 last->dtad_next = act; 11890 } else { 11891 first = act; 11892 } 11893 11894 last = act; 11895 11896 if (desc->dofa_difo == DOF_SECIDX_NONE) 11897 continue; 11898 11899 if ((difosec = dtrace_dof_sect(dof, 11900 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 11901 goto err; 11902 11903 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 11904 11905 if (act->dtad_difo == NULL) 11906 goto err; 11907 } 11908 11909 ASSERT(first != NULL); 11910 return (first); 11911 11912 err: 11913 for (act = first; act != NULL; act = next) { 11914 next = act->dtad_next; 11915 dtrace_actdesc_release(act, vstate); 11916 } 11917 11918 return (NULL); 11919 } 11920 11921 static dtrace_ecbdesc_t * 11922 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11923 cred_t *cr) 11924 { 11925 dtrace_ecbdesc_t *ep; 11926 dof_ecbdesc_t *ecb; 11927 dtrace_probedesc_t *desc; 11928 dtrace_predicate_t *pred = NULL; 11929 11930 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 11931 dtrace_dof_error(dof, "truncated ECB description"); 11932 return (NULL); 11933 } 11934 11935 if (sec->dofs_align != sizeof (uint64_t)) { 11936 dtrace_dof_error(dof, "bad alignment in ECB description"); 11937 return (NULL); 11938 } 11939 11940 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 11941 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 11942 11943 if (sec == NULL) 11944 return (NULL); 11945 11946 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 11947 ep->dted_uarg = ecb->dofe_uarg; 11948 desc = &ep->dted_probe; 11949 11950 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 11951 goto err; 11952 11953 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 11954 if ((sec = dtrace_dof_sect(dof, 11955 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 11956 goto err; 11957 11958 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 11959 goto err; 11960 11961 ep->dted_pred.dtpdd_predicate = pred; 11962 } 11963 11964 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 11965 if ((sec = dtrace_dof_sect(dof, 11966 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 11967 goto err; 11968 11969 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 11970 11971 if (ep->dted_action == NULL) 11972 goto err; 11973 } 11974 11975 return (ep); 11976 11977 err: 11978 if (pred != NULL) 11979 dtrace_predicate_release(pred, vstate); 11980 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 11981 return (NULL); 11982 } 11983 11984 /* 11985 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 11986 * specified DOF. At present, this amounts to simply adding 'ubase' to the 11987 * site of any user SETX relocations to account for load object base address. 11988 * In the future, if we need other relocations, this function can be extended. 11989 */ 11990 static int 11991 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 11992 { 11993 uintptr_t daddr = (uintptr_t)dof; 11994 dof_relohdr_t *dofr = 11995 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 11996 dof_sec_t *ss, *rs, *ts; 11997 dof_relodesc_t *r; 11998 uint_t i, n; 11999 12000 if (sec->dofs_size < sizeof (dof_relohdr_t) || 12001 sec->dofs_align != sizeof (dof_secidx_t)) { 12002 dtrace_dof_error(dof, "invalid relocation header"); 12003 return (-1); 12004 } 12005 12006 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 12007 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 12008 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 12009 12010 if (ss == NULL || rs == NULL || ts == NULL) 12011 return (-1); /* dtrace_dof_error() has been called already */ 12012 12013 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 12014 rs->dofs_align != sizeof (uint64_t)) { 12015 dtrace_dof_error(dof, "invalid relocation section"); 12016 return (-1); 12017 } 12018 12019 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 12020 n = rs->dofs_size / rs->dofs_entsize; 12021 12022 for (i = 0; i < n; i++) { 12023 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 12024 12025 switch (r->dofr_type) { 12026 case DOF_RELO_NONE: 12027 break; 12028 case DOF_RELO_SETX: 12029 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 12030 sizeof (uint64_t) > ts->dofs_size) { 12031 dtrace_dof_error(dof, "bad relocation offset"); 12032 return (-1); 12033 } 12034 12035 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 12036 dtrace_dof_error(dof, "misaligned setx relo"); 12037 return (-1); 12038 } 12039 12040 *(uint64_t *)taddr += ubase; 12041 break; 12042 default: 12043 dtrace_dof_error(dof, "invalid relocation type"); 12044 return (-1); 12045 } 12046 12047 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 12048 } 12049 12050 return (0); 12051 } 12052 12053 /* 12054 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 12055 * header: it should be at the front of a memory region that is at least 12056 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 12057 * size. It need not be validated in any other way. 12058 */ 12059 static int 12060 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 12061 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 12062 { 12063 uint64_t len = dof->dofh_loadsz, seclen; 12064 uintptr_t daddr = (uintptr_t)dof; 12065 dtrace_ecbdesc_t *ep; 12066 dtrace_enabling_t *enab; 12067 uint_t i; 12068 12069 ASSERT(MUTEX_HELD(&dtrace_lock)); 12070 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 12071 12072 /* 12073 * Check the DOF header identification bytes. In addition to checking 12074 * valid settings, we also verify that unused bits/bytes are zeroed so 12075 * we can use them later without fear of regressing existing binaries. 12076 */ 12077 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 12078 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 12079 dtrace_dof_error(dof, "DOF magic string mismatch"); 12080 return (-1); 12081 } 12082 12083 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 12084 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 12085 dtrace_dof_error(dof, "DOF has invalid data model"); 12086 return (-1); 12087 } 12088 12089 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 12090 dtrace_dof_error(dof, "DOF encoding mismatch"); 12091 return (-1); 12092 } 12093 12094 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 12095 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 12096 dtrace_dof_error(dof, "DOF version mismatch"); 12097 return (-1); 12098 } 12099 12100 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 12101 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 12102 return (-1); 12103 } 12104 12105 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 12106 dtrace_dof_error(dof, "DOF uses too many integer registers"); 12107 return (-1); 12108 } 12109 12110 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 12111 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 12112 return (-1); 12113 } 12114 12115 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 12116 if (dof->dofh_ident[i] != 0) { 12117 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 12118 return (-1); 12119 } 12120 } 12121 12122 if (dof->dofh_flags & ~DOF_FL_VALID) { 12123 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 12124 return (-1); 12125 } 12126 12127 if (dof->dofh_secsize == 0) { 12128 dtrace_dof_error(dof, "zero section header size"); 12129 return (-1); 12130 } 12131 12132 /* 12133 * Check that the section headers don't exceed the amount of DOF 12134 * data. Note that we cast the section size and number of sections 12135 * to uint64_t's to prevent possible overflow in the multiplication. 12136 */ 12137 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 12138 12139 if (dof->dofh_secoff > len || seclen > len || 12140 dof->dofh_secoff + seclen > len) { 12141 dtrace_dof_error(dof, "truncated section headers"); 12142 return (-1); 12143 } 12144 12145 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 12146 dtrace_dof_error(dof, "misaligned section headers"); 12147 return (-1); 12148 } 12149 12150 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 12151 dtrace_dof_error(dof, "misaligned section size"); 12152 return (-1); 12153 } 12154 12155 /* 12156 * Take an initial pass through the section headers to be sure that 12157 * the headers don't have stray offsets. If the 'noprobes' flag is 12158 * set, do not permit sections relating to providers, probes, or args. 12159 */ 12160 for (i = 0; i < dof->dofh_secnum; i++) { 12161 dof_sec_t *sec = (dof_sec_t *)(daddr + 12162 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12163 12164 if (noprobes) { 12165 switch (sec->dofs_type) { 12166 case DOF_SECT_PROVIDER: 12167 case DOF_SECT_PROBES: 12168 case DOF_SECT_PRARGS: 12169 case DOF_SECT_PROFFS: 12170 dtrace_dof_error(dof, "illegal sections " 12171 "for enabling"); 12172 return (-1); 12173 } 12174 } 12175 12176 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 12177 !(sec->dofs_flags & DOF_SECF_LOAD)) { 12178 dtrace_dof_error(dof, "loadable section with load " 12179 "flag unset"); 12180 return (-1); 12181 } 12182 12183 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12184 continue; /* just ignore non-loadable sections */ 12185 12186 if (sec->dofs_align & (sec->dofs_align - 1)) { 12187 dtrace_dof_error(dof, "bad section alignment"); 12188 return (-1); 12189 } 12190 12191 if (sec->dofs_offset & (sec->dofs_align - 1)) { 12192 dtrace_dof_error(dof, "misaligned section"); 12193 return (-1); 12194 } 12195 12196 if (sec->dofs_offset > len || sec->dofs_size > len || 12197 sec->dofs_offset + sec->dofs_size > len) { 12198 dtrace_dof_error(dof, "corrupt section header"); 12199 return (-1); 12200 } 12201 12202 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 12203 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 12204 dtrace_dof_error(dof, "non-terminating string table"); 12205 return (-1); 12206 } 12207 } 12208 12209 /* 12210 * Take a second pass through the sections and locate and perform any 12211 * relocations that are present. We do this after the first pass to 12212 * be sure that all sections have had their headers validated. 12213 */ 12214 for (i = 0; i < dof->dofh_secnum; i++) { 12215 dof_sec_t *sec = (dof_sec_t *)(daddr + 12216 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12217 12218 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12219 continue; /* skip sections that are not loadable */ 12220 12221 switch (sec->dofs_type) { 12222 case DOF_SECT_URELHDR: 12223 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 12224 return (-1); 12225 break; 12226 } 12227 } 12228 12229 if ((enab = *enabp) == NULL) 12230 enab = *enabp = dtrace_enabling_create(vstate); 12231 12232 for (i = 0; i < dof->dofh_secnum; i++) { 12233 dof_sec_t *sec = (dof_sec_t *)(daddr + 12234 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12235 12236 if (sec->dofs_type != DOF_SECT_ECBDESC) 12237 continue; 12238 12239 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 12240 dtrace_enabling_destroy(enab); 12241 *enabp = NULL; 12242 return (-1); 12243 } 12244 12245 dtrace_enabling_add(enab, ep); 12246 } 12247 12248 return (0); 12249 } 12250 12251 /* 12252 * Process DOF for any options. This routine assumes that the DOF has been 12253 * at least processed by dtrace_dof_slurp(). 12254 */ 12255 static int 12256 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 12257 { 12258 int i, rval; 12259 uint32_t entsize; 12260 size_t offs; 12261 dof_optdesc_t *desc; 12262 12263 for (i = 0; i < dof->dofh_secnum; i++) { 12264 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 12265 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12266 12267 if (sec->dofs_type != DOF_SECT_OPTDESC) 12268 continue; 12269 12270 if (sec->dofs_align != sizeof (uint64_t)) { 12271 dtrace_dof_error(dof, "bad alignment in " 12272 "option description"); 12273 return (EINVAL); 12274 } 12275 12276 if ((entsize = sec->dofs_entsize) == 0) { 12277 dtrace_dof_error(dof, "zeroed option entry size"); 12278 return (EINVAL); 12279 } 12280 12281 if (entsize < sizeof (dof_optdesc_t)) { 12282 dtrace_dof_error(dof, "bad option entry size"); 12283 return (EINVAL); 12284 } 12285 12286 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 12287 desc = (dof_optdesc_t *)((uintptr_t)dof + 12288 (uintptr_t)sec->dofs_offset + offs); 12289 12290 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 12291 dtrace_dof_error(dof, "non-zero option string"); 12292 return (EINVAL); 12293 } 12294 12295 if (desc->dofo_value == DTRACEOPT_UNSET) { 12296 dtrace_dof_error(dof, "unset option"); 12297 return (EINVAL); 12298 } 12299 12300 if ((rval = dtrace_state_option(state, 12301 desc->dofo_option, desc->dofo_value)) != 0) { 12302 dtrace_dof_error(dof, "rejected option"); 12303 return (rval); 12304 } 12305 } 12306 } 12307 12308 return (0); 12309 } 12310 12311 /* 12312 * DTrace Consumer State Functions 12313 */ 12314 int 12315 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 12316 { 12317 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 12318 void *base; 12319 uintptr_t limit; 12320 dtrace_dynvar_t *dvar, *next, *start; 12321 int i; 12322 12323 ASSERT(MUTEX_HELD(&dtrace_lock)); 12324 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 12325 12326 bzero(dstate, sizeof (dtrace_dstate_t)); 12327 12328 if ((dstate->dtds_chunksize = chunksize) == 0) 12329 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 12330 12331 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 12332 size = min; 12333 12334 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12335 return (ENOMEM); 12336 12337 dstate->dtds_size = size; 12338 dstate->dtds_base = base; 12339 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 12340 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 12341 12342 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 12343 12344 if (hashsize != 1 && (hashsize & 1)) 12345 hashsize--; 12346 12347 dstate->dtds_hashsize = hashsize; 12348 dstate->dtds_hash = dstate->dtds_base; 12349 12350 /* 12351 * Set all of our hash buckets to point to the single sink, and (if 12352 * it hasn't already been set), set the sink's hash value to be the 12353 * sink sentinel value. The sink is needed for dynamic variable 12354 * lookups to know that they have iterated over an entire, valid hash 12355 * chain. 12356 */ 12357 for (i = 0; i < hashsize; i++) 12358 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 12359 12360 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 12361 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 12362 12363 /* 12364 * Determine number of active CPUs. Divide free list evenly among 12365 * active CPUs. 12366 */ 12367 start = (dtrace_dynvar_t *) 12368 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 12369 limit = (uintptr_t)base + size; 12370 12371 maxper = (limit - (uintptr_t)start) / NCPU; 12372 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 12373 12374 for (i = 0; i < NCPU; i++) { 12375 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 12376 12377 /* 12378 * If we don't even have enough chunks to make it once through 12379 * NCPUs, we're just going to allocate everything to the first 12380 * CPU. And if we're on the last CPU, we're going to allocate 12381 * whatever is left over. In either case, we set the limit to 12382 * be the limit of the dynamic variable space. 12383 */ 12384 if (maxper == 0 || i == NCPU - 1) { 12385 limit = (uintptr_t)base + size; 12386 start = NULL; 12387 } else { 12388 limit = (uintptr_t)start + maxper; 12389 start = (dtrace_dynvar_t *)limit; 12390 } 12391 12392 ASSERT(limit <= (uintptr_t)base + size); 12393 12394 for (;;) { 12395 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 12396 dstate->dtds_chunksize); 12397 12398 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 12399 break; 12400 12401 dvar->dtdv_next = next; 12402 dvar = next; 12403 } 12404 12405 if (maxper == 0) 12406 break; 12407 } 12408 12409 return (0); 12410 } 12411 12412 void 12413 dtrace_dstate_fini(dtrace_dstate_t *dstate) 12414 { 12415 ASSERT(MUTEX_HELD(&cpu_lock)); 12416 12417 if (dstate->dtds_base == NULL) 12418 return; 12419 12420 kmem_free(dstate->dtds_base, dstate->dtds_size); 12421 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 12422 } 12423 12424 static void 12425 dtrace_vstate_fini(dtrace_vstate_t *vstate) 12426 { 12427 /* 12428 * Logical XOR, where are you? 12429 */ 12430 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 12431 12432 if (vstate->dtvs_nglobals > 0) { 12433 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 12434 sizeof (dtrace_statvar_t *)); 12435 } 12436 12437 if (vstate->dtvs_ntlocals > 0) { 12438 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 12439 sizeof (dtrace_difv_t)); 12440 } 12441 12442 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 12443 12444 if (vstate->dtvs_nlocals > 0) { 12445 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 12446 sizeof (dtrace_statvar_t *)); 12447 } 12448 } 12449 12450 static void 12451 dtrace_state_clean(dtrace_state_t *state) 12452 { 12453 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 12454 return; 12455 12456 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 12457 dtrace_speculation_clean(state); 12458 } 12459 12460 static void 12461 dtrace_state_deadman(dtrace_state_t *state) 12462 { 12463 hrtime_t now; 12464 12465 dtrace_sync(); 12466 12467 now = dtrace_gethrtime(); 12468 12469 if (state != dtrace_anon.dta_state && 12470 now - state->dts_laststatus >= dtrace_deadman_user) 12471 return; 12472 12473 /* 12474 * We must be sure that dts_alive never appears to be less than the 12475 * value upon entry to dtrace_state_deadman(), and because we lack a 12476 * dtrace_cas64(), we cannot store to it atomically. We thus instead 12477 * store INT64_MAX to it, followed by a memory barrier, followed by 12478 * the new value. This assures that dts_alive never appears to be 12479 * less than its true value, regardless of the order in which the 12480 * stores to the underlying storage are issued. 12481 */ 12482 state->dts_alive = INT64_MAX; 12483 dtrace_membar_producer(); 12484 state->dts_alive = now; 12485 } 12486 12487 dtrace_state_t * 12488 dtrace_state_create(dev_t *devp, cred_t *cr) 12489 { 12490 minor_t minor; 12491 major_t major; 12492 char c[30]; 12493 dtrace_state_t *state; 12494 dtrace_optval_t *opt; 12495 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 12496 12497 ASSERT(MUTEX_HELD(&dtrace_lock)); 12498 ASSERT(MUTEX_HELD(&cpu_lock)); 12499 12500 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 12501 VM_BESTFIT | VM_SLEEP); 12502 12503 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 12504 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 12505 return (NULL); 12506 } 12507 12508 state = ddi_get_soft_state(dtrace_softstate, minor); 12509 state->dts_epid = DTRACE_EPIDNONE + 1; 12510 12511 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); 12512 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 12513 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 12514 12515 if (devp != NULL) { 12516 major = getemajor(*devp); 12517 } else { 12518 major = ddi_driver_major(dtrace_devi); 12519 } 12520 12521 state->dts_dev = makedevice(major, minor); 12522 12523 if (devp != NULL) 12524 *devp = state->dts_dev; 12525 12526 /* 12527 * We allocate NCPU buffers. On the one hand, this can be quite 12528 * a bit of memory per instance (nearly 36K on a Starcat). On the 12529 * other hand, it saves an additional memory reference in the probe 12530 * path. 12531 */ 12532 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 12533 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 12534 state->dts_cleaner = CYCLIC_NONE; 12535 state->dts_deadman = CYCLIC_NONE; 12536 state->dts_vstate.dtvs_state = state; 12537 12538 for (i = 0; i < DTRACEOPT_MAX; i++) 12539 state->dts_options[i] = DTRACEOPT_UNSET; 12540 12541 /* 12542 * Set the default options. 12543 */ 12544 opt = state->dts_options; 12545 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 12546 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 12547 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 12548 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 12549 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 12550 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 12551 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 12552 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 12553 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 12554 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 12555 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 12556 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 12557 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 12558 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 12559 12560 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 12561 12562 /* 12563 * Depending on the user credentials, we set flag bits which alter probe 12564 * visibility or the amount of destructiveness allowed. In the case of 12565 * actual anonymous tracing, or the possession of all privileges, all of 12566 * the normal checks are bypassed. 12567 */ 12568 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 12569 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 12570 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 12571 } else { 12572 /* 12573 * Set up the credentials for this instantiation. We take a 12574 * hold on the credential to prevent it from disappearing on 12575 * us; this in turn prevents the zone_t referenced by this 12576 * credential from disappearing. This means that we can 12577 * examine the credential and the zone from probe context. 12578 */ 12579 crhold(cr); 12580 state->dts_cred.dcr_cred = cr; 12581 12582 /* 12583 * CRA_PROC means "we have *some* privilege for dtrace" and 12584 * unlocks the use of variables like pid, zonename, etc. 12585 */ 12586 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 12587 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12588 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 12589 } 12590 12591 /* 12592 * dtrace_user allows use of syscall and profile providers. 12593 * If the user also has proc_owner and/or proc_zone, we 12594 * extend the scope to include additional visibility and 12595 * destructive power. 12596 */ 12597 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 12598 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 12599 state->dts_cred.dcr_visible |= 12600 DTRACE_CRV_ALLPROC; 12601 12602 state->dts_cred.dcr_action |= 12603 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12604 } 12605 12606 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 12607 state->dts_cred.dcr_visible |= 12608 DTRACE_CRV_ALLZONE; 12609 12610 state->dts_cred.dcr_action |= 12611 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12612 } 12613 12614 /* 12615 * If we have all privs in whatever zone this is, 12616 * we can do destructive things to processes which 12617 * have altered credentials. 12618 */ 12619 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12620 cr->cr_zone->zone_privset)) { 12621 state->dts_cred.dcr_action |= 12622 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12623 } 12624 } 12625 12626 /* 12627 * Holding the dtrace_kernel privilege also implies that 12628 * the user has the dtrace_user privilege from a visibility 12629 * perspective. But without further privileges, some 12630 * destructive actions are not available. 12631 */ 12632 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 12633 /* 12634 * Make all probes in all zones visible. However, 12635 * this doesn't mean that all actions become available 12636 * to all zones. 12637 */ 12638 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 12639 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 12640 12641 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 12642 DTRACE_CRA_PROC; 12643 /* 12644 * Holding proc_owner means that destructive actions 12645 * for *this* zone are allowed. 12646 */ 12647 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12648 state->dts_cred.dcr_action |= 12649 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12650 12651 /* 12652 * Holding proc_zone means that destructive actions 12653 * for this user/group ID in all zones is allowed. 12654 */ 12655 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12656 state->dts_cred.dcr_action |= 12657 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12658 12659 /* 12660 * If we have all privs in whatever zone this is, 12661 * we can do destructive things to processes which 12662 * have altered credentials. 12663 */ 12664 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12665 cr->cr_zone->zone_privset)) { 12666 state->dts_cred.dcr_action |= 12667 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12668 } 12669 } 12670 12671 /* 12672 * Holding the dtrace_proc privilege gives control over fasttrap 12673 * and pid providers. We need to grant wider destructive 12674 * privileges in the event that the user has proc_owner and/or 12675 * proc_zone. 12676 */ 12677 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12678 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12679 state->dts_cred.dcr_action |= 12680 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12681 12682 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12683 state->dts_cred.dcr_action |= 12684 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12685 } 12686 } 12687 12688 return (state); 12689 } 12690 12691 static int 12692 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 12693 { 12694 dtrace_optval_t *opt = state->dts_options, size; 12695 processorid_t cpu; 12696 int flags = 0, rval, factor, divisor = 1; 12697 12698 ASSERT(MUTEX_HELD(&dtrace_lock)); 12699 ASSERT(MUTEX_HELD(&cpu_lock)); 12700 ASSERT(which < DTRACEOPT_MAX); 12701 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 12702 (state == dtrace_anon.dta_state && 12703 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 12704 12705 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 12706 return (0); 12707 12708 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 12709 cpu = opt[DTRACEOPT_CPU]; 12710 12711 if (which == DTRACEOPT_SPECSIZE) 12712 flags |= DTRACEBUF_NOSWITCH; 12713 12714 if (which == DTRACEOPT_BUFSIZE) { 12715 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 12716 flags |= DTRACEBUF_RING; 12717 12718 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 12719 flags |= DTRACEBUF_FILL; 12720 12721 if (state != dtrace_anon.dta_state || 12722 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 12723 flags |= DTRACEBUF_INACTIVE; 12724 } 12725 12726 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 12727 /* 12728 * The size must be 8-byte aligned. If the size is not 8-byte 12729 * aligned, drop it down by the difference. 12730 */ 12731 if (size & (sizeof (uint64_t) - 1)) 12732 size -= size & (sizeof (uint64_t) - 1); 12733 12734 if (size < state->dts_reserve) { 12735 /* 12736 * Buffers always must be large enough to accommodate 12737 * their prereserved space. We return E2BIG instead 12738 * of ENOMEM in this case to allow for user-level 12739 * software to differentiate the cases. 12740 */ 12741 return (E2BIG); 12742 } 12743 12744 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 12745 12746 if (rval != ENOMEM) { 12747 opt[which] = size; 12748 return (rval); 12749 } 12750 12751 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 12752 return (rval); 12753 12754 for (divisor = 2; divisor < factor; divisor <<= 1) 12755 continue; 12756 } 12757 12758 return (ENOMEM); 12759 } 12760 12761 static int 12762 dtrace_state_buffers(dtrace_state_t *state) 12763 { 12764 dtrace_speculation_t *spec = state->dts_speculations; 12765 int rval, i; 12766 12767 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 12768 DTRACEOPT_BUFSIZE)) != 0) 12769 return (rval); 12770 12771 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 12772 DTRACEOPT_AGGSIZE)) != 0) 12773 return (rval); 12774 12775 for (i = 0; i < state->dts_nspeculations; i++) { 12776 if ((rval = dtrace_state_buffer(state, 12777 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 12778 return (rval); 12779 } 12780 12781 return (0); 12782 } 12783 12784 static void 12785 dtrace_state_prereserve(dtrace_state_t *state) 12786 { 12787 dtrace_ecb_t *ecb; 12788 dtrace_probe_t *probe; 12789 12790 state->dts_reserve = 0; 12791 12792 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 12793 return; 12794 12795 /* 12796 * If our buffer policy is a "fill" buffer policy, we need to set the 12797 * prereserved space to be the space required by the END probes. 12798 */ 12799 probe = dtrace_probes[dtrace_probeid_end - 1]; 12800 ASSERT(probe != NULL); 12801 12802 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 12803 if (ecb->dte_state != state) 12804 continue; 12805 12806 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 12807 } 12808 } 12809 12810 static int 12811 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 12812 { 12813 dtrace_optval_t *opt = state->dts_options, sz, nspec; 12814 dtrace_speculation_t *spec; 12815 dtrace_buffer_t *buf; 12816 cyc_handler_t hdlr; 12817 cyc_time_t when; 12818 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 12819 dtrace_icookie_t cookie; 12820 12821 mutex_enter(&cpu_lock); 12822 mutex_enter(&dtrace_lock); 12823 12824 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 12825 rval = EBUSY; 12826 goto out; 12827 } 12828 12829 /* 12830 * Before we can perform any checks, we must prime all of the 12831 * retained enablings that correspond to this state. 12832 */ 12833 dtrace_enabling_prime(state); 12834 12835 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 12836 rval = EACCES; 12837 goto out; 12838 } 12839 12840 dtrace_state_prereserve(state); 12841 12842 /* 12843 * Now we want to do is try to allocate our speculations. 12844 * We do not automatically resize the number of speculations; if 12845 * this fails, we will fail the operation. 12846 */ 12847 nspec = opt[DTRACEOPT_NSPEC]; 12848 ASSERT(nspec != DTRACEOPT_UNSET); 12849 12850 if (nspec > INT_MAX) { 12851 rval = ENOMEM; 12852 goto out; 12853 } 12854 12855 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 12856 KM_NOSLEEP | KM_NORMALPRI); 12857 12858 if (spec == NULL) { 12859 rval = ENOMEM; 12860 goto out; 12861 } 12862 12863 state->dts_speculations = spec; 12864 state->dts_nspeculations = (int)nspec; 12865 12866 for (i = 0; i < nspec; i++) { 12867 if ((buf = kmem_zalloc(bufsize, 12868 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 12869 rval = ENOMEM; 12870 goto err; 12871 } 12872 12873 spec[i].dtsp_buffer = buf; 12874 } 12875 12876 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 12877 if (dtrace_anon.dta_state == NULL) { 12878 rval = ENOENT; 12879 goto out; 12880 } 12881 12882 if (state->dts_necbs != 0) { 12883 rval = EALREADY; 12884 goto out; 12885 } 12886 12887 state->dts_anon = dtrace_anon_grab(); 12888 ASSERT(state->dts_anon != NULL); 12889 state = state->dts_anon; 12890 12891 /* 12892 * We want "grabanon" to be set in the grabbed state, so we'll 12893 * copy that option value from the grabbing state into the 12894 * grabbed state. 12895 */ 12896 state->dts_options[DTRACEOPT_GRABANON] = 12897 opt[DTRACEOPT_GRABANON]; 12898 12899 *cpu = dtrace_anon.dta_beganon; 12900 12901 /* 12902 * If the anonymous state is active (as it almost certainly 12903 * is if the anonymous enabling ultimately matched anything), 12904 * we don't allow any further option processing -- but we 12905 * don't return failure. 12906 */ 12907 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 12908 goto out; 12909 } 12910 12911 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 12912 opt[DTRACEOPT_AGGSIZE] != 0) { 12913 if (state->dts_aggregations == NULL) { 12914 /* 12915 * We're not going to create an aggregation buffer 12916 * because we don't have any ECBs that contain 12917 * aggregations -- set this option to 0. 12918 */ 12919 opt[DTRACEOPT_AGGSIZE] = 0; 12920 } else { 12921 /* 12922 * If we have an aggregation buffer, we must also have 12923 * a buffer to use as scratch. 12924 */ 12925 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 12926 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 12927 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 12928 } 12929 } 12930 } 12931 12932 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 12933 opt[DTRACEOPT_SPECSIZE] != 0) { 12934 if (!state->dts_speculates) { 12935 /* 12936 * We're not going to create speculation buffers 12937 * because we don't have any ECBs that actually 12938 * speculate -- set the speculation size to 0. 12939 */ 12940 opt[DTRACEOPT_SPECSIZE] = 0; 12941 } 12942 } 12943 12944 /* 12945 * The bare minimum size for any buffer that we're actually going to 12946 * do anything to is sizeof (uint64_t). 12947 */ 12948 sz = sizeof (uint64_t); 12949 12950 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 12951 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 12952 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 12953 /* 12954 * A buffer size has been explicitly set to 0 (or to a size 12955 * that will be adjusted to 0) and we need the space -- we 12956 * need to return failure. We return ENOSPC to differentiate 12957 * it from failing to allocate a buffer due to failure to meet 12958 * the reserve (for which we return E2BIG). 12959 */ 12960 rval = ENOSPC; 12961 goto out; 12962 } 12963 12964 if ((rval = dtrace_state_buffers(state)) != 0) 12965 goto err; 12966 12967 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 12968 sz = dtrace_dstate_defsize; 12969 12970 do { 12971 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 12972 12973 if (rval == 0) 12974 break; 12975 12976 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 12977 goto err; 12978 } while (sz >>= 1); 12979 12980 opt[DTRACEOPT_DYNVARSIZE] = sz; 12981 12982 if (rval != 0) 12983 goto err; 12984 12985 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 12986 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 12987 12988 if (opt[DTRACEOPT_CLEANRATE] == 0) 12989 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 12990 12991 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 12992 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 12993 12994 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 12995 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 12996 12997 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 12998 hdlr.cyh_arg = state; 12999 hdlr.cyh_level = CY_LOW_LEVEL; 13000 13001 when.cyt_when = 0; 13002 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 13003 13004 state->dts_cleaner = cyclic_add(&hdlr, &when); 13005 13006 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 13007 hdlr.cyh_arg = state; 13008 hdlr.cyh_level = CY_LOW_LEVEL; 13009 13010 when.cyt_when = 0; 13011 when.cyt_interval = dtrace_deadman_interval; 13012 13013 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 13014 state->dts_deadman = cyclic_add(&hdlr, &when); 13015 13016 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 13017 13018 /* 13019 * Now it's time to actually fire the BEGIN probe. We need to disable 13020 * interrupts here both to record the CPU on which we fired the BEGIN 13021 * probe (the data from this CPU will be processed first at user 13022 * level) and to manually activate the buffer for this CPU. 13023 */ 13024 cookie = dtrace_interrupt_disable(); 13025 *cpu = CPU->cpu_id; 13026 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 13027 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 13028 13029 dtrace_probe(dtrace_probeid_begin, 13030 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13031 dtrace_interrupt_enable(cookie); 13032 /* 13033 * We may have had an exit action from a BEGIN probe; only change our 13034 * state to ACTIVE if we're still in WARMUP. 13035 */ 13036 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 13037 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 13038 13039 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 13040 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 13041 13042 /* 13043 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 13044 * want each CPU to transition its principal buffer out of the 13045 * INACTIVE state. Doing this assures that no CPU will suddenly begin 13046 * processing an ECB halfway down a probe's ECB chain; all CPUs will 13047 * atomically transition from processing none of a state's ECBs to 13048 * processing all of them. 13049 */ 13050 dtrace_xcall(DTRACE_CPUALL, 13051 (dtrace_xcall_t)dtrace_buffer_activate, state); 13052 goto out; 13053 13054 err: 13055 dtrace_buffer_free(state->dts_buffer); 13056 dtrace_buffer_free(state->dts_aggbuffer); 13057 13058 if ((nspec = state->dts_nspeculations) == 0) { 13059 ASSERT(state->dts_speculations == NULL); 13060 goto out; 13061 } 13062 13063 spec = state->dts_speculations; 13064 ASSERT(spec != NULL); 13065 13066 for (i = 0; i < state->dts_nspeculations; i++) { 13067 if ((buf = spec[i].dtsp_buffer) == NULL) 13068 break; 13069 13070 dtrace_buffer_free(buf); 13071 kmem_free(buf, bufsize); 13072 } 13073 13074 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13075 state->dts_nspeculations = 0; 13076 state->dts_speculations = NULL; 13077 13078 out: 13079 mutex_exit(&dtrace_lock); 13080 mutex_exit(&cpu_lock); 13081 13082 return (rval); 13083 } 13084 13085 static int 13086 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 13087 { 13088 dtrace_icookie_t cookie; 13089 13090 ASSERT(MUTEX_HELD(&dtrace_lock)); 13091 13092 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 13093 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 13094 return (EINVAL); 13095 13096 /* 13097 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 13098 * to be sure that every CPU has seen it. See below for the details 13099 * on why this is done. 13100 */ 13101 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 13102 dtrace_sync(); 13103 13104 /* 13105 * By this point, it is impossible for any CPU to be still processing 13106 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 13107 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 13108 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 13109 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 13110 * iff we're in the END probe. 13111 */ 13112 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 13113 dtrace_sync(); 13114 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 13115 13116 /* 13117 * Finally, we can release the reserve and call the END probe. We 13118 * disable interrupts across calling the END probe to allow us to 13119 * return the CPU on which we actually called the END probe. This 13120 * allows user-land to be sure that this CPU's principal buffer is 13121 * processed last. 13122 */ 13123 state->dts_reserve = 0; 13124 13125 cookie = dtrace_interrupt_disable(); 13126 *cpu = CPU->cpu_id; 13127 dtrace_probe(dtrace_probeid_end, 13128 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13129 dtrace_interrupt_enable(cookie); 13130 13131 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 13132 dtrace_sync(); 13133 13134 return (0); 13135 } 13136 13137 static int 13138 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 13139 dtrace_optval_t val) 13140 { 13141 ASSERT(MUTEX_HELD(&dtrace_lock)); 13142 13143 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13144 return (EBUSY); 13145 13146 if (option >= DTRACEOPT_MAX) 13147 return (EINVAL); 13148 13149 if (option != DTRACEOPT_CPU && val < 0) 13150 return (EINVAL); 13151 13152 switch (option) { 13153 case DTRACEOPT_DESTRUCTIVE: 13154 if (dtrace_destructive_disallow) 13155 return (EACCES); 13156 13157 state->dts_cred.dcr_destructive = 1; 13158 break; 13159 13160 case DTRACEOPT_BUFSIZE: 13161 case DTRACEOPT_DYNVARSIZE: 13162 case DTRACEOPT_AGGSIZE: 13163 case DTRACEOPT_SPECSIZE: 13164 case DTRACEOPT_STRSIZE: 13165 if (val < 0) 13166 return (EINVAL); 13167 13168 if (val >= LONG_MAX) { 13169 /* 13170 * If this is an otherwise negative value, set it to 13171 * the highest multiple of 128m less than LONG_MAX. 13172 * Technically, we're adjusting the size without 13173 * regard to the buffer resizing policy, but in fact, 13174 * this has no effect -- if we set the buffer size to 13175 * ~LONG_MAX and the buffer policy is ultimately set to 13176 * be "manual", the buffer allocation is guaranteed to 13177 * fail, if only because the allocation requires two 13178 * buffers. (We set the the size to the highest 13179 * multiple of 128m because it ensures that the size 13180 * will remain a multiple of a megabyte when 13181 * repeatedly halved -- all the way down to 15m.) 13182 */ 13183 val = LONG_MAX - (1 << 27) + 1; 13184 } 13185 } 13186 13187 state->dts_options[option] = val; 13188 13189 return (0); 13190 } 13191 13192 static void 13193 dtrace_state_destroy(dtrace_state_t *state) 13194 { 13195 dtrace_ecb_t *ecb; 13196 dtrace_vstate_t *vstate = &state->dts_vstate; 13197 minor_t minor = getminor(state->dts_dev); 13198 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13199 dtrace_speculation_t *spec = state->dts_speculations; 13200 int nspec = state->dts_nspeculations; 13201 uint32_t match; 13202 13203 ASSERT(MUTEX_HELD(&dtrace_lock)); 13204 ASSERT(MUTEX_HELD(&cpu_lock)); 13205 13206 /* 13207 * First, retract any retained enablings for this state. 13208 */ 13209 dtrace_enabling_retract(state); 13210 ASSERT(state->dts_nretained == 0); 13211 13212 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 13213 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 13214 /* 13215 * We have managed to come into dtrace_state_destroy() on a 13216 * hot enabling -- almost certainly because of a disorderly 13217 * shutdown of a consumer. (That is, a consumer that is 13218 * exiting without having called dtrace_stop().) In this case, 13219 * we're going to set our activity to be KILLED, and then 13220 * issue a sync to be sure that everyone is out of probe 13221 * context before we start blowing away ECBs. 13222 */ 13223 state->dts_activity = DTRACE_ACTIVITY_KILLED; 13224 dtrace_sync(); 13225 } 13226 13227 /* 13228 * Release the credential hold we took in dtrace_state_create(). 13229 */ 13230 if (state->dts_cred.dcr_cred != NULL) 13231 crfree(state->dts_cred.dcr_cred); 13232 13233 /* 13234 * Now we can safely disable and destroy any enabled probes. Because 13235 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 13236 * (especially if they're all enabled), we take two passes through the 13237 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 13238 * in the second we disable whatever is left over. 13239 */ 13240 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 13241 for (i = 0; i < state->dts_necbs; i++) { 13242 if ((ecb = state->dts_ecbs[i]) == NULL) 13243 continue; 13244 13245 if (match && ecb->dte_probe != NULL) { 13246 dtrace_probe_t *probe = ecb->dte_probe; 13247 dtrace_provider_t *prov = probe->dtpr_provider; 13248 13249 if (!(prov->dtpv_priv.dtpp_flags & match)) 13250 continue; 13251 } 13252 13253 dtrace_ecb_disable(ecb); 13254 dtrace_ecb_destroy(ecb); 13255 } 13256 13257 if (!match) 13258 break; 13259 } 13260 13261 /* 13262 * Before we free the buffers, perform one more sync to assure that 13263 * every CPU is out of probe context. 13264 */ 13265 dtrace_sync(); 13266 13267 dtrace_buffer_free(state->dts_buffer); 13268 dtrace_buffer_free(state->dts_aggbuffer); 13269 13270 for (i = 0; i < nspec; i++) 13271 dtrace_buffer_free(spec[i].dtsp_buffer); 13272 13273 if (state->dts_cleaner != CYCLIC_NONE) 13274 cyclic_remove(state->dts_cleaner); 13275 13276 if (state->dts_deadman != CYCLIC_NONE) 13277 cyclic_remove(state->dts_deadman); 13278 13279 dtrace_dstate_fini(&vstate->dtvs_dynvars); 13280 dtrace_vstate_fini(vstate); 13281 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 13282 13283 if (state->dts_aggregations != NULL) { 13284 #ifdef DEBUG 13285 for (i = 0; i < state->dts_naggregations; i++) 13286 ASSERT(state->dts_aggregations[i] == NULL); 13287 #endif 13288 ASSERT(state->dts_naggregations > 0); 13289 kmem_free(state->dts_aggregations, 13290 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 13291 } 13292 13293 kmem_free(state->dts_buffer, bufsize); 13294 kmem_free(state->dts_aggbuffer, bufsize); 13295 13296 for (i = 0; i < nspec; i++) 13297 kmem_free(spec[i].dtsp_buffer, bufsize); 13298 13299 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13300 13301 dtrace_format_destroy(state); 13302 13303 vmem_destroy(state->dts_aggid_arena); 13304 ddi_soft_state_free(dtrace_softstate, minor); 13305 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 13306 } 13307 13308 /* 13309 * DTrace Anonymous Enabling Functions 13310 */ 13311 static dtrace_state_t * 13312 dtrace_anon_grab(void) 13313 { 13314 dtrace_state_t *state; 13315 13316 ASSERT(MUTEX_HELD(&dtrace_lock)); 13317 13318 if ((state = dtrace_anon.dta_state) == NULL) { 13319 ASSERT(dtrace_anon.dta_enabling == NULL); 13320 return (NULL); 13321 } 13322 13323 ASSERT(dtrace_anon.dta_enabling != NULL); 13324 ASSERT(dtrace_retained != NULL); 13325 13326 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 13327 dtrace_anon.dta_enabling = NULL; 13328 dtrace_anon.dta_state = NULL; 13329 13330 return (state); 13331 } 13332 13333 static void 13334 dtrace_anon_property(void) 13335 { 13336 int i, rv; 13337 dtrace_state_t *state; 13338 dof_hdr_t *dof; 13339 char c[32]; /* enough for "dof-data-" + digits */ 13340 13341 ASSERT(MUTEX_HELD(&dtrace_lock)); 13342 ASSERT(MUTEX_HELD(&cpu_lock)); 13343 13344 for (i = 0; ; i++) { 13345 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 13346 13347 dtrace_err_verbose = 1; 13348 13349 if ((dof = dtrace_dof_property(c)) == NULL) { 13350 dtrace_err_verbose = 0; 13351 break; 13352 } 13353 13354 /* 13355 * We want to create anonymous state, so we need to transition 13356 * the kernel debugger to indicate that DTrace is active. If 13357 * this fails (e.g. because the debugger has modified text in 13358 * some way), we won't continue with the processing. 13359 */ 13360 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13361 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 13362 "enabling ignored."); 13363 dtrace_dof_destroy(dof); 13364 break; 13365 } 13366 13367 /* 13368 * If we haven't allocated an anonymous state, we'll do so now. 13369 */ 13370 if ((state = dtrace_anon.dta_state) == NULL) { 13371 state = dtrace_state_create(NULL, NULL); 13372 dtrace_anon.dta_state = state; 13373 13374 if (state == NULL) { 13375 /* 13376 * This basically shouldn't happen: the only 13377 * failure mode from dtrace_state_create() is a 13378 * failure of ddi_soft_state_zalloc() that 13379 * itself should never happen. Still, the 13380 * interface allows for a failure mode, and 13381 * we want to fail as gracefully as possible: 13382 * we'll emit an error message and cease 13383 * processing anonymous state in this case. 13384 */ 13385 cmn_err(CE_WARN, "failed to create " 13386 "anonymous state"); 13387 dtrace_dof_destroy(dof); 13388 break; 13389 } 13390 } 13391 13392 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 13393 &dtrace_anon.dta_enabling, 0, B_TRUE); 13394 13395 if (rv == 0) 13396 rv = dtrace_dof_options(dof, state); 13397 13398 dtrace_err_verbose = 0; 13399 dtrace_dof_destroy(dof); 13400 13401 if (rv != 0) { 13402 /* 13403 * This is malformed DOF; chuck any anonymous state 13404 * that we created. 13405 */ 13406 ASSERT(dtrace_anon.dta_enabling == NULL); 13407 dtrace_state_destroy(state); 13408 dtrace_anon.dta_state = NULL; 13409 break; 13410 } 13411 13412 ASSERT(dtrace_anon.dta_enabling != NULL); 13413 } 13414 13415 if (dtrace_anon.dta_enabling != NULL) { 13416 int rval; 13417 13418 /* 13419 * dtrace_enabling_retain() can only fail because we are 13420 * trying to retain more enablings than are allowed -- but 13421 * we only have one anonymous enabling, and we are guaranteed 13422 * to be allowed at least one retained enabling; we assert 13423 * that dtrace_enabling_retain() returns success. 13424 */ 13425 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 13426 ASSERT(rval == 0); 13427 13428 dtrace_enabling_dump(dtrace_anon.dta_enabling); 13429 } 13430 } 13431 13432 /* 13433 * DTrace Helper Functions 13434 */ 13435 static void 13436 dtrace_helper_trace(dtrace_helper_action_t *helper, 13437 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 13438 { 13439 uint32_t size, next, nnext, i; 13440 dtrace_helptrace_t *ent; 13441 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 13442 13443 if (!dtrace_helptrace_enabled) 13444 return; 13445 13446 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 13447 13448 /* 13449 * What would a tracing framework be without its own tracing 13450 * framework? (Well, a hell of a lot simpler, for starters...) 13451 */ 13452 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 13453 sizeof (uint64_t) - sizeof (uint64_t); 13454 13455 /* 13456 * Iterate until we can allocate a slot in the trace buffer. 13457 */ 13458 do { 13459 next = dtrace_helptrace_next; 13460 13461 if (next + size < dtrace_helptrace_bufsize) { 13462 nnext = next + size; 13463 } else { 13464 nnext = size; 13465 } 13466 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 13467 13468 /* 13469 * We have our slot; fill it in. 13470 */ 13471 if (nnext == size) 13472 next = 0; 13473 13474 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 13475 ent->dtht_helper = helper; 13476 ent->dtht_where = where; 13477 ent->dtht_nlocals = vstate->dtvs_nlocals; 13478 13479 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 13480 mstate->dtms_fltoffs : -1; 13481 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 13482 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 13483 13484 for (i = 0; i < vstate->dtvs_nlocals; i++) { 13485 dtrace_statvar_t *svar; 13486 13487 if ((svar = vstate->dtvs_locals[i]) == NULL) 13488 continue; 13489 13490 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 13491 ent->dtht_locals[i] = 13492 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; 13493 } 13494 } 13495 13496 static uint64_t 13497 dtrace_helper(int which, dtrace_mstate_t *mstate, 13498 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 13499 { 13500 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 13501 uint64_t sarg0 = mstate->dtms_arg[0]; 13502 uint64_t sarg1 = mstate->dtms_arg[1]; 13503 uint64_t rval; 13504 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 13505 dtrace_helper_action_t *helper; 13506 dtrace_vstate_t *vstate; 13507 dtrace_difo_t *pred; 13508 int i, trace = dtrace_helptrace_enabled; 13509 13510 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 13511 13512 if (helpers == NULL) 13513 return (0); 13514 13515 if ((helper = helpers->dthps_actions[which]) == NULL) 13516 return (0); 13517 13518 vstate = &helpers->dthps_vstate; 13519 mstate->dtms_arg[0] = arg0; 13520 mstate->dtms_arg[1] = arg1; 13521 13522 /* 13523 * Now iterate over each helper. If its predicate evaluates to 'true', 13524 * we'll call the corresponding actions. Note that the below calls 13525 * to dtrace_dif_emulate() may set faults in machine state. This is 13526 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 13527 * the stored DIF offset with its own (which is the desired behavior). 13528 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 13529 * from machine state; this is okay, too. 13530 */ 13531 for (; helper != NULL; helper = helper->dtha_next) { 13532 if ((pred = helper->dtha_predicate) != NULL) { 13533 if (trace) 13534 dtrace_helper_trace(helper, mstate, vstate, 0); 13535 13536 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 13537 goto next; 13538 13539 if (*flags & CPU_DTRACE_FAULT) 13540 goto err; 13541 } 13542 13543 for (i = 0; i < helper->dtha_nactions; i++) { 13544 if (trace) 13545 dtrace_helper_trace(helper, 13546 mstate, vstate, i + 1); 13547 13548 rval = dtrace_dif_emulate(helper->dtha_actions[i], 13549 mstate, vstate, state); 13550 13551 if (*flags & CPU_DTRACE_FAULT) 13552 goto err; 13553 } 13554 13555 next: 13556 if (trace) 13557 dtrace_helper_trace(helper, mstate, vstate, 13558 DTRACE_HELPTRACE_NEXT); 13559 } 13560 13561 if (trace) 13562 dtrace_helper_trace(helper, mstate, vstate, 13563 DTRACE_HELPTRACE_DONE); 13564 13565 /* 13566 * Restore the arg0 that we saved upon entry. 13567 */ 13568 mstate->dtms_arg[0] = sarg0; 13569 mstate->dtms_arg[1] = sarg1; 13570 13571 return (rval); 13572 13573 err: 13574 if (trace) 13575 dtrace_helper_trace(helper, mstate, vstate, 13576 DTRACE_HELPTRACE_ERR); 13577 13578 /* 13579 * Restore the arg0 that we saved upon entry. 13580 */ 13581 mstate->dtms_arg[0] = sarg0; 13582 mstate->dtms_arg[1] = sarg1; 13583 13584 return (NULL); 13585 } 13586 13587 static void 13588 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 13589 dtrace_vstate_t *vstate) 13590 { 13591 int i; 13592 13593 if (helper->dtha_predicate != NULL) 13594 dtrace_difo_release(helper->dtha_predicate, vstate); 13595 13596 for (i = 0; i < helper->dtha_nactions; i++) { 13597 ASSERT(helper->dtha_actions[i] != NULL); 13598 dtrace_difo_release(helper->dtha_actions[i], vstate); 13599 } 13600 13601 kmem_free(helper->dtha_actions, 13602 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 13603 kmem_free(helper, sizeof (dtrace_helper_action_t)); 13604 } 13605 13606 static int 13607 dtrace_helper_destroygen(int gen) 13608 { 13609 proc_t *p = curproc; 13610 dtrace_helpers_t *help = p->p_dtrace_helpers; 13611 dtrace_vstate_t *vstate; 13612 int i; 13613 13614 ASSERT(MUTEX_HELD(&dtrace_lock)); 13615 13616 if (help == NULL || gen > help->dthps_generation) 13617 return (EINVAL); 13618 13619 vstate = &help->dthps_vstate; 13620 13621 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 13622 dtrace_helper_action_t *last = NULL, *h, *next; 13623 13624 for (h = help->dthps_actions[i]; h != NULL; h = next) { 13625 next = h->dtha_next; 13626 13627 if (h->dtha_generation == gen) { 13628 if (last != NULL) { 13629 last->dtha_next = next; 13630 } else { 13631 help->dthps_actions[i] = next; 13632 } 13633 13634 dtrace_helper_action_destroy(h, vstate); 13635 } else { 13636 last = h; 13637 } 13638 } 13639 } 13640 13641 /* 13642 * Interate until we've cleared out all helper providers with the 13643 * given generation number. 13644 */ 13645 for (;;) { 13646 dtrace_helper_provider_t *prov; 13647 13648 /* 13649 * Look for a helper provider with the right generation. We 13650 * have to start back at the beginning of the list each time 13651 * because we drop dtrace_lock. It's unlikely that we'll make 13652 * more than two passes. 13653 */ 13654 for (i = 0; i < help->dthps_nprovs; i++) { 13655 prov = help->dthps_provs[i]; 13656 13657 if (prov->dthp_generation == gen) 13658 break; 13659 } 13660 13661 /* 13662 * If there were no matches, we're done. 13663 */ 13664 if (i == help->dthps_nprovs) 13665 break; 13666 13667 /* 13668 * Move the last helper provider into this slot. 13669 */ 13670 help->dthps_nprovs--; 13671 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 13672 help->dthps_provs[help->dthps_nprovs] = NULL; 13673 13674 mutex_exit(&dtrace_lock); 13675 13676 /* 13677 * If we have a meta provider, remove this helper provider. 13678 */ 13679 mutex_enter(&dtrace_meta_lock); 13680 if (dtrace_meta_pid != NULL) { 13681 ASSERT(dtrace_deferred_pid == NULL); 13682 dtrace_helper_provider_remove(&prov->dthp_prov, 13683 p->p_pid); 13684 } 13685 mutex_exit(&dtrace_meta_lock); 13686 13687 dtrace_helper_provider_destroy(prov); 13688 13689 mutex_enter(&dtrace_lock); 13690 } 13691 13692 return (0); 13693 } 13694 13695 static int 13696 dtrace_helper_validate(dtrace_helper_action_t *helper) 13697 { 13698 int err = 0, i; 13699 dtrace_difo_t *dp; 13700 13701 if ((dp = helper->dtha_predicate) != NULL) 13702 err += dtrace_difo_validate_helper(dp); 13703 13704 for (i = 0; i < helper->dtha_nactions; i++) 13705 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 13706 13707 return (err == 0); 13708 } 13709 13710 static int 13711 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 13712 { 13713 dtrace_helpers_t *help; 13714 dtrace_helper_action_t *helper, *last; 13715 dtrace_actdesc_t *act; 13716 dtrace_vstate_t *vstate; 13717 dtrace_predicate_t *pred; 13718 int count = 0, nactions = 0, i; 13719 13720 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 13721 return (EINVAL); 13722 13723 help = curproc->p_dtrace_helpers; 13724 last = help->dthps_actions[which]; 13725 vstate = &help->dthps_vstate; 13726 13727 for (count = 0; last != NULL; last = last->dtha_next) { 13728 count++; 13729 if (last->dtha_next == NULL) 13730 break; 13731 } 13732 13733 /* 13734 * If we already have dtrace_helper_actions_max helper actions for this 13735 * helper action type, we'll refuse to add a new one. 13736 */ 13737 if (count >= dtrace_helper_actions_max) 13738 return (ENOSPC); 13739 13740 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 13741 helper->dtha_generation = help->dthps_generation; 13742 13743 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 13744 ASSERT(pred->dtp_difo != NULL); 13745 dtrace_difo_hold(pred->dtp_difo); 13746 helper->dtha_predicate = pred->dtp_difo; 13747 } 13748 13749 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 13750 if (act->dtad_kind != DTRACEACT_DIFEXPR) 13751 goto err; 13752 13753 if (act->dtad_difo == NULL) 13754 goto err; 13755 13756 nactions++; 13757 } 13758 13759 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 13760 (helper->dtha_nactions = nactions), KM_SLEEP); 13761 13762 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 13763 dtrace_difo_hold(act->dtad_difo); 13764 helper->dtha_actions[i++] = act->dtad_difo; 13765 } 13766 13767 if (!dtrace_helper_validate(helper)) 13768 goto err; 13769 13770 if (last == NULL) { 13771 help->dthps_actions[which] = helper; 13772 } else { 13773 last->dtha_next = helper; 13774 } 13775 13776 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 13777 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 13778 dtrace_helptrace_next = 0; 13779 } 13780 13781 return (0); 13782 err: 13783 dtrace_helper_action_destroy(helper, vstate); 13784 return (EINVAL); 13785 } 13786 13787 static void 13788 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 13789 dof_helper_t *dofhp) 13790 { 13791 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 13792 13793 mutex_enter(&dtrace_meta_lock); 13794 mutex_enter(&dtrace_lock); 13795 13796 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 13797 /* 13798 * If the dtrace module is loaded but not attached, or if 13799 * there aren't isn't a meta provider registered to deal with 13800 * these provider descriptions, we need to postpone creating 13801 * the actual providers until later. 13802 */ 13803 13804 if (help->dthps_next == NULL && help->dthps_prev == NULL && 13805 dtrace_deferred_pid != help) { 13806 help->dthps_deferred = 1; 13807 help->dthps_pid = p->p_pid; 13808 help->dthps_next = dtrace_deferred_pid; 13809 help->dthps_prev = NULL; 13810 if (dtrace_deferred_pid != NULL) 13811 dtrace_deferred_pid->dthps_prev = help; 13812 dtrace_deferred_pid = help; 13813 } 13814 13815 mutex_exit(&dtrace_lock); 13816 13817 } else if (dofhp != NULL) { 13818 /* 13819 * If the dtrace module is loaded and we have a particular 13820 * helper provider description, pass that off to the 13821 * meta provider. 13822 */ 13823 13824 mutex_exit(&dtrace_lock); 13825 13826 dtrace_helper_provide(dofhp, p->p_pid); 13827 13828 } else { 13829 /* 13830 * Otherwise, just pass all the helper provider descriptions 13831 * off to the meta provider. 13832 */ 13833 13834 int i; 13835 mutex_exit(&dtrace_lock); 13836 13837 for (i = 0; i < help->dthps_nprovs; i++) { 13838 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 13839 p->p_pid); 13840 } 13841 } 13842 13843 mutex_exit(&dtrace_meta_lock); 13844 } 13845 13846 static int 13847 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 13848 { 13849 dtrace_helpers_t *help; 13850 dtrace_helper_provider_t *hprov, **tmp_provs; 13851 uint_t tmp_maxprovs, i; 13852 13853 ASSERT(MUTEX_HELD(&dtrace_lock)); 13854 13855 help = curproc->p_dtrace_helpers; 13856 ASSERT(help != NULL); 13857 13858 /* 13859 * If we already have dtrace_helper_providers_max helper providers, 13860 * we're refuse to add a new one. 13861 */ 13862 if (help->dthps_nprovs >= dtrace_helper_providers_max) 13863 return (ENOSPC); 13864 13865 /* 13866 * Check to make sure this isn't a duplicate. 13867 */ 13868 for (i = 0; i < help->dthps_nprovs; i++) { 13869 if (dofhp->dofhp_addr == 13870 help->dthps_provs[i]->dthp_prov.dofhp_addr) 13871 return (EALREADY); 13872 } 13873 13874 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 13875 hprov->dthp_prov = *dofhp; 13876 hprov->dthp_ref = 1; 13877 hprov->dthp_generation = gen; 13878 13879 /* 13880 * Allocate a bigger table for helper providers if it's already full. 13881 */ 13882 if (help->dthps_maxprovs == help->dthps_nprovs) { 13883 tmp_maxprovs = help->dthps_maxprovs; 13884 tmp_provs = help->dthps_provs; 13885 13886 if (help->dthps_maxprovs == 0) 13887 help->dthps_maxprovs = 2; 13888 else 13889 help->dthps_maxprovs *= 2; 13890 if (help->dthps_maxprovs > dtrace_helper_providers_max) 13891 help->dthps_maxprovs = dtrace_helper_providers_max; 13892 13893 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 13894 13895 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 13896 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 13897 13898 if (tmp_provs != NULL) { 13899 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 13900 sizeof (dtrace_helper_provider_t *)); 13901 kmem_free(tmp_provs, tmp_maxprovs * 13902 sizeof (dtrace_helper_provider_t *)); 13903 } 13904 } 13905 13906 help->dthps_provs[help->dthps_nprovs] = hprov; 13907 help->dthps_nprovs++; 13908 13909 return (0); 13910 } 13911 13912 static void 13913 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 13914 { 13915 mutex_enter(&dtrace_lock); 13916 13917 if (--hprov->dthp_ref == 0) { 13918 dof_hdr_t *dof; 13919 mutex_exit(&dtrace_lock); 13920 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 13921 dtrace_dof_destroy(dof); 13922 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 13923 } else { 13924 mutex_exit(&dtrace_lock); 13925 } 13926 } 13927 13928 static int 13929 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 13930 { 13931 uintptr_t daddr = (uintptr_t)dof; 13932 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 13933 dof_provider_t *provider; 13934 dof_probe_t *probe; 13935 uint8_t *arg; 13936 char *strtab, *typestr; 13937 dof_stridx_t typeidx; 13938 size_t typesz; 13939 uint_t nprobes, j, k; 13940 13941 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 13942 13943 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 13944 dtrace_dof_error(dof, "misaligned section offset"); 13945 return (-1); 13946 } 13947 13948 /* 13949 * The section needs to be large enough to contain the DOF provider 13950 * structure appropriate for the given version. 13951 */ 13952 if (sec->dofs_size < 13953 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 13954 offsetof(dof_provider_t, dofpv_prenoffs) : 13955 sizeof (dof_provider_t))) { 13956 dtrace_dof_error(dof, "provider section too small"); 13957 return (-1); 13958 } 13959 13960 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 13961 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 13962 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 13963 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 13964 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 13965 13966 if (str_sec == NULL || prb_sec == NULL || 13967 arg_sec == NULL || off_sec == NULL) 13968 return (-1); 13969 13970 enoff_sec = NULL; 13971 13972 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 13973 provider->dofpv_prenoffs != DOF_SECT_NONE && 13974 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 13975 provider->dofpv_prenoffs)) == NULL) 13976 return (-1); 13977 13978 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 13979 13980 if (provider->dofpv_name >= str_sec->dofs_size || 13981 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 13982 dtrace_dof_error(dof, "invalid provider name"); 13983 return (-1); 13984 } 13985 13986 if (prb_sec->dofs_entsize == 0 || 13987 prb_sec->dofs_entsize > prb_sec->dofs_size) { 13988 dtrace_dof_error(dof, "invalid entry size"); 13989 return (-1); 13990 } 13991 13992 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 13993 dtrace_dof_error(dof, "misaligned entry size"); 13994 return (-1); 13995 } 13996 13997 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 13998 dtrace_dof_error(dof, "invalid entry size"); 13999 return (-1); 14000 } 14001 14002 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 14003 dtrace_dof_error(dof, "misaligned section offset"); 14004 return (-1); 14005 } 14006 14007 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 14008 dtrace_dof_error(dof, "invalid entry size"); 14009 return (-1); 14010 } 14011 14012 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 14013 14014 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 14015 14016 /* 14017 * Take a pass through the probes to check for errors. 14018 */ 14019 for (j = 0; j < nprobes; j++) { 14020 probe = (dof_probe_t *)(uintptr_t)(daddr + 14021 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 14022 14023 if (probe->dofpr_func >= str_sec->dofs_size) { 14024 dtrace_dof_error(dof, "invalid function name"); 14025 return (-1); 14026 } 14027 14028 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 14029 dtrace_dof_error(dof, "function name too long"); 14030 return (-1); 14031 } 14032 14033 if (probe->dofpr_name >= str_sec->dofs_size || 14034 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 14035 dtrace_dof_error(dof, "invalid probe name"); 14036 return (-1); 14037 } 14038 14039 /* 14040 * The offset count must not wrap the index, and the offsets 14041 * must also not overflow the section's data. 14042 */ 14043 if (probe->dofpr_offidx + probe->dofpr_noffs < 14044 probe->dofpr_offidx || 14045 (probe->dofpr_offidx + probe->dofpr_noffs) * 14046 off_sec->dofs_entsize > off_sec->dofs_size) { 14047 dtrace_dof_error(dof, "invalid probe offset"); 14048 return (-1); 14049 } 14050 14051 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 14052 /* 14053 * If there's no is-enabled offset section, make sure 14054 * there aren't any is-enabled offsets. Otherwise 14055 * perform the same checks as for probe offsets 14056 * (immediately above). 14057 */ 14058 if (enoff_sec == NULL) { 14059 if (probe->dofpr_enoffidx != 0 || 14060 probe->dofpr_nenoffs != 0) { 14061 dtrace_dof_error(dof, "is-enabled " 14062 "offsets with null section"); 14063 return (-1); 14064 } 14065 } else if (probe->dofpr_enoffidx + 14066 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 14067 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 14068 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 14069 dtrace_dof_error(dof, "invalid is-enabled " 14070 "offset"); 14071 return (-1); 14072 } 14073 14074 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 14075 dtrace_dof_error(dof, "zero probe and " 14076 "is-enabled offsets"); 14077 return (-1); 14078 } 14079 } else if (probe->dofpr_noffs == 0) { 14080 dtrace_dof_error(dof, "zero probe offsets"); 14081 return (-1); 14082 } 14083 14084 if (probe->dofpr_argidx + probe->dofpr_xargc < 14085 probe->dofpr_argidx || 14086 (probe->dofpr_argidx + probe->dofpr_xargc) * 14087 arg_sec->dofs_entsize > arg_sec->dofs_size) { 14088 dtrace_dof_error(dof, "invalid args"); 14089 return (-1); 14090 } 14091 14092 typeidx = probe->dofpr_nargv; 14093 typestr = strtab + probe->dofpr_nargv; 14094 for (k = 0; k < probe->dofpr_nargc; k++) { 14095 if (typeidx >= str_sec->dofs_size) { 14096 dtrace_dof_error(dof, "bad " 14097 "native argument type"); 14098 return (-1); 14099 } 14100 14101 typesz = strlen(typestr) + 1; 14102 if (typesz > DTRACE_ARGTYPELEN) { 14103 dtrace_dof_error(dof, "native " 14104 "argument type too long"); 14105 return (-1); 14106 } 14107 typeidx += typesz; 14108 typestr += typesz; 14109 } 14110 14111 typeidx = probe->dofpr_xargv; 14112 typestr = strtab + probe->dofpr_xargv; 14113 for (k = 0; k < probe->dofpr_xargc; k++) { 14114 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 14115 dtrace_dof_error(dof, "bad " 14116 "native argument index"); 14117 return (-1); 14118 } 14119 14120 if (typeidx >= str_sec->dofs_size) { 14121 dtrace_dof_error(dof, "bad " 14122 "translated argument type"); 14123 return (-1); 14124 } 14125 14126 typesz = strlen(typestr) + 1; 14127 if (typesz > DTRACE_ARGTYPELEN) { 14128 dtrace_dof_error(dof, "translated argument " 14129 "type too long"); 14130 return (-1); 14131 } 14132 14133 typeidx += typesz; 14134 typestr += typesz; 14135 } 14136 } 14137 14138 return (0); 14139 } 14140 14141 static int 14142 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 14143 { 14144 dtrace_helpers_t *help; 14145 dtrace_vstate_t *vstate; 14146 dtrace_enabling_t *enab = NULL; 14147 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 14148 uintptr_t daddr = (uintptr_t)dof; 14149 14150 ASSERT(MUTEX_HELD(&dtrace_lock)); 14151 14152 if ((help = curproc->p_dtrace_helpers) == NULL) 14153 help = dtrace_helpers_create(curproc); 14154 14155 vstate = &help->dthps_vstate; 14156 14157 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 14158 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 14159 dtrace_dof_destroy(dof); 14160 return (rv); 14161 } 14162 14163 /* 14164 * Look for helper providers and validate their descriptions. 14165 */ 14166 if (dhp != NULL) { 14167 for (i = 0; i < dof->dofh_secnum; i++) { 14168 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 14169 dof->dofh_secoff + i * dof->dofh_secsize); 14170 14171 if (sec->dofs_type != DOF_SECT_PROVIDER) 14172 continue; 14173 14174 if (dtrace_helper_provider_validate(dof, sec) != 0) { 14175 dtrace_enabling_destroy(enab); 14176 dtrace_dof_destroy(dof); 14177 return (-1); 14178 } 14179 14180 nprovs++; 14181 } 14182 } 14183 14184 /* 14185 * Now we need to walk through the ECB descriptions in the enabling. 14186 */ 14187 for (i = 0; i < enab->dten_ndesc; i++) { 14188 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 14189 dtrace_probedesc_t *desc = &ep->dted_probe; 14190 14191 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 14192 continue; 14193 14194 if (strcmp(desc->dtpd_mod, "helper") != 0) 14195 continue; 14196 14197 if (strcmp(desc->dtpd_func, "ustack") != 0) 14198 continue; 14199 14200 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 14201 ep)) != 0) { 14202 /* 14203 * Adding this helper action failed -- we are now going 14204 * to rip out the entire generation and return failure. 14205 */ 14206 (void) dtrace_helper_destroygen(help->dthps_generation); 14207 dtrace_enabling_destroy(enab); 14208 dtrace_dof_destroy(dof); 14209 return (-1); 14210 } 14211 14212 nhelpers++; 14213 } 14214 14215 if (nhelpers < enab->dten_ndesc) 14216 dtrace_dof_error(dof, "unmatched helpers"); 14217 14218 gen = help->dthps_generation++; 14219 dtrace_enabling_destroy(enab); 14220 14221 if (dhp != NULL && nprovs > 0) { 14222 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 14223 if (dtrace_helper_provider_add(dhp, gen) == 0) { 14224 mutex_exit(&dtrace_lock); 14225 dtrace_helper_provider_register(curproc, help, dhp); 14226 mutex_enter(&dtrace_lock); 14227 14228 destroy = 0; 14229 } 14230 } 14231 14232 if (destroy) 14233 dtrace_dof_destroy(dof); 14234 14235 return (gen); 14236 } 14237 14238 static dtrace_helpers_t * 14239 dtrace_helpers_create(proc_t *p) 14240 { 14241 dtrace_helpers_t *help; 14242 14243 ASSERT(MUTEX_HELD(&dtrace_lock)); 14244 ASSERT(p->p_dtrace_helpers == NULL); 14245 14246 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 14247 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 14248 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 14249 14250 p->p_dtrace_helpers = help; 14251 dtrace_helpers++; 14252 14253 return (help); 14254 } 14255 14256 static void 14257 dtrace_helpers_destroy(void) 14258 { 14259 dtrace_helpers_t *help; 14260 dtrace_vstate_t *vstate; 14261 proc_t *p = curproc; 14262 int i; 14263 14264 mutex_enter(&dtrace_lock); 14265 14266 ASSERT(p->p_dtrace_helpers != NULL); 14267 ASSERT(dtrace_helpers > 0); 14268 14269 help = p->p_dtrace_helpers; 14270 vstate = &help->dthps_vstate; 14271 14272 /* 14273 * We're now going to lose the help from this process. 14274 */ 14275 p->p_dtrace_helpers = NULL; 14276 dtrace_sync(); 14277 14278 /* 14279 * Destory the helper actions. 14280 */ 14281 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14282 dtrace_helper_action_t *h, *next; 14283 14284 for (h = help->dthps_actions[i]; h != NULL; h = next) { 14285 next = h->dtha_next; 14286 dtrace_helper_action_destroy(h, vstate); 14287 h = next; 14288 } 14289 } 14290 14291 mutex_exit(&dtrace_lock); 14292 14293 /* 14294 * Destroy the helper providers. 14295 */ 14296 if (help->dthps_maxprovs > 0) { 14297 mutex_enter(&dtrace_meta_lock); 14298 if (dtrace_meta_pid != NULL) { 14299 ASSERT(dtrace_deferred_pid == NULL); 14300 14301 for (i = 0; i < help->dthps_nprovs; i++) { 14302 dtrace_helper_provider_remove( 14303 &help->dthps_provs[i]->dthp_prov, p->p_pid); 14304 } 14305 } else { 14306 mutex_enter(&dtrace_lock); 14307 ASSERT(help->dthps_deferred == 0 || 14308 help->dthps_next != NULL || 14309 help->dthps_prev != NULL || 14310 help == dtrace_deferred_pid); 14311 14312 /* 14313 * Remove the helper from the deferred list. 14314 */ 14315 if (help->dthps_next != NULL) 14316 help->dthps_next->dthps_prev = help->dthps_prev; 14317 if (help->dthps_prev != NULL) 14318 help->dthps_prev->dthps_next = help->dthps_next; 14319 if (dtrace_deferred_pid == help) { 14320 dtrace_deferred_pid = help->dthps_next; 14321 ASSERT(help->dthps_prev == NULL); 14322 } 14323 14324 mutex_exit(&dtrace_lock); 14325 } 14326 14327 mutex_exit(&dtrace_meta_lock); 14328 14329 for (i = 0; i < help->dthps_nprovs; i++) { 14330 dtrace_helper_provider_destroy(help->dthps_provs[i]); 14331 } 14332 14333 kmem_free(help->dthps_provs, help->dthps_maxprovs * 14334 sizeof (dtrace_helper_provider_t *)); 14335 } 14336 14337 mutex_enter(&dtrace_lock); 14338 14339 dtrace_vstate_fini(&help->dthps_vstate); 14340 kmem_free(help->dthps_actions, 14341 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 14342 kmem_free(help, sizeof (dtrace_helpers_t)); 14343 14344 --dtrace_helpers; 14345 mutex_exit(&dtrace_lock); 14346 } 14347 14348 static void 14349 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 14350 { 14351 dtrace_helpers_t *help, *newhelp; 14352 dtrace_helper_action_t *helper, *new, *last; 14353 dtrace_difo_t *dp; 14354 dtrace_vstate_t *vstate; 14355 int i, j, sz, hasprovs = 0; 14356 14357 mutex_enter(&dtrace_lock); 14358 ASSERT(from->p_dtrace_helpers != NULL); 14359 ASSERT(dtrace_helpers > 0); 14360 14361 help = from->p_dtrace_helpers; 14362 newhelp = dtrace_helpers_create(to); 14363 ASSERT(to->p_dtrace_helpers != NULL); 14364 14365 newhelp->dthps_generation = help->dthps_generation; 14366 vstate = &newhelp->dthps_vstate; 14367 14368 /* 14369 * Duplicate the helper actions. 14370 */ 14371 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14372 if ((helper = help->dthps_actions[i]) == NULL) 14373 continue; 14374 14375 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 14376 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 14377 KM_SLEEP); 14378 new->dtha_generation = helper->dtha_generation; 14379 14380 if ((dp = helper->dtha_predicate) != NULL) { 14381 dp = dtrace_difo_duplicate(dp, vstate); 14382 new->dtha_predicate = dp; 14383 } 14384 14385 new->dtha_nactions = helper->dtha_nactions; 14386 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 14387 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 14388 14389 for (j = 0; j < new->dtha_nactions; j++) { 14390 dtrace_difo_t *dp = helper->dtha_actions[j]; 14391 14392 ASSERT(dp != NULL); 14393 dp = dtrace_difo_duplicate(dp, vstate); 14394 new->dtha_actions[j] = dp; 14395 } 14396 14397 if (last != NULL) { 14398 last->dtha_next = new; 14399 } else { 14400 newhelp->dthps_actions[i] = new; 14401 } 14402 14403 last = new; 14404 } 14405 } 14406 14407 /* 14408 * Duplicate the helper providers and register them with the 14409 * DTrace framework. 14410 */ 14411 if (help->dthps_nprovs > 0) { 14412 newhelp->dthps_nprovs = help->dthps_nprovs; 14413 newhelp->dthps_maxprovs = help->dthps_nprovs; 14414 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 14415 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14416 for (i = 0; i < newhelp->dthps_nprovs; i++) { 14417 newhelp->dthps_provs[i] = help->dthps_provs[i]; 14418 newhelp->dthps_provs[i]->dthp_ref++; 14419 } 14420 14421 hasprovs = 1; 14422 } 14423 14424 mutex_exit(&dtrace_lock); 14425 14426 if (hasprovs) 14427 dtrace_helper_provider_register(to, newhelp, NULL); 14428 } 14429 14430 /* 14431 * DTrace Hook Functions 14432 */ 14433 static void 14434 dtrace_module_loaded(struct modctl *ctl) 14435 { 14436 dtrace_provider_t *prv; 14437 14438 mutex_enter(&dtrace_provider_lock); 14439 mutex_enter(&mod_lock); 14440 14441 ASSERT(ctl->mod_busy); 14442 14443 /* 14444 * We're going to call each providers per-module provide operation 14445 * specifying only this module. 14446 */ 14447 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 14448 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 14449 14450 mutex_exit(&mod_lock); 14451 mutex_exit(&dtrace_provider_lock); 14452 14453 /* 14454 * If we have any retained enablings, we need to match against them. 14455 * Enabling probes requires that cpu_lock be held, and we cannot hold 14456 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 14457 * module. (In particular, this happens when loading scheduling 14458 * classes.) So if we have any retained enablings, we need to dispatch 14459 * our task queue to do the match for us. 14460 */ 14461 mutex_enter(&dtrace_lock); 14462 14463 if (dtrace_retained == NULL) { 14464 mutex_exit(&dtrace_lock); 14465 return; 14466 } 14467 14468 (void) taskq_dispatch(dtrace_taskq, 14469 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 14470 14471 mutex_exit(&dtrace_lock); 14472 14473 /* 14474 * And now, for a little heuristic sleaze: in general, we want to 14475 * match modules as soon as they load. However, we cannot guarantee 14476 * this, because it would lead us to the lock ordering violation 14477 * outlined above. The common case, of course, is that cpu_lock is 14478 * _not_ held -- so we delay here for a clock tick, hoping that that's 14479 * long enough for the task queue to do its work. If it's not, it's 14480 * not a serious problem -- it just means that the module that we 14481 * just loaded may not be immediately instrumentable. 14482 */ 14483 delay(1); 14484 } 14485 14486 static void 14487 dtrace_module_unloaded(struct modctl *ctl) 14488 { 14489 dtrace_probe_t template, *probe, *first, *next; 14490 dtrace_provider_t *prov; 14491 14492 template.dtpr_mod = ctl->mod_modname; 14493 14494 mutex_enter(&dtrace_provider_lock); 14495 mutex_enter(&mod_lock); 14496 mutex_enter(&dtrace_lock); 14497 14498 if (dtrace_bymod == NULL) { 14499 /* 14500 * The DTrace module is loaded (obviously) but not attached; 14501 * we don't have any work to do. 14502 */ 14503 mutex_exit(&dtrace_provider_lock); 14504 mutex_exit(&mod_lock); 14505 mutex_exit(&dtrace_lock); 14506 return; 14507 } 14508 14509 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 14510 probe != NULL; probe = probe->dtpr_nextmod) { 14511 if (probe->dtpr_ecb != NULL) { 14512 mutex_exit(&dtrace_provider_lock); 14513 mutex_exit(&mod_lock); 14514 mutex_exit(&dtrace_lock); 14515 14516 /* 14517 * This shouldn't _actually_ be possible -- we're 14518 * unloading a module that has an enabled probe in it. 14519 * (It's normally up to the provider to make sure that 14520 * this can't happen.) However, because dtps_enable() 14521 * doesn't have a failure mode, there can be an 14522 * enable/unload race. Upshot: we don't want to 14523 * assert, but we're not going to disable the 14524 * probe, either. 14525 */ 14526 if (dtrace_err_verbose) { 14527 cmn_err(CE_WARN, "unloaded module '%s' had " 14528 "enabled probes", ctl->mod_modname); 14529 } 14530 14531 return; 14532 } 14533 } 14534 14535 probe = first; 14536 14537 for (first = NULL; probe != NULL; probe = next) { 14538 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 14539 14540 dtrace_probes[probe->dtpr_id - 1] = NULL; 14541 14542 next = probe->dtpr_nextmod; 14543 dtrace_hash_remove(dtrace_bymod, probe); 14544 dtrace_hash_remove(dtrace_byfunc, probe); 14545 dtrace_hash_remove(dtrace_byname, probe); 14546 14547 if (first == NULL) { 14548 first = probe; 14549 probe->dtpr_nextmod = NULL; 14550 } else { 14551 probe->dtpr_nextmod = first; 14552 first = probe; 14553 } 14554 } 14555 14556 /* 14557 * We've removed all of the module's probes from the hash chains and 14558 * from the probe array. Now issue a dtrace_sync() to be sure that 14559 * everyone has cleared out from any probe array processing. 14560 */ 14561 dtrace_sync(); 14562 14563 for (probe = first; probe != NULL; probe = first) { 14564 first = probe->dtpr_nextmod; 14565 prov = probe->dtpr_provider; 14566 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 14567 probe->dtpr_arg); 14568 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 14569 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 14570 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 14571 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 14572 kmem_free(probe, sizeof (dtrace_probe_t)); 14573 } 14574 14575 mutex_exit(&dtrace_lock); 14576 mutex_exit(&mod_lock); 14577 mutex_exit(&dtrace_provider_lock); 14578 } 14579 14580 void 14581 dtrace_suspend(void) 14582 { 14583 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 14584 } 14585 14586 void 14587 dtrace_resume(void) 14588 { 14589 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 14590 } 14591 14592 static int 14593 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 14594 { 14595 ASSERT(MUTEX_HELD(&cpu_lock)); 14596 mutex_enter(&dtrace_lock); 14597 14598 switch (what) { 14599 case CPU_CONFIG: { 14600 dtrace_state_t *state; 14601 dtrace_optval_t *opt, rs, c; 14602 14603 /* 14604 * For now, we only allocate a new buffer for anonymous state. 14605 */ 14606 if ((state = dtrace_anon.dta_state) == NULL) 14607 break; 14608 14609 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14610 break; 14611 14612 opt = state->dts_options; 14613 c = opt[DTRACEOPT_CPU]; 14614 14615 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 14616 break; 14617 14618 /* 14619 * Regardless of what the actual policy is, we're going to 14620 * temporarily set our resize policy to be manual. We're 14621 * also going to temporarily set our CPU option to denote 14622 * the newly configured CPU. 14623 */ 14624 rs = opt[DTRACEOPT_BUFRESIZE]; 14625 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 14626 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 14627 14628 (void) dtrace_state_buffers(state); 14629 14630 opt[DTRACEOPT_BUFRESIZE] = rs; 14631 opt[DTRACEOPT_CPU] = c; 14632 14633 break; 14634 } 14635 14636 case CPU_UNCONFIG: 14637 /* 14638 * We don't free the buffer in the CPU_UNCONFIG case. (The 14639 * buffer will be freed when the consumer exits.) 14640 */ 14641 break; 14642 14643 default: 14644 break; 14645 } 14646 14647 mutex_exit(&dtrace_lock); 14648 return (0); 14649 } 14650 14651 static void 14652 dtrace_cpu_setup_initial(processorid_t cpu) 14653 { 14654 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 14655 } 14656 14657 static void 14658 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 14659 { 14660 if (dtrace_toxranges >= dtrace_toxranges_max) { 14661 int osize, nsize; 14662 dtrace_toxrange_t *range; 14663 14664 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14665 14666 if (osize == 0) { 14667 ASSERT(dtrace_toxrange == NULL); 14668 ASSERT(dtrace_toxranges_max == 0); 14669 dtrace_toxranges_max = 1; 14670 } else { 14671 dtrace_toxranges_max <<= 1; 14672 } 14673 14674 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14675 range = kmem_zalloc(nsize, KM_SLEEP); 14676 14677 if (dtrace_toxrange != NULL) { 14678 ASSERT(osize != 0); 14679 bcopy(dtrace_toxrange, range, osize); 14680 kmem_free(dtrace_toxrange, osize); 14681 } 14682 14683 dtrace_toxrange = range; 14684 } 14685 14686 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); 14687 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); 14688 14689 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 14690 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 14691 dtrace_toxranges++; 14692 } 14693 14694 /* 14695 * DTrace Driver Cookbook Functions 14696 */ 14697 /*ARGSUSED*/ 14698 static int 14699 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 14700 { 14701 dtrace_provider_id_t id; 14702 dtrace_state_t *state = NULL; 14703 dtrace_enabling_t *enab; 14704 14705 mutex_enter(&cpu_lock); 14706 mutex_enter(&dtrace_provider_lock); 14707 mutex_enter(&dtrace_lock); 14708 14709 if (ddi_soft_state_init(&dtrace_softstate, 14710 sizeof (dtrace_state_t), 0) != 0) { 14711 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 14712 mutex_exit(&cpu_lock); 14713 mutex_exit(&dtrace_provider_lock); 14714 mutex_exit(&dtrace_lock); 14715 return (DDI_FAILURE); 14716 } 14717 14718 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 14719 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 14720 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 14721 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 14722 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 14723 ddi_remove_minor_node(devi, NULL); 14724 ddi_soft_state_fini(&dtrace_softstate); 14725 mutex_exit(&cpu_lock); 14726 mutex_exit(&dtrace_provider_lock); 14727 mutex_exit(&dtrace_lock); 14728 return (DDI_FAILURE); 14729 } 14730 14731 ddi_report_dev(devi); 14732 dtrace_devi = devi; 14733 14734 dtrace_modload = dtrace_module_loaded; 14735 dtrace_modunload = dtrace_module_unloaded; 14736 dtrace_cpu_init = dtrace_cpu_setup_initial; 14737 dtrace_helpers_cleanup = dtrace_helpers_destroy; 14738 dtrace_helpers_fork = dtrace_helpers_duplicate; 14739 dtrace_cpustart_init = dtrace_suspend; 14740 dtrace_cpustart_fini = dtrace_resume; 14741 dtrace_debugger_init = dtrace_suspend; 14742 dtrace_debugger_fini = dtrace_resume; 14743 14744 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 14745 14746 ASSERT(MUTEX_HELD(&cpu_lock)); 14747 14748 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 14749 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14750 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 14751 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 14752 VM_SLEEP | VMC_IDENTIFIER); 14753 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 14754 1, INT_MAX, 0); 14755 14756 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 14757 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 14758 NULL, NULL, NULL, NULL, NULL, 0); 14759 14760 ASSERT(MUTEX_HELD(&cpu_lock)); 14761 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 14762 offsetof(dtrace_probe_t, dtpr_nextmod), 14763 offsetof(dtrace_probe_t, dtpr_prevmod)); 14764 14765 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 14766 offsetof(dtrace_probe_t, dtpr_nextfunc), 14767 offsetof(dtrace_probe_t, dtpr_prevfunc)); 14768 14769 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 14770 offsetof(dtrace_probe_t, dtpr_nextname), 14771 offsetof(dtrace_probe_t, dtpr_prevname)); 14772 14773 if (dtrace_retain_max < 1) { 14774 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 14775 "setting to 1", dtrace_retain_max); 14776 dtrace_retain_max = 1; 14777 } 14778 14779 /* 14780 * Now discover our toxic ranges. 14781 */ 14782 dtrace_toxic_ranges(dtrace_toxrange_add); 14783 14784 /* 14785 * Before we register ourselves as a provider to our own framework, 14786 * we would like to assert that dtrace_provider is NULL -- but that's 14787 * not true if we were loaded as a dependency of a DTrace provider. 14788 * Once we've registered, we can assert that dtrace_provider is our 14789 * pseudo provider. 14790 */ 14791 (void) dtrace_register("dtrace", &dtrace_provider_attr, 14792 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 14793 14794 ASSERT(dtrace_provider != NULL); 14795 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 14796 14797 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 14798 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 14799 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 14800 dtrace_provider, NULL, NULL, "END", 0, NULL); 14801 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 14802 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 14803 14804 dtrace_anon_property(); 14805 mutex_exit(&cpu_lock); 14806 14807 /* 14808 * If DTrace helper tracing is enabled, we need to allocate the 14809 * trace buffer and initialize the values. 14810 */ 14811 if (dtrace_helptrace_enabled) { 14812 ASSERT(dtrace_helptrace_buffer == NULL); 14813 dtrace_helptrace_buffer = 14814 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 14815 dtrace_helptrace_next = 0; 14816 } 14817 14818 /* 14819 * If there are already providers, we must ask them to provide their 14820 * probes, and then match any anonymous enabling against them. Note 14821 * that there should be no other retained enablings at this time: 14822 * the only retained enablings at this time should be the anonymous 14823 * enabling. 14824 */ 14825 if (dtrace_anon.dta_enabling != NULL) { 14826 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 14827 14828 dtrace_enabling_provide(NULL); 14829 state = dtrace_anon.dta_state; 14830 14831 /* 14832 * We couldn't hold cpu_lock across the above call to 14833 * dtrace_enabling_provide(), but we must hold it to actually 14834 * enable the probes. We have to drop all of our locks, pick 14835 * up cpu_lock, and regain our locks before matching the 14836 * retained anonymous enabling. 14837 */ 14838 mutex_exit(&dtrace_lock); 14839 mutex_exit(&dtrace_provider_lock); 14840 14841 mutex_enter(&cpu_lock); 14842 mutex_enter(&dtrace_provider_lock); 14843 mutex_enter(&dtrace_lock); 14844 14845 if ((enab = dtrace_anon.dta_enabling) != NULL) 14846 (void) dtrace_enabling_match(enab, NULL); 14847 14848 mutex_exit(&cpu_lock); 14849 } 14850 14851 mutex_exit(&dtrace_lock); 14852 mutex_exit(&dtrace_provider_lock); 14853 14854 if (state != NULL) { 14855 /* 14856 * If we created any anonymous state, set it going now. 14857 */ 14858 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 14859 } 14860 14861 return (DDI_SUCCESS); 14862 } 14863 14864 /*ARGSUSED*/ 14865 static int 14866 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 14867 { 14868 dtrace_state_t *state; 14869 uint32_t priv; 14870 uid_t uid; 14871 zoneid_t zoneid; 14872 14873 if (getminor(*devp) == DTRACEMNRN_HELPER) 14874 return (0); 14875 14876 /* 14877 * If this wasn't an open with the "helper" minor, then it must be 14878 * the "dtrace" minor. 14879 */ 14880 if (getminor(*devp) != DTRACEMNRN_DTRACE) 14881 return (ENXIO); 14882 14883 /* 14884 * If no DTRACE_PRIV_* bits are set in the credential, then the 14885 * caller lacks sufficient permission to do anything with DTrace. 14886 */ 14887 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 14888 if (priv == DTRACE_PRIV_NONE) 14889 return (EACCES); 14890 14891 /* 14892 * Ask all providers to provide all their probes. 14893 */ 14894 mutex_enter(&dtrace_provider_lock); 14895 dtrace_probe_provide(NULL, NULL); 14896 mutex_exit(&dtrace_provider_lock); 14897 14898 mutex_enter(&cpu_lock); 14899 mutex_enter(&dtrace_lock); 14900 dtrace_opens++; 14901 dtrace_membar_producer(); 14902 14903 /* 14904 * If the kernel debugger is active (that is, if the kernel debugger 14905 * modified text in some way), we won't allow the open. 14906 */ 14907 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 14908 dtrace_opens--; 14909 mutex_exit(&cpu_lock); 14910 mutex_exit(&dtrace_lock); 14911 return (EBUSY); 14912 } 14913 14914 state = dtrace_state_create(devp, cred_p); 14915 mutex_exit(&cpu_lock); 14916 14917 if (state == NULL) { 14918 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 14919 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 14920 mutex_exit(&dtrace_lock); 14921 return (EAGAIN); 14922 } 14923 14924 mutex_exit(&dtrace_lock); 14925 14926 return (0); 14927 } 14928 14929 /*ARGSUSED*/ 14930 static int 14931 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 14932 { 14933 minor_t minor = getminor(dev); 14934 dtrace_state_t *state; 14935 14936 if (minor == DTRACEMNRN_HELPER) 14937 return (0); 14938 14939 state = ddi_get_soft_state(dtrace_softstate, minor); 14940 14941 mutex_enter(&cpu_lock); 14942 mutex_enter(&dtrace_lock); 14943 14944 if (state->dts_anon) { 14945 /* 14946 * There is anonymous state. Destroy that first. 14947 */ 14948 ASSERT(dtrace_anon.dta_state == NULL); 14949 dtrace_state_destroy(state->dts_anon); 14950 } 14951 14952 dtrace_state_destroy(state); 14953 ASSERT(dtrace_opens > 0); 14954 14955 /* 14956 * Only relinquish control of the kernel debugger interface when there 14957 * are no consumers and no anonymous enablings. 14958 */ 14959 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 14960 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 14961 14962 mutex_exit(&dtrace_lock); 14963 mutex_exit(&cpu_lock); 14964 14965 return (0); 14966 } 14967 14968 /*ARGSUSED*/ 14969 static int 14970 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 14971 { 14972 int rval; 14973 dof_helper_t help, *dhp = NULL; 14974 14975 switch (cmd) { 14976 case DTRACEHIOC_ADDDOF: 14977 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 14978 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 14979 return (EFAULT); 14980 } 14981 14982 dhp = &help; 14983 arg = (intptr_t)help.dofhp_dof; 14984 /*FALLTHROUGH*/ 14985 14986 case DTRACEHIOC_ADD: { 14987 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 14988 14989 if (dof == NULL) 14990 return (rval); 14991 14992 mutex_enter(&dtrace_lock); 14993 14994 /* 14995 * dtrace_helper_slurp() takes responsibility for the dof -- 14996 * it may free it now or it may save it and free it later. 14997 */ 14998 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 14999 *rv = rval; 15000 rval = 0; 15001 } else { 15002 rval = EINVAL; 15003 } 15004 15005 mutex_exit(&dtrace_lock); 15006 return (rval); 15007 } 15008 15009 case DTRACEHIOC_REMOVE: { 15010 mutex_enter(&dtrace_lock); 15011 rval = dtrace_helper_destroygen(arg); 15012 mutex_exit(&dtrace_lock); 15013 15014 return (rval); 15015 } 15016 15017 default: 15018 break; 15019 } 15020 15021 return (ENOTTY); 15022 } 15023 15024 /*ARGSUSED*/ 15025 static int 15026 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 15027 { 15028 minor_t minor = getminor(dev); 15029 dtrace_state_t *state; 15030 int rval; 15031 15032 if (minor == DTRACEMNRN_HELPER) 15033 return (dtrace_ioctl_helper(cmd, arg, rv)); 15034 15035 state = ddi_get_soft_state(dtrace_softstate, minor); 15036 15037 if (state->dts_anon) { 15038 ASSERT(dtrace_anon.dta_state == NULL); 15039 state = state->dts_anon; 15040 } 15041 15042 switch (cmd) { 15043 case DTRACEIOC_PROVIDER: { 15044 dtrace_providerdesc_t pvd; 15045 dtrace_provider_t *pvp; 15046 15047 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 15048 return (EFAULT); 15049 15050 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 15051 mutex_enter(&dtrace_provider_lock); 15052 15053 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 15054 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 15055 break; 15056 } 15057 15058 mutex_exit(&dtrace_provider_lock); 15059 15060 if (pvp == NULL) 15061 return (ESRCH); 15062 15063 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 15064 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 15065 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 15066 return (EFAULT); 15067 15068 return (0); 15069 } 15070 15071 case DTRACEIOC_EPROBE: { 15072 dtrace_eprobedesc_t epdesc; 15073 dtrace_ecb_t *ecb; 15074 dtrace_action_t *act; 15075 void *buf; 15076 size_t size; 15077 uintptr_t dest; 15078 int nrecs; 15079 15080 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 15081 return (EFAULT); 15082 15083 mutex_enter(&dtrace_lock); 15084 15085 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 15086 mutex_exit(&dtrace_lock); 15087 return (EINVAL); 15088 } 15089 15090 if (ecb->dte_probe == NULL) { 15091 mutex_exit(&dtrace_lock); 15092 return (EINVAL); 15093 } 15094 15095 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 15096 epdesc.dtepd_uarg = ecb->dte_uarg; 15097 epdesc.dtepd_size = ecb->dte_size; 15098 15099 nrecs = epdesc.dtepd_nrecs; 15100 epdesc.dtepd_nrecs = 0; 15101 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15102 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15103 continue; 15104 15105 epdesc.dtepd_nrecs++; 15106 } 15107 15108 /* 15109 * Now that we have the size, we need to allocate a temporary 15110 * buffer in which to store the complete description. We need 15111 * the temporary buffer to be able to drop dtrace_lock() 15112 * across the copyout(), below. 15113 */ 15114 size = sizeof (dtrace_eprobedesc_t) + 15115 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 15116 15117 buf = kmem_alloc(size, KM_SLEEP); 15118 dest = (uintptr_t)buf; 15119 15120 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 15121 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 15122 15123 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15124 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15125 continue; 15126 15127 if (nrecs-- == 0) 15128 break; 15129 15130 bcopy(&act->dta_rec, (void *)dest, 15131 sizeof (dtrace_recdesc_t)); 15132 dest += sizeof (dtrace_recdesc_t); 15133 } 15134 15135 mutex_exit(&dtrace_lock); 15136 15137 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15138 kmem_free(buf, size); 15139 return (EFAULT); 15140 } 15141 15142 kmem_free(buf, size); 15143 return (0); 15144 } 15145 15146 case DTRACEIOC_AGGDESC: { 15147 dtrace_aggdesc_t aggdesc; 15148 dtrace_action_t *act; 15149 dtrace_aggregation_t *agg; 15150 int nrecs; 15151 uint32_t offs; 15152 dtrace_recdesc_t *lrec; 15153 void *buf; 15154 size_t size; 15155 uintptr_t dest; 15156 15157 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 15158 return (EFAULT); 15159 15160 mutex_enter(&dtrace_lock); 15161 15162 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 15163 mutex_exit(&dtrace_lock); 15164 return (EINVAL); 15165 } 15166 15167 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 15168 15169 nrecs = aggdesc.dtagd_nrecs; 15170 aggdesc.dtagd_nrecs = 0; 15171 15172 offs = agg->dtag_base; 15173 lrec = &agg->dtag_action.dta_rec; 15174 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 15175 15176 for (act = agg->dtag_first; ; act = act->dta_next) { 15177 ASSERT(act->dta_intuple || 15178 DTRACEACT_ISAGG(act->dta_kind)); 15179 15180 /* 15181 * If this action has a record size of zero, it 15182 * denotes an argument to the aggregating action. 15183 * Because the presence of this record doesn't (or 15184 * shouldn't) affect the way the data is interpreted, 15185 * we don't copy it out to save user-level the 15186 * confusion of dealing with a zero-length record. 15187 */ 15188 if (act->dta_rec.dtrd_size == 0) { 15189 ASSERT(agg->dtag_hasarg); 15190 continue; 15191 } 15192 15193 aggdesc.dtagd_nrecs++; 15194 15195 if (act == &agg->dtag_action) 15196 break; 15197 } 15198 15199 /* 15200 * Now that we have the size, we need to allocate a temporary 15201 * buffer in which to store the complete description. We need 15202 * the temporary buffer to be able to drop dtrace_lock() 15203 * across the copyout(), below. 15204 */ 15205 size = sizeof (dtrace_aggdesc_t) + 15206 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 15207 15208 buf = kmem_alloc(size, KM_SLEEP); 15209 dest = (uintptr_t)buf; 15210 15211 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 15212 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 15213 15214 for (act = agg->dtag_first; ; act = act->dta_next) { 15215 dtrace_recdesc_t rec = act->dta_rec; 15216 15217 /* 15218 * See the comment in the above loop for why we pass 15219 * over zero-length records. 15220 */ 15221 if (rec.dtrd_size == 0) { 15222 ASSERT(agg->dtag_hasarg); 15223 continue; 15224 } 15225 15226 if (nrecs-- == 0) 15227 break; 15228 15229 rec.dtrd_offset -= offs; 15230 bcopy(&rec, (void *)dest, sizeof (rec)); 15231 dest += sizeof (dtrace_recdesc_t); 15232 15233 if (act == &agg->dtag_action) 15234 break; 15235 } 15236 15237 mutex_exit(&dtrace_lock); 15238 15239 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15240 kmem_free(buf, size); 15241 return (EFAULT); 15242 } 15243 15244 kmem_free(buf, size); 15245 return (0); 15246 } 15247 15248 case DTRACEIOC_ENABLE: { 15249 dof_hdr_t *dof; 15250 dtrace_enabling_t *enab = NULL; 15251 dtrace_vstate_t *vstate; 15252 int err = 0; 15253 15254 *rv = 0; 15255 15256 /* 15257 * If a NULL argument has been passed, we take this as our 15258 * cue to reevaluate our enablings. 15259 */ 15260 if (arg == NULL) { 15261 dtrace_enabling_matchall(); 15262 15263 return (0); 15264 } 15265 15266 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 15267 return (rval); 15268 15269 mutex_enter(&cpu_lock); 15270 mutex_enter(&dtrace_lock); 15271 vstate = &state->dts_vstate; 15272 15273 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 15274 mutex_exit(&dtrace_lock); 15275 mutex_exit(&cpu_lock); 15276 dtrace_dof_destroy(dof); 15277 return (EBUSY); 15278 } 15279 15280 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 15281 mutex_exit(&dtrace_lock); 15282 mutex_exit(&cpu_lock); 15283 dtrace_dof_destroy(dof); 15284 return (EINVAL); 15285 } 15286 15287 if ((rval = dtrace_dof_options(dof, state)) != 0) { 15288 dtrace_enabling_destroy(enab); 15289 mutex_exit(&dtrace_lock); 15290 mutex_exit(&cpu_lock); 15291 dtrace_dof_destroy(dof); 15292 return (rval); 15293 } 15294 15295 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 15296 err = dtrace_enabling_retain(enab); 15297 } else { 15298 dtrace_enabling_destroy(enab); 15299 } 15300 15301 mutex_exit(&cpu_lock); 15302 mutex_exit(&dtrace_lock); 15303 dtrace_dof_destroy(dof); 15304 15305 return (err); 15306 } 15307 15308 case DTRACEIOC_REPLICATE: { 15309 dtrace_repldesc_t desc; 15310 dtrace_probedesc_t *match = &desc.dtrpd_match; 15311 dtrace_probedesc_t *create = &desc.dtrpd_create; 15312 int err; 15313 15314 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15315 return (EFAULT); 15316 15317 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15318 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15319 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15320 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15321 15322 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15323 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15324 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15325 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15326 15327 mutex_enter(&dtrace_lock); 15328 err = dtrace_enabling_replicate(state, match, create); 15329 mutex_exit(&dtrace_lock); 15330 15331 return (err); 15332 } 15333 15334 case DTRACEIOC_PROBEMATCH: 15335 case DTRACEIOC_PROBES: { 15336 dtrace_probe_t *probe = NULL; 15337 dtrace_probedesc_t desc; 15338 dtrace_probekey_t pkey; 15339 dtrace_id_t i; 15340 int m = 0; 15341 uint32_t priv; 15342 uid_t uid; 15343 zoneid_t zoneid; 15344 15345 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15346 return (EFAULT); 15347 15348 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15349 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15350 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15351 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15352 15353 /* 15354 * Before we attempt to match this probe, we want to give 15355 * all providers the opportunity to provide it. 15356 */ 15357 if (desc.dtpd_id == DTRACE_IDNONE) { 15358 mutex_enter(&dtrace_provider_lock); 15359 dtrace_probe_provide(&desc, NULL); 15360 mutex_exit(&dtrace_provider_lock); 15361 desc.dtpd_id++; 15362 } 15363 15364 if (cmd == DTRACEIOC_PROBEMATCH) { 15365 dtrace_probekey(&desc, &pkey); 15366 pkey.dtpk_id = DTRACE_IDNONE; 15367 } 15368 15369 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 15370 15371 mutex_enter(&dtrace_lock); 15372 15373 if (cmd == DTRACEIOC_PROBEMATCH) { 15374 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15375 if ((probe = dtrace_probes[i - 1]) != NULL && 15376 (m = dtrace_match_probe(probe, &pkey, 15377 priv, uid, zoneid)) != 0) 15378 break; 15379 } 15380 15381 if (m < 0) { 15382 mutex_exit(&dtrace_lock); 15383 return (EINVAL); 15384 } 15385 15386 } else { 15387 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15388 if ((probe = dtrace_probes[i - 1]) != NULL && 15389 dtrace_match_priv(probe, priv, uid, zoneid)) 15390 break; 15391 } 15392 } 15393 15394 if (probe == NULL) { 15395 mutex_exit(&dtrace_lock); 15396 return (ESRCH); 15397 } 15398 15399 dtrace_probe_description(probe, &desc); 15400 mutex_exit(&dtrace_lock); 15401 15402 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15403 return (EFAULT); 15404 15405 return (0); 15406 } 15407 15408 case DTRACEIOC_PROBEARG: { 15409 dtrace_argdesc_t desc; 15410 dtrace_probe_t *probe; 15411 dtrace_provider_t *prov; 15412 15413 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15414 return (EFAULT); 15415 15416 if (desc.dtargd_id == DTRACE_IDNONE) 15417 return (EINVAL); 15418 15419 if (desc.dtargd_ndx == DTRACE_ARGNONE) 15420 return (EINVAL); 15421 15422 mutex_enter(&dtrace_provider_lock); 15423 mutex_enter(&mod_lock); 15424 mutex_enter(&dtrace_lock); 15425 15426 if (desc.dtargd_id > dtrace_nprobes) { 15427 mutex_exit(&dtrace_lock); 15428 mutex_exit(&mod_lock); 15429 mutex_exit(&dtrace_provider_lock); 15430 return (EINVAL); 15431 } 15432 15433 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 15434 mutex_exit(&dtrace_lock); 15435 mutex_exit(&mod_lock); 15436 mutex_exit(&dtrace_provider_lock); 15437 return (EINVAL); 15438 } 15439 15440 mutex_exit(&dtrace_lock); 15441 15442 prov = probe->dtpr_provider; 15443 15444 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 15445 /* 15446 * There isn't any typed information for this probe. 15447 * Set the argument number to DTRACE_ARGNONE. 15448 */ 15449 desc.dtargd_ndx = DTRACE_ARGNONE; 15450 } else { 15451 desc.dtargd_native[0] = '\0'; 15452 desc.dtargd_xlate[0] = '\0'; 15453 desc.dtargd_mapping = desc.dtargd_ndx; 15454 15455 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 15456 probe->dtpr_id, probe->dtpr_arg, &desc); 15457 } 15458 15459 mutex_exit(&mod_lock); 15460 mutex_exit(&dtrace_provider_lock); 15461 15462 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15463 return (EFAULT); 15464 15465 return (0); 15466 } 15467 15468 case DTRACEIOC_GO: { 15469 processorid_t cpuid; 15470 rval = dtrace_state_go(state, &cpuid); 15471 15472 if (rval != 0) 15473 return (rval); 15474 15475 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15476 return (EFAULT); 15477 15478 return (0); 15479 } 15480 15481 case DTRACEIOC_STOP: { 15482 processorid_t cpuid; 15483 15484 mutex_enter(&dtrace_lock); 15485 rval = dtrace_state_stop(state, &cpuid); 15486 mutex_exit(&dtrace_lock); 15487 15488 if (rval != 0) 15489 return (rval); 15490 15491 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15492 return (EFAULT); 15493 15494 return (0); 15495 } 15496 15497 case DTRACEIOC_DOFGET: { 15498 dof_hdr_t hdr, *dof; 15499 uint64_t len; 15500 15501 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 15502 return (EFAULT); 15503 15504 mutex_enter(&dtrace_lock); 15505 dof = dtrace_dof_create(state); 15506 mutex_exit(&dtrace_lock); 15507 15508 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 15509 rval = copyout(dof, (void *)arg, len); 15510 dtrace_dof_destroy(dof); 15511 15512 return (rval == 0 ? 0 : EFAULT); 15513 } 15514 15515 case DTRACEIOC_AGGSNAP: 15516 case DTRACEIOC_BUFSNAP: { 15517 dtrace_bufdesc_t desc; 15518 caddr_t cached; 15519 dtrace_buffer_t *buf; 15520 15521 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15522 return (EFAULT); 15523 15524 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 15525 return (EINVAL); 15526 15527 mutex_enter(&dtrace_lock); 15528 15529 if (cmd == DTRACEIOC_BUFSNAP) { 15530 buf = &state->dts_buffer[desc.dtbd_cpu]; 15531 } else { 15532 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 15533 } 15534 15535 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 15536 size_t sz = buf->dtb_offset; 15537 15538 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 15539 mutex_exit(&dtrace_lock); 15540 return (EBUSY); 15541 } 15542 15543 /* 15544 * If this buffer has already been consumed, we're 15545 * going to indicate that there's nothing left here 15546 * to consume. 15547 */ 15548 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 15549 mutex_exit(&dtrace_lock); 15550 15551 desc.dtbd_size = 0; 15552 desc.dtbd_drops = 0; 15553 desc.dtbd_errors = 0; 15554 desc.dtbd_oldest = 0; 15555 sz = sizeof (desc); 15556 15557 if (copyout(&desc, (void *)arg, sz) != 0) 15558 return (EFAULT); 15559 15560 return (0); 15561 } 15562 15563 /* 15564 * If this is a ring buffer that has wrapped, we want 15565 * to copy the whole thing out. 15566 */ 15567 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 15568 dtrace_buffer_polish(buf); 15569 sz = buf->dtb_size; 15570 } 15571 15572 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 15573 mutex_exit(&dtrace_lock); 15574 return (EFAULT); 15575 } 15576 15577 desc.dtbd_size = sz; 15578 desc.dtbd_drops = buf->dtb_drops; 15579 desc.dtbd_errors = buf->dtb_errors; 15580 desc.dtbd_oldest = buf->dtb_xamot_offset; 15581 15582 mutex_exit(&dtrace_lock); 15583 15584 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15585 return (EFAULT); 15586 15587 buf->dtb_flags |= DTRACEBUF_CONSUMED; 15588 15589 return (0); 15590 } 15591 15592 if (buf->dtb_tomax == NULL) { 15593 ASSERT(buf->dtb_xamot == NULL); 15594 mutex_exit(&dtrace_lock); 15595 return (ENOENT); 15596 } 15597 15598 cached = buf->dtb_tomax; 15599 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 15600 15601 dtrace_xcall(desc.dtbd_cpu, 15602 (dtrace_xcall_t)dtrace_buffer_switch, buf); 15603 15604 state->dts_errors += buf->dtb_xamot_errors; 15605 15606 /* 15607 * If the buffers did not actually switch, then the cross call 15608 * did not take place -- presumably because the given CPU is 15609 * not in the ready set. If this is the case, we'll return 15610 * ENOENT. 15611 */ 15612 if (buf->dtb_tomax == cached) { 15613 ASSERT(buf->dtb_xamot != cached); 15614 mutex_exit(&dtrace_lock); 15615 return (ENOENT); 15616 } 15617 15618 ASSERT(cached == buf->dtb_xamot); 15619 15620 /* 15621 * We have our snapshot; now copy it out. 15622 */ 15623 if (copyout(buf->dtb_xamot, desc.dtbd_data, 15624 buf->dtb_xamot_offset) != 0) { 15625 mutex_exit(&dtrace_lock); 15626 return (EFAULT); 15627 } 15628 15629 desc.dtbd_size = buf->dtb_xamot_offset; 15630 desc.dtbd_drops = buf->dtb_xamot_drops; 15631 desc.dtbd_errors = buf->dtb_xamot_errors; 15632 desc.dtbd_oldest = 0; 15633 15634 mutex_exit(&dtrace_lock); 15635 15636 /* 15637 * Finally, copy out the buffer description. 15638 */ 15639 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15640 return (EFAULT); 15641 15642 return (0); 15643 } 15644 15645 case DTRACEIOC_CONF: { 15646 dtrace_conf_t conf; 15647 15648 bzero(&conf, sizeof (conf)); 15649 conf.dtc_difversion = DIF_VERSION; 15650 conf.dtc_difintregs = DIF_DIR_NREGS; 15651 conf.dtc_diftupregs = DIF_DTR_NREGS; 15652 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 15653 15654 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 15655 return (EFAULT); 15656 15657 return (0); 15658 } 15659 15660 case DTRACEIOC_STATUS: { 15661 dtrace_status_t stat; 15662 dtrace_dstate_t *dstate; 15663 int i, j; 15664 uint64_t nerrs; 15665 15666 /* 15667 * See the comment in dtrace_state_deadman() for the reason 15668 * for setting dts_laststatus to INT64_MAX before setting 15669 * it to the correct value. 15670 */ 15671 state->dts_laststatus = INT64_MAX; 15672 dtrace_membar_producer(); 15673 state->dts_laststatus = dtrace_gethrtime(); 15674 15675 bzero(&stat, sizeof (stat)); 15676 15677 mutex_enter(&dtrace_lock); 15678 15679 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 15680 mutex_exit(&dtrace_lock); 15681 return (ENOENT); 15682 } 15683 15684 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 15685 stat.dtst_exiting = 1; 15686 15687 nerrs = state->dts_errors; 15688 dstate = &state->dts_vstate.dtvs_dynvars; 15689 15690 for (i = 0; i < NCPU; i++) { 15691 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 15692 15693 stat.dtst_dyndrops += dcpu->dtdsc_drops; 15694 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 15695 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 15696 15697 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 15698 stat.dtst_filled++; 15699 15700 nerrs += state->dts_buffer[i].dtb_errors; 15701 15702 for (j = 0; j < state->dts_nspeculations; j++) { 15703 dtrace_speculation_t *spec; 15704 dtrace_buffer_t *buf; 15705 15706 spec = &state->dts_speculations[j]; 15707 buf = &spec->dtsp_buffer[i]; 15708 stat.dtst_specdrops += buf->dtb_xamot_drops; 15709 } 15710 } 15711 15712 stat.dtst_specdrops_busy = state->dts_speculations_busy; 15713 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 15714 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 15715 stat.dtst_dblerrors = state->dts_dblerrors; 15716 stat.dtst_killed = 15717 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 15718 stat.dtst_errors = nerrs; 15719 15720 mutex_exit(&dtrace_lock); 15721 15722 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 15723 return (EFAULT); 15724 15725 return (0); 15726 } 15727 15728 case DTRACEIOC_FORMAT: { 15729 dtrace_fmtdesc_t fmt; 15730 char *str; 15731 int len; 15732 15733 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 15734 return (EFAULT); 15735 15736 mutex_enter(&dtrace_lock); 15737 15738 if (fmt.dtfd_format == 0 || 15739 fmt.dtfd_format > state->dts_nformats) { 15740 mutex_exit(&dtrace_lock); 15741 return (EINVAL); 15742 } 15743 15744 /* 15745 * Format strings are allocated contiguously and they are 15746 * never freed; if a format index is less than the number 15747 * of formats, we can assert that the format map is non-NULL 15748 * and that the format for the specified index is non-NULL. 15749 */ 15750 ASSERT(state->dts_formats != NULL); 15751 str = state->dts_formats[fmt.dtfd_format - 1]; 15752 ASSERT(str != NULL); 15753 15754 len = strlen(str) + 1; 15755 15756 if (len > fmt.dtfd_length) { 15757 fmt.dtfd_length = len; 15758 15759 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 15760 mutex_exit(&dtrace_lock); 15761 return (EINVAL); 15762 } 15763 } else { 15764 if (copyout(str, fmt.dtfd_string, len) != 0) { 15765 mutex_exit(&dtrace_lock); 15766 return (EINVAL); 15767 } 15768 } 15769 15770 mutex_exit(&dtrace_lock); 15771 return (0); 15772 } 15773 15774 default: 15775 break; 15776 } 15777 15778 return (ENOTTY); 15779 } 15780 15781 /*ARGSUSED*/ 15782 static int 15783 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 15784 { 15785 dtrace_state_t *state; 15786 15787 switch (cmd) { 15788 case DDI_DETACH: 15789 break; 15790 15791 case DDI_SUSPEND: 15792 return (DDI_SUCCESS); 15793 15794 default: 15795 return (DDI_FAILURE); 15796 } 15797 15798 mutex_enter(&cpu_lock); 15799 mutex_enter(&dtrace_provider_lock); 15800 mutex_enter(&dtrace_lock); 15801 15802 ASSERT(dtrace_opens == 0); 15803 15804 if (dtrace_helpers > 0) { 15805 mutex_exit(&dtrace_provider_lock); 15806 mutex_exit(&dtrace_lock); 15807 mutex_exit(&cpu_lock); 15808 return (DDI_FAILURE); 15809 } 15810 15811 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 15812 mutex_exit(&dtrace_provider_lock); 15813 mutex_exit(&dtrace_lock); 15814 mutex_exit(&cpu_lock); 15815 return (DDI_FAILURE); 15816 } 15817 15818 dtrace_provider = NULL; 15819 15820 if ((state = dtrace_anon_grab()) != NULL) { 15821 /* 15822 * If there were ECBs on this state, the provider should 15823 * have not been allowed to detach; assert that there is 15824 * none. 15825 */ 15826 ASSERT(state->dts_necbs == 0); 15827 dtrace_state_destroy(state); 15828 15829 /* 15830 * If we're being detached with anonymous state, we need to 15831 * indicate to the kernel debugger that DTrace is now inactive. 15832 */ 15833 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15834 } 15835 15836 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 15837 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 15838 dtrace_cpu_init = NULL; 15839 dtrace_helpers_cleanup = NULL; 15840 dtrace_helpers_fork = NULL; 15841 dtrace_cpustart_init = NULL; 15842 dtrace_cpustart_fini = NULL; 15843 dtrace_debugger_init = NULL; 15844 dtrace_debugger_fini = NULL; 15845 dtrace_modload = NULL; 15846 dtrace_modunload = NULL; 15847 15848 mutex_exit(&cpu_lock); 15849 15850 if (dtrace_helptrace_enabled) { 15851 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 15852 dtrace_helptrace_buffer = NULL; 15853 } 15854 15855 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 15856 dtrace_probes = NULL; 15857 dtrace_nprobes = 0; 15858 15859 dtrace_hash_destroy(dtrace_bymod); 15860 dtrace_hash_destroy(dtrace_byfunc); 15861 dtrace_hash_destroy(dtrace_byname); 15862 dtrace_bymod = NULL; 15863 dtrace_byfunc = NULL; 15864 dtrace_byname = NULL; 15865 15866 kmem_cache_destroy(dtrace_state_cache); 15867 vmem_destroy(dtrace_minor); 15868 vmem_destroy(dtrace_arena); 15869 15870 if (dtrace_toxrange != NULL) { 15871 kmem_free(dtrace_toxrange, 15872 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 15873 dtrace_toxrange = NULL; 15874 dtrace_toxranges = 0; 15875 dtrace_toxranges_max = 0; 15876 } 15877 15878 ddi_remove_minor_node(dtrace_devi, NULL); 15879 dtrace_devi = NULL; 15880 15881 ddi_soft_state_fini(&dtrace_softstate); 15882 15883 ASSERT(dtrace_vtime_references == 0); 15884 ASSERT(dtrace_opens == 0); 15885 ASSERT(dtrace_retained == NULL); 15886 15887 mutex_exit(&dtrace_lock); 15888 mutex_exit(&dtrace_provider_lock); 15889 15890 /* 15891 * We don't destroy the task queue until after we have dropped our 15892 * locks (taskq_destroy() may block on running tasks). To prevent 15893 * attempting to do work after we have effectively detached but before 15894 * the task queue has been destroyed, all tasks dispatched via the 15895 * task queue must check that DTrace is still attached before 15896 * performing any operation. 15897 */ 15898 taskq_destroy(dtrace_taskq); 15899 dtrace_taskq = NULL; 15900 15901 return (DDI_SUCCESS); 15902 } 15903 15904 /*ARGSUSED*/ 15905 static int 15906 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 15907 { 15908 int error; 15909 15910 switch (infocmd) { 15911 case DDI_INFO_DEVT2DEVINFO: 15912 *result = (void *)dtrace_devi; 15913 error = DDI_SUCCESS; 15914 break; 15915 case DDI_INFO_DEVT2INSTANCE: 15916 *result = (void *)0; 15917 error = DDI_SUCCESS; 15918 break; 15919 default: 15920 error = DDI_FAILURE; 15921 } 15922 return (error); 15923 } 15924 15925 static struct cb_ops dtrace_cb_ops = { 15926 dtrace_open, /* open */ 15927 dtrace_close, /* close */ 15928 nulldev, /* strategy */ 15929 nulldev, /* print */ 15930 nodev, /* dump */ 15931 nodev, /* read */ 15932 nodev, /* write */ 15933 dtrace_ioctl, /* ioctl */ 15934 nodev, /* devmap */ 15935 nodev, /* mmap */ 15936 nodev, /* segmap */ 15937 nochpoll, /* poll */ 15938 ddi_prop_op, /* cb_prop_op */ 15939 0, /* streamtab */ 15940 D_NEW | D_MP /* Driver compatibility flag */ 15941 }; 15942 15943 static struct dev_ops dtrace_ops = { 15944 DEVO_REV, /* devo_rev */ 15945 0, /* refcnt */ 15946 dtrace_info, /* get_dev_info */ 15947 nulldev, /* identify */ 15948 nulldev, /* probe */ 15949 dtrace_attach, /* attach */ 15950 dtrace_detach, /* detach */ 15951 nodev, /* reset */ 15952 &dtrace_cb_ops, /* driver operations */ 15953 NULL, /* bus operations */ 15954 nodev, /* dev power */ 15955 ddi_quiesce_not_needed, /* quiesce */ 15956 }; 15957 15958 static struct modldrv modldrv = { 15959 &mod_driverops, /* module type (this is a pseudo driver) */ 15960 "Dynamic Tracing", /* name of module */ 15961 &dtrace_ops, /* driver ops */ 15962 }; 15963 15964 static struct modlinkage modlinkage = { 15965 MODREV_1, 15966 (void *)&modldrv, 15967 NULL 15968 }; 15969 15970 int 15971 _init(void) 15972 { 15973 return (mod_install(&modlinkage)); 15974 } 15975 15976 int 15977 _info(struct modinfo *modinfop) 15978 { 15979 return (mod_info(&modlinkage, modinfop)); 15980 } 15981 15982 int 15983 _fini(void) 15984 { 15985 return (mod_remove(&modlinkage)); 15986 } 15987