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 148 /* 149 * DTrace External Variables 150 * 151 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 152 * available to DTrace consumers via the backtick (`) syntax. One of these, 153 * dtrace_zero, is made deliberately so: it is provided as a source of 154 * well-known, zero-filled memory. While this variable is not documented, 155 * it is used by some translators as an implementation detail. 156 */ 157 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 158 159 /* 160 * DTrace Internal Variables 161 */ 162 static dev_info_t *dtrace_devi; /* device info */ 163 static vmem_t *dtrace_arena; /* probe ID arena */ 164 static vmem_t *dtrace_minor; /* minor number arena */ 165 static taskq_t *dtrace_taskq; /* task queue */ 166 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 167 static int dtrace_nprobes; /* number of probes */ 168 static dtrace_provider_t *dtrace_provider; /* provider list */ 169 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 170 static int dtrace_opens; /* number of opens */ 171 static int dtrace_helpers; /* number of helpers */ 172 static void *dtrace_softstate; /* softstate pointer */ 173 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 174 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 175 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 176 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 177 static int dtrace_toxranges; /* number of toxic ranges */ 178 static int dtrace_toxranges_max; /* size of toxic range array */ 179 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 180 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 181 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 182 static kthread_t *dtrace_panicked; /* panicking thread */ 183 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 184 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 185 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 186 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 187 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 188 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 189 static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 190 191 /* 192 * DTrace Locking 193 * DTrace is protected by three (relatively coarse-grained) locks: 194 * 195 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 196 * including enabling state, probes, ECBs, consumer state, helper state, 197 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 198 * probe context is lock-free -- synchronization is handled via the 199 * dtrace_sync() cross call mechanism. 200 * 201 * (2) dtrace_provider_lock is required when manipulating provider state, or 202 * when provider state must be held constant. 203 * 204 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 205 * when meta provider state must be held constant. 206 * 207 * The lock ordering between these three locks is dtrace_meta_lock before 208 * dtrace_provider_lock before dtrace_lock. (In particular, there are 209 * several places where dtrace_provider_lock is held by the framework as it 210 * calls into the providers -- which then call back into the framework, 211 * grabbing dtrace_lock.) 212 * 213 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 214 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 215 * role as a coarse-grained lock; it is acquired before both of these locks. 216 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 217 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 218 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 219 * acquired _between_ dtrace_provider_lock and dtrace_lock. 220 */ 221 static kmutex_t dtrace_lock; /* probe state lock */ 222 static kmutex_t dtrace_provider_lock; /* provider state lock */ 223 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 224 225 /* 226 * DTrace Provider Variables 227 * 228 * These are the variables relating to DTrace as a provider (that is, the 229 * provider of the BEGIN, END, and ERROR probes). 230 */ 231 static dtrace_pattr_t dtrace_provider_attr = { 232 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 233 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 234 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 235 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 236 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 237 }; 238 239 static void 240 dtrace_nullop(void) 241 {} 242 243 static int 244 dtrace_enable_nullop(void) 245 { 246 return (0); 247 } 248 249 static dtrace_pops_t dtrace_provider_ops = { 250 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, 251 (void (*)(void *, struct modctl *))dtrace_nullop, 252 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop, 253 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 254 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 255 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 256 NULL, 257 NULL, 258 NULL, 259 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 260 }; 261 262 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 263 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 264 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 265 266 /* 267 * DTrace Helper Tracing Variables 268 */ 269 uint32_t dtrace_helptrace_next = 0; 270 uint32_t dtrace_helptrace_nlocals; 271 char *dtrace_helptrace_buffer; 272 int dtrace_helptrace_bufsize = 512 * 1024; 273 274 #ifdef DEBUG 275 int dtrace_helptrace_enabled = 1; 276 #else 277 int dtrace_helptrace_enabled = 0; 278 #endif 279 280 /* 281 * DTrace Error Hashing 282 * 283 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 284 * table. This is very useful for checking coverage of tests that are 285 * expected to induce DIF or DOF processing errors, and may be useful for 286 * debugging problems in the DIF code generator or in DOF generation . The 287 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 288 */ 289 #ifdef DEBUG 290 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 291 static const char *dtrace_errlast; 292 static kthread_t *dtrace_errthread; 293 static kmutex_t dtrace_errlock; 294 #endif 295 296 /* 297 * DTrace Macros and Constants 298 * 299 * These are various macros that are useful in various spots in the 300 * implementation, along with a few random constants that have no meaning 301 * outside of the implementation. There is no real structure to this cpp 302 * mishmash -- but is there ever? 303 */ 304 #define DTRACE_HASHSTR(hash, probe) \ 305 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 306 307 #define DTRACE_HASHNEXT(hash, probe) \ 308 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 309 310 #define DTRACE_HASHPREV(hash, probe) \ 311 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 312 313 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 314 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 315 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 316 317 #define DTRACE_AGGHASHSIZE_SLEW 17 318 319 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 320 321 /* 322 * The key for a thread-local variable consists of the lower 61 bits of the 323 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 324 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 325 * equal to a variable identifier. This is necessary (but not sufficient) to 326 * assure that global associative arrays never collide with thread-local 327 * variables. To guarantee that they cannot collide, we must also define the 328 * order for keying dynamic variables. That order is: 329 * 330 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 331 * 332 * Because the variable-key and the tls-key are in orthogonal spaces, there is 333 * no way for a global variable key signature to match a thread-local key 334 * signature. 335 */ 336 #define DTRACE_TLS_THRKEY(where) { \ 337 uint_t intr = 0; \ 338 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 339 for (; actv; actv >>= 1) \ 340 intr++; \ 341 ASSERT(intr < (1 << 3)); \ 342 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 343 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 344 } 345 346 #define DT_BSWAP_8(x) ((x) & 0xff) 347 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 348 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 349 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 350 351 #define DT_MASK_LO 0x00000000FFFFFFFFULL 352 353 #define DTRACE_STORE(type, tomax, offset, what) \ 354 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 355 356 #ifndef __i386 357 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 358 if (addr & (size - 1)) { \ 359 *flags |= CPU_DTRACE_BADALIGN; \ 360 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 361 return (0); \ 362 } 363 #else 364 #define DTRACE_ALIGNCHECK(addr, size, flags) 365 #endif 366 367 /* 368 * Test whether a range of memory starting at testaddr of size testsz falls 369 * within the range of memory described by addr, sz. We take care to avoid 370 * problems with overflow and underflow of the unsigned quantities, and 371 * disallow all negative sizes. Ranges of size 0 are allowed. 372 */ 373 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 374 ((testaddr) - (baseaddr) < (basesz) && \ 375 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \ 376 (testaddr) + (testsz) >= (testaddr)) 377 378 /* 379 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 380 * alloc_sz on the righthand side of the comparison in order to avoid overflow 381 * or underflow in the comparison with it. This is simpler than the INRANGE 382 * check above, because we know that the dtms_scratch_ptr is valid in the 383 * range. Allocations of size zero are allowed. 384 */ 385 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 386 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 387 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 388 389 #define DTRACE_LOADFUNC(bits) \ 390 /*CSTYLED*/ \ 391 uint##bits##_t \ 392 dtrace_load##bits(uintptr_t addr) \ 393 { \ 394 size_t size = bits / NBBY; \ 395 /*CSTYLED*/ \ 396 uint##bits##_t rval; \ 397 int i; \ 398 volatile uint16_t *flags = (volatile uint16_t *) \ 399 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ 400 \ 401 DTRACE_ALIGNCHECK(addr, size, flags); \ 402 \ 403 for (i = 0; i < dtrace_toxranges; i++) { \ 404 if (addr >= dtrace_toxrange[i].dtt_limit) \ 405 continue; \ 406 \ 407 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 408 continue; \ 409 \ 410 /* \ 411 * This address falls within a toxic region; return 0. \ 412 */ \ 413 *flags |= CPU_DTRACE_BADADDR; \ 414 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 415 return (0); \ 416 } \ 417 \ 418 *flags |= CPU_DTRACE_NOFAULT; \ 419 /*CSTYLED*/ \ 420 rval = *((volatile uint##bits##_t *)addr); \ 421 *flags &= ~CPU_DTRACE_NOFAULT; \ 422 \ 423 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 424 } 425 426 #ifdef _LP64 427 #define dtrace_loadptr dtrace_load64 428 #else 429 #define dtrace_loadptr dtrace_load32 430 #endif 431 432 #define DTRACE_DYNHASH_FREE 0 433 #define DTRACE_DYNHASH_SINK 1 434 #define DTRACE_DYNHASH_VALID 2 435 436 #define DTRACE_MATCH_FAIL -1 437 #define DTRACE_MATCH_NEXT 0 438 #define DTRACE_MATCH_DONE 1 439 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 440 #define DTRACE_STATE_ALIGN 64 441 442 #define DTRACE_FLAGS2FLT(flags) \ 443 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 444 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 445 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 446 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 447 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 448 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 449 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 450 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 451 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 452 DTRACEFLT_UNKNOWN) 453 454 #define DTRACEACT_ISSTRING(act) \ 455 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 456 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 457 458 static size_t dtrace_strlen(const char *, size_t); 459 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 460 static void dtrace_enabling_provide(dtrace_provider_t *); 461 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 462 static void dtrace_enabling_matchall(void); 463 static dtrace_state_t *dtrace_anon_grab(void); 464 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 465 dtrace_state_t *, uint64_t, uint64_t); 466 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 467 static void dtrace_buffer_drop(dtrace_buffer_t *); 468 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 469 dtrace_state_t *, dtrace_mstate_t *); 470 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 471 dtrace_optval_t); 472 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 473 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 474 475 /* 476 * DTrace Probe Context Functions 477 * 478 * These functions are called from probe context. Because probe context is 479 * any context in which C may be called, arbitrarily locks may be held, 480 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 481 * As a result, functions called from probe context may only call other DTrace 482 * support functions -- they may not interact at all with the system at large. 483 * (Note that the ASSERT macro is made probe-context safe by redefining it in 484 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 485 * loads are to be performed from probe context, they _must_ be in terms of 486 * the safe dtrace_load*() variants. 487 * 488 * Some functions in this block are not actually called from probe context; 489 * for these functions, there will be a comment above the function reading 490 * "Note: not called from probe context." 491 */ 492 void 493 dtrace_panic(const char *format, ...) 494 { 495 va_list alist; 496 497 va_start(alist, format); 498 dtrace_vpanic(format, alist); 499 va_end(alist); 500 } 501 502 int 503 dtrace_assfail(const char *a, const char *f, int l) 504 { 505 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 506 507 /* 508 * We just need something here that even the most clever compiler 509 * cannot optimize away. 510 */ 511 return (a[(uintptr_t)f]); 512 } 513 514 /* 515 * Atomically increment a specified error counter from probe context. 516 */ 517 static void 518 dtrace_error(uint32_t *counter) 519 { 520 /* 521 * Most counters stored to in probe context are per-CPU counters. 522 * However, there are some error conditions that are sufficiently 523 * arcane that they don't merit per-CPU storage. If these counters 524 * are incremented concurrently on different CPUs, scalability will be 525 * adversely affected -- but we don't expect them to be white-hot in a 526 * correctly constructed enabling... 527 */ 528 uint32_t oval, nval; 529 530 do { 531 oval = *counter; 532 533 if ((nval = oval + 1) == 0) { 534 /* 535 * If the counter would wrap, set it to 1 -- assuring 536 * that the counter is never zero when we have seen 537 * errors. (The counter must be 32-bits because we 538 * aren't guaranteed a 64-bit compare&swap operation.) 539 * To save this code both the infamy of being fingered 540 * by a priggish news story and the indignity of being 541 * the target of a neo-puritan witch trial, we're 542 * carefully avoiding any colorful description of the 543 * likelihood of this condition -- but suffice it to 544 * say that it is only slightly more likely than the 545 * overflow of predicate cache IDs, as discussed in 546 * dtrace_predicate_create(). 547 */ 548 nval = 1; 549 } 550 } while (dtrace_cas32(counter, oval, nval) != oval); 551 } 552 553 /* 554 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 555 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 556 */ 557 DTRACE_LOADFUNC(8) 558 DTRACE_LOADFUNC(16) 559 DTRACE_LOADFUNC(32) 560 DTRACE_LOADFUNC(64) 561 562 static int 563 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 564 { 565 if (dest < mstate->dtms_scratch_base) 566 return (0); 567 568 if (dest + size < dest) 569 return (0); 570 571 if (dest + size > mstate->dtms_scratch_ptr) 572 return (0); 573 574 return (1); 575 } 576 577 static int 578 dtrace_canstore_statvar(uint64_t addr, size_t sz, 579 dtrace_statvar_t **svars, int nsvars) 580 { 581 int i; 582 583 for (i = 0; i < nsvars; i++) { 584 dtrace_statvar_t *svar = svars[i]; 585 586 if (svar == NULL || svar->dtsv_size == 0) 587 continue; 588 589 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 590 return (1); 591 } 592 593 return (0); 594 } 595 596 /* 597 * Check to see if the address is within a memory region to which a store may 598 * be issued. This includes the DTrace scratch areas, and any DTrace variable 599 * region. The caller of dtrace_canstore() is responsible for performing any 600 * alignment checks that are needed before stores are actually executed. 601 */ 602 static int 603 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 604 dtrace_vstate_t *vstate) 605 { 606 /* 607 * First, check to see if the address is in scratch space... 608 */ 609 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 610 mstate->dtms_scratch_size)) 611 return (1); 612 613 /* 614 * Now check to see if it's a dynamic variable. This check will pick 615 * up both thread-local variables and any global dynamically-allocated 616 * variables. 617 */ 618 if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base, 619 vstate->dtvs_dynvars.dtds_size)) { 620 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 621 uintptr_t base = (uintptr_t)dstate->dtds_base + 622 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 623 uintptr_t chunkoffs; 624 625 /* 626 * Before we assume that we can store here, we need to make 627 * sure that it isn't in our metadata -- storing to our 628 * dynamic variable metadata would corrupt our state. For 629 * the range to not include any dynamic variable metadata, 630 * it must: 631 * 632 * (1) Start above the hash table that is at the base of 633 * the dynamic variable space 634 * 635 * (2) Have a starting chunk offset that is beyond the 636 * dtrace_dynvar_t that is at the base of every chunk 637 * 638 * (3) Not span a chunk boundary 639 * 640 */ 641 if (addr < base) 642 return (0); 643 644 chunkoffs = (addr - base) % dstate->dtds_chunksize; 645 646 if (chunkoffs < sizeof (dtrace_dynvar_t)) 647 return (0); 648 649 if (chunkoffs + sz > dstate->dtds_chunksize) 650 return (0); 651 652 return (1); 653 } 654 655 /* 656 * Finally, check the static local and global variables. These checks 657 * take the longest, so we perform them last. 658 */ 659 if (dtrace_canstore_statvar(addr, sz, 660 vstate->dtvs_locals, vstate->dtvs_nlocals)) 661 return (1); 662 663 if (dtrace_canstore_statvar(addr, sz, 664 vstate->dtvs_globals, vstate->dtvs_nglobals)) 665 return (1); 666 667 return (0); 668 } 669 670 671 /* 672 * Convenience routine to check to see if the address is within a memory 673 * region in which a load may be issued given the user's privilege level; 674 * if not, it sets the appropriate error flags and loads 'addr' into the 675 * illegal value slot. 676 * 677 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 678 * appropriate memory access protection. 679 */ 680 static int 681 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 682 dtrace_vstate_t *vstate) 683 { 684 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 685 686 /* 687 * If we hold the privilege to read from kernel memory, then 688 * everything is readable. 689 */ 690 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 691 return (1); 692 693 /* 694 * You can obviously read that which you can store. 695 */ 696 if (dtrace_canstore(addr, sz, mstate, vstate)) 697 return (1); 698 699 /* 700 * We're allowed to read from our own string table. 701 */ 702 if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab, 703 mstate->dtms_difo->dtdo_strlen)) 704 return (1); 705 706 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 707 *illval = addr; 708 return (0); 709 } 710 711 /* 712 * Convenience routine to check to see if a given string is within a memory 713 * region in which a load may be issued given the user's privilege level; 714 * this exists so that we don't need to issue unnecessary dtrace_strlen() 715 * calls in the event that the user has all privileges. 716 */ 717 static int 718 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 719 dtrace_vstate_t *vstate) 720 { 721 size_t strsz; 722 723 /* 724 * If we hold the privilege to read from kernel memory, then 725 * everything is readable. 726 */ 727 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 728 return (1); 729 730 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 731 if (dtrace_canload(addr, strsz, mstate, vstate)) 732 return (1); 733 734 return (0); 735 } 736 737 /* 738 * Convenience routine to check to see if a given variable is within a memory 739 * region in which a load may be issued given the user's privilege level. 740 */ 741 static int 742 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 743 dtrace_vstate_t *vstate) 744 { 745 size_t sz; 746 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 747 748 /* 749 * If we hold the privilege to read from kernel memory, then 750 * everything is readable. 751 */ 752 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 753 return (1); 754 755 if (type->dtdt_kind == DIF_TYPE_STRING) 756 sz = dtrace_strlen(src, 757 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 758 else 759 sz = type->dtdt_size; 760 761 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 762 } 763 764 /* 765 * Compare two strings using safe loads. 766 */ 767 static int 768 dtrace_strncmp(char *s1, char *s2, size_t limit) 769 { 770 uint8_t c1, c2; 771 volatile uint16_t *flags; 772 773 if (s1 == s2 || limit == 0) 774 return (0); 775 776 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 777 778 do { 779 if (s1 == NULL) { 780 c1 = '\0'; 781 } else { 782 c1 = dtrace_load8((uintptr_t)s1++); 783 } 784 785 if (s2 == NULL) { 786 c2 = '\0'; 787 } else { 788 c2 = dtrace_load8((uintptr_t)s2++); 789 } 790 791 if (c1 != c2) 792 return (c1 - c2); 793 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 794 795 return (0); 796 } 797 798 /* 799 * Compute strlen(s) for a string using safe memory accesses. The additional 800 * len parameter is used to specify a maximum length to ensure completion. 801 */ 802 static size_t 803 dtrace_strlen(const char *s, size_t lim) 804 { 805 uint_t len; 806 807 for (len = 0; len != lim; len++) { 808 if (dtrace_load8((uintptr_t)s++) == '\0') 809 break; 810 } 811 812 return (len); 813 } 814 815 /* 816 * Check if an address falls within a toxic region. 817 */ 818 static int 819 dtrace_istoxic(uintptr_t kaddr, size_t size) 820 { 821 uintptr_t taddr, tsize; 822 int i; 823 824 for (i = 0; i < dtrace_toxranges; i++) { 825 taddr = dtrace_toxrange[i].dtt_base; 826 tsize = dtrace_toxrange[i].dtt_limit - taddr; 827 828 if (kaddr - taddr < tsize) { 829 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 830 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr; 831 return (1); 832 } 833 834 if (taddr - kaddr < size) { 835 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 836 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr; 837 return (1); 838 } 839 } 840 841 return (0); 842 } 843 844 /* 845 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 846 * memory specified by the DIF program. The dst is assumed to be safe memory 847 * that we can store to directly because it is managed by DTrace. As with 848 * standard bcopy, overlapping copies are handled properly. 849 */ 850 static void 851 dtrace_bcopy(const void *src, void *dst, size_t len) 852 { 853 if (len != 0) { 854 uint8_t *s1 = dst; 855 const uint8_t *s2 = src; 856 857 if (s1 <= s2) { 858 do { 859 *s1++ = dtrace_load8((uintptr_t)s2++); 860 } while (--len != 0); 861 } else { 862 s2 += len; 863 s1 += len; 864 865 do { 866 *--s1 = dtrace_load8((uintptr_t)--s2); 867 } while (--len != 0); 868 } 869 } 870 } 871 872 /* 873 * Copy src to dst using safe memory accesses, up to either the specified 874 * length, or the point that a nul byte is encountered. The src is assumed to 875 * be unsafe memory specified by the DIF program. The dst is assumed to be 876 * safe memory that we can store to directly because it is managed by DTrace. 877 * Unlike dtrace_bcopy(), overlapping regions are not handled. 878 */ 879 static void 880 dtrace_strcpy(const void *src, void *dst, size_t len) 881 { 882 if (len != 0) { 883 uint8_t *s1 = dst, c; 884 const uint8_t *s2 = src; 885 886 do { 887 *s1++ = c = dtrace_load8((uintptr_t)s2++); 888 } while (--len != 0 && c != '\0'); 889 } 890 } 891 892 /* 893 * Copy src to dst, deriving the size and type from the specified (BYREF) 894 * variable type. The src is assumed to be unsafe memory specified by the DIF 895 * program. The dst is assumed to be DTrace variable memory that is of the 896 * specified type; we assume that we can store to directly. 897 */ 898 static void 899 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 900 { 901 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 902 903 if (type->dtdt_kind == DIF_TYPE_STRING) { 904 dtrace_strcpy(src, dst, type->dtdt_size); 905 } else { 906 dtrace_bcopy(src, dst, type->dtdt_size); 907 } 908 } 909 910 /* 911 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 912 * unsafe memory specified by the DIF program. The s2 data is assumed to be 913 * safe memory that we can access directly because it is managed by DTrace. 914 */ 915 static int 916 dtrace_bcmp(const void *s1, const void *s2, size_t len) 917 { 918 volatile uint16_t *flags; 919 920 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 921 922 if (s1 == s2) 923 return (0); 924 925 if (s1 == NULL || s2 == NULL) 926 return (1); 927 928 if (s1 != s2 && len != 0) { 929 const uint8_t *ps1 = s1; 930 const uint8_t *ps2 = s2; 931 932 do { 933 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 934 return (1); 935 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 936 } 937 return (0); 938 } 939 940 /* 941 * Zero the specified region using a simple byte-by-byte loop. Note that this 942 * is for safe DTrace-managed memory only. 943 */ 944 static void 945 dtrace_bzero(void *dst, size_t len) 946 { 947 uchar_t *cp; 948 949 for (cp = dst; len != 0; len--) 950 *cp++ = 0; 951 } 952 953 static void 954 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 955 { 956 uint64_t result[2]; 957 958 result[0] = addend1[0] + addend2[0]; 959 result[1] = addend1[1] + addend2[1] + 960 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 961 962 sum[0] = result[0]; 963 sum[1] = result[1]; 964 } 965 966 /* 967 * Shift the 128-bit value in a by b. If b is positive, shift left. 968 * If b is negative, shift right. 969 */ 970 static void 971 dtrace_shift_128(uint64_t *a, int b) 972 { 973 uint64_t mask; 974 975 if (b == 0) 976 return; 977 978 if (b < 0) { 979 b = -b; 980 if (b >= 64) { 981 a[0] = a[1] >> (b - 64); 982 a[1] = 0; 983 } else { 984 a[0] >>= b; 985 mask = 1LL << (64 - b); 986 mask -= 1; 987 a[0] |= ((a[1] & mask) << (64 - b)); 988 a[1] >>= b; 989 } 990 } else { 991 if (b >= 64) { 992 a[1] = a[0] << (b - 64); 993 a[0] = 0; 994 } else { 995 a[1] <<= b; 996 mask = a[0] >> (64 - b); 997 a[1] |= mask; 998 a[0] <<= b; 999 } 1000 } 1001 } 1002 1003 /* 1004 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1005 * use native multiplication on those, and then re-combine into the 1006 * resulting 128-bit value. 1007 * 1008 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1009 * hi1 * hi2 << 64 + 1010 * hi1 * lo2 << 32 + 1011 * hi2 * lo1 << 32 + 1012 * lo1 * lo2 1013 */ 1014 static void 1015 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1016 { 1017 uint64_t hi1, hi2, lo1, lo2; 1018 uint64_t tmp[2]; 1019 1020 hi1 = factor1 >> 32; 1021 hi2 = factor2 >> 32; 1022 1023 lo1 = factor1 & DT_MASK_LO; 1024 lo2 = factor2 & DT_MASK_LO; 1025 1026 product[0] = lo1 * lo2; 1027 product[1] = hi1 * hi2; 1028 1029 tmp[0] = hi1 * lo2; 1030 tmp[1] = 0; 1031 dtrace_shift_128(tmp, 32); 1032 dtrace_add_128(product, tmp, product); 1033 1034 tmp[0] = hi2 * lo1; 1035 tmp[1] = 0; 1036 dtrace_shift_128(tmp, 32); 1037 dtrace_add_128(product, tmp, product); 1038 } 1039 1040 /* 1041 * This privilege check should be used by actions and subroutines to 1042 * verify that the user credentials of the process that enabled the 1043 * invoking ECB match the target credentials 1044 */ 1045 static int 1046 dtrace_priv_proc_common_user(dtrace_state_t *state) 1047 { 1048 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1049 1050 /* 1051 * We should always have a non-NULL state cred here, since if cred 1052 * is null (anonymous tracing), we fast-path bypass this routine. 1053 */ 1054 ASSERT(s_cr != NULL); 1055 1056 if ((cr = CRED()) != NULL && 1057 s_cr->cr_uid == cr->cr_uid && 1058 s_cr->cr_uid == cr->cr_ruid && 1059 s_cr->cr_uid == cr->cr_suid && 1060 s_cr->cr_gid == cr->cr_gid && 1061 s_cr->cr_gid == cr->cr_rgid && 1062 s_cr->cr_gid == cr->cr_sgid) 1063 return (1); 1064 1065 return (0); 1066 } 1067 1068 /* 1069 * This privilege check should be used by actions and subroutines to 1070 * verify that the zone of the process that enabled the invoking ECB 1071 * matches the target credentials 1072 */ 1073 static int 1074 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1075 { 1076 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1077 1078 /* 1079 * We should always have a non-NULL state cred here, since if cred 1080 * is null (anonymous tracing), we fast-path bypass this routine. 1081 */ 1082 ASSERT(s_cr != NULL); 1083 1084 if ((cr = CRED()) != NULL && 1085 s_cr->cr_zone == cr->cr_zone) 1086 return (1); 1087 1088 return (0); 1089 } 1090 1091 /* 1092 * This privilege check should be used by actions and subroutines to 1093 * verify that the process has not setuid or changed credentials. 1094 */ 1095 static int 1096 dtrace_priv_proc_common_nocd() 1097 { 1098 proc_t *proc; 1099 1100 if ((proc = ttoproc(curthread)) != NULL && 1101 !(proc->p_flag & SNOCD)) 1102 return (1); 1103 1104 return (0); 1105 } 1106 1107 static int 1108 dtrace_priv_proc_destructive(dtrace_state_t *state) 1109 { 1110 int action = state->dts_cred.dcr_action; 1111 1112 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1113 dtrace_priv_proc_common_zone(state) == 0) 1114 goto bad; 1115 1116 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1117 dtrace_priv_proc_common_user(state) == 0) 1118 goto bad; 1119 1120 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1121 dtrace_priv_proc_common_nocd() == 0) 1122 goto bad; 1123 1124 return (1); 1125 1126 bad: 1127 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1128 1129 return (0); 1130 } 1131 1132 static int 1133 dtrace_priv_proc_control(dtrace_state_t *state) 1134 { 1135 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1136 return (1); 1137 1138 if (dtrace_priv_proc_common_zone(state) && 1139 dtrace_priv_proc_common_user(state) && 1140 dtrace_priv_proc_common_nocd()) 1141 return (1); 1142 1143 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1144 1145 return (0); 1146 } 1147 1148 static int 1149 dtrace_priv_proc(dtrace_state_t *state) 1150 { 1151 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1152 return (1); 1153 1154 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1155 1156 return (0); 1157 } 1158 1159 static int 1160 dtrace_priv_kernel(dtrace_state_t *state) 1161 { 1162 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1163 return (1); 1164 1165 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1166 1167 return (0); 1168 } 1169 1170 static int 1171 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1172 { 1173 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1174 return (1); 1175 1176 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1177 1178 return (0); 1179 } 1180 1181 /* 1182 * Note: not called from probe context. This function is called 1183 * asynchronously (and at a regular interval) from outside of probe context to 1184 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1185 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1186 */ 1187 void 1188 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1189 { 1190 dtrace_dynvar_t *dirty; 1191 dtrace_dstate_percpu_t *dcpu; 1192 dtrace_dynvar_t **rinsep; 1193 int i, j, work = 0; 1194 1195 for (i = 0; i < NCPU; i++) { 1196 dcpu = &dstate->dtds_percpu[i]; 1197 rinsep = &dcpu->dtdsc_rinsing; 1198 1199 /* 1200 * If the dirty list is NULL, there is no dirty work to do. 1201 */ 1202 if (dcpu->dtdsc_dirty == NULL) 1203 continue; 1204 1205 if (dcpu->dtdsc_rinsing != NULL) { 1206 /* 1207 * If the rinsing list is non-NULL, then it is because 1208 * this CPU was selected to accept another CPU's 1209 * dirty list -- and since that time, dirty buffers 1210 * have accumulated. This is a highly unlikely 1211 * condition, but we choose to ignore the dirty 1212 * buffers -- they'll be picked up a future cleanse. 1213 */ 1214 continue; 1215 } 1216 1217 if (dcpu->dtdsc_clean != NULL) { 1218 /* 1219 * If the clean list is non-NULL, then we're in a 1220 * situation where a CPU has done deallocations (we 1221 * have a non-NULL dirty list) but no allocations (we 1222 * also have a non-NULL clean list). We can't simply 1223 * move the dirty list into the clean list on this 1224 * CPU, yet we also don't want to allow this condition 1225 * to persist, lest a short clean list prevent a 1226 * massive dirty list from being cleaned (which in 1227 * turn could lead to otherwise avoidable dynamic 1228 * drops). To deal with this, we look for some CPU 1229 * with a NULL clean list, NULL dirty list, and NULL 1230 * rinsing list -- and then we borrow this CPU to 1231 * rinse our dirty list. 1232 */ 1233 for (j = 0; j < NCPU; j++) { 1234 dtrace_dstate_percpu_t *rinser; 1235 1236 rinser = &dstate->dtds_percpu[j]; 1237 1238 if (rinser->dtdsc_rinsing != NULL) 1239 continue; 1240 1241 if (rinser->dtdsc_dirty != NULL) 1242 continue; 1243 1244 if (rinser->dtdsc_clean != NULL) 1245 continue; 1246 1247 rinsep = &rinser->dtdsc_rinsing; 1248 break; 1249 } 1250 1251 if (j == NCPU) { 1252 /* 1253 * We were unable to find another CPU that 1254 * could accept this dirty list -- we are 1255 * therefore unable to clean it now. 1256 */ 1257 dtrace_dynvar_failclean++; 1258 continue; 1259 } 1260 } 1261 1262 work = 1; 1263 1264 /* 1265 * Atomically move the dirty list aside. 1266 */ 1267 do { 1268 dirty = dcpu->dtdsc_dirty; 1269 1270 /* 1271 * Before we zap the dirty list, set the rinsing list. 1272 * (This allows for a potential assertion in 1273 * dtrace_dynvar(): if a free dynamic variable appears 1274 * on a hash chain, either the dirty list or the 1275 * rinsing list for some CPU must be non-NULL.) 1276 */ 1277 *rinsep = dirty; 1278 dtrace_membar_producer(); 1279 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1280 dirty, NULL) != dirty); 1281 } 1282 1283 if (!work) { 1284 /* 1285 * We have no work to do; we can simply return. 1286 */ 1287 return; 1288 } 1289 1290 dtrace_sync(); 1291 1292 for (i = 0; i < NCPU; i++) { 1293 dcpu = &dstate->dtds_percpu[i]; 1294 1295 if (dcpu->dtdsc_rinsing == NULL) 1296 continue; 1297 1298 /* 1299 * We are now guaranteed that no hash chain contains a pointer 1300 * into this dirty list; we can make it clean. 1301 */ 1302 ASSERT(dcpu->dtdsc_clean == NULL); 1303 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1304 dcpu->dtdsc_rinsing = NULL; 1305 } 1306 1307 /* 1308 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1309 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1310 * This prevents a race whereby a CPU incorrectly decides that 1311 * the state should be something other than DTRACE_DSTATE_CLEAN 1312 * after dtrace_dynvar_clean() has completed. 1313 */ 1314 dtrace_sync(); 1315 1316 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1317 } 1318 1319 /* 1320 * Depending on the value of the op parameter, this function looks-up, 1321 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1322 * allocation is requested, this function will return a pointer to a 1323 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1324 * variable can be allocated. If NULL is returned, the appropriate counter 1325 * will be incremented. 1326 */ 1327 dtrace_dynvar_t * 1328 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1329 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1330 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1331 { 1332 uint64_t hashval = DTRACE_DYNHASH_VALID; 1333 dtrace_dynhash_t *hash = dstate->dtds_hash; 1334 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1335 processorid_t me = CPU->cpu_id, cpu = me; 1336 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1337 size_t bucket, ksize; 1338 size_t chunksize = dstate->dtds_chunksize; 1339 uintptr_t kdata, lock, nstate; 1340 uint_t i; 1341 1342 ASSERT(nkeys != 0); 1343 1344 /* 1345 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1346 * algorithm. For the by-value portions, we perform the algorithm in 1347 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1348 * bit, and seems to have only a minute effect on distribution. For 1349 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1350 * over each referenced byte. It's painful to do this, but it's much 1351 * better than pathological hash distribution. The efficacy of the 1352 * hashing algorithm (and a comparison with other algorithms) may be 1353 * found by running the ::dtrace_dynstat MDB dcmd. 1354 */ 1355 for (i = 0; i < nkeys; i++) { 1356 if (key[i].dttk_size == 0) { 1357 uint64_t val = key[i].dttk_value; 1358 1359 hashval += (val >> 48) & 0xffff; 1360 hashval += (hashval << 10); 1361 hashval ^= (hashval >> 6); 1362 1363 hashval += (val >> 32) & 0xffff; 1364 hashval += (hashval << 10); 1365 hashval ^= (hashval >> 6); 1366 1367 hashval += (val >> 16) & 0xffff; 1368 hashval += (hashval << 10); 1369 hashval ^= (hashval >> 6); 1370 1371 hashval += val & 0xffff; 1372 hashval += (hashval << 10); 1373 hashval ^= (hashval >> 6); 1374 } else { 1375 /* 1376 * This is incredibly painful, but it beats the hell 1377 * out of the alternative. 1378 */ 1379 uint64_t j, size = key[i].dttk_size; 1380 uintptr_t base = (uintptr_t)key[i].dttk_value; 1381 1382 if (!dtrace_canload(base, size, mstate, vstate)) 1383 break; 1384 1385 for (j = 0; j < size; j++) { 1386 hashval += dtrace_load8(base + j); 1387 hashval += (hashval << 10); 1388 hashval ^= (hashval >> 6); 1389 } 1390 } 1391 } 1392 1393 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1394 return (NULL); 1395 1396 hashval += (hashval << 3); 1397 hashval ^= (hashval >> 11); 1398 hashval += (hashval << 15); 1399 1400 /* 1401 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1402 * comes out to be one of our two sentinel hash values. If this 1403 * actually happens, we set the hashval to be a value known to be a 1404 * non-sentinel value. 1405 */ 1406 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1407 hashval = DTRACE_DYNHASH_VALID; 1408 1409 /* 1410 * Yes, it's painful to do a divide here. If the cycle count becomes 1411 * important here, tricks can be pulled to reduce it. (However, it's 1412 * critical that hash collisions be kept to an absolute minimum; 1413 * they're much more painful than a divide.) It's better to have a 1414 * solution that generates few collisions and still keeps things 1415 * relatively simple. 1416 */ 1417 bucket = hashval % dstate->dtds_hashsize; 1418 1419 if (op == DTRACE_DYNVAR_DEALLOC) { 1420 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1421 1422 for (;;) { 1423 while ((lock = *lockp) & 1) 1424 continue; 1425 1426 if (dtrace_casptr((void *)lockp, 1427 (void *)lock, (void *)(lock + 1)) == (void *)lock) 1428 break; 1429 } 1430 1431 dtrace_membar_producer(); 1432 } 1433 1434 top: 1435 prev = NULL; 1436 lock = hash[bucket].dtdh_lock; 1437 1438 dtrace_membar_consumer(); 1439 1440 start = hash[bucket].dtdh_chain; 1441 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1442 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1443 op != DTRACE_DYNVAR_DEALLOC)); 1444 1445 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1446 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1447 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1448 1449 if (dvar->dtdv_hashval != hashval) { 1450 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1451 /* 1452 * We've reached the sink, and therefore the 1453 * end of the hash chain; we can kick out of 1454 * the loop knowing that we have seen a valid 1455 * snapshot of state. 1456 */ 1457 ASSERT(dvar->dtdv_next == NULL); 1458 ASSERT(dvar == &dtrace_dynhash_sink); 1459 break; 1460 } 1461 1462 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1463 /* 1464 * We've gone off the rails: somewhere along 1465 * the line, one of the members of this hash 1466 * chain was deleted. Note that we could also 1467 * detect this by simply letting this loop run 1468 * to completion, as we would eventually hit 1469 * the end of the dirty list. However, we 1470 * want to avoid running the length of the 1471 * dirty list unnecessarily (it might be quite 1472 * long), so we catch this as early as 1473 * possible by detecting the hash marker. In 1474 * this case, we simply set dvar to NULL and 1475 * break; the conditional after the loop will 1476 * send us back to top. 1477 */ 1478 dvar = NULL; 1479 break; 1480 } 1481 1482 goto next; 1483 } 1484 1485 if (dtuple->dtt_nkeys != nkeys) 1486 goto next; 1487 1488 for (i = 0; i < nkeys; i++, dkey++) { 1489 if (dkey->dttk_size != key[i].dttk_size) 1490 goto next; /* size or type mismatch */ 1491 1492 if (dkey->dttk_size != 0) { 1493 if (dtrace_bcmp( 1494 (void *)(uintptr_t)key[i].dttk_value, 1495 (void *)(uintptr_t)dkey->dttk_value, 1496 dkey->dttk_size)) 1497 goto next; 1498 } else { 1499 if (dkey->dttk_value != key[i].dttk_value) 1500 goto next; 1501 } 1502 } 1503 1504 if (op != DTRACE_DYNVAR_DEALLOC) 1505 return (dvar); 1506 1507 ASSERT(dvar->dtdv_next == NULL || 1508 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1509 1510 if (prev != NULL) { 1511 ASSERT(hash[bucket].dtdh_chain != dvar); 1512 ASSERT(start != dvar); 1513 ASSERT(prev->dtdv_next == dvar); 1514 prev->dtdv_next = dvar->dtdv_next; 1515 } else { 1516 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1517 start, dvar->dtdv_next) != start) { 1518 /* 1519 * We have failed to atomically swing the 1520 * hash table head pointer, presumably because 1521 * of a conflicting allocation on another CPU. 1522 * We need to reread the hash chain and try 1523 * again. 1524 */ 1525 goto top; 1526 } 1527 } 1528 1529 dtrace_membar_producer(); 1530 1531 /* 1532 * Now set the hash value to indicate that it's free. 1533 */ 1534 ASSERT(hash[bucket].dtdh_chain != dvar); 1535 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1536 1537 dtrace_membar_producer(); 1538 1539 /* 1540 * Set the next pointer to point at the dirty list, and 1541 * atomically swing the dirty pointer to the newly freed dvar. 1542 */ 1543 do { 1544 next = dcpu->dtdsc_dirty; 1545 dvar->dtdv_next = next; 1546 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1547 1548 /* 1549 * Finally, unlock this hash bucket. 1550 */ 1551 ASSERT(hash[bucket].dtdh_lock == lock); 1552 ASSERT(lock & 1); 1553 hash[bucket].dtdh_lock++; 1554 1555 return (NULL); 1556 next: 1557 prev = dvar; 1558 continue; 1559 } 1560 1561 if (dvar == NULL) { 1562 /* 1563 * If dvar is NULL, it is because we went off the rails: 1564 * one of the elements that we traversed in the hash chain 1565 * was deleted while we were traversing it. In this case, 1566 * we assert that we aren't doing a dealloc (deallocs lock 1567 * the hash bucket to prevent themselves from racing with 1568 * one another), and retry the hash chain traversal. 1569 */ 1570 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1571 goto top; 1572 } 1573 1574 if (op != DTRACE_DYNVAR_ALLOC) { 1575 /* 1576 * If we are not to allocate a new variable, we want to 1577 * return NULL now. Before we return, check that the value 1578 * of the lock word hasn't changed. If it has, we may have 1579 * seen an inconsistent snapshot. 1580 */ 1581 if (op == DTRACE_DYNVAR_NOALLOC) { 1582 if (hash[bucket].dtdh_lock != lock) 1583 goto top; 1584 } else { 1585 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1586 ASSERT(hash[bucket].dtdh_lock == lock); 1587 ASSERT(lock & 1); 1588 hash[bucket].dtdh_lock++; 1589 } 1590 1591 return (NULL); 1592 } 1593 1594 /* 1595 * We need to allocate a new dynamic variable. The size we need is the 1596 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1597 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1598 * the size of any referred-to data (dsize). We then round the final 1599 * size up to the chunksize for allocation. 1600 */ 1601 for (ksize = 0, i = 0; i < nkeys; i++) 1602 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1603 1604 /* 1605 * This should be pretty much impossible, but could happen if, say, 1606 * strange DIF specified the tuple. Ideally, this should be an 1607 * assertion and not an error condition -- but that requires that the 1608 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1609 * bullet-proof. (That is, it must not be able to be fooled by 1610 * malicious DIF.) Given the lack of backwards branches in DIF, 1611 * solving this would presumably not amount to solving the Halting 1612 * Problem -- but it still seems awfully hard. 1613 */ 1614 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1615 ksize + dsize > chunksize) { 1616 dcpu->dtdsc_drops++; 1617 return (NULL); 1618 } 1619 1620 nstate = DTRACE_DSTATE_EMPTY; 1621 1622 do { 1623 retry: 1624 free = dcpu->dtdsc_free; 1625 1626 if (free == NULL) { 1627 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1628 void *rval; 1629 1630 if (clean == NULL) { 1631 /* 1632 * We're out of dynamic variable space on 1633 * this CPU. Unless we have tried all CPUs, 1634 * we'll try to allocate from a different 1635 * CPU. 1636 */ 1637 switch (dstate->dtds_state) { 1638 case DTRACE_DSTATE_CLEAN: { 1639 void *sp = &dstate->dtds_state; 1640 1641 if (++cpu >= NCPU) 1642 cpu = 0; 1643 1644 if (dcpu->dtdsc_dirty != NULL && 1645 nstate == DTRACE_DSTATE_EMPTY) 1646 nstate = DTRACE_DSTATE_DIRTY; 1647 1648 if (dcpu->dtdsc_rinsing != NULL) 1649 nstate = DTRACE_DSTATE_RINSING; 1650 1651 dcpu = &dstate->dtds_percpu[cpu]; 1652 1653 if (cpu != me) 1654 goto retry; 1655 1656 (void) dtrace_cas32(sp, 1657 DTRACE_DSTATE_CLEAN, nstate); 1658 1659 /* 1660 * To increment the correct bean 1661 * counter, take another lap. 1662 */ 1663 goto retry; 1664 } 1665 1666 case DTRACE_DSTATE_DIRTY: 1667 dcpu->dtdsc_dirty_drops++; 1668 break; 1669 1670 case DTRACE_DSTATE_RINSING: 1671 dcpu->dtdsc_rinsing_drops++; 1672 break; 1673 1674 case DTRACE_DSTATE_EMPTY: 1675 dcpu->dtdsc_drops++; 1676 break; 1677 } 1678 1679 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1680 return (NULL); 1681 } 1682 1683 /* 1684 * The clean list appears to be non-empty. We want to 1685 * move the clean list to the free list; we start by 1686 * moving the clean pointer aside. 1687 */ 1688 if (dtrace_casptr(&dcpu->dtdsc_clean, 1689 clean, NULL) != clean) { 1690 /* 1691 * We are in one of two situations: 1692 * 1693 * (a) The clean list was switched to the 1694 * free list by another CPU. 1695 * 1696 * (b) The clean list was added to by the 1697 * cleansing cyclic. 1698 * 1699 * In either of these situations, we can 1700 * just reattempt the free list allocation. 1701 */ 1702 goto retry; 1703 } 1704 1705 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1706 1707 /* 1708 * Now we'll move the clean list to our free list. 1709 * It's impossible for this to fail: the only way 1710 * the free list can be updated is through this 1711 * code path, and only one CPU can own the clean list. 1712 * Thus, it would only be possible for this to fail if 1713 * this code were racing with dtrace_dynvar_clean(). 1714 * (That is, if dtrace_dynvar_clean() updated the clean 1715 * list, and we ended up racing to update the free 1716 * list.) This race is prevented by the dtrace_sync() 1717 * in dtrace_dynvar_clean() -- which flushes the 1718 * owners of the clean lists out before resetting 1719 * the clean lists. 1720 */ 1721 dcpu = &dstate->dtds_percpu[me]; 1722 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1723 ASSERT(rval == NULL); 1724 goto retry; 1725 } 1726 1727 dvar = free; 1728 new_free = dvar->dtdv_next; 1729 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1730 1731 /* 1732 * We have now allocated a new chunk. We copy the tuple keys into the 1733 * tuple array and copy any referenced key data into the data space 1734 * following the tuple array. As we do this, we relocate dttk_value 1735 * in the final tuple to point to the key data address in the chunk. 1736 */ 1737 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1738 dvar->dtdv_data = (void *)(kdata + ksize); 1739 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1740 1741 for (i = 0; i < nkeys; i++) { 1742 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1743 size_t kesize = key[i].dttk_size; 1744 1745 if (kesize != 0) { 1746 dtrace_bcopy( 1747 (const void *)(uintptr_t)key[i].dttk_value, 1748 (void *)kdata, kesize); 1749 dkey->dttk_value = kdata; 1750 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1751 } else { 1752 dkey->dttk_value = key[i].dttk_value; 1753 } 1754 1755 dkey->dttk_size = kesize; 1756 } 1757 1758 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1759 dvar->dtdv_hashval = hashval; 1760 dvar->dtdv_next = start; 1761 1762 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1763 return (dvar); 1764 1765 /* 1766 * The cas has failed. Either another CPU is adding an element to 1767 * this hash chain, or another CPU is deleting an element from this 1768 * hash chain. The simplest way to deal with both of these cases 1769 * (though not necessarily the most efficient) is to free our 1770 * allocated block and tail-call ourselves. Note that the free is 1771 * to the dirty list and _not_ to the free list. This is to prevent 1772 * races with allocators, above. 1773 */ 1774 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1775 1776 dtrace_membar_producer(); 1777 1778 do { 1779 free = dcpu->dtdsc_dirty; 1780 dvar->dtdv_next = free; 1781 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1782 1783 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 1784 } 1785 1786 /*ARGSUSED*/ 1787 static void 1788 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1789 { 1790 if ((int64_t)nval < (int64_t)*oval) 1791 *oval = nval; 1792 } 1793 1794 /*ARGSUSED*/ 1795 static void 1796 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1797 { 1798 if ((int64_t)nval > (int64_t)*oval) 1799 *oval = nval; 1800 } 1801 1802 static void 1803 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1804 { 1805 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1806 int64_t val = (int64_t)nval; 1807 1808 if (val < 0) { 1809 for (i = 0; i < zero; i++) { 1810 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1811 quanta[i] += incr; 1812 return; 1813 } 1814 } 1815 } else { 1816 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1817 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1818 quanta[i - 1] += incr; 1819 return; 1820 } 1821 } 1822 1823 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1824 return; 1825 } 1826 1827 ASSERT(0); 1828 } 1829 1830 static void 1831 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1832 { 1833 uint64_t arg = *lquanta++; 1834 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1835 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1836 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1837 int32_t val = (int32_t)nval, level; 1838 1839 ASSERT(step != 0); 1840 ASSERT(levels != 0); 1841 1842 if (val < base) { 1843 /* 1844 * This is an underflow. 1845 */ 1846 lquanta[0] += incr; 1847 return; 1848 } 1849 1850 level = (val - base) / step; 1851 1852 if (level < levels) { 1853 lquanta[level + 1] += incr; 1854 return; 1855 } 1856 1857 /* 1858 * This is an overflow. 1859 */ 1860 lquanta[levels + 1] += incr; 1861 } 1862 1863 /*ARGSUSED*/ 1864 static void 1865 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 1866 { 1867 data[0]++; 1868 data[1] += nval; 1869 } 1870 1871 /*ARGSUSED*/ 1872 static void 1873 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 1874 { 1875 int64_t snval = (int64_t)nval; 1876 uint64_t tmp[2]; 1877 1878 data[0]++; 1879 data[1] += nval; 1880 1881 /* 1882 * What we want to say here is: 1883 * 1884 * data[2] += nval * nval; 1885 * 1886 * But given that nval is 64-bit, we could easily overflow, so 1887 * we do this as 128-bit arithmetic. 1888 */ 1889 if (snval < 0) 1890 snval = -snval; 1891 1892 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 1893 dtrace_add_128(data + 2, tmp, data + 2); 1894 } 1895 1896 /*ARGSUSED*/ 1897 static void 1898 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 1899 { 1900 *oval = *oval + 1; 1901 } 1902 1903 /*ARGSUSED*/ 1904 static void 1905 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 1906 { 1907 *oval += nval; 1908 } 1909 1910 /* 1911 * Aggregate given the tuple in the principal data buffer, and the aggregating 1912 * action denoted by the specified dtrace_aggregation_t. The aggregation 1913 * buffer is specified as the buf parameter. This routine does not return 1914 * failure; if there is no space in the aggregation buffer, the data will be 1915 * dropped, and a corresponding counter incremented. 1916 */ 1917 static void 1918 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 1919 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 1920 { 1921 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 1922 uint32_t i, ndx, size, fsize; 1923 uint32_t align = sizeof (uint64_t) - 1; 1924 dtrace_aggbuffer_t *agb; 1925 dtrace_aggkey_t *key; 1926 uint32_t hashval = 0, limit, isstr; 1927 caddr_t tomax, data, kdata; 1928 dtrace_actkind_t action; 1929 dtrace_action_t *act; 1930 uintptr_t offs; 1931 1932 if (buf == NULL) 1933 return; 1934 1935 if (!agg->dtag_hasarg) { 1936 /* 1937 * Currently, only quantize() and lquantize() take additional 1938 * arguments, and they have the same semantics: an increment 1939 * value that defaults to 1 when not present. If additional 1940 * aggregating actions take arguments, the setting of the 1941 * default argument value will presumably have to become more 1942 * sophisticated... 1943 */ 1944 arg = 1; 1945 } 1946 1947 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 1948 size = rec->dtrd_offset - agg->dtag_base; 1949 fsize = size + rec->dtrd_size; 1950 1951 ASSERT(dbuf->dtb_tomax != NULL); 1952 data = dbuf->dtb_tomax + offset + agg->dtag_base; 1953 1954 if ((tomax = buf->dtb_tomax) == NULL) { 1955 dtrace_buffer_drop(buf); 1956 return; 1957 } 1958 1959 /* 1960 * The metastructure is always at the bottom of the buffer. 1961 */ 1962 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 1963 sizeof (dtrace_aggbuffer_t)); 1964 1965 if (buf->dtb_offset == 0) { 1966 /* 1967 * We just kludge up approximately 1/8th of the size to be 1968 * buckets. If this guess ends up being routinely 1969 * off-the-mark, we may need to dynamically readjust this 1970 * based on past performance. 1971 */ 1972 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 1973 1974 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 1975 (uintptr_t)tomax || hashsize == 0) { 1976 /* 1977 * We've been given a ludicrously small buffer; 1978 * increment our drop count and leave. 1979 */ 1980 dtrace_buffer_drop(buf); 1981 return; 1982 } 1983 1984 /* 1985 * And now, a pathetic attempt to try to get a an odd (or 1986 * perchance, a prime) hash size for better hash distribution. 1987 */ 1988 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 1989 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 1990 1991 agb->dtagb_hashsize = hashsize; 1992 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 1993 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 1994 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 1995 1996 for (i = 0; i < agb->dtagb_hashsize; i++) 1997 agb->dtagb_hash[i] = NULL; 1998 } 1999 2000 ASSERT(agg->dtag_first != NULL); 2001 ASSERT(agg->dtag_first->dta_intuple); 2002 2003 /* 2004 * Calculate the hash value based on the key. Note that we _don't_ 2005 * include the aggid in the hashing (but we will store it as part of 2006 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2007 * algorithm: a simple, quick algorithm that has no known funnels, and 2008 * gets good distribution in practice. The efficacy of the hashing 2009 * algorithm (and a comparison with other algorithms) may be found by 2010 * running the ::dtrace_aggstat MDB dcmd. 2011 */ 2012 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2013 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2014 limit = i + act->dta_rec.dtrd_size; 2015 ASSERT(limit <= size); 2016 isstr = DTRACEACT_ISSTRING(act); 2017 2018 for (; i < limit; i++) { 2019 hashval += data[i]; 2020 hashval += (hashval << 10); 2021 hashval ^= (hashval >> 6); 2022 2023 if (isstr && data[i] == '\0') 2024 break; 2025 } 2026 } 2027 2028 hashval += (hashval << 3); 2029 hashval ^= (hashval >> 11); 2030 hashval += (hashval << 15); 2031 2032 /* 2033 * Yes, the divide here is expensive -- but it's generally the least 2034 * of the performance issues given the amount of data that we iterate 2035 * over to compute hash values, compare data, etc. 2036 */ 2037 ndx = hashval % agb->dtagb_hashsize; 2038 2039 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2040 ASSERT((caddr_t)key >= tomax); 2041 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2042 2043 if (hashval != key->dtak_hashval || key->dtak_size != size) 2044 continue; 2045 2046 kdata = key->dtak_data; 2047 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2048 2049 for (act = agg->dtag_first; act->dta_intuple; 2050 act = act->dta_next) { 2051 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2052 limit = i + act->dta_rec.dtrd_size; 2053 ASSERT(limit <= size); 2054 isstr = DTRACEACT_ISSTRING(act); 2055 2056 for (; i < limit; i++) { 2057 if (kdata[i] != data[i]) 2058 goto next; 2059 2060 if (isstr && data[i] == '\0') 2061 break; 2062 } 2063 } 2064 2065 if (action != key->dtak_action) { 2066 /* 2067 * We are aggregating on the same value in the same 2068 * aggregation with two different aggregating actions. 2069 * (This should have been picked up in the compiler, 2070 * so we may be dealing with errant or devious DIF.) 2071 * This is an error condition; we indicate as much, 2072 * and return. 2073 */ 2074 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2075 return; 2076 } 2077 2078 /* 2079 * This is a hit: we need to apply the aggregator to 2080 * the value at this key. 2081 */ 2082 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2083 return; 2084 next: 2085 continue; 2086 } 2087 2088 /* 2089 * We didn't find it. We need to allocate some zero-filled space, 2090 * link it into the hash table appropriately, and apply the aggregator 2091 * to the (zero-filled) value. 2092 */ 2093 offs = buf->dtb_offset; 2094 while (offs & (align - 1)) 2095 offs += sizeof (uint32_t); 2096 2097 /* 2098 * If we don't have enough room to both allocate a new key _and_ 2099 * its associated data, increment the drop count and return. 2100 */ 2101 if ((uintptr_t)tomax + offs + fsize > 2102 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2103 dtrace_buffer_drop(buf); 2104 return; 2105 } 2106 2107 /*CONSTCOND*/ 2108 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2109 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2110 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2111 2112 key->dtak_data = kdata = tomax + offs; 2113 buf->dtb_offset = offs + fsize; 2114 2115 /* 2116 * Now copy the data across. 2117 */ 2118 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2119 2120 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2121 kdata[i] = data[i]; 2122 2123 /* 2124 * Because strings are not zeroed out by default, we need to iterate 2125 * looking for actions that store strings, and we need to explicitly 2126 * pad these strings out with zeroes. 2127 */ 2128 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2129 int nul; 2130 2131 if (!DTRACEACT_ISSTRING(act)) 2132 continue; 2133 2134 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2135 limit = i + act->dta_rec.dtrd_size; 2136 ASSERT(limit <= size); 2137 2138 for (nul = 0; i < limit; i++) { 2139 if (nul) { 2140 kdata[i] = '\0'; 2141 continue; 2142 } 2143 2144 if (data[i] != '\0') 2145 continue; 2146 2147 nul = 1; 2148 } 2149 } 2150 2151 for (i = size; i < fsize; i++) 2152 kdata[i] = 0; 2153 2154 key->dtak_hashval = hashval; 2155 key->dtak_size = size; 2156 key->dtak_action = action; 2157 key->dtak_next = agb->dtagb_hash[ndx]; 2158 agb->dtagb_hash[ndx] = key; 2159 2160 /* 2161 * Finally, apply the aggregator. 2162 */ 2163 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2164 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2165 } 2166 2167 /* 2168 * Given consumer state, this routine finds a speculation in the INACTIVE 2169 * state and transitions it into the ACTIVE state. If there is no speculation 2170 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2171 * incremented -- it is up to the caller to take appropriate action. 2172 */ 2173 static int 2174 dtrace_speculation(dtrace_state_t *state) 2175 { 2176 int i = 0; 2177 dtrace_speculation_state_t current; 2178 uint32_t *stat = &state->dts_speculations_unavail, count; 2179 2180 while (i < state->dts_nspeculations) { 2181 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2182 2183 current = spec->dtsp_state; 2184 2185 if (current != DTRACESPEC_INACTIVE) { 2186 if (current == DTRACESPEC_COMMITTINGMANY || 2187 current == DTRACESPEC_COMMITTING || 2188 current == DTRACESPEC_DISCARDING) 2189 stat = &state->dts_speculations_busy; 2190 i++; 2191 continue; 2192 } 2193 2194 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2195 current, DTRACESPEC_ACTIVE) == current) 2196 return (i + 1); 2197 } 2198 2199 /* 2200 * We couldn't find a speculation. If we found as much as a single 2201 * busy speculation buffer, we'll attribute this failure as "busy" 2202 * instead of "unavail". 2203 */ 2204 do { 2205 count = *stat; 2206 } while (dtrace_cas32(stat, count, count + 1) != count); 2207 2208 return (0); 2209 } 2210 2211 /* 2212 * This routine commits an active speculation. If the specified speculation 2213 * is not in a valid state to perform a commit(), this routine will silently do 2214 * nothing. The state of the specified speculation is transitioned according 2215 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2216 */ 2217 static void 2218 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2219 dtrace_specid_t which) 2220 { 2221 dtrace_speculation_t *spec; 2222 dtrace_buffer_t *src, *dest; 2223 uintptr_t daddr, saddr, dlimit; 2224 dtrace_speculation_state_t current, new; 2225 intptr_t offs; 2226 2227 if (which == 0) 2228 return; 2229 2230 if (which > state->dts_nspeculations) { 2231 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2232 return; 2233 } 2234 2235 spec = &state->dts_speculations[which - 1]; 2236 src = &spec->dtsp_buffer[cpu]; 2237 dest = &state->dts_buffer[cpu]; 2238 2239 do { 2240 current = spec->dtsp_state; 2241 2242 if (current == DTRACESPEC_COMMITTINGMANY) 2243 break; 2244 2245 switch (current) { 2246 case DTRACESPEC_INACTIVE: 2247 case DTRACESPEC_DISCARDING: 2248 return; 2249 2250 case DTRACESPEC_COMMITTING: 2251 /* 2252 * This is only possible if we are (a) commit()'ing 2253 * without having done a prior speculate() on this CPU 2254 * and (b) racing with another commit() on a different 2255 * CPU. There's nothing to do -- we just assert that 2256 * our offset is 0. 2257 */ 2258 ASSERT(src->dtb_offset == 0); 2259 return; 2260 2261 case DTRACESPEC_ACTIVE: 2262 new = DTRACESPEC_COMMITTING; 2263 break; 2264 2265 case DTRACESPEC_ACTIVEONE: 2266 /* 2267 * This speculation is active on one CPU. If our 2268 * buffer offset is non-zero, we know that the one CPU 2269 * must be us. Otherwise, we are committing on a 2270 * different CPU from the speculate(), and we must 2271 * rely on being asynchronously cleaned. 2272 */ 2273 if (src->dtb_offset != 0) { 2274 new = DTRACESPEC_COMMITTING; 2275 break; 2276 } 2277 /*FALLTHROUGH*/ 2278 2279 case DTRACESPEC_ACTIVEMANY: 2280 new = DTRACESPEC_COMMITTINGMANY; 2281 break; 2282 2283 default: 2284 ASSERT(0); 2285 } 2286 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2287 current, new) != current); 2288 2289 /* 2290 * We have set the state to indicate that we are committing this 2291 * speculation. Now reserve the necessary space in the destination 2292 * buffer. 2293 */ 2294 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2295 sizeof (uint64_t), state, NULL)) < 0) { 2296 dtrace_buffer_drop(dest); 2297 goto out; 2298 } 2299 2300 /* 2301 * We have the space; copy the buffer across. (Note that this is a 2302 * highly subobtimal bcopy(); in the unlikely event that this becomes 2303 * a serious performance issue, a high-performance DTrace-specific 2304 * bcopy() should obviously be invented.) 2305 */ 2306 daddr = (uintptr_t)dest->dtb_tomax + offs; 2307 dlimit = daddr + src->dtb_offset; 2308 saddr = (uintptr_t)src->dtb_tomax; 2309 2310 /* 2311 * First, the aligned portion. 2312 */ 2313 while (dlimit - daddr >= sizeof (uint64_t)) { 2314 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2315 2316 daddr += sizeof (uint64_t); 2317 saddr += sizeof (uint64_t); 2318 } 2319 2320 /* 2321 * Now any left-over bit... 2322 */ 2323 while (dlimit - daddr) 2324 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2325 2326 /* 2327 * Finally, commit the reserved space in the destination buffer. 2328 */ 2329 dest->dtb_offset = offs + src->dtb_offset; 2330 2331 out: 2332 /* 2333 * If we're lucky enough to be the only active CPU on this speculation 2334 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2335 */ 2336 if (current == DTRACESPEC_ACTIVE || 2337 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2338 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2339 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2340 2341 ASSERT(rval == DTRACESPEC_COMMITTING); 2342 } 2343 2344 src->dtb_offset = 0; 2345 src->dtb_xamot_drops += src->dtb_drops; 2346 src->dtb_drops = 0; 2347 } 2348 2349 /* 2350 * This routine discards an active speculation. If the specified speculation 2351 * is not in a valid state to perform a discard(), this routine will silently 2352 * do nothing. The state of the specified speculation is transitioned 2353 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2354 */ 2355 static void 2356 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2357 dtrace_specid_t which) 2358 { 2359 dtrace_speculation_t *spec; 2360 dtrace_speculation_state_t current, new; 2361 dtrace_buffer_t *buf; 2362 2363 if (which == 0) 2364 return; 2365 2366 if (which > state->dts_nspeculations) { 2367 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2368 return; 2369 } 2370 2371 spec = &state->dts_speculations[which - 1]; 2372 buf = &spec->dtsp_buffer[cpu]; 2373 2374 do { 2375 current = spec->dtsp_state; 2376 2377 switch (current) { 2378 case DTRACESPEC_INACTIVE: 2379 case DTRACESPEC_COMMITTINGMANY: 2380 case DTRACESPEC_COMMITTING: 2381 case DTRACESPEC_DISCARDING: 2382 return; 2383 2384 case DTRACESPEC_ACTIVE: 2385 case DTRACESPEC_ACTIVEMANY: 2386 new = DTRACESPEC_DISCARDING; 2387 break; 2388 2389 case DTRACESPEC_ACTIVEONE: 2390 if (buf->dtb_offset != 0) { 2391 new = DTRACESPEC_INACTIVE; 2392 } else { 2393 new = DTRACESPEC_DISCARDING; 2394 } 2395 break; 2396 2397 default: 2398 ASSERT(0); 2399 } 2400 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2401 current, new) != current); 2402 2403 buf->dtb_offset = 0; 2404 buf->dtb_drops = 0; 2405 } 2406 2407 /* 2408 * Note: not called from probe context. This function is called 2409 * asynchronously from cross call context to clean any speculations that are 2410 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2411 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2412 * speculation. 2413 */ 2414 static void 2415 dtrace_speculation_clean_here(dtrace_state_t *state) 2416 { 2417 dtrace_icookie_t cookie; 2418 processorid_t cpu = CPU->cpu_id; 2419 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2420 dtrace_specid_t i; 2421 2422 cookie = dtrace_interrupt_disable(); 2423 2424 if (dest->dtb_tomax == NULL) { 2425 dtrace_interrupt_enable(cookie); 2426 return; 2427 } 2428 2429 for (i = 0; i < state->dts_nspeculations; i++) { 2430 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2431 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2432 2433 if (src->dtb_tomax == NULL) 2434 continue; 2435 2436 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2437 src->dtb_offset = 0; 2438 continue; 2439 } 2440 2441 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2442 continue; 2443 2444 if (src->dtb_offset == 0) 2445 continue; 2446 2447 dtrace_speculation_commit(state, cpu, i + 1); 2448 } 2449 2450 dtrace_interrupt_enable(cookie); 2451 } 2452 2453 /* 2454 * Note: not called from probe context. This function is called 2455 * asynchronously (and at a regular interval) to clean any speculations that 2456 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2457 * is work to be done, it cross calls all CPUs to perform that work; 2458 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2459 * INACTIVE state until they have been cleaned by all CPUs. 2460 */ 2461 static void 2462 dtrace_speculation_clean(dtrace_state_t *state) 2463 { 2464 int work = 0, rv; 2465 dtrace_specid_t i; 2466 2467 for (i = 0; i < state->dts_nspeculations; i++) { 2468 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2469 2470 ASSERT(!spec->dtsp_cleaning); 2471 2472 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2473 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2474 continue; 2475 2476 work++; 2477 spec->dtsp_cleaning = 1; 2478 } 2479 2480 if (!work) 2481 return; 2482 2483 dtrace_xcall(DTRACE_CPUALL, 2484 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2485 2486 /* 2487 * We now know that all CPUs have committed or discarded their 2488 * speculation buffers, as appropriate. We can now set the state 2489 * to inactive. 2490 */ 2491 for (i = 0; i < state->dts_nspeculations; i++) { 2492 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2493 dtrace_speculation_state_t current, new; 2494 2495 if (!spec->dtsp_cleaning) 2496 continue; 2497 2498 current = spec->dtsp_state; 2499 ASSERT(current == DTRACESPEC_DISCARDING || 2500 current == DTRACESPEC_COMMITTINGMANY); 2501 2502 new = DTRACESPEC_INACTIVE; 2503 2504 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2505 ASSERT(rv == current); 2506 spec->dtsp_cleaning = 0; 2507 } 2508 } 2509 2510 /* 2511 * Called as part of a speculate() to get the speculative buffer associated 2512 * with a given speculation. Returns NULL if the specified speculation is not 2513 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2514 * the active CPU is not the specified CPU -- the speculation will be 2515 * atomically transitioned into the ACTIVEMANY state. 2516 */ 2517 static dtrace_buffer_t * 2518 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2519 dtrace_specid_t which) 2520 { 2521 dtrace_speculation_t *spec; 2522 dtrace_speculation_state_t current, new; 2523 dtrace_buffer_t *buf; 2524 2525 if (which == 0) 2526 return (NULL); 2527 2528 if (which > state->dts_nspeculations) { 2529 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2530 return (NULL); 2531 } 2532 2533 spec = &state->dts_speculations[which - 1]; 2534 buf = &spec->dtsp_buffer[cpuid]; 2535 2536 do { 2537 current = spec->dtsp_state; 2538 2539 switch (current) { 2540 case DTRACESPEC_INACTIVE: 2541 case DTRACESPEC_COMMITTINGMANY: 2542 case DTRACESPEC_DISCARDING: 2543 return (NULL); 2544 2545 case DTRACESPEC_COMMITTING: 2546 ASSERT(buf->dtb_offset == 0); 2547 return (NULL); 2548 2549 case DTRACESPEC_ACTIVEONE: 2550 /* 2551 * This speculation is currently active on one CPU. 2552 * Check the offset in the buffer; if it's non-zero, 2553 * that CPU must be us (and we leave the state alone). 2554 * If it's zero, assume that we're starting on a new 2555 * CPU -- and change the state to indicate that the 2556 * speculation is active on more than one CPU. 2557 */ 2558 if (buf->dtb_offset != 0) 2559 return (buf); 2560 2561 new = DTRACESPEC_ACTIVEMANY; 2562 break; 2563 2564 case DTRACESPEC_ACTIVEMANY: 2565 return (buf); 2566 2567 case DTRACESPEC_ACTIVE: 2568 new = DTRACESPEC_ACTIVEONE; 2569 break; 2570 2571 default: 2572 ASSERT(0); 2573 } 2574 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2575 current, new) != current); 2576 2577 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2578 return (buf); 2579 } 2580 2581 /* 2582 * Return a string. In the event that the user lacks the privilege to access 2583 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2584 * don't fail access checking. 2585 * 2586 * dtrace_dif_variable() uses this routine as a helper for various 2587 * builtin values such as 'execname' and 'probefunc.' 2588 */ 2589 uintptr_t 2590 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 2591 dtrace_mstate_t *mstate) 2592 { 2593 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2594 uintptr_t ret; 2595 size_t strsz; 2596 2597 /* 2598 * The easy case: this probe is allowed to read all of memory, so 2599 * we can just return this as a vanilla pointer. 2600 */ 2601 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 2602 return (addr); 2603 2604 /* 2605 * This is the tougher case: we copy the string in question from 2606 * kernel memory into scratch memory and return it that way: this 2607 * ensures that we won't trip up when access checking tests the 2608 * BYREF return value. 2609 */ 2610 strsz = dtrace_strlen((char *)addr, size) + 1; 2611 2612 if (mstate->dtms_scratch_ptr + strsz > 2613 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2614 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2615 return (NULL); 2616 } 2617 2618 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2619 strsz); 2620 ret = mstate->dtms_scratch_ptr; 2621 mstate->dtms_scratch_ptr += strsz; 2622 return (ret); 2623 } 2624 2625 /* 2626 * This function implements the DIF emulator's variable lookups. The emulator 2627 * passes a reserved variable identifier and optional built-in array index. 2628 */ 2629 static uint64_t 2630 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2631 uint64_t ndx) 2632 { 2633 /* 2634 * If we're accessing one of the uncached arguments, we'll turn this 2635 * into a reference in the args array. 2636 */ 2637 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2638 ndx = v - DIF_VAR_ARG0; 2639 v = DIF_VAR_ARGS; 2640 } 2641 2642 switch (v) { 2643 case DIF_VAR_ARGS: 2644 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2645 if (ndx >= sizeof (mstate->dtms_arg) / 2646 sizeof (mstate->dtms_arg[0])) { 2647 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2648 dtrace_provider_t *pv; 2649 uint64_t val; 2650 2651 pv = mstate->dtms_probe->dtpr_provider; 2652 if (pv->dtpv_pops.dtps_getargval != NULL) 2653 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2654 mstate->dtms_probe->dtpr_id, 2655 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2656 else 2657 val = dtrace_getarg(ndx, aframes); 2658 2659 /* 2660 * This is regrettably required to keep the compiler 2661 * from tail-optimizing the call to dtrace_getarg(). 2662 * The condition always evaluates to true, but the 2663 * compiler has no way of figuring that out a priori. 2664 * (None of this would be necessary if the compiler 2665 * could be relied upon to _always_ tail-optimize 2666 * the call to dtrace_getarg() -- but it can't.) 2667 */ 2668 if (mstate->dtms_probe != NULL) 2669 return (val); 2670 2671 ASSERT(0); 2672 } 2673 2674 return (mstate->dtms_arg[ndx]); 2675 2676 case DIF_VAR_UREGS: { 2677 klwp_t *lwp; 2678 2679 if (!dtrace_priv_proc(state)) 2680 return (0); 2681 2682 if ((lwp = curthread->t_lwp) == NULL) { 2683 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2684 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL; 2685 return (0); 2686 } 2687 2688 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2689 } 2690 2691 case DIF_VAR_CURTHREAD: 2692 if (!dtrace_priv_kernel(state)) 2693 return (0); 2694 return ((uint64_t)(uintptr_t)curthread); 2695 2696 case DIF_VAR_TIMESTAMP: 2697 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2698 mstate->dtms_timestamp = dtrace_gethrtime(); 2699 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2700 } 2701 return (mstate->dtms_timestamp); 2702 2703 case DIF_VAR_VTIMESTAMP: 2704 ASSERT(dtrace_vtime_references != 0); 2705 return (curthread->t_dtrace_vtime); 2706 2707 case DIF_VAR_WALLTIMESTAMP: 2708 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2709 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2710 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2711 } 2712 return (mstate->dtms_walltimestamp); 2713 2714 case DIF_VAR_IPL: 2715 if (!dtrace_priv_kernel(state)) 2716 return (0); 2717 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2718 mstate->dtms_ipl = dtrace_getipl(); 2719 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2720 } 2721 return (mstate->dtms_ipl); 2722 2723 case DIF_VAR_EPID: 2724 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2725 return (mstate->dtms_epid); 2726 2727 case DIF_VAR_ID: 2728 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2729 return (mstate->dtms_probe->dtpr_id); 2730 2731 case DIF_VAR_STACKDEPTH: 2732 if (!dtrace_priv_kernel(state)) 2733 return (0); 2734 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2735 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2736 2737 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2738 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2739 } 2740 return (mstate->dtms_stackdepth); 2741 2742 case DIF_VAR_USTACKDEPTH: 2743 if (!dtrace_priv_proc(state)) 2744 return (0); 2745 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2746 /* 2747 * See comment in DIF_VAR_PID. 2748 */ 2749 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2750 CPU_ON_INTR(CPU)) { 2751 mstate->dtms_ustackdepth = 0; 2752 } else { 2753 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2754 mstate->dtms_ustackdepth = 2755 dtrace_getustackdepth(); 2756 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2757 } 2758 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2759 } 2760 return (mstate->dtms_ustackdepth); 2761 2762 case DIF_VAR_CALLER: 2763 if (!dtrace_priv_kernel(state)) 2764 return (0); 2765 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2766 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2767 2768 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2769 /* 2770 * If this is an unanchored probe, we are 2771 * required to go through the slow path: 2772 * dtrace_caller() only guarantees correct 2773 * results for anchored probes. 2774 */ 2775 pc_t caller[2]; 2776 2777 dtrace_getpcstack(caller, 2, aframes, 2778 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2779 mstate->dtms_caller = caller[1]; 2780 } else if ((mstate->dtms_caller = 2781 dtrace_caller(aframes)) == -1) { 2782 /* 2783 * We have failed to do this the quick way; 2784 * we must resort to the slower approach of 2785 * calling dtrace_getpcstack(). 2786 */ 2787 pc_t caller; 2788 2789 dtrace_getpcstack(&caller, 1, aframes, NULL); 2790 mstate->dtms_caller = caller; 2791 } 2792 2793 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 2794 } 2795 return (mstate->dtms_caller); 2796 2797 case DIF_VAR_UCALLER: 2798 if (!dtrace_priv_proc(state)) 2799 return (0); 2800 2801 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 2802 uint64_t ustack[3]; 2803 2804 /* 2805 * dtrace_getupcstack() fills in the first uint64_t 2806 * with the current PID. The second uint64_t will 2807 * be the program counter at user-level. The third 2808 * uint64_t will contain the caller, which is what 2809 * we're after. 2810 */ 2811 ustack[2] = NULL; 2812 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2813 dtrace_getupcstack(ustack, 3); 2814 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2815 mstate->dtms_ucaller = ustack[2]; 2816 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 2817 } 2818 2819 return (mstate->dtms_ucaller); 2820 2821 case DIF_VAR_PROBEPROV: 2822 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2823 return (dtrace_dif_varstr( 2824 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 2825 state, mstate)); 2826 2827 case DIF_VAR_PROBEMOD: 2828 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2829 return (dtrace_dif_varstr( 2830 (uintptr_t)mstate->dtms_probe->dtpr_mod, 2831 state, mstate)); 2832 2833 case DIF_VAR_PROBEFUNC: 2834 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2835 return (dtrace_dif_varstr( 2836 (uintptr_t)mstate->dtms_probe->dtpr_func, 2837 state, mstate)); 2838 2839 case DIF_VAR_PROBENAME: 2840 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2841 return (dtrace_dif_varstr( 2842 (uintptr_t)mstate->dtms_probe->dtpr_name, 2843 state, mstate)); 2844 2845 case DIF_VAR_PID: 2846 if (!dtrace_priv_proc(state)) 2847 return (0); 2848 2849 /* 2850 * Note that we are assuming that an unanchored probe is 2851 * always due to a high-level interrupt. (And we're assuming 2852 * that there is only a single high level interrupt.) 2853 */ 2854 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2855 return (pid0.pid_id); 2856 2857 /* 2858 * It is always safe to dereference one's own t_procp pointer: 2859 * it always points to a valid, allocated proc structure. 2860 * Further, it is always safe to dereference the p_pidp member 2861 * of one's own proc structure. (These are truisms becuase 2862 * threads and processes don't clean up their own state -- 2863 * they leave that task to whomever reaps them.) 2864 */ 2865 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 2866 2867 case DIF_VAR_PPID: 2868 if (!dtrace_priv_proc(state)) 2869 return (0); 2870 2871 /* 2872 * See comment in DIF_VAR_PID. 2873 */ 2874 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2875 return (pid0.pid_id); 2876 2877 /* 2878 * It is always safe to dereference one's own t_procp pointer: 2879 * it always points to a valid, allocated proc structure. 2880 * (This is true because threads don't clean up their own 2881 * state -- they leave that task to whomever reaps them.) 2882 */ 2883 return ((uint64_t)curthread->t_procp->p_ppid); 2884 2885 case DIF_VAR_TID: 2886 /* 2887 * See comment in DIF_VAR_PID. 2888 */ 2889 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2890 return (0); 2891 2892 return ((uint64_t)curthread->t_tid); 2893 2894 case DIF_VAR_EXECNAME: 2895 if (!dtrace_priv_proc(state)) 2896 return (0); 2897 2898 /* 2899 * See comment in DIF_VAR_PID. 2900 */ 2901 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2902 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 2903 2904 /* 2905 * It is always safe to dereference one's own t_procp pointer: 2906 * it always points to a valid, allocated proc structure. 2907 * (This is true because threads don't clean up their own 2908 * state -- they leave that task to whomever reaps them.) 2909 */ 2910 return (dtrace_dif_varstr( 2911 (uintptr_t)curthread->t_procp->p_user.u_comm, 2912 state, mstate)); 2913 2914 case DIF_VAR_ZONENAME: 2915 if (!dtrace_priv_proc(state)) 2916 return (0); 2917 2918 /* 2919 * See comment in DIF_VAR_PID. 2920 */ 2921 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2922 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 2923 2924 /* 2925 * It is always safe to dereference one's own t_procp pointer: 2926 * it always points to a valid, allocated proc structure. 2927 * (This is true because threads don't clean up their own 2928 * state -- they leave that task to whomever reaps them.) 2929 */ 2930 return (dtrace_dif_varstr( 2931 (uintptr_t)curthread->t_procp->p_zone->zone_name, 2932 state, mstate)); 2933 2934 case DIF_VAR_UID: 2935 if (!dtrace_priv_proc(state)) 2936 return (0); 2937 2938 /* 2939 * See comment in DIF_VAR_PID. 2940 */ 2941 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2942 return ((uint64_t)p0.p_cred->cr_uid); 2943 2944 /* 2945 * It is always safe to dereference one's own t_procp pointer: 2946 * it always points to a valid, allocated proc structure. 2947 * (This is true because threads don't clean up their own 2948 * state -- they leave that task to whomever reaps them.) 2949 * 2950 * Additionally, it is safe to dereference one's own process 2951 * credential, since this is never NULL after process birth. 2952 */ 2953 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 2954 2955 case DIF_VAR_GID: 2956 if (!dtrace_priv_proc(state)) 2957 return (0); 2958 2959 /* 2960 * See comment in DIF_VAR_PID. 2961 */ 2962 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2963 return ((uint64_t)p0.p_cred->cr_gid); 2964 2965 /* 2966 * It is always safe to dereference one's own t_procp pointer: 2967 * it always points to a valid, allocated proc structure. 2968 * (This is true because threads don't clean up their own 2969 * state -- they leave that task to whomever reaps them.) 2970 * 2971 * Additionally, it is safe to dereference one's own process 2972 * credential, since this is never NULL after process birth. 2973 */ 2974 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 2975 2976 case DIF_VAR_ERRNO: { 2977 klwp_t *lwp; 2978 if (!dtrace_priv_proc(state)) 2979 return (0); 2980 2981 /* 2982 * See comment in DIF_VAR_PID. 2983 */ 2984 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2985 return (0); 2986 2987 /* 2988 * It is always safe to dereference one's own t_lwp pointer in 2989 * the event that this pointer is non-NULL. (This is true 2990 * because threads and lwps don't clean up their own state -- 2991 * they leave that task to whomever reaps them.) 2992 */ 2993 if ((lwp = curthread->t_lwp) == NULL) 2994 return (0); 2995 2996 return ((uint64_t)lwp->lwp_errno); 2997 } 2998 default: 2999 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3000 return (0); 3001 } 3002 } 3003 3004 /* 3005 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 3006 * Notice that we don't bother validating the proper number of arguments or 3007 * their types in the tuple stack. This isn't needed because all argument 3008 * interpretation is safe because of our load safety -- the worst that can 3009 * happen is that a bogus program can obtain bogus results. 3010 */ 3011 static void 3012 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 3013 dtrace_key_t *tupregs, int nargs, 3014 dtrace_mstate_t *mstate, dtrace_state_t *state) 3015 { 3016 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 3017 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 3018 dtrace_vstate_t *vstate = &state->dts_vstate; 3019 3020 union { 3021 mutex_impl_t mi; 3022 uint64_t mx; 3023 } m; 3024 3025 union { 3026 krwlock_t ri; 3027 uintptr_t rw; 3028 } r; 3029 3030 switch (subr) { 3031 case DIF_SUBR_RAND: 3032 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 3033 break; 3034 3035 case DIF_SUBR_MUTEX_OWNED: 3036 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3037 mstate, vstate)) { 3038 regs[rd] = NULL; 3039 break; 3040 } 3041 3042 m.mx = dtrace_load64(tupregs[0].dttk_value); 3043 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3044 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3045 else 3046 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 3047 break; 3048 3049 case DIF_SUBR_MUTEX_OWNER: 3050 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3051 mstate, vstate)) { 3052 regs[rd] = NULL; 3053 break; 3054 } 3055 3056 m.mx = dtrace_load64(tupregs[0].dttk_value); 3057 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3058 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3059 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3060 else 3061 regs[rd] = 0; 3062 break; 3063 3064 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3065 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3066 mstate, vstate)) { 3067 regs[rd] = NULL; 3068 break; 3069 } 3070 3071 m.mx = dtrace_load64(tupregs[0].dttk_value); 3072 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3073 break; 3074 3075 case DIF_SUBR_MUTEX_TYPE_SPIN: 3076 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3077 mstate, vstate)) { 3078 regs[rd] = NULL; 3079 break; 3080 } 3081 3082 m.mx = dtrace_load64(tupregs[0].dttk_value); 3083 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3084 break; 3085 3086 case DIF_SUBR_RW_READ_HELD: { 3087 uintptr_t tmp; 3088 3089 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3090 mstate, vstate)) { 3091 regs[rd] = NULL; 3092 break; 3093 } 3094 3095 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3096 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 3097 break; 3098 } 3099 3100 case DIF_SUBR_RW_WRITE_HELD: 3101 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3102 mstate, vstate)) { 3103 regs[rd] = NULL; 3104 break; 3105 } 3106 3107 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3108 regs[rd] = _RW_WRITE_HELD(&r.ri); 3109 break; 3110 3111 case DIF_SUBR_RW_ISWRITER: 3112 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3113 mstate, vstate)) { 3114 regs[rd] = NULL; 3115 break; 3116 } 3117 3118 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3119 regs[rd] = _RW_ISWRITER(&r.ri); 3120 break; 3121 3122 case DIF_SUBR_BCOPY: { 3123 /* 3124 * We need to be sure that the destination is in the scratch 3125 * region -- no other region is allowed. 3126 */ 3127 uintptr_t src = tupregs[0].dttk_value; 3128 uintptr_t dest = tupregs[1].dttk_value; 3129 size_t size = tupregs[2].dttk_value; 3130 3131 if (!dtrace_inscratch(dest, size, mstate)) { 3132 *flags |= CPU_DTRACE_BADADDR; 3133 *illval = regs[rd]; 3134 break; 3135 } 3136 3137 if (!dtrace_canload(src, size, mstate, vstate)) { 3138 regs[rd] = NULL; 3139 break; 3140 } 3141 3142 dtrace_bcopy((void *)src, (void *)dest, size); 3143 break; 3144 } 3145 3146 case DIF_SUBR_ALLOCA: 3147 case DIF_SUBR_COPYIN: { 3148 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 3149 uint64_t size = 3150 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 3151 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 3152 3153 /* 3154 * This action doesn't require any credential checks since 3155 * probes will not activate in user contexts to which the 3156 * enabling user does not have permissions. 3157 */ 3158 3159 /* 3160 * Rounding up the user allocation size could have overflowed 3161 * a large, bogus allocation (like -1ULL) to 0. 3162 */ 3163 if (scratch_size < size || 3164 !DTRACE_INSCRATCH(mstate, scratch_size)) { 3165 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3166 regs[rd] = NULL; 3167 break; 3168 } 3169 3170 if (subr == DIF_SUBR_COPYIN) { 3171 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3172 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3173 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3174 } 3175 3176 mstate->dtms_scratch_ptr += scratch_size; 3177 regs[rd] = dest; 3178 break; 3179 } 3180 3181 case DIF_SUBR_COPYINTO: { 3182 uint64_t size = tupregs[1].dttk_value; 3183 uintptr_t dest = tupregs[2].dttk_value; 3184 3185 /* 3186 * This action doesn't require any credential checks since 3187 * probes will not activate in user contexts to which the 3188 * enabling user does not have permissions. 3189 */ 3190 if (!dtrace_inscratch(dest, size, mstate)) { 3191 *flags |= CPU_DTRACE_BADADDR; 3192 *illval = regs[rd]; 3193 break; 3194 } 3195 3196 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3197 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3198 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3199 break; 3200 } 3201 3202 case DIF_SUBR_COPYINSTR: { 3203 uintptr_t dest = mstate->dtms_scratch_ptr; 3204 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3205 3206 if (nargs > 1 && tupregs[1].dttk_value < size) 3207 size = tupregs[1].dttk_value + 1; 3208 3209 /* 3210 * This action doesn't require any credential checks since 3211 * probes will not activate in user contexts to which the 3212 * enabling user does not have permissions. 3213 */ 3214 if (!DTRACE_INSCRATCH(mstate, size)) { 3215 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3216 regs[rd] = NULL; 3217 break; 3218 } 3219 3220 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3221 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 3222 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3223 3224 ((char *)dest)[size - 1] = '\0'; 3225 mstate->dtms_scratch_ptr += size; 3226 regs[rd] = dest; 3227 break; 3228 } 3229 3230 case DIF_SUBR_MSGSIZE: 3231 case DIF_SUBR_MSGDSIZE: { 3232 uintptr_t baddr = tupregs[0].dttk_value, daddr; 3233 uintptr_t wptr, rptr; 3234 size_t count = 0; 3235 int cont = 0; 3236 3237 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3238 3239 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 3240 vstate)) { 3241 regs[rd] = NULL; 3242 break; 3243 } 3244 3245 wptr = dtrace_loadptr(baddr + 3246 offsetof(mblk_t, b_wptr)); 3247 3248 rptr = dtrace_loadptr(baddr + 3249 offsetof(mblk_t, b_rptr)); 3250 3251 if (wptr < rptr) { 3252 *flags |= CPU_DTRACE_BADADDR; 3253 *illval = tupregs[0].dttk_value; 3254 break; 3255 } 3256 3257 daddr = dtrace_loadptr(baddr + 3258 offsetof(mblk_t, b_datap)); 3259 3260 baddr = dtrace_loadptr(baddr + 3261 offsetof(mblk_t, b_cont)); 3262 3263 /* 3264 * We want to prevent against denial-of-service here, 3265 * so we're only going to search the list for 3266 * dtrace_msgdsize_max mblks. 3267 */ 3268 if (cont++ > dtrace_msgdsize_max) { 3269 *flags |= CPU_DTRACE_ILLOP; 3270 break; 3271 } 3272 3273 if (subr == DIF_SUBR_MSGDSIZE) { 3274 if (dtrace_load8(daddr + 3275 offsetof(dblk_t, db_type)) != M_DATA) 3276 continue; 3277 } 3278 3279 count += wptr - rptr; 3280 } 3281 3282 if (!(*flags & CPU_DTRACE_FAULT)) 3283 regs[rd] = count; 3284 3285 break; 3286 } 3287 3288 case DIF_SUBR_PROGENYOF: { 3289 pid_t pid = tupregs[0].dttk_value; 3290 proc_t *p; 3291 int rval = 0; 3292 3293 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3294 3295 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 3296 if (p->p_pidp->pid_id == pid) { 3297 rval = 1; 3298 break; 3299 } 3300 } 3301 3302 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3303 3304 regs[rd] = rval; 3305 break; 3306 } 3307 3308 case DIF_SUBR_SPECULATION: 3309 regs[rd] = dtrace_speculation(state); 3310 break; 3311 3312 case DIF_SUBR_COPYOUT: { 3313 uintptr_t kaddr = tupregs[0].dttk_value; 3314 uintptr_t uaddr = tupregs[1].dttk_value; 3315 uint64_t size = tupregs[2].dttk_value; 3316 3317 if (!dtrace_destructive_disallow && 3318 dtrace_priv_proc_control(state) && 3319 !dtrace_istoxic(kaddr, size)) { 3320 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3321 dtrace_copyout(kaddr, uaddr, size, flags); 3322 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3323 } 3324 break; 3325 } 3326 3327 case DIF_SUBR_COPYOUTSTR: { 3328 uintptr_t kaddr = tupregs[0].dttk_value; 3329 uintptr_t uaddr = tupregs[1].dttk_value; 3330 uint64_t size = tupregs[2].dttk_value; 3331 3332 if (!dtrace_destructive_disallow && 3333 dtrace_priv_proc_control(state) && 3334 !dtrace_istoxic(kaddr, size)) { 3335 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3336 dtrace_copyoutstr(kaddr, uaddr, size, flags); 3337 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3338 } 3339 break; 3340 } 3341 3342 case DIF_SUBR_STRLEN: { 3343 size_t sz; 3344 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 3345 sz = dtrace_strlen((char *)addr, 3346 state->dts_options[DTRACEOPT_STRSIZE]); 3347 3348 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 3349 regs[rd] = NULL; 3350 break; 3351 } 3352 3353 regs[rd] = sz; 3354 3355 break; 3356 } 3357 3358 case DIF_SUBR_STRCHR: 3359 case DIF_SUBR_STRRCHR: { 3360 /* 3361 * We're going to iterate over the string looking for the 3362 * specified character. We will iterate until we have reached 3363 * the string length or we have found the character. If this 3364 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 3365 * of the specified character instead of the first. 3366 */ 3367 uintptr_t saddr = tupregs[0].dttk_value; 3368 uintptr_t addr = tupregs[0].dttk_value; 3369 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 3370 char c, target = (char)tupregs[1].dttk_value; 3371 3372 for (regs[rd] = NULL; addr < limit; addr++) { 3373 if ((c = dtrace_load8(addr)) == target) { 3374 regs[rd] = addr; 3375 3376 if (subr == DIF_SUBR_STRCHR) 3377 break; 3378 } 3379 3380 if (c == '\0') 3381 break; 3382 } 3383 3384 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 3385 regs[rd] = NULL; 3386 break; 3387 } 3388 3389 break; 3390 } 3391 3392 case DIF_SUBR_STRSTR: 3393 case DIF_SUBR_INDEX: 3394 case DIF_SUBR_RINDEX: { 3395 /* 3396 * We're going to iterate over the string looking for the 3397 * specified string. We will iterate until we have reached 3398 * the string length or we have found the string. (Yes, this 3399 * is done in the most naive way possible -- but considering 3400 * that the string we're searching for is likely to be 3401 * relatively short, the complexity of Rabin-Karp or similar 3402 * hardly seems merited.) 3403 */ 3404 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 3405 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 3406 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3407 size_t len = dtrace_strlen(addr, size); 3408 size_t sublen = dtrace_strlen(substr, size); 3409 char *limit = addr + len, *orig = addr; 3410 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 3411 int inc = 1; 3412 3413 regs[rd] = notfound; 3414 3415 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 3416 regs[rd] = NULL; 3417 break; 3418 } 3419 3420 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 3421 vstate)) { 3422 regs[rd] = NULL; 3423 break; 3424 } 3425 3426 /* 3427 * strstr() and index()/rindex() have similar semantics if 3428 * both strings are the empty string: strstr() returns a 3429 * pointer to the (empty) string, and index() and rindex() 3430 * both return index 0 (regardless of any position argument). 3431 */ 3432 if (sublen == 0 && len == 0) { 3433 if (subr == DIF_SUBR_STRSTR) 3434 regs[rd] = (uintptr_t)addr; 3435 else 3436 regs[rd] = 0; 3437 break; 3438 } 3439 3440 if (subr != DIF_SUBR_STRSTR) { 3441 if (subr == DIF_SUBR_RINDEX) { 3442 limit = orig - 1; 3443 addr += len; 3444 inc = -1; 3445 } 3446 3447 /* 3448 * Both index() and rindex() take an optional position 3449 * argument that denotes the starting position. 3450 */ 3451 if (nargs == 3) { 3452 int64_t pos = (int64_t)tupregs[2].dttk_value; 3453 3454 /* 3455 * If the position argument to index() is 3456 * negative, Perl implicitly clamps it at 3457 * zero. This semantic is a little surprising 3458 * given the special meaning of negative 3459 * positions to similar Perl functions like 3460 * substr(), but it appears to reflect a 3461 * notion that index() can start from a 3462 * negative index and increment its way up to 3463 * the string. Given this notion, Perl's 3464 * rindex() is at least self-consistent in 3465 * that it implicitly clamps positions greater 3466 * than the string length to be the string 3467 * length. Where Perl completely loses 3468 * coherence, however, is when the specified 3469 * substring is the empty string (""). In 3470 * this case, even if the position is 3471 * negative, rindex() returns 0 -- and even if 3472 * the position is greater than the length, 3473 * index() returns the string length. These 3474 * semantics violate the notion that index() 3475 * should never return a value less than the 3476 * specified position and that rindex() should 3477 * never return a value greater than the 3478 * specified position. (One assumes that 3479 * these semantics are artifacts of Perl's 3480 * implementation and not the results of 3481 * deliberate design -- it beggars belief that 3482 * even Larry Wall could desire such oddness.) 3483 * While in the abstract one would wish for 3484 * consistent position semantics across 3485 * substr(), index() and rindex() -- or at the 3486 * very least self-consistent position 3487 * semantics for index() and rindex() -- we 3488 * instead opt to keep with the extant Perl 3489 * semantics, in all their broken glory. (Do 3490 * we have more desire to maintain Perl's 3491 * semantics than Perl does? Probably.) 3492 */ 3493 if (subr == DIF_SUBR_RINDEX) { 3494 if (pos < 0) { 3495 if (sublen == 0) 3496 regs[rd] = 0; 3497 break; 3498 } 3499 3500 if (pos > len) 3501 pos = len; 3502 } else { 3503 if (pos < 0) 3504 pos = 0; 3505 3506 if (pos >= len) { 3507 if (sublen == 0) 3508 regs[rd] = len; 3509 break; 3510 } 3511 } 3512 3513 addr = orig + pos; 3514 } 3515 } 3516 3517 for (regs[rd] = notfound; addr != limit; addr += inc) { 3518 if (dtrace_strncmp(addr, substr, sublen) == 0) { 3519 if (subr != DIF_SUBR_STRSTR) { 3520 /* 3521 * As D index() and rindex() are 3522 * modeled on Perl (and not on awk), 3523 * we return a zero-based (and not a 3524 * one-based) index. (For you Perl 3525 * weenies: no, we're not going to add 3526 * $[ -- and shouldn't you be at a con 3527 * or something?) 3528 */ 3529 regs[rd] = (uintptr_t)(addr - orig); 3530 break; 3531 } 3532 3533 ASSERT(subr == DIF_SUBR_STRSTR); 3534 regs[rd] = (uintptr_t)addr; 3535 break; 3536 } 3537 } 3538 3539 break; 3540 } 3541 3542 case DIF_SUBR_STRTOK: { 3543 uintptr_t addr = tupregs[0].dttk_value; 3544 uintptr_t tokaddr = tupregs[1].dttk_value; 3545 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3546 uintptr_t limit, toklimit = tokaddr + size; 3547 uint8_t c, tokmap[32]; /* 256 / 8 */ 3548 char *dest = (char *)mstate->dtms_scratch_ptr; 3549 int i; 3550 3551 /* 3552 * Check both the token buffer and (later) the input buffer, 3553 * since both could be non-scratch addresses. 3554 */ 3555 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 3556 regs[rd] = NULL; 3557 break; 3558 } 3559 3560 if (!DTRACE_INSCRATCH(mstate, size)) { 3561 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3562 regs[rd] = NULL; 3563 break; 3564 } 3565 3566 if (addr == NULL) { 3567 /* 3568 * If the address specified is NULL, we use our saved 3569 * strtok pointer from the mstate. Note that this 3570 * means that the saved strtok pointer is _only_ 3571 * valid within multiple enablings of the same probe -- 3572 * it behaves like an implicit clause-local variable. 3573 */ 3574 addr = mstate->dtms_strtok; 3575 } else { 3576 /* 3577 * If the user-specified address is non-NULL we must 3578 * access check it. This is the only time we have 3579 * a chance to do so, since this address may reside 3580 * in the string table of this clause-- future calls 3581 * (when we fetch addr from mstate->dtms_strtok) 3582 * would fail this access check. 3583 */ 3584 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 3585 regs[rd] = NULL; 3586 break; 3587 } 3588 } 3589 3590 /* 3591 * First, zero the token map, and then process the token 3592 * string -- setting a bit in the map for every character 3593 * found in the token string. 3594 */ 3595 for (i = 0; i < sizeof (tokmap); i++) 3596 tokmap[i] = 0; 3597 3598 for (; tokaddr < toklimit; tokaddr++) { 3599 if ((c = dtrace_load8(tokaddr)) == '\0') 3600 break; 3601 3602 ASSERT((c >> 3) < sizeof (tokmap)); 3603 tokmap[c >> 3] |= (1 << (c & 0x7)); 3604 } 3605 3606 for (limit = addr + size; addr < limit; addr++) { 3607 /* 3608 * We're looking for a character that is _not_ contained 3609 * in the token string. 3610 */ 3611 if ((c = dtrace_load8(addr)) == '\0') 3612 break; 3613 3614 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3615 break; 3616 } 3617 3618 if (c == '\0') { 3619 /* 3620 * We reached the end of the string without finding 3621 * any character that was not in the token string. 3622 * We return NULL in this case, and we set the saved 3623 * address to NULL as well. 3624 */ 3625 regs[rd] = NULL; 3626 mstate->dtms_strtok = NULL; 3627 break; 3628 } 3629 3630 /* 3631 * From here on, we're copying into the destination string. 3632 */ 3633 for (i = 0; addr < limit && i < size - 1; addr++) { 3634 if ((c = dtrace_load8(addr)) == '\0') 3635 break; 3636 3637 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3638 break; 3639 3640 ASSERT(i < size); 3641 dest[i++] = c; 3642 } 3643 3644 ASSERT(i < size); 3645 dest[i] = '\0'; 3646 regs[rd] = (uintptr_t)dest; 3647 mstate->dtms_scratch_ptr += size; 3648 mstate->dtms_strtok = addr; 3649 break; 3650 } 3651 3652 case DIF_SUBR_SUBSTR: { 3653 uintptr_t s = tupregs[0].dttk_value; 3654 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3655 char *d = (char *)mstate->dtms_scratch_ptr; 3656 int64_t index = (int64_t)tupregs[1].dttk_value; 3657 int64_t remaining = (int64_t)tupregs[2].dttk_value; 3658 size_t len = dtrace_strlen((char *)s, size); 3659 int64_t i; 3660 3661 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 3662 regs[rd] = NULL; 3663 break; 3664 } 3665 3666 if (!DTRACE_INSCRATCH(mstate, size)) { 3667 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3668 regs[rd] = NULL; 3669 break; 3670 } 3671 3672 if (nargs <= 2) 3673 remaining = (int64_t)size; 3674 3675 if (index < 0) { 3676 index += len; 3677 3678 if (index < 0 && index + remaining > 0) { 3679 remaining += index; 3680 index = 0; 3681 } 3682 } 3683 3684 if (index >= len || index < 0) { 3685 remaining = 0; 3686 } else if (remaining < 0) { 3687 remaining += len - index; 3688 } else if (index + remaining > size) { 3689 remaining = size - index; 3690 } 3691 3692 for (i = 0; i < remaining; i++) { 3693 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 3694 break; 3695 } 3696 3697 d[i] = '\0'; 3698 3699 mstate->dtms_scratch_ptr += size; 3700 regs[rd] = (uintptr_t)d; 3701 break; 3702 } 3703 3704 case DIF_SUBR_GETMAJOR: 3705 #ifdef _LP64 3706 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 3707 #else 3708 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 3709 #endif 3710 break; 3711 3712 case DIF_SUBR_GETMINOR: 3713 #ifdef _LP64 3714 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 3715 #else 3716 regs[rd] = tupregs[0].dttk_value & MAXMIN; 3717 #endif 3718 break; 3719 3720 case DIF_SUBR_DDI_PATHNAME: { 3721 /* 3722 * This one is a galactic mess. We are going to roughly 3723 * emulate ddi_pathname(), but it's made more complicated 3724 * by the fact that we (a) want to include the minor name and 3725 * (b) must proceed iteratively instead of recursively. 3726 */ 3727 uintptr_t dest = mstate->dtms_scratch_ptr; 3728 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3729 char *start = (char *)dest, *end = start + size - 1; 3730 uintptr_t daddr = tupregs[0].dttk_value; 3731 int64_t minor = (int64_t)tupregs[1].dttk_value; 3732 char *s; 3733 int i, len, depth = 0; 3734 3735 /* 3736 * Due to all the pointer jumping we do and context we must 3737 * rely upon, we just mandate that the user must have kernel 3738 * read privileges to use this routine. 3739 */ 3740 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 3741 *flags |= CPU_DTRACE_KPRIV; 3742 *illval = daddr; 3743 regs[rd] = NULL; 3744 } 3745 3746 if (!DTRACE_INSCRATCH(mstate, size)) { 3747 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3748 regs[rd] = NULL; 3749 break; 3750 } 3751 3752 *end = '\0'; 3753 3754 /* 3755 * We want to have a name for the minor. In order to do this, 3756 * we need to walk the minor list from the devinfo. We want 3757 * to be sure that we don't infinitely walk a circular list, 3758 * so we check for circularity by sending a scout pointer 3759 * ahead two elements for every element that we iterate over; 3760 * if the list is circular, these will ultimately point to the 3761 * same element. You may recognize this little trick as the 3762 * answer to a stupid interview question -- one that always 3763 * seems to be asked by those who had to have it laboriously 3764 * explained to them, and who can't even concisely describe 3765 * the conditions under which one would be forced to resort to 3766 * this technique. Needless to say, those conditions are 3767 * found here -- and probably only here. Is this the only use 3768 * of this infamous trick in shipping, production code? If it 3769 * isn't, it probably should be... 3770 */ 3771 if (minor != -1) { 3772 uintptr_t maddr = dtrace_loadptr(daddr + 3773 offsetof(struct dev_info, devi_minor)); 3774 3775 uintptr_t next = offsetof(struct ddi_minor_data, next); 3776 uintptr_t name = offsetof(struct ddi_minor_data, 3777 d_minor) + offsetof(struct ddi_minor, name); 3778 uintptr_t dev = offsetof(struct ddi_minor_data, 3779 d_minor) + offsetof(struct ddi_minor, dev); 3780 uintptr_t scout; 3781 3782 if (maddr != NULL) 3783 scout = dtrace_loadptr(maddr + next); 3784 3785 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3786 uint64_t m; 3787 #ifdef _LP64 3788 m = dtrace_load64(maddr + dev) & MAXMIN64; 3789 #else 3790 m = dtrace_load32(maddr + dev) & MAXMIN; 3791 #endif 3792 if (m != minor) { 3793 maddr = dtrace_loadptr(maddr + next); 3794 3795 if (scout == NULL) 3796 continue; 3797 3798 scout = dtrace_loadptr(scout + next); 3799 3800 if (scout == NULL) 3801 continue; 3802 3803 scout = dtrace_loadptr(scout + next); 3804 3805 if (scout == NULL) 3806 continue; 3807 3808 if (scout == maddr) { 3809 *flags |= CPU_DTRACE_ILLOP; 3810 break; 3811 } 3812 3813 continue; 3814 } 3815 3816 /* 3817 * We have the minor data. Now we need to 3818 * copy the minor's name into the end of the 3819 * pathname. 3820 */ 3821 s = (char *)dtrace_loadptr(maddr + name); 3822 len = dtrace_strlen(s, size); 3823 3824 if (*flags & CPU_DTRACE_FAULT) 3825 break; 3826 3827 if (len != 0) { 3828 if ((end -= (len + 1)) < start) 3829 break; 3830 3831 *end = ':'; 3832 } 3833 3834 for (i = 1; i <= len; i++) 3835 end[i] = dtrace_load8((uintptr_t)s++); 3836 break; 3837 } 3838 } 3839 3840 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3841 ddi_node_state_t devi_state; 3842 3843 devi_state = dtrace_load32(daddr + 3844 offsetof(struct dev_info, devi_node_state)); 3845 3846 if (*flags & CPU_DTRACE_FAULT) 3847 break; 3848 3849 if (devi_state >= DS_INITIALIZED) { 3850 s = (char *)dtrace_loadptr(daddr + 3851 offsetof(struct dev_info, devi_addr)); 3852 len = dtrace_strlen(s, size); 3853 3854 if (*flags & CPU_DTRACE_FAULT) 3855 break; 3856 3857 if (len != 0) { 3858 if ((end -= (len + 1)) < start) 3859 break; 3860 3861 *end = '@'; 3862 } 3863 3864 for (i = 1; i <= len; i++) 3865 end[i] = dtrace_load8((uintptr_t)s++); 3866 } 3867 3868 /* 3869 * Now for the node name... 3870 */ 3871 s = (char *)dtrace_loadptr(daddr + 3872 offsetof(struct dev_info, devi_node_name)); 3873 3874 daddr = dtrace_loadptr(daddr + 3875 offsetof(struct dev_info, devi_parent)); 3876 3877 /* 3878 * If our parent is NULL (that is, if we're the root 3879 * node), we're going to use the special path 3880 * "devices". 3881 */ 3882 if (daddr == NULL) 3883 s = "devices"; 3884 3885 len = dtrace_strlen(s, size); 3886 if (*flags & CPU_DTRACE_FAULT) 3887 break; 3888 3889 if ((end -= (len + 1)) < start) 3890 break; 3891 3892 for (i = 1; i <= len; i++) 3893 end[i] = dtrace_load8((uintptr_t)s++); 3894 *end = '/'; 3895 3896 if (depth++ > dtrace_devdepth_max) { 3897 *flags |= CPU_DTRACE_ILLOP; 3898 break; 3899 } 3900 } 3901 3902 if (end < start) 3903 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3904 3905 if (daddr == NULL) { 3906 regs[rd] = (uintptr_t)end; 3907 mstate->dtms_scratch_ptr += size; 3908 } 3909 3910 break; 3911 } 3912 3913 case DIF_SUBR_STRJOIN: { 3914 char *d = (char *)mstate->dtms_scratch_ptr; 3915 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3916 uintptr_t s1 = tupregs[0].dttk_value; 3917 uintptr_t s2 = tupregs[1].dttk_value; 3918 int i = 0; 3919 3920 if (!dtrace_strcanload(s1, size, mstate, vstate) || 3921 !dtrace_strcanload(s2, size, mstate, vstate)) { 3922 regs[rd] = NULL; 3923 break; 3924 } 3925 3926 if (!DTRACE_INSCRATCH(mstate, size)) { 3927 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3928 regs[rd] = NULL; 3929 break; 3930 } 3931 3932 for (;;) { 3933 if (i >= size) { 3934 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3935 regs[rd] = NULL; 3936 break; 3937 } 3938 3939 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 3940 i--; 3941 break; 3942 } 3943 } 3944 3945 for (;;) { 3946 if (i >= size) { 3947 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3948 regs[rd] = NULL; 3949 break; 3950 } 3951 3952 if ((d[i++] = dtrace_load8(s2++)) == '\0') 3953 break; 3954 } 3955 3956 if (i < size) { 3957 mstate->dtms_scratch_ptr += i; 3958 regs[rd] = (uintptr_t)d; 3959 } 3960 3961 break; 3962 } 3963 3964 case DIF_SUBR_LLTOSTR: { 3965 int64_t i = (int64_t)tupregs[0].dttk_value; 3966 int64_t val = i < 0 ? i * -1 : i; 3967 uint64_t size = 22; /* enough room for 2^64 in decimal */ 3968 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 3969 3970 if (!DTRACE_INSCRATCH(mstate, size)) { 3971 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3972 regs[rd] = NULL; 3973 break; 3974 } 3975 3976 for (*end-- = '\0'; val; val /= 10) 3977 *end-- = '0' + (val % 10); 3978 3979 if (i == 0) 3980 *end-- = '0'; 3981 3982 if (i < 0) 3983 *end-- = '-'; 3984 3985 regs[rd] = (uintptr_t)end + 1; 3986 mstate->dtms_scratch_ptr += size; 3987 break; 3988 } 3989 3990 case DIF_SUBR_HTONS: 3991 case DIF_SUBR_NTOHS: 3992 #ifdef _BIG_ENDIAN 3993 regs[rd] = (uint16_t)tupregs[0].dttk_value; 3994 #else 3995 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 3996 #endif 3997 break; 3998 3999 4000 case DIF_SUBR_HTONL: 4001 case DIF_SUBR_NTOHL: 4002 #ifdef _BIG_ENDIAN 4003 regs[rd] = (uint32_t)tupregs[0].dttk_value; 4004 #else 4005 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 4006 #endif 4007 break; 4008 4009 4010 case DIF_SUBR_HTONLL: 4011 case DIF_SUBR_NTOHLL: 4012 #ifdef _BIG_ENDIAN 4013 regs[rd] = (uint64_t)tupregs[0].dttk_value; 4014 #else 4015 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 4016 #endif 4017 break; 4018 4019 4020 case DIF_SUBR_DIRNAME: 4021 case DIF_SUBR_BASENAME: { 4022 char *dest = (char *)mstate->dtms_scratch_ptr; 4023 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4024 uintptr_t src = tupregs[0].dttk_value; 4025 int i, j, len = dtrace_strlen((char *)src, size); 4026 int lastbase = -1, firstbase = -1, lastdir = -1; 4027 int start, end; 4028 4029 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 4030 regs[rd] = NULL; 4031 break; 4032 } 4033 4034 if (!DTRACE_INSCRATCH(mstate, size)) { 4035 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4036 regs[rd] = NULL; 4037 break; 4038 } 4039 4040 /* 4041 * The basename and dirname for a zero-length string is 4042 * defined to be "." 4043 */ 4044 if (len == 0) { 4045 len = 1; 4046 src = (uintptr_t)"."; 4047 } 4048 4049 /* 4050 * Start from the back of the string, moving back toward the 4051 * front until we see a character that isn't a slash. That 4052 * character is the last character in the basename. 4053 */ 4054 for (i = len - 1; i >= 0; i--) { 4055 if (dtrace_load8(src + i) != '/') 4056 break; 4057 } 4058 4059 if (i >= 0) 4060 lastbase = i; 4061 4062 /* 4063 * Starting from the last character in the basename, move 4064 * towards the front until we find a slash. The character 4065 * that we processed immediately before that is the first 4066 * character in the basename. 4067 */ 4068 for (; i >= 0; i--) { 4069 if (dtrace_load8(src + i) == '/') 4070 break; 4071 } 4072 4073 if (i >= 0) 4074 firstbase = i + 1; 4075 4076 /* 4077 * Now keep going until we find a non-slash character. That 4078 * character is the last character in the dirname. 4079 */ 4080 for (; i >= 0; i--) { 4081 if (dtrace_load8(src + i) != '/') 4082 break; 4083 } 4084 4085 if (i >= 0) 4086 lastdir = i; 4087 4088 ASSERT(!(lastbase == -1 && firstbase != -1)); 4089 ASSERT(!(firstbase == -1 && lastdir != -1)); 4090 4091 if (lastbase == -1) { 4092 /* 4093 * We didn't find a non-slash character. We know that 4094 * the length is non-zero, so the whole string must be 4095 * slashes. In either the dirname or the basename 4096 * case, we return '/'. 4097 */ 4098 ASSERT(firstbase == -1); 4099 firstbase = lastbase = lastdir = 0; 4100 } 4101 4102 if (firstbase == -1) { 4103 /* 4104 * The entire string consists only of a basename 4105 * component. If we're looking for dirname, we need 4106 * to change our string to be just "."; if we're 4107 * looking for a basename, we'll just set the first 4108 * character of the basename to be 0. 4109 */ 4110 if (subr == DIF_SUBR_DIRNAME) { 4111 ASSERT(lastdir == -1); 4112 src = (uintptr_t)"."; 4113 lastdir = 0; 4114 } else { 4115 firstbase = 0; 4116 } 4117 } 4118 4119 if (subr == DIF_SUBR_DIRNAME) { 4120 if (lastdir == -1) { 4121 /* 4122 * We know that we have a slash in the name -- 4123 * or lastdir would be set to 0, above. And 4124 * because lastdir is -1, we know that this 4125 * slash must be the first character. (That 4126 * is, the full string must be of the form 4127 * "/basename".) In this case, the last 4128 * character of the directory name is 0. 4129 */ 4130 lastdir = 0; 4131 } 4132 4133 start = 0; 4134 end = lastdir; 4135 } else { 4136 ASSERT(subr == DIF_SUBR_BASENAME); 4137 ASSERT(firstbase != -1 && lastbase != -1); 4138 start = firstbase; 4139 end = lastbase; 4140 } 4141 4142 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 4143 dest[j] = dtrace_load8(src + i); 4144 4145 dest[j] = '\0'; 4146 regs[rd] = (uintptr_t)dest; 4147 mstate->dtms_scratch_ptr += size; 4148 break; 4149 } 4150 4151 case DIF_SUBR_CLEANPATH: { 4152 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4153 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4154 uintptr_t src = tupregs[0].dttk_value; 4155 int i = 0, j = 0; 4156 4157 if (!dtrace_strcanload(src, size, mstate, vstate)) { 4158 regs[rd] = NULL; 4159 break; 4160 } 4161 4162 if (!DTRACE_INSCRATCH(mstate, size)) { 4163 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4164 regs[rd] = NULL; 4165 break; 4166 } 4167 4168 /* 4169 * Move forward, loading each character. 4170 */ 4171 do { 4172 c = dtrace_load8(src + i++); 4173 next: 4174 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 4175 break; 4176 4177 if (c != '/') { 4178 dest[j++] = c; 4179 continue; 4180 } 4181 4182 c = dtrace_load8(src + i++); 4183 4184 if (c == '/') { 4185 /* 4186 * We have two slashes -- we can just advance 4187 * to the next character. 4188 */ 4189 goto next; 4190 } 4191 4192 if (c != '.') { 4193 /* 4194 * This is not "." and it's not ".." -- we can 4195 * just store the "/" and this character and 4196 * drive on. 4197 */ 4198 dest[j++] = '/'; 4199 dest[j++] = c; 4200 continue; 4201 } 4202 4203 c = dtrace_load8(src + i++); 4204 4205 if (c == '/') { 4206 /* 4207 * This is a "/./" component. We're not going 4208 * to store anything in the destination buffer; 4209 * we're just going to go to the next component. 4210 */ 4211 goto next; 4212 } 4213 4214 if (c != '.') { 4215 /* 4216 * This is not ".." -- we can just store the 4217 * "/." and this character and continue 4218 * processing. 4219 */ 4220 dest[j++] = '/'; 4221 dest[j++] = '.'; 4222 dest[j++] = c; 4223 continue; 4224 } 4225 4226 c = dtrace_load8(src + i++); 4227 4228 if (c != '/' && c != '\0') { 4229 /* 4230 * This is not ".." -- it's "..[mumble]". 4231 * We'll store the "/.." and this character 4232 * and continue processing. 4233 */ 4234 dest[j++] = '/'; 4235 dest[j++] = '.'; 4236 dest[j++] = '.'; 4237 dest[j++] = c; 4238 continue; 4239 } 4240 4241 /* 4242 * This is "/../" or "/..\0". We need to back up 4243 * our destination pointer until we find a "/". 4244 */ 4245 i--; 4246 while (j != 0 && dest[--j] != '/') 4247 continue; 4248 4249 if (c == '\0') 4250 dest[++j] = '/'; 4251 } while (c != '\0'); 4252 4253 dest[j] = '\0'; 4254 regs[rd] = (uintptr_t)dest; 4255 mstate->dtms_scratch_ptr += size; 4256 break; 4257 } 4258 4259 case DIF_SUBR_INET_NTOA: 4260 case DIF_SUBR_INET_NTOA6: 4261 case DIF_SUBR_INET_NTOP: { 4262 size_t size; 4263 int af, argi, i; 4264 char *base, *end; 4265 4266 if (subr == DIF_SUBR_INET_NTOP) { 4267 af = (int)tupregs[0].dttk_value; 4268 argi = 1; 4269 } else { 4270 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 4271 argi = 0; 4272 } 4273 4274 if (af == AF_INET) { 4275 ipaddr_t ip4; 4276 uint8_t *ptr8, val; 4277 4278 /* 4279 * Safely load the IPv4 address. 4280 */ 4281 ip4 = dtrace_load32(tupregs[argi].dttk_value); 4282 4283 /* 4284 * Check an IPv4 string will fit in scratch. 4285 */ 4286 size = INET_ADDRSTRLEN; 4287 if (!DTRACE_INSCRATCH(mstate, size)) { 4288 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4289 regs[rd] = NULL; 4290 break; 4291 } 4292 base = (char *)mstate->dtms_scratch_ptr; 4293 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4294 4295 /* 4296 * Stringify as a dotted decimal quad. 4297 */ 4298 *end-- = '\0'; 4299 ptr8 = (uint8_t *)&ip4; 4300 for (i = 3; i >= 0; i--) { 4301 val = ptr8[i]; 4302 4303 if (val == 0) { 4304 *end-- = '0'; 4305 } else { 4306 for (; val; val /= 10) { 4307 *end-- = '0' + (val % 10); 4308 } 4309 } 4310 4311 if (i > 0) 4312 *end-- = '.'; 4313 } 4314 ASSERT(end + 1 >= base); 4315 4316 } else if (af == AF_INET6) { 4317 struct in6_addr ip6; 4318 int firstzero, tryzero, numzero, v6end; 4319 uint16_t val; 4320 const char digits[] = "0123456789abcdef"; 4321 4322 /* 4323 * Stringify using RFC 1884 convention 2 - 16 bit 4324 * hexadecimal values with a zero-run compression. 4325 * Lower case hexadecimal digits are used. 4326 * eg, fe80::214:4fff:fe0b:76c8. 4327 * The IPv4 embedded form is returned for inet_ntop, 4328 * just the IPv4 string is returned for inet_ntoa6. 4329 */ 4330 4331 /* 4332 * Safely load the IPv6 address. 4333 */ 4334 dtrace_bcopy( 4335 (void *)(uintptr_t)tupregs[argi].dttk_value, 4336 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 4337 4338 /* 4339 * Check an IPv6 string will fit in scratch. 4340 */ 4341 size = INET6_ADDRSTRLEN; 4342 if (!DTRACE_INSCRATCH(mstate, size)) { 4343 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4344 regs[rd] = NULL; 4345 break; 4346 } 4347 base = (char *)mstate->dtms_scratch_ptr; 4348 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4349 *end-- = '\0'; 4350 4351 /* 4352 * Find the longest run of 16 bit zero values 4353 * for the single allowed zero compression - "::". 4354 */ 4355 firstzero = -1; 4356 tryzero = -1; 4357 numzero = 1; 4358 for (i = 0; i < sizeof (struct in6_addr); i++) { 4359 if (ip6._S6_un._S6_u8[i] == 0 && 4360 tryzero == -1 && i % 2 == 0) { 4361 tryzero = i; 4362 continue; 4363 } 4364 4365 if (tryzero != -1 && 4366 (ip6._S6_un._S6_u8[i] != 0 || 4367 i == sizeof (struct in6_addr) - 1)) { 4368 4369 if (i - tryzero <= numzero) { 4370 tryzero = -1; 4371 continue; 4372 } 4373 4374 firstzero = tryzero; 4375 numzero = i - i % 2 - tryzero; 4376 tryzero = -1; 4377 4378 if (ip6._S6_un._S6_u8[i] == 0 && 4379 i == sizeof (struct in6_addr) - 1) 4380 numzero += 2; 4381 } 4382 } 4383 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 4384 4385 /* 4386 * Check for an IPv4 embedded address. 4387 */ 4388 v6end = sizeof (struct in6_addr) - 2; 4389 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 4390 IN6_IS_ADDR_V4COMPAT(&ip6)) { 4391 for (i = sizeof (struct in6_addr) - 1; 4392 i >= DTRACE_V4MAPPED_OFFSET; i--) { 4393 ASSERT(end >= base); 4394 4395 val = ip6._S6_un._S6_u8[i]; 4396 4397 if (val == 0) { 4398 *end-- = '0'; 4399 } else { 4400 for (; val; val /= 10) { 4401 *end-- = '0' + val % 10; 4402 } 4403 } 4404 4405 if (i > DTRACE_V4MAPPED_OFFSET) 4406 *end-- = '.'; 4407 } 4408 4409 if (subr == DIF_SUBR_INET_NTOA6) 4410 goto inetout; 4411 4412 /* 4413 * Set v6end to skip the IPv4 address that 4414 * we have already stringified. 4415 */ 4416 v6end = 10; 4417 } 4418 4419 /* 4420 * Build the IPv6 string by working through the 4421 * address in reverse. 4422 */ 4423 for (i = v6end; i >= 0; i -= 2) { 4424 ASSERT(end >= base); 4425 4426 if (i == firstzero + numzero - 2) { 4427 *end-- = ':'; 4428 *end-- = ':'; 4429 i -= numzero - 2; 4430 continue; 4431 } 4432 4433 if (i < 14 && i != firstzero - 2) 4434 *end-- = ':'; 4435 4436 val = (ip6._S6_un._S6_u8[i] << 8) + 4437 ip6._S6_un._S6_u8[i + 1]; 4438 4439 if (val == 0) { 4440 *end-- = '0'; 4441 } else { 4442 for (; val; val /= 16) { 4443 *end-- = digits[val % 16]; 4444 } 4445 } 4446 } 4447 ASSERT(end + 1 >= base); 4448 4449 } else { 4450 /* 4451 * The user didn't use AH_INET or AH_INET6. 4452 */ 4453 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4454 regs[rd] = NULL; 4455 break; 4456 } 4457 4458 inetout: regs[rd] = (uintptr_t)end + 1; 4459 mstate->dtms_scratch_ptr += size; 4460 break; 4461 } 4462 4463 } 4464 } 4465 4466 /* 4467 * Emulate the execution of DTrace IR instructions specified by the given 4468 * DIF object. This function is deliberately void of assertions as all of 4469 * the necessary checks are handled by a call to dtrace_difo_validate(). 4470 */ 4471 static uint64_t 4472 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 4473 dtrace_vstate_t *vstate, dtrace_state_t *state) 4474 { 4475 const dif_instr_t *text = difo->dtdo_buf; 4476 const uint_t textlen = difo->dtdo_len; 4477 const char *strtab = difo->dtdo_strtab; 4478 const uint64_t *inttab = difo->dtdo_inttab; 4479 4480 uint64_t rval = 0; 4481 dtrace_statvar_t *svar; 4482 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 4483 dtrace_difv_t *v; 4484 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 4485 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 4486 4487 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 4488 uint64_t regs[DIF_DIR_NREGS]; 4489 uint64_t *tmp; 4490 4491 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 4492 int64_t cc_r; 4493 uint_t pc = 0, id, opc; 4494 uint8_t ttop = 0; 4495 dif_instr_t instr; 4496 uint_t r1, r2, rd; 4497 4498 /* 4499 * We stash the current DIF object into the machine state: we need it 4500 * for subsequent access checking. 4501 */ 4502 mstate->dtms_difo = difo; 4503 4504 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 4505 4506 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 4507 opc = pc; 4508 4509 instr = text[pc++]; 4510 r1 = DIF_INSTR_R1(instr); 4511 r2 = DIF_INSTR_R2(instr); 4512 rd = DIF_INSTR_RD(instr); 4513 4514 switch (DIF_INSTR_OP(instr)) { 4515 case DIF_OP_OR: 4516 regs[rd] = regs[r1] | regs[r2]; 4517 break; 4518 case DIF_OP_XOR: 4519 regs[rd] = regs[r1] ^ regs[r2]; 4520 break; 4521 case DIF_OP_AND: 4522 regs[rd] = regs[r1] & regs[r2]; 4523 break; 4524 case DIF_OP_SLL: 4525 regs[rd] = regs[r1] << regs[r2]; 4526 break; 4527 case DIF_OP_SRL: 4528 regs[rd] = regs[r1] >> regs[r2]; 4529 break; 4530 case DIF_OP_SUB: 4531 regs[rd] = regs[r1] - regs[r2]; 4532 break; 4533 case DIF_OP_ADD: 4534 regs[rd] = regs[r1] + regs[r2]; 4535 break; 4536 case DIF_OP_MUL: 4537 regs[rd] = regs[r1] * regs[r2]; 4538 break; 4539 case DIF_OP_SDIV: 4540 if (regs[r2] == 0) { 4541 regs[rd] = 0; 4542 *flags |= CPU_DTRACE_DIVZERO; 4543 } else { 4544 regs[rd] = (int64_t)regs[r1] / 4545 (int64_t)regs[r2]; 4546 } 4547 break; 4548 4549 case DIF_OP_UDIV: 4550 if (regs[r2] == 0) { 4551 regs[rd] = 0; 4552 *flags |= CPU_DTRACE_DIVZERO; 4553 } else { 4554 regs[rd] = regs[r1] / regs[r2]; 4555 } 4556 break; 4557 4558 case DIF_OP_SREM: 4559 if (regs[r2] == 0) { 4560 regs[rd] = 0; 4561 *flags |= CPU_DTRACE_DIVZERO; 4562 } else { 4563 regs[rd] = (int64_t)regs[r1] % 4564 (int64_t)regs[r2]; 4565 } 4566 break; 4567 4568 case DIF_OP_UREM: 4569 if (regs[r2] == 0) { 4570 regs[rd] = 0; 4571 *flags |= CPU_DTRACE_DIVZERO; 4572 } else { 4573 regs[rd] = regs[r1] % regs[r2]; 4574 } 4575 break; 4576 4577 case DIF_OP_NOT: 4578 regs[rd] = ~regs[r1]; 4579 break; 4580 case DIF_OP_MOV: 4581 regs[rd] = regs[r1]; 4582 break; 4583 case DIF_OP_CMP: 4584 cc_r = regs[r1] - regs[r2]; 4585 cc_n = cc_r < 0; 4586 cc_z = cc_r == 0; 4587 cc_v = 0; 4588 cc_c = regs[r1] < regs[r2]; 4589 break; 4590 case DIF_OP_TST: 4591 cc_n = cc_v = cc_c = 0; 4592 cc_z = regs[r1] == 0; 4593 break; 4594 case DIF_OP_BA: 4595 pc = DIF_INSTR_LABEL(instr); 4596 break; 4597 case DIF_OP_BE: 4598 if (cc_z) 4599 pc = DIF_INSTR_LABEL(instr); 4600 break; 4601 case DIF_OP_BNE: 4602 if (cc_z == 0) 4603 pc = DIF_INSTR_LABEL(instr); 4604 break; 4605 case DIF_OP_BG: 4606 if ((cc_z | (cc_n ^ cc_v)) == 0) 4607 pc = DIF_INSTR_LABEL(instr); 4608 break; 4609 case DIF_OP_BGU: 4610 if ((cc_c | cc_z) == 0) 4611 pc = DIF_INSTR_LABEL(instr); 4612 break; 4613 case DIF_OP_BGE: 4614 if ((cc_n ^ cc_v) == 0) 4615 pc = DIF_INSTR_LABEL(instr); 4616 break; 4617 case DIF_OP_BGEU: 4618 if (cc_c == 0) 4619 pc = DIF_INSTR_LABEL(instr); 4620 break; 4621 case DIF_OP_BL: 4622 if (cc_n ^ cc_v) 4623 pc = DIF_INSTR_LABEL(instr); 4624 break; 4625 case DIF_OP_BLU: 4626 if (cc_c) 4627 pc = DIF_INSTR_LABEL(instr); 4628 break; 4629 case DIF_OP_BLE: 4630 if (cc_z | (cc_n ^ cc_v)) 4631 pc = DIF_INSTR_LABEL(instr); 4632 break; 4633 case DIF_OP_BLEU: 4634 if (cc_c | cc_z) 4635 pc = DIF_INSTR_LABEL(instr); 4636 break; 4637 case DIF_OP_RLDSB: 4638 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 4639 *flags |= CPU_DTRACE_KPRIV; 4640 *illval = regs[r1]; 4641 break; 4642 } 4643 /*FALLTHROUGH*/ 4644 case DIF_OP_LDSB: 4645 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 4646 break; 4647 case DIF_OP_RLDSH: 4648 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 4649 *flags |= CPU_DTRACE_KPRIV; 4650 *illval = regs[r1]; 4651 break; 4652 } 4653 /*FALLTHROUGH*/ 4654 case DIF_OP_LDSH: 4655 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 4656 break; 4657 case DIF_OP_RLDSW: 4658 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 4659 *flags |= CPU_DTRACE_KPRIV; 4660 *illval = regs[r1]; 4661 break; 4662 } 4663 /*FALLTHROUGH*/ 4664 case DIF_OP_LDSW: 4665 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 4666 break; 4667 case DIF_OP_RLDUB: 4668 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 4669 *flags |= CPU_DTRACE_KPRIV; 4670 *illval = regs[r1]; 4671 break; 4672 } 4673 /*FALLTHROUGH*/ 4674 case DIF_OP_LDUB: 4675 regs[rd] = dtrace_load8(regs[r1]); 4676 break; 4677 case DIF_OP_RLDUH: 4678 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 4679 *flags |= CPU_DTRACE_KPRIV; 4680 *illval = regs[r1]; 4681 break; 4682 } 4683 /*FALLTHROUGH*/ 4684 case DIF_OP_LDUH: 4685 regs[rd] = dtrace_load16(regs[r1]); 4686 break; 4687 case DIF_OP_RLDUW: 4688 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 4689 *flags |= CPU_DTRACE_KPRIV; 4690 *illval = regs[r1]; 4691 break; 4692 } 4693 /*FALLTHROUGH*/ 4694 case DIF_OP_LDUW: 4695 regs[rd] = dtrace_load32(regs[r1]); 4696 break; 4697 case DIF_OP_RLDX: 4698 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 4699 *flags |= CPU_DTRACE_KPRIV; 4700 *illval = regs[r1]; 4701 break; 4702 } 4703 /*FALLTHROUGH*/ 4704 case DIF_OP_LDX: 4705 regs[rd] = dtrace_load64(regs[r1]); 4706 break; 4707 case DIF_OP_ULDSB: 4708 regs[rd] = (int8_t) 4709 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 4710 break; 4711 case DIF_OP_ULDSH: 4712 regs[rd] = (int16_t) 4713 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 4714 break; 4715 case DIF_OP_ULDSW: 4716 regs[rd] = (int32_t) 4717 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 4718 break; 4719 case DIF_OP_ULDUB: 4720 regs[rd] = 4721 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 4722 break; 4723 case DIF_OP_ULDUH: 4724 regs[rd] = 4725 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 4726 break; 4727 case DIF_OP_ULDUW: 4728 regs[rd] = 4729 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 4730 break; 4731 case DIF_OP_ULDX: 4732 regs[rd] = 4733 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 4734 break; 4735 case DIF_OP_RET: 4736 rval = regs[rd]; 4737 pc = textlen; 4738 break; 4739 case DIF_OP_NOP: 4740 break; 4741 case DIF_OP_SETX: 4742 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 4743 break; 4744 case DIF_OP_SETS: 4745 regs[rd] = (uint64_t)(uintptr_t) 4746 (strtab + DIF_INSTR_STRING(instr)); 4747 break; 4748 case DIF_OP_SCMP: { 4749 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 4750 uintptr_t s1 = regs[r1]; 4751 uintptr_t s2 = regs[r2]; 4752 4753 if (s1 != NULL && 4754 !dtrace_strcanload(s1, sz, mstate, vstate)) 4755 break; 4756 if (s2 != NULL && 4757 !dtrace_strcanload(s2, sz, mstate, vstate)) 4758 break; 4759 4760 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 4761 4762 cc_n = cc_r < 0; 4763 cc_z = cc_r == 0; 4764 cc_v = cc_c = 0; 4765 break; 4766 } 4767 case DIF_OP_LDGA: 4768 regs[rd] = dtrace_dif_variable(mstate, state, 4769 r1, regs[r2]); 4770 break; 4771 case DIF_OP_LDGS: 4772 id = DIF_INSTR_VAR(instr); 4773 4774 if (id >= DIF_VAR_OTHER_UBASE) { 4775 uintptr_t a; 4776 4777 id -= DIF_VAR_OTHER_UBASE; 4778 svar = vstate->dtvs_globals[id]; 4779 ASSERT(svar != NULL); 4780 v = &svar->dtsv_var; 4781 4782 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 4783 regs[rd] = svar->dtsv_data; 4784 break; 4785 } 4786 4787 a = (uintptr_t)svar->dtsv_data; 4788 4789 if (*(uint8_t *)a == UINT8_MAX) { 4790 /* 4791 * If the 0th byte is set to UINT8_MAX 4792 * then this is to be treated as a 4793 * reference to a NULL variable. 4794 */ 4795 regs[rd] = NULL; 4796 } else { 4797 regs[rd] = a + sizeof (uint64_t); 4798 } 4799 4800 break; 4801 } 4802 4803 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 4804 break; 4805 4806 case DIF_OP_STGS: 4807 id = DIF_INSTR_VAR(instr); 4808 4809 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4810 id -= DIF_VAR_OTHER_UBASE; 4811 4812 svar = vstate->dtvs_globals[id]; 4813 ASSERT(svar != NULL); 4814 v = &svar->dtsv_var; 4815 4816 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4817 uintptr_t a = (uintptr_t)svar->dtsv_data; 4818 4819 ASSERT(a != NULL); 4820 ASSERT(svar->dtsv_size != 0); 4821 4822 if (regs[rd] == NULL) { 4823 *(uint8_t *)a = UINT8_MAX; 4824 break; 4825 } else { 4826 *(uint8_t *)a = 0; 4827 a += sizeof (uint64_t); 4828 } 4829 if (!dtrace_vcanload( 4830 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 4831 mstate, vstate)) 4832 break; 4833 4834 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4835 (void *)a, &v->dtdv_type); 4836 break; 4837 } 4838 4839 svar->dtsv_data = regs[rd]; 4840 break; 4841 4842 case DIF_OP_LDTA: 4843 /* 4844 * There are no DTrace built-in thread-local arrays at 4845 * present. This opcode is saved for future work. 4846 */ 4847 *flags |= CPU_DTRACE_ILLOP; 4848 regs[rd] = 0; 4849 break; 4850 4851 case DIF_OP_LDLS: 4852 id = DIF_INSTR_VAR(instr); 4853 4854 if (id < DIF_VAR_OTHER_UBASE) { 4855 /* 4856 * For now, this has no meaning. 4857 */ 4858 regs[rd] = 0; 4859 break; 4860 } 4861 4862 id -= DIF_VAR_OTHER_UBASE; 4863 4864 ASSERT(id < vstate->dtvs_nlocals); 4865 ASSERT(vstate->dtvs_locals != NULL); 4866 4867 svar = vstate->dtvs_locals[id]; 4868 ASSERT(svar != NULL); 4869 v = &svar->dtsv_var; 4870 4871 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4872 uintptr_t a = (uintptr_t)svar->dtsv_data; 4873 size_t sz = v->dtdv_type.dtdt_size; 4874 4875 sz += sizeof (uint64_t); 4876 ASSERT(svar->dtsv_size == NCPU * sz); 4877 a += CPU->cpu_id * sz; 4878 4879 if (*(uint8_t *)a == UINT8_MAX) { 4880 /* 4881 * If the 0th byte is set to UINT8_MAX 4882 * then this is to be treated as a 4883 * reference to a NULL variable. 4884 */ 4885 regs[rd] = NULL; 4886 } else { 4887 regs[rd] = a + sizeof (uint64_t); 4888 } 4889 4890 break; 4891 } 4892 4893 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 4894 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 4895 regs[rd] = tmp[CPU->cpu_id]; 4896 break; 4897 4898 case DIF_OP_STLS: 4899 id = DIF_INSTR_VAR(instr); 4900 4901 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4902 id -= DIF_VAR_OTHER_UBASE; 4903 ASSERT(id < vstate->dtvs_nlocals); 4904 4905 ASSERT(vstate->dtvs_locals != NULL); 4906 svar = vstate->dtvs_locals[id]; 4907 ASSERT(svar != NULL); 4908 v = &svar->dtsv_var; 4909 4910 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4911 uintptr_t a = (uintptr_t)svar->dtsv_data; 4912 size_t sz = v->dtdv_type.dtdt_size; 4913 4914 sz += sizeof (uint64_t); 4915 ASSERT(svar->dtsv_size == NCPU * sz); 4916 a += CPU->cpu_id * sz; 4917 4918 if (regs[rd] == NULL) { 4919 *(uint8_t *)a = UINT8_MAX; 4920 break; 4921 } else { 4922 *(uint8_t *)a = 0; 4923 a += sizeof (uint64_t); 4924 } 4925 4926 if (!dtrace_vcanload( 4927 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 4928 mstate, vstate)) 4929 break; 4930 4931 dtrace_vcopy((void *)(uintptr_t)regs[rd], 4932 (void *)a, &v->dtdv_type); 4933 break; 4934 } 4935 4936 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 4937 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 4938 tmp[CPU->cpu_id] = regs[rd]; 4939 break; 4940 4941 case DIF_OP_LDTS: { 4942 dtrace_dynvar_t *dvar; 4943 dtrace_key_t *key; 4944 4945 id = DIF_INSTR_VAR(instr); 4946 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4947 id -= DIF_VAR_OTHER_UBASE; 4948 v = &vstate->dtvs_tlocals[id]; 4949 4950 key = &tupregs[DIF_DTR_NREGS]; 4951 key[0].dttk_value = (uint64_t)id; 4952 key[0].dttk_size = 0; 4953 DTRACE_TLS_THRKEY(key[1].dttk_value); 4954 key[1].dttk_size = 0; 4955 4956 dvar = dtrace_dynvar(dstate, 2, key, 4957 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 4958 mstate, vstate); 4959 4960 if (dvar == NULL) { 4961 regs[rd] = 0; 4962 break; 4963 } 4964 4965 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 4966 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 4967 } else { 4968 regs[rd] = *((uint64_t *)dvar->dtdv_data); 4969 } 4970 4971 break; 4972 } 4973 4974 case DIF_OP_STTS: { 4975 dtrace_dynvar_t *dvar; 4976 dtrace_key_t *key; 4977 4978 id = DIF_INSTR_VAR(instr); 4979 ASSERT(id >= DIF_VAR_OTHER_UBASE); 4980 id -= DIF_VAR_OTHER_UBASE; 4981 4982 key = &tupregs[DIF_DTR_NREGS]; 4983 key[0].dttk_value = (uint64_t)id; 4984 key[0].dttk_size = 0; 4985 DTRACE_TLS_THRKEY(key[1].dttk_value); 4986 key[1].dttk_size = 0; 4987 v = &vstate->dtvs_tlocals[id]; 4988 4989 dvar = dtrace_dynvar(dstate, 2, key, 4990 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 4991 v->dtdv_type.dtdt_size : sizeof (uint64_t), 4992 regs[rd] ? DTRACE_DYNVAR_ALLOC : 4993 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 4994 4995 /* 4996 * Given that we're storing to thread-local data, 4997 * we need to flush our predicate cache. 4998 */ 4999 curthread->t_predcache = NULL; 5000 5001 if (dvar == NULL) 5002 break; 5003 5004 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5005 if (!dtrace_vcanload( 5006 (void *)(uintptr_t)regs[rd], 5007 &v->dtdv_type, mstate, vstate)) 5008 break; 5009 5010 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5011 dvar->dtdv_data, &v->dtdv_type); 5012 } else { 5013 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5014 } 5015 5016 break; 5017 } 5018 5019 case DIF_OP_SRA: 5020 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 5021 break; 5022 5023 case DIF_OP_CALL: 5024 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 5025 regs, tupregs, ttop, mstate, state); 5026 break; 5027 5028 case DIF_OP_PUSHTR: 5029 if (ttop == DIF_DTR_NREGS) { 5030 *flags |= CPU_DTRACE_TUPOFLOW; 5031 break; 5032 } 5033 5034 if (r1 == DIF_TYPE_STRING) { 5035 /* 5036 * If this is a string type and the size is 0, 5037 * we'll use the system-wide default string 5038 * size. Note that we are _not_ looking at 5039 * the value of the DTRACEOPT_STRSIZE option; 5040 * had this been set, we would expect to have 5041 * a non-zero size value in the "pushtr". 5042 */ 5043 tupregs[ttop].dttk_size = 5044 dtrace_strlen((char *)(uintptr_t)regs[rd], 5045 regs[r2] ? regs[r2] : 5046 dtrace_strsize_default) + 1; 5047 } else { 5048 tupregs[ttop].dttk_size = regs[r2]; 5049 } 5050 5051 tupregs[ttop++].dttk_value = regs[rd]; 5052 break; 5053 5054 case DIF_OP_PUSHTV: 5055 if (ttop == DIF_DTR_NREGS) { 5056 *flags |= CPU_DTRACE_TUPOFLOW; 5057 break; 5058 } 5059 5060 tupregs[ttop].dttk_value = regs[rd]; 5061 tupregs[ttop++].dttk_size = 0; 5062 break; 5063 5064 case DIF_OP_POPTS: 5065 if (ttop != 0) 5066 ttop--; 5067 break; 5068 5069 case DIF_OP_FLUSHTS: 5070 ttop = 0; 5071 break; 5072 5073 case DIF_OP_LDGAA: 5074 case DIF_OP_LDTAA: { 5075 dtrace_dynvar_t *dvar; 5076 dtrace_key_t *key = tupregs; 5077 uint_t nkeys = ttop; 5078 5079 id = DIF_INSTR_VAR(instr); 5080 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5081 id -= DIF_VAR_OTHER_UBASE; 5082 5083 key[nkeys].dttk_value = (uint64_t)id; 5084 key[nkeys++].dttk_size = 0; 5085 5086 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 5087 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5088 key[nkeys++].dttk_size = 0; 5089 v = &vstate->dtvs_tlocals[id]; 5090 } else { 5091 v = &vstate->dtvs_globals[id]->dtsv_var; 5092 } 5093 5094 dvar = dtrace_dynvar(dstate, nkeys, key, 5095 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5096 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5097 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 5098 5099 if (dvar == NULL) { 5100 regs[rd] = 0; 5101 break; 5102 } 5103 5104 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5105 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5106 } else { 5107 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5108 } 5109 5110 break; 5111 } 5112 5113 case DIF_OP_STGAA: 5114 case DIF_OP_STTAA: { 5115 dtrace_dynvar_t *dvar; 5116 dtrace_key_t *key = tupregs; 5117 uint_t nkeys = ttop; 5118 5119 id = DIF_INSTR_VAR(instr); 5120 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5121 id -= DIF_VAR_OTHER_UBASE; 5122 5123 key[nkeys].dttk_value = (uint64_t)id; 5124 key[nkeys++].dttk_size = 0; 5125 5126 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 5127 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5128 key[nkeys++].dttk_size = 0; 5129 v = &vstate->dtvs_tlocals[id]; 5130 } else { 5131 v = &vstate->dtvs_globals[id]->dtsv_var; 5132 } 5133 5134 dvar = dtrace_dynvar(dstate, nkeys, key, 5135 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5136 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5137 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5138 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5139 5140 if (dvar == NULL) 5141 break; 5142 5143 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5144 if (!dtrace_vcanload( 5145 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5146 mstate, vstate)) 5147 break; 5148 5149 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5150 dvar->dtdv_data, &v->dtdv_type); 5151 } else { 5152 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5153 } 5154 5155 break; 5156 } 5157 5158 case DIF_OP_ALLOCS: { 5159 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5160 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 5161 5162 /* 5163 * Rounding up the user allocation size could have 5164 * overflowed large, bogus allocations (like -1ULL) to 5165 * 0. 5166 */ 5167 if (size < regs[r1] || 5168 !DTRACE_INSCRATCH(mstate, size)) { 5169 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5170 regs[rd] = NULL; 5171 break; 5172 } 5173 5174 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 5175 mstate->dtms_scratch_ptr += size; 5176 regs[rd] = ptr; 5177 break; 5178 } 5179 5180 case DIF_OP_COPYS: 5181 if (!dtrace_canstore(regs[rd], regs[r2], 5182 mstate, vstate)) { 5183 *flags |= CPU_DTRACE_BADADDR; 5184 *illval = regs[rd]; 5185 break; 5186 } 5187 5188 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 5189 break; 5190 5191 dtrace_bcopy((void *)(uintptr_t)regs[r1], 5192 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 5193 break; 5194 5195 case DIF_OP_STB: 5196 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 5197 *flags |= CPU_DTRACE_BADADDR; 5198 *illval = regs[rd]; 5199 break; 5200 } 5201 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 5202 break; 5203 5204 case DIF_OP_STH: 5205 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 5206 *flags |= CPU_DTRACE_BADADDR; 5207 *illval = regs[rd]; 5208 break; 5209 } 5210 if (regs[rd] & 1) { 5211 *flags |= CPU_DTRACE_BADALIGN; 5212 *illval = regs[rd]; 5213 break; 5214 } 5215 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 5216 break; 5217 5218 case DIF_OP_STW: 5219 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 5220 *flags |= CPU_DTRACE_BADADDR; 5221 *illval = regs[rd]; 5222 break; 5223 } 5224 if (regs[rd] & 3) { 5225 *flags |= CPU_DTRACE_BADALIGN; 5226 *illval = regs[rd]; 5227 break; 5228 } 5229 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 5230 break; 5231 5232 case DIF_OP_STX: 5233 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 5234 *flags |= CPU_DTRACE_BADADDR; 5235 *illval = regs[rd]; 5236 break; 5237 } 5238 if (regs[rd] & 7) { 5239 *flags |= CPU_DTRACE_BADALIGN; 5240 *illval = regs[rd]; 5241 break; 5242 } 5243 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 5244 break; 5245 } 5246 } 5247 5248 if (!(*flags & CPU_DTRACE_FAULT)) 5249 return (rval); 5250 5251 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 5252 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 5253 5254 return (0); 5255 } 5256 5257 static void 5258 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 5259 { 5260 dtrace_probe_t *probe = ecb->dte_probe; 5261 dtrace_provider_t *prov = probe->dtpr_provider; 5262 char c[DTRACE_FULLNAMELEN + 80], *str; 5263 char *msg = "dtrace: breakpoint action at probe "; 5264 char *ecbmsg = " (ecb "; 5265 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 5266 uintptr_t val = (uintptr_t)ecb; 5267 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 5268 5269 if (dtrace_destructive_disallow) 5270 return; 5271 5272 /* 5273 * It's impossible to be taking action on the NULL probe. 5274 */ 5275 ASSERT(probe != NULL); 5276 5277 /* 5278 * This is a poor man's (destitute man's?) sprintf(): we want to 5279 * print the provider name, module name, function name and name of 5280 * the probe, along with the hex address of the ECB with the breakpoint 5281 * action -- all of which we must place in the character buffer by 5282 * hand. 5283 */ 5284 while (*msg != '\0') 5285 c[i++] = *msg++; 5286 5287 for (str = prov->dtpv_name; *str != '\0'; str++) 5288 c[i++] = *str; 5289 c[i++] = ':'; 5290 5291 for (str = probe->dtpr_mod; *str != '\0'; str++) 5292 c[i++] = *str; 5293 c[i++] = ':'; 5294 5295 for (str = probe->dtpr_func; *str != '\0'; str++) 5296 c[i++] = *str; 5297 c[i++] = ':'; 5298 5299 for (str = probe->dtpr_name; *str != '\0'; str++) 5300 c[i++] = *str; 5301 5302 while (*ecbmsg != '\0') 5303 c[i++] = *ecbmsg++; 5304 5305 while (shift >= 0) { 5306 mask = (uintptr_t)0xf << shift; 5307 5308 if (val >= ((uintptr_t)1 << shift)) 5309 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 5310 shift -= 4; 5311 } 5312 5313 c[i++] = ')'; 5314 c[i] = '\0'; 5315 5316 debug_enter(c); 5317 } 5318 5319 static void 5320 dtrace_action_panic(dtrace_ecb_t *ecb) 5321 { 5322 dtrace_probe_t *probe = ecb->dte_probe; 5323 5324 /* 5325 * It's impossible to be taking action on the NULL probe. 5326 */ 5327 ASSERT(probe != NULL); 5328 5329 if (dtrace_destructive_disallow) 5330 return; 5331 5332 if (dtrace_panicked != NULL) 5333 return; 5334 5335 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 5336 return; 5337 5338 /* 5339 * We won the right to panic. (We want to be sure that only one 5340 * thread calls panic() from dtrace_probe(), and that panic() is 5341 * called exactly once.) 5342 */ 5343 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 5344 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 5345 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 5346 } 5347 5348 static void 5349 dtrace_action_raise(uint64_t sig) 5350 { 5351 if (dtrace_destructive_disallow) 5352 return; 5353 5354 if (sig >= NSIG) { 5355 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5356 return; 5357 } 5358 5359 /* 5360 * raise() has a queue depth of 1 -- we ignore all subsequent 5361 * invocations of the raise() action. 5362 */ 5363 if (curthread->t_dtrace_sig == 0) 5364 curthread->t_dtrace_sig = (uint8_t)sig; 5365 5366 curthread->t_sig_check = 1; 5367 aston(curthread); 5368 } 5369 5370 static void 5371 dtrace_action_stop(void) 5372 { 5373 if (dtrace_destructive_disallow) 5374 return; 5375 5376 if (!curthread->t_dtrace_stop) { 5377 curthread->t_dtrace_stop = 1; 5378 curthread->t_sig_check = 1; 5379 aston(curthread); 5380 } 5381 } 5382 5383 static void 5384 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 5385 { 5386 hrtime_t now; 5387 volatile uint16_t *flags; 5388 cpu_t *cpu = CPU; 5389 5390 if (dtrace_destructive_disallow) 5391 return; 5392 5393 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 5394 5395 now = dtrace_gethrtime(); 5396 5397 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 5398 /* 5399 * We need to advance the mark to the current time. 5400 */ 5401 cpu->cpu_dtrace_chillmark = now; 5402 cpu->cpu_dtrace_chilled = 0; 5403 } 5404 5405 /* 5406 * Now check to see if the requested chill time would take us over 5407 * the maximum amount of time allowed in the chill interval. (Or 5408 * worse, if the calculation itself induces overflow.) 5409 */ 5410 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 5411 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 5412 *flags |= CPU_DTRACE_ILLOP; 5413 return; 5414 } 5415 5416 while (dtrace_gethrtime() - now < val) 5417 continue; 5418 5419 /* 5420 * Normally, we assure that the value of the variable "timestamp" does 5421 * not change within an ECB. The presence of chill() represents an 5422 * exception to this rule, however. 5423 */ 5424 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 5425 cpu->cpu_dtrace_chilled += val; 5426 } 5427 5428 static void 5429 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 5430 uint64_t *buf, uint64_t arg) 5431 { 5432 int nframes = DTRACE_USTACK_NFRAMES(arg); 5433 int strsize = DTRACE_USTACK_STRSIZE(arg); 5434 uint64_t *pcs = &buf[1], *fps; 5435 char *str = (char *)&pcs[nframes]; 5436 int size, offs = 0, i, j; 5437 uintptr_t old = mstate->dtms_scratch_ptr, saved; 5438 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 5439 char *sym; 5440 5441 /* 5442 * Should be taking a faster path if string space has not been 5443 * allocated. 5444 */ 5445 ASSERT(strsize != 0); 5446 5447 /* 5448 * We will first allocate some temporary space for the frame pointers. 5449 */ 5450 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5451 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 5452 (nframes * sizeof (uint64_t)); 5453 5454 if (!DTRACE_INSCRATCH(mstate, size)) { 5455 /* 5456 * Not enough room for our frame pointers -- need to indicate 5457 * that we ran out of scratch space. 5458 */ 5459 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5460 return; 5461 } 5462 5463 mstate->dtms_scratch_ptr += size; 5464 saved = mstate->dtms_scratch_ptr; 5465 5466 /* 5467 * Now get a stack with both program counters and frame pointers. 5468 */ 5469 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5470 dtrace_getufpstack(buf, fps, nframes + 1); 5471 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5472 5473 /* 5474 * If that faulted, we're cooked. 5475 */ 5476 if (*flags & CPU_DTRACE_FAULT) 5477 goto out; 5478 5479 /* 5480 * Now we want to walk up the stack, calling the USTACK helper. For 5481 * each iteration, we restore the scratch pointer. 5482 */ 5483 for (i = 0; i < nframes; i++) { 5484 mstate->dtms_scratch_ptr = saved; 5485 5486 if (offs >= strsize) 5487 break; 5488 5489 sym = (char *)(uintptr_t)dtrace_helper( 5490 DTRACE_HELPER_ACTION_USTACK, 5491 mstate, state, pcs[i], fps[i]); 5492 5493 /* 5494 * If we faulted while running the helper, we're going to 5495 * clear the fault and null out the corresponding string. 5496 */ 5497 if (*flags & CPU_DTRACE_FAULT) { 5498 *flags &= ~CPU_DTRACE_FAULT; 5499 str[offs++] = '\0'; 5500 continue; 5501 } 5502 5503 if (sym == NULL) { 5504 str[offs++] = '\0'; 5505 continue; 5506 } 5507 5508 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5509 5510 /* 5511 * Now copy in the string that the helper returned to us. 5512 */ 5513 for (j = 0; offs + j < strsize; j++) { 5514 if ((str[offs + j] = sym[j]) == '\0') 5515 break; 5516 } 5517 5518 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5519 5520 offs += j + 1; 5521 } 5522 5523 if (offs >= strsize) { 5524 /* 5525 * If we didn't have room for all of the strings, we don't 5526 * abort processing -- this needn't be a fatal error -- but we 5527 * still want to increment a counter (dts_stkstroverflows) to 5528 * allow this condition to be warned about. (If this is from 5529 * a jstack() action, it is easily tuned via jstackstrsize.) 5530 */ 5531 dtrace_error(&state->dts_stkstroverflows); 5532 } 5533 5534 while (offs < strsize) 5535 str[offs++] = '\0'; 5536 5537 out: 5538 mstate->dtms_scratch_ptr = old; 5539 } 5540 5541 /* 5542 * If you're looking for the epicenter of DTrace, you just found it. This 5543 * is the function called by the provider to fire a probe -- from which all 5544 * subsequent probe-context DTrace activity emanates. 5545 */ 5546 void 5547 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 5548 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 5549 { 5550 processorid_t cpuid; 5551 dtrace_icookie_t cookie; 5552 dtrace_probe_t *probe; 5553 dtrace_mstate_t mstate; 5554 dtrace_ecb_t *ecb; 5555 dtrace_action_t *act; 5556 intptr_t offs; 5557 size_t size; 5558 int vtime, onintr; 5559 volatile uint16_t *flags; 5560 hrtime_t now; 5561 5562 /* 5563 * Kick out immediately if this CPU is still being born (in which case 5564 * curthread will be set to -1) or the current thread can't allow 5565 * probes in its current context. 5566 */ 5567 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 5568 return; 5569 5570 cookie = dtrace_interrupt_disable(); 5571 probe = dtrace_probes[id - 1]; 5572 cpuid = CPU->cpu_id; 5573 onintr = CPU_ON_INTR(CPU); 5574 5575 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 5576 probe->dtpr_predcache == curthread->t_predcache) { 5577 /* 5578 * We have hit in the predicate cache; we know that 5579 * this predicate would evaluate to be false. 5580 */ 5581 dtrace_interrupt_enable(cookie); 5582 return; 5583 } 5584 5585 if (panic_quiesce) { 5586 /* 5587 * We don't trace anything if we're panicking. 5588 */ 5589 dtrace_interrupt_enable(cookie); 5590 return; 5591 } 5592 5593 now = dtrace_gethrtime(); 5594 vtime = dtrace_vtime_references != 0; 5595 5596 if (vtime && curthread->t_dtrace_start) 5597 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 5598 5599 mstate.dtms_difo = NULL; 5600 mstate.dtms_probe = probe; 5601 mstate.dtms_strtok = NULL; 5602 mstate.dtms_arg[0] = arg0; 5603 mstate.dtms_arg[1] = arg1; 5604 mstate.dtms_arg[2] = arg2; 5605 mstate.dtms_arg[3] = arg3; 5606 mstate.dtms_arg[4] = arg4; 5607 5608 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 5609 5610 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 5611 dtrace_predicate_t *pred = ecb->dte_predicate; 5612 dtrace_state_t *state = ecb->dte_state; 5613 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 5614 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 5615 dtrace_vstate_t *vstate = &state->dts_vstate; 5616 dtrace_provider_t *prov = probe->dtpr_provider; 5617 int committed = 0; 5618 caddr_t tomax; 5619 5620 /* 5621 * A little subtlety with the following (seemingly innocuous) 5622 * declaration of the automatic 'val': by looking at the 5623 * code, you might think that it could be declared in the 5624 * action processing loop, below. (That is, it's only used in 5625 * the action processing loop.) However, it must be declared 5626 * out of that scope because in the case of DIF expression 5627 * arguments to aggregating actions, one iteration of the 5628 * action loop will use the last iteration's value. 5629 */ 5630 #ifdef lint 5631 uint64_t val = 0; 5632 #else 5633 uint64_t val; 5634 #endif 5635 5636 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 5637 *flags &= ~CPU_DTRACE_ERROR; 5638 5639 if (prov == dtrace_provider) { 5640 /* 5641 * If dtrace itself is the provider of this probe, 5642 * we're only going to continue processing the ECB if 5643 * arg0 (the dtrace_state_t) is equal to the ECB's 5644 * creating state. (This prevents disjoint consumers 5645 * from seeing one another's metaprobes.) 5646 */ 5647 if (arg0 != (uint64_t)(uintptr_t)state) 5648 continue; 5649 } 5650 5651 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 5652 /* 5653 * We're not currently active. If our provider isn't 5654 * the dtrace pseudo provider, we're not interested. 5655 */ 5656 if (prov != dtrace_provider) 5657 continue; 5658 5659 /* 5660 * Now we must further check if we are in the BEGIN 5661 * probe. If we are, we will only continue processing 5662 * if we're still in WARMUP -- if one BEGIN enabling 5663 * has invoked the exit() action, we don't want to 5664 * evaluate subsequent BEGIN enablings. 5665 */ 5666 if (probe->dtpr_id == dtrace_probeid_begin && 5667 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 5668 ASSERT(state->dts_activity == 5669 DTRACE_ACTIVITY_DRAINING); 5670 continue; 5671 } 5672 } 5673 5674 if (ecb->dte_cond) { 5675 /* 5676 * If the dte_cond bits indicate that this 5677 * consumer is only allowed to see user-mode firings 5678 * of this probe, call the provider's dtps_usermode() 5679 * entry point to check that the probe was fired 5680 * while in a user context. Skip this ECB if that's 5681 * not the case. 5682 */ 5683 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 5684 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 5685 probe->dtpr_id, probe->dtpr_arg) == 0) 5686 continue; 5687 5688 /* 5689 * This is more subtle than it looks. We have to be 5690 * absolutely certain that CRED() isn't going to 5691 * change out from under us so it's only legit to 5692 * examine that structure if we're in constrained 5693 * situations. Currently, the only times we'll this 5694 * check is if a non-super-user has enabled the 5695 * profile or syscall providers -- providers that 5696 * allow visibility of all processes. For the 5697 * profile case, the check above will ensure that 5698 * we're examining a user context. 5699 */ 5700 if (ecb->dte_cond & DTRACE_COND_OWNER) { 5701 cred_t *cr; 5702 cred_t *s_cr = 5703 ecb->dte_state->dts_cred.dcr_cred; 5704 proc_t *proc; 5705 5706 ASSERT(s_cr != NULL); 5707 5708 if ((cr = CRED()) == NULL || 5709 s_cr->cr_uid != cr->cr_uid || 5710 s_cr->cr_uid != cr->cr_ruid || 5711 s_cr->cr_uid != cr->cr_suid || 5712 s_cr->cr_gid != cr->cr_gid || 5713 s_cr->cr_gid != cr->cr_rgid || 5714 s_cr->cr_gid != cr->cr_sgid || 5715 (proc = ttoproc(curthread)) == NULL || 5716 (proc->p_flag & SNOCD)) 5717 continue; 5718 } 5719 5720 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 5721 cred_t *cr; 5722 cred_t *s_cr = 5723 ecb->dte_state->dts_cred.dcr_cred; 5724 5725 ASSERT(s_cr != NULL); 5726 5727 if ((cr = CRED()) == NULL || 5728 s_cr->cr_zone->zone_id != 5729 cr->cr_zone->zone_id) 5730 continue; 5731 } 5732 } 5733 5734 if (now - state->dts_alive > dtrace_deadman_timeout) { 5735 /* 5736 * We seem to be dead. Unless we (a) have kernel 5737 * destructive permissions (b) have expicitly enabled 5738 * destructive actions and (c) destructive actions have 5739 * not been disabled, we're going to transition into 5740 * the KILLED state, from which no further processing 5741 * on this state will be performed. 5742 */ 5743 if (!dtrace_priv_kernel_destructive(state) || 5744 !state->dts_cred.dcr_destructive || 5745 dtrace_destructive_disallow) { 5746 void *activity = &state->dts_activity; 5747 dtrace_activity_t current; 5748 5749 do { 5750 current = state->dts_activity; 5751 } while (dtrace_cas32(activity, current, 5752 DTRACE_ACTIVITY_KILLED) != current); 5753 5754 continue; 5755 } 5756 } 5757 5758 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 5759 ecb->dte_alignment, state, &mstate)) < 0) 5760 continue; 5761 5762 tomax = buf->dtb_tomax; 5763 ASSERT(tomax != NULL); 5764 5765 if (ecb->dte_size != 0) 5766 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 5767 5768 mstate.dtms_epid = ecb->dte_epid; 5769 mstate.dtms_present |= DTRACE_MSTATE_EPID; 5770 5771 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 5772 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 5773 else 5774 mstate.dtms_access = 0; 5775 5776 if (pred != NULL) { 5777 dtrace_difo_t *dp = pred->dtp_difo; 5778 int rval; 5779 5780 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 5781 5782 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 5783 dtrace_cacheid_t cid = probe->dtpr_predcache; 5784 5785 if (cid != DTRACE_CACHEIDNONE && !onintr) { 5786 /* 5787 * Update the predicate cache... 5788 */ 5789 ASSERT(cid == pred->dtp_cacheid); 5790 curthread->t_predcache = cid; 5791 } 5792 5793 continue; 5794 } 5795 } 5796 5797 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 5798 act != NULL; act = act->dta_next) { 5799 size_t valoffs; 5800 dtrace_difo_t *dp; 5801 dtrace_recdesc_t *rec = &act->dta_rec; 5802 5803 size = rec->dtrd_size; 5804 valoffs = offs + rec->dtrd_offset; 5805 5806 if (DTRACEACT_ISAGG(act->dta_kind)) { 5807 uint64_t v = 0xbad; 5808 dtrace_aggregation_t *agg; 5809 5810 agg = (dtrace_aggregation_t *)act; 5811 5812 if ((dp = act->dta_difo) != NULL) 5813 v = dtrace_dif_emulate(dp, 5814 &mstate, vstate, state); 5815 5816 if (*flags & CPU_DTRACE_ERROR) 5817 continue; 5818 5819 /* 5820 * Note that we always pass the expression 5821 * value from the previous iteration of the 5822 * action loop. This value will only be used 5823 * if there is an expression argument to the 5824 * aggregating action, denoted by the 5825 * dtag_hasarg field. 5826 */ 5827 dtrace_aggregate(agg, buf, 5828 offs, aggbuf, v, val); 5829 continue; 5830 } 5831 5832 switch (act->dta_kind) { 5833 case DTRACEACT_STOP: 5834 if (dtrace_priv_proc_destructive(state)) 5835 dtrace_action_stop(); 5836 continue; 5837 5838 case DTRACEACT_BREAKPOINT: 5839 if (dtrace_priv_kernel_destructive(state)) 5840 dtrace_action_breakpoint(ecb); 5841 continue; 5842 5843 case DTRACEACT_PANIC: 5844 if (dtrace_priv_kernel_destructive(state)) 5845 dtrace_action_panic(ecb); 5846 continue; 5847 5848 case DTRACEACT_STACK: 5849 if (!dtrace_priv_kernel(state)) 5850 continue; 5851 5852 dtrace_getpcstack((pc_t *)(tomax + valoffs), 5853 size / sizeof (pc_t), probe->dtpr_aframes, 5854 DTRACE_ANCHORED(probe) ? NULL : 5855 (uint32_t *)arg0); 5856 5857 continue; 5858 5859 case DTRACEACT_JSTACK: 5860 case DTRACEACT_USTACK: 5861 if (!dtrace_priv_proc(state)) 5862 continue; 5863 5864 /* 5865 * See comment in DIF_VAR_PID. 5866 */ 5867 if (DTRACE_ANCHORED(mstate.dtms_probe) && 5868 CPU_ON_INTR(CPU)) { 5869 int depth = DTRACE_USTACK_NFRAMES( 5870 rec->dtrd_arg) + 1; 5871 5872 dtrace_bzero((void *)(tomax + valoffs), 5873 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 5874 + depth * sizeof (uint64_t)); 5875 5876 continue; 5877 } 5878 5879 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 5880 curproc->p_dtrace_helpers != NULL) { 5881 /* 5882 * This is the slow path -- we have 5883 * allocated string space, and we're 5884 * getting the stack of a process that 5885 * has helpers. Call into a separate 5886 * routine to perform this processing. 5887 */ 5888 dtrace_action_ustack(&mstate, state, 5889 (uint64_t *)(tomax + valoffs), 5890 rec->dtrd_arg); 5891 continue; 5892 } 5893 5894 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5895 dtrace_getupcstack((uint64_t *) 5896 (tomax + valoffs), 5897 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 5898 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5899 continue; 5900 5901 default: 5902 break; 5903 } 5904 5905 dp = act->dta_difo; 5906 ASSERT(dp != NULL); 5907 5908 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 5909 5910 if (*flags & CPU_DTRACE_ERROR) 5911 continue; 5912 5913 switch (act->dta_kind) { 5914 case DTRACEACT_SPECULATE: 5915 ASSERT(buf == &state->dts_buffer[cpuid]); 5916 buf = dtrace_speculation_buffer(state, 5917 cpuid, val); 5918 5919 if (buf == NULL) { 5920 *flags |= CPU_DTRACE_DROP; 5921 continue; 5922 } 5923 5924 offs = dtrace_buffer_reserve(buf, 5925 ecb->dte_needed, ecb->dte_alignment, 5926 state, NULL); 5927 5928 if (offs < 0) { 5929 *flags |= CPU_DTRACE_DROP; 5930 continue; 5931 } 5932 5933 tomax = buf->dtb_tomax; 5934 ASSERT(tomax != NULL); 5935 5936 if (ecb->dte_size != 0) 5937 DTRACE_STORE(uint32_t, tomax, offs, 5938 ecb->dte_epid); 5939 continue; 5940 5941 case DTRACEACT_CHILL: 5942 if (dtrace_priv_kernel_destructive(state)) 5943 dtrace_action_chill(&mstate, val); 5944 continue; 5945 5946 case DTRACEACT_RAISE: 5947 if (dtrace_priv_proc_destructive(state)) 5948 dtrace_action_raise(val); 5949 continue; 5950 5951 case DTRACEACT_COMMIT: 5952 ASSERT(!committed); 5953 5954 /* 5955 * We need to commit our buffer state. 5956 */ 5957 if (ecb->dte_size) 5958 buf->dtb_offset = offs + ecb->dte_size; 5959 buf = &state->dts_buffer[cpuid]; 5960 dtrace_speculation_commit(state, cpuid, val); 5961 committed = 1; 5962 continue; 5963 5964 case DTRACEACT_DISCARD: 5965 dtrace_speculation_discard(state, cpuid, val); 5966 continue; 5967 5968 case DTRACEACT_DIFEXPR: 5969 case DTRACEACT_LIBACT: 5970 case DTRACEACT_PRINTF: 5971 case DTRACEACT_PRINTA: 5972 case DTRACEACT_SYSTEM: 5973 case DTRACEACT_FREOPEN: 5974 break; 5975 5976 case DTRACEACT_SYM: 5977 case DTRACEACT_MOD: 5978 if (!dtrace_priv_kernel(state)) 5979 continue; 5980 break; 5981 5982 case DTRACEACT_USYM: 5983 case DTRACEACT_UMOD: 5984 case DTRACEACT_UADDR: { 5985 struct pid *pid = curthread->t_procp->p_pidp; 5986 5987 if (!dtrace_priv_proc(state)) 5988 continue; 5989 5990 DTRACE_STORE(uint64_t, tomax, 5991 valoffs, (uint64_t)pid->pid_id); 5992 DTRACE_STORE(uint64_t, tomax, 5993 valoffs + sizeof (uint64_t), val); 5994 5995 continue; 5996 } 5997 5998 case DTRACEACT_EXIT: { 5999 /* 6000 * For the exit action, we are going to attempt 6001 * to atomically set our activity to be 6002 * draining. If this fails (either because 6003 * another CPU has beat us to the exit action, 6004 * or because our current activity is something 6005 * other than ACTIVE or WARMUP), we will 6006 * continue. This assures that the exit action 6007 * can be successfully recorded at most once 6008 * when we're in the ACTIVE state. If we're 6009 * encountering the exit() action while in 6010 * COOLDOWN, however, we want to honor the new 6011 * status code. (We know that we're the only 6012 * thread in COOLDOWN, so there is no race.) 6013 */ 6014 void *activity = &state->dts_activity; 6015 dtrace_activity_t current = state->dts_activity; 6016 6017 if (current == DTRACE_ACTIVITY_COOLDOWN) 6018 break; 6019 6020 if (current != DTRACE_ACTIVITY_WARMUP) 6021 current = DTRACE_ACTIVITY_ACTIVE; 6022 6023 if (dtrace_cas32(activity, current, 6024 DTRACE_ACTIVITY_DRAINING) != current) { 6025 *flags |= CPU_DTRACE_DROP; 6026 continue; 6027 } 6028 6029 break; 6030 } 6031 6032 default: 6033 ASSERT(0); 6034 } 6035 6036 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 6037 uintptr_t end = valoffs + size; 6038 6039 if (!dtrace_vcanload((void *)(uintptr_t)val, 6040 &dp->dtdo_rtype, &mstate, vstate)) 6041 continue; 6042 6043 /* 6044 * If this is a string, we're going to only 6045 * load until we find the zero byte -- after 6046 * which we'll store zero bytes. 6047 */ 6048 if (dp->dtdo_rtype.dtdt_kind == 6049 DIF_TYPE_STRING) { 6050 char c = '\0' + 1; 6051 int intuple = act->dta_intuple; 6052 size_t s; 6053 6054 for (s = 0; s < size; s++) { 6055 if (c != '\0') 6056 c = dtrace_load8(val++); 6057 6058 DTRACE_STORE(uint8_t, tomax, 6059 valoffs++, c); 6060 6061 if (c == '\0' && intuple) 6062 break; 6063 } 6064 6065 continue; 6066 } 6067 6068 while (valoffs < end) { 6069 DTRACE_STORE(uint8_t, tomax, valoffs++, 6070 dtrace_load8(val++)); 6071 } 6072 6073 continue; 6074 } 6075 6076 switch (size) { 6077 case 0: 6078 break; 6079 6080 case sizeof (uint8_t): 6081 DTRACE_STORE(uint8_t, tomax, valoffs, val); 6082 break; 6083 case sizeof (uint16_t): 6084 DTRACE_STORE(uint16_t, tomax, valoffs, val); 6085 break; 6086 case sizeof (uint32_t): 6087 DTRACE_STORE(uint32_t, tomax, valoffs, val); 6088 break; 6089 case sizeof (uint64_t): 6090 DTRACE_STORE(uint64_t, tomax, valoffs, val); 6091 break; 6092 default: 6093 /* 6094 * Any other size should have been returned by 6095 * reference, not by value. 6096 */ 6097 ASSERT(0); 6098 break; 6099 } 6100 } 6101 6102 if (*flags & CPU_DTRACE_DROP) 6103 continue; 6104 6105 if (*flags & CPU_DTRACE_FAULT) { 6106 int ndx; 6107 dtrace_action_t *err; 6108 6109 buf->dtb_errors++; 6110 6111 if (probe->dtpr_id == dtrace_probeid_error) { 6112 /* 6113 * There's nothing we can do -- we had an 6114 * error on the error probe. We bump an 6115 * error counter to at least indicate that 6116 * this condition happened. 6117 */ 6118 dtrace_error(&state->dts_dblerrors); 6119 continue; 6120 } 6121 6122 if (vtime) { 6123 /* 6124 * Before recursing on dtrace_probe(), we 6125 * need to explicitly clear out our start 6126 * time to prevent it from being accumulated 6127 * into t_dtrace_vtime. 6128 */ 6129 curthread->t_dtrace_start = 0; 6130 } 6131 6132 /* 6133 * Iterate over the actions to figure out which action 6134 * we were processing when we experienced the error. 6135 * Note that act points _past_ the faulting action; if 6136 * act is ecb->dte_action, the fault was in the 6137 * predicate, if it's ecb->dte_action->dta_next it's 6138 * in action #1, and so on. 6139 */ 6140 for (err = ecb->dte_action, ndx = 0; 6141 err != act; err = err->dta_next, ndx++) 6142 continue; 6143 6144 dtrace_probe_error(state, ecb->dte_epid, ndx, 6145 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 6146 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 6147 cpu_core[cpuid].cpuc_dtrace_illval); 6148 6149 continue; 6150 } 6151 6152 if (!committed) 6153 buf->dtb_offset = offs + ecb->dte_size; 6154 } 6155 6156 if (vtime) 6157 curthread->t_dtrace_start = dtrace_gethrtime(); 6158 6159 dtrace_interrupt_enable(cookie); 6160 } 6161 6162 /* 6163 * DTrace Probe Hashing Functions 6164 * 6165 * The functions in this section (and indeed, the functions in remaining 6166 * sections) are not _called_ from probe context. (Any exceptions to this are 6167 * marked with a "Note:".) Rather, they are called from elsewhere in the 6168 * DTrace framework to look-up probes in, add probes to and remove probes from 6169 * the DTrace probe hashes. (Each probe is hashed by each element of the 6170 * probe tuple -- allowing for fast lookups, regardless of what was 6171 * specified.) 6172 */ 6173 static uint_t 6174 dtrace_hash_str(char *p) 6175 { 6176 unsigned int g; 6177 uint_t hval = 0; 6178 6179 while (*p) { 6180 hval = (hval << 4) + *p++; 6181 if ((g = (hval & 0xf0000000)) != 0) 6182 hval ^= g >> 24; 6183 hval &= ~g; 6184 } 6185 return (hval); 6186 } 6187 6188 static dtrace_hash_t * 6189 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 6190 { 6191 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 6192 6193 hash->dth_stroffs = stroffs; 6194 hash->dth_nextoffs = nextoffs; 6195 hash->dth_prevoffs = prevoffs; 6196 6197 hash->dth_size = 1; 6198 hash->dth_mask = hash->dth_size - 1; 6199 6200 hash->dth_tab = kmem_zalloc(hash->dth_size * 6201 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 6202 6203 return (hash); 6204 } 6205 6206 static void 6207 dtrace_hash_destroy(dtrace_hash_t *hash) 6208 { 6209 #ifdef DEBUG 6210 int i; 6211 6212 for (i = 0; i < hash->dth_size; i++) 6213 ASSERT(hash->dth_tab[i] == NULL); 6214 #endif 6215 6216 kmem_free(hash->dth_tab, 6217 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 6218 kmem_free(hash, sizeof (dtrace_hash_t)); 6219 } 6220 6221 static void 6222 dtrace_hash_resize(dtrace_hash_t *hash) 6223 { 6224 int size = hash->dth_size, i, ndx; 6225 int new_size = hash->dth_size << 1; 6226 int new_mask = new_size - 1; 6227 dtrace_hashbucket_t **new_tab, *bucket, *next; 6228 6229 ASSERT((new_size & new_mask) == 0); 6230 6231 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 6232 6233 for (i = 0; i < size; i++) { 6234 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 6235 dtrace_probe_t *probe = bucket->dthb_chain; 6236 6237 ASSERT(probe != NULL); 6238 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 6239 6240 next = bucket->dthb_next; 6241 bucket->dthb_next = new_tab[ndx]; 6242 new_tab[ndx] = bucket; 6243 } 6244 } 6245 6246 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 6247 hash->dth_tab = new_tab; 6248 hash->dth_size = new_size; 6249 hash->dth_mask = new_mask; 6250 } 6251 6252 static void 6253 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 6254 { 6255 int hashval = DTRACE_HASHSTR(hash, new); 6256 int ndx = hashval & hash->dth_mask; 6257 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6258 dtrace_probe_t **nextp, **prevp; 6259 6260 for (; bucket != NULL; bucket = bucket->dthb_next) { 6261 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 6262 goto add; 6263 } 6264 6265 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 6266 dtrace_hash_resize(hash); 6267 dtrace_hash_add(hash, new); 6268 return; 6269 } 6270 6271 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 6272 bucket->dthb_next = hash->dth_tab[ndx]; 6273 hash->dth_tab[ndx] = bucket; 6274 hash->dth_nbuckets++; 6275 6276 add: 6277 nextp = DTRACE_HASHNEXT(hash, new); 6278 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 6279 *nextp = bucket->dthb_chain; 6280 6281 if (bucket->dthb_chain != NULL) { 6282 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 6283 ASSERT(*prevp == NULL); 6284 *prevp = new; 6285 } 6286 6287 bucket->dthb_chain = new; 6288 bucket->dthb_len++; 6289 } 6290 6291 static dtrace_probe_t * 6292 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 6293 { 6294 int hashval = DTRACE_HASHSTR(hash, template); 6295 int ndx = hashval & hash->dth_mask; 6296 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6297 6298 for (; bucket != NULL; bucket = bucket->dthb_next) { 6299 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6300 return (bucket->dthb_chain); 6301 } 6302 6303 return (NULL); 6304 } 6305 6306 static int 6307 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 6308 { 6309 int hashval = DTRACE_HASHSTR(hash, template); 6310 int ndx = hashval & hash->dth_mask; 6311 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6312 6313 for (; bucket != NULL; bucket = bucket->dthb_next) { 6314 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6315 return (bucket->dthb_len); 6316 } 6317 6318 return (NULL); 6319 } 6320 6321 static void 6322 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 6323 { 6324 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 6325 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6326 6327 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 6328 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 6329 6330 /* 6331 * Find the bucket that we're removing this probe from. 6332 */ 6333 for (; bucket != NULL; bucket = bucket->dthb_next) { 6334 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 6335 break; 6336 } 6337 6338 ASSERT(bucket != NULL); 6339 6340 if (*prevp == NULL) { 6341 if (*nextp == NULL) { 6342 /* 6343 * The removed probe was the only probe on this 6344 * bucket; we need to remove the bucket. 6345 */ 6346 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 6347 6348 ASSERT(bucket->dthb_chain == probe); 6349 ASSERT(b != NULL); 6350 6351 if (b == bucket) { 6352 hash->dth_tab[ndx] = bucket->dthb_next; 6353 } else { 6354 while (b->dthb_next != bucket) 6355 b = b->dthb_next; 6356 b->dthb_next = bucket->dthb_next; 6357 } 6358 6359 ASSERT(hash->dth_nbuckets > 0); 6360 hash->dth_nbuckets--; 6361 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 6362 return; 6363 } 6364 6365 bucket->dthb_chain = *nextp; 6366 } else { 6367 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 6368 } 6369 6370 if (*nextp != NULL) 6371 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 6372 } 6373 6374 /* 6375 * DTrace Utility Functions 6376 * 6377 * These are random utility functions that are _not_ called from probe context. 6378 */ 6379 static int 6380 dtrace_badattr(const dtrace_attribute_t *a) 6381 { 6382 return (a->dtat_name > DTRACE_STABILITY_MAX || 6383 a->dtat_data > DTRACE_STABILITY_MAX || 6384 a->dtat_class > DTRACE_CLASS_MAX); 6385 } 6386 6387 /* 6388 * Return a duplicate copy of a string. If the specified string is NULL, 6389 * this function returns a zero-length string. 6390 */ 6391 static char * 6392 dtrace_strdup(const char *str) 6393 { 6394 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 6395 6396 if (str != NULL) 6397 (void) strcpy(new, str); 6398 6399 return (new); 6400 } 6401 6402 #define DTRACE_ISALPHA(c) \ 6403 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 6404 6405 static int 6406 dtrace_badname(const char *s) 6407 { 6408 char c; 6409 6410 if (s == NULL || (c = *s++) == '\0') 6411 return (0); 6412 6413 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 6414 return (1); 6415 6416 while ((c = *s++) != '\0') { 6417 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 6418 c != '-' && c != '_' && c != '.' && c != '`') 6419 return (1); 6420 } 6421 6422 return (0); 6423 } 6424 6425 static void 6426 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 6427 { 6428 uint32_t priv; 6429 6430 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 6431 /* 6432 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 6433 */ 6434 priv = DTRACE_PRIV_ALL; 6435 } else { 6436 *uidp = crgetuid(cr); 6437 *zoneidp = crgetzoneid(cr); 6438 6439 priv = 0; 6440 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 6441 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 6442 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 6443 priv |= DTRACE_PRIV_USER; 6444 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 6445 priv |= DTRACE_PRIV_PROC; 6446 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 6447 priv |= DTRACE_PRIV_OWNER; 6448 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 6449 priv |= DTRACE_PRIV_ZONEOWNER; 6450 } 6451 6452 *privp = priv; 6453 } 6454 6455 #ifdef DTRACE_ERRDEBUG 6456 static void 6457 dtrace_errdebug(const char *str) 6458 { 6459 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; 6460 int occupied = 0; 6461 6462 mutex_enter(&dtrace_errlock); 6463 dtrace_errlast = str; 6464 dtrace_errthread = curthread; 6465 6466 while (occupied++ < DTRACE_ERRHASHSZ) { 6467 if (dtrace_errhash[hval].dter_msg == str) { 6468 dtrace_errhash[hval].dter_count++; 6469 goto out; 6470 } 6471 6472 if (dtrace_errhash[hval].dter_msg != NULL) { 6473 hval = (hval + 1) % DTRACE_ERRHASHSZ; 6474 continue; 6475 } 6476 6477 dtrace_errhash[hval].dter_msg = str; 6478 dtrace_errhash[hval].dter_count = 1; 6479 goto out; 6480 } 6481 6482 panic("dtrace: undersized error hash"); 6483 out: 6484 mutex_exit(&dtrace_errlock); 6485 } 6486 #endif 6487 6488 /* 6489 * DTrace Matching Functions 6490 * 6491 * These functions are used to match groups of probes, given some elements of 6492 * a probe tuple, or some globbed expressions for elements of a probe tuple. 6493 */ 6494 static int 6495 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 6496 zoneid_t zoneid) 6497 { 6498 if (priv != DTRACE_PRIV_ALL) { 6499 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 6500 uint32_t match = priv & ppriv; 6501 6502 /* 6503 * No PRIV_DTRACE_* privileges... 6504 */ 6505 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 6506 DTRACE_PRIV_KERNEL)) == 0) 6507 return (0); 6508 6509 /* 6510 * No matching bits, but there were bits to match... 6511 */ 6512 if (match == 0 && ppriv != 0) 6513 return (0); 6514 6515 /* 6516 * Need to have permissions to the process, but don't... 6517 */ 6518 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 6519 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 6520 return (0); 6521 } 6522 6523 /* 6524 * Need to be in the same zone unless we possess the 6525 * privilege to examine all zones. 6526 */ 6527 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 6528 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 6529 return (0); 6530 } 6531 } 6532 6533 return (1); 6534 } 6535 6536 /* 6537 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 6538 * consists of input pattern strings and an ops-vector to evaluate them. 6539 * This function returns >0 for match, 0 for no match, and <0 for error. 6540 */ 6541 static int 6542 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 6543 uint32_t priv, uid_t uid, zoneid_t zoneid) 6544 { 6545 dtrace_provider_t *pvp = prp->dtpr_provider; 6546 int rv; 6547 6548 if (pvp->dtpv_defunct) 6549 return (0); 6550 6551 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 6552 return (rv); 6553 6554 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 6555 return (rv); 6556 6557 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 6558 return (rv); 6559 6560 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 6561 return (rv); 6562 6563 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 6564 return (0); 6565 6566 return (rv); 6567 } 6568 6569 /* 6570 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 6571 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 6572 * libc's version, the kernel version only applies to 8-bit ASCII strings. 6573 * In addition, all of the recursion cases except for '*' matching have been 6574 * unwound. For '*', we still implement recursive evaluation, but a depth 6575 * counter is maintained and matching is aborted if we recurse too deep. 6576 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 6577 */ 6578 static int 6579 dtrace_match_glob(const char *s, const char *p, int depth) 6580 { 6581 const char *olds; 6582 char s1, c; 6583 int gs; 6584 6585 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 6586 return (-1); 6587 6588 if (s == NULL) 6589 s = ""; /* treat NULL as empty string */ 6590 6591 top: 6592 olds = s; 6593 s1 = *s++; 6594 6595 if (p == NULL) 6596 return (0); 6597 6598 if ((c = *p++) == '\0') 6599 return (s1 == '\0'); 6600 6601 switch (c) { 6602 case '[': { 6603 int ok = 0, notflag = 0; 6604 char lc = '\0'; 6605 6606 if (s1 == '\0') 6607 return (0); 6608 6609 if (*p == '!') { 6610 notflag = 1; 6611 p++; 6612 } 6613 6614 if ((c = *p++) == '\0') 6615 return (0); 6616 6617 do { 6618 if (c == '-' && lc != '\0' && *p != ']') { 6619 if ((c = *p++) == '\0') 6620 return (0); 6621 if (c == '\\' && (c = *p++) == '\0') 6622 return (0); 6623 6624 if (notflag) { 6625 if (s1 < lc || s1 > c) 6626 ok++; 6627 else 6628 return (0); 6629 } else if (lc <= s1 && s1 <= c) 6630 ok++; 6631 6632 } else if (c == '\\' && (c = *p++) == '\0') 6633 return (0); 6634 6635 lc = c; /* save left-hand 'c' for next iteration */ 6636 6637 if (notflag) { 6638 if (s1 != c) 6639 ok++; 6640 else 6641 return (0); 6642 } else if (s1 == c) 6643 ok++; 6644 6645 if ((c = *p++) == '\0') 6646 return (0); 6647 6648 } while (c != ']'); 6649 6650 if (ok) 6651 goto top; 6652 6653 return (0); 6654 } 6655 6656 case '\\': 6657 if ((c = *p++) == '\0') 6658 return (0); 6659 /*FALLTHRU*/ 6660 6661 default: 6662 if (c != s1) 6663 return (0); 6664 /*FALLTHRU*/ 6665 6666 case '?': 6667 if (s1 != '\0') 6668 goto top; 6669 return (0); 6670 6671 case '*': 6672 while (*p == '*') 6673 p++; /* consecutive *'s are identical to a single one */ 6674 6675 if (*p == '\0') 6676 return (1); 6677 6678 for (s = olds; *s != '\0'; s++) { 6679 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 6680 return (gs); 6681 } 6682 6683 return (0); 6684 } 6685 } 6686 6687 /*ARGSUSED*/ 6688 static int 6689 dtrace_match_string(const char *s, const char *p, int depth) 6690 { 6691 return (s != NULL && strcmp(s, p) == 0); 6692 } 6693 6694 /*ARGSUSED*/ 6695 static int 6696 dtrace_match_nul(const char *s, const char *p, int depth) 6697 { 6698 return (1); /* always match the empty pattern */ 6699 } 6700 6701 /*ARGSUSED*/ 6702 static int 6703 dtrace_match_nonzero(const char *s, const char *p, int depth) 6704 { 6705 return (s != NULL && s[0] != '\0'); 6706 } 6707 6708 static int 6709 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 6710 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 6711 { 6712 dtrace_probe_t template, *probe; 6713 dtrace_hash_t *hash = NULL; 6714 int len, rc, best = INT_MAX, nmatched = 0; 6715 dtrace_id_t i; 6716 6717 ASSERT(MUTEX_HELD(&dtrace_lock)); 6718 6719 /* 6720 * If the probe ID is specified in the key, just lookup by ID and 6721 * invoke the match callback once if a matching probe is found. 6722 */ 6723 if (pkp->dtpk_id != DTRACE_IDNONE) { 6724 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 6725 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 6726 if ((*matched)(probe, arg) == DTRACE_MATCH_FAIL) 6727 return (DTRACE_MATCH_FAIL); 6728 nmatched++; 6729 } 6730 return (nmatched); 6731 } 6732 6733 template.dtpr_mod = (char *)pkp->dtpk_mod; 6734 template.dtpr_func = (char *)pkp->dtpk_func; 6735 template.dtpr_name = (char *)pkp->dtpk_name; 6736 6737 /* 6738 * We want to find the most distinct of the module name, function 6739 * name, and name. So for each one that is not a glob pattern or 6740 * empty string, we perform a lookup in the corresponding hash and 6741 * use the hash table with the fewest collisions to do our search. 6742 */ 6743 if (pkp->dtpk_mmatch == &dtrace_match_string && 6744 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 6745 best = len; 6746 hash = dtrace_bymod; 6747 } 6748 6749 if (pkp->dtpk_fmatch == &dtrace_match_string && 6750 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 6751 best = len; 6752 hash = dtrace_byfunc; 6753 } 6754 6755 if (pkp->dtpk_nmatch == &dtrace_match_string && 6756 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 6757 best = len; 6758 hash = dtrace_byname; 6759 } 6760 6761 /* 6762 * If we did not select a hash table, iterate over every probe and 6763 * invoke our callback for each one that matches our input probe key. 6764 */ 6765 if (hash == NULL) { 6766 for (i = 0; i < dtrace_nprobes; i++) { 6767 if ((probe = dtrace_probes[i]) == NULL || 6768 dtrace_match_probe(probe, pkp, priv, uid, 6769 zoneid) <= 0) 6770 continue; 6771 6772 nmatched++; 6773 6774 if ((rc = (*matched)(probe, arg)) != 6775 DTRACE_MATCH_NEXT) { 6776 if (rc == DTRACE_MATCH_FAIL) 6777 return (DTRACE_MATCH_FAIL); 6778 break; 6779 } 6780 } 6781 6782 return (nmatched); 6783 } 6784 6785 /* 6786 * If we selected a hash table, iterate over each probe of the same key 6787 * name and invoke the callback for every probe that matches the other 6788 * attributes of our input probe key. 6789 */ 6790 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 6791 probe = *(DTRACE_HASHNEXT(hash, probe))) { 6792 6793 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 6794 continue; 6795 6796 nmatched++; 6797 6798 if ((rc = (*matched)(probe, arg)) != DTRACE_MATCH_NEXT) { 6799 if (rc == DTRACE_MATCH_FAIL) 6800 return (DTRACE_MATCH_FAIL); 6801 break; 6802 } 6803 } 6804 6805 return (nmatched); 6806 } 6807 6808 /* 6809 * Return the function pointer dtrace_probecmp() should use to compare the 6810 * specified pattern with a string. For NULL or empty patterns, we select 6811 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 6812 * For non-empty non-glob strings, we use dtrace_match_string(). 6813 */ 6814 static dtrace_probekey_f * 6815 dtrace_probekey_func(const char *p) 6816 { 6817 char c; 6818 6819 if (p == NULL || *p == '\0') 6820 return (&dtrace_match_nul); 6821 6822 while ((c = *p++) != '\0') { 6823 if (c == '[' || c == '?' || c == '*' || c == '\\') 6824 return (&dtrace_match_glob); 6825 } 6826 6827 return (&dtrace_match_string); 6828 } 6829 6830 /* 6831 * Build a probe comparison key for use with dtrace_match_probe() from the 6832 * given probe description. By convention, a null key only matches anchored 6833 * probes: if each field is the empty string, reset dtpk_fmatch to 6834 * dtrace_match_nonzero(). 6835 */ 6836 static void 6837 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 6838 { 6839 pkp->dtpk_prov = pdp->dtpd_provider; 6840 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 6841 6842 pkp->dtpk_mod = pdp->dtpd_mod; 6843 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 6844 6845 pkp->dtpk_func = pdp->dtpd_func; 6846 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 6847 6848 pkp->dtpk_name = pdp->dtpd_name; 6849 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 6850 6851 pkp->dtpk_id = pdp->dtpd_id; 6852 6853 if (pkp->dtpk_id == DTRACE_IDNONE && 6854 pkp->dtpk_pmatch == &dtrace_match_nul && 6855 pkp->dtpk_mmatch == &dtrace_match_nul && 6856 pkp->dtpk_fmatch == &dtrace_match_nul && 6857 pkp->dtpk_nmatch == &dtrace_match_nul) 6858 pkp->dtpk_fmatch = &dtrace_match_nonzero; 6859 } 6860 6861 /* 6862 * DTrace Provider-to-Framework API Functions 6863 * 6864 * These functions implement much of the Provider-to-Framework API, as 6865 * described in <sys/dtrace.h>. The parts of the API not in this section are 6866 * the functions in the API for probe management (found below), and 6867 * dtrace_probe() itself (found above). 6868 */ 6869 6870 /* 6871 * Register the calling provider with the DTrace framework. This should 6872 * generally be called by DTrace providers in their attach(9E) entry point. 6873 */ 6874 int 6875 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 6876 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 6877 { 6878 dtrace_provider_t *provider; 6879 6880 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 6881 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 6882 "arguments", name ? name : "<NULL>"); 6883 return (EINVAL); 6884 } 6885 6886 if (name[0] == '\0' || dtrace_badname(name)) { 6887 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 6888 "provider name", name); 6889 return (EINVAL); 6890 } 6891 6892 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 6893 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 6894 pops->dtps_destroy == NULL || 6895 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 6896 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 6897 "provider ops", name); 6898 return (EINVAL); 6899 } 6900 6901 if (dtrace_badattr(&pap->dtpa_provider) || 6902 dtrace_badattr(&pap->dtpa_mod) || 6903 dtrace_badattr(&pap->dtpa_func) || 6904 dtrace_badattr(&pap->dtpa_name) || 6905 dtrace_badattr(&pap->dtpa_args)) { 6906 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 6907 "provider attributes", name); 6908 return (EINVAL); 6909 } 6910 6911 if (priv & ~DTRACE_PRIV_ALL) { 6912 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 6913 "privilege attributes", name); 6914 return (EINVAL); 6915 } 6916 6917 if ((priv & DTRACE_PRIV_KERNEL) && 6918 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 6919 pops->dtps_usermode == NULL) { 6920 cmn_err(CE_WARN, "failed to register provider '%s': need " 6921 "dtps_usermode() op for given privilege attributes", name); 6922 return (EINVAL); 6923 } 6924 6925 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 6926 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 6927 (void) strcpy(provider->dtpv_name, name); 6928 6929 provider->dtpv_attr = *pap; 6930 provider->dtpv_priv.dtpp_flags = priv; 6931 if (cr != NULL) { 6932 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 6933 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 6934 } 6935 provider->dtpv_pops = *pops; 6936 6937 if (pops->dtps_provide == NULL) { 6938 ASSERT(pops->dtps_provide_module != NULL); 6939 provider->dtpv_pops.dtps_provide = 6940 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 6941 } 6942 6943 if (pops->dtps_provide_module == NULL) { 6944 ASSERT(pops->dtps_provide != NULL); 6945 provider->dtpv_pops.dtps_provide_module = 6946 (void (*)(void *, struct modctl *))dtrace_nullop; 6947 } 6948 6949 if (pops->dtps_suspend == NULL) { 6950 ASSERT(pops->dtps_resume == NULL); 6951 provider->dtpv_pops.dtps_suspend = 6952 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 6953 provider->dtpv_pops.dtps_resume = 6954 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 6955 } 6956 6957 provider->dtpv_arg = arg; 6958 *idp = (dtrace_provider_id_t)provider; 6959 6960 if (pops == &dtrace_provider_ops) { 6961 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 6962 ASSERT(MUTEX_HELD(&dtrace_lock)); 6963 ASSERT(dtrace_anon.dta_enabling == NULL); 6964 6965 /* 6966 * We make sure that the DTrace provider is at the head of 6967 * the provider chain. 6968 */ 6969 provider->dtpv_next = dtrace_provider; 6970 dtrace_provider = provider; 6971 return (0); 6972 } 6973 6974 mutex_enter(&dtrace_provider_lock); 6975 mutex_enter(&dtrace_lock); 6976 6977 /* 6978 * If there is at least one provider registered, we'll add this 6979 * provider after the first provider. 6980 */ 6981 if (dtrace_provider != NULL) { 6982 provider->dtpv_next = dtrace_provider->dtpv_next; 6983 dtrace_provider->dtpv_next = provider; 6984 } else { 6985 dtrace_provider = provider; 6986 } 6987 6988 if (dtrace_retained != NULL) { 6989 dtrace_enabling_provide(provider); 6990 6991 /* 6992 * Now we need to call dtrace_enabling_matchall() -- which 6993 * will acquire cpu_lock and dtrace_lock. We therefore need 6994 * to drop all of our locks before calling into it... 6995 */ 6996 mutex_exit(&dtrace_lock); 6997 mutex_exit(&dtrace_provider_lock); 6998 dtrace_enabling_matchall(); 6999 7000 return (0); 7001 } 7002 7003 mutex_exit(&dtrace_lock); 7004 mutex_exit(&dtrace_provider_lock); 7005 7006 return (0); 7007 } 7008 7009 /* 7010 * Unregister the specified provider from the DTrace framework. This should 7011 * generally be called by DTrace providers in their detach(9E) entry point. 7012 */ 7013 int 7014 dtrace_unregister(dtrace_provider_id_t id) 7015 { 7016 dtrace_provider_t *old = (dtrace_provider_t *)id; 7017 dtrace_provider_t *prev = NULL; 7018 int i, self = 0; 7019 dtrace_probe_t *probe, *first = NULL; 7020 7021 if (old->dtpv_pops.dtps_enable == 7022 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop) { 7023 /* 7024 * If DTrace itself is the provider, we're called with locks 7025 * already held. 7026 */ 7027 ASSERT(old == dtrace_provider); 7028 ASSERT(dtrace_devi != NULL); 7029 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7030 ASSERT(MUTEX_HELD(&dtrace_lock)); 7031 self = 1; 7032 7033 if (dtrace_provider->dtpv_next != NULL) { 7034 /* 7035 * There's another provider here; return failure. 7036 */ 7037 return (EBUSY); 7038 } 7039 } else { 7040 mutex_enter(&dtrace_provider_lock); 7041 mutex_enter(&mod_lock); 7042 mutex_enter(&dtrace_lock); 7043 } 7044 7045 /* 7046 * If anyone has /dev/dtrace open, or if there are anonymous enabled 7047 * probes, we refuse to let providers slither away, unless this 7048 * provider has already been explicitly invalidated. 7049 */ 7050 if (!old->dtpv_defunct && 7051 (dtrace_opens || (dtrace_anon.dta_state != NULL && 7052 dtrace_anon.dta_state->dts_necbs > 0))) { 7053 if (!self) { 7054 mutex_exit(&dtrace_lock); 7055 mutex_exit(&mod_lock); 7056 mutex_exit(&dtrace_provider_lock); 7057 } 7058 return (EBUSY); 7059 } 7060 7061 /* 7062 * Attempt to destroy the probes associated with this provider. 7063 */ 7064 for (i = 0; i < dtrace_nprobes; i++) { 7065 if ((probe = dtrace_probes[i]) == NULL) 7066 continue; 7067 7068 if (probe->dtpr_provider != old) 7069 continue; 7070 7071 if (probe->dtpr_ecb == NULL) 7072 continue; 7073 7074 /* 7075 * We have at least one ECB; we can't remove this provider. 7076 */ 7077 if (!self) { 7078 mutex_exit(&dtrace_lock); 7079 mutex_exit(&mod_lock); 7080 mutex_exit(&dtrace_provider_lock); 7081 } 7082 return (EBUSY); 7083 } 7084 7085 /* 7086 * All of the probes for this provider are disabled; we can safely 7087 * remove all of them from their hash chains and from the probe array. 7088 */ 7089 for (i = 0; i < dtrace_nprobes; i++) { 7090 if ((probe = dtrace_probes[i]) == NULL) 7091 continue; 7092 7093 if (probe->dtpr_provider != old) 7094 continue; 7095 7096 dtrace_probes[i] = NULL; 7097 7098 dtrace_hash_remove(dtrace_bymod, probe); 7099 dtrace_hash_remove(dtrace_byfunc, probe); 7100 dtrace_hash_remove(dtrace_byname, probe); 7101 7102 if (first == NULL) { 7103 first = probe; 7104 probe->dtpr_nextmod = NULL; 7105 } else { 7106 probe->dtpr_nextmod = first; 7107 first = probe; 7108 } 7109 } 7110 7111 /* 7112 * The provider's probes have been removed from the hash chains and 7113 * from the probe array. Now issue a dtrace_sync() to be sure that 7114 * everyone has cleared out from any probe array processing. 7115 */ 7116 dtrace_sync(); 7117 7118 for (probe = first; probe != NULL; probe = first) { 7119 first = probe->dtpr_nextmod; 7120 7121 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 7122 probe->dtpr_arg); 7123 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7124 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7125 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7126 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 7127 kmem_free(probe, sizeof (dtrace_probe_t)); 7128 } 7129 7130 if ((prev = dtrace_provider) == old) { 7131 ASSERT(self || dtrace_devi == NULL); 7132 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 7133 dtrace_provider = old->dtpv_next; 7134 } else { 7135 while (prev != NULL && prev->dtpv_next != old) 7136 prev = prev->dtpv_next; 7137 7138 if (prev == NULL) { 7139 panic("attempt to unregister non-existent " 7140 "dtrace provider %p\n", (void *)id); 7141 } 7142 7143 prev->dtpv_next = old->dtpv_next; 7144 } 7145 7146 if (!self) { 7147 mutex_exit(&dtrace_lock); 7148 mutex_exit(&mod_lock); 7149 mutex_exit(&dtrace_provider_lock); 7150 } 7151 7152 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 7153 kmem_free(old, sizeof (dtrace_provider_t)); 7154 7155 return (0); 7156 } 7157 7158 /* 7159 * Invalidate the specified provider. All subsequent probe lookups for the 7160 * specified provider will fail, but its probes will not be removed. 7161 */ 7162 void 7163 dtrace_invalidate(dtrace_provider_id_t id) 7164 { 7165 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 7166 7167 ASSERT(pvp->dtpv_pops.dtps_enable != 7168 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7169 7170 mutex_enter(&dtrace_provider_lock); 7171 mutex_enter(&dtrace_lock); 7172 7173 pvp->dtpv_defunct = 1; 7174 7175 mutex_exit(&dtrace_lock); 7176 mutex_exit(&dtrace_provider_lock); 7177 } 7178 7179 /* 7180 * Indicate whether or not DTrace has attached. 7181 */ 7182 int 7183 dtrace_attached(void) 7184 { 7185 /* 7186 * dtrace_provider will be non-NULL iff the DTrace driver has 7187 * attached. (It's non-NULL because DTrace is always itself a 7188 * provider.) 7189 */ 7190 return (dtrace_provider != NULL); 7191 } 7192 7193 /* 7194 * Remove all the unenabled probes for the given provider. This function is 7195 * not unlike dtrace_unregister(), except that it doesn't remove the provider 7196 * -- just as many of its associated probes as it can. 7197 */ 7198 int 7199 dtrace_condense(dtrace_provider_id_t id) 7200 { 7201 dtrace_provider_t *prov = (dtrace_provider_t *)id; 7202 int i; 7203 dtrace_probe_t *probe; 7204 7205 /* 7206 * Make sure this isn't the dtrace provider itself. 7207 */ 7208 ASSERT(prov->dtpv_pops.dtps_enable != 7209 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7210 7211 mutex_enter(&dtrace_provider_lock); 7212 mutex_enter(&dtrace_lock); 7213 7214 /* 7215 * Attempt to destroy the probes associated with this provider. 7216 */ 7217 for (i = 0; i < dtrace_nprobes; i++) { 7218 if ((probe = dtrace_probes[i]) == NULL) 7219 continue; 7220 7221 if (probe->dtpr_provider != prov) 7222 continue; 7223 7224 if (probe->dtpr_ecb != NULL) 7225 continue; 7226 7227 dtrace_probes[i] = NULL; 7228 7229 dtrace_hash_remove(dtrace_bymod, probe); 7230 dtrace_hash_remove(dtrace_byfunc, probe); 7231 dtrace_hash_remove(dtrace_byname, probe); 7232 7233 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 7234 probe->dtpr_arg); 7235 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7236 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7237 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7238 kmem_free(probe, sizeof (dtrace_probe_t)); 7239 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 7240 } 7241 7242 mutex_exit(&dtrace_lock); 7243 mutex_exit(&dtrace_provider_lock); 7244 7245 return (0); 7246 } 7247 7248 /* 7249 * DTrace Probe Management Functions 7250 * 7251 * The functions in this section perform the DTrace probe management, 7252 * including functions to create probes, look-up probes, and call into the 7253 * providers to request that probes be provided. Some of these functions are 7254 * in the Provider-to-Framework API; these functions can be identified by the 7255 * fact that they are not declared "static". 7256 */ 7257 7258 /* 7259 * Create a probe with the specified module name, function name, and name. 7260 */ 7261 dtrace_id_t 7262 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 7263 const char *func, const char *name, int aframes, void *arg) 7264 { 7265 dtrace_probe_t *probe, **probes; 7266 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 7267 dtrace_id_t id; 7268 7269 if (provider == dtrace_provider) { 7270 ASSERT(MUTEX_HELD(&dtrace_lock)); 7271 } else { 7272 mutex_enter(&dtrace_lock); 7273 } 7274 7275 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 7276 VM_BESTFIT | VM_SLEEP); 7277 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 7278 7279 probe->dtpr_id = id; 7280 probe->dtpr_gen = dtrace_probegen++; 7281 probe->dtpr_mod = dtrace_strdup(mod); 7282 probe->dtpr_func = dtrace_strdup(func); 7283 probe->dtpr_name = dtrace_strdup(name); 7284 probe->dtpr_arg = arg; 7285 probe->dtpr_aframes = aframes; 7286 probe->dtpr_provider = provider; 7287 7288 dtrace_hash_add(dtrace_bymod, probe); 7289 dtrace_hash_add(dtrace_byfunc, probe); 7290 dtrace_hash_add(dtrace_byname, probe); 7291 7292 if (id - 1 >= dtrace_nprobes) { 7293 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 7294 size_t nsize = osize << 1; 7295 7296 if (nsize == 0) { 7297 ASSERT(osize == 0); 7298 ASSERT(dtrace_probes == NULL); 7299 nsize = sizeof (dtrace_probe_t *); 7300 } 7301 7302 probes = kmem_zalloc(nsize, KM_SLEEP); 7303 7304 if (dtrace_probes == NULL) { 7305 ASSERT(osize == 0); 7306 dtrace_probes = probes; 7307 dtrace_nprobes = 1; 7308 } else { 7309 dtrace_probe_t **oprobes = dtrace_probes; 7310 7311 bcopy(oprobes, probes, osize); 7312 dtrace_membar_producer(); 7313 dtrace_probes = probes; 7314 7315 dtrace_sync(); 7316 7317 /* 7318 * All CPUs are now seeing the new probes array; we can 7319 * safely free the old array. 7320 */ 7321 kmem_free(oprobes, osize); 7322 dtrace_nprobes <<= 1; 7323 } 7324 7325 ASSERT(id - 1 < dtrace_nprobes); 7326 } 7327 7328 ASSERT(dtrace_probes[id - 1] == NULL); 7329 dtrace_probes[id - 1] = probe; 7330 7331 if (provider != dtrace_provider) 7332 mutex_exit(&dtrace_lock); 7333 7334 return (id); 7335 } 7336 7337 static dtrace_probe_t * 7338 dtrace_probe_lookup_id(dtrace_id_t id) 7339 { 7340 ASSERT(MUTEX_HELD(&dtrace_lock)); 7341 7342 if (id == 0 || id > dtrace_nprobes) 7343 return (NULL); 7344 7345 return (dtrace_probes[id - 1]); 7346 } 7347 7348 static int 7349 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 7350 { 7351 *((dtrace_id_t *)arg) = probe->dtpr_id; 7352 7353 return (DTRACE_MATCH_DONE); 7354 } 7355 7356 /* 7357 * Look up a probe based on provider and one or more of module name, function 7358 * name and probe name. 7359 */ 7360 dtrace_id_t 7361 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, 7362 const char *func, const char *name) 7363 { 7364 dtrace_probekey_t pkey; 7365 dtrace_id_t id; 7366 int match; 7367 7368 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 7369 pkey.dtpk_pmatch = &dtrace_match_string; 7370 pkey.dtpk_mod = mod; 7371 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 7372 pkey.dtpk_func = func; 7373 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 7374 pkey.dtpk_name = name; 7375 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 7376 pkey.dtpk_id = DTRACE_IDNONE; 7377 7378 mutex_enter(&dtrace_lock); 7379 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 7380 dtrace_probe_lookup_match, &id); 7381 mutex_exit(&dtrace_lock); 7382 7383 ASSERT(match == 1 || match == 0); 7384 return (match ? id : 0); 7385 } 7386 7387 /* 7388 * Returns the probe argument associated with the specified probe. 7389 */ 7390 void * 7391 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 7392 { 7393 dtrace_probe_t *probe; 7394 void *rval = NULL; 7395 7396 mutex_enter(&dtrace_lock); 7397 7398 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 7399 probe->dtpr_provider == (dtrace_provider_t *)id) 7400 rval = probe->dtpr_arg; 7401 7402 mutex_exit(&dtrace_lock); 7403 7404 return (rval); 7405 } 7406 7407 /* 7408 * Copy a probe into a probe description. 7409 */ 7410 static void 7411 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 7412 { 7413 bzero(pdp, sizeof (dtrace_probedesc_t)); 7414 pdp->dtpd_id = prp->dtpr_id; 7415 7416 (void) strncpy(pdp->dtpd_provider, 7417 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 7418 7419 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 7420 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 7421 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 7422 } 7423 7424 /* 7425 * Called to indicate that a probe -- or probes -- should be provided by a 7426 * specfied provider. If the specified description is NULL, the provider will 7427 * be told to provide all of its probes. (This is done whenever a new 7428 * consumer comes along, or whenever a retained enabling is to be matched.) If 7429 * the specified description is non-NULL, the provider is given the 7430 * opportunity to dynamically provide the specified probe, allowing providers 7431 * to support the creation of probes on-the-fly. (So-called _autocreated_ 7432 * probes.) If the provider is NULL, the operations will be applied to all 7433 * providers; if the provider is non-NULL the operations will only be applied 7434 * to the specified provider. The dtrace_provider_lock must be held, and the 7435 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 7436 * will need to grab the dtrace_lock when it reenters the framework through 7437 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 7438 */ 7439 static void 7440 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 7441 { 7442 struct modctl *ctl; 7443 int all = 0; 7444 7445 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7446 7447 if (prv == NULL) { 7448 all = 1; 7449 prv = dtrace_provider; 7450 } 7451 7452 do { 7453 /* 7454 * First, call the blanket provide operation. 7455 */ 7456 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 7457 7458 /* 7459 * Now call the per-module provide operation. We will grab 7460 * mod_lock to prevent the list from being modified. Note 7461 * that this also prevents the mod_busy bits from changing. 7462 * (mod_busy can only be changed with mod_lock held.) 7463 */ 7464 mutex_enter(&mod_lock); 7465 7466 ctl = &modules; 7467 do { 7468 if (ctl->mod_busy || ctl->mod_mp == NULL) 7469 continue; 7470 7471 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 7472 7473 } while ((ctl = ctl->mod_next) != &modules); 7474 7475 mutex_exit(&mod_lock); 7476 } while (all && (prv = prv->dtpv_next) != NULL); 7477 } 7478 7479 /* 7480 * Iterate over each probe, and call the Framework-to-Provider API function 7481 * denoted by offs. 7482 */ 7483 static void 7484 dtrace_probe_foreach(uintptr_t offs) 7485 { 7486 dtrace_provider_t *prov; 7487 void (*func)(void *, dtrace_id_t, void *); 7488 dtrace_probe_t *probe; 7489 dtrace_icookie_t cookie; 7490 int i; 7491 7492 /* 7493 * We disable interrupts to walk through the probe array. This is 7494 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 7495 * won't see stale data. 7496 */ 7497 cookie = dtrace_interrupt_disable(); 7498 7499 for (i = 0; i < dtrace_nprobes; i++) { 7500 if ((probe = dtrace_probes[i]) == NULL) 7501 continue; 7502 7503 if (probe->dtpr_ecb == NULL) { 7504 /* 7505 * This probe isn't enabled -- don't call the function. 7506 */ 7507 continue; 7508 } 7509 7510 prov = probe->dtpr_provider; 7511 func = *((void(**)(void *, dtrace_id_t, void *)) 7512 ((uintptr_t)&prov->dtpv_pops + offs)); 7513 7514 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 7515 } 7516 7517 dtrace_interrupt_enable(cookie); 7518 } 7519 7520 static int 7521 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 7522 { 7523 dtrace_probekey_t pkey; 7524 uint32_t priv; 7525 uid_t uid; 7526 zoneid_t zoneid; 7527 7528 ASSERT(MUTEX_HELD(&dtrace_lock)); 7529 dtrace_ecb_create_cache = NULL; 7530 7531 if (desc == NULL) { 7532 /* 7533 * If we're passed a NULL description, we're being asked to 7534 * create an ECB with a NULL probe. 7535 */ 7536 (void) dtrace_ecb_create_enable(NULL, enab); 7537 return (0); 7538 } 7539 7540 dtrace_probekey(desc, &pkey); 7541 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 7542 &priv, &uid, &zoneid); 7543 7544 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 7545 enab)); 7546 } 7547 7548 /* 7549 * DTrace Helper Provider Functions 7550 */ 7551 static void 7552 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 7553 { 7554 attr->dtat_name = DOF_ATTR_NAME(dofattr); 7555 attr->dtat_data = DOF_ATTR_DATA(dofattr); 7556 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 7557 } 7558 7559 static void 7560 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 7561 const dof_provider_t *dofprov, char *strtab) 7562 { 7563 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 7564 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 7565 dofprov->dofpv_provattr); 7566 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 7567 dofprov->dofpv_modattr); 7568 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 7569 dofprov->dofpv_funcattr); 7570 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 7571 dofprov->dofpv_nameattr); 7572 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 7573 dofprov->dofpv_argsattr); 7574 } 7575 7576 static void 7577 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 7578 { 7579 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7580 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7581 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 7582 dof_provider_t *provider; 7583 dof_probe_t *probe; 7584 uint32_t *off, *enoff; 7585 uint8_t *arg; 7586 char *strtab; 7587 uint_t i, nprobes; 7588 dtrace_helper_provdesc_t dhpv; 7589 dtrace_helper_probedesc_t dhpb; 7590 dtrace_meta_t *meta = dtrace_meta_pid; 7591 dtrace_mops_t *mops = &meta->dtm_mops; 7592 void *parg; 7593 7594 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 7595 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7596 provider->dofpv_strtab * dof->dofh_secsize); 7597 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7598 provider->dofpv_probes * dof->dofh_secsize); 7599 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7600 provider->dofpv_prargs * dof->dofh_secsize); 7601 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7602 provider->dofpv_proffs * dof->dofh_secsize); 7603 7604 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 7605 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 7606 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 7607 enoff = NULL; 7608 7609 /* 7610 * See dtrace_helper_provider_validate(). 7611 */ 7612 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 7613 provider->dofpv_prenoffs != DOF_SECT_NONE) { 7614 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7615 provider->dofpv_prenoffs * dof->dofh_secsize); 7616 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 7617 } 7618 7619 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 7620 7621 /* 7622 * Create the provider. 7623 */ 7624 dtrace_dofprov2hprov(&dhpv, provider, strtab); 7625 7626 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 7627 return; 7628 7629 meta->dtm_count++; 7630 7631 /* 7632 * Create the probes. 7633 */ 7634 for (i = 0; i < nprobes; i++) { 7635 probe = (dof_probe_t *)(uintptr_t)(daddr + 7636 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 7637 7638 dhpb.dthpb_mod = dhp->dofhp_mod; 7639 dhpb.dthpb_func = strtab + probe->dofpr_func; 7640 dhpb.dthpb_name = strtab + probe->dofpr_name; 7641 dhpb.dthpb_base = probe->dofpr_addr; 7642 dhpb.dthpb_offs = off + probe->dofpr_offidx; 7643 dhpb.dthpb_noffs = probe->dofpr_noffs; 7644 if (enoff != NULL) { 7645 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 7646 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 7647 } else { 7648 dhpb.dthpb_enoffs = NULL; 7649 dhpb.dthpb_nenoffs = 0; 7650 } 7651 dhpb.dthpb_args = arg + probe->dofpr_argidx; 7652 dhpb.dthpb_nargc = probe->dofpr_nargc; 7653 dhpb.dthpb_xargc = probe->dofpr_xargc; 7654 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 7655 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 7656 7657 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 7658 } 7659 } 7660 7661 static void 7662 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 7663 { 7664 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7665 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7666 int i; 7667 7668 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 7669 7670 for (i = 0; i < dof->dofh_secnum; i++) { 7671 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 7672 dof->dofh_secoff + i * dof->dofh_secsize); 7673 7674 if (sec->dofs_type != DOF_SECT_PROVIDER) 7675 continue; 7676 7677 dtrace_helper_provide_one(dhp, sec, pid); 7678 } 7679 7680 /* 7681 * We may have just created probes, so we must now rematch against 7682 * any retained enablings. Note that this call will acquire both 7683 * cpu_lock and dtrace_lock; the fact that we are holding 7684 * dtrace_meta_lock now is what defines the ordering with respect to 7685 * these three locks. 7686 */ 7687 dtrace_enabling_matchall(); 7688 } 7689 7690 static void 7691 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 7692 { 7693 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7694 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7695 dof_sec_t *str_sec; 7696 dof_provider_t *provider; 7697 char *strtab; 7698 dtrace_helper_provdesc_t dhpv; 7699 dtrace_meta_t *meta = dtrace_meta_pid; 7700 dtrace_mops_t *mops = &meta->dtm_mops; 7701 7702 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 7703 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 7704 provider->dofpv_strtab * dof->dofh_secsize); 7705 7706 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 7707 7708 /* 7709 * Create the provider. 7710 */ 7711 dtrace_dofprov2hprov(&dhpv, provider, strtab); 7712 7713 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 7714 7715 meta->dtm_count--; 7716 } 7717 7718 static void 7719 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 7720 { 7721 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 7722 dof_hdr_t *dof = (dof_hdr_t *)daddr; 7723 int i; 7724 7725 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 7726 7727 for (i = 0; i < dof->dofh_secnum; i++) { 7728 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 7729 dof->dofh_secoff + i * dof->dofh_secsize); 7730 7731 if (sec->dofs_type != DOF_SECT_PROVIDER) 7732 continue; 7733 7734 dtrace_helper_provider_remove_one(dhp, sec, pid); 7735 } 7736 } 7737 7738 /* 7739 * DTrace Meta Provider-to-Framework API Functions 7740 * 7741 * These functions implement the Meta Provider-to-Framework API, as described 7742 * in <sys/dtrace.h>. 7743 */ 7744 int 7745 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 7746 dtrace_meta_provider_id_t *idp) 7747 { 7748 dtrace_meta_t *meta; 7749 dtrace_helpers_t *help, *next; 7750 int i; 7751 7752 *idp = DTRACE_METAPROVNONE; 7753 7754 /* 7755 * We strictly don't need the name, but we hold onto it for 7756 * debuggability. All hail error queues! 7757 */ 7758 if (name == NULL) { 7759 cmn_err(CE_WARN, "failed to register meta-provider: " 7760 "invalid name"); 7761 return (EINVAL); 7762 } 7763 7764 if (mops == NULL || 7765 mops->dtms_create_probe == NULL || 7766 mops->dtms_provide_pid == NULL || 7767 mops->dtms_remove_pid == NULL) { 7768 cmn_err(CE_WARN, "failed to register meta-register %s: " 7769 "invalid ops", name); 7770 return (EINVAL); 7771 } 7772 7773 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 7774 meta->dtm_mops = *mops; 7775 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7776 (void) strcpy(meta->dtm_name, name); 7777 meta->dtm_arg = arg; 7778 7779 mutex_enter(&dtrace_meta_lock); 7780 mutex_enter(&dtrace_lock); 7781 7782 if (dtrace_meta_pid != NULL) { 7783 mutex_exit(&dtrace_lock); 7784 mutex_exit(&dtrace_meta_lock); 7785 cmn_err(CE_WARN, "failed to register meta-register %s: " 7786 "user-land meta-provider exists", name); 7787 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 7788 kmem_free(meta, sizeof (dtrace_meta_t)); 7789 return (EINVAL); 7790 } 7791 7792 dtrace_meta_pid = meta; 7793 *idp = (dtrace_meta_provider_id_t)meta; 7794 7795 /* 7796 * If there are providers and probes ready to go, pass them 7797 * off to the new meta provider now. 7798 */ 7799 7800 help = dtrace_deferred_pid; 7801 dtrace_deferred_pid = NULL; 7802 7803 mutex_exit(&dtrace_lock); 7804 7805 while (help != NULL) { 7806 for (i = 0; i < help->dthps_nprovs; i++) { 7807 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 7808 help->dthps_pid); 7809 } 7810 7811 next = help->dthps_next; 7812 help->dthps_next = NULL; 7813 help->dthps_prev = NULL; 7814 help->dthps_deferred = 0; 7815 help = next; 7816 } 7817 7818 mutex_exit(&dtrace_meta_lock); 7819 7820 return (0); 7821 } 7822 7823 int 7824 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 7825 { 7826 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 7827 7828 mutex_enter(&dtrace_meta_lock); 7829 mutex_enter(&dtrace_lock); 7830 7831 if (old == dtrace_meta_pid) { 7832 pp = &dtrace_meta_pid; 7833 } else { 7834 panic("attempt to unregister non-existent " 7835 "dtrace meta-provider %p\n", (void *)old); 7836 } 7837 7838 if (old->dtm_count != 0) { 7839 mutex_exit(&dtrace_lock); 7840 mutex_exit(&dtrace_meta_lock); 7841 return (EBUSY); 7842 } 7843 7844 *pp = NULL; 7845 7846 mutex_exit(&dtrace_lock); 7847 mutex_exit(&dtrace_meta_lock); 7848 7849 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 7850 kmem_free(old, sizeof (dtrace_meta_t)); 7851 7852 return (0); 7853 } 7854 7855 7856 /* 7857 * DTrace DIF Object Functions 7858 */ 7859 static int 7860 dtrace_difo_err(uint_t pc, const char *format, ...) 7861 { 7862 if (dtrace_err_verbose) { 7863 va_list alist; 7864 7865 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 7866 va_start(alist, format); 7867 (void) vuprintf(format, alist); 7868 va_end(alist); 7869 } 7870 7871 #ifdef DTRACE_ERRDEBUG 7872 dtrace_errdebug(format); 7873 #endif 7874 return (1); 7875 } 7876 7877 /* 7878 * Validate a DTrace DIF object by checking the IR instructions. The following 7879 * rules are currently enforced by dtrace_difo_validate(): 7880 * 7881 * 1. Each instruction must have a valid opcode 7882 * 2. Each register, string, variable, or subroutine reference must be valid 7883 * 3. No instruction can modify register %r0 (must be zero) 7884 * 4. All instruction reserved bits must be set to zero 7885 * 5. The last instruction must be a "ret" instruction 7886 * 6. All branch targets must reference a valid instruction _after_ the branch 7887 */ 7888 static int 7889 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 7890 cred_t *cr) 7891 { 7892 int err = 0, i; 7893 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 7894 int kcheckload; 7895 uint_t pc; 7896 7897 kcheckload = cr == NULL || 7898 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 7899 7900 dp->dtdo_destructive = 0; 7901 7902 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 7903 dif_instr_t instr = dp->dtdo_buf[pc]; 7904 7905 uint_t r1 = DIF_INSTR_R1(instr); 7906 uint_t r2 = DIF_INSTR_R2(instr); 7907 uint_t rd = DIF_INSTR_RD(instr); 7908 uint_t rs = DIF_INSTR_RS(instr); 7909 uint_t label = DIF_INSTR_LABEL(instr); 7910 uint_t v = DIF_INSTR_VAR(instr); 7911 uint_t subr = DIF_INSTR_SUBR(instr); 7912 uint_t type = DIF_INSTR_TYPE(instr); 7913 uint_t op = DIF_INSTR_OP(instr); 7914 7915 switch (op) { 7916 case DIF_OP_OR: 7917 case DIF_OP_XOR: 7918 case DIF_OP_AND: 7919 case DIF_OP_SLL: 7920 case DIF_OP_SRL: 7921 case DIF_OP_SRA: 7922 case DIF_OP_SUB: 7923 case DIF_OP_ADD: 7924 case DIF_OP_MUL: 7925 case DIF_OP_SDIV: 7926 case DIF_OP_UDIV: 7927 case DIF_OP_SREM: 7928 case DIF_OP_UREM: 7929 case DIF_OP_COPYS: 7930 if (r1 >= nregs) 7931 err += efunc(pc, "invalid register %u\n", r1); 7932 if (r2 >= nregs) 7933 err += efunc(pc, "invalid register %u\n", r2); 7934 if (rd >= nregs) 7935 err += efunc(pc, "invalid register %u\n", rd); 7936 if (rd == 0) 7937 err += efunc(pc, "cannot write to %r0\n"); 7938 break; 7939 case DIF_OP_NOT: 7940 case DIF_OP_MOV: 7941 case DIF_OP_ALLOCS: 7942 if (r1 >= nregs) 7943 err += efunc(pc, "invalid register %u\n", r1); 7944 if (r2 != 0) 7945 err += efunc(pc, "non-zero reserved bits\n"); 7946 if (rd >= nregs) 7947 err += efunc(pc, "invalid register %u\n", rd); 7948 if (rd == 0) 7949 err += efunc(pc, "cannot write to %r0\n"); 7950 break; 7951 case DIF_OP_LDSB: 7952 case DIF_OP_LDSH: 7953 case DIF_OP_LDSW: 7954 case DIF_OP_LDUB: 7955 case DIF_OP_LDUH: 7956 case DIF_OP_LDUW: 7957 case DIF_OP_LDX: 7958 if (r1 >= nregs) 7959 err += efunc(pc, "invalid register %u\n", r1); 7960 if (r2 != 0) 7961 err += efunc(pc, "non-zero reserved bits\n"); 7962 if (rd >= nregs) 7963 err += efunc(pc, "invalid register %u\n", rd); 7964 if (rd == 0) 7965 err += efunc(pc, "cannot write to %r0\n"); 7966 if (kcheckload) 7967 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 7968 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 7969 break; 7970 case DIF_OP_RLDSB: 7971 case DIF_OP_RLDSH: 7972 case DIF_OP_RLDSW: 7973 case DIF_OP_RLDUB: 7974 case DIF_OP_RLDUH: 7975 case DIF_OP_RLDUW: 7976 case DIF_OP_RLDX: 7977 if (r1 >= nregs) 7978 err += efunc(pc, "invalid register %u\n", r1); 7979 if (r2 != 0) 7980 err += efunc(pc, "non-zero reserved bits\n"); 7981 if (rd >= nregs) 7982 err += efunc(pc, "invalid register %u\n", rd); 7983 if (rd == 0) 7984 err += efunc(pc, "cannot write to %r0\n"); 7985 break; 7986 case DIF_OP_ULDSB: 7987 case DIF_OP_ULDSH: 7988 case DIF_OP_ULDSW: 7989 case DIF_OP_ULDUB: 7990 case DIF_OP_ULDUH: 7991 case DIF_OP_ULDUW: 7992 case DIF_OP_ULDX: 7993 if (r1 >= nregs) 7994 err += efunc(pc, "invalid register %u\n", r1); 7995 if (r2 != 0) 7996 err += efunc(pc, "non-zero reserved bits\n"); 7997 if (rd >= nregs) 7998 err += efunc(pc, "invalid register %u\n", rd); 7999 if (rd == 0) 8000 err += efunc(pc, "cannot write to %r0\n"); 8001 break; 8002 case DIF_OP_STB: 8003 case DIF_OP_STH: 8004 case DIF_OP_STW: 8005 case DIF_OP_STX: 8006 if (r1 >= nregs) 8007 err += efunc(pc, "invalid register %u\n", r1); 8008 if (r2 != 0) 8009 err += efunc(pc, "non-zero reserved bits\n"); 8010 if (rd >= nregs) 8011 err += efunc(pc, "invalid register %u\n", rd); 8012 if (rd == 0) 8013 err += efunc(pc, "cannot write to 0 address\n"); 8014 break; 8015 case DIF_OP_CMP: 8016 case DIF_OP_SCMP: 8017 if (r1 >= nregs) 8018 err += efunc(pc, "invalid register %u\n", r1); 8019 if (r2 >= nregs) 8020 err += efunc(pc, "invalid register %u\n", r2); 8021 if (rd != 0) 8022 err += efunc(pc, "non-zero reserved bits\n"); 8023 break; 8024 case DIF_OP_TST: 8025 if (r1 >= nregs) 8026 err += efunc(pc, "invalid register %u\n", r1); 8027 if (r2 != 0 || rd != 0) 8028 err += efunc(pc, "non-zero reserved bits\n"); 8029 break; 8030 case DIF_OP_BA: 8031 case DIF_OP_BE: 8032 case DIF_OP_BNE: 8033 case DIF_OP_BG: 8034 case DIF_OP_BGU: 8035 case DIF_OP_BGE: 8036 case DIF_OP_BGEU: 8037 case DIF_OP_BL: 8038 case DIF_OP_BLU: 8039 case DIF_OP_BLE: 8040 case DIF_OP_BLEU: 8041 if (label >= dp->dtdo_len) { 8042 err += efunc(pc, "invalid branch target %u\n", 8043 label); 8044 } 8045 if (label <= pc) { 8046 err += efunc(pc, "backward branch to %u\n", 8047 label); 8048 } 8049 break; 8050 case DIF_OP_RET: 8051 if (r1 != 0 || r2 != 0) 8052 err += efunc(pc, "non-zero reserved bits\n"); 8053 if (rd >= nregs) 8054 err += efunc(pc, "invalid register %u\n", rd); 8055 break; 8056 case DIF_OP_NOP: 8057 case DIF_OP_POPTS: 8058 case DIF_OP_FLUSHTS: 8059 if (r1 != 0 || r2 != 0 || rd != 0) 8060 err += efunc(pc, "non-zero reserved bits\n"); 8061 break; 8062 case DIF_OP_SETX: 8063 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 8064 err += efunc(pc, "invalid integer ref %u\n", 8065 DIF_INSTR_INTEGER(instr)); 8066 } 8067 if (rd >= nregs) 8068 err += efunc(pc, "invalid register %u\n", rd); 8069 if (rd == 0) 8070 err += efunc(pc, "cannot write to %r0\n"); 8071 break; 8072 case DIF_OP_SETS: 8073 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 8074 err += efunc(pc, "invalid string ref %u\n", 8075 DIF_INSTR_STRING(instr)); 8076 } 8077 if (rd >= nregs) 8078 err += efunc(pc, "invalid register %u\n", rd); 8079 if (rd == 0) 8080 err += efunc(pc, "cannot write to %r0\n"); 8081 break; 8082 case DIF_OP_LDGA: 8083 case DIF_OP_LDTA: 8084 if (r1 > DIF_VAR_ARRAY_MAX) 8085 err += efunc(pc, "invalid array %u\n", r1); 8086 if (r2 >= nregs) 8087 err += efunc(pc, "invalid register %u\n", r2); 8088 if (rd >= nregs) 8089 err += efunc(pc, "invalid register %u\n", rd); 8090 if (rd == 0) 8091 err += efunc(pc, "cannot write to %r0\n"); 8092 break; 8093 case DIF_OP_LDGS: 8094 case DIF_OP_LDTS: 8095 case DIF_OP_LDLS: 8096 case DIF_OP_LDGAA: 8097 case DIF_OP_LDTAA: 8098 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 8099 err += efunc(pc, "invalid variable %u\n", v); 8100 if (rd >= nregs) 8101 err += efunc(pc, "invalid register %u\n", rd); 8102 if (rd == 0) 8103 err += efunc(pc, "cannot write to %r0\n"); 8104 break; 8105 case DIF_OP_STGS: 8106 case DIF_OP_STTS: 8107 case DIF_OP_STLS: 8108 case DIF_OP_STGAA: 8109 case DIF_OP_STTAA: 8110 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 8111 err += efunc(pc, "invalid variable %u\n", v); 8112 if (rs >= nregs) 8113 err += efunc(pc, "invalid register %u\n", rd); 8114 break; 8115 case DIF_OP_CALL: 8116 if (subr > DIF_SUBR_MAX) 8117 err += efunc(pc, "invalid subr %u\n", subr); 8118 if (rd >= nregs) 8119 err += efunc(pc, "invalid register %u\n", rd); 8120 if (rd == 0) 8121 err += efunc(pc, "cannot write to %r0\n"); 8122 8123 if (subr == DIF_SUBR_COPYOUT || 8124 subr == DIF_SUBR_COPYOUTSTR) { 8125 dp->dtdo_destructive = 1; 8126 } 8127 break; 8128 case DIF_OP_PUSHTR: 8129 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 8130 err += efunc(pc, "invalid ref type %u\n", type); 8131 if (r2 >= nregs) 8132 err += efunc(pc, "invalid register %u\n", r2); 8133 if (rs >= nregs) 8134 err += efunc(pc, "invalid register %u\n", rs); 8135 break; 8136 case DIF_OP_PUSHTV: 8137 if (type != DIF_TYPE_CTF) 8138 err += efunc(pc, "invalid val type %u\n", type); 8139 if (r2 >= nregs) 8140 err += efunc(pc, "invalid register %u\n", r2); 8141 if (rs >= nregs) 8142 err += efunc(pc, "invalid register %u\n", rs); 8143 break; 8144 default: 8145 err += efunc(pc, "invalid opcode %u\n", 8146 DIF_INSTR_OP(instr)); 8147 } 8148 } 8149 8150 if (dp->dtdo_len != 0 && 8151 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 8152 err += efunc(dp->dtdo_len - 1, 8153 "expected 'ret' as last DIF instruction\n"); 8154 } 8155 8156 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 8157 /* 8158 * If we're not returning by reference, the size must be either 8159 * 0 or the size of one of the base types. 8160 */ 8161 switch (dp->dtdo_rtype.dtdt_size) { 8162 case 0: 8163 case sizeof (uint8_t): 8164 case sizeof (uint16_t): 8165 case sizeof (uint32_t): 8166 case sizeof (uint64_t): 8167 break; 8168 8169 default: 8170 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 8171 } 8172 } 8173 8174 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 8175 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 8176 dtrace_diftype_t *vt, *et; 8177 uint_t id, ndx; 8178 8179 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 8180 v->dtdv_scope != DIFV_SCOPE_THREAD && 8181 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 8182 err += efunc(i, "unrecognized variable scope %d\n", 8183 v->dtdv_scope); 8184 break; 8185 } 8186 8187 if (v->dtdv_kind != DIFV_KIND_ARRAY && 8188 v->dtdv_kind != DIFV_KIND_SCALAR) { 8189 err += efunc(i, "unrecognized variable type %d\n", 8190 v->dtdv_kind); 8191 break; 8192 } 8193 8194 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 8195 err += efunc(i, "%d exceeds variable id limit\n", id); 8196 break; 8197 } 8198 8199 if (id < DIF_VAR_OTHER_UBASE) 8200 continue; 8201 8202 /* 8203 * For user-defined variables, we need to check that this 8204 * definition is identical to any previous definition that we 8205 * encountered. 8206 */ 8207 ndx = id - DIF_VAR_OTHER_UBASE; 8208 8209 switch (v->dtdv_scope) { 8210 case DIFV_SCOPE_GLOBAL: 8211 if (ndx < vstate->dtvs_nglobals) { 8212 dtrace_statvar_t *svar; 8213 8214 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 8215 existing = &svar->dtsv_var; 8216 } 8217 8218 break; 8219 8220 case DIFV_SCOPE_THREAD: 8221 if (ndx < vstate->dtvs_ntlocals) 8222 existing = &vstate->dtvs_tlocals[ndx]; 8223 break; 8224 8225 case DIFV_SCOPE_LOCAL: 8226 if (ndx < vstate->dtvs_nlocals) { 8227 dtrace_statvar_t *svar; 8228 8229 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 8230 existing = &svar->dtsv_var; 8231 } 8232 8233 break; 8234 } 8235 8236 vt = &v->dtdv_type; 8237 8238 if (vt->dtdt_flags & DIF_TF_BYREF) { 8239 if (vt->dtdt_size == 0) { 8240 err += efunc(i, "zero-sized variable\n"); 8241 break; 8242 } 8243 8244 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 8245 vt->dtdt_size > dtrace_global_maxsize) { 8246 err += efunc(i, "oversized by-ref global\n"); 8247 break; 8248 } 8249 } 8250 8251 if (existing == NULL || existing->dtdv_id == 0) 8252 continue; 8253 8254 ASSERT(existing->dtdv_id == v->dtdv_id); 8255 ASSERT(existing->dtdv_scope == v->dtdv_scope); 8256 8257 if (existing->dtdv_kind != v->dtdv_kind) 8258 err += efunc(i, "%d changed variable kind\n", id); 8259 8260 et = &existing->dtdv_type; 8261 8262 if (vt->dtdt_flags != et->dtdt_flags) { 8263 err += efunc(i, "%d changed variable type flags\n", id); 8264 break; 8265 } 8266 8267 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 8268 err += efunc(i, "%d changed variable type size\n", id); 8269 break; 8270 } 8271 } 8272 8273 return (err); 8274 } 8275 8276 /* 8277 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 8278 * are much more constrained than normal DIFOs. Specifically, they may 8279 * not: 8280 * 8281 * 1. Make calls to subroutines other than copyin(), copyinstr() or 8282 * miscellaneous string routines 8283 * 2. Access DTrace variables other than the args[] array, and the 8284 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 8285 * 3. Have thread-local variables. 8286 * 4. Have dynamic variables. 8287 */ 8288 static int 8289 dtrace_difo_validate_helper(dtrace_difo_t *dp) 8290 { 8291 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8292 int err = 0; 8293 uint_t pc; 8294 8295 for (pc = 0; pc < dp->dtdo_len; pc++) { 8296 dif_instr_t instr = dp->dtdo_buf[pc]; 8297 8298 uint_t v = DIF_INSTR_VAR(instr); 8299 uint_t subr = DIF_INSTR_SUBR(instr); 8300 uint_t op = DIF_INSTR_OP(instr); 8301 8302 switch (op) { 8303 case DIF_OP_OR: 8304 case DIF_OP_XOR: 8305 case DIF_OP_AND: 8306 case DIF_OP_SLL: 8307 case DIF_OP_SRL: 8308 case DIF_OP_SRA: 8309 case DIF_OP_SUB: 8310 case DIF_OP_ADD: 8311 case DIF_OP_MUL: 8312 case DIF_OP_SDIV: 8313 case DIF_OP_UDIV: 8314 case DIF_OP_SREM: 8315 case DIF_OP_UREM: 8316 case DIF_OP_COPYS: 8317 case DIF_OP_NOT: 8318 case DIF_OP_MOV: 8319 case DIF_OP_RLDSB: 8320 case DIF_OP_RLDSH: 8321 case DIF_OP_RLDSW: 8322 case DIF_OP_RLDUB: 8323 case DIF_OP_RLDUH: 8324 case DIF_OP_RLDUW: 8325 case DIF_OP_RLDX: 8326 case DIF_OP_ULDSB: 8327 case DIF_OP_ULDSH: 8328 case DIF_OP_ULDSW: 8329 case DIF_OP_ULDUB: 8330 case DIF_OP_ULDUH: 8331 case DIF_OP_ULDUW: 8332 case DIF_OP_ULDX: 8333 case DIF_OP_STB: 8334 case DIF_OP_STH: 8335 case DIF_OP_STW: 8336 case DIF_OP_STX: 8337 case DIF_OP_ALLOCS: 8338 case DIF_OP_CMP: 8339 case DIF_OP_SCMP: 8340 case DIF_OP_TST: 8341 case DIF_OP_BA: 8342 case DIF_OP_BE: 8343 case DIF_OP_BNE: 8344 case DIF_OP_BG: 8345 case DIF_OP_BGU: 8346 case DIF_OP_BGE: 8347 case DIF_OP_BGEU: 8348 case DIF_OP_BL: 8349 case DIF_OP_BLU: 8350 case DIF_OP_BLE: 8351 case DIF_OP_BLEU: 8352 case DIF_OP_RET: 8353 case DIF_OP_NOP: 8354 case DIF_OP_POPTS: 8355 case DIF_OP_FLUSHTS: 8356 case DIF_OP_SETX: 8357 case DIF_OP_SETS: 8358 case DIF_OP_LDGA: 8359 case DIF_OP_LDLS: 8360 case DIF_OP_STGS: 8361 case DIF_OP_STLS: 8362 case DIF_OP_PUSHTR: 8363 case DIF_OP_PUSHTV: 8364 break; 8365 8366 case DIF_OP_LDGS: 8367 if (v >= DIF_VAR_OTHER_UBASE) 8368 break; 8369 8370 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 8371 break; 8372 8373 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 8374 v == DIF_VAR_PPID || v == DIF_VAR_TID || 8375 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 8376 v == DIF_VAR_UID || v == DIF_VAR_GID) 8377 break; 8378 8379 err += efunc(pc, "illegal variable %u\n", v); 8380 break; 8381 8382 case DIF_OP_LDTA: 8383 case DIF_OP_LDTS: 8384 case DIF_OP_LDGAA: 8385 case DIF_OP_LDTAA: 8386 err += efunc(pc, "illegal dynamic variable load\n"); 8387 break; 8388 8389 case DIF_OP_STTS: 8390 case DIF_OP_STGAA: 8391 case DIF_OP_STTAA: 8392 err += efunc(pc, "illegal dynamic variable store\n"); 8393 break; 8394 8395 case DIF_OP_CALL: 8396 if (subr == DIF_SUBR_ALLOCA || 8397 subr == DIF_SUBR_BCOPY || 8398 subr == DIF_SUBR_COPYIN || 8399 subr == DIF_SUBR_COPYINTO || 8400 subr == DIF_SUBR_COPYINSTR || 8401 subr == DIF_SUBR_INDEX || 8402 subr == DIF_SUBR_INET_NTOA || 8403 subr == DIF_SUBR_INET_NTOA6 || 8404 subr == DIF_SUBR_INET_NTOP || 8405 subr == DIF_SUBR_LLTOSTR || 8406 subr == DIF_SUBR_RINDEX || 8407 subr == DIF_SUBR_STRCHR || 8408 subr == DIF_SUBR_STRJOIN || 8409 subr == DIF_SUBR_STRRCHR || 8410 subr == DIF_SUBR_STRSTR || 8411 subr == DIF_SUBR_HTONS || 8412 subr == DIF_SUBR_HTONL || 8413 subr == DIF_SUBR_HTONLL || 8414 subr == DIF_SUBR_NTOHS || 8415 subr == DIF_SUBR_NTOHL || 8416 subr == DIF_SUBR_NTOHLL) 8417 break; 8418 8419 err += efunc(pc, "invalid subr %u\n", subr); 8420 break; 8421 8422 default: 8423 err += efunc(pc, "invalid opcode %u\n", 8424 DIF_INSTR_OP(instr)); 8425 } 8426 } 8427 8428 return (err); 8429 } 8430 8431 /* 8432 * Returns 1 if the expression in the DIF object can be cached on a per-thread 8433 * basis; 0 if not. 8434 */ 8435 static int 8436 dtrace_difo_cacheable(dtrace_difo_t *dp) 8437 { 8438 int i; 8439 8440 if (dp == NULL) 8441 return (0); 8442 8443 for (i = 0; i < dp->dtdo_varlen; i++) { 8444 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8445 8446 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 8447 continue; 8448 8449 switch (v->dtdv_id) { 8450 case DIF_VAR_CURTHREAD: 8451 case DIF_VAR_PID: 8452 case DIF_VAR_TID: 8453 case DIF_VAR_EXECNAME: 8454 case DIF_VAR_ZONENAME: 8455 break; 8456 8457 default: 8458 return (0); 8459 } 8460 } 8461 8462 /* 8463 * This DIF object may be cacheable. Now we need to look for any 8464 * array loading instructions, any memory loading instructions, or 8465 * any stores to thread-local variables. 8466 */ 8467 for (i = 0; i < dp->dtdo_len; i++) { 8468 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 8469 8470 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 8471 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 8472 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 8473 op == DIF_OP_LDGA || op == DIF_OP_STTS) 8474 return (0); 8475 } 8476 8477 return (1); 8478 } 8479 8480 static void 8481 dtrace_difo_hold(dtrace_difo_t *dp) 8482 { 8483 int i; 8484 8485 ASSERT(MUTEX_HELD(&dtrace_lock)); 8486 8487 dp->dtdo_refcnt++; 8488 ASSERT(dp->dtdo_refcnt != 0); 8489 8490 /* 8491 * We need to check this DIF object for references to the variable 8492 * DIF_VAR_VTIMESTAMP. 8493 */ 8494 for (i = 0; i < dp->dtdo_varlen; i++) { 8495 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8496 8497 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 8498 continue; 8499 8500 if (dtrace_vtime_references++ == 0) 8501 dtrace_vtime_enable(); 8502 } 8503 } 8504 8505 /* 8506 * This routine calculates the dynamic variable chunksize for a given DIF 8507 * object. The calculation is not fool-proof, and can probably be tricked by 8508 * malicious DIF -- but it works for all compiler-generated DIF. Because this 8509 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 8510 * if a dynamic variable size exceeds the chunksize. 8511 */ 8512 static void 8513 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8514 { 8515 uint64_t sval; 8516 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 8517 const dif_instr_t *text = dp->dtdo_buf; 8518 uint_t pc, srd = 0; 8519 uint_t ttop = 0; 8520 size_t size, ksize; 8521 uint_t id, i; 8522 8523 for (pc = 0; pc < dp->dtdo_len; pc++) { 8524 dif_instr_t instr = text[pc]; 8525 uint_t op = DIF_INSTR_OP(instr); 8526 uint_t rd = DIF_INSTR_RD(instr); 8527 uint_t r1 = DIF_INSTR_R1(instr); 8528 uint_t nkeys = 0; 8529 uchar_t scope; 8530 8531 dtrace_key_t *key = tupregs; 8532 8533 switch (op) { 8534 case DIF_OP_SETX: 8535 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 8536 srd = rd; 8537 continue; 8538 8539 case DIF_OP_STTS: 8540 key = &tupregs[DIF_DTR_NREGS]; 8541 key[0].dttk_size = 0; 8542 key[1].dttk_size = 0; 8543 nkeys = 2; 8544 scope = DIFV_SCOPE_THREAD; 8545 break; 8546 8547 case DIF_OP_STGAA: 8548 case DIF_OP_STTAA: 8549 nkeys = ttop; 8550 8551 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 8552 key[nkeys++].dttk_size = 0; 8553 8554 key[nkeys++].dttk_size = 0; 8555 8556 if (op == DIF_OP_STTAA) { 8557 scope = DIFV_SCOPE_THREAD; 8558 } else { 8559 scope = DIFV_SCOPE_GLOBAL; 8560 } 8561 8562 break; 8563 8564 case DIF_OP_PUSHTR: 8565 if (ttop == DIF_DTR_NREGS) 8566 return; 8567 8568 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 8569 /* 8570 * If the register for the size of the "pushtr" 8571 * is %r0 (or the value is 0) and the type is 8572 * a string, we'll use the system-wide default 8573 * string size. 8574 */ 8575 tupregs[ttop++].dttk_size = 8576 dtrace_strsize_default; 8577 } else { 8578 if (srd == 0) 8579 return; 8580 8581 tupregs[ttop++].dttk_size = sval; 8582 } 8583 8584 break; 8585 8586 case DIF_OP_PUSHTV: 8587 if (ttop == DIF_DTR_NREGS) 8588 return; 8589 8590 tupregs[ttop++].dttk_size = 0; 8591 break; 8592 8593 case DIF_OP_FLUSHTS: 8594 ttop = 0; 8595 break; 8596 8597 case DIF_OP_POPTS: 8598 if (ttop != 0) 8599 ttop--; 8600 break; 8601 } 8602 8603 sval = 0; 8604 srd = 0; 8605 8606 if (nkeys == 0) 8607 continue; 8608 8609 /* 8610 * We have a dynamic variable allocation; calculate its size. 8611 */ 8612 for (ksize = 0, i = 0; i < nkeys; i++) 8613 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 8614 8615 size = sizeof (dtrace_dynvar_t); 8616 size += sizeof (dtrace_key_t) * (nkeys - 1); 8617 size += ksize; 8618 8619 /* 8620 * Now we need to determine the size of the stored data. 8621 */ 8622 id = DIF_INSTR_VAR(instr); 8623 8624 for (i = 0; i < dp->dtdo_varlen; i++) { 8625 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8626 8627 if (v->dtdv_id == id && v->dtdv_scope == scope) { 8628 size += v->dtdv_type.dtdt_size; 8629 break; 8630 } 8631 } 8632 8633 if (i == dp->dtdo_varlen) 8634 return; 8635 8636 /* 8637 * We have the size. If this is larger than the chunk size 8638 * for our dynamic variable state, reset the chunk size. 8639 */ 8640 size = P2ROUNDUP(size, sizeof (uint64_t)); 8641 8642 if (size > vstate->dtvs_dynvars.dtds_chunksize) 8643 vstate->dtvs_dynvars.dtds_chunksize = size; 8644 } 8645 } 8646 8647 static void 8648 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8649 { 8650 int i, oldsvars, osz, nsz, otlocals, ntlocals; 8651 uint_t id; 8652 8653 ASSERT(MUTEX_HELD(&dtrace_lock)); 8654 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 8655 8656 for (i = 0; i < dp->dtdo_varlen; i++) { 8657 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8658 dtrace_statvar_t *svar, ***svarp; 8659 size_t dsize = 0; 8660 uint8_t scope = v->dtdv_scope; 8661 int *np; 8662 8663 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 8664 continue; 8665 8666 id -= DIF_VAR_OTHER_UBASE; 8667 8668 switch (scope) { 8669 case DIFV_SCOPE_THREAD: 8670 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 8671 dtrace_difv_t *tlocals; 8672 8673 if ((ntlocals = (otlocals << 1)) == 0) 8674 ntlocals = 1; 8675 8676 osz = otlocals * sizeof (dtrace_difv_t); 8677 nsz = ntlocals * sizeof (dtrace_difv_t); 8678 8679 tlocals = kmem_zalloc(nsz, KM_SLEEP); 8680 8681 if (osz != 0) { 8682 bcopy(vstate->dtvs_tlocals, 8683 tlocals, osz); 8684 kmem_free(vstate->dtvs_tlocals, osz); 8685 } 8686 8687 vstate->dtvs_tlocals = tlocals; 8688 vstate->dtvs_ntlocals = ntlocals; 8689 } 8690 8691 vstate->dtvs_tlocals[id] = *v; 8692 continue; 8693 8694 case DIFV_SCOPE_LOCAL: 8695 np = &vstate->dtvs_nlocals; 8696 svarp = &vstate->dtvs_locals; 8697 8698 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 8699 dsize = NCPU * (v->dtdv_type.dtdt_size + 8700 sizeof (uint64_t)); 8701 else 8702 dsize = NCPU * sizeof (uint64_t); 8703 8704 break; 8705 8706 case DIFV_SCOPE_GLOBAL: 8707 np = &vstate->dtvs_nglobals; 8708 svarp = &vstate->dtvs_globals; 8709 8710 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 8711 dsize = v->dtdv_type.dtdt_size + 8712 sizeof (uint64_t); 8713 8714 break; 8715 8716 default: 8717 ASSERT(0); 8718 } 8719 8720 while (id >= (oldsvars = *np)) { 8721 dtrace_statvar_t **statics; 8722 int newsvars, oldsize, newsize; 8723 8724 if ((newsvars = (oldsvars << 1)) == 0) 8725 newsvars = 1; 8726 8727 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 8728 newsize = newsvars * sizeof (dtrace_statvar_t *); 8729 8730 statics = kmem_zalloc(newsize, KM_SLEEP); 8731 8732 if (oldsize != 0) { 8733 bcopy(*svarp, statics, oldsize); 8734 kmem_free(*svarp, oldsize); 8735 } 8736 8737 *svarp = statics; 8738 *np = newsvars; 8739 } 8740 8741 if ((svar = (*svarp)[id]) == NULL) { 8742 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 8743 svar->dtsv_var = *v; 8744 8745 if ((svar->dtsv_size = dsize) != 0) { 8746 svar->dtsv_data = (uint64_t)(uintptr_t) 8747 kmem_zalloc(dsize, KM_SLEEP); 8748 } 8749 8750 (*svarp)[id] = svar; 8751 } 8752 8753 svar->dtsv_refcnt++; 8754 } 8755 8756 dtrace_difo_chunksize(dp, vstate); 8757 dtrace_difo_hold(dp); 8758 } 8759 8760 static dtrace_difo_t * 8761 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8762 { 8763 dtrace_difo_t *new; 8764 size_t sz; 8765 8766 ASSERT(dp->dtdo_buf != NULL); 8767 ASSERT(dp->dtdo_refcnt != 0); 8768 8769 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 8770 8771 ASSERT(dp->dtdo_buf != NULL); 8772 sz = dp->dtdo_len * sizeof (dif_instr_t); 8773 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 8774 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 8775 new->dtdo_len = dp->dtdo_len; 8776 8777 if (dp->dtdo_strtab != NULL) { 8778 ASSERT(dp->dtdo_strlen != 0); 8779 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 8780 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 8781 new->dtdo_strlen = dp->dtdo_strlen; 8782 } 8783 8784 if (dp->dtdo_inttab != NULL) { 8785 ASSERT(dp->dtdo_intlen != 0); 8786 sz = dp->dtdo_intlen * sizeof (uint64_t); 8787 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 8788 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 8789 new->dtdo_intlen = dp->dtdo_intlen; 8790 } 8791 8792 if (dp->dtdo_vartab != NULL) { 8793 ASSERT(dp->dtdo_varlen != 0); 8794 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 8795 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 8796 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 8797 new->dtdo_varlen = dp->dtdo_varlen; 8798 } 8799 8800 dtrace_difo_init(new, vstate); 8801 return (new); 8802 } 8803 8804 static void 8805 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8806 { 8807 int i; 8808 8809 ASSERT(dp->dtdo_refcnt == 0); 8810 8811 for (i = 0; i < dp->dtdo_varlen; i++) { 8812 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8813 dtrace_statvar_t *svar, **svarp; 8814 uint_t id; 8815 uint8_t scope = v->dtdv_scope; 8816 int *np; 8817 8818 switch (scope) { 8819 case DIFV_SCOPE_THREAD: 8820 continue; 8821 8822 case DIFV_SCOPE_LOCAL: 8823 np = &vstate->dtvs_nlocals; 8824 svarp = vstate->dtvs_locals; 8825 break; 8826 8827 case DIFV_SCOPE_GLOBAL: 8828 np = &vstate->dtvs_nglobals; 8829 svarp = vstate->dtvs_globals; 8830 break; 8831 8832 default: 8833 ASSERT(0); 8834 } 8835 8836 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 8837 continue; 8838 8839 id -= DIF_VAR_OTHER_UBASE; 8840 ASSERT(id < *np); 8841 8842 svar = svarp[id]; 8843 ASSERT(svar != NULL); 8844 ASSERT(svar->dtsv_refcnt > 0); 8845 8846 if (--svar->dtsv_refcnt > 0) 8847 continue; 8848 8849 if (svar->dtsv_size != 0) { 8850 ASSERT(svar->dtsv_data != NULL); 8851 kmem_free((void *)(uintptr_t)svar->dtsv_data, 8852 svar->dtsv_size); 8853 } 8854 8855 kmem_free(svar, sizeof (dtrace_statvar_t)); 8856 svarp[id] = NULL; 8857 } 8858 8859 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 8860 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 8861 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 8862 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 8863 8864 kmem_free(dp, sizeof (dtrace_difo_t)); 8865 } 8866 8867 static void 8868 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8869 { 8870 int i; 8871 8872 ASSERT(MUTEX_HELD(&dtrace_lock)); 8873 ASSERT(dp->dtdo_refcnt != 0); 8874 8875 for (i = 0; i < dp->dtdo_varlen; i++) { 8876 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8877 8878 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 8879 continue; 8880 8881 ASSERT(dtrace_vtime_references > 0); 8882 if (--dtrace_vtime_references == 0) 8883 dtrace_vtime_disable(); 8884 } 8885 8886 if (--dp->dtdo_refcnt == 0) 8887 dtrace_difo_destroy(dp, vstate); 8888 } 8889 8890 /* 8891 * DTrace Format Functions 8892 */ 8893 static uint16_t 8894 dtrace_format_add(dtrace_state_t *state, char *str) 8895 { 8896 char *fmt, **new; 8897 uint16_t ndx, len = strlen(str) + 1; 8898 8899 fmt = kmem_zalloc(len, KM_SLEEP); 8900 bcopy(str, fmt, len); 8901 8902 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 8903 if (state->dts_formats[ndx] == NULL) { 8904 state->dts_formats[ndx] = fmt; 8905 return (ndx + 1); 8906 } 8907 } 8908 8909 if (state->dts_nformats == USHRT_MAX) { 8910 /* 8911 * This is only likely if a denial-of-service attack is being 8912 * attempted. As such, it's okay to fail silently here. 8913 */ 8914 kmem_free(fmt, len); 8915 return (0); 8916 } 8917 8918 /* 8919 * For simplicity, we always resize the formats array to be exactly the 8920 * number of formats. 8921 */ 8922 ndx = state->dts_nformats++; 8923 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 8924 8925 if (state->dts_formats != NULL) { 8926 ASSERT(ndx != 0); 8927 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 8928 kmem_free(state->dts_formats, ndx * sizeof (char *)); 8929 } 8930 8931 state->dts_formats = new; 8932 state->dts_formats[ndx] = fmt; 8933 8934 return (ndx + 1); 8935 } 8936 8937 static void 8938 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 8939 { 8940 char *fmt; 8941 8942 ASSERT(state->dts_formats != NULL); 8943 ASSERT(format <= state->dts_nformats); 8944 ASSERT(state->dts_formats[format - 1] != NULL); 8945 8946 fmt = state->dts_formats[format - 1]; 8947 kmem_free(fmt, strlen(fmt) + 1); 8948 state->dts_formats[format - 1] = NULL; 8949 } 8950 8951 static void 8952 dtrace_format_destroy(dtrace_state_t *state) 8953 { 8954 int i; 8955 8956 if (state->dts_nformats == 0) { 8957 ASSERT(state->dts_formats == NULL); 8958 return; 8959 } 8960 8961 ASSERT(state->dts_formats != NULL); 8962 8963 for (i = 0; i < state->dts_nformats; i++) { 8964 char *fmt = state->dts_formats[i]; 8965 8966 if (fmt == NULL) 8967 continue; 8968 8969 kmem_free(fmt, strlen(fmt) + 1); 8970 } 8971 8972 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 8973 state->dts_nformats = 0; 8974 state->dts_formats = NULL; 8975 } 8976 8977 /* 8978 * DTrace Predicate Functions 8979 */ 8980 static dtrace_predicate_t * 8981 dtrace_predicate_create(dtrace_difo_t *dp) 8982 { 8983 dtrace_predicate_t *pred; 8984 8985 ASSERT(MUTEX_HELD(&dtrace_lock)); 8986 ASSERT(dp->dtdo_refcnt != 0); 8987 8988 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 8989 pred->dtp_difo = dp; 8990 pred->dtp_refcnt = 1; 8991 8992 if (!dtrace_difo_cacheable(dp)) 8993 return (pred); 8994 8995 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 8996 /* 8997 * This is only theoretically possible -- we have had 2^32 8998 * cacheable predicates on this machine. We cannot allow any 8999 * more predicates to become cacheable: as unlikely as it is, 9000 * there may be a thread caching a (now stale) predicate cache 9001 * ID. (N.B.: the temptation is being successfully resisted to 9002 * have this cmn_err() "Holy shit -- we executed this code!") 9003 */ 9004 return (pred); 9005 } 9006 9007 pred->dtp_cacheid = dtrace_predcache_id++; 9008 9009 return (pred); 9010 } 9011 9012 static void 9013 dtrace_predicate_hold(dtrace_predicate_t *pred) 9014 { 9015 ASSERT(MUTEX_HELD(&dtrace_lock)); 9016 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 9017 ASSERT(pred->dtp_refcnt > 0); 9018 9019 pred->dtp_refcnt++; 9020 } 9021 9022 static void 9023 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 9024 { 9025 dtrace_difo_t *dp = pred->dtp_difo; 9026 9027 ASSERT(MUTEX_HELD(&dtrace_lock)); 9028 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 9029 ASSERT(pred->dtp_refcnt > 0); 9030 9031 if (--pred->dtp_refcnt == 0) { 9032 dtrace_difo_release(pred->dtp_difo, vstate); 9033 kmem_free(pred, sizeof (dtrace_predicate_t)); 9034 } 9035 } 9036 9037 /* 9038 * DTrace Action Description Functions 9039 */ 9040 static dtrace_actdesc_t * 9041 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 9042 uint64_t uarg, uint64_t arg) 9043 { 9044 dtrace_actdesc_t *act; 9045 9046 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 9047 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 9048 9049 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 9050 act->dtad_kind = kind; 9051 act->dtad_ntuple = ntuple; 9052 act->dtad_uarg = uarg; 9053 act->dtad_arg = arg; 9054 act->dtad_refcnt = 1; 9055 9056 return (act); 9057 } 9058 9059 static void 9060 dtrace_actdesc_hold(dtrace_actdesc_t *act) 9061 { 9062 ASSERT(act->dtad_refcnt >= 1); 9063 act->dtad_refcnt++; 9064 } 9065 9066 static void 9067 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 9068 { 9069 dtrace_actkind_t kind = act->dtad_kind; 9070 dtrace_difo_t *dp; 9071 9072 ASSERT(act->dtad_refcnt >= 1); 9073 9074 if (--act->dtad_refcnt != 0) 9075 return; 9076 9077 if ((dp = act->dtad_difo) != NULL) 9078 dtrace_difo_release(dp, vstate); 9079 9080 if (DTRACEACT_ISPRINTFLIKE(kind)) { 9081 char *str = (char *)(uintptr_t)act->dtad_arg; 9082 9083 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 9084 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 9085 9086 if (str != NULL) 9087 kmem_free(str, strlen(str) + 1); 9088 } 9089 9090 kmem_free(act, sizeof (dtrace_actdesc_t)); 9091 } 9092 9093 /* 9094 * DTrace ECB Functions 9095 */ 9096 static dtrace_ecb_t * 9097 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 9098 { 9099 dtrace_ecb_t *ecb; 9100 dtrace_epid_t epid; 9101 9102 ASSERT(MUTEX_HELD(&dtrace_lock)); 9103 9104 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 9105 ecb->dte_predicate = NULL; 9106 ecb->dte_probe = probe; 9107 9108 /* 9109 * The default size is the size of the default action: recording 9110 * the epid. 9111 */ 9112 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9113 ecb->dte_alignment = sizeof (dtrace_epid_t); 9114 9115 epid = state->dts_epid++; 9116 9117 if (epid - 1 >= state->dts_necbs) { 9118 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 9119 int necbs = state->dts_necbs << 1; 9120 9121 ASSERT(epid == state->dts_necbs + 1); 9122 9123 if (necbs == 0) { 9124 ASSERT(oecbs == NULL); 9125 necbs = 1; 9126 } 9127 9128 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 9129 9130 if (oecbs != NULL) 9131 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 9132 9133 dtrace_membar_producer(); 9134 state->dts_ecbs = ecbs; 9135 9136 if (oecbs != NULL) { 9137 /* 9138 * If this state is active, we must dtrace_sync() 9139 * before we can free the old dts_ecbs array: we're 9140 * coming in hot, and there may be active ring 9141 * buffer processing (which indexes into the dts_ecbs 9142 * array) on another CPU. 9143 */ 9144 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 9145 dtrace_sync(); 9146 9147 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 9148 } 9149 9150 dtrace_membar_producer(); 9151 state->dts_necbs = necbs; 9152 } 9153 9154 ecb->dte_state = state; 9155 9156 ASSERT(state->dts_ecbs[epid - 1] == NULL); 9157 dtrace_membar_producer(); 9158 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 9159 9160 return (ecb); 9161 } 9162 9163 static int 9164 dtrace_ecb_enable(dtrace_ecb_t *ecb) 9165 { 9166 dtrace_probe_t *probe = ecb->dte_probe; 9167 9168 ASSERT(MUTEX_HELD(&cpu_lock)); 9169 ASSERT(MUTEX_HELD(&dtrace_lock)); 9170 ASSERT(ecb->dte_next == NULL); 9171 9172 if (probe == NULL) { 9173 /* 9174 * This is the NULL probe -- there's nothing to do. 9175 */ 9176 return (0); 9177 } 9178 9179 if (probe->dtpr_ecb == NULL) { 9180 dtrace_provider_t *prov = probe->dtpr_provider; 9181 9182 /* 9183 * We're the first ECB on this probe. 9184 */ 9185 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 9186 9187 if (ecb->dte_predicate != NULL) 9188 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 9189 9190 return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 9191 probe->dtpr_id, probe->dtpr_arg)); 9192 } else { 9193 /* 9194 * This probe is already active. Swing the last pointer to 9195 * point to the new ECB, and issue a dtrace_sync() to assure 9196 * that all CPUs have seen the change. 9197 */ 9198 ASSERT(probe->dtpr_ecb_last != NULL); 9199 probe->dtpr_ecb_last->dte_next = ecb; 9200 probe->dtpr_ecb_last = ecb; 9201 probe->dtpr_predcache = 0; 9202 9203 dtrace_sync(); 9204 return (0); 9205 } 9206 } 9207 9208 static void 9209 dtrace_ecb_resize(dtrace_ecb_t *ecb) 9210 { 9211 uint32_t maxalign = sizeof (dtrace_epid_t); 9212 uint32_t align = sizeof (uint8_t), offs, diff; 9213 dtrace_action_t *act; 9214 int wastuple = 0; 9215 uint32_t aggbase = UINT32_MAX; 9216 dtrace_state_t *state = ecb->dte_state; 9217 9218 /* 9219 * If we record anything, we always record the epid. (And we always 9220 * record it first.) 9221 */ 9222 offs = sizeof (dtrace_epid_t); 9223 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9224 9225 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9226 dtrace_recdesc_t *rec = &act->dta_rec; 9227 9228 if ((align = rec->dtrd_alignment) > maxalign) 9229 maxalign = align; 9230 9231 if (!wastuple && act->dta_intuple) { 9232 /* 9233 * This is the first record in a tuple. Align the 9234 * offset to be at offset 4 in an 8-byte aligned 9235 * block. 9236 */ 9237 diff = offs + sizeof (dtrace_aggid_t); 9238 9239 if (diff = (diff & (sizeof (uint64_t) - 1))) 9240 offs += sizeof (uint64_t) - diff; 9241 9242 aggbase = offs - sizeof (dtrace_aggid_t); 9243 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 9244 } 9245 9246 /*LINTED*/ 9247 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 9248 /* 9249 * The current offset is not properly aligned; align it. 9250 */ 9251 offs += align - diff; 9252 } 9253 9254 rec->dtrd_offset = offs; 9255 9256 if (offs + rec->dtrd_size > ecb->dte_needed) { 9257 ecb->dte_needed = offs + rec->dtrd_size; 9258 9259 if (ecb->dte_needed > state->dts_needed) 9260 state->dts_needed = ecb->dte_needed; 9261 } 9262 9263 if (DTRACEACT_ISAGG(act->dta_kind)) { 9264 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9265 dtrace_action_t *first = agg->dtag_first, *prev; 9266 9267 ASSERT(rec->dtrd_size != 0 && first != NULL); 9268 ASSERT(wastuple); 9269 ASSERT(aggbase != UINT32_MAX); 9270 9271 agg->dtag_base = aggbase; 9272 9273 while ((prev = first->dta_prev) != NULL && 9274 DTRACEACT_ISAGG(prev->dta_kind)) { 9275 agg = (dtrace_aggregation_t *)prev; 9276 first = agg->dtag_first; 9277 } 9278 9279 if (prev != NULL) { 9280 offs = prev->dta_rec.dtrd_offset + 9281 prev->dta_rec.dtrd_size; 9282 } else { 9283 offs = sizeof (dtrace_epid_t); 9284 } 9285 wastuple = 0; 9286 } else { 9287 if (!act->dta_intuple) 9288 ecb->dte_size = offs + rec->dtrd_size; 9289 9290 offs += rec->dtrd_size; 9291 } 9292 9293 wastuple = act->dta_intuple; 9294 } 9295 9296 if ((act = ecb->dte_action) != NULL && 9297 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 9298 ecb->dte_size == sizeof (dtrace_epid_t)) { 9299 /* 9300 * If the size is still sizeof (dtrace_epid_t), then all 9301 * actions store no data; set the size to 0. 9302 */ 9303 ecb->dte_alignment = maxalign; 9304 ecb->dte_size = 0; 9305 9306 /* 9307 * If the needed space is still sizeof (dtrace_epid_t), then 9308 * all actions need no additional space; set the needed 9309 * size to 0. 9310 */ 9311 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 9312 ecb->dte_needed = 0; 9313 9314 return; 9315 } 9316 9317 /* 9318 * Set our alignment, and make sure that the dte_size and dte_needed 9319 * are aligned to the size of an EPID. 9320 */ 9321 ecb->dte_alignment = maxalign; 9322 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 9323 ~(sizeof (dtrace_epid_t) - 1); 9324 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 9325 ~(sizeof (dtrace_epid_t) - 1); 9326 ASSERT(ecb->dte_size <= ecb->dte_needed); 9327 } 9328 9329 static dtrace_action_t * 9330 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9331 { 9332 dtrace_aggregation_t *agg; 9333 size_t size = sizeof (uint64_t); 9334 int ntuple = desc->dtad_ntuple; 9335 dtrace_action_t *act; 9336 dtrace_recdesc_t *frec; 9337 dtrace_aggid_t aggid; 9338 dtrace_state_t *state = ecb->dte_state; 9339 9340 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 9341 agg->dtag_ecb = ecb; 9342 9343 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 9344 9345 switch (desc->dtad_kind) { 9346 case DTRACEAGG_MIN: 9347 agg->dtag_initial = INT64_MAX; 9348 agg->dtag_aggregate = dtrace_aggregate_min; 9349 break; 9350 9351 case DTRACEAGG_MAX: 9352 agg->dtag_initial = INT64_MIN; 9353 agg->dtag_aggregate = dtrace_aggregate_max; 9354 break; 9355 9356 case DTRACEAGG_COUNT: 9357 agg->dtag_aggregate = dtrace_aggregate_count; 9358 break; 9359 9360 case DTRACEAGG_QUANTIZE: 9361 agg->dtag_aggregate = dtrace_aggregate_quantize; 9362 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 9363 sizeof (uint64_t); 9364 break; 9365 9366 case DTRACEAGG_LQUANTIZE: { 9367 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 9368 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 9369 9370 agg->dtag_initial = desc->dtad_arg; 9371 agg->dtag_aggregate = dtrace_aggregate_lquantize; 9372 9373 if (step == 0 || levels == 0) 9374 goto err; 9375 9376 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 9377 break; 9378 } 9379 9380 case DTRACEAGG_AVG: 9381 agg->dtag_aggregate = dtrace_aggregate_avg; 9382 size = sizeof (uint64_t) * 2; 9383 break; 9384 9385 case DTRACEAGG_STDDEV: 9386 agg->dtag_aggregate = dtrace_aggregate_stddev; 9387 size = sizeof (uint64_t) * 4; 9388 break; 9389 9390 case DTRACEAGG_SUM: 9391 agg->dtag_aggregate = dtrace_aggregate_sum; 9392 break; 9393 9394 default: 9395 goto err; 9396 } 9397 9398 agg->dtag_action.dta_rec.dtrd_size = size; 9399 9400 if (ntuple == 0) 9401 goto err; 9402 9403 /* 9404 * We must make sure that we have enough actions for the n-tuple. 9405 */ 9406 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 9407 if (DTRACEACT_ISAGG(act->dta_kind)) 9408 break; 9409 9410 if (--ntuple == 0) { 9411 /* 9412 * This is the action with which our n-tuple begins. 9413 */ 9414 agg->dtag_first = act; 9415 goto success; 9416 } 9417 } 9418 9419 /* 9420 * This n-tuple is short by ntuple elements. Return failure. 9421 */ 9422 ASSERT(ntuple != 0); 9423 err: 9424 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9425 return (NULL); 9426 9427 success: 9428 /* 9429 * If the last action in the tuple has a size of zero, it's actually 9430 * an expression argument for the aggregating action. 9431 */ 9432 ASSERT(ecb->dte_action_last != NULL); 9433 act = ecb->dte_action_last; 9434 9435 if (act->dta_kind == DTRACEACT_DIFEXPR) { 9436 ASSERT(act->dta_difo != NULL); 9437 9438 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 9439 agg->dtag_hasarg = 1; 9440 } 9441 9442 /* 9443 * We need to allocate an id for this aggregation. 9444 */ 9445 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 9446 VM_BESTFIT | VM_SLEEP); 9447 9448 if (aggid - 1 >= state->dts_naggregations) { 9449 dtrace_aggregation_t **oaggs = state->dts_aggregations; 9450 dtrace_aggregation_t **aggs; 9451 int naggs = state->dts_naggregations << 1; 9452 int onaggs = state->dts_naggregations; 9453 9454 ASSERT(aggid == state->dts_naggregations + 1); 9455 9456 if (naggs == 0) { 9457 ASSERT(oaggs == NULL); 9458 naggs = 1; 9459 } 9460 9461 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 9462 9463 if (oaggs != NULL) { 9464 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 9465 kmem_free(oaggs, onaggs * sizeof (*aggs)); 9466 } 9467 9468 state->dts_aggregations = aggs; 9469 state->dts_naggregations = naggs; 9470 } 9471 9472 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 9473 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 9474 9475 frec = &agg->dtag_first->dta_rec; 9476 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 9477 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 9478 9479 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 9480 ASSERT(!act->dta_intuple); 9481 act->dta_intuple = 1; 9482 } 9483 9484 return (&agg->dtag_action); 9485 } 9486 9487 static void 9488 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 9489 { 9490 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9491 dtrace_state_t *state = ecb->dte_state; 9492 dtrace_aggid_t aggid = agg->dtag_id; 9493 9494 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 9495 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 9496 9497 ASSERT(state->dts_aggregations[aggid - 1] == agg); 9498 state->dts_aggregations[aggid - 1] = NULL; 9499 9500 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9501 } 9502 9503 static int 9504 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9505 { 9506 dtrace_action_t *action, *last; 9507 dtrace_difo_t *dp = desc->dtad_difo; 9508 uint32_t size = 0, align = sizeof (uint8_t), mask; 9509 uint16_t format = 0; 9510 dtrace_recdesc_t *rec; 9511 dtrace_state_t *state = ecb->dte_state; 9512 dtrace_optval_t *opt = state->dts_options, nframes, strsize; 9513 uint64_t arg = desc->dtad_arg; 9514 9515 ASSERT(MUTEX_HELD(&dtrace_lock)); 9516 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 9517 9518 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 9519 /* 9520 * If this is an aggregating action, there must be neither 9521 * a speculate nor a commit on the action chain. 9522 */ 9523 dtrace_action_t *act; 9524 9525 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9526 if (act->dta_kind == DTRACEACT_COMMIT) 9527 return (EINVAL); 9528 9529 if (act->dta_kind == DTRACEACT_SPECULATE) 9530 return (EINVAL); 9531 } 9532 9533 action = dtrace_ecb_aggregation_create(ecb, desc); 9534 9535 if (action == NULL) 9536 return (EINVAL); 9537 } else { 9538 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 9539 (desc->dtad_kind == DTRACEACT_DIFEXPR && 9540 dp != NULL && dp->dtdo_destructive)) { 9541 state->dts_destructive = 1; 9542 } 9543 9544 switch (desc->dtad_kind) { 9545 case DTRACEACT_PRINTF: 9546 case DTRACEACT_PRINTA: 9547 case DTRACEACT_SYSTEM: 9548 case DTRACEACT_FREOPEN: 9549 /* 9550 * We know that our arg is a string -- turn it into a 9551 * format. 9552 */ 9553 if (arg == NULL) { 9554 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); 9555 format = 0; 9556 } else { 9557 ASSERT(arg != NULL); 9558 ASSERT(arg > KERNELBASE); 9559 format = dtrace_format_add(state, 9560 (char *)(uintptr_t)arg); 9561 } 9562 9563 /*FALLTHROUGH*/ 9564 case DTRACEACT_LIBACT: 9565 case DTRACEACT_DIFEXPR: 9566 if (dp == NULL) 9567 return (EINVAL); 9568 9569 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 9570 break; 9571 9572 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 9573 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9574 return (EINVAL); 9575 9576 size = opt[DTRACEOPT_STRSIZE]; 9577 } 9578 9579 break; 9580 9581 case DTRACEACT_STACK: 9582 if ((nframes = arg) == 0) { 9583 nframes = opt[DTRACEOPT_STACKFRAMES]; 9584 ASSERT(nframes > 0); 9585 arg = nframes; 9586 } 9587 9588 size = nframes * sizeof (pc_t); 9589 break; 9590 9591 case DTRACEACT_JSTACK: 9592 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 9593 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 9594 9595 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 9596 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 9597 9598 arg = DTRACE_USTACK_ARG(nframes, strsize); 9599 9600 /*FALLTHROUGH*/ 9601 case DTRACEACT_USTACK: 9602 if (desc->dtad_kind != DTRACEACT_JSTACK && 9603 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 9604 strsize = DTRACE_USTACK_STRSIZE(arg); 9605 nframes = opt[DTRACEOPT_USTACKFRAMES]; 9606 ASSERT(nframes > 0); 9607 arg = DTRACE_USTACK_ARG(nframes, strsize); 9608 } 9609 9610 /* 9611 * Save a slot for the pid. 9612 */ 9613 size = (nframes + 1) * sizeof (uint64_t); 9614 size += DTRACE_USTACK_STRSIZE(arg); 9615 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 9616 9617 break; 9618 9619 case DTRACEACT_SYM: 9620 case DTRACEACT_MOD: 9621 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 9622 sizeof (uint64_t)) || 9623 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9624 return (EINVAL); 9625 break; 9626 9627 case DTRACEACT_USYM: 9628 case DTRACEACT_UMOD: 9629 case DTRACEACT_UADDR: 9630 if (dp == NULL || 9631 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 9632 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9633 return (EINVAL); 9634 9635 /* 9636 * We have a slot for the pid, plus a slot for the 9637 * argument. To keep things simple (aligned with 9638 * bitness-neutral sizing), we store each as a 64-bit 9639 * quantity. 9640 */ 9641 size = 2 * sizeof (uint64_t); 9642 break; 9643 9644 case DTRACEACT_STOP: 9645 case DTRACEACT_BREAKPOINT: 9646 case DTRACEACT_PANIC: 9647 break; 9648 9649 case DTRACEACT_CHILL: 9650 case DTRACEACT_DISCARD: 9651 case DTRACEACT_RAISE: 9652 if (dp == NULL) 9653 return (EINVAL); 9654 break; 9655 9656 case DTRACEACT_EXIT: 9657 if (dp == NULL || 9658 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 9659 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 9660 return (EINVAL); 9661 break; 9662 9663 case DTRACEACT_SPECULATE: 9664 if (ecb->dte_size > sizeof (dtrace_epid_t)) 9665 return (EINVAL); 9666 9667 if (dp == NULL) 9668 return (EINVAL); 9669 9670 state->dts_speculates = 1; 9671 break; 9672 9673 case DTRACEACT_COMMIT: { 9674 dtrace_action_t *act = ecb->dte_action; 9675 9676 for (; act != NULL; act = act->dta_next) { 9677 if (act->dta_kind == DTRACEACT_COMMIT) 9678 return (EINVAL); 9679 } 9680 9681 if (dp == NULL) 9682 return (EINVAL); 9683 break; 9684 } 9685 9686 default: 9687 return (EINVAL); 9688 } 9689 9690 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 9691 /* 9692 * If this is a data-storing action or a speculate, 9693 * we must be sure that there isn't a commit on the 9694 * action chain. 9695 */ 9696 dtrace_action_t *act = ecb->dte_action; 9697 9698 for (; act != NULL; act = act->dta_next) { 9699 if (act->dta_kind == DTRACEACT_COMMIT) 9700 return (EINVAL); 9701 } 9702 } 9703 9704 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 9705 action->dta_rec.dtrd_size = size; 9706 } 9707 9708 action->dta_refcnt = 1; 9709 rec = &action->dta_rec; 9710 size = rec->dtrd_size; 9711 9712 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 9713 if (!(size & mask)) { 9714 align = mask + 1; 9715 break; 9716 } 9717 } 9718 9719 action->dta_kind = desc->dtad_kind; 9720 9721 if ((action->dta_difo = dp) != NULL) 9722 dtrace_difo_hold(dp); 9723 9724 rec->dtrd_action = action->dta_kind; 9725 rec->dtrd_arg = arg; 9726 rec->dtrd_uarg = desc->dtad_uarg; 9727 rec->dtrd_alignment = (uint16_t)align; 9728 rec->dtrd_format = format; 9729 9730 if ((last = ecb->dte_action_last) != NULL) { 9731 ASSERT(ecb->dte_action != NULL); 9732 action->dta_prev = last; 9733 last->dta_next = action; 9734 } else { 9735 ASSERT(ecb->dte_action == NULL); 9736 ecb->dte_action = action; 9737 } 9738 9739 ecb->dte_action_last = action; 9740 9741 return (0); 9742 } 9743 9744 static void 9745 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 9746 { 9747 dtrace_action_t *act = ecb->dte_action, *next; 9748 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 9749 dtrace_difo_t *dp; 9750 uint16_t format; 9751 9752 if (act != NULL && act->dta_refcnt > 1) { 9753 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 9754 act->dta_refcnt--; 9755 } else { 9756 for (; act != NULL; act = next) { 9757 next = act->dta_next; 9758 ASSERT(next != NULL || act == ecb->dte_action_last); 9759 ASSERT(act->dta_refcnt == 1); 9760 9761 if ((format = act->dta_rec.dtrd_format) != 0) 9762 dtrace_format_remove(ecb->dte_state, format); 9763 9764 if ((dp = act->dta_difo) != NULL) 9765 dtrace_difo_release(dp, vstate); 9766 9767 if (DTRACEACT_ISAGG(act->dta_kind)) { 9768 dtrace_ecb_aggregation_destroy(ecb, act); 9769 } else { 9770 kmem_free(act, sizeof (dtrace_action_t)); 9771 } 9772 } 9773 } 9774 9775 ecb->dte_action = NULL; 9776 ecb->dte_action_last = NULL; 9777 ecb->dte_size = sizeof (dtrace_epid_t); 9778 } 9779 9780 static void 9781 dtrace_ecb_disable(dtrace_ecb_t *ecb) 9782 { 9783 /* 9784 * We disable the ECB by removing it from its probe. 9785 */ 9786 dtrace_ecb_t *pecb, *prev = NULL; 9787 dtrace_probe_t *probe = ecb->dte_probe; 9788 9789 ASSERT(MUTEX_HELD(&dtrace_lock)); 9790 9791 if (probe == NULL) { 9792 /* 9793 * This is the NULL probe; there is nothing to disable. 9794 */ 9795 return; 9796 } 9797 9798 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 9799 if (pecb == ecb) 9800 break; 9801 prev = pecb; 9802 } 9803 9804 ASSERT(pecb != NULL); 9805 9806 if (prev == NULL) { 9807 probe->dtpr_ecb = ecb->dte_next; 9808 } else { 9809 prev->dte_next = ecb->dte_next; 9810 } 9811 9812 if (ecb == probe->dtpr_ecb_last) { 9813 ASSERT(ecb->dte_next == NULL); 9814 probe->dtpr_ecb_last = prev; 9815 } 9816 9817 /* 9818 * The ECB has been disconnected from the probe; now sync to assure 9819 * that all CPUs have seen the change before returning. 9820 */ 9821 dtrace_sync(); 9822 9823 if (probe->dtpr_ecb == NULL) { 9824 /* 9825 * That was the last ECB on the probe; clear the predicate 9826 * cache ID for the probe, disable it and sync one more time 9827 * to assure that we'll never hit it again. 9828 */ 9829 dtrace_provider_t *prov = probe->dtpr_provider; 9830 9831 ASSERT(ecb->dte_next == NULL); 9832 ASSERT(probe->dtpr_ecb_last == NULL); 9833 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 9834 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 9835 probe->dtpr_id, probe->dtpr_arg); 9836 dtrace_sync(); 9837 } else { 9838 /* 9839 * There is at least one ECB remaining on the probe. If there 9840 * is _exactly_ one, set the probe's predicate cache ID to be 9841 * the predicate cache ID of the remaining ECB. 9842 */ 9843 ASSERT(probe->dtpr_ecb_last != NULL); 9844 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 9845 9846 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 9847 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 9848 9849 ASSERT(probe->dtpr_ecb->dte_next == NULL); 9850 9851 if (p != NULL) 9852 probe->dtpr_predcache = p->dtp_cacheid; 9853 } 9854 9855 ecb->dte_next = NULL; 9856 } 9857 } 9858 9859 static void 9860 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 9861 { 9862 dtrace_state_t *state = ecb->dte_state; 9863 dtrace_vstate_t *vstate = &state->dts_vstate; 9864 dtrace_predicate_t *pred; 9865 dtrace_epid_t epid = ecb->dte_epid; 9866 9867 ASSERT(MUTEX_HELD(&dtrace_lock)); 9868 ASSERT(ecb->dte_next == NULL); 9869 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 9870 9871 if ((pred = ecb->dte_predicate) != NULL) 9872 dtrace_predicate_release(pred, vstate); 9873 9874 dtrace_ecb_action_remove(ecb); 9875 9876 ASSERT(state->dts_ecbs[epid - 1] == ecb); 9877 state->dts_ecbs[epid - 1] = NULL; 9878 9879 kmem_free(ecb, sizeof (dtrace_ecb_t)); 9880 } 9881 9882 static dtrace_ecb_t * 9883 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 9884 dtrace_enabling_t *enab) 9885 { 9886 dtrace_ecb_t *ecb; 9887 dtrace_predicate_t *pred; 9888 dtrace_actdesc_t *act; 9889 dtrace_provider_t *prov; 9890 dtrace_ecbdesc_t *desc = enab->dten_current; 9891 9892 ASSERT(MUTEX_HELD(&dtrace_lock)); 9893 ASSERT(state != NULL); 9894 9895 ecb = dtrace_ecb_add(state, probe); 9896 ecb->dte_uarg = desc->dted_uarg; 9897 9898 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 9899 dtrace_predicate_hold(pred); 9900 ecb->dte_predicate = pred; 9901 } 9902 9903 if (probe != NULL) { 9904 /* 9905 * If the provider shows more leg than the consumer is old 9906 * enough to see, we need to enable the appropriate implicit 9907 * predicate bits to prevent the ecb from activating at 9908 * revealing times. 9909 * 9910 * Providers specifying DTRACE_PRIV_USER at register time 9911 * are stating that they need the /proc-style privilege 9912 * model to be enforced, and this is what DTRACE_COND_OWNER 9913 * and DTRACE_COND_ZONEOWNER will then do at probe time. 9914 */ 9915 prov = probe->dtpr_provider; 9916 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 9917 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 9918 ecb->dte_cond |= DTRACE_COND_OWNER; 9919 9920 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 9921 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 9922 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 9923 9924 /* 9925 * If the provider shows us kernel innards and the user 9926 * is lacking sufficient privilege, enable the 9927 * DTRACE_COND_USERMODE implicit predicate. 9928 */ 9929 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 9930 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 9931 ecb->dte_cond |= DTRACE_COND_USERMODE; 9932 } 9933 9934 if (dtrace_ecb_create_cache != NULL) { 9935 /* 9936 * If we have a cached ecb, we'll use its action list instead 9937 * of creating our own (saving both time and space). 9938 */ 9939 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 9940 dtrace_action_t *act = cached->dte_action; 9941 9942 if (act != NULL) { 9943 ASSERT(act->dta_refcnt > 0); 9944 act->dta_refcnt++; 9945 ecb->dte_action = act; 9946 ecb->dte_action_last = cached->dte_action_last; 9947 ecb->dte_needed = cached->dte_needed; 9948 ecb->dte_size = cached->dte_size; 9949 ecb->dte_alignment = cached->dte_alignment; 9950 } 9951 9952 return (ecb); 9953 } 9954 9955 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 9956 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 9957 dtrace_ecb_destroy(ecb); 9958 return (NULL); 9959 } 9960 } 9961 9962 dtrace_ecb_resize(ecb); 9963 9964 return (dtrace_ecb_create_cache = ecb); 9965 } 9966 9967 static int 9968 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 9969 { 9970 dtrace_ecb_t *ecb; 9971 dtrace_enabling_t *enab = arg; 9972 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 9973 9974 ASSERT(state != NULL); 9975 9976 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 9977 /* 9978 * This probe was created in a generation for which this 9979 * enabling has previously created ECBs; we don't want to 9980 * enable it again, so just kick out. 9981 */ 9982 return (DTRACE_MATCH_NEXT); 9983 } 9984 9985 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 9986 return (DTRACE_MATCH_DONE); 9987 9988 if (dtrace_ecb_enable(ecb) < 0) 9989 return (DTRACE_MATCH_FAIL); 9990 9991 return (DTRACE_MATCH_NEXT); 9992 } 9993 9994 static dtrace_ecb_t * 9995 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 9996 { 9997 dtrace_ecb_t *ecb; 9998 9999 ASSERT(MUTEX_HELD(&dtrace_lock)); 10000 10001 if (id == 0 || id > state->dts_necbs) 10002 return (NULL); 10003 10004 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 10005 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 10006 10007 return (state->dts_ecbs[id - 1]); 10008 } 10009 10010 static dtrace_aggregation_t * 10011 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 10012 { 10013 dtrace_aggregation_t *agg; 10014 10015 ASSERT(MUTEX_HELD(&dtrace_lock)); 10016 10017 if (id == 0 || id > state->dts_naggregations) 10018 return (NULL); 10019 10020 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 10021 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 10022 agg->dtag_id == id); 10023 10024 return (state->dts_aggregations[id - 1]); 10025 } 10026 10027 /* 10028 * DTrace Buffer Functions 10029 * 10030 * The following functions manipulate DTrace buffers. Most of these functions 10031 * are called in the context of establishing or processing consumer state; 10032 * exceptions are explicitly noted. 10033 */ 10034 10035 /* 10036 * Note: called from cross call context. This function switches the two 10037 * buffers on a given CPU. The atomicity of this operation is assured by 10038 * disabling interrupts while the actual switch takes place; the disabling of 10039 * interrupts serializes the execution with any execution of dtrace_probe() on 10040 * the same CPU. 10041 */ 10042 static void 10043 dtrace_buffer_switch(dtrace_buffer_t *buf) 10044 { 10045 caddr_t tomax = buf->dtb_tomax; 10046 caddr_t xamot = buf->dtb_xamot; 10047 dtrace_icookie_t cookie; 10048 10049 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10050 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 10051 10052 cookie = dtrace_interrupt_disable(); 10053 buf->dtb_tomax = xamot; 10054 buf->dtb_xamot = tomax; 10055 buf->dtb_xamot_drops = buf->dtb_drops; 10056 buf->dtb_xamot_offset = buf->dtb_offset; 10057 buf->dtb_xamot_errors = buf->dtb_errors; 10058 buf->dtb_xamot_flags = buf->dtb_flags; 10059 buf->dtb_offset = 0; 10060 buf->dtb_drops = 0; 10061 buf->dtb_errors = 0; 10062 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 10063 dtrace_interrupt_enable(cookie); 10064 } 10065 10066 /* 10067 * Note: called from cross call context. This function activates a buffer 10068 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 10069 * is guaranteed by the disabling of interrupts. 10070 */ 10071 static void 10072 dtrace_buffer_activate(dtrace_state_t *state) 10073 { 10074 dtrace_buffer_t *buf; 10075 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 10076 10077 buf = &state->dts_buffer[CPU->cpu_id]; 10078 10079 if (buf->dtb_tomax != NULL) { 10080 /* 10081 * We might like to assert that the buffer is marked inactive, 10082 * but this isn't necessarily true: the buffer for the CPU 10083 * that processes the BEGIN probe has its buffer activated 10084 * manually. In this case, we take the (harmless) action 10085 * re-clearing the bit INACTIVE bit. 10086 */ 10087 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 10088 } 10089 10090 dtrace_interrupt_enable(cookie); 10091 } 10092 10093 static int 10094 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 10095 processorid_t cpu) 10096 { 10097 cpu_t *cp; 10098 dtrace_buffer_t *buf; 10099 10100 ASSERT(MUTEX_HELD(&cpu_lock)); 10101 ASSERT(MUTEX_HELD(&dtrace_lock)); 10102 10103 if (size > dtrace_nonroot_maxsize && 10104 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 10105 return (EFBIG); 10106 10107 cp = cpu_list; 10108 10109 do { 10110 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10111 continue; 10112 10113 buf = &bufs[cp->cpu_id]; 10114 10115 /* 10116 * If there is already a buffer allocated for this CPU, it 10117 * is only possible that this is a DR event. In this case, 10118 * the buffer size must match our specified size. 10119 */ 10120 if (buf->dtb_tomax != NULL) { 10121 ASSERT(buf->dtb_size == size); 10122 continue; 10123 } 10124 10125 ASSERT(buf->dtb_xamot == NULL); 10126 10127 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10128 goto err; 10129 10130 buf->dtb_size = size; 10131 buf->dtb_flags = flags; 10132 buf->dtb_offset = 0; 10133 buf->dtb_drops = 0; 10134 10135 if (flags & DTRACEBUF_NOSWITCH) 10136 continue; 10137 10138 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10139 goto err; 10140 } while ((cp = cp->cpu_next) != cpu_list); 10141 10142 return (0); 10143 10144 err: 10145 cp = cpu_list; 10146 10147 do { 10148 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10149 continue; 10150 10151 buf = &bufs[cp->cpu_id]; 10152 10153 if (buf->dtb_xamot != NULL) { 10154 ASSERT(buf->dtb_tomax != NULL); 10155 ASSERT(buf->dtb_size == size); 10156 kmem_free(buf->dtb_xamot, size); 10157 } 10158 10159 if (buf->dtb_tomax != NULL) { 10160 ASSERT(buf->dtb_size == size); 10161 kmem_free(buf->dtb_tomax, size); 10162 } 10163 10164 buf->dtb_tomax = NULL; 10165 buf->dtb_xamot = NULL; 10166 buf->dtb_size = 0; 10167 } while ((cp = cp->cpu_next) != cpu_list); 10168 10169 return (ENOMEM); 10170 } 10171 10172 /* 10173 * Note: called from probe context. This function just increments the drop 10174 * count on a buffer. It has been made a function to allow for the 10175 * possibility of understanding the source of mysterious drop counts. (A 10176 * problem for which one may be particularly disappointed that DTrace cannot 10177 * be used to understand DTrace.) 10178 */ 10179 static void 10180 dtrace_buffer_drop(dtrace_buffer_t *buf) 10181 { 10182 buf->dtb_drops++; 10183 } 10184 10185 /* 10186 * Note: called from probe context. This function is called to reserve space 10187 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 10188 * mstate. Returns the new offset in the buffer, or a negative value if an 10189 * error has occurred. 10190 */ 10191 static intptr_t 10192 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 10193 dtrace_state_t *state, dtrace_mstate_t *mstate) 10194 { 10195 intptr_t offs = buf->dtb_offset, soffs; 10196 intptr_t woffs; 10197 caddr_t tomax; 10198 size_t total; 10199 10200 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 10201 return (-1); 10202 10203 if ((tomax = buf->dtb_tomax) == NULL) { 10204 dtrace_buffer_drop(buf); 10205 return (-1); 10206 } 10207 10208 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 10209 while (offs & (align - 1)) { 10210 /* 10211 * Assert that our alignment is off by a number which 10212 * is itself sizeof (uint32_t) aligned. 10213 */ 10214 ASSERT(!((align - (offs & (align - 1))) & 10215 (sizeof (uint32_t) - 1))); 10216 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10217 offs += sizeof (uint32_t); 10218 } 10219 10220 if ((soffs = offs + needed) > buf->dtb_size) { 10221 dtrace_buffer_drop(buf); 10222 return (-1); 10223 } 10224 10225 if (mstate == NULL) 10226 return (offs); 10227 10228 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 10229 mstate->dtms_scratch_size = buf->dtb_size - soffs; 10230 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10231 10232 return (offs); 10233 } 10234 10235 if (buf->dtb_flags & DTRACEBUF_FILL) { 10236 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 10237 (buf->dtb_flags & DTRACEBUF_FULL)) 10238 return (-1); 10239 goto out; 10240 } 10241 10242 total = needed + (offs & (align - 1)); 10243 10244 /* 10245 * For a ring buffer, life is quite a bit more complicated. Before 10246 * we can store any padding, we need to adjust our wrapping offset. 10247 * (If we've never before wrapped or we're not about to, no adjustment 10248 * is required.) 10249 */ 10250 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 10251 offs + total > buf->dtb_size) { 10252 woffs = buf->dtb_xamot_offset; 10253 10254 if (offs + total > buf->dtb_size) { 10255 /* 10256 * We can't fit in the end of the buffer. First, a 10257 * sanity check that we can fit in the buffer at all. 10258 */ 10259 if (total > buf->dtb_size) { 10260 dtrace_buffer_drop(buf); 10261 return (-1); 10262 } 10263 10264 /* 10265 * We're going to be storing at the top of the buffer, 10266 * so now we need to deal with the wrapped offset. We 10267 * only reset our wrapped offset to 0 if it is 10268 * currently greater than the current offset. If it 10269 * is less than the current offset, it is because a 10270 * previous allocation induced a wrap -- but the 10271 * allocation didn't subsequently take the space due 10272 * to an error or false predicate evaluation. In this 10273 * case, we'll just leave the wrapped offset alone: if 10274 * the wrapped offset hasn't been advanced far enough 10275 * for this allocation, it will be adjusted in the 10276 * lower loop. 10277 */ 10278 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 10279 if (woffs >= offs) 10280 woffs = 0; 10281 } else { 10282 woffs = 0; 10283 } 10284 10285 /* 10286 * Now we know that we're going to be storing to the 10287 * top of the buffer and that there is room for us 10288 * there. We need to clear the buffer from the current 10289 * offset to the end (there may be old gunk there). 10290 */ 10291 while (offs < buf->dtb_size) 10292 tomax[offs++] = 0; 10293 10294 /* 10295 * We need to set our offset to zero. And because we 10296 * are wrapping, we need to set the bit indicating as 10297 * much. We can also adjust our needed space back 10298 * down to the space required by the ECB -- we know 10299 * that the top of the buffer is aligned. 10300 */ 10301 offs = 0; 10302 total = needed; 10303 buf->dtb_flags |= DTRACEBUF_WRAPPED; 10304 } else { 10305 /* 10306 * There is room for us in the buffer, so we simply 10307 * need to check the wrapped offset. 10308 */ 10309 if (woffs < offs) { 10310 /* 10311 * The wrapped offset is less than the offset. 10312 * This can happen if we allocated buffer space 10313 * that induced a wrap, but then we didn't 10314 * subsequently take the space due to an error 10315 * or false predicate evaluation. This is 10316 * okay; we know that _this_ allocation isn't 10317 * going to induce a wrap. We still can't 10318 * reset the wrapped offset to be zero, 10319 * however: the space may have been trashed in 10320 * the previous failed probe attempt. But at 10321 * least the wrapped offset doesn't need to 10322 * be adjusted at all... 10323 */ 10324 goto out; 10325 } 10326 } 10327 10328 while (offs + total > woffs) { 10329 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 10330 size_t size; 10331 10332 if (epid == DTRACE_EPIDNONE) { 10333 size = sizeof (uint32_t); 10334 } else { 10335 ASSERT(epid <= state->dts_necbs); 10336 ASSERT(state->dts_ecbs[epid - 1] != NULL); 10337 10338 size = state->dts_ecbs[epid - 1]->dte_size; 10339 } 10340 10341 ASSERT(woffs + size <= buf->dtb_size); 10342 ASSERT(size != 0); 10343 10344 if (woffs + size == buf->dtb_size) { 10345 /* 10346 * We've reached the end of the buffer; we want 10347 * to set the wrapped offset to 0 and break 10348 * out. However, if the offs is 0, then we're 10349 * in a strange edge-condition: the amount of 10350 * space that we want to reserve plus the size 10351 * of the record that we're overwriting is 10352 * greater than the size of the buffer. This 10353 * is problematic because if we reserve the 10354 * space but subsequently don't consume it (due 10355 * to a failed predicate or error) the wrapped 10356 * offset will be 0 -- yet the EPID at offset 0 10357 * will not be committed. This situation is 10358 * relatively easy to deal with: if we're in 10359 * this case, the buffer is indistinguishable 10360 * from one that hasn't wrapped; we need only 10361 * finish the job by clearing the wrapped bit, 10362 * explicitly setting the offset to be 0, and 10363 * zero'ing out the old data in the buffer. 10364 */ 10365 if (offs == 0) { 10366 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 10367 buf->dtb_offset = 0; 10368 woffs = total; 10369 10370 while (woffs < buf->dtb_size) 10371 tomax[woffs++] = 0; 10372 } 10373 10374 woffs = 0; 10375 break; 10376 } 10377 10378 woffs += size; 10379 } 10380 10381 /* 10382 * We have a wrapped offset. It may be that the wrapped offset 10383 * has become zero -- that's okay. 10384 */ 10385 buf->dtb_xamot_offset = woffs; 10386 } 10387 10388 out: 10389 /* 10390 * Now we can plow the buffer with any necessary padding. 10391 */ 10392 while (offs & (align - 1)) { 10393 /* 10394 * Assert that our alignment is off by a number which 10395 * is itself sizeof (uint32_t) aligned. 10396 */ 10397 ASSERT(!((align - (offs & (align - 1))) & 10398 (sizeof (uint32_t) - 1))); 10399 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10400 offs += sizeof (uint32_t); 10401 } 10402 10403 if (buf->dtb_flags & DTRACEBUF_FILL) { 10404 if (offs + needed > buf->dtb_size - state->dts_reserve) { 10405 buf->dtb_flags |= DTRACEBUF_FULL; 10406 return (-1); 10407 } 10408 } 10409 10410 if (mstate == NULL) 10411 return (offs); 10412 10413 /* 10414 * For ring buffers and fill buffers, the scratch space is always 10415 * the inactive buffer. 10416 */ 10417 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 10418 mstate->dtms_scratch_size = buf->dtb_size; 10419 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10420 10421 return (offs); 10422 } 10423 10424 static void 10425 dtrace_buffer_polish(dtrace_buffer_t *buf) 10426 { 10427 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 10428 ASSERT(MUTEX_HELD(&dtrace_lock)); 10429 10430 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 10431 return; 10432 10433 /* 10434 * We need to polish the ring buffer. There are three cases: 10435 * 10436 * - The first (and presumably most common) is that there is no gap 10437 * between the buffer offset and the wrapped offset. In this case, 10438 * there is nothing in the buffer that isn't valid data; we can 10439 * mark the buffer as polished and return. 10440 * 10441 * - The second (less common than the first but still more common 10442 * than the third) is that there is a gap between the buffer offset 10443 * and the wrapped offset, and the wrapped offset is larger than the 10444 * buffer offset. This can happen because of an alignment issue, or 10445 * can happen because of a call to dtrace_buffer_reserve() that 10446 * didn't subsequently consume the buffer space. In this case, 10447 * we need to zero the data from the buffer offset to the wrapped 10448 * offset. 10449 * 10450 * - The third (and least common) is that there is a gap between the 10451 * buffer offset and the wrapped offset, but the wrapped offset is 10452 * _less_ than the buffer offset. This can only happen because a 10453 * call to dtrace_buffer_reserve() induced a wrap, but the space 10454 * was not subsequently consumed. In this case, we need to zero the 10455 * space from the offset to the end of the buffer _and_ from the 10456 * top of the buffer to the wrapped offset. 10457 */ 10458 if (buf->dtb_offset < buf->dtb_xamot_offset) { 10459 bzero(buf->dtb_tomax + buf->dtb_offset, 10460 buf->dtb_xamot_offset - buf->dtb_offset); 10461 } 10462 10463 if (buf->dtb_offset > buf->dtb_xamot_offset) { 10464 bzero(buf->dtb_tomax + buf->dtb_offset, 10465 buf->dtb_size - buf->dtb_offset); 10466 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 10467 } 10468 } 10469 10470 static void 10471 dtrace_buffer_free(dtrace_buffer_t *bufs) 10472 { 10473 int i; 10474 10475 for (i = 0; i < NCPU; i++) { 10476 dtrace_buffer_t *buf = &bufs[i]; 10477 10478 if (buf->dtb_tomax == NULL) { 10479 ASSERT(buf->dtb_xamot == NULL); 10480 ASSERT(buf->dtb_size == 0); 10481 continue; 10482 } 10483 10484 if (buf->dtb_xamot != NULL) { 10485 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10486 kmem_free(buf->dtb_xamot, buf->dtb_size); 10487 } 10488 10489 kmem_free(buf->dtb_tomax, buf->dtb_size); 10490 buf->dtb_size = 0; 10491 buf->dtb_tomax = NULL; 10492 buf->dtb_xamot = NULL; 10493 } 10494 } 10495 10496 /* 10497 * DTrace Enabling Functions 10498 */ 10499 static dtrace_enabling_t * 10500 dtrace_enabling_create(dtrace_vstate_t *vstate) 10501 { 10502 dtrace_enabling_t *enab; 10503 10504 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 10505 enab->dten_vstate = vstate; 10506 10507 return (enab); 10508 } 10509 10510 static void 10511 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 10512 { 10513 dtrace_ecbdesc_t **ndesc; 10514 size_t osize, nsize; 10515 10516 /* 10517 * We can't add to enablings after we've enabled them, or after we've 10518 * retained them. 10519 */ 10520 ASSERT(enab->dten_probegen == 0); 10521 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 10522 10523 if (enab->dten_ndesc < enab->dten_maxdesc) { 10524 enab->dten_desc[enab->dten_ndesc++] = ecb; 10525 return; 10526 } 10527 10528 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 10529 10530 if (enab->dten_maxdesc == 0) { 10531 enab->dten_maxdesc = 1; 10532 } else { 10533 enab->dten_maxdesc <<= 1; 10534 } 10535 10536 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 10537 10538 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 10539 ndesc = kmem_zalloc(nsize, KM_SLEEP); 10540 bcopy(enab->dten_desc, ndesc, osize); 10541 kmem_free(enab->dten_desc, osize); 10542 10543 enab->dten_desc = ndesc; 10544 enab->dten_desc[enab->dten_ndesc++] = ecb; 10545 } 10546 10547 static void 10548 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 10549 dtrace_probedesc_t *pd) 10550 { 10551 dtrace_ecbdesc_t *new; 10552 dtrace_predicate_t *pred; 10553 dtrace_actdesc_t *act; 10554 10555 /* 10556 * We're going to create a new ECB description that matches the 10557 * specified ECB in every way, but has the specified probe description. 10558 */ 10559 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 10560 10561 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 10562 dtrace_predicate_hold(pred); 10563 10564 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 10565 dtrace_actdesc_hold(act); 10566 10567 new->dted_action = ecb->dted_action; 10568 new->dted_pred = ecb->dted_pred; 10569 new->dted_probe = *pd; 10570 new->dted_uarg = ecb->dted_uarg; 10571 10572 dtrace_enabling_add(enab, new); 10573 } 10574 10575 static void 10576 dtrace_enabling_dump(dtrace_enabling_t *enab) 10577 { 10578 int i; 10579 10580 for (i = 0; i < enab->dten_ndesc; i++) { 10581 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 10582 10583 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 10584 desc->dtpd_provider, desc->dtpd_mod, 10585 desc->dtpd_func, desc->dtpd_name); 10586 } 10587 } 10588 10589 static void 10590 dtrace_enabling_destroy(dtrace_enabling_t *enab) 10591 { 10592 int i; 10593 dtrace_ecbdesc_t *ep; 10594 dtrace_vstate_t *vstate = enab->dten_vstate; 10595 10596 ASSERT(MUTEX_HELD(&dtrace_lock)); 10597 10598 for (i = 0; i < enab->dten_ndesc; i++) { 10599 dtrace_actdesc_t *act, *next; 10600 dtrace_predicate_t *pred; 10601 10602 ep = enab->dten_desc[i]; 10603 10604 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 10605 dtrace_predicate_release(pred, vstate); 10606 10607 for (act = ep->dted_action; act != NULL; act = next) { 10608 next = act->dtad_next; 10609 dtrace_actdesc_release(act, vstate); 10610 } 10611 10612 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 10613 } 10614 10615 kmem_free(enab->dten_desc, 10616 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 10617 10618 /* 10619 * If this was a retained enabling, decrement the dts_nretained count 10620 * and take it off of the dtrace_retained list. 10621 */ 10622 if (enab->dten_prev != NULL || enab->dten_next != NULL || 10623 dtrace_retained == enab) { 10624 ASSERT(enab->dten_vstate->dtvs_state != NULL); 10625 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 10626 enab->dten_vstate->dtvs_state->dts_nretained--; 10627 dtrace_retained_gen++; 10628 } 10629 10630 if (enab->dten_prev == NULL) { 10631 if (dtrace_retained == enab) { 10632 dtrace_retained = enab->dten_next; 10633 10634 if (dtrace_retained != NULL) 10635 dtrace_retained->dten_prev = NULL; 10636 } 10637 } else { 10638 ASSERT(enab != dtrace_retained); 10639 ASSERT(dtrace_retained != NULL); 10640 enab->dten_prev->dten_next = enab->dten_next; 10641 } 10642 10643 if (enab->dten_next != NULL) { 10644 ASSERT(dtrace_retained != NULL); 10645 enab->dten_next->dten_prev = enab->dten_prev; 10646 } 10647 10648 kmem_free(enab, sizeof (dtrace_enabling_t)); 10649 } 10650 10651 static int 10652 dtrace_enabling_retain(dtrace_enabling_t *enab) 10653 { 10654 dtrace_state_t *state; 10655 10656 ASSERT(MUTEX_HELD(&dtrace_lock)); 10657 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 10658 ASSERT(enab->dten_vstate != NULL); 10659 10660 state = enab->dten_vstate->dtvs_state; 10661 ASSERT(state != NULL); 10662 10663 /* 10664 * We only allow each state to retain dtrace_retain_max enablings. 10665 */ 10666 if (state->dts_nretained >= dtrace_retain_max) 10667 return (ENOSPC); 10668 10669 state->dts_nretained++; 10670 dtrace_retained_gen++; 10671 10672 if (dtrace_retained == NULL) { 10673 dtrace_retained = enab; 10674 return (0); 10675 } 10676 10677 enab->dten_next = dtrace_retained; 10678 dtrace_retained->dten_prev = enab; 10679 dtrace_retained = enab; 10680 10681 return (0); 10682 } 10683 10684 static int 10685 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 10686 dtrace_probedesc_t *create) 10687 { 10688 dtrace_enabling_t *new, *enab; 10689 int found = 0, err = ENOENT; 10690 10691 ASSERT(MUTEX_HELD(&dtrace_lock)); 10692 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 10693 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 10694 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 10695 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 10696 10697 new = dtrace_enabling_create(&state->dts_vstate); 10698 10699 /* 10700 * Iterate over all retained enablings, looking for enablings that 10701 * match the specified state. 10702 */ 10703 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 10704 int i; 10705 10706 /* 10707 * dtvs_state can only be NULL for helper enablings -- and 10708 * helper enablings can't be retained. 10709 */ 10710 ASSERT(enab->dten_vstate->dtvs_state != NULL); 10711 10712 if (enab->dten_vstate->dtvs_state != state) 10713 continue; 10714 10715 /* 10716 * Now iterate over each probe description; we're looking for 10717 * an exact match to the specified probe description. 10718 */ 10719 for (i = 0; i < enab->dten_ndesc; i++) { 10720 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 10721 dtrace_probedesc_t *pd = &ep->dted_probe; 10722 10723 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 10724 continue; 10725 10726 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 10727 continue; 10728 10729 if (strcmp(pd->dtpd_func, match->dtpd_func)) 10730 continue; 10731 10732 if (strcmp(pd->dtpd_name, match->dtpd_name)) 10733 continue; 10734 10735 /* 10736 * We have a winning probe! Add it to our growing 10737 * enabling. 10738 */ 10739 found = 1; 10740 dtrace_enabling_addlike(new, ep, create); 10741 } 10742 } 10743 10744 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 10745 dtrace_enabling_destroy(new); 10746 return (err); 10747 } 10748 10749 return (0); 10750 } 10751 10752 static void 10753 dtrace_enabling_retract(dtrace_state_t *state) 10754 { 10755 dtrace_enabling_t *enab, *next; 10756 10757 ASSERT(MUTEX_HELD(&dtrace_lock)); 10758 10759 /* 10760 * Iterate over all retained enablings, destroy the enablings retained 10761 * for the specified state. 10762 */ 10763 for (enab = dtrace_retained; enab != NULL; enab = next) { 10764 next = enab->dten_next; 10765 10766 /* 10767 * dtvs_state can only be NULL for helper enablings -- and 10768 * helper enablings can't be retained. 10769 */ 10770 ASSERT(enab->dten_vstate->dtvs_state != NULL); 10771 10772 if (enab->dten_vstate->dtvs_state == state) { 10773 ASSERT(state->dts_nretained > 0); 10774 dtrace_enabling_destroy(enab); 10775 } 10776 } 10777 10778 ASSERT(state->dts_nretained == 0); 10779 } 10780 10781 static int 10782 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 10783 { 10784 int i = 0; 10785 int total_matched = 0, matched = 0; 10786 10787 ASSERT(MUTEX_HELD(&cpu_lock)); 10788 ASSERT(MUTEX_HELD(&dtrace_lock)); 10789 10790 for (i = 0; i < enab->dten_ndesc; i++) { 10791 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 10792 10793 enab->dten_current = ep; 10794 enab->dten_error = 0; 10795 10796 /* 10797 * If a provider failed to enable a probe then get out and 10798 * let the consumer know we failed. 10799 */ 10800 if ((matched = dtrace_probe_enable(&ep->dted_probe, enab)) < 0) 10801 return (EBUSY); 10802 10803 total_matched += matched; 10804 10805 if (enab->dten_error != 0) { 10806 /* 10807 * If we get an error half-way through enabling the 10808 * probes, we kick out -- perhaps with some number of 10809 * them enabled. Leaving enabled probes enabled may 10810 * be slightly confusing for user-level, but we expect 10811 * that no one will attempt to actually drive on in 10812 * the face of such errors. If this is an anonymous 10813 * enabling (indicated with a NULL nmatched pointer), 10814 * we cmn_err() a message. We aren't expecting to 10815 * get such an error -- such as it can exist at all, 10816 * it would be a result of corrupted DOF in the driver 10817 * properties. 10818 */ 10819 if (nmatched == NULL) { 10820 cmn_err(CE_WARN, "dtrace_enabling_match() " 10821 "error on %p: %d", (void *)ep, 10822 enab->dten_error); 10823 } 10824 10825 return (enab->dten_error); 10826 } 10827 } 10828 10829 enab->dten_probegen = dtrace_probegen; 10830 if (nmatched != NULL) 10831 *nmatched = total_matched; 10832 10833 return (0); 10834 } 10835 10836 static void 10837 dtrace_enabling_matchall(void) 10838 { 10839 dtrace_enabling_t *enab; 10840 10841 mutex_enter(&cpu_lock); 10842 mutex_enter(&dtrace_lock); 10843 10844 /* 10845 * Iterate over all retained enablings to see if any probes match 10846 * against them. We only perform this operation on enablings for which 10847 * we have sufficient permissions by virtue of being in the global zone 10848 * or in the same zone as the DTrace client. Because we can be called 10849 * after dtrace_detach() has been called, we cannot assert that there 10850 * are retained enablings. We can safely load from dtrace_retained, 10851 * however: the taskq_destroy() at the end of dtrace_detach() will 10852 * block pending our completion. 10853 */ 10854 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 10855 dtrace_cred_t *dcr = &enab->dten_vstate->dtvs_state->dts_cred; 10856 cred_t *cr = dcr->dcr_cred; 10857 zoneid_t zone = cr != NULL ? crgetzoneid(cr) : 0; 10858 10859 if ((dcr->dcr_visible & DTRACE_CRV_ALLZONE) || (cr != NULL && 10860 (zone == GLOBAL_ZONEID || getzoneid() == zone))) 10861 (void) dtrace_enabling_match(enab, NULL); 10862 } 10863 10864 mutex_exit(&dtrace_lock); 10865 mutex_exit(&cpu_lock); 10866 } 10867 10868 /* 10869 * If an enabling is to be enabled without having matched probes (that is, if 10870 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 10871 * enabling must be _primed_ by creating an ECB for every ECB description. 10872 * This must be done to assure that we know the number of speculations, the 10873 * number of aggregations, the minimum buffer size needed, etc. before we 10874 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 10875 * enabling any probes, we create ECBs for every ECB decription, but with a 10876 * NULL probe -- which is exactly what this function does. 10877 */ 10878 static void 10879 dtrace_enabling_prime(dtrace_state_t *state) 10880 { 10881 dtrace_enabling_t *enab; 10882 int i; 10883 10884 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 10885 ASSERT(enab->dten_vstate->dtvs_state != NULL); 10886 10887 if (enab->dten_vstate->dtvs_state != state) 10888 continue; 10889 10890 /* 10891 * We don't want to prime an enabling more than once, lest 10892 * we allow a malicious user to induce resource exhaustion. 10893 * (The ECBs that result from priming an enabling aren't 10894 * leaked -- but they also aren't deallocated until the 10895 * consumer state is destroyed.) 10896 */ 10897 if (enab->dten_primed) 10898 continue; 10899 10900 for (i = 0; i < enab->dten_ndesc; i++) { 10901 enab->dten_current = enab->dten_desc[i]; 10902 (void) dtrace_probe_enable(NULL, enab); 10903 } 10904 10905 enab->dten_primed = 1; 10906 } 10907 } 10908 10909 /* 10910 * Called to indicate that probes should be provided due to retained 10911 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 10912 * must take an initial lap through the enabling calling the dtps_provide() 10913 * entry point explicitly to allow for autocreated probes. 10914 */ 10915 static void 10916 dtrace_enabling_provide(dtrace_provider_t *prv) 10917 { 10918 int i, all = 0; 10919 dtrace_probedesc_t desc; 10920 dtrace_genid_t gen; 10921 10922 ASSERT(MUTEX_HELD(&dtrace_lock)); 10923 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 10924 10925 if (prv == NULL) { 10926 all = 1; 10927 prv = dtrace_provider; 10928 } 10929 10930 do { 10931 dtrace_enabling_t *enab; 10932 void *parg = prv->dtpv_arg; 10933 10934 retry: 10935 gen = dtrace_retained_gen; 10936 for (enab = dtrace_retained; enab != NULL; 10937 enab = enab->dten_next) { 10938 for (i = 0; i < enab->dten_ndesc; i++) { 10939 desc = enab->dten_desc[i]->dted_probe; 10940 mutex_exit(&dtrace_lock); 10941 prv->dtpv_pops.dtps_provide(parg, &desc); 10942 mutex_enter(&dtrace_lock); 10943 /* 10944 * Process the retained enablings again if 10945 * they have changed while we weren't holding 10946 * dtrace_lock. 10947 */ 10948 if (gen != dtrace_retained_gen) 10949 goto retry; 10950 } 10951 } 10952 } while (all && (prv = prv->dtpv_next) != NULL); 10953 10954 mutex_exit(&dtrace_lock); 10955 dtrace_probe_provide(NULL, all ? NULL : prv); 10956 mutex_enter(&dtrace_lock); 10957 } 10958 10959 /* 10960 * DTrace DOF Functions 10961 */ 10962 /*ARGSUSED*/ 10963 static void 10964 dtrace_dof_error(dof_hdr_t *dof, const char *str) 10965 { 10966 if (dtrace_err_verbose) 10967 cmn_err(CE_WARN, "failed to process DOF: %s", str); 10968 10969 #ifdef DTRACE_ERRDEBUG 10970 dtrace_errdebug(str); 10971 #endif 10972 } 10973 10974 /* 10975 * Create DOF out of a currently enabled state. Right now, we only create 10976 * DOF containing the run-time options -- but this could be expanded to create 10977 * complete DOF representing the enabled state. 10978 */ 10979 static dof_hdr_t * 10980 dtrace_dof_create(dtrace_state_t *state) 10981 { 10982 dof_hdr_t *dof; 10983 dof_sec_t *sec; 10984 dof_optdesc_t *opt; 10985 int i, len = sizeof (dof_hdr_t) + 10986 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 10987 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 10988 10989 ASSERT(MUTEX_HELD(&dtrace_lock)); 10990 10991 dof = kmem_zalloc(len, KM_SLEEP); 10992 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 10993 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 10994 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 10995 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 10996 10997 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 10998 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 10999 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 11000 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 11001 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 11002 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 11003 11004 dof->dofh_flags = 0; 11005 dof->dofh_hdrsize = sizeof (dof_hdr_t); 11006 dof->dofh_secsize = sizeof (dof_sec_t); 11007 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 11008 dof->dofh_secoff = sizeof (dof_hdr_t); 11009 dof->dofh_loadsz = len; 11010 dof->dofh_filesz = len; 11011 dof->dofh_pad = 0; 11012 11013 /* 11014 * Fill in the option section header... 11015 */ 11016 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 11017 sec->dofs_type = DOF_SECT_OPTDESC; 11018 sec->dofs_align = sizeof (uint64_t); 11019 sec->dofs_flags = DOF_SECF_LOAD; 11020 sec->dofs_entsize = sizeof (dof_optdesc_t); 11021 11022 opt = (dof_optdesc_t *)((uintptr_t)sec + 11023 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 11024 11025 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 11026 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11027 11028 for (i = 0; i < DTRACEOPT_MAX; i++) { 11029 opt[i].dofo_option = i; 11030 opt[i].dofo_strtab = DOF_SECIDX_NONE; 11031 opt[i].dofo_value = state->dts_options[i]; 11032 } 11033 11034 return (dof); 11035 } 11036 11037 static dof_hdr_t * 11038 dtrace_dof_copyin(uintptr_t uarg, int *errp) 11039 { 11040 dof_hdr_t hdr, *dof; 11041 11042 ASSERT(!MUTEX_HELD(&dtrace_lock)); 11043 11044 /* 11045 * First, we're going to copyin() the sizeof (dof_hdr_t). 11046 */ 11047 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 11048 dtrace_dof_error(NULL, "failed to copyin DOF header"); 11049 *errp = EFAULT; 11050 return (NULL); 11051 } 11052 11053 /* 11054 * Now we'll allocate the entire DOF and copy it in -- provided 11055 * that the length isn't outrageous. 11056 */ 11057 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 11058 dtrace_dof_error(&hdr, "load size exceeds maximum"); 11059 *errp = E2BIG; 11060 return (NULL); 11061 } 11062 11063 if (hdr.dofh_loadsz < sizeof (hdr)) { 11064 dtrace_dof_error(&hdr, "invalid load size"); 11065 *errp = EINVAL; 11066 return (NULL); 11067 } 11068 11069 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 11070 11071 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 11072 dof->dofh_loadsz != hdr.dofh_loadsz) { 11073 kmem_free(dof, hdr.dofh_loadsz); 11074 *errp = EFAULT; 11075 return (NULL); 11076 } 11077 11078 return (dof); 11079 } 11080 11081 static dof_hdr_t * 11082 dtrace_dof_property(const char *name) 11083 { 11084 uchar_t *buf; 11085 uint64_t loadsz; 11086 unsigned int len, i; 11087 dof_hdr_t *dof; 11088 11089 /* 11090 * Unfortunately, array of values in .conf files are always (and 11091 * only) interpreted to be integer arrays. We must read our DOF 11092 * as an integer array, and then squeeze it into a byte array. 11093 */ 11094 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 11095 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 11096 return (NULL); 11097 11098 for (i = 0; i < len; i++) 11099 buf[i] = (uchar_t)(((int *)buf)[i]); 11100 11101 if (len < sizeof (dof_hdr_t)) { 11102 ddi_prop_free(buf); 11103 dtrace_dof_error(NULL, "truncated header"); 11104 return (NULL); 11105 } 11106 11107 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 11108 ddi_prop_free(buf); 11109 dtrace_dof_error(NULL, "truncated DOF"); 11110 return (NULL); 11111 } 11112 11113 if (loadsz >= dtrace_dof_maxsize) { 11114 ddi_prop_free(buf); 11115 dtrace_dof_error(NULL, "oversized DOF"); 11116 return (NULL); 11117 } 11118 11119 dof = kmem_alloc(loadsz, KM_SLEEP); 11120 bcopy(buf, dof, loadsz); 11121 ddi_prop_free(buf); 11122 11123 return (dof); 11124 } 11125 11126 static void 11127 dtrace_dof_destroy(dof_hdr_t *dof) 11128 { 11129 kmem_free(dof, dof->dofh_loadsz); 11130 } 11131 11132 /* 11133 * Return the dof_sec_t pointer corresponding to a given section index. If the 11134 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 11135 * a type other than DOF_SECT_NONE is specified, the header is checked against 11136 * this type and NULL is returned if the types do not match. 11137 */ 11138 static dof_sec_t * 11139 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 11140 { 11141 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 11142 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 11143 11144 if (i >= dof->dofh_secnum) { 11145 dtrace_dof_error(dof, "referenced section index is invalid"); 11146 return (NULL); 11147 } 11148 11149 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 11150 dtrace_dof_error(dof, "referenced section is not loadable"); 11151 return (NULL); 11152 } 11153 11154 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 11155 dtrace_dof_error(dof, "referenced section is the wrong type"); 11156 return (NULL); 11157 } 11158 11159 return (sec); 11160 } 11161 11162 static dtrace_probedesc_t * 11163 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 11164 { 11165 dof_probedesc_t *probe; 11166 dof_sec_t *strtab; 11167 uintptr_t daddr = (uintptr_t)dof; 11168 uintptr_t str; 11169 size_t size; 11170 11171 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 11172 dtrace_dof_error(dof, "invalid probe section"); 11173 return (NULL); 11174 } 11175 11176 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11177 dtrace_dof_error(dof, "bad alignment in probe description"); 11178 return (NULL); 11179 } 11180 11181 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 11182 dtrace_dof_error(dof, "truncated probe description"); 11183 return (NULL); 11184 } 11185 11186 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 11187 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 11188 11189 if (strtab == NULL) 11190 return (NULL); 11191 11192 str = daddr + strtab->dofs_offset; 11193 size = strtab->dofs_size; 11194 11195 if (probe->dofp_provider >= strtab->dofs_size) { 11196 dtrace_dof_error(dof, "corrupt probe provider"); 11197 return (NULL); 11198 } 11199 11200 (void) strncpy(desc->dtpd_provider, 11201 (char *)(str + probe->dofp_provider), 11202 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 11203 11204 if (probe->dofp_mod >= strtab->dofs_size) { 11205 dtrace_dof_error(dof, "corrupt probe module"); 11206 return (NULL); 11207 } 11208 11209 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 11210 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 11211 11212 if (probe->dofp_func >= strtab->dofs_size) { 11213 dtrace_dof_error(dof, "corrupt probe function"); 11214 return (NULL); 11215 } 11216 11217 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 11218 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 11219 11220 if (probe->dofp_name >= strtab->dofs_size) { 11221 dtrace_dof_error(dof, "corrupt probe name"); 11222 return (NULL); 11223 } 11224 11225 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 11226 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 11227 11228 return (desc); 11229 } 11230 11231 static dtrace_difo_t * 11232 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11233 cred_t *cr) 11234 { 11235 dtrace_difo_t *dp; 11236 size_t ttl = 0; 11237 dof_difohdr_t *dofd; 11238 uintptr_t daddr = (uintptr_t)dof; 11239 size_t max = dtrace_difo_maxsize; 11240 int i, l, n; 11241 11242 static const struct { 11243 int section; 11244 int bufoffs; 11245 int lenoffs; 11246 int entsize; 11247 int align; 11248 const char *msg; 11249 } difo[] = { 11250 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 11251 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 11252 sizeof (dif_instr_t), "multiple DIF sections" }, 11253 11254 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 11255 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 11256 sizeof (uint64_t), "multiple integer tables" }, 11257 11258 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 11259 offsetof(dtrace_difo_t, dtdo_strlen), 0, 11260 sizeof (char), "multiple string tables" }, 11261 11262 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 11263 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 11264 sizeof (uint_t), "multiple variable tables" }, 11265 11266 { DOF_SECT_NONE, 0, 0, 0, NULL } 11267 }; 11268 11269 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 11270 dtrace_dof_error(dof, "invalid DIFO header section"); 11271 return (NULL); 11272 } 11273 11274 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11275 dtrace_dof_error(dof, "bad alignment in DIFO header"); 11276 return (NULL); 11277 } 11278 11279 if (sec->dofs_size < sizeof (dof_difohdr_t) || 11280 sec->dofs_size % sizeof (dof_secidx_t)) { 11281 dtrace_dof_error(dof, "bad size in DIFO header"); 11282 return (NULL); 11283 } 11284 11285 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 11286 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 11287 11288 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 11289 dp->dtdo_rtype = dofd->dofd_rtype; 11290 11291 for (l = 0; l < n; l++) { 11292 dof_sec_t *subsec; 11293 void **bufp; 11294 uint32_t *lenp; 11295 11296 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 11297 dofd->dofd_links[l])) == NULL) 11298 goto err; /* invalid section link */ 11299 11300 if (ttl + subsec->dofs_size > max) { 11301 dtrace_dof_error(dof, "exceeds maximum size"); 11302 goto err; 11303 } 11304 11305 ttl += subsec->dofs_size; 11306 11307 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 11308 if (subsec->dofs_type != difo[i].section) 11309 continue; 11310 11311 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 11312 dtrace_dof_error(dof, "section not loaded"); 11313 goto err; 11314 } 11315 11316 if (subsec->dofs_align != difo[i].align) { 11317 dtrace_dof_error(dof, "bad alignment"); 11318 goto err; 11319 } 11320 11321 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 11322 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 11323 11324 if (*bufp != NULL) { 11325 dtrace_dof_error(dof, difo[i].msg); 11326 goto err; 11327 } 11328 11329 if (difo[i].entsize != subsec->dofs_entsize) { 11330 dtrace_dof_error(dof, "entry size mismatch"); 11331 goto err; 11332 } 11333 11334 if (subsec->dofs_entsize != 0 && 11335 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 11336 dtrace_dof_error(dof, "corrupt entry size"); 11337 goto err; 11338 } 11339 11340 *lenp = subsec->dofs_size; 11341 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 11342 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 11343 *bufp, subsec->dofs_size); 11344 11345 if (subsec->dofs_entsize != 0) 11346 *lenp /= subsec->dofs_entsize; 11347 11348 break; 11349 } 11350 11351 /* 11352 * If we encounter a loadable DIFO sub-section that is not 11353 * known to us, assume this is a broken program and fail. 11354 */ 11355 if (difo[i].section == DOF_SECT_NONE && 11356 (subsec->dofs_flags & DOF_SECF_LOAD)) { 11357 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 11358 goto err; 11359 } 11360 } 11361 11362 if (dp->dtdo_buf == NULL) { 11363 /* 11364 * We can't have a DIF object without DIF text. 11365 */ 11366 dtrace_dof_error(dof, "missing DIF text"); 11367 goto err; 11368 } 11369 11370 /* 11371 * Before we validate the DIF object, run through the variable table 11372 * looking for the strings -- if any of their size are under, we'll set 11373 * their size to be the system-wide default string size. Note that 11374 * this should _not_ happen if the "strsize" option has been set -- 11375 * in this case, the compiler should have set the size to reflect the 11376 * setting of the option. 11377 */ 11378 for (i = 0; i < dp->dtdo_varlen; i++) { 11379 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 11380 dtrace_diftype_t *t = &v->dtdv_type; 11381 11382 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 11383 continue; 11384 11385 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 11386 t->dtdt_size = dtrace_strsize_default; 11387 } 11388 11389 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 11390 goto err; 11391 11392 dtrace_difo_init(dp, vstate); 11393 return (dp); 11394 11395 err: 11396 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 11397 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 11398 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 11399 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 11400 11401 kmem_free(dp, sizeof (dtrace_difo_t)); 11402 return (NULL); 11403 } 11404 11405 static dtrace_predicate_t * 11406 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11407 cred_t *cr) 11408 { 11409 dtrace_difo_t *dp; 11410 11411 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 11412 return (NULL); 11413 11414 return (dtrace_predicate_create(dp)); 11415 } 11416 11417 static dtrace_actdesc_t * 11418 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11419 cred_t *cr) 11420 { 11421 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 11422 dof_actdesc_t *desc; 11423 dof_sec_t *difosec; 11424 size_t offs; 11425 uintptr_t daddr = (uintptr_t)dof; 11426 uint64_t arg; 11427 dtrace_actkind_t kind; 11428 11429 if (sec->dofs_type != DOF_SECT_ACTDESC) { 11430 dtrace_dof_error(dof, "invalid action section"); 11431 return (NULL); 11432 } 11433 11434 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 11435 dtrace_dof_error(dof, "truncated action description"); 11436 return (NULL); 11437 } 11438 11439 if (sec->dofs_align != sizeof (uint64_t)) { 11440 dtrace_dof_error(dof, "bad alignment in action description"); 11441 return (NULL); 11442 } 11443 11444 if (sec->dofs_size < sec->dofs_entsize) { 11445 dtrace_dof_error(dof, "section entry size exceeds total size"); 11446 return (NULL); 11447 } 11448 11449 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 11450 dtrace_dof_error(dof, "bad entry size in action description"); 11451 return (NULL); 11452 } 11453 11454 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 11455 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 11456 return (NULL); 11457 } 11458 11459 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 11460 desc = (dof_actdesc_t *)(daddr + 11461 (uintptr_t)sec->dofs_offset + offs); 11462 kind = (dtrace_actkind_t)desc->dofa_kind; 11463 11464 if (DTRACEACT_ISPRINTFLIKE(kind) && 11465 (kind != DTRACEACT_PRINTA || 11466 desc->dofa_strtab != DOF_SECIDX_NONE)) { 11467 dof_sec_t *strtab; 11468 char *str, *fmt; 11469 uint64_t i; 11470 11471 /* 11472 * printf()-like actions must have a format string. 11473 */ 11474 if ((strtab = dtrace_dof_sect(dof, 11475 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 11476 goto err; 11477 11478 str = (char *)((uintptr_t)dof + 11479 (uintptr_t)strtab->dofs_offset); 11480 11481 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 11482 if (str[i] == '\0') 11483 break; 11484 } 11485 11486 if (i >= strtab->dofs_size) { 11487 dtrace_dof_error(dof, "bogus format string"); 11488 goto err; 11489 } 11490 11491 if (i == desc->dofa_arg) { 11492 dtrace_dof_error(dof, "empty format string"); 11493 goto err; 11494 } 11495 11496 i -= desc->dofa_arg; 11497 fmt = kmem_alloc(i + 1, KM_SLEEP); 11498 bcopy(&str[desc->dofa_arg], fmt, i + 1); 11499 arg = (uint64_t)(uintptr_t)fmt; 11500 } else { 11501 if (kind == DTRACEACT_PRINTA) { 11502 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 11503 arg = 0; 11504 } else { 11505 arg = desc->dofa_arg; 11506 } 11507 } 11508 11509 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 11510 desc->dofa_uarg, arg); 11511 11512 if (last != NULL) { 11513 last->dtad_next = act; 11514 } else { 11515 first = act; 11516 } 11517 11518 last = act; 11519 11520 if (desc->dofa_difo == DOF_SECIDX_NONE) 11521 continue; 11522 11523 if ((difosec = dtrace_dof_sect(dof, 11524 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 11525 goto err; 11526 11527 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 11528 11529 if (act->dtad_difo == NULL) 11530 goto err; 11531 } 11532 11533 ASSERT(first != NULL); 11534 return (first); 11535 11536 err: 11537 for (act = first; act != NULL; act = next) { 11538 next = act->dtad_next; 11539 dtrace_actdesc_release(act, vstate); 11540 } 11541 11542 return (NULL); 11543 } 11544 11545 static dtrace_ecbdesc_t * 11546 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11547 cred_t *cr) 11548 { 11549 dtrace_ecbdesc_t *ep; 11550 dof_ecbdesc_t *ecb; 11551 dtrace_probedesc_t *desc; 11552 dtrace_predicate_t *pred = NULL; 11553 11554 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 11555 dtrace_dof_error(dof, "truncated ECB description"); 11556 return (NULL); 11557 } 11558 11559 if (sec->dofs_align != sizeof (uint64_t)) { 11560 dtrace_dof_error(dof, "bad alignment in ECB description"); 11561 return (NULL); 11562 } 11563 11564 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 11565 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 11566 11567 if (sec == NULL) 11568 return (NULL); 11569 11570 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 11571 ep->dted_uarg = ecb->dofe_uarg; 11572 desc = &ep->dted_probe; 11573 11574 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 11575 goto err; 11576 11577 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 11578 if ((sec = dtrace_dof_sect(dof, 11579 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 11580 goto err; 11581 11582 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 11583 goto err; 11584 11585 ep->dted_pred.dtpdd_predicate = pred; 11586 } 11587 11588 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 11589 if ((sec = dtrace_dof_sect(dof, 11590 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 11591 goto err; 11592 11593 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 11594 11595 if (ep->dted_action == NULL) 11596 goto err; 11597 } 11598 11599 return (ep); 11600 11601 err: 11602 if (pred != NULL) 11603 dtrace_predicate_release(pred, vstate); 11604 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 11605 return (NULL); 11606 } 11607 11608 /* 11609 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 11610 * specified DOF. At present, this amounts to simply adding 'ubase' to the 11611 * site of any user SETX relocations to account for load object base address. 11612 * In the future, if we need other relocations, this function can be extended. 11613 */ 11614 static int 11615 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 11616 { 11617 uintptr_t daddr = (uintptr_t)dof; 11618 dof_relohdr_t *dofr = 11619 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 11620 dof_sec_t *ss, *rs, *ts; 11621 dof_relodesc_t *r; 11622 uint_t i, n; 11623 11624 if (sec->dofs_size < sizeof (dof_relohdr_t) || 11625 sec->dofs_align != sizeof (dof_secidx_t)) { 11626 dtrace_dof_error(dof, "invalid relocation header"); 11627 return (-1); 11628 } 11629 11630 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 11631 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 11632 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 11633 11634 if (ss == NULL || rs == NULL || ts == NULL) 11635 return (-1); /* dtrace_dof_error() has been called already */ 11636 11637 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 11638 rs->dofs_align != sizeof (uint64_t)) { 11639 dtrace_dof_error(dof, "invalid relocation section"); 11640 return (-1); 11641 } 11642 11643 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 11644 n = rs->dofs_size / rs->dofs_entsize; 11645 11646 for (i = 0; i < n; i++) { 11647 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 11648 11649 switch (r->dofr_type) { 11650 case DOF_RELO_NONE: 11651 break; 11652 case DOF_RELO_SETX: 11653 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 11654 sizeof (uint64_t) > ts->dofs_size) { 11655 dtrace_dof_error(dof, "bad relocation offset"); 11656 return (-1); 11657 } 11658 11659 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 11660 dtrace_dof_error(dof, "misaligned setx relo"); 11661 return (-1); 11662 } 11663 11664 *(uint64_t *)taddr += ubase; 11665 break; 11666 default: 11667 dtrace_dof_error(dof, "invalid relocation type"); 11668 return (-1); 11669 } 11670 11671 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 11672 } 11673 11674 return (0); 11675 } 11676 11677 /* 11678 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 11679 * header: it should be at the front of a memory region that is at least 11680 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 11681 * size. It need not be validated in any other way. 11682 */ 11683 static int 11684 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 11685 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 11686 { 11687 uint64_t len = dof->dofh_loadsz, seclen; 11688 uintptr_t daddr = (uintptr_t)dof; 11689 dtrace_ecbdesc_t *ep; 11690 dtrace_enabling_t *enab; 11691 uint_t i; 11692 11693 ASSERT(MUTEX_HELD(&dtrace_lock)); 11694 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 11695 11696 /* 11697 * Check the DOF header identification bytes. In addition to checking 11698 * valid settings, we also verify that unused bits/bytes are zeroed so 11699 * we can use them later without fear of regressing existing binaries. 11700 */ 11701 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 11702 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 11703 dtrace_dof_error(dof, "DOF magic string mismatch"); 11704 return (-1); 11705 } 11706 11707 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 11708 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 11709 dtrace_dof_error(dof, "DOF has invalid data model"); 11710 return (-1); 11711 } 11712 11713 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 11714 dtrace_dof_error(dof, "DOF encoding mismatch"); 11715 return (-1); 11716 } 11717 11718 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 11719 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 11720 dtrace_dof_error(dof, "DOF version mismatch"); 11721 return (-1); 11722 } 11723 11724 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 11725 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 11726 return (-1); 11727 } 11728 11729 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 11730 dtrace_dof_error(dof, "DOF uses too many integer registers"); 11731 return (-1); 11732 } 11733 11734 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 11735 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 11736 return (-1); 11737 } 11738 11739 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 11740 if (dof->dofh_ident[i] != 0) { 11741 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 11742 return (-1); 11743 } 11744 } 11745 11746 if (dof->dofh_flags & ~DOF_FL_VALID) { 11747 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 11748 return (-1); 11749 } 11750 11751 if (dof->dofh_secsize == 0) { 11752 dtrace_dof_error(dof, "zero section header size"); 11753 return (-1); 11754 } 11755 11756 /* 11757 * Check that the section headers don't exceed the amount of DOF 11758 * data. Note that we cast the section size and number of sections 11759 * to uint64_t's to prevent possible overflow in the multiplication. 11760 */ 11761 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 11762 11763 if (dof->dofh_secoff > len || seclen > len || 11764 dof->dofh_secoff + seclen > len) { 11765 dtrace_dof_error(dof, "truncated section headers"); 11766 return (-1); 11767 } 11768 11769 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 11770 dtrace_dof_error(dof, "misaligned section headers"); 11771 return (-1); 11772 } 11773 11774 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 11775 dtrace_dof_error(dof, "misaligned section size"); 11776 return (-1); 11777 } 11778 11779 /* 11780 * Take an initial pass through the section headers to be sure that 11781 * the headers don't have stray offsets. If the 'noprobes' flag is 11782 * set, do not permit sections relating to providers, probes, or args. 11783 */ 11784 for (i = 0; i < dof->dofh_secnum; i++) { 11785 dof_sec_t *sec = (dof_sec_t *)(daddr + 11786 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 11787 11788 if (noprobes) { 11789 switch (sec->dofs_type) { 11790 case DOF_SECT_PROVIDER: 11791 case DOF_SECT_PROBES: 11792 case DOF_SECT_PRARGS: 11793 case DOF_SECT_PROFFS: 11794 dtrace_dof_error(dof, "illegal sections " 11795 "for enabling"); 11796 return (-1); 11797 } 11798 } 11799 11800 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 11801 !(sec->dofs_flags & DOF_SECF_LOAD)) { 11802 dtrace_dof_error(dof, "loadable section with load " 11803 "flag unset"); 11804 return (-1); 11805 } 11806 11807 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 11808 continue; /* just ignore non-loadable sections */ 11809 11810 if (sec->dofs_align & (sec->dofs_align - 1)) { 11811 dtrace_dof_error(dof, "bad section alignment"); 11812 return (-1); 11813 } 11814 11815 if (sec->dofs_offset & (sec->dofs_align - 1)) { 11816 dtrace_dof_error(dof, "misaligned section"); 11817 return (-1); 11818 } 11819 11820 if (sec->dofs_offset > len || sec->dofs_size > len || 11821 sec->dofs_offset + sec->dofs_size > len) { 11822 dtrace_dof_error(dof, "corrupt section header"); 11823 return (-1); 11824 } 11825 11826 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 11827 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 11828 dtrace_dof_error(dof, "non-terminating string table"); 11829 return (-1); 11830 } 11831 } 11832 11833 /* 11834 * Take a second pass through the sections and locate and perform any 11835 * relocations that are present. We do this after the first pass to 11836 * be sure that all sections have had their headers validated. 11837 */ 11838 for (i = 0; i < dof->dofh_secnum; i++) { 11839 dof_sec_t *sec = (dof_sec_t *)(daddr + 11840 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 11841 11842 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 11843 continue; /* skip sections that are not loadable */ 11844 11845 switch (sec->dofs_type) { 11846 case DOF_SECT_URELHDR: 11847 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 11848 return (-1); 11849 break; 11850 } 11851 } 11852 11853 if ((enab = *enabp) == NULL) 11854 enab = *enabp = dtrace_enabling_create(vstate); 11855 11856 for (i = 0; i < dof->dofh_secnum; i++) { 11857 dof_sec_t *sec = (dof_sec_t *)(daddr + 11858 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 11859 11860 if (sec->dofs_type != DOF_SECT_ECBDESC) 11861 continue; 11862 11863 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 11864 dtrace_enabling_destroy(enab); 11865 *enabp = NULL; 11866 return (-1); 11867 } 11868 11869 dtrace_enabling_add(enab, ep); 11870 } 11871 11872 return (0); 11873 } 11874 11875 /* 11876 * Process DOF for any options. This routine assumes that the DOF has been 11877 * at least processed by dtrace_dof_slurp(). 11878 */ 11879 static int 11880 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 11881 { 11882 int i, rval; 11883 uint32_t entsize; 11884 size_t offs; 11885 dof_optdesc_t *desc; 11886 11887 for (i = 0; i < dof->dofh_secnum; i++) { 11888 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 11889 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 11890 11891 if (sec->dofs_type != DOF_SECT_OPTDESC) 11892 continue; 11893 11894 if (sec->dofs_align != sizeof (uint64_t)) { 11895 dtrace_dof_error(dof, "bad alignment in " 11896 "option description"); 11897 return (EINVAL); 11898 } 11899 11900 if ((entsize = sec->dofs_entsize) == 0) { 11901 dtrace_dof_error(dof, "zeroed option entry size"); 11902 return (EINVAL); 11903 } 11904 11905 if (entsize < sizeof (dof_optdesc_t)) { 11906 dtrace_dof_error(dof, "bad option entry size"); 11907 return (EINVAL); 11908 } 11909 11910 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 11911 desc = (dof_optdesc_t *)((uintptr_t)dof + 11912 (uintptr_t)sec->dofs_offset + offs); 11913 11914 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 11915 dtrace_dof_error(dof, "non-zero option string"); 11916 return (EINVAL); 11917 } 11918 11919 if (desc->dofo_value == DTRACEOPT_UNSET) { 11920 dtrace_dof_error(dof, "unset option"); 11921 return (EINVAL); 11922 } 11923 11924 if ((rval = dtrace_state_option(state, 11925 desc->dofo_option, desc->dofo_value)) != 0) { 11926 dtrace_dof_error(dof, "rejected option"); 11927 return (rval); 11928 } 11929 } 11930 } 11931 11932 return (0); 11933 } 11934 11935 /* 11936 * DTrace Consumer State Functions 11937 */ 11938 int 11939 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 11940 { 11941 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 11942 void *base; 11943 uintptr_t limit; 11944 dtrace_dynvar_t *dvar, *next, *start; 11945 int i; 11946 11947 ASSERT(MUTEX_HELD(&dtrace_lock)); 11948 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 11949 11950 bzero(dstate, sizeof (dtrace_dstate_t)); 11951 11952 if ((dstate->dtds_chunksize = chunksize) == 0) 11953 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 11954 11955 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 11956 size = min; 11957 11958 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 11959 return (ENOMEM); 11960 11961 dstate->dtds_size = size; 11962 dstate->dtds_base = base; 11963 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 11964 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 11965 11966 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 11967 11968 if (hashsize != 1 && (hashsize & 1)) 11969 hashsize--; 11970 11971 dstate->dtds_hashsize = hashsize; 11972 dstate->dtds_hash = dstate->dtds_base; 11973 11974 /* 11975 * Set all of our hash buckets to point to the single sink, and (if 11976 * it hasn't already been set), set the sink's hash value to be the 11977 * sink sentinel value. The sink is needed for dynamic variable 11978 * lookups to know that they have iterated over an entire, valid hash 11979 * chain. 11980 */ 11981 for (i = 0; i < hashsize; i++) 11982 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 11983 11984 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 11985 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 11986 11987 /* 11988 * Determine number of active CPUs. Divide free list evenly among 11989 * active CPUs. 11990 */ 11991 start = (dtrace_dynvar_t *) 11992 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 11993 limit = (uintptr_t)base + size; 11994 11995 maxper = (limit - (uintptr_t)start) / NCPU; 11996 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 11997 11998 for (i = 0; i < NCPU; i++) { 11999 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 12000 12001 /* 12002 * If we don't even have enough chunks to make it once through 12003 * NCPUs, we're just going to allocate everything to the first 12004 * CPU. And if we're on the last CPU, we're going to allocate 12005 * whatever is left over. In either case, we set the limit to 12006 * be the limit of the dynamic variable space. 12007 */ 12008 if (maxper == 0 || i == NCPU - 1) { 12009 limit = (uintptr_t)base + size; 12010 start = NULL; 12011 } else { 12012 limit = (uintptr_t)start + maxper; 12013 start = (dtrace_dynvar_t *)limit; 12014 } 12015 12016 ASSERT(limit <= (uintptr_t)base + size); 12017 12018 for (;;) { 12019 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 12020 dstate->dtds_chunksize); 12021 12022 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 12023 break; 12024 12025 dvar->dtdv_next = next; 12026 dvar = next; 12027 } 12028 12029 if (maxper == 0) 12030 break; 12031 } 12032 12033 return (0); 12034 } 12035 12036 void 12037 dtrace_dstate_fini(dtrace_dstate_t *dstate) 12038 { 12039 ASSERT(MUTEX_HELD(&cpu_lock)); 12040 12041 if (dstate->dtds_base == NULL) 12042 return; 12043 12044 kmem_free(dstate->dtds_base, dstate->dtds_size); 12045 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 12046 } 12047 12048 static void 12049 dtrace_vstate_fini(dtrace_vstate_t *vstate) 12050 { 12051 /* 12052 * Logical XOR, where are you? 12053 */ 12054 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 12055 12056 if (vstate->dtvs_nglobals > 0) { 12057 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 12058 sizeof (dtrace_statvar_t *)); 12059 } 12060 12061 if (vstate->dtvs_ntlocals > 0) { 12062 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 12063 sizeof (dtrace_difv_t)); 12064 } 12065 12066 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 12067 12068 if (vstate->dtvs_nlocals > 0) { 12069 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 12070 sizeof (dtrace_statvar_t *)); 12071 } 12072 } 12073 12074 static void 12075 dtrace_state_clean(dtrace_state_t *state) 12076 { 12077 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 12078 return; 12079 12080 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 12081 dtrace_speculation_clean(state); 12082 } 12083 12084 static void 12085 dtrace_state_deadman(dtrace_state_t *state) 12086 { 12087 hrtime_t now; 12088 12089 dtrace_sync(); 12090 12091 now = dtrace_gethrtime(); 12092 12093 if (state != dtrace_anon.dta_state && 12094 now - state->dts_laststatus >= dtrace_deadman_user) 12095 return; 12096 12097 /* 12098 * We must be sure that dts_alive never appears to be less than the 12099 * value upon entry to dtrace_state_deadman(), and because we lack a 12100 * dtrace_cas64(), we cannot store to it atomically. We thus instead 12101 * store INT64_MAX to it, followed by a memory barrier, followed by 12102 * the new value. This assures that dts_alive never appears to be 12103 * less than its true value, regardless of the order in which the 12104 * stores to the underlying storage are issued. 12105 */ 12106 state->dts_alive = INT64_MAX; 12107 dtrace_membar_producer(); 12108 state->dts_alive = now; 12109 } 12110 12111 dtrace_state_t * 12112 dtrace_state_create(dev_t *devp, cred_t *cr) 12113 { 12114 minor_t minor; 12115 major_t major; 12116 char c[30]; 12117 dtrace_state_t *state; 12118 dtrace_optval_t *opt; 12119 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 12120 12121 ASSERT(MUTEX_HELD(&dtrace_lock)); 12122 ASSERT(MUTEX_HELD(&cpu_lock)); 12123 12124 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 12125 VM_BESTFIT | VM_SLEEP); 12126 12127 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 12128 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 12129 return (NULL); 12130 } 12131 12132 state = ddi_get_soft_state(dtrace_softstate, minor); 12133 state->dts_epid = DTRACE_EPIDNONE + 1; 12134 12135 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); 12136 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 12137 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 12138 12139 if (devp != NULL) { 12140 major = getemajor(*devp); 12141 } else { 12142 major = ddi_driver_major(dtrace_devi); 12143 } 12144 12145 state->dts_dev = makedevice(major, minor); 12146 12147 if (devp != NULL) 12148 *devp = state->dts_dev; 12149 12150 /* 12151 * We allocate NCPU buffers. On the one hand, this can be quite 12152 * a bit of memory per instance (nearly 36K on a Starcat). On the 12153 * other hand, it saves an additional memory reference in the probe 12154 * path. 12155 */ 12156 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 12157 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 12158 state->dts_cleaner = CYCLIC_NONE; 12159 state->dts_deadman = CYCLIC_NONE; 12160 state->dts_vstate.dtvs_state = state; 12161 12162 for (i = 0; i < DTRACEOPT_MAX; i++) 12163 state->dts_options[i] = DTRACEOPT_UNSET; 12164 12165 /* 12166 * Set the default options. 12167 */ 12168 opt = state->dts_options; 12169 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 12170 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 12171 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 12172 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 12173 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 12174 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 12175 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 12176 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 12177 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 12178 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 12179 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 12180 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 12181 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 12182 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 12183 12184 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 12185 12186 /* 12187 * Depending on the user credentials, we set flag bits which alter probe 12188 * visibility or the amount of destructiveness allowed. In the case of 12189 * actual anonymous tracing, or the possession of all privileges, all of 12190 * the normal checks are bypassed. 12191 */ 12192 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 12193 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 12194 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 12195 } else { 12196 /* 12197 * Set up the credentials for this instantiation. We take a 12198 * hold on the credential to prevent it from disappearing on 12199 * us; this in turn prevents the zone_t referenced by this 12200 * credential from disappearing. This means that we can 12201 * examine the credential and the zone from probe context. 12202 */ 12203 crhold(cr); 12204 state->dts_cred.dcr_cred = cr; 12205 12206 /* 12207 * CRA_PROC means "we have *some* privilege for dtrace" and 12208 * unlocks the use of variables like pid, zonename, etc. 12209 */ 12210 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 12211 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12212 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 12213 } 12214 12215 /* 12216 * dtrace_user allows use of syscall and profile providers. 12217 * If the user also has proc_owner and/or proc_zone, we 12218 * extend the scope to include additional visibility and 12219 * destructive power. 12220 */ 12221 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 12222 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 12223 state->dts_cred.dcr_visible |= 12224 DTRACE_CRV_ALLPROC; 12225 12226 state->dts_cred.dcr_action |= 12227 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12228 } 12229 12230 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 12231 state->dts_cred.dcr_visible |= 12232 DTRACE_CRV_ALLZONE; 12233 12234 state->dts_cred.dcr_action |= 12235 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12236 } 12237 12238 /* 12239 * If we have all privs in whatever zone this is, 12240 * we can do destructive things to processes which 12241 * have altered credentials. 12242 */ 12243 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12244 cr->cr_zone->zone_privset)) { 12245 state->dts_cred.dcr_action |= 12246 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12247 } 12248 } 12249 12250 /* 12251 * Holding the dtrace_kernel privilege also implies that 12252 * the user has the dtrace_user privilege from a visibility 12253 * perspective. But without further privileges, some 12254 * destructive actions are not available. 12255 */ 12256 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 12257 /* 12258 * Make all probes in all zones visible. However, 12259 * this doesn't mean that all actions become available 12260 * to all zones. 12261 */ 12262 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 12263 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 12264 12265 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 12266 DTRACE_CRA_PROC; 12267 /* 12268 * Holding proc_owner means that destructive actions 12269 * for *this* zone are allowed. 12270 */ 12271 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12272 state->dts_cred.dcr_action |= 12273 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12274 12275 /* 12276 * Holding proc_zone means that destructive actions 12277 * for this user/group ID in all zones is allowed. 12278 */ 12279 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12280 state->dts_cred.dcr_action |= 12281 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12282 12283 /* 12284 * If we have all privs in whatever zone this is, 12285 * we can do destructive things to processes which 12286 * have altered credentials. 12287 */ 12288 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12289 cr->cr_zone->zone_privset)) { 12290 state->dts_cred.dcr_action |= 12291 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12292 } 12293 } 12294 12295 /* 12296 * Holding the dtrace_proc privilege gives control over fasttrap 12297 * and pid providers. We need to grant wider destructive 12298 * privileges in the event that the user has proc_owner and/or 12299 * proc_zone. 12300 */ 12301 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12302 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12303 state->dts_cred.dcr_action |= 12304 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12305 12306 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12307 state->dts_cred.dcr_action |= 12308 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12309 } 12310 } 12311 12312 return (state); 12313 } 12314 12315 static int 12316 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 12317 { 12318 dtrace_optval_t *opt = state->dts_options, size; 12319 processorid_t cpu; 12320 int flags = 0, rval; 12321 12322 ASSERT(MUTEX_HELD(&dtrace_lock)); 12323 ASSERT(MUTEX_HELD(&cpu_lock)); 12324 ASSERT(which < DTRACEOPT_MAX); 12325 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 12326 (state == dtrace_anon.dta_state && 12327 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 12328 12329 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 12330 return (0); 12331 12332 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 12333 cpu = opt[DTRACEOPT_CPU]; 12334 12335 if (which == DTRACEOPT_SPECSIZE) 12336 flags |= DTRACEBUF_NOSWITCH; 12337 12338 if (which == DTRACEOPT_BUFSIZE) { 12339 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 12340 flags |= DTRACEBUF_RING; 12341 12342 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 12343 flags |= DTRACEBUF_FILL; 12344 12345 if (state != dtrace_anon.dta_state || 12346 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 12347 flags |= DTRACEBUF_INACTIVE; 12348 } 12349 12350 for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { 12351 /* 12352 * The size must be 8-byte aligned. If the size is not 8-byte 12353 * aligned, drop it down by the difference. 12354 */ 12355 if (size & (sizeof (uint64_t) - 1)) 12356 size -= size & (sizeof (uint64_t) - 1); 12357 12358 if (size < state->dts_reserve) { 12359 /* 12360 * Buffers always must be large enough to accommodate 12361 * their prereserved space. We return E2BIG instead 12362 * of ENOMEM in this case to allow for user-level 12363 * software to differentiate the cases. 12364 */ 12365 return (E2BIG); 12366 } 12367 12368 rval = dtrace_buffer_alloc(buf, size, flags, cpu); 12369 12370 if (rval != ENOMEM) { 12371 opt[which] = size; 12372 return (rval); 12373 } 12374 12375 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 12376 return (rval); 12377 } 12378 12379 return (ENOMEM); 12380 } 12381 12382 static int 12383 dtrace_state_buffers(dtrace_state_t *state) 12384 { 12385 dtrace_speculation_t *spec = state->dts_speculations; 12386 int rval, i; 12387 12388 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 12389 DTRACEOPT_BUFSIZE)) != 0) 12390 return (rval); 12391 12392 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 12393 DTRACEOPT_AGGSIZE)) != 0) 12394 return (rval); 12395 12396 for (i = 0; i < state->dts_nspeculations; i++) { 12397 if ((rval = dtrace_state_buffer(state, 12398 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 12399 return (rval); 12400 } 12401 12402 return (0); 12403 } 12404 12405 static void 12406 dtrace_state_prereserve(dtrace_state_t *state) 12407 { 12408 dtrace_ecb_t *ecb; 12409 dtrace_probe_t *probe; 12410 12411 state->dts_reserve = 0; 12412 12413 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 12414 return; 12415 12416 /* 12417 * If our buffer policy is a "fill" buffer policy, we need to set the 12418 * prereserved space to be the space required by the END probes. 12419 */ 12420 probe = dtrace_probes[dtrace_probeid_end - 1]; 12421 ASSERT(probe != NULL); 12422 12423 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 12424 if (ecb->dte_state != state) 12425 continue; 12426 12427 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 12428 } 12429 } 12430 12431 static int 12432 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 12433 { 12434 dtrace_optval_t *opt = state->dts_options, sz, nspec; 12435 dtrace_speculation_t *spec; 12436 dtrace_buffer_t *buf; 12437 cyc_handler_t hdlr; 12438 cyc_time_t when; 12439 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 12440 dtrace_icookie_t cookie; 12441 12442 mutex_enter(&cpu_lock); 12443 mutex_enter(&dtrace_lock); 12444 12445 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 12446 rval = EBUSY; 12447 goto out; 12448 } 12449 12450 /* 12451 * Before we can perform any checks, we must prime all of the 12452 * retained enablings that correspond to this state. 12453 */ 12454 dtrace_enabling_prime(state); 12455 12456 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 12457 rval = EACCES; 12458 goto out; 12459 } 12460 12461 dtrace_state_prereserve(state); 12462 12463 /* 12464 * Now we want to do is try to allocate our speculations. 12465 * We do not automatically resize the number of speculations; if 12466 * this fails, we will fail the operation. 12467 */ 12468 nspec = opt[DTRACEOPT_NSPEC]; 12469 ASSERT(nspec != DTRACEOPT_UNSET); 12470 12471 if (nspec > INT_MAX) { 12472 rval = ENOMEM; 12473 goto out; 12474 } 12475 12476 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); 12477 12478 if (spec == NULL) { 12479 rval = ENOMEM; 12480 goto out; 12481 } 12482 12483 state->dts_speculations = spec; 12484 state->dts_nspeculations = (int)nspec; 12485 12486 for (i = 0; i < nspec; i++) { 12487 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { 12488 rval = ENOMEM; 12489 goto err; 12490 } 12491 12492 spec[i].dtsp_buffer = buf; 12493 } 12494 12495 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 12496 if (dtrace_anon.dta_state == NULL) { 12497 rval = ENOENT; 12498 goto out; 12499 } 12500 12501 if (state->dts_necbs != 0) { 12502 rval = EALREADY; 12503 goto out; 12504 } 12505 12506 state->dts_anon = dtrace_anon_grab(); 12507 ASSERT(state->dts_anon != NULL); 12508 state = state->dts_anon; 12509 12510 /* 12511 * We want "grabanon" to be set in the grabbed state, so we'll 12512 * copy that option value from the grabbing state into the 12513 * grabbed state. 12514 */ 12515 state->dts_options[DTRACEOPT_GRABANON] = 12516 opt[DTRACEOPT_GRABANON]; 12517 12518 *cpu = dtrace_anon.dta_beganon; 12519 12520 /* 12521 * If the anonymous state is active (as it almost certainly 12522 * is if the anonymous enabling ultimately matched anything), 12523 * we don't allow any further option processing -- but we 12524 * don't return failure. 12525 */ 12526 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 12527 goto out; 12528 } 12529 12530 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 12531 opt[DTRACEOPT_AGGSIZE] != 0) { 12532 if (state->dts_aggregations == NULL) { 12533 /* 12534 * We're not going to create an aggregation buffer 12535 * because we don't have any ECBs that contain 12536 * aggregations -- set this option to 0. 12537 */ 12538 opt[DTRACEOPT_AGGSIZE] = 0; 12539 } else { 12540 /* 12541 * If we have an aggregation buffer, we must also have 12542 * a buffer to use as scratch. 12543 */ 12544 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 12545 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 12546 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 12547 } 12548 } 12549 } 12550 12551 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 12552 opt[DTRACEOPT_SPECSIZE] != 0) { 12553 if (!state->dts_speculates) { 12554 /* 12555 * We're not going to create speculation buffers 12556 * because we don't have any ECBs that actually 12557 * speculate -- set the speculation size to 0. 12558 */ 12559 opt[DTRACEOPT_SPECSIZE] = 0; 12560 } 12561 } 12562 12563 /* 12564 * The bare minimum size for any buffer that we're actually going to 12565 * do anything to is sizeof (uint64_t). 12566 */ 12567 sz = sizeof (uint64_t); 12568 12569 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 12570 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 12571 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 12572 /* 12573 * A buffer size has been explicitly set to 0 (or to a size 12574 * that will be adjusted to 0) and we need the space -- we 12575 * need to return failure. We return ENOSPC to differentiate 12576 * it from failing to allocate a buffer due to failure to meet 12577 * the reserve (for which we return E2BIG). 12578 */ 12579 rval = ENOSPC; 12580 goto out; 12581 } 12582 12583 if ((rval = dtrace_state_buffers(state)) != 0) 12584 goto err; 12585 12586 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 12587 sz = dtrace_dstate_defsize; 12588 12589 do { 12590 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 12591 12592 if (rval == 0) 12593 break; 12594 12595 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 12596 goto err; 12597 } while (sz >>= 1); 12598 12599 opt[DTRACEOPT_DYNVARSIZE] = sz; 12600 12601 if (rval != 0) 12602 goto err; 12603 12604 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 12605 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 12606 12607 if (opt[DTRACEOPT_CLEANRATE] == 0) 12608 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 12609 12610 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 12611 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 12612 12613 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 12614 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 12615 12616 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 12617 hdlr.cyh_arg = state; 12618 hdlr.cyh_level = CY_LOW_LEVEL; 12619 12620 when.cyt_when = 0; 12621 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 12622 12623 state->dts_cleaner = cyclic_add(&hdlr, &when); 12624 12625 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 12626 hdlr.cyh_arg = state; 12627 hdlr.cyh_level = CY_LOW_LEVEL; 12628 12629 when.cyt_when = 0; 12630 when.cyt_interval = dtrace_deadman_interval; 12631 12632 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 12633 state->dts_deadman = cyclic_add(&hdlr, &when); 12634 12635 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 12636 12637 /* 12638 * Now it's time to actually fire the BEGIN probe. We need to disable 12639 * interrupts here both to record the CPU on which we fired the BEGIN 12640 * probe (the data from this CPU will be processed first at user 12641 * level) and to manually activate the buffer for this CPU. 12642 */ 12643 cookie = dtrace_interrupt_disable(); 12644 *cpu = CPU->cpu_id; 12645 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 12646 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 12647 12648 dtrace_probe(dtrace_probeid_begin, 12649 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 12650 dtrace_interrupt_enable(cookie); 12651 /* 12652 * We may have had an exit action from a BEGIN probe; only change our 12653 * state to ACTIVE if we're still in WARMUP. 12654 */ 12655 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 12656 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 12657 12658 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 12659 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 12660 12661 /* 12662 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 12663 * want each CPU to transition its principal buffer out of the 12664 * INACTIVE state. Doing this assures that no CPU will suddenly begin 12665 * processing an ECB halfway down a probe's ECB chain; all CPUs will 12666 * atomically transition from processing none of a state's ECBs to 12667 * processing all of them. 12668 */ 12669 dtrace_xcall(DTRACE_CPUALL, 12670 (dtrace_xcall_t)dtrace_buffer_activate, state); 12671 goto out; 12672 12673 err: 12674 dtrace_buffer_free(state->dts_buffer); 12675 dtrace_buffer_free(state->dts_aggbuffer); 12676 12677 if ((nspec = state->dts_nspeculations) == 0) { 12678 ASSERT(state->dts_speculations == NULL); 12679 goto out; 12680 } 12681 12682 spec = state->dts_speculations; 12683 ASSERT(spec != NULL); 12684 12685 for (i = 0; i < state->dts_nspeculations; i++) { 12686 if ((buf = spec[i].dtsp_buffer) == NULL) 12687 break; 12688 12689 dtrace_buffer_free(buf); 12690 kmem_free(buf, bufsize); 12691 } 12692 12693 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 12694 state->dts_nspeculations = 0; 12695 state->dts_speculations = NULL; 12696 12697 out: 12698 mutex_exit(&dtrace_lock); 12699 mutex_exit(&cpu_lock); 12700 12701 return (rval); 12702 } 12703 12704 static int 12705 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 12706 { 12707 dtrace_icookie_t cookie; 12708 12709 ASSERT(MUTEX_HELD(&dtrace_lock)); 12710 12711 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 12712 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 12713 return (EINVAL); 12714 12715 /* 12716 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 12717 * to be sure that every CPU has seen it. See below for the details 12718 * on why this is done. 12719 */ 12720 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 12721 dtrace_sync(); 12722 12723 /* 12724 * By this point, it is impossible for any CPU to be still processing 12725 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 12726 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 12727 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 12728 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 12729 * iff we're in the END probe. 12730 */ 12731 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 12732 dtrace_sync(); 12733 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 12734 12735 /* 12736 * Finally, we can release the reserve and call the END probe. We 12737 * disable interrupts across calling the END probe to allow us to 12738 * return the CPU on which we actually called the END probe. This 12739 * allows user-land to be sure that this CPU's principal buffer is 12740 * processed last. 12741 */ 12742 state->dts_reserve = 0; 12743 12744 cookie = dtrace_interrupt_disable(); 12745 *cpu = CPU->cpu_id; 12746 dtrace_probe(dtrace_probeid_end, 12747 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 12748 dtrace_interrupt_enable(cookie); 12749 12750 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 12751 dtrace_sync(); 12752 12753 return (0); 12754 } 12755 12756 static int 12757 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 12758 dtrace_optval_t val) 12759 { 12760 ASSERT(MUTEX_HELD(&dtrace_lock)); 12761 12762 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 12763 return (EBUSY); 12764 12765 if (option >= DTRACEOPT_MAX) 12766 return (EINVAL); 12767 12768 if (option != DTRACEOPT_CPU && val < 0) 12769 return (EINVAL); 12770 12771 switch (option) { 12772 case DTRACEOPT_DESTRUCTIVE: 12773 if (dtrace_destructive_disallow) 12774 return (EACCES); 12775 12776 state->dts_cred.dcr_destructive = 1; 12777 break; 12778 12779 case DTRACEOPT_BUFSIZE: 12780 case DTRACEOPT_DYNVARSIZE: 12781 case DTRACEOPT_AGGSIZE: 12782 case DTRACEOPT_SPECSIZE: 12783 case DTRACEOPT_STRSIZE: 12784 if (val < 0) 12785 return (EINVAL); 12786 12787 if (val >= LONG_MAX) { 12788 /* 12789 * If this is an otherwise negative value, set it to 12790 * the highest multiple of 128m less than LONG_MAX. 12791 * Technically, we're adjusting the size without 12792 * regard to the buffer resizing policy, but in fact, 12793 * this has no effect -- if we set the buffer size to 12794 * ~LONG_MAX and the buffer policy is ultimately set to 12795 * be "manual", the buffer allocation is guaranteed to 12796 * fail, if only because the allocation requires two 12797 * buffers. (We set the the size to the highest 12798 * multiple of 128m because it ensures that the size 12799 * will remain a multiple of a megabyte when 12800 * repeatedly halved -- all the way down to 15m.) 12801 */ 12802 val = LONG_MAX - (1 << 27) + 1; 12803 } 12804 } 12805 12806 state->dts_options[option] = val; 12807 12808 return (0); 12809 } 12810 12811 static void 12812 dtrace_state_destroy(dtrace_state_t *state) 12813 { 12814 dtrace_ecb_t *ecb; 12815 dtrace_vstate_t *vstate = &state->dts_vstate; 12816 minor_t minor = getminor(state->dts_dev); 12817 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 12818 dtrace_speculation_t *spec = state->dts_speculations; 12819 int nspec = state->dts_nspeculations; 12820 uint32_t match; 12821 12822 ASSERT(MUTEX_HELD(&dtrace_lock)); 12823 ASSERT(MUTEX_HELD(&cpu_lock)); 12824 12825 /* 12826 * First, retract any retained enablings for this state. 12827 */ 12828 dtrace_enabling_retract(state); 12829 ASSERT(state->dts_nretained == 0); 12830 12831 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 12832 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 12833 /* 12834 * We have managed to come into dtrace_state_destroy() on a 12835 * hot enabling -- almost certainly because of a disorderly 12836 * shutdown of a consumer. (That is, a consumer that is 12837 * exiting without having called dtrace_stop().) In this case, 12838 * we're going to set our activity to be KILLED, and then 12839 * issue a sync to be sure that everyone is out of probe 12840 * context before we start blowing away ECBs. 12841 */ 12842 state->dts_activity = DTRACE_ACTIVITY_KILLED; 12843 dtrace_sync(); 12844 } 12845 12846 /* 12847 * Release the credential hold we took in dtrace_state_create(). 12848 */ 12849 if (state->dts_cred.dcr_cred != NULL) 12850 crfree(state->dts_cred.dcr_cred); 12851 12852 /* 12853 * Now we can safely disable and destroy any enabled probes. Because 12854 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 12855 * (especially if they're all enabled), we take two passes through the 12856 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 12857 * in the second we disable whatever is left over. 12858 */ 12859 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 12860 for (i = 0; i < state->dts_necbs; i++) { 12861 if ((ecb = state->dts_ecbs[i]) == NULL) 12862 continue; 12863 12864 if (match && ecb->dte_probe != NULL) { 12865 dtrace_probe_t *probe = ecb->dte_probe; 12866 dtrace_provider_t *prov = probe->dtpr_provider; 12867 12868 if (!(prov->dtpv_priv.dtpp_flags & match)) 12869 continue; 12870 } 12871 12872 dtrace_ecb_disable(ecb); 12873 dtrace_ecb_destroy(ecb); 12874 } 12875 12876 if (!match) 12877 break; 12878 } 12879 12880 /* 12881 * Before we free the buffers, perform one more sync to assure that 12882 * every CPU is out of probe context. 12883 */ 12884 dtrace_sync(); 12885 12886 dtrace_buffer_free(state->dts_buffer); 12887 dtrace_buffer_free(state->dts_aggbuffer); 12888 12889 for (i = 0; i < nspec; i++) 12890 dtrace_buffer_free(spec[i].dtsp_buffer); 12891 12892 if (state->dts_cleaner != CYCLIC_NONE) 12893 cyclic_remove(state->dts_cleaner); 12894 12895 if (state->dts_deadman != CYCLIC_NONE) 12896 cyclic_remove(state->dts_deadman); 12897 12898 dtrace_dstate_fini(&vstate->dtvs_dynvars); 12899 dtrace_vstate_fini(vstate); 12900 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 12901 12902 if (state->dts_aggregations != NULL) { 12903 #ifdef DEBUG 12904 for (i = 0; i < state->dts_naggregations; i++) 12905 ASSERT(state->dts_aggregations[i] == NULL); 12906 #endif 12907 ASSERT(state->dts_naggregations > 0); 12908 kmem_free(state->dts_aggregations, 12909 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 12910 } 12911 12912 kmem_free(state->dts_buffer, bufsize); 12913 kmem_free(state->dts_aggbuffer, bufsize); 12914 12915 for (i = 0; i < nspec; i++) 12916 kmem_free(spec[i].dtsp_buffer, bufsize); 12917 12918 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 12919 12920 dtrace_format_destroy(state); 12921 12922 vmem_destroy(state->dts_aggid_arena); 12923 ddi_soft_state_free(dtrace_softstate, minor); 12924 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 12925 } 12926 12927 /* 12928 * DTrace Anonymous Enabling Functions 12929 */ 12930 static dtrace_state_t * 12931 dtrace_anon_grab(void) 12932 { 12933 dtrace_state_t *state; 12934 12935 ASSERT(MUTEX_HELD(&dtrace_lock)); 12936 12937 if ((state = dtrace_anon.dta_state) == NULL) { 12938 ASSERT(dtrace_anon.dta_enabling == NULL); 12939 return (NULL); 12940 } 12941 12942 ASSERT(dtrace_anon.dta_enabling != NULL); 12943 ASSERT(dtrace_retained != NULL); 12944 12945 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 12946 dtrace_anon.dta_enabling = NULL; 12947 dtrace_anon.dta_state = NULL; 12948 12949 return (state); 12950 } 12951 12952 static void 12953 dtrace_anon_property(void) 12954 { 12955 int i, rv; 12956 dtrace_state_t *state; 12957 dof_hdr_t *dof; 12958 char c[32]; /* enough for "dof-data-" + digits */ 12959 12960 ASSERT(MUTEX_HELD(&dtrace_lock)); 12961 ASSERT(MUTEX_HELD(&cpu_lock)); 12962 12963 for (i = 0; ; i++) { 12964 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 12965 12966 dtrace_err_verbose = 1; 12967 12968 if ((dof = dtrace_dof_property(c)) == NULL) { 12969 dtrace_err_verbose = 0; 12970 break; 12971 } 12972 12973 /* 12974 * We want to create anonymous state, so we need to transition 12975 * the kernel debugger to indicate that DTrace is active. If 12976 * this fails (e.g. because the debugger has modified text in 12977 * some way), we won't continue with the processing. 12978 */ 12979 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 12980 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 12981 "enabling ignored."); 12982 dtrace_dof_destroy(dof); 12983 break; 12984 } 12985 12986 /* 12987 * If we haven't allocated an anonymous state, we'll do so now. 12988 */ 12989 if ((state = dtrace_anon.dta_state) == NULL) { 12990 state = dtrace_state_create(NULL, NULL); 12991 dtrace_anon.dta_state = state; 12992 12993 if (state == NULL) { 12994 /* 12995 * This basically shouldn't happen: the only 12996 * failure mode from dtrace_state_create() is a 12997 * failure of ddi_soft_state_zalloc() that 12998 * itself should never happen. Still, the 12999 * interface allows for a failure mode, and 13000 * we want to fail as gracefully as possible: 13001 * we'll emit an error message and cease 13002 * processing anonymous state in this case. 13003 */ 13004 cmn_err(CE_WARN, "failed to create " 13005 "anonymous state"); 13006 dtrace_dof_destroy(dof); 13007 break; 13008 } 13009 } 13010 13011 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 13012 &dtrace_anon.dta_enabling, 0, B_TRUE); 13013 13014 if (rv == 0) 13015 rv = dtrace_dof_options(dof, state); 13016 13017 dtrace_err_verbose = 0; 13018 dtrace_dof_destroy(dof); 13019 13020 if (rv != 0) { 13021 /* 13022 * This is malformed DOF; chuck any anonymous state 13023 * that we created. 13024 */ 13025 ASSERT(dtrace_anon.dta_enabling == NULL); 13026 dtrace_state_destroy(state); 13027 dtrace_anon.dta_state = NULL; 13028 break; 13029 } 13030 13031 ASSERT(dtrace_anon.dta_enabling != NULL); 13032 } 13033 13034 if (dtrace_anon.dta_enabling != NULL) { 13035 int rval; 13036 13037 /* 13038 * dtrace_enabling_retain() can only fail because we are 13039 * trying to retain more enablings than are allowed -- but 13040 * we only have one anonymous enabling, and we are guaranteed 13041 * to be allowed at least one retained enabling; we assert 13042 * that dtrace_enabling_retain() returns success. 13043 */ 13044 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 13045 ASSERT(rval == 0); 13046 13047 dtrace_enabling_dump(dtrace_anon.dta_enabling); 13048 } 13049 } 13050 13051 /* 13052 * DTrace Helper Functions 13053 */ 13054 static void 13055 dtrace_helper_trace(dtrace_helper_action_t *helper, 13056 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 13057 { 13058 uint32_t size, next, nnext, i; 13059 dtrace_helptrace_t *ent; 13060 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 13061 13062 if (!dtrace_helptrace_enabled) 13063 return; 13064 13065 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 13066 13067 /* 13068 * What would a tracing framework be without its own tracing 13069 * framework? (Well, a hell of a lot simpler, for starters...) 13070 */ 13071 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 13072 sizeof (uint64_t) - sizeof (uint64_t); 13073 13074 /* 13075 * Iterate until we can allocate a slot in the trace buffer. 13076 */ 13077 do { 13078 next = dtrace_helptrace_next; 13079 13080 if (next + size < dtrace_helptrace_bufsize) { 13081 nnext = next + size; 13082 } else { 13083 nnext = size; 13084 } 13085 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 13086 13087 /* 13088 * We have our slot; fill it in. 13089 */ 13090 if (nnext == size) 13091 next = 0; 13092 13093 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 13094 ent->dtht_helper = helper; 13095 ent->dtht_where = where; 13096 ent->dtht_nlocals = vstate->dtvs_nlocals; 13097 13098 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 13099 mstate->dtms_fltoffs : -1; 13100 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 13101 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 13102 13103 for (i = 0; i < vstate->dtvs_nlocals; i++) { 13104 dtrace_statvar_t *svar; 13105 13106 if ((svar = vstate->dtvs_locals[i]) == NULL) 13107 continue; 13108 13109 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 13110 ent->dtht_locals[i] = 13111 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; 13112 } 13113 } 13114 13115 static uint64_t 13116 dtrace_helper(int which, dtrace_mstate_t *mstate, 13117 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 13118 { 13119 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 13120 uint64_t sarg0 = mstate->dtms_arg[0]; 13121 uint64_t sarg1 = mstate->dtms_arg[1]; 13122 uint64_t rval; 13123 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 13124 dtrace_helper_action_t *helper; 13125 dtrace_vstate_t *vstate; 13126 dtrace_difo_t *pred; 13127 int i, trace = dtrace_helptrace_enabled; 13128 13129 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 13130 13131 if (helpers == NULL) 13132 return (0); 13133 13134 if ((helper = helpers->dthps_actions[which]) == NULL) 13135 return (0); 13136 13137 vstate = &helpers->dthps_vstate; 13138 mstate->dtms_arg[0] = arg0; 13139 mstate->dtms_arg[1] = arg1; 13140 13141 /* 13142 * Now iterate over each helper. If its predicate evaluates to 'true', 13143 * we'll call the corresponding actions. Note that the below calls 13144 * to dtrace_dif_emulate() may set faults in machine state. This is 13145 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 13146 * the stored DIF offset with its own (which is the desired behavior). 13147 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 13148 * from machine state; this is okay, too. 13149 */ 13150 for (; helper != NULL; helper = helper->dtha_next) { 13151 if ((pred = helper->dtha_predicate) != NULL) { 13152 if (trace) 13153 dtrace_helper_trace(helper, mstate, vstate, 0); 13154 13155 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 13156 goto next; 13157 13158 if (*flags & CPU_DTRACE_FAULT) 13159 goto err; 13160 } 13161 13162 for (i = 0; i < helper->dtha_nactions; i++) { 13163 if (trace) 13164 dtrace_helper_trace(helper, 13165 mstate, vstate, i + 1); 13166 13167 rval = dtrace_dif_emulate(helper->dtha_actions[i], 13168 mstate, vstate, state); 13169 13170 if (*flags & CPU_DTRACE_FAULT) 13171 goto err; 13172 } 13173 13174 next: 13175 if (trace) 13176 dtrace_helper_trace(helper, mstate, vstate, 13177 DTRACE_HELPTRACE_NEXT); 13178 } 13179 13180 if (trace) 13181 dtrace_helper_trace(helper, mstate, vstate, 13182 DTRACE_HELPTRACE_DONE); 13183 13184 /* 13185 * Restore the arg0 that we saved upon entry. 13186 */ 13187 mstate->dtms_arg[0] = sarg0; 13188 mstate->dtms_arg[1] = sarg1; 13189 13190 return (rval); 13191 13192 err: 13193 if (trace) 13194 dtrace_helper_trace(helper, mstate, vstate, 13195 DTRACE_HELPTRACE_ERR); 13196 13197 /* 13198 * Restore the arg0 that we saved upon entry. 13199 */ 13200 mstate->dtms_arg[0] = sarg0; 13201 mstate->dtms_arg[1] = sarg1; 13202 13203 return (NULL); 13204 } 13205 13206 static void 13207 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 13208 dtrace_vstate_t *vstate) 13209 { 13210 int i; 13211 13212 if (helper->dtha_predicate != NULL) 13213 dtrace_difo_release(helper->dtha_predicate, vstate); 13214 13215 for (i = 0; i < helper->dtha_nactions; i++) { 13216 ASSERT(helper->dtha_actions[i] != NULL); 13217 dtrace_difo_release(helper->dtha_actions[i], vstate); 13218 } 13219 13220 kmem_free(helper->dtha_actions, 13221 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 13222 kmem_free(helper, sizeof (dtrace_helper_action_t)); 13223 } 13224 13225 static int 13226 dtrace_helper_destroygen(int gen) 13227 { 13228 proc_t *p = curproc; 13229 dtrace_helpers_t *help = p->p_dtrace_helpers; 13230 dtrace_vstate_t *vstate; 13231 int i; 13232 13233 ASSERT(MUTEX_HELD(&dtrace_lock)); 13234 13235 if (help == NULL || gen > help->dthps_generation) 13236 return (EINVAL); 13237 13238 vstate = &help->dthps_vstate; 13239 13240 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 13241 dtrace_helper_action_t *last = NULL, *h, *next; 13242 13243 for (h = help->dthps_actions[i]; h != NULL; h = next) { 13244 next = h->dtha_next; 13245 13246 if (h->dtha_generation == gen) { 13247 if (last != NULL) { 13248 last->dtha_next = next; 13249 } else { 13250 help->dthps_actions[i] = next; 13251 } 13252 13253 dtrace_helper_action_destroy(h, vstate); 13254 } else { 13255 last = h; 13256 } 13257 } 13258 } 13259 13260 /* 13261 * Interate until we've cleared out all helper providers with the 13262 * given generation number. 13263 */ 13264 for (;;) { 13265 dtrace_helper_provider_t *prov; 13266 13267 /* 13268 * Look for a helper provider with the right generation. We 13269 * have to start back at the beginning of the list each time 13270 * because we drop dtrace_lock. It's unlikely that we'll make 13271 * more than two passes. 13272 */ 13273 for (i = 0; i < help->dthps_nprovs; i++) { 13274 prov = help->dthps_provs[i]; 13275 13276 if (prov->dthp_generation == gen) 13277 break; 13278 } 13279 13280 /* 13281 * If there were no matches, we're done. 13282 */ 13283 if (i == help->dthps_nprovs) 13284 break; 13285 13286 /* 13287 * Move the last helper provider into this slot. 13288 */ 13289 help->dthps_nprovs--; 13290 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 13291 help->dthps_provs[help->dthps_nprovs] = NULL; 13292 13293 mutex_exit(&dtrace_lock); 13294 13295 /* 13296 * If we have a meta provider, remove this helper provider. 13297 */ 13298 mutex_enter(&dtrace_meta_lock); 13299 if (dtrace_meta_pid != NULL) { 13300 ASSERT(dtrace_deferred_pid == NULL); 13301 dtrace_helper_provider_remove(&prov->dthp_prov, 13302 p->p_pid); 13303 } 13304 mutex_exit(&dtrace_meta_lock); 13305 13306 dtrace_helper_provider_destroy(prov); 13307 13308 mutex_enter(&dtrace_lock); 13309 } 13310 13311 return (0); 13312 } 13313 13314 static int 13315 dtrace_helper_validate(dtrace_helper_action_t *helper) 13316 { 13317 int err = 0, i; 13318 dtrace_difo_t *dp; 13319 13320 if ((dp = helper->dtha_predicate) != NULL) 13321 err += dtrace_difo_validate_helper(dp); 13322 13323 for (i = 0; i < helper->dtha_nactions; i++) 13324 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 13325 13326 return (err == 0); 13327 } 13328 13329 static int 13330 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 13331 { 13332 dtrace_helpers_t *help; 13333 dtrace_helper_action_t *helper, *last; 13334 dtrace_actdesc_t *act; 13335 dtrace_vstate_t *vstate; 13336 dtrace_predicate_t *pred; 13337 int count = 0, nactions = 0, i; 13338 13339 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 13340 return (EINVAL); 13341 13342 help = curproc->p_dtrace_helpers; 13343 last = help->dthps_actions[which]; 13344 vstate = &help->dthps_vstate; 13345 13346 for (count = 0; last != NULL; last = last->dtha_next) { 13347 count++; 13348 if (last->dtha_next == NULL) 13349 break; 13350 } 13351 13352 /* 13353 * If we already have dtrace_helper_actions_max helper actions for this 13354 * helper action type, we'll refuse to add a new one. 13355 */ 13356 if (count >= dtrace_helper_actions_max) 13357 return (ENOSPC); 13358 13359 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 13360 helper->dtha_generation = help->dthps_generation; 13361 13362 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 13363 ASSERT(pred->dtp_difo != NULL); 13364 dtrace_difo_hold(pred->dtp_difo); 13365 helper->dtha_predicate = pred->dtp_difo; 13366 } 13367 13368 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 13369 if (act->dtad_kind != DTRACEACT_DIFEXPR) 13370 goto err; 13371 13372 if (act->dtad_difo == NULL) 13373 goto err; 13374 13375 nactions++; 13376 } 13377 13378 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 13379 (helper->dtha_nactions = nactions), KM_SLEEP); 13380 13381 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 13382 dtrace_difo_hold(act->dtad_difo); 13383 helper->dtha_actions[i++] = act->dtad_difo; 13384 } 13385 13386 if (!dtrace_helper_validate(helper)) 13387 goto err; 13388 13389 if (last == NULL) { 13390 help->dthps_actions[which] = helper; 13391 } else { 13392 last->dtha_next = helper; 13393 } 13394 13395 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 13396 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 13397 dtrace_helptrace_next = 0; 13398 } 13399 13400 return (0); 13401 err: 13402 dtrace_helper_action_destroy(helper, vstate); 13403 return (EINVAL); 13404 } 13405 13406 static void 13407 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 13408 dof_helper_t *dofhp) 13409 { 13410 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 13411 13412 mutex_enter(&dtrace_meta_lock); 13413 mutex_enter(&dtrace_lock); 13414 13415 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 13416 /* 13417 * If the dtrace module is loaded but not attached, or if 13418 * there aren't isn't a meta provider registered to deal with 13419 * these provider descriptions, we need to postpone creating 13420 * the actual providers until later. 13421 */ 13422 13423 if (help->dthps_next == NULL && help->dthps_prev == NULL && 13424 dtrace_deferred_pid != help) { 13425 help->dthps_deferred = 1; 13426 help->dthps_pid = p->p_pid; 13427 help->dthps_next = dtrace_deferred_pid; 13428 help->dthps_prev = NULL; 13429 if (dtrace_deferred_pid != NULL) 13430 dtrace_deferred_pid->dthps_prev = help; 13431 dtrace_deferred_pid = help; 13432 } 13433 13434 mutex_exit(&dtrace_lock); 13435 13436 } else if (dofhp != NULL) { 13437 /* 13438 * If the dtrace module is loaded and we have a particular 13439 * helper provider description, pass that off to the 13440 * meta provider. 13441 */ 13442 13443 mutex_exit(&dtrace_lock); 13444 13445 dtrace_helper_provide(dofhp, p->p_pid); 13446 13447 } else { 13448 /* 13449 * Otherwise, just pass all the helper provider descriptions 13450 * off to the meta provider. 13451 */ 13452 13453 int i; 13454 mutex_exit(&dtrace_lock); 13455 13456 for (i = 0; i < help->dthps_nprovs; i++) { 13457 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 13458 p->p_pid); 13459 } 13460 } 13461 13462 mutex_exit(&dtrace_meta_lock); 13463 } 13464 13465 static int 13466 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 13467 { 13468 dtrace_helpers_t *help; 13469 dtrace_helper_provider_t *hprov, **tmp_provs; 13470 uint_t tmp_maxprovs, i; 13471 13472 ASSERT(MUTEX_HELD(&dtrace_lock)); 13473 13474 help = curproc->p_dtrace_helpers; 13475 ASSERT(help != NULL); 13476 13477 /* 13478 * If we already have dtrace_helper_providers_max helper providers, 13479 * we're refuse to add a new one. 13480 */ 13481 if (help->dthps_nprovs >= dtrace_helper_providers_max) 13482 return (ENOSPC); 13483 13484 /* 13485 * Check to make sure this isn't a duplicate. 13486 */ 13487 for (i = 0; i < help->dthps_nprovs; i++) { 13488 if (dofhp->dofhp_addr == 13489 help->dthps_provs[i]->dthp_prov.dofhp_addr) 13490 return (EALREADY); 13491 } 13492 13493 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 13494 hprov->dthp_prov = *dofhp; 13495 hprov->dthp_ref = 1; 13496 hprov->dthp_generation = gen; 13497 13498 /* 13499 * Allocate a bigger table for helper providers if it's already full. 13500 */ 13501 if (help->dthps_maxprovs == help->dthps_nprovs) { 13502 tmp_maxprovs = help->dthps_maxprovs; 13503 tmp_provs = help->dthps_provs; 13504 13505 if (help->dthps_maxprovs == 0) 13506 help->dthps_maxprovs = 2; 13507 else 13508 help->dthps_maxprovs *= 2; 13509 if (help->dthps_maxprovs > dtrace_helper_providers_max) 13510 help->dthps_maxprovs = dtrace_helper_providers_max; 13511 13512 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 13513 13514 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 13515 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 13516 13517 if (tmp_provs != NULL) { 13518 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 13519 sizeof (dtrace_helper_provider_t *)); 13520 kmem_free(tmp_provs, tmp_maxprovs * 13521 sizeof (dtrace_helper_provider_t *)); 13522 } 13523 } 13524 13525 help->dthps_provs[help->dthps_nprovs] = hprov; 13526 help->dthps_nprovs++; 13527 13528 return (0); 13529 } 13530 13531 static void 13532 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 13533 { 13534 mutex_enter(&dtrace_lock); 13535 13536 if (--hprov->dthp_ref == 0) { 13537 dof_hdr_t *dof; 13538 mutex_exit(&dtrace_lock); 13539 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 13540 dtrace_dof_destroy(dof); 13541 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 13542 } else { 13543 mutex_exit(&dtrace_lock); 13544 } 13545 } 13546 13547 static int 13548 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 13549 { 13550 uintptr_t daddr = (uintptr_t)dof; 13551 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 13552 dof_provider_t *provider; 13553 dof_probe_t *probe; 13554 uint8_t *arg; 13555 char *strtab, *typestr; 13556 dof_stridx_t typeidx; 13557 size_t typesz; 13558 uint_t nprobes, j, k; 13559 13560 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 13561 13562 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 13563 dtrace_dof_error(dof, "misaligned section offset"); 13564 return (-1); 13565 } 13566 13567 /* 13568 * The section needs to be large enough to contain the DOF provider 13569 * structure appropriate for the given version. 13570 */ 13571 if (sec->dofs_size < 13572 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 13573 offsetof(dof_provider_t, dofpv_prenoffs) : 13574 sizeof (dof_provider_t))) { 13575 dtrace_dof_error(dof, "provider section too small"); 13576 return (-1); 13577 } 13578 13579 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 13580 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 13581 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 13582 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 13583 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 13584 13585 if (str_sec == NULL || prb_sec == NULL || 13586 arg_sec == NULL || off_sec == NULL) 13587 return (-1); 13588 13589 enoff_sec = NULL; 13590 13591 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 13592 provider->dofpv_prenoffs != DOF_SECT_NONE && 13593 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 13594 provider->dofpv_prenoffs)) == NULL) 13595 return (-1); 13596 13597 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 13598 13599 if (provider->dofpv_name >= str_sec->dofs_size || 13600 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 13601 dtrace_dof_error(dof, "invalid provider name"); 13602 return (-1); 13603 } 13604 13605 if (prb_sec->dofs_entsize == 0 || 13606 prb_sec->dofs_entsize > prb_sec->dofs_size) { 13607 dtrace_dof_error(dof, "invalid entry size"); 13608 return (-1); 13609 } 13610 13611 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 13612 dtrace_dof_error(dof, "misaligned entry size"); 13613 return (-1); 13614 } 13615 13616 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 13617 dtrace_dof_error(dof, "invalid entry size"); 13618 return (-1); 13619 } 13620 13621 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 13622 dtrace_dof_error(dof, "misaligned section offset"); 13623 return (-1); 13624 } 13625 13626 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 13627 dtrace_dof_error(dof, "invalid entry size"); 13628 return (-1); 13629 } 13630 13631 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 13632 13633 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 13634 13635 /* 13636 * Take a pass through the probes to check for errors. 13637 */ 13638 for (j = 0; j < nprobes; j++) { 13639 probe = (dof_probe_t *)(uintptr_t)(daddr + 13640 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 13641 13642 if (probe->dofpr_func >= str_sec->dofs_size) { 13643 dtrace_dof_error(dof, "invalid function name"); 13644 return (-1); 13645 } 13646 13647 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 13648 dtrace_dof_error(dof, "function name too long"); 13649 return (-1); 13650 } 13651 13652 if (probe->dofpr_name >= str_sec->dofs_size || 13653 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 13654 dtrace_dof_error(dof, "invalid probe name"); 13655 return (-1); 13656 } 13657 13658 /* 13659 * The offset count must not wrap the index, and the offsets 13660 * must also not overflow the section's data. 13661 */ 13662 if (probe->dofpr_offidx + probe->dofpr_noffs < 13663 probe->dofpr_offidx || 13664 (probe->dofpr_offidx + probe->dofpr_noffs) * 13665 off_sec->dofs_entsize > off_sec->dofs_size) { 13666 dtrace_dof_error(dof, "invalid probe offset"); 13667 return (-1); 13668 } 13669 13670 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 13671 /* 13672 * If there's no is-enabled offset section, make sure 13673 * there aren't any is-enabled offsets. Otherwise 13674 * perform the same checks as for probe offsets 13675 * (immediately above). 13676 */ 13677 if (enoff_sec == NULL) { 13678 if (probe->dofpr_enoffidx != 0 || 13679 probe->dofpr_nenoffs != 0) { 13680 dtrace_dof_error(dof, "is-enabled " 13681 "offsets with null section"); 13682 return (-1); 13683 } 13684 } else if (probe->dofpr_enoffidx + 13685 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 13686 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 13687 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 13688 dtrace_dof_error(dof, "invalid is-enabled " 13689 "offset"); 13690 return (-1); 13691 } 13692 13693 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 13694 dtrace_dof_error(dof, "zero probe and " 13695 "is-enabled offsets"); 13696 return (-1); 13697 } 13698 } else if (probe->dofpr_noffs == 0) { 13699 dtrace_dof_error(dof, "zero probe offsets"); 13700 return (-1); 13701 } 13702 13703 if (probe->dofpr_argidx + probe->dofpr_xargc < 13704 probe->dofpr_argidx || 13705 (probe->dofpr_argidx + probe->dofpr_xargc) * 13706 arg_sec->dofs_entsize > arg_sec->dofs_size) { 13707 dtrace_dof_error(dof, "invalid args"); 13708 return (-1); 13709 } 13710 13711 typeidx = probe->dofpr_nargv; 13712 typestr = strtab + probe->dofpr_nargv; 13713 for (k = 0; k < probe->dofpr_nargc; k++) { 13714 if (typeidx >= str_sec->dofs_size) { 13715 dtrace_dof_error(dof, "bad " 13716 "native argument type"); 13717 return (-1); 13718 } 13719 13720 typesz = strlen(typestr) + 1; 13721 if (typesz > DTRACE_ARGTYPELEN) { 13722 dtrace_dof_error(dof, "native " 13723 "argument type too long"); 13724 return (-1); 13725 } 13726 typeidx += typesz; 13727 typestr += typesz; 13728 } 13729 13730 typeidx = probe->dofpr_xargv; 13731 typestr = strtab + probe->dofpr_xargv; 13732 for (k = 0; k < probe->dofpr_xargc; k++) { 13733 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 13734 dtrace_dof_error(dof, "bad " 13735 "native argument index"); 13736 return (-1); 13737 } 13738 13739 if (typeidx >= str_sec->dofs_size) { 13740 dtrace_dof_error(dof, "bad " 13741 "translated argument type"); 13742 return (-1); 13743 } 13744 13745 typesz = strlen(typestr) + 1; 13746 if (typesz > DTRACE_ARGTYPELEN) { 13747 dtrace_dof_error(dof, "translated argument " 13748 "type too long"); 13749 return (-1); 13750 } 13751 13752 typeidx += typesz; 13753 typestr += typesz; 13754 } 13755 } 13756 13757 return (0); 13758 } 13759 13760 static int 13761 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 13762 { 13763 dtrace_helpers_t *help; 13764 dtrace_vstate_t *vstate; 13765 dtrace_enabling_t *enab = NULL; 13766 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 13767 uintptr_t daddr = (uintptr_t)dof; 13768 13769 ASSERT(MUTEX_HELD(&dtrace_lock)); 13770 13771 if ((help = curproc->p_dtrace_helpers) == NULL) 13772 help = dtrace_helpers_create(curproc); 13773 13774 vstate = &help->dthps_vstate; 13775 13776 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 13777 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 13778 dtrace_dof_destroy(dof); 13779 return (rv); 13780 } 13781 13782 /* 13783 * Look for helper providers and validate their descriptions. 13784 */ 13785 if (dhp != NULL) { 13786 for (i = 0; i < dof->dofh_secnum; i++) { 13787 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 13788 dof->dofh_secoff + i * dof->dofh_secsize); 13789 13790 if (sec->dofs_type != DOF_SECT_PROVIDER) 13791 continue; 13792 13793 if (dtrace_helper_provider_validate(dof, sec) != 0) { 13794 dtrace_enabling_destroy(enab); 13795 dtrace_dof_destroy(dof); 13796 return (-1); 13797 } 13798 13799 nprovs++; 13800 } 13801 } 13802 13803 /* 13804 * Now we need to walk through the ECB descriptions in the enabling. 13805 */ 13806 for (i = 0; i < enab->dten_ndesc; i++) { 13807 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 13808 dtrace_probedesc_t *desc = &ep->dted_probe; 13809 13810 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 13811 continue; 13812 13813 if (strcmp(desc->dtpd_mod, "helper") != 0) 13814 continue; 13815 13816 if (strcmp(desc->dtpd_func, "ustack") != 0) 13817 continue; 13818 13819 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 13820 ep)) != 0) { 13821 /* 13822 * Adding this helper action failed -- we are now going 13823 * to rip out the entire generation and return failure. 13824 */ 13825 (void) dtrace_helper_destroygen(help->dthps_generation); 13826 dtrace_enabling_destroy(enab); 13827 dtrace_dof_destroy(dof); 13828 return (-1); 13829 } 13830 13831 nhelpers++; 13832 } 13833 13834 if (nhelpers < enab->dten_ndesc) 13835 dtrace_dof_error(dof, "unmatched helpers"); 13836 13837 gen = help->dthps_generation++; 13838 dtrace_enabling_destroy(enab); 13839 13840 if (dhp != NULL && nprovs > 0) { 13841 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 13842 if (dtrace_helper_provider_add(dhp, gen) == 0) { 13843 mutex_exit(&dtrace_lock); 13844 dtrace_helper_provider_register(curproc, help, dhp); 13845 mutex_enter(&dtrace_lock); 13846 13847 destroy = 0; 13848 } 13849 } 13850 13851 if (destroy) 13852 dtrace_dof_destroy(dof); 13853 13854 return (gen); 13855 } 13856 13857 static dtrace_helpers_t * 13858 dtrace_helpers_create(proc_t *p) 13859 { 13860 dtrace_helpers_t *help; 13861 13862 ASSERT(MUTEX_HELD(&dtrace_lock)); 13863 ASSERT(p->p_dtrace_helpers == NULL); 13864 13865 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 13866 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 13867 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 13868 13869 p->p_dtrace_helpers = help; 13870 dtrace_helpers++; 13871 13872 return (help); 13873 } 13874 13875 static void 13876 dtrace_helpers_destroy(void) 13877 { 13878 dtrace_helpers_t *help; 13879 dtrace_vstate_t *vstate; 13880 proc_t *p = curproc; 13881 int i; 13882 13883 mutex_enter(&dtrace_lock); 13884 13885 ASSERT(p->p_dtrace_helpers != NULL); 13886 ASSERT(dtrace_helpers > 0); 13887 13888 help = p->p_dtrace_helpers; 13889 vstate = &help->dthps_vstate; 13890 13891 /* 13892 * We're now going to lose the help from this process. 13893 */ 13894 p->p_dtrace_helpers = NULL; 13895 dtrace_sync(); 13896 13897 /* 13898 * Destory the helper actions. 13899 */ 13900 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 13901 dtrace_helper_action_t *h, *next; 13902 13903 for (h = help->dthps_actions[i]; h != NULL; h = next) { 13904 next = h->dtha_next; 13905 dtrace_helper_action_destroy(h, vstate); 13906 h = next; 13907 } 13908 } 13909 13910 mutex_exit(&dtrace_lock); 13911 13912 /* 13913 * Destroy the helper providers. 13914 */ 13915 if (help->dthps_maxprovs > 0) { 13916 mutex_enter(&dtrace_meta_lock); 13917 if (dtrace_meta_pid != NULL) { 13918 ASSERT(dtrace_deferred_pid == NULL); 13919 13920 for (i = 0; i < help->dthps_nprovs; i++) { 13921 dtrace_helper_provider_remove( 13922 &help->dthps_provs[i]->dthp_prov, p->p_pid); 13923 } 13924 } else { 13925 mutex_enter(&dtrace_lock); 13926 ASSERT(help->dthps_deferred == 0 || 13927 help->dthps_next != NULL || 13928 help->dthps_prev != NULL || 13929 help == dtrace_deferred_pid); 13930 13931 /* 13932 * Remove the helper from the deferred list. 13933 */ 13934 if (help->dthps_next != NULL) 13935 help->dthps_next->dthps_prev = help->dthps_prev; 13936 if (help->dthps_prev != NULL) 13937 help->dthps_prev->dthps_next = help->dthps_next; 13938 if (dtrace_deferred_pid == help) { 13939 dtrace_deferred_pid = help->dthps_next; 13940 ASSERT(help->dthps_prev == NULL); 13941 } 13942 13943 mutex_exit(&dtrace_lock); 13944 } 13945 13946 mutex_exit(&dtrace_meta_lock); 13947 13948 for (i = 0; i < help->dthps_nprovs; i++) { 13949 dtrace_helper_provider_destroy(help->dthps_provs[i]); 13950 } 13951 13952 kmem_free(help->dthps_provs, help->dthps_maxprovs * 13953 sizeof (dtrace_helper_provider_t *)); 13954 } 13955 13956 mutex_enter(&dtrace_lock); 13957 13958 dtrace_vstate_fini(&help->dthps_vstate); 13959 kmem_free(help->dthps_actions, 13960 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 13961 kmem_free(help, sizeof (dtrace_helpers_t)); 13962 13963 --dtrace_helpers; 13964 mutex_exit(&dtrace_lock); 13965 } 13966 13967 static void 13968 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 13969 { 13970 dtrace_helpers_t *help, *newhelp; 13971 dtrace_helper_action_t *helper, *new, *last; 13972 dtrace_difo_t *dp; 13973 dtrace_vstate_t *vstate; 13974 int i, j, sz, hasprovs = 0; 13975 13976 mutex_enter(&dtrace_lock); 13977 ASSERT(from->p_dtrace_helpers != NULL); 13978 ASSERT(dtrace_helpers > 0); 13979 13980 help = from->p_dtrace_helpers; 13981 newhelp = dtrace_helpers_create(to); 13982 ASSERT(to->p_dtrace_helpers != NULL); 13983 13984 newhelp->dthps_generation = help->dthps_generation; 13985 vstate = &newhelp->dthps_vstate; 13986 13987 /* 13988 * Duplicate the helper actions. 13989 */ 13990 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 13991 if ((helper = help->dthps_actions[i]) == NULL) 13992 continue; 13993 13994 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 13995 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 13996 KM_SLEEP); 13997 new->dtha_generation = helper->dtha_generation; 13998 13999 if ((dp = helper->dtha_predicate) != NULL) { 14000 dp = dtrace_difo_duplicate(dp, vstate); 14001 new->dtha_predicate = dp; 14002 } 14003 14004 new->dtha_nactions = helper->dtha_nactions; 14005 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 14006 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 14007 14008 for (j = 0; j < new->dtha_nactions; j++) { 14009 dtrace_difo_t *dp = helper->dtha_actions[j]; 14010 14011 ASSERT(dp != NULL); 14012 dp = dtrace_difo_duplicate(dp, vstate); 14013 new->dtha_actions[j] = dp; 14014 } 14015 14016 if (last != NULL) { 14017 last->dtha_next = new; 14018 } else { 14019 newhelp->dthps_actions[i] = new; 14020 } 14021 14022 last = new; 14023 } 14024 } 14025 14026 /* 14027 * Duplicate the helper providers and register them with the 14028 * DTrace framework. 14029 */ 14030 if (help->dthps_nprovs > 0) { 14031 newhelp->dthps_nprovs = help->dthps_nprovs; 14032 newhelp->dthps_maxprovs = help->dthps_nprovs; 14033 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 14034 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14035 for (i = 0; i < newhelp->dthps_nprovs; i++) { 14036 newhelp->dthps_provs[i] = help->dthps_provs[i]; 14037 newhelp->dthps_provs[i]->dthp_ref++; 14038 } 14039 14040 hasprovs = 1; 14041 } 14042 14043 mutex_exit(&dtrace_lock); 14044 14045 if (hasprovs) 14046 dtrace_helper_provider_register(to, newhelp, NULL); 14047 } 14048 14049 /* 14050 * DTrace Hook Functions 14051 */ 14052 static void 14053 dtrace_module_loaded(struct modctl *ctl) 14054 { 14055 dtrace_provider_t *prv; 14056 14057 mutex_enter(&dtrace_provider_lock); 14058 mutex_enter(&mod_lock); 14059 14060 ASSERT(ctl->mod_busy); 14061 14062 /* 14063 * We're going to call each providers per-module provide operation 14064 * specifying only this module. 14065 */ 14066 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 14067 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 14068 14069 mutex_exit(&mod_lock); 14070 mutex_exit(&dtrace_provider_lock); 14071 14072 /* 14073 * If we have any retained enablings, we need to match against them. 14074 * Enabling probes requires that cpu_lock be held, and we cannot hold 14075 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 14076 * module. (In particular, this happens when loading scheduling 14077 * classes.) So if we have any retained enablings, we need to dispatch 14078 * our task queue to do the match for us. 14079 */ 14080 mutex_enter(&dtrace_lock); 14081 14082 if (dtrace_retained == NULL) { 14083 mutex_exit(&dtrace_lock); 14084 return; 14085 } 14086 14087 (void) taskq_dispatch(dtrace_taskq, 14088 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 14089 14090 mutex_exit(&dtrace_lock); 14091 14092 /* 14093 * And now, for a little heuristic sleaze: in general, we want to 14094 * match modules as soon as they load. However, we cannot guarantee 14095 * this, because it would lead us to the lock ordering violation 14096 * outlined above. The common case, of course, is that cpu_lock is 14097 * _not_ held -- so we delay here for a clock tick, hoping that that's 14098 * long enough for the task queue to do its work. If it's not, it's 14099 * not a serious problem -- it just means that the module that we 14100 * just loaded may not be immediately instrumentable. 14101 */ 14102 delay(1); 14103 } 14104 14105 static void 14106 dtrace_module_unloaded(struct modctl *ctl) 14107 { 14108 dtrace_probe_t template, *probe, *first, *next; 14109 dtrace_provider_t *prov; 14110 14111 template.dtpr_mod = ctl->mod_modname; 14112 14113 mutex_enter(&dtrace_provider_lock); 14114 mutex_enter(&mod_lock); 14115 mutex_enter(&dtrace_lock); 14116 14117 if (dtrace_bymod == NULL) { 14118 /* 14119 * The DTrace module is loaded (obviously) but not attached; 14120 * we don't have any work to do. 14121 */ 14122 mutex_exit(&dtrace_provider_lock); 14123 mutex_exit(&mod_lock); 14124 mutex_exit(&dtrace_lock); 14125 return; 14126 } 14127 14128 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 14129 probe != NULL; probe = probe->dtpr_nextmod) { 14130 if (probe->dtpr_ecb != NULL) { 14131 mutex_exit(&dtrace_provider_lock); 14132 mutex_exit(&mod_lock); 14133 mutex_exit(&dtrace_lock); 14134 14135 /* 14136 * This shouldn't _actually_ be possible -- we're 14137 * unloading a module that has an enabled probe in it. 14138 * (It's normally up to the provider to make sure that 14139 * this can't happen.) However, because dtps_enable() 14140 * doesn't have a failure mode, there can be an 14141 * enable/unload race. Upshot: we don't want to 14142 * assert, but we're not going to disable the 14143 * probe, either. 14144 */ 14145 if (dtrace_err_verbose) { 14146 cmn_err(CE_WARN, "unloaded module '%s' had " 14147 "enabled probes", ctl->mod_modname); 14148 } 14149 14150 return; 14151 } 14152 } 14153 14154 probe = first; 14155 14156 for (first = NULL; probe != NULL; probe = next) { 14157 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 14158 14159 dtrace_probes[probe->dtpr_id - 1] = NULL; 14160 14161 next = probe->dtpr_nextmod; 14162 dtrace_hash_remove(dtrace_bymod, probe); 14163 dtrace_hash_remove(dtrace_byfunc, probe); 14164 dtrace_hash_remove(dtrace_byname, probe); 14165 14166 if (first == NULL) { 14167 first = probe; 14168 probe->dtpr_nextmod = NULL; 14169 } else { 14170 probe->dtpr_nextmod = first; 14171 first = probe; 14172 } 14173 } 14174 14175 /* 14176 * We've removed all of the module's probes from the hash chains and 14177 * from the probe array. Now issue a dtrace_sync() to be sure that 14178 * everyone has cleared out from any probe array processing. 14179 */ 14180 dtrace_sync(); 14181 14182 for (probe = first; probe != NULL; probe = first) { 14183 first = probe->dtpr_nextmod; 14184 prov = probe->dtpr_provider; 14185 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 14186 probe->dtpr_arg); 14187 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 14188 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 14189 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 14190 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 14191 kmem_free(probe, sizeof (dtrace_probe_t)); 14192 } 14193 14194 mutex_exit(&dtrace_lock); 14195 mutex_exit(&mod_lock); 14196 mutex_exit(&dtrace_provider_lock); 14197 } 14198 14199 void 14200 dtrace_suspend(void) 14201 { 14202 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 14203 } 14204 14205 void 14206 dtrace_resume(void) 14207 { 14208 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 14209 } 14210 14211 static int 14212 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 14213 { 14214 ASSERT(MUTEX_HELD(&cpu_lock)); 14215 mutex_enter(&dtrace_lock); 14216 14217 switch (what) { 14218 case CPU_CONFIG: { 14219 dtrace_state_t *state; 14220 dtrace_optval_t *opt, rs, c; 14221 14222 /* 14223 * For now, we only allocate a new buffer for anonymous state. 14224 */ 14225 if ((state = dtrace_anon.dta_state) == NULL) 14226 break; 14227 14228 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14229 break; 14230 14231 opt = state->dts_options; 14232 c = opt[DTRACEOPT_CPU]; 14233 14234 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 14235 break; 14236 14237 /* 14238 * Regardless of what the actual policy is, we're going to 14239 * temporarily set our resize policy to be manual. We're 14240 * also going to temporarily set our CPU option to denote 14241 * the newly configured CPU. 14242 */ 14243 rs = opt[DTRACEOPT_BUFRESIZE]; 14244 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 14245 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 14246 14247 (void) dtrace_state_buffers(state); 14248 14249 opt[DTRACEOPT_BUFRESIZE] = rs; 14250 opt[DTRACEOPT_CPU] = c; 14251 14252 break; 14253 } 14254 14255 case CPU_UNCONFIG: 14256 /* 14257 * We don't free the buffer in the CPU_UNCONFIG case. (The 14258 * buffer will be freed when the consumer exits.) 14259 */ 14260 break; 14261 14262 default: 14263 break; 14264 } 14265 14266 mutex_exit(&dtrace_lock); 14267 return (0); 14268 } 14269 14270 static void 14271 dtrace_cpu_setup_initial(processorid_t cpu) 14272 { 14273 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 14274 } 14275 14276 static void 14277 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 14278 { 14279 if (dtrace_toxranges >= dtrace_toxranges_max) { 14280 int osize, nsize; 14281 dtrace_toxrange_t *range; 14282 14283 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14284 14285 if (osize == 0) { 14286 ASSERT(dtrace_toxrange == NULL); 14287 ASSERT(dtrace_toxranges_max == 0); 14288 dtrace_toxranges_max = 1; 14289 } else { 14290 dtrace_toxranges_max <<= 1; 14291 } 14292 14293 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14294 range = kmem_zalloc(nsize, KM_SLEEP); 14295 14296 if (dtrace_toxrange != NULL) { 14297 ASSERT(osize != 0); 14298 bcopy(dtrace_toxrange, range, osize); 14299 kmem_free(dtrace_toxrange, osize); 14300 } 14301 14302 dtrace_toxrange = range; 14303 } 14304 14305 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); 14306 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); 14307 14308 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 14309 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 14310 dtrace_toxranges++; 14311 } 14312 14313 /* 14314 * DTrace Driver Cookbook Functions 14315 */ 14316 /*ARGSUSED*/ 14317 static int 14318 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 14319 { 14320 dtrace_provider_id_t id; 14321 dtrace_state_t *state = NULL; 14322 dtrace_enabling_t *enab; 14323 14324 mutex_enter(&cpu_lock); 14325 mutex_enter(&dtrace_provider_lock); 14326 mutex_enter(&dtrace_lock); 14327 14328 if (ddi_soft_state_init(&dtrace_softstate, 14329 sizeof (dtrace_state_t), 0) != 0) { 14330 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 14331 mutex_exit(&cpu_lock); 14332 mutex_exit(&dtrace_provider_lock); 14333 mutex_exit(&dtrace_lock); 14334 return (DDI_FAILURE); 14335 } 14336 14337 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 14338 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 14339 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 14340 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 14341 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 14342 ddi_remove_minor_node(devi, NULL); 14343 ddi_soft_state_fini(&dtrace_softstate); 14344 mutex_exit(&cpu_lock); 14345 mutex_exit(&dtrace_provider_lock); 14346 mutex_exit(&dtrace_lock); 14347 return (DDI_FAILURE); 14348 } 14349 14350 ddi_report_dev(devi); 14351 dtrace_devi = devi; 14352 14353 dtrace_modload = dtrace_module_loaded; 14354 dtrace_modunload = dtrace_module_unloaded; 14355 dtrace_cpu_init = dtrace_cpu_setup_initial; 14356 dtrace_helpers_cleanup = dtrace_helpers_destroy; 14357 dtrace_helpers_fork = dtrace_helpers_duplicate; 14358 dtrace_cpustart_init = dtrace_suspend; 14359 dtrace_cpustart_fini = dtrace_resume; 14360 dtrace_debugger_init = dtrace_suspend; 14361 dtrace_debugger_fini = dtrace_resume; 14362 14363 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 14364 14365 ASSERT(MUTEX_HELD(&cpu_lock)); 14366 14367 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 14368 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14369 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 14370 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 14371 VM_SLEEP | VMC_IDENTIFIER); 14372 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 14373 1, INT_MAX, 0); 14374 14375 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 14376 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 14377 NULL, NULL, NULL, NULL, NULL, 0); 14378 14379 ASSERT(MUTEX_HELD(&cpu_lock)); 14380 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 14381 offsetof(dtrace_probe_t, dtpr_nextmod), 14382 offsetof(dtrace_probe_t, dtpr_prevmod)); 14383 14384 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 14385 offsetof(dtrace_probe_t, dtpr_nextfunc), 14386 offsetof(dtrace_probe_t, dtpr_prevfunc)); 14387 14388 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 14389 offsetof(dtrace_probe_t, dtpr_nextname), 14390 offsetof(dtrace_probe_t, dtpr_prevname)); 14391 14392 if (dtrace_retain_max < 1) { 14393 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 14394 "setting to 1", dtrace_retain_max); 14395 dtrace_retain_max = 1; 14396 } 14397 14398 /* 14399 * Now discover our toxic ranges. 14400 */ 14401 dtrace_toxic_ranges(dtrace_toxrange_add); 14402 14403 /* 14404 * Before we register ourselves as a provider to our own framework, 14405 * we would like to assert that dtrace_provider is NULL -- but that's 14406 * not true if we were loaded as a dependency of a DTrace provider. 14407 * Once we've registered, we can assert that dtrace_provider is our 14408 * pseudo provider. 14409 */ 14410 (void) dtrace_register("dtrace", &dtrace_provider_attr, 14411 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 14412 14413 ASSERT(dtrace_provider != NULL); 14414 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 14415 14416 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 14417 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 14418 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 14419 dtrace_provider, NULL, NULL, "END", 0, NULL); 14420 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 14421 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 14422 14423 dtrace_anon_property(); 14424 mutex_exit(&cpu_lock); 14425 14426 /* 14427 * If DTrace helper tracing is enabled, we need to allocate the 14428 * trace buffer and initialize the values. 14429 */ 14430 if (dtrace_helptrace_enabled) { 14431 ASSERT(dtrace_helptrace_buffer == NULL); 14432 dtrace_helptrace_buffer = 14433 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 14434 dtrace_helptrace_next = 0; 14435 } 14436 14437 /* 14438 * If there are already providers, we must ask them to provide their 14439 * probes, and then match any anonymous enabling against them. Note 14440 * that there should be no other retained enablings at this time: 14441 * the only retained enablings at this time should be the anonymous 14442 * enabling. 14443 */ 14444 if (dtrace_anon.dta_enabling != NULL) { 14445 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 14446 14447 dtrace_enabling_provide(NULL); 14448 state = dtrace_anon.dta_state; 14449 14450 /* 14451 * We couldn't hold cpu_lock across the above call to 14452 * dtrace_enabling_provide(), but we must hold it to actually 14453 * enable the probes. We have to drop all of our locks, pick 14454 * up cpu_lock, and regain our locks before matching the 14455 * retained anonymous enabling. 14456 */ 14457 mutex_exit(&dtrace_lock); 14458 mutex_exit(&dtrace_provider_lock); 14459 14460 mutex_enter(&cpu_lock); 14461 mutex_enter(&dtrace_provider_lock); 14462 mutex_enter(&dtrace_lock); 14463 14464 if ((enab = dtrace_anon.dta_enabling) != NULL) 14465 (void) dtrace_enabling_match(enab, NULL); 14466 14467 mutex_exit(&cpu_lock); 14468 } 14469 14470 mutex_exit(&dtrace_lock); 14471 mutex_exit(&dtrace_provider_lock); 14472 14473 if (state != NULL) { 14474 /* 14475 * If we created any anonymous state, set it going now. 14476 */ 14477 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 14478 } 14479 14480 return (DDI_SUCCESS); 14481 } 14482 14483 /*ARGSUSED*/ 14484 static int 14485 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 14486 { 14487 dtrace_state_t *state; 14488 uint32_t priv; 14489 uid_t uid; 14490 zoneid_t zoneid; 14491 14492 if (getminor(*devp) == DTRACEMNRN_HELPER) 14493 return (0); 14494 14495 /* 14496 * If this wasn't an open with the "helper" minor, then it must be 14497 * the "dtrace" minor. 14498 */ 14499 if (getminor(*devp) != DTRACEMNRN_DTRACE) 14500 return (ENXIO); 14501 14502 /* 14503 * If no DTRACE_PRIV_* bits are set in the credential, then the 14504 * caller lacks sufficient permission to do anything with DTrace. 14505 */ 14506 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 14507 if (priv == DTRACE_PRIV_NONE) 14508 return (EACCES); 14509 14510 /* 14511 * Ask all providers to provide all their probes. 14512 */ 14513 mutex_enter(&dtrace_provider_lock); 14514 dtrace_probe_provide(NULL, NULL); 14515 mutex_exit(&dtrace_provider_lock); 14516 14517 mutex_enter(&cpu_lock); 14518 mutex_enter(&dtrace_lock); 14519 dtrace_opens++; 14520 dtrace_membar_producer(); 14521 14522 /* 14523 * If the kernel debugger is active (that is, if the kernel debugger 14524 * modified text in some way), we won't allow the open. 14525 */ 14526 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 14527 dtrace_opens--; 14528 mutex_exit(&cpu_lock); 14529 mutex_exit(&dtrace_lock); 14530 return (EBUSY); 14531 } 14532 14533 state = dtrace_state_create(devp, cred_p); 14534 mutex_exit(&cpu_lock); 14535 14536 if (state == NULL) { 14537 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 14538 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 14539 mutex_exit(&dtrace_lock); 14540 return (EAGAIN); 14541 } 14542 14543 mutex_exit(&dtrace_lock); 14544 14545 return (0); 14546 } 14547 14548 /*ARGSUSED*/ 14549 static int 14550 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 14551 { 14552 minor_t minor = getminor(dev); 14553 dtrace_state_t *state; 14554 14555 if (minor == DTRACEMNRN_HELPER) 14556 return (0); 14557 14558 state = ddi_get_soft_state(dtrace_softstate, minor); 14559 14560 mutex_enter(&cpu_lock); 14561 mutex_enter(&dtrace_lock); 14562 14563 if (state->dts_anon) { 14564 /* 14565 * There is anonymous state. Destroy that first. 14566 */ 14567 ASSERT(dtrace_anon.dta_state == NULL); 14568 dtrace_state_destroy(state->dts_anon); 14569 } 14570 14571 dtrace_state_destroy(state); 14572 ASSERT(dtrace_opens > 0); 14573 14574 /* 14575 * Only relinquish control of the kernel debugger interface when there 14576 * are no consumers and no anonymous enablings. 14577 */ 14578 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 14579 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 14580 14581 mutex_exit(&dtrace_lock); 14582 mutex_exit(&cpu_lock); 14583 14584 return (0); 14585 } 14586 14587 /*ARGSUSED*/ 14588 static int 14589 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 14590 { 14591 int rval; 14592 dof_helper_t help, *dhp = NULL; 14593 14594 switch (cmd) { 14595 case DTRACEHIOC_ADDDOF: 14596 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 14597 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 14598 return (EFAULT); 14599 } 14600 14601 dhp = &help; 14602 arg = (intptr_t)help.dofhp_dof; 14603 /*FALLTHROUGH*/ 14604 14605 case DTRACEHIOC_ADD: { 14606 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 14607 14608 if (dof == NULL) 14609 return (rval); 14610 14611 mutex_enter(&dtrace_lock); 14612 14613 /* 14614 * dtrace_helper_slurp() takes responsibility for the dof -- 14615 * it may free it now or it may save it and free it later. 14616 */ 14617 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 14618 *rv = rval; 14619 rval = 0; 14620 } else { 14621 rval = EINVAL; 14622 } 14623 14624 mutex_exit(&dtrace_lock); 14625 return (rval); 14626 } 14627 14628 case DTRACEHIOC_REMOVE: { 14629 mutex_enter(&dtrace_lock); 14630 rval = dtrace_helper_destroygen(arg); 14631 mutex_exit(&dtrace_lock); 14632 14633 return (rval); 14634 } 14635 14636 default: 14637 break; 14638 } 14639 14640 return (ENOTTY); 14641 } 14642 14643 /*ARGSUSED*/ 14644 static int 14645 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 14646 { 14647 minor_t minor = getminor(dev); 14648 dtrace_state_t *state; 14649 int rval; 14650 14651 if (minor == DTRACEMNRN_HELPER) 14652 return (dtrace_ioctl_helper(cmd, arg, rv)); 14653 14654 state = ddi_get_soft_state(dtrace_softstate, minor); 14655 14656 if (state->dts_anon) { 14657 ASSERT(dtrace_anon.dta_state == NULL); 14658 state = state->dts_anon; 14659 } 14660 14661 switch (cmd) { 14662 case DTRACEIOC_PROVIDER: { 14663 dtrace_providerdesc_t pvd; 14664 dtrace_provider_t *pvp; 14665 14666 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 14667 return (EFAULT); 14668 14669 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 14670 mutex_enter(&dtrace_provider_lock); 14671 14672 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 14673 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 14674 break; 14675 } 14676 14677 mutex_exit(&dtrace_provider_lock); 14678 14679 if (pvp == NULL) 14680 return (ESRCH); 14681 14682 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 14683 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 14684 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 14685 return (EFAULT); 14686 14687 return (0); 14688 } 14689 14690 case DTRACEIOC_EPROBE: { 14691 dtrace_eprobedesc_t epdesc; 14692 dtrace_ecb_t *ecb; 14693 dtrace_action_t *act; 14694 void *buf; 14695 size_t size; 14696 uintptr_t dest; 14697 int nrecs; 14698 14699 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 14700 return (EFAULT); 14701 14702 mutex_enter(&dtrace_lock); 14703 14704 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 14705 mutex_exit(&dtrace_lock); 14706 return (EINVAL); 14707 } 14708 14709 if (ecb->dte_probe == NULL) { 14710 mutex_exit(&dtrace_lock); 14711 return (EINVAL); 14712 } 14713 14714 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 14715 epdesc.dtepd_uarg = ecb->dte_uarg; 14716 epdesc.dtepd_size = ecb->dte_size; 14717 14718 nrecs = epdesc.dtepd_nrecs; 14719 epdesc.dtepd_nrecs = 0; 14720 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 14721 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 14722 continue; 14723 14724 epdesc.dtepd_nrecs++; 14725 } 14726 14727 /* 14728 * Now that we have the size, we need to allocate a temporary 14729 * buffer in which to store the complete description. We need 14730 * the temporary buffer to be able to drop dtrace_lock() 14731 * across the copyout(), below. 14732 */ 14733 size = sizeof (dtrace_eprobedesc_t) + 14734 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 14735 14736 buf = kmem_alloc(size, KM_SLEEP); 14737 dest = (uintptr_t)buf; 14738 14739 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 14740 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 14741 14742 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 14743 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 14744 continue; 14745 14746 if (nrecs-- == 0) 14747 break; 14748 14749 bcopy(&act->dta_rec, (void *)dest, 14750 sizeof (dtrace_recdesc_t)); 14751 dest += sizeof (dtrace_recdesc_t); 14752 } 14753 14754 mutex_exit(&dtrace_lock); 14755 14756 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 14757 kmem_free(buf, size); 14758 return (EFAULT); 14759 } 14760 14761 kmem_free(buf, size); 14762 return (0); 14763 } 14764 14765 case DTRACEIOC_AGGDESC: { 14766 dtrace_aggdesc_t aggdesc; 14767 dtrace_action_t *act; 14768 dtrace_aggregation_t *agg; 14769 int nrecs; 14770 uint32_t offs; 14771 dtrace_recdesc_t *lrec; 14772 void *buf; 14773 size_t size; 14774 uintptr_t dest; 14775 14776 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 14777 return (EFAULT); 14778 14779 mutex_enter(&dtrace_lock); 14780 14781 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 14782 mutex_exit(&dtrace_lock); 14783 return (EINVAL); 14784 } 14785 14786 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 14787 14788 nrecs = aggdesc.dtagd_nrecs; 14789 aggdesc.dtagd_nrecs = 0; 14790 14791 offs = agg->dtag_base; 14792 lrec = &agg->dtag_action.dta_rec; 14793 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 14794 14795 for (act = agg->dtag_first; ; act = act->dta_next) { 14796 ASSERT(act->dta_intuple || 14797 DTRACEACT_ISAGG(act->dta_kind)); 14798 14799 /* 14800 * If this action has a record size of zero, it 14801 * denotes an argument to the aggregating action. 14802 * Because the presence of this record doesn't (or 14803 * shouldn't) affect the way the data is interpreted, 14804 * we don't copy it out to save user-level the 14805 * confusion of dealing with a zero-length record. 14806 */ 14807 if (act->dta_rec.dtrd_size == 0) { 14808 ASSERT(agg->dtag_hasarg); 14809 continue; 14810 } 14811 14812 aggdesc.dtagd_nrecs++; 14813 14814 if (act == &agg->dtag_action) 14815 break; 14816 } 14817 14818 /* 14819 * Now that we have the size, we need to allocate a temporary 14820 * buffer in which to store the complete description. We need 14821 * the temporary buffer to be able to drop dtrace_lock() 14822 * across the copyout(), below. 14823 */ 14824 size = sizeof (dtrace_aggdesc_t) + 14825 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 14826 14827 buf = kmem_alloc(size, KM_SLEEP); 14828 dest = (uintptr_t)buf; 14829 14830 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 14831 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 14832 14833 for (act = agg->dtag_first; ; act = act->dta_next) { 14834 dtrace_recdesc_t rec = act->dta_rec; 14835 14836 /* 14837 * See the comment in the above loop for why we pass 14838 * over zero-length records. 14839 */ 14840 if (rec.dtrd_size == 0) { 14841 ASSERT(agg->dtag_hasarg); 14842 continue; 14843 } 14844 14845 if (nrecs-- == 0) 14846 break; 14847 14848 rec.dtrd_offset -= offs; 14849 bcopy(&rec, (void *)dest, sizeof (rec)); 14850 dest += sizeof (dtrace_recdesc_t); 14851 14852 if (act == &agg->dtag_action) 14853 break; 14854 } 14855 14856 mutex_exit(&dtrace_lock); 14857 14858 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 14859 kmem_free(buf, size); 14860 return (EFAULT); 14861 } 14862 14863 kmem_free(buf, size); 14864 return (0); 14865 } 14866 14867 case DTRACEIOC_ENABLE: { 14868 dof_hdr_t *dof; 14869 dtrace_enabling_t *enab = NULL; 14870 dtrace_vstate_t *vstate; 14871 int err = 0; 14872 14873 *rv = 0; 14874 14875 /* 14876 * If a NULL argument has been passed, we take this as our 14877 * cue to reevaluate our enablings. 14878 */ 14879 if (arg == NULL) { 14880 dtrace_enabling_matchall(); 14881 14882 return (0); 14883 } 14884 14885 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 14886 return (rval); 14887 14888 mutex_enter(&cpu_lock); 14889 mutex_enter(&dtrace_lock); 14890 vstate = &state->dts_vstate; 14891 14892 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 14893 mutex_exit(&dtrace_lock); 14894 mutex_exit(&cpu_lock); 14895 dtrace_dof_destroy(dof); 14896 return (EBUSY); 14897 } 14898 14899 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 14900 mutex_exit(&dtrace_lock); 14901 mutex_exit(&cpu_lock); 14902 dtrace_dof_destroy(dof); 14903 return (EINVAL); 14904 } 14905 14906 if ((rval = dtrace_dof_options(dof, state)) != 0) { 14907 dtrace_enabling_destroy(enab); 14908 mutex_exit(&dtrace_lock); 14909 mutex_exit(&cpu_lock); 14910 dtrace_dof_destroy(dof); 14911 return (rval); 14912 } 14913 14914 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 14915 err = dtrace_enabling_retain(enab); 14916 } else { 14917 dtrace_enabling_destroy(enab); 14918 } 14919 14920 mutex_exit(&cpu_lock); 14921 mutex_exit(&dtrace_lock); 14922 dtrace_dof_destroy(dof); 14923 14924 return (err); 14925 } 14926 14927 case DTRACEIOC_REPLICATE: { 14928 dtrace_repldesc_t desc; 14929 dtrace_probedesc_t *match = &desc.dtrpd_match; 14930 dtrace_probedesc_t *create = &desc.dtrpd_create; 14931 int err; 14932 14933 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 14934 return (EFAULT); 14935 14936 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 14937 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 14938 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 14939 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 14940 14941 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 14942 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 14943 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 14944 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 14945 14946 mutex_enter(&dtrace_lock); 14947 err = dtrace_enabling_replicate(state, match, create); 14948 mutex_exit(&dtrace_lock); 14949 14950 return (err); 14951 } 14952 14953 case DTRACEIOC_PROBEMATCH: 14954 case DTRACEIOC_PROBES: { 14955 dtrace_probe_t *probe = NULL; 14956 dtrace_probedesc_t desc; 14957 dtrace_probekey_t pkey; 14958 dtrace_id_t i; 14959 int m = 0; 14960 uint32_t priv; 14961 uid_t uid; 14962 zoneid_t zoneid; 14963 14964 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 14965 return (EFAULT); 14966 14967 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 14968 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 14969 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 14970 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 14971 14972 /* 14973 * Before we attempt to match this probe, we want to give 14974 * all providers the opportunity to provide it. 14975 */ 14976 if (desc.dtpd_id == DTRACE_IDNONE) { 14977 mutex_enter(&dtrace_provider_lock); 14978 dtrace_probe_provide(&desc, NULL); 14979 mutex_exit(&dtrace_provider_lock); 14980 desc.dtpd_id++; 14981 } 14982 14983 if (cmd == DTRACEIOC_PROBEMATCH) { 14984 dtrace_probekey(&desc, &pkey); 14985 pkey.dtpk_id = DTRACE_IDNONE; 14986 } 14987 14988 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 14989 14990 mutex_enter(&dtrace_lock); 14991 14992 if (cmd == DTRACEIOC_PROBEMATCH) { 14993 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 14994 if ((probe = dtrace_probes[i - 1]) != NULL && 14995 (m = dtrace_match_probe(probe, &pkey, 14996 priv, uid, zoneid)) != 0) 14997 break; 14998 } 14999 15000 if (m < 0) { 15001 mutex_exit(&dtrace_lock); 15002 return (EINVAL); 15003 } 15004 15005 } else { 15006 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15007 if ((probe = dtrace_probes[i - 1]) != NULL && 15008 dtrace_match_priv(probe, priv, uid, zoneid)) 15009 break; 15010 } 15011 } 15012 15013 if (probe == NULL) { 15014 mutex_exit(&dtrace_lock); 15015 return (ESRCH); 15016 } 15017 15018 dtrace_probe_description(probe, &desc); 15019 mutex_exit(&dtrace_lock); 15020 15021 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15022 return (EFAULT); 15023 15024 return (0); 15025 } 15026 15027 case DTRACEIOC_PROBEARG: { 15028 dtrace_argdesc_t desc; 15029 dtrace_probe_t *probe; 15030 dtrace_provider_t *prov; 15031 15032 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15033 return (EFAULT); 15034 15035 if (desc.dtargd_id == DTRACE_IDNONE) 15036 return (EINVAL); 15037 15038 if (desc.dtargd_ndx == DTRACE_ARGNONE) 15039 return (EINVAL); 15040 15041 mutex_enter(&dtrace_provider_lock); 15042 mutex_enter(&mod_lock); 15043 mutex_enter(&dtrace_lock); 15044 15045 if (desc.dtargd_id > dtrace_nprobes) { 15046 mutex_exit(&dtrace_lock); 15047 mutex_exit(&mod_lock); 15048 mutex_exit(&dtrace_provider_lock); 15049 return (EINVAL); 15050 } 15051 15052 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 15053 mutex_exit(&dtrace_lock); 15054 mutex_exit(&mod_lock); 15055 mutex_exit(&dtrace_provider_lock); 15056 return (EINVAL); 15057 } 15058 15059 mutex_exit(&dtrace_lock); 15060 15061 prov = probe->dtpr_provider; 15062 15063 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 15064 /* 15065 * There isn't any typed information for this probe. 15066 * Set the argument number to DTRACE_ARGNONE. 15067 */ 15068 desc.dtargd_ndx = DTRACE_ARGNONE; 15069 } else { 15070 desc.dtargd_native[0] = '\0'; 15071 desc.dtargd_xlate[0] = '\0'; 15072 desc.dtargd_mapping = desc.dtargd_ndx; 15073 15074 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 15075 probe->dtpr_id, probe->dtpr_arg, &desc); 15076 } 15077 15078 mutex_exit(&mod_lock); 15079 mutex_exit(&dtrace_provider_lock); 15080 15081 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15082 return (EFAULT); 15083 15084 return (0); 15085 } 15086 15087 case DTRACEIOC_GO: { 15088 processorid_t cpuid; 15089 rval = dtrace_state_go(state, &cpuid); 15090 15091 if (rval != 0) 15092 return (rval); 15093 15094 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15095 return (EFAULT); 15096 15097 return (0); 15098 } 15099 15100 case DTRACEIOC_STOP: { 15101 processorid_t cpuid; 15102 15103 mutex_enter(&dtrace_lock); 15104 rval = dtrace_state_stop(state, &cpuid); 15105 mutex_exit(&dtrace_lock); 15106 15107 if (rval != 0) 15108 return (rval); 15109 15110 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15111 return (EFAULT); 15112 15113 return (0); 15114 } 15115 15116 case DTRACEIOC_DOFGET: { 15117 dof_hdr_t hdr, *dof; 15118 uint64_t len; 15119 15120 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 15121 return (EFAULT); 15122 15123 mutex_enter(&dtrace_lock); 15124 dof = dtrace_dof_create(state); 15125 mutex_exit(&dtrace_lock); 15126 15127 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 15128 rval = copyout(dof, (void *)arg, len); 15129 dtrace_dof_destroy(dof); 15130 15131 return (rval == 0 ? 0 : EFAULT); 15132 } 15133 15134 case DTRACEIOC_AGGSNAP: 15135 case DTRACEIOC_BUFSNAP: { 15136 dtrace_bufdesc_t desc; 15137 caddr_t cached; 15138 dtrace_buffer_t *buf; 15139 15140 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15141 return (EFAULT); 15142 15143 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 15144 return (EINVAL); 15145 15146 mutex_enter(&dtrace_lock); 15147 15148 if (cmd == DTRACEIOC_BUFSNAP) { 15149 buf = &state->dts_buffer[desc.dtbd_cpu]; 15150 } else { 15151 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 15152 } 15153 15154 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 15155 size_t sz = buf->dtb_offset; 15156 15157 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 15158 mutex_exit(&dtrace_lock); 15159 return (EBUSY); 15160 } 15161 15162 /* 15163 * If this buffer has already been consumed, we're 15164 * going to indicate that there's nothing left here 15165 * to consume. 15166 */ 15167 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 15168 mutex_exit(&dtrace_lock); 15169 15170 desc.dtbd_size = 0; 15171 desc.dtbd_drops = 0; 15172 desc.dtbd_errors = 0; 15173 desc.dtbd_oldest = 0; 15174 sz = sizeof (desc); 15175 15176 if (copyout(&desc, (void *)arg, sz) != 0) 15177 return (EFAULT); 15178 15179 return (0); 15180 } 15181 15182 /* 15183 * If this is a ring buffer that has wrapped, we want 15184 * to copy the whole thing out. 15185 */ 15186 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 15187 dtrace_buffer_polish(buf); 15188 sz = buf->dtb_size; 15189 } 15190 15191 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 15192 mutex_exit(&dtrace_lock); 15193 return (EFAULT); 15194 } 15195 15196 desc.dtbd_size = sz; 15197 desc.dtbd_drops = buf->dtb_drops; 15198 desc.dtbd_errors = buf->dtb_errors; 15199 desc.dtbd_oldest = buf->dtb_xamot_offset; 15200 15201 mutex_exit(&dtrace_lock); 15202 15203 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15204 return (EFAULT); 15205 15206 buf->dtb_flags |= DTRACEBUF_CONSUMED; 15207 15208 return (0); 15209 } 15210 15211 if (buf->dtb_tomax == NULL) { 15212 ASSERT(buf->dtb_xamot == NULL); 15213 mutex_exit(&dtrace_lock); 15214 return (ENOENT); 15215 } 15216 15217 cached = buf->dtb_tomax; 15218 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 15219 15220 dtrace_xcall(desc.dtbd_cpu, 15221 (dtrace_xcall_t)dtrace_buffer_switch, buf); 15222 15223 state->dts_errors += buf->dtb_xamot_errors; 15224 15225 /* 15226 * If the buffers did not actually switch, then the cross call 15227 * did not take place -- presumably because the given CPU is 15228 * not in the ready set. If this is the case, we'll return 15229 * ENOENT. 15230 */ 15231 if (buf->dtb_tomax == cached) { 15232 ASSERT(buf->dtb_xamot != cached); 15233 mutex_exit(&dtrace_lock); 15234 return (ENOENT); 15235 } 15236 15237 ASSERT(cached == buf->dtb_xamot); 15238 15239 /* 15240 * We have our snapshot; now copy it out. 15241 */ 15242 if (copyout(buf->dtb_xamot, desc.dtbd_data, 15243 buf->dtb_xamot_offset) != 0) { 15244 mutex_exit(&dtrace_lock); 15245 return (EFAULT); 15246 } 15247 15248 desc.dtbd_size = buf->dtb_xamot_offset; 15249 desc.dtbd_drops = buf->dtb_xamot_drops; 15250 desc.dtbd_errors = buf->dtb_xamot_errors; 15251 desc.dtbd_oldest = 0; 15252 15253 mutex_exit(&dtrace_lock); 15254 15255 /* 15256 * Finally, copy out the buffer description. 15257 */ 15258 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15259 return (EFAULT); 15260 15261 return (0); 15262 } 15263 15264 case DTRACEIOC_CONF: { 15265 dtrace_conf_t conf; 15266 15267 bzero(&conf, sizeof (conf)); 15268 conf.dtc_difversion = DIF_VERSION; 15269 conf.dtc_difintregs = DIF_DIR_NREGS; 15270 conf.dtc_diftupregs = DIF_DTR_NREGS; 15271 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 15272 15273 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 15274 return (EFAULT); 15275 15276 return (0); 15277 } 15278 15279 case DTRACEIOC_STATUS: { 15280 dtrace_status_t stat; 15281 dtrace_dstate_t *dstate; 15282 int i, j; 15283 uint64_t nerrs; 15284 15285 /* 15286 * See the comment in dtrace_state_deadman() for the reason 15287 * for setting dts_laststatus to INT64_MAX before setting 15288 * it to the correct value. 15289 */ 15290 state->dts_laststatus = INT64_MAX; 15291 dtrace_membar_producer(); 15292 state->dts_laststatus = dtrace_gethrtime(); 15293 15294 bzero(&stat, sizeof (stat)); 15295 15296 mutex_enter(&dtrace_lock); 15297 15298 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 15299 mutex_exit(&dtrace_lock); 15300 return (ENOENT); 15301 } 15302 15303 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 15304 stat.dtst_exiting = 1; 15305 15306 nerrs = state->dts_errors; 15307 dstate = &state->dts_vstate.dtvs_dynvars; 15308 15309 for (i = 0; i < NCPU; i++) { 15310 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 15311 15312 stat.dtst_dyndrops += dcpu->dtdsc_drops; 15313 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 15314 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 15315 15316 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 15317 stat.dtst_filled++; 15318 15319 nerrs += state->dts_buffer[i].dtb_errors; 15320 15321 for (j = 0; j < state->dts_nspeculations; j++) { 15322 dtrace_speculation_t *spec; 15323 dtrace_buffer_t *buf; 15324 15325 spec = &state->dts_speculations[j]; 15326 buf = &spec->dtsp_buffer[i]; 15327 stat.dtst_specdrops += buf->dtb_xamot_drops; 15328 } 15329 } 15330 15331 stat.dtst_specdrops_busy = state->dts_speculations_busy; 15332 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 15333 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 15334 stat.dtst_dblerrors = state->dts_dblerrors; 15335 stat.dtst_killed = 15336 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 15337 stat.dtst_errors = nerrs; 15338 15339 mutex_exit(&dtrace_lock); 15340 15341 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 15342 return (EFAULT); 15343 15344 return (0); 15345 } 15346 15347 case DTRACEIOC_FORMAT: { 15348 dtrace_fmtdesc_t fmt; 15349 char *str; 15350 int len; 15351 15352 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 15353 return (EFAULT); 15354 15355 mutex_enter(&dtrace_lock); 15356 15357 if (fmt.dtfd_format == 0 || 15358 fmt.dtfd_format > state->dts_nformats) { 15359 mutex_exit(&dtrace_lock); 15360 return (EINVAL); 15361 } 15362 15363 /* 15364 * Format strings are allocated contiguously and they are 15365 * never freed; if a format index is less than the number 15366 * of formats, we can assert that the format map is non-NULL 15367 * and that the format for the specified index is non-NULL. 15368 */ 15369 ASSERT(state->dts_formats != NULL); 15370 str = state->dts_formats[fmt.dtfd_format - 1]; 15371 ASSERT(str != NULL); 15372 15373 len = strlen(str) + 1; 15374 15375 if (len > fmt.dtfd_length) { 15376 fmt.dtfd_length = len; 15377 15378 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 15379 mutex_exit(&dtrace_lock); 15380 return (EINVAL); 15381 } 15382 } else { 15383 if (copyout(str, fmt.dtfd_string, len) != 0) { 15384 mutex_exit(&dtrace_lock); 15385 return (EINVAL); 15386 } 15387 } 15388 15389 mutex_exit(&dtrace_lock); 15390 return (0); 15391 } 15392 15393 default: 15394 break; 15395 } 15396 15397 return (ENOTTY); 15398 } 15399 15400 /*ARGSUSED*/ 15401 static int 15402 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 15403 { 15404 dtrace_state_t *state; 15405 15406 switch (cmd) { 15407 case DDI_DETACH: 15408 break; 15409 15410 case DDI_SUSPEND: 15411 return (DDI_SUCCESS); 15412 15413 default: 15414 return (DDI_FAILURE); 15415 } 15416 15417 mutex_enter(&cpu_lock); 15418 mutex_enter(&dtrace_provider_lock); 15419 mutex_enter(&dtrace_lock); 15420 15421 ASSERT(dtrace_opens == 0); 15422 15423 if (dtrace_helpers > 0) { 15424 mutex_exit(&dtrace_provider_lock); 15425 mutex_exit(&dtrace_lock); 15426 mutex_exit(&cpu_lock); 15427 return (DDI_FAILURE); 15428 } 15429 15430 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 15431 mutex_exit(&dtrace_provider_lock); 15432 mutex_exit(&dtrace_lock); 15433 mutex_exit(&cpu_lock); 15434 return (DDI_FAILURE); 15435 } 15436 15437 dtrace_provider = NULL; 15438 15439 if ((state = dtrace_anon_grab()) != NULL) { 15440 /* 15441 * If there were ECBs on this state, the provider should 15442 * have not been allowed to detach; assert that there is 15443 * none. 15444 */ 15445 ASSERT(state->dts_necbs == 0); 15446 dtrace_state_destroy(state); 15447 15448 /* 15449 * If we're being detached with anonymous state, we need to 15450 * indicate to the kernel debugger that DTrace is now inactive. 15451 */ 15452 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15453 } 15454 15455 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 15456 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 15457 dtrace_cpu_init = NULL; 15458 dtrace_helpers_cleanup = NULL; 15459 dtrace_helpers_fork = NULL; 15460 dtrace_cpustart_init = NULL; 15461 dtrace_cpustart_fini = NULL; 15462 dtrace_debugger_init = NULL; 15463 dtrace_debugger_fini = NULL; 15464 dtrace_modload = NULL; 15465 dtrace_modunload = NULL; 15466 15467 mutex_exit(&cpu_lock); 15468 15469 if (dtrace_helptrace_enabled) { 15470 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 15471 dtrace_helptrace_buffer = NULL; 15472 } 15473 15474 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 15475 dtrace_probes = NULL; 15476 dtrace_nprobes = 0; 15477 15478 dtrace_hash_destroy(dtrace_bymod); 15479 dtrace_hash_destroy(dtrace_byfunc); 15480 dtrace_hash_destroy(dtrace_byname); 15481 dtrace_bymod = NULL; 15482 dtrace_byfunc = NULL; 15483 dtrace_byname = NULL; 15484 15485 kmem_cache_destroy(dtrace_state_cache); 15486 vmem_destroy(dtrace_minor); 15487 vmem_destroy(dtrace_arena); 15488 15489 if (dtrace_toxrange != NULL) { 15490 kmem_free(dtrace_toxrange, 15491 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 15492 dtrace_toxrange = NULL; 15493 dtrace_toxranges = 0; 15494 dtrace_toxranges_max = 0; 15495 } 15496 15497 ddi_remove_minor_node(dtrace_devi, NULL); 15498 dtrace_devi = NULL; 15499 15500 ddi_soft_state_fini(&dtrace_softstate); 15501 15502 ASSERT(dtrace_vtime_references == 0); 15503 ASSERT(dtrace_opens == 0); 15504 ASSERT(dtrace_retained == NULL); 15505 15506 mutex_exit(&dtrace_lock); 15507 mutex_exit(&dtrace_provider_lock); 15508 15509 /* 15510 * We don't destroy the task queue until after we have dropped our 15511 * locks (taskq_destroy() may block on running tasks). To prevent 15512 * attempting to do work after we have effectively detached but before 15513 * the task queue has been destroyed, all tasks dispatched via the 15514 * task queue must check that DTrace is still attached before 15515 * performing any operation. 15516 */ 15517 taskq_destroy(dtrace_taskq); 15518 dtrace_taskq = NULL; 15519 15520 return (DDI_SUCCESS); 15521 } 15522 15523 /*ARGSUSED*/ 15524 static int 15525 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 15526 { 15527 int error; 15528 15529 switch (infocmd) { 15530 case DDI_INFO_DEVT2DEVINFO: 15531 *result = (void *)dtrace_devi; 15532 error = DDI_SUCCESS; 15533 break; 15534 case DDI_INFO_DEVT2INSTANCE: 15535 *result = (void *)0; 15536 error = DDI_SUCCESS; 15537 break; 15538 default: 15539 error = DDI_FAILURE; 15540 } 15541 return (error); 15542 } 15543 15544 static struct cb_ops dtrace_cb_ops = { 15545 dtrace_open, /* open */ 15546 dtrace_close, /* close */ 15547 nulldev, /* strategy */ 15548 nulldev, /* print */ 15549 nodev, /* dump */ 15550 nodev, /* read */ 15551 nodev, /* write */ 15552 dtrace_ioctl, /* ioctl */ 15553 nodev, /* devmap */ 15554 nodev, /* mmap */ 15555 nodev, /* segmap */ 15556 nochpoll, /* poll */ 15557 ddi_prop_op, /* cb_prop_op */ 15558 0, /* streamtab */ 15559 D_NEW | D_MP /* Driver compatibility flag */ 15560 }; 15561 15562 static struct dev_ops dtrace_ops = { 15563 DEVO_REV, /* devo_rev */ 15564 0, /* refcnt */ 15565 dtrace_info, /* get_dev_info */ 15566 nulldev, /* identify */ 15567 nulldev, /* probe */ 15568 dtrace_attach, /* attach */ 15569 dtrace_detach, /* detach */ 15570 nodev, /* reset */ 15571 &dtrace_cb_ops, /* driver operations */ 15572 NULL, /* bus operations */ 15573 nodev, /* dev power */ 15574 ddi_quiesce_not_needed, /* quiesce */ 15575 }; 15576 15577 static struct modldrv modldrv = { 15578 &mod_driverops, /* module type (this is a pseudo driver) */ 15579 "Dynamic Tracing", /* name of module */ 15580 &dtrace_ops, /* driver ops */ 15581 }; 15582 15583 static struct modlinkage modlinkage = { 15584 MODREV_1, 15585 (void *)&modldrv, 15586 NULL 15587 }; 15588 15589 int 15590 _init(void) 15591 { 15592 return (mod_install(&modlinkage)); 15593 } 15594 15595 int 15596 _info(struct modinfo *modinfop) 15597 { 15598 return (mod_info(&modlinkage, modinfop)); 15599 } 15600 15601 int 15602 _fini(void) 15603 { 15604 return (mod_remove(&modlinkage)); 15605 } 15606