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 * $FreeBSD$ 22 */ 23 24 /* 25 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 26 * Copyright (c) 2015, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 28 */ 29 30 /* 31 * DTrace - Dynamic Tracing for Solaris 32 * 33 * This is the implementation of the Solaris Dynamic Tracing framework 34 * (DTrace). The user-visible interface to DTrace is described at length in 35 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 36 * library, the in-kernel DTrace framework, and the DTrace providers are 37 * described in the block comments in the <sys/dtrace.h> header file. The 38 * internal architecture of DTrace is described in the block comments in the 39 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 40 * implementation very much assume mastery of all of these sources; if one has 41 * an unanswered question about the implementation, one should consult them 42 * first. 43 * 44 * The functions here are ordered roughly as follows: 45 * 46 * - Probe context functions 47 * - Probe hashing functions 48 * - Non-probe context utility functions 49 * - Matching functions 50 * - Provider-to-Framework API functions 51 * - Probe management functions 52 * - DIF object functions 53 * - Format functions 54 * - Predicate functions 55 * - ECB functions 56 * - Buffer functions 57 * - Enabling functions 58 * - DOF functions 59 * - Anonymous enabling functions 60 * - Consumer state functions 61 * - Helper functions 62 * - Hook functions 63 * - Driver cookbook functions 64 * 65 * Each group of functions begins with a block comment labelled the "DTrace 66 * [Group] Functions", allowing one to find each block by searching forward 67 * on capital-f functions. 68 */ 69 #include <sys/errno.h> 70 #ifndef illumos 71 #include <sys/time.h> 72 #endif 73 #include <sys/stat.h> 74 #include <sys/modctl.h> 75 #include <sys/conf.h> 76 #include <sys/systm.h> 77 #ifdef illumos 78 #include <sys/ddi.h> 79 #include <sys/sunddi.h> 80 #endif 81 #include <sys/cpuvar.h> 82 #include <sys/kmem.h> 83 #ifdef illumos 84 #include <sys/strsubr.h> 85 #endif 86 #include <sys/sysmacros.h> 87 #include <sys/dtrace_impl.h> 88 #include <sys/atomic.h> 89 #include <sys/cmn_err.h> 90 #ifdef illumos 91 #include <sys/mutex_impl.h> 92 #include <sys/rwlock_impl.h> 93 #endif 94 #include <sys/ctf_api.h> 95 #ifdef illumos 96 #include <sys/panic.h> 97 #include <sys/priv_impl.h> 98 #endif 99 #include <sys/policy.h> 100 #ifdef illumos 101 #include <sys/cred_impl.h> 102 #include <sys/procfs_isa.h> 103 #endif 104 #include <sys/taskq.h> 105 #ifdef illumos 106 #include <sys/mkdev.h> 107 #include <sys/kdi.h> 108 #endif 109 #include <sys/zone.h> 110 #include <sys/socket.h> 111 #include <netinet/in.h> 112 #include "strtolctype.h" 113 114 /* FreeBSD includes: */ 115 #ifndef illumos 116 #include <sys/callout.h> 117 #include <sys/ctype.h> 118 #include <sys/eventhandler.h> 119 #include <sys/limits.h> 120 #include <sys/kdb.h> 121 #include <sys/kernel.h> 122 #include <sys/malloc.h> 123 #include <sys/lock.h> 124 #include <sys/mutex.h> 125 #include <sys/ptrace.h> 126 #include <sys/rwlock.h> 127 #include <sys/sx.h> 128 #include <sys/sysctl.h> 129 130 #include <sys/dtrace_bsd.h> 131 132 #include <netinet/in.h> 133 134 #include "dtrace_cddl.h" 135 #include "dtrace_debug.c" 136 #endif 137 138 /* 139 * DTrace Tunable Variables 140 * 141 * The following variables may be tuned by adding a line to /etc/system that 142 * includes both the name of the DTrace module ("dtrace") and the name of the 143 * variable. For example: 144 * 145 * set dtrace:dtrace_destructive_disallow = 1 146 * 147 * In general, the only variables that one should be tuning this way are those 148 * that affect system-wide DTrace behavior, and for which the default behavior 149 * is undesirable. Most of these variables are tunable on a per-consumer 150 * basis using DTrace options, and need not be tuned on a system-wide basis. 151 * When tuning these variables, avoid pathological values; while some attempt 152 * is made to verify the integrity of these variables, they are not considered 153 * part of the supported interface to DTrace, and they are therefore not 154 * checked comprehensively. Further, these variables should not be tuned 155 * dynamically via "mdb -kw" or other means; they should only be tuned via 156 * /etc/system. 157 */ 158 int dtrace_destructive_disallow = 0; 159 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 160 size_t dtrace_difo_maxsize = (256 * 1024); 161 dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 162 size_t dtrace_statvar_maxsize = (16 * 1024); 163 size_t dtrace_actions_max = (16 * 1024); 164 size_t dtrace_retain_max = 1024; 165 dtrace_optval_t dtrace_helper_actions_max = 128; 166 dtrace_optval_t dtrace_helper_providers_max = 32; 167 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 168 size_t dtrace_strsize_default = 256; 169 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 170 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 171 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 172 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 173 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 174 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 175 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 176 dtrace_optval_t dtrace_nspec_default = 1; 177 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 178 dtrace_optval_t dtrace_stackframes_default = 20; 179 dtrace_optval_t dtrace_ustackframes_default = 20; 180 dtrace_optval_t dtrace_jstackframes_default = 50; 181 dtrace_optval_t dtrace_jstackstrsize_default = 512; 182 int dtrace_msgdsize_max = 128; 183 hrtime_t dtrace_chill_max = MSEC2NSEC(500); /* 500 ms */ 184 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 185 int dtrace_devdepth_max = 32; 186 int dtrace_err_verbose; 187 hrtime_t dtrace_deadman_interval = NANOSEC; 188 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 189 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 190 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 191 #ifndef illumos 192 int dtrace_memstr_max = 4096; 193 #endif 194 195 /* 196 * DTrace External Variables 197 * 198 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 199 * available to DTrace consumers via the backtick (`) syntax. One of these, 200 * dtrace_zero, is made deliberately so: it is provided as a source of 201 * well-known, zero-filled memory. While this variable is not documented, 202 * it is used by some translators as an implementation detail. 203 */ 204 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 205 206 /* 207 * DTrace Internal Variables 208 */ 209 #ifdef illumos 210 static dev_info_t *dtrace_devi; /* device info */ 211 #endif 212 #ifdef illumos 213 static vmem_t *dtrace_arena; /* probe ID arena */ 214 static vmem_t *dtrace_minor; /* minor number arena */ 215 #else 216 static taskq_t *dtrace_taskq; /* task queue */ 217 static struct unrhdr *dtrace_arena; /* Probe ID number. */ 218 #endif 219 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 220 static int dtrace_nprobes; /* number of probes */ 221 static dtrace_provider_t *dtrace_provider; /* provider list */ 222 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 223 static int dtrace_opens; /* number of opens */ 224 static int dtrace_helpers; /* number of helpers */ 225 static int dtrace_getf; /* number of unpriv getf()s */ 226 #ifdef illumos 227 static void *dtrace_softstate; /* softstate pointer */ 228 #endif 229 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 230 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 231 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 232 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 233 static int dtrace_toxranges; /* number of toxic ranges */ 234 static int dtrace_toxranges_max; /* size of toxic range array */ 235 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 236 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 237 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 238 static kthread_t *dtrace_panicked; /* panicking thread */ 239 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 240 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 241 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 242 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 243 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 244 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 245 static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 246 #ifndef illumos 247 static struct mtx dtrace_unr_mtx; 248 MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 249 int dtrace_in_probe; /* non-zero if executing a probe */ 250 #if defined(__i386__) || defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 251 uintptr_t dtrace_in_probe_addr; /* Address of invop when already in probe */ 252 #endif 253 static eventhandler_tag dtrace_kld_load_tag; 254 static eventhandler_tag dtrace_kld_unload_try_tag; 255 #endif 256 257 /* 258 * DTrace Locking 259 * DTrace is protected by three (relatively coarse-grained) locks: 260 * 261 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 262 * including enabling state, probes, ECBs, consumer state, helper state, 263 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 264 * probe context is lock-free -- synchronization is handled via the 265 * dtrace_sync() cross call mechanism. 266 * 267 * (2) dtrace_provider_lock is required when manipulating provider state, or 268 * when provider state must be held constant. 269 * 270 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 271 * when meta provider state must be held constant. 272 * 273 * The lock ordering between these three locks is dtrace_meta_lock before 274 * dtrace_provider_lock before dtrace_lock. (In particular, there are 275 * several places where dtrace_provider_lock is held by the framework as it 276 * calls into the providers -- which then call back into the framework, 277 * grabbing dtrace_lock.) 278 * 279 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 280 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 281 * role as a coarse-grained lock; it is acquired before both of these locks. 282 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 283 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 284 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 285 * acquired _between_ dtrace_provider_lock and dtrace_lock. 286 */ 287 static kmutex_t dtrace_lock; /* probe state lock */ 288 static kmutex_t dtrace_provider_lock; /* provider state lock */ 289 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 290 291 #ifndef illumos 292 /* XXX FreeBSD hacks. */ 293 #define cr_suid cr_svuid 294 #define cr_sgid cr_svgid 295 #define ipaddr_t in_addr_t 296 #define mod_modname pathname 297 #define vuprintf vprintf 298 #define ttoproc(_a) ((_a)->td_proc) 299 #define crgetzoneid(_a) 0 300 #define NCPU MAXCPU 301 #define SNOCD 0 302 #define CPU_ON_INTR(_a) 0 303 304 #define PRIV_EFFECTIVE (1 << 0) 305 #define PRIV_DTRACE_KERNEL (1 << 1) 306 #define PRIV_DTRACE_PROC (1 << 2) 307 #define PRIV_DTRACE_USER (1 << 3) 308 #define PRIV_PROC_OWNER (1 << 4) 309 #define PRIV_PROC_ZONE (1 << 5) 310 #define PRIV_ALL ~0 311 312 SYSCTL_DECL(_debug_dtrace); 313 SYSCTL_DECL(_kern_dtrace); 314 #endif 315 316 #ifdef illumos 317 #define curcpu CPU->cpu_id 318 #endif 319 320 321 /* 322 * DTrace Provider Variables 323 * 324 * These are the variables relating to DTrace as a provider (that is, the 325 * provider of the BEGIN, END, and ERROR probes). 326 */ 327 static dtrace_pattr_t dtrace_provider_attr = { 328 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 329 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 330 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 331 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 332 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 333 }; 334 335 static void 336 dtrace_nullop(void) 337 {} 338 339 static dtrace_pops_t dtrace_provider_ops = { 340 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 341 (void (*)(void *, modctl_t *))dtrace_nullop, 342 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 343 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 344 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 345 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 346 NULL, 347 NULL, 348 NULL, 349 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 350 }; 351 352 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 353 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 354 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 355 356 /* 357 * DTrace Helper Tracing Variables 358 * 359 * These variables should be set dynamically to enable helper tracing. The 360 * only variables that should be set are dtrace_helptrace_enable (which should 361 * be set to a non-zero value to allocate helper tracing buffers on the next 362 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a 363 * non-zero value to deallocate helper tracing buffers on the next close of 364 * /dev/dtrace). When (and only when) helper tracing is disabled, the 365 * buffer size may also be set via dtrace_helptrace_bufsize. 366 */ 367 int dtrace_helptrace_enable = 0; 368 int dtrace_helptrace_disable = 0; 369 int dtrace_helptrace_bufsize = 16 * 1024 * 1024; 370 uint32_t dtrace_helptrace_nlocals; 371 static dtrace_helptrace_t *dtrace_helptrace_buffer; 372 static uint32_t dtrace_helptrace_next = 0; 373 static int dtrace_helptrace_wrapped = 0; 374 375 /* 376 * DTrace Error Hashing 377 * 378 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 379 * table. This is very useful for checking coverage of tests that are 380 * expected to induce DIF or DOF processing errors, and may be useful for 381 * debugging problems in the DIF code generator or in DOF generation . The 382 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 383 */ 384 #ifdef DEBUG 385 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 386 static const char *dtrace_errlast; 387 static kthread_t *dtrace_errthread; 388 static kmutex_t dtrace_errlock; 389 #endif 390 391 /* 392 * DTrace Macros and Constants 393 * 394 * These are various macros that are useful in various spots in the 395 * implementation, along with a few random constants that have no meaning 396 * outside of the implementation. There is no real structure to this cpp 397 * mishmash -- but is there ever? 398 */ 399 #define DTRACE_HASHSTR(hash, probe) \ 400 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 401 402 #define DTRACE_HASHNEXT(hash, probe) \ 403 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 404 405 #define DTRACE_HASHPREV(hash, probe) \ 406 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 407 408 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 409 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 410 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 411 412 #define DTRACE_AGGHASHSIZE_SLEW 17 413 414 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 415 416 /* 417 * The key for a thread-local variable consists of the lower 61 bits of the 418 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 419 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 420 * equal to a variable identifier. This is necessary (but not sufficient) to 421 * assure that global associative arrays never collide with thread-local 422 * variables. To guarantee that they cannot collide, we must also define the 423 * order for keying dynamic variables. That order is: 424 * 425 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 426 * 427 * Because the variable-key and the tls-key are in orthogonal spaces, there is 428 * no way for a global variable key signature to match a thread-local key 429 * signature. 430 */ 431 #ifdef illumos 432 #define DTRACE_TLS_THRKEY(where) { \ 433 uint_t intr = 0; \ 434 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 435 for (; actv; actv >>= 1) \ 436 intr++; \ 437 ASSERT(intr < (1 << 3)); \ 438 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 439 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 440 } 441 #else 442 #define DTRACE_TLS_THRKEY(where) { \ 443 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 444 uint_t intr = 0; \ 445 uint_t actv = _c->cpu_intr_actv; \ 446 for (; actv; actv >>= 1) \ 447 intr++; \ 448 ASSERT(intr < (1 << 3)); \ 449 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 450 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 451 } 452 #endif 453 454 #define DT_BSWAP_8(x) ((x) & 0xff) 455 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 456 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 457 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 458 459 #define DT_MASK_LO 0x00000000FFFFFFFFULL 460 461 #define DTRACE_STORE(type, tomax, offset, what) \ 462 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 463 464 #ifndef __x86 465 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 466 if (addr & (size - 1)) { \ 467 *flags |= CPU_DTRACE_BADALIGN; \ 468 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 469 return (0); \ 470 } 471 #else 472 #define DTRACE_ALIGNCHECK(addr, size, flags) 473 #endif 474 475 /* 476 * Test whether a range of memory starting at testaddr of size testsz falls 477 * within the range of memory described by addr, sz. We take care to avoid 478 * problems with overflow and underflow of the unsigned quantities, and 479 * disallow all negative sizes. Ranges of size 0 are allowed. 480 */ 481 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 482 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 483 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 484 (testaddr) + (testsz) >= (testaddr)) 485 486 /* 487 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 488 * alloc_sz on the righthand side of the comparison in order to avoid overflow 489 * or underflow in the comparison with it. This is simpler than the INRANGE 490 * check above, because we know that the dtms_scratch_ptr is valid in the 491 * range. Allocations of size zero are allowed. 492 */ 493 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 494 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 495 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 496 497 #define DTRACE_LOADFUNC(bits) \ 498 /*CSTYLED*/ \ 499 uint##bits##_t \ 500 dtrace_load##bits(uintptr_t addr) \ 501 { \ 502 size_t size = bits / NBBY; \ 503 /*CSTYLED*/ \ 504 uint##bits##_t rval; \ 505 int i; \ 506 volatile uint16_t *flags = (volatile uint16_t *) \ 507 &cpu_core[curcpu].cpuc_dtrace_flags; \ 508 \ 509 DTRACE_ALIGNCHECK(addr, size, flags); \ 510 \ 511 for (i = 0; i < dtrace_toxranges; i++) { \ 512 if (addr >= dtrace_toxrange[i].dtt_limit) \ 513 continue; \ 514 \ 515 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 516 continue; \ 517 \ 518 /* \ 519 * This address falls within a toxic region; return 0. \ 520 */ \ 521 *flags |= CPU_DTRACE_BADADDR; \ 522 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 523 return (0); \ 524 } \ 525 \ 526 *flags |= CPU_DTRACE_NOFAULT; \ 527 /*CSTYLED*/ \ 528 rval = *((volatile uint##bits##_t *)addr); \ 529 *flags &= ~CPU_DTRACE_NOFAULT; \ 530 \ 531 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 532 } 533 534 #ifdef _LP64 535 #define dtrace_loadptr dtrace_load64 536 #else 537 #define dtrace_loadptr dtrace_load32 538 #endif 539 540 #define DTRACE_DYNHASH_FREE 0 541 #define DTRACE_DYNHASH_SINK 1 542 #define DTRACE_DYNHASH_VALID 2 543 544 #define DTRACE_MATCH_NEXT 0 545 #define DTRACE_MATCH_DONE 1 546 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 547 #define DTRACE_STATE_ALIGN 64 548 549 #define DTRACE_FLAGS2FLT(flags) \ 550 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 551 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 552 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 553 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 554 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 555 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 556 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 557 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 558 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 559 DTRACEFLT_UNKNOWN) 560 561 #define DTRACEACT_ISSTRING(act) \ 562 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 563 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 564 565 /* Function prototype definitions: */ 566 static size_t dtrace_strlen(const char *, size_t); 567 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 568 static void dtrace_enabling_provide(dtrace_provider_t *); 569 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 570 static void dtrace_enabling_matchall(void); 571 static void dtrace_enabling_reap(void); 572 static dtrace_state_t *dtrace_anon_grab(void); 573 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 574 dtrace_state_t *, uint64_t, uint64_t); 575 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 576 static void dtrace_buffer_drop(dtrace_buffer_t *); 577 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 578 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 579 dtrace_state_t *, dtrace_mstate_t *); 580 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 581 dtrace_optval_t); 582 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 583 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 584 uint16_t dtrace_load16(uintptr_t); 585 uint32_t dtrace_load32(uintptr_t); 586 uint64_t dtrace_load64(uintptr_t); 587 uint8_t dtrace_load8(uintptr_t); 588 void dtrace_dynvar_clean(dtrace_dstate_t *); 589 dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 590 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 591 uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 592 static int dtrace_priv_proc(dtrace_state_t *); 593 static void dtrace_getf_barrier(void); 594 595 /* 596 * DTrace Probe Context Functions 597 * 598 * These functions are called from probe context. Because probe context is 599 * any context in which C may be called, arbitrarily locks may be held, 600 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 601 * As a result, functions called from probe context may only call other DTrace 602 * support functions -- they may not interact at all with the system at large. 603 * (Note that the ASSERT macro is made probe-context safe by redefining it in 604 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 605 * loads are to be performed from probe context, they _must_ be in terms of 606 * the safe dtrace_load*() variants. 607 * 608 * Some functions in this block are not actually called from probe context; 609 * for these functions, there will be a comment above the function reading 610 * "Note: not called from probe context." 611 */ 612 void 613 dtrace_panic(const char *format, ...) 614 { 615 va_list alist; 616 617 va_start(alist, format); 618 #ifdef __FreeBSD__ 619 vpanic(format, alist); 620 #else 621 dtrace_vpanic(format, alist); 622 #endif 623 va_end(alist); 624 } 625 626 int 627 dtrace_assfail(const char *a, const char *f, int l) 628 { 629 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 630 631 /* 632 * We just need something here that even the most clever compiler 633 * cannot optimize away. 634 */ 635 return (a[(uintptr_t)f]); 636 } 637 638 /* 639 * Atomically increment a specified error counter from probe context. 640 */ 641 static void 642 dtrace_error(uint32_t *counter) 643 { 644 /* 645 * Most counters stored to in probe context are per-CPU counters. 646 * However, there are some error conditions that are sufficiently 647 * arcane that they don't merit per-CPU storage. If these counters 648 * are incremented concurrently on different CPUs, scalability will be 649 * adversely affected -- but we don't expect them to be white-hot in a 650 * correctly constructed enabling... 651 */ 652 uint32_t oval, nval; 653 654 do { 655 oval = *counter; 656 657 if ((nval = oval + 1) == 0) { 658 /* 659 * If the counter would wrap, set it to 1 -- assuring 660 * that the counter is never zero when we have seen 661 * errors. (The counter must be 32-bits because we 662 * aren't guaranteed a 64-bit compare&swap operation.) 663 * To save this code both the infamy of being fingered 664 * by a priggish news story and the indignity of being 665 * the target of a neo-puritan witch trial, we're 666 * carefully avoiding any colorful description of the 667 * likelihood of this condition -- but suffice it to 668 * say that it is only slightly more likely than the 669 * overflow of predicate cache IDs, as discussed in 670 * dtrace_predicate_create(). 671 */ 672 nval = 1; 673 } 674 } while (dtrace_cas32(counter, oval, nval) != oval); 675 } 676 677 /* 678 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 679 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 680 */ 681 DTRACE_LOADFUNC(8) 682 DTRACE_LOADFUNC(16) 683 DTRACE_LOADFUNC(32) 684 DTRACE_LOADFUNC(64) 685 686 static int 687 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 688 { 689 if (dest < mstate->dtms_scratch_base) 690 return (0); 691 692 if (dest + size < dest) 693 return (0); 694 695 if (dest + size > mstate->dtms_scratch_ptr) 696 return (0); 697 698 return (1); 699 } 700 701 static int 702 dtrace_canstore_statvar(uint64_t addr, size_t sz, 703 dtrace_statvar_t **svars, int nsvars) 704 { 705 int i; 706 size_t maxglobalsize, maxlocalsize; 707 708 if (nsvars == 0) 709 return (0); 710 711 maxglobalsize = dtrace_statvar_maxsize; 712 maxlocalsize = (maxglobalsize + sizeof (uint64_t)) * NCPU; 713 714 for (i = 0; i < nsvars; i++) { 715 dtrace_statvar_t *svar = svars[i]; 716 uint8_t scope; 717 size_t size; 718 719 if (svar == NULL || (size = svar->dtsv_size) == 0) 720 continue; 721 722 scope = svar->dtsv_var.dtdv_scope; 723 724 /* 725 * We verify that our size is valid in the spirit of providing 726 * defense in depth: we want to prevent attackers from using 727 * DTrace to escalate an orthogonal kernel heap corruption bug 728 * into the ability to store to arbitrary locations in memory. 729 */ 730 VERIFY((scope == DIFV_SCOPE_GLOBAL && size < maxglobalsize) || 731 (scope == DIFV_SCOPE_LOCAL && size < maxlocalsize)); 732 733 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 734 return (1); 735 } 736 737 return (0); 738 } 739 740 /* 741 * Check to see if the address is within a memory region to which a store may 742 * be issued. This includes the DTrace scratch areas, and any DTrace variable 743 * region. The caller of dtrace_canstore() is responsible for performing any 744 * alignment checks that are needed before stores are actually executed. 745 */ 746 static int 747 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 748 dtrace_vstate_t *vstate) 749 { 750 /* 751 * First, check to see if the address is in scratch space... 752 */ 753 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 754 mstate->dtms_scratch_size)) 755 return (1); 756 757 /* 758 * Now check to see if it's a dynamic variable. This check will pick 759 * up both thread-local variables and any global dynamically-allocated 760 * variables. 761 */ 762 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 763 vstate->dtvs_dynvars.dtds_size)) { 764 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 765 uintptr_t base = (uintptr_t)dstate->dtds_base + 766 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 767 uintptr_t chunkoffs; 768 769 /* 770 * Before we assume that we can store here, we need to make 771 * sure that it isn't in our metadata -- storing to our 772 * dynamic variable metadata would corrupt our state. For 773 * the range to not include any dynamic variable metadata, 774 * it must: 775 * 776 * (1) Start above the hash table that is at the base of 777 * the dynamic variable space 778 * 779 * (2) Have a starting chunk offset that is beyond the 780 * dtrace_dynvar_t that is at the base of every chunk 781 * 782 * (3) Not span a chunk boundary 783 * 784 */ 785 if (addr < base) 786 return (0); 787 788 chunkoffs = (addr - base) % dstate->dtds_chunksize; 789 790 if (chunkoffs < sizeof (dtrace_dynvar_t)) 791 return (0); 792 793 if (chunkoffs + sz > dstate->dtds_chunksize) 794 return (0); 795 796 return (1); 797 } 798 799 /* 800 * Finally, check the static local and global variables. These checks 801 * take the longest, so we perform them last. 802 */ 803 if (dtrace_canstore_statvar(addr, sz, 804 vstate->dtvs_locals, vstate->dtvs_nlocals)) 805 return (1); 806 807 if (dtrace_canstore_statvar(addr, sz, 808 vstate->dtvs_globals, vstate->dtvs_nglobals)) 809 return (1); 810 811 return (0); 812 } 813 814 815 /* 816 * Convenience routine to check to see if the address is within a memory 817 * region in which a load may be issued given the user's privilege level; 818 * if not, it sets the appropriate error flags and loads 'addr' into the 819 * illegal value slot. 820 * 821 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 822 * appropriate memory access protection. 823 */ 824 static int 825 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 826 dtrace_vstate_t *vstate) 827 { 828 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 829 file_t *fp; 830 831 /* 832 * If we hold the privilege to read from kernel memory, then 833 * everything is readable. 834 */ 835 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 836 return (1); 837 838 /* 839 * You can obviously read that which you can store. 840 */ 841 if (dtrace_canstore(addr, sz, mstate, vstate)) 842 return (1); 843 844 /* 845 * We're allowed to read from our own string table. 846 */ 847 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 848 mstate->dtms_difo->dtdo_strlen)) 849 return (1); 850 851 if (vstate->dtvs_state != NULL && 852 dtrace_priv_proc(vstate->dtvs_state)) { 853 proc_t *p; 854 855 /* 856 * When we have privileges to the current process, there are 857 * several context-related kernel structures that are safe to 858 * read, even absent the privilege to read from kernel memory. 859 * These reads are safe because these structures contain only 860 * state that (1) we're permitted to read, (2) is harmless or 861 * (3) contains pointers to additional kernel state that we're 862 * not permitted to read (and as such, do not present an 863 * opportunity for privilege escalation). Finally (and 864 * critically), because of the nature of their relation with 865 * the current thread context, the memory associated with these 866 * structures cannot change over the duration of probe context, 867 * and it is therefore impossible for this memory to be 868 * deallocated and reallocated as something else while it's 869 * being operated upon. 870 */ 871 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) 872 return (1); 873 874 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 875 sz, curthread->t_procp, sizeof (proc_t))) { 876 return (1); 877 } 878 879 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 880 curthread->t_cred, sizeof (cred_t))) { 881 return (1); 882 } 883 884 #ifdef illumos 885 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 886 &(p->p_pidp->pid_id), sizeof (pid_t))) { 887 return (1); 888 } 889 890 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 891 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 892 return (1); 893 } 894 #endif 895 } 896 897 if ((fp = mstate->dtms_getf) != NULL) { 898 uintptr_t psz = sizeof (void *); 899 vnode_t *vp; 900 vnodeops_t *op; 901 902 /* 903 * When getf() returns a file_t, the enabling is implicitly 904 * granted the (transient) right to read the returned file_t 905 * as well as the v_path and v_op->vnop_name of the underlying 906 * vnode. These accesses are allowed after a successful 907 * getf() because the members that they refer to cannot change 908 * once set -- and the barrier logic in the kernel's closef() 909 * path assures that the file_t and its referenced vode_t 910 * cannot themselves be stale (that is, it impossible for 911 * either dtms_getf itself or its f_vnode member to reference 912 * freed memory). 913 */ 914 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) 915 return (1); 916 917 if ((vp = fp->f_vnode) != NULL) { 918 #ifdef illumos 919 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) 920 return (1); 921 if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz, 922 vp->v_path, strlen(vp->v_path) + 1)) { 923 return (1); 924 } 925 #endif 926 927 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) 928 return (1); 929 930 #ifdef illumos 931 if ((op = vp->v_op) != NULL && 932 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 933 return (1); 934 } 935 936 if (op != NULL && op->vnop_name != NULL && 937 DTRACE_INRANGE(addr, sz, op->vnop_name, 938 strlen(op->vnop_name) + 1)) { 939 return (1); 940 } 941 #endif 942 } 943 } 944 945 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 946 *illval = addr; 947 return (0); 948 } 949 950 /* 951 * Convenience routine to check to see if a given string is within a memory 952 * region in which a load may be issued given the user's privilege level; 953 * this exists so that we don't need to issue unnecessary dtrace_strlen() 954 * calls in the event that the user has all privileges. 955 */ 956 static int 957 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 958 dtrace_vstate_t *vstate) 959 { 960 size_t strsz; 961 962 /* 963 * If we hold the privilege to read from kernel memory, then 964 * everything is readable. 965 */ 966 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 967 return (1); 968 969 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 970 if (dtrace_canload(addr, strsz, mstate, vstate)) 971 return (1); 972 973 return (0); 974 } 975 976 /* 977 * Convenience routine to check to see if a given variable is within a memory 978 * region in which a load may be issued given the user's privilege level. 979 */ 980 static int 981 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 982 dtrace_vstate_t *vstate) 983 { 984 size_t sz; 985 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 986 987 /* 988 * If we hold the privilege to read from kernel memory, then 989 * everything is readable. 990 */ 991 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 992 return (1); 993 994 if (type->dtdt_kind == DIF_TYPE_STRING) 995 sz = dtrace_strlen(src, 996 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 997 else 998 sz = type->dtdt_size; 999 1000 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 1001 } 1002 1003 /* 1004 * Convert a string to a signed integer using safe loads. 1005 * 1006 * NOTE: This function uses various macros from strtolctype.h to manipulate 1007 * digit values, etc -- these have all been checked to ensure they make 1008 * no additional function calls. 1009 */ 1010 static int64_t 1011 dtrace_strtoll(char *input, int base, size_t limit) 1012 { 1013 uintptr_t pos = (uintptr_t)input; 1014 int64_t val = 0; 1015 int x; 1016 boolean_t neg = B_FALSE; 1017 char c, cc, ccc; 1018 uintptr_t end = pos + limit; 1019 1020 /* 1021 * Consume any whitespace preceding digits. 1022 */ 1023 while ((c = dtrace_load8(pos)) == ' ' || c == '\t') 1024 pos++; 1025 1026 /* 1027 * Handle an explicit sign if one is present. 1028 */ 1029 if (c == '-' || c == '+') { 1030 if (c == '-') 1031 neg = B_TRUE; 1032 c = dtrace_load8(++pos); 1033 } 1034 1035 /* 1036 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it 1037 * if present. 1038 */ 1039 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' || 1040 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) { 1041 pos += 2; 1042 c = ccc; 1043 } 1044 1045 /* 1046 * Read in contiguous digits until the first non-digit character. 1047 */ 1048 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base; 1049 c = dtrace_load8(++pos)) 1050 val = val * base + x; 1051 1052 return (neg ? -val : val); 1053 } 1054 1055 /* 1056 * Compare two strings using safe loads. 1057 */ 1058 static int 1059 dtrace_strncmp(char *s1, char *s2, size_t limit) 1060 { 1061 uint8_t c1, c2; 1062 volatile uint16_t *flags; 1063 1064 if (s1 == s2 || limit == 0) 1065 return (0); 1066 1067 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1068 1069 do { 1070 if (s1 == NULL) { 1071 c1 = '\0'; 1072 } else { 1073 c1 = dtrace_load8((uintptr_t)s1++); 1074 } 1075 1076 if (s2 == NULL) { 1077 c2 = '\0'; 1078 } else { 1079 c2 = dtrace_load8((uintptr_t)s2++); 1080 } 1081 1082 if (c1 != c2) 1083 return (c1 - c2); 1084 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 1085 1086 return (0); 1087 } 1088 1089 /* 1090 * Compute strlen(s) for a string using safe memory accesses. The additional 1091 * len parameter is used to specify a maximum length to ensure completion. 1092 */ 1093 static size_t 1094 dtrace_strlen(const char *s, size_t lim) 1095 { 1096 uint_t len; 1097 1098 for (len = 0; len != lim; len++) { 1099 if (dtrace_load8((uintptr_t)s++) == '\0') 1100 break; 1101 } 1102 1103 return (len); 1104 } 1105 1106 /* 1107 * Check if an address falls within a toxic region. 1108 */ 1109 static int 1110 dtrace_istoxic(uintptr_t kaddr, size_t size) 1111 { 1112 uintptr_t taddr, tsize; 1113 int i; 1114 1115 for (i = 0; i < dtrace_toxranges; i++) { 1116 taddr = dtrace_toxrange[i].dtt_base; 1117 tsize = dtrace_toxrange[i].dtt_limit - taddr; 1118 1119 if (kaddr - taddr < tsize) { 1120 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1121 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 1122 return (1); 1123 } 1124 1125 if (taddr - kaddr < size) { 1126 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1127 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 1128 return (1); 1129 } 1130 } 1131 1132 return (0); 1133 } 1134 1135 /* 1136 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 1137 * memory specified by the DIF program. The dst is assumed to be safe memory 1138 * that we can store to directly because it is managed by DTrace. As with 1139 * standard bcopy, overlapping copies are handled properly. 1140 */ 1141 static void 1142 dtrace_bcopy(const void *src, void *dst, size_t len) 1143 { 1144 if (len != 0) { 1145 uint8_t *s1 = dst; 1146 const uint8_t *s2 = src; 1147 1148 if (s1 <= s2) { 1149 do { 1150 *s1++ = dtrace_load8((uintptr_t)s2++); 1151 } while (--len != 0); 1152 } else { 1153 s2 += len; 1154 s1 += len; 1155 1156 do { 1157 *--s1 = dtrace_load8((uintptr_t)--s2); 1158 } while (--len != 0); 1159 } 1160 } 1161 } 1162 1163 /* 1164 * Copy src to dst using safe memory accesses, up to either the specified 1165 * length, or the point that a nul byte is encountered. The src is assumed to 1166 * be unsafe memory specified by the DIF program. The dst is assumed to be 1167 * safe memory that we can store to directly because it is managed by DTrace. 1168 * Unlike dtrace_bcopy(), overlapping regions are not handled. 1169 */ 1170 static void 1171 dtrace_strcpy(const void *src, void *dst, size_t len) 1172 { 1173 if (len != 0) { 1174 uint8_t *s1 = dst, c; 1175 const uint8_t *s2 = src; 1176 1177 do { 1178 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1179 } while (--len != 0 && c != '\0'); 1180 } 1181 } 1182 1183 /* 1184 * Copy src to dst, deriving the size and type from the specified (BYREF) 1185 * variable type. The src is assumed to be unsafe memory specified by the DIF 1186 * program. The dst is assumed to be DTrace variable memory that is of the 1187 * specified type; we assume that we can store to directly. 1188 */ 1189 static void 1190 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 1191 { 1192 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1193 1194 if (type->dtdt_kind == DIF_TYPE_STRING) { 1195 dtrace_strcpy(src, dst, type->dtdt_size); 1196 } else { 1197 dtrace_bcopy(src, dst, type->dtdt_size); 1198 } 1199 } 1200 1201 /* 1202 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1203 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1204 * safe memory that we can access directly because it is managed by DTrace. 1205 */ 1206 static int 1207 dtrace_bcmp(const void *s1, const void *s2, size_t len) 1208 { 1209 volatile uint16_t *flags; 1210 1211 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1212 1213 if (s1 == s2) 1214 return (0); 1215 1216 if (s1 == NULL || s2 == NULL) 1217 return (1); 1218 1219 if (s1 != s2 && len != 0) { 1220 const uint8_t *ps1 = s1; 1221 const uint8_t *ps2 = s2; 1222 1223 do { 1224 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1225 return (1); 1226 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1227 } 1228 return (0); 1229 } 1230 1231 /* 1232 * Zero the specified region using a simple byte-by-byte loop. Note that this 1233 * is for safe DTrace-managed memory only. 1234 */ 1235 static void 1236 dtrace_bzero(void *dst, size_t len) 1237 { 1238 uchar_t *cp; 1239 1240 for (cp = dst; len != 0; len--) 1241 *cp++ = 0; 1242 } 1243 1244 static void 1245 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1246 { 1247 uint64_t result[2]; 1248 1249 result[0] = addend1[0] + addend2[0]; 1250 result[1] = addend1[1] + addend2[1] + 1251 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1252 1253 sum[0] = result[0]; 1254 sum[1] = result[1]; 1255 } 1256 1257 /* 1258 * Shift the 128-bit value in a by b. If b is positive, shift left. 1259 * If b is negative, shift right. 1260 */ 1261 static void 1262 dtrace_shift_128(uint64_t *a, int b) 1263 { 1264 uint64_t mask; 1265 1266 if (b == 0) 1267 return; 1268 1269 if (b < 0) { 1270 b = -b; 1271 if (b >= 64) { 1272 a[0] = a[1] >> (b - 64); 1273 a[1] = 0; 1274 } else { 1275 a[0] >>= b; 1276 mask = 1LL << (64 - b); 1277 mask -= 1; 1278 a[0] |= ((a[1] & mask) << (64 - b)); 1279 a[1] >>= b; 1280 } 1281 } else { 1282 if (b >= 64) { 1283 a[1] = a[0] << (b - 64); 1284 a[0] = 0; 1285 } else { 1286 a[1] <<= b; 1287 mask = a[0] >> (64 - b); 1288 a[1] |= mask; 1289 a[0] <<= b; 1290 } 1291 } 1292 } 1293 1294 /* 1295 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1296 * use native multiplication on those, and then re-combine into the 1297 * resulting 128-bit value. 1298 * 1299 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1300 * hi1 * hi2 << 64 + 1301 * hi1 * lo2 << 32 + 1302 * hi2 * lo1 << 32 + 1303 * lo1 * lo2 1304 */ 1305 static void 1306 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1307 { 1308 uint64_t hi1, hi2, lo1, lo2; 1309 uint64_t tmp[2]; 1310 1311 hi1 = factor1 >> 32; 1312 hi2 = factor2 >> 32; 1313 1314 lo1 = factor1 & DT_MASK_LO; 1315 lo2 = factor2 & DT_MASK_LO; 1316 1317 product[0] = lo1 * lo2; 1318 product[1] = hi1 * hi2; 1319 1320 tmp[0] = hi1 * lo2; 1321 tmp[1] = 0; 1322 dtrace_shift_128(tmp, 32); 1323 dtrace_add_128(product, tmp, product); 1324 1325 tmp[0] = hi2 * lo1; 1326 tmp[1] = 0; 1327 dtrace_shift_128(tmp, 32); 1328 dtrace_add_128(product, tmp, product); 1329 } 1330 1331 /* 1332 * This privilege check should be used by actions and subroutines to 1333 * verify that the user credentials of the process that enabled the 1334 * invoking ECB match the target credentials 1335 */ 1336 static int 1337 dtrace_priv_proc_common_user(dtrace_state_t *state) 1338 { 1339 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1340 1341 /* 1342 * We should always have a non-NULL state cred here, since if cred 1343 * is null (anonymous tracing), we fast-path bypass this routine. 1344 */ 1345 ASSERT(s_cr != NULL); 1346 1347 if ((cr = CRED()) != NULL && 1348 s_cr->cr_uid == cr->cr_uid && 1349 s_cr->cr_uid == cr->cr_ruid && 1350 s_cr->cr_uid == cr->cr_suid && 1351 s_cr->cr_gid == cr->cr_gid && 1352 s_cr->cr_gid == cr->cr_rgid && 1353 s_cr->cr_gid == cr->cr_sgid) 1354 return (1); 1355 1356 return (0); 1357 } 1358 1359 /* 1360 * This privilege check should be used by actions and subroutines to 1361 * verify that the zone of the process that enabled the invoking ECB 1362 * matches the target credentials 1363 */ 1364 static int 1365 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1366 { 1367 #ifdef illumos 1368 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1369 1370 /* 1371 * We should always have a non-NULL state cred here, since if cred 1372 * is null (anonymous tracing), we fast-path bypass this routine. 1373 */ 1374 ASSERT(s_cr != NULL); 1375 1376 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1377 return (1); 1378 1379 return (0); 1380 #else 1381 return (1); 1382 #endif 1383 } 1384 1385 /* 1386 * This privilege check should be used by actions and subroutines to 1387 * verify that the process has not setuid or changed credentials. 1388 */ 1389 static int 1390 dtrace_priv_proc_common_nocd(void) 1391 { 1392 proc_t *proc; 1393 1394 if ((proc = ttoproc(curthread)) != NULL && 1395 !(proc->p_flag & SNOCD)) 1396 return (1); 1397 1398 return (0); 1399 } 1400 1401 static int 1402 dtrace_priv_proc_destructive(dtrace_state_t *state) 1403 { 1404 int action = state->dts_cred.dcr_action; 1405 1406 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1407 dtrace_priv_proc_common_zone(state) == 0) 1408 goto bad; 1409 1410 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1411 dtrace_priv_proc_common_user(state) == 0) 1412 goto bad; 1413 1414 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1415 dtrace_priv_proc_common_nocd() == 0) 1416 goto bad; 1417 1418 return (1); 1419 1420 bad: 1421 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1422 1423 return (0); 1424 } 1425 1426 static int 1427 dtrace_priv_proc_control(dtrace_state_t *state) 1428 { 1429 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1430 return (1); 1431 1432 if (dtrace_priv_proc_common_zone(state) && 1433 dtrace_priv_proc_common_user(state) && 1434 dtrace_priv_proc_common_nocd()) 1435 return (1); 1436 1437 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1438 1439 return (0); 1440 } 1441 1442 static int 1443 dtrace_priv_proc(dtrace_state_t *state) 1444 { 1445 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1446 return (1); 1447 1448 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1449 1450 return (0); 1451 } 1452 1453 static int 1454 dtrace_priv_kernel(dtrace_state_t *state) 1455 { 1456 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1457 return (1); 1458 1459 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1460 1461 return (0); 1462 } 1463 1464 static int 1465 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1466 { 1467 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1468 return (1); 1469 1470 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1471 1472 return (0); 1473 } 1474 1475 /* 1476 * Determine if the dte_cond of the specified ECB allows for processing of 1477 * the current probe to continue. Note that this routine may allow continued 1478 * processing, but with access(es) stripped from the mstate's dtms_access 1479 * field. 1480 */ 1481 static int 1482 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1483 dtrace_ecb_t *ecb) 1484 { 1485 dtrace_probe_t *probe = ecb->dte_probe; 1486 dtrace_provider_t *prov = probe->dtpr_provider; 1487 dtrace_pops_t *pops = &prov->dtpv_pops; 1488 int mode = DTRACE_MODE_NOPRIV_DROP; 1489 1490 ASSERT(ecb->dte_cond); 1491 1492 #ifdef illumos 1493 if (pops->dtps_mode != NULL) { 1494 mode = pops->dtps_mode(prov->dtpv_arg, 1495 probe->dtpr_id, probe->dtpr_arg); 1496 1497 ASSERT((mode & DTRACE_MODE_USER) || 1498 (mode & DTRACE_MODE_KERNEL)); 1499 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1500 (mode & DTRACE_MODE_NOPRIV_DROP)); 1501 } 1502 1503 /* 1504 * If the dte_cond bits indicate that this consumer is only allowed to 1505 * see user-mode firings of this probe, call the provider's dtps_mode() 1506 * entry point to check that the probe was fired while in a user 1507 * context. If that's not the case, use the policy specified by the 1508 * provider to determine if we drop the probe or merely restrict 1509 * operation. 1510 */ 1511 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1512 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1513 1514 if (!(mode & DTRACE_MODE_USER)) { 1515 if (mode & DTRACE_MODE_NOPRIV_DROP) 1516 return (0); 1517 1518 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1519 } 1520 } 1521 #endif 1522 1523 /* 1524 * This is more subtle than it looks. We have to be absolutely certain 1525 * that CRED() isn't going to change out from under us so it's only 1526 * legit to examine that structure if we're in constrained situations. 1527 * Currently, the only times we'll this check is if a non-super-user 1528 * has enabled the profile or syscall providers -- providers that 1529 * allow visibility of all processes. For the profile case, the check 1530 * above will ensure that we're examining a user context. 1531 */ 1532 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1533 cred_t *cr; 1534 cred_t *s_cr = state->dts_cred.dcr_cred; 1535 proc_t *proc; 1536 1537 ASSERT(s_cr != NULL); 1538 1539 if ((cr = CRED()) == NULL || 1540 s_cr->cr_uid != cr->cr_uid || 1541 s_cr->cr_uid != cr->cr_ruid || 1542 s_cr->cr_uid != cr->cr_suid || 1543 s_cr->cr_gid != cr->cr_gid || 1544 s_cr->cr_gid != cr->cr_rgid || 1545 s_cr->cr_gid != cr->cr_sgid || 1546 (proc = ttoproc(curthread)) == NULL || 1547 (proc->p_flag & SNOCD)) { 1548 if (mode & DTRACE_MODE_NOPRIV_DROP) 1549 return (0); 1550 1551 #ifdef illumos 1552 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1553 #endif 1554 } 1555 } 1556 1557 #ifdef illumos 1558 /* 1559 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1560 * in our zone, check to see if our mode policy is to restrict rather 1561 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1562 * and DTRACE_ACCESS_ARGS 1563 */ 1564 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1565 cred_t *cr; 1566 cred_t *s_cr = state->dts_cred.dcr_cred; 1567 1568 ASSERT(s_cr != NULL); 1569 1570 if ((cr = CRED()) == NULL || 1571 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1572 if (mode & DTRACE_MODE_NOPRIV_DROP) 1573 return (0); 1574 1575 mstate->dtms_access &= 1576 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1577 } 1578 } 1579 #endif 1580 1581 return (1); 1582 } 1583 1584 /* 1585 * Note: not called from probe context. This function is called 1586 * asynchronously (and at a regular interval) from outside of probe context to 1587 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1588 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1589 */ 1590 void 1591 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1592 { 1593 dtrace_dynvar_t *dirty; 1594 dtrace_dstate_percpu_t *dcpu; 1595 dtrace_dynvar_t **rinsep; 1596 int i, j, work = 0; 1597 1598 for (i = 0; i < NCPU; i++) { 1599 dcpu = &dstate->dtds_percpu[i]; 1600 rinsep = &dcpu->dtdsc_rinsing; 1601 1602 /* 1603 * If the dirty list is NULL, there is no dirty work to do. 1604 */ 1605 if (dcpu->dtdsc_dirty == NULL) 1606 continue; 1607 1608 if (dcpu->dtdsc_rinsing != NULL) { 1609 /* 1610 * If the rinsing list is non-NULL, then it is because 1611 * this CPU was selected to accept another CPU's 1612 * dirty list -- and since that time, dirty buffers 1613 * have accumulated. This is a highly unlikely 1614 * condition, but we choose to ignore the dirty 1615 * buffers -- they'll be picked up a future cleanse. 1616 */ 1617 continue; 1618 } 1619 1620 if (dcpu->dtdsc_clean != NULL) { 1621 /* 1622 * If the clean list is non-NULL, then we're in a 1623 * situation where a CPU has done deallocations (we 1624 * have a non-NULL dirty list) but no allocations (we 1625 * also have a non-NULL clean list). We can't simply 1626 * move the dirty list into the clean list on this 1627 * CPU, yet we also don't want to allow this condition 1628 * to persist, lest a short clean list prevent a 1629 * massive dirty list from being cleaned (which in 1630 * turn could lead to otherwise avoidable dynamic 1631 * drops). To deal with this, we look for some CPU 1632 * with a NULL clean list, NULL dirty list, and NULL 1633 * rinsing list -- and then we borrow this CPU to 1634 * rinse our dirty list. 1635 */ 1636 for (j = 0; j < NCPU; j++) { 1637 dtrace_dstate_percpu_t *rinser; 1638 1639 rinser = &dstate->dtds_percpu[j]; 1640 1641 if (rinser->dtdsc_rinsing != NULL) 1642 continue; 1643 1644 if (rinser->dtdsc_dirty != NULL) 1645 continue; 1646 1647 if (rinser->dtdsc_clean != NULL) 1648 continue; 1649 1650 rinsep = &rinser->dtdsc_rinsing; 1651 break; 1652 } 1653 1654 if (j == NCPU) { 1655 /* 1656 * We were unable to find another CPU that 1657 * could accept this dirty list -- we are 1658 * therefore unable to clean it now. 1659 */ 1660 dtrace_dynvar_failclean++; 1661 continue; 1662 } 1663 } 1664 1665 work = 1; 1666 1667 /* 1668 * Atomically move the dirty list aside. 1669 */ 1670 do { 1671 dirty = dcpu->dtdsc_dirty; 1672 1673 /* 1674 * Before we zap the dirty list, set the rinsing list. 1675 * (This allows for a potential assertion in 1676 * dtrace_dynvar(): if a free dynamic variable appears 1677 * on a hash chain, either the dirty list or the 1678 * rinsing list for some CPU must be non-NULL.) 1679 */ 1680 *rinsep = dirty; 1681 dtrace_membar_producer(); 1682 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1683 dirty, NULL) != dirty); 1684 } 1685 1686 if (!work) { 1687 /* 1688 * We have no work to do; we can simply return. 1689 */ 1690 return; 1691 } 1692 1693 dtrace_sync(); 1694 1695 for (i = 0; i < NCPU; i++) { 1696 dcpu = &dstate->dtds_percpu[i]; 1697 1698 if (dcpu->dtdsc_rinsing == NULL) 1699 continue; 1700 1701 /* 1702 * We are now guaranteed that no hash chain contains a pointer 1703 * into this dirty list; we can make it clean. 1704 */ 1705 ASSERT(dcpu->dtdsc_clean == NULL); 1706 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1707 dcpu->dtdsc_rinsing = NULL; 1708 } 1709 1710 /* 1711 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1712 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1713 * This prevents a race whereby a CPU incorrectly decides that 1714 * the state should be something other than DTRACE_DSTATE_CLEAN 1715 * after dtrace_dynvar_clean() has completed. 1716 */ 1717 dtrace_sync(); 1718 1719 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1720 } 1721 1722 /* 1723 * Depending on the value of the op parameter, this function looks-up, 1724 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1725 * allocation is requested, this function will return a pointer to a 1726 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1727 * variable can be allocated. If NULL is returned, the appropriate counter 1728 * will be incremented. 1729 */ 1730 dtrace_dynvar_t * 1731 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1732 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1733 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1734 { 1735 uint64_t hashval = DTRACE_DYNHASH_VALID; 1736 dtrace_dynhash_t *hash = dstate->dtds_hash; 1737 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1738 processorid_t me = curcpu, cpu = me; 1739 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1740 size_t bucket, ksize; 1741 size_t chunksize = dstate->dtds_chunksize; 1742 uintptr_t kdata, lock, nstate; 1743 uint_t i; 1744 1745 ASSERT(nkeys != 0); 1746 1747 /* 1748 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1749 * algorithm. For the by-value portions, we perform the algorithm in 1750 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1751 * bit, and seems to have only a minute effect on distribution. For 1752 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1753 * over each referenced byte. It's painful to do this, but it's much 1754 * better than pathological hash distribution. The efficacy of the 1755 * hashing algorithm (and a comparison with other algorithms) may be 1756 * found by running the ::dtrace_dynstat MDB dcmd. 1757 */ 1758 for (i = 0; i < nkeys; i++) { 1759 if (key[i].dttk_size == 0) { 1760 uint64_t val = key[i].dttk_value; 1761 1762 hashval += (val >> 48) & 0xffff; 1763 hashval += (hashval << 10); 1764 hashval ^= (hashval >> 6); 1765 1766 hashval += (val >> 32) & 0xffff; 1767 hashval += (hashval << 10); 1768 hashval ^= (hashval >> 6); 1769 1770 hashval += (val >> 16) & 0xffff; 1771 hashval += (hashval << 10); 1772 hashval ^= (hashval >> 6); 1773 1774 hashval += val & 0xffff; 1775 hashval += (hashval << 10); 1776 hashval ^= (hashval >> 6); 1777 } else { 1778 /* 1779 * This is incredibly painful, but it beats the hell 1780 * out of the alternative. 1781 */ 1782 uint64_t j, size = key[i].dttk_size; 1783 uintptr_t base = (uintptr_t)key[i].dttk_value; 1784 1785 if (!dtrace_canload(base, size, mstate, vstate)) 1786 break; 1787 1788 for (j = 0; j < size; j++) { 1789 hashval += dtrace_load8(base + j); 1790 hashval += (hashval << 10); 1791 hashval ^= (hashval >> 6); 1792 } 1793 } 1794 } 1795 1796 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1797 return (NULL); 1798 1799 hashval += (hashval << 3); 1800 hashval ^= (hashval >> 11); 1801 hashval += (hashval << 15); 1802 1803 /* 1804 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1805 * comes out to be one of our two sentinel hash values. If this 1806 * actually happens, we set the hashval to be a value known to be a 1807 * non-sentinel value. 1808 */ 1809 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1810 hashval = DTRACE_DYNHASH_VALID; 1811 1812 /* 1813 * Yes, it's painful to do a divide here. If the cycle count becomes 1814 * important here, tricks can be pulled to reduce it. (However, it's 1815 * critical that hash collisions be kept to an absolute minimum; 1816 * they're much more painful than a divide.) It's better to have a 1817 * solution that generates few collisions and still keeps things 1818 * relatively simple. 1819 */ 1820 bucket = hashval % dstate->dtds_hashsize; 1821 1822 if (op == DTRACE_DYNVAR_DEALLOC) { 1823 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1824 1825 for (;;) { 1826 while ((lock = *lockp) & 1) 1827 continue; 1828 1829 if (dtrace_casptr((volatile void *)lockp, 1830 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1831 break; 1832 } 1833 1834 dtrace_membar_producer(); 1835 } 1836 1837 top: 1838 prev = NULL; 1839 lock = hash[bucket].dtdh_lock; 1840 1841 dtrace_membar_consumer(); 1842 1843 start = hash[bucket].dtdh_chain; 1844 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1845 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1846 op != DTRACE_DYNVAR_DEALLOC)); 1847 1848 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1849 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1850 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1851 1852 if (dvar->dtdv_hashval != hashval) { 1853 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1854 /* 1855 * We've reached the sink, and therefore the 1856 * end of the hash chain; we can kick out of 1857 * the loop knowing that we have seen a valid 1858 * snapshot of state. 1859 */ 1860 ASSERT(dvar->dtdv_next == NULL); 1861 ASSERT(dvar == &dtrace_dynhash_sink); 1862 break; 1863 } 1864 1865 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1866 /* 1867 * We've gone off the rails: somewhere along 1868 * the line, one of the members of this hash 1869 * chain was deleted. Note that we could also 1870 * detect this by simply letting this loop run 1871 * to completion, as we would eventually hit 1872 * the end of the dirty list. However, we 1873 * want to avoid running the length of the 1874 * dirty list unnecessarily (it might be quite 1875 * long), so we catch this as early as 1876 * possible by detecting the hash marker. In 1877 * this case, we simply set dvar to NULL and 1878 * break; the conditional after the loop will 1879 * send us back to top. 1880 */ 1881 dvar = NULL; 1882 break; 1883 } 1884 1885 goto next; 1886 } 1887 1888 if (dtuple->dtt_nkeys != nkeys) 1889 goto next; 1890 1891 for (i = 0; i < nkeys; i++, dkey++) { 1892 if (dkey->dttk_size != key[i].dttk_size) 1893 goto next; /* size or type mismatch */ 1894 1895 if (dkey->dttk_size != 0) { 1896 if (dtrace_bcmp( 1897 (void *)(uintptr_t)key[i].dttk_value, 1898 (void *)(uintptr_t)dkey->dttk_value, 1899 dkey->dttk_size)) 1900 goto next; 1901 } else { 1902 if (dkey->dttk_value != key[i].dttk_value) 1903 goto next; 1904 } 1905 } 1906 1907 if (op != DTRACE_DYNVAR_DEALLOC) 1908 return (dvar); 1909 1910 ASSERT(dvar->dtdv_next == NULL || 1911 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1912 1913 if (prev != NULL) { 1914 ASSERT(hash[bucket].dtdh_chain != dvar); 1915 ASSERT(start != dvar); 1916 ASSERT(prev->dtdv_next == dvar); 1917 prev->dtdv_next = dvar->dtdv_next; 1918 } else { 1919 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1920 start, dvar->dtdv_next) != start) { 1921 /* 1922 * We have failed to atomically swing the 1923 * hash table head pointer, presumably because 1924 * of a conflicting allocation on another CPU. 1925 * We need to reread the hash chain and try 1926 * again. 1927 */ 1928 goto top; 1929 } 1930 } 1931 1932 dtrace_membar_producer(); 1933 1934 /* 1935 * Now set the hash value to indicate that it's free. 1936 */ 1937 ASSERT(hash[bucket].dtdh_chain != dvar); 1938 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1939 1940 dtrace_membar_producer(); 1941 1942 /* 1943 * Set the next pointer to point at the dirty list, and 1944 * atomically swing the dirty pointer to the newly freed dvar. 1945 */ 1946 do { 1947 next = dcpu->dtdsc_dirty; 1948 dvar->dtdv_next = next; 1949 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1950 1951 /* 1952 * Finally, unlock this hash bucket. 1953 */ 1954 ASSERT(hash[bucket].dtdh_lock == lock); 1955 ASSERT(lock & 1); 1956 hash[bucket].dtdh_lock++; 1957 1958 return (NULL); 1959 next: 1960 prev = dvar; 1961 continue; 1962 } 1963 1964 if (dvar == NULL) { 1965 /* 1966 * If dvar is NULL, it is because we went off the rails: 1967 * one of the elements that we traversed in the hash chain 1968 * was deleted while we were traversing it. In this case, 1969 * we assert that we aren't doing a dealloc (deallocs lock 1970 * the hash bucket to prevent themselves from racing with 1971 * one another), and retry the hash chain traversal. 1972 */ 1973 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1974 goto top; 1975 } 1976 1977 if (op != DTRACE_DYNVAR_ALLOC) { 1978 /* 1979 * If we are not to allocate a new variable, we want to 1980 * return NULL now. Before we return, check that the value 1981 * of the lock word hasn't changed. If it has, we may have 1982 * seen an inconsistent snapshot. 1983 */ 1984 if (op == DTRACE_DYNVAR_NOALLOC) { 1985 if (hash[bucket].dtdh_lock != lock) 1986 goto top; 1987 } else { 1988 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1989 ASSERT(hash[bucket].dtdh_lock == lock); 1990 ASSERT(lock & 1); 1991 hash[bucket].dtdh_lock++; 1992 } 1993 1994 return (NULL); 1995 } 1996 1997 /* 1998 * We need to allocate a new dynamic variable. The size we need is the 1999 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 2000 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 2001 * the size of any referred-to data (dsize). We then round the final 2002 * size up to the chunksize for allocation. 2003 */ 2004 for (ksize = 0, i = 0; i < nkeys; i++) 2005 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 2006 2007 /* 2008 * This should be pretty much impossible, but could happen if, say, 2009 * strange DIF specified the tuple. Ideally, this should be an 2010 * assertion and not an error condition -- but that requires that the 2011 * chunksize calculation in dtrace_difo_chunksize() be absolutely 2012 * bullet-proof. (That is, it must not be able to be fooled by 2013 * malicious DIF.) Given the lack of backwards branches in DIF, 2014 * solving this would presumably not amount to solving the Halting 2015 * Problem -- but it still seems awfully hard. 2016 */ 2017 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 2018 ksize + dsize > chunksize) { 2019 dcpu->dtdsc_drops++; 2020 return (NULL); 2021 } 2022 2023 nstate = DTRACE_DSTATE_EMPTY; 2024 2025 do { 2026 retry: 2027 free = dcpu->dtdsc_free; 2028 2029 if (free == NULL) { 2030 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 2031 void *rval; 2032 2033 if (clean == NULL) { 2034 /* 2035 * We're out of dynamic variable space on 2036 * this CPU. Unless we have tried all CPUs, 2037 * we'll try to allocate from a different 2038 * CPU. 2039 */ 2040 switch (dstate->dtds_state) { 2041 case DTRACE_DSTATE_CLEAN: { 2042 void *sp = &dstate->dtds_state; 2043 2044 if (++cpu >= NCPU) 2045 cpu = 0; 2046 2047 if (dcpu->dtdsc_dirty != NULL && 2048 nstate == DTRACE_DSTATE_EMPTY) 2049 nstate = DTRACE_DSTATE_DIRTY; 2050 2051 if (dcpu->dtdsc_rinsing != NULL) 2052 nstate = DTRACE_DSTATE_RINSING; 2053 2054 dcpu = &dstate->dtds_percpu[cpu]; 2055 2056 if (cpu != me) 2057 goto retry; 2058 2059 (void) dtrace_cas32(sp, 2060 DTRACE_DSTATE_CLEAN, nstate); 2061 2062 /* 2063 * To increment the correct bean 2064 * counter, take another lap. 2065 */ 2066 goto retry; 2067 } 2068 2069 case DTRACE_DSTATE_DIRTY: 2070 dcpu->dtdsc_dirty_drops++; 2071 break; 2072 2073 case DTRACE_DSTATE_RINSING: 2074 dcpu->dtdsc_rinsing_drops++; 2075 break; 2076 2077 case DTRACE_DSTATE_EMPTY: 2078 dcpu->dtdsc_drops++; 2079 break; 2080 } 2081 2082 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 2083 return (NULL); 2084 } 2085 2086 /* 2087 * The clean list appears to be non-empty. We want to 2088 * move the clean list to the free list; we start by 2089 * moving the clean pointer aside. 2090 */ 2091 if (dtrace_casptr(&dcpu->dtdsc_clean, 2092 clean, NULL) != clean) { 2093 /* 2094 * We are in one of two situations: 2095 * 2096 * (a) The clean list was switched to the 2097 * free list by another CPU. 2098 * 2099 * (b) The clean list was added to by the 2100 * cleansing cyclic. 2101 * 2102 * In either of these situations, we can 2103 * just reattempt the free list allocation. 2104 */ 2105 goto retry; 2106 } 2107 2108 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 2109 2110 /* 2111 * Now we'll move the clean list to our free list. 2112 * It's impossible for this to fail: the only way 2113 * the free list can be updated is through this 2114 * code path, and only one CPU can own the clean list. 2115 * Thus, it would only be possible for this to fail if 2116 * this code were racing with dtrace_dynvar_clean(). 2117 * (That is, if dtrace_dynvar_clean() updated the clean 2118 * list, and we ended up racing to update the free 2119 * list.) This race is prevented by the dtrace_sync() 2120 * in dtrace_dynvar_clean() -- which flushes the 2121 * owners of the clean lists out before resetting 2122 * the clean lists. 2123 */ 2124 dcpu = &dstate->dtds_percpu[me]; 2125 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 2126 ASSERT(rval == NULL); 2127 goto retry; 2128 } 2129 2130 dvar = free; 2131 new_free = dvar->dtdv_next; 2132 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 2133 2134 /* 2135 * We have now allocated a new chunk. We copy the tuple keys into the 2136 * tuple array and copy any referenced key data into the data space 2137 * following the tuple array. As we do this, we relocate dttk_value 2138 * in the final tuple to point to the key data address in the chunk. 2139 */ 2140 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 2141 dvar->dtdv_data = (void *)(kdata + ksize); 2142 dvar->dtdv_tuple.dtt_nkeys = nkeys; 2143 2144 for (i = 0; i < nkeys; i++) { 2145 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 2146 size_t kesize = key[i].dttk_size; 2147 2148 if (kesize != 0) { 2149 dtrace_bcopy( 2150 (const void *)(uintptr_t)key[i].dttk_value, 2151 (void *)kdata, kesize); 2152 dkey->dttk_value = kdata; 2153 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 2154 } else { 2155 dkey->dttk_value = key[i].dttk_value; 2156 } 2157 2158 dkey->dttk_size = kesize; 2159 } 2160 2161 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 2162 dvar->dtdv_hashval = hashval; 2163 dvar->dtdv_next = start; 2164 2165 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 2166 return (dvar); 2167 2168 /* 2169 * The cas has failed. Either another CPU is adding an element to 2170 * this hash chain, or another CPU is deleting an element from this 2171 * hash chain. The simplest way to deal with both of these cases 2172 * (though not necessarily the most efficient) is to free our 2173 * allocated block and re-attempt it all. Note that the free is 2174 * to the dirty list and _not_ to the free list. This is to prevent 2175 * races with allocators, above. 2176 */ 2177 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2178 2179 dtrace_membar_producer(); 2180 2181 do { 2182 free = dcpu->dtdsc_dirty; 2183 dvar->dtdv_next = free; 2184 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 2185 2186 goto top; 2187 } 2188 2189 /*ARGSUSED*/ 2190 static void 2191 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 2192 { 2193 if ((int64_t)nval < (int64_t)*oval) 2194 *oval = nval; 2195 } 2196 2197 /*ARGSUSED*/ 2198 static void 2199 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2200 { 2201 if ((int64_t)nval > (int64_t)*oval) 2202 *oval = nval; 2203 } 2204 2205 static void 2206 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2207 { 2208 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2209 int64_t val = (int64_t)nval; 2210 2211 if (val < 0) { 2212 for (i = 0; i < zero; i++) { 2213 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2214 quanta[i] += incr; 2215 return; 2216 } 2217 } 2218 } else { 2219 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2220 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2221 quanta[i - 1] += incr; 2222 return; 2223 } 2224 } 2225 2226 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2227 return; 2228 } 2229 2230 ASSERT(0); 2231 } 2232 2233 static void 2234 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2235 { 2236 uint64_t arg = *lquanta++; 2237 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2238 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2239 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2240 int32_t val = (int32_t)nval, level; 2241 2242 ASSERT(step != 0); 2243 ASSERT(levels != 0); 2244 2245 if (val < base) { 2246 /* 2247 * This is an underflow. 2248 */ 2249 lquanta[0] += incr; 2250 return; 2251 } 2252 2253 level = (val - base) / step; 2254 2255 if (level < levels) { 2256 lquanta[level + 1] += incr; 2257 return; 2258 } 2259 2260 /* 2261 * This is an overflow. 2262 */ 2263 lquanta[levels + 1] += incr; 2264 } 2265 2266 static int 2267 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2268 uint16_t high, uint16_t nsteps, int64_t value) 2269 { 2270 int64_t this = 1, last, next; 2271 int base = 1, order; 2272 2273 ASSERT(factor <= nsteps); 2274 ASSERT(nsteps % factor == 0); 2275 2276 for (order = 0; order < low; order++) 2277 this *= factor; 2278 2279 /* 2280 * If our value is less than our factor taken to the power of the 2281 * low order of magnitude, it goes into the zeroth bucket. 2282 */ 2283 if (value < (last = this)) 2284 return (0); 2285 2286 for (this *= factor; order <= high; order++) { 2287 int nbuckets = this > nsteps ? nsteps : this; 2288 2289 if ((next = this * factor) < this) { 2290 /* 2291 * We should not generally get log/linear quantizations 2292 * with a high magnitude that allows 64-bits to 2293 * overflow, but we nonetheless protect against this 2294 * by explicitly checking for overflow, and clamping 2295 * our value accordingly. 2296 */ 2297 value = this - 1; 2298 } 2299 2300 if (value < this) { 2301 /* 2302 * If our value lies within this order of magnitude, 2303 * determine its position by taking the offset within 2304 * the order of magnitude, dividing by the bucket 2305 * width, and adding to our (accumulated) base. 2306 */ 2307 return (base + (value - last) / (this / nbuckets)); 2308 } 2309 2310 base += nbuckets - (nbuckets / factor); 2311 last = this; 2312 this = next; 2313 } 2314 2315 /* 2316 * Our value is greater than or equal to our factor taken to the 2317 * power of one plus the high magnitude -- return the top bucket. 2318 */ 2319 return (base); 2320 } 2321 2322 static void 2323 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2324 { 2325 uint64_t arg = *llquanta++; 2326 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2327 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2328 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2329 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2330 2331 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2332 low, high, nsteps, nval)] += incr; 2333 } 2334 2335 /*ARGSUSED*/ 2336 static void 2337 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2338 { 2339 data[0]++; 2340 data[1] += nval; 2341 } 2342 2343 /*ARGSUSED*/ 2344 static void 2345 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2346 { 2347 int64_t snval = (int64_t)nval; 2348 uint64_t tmp[2]; 2349 2350 data[0]++; 2351 data[1] += nval; 2352 2353 /* 2354 * What we want to say here is: 2355 * 2356 * data[2] += nval * nval; 2357 * 2358 * But given that nval is 64-bit, we could easily overflow, so 2359 * we do this as 128-bit arithmetic. 2360 */ 2361 if (snval < 0) 2362 snval = -snval; 2363 2364 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2365 dtrace_add_128(data + 2, tmp, data + 2); 2366 } 2367 2368 /*ARGSUSED*/ 2369 static void 2370 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2371 { 2372 *oval = *oval + 1; 2373 } 2374 2375 /*ARGSUSED*/ 2376 static void 2377 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2378 { 2379 *oval += nval; 2380 } 2381 2382 /* 2383 * Aggregate given the tuple in the principal data buffer, and the aggregating 2384 * action denoted by the specified dtrace_aggregation_t. The aggregation 2385 * buffer is specified as the buf parameter. This routine does not return 2386 * failure; if there is no space in the aggregation buffer, the data will be 2387 * dropped, and a corresponding counter incremented. 2388 */ 2389 static void 2390 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2391 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2392 { 2393 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2394 uint32_t i, ndx, size, fsize; 2395 uint32_t align = sizeof (uint64_t) - 1; 2396 dtrace_aggbuffer_t *agb; 2397 dtrace_aggkey_t *key; 2398 uint32_t hashval = 0, limit, isstr; 2399 caddr_t tomax, data, kdata; 2400 dtrace_actkind_t action; 2401 dtrace_action_t *act; 2402 uintptr_t offs; 2403 2404 if (buf == NULL) 2405 return; 2406 2407 if (!agg->dtag_hasarg) { 2408 /* 2409 * Currently, only quantize() and lquantize() take additional 2410 * arguments, and they have the same semantics: an increment 2411 * value that defaults to 1 when not present. If additional 2412 * aggregating actions take arguments, the setting of the 2413 * default argument value will presumably have to become more 2414 * sophisticated... 2415 */ 2416 arg = 1; 2417 } 2418 2419 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2420 size = rec->dtrd_offset - agg->dtag_base; 2421 fsize = size + rec->dtrd_size; 2422 2423 ASSERT(dbuf->dtb_tomax != NULL); 2424 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2425 2426 if ((tomax = buf->dtb_tomax) == NULL) { 2427 dtrace_buffer_drop(buf); 2428 return; 2429 } 2430 2431 /* 2432 * The metastructure is always at the bottom of the buffer. 2433 */ 2434 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2435 sizeof (dtrace_aggbuffer_t)); 2436 2437 if (buf->dtb_offset == 0) { 2438 /* 2439 * We just kludge up approximately 1/8th of the size to be 2440 * buckets. If this guess ends up being routinely 2441 * off-the-mark, we may need to dynamically readjust this 2442 * based on past performance. 2443 */ 2444 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2445 2446 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2447 (uintptr_t)tomax || hashsize == 0) { 2448 /* 2449 * We've been given a ludicrously small buffer; 2450 * increment our drop count and leave. 2451 */ 2452 dtrace_buffer_drop(buf); 2453 return; 2454 } 2455 2456 /* 2457 * And now, a pathetic attempt to try to get a an odd (or 2458 * perchance, a prime) hash size for better hash distribution. 2459 */ 2460 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2461 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2462 2463 agb->dtagb_hashsize = hashsize; 2464 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2465 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2466 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2467 2468 for (i = 0; i < agb->dtagb_hashsize; i++) 2469 agb->dtagb_hash[i] = NULL; 2470 } 2471 2472 ASSERT(agg->dtag_first != NULL); 2473 ASSERT(agg->dtag_first->dta_intuple); 2474 2475 /* 2476 * Calculate the hash value based on the key. Note that we _don't_ 2477 * include the aggid in the hashing (but we will store it as part of 2478 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2479 * algorithm: a simple, quick algorithm that has no known funnels, and 2480 * gets good distribution in practice. The efficacy of the hashing 2481 * algorithm (and a comparison with other algorithms) may be found by 2482 * running the ::dtrace_aggstat MDB dcmd. 2483 */ 2484 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2485 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2486 limit = i + act->dta_rec.dtrd_size; 2487 ASSERT(limit <= size); 2488 isstr = DTRACEACT_ISSTRING(act); 2489 2490 for (; i < limit; i++) { 2491 hashval += data[i]; 2492 hashval += (hashval << 10); 2493 hashval ^= (hashval >> 6); 2494 2495 if (isstr && data[i] == '\0') 2496 break; 2497 } 2498 } 2499 2500 hashval += (hashval << 3); 2501 hashval ^= (hashval >> 11); 2502 hashval += (hashval << 15); 2503 2504 /* 2505 * Yes, the divide here is expensive -- but it's generally the least 2506 * of the performance issues given the amount of data that we iterate 2507 * over to compute hash values, compare data, etc. 2508 */ 2509 ndx = hashval % agb->dtagb_hashsize; 2510 2511 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2512 ASSERT((caddr_t)key >= tomax); 2513 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2514 2515 if (hashval != key->dtak_hashval || key->dtak_size != size) 2516 continue; 2517 2518 kdata = key->dtak_data; 2519 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2520 2521 for (act = agg->dtag_first; act->dta_intuple; 2522 act = act->dta_next) { 2523 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2524 limit = i + act->dta_rec.dtrd_size; 2525 ASSERT(limit <= size); 2526 isstr = DTRACEACT_ISSTRING(act); 2527 2528 for (; i < limit; i++) { 2529 if (kdata[i] != data[i]) 2530 goto next; 2531 2532 if (isstr && data[i] == '\0') 2533 break; 2534 } 2535 } 2536 2537 if (action != key->dtak_action) { 2538 /* 2539 * We are aggregating on the same value in the same 2540 * aggregation with two different aggregating actions. 2541 * (This should have been picked up in the compiler, 2542 * so we may be dealing with errant or devious DIF.) 2543 * This is an error condition; we indicate as much, 2544 * and return. 2545 */ 2546 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2547 return; 2548 } 2549 2550 /* 2551 * This is a hit: we need to apply the aggregator to 2552 * the value at this key. 2553 */ 2554 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2555 return; 2556 next: 2557 continue; 2558 } 2559 2560 /* 2561 * We didn't find it. We need to allocate some zero-filled space, 2562 * link it into the hash table appropriately, and apply the aggregator 2563 * to the (zero-filled) value. 2564 */ 2565 offs = buf->dtb_offset; 2566 while (offs & (align - 1)) 2567 offs += sizeof (uint32_t); 2568 2569 /* 2570 * If we don't have enough room to both allocate a new key _and_ 2571 * its associated data, increment the drop count and return. 2572 */ 2573 if ((uintptr_t)tomax + offs + fsize > 2574 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2575 dtrace_buffer_drop(buf); 2576 return; 2577 } 2578 2579 /*CONSTCOND*/ 2580 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2581 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2582 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2583 2584 key->dtak_data = kdata = tomax + offs; 2585 buf->dtb_offset = offs + fsize; 2586 2587 /* 2588 * Now copy the data across. 2589 */ 2590 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2591 2592 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2593 kdata[i] = data[i]; 2594 2595 /* 2596 * Because strings are not zeroed out by default, we need to iterate 2597 * looking for actions that store strings, and we need to explicitly 2598 * pad these strings out with zeroes. 2599 */ 2600 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2601 int nul; 2602 2603 if (!DTRACEACT_ISSTRING(act)) 2604 continue; 2605 2606 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2607 limit = i + act->dta_rec.dtrd_size; 2608 ASSERT(limit <= size); 2609 2610 for (nul = 0; i < limit; i++) { 2611 if (nul) { 2612 kdata[i] = '\0'; 2613 continue; 2614 } 2615 2616 if (data[i] != '\0') 2617 continue; 2618 2619 nul = 1; 2620 } 2621 } 2622 2623 for (i = size; i < fsize; i++) 2624 kdata[i] = 0; 2625 2626 key->dtak_hashval = hashval; 2627 key->dtak_size = size; 2628 key->dtak_action = action; 2629 key->dtak_next = agb->dtagb_hash[ndx]; 2630 agb->dtagb_hash[ndx] = key; 2631 2632 /* 2633 * Finally, apply the aggregator. 2634 */ 2635 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2636 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2637 } 2638 2639 /* 2640 * Given consumer state, this routine finds a speculation in the INACTIVE 2641 * state and transitions it into the ACTIVE state. If there is no speculation 2642 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2643 * incremented -- it is up to the caller to take appropriate action. 2644 */ 2645 static int 2646 dtrace_speculation(dtrace_state_t *state) 2647 { 2648 int i = 0; 2649 dtrace_speculation_state_t current; 2650 uint32_t *stat = &state->dts_speculations_unavail, count; 2651 2652 while (i < state->dts_nspeculations) { 2653 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2654 2655 current = spec->dtsp_state; 2656 2657 if (current != DTRACESPEC_INACTIVE) { 2658 if (current == DTRACESPEC_COMMITTINGMANY || 2659 current == DTRACESPEC_COMMITTING || 2660 current == DTRACESPEC_DISCARDING) 2661 stat = &state->dts_speculations_busy; 2662 i++; 2663 continue; 2664 } 2665 2666 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2667 current, DTRACESPEC_ACTIVE) == current) 2668 return (i + 1); 2669 } 2670 2671 /* 2672 * We couldn't find a speculation. If we found as much as a single 2673 * busy speculation buffer, we'll attribute this failure as "busy" 2674 * instead of "unavail". 2675 */ 2676 do { 2677 count = *stat; 2678 } while (dtrace_cas32(stat, count, count + 1) != count); 2679 2680 return (0); 2681 } 2682 2683 /* 2684 * This routine commits an active speculation. If the specified speculation 2685 * is not in a valid state to perform a commit(), this routine will silently do 2686 * nothing. The state of the specified speculation is transitioned according 2687 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2688 */ 2689 static void 2690 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2691 dtrace_specid_t which) 2692 { 2693 dtrace_speculation_t *spec; 2694 dtrace_buffer_t *src, *dest; 2695 uintptr_t daddr, saddr, dlimit, slimit; 2696 dtrace_speculation_state_t current, new = 0; 2697 intptr_t offs; 2698 uint64_t timestamp; 2699 2700 if (which == 0) 2701 return; 2702 2703 if (which > state->dts_nspeculations) { 2704 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2705 return; 2706 } 2707 2708 spec = &state->dts_speculations[which - 1]; 2709 src = &spec->dtsp_buffer[cpu]; 2710 dest = &state->dts_buffer[cpu]; 2711 2712 do { 2713 current = spec->dtsp_state; 2714 2715 if (current == DTRACESPEC_COMMITTINGMANY) 2716 break; 2717 2718 switch (current) { 2719 case DTRACESPEC_INACTIVE: 2720 case DTRACESPEC_DISCARDING: 2721 return; 2722 2723 case DTRACESPEC_COMMITTING: 2724 /* 2725 * This is only possible if we are (a) commit()'ing 2726 * without having done a prior speculate() on this CPU 2727 * and (b) racing with another commit() on a different 2728 * CPU. There's nothing to do -- we just assert that 2729 * our offset is 0. 2730 */ 2731 ASSERT(src->dtb_offset == 0); 2732 return; 2733 2734 case DTRACESPEC_ACTIVE: 2735 new = DTRACESPEC_COMMITTING; 2736 break; 2737 2738 case DTRACESPEC_ACTIVEONE: 2739 /* 2740 * This speculation is active on one CPU. If our 2741 * buffer offset is non-zero, we know that the one CPU 2742 * must be us. Otherwise, we are committing on a 2743 * different CPU from the speculate(), and we must 2744 * rely on being asynchronously cleaned. 2745 */ 2746 if (src->dtb_offset != 0) { 2747 new = DTRACESPEC_COMMITTING; 2748 break; 2749 } 2750 /*FALLTHROUGH*/ 2751 2752 case DTRACESPEC_ACTIVEMANY: 2753 new = DTRACESPEC_COMMITTINGMANY; 2754 break; 2755 2756 default: 2757 ASSERT(0); 2758 } 2759 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2760 current, new) != current); 2761 2762 /* 2763 * We have set the state to indicate that we are committing this 2764 * speculation. Now reserve the necessary space in the destination 2765 * buffer. 2766 */ 2767 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2768 sizeof (uint64_t), state, NULL)) < 0) { 2769 dtrace_buffer_drop(dest); 2770 goto out; 2771 } 2772 2773 /* 2774 * We have sufficient space to copy the speculative buffer into the 2775 * primary buffer. First, modify the speculative buffer, filling 2776 * in the timestamp of all entries with the current time. The data 2777 * must have the commit() time rather than the time it was traced, 2778 * so that all entries in the primary buffer are in timestamp order. 2779 */ 2780 timestamp = dtrace_gethrtime(); 2781 saddr = (uintptr_t)src->dtb_tomax; 2782 slimit = saddr + src->dtb_offset; 2783 while (saddr < slimit) { 2784 size_t size; 2785 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2786 2787 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2788 saddr += sizeof (dtrace_epid_t); 2789 continue; 2790 } 2791 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2792 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2793 2794 ASSERT3U(saddr + size, <=, slimit); 2795 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2796 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2797 2798 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2799 2800 saddr += size; 2801 } 2802 2803 /* 2804 * Copy the buffer across. (Note that this is a 2805 * highly subobtimal bcopy(); in the unlikely event that this becomes 2806 * a serious performance issue, a high-performance DTrace-specific 2807 * bcopy() should obviously be invented.) 2808 */ 2809 daddr = (uintptr_t)dest->dtb_tomax + offs; 2810 dlimit = daddr + src->dtb_offset; 2811 saddr = (uintptr_t)src->dtb_tomax; 2812 2813 /* 2814 * First, the aligned portion. 2815 */ 2816 while (dlimit - daddr >= sizeof (uint64_t)) { 2817 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2818 2819 daddr += sizeof (uint64_t); 2820 saddr += sizeof (uint64_t); 2821 } 2822 2823 /* 2824 * Now any left-over bit... 2825 */ 2826 while (dlimit - daddr) 2827 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2828 2829 /* 2830 * Finally, commit the reserved space in the destination buffer. 2831 */ 2832 dest->dtb_offset = offs + src->dtb_offset; 2833 2834 out: 2835 /* 2836 * If we're lucky enough to be the only active CPU on this speculation 2837 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2838 */ 2839 if (current == DTRACESPEC_ACTIVE || 2840 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2841 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2842 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2843 2844 ASSERT(rval == DTRACESPEC_COMMITTING); 2845 } 2846 2847 src->dtb_offset = 0; 2848 src->dtb_xamot_drops += src->dtb_drops; 2849 src->dtb_drops = 0; 2850 } 2851 2852 /* 2853 * This routine discards an active speculation. If the specified speculation 2854 * is not in a valid state to perform a discard(), this routine will silently 2855 * do nothing. The state of the specified speculation is transitioned 2856 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2857 */ 2858 static void 2859 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2860 dtrace_specid_t which) 2861 { 2862 dtrace_speculation_t *spec; 2863 dtrace_speculation_state_t current, new = 0; 2864 dtrace_buffer_t *buf; 2865 2866 if (which == 0) 2867 return; 2868 2869 if (which > state->dts_nspeculations) { 2870 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2871 return; 2872 } 2873 2874 spec = &state->dts_speculations[which - 1]; 2875 buf = &spec->dtsp_buffer[cpu]; 2876 2877 do { 2878 current = spec->dtsp_state; 2879 2880 switch (current) { 2881 case DTRACESPEC_INACTIVE: 2882 case DTRACESPEC_COMMITTINGMANY: 2883 case DTRACESPEC_COMMITTING: 2884 case DTRACESPEC_DISCARDING: 2885 return; 2886 2887 case DTRACESPEC_ACTIVE: 2888 case DTRACESPEC_ACTIVEMANY: 2889 new = DTRACESPEC_DISCARDING; 2890 break; 2891 2892 case DTRACESPEC_ACTIVEONE: 2893 if (buf->dtb_offset != 0) { 2894 new = DTRACESPEC_INACTIVE; 2895 } else { 2896 new = DTRACESPEC_DISCARDING; 2897 } 2898 break; 2899 2900 default: 2901 ASSERT(0); 2902 } 2903 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2904 current, new) != current); 2905 2906 buf->dtb_offset = 0; 2907 buf->dtb_drops = 0; 2908 } 2909 2910 /* 2911 * Note: not called from probe context. This function is called 2912 * asynchronously from cross call context to clean any speculations that are 2913 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2914 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2915 * speculation. 2916 */ 2917 static void 2918 dtrace_speculation_clean_here(dtrace_state_t *state) 2919 { 2920 dtrace_icookie_t cookie; 2921 processorid_t cpu = curcpu; 2922 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2923 dtrace_specid_t i; 2924 2925 cookie = dtrace_interrupt_disable(); 2926 2927 if (dest->dtb_tomax == NULL) { 2928 dtrace_interrupt_enable(cookie); 2929 return; 2930 } 2931 2932 for (i = 0; i < state->dts_nspeculations; i++) { 2933 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2934 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2935 2936 if (src->dtb_tomax == NULL) 2937 continue; 2938 2939 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2940 src->dtb_offset = 0; 2941 continue; 2942 } 2943 2944 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2945 continue; 2946 2947 if (src->dtb_offset == 0) 2948 continue; 2949 2950 dtrace_speculation_commit(state, cpu, i + 1); 2951 } 2952 2953 dtrace_interrupt_enable(cookie); 2954 } 2955 2956 /* 2957 * Note: not called from probe context. This function is called 2958 * asynchronously (and at a regular interval) to clean any speculations that 2959 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2960 * is work to be done, it cross calls all CPUs to perform that work; 2961 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2962 * INACTIVE state until they have been cleaned by all CPUs. 2963 */ 2964 static void 2965 dtrace_speculation_clean(dtrace_state_t *state) 2966 { 2967 int work = 0, rv; 2968 dtrace_specid_t i; 2969 2970 for (i = 0; i < state->dts_nspeculations; i++) { 2971 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2972 2973 ASSERT(!spec->dtsp_cleaning); 2974 2975 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2976 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2977 continue; 2978 2979 work++; 2980 spec->dtsp_cleaning = 1; 2981 } 2982 2983 if (!work) 2984 return; 2985 2986 dtrace_xcall(DTRACE_CPUALL, 2987 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2988 2989 /* 2990 * We now know that all CPUs have committed or discarded their 2991 * speculation buffers, as appropriate. We can now set the state 2992 * to inactive. 2993 */ 2994 for (i = 0; i < state->dts_nspeculations; i++) { 2995 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2996 dtrace_speculation_state_t current, new; 2997 2998 if (!spec->dtsp_cleaning) 2999 continue; 3000 3001 current = spec->dtsp_state; 3002 ASSERT(current == DTRACESPEC_DISCARDING || 3003 current == DTRACESPEC_COMMITTINGMANY); 3004 3005 new = DTRACESPEC_INACTIVE; 3006 3007 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 3008 ASSERT(rv == current); 3009 spec->dtsp_cleaning = 0; 3010 } 3011 } 3012 3013 /* 3014 * Called as part of a speculate() to get the speculative buffer associated 3015 * with a given speculation. Returns NULL if the specified speculation is not 3016 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 3017 * the active CPU is not the specified CPU -- the speculation will be 3018 * atomically transitioned into the ACTIVEMANY state. 3019 */ 3020 static dtrace_buffer_t * 3021 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 3022 dtrace_specid_t which) 3023 { 3024 dtrace_speculation_t *spec; 3025 dtrace_speculation_state_t current, new = 0; 3026 dtrace_buffer_t *buf; 3027 3028 if (which == 0) 3029 return (NULL); 3030 3031 if (which > state->dts_nspeculations) { 3032 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 3033 return (NULL); 3034 } 3035 3036 spec = &state->dts_speculations[which - 1]; 3037 buf = &spec->dtsp_buffer[cpuid]; 3038 3039 do { 3040 current = spec->dtsp_state; 3041 3042 switch (current) { 3043 case DTRACESPEC_INACTIVE: 3044 case DTRACESPEC_COMMITTINGMANY: 3045 case DTRACESPEC_DISCARDING: 3046 return (NULL); 3047 3048 case DTRACESPEC_COMMITTING: 3049 ASSERT(buf->dtb_offset == 0); 3050 return (NULL); 3051 3052 case DTRACESPEC_ACTIVEONE: 3053 /* 3054 * This speculation is currently active on one CPU. 3055 * Check the offset in the buffer; if it's non-zero, 3056 * that CPU must be us (and we leave the state alone). 3057 * If it's zero, assume that we're starting on a new 3058 * CPU -- and change the state to indicate that the 3059 * speculation is active on more than one CPU. 3060 */ 3061 if (buf->dtb_offset != 0) 3062 return (buf); 3063 3064 new = DTRACESPEC_ACTIVEMANY; 3065 break; 3066 3067 case DTRACESPEC_ACTIVEMANY: 3068 return (buf); 3069 3070 case DTRACESPEC_ACTIVE: 3071 new = DTRACESPEC_ACTIVEONE; 3072 break; 3073 3074 default: 3075 ASSERT(0); 3076 } 3077 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3078 current, new) != current); 3079 3080 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 3081 return (buf); 3082 } 3083 3084 /* 3085 * Return a string. In the event that the user lacks the privilege to access 3086 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3087 * don't fail access checking. 3088 * 3089 * dtrace_dif_variable() uses this routine as a helper for various 3090 * builtin values such as 'execname' and 'probefunc.' 3091 */ 3092 uintptr_t 3093 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 3094 dtrace_mstate_t *mstate) 3095 { 3096 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3097 uintptr_t ret; 3098 size_t strsz; 3099 3100 /* 3101 * The easy case: this probe is allowed to read all of memory, so 3102 * we can just return this as a vanilla pointer. 3103 */ 3104 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 3105 return (addr); 3106 3107 /* 3108 * This is the tougher case: we copy the string in question from 3109 * kernel memory into scratch memory and return it that way: this 3110 * ensures that we won't trip up when access checking tests the 3111 * BYREF return value. 3112 */ 3113 strsz = dtrace_strlen((char *)addr, size) + 1; 3114 3115 if (mstate->dtms_scratch_ptr + strsz > 3116 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3117 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3118 return (0); 3119 } 3120 3121 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3122 strsz); 3123 ret = mstate->dtms_scratch_ptr; 3124 mstate->dtms_scratch_ptr += strsz; 3125 return (ret); 3126 } 3127 3128 /* 3129 * Return a string from a memoy address which is known to have one or 3130 * more concatenated, individually zero terminated, sub-strings. 3131 * In the event that the user lacks the privilege to access 3132 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3133 * don't fail access checking. 3134 * 3135 * dtrace_dif_variable() uses this routine as a helper for various 3136 * builtin values such as 'execargs'. 3137 */ 3138 static uintptr_t 3139 dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 3140 dtrace_mstate_t *mstate) 3141 { 3142 char *p; 3143 size_t i; 3144 uintptr_t ret; 3145 3146 if (mstate->dtms_scratch_ptr + strsz > 3147 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3148 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3149 return (0); 3150 } 3151 3152 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3153 strsz); 3154 3155 /* Replace sub-string termination characters with a space. */ 3156 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 3157 p++, i++) 3158 if (*p == '\0') 3159 *p = ' '; 3160 3161 ret = mstate->dtms_scratch_ptr; 3162 mstate->dtms_scratch_ptr += strsz; 3163 return (ret); 3164 } 3165 3166 /* 3167 * This function implements the DIF emulator's variable lookups. The emulator 3168 * passes a reserved variable identifier and optional built-in array index. 3169 */ 3170 static uint64_t 3171 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 3172 uint64_t ndx) 3173 { 3174 /* 3175 * If we're accessing one of the uncached arguments, we'll turn this 3176 * into a reference in the args array. 3177 */ 3178 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 3179 ndx = v - DIF_VAR_ARG0; 3180 v = DIF_VAR_ARGS; 3181 } 3182 3183 switch (v) { 3184 case DIF_VAR_ARGS: 3185 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 3186 if (ndx >= sizeof (mstate->dtms_arg) / 3187 sizeof (mstate->dtms_arg[0])) { 3188 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3189 dtrace_provider_t *pv; 3190 uint64_t val; 3191 3192 pv = mstate->dtms_probe->dtpr_provider; 3193 if (pv->dtpv_pops.dtps_getargval != NULL) 3194 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 3195 mstate->dtms_probe->dtpr_id, 3196 mstate->dtms_probe->dtpr_arg, ndx, aframes); 3197 else 3198 val = dtrace_getarg(ndx, aframes); 3199 3200 /* 3201 * This is regrettably required to keep the compiler 3202 * from tail-optimizing the call to dtrace_getarg(). 3203 * The condition always evaluates to true, but the 3204 * compiler has no way of figuring that out a priori. 3205 * (None of this would be necessary if the compiler 3206 * could be relied upon to _always_ tail-optimize 3207 * the call to dtrace_getarg() -- but it can't.) 3208 */ 3209 if (mstate->dtms_probe != NULL) 3210 return (val); 3211 3212 ASSERT(0); 3213 } 3214 3215 return (mstate->dtms_arg[ndx]); 3216 3217 #ifdef illumos 3218 case DIF_VAR_UREGS: { 3219 klwp_t *lwp; 3220 3221 if (!dtrace_priv_proc(state)) 3222 return (0); 3223 3224 if ((lwp = curthread->t_lwp) == NULL) { 3225 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3226 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 3227 return (0); 3228 } 3229 3230 return (dtrace_getreg(lwp->lwp_regs, ndx)); 3231 return (0); 3232 } 3233 #else 3234 case DIF_VAR_UREGS: { 3235 struct trapframe *tframe; 3236 3237 if (!dtrace_priv_proc(state)) 3238 return (0); 3239 3240 if ((tframe = curthread->td_frame) == NULL) { 3241 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3242 cpu_core[curcpu].cpuc_dtrace_illval = 0; 3243 return (0); 3244 } 3245 3246 return (dtrace_getreg(tframe, ndx)); 3247 } 3248 #endif 3249 3250 case DIF_VAR_CURTHREAD: 3251 if (!dtrace_priv_proc(state)) 3252 return (0); 3253 return ((uint64_t)(uintptr_t)curthread); 3254 3255 case DIF_VAR_TIMESTAMP: 3256 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 3257 mstate->dtms_timestamp = dtrace_gethrtime(); 3258 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 3259 } 3260 return (mstate->dtms_timestamp); 3261 3262 case DIF_VAR_VTIMESTAMP: 3263 ASSERT(dtrace_vtime_references != 0); 3264 return (curthread->t_dtrace_vtime); 3265 3266 case DIF_VAR_WALLTIMESTAMP: 3267 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3268 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3269 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3270 } 3271 return (mstate->dtms_walltimestamp); 3272 3273 #ifdef illumos 3274 case DIF_VAR_IPL: 3275 if (!dtrace_priv_kernel(state)) 3276 return (0); 3277 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3278 mstate->dtms_ipl = dtrace_getipl(); 3279 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3280 } 3281 return (mstate->dtms_ipl); 3282 #endif 3283 3284 case DIF_VAR_EPID: 3285 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3286 return (mstate->dtms_epid); 3287 3288 case DIF_VAR_ID: 3289 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3290 return (mstate->dtms_probe->dtpr_id); 3291 3292 case DIF_VAR_STACKDEPTH: 3293 if (!dtrace_priv_kernel(state)) 3294 return (0); 3295 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3296 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3297 3298 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3299 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3300 } 3301 return (mstate->dtms_stackdepth); 3302 3303 case DIF_VAR_USTACKDEPTH: 3304 if (!dtrace_priv_proc(state)) 3305 return (0); 3306 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3307 /* 3308 * See comment in DIF_VAR_PID. 3309 */ 3310 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3311 CPU_ON_INTR(CPU)) { 3312 mstate->dtms_ustackdepth = 0; 3313 } else { 3314 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3315 mstate->dtms_ustackdepth = 3316 dtrace_getustackdepth(); 3317 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3318 } 3319 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3320 } 3321 return (mstate->dtms_ustackdepth); 3322 3323 case DIF_VAR_CALLER: 3324 if (!dtrace_priv_kernel(state)) 3325 return (0); 3326 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3327 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3328 3329 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3330 /* 3331 * If this is an unanchored probe, we are 3332 * required to go through the slow path: 3333 * dtrace_caller() only guarantees correct 3334 * results for anchored probes. 3335 */ 3336 pc_t caller[2] = {0, 0}; 3337 3338 dtrace_getpcstack(caller, 2, aframes, 3339 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3340 mstate->dtms_caller = caller[1]; 3341 } else if ((mstate->dtms_caller = 3342 dtrace_caller(aframes)) == -1) { 3343 /* 3344 * We have failed to do this the quick way; 3345 * we must resort to the slower approach of 3346 * calling dtrace_getpcstack(). 3347 */ 3348 pc_t caller = 0; 3349 3350 dtrace_getpcstack(&caller, 1, aframes, NULL); 3351 mstate->dtms_caller = caller; 3352 } 3353 3354 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3355 } 3356 return (mstate->dtms_caller); 3357 3358 case DIF_VAR_UCALLER: 3359 if (!dtrace_priv_proc(state)) 3360 return (0); 3361 3362 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3363 uint64_t ustack[3]; 3364 3365 /* 3366 * dtrace_getupcstack() fills in the first uint64_t 3367 * with the current PID. The second uint64_t will 3368 * be the program counter at user-level. The third 3369 * uint64_t will contain the caller, which is what 3370 * we're after. 3371 */ 3372 ustack[2] = 0; 3373 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3374 dtrace_getupcstack(ustack, 3); 3375 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3376 mstate->dtms_ucaller = ustack[2]; 3377 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3378 } 3379 3380 return (mstate->dtms_ucaller); 3381 3382 case DIF_VAR_PROBEPROV: 3383 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3384 return (dtrace_dif_varstr( 3385 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3386 state, mstate)); 3387 3388 case DIF_VAR_PROBEMOD: 3389 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3390 return (dtrace_dif_varstr( 3391 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3392 state, mstate)); 3393 3394 case DIF_VAR_PROBEFUNC: 3395 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3396 return (dtrace_dif_varstr( 3397 (uintptr_t)mstate->dtms_probe->dtpr_func, 3398 state, mstate)); 3399 3400 case DIF_VAR_PROBENAME: 3401 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3402 return (dtrace_dif_varstr( 3403 (uintptr_t)mstate->dtms_probe->dtpr_name, 3404 state, mstate)); 3405 3406 case DIF_VAR_PID: 3407 if (!dtrace_priv_proc(state)) 3408 return (0); 3409 3410 #ifdef illumos 3411 /* 3412 * Note that we are assuming that an unanchored probe is 3413 * always due to a high-level interrupt. (And we're assuming 3414 * that there is only a single high level interrupt.) 3415 */ 3416 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3417 return (pid0.pid_id); 3418 3419 /* 3420 * It is always safe to dereference one's own t_procp pointer: 3421 * it always points to a valid, allocated proc structure. 3422 * Further, it is always safe to dereference the p_pidp member 3423 * of one's own proc structure. (These are truisms becuase 3424 * threads and processes don't clean up their own state -- 3425 * they leave that task to whomever reaps them.) 3426 */ 3427 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3428 #else 3429 return ((uint64_t)curproc->p_pid); 3430 #endif 3431 3432 case DIF_VAR_PPID: 3433 if (!dtrace_priv_proc(state)) 3434 return (0); 3435 3436 #ifdef illumos 3437 /* 3438 * See comment in DIF_VAR_PID. 3439 */ 3440 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3441 return (pid0.pid_id); 3442 3443 /* 3444 * It is always safe to dereference one's own t_procp pointer: 3445 * it always points to a valid, allocated proc structure. 3446 * (This is true because threads don't clean up their own 3447 * state -- they leave that task to whomever reaps them.) 3448 */ 3449 return ((uint64_t)curthread->t_procp->p_ppid); 3450 #else 3451 if (curproc->p_pid == proc0.p_pid) 3452 return (curproc->p_pid); 3453 else 3454 return (curproc->p_pptr->p_pid); 3455 #endif 3456 3457 case DIF_VAR_TID: 3458 #ifdef illumos 3459 /* 3460 * See comment in DIF_VAR_PID. 3461 */ 3462 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3463 return (0); 3464 #endif 3465 3466 return ((uint64_t)curthread->t_tid); 3467 3468 case DIF_VAR_EXECARGS: { 3469 struct pargs *p_args = curthread->td_proc->p_args; 3470 3471 if (p_args == NULL) 3472 return(0); 3473 3474 return (dtrace_dif_varstrz( 3475 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3476 } 3477 3478 case DIF_VAR_EXECNAME: 3479 #ifdef illumos 3480 if (!dtrace_priv_proc(state)) 3481 return (0); 3482 3483 /* 3484 * See comment in DIF_VAR_PID. 3485 */ 3486 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3487 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3488 3489 /* 3490 * It is always safe to dereference one's own t_procp pointer: 3491 * it always points to a valid, allocated proc structure. 3492 * (This is true because threads don't clean up their own 3493 * state -- they leave that task to whomever reaps them.) 3494 */ 3495 return (dtrace_dif_varstr( 3496 (uintptr_t)curthread->t_procp->p_user.u_comm, 3497 state, mstate)); 3498 #else 3499 return (dtrace_dif_varstr( 3500 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3501 #endif 3502 3503 case DIF_VAR_ZONENAME: 3504 #ifdef illumos 3505 if (!dtrace_priv_proc(state)) 3506 return (0); 3507 3508 /* 3509 * See comment in DIF_VAR_PID. 3510 */ 3511 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3512 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3513 3514 /* 3515 * It is always safe to dereference one's own t_procp pointer: 3516 * it always points to a valid, allocated proc structure. 3517 * (This is true because threads don't clean up their own 3518 * state -- they leave that task to whomever reaps them.) 3519 */ 3520 return (dtrace_dif_varstr( 3521 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3522 state, mstate)); 3523 #else 3524 return (0); 3525 #endif 3526 3527 case DIF_VAR_UID: 3528 if (!dtrace_priv_proc(state)) 3529 return (0); 3530 3531 #ifdef illumos 3532 /* 3533 * See comment in DIF_VAR_PID. 3534 */ 3535 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3536 return ((uint64_t)p0.p_cred->cr_uid); 3537 3538 /* 3539 * It is always safe to dereference one's own t_procp pointer: 3540 * it always points to a valid, allocated proc structure. 3541 * (This is true because threads don't clean up their own 3542 * state -- they leave that task to whomever reaps them.) 3543 * 3544 * Additionally, it is safe to dereference one's own process 3545 * credential, since this is never NULL after process birth. 3546 */ 3547 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3548 #else 3549 return ((uint64_t)curthread->td_ucred->cr_uid); 3550 #endif 3551 3552 case DIF_VAR_GID: 3553 if (!dtrace_priv_proc(state)) 3554 return (0); 3555 3556 #ifdef illumos 3557 /* 3558 * See comment in DIF_VAR_PID. 3559 */ 3560 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3561 return ((uint64_t)p0.p_cred->cr_gid); 3562 3563 /* 3564 * It is always safe to dereference one's own t_procp pointer: 3565 * it always points to a valid, allocated proc structure. 3566 * (This is true because threads don't clean up their own 3567 * state -- they leave that task to whomever reaps them.) 3568 * 3569 * Additionally, it is safe to dereference one's own process 3570 * credential, since this is never NULL after process birth. 3571 */ 3572 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3573 #else 3574 return ((uint64_t)curthread->td_ucred->cr_gid); 3575 #endif 3576 3577 case DIF_VAR_ERRNO: { 3578 #ifdef illumos 3579 klwp_t *lwp; 3580 if (!dtrace_priv_proc(state)) 3581 return (0); 3582 3583 /* 3584 * See comment in DIF_VAR_PID. 3585 */ 3586 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3587 return (0); 3588 3589 /* 3590 * It is always safe to dereference one's own t_lwp pointer in 3591 * the event that this pointer is non-NULL. (This is true 3592 * because threads and lwps don't clean up their own state -- 3593 * they leave that task to whomever reaps them.) 3594 */ 3595 if ((lwp = curthread->t_lwp) == NULL) 3596 return (0); 3597 3598 return ((uint64_t)lwp->lwp_errno); 3599 #else 3600 return (curthread->td_errno); 3601 #endif 3602 } 3603 #ifndef illumos 3604 case DIF_VAR_CPU: { 3605 return curcpu; 3606 } 3607 #endif 3608 default: 3609 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3610 return (0); 3611 } 3612 } 3613 3614 3615 typedef enum dtrace_json_state { 3616 DTRACE_JSON_REST = 1, 3617 DTRACE_JSON_OBJECT, 3618 DTRACE_JSON_STRING, 3619 DTRACE_JSON_STRING_ESCAPE, 3620 DTRACE_JSON_STRING_ESCAPE_UNICODE, 3621 DTRACE_JSON_COLON, 3622 DTRACE_JSON_COMMA, 3623 DTRACE_JSON_VALUE, 3624 DTRACE_JSON_IDENTIFIER, 3625 DTRACE_JSON_NUMBER, 3626 DTRACE_JSON_NUMBER_FRAC, 3627 DTRACE_JSON_NUMBER_EXP, 3628 DTRACE_JSON_COLLECT_OBJECT 3629 } dtrace_json_state_t; 3630 3631 /* 3632 * This function possesses just enough knowledge about JSON to extract a single 3633 * value from a JSON string and store it in the scratch buffer. It is able 3634 * to extract nested object values, and members of arrays by index. 3635 * 3636 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to 3637 * be looked up as we descend into the object tree. e.g. 3638 * 3639 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL 3640 * with nelems = 5. 3641 * 3642 * The run time of this function must be bounded above by strsize to limit the 3643 * amount of work done in probe context. As such, it is implemented as a 3644 * simple state machine, reading one character at a time using safe loads 3645 * until we find the requested element, hit a parsing error or run off the 3646 * end of the object or string. 3647 * 3648 * As there is no way for a subroutine to return an error without interrupting 3649 * clause execution, we simply return NULL in the event of a missing key or any 3650 * other error condition. Each NULL return in this function is commented with 3651 * the error condition it represents -- parsing or otherwise. 3652 * 3653 * The set of states for the state machine closely matches the JSON 3654 * specification (http://json.org/). Briefly: 3655 * 3656 * DTRACE_JSON_REST: 3657 * Skip whitespace until we find either a top-level Object, moving 3658 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE. 3659 * 3660 * DTRACE_JSON_OBJECT: 3661 * Locate the next key String in an Object. Sets a flag to denote 3662 * the next String as a key string and moves to DTRACE_JSON_STRING. 3663 * 3664 * DTRACE_JSON_COLON: 3665 * Skip whitespace until we find the colon that separates key Strings 3666 * from their values. Once found, move to DTRACE_JSON_VALUE. 3667 * 3668 * DTRACE_JSON_VALUE: 3669 * Detects the type of the next value (String, Number, Identifier, Object 3670 * or Array) and routes to the states that process that type. Here we also 3671 * deal with the element selector list if we are requested to traverse down 3672 * into the object tree. 3673 * 3674 * DTRACE_JSON_COMMA: 3675 * Skip whitespace until we find the comma that separates key-value pairs 3676 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays 3677 * (similarly DTRACE_JSON_VALUE). All following literal value processing 3678 * states return to this state at the end of their value, unless otherwise 3679 * noted. 3680 * 3681 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP: 3682 * Processes a Number literal from the JSON, including any exponent 3683 * component that may be present. Numbers are returned as strings, which 3684 * may be passed to strtoll() if an integer is required. 3685 * 3686 * DTRACE_JSON_IDENTIFIER: 3687 * Processes a "true", "false" or "null" literal in the JSON. 3688 * 3689 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE, 3690 * DTRACE_JSON_STRING_ESCAPE_UNICODE: 3691 * Processes a String literal from the JSON, whether the String denotes 3692 * a key, a value or part of a larger Object. Handles all escape sequences 3693 * present in the specification, including four-digit unicode characters, 3694 * but merely includes the escape sequence without converting it to the 3695 * actual escaped character. If the String is flagged as a key, we 3696 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA. 3697 * 3698 * DTRACE_JSON_COLLECT_OBJECT: 3699 * This state collects an entire Object (or Array), correctly handling 3700 * embedded strings. If the full element selector list matches this nested 3701 * object, we return the Object in full as a string. If not, we use this 3702 * state to skip to the next value at this level and continue processing. 3703 * 3704 * NOTE: This function uses various macros from strtolctype.h to manipulate 3705 * digit values, etc -- these have all been checked to ensure they make 3706 * no additional function calls. 3707 */ 3708 static char * 3709 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems, 3710 char *dest) 3711 { 3712 dtrace_json_state_t state = DTRACE_JSON_REST; 3713 int64_t array_elem = INT64_MIN; 3714 int64_t array_pos = 0; 3715 uint8_t escape_unicount = 0; 3716 boolean_t string_is_key = B_FALSE; 3717 boolean_t collect_object = B_FALSE; 3718 boolean_t found_key = B_FALSE; 3719 boolean_t in_array = B_FALSE; 3720 uint32_t braces = 0, brackets = 0; 3721 char *elem = elemlist; 3722 char *dd = dest; 3723 uintptr_t cur; 3724 3725 for (cur = json; cur < json + size; cur++) { 3726 char cc = dtrace_load8(cur); 3727 if (cc == '\0') 3728 return (NULL); 3729 3730 switch (state) { 3731 case DTRACE_JSON_REST: 3732 if (isspace(cc)) 3733 break; 3734 3735 if (cc == '{') { 3736 state = DTRACE_JSON_OBJECT; 3737 break; 3738 } 3739 3740 if (cc == '[') { 3741 in_array = B_TRUE; 3742 array_pos = 0; 3743 array_elem = dtrace_strtoll(elem, 10, size); 3744 found_key = array_elem == 0 ? B_TRUE : B_FALSE; 3745 state = DTRACE_JSON_VALUE; 3746 break; 3747 } 3748 3749 /* 3750 * ERROR: expected to find a top-level object or array. 3751 */ 3752 return (NULL); 3753 case DTRACE_JSON_OBJECT: 3754 if (isspace(cc)) 3755 break; 3756 3757 if (cc == '"') { 3758 state = DTRACE_JSON_STRING; 3759 string_is_key = B_TRUE; 3760 break; 3761 } 3762 3763 /* 3764 * ERROR: either the object did not start with a key 3765 * string, or we've run off the end of the object 3766 * without finding the requested key. 3767 */ 3768 return (NULL); 3769 case DTRACE_JSON_STRING: 3770 if (cc == '\\') { 3771 *dd++ = '\\'; 3772 state = DTRACE_JSON_STRING_ESCAPE; 3773 break; 3774 } 3775 3776 if (cc == '"') { 3777 if (collect_object) { 3778 /* 3779 * We don't reset the dest here, as 3780 * the string is part of a larger 3781 * object being collected. 3782 */ 3783 *dd++ = cc; 3784 collect_object = B_FALSE; 3785 state = DTRACE_JSON_COLLECT_OBJECT; 3786 break; 3787 } 3788 *dd = '\0'; 3789 dd = dest; /* reset string buffer */ 3790 if (string_is_key) { 3791 if (dtrace_strncmp(dest, elem, 3792 size) == 0) 3793 found_key = B_TRUE; 3794 } else if (found_key) { 3795 if (nelems > 1) { 3796 /* 3797 * We expected an object, not 3798 * this string. 3799 */ 3800 return (NULL); 3801 } 3802 return (dest); 3803 } 3804 state = string_is_key ? DTRACE_JSON_COLON : 3805 DTRACE_JSON_COMMA; 3806 string_is_key = B_FALSE; 3807 break; 3808 } 3809 3810 *dd++ = cc; 3811 break; 3812 case DTRACE_JSON_STRING_ESCAPE: 3813 *dd++ = cc; 3814 if (cc == 'u') { 3815 escape_unicount = 0; 3816 state = DTRACE_JSON_STRING_ESCAPE_UNICODE; 3817 } else { 3818 state = DTRACE_JSON_STRING; 3819 } 3820 break; 3821 case DTRACE_JSON_STRING_ESCAPE_UNICODE: 3822 if (!isxdigit(cc)) { 3823 /* 3824 * ERROR: invalid unicode escape, expected 3825 * four valid hexidecimal digits. 3826 */ 3827 return (NULL); 3828 } 3829 3830 *dd++ = cc; 3831 if (++escape_unicount == 4) 3832 state = DTRACE_JSON_STRING; 3833 break; 3834 case DTRACE_JSON_COLON: 3835 if (isspace(cc)) 3836 break; 3837 3838 if (cc == ':') { 3839 state = DTRACE_JSON_VALUE; 3840 break; 3841 } 3842 3843 /* 3844 * ERROR: expected a colon. 3845 */ 3846 return (NULL); 3847 case DTRACE_JSON_COMMA: 3848 if (isspace(cc)) 3849 break; 3850 3851 if (cc == ',') { 3852 if (in_array) { 3853 state = DTRACE_JSON_VALUE; 3854 if (++array_pos == array_elem) 3855 found_key = B_TRUE; 3856 } else { 3857 state = DTRACE_JSON_OBJECT; 3858 } 3859 break; 3860 } 3861 3862 /* 3863 * ERROR: either we hit an unexpected character, or 3864 * we reached the end of the object or array without 3865 * finding the requested key. 3866 */ 3867 return (NULL); 3868 case DTRACE_JSON_IDENTIFIER: 3869 if (islower(cc)) { 3870 *dd++ = cc; 3871 break; 3872 } 3873 3874 *dd = '\0'; 3875 dd = dest; /* reset string buffer */ 3876 3877 if (dtrace_strncmp(dest, "true", 5) == 0 || 3878 dtrace_strncmp(dest, "false", 6) == 0 || 3879 dtrace_strncmp(dest, "null", 5) == 0) { 3880 if (found_key) { 3881 if (nelems > 1) { 3882 /* 3883 * ERROR: We expected an object, 3884 * not this identifier. 3885 */ 3886 return (NULL); 3887 } 3888 return (dest); 3889 } else { 3890 cur--; 3891 state = DTRACE_JSON_COMMA; 3892 break; 3893 } 3894 } 3895 3896 /* 3897 * ERROR: we did not recognise the identifier as one 3898 * of those in the JSON specification. 3899 */ 3900 return (NULL); 3901 case DTRACE_JSON_NUMBER: 3902 if (cc == '.') { 3903 *dd++ = cc; 3904 state = DTRACE_JSON_NUMBER_FRAC; 3905 break; 3906 } 3907 3908 if (cc == 'x' || cc == 'X') { 3909 /* 3910 * ERROR: specification explicitly excludes 3911 * hexidecimal or octal numbers. 3912 */ 3913 return (NULL); 3914 } 3915 3916 /* FALLTHRU */ 3917 case DTRACE_JSON_NUMBER_FRAC: 3918 if (cc == 'e' || cc == 'E') { 3919 *dd++ = cc; 3920 state = DTRACE_JSON_NUMBER_EXP; 3921 break; 3922 } 3923 3924 if (cc == '+' || cc == '-') { 3925 /* 3926 * ERROR: expect sign as part of exponent only. 3927 */ 3928 return (NULL); 3929 } 3930 /* FALLTHRU */ 3931 case DTRACE_JSON_NUMBER_EXP: 3932 if (isdigit(cc) || cc == '+' || cc == '-') { 3933 *dd++ = cc; 3934 break; 3935 } 3936 3937 *dd = '\0'; 3938 dd = dest; /* reset string buffer */ 3939 if (found_key) { 3940 if (nelems > 1) { 3941 /* 3942 * ERROR: We expected an object, not 3943 * this number. 3944 */ 3945 return (NULL); 3946 } 3947 return (dest); 3948 } 3949 3950 cur--; 3951 state = DTRACE_JSON_COMMA; 3952 break; 3953 case DTRACE_JSON_VALUE: 3954 if (isspace(cc)) 3955 break; 3956 3957 if (cc == '{' || cc == '[') { 3958 if (nelems > 1 && found_key) { 3959 in_array = cc == '[' ? B_TRUE : B_FALSE; 3960 /* 3961 * If our element selector directs us 3962 * to descend into this nested object, 3963 * then move to the next selector 3964 * element in the list and restart the 3965 * state machine. 3966 */ 3967 while (*elem != '\0') 3968 elem++; 3969 elem++; /* skip the inter-element NUL */ 3970 nelems--; 3971 dd = dest; 3972 if (in_array) { 3973 state = DTRACE_JSON_VALUE; 3974 array_pos = 0; 3975 array_elem = dtrace_strtoll( 3976 elem, 10, size); 3977 found_key = array_elem == 0 ? 3978 B_TRUE : B_FALSE; 3979 } else { 3980 found_key = B_FALSE; 3981 state = DTRACE_JSON_OBJECT; 3982 } 3983 break; 3984 } 3985 3986 /* 3987 * Otherwise, we wish to either skip this 3988 * nested object or return it in full. 3989 */ 3990 if (cc == '[') 3991 brackets = 1; 3992 else 3993 braces = 1; 3994 *dd++ = cc; 3995 state = DTRACE_JSON_COLLECT_OBJECT; 3996 break; 3997 } 3998 3999 if (cc == '"') { 4000 state = DTRACE_JSON_STRING; 4001 break; 4002 } 4003 4004 if (islower(cc)) { 4005 /* 4006 * Here we deal with true, false and null. 4007 */ 4008 *dd++ = cc; 4009 state = DTRACE_JSON_IDENTIFIER; 4010 break; 4011 } 4012 4013 if (cc == '-' || isdigit(cc)) { 4014 *dd++ = cc; 4015 state = DTRACE_JSON_NUMBER; 4016 break; 4017 } 4018 4019 /* 4020 * ERROR: unexpected character at start of value. 4021 */ 4022 return (NULL); 4023 case DTRACE_JSON_COLLECT_OBJECT: 4024 if (cc == '\0') 4025 /* 4026 * ERROR: unexpected end of input. 4027 */ 4028 return (NULL); 4029 4030 *dd++ = cc; 4031 if (cc == '"') { 4032 collect_object = B_TRUE; 4033 state = DTRACE_JSON_STRING; 4034 break; 4035 } 4036 4037 if (cc == ']') { 4038 if (brackets-- == 0) { 4039 /* 4040 * ERROR: unbalanced brackets. 4041 */ 4042 return (NULL); 4043 } 4044 } else if (cc == '}') { 4045 if (braces-- == 0) { 4046 /* 4047 * ERROR: unbalanced braces. 4048 */ 4049 return (NULL); 4050 } 4051 } else if (cc == '{') { 4052 braces++; 4053 } else if (cc == '[') { 4054 brackets++; 4055 } 4056 4057 if (brackets == 0 && braces == 0) { 4058 if (found_key) { 4059 *dd = '\0'; 4060 return (dest); 4061 } 4062 dd = dest; /* reset string buffer */ 4063 state = DTRACE_JSON_COMMA; 4064 } 4065 break; 4066 } 4067 } 4068 return (NULL); 4069 } 4070 4071 /* 4072 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 4073 * Notice that we don't bother validating the proper number of arguments or 4074 * their types in the tuple stack. This isn't needed because all argument 4075 * interpretation is safe because of our load safety -- the worst that can 4076 * happen is that a bogus program can obtain bogus results. 4077 */ 4078 static void 4079 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 4080 dtrace_key_t *tupregs, int nargs, 4081 dtrace_mstate_t *mstate, dtrace_state_t *state) 4082 { 4083 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4084 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4085 dtrace_vstate_t *vstate = &state->dts_vstate; 4086 4087 #ifdef illumos 4088 union { 4089 mutex_impl_t mi; 4090 uint64_t mx; 4091 } m; 4092 4093 union { 4094 krwlock_t ri; 4095 uintptr_t rw; 4096 } r; 4097 #else 4098 struct thread *lowner; 4099 union { 4100 struct lock_object *li; 4101 uintptr_t lx; 4102 } l; 4103 #endif 4104 4105 switch (subr) { 4106 case DIF_SUBR_RAND: 4107 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 4108 break; 4109 4110 #ifdef illumos 4111 case DIF_SUBR_MUTEX_OWNED: 4112 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4113 mstate, vstate)) { 4114 regs[rd] = 0; 4115 break; 4116 } 4117 4118 m.mx = dtrace_load64(tupregs[0].dttk_value); 4119 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 4120 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 4121 else 4122 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 4123 break; 4124 4125 case DIF_SUBR_MUTEX_OWNER: 4126 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4127 mstate, vstate)) { 4128 regs[rd] = 0; 4129 break; 4130 } 4131 4132 m.mx = dtrace_load64(tupregs[0].dttk_value); 4133 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 4134 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 4135 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 4136 else 4137 regs[rd] = 0; 4138 break; 4139 4140 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4141 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4142 mstate, vstate)) { 4143 regs[rd] = 0; 4144 break; 4145 } 4146 4147 m.mx = dtrace_load64(tupregs[0].dttk_value); 4148 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 4149 break; 4150 4151 case DIF_SUBR_MUTEX_TYPE_SPIN: 4152 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4153 mstate, vstate)) { 4154 regs[rd] = 0; 4155 break; 4156 } 4157 4158 m.mx = dtrace_load64(tupregs[0].dttk_value); 4159 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 4160 break; 4161 4162 case DIF_SUBR_RW_READ_HELD: { 4163 uintptr_t tmp; 4164 4165 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4166 mstate, vstate)) { 4167 regs[rd] = 0; 4168 break; 4169 } 4170 4171 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4172 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 4173 break; 4174 } 4175 4176 case DIF_SUBR_RW_WRITE_HELD: 4177 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4178 mstate, vstate)) { 4179 regs[rd] = 0; 4180 break; 4181 } 4182 4183 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4184 regs[rd] = _RW_WRITE_HELD(&r.ri); 4185 break; 4186 4187 case DIF_SUBR_RW_ISWRITER: 4188 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4189 mstate, vstate)) { 4190 regs[rd] = 0; 4191 break; 4192 } 4193 4194 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4195 regs[rd] = _RW_ISWRITER(&r.ri); 4196 break; 4197 4198 #else /* !illumos */ 4199 case DIF_SUBR_MUTEX_OWNED: 4200 if (!dtrace_canload(tupregs[0].dttk_value, 4201 sizeof (struct lock_object), mstate, vstate)) { 4202 regs[rd] = 0; 4203 break; 4204 } 4205 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4206 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4207 break; 4208 4209 case DIF_SUBR_MUTEX_OWNER: 4210 if (!dtrace_canload(tupregs[0].dttk_value, 4211 sizeof (struct lock_object), mstate, vstate)) { 4212 regs[rd] = 0; 4213 break; 4214 } 4215 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4216 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4217 regs[rd] = (uintptr_t)lowner; 4218 break; 4219 4220 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4221 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4222 mstate, vstate)) { 4223 regs[rd] = 0; 4224 break; 4225 } 4226 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4227 /* XXX - should be only LC_SLEEPABLE? */ 4228 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & 4229 (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0; 4230 break; 4231 4232 case DIF_SUBR_MUTEX_TYPE_SPIN: 4233 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4234 mstate, vstate)) { 4235 regs[rd] = 0; 4236 break; 4237 } 4238 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4239 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 4240 break; 4241 4242 case DIF_SUBR_RW_READ_HELD: 4243 case DIF_SUBR_SX_SHARED_HELD: 4244 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4245 mstate, vstate)) { 4246 regs[rd] = 0; 4247 break; 4248 } 4249 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4250 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4251 lowner == NULL; 4252 break; 4253 4254 case DIF_SUBR_RW_WRITE_HELD: 4255 case DIF_SUBR_SX_EXCLUSIVE_HELD: 4256 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4257 mstate, vstate)) { 4258 regs[rd] = 0; 4259 break; 4260 } 4261 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4262 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4263 regs[rd] = (lowner == curthread); 4264 break; 4265 4266 case DIF_SUBR_RW_ISWRITER: 4267 case DIF_SUBR_SX_ISEXCLUSIVE: 4268 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4269 mstate, vstate)) { 4270 regs[rd] = 0; 4271 break; 4272 } 4273 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4274 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4275 lowner != NULL; 4276 break; 4277 #endif /* illumos */ 4278 4279 case DIF_SUBR_BCOPY: { 4280 /* 4281 * We need to be sure that the destination is in the scratch 4282 * region -- no other region is allowed. 4283 */ 4284 uintptr_t src = tupregs[0].dttk_value; 4285 uintptr_t dest = tupregs[1].dttk_value; 4286 size_t size = tupregs[2].dttk_value; 4287 4288 if (!dtrace_inscratch(dest, size, mstate)) { 4289 *flags |= CPU_DTRACE_BADADDR; 4290 *illval = regs[rd]; 4291 break; 4292 } 4293 4294 if (!dtrace_canload(src, size, mstate, vstate)) { 4295 regs[rd] = 0; 4296 break; 4297 } 4298 4299 dtrace_bcopy((void *)src, (void *)dest, size); 4300 break; 4301 } 4302 4303 case DIF_SUBR_ALLOCA: 4304 case DIF_SUBR_COPYIN: { 4305 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4306 uint64_t size = 4307 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 4308 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 4309 4310 /* 4311 * This action doesn't require any credential checks since 4312 * probes will not activate in user contexts to which the 4313 * enabling user does not have permissions. 4314 */ 4315 4316 /* 4317 * Rounding up the user allocation size could have overflowed 4318 * a large, bogus allocation (like -1ULL) to 0. 4319 */ 4320 if (scratch_size < size || 4321 !DTRACE_INSCRATCH(mstate, scratch_size)) { 4322 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4323 regs[rd] = 0; 4324 break; 4325 } 4326 4327 if (subr == DIF_SUBR_COPYIN) { 4328 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4329 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4330 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4331 } 4332 4333 mstate->dtms_scratch_ptr += scratch_size; 4334 regs[rd] = dest; 4335 break; 4336 } 4337 4338 case DIF_SUBR_COPYINTO: { 4339 uint64_t size = tupregs[1].dttk_value; 4340 uintptr_t dest = tupregs[2].dttk_value; 4341 4342 /* 4343 * This action doesn't require any credential checks since 4344 * probes will not activate in user contexts to which the 4345 * enabling user does not have permissions. 4346 */ 4347 if (!dtrace_inscratch(dest, size, mstate)) { 4348 *flags |= CPU_DTRACE_BADADDR; 4349 *illval = regs[rd]; 4350 break; 4351 } 4352 4353 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4354 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4355 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4356 break; 4357 } 4358 4359 case DIF_SUBR_COPYINSTR: { 4360 uintptr_t dest = mstate->dtms_scratch_ptr; 4361 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4362 4363 if (nargs > 1 && tupregs[1].dttk_value < size) 4364 size = tupregs[1].dttk_value + 1; 4365 4366 /* 4367 * This action doesn't require any credential checks since 4368 * probes will not activate in user contexts to which the 4369 * enabling user does not have permissions. 4370 */ 4371 if (!DTRACE_INSCRATCH(mstate, size)) { 4372 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4373 regs[rd] = 0; 4374 break; 4375 } 4376 4377 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4378 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 4379 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4380 4381 ((char *)dest)[size - 1] = '\0'; 4382 mstate->dtms_scratch_ptr += size; 4383 regs[rd] = dest; 4384 break; 4385 } 4386 4387 #ifdef illumos 4388 case DIF_SUBR_MSGSIZE: 4389 case DIF_SUBR_MSGDSIZE: { 4390 uintptr_t baddr = tupregs[0].dttk_value, daddr; 4391 uintptr_t wptr, rptr; 4392 size_t count = 0; 4393 int cont = 0; 4394 4395 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 4396 4397 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 4398 vstate)) { 4399 regs[rd] = 0; 4400 break; 4401 } 4402 4403 wptr = dtrace_loadptr(baddr + 4404 offsetof(mblk_t, b_wptr)); 4405 4406 rptr = dtrace_loadptr(baddr + 4407 offsetof(mblk_t, b_rptr)); 4408 4409 if (wptr < rptr) { 4410 *flags |= CPU_DTRACE_BADADDR; 4411 *illval = tupregs[0].dttk_value; 4412 break; 4413 } 4414 4415 daddr = dtrace_loadptr(baddr + 4416 offsetof(mblk_t, b_datap)); 4417 4418 baddr = dtrace_loadptr(baddr + 4419 offsetof(mblk_t, b_cont)); 4420 4421 /* 4422 * We want to prevent against denial-of-service here, 4423 * so we're only going to search the list for 4424 * dtrace_msgdsize_max mblks. 4425 */ 4426 if (cont++ > dtrace_msgdsize_max) { 4427 *flags |= CPU_DTRACE_ILLOP; 4428 break; 4429 } 4430 4431 if (subr == DIF_SUBR_MSGDSIZE) { 4432 if (dtrace_load8(daddr + 4433 offsetof(dblk_t, db_type)) != M_DATA) 4434 continue; 4435 } 4436 4437 count += wptr - rptr; 4438 } 4439 4440 if (!(*flags & CPU_DTRACE_FAULT)) 4441 regs[rd] = count; 4442 4443 break; 4444 } 4445 #endif 4446 4447 case DIF_SUBR_PROGENYOF: { 4448 pid_t pid = tupregs[0].dttk_value; 4449 proc_t *p; 4450 int rval = 0; 4451 4452 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4453 4454 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 4455 #ifdef illumos 4456 if (p->p_pidp->pid_id == pid) { 4457 #else 4458 if (p->p_pid == pid) { 4459 #endif 4460 rval = 1; 4461 break; 4462 } 4463 } 4464 4465 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4466 4467 regs[rd] = rval; 4468 break; 4469 } 4470 4471 case DIF_SUBR_SPECULATION: 4472 regs[rd] = dtrace_speculation(state); 4473 break; 4474 4475 case DIF_SUBR_COPYOUT: { 4476 uintptr_t kaddr = tupregs[0].dttk_value; 4477 uintptr_t uaddr = tupregs[1].dttk_value; 4478 uint64_t size = tupregs[2].dttk_value; 4479 4480 if (!dtrace_destructive_disallow && 4481 dtrace_priv_proc_control(state) && 4482 !dtrace_istoxic(kaddr, size) && 4483 dtrace_canload(kaddr, size, mstate, vstate)) { 4484 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4485 dtrace_copyout(kaddr, uaddr, size, flags); 4486 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4487 } 4488 break; 4489 } 4490 4491 case DIF_SUBR_COPYOUTSTR: { 4492 uintptr_t kaddr = tupregs[0].dttk_value; 4493 uintptr_t uaddr = tupregs[1].dttk_value; 4494 uint64_t size = tupregs[2].dttk_value; 4495 4496 if (!dtrace_destructive_disallow && 4497 dtrace_priv_proc_control(state) && 4498 !dtrace_istoxic(kaddr, size) && 4499 dtrace_strcanload(kaddr, size, mstate, vstate)) { 4500 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4501 dtrace_copyoutstr(kaddr, uaddr, size, flags); 4502 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4503 } 4504 break; 4505 } 4506 4507 case DIF_SUBR_STRLEN: { 4508 size_t sz; 4509 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 4510 sz = dtrace_strlen((char *)addr, 4511 state->dts_options[DTRACEOPT_STRSIZE]); 4512 4513 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 4514 regs[rd] = 0; 4515 break; 4516 } 4517 4518 regs[rd] = sz; 4519 4520 break; 4521 } 4522 4523 case DIF_SUBR_STRCHR: 4524 case DIF_SUBR_STRRCHR: { 4525 /* 4526 * We're going to iterate over the string looking for the 4527 * specified character. We will iterate until we have reached 4528 * the string length or we have found the character. If this 4529 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 4530 * of the specified character instead of the first. 4531 */ 4532 uintptr_t saddr = tupregs[0].dttk_value; 4533 uintptr_t addr = tupregs[0].dttk_value; 4534 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 4535 char c, target = (char)tupregs[1].dttk_value; 4536 4537 for (regs[rd] = 0; addr < limit; addr++) { 4538 if ((c = dtrace_load8(addr)) == target) { 4539 regs[rd] = addr; 4540 4541 if (subr == DIF_SUBR_STRCHR) 4542 break; 4543 } 4544 4545 if (c == '\0') 4546 break; 4547 } 4548 4549 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 4550 regs[rd] = 0; 4551 break; 4552 } 4553 4554 break; 4555 } 4556 4557 case DIF_SUBR_STRSTR: 4558 case DIF_SUBR_INDEX: 4559 case DIF_SUBR_RINDEX: { 4560 /* 4561 * We're going to iterate over the string looking for the 4562 * specified string. We will iterate until we have reached 4563 * the string length or we have found the string. (Yes, this 4564 * is done in the most naive way possible -- but considering 4565 * that the string we're searching for is likely to be 4566 * relatively short, the complexity of Rabin-Karp or similar 4567 * hardly seems merited.) 4568 */ 4569 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 4570 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 4571 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4572 size_t len = dtrace_strlen(addr, size); 4573 size_t sublen = dtrace_strlen(substr, size); 4574 char *limit = addr + len, *orig = addr; 4575 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 4576 int inc = 1; 4577 4578 regs[rd] = notfound; 4579 4580 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 4581 regs[rd] = 0; 4582 break; 4583 } 4584 4585 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 4586 vstate)) { 4587 regs[rd] = 0; 4588 break; 4589 } 4590 4591 /* 4592 * strstr() and index()/rindex() have similar semantics if 4593 * both strings are the empty string: strstr() returns a 4594 * pointer to the (empty) string, and index() and rindex() 4595 * both return index 0 (regardless of any position argument). 4596 */ 4597 if (sublen == 0 && len == 0) { 4598 if (subr == DIF_SUBR_STRSTR) 4599 regs[rd] = (uintptr_t)addr; 4600 else 4601 regs[rd] = 0; 4602 break; 4603 } 4604 4605 if (subr != DIF_SUBR_STRSTR) { 4606 if (subr == DIF_SUBR_RINDEX) { 4607 limit = orig - 1; 4608 addr += len; 4609 inc = -1; 4610 } 4611 4612 /* 4613 * Both index() and rindex() take an optional position 4614 * argument that denotes the starting position. 4615 */ 4616 if (nargs == 3) { 4617 int64_t pos = (int64_t)tupregs[2].dttk_value; 4618 4619 /* 4620 * If the position argument to index() is 4621 * negative, Perl implicitly clamps it at 4622 * zero. This semantic is a little surprising 4623 * given the special meaning of negative 4624 * positions to similar Perl functions like 4625 * substr(), but it appears to reflect a 4626 * notion that index() can start from a 4627 * negative index and increment its way up to 4628 * the string. Given this notion, Perl's 4629 * rindex() is at least self-consistent in 4630 * that it implicitly clamps positions greater 4631 * than the string length to be the string 4632 * length. Where Perl completely loses 4633 * coherence, however, is when the specified 4634 * substring is the empty string (""). In 4635 * this case, even if the position is 4636 * negative, rindex() returns 0 -- and even if 4637 * the position is greater than the length, 4638 * index() returns the string length. These 4639 * semantics violate the notion that index() 4640 * should never return a value less than the 4641 * specified position and that rindex() should 4642 * never return a value greater than the 4643 * specified position. (One assumes that 4644 * these semantics are artifacts of Perl's 4645 * implementation and not the results of 4646 * deliberate design -- it beggars belief that 4647 * even Larry Wall could desire such oddness.) 4648 * While in the abstract one would wish for 4649 * consistent position semantics across 4650 * substr(), index() and rindex() -- or at the 4651 * very least self-consistent position 4652 * semantics for index() and rindex() -- we 4653 * instead opt to keep with the extant Perl 4654 * semantics, in all their broken glory. (Do 4655 * we have more desire to maintain Perl's 4656 * semantics than Perl does? Probably.) 4657 */ 4658 if (subr == DIF_SUBR_RINDEX) { 4659 if (pos < 0) { 4660 if (sublen == 0) 4661 regs[rd] = 0; 4662 break; 4663 } 4664 4665 if (pos > len) 4666 pos = len; 4667 } else { 4668 if (pos < 0) 4669 pos = 0; 4670 4671 if (pos >= len) { 4672 if (sublen == 0) 4673 regs[rd] = len; 4674 break; 4675 } 4676 } 4677 4678 addr = orig + pos; 4679 } 4680 } 4681 4682 for (regs[rd] = notfound; addr != limit; addr += inc) { 4683 if (dtrace_strncmp(addr, substr, sublen) == 0) { 4684 if (subr != DIF_SUBR_STRSTR) { 4685 /* 4686 * As D index() and rindex() are 4687 * modeled on Perl (and not on awk), 4688 * we return a zero-based (and not a 4689 * one-based) index. (For you Perl 4690 * weenies: no, we're not going to add 4691 * $[ -- and shouldn't you be at a con 4692 * or something?) 4693 */ 4694 regs[rd] = (uintptr_t)(addr - orig); 4695 break; 4696 } 4697 4698 ASSERT(subr == DIF_SUBR_STRSTR); 4699 regs[rd] = (uintptr_t)addr; 4700 break; 4701 } 4702 } 4703 4704 break; 4705 } 4706 4707 case DIF_SUBR_STRTOK: { 4708 uintptr_t addr = tupregs[0].dttk_value; 4709 uintptr_t tokaddr = tupregs[1].dttk_value; 4710 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4711 uintptr_t limit, toklimit = tokaddr + size; 4712 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 4713 char *dest = (char *)mstate->dtms_scratch_ptr; 4714 int i; 4715 4716 /* 4717 * Check both the token buffer and (later) the input buffer, 4718 * since both could be non-scratch addresses. 4719 */ 4720 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 4721 regs[rd] = 0; 4722 break; 4723 } 4724 4725 if (!DTRACE_INSCRATCH(mstate, size)) { 4726 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4727 regs[rd] = 0; 4728 break; 4729 } 4730 4731 if (addr == 0) { 4732 /* 4733 * If the address specified is NULL, we use our saved 4734 * strtok pointer from the mstate. Note that this 4735 * means that the saved strtok pointer is _only_ 4736 * valid within multiple enablings of the same probe -- 4737 * it behaves like an implicit clause-local variable. 4738 */ 4739 addr = mstate->dtms_strtok; 4740 } else { 4741 /* 4742 * If the user-specified address is non-NULL we must 4743 * access check it. This is the only time we have 4744 * a chance to do so, since this address may reside 4745 * in the string table of this clause-- future calls 4746 * (when we fetch addr from mstate->dtms_strtok) 4747 * would fail this access check. 4748 */ 4749 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 4750 regs[rd] = 0; 4751 break; 4752 } 4753 } 4754 4755 /* 4756 * First, zero the token map, and then process the token 4757 * string -- setting a bit in the map for every character 4758 * found in the token string. 4759 */ 4760 for (i = 0; i < sizeof (tokmap); i++) 4761 tokmap[i] = 0; 4762 4763 for (; tokaddr < toklimit; tokaddr++) { 4764 if ((c = dtrace_load8(tokaddr)) == '\0') 4765 break; 4766 4767 ASSERT((c >> 3) < sizeof (tokmap)); 4768 tokmap[c >> 3] |= (1 << (c & 0x7)); 4769 } 4770 4771 for (limit = addr + size; addr < limit; addr++) { 4772 /* 4773 * We're looking for a character that is _not_ contained 4774 * in the token string. 4775 */ 4776 if ((c = dtrace_load8(addr)) == '\0') 4777 break; 4778 4779 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 4780 break; 4781 } 4782 4783 if (c == '\0') { 4784 /* 4785 * We reached the end of the string without finding 4786 * any character that was not in the token string. 4787 * We return NULL in this case, and we set the saved 4788 * address to NULL as well. 4789 */ 4790 regs[rd] = 0; 4791 mstate->dtms_strtok = 0; 4792 break; 4793 } 4794 4795 /* 4796 * From here on, we're copying into the destination string. 4797 */ 4798 for (i = 0; addr < limit && i < size - 1; addr++) { 4799 if ((c = dtrace_load8(addr)) == '\0') 4800 break; 4801 4802 if (tokmap[c >> 3] & (1 << (c & 0x7))) 4803 break; 4804 4805 ASSERT(i < size); 4806 dest[i++] = c; 4807 } 4808 4809 ASSERT(i < size); 4810 dest[i] = '\0'; 4811 regs[rd] = (uintptr_t)dest; 4812 mstate->dtms_scratch_ptr += size; 4813 mstate->dtms_strtok = addr; 4814 break; 4815 } 4816 4817 case DIF_SUBR_SUBSTR: { 4818 uintptr_t s = tupregs[0].dttk_value; 4819 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4820 char *d = (char *)mstate->dtms_scratch_ptr; 4821 int64_t index = (int64_t)tupregs[1].dttk_value; 4822 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4823 size_t len = dtrace_strlen((char *)s, size); 4824 int64_t i; 4825 4826 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4827 regs[rd] = 0; 4828 break; 4829 } 4830 4831 if (!DTRACE_INSCRATCH(mstate, size)) { 4832 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4833 regs[rd] = 0; 4834 break; 4835 } 4836 4837 if (nargs <= 2) 4838 remaining = (int64_t)size; 4839 4840 if (index < 0) { 4841 index += len; 4842 4843 if (index < 0 && index + remaining > 0) { 4844 remaining += index; 4845 index = 0; 4846 } 4847 } 4848 4849 if (index >= len || index < 0) { 4850 remaining = 0; 4851 } else if (remaining < 0) { 4852 remaining += len - index; 4853 } else if (index + remaining > size) { 4854 remaining = size - index; 4855 } 4856 4857 for (i = 0; i < remaining; i++) { 4858 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4859 break; 4860 } 4861 4862 d[i] = '\0'; 4863 4864 mstate->dtms_scratch_ptr += size; 4865 regs[rd] = (uintptr_t)d; 4866 break; 4867 } 4868 4869 case DIF_SUBR_JSON: { 4870 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4871 uintptr_t json = tupregs[0].dttk_value; 4872 size_t jsonlen = dtrace_strlen((char *)json, size); 4873 uintptr_t elem = tupregs[1].dttk_value; 4874 size_t elemlen = dtrace_strlen((char *)elem, size); 4875 4876 char *dest = (char *)mstate->dtms_scratch_ptr; 4877 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1; 4878 char *ee = elemlist; 4879 int nelems = 1; 4880 uintptr_t cur; 4881 4882 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) || 4883 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) { 4884 regs[rd] = 0; 4885 break; 4886 } 4887 4888 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) { 4889 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4890 regs[rd] = 0; 4891 break; 4892 } 4893 4894 /* 4895 * Read the element selector and split it up into a packed list 4896 * of strings. 4897 */ 4898 for (cur = elem; cur < elem + elemlen; cur++) { 4899 char cc = dtrace_load8(cur); 4900 4901 if (cur == elem && cc == '[') { 4902 /* 4903 * If the first element selector key is 4904 * actually an array index then ignore the 4905 * bracket. 4906 */ 4907 continue; 4908 } 4909 4910 if (cc == ']') 4911 continue; 4912 4913 if (cc == '.' || cc == '[') { 4914 nelems++; 4915 cc = '\0'; 4916 } 4917 4918 *ee++ = cc; 4919 } 4920 *ee++ = '\0'; 4921 4922 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist, 4923 nelems, dest)) != 0) 4924 mstate->dtms_scratch_ptr += jsonlen + 1; 4925 break; 4926 } 4927 4928 case DIF_SUBR_TOUPPER: 4929 case DIF_SUBR_TOLOWER: { 4930 uintptr_t s = tupregs[0].dttk_value; 4931 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4932 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4933 size_t len = dtrace_strlen((char *)s, size); 4934 char lower, upper, convert; 4935 int64_t i; 4936 4937 if (subr == DIF_SUBR_TOUPPER) { 4938 lower = 'a'; 4939 upper = 'z'; 4940 convert = 'A'; 4941 } else { 4942 lower = 'A'; 4943 upper = 'Z'; 4944 convert = 'a'; 4945 } 4946 4947 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4948 regs[rd] = 0; 4949 break; 4950 } 4951 4952 if (!DTRACE_INSCRATCH(mstate, size)) { 4953 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4954 regs[rd] = 0; 4955 break; 4956 } 4957 4958 for (i = 0; i < size - 1; i++) { 4959 if ((c = dtrace_load8(s + i)) == '\0') 4960 break; 4961 4962 if (c >= lower && c <= upper) 4963 c = convert + (c - lower); 4964 4965 dest[i] = c; 4966 } 4967 4968 ASSERT(i < size); 4969 dest[i] = '\0'; 4970 regs[rd] = (uintptr_t)dest; 4971 mstate->dtms_scratch_ptr += size; 4972 break; 4973 } 4974 4975 #ifdef illumos 4976 case DIF_SUBR_GETMAJOR: 4977 #ifdef _LP64 4978 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 4979 #else 4980 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 4981 #endif 4982 break; 4983 4984 case DIF_SUBR_GETMINOR: 4985 #ifdef _LP64 4986 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 4987 #else 4988 regs[rd] = tupregs[0].dttk_value & MAXMIN; 4989 #endif 4990 break; 4991 4992 case DIF_SUBR_DDI_PATHNAME: { 4993 /* 4994 * This one is a galactic mess. We are going to roughly 4995 * emulate ddi_pathname(), but it's made more complicated 4996 * by the fact that we (a) want to include the minor name and 4997 * (b) must proceed iteratively instead of recursively. 4998 */ 4999 uintptr_t dest = mstate->dtms_scratch_ptr; 5000 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5001 char *start = (char *)dest, *end = start + size - 1; 5002 uintptr_t daddr = tupregs[0].dttk_value; 5003 int64_t minor = (int64_t)tupregs[1].dttk_value; 5004 char *s; 5005 int i, len, depth = 0; 5006 5007 /* 5008 * Due to all the pointer jumping we do and context we must 5009 * rely upon, we just mandate that the user must have kernel 5010 * read privileges to use this routine. 5011 */ 5012 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 5013 *flags |= CPU_DTRACE_KPRIV; 5014 *illval = daddr; 5015 regs[rd] = 0; 5016 } 5017 5018 if (!DTRACE_INSCRATCH(mstate, size)) { 5019 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5020 regs[rd] = 0; 5021 break; 5022 } 5023 5024 *end = '\0'; 5025 5026 /* 5027 * We want to have a name for the minor. In order to do this, 5028 * we need to walk the minor list from the devinfo. We want 5029 * to be sure that we don't infinitely walk a circular list, 5030 * so we check for circularity by sending a scout pointer 5031 * ahead two elements for every element that we iterate over; 5032 * if the list is circular, these will ultimately point to the 5033 * same element. You may recognize this little trick as the 5034 * answer to a stupid interview question -- one that always 5035 * seems to be asked by those who had to have it laboriously 5036 * explained to them, and who can't even concisely describe 5037 * the conditions under which one would be forced to resort to 5038 * this technique. Needless to say, those conditions are 5039 * found here -- and probably only here. Is this the only use 5040 * of this infamous trick in shipping, production code? If it 5041 * isn't, it probably should be... 5042 */ 5043 if (minor != -1) { 5044 uintptr_t maddr = dtrace_loadptr(daddr + 5045 offsetof(struct dev_info, devi_minor)); 5046 5047 uintptr_t next = offsetof(struct ddi_minor_data, next); 5048 uintptr_t name = offsetof(struct ddi_minor_data, 5049 d_minor) + offsetof(struct ddi_minor, name); 5050 uintptr_t dev = offsetof(struct ddi_minor_data, 5051 d_minor) + offsetof(struct ddi_minor, dev); 5052 uintptr_t scout; 5053 5054 if (maddr != NULL) 5055 scout = dtrace_loadptr(maddr + next); 5056 5057 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5058 uint64_t m; 5059 #ifdef _LP64 5060 m = dtrace_load64(maddr + dev) & MAXMIN64; 5061 #else 5062 m = dtrace_load32(maddr + dev) & MAXMIN; 5063 #endif 5064 if (m != minor) { 5065 maddr = dtrace_loadptr(maddr + next); 5066 5067 if (scout == NULL) 5068 continue; 5069 5070 scout = dtrace_loadptr(scout + next); 5071 5072 if (scout == NULL) 5073 continue; 5074 5075 scout = dtrace_loadptr(scout + next); 5076 5077 if (scout == NULL) 5078 continue; 5079 5080 if (scout == maddr) { 5081 *flags |= CPU_DTRACE_ILLOP; 5082 break; 5083 } 5084 5085 continue; 5086 } 5087 5088 /* 5089 * We have the minor data. Now we need to 5090 * copy the minor's name into the end of the 5091 * pathname. 5092 */ 5093 s = (char *)dtrace_loadptr(maddr + name); 5094 len = dtrace_strlen(s, size); 5095 5096 if (*flags & CPU_DTRACE_FAULT) 5097 break; 5098 5099 if (len != 0) { 5100 if ((end -= (len + 1)) < start) 5101 break; 5102 5103 *end = ':'; 5104 } 5105 5106 for (i = 1; i <= len; i++) 5107 end[i] = dtrace_load8((uintptr_t)s++); 5108 break; 5109 } 5110 } 5111 5112 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5113 ddi_node_state_t devi_state; 5114 5115 devi_state = dtrace_load32(daddr + 5116 offsetof(struct dev_info, devi_node_state)); 5117 5118 if (*flags & CPU_DTRACE_FAULT) 5119 break; 5120 5121 if (devi_state >= DS_INITIALIZED) { 5122 s = (char *)dtrace_loadptr(daddr + 5123 offsetof(struct dev_info, devi_addr)); 5124 len = dtrace_strlen(s, size); 5125 5126 if (*flags & CPU_DTRACE_FAULT) 5127 break; 5128 5129 if (len != 0) { 5130 if ((end -= (len + 1)) < start) 5131 break; 5132 5133 *end = '@'; 5134 } 5135 5136 for (i = 1; i <= len; i++) 5137 end[i] = dtrace_load8((uintptr_t)s++); 5138 } 5139 5140 /* 5141 * Now for the node name... 5142 */ 5143 s = (char *)dtrace_loadptr(daddr + 5144 offsetof(struct dev_info, devi_node_name)); 5145 5146 daddr = dtrace_loadptr(daddr + 5147 offsetof(struct dev_info, devi_parent)); 5148 5149 /* 5150 * If our parent is NULL (that is, if we're the root 5151 * node), we're going to use the special path 5152 * "devices". 5153 */ 5154 if (daddr == 0) 5155 s = "devices"; 5156 5157 len = dtrace_strlen(s, size); 5158 if (*flags & CPU_DTRACE_FAULT) 5159 break; 5160 5161 if ((end -= (len + 1)) < start) 5162 break; 5163 5164 for (i = 1; i <= len; i++) 5165 end[i] = dtrace_load8((uintptr_t)s++); 5166 *end = '/'; 5167 5168 if (depth++ > dtrace_devdepth_max) { 5169 *flags |= CPU_DTRACE_ILLOP; 5170 break; 5171 } 5172 } 5173 5174 if (end < start) 5175 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5176 5177 if (daddr == 0) { 5178 regs[rd] = (uintptr_t)end; 5179 mstate->dtms_scratch_ptr += size; 5180 } 5181 5182 break; 5183 } 5184 #endif 5185 5186 case DIF_SUBR_STRJOIN: { 5187 char *d = (char *)mstate->dtms_scratch_ptr; 5188 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5189 uintptr_t s1 = tupregs[0].dttk_value; 5190 uintptr_t s2 = tupregs[1].dttk_value; 5191 int i = 0; 5192 5193 if (!dtrace_strcanload(s1, size, mstate, vstate) || 5194 !dtrace_strcanload(s2, size, mstate, vstate)) { 5195 regs[rd] = 0; 5196 break; 5197 } 5198 5199 if (!DTRACE_INSCRATCH(mstate, size)) { 5200 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5201 regs[rd] = 0; 5202 break; 5203 } 5204 5205 for (;;) { 5206 if (i >= size) { 5207 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5208 regs[rd] = 0; 5209 break; 5210 } 5211 5212 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 5213 i--; 5214 break; 5215 } 5216 } 5217 5218 for (;;) { 5219 if (i >= size) { 5220 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5221 regs[rd] = 0; 5222 break; 5223 } 5224 5225 if ((d[i++] = dtrace_load8(s2++)) == '\0') 5226 break; 5227 } 5228 5229 if (i < size) { 5230 mstate->dtms_scratch_ptr += i; 5231 regs[rd] = (uintptr_t)d; 5232 } 5233 5234 break; 5235 } 5236 5237 case DIF_SUBR_STRTOLL: { 5238 uintptr_t s = tupregs[0].dttk_value; 5239 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5240 int base = 10; 5241 5242 if (nargs > 1) { 5243 if ((base = tupregs[1].dttk_value) <= 1 || 5244 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5245 *flags |= CPU_DTRACE_ILLOP; 5246 break; 5247 } 5248 } 5249 5250 if (!dtrace_strcanload(s, size, mstate, vstate)) { 5251 regs[rd] = INT64_MIN; 5252 break; 5253 } 5254 5255 regs[rd] = dtrace_strtoll((char *)s, base, size); 5256 break; 5257 } 5258 5259 case DIF_SUBR_LLTOSTR: { 5260 int64_t i = (int64_t)tupregs[0].dttk_value; 5261 uint64_t val, digit; 5262 uint64_t size = 65; /* enough room for 2^64 in binary */ 5263 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 5264 int base = 10; 5265 5266 if (nargs > 1) { 5267 if ((base = tupregs[1].dttk_value) <= 1 || 5268 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5269 *flags |= CPU_DTRACE_ILLOP; 5270 break; 5271 } 5272 } 5273 5274 val = (base == 10 && i < 0) ? i * -1 : i; 5275 5276 if (!DTRACE_INSCRATCH(mstate, size)) { 5277 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5278 regs[rd] = 0; 5279 break; 5280 } 5281 5282 for (*end-- = '\0'; val; val /= base) { 5283 if ((digit = val % base) <= '9' - '0') { 5284 *end-- = '0' + digit; 5285 } else { 5286 *end-- = 'a' + (digit - ('9' - '0') - 1); 5287 } 5288 } 5289 5290 if (i == 0 && base == 16) 5291 *end-- = '0'; 5292 5293 if (base == 16) 5294 *end-- = 'x'; 5295 5296 if (i == 0 || base == 8 || base == 16) 5297 *end-- = '0'; 5298 5299 if (i < 0 && base == 10) 5300 *end-- = '-'; 5301 5302 regs[rd] = (uintptr_t)end + 1; 5303 mstate->dtms_scratch_ptr += size; 5304 break; 5305 } 5306 5307 case DIF_SUBR_HTONS: 5308 case DIF_SUBR_NTOHS: 5309 #if BYTE_ORDER == BIG_ENDIAN 5310 regs[rd] = (uint16_t)tupregs[0].dttk_value; 5311 #else 5312 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 5313 #endif 5314 break; 5315 5316 5317 case DIF_SUBR_HTONL: 5318 case DIF_SUBR_NTOHL: 5319 #if BYTE_ORDER == BIG_ENDIAN 5320 regs[rd] = (uint32_t)tupregs[0].dttk_value; 5321 #else 5322 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 5323 #endif 5324 break; 5325 5326 5327 case DIF_SUBR_HTONLL: 5328 case DIF_SUBR_NTOHLL: 5329 #if BYTE_ORDER == BIG_ENDIAN 5330 regs[rd] = (uint64_t)tupregs[0].dttk_value; 5331 #else 5332 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 5333 #endif 5334 break; 5335 5336 5337 case DIF_SUBR_DIRNAME: 5338 case DIF_SUBR_BASENAME: { 5339 char *dest = (char *)mstate->dtms_scratch_ptr; 5340 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5341 uintptr_t src = tupregs[0].dttk_value; 5342 int i, j, len = dtrace_strlen((char *)src, size); 5343 int lastbase = -1, firstbase = -1, lastdir = -1; 5344 int start, end; 5345 5346 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 5347 regs[rd] = 0; 5348 break; 5349 } 5350 5351 if (!DTRACE_INSCRATCH(mstate, size)) { 5352 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5353 regs[rd] = 0; 5354 break; 5355 } 5356 5357 /* 5358 * The basename and dirname for a zero-length string is 5359 * defined to be "." 5360 */ 5361 if (len == 0) { 5362 len = 1; 5363 src = (uintptr_t)"."; 5364 } 5365 5366 /* 5367 * Start from the back of the string, moving back toward the 5368 * front until we see a character that isn't a slash. That 5369 * character is the last character in the basename. 5370 */ 5371 for (i = len - 1; i >= 0; i--) { 5372 if (dtrace_load8(src + i) != '/') 5373 break; 5374 } 5375 5376 if (i >= 0) 5377 lastbase = i; 5378 5379 /* 5380 * Starting from the last character in the basename, move 5381 * towards the front until we find a slash. The character 5382 * that we processed immediately before that is the first 5383 * character in the basename. 5384 */ 5385 for (; i >= 0; i--) { 5386 if (dtrace_load8(src + i) == '/') 5387 break; 5388 } 5389 5390 if (i >= 0) 5391 firstbase = i + 1; 5392 5393 /* 5394 * Now keep going until we find a non-slash character. That 5395 * character is the last character in the dirname. 5396 */ 5397 for (; i >= 0; i--) { 5398 if (dtrace_load8(src + i) != '/') 5399 break; 5400 } 5401 5402 if (i >= 0) 5403 lastdir = i; 5404 5405 ASSERT(!(lastbase == -1 && firstbase != -1)); 5406 ASSERT(!(firstbase == -1 && lastdir != -1)); 5407 5408 if (lastbase == -1) { 5409 /* 5410 * We didn't find a non-slash character. We know that 5411 * the length is non-zero, so the whole string must be 5412 * slashes. In either the dirname or the basename 5413 * case, we return '/'. 5414 */ 5415 ASSERT(firstbase == -1); 5416 firstbase = lastbase = lastdir = 0; 5417 } 5418 5419 if (firstbase == -1) { 5420 /* 5421 * The entire string consists only of a basename 5422 * component. If we're looking for dirname, we need 5423 * to change our string to be just "."; if we're 5424 * looking for a basename, we'll just set the first 5425 * character of the basename to be 0. 5426 */ 5427 if (subr == DIF_SUBR_DIRNAME) { 5428 ASSERT(lastdir == -1); 5429 src = (uintptr_t)"."; 5430 lastdir = 0; 5431 } else { 5432 firstbase = 0; 5433 } 5434 } 5435 5436 if (subr == DIF_SUBR_DIRNAME) { 5437 if (lastdir == -1) { 5438 /* 5439 * We know that we have a slash in the name -- 5440 * or lastdir would be set to 0, above. And 5441 * because lastdir is -1, we know that this 5442 * slash must be the first character. (That 5443 * is, the full string must be of the form 5444 * "/basename".) In this case, the last 5445 * character of the directory name is 0. 5446 */ 5447 lastdir = 0; 5448 } 5449 5450 start = 0; 5451 end = lastdir; 5452 } else { 5453 ASSERT(subr == DIF_SUBR_BASENAME); 5454 ASSERT(firstbase != -1 && lastbase != -1); 5455 start = firstbase; 5456 end = lastbase; 5457 } 5458 5459 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 5460 dest[j] = dtrace_load8(src + i); 5461 5462 dest[j] = '\0'; 5463 regs[rd] = (uintptr_t)dest; 5464 mstate->dtms_scratch_ptr += size; 5465 break; 5466 } 5467 5468 case DIF_SUBR_GETF: { 5469 uintptr_t fd = tupregs[0].dttk_value; 5470 struct filedesc *fdp; 5471 file_t *fp; 5472 5473 if (!dtrace_priv_proc(state)) { 5474 regs[rd] = 0; 5475 break; 5476 } 5477 fdp = curproc->p_fd; 5478 FILEDESC_SLOCK(fdp); 5479 fp = fget_locked(fdp, fd); 5480 mstate->dtms_getf = fp; 5481 regs[rd] = (uintptr_t)fp; 5482 FILEDESC_SUNLOCK(fdp); 5483 break; 5484 } 5485 5486 case DIF_SUBR_CLEANPATH: { 5487 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5488 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5489 uintptr_t src = tupregs[0].dttk_value; 5490 int i = 0, j = 0; 5491 #ifdef illumos 5492 zone_t *z; 5493 #endif 5494 5495 if (!dtrace_strcanload(src, size, mstate, vstate)) { 5496 regs[rd] = 0; 5497 break; 5498 } 5499 5500 if (!DTRACE_INSCRATCH(mstate, size)) { 5501 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5502 regs[rd] = 0; 5503 break; 5504 } 5505 5506 /* 5507 * Move forward, loading each character. 5508 */ 5509 do { 5510 c = dtrace_load8(src + i++); 5511 next: 5512 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 5513 break; 5514 5515 if (c != '/') { 5516 dest[j++] = c; 5517 continue; 5518 } 5519 5520 c = dtrace_load8(src + i++); 5521 5522 if (c == '/') { 5523 /* 5524 * We have two slashes -- we can just advance 5525 * to the next character. 5526 */ 5527 goto next; 5528 } 5529 5530 if (c != '.') { 5531 /* 5532 * This is not "." and it's not ".." -- we can 5533 * just store the "/" and this character and 5534 * drive on. 5535 */ 5536 dest[j++] = '/'; 5537 dest[j++] = c; 5538 continue; 5539 } 5540 5541 c = dtrace_load8(src + i++); 5542 5543 if (c == '/') { 5544 /* 5545 * This is a "/./" component. We're not going 5546 * to store anything in the destination buffer; 5547 * we're just going to go to the next component. 5548 */ 5549 goto next; 5550 } 5551 5552 if (c != '.') { 5553 /* 5554 * This is not ".." -- we can just store the 5555 * "/." and this character and continue 5556 * processing. 5557 */ 5558 dest[j++] = '/'; 5559 dest[j++] = '.'; 5560 dest[j++] = c; 5561 continue; 5562 } 5563 5564 c = dtrace_load8(src + i++); 5565 5566 if (c != '/' && c != '\0') { 5567 /* 5568 * This is not ".." -- it's "..[mumble]". 5569 * We'll store the "/.." and this character 5570 * and continue processing. 5571 */ 5572 dest[j++] = '/'; 5573 dest[j++] = '.'; 5574 dest[j++] = '.'; 5575 dest[j++] = c; 5576 continue; 5577 } 5578 5579 /* 5580 * This is "/../" or "/..\0". We need to back up 5581 * our destination pointer until we find a "/". 5582 */ 5583 i--; 5584 while (j != 0 && dest[--j] != '/') 5585 continue; 5586 5587 if (c == '\0') 5588 dest[++j] = '/'; 5589 } while (c != '\0'); 5590 5591 dest[j] = '\0'; 5592 5593 #ifdef illumos 5594 if (mstate->dtms_getf != NULL && 5595 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 5596 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 5597 /* 5598 * If we've done a getf() as a part of this ECB and we 5599 * don't have kernel access (and we're not in the global 5600 * zone), check if the path we cleaned up begins with 5601 * the zone's root path, and trim it off if so. Note 5602 * that this is an output cleanliness issue, not a 5603 * security issue: knowing one's zone root path does 5604 * not enable privilege escalation. 5605 */ 5606 if (strstr(dest, z->zone_rootpath) == dest) 5607 dest += strlen(z->zone_rootpath) - 1; 5608 } 5609 #endif 5610 5611 regs[rd] = (uintptr_t)dest; 5612 mstate->dtms_scratch_ptr += size; 5613 break; 5614 } 5615 5616 case DIF_SUBR_INET_NTOA: 5617 case DIF_SUBR_INET_NTOA6: 5618 case DIF_SUBR_INET_NTOP: { 5619 size_t size; 5620 int af, argi, i; 5621 char *base, *end; 5622 5623 if (subr == DIF_SUBR_INET_NTOP) { 5624 af = (int)tupregs[0].dttk_value; 5625 argi = 1; 5626 } else { 5627 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 5628 argi = 0; 5629 } 5630 5631 if (af == AF_INET) { 5632 ipaddr_t ip4; 5633 uint8_t *ptr8, val; 5634 5635 /* 5636 * Safely load the IPv4 address. 5637 */ 5638 ip4 = dtrace_load32(tupregs[argi].dttk_value); 5639 5640 /* 5641 * Check an IPv4 string will fit in scratch. 5642 */ 5643 size = INET_ADDRSTRLEN; 5644 if (!DTRACE_INSCRATCH(mstate, size)) { 5645 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5646 regs[rd] = 0; 5647 break; 5648 } 5649 base = (char *)mstate->dtms_scratch_ptr; 5650 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5651 5652 /* 5653 * Stringify as a dotted decimal quad. 5654 */ 5655 *end-- = '\0'; 5656 ptr8 = (uint8_t *)&ip4; 5657 for (i = 3; i >= 0; i--) { 5658 val = ptr8[i]; 5659 5660 if (val == 0) { 5661 *end-- = '0'; 5662 } else { 5663 for (; val; val /= 10) { 5664 *end-- = '0' + (val % 10); 5665 } 5666 } 5667 5668 if (i > 0) 5669 *end-- = '.'; 5670 } 5671 ASSERT(end + 1 >= base); 5672 5673 } else if (af == AF_INET6) { 5674 struct in6_addr ip6; 5675 int firstzero, tryzero, numzero, v6end; 5676 uint16_t val; 5677 const char digits[] = "0123456789abcdef"; 5678 5679 /* 5680 * Stringify using RFC 1884 convention 2 - 16 bit 5681 * hexadecimal values with a zero-run compression. 5682 * Lower case hexadecimal digits are used. 5683 * eg, fe80::214:4fff:fe0b:76c8. 5684 * The IPv4 embedded form is returned for inet_ntop, 5685 * just the IPv4 string is returned for inet_ntoa6. 5686 */ 5687 5688 /* 5689 * Safely load the IPv6 address. 5690 */ 5691 dtrace_bcopy( 5692 (void *)(uintptr_t)tupregs[argi].dttk_value, 5693 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 5694 5695 /* 5696 * Check an IPv6 string will fit in scratch. 5697 */ 5698 size = INET6_ADDRSTRLEN; 5699 if (!DTRACE_INSCRATCH(mstate, size)) { 5700 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5701 regs[rd] = 0; 5702 break; 5703 } 5704 base = (char *)mstate->dtms_scratch_ptr; 5705 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5706 *end-- = '\0'; 5707 5708 /* 5709 * Find the longest run of 16 bit zero values 5710 * for the single allowed zero compression - "::". 5711 */ 5712 firstzero = -1; 5713 tryzero = -1; 5714 numzero = 1; 5715 for (i = 0; i < sizeof (struct in6_addr); i++) { 5716 #ifdef illumos 5717 if (ip6._S6_un._S6_u8[i] == 0 && 5718 #else 5719 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5720 #endif 5721 tryzero == -1 && i % 2 == 0) { 5722 tryzero = i; 5723 continue; 5724 } 5725 5726 if (tryzero != -1 && 5727 #ifdef illumos 5728 (ip6._S6_un._S6_u8[i] != 0 || 5729 #else 5730 (ip6.__u6_addr.__u6_addr8[i] != 0 || 5731 #endif 5732 i == sizeof (struct in6_addr) - 1)) { 5733 5734 if (i - tryzero <= numzero) { 5735 tryzero = -1; 5736 continue; 5737 } 5738 5739 firstzero = tryzero; 5740 numzero = i - i % 2 - tryzero; 5741 tryzero = -1; 5742 5743 #ifdef illumos 5744 if (ip6._S6_un._S6_u8[i] == 0 && 5745 #else 5746 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5747 #endif 5748 i == sizeof (struct in6_addr) - 1) 5749 numzero += 2; 5750 } 5751 } 5752 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 5753 5754 /* 5755 * Check for an IPv4 embedded address. 5756 */ 5757 v6end = sizeof (struct in6_addr) - 2; 5758 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 5759 IN6_IS_ADDR_V4COMPAT(&ip6)) { 5760 for (i = sizeof (struct in6_addr) - 1; 5761 i >= DTRACE_V4MAPPED_OFFSET; i--) { 5762 ASSERT(end >= base); 5763 5764 #ifdef illumos 5765 val = ip6._S6_un._S6_u8[i]; 5766 #else 5767 val = ip6.__u6_addr.__u6_addr8[i]; 5768 #endif 5769 5770 if (val == 0) { 5771 *end-- = '0'; 5772 } else { 5773 for (; val; val /= 10) { 5774 *end-- = '0' + val % 10; 5775 } 5776 } 5777 5778 if (i > DTRACE_V4MAPPED_OFFSET) 5779 *end-- = '.'; 5780 } 5781 5782 if (subr == DIF_SUBR_INET_NTOA6) 5783 goto inetout; 5784 5785 /* 5786 * Set v6end to skip the IPv4 address that 5787 * we have already stringified. 5788 */ 5789 v6end = 10; 5790 } 5791 5792 /* 5793 * Build the IPv6 string by working through the 5794 * address in reverse. 5795 */ 5796 for (i = v6end; i >= 0; i -= 2) { 5797 ASSERT(end >= base); 5798 5799 if (i == firstzero + numzero - 2) { 5800 *end-- = ':'; 5801 *end-- = ':'; 5802 i -= numzero - 2; 5803 continue; 5804 } 5805 5806 if (i < 14 && i != firstzero - 2) 5807 *end-- = ':'; 5808 5809 #ifdef illumos 5810 val = (ip6._S6_un._S6_u8[i] << 8) + 5811 ip6._S6_un._S6_u8[i + 1]; 5812 #else 5813 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 5814 ip6.__u6_addr.__u6_addr8[i + 1]; 5815 #endif 5816 5817 if (val == 0) { 5818 *end-- = '0'; 5819 } else { 5820 for (; val; val /= 16) { 5821 *end-- = digits[val % 16]; 5822 } 5823 } 5824 } 5825 ASSERT(end + 1 >= base); 5826 5827 } else { 5828 /* 5829 * The user didn't use AH_INET or AH_INET6. 5830 */ 5831 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5832 regs[rd] = 0; 5833 break; 5834 } 5835 5836 inetout: regs[rd] = (uintptr_t)end + 1; 5837 mstate->dtms_scratch_ptr += size; 5838 break; 5839 } 5840 5841 case DIF_SUBR_MEMREF: { 5842 uintptr_t size = 2 * sizeof(uintptr_t); 5843 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5844 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 5845 5846 /* address and length */ 5847 memref[0] = tupregs[0].dttk_value; 5848 memref[1] = tupregs[1].dttk_value; 5849 5850 regs[rd] = (uintptr_t) memref; 5851 mstate->dtms_scratch_ptr += scratch_size; 5852 break; 5853 } 5854 5855 #ifndef illumos 5856 case DIF_SUBR_MEMSTR: { 5857 char *str = (char *)mstate->dtms_scratch_ptr; 5858 uintptr_t mem = tupregs[0].dttk_value; 5859 char c = tupregs[1].dttk_value; 5860 size_t size = tupregs[2].dttk_value; 5861 uint8_t n; 5862 int i; 5863 5864 regs[rd] = 0; 5865 5866 if (size == 0) 5867 break; 5868 5869 if (!dtrace_canload(mem, size - 1, mstate, vstate)) 5870 break; 5871 5872 if (!DTRACE_INSCRATCH(mstate, size)) { 5873 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5874 break; 5875 } 5876 5877 if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) { 5878 *flags |= CPU_DTRACE_ILLOP; 5879 break; 5880 } 5881 5882 for (i = 0; i < size - 1; i++) { 5883 n = dtrace_load8(mem++); 5884 str[i] = (n == 0) ? c : n; 5885 } 5886 str[size - 1] = 0; 5887 5888 regs[rd] = (uintptr_t)str; 5889 mstate->dtms_scratch_ptr += size; 5890 break; 5891 } 5892 #endif 5893 5894 case DIF_SUBR_TYPEREF: { 5895 uintptr_t size = 4 * sizeof(uintptr_t); 5896 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5897 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 5898 5899 /* address, num_elements, type_str, type_len */ 5900 typeref[0] = tupregs[0].dttk_value; 5901 typeref[1] = tupregs[1].dttk_value; 5902 typeref[2] = tupregs[2].dttk_value; 5903 typeref[3] = tupregs[3].dttk_value; 5904 5905 regs[rd] = (uintptr_t) typeref; 5906 mstate->dtms_scratch_ptr += scratch_size; 5907 break; 5908 } 5909 } 5910 } 5911 5912 /* 5913 * Emulate the execution of DTrace IR instructions specified by the given 5914 * DIF object. This function is deliberately void of assertions as all of 5915 * the necessary checks are handled by a call to dtrace_difo_validate(). 5916 */ 5917 static uint64_t 5918 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 5919 dtrace_vstate_t *vstate, dtrace_state_t *state) 5920 { 5921 const dif_instr_t *text = difo->dtdo_buf; 5922 const uint_t textlen = difo->dtdo_len; 5923 const char *strtab = difo->dtdo_strtab; 5924 const uint64_t *inttab = difo->dtdo_inttab; 5925 5926 uint64_t rval = 0; 5927 dtrace_statvar_t *svar; 5928 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 5929 dtrace_difv_t *v; 5930 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 5931 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 5932 5933 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 5934 uint64_t regs[DIF_DIR_NREGS]; 5935 uint64_t *tmp; 5936 5937 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 5938 int64_t cc_r; 5939 uint_t pc = 0, id, opc = 0; 5940 uint8_t ttop = 0; 5941 dif_instr_t instr; 5942 uint_t r1, r2, rd; 5943 5944 /* 5945 * We stash the current DIF object into the machine state: we need it 5946 * for subsequent access checking. 5947 */ 5948 mstate->dtms_difo = difo; 5949 5950 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 5951 5952 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 5953 opc = pc; 5954 5955 instr = text[pc++]; 5956 r1 = DIF_INSTR_R1(instr); 5957 r2 = DIF_INSTR_R2(instr); 5958 rd = DIF_INSTR_RD(instr); 5959 5960 switch (DIF_INSTR_OP(instr)) { 5961 case DIF_OP_OR: 5962 regs[rd] = regs[r1] | regs[r2]; 5963 break; 5964 case DIF_OP_XOR: 5965 regs[rd] = regs[r1] ^ regs[r2]; 5966 break; 5967 case DIF_OP_AND: 5968 regs[rd] = regs[r1] & regs[r2]; 5969 break; 5970 case DIF_OP_SLL: 5971 regs[rd] = regs[r1] << regs[r2]; 5972 break; 5973 case DIF_OP_SRL: 5974 regs[rd] = regs[r1] >> regs[r2]; 5975 break; 5976 case DIF_OP_SUB: 5977 regs[rd] = regs[r1] - regs[r2]; 5978 break; 5979 case DIF_OP_ADD: 5980 regs[rd] = regs[r1] + regs[r2]; 5981 break; 5982 case DIF_OP_MUL: 5983 regs[rd] = regs[r1] * regs[r2]; 5984 break; 5985 case DIF_OP_SDIV: 5986 if (regs[r2] == 0) { 5987 regs[rd] = 0; 5988 *flags |= CPU_DTRACE_DIVZERO; 5989 } else { 5990 regs[rd] = (int64_t)regs[r1] / 5991 (int64_t)regs[r2]; 5992 } 5993 break; 5994 5995 case DIF_OP_UDIV: 5996 if (regs[r2] == 0) { 5997 regs[rd] = 0; 5998 *flags |= CPU_DTRACE_DIVZERO; 5999 } else { 6000 regs[rd] = regs[r1] / regs[r2]; 6001 } 6002 break; 6003 6004 case DIF_OP_SREM: 6005 if (regs[r2] == 0) { 6006 regs[rd] = 0; 6007 *flags |= CPU_DTRACE_DIVZERO; 6008 } else { 6009 regs[rd] = (int64_t)regs[r1] % 6010 (int64_t)regs[r2]; 6011 } 6012 break; 6013 6014 case DIF_OP_UREM: 6015 if (regs[r2] == 0) { 6016 regs[rd] = 0; 6017 *flags |= CPU_DTRACE_DIVZERO; 6018 } else { 6019 regs[rd] = regs[r1] % regs[r2]; 6020 } 6021 break; 6022 6023 case DIF_OP_NOT: 6024 regs[rd] = ~regs[r1]; 6025 break; 6026 case DIF_OP_MOV: 6027 regs[rd] = regs[r1]; 6028 break; 6029 case DIF_OP_CMP: 6030 cc_r = regs[r1] - regs[r2]; 6031 cc_n = cc_r < 0; 6032 cc_z = cc_r == 0; 6033 cc_v = 0; 6034 cc_c = regs[r1] < regs[r2]; 6035 break; 6036 case DIF_OP_TST: 6037 cc_n = cc_v = cc_c = 0; 6038 cc_z = regs[r1] == 0; 6039 break; 6040 case DIF_OP_BA: 6041 pc = DIF_INSTR_LABEL(instr); 6042 break; 6043 case DIF_OP_BE: 6044 if (cc_z) 6045 pc = DIF_INSTR_LABEL(instr); 6046 break; 6047 case DIF_OP_BNE: 6048 if (cc_z == 0) 6049 pc = DIF_INSTR_LABEL(instr); 6050 break; 6051 case DIF_OP_BG: 6052 if ((cc_z | (cc_n ^ cc_v)) == 0) 6053 pc = DIF_INSTR_LABEL(instr); 6054 break; 6055 case DIF_OP_BGU: 6056 if ((cc_c | cc_z) == 0) 6057 pc = DIF_INSTR_LABEL(instr); 6058 break; 6059 case DIF_OP_BGE: 6060 if ((cc_n ^ cc_v) == 0) 6061 pc = DIF_INSTR_LABEL(instr); 6062 break; 6063 case DIF_OP_BGEU: 6064 if (cc_c == 0) 6065 pc = DIF_INSTR_LABEL(instr); 6066 break; 6067 case DIF_OP_BL: 6068 if (cc_n ^ cc_v) 6069 pc = DIF_INSTR_LABEL(instr); 6070 break; 6071 case DIF_OP_BLU: 6072 if (cc_c) 6073 pc = DIF_INSTR_LABEL(instr); 6074 break; 6075 case DIF_OP_BLE: 6076 if (cc_z | (cc_n ^ cc_v)) 6077 pc = DIF_INSTR_LABEL(instr); 6078 break; 6079 case DIF_OP_BLEU: 6080 if (cc_c | cc_z) 6081 pc = DIF_INSTR_LABEL(instr); 6082 break; 6083 case DIF_OP_RLDSB: 6084 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6085 break; 6086 /*FALLTHROUGH*/ 6087 case DIF_OP_LDSB: 6088 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 6089 break; 6090 case DIF_OP_RLDSH: 6091 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6092 break; 6093 /*FALLTHROUGH*/ 6094 case DIF_OP_LDSH: 6095 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 6096 break; 6097 case DIF_OP_RLDSW: 6098 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6099 break; 6100 /*FALLTHROUGH*/ 6101 case DIF_OP_LDSW: 6102 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 6103 break; 6104 case DIF_OP_RLDUB: 6105 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6106 break; 6107 /*FALLTHROUGH*/ 6108 case DIF_OP_LDUB: 6109 regs[rd] = dtrace_load8(regs[r1]); 6110 break; 6111 case DIF_OP_RLDUH: 6112 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6113 break; 6114 /*FALLTHROUGH*/ 6115 case DIF_OP_LDUH: 6116 regs[rd] = dtrace_load16(regs[r1]); 6117 break; 6118 case DIF_OP_RLDUW: 6119 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6120 break; 6121 /*FALLTHROUGH*/ 6122 case DIF_OP_LDUW: 6123 regs[rd] = dtrace_load32(regs[r1]); 6124 break; 6125 case DIF_OP_RLDX: 6126 if (!dtrace_canload(regs[r1], 8, mstate, vstate)) 6127 break; 6128 /*FALLTHROUGH*/ 6129 case DIF_OP_LDX: 6130 regs[rd] = dtrace_load64(regs[r1]); 6131 break; 6132 case DIF_OP_ULDSB: 6133 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6134 regs[rd] = (int8_t) 6135 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6136 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6137 break; 6138 case DIF_OP_ULDSH: 6139 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6140 regs[rd] = (int16_t) 6141 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6142 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6143 break; 6144 case DIF_OP_ULDSW: 6145 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6146 regs[rd] = (int32_t) 6147 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6148 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6149 break; 6150 case DIF_OP_ULDUB: 6151 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6152 regs[rd] = 6153 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6154 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6155 break; 6156 case DIF_OP_ULDUH: 6157 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6158 regs[rd] = 6159 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6160 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6161 break; 6162 case DIF_OP_ULDUW: 6163 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6164 regs[rd] = 6165 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6166 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6167 break; 6168 case DIF_OP_ULDX: 6169 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6170 regs[rd] = 6171 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 6172 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6173 break; 6174 case DIF_OP_RET: 6175 rval = regs[rd]; 6176 pc = textlen; 6177 break; 6178 case DIF_OP_NOP: 6179 break; 6180 case DIF_OP_SETX: 6181 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 6182 break; 6183 case DIF_OP_SETS: 6184 regs[rd] = (uint64_t)(uintptr_t) 6185 (strtab + DIF_INSTR_STRING(instr)); 6186 break; 6187 case DIF_OP_SCMP: { 6188 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 6189 uintptr_t s1 = regs[r1]; 6190 uintptr_t s2 = regs[r2]; 6191 6192 if (s1 != 0 && 6193 !dtrace_strcanload(s1, sz, mstate, vstate)) 6194 break; 6195 if (s2 != 0 && 6196 !dtrace_strcanload(s2, sz, mstate, vstate)) 6197 break; 6198 6199 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 6200 6201 cc_n = cc_r < 0; 6202 cc_z = cc_r == 0; 6203 cc_v = cc_c = 0; 6204 break; 6205 } 6206 case DIF_OP_LDGA: 6207 regs[rd] = dtrace_dif_variable(mstate, state, 6208 r1, regs[r2]); 6209 break; 6210 case DIF_OP_LDGS: 6211 id = DIF_INSTR_VAR(instr); 6212 6213 if (id >= DIF_VAR_OTHER_UBASE) { 6214 uintptr_t a; 6215 6216 id -= DIF_VAR_OTHER_UBASE; 6217 svar = vstate->dtvs_globals[id]; 6218 ASSERT(svar != NULL); 6219 v = &svar->dtsv_var; 6220 6221 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 6222 regs[rd] = svar->dtsv_data; 6223 break; 6224 } 6225 6226 a = (uintptr_t)svar->dtsv_data; 6227 6228 if (*(uint8_t *)a == UINT8_MAX) { 6229 /* 6230 * If the 0th byte is set to UINT8_MAX 6231 * then this is to be treated as a 6232 * reference to a NULL variable. 6233 */ 6234 regs[rd] = 0; 6235 } else { 6236 regs[rd] = a + sizeof (uint64_t); 6237 } 6238 6239 break; 6240 } 6241 6242 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 6243 break; 6244 6245 case DIF_OP_STGS: 6246 id = DIF_INSTR_VAR(instr); 6247 6248 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6249 id -= DIF_VAR_OTHER_UBASE; 6250 6251 svar = vstate->dtvs_globals[id]; 6252 ASSERT(svar != NULL); 6253 v = &svar->dtsv_var; 6254 6255 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6256 uintptr_t a = (uintptr_t)svar->dtsv_data; 6257 6258 ASSERT(a != 0); 6259 ASSERT(svar->dtsv_size != 0); 6260 6261 if (regs[rd] == 0) { 6262 *(uint8_t *)a = UINT8_MAX; 6263 break; 6264 } else { 6265 *(uint8_t *)a = 0; 6266 a += sizeof (uint64_t); 6267 } 6268 if (!dtrace_vcanload( 6269 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6270 mstate, vstate)) 6271 break; 6272 6273 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6274 (void *)a, &v->dtdv_type); 6275 break; 6276 } 6277 6278 svar->dtsv_data = regs[rd]; 6279 break; 6280 6281 case DIF_OP_LDTA: 6282 /* 6283 * There are no DTrace built-in thread-local arrays at 6284 * present. This opcode is saved for future work. 6285 */ 6286 *flags |= CPU_DTRACE_ILLOP; 6287 regs[rd] = 0; 6288 break; 6289 6290 case DIF_OP_LDLS: 6291 id = DIF_INSTR_VAR(instr); 6292 6293 if (id < DIF_VAR_OTHER_UBASE) { 6294 /* 6295 * For now, this has no meaning. 6296 */ 6297 regs[rd] = 0; 6298 break; 6299 } 6300 6301 id -= DIF_VAR_OTHER_UBASE; 6302 6303 ASSERT(id < vstate->dtvs_nlocals); 6304 ASSERT(vstate->dtvs_locals != NULL); 6305 6306 svar = vstate->dtvs_locals[id]; 6307 ASSERT(svar != NULL); 6308 v = &svar->dtsv_var; 6309 6310 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6311 uintptr_t a = (uintptr_t)svar->dtsv_data; 6312 size_t sz = v->dtdv_type.dtdt_size; 6313 6314 sz += sizeof (uint64_t); 6315 ASSERT(svar->dtsv_size == NCPU * sz); 6316 a += curcpu * sz; 6317 6318 if (*(uint8_t *)a == UINT8_MAX) { 6319 /* 6320 * If the 0th byte is set to UINT8_MAX 6321 * then this is to be treated as a 6322 * reference to a NULL variable. 6323 */ 6324 regs[rd] = 0; 6325 } else { 6326 regs[rd] = a + sizeof (uint64_t); 6327 } 6328 6329 break; 6330 } 6331 6332 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6333 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6334 regs[rd] = tmp[curcpu]; 6335 break; 6336 6337 case DIF_OP_STLS: 6338 id = DIF_INSTR_VAR(instr); 6339 6340 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6341 id -= DIF_VAR_OTHER_UBASE; 6342 ASSERT(id < vstate->dtvs_nlocals); 6343 6344 ASSERT(vstate->dtvs_locals != NULL); 6345 svar = vstate->dtvs_locals[id]; 6346 ASSERT(svar != NULL); 6347 v = &svar->dtsv_var; 6348 6349 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6350 uintptr_t a = (uintptr_t)svar->dtsv_data; 6351 size_t sz = v->dtdv_type.dtdt_size; 6352 6353 sz += sizeof (uint64_t); 6354 ASSERT(svar->dtsv_size == NCPU * sz); 6355 a += curcpu * sz; 6356 6357 if (regs[rd] == 0) { 6358 *(uint8_t *)a = UINT8_MAX; 6359 break; 6360 } else { 6361 *(uint8_t *)a = 0; 6362 a += sizeof (uint64_t); 6363 } 6364 6365 if (!dtrace_vcanload( 6366 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6367 mstate, vstate)) 6368 break; 6369 6370 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6371 (void *)a, &v->dtdv_type); 6372 break; 6373 } 6374 6375 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6376 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6377 tmp[curcpu] = regs[rd]; 6378 break; 6379 6380 case DIF_OP_LDTS: { 6381 dtrace_dynvar_t *dvar; 6382 dtrace_key_t *key; 6383 6384 id = DIF_INSTR_VAR(instr); 6385 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6386 id -= DIF_VAR_OTHER_UBASE; 6387 v = &vstate->dtvs_tlocals[id]; 6388 6389 key = &tupregs[DIF_DTR_NREGS]; 6390 key[0].dttk_value = (uint64_t)id; 6391 key[0].dttk_size = 0; 6392 DTRACE_TLS_THRKEY(key[1].dttk_value); 6393 key[1].dttk_size = 0; 6394 6395 dvar = dtrace_dynvar(dstate, 2, key, 6396 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 6397 mstate, vstate); 6398 6399 if (dvar == NULL) { 6400 regs[rd] = 0; 6401 break; 6402 } 6403 6404 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6405 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6406 } else { 6407 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6408 } 6409 6410 break; 6411 } 6412 6413 case DIF_OP_STTS: { 6414 dtrace_dynvar_t *dvar; 6415 dtrace_key_t *key; 6416 6417 id = DIF_INSTR_VAR(instr); 6418 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6419 id -= DIF_VAR_OTHER_UBASE; 6420 6421 key = &tupregs[DIF_DTR_NREGS]; 6422 key[0].dttk_value = (uint64_t)id; 6423 key[0].dttk_size = 0; 6424 DTRACE_TLS_THRKEY(key[1].dttk_value); 6425 key[1].dttk_size = 0; 6426 v = &vstate->dtvs_tlocals[id]; 6427 6428 dvar = dtrace_dynvar(dstate, 2, key, 6429 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6430 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6431 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6432 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6433 6434 /* 6435 * Given that we're storing to thread-local data, 6436 * we need to flush our predicate cache. 6437 */ 6438 curthread->t_predcache = 0; 6439 6440 if (dvar == NULL) 6441 break; 6442 6443 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6444 if (!dtrace_vcanload( 6445 (void *)(uintptr_t)regs[rd], 6446 &v->dtdv_type, mstate, vstate)) 6447 break; 6448 6449 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6450 dvar->dtdv_data, &v->dtdv_type); 6451 } else { 6452 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6453 } 6454 6455 break; 6456 } 6457 6458 case DIF_OP_SRA: 6459 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 6460 break; 6461 6462 case DIF_OP_CALL: 6463 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 6464 regs, tupregs, ttop, mstate, state); 6465 break; 6466 6467 case DIF_OP_PUSHTR: 6468 if (ttop == DIF_DTR_NREGS) { 6469 *flags |= CPU_DTRACE_TUPOFLOW; 6470 break; 6471 } 6472 6473 if (r1 == DIF_TYPE_STRING) { 6474 /* 6475 * If this is a string type and the size is 0, 6476 * we'll use the system-wide default string 6477 * size. Note that we are _not_ looking at 6478 * the value of the DTRACEOPT_STRSIZE option; 6479 * had this been set, we would expect to have 6480 * a non-zero size value in the "pushtr". 6481 */ 6482 tupregs[ttop].dttk_size = 6483 dtrace_strlen((char *)(uintptr_t)regs[rd], 6484 regs[r2] ? regs[r2] : 6485 dtrace_strsize_default) + 1; 6486 } else { 6487 if (regs[r2] > LONG_MAX) { 6488 *flags |= CPU_DTRACE_ILLOP; 6489 break; 6490 } 6491 6492 tupregs[ttop].dttk_size = regs[r2]; 6493 } 6494 6495 tupregs[ttop++].dttk_value = regs[rd]; 6496 break; 6497 6498 case DIF_OP_PUSHTV: 6499 if (ttop == DIF_DTR_NREGS) { 6500 *flags |= CPU_DTRACE_TUPOFLOW; 6501 break; 6502 } 6503 6504 tupregs[ttop].dttk_value = regs[rd]; 6505 tupregs[ttop++].dttk_size = 0; 6506 break; 6507 6508 case DIF_OP_POPTS: 6509 if (ttop != 0) 6510 ttop--; 6511 break; 6512 6513 case DIF_OP_FLUSHTS: 6514 ttop = 0; 6515 break; 6516 6517 case DIF_OP_LDGAA: 6518 case DIF_OP_LDTAA: { 6519 dtrace_dynvar_t *dvar; 6520 dtrace_key_t *key = tupregs; 6521 uint_t nkeys = ttop; 6522 6523 id = DIF_INSTR_VAR(instr); 6524 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6525 id -= DIF_VAR_OTHER_UBASE; 6526 6527 key[nkeys].dttk_value = (uint64_t)id; 6528 key[nkeys++].dttk_size = 0; 6529 6530 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 6531 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6532 key[nkeys++].dttk_size = 0; 6533 v = &vstate->dtvs_tlocals[id]; 6534 } else { 6535 v = &vstate->dtvs_globals[id]->dtsv_var; 6536 } 6537 6538 dvar = dtrace_dynvar(dstate, nkeys, key, 6539 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6540 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6541 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 6542 6543 if (dvar == NULL) { 6544 regs[rd] = 0; 6545 break; 6546 } 6547 6548 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6549 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6550 } else { 6551 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6552 } 6553 6554 break; 6555 } 6556 6557 case DIF_OP_STGAA: 6558 case DIF_OP_STTAA: { 6559 dtrace_dynvar_t *dvar; 6560 dtrace_key_t *key = tupregs; 6561 uint_t nkeys = ttop; 6562 6563 id = DIF_INSTR_VAR(instr); 6564 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6565 id -= DIF_VAR_OTHER_UBASE; 6566 6567 key[nkeys].dttk_value = (uint64_t)id; 6568 key[nkeys++].dttk_size = 0; 6569 6570 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 6571 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6572 key[nkeys++].dttk_size = 0; 6573 v = &vstate->dtvs_tlocals[id]; 6574 } else { 6575 v = &vstate->dtvs_globals[id]->dtsv_var; 6576 } 6577 6578 dvar = dtrace_dynvar(dstate, nkeys, key, 6579 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6580 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6581 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6582 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6583 6584 if (dvar == NULL) 6585 break; 6586 6587 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6588 if (!dtrace_vcanload( 6589 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6590 mstate, vstate)) 6591 break; 6592 6593 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6594 dvar->dtdv_data, &v->dtdv_type); 6595 } else { 6596 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6597 } 6598 6599 break; 6600 } 6601 6602 case DIF_OP_ALLOCS: { 6603 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6604 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 6605 6606 /* 6607 * Rounding up the user allocation size could have 6608 * overflowed large, bogus allocations (like -1ULL) to 6609 * 0. 6610 */ 6611 if (size < regs[r1] || 6612 !DTRACE_INSCRATCH(mstate, size)) { 6613 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6614 regs[rd] = 0; 6615 break; 6616 } 6617 6618 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 6619 mstate->dtms_scratch_ptr += size; 6620 regs[rd] = ptr; 6621 break; 6622 } 6623 6624 case DIF_OP_COPYS: 6625 if (!dtrace_canstore(regs[rd], regs[r2], 6626 mstate, vstate)) { 6627 *flags |= CPU_DTRACE_BADADDR; 6628 *illval = regs[rd]; 6629 break; 6630 } 6631 6632 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 6633 break; 6634 6635 dtrace_bcopy((void *)(uintptr_t)regs[r1], 6636 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 6637 break; 6638 6639 case DIF_OP_STB: 6640 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 6641 *flags |= CPU_DTRACE_BADADDR; 6642 *illval = regs[rd]; 6643 break; 6644 } 6645 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 6646 break; 6647 6648 case DIF_OP_STH: 6649 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 6650 *flags |= CPU_DTRACE_BADADDR; 6651 *illval = regs[rd]; 6652 break; 6653 } 6654 if (regs[rd] & 1) { 6655 *flags |= CPU_DTRACE_BADALIGN; 6656 *illval = regs[rd]; 6657 break; 6658 } 6659 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 6660 break; 6661 6662 case DIF_OP_STW: 6663 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 6664 *flags |= CPU_DTRACE_BADADDR; 6665 *illval = regs[rd]; 6666 break; 6667 } 6668 if (regs[rd] & 3) { 6669 *flags |= CPU_DTRACE_BADALIGN; 6670 *illval = regs[rd]; 6671 break; 6672 } 6673 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 6674 break; 6675 6676 case DIF_OP_STX: 6677 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 6678 *flags |= CPU_DTRACE_BADADDR; 6679 *illval = regs[rd]; 6680 break; 6681 } 6682 if (regs[rd] & 7) { 6683 *flags |= CPU_DTRACE_BADALIGN; 6684 *illval = regs[rd]; 6685 break; 6686 } 6687 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 6688 break; 6689 } 6690 } 6691 6692 if (!(*flags & CPU_DTRACE_FAULT)) 6693 return (rval); 6694 6695 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 6696 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 6697 6698 return (0); 6699 } 6700 6701 static void 6702 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 6703 { 6704 dtrace_probe_t *probe = ecb->dte_probe; 6705 dtrace_provider_t *prov = probe->dtpr_provider; 6706 char c[DTRACE_FULLNAMELEN + 80], *str; 6707 char *msg = "dtrace: breakpoint action at probe "; 6708 char *ecbmsg = " (ecb "; 6709 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 6710 uintptr_t val = (uintptr_t)ecb; 6711 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 6712 6713 if (dtrace_destructive_disallow) 6714 return; 6715 6716 /* 6717 * It's impossible to be taking action on the NULL probe. 6718 */ 6719 ASSERT(probe != NULL); 6720 6721 /* 6722 * This is a poor man's (destitute man's?) sprintf(): we want to 6723 * print the provider name, module name, function name and name of 6724 * the probe, along with the hex address of the ECB with the breakpoint 6725 * action -- all of which we must place in the character buffer by 6726 * hand. 6727 */ 6728 while (*msg != '\0') 6729 c[i++] = *msg++; 6730 6731 for (str = prov->dtpv_name; *str != '\0'; str++) 6732 c[i++] = *str; 6733 c[i++] = ':'; 6734 6735 for (str = probe->dtpr_mod; *str != '\0'; str++) 6736 c[i++] = *str; 6737 c[i++] = ':'; 6738 6739 for (str = probe->dtpr_func; *str != '\0'; str++) 6740 c[i++] = *str; 6741 c[i++] = ':'; 6742 6743 for (str = probe->dtpr_name; *str != '\0'; str++) 6744 c[i++] = *str; 6745 6746 while (*ecbmsg != '\0') 6747 c[i++] = *ecbmsg++; 6748 6749 while (shift >= 0) { 6750 mask = (uintptr_t)0xf << shift; 6751 6752 if (val >= ((uintptr_t)1 << shift)) 6753 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 6754 shift -= 4; 6755 } 6756 6757 c[i++] = ')'; 6758 c[i] = '\0'; 6759 6760 #ifdef illumos 6761 debug_enter(c); 6762 #else 6763 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 6764 #endif 6765 } 6766 6767 static void 6768 dtrace_action_panic(dtrace_ecb_t *ecb) 6769 { 6770 dtrace_probe_t *probe = ecb->dte_probe; 6771 6772 /* 6773 * It's impossible to be taking action on the NULL probe. 6774 */ 6775 ASSERT(probe != NULL); 6776 6777 if (dtrace_destructive_disallow) 6778 return; 6779 6780 if (dtrace_panicked != NULL) 6781 return; 6782 6783 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 6784 return; 6785 6786 /* 6787 * We won the right to panic. (We want to be sure that only one 6788 * thread calls panic() from dtrace_probe(), and that panic() is 6789 * called exactly once.) 6790 */ 6791 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 6792 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 6793 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 6794 } 6795 6796 static void 6797 dtrace_action_raise(uint64_t sig) 6798 { 6799 if (dtrace_destructive_disallow) 6800 return; 6801 6802 if (sig >= NSIG) { 6803 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 6804 return; 6805 } 6806 6807 #ifdef illumos 6808 /* 6809 * raise() has a queue depth of 1 -- we ignore all subsequent 6810 * invocations of the raise() action. 6811 */ 6812 if (curthread->t_dtrace_sig == 0) 6813 curthread->t_dtrace_sig = (uint8_t)sig; 6814 6815 curthread->t_sig_check = 1; 6816 aston(curthread); 6817 #else 6818 struct proc *p = curproc; 6819 PROC_LOCK(p); 6820 kern_psignal(p, sig); 6821 PROC_UNLOCK(p); 6822 #endif 6823 } 6824 6825 static void 6826 dtrace_action_stop(void) 6827 { 6828 if (dtrace_destructive_disallow) 6829 return; 6830 6831 #ifdef illumos 6832 if (!curthread->t_dtrace_stop) { 6833 curthread->t_dtrace_stop = 1; 6834 curthread->t_sig_check = 1; 6835 aston(curthread); 6836 } 6837 #else 6838 struct proc *p = curproc; 6839 PROC_LOCK(p); 6840 kern_psignal(p, SIGSTOP); 6841 PROC_UNLOCK(p); 6842 #endif 6843 } 6844 6845 static void 6846 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 6847 { 6848 hrtime_t now; 6849 volatile uint16_t *flags; 6850 #ifdef illumos 6851 cpu_t *cpu = CPU; 6852 #else 6853 cpu_t *cpu = &solaris_cpu[curcpu]; 6854 #endif 6855 6856 if (dtrace_destructive_disallow) 6857 return; 6858 6859 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 6860 6861 now = dtrace_gethrtime(); 6862 6863 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 6864 /* 6865 * We need to advance the mark to the current time. 6866 */ 6867 cpu->cpu_dtrace_chillmark = now; 6868 cpu->cpu_dtrace_chilled = 0; 6869 } 6870 6871 /* 6872 * Now check to see if the requested chill time would take us over 6873 * the maximum amount of time allowed in the chill interval. (Or 6874 * worse, if the calculation itself induces overflow.) 6875 */ 6876 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 6877 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 6878 *flags |= CPU_DTRACE_ILLOP; 6879 return; 6880 } 6881 6882 while (dtrace_gethrtime() - now < val) 6883 continue; 6884 6885 /* 6886 * Normally, we assure that the value of the variable "timestamp" does 6887 * not change within an ECB. The presence of chill() represents an 6888 * exception to this rule, however. 6889 */ 6890 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 6891 cpu->cpu_dtrace_chilled += val; 6892 } 6893 6894 static void 6895 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 6896 uint64_t *buf, uint64_t arg) 6897 { 6898 int nframes = DTRACE_USTACK_NFRAMES(arg); 6899 int strsize = DTRACE_USTACK_STRSIZE(arg); 6900 uint64_t *pcs = &buf[1], *fps; 6901 char *str = (char *)&pcs[nframes]; 6902 int size, offs = 0, i, j; 6903 uintptr_t old = mstate->dtms_scratch_ptr, saved; 6904 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 6905 char *sym; 6906 6907 /* 6908 * Should be taking a faster path if string space has not been 6909 * allocated. 6910 */ 6911 ASSERT(strsize != 0); 6912 6913 /* 6914 * We will first allocate some temporary space for the frame pointers. 6915 */ 6916 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6917 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 6918 (nframes * sizeof (uint64_t)); 6919 6920 if (!DTRACE_INSCRATCH(mstate, size)) { 6921 /* 6922 * Not enough room for our frame pointers -- need to indicate 6923 * that we ran out of scratch space. 6924 */ 6925 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6926 return; 6927 } 6928 6929 mstate->dtms_scratch_ptr += size; 6930 saved = mstate->dtms_scratch_ptr; 6931 6932 /* 6933 * Now get a stack with both program counters and frame pointers. 6934 */ 6935 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6936 dtrace_getufpstack(buf, fps, nframes + 1); 6937 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6938 6939 /* 6940 * If that faulted, we're cooked. 6941 */ 6942 if (*flags & CPU_DTRACE_FAULT) 6943 goto out; 6944 6945 /* 6946 * Now we want to walk up the stack, calling the USTACK helper. For 6947 * each iteration, we restore the scratch pointer. 6948 */ 6949 for (i = 0; i < nframes; i++) { 6950 mstate->dtms_scratch_ptr = saved; 6951 6952 if (offs >= strsize) 6953 break; 6954 6955 sym = (char *)(uintptr_t)dtrace_helper( 6956 DTRACE_HELPER_ACTION_USTACK, 6957 mstate, state, pcs[i], fps[i]); 6958 6959 /* 6960 * If we faulted while running the helper, we're going to 6961 * clear the fault and null out the corresponding string. 6962 */ 6963 if (*flags & CPU_DTRACE_FAULT) { 6964 *flags &= ~CPU_DTRACE_FAULT; 6965 str[offs++] = '\0'; 6966 continue; 6967 } 6968 6969 if (sym == NULL) { 6970 str[offs++] = '\0'; 6971 continue; 6972 } 6973 6974 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6975 6976 /* 6977 * Now copy in the string that the helper returned to us. 6978 */ 6979 for (j = 0; offs + j < strsize; j++) { 6980 if ((str[offs + j] = sym[j]) == '\0') 6981 break; 6982 } 6983 6984 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6985 6986 offs += j + 1; 6987 } 6988 6989 if (offs >= strsize) { 6990 /* 6991 * If we didn't have room for all of the strings, we don't 6992 * abort processing -- this needn't be a fatal error -- but we 6993 * still want to increment a counter (dts_stkstroverflows) to 6994 * allow this condition to be warned about. (If this is from 6995 * a jstack() action, it is easily tuned via jstackstrsize.) 6996 */ 6997 dtrace_error(&state->dts_stkstroverflows); 6998 } 6999 7000 while (offs < strsize) 7001 str[offs++] = '\0'; 7002 7003 out: 7004 mstate->dtms_scratch_ptr = old; 7005 } 7006 7007 static void 7008 dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size, 7009 size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind) 7010 { 7011 volatile uint16_t *flags; 7012 uint64_t val = *valp; 7013 size_t valoffs = *valoffsp; 7014 7015 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 7016 ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF); 7017 7018 /* 7019 * If this is a string, we're going to only load until we find the zero 7020 * byte -- after which we'll store zero bytes. 7021 */ 7022 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 7023 char c = '\0' + 1; 7024 size_t s; 7025 7026 for (s = 0; s < size; s++) { 7027 if (c != '\0' && dtkind == DIF_TF_BYREF) { 7028 c = dtrace_load8(val++); 7029 } else if (c != '\0' && dtkind == DIF_TF_BYUREF) { 7030 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7031 c = dtrace_fuword8((void *)(uintptr_t)val++); 7032 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7033 if (*flags & CPU_DTRACE_FAULT) 7034 break; 7035 } 7036 7037 DTRACE_STORE(uint8_t, tomax, valoffs++, c); 7038 7039 if (c == '\0' && intuple) 7040 break; 7041 } 7042 } else { 7043 uint8_t c; 7044 while (valoffs < end) { 7045 if (dtkind == DIF_TF_BYREF) { 7046 c = dtrace_load8(val++); 7047 } else if (dtkind == DIF_TF_BYUREF) { 7048 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7049 c = dtrace_fuword8((void *)(uintptr_t)val++); 7050 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7051 if (*flags & CPU_DTRACE_FAULT) 7052 break; 7053 } 7054 7055 DTRACE_STORE(uint8_t, tomax, 7056 valoffs++, c); 7057 } 7058 } 7059 7060 *valp = val; 7061 *valoffsp = valoffs; 7062 } 7063 7064 /* 7065 * If you're looking for the epicenter of DTrace, you just found it. This 7066 * is the function called by the provider to fire a probe -- from which all 7067 * subsequent probe-context DTrace activity emanates. 7068 */ 7069 void 7070 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 7071 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 7072 { 7073 processorid_t cpuid; 7074 dtrace_icookie_t cookie; 7075 dtrace_probe_t *probe; 7076 dtrace_mstate_t mstate; 7077 dtrace_ecb_t *ecb; 7078 dtrace_action_t *act; 7079 intptr_t offs; 7080 size_t size; 7081 int vtime, onintr; 7082 volatile uint16_t *flags; 7083 hrtime_t now; 7084 7085 if (panicstr != NULL) 7086 return; 7087 7088 #ifdef illumos 7089 /* 7090 * Kick out immediately if this CPU is still being born (in which case 7091 * curthread will be set to -1) or the current thread can't allow 7092 * probes in its current context. 7093 */ 7094 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 7095 return; 7096 #endif 7097 7098 cookie = dtrace_interrupt_disable(); 7099 probe = dtrace_probes[id - 1]; 7100 cpuid = curcpu; 7101 onintr = CPU_ON_INTR(CPU); 7102 7103 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 7104 probe->dtpr_predcache == curthread->t_predcache) { 7105 /* 7106 * We have hit in the predicate cache; we know that 7107 * this predicate would evaluate to be false. 7108 */ 7109 dtrace_interrupt_enable(cookie); 7110 return; 7111 } 7112 7113 #ifdef illumos 7114 if (panic_quiesce) { 7115 #else 7116 if (panicstr != NULL) { 7117 #endif 7118 /* 7119 * We don't trace anything if we're panicking. 7120 */ 7121 dtrace_interrupt_enable(cookie); 7122 return; 7123 } 7124 7125 now = mstate.dtms_timestamp = dtrace_gethrtime(); 7126 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7127 vtime = dtrace_vtime_references != 0; 7128 7129 if (vtime && curthread->t_dtrace_start) 7130 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 7131 7132 mstate.dtms_difo = NULL; 7133 mstate.dtms_probe = probe; 7134 mstate.dtms_strtok = 0; 7135 mstate.dtms_arg[0] = arg0; 7136 mstate.dtms_arg[1] = arg1; 7137 mstate.dtms_arg[2] = arg2; 7138 mstate.dtms_arg[3] = arg3; 7139 mstate.dtms_arg[4] = arg4; 7140 7141 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 7142 7143 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 7144 dtrace_predicate_t *pred = ecb->dte_predicate; 7145 dtrace_state_t *state = ecb->dte_state; 7146 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 7147 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 7148 dtrace_vstate_t *vstate = &state->dts_vstate; 7149 dtrace_provider_t *prov = probe->dtpr_provider; 7150 uint64_t tracememsize = 0; 7151 int committed = 0; 7152 caddr_t tomax; 7153 7154 /* 7155 * A little subtlety with the following (seemingly innocuous) 7156 * declaration of the automatic 'val': by looking at the 7157 * code, you might think that it could be declared in the 7158 * action processing loop, below. (That is, it's only used in 7159 * the action processing loop.) However, it must be declared 7160 * out of that scope because in the case of DIF expression 7161 * arguments to aggregating actions, one iteration of the 7162 * action loop will use the last iteration's value. 7163 */ 7164 uint64_t val = 0; 7165 7166 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 7167 mstate.dtms_getf = NULL; 7168 7169 *flags &= ~CPU_DTRACE_ERROR; 7170 7171 if (prov == dtrace_provider) { 7172 /* 7173 * If dtrace itself is the provider of this probe, 7174 * we're only going to continue processing the ECB if 7175 * arg0 (the dtrace_state_t) is equal to the ECB's 7176 * creating state. (This prevents disjoint consumers 7177 * from seeing one another's metaprobes.) 7178 */ 7179 if (arg0 != (uint64_t)(uintptr_t)state) 7180 continue; 7181 } 7182 7183 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 7184 /* 7185 * We're not currently active. If our provider isn't 7186 * the dtrace pseudo provider, we're not interested. 7187 */ 7188 if (prov != dtrace_provider) 7189 continue; 7190 7191 /* 7192 * Now we must further check if we are in the BEGIN 7193 * probe. If we are, we will only continue processing 7194 * if we're still in WARMUP -- if one BEGIN enabling 7195 * has invoked the exit() action, we don't want to 7196 * evaluate subsequent BEGIN enablings. 7197 */ 7198 if (probe->dtpr_id == dtrace_probeid_begin && 7199 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 7200 ASSERT(state->dts_activity == 7201 DTRACE_ACTIVITY_DRAINING); 7202 continue; 7203 } 7204 } 7205 7206 if (ecb->dte_cond) { 7207 /* 7208 * If the dte_cond bits indicate that this 7209 * consumer is only allowed to see user-mode firings 7210 * of this probe, call the provider's dtps_usermode() 7211 * entry point to check that the probe was fired 7212 * while in a user context. Skip this ECB if that's 7213 * not the case. 7214 */ 7215 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 7216 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 7217 probe->dtpr_id, probe->dtpr_arg) == 0) 7218 continue; 7219 7220 #ifdef illumos 7221 /* 7222 * This is more subtle than it looks. We have to be 7223 * absolutely certain that CRED() isn't going to 7224 * change out from under us so it's only legit to 7225 * examine that structure if we're in constrained 7226 * situations. Currently, the only times we'll this 7227 * check is if a non-super-user has enabled the 7228 * profile or syscall providers -- providers that 7229 * allow visibility of all processes. For the 7230 * profile case, the check above will ensure that 7231 * we're examining a user context. 7232 */ 7233 if (ecb->dte_cond & DTRACE_COND_OWNER) { 7234 cred_t *cr; 7235 cred_t *s_cr = 7236 ecb->dte_state->dts_cred.dcr_cred; 7237 proc_t *proc; 7238 7239 ASSERT(s_cr != NULL); 7240 7241 if ((cr = CRED()) == NULL || 7242 s_cr->cr_uid != cr->cr_uid || 7243 s_cr->cr_uid != cr->cr_ruid || 7244 s_cr->cr_uid != cr->cr_suid || 7245 s_cr->cr_gid != cr->cr_gid || 7246 s_cr->cr_gid != cr->cr_rgid || 7247 s_cr->cr_gid != cr->cr_sgid || 7248 (proc = ttoproc(curthread)) == NULL || 7249 (proc->p_flag & SNOCD)) 7250 continue; 7251 } 7252 7253 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 7254 cred_t *cr; 7255 cred_t *s_cr = 7256 ecb->dte_state->dts_cred.dcr_cred; 7257 7258 ASSERT(s_cr != NULL); 7259 7260 if ((cr = CRED()) == NULL || 7261 s_cr->cr_zone->zone_id != 7262 cr->cr_zone->zone_id) 7263 continue; 7264 } 7265 #endif 7266 } 7267 7268 if (now - state->dts_alive > dtrace_deadman_timeout) { 7269 /* 7270 * We seem to be dead. Unless we (a) have kernel 7271 * destructive permissions (b) have explicitly enabled 7272 * destructive actions and (c) destructive actions have 7273 * not been disabled, we're going to transition into 7274 * the KILLED state, from which no further processing 7275 * on this state will be performed. 7276 */ 7277 if (!dtrace_priv_kernel_destructive(state) || 7278 !state->dts_cred.dcr_destructive || 7279 dtrace_destructive_disallow) { 7280 void *activity = &state->dts_activity; 7281 dtrace_activity_t current; 7282 7283 do { 7284 current = state->dts_activity; 7285 } while (dtrace_cas32(activity, current, 7286 DTRACE_ACTIVITY_KILLED) != current); 7287 7288 continue; 7289 } 7290 } 7291 7292 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 7293 ecb->dte_alignment, state, &mstate)) < 0) 7294 continue; 7295 7296 tomax = buf->dtb_tomax; 7297 ASSERT(tomax != NULL); 7298 7299 if (ecb->dte_size != 0) { 7300 dtrace_rechdr_t dtrh; 7301 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 7302 mstate.dtms_timestamp = dtrace_gethrtime(); 7303 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7304 } 7305 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 7306 dtrh.dtrh_epid = ecb->dte_epid; 7307 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 7308 mstate.dtms_timestamp); 7309 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 7310 } 7311 7312 mstate.dtms_epid = ecb->dte_epid; 7313 mstate.dtms_present |= DTRACE_MSTATE_EPID; 7314 7315 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 7316 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 7317 else 7318 mstate.dtms_access = 0; 7319 7320 if (pred != NULL) { 7321 dtrace_difo_t *dp = pred->dtp_difo; 7322 int rval; 7323 7324 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 7325 7326 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 7327 dtrace_cacheid_t cid = probe->dtpr_predcache; 7328 7329 if (cid != DTRACE_CACHEIDNONE && !onintr) { 7330 /* 7331 * Update the predicate cache... 7332 */ 7333 ASSERT(cid == pred->dtp_cacheid); 7334 curthread->t_predcache = cid; 7335 } 7336 7337 continue; 7338 } 7339 } 7340 7341 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 7342 act != NULL; act = act->dta_next) { 7343 size_t valoffs; 7344 dtrace_difo_t *dp; 7345 dtrace_recdesc_t *rec = &act->dta_rec; 7346 7347 size = rec->dtrd_size; 7348 valoffs = offs + rec->dtrd_offset; 7349 7350 if (DTRACEACT_ISAGG(act->dta_kind)) { 7351 uint64_t v = 0xbad; 7352 dtrace_aggregation_t *agg; 7353 7354 agg = (dtrace_aggregation_t *)act; 7355 7356 if ((dp = act->dta_difo) != NULL) 7357 v = dtrace_dif_emulate(dp, 7358 &mstate, vstate, state); 7359 7360 if (*flags & CPU_DTRACE_ERROR) 7361 continue; 7362 7363 /* 7364 * Note that we always pass the expression 7365 * value from the previous iteration of the 7366 * action loop. This value will only be used 7367 * if there is an expression argument to the 7368 * aggregating action, denoted by the 7369 * dtag_hasarg field. 7370 */ 7371 dtrace_aggregate(agg, buf, 7372 offs, aggbuf, v, val); 7373 continue; 7374 } 7375 7376 switch (act->dta_kind) { 7377 case DTRACEACT_STOP: 7378 if (dtrace_priv_proc_destructive(state)) 7379 dtrace_action_stop(); 7380 continue; 7381 7382 case DTRACEACT_BREAKPOINT: 7383 if (dtrace_priv_kernel_destructive(state)) 7384 dtrace_action_breakpoint(ecb); 7385 continue; 7386 7387 case DTRACEACT_PANIC: 7388 if (dtrace_priv_kernel_destructive(state)) 7389 dtrace_action_panic(ecb); 7390 continue; 7391 7392 case DTRACEACT_STACK: 7393 if (!dtrace_priv_kernel(state)) 7394 continue; 7395 7396 dtrace_getpcstack((pc_t *)(tomax + valoffs), 7397 size / sizeof (pc_t), probe->dtpr_aframes, 7398 DTRACE_ANCHORED(probe) ? NULL : 7399 (uint32_t *)arg0); 7400 continue; 7401 7402 case DTRACEACT_JSTACK: 7403 case DTRACEACT_USTACK: 7404 if (!dtrace_priv_proc(state)) 7405 continue; 7406 7407 /* 7408 * See comment in DIF_VAR_PID. 7409 */ 7410 if (DTRACE_ANCHORED(mstate.dtms_probe) && 7411 CPU_ON_INTR(CPU)) { 7412 int depth = DTRACE_USTACK_NFRAMES( 7413 rec->dtrd_arg) + 1; 7414 7415 dtrace_bzero((void *)(tomax + valoffs), 7416 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 7417 + depth * sizeof (uint64_t)); 7418 7419 continue; 7420 } 7421 7422 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 7423 curproc->p_dtrace_helpers != NULL) { 7424 /* 7425 * This is the slow path -- we have 7426 * allocated string space, and we're 7427 * getting the stack of a process that 7428 * has helpers. Call into a separate 7429 * routine to perform this processing. 7430 */ 7431 dtrace_action_ustack(&mstate, state, 7432 (uint64_t *)(tomax + valoffs), 7433 rec->dtrd_arg); 7434 continue; 7435 } 7436 7437 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7438 dtrace_getupcstack((uint64_t *) 7439 (tomax + valoffs), 7440 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 7441 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7442 continue; 7443 7444 default: 7445 break; 7446 } 7447 7448 dp = act->dta_difo; 7449 ASSERT(dp != NULL); 7450 7451 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 7452 7453 if (*flags & CPU_DTRACE_ERROR) 7454 continue; 7455 7456 switch (act->dta_kind) { 7457 case DTRACEACT_SPECULATE: { 7458 dtrace_rechdr_t *dtrh; 7459 7460 ASSERT(buf == &state->dts_buffer[cpuid]); 7461 buf = dtrace_speculation_buffer(state, 7462 cpuid, val); 7463 7464 if (buf == NULL) { 7465 *flags |= CPU_DTRACE_DROP; 7466 continue; 7467 } 7468 7469 offs = dtrace_buffer_reserve(buf, 7470 ecb->dte_needed, ecb->dte_alignment, 7471 state, NULL); 7472 7473 if (offs < 0) { 7474 *flags |= CPU_DTRACE_DROP; 7475 continue; 7476 } 7477 7478 tomax = buf->dtb_tomax; 7479 ASSERT(tomax != NULL); 7480 7481 if (ecb->dte_size == 0) 7482 continue; 7483 7484 ASSERT3U(ecb->dte_size, >=, 7485 sizeof (dtrace_rechdr_t)); 7486 dtrh = ((void *)(tomax + offs)); 7487 dtrh->dtrh_epid = ecb->dte_epid; 7488 /* 7489 * When the speculation is committed, all of 7490 * the records in the speculative buffer will 7491 * have their timestamps set to the commit 7492 * time. Until then, it is set to a sentinel 7493 * value, for debugability. 7494 */ 7495 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 7496 continue; 7497 } 7498 7499 case DTRACEACT_PRINTM: { 7500 /* The DIF returns a 'memref'. */ 7501 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 7502 7503 /* Get the size from the memref. */ 7504 size = memref[1]; 7505 7506 /* 7507 * Check if the size exceeds the allocated 7508 * buffer size. 7509 */ 7510 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7511 /* Flag a drop! */ 7512 *flags |= CPU_DTRACE_DROP; 7513 continue; 7514 } 7515 7516 /* Store the size in the buffer first. */ 7517 DTRACE_STORE(uintptr_t, tomax, 7518 valoffs, size); 7519 7520 /* 7521 * Offset the buffer address to the start 7522 * of the data. 7523 */ 7524 valoffs += sizeof(uintptr_t); 7525 7526 /* 7527 * Reset to the memory address rather than 7528 * the memref array, then let the BYREF 7529 * code below do the work to store the 7530 * memory data in the buffer. 7531 */ 7532 val = memref[0]; 7533 break; 7534 } 7535 7536 case DTRACEACT_PRINTT: { 7537 /* The DIF returns a 'typeref'. */ 7538 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 7539 char c = '\0' + 1; 7540 size_t s; 7541 7542 /* 7543 * Get the type string length and round it 7544 * up so that the data that follows is 7545 * aligned for easy access. 7546 */ 7547 size_t typs = strlen((char *) typeref[2]) + 1; 7548 typs = roundup(typs, sizeof(uintptr_t)); 7549 7550 /* 7551 *Get the size from the typeref using the 7552 * number of elements and the type size. 7553 */ 7554 size = typeref[1] * typeref[3]; 7555 7556 /* 7557 * Check if the size exceeds the allocated 7558 * buffer size. 7559 */ 7560 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7561 /* Flag a drop! */ 7562 *flags |= CPU_DTRACE_DROP; 7563 7564 } 7565 7566 /* Store the size in the buffer first. */ 7567 DTRACE_STORE(uintptr_t, tomax, 7568 valoffs, size); 7569 valoffs += sizeof(uintptr_t); 7570 7571 /* Store the type size in the buffer. */ 7572 DTRACE_STORE(uintptr_t, tomax, 7573 valoffs, typeref[3]); 7574 valoffs += sizeof(uintptr_t); 7575 7576 val = typeref[2]; 7577 7578 for (s = 0; s < typs; s++) { 7579 if (c != '\0') 7580 c = dtrace_load8(val++); 7581 7582 DTRACE_STORE(uint8_t, tomax, 7583 valoffs++, c); 7584 } 7585 7586 /* 7587 * Reset to the memory address rather than 7588 * the typeref array, then let the BYREF 7589 * code below do the work to store the 7590 * memory data in the buffer. 7591 */ 7592 val = typeref[0]; 7593 break; 7594 } 7595 7596 case DTRACEACT_CHILL: 7597 if (dtrace_priv_kernel_destructive(state)) 7598 dtrace_action_chill(&mstate, val); 7599 continue; 7600 7601 case DTRACEACT_RAISE: 7602 if (dtrace_priv_proc_destructive(state)) 7603 dtrace_action_raise(val); 7604 continue; 7605 7606 case DTRACEACT_COMMIT: 7607 ASSERT(!committed); 7608 7609 /* 7610 * We need to commit our buffer state. 7611 */ 7612 if (ecb->dte_size) 7613 buf->dtb_offset = offs + ecb->dte_size; 7614 buf = &state->dts_buffer[cpuid]; 7615 dtrace_speculation_commit(state, cpuid, val); 7616 committed = 1; 7617 continue; 7618 7619 case DTRACEACT_DISCARD: 7620 dtrace_speculation_discard(state, cpuid, val); 7621 continue; 7622 7623 case DTRACEACT_DIFEXPR: 7624 case DTRACEACT_LIBACT: 7625 case DTRACEACT_PRINTF: 7626 case DTRACEACT_PRINTA: 7627 case DTRACEACT_SYSTEM: 7628 case DTRACEACT_FREOPEN: 7629 case DTRACEACT_TRACEMEM: 7630 break; 7631 7632 case DTRACEACT_TRACEMEM_DYNSIZE: 7633 tracememsize = val; 7634 break; 7635 7636 case DTRACEACT_SYM: 7637 case DTRACEACT_MOD: 7638 if (!dtrace_priv_kernel(state)) 7639 continue; 7640 break; 7641 7642 case DTRACEACT_USYM: 7643 case DTRACEACT_UMOD: 7644 case DTRACEACT_UADDR: { 7645 #ifdef illumos 7646 struct pid *pid = curthread->t_procp->p_pidp; 7647 #endif 7648 7649 if (!dtrace_priv_proc(state)) 7650 continue; 7651 7652 DTRACE_STORE(uint64_t, tomax, 7653 #ifdef illumos 7654 valoffs, (uint64_t)pid->pid_id); 7655 #else 7656 valoffs, (uint64_t) curproc->p_pid); 7657 #endif 7658 DTRACE_STORE(uint64_t, tomax, 7659 valoffs + sizeof (uint64_t), val); 7660 7661 continue; 7662 } 7663 7664 case DTRACEACT_EXIT: { 7665 /* 7666 * For the exit action, we are going to attempt 7667 * to atomically set our activity to be 7668 * draining. If this fails (either because 7669 * another CPU has beat us to the exit action, 7670 * or because our current activity is something 7671 * other than ACTIVE or WARMUP), we will 7672 * continue. This assures that the exit action 7673 * can be successfully recorded at most once 7674 * when we're in the ACTIVE state. If we're 7675 * encountering the exit() action while in 7676 * COOLDOWN, however, we want to honor the new 7677 * status code. (We know that we're the only 7678 * thread in COOLDOWN, so there is no race.) 7679 */ 7680 void *activity = &state->dts_activity; 7681 dtrace_activity_t current = state->dts_activity; 7682 7683 if (current == DTRACE_ACTIVITY_COOLDOWN) 7684 break; 7685 7686 if (current != DTRACE_ACTIVITY_WARMUP) 7687 current = DTRACE_ACTIVITY_ACTIVE; 7688 7689 if (dtrace_cas32(activity, current, 7690 DTRACE_ACTIVITY_DRAINING) != current) { 7691 *flags |= CPU_DTRACE_DROP; 7692 continue; 7693 } 7694 7695 break; 7696 } 7697 7698 default: 7699 ASSERT(0); 7700 } 7701 7702 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF || 7703 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) { 7704 uintptr_t end = valoffs + size; 7705 7706 if (tracememsize != 0 && 7707 valoffs + tracememsize < end) { 7708 end = valoffs + tracememsize; 7709 tracememsize = 0; 7710 } 7711 7712 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF && 7713 !dtrace_vcanload((void *)(uintptr_t)val, 7714 &dp->dtdo_rtype, &mstate, vstate)) 7715 continue; 7716 7717 dtrace_store_by_ref(dp, tomax, size, &valoffs, 7718 &val, end, act->dta_intuple, 7719 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ? 7720 DIF_TF_BYREF: DIF_TF_BYUREF); 7721 continue; 7722 } 7723 7724 switch (size) { 7725 case 0: 7726 break; 7727 7728 case sizeof (uint8_t): 7729 DTRACE_STORE(uint8_t, tomax, valoffs, val); 7730 break; 7731 case sizeof (uint16_t): 7732 DTRACE_STORE(uint16_t, tomax, valoffs, val); 7733 break; 7734 case sizeof (uint32_t): 7735 DTRACE_STORE(uint32_t, tomax, valoffs, val); 7736 break; 7737 case sizeof (uint64_t): 7738 DTRACE_STORE(uint64_t, tomax, valoffs, val); 7739 break; 7740 default: 7741 /* 7742 * Any other size should have been returned by 7743 * reference, not by value. 7744 */ 7745 ASSERT(0); 7746 break; 7747 } 7748 } 7749 7750 if (*flags & CPU_DTRACE_DROP) 7751 continue; 7752 7753 if (*flags & CPU_DTRACE_FAULT) { 7754 int ndx; 7755 dtrace_action_t *err; 7756 7757 buf->dtb_errors++; 7758 7759 if (probe->dtpr_id == dtrace_probeid_error) { 7760 /* 7761 * There's nothing we can do -- we had an 7762 * error on the error probe. We bump an 7763 * error counter to at least indicate that 7764 * this condition happened. 7765 */ 7766 dtrace_error(&state->dts_dblerrors); 7767 continue; 7768 } 7769 7770 if (vtime) { 7771 /* 7772 * Before recursing on dtrace_probe(), we 7773 * need to explicitly clear out our start 7774 * time to prevent it from being accumulated 7775 * into t_dtrace_vtime. 7776 */ 7777 curthread->t_dtrace_start = 0; 7778 } 7779 7780 /* 7781 * Iterate over the actions to figure out which action 7782 * we were processing when we experienced the error. 7783 * Note that act points _past_ the faulting action; if 7784 * act is ecb->dte_action, the fault was in the 7785 * predicate, if it's ecb->dte_action->dta_next it's 7786 * in action #1, and so on. 7787 */ 7788 for (err = ecb->dte_action, ndx = 0; 7789 err != act; err = err->dta_next, ndx++) 7790 continue; 7791 7792 dtrace_probe_error(state, ecb->dte_epid, ndx, 7793 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 7794 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 7795 cpu_core[cpuid].cpuc_dtrace_illval); 7796 7797 continue; 7798 } 7799 7800 if (!committed) 7801 buf->dtb_offset = offs + ecb->dte_size; 7802 } 7803 7804 if (vtime) 7805 curthread->t_dtrace_start = dtrace_gethrtime(); 7806 7807 dtrace_interrupt_enable(cookie); 7808 } 7809 7810 /* 7811 * DTrace Probe Hashing Functions 7812 * 7813 * The functions in this section (and indeed, the functions in remaining 7814 * sections) are not _called_ from probe context. (Any exceptions to this are 7815 * marked with a "Note:".) Rather, they are called from elsewhere in the 7816 * DTrace framework to look-up probes in, add probes to and remove probes from 7817 * the DTrace probe hashes. (Each probe is hashed by each element of the 7818 * probe tuple -- allowing for fast lookups, regardless of what was 7819 * specified.) 7820 */ 7821 static uint_t 7822 dtrace_hash_str(const char *p) 7823 { 7824 unsigned int g; 7825 uint_t hval = 0; 7826 7827 while (*p) { 7828 hval = (hval << 4) + *p++; 7829 if ((g = (hval & 0xf0000000)) != 0) 7830 hval ^= g >> 24; 7831 hval &= ~g; 7832 } 7833 return (hval); 7834 } 7835 7836 static dtrace_hash_t * 7837 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 7838 { 7839 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 7840 7841 hash->dth_stroffs = stroffs; 7842 hash->dth_nextoffs = nextoffs; 7843 hash->dth_prevoffs = prevoffs; 7844 7845 hash->dth_size = 1; 7846 hash->dth_mask = hash->dth_size - 1; 7847 7848 hash->dth_tab = kmem_zalloc(hash->dth_size * 7849 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 7850 7851 return (hash); 7852 } 7853 7854 static void 7855 dtrace_hash_destroy(dtrace_hash_t *hash) 7856 { 7857 #ifdef DEBUG 7858 int i; 7859 7860 for (i = 0; i < hash->dth_size; i++) 7861 ASSERT(hash->dth_tab[i] == NULL); 7862 #endif 7863 7864 kmem_free(hash->dth_tab, 7865 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 7866 kmem_free(hash, sizeof (dtrace_hash_t)); 7867 } 7868 7869 static void 7870 dtrace_hash_resize(dtrace_hash_t *hash) 7871 { 7872 int size = hash->dth_size, i, ndx; 7873 int new_size = hash->dth_size << 1; 7874 int new_mask = new_size - 1; 7875 dtrace_hashbucket_t **new_tab, *bucket, *next; 7876 7877 ASSERT((new_size & new_mask) == 0); 7878 7879 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 7880 7881 for (i = 0; i < size; i++) { 7882 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 7883 dtrace_probe_t *probe = bucket->dthb_chain; 7884 7885 ASSERT(probe != NULL); 7886 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 7887 7888 next = bucket->dthb_next; 7889 bucket->dthb_next = new_tab[ndx]; 7890 new_tab[ndx] = bucket; 7891 } 7892 } 7893 7894 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 7895 hash->dth_tab = new_tab; 7896 hash->dth_size = new_size; 7897 hash->dth_mask = new_mask; 7898 } 7899 7900 static void 7901 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 7902 { 7903 int hashval = DTRACE_HASHSTR(hash, new); 7904 int ndx = hashval & hash->dth_mask; 7905 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7906 dtrace_probe_t **nextp, **prevp; 7907 7908 for (; bucket != NULL; bucket = bucket->dthb_next) { 7909 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 7910 goto add; 7911 } 7912 7913 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 7914 dtrace_hash_resize(hash); 7915 dtrace_hash_add(hash, new); 7916 return; 7917 } 7918 7919 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 7920 bucket->dthb_next = hash->dth_tab[ndx]; 7921 hash->dth_tab[ndx] = bucket; 7922 hash->dth_nbuckets++; 7923 7924 add: 7925 nextp = DTRACE_HASHNEXT(hash, new); 7926 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 7927 *nextp = bucket->dthb_chain; 7928 7929 if (bucket->dthb_chain != NULL) { 7930 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 7931 ASSERT(*prevp == NULL); 7932 *prevp = new; 7933 } 7934 7935 bucket->dthb_chain = new; 7936 bucket->dthb_len++; 7937 } 7938 7939 static dtrace_probe_t * 7940 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 7941 { 7942 int hashval = DTRACE_HASHSTR(hash, template); 7943 int ndx = hashval & hash->dth_mask; 7944 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7945 7946 for (; bucket != NULL; bucket = bucket->dthb_next) { 7947 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 7948 return (bucket->dthb_chain); 7949 } 7950 7951 return (NULL); 7952 } 7953 7954 static int 7955 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 7956 { 7957 int hashval = DTRACE_HASHSTR(hash, template); 7958 int ndx = hashval & hash->dth_mask; 7959 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7960 7961 for (; bucket != NULL; bucket = bucket->dthb_next) { 7962 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 7963 return (bucket->dthb_len); 7964 } 7965 7966 return (0); 7967 } 7968 7969 static void 7970 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 7971 { 7972 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 7973 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7974 7975 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 7976 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 7977 7978 /* 7979 * Find the bucket that we're removing this probe from. 7980 */ 7981 for (; bucket != NULL; bucket = bucket->dthb_next) { 7982 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 7983 break; 7984 } 7985 7986 ASSERT(bucket != NULL); 7987 7988 if (*prevp == NULL) { 7989 if (*nextp == NULL) { 7990 /* 7991 * The removed probe was the only probe on this 7992 * bucket; we need to remove the bucket. 7993 */ 7994 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 7995 7996 ASSERT(bucket->dthb_chain == probe); 7997 ASSERT(b != NULL); 7998 7999 if (b == bucket) { 8000 hash->dth_tab[ndx] = bucket->dthb_next; 8001 } else { 8002 while (b->dthb_next != bucket) 8003 b = b->dthb_next; 8004 b->dthb_next = bucket->dthb_next; 8005 } 8006 8007 ASSERT(hash->dth_nbuckets > 0); 8008 hash->dth_nbuckets--; 8009 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 8010 return; 8011 } 8012 8013 bucket->dthb_chain = *nextp; 8014 } else { 8015 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 8016 } 8017 8018 if (*nextp != NULL) 8019 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 8020 } 8021 8022 /* 8023 * DTrace Utility Functions 8024 * 8025 * These are random utility functions that are _not_ called from probe context. 8026 */ 8027 static int 8028 dtrace_badattr(const dtrace_attribute_t *a) 8029 { 8030 return (a->dtat_name > DTRACE_STABILITY_MAX || 8031 a->dtat_data > DTRACE_STABILITY_MAX || 8032 a->dtat_class > DTRACE_CLASS_MAX); 8033 } 8034 8035 /* 8036 * Return a duplicate copy of a string. If the specified string is NULL, 8037 * this function returns a zero-length string. 8038 */ 8039 static char * 8040 dtrace_strdup(const char *str) 8041 { 8042 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 8043 8044 if (str != NULL) 8045 (void) strcpy(new, str); 8046 8047 return (new); 8048 } 8049 8050 #define DTRACE_ISALPHA(c) \ 8051 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 8052 8053 static int 8054 dtrace_badname(const char *s) 8055 { 8056 char c; 8057 8058 if (s == NULL || (c = *s++) == '\0') 8059 return (0); 8060 8061 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 8062 return (1); 8063 8064 while ((c = *s++) != '\0') { 8065 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 8066 c != '-' && c != '_' && c != '.' && c != '`') 8067 return (1); 8068 } 8069 8070 return (0); 8071 } 8072 8073 static void 8074 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 8075 { 8076 uint32_t priv; 8077 8078 #ifdef illumos 8079 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 8080 /* 8081 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 8082 */ 8083 priv = DTRACE_PRIV_ALL; 8084 } else { 8085 *uidp = crgetuid(cr); 8086 *zoneidp = crgetzoneid(cr); 8087 8088 priv = 0; 8089 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 8090 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 8091 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 8092 priv |= DTRACE_PRIV_USER; 8093 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 8094 priv |= DTRACE_PRIV_PROC; 8095 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 8096 priv |= DTRACE_PRIV_OWNER; 8097 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 8098 priv |= DTRACE_PRIV_ZONEOWNER; 8099 } 8100 #else 8101 priv = DTRACE_PRIV_ALL; 8102 #endif 8103 8104 *privp = priv; 8105 } 8106 8107 #ifdef DTRACE_ERRDEBUG 8108 static void 8109 dtrace_errdebug(const char *str) 8110 { 8111 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 8112 int occupied = 0; 8113 8114 mutex_enter(&dtrace_errlock); 8115 dtrace_errlast = str; 8116 dtrace_errthread = curthread; 8117 8118 while (occupied++ < DTRACE_ERRHASHSZ) { 8119 if (dtrace_errhash[hval].dter_msg == str) { 8120 dtrace_errhash[hval].dter_count++; 8121 goto out; 8122 } 8123 8124 if (dtrace_errhash[hval].dter_msg != NULL) { 8125 hval = (hval + 1) % DTRACE_ERRHASHSZ; 8126 continue; 8127 } 8128 8129 dtrace_errhash[hval].dter_msg = str; 8130 dtrace_errhash[hval].dter_count = 1; 8131 goto out; 8132 } 8133 8134 panic("dtrace: undersized error hash"); 8135 out: 8136 mutex_exit(&dtrace_errlock); 8137 } 8138 #endif 8139 8140 /* 8141 * DTrace Matching Functions 8142 * 8143 * These functions are used to match groups of probes, given some elements of 8144 * a probe tuple, or some globbed expressions for elements of a probe tuple. 8145 */ 8146 static int 8147 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 8148 zoneid_t zoneid) 8149 { 8150 if (priv != DTRACE_PRIV_ALL) { 8151 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 8152 uint32_t match = priv & ppriv; 8153 8154 /* 8155 * No PRIV_DTRACE_* privileges... 8156 */ 8157 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 8158 DTRACE_PRIV_KERNEL)) == 0) 8159 return (0); 8160 8161 /* 8162 * No matching bits, but there were bits to match... 8163 */ 8164 if (match == 0 && ppriv != 0) 8165 return (0); 8166 8167 /* 8168 * Need to have permissions to the process, but don't... 8169 */ 8170 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 8171 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 8172 return (0); 8173 } 8174 8175 /* 8176 * Need to be in the same zone unless we possess the 8177 * privilege to examine all zones. 8178 */ 8179 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 8180 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 8181 return (0); 8182 } 8183 } 8184 8185 return (1); 8186 } 8187 8188 /* 8189 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 8190 * consists of input pattern strings and an ops-vector to evaluate them. 8191 * This function returns >0 for match, 0 for no match, and <0 for error. 8192 */ 8193 static int 8194 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 8195 uint32_t priv, uid_t uid, zoneid_t zoneid) 8196 { 8197 dtrace_provider_t *pvp = prp->dtpr_provider; 8198 int rv; 8199 8200 if (pvp->dtpv_defunct) 8201 return (0); 8202 8203 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 8204 return (rv); 8205 8206 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 8207 return (rv); 8208 8209 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 8210 return (rv); 8211 8212 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 8213 return (rv); 8214 8215 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 8216 return (0); 8217 8218 return (rv); 8219 } 8220 8221 /* 8222 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 8223 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 8224 * libc's version, the kernel version only applies to 8-bit ASCII strings. 8225 * In addition, all of the recursion cases except for '*' matching have been 8226 * unwound. For '*', we still implement recursive evaluation, but a depth 8227 * counter is maintained and matching is aborted if we recurse too deep. 8228 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 8229 */ 8230 static int 8231 dtrace_match_glob(const char *s, const char *p, int depth) 8232 { 8233 const char *olds; 8234 char s1, c; 8235 int gs; 8236 8237 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 8238 return (-1); 8239 8240 if (s == NULL) 8241 s = ""; /* treat NULL as empty string */ 8242 8243 top: 8244 olds = s; 8245 s1 = *s++; 8246 8247 if (p == NULL) 8248 return (0); 8249 8250 if ((c = *p++) == '\0') 8251 return (s1 == '\0'); 8252 8253 switch (c) { 8254 case '[': { 8255 int ok = 0, notflag = 0; 8256 char lc = '\0'; 8257 8258 if (s1 == '\0') 8259 return (0); 8260 8261 if (*p == '!') { 8262 notflag = 1; 8263 p++; 8264 } 8265 8266 if ((c = *p++) == '\0') 8267 return (0); 8268 8269 do { 8270 if (c == '-' && lc != '\0' && *p != ']') { 8271 if ((c = *p++) == '\0') 8272 return (0); 8273 if (c == '\\' && (c = *p++) == '\0') 8274 return (0); 8275 8276 if (notflag) { 8277 if (s1 < lc || s1 > c) 8278 ok++; 8279 else 8280 return (0); 8281 } else if (lc <= s1 && s1 <= c) 8282 ok++; 8283 8284 } else if (c == '\\' && (c = *p++) == '\0') 8285 return (0); 8286 8287 lc = c; /* save left-hand 'c' for next iteration */ 8288 8289 if (notflag) { 8290 if (s1 != c) 8291 ok++; 8292 else 8293 return (0); 8294 } else if (s1 == c) 8295 ok++; 8296 8297 if ((c = *p++) == '\0') 8298 return (0); 8299 8300 } while (c != ']'); 8301 8302 if (ok) 8303 goto top; 8304 8305 return (0); 8306 } 8307 8308 case '\\': 8309 if ((c = *p++) == '\0') 8310 return (0); 8311 /*FALLTHRU*/ 8312 8313 default: 8314 if (c != s1) 8315 return (0); 8316 /*FALLTHRU*/ 8317 8318 case '?': 8319 if (s1 != '\0') 8320 goto top; 8321 return (0); 8322 8323 case '*': 8324 while (*p == '*') 8325 p++; /* consecutive *'s are identical to a single one */ 8326 8327 if (*p == '\0') 8328 return (1); 8329 8330 for (s = olds; *s != '\0'; s++) { 8331 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 8332 return (gs); 8333 } 8334 8335 return (0); 8336 } 8337 } 8338 8339 /*ARGSUSED*/ 8340 static int 8341 dtrace_match_string(const char *s, const char *p, int depth) 8342 { 8343 return (s != NULL && strcmp(s, p) == 0); 8344 } 8345 8346 /*ARGSUSED*/ 8347 static int 8348 dtrace_match_nul(const char *s, const char *p, int depth) 8349 { 8350 return (1); /* always match the empty pattern */ 8351 } 8352 8353 /*ARGSUSED*/ 8354 static int 8355 dtrace_match_nonzero(const char *s, const char *p, int depth) 8356 { 8357 return (s != NULL && s[0] != '\0'); 8358 } 8359 8360 static int 8361 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 8362 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 8363 { 8364 dtrace_probe_t template, *probe; 8365 dtrace_hash_t *hash = NULL; 8366 int len, best = INT_MAX, nmatched = 0; 8367 dtrace_id_t i; 8368 8369 ASSERT(MUTEX_HELD(&dtrace_lock)); 8370 8371 /* 8372 * If the probe ID is specified in the key, just lookup by ID and 8373 * invoke the match callback once if a matching probe is found. 8374 */ 8375 if (pkp->dtpk_id != DTRACE_IDNONE) { 8376 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 8377 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 8378 (void) (*matched)(probe, arg); 8379 nmatched++; 8380 } 8381 return (nmatched); 8382 } 8383 8384 template.dtpr_mod = (char *)pkp->dtpk_mod; 8385 template.dtpr_func = (char *)pkp->dtpk_func; 8386 template.dtpr_name = (char *)pkp->dtpk_name; 8387 8388 /* 8389 * We want to find the most distinct of the module name, function 8390 * name, and name. So for each one that is not a glob pattern or 8391 * empty string, we perform a lookup in the corresponding hash and 8392 * use the hash table with the fewest collisions to do our search. 8393 */ 8394 if (pkp->dtpk_mmatch == &dtrace_match_string && 8395 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 8396 best = len; 8397 hash = dtrace_bymod; 8398 } 8399 8400 if (pkp->dtpk_fmatch == &dtrace_match_string && 8401 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 8402 best = len; 8403 hash = dtrace_byfunc; 8404 } 8405 8406 if (pkp->dtpk_nmatch == &dtrace_match_string && 8407 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 8408 best = len; 8409 hash = dtrace_byname; 8410 } 8411 8412 /* 8413 * If we did not select a hash table, iterate over every probe and 8414 * invoke our callback for each one that matches our input probe key. 8415 */ 8416 if (hash == NULL) { 8417 for (i = 0; i < dtrace_nprobes; i++) { 8418 if ((probe = dtrace_probes[i]) == NULL || 8419 dtrace_match_probe(probe, pkp, priv, uid, 8420 zoneid) <= 0) 8421 continue; 8422 8423 nmatched++; 8424 8425 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8426 break; 8427 } 8428 8429 return (nmatched); 8430 } 8431 8432 /* 8433 * If we selected a hash table, iterate over each probe of the same key 8434 * name and invoke the callback for every probe that matches the other 8435 * attributes of our input probe key. 8436 */ 8437 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 8438 probe = *(DTRACE_HASHNEXT(hash, probe))) { 8439 8440 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 8441 continue; 8442 8443 nmatched++; 8444 8445 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8446 break; 8447 } 8448 8449 return (nmatched); 8450 } 8451 8452 /* 8453 * Return the function pointer dtrace_probecmp() should use to compare the 8454 * specified pattern with a string. For NULL or empty patterns, we select 8455 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 8456 * For non-empty non-glob strings, we use dtrace_match_string(). 8457 */ 8458 static dtrace_probekey_f * 8459 dtrace_probekey_func(const char *p) 8460 { 8461 char c; 8462 8463 if (p == NULL || *p == '\0') 8464 return (&dtrace_match_nul); 8465 8466 while ((c = *p++) != '\0') { 8467 if (c == '[' || c == '?' || c == '*' || c == '\\') 8468 return (&dtrace_match_glob); 8469 } 8470 8471 return (&dtrace_match_string); 8472 } 8473 8474 /* 8475 * Build a probe comparison key for use with dtrace_match_probe() from the 8476 * given probe description. By convention, a null key only matches anchored 8477 * probes: if each field is the empty string, reset dtpk_fmatch to 8478 * dtrace_match_nonzero(). 8479 */ 8480 static void 8481 dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 8482 { 8483 pkp->dtpk_prov = pdp->dtpd_provider; 8484 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 8485 8486 pkp->dtpk_mod = pdp->dtpd_mod; 8487 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 8488 8489 pkp->dtpk_func = pdp->dtpd_func; 8490 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 8491 8492 pkp->dtpk_name = pdp->dtpd_name; 8493 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 8494 8495 pkp->dtpk_id = pdp->dtpd_id; 8496 8497 if (pkp->dtpk_id == DTRACE_IDNONE && 8498 pkp->dtpk_pmatch == &dtrace_match_nul && 8499 pkp->dtpk_mmatch == &dtrace_match_nul && 8500 pkp->dtpk_fmatch == &dtrace_match_nul && 8501 pkp->dtpk_nmatch == &dtrace_match_nul) 8502 pkp->dtpk_fmatch = &dtrace_match_nonzero; 8503 } 8504 8505 /* 8506 * DTrace Provider-to-Framework API Functions 8507 * 8508 * These functions implement much of the Provider-to-Framework API, as 8509 * described in <sys/dtrace.h>. The parts of the API not in this section are 8510 * the functions in the API for probe management (found below), and 8511 * dtrace_probe() itself (found above). 8512 */ 8513 8514 /* 8515 * Register the calling provider with the DTrace framework. This should 8516 * generally be called by DTrace providers in their attach(9E) entry point. 8517 */ 8518 int 8519 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 8520 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 8521 { 8522 dtrace_provider_t *provider; 8523 8524 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 8525 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8526 "arguments", name ? name : "<NULL>"); 8527 return (EINVAL); 8528 } 8529 8530 if (name[0] == '\0' || dtrace_badname(name)) { 8531 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8532 "provider name", name); 8533 return (EINVAL); 8534 } 8535 8536 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 8537 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 8538 pops->dtps_destroy == NULL || 8539 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 8540 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8541 "provider ops", name); 8542 return (EINVAL); 8543 } 8544 8545 if (dtrace_badattr(&pap->dtpa_provider) || 8546 dtrace_badattr(&pap->dtpa_mod) || 8547 dtrace_badattr(&pap->dtpa_func) || 8548 dtrace_badattr(&pap->dtpa_name) || 8549 dtrace_badattr(&pap->dtpa_args)) { 8550 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8551 "provider attributes", name); 8552 return (EINVAL); 8553 } 8554 8555 if (priv & ~DTRACE_PRIV_ALL) { 8556 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8557 "privilege attributes", name); 8558 return (EINVAL); 8559 } 8560 8561 if ((priv & DTRACE_PRIV_KERNEL) && 8562 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 8563 pops->dtps_usermode == NULL) { 8564 cmn_err(CE_WARN, "failed to register provider '%s': need " 8565 "dtps_usermode() op for given privilege attributes", name); 8566 return (EINVAL); 8567 } 8568 8569 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 8570 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8571 (void) strcpy(provider->dtpv_name, name); 8572 8573 provider->dtpv_attr = *pap; 8574 provider->dtpv_priv.dtpp_flags = priv; 8575 if (cr != NULL) { 8576 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 8577 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 8578 } 8579 provider->dtpv_pops = *pops; 8580 8581 if (pops->dtps_provide == NULL) { 8582 ASSERT(pops->dtps_provide_module != NULL); 8583 provider->dtpv_pops.dtps_provide = 8584 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 8585 } 8586 8587 if (pops->dtps_provide_module == NULL) { 8588 ASSERT(pops->dtps_provide != NULL); 8589 provider->dtpv_pops.dtps_provide_module = 8590 (void (*)(void *, modctl_t *))dtrace_nullop; 8591 } 8592 8593 if (pops->dtps_suspend == NULL) { 8594 ASSERT(pops->dtps_resume == NULL); 8595 provider->dtpv_pops.dtps_suspend = 8596 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8597 provider->dtpv_pops.dtps_resume = 8598 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8599 } 8600 8601 provider->dtpv_arg = arg; 8602 *idp = (dtrace_provider_id_t)provider; 8603 8604 if (pops == &dtrace_provider_ops) { 8605 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8606 ASSERT(MUTEX_HELD(&dtrace_lock)); 8607 ASSERT(dtrace_anon.dta_enabling == NULL); 8608 8609 /* 8610 * We make sure that the DTrace provider is at the head of 8611 * the provider chain. 8612 */ 8613 provider->dtpv_next = dtrace_provider; 8614 dtrace_provider = provider; 8615 return (0); 8616 } 8617 8618 mutex_enter(&dtrace_provider_lock); 8619 mutex_enter(&dtrace_lock); 8620 8621 /* 8622 * If there is at least one provider registered, we'll add this 8623 * provider after the first provider. 8624 */ 8625 if (dtrace_provider != NULL) { 8626 provider->dtpv_next = dtrace_provider->dtpv_next; 8627 dtrace_provider->dtpv_next = provider; 8628 } else { 8629 dtrace_provider = provider; 8630 } 8631 8632 if (dtrace_retained != NULL) { 8633 dtrace_enabling_provide(provider); 8634 8635 /* 8636 * Now we need to call dtrace_enabling_matchall() -- which 8637 * will acquire cpu_lock and dtrace_lock. We therefore need 8638 * to drop all of our locks before calling into it... 8639 */ 8640 mutex_exit(&dtrace_lock); 8641 mutex_exit(&dtrace_provider_lock); 8642 dtrace_enabling_matchall(); 8643 8644 return (0); 8645 } 8646 8647 mutex_exit(&dtrace_lock); 8648 mutex_exit(&dtrace_provider_lock); 8649 8650 return (0); 8651 } 8652 8653 /* 8654 * Unregister the specified provider from the DTrace framework. This should 8655 * generally be called by DTrace providers in their detach(9E) entry point. 8656 */ 8657 int 8658 dtrace_unregister(dtrace_provider_id_t id) 8659 { 8660 dtrace_provider_t *old = (dtrace_provider_t *)id; 8661 dtrace_provider_t *prev = NULL; 8662 int i, self = 0, noreap = 0; 8663 dtrace_probe_t *probe, *first = NULL; 8664 8665 if (old->dtpv_pops.dtps_enable == 8666 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 8667 /* 8668 * If DTrace itself is the provider, we're called with locks 8669 * already held. 8670 */ 8671 ASSERT(old == dtrace_provider); 8672 #ifdef illumos 8673 ASSERT(dtrace_devi != NULL); 8674 #endif 8675 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8676 ASSERT(MUTEX_HELD(&dtrace_lock)); 8677 self = 1; 8678 8679 if (dtrace_provider->dtpv_next != NULL) { 8680 /* 8681 * There's another provider here; return failure. 8682 */ 8683 return (EBUSY); 8684 } 8685 } else { 8686 mutex_enter(&dtrace_provider_lock); 8687 #ifdef illumos 8688 mutex_enter(&mod_lock); 8689 #endif 8690 mutex_enter(&dtrace_lock); 8691 } 8692 8693 /* 8694 * If anyone has /dev/dtrace open, or if there are anonymous enabled 8695 * probes, we refuse to let providers slither away, unless this 8696 * provider has already been explicitly invalidated. 8697 */ 8698 if (!old->dtpv_defunct && 8699 (dtrace_opens || (dtrace_anon.dta_state != NULL && 8700 dtrace_anon.dta_state->dts_necbs > 0))) { 8701 if (!self) { 8702 mutex_exit(&dtrace_lock); 8703 #ifdef illumos 8704 mutex_exit(&mod_lock); 8705 #endif 8706 mutex_exit(&dtrace_provider_lock); 8707 } 8708 return (EBUSY); 8709 } 8710 8711 /* 8712 * Attempt to destroy the probes associated with this provider. 8713 */ 8714 for (i = 0; i < dtrace_nprobes; i++) { 8715 if ((probe = dtrace_probes[i]) == NULL) 8716 continue; 8717 8718 if (probe->dtpr_provider != old) 8719 continue; 8720 8721 if (probe->dtpr_ecb == NULL) 8722 continue; 8723 8724 /* 8725 * If we are trying to unregister a defunct provider, and the 8726 * provider was made defunct within the interval dictated by 8727 * dtrace_unregister_defunct_reap, we'll (asynchronously) 8728 * attempt to reap our enablings. To denote that the provider 8729 * should reattempt to unregister itself at some point in the 8730 * future, we will return a differentiable error code (EAGAIN 8731 * instead of EBUSY) in this case. 8732 */ 8733 if (dtrace_gethrtime() - old->dtpv_defunct > 8734 dtrace_unregister_defunct_reap) 8735 noreap = 1; 8736 8737 if (!self) { 8738 mutex_exit(&dtrace_lock); 8739 #ifdef illumos 8740 mutex_exit(&mod_lock); 8741 #endif 8742 mutex_exit(&dtrace_provider_lock); 8743 } 8744 8745 if (noreap) 8746 return (EBUSY); 8747 8748 (void) taskq_dispatch(dtrace_taskq, 8749 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 8750 8751 return (EAGAIN); 8752 } 8753 8754 /* 8755 * All of the probes for this provider are disabled; we can safely 8756 * remove all of them from their hash chains and from the probe array. 8757 */ 8758 for (i = 0; i < dtrace_nprobes; i++) { 8759 if ((probe = dtrace_probes[i]) == NULL) 8760 continue; 8761 8762 if (probe->dtpr_provider != old) 8763 continue; 8764 8765 dtrace_probes[i] = NULL; 8766 8767 dtrace_hash_remove(dtrace_bymod, probe); 8768 dtrace_hash_remove(dtrace_byfunc, probe); 8769 dtrace_hash_remove(dtrace_byname, probe); 8770 8771 if (first == NULL) { 8772 first = probe; 8773 probe->dtpr_nextmod = NULL; 8774 } else { 8775 probe->dtpr_nextmod = first; 8776 first = probe; 8777 } 8778 } 8779 8780 /* 8781 * The provider's probes have been removed from the hash chains and 8782 * from the probe array. Now issue a dtrace_sync() to be sure that 8783 * everyone has cleared out from any probe array processing. 8784 */ 8785 dtrace_sync(); 8786 8787 for (probe = first; probe != NULL; probe = first) { 8788 first = probe->dtpr_nextmod; 8789 8790 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 8791 probe->dtpr_arg); 8792 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8793 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8794 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8795 #ifdef illumos 8796 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 8797 #else 8798 free_unr(dtrace_arena, probe->dtpr_id); 8799 #endif 8800 kmem_free(probe, sizeof (dtrace_probe_t)); 8801 } 8802 8803 if ((prev = dtrace_provider) == old) { 8804 #ifdef illumos 8805 ASSERT(self || dtrace_devi == NULL); 8806 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 8807 #endif 8808 dtrace_provider = old->dtpv_next; 8809 } else { 8810 while (prev != NULL && prev->dtpv_next != old) 8811 prev = prev->dtpv_next; 8812 8813 if (prev == NULL) { 8814 panic("attempt to unregister non-existent " 8815 "dtrace provider %p\n", (void *)id); 8816 } 8817 8818 prev->dtpv_next = old->dtpv_next; 8819 } 8820 8821 if (!self) { 8822 mutex_exit(&dtrace_lock); 8823 #ifdef illumos 8824 mutex_exit(&mod_lock); 8825 #endif 8826 mutex_exit(&dtrace_provider_lock); 8827 } 8828 8829 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 8830 kmem_free(old, sizeof (dtrace_provider_t)); 8831 8832 return (0); 8833 } 8834 8835 /* 8836 * Invalidate the specified provider. All subsequent probe lookups for the 8837 * specified provider will fail, but its probes will not be removed. 8838 */ 8839 void 8840 dtrace_invalidate(dtrace_provider_id_t id) 8841 { 8842 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 8843 8844 ASSERT(pvp->dtpv_pops.dtps_enable != 8845 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8846 8847 mutex_enter(&dtrace_provider_lock); 8848 mutex_enter(&dtrace_lock); 8849 8850 pvp->dtpv_defunct = dtrace_gethrtime(); 8851 8852 mutex_exit(&dtrace_lock); 8853 mutex_exit(&dtrace_provider_lock); 8854 } 8855 8856 /* 8857 * Indicate whether or not DTrace has attached. 8858 */ 8859 int 8860 dtrace_attached(void) 8861 { 8862 /* 8863 * dtrace_provider will be non-NULL iff the DTrace driver has 8864 * attached. (It's non-NULL because DTrace is always itself a 8865 * provider.) 8866 */ 8867 return (dtrace_provider != NULL); 8868 } 8869 8870 /* 8871 * Remove all the unenabled probes for the given provider. This function is 8872 * not unlike dtrace_unregister(), except that it doesn't remove the provider 8873 * -- just as many of its associated probes as it can. 8874 */ 8875 int 8876 dtrace_condense(dtrace_provider_id_t id) 8877 { 8878 dtrace_provider_t *prov = (dtrace_provider_t *)id; 8879 int i; 8880 dtrace_probe_t *probe; 8881 8882 /* 8883 * Make sure this isn't the dtrace provider itself. 8884 */ 8885 ASSERT(prov->dtpv_pops.dtps_enable != 8886 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8887 8888 mutex_enter(&dtrace_provider_lock); 8889 mutex_enter(&dtrace_lock); 8890 8891 /* 8892 * Attempt to destroy the probes associated with this provider. 8893 */ 8894 for (i = 0; i < dtrace_nprobes; i++) { 8895 if ((probe = dtrace_probes[i]) == NULL) 8896 continue; 8897 8898 if (probe->dtpr_provider != prov) 8899 continue; 8900 8901 if (probe->dtpr_ecb != NULL) 8902 continue; 8903 8904 dtrace_probes[i] = NULL; 8905 8906 dtrace_hash_remove(dtrace_bymod, probe); 8907 dtrace_hash_remove(dtrace_byfunc, probe); 8908 dtrace_hash_remove(dtrace_byname, probe); 8909 8910 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 8911 probe->dtpr_arg); 8912 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8913 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8914 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8915 kmem_free(probe, sizeof (dtrace_probe_t)); 8916 #ifdef illumos 8917 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 8918 #else 8919 free_unr(dtrace_arena, i + 1); 8920 #endif 8921 } 8922 8923 mutex_exit(&dtrace_lock); 8924 mutex_exit(&dtrace_provider_lock); 8925 8926 return (0); 8927 } 8928 8929 /* 8930 * DTrace Probe Management Functions 8931 * 8932 * The functions in this section perform the DTrace probe management, 8933 * including functions to create probes, look-up probes, and call into the 8934 * providers to request that probes be provided. Some of these functions are 8935 * in the Provider-to-Framework API; these functions can be identified by the 8936 * fact that they are not declared "static". 8937 */ 8938 8939 /* 8940 * Create a probe with the specified module name, function name, and name. 8941 */ 8942 dtrace_id_t 8943 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 8944 const char *func, const char *name, int aframes, void *arg) 8945 { 8946 dtrace_probe_t *probe, **probes; 8947 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 8948 dtrace_id_t id; 8949 8950 if (provider == dtrace_provider) { 8951 ASSERT(MUTEX_HELD(&dtrace_lock)); 8952 } else { 8953 mutex_enter(&dtrace_lock); 8954 } 8955 8956 #ifdef illumos 8957 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 8958 VM_BESTFIT | VM_SLEEP); 8959 #else 8960 id = alloc_unr(dtrace_arena); 8961 #endif 8962 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 8963 8964 probe->dtpr_id = id; 8965 probe->dtpr_gen = dtrace_probegen++; 8966 probe->dtpr_mod = dtrace_strdup(mod); 8967 probe->dtpr_func = dtrace_strdup(func); 8968 probe->dtpr_name = dtrace_strdup(name); 8969 probe->dtpr_arg = arg; 8970 probe->dtpr_aframes = aframes; 8971 probe->dtpr_provider = provider; 8972 8973 dtrace_hash_add(dtrace_bymod, probe); 8974 dtrace_hash_add(dtrace_byfunc, probe); 8975 dtrace_hash_add(dtrace_byname, probe); 8976 8977 if (id - 1 >= dtrace_nprobes) { 8978 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 8979 size_t nsize = osize << 1; 8980 8981 if (nsize == 0) { 8982 ASSERT(osize == 0); 8983 ASSERT(dtrace_probes == NULL); 8984 nsize = sizeof (dtrace_probe_t *); 8985 } 8986 8987 probes = kmem_zalloc(nsize, KM_SLEEP); 8988 8989 if (dtrace_probes == NULL) { 8990 ASSERT(osize == 0); 8991 dtrace_probes = probes; 8992 dtrace_nprobes = 1; 8993 } else { 8994 dtrace_probe_t **oprobes = dtrace_probes; 8995 8996 bcopy(oprobes, probes, osize); 8997 dtrace_membar_producer(); 8998 dtrace_probes = probes; 8999 9000 dtrace_sync(); 9001 9002 /* 9003 * All CPUs are now seeing the new probes array; we can 9004 * safely free the old array. 9005 */ 9006 kmem_free(oprobes, osize); 9007 dtrace_nprobes <<= 1; 9008 } 9009 9010 ASSERT(id - 1 < dtrace_nprobes); 9011 } 9012 9013 ASSERT(dtrace_probes[id - 1] == NULL); 9014 dtrace_probes[id - 1] = probe; 9015 9016 if (provider != dtrace_provider) 9017 mutex_exit(&dtrace_lock); 9018 9019 return (id); 9020 } 9021 9022 static dtrace_probe_t * 9023 dtrace_probe_lookup_id(dtrace_id_t id) 9024 { 9025 ASSERT(MUTEX_HELD(&dtrace_lock)); 9026 9027 if (id == 0 || id > dtrace_nprobes) 9028 return (NULL); 9029 9030 return (dtrace_probes[id - 1]); 9031 } 9032 9033 static int 9034 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 9035 { 9036 *((dtrace_id_t *)arg) = probe->dtpr_id; 9037 9038 return (DTRACE_MATCH_DONE); 9039 } 9040 9041 /* 9042 * Look up a probe based on provider and one or more of module name, function 9043 * name and probe name. 9044 */ 9045 dtrace_id_t 9046 dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 9047 char *func, char *name) 9048 { 9049 dtrace_probekey_t pkey; 9050 dtrace_id_t id; 9051 int match; 9052 9053 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 9054 pkey.dtpk_pmatch = &dtrace_match_string; 9055 pkey.dtpk_mod = mod; 9056 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 9057 pkey.dtpk_func = func; 9058 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 9059 pkey.dtpk_name = name; 9060 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 9061 pkey.dtpk_id = DTRACE_IDNONE; 9062 9063 mutex_enter(&dtrace_lock); 9064 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 9065 dtrace_probe_lookup_match, &id); 9066 mutex_exit(&dtrace_lock); 9067 9068 ASSERT(match == 1 || match == 0); 9069 return (match ? id : 0); 9070 } 9071 9072 /* 9073 * Returns the probe argument associated with the specified probe. 9074 */ 9075 void * 9076 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 9077 { 9078 dtrace_probe_t *probe; 9079 void *rval = NULL; 9080 9081 mutex_enter(&dtrace_lock); 9082 9083 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 9084 probe->dtpr_provider == (dtrace_provider_t *)id) 9085 rval = probe->dtpr_arg; 9086 9087 mutex_exit(&dtrace_lock); 9088 9089 return (rval); 9090 } 9091 9092 /* 9093 * Copy a probe into a probe description. 9094 */ 9095 static void 9096 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 9097 { 9098 bzero(pdp, sizeof (dtrace_probedesc_t)); 9099 pdp->dtpd_id = prp->dtpr_id; 9100 9101 (void) strncpy(pdp->dtpd_provider, 9102 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 9103 9104 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 9105 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 9106 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 9107 } 9108 9109 /* 9110 * Called to indicate that a probe -- or probes -- should be provided by a 9111 * specfied provider. If the specified description is NULL, the provider will 9112 * be told to provide all of its probes. (This is done whenever a new 9113 * consumer comes along, or whenever a retained enabling is to be matched.) If 9114 * the specified description is non-NULL, the provider is given the 9115 * opportunity to dynamically provide the specified probe, allowing providers 9116 * to support the creation of probes on-the-fly. (So-called _autocreated_ 9117 * probes.) If the provider is NULL, the operations will be applied to all 9118 * providers; if the provider is non-NULL the operations will only be applied 9119 * to the specified provider. The dtrace_provider_lock must be held, and the 9120 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 9121 * will need to grab the dtrace_lock when it reenters the framework through 9122 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 9123 */ 9124 static void 9125 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 9126 { 9127 #ifdef illumos 9128 modctl_t *ctl; 9129 #endif 9130 int all = 0; 9131 9132 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9133 9134 if (prv == NULL) { 9135 all = 1; 9136 prv = dtrace_provider; 9137 } 9138 9139 do { 9140 /* 9141 * First, call the blanket provide operation. 9142 */ 9143 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 9144 9145 #ifdef illumos 9146 /* 9147 * Now call the per-module provide operation. We will grab 9148 * mod_lock to prevent the list from being modified. Note 9149 * that this also prevents the mod_busy bits from changing. 9150 * (mod_busy can only be changed with mod_lock held.) 9151 */ 9152 mutex_enter(&mod_lock); 9153 9154 ctl = &modules; 9155 do { 9156 if (ctl->mod_busy || ctl->mod_mp == NULL) 9157 continue; 9158 9159 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 9160 9161 } while ((ctl = ctl->mod_next) != &modules); 9162 9163 mutex_exit(&mod_lock); 9164 #endif 9165 } while (all && (prv = prv->dtpv_next) != NULL); 9166 } 9167 9168 #ifdef illumos 9169 /* 9170 * Iterate over each probe, and call the Framework-to-Provider API function 9171 * denoted by offs. 9172 */ 9173 static void 9174 dtrace_probe_foreach(uintptr_t offs) 9175 { 9176 dtrace_provider_t *prov; 9177 void (*func)(void *, dtrace_id_t, void *); 9178 dtrace_probe_t *probe; 9179 dtrace_icookie_t cookie; 9180 int i; 9181 9182 /* 9183 * We disable interrupts to walk through the probe array. This is 9184 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 9185 * won't see stale data. 9186 */ 9187 cookie = dtrace_interrupt_disable(); 9188 9189 for (i = 0; i < dtrace_nprobes; i++) { 9190 if ((probe = dtrace_probes[i]) == NULL) 9191 continue; 9192 9193 if (probe->dtpr_ecb == NULL) { 9194 /* 9195 * This probe isn't enabled -- don't call the function. 9196 */ 9197 continue; 9198 } 9199 9200 prov = probe->dtpr_provider; 9201 func = *((void(**)(void *, dtrace_id_t, void *)) 9202 ((uintptr_t)&prov->dtpv_pops + offs)); 9203 9204 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 9205 } 9206 9207 dtrace_interrupt_enable(cookie); 9208 } 9209 #endif 9210 9211 static int 9212 dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 9213 { 9214 dtrace_probekey_t pkey; 9215 uint32_t priv; 9216 uid_t uid; 9217 zoneid_t zoneid; 9218 9219 ASSERT(MUTEX_HELD(&dtrace_lock)); 9220 dtrace_ecb_create_cache = NULL; 9221 9222 if (desc == NULL) { 9223 /* 9224 * If we're passed a NULL description, we're being asked to 9225 * create an ECB with a NULL probe. 9226 */ 9227 (void) dtrace_ecb_create_enable(NULL, enab); 9228 return (0); 9229 } 9230 9231 dtrace_probekey(desc, &pkey); 9232 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 9233 &priv, &uid, &zoneid); 9234 9235 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 9236 enab)); 9237 } 9238 9239 /* 9240 * DTrace Helper Provider Functions 9241 */ 9242 static void 9243 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 9244 { 9245 attr->dtat_name = DOF_ATTR_NAME(dofattr); 9246 attr->dtat_data = DOF_ATTR_DATA(dofattr); 9247 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 9248 } 9249 9250 static void 9251 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 9252 const dof_provider_t *dofprov, char *strtab) 9253 { 9254 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 9255 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 9256 dofprov->dofpv_provattr); 9257 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 9258 dofprov->dofpv_modattr); 9259 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 9260 dofprov->dofpv_funcattr); 9261 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 9262 dofprov->dofpv_nameattr); 9263 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 9264 dofprov->dofpv_argsattr); 9265 } 9266 9267 static void 9268 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9269 { 9270 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9271 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9272 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 9273 dof_provider_t *provider; 9274 dof_probe_t *probe; 9275 uint32_t *off, *enoff; 9276 uint8_t *arg; 9277 char *strtab; 9278 uint_t i, nprobes; 9279 dtrace_helper_provdesc_t dhpv; 9280 dtrace_helper_probedesc_t dhpb; 9281 dtrace_meta_t *meta = dtrace_meta_pid; 9282 dtrace_mops_t *mops = &meta->dtm_mops; 9283 void *parg; 9284 9285 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9286 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9287 provider->dofpv_strtab * dof->dofh_secsize); 9288 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9289 provider->dofpv_probes * dof->dofh_secsize); 9290 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9291 provider->dofpv_prargs * dof->dofh_secsize); 9292 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9293 provider->dofpv_proffs * dof->dofh_secsize); 9294 9295 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9296 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 9297 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 9298 enoff = NULL; 9299 9300 /* 9301 * See dtrace_helper_provider_validate(). 9302 */ 9303 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 9304 provider->dofpv_prenoffs != DOF_SECT_NONE) { 9305 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9306 provider->dofpv_prenoffs * dof->dofh_secsize); 9307 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 9308 } 9309 9310 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 9311 9312 /* 9313 * Create the provider. 9314 */ 9315 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9316 9317 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 9318 return; 9319 9320 meta->dtm_count++; 9321 9322 /* 9323 * Create the probes. 9324 */ 9325 for (i = 0; i < nprobes; i++) { 9326 probe = (dof_probe_t *)(uintptr_t)(daddr + 9327 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 9328 9329 dhpb.dthpb_mod = dhp->dofhp_mod; 9330 dhpb.dthpb_func = strtab + probe->dofpr_func; 9331 dhpb.dthpb_name = strtab + probe->dofpr_name; 9332 dhpb.dthpb_base = probe->dofpr_addr; 9333 dhpb.dthpb_offs = off + probe->dofpr_offidx; 9334 dhpb.dthpb_noffs = probe->dofpr_noffs; 9335 if (enoff != NULL) { 9336 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 9337 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 9338 } else { 9339 dhpb.dthpb_enoffs = NULL; 9340 dhpb.dthpb_nenoffs = 0; 9341 } 9342 dhpb.dthpb_args = arg + probe->dofpr_argidx; 9343 dhpb.dthpb_nargc = probe->dofpr_nargc; 9344 dhpb.dthpb_xargc = probe->dofpr_xargc; 9345 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 9346 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 9347 9348 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 9349 } 9350 } 9351 9352 static void 9353 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 9354 { 9355 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9356 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9357 int i; 9358 9359 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9360 9361 for (i = 0; i < dof->dofh_secnum; i++) { 9362 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9363 dof->dofh_secoff + i * dof->dofh_secsize); 9364 9365 if (sec->dofs_type != DOF_SECT_PROVIDER) 9366 continue; 9367 9368 dtrace_helper_provide_one(dhp, sec, pid); 9369 } 9370 9371 /* 9372 * We may have just created probes, so we must now rematch against 9373 * any retained enablings. Note that this call will acquire both 9374 * cpu_lock and dtrace_lock; the fact that we are holding 9375 * dtrace_meta_lock now is what defines the ordering with respect to 9376 * these three locks. 9377 */ 9378 dtrace_enabling_matchall(); 9379 } 9380 9381 static void 9382 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9383 { 9384 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9385 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9386 dof_sec_t *str_sec; 9387 dof_provider_t *provider; 9388 char *strtab; 9389 dtrace_helper_provdesc_t dhpv; 9390 dtrace_meta_t *meta = dtrace_meta_pid; 9391 dtrace_mops_t *mops = &meta->dtm_mops; 9392 9393 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9394 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9395 provider->dofpv_strtab * dof->dofh_secsize); 9396 9397 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9398 9399 /* 9400 * Create the provider. 9401 */ 9402 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9403 9404 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 9405 9406 meta->dtm_count--; 9407 } 9408 9409 static void 9410 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 9411 { 9412 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9413 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9414 int i; 9415 9416 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9417 9418 for (i = 0; i < dof->dofh_secnum; i++) { 9419 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9420 dof->dofh_secoff + i * dof->dofh_secsize); 9421 9422 if (sec->dofs_type != DOF_SECT_PROVIDER) 9423 continue; 9424 9425 dtrace_helper_provider_remove_one(dhp, sec, pid); 9426 } 9427 } 9428 9429 /* 9430 * DTrace Meta Provider-to-Framework API Functions 9431 * 9432 * These functions implement the Meta Provider-to-Framework API, as described 9433 * in <sys/dtrace.h>. 9434 */ 9435 int 9436 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 9437 dtrace_meta_provider_id_t *idp) 9438 { 9439 dtrace_meta_t *meta; 9440 dtrace_helpers_t *help, *next; 9441 int i; 9442 9443 *idp = DTRACE_METAPROVNONE; 9444 9445 /* 9446 * We strictly don't need the name, but we hold onto it for 9447 * debuggability. All hail error queues! 9448 */ 9449 if (name == NULL) { 9450 cmn_err(CE_WARN, "failed to register meta-provider: " 9451 "invalid name"); 9452 return (EINVAL); 9453 } 9454 9455 if (mops == NULL || 9456 mops->dtms_create_probe == NULL || 9457 mops->dtms_provide_pid == NULL || 9458 mops->dtms_remove_pid == NULL) { 9459 cmn_err(CE_WARN, "failed to register meta-register %s: " 9460 "invalid ops", name); 9461 return (EINVAL); 9462 } 9463 9464 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 9465 meta->dtm_mops = *mops; 9466 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 9467 (void) strcpy(meta->dtm_name, name); 9468 meta->dtm_arg = arg; 9469 9470 mutex_enter(&dtrace_meta_lock); 9471 mutex_enter(&dtrace_lock); 9472 9473 if (dtrace_meta_pid != NULL) { 9474 mutex_exit(&dtrace_lock); 9475 mutex_exit(&dtrace_meta_lock); 9476 cmn_err(CE_WARN, "failed to register meta-register %s: " 9477 "user-land meta-provider exists", name); 9478 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 9479 kmem_free(meta, sizeof (dtrace_meta_t)); 9480 return (EINVAL); 9481 } 9482 9483 dtrace_meta_pid = meta; 9484 *idp = (dtrace_meta_provider_id_t)meta; 9485 9486 /* 9487 * If there are providers and probes ready to go, pass them 9488 * off to the new meta provider now. 9489 */ 9490 9491 help = dtrace_deferred_pid; 9492 dtrace_deferred_pid = NULL; 9493 9494 mutex_exit(&dtrace_lock); 9495 9496 while (help != NULL) { 9497 for (i = 0; i < help->dthps_nprovs; i++) { 9498 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 9499 help->dthps_pid); 9500 } 9501 9502 next = help->dthps_next; 9503 help->dthps_next = NULL; 9504 help->dthps_prev = NULL; 9505 help->dthps_deferred = 0; 9506 help = next; 9507 } 9508 9509 mutex_exit(&dtrace_meta_lock); 9510 9511 return (0); 9512 } 9513 9514 int 9515 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 9516 { 9517 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 9518 9519 mutex_enter(&dtrace_meta_lock); 9520 mutex_enter(&dtrace_lock); 9521 9522 if (old == dtrace_meta_pid) { 9523 pp = &dtrace_meta_pid; 9524 } else { 9525 panic("attempt to unregister non-existent " 9526 "dtrace meta-provider %p\n", (void *)old); 9527 } 9528 9529 if (old->dtm_count != 0) { 9530 mutex_exit(&dtrace_lock); 9531 mutex_exit(&dtrace_meta_lock); 9532 return (EBUSY); 9533 } 9534 9535 *pp = NULL; 9536 9537 mutex_exit(&dtrace_lock); 9538 mutex_exit(&dtrace_meta_lock); 9539 9540 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 9541 kmem_free(old, sizeof (dtrace_meta_t)); 9542 9543 return (0); 9544 } 9545 9546 9547 /* 9548 * DTrace DIF Object Functions 9549 */ 9550 static int 9551 dtrace_difo_err(uint_t pc, const char *format, ...) 9552 { 9553 if (dtrace_err_verbose) { 9554 va_list alist; 9555 9556 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 9557 va_start(alist, format); 9558 (void) vuprintf(format, alist); 9559 va_end(alist); 9560 } 9561 9562 #ifdef DTRACE_ERRDEBUG 9563 dtrace_errdebug(format); 9564 #endif 9565 return (1); 9566 } 9567 9568 /* 9569 * Validate a DTrace DIF object by checking the IR instructions. The following 9570 * rules are currently enforced by dtrace_difo_validate(): 9571 * 9572 * 1. Each instruction must have a valid opcode 9573 * 2. Each register, string, variable, or subroutine reference must be valid 9574 * 3. No instruction can modify register %r0 (must be zero) 9575 * 4. All instruction reserved bits must be set to zero 9576 * 5. The last instruction must be a "ret" instruction 9577 * 6. All branch targets must reference a valid instruction _after_ the branch 9578 */ 9579 static int 9580 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 9581 cred_t *cr) 9582 { 9583 int err = 0, i; 9584 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9585 int kcheckload; 9586 uint_t pc; 9587 9588 kcheckload = cr == NULL || 9589 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 9590 9591 dp->dtdo_destructive = 0; 9592 9593 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 9594 dif_instr_t instr = dp->dtdo_buf[pc]; 9595 9596 uint_t r1 = DIF_INSTR_R1(instr); 9597 uint_t r2 = DIF_INSTR_R2(instr); 9598 uint_t rd = DIF_INSTR_RD(instr); 9599 uint_t rs = DIF_INSTR_RS(instr); 9600 uint_t label = DIF_INSTR_LABEL(instr); 9601 uint_t v = DIF_INSTR_VAR(instr); 9602 uint_t subr = DIF_INSTR_SUBR(instr); 9603 uint_t type = DIF_INSTR_TYPE(instr); 9604 uint_t op = DIF_INSTR_OP(instr); 9605 9606 switch (op) { 9607 case DIF_OP_OR: 9608 case DIF_OP_XOR: 9609 case DIF_OP_AND: 9610 case DIF_OP_SLL: 9611 case DIF_OP_SRL: 9612 case DIF_OP_SRA: 9613 case DIF_OP_SUB: 9614 case DIF_OP_ADD: 9615 case DIF_OP_MUL: 9616 case DIF_OP_SDIV: 9617 case DIF_OP_UDIV: 9618 case DIF_OP_SREM: 9619 case DIF_OP_UREM: 9620 case DIF_OP_COPYS: 9621 if (r1 >= nregs) 9622 err += efunc(pc, "invalid register %u\n", r1); 9623 if (r2 >= nregs) 9624 err += efunc(pc, "invalid register %u\n", r2); 9625 if (rd >= nregs) 9626 err += efunc(pc, "invalid register %u\n", rd); 9627 if (rd == 0) 9628 err += efunc(pc, "cannot write to %r0\n"); 9629 break; 9630 case DIF_OP_NOT: 9631 case DIF_OP_MOV: 9632 case DIF_OP_ALLOCS: 9633 if (r1 >= nregs) 9634 err += efunc(pc, "invalid register %u\n", r1); 9635 if (r2 != 0) 9636 err += efunc(pc, "non-zero reserved bits\n"); 9637 if (rd >= nregs) 9638 err += efunc(pc, "invalid register %u\n", rd); 9639 if (rd == 0) 9640 err += efunc(pc, "cannot write to %r0\n"); 9641 break; 9642 case DIF_OP_LDSB: 9643 case DIF_OP_LDSH: 9644 case DIF_OP_LDSW: 9645 case DIF_OP_LDUB: 9646 case DIF_OP_LDUH: 9647 case DIF_OP_LDUW: 9648 case DIF_OP_LDX: 9649 if (r1 >= nregs) 9650 err += efunc(pc, "invalid register %u\n", r1); 9651 if (r2 != 0) 9652 err += efunc(pc, "non-zero reserved bits\n"); 9653 if (rd >= nregs) 9654 err += efunc(pc, "invalid register %u\n", rd); 9655 if (rd == 0) 9656 err += efunc(pc, "cannot write to %r0\n"); 9657 if (kcheckload) 9658 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 9659 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 9660 break; 9661 case DIF_OP_RLDSB: 9662 case DIF_OP_RLDSH: 9663 case DIF_OP_RLDSW: 9664 case DIF_OP_RLDUB: 9665 case DIF_OP_RLDUH: 9666 case DIF_OP_RLDUW: 9667 case DIF_OP_RLDX: 9668 if (r1 >= nregs) 9669 err += efunc(pc, "invalid register %u\n", r1); 9670 if (r2 != 0) 9671 err += efunc(pc, "non-zero reserved bits\n"); 9672 if (rd >= nregs) 9673 err += efunc(pc, "invalid register %u\n", rd); 9674 if (rd == 0) 9675 err += efunc(pc, "cannot write to %r0\n"); 9676 break; 9677 case DIF_OP_ULDSB: 9678 case DIF_OP_ULDSH: 9679 case DIF_OP_ULDSW: 9680 case DIF_OP_ULDUB: 9681 case DIF_OP_ULDUH: 9682 case DIF_OP_ULDUW: 9683 case DIF_OP_ULDX: 9684 if (r1 >= nregs) 9685 err += efunc(pc, "invalid register %u\n", r1); 9686 if (r2 != 0) 9687 err += efunc(pc, "non-zero reserved bits\n"); 9688 if (rd >= nregs) 9689 err += efunc(pc, "invalid register %u\n", rd); 9690 if (rd == 0) 9691 err += efunc(pc, "cannot write to %r0\n"); 9692 break; 9693 case DIF_OP_STB: 9694 case DIF_OP_STH: 9695 case DIF_OP_STW: 9696 case DIF_OP_STX: 9697 if (r1 >= nregs) 9698 err += efunc(pc, "invalid register %u\n", r1); 9699 if (r2 != 0) 9700 err += efunc(pc, "non-zero reserved bits\n"); 9701 if (rd >= nregs) 9702 err += efunc(pc, "invalid register %u\n", rd); 9703 if (rd == 0) 9704 err += efunc(pc, "cannot write to 0 address\n"); 9705 break; 9706 case DIF_OP_CMP: 9707 case DIF_OP_SCMP: 9708 if (r1 >= nregs) 9709 err += efunc(pc, "invalid register %u\n", r1); 9710 if (r2 >= nregs) 9711 err += efunc(pc, "invalid register %u\n", r2); 9712 if (rd != 0) 9713 err += efunc(pc, "non-zero reserved bits\n"); 9714 break; 9715 case DIF_OP_TST: 9716 if (r1 >= nregs) 9717 err += efunc(pc, "invalid register %u\n", r1); 9718 if (r2 != 0 || rd != 0) 9719 err += efunc(pc, "non-zero reserved bits\n"); 9720 break; 9721 case DIF_OP_BA: 9722 case DIF_OP_BE: 9723 case DIF_OP_BNE: 9724 case DIF_OP_BG: 9725 case DIF_OP_BGU: 9726 case DIF_OP_BGE: 9727 case DIF_OP_BGEU: 9728 case DIF_OP_BL: 9729 case DIF_OP_BLU: 9730 case DIF_OP_BLE: 9731 case DIF_OP_BLEU: 9732 if (label >= dp->dtdo_len) { 9733 err += efunc(pc, "invalid branch target %u\n", 9734 label); 9735 } 9736 if (label <= pc) { 9737 err += efunc(pc, "backward branch to %u\n", 9738 label); 9739 } 9740 break; 9741 case DIF_OP_RET: 9742 if (r1 != 0 || r2 != 0) 9743 err += efunc(pc, "non-zero reserved bits\n"); 9744 if (rd >= nregs) 9745 err += efunc(pc, "invalid register %u\n", rd); 9746 break; 9747 case DIF_OP_NOP: 9748 case DIF_OP_POPTS: 9749 case DIF_OP_FLUSHTS: 9750 if (r1 != 0 || r2 != 0 || rd != 0) 9751 err += efunc(pc, "non-zero reserved bits\n"); 9752 break; 9753 case DIF_OP_SETX: 9754 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 9755 err += efunc(pc, "invalid integer ref %u\n", 9756 DIF_INSTR_INTEGER(instr)); 9757 } 9758 if (rd >= nregs) 9759 err += efunc(pc, "invalid register %u\n", rd); 9760 if (rd == 0) 9761 err += efunc(pc, "cannot write to %r0\n"); 9762 break; 9763 case DIF_OP_SETS: 9764 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 9765 err += efunc(pc, "invalid string ref %u\n", 9766 DIF_INSTR_STRING(instr)); 9767 } 9768 if (rd >= nregs) 9769 err += efunc(pc, "invalid register %u\n", rd); 9770 if (rd == 0) 9771 err += efunc(pc, "cannot write to %r0\n"); 9772 break; 9773 case DIF_OP_LDGA: 9774 case DIF_OP_LDTA: 9775 if (r1 > DIF_VAR_ARRAY_MAX) 9776 err += efunc(pc, "invalid array %u\n", r1); 9777 if (r2 >= nregs) 9778 err += efunc(pc, "invalid register %u\n", r2); 9779 if (rd >= nregs) 9780 err += efunc(pc, "invalid register %u\n", rd); 9781 if (rd == 0) 9782 err += efunc(pc, "cannot write to %r0\n"); 9783 break; 9784 case DIF_OP_LDGS: 9785 case DIF_OP_LDTS: 9786 case DIF_OP_LDLS: 9787 case DIF_OP_LDGAA: 9788 case DIF_OP_LDTAA: 9789 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 9790 err += efunc(pc, "invalid variable %u\n", v); 9791 if (rd >= nregs) 9792 err += efunc(pc, "invalid register %u\n", rd); 9793 if (rd == 0) 9794 err += efunc(pc, "cannot write to %r0\n"); 9795 break; 9796 case DIF_OP_STGS: 9797 case DIF_OP_STTS: 9798 case DIF_OP_STLS: 9799 case DIF_OP_STGAA: 9800 case DIF_OP_STTAA: 9801 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 9802 err += efunc(pc, "invalid variable %u\n", v); 9803 if (rs >= nregs) 9804 err += efunc(pc, "invalid register %u\n", rd); 9805 break; 9806 case DIF_OP_CALL: 9807 if (subr > DIF_SUBR_MAX) 9808 err += efunc(pc, "invalid subr %u\n", subr); 9809 if (rd >= nregs) 9810 err += efunc(pc, "invalid register %u\n", rd); 9811 if (rd == 0) 9812 err += efunc(pc, "cannot write to %r0\n"); 9813 9814 if (subr == DIF_SUBR_COPYOUT || 9815 subr == DIF_SUBR_COPYOUTSTR) { 9816 dp->dtdo_destructive = 1; 9817 } 9818 9819 if (subr == DIF_SUBR_GETF) { 9820 /* 9821 * If we have a getf() we need to record that 9822 * in our state. Note that our state can be 9823 * NULL if this is a helper -- but in that 9824 * case, the call to getf() is itself illegal, 9825 * and will be caught (slightly later) when 9826 * the helper is validated. 9827 */ 9828 if (vstate->dtvs_state != NULL) 9829 vstate->dtvs_state->dts_getf++; 9830 } 9831 9832 break; 9833 case DIF_OP_PUSHTR: 9834 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 9835 err += efunc(pc, "invalid ref type %u\n", type); 9836 if (r2 >= nregs) 9837 err += efunc(pc, "invalid register %u\n", r2); 9838 if (rs >= nregs) 9839 err += efunc(pc, "invalid register %u\n", rs); 9840 break; 9841 case DIF_OP_PUSHTV: 9842 if (type != DIF_TYPE_CTF) 9843 err += efunc(pc, "invalid val type %u\n", type); 9844 if (r2 >= nregs) 9845 err += efunc(pc, "invalid register %u\n", r2); 9846 if (rs >= nregs) 9847 err += efunc(pc, "invalid register %u\n", rs); 9848 break; 9849 default: 9850 err += efunc(pc, "invalid opcode %u\n", 9851 DIF_INSTR_OP(instr)); 9852 } 9853 } 9854 9855 if (dp->dtdo_len != 0 && 9856 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 9857 err += efunc(dp->dtdo_len - 1, 9858 "expected 'ret' as last DIF instruction\n"); 9859 } 9860 9861 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) { 9862 /* 9863 * If we're not returning by reference, the size must be either 9864 * 0 or the size of one of the base types. 9865 */ 9866 switch (dp->dtdo_rtype.dtdt_size) { 9867 case 0: 9868 case sizeof (uint8_t): 9869 case sizeof (uint16_t): 9870 case sizeof (uint32_t): 9871 case sizeof (uint64_t): 9872 break; 9873 9874 default: 9875 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 9876 } 9877 } 9878 9879 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 9880 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 9881 dtrace_diftype_t *vt, *et; 9882 uint_t id, ndx; 9883 9884 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 9885 v->dtdv_scope != DIFV_SCOPE_THREAD && 9886 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 9887 err += efunc(i, "unrecognized variable scope %d\n", 9888 v->dtdv_scope); 9889 break; 9890 } 9891 9892 if (v->dtdv_kind != DIFV_KIND_ARRAY && 9893 v->dtdv_kind != DIFV_KIND_SCALAR) { 9894 err += efunc(i, "unrecognized variable type %d\n", 9895 v->dtdv_kind); 9896 break; 9897 } 9898 9899 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 9900 err += efunc(i, "%d exceeds variable id limit\n", id); 9901 break; 9902 } 9903 9904 if (id < DIF_VAR_OTHER_UBASE) 9905 continue; 9906 9907 /* 9908 * For user-defined variables, we need to check that this 9909 * definition is identical to any previous definition that we 9910 * encountered. 9911 */ 9912 ndx = id - DIF_VAR_OTHER_UBASE; 9913 9914 switch (v->dtdv_scope) { 9915 case DIFV_SCOPE_GLOBAL: 9916 if (ndx < vstate->dtvs_nglobals) { 9917 dtrace_statvar_t *svar; 9918 9919 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 9920 existing = &svar->dtsv_var; 9921 } 9922 9923 break; 9924 9925 case DIFV_SCOPE_THREAD: 9926 if (ndx < vstate->dtvs_ntlocals) 9927 existing = &vstate->dtvs_tlocals[ndx]; 9928 break; 9929 9930 case DIFV_SCOPE_LOCAL: 9931 if (ndx < vstate->dtvs_nlocals) { 9932 dtrace_statvar_t *svar; 9933 9934 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 9935 existing = &svar->dtsv_var; 9936 } 9937 9938 break; 9939 } 9940 9941 vt = &v->dtdv_type; 9942 9943 if (vt->dtdt_flags & DIF_TF_BYREF) { 9944 if (vt->dtdt_size == 0) { 9945 err += efunc(i, "zero-sized variable\n"); 9946 break; 9947 } 9948 9949 if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL || 9950 v->dtdv_scope == DIFV_SCOPE_LOCAL) && 9951 vt->dtdt_size > dtrace_statvar_maxsize) { 9952 err += efunc(i, "oversized by-ref static\n"); 9953 break; 9954 } 9955 } 9956 9957 if (existing == NULL || existing->dtdv_id == 0) 9958 continue; 9959 9960 ASSERT(existing->dtdv_id == v->dtdv_id); 9961 ASSERT(existing->dtdv_scope == v->dtdv_scope); 9962 9963 if (existing->dtdv_kind != v->dtdv_kind) 9964 err += efunc(i, "%d changed variable kind\n", id); 9965 9966 et = &existing->dtdv_type; 9967 9968 if (vt->dtdt_flags != et->dtdt_flags) { 9969 err += efunc(i, "%d changed variable type flags\n", id); 9970 break; 9971 } 9972 9973 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 9974 err += efunc(i, "%d changed variable type size\n", id); 9975 break; 9976 } 9977 } 9978 9979 return (err); 9980 } 9981 9982 /* 9983 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 9984 * are much more constrained than normal DIFOs. Specifically, they may 9985 * not: 9986 * 9987 * 1. Make calls to subroutines other than copyin(), copyinstr() or 9988 * miscellaneous string routines 9989 * 2. Access DTrace variables other than the args[] array, and the 9990 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 9991 * 3. Have thread-local variables. 9992 * 4. Have dynamic variables. 9993 */ 9994 static int 9995 dtrace_difo_validate_helper(dtrace_difo_t *dp) 9996 { 9997 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9998 int err = 0; 9999 uint_t pc; 10000 10001 for (pc = 0; pc < dp->dtdo_len; pc++) { 10002 dif_instr_t instr = dp->dtdo_buf[pc]; 10003 10004 uint_t v = DIF_INSTR_VAR(instr); 10005 uint_t subr = DIF_INSTR_SUBR(instr); 10006 uint_t op = DIF_INSTR_OP(instr); 10007 10008 switch (op) { 10009 case DIF_OP_OR: 10010 case DIF_OP_XOR: 10011 case DIF_OP_AND: 10012 case DIF_OP_SLL: 10013 case DIF_OP_SRL: 10014 case DIF_OP_SRA: 10015 case DIF_OP_SUB: 10016 case DIF_OP_ADD: 10017 case DIF_OP_MUL: 10018 case DIF_OP_SDIV: 10019 case DIF_OP_UDIV: 10020 case DIF_OP_SREM: 10021 case DIF_OP_UREM: 10022 case DIF_OP_COPYS: 10023 case DIF_OP_NOT: 10024 case DIF_OP_MOV: 10025 case DIF_OP_RLDSB: 10026 case DIF_OP_RLDSH: 10027 case DIF_OP_RLDSW: 10028 case DIF_OP_RLDUB: 10029 case DIF_OP_RLDUH: 10030 case DIF_OP_RLDUW: 10031 case DIF_OP_RLDX: 10032 case DIF_OP_ULDSB: 10033 case DIF_OP_ULDSH: 10034 case DIF_OP_ULDSW: 10035 case DIF_OP_ULDUB: 10036 case DIF_OP_ULDUH: 10037 case DIF_OP_ULDUW: 10038 case DIF_OP_ULDX: 10039 case DIF_OP_STB: 10040 case DIF_OP_STH: 10041 case DIF_OP_STW: 10042 case DIF_OP_STX: 10043 case DIF_OP_ALLOCS: 10044 case DIF_OP_CMP: 10045 case DIF_OP_SCMP: 10046 case DIF_OP_TST: 10047 case DIF_OP_BA: 10048 case DIF_OP_BE: 10049 case DIF_OP_BNE: 10050 case DIF_OP_BG: 10051 case DIF_OP_BGU: 10052 case DIF_OP_BGE: 10053 case DIF_OP_BGEU: 10054 case DIF_OP_BL: 10055 case DIF_OP_BLU: 10056 case DIF_OP_BLE: 10057 case DIF_OP_BLEU: 10058 case DIF_OP_RET: 10059 case DIF_OP_NOP: 10060 case DIF_OP_POPTS: 10061 case DIF_OP_FLUSHTS: 10062 case DIF_OP_SETX: 10063 case DIF_OP_SETS: 10064 case DIF_OP_LDGA: 10065 case DIF_OP_LDLS: 10066 case DIF_OP_STGS: 10067 case DIF_OP_STLS: 10068 case DIF_OP_PUSHTR: 10069 case DIF_OP_PUSHTV: 10070 break; 10071 10072 case DIF_OP_LDGS: 10073 if (v >= DIF_VAR_OTHER_UBASE) 10074 break; 10075 10076 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 10077 break; 10078 10079 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 10080 v == DIF_VAR_PPID || v == DIF_VAR_TID || 10081 v == DIF_VAR_EXECARGS || 10082 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 10083 v == DIF_VAR_UID || v == DIF_VAR_GID) 10084 break; 10085 10086 err += efunc(pc, "illegal variable %u\n", v); 10087 break; 10088 10089 case DIF_OP_LDTA: 10090 case DIF_OP_LDTS: 10091 case DIF_OP_LDGAA: 10092 case DIF_OP_LDTAA: 10093 err += efunc(pc, "illegal dynamic variable load\n"); 10094 break; 10095 10096 case DIF_OP_STTS: 10097 case DIF_OP_STGAA: 10098 case DIF_OP_STTAA: 10099 err += efunc(pc, "illegal dynamic variable store\n"); 10100 break; 10101 10102 case DIF_OP_CALL: 10103 if (subr == DIF_SUBR_ALLOCA || 10104 subr == DIF_SUBR_BCOPY || 10105 subr == DIF_SUBR_COPYIN || 10106 subr == DIF_SUBR_COPYINTO || 10107 subr == DIF_SUBR_COPYINSTR || 10108 subr == DIF_SUBR_INDEX || 10109 subr == DIF_SUBR_INET_NTOA || 10110 subr == DIF_SUBR_INET_NTOA6 || 10111 subr == DIF_SUBR_INET_NTOP || 10112 subr == DIF_SUBR_JSON || 10113 subr == DIF_SUBR_LLTOSTR || 10114 subr == DIF_SUBR_STRTOLL || 10115 subr == DIF_SUBR_RINDEX || 10116 subr == DIF_SUBR_STRCHR || 10117 subr == DIF_SUBR_STRJOIN || 10118 subr == DIF_SUBR_STRRCHR || 10119 subr == DIF_SUBR_STRSTR || 10120 subr == DIF_SUBR_HTONS || 10121 subr == DIF_SUBR_HTONL || 10122 subr == DIF_SUBR_HTONLL || 10123 subr == DIF_SUBR_NTOHS || 10124 subr == DIF_SUBR_NTOHL || 10125 subr == DIF_SUBR_NTOHLL || 10126 subr == DIF_SUBR_MEMREF || 10127 #ifndef illumos 10128 subr == DIF_SUBR_MEMSTR || 10129 #endif 10130 subr == DIF_SUBR_TYPEREF) 10131 break; 10132 10133 err += efunc(pc, "invalid subr %u\n", subr); 10134 break; 10135 10136 default: 10137 err += efunc(pc, "invalid opcode %u\n", 10138 DIF_INSTR_OP(instr)); 10139 } 10140 } 10141 10142 return (err); 10143 } 10144 10145 /* 10146 * Returns 1 if the expression in the DIF object can be cached on a per-thread 10147 * basis; 0 if not. 10148 */ 10149 static int 10150 dtrace_difo_cacheable(dtrace_difo_t *dp) 10151 { 10152 int i; 10153 10154 if (dp == NULL) 10155 return (0); 10156 10157 for (i = 0; i < dp->dtdo_varlen; i++) { 10158 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10159 10160 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 10161 continue; 10162 10163 switch (v->dtdv_id) { 10164 case DIF_VAR_CURTHREAD: 10165 case DIF_VAR_PID: 10166 case DIF_VAR_TID: 10167 case DIF_VAR_EXECARGS: 10168 case DIF_VAR_EXECNAME: 10169 case DIF_VAR_ZONENAME: 10170 break; 10171 10172 default: 10173 return (0); 10174 } 10175 } 10176 10177 /* 10178 * This DIF object may be cacheable. Now we need to look for any 10179 * array loading instructions, any memory loading instructions, or 10180 * any stores to thread-local variables. 10181 */ 10182 for (i = 0; i < dp->dtdo_len; i++) { 10183 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 10184 10185 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 10186 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 10187 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 10188 op == DIF_OP_LDGA || op == DIF_OP_STTS) 10189 return (0); 10190 } 10191 10192 return (1); 10193 } 10194 10195 static void 10196 dtrace_difo_hold(dtrace_difo_t *dp) 10197 { 10198 int i; 10199 10200 ASSERT(MUTEX_HELD(&dtrace_lock)); 10201 10202 dp->dtdo_refcnt++; 10203 ASSERT(dp->dtdo_refcnt != 0); 10204 10205 /* 10206 * We need to check this DIF object for references to the variable 10207 * DIF_VAR_VTIMESTAMP. 10208 */ 10209 for (i = 0; i < dp->dtdo_varlen; i++) { 10210 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10211 10212 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10213 continue; 10214 10215 if (dtrace_vtime_references++ == 0) 10216 dtrace_vtime_enable(); 10217 } 10218 } 10219 10220 /* 10221 * This routine calculates the dynamic variable chunksize for a given DIF 10222 * object. The calculation is not fool-proof, and can probably be tricked by 10223 * malicious DIF -- but it works for all compiler-generated DIF. Because this 10224 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 10225 * if a dynamic variable size exceeds the chunksize. 10226 */ 10227 static void 10228 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10229 { 10230 uint64_t sval = 0; 10231 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 10232 const dif_instr_t *text = dp->dtdo_buf; 10233 uint_t pc, srd = 0; 10234 uint_t ttop = 0; 10235 size_t size, ksize; 10236 uint_t id, i; 10237 10238 for (pc = 0; pc < dp->dtdo_len; pc++) { 10239 dif_instr_t instr = text[pc]; 10240 uint_t op = DIF_INSTR_OP(instr); 10241 uint_t rd = DIF_INSTR_RD(instr); 10242 uint_t r1 = DIF_INSTR_R1(instr); 10243 uint_t nkeys = 0; 10244 uchar_t scope = 0; 10245 10246 dtrace_key_t *key = tupregs; 10247 10248 switch (op) { 10249 case DIF_OP_SETX: 10250 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 10251 srd = rd; 10252 continue; 10253 10254 case DIF_OP_STTS: 10255 key = &tupregs[DIF_DTR_NREGS]; 10256 key[0].dttk_size = 0; 10257 key[1].dttk_size = 0; 10258 nkeys = 2; 10259 scope = DIFV_SCOPE_THREAD; 10260 break; 10261 10262 case DIF_OP_STGAA: 10263 case DIF_OP_STTAA: 10264 nkeys = ttop; 10265 10266 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 10267 key[nkeys++].dttk_size = 0; 10268 10269 key[nkeys++].dttk_size = 0; 10270 10271 if (op == DIF_OP_STTAA) { 10272 scope = DIFV_SCOPE_THREAD; 10273 } else { 10274 scope = DIFV_SCOPE_GLOBAL; 10275 } 10276 10277 break; 10278 10279 case DIF_OP_PUSHTR: 10280 if (ttop == DIF_DTR_NREGS) 10281 return; 10282 10283 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 10284 /* 10285 * If the register for the size of the "pushtr" 10286 * is %r0 (or the value is 0) and the type is 10287 * a string, we'll use the system-wide default 10288 * string size. 10289 */ 10290 tupregs[ttop++].dttk_size = 10291 dtrace_strsize_default; 10292 } else { 10293 if (srd == 0) 10294 return; 10295 10296 if (sval > LONG_MAX) 10297 return; 10298 10299 tupregs[ttop++].dttk_size = sval; 10300 } 10301 10302 break; 10303 10304 case DIF_OP_PUSHTV: 10305 if (ttop == DIF_DTR_NREGS) 10306 return; 10307 10308 tupregs[ttop++].dttk_size = 0; 10309 break; 10310 10311 case DIF_OP_FLUSHTS: 10312 ttop = 0; 10313 break; 10314 10315 case DIF_OP_POPTS: 10316 if (ttop != 0) 10317 ttop--; 10318 break; 10319 } 10320 10321 sval = 0; 10322 srd = 0; 10323 10324 if (nkeys == 0) 10325 continue; 10326 10327 /* 10328 * We have a dynamic variable allocation; calculate its size. 10329 */ 10330 for (ksize = 0, i = 0; i < nkeys; i++) 10331 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 10332 10333 size = sizeof (dtrace_dynvar_t); 10334 size += sizeof (dtrace_key_t) * (nkeys - 1); 10335 size += ksize; 10336 10337 /* 10338 * Now we need to determine the size of the stored data. 10339 */ 10340 id = DIF_INSTR_VAR(instr); 10341 10342 for (i = 0; i < dp->dtdo_varlen; i++) { 10343 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10344 10345 if (v->dtdv_id == id && v->dtdv_scope == scope) { 10346 size += v->dtdv_type.dtdt_size; 10347 break; 10348 } 10349 } 10350 10351 if (i == dp->dtdo_varlen) 10352 return; 10353 10354 /* 10355 * We have the size. If this is larger than the chunk size 10356 * for our dynamic variable state, reset the chunk size. 10357 */ 10358 size = P2ROUNDUP(size, sizeof (uint64_t)); 10359 10360 /* 10361 * Before setting the chunk size, check that we're not going 10362 * to set it to a negative value... 10363 */ 10364 if (size > LONG_MAX) 10365 return; 10366 10367 /* 10368 * ...and make certain that we didn't badly overflow. 10369 */ 10370 if (size < ksize || size < sizeof (dtrace_dynvar_t)) 10371 return; 10372 10373 if (size > vstate->dtvs_dynvars.dtds_chunksize) 10374 vstate->dtvs_dynvars.dtds_chunksize = size; 10375 } 10376 } 10377 10378 static void 10379 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10380 { 10381 int i, oldsvars, osz, nsz, otlocals, ntlocals; 10382 uint_t id; 10383 10384 ASSERT(MUTEX_HELD(&dtrace_lock)); 10385 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 10386 10387 for (i = 0; i < dp->dtdo_varlen; i++) { 10388 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10389 dtrace_statvar_t *svar, ***svarp = NULL; 10390 size_t dsize = 0; 10391 uint8_t scope = v->dtdv_scope; 10392 int *np = NULL; 10393 10394 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10395 continue; 10396 10397 id -= DIF_VAR_OTHER_UBASE; 10398 10399 switch (scope) { 10400 case DIFV_SCOPE_THREAD: 10401 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 10402 dtrace_difv_t *tlocals; 10403 10404 if ((ntlocals = (otlocals << 1)) == 0) 10405 ntlocals = 1; 10406 10407 osz = otlocals * sizeof (dtrace_difv_t); 10408 nsz = ntlocals * sizeof (dtrace_difv_t); 10409 10410 tlocals = kmem_zalloc(nsz, KM_SLEEP); 10411 10412 if (osz != 0) { 10413 bcopy(vstate->dtvs_tlocals, 10414 tlocals, osz); 10415 kmem_free(vstate->dtvs_tlocals, osz); 10416 } 10417 10418 vstate->dtvs_tlocals = tlocals; 10419 vstate->dtvs_ntlocals = ntlocals; 10420 } 10421 10422 vstate->dtvs_tlocals[id] = *v; 10423 continue; 10424 10425 case DIFV_SCOPE_LOCAL: 10426 np = &vstate->dtvs_nlocals; 10427 svarp = &vstate->dtvs_locals; 10428 10429 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10430 dsize = NCPU * (v->dtdv_type.dtdt_size + 10431 sizeof (uint64_t)); 10432 else 10433 dsize = NCPU * sizeof (uint64_t); 10434 10435 break; 10436 10437 case DIFV_SCOPE_GLOBAL: 10438 np = &vstate->dtvs_nglobals; 10439 svarp = &vstate->dtvs_globals; 10440 10441 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10442 dsize = v->dtdv_type.dtdt_size + 10443 sizeof (uint64_t); 10444 10445 break; 10446 10447 default: 10448 ASSERT(0); 10449 } 10450 10451 while (id >= (oldsvars = *np)) { 10452 dtrace_statvar_t **statics; 10453 int newsvars, oldsize, newsize; 10454 10455 if ((newsvars = (oldsvars << 1)) == 0) 10456 newsvars = 1; 10457 10458 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 10459 newsize = newsvars * sizeof (dtrace_statvar_t *); 10460 10461 statics = kmem_zalloc(newsize, KM_SLEEP); 10462 10463 if (oldsize != 0) { 10464 bcopy(*svarp, statics, oldsize); 10465 kmem_free(*svarp, oldsize); 10466 } 10467 10468 *svarp = statics; 10469 *np = newsvars; 10470 } 10471 10472 if ((svar = (*svarp)[id]) == NULL) { 10473 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 10474 svar->dtsv_var = *v; 10475 10476 if ((svar->dtsv_size = dsize) != 0) { 10477 svar->dtsv_data = (uint64_t)(uintptr_t) 10478 kmem_zalloc(dsize, KM_SLEEP); 10479 } 10480 10481 (*svarp)[id] = svar; 10482 } 10483 10484 svar->dtsv_refcnt++; 10485 } 10486 10487 dtrace_difo_chunksize(dp, vstate); 10488 dtrace_difo_hold(dp); 10489 } 10490 10491 static dtrace_difo_t * 10492 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10493 { 10494 dtrace_difo_t *new; 10495 size_t sz; 10496 10497 ASSERT(dp->dtdo_buf != NULL); 10498 ASSERT(dp->dtdo_refcnt != 0); 10499 10500 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10501 10502 ASSERT(dp->dtdo_buf != NULL); 10503 sz = dp->dtdo_len * sizeof (dif_instr_t); 10504 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 10505 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 10506 new->dtdo_len = dp->dtdo_len; 10507 10508 if (dp->dtdo_strtab != NULL) { 10509 ASSERT(dp->dtdo_strlen != 0); 10510 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 10511 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 10512 new->dtdo_strlen = dp->dtdo_strlen; 10513 } 10514 10515 if (dp->dtdo_inttab != NULL) { 10516 ASSERT(dp->dtdo_intlen != 0); 10517 sz = dp->dtdo_intlen * sizeof (uint64_t); 10518 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 10519 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 10520 new->dtdo_intlen = dp->dtdo_intlen; 10521 } 10522 10523 if (dp->dtdo_vartab != NULL) { 10524 ASSERT(dp->dtdo_varlen != 0); 10525 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 10526 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 10527 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 10528 new->dtdo_varlen = dp->dtdo_varlen; 10529 } 10530 10531 dtrace_difo_init(new, vstate); 10532 return (new); 10533 } 10534 10535 static void 10536 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10537 { 10538 int i; 10539 10540 ASSERT(dp->dtdo_refcnt == 0); 10541 10542 for (i = 0; i < dp->dtdo_varlen; i++) { 10543 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10544 dtrace_statvar_t *svar, **svarp = NULL; 10545 uint_t id; 10546 uint8_t scope = v->dtdv_scope; 10547 int *np = NULL; 10548 10549 switch (scope) { 10550 case DIFV_SCOPE_THREAD: 10551 continue; 10552 10553 case DIFV_SCOPE_LOCAL: 10554 np = &vstate->dtvs_nlocals; 10555 svarp = vstate->dtvs_locals; 10556 break; 10557 10558 case DIFV_SCOPE_GLOBAL: 10559 np = &vstate->dtvs_nglobals; 10560 svarp = vstate->dtvs_globals; 10561 break; 10562 10563 default: 10564 ASSERT(0); 10565 } 10566 10567 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10568 continue; 10569 10570 id -= DIF_VAR_OTHER_UBASE; 10571 ASSERT(id < *np); 10572 10573 svar = svarp[id]; 10574 ASSERT(svar != NULL); 10575 ASSERT(svar->dtsv_refcnt > 0); 10576 10577 if (--svar->dtsv_refcnt > 0) 10578 continue; 10579 10580 if (svar->dtsv_size != 0) { 10581 ASSERT(svar->dtsv_data != 0); 10582 kmem_free((void *)(uintptr_t)svar->dtsv_data, 10583 svar->dtsv_size); 10584 } 10585 10586 kmem_free(svar, sizeof (dtrace_statvar_t)); 10587 svarp[id] = NULL; 10588 } 10589 10590 if (dp->dtdo_buf != NULL) 10591 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10592 if (dp->dtdo_inttab != NULL) 10593 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10594 if (dp->dtdo_strtab != NULL) 10595 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10596 if (dp->dtdo_vartab != NULL) 10597 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10598 10599 kmem_free(dp, sizeof (dtrace_difo_t)); 10600 } 10601 10602 static void 10603 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10604 { 10605 int i; 10606 10607 ASSERT(MUTEX_HELD(&dtrace_lock)); 10608 ASSERT(dp->dtdo_refcnt != 0); 10609 10610 for (i = 0; i < dp->dtdo_varlen; i++) { 10611 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10612 10613 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10614 continue; 10615 10616 ASSERT(dtrace_vtime_references > 0); 10617 if (--dtrace_vtime_references == 0) 10618 dtrace_vtime_disable(); 10619 } 10620 10621 if (--dp->dtdo_refcnt == 0) 10622 dtrace_difo_destroy(dp, vstate); 10623 } 10624 10625 /* 10626 * DTrace Format Functions 10627 */ 10628 static uint16_t 10629 dtrace_format_add(dtrace_state_t *state, char *str) 10630 { 10631 char *fmt, **new; 10632 uint16_t ndx, len = strlen(str) + 1; 10633 10634 fmt = kmem_zalloc(len, KM_SLEEP); 10635 bcopy(str, fmt, len); 10636 10637 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 10638 if (state->dts_formats[ndx] == NULL) { 10639 state->dts_formats[ndx] = fmt; 10640 return (ndx + 1); 10641 } 10642 } 10643 10644 if (state->dts_nformats == USHRT_MAX) { 10645 /* 10646 * This is only likely if a denial-of-service attack is being 10647 * attempted. As such, it's okay to fail silently here. 10648 */ 10649 kmem_free(fmt, len); 10650 return (0); 10651 } 10652 10653 /* 10654 * For simplicity, we always resize the formats array to be exactly the 10655 * number of formats. 10656 */ 10657 ndx = state->dts_nformats++; 10658 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 10659 10660 if (state->dts_formats != NULL) { 10661 ASSERT(ndx != 0); 10662 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 10663 kmem_free(state->dts_formats, ndx * sizeof (char *)); 10664 } 10665 10666 state->dts_formats = new; 10667 state->dts_formats[ndx] = fmt; 10668 10669 return (ndx + 1); 10670 } 10671 10672 static void 10673 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 10674 { 10675 char *fmt; 10676 10677 ASSERT(state->dts_formats != NULL); 10678 ASSERT(format <= state->dts_nformats); 10679 ASSERT(state->dts_formats[format - 1] != NULL); 10680 10681 fmt = state->dts_formats[format - 1]; 10682 kmem_free(fmt, strlen(fmt) + 1); 10683 state->dts_formats[format - 1] = NULL; 10684 } 10685 10686 static void 10687 dtrace_format_destroy(dtrace_state_t *state) 10688 { 10689 int i; 10690 10691 if (state->dts_nformats == 0) { 10692 ASSERT(state->dts_formats == NULL); 10693 return; 10694 } 10695 10696 ASSERT(state->dts_formats != NULL); 10697 10698 for (i = 0; i < state->dts_nformats; i++) { 10699 char *fmt = state->dts_formats[i]; 10700 10701 if (fmt == NULL) 10702 continue; 10703 10704 kmem_free(fmt, strlen(fmt) + 1); 10705 } 10706 10707 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 10708 state->dts_nformats = 0; 10709 state->dts_formats = NULL; 10710 } 10711 10712 /* 10713 * DTrace Predicate Functions 10714 */ 10715 static dtrace_predicate_t * 10716 dtrace_predicate_create(dtrace_difo_t *dp) 10717 { 10718 dtrace_predicate_t *pred; 10719 10720 ASSERT(MUTEX_HELD(&dtrace_lock)); 10721 ASSERT(dp->dtdo_refcnt != 0); 10722 10723 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 10724 pred->dtp_difo = dp; 10725 pred->dtp_refcnt = 1; 10726 10727 if (!dtrace_difo_cacheable(dp)) 10728 return (pred); 10729 10730 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 10731 /* 10732 * This is only theoretically possible -- we have had 2^32 10733 * cacheable predicates on this machine. We cannot allow any 10734 * more predicates to become cacheable: as unlikely as it is, 10735 * there may be a thread caching a (now stale) predicate cache 10736 * ID. (N.B.: the temptation is being successfully resisted to 10737 * have this cmn_err() "Holy shit -- we executed this code!") 10738 */ 10739 return (pred); 10740 } 10741 10742 pred->dtp_cacheid = dtrace_predcache_id++; 10743 10744 return (pred); 10745 } 10746 10747 static void 10748 dtrace_predicate_hold(dtrace_predicate_t *pred) 10749 { 10750 ASSERT(MUTEX_HELD(&dtrace_lock)); 10751 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 10752 ASSERT(pred->dtp_refcnt > 0); 10753 10754 pred->dtp_refcnt++; 10755 } 10756 10757 static void 10758 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 10759 { 10760 dtrace_difo_t *dp = pred->dtp_difo; 10761 10762 ASSERT(MUTEX_HELD(&dtrace_lock)); 10763 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 10764 ASSERT(pred->dtp_refcnt > 0); 10765 10766 if (--pred->dtp_refcnt == 0) { 10767 dtrace_difo_release(pred->dtp_difo, vstate); 10768 kmem_free(pred, sizeof (dtrace_predicate_t)); 10769 } 10770 } 10771 10772 /* 10773 * DTrace Action Description Functions 10774 */ 10775 static dtrace_actdesc_t * 10776 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 10777 uint64_t uarg, uint64_t arg) 10778 { 10779 dtrace_actdesc_t *act; 10780 10781 #ifdef illumos 10782 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 10783 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 10784 #endif 10785 10786 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 10787 act->dtad_kind = kind; 10788 act->dtad_ntuple = ntuple; 10789 act->dtad_uarg = uarg; 10790 act->dtad_arg = arg; 10791 act->dtad_refcnt = 1; 10792 10793 return (act); 10794 } 10795 10796 static void 10797 dtrace_actdesc_hold(dtrace_actdesc_t *act) 10798 { 10799 ASSERT(act->dtad_refcnt >= 1); 10800 act->dtad_refcnt++; 10801 } 10802 10803 static void 10804 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 10805 { 10806 dtrace_actkind_t kind = act->dtad_kind; 10807 dtrace_difo_t *dp; 10808 10809 ASSERT(act->dtad_refcnt >= 1); 10810 10811 if (--act->dtad_refcnt != 0) 10812 return; 10813 10814 if ((dp = act->dtad_difo) != NULL) 10815 dtrace_difo_release(dp, vstate); 10816 10817 if (DTRACEACT_ISPRINTFLIKE(kind)) { 10818 char *str = (char *)(uintptr_t)act->dtad_arg; 10819 10820 #ifdef illumos 10821 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 10822 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 10823 #endif 10824 10825 if (str != NULL) 10826 kmem_free(str, strlen(str) + 1); 10827 } 10828 10829 kmem_free(act, sizeof (dtrace_actdesc_t)); 10830 } 10831 10832 /* 10833 * DTrace ECB Functions 10834 */ 10835 static dtrace_ecb_t * 10836 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 10837 { 10838 dtrace_ecb_t *ecb; 10839 dtrace_epid_t epid; 10840 10841 ASSERT(MUTEX_HELD(&dtrace_lock)); 10842 10843 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 10844 ecb->dte_predicate = NULL; 10845 ecb->dte_probe = probe; 10846 10847 /* 10848 * The default size is the size of the default action: recording 10849 * the header. 10850 */ 10851 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 10852 ecb->dte_alignment = sizeof (dtrace_epid_t); 10853 10854 epid = state->dts_epid++; 10855 10856 if (epid - 1 >= state->dts_necbs) { 10857 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 10858 int necbs = state->dts_necbs << 1; 10859 10860 ASSERT(epid == state->dts_necbs + 1); 10861 10862 if (necbs == 0) { 10863 ASSERT(oecbs == NULL); 10864 necbs = 1; 10865 } 10866 10867 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 10868 10869 if (oecbs != NULL) 10870 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 10871 10872 dtrace_membar_producer(); 10873 state->dts_ecbs = ecbs; 10874 10875 if (oecbs != NULL) { 10876 /* 10877 * If this state is active, we must dtrace_sync() 10878 * before we can free the old dts_ecbs array: we're 10879 * coming in hot, and there may be active ring 10880 * buffer processing (which indexes into the dts_ecbs 10881 * array) on another CPU. 10882 */ 10883 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 10884 dtrace_sync(); 10885 10886 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 10887 } 10888 10889 dtrace_membar_producer(); 10890 state->dts_necbs = necbs; 10891 } 10892 10893 ecb->dte_state = state; 10894 10895 ASSERT(state->dts_ecbs[epid - 1] == NULL); 10896 dtrace_membar_producer(); 10897 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 10898 10899 return (ecb); 10900 } 10901 10902 static void 10903 dtrace_ecb_enable(dtrace_ecb_t *ecb) 10904 { 10905 dtrace_probe_t *probe = ecb->dte_probe; 10906 10907 ASSERT(MUTEX_HELD(&cpu_lock)); 10908 ASSERT(MUTEX_HELD(&dtrace_lock)); 10909 ASSERT(ecb->dte_next == NULL); 10910 10911 if (probe == NULL) { 10912 /* 10913 * This is the NULL probe -- there's nothing to do. 10914 */ 10915 return; 10916 } 10917 10918 if (probe->dtpr_ecb == NULL) { 10919 dtrace_provider_t *prov = probe->dtpr_provider; 10920 10921 /* 10922 * We're the first ECB on this probe. 10923 */ 10924 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 10925 10926 if (ecb->dte_predicate != NULL) 10927 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 10928 10929 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 10930 probe->dtpr_id, probe->dtpr_arg); 10931 } else { 10932 /* 10933 * This probe is already active. Swing the last pointer to 10934 * point to the new ECB, and issue a dtrace_sync() to assure 10935 * that all CPUs have seen the change. 10936 */ 10937 ASSERT(probe->dtpr_ecb_last != NULL); 10938 probe->dtpr_ecb_last->dte_next = ecb; 10939 probe->dtpr_ecb_last = ecb; 10940 probe->dtpr_predcache = 0; 10941 10942 dtrace_sync(); 10943 } 10944 } 10945 10946 static void 10947 dtrace_ecb_resize(dtrace_ecb_t *ecb) 10948 { 10949 dtrace_action_t *act; 10950 uint32_t curneeded = UINT32_MAX; 10951 uint32_t aggbase = UINT32_MAX; 10952 10953 /* 10954 * If we record anything, we always record the dtrace_rechdr_t. (And 10955 * we always record it first.) 10956 */ 10957 ecb->dte_size = sizeof (dtrace_rechdr_t); 10958 ecb->dte_alignment = sizeof (dtrace_epid_t); 10959 10960 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 10961 dtrace_recdesc_t *rec = &act->dta_rec; 10962 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 10963 10964 ecb->dte_alignment = MAX(ecb->dte_alignment, 10965 rec->dtrd_alignment); 10966 10967 if (DTRACEACT_ISAGG(act->dta_kind)) { 10968 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 10969 10970 ASSERT(rec->dtrd_size != 0); 10971 ASSERT(agg->dtag_first != NULL); 10972 ASSERT(act->dta_prev->dta_intuple); 10973 ASSERT(aggbase != UINT32_MAX); 10974 ASSERT(curneeded != UINT32_MAX); 10975 10976 agg->dtag_base = aggbase; 10977 10978 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 10979 rec->dtrd_offset = curneeded; 10980 curneeded += rec->dtrd_size; 10981 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 10982 10983 aggbase = UINT32_MAX; 10984 curneeded = UINT32_MAX; 10985 } else if (act->dta_intuple) { 10986 if (curneeded == UINT32_MAX) { 10987 /* 10988 * This is the first record in a tuple. Align 10989 * curneeded to be at offset 4 in an 8-byte 10990 * aligned block. 10991 */ 10992 ASSERT(act->dta_prev == NULL || 10993 !act->dta_prev->dta_intuple); 10994 ASSERT3U(aggbase, ==, UINT32_MAX); 10995 curneeded = P2PHASEUP(ecb->dte_size, 10996 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 10997 10998 aggbase = curneeded - sizeof (dtrace_aggid_t); 10999 ASSERT(IS_P2ALIGNED(aggbase, 11000 sizeof (uint64_t))); 11001 } 11002 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11003 rec->dtrd_offset = curneeded; 11004 curneeded += rec->dtrd_size; 11005 } else { 11006 /* tuples must be followed by an aggregation */ 11007 ASSERT(act->dta_prev == NULL || 11008 !act->dta_prev->dta_intuple); 11009 11010 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 11011 rec->dtrd_alignment); 11012 rec->dtrd_offset = ecb->dte_size; 11013 ecb->dte_size += rec->dtrd_size; 11014 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 11015 } 11016 } 11017 11018 if ((act = ecb->dte_action) != NULL && 11019 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 11020 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 11021 /* 11022 * If the size is still sizeof (dtrace_rechdr_t), then all 11023 * actions store no data; set the size to 0. 11024 */ 11025 ecb->dte_size = 0; 11026 } 11027 11028 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 11029 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 11030 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 11031 ecb->dte_needed); 11032 } 11033 11034 static dtrace_action_t * 11035 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11036 { 11037 dtrace_aggregation_t *agg; 11038 size_t size = sizeof (uint64_t); 11039 int ntuple = desc->dtad_ntuple; 11040 dtrace_action_t *act; 11041 dtrace_recdesc_t *frec; 11042 dtrace_aggid_t aggid; 11043 dtrace_state_t *state = ecb->dte_state; 11044 11045 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 11046 agg->dtag_ecb = ecb; 11047 11048 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 11049 11050 switch (desc->dtad_kind) { 11051 case DTRACEAGG_MIN: 11052 agg->dtag_initial = INT64_MAX; 11053 agg->dtag_aggregate = dtrace_aggregate_min; 11054 break; 11055 11056 case DTRACEAGG_MAX: 11057 agg->dtag_initial = INT64_MIN; 11058 agg->dtag_aggregate = dtrace_aggregate_max; 11059 break; 11060 11061 case DTRACEAGG_COUNT: 11062 agg->dtag_aggregate = dtrace_aggregate_count; 11063 break; 11064 11065 case DTRACEAGG_QUANTIZE: 11066 agg->dtag_aggregate = dtrace_aggregate_quantize; 11067 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 11068 sizeof (uint64_t); 11069 break; 11070 11071 case DTRACEAGG_LQUANTIZE: { 11072 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 11073 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 11074 11075 agg->dtag_initial = desc->dtad_arg; 11076 agg->dtag_aggregate = dtrace_aggregate_lquantize; 11077 11078 if (step == 0 || levels == 0) 11079 goto err; 11080 11081 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 11082 break; 11083 } 11084 11085 case DTRACEAGG_LLQUANTIZE: { 11086 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 11087 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 11088 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 11089 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 11090 int64_t v; 11091 11092 agg->dtag_initial = desc->dtad_arg; 11093 agg->dtag_aggregate = dtrace_aggregate_llquantize; 11094 11095 if (factor < 2 || low >= high || nsteps < factor) 11096 goto err; 11097 11098 /* 11099 * Now check that the number of steps evenly divides a power 11100 * of the factor. (This assures both integer bucket size and 11101 * linearity within each magnitude.) 11102 */ 11103 for (v = factor; v < nsteps; v *= factor) 11104 continue; 11105 11106 if ((v % nsteps) || (nsteps % factor)) 11107 goto err; 11108 11109 size = (dtrace_aggregate_llquantize_bucket(factor, 11110 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 11111 break; 11112 } 11113 11114 case DTRACEAGG_AVG: 11115 agg->dtag_aggregate = dtrace_aggregate_avg; 11116 size = sizeof (uint64_t) * 2; 11117 break; 11118 11119 case DTRACEAGG_STDDEV: 11120 agg->dtag_aggregate = dtrace_aggregate_stddev; 11121 size = sizeof (uint64_t) * 4; 11122 break; 11123 11124 case DTRACEAGG_SUM: 11125 agg->dtag_aggregate = dtrace_aggregate_sum; 11126 break; 11127 11128 default: 11129 goto err; 11130 } 11131 11132 agg->dtag_action.dta_rec.dtrd_size = size; 11133 11134 if (ntuple == 0) 11135 goto err; 11136 11137 /* 11138 * We must make sure that we have enough actions for the n-tuple. 11139 */ 11140 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 11141 if (DTRACEACT_ISAGG(act->dta_kind)) 11142 break; 11143 11144 if (--ntuple == 0) { 11145 /* 11146 * This is the action with which our n-tuple begins. 11147 */ 11148 agg->dtag_first = act; 11149 goto success; 11150 } 11151 } 11152 11153 /* 11154 * This n-tuple is short by ntuple elements. Return failure. 11155 */ 11156 ASSERT(ntuple != 0); 11157 err: 11158 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11159 return (NULL); 11160 11161 success: 11162 /* 11163 * If the last action in the tuple has a size of zero, it's actually 11164 * an expression argument for the aggregating action. 11165 */ 11166 ASSERT(ecb->dte_action_last != NULL); 11167 act = ecb->dte_action_last; 11168 11169 if (act->dta_kind == DTRACEACT_DIFEXPR) { 11170 ASSERT(act->dta_difo != NULL); 11171 11172 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 11173 agg->dtag_hasarg = 1; 11174 } 11175 11176 /* 11177 * We need to allocate an id for this aggregation. 11178 */ 11179 #ifdef illumos 11180 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 11181 VM_BESTFIT | VM_SLEEP); 11182 #else 11183 aggid = alloc_unr(state->dts_aggid_arena); 11184 #endif 11185 11186 if (aggid - 1 >= state->dts_naggregations) { 11187 dtrace_aggregation_t **oaggs = state->dts_aggregations; 11188 dtrace_aggregation_t **aggs; 11189 int naggs = state->dts_naggregations << 1; 11190 int onaggs = state->dts_naggregations; 11191 11192 ASSERT(aggid == state->dts_naggregations + 1); 11193 11194 if (naggs == 0) { 11195 ASSERT(oaggs == NULL); 11196 naggs = 1; 11197 } 11198 11199 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 11200 11201 if (oaggs != NULL) { 11202 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 11203 kmem_free(oaggs, onaggs * sizeof (*aggs)); 11204 } 11205 11206 state->dts_aggregations = aggs; 11207 state->dts_naggregations = naggs; 11208 } 11209 11210 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 11211 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 11212 11213 frec = &agg->dtag_first->dta_rec; 11214 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 11215 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 11216 11217 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 11218 ASSERT(!act->dta_intuple); 11219 act->dta_intuple = 1; 11220 } 11221 11222 return (&agg->dtag_action); 11223 } 11224 11225 static void 11226 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 11227 { 11228 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11229 dtrace_state_t *state = ecb->dte_state; 11230 dtrace_aggid_t aggid = agg->dtag_id; 11231 11232 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 11233 #ifdef illumos 11234 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 11235 #else 11236 free_unr(state->dts_aggid_arena, aggid); 11237 #endif 11238 11239 ASSERT(state->dts_aggregations[aggid - 1] == agg); 11240 state->dts_aggregations[aggid - 1] = NULL; 11241 11242 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11243 } 11244 11245 static int 11246 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11247 { 11248 dtrace_action_t *action, *last; 11249 dtrace_difo_t *dp = desc->dtad_difo; 11250 uint32_t size = 0, align = sizeof (uint8_t), mask; 11251 uint16_t format = 0; 11252 dtrace_recdesc_t *rec; 11253 dtrace_state_t *state = ecb->dte_state; 11254 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 11255 uint64_t arg = desc->dtad_arg; 11256 11257 ASSERT(MUTEX_HELD(&dtrace_lock)); 11258 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 11259 11260 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 11261 /* 11262 * If this is an aggregating action, there must be neither 11263 * a speculate nor a commit on the action chain. 11264 */ 11265 dtrace_action_t *act; 11266 11267 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11268 if (act->dta_kind == DTRACEACT_COMMIT) 11269 return (EINVAL); 11270 11271 if (act->dta_kind == DTRACEACT_SPECULATE) 11272 return (EINVAL); 11273 } 11274 11275 action = dtrace_ecb_aggregation_create(ecb, desc); 11276 11277 if (action == NULL) 11278 return (EINVAL); 11279 } else { 11280 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 11281 (desc->dtad_kind == DTRACEACT_DIFEXPR && 11282 dp != NULL && dp->dtdo_destructive)) { 11283 state->dts_destructive = 1; 11284 } 11285 11286 switch (desc->dtad_kind) { 11287 case DTRACEACT_PRINTF: 11288 case DTRACEACT_PRINTA: 11289 case DTRACEACT_SYSTEM: 11290 case DTRACEACT_FREOPEN: 11291 case DTRACEACT_DIFEXPR: 11292 /* 11293 * We know that our arg is a string -- turn it into a 11294 * format. 11295 */ 11296 if (arg == 0) { 11297 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 11298 desc->dtad_kind == DTRACEACT_DIFEXPR); 11299 format = 0; 11300 } else { 11301 ASSERT(arg != 0); 11302 #ifdef illumos 11303 ASSERT(arg > KERNELBASE); 11304 #endif 11305 format = dtrace_format_add(state, 11306 (char *)(uintptr_t)arg); 11307 } 11308 11309 /*FALLTHROUGH*/ 11310 case DTRACEACT_LIBACT: 11311 case DTRACEACT_TRACEMEM: 11312 case DTRACEACT_TRACEMEM_DYNSIZE: 11313 if (dp == NULL) 11314 return (EINVAL); 11315 11316 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 11317 break; 11318 11319 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 11320 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11321 return (EINVAL); 11322 11323 size = opt[DTRACEOPT_STRSIZE]; 11324 } 11325 11326 break; 11327 11328 case DTRACEACT_STACK: 11329 if ((nframes = arg) == 0) { 11330 nframes = opt[DTRACEOPT_STACKFRAMES]; 11331 ASSERT(nframes > 0); 11332 arg = nframes; 11333 } 11334 11335 size = nframes * sizeof (pc_t); 11336 break; 11337 11338 case DTRACEACT_JSTACK: 11339 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 11340 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 11341 11342 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 11343 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 11344 11345 arg = DTRACE_USTACK_ARG(nframes, strsize); 11346 11347 /*FALLTHROUGH*/ 11348 case DTRACEACT_USTACK: 11349 if (desc->dtad_kind != DTRACEACT_JSTACK && 11350 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 11351 strsize = DTRACE_USTACK_STRSIZE(arg); 11352 nframes = opt[DTRACEOPT_USTACKFRAMES]; 11353 ASSERT(nframes > 0); 11354 arg = DTRACE_USTACK_ARG(nframes, strsize); 11355 } 11356 11357 /* 11358 * Save a slot for the pid. 11359 */ 11360 size = (nframes + 1) * sizeof (uint64_t); 11361 size += DTRACE_USTACK_STRSIZE(arg); 11362 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 11363 11364 break; 11365 11366 case DTRACEACT_SYM: 11367 case DTRACEACT_MOD: 11368 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 11369 sizeof (uint64_t)) || 11370 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11371 return (EINVAL); 11372 break; 11373 11374 case DTRACEACT_USYM: 11375 case DTRACEACT_UMOD: 11376 case DTRACEACT_UADDR: 11377 if (dp == NULL || 11378 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 11379 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11380 return (EINVAL); 11381 11382 /* 11383 * We have a slot for the pid, plus a slot for the 11384 * argument. To keep things simple (aligned with 11385 * bitness-neutral sizing), we store each as a 64-bit 11386 * quantity. 11387 */ 11388 size = 2 * sizeof (uint64_t); 11389 break; 11390 11391 case DTRACEACT_STOP: 11392 case DTRACEACT_BREAKPOINT: 11393 case DTRACEACT_PANIC: 11394 break; 11395 11396 case DTRACEACT_CHILL: 11397 case DTRACEACT_DISCARD: 11398 case DTRACEACT_RAISE: 11399 if (dp == NULL) 11400 return (EINVAL); 11401 break; 11402 11403 case DTRACEACT_EXIT: 11404 if (dp == NULL || 11405 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 11406 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11407 return (EINVAL); 11408 break; 11409 11410 case DTRACEACT_SPECULATE: 11411 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 11412 return (EINVAL); 11413 11414 if (dp == NULL) 11415 return (EINVAL); 11416 11417 state->dts_speculates = 1; 11418 break; 11419 11420 case DTRACEACT_PRINTM: 11421 size = dp->dtdo_rtype.dtdt_size; 11422 break; 11423 11424 case DTRACEACT_PRINTT: 11425 size = dp->dtdo_rtype.dtdt_size; 11426 break; 11427 11428 case DTRACEACT_COMMIT: { 11429 dtrace_action_t *act = ecb->dte_action; 11430 11431 for (; act != NULL; act = act->dta_next) { 11432 if (act->dta_kind == DTRACEACT_COMMIT) 11433 return (EINVAL); 11434 } 11435 11436 if (dp == NULL) 11437 return (EINVAL); 11438 break; 11439 } 11440 11441 default: 11442 return (EINVAL); 11443 } 11444 11445 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 11446 /* 11447 * If this is a data-storing action or a speculate, 11448 * we must be sure that there isn't a commit on the 11449 * action chain. 11450 */ 11451 dtrace_action_t *act = ecb->dte_action; 11452 11453 for (; act != NULL; act = act->dta_next) { 11454 if (act->dta_kind == DTRACEACT_COMMIT) 11455 return (EINVAL); 11456 } 11457 } 11458 11459 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 11460 action->dta_rec.dtrd_size = size; 11461 } 11462 11463 action->dta_refcnt = 1; 11464 rec = &action->dta_rec; 11465 size = rec->dtrd_size; 11466 11467 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 11468 if (!(size & mask)) { 11469 align = mask + 1; 11470 break; 11471 } 11472 } 11473 11474 action->dta_kind = desc->dtad_kind; 11475 11476 if ((action->dta_difo = dp) != NULL) 11477 dtrace_difo_hold(dp); 11478 11479 rec->dtrd_action = action->dta_kind; 11480 rec->dtrd_arg = arg; 11481 rec->dtrd_uarg = desc->dtad_uarg; 11482 rec->dtrd_alignment = (uint16_t)align; 11483 rec->dtrd_format = format; 11484 11485 if ((last = ecb->dte_action_last) != NULL) { 11486 ASSERT(ecb->dte_action != NULL); 11487 action->dta_prev = last; 11488 last->dta_next = action; 11489 } else { 11490 ASSERT(ecb->dte_action == NULL); 11491 ecb->dte_action = action; 11492 } 11493 11494 ecb->dte_action_last = action; 11495 11496 return (0); 11497 } 11498 11499 static void 11500 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 11501 { 11502 dtrace_action_t *act = ecb->dte_action, *next; 11503 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 11504 dtrace_difo_t *dp; 11505 uint16_t format; 11506 11507 if (act != NULL && act->dta_refcnt > 1) { 11508 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 11509 act->dta_refcnt--; 11510 } else { 11511 for (; act != NULL; act = next) { 11512 next = act->dta_next; 11513 ASSERT(next != NULL || act == ecb->dte_action_last); 11514 ASSERT(act->dta_refcnt == 1); 11515 11516 if ((format = act->dta_rec.dtrd_format) != 0) 11517 dtrace_format_remove(ecb->dte_state, format); 11518 11519 if ((dp = act->dta_difo) != NULL) 11520 dtrace_difo_release(dp, vstate); 11521 11522 if (DTRACEACT_ISAGG(act->dta_kind)) { 11523 dtrace_ecb_aggregation_destroy(ecb, act); 11524 } else { 11525 kmem_free(act, sizeof (dtrace_action_t)); 11526 } 11527 } 11528 } 11529 11530 ecb->dte_action = NULL; 11531 ecb->dte_action_last = NULL; 11532 ecb->dte_size = 0; 11533 } 11534 11535 static void 11536 dtrace_ecb_disable(dtrace_ecb_t *ecb) 11537 { 11538 /* 11539 * We disable the ECB by removing it from its probe. 11540 */ 11541 dtrace_ecb_t *pecb, *prev = NULL; 11542 dtrace_probe_t *probe = ecb->dte_probe; 11543 11544 ASSERT(MUTEX_HELD(&dtrace_lock)); 11545 11546 if (probe == NULL) { 11547 /* 11548 * This is the NULL probe; there is nothing to disable. 11549 */ 11550 return; 11551 } 11552 11553 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 11554 if (pecb == ecb) 11555 break; 11556 prev = pecb; 11557 } 11558 11559 ASSERT(pecb != NULL); 11560 11561 if (prev == NULL) { 11562 probe->dtpr_ecb = ecb->dte_next; 11563 } else { 11564 prev->dte_next = ecb->dte_next; 11565 } 11566 11567 if (ecb == probe->dtpr_ecb_last) { 11568 ASSERT(ecb->dte_next == NULL); 11569 probe->dtpr_ecb_last = prev; 11570 } 11571 11572 /* 11573 * The ECB has been disconnected from the probe; now sync to assure 11574 * that all CPUs have seen the change before returning. 11575 */ 11576 dtrace_sync(); 11577 11578 if (probe->dtpr_ecb == NULL) { 11579 /* 11580 * That was the last ECB on the probe; clear the predicate 11581 * cache ID for the probe, disable it and sync one more time 11582 * to assure that we'll never hit it again. 11583 */ 11584 dtrace_provider_t *prov = probe->dtpr_provider; 11585 11586 ASSERT(ecb->dte_next == NULL); 11587 ASSERT(probe->dtpr_ecb_last == NULL); 11588 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 11589 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 11590 probe->dtpr_id, probe->dtpr_arg); 11591 dtrace_sync(); 11592 } else { 11593 /* 11594 * There is at least one ECB remaining on the probe. If there 11595 * is _exactly_ one, set the probe's predicate cache ID to be 11596 * the predicate cache ID of the remaining ECB. 11597 */ 11598 ASSERT(probe->dtpr_ecb_last != NULL); 11599 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 11600 11601 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 11602 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 11603 11604 ASSERT(probe->dtpr_ecb->dte_next == NULL); 11605 11606 if (p != NULL) 11607 probe->dtpr_predcache = p->dtp_cacheid; 11608 } 11609 11610 ecb->dte_next = NULL; 11611 } 11612 } 11613 11614 static void 11615 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 11616 { 11617 dtrace_state_t *state = ecb->dte_state; 11618 dtrace_vstate_t *vstate = &state->dts_vstate; 11619 dtrace_predicate_t *pred; 11620 dtrace_epid_t epid = ecb->dte_epid; 11621 11622 ASSERT(MUTEX_HELD(&dtrace_lock)); 11623 ASSERT(ecb->dte_next == NULL); 11624 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 11625 11626 if ((pred = ecb->dte_predicate) != NULL) 11627 dtrace_predicate_release(pred, vstate); 11628 11629 dtrace_ecb_action_remove(ecb); 11630 11631 ASSERT(state->dts_ecbs[epid - 1] == ecb); 11632 state->dts_ecbs[epid - 1] = NULL; 11633 11634 kmem_free(ecb, sizeof (dtrace_ecb_t)); 11635 } 11636 11637 static dtrace_ecb_t * 11638 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 11639 dtrace_enabling_t *enab) 11640 { 11641 dtrace_ecb_t *ecb; 11642 dtrace_predicate_t *pred; 11643 dtrace_actdesc_t *act; 11644 dtrace_provider_t *prov; 11645 dtrace_ecbdesc_t *desc = enab->dten_current; 11646 11647 ASSERT(MUTEX_HELD(&dtrace_lock)); 11648 ASSERT(state != NULL); 11649 11650 ecb = dtrace_ecb_add(state, probe); 11651 ecb->dte_uarg = desc->dted_uarg; 11652 11653 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 11654 dtrace_predicate_hold(pred); 11655 ecb->dte_predicate = pred; 11656 } 11657 11658 if (probe != NULL) { 11659 /* 11660 * If the provider shows more leg than the consumer is old 11661 * enough to see, we need to enable the appropriate implicit 11662 * predicate bits to prevent the ecb from activating at 11663 * revealing times. 11664 * 11665 * Providers specifying DTRACE_PRIV_USER at register time 11666 * are stating that they need the /proc-style privilege 11667 * model to be enforced, and this is what DTRACE_COND_OWNER 11668 * and DTRACE_COND_ZONEOWNER will then do at probe time. 11669 */ 11670 prov = probe->dtpr_provider; 11671 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 11672 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11673 ecb->dte_cond |= DTRACE_COND_OWNER; 11674 11675 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 11676 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11677 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 11678 11679 /* 11680 * If the provider shows us kernel innards and the user 11681 * is lacking sufficient privilege, enable the 11682 * DTRACE_COND_USERMODE implicit predicate. 11683 */ 11684 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 11685 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 11686 ecb->dte_cond |= DTRACE_COND_USERMODE; 11687 } 11688 11689 if (dtrace_ecb_create_cache != NULL) { 11690 /* 11691 * If we have a cached ecb, we'll use its action list instead 11692 * of creating our own (saving both time and space). 11693 */ 11694 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 11695 dtrace_action_t *act = cached->dte_action; 11696 11697 if (act != NULL) { 11698 ASSERT(act->dta_refcnt > 0); 11699 act->dta_refcnt++; 11700 ecb->dte_action = act; 11701 ecb->dte_action_last = cached->dte_action_last; 11702 ecb->dte_needed = cached->dte_needed; 11703 ecb->dte_size = cached->dte_size; 11704 ecb->dte_alignment = cached->dte_alignment; 11705 } 11706 11707 return (ecb); 11708 } 11709 11710 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 11711 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 11712 dtrace_ecb_destroy(ecb); 11713 return (NULL); 11714 } 11715 } 11716 11717 dtrace_ecb_resize(ecb); 11718 11719 return (dtrace_ecb_create_cache = ecb); 11720 } 11721 11722 static int 11723 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 11724 { 11725 dtrace_ecb_t *ecb; 11726 dtrace_enabling_t *enab = arg; 11727 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 11728 11729 ASSERT(state != NULL); 11730 11731 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 11732 /* 11733 * This probe was created in a generation for which this 11734 * enabling has previously created ECBs; we don't want to 11735 * enable it again, so just kick out. 11736 */ 11737 return (DTRACE_MATCH_NEXT); 11738 } 11739 11740 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 11741 return (DTRACE_MATCH_DONE); 11742 11743 dtrace_ecb_enable(ecb); 11744 return (DTRACE_MATCH_NEXT); 11745 } 11746 11747 static dtrace_ecb_t * 11748 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 11749 { 11750 dtrace_ecb_t *ecb; 11751 11752 ASSERT(MUTEX_HELD(&dtrace_lock)); 11753 11754 if (id == 0 || id > state->dts_necbs) 11755 return (NULL); 11756 11757 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 11758 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 11759 11760 return (state->dts_ecbs[id - 1]); 11761 } 11762 11763 static dtrace_aggregation_t * 11764 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 11765 { 11766 dtrace_aggregation_t *agg; 11767 11768 ASSERT(MUTEX_HELD(&dtrace_lock)); 11769 11770 if (id == 0 || id > state->dts_naggregations) 11771 return (NULL); 11772 11773 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 11774 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 11775 agg->dtag_id == id); 11776 11777 return (state->dts_aggregations[id - 1]); 11778 } 11779 11780 /* 11781 * DTrace Buffer Functions 11782 * 11783 * The following functions manipulate DTrace buffers. Most of these functions 11784 * are called in the context of establishing or processing consumer state; 11785 * exceptions are explicitly noted. 11786 */ 11787 11788 /* 11789 * Note: called from cross call context. This function switches the two 11790 * buffers on a given CPU. The atomicity of this operation is assured by 11791 * disabling interrupts while the actual switch takes place; the disabling of 11792 * interrupts serializes the execution with any execution of dtrace_probe() on 11793 * the same CPU. 11794 */ 11795 static void 11796 dtrace_buffer_switch(dtrace_buffer_t *buf) 11797 { 11798 caddr_t tomax = buf->dtb_tomax; 11799 caddr_t xamot = buf->dtb_xamot; 11800 dtrace_icookie_t cookie; 11801 hrtime_t now; 11802 11803 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11804 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 11805 11806 cookie = dtrace_interrupt_disable(); 11807 now = dtrace_gethrtime(); 11808 buf->dtb_tomax = xamot; 11809 buf->dtb_xamot = tomax; 11810 buf->dtb_xamot_drops = buf->dtb_drops; 11811 buf->dtb_xamot_offset = buf->dtb_offset; 11812 buf->dtb_xamot_errors = buf->dtb_errors; 11813 buf->dtb_xamot_flags = buf->dtb_flags; 11814 buf->dtb_offset = 0; 11815 buf->dtb_drops = 0; 11816 buf->dtb_errors = 0; 11817 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 11818 buf->dtb_interval = now - buf->dtb_switched; 11819 buf->dtb_switched = now; 11820 dtrace_interrupt_enable(cookie); 11821 } 11822 11823 /* 11824 * Note: called from cross call context. This function activates a buffer 11825 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 11826 * is guaranteed by the disabling of interrupts. 11827 */ 11828 static void 11829 dtrace_buffer_activate(dtrace_state_t *state) 11830 { 11831 dtrace_buffer_t *buf; 11832 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 11833 11834 buf = &state->dts_buffer[curcpu]; 11835 11836 if (buf->dtb_tomax != NULL) { 11837 /* 11838 * We might like to assert that the buffer is marked inactive, 11839 * but this isn't necessarily true: the buffer for the CPU 11840 * that processes the BEGIN probe has its buffer activated 11841 * manually. In this case, we take the (harmless) action 11842 * re-clearing the bit INACTIVE bit. 11843 */ 11844 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 11845 } 11846 11847 dtrace_interrupt_enable(cookie); 11848 } 11849 11850 static int 11851 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 11852 processorid_t cpu, int *factor) 11853 { 11854 #ifdef illumos 11855 cpu_t *cp; 11856 #endif 11857 dtrace_buffer_t *buf; 11858 int allocated = 0, desired = 0; 11859 11860 #ifdef illumos 11861 ASSERT(MUTEX_HELD(&cpu_lock)); 11862 ASSERT(MUTEX_HELD(&dtrace_lock)); 11863 11864 *factor = 1; 11865 11866 if (size > dtrace_nonroot_maxsize && 11867 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 11868 return (EFBIG); 11869 11870 cp = cpu_list; 11871 11872 do { 11873 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 11874 continue; 11875 11876 buf = &bufs[cp->cpu_id]; 11877 11878 /* 11879 * If there is already a buffer allocated for this CPU, it 11880 * is only possible that this is a DR event. In this case, 11881 */ 11882 if (buf->dtb_tomax != NULL) { 11883 ASSERT(buf->dtb_size == size); 11884 continue; 11885 } 11886 11887 ASSERT(buf->dtb_xamot == NULL); 11888 11889 if ((buf->dtb_tomax = kmem_zalloc(size, 11890 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11891 goto err; 11892 11893 buf->dtb_size = size; 11894 buf->dtb_flags = flags; 11895 buf->dtb_offset = 0; 11896 buf->dtb_drops = 0; 11897 11898 if (flags & DTRACEBUF_NOSWITCH) 11899 continue; 11900 11901 if ((buf->dtb_xamot = kmem_zalloc(size, 11902 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11903 goto err; 11904 } while ((cp = cp->cpu_next) != cpu_list); 11905 11906 return (0); 11907 11908 err: 11909 cp = cpu_list; 11910 11911 do { 11912 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 11913 continue; 11914 11915 buf = &bufs[cp->cpu_id]; 11916 desired += 2; 11917 11918 if (buf->dtb_xamot != NULL) { 11919 ASSERT(buf->dtb_tomax != NULL); 11920 ASSERT(buf->dtb_size == size); 11921 kmem_free(buf->dtb_xamot, size); 11922 allocated++; 11923 } 11924 11925 if (buf->dtb_tomax != NULL) { 11926 ASSERT(buf->dtb_size == size); 11927 kmem_free(buf->dtb_tomax, size); 11928 allocated++; 11929 } 11930 11931 buf->dtb_tomax = NULL; 11932 buf->dtb_xamot = NULL; 11933 buf->dtb_size = 0; 11934 } while ((cp = cp->cpu_next) != cpu_list); 11935 #else 11936 int i; 11937 11938 *factor = 1; 11939 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \ 11940 defined(__mips__) || defined(__powerpc__) 11941 /* 11942 * FreeBSD isn't good at limiting the amount of memory we 11943 * ask to malloc, so let's place a limit here before trying 11944 * to do something that might well end in tears at bedtime. 11945 */ 11946 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 11947 return (ENOMEM); 11948 #endif 11949 11950 ASSERT(MUTEX_HELD(&dtrace_lock)); 11951 CPU_FOREACH(i) { 11952 if (cpu != DTRACE_CPUALL && cpu != i) 11953 continue; 11954 11955 buf = &bufs[i]; 11956 11957 /* 11958 * If there is already a buffer allocated for this CPU, it 11959 * is only possible that this is a DR event. In this case, 11960 * the buffer size must match our specified size. 11961 */ 11962 if (buf->dtb_tomax != NULL) { 11963 ASSERT(buf->dtb_size == size); 11964 continue; 11965 } 11966 11967 ASSERT(buf->dtb_xamot == NULL); 11968 11969 if ((buf->dtb_tomax = kmem_zalloc(size, 11970 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11971 goto err; 11972 11973 buf->dtb_size = size; 11974 buf->dtb_flags = flags; 11975 buf->dtb_offset = 0; 11976 buf->dtb_drops = 0; 11977 11978 if (flags & DTRACEBUF_NOSWITCH) 11979 continue; 11980 11981 if ((buf->dtb_xamot = kmem_zalloc(size, 11982 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11983 goto err; 11984 } 11985 11986 return (0); 11987 11988 err: 11989 /* 11990 * Error allocating memory, so free the buffers that were 11991 * allocated before the failed allocation. 11992 */ 11993 CPU_FOREACH(i) { 11994 if (cpu != DTRACE_CPUALL && cpu != i) 11995 continue; 11996 11997 buf = &bufs[i]; 11998 desired += 2; 11999 12000 if (buf->dtb_xamot != NULL) { 12001 ASSERT(buf->dtb_tomax != NULL); 12002 ASSERT(buf->dtb_size == size); 12003 kmem_free(buf->dtb_xamot, size); 12004 allocated++; 12005 } 12006 12007 if (buf->dtb_tomax != NULL) { 12008 ASSERT(buf->dtb_size == size); 12009 kmem_free(buf->dtb_tomax, size); 12010 allocated++; 12011 } 12012 12013 buf->dtb_tomax = NULL; 12014 buf->dtb_xamot = NULL; 12015 buf->dtb_size = 0; 12016 12017 } 12018 #endif 12019 *factor = desired / (allocated > 0 ? allocated : 1); 12020 12021 return (ENOMEM); 12022 } 12023 12024 /* 12025 * Note: called from probe context. This function just increments the drop 12026 * count on a buffer. It has been made a function to allow for the 12027 * possibility of understanding the source of mysterious drop counts. (A 12028 * problem for which one may be particularly disappointed that DTrace cannot 12029 * be used to understand DTrace.) 12030 */ 12031 static void 12032 dtrace_buffer_drop(dtrace_buffer_t *buf) 12033 { 12034 buf->dtb_drops++; 12035 } 12036 12037 /* 12038 * Note: called from probe context. This function is called to reserve space 12039 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 12040 * mstate. Returns the new offset in the buffer, or a negative value if an 12041 * error has occurred. 12042 */ 12043 static intptr_t 12044 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 12045 dtrace_state_t *state, dtrace_mstate_t *mstate) 12046 { 12047 intptr_t offs = buf->dtb_offset, soffs; 12048 intptr_t woffs; 12049 caddr_t tomax; 12050 size_t total; 12051 12052 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 12053 return (-1); 12054 12055 if ((tomax = buf->dtb_tomax) == NULL) { 12056 dtrace_buffer_drop(buf); 12057 return (-1); 12058 } 12059 12060 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 12061 while (offs & (align - 1)) { 12062 /* 12063 * Assert that our alignment is off by a number which 12064 * is itself sizeof (uint32_t) aligned. 12065 */ 12066 ASSERT(!((align - (offs & (align - 1))) & 12067 (sizeof (uint32_t) - 1))); 12068 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12069 offs += sizeof (uint32_t); 12070 } 12071 12072 if ((soffs = offs + needed) > buf->dtb_size) { 12073 dtrace_buffer_drop(buf); 12074 return (-1); 12075 } 12076 12077 if (mstate == NULL) 12078 return (offs); 12079 12080 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 12081 mstate->dtms_scratch_size = buf->dtb_size - soffs; 12082 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12083 12084 return (offs); 12085 } 12086 12087 if (buf->dtb_flags & DTRACEBUF_FILL) { 12088 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 12089 (buf->dtb_flags & DTRACEBUF_FULL)) 12090 return (-1); 12091 goto out; 12092 } 12093 12094 total = needed + (offs & (align - 1)); 12095 12096 /* 12097 * For a ring buffer, life is quite a bit more complicated. Before 12098 * we can store any padding, we need to adjust our wrapping offset. 12099 * (If we've never before wrapped or we're not about to, no adjustment 12100 * is required.) 12101 */ 12102 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 12103 offs + total > buf->dtb_size) { 12104 woffs = buf->dtb_xamot_offset; 12105 12106 if (offs + total > buf->dtb_size) { 12107 /* 12108 * We can't fit in the end of the buffer. First, a 12109 * sanity check that we can fit in the buffer at all. 12110 */ 12111 if (total > buf->dtb_size) { 12112 dtrace_buffer_drop(buf); 12113 return (-1); 12114 } 12115 12116 /* 12117 * We're going to be storing at the top of the buffer, 12118 * so now we need to deal with the wrapped offset. We 12119 * only reset our wrapped offset to 0 if it is 12120 * currently greater than the current offset. If it 12121 * is less than the current offset, it is because a 12122 * previous allocation induced a wrap -- but the 12123 * allocation didn't subsequently take the space due 12124 * to an error or false predicate evaluation. In this 12125 * case, we'll just leave the wrapped offset alone: if 12126 * the wrapped offset hasn't been advanced far enough 12127 * for this allocation, it will be adjusted in the 12128 * lower loop. 12129 */ 12130 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 12131 if (woffs >= offs) 12132 woffs = 0; 12133 } else { 12134 woffs = 0; 12135 } 12136 12137 /* 12138 * Now we know that we're going to be storing to the 12139 * top of the buffer and that there is room for us 12140 * there. We need to clear the buffer from the current 12141 * offset to the end (there may be old gunk there). 12142 */ 12143 while (offs < buf->dtb_size) 12144 tomax[offs++] = 0; 12145 12146 /* 12147 * We need to set our offset to zero. And because we 12148 * are wrapping, we need to set the bit indicating as 12149 * much. We can also adjust our needed space back 12150 * down to the space required by the ECB -- we know 12151 * that the top of the buffer is aligned. 12152 */ 12153 offs = 0; 12154 total = needed; 12155 buf->dtb_flags |= DTRACEBUF_WRAPPED; 12156 } else { 12157 /* 12158 * There is room for us in the buffer, so we simply 12159 * need to check the wrapped offset. 12160 */ 12161 if (woffs < offs) { 12162 /* 12163 * The wrapped offset is less than the offset. 12164 * This can happen if we allocated buffer space 12165 * that induced a wrap, but then we didn't 12166 * subsequently take the space due to an error 12167 * or false predicate evaluation. This is 12168 * okay; we know that _this_ allocation isn't 12169 * going to induce a wrap. We still can't 12170 * reset the wrapped offset to be zero, 12171 * however: the space may have been trashed in 12172 * the previous failed probe attempt. But at 12173 * least the wrapped offset doesn't need to 12174 * be adjusted at all... 12175 */ 12176 goto out; 12177 } 12178 } 12179 12180 while (offs + total > woffs) { 12181 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 12182 size_t size; 12183 12184 if (epid == DTRACE_EPIDNONE) { 12185 size = sizeof (uint32_t); 12186 } else { 12187 ASSERT3U(epid, <=, state->dts_necbs); 12188 ASSERT(state->dts_ecbs[epid - 1] != NULL); 12189 12190 size = state->dts_ecbs[epid - 1]->dte_size; 12191 } 12192 12193 ASSERT(woffs + size <= buf->dtb_size); 12194 ASSERT(size != 0); 12195 12196 if (woffs + size == buf->dtb_size) { 12197 /* 12198 * We've reached the end of the buffer; we want 12199 * to set the wrapped offset to 0 and break 12200 * out. However, if the offs is 0, then we're 12201 * in a strange edge-condition: the amount of 12202 * space that we want to reserve plus the size 12203 * of the record that we're overwriting is 12204 * greater than the size of the buffer. This 12205 * is problematic because if we reserve the 12206 * space but subsequently don't consume it (due 12207 * to a failed predicate or error) the wrapped 12208 * offset will be 0 -- yet the EPID at offset 0 12209 * will not be committed. This situation is 12210 * relatively easy to deal with: if we're in 12211 * this case, the buffer is indistinguishable 12212 * from one that hasn't wrapped; we need only 12213 * finish the job by clearing the wrapped bit, 12214 * explicitly setting the offset to be 0, and 12215 * zero'ing out the old data in the buffer. 12216 */ 12217 if (offs == 0) { 12218 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 12219 buf->dtb_offset = 0; 12220 woffs = total; 12221 12222 while (woffs < buf->dtb_size) 12223 tomax[woffs++] = 0; 12224 } 12225 12226 woffs = 0; 12227 break; 12228 } 12229 12230 woffs += size; 12231 } 12232 12233 /* 12234 * We have a wrapped offset. It may be that the wrapped offset 12235 * has become zero -- that's okay. 12236 */ 12237 buf->dtb_xamot_offset = woffs; 12238 } 12239 12240 out: 12241 /* 12242 * Now we can plow the buffer with any necessary padding. 12243 */ 12244 while (offs & (align - 1)) { 12245 /* 12246 * Assert that our alignment is off by a number which 12247 * is itself sizeof (uint32_t) aligned. 12248 */ 12249 ASSERT(!((align - (offs & (align - 1))) & 12250 (sizeof (uint32_t) - 1))); 12251 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12252 offs += sizeof (uint32_t); 12253 } 12254 12255 if (buf->dtb_flags & DTRACEBUF_FILL) { 12256 if (offs + needed > buf->dtb_size - state->dts_reserve) { 12257 buf->dtb_flags |= DTRACEBUF_FULL; 12258 return (-1); 12259 } 12260 } 12261 12262 if (mstate == NULL) 12263 return (offs); 12264 12265 /* 12266 * For ring buffers and fill buffers, the scratch space is always 12267 * the inactive buffer. 12268 */ 12269 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 12270 mstate->dtms_scratch_size = buf->dtb_size; 12271 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12272 12273 return (offs); 12274 } 12275 12276 static void 12277 dtrace_buffer_polish(dtrace_buffer_t *buf) 12278 { 12279 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 12280 ASSERT(MUTEX_HELD(&dtrace_lock)); 12281 12282 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 12283 return; 12284 12285 /* 12286 * We need to polish the ring buffer. There are three cases: 12287 * 12288 * - The first (and presumably most common) is that there is no gap 12289 * between the buffer offset and the wrapped offset. In this case, 12290 * there is nothing in the buffer that isn't valid data; we can 12291 * mark the buffer as polished and return. 12292 * 12293 * - The second (less common than the first but still more common 12294 * than the third) is that there is a gap between the buffer offset 12295 * and the wrapped offset, and the wrapped offset is larger than the 12296 * buffer offset. This can happen because of an alignment issue, or 12297 * can happen because of a call to dtrace_buffer_reserve() that 12298 * didn't subsequently consume the buffer space. In this case, 12299 * we need to zero the data from the buffer offset to the wrapped 12300 * offset. 12301 * 12302 * - The third (and least common) is that there is a gap between the 12303 * buffer offset and the wrapped offset, but the wrapped offset is 12304 * _less_ than the buffer offset. This can only happen because a 12305 * call to dtrace_buffer_reserve() induced a wrap, but the space 12306 * was not subsequently consumed. In this case, we need to zero the 12307 * space from the offset to the end of the buffer _and_ from the 12308 * top of the buffer to the wrapped offset. 12309 */ 12310 if (buf->dtb_offset < buf->dtb_xamot_offset) { 12311 bzero(buf->dtb_tomax + buf->dtb_offset, 12312 buf->dtb_xamot_offset - buf->dtb_offset); 12313 } 12314 12315 if (buf->dtb_offset > buf->dtb_xamot_offset) { 12316 bzero(buf->dtb_tomax + buf->dtb_offset, 12317 buf->dtb_size - buf->dtb_offset); 12318 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 12319 } 12320 } 12321 12322 /* 12323 * This routine determines if data generated at the specified time has likely 12324 * been entirely consumed at user-level. This routine is called to determine 12325 * if an ECB on a defunct probe (but for an active enabling) can be safely 12326 * disabled and destroyed. 12327 */ 12328 static int 12329 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 12330 { 12331 int i; 12332 12333 for (i = 0; i < NCPU; i++) { 12334 dtrace_buffer_t *buf = &bufs[i]; 12335 12336 if (buf->dtb_size == 0) 12337 continue; 12338 12339 if (buf->dtb_flags & DTRACEBUF_RING) 12340 return (0); 12341 12342 if (!buf->dtb_switched && buf->dtb_offset != 0) 12343 return (0); 12344 12345 if (buf->dtb_switched - buf->dtb_interval < when) 12346 return (0); 12347 } 12348 12349 return (1); 12350 } 12351 12352 static void 12353 dtrace_buffer_free(dtrace_buffer_t *bufs) 12354 { 12355 int i; 12356 12357 for (i = 0; i < NCPU; i++) { 12358 dtrace_buffer_t *buf = &bufs[i]; 12359 12360 if (buf->dtb_tomax == NULL) { 12361 ASSERT(buf->dtb_xamot == NULL); 12362 ASSERT(buf->dtb_size == 0); 12363 continue; 12364 } 12365 12366 if (buf->dtb_xamot != NULL) { 12367 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 12368 kmem_free(buf->dtb_xamot, buf->dtb_size); 12369 } 12370 12371 kmem_free(buf->dtb_tomax, buf->dtb_size); 12372 buf->dtb_size = 0; 12373 buf->dtb_tomax = NULL; 12374 buf->dtb_xamot = NULL; 12375 } 12376 } 12377 12378 /* 12379 * DTrace Enabling Functions 12380 */ 12381 static dtrace_enabling_t * 12382 dtrace_enabling_create(dtrace_vstate_t *vstate) 12383 { 12384 dtrace_enabling_t *enab; 12385 12386 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 12387 enab->dten_vstate = vstate; 12388 12389 return (enab); 12390 } 12391 12392 static void 12393 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 12394 { 12395 dtrace_ecbdesc_t **ndesc; 12396 size_t osize, nsize; 12397 12398 /* 12399 * We can't add to enablings after we've enabled them, or after we've 12400 * retained them. 12401 */ 12402 ASSERT(enab->dten_probegen == 0); 12403 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12404 12405 if (enab->dten_ndesc < enab->dten_maxdesc) { 12406 enab->dten_desc[enab->dten_ndesc++] = ecb; 12407 return; 12408 } 12409 12410 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12411 12412 if (enab->dten_maxdesc == 0) { 12413 enab->dten_maxdesc = 1; 12414 } else { 12415 enab->dten_maxdesc <<= 1; 12416 } 12417 12418 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 12419 12420 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12421 ndesc = kmem_zalloc(nsize, KM_SLEEP); 12422 bcopy(enab->dten_desc, ndesc, osize); 12423 if (enab->dten_desc != NULL) 12424 kmem_free(enab->dten_desc, osize); 12425 12426 enab->dten_desc = ndesc; 12427 enab->dten_desc[enab->dten_ndesc++] = ecb; 12428 } 12429 12430 static void 12431 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 12432 dtrace_probedesc_t *pd) 12433 { 12434 dtrace_ecbdesc_t *new; 12435 dtrace_predicate_t *pred; 12436 dtrace_actdesc_t *act; 12437 12438 /* 12439 * We're going to create a new ECB description that matches the 12440 * specified ECB in every way, but has the specified probe description. 12441 */ 12442 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12443 12444 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 12445 dtrace_predicate_hold(pred); 12446 12447 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 12448 dtrace_actdesc_hold(act); 12449 12450 new->dted_action = ecb->dted_action; 12451 new->dted_pred = ecb->dted_pred; 12452 new->dted_probe = *pd; 12453 new->dted_uarg = ecb->dted_uarg; 12454 12455 dtrace_enabling_add(enab, new); 12456 } 12457 12458 static void 12459 dtrace_enabling_dump(dtrace_enabling_t *enab) 12460 { 12461 int i; 12462 12463 for (i = 0; i < enab->dten_ndesc; i++) { 12464 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 12465 12466 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 12467 desc->dtpd_provider, desc->dtpd_mod, 12468 desc->dtpd_func, desc->dtpd_name); 12469 } 12470 } 12471 12472 static void 12473 dtrace_enabling_destroy(dtrace_enabling_t *enab) 12474 { 12475 int i; 12476 dtrace_ecbdesc_t *ep; 12477 dtrace_vstate_t *vstate = enab->dten_vstate; 12478 12479 ASSERT(MUTEX_HELD(&dtrace_lock)); 12480 12481 for (i = 0; i < enab->dten_ndesc; i++) { 12482 dtrace_actdesc_t *act, *next; 12483 dtrace_predicate_t *pred; 12484 12485 ep = enab->dten_desc[i]; 12486 12487 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 12488 dtrace_predicate_release(pred, vstate); 12489 12490 for (act = ep->dted_action; act != NULL; act = next) { 12491 next = act->dtad_next; 12492 dtrace_actdesc_release(act, vstate); 12493 } 12494 12495 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12496 } 12497 12498 if (enab->dten_desc != NULL) 12499 kmem_free(enab->dten_desc, 12500 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 12501 12502 /* 12503 * If this was a retained enabling, decrement the dts_nretained count 12504 * and take it off of the dtrace_retained list. 12505 */ 12506 if (enab->dten_prev != NULL || enab->dten_next != NULL || 12507 dtrace_retained == enab) { 12508 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12509 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 12510 enab->dten_vstate->dtvs_state->dts_nretained--; 12511 dtrace_retained_gen++; 12512 } 12513 12514 if (enab->dten_prev == NULL) { 12515 if (dtrace_retained == enab) { 12516 dtrace_retained = enab->dten_next; 12517 12518 if (dtrace_retained != NULL) 12519 dtrace_retained->dten_prev = NULL; 12520 } 12521 } else { 12522 ASSERT(enab != dtrace_retained); 12523 ASSERT(dtrace_retained != NULL); 12524 enab->dten_prev->dten_next = enab->dten_next; 12525 } 12526 12527 if (enab->dten_next != NULL) { 12528 ASSERT(dtrace_retained != NULL); 12529 enab->dten_next->dten_prev = enab->dten_prev; 12530 } 12531 12532 kmem_free(enab, sizeof (dtrace_enabling_t)); 12533 } 12534 12535 static int 12536 dtrace_enabling_retain(dtrace_enabling_t *enab) 12537 { 12538 dtrace_state_t *state; 12539 12540 ASSERT(MUTEX_HELD(&dtrace_lock)); 12541 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12542 ASSERT(enab->dten_vstate != NULL); 12543 12544 state = enab->dten_vstate->dtvs_state; 12545 ASSERT(state != NULL); 12546 12547 /* 12548 * We only allow each state to retain dtrace_retain_max enablings. 12549 */ 12550 if (state->dts_nretained >= dtrace_retain_max) 12551 return (ENOSPC); 12552 12553 state->dts_nretained++; 12554 dtrace_retained_gen++; 12555 12556 if (dtrace_retained == NULL) { 12557 dtrace_retained = enab; 12558 return (0); 12559 } 12560 12561 enab->dten_next = dtrace_retained; 12562 dtrace_retained->dten_prev = enab; 12563 dtrace_retained = enab; 12564 12565 return (0); 12566 } 12567 12568 static int 12569 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 12570 dtrace_probedesc_t *create) 12571 { 12572 dtrace_enabling_t *new, *enab; 12573 int found = 0, err = ENOENT; 12574 12575 ASSERT(MUTEX_HELD(&dtrace_lock)); 12576 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 12577 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 12578 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 12579 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 12580 12581 new = dtrace_enabling_create(&state->dts_vstate); 12582 12583 /* 12584 * Iterate over all retained enablings, looking for enablings that 12585 * match the specified state. 12586 */ 12587 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12588 int i; 12589 12590 /* 12591 * dtvs_state can only be NULL for helper enablings -- and 12592 * helper enablings can't be retained. 12593 */ 12594 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12595 12596 if (enab->dten_vstate->dtvs_state != state) 12597 continue; 12598 12599 /* 12600 * Now iterate over each probe description; we're looking for 12601 * an exact match to the specified probe description. 12602 */ 12603 for (i = 0; i < enab->dten_ndesc; i++) { 12604 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12605 dtrace_probedesc_t *pd = &ep->dted_probe; 12606 12607 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 12608 continue; 12609 12610 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 12611 continue; 12612 12613 if (strcmp(pd->dtpd_func, match->dtpd_func)) 12614 continue; 12615 12616 if (strcmp(pd->dtpd_name, match->dtpd_name)) 12617 continue; 12618 12619 /* 12620 * We have a winning probe! Add it to our growing 12621 * enabling. 12622 */ 12623 found = 1; 12624 dtrace_enabling_addlike(new, ep, create); 12625 } 12626 } 12627 12628 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 12629 dtrace_enabling_destroy(new); 12630 return (err); 12631 } 12632 12633 return (0); 12634 } 12635 12636 static void 12637 dtrace_enabling_retract(dtrace_state_t *state) 12638 { 12639 dtrace_enabling_t *enab, *next; 12640 12641 ASSERT(MUTEX_HELD(&dtrace_lock)); 12642 12643 /* 12644 * Iterate over all retained enablings, destroy the enablings retained 12645 * for the specified state. 12646 */ 12647 for (enab = dtrace_retained; enab != NULL; enab = next) { 12648 next = enab->dten_next; 12649 12650 /* 12651 * dtvs_state can only be NULL for helper enablings -- and 12652 * helper enablings can't be retained. 12653 */ 12654 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12655 12656 if (enab->dten_vstate->dtvs_state == state) { 12657 ASSERT(state->dts_nretained > 0); 12658 dtrace_enabling_destroy(enab); 12659 } 12660 } 12661 12662 ASSERT(state->dts_nretained == 0); 12663 } 12664 12665 static int 12666 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 12667 { 12668 int i = 0; 12669 int matched = 0; 12670 12671 ASSERT(MUTEX_HELD(&cpu_lock)); 12672 ASSERT(MUTEX_HELD(&dtrace_lock)); 12673 12674 for (i = 0; i < enab->dten_ndesc; i++) { 12675 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12676 12677 enab->dten_current = ep; 12678 enab->dten_error = 0; 12679 12680 matched += dtrace_probe_enable(&ep->dted_probe, enab); 12681 12682 if (enab->dten_error != 0) { 12683 /* 12684 * If we get an error half-way through enabling the 12685 * probes, we kick out -- perhaps with some number of 12686 * them enabled. Leaving enabled probes enabled may 12687 * be slightly confusing for user-level, but we expect 12688 * that no one will attempt to actually drive on in 12689 * the face of such errors. If this is an anonymous 12690 * enabling (indicated with a NULL nmatched pointer), 12691 * we cmn_err() a message. We aren't expecting to 12692 * get such an error -- such as it can exist at all, 12693 * it would be a result of corrupted DOF in the driver 12694 * properties. 12695 */ 12696 if (nmatched == NULL) { 12697 cmn_err(CE_WARN, "dtrace_enabling_match() " 12698 "error on %p: %d", (void *)ep, 12699 enab->dten_error); 12700 } 12701 12702 return (enab->dten_error); 12703 } 12704 } 12705 12706 enab->dten_probegen = dtrace_probegen; 12707 if (nmatched != NULL) 12708 *nmatched = matched; 12709 12710 return (0); 12711 } 12712 12713 static void 12714 dtrace_enabling_matchall(void) 12715 { 12716 dtrace_enabling_t *enab; 12717 12718 mutex_enter(&cpu_lock); 12719 mutex_enter(&dtrace_lock); 12720 12721 /* 12722 * Iterate over all retained enablings to see if any probes match 12723 * against them. We only perform this operation on enablings for which 12724 * we have sufficient permissions by virtue of being in the global zone 12725 * or in the same zone as the DTrace client. Because we can be called 12726 * after dtrace_detach() has been called, we cannot assert that there 12727 * are retained enablings. We can safely load from dtrace_retained, 12728 * however: the taskq_destroy() at the end of dtrace_detach() will 12729 * block pending our completion. 12730 */ 12731 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12732 #ifdef illumos 12733 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 12734 12735 if (INGLOBALZONE(curproc) || 12736 cr != NULL && getzoneid() == crgetzoneid(cr)) 12737 #endif 12738 (void) dtrace_enabling_match(enab, NULL); 12739 } 12740 12741 mutex_exit(&dtrace_lock); 12742 mutex_exit(&cpu_lock); 12743 } 12744 12745 /* 12746 * If an enabling is to be enabled without having matched probes (that is, if 12747 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 12748 * enabling must be _primed_ by creating an ECB for every ECB description. 12749 * This must be done to assure that we know the number of speculations, the 12750 * number of aggregations, the minimum buffer size needed, etc. before we 12751 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 12752 * enabling any probes, we create ECBs for every ECB decription, but with a 12753 * NULL probe -- which is exactly what this function does. 12754 */ 12755 static void 12756 dtrace_enabling_prime(dtrace_state_t *state) 12757 { 12758 dtrace_enabling_t *enab; 12759 int i; 12760 12761 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12762 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12763 12764 if (enab->dten_vstate->dtvs_state != state) 12765 continue; 12766 12767 /* 12768 * We don't want to prime an enabling more than once, lest 12769 * we allow a malicious user to induce resource exhaustion. 12770 * (The ECBs that result from priming an enabling aren't 12771 * leaked -- but they also aren't deallocated until the 12772 * consumer state is destroyed.) 12773 */ 12774 if (enab->dten_primed) 12775 continue; 12776 12777 for (i = 0; i < enab->dten_ndesc; i++) { 12778 enab->dten_current = enab->dten_desc[i]; 12779 (void) dtrace_probe_enable(NULL, enab); 12780 } 12781 12782 enab->dten_primed = 1; 12783 } 12784 } 12785 12786 /* 12787 * Called to indicate that probes should be provided due to retained 12788 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 12789 * must take an initial lap through the enabling calling the dtps_provide() 12790 * entry point explicitly to allow for autocreated probes. 12791 */ 12792 static void 12793 dtrace_enabling_provide(dtrace_provider_t *prv) 12794 { 12795 int i, all = 0; 12796 dtrace_probedesc_t desc; 12797 dtrace_genid_t gen; 12798 12799 ASSERT(MUTEX_HELD(&dtrace_lock)); 12800 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 12801 12802 if (prv == NULL) { 12803 all = 1; 12804 prv = dtrace_provider; 12805 } 12806 12807 do { 12808 dtrace_enabling_t *enab; 12809 void *parg = prv->dtpv_arg; 12810 12811 retry: 12812 gen = dtrace_retained_gen; 12813 for (enab = dtrace_retained; enab != NULL; 12814 enab = enab->dten_next) { 12815 for (i = 0; i < enab->dten_ndesc; i++) { 12816 desc = enab->dten_desc[i]->dted_probe; 12817 mutex_exit(&dtrace_lock); 12818 prv->dtpv_pops.dtps_provide(parg, &desc); 12819 mutex_enter(&dtrace_lock); 12820 /* 12821 * Process the retained enablings again if 12822 * they have changed while we weren't holding 12823 * dtrace_lock. 12824 */ 12825 if (gen != dtrace_retained_gen) 12826 goto retry; 12827 } 12828 } 12829 } while (all && (prv = prv->dtpv_next) != NULL); 12830 12831 mutex_exit(&dtrace_lock); 12832 dtrace_probe_provide(NULL, all ? NULL : prv); 12833 mutex_enter(&dtrace_lock); 12834 } 12835 12836 /* 12837 * Called to reap ECBs that are attached to probes from defunct providers. 12838 */ 12839 static void 12840 dtrace_enabling_reap(void) 12841 { 12842 dtrace_provider_t *prov; 12843 dtrace_probe_t *probe; 12844 dtrace_ecb_t *ecb; 12845 hrtime_t when; 12846 int i; 12847 12848 mutex_enter(&cpu_lock); 12849 mutex_enter(&dtrace_lock); 12850 12851 for (i = 0; i < dtrace_nprobes; i++) { 12852 if ((probe = dtrace_probes[i]) == NULL) 12853 continue; 12854 12855 if (probe->dtpr_ecb == NULL) 12856 continue; 12857 12858 prov = probe->dtpr_provider; 12859 12860 if ((when = prov->dtpv_defunct) == 0) 12861 continue; 12862 12863 /* 12864 * We have ECBs on a defunct provider: we want to reap these 12865 * ECBs to allow the provider to unregister. The destruction 12866 * of these ECBs must be done carefully: if we destroy the ECB 12867 * and the consumer later wishes to consume an EPID that 12868 * corresponds to the destroyed ECB (and if the EPID metadata 12869 * has not been previously consumed), the consumer will abort 12870 * processing on the unknown EPID. To reduce (but not, sadly, 12871 * eliminate) the possibility of this, we will only destroy an 12872 * ECB for a defunct provider if, for the state that 12873 * corresponds to the ECB: 12874 * 12875 * (a) There is no speculative tracing (which can effectively 12876 * cache an EPID for an arbitrary amount of time). 12877 * 12878 * (b) The principal buffers have been switched twice since the 12879 * provider became defunct. 12880 * 12881 * (c) The aggregation buffers are of zero size or have been 12882 * switched twice since the provider became defunct. 12883 * 12884 * We use dts_speculates to determine (a) and call a function 12885 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 12886 * that as soon as we've been unable to destroy one of the ECBs 12887 * associated with the probe, we quit trying -- reaping is only 12888 * fruitful in as much as we can destroy all ECBs associated 12889 * with the defunct provider's probes. 12890 */ 12891 while ((ecb = probe->dtpr_ecb) != NULL) { 12892 dtrace_state_t *state = ecb->dte_state; 12893 dtrace_buffer_t *buf = state->dts_buffer; 12894 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 12895 12896 if (state->dts_speculates) 12897 break; 12898 12899 if (!dtrace_buffer_consumed(buf, when)) 12900 break; 12901 12902 if (!dtrace_buffer_consumed(aggbuf, when)) 12903 break; 12904 12905 dtrace_ecb_disable(ecb); 12906 ASSERT(probe->dtpr_ecb != ecb); 12907 dtrace_ecb_destroy(ecb); 12908 } 12909 } 12910 12911 mutex_exit(&dtrace_lock); 12912 mutex_exit(&cpu_lock); 12913 } 12914 12915 /* 12916 * DTrace DOF Functions 12917 */ 12918 /*ARGSUSED*/ 12919 static void 12920 dtrace_dof_error(dof_hdr_t *dof, const char *str) 12921 { 12922 if (dtrace_err_verbose) 12923 cmn_err(CE_WARN, "failed to process DOF: %s", str); 12924 12925 #ifdef DTRACE_ERRDEBUG 12926 dtrace_errdebug(str); 12927 #endif 12928 } 12929 12930 /* 12931 * Create DOF out of a currently enabled state. Right now, we only create 12932 * DOF containing the run-time options -- but this could be expanded to create 12933 * complete DOF representing the enabled state. 12934 */ 12935 static dof_hdr_t * 12936 dtrace_dof_create(dtrace_state_t *state) 12937 { 12938 dof_hdr_t *dof; 12939 dof_sec_t *sec; 12940 dof_optdesc_t *opt; 12941 int i, len = sizeof (dof_hdr_t) + 12942 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 12943 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 12944 12945 ASSERT(MUTEX_HELD(&dtrace_lock)); 12946 12947 dof = kmem_zalloc(len, KM_SLEEP); 12948 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 12949 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 12950 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 12951 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 12952 12953 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 12954 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 12955 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 12956 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 12957 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 12958 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 12959 12960 dof->dofh_flags = 0; 12961 dof->dofh_hdrsize = sizeof (dof_hdr_t); 12962 dof->dofh_secsize = sizeof (dof_sec_t); 12963 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 12964 dof->dofh_secoff = sizeof (dof_hdr_t); 12965 dof->dofh_loadsz = len; 12966 dof->dofh_filesz = len; 12967 dof->dofh_pad = 0; 12968 12969 /* 12970 * Fill in the option section header... 12971 */ 12972 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 12973 sec->dofs_type = DOF_SECT_OPTDESC; 12974 sec->dofs_align = sizeof (uint64_t); 12975 sec->dofs_flags = DOF_SECF_LOAD; 12976 sec->dofs_entsize = sizeof (dof_optdesc_t); 12977 12978 opt = (dof_optdesc_t *)((uintptr_t)sec + 12979 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 12980 12981 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 12982 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 12983 12984 for (i = 0; i < DTRACEOPT_MAX; i++) { 12985 opt[i].dofo_option = i; 12986 opt[i].dofo_strtab = DOF_SECIDX_NONE; 12987 opt[i].dofo_value = state->dts_options[i]; 12988 } 12989 12990 return (dof); 12991 } 12992 12993 static dof_hdr_t * 12994 dtrace_dof_copyin(uintptr_t uarg, int *errp) 12995 { 12996 dof_hdr_t hdr, *dof; 12997 12998 ASSERT(!MUTEX_HELD(&dtrace_lock)); 12999 13000 /* 13001 * First, we're going to copyin() the sizeof (dof_hdr_t). 13002 */ 13003 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 13004 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13005 *errp = EFAULT; 13006 return (NULL); 13007 } 13008 13009 /* 13010 * Now we'll allocate the entire DOF and copy it in -- provided 13011 * that the length isn't outrageous. 13012 */ 13013 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13014 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13015 *errp = E2BIG; 13016 return (NULL); 13017 } 13018 13019 if (hdr.dofh_loadsz < sizeof (hdr)) { 13020 dtrace_dof_error(&hdr, "invalid load size"); 13021 *errp = EINVAL; 13022 return (NULL); 13023 } 13024 13025 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 13026 13027 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 13028 dof->dofh_loadsz != hdr.dofh_loadsz) { 13029 kmem_free(dof, hdr.dofh_loadsz); 13030 *errp = EFAULT; 13031 return (NULL); 13032 } 13033 13034 return (dof); 13035 } 13036 13037 #ifdef __FreeBSD__ 13038 static dof_hdr_t * 13039 dtrace_dof_copyin_proc(struct proc *p, uintptr_t uarg, int *errp) 13040 { 13041 dof_hdr_t hdr, *dof; 13042 struct thread *td; 13043 size_t loadsz; 13044 13045 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13046 13047 td = curthread; 13048 13049 /* 13050 * First, we're going to copyin() the sizeof (dof_hdr_t). 13051 */ 13052 if (proc_readmem(td, p, uarg, &hdr, sizeof(hdr)) != sizeof(hdr)) { 13053 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13054 *errp = EFAULT; 13055 return (NULL); 13056 } 13057 13058 /* 13059 * Now we'll allocate the entire DOF and copy it in -- provided 13060 * that the length isn't outrageous. 13061 */ 13062 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13063 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13064 *errp = E2BIG; 13065 return (NULL); 13066 } 13067 loadsz = (size_t)hdr.dofh_loadsz; 13068 13069 if (loadsz < sizeof (hdr)) { 13070 dtrace_dof_error(&hdr, "invalid load size"); 13071 *errp = EINVAL; 13072 return (NULL); 13073 } 13074 13075 dof = kmem_alloc(loadsz, KM_SLEEP); 13076 13077 if (proc_readmem(td, p, uarg, dof, loadsz) != loadsz || 13078 dof->dofh_loadsz != loadsz) { 13079 kmem_free(dof, hdr.dofh_loadsz); 13080 *errp = EFAULT; 13081 return (NULL); 13082 } 13083 13084 return (dof); 13085 } 13086 13087 static __inline uchar_t 13088 dtrace_dof_char(char c) 13089 { 13090 13091 switch (c) { 13092 case '0': 13093 case '1': 13094 case '2': 13095 case '3': 13096 case '4': 13097 case '5': 13098 case '6': 13099 case '7': 13100 case '8': 13101 case '9': 13102 return (c - '0'); 13103 case 'A': 13104 case 'B': 13105 case 'C': 13106 case 'D': 13107 case 'E': 13108 case 'F': 13109 return (c - 'A' + 10); 13110 case 'a': 13111 case 'b': 13112 case 'c': 13113 case 'd': 13114 case 'e': 13115 case 'f': 13116 return (c - 'a' + 10); 13117 } 13118 /* Should not reach here. */ 13119 return (0); 13120 } 13121 #endif /* __FreeBSD__ */ 13122 13123 static dof_hdr_t * 13124 dtrace_dof_property(const char *name) 13125 { 13126 uchar_t *buf; 13127 uint64_t loadsz; 13128 unsigned int len, i; 13129 dof_hdr_t *dof; 13130 13131 #ifdef illumos 13132 /* 13133 * Unfortunately, array of values in .conf files are always (and 13134 * only) interpreted to be integer arrays. We must read our DOF 13135 * as an integer array, and then squeeze it into a byte array. 13136 */ 13137 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 13138 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 13139 return (NULL); 13140 13141 for (i = 0; i < len; i++) 13142 buf[i] = (uchar_t)(((int *)buf)[i]); 13143 13144 if (len < sizeof (dof_hdr_t)) { 13145 ddi_prop_free(buf); 13146 dtrace_dof_error(NULL, "truncated header"); 13147 return (NULL); 13148 } 13149 13150 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 13151 ddi_prop_free(buf); 13152 dtrace_dof_error(NULL, "truncated DOF"); 13153 return (NULL); 13154 } 13155 13156 if (loadsz >= dtrace_dof_maxsize) { 13157 ddi_prop_free(buf); 13158 dtrace_dof_error(NULL, "oversized DOF"); 13159 return (NULL); 13160 } 13161 13162 dof = kmem_alloc(loadsz, KM_SLEEP); 13163 bcopy(buf, dof, loadsz); 13164 ddi_prop_free(buf); 13165 #else 13166 char *p; 13167 char *p_env; 13168 13169 if ((p_env = kern_getenv(name)) == NULL) 13170 return (NULL); 13171 13172 len = strlen(p_env) / 2; 13173 13174 buf = kmem_alloc(len, KM_SLEEP); 13175 13176 dof = (dof_hdr_t *) buf; 13177 13178 p = p_env; 13179 13180 for (i = 0; i < len; i++) { 13181 buf[i] = (dtrace_dof_char(p[0]) << 4) | 13182 dtrace_dof_char(p[1]); 13183 p += 2; 13184 } 13185 13186 freeenv(p_env); 13187 13188 if (len < sizeof (dof_hdr_t)) { 13189 kmem_free(buf, 0); 13190 dtrace_dof_error(NULL, "truncated header"); 13191 return (NULL); 13192 } 13193 13194 if (len < (loadsz = dof->dofh_loadsz)) { 13195 kmem_free(buf, 0); 13196 dtrace_dof_error(NULL, "truncated DOF"); 13197 return (NULL); 13198 } 13199 13200 if (loadsz >= dtrace_dof_maxsize) { 13201 kmem_free(buf, 0); 13202 dtrace_dof_error(NULL, "oversized DOF"); 13203 return (NULL); 13204 } 13205 #endif 13206 13207 return (dof); 13208 } 13209 13210 static void 13211 dtrace_dof_destroy(dof_hdr_t *dof) 13212 { 13213 kmem_free(dof, dof->dofh_loadsz); 13214 } 13215 13216 /* 13217 * Return the dof_sec_t pointer corresponding to a given section index. If the 13218 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 13219 * a type other than DOF_SECT_NONE is specified, the header is checked against 13220 * this type and NULL is returned if the types do not match. 13221 */ 13222 static dof_sec_t * 13223 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 13224 { 13225 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 13226 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 13227 13228 if (i >= dof->dofh_secnum) { 13229 dtrace_dof_error(dof, "referenced section index is invalid"); 13230 return (NULL); 13231 } 13232 13233 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 13234 dtrace_dof_error(dof, "referenced section is not loadable"); 13235 return (NULL); 13236 } 13237 13238 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 13239 dtrace_dof_error(dof, "referenced section is the wrong type"); 13240 return (NULL); 13241 } 13242 13243 return (sec); 13244 } 13245 13246 static dtrace_probedesc_t * 13247 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 13248 { 13249 dof_probedesc_t *probe; 13250 dof_sec_t *strtab; 13251 uintptr_t daddr = (uintptr_t)dof; 13252 uintptr_t str; 13253 size_t size; 13254 13255 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 13256 dtrace_dof_error(dof, "invalid probe section"); 13257 return (NULL); 13258 } 13259 13260 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13261 dtrace_dof_error(dof, "bad alignment in probe description"); 13262 return (NULL); 13263 } 13264 13265 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 13266 dtrace_dof_error(dof, "truncated probe description"); 13267 return (NULL); 13268 } 13269 13270 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 13271 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 13272 13273 if (strtab == NULL) 13274 return (NULL); 13275 13276 str = daddr + strtab->dofs_offset; 13277 size = strtab->dofs_size; 13278 13279 if (probe->dofp_provider >= strtab->dofs_size) { 13280 dtrace_dof_error(dof, "corrupt probe provider"); 13281 return (NULL); 13282 } 13283 13284 (void) strncpy(desc->dtpd_provider, 13285 (char *)(str + probe->dofp_provider), 13286 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 13287 13288 if (probe->dofp_mod >= strtab->dofs_size) { 13289 dtrace_dof_error(dof, "corrupt probe module"); 13290 return (NULL); 13291 } 13292 13293 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 13294 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 13295 13296 if (probe->dofp_func >= strtab->dofs_size) { 13297 dtrace_dof_error(dof, "corrupt probe function"); 13298 return (NULL); 13299 } 13300 13301 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 13302 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 13303 13304 if (probe->dofp_name >= strtab->dofs_size) { 13305 dtrace_dof_error(dof, "corrupt probe name"); 13306 return (NULL); 13307 } 13308 13309 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 13310 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 13311 13312 return (desc); 13313 } 13314 13315 static dtrace_difo_t * 13316 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13317 cred_t *cr) 13318 { 13319 dtrace_difo_t *dp; 13320 size_t ttl = 0; 13321 dof_difohdr_t *dofd; 13322 uintptr_t daddr = (uintptr_t)dof; 13323 size_t max = dtrace_difo_maxsize; 13324 int i, l, n; 13325 13326 static const struct { 13327 int section; 13328 int bufoffs; 13329 int lenoffs; 13330 int entsize; 13331 int align; 13332 const char *msg; 13333 } difo[] = { 13334 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 13335 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 13336 sizeof (dif_instr_t), "multiple DIF sections" }, 13337 13338 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 13339 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 13340 sizeof (uint64_t), "multiple integer tables" }, 13341 13342 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 13343 offsetof(dtrace_difo_t, dtdo_strlen), 0, 13344 sizeof (char), "multiple string tables" }, 13345 13346 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 13347 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 13348 sizeof (uint_t), "multiple variable tables" }, 13349 13350 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 13351 }; 13352 13353 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 13354 dtrace_dof_error(dof, "invalid DIFO header section"); 13355 return (NULL); 13356 } 13357 13358 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13359 dtrace_dof_error(dof, "bad alignment in DIFO header"); 13360 return (NULL); 13361 } 13362 13363 if (sec->dofs_size < sizeof (dof_difohdr_t) || 13364 sec->dofs_size % sizeof (dof_secidx_t)) { 13365 dtrace_dof_error(dof, "bad size in DIFO header"); 13366 return (NULL); 13367 } 13368 13369 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13370 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 13371 13372 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 13373 dp->dtdo_rtype = dofd->dofd_rtype; 13374 13375 for (l = 0; l < n; l++) { 13376 dof_sec_t *subsec; 13377 void **bufp; 13378 uint32_t *lenp; 13379 13380 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 13381 dofd->dofd_links[l])) == NULL) 13382 goto err; /* invalid section link */ 13383 13384 if (ttl + subsec->dofs_size > max) { 13385 dtrace_dof_error(dof, "exceeds maximum size"); 13386 goto err; 13387 } 13388 13389 ttl += subsec->dofs_size; 13390 13391 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 13392 if (subsec->dofs_type != difo[i].section) 13393 continue; 13394 13395 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 13396 dtrace_dof_error(dof, "section not loaded"); 13397 goto err; 13398 } 13399 13400 if (subsec->dofs_align != difo[i].align) { 13401 dtrace_dof_error(dof, "bad alignment"); 13402 goto err; 13403 } 13404 13405 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 13406 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 13407 13408 if (*bufp != NULL) { 13409 dtrace_dof_error(dof, difo[i].msg); 13410 goto err; 13411 } 13412 13413 if (difo[i].entsize != subsec->dofs_entsize) { 13414 dtrace_dof_error(dof, "entry size mismatch"); 13415 goto err; 13416 } 13417 13418 if (subsec->dofs_entsize != 0 && 13419 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 13420 dtrace_dof_error(dof, "corrupt entry size"); 13421 goto err; 13422 } 13423 13424 *lenp = subsec->dofs_size; 13425 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 13426 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 13427 *bufp, subsec->dofs_size); 13428 13429 if (subsec->dofs_entsize != 0) 13430 *lenp /= subsec->dofs_entsize; 13431 13432 break; 13433 } 13434 13435 /* 13436 * If we encounter a loadable DIFO sub-section that is not 13437 * known to us, assume this is a broken program and fail. 13438 */ 13439 if (difo[i].section == DOF_SECT_NONE && 13440 (subsec->dofs_flags & DOF_SECF_LOAD)) { 13441 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 13442 goto err; 13443 } 13444 } 13445 13446 if (dp->dtdo_buf == NULL) { 13447 /* 13448 * We can't have a DIF object without DIF text. 13449 */ 13450 dtrace_dof_error(dof, "missing DIF text"); 13451 goto err; 13452 } 13453 13454 /* 13455 * Before we validate the DIF object, run through the variable table 13456 * looking for the strings -- if any of their size are under, we'll set 13457 * their size to be the system-wide default string size. Note that 13458 * this should _not_ happen if the "strsize" option has been set -- 13459 * in this case, the compiler should have set the size to reflect the 13460 * setting of the option. 13461 */ 13462 for (i = 0; i < dp->dtdo_varlen; i++) { 13463 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 13464 dtrace_diftype_t *t = &v->dtdv_type; 13465 13466 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 13467 continue; 13468 13469 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 13470 t->dtdt_size = dtrace_strsize_default; 13471 } 13472 13473 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 13474 goto err; 13475 13476 dtrace_difo_init(dp, vstate); 13477 return (dp); 13478 13479 err: 13480 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 13481 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 13482 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 13483 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 13484 13485 kmem_free(dp, sizeof (dtrace_difo_t)); 13486 return (NULL); 13487 } 13488 13489 static dtrace_predicate_t * 13490 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13491 cred_t *cr) 13492 { 13493 dtrace_difo_t *dp; 13494 13495 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 13496 return (NULL); 13497 13498 return (dtrace_predicate_create(dp)); 13499 } 13500 13501 static dtrace_actdesc_t * 13502 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13503 cred_t *cr) 13504 { 13505 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 13506 dof_actdesc_t *desc; 13507 dof_sec_t *difosec; 13508 size_t offs; 13509 uintptr_t daddr = (uintptr_t)dof; 13510 uint64_t arg; 13511 dtrace_actkind_t kind; 13512 13513 if (sec->dofs_type != DOF_SECT_ACTDESC) { 13514 dtrace_dof_error(dof, "invalid action section"); 13515 return (NULL); 13516 } 13517 13518 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 13519 dtrace_dof_error(dof, "truncated action description"); 13520 return (NULL); 13521 } 13522 13523 if (sec->dofs_align != sizeof (uint64_t)) { 13524 dtrace_dof_error(dof, "bad alignment in action description"); 13525 return (NULL); 13526 } 13527 13528 if (sec->dofs_size < sec->dofs_entsize) { 13529 dtrace_dof_error(dof, "section entry size exceeds total size"); 13530 return (NULL); 13531 } 13532 13533 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 13534 dtrace_dof_error(dof, "bad entry size in action description"); 13535 return (NULL); 13536 } 13537 13538 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 13539 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 13540 return (NULL); 13541 } 13542 13543 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 13544 desc = (dof_actdesc_t *)(daddr + 13545 (uintptr_t)sec->dofs_offset + offs); 13546 kind = (dtrace_actkind_t)desc->dofa_kind; 13547 13548 if ((DTRACEACT_ISPRINTFLIKE(kind) && 13549 (kind != DTRACEACT_PRINTA || 13550 desc->dofa_strtab != DOF_SECIDX_NONE)) || 13551 (kind == DTRACEACT_DIFEXPR && 13552 desc->dofa_strtab != DOF_SECIDX_NONE)) { 13553 dof_sec_t *strtab; 13554 char *str, *fmt; 13555 uint64_t i; 13556 13557 /* 13558 * The argument to these actions is an index into the 13559 * DOF string table. For printf()-like actions, this 13560 * is the format string. For print(), this is the 13561 * CTF type of the expression result. 13562 */ 13563 if ((strtab = dtrace_dof_sect(dof, 13564 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 13565 goto err; 13566 13567 str = (char *)((uintptr_t)dof + 13568 (uintptr_t)strtab->dofs_offset); 13569 13570 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 13571 if (str[i] == '\0') 13572 break; 13573 } 13574 13575 if (i >= strtab->dofs_size) { 13576 dtrace_dof_error(dof, "bogus format string"); 13577 goto err; 13578 } 13579 13580 if (i == desc->dofa_arg) { 13581 dtrace_dof_error(dof, "empty format string"); 13582 goto err; 13583 } 13584 13585 i -= desc->dofa_arg; 13586 fmt = kmem_alloc(i + 1, KM_SLEEP); 13587 bcopy(&str[desc->dofa_arg], fmt, i + 1); 13588 arg = (uint64_t)(uintptr_t)fmt; 13589 } else { 13590 if (kind == DTRACEACT_PRINTA) { 13591 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 13592 arg = 0; 13593 } else { 13594 arg = desc->dofa_arg; 13595 } 13596 } 13597 13598 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 13599 desc->dofa_uarg, arg); 13600 13601 if (last != NULL) { 13602 last->dtad_next = act; 13603 } else { 13604 first = act; 13605 } 13606 13607 last = act; 13608 13609 if (desc->dofa_difo == DOF_SECIDX_NONE) 13610 continue; 13611 13612 if ((difosec = dtrace_dof_sect(dof, 13613 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 13614 goto err; 13615 13616 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 13617 13618 if (act->dtad_difo == NULL) 13619 goto err; 13620 } 13621 13622 ASSERT(first != NULL); 13623 return (first); 13624 13625 err: 13626 for (act = first; act != NULL; act = next) { 13627 next = act->dtad_next; 13628 dtrace_actdesc_release(act, vstate); 13629 } 13630 13631 return (NULL); 13632 } 13633 13634 static dtrace_ecbdesc_t * 13635 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13636 cred_t *cr) 13637 { 13638 dtrace_ecbdesc_t *ep; 13639 dof_ecbdesc_t *ecb; 13640 dtrace_probedesc_t *desc; 13641 dtrace_predicate_t *pred = NULL; 13642 13643 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 13644 dtrace_dof_error(dof, "truncated ECB description"); 13645 return (NULL); 13646 } 13647 13648 if (sec->dofs_align != sizeof (uint64_t)) { 13649 dtrace_dof_error(dof, "bad alignment in ECB description"); 13650 return (NULL); 13651 } 13652 13653 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 13654 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 13655 13656 if (sec == NULL) 13657 return (NULL); 13658 13659 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 13660 ep->dted_uarg = ecb->dofe_uarg; 13661 desc = &ep->dted_probe; 13662 13663 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 13664 goto err; 13665 13666 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 13667 if ((sec = dtrace_dof_sect(dof, 13668 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 13669 goto err; 13670 13671 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 13672 goto err; 13673 13674 ep->dted_pred.dtpdd_predicate = pred; 13675 } 13676 13677 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 13678 if ((sec = dtrace_dof_sect(dof, 13679 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 13680 goto err; 13681 13682 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 13683 13684 if (ep->dted_action == NULL) 13685 goto err; 13686 } 13687 13688 return (ep); 13689 13690 err: 13691 if (pred != NULL) 13692 dtrace_predicate_release(pred, vstate); 13693 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 13694 return (NULL); 13695 } 13696 13697 /* 13698 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 13699 * specified DOF. At present, this amounts to simply adding 'ubase' to the 13700 * site of any user SETX relocations to account for load object base address. 13701 * In the future, if we need other relocations, this function can be extended. 13702 */ 13703 static int 13704 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 13705 { 13706 uintptr_t daddr = (uintptr_t)dof; 13707 dof_relohdr_t *dofr = 13708 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13709 dof_sec_t *ss, *rs, *ts; 13710 dof_relodesc_t *r; 13711 uint_t i, n; 13712 13713 if (sec->dofs_size < sizeof (dof_relohdr_t) || 13714 sec->dofs_align != sizeof (dof_secidx_t)) { 13715 dtrace_dof_error(dof, "invalid relocation header"); 13716 return (-1); 13717 } 13718 13719 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 13720 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 13721 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 13722 13723 if (ss == NULL || rs == NULL || ts == NULL) 13724 return (-1); /* dtrace_dof_error() has been called already */ 13725 13726 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 13727 rs->dofs_align != sizeof (uint64_t)) { 13728 dtrace_dof_error(dof, "invalid relocation section"); 13729 return (-1); 13730 } 13731 13732 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 13733 n = rs->dofs_size / rs->dofs_entsize; 13734 13735 for (i = 0; i < n; i++) { 13736 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 13737 13738 switch (r->dofr_type) { 13739 case DOF_RELO_NONE: 13740 break; 13741 case DOF_RELO_SETX: 13742 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 13743 sizeof (uint64_t) > ts->dofs_size) { 13744 dtrace_dof_error(dof, "bad relocation offset"); 13745 return (-1); 13746 } 13747 13748 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 13749 dtrace_dof_error(dof, "misaligned setx relo"); 13750 return (-1); 13751 } 13752 13753 *(uint64_t *)taddr += ubase; 13754 break; 13755 default: 13756 dtrace_dof_error(dof, "invalid relocation type"); 13757 return (-1); 13758 } 13759 13760 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 13761 } 13762 13763 return (0); 13764 } 13765 13766 /* 13767 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 13768 * header: it should be at the front of a memory region that is at least 13769 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 13770 * size. It need not be validated in any other way. 13771 */ 13772 static int 13773 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 13774 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 13775 { 13776 uint64_t len = dof->dofh_loadsz, seclen; 13777 uintptr_t daddr = (uintptr_t)dof; 13778 dtrace_ecbdesc_t *ep; 13779 dtrace_enabling_t *enab; 13780 uint_t i; 13781 13782 ASSERT(MUTEX_HELD(&dtrace_lock)); 13783 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 13784 13785 /* 13786 * Check the DOF header identification bytes. In addition to checking 13787 * valid settings, we also verify that unused bits/bytes are zeroed so 13788 * we can use them later without fear of regressing existing binaries. 13789 */ 13790 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 13791 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 13792 dtrace_dof_error(dof, "DOF magic string mismatch"); 13793 return (-1); 13794 } 13795 13796 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 13797 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 13798 dtrace_dof_error(dof, "DOF has invalid data model"); 13799 return (-1); 13800 } 13801 13802 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 13803 dtrace_dof_error(dof, "DOF encoding mismatch"); 13804 return (-1); 13805 } 13806 13807 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 13808 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 13809 dtrace_dof_error(dof, "DOF version mismatch"); 13810 return (-1); 13811 } 13812 13813 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 13814 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 13815 return (-1); 13816 } 13817 13818 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 13819 dtrace_dof_error(dof, "DOF uses too many integer registers"); 13820 return (-1); 13821 } 13822 13823 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 13824 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 13825 return (-1); 13826 } 13827 13828 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 13829 if (dof->dofh_ident[i] != 0) { 13830 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 13831 return (-1); 13832 } 13833 } 13834 13835 if (dof->dofh_flags & ~DOF_FL_VALID) { 13836 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 13837 return (-1); 13838 } 13839 13840 if (dof->dofh_secsize == 0) { 13841 dtrace_dof_error(dof, "zero section header size"); 13842 return (-1); 13843 } 13844 13845 /* 13846 * Check that the section headers don't exceed the amount of DOF 13847 * data. Note that we cast the section size and number of sections 13848 * to uint64_t's to prevent possible overflow in the multiplication. 13849 */ 13850 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 13851 13852 if (dof->dofh_secoff > len || seclen > len || 13853 dof->dofh_secoff + seclen > len) { 13854 dtrace_dof_error(dof, "truncated section headers"); 13855 return (-1); 13856 } 13857 13858 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 13859 dtrace_dof_error(dof, "misaligned section headers"); 13860 return (-1); 13861 } 13862 13863 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 13864 dtrace_dof_error(dof, "misaligned section size"); 13865 return (-1); 13866 } 13867 13868 /* 13869 * Take an initial pass through the section headers to be sure that 13870 * the headers don't have stray offsets. If the 'noprobes' flag is 13871 * set, do not permit sections relating to providers, probes, or args. 13872 */ 13873 for (i = 0; i < dof->dofh_secnum; i++) { 13874 dof_sec_t *sec = (dof_sec_t *)(daddr + 13875 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13876 13877 if (noprobes) { 13878 switch (sec->dofs_type) { 13879 case DOF_SECT_PROVIDER: 13880 case DOF_SECT_PROBES: 13881 case DOF_SECT_PRARGS: 13882 case DOF_SECT_PROFFS: 13883 dtrace_dof_error(dof, "illegal sections " 13884 "for enabling"); 13885 return (-1); 13886 } 13887 } 13888 13889 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 13890 !(sec->dofs_flags & DOF_SECF_LOAD)) { 13891 dtrace_dof_error(dof, "loadable section with load " 13892 "flag unset"); 13893 return (-1); 13894 } 13895 13896 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 13897 continue; /* just ignore non-loadable sections */ 13898 13899 if (!ISP2(sec->dofs_align)) { 13900 dtrace_dof_error(dof, "bad section alignment"); 13901 return (-1); 13902 } 13903 13904 if (sec->dofs_offset & (sec->dofs_align - 1)) { 13905 dtrace_dof_error(dof, "misaligned section"); 13906 return (-1); 13907 } 13908 13909 if (sec->dofs_offset > len || sec->dofs_size > len || 13910 sec->dofs_offset + sec->dofs_size > len) { 13911 dtrace_dof_error(dof, "corrupt section header"); 13912 return (-1); 13913 } 13914 13915 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 13916 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 13917 dtrace_dof_error(dof, "non-terminating string table"); 13918 return (-1); 13919 } 13920 } 13921 13922 /* 13923 * Take a second pass through the sections and locate and perform any 13924 * relocations that are present. We do this after the first pass to 13925 * be sure that all sections have had their headers validated. 13926 */ 13927 for (i = 0; i < dof->dofh_secnum; i++) { 13928 dof_sec_t *sec = (dof_sec_t *)(daddr + 13929 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13930 13931 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 13932 continue; /* skip sections that are not loadable */ 13933 13934 switch (sec->dofs_type) { 13935 case DOF_SECT_URELHDR: 13936 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 13937 return (-1); 13938 break; 13939 } 13940 } 13941 13942 if ((enab = *enabp) == NULL) 13943 enab = *enabp = dtrace_enabling_create(vstate); 13944 13945 for (i = 0; i < dof->dofh_secnum; i++) { 13946 dof_sec_t *sec = (dof_sec_t *)(daddr + 13947 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13948 13949 if (sec->dofs_type != DOF_SECT_ECBDESC) 13950 continue; 13951 13952 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 13953 dtrace_enabling_destroy(enab); 13954 *enabp = NULL; 13955 return (-1); 13956 } 13957 13958 dtrace_enabling_add(enab, ep); 13959 } 13960 13961 return (0); 13962 } 13963 13964 /* 13965 * Process DOF for any options. This routine assumes that the DOF has been 13966 * at least processed by dtrace_dof_slurp(). 13967 */ 13968 static int 13969 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 13970 { 13971 int i, rval; 13972 uint32_t entsize; 13973 size_t offs; 13974 dof_optdesc_t *desc; 13975 13976 for (i = 0; i < dof->dofh_secnum; i++) { 13977 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 13978 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13979 13980 if (sec->dofs_type != DOF_SECT_OPTDESC) 13981 continue; 13982 13983 if (sec->dofs_align != sizeof (uint64_t)) { 13984 dtrace_dof_error(dof, "bad alignment in " 13985 "option description"); 13986 return (EINVAL); 13987 } 13988 13989 if ((entsize = sec->dofs_entsize) == 0) { 13990 dtrace_dof_error(dof, "zeroed option entry size"); 13991 return (EINVAL); 13992 } 13993 13994 if (entsize < sizeof (dof_optdesc_t)) { 13995 dtrace_dof_error(dof, "bad option entry size"); 13996 return (EINVAL); 13997 } 13998 13999 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 14000 desc = (dof_optdesc_t *)((uintptr_t)dof + 14001 (uintptr_t)sec->dofs_offset + offs); 14002 14003 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 14004 dtrace_dof_error(dof, "non-zero option string"); 14005 return (EINVAL); 14006 } 14007 14008 if (desc->dofo_value == DTRACEOPT_UNSET) { 14009 dtrace_dof_error(dof, "unset option"); 14010 return (EINVAL); 14011 } 14012 14013 if ((rval = dtrace_state_option(state, 14014 desc->dofo_option, desc->dofo_value)) != 0) { 14015 dtrace_dof_error(dof, "rejected option"); 14016 return (rval); 14017 } 14018 } 14019 } 14020 14021 return (0); 14022 } 14023 14024 /* 14025 * DTrace Consumer State Functions 14026 */ 14027 static int 14028 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 14029 { 14030 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 14031 void *base; 14032 uintptr_t limit; 14033 dtrace_dynvar_t *dvar, *next, *start; 14034 int i; 14035 14036 ASSERT(MUTEX_HELD(&dtrace_lock)); 14037 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 14038 14039 bzero(dstate, sizeof (dtrace_dstate_t)); 14040 14041 if ((dstate->dtds_chunksize = chunksize) == 0) 14042 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 14043 14044 VERIFY(dstate->dtds_chunksize < LONG_MAX); 14045 14046 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 14047 size = min; 14048 14049 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 14050 return (ENOMEM); 14051 14052 dstate->dtds_size = size; 14053 dstate->dtds_base = base; 14054 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 14055 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 14056 14057 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 14058 14059 if (hashsize != 1 && (hashsize & 1)) 14060 hashsize--; 14061 14062 dstate->dtds_hashsize = hashsize; 14063 dstate->dtds_hash = dstate->dtds_base; 14064 14065 /* 14066 * Set all of our hash buckets to point to the single sink, and (if 14067 * it hasn't already been set), set the sink's hash value to be the 14068 * sink sentinel value. The sink is needed for dynamic variable 14069 * lookups to know that they have iterated over an entire, valid hash 14070 * chain. 14071 */ 14072 for (i = 0; i < hashsize; i++) 14073 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 14074 14075 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 14076 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 14077 14078 /* 14079 * Determine number of active CPUs. Divide free list evenly among 14080 * active CPUs. 14081 */ 14082 start = (dtrace_dynvar_t *) 14083 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 14084 limit = (uintptr_t)base + size; 14085 14086 VERIFY((uintptr_t)start < limit); 14087 VERIFY((uintptr_t)start >= (uintptr_t)base); 14088 14089 maxper = (limit - (uintptr_t)start) / NCPU; 14090 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 14091 14092 #ifndef illumos 14093 CPU_FOREACH(i) { 14094 #else 14095 for (i = 0; i < NCPU; i++) { 14096 #endif 14097 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 14098 14099 /* 14100 * If we don't even have enough chunks to make it once through 14101 * NCPUs, we're just going to allocate everything to the first 14102 * CPU. And if we're on the last CPU, we're going to allocate 14103 * whatever is left over. In either case, we set the limit to 14104 * be the limit of the dynamic variable space. 14105 */ 14106 if (maxper == 0 || i == NCPU - 1) { 14107 limit = (uintptr_t)base + size; 14108 start = NULL; 14109 } else { 14110 limit = (uintptr_t)start + maxper; 14111 start = (dtrace_dynvar_t *)limit; 14112 } 14113 14114 VERIFY(limit <= (uintptr_t)base + size); 14115 14116 for (;;) { 14117 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 14118 dstate->dtds_chunksize); 14119 14120 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 14121 break; 14122 14123 VERIFY((uintptr_t)dvar >= (uintptr_t)base && 14124 (uintptr_t)dvar <= (uintptr_t)base + size); 14125 dvar->dtdv_next = next; 14126 dvar = next; 14127 } 14128 14129 if (maxper == 0) 14130 break; 14131 } 14132 14133 return (0); 14134 } 14135 14136 static void 14137 dtrace_dstate_fini(dtrace_dstate_t *dstate) 14138 { 14139 ASSERT(MUTEX_HELD(&cpu_lock)); 14140 14141 if (dstate->dtds_base == NULL) 14142 return; 14143 14144 kmem_free(dstate->dtds_base, dstate->dtds_size); 14145 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 14146 } 14147 14148 static void 14149 dtrace_vstate_fini(dtrace_vstate_t *vstate) 14150 { 14151 /* 14152 * Logical XOR, where are you? 14153 */ 14154 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 14155 14156 if (vstate->dtvs_nglobals > 0) { 14157 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 14158 sizeof (dtrace_statvar_t *)); 14159 } 14160 14161 if (vstate->dtvs_ntlocals > 0) { 14162 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 14163 sizeof (dtrace_difv_t)); 14164 } 14165 14166 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 14167 14168 if (vstate->dtvs_nlocals > 0) { 14169 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 14170 sizeof (dtrace_statvar_t *)); 14171 } 14172 } 14173 14174 #ifdef illumos 14175 static void 14176 dtrace_state_clean(dtrace_state_t *state) 14177 { 14178 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14179 return; 14180 14181 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14182 dtrace_speculation_clean(state); 14183 } 14184 14185 static void 14186 dtrace_state_deadman(dtrace_state_t *state) 14187 { 14188 hrtime_t now; 14189 14190 dtrace_sync(); 14191 14192 now = dtrace_gethrtime(); 14193 14194 if (state != dtrace_anon.dta_state && 14195 now - state->dts_laststatus >= dtrace_deadman_user) 14196 return; 14197 14198 /* 14199 * We must be sure that dts_alive never appears to be less than the 14200 * value upon entry to dtrace_state_deadman(), and because we lack a 14201 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14202 * store INT64_MAX to it, followed by a memory barrier, followed by 14203 * the new value. This assures that dts_alive never appears to be 14204 * less than its true value, regardless of the order in which the 14205 * stores to the underlying storage are issued. 14206 */ 14207 state->dts_alive = INT64_MAX; 14208 dtrace_membar_producer(); 14209 state->dts_alive = now; 14210 } 14211 #else /* !illumos */ 14212 static void 14213 dtrace_state_clean(void *arg) 14214 { 14215 dtrace_state_t *state = arg; 14216 dtrace_optval_t *opt = state->dts_options; 14217 14218 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14219 return; 14220 14221 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14222 dtrace_speculation_clean(state); 14223 14224 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14225 dtrace_state_clean, state); 14226 } 14227 14228 static void 14229 dtrace_state_deadman(void *arg) 14230 { 14231 dtrace_state_t *state = arg; 14232 hrtime_t now; 14233 14234 dtrace_sync(); 14235 14236 dtrace_debug_output(); 14237 14238 now = dtrace_gethrtime(); 14239 14240 if (state != dtrace_anon.dta_state && 14241 now - state->dts_laststatus >= dtrace_deadman_user) 14242 return; 14243 14244 /* 14245 * We must be sure that dts_alive never appears to be less than the 14246 * value upon entry to dtrace_state_deadman(), and because we lack a 14247 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14248 * store INT64_MAX to it, followed by a memory barrier, followed by 14249 * the new value. This assures that dts_alive never appears to be 14250 * less than its true value, regardless of the order in which the 14251 * stores to the underlying storage are issued. 14252 */ 14253 state->dts_alive = INT64_MAX; 14254 dtrace_membar_producer(); 14255 state->dts_alive = now; 14256 14257 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14258 dtrace_state_deadman, state); 14259 } 14260 #endif /* illumos */ 14261 14262 static dtrace_state_t * 14263 #ifdef illumos 14264 dtrace_state_create(dev_t *devp, cred_t *cr) 14265 #else 14266 dtrace_state_create(struct cdev *dev) 14267 #endif 14268 { 14269 #ifdef illumos 14270 minor_t minor; 14271 major_t major; 14272 #else 14273 cred_t *cr = NULL; 14274 int m = 0; 14275 #endif 14276 char c[30]; 14277 dtrace_state_t *state; 14278 dtrace_optval_t *opt; 14279 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 14280 14281 ASSERT(MUTEX_HELD(&dtrace_lock)); 14282 ASSERT(MUTEX_HELD(&cpu_lock)); 14283 14284 #ifdef illumos 14285 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 14286 VM_BESTFIT | VM_SLEEP); 14287 14288 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 14289 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14290 return (NULL); 14291 } 14292 14293 state = ddi_get_soft_state(dtrace_softstate, minor); 14294 #else 14295 if (dev != NULL) { 14296 cr = dev->si_cred; 14297 m = dev2unit(dev); 14298 } 14299 14300 /* Allocate memory for the state. */ 14301 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 14302 #endif 14303 14304 state->dts_epid = DTRACE_EPIDNONE + 1; 14305 14306 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 14307 #ifdef illumos 14308 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 14309 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14310 14311 if (devp != NULL) { 14312 major = getemajor(*devp); 14313 } else { 14314 major = ddi_driver_major(dtrace_devi); 14315 } 14316 14317 state->dts_dev = makedevice(major, minor); 14318 14319 if (devp != NULL) 14320 *devp = state->dts_dev; 14321 #else 14322 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 14323 state->dts_dev = dev; 14324 #endif 14325 14326 /* 14327 * We allocate NCPU buffers. On the one hand, this can be quite 14328 * a bit of memory per instance (nearly 36K on a Starcat). On the 14329 * other hand, it saves an additional memory reference in the probe 14330 * path. 14331 */ 14332 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 14333 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 14334 14335 #ifdef illumos 14336 state->dts_cleaner = CYCLIC_NONE; 14337 state->dts_deadman = CYCLIC_NONE; 14338 #else 14339 callout_init(&state->dts_cleaner, 1); 14340 callout_init(&state->dts_deadman, 1); 14341 #endif 14342 state->dts_vstate.dtvs_state = state; 14343 14344 for (i = 0; i < DTRACEOPT_MAX; i++) 14345 state->dts_options[i] = DTRACEOPT_UNSET; 14346 14347 /* 14348 * Set the default options. 14349 */ 14350 opt = state->dts_options; 14351 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 14352 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 14353 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 14354 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 14355 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 14356 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 14357 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 14358 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 14359 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 14360 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 14361 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 14362 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 14363 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 14364 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 14365 14366 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 14367 14368 /* 14369 * Depending on the user credentials, we set flag bits which alter probe 14370 * visibility or the amount of destructiveness allowed. In the case of 14371 * actual anonymous tracing, or the possession of all privileges, all of 14372 * the normal checks are bypassed. 14373 */ 14374 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 14375 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 14376 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 14377 } else { 14378 /* 14379 * Set up the credentials for this instantiation. We take a 14380 * hold on the credential to prevent it from disappearing on 14381 * us; this in turn prevents the zone_t referenced by this 14382 * credential from disappearing. This means that we can 14383 * examine the credential and the zone from probe context. 14384 */ 14385 crhold(cr); 14386 state->dts_cred.dcr_cred = cr; 14387 14388 /* 14389 * CRA_PROC means "we have *some* privilege for dtrace" and 14390 * unlocks the use of variables like pid, zonename, etc. 14391 */ 14392 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 14393 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14394 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 14395 } 14396 14397 /* 14398 * dtrace_user allows use of syscall and profile providers. 14399 * If the user also has proc_owner and/or proc_zone, we 14400 * extend the scope to include additional visibility and 14401 * destructive power. 14402 */ 14403 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 14404 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 14405 state->dts_cred.dcr_visible |= 14406 DTRACE_CRV_ALLPROC; 14407 14408 state->dts_cred.dcr_action |= 14409 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14410 } 14411 14412 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 14413 state->dts_cred.dcr_visible |= 14414 DTRACE_CRV_ALLZONE; 14415 14416 state->dts_cred.dcr_action |= 14417 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14418 } 14419 14420 /* 14421 * If we have all privs in whatever zone this is, 14422 * we can do destructive things to processes which 14423 * have altered credentials. 14424 */ 14425 #ifdef illumos 14426 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14427 cr->cr_zone->zone_privset)) { 14428 state->dts_cred.dcr_action |= 14429 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14430 } 14431 #endif 14432 } 14433 14434 /* 14435 * Holding the dtrace_kernel privilege also implies that 14436 * the user has the dtrace_user privilege from a visibility 14437 * perspective. But without further privileges, some 14438 * destructive actions are not available. 14439 */ 14440 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 14441 /* 14442 * Make all probes in all zones visible. However, 14443 * this doesn't mean that all actions become available 14444 * to all zones. 14445 */ 14446 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 14447 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 14448 14449 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 14450 DTRACE_CRA_PROC; 14451 /* 14452 * Holding proc_owner means that destructive actions 14453 * for *this* zone are allowed. 14454 */ 14455 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14456 state->dts_cred.dcr_action |= 14457 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14458 14459 /* 14460 * Holding proc_zone means that destructive actions 14461 * for this user/group ID in all zones is allowed. 14462 */ 14463 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14464 state->dts_cred.dcr_action |= 14465 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14466 14467 #ifdef illumos 14468 /* 14469 * If we have all privs in whatever zone this is, 14470 * we can do destructive things to processes which 14471 * have altered credentials. 14472 */ 14473 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14474 cr->cr_zone->zone_privset)) { 14475 state->dts_cred.dcr_action |= 14476 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14477 } 14478 #endif 14479 } 14480 14481 /* 14482 * Holding the dtrace_proc privilege gives control over fasttrap 14483 * and pid providers. We need to grant wider destructive 14484 * privileges in the event that the user has proc_owner and/or 14485 * proc_zone. 14486 */ 14487 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14488 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14489 state->dts_cred.dcr_action |= 14490 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14491 14492 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14493 state->dts_cred.dcr_action |= 14494 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14495 } 14496 } 14497 14498 return (state); 14499 } 14500 14501 static int 14502 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 14503 { 14504 dtrace_optval_t *opt = state->dts_options, size; 14505 processorid_t cpu = 0;; 14506 int flags = 0, rval, factor, divisor = 1; 14507 14508 ASSERT(MUTEX_HELD(&dtrace_lock)); 14509 ASSERT(MUTEX_HELD(&cpu_lock)); 14510 ASSERT(which < DTRACEOPT_MAX); 14511 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 14512 (state == dtrace_anon.dta_state && 14513 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 14514 14515 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 14516 return (0); 14517 14518 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 14519 cpu = opt[DTRACEOPT_CPU]; 14520 14521 if (which == DTRACEOPT_SPECSIZE) 14522 flags |= DTRACEBUF_NOSWITCH; 14523 14524 if (which == DTRACEOPT_BUFSIZE) { 14525 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 14526 flags |= DTRACEBUF_RING; 14527 14528 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 14529 flags |= DTRACEBUF_FILL; 14530 14531 if (state != dtrace_anon.dta_state || 14532 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14533 flags |= DTRACEBUF_INACTIVE; 14534 } 14535 14536 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 14537 /* 14538 * The size must be 8-byte aligned. If the size is not 8-byte 14539 * aligned, drop it down by the difference. 14540 */ 14541 if (size & (sizeof (uint64_t) - 1)) 14542 size -= size & (sizeof (uint64_t) - 1); 14543 14544 if (size < state->dts_reserve) { 14545 /* 14546 * Buffers always must be large enough to accommodate 14547 * their prereserved space. We return E2BIG instead 14548 * of ENOMEM in this case to allow for user-level 14549 * software to differentiate the cases. 14550 */ 14551 return (E2BIG); 14552 } 14553 14554 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 14555 14556 if (rval != ENOMEM) { 14557 opt[which] = size; 14558 return (rval); 14559 } 14560 14561 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14562 return (rval); 14563 14564 for (divisor = 2; divisor < factor; divisor <<= 1) 14565 continue; 14566 } 14567 14568 return (ENOMEM); 14569 } 14570 14571 static int 14572 dtrace_state_buffers(dtrace_state_t *state) 14573 { 14574 dtrace_speculation_t *spec = state->dts_speculations; 14575 int rval, i; 14576 14577 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 14578 DTRACEOPT_BUFSIZE)) != 0) 14579 return (rval); 14580 14581 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 14582 DTRACEOPT_AGGSIZE)) != 0) 14583 return (rval); 14584 14585 for (i = 0; i < state->dts_nspeculations; i++) { 14586 if ((rval = dtrace_state_buffer(state, 14587 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 14588 return (rval); 14589 } 14590 14591 return (0); 14592 } 14593 14594 static void 14595 dtrace_state_prereserve(dtrace_state_t *state) 14596 { 14597 dtrace_ecb_t *ecb; 14598 dtrace_probe_t *probe; 14599 14600 state->dts_reserve = 0; 14601 14602 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 14603 return; 14604 14605 /* 14606 * If our buffer policy is a "fill" buffer policy, we need to set the 14607 * prereserved space to be the space required by the END probes. 14608 */ 14609 probe = dtrace_probes[dtrace_probeid_end - 1]; 14610 ASSERT(probe != NULL); 14611 14612 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 14613 if (ecb->dte_state != state) 14614 continue; 14615 14616 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 14617 } 14618 } 14619 14620 static int 14621 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 14622 { 14623 dtrace_optval_t *opt = state->dts_options, sz, nspec; 14624 dtrace_speculation_t *spec; 14625 dtrace_buffer_t *buf; 14626 #ifdef illumos 14627 cyc_handler_t hdlr; 14628 cyc_time_t when; 14629 #endif 14630 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 14631 dtrace_icookie_t cookie; 14632 14633 mutex_enter(&cpu_lock); 14634 mutex_enter(&dtrace_lock); 14635 14636 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 14637 rval = EBUSY; 14638 goto out; 14639 } 14640 14641 /* 14642 * Before we can perform any checks, we must prime all of the 14643 * retained enablings that correspond to this state. 14644 */ 14645 dtrace_enabling_prime(state); 14646 14647 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 14648 rval = EACCES; 14649 goto out; 14650 } 14651 14652 dtrace_state_prereserve(state); 14653 14654 /* 14655 * Now we want to do is try to allocate our speculations. 14656 * We do not automatically resize the number of speculations; if 14657 * this fails, we will fail the operation. 14658 */ 14659 nspec = opt[DTRACEOPT_NSPEC]; 14660 ASSERT(nspec != DTRACEOPT_UNSET); 14661 14662 if (nspec > INT_MAX) { 14663 rval = ENOMEM; 14664 goto out; 14665 } 14666 14667 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 14668 KM_NOSLEEP | KM_NORMALPRI); 14669 14670 if (spec == NULL) { 14671 rval = ENOMEM; 14672 goto out; 14673 } 14674 14675 state->dts_speculations = spec; 14676 state->dts_nspeculations = (int)nspec; 14677 14678 for (i = 0; i < nspec; i++) { 14679 if ((buf = kmem_zalloc(bufsize, 14680 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 14681 rval = ENOMEM; 14682 goto err; 14683 } 14684 14685 spec[i].dtsp_buffer = buf; 14686 } 14687 14688 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 14689 if (dtrace_anon.dta_state == NULL) { 14690 rval = ENOENT; 14691 goto out; 14692 } 14693 14694 if (state->dts_necbs != 0) { 14695 rval = EALREADY; 14696 goto out; 14697 } 14698 14699 state->dts_anon = dtrace_anon_grab(); 14700 ASSERT(state->dts_anon != NULL); 14701 state = state->dts_anon; 14702 14703 /* 14704 * We want "grabanon" to be set in the grabbed state, so we'll 14705 * copy that option value from the grabbing state into the 14706 * grabbed state. 14707 */ 14708 state->dts_options[DTRACEOPT_GRABANON] = 14709 opt[DTRACEOPT_GRABANON]; 14710 14711 *cpu = dtrace_anon.dta_beganon; 14712 14713 /* 14714 * If the anonymous state is active (as it almost certainly 14715 * is if the anonymous enabling ultimately matched anything), 14716 * we don't allow any further option processing -- but we 14717 * don't return failure. 14718 */ 14719 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 14720 goto out; 14721 } 14722 14723 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 14724 opt[DTRACEOPT_AGGSIZE] != 0) { 14725 if (state->dts_aggregations == NULL) { 14726 /* 14727 * We're not going to create an aggregation buffer 14728 * because we don't have any ECBs that contain 14729 * aggregations -- set this option to 0. 14730 */ 14731 opt[DTRACEOPT_AGGSIZE] = 0; 14732 } else { 14733 /* 14734 * If we have an aggregation buffer, we must also have 14735 * a buffer to use as scratch. 14736 */ 14737 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 14738 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 14739 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 14740 } 14741 } 14742 } 14743 14744 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 14745 opt[DTRACEOPT_SPECSIZE] != 0) { 14746 if (!state->dts_speculates) { 14747 /* 14748 * We're not going to create speculation buffers 14749 * because we don't have any ECBs that actually 14750 * speculate -- set the speculation size to 0. 14751 */ 14752 opt[DTRACEOPT_SPECSIZE] = 0; 14753 } 14754 } 14755 14756 /* 14757 * The bare minimum size for any buffer that we're actually going to 14758 * do anything to is sizeof (uint64_t). 14759 */ 14760 sz = sizeof (uint64_t); 14761 14762 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 14763 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 14764 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 14765 /* 14766 * A buffer size has been explicitly set to 0 (or to a size 14767 * that will be adjusted to 0) and we need the space -- we 14768 * need to return failure. We return ENOSPC to differentiate 14769 * it from failing to allocate a buffer due to failure to meet 14770 * the reserve (for which we return E2BIG). 14771 */ 14772 rval = ENOSPC; 14773 goto out; 14774 } 14775 14776 if ((rval = dtrace_state_buffers(state)) != 0) 14777 goto err; 14778 14779 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 14780 sz = dtrace_dstate_defsize; 14781 14782 do { 14783 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 14784 14785 if (rval == 0) 14786 break; 14787 14788 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14789 goto err; 14790 } while (sz >>= 1); 14791 14792 opt[DTRACEOPT_DYNVARSIZE] = sz; 14793 14794 if (rval != 0) 14795 goto err; 14796 14797 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 14798 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 14799 14800 if (opt[DTRACEOPT_CLEANRATE] == 0) 14801 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 14802 14803 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 14804 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 14805 14806 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 14807 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 14808 14809 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 14810 #ifdef illumos 14811 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 14812 hdlr.cyh_arg = state; 14813 hdlr.cyh_level = CY_LOW_LEVEL; 14814 14815 when.cyt_when = 0; 14816 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 14817 14818 state->dts_cleaner = cyclic_add(&hdlr, &when); 14819 14820 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 14821 hdlr.cyh_arg = state; 14822 hdlr.cyh_level = CY_LOW_LEVEL; 14823 14824 when.cyt_when = 0; 14825 when.cyt_interval = dtrace_deadman_interval; 14826 14827 state->dts_deadman = cyclic_add(&hdlr, &when); 14828 #else 14829 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14830 dtrace_state_clean, state); 14831 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14832 dtrace_state_deadman, state); 14833 #endif 14834 14835 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 14836 14837 #ifdef illumos 14838 if (state->dts_getf != 0 && 14839 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 14840 /* 14841 * We don't have kernel privs but we have at least one call 14842 * to getf(); we need to bump our zone's count, and (if 14843 * this is the first enabling to have an unprivileged call 14844 * to getf()) we need to hook into closef(). 14845 */ 14846 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 14847 14848 if (dtrace_getf++ == 0) { 14849 ASSERT(dtrace_closef == NULL); 14850 dtrace_closef = dtrace_getf_barrier; 14851 } 14852 } 14853 #endif 14854 14855 /* 14856 * Now it's time to actually fire the BEGIN probe. We need to disable 14857 * interrupts here both to record the CPU on which we fired the BEGIN 14858 * probe (the data from this CPU will be processed first at user 14859 * level) and to manually activate the buffer for this CPU. 14860 */ 14861 cookie = dtrace_interrupt_disable(); 14862 *cpu = curcpu; 14863 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 14864 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 14865 14866 dtrace_probe(dtrace_probeid_begin, 14867 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 14868 dtrace_interrupt_enable(cookie); 14869 /* 14870 * We may have had an exit action from a BEGIN probe; only change our 14871 * state to ACTIVE if we're still in WARMUP. 14872 */ 14873 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 14874 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 14875 14876 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 14877 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 14878 14879 /* 14880 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 14881 * want each CPU to transition its principal buffer out of the 14882 * INACTIVE state. Doing this assures that no CPU will suddenly begin 14883 * processing an ECB halfway down a probe's ECB chain; all CPUs will 14884 * atomically transition from processing none of a state's ECBs to 14885 * processing all of them. 14886 */ 14887 dtrace_xcall(DTRACE_CPUALL, 14888 (dtrace_xcall_t)dtrace_buffer_activate, state); 14889 goto out; 14890 14891 err: 14892 dtrace_buffer_free(state->dts_buffer); 14893 dtrace_buffer_free(state->dts_aggbuffer); 14894 14895 if ((nspec = state->dts_nspeculations) == 0) { 14896 ASSERT(state->dts_speculations == NULL); 14897 goto out; 14898 } 14899 14900 spec = state->dts_speculations; 14901 ASSERT(spec != NULL); 14902 14903 for (i = 0; i < state->dts_nspeculations; i++) { 14904 if ((buf = spec[i].dtsp_buffer) == NULL) 14905 break; 14906 14907 dtrace_buffer_free(buf); 14908 kmem_free(buf, bufsize); 14909 } 14910 14911 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 14912 state->dts_nspeculations = 0; 14913 state->dts_speculations = NULL; 14914 14915 out: 14916 mutex_exit(&dtrace_lock); 14917 mutex_exit(&cpu_lock); 14918 14919 return (rval); 14920 } 14921 14922 static int 14923 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 14924 { 14925 dtrace_icookie_t cookie; 14926 14927 ASSERT(MUTEX_HELD(&dtrace_lock)); 14928 14929 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 14930 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 14931 return (EINVAL); 14932 14933 /* 14934 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 14935 * to be sure that every CPU has seen it. See below for the details 14936 * on why this is done. 14937 */ 14938 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 14939 dtrace_sync(); 14940 14941 /* 14942 * By this point, it is impossible for any CPU to be still processing 14943 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 14944 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 14945 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 14946 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 14947 * iff we're in the END probe. 14948 */ 14949 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 14950 dtrace_sync(); 14951 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 14952 14953 /* 14954 * Finally, we can release the reserve and call the END probe. We 14955 * disable interrupts across calling the END probe to allow us to 14956 * return the CPU on which we actually called the END probe. This 14957 * allows user-land to be sure that this CPU's principal buffer is 14958 * processed last. 14959 */ 14960 state->dts_reserve = 0; 14961 14962 cookie = dtrace_interrupt_disable(); 14963 *cpu = curcpu; 14964 dtrace_probe(dtrace_probeid_end, 14965 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 14966 dtrace_interrupt_enable(cookie); 14967 14968 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 14969 dtrace_sync(); 14970 14971 #ifdef illumos 14972 if (state->dts_getf != 0 && 14973 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 14974 /* 14975 * We don't have kernel privs but we have at least one call 14976 * to getf(); we need to lower our zone's count, and (if 14977 * this is the last enabling to have an unprivileged call 14978 * to getf()) we need to clear the closef() hook. 14979 */ 14980 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 14981 ASSERT(dtrace_closef == dtrace_getf_barrier); 14982 ASSERT(dtrace_getf > 0); 14983 14984 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 14985 14986 if (--dtrace_getf == 0) 14987 dtrace_closef = NULL; 14988 } 14989 #endif 14990 14991 return (0); 14992 } 14993 14994 static int 14995 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 14996 dtrace_optval_t val) 14997 { 14998 ASSERT(MUTEX_HELD(&dtrace_lock)); 14999 15000 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 15001 return (EBUSY); 15002 15003 if (option >= DTRACEOPT_MAX) 15004 return (EINVAL); 15005 15006 if (option != DTRACEOPT_CPU && val < 0) 15007 return (EINVAL); 15008 15009 switch (option) { 15010 case DTRACEOPT_DESTRUCTIVE: 15011 if (dtrace_destructive_disallow) 15012 return (EACCES); 15013 15014 state->dts_cred.dcr_destructive = 1; 15015 break; 15016 15017 case DTRACEOPT_BUFSIZE: 15018 case DTRACEOPT_DYNVARSIZE: 15019 case DTRACEOPT_AGGSIZE: 15020 case DTRACEOPT_SPECSIZE: 15021 case DTRACEOPT_STRSIZE: 15022 if (val < 0) 15023 return (EINVAL); 15024 15025 if (val >= LONG_MAX) { 15026 /* 15027 * If this is an otherwise negative value, set it to 15028 * the highest multiple of 128m less than LONG_MAX. 15029 * Technically, we're adjusting the size without 15030 * regard to the buffer resizing policy, but in fact, 15031 * this has no effect -- if we set the buffer size to 15032 * ~LONG_MAX and the buffer policy is ultimately set to 15033 * be "manual", the buffer allocation is guaranteed to 15034 * fail, if only because the allocation requires two 15035 * buffers. (We set the the size to the highest 15036 * multiple of 128m because it ensures that the size 15037 * will remain a multiple of a megabyte when 15038 * repeatedly halved -- all the way down to 15m.) 15039 */ 15040 val = LONG_MAX - (1 << 27) + 1; 15041 } 15042 } 15043 15044 state->dts_options[option] = val; 15045 15046 return (0); 15047 } 15048 15049 static void 15050 dtrace_state_destroy(dtrace_state_t *state) 15051 { 15052 dtrace_ecb_t *ecb; 15053 dtrace_vstate_t *vstate = &state->dts_vstate; 15054 #ifdef illumos 15055 minor_t minor = getminor(state->dts_dev); 15056 #endif 15057 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 15058 dtrace_speculation_t *spec = state->dts_speculations; 15059 int nspec = state->dts_nspeculations; 15060 uint32_t match; 15061 15062 ASSERT(MUTEX_HELD(&dtrace_lock)); 15063 ASSERT(MUTEX_HELD(&cpu_lock)); 15064 15065 /* 15066 * First, retract any retained enablings for this state. 15067 */ 15068 dtrace_enabling_retract(state); 15069 ASSERT(state->dts_nretained == 0); 15070 15071 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 15072 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 15073 /* 15074 * We have managed to come into dtrace_state_destroy() on a 15075 * hot enabling -- almost certainly because of a disorderly 15076 * shutdown of a consumer. (That is, a consumer that is 15077 * exiting without having called dtrace_stop().) In this case, 15078 * we're going to set our activity to be KILLED, and then 15079 * issue a sync to be sure that everyone is out of probe 15080 * context before we start blowing away ECBs. 15081 */ 15082 state->dts_activity = DTRACE_ACTIVITY_KILLED; 15083 dtrace_sync(); 15084 } 15085 15086 /* 15087 * Release the credential hold we took in dtrace_state_create(). 15088 */ 15089 if (state->dts_cred.dcr_cred != NULL) 15090 crfree(state->dts_cred.dcr_cred); 15091 15092 /* 15093 * Now we can safely disable and destroy any enabled probes. Because 15094 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 15095 * (especially if they're all enabled), we take two passes through the 15096 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 15097 * in the second we disable whatever is left over. 15098 */ 15099 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 15100 for (i = 0; i < state->dts_necbs; i++) { 15101 if ((ecb = state->dts_ecbs[i]) == NULL) 15102 continue; 15103 15104 if (match && ecb->dte_probe != NULL) { 15105 dtrace_probe_t *probe = ecb->dte_probe; 15106 dtrace_provider_t *prov = probe->dtpr_provider; 15107 15108 if (!(prov->dtpv_priv.dtpp_flags & match)) 15109 continue; 15110 } 15111 15112 dtrace_ecb_disable(ecb); 15113 dtrace_ecb_destroy(ecb); 15114 } 15115 15116 if (!match) 15117 break; 15118 } 15119 15120 /* 15121 * Before we free the buffers, perform one more sync to assure that 15122 * every CPU is out of probe context. 15123 */ 15124 dtrace_sync(); 15125 15126 dtrace_buffer_free(state->dts_buffer); 15127 dtrace_buffer_free(state->dts_aggbuffer); 15128 15129 for (i = 0; i < nspec; i++) 15130 dtrace_buffer_free(spec[i].dtsp_buffer); 15131 15132 #ifdef illumos 15133 if (state->dts_cleaner != CYCLIC_NONE) 15134 cyclic_remove(state->dts_cleaner); 15135 15136 if (state->dts_deadman != CYCLIC_NONE) 15137 cyclic_remove(state->dts_deadman); 15138 #else 15139 callout_stop(&state->dts_cleaner); 15140 callout_drain(&state->dts_cleaner); 15141 callout_stop(&state->dts_deadman); 15142 callout_drain(&state->dts_deadman); 15143 #endif 15144 15145 dtrace_dstate_fini(&vstate->dtvs_dynvars); 15146 dtrace_vstate_fini(vstate); 15147 if (state->dts_ecbs != NULL) 15148 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 15149 15150 if (state->dts_aggregations != NULL) { 15151 #ifdef DEBUG 15152 for (i = 0; i < state->dts_naggregations; i++) 15153 ASSERT(state->dts_aggregations[i] == NULL); 15154 #endif 15155 ASSERT(state->dts_naggregations > 0); 15156 kmem_free(state->dts_aggregations, 15157 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 15158 } 15159 15160 kmem_free(state->dts_buffer, bufsize); 15161 kmem_free(state->dts_aggbuffer, bufsize); 15162 15163 for (i = 0; i < nspec; i++) 15164 kmem_free(spec[i].dtsp_buffer, bufsize); 15165 15166 if (spec != NULL) 15167 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15168 15169 dtrace_format_destroy(state); 15170 15171 if (state->dts_aggid_arena != NULL) { 15172 #ifdef illumos 15173 vmem_destroy(state->dts_aggid_arena); 15174 #else 15175 delete_unrhdr(state->dts_aggid_arena); 15176 #endif 15177 state->dts_aggid_arena = NULL; 15178 } 15179 #ifdef illumos 15180 ddi_soft_state_free(dtrace_softstate, minor); 15181 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 15182 #endif 15183 } 15184 15185 /* 15186 * DTrace Anonymous Enabling Functions 15187 */ 15188 static dtrace_state_t * 15189 dtrace_anon_grab(void) 15190 { 15191 dtrace_state_t *state; 15192 15193 ASSERT(MUTEX_HELD(&dtrace_lock)); 15194 15195 if ((state = dtrace_anon.dta_state) == NULL) { 15196 ASSERT(dtrace_anon.dta_enabling == NULL); 15197 return (NULL); 15198 } 15199 15200 ASSERT(dtrace_anon.dta_enabling != NULL); 15201 ASSERT(dtrace_retained != NULL); 15202 15203 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 15204 dtrace_anon.dta_enabling = NULL; 15205 dtrace_anon.dta_state = NULL; 15206 15207 return (state); 15208 } 15209 15210 static void 15211 dtrace_anon_property(void) 15212 { 15213 int i, rv; 15214 dtrace_state_t *state; 15215 dof_hdr_t *dof; 15216 char c[32]; /* enough for "dof-data-" + digits */ 15217 15218 ASSERT(MUTEX_HELD(&dtrace_lock)); 15219 ASSERT(MUTEX_HELD(&cpu_lock)); 15220 15221 for (i = 0; ; i++) { 15222 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 15223 15224 dtrace_err_verbose = 1; 15225 15226 if ((dof = dtrace_dof_property(c)) == NULL) { 15227 dtrace_err_verbose = 0; 15228 break; 15229 } 15230 15231 #ifdef illumos 15232 /* 15233 * We want to create anonymous state, so we need to transition 15234 * the kernel debugger to indicate that DTrace is active. If 15235 * this fails (e.g. because the debugger has modified text in 15236 * some way), we won't continue with the processing. 15237 */ 15238 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15239 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 15240 "enabling ignored."); 15241 dtrace_dof_destroy(dof); 15242 break; 15243 } 15244 #endif 15245 15246 /* 15247 * If we haven't allocated an anonymous state, we'll do so now. 15248 */ 15249 if ((state = dtrace_anon.dta_state) == NULL) { 15250 #ifdef illumos 15251 state = dtrace_state_create(NULL, NULL); 15252 #else 15253 state = dtrace_state_create(NULL); 15254 #endif 15255 dtrace_anon.dta_state = state; 15256 15257 if (state == NULL) { 15258 /* 15259 * This basically shouldn't happen: the only 15260 * failure mode from dtrace_state_create() is a 15261 * failure of ddi_soft_state_zalloc() that 15262 * itself should never happen. Still, the 15263 * interface allows for a failure mode, and 15264 * we want to fail as gracefully as possible: 15265 * we'll emit an error message and cease 15266 * processing anonymous state in this case. 15267 */ 15268 cmn_err(CE_WARN, "failed to create " 15269 "anonymous state"); 15270 dtrace_dof_destroy(dof); 15271 break; 15272 } 15273 } 15274 15275 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 15276 &dtrace_anon.dta_enabling, 0, B_TRUE); 15277 15278 if (rv == 0) 15279 rv = dtrace_dof_options(dof, state); 15280 15281 dtrace_err_verbose = 0; 15282 dtrace_dof_destroy(dof); 15283 15284 if (rv != 0) { 15285 /* 15286 * This is malformed DOF; chuck any anonymous state 15287 * that we created. 15288 */ 15289 ASSERT(dtrace_anon.dta_enabling == NULL); 15290 dtrace_state_destroy(state); 15291 dtrace_anon.dta_state = NULL; 15292 break; 15293 } 15294 15295 ASSERT(dtrace_anon.dta_enabling != NULL); 15296 } 15297 15298 if (dtrace_anon.dta_enabling != NULL) { 15299 int rval; 15300 15301 /* 15302 * dtrace_enabling_retain() can only fail because we are 15303 * trying to retain more enablings than are allowed -- but 15304 * we only have one anonymous enabling, and we are guaranteed 15305 * to be allowed at least one retained enabling; we assert 15306 * that dtrace_enabling_retain() returns success. 15307 */ 15308 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 15309 ASSERT(rval == 0); 15310 15311 dtrace_enabling_dump(dtrace_anon.dta_enabling); 15312 } 15313 } 15314 15315 /* 15316 * DTrace Helper Functions 15317 */ 15318 static void 15319 dtrace_helper_trace(dtrace_helper_action_t *helper, 15320 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 15321 { 15322 uint32_t size, next, nnext, i; 15323 dtrace_helptrace_t *ent, *buffer; 15324 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 15325 15326 if ((buffer = dtrace_helptrace_buffer) == NULL) 15327 return; 15328 15329 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 15330 15331 /* 15332 * What would a tracing framework be without its own tracing 15333 * framework? (Well, a hell of a lot simpler, for starters...) 15334 */ 15335 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 15336 sizeof (uint64_t) - sizeof (uint64_t); 15337 15338 /* 15339 * Iterate until we can allocate a slot in the trace buffer. 15340 */ 15341 do { 15342 next = dtrace_helptrace_next; 15343 15344 if (next + size < dtrace_helptrace_bufsize) { 15345 nnext = next + size; 15346 } else { 15347 nnext = size; 15348 } 15349 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 15350 15351 /* 15352 * We have our slot; fill it in. 15353 */ 15354 if (nnext == size) { 15355 dtrace_helptrace_wrapped++; 15356 next = 0; 15357 } 15358 15359 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next); 15360 ent->dtht_helper = helper; 15361 ent->dtht_where = where; 15362 ent->dtht_nlocals = vstate->dtvs_nlocals; 15363 15364 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 15365 mstate->dtms_fltoffs : -1; 15366 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 15367 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 15368 15369 for (i = 0; i < vstate->dtvs_nlocals; i++) { 15370 dtrace_statvar_t *svar; 15371 15372 if ((svar = vstate->dtvs_locals[i]) == NULL) 15373 continue; 15374 15375 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 15376 ent->dtht_locals[i] = 15377 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 15378 } 15379 } 15380 15381 static uint64_t 15382 dtrace_helper(int which, dtrace_mstate_t *mstate, 15383 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 15384 { 15385 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 15386 uint64_t sarg0 = mstate->dtms_arg[0]; 15387 uint64_t sarg1 = mstate->dtms_arg[1]; 15388 uint64_t rval = 0; 15389 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 15390 dtrace_helper_action_t *helper; 15391 dtrace_vstate_t *vstate; 15392 dtrace_difo_t *pred; 15393 int i, trace = dtrace_helptrace_buffer != NULL; 15394 15395 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 15396 15397 if (helpers == NULL) 15398 return (0); 15399 15400 if ((helper = helpers->dthps_actions[which]) == NULL) 15401 return (0); 15402 15403 vstate = &helpers->dthps_vstate; 15404 mstate->dtms_arg[0] = arg0; 15405 mstate->dtms_arg[1] = arg1; 15406 15407 /* 15408 * Now iterate over each helper. If its predicate evaluates to 'true', 15409 * we'll call the corresponding actions. Note that the below calls 15410 * to dtrace_dif_emulate() may set faults in machine state. This is 15411 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 15412 * the stored DIF offset with its own (which is the desired behavior). 15413 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 15414 * from machine state; this is okay, too. 15415 */ 15416 for (; helper != NULL; helper = helper->dtha_next) { 15417 if ((pred = helper->dtha_predicate) != NULL) { 15418 if (trace) 15419 dtrace_helper_trace(helper, mstate, vstate, 0); 15420 15421 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 15422 goto next; 15423 15424 if (*flags & CPU_DTRACE_FAULT) 15425 goto err; 15426 } 15427 15428 for (i = 0; i < helper->dtha_nactions; i++) { 15429 if (trace) 15430 dtrace_helper_trace(helper, 15431 mstate, vstate, i + 1); 15432 15433 rval = dtrace_dif_emulate(helper->dtha_actions[i], 15434 mstate, vstate, state); 15435 15436 if (*flags & CPU_DTRACE_FAULT) 15437 goto err; 15438 } 15439 15440 next: 15441 if (trace) 15442 dtrace_helper_trace(helper, mstate, vstate, 15443 DTRACE_HELPTRACE_NEXT); 15444 } 15445 15446 if (trace) 15447 dtrace_helper_trace(helper, mstate, vstate, 15448 DTRACE_HELPTRACE_DONE); 15449 15450 /* 15451 * Restore the arg0 that we saved upon entry. 15452 */ 15453 mstate->dtms_arg[0] = sarg0; 15454 mstate->dtms_arg[1] = sarg1; 15455 15456 return (rval); 15457 15458 err: 15459 if (trace) 15460 dtrace_helper_trace(helper, mstate, vstate, 15461 DTRACE_HELPTRACE_ERR); 15462 15463 /* 15464 * Restore the arg0 that we saved upon entry. 15465 */ 15466 mstate->dtms_arg[0] = sarg0; 15467 mstate->dtms_arg[1] = sarg1; 15468 15469 return (0); 15470 } 15471 15472 static void 15473 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 15474 dtrace_vstate_t *vstate) 15475 { 15476 int i; 15477 15478 if (helper->dtha_predicate != NULL) 15479 dtrace_difo_release(helper->dtha_predicate, vstate); 15480 15481 for (i = 0; i < helper->dtha_nactions; i++) { 15482 ASSERT(helper->dtha_actions[i] != NULL); 15483 dtrace_difo_release(helper->dtha_actions[i], vstate); 15484 } 15485 15486 kmem_free(helper->dtha_actions, 15487 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 15488 kmem_free(helper, sizeof (dtrace_helper_action_t)); 15489 } 15490 15491 static int 15492 dtrace_helper_destroygen(dtrace_helpers_t *help, int gen) 15493 { 15494 proc_t *p = curproc; 15495 dtrace_vstate_t *vstate; 15496 int i; 15497 15498 if (help == NULL) 15499 help = p->p_dtrace_helpers; 15500 15501 ASSERT(MUTEX_HELD(&dtrace_lock)); 15502 15503 if (help == NULL || gen > help->dthps_generation) 15504 return (EINVAL); 15505 15506 vstate = &help->dthps_vstate; 15507 15508 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15509 dtrace_helper_action_t *last = NULL, *h, *next; 15510 15511 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15512 next = h->dtha_next; 15513 15514 if (h->dtha_generation == gen) { 15515 if (last != NULL) { 15516 last->dtha_next = next; 15517 } else { 15518 help->dthps_actions[i] = next; 15519 } 15520 15521 dtrace_helper_action_destroy(h, vstate); 15522 } else { 15523 last = h; 15524 } 15525 } 15526 } 15527 15528 /* 15529 * Interate until we've cleared out all helper providers with the 15530 * given generation number. 15531 */ 15532 for (;;) { 15533 dtrace_helper_provider_t *prov; 15534 15535 /* 15536 * Look for a helper provider with the right generation. We 15537 * have to start back at the beginning of the list each time 15538 * because we drop dtrace_lock. It's unlikely that we'll make 15539 * more than two passes. 15540 */ 15541 for (i = 0; i < help->dthps_nprovs; i++) { 15542 prov = help->dthps_provs[i]; 15543 15544 if (prov->dthp_generation == gen) 15545 break; 15546 } 15547 15548 /* 15549 * If there were no matches, we're done. 15550 */ 15551 if (i == help->dthps_nprovs) 15552 break; 15553 15554 /* 15555 * Move the last helper provider into this slot. 15556 */ 15557 help->dthps_nprovs--; 15558 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 15559 help->dthps_provs[help->dthps_nprovs] = NULL; 15560 15561 mutex_exit(&dtrace_lock); 15562 15563 /* 15564 * If we have a meta provider, remove this helper provider. 15565 */ 15566 mutex_enter(&dtrace_meta_lock); 15567 if (dtrace_meta_pid != NULL) { 15568 ASSERT(dtrace_deferred_pid == NULL); 15569 dtrace_helper_provider_remove(&prov->dthp_prov, 15570 p->p_pid); 15571 } 15572 mutex_exit(&dtrace_meta_lock); 15573 15574 dtrace_helper_provider_destroy(prov); 15575 15576 mutex_enter(&dtrace_lock); 15577 } 15578 15579 return (0); 15580 } 15581 15582 static int 15583 dtrace_helper_validate(dtrace_helper_action_t *helper) 15584 { 15585 int err = 0, i; 15586 dtrace_difo_t *dp; 15587 15588 if ((dp = helper->dtha_predicate) != NULL) 15589 err += dtrace_difo_validate_helper(dp); 15590 15591 for (i = 0; i < helper->dtha_nactions; i++) 15592 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 15593 15594 return (err == 0); 15595 } 15596 15597 static int 15598 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep, 15599 dtrace_helpers_t *help) 15600 { 15601 dtrace_helper_action_t *helper, *last; 15602 dtrace_actdesc_t *act; 15603 dtrace_vstate_t *vstate; 15604 dtrace_predicate_t *pred; 15605 int count = 0, nactions = 0, i; 15606 15607 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 15608 return (EINVAL); 15609 15610 last = help->dthps_actions[which]; 15611 vstate = &help->dthps_vstate; 15612 15613 for (count = 0; last != NULL; last = last->dtha_next) { 15614 count++; 15615 if (last->dtha_next == NULL) 15616 break; 15617 } 15618 15619 /* 15620 * If we already have dtrace_helper_actions_max helper actions for this 15621 * helper action type, we'll refuse to add a new one. 15622 */ 15623 if (count >= dtrace_helper_actions_max) 15624 return (ENOSPC); 15625 15626 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 15627 helper->dtha_generation = help->dthps_generation; 15628 15629 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 15630 ASSERT(pred->dtp_difo != NULL); 15631 dtrace_difo_hold(pred->dtp_difo); 15632 helper->dtha_predicate = pred->dtp_difo; 15633 } 15634 15635 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 15636 if (act->dtad_kind != DTRACEACT_DIFEXPR) 15637 goto err; 15638 15639 if (act->dtad_difo == NULL) 15640 goto err; 15641 15642 nactions++; 15643 } 15644 15645 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 15646 (helper->dtha_nactions = nactions), KM_SLEEP); 15647 15648 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 15649 dtrace_difo_hold(act->dtad_difo); 15650 helper->dtha_actions[i++] = act->dtad_difo; 15651 } 15652 15653 if (!dtrace_helper_validate(helper)) 15654 goto err; 15655 15656 if (last == NULL) { 15657 help->dthps_actions[which] = helper; 15658 } else { 15659 last->dtha_next = helper; 15660 } 15661 15662 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 15663 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 15664 dtrace_helptrace_next = 0; 15665 } 15666 15667 return (0); 15668 err: 15669 dtrace_helper_action_destroy(helper, vstate); 15670 return (EINVAL); 15671 } 15672 15673 static void 15674 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 15675 dof_helper_t *dofhp) 15676 { 15677 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 15678 15679 mutex_enter(&dtrace_meta_lock); 15680 mutex_enter(&dtrace_lock); 15681 15682 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 15683 /* 15684 * If the dtrace module is loaded but not attached, or if 15685 * there aren't isn't a meta provider registered to deal with 15686 * these provider descriptions, we need to postpone creating 15687 * the actual providers until later. 15688 */ 15689 15690 if (help->dthps_next == NULL && help->dthps_prev == NULL && 15691 dtrace_deferred_pid != help) { 15692 help->dthps_deferred = 1; 15693 help->dthps_pid = p->p_pid; 15694 help->dthps_next = dtrace_deferred_pid; 15695 help->dthps_prev = NULL; 15696 if (dtrace_deferred_pid != NULL) 15697 dtrace_deferred_pid->dthps_prev = help; 15698 dtrace_deferred_pid = help; 15699 } 15700 15701 mutex_exit(&dtrace_lock); 15702 15703 } else if (dofhp != NULL) { 15704 /* 15705 * If the dtrace module is loaded and we have a particular 15706 * helper provider description, pass that off to the 15707 * meta provider. 15708 */ 15709 15710 mutex_exit(&dtrace_lock); 15711 15712 dtrace_helper_provide(dofhp, p->p_pid); 15713 15714 } else { 15715 /* 15716 * Otherwise, just pass all the helper provider descriptions 15717 * off to the meta provider. 15718 */ 15719 15720 int i; 15721 mutex_exit(&dtrace_lock); 15722 15723 for (i = 0; i < help->dthps_nprovs; i++) { 15724 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 15725 p->p_pid); 15726 } 15727 } 15728 15729 mutex_exit(&dtrace_meta_lock); 15730 } 15731 15732 static int 15733 dtrace_helper_provider_add(dof_helper_t *dofhp, dtrace_helpers_t *help, int gen) 15734 { 15735 dtrace_helper_provider_t *hprov, **tmp_provs; 15736 uint_t tmp_maxprovs, i; 15737 15738 ASSERT(MUTEX_HELD(&dtrace_lock)); 15739 ASSERT(help != NULL); 15740 15741 /* 15742 * If we already have dtrace_helper_providers_max helper providers, 15743 * we're refuse to add a new one. 15744 */ 15745 if (help->dthps_nprovs >= dtrace_helper_providers_max) 15746 return (ENOSPC); 15747 15748 /* 15749 * Check to make sure this isn't a duplicate. 15750 */ 15751 for (i = 0; i < help->dthps_nprovs; i++) { 15752 if (dofhp->dofhp_addr == 15753 help->dthps_provs[i]->dthp_prov.dofhp_addr) 15754 return (EALREADY); 15755 } 15756 15757 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 15758 hprov->dthp_prov = *dofhp; 15759 hprov->dthp_ref = 1; 15760 hprov->dthp_generation = gen; 15761 15762 /* 15763 * Allocate a bigger table for helper providers if it's already full. 15764 */ 15765 if (help->dthps_maxprovs == help->dthps_nprovs) { 15766 tmp_maxprovs = help->dthps_maxprovs; 15767 tmp_provs = help->dthps_provs; 15768 15769 if (help->dthps_maxprovs == 0) 15770 help->dthps_maxprovs = 2; 15771 else 15772 help->dthps_maxprovs *= 2; 15773 if (help->dthps_maxprovs > dtrace_helper_providers_max) 15774 help->dthps_maxprovs = dtrace_helper_providers_max; 15775 15776 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 15777 15778 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 15779 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 15780 15781 if (tmp_provs != NULL) { 15782 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 15783 sizeof (dtrace_helper_provider_t *)); 15784 kmem_free(tmp_provs, tmp_maxprovs * 15785 sizeof (dtrace_helper_provider_t *)); 15786 } 15787 } 15788 15789 help->dthps_provs[help->dthps_nprovs] = hprov; 15790 help->dthps_nprovs++; 15791 15792 return (0); 15793 } 15794 15795 static void 15796 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 15797 { 15798 mutex_enter(&dtrace_lock); 15799 15800 if (--hprov->dthp_ref == 0) { 15801 dof_hdr_t *dof; 15802 mutex_exit(&dtrace_lock); 15803 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 15804 dtrace_dof_destroy(dof); 15805 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 15806 } else { 15807 mutex_exit(&dtrace_lock); 15808 } 15809 } 15810 15811 static int 15812 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 15813 { 15814 uintptr_t daddr = (uintptr_t)dof; 15815 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 15816 dof_provider_t *provider; 15817 dof_probe_t *probe; 15818 uint8_t *arg; 15819 char *strtab, *typestr; 15820 dof_stridx_t typeidx; 15821 size_t typesz; 15822 uint_t nprobes, j, k; 15823 15824 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 15825 15826 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 15827 dtrace_dof_error(dof, "misaligned section offset"); 15828 return (-1); 15829 } 15830 15831 /* 15832 * The section needs to be large enough to contain the DOF provider 15833 * structure appropriate for the given version. 15834 */ 15835 if (sec->dofs_size < 15836 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 15837 offsetof(dof_provider_t, dofpv_prenoffs) : 15838 sizeof (dof_provider_t))) { 15839 dtrace_dof_error(dof, "provider section too small"); 15840 return (-1); 15841 } 15842 15843 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 15844 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 15845 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 15846 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 15847 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 15848 15849 if (str_sec == NULL || prb_sec == NULL || 15850 arg_sec == NULL || off_sec == NULL) 15851 return (-1); 15852 15853 enoff_sec = NULL; 15854 15855 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 15856 provider->dofpv_prenoffs != DOF_SECT_NONE && 15857 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 15858 provider->dofpv_prenoffs)) == NULL) 15859 return (-1); 15860 15861 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 15862 15863 if (provider->dofpv_name >= str_sec->dofs_size || 15864 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 15865 dtrace_dof_error(dof, "invalid provider name"); 15866 return (-1); 15867 } 15868 15869 if (prb_sec->dofs_entsize == 0 || 15870 prb_sec->dofs_entsize > prb_sec->dofs_size) { 15871 dtrace_dof_error(dof, "invalid entry size"); 15872 return (-1); 15873 } 15874 15875 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 15876 dtrace_dof_error(dof, "misaligned entry size"); 15877 return (-1); 15878 } 15879 15880 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 15881 dtrace_dof_error(dof, "invalid entry size"); 15882 return (-1); 15883 } 15884 15885 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 15886 dtrace_dof_error(dof, "misaligned section offset"); 15887 return (-1); 15888 } 15889 15890 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 15891 dtrace_dof_error(dof, "invalid entry size"); 15892 return (-1); 15893 } 15894 15895 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 15896 15897 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 15898 15899 /* 15900 * Take a pass through the probes to check for errors. 15901 */ 15902 for (j = 0; j < nprobes; j++) { 15903 probe = (dof_probe_t *)(uintptr_t)(daddr + 15904 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 15905 15906 if (probe->dofpr_func >= str_sec->dofs_size) { 15907 dtrace_dof_error(dof, "invalid function name"); 15908 return (-1); 15909 } 15910 15911 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 15912 dtrace_dof_error(dof, "function name too long"); 15913 return (-1); 15914 } 15915 15916 if (probe->dofpr_name >= str_sec->dofs_size || 15917 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 15918 dtrace_dof_error(dof, "invalid probe name"); 15919 return (-1); 15920 } 15921 15922 /* 15923 * The offset count must not wrap the index, and the offsets 15924 * must also not overflow the section's data. 15925 */ 15926 if (probe->dofpr_offidx + probe->dofpr_noffs < 15927 probe->dofpr_offidx || 15928 (probe->dofpr_offidx + probe->dofpr_noffs) * 15929 off_sec->dofs_entsize > off_sec->dofs_size) { 15930 dtrace_dof_error(dof, "invalid probe offset"); 15931 return (-1); 15932 } 15933 15934 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 15935 /* 15936 * If there's no is-enabled offset section, make sure 15937 * there aren't any is-enabled offsets. Otherwise 15938 * perform the same checks as for probe offsets 15939 * (immediately above). 15940 */ 15941 if (enoff_sec == NULL) { 15942 if (probe->dofpr_enoffidx != 0 || 15943 probe->dofpr_nenoffs != 0) { 15944 dtrace_dof_error(dof, "is-enabled " 15945 "offsets with null section"); 15946 return (-1); 15947 } 15948 } else if (probe->dofpr_enoffidx + 15949 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 15950 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 15951 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 15952 dtrace_dof_error(dof, "invalid is-enabled " 15953 "offset"); 15954 return (-1); 15955 } 15956 15957 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 15958 dtrace_dof_error(dof, "zero probe and " 15959 "is-enabled offsets"); 15960 return (-1); 15961 } 15962 } else if (probe->dofpr_noffs == 0) { 15963 dtrace_dof_error(dof, "zero probe offsets"); 15964 return (-1); 15965 } 15966 15967 if (probe->dofpr_argidx + probe->dofpr_xargc < 15968 probe->dofpr_argidx || 15969 (probe->dofpr_argidx + probe->dofpr_xargc) * 15970 arg_sec->dofs_entsize > arg_sec->dofs_size) { 15971 dtrace_dof_error(dof, "invalid args"); 15972 return (-1); 15973 } 15974 15975 typeidx = probe->dofpr_nargv; 15976 typestr = strtab + probe->dofpr_nargv; 15977 for (k = 0; k < probe->dofpr_nargc; k++) { 15978 if (typeidx >= str_sec->dofs_size) { 15979 dtrace_dof_error(dof, "bad " 15980 "native argument type"); 15981 return (-1); 15982 } 15983 15984 typesz = strlen(typestr) + 1; 15985 if (typesz > DTRACE_ARGTYPELEN) { 15986 dtrace_dof_error(dof, "native " 15987 "argument type too long"); 15988 return (-1); 15989 } 15990 typeidx += typesz; 15991 typestr += typesz; 15992 } 15993 15994 typeidx = probe->dofpr_xargv; 15995 typestr = strtab + probe->dofpr_xargv; 15996 for (k = 0; k < probe->dofpr_xargc; k++) { 15997 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 15998 dtrace_dof_error(dof, "bad " 15999 "native argument index"); 16000 return (-1); 16001 } 16002 16003 if (typeidx >= str_sec->dofs_size) { 16004 dtrace_dof_error(dof, "bad " 16005 "translated argument type"); 16006 return (-1); 16007 } 16008 16009 typesz = strlen(typestr) + 1; 16010 if (typesz > DTRACE_ARGTYPELEN) { 16011 dtrace_dof_error(dof, "translated argument " 16012 "type too long"); 16013 return (-1); 16014 } 16015 16016 typeidx += typesz; 16017 typestr += typesz; 16018 } 16019 } 16020 16021 return (0); 16022 } 16023 16024 static int 16025 #ifdef __FreeBSD__ 16026 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp, struct proc *p) 16027 #else 16028 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 16029 #endif 16030 { 16031 dtrace_helpers_t *help; 16032 dtrace_vstate_t *vstate; 16033 dtrace_enabling_t *enab = NULL; 16034 #ifndef __FreeBSD__ 16035 proc_t *p = curproc; 16036 #endif 16037 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 16038 uintptr_t daddr = (uintptr_t)dof; 16039 16040 ASSERT(MUTEX_HELD(&dtrace_lock)); 16041 16042 if ((help = p->p_dtrace_helpers) == NULL) 16043 help = dtrace_helpers_create(p); 16044 16045 vstate = &help->dthps_vstate; 16046 16047 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 16048 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 16049 dtrace_dof_destroy(dof); 16050 return (rv); 16051 } 16052 16053 /* 16054 * Look for helper providers and validate their descriptions. 16055 */ 16056 if (dhp != NULL) { 16057 for (i = 0; i < dof->dofh_secnum; i++) { 16058 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 16059 dof->dofh_secoff + i * dof->dofh_secsize); 16060 16061 if (sec->dofs_type != DOF_SECT_PROVIDER) 16062 continue; 16063 16064 if (dtrace_helper_provider_validate(dof, sec) != 0) { 16065 dtrace_enabling_destroy(enab); 16066 dtrace_dof_destroy(dof); 16067 return (-1); 16068 } 16069 16070 nprovs++; 16071 } 16072 } 16073 16074 /* 16075 * Now we need to walk through the ECB descriptions in the enabling. 16076 */ 16077 for (i = 0; i < enab->dten_ndesc; i++) { 16078 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 16079 dtrace_probedesc_t *desc = &ep->dted_probe; 16080 16081 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 16082 continue; 16083 16084 if (strcmp(desc->dtpd_mod, "helper") != 0) 16085 continue; 16086 16087 if (strcmp(desc->dtpd_func, "ustack") != 0) 16088 continue; 16089 16090 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 16091 ep, help)) != 0) { 16092 /* 16093 * Adding this helper action failed -- we are now going 16094 * to rip out the entire generation and return failure. 16095 */ 16096 (void) dtrace_helper_destroygen(help, 16097 help->dthps_generation); 16098 dtrace_enabling_destroy(enab); 16099 dtrace_dof_destroy(dof); 16100 return (-1); 16101 } 16102 16103 nhelpers++; 16104 } 16105 16106 if (nhelpers < enab->dten_ndesc) 16107 dtrace_dof_error(dof, "unmatched helpers"); 16108 16109 gen = help->dthps_generation++; 16110 dtrace_enabling_destroy(enab); 16111 16112 if (dhp != NULL && nprovs > 0) { 16113 /* 16114 * Now that this is in-kernel, we change the sense of the 16115 * members: dofhp_dof denotes the in-kernel copy of the DOF 16116 * and dofhp_addr denotes the address at user-level. 16117 */ 16118 dhp->dofhp_addr = dhp->dofhp_dof; 16119 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 16120 16121 if (dtrace_helper_provider_add(dhp, help, gen) == 0) { 16122 mutex_exit(&dtrace_lock); 16123 dtrace_helper_provider_register(p, help, dhp); 16124 mutex_enter(&dtrace_lock); 16125 16126 destroy = 0; 16127 } 16128 } 16129 16130 if (destroy) 16131 dtrace_dof_destroy(dof); 16132 16133 return (gen); 16134 } 16135 16136 static dtrace_helpers_t * 16137 dtrace_helpers_create(proc_t *p) 16138 { 16139 dtrace_helpers_t *help; 16140 16141 ASSERT(MUTEX_HELD(&dtrace_lock)); 16142 ASSERT(p->p_dtrace_helpers == NULL); 16143 16144 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 16145 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 16146 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 16147 16148 p->p_dtrace_helpers = help; 16149 dtrace_helpers++; 16150 16151 return (help); 16152 } 16153 16154 #ifdef illumos 16155 static 16156 #endif 16157 void 16158 dtrace_helpers_destroy(proc_t *p) 16159 { 16160 dtrace_helpers_t *help; 16161 dtrace_vstate_t *vstate; 16162 #ifdef illumos 16163 proc_t *p = curproc; 16164 #endif 16165 int i; 16166 16167 mutex_enter(&dtrace_lock); 16168 16169 ASSERT(p->p_dtrace_helpers != NULL); 16170 ASSERT(dtrace_helpers > 0); 16171 16172 help = p->p_dtrace_helpers; 16173 vstate = &help->dthps_vstate; 16174 16175 /* 16176 * We're now going to lose the help from this process. 16177 */ 16178 p->p_dtrace_helpers = NULL; 16179 dtrace_sync(); 16180 16181 /* 16182 * Destory the helper actions. 16183 */ 16184 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16185 dtrace_helper_action_t *h, *next; 16186 16187 for (h = help->dthps_actions[i]; h != NULL; h = next) { 16188 next = h->dtha_next; 16189 dtrace_helper_action_destroy(h, vstate); 16190 h = next; 16191 } 16192 } 16193 16194 mutex_exit(&dtrace_lock); 16195 16196 /* 16197 * Destroy the helper providers. 16198 */ 16199 if (help->dthps_maxprovs > 0) { 16200 mutex_enter(&dtrace_meta_lock); 16201 if (dtrace_meta_pid != NULL) { 16202 ASSERT(dtrace_deferred_pid == NULL); 16203 16204 for (i = 0; i < help->dthps_nprovs; i++) { 16205 dtrace_helper_provider_remove( 16206 &help->dthps_provs[i]->dthp_prov, p->p_pid); 16207 } 16208 } else { 16209 mutex_enter(&dtrace_lock); 16210 ASSERT(help->dthps_deferred == 0 || 16211 help->dthps_next != NULL || 16212 help->dthps_prev != NULL || 16213 help == dtrace_deferred_pid); 16214 16215 /* 16216 * Remove the helper from the deferred list. 16217 */ 16218 if (help->dthps_next != NULL) 16219 help->dthps_next->dthps_prev = help->dthps_prev; 16220 if (help->dthps_prev != NULL) 16221 help->dthps_prev->dthps_next = help->dthps_next; 16222 if (dtrace_deferred_pid == help) { 16223 dtrace_deferred_pid = help->dthps_next; 16224 ASSERT(help->dthps_prev == NULL); 16225 } 16226 16227 mutex_exit(&dtrace_lock); 16228 } 16229 16230 mutex_exit(&dtrace_meta_lock); 16231 16232 for (i = 0; i < help->dthps_nprovs; i++) { 16233 dtrace_helper_provider_destroy(help->dthps_provs[i]); 16234 } 16235 16236 kmem_free(help->dthps_provs, help->dthps_maxprovs * 16237 sizeof (dtrace_helper_provider_t *)); 16238 } 16239 16240 mutex_enter(&dtrace_lock); 16241 16242 dtrace_vstate_fini(&help->dthps_vstate); 16243 kmem_free(help->dthps_actions, 16244 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 16245 kmem_free(help, sizeof (dtrace_helpers_t)); 16246 16247 --dtrace_helpers; 16248 mutex_exit(&dtrace_lock); 16249 } 16250 16251 #ifdef illumos 16252 static 16253 #endif 16254 void 16255 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 16256 { 16257 dtrace_helpers_t *help, *newhelp; 16258 dtrace_helper_action_t *helper, *new, *last; 16259 dtrace_difo_t *dp; 16260 dtrace_vstate_t *vstate; 16261 int i, j, sz, hasprovs = 0; 16262 16263 mutex_enter(&dtrace_lock); 16264 ASSERT(from->p_dtrace_helpers != NULL); 16265 ASSERT(dtrace_helpers > 0); 16266 16267 help = from->p_dtrace_helpers; 16268 newhelp = dtrace_helpers_create(to); 16269 ASSERT(to->p_dtrace_helpers != NULL); 16270 16271 newhelp->dthps_generation = help->dthps_generation; 16272 vstate = &newhelp->dthps_vstate; 16273 16274 /* 16275 * Duplicate the helper actions. 16276 */ 16277 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16278 if ((helper = help->dthps_actions[i]) == NULL) 16279 continue; 16280 16281 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 16282 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 16283 KM_SLEEP); 16284 new->dtha_generation = helper->dtha_generation; 16285 16286 if ((dp = helper->dtha_predicate) != NULL) { 16287 dp = dtrace_difo_duplicate(dp, vstate); 16288 new->dtha_predicate = dp; 16289 } 16290 16291 new->dtha_nactions = helper->dtha_nactions; 16292 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 16293 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 16294 16295 for (j = 0; j < new->dtha_nactions; j++) { 16296 dtrace_difo_t *dp = helper->dtha_actions[j]; 16297 16298 ASSERT(dp != NULL); 16299 dp = dtrace_difo_duplicate(dp, vstate); 16300 new->dtha_actions[j] = dp; 16301 } 16302 16303 if (last != NULL) { 16304 last->dtha_next = new; 16305 } else { 16306 newhelp->dthps_actions[i] = new; 16307 } 16308 16309 last = new; 16310 } 16311 } 16312 16313 /* 16314 * Duplicate the helper providers and register them with the 16315 * DTrace framework. 16316 */ 16317 if (help->dthps_nprovs > 0) { 16318 newhelp->dthps_nprovs = help->dthps_nprovs; 16319 newhelp->dthps_maxprovs = help->dthps_nprovs; 16320 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 16321 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16322 for (i = 0; i < newhelp->dthps_nprovs; i++) { 16323 newhelp->dthps_provs[i] = help->dthps_provs[i]; 16324 newhelp->dthps_provs[i]->dthp_ref++; 16325 } 16326 16327 hasprovs = 1; 16328 } 16329 16330 mutex_exit(&dtrace_lock); 16331 16332 if (hasprovs) 16333 dtrace_helper_provider_register(to, newhelp, NULL); 16334 } 16335 16336 /* 16337 * DTrace Hook Functions 16338 */ 16339 static void 16340 dtrace_module_loaded(modctl_t *ctl) 16341 { 16342 dtrace_provider_t *prv; 16343 16344 mutex_enter(&dtrace_provider_lock); 16345 #ifdef illumos 16346 mutex_enter(&mod_lock); 16347 #endif 16348 16349 #ifdef illumos 16350 ASSERT(ctl->mod_busy); 16351 #endif 16352 16353 /* 16354 * We're going to call each providers per-module provide operation 16355 * specifying only this module. 16356 */ 16357 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 16358 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 16359 16360 #ifdef illumos 16361 mutex_exit(&mod_lock); 16362 #endif 16363 mutex_exit(&dtrace_provider_lock); 16364 16365 /* 16366 * If we have any retained enablings, we need to match against them. 16367 * Enabling probes requires that cpu_lock be held, and we cannot hold 16368 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 16369 * module. (In particular, this happens when loading scheduling 16370 * classes.) So if we have any retained enablings, we need to dispatch 16371 * our task queue to do the match for us. 16372 */ 16373 mutex_enter(&dtrace_lock); 16374 16375 if (dtrace_retained == NULL) { 16376 mutex_exit(&dtrace_lock); 16377 return; 16378 } 16379 16380 (void) taskq_dispatch(dtrace_taskq, 16381 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 16382 16383 mutex_exit(&dtrace_lock); 16384 16385 /* 16386 * And now, for a little heuristic sleaze: in general, we want to 16387 * match modules as soon as they load. However, we cannot guarantee 16388 * this, because it would lead us to the lock ordering violation 16389 * outlined above. The common case, of course, is that cpu_lock is 16390 * _not_ held -- so we delay here for a clock tick, hoping that that's 16391 * long enough for the task queue to do its work. If it's not, it's 16392 * not a serious problem -- it just means that the module that we 16393 * just loaded may not be immediately instrumentable. 16394 */ 16395 delay(1); 16396 } 16397 16398 static void 16399 #ifdef illumos 16400 dtrace_module_unloaded(modctl_t *ctl) 16401 #else 16402 dtrace_module_unloaded(modctl_t *ctl, int *error) 16403 #endif 16404 { 16405 dtrace_probe_t template, *probe, *first, *next; 16406 dtrace_provider_t *prov; 16407 #ifndef illumos 16408 char modname[DTRACE_MODNAMELEN]; 16409 size_t len; 16410 #endif 16411 16412 #ifdef illumos 16413 template.dtpr_mod = ctl->mod_modname; 16414 #else 16415 /* Handle the fact that ctl->filename may end in ".ko". */ 16416 strlcpy(modname, ctl->filename, sizeof(modname)); 16417 len = strlen(ctl->filename); 16418 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0) 16419 modname[len - 3] = '\0'; 16420 template.dtpr_mod = modname; 16421 #endif 16422 16423 mutex_enter(&dtrace_provider_lock); 16424 #ifdef illumos 16425 mutex_enter(&mod_lock); 16426 #endif 16427 mutex_enter(&dtrace_lock); 16428 16429 #ifndef illumos 16430 if (ctl->nenabled > 0) { 16431 /* Don't allow unloads if a probe is enabled. */ 16432 mutex_exit(&dtrace_provider_lock); 16433 mutex_exit(&dtrace_lock); 16434 *error = -1; 16435 printf( 16436 "kldunload: attempt to unload module that has DTrace probes enabled\n"); 16437 return; 16438 } 16439 #endif 16440 16441 if (dtrace_bymod == NULL) { 16442 /* 16443 * The DTrace module is loaded (obviously) but not attached; 16444 * we don't have any work to do. 16445 */ 16446 mutex_exit(&dtrace_provider_lock); 16447 #ifdef illumos 16448 mutex_exit(&mod_lock); 16449 #endif 16450 mutex_exit(&dtrace_lock); 16451 return; 16452 } 16453 16454 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 16455 probe != NULL; probe = probe->dtpr_nextmod) { 16456 if (probe->dtpr_ecb != NULL) { 16457 mutex_exit(&dtrace_provider_lock); 16458 #ifdef illumos 16459 mutex_exit(&mod_lock); 16460 #endif 16461 mutex_exit(&dtrace_lock); 16462 16463 /* 16464 * This shouldn't _actually_ be possible -- we're 16465 * unloading a module that has an enabled probe in it. 16466 * (It's normally up to the provider to make sure that 16467 * this can't happen.) However, because dtps_enable() 16468 * doesn't have a failure mode, there can be an 16469 * enable/unload race. Upshot: we don't want to 16470 * assert, but we're not going to disable the 16471 * probe, either. 16472 */ 16473 if (dtrace_err_verbose) { 16474 #ifdef illumos 16475 cmn_err(CE_WARN, "unloaded module '%s' had " 16476 "enabled probes", ctl->mod_modname); 16477 #else 16478 cmn_err(CE_WARN, "unloaded module '%s' had " 16479 "enabled probes", modname); 16480 #endif 16481 } 16482 16483 return; 16484 } 16485 } 16486 16487 probe = first; 16488 16489 for (first = NULL; probe != NULL; probe = next) { 16490 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 16491 16492 dtrace_probes[probe->dtpr_id - 1] = NULL; 16493 16494 next = probe->dtpr_nextmod; 16495 dtrace_hash_remove(dtrace_bymod, probe); 16496 dtrace_hash_remove(dtrace_byfunc, probe); 16497 dtrace_hash_remove(dtrace_byname, probe); 16498 16499 if (first == NULL) { 16500 first = probe; 16501 probe->dtpr_nextmod = NULL; 16502 } else { 16503 probe->dtpr_nextmod = first; 16504 first = probe; 16505 } 16506 } 16507 16508 /* 16509 * We've removed all of the module's probes from the hash chains and 16510 * from the probe array. Now issue a dtrace_sync() to be sure that 16511 * everyone has cleared out from any probe array processing. 16512 */ 16513 dtrace_sync(); 16514 16515 for (probe = first; probe != NULL; probe = first) { 16516 first = probe->dtpr_nextmod; 16517 prov = probe->dtpr_provider; 16518 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 16519 probe->dtpr_arg); 16520 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 16521 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 16522 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 16523 #ifdef illumos 16524 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 16525 #else 16526 free_unr(dtrace_arena, probe->dtpr_id); 16527 #endif 16528 kmem_free(probe, sizeof (dtrace_probe_t)); 16529 } 16530 16531 mutex_exit(&dtrace_lock); 16532 #ifdef illumos 16533 mutex_exit(&mod_lock); 16534 #endif 16535 mutex_exit(&dtrace_provider_lock); 16536 } 16537 16538 #ifndef illumos 16539 static void 16540 dtrace_kld_load(void *arg __unused, linker_file_t lf) 16541 { 16542 16543 dtrace_module_loaded(lf); 16544 } 16545 16546 static void 16547 dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error) 16548 { 16549 16550 if (*error != 0) 16551 /* We already have an error, so don't do anything. */ 16552 return; 16553 dtrace_module_unloaded(lf, error); 16554 } 16555 #endif 16556 16557 #ifdef illumos 16558 static void 16559 dtrace_suspend(void) 16560 { 16561 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 16562 } 16563 16564 static void 16565 dtrace_resume(void) 16566 { 16567 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 16568 } 16569 #endif 16570 16571 static int 16572 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 16573 { 16574 ASSERT(MUTEX_HELD(&cpu_lock)); 16575 mutex_enter(&dtrace_lock); 16576 16577 switch (what) { 16578 case CPU_CONFIG: { 16579 dtrace_state_t *state; 16580 dtrace_optval_t *opt, rs, c; 16581 16582 /* 16583 * For now, we only allocate a new buffer for anonymous state. 16584 */ 16585 if ((state = dtrace_anon.dta_state) == NULL) 16586 break; 16587 16588 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 16589 break; 16590 16591 opt = state->dts_options; 16592 c = opt[DTRACEOPT_CPU]; 16593 16594 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 16595 break; 16596 16597 /* 16598 * Regardless of what the actual policy is, we're going to 16599 * temporarily set our resize policy to be manual. We're 16600 * also going to temporarily set our CPU option to denote 16601 * the newly configured CPU. 16602 */ 16603 rs = opt[DTRACEOPT_BUFRESIZE]; 16604 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 16605 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 16606 16607 (void) dtrace_state_buffers(state); 16608 16609 opt[DTRACEOPT_BUFRESIZE] = rs; 16610 opt[DTRACEOPT_CPU] = c; 16611 16612 break; 16613 } 16614 16615 case CPU_UNCONFIG: 16616 /* 16617 * We don't free the buffer in the CPU_UNCONFIG case. (The 16618 * buffer will be freed when the consumer exits.) 16619 */ 16620 break; 16621 16622 default: 16623 break; 16624 } 16625 16626 mutex_exit(&dtrace_lock); 16627 return (0); 16628 } 16629 16630 #ifdef illumos 16631 static void 16632 dtrace_cpu_setup_initial(processorid_t cpu) 16633 { 16634 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 16635 } 16636 #endif 16637 16638 static void 16639 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 16640 { 16641 if (dtrace_toxranges >= dtrace_toxranges_max) { 16642 int osize, nsize; 16643 dtrace_toxrange_t *range; 16644 16645 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16646 16647 if (osize == 0) { 16648 ASSERT(dtrace_toxrange == NULL); 16649 ASSERT(dtrace_toxranges_max == 0); 16650 dtrace_toxranges_max = 1; 16651 } else { 16652 dtrace_toxranges_max <<= 1; 16653 } 16654 16655 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16656 range = kmem_zalloc(nsize, KM_SLEEP); 16657 16658 if (dtrace_toxrange != NULL) { 16659 ASSERT(osize != 0); 16660 bcopy(dtrace_toxrange, range, osize); 16661 kmem_free(dtrace_toxrange, osize); 16662 } 16663 16664 dtrace_toxrange = range; 16665 } 16666 16667 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 16668 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 16669 16670 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 16671 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 16672 dtrace_toxranges++; 16673 } 16674 16675 static void 16676 dtrace_getf_barrier() 16677 { 16678 #ifdef illumos 16679 /* 16680 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 16681 * that contain calls to getf(), this routine will be called on every 16682 * closef() before either the underlying vnode is released or the 16683 * file_t itself is freed. By the time we are here, it is essential 16684 * that the file_t can no longer be accessed from a call to getf() 16685 * in probe context -- that assures that a dtrace_sync() can be used 16686 * to clear out any enablings referring to the old structures. 16687 */ 16688 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 16689 kcred->cr_zone->zone_dtrace_getf != 0) 16690 dtrace_sync(); 16691 #endif 16692 } 16693 16694 /* 16695 * DTrace Driver Cookbook Functions 16696 */ 16697 #ifdef illumos 16698 /*ARGSUSED*/ 16699 static int 16700 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 16701 { 16702 dtrace_provider_id_t id; 16703 dtrace_state_t *state = NULL; 16704 dtrace_enabling_t *enab; 16705 16706 mutex_enter(&cpu_lock); 16707 mutex_enter(&dtrace_provider_lock); 16708 mutex_enter(&dtrace_lock); 16709 16710 if (ddi_soft_state_init(&dtrace_softstate, 16711 sizeof (dtrace_state_t), 0) != 0) { 16712 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 16713 mutex_exit(&cpu_lock); 16714 mutex_exit(&dtrace_provider_lock); 16715 mutex_exit(&dtrace_lock); 16716 return (DDI_FAILURE); 16717 } 16718 16719 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 16720 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 16721 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 16722 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 16723 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 16724 ddi_remove_minor_node(devi, NULL); 16725 ddi_soft_state_fini(&dtrace_softstate); 16726 mutex_exit(&cpu_lock); 16727 mutex_exit(&dtrace_provider_lock); 16728 mutex_exit(&dtrace_lock); 16729 return (DDI_FAILURE); 16730 } 16731 16732 ddi_report_dev(devi); 16733 dtrace_devi = devi; 16734 16735 dtrace_modload = dtrace_module_loaded; 16736 dtrace_modunload = dtrace_module_unloaded; 16737 dtrace_cpu_init = dtrace_cpu_setup_initial; 16738 dtrace_helpers_cleanup = dtrace_helpers_destroy; 16739 dtrace_helpers_fork = dtrace_helpers_duplicate; 16740 dtrace_cpustart_init = dtrace_suspend; 16741 dtrace_cpustart_fini = dtrace_resume; 16742 dtrace_debugger_init = dtrace_suspend; 16743 dtrace_debugger_fini = dtrace_resume; 16744 16745 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16746 16747 ASSERT(MUTEX_HELD(&cpu_lock)); 16748 16749 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 16750 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 16751 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 16752 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 16753 VM_SLEEP | VMC_IDENTIFIER); 16754 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 16755 1, INT_MAX, 0); 16756 16757 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 16758 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 16759 NULL, NULL, NULL, NULL, NULL, 0); 16760 16761 ASSERT(MUTEX_HELD(&cpu_lock)); 16762 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 16763 offsetof(dtrace_probe_t, dtpr_nextmod), 16764 offsetof(dtrace_probe_t, dtpr_prevmod)); 16765 16766 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 16767 offsetof(dtrace_probe_t, dtpr_nextfunc), 16768 offsetof(dtrace_probe_t, dtpr_prevfunc)); 16769 16770 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 16771 offsetof(dtrace_probe_t, dtpr_nextname), 16772 offsetof(dtrace_probe_t, dtpr_prevname)); 16773 16774 if (dtrace_retain_max < 1) { 16775 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 16776 "setting to 1", dtrace_retain_max); 16777 dtrace_retain_max = 1; 16778 } 16779 16780 /* 16781 * Now discover our toxic ranges. 16782 */ 16783 dtrace_toxic_ranges(dtrace_toxrange_add); 16784 16785 /* 16786 * Before we register ourselves as a provider to our own framework, 16787 * we would like to assert that dtrace_provider is NULL -- but that's 16788 * not true if we were loaded as a dependency of a DTrace provider. 16789 * Once we've registered, we can assert that dtrace_provider is our 16790 * pseudo provider. 16791 */ 16792 (void) dtrace_register("dtrace", &dtrace_provider_attr, 16793 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 16794 16795 ASSERT(dtrace_provider != NULL); 16796 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 16797 16798 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 16799 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 16800 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 16801 dtrace_provider, NULL, NULL, "END", 0, NULL); 16802 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 16803 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 16804 16805 dtrace_anon_property(); 16806 mutex_exit(&cpu_lock); 16807 16808 /* 16809 * If there are already providers, we must ask them to provide their 16810 * probes, and then match any anonymous enabling against them. Note 16811 * that there should be no other retained enablings at this time: 16812 * the only retained enablings at this time should be the anonymous 16813 * enabling. 16814 */ 16815 if (dtrace_anon.dta_enabling != NULL) { 16816 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 16817 16818 dtrace_enabling_provide(NULL); 16819 state = dtrace_anon.dta_state; 16820 16821 /* 16822 * We couldn't hold cpu_lock across the above call to 16823 * dtrace_enabling_provide(), but we must hold it to actually 16824 * enable the probes. We have to drop all of our locks, pick 16825 * up cpu_lock, and regain our locks before matching the 16826 * retained anonymous enabling. 16827 */ 16828 mutex_exit(&dtrace_lock); 16829 mutex_exit(&dtrace_provider_lock); 16830 16831 mutex_enter(&cpu_lock); 16832 mutex_enter(&dtrace_provider_lock); 16833 mutex_enter(&dtrace_lock); 16834 16835 if ((enab = dtrace_anon.dta_enabling) != NULL) 16836 (void) dtrace_enabling_match(enab, NULL); 16837 16838 mutex_exit(&cpu_lock); 16839 } 16840 16841 mutex_exit(&dtrace_lock); 16842 mutex_exit(&dtrace_provider_lock); 16843 16844 if (state != NULL) { 16845 /* 16846 * If we created any anonymous state, set it going now. 16847 */ 16848 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 16849 } 16850 16851 return (DDI_SUCCESS); 16852 } 16853 #endif /* illumos */ 16854 16855 #ifndef illumos 16856 static void dtrace_dtr(void *); 16857 #endif 16858 16859 /*ARGSUSED*/ 16860 static int 16861 #ifdef illumos 16862 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 16863 #else 16864 dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 16865 #endif 16866 { 16867 dtrace_state_t *state; 16868 uint32_t priv; 16869 uid_t uid; 16870 zoneid_t zoneid; 16871 16872 #ifdef illumos 16873 if (getminor(*devp) == DTRACEMNRN_HELPER) 16874 return (0); 16875 16876 /* 16877 * If this wasn't an open with the "helper" minor, then it must be 16878 * the "dtrace" minor. 16879 */ 16880 if (getminor(*devp) == DTRACEMNRN_DTRACE) 16881 return (ENXIO); 16882 #else 16883 cred_t *cred_p = NULL; 16884 cred_p = dev->si_cred; 16885 16886 /* 16887 * If no DTRACE_PRIV_* bits are set in the credential, then the 16888 * caller lacks sufficient permission to do anything with DTrace. 16889 */ 16890 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 16891 if (priv == DTRACE_PRIV_NONE) { 16892 #endif 16893 16894 return (EACCES); 16895 } 16896 16897 /* 16898 * Ask all providers to provide all their probes. 16899 */ 16900 mutex_enter(&dtrace_provider_lock); 16901 dtrace_probe_provide(NULL, NULL); 16902 mutex_exit(&dtrace_provider_lock); 16903 16904 mutex_enter(&cpu_lock); 16905 mutex_enter(&dtrace_lock); 16906 dtrace_opens++; 16907 dtrace_membar_producer(); 16908 16909 #ifdef illumos 16910 /* 16911 * If the kernel debugger is active (that is, if the kernel debugger 16912 * modified text in some way), we won't allow the open. 16913 */ 16914 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 16915 dtrace_opens--; 16916 mutex_exit(&cpu_lock); 16917 mutex_exit(&dtrace_lock); 16918 return (EBUSY); 16919 } 16920 16921 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) { 16922 /* 16923 * If DTrace helper tracing is enabled, we need to allocate the 16924 * trace buffer and initialize the values. 16925 */ 16926 dtrace_helptrace_buffer = 16927 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 16928 dtrace_helptrace_next = 0; 16929 dtrace_helptrace_wrapped = 0; 16930 dtrace_helptrace_enable = 0; 16931 } 16932 16933 state = dtrace_state_create(devp, cred_p); 16934 #else 16935 state = dtrace_state_create(dev); 16936 devfs_set_cdevpriv(state, dtrace_dtr); 16937 #endif 16938 16939 mutex_exit(&cpu_lock); 16940 16941 if (state == NULL) { 16942 #ifdef illumos 16943 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 16944 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16945 #else 16946 --dtrace_opens; 16947 #endif 16948 mutex_exit(&dtrace_lock); 16949 return (EAGAIN); 16950 } 16951 16952 mutex_exit(&dtrace_lock); 16953 16954 return (0); 16955 } 16956 16957 /*ARGSUSED*/ 16958 #ifdef illumos 16959 static int 16960 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 16961 #else 16962 static void 16963 dtrace_dtr(void *data) 16964 #endif 16965 { 16966 #ifdef illumos 16967 minor_t minor = getminor(dev); 16968 dtrace_state_t *state; 16969 #endif 16970 dtrace_helptrace_t *buf = NULL; 16971 16972 #ifdef illumos 16973 if (minor == DTRACEMNRN_HELPER) 16974 return (0); 16975 16976 state = ddi_get_soft_state(dtrace_softstate, minor); 16977 #else 16978 dtrace_state_t *state = data; 16979 #endif 16980 16981 mutex_enter(&cpu_lock); 16982 mutex_enter(&dtrace_lock); 16983 16984 #ifdef illumos 16985 if (state->dts_anon) 16986 #else 16987 if (state != NULL && state->dts_anon) 16988 #endif 16989 { 16990 /* 16991 * There is anonymous state. Destroy that first. 16992 */ 16993 ASSERT(dtrace_anon.dta_state == NULL); 16994 dtrace_state_destroy(state->dts_anon); 16995 } 16996 16997 if (dtrace_helptrace_disable) { 16998 /* 16999 * If we have been told to disable helper tracing, set the 17000 * buffer to NULL before calling into dtrace_state_destroy(); 17001 * we take advantage of its dtrace_sync() to know that no 17002 * CPU is in probe context with enabled helper tracing 17003 * after it returns. 17004 */ 17005 buf = dtrace_helptrace_buffer; 17006 dtrace_helptrace_buffer = NULL; 17007 } 17008 17009 #ifdef illumos 17010 dtrace_state_destroy(state); 17011 #else 17012 if (state != NULL) { 17013 dtrace_state_destroy(state); 17014 kmem_free(state, 0); 17015 } 17016 #endif 17017 ASSERT(dtrace_opens > 0); 17018 17019 #ifdef illumos 17020 /* 17021 * Only relinquish control of the kernel debugger interface when there 17022 * are no consumers and no anonymous enablings. 17023 */ 17024 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17025 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17026 #else 17027 --dtrace_opens; 17028 #endif 17029 17030 if (buf != NULL) { 17031 kmem_free(buf, dtrace_helptrace_bufsize); 17032 dtrace_helptrace_disable = 0; 17033 } 17034 17035 mutex_exit(&dtrace_lock); 17036 mutex_exit(&cpu_lock); 17037 17038 #ifdef illumos 17039 return (0); 17040 #endif 17041 } 17042 17043 #ifdef illumos 17044 /*ARGSUSED*/ 17045 static int 17046 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 17047 { 17048 int rval; 17049 dof_helper_t help, *dhp = NULL; 17050 17051 switch (cmd) { 17052 case DTRACEHIOC_ADDDOF: 17053 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 17054 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 17055 return (EFAULT); 17056 } 17057 17058 dhp = &help; 17059 arg = (intptr_t)help.dofhp_dof; 17060 /*FALLTHROUGH*/ 17061 17062 case DTRACEHIOC_ADD: { 17063 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 17064 17065 if (dof == NULL) 17066 return (rval); 17067 17068 mutex_enter(&dtrace_lock); 17069 17070 /* 17071 * dtrace_helper_slurp() takes responsibility for the dof -- 17072 * it may free it now or it may save it and free it later. 17073 */ 17074 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 17075 *rv = rval; 17076 rval = 0; 17077 } else { 17078 rval = EINVAL; 17079 } 17080 17081 mutex_exit(&dtrace_lock); 17082 return (rval); 17083 } 17084 17085 case DTRACEHIOC_REMOVE: { 17086 mutex_enter(&dtrace_lock); 17087 rval = dtrace_helper_destroygen(NULL, arg); 17088 mutex_exit(&dtrace_lock); 17089 17090 return (rval); 17091 } 17092 17093 default: 17094 break; 17095 } 17096 17097 return (ENOTTY); 17098 } 17099 17100 /*ARGSUSED*/ 17101 static int 17102 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 17103 { 17104 minor_t minor = getminor(dev); 17105 dtrace_state_t *state; 17106 int rval; 17107 17108 if (minor == DTRACEMNRN_HELPER) 17109 return (dtrace_ioctl_helper(cmd, arg, rv)); 17110 17111 state = ddi_get_soft_state(dtrace_softstate, minor); 17112 17113 if (state->dts_anon) { 17114 ASSERT(dtrace_anon.dta_state == NULL); 17115 state = state->dts_anon; 17116 } 17117 17118 switch (cmd) { 17119 case DTRACEIOC_PROVIDER: { 17120 dtrace_providerdesc_t pvd; 17121 dtrace_provider_t *pvp; 17122 17123 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 17124 return (EFAULT); 17125 17126 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 17127 mutex_enter(&dtrace_provider_lock); 17128 17129 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 17130 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 17131 break; 17132 } 17133 17134 mutex_exit(&dtrace_provider_lock); 17135 17136 if (pvp == NULL) 17137 return (ESRCH); 17138 17139 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 17140 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 17141 17142 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 17143 return (EFAULT); 17144 17145 return (0); 17146 } 17147 17148 case DTRACEIOC_EPROBE: { 17149 dtrace_eprobedesc_t epdesc; 17150 dtrace_ecb_t *ecb; 17151 dtrace_action_t *act; 17152 void *buf; 17153 size_t size; 17154 uintptr_t dest; 17155 int nrecs; 17156 17157 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 17158 return (EFAULT); 17159 17160 mutex_enter(&dtrace_lock); 17161 17162 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 17163 mutex_exit(&dtrace_lock); 17164 return (EINVAL); 17165 } 17166 17167 if (ecb->dte_probe == NULL) { 17168 mutex_exit(&dtrace_lock); 17169 return (EINVAL); 17170 } 17171 17172 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 17173 epdesc.dtepd_uarg = ecb->dte_uarg; 17174 epdesc.dtepd_size = ecb->dte_size; 17175 17176 nrecs = epdesc.dtepd_nrecs; 17177 epdesc.dtepd_nrecs = 0; 17178 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17179 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17180 continue; 17181 17182 epdesc.dtepd_nrecs++; 17183 } 17184 17185 /* 17186 * Now that we have the size, we need to allocate a temporary 17187 * buffer in which to store the complete description. We need 17188 * the temporary buffer to be able to drop dtrace_lock() 17189 * across the copyout(), below. 17190 */ 17191 size = sizeof (dtrace_eprobedesc_t) + 17192 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 17193 17194 buf = kmem_alloc(size, KM_SLEEP); 17195 dest = (uintptr_t)buf; 17196 17197 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 17198 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 17199 17200 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17201 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17202 continue; 17203 17204 if (nrecs-- == 0) 17205 break; 17206 17207 bcopy(&act->dta_rec, (void *)dest, 17208 sizeof (dtrace_recdesc_t)); 17209 dest += sizeof (dtrace_recdesc_t); 17210 } 17211 17212 mutex_exit(&dtrace_lock); 17213 17214 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17215 kmem_free(buf, size); 17216 return (EFAULT); 17217 } 17218 17219 kmem_free(buf, size); 17220 return (0); 17221 } 17222 17223 case DTRACEIOC_AGGDESC: { 17224 dtrace_aggdesc_t aggdesc; 17225 dtrace_action_t *act; 17226 dtrace_aggregation_t *agg; 17227 int nrecs; 17228 uint32_t offs; 17229 dtrace_recdesc_t *lrec; 17230 void *buf; 17231 size_t size; 17232 uintptr_t dest; 17233 17234 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 17235 return (EFAULT); 17236 17237 mutex_enter(&dtrace_lock); 17238 17239 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 17240 mutex_exit(&dtrace_lock); 17241 return (EINVAL); 17242 } 17243 17244 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 17245 17246 nrecs = aggdesc.dtagd_nrecs; 17247 aggdesc.dtagd_nrecs = 0; 17248 17249 offs = agg->dtag_base; 17250 lrec = &agg->dtag_action.dta_rec; 17251 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 17252 17253 for (act = agg->dtag_first; ; act = act->dta_next) { 17254 ASSERT(act->dta_intuple || 17255 DTRACEACT_ISAGG(act->dta_kind)); 17256 17257 /* 17258 * If this action has a record size of zero, it 17259 * denotes an argument to the aggregating action. 17260 * Because the presence of this record doesn't (or 17261 * shouldn't) affect the way the data is interpreted, 17262 * we don't copy it out to save user-level the 17263 * confusion of dealing with a zero-length record. 17264 */ 17265 if (act->dta_rec.dtrd_size == 0) { 17266 ASSERT(agg->dtag_hasarg); 17267 continue; 17268 } 17269 17270 aggdesc.dtagd_nrecs++; 17271 17272 if (act == &agg->dtag_action) 17273 break; 17274 } 17275 17276 /* 17277 * Now that we have the size, we need to allocate a temporary 17278 * buffer in which to store the complete description. We need 17279 * the temporary buffer to be able to drop dtrace_lock() 17280 * across the copyout(), below. 17281 */ 17282 size = sizeof (dtrace_aggdesc_t) + 17283 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 17284 17285 buf = kmem_alloc(size, KM_SLEEP); 17286 dest = (uintptr_t)buf; 17287 17288 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 17289 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 17290 17291 for (act = agg->dtag_first; ; act = act->dta_next) { 17292 dtrace_recdesc_t rec = act->dta_rec; 17293 17294 /* 17295 * See the comment in the above loop for why we pass 17296 * over zero-length records. 17297 */ 17298 if (rec.dtrd_size == 0) { 17299 ASSERT(agg->dtag_hasarg); 17300 continue; 17301 } 17302 17303 if (nrecs-- == 0) 17304 break; 17305 17306 rec.dtrd_offset -= offs; 17307 bcopy(&rec, (void *)dest, sizeof (rec)); 17308 dest += sizeof (dtrace_recdesc_t); 17309 17310 if (act == &agg->dtag_action) 17311 break; 17312 } 17313 17314 mutex_exit(&dtrace_lock); 17315 17316 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17317 kmem_free(buf, size); 17318 return (EFAULT); 17319 } 17320 17321 kmem_free(buf, size); 17322 return (0); 17323 } 17324 17325 case DTRACEIOC_ENABLE: { 17326 dof_hdr_t *dof; 17327 dtrace_enabling_t *enab = NULL; 17328 dtrace_vstate_t *vstate; 17329 int err = 0; 17330 17331 *rv = 0; 17332 17333 /* 17334 * If a NULL argument has been passed, we take this as our 17335 * cue to reevaluate our enablings. 17336 */ 17337 if (arg == NULL) { 17338 dtrace_enabling_matchall(); 17339 17340 return (0); 17341 } 17342 17343 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 17344 return (rval); 17345 17346 mutex_enter(&cpu_lock); 17347 mutex_enter(&dtrace_lock); 17348 vstate = &state->dts_vstate; 17349 17350 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 17351 mutex_exit(&dtrace_lock); 17352 mutex_exit(&cpu_lock); 17353 dtrace_dof_destroy(dof); 17354 return (EBUSY); 17355 } 17356 17357 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 17358 mutex_exit(&dtrace_lock); 17359 mutex_exit(&cpu_lock); 17360 dtrace_dof_destroy(dof); 17361 return (EINVAL); 17362 } 17363 17364 if ((rval = dtrace_dof_options(dof, state)) != 0) { 17365 dtrace_enabling_destroy(enab); 17366 mutex_exit(&dtrace_lock); 17367 mutex_exit(&cpu_lock); 17368 dtrace_dof_destroy(dof); 17369 return (rval); 17370 } 17371 17372 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 17373 err = dtrace_enabling_retain(enab); 17374 } else { 17375 dtrace_enabling_destroy(enab); 17376 } 17377 17378 mutex_exit(&cpu_lock); 17379 mutex_exit(&dtrace_lock); 17380 dtrace_dof_destroy(dof); 17381 17382 return (err); 17383 } 17384 17385 case DTRACEIOC_REPLICATE: { 17386 dtrace_repldesc_t desc; 17387 dtrace_probedesc_t *match = &desc.dtrpd_match; 17388 dtrace_probedesc_t *create = &desc.dtrpd_create; 17389 int err; 17390 17391 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17392 return (EFAULT); 17393 17394 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17395 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17396 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17397 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17398 17399 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17400 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17401 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17402 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17403 17404 mutex_enter(&dtrace_lock); 17405 err = dtrace_enabling_replicate(state, match, create); 17406 mutex_exit(&dtrace_lock); 17407 17408 return (err); 17409 } 17410 17411 case DTRACEIOC_PROBEMATCH: 17412 case DTRACEIOC_PROBES: { 17413 dtrace_probe_t *probe = NULL; 17414 dtrace_probedesc_t desc; 17415 dtrace_probekey_t pkey; 17416 dtrace_id_t i; 17417 int m = 0; 17418 uint32_t priv; 17419 uid_t uid; 17420 zoneid_t zoneid; 17421 17422 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17423 return (EFAULT); 17424 17425 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17426 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17427 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17428 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17429 17430 /* 17431 * Before we attempt to match this probe, we want to give 17432 * all providers the opportunity to provide it. 17433 */ 17434 if (desc.dtpd_id == DTRACE_IDNONE) { 17435 mutex_enter(&dtrace_provider_lock); 17436 dtrace_probe_provide(&desc, NULL); 17437 mutex_exit(&dtrace_provider_lock); 17438 desc.dtpd_id++; 17439 } 17440 17441 if (cmd == DTRACEIOC_PROBEMATCH) { 17442 dtrace_probekey(&desc, &pkey); 17443 pkey.dtpk_id = DTRACE_IDNONE; 17444 } 17445 17446 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 17447 17448 mutex_enter(&dtrace_lock); 17449 17450 if (cmd == DTRACEIOC_PROBEMATCH) { 17451 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17452 if ((probe = dtrace_probes[i - 1]) != NULL && 17453 (m = dtrace_match_probe(probe, &pkey, 17454 priv, uid, zoneid)) != 0) 17455 break; 17456 } 17457 17458 if (m < 0) { 17459 mutex_exit(&dtrace_lock); 17460 return (EINVAL); 17461 } 17462 17463 } else { 17464 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17465 if ((probe = dtrace_probes[i - 1]) != NULL && 17466 dtrace_match_priv(probe, priv, uid, zoneid)) 17467 break; 17468 } 17469 } 17470 17471 if (probe == NULL) { 17472 mutex_exit(&dtrace_lock); 17473 return (ESRCH); 17474 } 17475 17476 dtrace_probe_description(probe, &desc); 17477 mutex_exit(&dtrace_lock); 17478 17479 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17480 return (EFAULT); 17481 17482 return (0); 17483 } 17484 17485 case DTRACEIOC_PROBEARG: { 17486 dtrace_argdesc_t desc; 17487 dtrace_probe_t *probe; 17488 dtrace_provider_t *prov; 17489 17490 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17491 return (EFAULT); 17492 17493 if (desc.dtargd_id == DTRACE_IDNONE) 17494 return (EINVAL); 17495 17496 if (desc.dtargd_ndx == DTRACE_ARGNONE) 17497 return (EINVAL); 17498 17499 mutex_enter(&dtrace_provider_lock); 17500 mutex_enter(&mod_lock); 17501 mutex_enter(&dtrace_lock); 17502 17503 if (desc.dtargd_id > dtrace_nprobes) { 17504 mutex_exit(&dtrace_lock); 17505 mutex_exit(&mod_lock); 17506 mutex_exit(&dtrace_provider_lock); 17507 return (EINVAL); 17508 } 17509 17510 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 17511 mutex_exit(&dtrace_lock); 17512 mutex_exit(&mod_lock); 17513 mutex_exit(&dtrace_provider_lock); 17514 return (EINVAL); 17515 } 17516 17517 mutex_exit(&dtrace_lock); 17518 17519 prov = probe->dtpr_provider; 17520 17521 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 17522 /* 17523 * There isn't any typed information for this probe. 17524 * Set the argument number to DTRACE_ARGNONE. 17525 */ 17526 desc.dtargd_ndx = DTRACE_ARGNONE; 17527 } else { 17528 desc.dtargd_native[0] = '\0'; 17529 desc.dtargd_xlate[0] = '\0'; 17530 desc.dtargd_mapping = desc.dtargd_ndx; 17531 17532 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 17533 probe->dtpr_id, probe->dtpr_arg, &desc); 17534 } 17535 17536 mutex_exit(&mod_lock); 17537 mutex_exit(&dtrace_provider_lock); 17538 17539 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17540 return (EFAULT); 17541 17542 return (0); 17543 } 17544 17545 case DTRACEIOC_GO: { 17546 processorid_t cpuid; 17547 rval = dtrace_state_go(state, &cpuid); 17548 17549 if (rval != 0) 17550 return (rval); 17551 17552 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17553 return (EFAULT); 17554 17555 return (0); 17556 } 17557 17558 case DTRACEIOC_STOP: { 17559 processorid_t cpuid; 17560 17561 mutex_enter(&dtrace_lock); 17562 rval = dtrace_state_stop(state, &cpuid); 17563 mutex_exit(&dtrace_lock); 17564 17565 if (rval != 0) 17566 return (rval); 17567 17568 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17569 return (EFAULT); 17570 17571 return (0); 17572 } 17573 17574 case DTRACEIOC_DOFGET: { 17575 dof_hdr_t hdr, *dof; 17576 uint64_t len; 17577 17578 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 17579 return (EFAULT); 17580 17581 mutex_enter(&dtrace_lock); 17582 dof = dtrace_dof_create(state); 17583 mutex_exit(&dtrace_lock); 17584 17585 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 17586 rval = copyout(dof, (void *)arg, len); 17587 dtrace_dof_destroy(dof); 17588 17589 return (rval == 0 ? 0 : EFAULT); 17590 } 17591 17592 case DTRACEIOC_AGGSNAP: 17593 case DTRACEIOC_BUFSNAP: { 17594 dtrace_bufdesc_t desc; 17595 caddr_t cached; 17596 dtrace_buffer_t *buf; 17597 17598 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17599 return (EFAULT); 17600 17601 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 17602 return (EINVAL); 17603 17604 mutex_enter(&dtrace_lock); 17605 17606 if (cmd == DTRACEIOC_BUFSNAP) { 17607 buf = &state->dts_buffer[desc.dtbd_cpu]; 17608 } else { 17609 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 17610 } 17611 17612 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 17613 size_t sz = buf->dtb_offset; 17614 17615 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 17616 mutex_exit(&dtrace_lock); 17617 return (EBUSY); 17618 } 17619 17620 /* 17621 * If this buffer has already been consumed, we're 17622 * going to indicate that there's nothing left here 17623 * to consume. 17624 */ 17625 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 17626 mutex_exit(&dtrace_lock); 17627 17628 desc.dtbd_size = 0; 17629 desc.dtbd_drops = 0; 17630 desc.dtbd_errors = 0; 17631 desc.dtbd_oldest = 0; 17632 sz = sizeof (desc); 17633 17634 if (copyout(&desc, (void *)arg, sz) != 0) 17635 return (EFAULT); 17636 17637 return (0); 17638 } 17639 17640 /* 17641 * If this is a ring buffer that has wrapped, we want 17642 * to copy the whole thing out. 17643 */ 17644 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 17645 dtrace_buffer_polish(buf); 17646 sz = buf->dtb_size; 17647 } 17648 17649 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 17650 mutex_exit(&dtrace_lock); 17651 return (EFAULT); 17652 } 17653 17654 desc.dtbd_size = sz; 17655 desc.dtbd_drops = buf->dtb_drops; 17656 desc.dtbd_errors = buf->dtb_errors; 17657 desc.dtbd_oldest = buf->dtb_xamot_offset; 17658 desc.dtbd_timestamp = dtrace_gethrtime(); 17659 17660 mutex_exit(&dtrace_lock); 17661 17662 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17663 return (EFAULT); 17664 17665 buf->dtb_flags |= DTRACEBUF_CONSUMED; 17666 17667 return (0); 17668 } 17669 17670 if (buf->dtb_tomax == NULL) { 17671 ASSERT(buf->dtb_xamot == NULL); 17672 mutex_exit(&dtrace_lock); 17673 return (ENOENT); 17674 } 17675 17676 cached = buf->dtb_tomax; 17677 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 17678 17679 dtrace_xcall(desc.dtbd_cpu, 17680 (dtrace_xcall_t)dtrace_buffer_switch, buf); 17681 17682 state->dts_errors += buf->dtb_xamot_errors; 17683 17684 /* 17685 * If the buffers did not actually switch, then the cross call 17686 * did not take place -- presumably because the given CPU is 17687 * not in the ready set. If this is the case, we'll return 17688 * ENOENT. 17689 */ 17690 if (buf->dtb_tomax == cached) { 17691 ASSERT(buf->dtb_xamot != cached); 17692 mutex_exit(&dtrace_lock); 17693 return (ENOENT); 17694 } 17695 17696 ASSERT(cached == buf->dtb_xamot); 17697 17698 /* 17699 * We have our snapshot; now copy it out. 17700 */ 17701 if (copyout(buf->dtb_xamot, desc.dtbd_data, 17702 buf->dtb_xamot_offset) != 0) { 17703 mutex_exit(&dtrace_lock); 17704 return (EFAULT); 17705 } 17706 17707 desc.dtbd_size = buf->dtb_xamot_offset; 17708 desc.dtbd_drops = buf->dtb_xamot_drops; 17709 desc.dtbd_errors = buf->dtb_xamot_errors; 17710 desc.dtbd_oldest = 0; 17711 desc.dtbd_timestamp = buf->dtb_switched; 17712 17713 mutex_exit(&dtrace_lock); 17714 17715 /* 17716 * Finally, copy out the buffer description. 17717 */ 17718 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17719 return (EFAULT); 17720 17721 return (0); 17722 } 17723 17724 case DTRACEIOC_CONF: { 17725 dtrace_conf_t conf; 17726 17727 bzero(&conf, sizeof (conf)); 17728 conf.dtc_difversion = DIF_VERSION; 17729 conf.dtc_difintregs = DIF_DIR_NREGS; 17730 conf.dtc_diftupregs = DIF_DTR_NREGS; 17731 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 17732 17733 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 17734 return (EFAULT); 17735 17736 return (0); 17737 } 17738 17739 case DTRACEIOC_STATUS: { 17740 dtrace_status_t stat; 17741 dtrace_dstate_t *dstate; 17742 int i, j; 17743 uint64_t nerrs; 17744 17745 /* 17746 * See the comment in dtrace_state_deadman() for the reason 17747 * for setting dts_laststatus to INT64_MAX before setting 17748 * it to the correct value. 17749 */ 17750 state->dts_laststatus = INT64_MAX; 17751 dtrace_membar_producer(); 17752 state->dts_laststatus = dtrace_gethrtime(); 17753 17754 bzero(&stat, sizeof (stat)); 17755 17756 mutex_enter(&dtrace_lock); 17757 17758 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 17759 mutex_exit(&dtrace_lock); 17760 return (ENOENT); 17761 } 17762 17763 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 17764 stat.dtst_exiting = 1; 17765 17766 nerrs = state->dts_errors; 17767 dstate = &state->dts_vstate.dtvs_dynvars; 17768 17769 for (i = 0; i < NCPU; i++) { 17770 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 17771 17772 stat.dtst_dyndrops += dcpu->dtdsc_drops; 17773 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 17774 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 17775 17776 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 17777 stat.dtst_filled++; 17778 17779 nerrs += state->dts_buffer[i].dtb_errors; 17780 17781 for (j = 0; j < state->dts_nspeculations; j++) { 17782 dtrace_speculation_t *spec; 17783 dtrace_buffer_t *buf; 17784 17785 spec = &state->dts_speculations[j]; 17786 buf = &spec->dtsp_buffer[i]; 17787 stat.dtst_specdrops += buf->dtb_xamot_drops; 17788 } 17789 } 17790 17791 stat.dtst_specdrops_busy = state->dts_speculations_busy; 17792 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 17793 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 17794 stat.dtst_dblerrors = state->dts_dblerrors; 17795 stat.dtst_killed = 17796 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 17797 stat.dtst_errors = nerrs; 17798 17799 mutex_exit(&dtrace_lock); 17800 17801 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 17802 return (EFAULT); 17803 17804 return (0); 17805 } 17806 17807 case DTRACEIOC_FORMAT: { 17808 dtrace_fmtdesc_t fmt; 17809 char *str; 17810 int len; 17811 17812 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 17813 return (EFAULT); 17814 17815 mutex_enter(&dtrace_lock); 17816 17817 if (fmt.dtfd_format == 0 || 17818 fmt.dtfd_format > state->dts_nformats) { 17819 mutex_exit(&dtrace_lock); 17820 return (EINVAL); 17821 } 17822 17823 /* 17824 * Format strings are allocated contiguously and they are 17825 * never freed; if a format index is less than the number 17826 * of formats, we can assert that the format map is non-NULL 17827 * and that the format for the specified index is non-NULL. 17828 */ 17829 ASSERT(state->dts_formats != NULL); 17830 str = state->dts_formats[fmt.dtfd_format - 1]; 17831 ASSERT(str != NULL); 17832 17833 len = strlen(str) + 1; 17834 17835 if (len > fmt.dtfd_length) { 17836 fmt.dtfd_length = len; 17837 17838 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 17839 mutex_exit(&dtrace_lock); 17840 return (EINVAL); 17841 } 17842 } else { 17843 if (copyout(str, fmt.dtfd_string, len) != 0) { 17844 mutex_exit(&dtrace_lock); 17845 return (EINVAL); 17846 } 17847 } 17848 17849 mutex_exit(&dtrace_lock); 17850 return (0); 17851 } 17852 17853 default: 17854 break; 17855 } 17856 17857 return (ENOTTY); 17858 } 17859 17860 /*ARGSUSED*/ 17861 static int 17862 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 17863 { 17864 dtrace_state_t *state; 17865 17866 switch (cmd) { 17867 case DDI_DETACH: 17868 break; 17869 17870 case DDI_SUSPEND: 17871 return (DDI_SUCCESS); 17872 17873 default: 17874 return (DDI_FAILURE); 17875 } 17876 17877 mutex_enter(&cpu_lock); 17878 mutex_enter(&dtrace_provider_lock); 17879 mutex_enter(&dtrace_lock); 17880 17881 ASSERT(dtrace_opens == 0); 17882 17883 if (dtrace_helpers > 0) { 17884 mutex_exit(&dtrace_provider_lock); 17885 mutex_exit(&dtrace_lock); 17886 mutex_exit(&cpu_lock); 17887 return (DDI_FAILURE); 17888 } 17889 17890 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 17891 mutex_exit(&dtrace_provider_lock); 17892 mutex_exit(&dtrace_lock); 17893 mutex_exit(&cpu_lock); 17894 return (DDI_FAILURE); 17895 } 17896 17897 dtrace_provider = NULL; 17898 17899 if ((state = dtrace_anon_grab()) != NULL) { 17900 /* 17901 * If there were ECBs on this state, the provider should 17902 * have not been allowed to detach; assert that there is 17903 * none. 17904 */ 17905 ASSERT(state->dts_necbs == 0); 17906 dtrace_state_destroy(state); 17907 17908 /* 17909 * If we're being detached with anonymous state, we need to 17910 * indicate to the kernel debugger that DTrace is now inactive. 17911 */ 17912 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17913 } 17914 17915 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 17916 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 17917 dtrace_cpu_init = NULL; 17918 dtrace_helpers_cleanup = NULL; 17919 dtrace_helpers_fork = NULL; 17920 dtrace_cpustart_init = NULL; 17921 dtrace_cpustart_fini = NULL; 17922 dtrace_debugger_init = NULL; 17923 dtrace_debugger_fini = NULL; 17924 dtrace_modload = NULL; 17925 dtrace_modunload = NULL; 17926 17927 ASSERT(dtrace_getf == 0); 17928 ASSERT(dtrace_closef == NULL); 17929 17930 mutex_exit(&cpu_lock); 17931 17932 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 17933 dtrace_probes = NULL; 17934 dtrace_nprobes = 0; 17935 17936 dtrace_hash_destroy(dtrace_bymod); 17937 dtrace_hash_destroy(dtrace_byfunc); 17938 dtrace_hash_destroy(dtrace_byname); 17939 dtrace_bymod = NULL; 17940 dtrace_byfunc = NULL; 17941 dtrace_byname = NULL; 17942 17943 kmem_cache_destroy(dtrace_state_cache); 17944 vmem_destroy(dtrace_minor); 17945 vmem_destroy(dtrace_arena); 17946 17947 if (dtrace_toxrange != NULL) { 17948 kmem_free(dtrace_toxrange, 17949 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 17950 dtrace_toxrange = NULL; 17951 dtrace_toxranges = 0; 17952 dtrace_toxranges_max = 0; 17953 } 17954 17955 ddi_remove_minor_node(dtrace_devi, NULL); 17956 dtrace_devi = NULL; 17957 17958 ddi_soft_state_fini(&dtrace_softstate); 17959 17960 ASSERT(dtrace_vtime_references == 0); 17961 ASSERT(dtrace_opens == 0); 17962 ASSERT(dtrace_retained == NULL); 17963 17964 mutex_exit(&dtrace_lock); 17965 mutex_exit(&dtrace_provider_lock); 17966 17967 /* 17968 * We don't destroy the task queue until after we have dropped our 17969 * locks (taskq_destroy() may block on running tasks). To prevent 17970 * attempting to do work after we have effectively detached but before 17971 * the task queue has been destroyed, all tasks dispatched via the 17972 * task queue must check that DTrace is still attached before 17973 * performing any operation. 17974 */ 17975 taskq_destroy(dtrace_taskq); 17976 dtrace_taskq = NULL; 17977 17978 return (DDI_SUCCESS); 17979 } 17980 #endif 17981 17982 #ifdef illumos 17983 /*ARGSUSED*/ 17984 static int 17985 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 17986 { 17987 int error; 17988 17989 switch (infocmd) { 17990 case DDI_INFO_DEVT2DEVINFO: 17991 *result = (void *)dtrace_devi; 17992 error = DDI_SUCCESS; 17993 break; 17994 case DDI_INFO_DEVT2INSTANCE: 17995 *result = (void *)0; 17996 error = DDI_SUCCESS; 17997 break; 17998 default: 17999 error = DDI_FAILURE; 18000 } 18001 return (error); 18002 } 18003 #endif 18004 18005 #ifdef illumos 18006 static struct cb_ops dtrace_cb_ops = { 18007 dtrace_open, /* open */ 18008 dtrace_close, /* close */ 18009 nulldev, /* strategy */ 18010 nulldev, /* print */ 18011 nodev, /* dump */ 18012 nodev, /* read */ 18013 nodev, /* write */ 18014 dtrace_ioctl, /* ioctl */ 18015 nodev, /* devmap */ 18016 nodev, /* mmap */ 18017 nodev, /* segmap */ 18018 nochpoll, /* poll */ 18019 ddi_prop_op, /* cb_prop_op */ 18020 0, /* streamtab */ 18021 D_NEW | D_MP /* Driver compatibility flag */ 18022 }; 18023 18024 static struct dev_ops dtrace_ops = { 18025 DEVO_REV, /* devo_rev */ 18026 0, /* refcnt */ 18027 dtrace_info, /* get_dev_info */ 18028 nulldev, /* identify */ 18029 nulldev, /* probe */ 18030 dtrace_attach, /* attach */ 18031 dtrace_detach, /* detach */ 18032 nodev, /* reset */ 18033 &dtrace_cb_ops, /* driver operations */ 18034 NULL, /* bus operations */ 18035 nodev /* dev power */ 18036 }; 18037 18038 static struct modldrv modldrv = { 18039 &mod_driverops, /* module type (this is a pseudo driver) */ 18040 "Dynamic Tracing", /* name of module */ 18041 &dtrace_ops, /* driver ops */ 18042 }; 18043 18044 static struct modlinkage modlinkage = { 18045 MODREV_1, 18046 (void *)&modldrv, 18047 NULL 18048 }; 18049 18050 int 18051 _init(void) 18052 { 18053 return (mod_install(&modlinkage)); 18054 } 18055 18056 int 18057 _info(struct modinfo *modinfop) 18058 { 18059 return (mod_info(&modlinkage, modinfop)); 18060 } 18061 18062 int 18063 _fini(void) 18064 { 18065 return (mod_remove(&modlinkage)); 18066 } 18067 #else 18068 18069 static d_ioctl_t dtrace_ioctl; 18070 static d_ioctl_t dtrace_ioctl_helper; 18071 static void dtrace_load(void *); 18072 static int dtrace_unload(void); 18073 static struct cdev *dtrace_dev; 18074 static struct cdev *helper_dev; 18075 18076 void dtrace_invop_init(void); 18077 void dtrace_invop_uninit(void); 18078 18079 static struct cdevsw dtrace_cdevsw = { 18080 .d_version = D_VERSION, 18081 .d_ioctl = dtrace_ioctl, 18082 .d_open = dtrace_open, 18083 .d_name = "dtrace", 18084 }; 18085 18086 static struct cdevsw helper_cdevsw = { 18087 .d_version = D_VERSION, 18088 .d_ioctl = dtrace_ioctl_helper, 18089 .d_name = "helper", 18090 }; 18091 18092 #include <dtrace_anon.c> 18093 #include <dtrace_ioctl.c> 18094 #include <dtrace_load.c> 18095 #include <dtrace_modevent.c> 18096 #include <dtrace_sysctl.c> 18097 #include <dtrace_unload.c> 18098 #include <dtrace_vtime.c> 18099 #include <dtrace_hacks.c> 18100 #include <dtrace_isa.c> 18101 18102 SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 18103 SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 18104 SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 18105 18106 DEV_MODULE(dtrace, dtrace_modevent, NULL); 18107 MODULE_VERSION(dtrace, 1); 18108 MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 18109 #endif 18110