1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 25 * Copyright (c) 2012 by Delphix. All rights reserved. 26 */ 27 28 /* 29 * DTrace - Dynamic Tracing for Solaris 30 * 31 * This is the implementation of the Solaris Dynamic Tracing framework 32 * (DTrace). The user-visible interface to DTrace is described at length in 33 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 34 * library, the in-kernel DTrace framework, and the DTrace providers are 35 * described in the block comments in the <sys/dtrace.h> header file. The 36 * internal architecture of DTrace is described in the block comments in the 37 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 38 * implementation very much assume mastery of all of these sources; if one has 39 * an unanswered question about the implementation, one should consult them 40 * first. 41 * 42 * The functions here are ordered roughly as follows: 43 * 44 * - Probe context functions 45 * - Probe hashing functions 46 * - Non-probe context utility functions 47 * - Matching functions 48 * - Provider-to-Framework API functions 49 * - Probe management functions 50 * - DIF object functions 51 * - Format functions 52 * - Predicate functions 53 * - ECB functions 54 * - Buffer functions 55 * - Enabling functions 56 * - DOF functions 57 * - Anonymous enabling functions 58 * - Consumer state functions 59 * - Helper functions 60 * - Hook functions 61 * - Driver cookbook functions 62 * 63 * Each group of functions begins with a block comment labelled the "DTrace 64 * [Group] Functions", allowing one to find each block by searching forward 65 * on capital-f functions. 66 */ 67 #include <sys/errno.h> 68 #include <sys/stat.h> 69 #include <sys/modctl.h> 70 #include <sys/conf.h> 71 #include <sys/systm.h> 72 #include <sys/ddi.h> 73 #include <sys/sunddi.h> 74 #include <sys/cpuvar.h> 75 #include <sys/kmem.h> 76 #include <sys/strsubr.h> 77 #include <sys/sysmacros.h> 78 #include <sys/dtrace_impl.h> 79 #include <sys/atomic.h> 80 #include <sys/cmn_err.h> 81 #include <sys/mutex_impl.h> 82 #include <sys/rwlock_impl.h> 83 #include <sys/ctf_api.h> 84 #include <sys/panic.h> 85 #include <sys/priv_impl.h> 86 #include <sys/policy.h> 87 #include <sys/cred_impl.h> 88 #include <sys/procfs_isa.h> 89 #include <sys/taskq.h> 90 #include <sys/mkdev.h> 91 #include <sys/kdi.h> 92 #include <sys/zone.h> 93 #include <sys/socket.h> 94 #include <netinet/in.h> 95 96 /* 97 * DTrace Tunable Variables 98 * 99 * The following variables may be tuned by adding a line to /etc/system that 100 * includes both the name of the DTrace module ("dtrace") and the name of the 101 * variable. For example: 102 * 103 * set dtrace:dtrace_destructive_disallow = 1 104 * 105 * In general, the only variables that one should be tuning this way are those 106 * that affect system-wide DTrace behavior, and for which the default behavior 107 * is undesirable. Most of these variables are tunable on a per-consumer 108 * basis using DTrace options, and need not be tuned on a system-wide basis. 109 * When tuning these variables, avoid pathological values; while some attempt 110 * is made to verify the integrity of these variables, they are not considered 111 * part of the supported interface to DTrace, and they are therefore not 112 * checked comprehensively. Further, these variables should not be tuned 113 * dynamically via "mdb -kw" or other means; they should only be tuned via 114 * /etc/system. 115 */ 116 int dtrace_destructive_disallow = 0; 117 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 118 size_t dtrace_difo_maxsize = (256 * 1024); 119 dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 120 size_t dtrace_global_maxsize = (16 * 1024); 121 size_t dtrace_actions_max = (16 * 1024); 122 size_t dtrace_retain_max = 1024; 123 dtrace_optval_t dtrace_helper_actions_max = 1024; 124 dtrace_optval_t dtrace_helper_providers_max = 32; 125 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 126 size_t dtrace_strsize_default = 256; 127 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 128 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 129 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 130 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 131 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 132 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 133 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 134 dtrace_optval_t dtrace_nspec_default = 1; 135 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 136 dtrace_optval_t dtrace_stackframes_default = 20; 137 dtrace_optval_t dtrace_ustackframes_default = 20; 138 dtrace_optval_t dtrace_jstackframes_default = 50; 139 dtrace_optval_t dtrace_jstackstrsize_default = 512; 140 int dtrace_msgdsize_max = 128; 141 hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 142 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 143 int dtrace_devdepth_max = 32; 144 int dtrace_err_verbose; 145 hrtime_t dtrace_deadman_interval = NANOSEC; 146 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 147 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 148 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 149 150 /* 151 * DTrace External Variables 152 * 153 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 154 * available to DTrace consumers via the backtick (`) syntax. One of these, 155 * dtrace_zero, is made deliberately so: it is provided as a source of 156 * well-known, zero-filled memory. While this variable is not documented, 157 * it is used by some translators as an implementation detail. 158 */ 159 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 160 161 /* 162 * DTrace Internal Variables 163 */ 164 static dev_info_t *dtrace_devi; /* device info */ 165 static vmem_t *dtrace_arena; /* probe ID arena */ 166 static vmem_t *dtrace_minor; /* minor number arena */ 167 static taskq_t *dtrace_taskq; /* task queue */ 168 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 169 static int dtrace_nprobes; /* number of probes */ 170 static dtrace_provider_t *dtrace_provider; /* provider list */ 171 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 172 static int dtrace_opens; /* number of opens */ 173 static int dtrace_helpers; /* number of helpers */ 174 static int dtrace_getf; /* number of unpriv getf()s */ 175 static void *dtrace_softstate; /* softstate pointer */ 176 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 177 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 178 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 179 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 180 static int dtrace_toxranges; /* number of toxic ranges */ 181 static int dtrace_toxranges_max; /* size of toxic range array */ 182 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 183 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 184 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 185 static kthread_t *dtrace_panicked; /* panicking thread */ 186 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 187 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 188 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 189 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 190 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 191 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 192 static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 193 194 /* 195 * DTrace Locking 196 * DTrace is protected by three (relatively coarse-grained) locks: 197 * 198 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 199 * including enabling state, probes, ECBs, consumer state, helper state, 200 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 201 * probe context is lock-free -- synchronization is handled via the 202 * dtrace_sync() cross call mechanism. 203 * 204 * (2) dtrace_provider_lock is required when manipulating provider state, or 205 * when provider state must be held constant. 206 * 207 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 208 * when meta provider state must be held constant. 209 * 210 * The lock ordering between these three locks is dtrace_meta_lock before 211 * dtrace_provider_lock before dtrace_lock. (In particular, there are 212 * several places where dtrace_provider_lock is held by the framework as it 213 * calls into the providers -- which then call back into the framework, 214 * grabbing dtrace_lock.) 215 * 216 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 217 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 218 * role as a coarse-grained lock; it is acquired before both of these locks. 219 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 220 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 221 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 222 * acquired _between_ dtrace_provider_lock and dtrace_lock. 223 */ 224 static kmutex_t dtrace_lock; /* probe state lock */ 225 static kmutex_t dtrace_provider_lock; /* provider state lock */ 226 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 227 228 /* 229 * DTrace Provider Variables 230 * 231 * These are the variables relating to DTrace as a provider (that is, the 232 * provider of the BEGIN, END, and ERROR probes). 233 */ 234 static dtrace_pattr_t dtrace_provider_attr = { 235 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 236 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 237 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 238 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 239 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 240 }; 241 242 static void 243 dtrace_nullop(void) 244 {} 245 246 static int 247 dtrace_enable_nullop(void) 248 { 249 return (0); 250 } 251 252 static dtrace_pops_t dtrace_provider_ops = { 253 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, 254 (void (*)(void *, struct modctl *))dtrace_nullop, 255 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop, 256 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 257 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 258 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 259 NULL, 260 NULL, 261 NULL, 262 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 263 }; 264 265 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 266 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 267 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 268 269 /* 270 * DTrace Helper Tracing Variables 271 * 272 * These variables should be set dynamically to enable helper tracing. The 273 * only variables that should be set are dtrace_helptrace_enable (which should 274 * be set to a non-zero value to allocate helper tracing buffers on the next 275 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a 276 * non-zero value to deallocate helper tracing buffers on the next close of 277 * /dev/dtrace). When (and only when) helper tracing is disabled, the 278 * buffer size may also be set via dtrace_helptrace_bufsize. 279 */ 280 int dtrace_helptrace_enable = 0; 281 int dtrace_helptrace_disable = 0; 282 int dtrace_helptrace_bufsize = 16 * 1024 * 1024; 283 uint32_t dtrace_helptrace_nlocals; 284 static dtrace_helptrace_t *dtrace_helptrace_buffer; 285 static uint32_t dtrace_helptrace_next = 0; 286 static int dtrace_helptrace_wrapped = 0; 287 288 /* 289 * DTrace Error Hashing 290 * 291 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 292 * table. This is very useful for checking coverage of tests that are 293 * expected to induce DIF or DOF processing errors, and may be useful for 294 * debugging problems in the DIF code generator or in DOF generation . The 295 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 296 */ 297 #ifdef DEBUG 298 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 299 static const char *dtrace_errlast; 300 static kthread_t *dtrace_errthread; 301 static kmutex_t dtrace_errlock; 302 #endif 303 304 /* 305 * DTrace Macros and Constants 306 * 307 * These are various macros that are useful in various spots in the 308 * implementation, along with a few random constants that have no meaning 309 * outside of the implementation. There is no real structure to this cpp 310 * mishmash -- but is there ever? 311 */ 312 #define DTRACE_HASHSTR(hash, probe) \ 313 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 314 315 #define DTRACE_HASHNEXT(hash, probe) \ 316 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 317 318 #define DTRACE_HASHPREV(hash, probe) \ 319 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 320 321 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 322 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 323 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 324 325 #define DTRACE_AGGHASHSIZE_SLEW 17 326 327 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 328 329 /* 330 * The key for a thread-local variable consists of the lower 61 bits of the 331 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 332 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 333 * equal to a variable identifier. This is necessary (but not sufficient) to 334 * assure that global associative arrays never collide with thread-local 335 * variables. To guarantee that they cannot collide, we must also define the 336 * order for keying dynamic variables. That order is: 337 * 338 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 339 * 340 * Because the variable-key and the tls-key are in orthogonal spaces, there is 341 * no way for a global variable key signature to match a thread-local key 342 * signature. 343 */ 344 #define DTRACE_TLS_THRKEY(where) { \ 345 uint_t intr = 0; \ 346 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 347 for (; actv; actv >>= 1) \ 348 intr++; \ 349 ASSERT(intr < (1 << 3)); \ 350 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 351 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 352 } 353 354 #define DT_BSWAP_8(x) ((x) & 0xff) 355 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 356 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 357 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 358 359 #define DT_MASK_LO 0x00000000FFFFFFFFULL 360 361 #define DTRACE_STORE(type, tomax, offset, what) \ 362 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 363 364 #ifndef __i386 365 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 366 if (addr & (size - 1)) { \ 367 *flags |= CPU_DTRACE_BADALIGN; \ 368 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 369 return (0); \ 370 } 371 #else 372 #define DTRACE_ALIGNCHECK(addr, size, flags) 373 #endif 374 375 /* 376 * Test whether a range of memory starting at testaddr of size testsz falls 377 * within the range of memory described by addr, sz. We take care to avoid 378 * problems with overflow and underflow of the unsigned quantities, and 379 * disallow all negative sizes. Ranges of size 0 are allowed. 380 */ 381 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 382 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 383 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 384 (testaddr) + (testsz) >= (testaddr)) 385 386 /* 387 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 388 * alloc_sz on the righthand side of the comparison in order to avoid overflow 389 * or underflow in the comparison with it. This is simpler than the INRANGE 390 * check above, because we know that the dtms_scratch_ptr is valid in the 391 * range. Allocations of size zero are allowed. 392 */ 393 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 394 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 395 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 396 397 #define DTRACE_LOADFUNC(bits) \ 398 /*CSTYLED*/ \ 399 uint##bits##_t \ 400 dtrace_load##bits(uintptr_t addr) \ 401 { \ 402 size_t size = bits / NBBY; \ 403 /*CSTYLED*/ \ 404 uint##bits##_t rval; \ 405 int i; \ 406 volatile uint16_t *flags = (volatile uint16_t *) \ 407 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ 408 \ 409 DTRACE_ALIGNCHECK(addr, size, flags); \ 410 \ 411 for (i = 0; i < dtrace_toxranges; i++) { \ 412 if (addr >= dtrace_toxrange[i].dtt_limit) \ 413 continue; \ 414 \ 415 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 416 continue; \ 417 \ 418 /* \ 419 * This address falls within a toxic region; return 0. \ 420 */ \ 421 *flags |= CPU_DTRACE_BADADDR; \ 422 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ 423 return (0); \ 424 } \ 425 \ 426 *flags |= CPU_DTRACE_NOFAULT; \ 427 /*CSTYLED*/ \ 428 rval = *((volatile uint##bits##_t *)addr); \ 429 *flags &= ~CPU_DTRACE_NOFAULT; \ 430 \ 431 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 432 } 433 434 #ifdef _LP64 435 #define dtrace_loadptr dtrace_load64 436 #else 437 #define dtrace_loadptr dtrace_load32 438 #endif 439 440 #define DTRACE_DYNHASH_FREE 0 441 #define DTRACE_DYNHASH_SINK 1 442 #define DTRACE_DYNHASH_VALID 2 443 444 #define DTRACE_MATCH_FAIL -1 445 #define DTRACE_MATCH_NEXT 0 446 #define DTRACE_MATCH_DONE 1 447 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 448 #define DTRACE_STATE_ALIGN 64 449 450 #define DTRACE_FLAGS2FLT(flags) \ 451 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 452 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 453 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 454 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 455 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 456 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 457 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 458 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 459 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 460 DTRACEFLT_UNKNOWN) 461 462 #define DTRACEACT_ISSTRING(act) \ 463 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 464 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 465 466 static size_t dtrace_strlen(const char *, size_t); 467 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 468 static void dtrace_enabling_provide(dtrace_provider_t *); 469 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 470 static void dtrace_enabling_matchall(void); 471 static void dtrace_enabling_reap(void); 472 static dtrace_state_t *dtrace_anon_grab(void); 473 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 474 dtrace_state_t *, uint64_t, uint64_t); 475 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 476 static void dtrace_buffer_drop(dtrace_buffer_t *); 477 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 478 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 479 dtrace_state_t *, dtrace_mstate_t *); 480 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 481 dtrace_optval_t); 482 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 483 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 484 static int dtrace_priv_proc(dtrace_state_t *, dtrace_mstate_t *); 485 static void dtrace_getf_barrier(void); 486 487 /* 488 * DTrace Probe Context Functions 489 * 490 * These functions are called from probe context. Because probe context is 491 * any context in which C may be called, arbitrarily locks may be held, 492 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 493 * As a result, functions called from probe context may only call other DTrace 494 * support functions -- they may not interact at all with the system at large. 495 * (Note that the ASSERT macro is made probe-context safe by redefining it in 496 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 497 * loads are to be performed from probe context, they _must_ be in terms of 498 * the safe dtrace_load*() variants. 499 * 500 * Some functions in this block are not actually called from probe context; 501 * for these functions, there will be a comment above the function reading 502 * "Note: not called from probe context." 503 */ 504 void 505 dtrace_panic(const char *format, ...) 506 { 507 va_list alist; 508 509 va_start(alist, format); 510 dtrace_vpanic(format, alist); 511 va_end(alist); 512 } 513 514 int 515 dtrace_assfail(const char *a, const char *f, int l) 516 { 517 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 518 519 /* 520 * We just need something here that even the most clever compiler 521 * cannot optimize away. 522 */ 523 return (a[(uintptr_t)f]); 524 } 525 526 /* 527 * Atomically increment a specified error counter from probe context. 528 */ 529 static void 530 dtrace_error(uint32_t *counter) 531 { 532 /* 533 * Most counters stored to in probe context are per-CPU counters. 534 * However, there are some error conditions that are sufficiently 535 * arcane that they don't merit per-CPU storage. If these counters 536 * are incremented concurrently on different CPUs, scalability will be 537 * adversely affected -- but we don't expect them to be white-hot in a 538 * correctly constructed enabling... 539 */ 540 uint32_t oval, nval; 541 542 do { 543 oval = *counter; 544 545 if ((nval = oval + 1) == 0) { 546 /* 547 * If the counter would wrap, set it to 1 -- assuring 548 * that the counter is never zero when we have seen 549 * errors. (The counter must be 32-bits because we 550 * aren't guaranteed a 64-bit compare&swap operation.) 551 * To save this code both the infamy of being fingered 552 * by a priggish news story and the indignity of being 553 * the target of a neo-puritan witch trial, we're 554 * carefully avoiding any colorful description of the 555 * likelihood of this condition -- but suffice it to 556 * say that it is only slightly more likely than the 557 * overflow of predicate cache IDs, as discussed in 558 * dtrace_predicate_create(). 559 */ 560 nval = 1; 561 } 562 } while (dtrace_cas32(counter, oval, nval) != oval); 563 } 564 565 /* 566 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 567 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 568 */ 569 DTRACE_LOADFUNC(8) 570 DTRACE_LOADFUNC(16) 571 DTRACE_LOADFUNC(32) 572 DTRACE_LOADFUNC(64) 573 574 static int 575 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 576 { 577 if (dest < mstate->dtms_scratch_base) 578 return (0); 579 580 if (dest + size < dest) 581 return (0); 582 583 if (dest + size > mstate->dtms_scratch_ptr) 584 return (0); 585 586 return (1); 587 } 588 589 static int 590 dtrace_canstore_statvar(uint64_t addr, size_t sz, 591 dtrace_statvar_t **svars, int nsvars) 592 { 593 int i; 594 595 for (i = 0; i < nsvars; i++) { 596 dtrace_statvar_t *svar = svars[i]; 597 598 if (svar == NULL || svar->dtsv_size == 0) 599 continue; 600 601 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 602 return (1); 603 } 604 605 return (0); 606 } 607 608 /* 609 * Check to see if the address is within a memory region to which a store may 610 * be issued. This includes the DTrace scratch areas, and any DTrace variable 611 * region. The caller of dtrace_canstore() is responsible for performing any 612 * alignment checks that are needed before stores are actually executed. 613 */ 614 static int 615 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 616 dtrace_vstate_t *vstate) 617 { 618 /* 619 * First, check to see if the address is in scratch space... 620 */ 621 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 622 mstate->dtms_scratch_size)) 623 return (1); 624 625 /* 626 * Now check to see if it's a dynamic variable. This check will pick 627 * up both thread-local variables and any global dynamically-allocated 628 * variables. 629 */ 630 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 631 vstate->dtvs_dynvars.dtds_size)) { 632 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 633 uintptr_t base = (uintptr_t)dstate->dtds_base + 634 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 635 uintptr_t chunkoffs; 636 637 /* 638 * Before we assume that we can store here, we need to make 639 * sure that it isn't in our metadata -- storing to our 640 * dynamic variable metadata would corrupt our state. For 641 * the range to not include any dynamic variable metadata, 642 * it must: 643 * 644 * (1) Start above the hash table that is at the base of 645 * the dynamic variable space 646 * 647 * (2) Have a starting chunk offset that is beyond the 648 * dtrace_dynvar_t that is at the base of every chunk 649 * 650 * (3) Not span a chunk boundary 651 * 652 */ 653 if (addr < base) 654 return (0); 655 656 chunkoffs = (addr - base) % dstate->dtds_chunksize; 657 658 if (chunkoffs < sizeof (dtrace_dynvar_t)) 659 return (0); 660 661 if (chunkoffs + sz > dstate->dtds_chunksize) 662 return (0); 663 664 return (1); 665 } 666 667 /* 668 * Finally, check the static local and global variables. These checks 669 * take the longest, so we perform them last. 670 */ 671 if (dtrace_canstore_statvar(addr, sz, 672 vstate->dtvs_locals, vstate->dtvs_nlocals)) 673 return (1); 674 675 if (dtrace_canstore_statvar(addr, sz, 676 vstate->dtvs_globals, vstate->dtvs_nglobals)) 677 return (1); 678 679 return (0); 680 } 681 682 683 /* 684 * Convenience routine to check to see if the address is within a memory 685 * region in which a load may be issued given the user's privilege level; 686 * if not, it sets the appropriate error flags and loads 'addr' into the 687 * illegal value slot. 688 * 689 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 690 * appropriate memory access protection. 691 */ 692 static int 693 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 694 dtrace_vstate_t *vstate) 695 { 696 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 697 file_t *fp; 698 699 /* 700 * If we hold the privilege to read from kernel memory, then 701 * everything is readable. 702 */ 703 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 704 return (1); 705 706 /* 707 * You can obviously read that which you can store. 708 */ 709 if (dtrace_canstore(addr, sz, mstate, vstate)) 710 return (1); 711 712 /* 713 * We're allowed to read from our own string table. 714 */ 715 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 716 mstate->dtms_difo->dtdo_strlen)) 717 return (1); 718 719 if (vstate->dtvs_state != NULL && 720 dtrace_priv_proc(vstate->dtvs_state, mstate)) { 721 proc_t *p; 722 723 /* 724 * When we have privileges to the current process, there are 725 * several context-related kernel structures that are safe to 726 * read, even absent the privilege to read from kernel memory. 727 * These reads are safe because these structures contain only 728 * state that (1) we're permitted to read, (2) is harmless or 729 * (3) contains pointers to additional kernel state that we're 730 * not permitted to read (and as such, do not present an 731 * opportunity for privilege escalation). Finally (and 732 * critically), because of the nature of their relation with 733 * the current thread context, the memory associated with these 734 * structures cannot change over the duration of probe context, 735 * and it is therefore impossible for this memory to be 736 * deallocated and reallocated as something else while it's 737 * being operated upon. 738 */ 739 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) 740 return (1); 741 742 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 743 sz, curthread->t_procp, sizeof (proc_t))) { 744 return (1); 745 } 746 747 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 748 curthread->t_cred, sizeof (cred_t))) { 749 return (1); 750 } 751 752 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 753 &(p->p_pidp->pid_id), sizeof (pid_t))) { 754 return (1); 755 } 756 757 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 758 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 759 return (1); 760 } 761 } 762 763 if ((fp = mstate->dtms_getf) != NULL) { 764 uintptr_t psz = sizeof (void *); 765 vnode_t *vp; 766 vnodeops_t *op; 767 768 /* 769 * When getf() returns a file_t, the enabling is implicitly 770 * granted the (transient) right to read the returned file_t 771 * as well as the v_path and v_op->vnop_name of the underlying 772 * vnode. These accesses are allowed after a successful 773 * getf() because the members that they refer to cannot change 774 * once set -- and the barrier logic in the kernel's closef() 775 * path assures that the file_t and its referenced vode_t 776 * cannot themselves be stale (that is, it impossible for 777 * either dtms_getf itself or its f_vnode member to reference 778 * freed memory). 779 */ 780 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) 781 return (1); 782 783 if ((vp = fp->f_vnode) != NULL) { 784 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) 785 return (1); 786 787 if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz, 788 vp->v_path, strlen(vp->v_path) + 1)) { 789 return (1); 790 } 791 792 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) 793 return (1); 794 795 if ((op = vp->v_op) != NULL && 796 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 797 return (1); 798 } 799 800 if (op != NULL && op->vnop_name != NULL && 801 DTRACE_INRANGE(addr, sz, op->vnop_name, 802 strlen(op->vnop_name) + 1)) { 803 return (1); 804 } 805 } 806 } 807 808 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 809 *illval = addr; 810 return (0); 811 } 812 813 /* 814 * Convenience routine to check to see if a given string is within a memory 815 * region in which a load may be issued given the user's privilege level; 816 * this exists so that we don't need to issue unnecessary dtrace_strlen() 817 * calls in the event that the user has all privileges. 818 */ 819 static int 820 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 821 dtrace_vstate_t *vstate) 822 { 823 size_t strsz; 824 825 /* 826 * If we hold the privilege to read from kernel memory, then 827 * everything is readable. 828 */ 829 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 830 return (1); 831 832 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 833 if (dtrace_canload(addr, strsz, mstate, vstate)) 834 return (1); 835 836 return (0); 837 } 838 839 /* 840 * Convenience routine to check to see if a given variable is within a memory 841 * region in which a load may be issued given the user's privilege level. 842 */ 843 static int 844 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 845 dtrace_vstate_t *vstate) 846 { 847 size_t sz, strsize; 848 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 849 850 /* 851 * If we hold the privilege to read from kernel memory, then 852 * everything is readable. 853 */ 854 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 855 return (1); 856 857 if (type->dtdt_kind == DIF_TYPE_STRING) { 858 dtrace_state_t *state = vstate->dtvs_state; 859 860 if (state != NULL) { 861 strsize = state->dts_options[DTRACEOPT_STRSIZE]; 862 } else { 863 /* 864 * In helper context, we have a NULL state; fall back 865 * to using the system-wide default for the string size 866 * in this case. 867 */ 868 strsize = dtrace_strsize_default; 869 } 870 871 sz = dtrace_strlen(src, strsize) + 1; 872 } else { 873 sz = type->dtdt_size; 874 } 875 876 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 877 } 878 879 /* 880 * Compare two strings using safe loads. 881 */ 882 static int 883 dtrace_strncmp(char *s1, char *s2, size_t limit) 884 { 885 uint8_t c1, c2; 886 volatile uint16_t *flags; 887 888 if (s1 == s2 || limit == 0) 889 return (0); 890 891 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 892 893 do { 894 if (s1 == NULL) { 895 c1 = '\0'; 896 } else { 897 c1 = dtrace_load8((uintptr_t)s1++); 898 } 899 900 if (s2 == NULL) { 901 c2 = '\0'; 902 } else { 903 c2 = dtrace_load8((uintptr_t)s2++); 904 } 905 906 if (c1 != c2) 907 return (c1 - c2); 908 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 909 910 return (0); 911 } 912 913 /* 914 * Compute strlen(s) for a string using safe memory accesses. The additional 915 * len parameter is used to specify a maximum length to ensure completion. 916 */ 917 static size_t 918 dtrace_strlen(const char *s, size_t lim) 919 { 920 uint_t len; 921 922 for (len = 0; len != lim; len++) { 923 if (dtrace_load8((uintptr_t)s++) == '\0') 924 break; 925 } 926 927 return (len); 928 } 929 930 /* 931 * Check if an address falls within a toxic region. 932 */ 933 static int 934 dtrace_istoxic(uintptr_t kaddr, size_t size) 935 { 936 uintptr_t taddr, tsize; 937 int i; 938 939 for (i = 0; i < dtrace_toxranges; i++) { 940 taddr = dtrace_toxrange[i].dtt_base; 941 tsize = dtrace_toxrange[i].dtt_limit - taddr; 942 943 if (kaddr - taddr < tsize) { 944 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 945 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr; 946 return (1); 947 } 948 949 if (taddr - kaddr < size) { 950 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 951 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr; 952 return (1); 953 } 954 } 955 956 return (0); 957 } 958 959 /* 960 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 961 * memory specified by the DIF program. The dst is assumed to be safe memory 962 * that we can store to directly because it is managed by DTrace. As with 963 * standard bcopy, overlapping copies are handled properly. 964 */ 965 static void 966 dtrace_bcopy(const void *src, void *dst, size_t len) 967 { 968 if (len != 0) { 969 uint8_t *s1 = dst; 970 const uint8_t *s2 = src; 971 972 if (s1 <= s2) { 973 do { 974 *s1++ = dtrace_load8((uintptr_t)s2++); 975 } while (--len != 0); 976 } else { 977 s2 += len; 978 s1 += len; 979 980 do { 981 *--s1 = dtrace_load8((uintptr_t)--s2); 982 } while (--len != 0); 983 } 984 } 985 } 986 987 /* 988 * Copy src to dst using safe memory accesses, up to either the specified 989 * length, or the point that a nul byte is encountered. The src is assumed to 990 * be unsafe memory specified by the DIF program. The dst is assumed to be 991 * safe memory that we can store to directly because it is managed by DTrace. 992 * Unlike dtrace_bcopy(), overlapping regions are not handled. 993 */ 994 static void 995 dtrace_strcpy(const void *src, void *dst, size_t len) 996 { 997 if (len != 0) { 998 uint8_t *s1 = dst, c; 999 const uint8_t *s2 = src; 1000 1001 do { 1002 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1003 } while (--len != 0 && c != '\0'); 1004 } 1005 } 1006 1007 /* 1008 * Copy src to dst, deriving the size and type from the specified (BYREF) 1009 * variable type. The src is assumed to be unsafe memory specified by the DIF 1010 * program. The dst is assumed to be DTrace variable memory that is of the 1011 * specified type; we assume that we can store to directly. 1012 */ 1013 static void 1014 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 1015 { 1016 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1017 1018 if (type->dtdt_kind == DIF_TYPE_STRING) { 1019 dtrace_strcpy(src, dst, type->dtdt_size); 1020 } else { 1021 dtrace_bcopy(src, dst, type->dtdt_size); 1022 } 1023 } 1024 1025 /* 1026 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1027 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1028 * safe memory that we can access directly because it is managed by DTrace. 1029 */ 1030 static int 1031 dtrace_bcmp(const void *s1, const void *s2, size_t len) 1032 { 1033 volatile uint16_t *flags; 1034 1035 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 1036 1037 if (s1 == s2) 1038 return (0); 1039 1040 if (s1 == NULL || s2 == NULL) 1041 return (1); 1042 1043 if (s1 != s2 && len != 0) { 1044 const uint8_t *ps1 = s1; 1045 const uint8_t *ps2 = s2; 1046 1047 do { 1048 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1049 return (1); 1050 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1051 } 1052 return (0); 1053 } 1054 1055 /* 1056 * Zero the specified region using a simple byte-by-byte loop. Note that this 1057 * is for safe DTrace-managed memory only. 1058 */ 1059 static void 1060 dtrace_bzero(void *dst, size_t len) 1061 { 1062 uchar_t *cp; 1063 1064 for (cp = dst; len != 0; len--) 1065 *cp++ = 0; 1066 } 1067 1068 static void 1069 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1070 { 1071 uint64_t result[2]; 1072 1073 result[0] = addend1[0] + addend2[0]; 1074 result[1] = addend1[1] + addend2[1] + 1075 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1076 1077 sum[0] = result[0]; 1078 sum[1] = result[1]; 1079 } 1080 1081 /* 1082 * Shift the 128-bit value in a by b. If b is positive, shift left. 1083 * If b is negative, shift right. 1084 */ 1085 static void 1086 dtrace_shift_128(uint64_t *a, int b) 1087 { 1088 uint64_t mask; 1089 1090 if (b == 0) 1091 return; 1092 1093 if (b < 0) { 1094 b = -b; 1095 if (b >= 64) { 1096 a[0] = a[1] >> (b - 64); 1097 a[1] = 0; 1098 } else { 1099 a[0] >>= b; 1100 mask = 1LL << (64 - b); 1101 mask -= 1; 1102 a[0] |= ((a[1] & mask) << (64 - b)); 1103 a[1] >>= b; 1104 } 1105 } else { 1106 if (b >= 64) { 1107 a[1] = a[0] << (b - 64); 1108 a[0] = 0; 1109 } else { 1110 a[1] <<= b; 1111 mask = a[0] >> (64 - b); 1112 a[1] |= mask; 1113 a[0] <<= b; 1114 } 1115 } 1116 } 1117 1118 /* 1119 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1120 * use native multiplication on those, and then re-combine into the 1121 * resulting 128-bit value. 1122 * 1123 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1124 * hi1 * hi2 << 64 + 1125 * hi1 * lo2 << 32 + 1126 * hi2 * lo1 << 32 + 1127 * lo1 * lo2 1128 */ 1129 static void 1130 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1131 { 1132 uint64_t hi1, hi2, lo1, lo2; 1133 uint64_t tmp[2]; 1134 1135 hi1 = factor1 >> 32; 1136 hi2 = factor2 >> 32; 1137 1138 lo1 = factor1 & DT_MASK_LO; 1139 lo2 = factor2 & DT_MASK_LO; 1140 1141 product[0] = lo1 * lo2; 1142 product[1] = hi1 * hi2; 1143 1144 tmp[0] = hi1 * lo2; 1145 tmp[1] = 0; 1146 dtrace_shift_128(tmp, 32); 1147 dtrace_add_128(product, tmp, product); 1148 1149 tmp[0] = hi2 * lo1; 1150 tmp[1] = 0; 1151 dtrace_shift_128(tmp, 32); 1152 dtrace_add_128(product, tmp, product); 1153 } 1154 1155 /* 1156 * This privilege check should be used by actions and subroutines to 1157 * verify that the user credentials of the process that enabled the 1158 * invoking ECB match the target credentials 1159 */ 1160 static int 1161 dtrace_priv_proc_common_user(dtrace_state_t *state) 1162 { 1163 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1164 1165 /* 1166 * We should always have a non-NULL state cred here, since if cred 1167 * is null (anonymous tracing), we fast-path bypass this routine. 1168 */ 1169 ASSERT(s_cr != NULL); 1170 1171 if ((cr = CRED()) != NULL && 1172 s_cr->cr_uid == cr->cr_uid && 1173 s_cr->cr_uid == cr->cr_ruid && 1174 s_cr->cr_uid == cr->cr_suid && 1175 s_cr->cr_gid == cr->cr_gid && 1176 s_cr->cr_gid == cr->cr_rgid && 1177 s_cr->cr_gid == cr->cr_sgid) 1178 return (1); 1179 1180 return (0); 1181 } 1182 1183 /* 1184 * This privilege check should be used by actions and subroutines to 1185 * verify that the zone of the process that enabled the invoking ECB 1186 * matches the target credentials 1187 */ 1188 static int 1189 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1190 { 1191 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1192 1193 /* 1194 * We should always have a non-NULL state cred here, since if cred 1195 * is null (anonymous tracing), we fast-path bypass this routine. 1196 */ 1197 ASSERT(s_cr != NULL); 1198 1199 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1200 return (1); 1201 1202 return (0); 1203 } 1204 1205 /* 1206 * This privilege check should be used by actions and subroutines to 1207 * verify that the process has not setuid or changed credentials. 1208 */ 1209 static int 1210 dtrace_priv_proc_common_nocd() 1211 { 1212 proc_t *proc; 1213 1214 if ((proc = ttoproc(curthread)) != NULL && 1215 !(proc->p_flag & SNOCD)) 1216 return (1); 1217 1218 return (0); 1219 } 1220 1221 static int 1222 dtrace_priv_proc_destructive(dtrace_state_t *state, dtrace_mstate_t *mstate) 1223 { 1224 int action = state->dts_cred.dcr_action; 1225 1226 if (!(mstate->dtms_access & DTRACE_ACCESS_PROC)) 1227 goto bad; 1228 1229 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1230 dtrace_priv_proc_common_zone(state) == 0) 1231 goto bad; 1232 1233 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1234 dtrace_priv_proc_common_user(state) == 0) 1235 goto bad; 1236 1237 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1238 dtrace_priv_proc_common_nocd() == 0) 1239 goto bad; 1240 1241 return (1); 1242 1243 bad: 1244 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1245 1246 return (0); 1247 } 1248 1249 static int 1250 dtrace_priv_proc_control(dtrace_state_t *state, dtrace_mstate_t *mstate) 1251 { 1252 if (mstate->dtms_access & DTRACE_ACCESS_PROC) { 1253 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1254 return (1); 1255 1256 if (dtrace_priv_proc_common_zone(state) && 1257 dtrace_priv_proc_common_user(state) && 1258 dtrace_priv_proc_common_nocd()) 1259 return (1); 1260 } 1261 1262 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1263 1264 return (0); 1265 } 1266 1267 static int 1268 dtrace_priv_proc(dtrace_state_t *state, dtrace_mstate_t *mstate) 1269 { 1270 if ((mstate->dtms_access & DTRACE_ACCESS_PROC) && 1271 (state->dts_cred.dcr_action & DTRACE_CRA_PROC)) 1272 return (1); 1273 1274 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1275 1276 return (0); 1277 } 1278 1279 static int 1280 dtrace_priv_kernel(dtrace_state_t *state) 1281 { 1282 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1283 return (1); 1284 1285 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1286 1287 return (0); 1288 } 1289 1290 static int 1291 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1292 { 1293 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1294 return (1); 1295 1296 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1297 1298 return (0); 1299 } 1300 1301 /* 1302 * Determine if the dte_cond of the specified ECB allows for processing of 1303 * the current probe to continue. Note that this routine may allow continued 1304 * processing, but with access(es) stripped from the mstate's dtms_access 1305 * field. 1306 */ 1307 static int 1308 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1309 dtrace_ecb_t *ecb) 1310 { 1311 dtrace_probe_t *probe = ecb->dte_probe; 1312 dtrace_provider_t *prov = probe->dtpr_provider; 1313 dtrace_pops_t *pops = &prov->dtpv_pops; 1314 int mode = DTRACE_MODE_NOPRIV_DROP; 1315 1316 ASSERT(ecb->dte_cond); 1317 1318 if (pops->dtps_mode != NULL) { 1319 mode = pops->dtps_mode(prov->dtpv_arg, 1320 probe->dtpr_id, probe->dtpr_arg); 1321 1322 ASSERT(mode & (DTRACE_MODE_USER | DTRACE_MODE_KERNEL)); 1323 ASSERT(mode & (DTRACE_MODE_NOPRIV_RESTRICT | 1324 DTRACE_MODE_NOPRIV_DROP)); 1325 } 1326 1327 /* 1328 * If the dte_cond bits indicate that this consumer is only allowed to 1329 * see user-mode firings of this probe, check that the probe was fired 1330 * while in a user context. If that's not the case, use the policy 1331 * specified by the provider to determine if we drop the probe or 1332 * merely restrict operation. 1333 */ 1334 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1335 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1336 1337 if (!(mode & DTRACE_MODE_USER)) { 1338 if (mode & DTRACE_MODE_NOPRIV_DROP) 1339 return (0); 1340 1341 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1342 } 1343 } 1344 1345 /* 1346 * This is more subtle than it looks. We have to be absolutely certain 1347 * that CRED() isn't going to change out from under us so it's only 1348 * legit to examine that structure if we're in constrained situations. 1349 * Currently, the only times we'll this check is if a non-super-user 1350 * has enabled the profile or syscall providers -- providers that 1351 * allow visibility of all processes. For the profile case, the check 1352 * above will ensure that we're examining a user context. 1353 */ 1354 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1355 cred_t *cr; 1356 cred_t *s_cr = state->dts_cred.dcr_cred; 1357 proc_t *proc; 1358 1359 ASSERT(s_cr != NULL); 1360 1361 if ((cr = CRED()) == NULL || 1362 s_cr->cr_uid != cr->cr_uid || 1363 s_cr->cr_uid != cr->cr_ruid || 1364 s_cr->cr_uid != cr->cr_suid || 1365 s_cr->cr_gid != cr->cr_gid || 1366 s_cr->cr_gid != cr->cr_rgid || 1367 s_cr->cr_gid != cr->cr_sgid || 1368 (proc = ttoproc(curthread)) == NULL || 1369 (proc->p_flag & SNOCD)) { 1370 if (mode & DTRACE_MODE_NOPRIV_DROP) 1371 return (0); 1372 1373 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1374 } 1375 } 1376 1377 /* 1378 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1379 * in our zone, check to see if our mode policy is to restrict rather 1380 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1381 * and DTRACE_ACCESS_ARGS 1382 */ 1383 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1384 cred_t *cr; 1385 cred_t *s_cr = state->dts_cred.dcr_cred; 1386 1387 ASSERT(s_cr != NULL); 1388 1389 if ((cr = CRED()) == NULL || 1390 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1391 if (mode & DTRACE_MODE_NOPRIV_DROP) 1392 return (0); 1393 1394 mstate->dtms_access &= 1395 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1396 } 1397 } 1398 1399 /* 1400 * By merits of being in this code path at all, we have limited 1401 * privileges. If the provider has indicated that limited privileges 1402 * are to denote restricted operation, strip off the ability to access 1403 * arguments. 1404 */ 1405 if (mode & DTRACE_MODE_LIMITEDPRIV_RESTRICT) 1406 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1407 1408 return (1); 1409 } 1410 1411 /* 1412 * Note: not called from probe context. This function is called 1413 * asynchronously (and at a regular interval) from outside of probe context to 1414 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1415 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1416 */ 1417 void 1418 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1419 { 1420 dtrace_dynvar_t *dirty; 1421 dtrace_dstate_percpu_t *dcpu; 1422 dtrace_dynvar_t **rinsep; 1423 int i, j, work = 0; 1424 1425 for (i = 0; i < NCPU; i++) { 1426 dcpu = &dstate->dtds_percpu[i]; 1427 rinsep = &dcpu->dtdsc_rinsing; 1428 1429 /* 1430 * If the dirty list is NULL, there is no dirty work to do. 1431 */ 1432 if (dcpu->dtdsc_dirty == NULL) 1433 continue; 1434 1435 if (dcpu->dtdsc_rinsing != NULL) { 1436 /* 1437 * If the rinsing list is non-NULL, then it is because 1438 * this CPU was selected to accept another CPU's 1439 * dirty list -- and since that time, dirty buffers 1440 * have accumulated. This is a highly unlikely 1441 * condition, but we choose to ignore the dirty 1442 * buffers -- they'll be picked up a future cleanse. 1443 */ 1444 continue; 1445 } 1446 1447 if (dcpu->dtdsc_clean != NULL) { 1448 /* 1449 * If the clean list is non-NULL, then we're in a 1450 * situation where a CPU has done deallocations (we 1451 * have a non-NULL dirty list) but no allocations (we 1452 * also have a non-NULL clean list). We can't simply 1453 * move the dirty list into the clean list on this 1454 * CPU, yet we also don't want to allow this condition 1455 * to persist, lest a short clean list prevent a 1456 * massive dirty list from being cleaned (which in 1457 * turn could lead to otherwise avoidable dynamic 1458 * drops). To deal with this, we look for some CPU 1459 * with a NULL clean list, NULL dirty list, and NULL 1460 * rinsing list -- and then we borrow this CPU to 1461 * rinse our dirty list. 1462 */ 1463 for (j = 0; j < NCPU; j++) { 1464 dtrace_dstate_percpu_t *rinser; 1465 1466 rinser = &dstate->dtds_percpu[j]; 1467 1468 if (rinser->dtdsc_rinsing != NULL) 1469 continue; 1470 1471 if (rinser->dtdsc_dirty != NULL) 1472 continue; 1473 1474 if (rinser->dtdsc_clean != NULL) 1475 continue; 1476 1477 rinsep = &rinser->dtdsc_rinsing; 1478 break; 1479 } 1480 1481 if (j == NCPU) { 1482 /* 1483 * We were unable to find another CPU that 1484 * could accept this dirty list -- we are 1485 * therefore unable to clean it now. 1486 */ 1487 dtrace_dynvar_failclean++; 1488 continue; 1489 } 1490 } 1491 1492 work = 1; 1493 1494 /* 1495 * Atomically move the dirty list aside. 1496 */ 1497 do { 1498 dirty = dcpu->dtdsc_dirty; 1499 1500 /* 1501 * Before we zap the dirty list, set the rinsing list. 1502 * (This allows for a potential assertion in 1503 * dtrace_dynvar(): if a free dynamic variable appears 1504 * on a hash chain, either the dirty list or the 1505 * rinsing list for some CPU must be non-NULL.) 1506 */ 1507 *rinsep = dirty; 1508 dtrace_membar_producer(); 1509 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1510 dirty, NULL) != dirty); 1511 } 1512 1513 if (!work) { 1514 /* 1515 * We have no work to do; we can simply return. 1516 */ 1517 return; 1518 } 1519 1520 dtrace_sync(); 1521 1522 for (i = 0; i < NCPU; i++) { 1523 dcpu = &dstate->dtds_percpu[i]; 1524 1525 if (dcpu->dtdsc_rinsing == NULL) 1526 continue; 1527 1528 /* 1529 * We are now guaranteed that no hash chain contains a pointer 1530 * into this dirty list; we can make it clean. 1531 */ 1532 ASSERT(dcpu->dtdsc_clean == NULL); 1533 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1534 dcpu->dtdsc_rinsing = NULL; 1535 } 1536 1537 /* 1538 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1539 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1540 * This prevents a race whereby a CPU incorrectly decides that 1541 * the state should be something other than DTRACE_DSTATE_CLEAN 1542 * after dtrace_dynvar_clean() has completed. 1543 */ 1544 dtrace_sync(); 1545 1546 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1547 } 1548 1549 /* 1550 * Depending on the value of the op parameter, this function looks-up, 1551 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1552 * allocation is requested, this function will return a pointer to a 1553 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1554 * variable can be allocated. If NULL is returned, the appropriate counter 1555 * will be incremented. 1556 */ 1557 dtrace_dynvar_t * 1558 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1559 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1560 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1561 { 1562 uint64_t hashval = DTRACE_DYNHASH_VALID; 1563 dtrace_dynhash_t *hash = dstate->dtds_hash; 1564 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1565 processorid_t me = CPU->cpu_id, cpu = me; 1566 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1567 size_t bucket, ksize; 1568 size_t chunksize = dstate->dtds_chunksize; 1569 uintptr_t kdata, lock, nstate; 1570 uint_t i; 1571 1572 ASSERT(nkeys != 0); 1573 1574 /* 1575 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1576 * algorithm. For the by-value portions, we perform the algorithm in 1577 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1578 * bit, and seems to have only a minute effect on distribution. For 1579 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1580 * over each referenced byte. It's painful to do this, but it's much 1581 * better than pathological hash distribution. The efficacy of the 1582 * hashing algorithm (and a comparison with other algorithms) may be 1583 * found by running the ::dtrace_dynstat MDB dcmd. 1584 */ 1585 for (i = 0; i < nkeys; i++) { 1586 if (key[i].dttk_size == 0) { 1587 uint64_t val = key[i].dttk_value; 1588 1589 hashval += (val >> 48) & 0xffff; 1590 hashval += (hashval << 10); 1591 hashval ^= (hashval >> 6); 1592 1593 hashval += (val >> 32) & 0xffff; 1594 hashval += (hashval << 10); 1595 hashval ^= (hashval >> 6); 1596 1597 hashval += (val >> 16) & 0xffff; 1598 hashval += (hashval << 10); 1599 hashval ^= (hashval >> 6); 1600 1601 hashval += val & 0xffff; 1602 hashval += (hashval << 10); 1603 hashval ^= (hashval >> 6); 1604 } else { 1605 /* 1606 * This is incredibly painful, but it beats the hell 1607 * out of the alternative. 1608 */ 1609 uint64_t j, size = key[i].dttk_size; 1610 uintptr_t base = (uintptr_t)key[i].dttk_value; 1611 1612 if (!dtrace_canload(base, size, mstate, vstate)) 1613 break; 1614 1615 for (j = 0; j < size; j++) { 1616 hashval += dtrace_load8(base + j); 1617 hashval += (hashval << 10); 1618 hashval ^= (hashval >> 6); 1619 } 1620 } 1621 } 1622 1623 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1624 return (NULL); 1625 1626 hashval += (hashval << 3); 1627 hashval ^= (hashval >> 11); 1628 hashval += (hashval << 15); 1629 1630 /* 1631 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1632 * comes out to be one of our two sentinel hash values. If this 1633 * actually happens, we set the hashval to be a value known to be a 1634 * non-sentinel value. 1635 */ 1636 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1637 hashval = DTRACE_DYNHASH_VALID; 1638 1639 /* 1640 * Yes, it's painful to do a divide here. If the cycle count becomes 1641 * important here, tricks can be pulled to reduce it. (However, it's 1642 * critical that hash collisions be kept to an absolute minimum; 1643 * they're much more painful than a divide.) It's better to have a 1644 * solution that generates few collisions and still keeps things 1645 * relatively simple. 1646 */ 1647 bucket = hashval % dstate->dtds_hashsize; 1648 1649 if (op == DTRACE_DYNVAR_DEALLOC) { 1650 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1651 1652 for (;;) { 1653 while ((lock = *lockp) & 1) 1654 continue; 1655 1656 if (dtrace_casptr((void *)lockp, 1657 (void *)lock, (void *)(lock + 1)) == (void *)lock) 1658 break; 1659 } 1660 1661 dtrace_membar_producer(); 1662 } 1663 1664 top: 1665 prev = NULL; 1666 lock = hash[bucket].dtdh_lock; 1667 1668 dtrace_membar_consumer(); 1669 1670 start = hash[bucket].dtdh_chain; 1671 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1672 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1673 op != DTRACE_DYNVAR_DEALLOC)); 1674 1675 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1676 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1677 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1678 1679 if (dvar->dtdv_hashval != hashval) { 1680 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1681 /* 1682 * We've reached the sink, and therefore the 1683 * end of the hash chain; we can kick out of 1684 * the loop knowing that we have seen a valid 1685 * snapshot of state. 1686 */ 1687 ASSERT(dvar->dtdv_next == NULL); 1688 ASSERT(dvar == &dtrace_dynhash_sink); 1689 break; 1690 } 1691 1692 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1693 /* 1694 * We've gone off the rails: somewhere along 1695 * the line, one of the members of this hash 1696 * chain was deleted. Note that we could also 1697 * detect this by simply letting this loop run 1698 * to completion, as we would eventually hit 1699 * the end of the dirty list. However, we 1700 * want to avoid running the length of the 1701 * dirty list unnecessarily (it might be quite 1702 * long), so we catch this as early as 1703 * possible by detecting the hash marker. In 1704 * this case, we simply set dvar to NULL and 1705 * break; the conditional after the loop will 1706 * send us back to top. 1707 */ 1708 dvar = NULL; 1709 break; 1710 } 1711 1712 goto next; 1713 } 1714 1715 if (dtuple->dtt_nkeys != nkeys) 1716 goto next; 1717 1718 for (i = 0; i < nkeys; i++, dkey++) { 1719 if (dkey->dttk_size != key[i].dttk_size) 1720 goto next; /* size or type mismatch */ 1721 1722 if (dkey->dttk_size != 0) { 1723 if (dtrace_bcmp( 1724 (void *)(uintptr_t)key[i].dttk_value, 1725 (void *)(uintptr_t)dkey->dttk_value, 1726 dkey->dttk_size)) 1727 goto next; 1728 } else { 1729 if (dkey->dttk_value != key[i].dttk_value) 1730 goto next; 1731 } 1732 } 1733 1734 if (op != DTRACE_DYNVAR_DEALLOC) 1735 return (dvar); 1736 1737 ASSERT(dvar->dtdv_next == NULL || 1738 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1739 1740 if (prev != NULL) { 1741 ASSERT(hash[bucket].dtdh_chain != dvar); 1742 ASSERT(start != dvar); 1743 ASSERT(prev->dtdv_next == dvar); 1744 prev->dtdv_next = dvar->dtdv_next; 1745 } else { 1746 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1747 start, dvar->dtdv_next) != start) { 1748 /* 1749 * We have failed to atomically swing the 1750 * hash table head pointer, presumably because 1751 * of a conflicting allocation on another CPU. 1752 * We need to reread the hash chain and try 1753 * again. 1754 */ 1755 goto top; 1756 } 1757 } 1758 1759 dtrace_membar_producer(); 1760 1761 /* 1762 * Now set the hash value to indicate that it's free. 1763 */ 1764 ASSERT(hash[bucket].dtdh_chain != dvar); 1765 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1766 1767 dtrace_membar_producer(); 1768 1769 /* 1770 * Set the next pointer to point at the dirty list, and 1771 * atomically swing the dirty pointer to the newly freed dvar. 1772 */ 1773 do { 1774 next = dcpu->dtdsc_dirty; 1775 dvar->dtdv_next = next; 1776 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1777 1778 /* 1779 * Finally, unlock this hash bucket. 1780 */ 1781 ASSERT(hash[bucket].dtdh_lock == lock); 1782 ASSERT(lock & 1); 1783 hash[bucket].dtdh_lock++; 1784 1785 return (NULL); 1786 next: 1787 prev = dvar; 1788 continue; 1789 } 1790 1791 if (dvar == NULL) { 1792 /* 1793 * If dvar is NULL, it is because we went off the rails: 1794 * one of the elements that we traversed in the hash chain 1795 * was deleted while we were traversing it. In this case, 1796 * we assert that we aren't doing a dealloc (deallocs lock 1797 * the hash bucket to prevent themselves from racing with 1798 * one another), and retry the hash chain traversal. 1799 */ 1800 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1801 goto top; 1802 } 1803 1804 if (op != DTRACE_DYNVAR_ALLOC) { 1805 /* 1806 * If we are not to allocate a new variable, we want to 1807 * return NULL now. Before we return, check that the value 1808 * of the lock word hasn't changed. If it has, we may have 1809 * seen an inconsistent snapshot. 1810 */ 1811 if (op == DTRACE_DYNVAR_NOALLOC) { 1812 if (hash[bucket].dtdh_lock != lock) 1813 goto top; 1814 } else { 1815 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1816 ASSERT(hash[bucket].dtdh_lock == lock); 1817 ASSERT(lock & 1); 1818 hash[bucket].dtdh_lock++; 1819 } 1820 1821 return (NULL); 1822 } 1823 1824 /* 1825 * We need to allocate a new dynamic variable. The size we need is the 1826 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1827 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1828 * the size of any referred-to data (dsize). We then round the final 1829 * size up to the chunksize for allocation. 1830 */ 1831 for (ksize = 0, i = 0; i < nkeys; i++) 1832 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1833 1834 /* 1835 * This should be pretty much impossible, but could happen if, say, 1836 * strange DIF specified the tuple. Ideally, this should be an 1837 * assertion and not an error condition -- but that requires that the 1838 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1839 * bullet-proof. (That is, it must not be able to be fooled by 1840 * malicious DIF.) Given the lack of backwards branches in DIF, 1841 * solving this would presumably not amount to solving the Halting 1842 * Problem -- but it still seems awfully hard. 1843 */ 1844 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1845 ksize + dsize > chunksize) { 1846 dcpu->dtdsc_drops++; 1847 return (NULL); 1848 } 1849 1850 nstate = DTRACE_DSTATE_EMPTY; 1851 1852 do { 1853 retry: 1854 free = dcpu->dtdsc_free; 1855 1856 if (free == NULL) { 1857 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1858 void *rval; 1859 1860 if (clean == NULL) { 1861 /* 1862 * We're out of dynamic variable space on 1863 * this CPU. Unless we have tried all CPUs, 1864 * we'll try to allocate from a different 1865 * CPU. 1866 */ 1867 switch (dstate->dtds_state) { 1868 case DTRACE_DSTATE_CLEAN: { 1869 void *sp = &dstate->dtds_state; 1870 1871 if (++cpu >= NCPU) 1872 cpu = 0; 1873 1874 if (dcpu->dtdsc_dirty != NULL && 1875 nstate == DTRACE_DSTATE_EMPTY) 1876 nstate = DTRACE_DSTATE_DIRTY; 1877 1878 if (dcpu->dtdsc_rinsing != NULL) 1879 nstate = DTRACE_DSTATE_RINSING; 1880 1881 dcpu = &dstate->dtds_percpu[cpu]; 1882 1883 if (cpu != me) 1884 goto retry; 1885 1886 (void) dtrace_cas32(sp, 1887 DTRACE_DSTATE_CLEAN, nstate); 1888 1889 /* 1890 * To increment the correct bean 1891 * counter, take another lap. 1892 */ 1893 goto retry; 1894 } 1895 1896 case DTRACE_DSTATE_DIRTY: 1897 dcpu->dtdsc_dirty_drops++; 1898 break; 1899 1900 case DTRACE_DSTATE_RINSING: 1901 dcpu->dtdsc_rinsing_drops++; 1902 break; 1903 1904 case DTRACE_DSTATE_EMPTY: 1905 dcpu->dtdsc_drops++; 1906 break; 1907 } 1908 1909 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1910 return (NULL); 1911 } 1912 1913 /* 1914 * The clean list appears to be non-empty. We want to 1915 * move the clean list to the free list; we start by 1916 * moving the clean pointer aside. 1917 */ 1918 if (dtrace_casptr(&dcpu->dtdsc_clean, 1919 clean, NULL) != clean) { 1920 /* 1921 * We are in one of two situations: 1922 * 1923 * (a) The clean list was switched to the 1924 * free list by another CPU. 1925 * 1926 * (b) The clean list was added to by the 1927 * cleansing cyclic. 1928 * 1929 * In either of these situations, we can 1930 * just reattempt the free list allocation. 1931 */ 1932 goto retry; 1933 } 1934 1935 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1936 1937 /* 1938 * Now we'll move the clean list to our free list. 1939 * It's impossible for this to fail: the only way 1940 * the free list can be updated is through this 1941 * code path, and only one CPU can own the clean list. 1942 * Thus, it would only be possible for this to fail if 1943 * this code were racing with dtrace_dynvar_clean(). 1944 * (That is, if dtrace_dynvar_clean() updated the clean 1945 * list, and we ended up racing to update the free 1946 * list.) This race is prevented by the dtrace_sync() 1947 * in dtrace_dynvar_clean() -- which flushes the 1948 * owners of the clean lists out before resetting 1949 * the clean lists. 1950 */ 1951 dcpu = &dstate->dtds_percpu[me]; 1952 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1953 ASSERT(rval == NULL); 1954 goto retry; 1955 } 1956 1957 dvar = free; 1958 new_free = dvar->dtdv_next; 1959 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1960 1961 /* 1962 * We have now allocated a new chunk. We copy the tuple keys into the 1963 * tuple array and copy any referenced key data into the data space 1964 * following the tuple array. As we do this, we relocate dttk_value 1965 * in the final tuple to point to the key data address in the chunk. 1966 */ 1967 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1968 dvar->dtdv_data = (void *)(kdata + ksize); 1969 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1970 1971 for (i = 0; i < nkeys; i++) { 1972 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1973 size_t kesize = key[i].dttk_size; 1974 1975 if (kesize != 0) { 1976 dtrace_bcopy( 1977 (const void *)(uintptr_t)key[i].dttk_value, 1978 (void *)kdata, kesize); 1979 dkey->dttk_value = kdata; 1980 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1981 } else { 1982 dkey->dttk_value = key[i].dttk_value; 1983 } 1984 1985 dkey->dttk_size = kesize; 1986 } 1987 1988 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1989 dvar->dtdv_hashval = hashval; 1990 dvar->dtdv_next = start; 1991 1992 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1993 return (dvar); 1994 1995 /* 1996 * The cas has failed. Either another CPU is adding an element to 1997 * this hash chain, or another CPU is deleting an element from this 1998 * hash chain. The simplest way to deal with both of these cases 1999 * (though not necessarily the most efficient) is to free our 2000 * allocated block and tail-call ourselves. Note that the free is 2001 * to the dirty list and _not_ to the free list. This is to prevent 2002 * races with allocators, above. 2003 */ 2004 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2005 2006 dtrace_membar_producer(); 2007 2008 do { 2009 free = dcpu->dtdsc_dirty; 2010 dvar->dtdv_next = free; 2011 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 2012 2013 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 2014 } 2015 2016 /*ARGSUSED*/ 2017 static void 2018 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 2019 { 2020 if ((int64_t)nval < (int64_t)*oval) 2021 *oval = nval; 2022 } 2023 2024 /*ARGSUSED*/ 2025 static void 2026 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2027 { 2028 if ((int64_t)nval > (int64_t)*oval) 2029 *oval = nval; 2030 } 2031 2032 static void 2033 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2034 { 2035 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2036 int64_t val = (int64_t)nval; 2037 2038 if (val < 0) { 2039 for (i = 0; i < zero; i++) { 2040 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2041 quanta[i] += incr; 2042 return; 2043 } 2044 } 2045 } else { 2046 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2047 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2048 quanta[i - 1] += incr; 2049 return; 2050 } 2051 } 2052 2053 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2054 return; 2055 } 2056 2057 ASSERT(0); 2058 } 2059 2060 static void 2061 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2062 { 2063 uint64_t arg = *lquanta++; 2064 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2065 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2066 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2067 int32_t val = (int32_t)nval, level; 2068 2069 ASSERT(step != 0); 2070 ASSERT(levels != 0); 2071 2072 if (val < base) { 2073 /* 2074 * This is an underflow. 2075 */ 2076 lquanta[0] += incr; 2077 return; 2078 } 2079 2080 level = (val - base) / step; 2081 2082 if (level < levels) { 2083 lquanta[level + 1] += incr; 2084 return; 2085 } 2086 2087 /* 2088 * This is an overflow. 2089 */ 2090 lquanta[levels + 1] += incr; 2091 } 2092 2093 static int 2094 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2095 uint16_t high, uint16_t nsteps, int64_t value) 2096 { 2097 int64_t this = 1, last, next; 2098 int base = 1, order; 2099 2100 ASSERT(factor <= nsteps); 2101 ASSERT(nsteps % factor == 0); 2102 2103 for (order = 0; order < low; order++) 2104 this *= factor; 2105 2106 /* 2107 * If our value is less than our factor taken to the power of the 2108 * low order of magnitude, it goes into the zeroth bucket. 2109 */ 2110 if (value < (last = this)) 2111 return (0); 2112 2113 for (this *= factor; order <= high; order++) { 2114 int nbuckets = this > nsteps ? nsteps : this; 2115 2116 if ((next = this * factor) < this) { 2117 /* 2118 * We should not generally get log/linear quantizations 2119 * with a high magnitude that allows 64-bits to 2120 * overflow, but we nonetheless protect against this 2121 * by explicitly checking for overflow, and clamping 2122 * our value accordingly. 2123 */ 2124 value = this - 1; 2125 } 2126 2127 if (value < this) { 2128 /* 2129 * If our value lies within this order of magnitude, 2130 * determine its position by taking the offset within 2131 * the order of magnitude, dividing by the bucket 2132 * width, and adding to our (accumulated) base. 2133 */ 2134 return (base + (value - last) / (this / nbuckets)); 2135 } 2136 2137 base += nbuckets - (nbuckets / factor); 2138 last = this; 2139 this = next; 2140 } 2141 2142 /* 2143 * Our value is greater than or equal to our factor taken to the 2144 * power of one plus the high magnitude -- return the top bucket. 2145 */ 2146 return (base); 2147 } 2148 2149 static void 2150 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2151 { 2152 uint64_t arg = *llquanta++; 2153 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2154 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2155 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2156 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2157 2158 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2159 low, high, nsteps, nval)] += incr; 2160 } 2161 2162 /*ARGSUSED*/ 2163 static void 2164 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2165 { 2166 data[0]++; 2167 data[1] += nval; 2168 } 2169 2170 /*ARGSUSED*/ 2171 static void 2172 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2173 { 2174 int64_t snval = (int64_t)nval; 2175 uint64_t tmp[2]; 2176 2177 data[0]++; 2178 data[1] += nval; 2179 2180 /* 2181 * What we want to say here is: 2182 * 2183 * data[2] += nval * nval; 2184 * 2185 * But given that nval is 64-bit, we could easily overflow, so 2186 * we do this as 128-bit arithmetic. 2187 */ 2188 if (snval < 0) 2189 snval = -snval; 2190 2191 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2192 dtrace_add_128(data + 2, tmp, data + 2); 2193 } 2194 2195 /*ARGSUSED*/ 2196 static void 2197 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2198 { 2199 *oval = *oval + 1; 2200 } 2201 2202 /*ARGSUSED*/ 2203 static void 2204 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2205 { 2206 *oval += nval; 2207 } 2208 2209 /* 2210 * Aggregate given the tuple in the principal data buffer, and the aggregating 2211 * action denoted by the specified dtrace_aggregation_t. The aggregation 2212 * buffer is specified as the buf parameter. This routine does not return 2213 * failure; if there is no space in the aggregation buffer, the data will be 2214 * dropped, and a corresponding counter incremented. 2215 */ 2216 static void 2217 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2218 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2219 { 2220 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2221 uint32_t i, ndx, size, fsize; 2222 uint32_t align = sizeof (uint64_t) - 1; 2223 dtrace_aggbuffer_t *agb; 2224 dtrace_aggkey_t *key; 2225 uint32_t hashval = 0, limit, isstr; 2226 caddr_t tomax, data, kdata; 2227 dtrace_actkind_t action; 2228 dtrace_action_t *act; 2229 uintptr_t offs; 2230 2231 if (buf == NULL) 2232 return; 2233 2234 if (!agg->dtag_hasarg) { 2235 /* 2236 * Currently, only quantize() and lquantize() take additional 2237 * arguments, and they have the same semantics: an increment 2238 * value that defaults to 1 when not present. If additional 2239 * aggregating actions take arguments, the setting of the 2240 * default argument value will presumably have to become more 2241 * sophisticated... 2242 */ 2243 arg = 1; 2244 } 2245 2246 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2247 size = rec->dtrd_offset - agg->dtag_base; 2248 fsize = size + rec->dtrd_size; 2249 2250 ASSERT(dbuf->dtb_tomax != NULL); 2251 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2252 2253 if ((tomax = buf->dtb_tomax) == NULL) { 2254 dtrace_buffer_drop(buf); 2255 return; 2256 } 2257 2258 /* 2259 * The metastructure is always at the bottom of the buffer. 2260 */ 2261 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2262 sizeof (dtrace_aggbuffer_t)); 2263 2264 if (buf->dtb_offset == 0) { 2265 /* 2266 * We just kludge up approximately 1/8th of the size to be 2267 * buckets. If this guess ends up being routinely 2268 * off-the-mark, we may need to dynamically readjust this 2269 * based on past performance. 2270 */ 2271 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2272 2273 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2274 (uintptr_t)tomax || hashsize == 0) { 2275 /* 2276 * We've been given a ludicrously small buffer; 2277 * increment our drop count and leave. 2278 */ 2279 dtrace_buffer_drop(buf); 2280 return; 2281 } 2282 2283 /* 2284 * And now, a pathetic attempt to try to get a an odd (or 2285 * perchance, a prime) hash size for better hash distribution. 2286 */ 2287 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2288 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2289 2290 agb->dtagb_hashsize = hashsize; 2291 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2292 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2293 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2294 2295 for (i = 0; i < agb->dtagb_hashsize; i++) 2296 agb->dtagb_hash[i] = NULL; 2297 } 2298 2299 ASSERT(agg->dtag_first != NULL); 2300 ASSERT(agg->dtag_first->dta_intuple); 2301 2302 /* 2303 * Calculate the hash value based on the key. Note that we _don't_ 2304 * include the aggid in the hashing (but we will store it as part of 2305 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2306 * algorithm: a simple, quick algorithm that has no known funnels, and 2307 * gets good distribution in practice. The efficacy of the hashing 2308 * algorithm (and a comparison with other algorithms) may be found by 2309 * running the ::dtrace_aggstat MDB dcmd. 2310 */ 2311 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2312 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2313 limit = i + act->dta_rec.dtrd_size; 2314 ASSERT(limit <= size); 2315 isstr = DTRACEACT_ISSTRING(act); 2316 2317 for (; i < limit; i++) { 2318 hashval += data[i]; 2319 hashval += (hashval << 10); 2320 hashval ^= (hashval >> 6); 2321 2322 if (isstr && data[i] == '\0') 2323 break; 2324 } 2325 } 2326 2327 hashval += (hashval << 3); 2328 hashval ^= (hashval >> 11); 2329 hashval += (hashval << 15); 2330 2331 /* 2332 * Yes, the divide here is expensive -- but it's generally the least 2333 * of the performance issues given the amount of data that we iterate 2334 * over to compute hash values, compare data, etc. 2335 */ 2336 ndx = hashval % agb->dtagb_hashsize; 2337 2338 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2339 ASSERT((caddr_t)key >= tomax); 2340 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2341 2342 if (hashval != key->dtak_hashval || key->dtak_size != size) 2343 continue; 2344 2345 kdata = key->dtak_data; 2346 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2347 2348 for (act = agg->dtag_first; act->dta_intuple; 2349 act = act->dta_next) { 2350 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2351 limit = i + act->dta_rec.dtrd_size; 2352 ASSERT(limit <= size); 2353 isstr = DTRACEACT_ISSTRING(act); 2354 2355 for (; i < limit; i++) { 2356 if (kdata[i] != data[i]) 2357 goto next; 2358 2359 if (isstr && data[i] == '\0') 2360 break; 2361 } 2362 } 2363 2364 if (action != key->dtak_action) { 2365 /* 2366 * We are aggregating on the same value in the same 2367 * aggregation with two different aggregating actions. 2368 * (This should have been picked up in the compiler, 2369 * so we may be dealing with errant or devious DIF.) 2370 * This is an error condition; we indicate as much, 2371 * and return. 2372 */ 2373 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2374 return; 2375 } 2376 2377 /* 2378 * This is a hit: we need to apply the aggregator to 2379 * the value at this key. 2380 */ 2381 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2382 return; 2383 next: 2384 continue; 2385 } 2386 2387 /* 2388 * We didn't find it. We need to allocate some zero-filled space, 2389 * link it into the hash table appropriately, and apply the aggregator 2390 * to the (zero-filled) value. 2391 */ 2392 offs = buf->dtb_offset; 2393 while (offs & (align - 1)) 2394 offs += sizeof (uint32_t); 2395 2396 /* 2397 * If we don't have enough room to both allocate a new key _and_ 2398 * its associated data, increment the drop count and return. 2399 */ 2400 if ((uintptr_t)tomax + offs + fsize > 2401 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2402 dtrace_buffer_drop(buf); 2403 return; 2404 } 2405 2406 /*CONSTCOND*/ 2407 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2408 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2409 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2410 2411 key->dtak_data = kdata = tomax + offs; 2412 buf->dtb_offset = offs + fsize; 2413 2414 /* 2415 * Now copy the data across. 2416 */ 2417 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2418 2419 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2420 kdata[i] = data[i]; 2421 2422 /* 2423 * Because strings are not zeroed out by default, we need to iterate 2424 * looking for actions that store strings, and we need to explicitly 2425 * pad these strings out with zeroes. 2426 */ 2427 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2428 int nul; 2429 2430 if (!DTRACEACT_ISSTRING(act)) 2431 continue; 2432 2433 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2434 limit = i + act->dta_rec.dtrd_size; 2435 ASSERT(limit <= size); 2436 2437 for (nul = 0; i < limit; i++) { 2438 if (nul) { 2439 kdata[i] = '\0'; 2440 continue; 2441 } 2442 2443 if (data[i] != '\0') 2444 continue; 2445 2446 nul = 1; 2447 } 2448 } 2449 2450 for (i = size; i < fsize; i++) 2451 kdata[i] = 0; 2452 2453 key->dtak_hashval = hashval; 2454 key->dtak_size = size; 2455 key->dtak_action = action; 2456 key->dtak_next = agb->dtagb_hash[ndx]; 2457 agb->dtagb_hash[ndx] = key; 2458 2459 /* 2460 * Finally, apply the aggregator. 2461 */ 2462 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2463 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2464 } 2465 2466 /* 2467 * Given consumer state, this routine finds a speculation in the INACTIVE 2468 * state and transitions it into the ACTIVE state. If there is no speculation 2469 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2470 * incremented -- it is up to the caller to take appropriate action. 2471 */ 2472 static int 2473 dtrace_speculation(dtrace_state_t *state) 2474 { 2475 int i = 0; 2476 dtrace_speculation_state_t current; 2477 uint32_t *stat = &state->dts_speculations_unavail, count; 2478 2479 while (i < state->dts_nspeculations) { 2480 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2481 2482 current = spec->dtsp_state; 2483 2484 if (current != DTRACESPEC_INACTIVE) { 2485 if (current == DTRACESPEC_COMMITTINGMANY || 2486 current == DTRACESPEC_COMMITTING || 2487 current == DTRACESPEC_DISCARDING) 2488 stat = &state->dts_speculations_busy; 2489 i++; 2490 continue; 2491 } 2492 2493 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2494 current, DTRACESPEC_ACTIVE) == current) 2495 return (i + 1); 2496 } 2497 2498 /* 2499 * We couldn't find a speculation. If we found as much as a single 2500 * busy speculation buffer, we'll attribute this failure as "busy" 2501 * instead of "unavail". 2502 */ 2503 do { 2504 count = *stat; 2505 } while (dtrace_cas32(stat, count, count + 1) != count); 2506 2507 return (0); 2508 } 2509 2510 /* 2511 * This routine commits an active speculation. If the specified speculation 2512 * is not in a valid state to perform a commit(), this routine will silently do 2513 * nothing. The state of the specified speculation is transitioned according 2514 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2515 */ 2516 static void 2517 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2518 dtrace_specid_t which) 2519 { 2520 dtrace_speculation_t *spec; 2521 dtrace_buffer_t *src, *dest; 2522 uintptr_t daddr, saddr, dlimit, slimit; 2523 dtrace_speculation_state_t current, new; 2524 intptr_t offs; 2525 uint64_t timestamp; 2526 2527 if (which == 0) 2528 return; 2529 2530 if (which > state->dts_nspeculations) { 2531 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2532 return; 2533 } 2534 2535 spec = &state->dts_speculations[which - 1]; 2536 src = &spec->dtsp_buffer[cpu]; 2537 dest = &state->dts_buffer[cpu]; 2538 2539 do { 2540 current = spec->dtsp_state; 2541 2542 if (current == DTRACESPEC_COMMITTINGMANY) 2543 break; 2544 2545 switch (current) { 2546 case DTRACESPEC_INACTIVE: 2547 case DTRACESPEC_DISCARDING: 2548 return; 2549 2550 case DTRACESPEC_COMMITTING: 2551 /* 2552 * This is only possible if we are (a) commit()'ing 2553 * without having done a prior speculate() on this CPU 2554 * and (b) racing with another commit() on a different 2555 * CPU. There's nothing to do -- we just assert that 2556 * our offset is 0. 2557 */ 2558 ASSERT(src->dtb_offset == 0); 2559 return; 2560 2561 case DTRACESPEC_ACTIVE: 2562 new = DTRACESPEC_COMMITTING; 2563 break; 2564 2565 case DTRACESPEC_ACTIVEONE: 2566 /* 2567 * This speculation is active on one CPU. If our 2568 * buffer offset is non-zero, we know that the one CPU 2569 * must be us. Otherwise, we are committing on a 2570 * different CPU from the speculate(), and we must 2571 * rely on being asynchronously cleaned. 2572 */ 2573 if (src->dtb_offset != 0) { 2574 new = DTRACESPEC_COMMITTING; 2575 break; 2576 } 2577 /*FALLTHROUGH*/ 2578 2579 case DTRACESPEC_ACTIVEMANY: 2580 new = DTRACESPEC_COMMITTINGMANY; 2581 break; 2582 2583 default: 2584 ASSERT(0); 2585 } 2586 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2587 current, new) != current); 2588 2589 /* 2590 * We have set the state to indicate that we are committing this 2591 * speculation. Now reserve the necessary space in the destination 2592 * buffer. 2593 */ 2594 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2595 sizeof (uint64_t), state, NULL)) < 0) { 2596 dtrace_buffer_drop(dest); 2597 goto out; 2598 } 2599 2600 /* 2601 * We have sufficient space to copy the speculative buffer into the 2602 * primary buffer. First, modify the speculative buffer, filling 2603 * in the timestamp of all entries with the current time. The data 2604 * must have the commit() time rather than the time it was traced, 2605 * so that all entries in the primary buffer are in timestamp order. 2606 */ 2607 timestamp = dtrace_gethrtime(); 2608 saddr = (uintptr_t)src->dtb_tomax; 2609 slimit = saddr + src->dtb_offset; 2610 while (saddr < slimit) { 2611 size_t size; 2612 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2613 2614 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2615 saddr += sizeof (dtrace_epid_t); 2616 continue; 2617 } 2618 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2619 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2620 2621 ASSERT3U(saddr + size, <=, slimit); 2622 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2623 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2624 2625 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2626 2627 saddr += size; 2628 } 2629 2630 /* 2631 * Copy the buffer across. (Note that this is a 2632 * highly subobtimal bcopy(); in the unlikely event that this becomes 2633 * a serious performance issue, a high-performance DTrace-specific 2634 * bcopy() should obviously be invented.) 2635 */ 2636 daddr = (uintptr_t)dest->dtb_tomax + offs; 2637 dlimit = daddr + src->dtb_offset; 2638 saddr = (uintptr_t)src->dtb_tomax; 2639 2640 /* 2641 * First, the aligned portion. 2642 */ 2643 while (dlimit - daddr >= sizeof (uint64_t)) { 2644 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2645 2646 daddr += sizeof (uint64_t); 2647 saddr += sizeof (uint64_t); 2648 } 2649 2650 /* 2651 * Now any left-over bit... 2652 */ 2653 while (dlimit - daddr) 2654 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2655 2656 /* 2657 * Finally, commit the reserved space in the destination buffer. 2658 */ 2659 dest->dtb_offset = offs + src->dtb_offset; 2660 2661 out: 2662 /* 2663 * If we're lucky enough to be the only active CPU on this speculation 2664 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2665 */ 2666 if (current == DTRACESPEC_ACTIVE || 2667 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2668 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2669 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2670 2671 ASSERT(rval == DTRACESPEC_COMMITTING); 2672 } 2673 2674 src->dtb_offset = 0; 2675 src->dtb_xamot_drops += src->dtb_drops; 2676 src->dtb_drops = 0; 2677 } 2678 2679 /* 2680 * This routine discards an active speculation. If the specified speculation 2681 * is not in a valid state to perform a discard(), this routine will silently 2682 * do nothing. The state of the specified speculation is transitioned 2683 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2684 */ 2685 static void 2686 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2687 dtrace_specid_t which) 2688 { 2689 dtrace_speculation_t *spec; 2690 dtrace_speculation_state_t current, new; 2691 dtrace_buffer_t *buf; 2692 2693 if (which == 0) 2694 return; 2695 2696 if (which > state->dts_nspeculations) { 2697 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2698 return; 2699 } 2700 2701 spec = &state->dts_speculations[which - 1]; 2702 buf = &spec->dtsp_buffer[cpu]; 2703 2704 do { 2705 current = spec->dtsp_state; 2706 2707 switch (current) { 2708 case DTRACESPEC_INACTIVE: 2709 case DTRACESPEC_COMMITTINGMANY: 2710 case DTRACESPEC_COMMITTING: 2711 case DTRACESPEC_DISCARDING: 2712 return; 2713 2714 case DTRACESPEC_ACTIVE: 2715 case DTRACESPEC_ACTIVEMANY: 2716 new = DTRACESPEC_DISCARDING; 2717 break; 2718 2719 case DTRACESPEC_ACTIVEONE: 2720 if (buf->dtb_offset != 0) { 2721 new = DTRACESPEC_INACTIVE; 2722 } else { 2723 new = DTRACESPEC_DISCARDING; 2724 } 2725 break; 2726 2727 default: 2728 ASSERT(0); 2729 } 2730 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2731 current, new) != current); 2732 2733 buf->dtb_offset = 0; 2734 buf->dtb_drops = 0; 2735 } 2736 2737 /* 2738 * Note: not called from probe context. This function is called 2739 * asynchronously from cross call context to clean any speculations that are 2740 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2741 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2742 * speculation. 2743 */ 2744 static void 2745 dtrace_speculation_clean_here(dtrace_state_t *state) 2746 { 2747 dtrace_icookie_t cookie; 2748 processorid_t cpu = CPU->cpu_id; 2749 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2750 dtrace_specid_t i; 2751 2752 cookie = dtrace_interrupt_disable(); 2753 2754 if (dest->dtb_tomax == NULL) { 2755 dtrace_interrupt_enable(cookie); 2756 return; 2757 } 2758 2759 for (i = 0; i < state->dts_nspeculations; i++) { 2760 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2761 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2762 2763 if (src->dtb_tomax == NULL) 2764 continue; 2765 2766 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2767 src->dtb_offset = 0; 2768 continue; 2769 } 2770 2771 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2772 continue; 2773 2774 if (src->dtb_offset == 0) 2775 continue; 2776 2777 dtrace_speculation_commit(state, cpu, i + 1); 2778 } 2779 2780 dtrace_interrupt_enable(cookie); 2781 } 2782 2783 /* 2784 * Note: not called from probe context. This function is called 2785 * asynchronously (and at a regular interval) to clean any speculations that 2786 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2787 * is work to be done, it cross calls all CPUs to perform that work; 2788 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2789 * INACTIVE state until they have been cleaned by all CPUs. 2790 */ 2791 static void 2792 dtrace_speculation_clean(dtrace_state_t *state) 2793 { 2794 int work = 0, rv; 2795 dtrace_specid_t i; 2796 2797 for (i = 0; i < state->dts_nspeculations; i++) { 2798 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2799 2800 ASSERT(!spec->dtsp_cleaning); 2801 2802 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2803 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2804 continue; 2805 2806 work++; 2807 spec->dtsp_cleaning = 1; 2808 } 2809 2810 if (!work) 2811 return; 2812 2813 dtrace_xcall(DTRACE_CPUALL, 2814 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2815 2816 /* 2817 * We now know that all CPUs have committed or discarded their 2818 * speculation buffers, as appropriate. We can now set the state 2819 * to inactive. 2820 */ 2821 for (i = 0; i < state->dts_nspeculations; i++) { 2822 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2823 dtrace_speculation_state_t current, new; 2824 2825 if (!spec->dtsp_cleaning) 2826 continue; 2827 2828 current = spec->dtsp_state; 2829 ASSERT(current == DTRACESPEC_DISCARDING || 2830 current == DTRACESPEC_COMMITTINGMANY); 2831 2832 new = DTRACESPEC_INACTIVE; 2833 2834 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2835 ASSERT(rv == current); 2836 spec->dtsp_cleaning = 0; 2837 } 2838 } 2839 2840 /* 2841 * Called as part of a speculate() to get the speculative buffer associated 2842 * with a given speculation. Returns NULL if the specified speculation is not 2843 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2844 * the active CPU is not the specified CPU -- the speculation will be 2845 * atomically transitioned into the ACTIVEMANY state. 2846 */ 2847 static dtrace_buffer_t * 2848 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2849 dtrace_specid_t which) 2850 { 2851 dtrace_speculation_t *spec; 2852 dtrace_speculation_state_t current, new; 2853 dtrace_buffer_t *buf; 2854 2855 if (which == 0) 2856 return (NULL); 2857 2858 if (which > state->dts_nspeculations) { 2859 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2860 return (NULL); 2861 } 2862 2863 spec = &state->dts_speculations[which - 1]; 2864 buf = &spec->dtsp_buffer[cpuid]; 2865 2866 do { 2867 current = spec->dtsp_state; 2868 2869 switch (current) { 2870 case DTRACESPEC_INACTIVE: 2871 case DTRACESPEC_COMMITTINGMANY: 2872 case DTRACESPEC_DISCARDING: 2873 return (NULL); 2874 2875 case DTRACESPEC_COMMITTING: 2876 ASSERT(buf->dtb_offset == 0); 2877 return (NULL); 2878 2879 case DTRACESPEC_ACTIVEONE: 2880 /* 2881 * This speculation is currently active on one CPU. 2882 * Check the offset in the buffer; if it's non-zero, 2883 * that CPU must be us (and we leave the state alone). 2884 * If it's zero, assume that we're starting on a new 2885 * CPU -- and change the state to indicate that the 2886 * speculation is active on more than one CPU. 2887 */ 2888 if (buf->dtb_offset != 0) 2889 return (buf); 2890 2891 new = DTRACESPEC_ACTIVEMANY; 2892 break; 2893 2894 case DTRACESPEC_ACTIVEMANY: 2895 return (buf); 2896 2897 case DTRACESPEC_ACTIVE: 2898 new = DTRACESPEC_ACTIVEONE; 2899 break; 2900 2901 default: 2902 ASSERT(0); 2903 } 2904 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2905 current, new) != current); 2906 2907 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2908 return (buf); 2909 } 2910 2911 /* 2912 * Return a string. In the event that the user lacks the privilege to access 2913 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2914 * don't fail access checking. 2915 * 2916 * dtrace_dif_variable() uses this routine as a helper for various 2917 * builtin values such as 'execname' and 'probefunc.' 2918 */ 2919 uintptr_t 2920 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 2921 dtrace_mstate_t *mstate) 2922 { 2923 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2924 uintptr_t ret; 2925 size_t strsz; 2926 2927 /* 2928 * The easy case: this probe is allowed to read all of memory, so 2929 * we can just return this as a vanilla pointer. 2930 */ 2931 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 2932 return (addr); 2933 2934 /* 2935 * This is the tougher case: we copy the string in question from 2936 * kernel memory into scratch memory and return it that way: this 2937 * ensures that we won't trip up when access checking tests the 2938 * BYREF return value. 2939 */ 2940 strsz = dtrace_strlen((char *)addr, size) + 1; 2941 2942 if (mstate->dtms_scratch_ptr + strsz > 2943 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2944 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2945 return (NULL); 2946 } 2947 2948 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2949 strsz); 2950 ret = mstate->dtms_scratch_ptr; 2951 mstate->dtms_scratch_ptr += strsz; 2952 return (ret); 2953 } 2954 2955 /* 2956 * This function implements the DIF emulator's variable lookups. The emulator 2957 * passes a reserved variable identifier and optional built-in array index. 2958 */ 2959 static uint64_t 2960 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2961 uint64_t ndx) 2962 { 2963 /* 2964 * If we're accessing one of the uncached arguments, we'll turn this 2965 * into a reference in the args array. 2966 */ 2967 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2968 ndx = v - DIF_VAR_ARG0; 2969 v = DIF_VAR_ARGS; 2970 } 2971 2972 switch (v) { 2973 case DIF_VAR_ARGS: 2974 if (!(mstate->dtms_access & DTRACE_ACCESS_ARGS)) { 2975 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= 2976 CPU_DTRACE_KPRIV; 2977 return (0); 2978 } 2979 2980 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2981 if (ndx >= sizeof (mstate->dtms_arg) / 2982 sizeof (mstate->dtms_arg[0])) { 2983 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2984 dtrace_provider_t *pv; 2985 uint64_t val; 2986 2987 pv = mstate->dtms_probe->dtpr_provider; 2988 if (pv->dtpv_pops.dtps_getargval != NULL) 2989 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2990 mstate->dtms_probe->dtpr_id, 2991 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2992 else 2993 val = dtrace_getarg(ndx, aframes); 2994 2995 /* 2996 * This is regrettably required to keep the compiler 2997 * from tail-optimizing the call to dtrace_getarg(). 2998 * The condition always evaluates to true, but the 2999 * compiler has no way of figuring that out a priori. 3000 * (None of this would be necessary if the compiler 3001 * could be relied upon to _always_ tail-optimize 3002 * the call to dtrace_getarg() -- but it can't.) 3003 */ 3004 if (mstate->dtms_probe != NULL) 3005 return (val); 3006 3007 ASSERT(0); 3008 } 3009 3010 return (mstate->dtms_arg[ndx]); 3011 3012 case DIF_VAR_UREGS: { 3013 klwp_t *lwp; 3014 3015 if (!dtrace_priv_proc(state, mstate)) 3016 return (0); 3017 3018 if ((lwp = curthread->t_lwp) == NULL) { 3019 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3020 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL; 3021 return (0); 3022 } 3023 3024 return (dtrace_getreg(lwp->lwp_regs, ndx)); 3025 } 3026 3027 case DIF_VAR_VMREGS: { 3028 uint64_t rval; 3029 3030 if (!dtrace_priv_kernel(state)) 3031 return (0); 3032 3033 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3034 3035 rval = dtrace_getvmreg(ndx, 3036 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags); 3037 3038 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3039 3040 return (rval); 3041 } 3042 3043 case DIF_VAR_CURTHREAD: 3044 if (!dtrace_priv_proc(state, mstate)) 3045 return (0); 3046 return ((uint64_t)(uintptr_t)curthread); 3047 3048 case DIF_VAR_TIMESTAMP: 3049 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 3050 mstate->dtms_timestamp = dtrace_gethrtime(); 3051 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 3052 } 3053 return (mstate->dtms_timestamp); 3054 3055 case DIF_VAR_VTIMESTAMP: 3056 ASSERT(dtrace_vtime_references != 0); 3057 return (curthread->t_dtrace_vtime); 3058 3059 case DIF_VAR_WALLTIMESTAMP: 3060 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3061 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3062 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3063 } 3064 return (mstate->dtms_walltimestamp); 3065 3066 case DIF_VAR_IPL: 3067 if (!dtrace_priv_kernel(state)) 3068 return (0); 3069 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3070 mstate->dtms_ipl = dtrace_getipl(); 3071 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3072 } 3073 return (mstate->dtms_ipl); 3074 3075 case DIF_VAR_EPID: 3076 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3077 return (mstate->dtms_epid); 3078 3079 case DIF_VAR_ID: 3080 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3081 return (mstate->dtms_probe->dtpr_id); 3082 3083 case DIF_VAR_STACKDEPTH: 3084 if (!dtrace_priv_kernel(state)) 3085 return (0); 3086 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3087 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3088 3089 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3090 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3091 } 3092 return (mstate->dtms_stackdepth); 3093 3094 case DIF_VAR_USTACKDEPTH: 3095 if (!dtrace_priv_proc(state, mstate)) 3096 return (0); 3097 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3098 /* 3099 * See comment in DIF_VAR_PID. 3100 */ 3101 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3102 CPU_ON_INTR(CPU)) { 3103 mstate->dtms_ustackdepth = 0; 3104 } else { 3105 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3106 mstate->dtms_ustackdepth = 3107 dtrace_getustackdepth(); 3108 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3109 } 3110 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3111 } 3112 return (mstate->dtms_ustackdepth); 3113 3114 case DIF_VAR_CALLER: 3115 if (!dtrace_priv_kernel(state)) 3116 return (0); 3117 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3118 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3119 3120 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3121 /* 3122 * If this is an unanchored probe, we are 3123 * required to go through the slow path: 3124 * dtrace_caller() only guarantees correct 3125 * results for anchored probes. 3126 */ 3127 pc_t caller[2]; 3128 3129 dtrace_getpcstack(caller, 2, aframes, 3130 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3131 mstate->dtms_caller = caller[1]; 3132 } else if ((mstate->dtms_caller = 3133 dtrace_caller(aframes)) == -1) { 3134 /* 3135 * We have failed to do this the quick way; 3136 * we must resort to the slower approach of 3137 * calling dtrace_getpcstack(). 3138 */ 3139 pc_t caller; 3140 3141 dtrace_getpcstack(&caller, 1, aframes, NULL); 3142 mstate->dtms_caller = caller; 3143 } 3144 3145 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3146 } 3147 return (mstate->dtms_caller); 3148 3149 case DIF_VAR_UCALLER: 3150 if (!dtrace_priv_proc(state, mstate)) 3151 return (0); 3152 3153 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3154 uint64_t ustack[3]; 3155 3156 /* 3157 * dtrace_getupcstack() fills in the first uint64_t 3158 * with the current PID. The second uint64_t will 3159 * be the program counter at user-level. The third 3160 * uint64_t will contain the caller, which is what 3161 * we're after. 3162 */ 3163 ustack[2] = NULL; 3164 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3165 dtrace_getupcstack(ustack, 3); 3166 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3167 mstate->dtms_ucaller = ustack[2]; 3168 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3169 } 3170 3171 return (mstate->dtms_ucaller); 3172 3173 case DIF_VAR_PROBEPROV: 3174 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3175 return (dtrace_dif_varstr( 3176 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3177 state, mstate)); 3178 3179 case DIF_VAR_PROBEMOD: 3180 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3181 return (dtrace_dif_varstr( 3182 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3183 state, mstate)); 3184 3185 case DIF_VAR_PROBEFUNC: 3186 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3187 return (dtrace_dif_varstr( 3188 (uintptr_t)mstate->dtms_probe->dtpr_func, 3189 state, mstate)); 3190 3191 case DIF_VAR_PROBENAME: 3192 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3193 return (dtrace_dif_varstr( 3194 (uintptr_t)mstate->dtms_probe->dtpr_name, 3195 state, mstate)); 3196 3197 case DIF_VAR_PID: 3198 if (!dtrace_priv_proc(state, mstate)) 3199 return (0); 3200 3201 /* 3202 * Note that we are assuming that an unanchored probe is 3203 * always due to a high-level interrupt. (And we're assuming 3204 * that there is only a single high level interrupt.) 3205 */ 3206 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3207 return (pid0.pid_id); 3208 3209 /* 3210 * It is always safe to dereference one's own t_procp pointer: 3211 * it always points to a valid, allocated proc structure. 3212 * Further, it is always safe to dereference the p_pidp member 3213 * of one's own proc structure. (These are truisms becuase 3214 * threads and processes don't clean up their own state -- 3215 * they leave that task to whomever reaps them.) 3216 */ 3217 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3218 3219 case DIF_VAR_PPID: 3220 if (!dtrace_priv_proc(state, mstate)) 3221 return (0); 3222 3223 /* 3224 * See comment in DIF_VAR_PID. 3225 */ 3226 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3227 return (pid0.pid_id); 3228 3229 /* 3230 * It is always safe to dereference one's own t_procp pointer: 3231 * it always points to a valid, allocated proc structure. 3232 * (This is true because threads don't clean up their own 3233 * state -- they leave that task to whomever reaps them.) 3234 */ 3235 return ((uint64_t)curthread->t_procp->p_ppid); 3236 3237 case DIF_VAR_TID: 3238 /* 3239 * See comment in DIF_VAR_PID. 3240 */ 3241 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3242 return (0); 3243 3244 return ((uint64_t)curthread->t_tid); 3245 3246 case DIF_VAR_EXECNAME: 3247 if (!dtrace_priv_proc(state, mstate)) 3248 return (0); 3249 3250 /* 3251 * See comment in DIF_VAR_PID. 3252 */ 3253 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3254 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3255 3256 /* 3257 * It is always safe to dereference one's own t_procp pointer: 3258 * it always points to a valid, allocated proc structure. 3259 * (This is true because threads don't clean up their own 3260 * state -- they leave that task to whomever reaps them.) 3261 */ 3262 return (dtrace_dif_varstr( 3263 (uintptr_t)curthread->t_procp->p_user.u_comm, 3264 state, mstate)); 3265 3266 case DIF_VAR_ZONENAME: 3267 if (!dtrace_priv_proc(state, mstate)) 3268 return (0); 3269 3270 /* 3271 * See comment in DIF_VAR_PID. 3272 */ 3273 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3274 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3275 3276 /* 3277 * It is always safe to dereference one's own t_procp pointer: 3278 * it always points to a valid, allocated proc structure. 3279 * (This is true because threads don't clean up their own 3280 * state -- they leave that task to whomever reaps them.) 3281 */ 3282 return (dtrace_dif_varstr( 3283 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3284 state, mstate)); 3285 3286 case DIF_VAR_UID: 3287 if (!dtrace_priv_proc(state, mstate)) 3288 return (0); 3289 3290 /* 3291 * See comment in DIF_VAR_PID. 3292 */ 3293 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3294 return ((uint64_t)p0.p_cred->cr_uid); 3295 3296 /* 3297 * It is always safe to dereference one's own t_procp pointer: 3298 * it always points to a valid, allocated proc structure. 3299 * (This is true because threads don't clean up their own 3300 * state -- they leave that task to whomever reaps them.) 3301 * 3302 * Additionally, it is safe to dereference one's own process 3303 * credential, since this is never NULL after process birth. 3304 */ 3305 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3306 3307 case DIF_VAR_GID: 3308 if (!dtrace_priv_proc(state, mstate)) 3309 return (0); 3310 3311 /* 3312 * See comment in DIF_VAR_PID. 3313 */ 3314 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3315 return ((uint64_t)p0.p_cred->cr_gid); 3316 3317 /* 3318 * It is always safe to dereference one's own t_procp pointer: 3319 * it always points to a valid, allocated proc structure. 3320 * (This is true because threads don't clean up their own 3321 * state -- they leave that task to whomever reaps them.) 3322 * 3323 * Additionally, it is safe to dereference one's own process 3324 * credential, since this is never NULL after process birth. 3325 */ 3326 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3327 3328 case DIF_VAR_ERRNO: { 3329 klwp_t *lwp; 3330 if (!dtrace_priv_proc(state, mstate)) 3331 return (0); 3332 3333 /* 3334 * See comment in DIF_VAR_PID. 3335 */ 3336 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3337 return (0); 3338 3339 /* 3340 * It is always safe to dereference one's own t_lwp pointer in 3341 * the event that this pointer is non-NULL. (This is true 3342 * because threads and lwps don't clean up their own state -- 3343 * they leave that task to whomever reaps them.) 3344 */ 3345 if ((lwp = curthread->t_lwp) == NULL) 3346 return (0); 3347 3348 return ((uint64_t)lwp->lwp_errno); 3349 } 3350 default: 3351 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3352 return (0); 3353 } 3354 } 3355 3356 /* 3357 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 3358 * Notice that we don't bother validating the proper number of arguments or 3359 * their types in the tuple stack. This isn't needed because all argument 3360 * interpretation is safe because of our load safety -- the worst that can 3361 * happen is that a bogus program can obtain bogus results. 3362 */ 3363 static void 3364 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 3365 dtrace_key_t *tupregs, int nargs, 3366 dtrace_mstate_t *mstate, dtrace_state_t *state) 3367 { 3368 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 3369 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 3370 dtrace_vstate_t *vstate = &state->dts_vstate; 3371 3372 union { 3373 mutex_impl_t mi; 3374 uint64_t mx; 3375 } m; 3376 3377 union { 3378 krwlock_t ri; 3379 uintptr_t rw; 3380 } r; 3381 3382 switch (subr) { 3383 case DIF_SUBR_RAND: 3384 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 3385 break; 3386 3387 case DIF_SUBR_MUTEX_OWNED: 3388 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3389 mstate, vstate)) { 3390 regs[rd] = NULL; 3391 break; 3392 } 3393 3394 m.mx = dtrace_load64(tupregs[0].dttk_value); 3395 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3396 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3397 else 3398 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 3399 break; 3400 3401 case DIF_SUBR_MUTEX_OWNER: 3402 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3403 mstate, vstate)) { 3404 regs[rd] = NULL; 3405 break; 3406 } 3407 3408 m.mx = dtrace_load64(tupregs[0].dttk_value); 3409 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3410 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3411 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3412 else 3413 regs[rd] = 0; 3414 break; 3415 3416 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3417 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3418 mstate, vstate)) { 3419 regs[rd] = NULL; 3420 break; 3421 } 3422 3423 m.mx = dtrace_load64(tupregs[0].dttk_value); 3424 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3425 break; 3426 3427 case DIF_SUBR_MUTEX_TYPE_SPIN: 3428 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3429 mstate, vstate)) { 3430 regs[rd] = NULL; 3431 break; 3432 } 3433 3434 m.mx = dtrace_load64(tupregs[0].dttk_value); 3435 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3436 break; 3437 3438 case DIF_SUBR_RW_READ_HELD: { 3439 uintptr_t tmp; 3440 3441 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3442 mstate, vstate)) { 3443 regs[rd] = NULL; 3444 break; 3445 } 3446 3447 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3448 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 3449 break; 3450 } 3451 3452 case DIF_SUBR_RW_WRITE_HELD: 3453 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3454 mstate, vstate)) { 3455 regs[rd] = NULL; 3456 break; 3457 } 3458 3459 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3460 regs[rd] = _RW_WRITE_HELD(&r.ri); 3461 break; 3462 3463 case DIF_SUBR_RW_ISWRITER: 3464 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3465 mstate, vstate)) { 3466 regs[rd] = NULL; 3467 break; 3468 } 3469 3470 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3471 regs[rd] = _RW_ISWRITER(&r.ri); 3472 break; 3473 3474 case DIF_SUBR_BCOPY: { 3475 /* 3476 * We need to be sure that the destination is in the scratch 3477 * region -- no other region is allowed. 3478 */ 3479 uintptr_t src = tupregs[0].dttk_value; 3480 uintptr_t dest = tupregs[1].dttk_value; 3481 size_t size = tupregs[2].dttk_value; 3482 3483 if (!dtrace_inscratch(dest, size, mstate)) { 3484 *flags |= CPU_DTRACE_BADADDR; 3485 *illval = regs[rd]; 3486 break; 3487 } 3488 3489 if (!dtrace_canload(src, size, mstate, vstate)) { 3490 regs[rd] = NULL; 3491 break; 3492 } 3493 3494 dtrace_bcopy((void *)src, (void *)dest, size); 3495 break; 3496 } 3497 3498 case DIF_SUBR_ALLOCA: 3499 case DIF_SUBR_COPYIN: { 3500 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 3501 uint64_t size = 3502 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 3503 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 3504 3505 /* 3506 * This action doesn't require any credential checks since 3507 * probes will not activate in user contexts to which the 3508 * enabling user does not have permissions. 3509 */ 3510 3511 /* 3512 * Rounding up the user allocation size could have overflowed 3513 * a large, bogus allocation (like -1ULL) to 0. 3514 */ 3515 if (scratch_size < size || 3516 !DTRACE_INSCRATCH(mstate, scratch_size)) { 3517 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3518 regs[rd] = NULL; 3519 break; 3520 } 3521 3522 if (subr == DIF_SUBR_COPYIN) { 3523 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3524 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3525 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3526 } 3527 3528 mstate->dtms_scratch_ptr += scratch_size; 3529 regs[rd] = dest; 3530 break; 3531 } 3532 3533 case DIF_SUBR_COPYINTO: { 3534 uint64_t size = tupregs[1].dttk_value; 3535 uintptr_t dest = tupregs[2].dttk_value; 3536 3537 /* 3538 * This action doesn't require any credential checks since 3539 * probes will not activate in user contexts to which the 3540 * enabling user does not have permissions. 3541 */ 3542 if (!dtrace_inscratch(dest, size, mstate)) { 3543 *flags |= CPU_DTRACE_BADADDR; 3544 *illval = regs[rd]; 3545 break; 3546 } 3547 3548 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3549 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3550 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3551 break; 3552 } 3553 3554 case DIF_SUBR_COPYINSTR: { 3555 uintptr_t dest = mstate->dtms_scratch_ptr; 3556 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3557 3558 if (nargs > 1 && tupregs[1].dttk_value < size) 3559 size = tupregs[1].dttk_value + 1; 3560 3561 /* 3562 * This action doesn't require any credential checks since 3563 * probes will not activate in user contexts to which the 3564 * enabling user does not have permissions. 3565 */ 3566 if (!DTRACE_INSCRATCH(mstate, size)) { 3567 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3568 regs[rd] = NULL; 3569 break; 3570 } 3571 3572 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3573 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 3574 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3575 3576 ((char *)dest)[size - 1] = '\0'; 3577 mstate->dtms_scratch_ptr += size; 3578 regs[rd] = dest; 3579 break; 3580 } 3581 3582 case DIF_SUBR_MSGSIZE: 3583 case DIF_SUBR_MSGDSIZE: { 3584 uintptr_t baddr = tupregs[0].dttk_value, daddr; 3585 uintptr_t wptr, rptr; 3586 size_t count = 0; 3587 int cont = 0; 3588 3589 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 3590 3591 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 3592 vstate)) { 3593 regs[rd] = NULL; 3594 break; 3595 } 3596 3597 wptr = dtrace_loadptr(baddr + 3598 offsetof(mblk_t, b_wptr)); 3599 3600 rptr = dtrace_loadptr(baddr + 3601 offsetof(mblk_t, b_rptr)); 3602 3603 if (wptr < rptr) { 3604 *flags |= CPU_DTRACE_BADADDR; 3605 *illval = tupregs[0].dttk_value; 3606 break; 3607 } 3608 3609 daddr = dtrace_loadptr(baddr + 3610 offsetof(mblk_t, b_datap)); 3611 3612 baddr = dtrace_loadptr(baddr + 3613 offsetof(mblk_t, b_cont)); 3614 3615 /* 3616 * We want to prevent against denial-of-service here, 3617 * so we're only going to search the list for 3618 * dtrace_msgdsize_max mblks. 3619 */ 3620 if (cont++ > dtrace_msgdsize_max) { 3621 *flags |= CPU_DTRACE_ILLOP; 3622 break; 3623 } 3624 3625 if (subr == DIF_SUBR_MSGDSIZE) { 3626 if (dtrace_load8(daddr + 3627 offsetof(dblk_t, db_type)) != M_DATA) 3628 continue; 3629 } 3630 3631 count += wptr - rptr; 3632 } 3633 3634 if (!(*flags & CPU_DTRACE_FAULT)) 3635 regs[rd] = count; 3636 3637 break; 3638 } 3639 3640 case DIF_SUBR_PROGENYOF: { 3641 pid_t pid = tupregs[0].dttk_value; 3642 proc_t *p; 3643 int rval = 0; 3644 3645 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3646 3647 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 3648 if (p->p_pidp->pid_id == pid) { 3649 rval = 1; 3650 break; 3651 } 3652 } 3653 3654 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3655 3656 regs[rd] = rval; 3657 break; 3658 } 3659 3660 case DIF_SUBR_SPECULATION: 3661 regs[rd] = dtrace_speculation(state); 3662 break; 3663 3664 case DIF_SUBR_COPYOUT: { 3665 uintptr_t kaddr = tupregs[0].dttk_value; 3666 uintptr_t uaddr = tupregs[1].dttk_value; 3667 uint64_t size = tupregs[2].dttk_value; 3668 3669 if (!dtrace_destructive_disallow && 3670 dtrace_priv_proc_control(state, mstate) && 3671 !dtrace_istoxic(kaddr, size)) { 3672 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3673 dtrace_copyout(kaddr, uaddr, size, flags); 3674 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3675 } 3676 break; 3677 } 3678 3679 case DIF_SUBR_COPYOUTSTR: { 3680 uintptr_t kaddr = tupregs[0].dttk_value; 3681 uintptr_t uaddr = tupregs[1].dttk_value; 3682 uint64_t size = tupregs[2].dttk_value; 3683 3684 if (!dtrace_destructive_disallow && 3685 dtrace_priv_proc_control(state, mstate) && 3686 !dtrace_istoxic(kaddr, size)) { 3687 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3688 dtrace_copyoutstr(kaddr, uaddr, size, flags); 3689 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3690 } 3691 break; 3692 } 3693 3694 case DIF_SUBR_STRLEN: { 3695 size_t sz; 3696 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 3697 sz = dtrace_strlen((char *)addr, 3698 state->dts_options[DTRACEOPT_STRSIZE]); 3699 3700 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 3701 regs[rd] = NULL; 3702 break; 3703 } 3704 3705 regs[rd] = sz; 3706 3707 break; 3708 } 3709 3710 case DIF_SUBR_STRCHR: 3711 case DIF_SUBR_STRRCHR: { 3712 /* 3713 * We're going to iterate over the string looking for the 3714 * specified character. We will iterate until we have reached 3715 * the string length or we have found the character. If this 3716 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 3717 * of the specified character instead of the first. 3718 */ 3719 uintptr_t saddr = tupregs[0].dttk_value; 3720 uintptr_t addr = tupregs[0].dttk_value; 3721 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 3722 char c, target = (char)tupregs[1].dttk_value; 3723 3724 for (regs[rd] = NULL; addr < limit; addr++) { 3725 if ((c = dtrace_load8(addr)) == target) { 3726 regs[rd] = addr; 3727 3728 if (subr == DIF_SUBR_STRCHR) 3729 break; 3730 } 3731 3732 if (c == '\0') 3733 break; 3734 } 3735 3736 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 3737 regs[rd] = NULL; 3738 break; 3739 } 3740 3741 break; 3742 } 3743 3744 case DIF_SUBR_STRSTR: 3745 case DIF_SUBR_INDEX: 3746 case DIF_SUBR_RINDEX: { 3747 /* 3748 * We're going to iterate over the string looking for the 3749 * specified string. We will iterate until we have reached 3750 * the string length or we have found the string. (Yes, this 3751 * is done in the most naive way possible -- but considering 3752 * that the string we're searching for is likely to be 3753 * relatively short, the complexity of Rabin-Karp or similar 3754 * hardly seems merited.) 3755 */ 3756 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 3757 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 3758 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3759 size_t len = dtrace_strlen(addr, size); 3760 size_t sublen = dtrace_strlen(substr, size); 3761 char *limit = addr + len, *orig = addr; 3762 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 3763 int inc = 1; 3764 3765 regs[rd] = notfound; 3766 3767 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 3768 regs[rd] = NULL; 3769 break; 3770 } 3771 3772 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 3773 vstate)) { 3774 regs[rd] = NULL; 3775 break; 3776 } 3777 3778 /* 3779 * strstr() and index()/rindex() have similar semantics if 3780 * both strings are the empty string: strstr() returns a 3781 * pointer to the (empty) string, and index() and rindex() 3782 * both return index 0 (regardless of any position argument). 3783 */ 3784 if (sublen == 0 && len == 0) { 3785 if (subr == DIF_SUBR_STRSTR) 3786 regs[rd] = (uintptr_t)addr; 3787 else 3788 regs[rd] = 0; 3789 break; 3790 } 3791 3792 if (subr != DIF_SUBR_STRSTR) { 3793 if (subr == DIF_SUBR_RINDEX) { 3794 limit = orig - 1; 3795 addr += len; 3796 inc = -1; 3797 } 3798 3799 /* 3800 * Both index() and rindex() take an optional position 3801 * argument that denotes the starting position. 3802 */ 3803 if (nargs == 3) { 3804 int64_t pos = (int64_t)tupregs[2].dttk_value; 3805 3806 /* 3807 * If the position argument to index() is 3808 * negative, Perl implicitly clamps it at 3809 * zero. This semantic is a little surprising 3810 * given the special meaning of negative 3811 * positions to similar Perl functions like 3812 * substr(), but it appears to reflect a 3813 * notion that index() can start from a 3814 * negative index and increment its way up to 3815 * the string. Given this notion, Perl's 3816 * rindex() is at least self-consistent in 3817 * that it implicitly clamps positions greater 3818 * than the string length to be the string 3819 * length. Where Perl completely loses 3820 * coherence, however, is when the specified 3821 * substring is the empty string (""). In 3822 * this case, even if the position is 3823 * negative, rindex() returns 0 -- and even if 3824 * the position is greater than the length, 3825 * index() returns the string length. These 3826 * semantics violate the notion that index() 3827 * should never return a value less than the 3828 * specified position and that rindex() should 3829 * never return a value greater than the 3830 * specified position. (One assumes that 3831 * these semantics are artifacts of Perl's 3832 * implementation and not the results of 3833 * deliberate design -- it beggars belief that 3834 * even Larry Wall could desire such oddness.) 3835 * While in the abstract one would wish for 3836 * consistent position semantics across 3837 * substr(), index() and rindex() -- or at the 3838 * very least self-consistent position 3839 * semantics for index() and rindex() -- we 3840 * instead opt to keep with the extant Perl 3841 * semantics, in all their broken glory. (Do 3842 * we have more desire to maintain Perl's 3843 * semantics than Perl does? Probably.) 3844 */ 3845 if (subr == DIF_SUBR_RINDEX) { 3846 if (pos < 0) { 3847 if (sublen == 0) 3848 regs[rd] = 0; 3849 break; 3850 } 3851 3852 if (pos > len) 3853 pos = len; 3854 } else { 3855 if (pos < 0) 3856 pos = 0; 3857 3858 if (pos >= len) { 3859 if (sublen == 0) 3860 regs[rd] = len; 3861 break; 3862 } 3863 } 3864 3865 addr = orig + pos; 3866 } 3867 } 3868 3869 for (regs[rd] = notfound; addr != limit; addr += inc) { 3870 if (dtrace_strncmp(addr, substr, sublen) == 0) { 3871 if (subr != DIF_SUBR_STRSTR) { 3872 /* 3873 * As D index() and rindex() are 3874 * modeled on Perl (and not on awk), 3875 * we return a zero-based (and not a 3876 * one-based) index. (For you Perl 3877 * weenies: no, we're not going to add 3878 * $[ -- and shouldn't you be at a con 3879 * or something?) 3880 */ 3881 regs[rd] = (uintptr_t)(addr - orig); 3882 break; 3883 } 3884 3885 ASSERT(subr == DIF_SUBR_STRSTR); 3886 regs[rd] = (uintptr_t)addr; 3887 break; 3888 } 3889 } 3890 3891 break; 3892 } 3893 3894 case DIF_SUBR_STRTOK: { 3895 uintptr_t addr = tupregs[0].dttk_value; 3896 uintptr_t tokaddr = tupregs[1].dttk_value; 3897 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3898 uintptr_t limit, toklimit = tokaddr + size; 3899 uint8_t c, tokmap[32]; /* 256 / 8 */ 3900 char *dest = (char *)mstate->dtms_scratch_ptr; 3901 int i; 3902 3903 /* 3904 * Check both the token buffer and (later) the input buffer, 3905 * since both could be non-scratch addresses. 3906 */ 3907 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 3908 regs[rd] = NULL; 3909 break; 3910 } 3911 3912 if (!DTRACE_INSCRATCH(mstate, size)) { 3913 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3914 regs[rd] = NULL; 3915 break; 3916 } 3917 3918 if (addr == NULL) { 3919 /* 3920 * If the address specified is NULL, we use our saved 3921 * strtok pointer from the mstate. Note that this 3922 * means that the saved strtok pointer is _only_ 3923 * valid within multiple enablings of the same probe -- 3924 * it behaves like an implicit clause-local variable. 3925 */ 3926 addr = mstate->dtms_strtok; 3927 } else { 3928 /* 3929 * If the user-specified address is non-NULL we must 3930 * access check it. This is the only time we have 3931 * a chance to do so, since this address may reside 3932 * in the string table of this clause-- future calls 3933 * (when we fetch addr from mstate->dtms_strtok) 3934 * would fail this access check. 3935 */ 3936 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 3937 regs[rd] = NULL; 3938 break; 3939 } 3940 } 3941 3942 /* 3943 * First, zero the token map, and then process the token 3944 * string -- setting a bit in the map for every character 3945 * found in the token string. 3946 */ 3947 for (i = 0; i < sizeof (tokmap); i++) 3948 tokmap[i] = 0; 3949 3950 for (; tokaddr < toklimit; tokaddr++) { 3951 if ((c = dtrace_load8(tokaddr)) == '\0') 3952 break; 3953 3954 ASSERT((c >> 3) < sizeof (tokmap)); 3955 tokmap[c >> 3] |= (1 << (c & 0x7)); 3956 } 3957 3958 for (limit = addr + size; addr < limit; addr++) { 3959 /* 3960 * We're looking for a character that is _not_ contained 3961 * in the token string. 3962 */ 3963 if ((c = dtrace_load8(addr)) == '\0') 3964 break; 3965 3966 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3967 break; 3968 } 3969 3970 if (c == '\0') { 3971 /* 3972 * We reached the end of the string without finding 3973 * any character that was not in the token string. 3974 * We return NULL in this case, and we set the saved 3975 * address to NULL as well. 3976 */ 3977 regs[rd] = NULL; 3978 mstate->dtms_strtok = NULL; 3979 break; 3980 } 3981 3982 /* 3983 * From here on, we're copying into the destination string. 3984 */ 3985 for (i = 0; addr < limit && i < size - 1; addr++) { 3986 if ((c = dtrace_load8(addr)) == '\0') 3987 break; 3988 3989 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3990 break; 3991 3992 ASSERT(i < size); 3993 dest[i++] = c; 3994 } 3995 3996 ASSERT(i < size); 3997 dest[i] = '\0'; 3998 regs[rd] = (uintptr_t)dest; 3999 mstate->dtms_scratch_ptr += size; 4000 mstate->dtms_strtok = addr; 4001 break; 4002 } 4003 4004 case DIF_SUBR_SUBSTR: { 4005 uintptr_t s = tupregs[0].dttk_value; 4006 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4007 char *d = (char *)mstate->dtms_scratch_ptr; 4008 int64_t index = (int64_t)tupregs[1].dttk_value; 4009 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4010 size_t len = dtrace_strlen((char *)s, size); 4011 int64_t i; 4012 4013 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4014 regs[rd] = NULL; 4015 break; 4016 } 4017 4018 if (!DTRACE_INSCRATCH(mstate, size)) { 4019 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4020 regs[rd] = NULL; 4021 break; 4022 } 4023 4024 if (nargs <= 2) 4025 remaining = (int64_t)size; 4026 4027 if (index < 0) { 4028 index += len; 4029 4030 if (index < 0 && index + remaining > 0) { 4031 remaining += index; 4032 index = 0; 4033 } 4034 } 4035 4036 if (index >= len || index < 0) { 4037 remaining = 0; 4038 } else if (remaining < 0) { 4039 remaining += len - index; 4040 } else if (index + remaining > size) { 4041 remaining = size - index; 4042 } 4043 4044 for (i = 0; i < remaining; i++) { 4045 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4046 break; 4047 } 4048 4049 d[i] = '\0'; 4050 4051 mstate->dtms_scratch_ptr += size; 4052 regs[rd] = (uintptr_t)d; 4053 break; 4054 } 4055 4056 case DIF_SUBR_TOUPPER: 4057 case DIF_SUBR_TOLOWER: { 4058 uintptr_t s = tupregs[0].dttk_value; 4059 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4060 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4061 size_t len = dtrace_strlen((char *)s, size); 4062 char lower, upper, convert; 4063 int64_t i; 4064 4065 if (subr == DIF_SUBR_TOUPPER) { 4066 lower = 'a'; 4067 upper = 'z'; 4068 convert = 'A'; 4069 } else { 4070 lower = 'A'; 4071 upper = 'Z'; 4072 convert = 'a'; 4073 } 4074 4075 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4076 regs[rd] = NULL; 4077 break; 4078 } 4079 4080 if (!DTRACE_INSCRATCH(mstate, size)) { 4081 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4082 regs[rd] = NULL; 4083 break; 4084 } 4085 4086 for (i = 0; i < size - 1; i++) { 4087 if ((c = dtrace_load8(s + i)) == '\0') 4088 break; 4089 4090 if (c >= lower && c <= upper) 4091 c = convert + (c - lower); 4092 4093 dest[i] = c; 4094 } 4095 4096 ASSERT(i < size); 4097 dest[i] = '\0'; 4098 regs[rd] = (uintptr_t)dest; 4099 mstate->dtms_scratch_ptr += size; 4100 break; 4101 } 4102 4103 case DIF_SUBR_GETMAJOR: 4104 #ifdef _LP64 4105 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 4106 #else 4107 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 4108 #endif 4109 break; 4110 4111 case DIF_SUBR_GETMINOR: 4112 #ifdef _LP64 4113 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 4114 #else 4115 regs[rd] = tupregs[0].dttk_value & MAXMIN; 4116 #endif 4117 break; 4118 4119 case DIF_SUBR_DDI_PATHNAME: { 4120 /* 4121 * This one is a galactic mess. We are going to roughly 4122 * emulate ddi_pathname(), but it's made more complicated 4123 * by the fact that we (a) want to include the minor name and 4124 * (b) must proceed iteratively instead of recursively. 4125 */ 4126 uintptr_t dest = mstate->dtms_scratch_ptr; 4127 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4128 char *start = (char *)dest, *end = start + size - 1; 4129 uintptr_t daddr = tupregs[0].dttk_value; 4130 int64_t minor = (int64_t)tupregs[1].dttk_value; 4131 char *s; 4132 int i, len, depth = 0; 4133 4134 /* 4135 * Due to all the pointer jumping we do and context we must 4136 * rely upon, we just mandate that the user must have kernel 4137 * read privileges to use this routine. 4138 */ 4139 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 4140 *flags |= CPU_DTRACE_KPRIV; 4141 *illval = daddr; 4142 regs[rd] = NULL; 4143 } 4144 4145 if (!DTRACE_INSCRATCH(mstate, size)) { 4146 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4147 regs[rd] = NULL; 4148 break; 4149 } 4150 4151 *end = '\0'; 4152 4153 /* 4154 * We want to have a name for the minor. In order to do this, 4155 * we need to walk the minor list from the devinfo. We want 4156 * to be sure that we don't infinitely walk a circular list, 4157 * so we check for circularity by sending a scout pointer 4158 * ahead two elements for every element that we iterate over; 4159 * if the list is circular, these will ultimately point to the 4160 * same element. You may recognize this little trick as the 4161 * answer to a stupid interview question -- one that always 4162 * seems to be asked by those who had to have it laboriously 4163 * explained to them, and who can't even concisely describe 4164 * the conditions under which one would be forced to resort to 4165 * this technique. Needless to say, those conditions are 4166 * found here -- and probably only here. Is this the only use 4167 * of this infamous trick in shipping, production code? If it 4168 * isn't, it probably should be... 4169 */ 4170 if (minor != -1) { 4171 uintptr_t maddr = dtrace_loadptr(daddr + 4172 offsetof(struct dev_info, devi_minor)); 4173 4174 uintptr_t next = offsetof(struct ddi_minor_data, next); 4175 uintptr_t name = offsetof(struct ddi_minor_data, 4176 d_minor) + offsetof(struct ddi_minor, name); 4177 uintptr_t dev = offsetof(struct ddi_minor_data, 4178 d_minor) + offsetof(struct ddi_minor, dev); 4179 uintptr_t scout; 4180 4181 if (maddr != NULL) 4182 scout = dtrace_loadptr(maddr + next); 4183 4184 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4185 uint64_t m; 4186 #ifdef _LP64 4187 m = dtrace_load64(maddr + dev) & MAXMIN64; 4188 #else 4189 m = dtrace_load32(maddr + dev) & MAXMIN; 4190 #endif 4191 if (m != minor) { 4192 maddr = dtrace_loadptr(maddr + next); 4193 4194 if (scout == NULL) 4195 continue; 4196 4197 scout = dtrace_loadptr(scout + next); 4198 4199 if (scout == NULL) 4200 continue; 4201 4202 scout = dtrace_loadptr(scout + next); 4203 4204 if (scout == NULL) 4205 continue; 4206 4207 if (scout == maddr) { 4208 *flags |= CPU_DTRACE_ILLOP; 4209 break; 4210 } 4211 4212 continue; 4213 } 4214 4215 /* 4216 * We have the minor data. Now we need to 4217 * copy the minor's name into the end of the 4218 * pathname. 4219 */ 4220 s = (char *)dtrace_loadptr(maddr + name); 4221 len = dtrace_strlen(s, size); 4222 4223 if (*flags & CPU_DTRACE_FAULT) 4224 break; 4225 4226 if (len != 0) { 4227 if ((end -= (len + 1)) < start) 4228 break; 4229 4230 *end = ':'; 4231 } 4232 4233 for (i = 1; i <= len; i++) 4234 end[i] = dtrace_load8((uintptr_t)s++); 4235 break; 4236 } 4237 } 4238 4239 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4240 ddi_node_state_t devi_state; 4241 4242 devi_state = dtrace_load32(daddr + 4243 offsetof(struct dev_info, devi_node_state)); 4244 4245 if (*flags & CPU_DTRACE_FAULT) 4246 break; 4247 4248 if (devi_state >= DS_INITIALIZED) { 4249 s = (char *)dtrace_loadptr(daddr + 4250 offsetof(struct dev_info, devi_addr)); 4251 len = dtrace_strlen(s, size); 4252 4253 if (*flags & CPU_DTRACE_FAULT) 4254 break; 4255 4256 if (len != 0) { 4257 if ((end -= (len + 1)) < start) 4258 break; 4259 4260 *end = '@'; 4261 } 4262 4263 for (i = 1; i <= len; i++) 4264 end[i] = dtrace_load8((uintptr_t)s++); 4265 } 4266 4267 /* 4268 * Now for the node name... 4269 */ 4270 s = (char *)dtrace_loadptr(daddr + 4271 offsetof(struct dev_info, devi_node_name)); 4272 4273 daddr = dtrace_loadptr(daddr + 4274 offsetof(struct dev_info, devi_parent)); 4275 4276 /* 4277 * If our parent is NULL (that is, if we're the root 4278 * node), we're going to use the special path 4279 * "devices". 4280 */ 4281 if (daddr == NULL) 4282 s = "devices"; 4283 4284 len = dtrace_strlen(s, size); 4285 if (*flags & CPU_DTRACE_FAULT) 4286 break; 4287 4288 if ((end -= (len + 1)) < start) 4289 break; 4290 4291 for (i = 1; i <= len; i++) 4292 end[i] = dtrace_load8((uintptr_t)s++); 4293 *end = '/'; 4294 4295 if (depth++ > dtrace_devdepth_max) { 4296 *flags |= CPU_DTRACE_ILLOP; 4297 break; 4298 } 4299 } 4300 4301 if (end < start) 4302 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4303 4304 if (daddr == NULL) { 4305 regs[rd] = (uintptr_t)end; 4306 mstate->dtms_scratch_ptr += size; 4307 } 4308 4309 break; 4310 } 4311 4312 case DIF_SUBR_STRJOIN: { 4313 char *d = (char *)mstate->dtms_scratch_ptr; 4314 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4315 uintptr_t s1 = tupregs[0].dttk_value; 4316 uintptr_t s2 = tupregs[1].dttk_value; 4317 int i = 0; 4318 4319 if (!dtrace_strcanload(s1, size, mstate, vstate) || 4320 !dtrace_strcanload(s2, size, mstate, vstate)) { 4321 regs[rd] = NULL; 4322 break; 4323 } 4324 4325 if (!DTRACE_INSCRATCH(mstate, size)) { 4326 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4327 regs[rd] = NULL; 4328 break; 4329 } 4330 4331 for (;;) { 4332 if (i >= size) { 4333 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4334 regs[rd] = NULL; 4335 break; 4336 } 4337 4338 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 4339 i--; 4340 break; 4341 } 4342 } 4343 4344 for (;;) { 4345 if (i >= size) { 4346 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4347 regs[rd] = NULL; 4348 break; 4349 } 4350 4351 if ((d[i++] = dtrace_load8(s2++)) == '\0') 4352 break; 4353 } 4354 4355 if (i < size) { 4356 mstate->dtms_scratch_ptr += i; 4357 regs[rd] = (uintptr_t)d; 4358 } 4359 4360 break; 4361 } 4362 4363 case DIF_SUBR_LLTOSTR: { 4364 int64_t i = (int64_t)tupregs[0].dttk_value; 4365 uint64_t val, digit; 4366 uint64_t size = 65; /* enough room for 2^64 in binary */ 4367 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 4368 int base = 10; 4369 4370 if (nargs > 1) { 4371 if ((base = tupregs[1].dttk_value) <= 1 || 4372 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 4373 *flags |= CPU_DTRACE_ILLOP; 4374 break; 4375 } 4376 } 4377 4378 val = (base == 10 && i < 0) ? i * -1 : i; 4379 4380 if (!DTRACE_INSCRATCH(mstate, size)) { 4381 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4382 regs[rd] = NULL; 4383 break; 4384 } 4385 4386 for (*end-- = '\0'; val; val /= base) { 4387 if ((digit = val % base) <= '9' - '0') { 4388 *end-- = '0' + digit; 4389 } else { 4390 *end-- = 'a' + (digit - ('9' - '0') - 1); 4391 } 4392 } 4393 4394 if (i == 0 && base == 16) 4395 *end-- = '0'; 4396 4397 if (base == 16) 4398 *end-- = 'x'; 4399 4400 if (i == 0 || base == 8 || base == 16) 4401 *end-- = '0'; 4402 4403 if (i < 0 && base == 10) 4404 *end-- = '-'; 4405 4406 regs[rd] = (uintptr_t)end + 1; 4407 mstate->dtms_scratch_ptr += size; 4408 break; 4409 } 4410 4411 case DIF_SUBR_HTONS: 4412 case DIF_SUBR_NTOHS: 4413 #ifdef _BIG_ENDIAN 4414 regs[rd] = (uint16_t)tupregs[0].dttk_value; 4415 #else 4416 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 4417 #endif 4418 break; 4419 4420 4421 case DIF_SUBR_HTONL: 4422 case DIF_SUBR_NTOHL: 4423 #ifdef _BIG_ENDIAN 4424 regs[rd] = (uint32_t)tupregs[0].dttk_value; 4425 #else 4426 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 4427 #endif 4428 break; 4429 4430 4431 case DIF_SUBR_HTONLL: 4432 case DIF_SUBR_NTOHLL: 4433 #ifdef _BIG_ENDIAN 4434 regs[rd] = (uint64_t)tupregs[0].dttk_value; 4435 #else 4436 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 4437 #endif 4438 break; 4439 4440 4441 case DIF_SUBR_DIRNAME: 4442 case DIF_SUBR_BASENAME: { 4443 char *dest = (char *)mstate->dtms_scratch_ptr; 4444 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4445 uintptr_t src = tupregs[0].dttk_value; 4446 int i, j, len = dtrace_strlen((char *)src, size); 4447 int lastbase = -1, firstbase = -1, lastdir = -1; 4448 int start, end; 4449 4450 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 4451 regs[rd] = NULL; 4452 break; 4453 } 4454 4455 if (!DTRACE_INSCRATCH(mstate, size)) { 4456 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4457 regs[rd] = NULL; 4458 break; 4459 } 4460 4461 /* 4462 * The basename and dirname for a zero-length string is 4463 * defined to be "." 4464 */ 4465 if (len == 0) { 4466 len = 1; 4467 src = (uintptr_t)"."; 4468 } 4469 4470 /* 4471 * Start from the back of the string, moving back toward the 4472 * front until we see a character that isn't a slash. That 4473 * character is the last character in the basename. 4474 */ 4475 for (i = len - 1; i >= 0; i--) { 4476 if (dtrace_load8(src + i) != '/') 4477 break; 4478 } 4479 4480 if (i >= 0) 4481 lastbase = i; 4482 4483 /* 4484 * Starting from the last character in the basename, move 4485 * towards the front until we find a slash. The character 4486 * that we processed immediately before that is the first 4487 * character in the basename. 4488 */ 4489 for (; i >= 0; i--) { 4490 if (dtrace_load8(src + i) == '/') 4491 break; 4492 } 4493 4494 if (i >= 0) 4495 firstbase = i + 1; 4496 4497 /* 4498 * Now keep going until we find a non-slash character. That 4499 * character is the last character in the dirname. 4500 */ 4501 for (; i >= 0; i--) { 4502 if (dtrace_load8(src + i) != '/') 4503 break; 4504 } 4505 4506 if (i >= 0) 4507 lastdir = i; 4508 4509 ASSERT(!(lastbase == -1 && firstbase != -1)); 4510 ASSERT(!(firstbase == -1 && lastdir != -1)); 4511 4512 if (lastbase == -1) { 4513 /* 4514 * We didn't find a non-slash character. We know that 4515 * the length is non-zero, so the whole string must be 4516 * slashes. In either the dirname or the basename 4517 * case, we return '/'. 4518 */ 4519 ASSERT(firstbase == -1); 4520 firstbase = lastbase = lastdir = 0; 4521 } 4522 4523 if (firstbase == -1) { 4524 /* 4525 * The entire string consists only of a basename 4526 * component. If we're looking for dirname, we need 4527 * to change our string to be just "."; if we're 4528 * looking for a basename, we'll just set the first 4529 * character of the basename to be 0. 4530 */ 4531 if (subr == DIF_SUBR_DIRNAME) { 4532 ASSERT(lastdir == -1); 4533 src = (uintptr_t)"."; 4534 lastdir = 0; 4535 } else { 4536 firstbase = 0; 4537 } 4538 } 4539 4540 if (subr == DIF_SUBR_DIRNAME) { 4541 if (lastdir == -1) { 4542 /* 4543 * We know that we have a slash in the name -- 4544 * or lastdir would be set to 0, above. And 4545 * because lastdir is -1, we know that this 4546 * slash must be the first character. (That 4547 * is, the full string must be of the form 4548 * "/basename".) In this case, the last 4549 * character of the directory name is 0. 4550 */ 4551 lastdir = 0; 4552 } 4553 4554 start = 0; 4555 end = lastdir; 4556 } else { 4557 ASSERT(subr == DIF_SUBR_BASENAME); 4558 ASSERT(firstbase != -1 && lastbase != -1); 4559 start = firstbase; 4560 end = lastbase; 4561 } 4562 4563 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 4564 dest[j] = dtrace_load8(src + i); 4565 4566 dest[j] = '\0'; 4567 regs[rd] = (uintptr_t)dest; 4568 mstate->dtms_scratch_ptr += size; 4569 break; 4570 } 4571 4572 case DIF_SUBR_GETF: { 4573 uintptr_t fd = tupregs[0].dttk_value; 4574 uf_info_t *finfo = &curthread->t_procp->p_user.u_finfo; 4575 file_t *fp; 4576 4577 if (!dtrace_priv_proc(state, mstate)) { 4578 regs[rd] = NULL; 4579 break; 4580 } 4581 4582 /* 4583 * This is safe because fi_nfiles only increases, and the 4584 * fi_list array is not freed when the array size doubles. 4585 * (See the comment in flist_grow() for details on the 4586 * management of the u_finfo structure.) 4587 */ 4588 fp = fd < finfo->fi_nfiles ? finfo->fi_list[fd].uf_file : NULL; 4589 4590 mstate->dtms_getf = fp; 4591 regs[rd] = (uintptr_t)fp; 4592 break; 4593 } 4594 4595 case DIF_SUBR_CLEANPATH: { 4596 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4597 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4598 uintptr_t src = tupregs[0].dttk_value; 4599 int i = 0, j = 0; 4600 zone_t *z; 4601 4602 if (!dtrace_strcanload(src, size, mstate, vstate)) { 4603 regs[rd] = NULL; 4604 break; 4605 } 4606 4607 if (!DTRACE_INSCRATCH(mstate, size)) { 4608 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4609 regs[rd] = NULL; 4610 break; 4611 } 4612 4613 /* 4614 * Move forward, loading each character. 4615 */ 4616 do { 4617 c = dtrace_load8(src + i++); 4618 next: 4619 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 4620 break; 4621 4622 if (c != '/') { 4623 dest[j++] = c; 4624 continue; 4625 } 4626 4627 c = dtrace_load8(src + i++); 4628 4629 if (c == '/') { 4630 /* 4631 * We have two slashes -- we can just advance 4632 * to the next character. 4633 */ 4634 goto next; 4635 } 4636 4637 if (c != '.') { 4638 /* 4639 * This is not "." and it's not ".." -- we can 4640 * just store the "/" and this character and 4641 * drive on. 4642 */ 4643 dest[j++] = '/'; 4644 dest[j++] = c; 4645 continue; 4646 } 4647 4648 c = dtrace_load8(src + i++); 4649 4650 if (c == '/') { 4651 /* 4652 * This is a "/./" component. We're not going 4653 * to store anything in the destination buffer; 4654 * we're just going to go to the next component. 4655 */ 4656 goto next; 4657 } 4658 4659 if (c != '.') { 4660 /* 4661 * This is not ".." -- we can just store the 4662 * "/." and this character and continue 4663 * processing. 4664 */ 4665 dest[j++] = '/'; 4666 dest[j++] = '.'; 4667 dest[j++] = c; 4668 continue; 4669 } 4670 4671 c = dtrace_load8(src + i++); 4672 4673 if (c != '/' && c != '\0') { 4674 /* 4675 * This is not ".." -- it's "..[mumble]". 4676 * We'll store the "/.." and this character 4677 * and continue processing. 4678 */ 4679 dest[j++] = '/'; 4680 dest[j++] = '.'; 4681 dest[j++] = '.'; 4682 dest[j++] = c; 4683 continue; 4684 } 4685 4686 /* 4687 * This is "/../" or "/..\0". We need to back up 4688 * our destination pointer until we find a "/". 4689 */ 4690 i--; 4691 while (j != 0 && dest[--j] != '/') 4692 continue; 4693 4694 if (c == '\0') 4695 dest[++j] = '/'; 4696 } while (c != '\0'); 4697 4698 dest[j] = '\0'; 4699 4700 if (mstate->dtms_getf != NULL && 4701 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 4702 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 4703 /* 4704 * If we've done a getf() as a part of this ECB and we 4705 * don't have kernel access (and we're not in the global 4706 * zone), check if the path we cleaned up begins with 4707 * the zone's root path, and trim it off if so. Note 4708 * that this is an output cleanliness issue, not a 4709 * security issue: knowing one's zone root path does 4710 * not enable privilege escalation. 4711 */ 4712 if (strstr(dest, z->zone_rootpath) == dest) 4713 dest += strlen(z->zone_rootpath) - 1; 4714 } 4715 4716 regs[rd] = (uintptr_t)dest; 4717 mstate->dtms_scratch_ptr += size; 4718 break; 4719 } 4720 4721 case DIF_SUBR_INET_NTOA: 4722 case DIF_SUBR_INET_NTOA6: 4723 case DIF_SUBR_INET_NTOP: { 4724 size_t size; 4725 int af, argi, i; 4726 char *base, *end; 4727 4728 if (subr == DIF_SUBR_INET_NTOP) { 4729 af = (int)tupregs[0].dttk_value; 4730 argi = 1; 4731 } else { 4732 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 4733 argi = 0; 4734 } 4735 4736 if (af == AF_INET) { 4737 ipaddr_t ip4; 4738 uint8_t *ptr8, val; 4739 4740 /* 4741 * Safely load the IPv4 address. 4742 */ 4743 ip4 = dtrace_load32(tupregs[argi].dttk_value); 4744 4745 /* 4746 * Check an IPv4 string will fit in scratch. 4747 */ 4748 size = INET_ADDRSTRLEN; 4749 if (!DTRACE_INSCRATCH(mstate, size)) { 4750 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4751 regs[rd] = NULL; 4752 break; 4753 } 4754 base = (char *)mstate->dtms_scratch_ptr; 4755 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4756 4757 /* 4758 * Stringify as a dotted decimal quad. 4759 */ 4760 *end-- = '\0'; 4761 ptr8 = (uint8_t *)&ip4; 4762 for (i = 3; i >= 0; i--) { 4763 val = ptr8[i]; 4764 4765 if (val == 0) { 4766 *end-- = '0'; 4767 } else { 4768 for (; val; val /= 10) { 4769 *end-- = '0' + (val % 10); 4770 } 4771 } 4772 4773 if (i > 0) 4774 *end-- = '.'; 4775 } 4776 ASSERT(end + 1 >= base); 4777 4778 } else if (af == AF_INET6) { 4779 struct in6_addr ip6; 4780 int firstzero, tryzero, numzero, v6end; 4781 uint16_t val; 4782 const char digits[] = "0123456789abcdef"; 4783 4784 /* 4785 * Stringify using RFC 1884 convention 2 - 16 bit 4786 * hexadecimal values with a zero-run compression. 4787 * Lower case hexadecimal digits are used. 4788 * eg, fe80::214:4fff:fe0b:76c8. 4789 * The IPv4 embedded form is returned for inet_ntop, 4790 * just the IPv4 string is returned for inet_ntoa6. 4791 */ 4792 4793 /* 4794 * Safely load the IPv6 address. 4795 */ 4796 dtrace_bcopy( 4797 (void *)(uintptr_t)tupregs[argi].dttk_value, 4798 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 4799 4800 /* 4801 * Check an IPv6 string will fit in scratch. 4802 */ 4803 size = INET6_ADDRSTRLEN; 4804 if (!DTRACE_INSCRATCH(mstate, size)) { 4805 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4806 regs[rd] = NULL; 4807 break; 4808 } 4809 base = (char *)mstate->dtms_scratch_ptr; 4810 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4811 *end-- = '\0'; 4812 4813 /* 4814 * Find the longest run of 16 bit zero values 4815 * for the single allowed zero compression - "::". 4816 */ 4817 firstzero = -1; 4818 tryzero = -1; 4819 numzero = 1; 4820 for (i = 0; i < sizeof (struct in6_addr); i++) { 4821 if (ip6._S6_un._S6_u8[i] == 0 && 4822 tryzero == -1 && i % 2 == 0) { 4823 tryzero = i; 4824 continue; 4825 } 4826 4827 if (tryzero != -1 && 4828 (ip6._S6_un._S6_u8[i] != 0 || 4829 i == sizeof (struct in6_addr) - 1)) { 4830 4831 if (i - tryzero <= numzero) { 4832 tryzero = -1; 4833 continue; 4834 } 4835 4836 firstzero = tryzero; 4837 numzero = i - i % 2 - tryzero; 4838 tryzero = -1; 4839 4840 if (ip6._S6_un._S6_u8[i] == 0 && 4841 i == sizeof (struct in6_addr) - 1) 4842 numzero += 2; 4843 } 4844 } 4845 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 4846 4847 /* 4848 * Check for an IPv4 embedded address. 4849 */ 4850 v6end = sizeof (struct in6_addr) - 2; 4851 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 4852 IN6_IS_ADDR_V4COMPAT(&ip6)) { 4853 for (i = sizeof (struct in6_addr) - 1; 4854 i >= DTRACE_V4MAPPED_OFFSET; i--) { 4855 ASSERT(end >= base); 4856 4857 val = ip6._S6_un._S6_u8[i]; 4858 4859 if (val == 0) { 4860 *end-- = '0'; 4861 } else { 4862 for (; val; val /= 10) { 4863 *end-- = '0' + val % 10; 4864 } 4865 } 4866 4867 if (i > DTRACE_V4MAPPED_OFFSET) 4868 *end-- = '.'; 4869 } 4870 4871 if (subr == DIF_SUBR_INET_NTOA6) 4872 goto inetout; 4873 4874 /* 4875 * Set v6end to skip the IPv4 address that 4876 * we have already stringified. 4877 */ 4878 v6end = 10; 4879 } 4880 4881 /* 4882 * Build the IPv6 string by working through the 4883 * address in reverse. 4884 */ 4885 for (i = v6end; i >= 0; i -= 2) { 4886 ASSERT(end >= base); 4887 4888 if (i == firstzero + numzero - 2) { 4889 *end-- = ':'; 4890 *end-- = ':'; 4891 i -= numzero - 2; 4892 continue; 4893 } 4894 4895 if (i < 14 && i != firstzero - 2) 4896 *end-- = ':'; 4897 4898 val = (ip6._S6_un._S6_u8[i] << 8) + 4899 ip6._S6_un._S6_u8[i + 1]; 4900 4901 if (val == 0) { 4902 *end-- = '0'; 4903 } else { 4904 for (; val; val /= 16) { 4905 *end-- = digits[val % 16]; 4906 } 4907 } 4908 } 4909 ASSERT(end + 1 >= base); 4910 4911 } else { 4912 /* 4913 * The user didn't use AH_INET or AH_INET6. 4914 */ 4915 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4916 regs[rd] = NULL; 4917 break; 4918 } 4919 4920 inetout: regs[rd] = (uintptr_t)end + 1; 4921 mstate->dtms_scratch_ptr += size; 4922 break; 4923 } 4924 4925 } 4926 } 4927 4928 /* 4929 * Emulate the execution of DTrace IR instructions specified by the given 4930 * DIF object. This function is deliberately void of assertions as all of 4931 * the necessary checks are handled by a call to dtrace_difo_validate(). 4932 */ 4933 static uint64_t 4934 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 4935 dtrace_vstate_t *vstate, dtrace_state_t *state) 4936 { 4937 const dif_instr_t *text = difo->dtdo_buf; 4938 const uint_t textlen = difo->dtdo_len; 4939 const char *strtab = difo->dtdo_strtab; 4940 const uint64_t *inttab = difo->dtdo_inttab; 4941 4942 uint64_t rval = 0; 4943 dtrace_statvar_t *svar; 4944 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 4945 dtrace_difv_t *v; 4946 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 4947 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 4948 4949 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 4950 uint64_t regs[DIF_DIR_NREGS]; 4951 uint64_t *tmp; 4952 4953 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 4954 int64_t cc_r; 4955 uint_t pc = 0, id, opc; 4956 uint8_t ttop = 0; 4957 dif_instr_t instr; 4958 uint_t r1, r2, rd; 4959 4960 /* 4961 * We stash the current DIF object into the machine state: we need it 4962 * for subsequent access checking. 4963 */ 4964 mstate->dtms_difo = difo; 4965 4966 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 4967 4968 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 4969 opc = pc; 4970 4971 instr = text[pc++]; 4972 r1 = DIF_INSTR_R1(instr); 4973 r2 = DIF_INSTR_R2(instr); 4974 rd = DIF_INSTR_RD(instr); 4975 4976 switch (DIF_INSTR_OP(instr)) { 4977 case DIF_OP_OR: 4978 regs[rd] = regs[r1] | regs[r2]; 4979 break; 4980 case DIF_OP_XOR: 4981 regs[rd] = regs[r1] ^ regs[r2]; 4982 break; 4983 case DIF_OP_AND: 4984 regs[rd] = regs[r1] & regs[r2]; 4985 break; 4986 case DIF_OP_SLL: 4987 regs[rd] = regs[r1] << regs[r2]; 4988 break; 4989 case DIF_OP_SRL: 4990 regs[rd] = regs[r1] >> regs[r2]; 4991 break; 4992 case DIF_OP_SUB: 4993 regs[rd] = regs[r1] - regs[r2]; 4994 break; 4995 case DIF_OP_ADD: 4996 regs[rd] = regs[r1] + regs[r2]; 4997 break; 4998 case DIF_OP_MUL: 4999 regs[rd] = regs[r1] * regs[r2]; 5000 break; 5001 case DIF_OP_SDIV: 5002 if (regs[r2] == 0) { 5003 regs[rd] = 0; 5004 *flags |= CPU_DTRACE_DIVZERO; 5005 } else { 5006 regs[rd] = (int64_t)regs[r1] / 5007 (int64_t)regs[r2]; 5008 } 5009 break; 5010 5011 case DIF_OP_UDIV: 5012 if (regs[r2] == 0) { 5013 regs[rd] = 0; 5014 *flags |= CPU_DTRACE_DIVZERO; 5015 } else { 5016 regs[rd] = regs[r1] / regs[r2]; 5017 } 5018 break; 5019 5020 case DIF_OP_SREM: 5021 if (regs[r2] == 0) { 5022 regs[rd] = 0; 5023 *flags |= CPU_DTRACE_DIVZERO; 5024 } else { 5025 regs[rd] = (int64_t)regs[r1] % 5026 (int64_t)regs[r2]; 5027 } 5028 break; 5029 5030 case DIF_OP_UREM: 5031 if (regs[r2] == 0) { 5032 regs[rd] = 0; 5033 *flags |= CPU_DTRACE_DIVZERO; 5034 } else { 5035 regs[rd] = regs[r1] % regs[r2]; 5036 } 5037 break; 5038 5039 case DIF_OP_NOT: 5040 regs[rd] = ~regs[r1]; 5041 break; 5042 case DIF_OP_MOV: 5043 regs[rd] = regs[r1]; 5044 break; 5045 case DIF_OP_CMP: 5046 cc_r = regs[r1] - regs[r2]; 5047 cc_n = cc_r < 0; 5048 cc_z = cc_r == 0; 5049 cc_v = 0; 5050 cc_c = regs[r1] < regs[r2]; 5051 break; 5052 case DIF_OP_TST: 5053 cc_n = cc_v = cc_c = 0; 5054 cc_z = regs[r1] == 0; 5055 break; 5056 case DIF_OP_BA: 5057 pc = DIF_INSTR_LABEL(instr); 5058 break; 5059 case DIF_OP_BE: 5060 if (cc_z) 5061 pc = DIF_INSTR_LABEL(instr); 5062 break; 5063 case DIF_OP_BNE: 5064 if (cc_z == 0) 5065 pc = DIF_INSTR_LABEL(instr); 5066 break; 5067 case DIF_OP_BG: 5068 if ((cc_z | (cc_n ^ cc_v)) == 0) 5069 pc = DIF_INSTR_LABEL(instr); 5070 break; 5071 case DIF_OP_BGU: 5072 if ((cc_c | cc_z) == 0) 5073 pc = DIF_INSTR_LABEL(instr); 5074 break; 5075 case DIF_OP_BGE: 5076 if ((cc_n ^ cc_v) == 0) 5077 pc = DIF_INSTR_LABEL(instr); 5078 break; 5079 case DIF_OP_BGEU: 5080 if (cc_c == 0) 5081 pc = DIF_INSTR_LABEL(instr); 5082 break; 5083 case DIF_OP_BL: 5084 if (cc_n ^ cc_v) 5085 pc = DIF_INSTR_LABEL(instr); 5086 break; 5087 case DIF_OP_BLU: 5088 if (cc_c) 5089 pc = DIF_INSTR_LABEL(instr); 5090 break; 5091 case DIF_OP_BLE: 5092 if (cc_z | (cc_n ^ cc_v)) 5093 pc = DIF_INSTR_LABEL(instr); 5094 break; 5095 case DIF_OP_BLEU: 5096 if (cc_c | cc_z) 5097 pc = DIF_INSTR_LABEL(instr); 5098 break; 5099 case DIF_OP_RLDSB: 5100 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 5101 break; 5102 /*FALLTHROUGH*/ 5103 case DIF_OP_LDSB: 5104 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 5105 break; 5106 case DIF_OP_RLDSH: 5107 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 5108 break; 5109 /*FALLTHROUGH*/ 5110 case DIF_OP_LDSH: 5111 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 5112 break; 5113 case DIF_OP_RLDSW: 5114 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 5115 break; 5116 /*FALLTHROUGH*/ 5117 case DIF_OP_LDSW: 5118 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 5119 break; 5120 case DIF_OP_RLDUB: 5121 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 5122 break; 5123 /*FALLTHROUGH*/ 5124 case DIF_OP_LDUB: 5125 regs[rd] = dtrace_load8(regs[r1]); 5126 break; 5127 case DIF_OP_RLDUH: 5128 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 5129 break; 5130 /*FALLTHROUGH*/ 5131 case DIF_OP_LDUH: 5132 regs[rd] = dtrace_load16(regs[r1]); 5133 break; 5134 case DIF_OP_RLDUW: 5135 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 5136 break; 5137 /*FALLTHROUGH*/ 5138 case DIF_OP_LDUW: 5139 regs[rd] = dtrace_load32(regs[r1]); 5140 break; 5141 case DIF_OP_RLDX: 5142 if (!dtrace_canload(regs[r1], 8, mstate, vstate)) 5143 break; 5144 /*FALLTHROUGH*/ 5145 case DIF_OP_LDX: 5146 regs[rd] = dtrace_load64(regs[r1]); 5147 break; 5148 case DIF_OP_ULDSB: 5149 regs[rd] = (int8_t) 5150 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5151 break; 5152 case DIF_OP_ULDSH: 5153 regs[rd] = (int16_t) 5154 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5155 break; 5156 case DIF_OP_ULDSW: 5157 regs[rd] = (int32_t) 5158 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5159 break; 5160 case DIF_OP_ULDUB: 5161 regs[rd] = 5162 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5163 break; 5164 case DIF_OP_ULDUH: 5165 regs[rd] = 5166 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5167 break; 5168 case DIF_OP_ULDUW: 5169 regs[rd] = 5170 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5171 break; 5172 case DIF_OP_ULDX: 5173 regs[rd] = 5174 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 5175 break; 5176 case DIF_OP_RET: 5177 rval = regs[rd]; 5178 pc = textlen; 5179 break; 5180 case DIF_OP_NOP: 5181 break; 5182 case DIF_OP_SETX: 5183 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 5184 break; 5185 case DIF_OP_SETS: 5186 regs[rd] = (uint64_t)(uintptr_t) 5187 (strtab + DIF_INSTR_STRING(instr)); 5188 break; 5189 case DIF_OP_SCMP: { 5190 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 5191 uintptr_t s1 = regs[r1]; 5192 uintptr_t s2 = regs[r2]; 5193 5194 if (s1 != NULL && 5195 !dtrace_strcanload(s1, sz, mstate, vstate)) 5196 break; 5197 if (s2 != NULL && 5198 !dtrace_strcanload(s2, sz, mstate, vstate)) 5199 break; 5200 5201 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 5202 5203 cc_n = cc_r < 0; 5204 cc_z = cc_r == 0; 5205 cc_v = cc_c = 0; 5206 break; 5207 } 5208 case DIF_OP_LDGA: 5209 regs[rd] = dtrace_dif_variable(mstate, state, 5210 r1, regs[r2]); 5211 break; 5212 case DIF_OP_LDGS: 5213 id = DIF_INSTR_VAR(instr); 5214 5215 if (id >= DIF_VAR_OTHER_UBASE) { 5216 uintptr_t a; 5217 5218 id -= DIF_VAR_OTHER_UBASE; 5219 svar = vstate->dtvs_globals[id]; 5220 ASSERT(svar != NULL); 5221 v = &svar->dtsv_var; 5222 5223 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 5224 regs[rd] = svar->dtsv_data; 5225 break; 5226 } 5227 5228 a = (uintptr_t)svar->dtsv_data; 5229 5230 if (*(uint8_t *)a == UINT8_MAX) { 5231 /* 5232 * If the 0th byte is set to UINT8_MAX 5233 * then this is to be treated as a 5234 * reference to a NULL variable. 5235 */ 5236 regs[rd] = NULL; 5237 } else { 5238 regs[rd] = a + sizeof (uint64_t); 5239 } 5240 5241 break; 5242 } 5243 5244 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 5245 break; 5246 5247 case DIF_OP_STGS: 5248 id = DIF_INSTR_VAR(instr); 5249 5250 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5251 id -= DIF_VAR_OTHER_UBASE; 5252 5253 svar = vstate->dtvs_globals[id]; 5254 ASSERT(svar != NULL); 5255 v = &svar->dtsv_var; 5256 5257 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5258 uintptr_t a = (uintptr_t)svar->dtsv_data; 5259 5260 ASSERT(a != NULL); 5261 ASSERT(svar->dtsv_size != 0); 5262 5263 if (regs[rd] == NULL) { 5264 *(uint8_t *)a = UINT8_MAX; 5265 break; 5266 } else { 5267 *(uint8_t *)a = 0; 5268 a += sizeof (uint64_t); 5269 } 5270 if (!dtrace_vcanload( 5271 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5272 mstate, vstate)) 5273 break; 5274 5275 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5276 (void *)a, &v->dtdv_type); 5277 break; 5278 } 5279 5280 svar->dtsv_data = regs[rd]; 5281 break; 5282 5283 case DIF_OP_LDTA: 5284 /* 5285 * There are no DTrace built-in thread-local arrays at 5286 * present. This opcode is saved for future work. 5287 */ 5288 *flags |= CPU_DTRACE_ILLOP; 5289 regs[rd] = 0; 5290 break; 5291 5292 case DIF_OP_LDLS: 5293 id = DIF_INSTR_VAR(instr); 5294 5295 if (id < DIF_VAR_OTHER_UBASE) { 5296 /* 5297 * For now, this has no meaning. 5298 */ 5299 regs[rd] = 0; 5300 break; 5301 } 5302 5303 id -= DIF_VAR_OTHER_UBASE; 5304 5305 ASSERT(id < vstate->dtvs_nlocals); 5306 ASSERT(vstate->dtvs_locals != NULL); 5307 5308 svar = vstate->dtvs_locals[id]; 5309 ASSERT(svar != NULL); 5310 v = &svar->dtsv_var; 5311 5312 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5313 uintptr_t a = (uintptr_t)svar->dtsv_data; 5314 size_t sz = v->dtdv_type.dtdt_size; 5315 5316 sz += sizeof (uint64_t); 5317 ASSERT(svar->dtsv_size == NCPU * sz); 5318 a += CPU->cpu_id * sz; 5319 5320 if (*(uint8_t *)a == UINT8_MAX) { 5321 /* 5322 * If the 0th byte is set to UINT8_MAX 5323 * then this is to be treated as a 5324 * reference to a NULL variable. 5325 */ 5326 regs[rd] = NULL; 5327 } else { 5328 regs[rd] = a + sizeof (uint64_t); 5329 } 5330 5331 break; 5332 } 5333 5334 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5335 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5336 regs[rd] = tmp[CPU->cpu_id]; 5337 break; 5338 5339 case DIF_OP_STLS: 5340 id = DIF_INSTR_VAR(instr); 5341 5342 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5343 id -= DIF_VAR_OTHER_UBASE; 5344 ASSERT(id < vstate->dtvs_nlocals); 5345 5346 ASSERT(vstate->dtvs_locals != NULL); 5347 svar = vstate->dtvs_locals[id]; 5348 ASSERT(svar != NULL); 5349 v = &svar->dtsv_var; 5350 5351 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5352 uintptr_t a = (uintptr_t)svar->dtsv_data; 5353 size_t sz = v->dtdv_type.dtdt_size; 5354 5355 sz += sizeof (uint64_t); 5356 ASSERT(svar->dtsv_size == NCPU * sz); 5357 a += CPU->cpu_id * sz; 5358 5359 if (regs[rd] == NULL) { 5360 *(uint8_t *)a = UINT8_MAX; 5361 break; 5362 } else { 5363 *(uint8_t *)a = 0; 5364 a += sizeof (uint64_t); 5365 } 5366 5367 if (!dtrace_vcanload( 5368 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5369 mstate, vstate)) 5370 break; 5371 5372 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5373 (void *)a, &v->dtdv_type); 5374 break; 5375 } 5376 5377 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5378 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5379 tmp[CPU->cpu_id] = regs[rd]; 5380 break; 5381 5382 case DIF_OP_LDTS: { 5383 dtrace_dynvar_t *dvar; 5384 dtrace_key_t *key; 5385 5386 id = DIF_INSTR_VAR(instr); 5387 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5388 id -= DIF_VAR_OTHER_UBASE; 5389 v = &vstate->dtvs_tlocals[id]; 5390 5391 key = &tupregs[DIF_DTR_NREGS]; 5392 key[0].dttk_value = (uint64_t)id; 5393 key[0].dttk_size = 0; 5394 DTRACE_TLS_THRKEY(key[1].dttk_value); 5395 key[1].dttk_size = 0; 5396 5397 dvar = dtrace_dynvar(dstate, 2, key, 5398 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 5399 mstate, vstate); 5400 5401 if (dvar == NULL) { 5402 regs[rd] = 0; 5403 break; 5404 } 5405 5406 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5407 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5408 } else { 5409 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5410 } 5411 5412 break; 5413 } 5414 5415 case DIF_OP_STTS: { 5416 dtrace_dynvar_t *dvar; 5417 dtrace_key_t *key; 5418 5419 id = DIF_INSTR_VAR(instr); 5420 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5421 id -= DIF_VAR_OTHER_UBASE; 5422 5423 key = &tupregs[DIF_DTR_NREGS]; 5424 key[0].dttk_value = (uint64_t)id; 5425 key[0].dttk_size = 0; 5426 DTRACE_TLS_THRKEY(key[1].dttk_value); 5427 key[1].dttk_size = 0; 5428 v = &vstate->dtvs_tlocals[id]; 5429 5430 dvar = dtrace_dynvar(dstate, 2, key, 5431 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5432 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5433 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5434 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5435 5436 /* 5437 * Given that we're storing to thread-local data, 5438 * we need to flush our predicate cache. 5439 */ 5440 curthread->t_predcache = NULL; 5441 5442 if (dvar == NULL) 5443 break; 5444 5445 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5446 if (!dtrace_vcanload( 5447 (void *)(uintptr_t)regs[rd], 5448 &v->dtdv_type, mstate, vstate)) 5449 break; 5450 5451 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5452 dvar->dtdv_data, &v->dtdv_type); 5453 } else { 5454 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5455 } 5456 5457 break; 5458 } 5459 5460 case DIF_OP_SRA: 5461 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 5462 break; 5463 5464 case DIF_OP_CALL: 5465 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 5466 regs, tupregs, ttop, mstate, state); 5467 break; 5468 5469 case DIF_OP_PUSHTR: 5470 if (ttop == DIF_DTR_NREGS) { 5471 *flags |= CPU_DTRACE_TUPOFLOW; 5472 break; 5473 } 5474 5475 if (r1 == DIF_TYPE_STRING) { 5476 /* 5477 * If this is a string type and the size is 0, 5478 * we'll use the system-wide default string 5479 * size. Note that we are _not_ looking at 5480 * the value of the DTRACEOPT_STRSIZE option; 5481 * had this been set, we would expect to have 5482 * a non-zero size value in the "pushtr". 5483 */ 5484 tupregs[ttop].dttk_size = 5485 dtrace_strlen((char *)(uintptr_t)regs[rd], 5486 regs[r2] ? regs[r2] : 5487 dtrace_strsize_default) + 1; 5488 } else { 5489 tupregs[ttop].dttk_size = regs[r2]; 5490 } 5491 5492 tupregs[ttop++].dttk_value = regs[rd]; 5493 break; 5494 5495 case DIF_OP_PUSHTV: 5496 if (ttop == DIF_DTR_NREGS) { 5497 *flags |= CPU_DTRACE_TUPOFLOW; 5498 break; 5499 } 5500 5501 tupregs[ttop].dttk_value = regs[rd]; 5502 tupregs[ttop++].dttk_size = 0; 5503 break; 5504 5505 case DIF_OP_POPTS: 5506 if (ttop != 0) 5507 ttop--; 5508 break; 5509 5510 case DIF_OP_FLUSHTS: 5511 ttop = 0; 5512 break; 5513 5514 case DIF_OP_LDGAA: 5515 case DIF_OP_LDTAA: { 5516 dtrace_dynvar_t *dvar; 5517 dtrace_key_t *key = tupregs; 5518 uint_t nkeys = ttop; 5519 5520 id = DIF_INSTR_VAR(instr); 5521 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5522 id -= DIF_VAR_OTHER_UBASE; 5523 5524 key[nkeys].dttk_value = (uint64_t)id; 5525 key[nkeys++].dttk_size = 0; 5526 5527 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 5528 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5529 key[nkeys++].dttk_size = 0; 5530 v = &vstate->dtvs_tlocals[id]; 5531 } else { 5532 v = &vstate->dtvs_globals[id]->dtsv_var; 5533 } 5534 5535 dvar = dtrace_dynvar(dstate, nkeys, key, 5536 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5537 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5538 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 5539 5540 if (dvar == NULL) { 5541 regs[rd] = 0; 5542 break; 5543 } 5544 5545 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5546 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5547 } else { 5548 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5549 } 5550 5551 break; 5552 } 5553 5554 case DIF_OP_STGAA: 5555 case DIF_OP_STTAA: { 5556 dtrace_dynvar_t *dvar; 5557 dtrace_key_t *key = tupregs; 5558 uint_t nkeys = ttop; 5559 5560 id = DIF_INSTR_VAR(instr); 5561 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5562 id -= DIF_VAR_OTHER_UBASE; 5563 5564 key[nkeys].dttk_value = (uint64_t)id; 5565 key[nkeys++].dttk_size = 0; 5566 5567 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 5568 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5569 key[nkeys++].dttk_size = 0; 5570 v = &vstate->dtvs_tlocals[id]; 5571 } else { 5572 v = &vstate->dtvs_globals[id]->dtsv_var; 5573 } 5574 5575 dvar = dtrace_dynvar(dstate, nkeys, key, 5576 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5577 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5578 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5579 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5580 5581 if (dvar == NULL) 5582 break; 5583 5584 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5585 if (!dtrace_vcanload( 5586 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5587 mstate, vstate)) 5588 break; 5589 5590 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5591 dvar->dtdv_data, &v->dtdv_type); 5592 } else { 5593 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5594 } 5595 5596 break; 5597 } 5598 5599 case DIF_OP_ALLOCS: { 5600 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5601 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 5602 5603 /* 5604 * Rounding up the user allocation size could have 5605 * overflowed large, bogus allocations (like -1ULL) to 5606 * 0. 5607 */ 5608 if (size < regs[r1] || 5609 !DTRACE_INSCRATCH(mstate, size)) { 5610 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5611 regs[rd] = NULL; 5612 break; 5613 } 5614 5615 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 5616 mstate->dtms_scratch_ptr += size; 5617 regs[rd] = ptr; 5618 break; 5619 } 5620 5621 case DIF_OP_COPYS: 5622 if (!dtrace_canstore(regs[rd], regs[r2], 5623 mstate, vstate)) { 5624 *flags |= CPU_DTRACE_BADADDR; 5625 *illval = regs[rd]; 5626 break; 5627 } 5628 5629 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 5630 break; 5631 5632 dtrace_bcopy((void *)(uintptr_t)regs[r1], 5633 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 5634 break; 5635 5636 case DIF_OP_STB: 5637 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 5638 *flags |= CPU_DTRACE_BADADDR; 5639 *illval = regs[rd]; 5640 break; 5641 } 5642 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 5643 break; 5644 5645 case DIF_OP_STH: 5646 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 5647 *flags |= CPU_DTRACE_BADADDR; 5648 *illval = regs[rd]; 5649 break; 5650 } 5651 if (regs[rd] & 1) { 5652 *flags |= CPU_DTRACE_BADALIGN; 5653 *illval = regs[rd]; 5654 break; 5655 } 5656 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 5657 break; 5658 5659 case DIF_OP_STW: 5660 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 5661 *flags |= CPU_DTRACE_BADADDR; 5662 *illval = regs[rd]; 5663 break; 5664 } 5665 if (regs[rd] & 3) { 5666 *flags |= CPU_DTRACE_BADALIGN; 5667 *illval = regs[rd]; 5668 break; 5669 } 5670 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 5671 break; 5672 5673 case DIF_OP_STX: 5674 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 5675 *flags |= CPU_DTRACE_BADADDR; 5676 *illval = regs[rd]; 5677 break; 5678 } 5679 if (regs[rd] & 7) { 5680 *flags |= CPU_DTRACE_BADALIGN; 5681 *illval = regs[rd]; 5682 break; 5683 } 5684 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 5685 break; 5686 } 5687 } 5688 5689 if (!(*flags & CPU_DTRACE_FAULT)) 5690 return (rval); 5691 5692 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 5693 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 5694 5695 return (0); 5696 } 5697 5698 static void 5699 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 5700 { 5701 dtrace_probe_t *probe = ecb->dte_probe; 5702 dtrace_provider_t *prov = probe->dtpr_provider; 5703 char c[DTRACE_FULLNAMELEN + 80], *str; 5704 char *msg = "dtrace: breakpoint action at probe "; 5705 char *ecbmsg = " (ecb "; 5706 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 5707 uintptr_t val = (uintptr_t)ecb; 5708 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 5709 5710 if (dtrace_destructive_disallow) 5711 return; 5712 5713 /* 5714 * It's impossible to be taking action on the NULL probe. 5715 */ 5716 ASSERT(probe != NULL); 5717 5718 /* 5719 * This is a poor man's (destitute man's?) sprintf(): we want to 5720 * print the provider name, module name, function name and name of 5721 * the probe, along with the hex address of the ECB with the breakpoint 5722 * action -- all of which we must place in the character buffer by 5723 * hand. 5724 */ 5725 while (*msg != '\0') 5726 c[i++] = *msg++; 5727 5728 for (str = prov->dtpv_name; *str != '\0'; str++) 5729 c[i++] = *str; 5730 c[i++] = ':'; 5731 5732 for (str = probe->dtpr_mod; *str != '\0'; str++) 5733 c[i++] = *str; 5734 c[i++] = ':'; 5735 5736 for (str = probe->dtpr_func; *str != '\0'; str++) 5737 c[i++] = *str; 5738 c[i++] = ':'; 5739 5740 for (str = probe->dtpr_name; *str != '\0'; str++) 5741 c[i++] = *str; 5742 5743 while (*ecbmsg != '\0') 5744 c[i++] = *ecbmsg++; 5745 5746 while (shift >= 0) { 5747 mask = (uintptr_t)0xf << shift; 5748 5749 if (val >= ((uintptr_t)1 << shift)) 5750 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 5751 shift -= 4; 5752 } 5753 5754 c[i++] = ')'; 5755 c[i] = '\0'; 5756 5757 debug_enter(c); 5758 } 5759 5760 static void 5761 dtrace_action_panic(dtrace_ecb_t *ecb) 5762 { 5763 dtrace_probe_t *probe = ecb->dte_probe; 5764 5765 /* 5766 * It's impossible to be taking action on the NULL probe. 5767 */ 5768 ASSERT(probe != NULL); 5769 5770 if (dtrace_destructive_disallow) 5771 return; 5772 5773 if (dtrace_panicked != NULL) 5774 return; 5775 5776 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 5777 return; 5778 5779 /* 5780 * We won the right to panic. (We want to be sure that only one 5781 * thread calls panic() from dtrace_probe(), and that panic() is 5782 * called exactly once.) 5783 */ 5784 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 5785 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 5786 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 5787 } 5788 5789 static void 5790 dtrace_action_raise(uint64_t sig) 5791 { 5792 if (dtrace_destructive_disallow) 5793 return; 5794 5795 if (sig >= NSIG) { 5796 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5797 return; 5798 } 5799 5800 /* 5801 * raise() has a queue depth of 1 -- we ignore all subsequent 5802 * invocations of the raise() action. 5803 */ 5804 if (curthread->t_dtrace_sig == 0) 5805 curthread->t_dtrace_sig = (uint8_t)sig; 5806 5807 curthread->t_sig_check = 1; 5808 aston(curthread); 5809 } 5810 5811 static void 5812 dtrace_action_stop(void) 5813 { 5814 if (dtrace_destructive_disallow) 5815 return; 5816 5817 if (!curthread->t_dtrace_stop) { 5818 curthread->t_dtrace_stop = 1; 5819 curthread->t_sig_check = 1; 5820 aston(curthread); 5821 } 5822 } 5823 5824 static void 5825 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 5826 { 5827 hrtime_t now; 5828 volatile uint16_t *flags; 5829 cpu_t *cpu = CPU; 5830 5831 if (dtrace_destructive_disallow) 5832 return; 5833 5834 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 5835 5836 now = dtrace_gethrtime(); 5837 5838 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 5839 /* 5840 * We need to advance the mark to the current time. 5841 */ 5842 cpu->cpu_dtrace_chillmark = now; 5843 cpu->cpu_dtrace_chilled = 0; 5844 } 5845 5846 /* 5847 * Now check to see if the requested chill time would take us over 5848 * the maximum amount of time allowed in the chill interval. (Or 5849 * worse, if the calculation itself induces overflow.) 5850 */ 5851 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 5852 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 5853 *flags |= CPU_DTRACE_ILLOP; 5854 return; 5855 } 5856 5857 while (dtrace_gethrtime() - now < val) 5858 continue; 5859 5860 /* 5861 * Normally, we assure that the value of the variable "timestamp" does 5862 * not change within an ECB. The presence of chill() represents an 5863 * exception to this rule, however. 5864 */ 5865 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 5866 cpu->cpu_dtrace_chilled += val; 5867 } 5868 5869 static void 5870 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 5871 uint64_t *buf, uint64_t arg) 5872 { 5873 int nframes = DTRACE_USTACK_NFRAMES(arg); 5874 int strsize = DTRACE_USTACK_STRSIZE(arg); 5875 uint64_t *pcs = &buf[1], *fps; 5876 char *str = (char *)&pcs[nframes]; 5877 int size, offs = 0, i, j; 5878 uintptr_t old = mstate->dtms_scratch_ptr, saved; 5879 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 5880 char *sym; 5881 5882 /* 5883 * Should be taking a faster path if string space has not been 5884 * allocated. 5885 */ 5886 ASSERT(strsize != 0); 5887 5888 /* 5889 * We will first allocate some temporary space for the frame pointers. 5890 */ 5891 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5892 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 5893 (nframes * sizeof (uint64_t)); 5894 5895 if (!DTRACE_INSCRATCH(mstate, size)) { 5896 /* 5897 * Not enough room for our frame pointers -- need to indicate 5898 * that we ran out of scratch space. 5899 */ 5900 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5901 return; 5902 } 5903 5904 mstate->dtms_scratch_ptr += size; 5905 saved = mstate->dtms_scratch_ptr; 5906 5907 /* 5908 * Now get a stack with both program counters and frame pointers. 5909 */ 5910 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5911 dtrace_getufpstack(buf, fps, nframes + 1); 5912 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5913 5914 /* 5915 * If that faulted, we're cooked. 5916 */ 5917 if (*flags & CPU_DTRACE_FAULT) 5918 goto out; 5919 5920 /* 5921 * Now we want to walk up the stack, calling the USTACK helper. For 5922 * each iteration, we restore the scratch pointer. 5923 */ 5924 for (i = 0; i < nframes; i++) { 5925 mstate->dtms_scratch_ptr = saved; 5926 5927 if (offs >= strsize) 5928 break; 5929 5930 sym = (char *)(uintptr_t)dtrace_helper( 5931 DTRACE_HELPER_ACTION_USTACK, 5932 mstate, state, pcs[i], fps[i]); 5933 5934 /* 5935 * If we faulted while running the helper, we're going to 5936 * clear the fault and null out the corresponding string. 5937 */ 5938 if (*flags & CPU_DTRACE_FAULT) { 5939 *flags &= ~CPU_DTRACE_FAULT; 5940 str[offs++] = '\0'; 5941 continue; 5942 } 5943 5944 if (sym == NULL) { 5945 str[offs++] = '\0'; 5946 continue; 5947 } 5948 5949 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5950 5951 /* 5952 * Now copy in the string that the helper returned to us. 5953 */ 5954 for (j = 0; offs + j < strsize; j++) { 5955 if ((str[offs + j] = sym[j]) == '\0') 5956 break; 5957 } 5958 5959 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5960 5961 offs += j + 1; 5962 } 5963 5964 if (offs >= strsize) { 5965 /* 5966 * If we didn't have room for all of the strings, we don't 5967 * abort processing -- this needn't be a fatal error -- but we 5968 * still want to increment a counter (dts_stkstroverflows) to 5969 * allow this condition to be warned about. (If this is from 5970 * a jstack() action, it is easily tuned via jstackstrsize.) 5971 */ 5972 dtrace_error(&state->dts_stkstroverflows); 5973 } 5974 5975 while (offs < strsize) 5976 str[offs++] = '\0'; 5977 5978 out: 5979 mstate->dtms_scratch_ptr = old; 5980 } 5981 5982 /* 5983 * If you're looking for the epicenter of DTrace, you just found it. This 5984 * is the function called by the provider to fire a probe -- from which all 5985 * subsequent probe-context DTrace activity emanates. 5986 */ 5987 void 5988 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 5989 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 5990 { 5991 processorid_t cpuid; 5992 dtrace_icookie_t cookie; 5993 dtrace_probe_t *probe; 5994 dtrace_mstate_t mstate; 5995 dtrace_ecb_t *ecb; 5996 dtrace_action_t *act; 5997 intptr_t offs; 5998 size_t size; 5999 int vtime, onintr; 6000 volatile uint16_t *flags; 6001 hrtime_t now; 6002 6003 /* 6004 * Kick out immediately if this CPU is still being born (in which case 6005 * curthread will be set to -1) or the current thread can't allow 6006 * probes in its current context. 6007 */ 6008 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 6009 return; 6010 6011 cookie = dtrace_interrupt_disable(); 6012 probe = dtrace_probes[id - 1]; 6013 cpuid = CPU->cpu_id; 6014 onintr = CPU_ON_INTR(CPU); 6015 6016 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 6017 probe->dtpr_predcache == curthread->t_predcache) { 6018 /* 6019 * We have hit in the predicate cache; we know that 6020 * this predicate would evaluate to be false. 6021 */ 6022 dtrace_interrupt_enable(cookie); 6023 return; 6024 } 6025 6026 if (panic_quiesce) { 6027 /* 6028 * We don't trace anything if we're panicking. 6029 */ 6030 dtrace_interrupt_enable(cookie); 6031 return; 6032 } 6033 6034 now = dtrace_gethrtime(); 6035 vtime = dtrace_vtime_references != 0; 6036 6037 if (vtime && curthread->t_dtrace_start) 6038 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 6039 6040 mstate.dtms_difo = NULL; 6041 mstate.dtms_probe = probe; 6042 mstate.dtms_strtok = NULL; 6043 mstate.dtms_arg[0] = arg0; 6044 mstate.dtms_arg[1] = arg1; 6045 mstate.dtms_arg[2] = arg2; 6046 mstate.dtms_arg[3] = arg3; 6047 mstate.dtms_arg[4] = arg4; 6048 6049 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 6050 6051 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 6052 dtrace_predicate_t *pred = ecb->dte_predicate; 6053 dtrace_state_t *state = ecb->dte_state; 6054 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 6055 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 6056 dtrace_vstate_t *vstate = &state->dts_vstate; 6057 dtrace_provider_t *prov = probe->dtpr_provider; 6058 uint64_t tracememsize = 0; 6059 int committed = 0; 6060 caddr_t tomax; 6061 6062 /* 6063 * A little subtlety with the following (seemingly innocuous) 6064 * declaration of the automatic 'val': by looking at the 6065 * code, you might think that it could be declared in the 6066 * action processing loop, below. (That is, it's only used in 6067 * the action processing loop.) However, it must be declared 6068 * out of that scope because in the case of DIF expression 6069 * arguments to aggregating actions, one iteration of the 6070 * action loop will use the last iteration's value. 6071 */ 6072 #ifdef lint 6073 uint64_t val = 0; 6074 #else 6075 uint64_t val; 6076 #endif 6077 6078 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 6079 mstate.dtms_access = DTRACE_ACCESS_ARGS | DTRACE_ACCESS_PROC; 6080 mstate.dtms_getf = NULL; 6081 6082 *flags &= ~CPU_DTRACE_ERROR; 6083 6084 if (prov == dtrace_provider) { 6085 /* 6086 * If dtrace itself is the provider of this probe, 6087 * we're only going to continue processing the ECB if 6088 * arg0 (the dtrace_state_t) is equal to the ECB's 6089 * creating state. (This prevents disjoint consumers 6090 * from seeing one another's metaprobes.) 6091 */ 6092 if (arg0 != (uint64_t)(uintptr_t)state) 6093 continue; 6094 } 6095 6096 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 6097 /* 6098 * We're not currently active. If our provider isn't 6099 * the dtrace pseudo provider, we're not interested. 6100 */ 6101 if (prov != dtrace_provider) 6102 continue; 6103 6104 /* 6105 * Now we must further check if we are in the BEGIN 6106 * probe. If we are, we will only continue processing 6107 * if we're still in WARMUP -- if one BEGIN enabling 6108 * has invoked the exit() action, we don't want to 6109 * evaluate subsequent BEGIN enablings. 6110 */ 6111 if (probe->dtpr_id == dtrace_probeid_begin && 6112 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 6113 ASSERT(state->dts_activity == 6114 DTRACE_ACTIVITY_DRAINING); 6115 continue; 6116 } 6117 } 6118 6119 if (ecb->dte_cond && !dtrace_priv_probe(state, &mstate, ecb)) 6120 continue; 6121 6122 if (now - state->dts_alive > dtrace_deadman_timeout) { 6123 /* 6124 * We seem to be dead. Unless we (a) have kernel 6125 * destructive permissions (b) have explicitly enabled 6126 * destructive actions and (c) destructive actions have 6127 * not been disabled, we're going to transition into 6128 * the KILLED state, from which no further processing 6129 * on this state will be performed. 6130 */ 6131 if (!dtrace_priv_kernel_destructive(state) || 6132 !state->dts_cred.dcr_destructive || 6133 dtrace_destructive_disallow) { 6134 void *activity = &state->dts_activity; 6135 dtrace_activity_t current; 6136 6137 do { 6138 current = state->dts_activity; 6139 } while (dtrace_cas32(activity, current, 6140 DTRACE_ACTIVITY_KILLED) != current); 6141 6142 continue; 6143 } 6144 } 6145 6146 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 6147 ecb->dte_alignment, state, &mstate)) < 0) 6148 continue; 6149 6150 tomax = buf->dtb_tomax; 6151 ASSERT(tomax != NULL); 6152 6153 if (ecb->dte_size != 0) { 6154 dtrace_rechdr_t dtrh; 6155 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 6156 mstate.dtms_timestamp = dtrace_gethrtime(); 6157 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 6158 } 6159 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 6160 dtrh.dtrh_epid = ecb->dte_epid; 6161 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 6162 mstate.dtms_timestamp); 6163 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 6164 } 6165 6166 mstate.dtms_epid = ecb->dte_epid; 6167 mstate.dtms_present |= DTRACE_MSTATE_EPID; 6168 6169 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 6170 mstate.dtms_access |= DTRACE_ACCESS_KERNEL; 6171 6172 if (pred != NULL) { 6173 dtrace_difo_t *dp = pred->dtp_difo; 6174 int rval; 6175 6176 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 6177 6178 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 6179 dtrace_cacheid_t cid = probe->dtpr_predcache; 6180 6181 if (cid != DTRACE_CACHEIDNONE && !onintr) { 6182 /* 6183 * Update the predicate cache... 6184 */ 6185 ASSERT(cid == pred->dtp_cacheid); 6186 curthread->t_predcache = cid; 6187 } 6188 6189 continue; 6190 } 6191 } 6192 6193 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 6194 act != NULL; act = act->dta_next) { 6195 size_t valoffs; 6196 dtrace_difo_t *dp; 6197 dtrace_recdesc_t *rec = &act->dta_rec; 6198 6199 size = rec->dtrd_size; 6200 valoffs = offs + rec->dtrd_offset; 6201 6202 if (DTRACEACT_ISAGG(act->dta_kind)) { 6203 uint64_t v = 0xbad; 6204 dtrace_aggregation_t *agg; 6205 6206 agg = (dtrace_aggregation_t *)act; 6207 6208 if ((dp = act->dta_difo) != NULL) 6209 v = dtrace_dif_emulate(dp, 6210 &mstate, vstate, state); 6211 6212 if (*flags & CPU_DTRACE_ERROR) 6213 continue; 6214 6215 /* 6216 * Note that we always pass the expression 6217 * value from the previous iteration of the 6218 * action loop. This value will only be used 6219 * if there is an expression argument to the 6220 * aggregating action, denoted by the 6221 * dtag_hasarg field. 6222 */ 6223 dtrace_aggregate(agg, buf, 6224 offs, aggbuf, v, val); 6225 continue; 6226 } 6227 6228 switch (act->dta_kind) { 6229 case DTRACEACT_STOP: 6230 if (dtrace_priv_proc_destructive(state, 6231 &mstate)) 6232 dtrace_action_stop(); 6233 continue; 6234 6235 case DTRACEACT_BREAKPOINT: 6236 if (dtrace_priv_kernel_destructive(state)) 6237 dtrace_action_breakpoint(ecb); 6238 continue; 6239 6240 case DTRACEACT_PANIC: 6241 if (dtrace_priv_kernel_destructive(state)) 6242 dtrace_action_panic(ecb); 6243 continue; 6244 6245 case DTRACEACT_STACK: 6246 if (!dtrace_priv_kernel(state)) 6247 continue; 6248 6249 dtrace_getpcstack((pc_t *)(tomax + valoffs), 6250 size / sizeof (pc_t), probe->dtpr_aframes, 6251 DTRACE_ANCHORED(probe) ? NULL : 6252 (uint32_t *)arg0); 6253 6254 continue; 6255 6256 case DTRACEACT_JSTACK: 6257 case DTRACEACT_USTACK: 6258 if (!dtrace_priv_proc(state, &mstate)) 6259 continue; 6260 6261 /* 6262 * See comment in DIF_VAR_PID. 6263 */ 6264 if (DTRACE_ANCHORED(mstate.dtms_probe) && 6265 CPU_ON_INTR(CPU)) { 6266 int depth = DTRACE_USTACK_NFRAMES( 6267 rec->dtrd_arg) + 1; 6268 6269 dtrace_bzero((void *)(tomax + valoffs), 6270 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 6271 + depth * sizeof (uint64_t)); 6272 6273 continue; 6274 } 6275 6276 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 6277 curproc->p_dtrace_helpers != NULL) { 6278 /* 6279 * This is the slow path -- we have 6280 * allocated string space, and we're 6281 * getting the stack of a process that 6282 * has helpers. Call into a separate 6283 * routine to perform this processing. 6284 */ 6285 dtrace_action_ustack(&mstate, state, 6286 (uint64_t *)(tomax + valoffs), 6287 rec->dtrd_arg); 6288 continue; 6289 } 6290 6291 /* 6292 * Clear the string space, since there's no 6293 * helper to do it for us. 6294 */ 6295 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0) { 6296 int depth = DTRACE_USTACK_NFRAMES( 6297 rec->dtrd_arg); 6298 size_t strsize = DTRACE_USTACK_STRSIZE( 6299 rec->dtrd_arg); 6300 uint64_t *buf = (uint64_t *)(tomax + 6301 valoffs); 6302 void *strspace = &buf[depth + 1]; 6303 6304 dtrace_bzero(strspace, 6305 MIN(depth, strsize)); 6306 } 6307 6308 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6309 dtrace_getupcstack((uint64_t *) 6310 (tomax + valoffs), 6311 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 6312 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6313 continue; 6314 6315 default: 6316 break; 6317 } 6318 6319 dp = act->dta_difo; 6320 ASSERT(dp != NULL); 6321 6322 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 6323 6324 if (*flags & CPU_DTRACE_ERROR) 6325 continue; 6326 6327 switch (act->dta_kind) { 6328 case DTRACEACT_SPECULATE: { 6329 dtrace_rechdr_t *dtrh; 6330 6331 ASSERT(buf == &state->dts_buffer[cpuid]); 6332 buf = dtrace_speculation_buffer(state, 6333 cpuid, val); 6334 6335 if (buf == NULL) { 6336 *flags |= CPU_DTRACE_DROP; 6337 continue; 6338 } 6339 6340 offs = dtrace_buffer_reserve(buf, 6341 ecb->dte_needed, ecb->dte_alignment, 6342 state, NULL); 6343 6344 if (offs < 0) { 6345 *flags |= CPU_DTRACE_DROP; 6346 continue; 6347 } 6348 6349 tomax = buf->dtb_tomax; 6350 ASSERT(tomax != NULL); 6351 6352 if (ecb->dte_size == 0) 6353 continue; 6354 6355 ASSERT3U(ecb->dte_size, >=, 6356 sizeof (dtrace_rechdr_t)); 6357 dtrh = ((void *)(tomax + offs)); 6358 dtrh->dtrh_epid = ecb->dte_epid; 6359 /* 6360 * When the speculation is committed, all of 6361 * the records in the speculative buffer will 6362 * have their timestamps set to the commit 6363 * time. Until then, it is set to a sentinel 6364 * value, for debugability. 6365 */ 6366 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 6367 continue; 6368 } 6369 6370 case DTRACEACT_CHILL: 6371 if (dtrace_priv_kernel_destructive(state)) 6372 dtrace_action_chill(&mstate, val); 6373 continue; 6374 6375 case DTRACEACT_RAISE: 6376 if (dtrace_priv_proc_destructive(state, 6377 &mstate)) 6378 dtrace_action_raise(val); 6379 continue; 6380 6381 case DTRACEACT_COMMIT: 6382 ASSERT(!committed); 6383 6384 /* 6385 * We need to commit our buffer state. 6386 */ 6387 if (ecb->dte_size) 6388 buf->dtb_offset = offs + ecb->dte_size; 6389 buf = &state->dts_buffer[cpuid]; 6390 dtrace_speculation_commit(state, cpuid, val); 6391 committed = 1; 6392 continue; 6393 6394 case DTRACEACT_DISCARD: 6395 dtrace_speculation_discard(state, cpuid, val); 6396 continue; 6397 6398 case DTRACEACT_DIFEXPR: 6399 case DTRACEACT_LIBACT: 6400 case DTRACEACT_PRINTF: 6401 case DTRACEACT_PRINTA: 6402 case DTRACEACT_SYSTEM: 6403 case DTRACEACT_FREOPEN: 6404 case DTRACEACT_TRACEMEM: 6405 break; 6406 6407 case DTRACEACT_TRACEMEM_DYNSIZE: 6408 tracememsize = val; 6409 break; 6410 6411 case DTRACEACT_SYM: 6412 case DTRACEACT_MOD: 6413 if (!dtrace_priv_kernel(state)) 6414 continue; 6415 break; 6416 6417 case DTRACEACT_USYM: 6418 case DTRACEACT_UMOD: 6419 case DTRACEACT_UADDR: { 6420 struct pid *pid = curthread->t_procp->p_pidp; 6421 6422 if (!dtrace_priv_proc(state, &mstate)) 6423 continue; 6424 6425 DTRACE_STORE(uint64_t, tomax, 6426 valoffs, (uint64_t)pid->pid_id); 6427 DTRACE_STORE(uint64_t, tomax, 6428 valoffs + sizeof (uint64_t), val); 6429 6430 continue; 6431 } 6432 6433 case DTRACEACT_EXIT: { 6434 /* 6435 * For the exit action, we are going to attempt 6436 * to atomically set our activity to be 6437 * draining. If this fails (either because 6438 * another CPU has beat us to the exit action, 6439 * or because our current activity is something 6440 * other than ACTIVE or WARMUP), we will 6441 * continue. This assures that the exit action 6442 * can be successfully recorded at most once 6443 * when we're in the ACTIVE state. If we're 6444 * encountering the exit() action while in 6445 * COOLDOWN, however, we want to honor the new 6446 * status code. (We know that we're the only 6447 * thread in COOLDOWN, so there is no race.) 6448 */ 6449 void *activity = &state->dts_activity; 6450 dtrace_activity_t current = state->dts_activity; 6451 6452 if (current == DTRACE_ACTIVITY_COOLDOWN) 6453 break; 6454 6455 if (current != DTRACE_ACTIVITY_WARMUP) 6456 current = DTRACE_ACTIVITY_ACTIVE; 6457 6458 if (dtrace_cas32(activity, current, 6459 DTRACE_ACTIVITY_DRAINING) != current) { 6460 *flags |= CPU_DTRACE_DROP; 6461 continue; 6462 } 6463 6464 break; 6465 } 6466 6467 default: 6468 ASSERT(0); 6469 } 6470 6471 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 6472 uintptr_t end = valoffs + size; 6473 6474 if (tracememsize != 0 && 6475 valoffs + tracememsize < end) { 6476 end = valoffs + tracememsize; 6477 tracememsize = 0; 6478 } 6479 6480 if (!dtrace_vcanload((void *)(uintptr_t)val, 6481 &dp->dtdo_rtype, &mstate, vstate)) 6482 continue; 6483 6484 /* 6485 * If this is a string, we're going to only 6486 * load until we find the zero byte -- after 6487 * which we'll store zero bytes. 6488 */ 6489 if (dp->dtdo_rtype.dtdt_kind == 6490 DIF_TYPE_STRING) { 6491 char c = '\0' + 1; 6492 int intuple = act->dta_intuple; 6493 size_t s; 6494 6495 for (s = 0; s < size; s++) { 6496 if (c != '\0') 6497 c = dtrace_load8(val++); 6498 6499 DTRACE_STORE(uint8_t, tomax, 6500 valoffs++, c); 6501 6502 if (c == '\0' && intuple) 6503 break; 6504 } 6505 6506 continue; 6507 } 6508 6509 while (valoffs < end) { 6510 DTRACE_STORE(uint8_t, tomax, valoffs++, 6511 dtrace_load8(val++)); 6512 } 6513 6514 continue; 6515 } 6516 6517 switch (size) { 6518 case 0: 6519 break; 6520 6521 case sizeof (uint8_t): 6522 DTRACE_STORE(uint8_t, tomax, valoffs, val); 6523 break; 6524 case sizeof (uint16_t): 6525 DTRACE_STORE(uint16_t, tomax, valoffs, val); 6526 break; 6527 case sizeof (uint32_t): 6528 DTRACE_STORE(uint32_t, tomax, valoffs, val); 6529 break; 6530 case sizeof (uint64_t): 6531 DTRACE_STORE(uint64_t, tomax, valoffs, val); 6532 break; 6533 default: 6534 /* 6535 * Any other size should have been returned by 6536 * reference, not by value. 6537 */ 6538 ASSERT(0); 6539 break; 6540 } 6541 } 6542 6543 if (*flags & CPU_DTRACE_DROP) 6544 continue; 6545 6546 if (*flags & CPU_DTRACE_FAULT) { 6547 int ndx; 6548 dtrace_action_t *err; 6549 6550 buf->dtb_errors++; 6551 6552 if (probe->dtpr_id == dtrace_probeid_error) { 6553 /* 6554 * There's nothing we can do -- we had an 6555 * error on the error probe. We bump an 6556 * error counter to at least indicate that 6557 * this condition happened. 6558 */ 6559 dtrace_error(&state->dts_dblerrors); 6560 continue; 6561 } 6562 6563 if (vtime) { 6564 /* 6565 * Before recursing on dtrace_probe(), we 6566 * need to explicitly clear out our start 6567 * time to prevent it from being accumulated 6568 * into t_dtrace_vtime. 6569 */ 6570 curthread->t_dtrace_start = 0; 6571 } 6572 6573 /* 6574 * Iterate over the actions to figure out which action 6575 * we were processing when we experienced the error. 6576 * Note that act points _past_ the faulting action; if 6577 * act is ecb->dte_action, the fault was in the 6578 * predicate, if it's ecb->dte_action->dta_next it's 6579 * in action #1, and so on. 6580 */ 6581 for (err = ecb->dte_action, ndx = 0; 6582 err != act; err = err->dta_next, ndx++) 6583 continue; 6584 6585 dtrace_probe_error(state, ecb->dte_epid, ndx, 6586 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 6587 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 6588 cpu_core[cpuid].cpuc_dtrace_illval); 6589 6590 continue; 6591 } 6592 6593 if (!committed) 6594 buf->dtb_offset = offs + ecb->dte_size; 6595 } 6596 6597 if (vtime) 6598 curthread->t_dtrace_start = dtrace_gethrtime(); 6599 6600 dtrace_interrupt_enable(cookie); 6601 } 6602 6603 /* 6604 * DTrace Probe Hashing Functions 6605 * 6606 * The functions in this section (and indeed, the functions in remaining 6607 * sections) are not _called_ from probe context. (Any exceptions to this are 6608 * marked with a "Note:".) Rather, they are called from elsewhere in the 6609 * DTrace framework to look-up probes in, add probes to and remove probes from 6610 * the DTrace probe hashes. (Each probe is hashed by each element of the 6611 * probe tuple -- allowing for fast lookups, regardless of what was 6612 * specified.) 6613 */ 6614 static uint_t 6615 dtrace_hash_str(char *p) 6616 { 6617 unsigned int g; 6618 uint_t hval = 0; 6619 6620 while (*p) { 6621 hval = (hval << 4) + *p++; 6622 if ((g = (hval & 0xf0000000)) != 0) 6623 hval ^= g >> 24; 6624 hval &= ~g; 6625 } 6626 return (hval); 6627 } 6628 6629 static dtrace_hash_t * 6630 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 6631 { 6632 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 6633 6634 hash->dth_stroffs = stroffs; 6635 hash->dth_nextoffs = nextoffs; 6636 hash->dth_prevoffs = prevoffs; 6637 6638 hash->dth_size = 1; 6639 hash->dth_mask = hash->dth_size - 1; 6640 6641 hash->dth_tab = kmem_zalloc(hash->dth_size * 6642 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 6643 6644 return (hash); 6645 } 6646 6647 static void 6648 dtrace_hash_destroy(dtrace_hash_t *hash) 6649 { 6650 #ifdef DEBUG 6651 int i; 6652 6653 for (i = 0; i < hash->dth_size; i++) 6654 ASSERT(hash->dth_tab[i] == NULL); 6655 #endif 6656 6657 kmem_free(hash->dth_tab, 6658 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 6659 kmem_free(hash, sizeof (dtrace_hash_t)); 6660 } 6661 6662 static void 6663 dtrace_hash_resize(dtrace_hash_t *hash) 6664 { 6665 int size = hash->dth_size, i, ndx; 6666 int new_size = hash->dth_size << 1; 6667 int new_mask = new_size - 1; 6668 dtrace_hashbucket_t **new_tab, *bucket, *next; 6669 6670 ASSERT((new_size & new_mask) == 0); 6671 6672 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 6673 6674 for (i = 0; i < size; i++) { 6675 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 6676 dtrace_probe_t *probe = bucket->dthb_chain; 6677 6678 ASSERT(probe != NULL); 6679 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 6680 6681 next = bucket->dthb_next; 6682 bucket->dthb_next = new_tab[ndx]; 6683 new_tab[ndx] = bucket; 6684 } 6685 } 6686 6687 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 6688 hash->dth_tab = new_tab; 6689 hash->dth_size = new_size; 6690 hash->dth_mask = new_mask; 6691 } 6692 6693 static void 6694 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 6695 { 6696 int hashval = DTRACE_HASHSTR(hash, new); 6697 int ndx = hashval & hash->dth_mask; 6698 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6699 dtrace_probe_t **nextp, **prevp; 6700 6701 for (; bucket != NULL; bucket = bucket->dthb_next) { 6702 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 6703 goto add; 6704 } 6705 6706 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 6707 dtrace_hash_resize(hash); 6708 dtrace_hash_add(hash, new); 6709 return; 6710 } 6711 6712 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 6713 bucket->dthb_next = hash->dth_tab[ndx]; 6714 hash->dth_tab[ndx] = bucket; 6715 hash->dth_nbuckets++; 6716 6717 add: 6718 nextp = DTRACE_HASHNEXT(hash, new); 6719 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 6720 *nextp = bucket->dthb_chain; 6721 6722 if (bucket->dthb_chain != NULL) { 6723 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 6724 ASSERT(*prevp == NULL); 6725 *prevp = new; 6726 } 6727 6728 bucket->dthb_chain = new; 6729 bucket->dthb_len++; 6730 } 6731 6732 static dtrace_probe_t * 6733 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 6734 { 6735 int hashval = DTRACE_HASHSTR(hash, template); 6736 int ndx = hashval & hash->dth_mask; 6737 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6738 6739 for (; bucket != NULL; bucket = bucket->dthb_next) { 6740 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6741 return (bucket->dthb_chain); 6742 } 6743 6744 return (NULL); 6745 } 6746 6747 static int 6748 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 6749 { 6750 int hashval = DTRACE_HASHSTR(hash, template); 6751 int ndx = hashval & hash->dth_mask; 6752 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6753 6754 for (; bucket != NULL; bucket = bucket->dthb_next) { 6755 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6756 return (bucket->dthb_len); 6757 } 6758 6759 return (NULL); 6760 } 6761 6762 static void 6763 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 6764 { 6765 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 6766 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6767 6768 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 6769 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 6770 6771 /* 6772 * Find the bucket that we're removing this probe from. 6773 */ 6774 for (; bucket != NULL; bucket = bucket->dthb_next) { 6775 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 6776 break; 6777 } 6778 6779 ASSERT(bucket != NULL); 6780 6781 if (*prevp == NULL) { 6782 if (*nextp == NULL) { 6783 /* 6784 * The removed probe was the only probe on this 6785 * bucket; we need to remove the bucket. 6786 */ 6787 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 6788 6789 ASSERT(bucket->dthb_chain == probe); 6790 ASSERT(b != NULL); 6791 6792 if (b == bucket) { 6793 hash->dth_tab[ndx] = bucket->dthb_next; 6794 } else { 6795 while (b->dthb_next != bucket) 6796 b = b->dthb_next; 6797 b->dthb_next = bucket->dthb_next; 6798 } 6799 6800 ASSERT(hash->dth_nbuckets > 0); 6801 hash->dth_nbuckets--; 6802 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 6803 return; 6804 } 6805 6806 bucket->dthb_chain = *nextp; 6807 } else { 6808 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 6809 } 6810 6811 if (*nextp != NULL) 6812 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 6813 } 6814 6815 /* 6816 * DTrace Utility Functions 6817 * 6818 * These are random utility functions that are _not_ called from probe context. 6819 */ 6820 static int 6821 dtrace_badattr(const dtrace_attribute_t *a) 6822 { 6823 return (a->dtat_name > DTRACE_STABILITY_MAX || 6824 a->dtat_data > DTRACE_STABILITY_MAX || 6825 a->dtat_class > DTRACE_CLASS_MAX); 6826 } 6827 6828 /* 6829 * Return a duplicate copy of a string. If the specified string is NULL, 6830 * this function returns a zero-length string. 6831 */ 6832 static char * 6833 dtrace_strdup(const char *str) 6834 { 6835 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 6836 6837 if (str != NULL) 6838 (void) strcpy(new, str); 6839 6840 return (new); 6841 } 6842 6843 #define DTRACE_ISALPHA(c) \ 6844 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 6845 6846 static int 6847 dtrace_badname(const char *s) 6848 { 6849 char c; 6850 6851 if (s == NULL || (c = *s++) == '\0') 6852 return (0); 6853 6854 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 6855 return (1); 6856 6857 while ((c = *s++) != '\0') { 6858 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 6859 c != '-' && c != '_' && c != '.' && c != '`') 6860 return (1); 6861 } 6862 6863 return (0); 6864 } 6865 6866 static void 6867 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 6868 { 6869 uint32_t priv; 6870 6871 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 6872 /* 6873 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 6874 */ 6875 priv = DTRACE_PRIV_ALL; 6876 } else { 6877 *uidp = crgetuid(cr); 6878 *zoneidp = crgetzonedid(cr); 6879 6880 priv = 0; 6881 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 6882 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 6883 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 6884 priv |= DTRACE_PRIV_USER; 6885 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 6886 priv |= DTRACE_PRIV_PROC; 6887 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 6888 priv |= DTRACE_PRIV_OWNER; 6889 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 6890 priv |= DTRACE_PRIV_ZONEOWNER; 6891 } 6892 6893 *privp = priv; 6894 } 6895 6896 #ifdef DTRACE_ERRDEBUG 6897 static void 6898 dtrace_errdebug(const char *str) 6899 { 6900 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; 6901 int occupied = 0; 6902 6903 mutex_enter(&dtrace_errlock); 6904 dtrace_errlast = str; 6905 dtrace_errthread = curthread; 6906 6907 while (occupied++ < DTRACE_ERRHASHSZ) { 6908 if (dtrace_errhash[hval].dter_msg == str) { 6909 dtrace_errhash[hval].dter_count++; 6910 goto out; 6911 } 6912 6913 if (dtrace_errhash[hval].dter_msg != NULL) { 6914 hval = (hval + 1) % DTRACE_ERRHASHSZ; 6915 continue; 6916 } 6917 6918 dtrace_errhash[hval].dter_msg = str; 6919 dtrace_errhash[hval].dter_count = 1; 6920 goto out; 6921 } 6922 6923 panic("dtrace: undersized error hash"); 6924 out: 6925 mutex_exit(&dtrace_errlock); 6926 } 6927 #endif 6928 6929 /* 6930 * DTrace Matching Functions 6931 * 6932 * These functions are used to match groups of probes, given some elements of 6933 * a probe tuple, or some globbed expressions for elements of a probe tuple. 6934 */ 6935 static int 6936 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 6937 zoneid_t zoneid) 6938 { 6939 if (priv != DTRACE_PRIV_ALL) { 6940 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 6941 uint32_t match = priv & ppriv; 6942 6943 /* 6944 * No PRIV_DTRACE_* privileges... 6945 */ 6946 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 6947 DTRACE_PRIV_KERNEL)) == 0) 6948 return (0); 6949 6950 /* 6951 * No matching bits, but there were bits to match... 6952 */ 6953 if (match == 0 && ppriv != 0) 6954 return (0); 6955 6956 /* 6957 * Need to have permissions to the process, but don't... 6958 */ 6959 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 6960 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 6961 return (0); 6962 } 6963 6964 /* 6965 * Need to be in the same zone unless we possess the 6966 * privilege to examine all zones. 6967 */ 6968 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 6969 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 6970 return (0); 6971 } 6972 } 6973 6974 return (1); 6975 } 6976 6977 /* 6978 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 6979 * consists of input pattern strings and an ops-vector to evaluate them. 6980 * This function returns >0 for match, 0 for no match, and <0 for error. 6981 */ 6982 static int 6983 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 6984 uint32_t priv, uid_t uid, zoneid_t zoneid) 6985 { 6986 dtrace_provider_t *pvp = prp->dtpr_provider; 6987 int rv; 6988 6989 if (pvp->dtpv_defunct) 6990 return (0); 6991 6992 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 6993 return (rv); 6994 6995 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 6996 return (rv); 6997 6998 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 6999 return (rv); 7000 7001 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 7002 return (rv); 7003 7004 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 7005 return (0); 7006 7007 return (rv); 7008 } 7009 7010 /* 7011 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 7012 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 7013 * libc's version, the kernel version only applies to 8-bit ASCII strings. 7014 * In addition, all of the recursion cases except for '*' matching have been 7015 * unwound. For '*', we still implement recursive evaluation, but a depth 7016 * counter is maintained and matching is aborted if we recurse too deep. 7017 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 7018 */ 7019 static int 7020 dtrace_match_glob(const char *s, const char *p, int depth) 7021 { 7022 const char *olds; 7023 char s1, c; 7024 int gs; 7025 7026 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 7027 return (-1); 7028 7029 if (s == NULL) 7030 s = ""; /* treat NULL as empty string */ 7031 7032 top: 7033 olds = s; 7034 s1 = *s++; 7035 7036 if (p == NULL) 7037 return (0); 7038 7039 if ((c = *p++) == '\0') 7040 return (s1 == '\0'); 7041 7042 switch (c) { 7043 case '[': { 7044 int ok = 0, notflag = 0; 7045 char lc = '\0'; 7046 7047 if (s1 == '\0') 7048 return (0); 7049 7050 if (*p == '!') { 7051 notflag = 1; 7052 p++; 7053 } 7054 7055 if ((c = *p++) == '\0') 7056 return (0); 7057 7058 do { 7059 if (c == '-' && lc != '\0' && *p != ']') { 7060 if ((c = *p++) == '\0') 7061 return (0); 7062 if (c == '\\' && (c = *p++) == '\0') 7063 return (0); 7064 7065 if (notflag) { 7066 if (s1 < lc || s1 > c) 7067 ok++; 7068 else 7069 return (0); 7070 } else if (lc <= s1 && s1 <= c) 7071 ok++; 7072 7073 } else if (c == '\\' && (c = *p++) == '\0') 7074 return (0); 7075 7076 lc = c; /* save left-hand 'c' for next iteration */ 7077 7078 if (notflag) { 7079 if (s1 != c) 7080 ok++; 7081 else 7082 return (0); 7083 } else if (s1 == c) 7084 ok++; 7085 7086 if ((c = *p++) == '\0') 7087 return (0); 7088 7089 } while (c != ']'); 7090 7091 if (ok) 7092 goto top; 7093 7094 return (0); 7095 } 7096 7097 case '\\': 7098 if ((c = *p++) == '\0') 7099 return (0); 7100 /*FALLTHRU*/ 7101 7102 default: 7103 if (c != s1) 7104 return (0); 7105 /*FALLTHRU*/ 7106 7107 case '?': 7108 if (s1 != '\0') 7109 goto top; 7110 return (0); 7111 7112 case '*': 7113 while (*p == '*') 7114 p++; /* consecutive *'s are identical to a single one */ 7115 7116 if (*p == '\0') 7117 return (1); 7118 7119 for (s = olds; *s != '\0'; s++) { 7120 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 7121 return (gs); 7122 } 7123 7124 return (0); 7125 } 7126 } 7127 7128 /*ARGSUSED*/ 7129 static int 7130 dtrace_match_string(const char *s, const char *p, int depth) 7131 { 7132 return (s != NULL && strcmp(s, p) == 0); 7133 } 7134 7135 /*ARGSUSED*/ 7136 static int 7137 dtrace_match_nul(const char *s, const char *p, int depth) 7138 { 7139 return (1); /* always match the empty pattern */ 7140 } 7141 7142 /*ARGSUSED*/ 7143 static int 7144 dtrace_match_nonzero(const char *s, const char *p, int depth) 7145 { 7146 return (s != NULL && s[0] != '\0'); 7147 } 7148 7149 static int 7150 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 7151 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 7152 { 7153 dtrace_probe_t template, *probe; 7154 dtrace_hash_t *hash = NULL; 7155 int len, rc, best = INT_MAX, nmatched = 0; 7156 dtrace_id_t i; 7157 7158 ASSERT(MUTEX_HELD(&dtrace_lock)); 7159 7160 /* 7161 * If the probe ID is specified in the key, just lookup by ID and 7162 * invoke the match callback once if a matching probe is found. 7163 */ 7164 if (pkp->dtpk_id != DTRACE_IDNONE) { 7165 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 7166 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 7167 if ((*matched)(probe, arg) == DTRACE_MATCH_FAIL) 7168 return (DTRACE_MATCH_FAIL); 7169 nmatched++; 7170 } 7171 return (nmatched); 7172 } 7173 7174 template.dtpr_mod = (char *)pkp->dtpk_mod; 7175 template.dtpr_func = (char *)pkp->dtpk_func; 7176 template.dtpr_name = (char *)pkp->dtpk_name; 7177 7178 /* 7179 * We want to find the most distinct of the module name, function 7180 * name, and name. So for each one that is not a glob pattern or 7181 * empty string, we perform a lookup in the corresponding hash and 7182 * use the hash table with the fewest collisions to do our search. 7183 */ 7184 if (pkp->dtpk_mmatch == &dtrace_match_string && 7185 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 7186 best = len; 7187 hash = dtrace_bymod; 7188 } 7189 7190 if (pkp->dtpk_fmatch == &dtrace_match_string && 7191 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 7192 best = len; 7193 hash = dtrace_byfunc; 7194 } 7195 7196 if (pkp->dtpk_nmatch == &dtrace_match_string && 7197 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 7198 best = len; 7199 hash = dtrace_byname; 7200 } 7201 7202 /* 7203 * If we did not select a hash table, iterate over every probe and 7204 * invoke our callback for each one that matches our input probe key. 7205 */ 7206 if (hash == NULL) { 7207 for (i = 0; i < dtrace_nprobes; i++) { 7208 if ((probe = dtrace_probes[i]) == NULL || 7209 dtrace_match_probe(probe, pkp, priv, uid, 7210 zoneid) <= 0) 7211 continue; 7212 7213 nmatched++; 7214 7215 if ((rc = (*matched)(probe, arg)) != 7216 DTRACE_MATCH_NEXT) { 7217 if (rc == DTRACE_MATCH_FAIL) 7218 return (DTRACE_MATCH_FAIL); 7219 break; 7220 } 7221 } 7222 7223 return (nmatched); 7224 } 7225 7226 /* 7227 * If we selected a hash table, iterate over each probe of the same key 7228 * name and invoke the callback for every probe that matches the other 7229 * attributes of our input probe key. 7230 */ 7231 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 7232 probe = *(DTRACE_HASHNEXT(hash, probe))) { 7233 7234 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 7235 continue; 7236 7237 nmatched++; 7238 7239 if ((rc = (*matched)(probe, arg)) != DTRACE_MATCH_NEXT) { 7240 if (rc == DTRACE_MATCH_FAIL) 7241 return (DTRACE_MATCH_FAIL); 7242 break; 7243 } 7244 } 7245 7246 return (nmatched); 7247 } 7248 7249 /* 7250 * Return the function pointer dtrace_probecmp() should use to compare the 7251 * specified pattern with a string. For NULL or empty patterns, we select 7252 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 7253 * For non-empty non-glob strings, we use dtrace_match_string(). 7254 */ 7255 static dtrace_probekey_f * 7256 dtrace_probekey_func(const char *p) 7257 { 7258 char c; 7259 7260 if (p == NULL || *p == '\0') 7261 return (&dtrace_match_nul); 7262 7263 while ((c = *p++) != '\0') { 7264 if (c == '[' || c == '?' || c == '*' || c == '\\') 7265 return (&dtrace_match_glob); 7266 } 7267 7268 return (&dtrace_match_string); 7269 } 7270 7271 /* 7272 * Build a probe comparison key for use with dtrace_match_probe() from the 7273 * given probe description. By convention, a null key only matches anchored 7274 * probes: if each field is the empty string, reset dtpk_fmatch to 7275 * dtrace_match_nonzero(). 7276 */ 7277 static void 7278 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 7279 { 7280 pkp->dtpk_prov = pdp->dtpd_provider; 7281 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 7282 7283 pkp->dtpk_mod = pdp->dtpd_mod; 7284 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 7285 7286 pkp->dtpk_func = pdp->dtpd_func; 7287 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 7288 7289 pkp->dtpk_name = pdp->dtpd_name; 7290 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 7291 7292 pkp->dtpk_id = pdp->dtpd_id; 7293 7294 if (pkp->dtpk_id == DTRACE_IDNONE && 7295 pkp->dtpk_pmatch == &dtrace_match_nul && 7296 pkp->dtpk_mmatch == &dtrace_match_nul && 7297 pkp->dtpk_fmatch == &dtrace_match_nul && 7298 pkp->dtpk_nmatch == &dtrace_match_nul) 7299 pkp->dtpk_fmatch = &dtrace_match_nonzero; 7300 } 7301 7302 /* 7303 * DTrace Provider-to-Framework API Functions 7304 * 7305 * These functions implement much of the Provider-to-Framework API, as 7306 * described in <sys/dtrace.h>. The parts of the API not in this section are 7307 * the functions in the API for probe management (found below), and 7308 * dtrace_probe() itself (found above). 7309 */ 7310 7311 /* 7312 * Register the calling provider with the DTrace framework. This should 7313 * generally be called by DTrace providers in their attach(9E) entry point. 7314 */ 7315 int 7316 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 7317 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 7318 { 7319 dtrace_provider_t *provider; 7320 7321 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 7322 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7323 "arguments", name ? name : "<NULL>"); 7324 return (EINVAL); 7325 } 7326 7327 if (name[0] == '\0' || dtrace_badname(name)) { 7328 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7329 "provider name", name); 7330 return (EINVAL); 7331 } 7332 7333 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 7334 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 7335 pops->dtps_destroy == NULL || 7336 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 7337 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7338 "provider ops", name); 7339 return (EINVAL); 7340 } 7341 7342 if (dtrace_badattr(&pap->dtpa_provider) || 7343 dtrace_badattr(&pap->dtpa_mod) || 7344 dtrace_badattr(&pap->dtpa_func) || 7345 dtrace_badattr(&pap->dtpa_name) || 7346 dtrace_badattr(&pap->dtpa_args)) { 7347 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7348 "provider attributes", name); 7349 return (EINVAL); 7350 } 7351 7352 if (priv & ~DTRACE_PRIV_ALL) { 7353 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7354 "privilege attributes", name); 7355 return (EINVAL); 7356 } 7357 7358 if ((priv & DTRACE_PRIV_KERNEL) && 7359 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 7360 pops->dtps_mode == NULL) { 7361 cmn_err(CE_WARN, "failed to register provider '%s': need " 7362 "dtps_mode() op for given privilege attributes", name); 7363 return (EINVAL); 7364 } 7365 7366 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 7367 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7368 (void) strcpy(provider->dtpv_name, name); 7369 7370 provider->dtpv_attr = *pap; 7371 provider->dtpv_priv.dtpp_flags = priv; 7372 if (cr != NULL) { 7373 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 7374 provider->dtpv_priv.dtpp_zoneid = crgetzonedid(cr); 7375 } 7376 provider->dtpv_pops = *pops; 7377 7378 if (pops->dtps_provide == NULL) { 7379 ASSERT(pops->dtps_provide_module != NULL); 7380 provider->dtpv_pops.dtps_provide = 7381 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 7382 } 7383 7384 if (pops->dtps_provide_module == NULL) { 7385 ASSERT(pops->dtps_provide != NULL); 7386 provider->dtpv_pops.dtps_provide_module = 7387 (void (*)(void *, struct modctl *))dtrace_nullop; 7388 } 7389 7390 if (pops->dtps_suspend == NULL) { 7391 ASSERT(pops->dtps_resume == NULL); 7392 provider->dtpv_pops.dtps_suspend = 7393 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7394 provider->dtpv_pops.dtps_resume = 7395 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7396 } 7397 7398 provider->dtpv_arg = arg; 7399 *idp = (dtrace_provider_id_t)provider; 7400 7401 if (pops == &dtrace_provider_ops) { 7402 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7403 ASSERT(MUTEX_HELD(&dtrace_lock)); 7404 ASSERT(dtrace_anon.dta_enabling == NULL); 7405 7406 /* 7407 * We make sure that the DTrace provider is at the head of 7408 * the provider chain. 7409 */ 7410 provider->dtpv_next = dtrace_provider; 7411 dtrace_provider = provider; 7412 return (0); 7413 } 7414 7415 mutex_enter(&dtrace_provider_lock); 7416 mutex_enter(&dtrace_lock); 7417 7418 /* 7419 * If there is at least one provider registered, we'll add this 7420 * provider after the first provider. 7421 */ 7422 if (dtrace_provider != NULL) { 7423 provider->dtpv_next = dtrace_provider->dtpv_next; 7424 dtrace_provider->dtpv_next = provider; 7425 } else { 7426 dtrace_provider = provider; 7427 } 7428 7429 if (dtrace_retained != NULL) { 7430 dtrace_enabling_provide(provider); 7431 7432 /* 7433 * Now we need to call dtrace_enabling_matchall() -- which 7434 * will acquire cpu_lock and dtrace_lock. We therefore need 7435 * to drop all of our locks before calling into it... 7436 */ 7437 mutex_exit(&dtrace_lock); 7438 mutex_exit(&dtrace_provider_lock); 7439 dtrace_enabling_matchall(); 7440 7441 return (0); 7442 } 7443 7444 mutex_exit(&dtrace_lock); 7445 mutex_exit(&dtrace_provider_lock); 7446 7447 return (0); 7448 } 7449 7450 /* 7451 * Unregister the specified provider from the DTrace framework. This should 7452 * generally be called by DTrace providers in their detach(9E) entry point. 7453 */ 7454 int 7455 dtrace_unregister(dtrace_provider_id_t id) 7456 { 7457 dtrace_provider_t *old = (dtrace_provider_t *)id; 7458 dtrace_provider_t *prev = NULL; 7459 int i, self = 0, noreap = 0; 7460 dtrace_probe_t *probe, *first = NULL; 7461 7462 if (old->dtpv_pops.dtps_enable == 7463 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop) { 7464 /* 7465 * If DTrace itself is the provider, we're called with locks 7466 * already held. 7467 */ 7468 ASSERT(old == dtrace_provider); 7469 ASSERT(dtrace_devi != NULL); 7470 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7471 ASSERT(MUTEX_HELD(&dtrace_lock)); 7472 self = 1; 7473 7474 if (dtrace_provider->dtpv_next != NULL) { 7475 /* 7476 * There's another provider here; return failure. 7477 */ 7478 return (EBUSY); 7479 } 7480 } else { 7481 mutex_enter(&dtrace_provider_lock); 7482 mutex_enter(&mod_lock); 7483 mutex_enter(&dtrace_lock); 7484 } 7485 7486 /* 7487 * If anyone has /dev/dtrace open, or if there are anonymous enabled 7488 * probes, we refuse to let providers slither away, unless this 7489 * provider has already been explicitly invalidated. 7490 */ 7491 if (!old->dtpv_defunct && 7492 (dtrace_opens || (dtrace_anon.dta_state != NULL && 7493 dtrace_anon.dta_state->dts_necbs > 0))) { 7494 if (!self) { 7495 mutex_exit(&dtrace_lock); 7496 mutex_exit(&mod_lock); 7497 mutex_exit(&dtrace_provider_lock); 7498 } 7499 return (EBUSY); 7500 } 7501 7502 /* 7503 * Attempt to destroy the probes associated with this provider. 7504 */ 7505 for (i = 0; i < dtrace_nprobes; i++) { 7506 if ((probe = dtrace_probes[i]) == NULL) 7507 continue; 7508 7509 if (probe->dtpr_provider != old) 7510 continue; 7511 7512 if (probe->dtpr_ecb == NULL) 7513 continue; 7514 7515 /* 7516 * If we are trying to unregister a defunct provider, and the 7517 * provider was made defunct within the interval dictated by 7518 * dtrace_unregister_defunct_reap, we'll (asynchronously) 7519 * attempt to reap our enablings. To denote that the provider 7520 * should reattempt to unregister itself at some point in the 7521 * future, we will return a differentiable error code (EAGAIN 7522 * instead of EBUSY) in this case. 7523 */ 7524 if (dtrace_gethrtime() - old->dtpv_defunct > 7525 dtrace_unregister_defunct_reap) 7526 noreap = 1; 7527 7528 if (!self) { 7529 mutex_exit(&dtrace_lock); 7530 mutex_exit(&mod_lock); 7531 mutex_exit(&dtrace_provider_lock); 7532 } 7533 7534 if (noreap) 7535 return (EBUSY); 7536 7537 (void) taskq_dispatch(dtrace_taskq, 7538 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 7539 7540 return (EAGAIN); 7541 } 7542 7543 /* 7544 * All of the probes for this provider are disabled; we can safely 7545 * remove all of them from their hash chains and from the probe array. 7546 */ 7547 for (i = 0; i < dtrace_nprobes; i++) { 7548 if ((probe = dtrace_probes[i]) == NULL) 7549 continue; 7550 7551 if (probe->dtpr_provider != old) 7552 continue; 7553 7554 dtrace_probes[i] = NULL; 7555 7556 dtrace_hash_remove(dtrace_bymod, probe); 7557 dtrace_hash_remove(dtrace_byfunc, probe); 7558 dtrace_hash_remove(dtrace_byname, probe); 7559 7560 if (first == NULL) { 7561 first = probe; 7562 probe->dtpr_nextmod = NULL; 7563 } else { 7564 probe->dtpr_nextmod = first; 7565 first = probe; 7566 } 7567 } 7568 7569 /* 7570 * The provider's probes have been removed from the hash chains and 7571 * from the probe array. Now issue a dtrace_sync() to be sure that 7572 * everyone has cleared out from any probe array processing. 7573 */ 7574 dtrace_sync(); 7575 7576 for (probe = first; probe != NULL; probe = first) { 7577 first = probe->dtpr_nextmod; 7578 7579 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 7580 probe->dtpr_arg); 7581 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7582 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7583 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7584 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 7585 kmem_free(probe, sizeof (dtrace_probe_t)); 7586 } 7587 7588 if ((prev = dtrace_provider) == old) { 7589 ASSERT(self || dtrace_devi == NULL); 7590 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 7591 dtrace_provider = old->dtpv_next; 7592 } else { 7593 while (prev != NULL && prev->dtpv_next != old) 7594 prev = prev->dtpv_next; 7595 7596 if (prev == NULL) { 7597 panic("attempt to unregister non-existent " 7598 "dtrace provider %p\n", (void *)id); 7599 } 7600 7601 prev->dtpv_next = old->dtpv_next; 7602 } 7603 7604 if (!self) { 7605 mutex_exit(&dtrace_lock); 7606 mutex_exit(&mod_lock); 7607 mutex_exit(&dtrace_provider_lock); 7608 } 7609 7610 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 7611 kmem_free(old, sizeof (dtrace_provider_t)); 7612 7613 return (0); 7614 } 7615 7616 /* 7617 * Invalidate the specified provider. All subsequent probe lookups for the 7618 * specified provider will fail, but its probes will not be removed. 7619 */ 7620 void 7621 dtrace_invalidate(dtrace_provider_id_t id) 7622 { 7623 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 7624 7625 ASSERT(pvp->dtpv_pops.dtps_enable != 7626 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7627 7628 mutex_enter(&dtrace_provider_lock); 7629 mutex_enter(&dtrace_lock); 7630 7631 pvp->dtpv_defunct = dtrace_gethrtime(); 7632 7633 mutex_exit(&dtrace_lock); 7634 mutex_exit(&dtrace_provider_lock); 7635 } 7636 7637 /* 7638 * Indicate whether or not DTrace has attached. 7639 */ 7640 int 7641 dtrace_attached(void) 7642 { 7643 /* 7644 * dtrace_provider will be non-NULL iff the DTrace driver has 7645 * attached. (It's non-NULL because DTrace is always itself a 7646 * provider.) 7647 */ 7648 return (dtrace_provider != NULL); 7649 } 7650 7651 /* 7652 * Remove all the unenabled probes for the given provider. This function is 7653 * not unlike dtrace_unregister(), except that it doesn't remove the provider 7654 * -- just as many of its associated probes as it can. 7655 */ 7656 int 7657 dtrace_condense(dtrace_provider_id_t id) 7658 { 7659 dtrace_provider_t *prov = (dtrace_provider_t *)id; 7660 int i; 7661 dtrace_probe_t *probe; 7662 7663 /* 7664 * Make sure this isn't the dtrace provider itself. 7665 */ 7666 ASSERT(prov->dtpv_pops.dtps_enable != 7667 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7668 7669 mutex_enter(&dtrace_provider_lock); 7670 mutex_enter(&dtrace_lock); 7671 7672 /* 7673 * Attempt to destroy the probes associated with this provider. 7674 */ 7675 for (i = 0; i < dtrace_nprobes; i++) { 7676 if ((probe = dtrace_probes[i]) == NULL) 7677 continue; 7678 7679 if (probe->dtpr_provider != prov) 7680 continue; 7681 7682 if (probe->dtpr_ecb != NULL) 7683 continue; 7684 7685 dtrace_probes[i] = NULL; 7686 7687 dtrace_hash_remove(dtrace_bymod, probe); 7688 dtrace_hash_remove(dtrace_byfunc, probe); 7689 dtrace_hash_remove(dtrace_byname, probe); 7690 7691 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 7692 probe->dtpr_arg); 7693 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7694 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7695 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7696 kmem_free(probe, sizeof (dtrace_probe_t)); 7697 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 7698 } 7699 7700 mutex_exit(&dtrace_lock); 7701 mutex_exit(&dtrace_provider_lock); 7702 7703 return (0); 7704 } 7705 7706 /* 7707 * DTrace Probe Management Functions 7708 * 7709 * The functions in this section perform the DTrace probe management, 7710 * including functions to create probes, look-up probes, and call into the 7711 * providers to request that probes be provided. Some of these functions are 7712 * in the Provider-to-Framework API; these functions can be identified by the 7713 * fact that they are not declared "static". 7714 */ 7715 7716 /* 7717 * Create a probe with the specified module name, function name, and name. 7718 */ 7719 dtrace_id_t 7720 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 7721 const char *func, const char *name, int aframes, void *arg) 7722 { 7723 dtrace_probe_t *probe, **probes; 7724 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 7725 dtrace_id_t id; 7726 7727 if (provider == dtrace_provider) { 7728 ASSERT(MUTEX_HELD(&dtrace_lock)); 7729 } else { 7730 mutex_enter(&dtrace_lock); 7731 } 7732 7733 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 7734 VM_BESTFIT | VM_SLEEP); 7735 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 7736 7737 probe->dtpr_id = id; 7738 probe->dtpr_gen = dtrace_probegen++; 7739 probe->dtpr_mod = dtrace_strdup(mod); 7740 probe->dtpr_func = dtrace_strdup(func); 7741 probe->dtpr_name = dtrace_strdup(name); 7742 probe->dtpr_arg = arg; 7743 probe->dtpr_aframes = aframes; 7744 probe->dtpr_provider = provider; 7745 7746 dtrace_hash_add(dtrace_bymod, probe); 7747 dtrace_hash_add(dtrace_byfunc, probe); 7748 dtrace_hash_add(dtrace_byname, probe); 7749 7750 if (id - 1 >= dtrace_nprobes) { 7751 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 7752 size_t nsize = osize << 1; 7753 7754 if (nsize == 0) { 7755 ASSERT(osize == 0); 7756 ASSERT(dtrace_probes == NULL); 7757 nsize = sizeof (dtrace_probe_t *); 7758 } 7759 7760 probes = kmem_zalloc(nsize, KM_SLEEP); 7761 7762 if (dtrace_probes == NULL) { 7763 ASSERT(osize == 0); 7764 dtrace_probes = probes; 7765 dtrace_nprobes = 1; 7766 } else { 7767 dtrace_probe_t **oprobes = dtrace_probes; 7768 7769 bcopy(oprobes, probes, osize); 7770 dtrace_membar_producer(); 7771 dtrace_probes = probes; 7772 7773 dtrace_sync(); 7774 7775 /* 7776 * All CPUs are now seeing the new probes array; we can 7777 * safely free the old array. 7778 */ 7779 kmem_free(oprobes, osize); 7780 dtrace_nprobes <<= 1; 7781 } 7782 7783 ASSERT(id - 1 < dtrace_nprobes); 7784 } 7785 7786 ASSERT(dtrace_probes[id - 1] == NULL); 7787 dtrace_probes[id - 1] = probe; 7788 7789 if (provider != dtrace_provider) 7790 mutex_exit(&dtrace_lock); 7791 7792 return (id); 7793 } 7794 7795 static dtrace_probe_t * 7796 dtrace_probe_lookup_id(dtrace_id_t id) 7797 { 7798 ASSERT(MUTEX_HELD(&dtrace_lock)); 7799 7800 if (id == 0 || id > dtrace_nprobes) 7801 return (NULL); 7802 7803 return (dtrace_probes[id - 1]); 7804 } 7805 7806 static int 7807 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 7808 { 7809 *((dtrace_id_t *)arg) = probe->dtpr_id; 7810 7811 return (DTRACE_MATCH_DONE); 7812 } 7813 7814 /* 7815 * Look up a probe based on provider and one or more of module name, function 7816 * name and probe name. 7817 */ 7818 dtrace_id_t 7819 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, 7820 const char *func, const char *name) 7821 { 7822 dtrace_probekey_t pkey; 7823 dtrace_id_t id; 7824 int match; 7825 7826 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 7827 pkey.dtpk_pmatch = &dtrace_match_string; 7828 pkey.dtpk_mod = mod; 7829 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 7830 pkey.dtpk_func = func; 7831 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 7832 pkey.dtpk_name = name; 7833 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 7834 pkey.dtpk_id = DTRACE_IDNONE; 7835 7836 mutex_enter(&dtrace_lock); 7837 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 7838 dtrace_probe_lookup_match, &id); 7839 mutex_exit(&dtrace_lock); 7840 7841 ASSERT(match == 1 || match == 0); 7842 return (match ? id : 0); 7843 } 7844 7845 /* 7846 * Returns the probe argument associated with the specified probe. 7847 */ 7848 void * 7849 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 7850 { 7851 dtrace_probe_t *probe; 7852 void *rval = NULL; 7853 7854 mutex_enter(&dtrace_lock); 7855 7856 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 7857 probe->dtpr_provider == (dtrace_provider_t *)id) 7858 rval = probe->dtpr_arg; 7859 7860 mutex_exit(&dtrace_lock); 7861 7862 return (rval); 7863 } 7864 7865 /* 7866 * Copy a probe into a probe description. 7867 */ 7868 static void 7869 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 7870 { 7871 bzero(pdp, sizeof (dtrace_probedesc_t)); 7872 pdp->dtpd_id = prp->dtpr_id; 7873 7874 (void) strncpy(pdp->dtpd_provider, 7875 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 7876 7877 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 7878 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 7879 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 7880 } 7881 7882 /* 7883 * Called to indicate that a probe -- or probes -- should be provided by a 7884 * specfied provider. If the specified description is NULL, the provider will 7885 * be told to provide all of its probes. (This is done whenever a new 7886 * consumer comes along, or whenever a retained enabling is to be matched.) If 7887 * the specified description is non-NULL, the provider is given the 7888 * opportunity to dynamically provide the specified probe, allowing providers 7889 * to support the creation of probes on-the-fly. (So-called _autocreated_ 7890 * probes.) If the provider is NULL, the operations will be applied to all 7891 * providers; if the provider is non-NULL the operations will only be applied 7892 * to the specified provider. The dtrace_provider_lock must be held, and the 7893 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 7894 * will need to grab the dtrace_lock when it reenters the framework through 7895 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 7896 */ 7897 static void 7898 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 7899 { 7900 struct modctl *ctl; 7901 int all = 0; 7902 7903 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7904 7905 if (prv == NULL) { 7906 all = 1; 7907 prv = dtrace_provider; 7908 } 7909 7910 do { 7911 /* 7912 * First, call the blanket provide operation. 7913 */ 7914 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 7915 7916 /* 7917 * Now call the per-module provide operation. We will grab 7918 * mod_lock to prevent the list from being modified. Note 7919 * that this also prevents the mod_busy bits from changing. 7920 * (mod_busy can only be changed with mod_lock held.) 7921 */ 7922 mutex_enter(&mod_lock); 7923 7924 ctl = &modules; 7925 do { 7926 if (ctl->mod_busy || ctl->mod_mp == NULL) 7927 continue; 7928 7929 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 7930 7931 } while ((ctl = ctl->mod_next) != &modules); 7932 7933 mutex_exit(&mod_lock); 7934 } while (all && (prv = prv->dtpv_next) != NULL); 7935 } 7936 7937 /* 7938 * Iterate over each probe, and call the Framework-to-Provider API function 7939 * denoted by offs. 7940 */ 7941 static void 7942 dtrace_probe_foreach(uintptr_t offs) 7943 { 7944 dtrace_provider_t *prov; 7945 void (*func)(void *, dtrace_id_t, void *); 7946 dtrace_probe_t *probe; 7947 dtrace_icookie_t cookie; 7948 int i; 7949 7950 /* 7951 * We disable interrupts to walk through the probe array. This is 7952 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 7953 * won't see stale data. 7954 */ 7955 cookie = dtrace_interrupt_disable(); 7956 7957 for (i = 0; i < dtrace_nprobes; i++) { 7958 if ((probe = dtrace_probes[i]) == NULL) 7959 continue; 7960 7961 if (probe->dtpr_ecb == NULL) { 7962 /* 7963 * This probe isn't enabled -- don't call the function. 7964 */ 7965 continue; 7966 } 7967 7968 prov = probe->dtpr_provider; 7969 func = *((void(**)(void *, dtrace_id_t, void *)) 7970 ((uintptr_t)&prov->dtpv_pops + offs)); 7971 7972 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 7973 } 7974 7975 dtrace_interrupt_enable(cookie); 7976 } 7977 7978 static int 7979 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 7980 { 7981 dtrace_probekey_t pkey; 7982 uint32_t priv; 7983 uid_t uid; 7984 zoneid_t zoneid; 7985 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 7986 7987 ASSERT(MUTEX_HELD(&dtrace_lock)); 7988 dtrace_ecb_create_cache = NULL; 7989 7990 if (desc == NULL) { 7991 /* 7992 * If we're passed a NULL description, we're being asked to 7993 * create an ECB with a NULL probe. 7994 */ 7995 (void) dtrace_ecb_create_enable(NULL, enab); 7996 return (0); 7997 } 7998 7999 dtrace_probekey(desc, &pkey); 8000 dtrace_cred2priv(state->dts_cred.dcr_cred, &priv, &uid, &zoneid); 8001 8002 if ((priv & DTRACE_PRIV_ZONEOWNER) && 8003 state->dts_options[DTRACEOPT_ZONE] != DTRACEOPT_UNSET) { 8004 /* 8005 * If we have the privilege of instrumenting all zones but we 8006 * have been told to instrument but one, we will spoof this up 8007 * depriving ourselves of DTRACE_PRIV_ZONEOWNER for purposes 8008 * of dtrace_match(). (Note that DTRACEOPT_ZONE is not for 8009 * security but rather for performance: it allows the global 8010 * zone to instrument USDT probes in a local zone without 8011 * requiring all zones to be instrumented.) 8012 */ 8013 priv &= ~DTRACE_PRIV_ZONEOWNER; 8014 zoneid = state->dts_options[DTRACEOPT_ZONE]; 8015 } 8016 8017 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 8018 enab)); 8019 } 8020 8021 /* 8022 * DTrace Helper Provider Functions 8023 */ 8024 static void 8025 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 8026 { 8027 attr->dtat_name = DOF_ATTR_NAME(dofattr); 8028 attr->dtat_data = DOF_ATTR_DATA(dofattr); 8029 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 8030 } 8031 8032 static void 8033 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 8034 const dof_provider_t *dofprov, char *strtab) 8035 { 8036 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 8037 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 8038 dofprov->dofpv_provattr); 8039 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 8040 dofprov->dofpv_modattr); 8041 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 8042 dofprov->dofpv_funcattr); 8043 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 8044 dofprov->dofpv_nameattr); 8045 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 8046 dofprov->dofpv_argsattr); 8047 } 8048 8049 static void 8050 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8051 { 8052 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8053 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8054 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 8055 dof_provider_t *provider; 8056 dof_probe_t *probe; 8057 uint32_t *off, *enoff; 8058 uint8_t *arg; 8059 char *strtab; 8060 uint_t i, nprobes; 8061 dtrace_helper_provdesc_t dhpv; 8062 dtrace_helper_probedesc_t dhpb; 8063 dtrace_meta_t *meta = dtrace_meta_pid; 8064 dtrace_mops_t *mops = &meta->dtm_mops; 8065 void *parg; 8066 8067 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8068 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8069 provider->dofpv_strtab * dof->dofh_secsize); 8070 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8071 provider->dofpv_probes * dof->dofh_secsize); 8072 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8073 provider->dofpv_prargs * dof->dofh_secsize); 8074 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8075 provider->dofpv_proffs * dof->dofh_secsize); 8076 8077 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8078 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 8079 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 8080 enoff = NULL; 8081 8082 /* 8083 * See dtrace_helper_provider_validate(). 8084 */ 8085 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 8086 provider->dofpv_prenoffs != DOF_SECT_NONE) { 8087 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8088 provider->dofpv_prenoffs * dof->dofh_secsize); 8089 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 8090 } 8091 8092 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 8093 8094 /* 8095 * Create the provider. 8096 */ 8097 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8098 8099 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 8100 return; 8101 8102 meta->dtm_count++; 8103 8104 /* 8105 * Create the probes. 8106 */ 8107 for (i = 0; i < nprobes; i++) { 8108 probe = (dof_probe_t *)(uintptr_t)(daddr + 8109 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 8110 8111 dhpb.dthpb_mod = dhp->dofhp_mod; 8112 dhpb.dthpb_func = strtab + probe->dofpr_func; 8113 dhpb.dthpb_name = strtab + probe->dofpr_name; 8114 dhpb.dthpb_base = probe->dofpr_addr; 8115 dhpb.dthpb_offs = off + probe->dofpr_offidx; 8116 dhpb.dthpb_noffs = probe->dofpr_noffs; 8117 if (enoff != NULL) { 8118 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 8119 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 8120 } else { 8121 dhpb.dthpb_enoffs = NULL; 8122 dhpb.dthpb_nenoffs = 0; 8123 } 8124 dhpb.dthpb_args = arg + probe->dofpr_argidx; 8125 dhpb.dthpb_nargc = probe->dofpr_nargc; 8126 dhpb.dthpb_xargc = probe->dofpr_xargc; 8127 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 8128 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 8129 8130 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 8131 } 8132 } 8133 8134 static void 8135 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 8136 { 8137 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8138 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8139 int i; 8140 8141 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8142 8143 for (i = 0; i < dof->dofh_secnum; i++) { 8144 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8145 dof->dofh_secoff + i * dof->dofh_secsize); 8146 8147 if (sec->dofs_type != DOF_SECT_PROVIDER) 8148 continue; 8149 8150 dtrace_helper_provide_one(dhp, sec, pid); 8151 } 8152 8153 /* 8154 * We may have just created probes, so we must now rematch against 8155 * any retained enablings. Note that this call will acquire both 8156 * cpu_lock and dtrace_lock; the fact that we are holding 8157 * dtrace_meta_lock now is what defines the ordering with respect to 8158 * these three locks. 8159 */ 8160 dtrace_enabling_matchall(); 8161 } 8162 8163 static void 8164 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8165 { 8166 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8167 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8168 dof_sec_t *str_sec; 8169 dof_provider_t *provider; 8170 char *strtab; 8171 dtrace_helper_provdesc_t dhpv; 8172 dtrace_meta_t *meta = dtrace_meta_pid; 8173 dtrace_mops_t *mops = &meta->dtm_mops; 8174 8175 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8176 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8177 provider->dofpv_strtab * dof->dofh_secsize); 8178 8179 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8180 8181 /* 8182 * Create the provider. 8183 */ 8184 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8185 8186 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 8187 8188 meta->dtm_count--; 8189 } 8190 8191 static void 8192 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 8193 { 8194 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8195 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8196 int i; 8197 8198 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8199 8200 for (i = 0; i < dof->dofh_secnum; i++) { 8201 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8202 dof->dofh_secoff + i * dof->dofh_secsize); 8203 8204 if (sec->dofs_type != DOF_SECT_PROVIDER) 8205 continue; 8206 8207 dtrace_helper_provider_remove_one(dhp, sec, pid); 8208 } 8209 } 8210 8211 /* 8212 * DTrace Meta Provider-to-Framework API Functions 8213 * 8214 * These functions implement the Meta Provider-to-Framework API, as described 8215 * in <sys/dtrace.h>. 8216 */ 8217 int 8218 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 8219 dtrace_meta_provider_id_t *idp) 8220 { 8221 dtrace_meta_t *meta; 8222 dtrace_helpers_t *help, *next; 8223 int i; 8224 8225 *idp = DTRACE_METAPROVNONE; 8226 8227 /* 8228 * We strictly don't need the name, but we hold onto it for 8229 * debuggability. All hail error queues! 8230 */ 8231 if (name == NULL) { 8232 cmn_err(CE_WARN, "failed to register meta-provider: " 8233 "invalid name"); 8234 return (EINVAL); 8235 } 8236 8237 if (mops == NULL || 8238 mops->dtms_create_probe == NULL || 8239 mops->dtms_provide_pid == NULL || 8240 mops->dtms_remove_pid == NULL) { 8241 cmn_err(CE_WARN, "failed to register meta-register %s: " 8242 "invalid ops", name); 8243 return (EINVAL); 8244 } 8245 8246 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 8247 meta->dtm_mops = *mops; 8248 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8249 (void) strcpy(meta->dtm_name, name); 8250 meta->dtm_arg = arg; 8251 8252 mutex_enter(&dtrace_meta_lock); 8253 mutex_enter(&dtrace_lock); 8254 8255 if (dtrace_meta_pid != NULL) { 8256 mutex_exit(&dtrace_lock); 8257 mutex_exit(&dtrace_meta_lock); 8258 cmn_err(CE_WARN, "failed to register meta-register %s: " 8259 "user-land meta-provider exists", name); 8260 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 8261 kmem_free(meta, sizeof (dtrace_meta_t)); 8262 return (EINVAL); 8263 } 8264 8265 dtrace_meta_pid = meta; 8266 *idp = (dtrace_meta_provider_id_t)meta; 8267 8268 /* 8269 * If there are providers and probes ready to go, pass them 8270 * off to the new meta provider now. 8271 */ 8272 8273 help = dtrace_deferred_pid; 8274 dtrace_deferred_pid = NULL; 8275 8276 mutex_exit(&dtrace_lock); 8277 8278 while (help != NULL) { 8279 for (i = 0; i < help->dthps_nprovs; i++) { 8280 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 8281 help->dthps_pid); 8282 } 8283 8284 next = help->dthps_next; 8285 help->dthps_next = NULL; 8286 help->dthps_prev = NULL; 8287 help->dthps_deferred = 0; 8288 help = next; 8289 } 8290 8291 mutex_exit(&dtrace_meta_lock); 8292 8293 return (0); 8294 } 8295 8296 int 8297 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 8298 { 8299 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 8300 8301 mutex_enter(&dtrace_meta_lock); 8302 mutex_enter(&dtrace_lock); 8303 8304 if (old == dtrace_meta_pid) { 8305 pp = &dtrace_meta_pid; 8306 } else { 8307 panic("attempt to unregister non-existent " 8308 "dtrace meta-provider %p\n", (void *)old); 8309 } 8310 8311 if (old->dtm_count != 0) { 8312 mutex_exit(&dtrace_lock); 8313 mutex_exit(&dtrace_meta_lock); 8314 return (EBUSY); 8315 } 8316 8317 *pp = NULL; 8318 8319 mutex_exit(&dtrace_lock); 8320 mutex_exit(&dtrace_meta_lock); 8321 8322 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 8323 kmem_free(old, sizeof (dtrace_meta_t)); 8324 8325 return (0); 8326 } 8327 8328 8329 /* 8330 * DTrace DIF Object Functions 8331 */ 8332 static int 8333 dtrace_difo_err(uint_t pc, const char *format, ...) 8334 { 8335 if (dtrace_err_verbose) { 8336 va_list alist; 8337 8338 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 8339 va_start(alist, format); 8340 (void) vuprintf(format, alist); 8341 va_end(alist); 8342 } 8343 8344 #ifdef DTRACE_ERRDEBUG 8345 dtrace_errdebug(format); 8346 #endif 8347 return (1); 8348 } 8349 8350 /* 8351 * Validate a DTrace DIF object by checking the IR instructions. The following 8352 * rules are currently enforced by dtrace_difo_validate(): 8353 * 8354 * 1. Each instruction must have a valid opcode 8355 * 2. Each register, string, variable, or subroutine reference must be valid 8356 * 3. No instruction can modify register %r0 (must be zero) 8357 * 4. All instruction reserved bits must be set to zero 8358 * 5. The last instruction must be a "ret" instruction 8359 * 6. All branch targets must reference a valid instruction _after_ the branch 8360 */ 8361 static int 8362 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 8363 cred_t *cr) 8364 { 8365 int err = 0, i; 8366 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8367 int kcheckload; 8368 uint_t pc; 8369 8370 kcheckload = cr == NULL || 8371 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 8372 8373 dp->dtdo_destructive = 0; 8374 8375 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 8376 dif_instr_t instr = dp->dtdo_buf[pc]; 8377 8378 uint_t r1 = DIF_INSTR_R1(instr); 8379 uint_t r2 = DIF_INSTR_R2(instr); 8380 uint_t rd = DIF_INSTR_RD(instr); 8381 uint_t rs = DIF_INSTR_RS(instr); 8382 uint_t label = DIF_INSTR_LABEL(instr); 8383 uint_t v = DIF_INSTR_VAR(instr); 8384 uint_t subr = DIF_INSTR_SUBR(instr); 8385 uint_t type = DIF_INSTR_TYPE(instr); 8386 uint_t op = DIF_INSTR_OP(instr); 8387 8388 switch (op) { 8389 case DIF_OP_OR: 8390 case DIF_OP_XOR: 8391 case DIF_OP_AND: 8392 case DIF_OP_SLL: 8393 case DIF_OP_SRL: 8394 case DIF_OP_SRA: 8395 case DIF_OP_SUB: 8396 case DIF_OP_ADD: 8397 case DIF_OP_MUL: 8398 case DIF_OP_SDIV: 8399 case DIF_OP_UDIV: 8400 case DIF_OP_SREM: 8401 case DIF_OP_UREM: 8402 case DIF_OP_COPYS: 8403 if (r1 >= nregs) 8404 err += efunc(pc, "invalid register %u\n", r1); 8405 if (r2 >= nregs) 8406 err += efunc(pc, "invalid register %u\n", r2); 8407 if (rd >= nregs) 8408 err += efunc(pc, "invalid register %u\n", rd); 8409 if (rd == 0) 8410 err += efunc(pc, "cannot write to %r0\n"); 8411 break; 8412 case DIF_OP_NOT: 8413 case DIF_OP_MOV: 8414 case DIF_OP_ALLOCS: 8415 if (r1 >= nregs) 8416 err += efunc(pc, "invalid register %u\n", r1); 8417 if (r2 != 0) 8418 err += efunc(pc, "non-zero reserved bits\n"); 8419 if (rd >= nregs) 8420 err += efunc(pc, "invalid register %u\n", rd); 8421 if (rd == 0) 8422 err += efunc(pc, "cannot write to %r0\n"); 8423 break; 8424 case DIF_OP_LDSB: 8425 case DIF_OP_LDSH: 8426 case DIF_OP_LDSW: 8427 case DIF_OP_LDUB: 8428 case DIF_OP_LDUH: 8429 case DIF_OP_LDUW: 8430 case DIF_OP_LDX: 8431 if (r1 >= nregs) 8432 err += efunc(pc, "invalid register %u\n", r1); 8433 if (r2 != 0) 8434 err += efunc(pc, "non-zero reserved bits\n"); 8435 if (rd >= nregs) 8436 err += efunc(pc, "invalid register %u\n", rd); 8437 if (rd == 0) 8438 err += efunc(pc, "cannot write to %r0\n"); 8439 if (kcheckload) 8440 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 8441 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 8442 break; 8443 case DIF_OP_RLDSB: 8444 case DIF_OP_RLDSH: 8445 case DIF_OP_RLDSW: 8446 case DIF_OP_RLDUB: 8447 case DIF_OP_RLDUH: 8448 case DIF_OP_RLDUW: 8449 case DIF_OP_RLDX: 8450 if (r1 >= nregs) 8451 err += efunc(pc, "invalid register %u\n", r1); 8452 if (r2 != 0) 8453 err += efunc(pc, "non-zero reserved bits\n"); 8454 if (rd >= nregs) 8455 err += efunc(pc, "invalid register %u\n", rd); 8456 if (rd == 0) 8457 err += efunc(pc, "cannot write to %r0\n"); 8458 break; 8459 case DIF_OP_ULDSB: 8460 case DIF_OP_ULDSH: 8461 case DIF_OP_ULDSW: 8462 case DIF_OP_ULDUB: 8463 case DIF_OP_ULDUH: 8464 case DIF_OP_ULDUW: 8465 case DIF_OP_ULDX: 8466 if (r1 >= nregs) 8467 err += efunc(pc, "invalid register %u\n", r1); 8468 if (r2 != 0) 8469 err += efunc(pc, "non-zero reserved bits\n"); 8470 if (rd >= nregs) 8471 err += efunc(pc, "invalid register %u\n", rd); 8472 if (rd == 0) 8473 err += efunc(pc, "cannot write to %r0\n"); 8474 break; 8475 case DIF_OP_STB: 8476 case DIF_OP_STH: 8477 case DIF_OP_STW: 8478 case DIF_OP_STX: 8479 if (r1 >= nregs) 8480 err += efunc(pc, "invalid register %u\n", r1); 8481 if (r2 != 0) 8482 err += efunc(pc, "non-zero reserved bits\n"); 8483 if (rd >= nregs) 8484 err += efunc(pc, "invalid register %u\n", rd); 8485 if (rd == 0) 8486 err += efunc(pc, "cannot write to 0 address\n"); 8487 break; 8488 case DIF_OP_CMP: 8489 case DIF_OP_SCMP: 8490 if (r1 >= nregs) 8491 err += efunc(pc, "invalid register %u\n", r1); 8492 if (r2 >= nregs) 8493 err += efunc(pc, "invalid register %u\n", r2); 8494 if (rd != 0) 8495 err += efunc(pc, "non-zero reserved bits\n"); 8496 break; 8497 case DIF_OP_TST: 8498 if (r1 >= nregs) 8499 err += efunc(pc, "invalid register %u\n", r1); 8500 if (r2 != 0 || rd != 0) 8501 err += efunc(pc, "non-zero reserved bits\n"); 8502 break; 8503 case DIF_OP_BA: 8504 case DIF_OP_BE: 8505 case DIF_OP_BNE: 8506 case DIF_OP_BG: 8507 case DIF_OP_BGU: 8508 case DIF_OP_BGE: 8509 case DIF_OP_BGEU: 8510 case DIF_OP_BL: 8511 case DIF_OP_BLU: 8512 case DIF_OP_BLE: 8513 case DIF_OP_BLEU: 8514 if (label >= dp->dtdo_len) { 8515 err += efunc(pc, "invalid branch target %u\n", 8516 label); 8517 } 8518 if (label <= pc) { 8519 err += efunc(pc, "backward branch to %u\n", 8520 label); 8521 } 8522 break; 8523 case DIF_OP_RET: 8524 if (r1 != 0 || r2 != 0) 8525 err += efunc(pc, "non-zero reserved bits\n"); 8526 if (rd >= nregs) 8527 err += efunc(pc, "invalid register %u\n", rd); 8528 break; 8529 case DIF_OP_NOP: 8530 case DIF_OP_POPTS: 8531 case DIF_OP_FLUSHTS: 8532 if (r1 != 0 || r2 != 0 || rd != 0) 8533 err += efunc(pc, "non-zero reserved bits\n"); 8534 break; 8535 case DIF_OP_SETX: 8536 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 8537 err += efunc(pc, "invalid integer ref %u\n", 8538 DIF_INSTR_INTEGER(instr)); 8539 } 8540 if (rd >= nregs) 8541 err += efunc(pc, "invalid register %u\n", rd); 8542 if (rd == 0) 8543 err += efunc(pc, "cannot write to %r0\n"); 8544 break; 8545 case DIF_OP_SETS: 8546 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 8547 err += efunc(pc, "invalid string ref %u\n", 8548 DIF_INSTR_STRING(instr)); 8549 } 8550 if (rd >= nregs) 8551 err += efunc(pc, "invalid register %u\n", rd); 8552 if (rd == 0) 8553 err += efunc(pc, "cannot write to %r0\n"); 8554 break; 8555 case DIF_OP_LDGA: 8556 case DIF_OP_LDTA: 8557 if (r1 > DIF_VAR_ARRAY_MAX) 8558 err += efunc(pc, "invalid array %u\n", r1); 8559 if (r2 >= nregs) 8560 err += efunc(pc, "invalid register %u\n", r2); 8561 if (rd >= nregs) 8562 err += efunc(pc, "invalid register %u\n", rd); 8563 if (rd == 0) 8564 err += efunc(pc, "cannot write to %r0\n"); 8565 break; 8566 case DIF_OP_LDGS: 8567 case DIF_OP_LDTS: 8568 case DIF_OP_LDLS: 8569 case DIF_OP_LDGAA: 8570 case DIF_OP_LDTAA: 8571 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 8572 err += efunc(pc, "invalid variable %u\n", v); 8573 if (rd >= nregs) 8574 err += efunc(pc, "invalid register %u\n", rd); 8575 if (rd == 0) 8576 err += efunc(pc, "cannot write to %r0\n"); 8577 break; 8578 case DIF_OP_STGS: 8579 case DIF_OP_STTS: 8580 case DIF_OP_STLS: 8581 case DIF_OP_STGAA: 8582 case DIF_OP_STTAA: 8583 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 8584 err += efunc(pc, "invalid variable %u\n", v); 8585 if (rs >= nregs) 8586 err += efunc(pc, "invalid register %u\n", rd); 8587 break; 8588 case DIF_OP_CALL: 8589 if (subr > DIF_SUBR_MAX) 8590 err += efunc(pc, "invalid subr %u\n", subr); 8591 if (rd >= nregs) 8592 err += efunc(pc, "invalid register %u\n", rd); 8593 if (rd == 0) 8594 err += efunc(pc, "cannot write to %r0\n"); 8595 8596 if (subr == DIF_SUBR_COPYOUT || 8597 subr == DIF_SUBR_COPYOUTSTR) { 8598 dp->dtdo_destructive = 1; 8599 } 8600 8601 if (subr == DIF_SUBR_GETF) { 8602 /* 8603 * If we have a getf() we need to record that 8604 * in our state. Note that our state can be 8605 * NULL if this is a helper -- but in that 8606 * case, the call to getf() is itself illegal, 8607 * and will be caught (slightly later) when 8608 * the helper is validated. 8609 */ 8610 if (vstate->dtvs_state != NULL) 8611 vstate->dtvs_state->dts_getf++; 8612 } 8613 8614 break; 8615 case DIF_OP_PUSHTR: 8616 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 8617 err += efunc(pc, "invalid ref type %u\n", type); 8618 if (r2 >= nregs) 8619 err += efunc(pc, "invalid register %u\n", r2); 8620 if (rs >= nregs) 8621 err += efunc(pc, "invalid register %u\n", rs); 8622 break; 8623 case DIF_OP_PUSHTV: 8624 if (type != DIF_TYPE_CTF) 8625 err += efunc(pc, "invalid val type %u\n", type); 8626 if (r2 >= nregs) 8627 err += efunc(pc, "invalid register %u\n", r2); 8628 if (rs >= nregs) 8629 err += efunc(pc, "invalid register %u\n", rs); 8630 break; 8631 default: 8632 err += efunc(pc, "invalid opcode %u\n", 8633 DIF_INSTR_OP(instr)); 8634 } 8635 } 8636 8637 if (dp->dtdo_len != 0 && 8638 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 8639 err += efunc(dp->dtdo_len - 1, 8640 "expected 'ret' as last DIF instruction\n"); 8641 } 8642 8643 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 8644 /* 8645 * If we're not returning by reference, the size must be either 8646 * 0 or the size of one of the base types. 8647 */ 8648 switch (dp->dtdo_rtype.dtdt_size) { 8649 case 0: 8650 case sizeof (uint8_t): 8651 case sizeof (uint16_t): 8652 case sizeof (uint32_t): 8653 case sizeof (uint64_t): 8654 break; 8655 8656 default: 8657 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 8658 } 8659 } 8660 8661 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 8662 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 8663 dtrace_diftype_t *vt, *et; 8664 uint_t id, ndx; 8665 8666 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 8667 v->dtdv_scope != DIFV_SCOPE_THREAD && 8668 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 8669 err += efunc(i, "unrecognized variable scope %d\n", 8670 v->dtdv_scope); 8671 break; 8672 } 8673 8674 if (v->dtdv_kind != DIFV_KIND_ARRAY && 8675 v->dtdv_kind != DIFV_KIND_SCALAR) { 8676 err += efunc(i, "unrecognized variable type %d\n", 8677 v->dtdv_kind); 8678 break; 8679 } 8680 8681 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 8682 err += efunc(i, "%d exceeds variable id limit\n", id); 8683 break; 8684 } 8685 8686 if (id < DIF_VAR_OTHER_UBASE) 8687 continue; 8688 8689 /* 8690 * For user-defined variables, we need to check that this 8691 * definition is identical to any previous definition that we 8692 * encountered. 8693 */ 8694 ndx = id - DIF_VAR_OTHER_UBASE; 8695 8696 switch (v->dtdv_scope) { 8697 case DIFV_SCOPE_GLOBAL: 8698 if (ndx < vstate->dtvs_nglobals) { 8699 dtrace_statvar_t *svar; 8700 8701 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 8702 existing = &svar->dtsv_var; 8703 } 8704 8705 break; 8706 8707 case DIFV_SCOPE_THREAD: 8708 if (ndx < vstate->dtvs_ntlocals) 8709 existing = &vstate->dtvs_tlocals[ndx]; 8710 break; 8711 8712 case DIFV_SCOPE_LOCAL: 8713 if (ndx < vstate->dtvs_nlocals) { 8714 dtrace_statvar_t *svar; 8715 8716 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 8717 existing = &svar->dtsv_var; 8718 } 8719 8720 break; 8721 } 8722 8723 vt = &v->dtdv_type; 8724 8725 if (vt->dtdt_flags & DIF_TF_BYREF) { 8726 if (vt->dtdt_size == 0) { 8727 err += efunc(i, "zero-sized variable\n"); 8728 break; 8729 } 8730 8731 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 8732 vt->dtdt_size > dtrace_global_maxsize) { 8733 err += efunc(i, "oversized by-ref global\n"); 8734 break; 8735 } 8736 } 8737 8738 if (existing == NULL || existing->dtdv_id == 0) 8739 continue; 8740 8741 ASSERT(existing->dtdv_id == v->dtdv_id); 8742 ASSERT(existing->dtdv_scope == v->dtdv_scope); 8743 8744 if (existing->dtdv_kind != v->dtdv_kind) 8745 err += efunc(i, "%d changed variable kind\n", id); 8746 8747 et = &existing->dtdv_type; 8748 8749 if (vt->dtdt_flags != et->dtdt_flags) { 8750 err += efunc(i, "%d changed variable type flags\n", id); 8751 break; 8752 } 8753 8754 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 8755 err += efunc(i, "%d changed variable type size\n", id); 8756 break; 8757 } 8758 } 8759 8760 return (err); 8761 } 8762 8763 /* 8764 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 8765 * are much more constrained than normal DIFOs. Specifically, they may 8766 * not: 8767 * 8768 * 1. Make calls to subroutines other than copyin(), copyinstr() or 8769 * miscellaneous string routines 8770 * 2. Access DTrace variables other than the args[] array, and the 8771 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 8772 * 3. Have thread-local variables. 8773 * 4. Have dynamic variables. 8774 */ 8775 static int 8776 dtrace_difo_validate_helper(dtrace_difo_t *dp) 8777 { 8778 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8779 int err = 0; 8780 uint_t pc; 8781 8782 for (pc = 0; pc < dp->dtdo_len; pc++) { 8783 dif_instr_t instr = dp->dtdo_buf[pc]; 8784 8785 uint_t v = DIF_INSTR_VAR(instr); 8786 uint_t subr = DIF_INSTR_SUBR(instr); 8787 uint_t op = DIF_INSTR_OP(instr); 8788 8789 switch (op) { 8790 case DIF_OP_OR: 8791 case DIF_OP_XOR: 8792 case DIF_OP_AND: 8793 case DIF_OP_SLL: 8794 case DIF_OP_SRL: 8795 case DIF_OP_SRA: 8796 case DIF_OP_SUB: 8797 case DIF_OP_ADD: 8798 case DIF_OP_MUL: 8799 case DIF_OP_SDIV: 8800 case DIF_OP_UDIV: 8801 case DIF_OP_SREM: 8802 case DIF_OP_UREM: 8803 case DIF_OP_COPYS: 8804 case DIF_OP_NOT: 8805 case DIF_OP_MOV: 8806 case DIF_OP_RLDSB: 8807 case DIF_OP_RLDSH: 8808 case DIF_OP_RLDSW: 8809 case DIF_OP_RLDUB: 8810 case DIF_OP_RLDUH: 8811 case DIF_OP_RLDUW: 8812 case DIF_OP_RLDX: 8813 case DIF_OP_ULDSB: 8814 case DIF_OP_ULDSH: 8815 case DIF_OP_ULDSW: 8816 case DIF_OP_ULDUB: 8817 case DIF_OP_ULDUH: 8818 case DIF_OP_ULDUW: 8819 case DIF_OP_ULDX: 8820 case DIF_OP_STB: 8821 case DIF_OP_STH: 8822 case DIF_OP_STW: 8823 case DIF_OP_STX: 8824 case DIF_OP_ALLOCS: 8825 case DIF_OP_CMP: 8826 case DIF_OP_SCMP: 8827 case DIF_OP_TST: 8828 case DIF_OP_BA: 8829 case DIF_OP_BE: 8830 case DIF_OP_BNE: 8831 case DIF_OP_BG: 8832 case DIF_OP_BGU: 8833 case DIF_OP_BGE: 8834 case DIF_OP_BGEU: 8835 case DIF_OP_BL: 8836 case DIF_OP_BLU: 8837 case DIF_OP_BLE: 8838 case DIF_OP_BLEU: 8839 case DIF_OP_RET: 8840 case DIF_OP_NOP: 8841 case DIF_OP_POPTS: 8842 case DIF_OP_FLUSHTS: 8843 case DIF_OP_SETX: 8844 case DIF_OP_SETS: 8845 case DIF_OP_LDGA: 8846 case DIF_OP_LDLS: 8847 case DIF_OP_STGS: 8848 case DIF_OP_STLS: 8849 case DIF_OP_PUSHTR: 8850 case DIF_OP_PUSHTV: 8851 break; 8852 8853 case DIF_OP_LDGS: 8854 if (v >= DIF_VAR_OTHER_UBASE) 8855 break; 8856 8857 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 8858 break; 8859 8860 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 8861 v == DIF_VAR_PPID || v == DIF_VAR_TID || 8862 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 8863 v == DIF_VAR_UID || v == DIF_VAR_GID) 8864 break; 8865 8866 err += efunc(pc, "illegal variable %u\n", v); 8867 break; 8868 8869 case DIF_OP_LDTA: 8870 case DIF_OP_LDTS: 8871 case DIF_OP_LDGAA: 8872 case DIF_OP_LDTAA: 8873 err += efunc(pc, "illegal dynamic variable load\n"); 8874 break; 8875 8876 case DIF_OP_STTS: 8877 case DIF_OP_STGAA: 8878 case DIF_OP_STTAA: 8879 err += efunc(pc, "illegal dynamic variable store\n"); 8880 break; 8881 8882 case DIF_OP_CALL: 8883 if (subr == DIF_SUBR_ALLOCA || 8884 subr == DIF_SUBR_BCOPY || 8885 subr == DIF_SUBR_COPYIN || 8886 subr == DIF_SUBR_COPYINTO || 8887 subr == DIF_SUBR_COPYINSTR || 8888 subr == DIF_SUBR_INDEX || 8889 subr == DIF_SUBR_INET_NTOA || 8890 subr == DIF_SUBR_INET_NTOA6 || 8891 subr == DIF_SUBR_INET_NTOP || 8892 subr == DIF_SUBR_LLTOSTR || 8893 subr == DIF_SUBR_RINDEX || 8894 subr == DIF_SUBR_STRCHR || 8895 subr == DIF_SUBR_STRJOIN || 8896 subr == DIF_SUBR_STRRCHR || 8897 subr == DIF_SUBR_STRSTR || 8898 subr == DIF_SUBR_HTONS || 8899 subr == DIF_SUBR_HTONL || 8900 subr == DIF_SUBR_HTONLL || 8901 subr == DIF_SUBR_NTOHS || 8902 subr == DIF_SUBR_NTOHL || 8903 subr == DIF_SUBR_NTOHLL) 8904 break; 8905 8906 err += efunc(pc, "invalid subr %u\n", subr); 8907 break; 8908 8909 default: 8910 err += efunc(pc, "invalid opcode %u\n", 8911 DIF_INSTR_OP(instr)); 8912 } 8913 } 8914 8915 return (err); 8916 } 8917 8918 /* 8919 * Returns 1 if the expression in the DIF object can be cached on a per-thread 8920 * basis; 0 if not. 8921 */ 8922 static int 8923 dtrace_difo_cacheable(dtrace_difo_t *dp) 8924 { 8925 int i; 8926 8927 if (dp == NULL) 8928 return (0); 8929 8930 for (i = 0; i < dp->dtdo_varlen; i++) { 8931 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8932 8933 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 8934 continue; 8935 8936 switch (v->dtdv_id) { 8937 case DIF_VAR_CURTHREAD: 8938 case DIF_VAR_PID: 8939 case DIF_VAR_TID: 8940 case DIF_VAR_EXECNAME: 8941 case DIF_VAR_ZONENAME: 8942 break; 8943 8944 default: 8945 return (0); 8946 } 8947 } 8948 8949 /* 8950 * This DIF object may be cacheable. Now we need to look for any 8951 * array loading instructions, any memory loading instructions, or 8952 * any stores to thread-local variables. 8953 */ 8954 for (i = 0; i < dp->dtdo_len; i++) { 8955 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 8956 8957 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 8958 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 8959 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 8960 op == DIF_OP_LDGA || op == DIF_OP_STTS) 8961 return (0); 8962 } 8963 8964 return (1); 8965 } 8966 8967 static void 8968 dtrace_difo_hold(dtrace_difo_t *dp) 8969 { 8970 int i; 8971 8972 ASSERT(MUTEX_HELD(&dtrace_lock)); 8973 8974 dp->dtdo_refcnt++; 8975 ASSERT(dp->dtdo_refcnt != 0); 8976 8977 /* 8978 * We need to check this DIF object for references to the variable 8979 * DIF_VAR_VTIMESTAMP. 8980 */ 8981 for (i = 0; i < dp->dtdo_varlen; i++) { 8982 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8983 8984 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 8985 continue; 8986 8987 if (dtrace_vtime_references++ == 0) 8988 dtrace_vtime_enable(); 8989 } 8990 } 8991 8992 /* 8993 * This routine calculates the dynamic variable chunksize for a given DIF 8994 * object. The calculation is not fool-proof, and can probably be tricked by 8995 * malicious DIF -- but it works for all compiler-generated DIF. Because this 8996 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 8997 * if a dynamic variable size exceeds the chunksize. 8998 */ 8999 static void 9000 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9001 { 9002 uint64_t sval; 9003 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 9004 const dif_instr_t *text = dp->dtdo_buf; 9005 uint_t pc, srd = 0; 9006 uint_t ttop = 0; 9007 size_t size, ksize; 9008 uint_t id, i; 9009 9010 for (pc = 0; pc < dp->dtdo_len; pc++) { 9011 dif_instr_t instr = text[pc]; 9012 uint_t op = DIF_INSTR_OP(instr); 9013 uint_t rd = DIF_INSTR_RD(instr); 9014 uint_t r1 = DIF_INSTR_R1(instr); 9015 uint_t nkeys = 0; 9016 uchar_t scope; 9017 9018 dtrace_key_t *key = tupregs; 9019 9020 switch (op) { 9021 case DIF_OP_SETX: 9022 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 9023 srd = rd; 9024 continue; 9025 9026 case DIF_OP_STTS: 9027 key = &tupregs[DIF_DTR_NREGS]; 9028 key[0].dttk_size = 0; 9029 key[1].dttk_size = 0; 9030 nkeys = 2; 9031 scope = DIFV_SCOPE_THREAD; 9032 break; 9033 9034 case DIF_OP_STGAA: 9035 case DIF_OP_STTAA: 9036 nkeys = ttop; 9037 9038 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 9039 key[nkeys++].dttk_size = 0; 9040 9041 key[nkeys++].dttk_size = 0; 9042 9043 if (op == DIF_OP_STTAA) { 9044 scope = DIFV_SCOPE_THREAD; 9045 } else { 9046 scope = DIFV_SCOPE_GLOBAL; 9047 } 9048 9049 break; 9050 9051 case DIF_OP_PUSHTR: 9052 if (ttop == DIF_DTR_NREGS) 9053 return; 9054 9055 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 9056 /* 9057 * If the register for the size of the "pushtr" 9058 * is %r0 (or the value is 0) and the type is 9059 * a string, we'll use the system-wide default 9060 * string size. 9061 */ 9062 tupregs[ttop++].dttk_size = 9063 dtrace_strsize_default; 9064 } else { 9065 if (srd == 0) 9066 return; 9067 9068 tupregs[ttop++].dttk_size = sval; 9069 } 9070 9071 break; 9072 9073 case DIF_OP_PUSHTV: 9074 if (ttop == DIF_DTR_NREGS) 9075 return; 9076 9077 tupregs[ttop++].dttk_size = 0; 9078 break; 9079 9080 case DIF_OP_FLUSHTS: 9081 ttop = 0; 9082 break; 9083 9084 case DIF_OP_POPTS: 9085 if (ttop != 0) 9086 ttop--; 9087 break; 9088 } 9089 9090 sval = 0; 9091 srd = 0; 9092 9093 if (nkeys == 0) 9094 continue; 9095 9096 /* 9097 * We have a dynamic variable allocation; calculate its size. 9098 */ 9099 for (ksize = 0, i = 0; i < nkeys; i++) 9100 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 9101 9102 size = sizeof (dtrace_dynvar_t); 9103 size += sizeof (dtrace_key_t) * (nkeys - 1); 9104 size += ksize; 9105 9106 /* 9107 * Now we need to determine the size of the stored data. 9108 */ 9109 id = DIF_INSTR_VAR(instr); 9110 9111 for (i = 0; i < dp->dtdo_varlen; i++) { 9112 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9113 9114 if (v->dtdv_id == id && v->dtdv_scope == scope) { 9115 size += v->dtdv_type.dtdt_size; 9116 break; 9117 } 9118 } 9119 9120 if (i == dp->dtdo_varlen) 9121 return; 9122 9123 /* 9124 * We have the size. If this is larger than the chunk size 9125 * for our dynamic variable state, reset the chunk size. 9126 */ 9127 size = P2ROUNDUP(size, sizeof (uint64_t)); 9128 9129 if (size > vstate->dtvs_dynvars.dtds_chunksize) 9130 vstate->dtvs_dynvars.dtds_chunksize = size; 9131 } 9132 } 9133 9134 static void 9135 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9136 { 9137 int i, oldsvars, osz, nsz, otlocals, ntlocals; 9138 uint_t id; 9139 9140 ASSERT(MUTEX_HELD(&dtrace_lock)); 9141 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 9142 9143 for (i = 0; i < dp->dtdo_varlen; i++) { 9144 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9145 dtrace_statvar_t *svar, ***svarp; 9146 size_t dsize = 0; 9147 uint8_t scope = v->dtdv_scope; 9148 int *np; 9149 9150 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9151 continue; 9152 9153 id -= DIF_VAR_OTHER_UBASE; 9154 9155 switch (scope) { 9156 case DIFV_SCOPE_THREAD: 9157 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 9158 dtrace_difv_t *tlocals; 9159 9160 if ((ntlocals = (otlocals << 1)) == 0) 9161 ntlocals = 1; 9162 9163 osz = otlocals * sizeof (dtrace_difv_t); 9164 nsz = ntlocals * sizeof (dtrace_difv_t); 9165 9166 tlocals = kmem_zalloc(nsz, KM_SLEEP); 9167 9168 if (osz != 0) { 9169 bcopy(vstate->dtvs_tlocals, 9170 tlocals, osz); 9171 kmem_free(vstate->dtvs_tlocals, osz); 9172 } 9173 9174 vstate->dtvs_tlocals = tlocals; 9175 vstate->dtvs_ntlocals = ntlocals; 9176 } 9177 9178 vstate->dtvs_tlocals[id] = *v; 9179 continue; 9180 9181 case DIFV_SCOPE_LOCAL: 9182 np = &vstate->dtvs_nlocals; 9183 svarp = &vstate->dtvs_locals; 9184 9185 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9186 dsize = NCPU * (v->dtdv_type.dtdt_size + 9187 sizeof (uint64_t)); 9188 else 9189 dsize = NCPU * sizeof (uint64_t); 9190 9191 break; 9192 9193 case DIFV_SCOPE_GLOBAL: 9194 np = &vstate->dtvs_nglobals; 9195 svarp = &vstate->dtvs_globals; 9196 9197 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9198 dsize = v->dtdv_type.dtdt_size + 9199 sizeof (uint64_t); 9200 9201 break; 9202 9203 default: 9204 ASSERT(0); 9205 } 9206 9207 while (id >= (oldsvars = *np)) { 9208 dtrace_statvar_t **statics; 9209 int newsvars, oldsize, newsize; 9210 9211 if ((newsvars = (oldsvars << 1)) == 0) 9212 newsvars = 1; 9213 9214 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 9215 newsize = newsvars * sizeof (dtrace_statvar_t *); 9216 9217 statics = kmem_zalloc(newsize, KM_SLEEP); 9218 9219 if (oldsize != 0) { 9220 bcopy(*svarp, statics, oldsize); 9221 kmem_free(*svarp, oldsize); 9222 } 9223 9224 *svarp = statics; 9225 *np = newsvars; 9226 } 9227 9228 if ((svar = (*svarp)[id]) == NULL) { 9229 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 9230 svar->dtsv_var = *v; 9231 9232 if ((svar->dtsv_size = dsize) != 0) { 9233 svar->dtsv_data = (uint64_t)(uintptr_t) 9234 kmem_zalloc(dsize, KM_SLEEP); 9235 } 9236 9237 (*svarp)[id] = svar; 9238 } 9239 9240 svar->dtsv_refcnt++; 9241 } 9242 9243 dtrace_difo_chunksize(dp, vstate); 9244 dtrace_difo_hold(dp); 9245 } 9246 9247 static dtrace_difo_t * 9248 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9249 { 9250 dtrace_difo_t *new; 9251 size_t sz; 9252 9253 ASSERT(dp->dtdo_buf != NULL); 9254 ASSERT(dp->dtdo_refcnt != 0); 9255 9256 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 9257 9258 ASSERT(dp->dtdo_buf != NULL); 9259 sz = dp->dtdo_len * sizeof (dif_instr_t); 9260 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 9261 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 9262 new->dtdo_len = dp->dtdo_len; 9263 9264 if (dp->dtdo_strtab != NULL) { 9265 ASSERT(dp->dtdo_strlen != 0); 9266 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 9267 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 9268 new->dtdo_strlen = dp->dtdo_strlen; 9269 } 9270 9271 if (dp->dtdo_inttab != NULL) { 9272 ASSERT(dp->dtdo_intlen != 0); 9273 sz = dp->dtdo_intlen * sizeof (uint64_t); 9274 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 9275 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 9276 new->dtdo_intlen = dp->dtdo_intlen; 9277 } 9278 9279 if (dp->dtdo_vartab != NULL) { 9280 ASSERT(dp->dtdo_varlen != 0); 9281 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 9282 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 9283 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 9284 new->dtdo_varlen = dp->dtdo_varlen; 9285 } 9286 9287 dtrace_difo_init(new, vstate); 9288 return (new); 9289 } 9290 9291 static void 9292 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9293 { 9294 int i; 9295 9296 ASSERT(dp->dtdo_refcnt == 0); 9297 9298 for (i = 0; i < dp->dtdo_varlen; i++) { 9299 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9300 dtrace_statvar_t *svar, **svarp; 9301 uint_t id; 9302 uint8_t scope = v->dtdv_scope; 9303 int *np; 9304 9305 switch (scope) { 9306 case DIFV_SCOPE_THREAD: 9307 continue; 9308 9309 case DIFV_SCOPE_LOCAL: 9310 np = &vstate->dtvs_nlocals; 9311 svarp = vstate->dtvs_locals; 9312 break; 9313 9314 case DIFV_SCOPE_GLOBAL: 9315 np = &vstate->dtvs_nglobals; 9316 svarp = vstate->dtvs_globals; 9317 break; 9318 9319 default: 9320 ASSERT(0); 9321 } 9322 9323 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9324 continue; 9325 9326 id -= DIF_VAR_OTHER_UBASE; 9327 ASSERT(id < *np); 9328 9329 svar = svarp[id]; 9330 ASSERT(svar != NULL); 9331 ASSERT(svar->dtsv_refcnt > 0); 9332 9333 if (--svar->dtsv_refcnt > 0) 9334 continue; 9335 9336 if (svar->dtsv_size != 0) { 9337 ASSERT(svar->dtsv_data != NULL); 9338 kmem_free((void *)(uintptr_t)svar->dtsv_data, 9339 svar->dtsv_size); 9340 } 9341 9342 kmem_free(svar, sizeof (dtrace_statvar_t)); 9343 svarp[id] = NULL; 9344 } 9345 9346 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 9347 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 9348 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 9349 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 9350 9351 kmem_free(dp, sizeof (dtrace_difo_t)); 9352 } 9353 9354 static void 9355 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9356 { 9357 int i; 9358 9359 ASSERT(MUTEX_HELD(&dtrace_lock)); 9360 ASSERT(dp->dtdo_refcnt != 0); 9361 9362 for (i = 0; i < dp->dtdo_varlen; i++) { 9363 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9364 9365 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9366 continue; 9367 9368 ASSERT(dtrace_vtime_references > 0); 9369 if (--dtrace_vtime_references == 0) 9370 dtrace_vtime_disable(); 9371 } 9372 9373 if (--dp->dtdo_refcnt == 0) 9374 dtrace_difo_destroy(dp, vstate); 9375 } 9376 9377 /* 9378 * DTrace Format Functions 9379 */ 9380 static uint16_t 9381 dtrace_format_add(dtrace_state_t *state, char *str) 9382 { 9383 char *fmt, **new; 9384 uint16_t ndx, len = strlen(str) + 1; 9385 9386 fmt = kmem_zalloc(len, KM_SLEEP); 9387 bcopy(str, fmt, len); 9388 9389 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 9390 if (state->dts_formats[ndx] == NULL) { 9391 state->dts_formats[ndx] = fmt; 9392 return (ndx + 1); 9393 } 9394 } 9395 9396 if (state->dts_nformats == USHRT_MAX) { 9397 /* 9398 * This is only likely if a denial-of-service attack is being 9399 * attempted. As such, it's okay to fail silently here. 9400 */ 9401 kmem_free(fmt, len); 9402 return (0); 9403 } 9404 9405 /* 9406 * For simplicity, we always resize the formats array to be exactly the 9407 * number of formats. 9408 */ 9409 ndx = state->dts_nformats++; 9410 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 9411 9412 if (state->dts_formats != NULL) { 9413 ASSERT(ndx != 0); 9414 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 9415 kmem_free(state->dts_formats, ndx * sizeof (char *)); 9416 } 9417 9418 state->dts_formats = new; 9419 state->dts_formats[ndx] = fmt; 9420 9421 return (ndx + 1); 9422 } 9423 9424 static void 9425 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 9426 { 9427 char *fmt; 9428 9429 ASSERT(state->dts_formats != NULL); 9430 ASSERT(format <= state->dts_nformats); 9431 ASSERT(state->dts_formats[format - 1] != NULL); 9432 9433 fmt = state->dts_formats[format - 1]; 9434 kmem_free(fmt, strlen(fmt) + 1); 9435 state->dts_formats[format - 1] = NULL; 9436 } 9437 9438 static void 9439 dtrace_format_destroy(dtrace_state_t *state) 9440 { 9441 int i; 9442 9443 if (state->dts_nformats == 0) { 9444 ASSERT(state->dts_formats == NULL); 9445 return; 9446 } 9447 9448 ASSERT(state->dts_formats != NULL); 9449 9450 for (i = 0; i < state->dts_nformats; i++) { 9451 char *fmt = state->dts_formats[i]; 9452 9453 if (fmt == NULL) 9454 continue; 9455 9456 kmem_free(fmt, strlen(fmt) + 1); 9457 } 9458 9459 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 9460 state->dts_nformats = 0; 9461 state->dts_formats = NULL; 9462 } 9463 9464 /* 9465 * DTrace Predicate Functions 9466 */ 9467 static dtrace_predicate_t * 9468 dtrace_predicate_create(dtrace_difo_t *dp) 9469 { 9470 dtrace_predicate_t *pred; 9471 9472 ASSERT(MUTEX_HELD(&dtrace_lock)); 9473 ASSERT(dp->dtdo_refcnt != 0); 9474 9475 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 9476 pred->dtp_difo = dp; 9477 pred->dtp_refcnt = 1; 9478 9479 if (!dtrace_difo_cacheable(dp)) 9480 return (pred); 9481 9482 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 9483 /* 9484 * This is only theoretically possible -- we have had 2^32 9485 * cacheable predicates on this machine. We cannot allow any 9486 * more predicates to become cacheable: as unlikely as it is, 9487 * there may be a thread caching a (now stale) predicate cache 9488 * ID. (N.B.: the temptation is being successfully resisted to 9489 * have this cmn_err() "Holy shit -- we executed this code!") 9490 */ 9491 return (pred); 9492 } 9493 9494 pred->dtp_cacheid = dtrace_predcache_id++; 9495 9496 return (pred); 9497 } 9498 9499 static void 9500 dtrace_predicate_hold(dtrace_predicate_t *pred) 9501 { 9502 ASSERT(MUTEX_HELD(&dtrace_lock)); 9503 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 9504 ASSERT(pred->dtp_refcnt > 0); 9505 9506 pred->dtp_refcnt++; 9507 } 9508 9509 static void 9510 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 9511 { 9512 dtrace_difo_t *dp = pred->dtp_difo; 9513 9514 ASSERT(MUTEX_HELD(&dtrace_lock)); 9515 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 9516 ASSERT(pred->dtp_refcnt > 0); 9517 9518 if (--pred->dtp_refcnt == 0) { 9519 dtrace_difo_release(pred->dtp_difo, vstate); 9520 kmem_free(pred, sizeof (dtrace_predicate_t)); 9521 } 9522 } 9523 9524 /* 9525 * DTrace Action Description Functions 9526 */ 9527 static dtrace_actdesc_t * 9528 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 9529 uint64_t uarg, uint64_t arg) 9530 { 9531 dtrace_actdesc_t *act; 9532 9533 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 9534 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 9535 9536 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 9537 act->dtad_kind = kind; 9538 act->dtad_ntuple = ntuple; 9539 act->dtad_uarg = uarg; 9540 act->dtad_arg = arg; 9541 act->dtad_refcnt = 1; 9542 9543 return (act); 9544 } 9545 9546 static void 9547 dtrace_actdesc_hold(dtrace_actdesc_t *act) 9548 { 9549 ASSERT(act->dtad_refcnt >= 1); 9550 act->dtad_refcnt++; 9551 } 9552 9553 static void 9554 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 9555 { 9556 dtrace_actkind_t kind = act->dtad_kind; 9557 dtrace_difo_t *dp; 9558 9559 ASSERT(act->dtad_refcnt >= 1); 9560 9561 if (--act->dtad_refcnt != 0) 9562 return; 9563 9564 if ((dp = act->dtad_difo) != NULL) 9565 dtrace_difo_release(dp, vstate); 9566 9567 if (DTRACEACT_ISPRINTFLIKE(kind)) { 9568 char *str = (char *)(uintptr_t)act->dtad_arg; 9569 9570 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 9571 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 9572 9573 if (str != NULL) 9574 kmem_free(str, strlen(str) + 1); 9575 } 9576 9577 kmem_free(act, sizeof (dtrace_actdesc_t)); 9578 } 9579 9580 /* 9581 * DTrace ECB Functions 9582 */ 9583 static dtrace_ecb_t * 9584 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 9585 { 9586 dtrace_ecb_t *ecb; 9587 dtrace_epid_t epid; 9588 9589 ASSERT(MUTEX_HELD(&dtrace_lock)); 9590 9591 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 9592 ecb->dte_predicate = NULL; 9593 ecb->dte_probe = probe; 9594 9595 /* 9596 * The default size is the size of the default action: recording 9597 * the header. 9598 */ 9599 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 9600 ecb->dte_alignment = sizeof (dtrace_epid_t); 9601 9602 epid = state->dts_epid++; 9603 9604 if (epid - 1 >= state->dts_necbs) { 9605 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 9606 int necbs = state->dts_necbs << 1; 9607 9608 ASSERT(epid == state->dts_necbs + 1); 9609 9610 if (necbs == 0) { 9611 ASSERT(oecbs == NULL); 9612 necbs = 1; 9613 } 9614 9615 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 9616 9617 if (oecbs != NULL) 9618 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 9619 9620 dtrace_membar_producer(); 9621 state->dts_ecbs = ecbs; 9622 9623 if (oecbs != NULL) { 9624 /* 9625 * If this state is active, we must dtrace_sync() 9626 * before we can free the old dts_ecbs array: we're 9627 * coming in hot, and there may be active ring 9628 * buffer processing (which indexes into the dts_ecbs 9629 * array) on another CPU. 9630 */ 9631 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 9632 dtrace_sync(); 9633 9634 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 9635 } 9636 9637 dtrace_membar_producer(); 9638 state->dts_necbs = necbs; 9639 } 9640 9641 ecb->dte_state = state; 9642 9643 ASSERT(state->dts_ecbs[epid - 1] == NULL); 9644 dtrace_membar_producer(); 9645 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 9646 9647 return (ecb); 9648 } 9649 9650 static int 9651 dtrace_ecb_enable(dtrace_ecb_t *ecb) 9652 { 9653 dtrace_probe_t *probe = ecb->dte_probe; 9654 9655 ASSERT(MUTEX_HELD(&cpu_lock)); 9656 ASSERT(MUTEX_HELD(&dtrace_lock)); 9657 ASSERT(ecb->dte_next == NULL); 9658 9659 if (probe == NULL) { 9660 /* 9661 * This is the NULL probe -- there's nothing to do. 9662 */ 9663 return (0); 9664 } 9665 9666 if (probe->dtpr_ecb == NULL) { 9667 dtrace_provider_t *prov = probe->dtpr_provider; 9668 9669 /* 9670 * We're the first ECB on this probe. 9671 */ 9672 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 9673 9674 if (ecb->dte_predicate != NULL) 9675 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 9676 9677 return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 9678 probe->dtpr_id, probe->dtpr_arg)); 9679 } else { 9680 /* 9681 * This probe is already active. Swing the last pointer to 9682 * point to the new ECB, and issue a dtrace_sync() to assure 9683 * that all CPUs have seen the change. 9684 */ 9685 ASSERT(probe->dtpr_ecb_last != NULL); 9686 probe->dtpr_ecb_last->dte_next = ecb; 9687 probe->dtpr_ecb_last = ecb; 9688 probe->dtpr_predcache = 0; 9689 9690 dtrace_sync(); 9691 return (0); 9692 } 9693 } 9694 9695 static void 9696 dtrace_ecb_resize(dtrace_ecb_t *ecb) 9697 { 9698 dtrace_action_t *act; 9699 uint32_t curneeded = UINT32_MAX; 9700 uint32_t aggbase = UINT32_MAX; 9701 9702 /* 9703 * If we record anything, we always record the dtrace_rechdr_t. (And 9704 * we always record it first.) 9705 */ 9706 ecb->dte_size = sizeof (dtrace_rechdr_t); 9707 ecb->dte_alignment = sizeof (dtrace_epid_t); 9708 9709 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9710 dtrace_recdesc_t *rec = &act->dta_rec; 9711 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 9712 9713 ecb->dte_alignment = MAX(ecb->dte_alignment, 9714 rec->dtrd_alignment); 9715 9716 if (DTRACEACT_ISAGG(act->dta_kind)) { 9717 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9718 9719 ASSERT(rec->dtrd_size != 0); 9720 ASSERT(agg->dtag_first != NULL); 9721 ASSERT(act->dta_prev->dta_intuple); 9722 ASSERT(aggbase != UINT32_MAX); 9723 ASSERT(curneeded != UINT32_MAX); 9724 9725 agg->dtag_base = aggbase; 9726 9727 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 9728 rec->dtrd_offset = curneeded; 9729 curneeded += rec->dtrd_size; 9730 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 9731 9732 aggbase = UINT32_MAX; 9733 curneeded = UINT32_MAX; 9734 } else if (act->dta_intuple) { 9735 if (curneeded == UINT32_MAX) { 9736 /* 9737 * This is the first record in a tuple. Align 9738 * curneeded to be at offset 4 in an 8-byte 9739 * aligned block. 9740 */ 9741 ASSERT(act->dta_prev == NULL || 9742 !act->dta_prev->dta_intuple); 9743 ASSERT3U(aggbase, ==, UINT32_MAX); 9744 curneeded = P2PHASEUP(ecb->dte_size, 9745 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 9746 9747 aggbase = curneeded - sizeof (dtrace_aggid_t); 9748 ASSERT(IS_P2ALIGNED(aggbase, 9749 sizeof (uint64_t))); 9750 } 9751 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 9752 rec->dtrd_offset = curneeded; 9753 curneeded += rec->dtrd_size; 9754 } else { 9755 /* tuples must be followed by an aggregation */ 9756 ASSERT(act->dta_prev == NULL || 9757 !act->dta_prev->dta_intuple); 9758 9759 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 9760 rec->dtrd_alignment); 9761 rec->dtrd_offset = ecb->dte_size; 9762 ecb->dte_size += rec->dtrd_size; 9763 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 9764 } 9765 } 9766 9767 if ((act = ecb->dte_action) != NULL && 9768 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 9769 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 9770 /* 9771 * If the size is still sizeof (dtrace_rechdr_t), then all 9772 * actions store no data; set the size to 0. 9773 */ 9774 ecb->dte_size = 0; 9775 } 9776 9777 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 9778 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 9779 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 9780 ecb->dte_needed); 9781 } 9782 9783 static dtrace_action_t * 9784 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9785 { 9786 dtrace_aggregation_t *agg; 9787 size_t size = sizeof (uint64_t); 9788 int ntuple = desc->dtad_ntuple; 9789 dtrace_action_t *act; 9790 dtrace_recdesc_t *frec; 9791 dtrace_aggid_t aggid; 9792 dtrace_state_t *state = ecb->dte_state; 9793 9794 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 9795 agg->dtag_ecb = ecb; 9796 9797 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 9798 9799 switch (desc->dtad_kind) { 9800 case DTRACEAGG_MIN: 9801 agg->dtag_initial = INT64_MAX; 9802 agg->dtag_aggregate = dtrace_aggregate_min; 9803 break; 9804 9805 case DTRACEAGG_MAX: 9806 agg->dtag_initial = INT64_MIN; 9807 agg->dtag_aggregate = dtrace_aggregate_max; 9808 break; 9809 9810 case DTRACEAGG_COUNT: 9811 agg->dtag_aggregate = dtrace_aggregate_count; 9812 break; 9813 9814 case DTRACEAGG_QUANTIZE: 9815 agg->dtag_aggregate = dtrace_aggregate_quantize; 9816 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 9817 sizeof (uint64_t); 9818 break; 9819 9820 case DTRACEAGG_LQUANTIZE: { 9821 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 9822 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 9823 9824 agg->dtag_initial = desc->dtad_arg; 9825 agg->dtag_aggregate = dtrace_aggregate_lquantize; 9826 9827 if (step == 0 || levels == 0) 9828 goto err; 9829 9830 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 9831 break; 9832 } 9833 9834 case DTRACEAGG_LLQUANTIZE: { 9835 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 9836 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 9837 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 9838 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 9839 int64_t v; 9840 9841 agg->dtag_initial = desc->dtad_arg; 9842 agg->dtag_aggregate = dtrace_aggregate_llquantize; 9843 9844 if (factor < 2 || low >= high || nsteps < factor) 9845 goto err; 9846 9847 /* 9848 * Now check that the number of steps evenly divides a power 9849 * of the factor. (This assures both integer bucket size and 9850 * linearity within each magnitude.) 9851 */ 9852 for (v = factor; v < nsteps; v *= factor) 9853 continue; 9854 9855 if ((v % nsteps) || (nsteps % factor)) 9856 goto err; 9857 9858 size = (dtrace_aggregate_llquantize_bucket(factor, 9859 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 9860 break; 9861 } 9862 9863 case DTRACEAGG_AVG: 9864 agg->dtag_aggregate = dtrace_aggregate_avg; 9865 size = sizeof (uint64_t) * 2; 9866 break; 9867 9868 case DTRACEAGG_STDDEV: 9869 agg->dtag_aggregate = dtrace_aggregate_stddev; 9870 size = sizeof (uint64_t) * 4; 9871 break; 9872 9873 case DTRACEAGG_SUM: 9874 agg->dtag_aggregate = dtrace_aggregate_sum; 9875 break; 9876 9877 default: 9878 goto err; 9879 } 9880 9881 agg->dtag_action.dta_rec.dtrd_size = size; 9882 9883 if (ntuple == 0) 9884 goto err; 9885 9886 /* 9887 * We must make sure that we have enough actions for the n-tuple. 9888 */ 9889 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 9890 if (DTRACEACT_ISAGG(act->dta_kind)) 9891 break; 9892 9893 if (--ntuple == 0) { 9894 /* 9895 * This is the action with which our n-tuple begins. 9896 */ 9897 agg->dtag_first = act; 9898 goto success; 9899 } 9900 } 9901 9902 /* 9903 * This n-tuple is short by ntuple elements. Return failure. 9904 */ 9905 ASSERT(ntuple != 0); 9906 err: 9907 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9908 return (NULL); 9909 9910 success: 9911 /* 9912 * If the last action in the tuple has a size of zero, it's actually 9913 * an expression argument for the aggregating action. 9914 */ 9915 ASSERT(ecb->dte_action_last != NULL); 9916 act = ecb->dte_action_last; 9917 9918 if (act->dta_kind == DTRACEACT_DIFEXPR) { 9919 ASSERT(act->dta_difo != NULL); 9920 9921 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 9922 agg->dtag_hasarg = 1; 9923 } 9924 9925 /* 9926 * We need to allocate an id for this aggregation. 9927 */ 9928 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 9929 VM_BESTFIT | VM_SLEEP); 9930 9931 if (aggid - 1 >= state->dts_naggregations) { 9932 dtrace_aggregation_t **oaggs = state->dts_aggregations; 9933 dtrace_aggregation_t **aggs; 9934 int naggs = state->dts_naggregations << 1; 9935 int onaggs = state->dts_naggregations; 9936 9937 ASSERT(aggid == state->dts_naggregations + 1); 9938 9939 if (naggs == 0) { 9940 ASSERT(oaggs == NULL); 9941 naggs = 1; 9942 } 9943 9944 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 9945 9946 if (oaggs != NULL) { 9947 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 9948 kmem_free(oaggs, onaggs * sizeof (*aggs)); 9949 } 9950 9951 state->dts_aggregations = aggs; 9952 state->dts_naggregations = naggs; 9953 } 9954 9955 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 9956 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 9957 9958 frec = &agg->dtag_first->dta_rec; 9959 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 9960 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 9961 9962 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 9963 ASSERT(!act->dta_intuple); 9964 act->dta_intuple = 1; 9965 } 9966 9967 return (&agg->dtag_action); 9968 } 9969 9970 static void 9971 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 9972 { 9973 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9974 dtrace_state_t *state = ecb->dte_state; 9975 dtrace_aggid_t aggid = agg->dtag_id; 9976 9977 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 9978 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 9979 9980 ASSERT(state->dts_aggregations[aggid - 1] == agg); 9981 state->dts_aggregations[aggid - 1] = NULL; 9982 9983 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9984 } 9985 9986 static int 9987 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9988 { 9989 dtrace_action_t *action, *last; 9990 dtrace_difo_t *dp = desc->dtad_difo; 9991 uint32_t size = 0, align = sizeof (uint8_t), mask; 9992 uint16_t format = 0; 9993 dtrace_recdesc_t *rec; 9994 dtrace_state_t *state = ecb->dte_state; 9995 dtrace_optval_t *opt = state->dts_options, nframes, strsize; 9996 uint64_t arg = desc->dtad_arg; 9997 9998 ASSERT(MUTEX_HELD(&dtrace_lock)); 9999 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 10000 10001 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 10002 /* 10003 * If this is an aggregating action, there must be neither 10004 * a speculate nor a commit on the action chain. 10005 */ 10006 dtrace_action_t *act; 10007 10008 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 10009 if (act->dta_kind == DTRACEACT_COMMIT) 10010 return (EINVAL); 10011 10012 if (act->dta_kind == DTRACEACT_SPECULATE) 10013 return (EINVAL); 10014 } 10015 10016 action = dtrace_ecb_aggregation_create(ecb, desc); 10017 10018 if (action == NULL) 10019 return (EINVAL); 10020 } else { 10021 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 10022 (desc->dtad_kind == DTRACEACT_DIFEXPR && 10023 dp != NULL && dp->dtdo_destructive)) { 10024 state->dts_destructive = 1; 10025 } 10026 10027 switch (desc->dtad_kind) { 10028 case DTRACEACT_PRINTF: 10029 case DTRACEACT_PRINTA: 10030 case DTRACEACT_SYSTEM: 10031 case DTRACEACT_FREOPEN: 10032 case DTRACEACT_DIFEXPR: 10033 /* 10034 * We know that our arg is a string -- turn it into a 10035 * format. 10036 */ 10037 if (arg == NULL) { 10038 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 10039 desc->dtad_kind == DTRACEACT_DIFEXPR); 10040 format = 0; 10041 } else { 10042 ASSERT(arg != NULL); 10043 ASSERT(arg > KERNELBASE); 10044 format = dtrace_format_add(state, 10045 (char *)(uintptr_t)arg); 10046 } 10047 10048 /*FALLTHROUGH*/ 10049 case DTRACEACT_LIBACT: 10050 case DTRACEACT_TRACEMEM: 10051 case DTRACEACT_TRACEMEM_DYNSIZE: 10052 if (dp == NULL) 10053 return (EINVAL); 10054 10055 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 10056 break; 10057 10058 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 10059 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10060 return (EINVAL); 10061 10062 size = opt[DTRACEOPT_STRSIZE]; 10063 } 10064 10065 break; 10066 10067 case DTRACEACT_STACK: 10068 if ((nframes = arg) == 0) { 10069 nframes = opt[DTRACEOPT_STACKFRAMES]; 10070 ASSERT(nframes > 0); 10071 arg = nframes; 10072 } 10073 10074 size = nframes * sizeof (pc_t); 10075 break; 10076 10077 case DTRACEACT_JSTACK: 10078 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 10079 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 10080 10081 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 10082 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 10083 10084 arg = DTRACE_USTACK_ARG(nframes, strsize); 10085 10086 /*FALLTHROUGH*/ 10087 case DTRACEACT_USTACK: 10088 if (desc->dtad_kind != DTRACEACT_JSTACK && 10089 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 10090 strsize = DTRACE_USTACK_STRSIZE(arg); 10091 nframes = opt[DTRACEOPT_USTACKFRAMES]; 10092 ASSERT(nframes > 0); 10093 arg = DTRACE_USTACK_ARG(nframes, strsize); 10094 } 10095 10096 /* 10097 * Save a slot for the pid. 10098 */ 10099 size = (nframes + 1) * sizeof (uint64_t); 10100 size += DTRACE_USTACK_STRSIZE(arg); 10101 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 10102 10103 break; 10104 10105 case DTRACEACT_SYM: 10106 case DTRACEACT_MOD: 10107 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 10108 sizeof (uint64_t)) || 10109 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10110 return (EINVAL); 10111 break; 10112 10113 case DTRACEACT_USYM: 10114 case DTRACEACT_UMOD: 10115 case DTRACEACT_UADDR: 10116 if (dp == NULL || 10117 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 10118 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10119 return (EINVAL); 10120 10121 /* 10122 * We have a slot for the pid, plus a slot for the 10123 * argument. To keep things simple (aligned with 10124 * bitness-neutral sizing), we store each as a 64-bit 10125 * quantity. 10126 */ 10127 size = 2 * sizeof (uint64_t); 10128 break; 10129 10130 case DTRACEACT_STOP: 10131 case DTRACEACT_BREAKPOINT: 10132 case DTRACEACT_PANIC: 10133 break; 10134 10135 case DTRACEACT_CHILL: 10136 case DTRACEACT_DISCARD: 10137 case DTRACEACT_RAISE: 10138 if (dp == NULL) 10139 return (EINVAL); 10140 break; 10141 10142 case DTRACEACT_EXIT: 10143 if (dp == NULL || 10144 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 10145 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10146 return (EINVAL); 10147 break; 10148 10149 case DTRACEACT_SPECULATE: 10150 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 10151 return (EINVAL); 10152 10153 if (dp == NULL) 10154 return (EINVAL); 10155 10156 state->dts_speculates = 1; 10157 break; 10158 10159 case DTRACEACT_COMMIT: { 10160 dtrace_action_t *act = ecb->dte_action; 10161 10162 for (; act != NULL; act = act->dta_next) { 10163 if (act->dta_kind == DTRACEACT_COMMIT) 10164 return (EINVAL); 10165 } 10166 10167 if (dp == NULL) 10168 return (EINVAL); 10169 break; 10170 } 10171 10172 default: 10173 return (EINVAL); 10174 } 10175 10176 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 10177 /* 10178 * If this is a data-storing action or a speculate, 10179 * we must be sure that there isn't a commit on the 10180 * action chain. 10181 */ 10182 dtrace_action_t *act = ecb->dte_action; 10183 10184 for (; act != NULL; act = act->dta_next) { 10185 if (act->dta_kind == DTRACEACT_COMMIT) 10186 return (EINVAL); 10187 } 10188 } 10189 10190 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 10191 action->dta_rec.dtrd_size = size; 10192 } 10193 10194 action->dta_refcnt = 1; 10195 rec = &action->dta_rec; 10196 size = rec->dtrd_size; 10197 10198 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 10199 if (!(size & mask)) { 10200 align = mask + 1; 10201 break; 10202 } 10203 } 10204 10205 action->dta_kind = desc->dtad_kind; 10206 10207 if ((action->dta_difo = dp) != NULL) 10208 dtrace_difo_hold(dp); 10209 10210 rec->dtrd_action = action->dta_kind; 10211 rec->dtrd_arg = arg; 10212 rec->dtrd_uarg = desc->dtad_uarg; 10213 rec->dtrd_alignment = (uint16_t)align; 10214 rec->dtrd_format = format; 10215 10216 if ((last = ecb->dte_action_last) != NULL) { 10217 ASSERT(ecb->dte_action != NULL); 10218 action->dta_prev = last; 10219 last->dta_next = action; 10220 } else { 10221 ASSERT(ecb->dte_action == NULL); 10222 ecb->dte_action = action; 10223 } 10224 10225 ecb->dte_action_last = action; 10226 10227 return (0); 10228 } 10229 10230 static void 10231 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 10232 { 10233 dtrace_action_t *act = ecb->dte_action, *next; 10234 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 10235 dtrace_difo_t *dp; 10236 uint16_t format; 10237 10238 if (act != NULL && act->dta_refcnt > 1) { 10239 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 10240 act->dta_refcnt--; 10241 } else { 10242 for (; act != NULL; act = next) { 10243 next = act->dta_next; 10244 ASSERT(next != NULL || act == ecb->dte_action_last); 10245 ASSERT(act->dta_refcnt == 1); 10246 10247 if ((format = act->dta_rec.dtrd_format) != 0) 10248 dtrace_format_remove(ecb->dte_state, format); 10249 10250 if ((dp = act->dta_difo) != NULL) 10251 dtrace_difo_release(dp, vstate); 10252 10253 if (DTRACEACT_ISAGG(act->dta_kind)) { 10254 dtrace_ecb_aggregation_destroy(ecb, act); 10255 } else { 10256 kmem_free(act, sizeof (dtrace_action_t)); 10257 } 10258 } 10259 } 10260 10261 ecb->dte_action = NULL; 10262 ecb->dte_action_last = NULL; 10263 ecb->dte_size = 0; 10264 } 10265 10266 static void 10267 dtrace_ecb_disable(dtrace_ecb_t *ecb) 10268 { 10269 /* 10270 * We disable the ECB by removing it from its probe. 10271 */ 10272 dtrace_ecb_t *pecb, *prev = NULL; 10273 dtrace_probe_t *probe = ecb->dte_probe; 10274 10275 ASSERT(MUTEX_HELD(&dtrace_lock)); 10276 10277 if (probe == NULL) { 10278 /* 10279 * This is the NULL probe; there is nothing to disable. 10280 */ 10281 return; 10282 } 10283 10284 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 10285 if (pecb == ecb) 10286 break; 10287 prev = pecb; 10288 } 10289 10290 ASSERT(pecb != NULL); 10291 10292 if (prev == NULL) { 10293 probe->dtpr_ecb = ecb->dte_next; 10294 } else { 10295 prev->dte_next = ecb->dte_next; 10296 } 10297 10298 if (ecb == probe->dtpr_ecb_last) { 10299 ASSERT(ecb->dte_next == NULL); 10300 probe->dtpr_ecb_last = prev; 10301 } 10302 10303 /* 10304 * The ECB has been disconnected from the probe; now sync to assure 10305 * that all CPUs have seen the change before returning. 10306 */ 10307 dtrace_sync(); 10308 10309 if (probe->dtpr_ecb == NULL) { 10310 /* 10311 * That was the last ECB on the probe; clear the predicate 10312 * cache ID for the probe, disable it and sync one more time 10313 * to assure that we'll never hit it again. 10314 */ 10315 dtrace_provider_t *prov = probe->dtpr_provider; 10316 10317 ASSERT(ecb->dte_next == NULL); 10318 ASSERT(probe->dtpr_ecb_last == NULL); 10319 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 10320 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 10321 probe->dtpr_id, probe->dtpr_arg); 10322 dtrace_sync(); 10323 } else { 10324 /* 10325 * There is at least one ECB remaining on the probe. If there 10326 * is _exactly_ one, set the probe's predicate cache ID to be 10327 * the predicate cache ID of the remaining ECB. 10328 */ 10329 ASSERT(probe->dtpr_ecb_last != NULL); 10330 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 10331 10332 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 10333 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 10334 10335 ASSERT(probe->dtpr_ecb->dte_next == NULL); 10336 10337 if (p != NULL) 10338 probe->dtpr_predcache = p->dtp_cacheid; 10339 } 10340 10341 ecb->dte_next = NULL; 10342 } 10343 } 10344 10345 static void 10346 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 10347 { 10348 dtrace_state_t *state = ecb->dte_state; 10349 dtrace_vstate_t *vstate = &state->dts_vstate; 10350 dtrace_predicate_t *pred; 10351 dtrace_epid_t epid = ecb->dte_epid; 10352 10353 ASSERT(MUTEX_HELD(&dtrace_lock)); 10354 ASSERT(ecb->dte_next == NULL); 10355 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 10356 10357 if ((pred = ecb->dte_predicate) != NULL) 10358 dtrace_predicate_release(pred, vstate); 10359 10360 dtrace_ecb_action_remove(ecb); 10361 10362 ASSERT(state->dts_ecbs[epid - 1] == ecb); 10363 state->dts_ecbs[epid - 1] = NULL; 10364 10365 kmem_free(ecb, sizeof (dtrace_ecb_t)); 10366 } 10367 10368 static dtrace_ecb_t * 10369 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 10370 dtrace_enabling_t *enab) 10371 { 10372 dtrace_ecb_t *ecb; 10373 dtrace_predicate_t *pred; 10374 dtrace_actdesc_t *act; 10375 dtrace_provider_t *prov; 10376 dtrace_ecbdesc_t *desc = enab->dten_current; 10377 10378 ASSERT(MUTEX_HELD(&dtrace_lock)); 10379 ASSERT(state != NULL); 10380 10381 ecb = dtrace_ecb_add(state, probe); 10382 ecb->dte_uarg = desc->dted_uarg; 10383 10384 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 10385 dtrace_predicate_hold(pred); 10386 ecb->dte_predicate = pred; 10387 } 10388 10389 if (probe != NULL) { 10390 /* 10391 * If the provider shows more leg than the consumer is old 10392 * enough to see, we need to enable the appropriate implicit 10393 * predicate bits to prevent the ecb from activating at 10394 * revealing times. 10395 * 10396 * Providers specifying DTRACE_PRIV_USER at register time 10397 * are stating that they need the /proc-style privilege 10398 * model to be enforced, and this is what DTRACE_COND_OWNER 10399 * and DTRACE_COND_ZONEOWNER will then do at probe time. 10400 */ 10401 prov = probe->dtpr_provider; 10402 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 10403 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10404 ecb->dte_cond |= DTRACE_COND_OWNER; 10405 10406 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 10407 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10408 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 10409 10410 /* 10411 * If the provider shows us kernel innards and the user 10412 * is lacking sufficient privilege, enable the 10413 * DTRACE_COND_USERMODE implicit predicate. 10414 */ 10415 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 10416 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 10417 ecb->dte_cond |= DTRACE_COND_USERMODE; 10418 } 10419 10420 if (dtrace_ecb_create_cache != NULL) { 10421 /* 10422 * If we have a cached ecb, we'll use its action list instead 10423 * of creating our own (saving both time and space). 10424 */ 10425 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 10426 dtrace_action_t *act = cached->dte_action; 10427 10428 if (act != NULL) { 10429 ASSERT(act->dta_refcnt > 0); 10430 act->dta_refcnt++; 10431 ecb->dte_action = act; 10432 ecb->dte_action_last = cached->dte_action_last; 10433 ecb->dte_needed = cached->dte_needed; 10434 ecb->dte_size = cached->dte_size; 10435 ecb->dte_alignment = cached->dte_alignment; 10436 } 10437 10438 return (ecb); 10439 } 10440 10441 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 10442 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 10443 dtrace_ecb_destroy(ecb); 10444 return (NULL); 10445 } 10446 } 10447 10448 dtrace_ecb_resize(ecb); 10449 10450 return (dtrace_ecb_create_cache = ecb); 10451 } 10452 10453 static int 10454 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 10455 { 10456 dtrace_ecb_t *ecb; 10457 dtrace_enabling_t *enab = arg; 10458 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 10459 10460 ASSERT(state != NULL); 10461 10462 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 10463 /* 10464 * This probe was created in a generation for which this 10465 * enabling has previously created ECBs; we don't want to 10466 * enable it again, so just kick out. 10467 */ 10468 return (DTRACE_MATCH_NEXT); 10469 } 10470 10471 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 10472 return (DTRACE_MATCH_DONE); 10473 10474 if (dtrace_ecb_enable(ecb) < 0) 10475 return (DTRACE_MATCH_FAIL); 10476 10477 return (DTRACE_MATCH_NEXT); 10478 } 10479 10480 static dtrace_ecb_t * 10481 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 10482 { 10483 dtrace_ecb_t *ecb; 10484 10485 ASSERT(MUTEX_HELD(&dtrace_lock)); 10486 10487 if (id == 0 || id > state->dts_necbs) 10488 return (NULL); 10489 10490 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 10491 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 10492 10493 return (state->dts_ecbs[id - 1]); 10494 } 10495 10496 static dtrace_aggregation_t * 10497 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 10498 { 10499 dtrace_aggregation_t *agg; 10500 10501 ASSERT(MUTEX_HELD(&dtrace_lock)); 10502 10503 if (id == 0 || id > state->dts_naggregations) 10504 return (NULL); 10505 10506 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 10507 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 10508 agg->dtag_id == id); 10509 10510 return (state->dts_aggregations[id - 1]); 10511 } 10512 10513 /* 10514 * DTrace Buffer Functions 10515 * 10516 * The following functions manipulate DTrace buffers. Most of these functions 10517 * are called in the context of establishing or processing consumer state; 10518 * exceptions are explicitly noted. 10519 */ 10520 10521 /* 10522 * Note: called from cross call context. This function switches the two 10523 * buffers on a given CPU. The atomicity of this operation is assured by 10524 * disabling interrupts while the actual switch takes place; the disabling of 10525 * interrupts serializes the execution with any execution of dtrace_probe() on 10526 * the same CPU. 10527 */ 10528 static void 10529 dtrace_buffer_switch(dtrace_buffer_t *buf) 10530 { 10531 caddr_t tomax = buf->dtb_tomax; 10532 caddr_t xamot = buf->dtb_xamot; 10533 dtrace_icookie_t cookie; 10534 hrtime_t now; 10535 10536 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10537 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 10538 10539 cookie = dtrace_interrupt_disable(); 10540 now = dtrace_gethrtime(); 10541 buf->dtb_tomax = xamot; 10542 buf->dtb_xamot = tomax; 10543 buf->dtb_xamot_drops = buf->dtb_drops; 10544 buf->dtb_xamot_offset = buf->dtb_offset; 10545 buf->dtb_xamot_errors = buf->dtb_errors; 10546 buf->dtb_xamot_flags = buf->dtb_flags; 10547 buf->dtb_offset = 0; 10548 buf->dtb_drops = 0; 10549 buf->dtb_errors = 0; 10550 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 10551 buf->dtb_interval = now - buf->dtb_switched; 10552 buf->dtb_switched = now; 10553 dtrace_interrupt_enable(cookie); 10554 } 10555 10556 /* 10557 * Note: called from cross call context. This function activates a buffer 10558 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 10559 * is guaranteed by the disabling of interrupts. 10560 */ 10561 static void 10562 dtrace_buffer_activate(dtrace_state_t *state) 10563 { 10564 dtrace_buffer_t *buf; 10565 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 10566 10567 buf = &state->dts_buffer[CPU->cpu_id]; 10568 10569 if (buf->dtb_tomax != NULL) { 10570 /* 10571 * We might like to assert that the buffer is marked inactive, 10572 * but this isn't necessarily true: the buffer for the CPU 10573 * that processes the BEGIN probe has its buffer activated 10574 * manually. In this case, we take the (harmless) action 10575 * re-clearing the bit INACTIVE bit. 10576 */ 10577 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 10578 } 10579 10580 dtrace_interrupt_enable(cookie); 10581 } 10582 10583 static int 10584 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 10585 processorid_t cpu, int *factor) 10586 { 10587 cpu_t *cp; 10588 dtrace_buffer_t *buf; 10589 int allocated = 0, desired = 0; 10590 10591 ASSERT(MUTEX_HELD(&cpu_lock)); 10592 ASSERT(MUTEX_HELD(&dtrace_lock)); 10593 10594 *factor = 1; 10595 10596 if (size > dtrace_nonroot_maxsize && 10597 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 10598 return (EFBIG); 10599 10600 cp = cpu_list; 10601 10602 do { 10603 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10604 continue; 10605 10606 buf = &bufs[cp->cpu_id]; 10607 10608 /* 10609 * If there is already a buffer allocated for this CPU, it 10610 * is only possible that this is a DR event. In this case, 10611 * the buffer size must match our specified size. 10612 */ 10613 if (buf->dtb_tomax != NULL) { 10614 ASSERT(buf->dtb_size == size); 10615 continue; 10616 } 10617 10618 ASSERT(buf->dtb_xamot == NULL); 10619 10620 if ((buf->dtb_tomax = kmem_zalloc(size, 10621 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10622 goto err; 10623 10624 buf->dtb_size = size; 10625 buf->dtb_flags = flags; 10626 buf->dtb_offset = 0; 10627 buf->dtb_drops = 0; 10628 10629 if (flags & DTRACEBUF_NOSWITCH) 10630 continue; 10631 10632 if ((buf->dtb_xamot = kmem_zalloc(size, 10633 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10634 goto err; 10635 } while ((cp = cp->cpu_next) != cpu_list); 10636 10637 return (0); 10638 10639 err: 10640 cp = cpu_list; 10641 10642 do { 10643 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10644 continue; 10645 10646 buf = &bufs[cp->cpu_id]; 10647 desired += 2; 10648 10649 if (buf->dtb_xamot != NULL) { 10650 ASSERT(buf->dtb_tomax != NULL); 10651 ASSERT(buf->dtb_size == size); 10652 kmem_free(buf->dtb_xamot, size); 10653 allocated++; 10654 } 10655 10656 if (buf->dtb_tomax != NULL) { 10657 ASSERT(buf->dtb_size == size); 10658 kmem_free(buf->dtb_tomax, size); 10659 allocated++; 10660 } 10661 10662 buf->dtb_tomax = NULL; 10663 buf->dtb_xamot = NULL; 10664 buf->dtb_size = 0; 10665 } while ((cp = cp->cpu_next) != cpu_list); 10666 10667 *factor = desired / (allocated > 0 ? allocated : 1); 10668 10669 return (ENOMEM); 10670 } 10671 10672 /* 10673 * Note: called from probe context. This function just increments the drop 10674 * count on a buffer. It has been made a function to allow for the 10675 * possibility of understanding the source of mysterious drop counts. (A 10676 * problem for which one may be particularly disappointed that DTrace cannot 10677 * be used to understand DTrace.) 10678 */ 10679 static void 10680 dtrace_buffer_drop(dtrace_buffer_t *buf) 10681 { 10682 buf->dtb_drops++; 10683 } 10684 10685 /* 10686 * Note: called from probe context. This function is called to reserve space 10687 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 10688 * mstate. Returns the new offset in the buffer, or a negative value if an 10689 * error has occurred. 10690 */ 10691 static intptr_t 10692 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 10693 dtrace_state_t *state, dtrace_mstate_t *mstate) 10694 { 10695 intptr_t offs = buf->dtb_offset, soffs; 10696 intptr_t woffs; 10697 caddr_t tomax; 10698 size_t total; 10699 10700 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 10701 return (-1); 10702 10703 if ((tomax = buf->dtb_tomax) == NULL) { 10704 dtrace_buffer_drop(buf); 10705 return (-1); 10706 } 10707 10708 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 10709 while (offs & (align - 1)) { 10710 /* 10711 * Assert that our alignment is off by a number which 10712 * is itself sizeof (uint32_t) aligned. 10713 */ 10714 ASSERT(!((align - (offs & (align - 1))) & 10715 (sizeof (uint32_t) - 1))); 10716 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10717 offs += sizeof (uint32_t); 10718 } 10719 10720 if ((soffs = offs + needed) > buf->dtb_size) { 10721 dtrace_buffer_drop(buf); 10722 return (-1); 10723 } 10724 10725 if (mstate == NULL) 10726 return (offs); 10727 10728 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 10729 mstate->dtms_scratch_size = buf->dtb_size - soffs; 10730 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10731 10732 return (offs); 10733 } 10734 10735 if (buf->dtb_flags & DTRACEBUF_FILL) { 10736 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 10737 (buf->dtb_flags & DTRACEBUF_FULL)) 10738 return (-1); 10739 goto out; 10740 } 10741 10742 total = needed + (offs & (align - 1)); 10743 10744 /* 10745 * For a ring buffer, life is quite a bit more complicated. Before 10746 * we can store any padding, we need to adjust our wrapping offset. 10747 * (If we've never before wrapped or we're not about to, no adjustment 10748 * is required.) 10749 */ 10750 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 10751 offs + total > buf->dtb_size) { 10752 woffs = buf->dtb_xamot_offset; 10753 10754 if (offs + total > buf->dtb_size) { 10755 /* 10756 * We can't fit in the end of the buffer. First, a 10757 * sanity check that we can fit in the buffer at all. 10758 */ 10759 if (total > buf->dtb_size) { 10760 dtrace_buffer_drop(buf); 10761 return (-1); 10762 } 10763 10764 /* 10765 * We're going to be storing at the top of the buffer, 10766 * so now we need to deal with the wrapped offset. We 10767 * only reset our wrapped offset to 0 if it is 10768 * currently greater than the current offset. If it 10769 * is less than the current offset, it is because a 10770 * previous allocation induced a wrap -- but the 10771 * allocation didn't subsequently take the space due 10772 * to an error or false predicate evaluation. In this 10773 * case, we'll just leave the wrapped offset alone: if 10774 * the wrapped offset hasn't been advanced far enough 10775 * for this allocation, it will be adjusted in the 10776 * lower loop. 10777 */ 10778 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 10779 if (woffs >= offs) 10780 woffs = 0; 10781 } else { 10782 woffs = 0; 10783 } 10784 10785 /* 10786 * Now we know that we're going to be storing to the 10787 * top of the buffer and that there is room for us 10788 * there. We need to clear the buffer from the current 10789 * offset to the end (there may be old gunk there). 10790 */ 10791 while (offs < buf->dtb_size) 10792 tomax[offs++] = 0; 10793 10794 /* 10795 * We need to set our offset to zero. And because we 10796 * are wrapping, we need to set the bit indicating as 10797 * much. We can also adjust our needed space back 10798 * down to the space required by the ECB -- we know 10799 * that the top of the buffer is aligned. 10800 */ 10801 offs = 0; 10802 total = needed; 10803 buf->dtb_flags |= DTRACEBUF_WRAPPED; 10804 } else { 10805 /* 10806 * There is room for us in the buffer, so we simply 10807 * need to check the wrapped offset. 10808 */ 10809 if (woffs < offs) { 10810 /* 10811 * The wrapped offset is less than the offset. 10812 * This can happen if we allocated buffer space 10813 * that induced a wrap, but then we didn't 10814 * subsequently take the space due to an error 10815 * or false predicate evaluation. This is 10816 * okay; we know that _this_ allocation isn't 10817 * going to induce a wrap. We still can't 10818 * reset the wrapped offset to be zero, 10819 * however: the space may have been trashed in 10820 * the previous failed probe attempt. But at 10821 * least the wrapped offset doesn't need to 10822 * be adjusted at all... 10823 */ 10824 goto out; 10825 } 10826 } 10827 10828 while (offs + total > woffs) { 10829 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 10830 size_t size; 10831 10832 if (epid == DTRACE_EPIDNONE) { 10833 size = sizeof (uint32_t); 10834 } else { 10835 ASSERT3U(epid, <=, state->dts_necbs); 10836 ASSERT(state->dts_ecbs[epid - 1] != NULL); 10837 10838 size = state->dts_ecbs[epid - 1]->dte_size; 10839 } 10840 10841 ASSERT(woffs + size <= buf->dtb_size); 10842 ASSERT(size != 0); 10843 10844 if (woffs + size == buf->dtb_size) { 10845 /* 10846 * We've reached the end of the buffer; we want 10847 * to set the wrapped offset to 0 and break 10848 * out. However, if the offs is 0, then we're 10849 * in a strange edge-condition: the amount of 10850 * space that we want to reserve plus the size 10851 * of the record that we're overwriting is 10852 * greater than the size of the buffer. This 10853 * is problematic because if we reserve the 10854 * space but subsequently don't consume it (due 10855 * to a failed predicate or error) the wrapped 10856 * offset will be 0 -- yet the EPID at offset 0 10857 * will not be committed. This situation is 10858 * relatively easy to deal with: if we're in 10859 * this case, the buffer is indistinguishable 10860 * from one that hasn't wrapped; we need only 10861 * finish the job by clearing the wrapped bit, 10862 * explicitly setting the offset to be 0, and 10863 * zero'ing out the old data in the buffer. 10864 */ 10865 if (offs == 0) { 10866 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 10867 buf->dtb_offset = 0; 10868 woffs = total; 10869 10870 while (woffs < buf->dtb_size) 10871 tomax[woffs++] = 0; 10872 } 10873 10874 woffs = 0; 10875 break; 10876 } 10877 10878 woffs += size; 10879 } 10880 10881 /* 10882 * We have a wrapped offset. It may be that the wrapped offset 10883 * has become zero -- that's okay. 10884 */ 10885 buf->dtb_xamot_offset = woffs; 10886 } 10887 10888 out: 10889 /* 10890 * Now we can plow the buffer with any necessary padding. 10891 */ 10892 while (offs & (align - 1)) { 10893 /* 10894 * Assert that our alignment is off by a number which 10895 * is itself sizeof (uint32_t) aligned. 10896 */ 10897 ASSERT(!((align - (offs & (align - 1))) & 10898 (sizeof (uint32_t) - 1))); 10899 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10900 offs += sizeof (uint32_t); 10901 } 10902 10903 if (buf->dtb_flags & DTRACEBUF_FILL) { 10904 if (offs + needed > buf->dtb_size - state->dts_reserve) { 10905 buf->dtb_flags |= DTRACEBUF_FULL; 10906 return (-1); 10907 } 10908 } 10909 10910 if (mstate == NULL) 10911 return (offs); 10912 10913 /* 10914 * For ring buffers and fill buffers, the scratch space is always 10915 * the inactive buffer. 10916 */ 10917 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 10918 mstate->dtms_scratch_size = buf->dtb_size; 10919 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10920 10921 return (offs); 10922 } 10923 10924 static void 10925 dtrace_buffer_polish(dtrace_buffer_t *buf) 10926 { 10927 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 10928 ASSERT(MUTEX_HELD(&dtrace_lock)); 10929 10930 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 10931 return; 10932 10933 /* 10934 * We need to polish the ring buffer. There are three cases: 10935 * 10936 * - The first (and presumably most common) is that there is no gap 10937 * between the buffer offset and the wrapped offset. In this case, 10938 * there is nothing in the buffer that isn't valid data; we can 10939 * mark the buffer as polished and return. 10940 * 10941 * - The second (less common than the first but still more common 10942 * than the third) is that there is a gap between the buffer offset 10943 * and the wrapped offset, and the wrapped offset is larger than the 10944 * buffer offset. This can happen because of an alignment issue, or 10945 * can happen because of a call to dtrace_buffer_reserve() that 10946 * didn't subsequently consume the buffer space. In this case, 10947 * we need to zero the data from the buffer offset to the wrapped 10948 * offset. 10949 * 10950 * - The third (and least common) is that there is a gap between the 10951 * buffer offset and the wrapped offset, but the wrapped offset is 10952 * _less_ than the buffer offset. This can only happen because a 10953 * call to dtrace_buffer_reserve() induced a wrap, but the space 10954 * was not subsequently consumed. In this case, we need to zero the 10955 * space from the offset to the end of the buffer _and_ from the 10956 * top of the buffer to the wrapped offset. 10957 */ 10958 if (buf->dtb_offset < buf->dtb_xamot_offset) { 10959 bzero(buf->dtb_tomax + buf->dtb_offset, 10960 buf->dtb_xamot_offset - buf->dtb_offset); 10961 } 10962 10963 if (buf->dtb_offset > buf->dtb_xamot_offset) { 10964 bzero(buf->dtb_tomax + buf->dtb_offset, 10965 buf->dtb_size - buf->dtb_offset); 10966 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 10967 } 10968 } 10969 10970 /* 10971 * This routine determines if data generated at the specified time has likely 10972 * been entirely consumed at user-level. This routine is called to determine 10973 * if an ECB on a defunct probe (but for an active enabling) can be safely 10974 * disabled and destroyed. 10975 */ 10976 static int 10977 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 10978 { 10979 int i; 10980 10981 for (i = 0; i < NCPU; i++) { 10982 dtrace_buffer_t *buf = &bufs[i]; 10983 10984 if (buf->dtb_size == 0) 10985 continue; 10986 10987 if (buf->dtb_flags & DTRACEBUF_RING) 10988 return (0); 10989 10990 if (!buf->dtb_switched && buf->dtb_offset != 0) 10991 return (0); 10992 10993 if (buf->dtb_switched - buf->dtb_interval < when) 10994 return (0); 10995 } 10996 10997 return (1); 10998 } 10999 11000 static void 11001 dtrace_buffer_free(dtrace_buffer_t *bufs) 11002 { 11003 int i; 11004 11005 for (i = 0; i < NCPU; i++) { 11006 dtrace_buffer_t *buf = &bufs[i]; 11007 11008 if (buf->dtb_tomax == NULL) { 11009 ASSERT(buf->dtb_xamot == NULL); 11010 ASSERT(buf->dtb_size == 0); 11011 continue; 11012 } 11013 11014 if (buf->dtb_xamot != NULL) { 11015 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11016 kmem_free(buf->dtb_xamot, buf->dtb_size); 11017 } 11018 11019 kmem_free(buf->dtb_tomax, buf->dtb_size); 11020 buf->dtb_size = 0; 11021 buf->dtb_tomax = NULL; 11022 buf->dtb_xamot = NULL; 11023 } 11024 } 11025 11026 /* 11027 * DTrace Enabling Functions 11028 */ 11029 static dtrace_enabling_t * 11030 dtrace_enabling_create(dtrace_vstate_t *vstate) 11031 { 11032 dtrace_enabling_t *enab; 11033 11034 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 11035 enab->dten_vstate = vstate; 11036 11037 return (enab); 11038 } 11039 11040 static void 11041 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 11042 { 11043 dtrace_ecbdesc_t **ndesc; 11044 size_t osize, nsize; 11045 11046 /* 11047 * We can't add to enablings after we've enabled them, or after we've 11048 * retained them. 11049 */ 11050 ASSERT(enab->dten_probegen == 0); 11051 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11052 11053 if (enab->dten_ndesc < enab->dten_maxdesc) { 11054 enab->dten_desc[enab->dten_ndesc++] = ecb; 11055 return; 11056 } 11057 11058 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11059 11060 if (enab->dten_maxdesc == 0) { 11061 enab->dten_maxdesc = 1; 11062 } else { 11063 enab->dten_maxdesc <<= 1; 11064 } 11065 11066 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 11067 11068 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11069 ndesc = kmem_zalloc(nsize, KM_SLEEP); 11070 bcopy(enab->dten_desc, ndesc, osize); 11071 kmem_free(enab->dten_desc, osize); 11072 11073 enab->dten_desc = ndesc; 11074 enab->dten_desc[enab->dten_ndesc++] = ecb; 11075 } 11076 11077 static void 11078 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 11079 dtrace_probedesc_t *pd) 11080 { 11081 dtrace_ecbdesc_t *new; 11082 dtrace_predicate_t *pred; 11083 dtrace_actdesc_t *act; 11084 11085 /* 11086 * We're going to create a new ECB description that matches the 11087 * specified ECB in every way, but has the specified probe description. 11088 */ 11089 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 11090 11091 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 11092 dtrace_predicate_hold(pred); 11093 11094 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 11095 dtrace_actdesc_hold(act); 11096 11097 new->dted_action = ecb->dted_action; 11098 new->dted_pred = ecb->dted_pred; 11099 new->dted_probe = *pd; 11100 new->dted_uarg = ecb->dted_uarg; 11101 11102 dtrace_enabling_add(enab, new); 11103 } 11104 11105 static void 11106 dtrace_enabling_dump(dtrace_enabling_t *enab) 11107 { 11108 int i; 11109 11110 for (i = 0; i < enab->dten_ndesc; i++) { 11111 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 11112 11113 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 11114 desc->dtpd_provider, desc->dtpd_mod, 11115 desc->dtpd_func, desc->dtpd_name); 11116 } 11117 } 11118 11119 static void 11120 dtrace_enabling_destroy(dtrace_enabling_t *enab) 11121 { 11122 int i; 11123 dtrace_ecbdesc_t *ep; 11124 dtrace_vstate_t *vstate = enab->dten_vstate; 11125 11126 ASSERT(MUTEX_HELD(&dtrace_lock)); 11127 11128 for (i = 0; i < enab->dten_ndesc; i++) { 11129 dtrace_actdesc_t *act, *next; 11130 dtrace_predicate_t *pred; 11131 11132 ep = enab->dten_desc[i]; 11133 11134 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 11135 dtrace_predicate_release(pred, vstate); 11136 11137 for (act = ep->dted_action; act != NULL; act = next) { 11138 next = act->dtad_next; 11139 dtrace_actdesc_release(act, vstate); 11140 } 11141 11142 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 11143 } 11144 11145 kmem_free(enab->dten_desc, 11146 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 11147 11148 /* 11149 * If this was a retained enabling, decrement the dts_nretained count 11150 * and take it off of the dtrace_retained list. 11151 */ 11152 if (enab->dten_prev != NULL || enab->dten_next != NULL || 11153 dtrace_retained == enab) { 11154 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11155 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 11156 enab->dten_vstate->dtvs_state->dts_nretained--; 11157 dtrace_retained_gen++; 11158 } 11159 11160 if (enab->dten_prev == NULL) { 11161 if (dtrace_retained == enab) { 11162 dtrace_retained = enab->dten_next; 11163 11164 if (dtrace_retained != NULL) 11165 dtrace_retained->dten_prev = NULL; 11166 } 11167 } else { 11168 ASSERT(enab != dtrace_retained); 11169 ASSERT(dtrace_retained != NULL); 11170 enab->dten_prev->dten_next = enab->dten_next; 11171 } 11172 11173 if (enab->dten_next != NULL) { 11174 ASSERT(dtrace_retained != NULL); 11175 enab->dten_next->dten_prev = enab->dten_prev; 11176 } 11177 11178 kmem_free(enab, sizeof (dtrace_enabling_t)); 11179 } 11180 11181 static int 11182 dtrace_enabling_retain(dtrace_enabling_t *enab) 11183 { 11184 dtrace_state_t *state; 11185 11186 ASSERT(MUTEX_HELD(&dtrace_lock)); 11187 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11188 ASSERT(enab->dten_vstate != NULL); 11189 11190 state = enab->dten_vstate->dtvs_state; 11191 ASSERT(state != NULL); 11192 11193 /* 11194 * We only allow each state to retain dtrace_retain_max enablings. 11195 */ 11196 if (state->dts_nretained >= dtrace_retain_max) 11197 return (ENOSPC); 11198 11199 state->dts_nretained++; 11200 dtrace_retained_gen++; 11201 11202 if (dtrace_retained == NULL) { 11203 dtrace_retained = enab; 11204 return (0); 11205 } 11206 11207 enab->dten_next = dtrace_retained; 11208 dtrace_retained->dten_prev = enab; 11209 dtrace_retained = enab; 11210 11211 return (0); 11212 } 11213 11214 static int 11215 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 11216 dtrace_probedesc_t *create) 11217 { 11218 dtrace_enabling_t *new, *enab; 11219 int found = 0, err = ENOENT; 11220 11221 ASSERT(MUTEX_HELD(&dtrace_lock)); 11222 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 11223 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 11224 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 11225 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 11226 11227 new = dtrace_enabling_create(&state->dts_vstate); 11228 11229 /* 11230 * Iterate over all retained enablings, looking for enablings that 11231 * match the specified state. 11232 */ 11233 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11234 int i; 11235 11236 /* 11237 * dtvs_state can only be NULL for helper enablings -- and 11238 * helper enablings can't be retained. 11239 */ 11240 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11241 11242 if (enab->dten_vstate->dtvs_state != state) 11243 continue; 11244 11245 /* 11246 * Now iterate over each probe description; we're looking for 11247 * an exact match to the specified probe description. 11248 */ 11249 for (i = 0; i < enab->dten_ndesc; i++) { 11250 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11251 dtrace_probedesc_t *pd = &ep->dted_probe; 11252 11253 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 11254 continue; 11255 11256 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 11257 continue; 11258 11259 if (strcmp(pd->dtpd_func, match->dtpd_func)) 11260 continue; 11261 11262 if (strcmp(pd->dtpd_name, match->dtpd_name)) 11263 continue; 11264 11265 /* 11266 * We have a winning probe! Add it to our growing 11267 * enabling. 11268 */ 11269 found = 1; 11270 dtrace_enabling_addlike(new, ep, create); 11271 } 11272 } 11273 11274 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 11275 dtrace_enabling_destroy(new); 11276 return (err); 11277 } 11278 11279 return (0); 11280 } 11281 11282 static void 11283 dtrace_enabling_retract(dtrace_state_t *state) 11284 { 11285 dtrace_enabling_t *enab, *next; 11286 11287 ASSERT(MUTEX_HELD(&dtrace_lock)); 11288 11289 /* 11290 * Iterate over all retained enablings, destroy the enablings retained 11291 * for the specified state. 11292 */ 11293 for (enab = dtrace_retained; enab != NULL; enab = next) { 11294 next = enab->dten_next; 11295 11296 /* 11297 * dtvs_state can only be NULL for helper enablings -- and 11298 * helper enablings can't be retained. 11299 */ 11300 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11301 11302 if (enab->dten_vstate->dtvs_state == state) { 11303 ASSERT(state->dts_nretained > 0); 11304 dtrace_enabling_destroy(enab); 11305 } 11306 } 11307 11308 ASSERT(state->dts_nretained == 0); 11309 } 11310 11311 static int 11312 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 11313 { 11314 int i = 0; 11315 int total_matched = 0, matched = 0; 11316 11317 ASSERT(MUTEX_HELD(&cpu_lock)); 11318 ASSERT(MUTEX_HELD(&dtrace_lock)); 11319 11320 for (i = 0; i < enab->dten_ndesc; i++) { 11321 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11322 11323 enab->dten_current = ep; 11324 enab->dten_error = 0; 11325 11326 /* 11327 * If a provider failed to enable a probe then get out and 11328 * let the consumer know we failed. 11329 */ 11330 if ((matched = dtrace_probe_enable(&ep->dted_probe, enab)) < 0) 11331 return (EBUSY); 11332 11333 total_matched += matched; 11334 11335 if (enab->dten_error != 0) { 11336 /* 11337 * If we get an error half-way through enabling the 11338 * probes, we kick out -- perhaps with some number of 11339 * them enabled. Leaving enabled probes enabled may 11340 * be slightly confusing for user-level, but we expect 11341 * that no one will attempt to actually drive on in 11342 * the face of such errors. If this is an anonymous 11343 * enabling (indicated with a NULL nmatched pointer), 11344 * we cmn_err() a message. We aren't expecting to 11345 * get such an error -- such as it can exist at all, 11346 * it would be a result of corrupted DOF in the driver 11347 * properties. 11348 */ 11349 if (nmatched == NULL) { 11350 cmn_err(CE_WARN, "dtrace_enabling_match() " 11351 "error on %p: %d", (void *)ep, 11352 enab->dten_error); 11353 } 11354 11355 return (enab->dten_error); 11356 } 11357 } 11358 11359 enab->dten_probegen = dtrace_probegen; 11360 if (nmatched != NULL) 11361 *nmatched = total_matched; 11362 11363 return (0); 11364 } 11365 11366 static void 11367 dtrace_enabling_matchall(void) 11368 { 11369 dtrace_enabling_t *enab; 11370 11371 mutex_enter(&cpu_lock); 11372 mutex_enter(&dtrace_lock); 11373 11374 /* 11375 * Iterate over all retained enablings to see if any probes match 11376 * against them. We only perform this operation on enablings for which 11377 * we have sufficient permissions by virtue of being in the global zone 11378 * or in the same zone as the DTrace client. Because we can be called 11379 * after dtrace_detach() has been called, we cannot assert that there 11380 * are retained enablings. We can safely load from dtrace_retained, 11381 * however: the taskq_destroy() at the end of dtrace_detach() will 11382 * block pending our completion. 11383 */ 11384 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11385 dtrace_cred_t *dcr = &enab->dten_vstate->dtvs_state->dts_cred; 11386 cred_t *cr = dcr->dcr_cred; 11387 zoneid_t zone = cr != NULL ? crgetzoneid(cr) : 0; 11388 11389 if ((dcr->dcr_visible & DTRACE_CRV_ALLZONE) || (cr != NULL && 11390 (zone == GLOBAL_ZONEID || getzoneid() == zone))) 11391 (void) dtrace_enabling_match(enab, NULL); 11392 } 11393 11394 mutex_exit(&dtrace_lock); 11395 mutex_exit(&cpu_lock); 11396 } 11397 11398 /* 11399 * If an enabling is to be enabled without having matched probes (that is, if 11400 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 11401 * enabling must be _primed_ by creating an ECB for every ECB description. 11402 * This must be done to assure that we know the number of speculations, the 11403 * number of aggregations, the minimum buffer size needed, etc. before we 11404 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 11405 * enabling any probes, we create ECBs for every ECB decription, but with a 11406 * NULL probe -- which is exactly what this function does. 11407 */ 11408 static void 11409 dtrace_enabling_prime(dtrace_state_t *state) 11410 { 11411 dtrace_enabling_t *enab; 11412 int i; 11413 11414 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11415 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11416 11417 if (enab->dten_vstate->dtvs_state != state) 11418 continue; 11419 11420 /* 11421 * We don't want to prime an enabling more than once, lest 11422 * we allow a malicious user to induce resource exhaustion. 11423 * (The ECBs that result from priming an enabling aren't 11424 * leaked -- but they also aren't deallocated until the 11425 * consumer state is destroyed.) 11426 */ 11427 if (enab->dten_primed) 11428 continue; 11429 11430 for (i = 0; i < enab->dten_ndesc; i++) { 11431 enab->dten_current = enab->dten_desc[i]; 11432 (void) dtrace_probe_enable(NULL, enab); 11433 } 11434 11435 enab->dten_primed = 1; 11436 } 11437 } 11438 11439 /* 11440 * Called to indicate that probes should be provided due to retained 11441 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 11442 * must take an initial lap through the enabling calling the dtps_provide() 11443 * entry point explicitly to allow for autocreated probes. 11444 */ 11445 static void 11446 dtrace_enabling_provide(dtrace_provider_t *prv) 11447 { 11448 int i, all = 0; 11449 dtrace_probedesc_t desc; 11450 dtrace_genid_t gen; 11451 11452 ASSERT(MUTEX_HELD(&dtrace_lock)); 11453 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 11454 11455 if (prv == NULL) { 11456 all = 1; 11457 prv = dtrace_provider; 11458 } 11459 11460 do { 11461 dtrace_enabling_t *enab; 11462 void *parg = prv->dtpv_arg; 11463 11464 retry: 11465 gen = dtrace_retained_gen; 11466 for (enab = dtrace_retained; enab != NULL; 11467 enab = enab->dten_next) { 11468 for (i = 0; i < enab->dten_ndesc; i++) { 11469 desc = enab->dten_desc[i]->dted_probe; 11470 mutex_exit(&dtrace_lock); 11471 prv->dtpv_pops.dtps_provide(parg, &desc); 11472 mutex_enter(&dtrace_lock); 11473 /* 11474 * Process the retained enablings again if 11475 * they have changed while we weren't holding 11476 * dtrace_lock. 11477 */ 11478 if (gen != dtrace_retained_gen) 11479 goto retry; 11480 } 11481 } 11482 } while (all && (prv = prv->dtpv_next) != NULL); 11483 11484 mutex_exit(&dtrace_lock); 11485 dtrace_probe_provide(NULL, all ? NULL : prv); 11486 mutex_enter(&dtrace_lock); 11487 } 11488 11489 /* 11490 * Called to reap ECBs that are attached to probes from defunct providers. 11491 */ 11492 static void 11493 dtrace_enabling_reap(void) 11494 { 11495 dtrace_provider_t *prov; 11496 dtrace_probe_t *probe; 11497 dtrace_ecb_t *ecb; 11498 hrtime_t when; 11499 int i; 11500 11501 mutex_enter(&cpu_lock); 11502 mutex_enter(&dtrace_lock); 11503 11504 for (i = 0; i < dtrace_nprobes; i++) { 11505 if ((probe = dtrace_probes[i]) == NULL) 11506 continue; 11507 11508 if (probe->dtpr_ecb == NULL) 11509 continue; 11510 11511 prov = probe->dtpr_provider; 11512 11513 if ((when = prov->dtpv_defunct) == 0) 11514 continue; 11515 11516 /* 11517 * We have ECBs on a defunct provider: we want to reap these 11518 * ECBs to allow the provider to unregister. The destruction 11519 * of these ECBs must be done carefully: if we destroy the ECB 11520 * and the consumer later wishes to consume an EPID that 11521 * corresponds to the destroyed ECB (and if the EPID metadata 11522 * has not been previously consumed), the consumer will abort 11523 * processing on the unknown EPID. To reduce (but not, sadly, 11524 * eliminate) the possibility of this, we will only destroy an 11525 * ECB for a defunct provider if, for the state that 11526 * corresponds to the ECB: 11527 * 11528 * (a) There is no speculative tracing (which can effectively 11529 * cache an EPID for an arbitrary amount of time). 11530 * 11531 * (b) The principal buffers have been switched twice since the 11532 * provider became defunct. 11533 * 11534 * (c) The aggregation buffers are of zero size or have been 11535 * switched twice since the provider became defunct. 11536 * 11537 * We use dts_speculates to determine (a) and call a function 11538 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 11539 * that as soon as we've been unable to destroy one of the ECBs 11540 * associated with the probe, we quit trying -- reaping is only 11541 * fruitful in as much as we can destroy all ECBs associated 11542 * with the defunct provider's probes. 11543 */ 11544 while ((ecb = probe->dtpr_ecb) != NULL) { 11545 dtrace_state_t *state = ecb->dte_state; 11546 dtrace_buffer_t *buf = state->dts_buffer; 11547 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 11548 11549 if (state->dts_speculates) 11550 break; 11551 11552 if (!dtrace_buffer_consumed(buf, when)) 11553 break; 11554 11555 if (!dtrace_buffer_consumed(aggbuf, when)) 11556 break; 11557 11558 dtrace_ecb_disable(ecb); 11559 ASSERT(probe->dtpr_ecb != ecb); 11560 dtrace_ecb_destroy(ecb); 11561 } 11562 } 11563 11564 mutex_exit(&dtrace_lock); 11565 mutex_exit(&cpu_lock); 11566 } 11567 11568 /* 11569 * DTrace DOF Functions 11570 */ 11571 /*ARGSUSED*/ 11572 static void 11573 dtrace_dof_error(dof_hdr_t *dof, const char *str) 11574 { 11575 if (dtrace_err_verbose) 11576 cmn_err(CE_WARN, "failed to process DOF: %s", str); 11577 11578 #ifdef DTRACE_ERRDEBUG 11579 dtrace_errdebug(str); 11580 #endif 11581 } 11582 11583 /* 11584 * Create DOF out of a currently enabled state. Right now, we only create 11585 * DOF containing the run-time options -- but this could be expanded to create 11586 * complete DOF representing the enabled state. 11587 */ 11588 static dof_hdr_t * 11589 dtrace_dof_create(dtrace_state_t *state) 11590 { 11591 dof_hdr_t *dof; 11592 dof_sec_t *sec; 11593 dof_optdesc_t *opt; 11594 int i, len = sizeof (dof_hdr_t) + 11595 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 11596 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11597 11598 ASSERT(MUTEX_HELD(&dtrace_lock)); 11599 11600 dof = kmem_zalloc(len, KM_SLEEP); 11601 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 11602 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 11603 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 11604 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 11605 11606 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 11607 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 11608 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 11609 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 11610 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 11611 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 11612 11613 dof->dofh_flags = 0; 11614 dof->dofh_hdrsize = sizeof (dof_hdr_t); 11615 dof->dofh_secsize = sizeof (dof_sec_t); 11616 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 11617 dof->dofh_secoff = sizeof (dof_hdr_t); 11618 dof->dofh_loadsz = len; 11619 dof->dofh_filesz = len; 11620 dof->dofh_pad = 0; 11621 11622 /* 11623 * Fill in the option section header... 11624 */ 11625 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 11626 sec->dofs_type = DOF_SECT_OPTDESC; 11627 sec->dofs_align = sizeof (uint64_t); 11628 sec->dofs_flags = DOF_SECF_LOAD; 11629 sec->dofs_entsize = sizeof (dof_optdesc_t); 11630 11631 opt = (dof_optdesc_t *)((uintptr_t)sec + 11632 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 11633 11634 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 11635 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11636 11637 for (i = 0; i < DTRACEOPT_MAX; i++) { 11638 opt[i].dofo_option = i; 11639 opt[i].dofo_strtab = DOF_SECIDX_NONE; 11640 opt[i].dofo_value = state->dts_options[i]; 11641 } 11642 11643 return (dof); 11644 } 11645 11646 static dof_hdr_t * 11647 dtrace_dof_copyin(uintptr_t uarg, int *errp) 11648 { 11649 dof_hdr_t hdr, *dof; 11650 11651 ASSERT(!MUTEX_HELD(&dtrace_lock)); 11652 11653 /* 11654 * First, we're going to copyin() the sizeof (dof_hdr_t). 11655 */ 11656 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 11657 dtrace_dof_error(NULL, "failed to copyin DOF header"); 11658 *errp = EFAULT; 11659 return (NULL); 11660 } 11661 11662 /* 11663 * Now we'll allocate the entire DOF and copy it in -- provided 11664 * that the length isn't outrageous. 11665 */ 11666 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 11667 dtrace_dof_error(&hdr, "load size exceeds maximum"); 11668 *errp = E2BIG; 11669 return (NULL); 11670 } 11671 11672 if (hdr.dofh_loadsz < sizeof (hdr)) { 11673 dtrace_dof_error(&hdr, "invalid load size"); 11674 *errp = EINVAL; 11675 return (NULL); 11676 } 11677 11678 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 11679 11680 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 11681 dof->dofh_loadsz != hdr.dofh_loadsz) { 11682 kmem_free(dof, hdr.dofh_loadsz); 11683 *errp = EFAULT; 11684 return (NULL); 11685 } 11686 11687 return (dof); 11688 } 11689 11690 static dof_hdr_t * 11691 dtrace_dof_property(const char *name) 11692 { 11693 uchar_t *buf; 11694 uint64_t loadsz; 11695 unsigned int len, i; 11696 dof_hdr_t *dof; 11697 11698 /* 11699 * Unfortunately, array of values in .conf files are always (and 11700 * only) interpreted to be integer arrays. We must read our DOF 11701 * as an integer array, and then squeeze it into a byte array. 11702 */ 11703 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 11704 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 11705 return (NULL); 11706 11707 for (i = 0; i < len; i++) 11708 buf[i] = (uchar_t)(((int *)buf)[i]); 11709 11710 if (len < sizeof (dof_hdr_t)) { 11711 ddi_prop_free(buf); 11712 dtrace_dof_error(NULL, "truncated header"); 11713 return (NULL); 11714 } 11715 11716 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 11717 ddi_prop_free(buf); 11718 dtrace_dof_error(NULL, "truncated DOF"); 11719 return (NULL); 11720 } 11721 11722 if (loadsz >= dtrace_dof_maxsize) { 11723 ddi_prop_free(buf); 11724 dtrace_dof_error(NULL, "oversized DOF"); 11725 return (NULL); 11726 } 11727 11728 dof = kmem_alloc(loadsz, KM_SLEEP); 11729 bcopy(buf, dof, loadsz); 11730 ddi_prop_free(buf); 11731 11732 return (dof); 11733 } 11734 11735 static void 11736 dtrace_dof_destroy(dof_hdr_t *dof) 11737 { 11738 kmem_free(dof, dof->dofh_loadsz); 11739 } 11740 11741 /* 11742 * Return the dof_sec_t pointer corresponding to a given section index. If the 11743 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 11744 * a type other than DOF_SECT_NONE is specified, the header is checked against 11745 * this type and NULL is returned if the types do not match. 11746 */ 11747 static dof_sec_t * 11748 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 11749 { 11750 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 11751 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 11752 11753 if (i >= dof->dofh_secnum) { 11754 dtrace_dof_error(dof, "referenced section index is invalid"); 11755 return (NULL); 11756 } 11757 11758 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 11759 dtrace_dof_error(dof, "referenced section is not loadable"); 11760 return (NULL); 11761 } 11762 11763 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 11764 dtrace_dof_error(dof, "referenced section is the wrong type"); 11765 return (NULL); 11766 } 11767 11768 return (sec); 11769 } 11770 11771 static dtrace_probedesc_t * 11772 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 11773 { 11774 dof_probedesc_t *probe; 11775 dof_sec_t *strtab; 11776 uintptr_t daddr = (uintptr_t)dof; 11777 uintptr_t str; 11778 size_t size; 11779 11780 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 11781 dtrace_dof_error(dof, "invalid probe section"); 11782 return (NULL); 11783 } 11784 11785 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11786 dtrace_dof_error(dof, "bad alignment in probe description"); 11787 return (NULL); 11788 } 11789 11790 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 11791 dtrace_dof_error(dof, "truncated probe description"); 11792 return (NULL); 11793 } 11794 11795 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 11796 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 11797 11798 if (strtab == NULL) 11799 return (NULL); 11800 11801 str = daddr + strtab->dofs_offset; 11802 size = strtab->dofs_size; 11803 11804 if (probe->dofp_provider >= strtab->dofs_size) { 11805 dtrace_dof_error(dof, "corrupt probe provider"); 11806 return (NULL); 11807 } 11808 11809 (void) strncpy(desc->dtpd_provider, 11810 (char *)(str + probe->dofp_provider), 11811 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 11812 11813 if (probe->dofp_mod >= strtab->dofs_size) { 11814 dtrace_dof_error(dof, "corrupt probe module"); 11815 return (NULL); 11816 } 11817 11818 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 11819 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 11820 11821 if (probe->dofp_func >= strtab->dofs_size) { 11822 dtrace_dof_error(dof, "corrupt probe function"); 11823 return (NULL); 11824 } 11825 11826 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 11827 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 11828 11829 if (probe->dofp_name >= strtab->dofs_size) { 11830 dtrace_dof_error(dof, "corrupt probe name"); 11831 return (NULL); 11832 } 11833 11834 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 11835 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 11836 11837 return (desc); 11838 } 11839 11840 static dtrace_difo_t * 11841 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11842 cred_t *cr) 11843 { 11844 dtrace_difo_t *dp; 11845 size_t ttl = 0; 11846 dof_difohdr_t *dofd; 11847 uintptr_t daddr = (uintptr_t)dof; 11848 size_t max = dtrace_difo_maxsize; 11849 int i, l, n; 11850 11851 static const struct { 11852 int section; 11853 int bufoffs; 11854 int lenoffs; 11855 int entsize; 11856 int align; 11857 const char *msg; 11858 } difo[] = { 11859 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 11860 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 11861 sizeof (dif_instr_t), "multiple DIF sections" }, 11862 11863 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 11864 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 11865 sizeof (uint64_t), "multiple integer tables" }, 11866 11867 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 11868 offsetof(dtrace_difo_t, dtdo_strlen), 0, 11869 sizeof (char), "multiple string tables" }, 11870 11871 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 11872 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 11873 sizeof (uint_t), "multiple variable tables" }, 11874 11875 { DOF_SECT_NONE, 0, 0, 0, NULL } 11876 }; 11877 11878 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 11879 dtrace_dof_error(dof, "invalid DIFO header section"); 11880 return (NULL); 11881 } 11882 11883 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11884 dtrace_dof_error(dof, "bad alignment in DIFO header"); 11885 return (NULL); 11886 } 11887 11888 if (sec->dofs_size < sizeof (dof_difohdr_t) || 11889 sec->dofs_size % sizeof (dof_secidx_t)) { 11890 dtrace_dof_error(dof, "bad size in DIFO header"); 11891 return (NULL); 11892 } 11893 11894 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 11895 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 11896 11897 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 11898 dp->dtdo_rtype = dofd->dofd_rtype; 11899 11900 for (l = 0; l < n; l++) { 11901 dof_sec_t *subsec; 11902 void **bufp; 11903 uint32_t *lenp; 11904 11905 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 11906 dofd->dofd_links[l])) == NULL) 11907 goto err; /* invalid section link */ 11908 11909 if (ttl + subsec->dofs_size > max) { 11910 dtrace_dof_error(dof, "exceeds maximum size"); 11911 goto err; 11912 } 11913 11914 ttl += subsec->dofs_size; 11915 11916 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 11917 if (subsec->dofs_type != difo[i].section) 11918 continue; 11919 11920 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 11921 dtrace_dof_error(dof, "section not loaded"); 11922 goto err; 11923 } 11924 11925 if (subsec->dofs_align != difo[i].align) { 11926 dtrace_dof_error(dof, "bad alignment"); 11927 goto err; 11928 } 11929 11930 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 11931 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 11932 11933 if (*bufp != NULL) { 11934 dtrace_dof_error(dof, difo[i].msg); 11935 goto err; 11936 } 11937 11938 if (difo[i].entsize != subsec->dofs_entsize) { 11939 dtrace_dof_error(dof, "entry size mismatch"); 11940 goto err; 11941 } 11942 11943 if (subsec->dofs_entsize != 0 && 11944 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 11945 dtrace_dof_error(dof, "corrupt entry size"); 11946 goto err; 11947 } 11948 11949 *lenp = subsec->dofs_size; 11950 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 11951 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 11952 *bufp, subsec->dofs_size); 11953 11954 if (subsec->dofs_entsize != 0) 11955 *lenp /= subsec->dofs_entsize; 11956 11957 break; 11958 } 11959 11960 /* 11961 * If we encounter a loadable DIFO sub-section that is not 11962 * known to us, assume this is a broken program and fail. 11963 */ 11964 if (difo[i].section == DOF_SECT_NONE && 11965 (subsec->dofs_flags & DOF_SECF_LOAD)) { 11966 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 11967 goto err; 11968 } 11969 } 11970 11971 if (dp->dtdo_buf == NULL) { 11972 /* 11973 * We can't have a DIF object without DIF text. 11974 */ 11975 dtrace_dof_error(dof, "missing DIF text"); 11976 goto err; 11977 } 11978 11979 /* 11980 * Before we validate the DIF object, run through the variable table 11981 * looking for the strings -- if any of their size are under, we'll set 11982 * their size to be the system-wide default string size. Note that 11983 * this should _not_ happen if the "strsize" option has been set -- 11984 * in this case, the compiler should have set the size to reflect the 11985 * setting of the option. 11986 */ 11987 for (i = 0; i < dp->dtdo_varlen; i++) { 11988 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 11989 dtrace_diftype_t *t = &v->dtdv_type; 11990 11991 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 11992 continue; 11993 11994 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 11995 t->dtdt_size = dtrace_strsize_default; 11996 } 11997 11998 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 11999 goto err; 12000 12001 dtrace_difo_init(dp, vstate); 12002 return (dp); 12003 12004 err: 12005 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 12006 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 12007 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 12008 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 12009 12010 kmem_free(dp, sizeof (dtrace_difo_t)); 12011 return (NULL); 12012 } 12013 12014 static dtrace_predicate_t * 12015 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12016 cred_t *cr) 12017 { 12018 dtrace_difo_t *dp; 12019 12020 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 12021 return (NULL); 12022 12023 return (dtrace_predicate_create(dp)); 12024 } 12025 12026 static dtrace_actdesc_t * 12027 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12028 cred_t *cr) 12029 { 12030 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 12031 dof_actdesc_t *desc; 12032 dof_sec_t *difosec; 12033 size_t offs; 12034 uintptr_t daddr = (uintptr_t)dof; 12035 uint64_t arg; 12036 dtrace_actkind_t kind; 12037 12038 if (sec->dofs_type != DOF_SECT_ACTDESC) { 12039 dtrace_dof_error(dof, "invalid action section"); 12040 return (NULL); 12041 } 12042 12043 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 12044 dtrace_dof_error(dof, "truncated action description"); 12045 return (NULL); 12046 } 12047 12048 if (sec->dofs_align != sizeof (uint64_t)) { 12049 dtrace_dof_error(dof, "bad alignment in action description"); 12050 return (NULL); 12051 } 12052 12053 if (sec->dofs_size < sec->dofs_entsize) { 12054 dtrace_dof_error(dof, "section entry size exceeds total size"); 12055 return (NULL); 12056 } 12057 12058 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 12059 dtrace_dof_error(dof, "bad entry size in action description"); 12060 return (NULL); 12061 } 12062 12063 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 12064 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 12065 return (NULL); 12066 } 12067 12068 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 12069 desc = (dof_actdesc_t *)(daddr + 12070 (uintptr_t)sec->dofs_offset + offs); 12071 kind = (dtrace_actkind_t)desc->dofa_kind; 12072 12073 if ((DTRACEACT_ISPRINTFLIKE(kind) && 12074 (kind != DTRACEACT_PRINTA || 12075 desc->dofa_strtab != DOF_SECIDX_NONE)) || 12076 (kind == DTRACEACT_DIFEXPR && 12077 desc->dofa_strtab != DOF_SECIDX_NONE)) { 12078 dof_sec_t *strtab; 12079 char *str, *fmt; 12080 uint64_t i; 12081 12082 /* 12083 * The argument to these actions is an index into the 12084 * DOF string table. For printf()-like actions, this 12085 * is the format string. For print(), this is the 12086 * CTF type of the expression result. 12087 */ 12088 if ((strtab = dtrace_dof_sect(dof, 12089 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 12090 goto err; 12091 12092 str = (char *)((uintptr_t)dof + 12093 (uintptr_t)strtab->dofs_offset); 12094 12095 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 12096 if (str[i] == '\0') 12097 break; 12098 } 12099 12100 if (i >= strtab->dofs_size) { 12101 dtrace_dof_error(dof, "bogus format string"); 12102 goto err; 12103 } 12104 12105 if (i == desc->dofa_arg) { 12106 dtrace_dof_error(dof, "empty format string"); 12107 goto err; 12108 } 12109 12110 i -= desc->dofa_arg; 12111 fmt = kmem_alloc(i + 1, KM_SLEEP); 12112 bcopy(&str[desc->dofa_arg], fmt, i + 1); 12113 arg = (uint64_t)(uintptr_t)fmt; 12114 } else { 12115 if (kind == DTRACEACT_PRINTA) { 12116 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 12117 arg = 0; 12118 } else { 12119 arg = desc->dofa_arg; 12120 } 12121 } 12122 12123 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 12124 desc->dofa_uarg, arg); 12125 12126 if (last != NULL) { 12127 last->dtad_next = act; 12128 } else { 12129 first = act; 12130 } 12131 12132 last = act; 12133 12134 if (desc->dofa_difo == DOF_SECIDX_NONE) 12135 continue; 12136 12137 if ((difosec = dtrace_dof_sect(dof, 12138 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 12139 goto err; 12140 12141 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 12142 12143 if (act->dtad_difo == NULL) 12144 goto err; 12145 } 12146 12147 ASSERT(first != NULL); 12148 return (first); 12149 12150 err: 12151 for (act = first; act != NULL; act = next) { 12152 next = act->dtad_next; 12153 dtrace_actdesc_release(act, vstate); 12154 } 12155 12156 return (NULL); 12157 } 12158 12159 static dtrace_ecbdesc_t * 12160 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12161 cred_t *cr) 12162 { 12163 dtrace_ecbdesc_t *ep; 12164 dof_ecbdesc_t *ecb; 12165 dtrace_probedesc_t *desc; 12166 dtrace_predicate_t *pred = NULL; 12167 12168 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 12169 dtrace_dof_error(dof, "truncated ECB description"); 12170 return (NULL); 12171 } 12172 12173 if (sec->dofs_align != sizeof (uint64_t)) { 12174 dtrace_dof_error(dof, "bad alignment in ECB description"); 12175 return (NULL); 12176 } 12177 12178 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 12179 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 12180 12181 if (sec == NULL) 12182 return (NULL); 12183 12184 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12185 ep->dted_uarg = ecb->dofe_uarg; 12186 desc = &ep->dted_probe; 12187 12188 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 12189 goto err; 12190 12191 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 12192 if ((sec = dtrace_dof_sect(dof, 12193 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 12194 goto err; 12195 12196 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 12197 goto err; 12198 12199 ep->dted_pred.dtpdd_predicate = pred; 12200 } 12201 12202 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 12203 if ((sec = dtrace_dof_sect(dof, 12204 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 12205 goto err; 12206 12207 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 12208 12209 if (ep->dted_action == NULL) 12210 goto err; 12211 } 12212 12213 return (ep); 12214 12215 err: 12216 if (pred != NULL) 12217 dtrace_predicate_release(pred, vstate); 12218 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12219 return (NULL); 12220 } 12221 12222 /* 12223 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 12224 * specified DOF. At present, this amounts to simply adding 'ubase' to the 12225 * site of any user SETX relocations to account for load object base address. 12226 * In the future, if we need other relocations, this function can be extended. 12227 */ 12228 static int 12229 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 12230 { 12231 uintptr_t daddr = (uintptr_t)dof; 12232 dof_relohdr_t *dofr = 12233 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12234 dof_sec_t *ss, *rs, *ts; 12235 dof_relodesc_t *r; 12236 uint_t i, n; 12237 12238 if (sec->dofs_size < sizeof (dof_relohdr_t) || 12239 sec->dofs_align != sizeof (dof_secidx_t)) { 12240 dtrace_dof_error(dof, "invalid relocation header"); 12241 return (-1); 12242 } 12243 12244 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 12245 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 12246 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 12247 12248 if (ss == NULL || rs == NULL || ts == NULL) 12249 return (-1); /* dtrace_dof_error() has been called already */ 12250 12251 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 12252 rs->dofs_align != sizeof (uint64_t)) { 12253 dtrace_dof_error(dof, "invalid relocation section"); 12254 return (-1); 12255 } 12256 12257 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 12258 n = rs->dofs_size / rs->dofs_entsize; 12259 12260 for (i = 0; i < n; i++) { 12261 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 12262 12263 switch (r->dofr_type) { 12264 case DOF_RELO_NONE: 12265 break; 12266 case DOF_RELO_SETX: 12267 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 12268 sizeof (uint64_t) > ts->dofs_size) { 12269 dtrace_dof_error(dof, "bad relocation offset"); 12270 return (-1); 12271 } 12272 12273 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 12274 dtrace_dof_error(dof, "misaligned setx relo"); 12275 return (-1); 12276 } 12277 12278 *(uint64_t *)taddr += ubase; 12279 break; 12280 default: 12281 dtrace_dof_error(dof, "invalid relocation type"); 12282 return (-1); 12283 } 12284 12285 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 12286 } 12287 12288 return (0); 12289 } 12290 12291 /* 12292 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 12293 * header: it should be at the front of a memory region that is at least 12294 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 12295 * size. It need not be validated in any other way. 12296 */ 12297 static int 12298 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 12299 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 12300 { 12301 uint64_t len = dof->dofh_loadsz, seclen; 12302 uintptr_t daddr = (uintptr_t)dof; 12303 dtrace_ecbdesc_t *ep; 12304 dtrace_enabling_t *enab; 12305 uint_t i; 12306 12307 ASSERT(MUTEX_HELD(&dtrace_lock)); 12308 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 12309 12310 /* 12311 * Check the DOF header identification bytes. In addition to checking 12312 * valid settings, we also verify that unused bits/bytes are zeroed so 12313 * we can use them later without fear of regressing existing binaries. 12314 */ 12315 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 12316 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 12317 dtrace_dof_error(dof, "DOF magic string mismatch"); 12318 return (-1); 12319 } 12320 12321 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 12322 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 12323 dtrace_dof_error(dof, "DOF has invalid data model"); 12324 return (-1); 12325 } 12326 12327 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 12328 dtrace_dof_error(dof, "DOF encoding mismatch"); 12329 return (-1); 12330 } 12331 12332 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 12333 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 12334 dtrace_dof_error(dof, "DOF version mismatch"); 12335 return (-1); 12336 } 12337 12338 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 12339 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 12340 return (-1); 12341 } 12342 12343 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 12344 dtrace_dof_error(dof, "DOF uses too many integer registers"); 12345 return (-1); 12346 } 12347 12348 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 12349 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 12350 return (-1); 12351 } 12352 12353 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 12354 if (dof->dofh_ident[i] != 0) { 12355 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 12356 return (-1); 12357 } 12358 } 12359 12360 if (dof->dofh_flags & ~DOF_FL_VALID) { 12361 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 12362 return (-1); 12363 } 12364 12365 if (dof->dofh_secsize == 0) { 12366 dtrace_dof_error(dof, "zero section header size"); 12367 return (-1); 12368 } 12369 12370 /* 12371 * Check that the section headers don't exceed the amount of DOF 12372 * data. Note that we cast the section size and number of sections 12373 * to uint64_t's to prevent possible overflow in the multiplication. 12374 */ 12375 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 12376 12377 if (dof->dofh_secoff > len || seclen > len || 12378 dof->dofh_secoff + seclen > len) { 12379 dtrace_dof_error(dof, "truncated section headers"); 12380 return (-1); 12381 } 12382 12383 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 12384 dtrace_dof_error(dof, "misaligned section headers"); 12385 return (-1); 12386 } 12387 12388 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 12389 dtrace_dof_error(dof, "misaligned section size"); 12390 return (-1); 12391 } 12392 12393 /* 12394 * Take an initial pass through the section headers to be sure that 12395 * the headers don't have stray offsets. If the 'noprobes' flag is 12396 * set, do not permit sections relating to providers, probes, or args. 12397 */ 12398 for (i = 0; i < dof->dofh_secnum; i++) { 12399 dof_sec_t *sec = (dof_sec_t *)(daddr + 12400 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12401 12402 if (noprobes) { 12403 switch (sec->dofs_type) { 12404 case DOF_SECT_PROVIDER: 12405 case DOF_SECT_PROBES: 12406 case DOF_SECT_PRARGS: 12407 case DOF_SECT_PROFFS: 12408 dtrace_dof_error(dof, "illegal sections " 12409 "for enabling"); 12410 return (-1); 12411 } 12412 } 12413 12414 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 12415 !(sec->dofs_flags & DOF_SECF_LOAD)) { 12416 dtrace_dof_error(dof, "loadable section with load " 12417 "flag unset"); 12418 return (-1); 12419 } 12420 12421 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12422 continue; /* just ignore non-loadable sections */ 12423 12424 if (sec->dofs_align & (sec->dofs_align - 1)) { 12425 dtrace_dof_error(dof, "bad section alignment"); 12426 return (-1); 12427 } 12428 12429 if (sec->dofs_offset & (sec->dofs_align - 1)) { 12430 dtrace_dof_error(dof, "misaligned section"); 12431 return (-1); 12432 } 12433 12434 if (sec->dofs_offset > len || sec->dofs_size > len || 12435 sec->dofs_offset + sec->dofs_size > len) { 12436 dtrace_dof_error(dof, "corrupt section header"); 12437 return (-1); 12438 } 12439 12440 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 12441 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 12442 dtrace_dof_error(dof, "non-terminating string table"); 12443 return (-1); 12444 } 12445 } 12446 12447 /* 12448 * Take a second pass through the sections and locate and perform any 12449 * relocations that are present. We do this after the first pass to 12450 * be sure that all sections have had their headers validated. 12451 */ 12452 for (i = 0; i < dof->dofh_secnum; i++) { 12453 dof_sec_t *sec = (dof_sec_t *)(daddr + 12454 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12455 12456 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12457 continue; /* skip sections that are not loadable */ 12458 12459 switch (sec->dofs_type) { 12460 case DOF_SECT_URELHDR: 12461 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 12462 return (-1); 12463 break; 12464 } 12465 } 12466 12467 if ((enab = *enabp) == NULL) 12468 enab = *enabp = dtrace_enabling_create(vstate); 12469 12470 for (i = 0; i < dof->dofh_secnum; i++) { 12471 dof_sec_t *sec = (dof_sec_t *)(daddr + 12472 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12473 12474 if (sec->dofs_type != DOF_SECT_ECBDESC) 12475 continue; 12476 12477 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 12478 dtrace_enabling_destroy(enab); 12479 *enabp = NULL; 12480 return (-1); 12481 } 12482 12483 dtrace_enabling_add(enab, ep); 12484 } 12485 12486 return (0); 12487 } 12488 12489 /* 12490 * Process DOF for any options. This routine assumes that the DOF has been 12491 * at least processed by dtrace_dof_slurp(). 12492 */ 12493 static int 12494 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 12495 { 12496 int i, rval; 12497 uint32_t entsize; 12498 size_t offs; 12499 dof_optdesc_t *desc; 12500 12501 for (i = 0; i < dof->dofh_secnum; i++) { 12502 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 12503 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12504 12505 if (sec->dofs_type != DOF_SECT_OPTDESC) 12506 continue; 12507 12508 if (sec->dofs_align != sizeof (uint64_t)) { 12509 dtrace_dof_error(dof, "bad alignment in " 12510 "option description"); 12511 return (EINVAL); 12512 } 12513 12514 if ((entsize = sec->dofs_entsize) == 0) { 12515 dtrace_dof_error(dof, "zeroed option entry size"); 12516 return (EINVAL); 12517 } 12518 12519 if (entsize < sizeof (dof_optdesc_t)) { 12520 dtrace_dof_error(dof, "bad option entry size"); 12521 return (EINVAL); 12522 } 12523 12524 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 12525 desc = (dof_optdesc_t *)((uintptr_t)dof + 12526 (uintptr_t)sec->dofs_offset + offs); 12527 12528 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 12529 dtrace_dof_error(dof, "non-zero option string"); 12530 return (EINVAL); 12531 } 12532 12533 if (desc->dofo_value == DTRACEOPT_UNSET) { 12534 dtrace_dof_error(dof, "unset option"); 12535 return (EINVAL); 12536 } 12537 12538 if ((rval = dtrace_state_option(state, 12539 desc->dofo_option, desc->dofo_value)) != 0) { 12540 dtrace_dof_error(dof, "rejected option"); 12541 return (rval); 12542 } 12543 } 12544 } 12545 12546 return (0); 12547 } 12548 12549 /* 12550 * DTrace Consumer State Functions 12551 */ 12552 int 12553 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 12554 { 12555 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 12556 void *base; 12557 uintptr_t limit; 12558 dtrace_dynvar_t *dvar, *next, *start; 12559 int i; 12560 12561 ASSERT(MUTEX_HELD(&dtrace_lock)); 12562 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 12563 12564 bzero(dstate, sizeof (dtrace_dstate_t)); 12565 12566 if ((dstate->dtds_chunksize = chunksize) == 0) 12567 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 12568 12569 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 12570 size = min; 12571 12572 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12573 return (ENOMEM); 12574 12575 dstate->dtds_size = size; 12576 dstate->dtds_base = base; 12577 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 12578 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 12579 12580 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 12581 12582 if (hashsize != 1 && (hashsize & 1)) 12583 hashsize--; 12584 12585 dstate->dtds_hashsize = hashsize; 12586 dstate->dtds_hash = dstate->dtds_base; 12587 12588 /* 12589 * Set all of our hash buckets to point to the single sink, and (if 12590 * it hasn't already been set), set the sink's hash value to be the 12591 * sink sentinel value. The sink is needed for dynamic variable 12592 * lookups to know that they have iterated over an entire, valid hash 12593 * chain. 12594 */ 12595 for (i = 0; i < hashsize; i++) 12596 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 12597 12598 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 12599 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 12600 12601 /* 12602 * Determine number of active CPUs. Divide free list evenly among 12603 * active CPUs. 12604 */ 12605 start = (dtrace_dynvar_t *) 12606 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 12607 limit = (uintptr_t)base + size; 12608 12609 maxper = (limit - (uintptr_t)start) / NCPU; 12610 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 12611 12612 for (i = 0; i < NCPU; i++) { 12613 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 12614 12615 /* 12616 * If we don't even have enough chunks to make it once through 12617 * NCPUs, we're just going to allocate everything to the first 12618 * CPU. And if we're on the last CPU, we're going to allocate 12619 * whatever is left over. In either case, we set the limit to 12620 * be the limit of the dynamic variable space. 12621 */ 12622 if (maxper == 0 || i == NCPU - 1) { 12623 limit = (uintptr_t)base + size; 12624 start = NULL; 12625 } else { 12626 limit = (uintptr_t)start + maxper; 12627 start = (dtrace_dynvar_t *)limit; 12628 } 12629 12630 ASSERT(limit <= (uintptr_t)base + size); 12631 12632 for (;;) { 12633 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 12634 dstate->dtds_chunksize); 12635 12636 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 12637 break; 12638 12639 dvar->dtdv_next = next; 12640 dvar = next; 12641 } 12642 12643 if (maxper == 0) 12644 break; 12645 } 12646 12647 return (0); 12648 } 12649 12650 void 12651 dtrace_dstate_fini(dtrace_dstate_t *dstate) 12652 { 12653 ASSERT(MUTEX_HELD(&cpu_lock)); 12654 12655 if (dstate->dtds_base == NULL) 12656 return; 12657 12658 kmem_free(dstate->dtds_base, dstate->dtds_size); 12659 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 12660 } 12661 12662 static void 12663 dtrace_vstate_fini(dtrace_vstate_t *vstate) 12664 { 12665 /* 12666 * Logical XOR, where are you? 12667 */ 12668 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 12669 12670 if (vstate->dtvs_nglobals > 0) { 12671 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 12672 sizeof (dtrace_statvar_t *)); 12673 } 12674 12675 if (vstate->dtvs_ntlocals > 0) { 12676 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 12677 sizeof (dtrace_difv_t)); 12678 } 12679 12680 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 12681 12682 if (vstate->dtvs_nlocals > 0) { 12683 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 12684 sizeof (dtrace_statvar_t *)); 12685 } 12686 } 12687 12688 static void 12689 dtrace_state_clean(dtrace_state_t *state) 12690 { 12691 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 12692 return; 12693 12694 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 12695 dtrace_speculation_clean(state); 12696 } 12697 12698 static void 12699 dtrace_state_deadman(dtrace_state_t *state) 12700 { 12701 hrtime_t now; 12702 12703 dtrace_sync(); 12704 12705 now = dtrace_gethrtime(); 12706 12707 if (state != dtrace_anon.dta_state && 12708 now - state->dts_laststatus >= dtrace_deadman_user) 12709 return; 12710 12711 /* 12712 * We must be sure that dts_alive never appears to be less than the 12713 * value upon entry to dtrace_state_deadman(), and because we lack a 12714 * dtrace_cas64(), we cannot store to it atomically. We thus instead 12715 * store INT64_MAX to it, followed by a memory barrier, followed by 12716 * the new value. This assures that dts_alive never appears to be 12717 * less than its true value, regardless of the order in which the 12718 * stores to the underlying storage are issued. 12719 */ 12720 state->dts_alive = INT64_MAX; 12721 dtrace_membar_producer(); 12722 state->dts_alive = now; 12723 } 12724 12725 dtrace_state_t * 12726 dtrace_state_create(dev_t *devp, cred_t *cr) 12727 { 12728 minor_t minor; 12729 major_t major; 12730 char c[30]; 12731 dtrace_state_t *state; 12732 dtrace_optval_t *opt; 12733 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 12734 12735 ASSERT(MUTEX_HELD(&dtrace_lock)); 12736 ASSERT(MUTEX_HELD(&cpu_lock)); 12737 12738 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 12739 VM_BESTFIT | VM_SLEEP); 12740 12741 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 12742 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 12743 return (NULL); 12744 } 12745 12746 state = ddi_get_soft_state(dtrace_softstate, minor); 12747 state->dts_epid = DTRACE_EPIDNONE + 1; 12748 12749 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); 12750 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 12751 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 12752 12753 if (devp != NULL) { 12754 major = getemajor(*devp); 12755 } else { 12756 major = ddi_driver_major(dtrace_devi); 12757 } 12758 12759 state->dts_dev = makedevice(major, minor); 12760 12761 if (devp != NULL) 12762 *devp = state->dts_dev; 12763 12764 /* 12765 * We allocate NCPU buffers. On the one hand, this can be quite 12766 * a bit of memory per instance (nearly 36K on a Starcat). On the 12767 * other hand, it saves an additional memory reference in the probe 12768 * path. 12769 */ 12770 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 12771 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 12772 state->dts_cleaner = CYCLIC_NONE; 12773 state->dts_deadman = CYCLIC_NONE; 12774 state->dts_vstate.dtvs_state = state; 12775 12776 for (i = 0; i < DTRACEOPT_MAX; i++) 12777 state->dts_options[i] = DTRACEOPT_UNSET; 12778 12779 /* 12780 * Set the default options. 12781 */ 12782 opt = state->dts_options; 12783 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 12784 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 12785 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 12786 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 12787 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 12788 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 12789 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 12790 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 12791 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 12792 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 12793 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 12794 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 12795 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 12796 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 12797 12798 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 12799 12800 /* 12801 * Depending on the user credentials, we set flag bits which alter probe 12802 * visibility or the amount of destructiveness allowed. In the case of 12803 * actual anonymous tracing, or the possession of all privileges, all of 12804 * the normal checks are bypassed. 12805 */ 12806 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 12807 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 12808 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 12809 } else { 12810 /* 12811 * Set up the credentials for this instantiation. We take a 12812 * hold on the credential to prevent it from disappearing on 12813 * us; this in turn prevents the zone_t referenced by this 12814 * credential from disappearing. This means that we can 12815 * examine the credential and the zone from probe context. 12816 */ 12817 crhold(cr); 12818 state->dts_cred.dcr_cred = cr; 12819 12820 /* 12821 * CRA_PROC means "we have *some* privilege for dtrace" and 12822 * unlocks the use of variables like pid, zonename, etc. 12823 */ 12824 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 12825 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12826 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 12827 } 12828 12829 /* 12830 * dtrace_user allows use of syscall and profile providers. 12831 * If the user also has proc_owner and/or proc_zone, we 12832 * extend the scope to include additional visibility and 12833 * destructive power. 12834 */ 12835 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 12836 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 12837 state->dts_cred.dcr_visible |= 12838 DTRACE_CRV_ALLPROC; 12839 12840 state->dts_cred.dcr_action |= 12841 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12842 } 12843 12844 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 12845 state->dts_cred.dcr_visible |= 12846 DTRACE_CRV_ALLZONE; 12847 12848 state->dts_cred.dcr_action |= 12849 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12850 } 12851 12852 /* 12853 * If we have all privs in whatever zone this is, 12854 * we can do destructive things to processes which 12855 * have altered credentials. 12856 */ 12857 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12858 cr->cr_zone->zone_privset)) { 12859 state->dts_cred.dcr_action |= 12860 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12861 } 12862 } 12863 12864 /* 12865 * Holding the dtrace_kernel privilege also implies that 12866 * the user has the dtrace_user privilege from a visibility 12867 * perspective. But without further privileges, some 12868 * destructive actions are not available. 12869 */ 12870 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 12871 /* 12872 * Make all probes in all zones visible. However, 12873 * this doesn't mean that all actions become available 12874 * to all zones. 12875 */ 12876 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 12877 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 12878 12879 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 12880 DTRACE_CRA_PROC; 12881 /* 12882 * Holding proc_owner means that destructive actions 12883 * for *this* zone are allowed. 12884 */ 12885 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12886 state->dts_cred.dcr_action |= 12887 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12888 12889 /* 12890 * Holding proc_zone means that destructive actions 12891 * for this user/group ID in all zones is allowed. 12892 */ 12893 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12894 state->dts_cred.dcr_action |= 12895 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12896 12897 /* 12898 * If we have all privs in whatever zone this is, 12899 * we can do destructive things to processes which 12900 * have altered credentials. 12901 */ 12902 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12903 cr->cr_zone->zone_privset)) { 12904 state->dts_cred.dcr_action |= 12905 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12906 } 12907 } 12908 12909 /* 12910 * Holding the dtrace_proc privilege gives control over fasttrap 12911 * and pid providers. We need to grant wider destructive 12912 * privileges in the event that the user has proc_owner and/or 12913 * proc_zone. 12914 */ 12915 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12916 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12917 state->dts_cred.dcr_action |= 12918 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12919 12920 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12921 state->dts_cred.dcr_action |= 12922 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12923 } 12924 } 12925 12926 return (state); 12927 } 12928 12929 static int 12930 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 12931 { 12932 dtrace_optval_t *opt = state->dts_options, size; 12933 processorid_t cpu; 12934 int flags = 0, rval, factor, divisor = 1; 12935 12936 ASSERT(MUTEX_HELD(&dtrace_lock)); 12937 ASSERT(MUTEX_HELD(&cpu_lock)); 12938 ASSERT(which < DTRACEOPT_MAX); 12939 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 12940 (state == dtrace_anon.dta_state && 12941 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 12942 12943 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 12944 return (0); 12945 12946 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 12947 cpu = opt[DTRACEOPT_CPU]; 12948 12949 if (which == DTRACEOPT_SPECSIZE) 12950 flags |= DTRACEBUF_NOSWITCH; 12951 12952 if (which == DTRACEOPT_BUFSIZE) { 12953 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 12954 flags |= DTRACEBUF_RING; 12955 12956 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 12957 flags |= DTRACEBUF_FILL; 12958 12959 if (state != dtrace_anon.dta_state || 12960 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 12961 flags |= DTRACEBUF_INACTIVE; 12962 } 12963 12964 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 12965 /* 12966 * The size must be 8-byte aligned. If the size is not 8-byte 12967 * aligned, drop it down by the difference. 12968 */ 12969 if (size & (sizeof (uint64_t) - 1)) 12970 size -= size & (sizeof (uint64_t) - 1); 12971 12972 if (size < state->dts_reserve) { 12973 /* 12974 * Buffers always must be large enough to accommodate 12975 * their prereserved space. We return E2BIG instead 12976 * of ENOMEM in this case to allow for user-level 12977 * software to differentiate the cases. 12978 */ 12979 return (E2BIG); 12980 } 12981 12982 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 12983 12984 if (rval != ENOMEM) { 12985 opt[which] = size; 12986 return (rval); 12987 } 12988 12989 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 12990 return (rval); 12991 12992 for (divisor = 2; divisor < factor; divisor <<= 1) 12993 continue; 12994 } 12995 12996 return (ENOMEM); 12997 } 12998 12999 static int 13000 dtrace_state_buffers(dtrace_state_t *state) 13001 { 13002 dtrace_speculation_t *spec = state->dts_speculations; 13003 int rval, i; 13004 13005 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 13006 DTRACEOPT_BUFSIZE)) != 0) 13007 return (rval); 13008 13009 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 13010 DTRACEOPT_AGGSIZE)) != 0) 13011 return (rval); 13012 13013 for (i = 0; i < state->dts_nspeculations; i++) { 13014 if ((rval = dtrace_state_buffer(state, 13015 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 13016 return (rval); 13017 } 13018 13019 return (0); 13020 } 13021 13022 static void 13023 dtrace_state_prereserve(dtrace_state_t *state) 13024 { 13025 dtrace_ecb_t *ecb; 13026 dtrace_probe_t *probe; 13027 13028 state->dts_reserve = 0; 13029 13030 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 13031 return; 13032 13033 /* 13034 * If our buffer policy is a "fill" buffer policy, we need to set the 13035 * prereserved space to be the space required by the END probes. 13036 */ 13037 probe = dtrace_probes[dtrace_probeid_end - 1]; 13038 ASSERT(probe != NULL); 13039 13040 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 13041 if (ecb->dte_state != state) 13042 continue; 13043 13044 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 13045 } 13046 } 13047 13048 static int 13049 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 13050 { 13051 dtrace_optval_t *opt = state->dts_options, sz, nspec; 13052 dtrace_speculation_t *spec; 13053 dtrace_buffer_t *buf; 13054 cyc_handler_t hdlr; 13055 cyc_time_t when; 13056 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13057 dtrace_icookie_t cookie; 13058 13059 mutex_enter(&cpu_lock); 13060 mutex_enter(&dtrace_lock); 13061 13062 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13063 rval = EBUSY; 13064 goto out; 13065 } 13066 13067 /* 13068 * Before we can perform any checks, we must prime all of the 13069 * retained enablings that correspond to this state. 13070 */ 13071 dtrace_enabling_prime(state); 13072 13073 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 13074 rval = EACCES; 13075 goto out; 13076 } 13077 13078 dtrace_state_prereserve(state); 13079 13080 /* 13081 * Now we want to do is try to allocate our speculations. 13082 * We do not automatically resize the number of speculations; if 13083 * this fails, we will fail the operation. 13084 */ 13085 nspec = opt[DTRACEOPT_NSPEC]; 13086 ASSERT(nspec != DTRACEOPT_UNSET); 13087 13088 if (nspec > INT_MAX) { 13089 rval = ENOMEM; 13090 goto out; 13091 } 13092 13093 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 13094 KM_NOSLEEP | KM_NORMALPRI); 13095 13096 if (spec == NULL) { 13097 rval = ENOMEM; 13098 goto out; 13099 } 13100 13101 state->dts_speculations = spec; 13102 state->dts_nspeculations = (int)nspec; 13103 13104 for (i = 0; i < nspec; i++) { 13105 if ((buf = kmem_zalloc(bufsize, 13106 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 13107 rval = ENOMEM; 13108 goto err; 13109 } 13110 13111 spec[i].dtsp_buffer = buf; 13112 } 13113 13114 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 13115 if (dtrace_anon.dta_state == NULL) { 13116 rval = ENOENT; 13117 goto out; 13118 } 13119 13120 if (state->dts_necbs != 0) { 13121 rval = EALREADY; 13122 goto out; 13123 } 13124 13125 state->dts_anon = dtrace_anon_grab(); 13126 ASSERT(state->dts_anon != NULL); 13127 state = state->dts_anon; 13128 13129 /* 13130 * We want "grabanon" to be set in the grabbed state, so we'll 13131 * copy that option value from the grabbing state into the 13132 * grabbed state. 13133 */ 13134 state->dts_options[DTRACEOPT_GRABANON] = 13135 opt[DTRACEOPT_GRABANON]; 13136 13137 *cpu = dtrace_anon.dta_beganon; 13138 13139 /* 13140 * If the anonymous state is active (as it almost certainly 13141 * is if the anonymous enabling ultimately matched anything), 13142 * we don't allow any further option processing -- but we 13143 * don't return failure. 13144 */ 13145 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13146 goto out; 13147 } 13148 13149 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 13150 opt[DTRACEOPT_AGGSIZE] != 0) { 13151 if (state->dts_aggregations == NULL) { 13152 /* 13153 * We're not going to create an aggregation buffer 13154 * because we don't have any ECBs that contain 13155 * aggregations -- set this option to 0. 13156 */ 13157 opt[DTRACEOPT_AGGSIZE] = 0; 13158 } else { 13159 /* 13160 * If we have an aggregation buffer, we must also have 13161 * a buffer to use as scratch. 13162 */ 13163 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 13164 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 13165 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 13166 } 13167 } 13168 } 13169 13170 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 13171 opt[DTRACEOPT_SPECSIZE] != 0) { 13172 if (!state->dts_speculates) { 13173 /* 13174 * We're not going to create speculation buffers 13175 * because we don't have any ECBs that actually 13176 * speculate -- set the speculation size to 0. 13177 */ 13178 opt[DTRACEOPT_SPECSIZE] = 0; 13179 } 13180 } 13181 13182 /* 13183 * The bare minimum size for any buffer that we're actually going to 13184 * do anything to is sizeof (uint64_t). 13185 */ 13186 sz = sizeof (uint64_t); 13187 13188 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 13189 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 13190 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 13191 /* 13192 * A buffer size has been explicitly set to 0 (or to a size 13193 * that will be adjusted to 0) and we need the space -- we 13194 * need to return failure. We return ENOSPC to differentiate 13195 * it from failing to allocate a buffer due to failure to meet 13196 * the reserve (for which we return E2BIG). 13197 */ 13198 rval = ENOSPC; 13199 goto out; 13200 } 13201 13202 if ((rval = dtrace_state_buffers(state)) != 0) 13203 goto err; 13204 13205 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 13206 sz = dtrace_dstate_defsize; 13207 13208 do { 13209 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 13210 13211 if (rval == 0) 13212 break; 13213 13214 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13215 goto err; 13216 } while (sz >>= 1); 13217 13218 opt[DTRACEOPT_DYNVARSIZE] = sz; 13219 13220 if (rval != 0) 13221 goto err; 13222 13223 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 13224 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 13225 13226 if (opt[DTRACEOPT_CLEANRATE] == 0) 13227 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13228 13229 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 13230 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 13231 13232 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 13233 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13234 13235 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 13236 hdlr.cyh_arg = state; 13237 hdlr.cyh_level = CY_LOW_LEVEL; 13238 13239 when.cyt_when = 0; 13240 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 13241 13242 state->dts_cleaner = cyclic_add(&hdlr, &when); 13243 13244 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 13245 hdlr.cyh_arg = state; 13246 hdlr.cyh_level = CY_LOW_LEVEL; 13247 13248 when.cyt_when = 0; 13249 when.cyt_interval = dtrace_deadman_interval; 13250 13251 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 13252 state->dts_deadman = cyclic_add(&hdlr, &when); 13253 13254 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 13255 13256 if (state->dts_getf != 0 && 13257 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 13258 /* 13259 * We don't have kernel privs but we have at least one call 13260 * to getf(); we need to bump our zone's count, and (if 13261 * this is the first enabling to have an unprivileged call 13262 * to getf()) we need to hook into closef(). 13263 */ 13264 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 13265 13266 if (dtrace_getf++ == 0) { 13267 ASSERT(dtrace_closef == NULL); 13268 dtrace_closef = dtrace_getf_barrier; 13269 } 13270 } 13271 13272 /* 13273 * Now it's time to actually fire the BEGIN probe. We need to disable 13274 * interrupts here both to record the CPU on which we fired the BEGIN 13275 * probe (the data from this CPU will be processed first at user 13276 * level) and to manually activate the buffer for this CPU. 13277 */ 13278 cookie = dtrace_interrupt_disable(); 13279 *cpu = CPU->cpu_id; 13280 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 13281 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 13282 13283 dtrace_probe(dtrace_probeid_begin, 13284 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13285 dtrace_interrupt_enable(cookie); 13286 /* 13287 * We may have had an exit action from a BEGIN probe; only change our 13288 * state to ACTIVE if we're still in WARMUP. 13289 */ 13290 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 13291 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 13292 13293 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 13294 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 13295 13296 /* 13297 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 13298 * want each CPU to transition its principal buffer out of the 13299 * INACTIVE state. Doing this assures that no CPU will suddenly begin 13300 * processing an ECB halfway down a probe's ECB chain; all CPUs will 13301 * atomically transition from processing none of a state's ECBs to 13302 * processing all of them. 13303 */ 13304 dtrace_xcall(DTRACE_CPUALL, 13305 (dtrace_xcall_t)dtrace_buffer_activate, state); 13306 goto out; 13307 13308 err: 13309 dtrace_buffer_free(state->dts_buffer); 13310 dtrace_buffer_free(state->dts_aggbuffer); 13311 13312 if ((nspec = state->dts_nspeculations) == 0) { 13313 ASSERT(state->dts_speculations == NULL); 13314 goto out; 13315 } 13316 13317 spec = state->dts_speculations; 13318 ASSERT(spec != NULL); 13319 13320 for (i = 0; i < state->dts_nspeculations; i++) { 13321 if ((buf = spec[i].dtsp_buffer) == NULL) 13322 break; 13323 13324 dtrace_buffer_free(buf); 13325 kmem_free(buf, bufsize); 13326 } 13327 13328 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13329 state->dts_nspeculations = 0; 13330 state->dts_speculations = NULL; 13331 13332 out: 13333 mutex_exit(&dtrace_lock); 13334 mutex_exit(&cpu_lock); 13335 13336 return (rval); 13337 } 13338 13339 static int 13340 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 13341 { 13342 dtrace_icookie_t cookie; 13343 13344 ASSERT(MUTEX_HELD(&dtrace_lock)); 13345 13346 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 13347 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 13348 return (EINVAL); 13349 13350 /* 13351 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 13352 * to be sure that every CPU has seen it. See below for the details 13353 * on why this is done. 13354 */ 13355 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 13356 dtrace_sync(); 13357 13358 /* 13359 * By this point, it is impossible for any CPU to be still processing 13360 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 13361 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 13362 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 13363 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 13364 * iff we're in the END probe. 13365 */ 13366 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 13367 dtrace_sync(); 13368 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 13369 13370 /* 13371 * Finally, we can release the reserve and call the END probe. We 13372 * disable interrupts across calling the END probe to allow us to 13373 * return the CPU on which we actually called the END probe. This 13374 * allows user-land to be sure that this CPU's principal buffer is 13375 * processed last. 13376 */ 13377 state->dts_reserve = 0; 13378 13379 cookie = dtrace_interrupt_disable(); 13380 *cpu = CPU->cpu_id; 13381 dtrace_probe(dtrace_probeid_end, 13382 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13383 dtrace_interrupt_enable(cookie); 13384 13385 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 13386 dtrace_sync(); 13387 13388 if (state->dts_getf != 0 && 13389 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 13390 /* 13391 * We don't have kernel privs but we have at least one call 13392 * to getf(); we need to lower our zone's count, and (if 13393 * this is the last enabling to have an unprivileged call 13394 * to getf()) we need to clear the closef() hook. 13395 */ 13396 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 13397 ASSERT(dtrace_closef == dtrace_getf_barrier); 13398 ASSERT(dtrace_getf > 0); 13399 13400 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 13401 13402 if (--dtrace_getf == 0) 13403 dtrace_closef = NULL; 13404 } 13405 13406 return (0); 13407 } 13408 13409 static int 13410 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 13411 dtrace_optval_t val) 13412 { 13413 ASSERT(MUTEX_HELD(&dtrace_lock)); 13414 13415 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13416 return (EBUSY); 13417 13418 if (option >= DTRACEOPT_MAX) 13419 return (EINVAL); 13420 13421 if (option != DTRACEOPT_CPU && val < 0) 13422 return (EINVAL); 13423 13424 switch (option) { 13425 case DTRACEOPT_DESTRUCTIVE: 13426 if (dtrace_destructive_disallow) 13427 return (EACCES); 13428 13429 state->dts_cred.dcr_destructive = 1; 13430 break; 13431 13432 case DTRACEOPT_BUFSIZE: 13433 case DTRACEOPT_DYNVARSIZE: 13434 case DTRACEOPT_AGGSIZE: 13435 case DTRACEOPT_SPECSIZE: 13436 case DTRACEOPT_STRSIZE: 13437 if (val < 0) 13438 return (EINVAL); 13439 13440 if (val >= LONG_MAX) { 13441 /* 13442 * If this is an otherwise negative value, set it to 13443 * the highest multiple of 128m less than LONG_MAX. 13444 * Technically, we're adjusting the size without 13445 * regard to the buffer resizing policy, but in fact, 13446 * this has no effect -- if we set the buffer size to 13447 * ~LONG_MAX and the buffer policy is ultimately set to 13448 * be "manual", the buffer allocation is guaranteed to 13449 * fail, if only because the allocation requires two 13450 * buffers. (We set the the size to the highest 13451 * multiple of 128m because it ensures that the size 13452 * will remain a multiple of a megabyte when 13453 * repeatedly halved -- all the way down to 15m.) 13454 */ 13455 val = LONG_MAX - (1 << 27) + 1; 13456 } 13457 } 13458 13459 state->dts_options[option] = val; 13460 13461 return (0); 13462 } 13463 13464 static void 13465 dtrace_state_destroy(dtrace_state_t *state) 13466 { 13467 dtrace_ecb_t *ecb; 13468 dtrace_vstate_t *vstate = &state->dts_vstate; 13469 minor_t minor = getminor(state->dts_dev); 13470 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13471 dtrace_speculation_t *spec = state->dts_speculations; 13472 int nspec = state->dts_nspeculations; 13473 uint32_t match; 13474 13475 ASSERT(MUTEX_HELD(&dtrace_lock)); 13476 ASSERT(MUTEX_HELD(&cpu_lock)); 13477 13478 /* 13479 * First, retract any retained enablings for this state. 13480 */ 13481 dtrace_enabling_retract(state); 13482 ASSERT(state->dts_nretained == 0); 13483 13484 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 13485 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 13486 /* 13487 * We have managed to come into dtrace_state_destroy() on a 13488 * hot enabling -- almost certainly because of a disorderly 13489 * shutdown of a consumer. (That is, a consumer that is 13490 * exiting without having called dtrace_stop().) In this case, 13491 * we're going to set our activity to be KILLED, and then 13492 * issue a sync to be sure that everyone is out of probe 13493 * context before we start blowing away ECBs. 13494 */ 13495 state->dts_activity = DTRACE_ACTIVITY_KILLED; 13496 dtrace_sync(); 13497 } 13498 13499 /* 13500 * Release the credential hold we took in dtrace_state_create(). 13501 */ 13502 if (state->dts_cred.dcr_cred != NULL) 13503 crfree(state->dts_cred.dcr_cred); 13504 13505 /* 13506 * Now we can safely disable and destroy any enabled probes. Because 13507 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 13508 * (especially if they're all enabled), we take two passes through the 13509 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 13510 * in the second we disable whatever is left over. 13511 */ 13512 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 13513 for (i = 0; i < state->dts_necbs; i++) { 13514 if ((ecb = state->dts_ecbs[i]) == NULL) 13515 continue; 13516 13517 if (match && ecb->dte_probe != NULL) { 13518 dtrace_probe_t *probe = ecb->dte_probe; 13519 dtrace_provider_t *prov = probe->dtpr_provider; 13520 13521 if (!(prov->dtpv_priv.dtpp_flags & match)) 13522 continue; 13523 } 13524 13525 dtrace_ecb_disable(ecb); 13526 dtrace_ecb_destroy(ecb); 13527 } 13528 13529 if (!match) 13530 break; 13531 } 13532 13533 /* 13534 * Before we free the buffers, perform one more sync to assure that 13535 * every CPU is out of probe context. 13536 */ 13537 dtrace_sync(); 13538 13539 dtrace_buffer_free(state->dts_buffer); 13540 dtrace_buffer_free(state->dts_aggbuffer); 13541 13542 for (i = 0; i < nspec; i++) 13543 dtrace_buffer_free(spec[i].dtsp_buffer); 13544 13545 if (state->dts_cleaner != CYCLIC_NONE) 13546 cyclic_remove(state->dts_cleaner); 13547 13548 if (state->dts_deadman != CYCLIC_NONE) 13549 cyclic_remove(state->dts_deadman); 13550 13551 dtrace_dstate_fini(&vstate->dtvs_dynvars); 13552 dtrace_vstate_fini(vstate); 13553 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 13554 13555 if (state->dts_aggregations != NULL) { 13556 #ifdef DEBUG 13557 for (i = 0; i < state->dts_naggregations; i++) 13558 ASSERT(state->dts_aggregations[i] == NULL); 13559 #endif 13560 ASSERT(state->dts_naggregations > 0); 13561 kmem_free(state->dts_aggregations, 13562 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 13563 } 13564 13565 kmem_free(state->dts_buffer, bufsize); 13566 kmem_free(state->dts_aggbuffer, bufsize); 13567 13568 for (i = 0; i < nspec; i++) 13569 kmem_free(spec[i].dtsp_buffer, bufsize); 13570 13571 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13572 13573 dtrace_format_destroy(state); 13574 13575 vmem_destroy(state->dts_aggid_arena); 13576 ddi_soft_state_free(dtrace_softstate, minor); 13577 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 13578 } 13579 13580 /* 13581 * DTrace Anonymous Enabling Functions 13582 */ 13583 static dtrace_state_t * 13584 dtrace_anon_grab(void) 13585 { 13586 dtrace_state_t *state; 13587 13588 ASSERT(MUTEX_HELD(&dtrace_lock)); 13589 13590 if ((state = dtrace_anon.dta_state) == NULL) { 13591 ASSERT(dtrace_anon.dta_enabling == NULL); 13592 return (NULL); 13593 } 13594 13595 ASSERT(dtrace_anon.dta_enabling != NULL); 13596 ASSERT(dtrace_retained != NULL); 13597 13598 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 13599 dtrace_anon.dta_enabling = NULL; 13600 dtrace_anon.dta_state = NULL; 13601 13602 return (state); 13603 } 13604 13605 static void 13606 dtrace_anon_property(void) 13607 { 13608 int i, rv; 13609 dtrace_state_t *state; 13610 dof_hdr_t *dof; 13611 char c[32]; /* enough for "dof-data-" + digits */ 13612 13613 ASSERT(MUTEX_HELD(&dtrace_lock)); 13614 ASSERT(MUTEX_HELD(&cpu_lock)); 13615 13616 for (i = 0; ; i++) { 13617 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 13618 13619 dtrace_err_verbose = 1; 13620 13621 if ((dof = dtrace_dof_property(c)) == NULL) { 13622 dtrace_err_verbose = 0; 13623 break; 13624 } 13625 13626 /* 13627 * We want to create anonymous state, so we need to transition 13628 * the kernel debugger to indicate that DTrace is active. If 13629 * this fails (e.g. because the debugger has modified text in 13630 * some way), we won't continue with the processing. 13631 */ 13632 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13633 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 13634 "enabling ignored."); 13635 dtrace_dof_destroy(dof); 13636 break; 13637 } 13638 13639 /* 13640 * If we haven't allocated an anonymous state, we'll do so now. 13641 */ 13642 if ((state = dtrace_anon.dta_state) == NULL) { 13643 state = dtrace_state_create(NULL, NULL); 13644 dtrace_anon.dta_state = state; 13645 13646 if (state == NULL) { 13647 /* 13648 * This basically shouldn't happen: the only 13649 * failure mode from dtrace_state_create() is a 13650 * failure of ddi_soft_state_zalloc() that 13651 * itself should never happen. Still, the 13652 * interface allows for a failure mode, and 13653 * we want to fail as gracefully as possible: 13654 * we'll emit an error message and cease 13655 * processing anonymous state in this case. 13656 */ 13657 cmn_err(CE_WARN, "failed to create " 13658 "anonymous state"); 13659 dtrace_dof_destroy(dof); 13660 break; 13661 } 13662 } 13663 13664 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 13665 &dtrace_anon.dta_enabling, 0, B_TRUE); 13666 13667 if (rv == 0) 13668 rv = dtrace_dof_options(dof, state); 13669 13670 dtrace_err_verbose = 0; 13671 dtrace_dof_destroy(dof); 13672 13673 if (rv != 0) { 13674 /* 13675 * This is malformed DOF; chuck any anonymous state 13676 * that we created. 13677 */ 13678 ASSERT(dtrace_anon.dta_enabling == NULL); 13679 dtrace_state_destroy(state); 13680 dtrace_anon.dta_state = NULL; 13681 break; 13682 } 13683 13684 ASSERT(dtrace_anon.dta_enabling != NULL); 13685 } 13686 13687 if (dtrace_anon.dta_enabling != NULL) { 13688 int rval; 13689 13690 /* 13691 * dtrace_enabling_retain() can only fail because we are 13692 * trying to retain more enablings than are allowed -- but 13693 * we only have one anonymous enabling, and we are guaranteed 13694 * to be allowed at least one retained enabling; we assert 13695 * that dtrace_enabling_retain() returns success. 13696 */ 13697 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 13698 ASSERT(rval == 0); 13699 13700 dtrace_enabling_dump(dtrace_anon.dta_enabling); 13701 } 13702 } 13703 13704 /* 13705 * DTrace Helper Functions 13706 */ 13707 static void 13708 dtrace_helper_trace(dtrace_helper_action_t *helper, 13709 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 13710 { 13711 uint32_t size, next, nnext, i; 13712 dtrace_helptrace_t *ent, *buffer; 13713 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 13714 13715 if ((buffer = dtrace_helptrace_buffer) == NULL) 13716 return; 13717 13718 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 13719 13720 /* 13721 * What would a tracing framework be without its own tracing 13722 * framework? (Well, a hell of a lot simpler, for starters...) 13723 */ 13724 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 13725 sizeof (uint64_t) - sizeof (uint64_t); 13726 13727 /* 13728 * Iterate until we can allocate a slot in the trace buffer. 13729 */ 13730 do { 13731 next = dtrace_helptrace_next; 13732 13733 if (next + size < dtrace_helptrace_bufsize) { 13734 nnext = next + size; 13735 } else { 13736 nnext = size; 13737 } 13738 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 13739 13740 /* 13741 * We have our slot; fill it in. 13742 */ 13743 if (nnext == size) { 13744 dtrace_helptrace_wrapped++; 13745 next = 0; 13746 } 13747 13748 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next); 13749 ent->dtht_helper = helper; 13750 ent->dtht_where = where; 13751 ent->dtht_nlocals = vstate->dtvs_nlocals; 13752 13753 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 13754 mstate->dtms_fltoffs : -1; 13755 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 13756 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; 13757 13758 for (i = 0; i < vstate->dtvs_nlocals; i++) { 13759 dtrace_statvar_t *svar; 13760 13761 if ((svar = vstate->dtvs_locals[i]) == NULL) 13762 continue; 13763 13764 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 13765 ent->dtht_locals[i] = 13766 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; 13767 } 13768 } 13769 13770 static uint64_t 13771 dtrace_helper(int which, dtrace_mstate_t *mstate, 13772 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 13773 { 13774 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; 13775 uint64_t sarg0 = mstate->dtms_arg[0]; 13776 uint64_t sarg1 = mstate->dtms_arg[1]; 13777 uint64_t rval; 13778 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 13779 dtrace_helper_action_t *helper; 13780 dtrace_vstate_t *vstate; 13781 dtrace_difo_t *pred; 13782 int i, trace = dtrace_helptrace_buffer != NULL; 13783 13784 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 13785 13786 if (helpers == NULL) 13787 return (0); 13788 13789 if ((helper = helpers->dthps_actions[which]) == NULL) 13790 return (0); 13791 13792 vstate = &helpers->dthps_vstate; 13793 mstate->dtms_arg[0] = arg0; 13794 mstate->dtms_arg[1] = arg1; 13795 13796 /* 13797 * Now iterate over each helper. If its predicate evaluates to 'true', 13798 * we'll call the corresponding actions. Note that the below calls 13799 * to dtrace_dif_emulate() may set faults in machine state. This is 13800 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 13801 * the stored DIF offset with its own (which is the desired behavior). 13802 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 13803 * from machine state; this is okay, too. 13804 */ 13805 for (; helper != NULL; helper = helper->dtha_next) { 13806 if ((pred = helper->dtha_predicate) != NULL) { 13807 if (trace) 13808 dtrace_helper_trace(helper, mstate, vstate, 0); 13809 13810 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 13811 goto next; 13812 13813 if (*flags & CPU_DTRACE_FAULT) 13814 goto err; 13815 } 13816 13817 for (i = 0; i < helper->dtha_nactions; i++) { 13818 if (trace) 13819 dtrace_helper_trace(helper, 13820 mstate, vstate, i + 1); 13821 13822 rval = dtrace_dif_emulate(helper->dtha_actions[i], 13823 mstate, vstate, state); 13824 13825 if (*flags & CPU_DTRACE_FAULT) 13826 goto err; 13827 } 13828 13829 next: 13830 if (trace) 13831 dtrace_helper_trace(helper, mstate, vstate, 13832 DTRACE_HELPTRACE_NEXT); 13833 } 13834 13835 if (trace) 13836 dtrace_helper_trace(helper, mstate, vstate, 13837 DTRACE_HELPTRACE_DONE); 13838 13839 /* 13840 * Restore the arg0 that we saved upon entry. 13841 */ 13842 mstate->dtms_arg[0] = sarg0; 13843 mstate->dtms_arg[1] = sarg1; 13844 13845 return (rval); 13846 13847 err: 13848 if (trace) 13849 dtrace_helper_trace(helper, mstate, vstate, 13850 DTRACE_HELPTRACE_ERR); 13851 13852 /* 13853 * Restore the arg0 that we saved upon entry. 13854 */ 13855 mstate->dtms_arg[0] = sarg0; 13856 mstate->dtms_arg[1] = sarg1; 13857 13858 return (NULL); 13859 } 13860 13861 static void 13862 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 13863 dtrace_vstate_t *vstate) 13864 { 13865 int i; 13866 13867 if (helper->dtha_predicate != NULL) 13868 dtrace_difo_release(helper->dtha_predicate, vstate); 13869 13870 for (i = 0; i < helper->dtha_nactions; i++) { 13871 ASSERT(helper->dtha_actions[i] != NULL); 13872 dtrace_difo_release(helper->dtha_actions[i], vstate); 13873 } 13874 13875 kmem_free(helper->dtha_actions, 13876 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 13877 kmem_free(helper, sizeof (dtrace_helper_action_t)); 13878 } 13879 13880 static int 13881 dtrace_helper_destroygen(int gen) 13882 { 13883 proc_t *p = curproc; 13884 dtrace_helpers_t *help = p->p_dtrace_helpers; 13885 dtrace_vstate_t *vstate; 13886 int i; 13887 13888 ASSERT(MUTEX_HELD(&dtrace_lock)); 13889 13890 if (help == NULL || gen > help->dthps_generation) 13891 return (EINVAL); 13892 13893 vstate = &help->dthps_vstate; 13894 13895 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 13896 dtrace_helper_action_t *last = NULL, *h, *next; 13897 13898 for (h = help->dthps_actions[i]; h != NULL; h = next) { 13899 next = h->dtha_next; 13900 13901 if (h->dtha_generation == gen) { 13902 if (last != NULL) { 13903 last->dtha_next = next; 13904 } else { 13905 help->dthps_actions[i] = next; 13906 } 13907 13908 dtrace_helper_action_destroy(h, vstate); 13909 } else { 13910 last = h; 13911 } 13912 } 13913 } 13914 13915 /* 13916 * Interate until we've cleared out all helper providers with the 13917 * given generation number. 13918 */ 13919 for (;;) { 13920 dtrace_helper_provider_t *prov; 13921 13922 /* 13923 * Look for a helper provider with the right generation. We 13924 * have to start back at the beginning of the list each time 13925 * because we drop dtrace_lock. It's unlikely that we'll make 13926 * more than two passes. 13927 */ 13928 for (i = 0; i < help->dthps_nprovs; i++) { 13929 prov = help->dthps_provs[i]; 13930 13931 if (prov->dthp_generation == gen) 13932 break; 13933 } 13934 13935 /* 13936 * If there were no matches, we're done. 13937 */ 13938 if (i == help->dthps_nprovs) 13939 break; 13940 13941 /* 13942 * Move the last helper provider into this slot. 13943 */ 13944 help->dthps_nprovs--; 13945 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 13946 help->dthps_provs[help->dthps_nprovs] = NULL; 13947 13948 mutex_exit(&dtrace_lock); 13949 13950 /* 13951 * If we have a meta provider, remove this helper provider. 13952 */ 13953 mutex_enter(&dtrace_meta_lock); 13954 if (dtrace_meta_pid != NULL) { 13955 ASSERT(dtrace_deferred_pid == NULL); 13956 dtrace_helper_provider_remove(&prov->dthp_prov, 13957 p->p_pid); 13958 } 13959 mutex_exit(&dtrace_meta_lock); 13960 13961 dtrace_helper_provider_destroy(prov); 13962 13963 mutex_enter(&dtrace_lock); 13964 } 13965 13966 return (0); 13967 } 13968 13969 static int 13970 dtrace_helper_validate(dtrace_helper_action_t *helper) 13971 { 13972 int err = 0, i; 13973 dtrace_difo_t *dp; 13974 13975 if ((dp = helper->dtha_predicate) != NULL) 13976 err += dtrace_difo_validate_helper(dp); 13977 13978 for (i = 0; i < helper->dtha_nactions; i++) 13979 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 13980 13981 return (err == 0); 13982 } 13983 13984 static int 13985 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 13986 { 13987 dtrace_helpers_t *help; 13988 dtrace_helper_action_t *helper, *last; 13989 dtrace_actdesc_t *act; 13990 dtrace_vstate_t *vstate; 13991 dtrace_predicate_t *pred; 13992 int count = 0, nactions = 0, i; 13993 13994 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 13995 return (EINVAL); 13996 13997 help = curproc->p_dtrace_helpers; 13998 last = help->dthps_actions[which]; 13999 vstate = &help->dthps_vstate; 14000 14001 for (count = 0; last != NULL; last = last->dtha_next) { 14002 count++; 14003 if (last->dtha_next == NULL) 14004 break; 14005 } 14006 14007 /* 14008 * If we already have dtrace_helper_actions_max helper actions for this 14009 * helper action type, we'll refuse to add a new one. 14010 */ 14011 if (count >= dtrace_helper_actions_max) 14012 return (ENOSPC); 14013 14014 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 14015 helper->dtha_generation = help->dthps_generation; 14016 14017 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 14018 ASSERT(pred->dtp_difo != NULL); 14019 dtrace_difo_hold(pred->dtp_difo); 14020 helper->dtha_predicate = pred->dtp_difo; 14021 } 14022 14023 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 14024 if (act->dtad_kind != DTRACEACT_DIFEXPR) 14025 goto err; 14026 14027 if (act->dtad_difo == NULL) 14028 goto err; 14029 14030 nactions++; 14031 } 14032 14033 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 14034 (helper->dtha_nactions = nactions), KM_SLEEP); 14035 14036 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 14037 dtrace_difo_hold(act->dtad_difo); 14038 helper->dtha_actions[i++] = act->dtad_difo; 14039 } 14040 14041 if (!dtrace_helper_validate(helper)) 14042 goto err; 14043 14044 if (last == NULL) { 14045 help->dthps_actions[which] = helper; 14046 } else { 14047 last->dtha_next = helper; 14048 } 14049 14050 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 14051 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 14052 dtrace_helptrace_next = 0; 14053 } 14054 14055 return (0); 14056 err: 14057 dtrace_helper_action_destroy(helper, vstate); 14058 return (EINVAL); 14059 } 14060 14061 static void 14062 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 14063 dof_helper_t *dofhp) 14064 { 14065 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 14066 14067 mutex_enter(&dtrace_meta_lock); 14068 mutex_enter(&dtrace_lock); 14069 14070 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 14071 /* 14072 * If the dtrace module is loaded but not attached, or if 14073 * there aren't isn't a meta provider registered to deal with 14074 * these provider descriptions, we need to postpone creating 14075 * the actual providers until later. 14076 */ 14077 14078 if (help->dthps_next == NULL && help->dthps_prev == NULL && 14079 dtrace_deferred_pid != help) { 14080 help->dthps_deferred = 1; 14081 help->dthps_pid = p->p_pid; 14082 help->dthps_next = dtrace_deferred_pid; 14083 help->dthps_prev = NULL; 14084 if (dtrace_deferred_pid != NULL) 14085 dtrace_deferred_pid->dthps_prev = help; 14086 dtrace_deferred_pid = help; 14087 } 14088 14089 mutex_exit(&dtrace_lock); 14090 14091 } else if (dofhp != NULL) { 14092 /* 14093 * If the dtrace module is loaded and we have a particular 14094 * helper provider description, pass that off to the 14095 * meta provider. 14096 */ 14097 14098 mutex_exit(&dtrace_lock); 14099 14100 dtrace_helper_provide(dofhp, p->p_pid); 14101 14102 } else { 14103 /* 14104 * Otherwise, just pass all the helper provider descriptions 14105 * off to the meta provider. 14106 */ 14107 14108 int i; 14109 mutex_exit(&dtrace_lock); 14110 14111 for (i = 0; i < help->dthps_nprovs; i++) { 14112 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 14113 p->p_pid); 14114 } 14115 } 14116 14117 mutex_exit(&dtrace_meta_lock); 14118 } 14119 14120 static int 14121 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 14122 { 14123 dtrace_helpers_t *help; 14124 dtrace_helper_provider_t *hprov, **tmp_provs; 14125 uint_t tmp_maxprovs, i; 14126 14127 ASSERT(MUTEX_HELD(&dtrace_lock)); 14128 14129 help = curproc->p_dtrace_helpers; 14130 ASSERT(help != NULL); 14131 14132 /* 14133 * If we already have dtrace_helper_providers_max helper providers, 14134 * we're refuse to add a new one. 14135 */ 14136 if (help->dthps_nprovs >= dtrace_helper_providers_max) 14137 return (ENOSPC); 14138 14139 /* 14140 * Check to make sure this isn't a duplicate. 14141 */ 14142 for (i = 0; i < help->dthps_nprovs; i++) { 14143 if (dofhp->dofhp_addr == 14144 help->dthps_provs[i]->dthp_prov.dofhp_addr) 14145 return (EALREADY); 14146 } 14147 14148 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 14149 hprov->dthp_prov = *dofhp; 14150 hprov->dthp_ref = 1; 14151 hprov->dthp_generation = gen; 14152 14153 /* 14154 * Allocate a bigger table for helper providers if it's already full. 14155 */ 14156 if (help->dthps_maxprovs == help->dthps_nprovs) { 14157 tmp_maxprovs = help->dthps_maxprovs; 14158 tmp_provs = help->dthps_provs; 14159 14160 if (help->dthps_maxprovs == 0) 14161 help->dthps_maxprovs = 2; 14162 else 14163 help->dthps_maxprovs *= 2; 14164 if (help->dthps_maxprovs > dtrace_helper_providers_max) 14165 help->dthps_maxprovs = dtrace_helper_providers_max; 14166 14167 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 14168 14169 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 14170 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14171 14172 if (tmp_provs != NULL) { 14173 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 14174 sizeof (dtrace_helper_provider_t *)); 14175 kmem_free(tmp_provs, tmp_maxprovs * 14176 sizeof (dtrace_helper_provider_t *)); 14177 } 14178 } 14179 14180 help->dthps_provs[help->dthps_nprovs] = hprov; 14181 help->dthps_nprovs++; 14182 14183 return (0); 14184 } 14185 14186 static void 14187 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 14188 { 14189 mutex_enter(&dtrace_lock); 14190 14191 if (--hprov->dthp_ref == 0) { 14192 dof_hdr_t *dof; 14193 mutex_exit(&dtrace_lock); 14194 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 14195 dtrace_dof_destroy(dof); 14196 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 14197 } else { 14198 mutex_exit(&dtrace_lock); 14199 } 14200 } 14201 14202 static int 14203 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 14204 { 14205 uintptr_t daddr = (uintptr_t)dof; 14206 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 14207 dof_provider_t *provider; 14208 dof_probe_t *probe; 14209 uint8_t *arg; 14210 char *strtab, *typestr; 14211 dof_stridx_t typeidx; 14212 size_t typesz; 14213 uint_t nprobes, j, k; 14214 14215 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 14216 14217 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 14218 dtrace_dof_error(dof, "misaligned section offset"); 14219 return (-1); 14220 } 14221 14222 /* 14223 * The section needs to be large enough to contain the DOF provider 14224 * structure appropriate for the given version. 14225 */ 14226 if (sec->dofs_size < 14227 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 14228 offsetof(dof_provider_t, dofpv_prenoffs) : 14229 sizeof (dof_provider_t))) { 14230 dtrace_dof_error(dof, "provider section too small"); 14231 return (-1); 14232 } 14233 14234 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 14235 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 14236 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 14237 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 14238 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 14239 14240 if (str_sec == NULL || prb_sec == NULL || 14241 arg_sec == NULL || off_sec == NULL) 14242 return (-1); 14243 14244 enoff_sec = NULL; 14245 14246 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14247 provider->dofpv_prenoffs != DOF_SECT_NONE && 14248 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 14249 provider->dofpv_prenoffs)) == NULL) 14250 return (-1); 14251 14252 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 14253 14254 if (provider->dofpv_name >= str_sec->dofs_size || 14255 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 14256 dtrace_dof_error(dof, "invalid provider name"); 14257 return (-1); 14258 } 14259 14260 if (prb_sec->dofs_entsize == 0 || 14261 prb_sec->dofs_entsize > prb_sec->dofs_size) { 14262 dtrace_dof_error(dof, "invalid entry size"); 14263 return (-1); 14264 } 14265 14266 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 14267 dtrace_dof_error(dof, "misaligned entry size"); 14268 return (-1); 14269 } 14270 14271 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 14272 dtrace_dof_error(dof, "invalid entry size"); 14273 return (-1); 14274 } 14275 14276 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 14277 dtrace_dof_error(dof, "misaligned section offset"); 14278 return (-1); 14279 } 14280 14281 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 14282 dtrace_dof_error(dof, "invalid entry size"); 14283 return (-1); 14284 } 14285 14286 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 14287 14288 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 14289 14290 /* 14291 * Take a pass through the probes to check for errors. 14292 */ 14293 for (j = 0; j < nprobes; j++) { 14294 probe = (dof_probe_t *)(uintptr_t)(daddr + 14295 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 14296 14297 if (probe->dofpr_func >= str_sec->dofs_size) { 14298 dtrace_dof_error(dof, "invalid function name"); 14299 return (-1); 14300 } 14301 14302 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 14303 dtrace_dof_error(dof, "function name too long"); 14304 return (-1); 14305 } 14306 14307 if (probe->dofpr_name >= str_sec->dofs_size || 14308 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 14309 dtrace_dof_error(dof, "invalid probe name"); 14310 return (-1); 14311 } 14312 14313 /* 14314 * The offset count must not wrap the index, and the offsets 14315 * must also not overflow the section's data. 14316 */ 14317 if (probe->dofpr_offidx + probe->dofpr_noffs < 14318 probe->dofpr_offidx || 14319 (probe->dofpr_offidx + probe->dofpr_noffs) * 14320 off_sec->dofs_entsize > off_sec->dofs_size) { 14321 dtrace_dof_error(dof, "invalid probe offset"); 14322 return (-1); 14323 } 14324 14325 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 14326 /* 14327 * If there's no is-enabled offset section, make sure 14328 * there aren't any is-enabled offsets. Otherwise 14329 * perform the same checks as for probe offsets 14330 * (immediately above). 14331 */ 14332 if (enoff_sec == NULL) { 14333 if (probe->dofpr_enoffidx != 0 || 14334 probe->dofpr_nenoffs != 0) { 14335 dtrace_dof_error(dof, "is-enabled " 14336 "offsets with null section"); 14337 return (-1); 14338 } 14339 } else if (probe->dofpr_enoffidx + 14340 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 14341 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 14342 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 14343 dtrace_dof_error(dof, "invalid is-enabled " 14344 "offset"); 14345 return (-1); 14346 } 14347 14348 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 14349 dtrace_dof_error(dof, "zero probe and " 14350 "is-enabled offsets"); 14351 return (-1); 14352 } 14353 } else if (probe->dofpr_noffs == 0) { 14354 dtrace_dof_error(dof, "zero probe offsets"); 14355 return (-1); 14356 } 14357 14358 if (probe->dofpr_argidx + probe->dofpr_xargc < 14359 probe->dofpr_argidx || 14360 (probe->dofpr_argidx + probe->dofpr_xargc) * 14361 arg_sec->dofs_entsize > arg_sec->dofs_size) { 14362 dtrace_dof_error(dof, "invalid args"); 14363 return (-1); 14364 } 14365 14366 typeidx = probe->dofpr_nargv; 14367 typestr = strtab + probe->dofpr_nargv; 14368 for (k = 0; k < probe->dofpr_nargc; k++) { 14369 if (typeidx >= str_sec->dofs_size) { 14370 dtrace_dof_error(dof, "bad " 14371 "native argument type"); 14372 return (-1); 14373 } 14374 14375 typesz = strlen(typestr) + 1; 14376 if (typesz > DTRACE_ARGTYPELEN) { 14377 dtrace_dof_error(dof, "native " 14378 "argument type too long"); 14379 return (-1); 14380 } 14381 typeidx += typesz; 14382 typestr += typesz; 14383 } 14384 14385 typeidx = probe->dofpr_xargv; 14386 typestr = strtab + probe->dofpr_xargv; 14387 for (k = 0; k < probe->dofpr_xargc; k++) { 14388 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 14389 dtrace_dof_error(dof, "bad " 14390 "native argument index"); 14391 return (-1); 14392 } 14393 14394 if (typeidx >= str_sec->dofs_size) { 14395 dtrace_dof_error(dof, "bad " 14396 "translated argument type"); 14397 return (-1); 14398 } 14399 14400 typesz = strlen(typestr) + 1; 14401 if (typesz > DTRACE_ARGTYPELEN) { 14402 dtrace_dof_error(dof, "translated argument " 14403 "type too long"); 14404 return (-1); 14405 } 14406 14407 typeidx += typesz; 14408 typestr += typesz; 14409 } 14410 } 14411 14412 return (0); 14413 } 14414 14415 static int 14416 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 14417 { 14418 dtrace_helpers_t *help; 14419 dtrace_vstate_t *vstate; 14420 dtrace_enabling_t *enab = NULL; 14421 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 14422 uintptr_t daddr = (uintptr_t)dof; 14423 14424 ASSERT(MUTEX_HELD(&dtrace_lock)); 14425 14426 if ((help = curproc->p_dtrace_helpers) == NULL) 14427 help = dtrace_helpers_create(curproc); 14428 14429 vstate = &help->dthps_vstate; 14430 14431 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 14432 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 14433 dtrace_dof_destroy(dof); 14434 return (rv); 14435 } 14436 14437 /* 14438 * Look for helper providers and validate their descriptions. 14439 */ 14440 if (dhp != NULL) { 14441 for (i = 0; i < dof->dofh_secnum; i++) { 14442 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 14443 dof->dofh_secoff + i * dof->dofh_secsize); 14444 14445 if (sec->dofs_type != DOF_SECT_PROVIDER) 14446 continue; 14447 14448 if (dtrace_helper_provider_validate(dof, sec) != 0) { 14449 dtrace_enabling_destroy(enab); 14450 dtrace_dof_destroy(dof); 14451 return (-1); 14452 } 14453 14454 nprovs++; 14455 } 14456 } 14457 14458 /* 14459 * Now we need to walk through the ECB descriptions in the enabling. 14460 */ 14461 for (i = 0; i < enab->dten_ndesc; i++) { 14462 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 14463 dtrace_probedesc_t *desc = &ep->dted_probe; 14464 14465 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 14466 continue; 14467 14468 if (strcmp(desc->dtpd_mod, "helper") != 0) 14469 continue; 14470 14471 if (strcmp(desc->dtpd_func, "ustack") != 0) 14472 continue; 14473 14474 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 14475 ep)) != 0) { 14476 /* 14477 * Adding this helper action failed -- we are now going 14478 * to rip out the entire generation and return failure. 14479 */ 14480 (void) dtrace_helper_destroygen(help->dthps_generation); 14481 dtrace_enabling_destroy(enab); 14482 dtrace_dof_destroy(dof); 14483 return (-1); 14484 } 14485 14486 nhelpers++; 14487 } 14488 14489 if (nhelpers < enab->dten_ndesc) 14490 dtrace_dof_error(dof, "unmatched helpers"); 14491 14492 gen = help->dthps_generation++; 14493 dtrace_enabling_destroy(enab); 14494 14495 if (dhp != NULL && nprovs > 0) { 14496 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 14497 if (dtrace_helper_provider_add(dhp, gen) == 0) { 14498 mutex_exit(&dtrace_lock); 14499 dtrace_helper_provider_register(curproc, help, dhp); 14500 mutex_enter(&dtrace_lock); 14501 14502 destroy = 0; 14503 } 14504 } 14505 14506 if (destroy) 14507 dtrace_dof_destroy(dof); 14508 14509 return (gen); 14510 } 14511 14512 static dtrace_helpers_t * 14513 dtrace_helpers_create(proc_t *p) 14514 { 14515 dtrace_helpers_t *help; 14516 14517 ASSERT(MUTEX_HELD(&dtrace_lock)); 14518 ASSERT(p->p_dtrace_helpers == NULL); 14519 14520 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 14521 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 14522 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 14523 14524 p->p_dtrace_helpers = help; 14525 dtrace_helpers++; 14526 14527 return (help); 14528 } 14529 14530 static void 14531 dtrace_helpers_destroy(void) 14532 { 14533 dtrace_helpers_t *help; 14534 dtrace_vstate_t *vstate; 14535 proc_t *p = curproc; 14536 int i; 14537 14538 mutex_enter(&dtrace_lock); 14539 14540 ASSERT(p->p_dtrace_helpers != NULL); 14541 ASSERT(dtrace_helpers > 0); 14542 14543 help = p->p_dtrace_helpers; 14544 vstate = &help->dthps_vstate; 14545 14546 /* 14547 * We're now going to lose the help from this process. 14548 */ 14549 p->p_dtrace_helpers = NULL; 14550 dtrace_sync(); 14551 14552 /* 14553 * Destory the helper actions. 14554 */ 14555 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14556 dtrace_helper_action_t *h, *next; 14557 14558 for (h = help->dthps_actions[i]; h != NULL; h = next) { 14559 next = h->dtha_next; 14560 dtrace_helper_action_destroy(h, vstate); 14561 h = next; 14562 } 14563 } 14564 14565 mutex_exit(&dtrace_lock); 14566 14567 /* 14568 * Destroy the helper providers. 14569 */ 14570 if (help->dthps_maxprovs > 0) { 14571 mutex_enter(&dtrace_meta_lock); 14572 if (dtrace_meta_pid != NULL) { 14573 ASSERT(dtrace_deferred_pid == NULL); 14574 14575 for (i = 0; i < help->dthps_nprovs; i++) { 14576 dtrace_helper_provider_remove( 14577 &help->dthps_provs[i]->dthp_prov, p->p_pid); 14578 } 14579 } else { 14580 mutex_enter(&dtrace_lock); 14581 ASSERT(help->dthps_deferred == 0 || 14582 help->dthps_next != NULL || 14583 help->dthps_prev != NULL || 14584 help == dtrace_deferred_pid); 14585 14586 /* 14587 * Remove the helper from the deferred list. 14588 */ 14589 if (help->dthps_next != NULL) 14590 help->dthps_next->dthps_prev = help->dthps_prev; 14591 if (help->dthps_prev != NULL) 14592 help->dthps_prev->dthps_next = help->dthps_next; 14593 if (dtrace_deferred_pid == help) { 14594 dtrace_deferred_pid = help->dthps_next; 14595 ASSERT(help->dthps_prev == NULL); 14596 } 14597 14598 mutex_exit(&dtrace_lock); 14599 } 14600 14601 mutex_exit(&dtrace_meta_lock); 14602 14603 for (i = 0; i < help->dthps_nprovs; i++) { 14604 dtrace_helper_provider_destroy(help->dthps_provs[i]); 14605 } 14606 14607 kmem_free(help->dthps_provs, help->dthps_maxprovs * 14608 sizeof (dtrace_helper_provider_t *)); 14609 } 14610 14611 mutex_enter(&dtrace_lock); 14612 14613 dtrace_vstate_fini(&help->dthps_vstate); 14614 kmem_free(help->dthps_actions, 14615 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 14616 kmem_free(help, sizeof (dtrace_helpers_t)); 14617 14618 --dtrace_helpers; 14619 mutex_exit(&dtrace_lock); 14620 } 14621 14622 static void 14623 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 14624 { 14625 dtrace_helpers_t *help, *newhelp; 14626 dtrace_helper_action_t *helper, *new, *last; 14627 dtrace_difo_t *dp; 14628 dtrace_vstate_t *vstate; 14629 int i, j, sz, hasprovs = 0; 14630 14631 mutex_enter(&dtrace_lock); 14632 ASSERT(from->p_dtrace_helpers != NULL); 14633 ASSERT(dtrace_helpers > 0); 14634 14635 help = from->p_dtrace_helpers; 14636 newhelp = dtrace_helpers_create(to); 14637 ASSERT(to->p_dtrace_helpers != NULL); 14638 14639 newhelp->dthps_generation = help->dthps_generation; 14640 vstate = &newhelp->dthps_vstate; 14641 14642 /* 14643 * Duplicate the helper actions. 14644 */ 14645 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14646 if ((helper = help->dthps_actions[i]) == NULL) 14647 continue; 14648 14649 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 14650 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 14651 KM_SLEEP); 14652 new->dtha_generation = helper->dtha_generation; 14653 14654 if ((dp = helper->dtha_predicate) != NULL) { 14655 dp = dtrace_difo_duplicate(dp, vstate); 14656 new->dtha_predicate = dp; 14657 } 14658 14659 new->dtha_nactions = helper->dtha_nactions; 14660 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 14661 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 14662 14663 for (j = 0; j < new->dtha_nactions; j++) { 14664 dtrace_difo_t *dp = helper->dtha_actions[j]; 14665 14666 ASSERT(dp != NULL); 14667 dp = dtrace_difo_duplicate(dp, vstate); 14668 new->dtha_actions[j] = dp; 14669 } 14670 14671 if (last != NULL) { 14672 last->dtha_next = new; 14673 } else { 14674 newhelp->dthps_actions[i] = new; 14675 } 14676 14677 last = new; 14678 } 14679 } 14680 14681 /* 14682 * Duplicate the helper providers and register them with the 14683 * DTrace framework. 14684 */ 14685 if (help->dthps_nprovs > 0) { 14686 newhelp->dthps_nprovs = help->dthps_nprovs; 14687 newhelp->dthps_maxprovs = help->dthps_nprovs; 14688 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 14689 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14690 for (i = 0; i < newhelp->dthps_nprovs; i++) { 14691 newhelp->dthps_provs[i] = help->dthps_provs[i]; 14692 newhelp->dthps_provs[i]->dthp_ref++; 14693 } 14694 14695 hasprovs = 1; 14696 } 14697 14698 mutex_exit(&dtrace_lock); 14699 14700 if (hasprovs) 14701 dtrace_helper_provider_register(to, newhelp, NULL); 14702 } 14703 14704 /* 14705 * DTrace Hook Functions 14706 */ 14707 static void 14708 dtrace_module_loaded(struct modctl *ctl) 14709 { 14710 dtrace_provider_t *prv; 14711 14712 mutex_enter(&dtrace_provider_lock); 14713 mutex_enter(&mod_lock); 14714 14715 ASSERT(ctl->mod_busy); 14716 14717 /* 14718 * We're going to call each providers per-module provide operation 14719 * specifying only this module. 14720 */ 14721 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 14722 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 14723 14724 mutex_exit(&mod_lock); 14725 mutex_exit(&dtrace_provider_lock); 14726 14727 /* 14728 * If we have any retained enablings, we need to match against them. 14729 * Enabling probes requires that cpu_lock be held, and we cannot hold 14730 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 14731 * module. (In particular, this happens when loading scheduling 14732 * classes.) So if we have any retained enablings, we need to dispatch 14733 * our task queue to do the match for us. 14734 */ 14735 mutex_enter(&dtrace_lock); 14736 14737 if (dtrace_retained == NULL) { 14738 mutex_exit(&dtrace_lock); 14739 return; 14740 } 14741 14742 (void) taskq_dispatch(dtrace_taskq, 14743 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 14744 14745 mutex_exit(&dtrace_lock); 14746 14747 /* 14748 * And now, for a little heuristic sleaze: in general, we want to 14749 * match modules as soon as they load. However, we cannot guarantee 14750 * this, because it would lead us to the lock ordering violation 14751 * outlined above. The common case, of course, is that cpu_lock is 14752 * _not_ held -- so we delay here for a clock tick, hoping that that's 14753 * long enough for the task queue to do its work. If it's not, it's 14754 * not a serious problem -- it just means that the module that we 14755 * just loaded may not be immediately instrumentable. 14756 */ 14757 delay(1); 14758 } 14759 14760 static void 14761 dtrace_module_unloaded(struct modctl *ctl) 14762 { 14763 dtrace_probe_t template, *probe, *first, *next; 14764 dtrace_provider_t *prov; 14765 14766 template.dtpr_mod = ctl->mod_modname; 14767 14768 mutex_enter(&dtrace_provider_lock); 14769 mutex_enter(&mod_lock); 14770 mutex_enter(&dtrace_lock); 14771 14772 if (dtrace_bymod == NULL) { 14773 /* 14774 * The DTrace module is loaded (obviously) but not attached; 14775 * we don't have any work to do. 14776 */ 14777 mutex_exit(&dtrace_provider_lock); 14778 mutex_exit(&mod_lock); 14779 mutex_exit(&dtrace_lock); 14780 return; 14781 } 14782 14783 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 14784 probe != NULL; probe = probe->dtpr_nextmod) { 14785 if (probe->dtpr_ecb != NULL) { 14786 mutex_exit(&dtrace_provider_lock); 14787 mutex_exit(&mod_lock); 14788 mutex_exit(&dtrace_lock); 14789 14790 /* 14791 * This shouldn't _actually_ be possible -- we're 14792 * unloading a module that has an enabled probe in it. 14793 * (It's normally up to the provider to make sure that 14794 * this can't happen.) However, because dtps_enable() 14795 * doesn't have a failure mode, there can be an 14796 * enable/unload race. Upshot: we don't want to 14797 * assert, but we're not going to disable the 14798 * probe, either. 14799 */ 14800 if (dtrace_err_verbose) { 14801 cmn_err(CE_WARN, "unloaded module '%s' had " 14802 "enabled probes", ctl->mod_modname); 14803 } 14804 14805 return; 14806 } 14807 } 14808 14809 probe = first; 14810 14811 for (first = NULL; probe != NULL; probe = next) { 14812 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 14813 14814 dtrace_probes[probe->dtpr_id - 1] = NULL; 14815 14816 next = probe->dtpr_nextmod; 14817 dtrace_hash_remove(dtrace_bymod, probe); 14818 dtrace_hash_remove(dtrace_byfunc, probe); 14819 dtrace_hash_remove(dtrace_byname, probe); 14820 14821 if (first == NULL) { 14822 first = probe; 14823 probe->dtpr_nextmod = NULL; 14824 } else { 14825 probe->dtpr_nextmod = first; 14826 first = probe; 14827 } 14828 } 14829 14830 /* 14831 * We've removed all of the module's probes from the hash chains and 14832 * from the probe array. Now issue a dtrace_sync() to be sure that 14833 * everyone has cleared out from any probe array processing. 14834 */ 14835 dtrace_sync(); 14836 14837 for (probe = first; probe != NULL; probe = first) { 14838 first = probe->dtpr_nextmod; 14839 prov = probe->dtpr_provider; 14840 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 14841 probe->dtpr_arg); 14842 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 14843 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 14844 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 14845 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 14846 kmem_free(probe, sizeof (dtrace_probe_t)); 14847 } 14848 14849 mutex_exit(&dtrace_lock); 14850 mutex_exit(&mod_lock); 14851 mutex_exit(&dtrace_provider_lock); 14852 } 14853 14854 void 14855 dtrace_suspend(void) 14856 { 14857 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 14858 } 14859 14860 void 14861 dtrace_resume(void) 14862 { 14863 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 14864 } 14865 14866 static int 14867 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 14868 { 14869 ASSERT(MUTEX_HELD(&cpu_lock)); 14870 mutex_enter(&dtrace_lock); 14871 14872 switch (what) { 14873 case CPU_CONFIG: { 14874 dtrace_state_t *state; 14875 dtrace_optval_t *opt, rs, c; 14876 14877 /* 14878 * For now, we only allocate a new buffer for anonymous state. 14879 */ 14880 if ((state = dtrace_anon.dta_state) == NULL) 14881 break; 14882 14883 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14884 break; 14885 14886 opt = state->dts_options; 14887 c = opt[DTRACEOPT_CPU]; 14888 14889 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 14890 break; 14891 14892 /* 14893 * Regardless of what the actual policy is, we're going to 14894 * temporarily set our resize policy to be manual. We're 14895 * also going to temporarily set our CPU option to denote 14896 * the newly configured CPU. 14897 */ 14898 rs = opt[DTRACEOPT_BUFRESIZE]; 14899 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 14900 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 14901 14902 (void) dtrace_state_buffers(state); 14903 14904 opt[DTRACEOPT_BUFRESIZE] = rs; 14905 opt[DTRACEOPT_CPU] = c; 14906 14907 break; 14908 } 14909 14910 case CPU_UNCONFIG: 14911 /* 14912 * We don't free the buffer in the CPU_UNCONFIG case. (The 14913 * buffer will be freed when the consumer exits.) 14914 */ 14915 break; 14916 14917 default: 14918 break; 14919 } 14920 14921 mutex_exit(&dtrace_lock); 14922 return (0); 14923 } 14924 14925 static void 14926 dtrace_cpu_setup_initial(processorid_t cpu) 14927 { 14928 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 14929 } 14930 14931 static void 14932 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 14933 { 14934 if (dtrace_toxranges >= dtrace_toxranges_max) { 14935 int osize, nsize; 14936 dtrace_toxrange_t *range; 14937 14938 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14939 14940 if (osize == 0) { 14941 ASSERT(dtrace_toxrange == NULL); 14942 ASSERT(dtrace_toxranges_max == 0); 14943 dtrace_toxranges_max = 1; 14944 } else { 14945 dtrace_toxranges_max <<= 1; 14946 } 14947 14948 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14949 range = kmem_zalloc(nsize, KM_SLEEP); 14950 14951 if (dtrace_toxrange != NULL) { 14952 ASSERT(osize != 0); 14953 bcopy(dtrace_toxrange, range, osize); 14954 kmem_free(dtrace_toxrange, osize); 14955 } 14956 14957 dtrace_toxrange = range; 14958 } 14959 14960 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); 14961 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); 14962 14963 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 14964 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 14965 dtrace_toxranges++; 14966 } 14967 14968 static void 14969 dtrace_getf_barrier() 14970 { 14971 /* 14972 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 14973 * that contain calls to getf(), this routine will be called on every 14974 * closef() before either the underlying vnode is released or the 14975 * file_t itself is freed. By the time we are here, it is essential 14976 * that the file_t can no longer be accessed from a call to getf() 14977 * in probe context -- that assures that a dtrace_sync() can be used 14978 * to clear out any enablings referring to the old structures. 14979 */ 14980 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 14981 kcred->cr_zone->zone_dtrace_getf != 0) 14982 dtrace_sync(); 14983 } 14984 14985 /* 14986 * DTrace Driver Cookbook Functions 14987 */ 14988 /*ARGSUSED*/ 14989 static int 14990 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 14991 { 14992 dtrace_provider_id_t id; 14993 dtrace_state_t *state = NULL; 14994 dtrace_enabling_t *enab; 14995 14996 mutex_enter(&cpu_lock); 14997 mutex_enter(&dtrace_provider_lock); 14998 mutex_enter(&dtrace_lock); 14999 15000 if (ddi_soft_state_init(&dtrace_softstate, 15001 sizeof (dtrace_state_t), 0) != 0) { 15002 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 15003 mutex_exit(&cpu_lock); 15004 mutex_exit(&dtrace_provider_lock); 15005 mutex_exit(&dtrace_lock); 15006 return (DDI_FAILURE); 15007 } 15008 15009 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 15010 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 15011 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 15012 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 15013 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 15014 ddi_remove_minor_node(devi, NULL); 15015 ddi_soft_state_fini(&dtrace_softstate); 15016 mutex_exit(&cpu_lock); 15017 mutex_exit(&dtrace_provider_lock); 15018 mutex_exit(&dtrace_lock); 15019 return (DDI_FAILURE); 15020 } 15021 15022 ddi_report_dev(devi); 15023 dtrace_devi = devi; 15024 15025 dtrace_modload = dtrace_module_loaded; 15026 dtrace_modunload = dtrace_module_unloaded; 15027 dtrace_cpu_init = dtrace_cpu_setup_initial; 15028 dtrace_helpers_cleanup = dtrace_helpers_destroy; 15029 dtrace_helpers_fork = dtrace_helpers_duplicate; 15030 dtrace_cpustart_init = dtrace_suspend; 15031 dtrace_cpustart_fini = dtrace_resume; 15032 dtrace_debugger_init = dtrace_suspend; 15033 dtrace_debugger_fini = dtrace_resume; 15034 15035 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 15036 15037 ASSERT(MUTEX_HELD(&cpu_lock)); 15038 15039 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 15040 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 15041 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 15042 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 15043 VM_SLEEP | VMC_IDENTIFIER); 15044 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 15045 1, INT_MAX, 0); 15046 15047 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 15048 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 15049 NULL, NULL, NULL, NULL, NULL, 0); 15050 15051 ASSERT(MUTEX_HELD(&cpu_lock)); 15052 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 15053 offsetof(dtrace_probe_t, dtpr_nextmod), 15054 offsetof(dtrace_probe_t, dtpr_prevmod)); 15055 15056 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 15057 offsetof(dtrace_probe_t, dtpr_nextfunc), 15058 offsetof(dtrace_probe_t, dtpr_prevfunc)); 15059 15060 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 15061 offsetof(dtrace_probe_t, dtpr_nextname), 15062 offsetof(dtrace_probe_t, dtpr_prevname)); 15063 15064 if (dtrace_retain_max < 1) { 15065 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 15066 "setting to 1", dtrace_retain_max); 15067 dtrace_retain_max = 1; 15068 } 15069 15070 /* 15071 * Now discover our toxic ranges. 15072 */ 15073 dtrace_toxic_ranges(dtrace_toxrange_add); 15074 15075 /* 15076 * Before we register ourselves as a provider to our own framework, 15077 * we would like to assert that dtrace_provider is NULL -- but that's 15078 * not true if we were loaded as a dependency of a DTrace provider. 15079 * Once we've registered, we can assert that dtrace_provider is our 15080 * pseudo provider. 15081 */ 15082 (void) dtrace_register("dtrace", &dtrace_provider_attr, 15083 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 15084 15085 ASSERT(dtrace_provider != NULL); 15086 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 15087 15088 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 15089 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 15090 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 15091 dtrace_provider, NULL, NULL, "END", 0, NULL); 15092 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 15093 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 15094 15095 dtrace_anon_property(); 15096 mutex_exit(&cpu_lock); 15097 15098 /* 15099 * If there are already providers, we must ask them to provide their 15100 * probes, and then match any anonymous enabling against them. Note 15101 * that there should be no other retained enablings at this time: 15102 * the only retained enablings at this time should be the anonymous 15103 * enabling. 15104 */ 15105 if (dtrace_anon.dta_enabling != NULL) { 15106 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 15107 15108 dtrace_enabling_provide(NULL); 15109 state = dtrace_anon.dta_state; 15110 15111 /* 15112 * We couldn't hold cpu_lock across the above call to 15113 * dtrace_enabling_provide(), but we must hold it to actually 15114 * enable the probes. We have to drop all of our locks, pick 15115 * up cpu_lock, and regain our locks before matching the 15116 * retained anonymous enabling. 15117 */ 15118 mutex_exit(&dtrace_lock); 15119 mutex_exit(&dtrace_provider_lock); 15120 15121 mutex_enter(&cpu_lock); 15122 mutex_enter(&dtrace_provider_lock); 15123 mutex_enter(&dtrace_lock); 15124 15125 if ((enab = dtrace_anon.dta_enabling) != NULL) 15126 (void) dtrace_enabling_match(enab, NULL); 15127 15128 mutex_exit(&cpu_lock); 15129 } 15130 15131 mutex_exit(&dtrace_lock); 15132 mutex_exit(&dtrace_provider_lock); 15133 15134 if (state != NULL) { 15135 /* 15136 * If we created any anonymous state, set it going now. 15137 */ 15138 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 15139 } 15140 15141 return (DDI_SUCCESS); 15142 } 15143 15144 /*ARGSUSED*/ 15145 static int 15146 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 15147 { 15148 dtrace_state_t *state; 15149 uint32_t priv; 15150 uid_t uid; 15151 zoneid_t zoneid; 15152 15153 if (getminor(*devp) == DTRACEMNRN_HELPER) 15154 return (0); 15155 15156 /* 15157 * If this wasn't an open with the "helper" minor, then it must be 15158 * the "dtrace" minor. 15159 */ 15160 if (getminor(*devp) != DTRACEMNRN_DTRACE) 15161 return (ENXIO); 15162 15163 /* 15164 * If no DTRACE_PRIV_* bits are set in the credential, then the 15165 * caller lacks sufficient permission to do anything with DTrace. 15166 */ 15167 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 15168 if (priv == DTRACE_PRIV_NONE) 15169 return (EACCES); 15170 15171 /* 15172 * Ask all providers to provide all their probes. 15173 */ 15174 mutex_enter(&dtrace_provider_lock); 15175 dtrace_probe_provide(NULL, NULL); 15176 mutex_exit(&dtrace_provider_lock); 15177 15178 mutex_enter(&cpu_lock); 15179 mutex_enter(&dtrace_lock); 15180 dtrace_opens++; 15181 dtrace_membar_producer(); 15182 15183 /* 15184 * If the kernel debugger is active (that is, if the kernel debugger 15185 * modified text in some way), we won't allow the open. 15186 */ 15187 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15188 dtrace_opens--; 15189 mutex_exit(&cpu_lock); 15190 mutex_exit(&dtrace_lock); 15191 return (EBUSY); 15192 } 15193 15194 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) { 15195 /* 15196 * If DTrace helper tracing is enabled, we need to allocate the 15197 * trace buffer and initialize the values. 15198 */ 15199 dtrace_helptrace_buffer = 15200 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 15201 dtrace_helptrace_next = 0; 15202 dtrace_helptrace_wrapped = 0; 15203 dtrace_helptrace_enable = 0; 15204 } 15205 15206 state = dtrace_state_create(devp, cred_p); 15207 mutex_exit(&cpu_lock); 15208 15209 if (state == NULL) { 15210 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 15211 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15212 mutex_exit(&dtrace_lock); 15213 return (EAGAIN); 15214 } 15215 15216 mutex_exit(&dtrace_lock); 15217 15218 return (0); 15219 } 15220 15221 /*ARGSUSED*/ 15222 static int 15223 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 15224 { 15225 minor_t minor = getminor(dev); 15226 dtrace_state_t *state; 15227 dtrace_helptrace_t *buf = NULL; 15228 15229 if (minor == DTRACEMNRN_HELPER) 15230 return (0); 15231 15232 state = ddi_get_soft_state(dtrace_softstate, minor); 15233 15234 mutex_enter(&cpu_lock); 15235 mutex_enter(&dtrace_lock); 15236 15237 if (state->dts_anon) { 15238 /* 15239 * There is anonymous state. Destroy that first. 15240 */ 15241 ASSERT(dtrace_anon.dta_state == NULL); 15242 dtrace_state_destroy(state->dts_anon); 15243 } 15244 15245 if (dtrace_helptrace_disable) { 15246 /* 15247 * If we have been told to disable helper tracing, set the 15248 * buffer to NULL before calling into dtrace_state_destroy(); 15249 * we take advantage of its dtrace_sync() to know that no 15250 * CPU is in probe context with enabled helper tracing 15251 * after it returns. 15252 */ 15253 buf = dtrace_helptrace_buffer; 15254 dtrace_helptrace_buffer = NULL; 15255 } 15256 15257 dtrace_state_destroy(state); 15258 ASSERT(dtrace_opens > 0); 15259 15260 /* 15261 * Only relinquish control of the kernel debugger interface when there 15262 * are no consumers and no anonymous enablings. 15263 */ 15264 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 15265 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15266 15267 if (buf != NULL) { 15268 kmem_free(buf, dtrace_helptrace_bufsize); 15269 dtrace_helptrace_disable = 0; 15270 } 15271 15272 mutex_exit(&dtrace_lock); 15273 mutex_exit(&cpu_lock); 15274 15275 return (0); 15276 } 15277 15278 /*ARGSUSED*/ 15279 static int 15280 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 15281 { 15282 int rval; 15283 dof_helper_t help, *dhp = NULL; 15284 15285 switch (cmd) { 15286 case DTRACEHIOC_ADDDOF: 15287 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 15288 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 15289 return (EFAULT); 15290 } 15291 15292 dhp = &help; 15293 arg = (intptr_t)help.dofhp_dof; 15294 /*FALLTHROUGH*/ 15295 15296 case DTRACEHIOC_ADD: { 15297 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 15298 15299 if (dof == NULL) 15300 return (rval); 15301 15302 mutex_enter(&dtrace_lock); 15303 15304 /* 15305 * dtrace_helper_slurp() takes responsibility for the dof -- 15306 * it may free it now or it may save it and free it later. 15307 */ 15308 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 15309 *rv = rval; 15310 rval = 0; 15311 } else { 15312 rval = EINVAL; 15313 } 15314 15315 mutex_exit(&dtrace_lock); 15316 return (rval); 15317 } 15318 15319 case DTRACEHIOC_REMOVE: { 15320 mutex_enter(&dtrace_lock); 15321 rval = dtrace_helper_destroygen(arg); 15322 mutex_exit(&dtrace_lock); 15323 15324 return (rval); 15325 } 15326 15327 default: 15328 break; 15329 } 15330 15331 return (ENOTTY); 15332 } 15333 15334 /*ARGSUSED*/ 15335 static int 15336 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 15337 { 15338 minor_t minor = getminor(dev); 15339 dtrace_state_t *state; 15340 int rval; 15341 15342 if (minor == DTRACEMNRN_HELPER) 15343 return (dtrace_ioctl_helper(cmd, arg, rv)); 15344 15345 state = ddi_get_soft_state(dtrace_softstate, minor); 15346 15347 if (state->dts_anon) { 15348 ASSERT(dtrace_anon.dta_state == NULL); 15349 state = state->dts_anon; 15350 } 15351 15352 switch (cmd) { 15353 case DTRACEIOC_PROVIDER: { 15354 dtrace_providerdesc_t pvd; 15355 dtrace_provider_t *pvp; 15356 15357 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 15358 return (EFAULT); 15359 15360 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 15361 mutex_enter(&dtrace_provider_lock); 15362 15363 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 15364 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 15365 break; 15366 } 15367 15368 mutex_exit(&dtrace_provider_lock); 15369 15370 if (pvp == NULL) 15371 return (ESRCH); 15372 15373 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 15374 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 15375 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 15376 return (EFAULT); 15377 15378 return (0); 15379 } 15380 15381 case DTRACEIOC_EPROBE: { 15382 dtrace_eprobedesc_t epdesc; 15383 dtrace_ecb_t *ecb; 15384 dtrace_action_t *act; 15385 void *buf; 15386 size_t size; 15387 uintptr_t dest; 15388 int nrecs; 15389 15390 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 15391 return (EFAULT); 15392 15393 mutex_enter(&dtrace_lock); 15394 15395 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 15396 mutex_exit(&dtrace_lock); 15397 return (EINVAL); 15398 } 15399 15400 if (ecb->dte_probe == NULL) { 15401 mutex_exit(&dtrace_lock); 15402 return (EINVAL); 15403 } 15404 15405 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 15406 epdesc.dtepd_uarg = ecb->dte_uarg; 15407 epdesc.dtepd_size = ecb->dte_size; 15408 15409 nrecs = epdesc.dtepd_nrecs; 15410 epdesc.dtepd_nrecs = 0; 15411 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15412 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15413 continue; 15414 15415 epdesc.dtepd_nrecs++; 15416 } 15417 15418 /* 15419 * Now that we have the size, we need to allocate a temporary 15420 * buffer in which to store the complete description. We need 15421 * the temporary buffer to be able to drop dtrace_lock() 15422 * across the copyout(), below. 15423 */ 15424 size = sizeof (dtrace_eprobedesc_t) + 15425 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 15426 15427 buf = kmem_alloc(size, KM_SLEEP); 15428 dest = (uintptr_t)buf; 15429 15430 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 15431 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 15432 15433 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15434 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15435 continue; 15436 15437 if (nrecs-- == 0) 15438 break; 15439 15440 bcopy(&act->dta_rec, (void *)dest, 15441 sizeof (dtrace_recdesc_t)); 15442 dest += sizeof (dtrace_recdesc_t); 15443 } 15444 15445 mutex_exit(&dtrace_lock); 15446 15447 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15448 kmem_free(buf, size); 15449 return (EFAULT); 15450 } 15451 15452 kmem_free(buf, size); 15453 return (0); 15454 } 15455 15456 case DTRACEIOC_AGGDESC: { 15457 dtrace_aggdesc_t aggdesc; 15458 dtrace_action_t *act; 15459 dtrace_aggregation_t *agg; 15460 int nrecs; 15461 uint32_t offs; 15462 dtrace_recdesc_t *lrec; 15463 void *buf; 15464 size_t size; 15465 uintptr_t dest; 15466 15467 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 15468 return (EFAULT); 15469 15470 mutex_enter(&dtrace_lock); 15471 15472 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 15473 mutex_exit(&dtrace_lock); 15474 return (EINVAL); 15475 } 15476 15477 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 15478 15479 nrecs = aggdesc.dtagd_nrecs; 15480 aggdesc.dtagd_nrecs = 0; 15481 15482 offs = agg->dtag_base; 15483 lrec = &agg->dtag_action.dta_rec; 15484 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 15485 15486 for (act = agg->dtag_first; ; act = act->dta_next) { 15487 ASSERT(act->dta_intuple || 15488 DTRACEACT_ISAGG(act->dta_kind)); 15489 15490 /* 15491 * If this action has a record size of zero, it 15492 * denotes an argument to the aggregating action. 15493 * Because the presence of this record doesn't (or 15494 * shouldn't) affect the way the data is interpreted, 15495 * we don't copy it out to save user-level the 15496 * confusion of dealing with a zero-length record. 15497 */ 15498 if (act->dta_rec.dtrd_size == 0) { 15499 ASSERT(agg->dtag_hasarg); 15500 continue; 15501 } 15502 15503 aggdesc.dtagd_nrecs++; 15504 15505 if (act == &agg->dtag_action) 15506 break; 15507 } 15508 15509 /* 15510 * Now that we have the size, we need to allocate a temporary 15511 * buffer in which to store the complete description. We need 15512 * the temporary buffer to be able to drop dtrace_lock() 15513 * across the copyout(), below. 15514 */ 15515 size = sizeof (dtrace_aggdesc_t) + 15516 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 15517 15518 buf = kmem_alloc(size, KM_SLEEP); 15519 dest = (uintptr_t)buf; 15520 15521 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 15522 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 15523 15524 for (act = agg->dtag_first; ; act = act->dta_next) { 15525 dtrace_recdesc_t rec = act->dta_rec; 15526 15527 /* 15528 * See the comment in the above loop for why we pass 15529 * over zero-length records. 15530 */ 15531 if (rec.dtrd_size == 0) { 15532 ASSERT(agg->dtag_hasarg); 15533 continue; 15534 } 15535 15536 if (nrecs-- == 0) 15537 break; 15538 15539 rec.dtrd_offset -= offs; 15540 bcopy(&rec, (void *)dest, sizeof (rec)); 15541 dest += sizeof (dtrace_recdesc_t); 15542 15543 if (act == &agg->dtag_action) 15544 break; 15545 } 15546 15547 mutex_exit(&dtrace_lock); 15548 15549 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15550 kmem_free(buf, size); 15551 return (EFAULT); 15552 } 15553 15554 kmem_free(buf, size); 15555 return (0); 15556 } 15557 15558 case DTRACEIOC_ENABLE: { 15559 dof_hdr_t *dof; 15560 dtrace_enabling_t *enab = NULL; 15561 dtrace_vstate_t *vstate; 15562 int err = 0; 15563 15564 *rv = 0; 15565 15566 /* 15567 * If a NULL argument has been passed, we take this as our 15568 * cue to reevaluate our enablings. 15569 */ 15570 if (arg == NULL) { 15571 dtrace_enabling_matchall(); 15572 15573 return (0); 15574 } 15575 15576 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 15577 return (rval); 15578 15579 mutex_enter(&cpu_lock); 15580 mutex_enter(&dtrace_lock); 15581 vstate = &state->dts_vstate; 15582 15583 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 15584 mutex_exit(&dtrace_lock); 15585 mutex_exit(&cpu_lock); 15586 dtrace_dof_destroy(dof); 15587 return (EBUSY); 15588 } 15589 15590 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 15591 mutex_exit(&dtrace_lock); 15592 mutex_exit(&cpu_lock); 15593 dtrace_dof_destroy(dof); 15594 return (EINVAL); 15595 } 15596 15597 if ((rval = dtrace_dof_options(dof, state)) != 0) { 15598 dtrace_enabling_destroy(enab); 15599 mutex_exit(&dtrace_lock); 15600 mutex_exit(&cpu_lock); 15601 dtrace_dof_destroy(dof); 15602 return (rval); 15603 } 15604 15605 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 15606 err = dtrace_enabling_retain(enab); 15607 } else { 15608 dtrace_enabling_destroy(enab); 15609 } 15610 15611 mutex_exit(&cpu_lock); 15612 mutex_exit(&dtrace_lock); 15613 dtrace_dof_destroy(dof); 15614 15615 return (err); 15616 } 15617 15618 case DTRACEIOC_REPLICATE: { 15619 dtrace_repldesc_t desc; 15620 dtrace_probedesc_t *match = &desc.dtrpd_match; 15621 dtrace_probedesc_t *create = &desc.dtrpd_create; 15622 int err; 15623 15624 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15625 return (EFAULT); 15626 15627 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15628 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15629 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15630 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15631 15632 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15633 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15634 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15635 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15636 15637 mutex_enter(&dtrace_lock); 15638 err = dtrace_enabling_replicate(state, match, create); 15639 mutex_exit(&dtrace_lock); 15640 15641 return (err); 15642 } 15643 15644 case DTRACEIOC_PROBEMATCH: 15645 case DTRACEIOC_PROBES: { 15646 dtrace_probe_t *probe = NULL; 15647 dtrace_probedesc_t desc; 15648 dtrace_probekey_t pkey; 15649 dtrace_id_t i; 15650 int m = 0; 15651 uint32_t priv; 15652 uid_t uid; 15653 zoneid_t zoneid; 15654 15655 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15656 return (EFAULT); 15657 15658 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15659 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15660 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15661 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15662 15663 /* 15664 * Before we attempt to match this probe, we want to give 15665 * all providers the opportunity to provide it. 15666 */ 15667 if (desc.dtpd_id == DTRACE_IDNONE) { 15668 mutex_enter(&dtrace_provider_lock); 15669 dtrace_probe_provide(&desc, NULL); 15670 mutex_exit(&dtrace_provider_lock); 15671 desc.dtpd_id++; 15672 } 15673 15674 if (cmd == DTRACEIOC_PROBEMATCH) { 15675 dtrace_probekey(&desc, &pkey); 15676 pkey.dtpk_id = DTRACE_IDNONE; 15677 } 15678 15679 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 15680 15681 mutex_enter(&dtrace_lock); 15682 15683 if (cmd == DTRACEIOC_PROBEMATCH) { 15684 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15685 if ((probe = dtrace_probes[i - 1]) != NULL && 15686 (m = dtrace_match_probe(probe, &pkey, 15687 priv, uid, zoneid)) != 0) 15688 break; 15689 } 15690 15691 if (m < 0) { 15692 mutex_exit(&dtrace_lock); 15693 return (EINVAL); 15694 } 15695 15696 } else { 15697 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15698 if ((probe = dtrace_probes[i - 1]) != NULL && 15699 dtrace_match_priv(probe, priv, uid, zoneid)) 15700 break; 15701 } 15702 } 15703 15704 if (probe == NULL) { 15705 mutex_exit(&dtrace_lock); 15706 return (ESRCH); 15707 } 15708 15709 dtrace_probe_description(probe, &desc); 15710 mutex_exit(&dtrace_lock); 15711 15712 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15713 return (EFAULT); 15714 15715 return (0); 15716 } 15717 15718 case DTRACEIOC_PROBEARG: { 15719 dtrace_argdesc_t desc; 15720 dtrace_probe_t *probe; 15721 dtrace_provider_t *prov; 15722 15723 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15724 return (EFAULT); 15725 15726 if (desc.dtargd_id == DTRACE_IDNONE) 15727 return (EINVAL); 15728 15729 if (desc.dtargd_ndx == DTRACE_ARGNONE) 15730 return (EINVAL); 15731 15732 mutex_enter(&dtrace_provider_lock); 15733 mutex_enter(&mod_lock); 15734 mutex_enter(&dtrace_lock); 15735 15736 if (desc.dtargd_id > dtrace_nprobes) { 15737 mutex_exit(&dtrace_lock); 15738 mutex_exit(&mod_lock); 15739 mutex_exit(&dtrace_provider_lock); 15740 return (EINVAL); 15741 } 15742 15743 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 15744 mutex_exit(&dtrace_lock); 15745 mutex_exit(&mod_lock); 15746 mutex_exit(&dtrace_provider_lock); 15747 return (EINVAL); 15748 } 15749 15750 mutex_exit(&dtrace_lock); 15751 15752 prov = probe->dtpr_provider; 15753 15754 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 15755 /* 15756 * There isn't any typed information for this probe. 15757 * Set the argument number to DTRACE_ARGNONE. 15758 */ 15759 desc.dtargd_ndx = DTRACE_ARGNONE; 15760 } else { 15761 desc.dtargd_native[0] = '\0'; 15762 desc.dtargd_xlate[0] = '\0'; 15763 desc.dtargd_mapping = desc.dtargd_ndx; 15764 15765 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 15766 probe->dtpr_id, probe->dtpr_arg, &desc); 15767 } 15768 15769 mutex_exit(&mod_lock); 15770 mutex_exit(&dtrace_provider_lock); 15771 15772 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15773 return (EFAULT); 15774 15775 return (0); 15776 } 15777 15778 case DTRACEIOC_GO: { 15779 processorid_t cpuid; 15780 rval = dtrace_state_go(state, &cpuid); 15781 15782 if (rval != 0) 15783 return (rval); 15784 15785 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15786 return (EFAULT); 15787 15788 return (0); 15789 } 15790 15791 case DTRACEIOC_STOP: { 15792 processorid_t cpuid; 15793 15794 mutex_enter(&dtrace_lock); 15795 rval = dtrace_state_stop(state, &cpuid); 15796 mutex_exit(&dtrace_lock); 15797 15798 if (rval != 0) 15799 return (rval); 15800 15801 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15802 return (EFAULT); 15803 15804 return (0); 15805 } 15806 15807 case DTRACEIOC_DOFGET: { 15808 dof_hdr_t hdr, *dof; 15809 uint64_t len; 15810 15811 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 15812 return (EFAULT); 15813 15814 mutex_enter(&dtrace_lock); 15815 dof = dtrace_dof_create(state); 15816 mutex_exit(&dtrace_lock); 15817 15818 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 15819 rval = copyout(dof, (void *)arg, len); 15820 dtrace_dof_destroy(dof); 15821 15822 return (rval == 0 ? 0 : EFAULT); 15823 } 15824 15825 case DTRACEIOC_AGGSNAP: 15826 case DTRACEIOC_BUFSNAP: { 15827 dtrace_bufdesc_t desc; 15828 caddr_t cached; 15829 dtrace_buffer_t *buf; 15830 15831 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15832 return (EFAULT); 15833 15834 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 15835 return (EINVAL); 15836 15837 mutex_enter(&dtrace_lock); 15838 15839 if (cmd == DTRACEIOC_BUFSNAP) { 15840 buf = &state->dts_buffer[desc.dtbd_cpu]; 15841 } else { 15842 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 15843 } 15844 15845 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 15846 size_t sz = buf->dtb_offset; 15847 15848 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 15849 mutex_exit(&dtrace_lock); 15850 return (EBUSY); 15851 } 15852 15853 /* 15854 * If this buffer has already been consumed, we're 15855 * going to indicate that there's nothing left here 15856 * to consume. 15857 */ 15858 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 15859 mutex_exit(&dtrace_lock); 15860 15861 desc.dtbd_size = 0; 15862 desc.dtbd_drops = 0; 15863 desc.dtbd_errors = 0; 15864 desc.dtbd_oldest = 0; 15865 sz = sizeof (desc); 15866 15867 if (copyout(&desc, (void *)arg, sz) != 0) 15868 return (EFAULT); 15869 15870 return (0); 15871 } 15872 15873 /* 15874 * If this is a ring buffer that has wrapped, we want 15875 * to copy the whole thing out. 15876 */ 15877 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 15878 dtrace_buffer_polish(buf); 15879 sz = buf->dtb_size; 15880 } 15881 15882 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 15883 mutex_exit(&dtrace_lock); 15884 return (EFAULT); 15885 } 15886 15887 desc.dtbd_size = sz; 15888 desc.dtbd_drops = buf->dtb_drops; 15889 desc.dtbd_errors = buf->dtb_errors; 15890 desc.dtbd_oldest = buf->dtb_xamot_offset; 15891 desc.dtbd_timestamp = dtrace_gethrtime(); 15892 15893 mutex_exit(&dtrace_lock); 15894 15895 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15896 return (EFAULT); 15897 15898 buf->dtb_flags |= DTRACEBUF_CONSUMED; 15899 15900 return (0); 15901 } 15902 15903 if (buf->dtb_tomax == NULL) { 15904 ASSERT(buf->dtb_xamot == NULL); 15905 mutex_exit(&dtrace_lock); 15906 return (ENOENT); 15907 } 15908 15909 cached = buf->dtb_tomax; 15910 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 15911 15912 dtrace_xcall(desc.dtbd_cpu, 15913 (dtrace_xcall_t)dtrace_buffer_switch, buf); 15914 15915 state->dts_errors += buf->dtb_xamot_errors; 15916 15917 /* 15918 * If the buffers did not actually switch, then the cross call 15919 * did not take place -- presumably because the given CPU is 15920 * not in the ready set. If this is the case, we'll return 15921 * ENOENT. 15922 */ 15923 if (buf->dtb_tomax == cached) { 15924 ASSERT(buf->dtb_xamot != cached); 15925 mutex_exit(&dtrace_lock); 15926 return (ENOENT); 15927 } 15928 15929 ASSERT(cached == buf->dtb_xamot); 15930 15931 /* 15932 * We have our snapshot; now copy it out. 15933 */ 15934 if (copyout(buf->dtb_xamot, desc.dtbd_data, 15935 buf->dtb_xamot_offset) != 0) { 15936 mutex_exit(&dtrace_lock); 15937 return (EFAULT); 15938 } 15939 15940 desc.dtbd_size = buf->dtb_xamot_offset; 15941 desc.dtbd_drops = buf->dtb_xamot_drops; 15942 desc.dtbd_errors = buf->dtb_xamot_errors; 15943 desc.dtbd_oldest = 0; 15944 desc.dtbd_timestamp = buf->dtb_switched; 15945 15946 mutex_exit(&dtrace_lock); 15947 15948 /* 15949 * Finally, copy out the buffer description. 15950 */ 15951 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15952 return (EFAULT); 15953 15954 return (0); 15955 } 15956 15957 case DTRACEIOC_CONF: { 15958 dtrace_conf_t conf; 15959 15960 bzero(&conf, sizeof (conf)); 15961 conf.dtc_difversion = DIF_VERSION; 15962 conf.dtc_difintregs = DIF_DIR_NREGS; 15963 conf.dtc_diftupregs = DIF_DTR_NREGS; 15964 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 15965 15966 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 15967 return (EFAULT); 15968 15969 return (0); 15970 } 15971 15972 case DTRACEIOC_STATUS: { 15973 dtrace_status_t stat; 15974 dtrace_dstate_t *dstate; 15975 int i, j; 15976 uint64_t nerrs; 15977 15978 /* 15979 * See the comment in dtrace_state_deadman() for the reason 15980 * for setting dts_laststatus to INT64_MAX before setting 15981 * it to the correct value. 15982 */ 15983 state->dts_laststatus = INT64_MAX; 15984 dtrace_membar_producer(); 15985 state->dts_laststatus = dtrace_gethrtime(); 15986 15987 bzero(&stat, sizeof (stat)); 15988 15989 mutex_enter(&dtrace_lock); 15990 15991 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 15992 mutex_exit(&dtrace_lock); 15993 return (ENOENT); 15994 } 15995 15996 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 15997 stat.dtst_exiting = 1; 15998 15999 nerrs = state->dts_errors; 16000 dstate = &state->dts_vstate.dtvs_dynvars; 16001 16002 for (i = 0; i < NCPU; i++) { 16003 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 16004 16005 stat.dtst_dyndrops += dcpu->dtdsc_drops; 16006 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 16007 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 16008 16009 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 16010 stat.dtst_filled++; 16011 16012 nerrs += state->dts_buffer[i].dtb_errors; 16013 16014 for (j = 0; j < state->dts_nspeculations; j++) { 16015 dtrace_speculation_t *spec; 16016 dtrace_buffer_t *buf; 16017 16018 spec = &state->dts_speculations[j]; 16019 buf = &spec->dtsp_buffer[i]; 16020 stat.dtst_specdrops += buf->dtb_xamot_drops; 16021 } 16022 } 16023 16024 stat.dtst_specdrops_busy = state->dts_speculations_busy; 16025 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 16026 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 16027 stat.dtst_dblerrors = state->dts_dblerrors; 16028 stat.dtst_killed = 16029 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 16030 stat.dtst_errors = nerrs; 16031 16032 mutex_exit(&dtrace_lock); 16033 16034 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 16035 return (EFAULT); 16036 16037 return (0); 16038 } 16039 16040 case DTRACEIOC_FORMAT: { 16041 dtrace_fmtdesc_t fmt; 16042 char *str; 16043 int len; 16044 16045 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 16046 return (EFAULT); 16047 16048 mutex_enter(&dtrace_lock); 16049 16050 if (fmt.dtfd_format == 0 || 16051 fmt.dtfd_format > state->dts_nformats) { 16052 mutex_exit(&dtrace_lock); 16053 return (EINVAL); 16054 } 16055 16056 /* 16057 * Format strings are allocated contiguously and they are 16058 * never freed; if a format index is less than the number 16059 * of formats, we can assert that the format map is non-NULL 16060 * and that the format for the specified index is non-NULL. 16061 */ 16062 ASSERT(state->dts_formats != NULL); 16063 str = state->dts_formats[fmt.dtfd_format - 1]; 16064 ASSERT(str != NULL); 16065 16066 len = strlen(str) + 1; 16067 16068 if (len > fmt.dtfd_length) { 16069 fmt.dtfd_length = len; 16070 16071 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 16072 mutex_exit(&dtrace_lock); 16073 return (EINVAL); 16074 } 16075 } else { 16076 if (copyout(str, fmt.dtfd_string, len) != 0) { 16077 mutex_exit(&dtrace_lock); 16078 return (EINVAL); 16079 } 16080 } 16081 16082 mutex_exit(&dtrace_lock); 16083 return (0); 16084 } 16085 16086 default: 16087 break; 16088 } 16089 16090 return (ENOTTY); 16091 } 16092 16093 /*ARGSUSED*/ 16094 static int 16095 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 16096 { 16097 dtrace_state_t *state; 16098 16099 switch (cmd) { 16100 case DDI_DETACH: 16101 break; 16102 16103 case DDI_SUSPEND: 16104 return (DDI_SUCCESS); 16105 16106 default: 16107 return (DDI_FAILURE); 16108 } 16109 16110 mutex_enter(&cpu_lock); 16111 mutex_enter(&dtrace_provider_lock); 16112 mutex_enter(&dtrace_lock); 16113 16114 ASSERT(dtrace_opens == 0); 16115 16116 if (dtrace_helpers > 0) { 16117 mutex_exit(&dtrace_provider_lock); 16118 mutex_exit(&dtrace_lock); 16119 mutex_exit(&cpu_lock); 16120 return (DDI_FAILURE); 16121 } 16122 16123 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 16124 mutex_exit(&dtrace_provider_lock); 16125 mutex_exit(&dtrace_lock); 16126 mutex_exit(&cpu_lock); 16127 return (DDI_FAILURE); 16128 } 16129 16130 dtrace_provider = NULL; 16131 16132 if ((state = dtrace_anon_grab()) != NULL) { 16133 /* 16134 * If there were ECBs on this state, the provider should 16135 * have not been allowed to detach; assert that there is 16136 * none. 16137 */ 16138 ASSERT(state->dts_necbs == 0); 16139 dtrace_state_destroy(state); 16140 16141 /* 16142 * If we're being detached with anonymous state, we need to 16143 * indicate to the kernel debugger that DTrace is now inactive. 16144 */ 16145 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16146 } 16147 16148 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 16149 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16150 dtrace_cpu_init = NULL; 16151 dtrace_helpers_cleanup = NULL; 16152 dtrace_helpers_fork = NULL; 16153 dtrace_cpustart_init = NULL; 16154 dtrace_cpustart_fini = NULL; 16155 dtrace_debugger_init = NULL; 16156 dtrace_debugger_fini = NULL; 16157 dtrace_modload = NULL; 16158 dtrace_modunload = NULL; 16159 16160 ASSERT(dtrace_getf == 0); 16161 ASSERT(dtrace_closef == NULL); 16162 16163 mutex_exit(&cpu_lock); 16164 16165 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 16166 dtrace_probes = NULL; 16167 dtrace_nprobes = 0; 16168 16169 dtrace_hash_destroy(dtrace_bymod); 16170 dtrace_hash_destroy(dtrace_byfunc); 16171 dtrace_hash_destroy(dtrace_byname); 16172 dtrace_bymod = NULL; 16173 dtrace_byfunc = NULL; 16174 dtrace_byname = NULL; 16175 16176 kmem_cache_destroy(dtrace_state_cache); 16177 vmem_destroy(dtrace_minor); 16178 vmem_destroy(dtrace_arena); 16179 16180 if (dtrace_toxrange != NULL) { 16181 kmem_free(dtrace_toxrange, 16182 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 16183 dtrace_toxrange = NULL; 16184 dtrace_toxranges = 0; 16185 dtrace_toxranges_max = 0; 16186 } 16187 16188 ddi_remove_minor_node(dtrace_devi, NULL); 16189 dtrace_devi = NULL; 16190 16191 ddi_soft_state_fini(&dtrace_softstate); 16192 16193 ASSERT(dtrace_vtime_references == 0); 16194 ASSERT(dtrace_opens == 0); 16195 ASSERT(dtrace_retained == NULL); 16196 16197 mutex_exit(&dtrace_lock); 16198 mutex_exit(&dtrace_provider_lock); 16199 16200 /* 16201 * We don't destroy the task queue until after we have dropped our 16202 * locks (taskq_destroy() may block on running tasks). To prevent 16203 * attempting to do work after we have effectively detached but before 16204 * the task queue has been destroyed, all tasks dispatched via the 16205 * task queue must check that DTrace is still attached before 16206 * performing any operation. 16207 */ 16208 taskq_destroy(dtrace_taskq); 16209 dtrace_taskq = NULL; 16210 16211 return (DDI_SUCCESS); 16212 } 16213 16214 /*ARGSUSED*/ 16215 static int 16216 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 16217 { 16218 int error; 16219 16220 switch (infocmd) { 16221 case DDI_INFO_DEVT2DEVINFO: 16222 *result = (void *)dtrace_devi; 16223 error = DDI_SUCCESS; 16224 break; 16225 case DDI_INFO_DEVT2INSTANCE: 16226 *result = (void *)0; 16227 error = DDI_SUCCESS; 16228 break; 16229 default: 16230 error = DDI_FAILURE; 16231 } 16232 return (error); 16233 } 16234 16235 static struct cb_ops dtrace_cb_ops = { 16236 dtrace_open, /* open */ 16237 dtrace_close, /* close */ 16238 nulldev, /* strategy */ 16239 nulldev, /* print */ 16240 nodev, /* dump */ 16241 nodev, /* read */ 16242 nodev, /* write */ 16243 dtrace_ioctl, /* ioctl */ 16244 nodev, /* devmap */ 16245 nodev, /* mmap */ 16246 nodev, /* segmap */ 16247 nochpoll, /* poll */ 16248 ddi_prop_op, /* cb_prop_op */ 16249 0, /* streamtab */ 16250 D_NEW | D_MP /* Driver compatibility flag */ 16251 }; 16252 16253 static struct dev_ops dtrace_ops = { 16254 DEVO_REV, /* devo_rev */ 16255 0, /* refcnt */ 16256 dtrace_info, /* get_dev_info */ 16257 nulldev, /* identify */ 16258 nulldev, /* probe */ 16259 dtrace_attach, /* attach */ 16260 dtrace_detach, /* detach */ 16261 nodev, /* reset */ 16262 &dtrace_cb_ops, /* driver operations */ 16263 NULL, /* bus operations */ 16264 nodev, /* dev power */ 16265 ddi_quiesce_not_needed, /* quiesce */ 16266 }; 16267 16268 static struct modldrv modldrv = { 16269 &mod_driverops, /* module type (this is a pseudo driver) */ 16270 "Dynamic Tracing", /* name of module */ 16271 &dtrace_ops, /* driver ops */ 16272 }; 16273 16274 static struct modlinkage modlinkage = { 16275 MODREV_1, 16276 (void *)&modldrv, 16277 NULL 16278 }; 16279 16280 int 16281 _init(void) 16282 { 16283 return (mod_install(&modlinkage)); 16284 } 16285 16286 int 16287 _info(struct modinfo *modinfop) 16288 { 16289 return (mod_info(&modlinkage, modinfop)); 16290 } 16291 16292 int 16293 _fini(void) 16294 { 16295 return (mod_remove(&modlinkage)); 16296 }