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) 2016, 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/linker.h> 121 #include <sys/kdb.h> 122 #include <sys/kernel.h> 123 #include <sys/malloc.h> 124 #include <sys/lock.h> 125 #include <sys/mutex.h> 126 #include <sys/ptrace.h> 127 #include <sys/rwlock.h> 128 #include <sys/sx.h> 129 #include <sys/sysctl.h> 130 131 #include <sys/dtrace_bsd.h> 132 133 #include <netinet/in.h> 134 135 #include "dtrace_cddl.h" 136 #include "dtrace_debug.c" 137 #endif 138 139 /* 140 * DTrace Tunable Variables 141 * 142 * The following variables may be tuned by adding a line to /etc/system that 143 * includes both the name of the DTrace module ("dtrace") and the name of the 144 * variable. For example: 145 * 146 * set dtrace:dtrace_destructive_disallow = 1 147 * 148 * In general, the only variables that one should be tuning this way are those 149 * that affect system-wide DTrace behavior, and for which the default behavior 150 * is undesirable. Most of these variables are tunable on a per-consumer 151 * basis using DTrace options, and need not be tuned on a system-wide basis. 152 * When tuning these variables, avoid pathological values; while some attempt 153 * is made to verify the integrity of these variables, they are not considered 154 * part of the supported interface to DTrace, and they are therefore not 155 * checked comprehensively. Further, these variables should not be tuned 156 * dynamically via "mdb -kw" or other means; they should only be tuned via 157 * /etc/system. 158 */ 159 int dtrace_destructive_disallow = 0; 160 #ifndef illumos 161 /* Positive logic version of dtrace_destructive_disallow for loader tunable */ 162 int dtrace_allow_destructive = 1; 163 #endif 164 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 165 size_t dtrace_difo_maxsize = (256 * 1024); 166 dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 167 size_t dtrace_statvar_maxsize = (16 * 1024); 168 size_t dtrace_actions_max = (16 * 1024); 169 size_t dtrace_retain_max = 1024; 170 dtrace_optval_t dtrace_helper_actions_max = 128; 171 dtrace_optval_t dtrace_helper_providers_max = 32; 172 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 173 size_t dtrace_strsize_default = 256; 174 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 175 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 176 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 177 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 178 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 179 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 180 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 181 dtrace_optval_t dtrace_nspec_default = 1; 182 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 183 dtrace_optval_t dtrace_stackframes_default = 20; 184 dtrace_optval_t dtrace_ustackframes_default = 20; 185 dtrace_optval_t dtrace_jstackframes_default = 50; 186 dtrace_optval_t dtrace_jstackstrsize_default = 512; 187 int dtrace_msgdsize_max = 128; 188 hrtime_t dtrace_chill_max = MSEC2NSEC(500); /* 500 ms */ 189 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 190 int dtrace_devdepth_max = 32; 191 int dtrace_err_verbose; 192 hrtime_t dtrace_deadman_interval = NANOSEC; 193 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 194 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 195 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 196 #ifndef illumos 197 int dtrace_memstr_max = 4096; 198 #endif 199 200 /* 201 * DTrace External Variables 202 * 203 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 204 * available to DTrace consumers via the backtick (`) syntax. One of these, 205 * dtrace_zero, is made deliberately so: it is provided as a source of 206 * well-known, zero-filled memory. While this variable is not documented, 207 * it is used by some translators as an implementation detail. 208 */ 209 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 210 211 /* 212 * DTrace Internal Variables 213 */ 214 #ifdef illumos 215 static dev_info_t *dtrace_devi; /* device info */ 216 #endif 217 #ifdef illumos 218 static vmem_t *dtrace_arena; /* probe ID arena */ 219 static vmem_t *dtrace_minor; /* minor number arena */ 220 #else 221 static taskq_t *dtrace_taskq; /* task queue */ 222 static struct unrhdr *dtrace_arena; /* Probe ID number. */ 223 #endif 224 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 225 static int dtrace_nprobes; /* number of probes */ 226 static dtrace_provider_t *dtrace_provider; /* provider list */ 227 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 228 static int dtrace_opens; /* number of opens */ 229 static int dtrace_helpers; /* number of helpers */ 230 static int dtrace_getf; /* number of unpriv getf()s */ 231 #ifdef illumos 232 static void *dtrace_softstate; /* softstate pointer */ 233 #endif 234 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 235 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 236 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 237 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 238 static int dtrace_toxranges; /* number of toxic ranges */ 239 static int dtrace_toxranges_max; /* size of toxic range array */ 240 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 241 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 242 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 243 static kthread_t *dtrace_panicked; /* panicking thread */ 244 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 245 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 246 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 247 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 248 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 249 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 250 static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 251 #ifndef illumos 252 static struct mtx dtrace_unr_mtx; 253 MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 254 static eventhandler_tag dtrace_kld_load_tag; 255 static eventhandler_tag dtrace_kld_unload_try_tag; 256 #endif 257 258 /* 259 * DTrace Locking 260 * DTrace is protected by three (relatively coarse-grained) locks: 261 * 262 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 263 * including enabling state, probes, ECBs, consumer state, helper state, 264 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 265 * probe context is lock-free -- synchronization is handled via the 266 * dtrace_sync() cross call mechanism. 267 * 268 * (2) dtrace_provider_lock is required when manipulating provider state, or 269 * when provider state must be held constant. 270 * 271 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 272 * when meta provider state must be held constant. 273 * 274 * The lock ordering between these three locks is dtrace_meta_lock before 275 * dtrace_provider_lock before dtrace_lock. (In particular, there are 276 * several places where dtrace_provider_lock is held by the framework as it 277 * calls into the providers -- which then call back into the framework, 278 * grabbing dtrace_lock.) 279 * 280 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 281 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 282 * role as a coarse-grained lock; it is acquired before both of these locks. 283 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 284 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 285 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 286 * acquired _between_ dtrace_provider_lock and dtrace_lock. 287 */ 288 static kmutex_t dtrace_lock; /* probe state lock */ 289 static kmutex_t dtrace_provider_lock; /* provider state lock */ 290 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 291 292 #ifndef illumos 293 /* XXX FreeBSD hacks. */ 294 #define cr_suid cr_svuid 295 #define cr_sgid cr_svgid 296 #define ipaddr_t in_addr_t 297 #define mod_modname pathname 298 #define vuprintf vprintf 299 #define ttoproc(_a) ((_a)->td_proc) 300 #define crgetzoneid(_a) 0 301 #define NCPU MAXCPU 302 #define SNOCD 0 303 #define CPU_ON_INTR(_a) 0 304 305 #define PRIV_EFFECTIVE (1 << 0) 306 #define PRIV_DTRACE_KERNEL (1 << 1) 307 #define PRIV_DTRACE_PROC (1 << 2) 308 #define PRIV_DTRACE_USER (1 << 3) 309 #define PRIV_PROC_OWNER (1 << 4) 310 #define PRIV_PROC_ZONE (1 << 5) 311 #define PRIV_ALL ~0 312 313 SYSCTL_DECL(_debug_dtrace); 314 SYSCTL_DECL(_kern_dtrace); 315 #endif 316 317 #ifdef illumos 318 #define curcpu CPU->cpu_id 319 #endif 320 321 322 /* 323 * DTrace Provider Variables 324 * 325 * These are the variables relating to DTrace as a provider (that is, the 326 * provider of the BEGIN, END, and ERROR probes). 327 */ 328 static dtrace_pattr_t dtrace_provider_attr = { 329 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 330 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 331 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 332 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 333 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 334 }; 335 336 static void 337 dtrace_nullop(void) 338 {} 339 340 static dtrace_pops_t dtrace_provider_ops = { 341 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 342 (void (*)(void *, modctl_t *))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 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 347 NULL, 348 NULL, 349 NULL, 350 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 351 }; 352 353 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 354 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 355 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 356 357 /* 358 * DTrace Helper Tracing Variables 359 * 360 * These variables should be set dynamically to enable helper tracing. The 361 * only variables that should be set are dtrace_helptrace_enable (which should 362 * be set to a non-zero value to allocate helper tracing buffers on the next 363 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a 364 * non-zero value to deallocate helper tracing buffers on the next close of 365 * /dev/dtrace). When (and only when) helper tracing is disabled, the 366 * buffer size may also be set via dtrace_helptrace_bufsize. 367 */ 368 int dtrace_helptrace_enable = 0; 369 int dtrace_helptrace_disable = 0; 370 int dtrace_helptrace_bufsize = 16 * 1024 * 1024; 371 uint32_t dtrace_helptrace_nlocals; 372 static dtrace_helptrace_t *dtrace_helptrace_buffer; 373 static uint32_t dtrace_helptrace_next = 0; 374 static int dtrace_helptrace_wrapped = 0; 375 376 /* 377 * DTrace Error Hashing 378 * 379 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 380 * table. This is very useful for checking coverage of tests that are 381 * expected to induce DIF or DOF processing errors, and may be useful for 382 * debugging problems in the DIF code generator or in DOF generation . The 383 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 384 */ 385 #ifdef DEBUG 386 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 387 static const char *dtrace_errlast; 388 static kthread_t *dtrace_errthread; 389 static kmutex_t dtrace_errlock; 390 #endif 391 392 /* 393 * DTrace Macros and Constants 394 * 395 * These are various macros that are useful in various spots in the 396 * implementation, along with a few random constants that have no meaning 397 * outside of the implementation. There is no real structure to this cpp 398 * mishmash -- but is there ever? 399 */ 400 #define DTRACE_HASHSTR(hash, probe) \ 401 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 402 403 #define DTRACE_HASHNEXT(hash, probe) \ 404 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 405 406 #define DTRACE_HASHPREV(hash, probe) \ 407 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 408 409 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 410 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 411 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 412 413 #define DTRACE_AGGHASHSIZE_SLEW 17 414 415 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 416 417 /* 418 * The key for a thread-local variable consists of the lower 61 bits of the 419 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 420 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 421 * equal to a variable identifier. This is necessary (but not sufficient) to 422 * assure that global associative arrays never collide with thread-local 423 * variables. To guarantee that they cannot collide, we must also define the 424 * order for keying dynamic variables. That order is: 425 * 426 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 427 * 428 * Because the variable-key and the tls-key are in orthogonal spaces, there is 429 * no way for a global variable key signature to match a thread-local key 430 * signature. 431 */ 432 #ifdef illumos 433 #define DTRACE_TLS_THRKEY(where) { \ 434 uint_t intr = 0; \ 435 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 436 for (; actv; actv >>= 1) \ 437 intr++; \ 438 ASSERT(intr < (1 << 3)); \ 439 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 440 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 441 } 442 #else 443 #define DTRACE_TLS_THRKEY(where) { \ 444 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 445 uint_t intr = 0; \ 446 uint_t actv = _c->cpu_intr_actv; \ 447 for (; actv; actv >>= 1) \ 448 intr++; \ 449 ASSERT(intr < (1 << 3)); \ 450 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 451 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 452 } 453 #endif 454 455 #define DT_BSWAP_8(x) ((x) & 0xff) 456 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 457 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 458 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 459 460 #define DT_MASK_LO 0x00000000FFFFFFFFULL 461 462 #define DTRACE_STORE(type, tomax, offset, what) \ 463 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 464 465 #ifndef __x86 466 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 467 if (addr & (size - 1)) { \ 468 *flags |= CPU_DTRACE_BADALIGN; \ 469 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 470 return (0); \ 471 } 472 #else 473 #define DTRACE_ALIGNCHECK(addr, size, flags) 474 #endif 475 476 /* 477 * Test whether a range of memory starting at testaddr of size testsz falls 478 * within the range of memory described by addr, sz. We take care to avoid 479 * problems with overflow and underflow of the unsigned quantities, and 480 * disallow all negative sizes. Ranges of size 0 are allowed. 481 */ 482 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 483 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 484 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 485 (testaddr) + (testsz) >= (testaddr)) 486 487 #define DTRACE_RANGE_REMAIN(remp, addr, baseaddr, basesz) \ 488 do { \ 489 if ((remp) != NULL) { \ 490 *(remp) = (uintptr_t)(baseaddr) + (basesz) - (addr); \ 491 } \ 492 _NOTE(CONSTCOND) } while (0) 493 494 495 /* 496 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 497 * alloc_sz on the righthand side of the comparison in order to avoid overflow 498 * or underflow in the comparison with it. This is simpler than the INRANGE 499 * check above, because we know that the dtms_scratch_ptr is valid in the 500 * range. Allocations of size zero are allowed. 501 */ 502 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 503 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 504 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 505 506 #define DTRACE_LOADFUNC(bits) \ 507 /*CSTYLED*/ \ 508 uint##bits##_t \ 509 dtrace_load##bits(uintptr_t addr) \ 510 { \ 511 size_t size = bits / NBBY; \ 512 /*CSTYLED*/ \ 513 uint##bits##_t rval; \ 514 int i; \ 515 volatile uint16_t *flags = (volatile uint16_t *) \ 516 &cpu_core[curcpu].cpuc_dtrace_flags; \ 517 \ 518 DTRACE_ALIGNCHECK(addr, size, flags); \ 519 \ 520 for (i = 0; i < dtrace_toxranges; i++) { \ 521 if (addr >= dtrace_toxrange[i].dtt_limit) \ 522 continue; \ 523 \ 524 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 525 continue; \ 526 \ 527 /* \ 528 * This address falls within a toxic region; return 0. \ 529 */ \ 530 *flags |= CPU_DTRACE_BADADDR; \ 531 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 532 return (0); \ 533 } \ 534 \ 535 *flags |= CPU_DTRACE_NOFAULT; \ 536 /*CSTYLED*/ \ 537 rval = *((volatile uint##bits##_t *)addr); \ 538 *flags &= ~CPU_DTRACE_NOFAULT; \ 539 \ 540 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 541 } 542 543 #ifdef _LP64 544 #define dtrace_loadptr dtrace_load64 545 #else 546 #define dtrace_loadptr dtrace_load32 547 #endif 548 549 #define DTRACE_DYNHASH_FREE 0 550 #define DTRACE_DYNHASH_SINK 1 551 #define DTRACE_DYNHASH_VALID 2 552 553 #define DTRACE_MATCH_NEXT 0 554 #define DTRACE_MATCH_DONE 1 555 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 556 #define DTRACE_STATE_ALIGN 64 557 558 #define DTRACE_FLAGS2FLT(flags) \ 559 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 560 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 561 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 562 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 563 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 564 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 565 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 566 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 567 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 568 DTRACEFLT_UNKNOWN) 569 570 #define DTRACEACT_ISSTRING(act) \ 571 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 572 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 573 574 /* Function prototype definitions: */ 575 static size_t dtrace_strlen(const char *, size_t); 576 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 577 static void dtrace_enabling_provide(dtrace_provider_t *); 578 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 579 static void dtrace_enabling_matchall(void); 580 static void dtrace_enabling_reap(void); 581 static dtrace_state_t *dtrace_anon_grab(void); 582 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 583 dtrace_state_t *, uint64_t, uint64_t); 584 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 585 static void dtrace_buffer_drop(dtrace_buffer_t *); 586 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 587 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 588 dtrace_state_t *, dtrace_mstate_t *); 589 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 590 dtrace_optval_t); 591 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 592 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 593 uint16_t dtrace_load16(uintptr_t); 594 uint32_t dtrace_load32(uintptr_t); 595 uint64_t dtrace_load64(uintptr_t); 596 uint8_t dtrace_load8(uintptr_t); 597 void dtrace_dynvar_clean(dtrace_dstate_t *); 598 dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 599 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 600 uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 601 static int dtrace_priv_proc(dtrace_state_t *); 602 static void dtrace_getf_barrier(void); 603 static int dtrace_canload_remains(uint64_t, size_t, size_t *, 604 dtrace_mstate_t *, dtrace_vstate_t *); 605 static int dtrace_canstore_remains(uint64_t, size_t, size_t *, 606 dtrace_mstate_t *, dtrace_vstate_t *); 607 608 /* 609 * DTrace Probe Context Functions 610 * 611 * These functions are called from probe context. Because probe context is 612 * any context in which C may be called, arbitrarily locks may be held, 613 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 614 * As a result, functions called from probe context may only call other DTrace 615 * support functions -- they may not interact at all with the system at large. 616 * (Note that the ASSERT macro is made probe-context safe by redefining it in 617 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 618 * loads are to be performed from probe context, they _must_ be in terms of 619 * the safe dtrace_load*() variants. 620 * 621 * Some functions in this block are not actually called from probe context; 622 * for these functions, there will be a comment above the function reading 623 * "Note: not called from probe context." 624 */ 625 void 626 dtrace_panic(const char *format, ...) 627 { 628 va_list alist; 629 630 va_start(alist, format); 631 #ifdef __FreeBSD__ 632 vpanic(format, alist); 633 #else 634 dtrace_vpanic(format, alist); 635 #endif 636 va_end(alist); 637 } 638 639 int 640 dtrace_assfail(const char *a, const char *f, int l) 641 { 642 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 643 644 /* 645 * We just need something here that even the most clever compiler 646 * cannot optimize away. 647 */ 648 return (a[(uintptr_t)f]); 649 } 650 651 /* 652 * Atomically increment a specified error counter from probe context. 653 */ 654 static void 655 dtrace_error(uint32_t *counter) 656 { 657 /* 658 * Most counters stored to in probe context are per-CPU counters. 659 * However, there are some error conditions that are sufficiently 660 * arcane that they don't merit per-CPU storage. If these counters 661 * are incremented concurrently on different CPUs, scalability will be 662 * adversely affected -- but we don't expect them to be white-hot in a 663 * correctly constructed enabling... 664 */ 665 uint32_t oval, nval; 666 667 do { 668 oval = *counter; 669 670 if ((nval = oval + 1) == 0) { 671 /* 672 * If the counter would wrap, set it to 1 -- assuring 673 * that the counter is never zero when we have seen 674 * errors. (The counter must be 32-bits because we 675 * aren't guaranteed a 64-bit compare&swap operation.) 676 * To save this code both the infamy of being fingered 677 * by a priggish news story and the indignity of being 678 * the target of a neo-puritan witch trial, we're 679 * carefully avoiding any colorful description of the 680 * likelihood of this condition -- but suffice it to 681 * say that it is only slightly more likely than the 682 * overflow of predicate cache IDs, as discussed in 683 * dtrace_predicate_create(). 684 */ 685 nval = 1; 686 } 687 } while (dtrace_cas32(counter, oval, nval) != oval); 688 } 689 690 /* 691 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 692 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 693 */ 694 /* BEGIN CSTYLED */ 695 DTRACE_LOADFUNC(8) 696 DTRACE_LOADFUNC(16) 697 DTRACE_LOADFUNC(32) 698 DTRACE_LOADFUNC(64) 699 /* END CSTYLED */ 700 701 static int 702 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 703 { 704 if (dest < mstate->dtms_scratch_base) 705 return (0); 706 707 if (dest + size < dest) 708 return (0); 709 710 if (dest + size > mstate->dtms_scratch_ptr) 711 return (0); 712 713 return (1); 714 } 715 716 static int 717 dtrace_canstore_statvar(uint64_t addr, size_t sz, size_t *remain, 718 dtrace_statvar_t **svars, int nsvars) 719 { 720 int i; 721 size_t maxglobalsize, maxlocalsize; 722 723 if (nsvars == 0) 724 return (0); 725 726 maxglobalsize = dtrace_statvar_maxsize + sizeof (uint64_t); 727 maxlocalsize = maxglobalsize * NCPU; 728 729 for (i = 0; i < nsvars; i++) { 730 dtrace_statvar_t *svar = svars[i]; 731 uint8_t scope; 732 size_t size; 733 734 if (svar == NULL || (size = svar->dtsv_size) == 0) 735 continue; 736 737 scope = svar->dtsv_var.dtdv_scope; 738 739 /* 740 * We verify that our size is valid in the spirit of providing 741 * defense in depth: we want to prevent attackers from using 742 * DTrace to escalate an orthogonal kernel heap corruption bug 743 * into the ability to store to arbitrary locations in memory. 744 */ 745 VERIFY((scope == DIFV_SCOPE_GLOBAL && size <= maxglobalsize) || 746 (scope == DIFV_SCOPE_LOCAL && size <= maxlocalsize)); 747 748 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, 749 svar->dtsv_size)) { 750 DTRACE_RANGE_REMAIN(remain, addr, svar->dtsv_data, 751 svar->dtsv_size); 752 return (1); 753 } 754 } 755 756 return (0); 757 } 758 759 /* 760 * Check to see if the address is within a memory region to which a store may 761 * be issued. This includes the DTrace scratch areas, and any DTrace variable 762 * region. The caller of dtrace_canstore() is responsible for performing any 763 * alignment checks that are needed before stores are actually executed. 764 */ 765 static int 766 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 767 dtrace_vstate_t *vstate) 768 { 769 return (dtrace_canstore_remains(addr, sz, NULL, mstate, vstate)); 770 } 771 772 /* 773 * Implementation of dtrace_canstore which communicates the upper bound of the 774 * allowed memory region. 775 */ 776 static int 777 dtrace_canstore_remains(uint64_t addr, size_t sz, size_t *remain, 778 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 779 { 780 /* 781 * First, check to see if the address is in scratch space... 782 */ 783 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 784 mstate->dtms_scratch_size)) { 785 DTRACE_RANGE_REMAIN(remain, addr, mstate->dtms_scratch_base, 786 mstate->dtms_scratch_size); 787 return (1); 788 } 789 790 /* 791 * Now check to see if it's a dynamic variable. This check will pick 792 * up both thread-local variables and any global dynamically-allocated 793 * variables. 794 */ 795 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 796 vstate->dtvs_dynvars.dtds_size)) { 797 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 798 uintptr_t base = (uintptr_t)dstate->dtds_base + 799 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 800 uintptr_t chunkoffs; 801 dtrace_dynvar_t *dvar; 802 803 /* 804 * Before we assume that we can store here, we need to make 805 * sure that it isn't in our metadata -- storing to our 806 * dynamic variable metadata would corrupt our state. For 807 * the range to not include any dynamic variable metadata, 808 * it must: 809 * 810 * (1) Start above the hash table that is at the base of 811 * the dynamic variable space 812 * 813 * (2) Have a starting chunk offset that is beyond the 814 * dtrace_dynvar_t that is at the base of every chunk 815 * 816 * (3) Not span a chunk boundary 817 * 818 * (4) Not be in the tuple space of a dynamic variable 819 * 820 */ 821 if (addr < base) 822 return (0); 823 824 chunkoffs = (addr - base) % dstate->dtds_chunksize; 825 826 if (chunkoffs < sizeof (dtrace_dynvar_t)) 827 return (0); 828 829 if (chunkoffs + sz > dstate->dtds_chunksize) 830 return (0); 831 832 dvar = (dtrace_dynvar_t *)((uintptr_t)addr - chunkoffs); 833 834 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) 835 return (0); 836 837 if (chunkoffs < sizeof (dtrace_dynvar_t) + 838 ((dvar->dtdv_tuple.dtt_nkeys - 1) * sizeof (dtrace_key_t))) 839 return (0); 840 841 DTRACE_RANGE_REMAIN(remain, addr, dvar, dstate->dtds_chunksize); 842 return (1); 843 } 844 845 /* 846 * Finally, check the static local and global variables. These checks 847 * take the longest, so we perform them last. 848 */ 849 if (dtrace_canstore_statvar(addr, sz, remain, 850 vstate->dtvs_locals, vstate->dtvs_nlocals)) 851 return (1); 852 853 if (dtrace_canstore_statvar(addr, sz, remain, 854 vstate->dtvs_globals, vstate->dtvs_nglobals)) 855 return (1); 856 857 return (0); 858 } 859 860 861 /* 862 * Convenience routine to check to see if the address is within a memory 863 * region in which a load may be issued given the user's privilege level; 864 * if not, it sets the appropriate error flags and loads 'addr' into the 865 * illegal value slot. 866 * 867 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 868 * appropriate memory access protection. 869 */ 870 static int 871 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 872 dtrace_vstate_t *vstate) 873 { 874 return (dtrace_canload_remains(addr, sz, NULL, mstate, vstate)); 875 } 876 877 /* 878 * Implementation of dtrace_canload which communicates the uppoer bound of the 879 * allowed memory region. 880 */ 881 static int 882 dtrace_canload_remains(uint64_t addr, size_t sz, size_t *remain, 883 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 884 { 885 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 886 file_t *fp; 887 888 /* 889 * If we hold the privilege to read from kernel memory, then 890 * everything is readable. 891 */ 892 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 893 DTRACE_RANGE_REMAIN(remain, addr, addr, sz); 894 return (1); 895 } 896 897 /* 898 * You can obviously read that which you can store. 899 */ 900 if (dtrace_canstore_remains(addr, sz, remain, mstate, vstate)) 901 return (1); 902 903 /* 904 * We're allowed to read from our own string table. 905 */ 906 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 907 mstate->dtms_difo->dtdo_strlen)) { 908 DTRACE_RANGE_REMAIN(remain, addr, 909 mstate->dtms_difo->dtdo_strtab, 910 mstate->dtms_difo->dtdo_strlen); 911 return (1); 912 } 913 914 if (vstate->dtvs_state != NULL && 915 dtrace_priv_proc(vstate->dtvs_state)) { 916 proc_t *p; 917 918 /* 919 * When we have privileges to the current process, there are 920 * several context-related kernel structures that are safe to 921 * read, even absent the privilege to read from kernel memory. 922 * These reads are safe because these structures contain only 923 * state that (1) we're permitted to read, (2) is harmless or 924 * (3) contains pointers to additional kernel state that we're 925 * not permitted to read (and as such, do not present an 926 * opportunity for privilege escalation). Finally (and 927 * critically), because of the nature of their relation with 928 * the current thread context, the memory associated with these 929 * structures cannot change over the duration of probe context, 930 * and it is therefore impossible for this memory to be 931 * deallocated and reallocated as something else while it's 932 * being operated upon. 933 */ 934 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) { 935 DTRACE_RANGE_REMAIN(remain, addr, curthread, 936 sizeof (kthread_t)); 937 return (1); 938 } 939 940 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 941 sz, curthread->t_procp, sizeof (proc_t))) { 942 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_procp, 943 sizeof (proc_t)); 944 return (1); 945 } 946 947 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 948 curthread->t_cred, sizeof (cred_t))) { 949 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cred, 950 sizeof (cred_t)); 951 return (1); 952 } 953 954 #ifdef illumos 955 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 956 &(p->p_pidp->pid_id), sizeof (pid_t))) { 957 DTRACE_RANGE_REMAIN(remain, addr, &(p->p_pidp->pid_id), 958 sizeof (pid_t)); 959 return (1); 960 } 961 962 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 963 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 964 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cpu, 965 offsetof(cpu_t, cpu_pause_thread)); 966 return (1); 967 } 968 #endif 969 } 970 971 if ((fp = mstate->dtms_getf) != NULL) { 972 uintptr_t psz = sizeof (void *); 973 vnode_t *vp; 974 vnodeops_t *op; 975 976 /* 977 * When getf() returns a file_t, the enabling is implicitly 978 * granted the (transient) right to read the returned file_t 979 * as well as the v_path and v_op->vnop_name of the underlying 980 * vnode. These accesses are allowed after a successful 981 * getf() because the members that they refer to cannot change 982 * once set -- and the barrier logic in the kernel's closef() 983 * path assures that the file_t and its referenced vode_t 984 * cannot themselves be stale (that is, it impossible for 985 * either dtms_getf itself or its f_vnode member to reference 986 * freed memory). 987 */ 988 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) { 989 DTRACE_RANGE_REMAIN(remain, addr, fp, sizeof (file_t)); 990 return (1); 991 } 992 993 if ((vp = fp->f_vnode) != NULL) { 994 size_t slen; 995 #ifdef illumos 996 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) { 997 DTRACE_RANGE_REMAIN(remain, addr, &vp->v_path, 998 psz); 999 return (1); 1000 } 1001 slen = strlen(vp->v_path) + 1; 1002 if (DTRACE_INRANGE(addr, sz, vp->v_path, slen)) { 1003 DTRACE_RANGE_REMAIN(remain, addr, vp->v_path, 1004 slen); 1005 return (1); 1006 } 1007 #endif 1008 1009 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) { 1010 DTRACE_RANGE_REMAIN(remain, addr, &vp->v_op, 1011 psz); 1012 return (1); 1013 } 1014 1015 #ifdef illumos 1016 if ((op = vp->v_op) != NULL && 1017 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 1018 DTRACE_RANGE_REMAIN(remain, addr, 1019 &op->vnop_name, psz); 1020 return (1); 1021 } 1022 1023 if (op != NULL && op->vnop_name != NULL && 1024 DTRACE_INRANGE(addr, sz, op->vnop_name, 1025 (slen = strlen(op->vnop_name) + 1))) { 1026 DTRACE_RANGE_REMAIN(remain, addr, 1027 op->vnop_name, slen); 1028 return (1); 1029 } 1030 #endif 1031 } 1032 } 1033 1034 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 1035 *illval = addr; 1036 return (0); 1037 } 1038 1039 /* 1040 * Convenience routine to check to see if a given string is within a memory 1041 * region in which a load may be issued given the user's privilege level; 1042 * this exists so that we don't need to issue unnecessary dtrace_strlen() 1043 * calls in the event that the user has all privileges. 1044 */ 1045 static int 1046 dtrace_strcanload(uint64_t addr, size_t sz, size_t *remain, 1047 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1048 { 1049 size_t rsize; 1050 1051 /* 1052 * If we hold the privilege to read from kernel memory, then 1053 * everything is readable. 1054 */ 1055 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 1056 DTRACE_RANGE_REMAIN(remain, addr, addr, sz); 1057 return (1); 1058 } 1059 1060 /* 1061 * Even if the caller is uninterested in querying the remaining valid 1062 * range, it is required to ensure that the access is allowed. 1063 */ 1064 if (remain == NULL) { 1065 remain = &rsize; 1066 } 1067 if (dtrace_canload_remains(addr, 0, remain, mstate, vstate)) { 1068 size_t strsz; 1069 /* 1070 * Perform the strlen after determining the length of the 1071 * memory region which is accessible. This prevents timing 1072 * information from being used to find NULs in memory which is 1073 * not accessible to the caller. 1074 */ 1075 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, 1076 MIN(sz, *remain)); 1077 if (strsz <= *remain) { 1078 return (1); 1079 } 1080 } 1081 1082 return (0); 1083 } 1084 1085 /* 1086 * Convenience routine to check to see if a given variable is within a memory 1087 * region in which a load may be issued given the user's privilege level. 1088 */ 1089 static int 1090 dtrace_vcanload(void *src, dtrace_diftype_t *type, size_t *remain, 1091 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1092 { 1093 size_t sz; 1094 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1095 1096 /* 1097 * Calculate the max size before performing any checks since even 1098 * DTRACE_ACCESS_KERNEL-credentialed callers expect that this function 1099 * return the max length via 'remain'. 1100 */ 1101 if (type->dtdt_kind == DIF_TYPE_STRING) { 1102 dtrace_state_t *state = vstate->dtvs_state; 1103 1104 if (state != NULL) { 1105 sz = state->dts_options[DTRACEOPT_STRSIZE]; 1106 } else { 1107 /* 1108 * In helper context, we have a NULL state; fall back 1109 * to using the system-wide default for the string size 1110 * in this case. 1111 */ 1112 sz = dtrace_strsize_default; 1113 } 1114 } else { 1115 sz = type->dtdt_size; 1116 } 1117 1118 /* 1119 * If we hold the privilege to read from kernel memory, then 1120 * everything is readable. 1121 */ 1122 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 1123 DTRACE_RANGE_REMAIN(remain, (uintptr_t)src, src, sz); 1124 return (1); 1125 } 1126 1127 if (type->dtdt_kind == DIF_TYPE_STRING) { 1128 return (dtrace_strcanload((uintptr_t)src, sz, remain, mstate, 1129 vstate)); 1130 } 1131 return (dtrace_canload_remains((uintptr_t)src, sz, remain, mstate, 1132 vstate)); 1133 } 1134 1135 /* 1136 * Convert a string to a signed integer using safe loads. 1137 * 1138 * NOTE: This function uses various macros from strtolctype.h to manipulate 1139 * digit values, etc -- these have all been checked to ensure they make 1140 * no additional function calls. 1141 */ 1142 static int64_t 1143 dtrace_strtoll(char *input, int base, size_t limit) 1144 { 1145 uintptr_t pos = (uintptr_t)input; 1146 int64_t val = 0; 1147 int x; 1148 boolean_t neg = B_FALSE; 1149 char c, cc, ccc; 1150 uintptr_t end = pos + limit; 1151 1152 /* 1153 * Consume any whitespace preceding digits. 1154 */ 1155 while ((c = dtrace_load8(pos)) == ' ' || c == '\t') 1156 pos++; 1157 1158 /* 1159 * Handle an explicit sign if one is present. 1160 */ 1161 if (c == '-' || c == '+') { 1162 if (c == '-') 1163 neg = B_TRUE; 1164 c = dtrace_load8(++pos); 1165 } 1166 1167 /* 1168 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it 1169 * if present. 1170 */ 1171 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' || 1172 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) { 1173 pos += 2; 1174 c = ccc; 1175 } 1176 1177 /* 1178 * Read in contiguous digits until the first non-digit character. 1179 */ 1180 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base; 1181 c = dtrace_load8(++pos)) 1182 val = val * base + x; 1183 1184 return (neg ? -val : val); 1185 } 1186 1187 /* 1188 * Compare two strings using safe loads. 1189 */ 1190 static int 1191 dtrace_strncmp(char *s1, char *s2, size_t limit) 1192 { 1193 uint8_t c1, c2; 1194 volatile uint16_t *flags; 1195 1196 if (s1 == s2 || limit == 0) 1197 return (0); 1198 1199 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1200 1201 do { 1202 if (s1 == NULL) { 1203 c1 = '\0'; 1204 } else { 1205 c1 = dtrace_load8((uintptr_t)s1++); 1206 } 1207 1208 if (s2 == NULL) { 1209 c2 = '\0'; 1210 } else { 1211 c2 = dtrace_load8((uintptr_t)s2++); 1212 } 1213 1214 if (c1 != c2) 1215 return (c1 - c2); 1216 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 1217 1218 return (0); 1219 } 1220 1221 /* 1222 * Compute strlen(s) for a string using safe memory accesses. The additional 1223 * len parameter is used to specify a maximum length to ensure completion. 1224 */ 1225 static size_t 1226 dtrace_strlen(const char *s, size_t lim) 1227 { 1228 uint_t len; 1229 1230 for (len = 0; len != lim; len++) { 1231 if (dtrace_load8((uintptr_t)s++) == '\0') 1232 break; 1233 } 1234 1235 return (len); 1236 } 1237 1238 /* 1239 * Check if an address falls within a toxic region. 1240 */ 1241 static int 1242 dtrace_istoxic(uintptr_t kaddr, size_t size) 1243 { 1244 uintptr_t taddr, tsize; 1245 int i; 1246 1247 for (i = 0; i < dtrace_toxranges; i++) { 1248 taddr = dtrace_toxrange[i].dtt_base; 1249 tsize = dtrace_toxrange[i].dtt_limit - taddr; 1250 1251 if (kaddr - taddr < tsize) { 1252 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1253 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 1254 return (1); 1255 } 1256 1257 if (taddr - kaddr < size) { 1258 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1259 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 1260 return (1); 1261 } 1262 } 1263 1264 return (0); 1265 } 1266 1267 /* 1268 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 1269 * memory specified by the DIF program. The dst is assumed to be safe memory 1270 * that we can store to directly because it is managed by DTrace. As with 1271 * standard bcopy, overlapping copies are handled properly. 1272 */ 1273 static void 1274 dtrace_bcopy(const void *src, void *dst, size_t len) 1275 { 1276 if (len != 0) { 1277 uint8_t *s1 = dst; 1278 const uint8_t *s2 = src; 1279 1280 if (s1 <= s2) { 1281 do { 1282 *s1++ = dtrace_load8((uintptr_t)s2++); 1283 } while (--len != 0); 1284 } else { 1285 s2 += len; 1286 s1 += len; 1287 1288 do { 1289 *--s1 = dtrace_load8((uintptr_t)--s2); 1290 } while (--len != 0); 1291 } 1292 } 1293 } 1294 1295 /* 1296 * Copy src to dst using safe memory accesses, up to either the specified 1297 * length, or the point that a nul byte is encountered. The src is assumed to 1298 * be unsafe memory specified by the DIF program. The dst is assumed to be 1299 * safe memory that we can store to directly because it is managed by DTrace. 1300 * Unlike dtrace_bcopy(), overlapping regions are not handled. 1301 */ 1302 static void 1303 dtrace_strcpy(const void *src, void *dst, size_t len) 1304 { 1305 if (len != 0) { 1306 uint8_t *s1 = dst, c; 1307 const uint8_t *s2 = src; 1308 1309 do { 1310 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1311 } while (--len != 0 && c != '\0'); 1312 } 1313 } 1314 1315 /* 1316 * Copy src to dst, deriving the size and type from the specified (BYREF) 1317 * variable type. The src is assumed to be unsafe memory specified by the DIF 1318 * program. The dst is assumed to be DTrace variable memory that is of the 1319 * specified type; we assume that we can store to directly. 1320 */ 1321 static void 1322 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type, size_t limit) 1323 { 1324 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1325 1326 if (type->dtdt_kind == DIF_TYPE_STRING) { 1327 dtrace_strcpy(src, dst, MIN(type->dtdt_size, limit)); 1328 } else { 1329 dtrace_bcopy(src, dst, MIN(type->dtdt_size, limit)); 1330 } 1331 } 1332 1333 /* 1334 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1335 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1336 * safe memory that we can access directly because it is managed by DTrace. 1337 */ 1338 static int 1339 dtrace_bcmp(const void *s1, const void *s2, size_t len) 1340 { 1341 volatile uint16_t *flags; 1342 1343 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1344 1345 if (s1 == s2) 1346 return (0); 1347 1348 if (s1 == NULL || s2 == NULL) 1349 return (1); 1350 1351 if (s1 != s2 && len != 0) { 1352 const uint8_t *ps1 = s1; 1353 const uint8_t *ps2 = s2; 1354 1355 do { 1356 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1357 return (1); 1358 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1359 } 1360 return (0); 1361 } 1362 1363 /* 1364 * Zero the specified region using a simple byte-by-byte loop. Note that this 1365 * is for safe DTrace-managed memory only. 1366 */ 1367 static void 1368 dtrace_bzero(void *dst, size_t len) 1369 { 1370 uchar_t *cp; 1371 1372 for (cp = dst; len != 0; len--) 1373 *cp++ = 0; 1374 } 1375 1376 static void 1377 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1378 { 1379 uint64_t result[2]; 1380 1381 result[0] = addend1[0] + addend2[0]; 1382 result[1] = addend1[1] + addend2[1] + 1383 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1384 1385 sum[0] = result[0]; 1386 sum[1] = result[1]; 1387 } 1388 1389 /* 1390 * Shift the 128-bit value in a by b. If b is positive, shift left. 1391 * If b is negative, shift right. 1392 */ 1393 static void 1394 dtrace_shift_128(uint64_t *a, int b) 1395 { 1396 uint64_t mask; 1397 1398 if (b == 0) 1399 return; 1400 1401 if (b < 0) { 1402 b = -b; 1403 if (b >= 64) { 1404 a[0] = a[1] >> (b - 64); 1405 a[1] = 0; 1406 } else { 1407 a[0] >>= b; 1408 mask = 1LL << (64 - b); 1409 mask -= 1; 1410 a[0] |= ((a[1] & mask) << (64 - b)); 1411 a[1] >>= b; 1412 } 1413 } else { 1414 if (b >= 64) { 1415 a[1] = a[0] << (b - 64); 1416 a[0] = 0; 1417 } else { 1418 a[1] <<= b; 1419 mask = a[0] >> (64 - b); 1420 a[1] |= mask; 1421 a[0] <<= b; 1422 } 1423 } 1424 } 1425 1426 /* 1427 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1428 * use native multiplication on those, and then re-combine into the 1429 * resulting 128-bit value. 1430 * 1431 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1432 * hi1 * hi2 << 64 + 1433 * hi1 * lo2 << 32 + 1434 * hi2 * lo1 << 32 + 1435 * lo1 * lo2 1436 */ 1437 static void 1438 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1439 { 1440 uint64_t hi1, hi2, lo1, lo2; 1441 uint64_t tmp[2]; 1442 1443 hi1 = factor1 >> 32; 1444 hi2 = factor2 >> 32; 1445 1446 lo1 = factor1 & DT_MASK_LO; 1447 lo2 = factor2 & DT_MASK_LO; 1448 1449 product[0] = lo1 * lo2; 1450 product[1] = hi1 * hi2; 1451 1452 tmp[0] = hi1 * lo2; 1453 tmp[1] = 0; 1454 dtrace_shift_128(tmp, 32); 1455 dtrace_add_128(product, tmp, product); 1456 1457 tmp[0] = hi2 * lo1; 1458 tmp[1] = 0; 1459 dtrace_shift_128(tmp, 32); 1460 dtrace_add_128(product, tmp, product); 1461 } 1462 1463 /* 1464 * This privilege check should be used by actions and subroutines to 1465 * verify that the user credentials of the process that enabled the 1466 * invoking ECB match the target credentials 1467 */ 1468 static int 1469 dtrace_priv_proc_common_user(dtrace_state_t *state) 1470 { 1471 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1472 1473 /* 1474 * We should always have a non-NULL state cred here, since if cred 1475 * is null (anonymous tracing), we fast-path bypass this routine. 1476 */ 1477 ASSERT(s_cr != NULL); 1478 1479 if ((cr = CRED()) != NULL && 1480 s_cr->cr_uid == cr->cr_uid && 1481 s_cr->cr_uid == cr->cr_ruid && 1482 s_cr->cr_uid == cr->cr_suid && 1483 s_cr->cr_gid == cr->cr_gid && 1484 s_cr->cr_gid == cr->cr_rgid && 1485 s_cr->cr_gid == cr->cr_sgid) 1486 return (1); 1487 1488 return (0); 1489 } 1490 1491 /* 1492 * This privilege check should be used by actions and subroutines to 1493 * verify that the zone of the process that enabled the invoking ECB 1494 * matches the target credentials 1495 */ 1496 static int 1497 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1498 { 1499 #ifdef illumos 1500 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1501 1502 /* 1503 * We should always have a non-NULL state cred here, since if cred 1504 * is null (anonymous tracing), we fast-path bypass this routine. 1505 */ 1506 ASSERT(s_cr != NULL); 1507 1508 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1509 return (1); 1510 1511 return (0); 1512 #else 1513 return (1); 1514 #endif 1515 } 1516 1517 /* 1518 * This privilege check should be used by actions and subroutines to 1519 * verify that the process has not setuid or changed credentials. 1520 */ 1521 static int 1522 dtrace_priv_proc_common_nocd(void) 1523 { 1524 proc_t *proc; 1525 1526 if ((proc = ttoproc(curthread)) != NULL && 1527 !(proc->p_flag & SNOCD)) 1528 return (1); 1529 1530 return (0); 1531 } 1532 1533 static int 1534 dtrace_priv_proc_destructive(dtrace_state_t *state) 1535 { 1536 int action = state->dts_cred.dcr_action; 1537 1538 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1539 dtrace_priv_proc_common_zone(state) == 0) 1540 goto bad; 1541 1542 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1543 dtrace_priv_proc_common_user(state) == 0) 1544 goto bad; 1545 1546 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1547 dtrace_priv_proc_common_nocd() == 0) 1548 goto bad; 1549 1550 return (1); 1551 1552 bad: 1553 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1554 1555 return (0); 1556 } 1557 1558 static int 1559 dtrace_priv_proc_control(dtrace_state_t *state) 1560 { 1561 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1562 return (1); 1563 1564 if (dtrace_priv_proc_common_zone(state) && 1565 dtrace_priv_proc_common_user(state) && 1566 dtrace_priv_proc_common_nocd()) 1567 return (1); 1568 1569 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1570 1571 return (0); 1572 } 1573 1574 static int 1575 dtrace_priv_proc(dtrace_state_t *state) 1576 { 1577 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1578 return (1); 1579 1580 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1581 1582 return (0); 1583 } 1584 1585 static int 1586 dtrace_priv_kernel(dtrace_state_t *state) 1587 { 1588 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1589 return (1); 1590 1591 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1592 1593 return (0); 1594 } 1595 1596 static int 1597 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1598 { 1599 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1600 return (1); 1601 1602 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1603 1604 return (0); 1605 } 1606 1607 /* 1608 * Determine if the dte_cond of the specified ECB allows for processing of 1609 * the current probe to continue. Note that this routine may allow continued 1610 * processing, but with access(es) stripped from the mstate's dtms_access 1611 * field. 1612 */ 1613 static int 1614 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1615 dtrace_ecb_t *ecb) 1616 { 1617 dtrace_probe_t *probe = ecb->dte_probe; 1618 dtrace_provider_t *prov = probe->dtpr_provider; 1619 dtrace_pops_t *pops = &prov->dtpv_pops; 1620 int mode = DTRACE_MODE_NOPRIV_DROP; 1621 1622 ASSERT(ecb->dte_cond); 1623 1624 #ifdef illumos 1625 if (pops->dtps_mode != NULL) { 1626 mode = pops->dtps_mode(prov->dtpv_arg, 1627 probe->dtpr_id, probe->dtpr_arg); 1628 1629 ASSERT((mode & DTRACE_MODE_USER) || 1630 (mode & DTRACE_MODE_KERNEL)); 1631 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1632 (mode & DTRACE_MODE_NOPRIV_DROP)); 1633 } 1634 1635 /* 1636 * If the dte_cond bits indicate that this consumer is only allowed to 1637 * see user-mode firings of this probe, call the provider's dtps_mode() 1638 * entry point to check that the probe was fired while in a user 1639 * context. If that's not the case, use the policy specified by the 1640 * provider to determine if we drop the probe or merely restrict 1641 * operation. 1642 */ 1643 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1644 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1645 1646 if (!(mode & DTRACE_MODE_USER)) { 1647 if (mode & DTRACE_MODE_NOPRIV_DROP) 1648 return (0); 1649 1650 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1651 } 1652 } 1653 #endif 1654 1655 /* 1656 * This is more subtle than it looks. We have to be absolutely certain 1657 * that CRED() isn't going to change out from under us so it's only 1658 * legit to examine that structure if we're in constrained situations. 1659 * Currently, the only times we'll this check is if a non-super-user 1660 * has enabled the profile or syscall providers -- providers that 1661 * allow visibility of all processes. For the profile case, the check 1662 * above will ensure that we're examining a user context. 1663 */ 1664 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1665 cred_t *cr; 1666 cred_t *s_cr = state->dts_cred.dcr_cred; 1667 proc_t *proc; 1668 1669 ASSERT(s_cr != NULL); 1670 1671 if ((cr = CRED()) == NULL || 1672 s_cr->cr_uid != cr->cr_uid || 1673 s_cr->cr_uid != cr->cr_ruid || 1674 s_cr->cr_uid != cr->cr_suid || 1675 s_cr->cr_gid != cr->cr_gid || 1676 s_cr->cr_gid != cr->cr_rgid || 1677 s_cr->cr_gid != cr->cr_sgid || 1678 (proc = ttoproc(curthread)) == NULL || 1679 (proc->p_flag & SNOCD)) { 1680 if (mode & DTRACE_MODE_NOPRIV_DROP) 1681 return (0); 1682 1683 #ifdef illumos 1684 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1685 #endif 1686 } 1687 } 1688 1689 #ifdef illumos 1690 /* 1691 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1692 * in our zone, check to see if our mode policy is to restrict rather 1693 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1694 * and DTRACE_ACCESS_ARGS 1695 */ 1696 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1697 cred_t *cr; 1698 cred_t *s_cr = state->dts_cred.dcr_cred; 1699 1700 ASSERT(s_cr != NULL); 1701 1702 if ((cr = CRED()) == NULL || 1703 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1704 if (mode & DTRACE_MODE_NOPRIV_DROP) 1705 return (0); 1706 1707 mstate->dtms_access &= 1708 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1709 } 1710 } 1711 #endif 1712 1713 return (1); 1714 } 1715 1716 /* 1717 * Note: not called from probe context. This function is called 1718 * asynchronously (and at a regular interval) from outside of probe context to 1719 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1720 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1721 */ 1722 void 1723 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1724 { 1725 dtrace_dynvar_t *dirty; 1726 dtrace_dstate_percpu_t *dcpu; 1727 dtrace_dynvar_t **rinsep; 1728 int i, j, work = 0; 1729 1730 for (i = 0; i < NCPU; i++) { 1731 dcpu = &dstate->dtds_percpu[i]; 1732 rinsep = &dcpu->dtdsc_rinsing; 1733 1734 /* 1735 * If the dirty list is NULL, there is no dirty work to do. 1736 */ 1737 if (dcpu->dtdsc_dirty == NULL) 1738 continue; 1739 1740 if (dcpu->dtdsc_rinsing != NULL) { 1741 /* 1742 * If the rinsing list is non-NULL, then it is because 1743 * this CPU was selected to accept another CPU's 1744 * dirty list -- and since that time, dirty buffers 1745 * have accumulated. This is a highly unlikely 1746 * condition, but we choose to ignore the dirty 1747 * buffers -- they'll be picked up a future cleanse. 1748 */ 1749 continue; 1750 } 1751 1752 if (dcpu->dtdsc_clean != NULL) { 1753 /* 1754 * If the clean list is non-NULL, then we're in a 1755 * situation where a CPU has done deallocations (we 1756 * have a non-NULL dirty list) but no allocations (we 1757 * also have a non-NULL clean list). We can't simply 1758 * move the dirty list into the clean list on this 1759 * CPU, yet we also don't want to allow this condition 1760 * to persist, lest a short clean list prevent a 1761 * massive dirty list from being cleaned (which in 1762 * turn could lead to otherwise avoidable dynamic 1763 * drops). To deal with this, we look for some CPU 1764 * with a NULL clean list, NULL dirty list, and NULL 1765 * rinsing list -- and then we borrow this CPU to 1766 * rinse our dirty list. 1767 */ 1768 for (j = 0; j < NCPU; j++) { 1769 dtrace_dstate_percpu_t *rinser; 1770 1771 rinser = &dstate->dtds_percpu[j]; 1772 1773 if (rinser->dtdsc_rinsing != NULL) 1774 continue; 1775 1776 if (rinser->dtdsc_dirty != NULL) 1777 continue; 1778 1779 if (rinser->dtdsc_clean != NULL) 1780 continue; 1781 1782 rinsep = &rinser->dtdsc_rinsing; 1783 break; 1784 } 1785 1786 if (j == NCPU) { 1787 /* 1788 * We were unable to find another CPU that 1789 * could accept this dirty list -- we are 1790 * therefore unable to clean it now. 1791 */ 1792 dtrace_dynvar_failclean++; 1793 continue; 1794 } 1795 } 1796 1797 work = 1; 1798 1799 /* 1800 * Atomically move the dirty list aside. 1801 */ 1802 do { 1803 dirty = dcpu->dtdsc_dirty; 1804 1805 /* 1806 * Before we zap the dirty list, set the rinsing list. 1807 * (This allows for a potential assertion in 1808 * dtrace_dynvar(): if a free dynamic variable appears 1809 * on a hash chain, either the dirty list or the 1810 * rinsing list for some CPU must be non-NULL.) 1811 */ 1812 *rinsep = dirty; 1813 dtrace_membar_producer(); 1814 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1815 dirty, NULL) != dirty); 1816 } 1817 1818 if (!work) { 1819 /* 1820 * We have no work to do; we can simply return. 1821 */ 1822 return; 1823 } 1824 1825 dtrace_sync(); 1826 1827 for (i = 0; i < NCPU; i++) { 1828 dcpu = &dstate->dtds_percpu[i]; 1829 1830 if (dcpu->dtdsc_rinsing == NULL) 1831 continue; 1832 1833 /* 1834 * We are now guaranteed that no hash chain contains a pointer 1835 * into this dirty list; we can make it clean. 1836 */ 1837 ASSERT(dcpu->dtdsc_clean == NULL); 1838 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1839 dcpu->dtdsc_rinsing = NULL; 1840 } 1841 1842 /* 1843 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1844 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1845 * This prevents a race whereby a CPU incorrectly decides that 1846 * the state should be something other than DTRACE_DSTATE_CLEAN 1847 * after dtrace_dynvar_clean() has completed. 1848 */ 1849 dtrace_sync(); 1850 1851 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1852 } 1853 1854 /* 1855 * Depending on the value of the op parameter, this function looks-up, 1856 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1857 * allocation is requested, this function will return a pointer to a 1858 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1859 * variable can be allocated. If NULL is returned, the appropriate counter 1860 * will be incremented. 1861 */ 1862 dtrace_dynvar_t * 1863 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1864 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1865 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1866 { 1867 uint64_t hashval = DTRACE_DYNHASH_VALID; 1868 dtrace_dynhash_t *hash = dstate->dtds_hash; 1869 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1870 processorid_t me = curcpu, cpu = me; 1871 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1872 size_t bucket, ksize; 1873 size_t chunksize = dstate->dtds_chunksize; 1874 uintptr_t kdata, lock, nstate; 1875 uint_t i; 1876 1877 ASSERT(nkeys != 0); 1878 1879 /* 1880 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1881 * algorithm. For the by-value portions, we perform the algorithm in 1882 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1883 * bit, and seems to have only a minute effect on distribution. For 1884 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1885 * over each referenced byte. It's painful to do this, but it's much 1886 * better than pathological hash distribution. The efficacy of the 1887 * hashing algorithm (and a comparison with other algorithms) may be 1888 * found by running the ::dtrace_dynstat MDB dcmd. 1889 */ 1890 for (i = 0; i < nkeys; i++) { 1891 if (key[i].dttk_size == 0) { 1892 uint64_t val = key[i].dttk_value; 1893 1894 hashval += (val >> 48) & 0xffff; 1895 hashval += (hashval << 10); 1896 hashval ^= (hashval >> 6); 1897 1898 hashval += (val >> 32) & 0xffff; 1899 hashval += (hashval << 10); 1900 hashval ^= (hashval >> 6); 1901 1902 hashval += (val >> 16) & 0xffff; 1903 hashval += (hashval << 10); 1904 hashval ^= (hashval >> 6); 1905 1906 hashval += val & 0xffff; 1907 hashval += (hashval << 10); 1908 hashval ^= (hashval >> 6); 1909 } else { 1910 /* 1911 * This is incredibly painful, but it beats the hell 1912 * out of the alternative. 1913 */ 1914 uint64_t j, size = key[i].dttk_size; 1915 uintptr_t base = (uintptr_t)key[i].dttk_value; 1916 1917 if (!dtrace_canload(base, size, mstate, vstate)) 1918 break; 1919 1920 for (j = 0; j < size; j++) { 1921 hashval += dtrace_load8(base + j); 1922 hashval += (hashval << 10); 1923 hashval ^= (hashval >> 6); 1924 } 1925 } 1926 } 1927 1928 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1929 return (NULL); 1930 1931 hashval += (hashval << 3); 1932 hashval ^= (hashval >> 11); 1933 hashval += (hashval << 15); 1934 1935 /* 1936 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1937 * comes out to be one of our two sentinel hash values. If this 1938 * actually happens, we set the hashval to be a value known to be a 1939 * non-sentinel value. 1940 */ 1941 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1942 hashval = DTRACE_DYNHASH_VALID; 1943 1944 /* 1945 * Yes, it's painful to do a divide here. If the cycle count becomes 1946 * important here, tricks can be pulled to reduce it. (However, it's 1947 * critical that hash collisions be kept to an absolute minimum; 1948 * they're much more painful than a divide.) It's better to have a 1949 * solution that generates few collisions and still keeps things 1950 * relatively simple. 1951 */ 1952 bucket = hashval % dstate->dtds_hashsize; 1953 1954 if (op == DTRACE_DYNVAR_DEALLOC) { 1955 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1956 1957 for (;;) { 1958 while ((lock = *lockp) & 1) 1959 continue; 1960 1961 if (dtrace_casptr((volatile void *)lockp, 1962 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1963 break; 1964 } 1965 1966 dtrace_membar_producer(); 1967 } 1968 1969 top: 1970 prev = NULL; 1971 lock = hash[bucket].dtdh_lock; 1972 1973 dtrace_membar_consumer(); 1974 1975 start = hash[bucket].dtdh_chain; 1976 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1977 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1978 op != DTRACE_DYNVAR_DEALLOC)); 1979 1980 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1981 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1982 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1983 1984 if (dvar->dtdv_hashval != hashval) { 1985 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1986 /* 1987 * We've reached the sink, and therefore the 1988 * end of the hash chain; we can kick out of 1989 * the loop knowing that we have seen a valid 1990 * snapshot of state. 1991 */ 1992 ASSERT(dvar->dtdv_next == NULL); 1993 ASSERT(dvar == &dtrace_dynhash_sink); 1994 break; 1995 } 1996 1997 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1998 /* 1999 * We've gone off the rails: somewhere along 2000 * the line, one of the members of this hash 2001 * chain was deleted. Note that we could also 2002 * detect this by simply letting this loop run 2003 * to completion, as we would eventually hit 2004 * the end of the dirty list. However, we 2005 * want to avoid running the length of the 2006 * dirty list unnecessarily (it might be quite 2007 * long), so we catch this as early as 2008 * possible by detecting the hash marker. In 2009 * this case, we simply set dvar to NULL and 2010 * break; the conditional after the loop will 2011 * send us back to top. 2012 */ 2013 dvar = NULL; 2014 break; 2015 } 2016 2017 goto next; 2018 } 2019 2020 if (dtuple->dtt_nkeys != nkeys) 2021 goto next; 2022 2023 for (i = 0; i < nkeys; i++, dkey++) { 2024 if (dkey->dttk_size != key[i].dttk_size) 2025 goto next; /* size or type mismatch */ 2026 2027 if (dkey->dttk_size != 0) { 2028 if (dtrace_bcmp( 2029 (void *)(uintptr_t)key[i].dttk_value, 2030 (void *)(uintptr_t)dkey->dttk_value, 2031 dkey->dttk_size)) 2032 goto next; 2033 } else { 2034 if (dkey->dttk_value != key[i].dttk_value) 2035 goto next; 2036 } 2037 } 2038 2039 if (op != DTRACE_DYNVAR_DEALLOC) 2040 return (dvar); 2041 2042 ASSERT(dvar->dtdv_next == NULL || 2043 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 2044 2045 if (prev != NULL) { 2046 ASSERT(hash[bucket].dtdh_chain != dvar); 2047 ASSERT(start != dvar); 2048 ASSERT(prev->dtdv_next == dvar); 2049 prev->dtdv_next = dvar->dtdv_next; 2050 } else { 2051 if (dtrace_casptr(&hash[bucket].dtdh_chain, 2052 start, dvar->dtdv_next) != start) { 2053 /* 2054 * We have failed to atomically swing the 2055 * hash table head pointer, presumably because 2056 * of a conflicting allocation on another CPU. 2057 * We need to reread the hash chain and try 2058 * again. 2059 */ 2060 goto top; 2061 } 2062 } 2063 2064 dtrace_membar_producer(); 2065 2066 /* 2067 * Now set the hash value to indicate that it's free. 2068 */ 2069 ASSERT(hash[bucket].dtdh_chain != dvar); 2070 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2071 2072 dtrace_membar_producer(); 2073 2074 /* 2075 * Set the next pointer to point at the dirty list, and 2076 * atomically swing the dirty pointer to the newly freed dvar. 2077 */ 2078 do { 2079 next = dcpu->dtdsc_dirty; 2080 dvar->dtdv_next = next; 2081 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 2082 2083 /* 2084 * Finally, unlock this hash bucket. 2085 */ 2086 ASSERT(hash[bucket].dtdh_lock == lock); 2087 ASSERT(lock & 1); 2088 hash[bucket].dtdh_lock++; 2089 2090 return (NULL); 2091 next: 2092 prev = dvar; 2093 continue; 2094 } 2095 2096 if (dvar == NULL) { 2097 /* 2098 * If dvar is NULL, it is because we went off the rails: 2099 * one of the elements that we traversed in the hash chain 2100 * was deleted while we were traversing it. In this case, 2101 * we assert that we aren't doing a dealloc (deallocs lock 2102 * the hash bucket to prevent themselves from racing with 2103 * one another), and retry the hash chain traversal. 2104 */ 2105 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 2106 goto top; 2107 } 2108 2109 if (op != DTRACE_DYNVAR_ALLOC) { 2110 /* 2111 * If we are not to allocate a new variable, we want to 2112 * return NULL now. Before we return, check that the value 2113 * of the lock word hasn't changed. If it has, we may have 2114 * seen an inconsistent snapshot. 2115 */ 2116 if (op == DTRACE_DYNVAR_NOALLOC) { 2117 if (hash[bucket].dtdh_lock != lock) 2118 goto top; 2119 } else { 2120 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 2121 ASSERT(hash[bucket].dtdh_lock == lock); 2122 ASSERT(lock & 1); 2123 hash[bucket].dtdh_lock++; 2124 } 2125 2126 return (NULL); 2127 } 2128 2129 /* 2130 * We need to allocate a new dynamic variable. The size we need is the 2131 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 2132 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 2133 * the size of any referred-to data (dsize). We then round the final 2134 * size up to the chunksize for allocation. 2135 */ 2136 for (ksize = 0, i = 0; i < nkeys; i++) 2137 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 2138 2139 /* 2140 * This should be pretty much impossible, but could happen if, say, 2141 * strange DIF specified the tuple. Ideally, this should be an 2142 * assertion and not an error condition -- but that requires that the 2143 * chunksize calculation in dtrace_difo_chunksize() be absolutely 2144 * bullet-proof. (That is, it must not be able to be fooled by 2145 * malicious DIF.) Given the lack of backwards branches in DIF, 2146 * solving this would presumably not amount to solving the Halting 2147 * Problem -- but it still seems awfully hard. 2148 */ 2149 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 2150 ksize + dsize > chunksize) { 2151 dcpu->dtdsc_drops++; 2152 return (NULL); 2153 } 2154 2155 nstate = DTRACE_DSTATE_EMPTY; 2156 2157 do { 2158 retry: 2159 free = dcpu->dtdsc_free; 2160 2161 if (free == NULL) { 2162 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 2163 void *rval; 2164 2165 if (clean == NULL) { 2166 /* 2167 * We're out of dynamic variable space on 2168 * this CPU. Unless we have tried all CPUs, 2169 * we'll try to allocate from a different 2170 * CPU. 2171 */ 2172 switch (dstate->dtds_state) { 2173 case DTRACE_DSTATE_CLEAN: { 2174 void *sp = &dstate->dtds_state; 2175 2176 if (++cpu >= NCPU) 2177 cpu = 0; 2178 2179 if (dcpu->dtdsc_dirty != NULL && 2180 nstate == DTRACE_DSTATE_EMPTY) 2181 nstate = DTRACE_DSTATE_DIRTY; 2182 2183 if (dcpu->dtdsc_rinsing != NULL) 2184 nstate = DTRACE_DSTATE_RINSING; 2185 2186 dcpu = &dstate->dtds_percpu[cpu]; 2187 2188 if (cpu != me) 2189 goto retry; 2190 2191 (void) dtrace_cas32(sp, 2192 DTRACE_DSTATE_CLEAN, nstate); 2193 2194 /* 2195 * To increment the correct bean 2196 * counter, take another lap. 2197 */ 2198 goto retry; 2199 } 2200 2201 case DTRACE_DSTATE_DIRTY: 2202 dcpu->dtdsc_dirty_drops++; 2203 break; 2204 2205 case DTRACE_DSTATE_RINSING: 2206 dcpu->dtdsc_rinsing_drops++; 2207 break; 2208 2209 case DTRACE_DSTATE_EMPTY: 2210 dcpu->dtdsc_drops++; 2211 break; 2212 } 2213 2214 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 2215 return (NULL); 2216 } 2217 2218 /* 2219 * The clean list appears to be non-empty. We want to 2220 * move the clean list to the free list; we start by 2221 * moving the clean pointer aside. 2222 */ 2223 if (dtrace_casptr(&dcpu->dtdsc_clean, 2224 clean, NULL) != clean) { 2225 /* 2226 * We are in one of two situations: 2227 * 2228 * (a) The clean list was switched to the 2229 * free list by another CPU. 2230 * 2231 * (b) The clean list was added to by the 2232 * cleansing cyclic. 2233 * 2234 * In either of these situations, we can 2235 * just reattempt the free list allocation. 2236 */ 2237 goto retry; 2238 } 2239 2240 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 2241 2242 /* 2243 * Now we'll move the clean list to our free list. 2244 * It's impossible for this to fail: the only way 2245 * the free list can be updated is through this 2246 * code path, and only one CPU can own the clean list. 2247 * Thus, it would only be possible for this to fail if 2248 * this code were racing with dtrace_dynvar_clean(). 2249 * (That is, if dtrace_dynvar_clean() updated the clean 2250 * list, and we ended up racing to update the free 2251 * list.) This race is prevented by the dtrace_sync() 2252 * in dtrace_dynvar_clean() -- which flushes the 2253 * owners of the clean lists out before resetting 2254 * the clean lists. 2255 */ 2256 dcpu = &dstate->dtds_percpu[me]; 2257 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 2258 ASSERT(rval == NULL); 2259 goto retry; 2260 } 2261 2262 dvar = free; 2263 new_free = dvar->dtdv_next; 2264 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 2265 2266 /* 2267 * We have now allocated a new chunk. We copy the tuple keys into the 2268 * tuple array and copy any referenced key data into the data space 2269 * following the tuple array. As we do this, we relocate dttk_value 2270 * in the final tuple to point to the key data address in the chunk. 2271 */ 2272 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 2273 dvar->dtdv_data = (void *)(kdata + ksize); 2274 dvar->dtdv_tuple.dtt_nkeys = nkeys; 2275 2276 for (i = 0; i < nkeys; i++) { 2277 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 2278 size_t kesize = key[i].dttk_size; 2279 2280 if (kesize != 0) { 2281 dtrace_bcopy( 2282 (const void *)(uintptr_t)key[i].dttk_value, 2283 (void *)kdata, kesize); 2284 dkey->dttk_value = kdata; 2285 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 2286 } else { 2287 dkey->dttk_value = key[i].dttk_value; 2288 } 2289 2290 dkey->dttk_size = kesize; 2291 } 2292 2293 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 2294 dvar->dtdv_hashval = hashval; 2295 dvar->dtdv_next = start; 2296 2297 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 2298 return (dvar); 2299 2300 /* 2301 * The cas has failed. Either another CPU is adding an element to 2302 * this hash chain, or another CPU is deleting an element from this 2303 * hash chain. The simplest way to deal with both of these cases 2304 * (though not necessarily the most efficient) is to free our 2305 * allocated block and re-attempt it all. Note that the free is 2306 * to the dirty list and _not_ to the free list. This is to prevent 2307 * races with allocators, above. 2308 */ 2309 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2310 2311 dtrace_membar_producer(); 2312 2313 do { 2314 free = dcpu->dtdsc_dirty; 2315 dvar->dtdv_next = free; 2316 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 2317 2318 goto top; 2319 } 2320 2321 /*ARGSUSED*/ 2322 static void 2323 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 2324 { 2325 if ((int64_t)nval < (int64_t)*oval) 2326 *oval = nval; 2327 } 2328 2329 /*ARGSUSED*/ 2330 static void 2331 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2332 { 2333 if ((int64_t)nval > (int64_t)*oval) 2334 *oval = nval; 2335 } 2336 2337 static void 2338 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2339 { 2340 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2341 int64_t val = (int64_t)nval; 2342 2343 if (val < 0) { 2344 for (i = 0; i < zero; i++) { 2345 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2346 quanta[i] += incr; 2347 return; 2348 } 2349 } 2350 } else { 2351 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2352 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2353 quanta[i - 1] += incr; 2354 return; 2355 } 2356 } 2357 2358 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2359 return; 2360 } 2361 2362 ASSERT(0); 2363 } 2364 2365 static void 2366 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2367 { 2368 uint64_t arg = *lquanta++; 2369 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2370 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2371 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2372 int32_t val = (int32_t)nval, level; 2373 2374 ASSERT(step != 0); 2375 ASSERT(levels != 0); 2376 2377 if (val < base) { 2378 /* 2379 * This is an underflow. 2380 */ 2381 lquanta[0] += incr; 2382 return; 2383 } 2384 2385 level = (val - base) / step; 2386 2387 if (level < levels) { 2388 lquanta[level + 1] += incr; 2389 return; 2390 } 2391 2392 /* 2393 * This is an overflow. 2394 */ 2395 lquanta[levels + 1] += incr; 2396 } 2397 2398 static int 2399 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2400 uint16_t high, uint16_t nsteps, int64_t value) 2401 { 2402 int64_t this = 1, last, next; 2403 int base = 1, order; 2404 2405 ASSERT(factor <= nsteps); 2406 ASSERT(nsteps % factor == 0); 2407 2408 for (order = 0; order < low; order++) 2409 this *= factor; 2410 2411 /* 2412 * If our value is less than our factor taken to the power of the 2413 * low order of magnitude, it goes into the zeroth bucket. 2414 */ 2415 if (value < (last = this)) 2416 return (0); 2417 2418 for (this *= factor; order <= high; order++) { 2419 int nbuckets = this > nsteps ? nsteps : this; 2420 2421 if ((next = this * factor) < this) { 2422 /* 2423 * We should not generally get log/linear quantizations 2424 * with a high magnitude that allows 64-bits to 2425 * overflow, but we nonetheless protect against this 2426 * by explicitly checking for overflow, and clamping 2427 * our value accordingly. 2428 */ 2429 value = this - 1; 2430 } 2431 2432 if (value < this) { 2433 /* 2434 * If our value lies within this order of magnitude, 2435 * determine its position by taking the offset within 2436 * the order of magnitude, dividing by the bucket 2437 * width, and adding to our (accumulated) base. 2438 */ 2439 return (base + (value - last) / (this / nbuckets)); 2440 } 2441 2442 base += nbuckets - (nbuckets / factor); 2443 last = this; 2444 this = next; 2445 } 2446 2447 /* 2448 * Our value is greater than or equal to our factor taken to the 2449 * power of one plus the high magnitude -- return the top bucket. 2450 */ 2451 return (base); 2452 } 2453 2454 static void 2455 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2456 { 2457 uint64_t arg = *llquanta++; 2458 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2459 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2460 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2461 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2462 2463 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2464 low, high, nsteps, nval)] += incr; 2465 } 2466 2467 /*ARGSUSED*/ 2468 static void 2469 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2470 { 2471 data[0]++; 2472 data[1] += nval; 2473 } 2474 2475 /*ARGSUSED*/ 2476 static void 2477 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2478 { 2479 int64_t snval = (int64_t)nval; 2480 uint64_t tmp[2]; 2481 2482 data[0]++; 2483 data[1] += nval; 2484 2485 /* 2486 * What we want to say here is: 2487 * 2488 * data[2] += nval * nval; 2489 * 2490 * But given that nval is 64-bit, we could easily overflow, so 2491 * we do this as 128-bit arithmetic. 2492 */ 2493 if (snval < 0) 2494 snval = -snval; 2495 2496 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2497 dtrace_add_128(data + 2, tmp, data + 2); 2498 } 2499 2500 /*ARGSUSED*/ 2501 static void 2502 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2503 { 2504 *oval = *oval + 1; 2505 } 2506 2507 /*ARGSUSED*/ 2508 static void 2509 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2510 { 2511 *oval += nval; 2512 } 2513 2514 /* 2515 * Aggregate given the tuple in the principal data buffer, and the aggregating 2516 * action denoted by the specified dtrace_aggregation_t. The aggregation 2517 * buffer is specified as the buf parameter. This routine does not return 2518 * failure; if there is no space in the aggregation buffer, the data will be 2519 * dropped, and a corresponding counter incremented. 2520 */ 2521 static void 2522 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2523 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2524 { 2525 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2526 uint32_t i, ndx, size, fsize; 2527 uint32_t align = sizeof (uint64_t) - 1; 2528 dtrace_aggbuffer_t *agb; 2529 dtrace_aggkey_t *key; 2530 uint32_t hashval = 0, limit, isstr; 2531 caddr_t tomax, data, kdata; 2532 dtrace_actkind_t action; 2533 dtrace_action_t *act; 2534 uintptr_t offs; 2535 2536 if (buf == NULL) 2537 return; 2538 2539 if (!agg->dtag_hasarg) { 2540 /* 2541 * Currently, only quantize() and lquantize() take additional 2542 * arguments, and they have the same semantics: an increment 2543 * value that defaults to 1 when not present. If additional 2544 * aggregating actions take arguments, the setting of the 2545 * default argument value will presumably have to become more 2546 * sophisticated... 2547 */ 2548 arg = 1; 2549 } 2550 2551 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2552 size = rec->dtrd_offset - agg->dtag_base; 2553 fsize = size + rec->dtrd_size; 2554 2555 ASSERT(dbuf->dtb_tomax != NULL); 2556 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2557 2558 if ((tomax = buf->dtb_tomax) == NULL) { 2559 dtrace_buffer_drop(buf); 2560 return; 2561 } 2562 2563 /* 2564 * The metastructure is always at the bottom of the buffer. 2565 */ 2566 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2567 sizeof (dtrace_aggbuffer_t)); 2568 2569 if (buf->dtb_offset == 0) { 2570 /* 2571 * We just kludge up approximately 1/8th of the size to be 2572 * buckets. If this guess ends up being routinely 2573 * off-the-mark, we may need to dynamically readjust this 2574 * based on past performance. 2575 */ 2576 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2577 2578 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2579 (uintptr_t)tomax || hashsize == 0) { 2580 /* 2581 * We've been given a ludicrously small buffer; 2582 * increment our drop count and leave. 2583 */ 2584 dtrace_buffer_drop(buf); 2585 return; 2586 } 2587 2588 /* 2589 * And now, a pathetic attempt to try to get a an odd (or 2590 * perchance, a prime) hash size for better hash distribution. 2591 */ 2592 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2593 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2594 2595 agb->dtagb_hashsize = hashsize; 2596 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2597 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2598 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2599 2600 for (i = 0; i < agb->dtagb_hashsize; i++) 2601 agb->dtagb_hash[i] = NULL; 2602 } 2603 2604 ASSERT(agg->dtag_first != NULL); 2605 ASSERT(agg->dtag_first->dta_intuple); 2606 2607 /* 2608 * Calculate the hash value based on the key. Note that we _don't_ 2609 * include the aggid in the hashing (but we will store it as part of 2610 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2611 * algorithm: a simple, quick algorithm that has no known funnels, and 2612 * gets good distribution in practice. The efficacy of the hashing 2613 * algorithm (and a comparison with other algorithms) may be found by 2614 * running the ::dtrace_aggstat MDB dcmd. 2615 */ 2616 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2617 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2618 limit = i + act->dta_rec.dtrd_size; 2619 ASSERT(limit <= size); 2620 isstr = DTRACEACT_ISSTRING(act); 2621 2622 for (; i < limit; i++) { 2623 hashval += data[i]; 2624 hashval += (hashval << 10); 2625 hashval ^= (hashval >> 6); 2626 2627 if (isstr && data[i] == '\0') 2628 break; 2629 } 2630 } 2631 2632 hashval += (hashval << 3); 2633 hashval ^= (hashval >> 11); 2634 hashval += (hashval << 15); 2635 2636 /* 2637 * Yes, the divide here is expensive -- but it's generally the least 2638 * of the performance issues given the amount of data that we iterate 2639 * over to compute hash values, compare data, etc. 2640 */ 2641 ndx = hashval % agb->dtagb_hashsize; 2642 2643 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2644 ASSERT((caddr_t)key >= tomax); 2645 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2646 2647 if (hashval != key->dtak_hashval || key->dtak_size != size) 2648 continue; 2649 2650 kdata = key->dtak_data; 2651 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2652 2653 for (act = agg->dtag_first; act->dta_intuple; 2654 act = act->dta_next) { 2655 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2656 limit = i + act->dta_rec.dtrd_size; 2657 ASSERT(limit <= size); 2658 isstr = DTRACEACT_ISSTRING(act); 2659 2660 for (; i < limit; i++) { 2661 if (kdata[i] != data[i]) 2662 goto next; 2663 2664 if (isstr && data[i] == '\0') 2665 break; 2666 } 2667 } 2668 2669 if (action != key->dtak_action) { 2670 /* 2671 * We are aggregating on the same value in the same 2672 * aggregation with two different aggregating actions. 2673 * (This should have been picked up in the compiler, 2674 * so we may be dealing with errant or devious DIF.) 2675 * This is an error condition; we indicate as much, 2676 * and return. 2677 */ 2678 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2679 return; 2680 } 2681 2682 /* 2683 * This is a hit: we need to apply the aggregator to 2684 * the value at this key. 2685 */ 2686 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2687 return; 2688 next: 2689 continue; 2690 } 2691 2692 /* 2693 * We didn't find it. We need to allocate some zero-filled space, 2694 * link it into the hash table appropriately, and apply the aggregator 2695 * to the (zero-filled) value. 2696 */ 2697 offs = buf->dtb_offset; 2698 while (offs & (align - 1)) 2699 offs += sizeof (uint32_t); 2700 2701 /* 2702 * If we don't have enough room to both allocate a new key _and_ 2703 * its associated data, increment the drop count and return. 2704 */ 2705 if ((uintptr_t)tomax + offs + fsize > 2706 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2707 dtrace_buffer_drop(buf); 2708 return; 2709 } 2710 2711 /*CONSTCOND*/ 2712 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2713 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2714 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2715 2716 key->dtak_data = kdata = tomax + offs; 2717 buf->dtb_offset = offs + fsize; 2718 2719 /* 2720 * Now copy the data across. 2721 */ 2722 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2723 2724 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2725 kdata[i] = data[i]; 2726 2727 /* 2728 * Because strings are not zeroed out by default, we need to iterate 2729 * looking for actions that store strings, and we need to explicitly 2730 * pad these strings out with zeroes. 2731 */ 2732 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2733 int nul; 2734 2735 if (!DTRACEACT_ISSTRING(act)) 2736 continue; 2737 2738 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2739 limit = i + act->dta_rec.dtrd_size; 2740 ASSERT(limit <= size); 2741 2742 for (nul = 0; i < limit; i++) { 2743 if (nul) { 2744 kdata[i] = '\0'; 2745 continue; 2746 } 2747 2748 if (data[i] != '\0') 2749 continue; 2750 2751 nul = 1; 2752 } 2753 } 2754 2755 for (i = size; i < fsize; i++) 2756 kdata[i] = 0; 2757 2758 key->dtak_hashval = hashval; 2759 key->dtak_size = size; 2760 key->dtak_action = action; 2761 key->dtak_next = agb->dtagb_hash[ndx]; 2762 agb->dtagb_hash[ndx] = key; 2763 2764 /* 2765 * Finally, apply the aggregator. 2766 */ 2767 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2768 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2769 } 2770 2771 /* 2772 * Given consumer state, this routine finds a speculation in the INACTIVE 2773 * state and transitions it into the ACTIVE state. If there is no speculation 2774 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2775 * incremented -- it is up to the caller to take appropriate action. 2776 */ 2777 static int 2778 dtrace_speculation(dtrace_state_t *state) 2779 { 2780 int i = 0; 2781 dtrace_speculation_state_t current; 2782 uint32_t *stat = &state->dts_speculations_unavail, count; 2783 2784 while (i < state->dts_nspeculations) { 2785 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2786 2787 current = spec->dtsp_state; 2788 2789 if (current != DTRACESPEC_INACTIVE) { 2790 if (current == DTRACESPEC_COMMITTINGMANY || 2791 current == DTRACESPEC_COMMITTING || 2792 current == DTRACESPEC_DISCARDING) 2793 stat = &state->dts_speculations_busy; 2794 i++; 2795 continue; 2796 } 2797 2798 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2799 current, DTRACESPEC_ACTIVE) == current) 2800 return (i + 1); 2801 } 2802 2803 /* 2804 * We couldn't find a speculation. If we found as much as a single 2805 * busy speculation buffer, we'll attribute this failure as "busy" 2806 * instead of "unavail". 2807 */ 2808 do { 2809 count = *stat; 2810 } while (dtrace_cas32(stat, count, count + 1) != count); 2811 2812 return (0); 2813 } 2814 2815 /* 2816 * This routine commits an active speculation. If the specified speculation 2817 * is not in a valid state to perform a commit(), this routine will silently do 2818 * nothing. The state of the specified speculation is transitioned according 2819 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2820 */ 2821 static void 2822 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2823 dtrace_specid_t which) 2824 { 2825 dtrace_speculation_t *spec; 2826 dtrace_buffer_t *src, *dest; 2827 uintptr_t daddr, saddr, dlimit, slimit; 2828 dtrace_speculation_state_t current, new = 0; 2829 intptr_t offs; 2830 uint64_t timestamp; 2831 2832 if (which == 0) 2833 return; 2834 2835 if (which > state->dts_nspeculations) { 2836 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2837 return; 2838 } 2839 2840 spec = &state->dts_speculations[which - 1]; 2841 src = &spec->dtsp_buffer[cpu]; 2842 dest = &state->dts_buffer[cpu]; 2843 2844 do { 2845 current = spec->dtsp_state; 2846 2847 if (current == DTRACESPEC_COMMITTINGMANY) 2848 break; 2849 2850 switch (current) { 2851 case DTRACESPEC_INACTIVE: 2852 case DTRACESPEC_DISCARDING: 2853 return; 2854 2855 case DTRACESPEC_COMMITTING: 2856 /* 2857 * This is only possible if we are (a) commit()'ing 2858 * without having done a prior speculate() on this CPU 2859 * and (b) racing with another commit() on a different 2860 * CPU. There's nothing to do -- we just assert that 2861 * our offset is 0. 2862 */ 2863 ASSERT(src->dtb_offset == 0); 2864 return; 2865 2866 case DTRACESPEC_ACTIVE: 2867 new = DTRACESPEC_COMMITTING; 2868 break; 2869 2870 case DTRACESPEC_ACTIVEONE: 2871 /* 2872 * This speculation is active on one CPU. If our 2873 * buffer offset is non-zero, we know that the one CPU 2874 * must be us. Otherwise, we are committing on a 2875 * different CPU from the speculate(), and we must 2876 * rely on being asynchronously cleaned. 2877 */ 2878 if (src->dtb_offset != 0) { 2879 new = DTRACESPEC_COMMITTING; 2880 break; 2881 } 2882 /*FALLTHROUGH*/ 2883 2884 case DTRACESPEC_ACTIVEMANY: 2885 new = DTRACESPEC_COMMITTINGMANY; 2886 break; 2887 2888 default: 2889 ASSERT(0); 2890 } 2891 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2892 current, new) != current); 2893 2894 /* 2895 * We have set the state to indicate that we are committing this 2896 * speculation. Now reserve the necessary space in the destination 2897 * buffer. 2898 */ 2899 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2900 sizeof (uint64_t), state, NULL)) < 0) { 2901 dtrace_buffer_drop(dest); 2902 goto out; 2903 } 2904 2905 /* 2906 * We have sufficient space to copy the speculative buffer into the 2907 * primary buffer. First, modify the speculative buffer, filling 2908 * in the timestamp of all entries with the current time. The data 2909 * must have the commit() time rather than the time it was traced, 2910 * so that all entries in the primary buffer are in timestamp order. 2911 */ 2912 timestamp = dtrace_gethrtime(); 2913 saddr = (uintptr_t)src->dtb_tomax; 2914 slimit = saddr + src->dtb_offset; 2915 while (saddr < slimit) { 2916 size_t size; 2917 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2918 2919 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2920 saddr += sizeof (dtrace_epid_t); 2921 continue; 2922 } 2923 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2924 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2925 2926 ASSERT3U(saddr + size, <=, slimit); 2927 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2928 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2929 2930 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2931 2932 saddr += size; 2933 } 2934 2935 /* 2936 * Copy the buffer across. (Note that this is a 2937 * highly subobtimal bcopy(); in the unlikely event that this becomes 2938 * a serious performance issue, a high-performance DTrace-specific 2939 * bcopy() should obviously be invented.) 2940 */ 2941 daddr = (uintptr_t)dest->dtb_tomax + offs; 2942 dlimit = daddr + src->dtb_offset; 2943 saddr = (uintptr_t)src->dtb_tomax; 2944 2945 /* 2946 * First, the aligned portion. 2947 */ 2948 while (dlimit - daddr >= sizeof (uint64_t)) { 2949 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2950 2951 daddr += sizeof (uint64_t); 2952 saddr += sizeof (uint64_t); 2953 } 2954 2955 /* 2956 * Now any left-over bit... 2957 */ 2958 while (dlimit - daddr) 2959 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2960 2961 /* 2962 * Finally, commit the reserved space in the destination buffer. 2963 */ 2964 dest->dtb_offset = offs + src->dtb_offset; 2965 2966 out: 2967 /* 2968 * If we're lucky enough to be the only active CPU on this speculation 2969 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2970 */ 2971 if (current == DTRACESPEC_ACTIVE || 2972 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2973 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2974 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2975 2976 ASSERT(rval == DTRACESPEC_COMMITTING); 2977 } 2978 2979 src->dtb_offset = 0; 2980 src->dtb_xamot_drops += src->dtb_drops; 2981 src->dtb_drops = 0; 2982 } 2983 2984 /* 2985 * This routine discards an active speculation. If the specified speculation 2986 * is not in a valid state to perform a discard(), this routine will silently 2987 * do nothing. The state of the specified speculation is transitioned 2988 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2989 */ 2990 static void 2991 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2992 dtrace_specid_t which) 2993 { 2994 dtrace_speculation_t *spec; 2995 dtrace_speculation_state_t current, new = 0; 2996 dtrace_buffer_t *buf; 2997 2998 if (which == 0) 2999 return; 3000 3001 if (which > state->dts_nspeculations) { 3002 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 3003 return; 3004 } 3005 3006 spec = &state->dts_speculations[which - 1]; 3007 buf = &spec->dtsp_buffer[cpu]; 3008 3009 do { 3010 current = spec->dtsp_state; 3011 3012 switch (current) { 3013 case DTRACESPEC_INACTIVE: 3014 case DTRACESPEC_COMMITTINGMANY: 3015 case DTRACESPEC_COMMITTING: 3016 case DTRACESPEC_DISCARDING: 3017 return; 3018 3019 case DTRACESPEC_ACTIVE: 3020 case DTRACESPEC_ACTIVEMANY: 3021 new = DTRACESPEC_DISCARDING; 3022 break; 3023 3024 case DTRACESPEC_ACTIVEONE: 3025 if (buf->dtb_offset != 0) { 3026 new = DTRACESPEC_INACTIVE; 3027 } else { 3028 new = DTRACESPEC_DISCARDING; 3029 } 3030 break; 3031 3032 default: 3033 ASSERT(0); 3034 } 3035 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3036 current, new) != current); 3037 3038 buf->dtb_offset = 0; 3039 buf->dtb_drops = 0; 3040 } 3041 3042 /* 3043 * Note: not called from probe context. This function is called 3044 * asynchronously from cross call context to clean any speculations that are 3045 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 3046 * transitioned back to the INACTIVE state until all CPUs have cleaned the 3047 * speculation. 3048 */ 3049 static void 3050 dtrace_speculation_clean_here(dtrace_state_t *state) 3051 { 3052 dtrace_icookie_t cookie; 3053 processorid_t cpu = curcpu; 3054 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 3055 dtrace_specid_t i; 3056 3057 cookie = dtrace_interrupt_disable(); 3058 3059 if (dest->dtb_tomax == NULL) { 3060 dtrace_interrupt_enable(cookie); 3061 return; 3062 } 3063 3064 for (i = 0; i < state->dts_nspeculations; i++) { 3065 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3066 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 3067 3068 if (src->dtb_tomax == NULL) 3069 continue; 3070 3071 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 3072 src->dtb_offset = 0; 3073 continue; 3074 } 3075 3076 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 3077 continue; 3078 3079 if (src->dtb_offset == 0) 3080 continue; 3081 3082 dtrace_speculation_commit(state, cpu, i + 1); 3083 } 3084 3085 dtrace_interrupt_enable(cookie); 3086 } 3087 3088 /* 3089 * Note: not called from probe context. This function is called 3090 * asynchronously (and at a regular interval) to clean any speculations that 3091 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 3092 * is work to be done, it cross calls all CPUs to perform that work; 3093 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 3094 * INACTIVE state until they have been cleaned by all CPUs. 3095 */ 3096 static void 3097 dtrace_speculation_clean(dtrace_state_t *state) 3098 { 3099 int work = 0, rv; 3100 dtrace_specid_t i; 3101 3102 for (i = 0; i < state->dts_nspeculations; i++) { 3103 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3104 3105 ASSERT(!spec->dtsp_cleaning); 3106 3107 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 3108 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 3109 continue; 3110 3111 work++; 3112 spec->dtsp_cleaning = 1; 3113 } 3114 3115 if (!work) 3116 return; 3117 3118 dtrace_xcall(DTRACE_CPUALL, 3119 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 3120 3121 /* 3122 * We now know that all CPUs have committed or discarded their 3123 * speculation buffers, as appropriate. We can now set the state 3124 * to inactive. 3125 */ 3126 for (i = 0; i < state->dts_nspeculations; i++) { 3127 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3128 dtrace_speculation_state_t current, new; 3129 3130 if (!spec->dtsp_cleaning) 3131 continue; 3132 3133 current = spec->dtsp_state; 3134 ASSERT(current == DTRACESPEC_DISCARDING || 3135 current == DTRACESPEC_COMMITTINGMANY); 3136 3137 new = DTRACESPEC_INACTIVE; 3138 3139 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 3140 ASSERT(rv == current); 3141 spec->dtsp_cleaning = 0; 3142 } 3143 } 3144 3145 /* 3146 * Called as part of a speculate() to get the speculative buffer associated 3147 * with a given speculation. Returns NULL if the specified speculation is not 3148 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 3149 * the active CPU is not the specified CPU -- the speculation will be 3150 * atomically transitioned into the ACTIVEMANY state. 3151 */ 3152 static dtrace_buffer_t * 3153 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 3154 dtrace_specid_t which) 3155 { 3156 dtrace_speculation_t *spec; 3157 dtrace_speculation_state_t current, new = 0; 3158 dtrace_buffer_t *buf; 3159 3160 if (which == 0) 3161 return (NULL); 3162 3163 if (which > state->dts_nspeculations) { 3164 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 3165 return (NULL); 3166 } 3167 3168 spec = &state->dts_speculations[which - 1]; 3169 buf = &spec->dtsp_buffer[cpuid]; 3170 3171 do { 3172 current = spec->dtsp_state; 3173 3174 switch (current) { 3175 case DTRACESPEC_INACTIVE: 3176 case DTRACESPEC_COMMITTINGMANY: 3177 case DTRACESPEC_DISCARDING: 3178 return (NULL); 3179 3180 case DTRACESPEC_COMMITTING: 3181 ASSERT(buf->dtb_offset == 0); 3182 return (NULL); 3183 3184 case DTRACESPEC_ACTIVEONE: 3185 /* 3186 * This speculation is currently active on one CPU. 3187 * Check the offset in the buffer; if it's non-zero, 3188 * that CPU must be us (and we leave the state alone). 3189 * If it's zero, assume that we're starting on a new 3190 * CPU -- and change the state to indicate that the 3191 * speculation is active on more than one CPU. 3192 */ 3193 if (buf->dtb_offset != 0) 3194 return (buf); 3195 3196 new = DTRACESPEC_ACTIVEMANY; 3197 break; 3198 3199 case DTRACESPEC_ACTIVEMANY: 3200 return (buf); 3201 3202 case DTRACESPEC_ACTIVE: 3203 new = DTRACESPEC_ACTIVEONE; 3204 break; 3205 3206 default: 3207 ASSERT(0); 3208 } 3209 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3210 current, new) != current); 3211 3212 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 3213 return (buf); 3214 } 3215 3216 /* 3217 * Return a string. In the event that the user lacks the privilege to access 3218 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3219 * don't fail access checking. 3220 * 3221 * dtrace_dif_variable() uses this routine as a helper for various 3222 * builtin values such as 'execname' and 'probefunc.' 3223 */ 3224 uintptr_t 3225 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 3226 dtrace_mstate_t *mstate) 3227 { 3228 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3229 uintptr_t ret; 3230 size_t strsz; 3231 3232 /* 3233 * The easy case: this probe is allowed to read all of memory, so 3234 * we can just return this as a vanilla pointer. 3235 */ 3236 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 3237 return (addr); 3238 3239 /* 3240 * This is the tougher case: we copy the string in question from 3241 * kernel memory into scratch memory and return it that way: this 3242 * ensures that we won't trip up when access checking tests the 3243 * BYREF return value. 3244 */ 3245 strsz = dtrace_strlen((char *)addr, size) + 1; 3246 3247 if (mstate->dtms_scratch_ptr + strsz > 3248 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3249 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3250 return (0); 3251 } 3252 3253 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3254 strsz); 3255 ret = mstate->dtms_scratch_ptr; 3256 mstate->dtms_scratch_ptr += strsz; 3257 return (ret); 3258 } 3259 3260 /* 3261 * Return a string from a memoy address which is known to have one or 3262 * more concatenated, individually zero terminated, sub-strings. 3263 * In the event that the user lacks the privilege to access 3264 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3265 * don't fail access checking. 3266 * 3267 * dtrace_dif_variable() uses this routine as a helper for various 3268 * builtin values such as 'execargs'. 3269 */ 3270 static uintptr_t 3271 dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 3272 dtrace_mstate_t *mstate) 3273 { 3274 char *p; 3275 size_t i; 3276 uintptr_t ret; 3277 3278 if (mstate->dtms_scratch_ptr + strsz > 3279 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3280 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3281 return (0); 3282 } 3283 3284 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3285 strsz); 3286 3287 /* Replace sub-string termination characters with a space. */ 3288 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 3289 p++, i++) 3290 if (*p == '\0') 3291 *p = ' '; 3292 3293 ret = mstate->dtms_scratch_ptr; 3294 mstate->dtms_scratch_ptr += strsz; 3295 return (ret); 3296 } 3297 3298 /* 3299 * This function implements the DIF emulator's variable lookups. The emulator 3300 * passes a reserved variable identifier and optional built-in array index. 3301 */ 3302 static uint64_t 3303 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 3304 uint64_t ndx) 3305 { 3306 /* 3307 * If we're accessing one of the uncached arguments, we'll turn this 3308 * into a reference in the args array. 3309 */ 3310 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 3311 ndx = v - DIF_VAR_ARG0; 3312 v = DIF_VAR_ARGS; 3313 } 3314 3315 switch (v) { 3316 case DIF_VAR_ARGS: 3317 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 3318 if (ndx >= sizeof (mstate->dtms_arg) / 3319 sizeof (mstate->dtms_arg[0])) { 3320 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3321 dtrace_provider_t *pv; 3322 uint64_t val; 3323 3324 pv = mstate->dtms_probe->dtpr_provider; 3325 if (pv->dtpv_pops.dtps_getargval != NULL) 3326 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 3327 mstate->dtms_probe->dtpr_id, 3328 mstate->dtms_probe->dtpr_arg, ndx, aframes); 3329 else 3330 val = dtrace_getarg(ndx, aframes); 3331 3332 /* 3333 * This is regrettably required to keep the compiler 3334 * from tail-optimizing the call to dtrace_getarg(). 3335 * The condition always evaluates to true, but the 3336 * compiler has no way of figuring that out a priori. 3337 * (None of this would be necessary if the compiler 3338 * could be relied upon to _always_ tail-optimize 3339 * the call to dtrace_getarg() -- but it can't.) 3340 */ 3341 if (mstate->dtms_probe != NULL) 3342 return (val); 3343 3344 ASSERT(0); 3345 } 3346 3347 return (mstate->dtms_arg[ndx]); 3348 3349 #ifdef illumos 3350 case DIF_VAR_UREGS: { 3351 klwp_t *lwp; 3352 3353 if (!dtrace_priv_proc(state)) 3354 return (0); 3355 3356 if ((lwp = curthread->t_lwp) == NULL) { 3357 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3358 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 3359 return (0); 3360 } 3361 3362 return (dtrace_getreg(lwp->lwp_regs, ndx)); 3363 return (0); 3364 } 3365 #else 3366 case DIF_VAR_UREGS: { 3367 struct trapframe *tframe; 3368 3369 if (!dtrace_priv_proc(state)) 3370 return (0); 3371 3372 if ((tframe = curthread->td_frame) == NULL) { 3373 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3374 cpu_core[curcpu].cpuc_dtrace_illval = 0; 3375 return (0); 3376 } 3377 3378 return (dtrace_getreg(tframe, ndx)); 3379 } 3380 #endif 3381 3382 case DIF_VAR_CURTHREAD: 3383 if (!dtrace_priv_proc(state)) 3384 return (0); 3385 return ((uint64_t)(uintptr_t)curthread); 3386 3387 case DIF_VAR_TIMESTAMP: 3388 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 3389 mstate->dtms_timestamp = dtrace_gethrtime(); 3390 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 3391 } 3392 return (mstate->dtms_timestamp); 3393 3394 case DIF_VAR_VTIMESTAMP: 3395 ASSERT(dtrace_vtime_references != 0); 3396 return (curthread->t_dtrace_vtime); 3397 3398 case DIF_VAR_WALLTIMESTAMP: 3399 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3400 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3401 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3402 } 3403 return (mstate->dtms_walltimestamp); 3404 3405 #ifdef illumos 3406 case DIF_VAR_IPL: 3407 if (!dtrace_priv_kernel(state)) 3408 return (0); 3409 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3410 mstate->dtms_ipl = dtrace_getipl(); 3411 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3412 } 3413 return (mstate->dtms_ipl); 3414 #endif 3415 3416 case DIF_VAR_EPID: 3417 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3418 return (mstate->dtms_epid); 3419 3420 case DIF_VAR_ID: 3421 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3422 return (mstate->dtms_probe->dtpr_id); 3423 3424 case DIF_VAR_STACKDEPTH: 3425 if (!dtrace_priv_kernel(state)) 3426 return (0); 3427 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3428 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3429 3430 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3431 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3432 } 3433 return (mstate->dtms_stackdepth); 3434 3435 case DIF_VAR_USTACKDEPTH: 3436 if (!dtrace_priv_proc(state)) 3437 return (0); 3438 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3439 /* 3440 * See comment in DIF_VAR_PID. 3441 */ 3442 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3443 CPU_ON_INTR(CPU)) { 3444 mstate->dtms_ustackdepth = 0; 3445 } else { 3446 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3447 mstate->dtms_ustackdepth = 3448 dtrace_getustackdepth(); 3449 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3450 } 3451 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3452 } 3453 return (mstate->dtms_ustackdepth); 3454 3455 case DIF_VAR_CALLER: 3456 if (!dtrace_priv_kernel(state)) 3457 return (0); 3458 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3459 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3460 3461 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3462 /* 3463 * If this is an unanchored probe, we are 3464 * required to go through the slow path: 3465 * dtrace_caller() only guarantees correct 3466 * results for anchored probes. 3467 */ 3468 pc_t caller[2] = {0, 0}; 3469 3470 dtrace_getpcstack(caller, 2, aframes, 3471 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3472 mstate->dtms_caller = caller[1]; 3473 } else if ((mstate->dtms_caller = 3474 dtrace_caller(aframes)) == -1) { 3475 /* 3476 * We have failed to do this the quick way; 3477 * we must resort to the slower approach of 3478 * calling dtrace_getpcstack(). 3479 */ 3480 pc_t caller = 0; 3481 3482 dtrace_getpcstack(&caller, 1, aframes, NULL); 3483 mstate->dtms_caller = caller; 3484 } 3485 3486 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3487 } 3488 return (mstate->dtms_caller); 3489 3490 case DIF_VAR_UCALLER: 3491 if (!dtrace_priv_proc(state)) 3492 return (0); 3493 3494 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3495 uint64_t ustack[3]; 3496 3497 /* 3498 * dtrace_getupcstack() fills in the first uint64_t 3499 * with the current PID. The second uint64_t will 3500 * be the program counter at user-level. The third 3501 * uint64_t will contain the caller, which is what 3502 * we're after. 3503 */ 3504 ustack[2] = 0; 3505 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3506 dtrace_getupcstack(ustack, 3); 3507 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3508 mstate->dtms_ucaller = ustack[2]; 3509 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3510 } 3511 3512 return (mstate->dtms_ucaller); 3513 3514 case DIF_VAR_PROBEPROV: 3515 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3516 return (dtrace_dif_varstr( 3517 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3518 state, mstate)); 3519 3520 case DIF_VAR_PROBEMOD: 3521 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3522 return (dtrace_dif_varstr( 3523 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3524 state, mstate)); 3525 3526 case DIF_VAR_PROBEFUNC: 3527 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3528 return (dtrace_dif_varstr( 3529 (uintptr_t)mstate->dtms_probe->dtpr_func, 3530 state, mstate)); 3531 3532 case DIF_VAR_PROBENAME: 3533 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3534 return (dtrace_dif_varstr( 3535 (uintptr_t)mstate->dtms_probe->dtpr_name, 3536 state, mstate)); 3537 3538 case DIF_VAR_PID: 3539 if (!dtrace_priv_proc(state)) 3540 return (0); 3541 3542 #ifdef illumos 3543 /* 3544 * Note that we are assuming that an unanchored probe is 3545 * always due to a high-level interrupt. (And we're assuming 3546 * that there is only a single high level interrupt.) 3547 */ 3548 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3549 return (pid0.pid_id); 3550 3551 /* 3552 * It is always safe to dereference one's own t_procp pointer: 3553 * it always points to a valid, allocated proc structure. 3554 * Further, it is always safe to dereference the p_pidp member 3555 * of one's own proc structure. (These are truisms becuase 3556 * threads and processes don't clean up their own state -- 3557 * they leave that task to whomever reaps them.) 3558 */ 3559 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3560 #else 3561 return ((uint64_t)curproc->p_pid); 3562 #endif 3563 3564 case DIF_VAR_PPID: 3565 if (!dtrace_priv_proc(state)) 3566 return (0); 3567 3568 #ifdef illumos 3569 /* 3570 * See comment in DIF_VAR_PID. 3571 */ 3572 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3573 return (pid0.pid_id); 3574 3575 /* 3576 * It is always safe to dereference one's own t_procp pointer: 3577 * it always points to a valid, allocated proc structure. 3578 * (This is true because threads don't clean up their own 3579 * state -- they leave that task to whomever reaps them.) 3580 */ 3581 return ((uint64_t)curthread->t_procp->p_ppid); 3582 #else 3583 if (curproc->p_pid == proc0.p_pid) 3584 return (curproc->p_pid); 3585 else 3586 return (curproc->p_pptr->p_pid); 3587 #endif 3588 3589 case DIF_VAR_TID: 3590 #ifdef illumos 3591 /* 3592 * See comment in DIF_VAR_PID. 3593 */ 3594 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3595 return (0); 3596 #endif 3597 3598 return ((uint64_t)curthread->t_tid); 3599 3600 case DIF_VAR_EXECARGS: { 3601 struct pargs *p_args = curthread->td_proc->p_args; 3602 3603 if (p_args == NULL) 3604 return(0); 3605 3606 return (dtrace_dif_varstrz( 3607 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3608 } 3609 3610 case DIF_VAR_EXECNAME: 3611 #ifdef illumos 3612 if (!dtrace_priv_proc(state)) 3613 return (0); 3614 3615 /* 3616 * See comment in DIF_VAR_PID. 3617 */ 3618 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3619 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3620 3621 /* 3622 * It is always safe to dereference one's own t_procp pointer: 3623 * it always points to a valid, allocated proc structure. 3624 * (This is true because threads don't clean up their own 3625 * state -- they leave that task to whomever reaps them.) 3626 */ 3627 return (dtrace_dif_varstr( 3628 (uintptr_t)curthread->t_procp->p_user.u_comm, 3629 state, mstate)); 3630 #else 3631 return (dtrace_dif_varstr( 3632 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3633 #endif 3634 3635 case DIF_VAR_ZONENAME: 3636 #ifdef illumos 3637 if (!dtrace_priv_proc(state)) 3638 return (0); 3639 3640 /* 3641 * See comment in DIF_VAR_PID. 3642 */ 3643 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3644 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3645 3646 /* 3647 * It is always safe to dereference one's own t_procp pointer: 3648 * it always points to a valid, allocated proc structure. 3649 * (This is true because threads don't clean up their own 3650 * state -- they leave that task to whomever reaps them.) 3651 */ 3652 return (dtrace_dif_varstr( 3653 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3654 state, mstate)); 3655 #else 3656 return (0); 3657 #endif 3658 3659 case DIF_VAR_UID: 3660 if (!dtrace_priv_proc(state)) 3661 return (0); 3662 3663 #ifdef illumos 3664 /* 3665 * See comment in DIF_VAR_PID. 3666 */ 3667 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3668 return ((uint64_t)p0.p_cred->cr_uid); 3669 3670 /* 3671 * It is always safe to dereference one's own t_procp pointer: 3672 * it always points to a valid, allocated proc structure. 3673 * (This is true because threads don't clean up their own 3674 * state -- they leave that task to whomever reaps them.) 3675 * 3676 * Additionally, it is safe to dereference one's own process 3677 * credential, since this is never NULL after process birth. 3678 */ 3679 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3680 #else 3681 return ((uint64_t)curthread->td_ucred->cr_uid); 3682 #endif 3683 3684 case DIF_VAR_GID: 3685 if (!dtrace_priv_proc(state)) 3686 return (0); 3687 3688 #ifdef illumos 3689 /* 3690 * See comment in DIF_VAR_PID. 3691 */ 3692 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3693 return ((uint64_t)p0.p_cred->cr_gid); 3694 3695 /* 3696 * It is always safe to dereference one's own t_procp pointer: 3697 * it always points to a valid, allocated proc structure. 3698 * (This is true because threads don't clean up their own 3699 * state -- they leave that task to whomever reaps them.) 3700 * 3701 * Additionally, it is safe to dereference one's own process 3702 * credential, since this is never NULL after process birth. 3703 */ 3704 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3705 #else 3706 return ((uint64_t)curthread->td_ucred->cr_gid); 3707 #endif 3708 3709 case DIF_VAR_ERRNO: { 3710 #ifdef illumos 3711 klwp_t *lwp; 3712 if (!dtrace_priv_proc(state)) 3713 return (0); 3714 3715 /* 3716 * See comment in DIF_VAR_PID. 3717 */ 3718 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3719 return (0); 3720 3721 /* 3722 * It is always safe to dereference one's own t_lwp pointer in 3723 * the event that this pointer is non-NULL. (This is true 3724 * because threads and lwps don't clean up their own state -- 3725 * they leave that task to whomever reaps them.) 3726 */ 3727 if ((lwp = curthread->t_lwp) == NULL) 3728 return (0); 3729 3730 return ((uint64_t)lwp->lwp_errno); 3731 #else 3732 return (curthread->td_errno); 3733 #endif 3734 } 3735 #ifndef illumos 3736 case DIF_VAR_CPU: { 3737 return curcpu; 3738 } 3739 #endif 3740 default: 3741 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3742 return (0); 3743 } 3744 } 3745 3746 3747 typedef enum dtrace_json_state { 3748 DTRACE_JSON_REST = 1, 3749 DTRACE_JSON_OBJECT, 3750 DTRACE_JSON_STRING, 3751 DTRACE_JSON_STRING_ESCAPE, 3752 DTRACE_JSON_STRING_ESCAPE_UNICODE, 3753 DTRACE_JSON_COLON, 3754 DTRACE_JSON_COMMA, 3755 DTRACE_JSON_VALUE, 3756 DTRACE_JSON_IDENTIFIER, 3757 DTRACE_JSON_NUMBER, 3758 DTRACE_JSON_NUMBER_FRAC, 3759 DTRACE_JSON_NUMBER_EXP, 3760 DTRACE_JSON_COLLECT_OBJECT 3761 } dtrace_json_state_t; 3762 3763 /* 3764 * This function possesses just enough knowledge about JSON to extract a single 3765 * value from a JSON string and store it in the scratch buffer. It is able 3766 * to extract nested object values, and members of arrays by index. 3767 * 3768 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to 3769 * be looked up as we descend into the object tree. e.g. 3770 * 3771 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL 3772 * with nelems = 5. 3773 * 3774 * The run time of this function must be bounded above by strsize to limit the 3775 * amount of work done in probe context. As such, it is implemented as a 3776 * simple state machine, reading one character at a time using safe loads 3777 * until we find the requested element, hit a parsing error or run off the 3778 * end of the object or string. 3779 * 3780 * As there is no way for a subroutine to return an error without interrupting 3781 * clause execution, we simply return NULL in the event of a missing key or any 3782 * other error condition. Each NULL return in this function is commented with 3783 * the error condition it represents -- parsing or otherwise. 3784 * 3785 * The set of states for the state machine closely matches the JSON 3786 * specification (http://json.org/). Briefly: 3787 * 3788 * DTRACE_JSON_REST: 3789 * Skip whitespace until we find either a top-level Object, moving 3790 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE. 3791 * 3792 * DTRACE_JSON_OBJECT: 3793 * Locate the next key String in an Object. Sets a flag to denote 3794 * the next String as a key string and moves to DTRACE_JSON_STRING. 3795 * 3796 * DTRACE_JSON_COLON: 3797 * Skip whitespace until we find the colon that separates key Strings 3798 * from their values. Once found, move to DTRACE_JSON_VALUE. 3799 * 3800 * DTRACE_JSON_VALUE: 3801 * Detects the type of the next value (String, Number, Identifier, Object 3802 * or Array) and routes to the states that process that type. Here we also 3803 * deal with the element selector list if we are requested to traverse down 3804 * into the object tree. 3805 * 3806 * DTRACE_JSON_COMMA: 3807 * Skip whitespace until we find the comma that separates key-value pairs 3808 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays 3809 * (similarly DTRACE_JSON_VALUE). All following literal value processing 3810 * states return to this state at the end of their value, unless otherwise 3811 * noted. 3812 * 3813 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP: 3814 * Processes a Number literal from the JSON, including any exponent 3815 * component that may be present. Numbers are returned as strings, which 3816 * may be passed to strtoll() if an integer is required. 3817 * 3818 * DTRACE_JSON_IDENTIFIER: 3819 * Processes a "true", "false" or "null" literal in the JSON. 3820 * 3821 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE, 3822 * DTRACE_JSON_STRING_ESCAPE_UNICODE: 3823 * Processes a String literal from the JSON, whether the String denotes 3824 * a key, a value or part of a larger Object. Handles all escape sequences 3825 * present in the specification, including four-digit unicode characters, 3826 * but merely includes the escape sequence without converting it to the 3827 * actual escaped character. If the String is flagged as a key, we 3828 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA. 3829 * 3830 * DTRACE_JSON_COLLECT_OBJECT: 3831 * This state collects an entire Object (or Array), correctly handling 3832 * embedded strings. If the full element selector list matches this nested 3833 * object, we return the Object in full as a string. If not, we use this 3834 * state to skip to the next value at this level and continue processing. 3835 * 3836 * NOTE: This function uses various macros from strtolctype.h to manipulate 3837 * digit values, etc -- these have all been checked to ensure they make 3838 * no additional function calls. 3839 */ 3840 static char * 3841 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems, 3842 char *dest) 3843 { 3844 dtrace_json_state_t state = DTRACE_JSON_REST; 3845 int64_t array_elem = INT64_MIN; 3846 int64_t array_pos = 0; 3847 uint8_t escape_unicount = 0; 3848 boolean_t string_is_key = B_FALSE; 3849 boolean_t collect_object = B_FALSE; 3850 boolean_t found_key = B_FALSE; 3851 boolean_t in_array = B_FALSE; 3852 uint32_t braces = 0, brackets = 0; 3853 char *elem = elemlist; 3854 char *dd = dest; 3855 uintptr_t cur; 3856 3857 for (cur = json; cur < json + size; cur++) { 3858 char cc = dtrace_load8(cur); 3859 if (cc == '\0') 3860 return (NULL); 3861 3862 switch (state) { 3863 case DTRACE_JSON_REST: 3864 if (isspace(cc)) 3865 break; 3866 3867 if (cc == '{') { 3868 state = DTRACE_JSON_OBJECT; 3869 break; 3870 } 3871 3872 if (cc == '[') { 3873 in_array = B_TRUE; 3874 array_pos = 0; 3875 array_elem = dtrace_strtoll(elem, 10, size); 3876 found_key = array_elem == 0 ? B_TRUE : B_FALSE; 3877 state = DTRACE_JSON_VALUE; 3878 break; 3879 } 3880 3881 /* 3882 * ERROR: expected to find a top-level object or array. 3883 */ 3884 return (NULL); 3885 case DTRACE_JSON_OBJECT: 3886 if (isspace(cc)) 3887 break; 3888 3889 if (cc == '"') { 3890 state = DTRACE_JSON_STRING; 3891 string_is_key = B_TRUE; 3892 break; 3893 } 3894 3895 /* 3896 * ERROR: either the object did not start with a key 3897 * string, or we've run off the end of the object 3898 * without finding the requested key. 3899 */ 3900 return (NULL); 3901 case DTRACE_JSON_STRING: 3902 if (cc == '\\') { 3903 *dd++ = '\\'; 3904 state = DTRACE_JSON_STRING_ESCAPE; 3905 break; 3906 } 3907 3908 if (cc == '"') { 3909 if (collect_object) { 3910 /* 3911 * We don't reset the dest here, as 3912 * the string is part of a larger 3913 * object being collected. 3914 */ 3915 *dd++ = cc; 3916 collect_object = B_FALSE; 3917 state = DTRACE_JSON_COLLECT_OBJECT; 3918 break; 3919 } 3920 *dd = '\0'; 3921 dd = dest; /* reset string buffer */ 3922 if (string_is_key) { 3923 if (dtrace_strncmp(dest, elem, 3924 size) == 0) 3925 found_key = B_TRUE; 3926 } else if (found_key) { 3927 if (nelems > 1) { 3928 /* 3929 * We expected an object, not 3930 * this string. 3931 */ 3932 return (NULL); 3933 } 3934 return (dest); 3935 } 3936 state = string_is_key ? DTRACE_JSON_COLON : 3937 DTRACE_JSON_COMMA; 3938 string_is_key = B_FALSE; 3939 break; 3940 } 3941 3942 *dd++ = cc; 3943 break; 3944 case DTRACE_JSON_STRING_ESCAPE: 3945 *dd++ = cc; 3946 if (cc == 'u') { 3947 escape_unicount = 0; 3948 state = DTRACE_JSON_STRING_ESCAPE_UNICODE; 3949 } else { 3950 state = DTRACE_JSON_STRING; 3951 } 3952 break; 3953 case DTRACE_JSON_STRING_ESCAPE_UNICODE: 3954 if (!isxdigit(cc)) { 3955 /* 3956 * ERROR: invalid unicode escape, expected 3957 * four valid hexidecimal digits. 3958 */ 3959 return (NULL); 3960 } 3961 3962 *dd++ = cc; 3963 if (++escape_unicount == 4) 3964 state = DTRACE_JSON_STRING; 3965 break; 3966 case DTRACE_JSON_COLON: 3967 if (isspace(cc)) 3968 break; 3969 3970 if (cc == ':') { 3971 state = DTRACE_JSON_VALUE; 3972 break; 3973 } 3974 3975 /* 3976 * ERROR: expected a colon. 3977 */ 3978 return (NULL); 3979 case DTRACE_JSON_COMMA: 3980 if (isspace(cc)) 3981 break; 3982 3983 if (cc == ',') { 3984 if (in_array) { 3985 state = DTRACE_JSON_VALUE; 3986 if (++array_pos == array_elem) 3987 found_key = B_TRUE; 3988 } else { 3989 state = DTRACE_JSON_OBJECT; 3990 } 3991 break; 3992 } 3993 3994 /* 3995 * ERROR: either we hit an unexpected character, or 3996 * we reached the end of the object or array without 3997 * finding the requested key. 3998 */ 3999 return (NULL); 4000 case DTRACE_JSON_IDENTIFIER: 4001 if (islower(cc)) { 4002 *dd++ = cc; 4003 break; 4004 } 4005 4006 *dd = '\0'; 4007 dd = dest; /* reset string buffer */ 4008 4009 if (dtrace_strncmp(dest, "true", 5) == 0 || 4010 dtrace_strncmp(dest, "false", 6) == 0 || 4011 dtrace_strncmp(dest, "null", 5) == 0) { 4012 if (found_key) { 4013 if (nelems > 1) { 4014 /* 4015 * ERROR: We expected an object, 4016 * not this identifier. 4017 */ 4018 return (NULL); 4019 } 4020 return (dest); 4021 } else { 4022 cur--; 4023 state = DTRACE_JSON_COMMA; 4024 break; 4025 } 4026 } 4027 4028 /* 4029 * ERROR: we did not recognise the identifier as one 4030 * of those in the JSON specification. 4031 */ 4032 return (NULL); 4033 case DTRACE_JSON_NUMBER: 4034 if (cc == '.') { 4035 *dd++ = cc; 4036 state = DTRACE_JSON_NUMBER_FRAC; 4037 break; 4038 } 4039 4040 if (cc == 'x' || cc == 'X') { 4041 /* 4042 * ERROR: specification explicitly excludes 4043 * hexidecimal or octal numbers. 4044 */ 4045 return (NULL); 4046 } 4047 4048 /* FALLTHRU */ 4049 case DTRACE_JSON_NUMBER_FRAC: 4050 if (cc == 'e' || cc == 'E') { 4051 *dd++ = cc; 4052 state = DTRACE_JSON_NUMBER_EXP; 4053 break; 4054 } 4055 4056 if (cc == '+' || cc == '-') { 4057 /* 4058 * ERROR: expect sign as part of exponent only. 4059 */ 4060 return (NULL); 4061 } 4062 /* FALLTHRU */ 4063 case DTRACE_JSON_NUMBER_EXP: 4064 if (isdigit(cc) || cc == '+' || cc == '-') { 4065 *dd++ = cc; 4066 break; 4067 } 4068 4069 *dd = '\0'; 4070 dd = dest; /* reset string buffer */ 4071 if (found_key) { 4072 if (nelems > 1) { 4073 /* 4074 * ERROR: We expected an object, not 4075 * this number. 4076 */ 4077 return (NULL); 4078 } 4079 return (dest); 4080 } 4081 4082 cur--; 4083 state = DTRACE_JSON_COMMA; 4084 break; 4085 case DTRACE_JSON_VALUE: 4086 if (isspace(cc)) 4087 break; 4088 4089 if (cc == '{' || cc == '[') { 4090 if (nelems > 1 && found_key) { 4091 in_array = cc == '[' ? B_TRUE : B_FALSE; 4092 /* 4093 * If our element selector directs us 4094 * to descend into this nested object, 4095 * then move to the next selector 4096 * element in the list and restart the 4097 * state machine. 4098 */ 4099 while (*elem != '\0') 4100 elem++; 4101 elem++; /* skip the inter-element NUL */ 4102 nelems--; 4103 dd = dest; 4104 if (in_array) { 4105 state = DTRACE_JSON_VALUE; 4106 array_pos = 0; 4107 array_elem = dtrace_strtoll( 4108 elem, 10, size); 4109 found_key = array_elem == 0 ? 4110 B_TRUE : B_FALSE; 4111 } else { 4112 found_key = B_FALSE; 4113 state = DTRACE_JSON_OBJECT; 4114 } 4115 break; 4116 } 4117 4118 /* 4119 * Otherwise, we wish to either skip this 4120 * nested object or return it in full. 4121 */ 4122 if (cc == '[') 4123 brackets = 1; 4124 else 4125 braces = 1; 4126 *dd++ = cc; 4127 state = DTRACE_JSON_COLLECT_OBJECT; 4128 break; 4129 } 4130 4131 if (cc == '"') { 4132 state = DTRACE_JSON_STRING; 4133 break; 4134 } 4135 4136 if (islower(cc)) { 4137 /* 4138 * Here we deal with true, false and null. 4139 */ 4140 *dd++ = cc; 4141 state = DTRACE_JSON_IDENTIFIER; 4142 break; 4143 } 4144 4145 if (cc == '-' || isdigit(cc)) { 4146 *dd++ = cc; 4147 state = DTRACE_JSON_NUMBER; 4148 break; 4149 } 4150 4151 /* 4152 * ERROR: unexpected character at start of value. 4153 */ 4154 return (NULL); 4155 case DTRACE_JSON_COLLECT_OBJECT: 4156 if (cc == '\0') 4157 /* 4158 * ERROR: unexpected end of input. 4159 */ 4160 return (NULL); 4161 4162 *dd++ = cc; 4163 if (cc == '"') { 4164 collect_object = B_TRUE; 4165 state = DTRACE_JSON_STRING; 4166 break; 4167 } 4168 4169 if (cc == ']') { 4170 if (brackets-- == 0) { 4171 /* 4172 * ERROR: unbalanced brackets. 4173 */ 4174 return (NULL); 4175 } 4176 } else if (cc == '}') { 4177 if (braces-- == 0) { 4178 /* 4179 * ERROR: unbalanced braces. 4180 */ 4181 return (NULL); 4182 } 4183 } else if (cc == '{') { 4184 braces++; 4185 } else if (cc == '[') { 4186 brackets++; 4187 } 4188 4189 if (brackets == 0 && braces == 0) { 4190 if (found_key) { 4191 *dd = '\0'; 4192 return (dest); 4193 } 4194 dd = dest; /* reset string buffer */ 4195 state = DTRACE_JSON_COMMA; 4196 } 4197 break; 4198 } 4199 } 4200 return (NULL); 4201 } 4202 4203 /* 4204 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 4205 * Notice that we don't bother validating the proper number of arguments or 4206 * their types in the tuple stack. This isn't needed because all argument 4207 * interpretation is safe because of our load safety -- the worst that can 4208 * happen is that a bogus program can obtain bogus results. 4209 */ 4210 static void 4211 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 4212 dtrace_key_t *tupregs, int nargs, 4213 dtrace_mstate_t *mstate, dtrace_state_t *state) 4214 { 4215 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4216 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4217 dtrace_vstate_t *vstate = &state->dts_vstate; 4218 4219 #ifdef illumos 4220 union { 4221 mutex_impl_t mi; 4222 uint64_t mx; 4223 } m; 4224 4225 union { 4226 krwlock_t ri; 4227 uintptr_t rw; 4228 } r; 4229 #else 4230 struct thread *lowner; 4231 union { 4232 struct lock_object *li; 4233 uintptr_t lx; 4234 } l; 4235 #endif 4236 4237 switch (subr) { 4238 case DIF_SUBR_RAND: 4239 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 4240 break; 4241 4242 #ifdef illumos 4243 case DIF_SUBR_MUTEX_OWNED: 4244 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4245 mstate, vstate)) { 4246 regs[rd] = 0; 4247 break; 4248 } 4249 4250 m.mx = dtrace_load64(tupregs[0].dttk_value); 4251 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 4252 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 4253 else 4254 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 4255 break; 4256 4257 case DIF_SUBR_MUTEX_OWNER: 4258 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4259 mstate, vstate)) { 4260 regs[rd] = 0; 4261 break; 4262 } 4263 4264 m.mx = dtrace_load64(tupregs[0].dttk_value); 4265 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 4266 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 4267 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 4268 else 4269 regs[rd] = 0; 4270 break; 4271 4272 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4273 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4274 mstate, vstate)) { 4275 regs[rd] = 0; 4276 break; 4277 } 4278 4279 m.mx = dtrace_load64(tupregs[0].dttk_value); 4280 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 4281 break; 4282 4283 case DIF_SUBR_MUTEX_TYPE_SPIN: 4284 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4285 mstate, vstate)) { 4286 regs[rd] = 0; 4287 break; 4288 } 4289 4290 m.mx = dtrace_load64(tupregs[0].dttk_value); 4291 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 4292 break; 4293 4294 case DIF_SUBR_RW_READ_HELD: { 4295 uintptr_t tmp; 4296 4297 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4298 mstate, vstate)) { 4299 regs[rd] = 0; 4300 break; 4301 } 4302 4303 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4304 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 4305 break; 4306 } 4307 4308 case DIF_SUBR_RW_WRITE_HELD: 4309 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4310 mstate, vstate)) { 4311 regs[rd] = 0; 4312 break; 4313 } 4314 4315 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4316 regs[rd] = _RW_WRITE_HELD(&r.ri); 4317 break; 4318 4319 case DIF_SUBR_RW_ISWRITER: 4320 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4321 mstate, vstate)) { 4322 regs[rd] = 0; 4323 break; 4324 } 4325 4326 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4327 regs[rd] = _RW_ISWRITER(&r.ri); 4328 break; 4329 4330 #else /* !illumos */ 4331 case DIF_SUBR_MUTEX_OWNED: 4332 if (!dtrace_canload(tupregs[0].dttk_value, 4333 sizeof (struct lock_object), mstate, vstate)) { 4334 regs[rd] = 0; 4335 break; 4336 } 4337 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4338 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4339 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4340 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4341 break; 4342 4343 case DIF_SUBR_MUTEX_OWNER: 4344 if (!dtrace_canload(tupregs[0].dttk_value, 4345 sizeof (struct lock_object), mstate, vstate)) { 4346 regs[rd] = 0; 4347 break; 4348 } 4349 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4350 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4351 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4352 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4353 regs[rd] = (uintptr_t)lowner; 4354 break; 4355 4356 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4357 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4358 mstate, vstate)) { 4359 regs[rd] = 0; 4360 break; 4361 } 4362 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4363 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4364 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SLEEPLOCK) != 0; 4365 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4366 break; 4367 4368 case DIF_SUBR_MUTEX_TYPE_SPIN: 4369 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4370 mstate, vstate)) { 4371 regs[rd] = 0; 4372 break; 4373 } 4374 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4375 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4376 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 4377 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4378 break; 4379 4380 case DIF_SUBR_RW_READ_HELD: 4381 case DIF_SUBR_SX_SHARED_HELD: 4382 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4383 mstate, vstate)) { 4384 regs[rd] = 0; 4385 break; 4386 } 4387 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4388 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4389 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4390 lowner == NULL; 4391 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4392 break; 4393 4394 case DIF_SUBR_RW_WRITE_HELD: 4395 case DIF_SUBR_SX_EXCLUSIVE_HELD: 4396 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4397 mstate, vstate)) { 4398 regs[rd] = 0; 4399 break; 4400 } 4401 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4402 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4403 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4404 lowner != NULL; 4405 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4406 break; 4407 4408 case DIF_SUBR_RW_ISWRITER: 4409 case DIF_SUBR_SX_ISEXCLUSIVE: 4410 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4411 mstate, vstate)) { 4412 regs[rd] = 0; 4413 break; 4414 } 4415 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4416 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4417 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4418 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4419 regs[rd] = (lowner == curthread); 4420 break; 4421 #endif /* illumos */ 4422 4423 case DIF_SUBR_BCOPY: { 4424 /* 4425 * We need to be sure that the destination is in the scratch 4426 * region -- no other region is allowed. 4427 */ 4428 uintptr_t src = tupregs[0].dttk_value; 4429 uintptr_t dest = tupregs[1].dttk_value; 4430 size_t size = tupregs[2].dttk_value; 4431 4432 if (!dtrace_inscratch(dest, size, mstate)) { 4433 *flags |= CPU_DTRACE_BADADDR; 4434 *illval = regs[rd]; 4435 break; 4436 } 4437 4438 if (!dtrace_canload(src, size, mstate, vstate)) { 4439 regs[rd] = 0; 4440 break; 4441 } 4442 4443 dtrace_bcopy((void *)src, (void *)dest, size); 4444 break; 4445 } 4446 4447 case DIF_SUBR_ALLOCA: 4448 case DIF_SUBR_COPYIN: { 4449 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4450 uint64_t size = 4451 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 4452 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 4453 4454 /* 4455 * This action doesn't require any credential checks since 4456 * probes will not activate in user contexts to which the 4457 * enabling user does not have permissions. 4458 */ 4459 4460 /* 4461 * Rounding up the user allocation size could have overflowed 4462 * a large, bogus allocation (like -1ULL) to 0. 4463 */ 4464 if (scratch_size < size || 4465 !DTRACE_INSCRATCH(mstate, scratch_size)) { 4466 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4467 regs[rd] = 0; 4468 break; 4469 } 4470 4471 if (subr == DIF_SUBR_COPYIN) { 4472 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4473 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4474 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4475 } 4476 4477 mstate->dtms_scratch_ptr += scratch_size; 4478 regs[rd] = dest; 4479 break; 4480 } 4481 4482 case DIF_SUBR_COPYINTO: { 4483 uint64_t size = tupregs[1].dttk_value; 4484 uintptr_t dest = tupregs[2].dttk_value; 4485 4486 /* 4487 * This action doesn't require any credential checks since 4488 * probes will not activate in user contexts to which the 4489 * enabling user does not have permissions. 4490 */ 4491 if (!dtrace_inscratch(dest, size, mstate)) { 4492 *flags |= CPU_DTRACE_BADADDR; 4493 *illval = regs[rd]; 4494 break; 4495 } 4496 4497 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4498 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4499 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4500 break; 4501 } 4502 4503 case DIF_SUBR_COPYINSTR: { 4504 uintptr_t dest = mstate->dtms_scratch_ptr; 4505 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4506 4507 if (nargs > 1 && tupregs[1].dttk_value < size) 4508 size = tupregs[1].dttk_value + 1; 4509 4510 /* 4511 * This action doesn't require any credential checks since 4512 * probes will not activate in user contexts to which the 4513 * enabling user does not have permissions. 4514 */ 4515 if (!DTRACE_INSCRATCH(mstate, size)) { 4516 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4517 regs[rd] = 0; 4518 break; 4519 } 4520 4521 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4522 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 4523 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4524 4525 ((char *)dest)[size - 1] = '\0'; 4526 mstate->dtms_scratch_ptr += size; 4527 regs[rd] = dest; 4528 break; 4529 } 4530 4531 #ifdef illumos 4532 case DIF_SUBR_MSGSIZE: 4533 case DIF_SUBR_MSGDSIZE: { 4534 uintptr_t baddr = tupregs[0].dttk_value, daddr; 4535 uintptr_t wptr, rptr; 4536 size_t count = 0; 4537 int cont = 0; 4538 4539 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 4540 4541 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 4542 vstate)) { 4543 regs[rd] = 0; 4544 break; 4545 } 4546 4547 wptr = dtrace_loadptr(baddr + 4548 offsetof(mblk_t, b_wptr)); 4549 4550 rptr = dtrace_loadptr(baddr + 4551 offsetof(mblk_t, b_rptr)); 4552 4553 if (wptr < rptr) { 4554 *flags |= CPU_DTRACE_BADADDR; 4555 *illval = tupregs[0].dttk_value; 4556 break; 4557 } 4558 4559 daddr = dtrace_loadptr(baddr + 4560 offsetof(mblk_t, b_datap)); 4561 4562 baddr = dtrace_loadptr(baddr + 4563 offsetof(mblk_t, b_cont)); 4564 4565 /* 4566 * We want to prevent against denial-of-service here, 4567 * so we're only going to search the list for 4568 * dtrace_msgdsize_max mblks. 4569 */ 4570 if (cont++ > dtrace_msgdsize_max) { 4571 *flags |= CPU_DTRACE_ILLOP; 4572 break; 4573 } 4574 4575 if (subr == DIF_SUBR_MSGDSIZE) { 4576 if (dtrace_load8(daddr + 4577 offsetof(dblk_t, db_type)) != M_DATA) 4578 continue; 4579 } 4580 4581 count += wptr - rptr; 4582 } 4583 4584 if (!(*flags & CPU_DTRACE_FAULT)) 4585 regs[rd] = count; 4586 4587 break; 4588 } 4589 #endif 4590 4591 case DIF_SUBR_PROGENYOF: { 4592 pid_t pid = tupregs[0].dttk_value; 4593 proc_t *p; 4594 int rval = 0; 4595 4596 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4597 4598 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 4599 #ifdef illumos 4600 if (p->p_pidp->pid_id == pid) { 4601 #else 4602 if (p->p_pid == pid) { 4603 #endif 4604 rval = 1; 4605 break; 4606 } 4607 } 4608 4609 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4610 4611 regs[rd] = rval; 4612 break; 4613 } 4614 4615 case DIF_SUBR_SPECULATION: 4616 regs[rd] = dtrace_speculation(state); 4617 break; 4618 4619 case DIF_SUBR_COPYOUT: { 4620 uintptr_t kaddr = tupregs[0].dttk_value; 4621 uintptr_t uaddr = tupregs[1].dttk_value; 4622 uint64_t size = tupregs[2].dttk_value; 4623 4624 if (!dtrace_destructive_disallow && 4625 dtrace_priv_proc_control(state) && 4626 !dtrace_istoxic(kaddr, size) && 4627 dtrace_canload(kaddr, size, mstate, vstate)) { 4628 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4629 dtrace_copyout(kaddr, uaddr, size, flags); 4630 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4631 } 4632 break; 4633 } 4634 4635 case DIF_SUBR_COPYOUTSTR: { 4636 uintptr_t kaddr = tupregs[0].dttk_value; 4637 uintptr_t uaddr = tupregs[1].dttk_value; 4638 uint64_t size = tupregs[2].dttk_value; 4639 size_t lim; 4640 4641 if (!dtrace_destructive_disallow && 4642 dtrace_priv_proc_control(state) && 4643 !dtrace_istoxic(kaddr, size) && 4644 dtrace_strcanload(kaddr, size, &lim, mstate, vstate)) { 4645 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4646 dtrace_copyoutstr(kaddr, uaddr, lim, flags); 4647 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4648 } 4649 break; 4650 } 4651 4652 case DIF_SUBR_STRLEN: { 4653 size_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4654 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 4655 size_t lim; 4656 4657 if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) { 4658 regs[rd] = 0; 4659 break; 4660 } 4661 4662 regs[rd] = dtrace_strlen((char *)addr, lim); 4663 break; 4664 } 4665 4666 case DIF_SUBR_STRCHR: 4667 case DIF_SUBR_STRRCHR: { 4668 /* 4669 * We're going to iterate over the string looking for the 4670 * specified character. We will iterate until we have reached 4671 * the string length or we have found the character. If this 4672 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 4673 * of the specified character instead of the first. 4674 */ 4675 uintptr_t addr = tupregs[0].dttk_value; 4676 uintptr_t addr_limit; 4677 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4678 size_t lim; 4679 char c, target = (char)tupregs[1].dttk_value; 4680 4681 if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) { 4682 regs[rd] = 0; 4683 break; 4684 } 4685 addr_limit = addr + lim; 4686 4687 for (regs[rd] = 0; addr < addr_limit; addr++) { 4688 if ((c = dtrace_load8(addr)) == target) { 4689 regs[rd] = addr; 4690 4691 if (subr == DIF_SUBR_STRCHR) 4692 break; 4693 } 4694 4695 if (c == '\0') 4696 break; 4697 } 4698 break; 4699 } 4700 4701 case DIF_SUBR_STRSTR: 4702 case DIF_SUBR_INDEX: 4703 case DIF_SUBR_RINDEX: { 4704 /* 4705 * We're going to iterate over the string looking for the 4706 * specified string. We will iterate until we have reached 4707 * the string length or we have found the string. (Yes, this 4708 * is done in the most naive way possible -- but considering 4709 * that the string we're searching for is likely to be 4710 * relatively short, the complexity of Rabin-Karp or similar 4711 * hardly seems merited.) 4712 */ 4713 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 4714 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 4715 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4716 size_t len = dtrace_strlen(addr, size); 4717 size_t sublen = dtrace_strlen(substr, size); 4718 char *limit = addr + len, *orig = addr; 4719 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 4720 int inc = 1; 4721 4722 regs[rd] = notfound; 4723 4724 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 4725 regs[rd] = 0; 4726 break; 4727 } 4728 4729 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 4730 vstate)) { 4731 regs[rd] = 0; 4732 break; 4733 } 4734 4735 /* 4736 * strstr() and index()/rindex() have similar semantics if 4737 * both strings are the empty string: strstr() returns a 4738 * pointer to the (empty) string, and index() and rindex() 4739 * both return index 0 (regardless of any position argument). 4740 */ 4741 if (sublen == 0 && len == 0) { 4742 if (subr == DIF_SUBR_STRSTR) 4743 regs[rd] = (uintptr_t)addr; 4744 else 4745 regs[rd] = 0; 4746 break; 4747 } 4748 4749 if (subr != DIF_SUBR_STRSTR) { 4750 if (subr == DIF_SUBR_RINDEX) { 4751 limit = orig - 1; 4752 addr += len; 4753 inc = -1; 4754 } 4755 4756 /* 4757 * Both index() and rindex() take an optional position 4758 * argument that denotes the starting position. 4759 */ 4760 if (nargs == 3) { 4761 int64_t pos = (int64_t)tupregs[2].dttk_value; 4762 4763 /* 4764 * If the position argument to index() is 4765 * negative, Perl implicitly clamps it at 4766 * zero. This semantic is a little surprising 4767 * given the special meaning of negative 4768 * positions to similar Perl functions like 4769 * substr(), but it appears to reflect a 4770 * notion that index() can start from a 4771 * negative index and increment its way up to 4772 * the string. Given this notion, Perl's 4773 * rindex() is at least self-consistent in 4774 * that it implicitly clamps positions greater 4775 * than the string length to be the string 4776 * length. Where Perl completely loses 4777 * coherence, however, is when the specified 4778 * substring is the empty string (""). In 4779 * this case, even if the position is 4780 * negative, rindex() returns 0 -- and even if 4781 * the position is greater than the length, 4782 * index() returns the string length. These 4783 * semantics violate the notion that index() 4784 * should never return a value less than the 4785 * specified position and that rindex() should 4786 * never return a value greater than the 4787 * specified position. (One assumes that 4788 * these semantics are artifacts of Perl's 4789 * implementation and not the results of 4790 * deliberate design -- it beggars belief that 4791 * even Larry Wall could desire such oddness.) 4792 * While in the abstract one would wish for 4793 * consistent position semantics across 4794 * substr(), index() and rindex() -- or at the 4795 * very least self-consistent position 4796 * semantics for index() and rindex() -- we 4797 * instead opt to keep with the extant Perl 4798 * semantics, in all their broken glory. (Do 4799 * we have more desire to maintain Perl's 4800 * semantics than Perl does? Probably.) 4801 */ 4802 if (subr == DIF_SUBR_RINDEX) { 4803 if (pos < 0) { 4804 if (sublen == 0) 4805 regs[rd] = 0; 4806 break; 4807 } 4808 4809 if (pos > len) 4810 pos = len; 4811 } else { 4812 if (pos < 0) 4813 pos = 0; 4814 4815 if (pos >= len) { 4816 if (sublen == 0) 4817 regs[rd] = len; 4818 break; 4819 } 4820 } 4821 4822 addr = orig + pos; 4823 } 4824 } 4825 4826 for (regs[rd] = notfound; addr != limit; addr += inc) { 4827 if (dtrace_strncmp(addr, substr, sublen) == 0) { 4828 if (subr != DIF_SUBR_STRSTR) { 4829 /* 4830 * As D index() and rindex() are 4831 * modeled on Perl (and not on awk), 4832 * we return a zero-based (and not a 4833 * one-based) index. (For you Perl 4834 * weenies: no, we're not going to add 4835 * $[ -- and shouldn't you be at a con 4836 * or something?) 4837 */ 4838 regs[rd] = (uintptr_t)(addr - orig); 4839 break; 4840 } 4841 4842 ASSERT(subr == DIF_SUBR_STRSTR); 4843 regs[rd] = (uintptr_t)addr; 4844 break; 4845 } 4846 } 4847 4848 break; 4849 } 4850 4851 case DIF_SUBR_STRTOK: { 4852 uintptr_t addr = tupregs[0].dttk_value; 4853 uintptr_t tokaddr = tupregs[1].dttk_value; 4854 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4855 uintptr_t limit, toklimit; 4856 size_t clim; 4857 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 4858 char *dest = (char *)mstate->dtms_scratch_ptr; 4859 int i; 4860 4861 /* 4862 * Check both the token buffer and (later) the input buffer, 4863 * since both could be non-scratch addresses. 4864 */ 4865 if (!dtrace_strcanload(tokaddr, size, &clim, mstate, vstate)) { 4866 regs[rd] = 0; 4867 break; 4868 } 4869 toklimit = tokaddr + clim; 4870 4871 if (!DTRACE_INSCRATCH(mstate, size)) { 4872 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4873 regs[rd] = 0; 4874 break; 4875 } 4876 4877 if (addr == 0) { 4878 /* 4879 * If the address specified is NULL, we use our saved 4880 * strtok pointer from the mstate. Note that this 4881 * means that the saved strtok pointer is _only_ 4882 * valid within multiple enablings of the same probe -- 4883 * it behaves like an implicit clause-local variable. 4884 */ 4885 addr = mstate->dtms_strtok; 4886 limit = mstate->dtms_strtok_limit; 4887 } else { 4888 /* 4889 * If the user-specified address is non-NULL we must 4890 * access check it. This is the only time we have 4891 * a chance to do so, since this address may reside 4892 * in the string table of this clause-- future calls 4893 * (when we fetch addr from mstate->dtms_strtok) 4894 * would fail this access check. 4895 */ 4896 if (!dtrace_strcanload(addr, size, &clim, mstate, 4897 vstate)) { 4898 regs[rd] = 0; 4899 break; 4900 } 4901 limit = addr + clim; 4902 } 4903 4904 /* 4905 * First, zero the token map, and then process the token 4906 * string -- setting a bit in the map for every character 4907 * found in the token string. 4908 */ 4909 for (i = 0; i < sizeof (tokmap); i++) 4910 tokmap[i] = 0; 4911 4912 for (; tokaddr < toklimit; tokaddr++) { 4913 if ((c = dtrace_load8(tokaddr)) == '\0') 4914 break; 4915 4916 ASSERT((c >> 3) < sizeof (tokmap)); 4917 tokmap[c >> 3] |= (1 << (c & 0x7)); 4918 } 4919 4920 for (; addr < limit; addr++) { 4921 /* 4922 * We're looking for a character that is _not_ 4923 * contained in the token string. 4924 */ 4925 if ((c = dtrace_load8(addr)) == '\0') 4926 break; 4927 4928 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 4929 break; 4930 } 4931 4932 if (c == '\0') { 4933 /* 4934 * We reached the end of the string without finding 4935 * any character that was not in the token string. 4936 * We return NULL in this case, and we set the saved 4937 * address to NULL as well. 4938 */ 4939 regs[rd] = 0; 4940 mstate->dtms_strtok = 0; 4941 mstate->dtms_strtok_limit = 0; 4942 break; 4943 } 4944 4945 /* 4946 * From here on, we're copying into the destination string. 4947 */ 4948 for (i = 0; addr < limit && i < size - 1; addr++) { 4949 if ((c = dtrace_load8(addr)) == '\0') 4950 break; 4951 4952 if (tokmap[c >> 3] & (1 << (c & 0x7))) 4953 break; 4954 4955 ASSERT(i < size); 4956 dest[i++] = c; 4957 } 4958 4959 ASSERT(i < size); 4960 dest[i] = '\0'; 4961 regs[rd] = (uintptr_t)dest; 4962 mstate->dtms_scratch_ptr += size; 4963 mstate->dtms_strtok = addr; 4964 mstate->dtms_strtok_limit = limit; 4965 break; 4966 } 4967 4968 case DIF_SUBR_SUBSTR: { 4969 uintptr_t s = tupregs[0].dttk_value; 4970 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4971 char *d = (char *)mstate->dtms_scratch_ptr; 4972 int64_t index = (int64_t)tupregs[1].dttk_value; 4973 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4974 size_t len = dtrace_strlen((char *)s, size); 4975 int64_t i; 4976 4977 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4978 regs[rd] = 0; 4979 break; 4980 } 4981 4982 if (!DTRACE_INSCRATCH(mstate, size)) { 4983 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4984 regs[rd] = 0; 4985 break; 4986 } 4987 4988 if (nargs <= 2) 4989 remaining = (int64_t)size; 4990 4991 if (index < 0) { 4992 index += len; 4993 4994 if (index < 0 && index + remaining > 0) { 4995 remaining += index; 4996 index = 0; 4997 } 4998 } 4999 5000 if (index >= len || index < 0) { 5001 remaining = 0; 5002 } else if (remaining < 0) { 5003 remaining += len - index; 5004 } else if (index + remaining > size) { 5005 remaining = size - index; 5006 } 5007 5008 for (i = 0; i < remaining; i++) { 5009 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 5010 break; 5011 } 5012 5013 d[i] = '\0'; 5014 5015 mstate->dtms_scratch_ptr += size; 5016 regs[rd] = (uintptr_t)d; 5017 break; 5018 } 5019 5020 case DIF_SUBR_JSON: { 5021 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5022 uintptr_t json = tupregs[0].dttk_value; 5023 size_t jsonlen = dtrace_strlen((char *)json, size); 5024 uintptr_t elem = tupregs[1].dttk_value; 5025 size_t elemlen = dtrace_strlen((char *)elem, size); 5026 5027 char *dest = (char *)mstate->dtms_scratch_ptr; 5028 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1; 5029 char *ee = elemlist; 5030 int nelems = 1; 5031 uintptr_t cur; 5032 5033 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) || 5034 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) { 5035 regs[rd] = 0; 5036 break; 5037 } 5038 5039 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) { 5040 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5041 regs[rd] = 0; 5042 break; 5043 } 5044 5045 /* 5046 * Read the element selector and split it up into a packed list 5047 * of strings. 5048 */ 5049 for (cur = elem; cur < elem + elemlen; cur++) { 5050 char cc = dtrace_load8(cur); 5051 5052 if (cur == elem && cc == '[') { 5053 /* 5054 * If the first element selector key is 5055 * actually an array index then ignore the 5056 * bracket. 5057 */ 5058 continue; 5059 } 5060 5061 if (cc == ']') 5062 continue; 5063 5064 if (cc == '.' || cc == '[') { 5065 nelems++; 5066 cc = '\0'; 5067 } 5068 5069 *ee++ = cc; 5070 } 5071 *ee++ = '\0'; 5072 5073 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist, 5074 nelems, dest)) != 0) 5075 mstate->dtms_scratch_ptr += jsonlen + 1; 5076 break; 5077 } 5078 5079 case DIF_SUBR_TOUPPER: 5080 case DIF_SUBR_TOLOWER: { 5081 uintptr_t s = tupregs[0].dttk_value; 5082 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5083 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5084 size_t len = dtrace_strlen((char *)s, size); 5085 char lower, upper, convert; 5086 int64_t i; 5087 5088 if (subr == DIF_SUBR_TOUPPER) { 5089 lower = 'a'; 5090 upper = 'z'; 5091 convert = 'A'; 5092 } else { 5093 lower = 'A'; 5094 upper = 'Z'; 5095 convert = 'a'; 5096 } 5097 5098 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 5099 regs[rd] = 0; 5100 break; 5101 } 5102 5103 if (!DTRACE_INSCRATCH(mstate, size)) { 5104 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5105 regs[rd] = 0; 5106 break; 5107 } 5108 5109 for (i = 0; i < size - 1; i++) { 5110 if ((c = dtrace_load8(s + i)) == '\0') 5111 break; 5112 5113 if (c >= lower && c <= upper) 5114 c = convert + (c - lower); 5115 5116 dest[i] = c; 5117 } 5118 5119 ASSERT(i < size); 5120 dest[i] = '\0'; 5121 regs[rd] = (uintptr_t)dest; 5122 mstate->dtms_scratch_ptr += size; 5123 break; 5124 } 5125 5126 #ifdef illumos 5127 case DIF_SUBR_GETMAJOR: 5128 #ifdef _LP64 5129 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 5130 #else 5131 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 5132 #endif 5133 break; 5134 5135 case DIF_SUBR_GETMINOR: 5136 #ifdef _LP64 5137 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 5138 #else 5139 regs[rd] = tupregs[0].dttk_value & MAXMIN; 5140 #endif 5141 break; 5142 5143 case DIF_SUBR_DDI_PATHNAME: { 5144 /* 5145 * This one is a galactic mess. We are going to roughly 5146 * emulate ddi_pathname(), but it's made more complicated 5147 * by the fact that we (a) want to include the minor name and 5148 * (b) must proceed iteratively instead of recursively. 5149 */ 5150 uintptr_t dest = mstate->dtms_scratch_ptr; 5151 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5152 char *start = (char *)dest, *end = start + size - 1; 5153 uintptr_t daddr = tupregs[0].dttk_value; 5154 int64_t minor = (int64_t)tupregs[1].dttk_value; 5155 char *s; 5156 int i, len, depth = 0; 5157 5158 /* 5159 * Due to all the pointer jumping we do and context we must 5160 * rely upon, we just mandate that the user must have kernel 5161 * read privileges to use this routine. 5162 */ 5163 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 5164 *flags |= CPU_DTRACE_KPRIV; 5165 *illval = daddr; 5166 regs[rd] = 0; 5167 } 5168 5169 if (!DTRACE_INSCRATCH(mstate, size)) { 5170 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5171 regs[rd] = 0; 5172 break; 5173 } 5174 5175 *end = '\0'; 5176 5177 /* 5178 * We want to have a name for the minor. In order to do this, 5179 * we need to walk the minor list from the devinfo. We want 5180 * to be sure that we don't infinitely walk a circular list, 5181 * so we check for circularity by sending a scout pointer 5182 * ahead two elements for every element that we iterate over; 5183 * if the list is circular, these will ultimately point to the 5184 * same element. You may recognize this little trick as the 5185 * answer to a stupid interview question -- one that always 5186 * seems to be asked by those who had to have it laboriously 5187 * explained to them, and who can't even concisely describe 5188 * the conditions under which one would be forced to resort to 5189 * this technique. Needless to say, those conditions are 5190 * found here -- and probably only here. Is this the only use 5191 * of this infamous trick in shipping, production code? If it 5192 * isn't, it probably should be... 5193 */ 5194 if (minor != -1) { 5195 uintptr_t maddr = dtrace_loadptr(daddr + 5196 offsetof(struct dev_info, devi_minor)); 5197 5198 uintptr_t next = offsetof(struct ddi_minor_data, next); 5199 uintptr_t name = offsetof(struct ddi_minor_data, 5200 d_minor) + offsetof(struct ddi_minor, name); 5201 uintptr_t dev = offsetof(struct ddi_minor_data, 5202 d_minor) + offsetof(struct ddi_minor, dev); 5203 uintptr_t scout; 5204 5205 if (maddr != NULL) 5206 scout = dtrace_loadptr(maddr + next); 5207 5208 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5209 uint64_t m; 5210 #ifdef _LP64 5211 m = dtrace_load64(maddr + dev) & MAXMIN64; 5212 #else 5213 m = dtrace_load32(maddr + dev) & MAXMIN; 5214 #endif 5215 if (m != minor) { 5216 maddr = dtrace_loadptr(maddr + next); 5217 5218 if (scout == NULL) 5219 continue; 5220 5221 scout = dtrace_loadptr(scout + next); 5222 5223 if (scout == NULL) 5224 continue; 5225 5226 scout = dtrace_loadptr(scout + next); 5227 5228 if (scout == NULL) 5229 continue; 5230 5231 if (scout == maddr) { 5232 *flags |= CPU_DTRACE_ILLOP; 5233 break; 5234 } 5235 5236 continue; 5237 } 5238 5239 /* 5240 * We have the minor data. Now we need to 5241 * copy the minor's name into the end of the 5242 * pathname. 5243 */ 5244 s = (char *)dtrace_loadptr(maddr + name); 5245 len = dtrace_strlen(s, size); 5246 5247 if (*flags & CPU_DTRACE_FAULT) 5248 break; 5249 5250 if (len != 0) { 5251 if ((end -= (len + 1)) < start) 5252 break; 5253 5254 *end = ':'; 5255 } 5256 5257 for (i = 1; i <= len; i++) 5258 end[i] = dtrace_load8((uintptr_t)s++); 5259 break; 5260 } 5261 } 5262 5263 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5264 ddi_node_state_t devi_state; 5265 5266 devi_state = dtrace_load32(daddr + 5267 offsetof(struct dev_info, devi_node_state)); 5268 5269 if (*flags & CPU_DTRACE_FAULT) 5270 break; 5271 5272 if (devi_state >= DS_INITIALIZED) { 5273 s = (char *)dtrace_loadptr(daddr + 5274 offsetof(struct dev_info, devi_addr)); 5275 len = dtrace_strlen(s, size); 5276 5277 if (*flags & CPU_DTRACE_FAULT) 5278 break; 5279 5280 if (len != 0) { 5281 if ((end -= (len + 1)) < start) 5282 break; 5283 5284 *end = '@'; 5285 } 5286 5287 for (i = 1; i <= len; i++) 5288 end[i] = dtrace_load8((uintptr_t)s++); 5289 } 5290 5291 /* 5292 * Now for the node name... 5293 */ 5294 s = (char *)dtrace_loadptr(daddr + 5295 offsetof(struct dev_info, devi_node_name)); 5296 5297 daddr = dtrace_loadptr(daddr + 5298 offsetof(struct dev_info, devi_parent)); 5299 5300 /* 5301 * If our parent is NULL (that is, if we're the root 5302 * node), we're going to use the special path 5303 * "devices". 5304 */ 5305 if (daddr == 0) 5306 s = "devices"; 5307 5308 len = dtrace_strlen(s, size); 5309 if (*flags & CPU_DTRACE_FAULT) 5310 break; 5311 5312 if ((end -= (len + 1)) < start) 5313 break; 5314 5315 for (i = 1; i <= len; i++) 5316 end[i] = dtrace_load8((uintptr_t)s++); 5317 *end = '/'; 5318 5319 if (depth++ > dtrace_devdepth_max) { 5320 *flags |= CPU_DTRACE_ILLOP; 5321 break; 5322 } 5323 } 5324 5325 if (end < start) 5326 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5327 5328 if (daddr == 0) { 5329 regs[rd] = (uintptr_t)end; 5330 mstate->dtms_scratch_ptr += size; 5331 } 5332 5333 break; 5334 } 5335 #endif 5336 5337 case DIF_SUBR_STRJOIN: { 5338 char *d = (char *)mstate->dtms_scratch_ptr; 5339 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5340 uintptr_t s1 = tupregs[0].dttk_value; 5341 uintptr_t s2 = tupregs[1].dttk_value; 5342 int i = 0, j = 0; 5343 size_t lim1, lim2; 5344 char c; 5345 5346 if (!dtrace_strcanload(s1, size, &lim1, mstate, vstate) || 5347 !dtrace_strcanload(s2, size, &lim2, mstate, vstate)) { 5348 regs[rd] = 0; 5349 break; 5350 } 5351 5352 if (!DTRACE_INSCRATCH(mstate, size)) { 5353 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5354 regs[rd] = 0; 5355 break; 5356 } 5357 5358 for (;;) { 5359 if (i >= size) { 5360 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5361 regs[rd] = 0; 5362 break; 5363 } 5364 c = (i >= lim1) ? '\0' : dtrace_load8(s1++); 5365 if ((d[i++] = c) == '\0') { 5366 i--; 5367 break; 5368 } 5369 } 5370 5371 for (;;) { 5372 if (i >= size) { 5373 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5374 regs[rd] = 0; 5375 break; 5376 } 5377 5378 c = (j++ >= lim2) ? '\0' : dtrace_load8(s2++); 5379 if ((d[i++] = c) == '\0') 5380 break; 5381 } 5382 5383 if (i < size) { 5384 mstate->dtms_scratch_ptr += i; 5385 regs[rd] = (uintptr_t)d; 5386 } 5387 5388 break; 5389 } 5390 5391 case DIF_SUBR_STRTOLL: { 5392 uintptr_t s = tupregs[0].dttk_value; 5393 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5394 size_t lim; 5395 int base = 10; 5396 5397 if (nargs > 1) { 5398 if ((base = tupregs[1].dttk_value) <= 1 || 5399 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5400 *flags |= CPU_DTRACE_ILLOP; 5401 break; 5402 } 5403 } 5404 5405 if (!dtrace_strcanload(s, size, &lim, mstate, vstate)) { 5406 regs[rd] = INT64_MIN; 5407 break; 5408 } 5409 5410 regs[rd] = dtrace_strtoll((char *)s, base, lim); 5411 break; 5412 } 5413 5414 case DIF_SUBR_LLTOSTR: { 5415 int64_t i = (int64_t)tupregs[0].dttk_value; 5416 uint64_t val, digit; 5417 uint64_t size = 65; /* enough room for 2^64 in binary */ 5418 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 5419 int base = 10; 5420 5421 if (nargs > 1) { 5422 if ((base = tupregs[1].dttk_value) <= 1 || 5423 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5424 *flags |= CPU_DTRACE_ILLOP; 5425 break; 5426 } 5427 } 5428 5429 val = (base == 10 && i < 0) ? i * -1 : i; 5430 5431 if (!DTRACE_INSCRATCH(mstate, size)) { 5432 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5433 regs[rd] = 0; 5434 break; 5435 } 5436 5437 for (*end-- = '\0'; val; val /= base) { 5438 if ((digit = val % base) <= '9' - '0') { 5439 *end-- = '0' + digit; 5440 } else { 5441 *end-- = 'a' + (digit - ('9' - '0') - 1); 5442 } 5443 } 5444 5445 if (i == 0 && base == 16) 5446 *end-- = '0'; 5447 5448 if (base == 16) 5449 *end-- = 'x'; 5450 5451 if (i == 0 || base == 8 || base == 16) 5452 *end-- = '0'; 5453 5454 if (i < 0 && base == 10) 5455 *end-- = '-'; 5456 5457 regs[rd] = (uintptr_t)end + 1; 5458 mstate->dtms_scratch_ptr += size; 5459 break; 5460 } 5461 5462 case DIF_SUBR_HTONS: 5463 case DIF_SUBR_NTOHS: 5464 #if BYTE_ORDER == BIG_ENDIAN 5465 regs[rd] = (uint16_t)tupregs[0].dttk_value; 5466 #else 5467 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 5468 #endif 5469 break; 5470 5471 5472 case DIF_SUBR_HTONL: 5473 case DIF_SUBR_NTOHL: 5474 #if BYTE_ORDER == BIG_ENDIAN 5475 regs[rd] = (uint32_t)tupregs[0].dttk_value; 5476 #else 5477 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 5478 #endif 5479 break; 5480 5481 5482 case DIF_SUBR_HTONLL: 5483 case DIF_SUBR_NTOHLL: 5484 #if BYTE_ORDER == BIG_ENDIAN 5485 regs[rd] = (uint64_t)tupregs[0].dttk_value; 5486 #else 5487 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 5488 #endif 5489 break; 5490 5491 5492 case DIF_SUBR_DIRNAME: 5493 case DIF_SUBR_BASENAME: { 5494 char *dest = (char *)mstate->dtms_scratch_ptr; 5495 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5496 uintptr_t src = tupregs[0].dttk_value; 5497 int i, j, len = dtrace_strlen((char *)src, size); 5498 int lastbase = -1, firstbase = -1, lastdir = -1; 5499 int start, end; 5500 5501 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 5502 regs[rd] = 0; 5503 break; 5504 } 5505 5506 if (!DTRACE_INSCRATCH(mstate, size)) { 5507 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5508 regs[rd] = 0; 5509 break; 5510 } 5511 5512 /* 5513 * The basename and dirname for a zero-length string is 5514 * defined to be "." 5515 */ 5516 if (len == 0) { 5517 len = 1; 5518 src = (uintptr_t)"."; 5519 } 5520 5521 /* 5522 * Start from the back of the string, moving back toward the 5523 * front until we see a character that isn't a slash. That 5524 * character is the last character in the basename. 5525 */ 5526 for (i = len - 1; i >= 0; i--) { 5527 if (dtrace_load8(src + i) != '/') 5528 break; 5529 } 5530 5531 if (i >= 0) 5532 lastbase = i; 5533 5534 /* 5535 * Starting from the last character in the basename, move 5536 * towards the front until we find a slash. The character 5537 * that we processed immediately before that is the first 5538 * character in the basename. 5539 */ 5540 for (; i >= 0; i--) { 5541 if (dtrace_load8(src + i) == '/') 5542 break; 5543 } 5544 5545 if (i >= 0) 5546 firstbase = i + 1; 5547 5548 /* 5549 * Now keep going until we find a non-slash character. That 5550 * character is the last character in the dirname. 5551 */ 5552 for (; i >= 0; i--) { 5553 if (dtrace_load8(src + i) != '/') 5554 break; 5555 } 5556 5557 if (i >= 0) 5558 lastdir = i; 5559 5560 ASSERT(!(lastbase == -1 && firstbase != -1)); 5561 ASSERT(!(firstbase == -1 && lastdir != -1)); 5562 5563 if (lastbase == -1) { 5564 /* 5565 * We didn't find a non-slash character. We know that 5566 * the length is non-zero, so the whole string must be 5567 * slashes. In either the dirname or the basename 5568 * case, we return '/'. 5569 */ 5570 ASSERT(firstbase == -1); 5571 firstbase = lastbase = lastdir = 0; 5572 } 5573 5574 if (firstbase == -1) { 5575 /* 5576 * The entire string consists only of a basename 5577 * component. If we're looking for dirname, we need 5578 * to change our string to be just "."; if we're 5579 * looking for a basename, we'll just set the first 5580 * character of the basename to be 0. 5581 */ 5582 if (subr == DIF_SUBR_DIRNAME) { 5583 ASSERT(lastdir == -1); 5584 src = (uintptr_t)"."; 5585 lastdir = 0; 5586 } else { 5587 firstbase = 0; 5588 } 5589 } 5590 5591 if (subr == DIF_SUBR_DIRNAME) { 5592 if (lastdir == -1) { 5593 /* 5594 * We know that we have a slash in the name -- 5595 * or lastdir would be set to 0, above. And 5596 * because lastdir is -1, we know that this 5597 * slash must be the first character. (That 5598 * is, the full string must be of the form 5599 * "/basename".) In this case, the last 5600 * character of the directory name is 0. 5601 */ 5602 lastdir = 0; 5603 } 5604 5605 start = 0; 5606 end = lastdir; 5607 } else { 5608 ASSERT(subr == DIF_SUBR_BASENAME); 5609 ASSERT(firstbase != -1 && lastbase != -1); 5610 start = firstbase; 5611 end = lastbase; 5612 } 5613 5614 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 5615 dest[j] = dtrace_load8(src + i); 5616 5617 dest[j] = '\0'; 5618 regs[rd] = (uintptr_t)dest; 5619 mstate->dtms_scratch_ptr += size; 5620 break; 5621 } 5622 5623 case DIF_SUBR_GETF: { 5624 uintptr_t fd = tupregs[0].dttk_value; 5625 struct filedesc *fdp; 5626 file_t *fp; 5627 5628 if (!dtrace_priv_proc(state)) { 5629 regs[rd] = 0; 5630 break; 5631 } 5632 fdp = curproc->p_fd; 5633 FILEDESC_SLOCK(fdp); 5634 fp = fget_locked(fdp, fd); 5635 mstate->dtms_getf = fp; 5636 regs[rd] = (uintptr_t)fp; 5637 FILEDESC_SUNLOCK(fdp); 5638 break; 5639 } 5640 5641 case DIF_SUBR_CLEANPATH: { 5642 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5643 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5644 uintptr_t src = tupregs[0].dttk_value; 5645 size_t lim; 5646 int i = 0, j = 0; 5647 #ifdef illumos 5648 zone_t *z; 5649 #endif 5650 5651 if (!dtrace_strcanload(src, size, &lim, mstate, vstate)) { 5652 regs[rd] = 0; 5653 break; 5654 } 5655 5656 if (!DTRACE_INSCRATCH(mstate, size)) { 5657 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5658 regs[rd] = 0; 5659 break; 5660 } 5661 5662 /* 5663 * Move forward, loading each character. 5664 */ 5665 do { 5666 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5667 next: 5668 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 5669 break; 5670 5671 if (c != '/') { 5672 dest[j++] = c; 5673 continue; 5674 } 5675 5676 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5677 5678 if (c == '/') { 5679 /* 5680 * We have two slashes -- we can just advance 5681 * to the next character. 5682 */ 5683 goto next; 5684 } 5685 5686 if (c != '.') { 5687 /* 5688 * This is not "." and it's not ".." -- we can 5689 * just store the "/" and this character and 5690 * drive on. 5691 */ 5692 dest[j++] = '/'; 5693 dest[j++] = c; 5694 continue; 5695 } 5696 5697 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5698 5699 if (c == '/') { 5700 /* 5701 * This is a "/./" component. We're not going 5702 * to store anything in the destination buffer; 5703 * we're just going to go to the next component. 5704 */ 5705 goto next; 5706 } 5707 5708 if (c != '.') { 5709 /* 5710 * This is not ".." -- we can just store the 5711 * "/." and this character and continue 5712 * processing. 5713 */ 5714 dest[j++] = '/'; 5715 dest[j++] = '.'; 5716 dest[j++] = c; 5717 continue; 5718 } 5719 5720 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5721 5722 if (c != '/' && c != '\0') { 5723 /* 5724 * This is not ".." -- it's "..[mumble]". 5725 * We'll store the "/.." and this character 5726 * and continue processing. 5727 */ 5728 dest[j++] = '/'; 5729 dest[j++] = '.'; 5730 dest[j++] = '.'; 5731 dest[j++] = c; 5732 continue; 5733 } 5734 5735 /* 5736 * This is "/../" or "/..\0". We need to back up 5737 * our destination pointer until we find a "/". 5738 */ 5739 i--; 5740 while (j != 0 && dest[--j] != '/') 5741 continue; 5742 5743 if (c == '\0') 5744 dest[++j] = '/'; 5745 } while (c != '\0'); 5746 5747 dest[j] = '\0'; 5748 5749 #ifdef illumos 5750 if (mstate->dtms_getf != NULL && 5751 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 5752 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 5753 /* 5754 * If we've done a getf() as a part of this ECB and we 5755 * don't have kernel access (and we're not in the global 5756 * zone), check if the path we cleaned up begins with 5757 * the zone's root path, and trim it off if so. Note 5758 * that this is an output cleanliness issue, not a 5759 * security issue: knowing one's zone root path does 5760 * not enable privilege escalation. 5761 */ 5762 if (strstr(dest, z->zone_rootpath) == dest) 5763 dest += strlen(z->zone_rootpath) - 1; 5764 } 5765 #endif 5766 5767 regs[rd] = (uintptr_t)dest; 5768 mstate->dtms_scratch_ptr += size; 5769 break; 5770 } 5771 5772 case DIF_SUBR_INET_NTOA: 5773 case DIF_SUBR_INET_NTOA6: 5774 case DIF_SUBR_INET_NTOP: { 5775 size_t size; 5776 int af, argi, i; 5777 char *base, *end; 5778 5779 if (subr == DIF_SUBR_INET_NTOP) { 5780 af = (int)tupregs[0].dttk_value; 5781 argi = 1; 5782 } else { 5783 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 5784 argi = 0; 5785 } 5786 5787 if (af == AF_INET) { 5788 ipaddr_t ip4; 5789 uint8_t *ptr8, val; 5790 5791 if (!dtrace_canload(tupregs[argi].dttk_value, 5792 sizeof (ipaddr_t), mstate, vstate)) { 5793 regs[rd] = 0; 5794 break; 5795 } 5796 5797 /* 5798 * Safely load the IPv4 address. 5799 */ 5800 ip4 = dtrace_load32(tupregs[argi].dttk_value); 5801 5802 /* 5803 * Check an IPv4 string will fit in scratch. 5804 */ 5805 size = INET_ADDRSTRLEN; 5806 if (!DTRACE_INSCRATCH(mstate, size)) { 5807 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5808 regs[rd] = 0; 5809 break; 5810 } 5811 base = (char *)mstate->dtms_scratch_ptr; 5812 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5813 5814 /* 5815 * Stringify as a dotted decimal quad. 5816 */ 5817 *end-- = '\0'; 5818 ptr8 = (uint8_t *)&ip4; 5819 for (i = 3; i >= 0; i--) { 5820 val = ptr8[i]; 5821 5822 if (val == 0) { 5823 *end-- = '0'; 5824 } else { 5825 for (; val; val /= 10) { 5826 *end-- = '0' + (val % 10); 5827 } 5828 } 5829 5830 if (i > 0) 5831 *end-- = '.'; 5832 } 5833 ASSERT(end + 1 >= base); 5834 5835 } else if (af == AF_INET6) { 5836 struct in6_addr ip6; 5837 int firstzero, tryzero, numzero, v6end; 5838 uint16_t val; 5839 const char digits[] = "0123456789abcdef"; 5840 5841 /* 5842 * Stringify using RFC 1884 convention 2 - 16 bit 5843 * hexadecimal values with a zero-run compression. 5844 * Lower case hexadecimal digits are used. 5845 * eg, fe80::214:4fff:fe0b:76c8. 5846 * The IPv4 embedded form is returned for inet_ntop, 5847 * just the IPv4 string is returned for inet_ntoa6. 5848 */ 5849 5850 if (!dtrace_canload(tupregs[argi].dttk_value, 5851 sizeof (struct in6_addr), mstate, vstate)) { 5852 regs[rd] = 0; 5853 break; 5854 } 5855 5856 /* 5857 * Safely load the IPv6 address. 5858 */ 5859 dtrace_bcopy( 5860 (void *)(uintptr_t)tupregs[argi].dttk_value, 5861 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 5862 5863 /* 5864 * Check an IPv6 string will fit in scratch. 5865 */ 5866 size = INET6_ADDRSTRLEN; 5867 if (!DTRACE_INSCRATCH(mstate, size)) { 5868 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5869 regs[rd] = 0; 5870 break; 5871 } 5872 base = (char *)mstate->dtms_scratch_ptr; 5873 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5874 *end-- = '\0'; 5875 5876 /* 5877 * Find the longest run of 16 bit zero values 5878 * for the single allowed zero compression - "::". 5879 */ 5880 firstzero = -1; 5881 tryzero = -1; 5882 numzero = 1; 5883 for (i = 0; i < sizeof (struct in6_addr); i++) { 5884 #ifdef illumos 5885 if (ip6._S6_un._S6_u8[i] == 0 && 5886 #else 5887 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5888 #endif 5889 tryzero == -1 && i % 2 == 0) { 5890 tryzero = i; 5891 continue; 5892 } 5893 5894 if (tryzero != -1 && 5895 #ifdef illumos 5896 (ip6._S6_un._S6_u8[i] != 0 || 5897 #else 5898 (ip6.__u6_addr.__u6_addr8[i] != 0 || 5899 #endif 5900 i == sizeof (struct in6_addr) - 1)) { 5901 5902 if (i - tryzero <= numzero) { 5903 tryzero = -1; 5904 continue; 5905 } 5906 5907 firstzero = tryzero; 5908 numzero = i - i % 2 - tryzero; 5909 tryzero = -1; 5910 5911 #ifdef illumos 5912 if (ip6._S6_un._S6_u8[i] == 0 && 5913 #else 5914 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5915 #endif 5916 i == sizeof (struct in6_addr) - 1) 5917 numzero += 2; 5918 } 5919 } 5920 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 5921 5922 /* 5923 * Check for an IPv4 embedded address. 5924 */ 5925 v6end = sizeof (struct in6_addr) - 2; 5926 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 5927 IN6_IS_ADDR_V4COMPAT(&ip6)) { 5928 for (i = sizeof (struct in6_addr) - 1; 5929 i >= DTRACE_V4MAPPED_OFFSET; i--) { 5930 ASSERT(end >= base); 5931 5932 #ifdef illumos 5933 val = ip6._S6_un._S6_u8[i]; 5934 #else 5935 val = ip6.__u6_addr.__u6_addr8[i]; 5936 #endif 5937 5938 if (val == 0) { 5939 *end-- = '0'; 5940 } else { 5941 for (; val; val /= 10) { 5942 *end-- = '0' + val % 10; 5943 } 5944 } 5945 5946 if (i > DTRACE_V4MAPPED_OFFSET) 5947 *end-- = '.'; 5948 } 5949 5950 if (subr == DIF_SUBR_INET_NTOA6) 5951 goto inetout; 5952 5953 /* 5954 * Set v6end to skip the IPv4 address that 5955 * we have already stringified. 5956 */ 5957 v6end = 10; 5958 } 5959 5960 /* 5961 * Build the IPv6 string by working through the 5962 * address in reverse. 5963 */ 5964 for (i = v6end; i >= 0; i -= 2) { 5965 ASSERT(end >= base); 5966 5967 if (i == firstzero + numzero - 2) { 5968 *end-- = ':'; 5969 *end-- = ':'; 5970 i -= numzero - 2; 5971 continue; 5972 } 5973 5974 if (i < 14 && i != firstzero - 2) 5975 *end-- = ':'; 5976 5977 #ifdef illumos 5978 val = (ip6._S6_un._S6_u8[i] << 8) + 5979 ip6._S6_un._S6_u8[i + 1]; 5980 #else 5981 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 5982 ip6.__u6_addr.__u6_addr8[i + 1]; 5983 #endif 5984 5985 if (val == 0) { 5986 *end-- = '0'; 5987 } else { 5988 for (; val; val /= 16) { 5989 *end-- = digits[val % 16]; 5990 } 5991 } 5992 } 5993 ASSERT(end + 1 >= base); 5994 5995 } else { 5996 /* 5997 * The user didn't use AH_INET or AH_INET6. 5998 */ 5999 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 6000 regs[rd] = 0; 6001 break; 6002 } 6003 6004 inetout: regs[rd] = (uintptr_t)end + 1; 6005 mstate->dtms_scratch_ptr += size; 6006 break; 6007 } 6008 6009 case DIF_SUBR_MEMREF: { 6010 uintptr_t size = 2 * sizeof(uintptr_t); 6011 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 6012 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 6013 6014 /* address and length */ 6015 memref[0] = tupregs[0].dttk_value; 6016 memref[1] = tupregs[1].dttk_value; 6017 6018 regs[rd] = (uintptr_t) memref; 6019 mstate->dtms_scratch_ptr += scratch_size; 6020 break; 6021 } 6022 6023 #ifndef illumos 6024 case DIF_SUBR_MEMSTR: { 6025 char *str = (char *)mstate->dtms_scratch_ptr; 6026 uintptr_t mem = tupregs[0].dttk_value; 6027 char c = tupregs[1].dttk_value; 6028 size_t size = tupregs[2].dttk_value; 6029 uint8_t n; 6030 int i; 6031 6032 regs[rd] = 0; 6033 6034 if (size == 0) 6035 break; 6036 6037 if (!dtrace_canload(mem, size - 1, mstate, vstate)) 6038 break; 6039 6040 if (!DTRACE_INSCRATCH(mstate, size)) { 6041 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6042 break; 6043 } 6044 6045 if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) { 6046 *flags |= CPU_DTRACE_ILLOP; 6047 break; 6048 } 6049 6050 for (i = 0; i < size - 1; i++) { 6051 n = dtrace_load8(mem++); 6052 str[i] = (n == 0) ? c : n; 6053 } 6054 str[size - 1] = 0; 6055 6056 regs[rd] = (uintptr_t)str; 6057 mstate->dtms_scratch_ptr += size; 6058 break; 6059 } 6060 #endif 6061 } 6062 } 6063 6064 /* 6065 * Emulate the execution of DTrace IR instructions specified by the given 6066 * DIF object. This function is deliberately void of assertions as all of 6067 * the necessary checks are handled by a call to dtrace_difo_validate(). 6068 */ 6069 static uint64_t 6070 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 6071 dtrace_vstate_t *vstate, dtrace_state_t *state) 6072 { 6073 const dif_instr_t *text = difo->dtdo_buf; 6074 const uint_t textlen = difo->dtdo_len; 6075 const char *strtab = difo->dtdo_strtab; 6076 const uint64_t *inttab = difo->dtdo_inttab; 6077 6078 uint64_t rval = 0; 6079 dtrace_statvar_t *svar; 6080 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 6081 dtrace_difv_t *v; 6082 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 6083 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 6084 6085 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 6086 uint64_t regs[DIF_DIR_NREGS]; 6087 uint64_t *tmp; 6088 6089 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 6090 int64_t cc_r; 6091 uint_t pc = 0, id, opc = 0; 6092 uint8_t ttop = 0; 6093 dif_instr_t instr; 6094 uint_t r1, r2, rd; 6095 6096 /* 6097 * We stash the current DIF object into the machine state: we need it 6098 * for subsequent access checking. 6099 */ 6100 mstate->dtms_difo = difo; 6101 6102 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 6103 6104 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 6105 opc = pc; 6106 6107 instr = text[pc++]; 6108 r1 = DIF_INSTR_R1(instr); 6109 r2 = DIF_INSTR_R2(instr); 6110 rd = DIF_INSTR_RD(instr); 6111 6112 switch (DIF_INSTR_OP(instr)) { 6113 case DIF_OP_OR: 6114 regs[rd] = regs[r1] | regs[r2]; 6115 break; 6116 case DIF_OP_XOR: 6117 regs[rd] = regs[r1] ^ regs[r2]; 6118 break; 6119 case DIF_OP_AND: 6120 regs[rd] = regs[r1] & regs[r2]; 6121 break; 6122 case DIF_OP_SLL: 6123 regs[rd] = regs[r1] << regs[r2]; 6124 break; 6125 case DIF_OP_SRL: 6126 regs[rd] = regs[r1] >> regs[r2]; 6127 break; 6128 case DIF_OP_SUB: 6129 regs[rd] = regs[r1] - regs[r2]; 6130 break; 6131 case DIF_OP_ADD: 6132 regs[rd] = regs[r1] + regs[r2]; 6133 break; 6134 case DIF_OP_MUL: 6135 regs[rd] = regs[r1] * regs[r2]; 6136 break; 6137 case DIF_OP_SDIV: 6138 if (regs[r2] == 0) { 6139 regs[rd] = 0; 6140 *flags |= CPU_DTRACE_DIVZERO; 6141 } else { 6142 regs[rd] = (int64_t)regs[r1] / 6143 (int64_t)regs[r2]; 6144 } 6145 break; 6146 6147 case DIF_OP_UDIV: 6148 if (regs[r2] == 0) { 6149 regs[rd] = 0; 6150 *flags |= CPU_DTRACE_DIVZERO; 6151 } else { 6152 regs[rd] = regs[r1] / regs[r2]; 6153 } 6154 break; 6155 6156 case DIF_OP_SREM: 6157 if (regs[r2] == 0) { 6158 regs[rd] = 0; 6159 *flags |= CPU_DTRACE_DIVZERO; 6160 } else { 6161 regs[rd] = (int64_t)regs[r1] % 6162 (int64_t)regs[r2]; 6163 } 6164 break; 6165 6166 case DIF_OP_UREM: 6167 if (regs[r2] == 0) { 6168 regs[rd] = 0; 6169 *flags |= CPU_DTRACE_DIVZERO; 6170 } else { 6171 regs[rd] = regs[r1] % regs[r2]; 6172 } 6173 break; 6174 6175 case DIF_OP_NOT: 6176 regs[rd] = ~regs[r1]; 6177 break; 6178 case DIF_OP_MOV: 6179 regs[rd] = regs[r1]; 6180 break; 6181 case DIF_OP_CMP: 6182 cc_r = regs[r1] - regs[r2]; 6183 cc_n = cc_r < 0; 6184 cc_z = cc_r == 0; 6185 cc_v = 0; 6186 cc_c = regs[r1] < regs[r2]; 6187 break; 6188 case DIF_OP_TST: 6189 cc_n = cc_v = cc_c = 0; 6190 cc_z = regs[r1] == 0; 6191 break; 6192 case DIF_OP_BA: 6193 pc = DIF_INSTR_LABEL(instr); 6194 break; 6195 case DIF_OP_BE: 6196 if (cc_z) 6197 pc = DIF_INSTR_LABEL(instr); 6198 break; 6199 case DIF_OP_BNE: 6200 if (cc_z == 0) 6201 pc = DIF_INSTR_LABEL(instr); 6202 break; 6203 case DIF_OP_BG: 6204 if ((cc_z | (cc_n ^ cc_v)) == 0) 6205 pc = DIF_INSTR_LABEL(instr); 6206 break; 6207 case DIF_OP_BGU: 6208 if ((cc_c | cc_z) == 0) 6209 pc = DIF_INSTR_LABEL(instr); 6210 break; 6211 case DIF_OP_BGE: 6212 if ((cc_n ^ cc_v) == 0) 6213 pc = DIF_INSTR_LABEL(instr); 6214 break; 6215 case DIF_OP_BGEU: 6216 if (cc_c == 0) 6217 pc = DIF_INSTR_LABEL(instr); 6218 break; 6219 case DIF_OP_BL: 6220 if (cc_n ^ cc_v) 6221 pc = DIF_INSTR_LABEL(instr); 6222 break; 6223 case DIF_OP_BLU: 6224 if (cc_c) 6225 pc = DIF_INSTR_LABEL(instr); 6226 break; 6227 case DIF_OP_BLE: 6228 if (cc_z | (cc_n ^ cc_v)) 6229 pc = DIF_INSTR_LABEL(instr); 6230 break; 6231 case DIF_OP_BLEU: 6232 if (cc_c | cc_z) 6233 pc = DIF_INSTR_LABEL(instr); 6234 break; 6235 case DIF_OP_RLDSB: 6236 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6237 break; 6238 /*FALLTHROUGH*/ 6239 case DIF_OP_LDSB: 6240 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 6241 break; 6242 case DIF_OP_RLDSH: 6243 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6244 break; 6245 /*FALLTHROUGH*/ 6246 case DIF_OP_LDSH: 6247 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 6248 break; 6249 case DIF_OP_RLDSW: 6250 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6251 break; 6252 /*FALLTHROUGH*/ 6253 case DIF_OP_LDSW: 6254 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 6255 break; 6256 case DIF_OP_RLDUB: 6257 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6258 break; 6259 /*FALLTHROUGH*/ 6260 case DIF_OP_LDUB: 6261 regs[rd] = dtrace_load8(regs[r1]); 6262 break; 6263 case DIF_OP_RLDUH: 6264 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6265 break; 6266 /*FALLTHROUGH*/ 6267 case DIF_OP_LDUH: 6268 regs[rd] = dtrace_load16(regs[r1]); 6269 break; 6270 case DIF_OP_RLDUW: 6271 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6272 break; 6273 /*FALLTHROUGH*/ 6274 case DIF_OP_LDUW: 6275 regs[rd] = dtrace_load32(regs[r1]); 6276 break; 6277 case DIF_OP_RLDX: 6278 if (!dtrace_canload(regs[r1], 8, mstate, vstate)) 6279 break; 6280 /*FALLTHROUGH*/ 6281 case DIF_OP_LDX: 6282 regs[rd] = dtrace_load64(regs[r1]); 6283 break; 6284 case DIF_OP_ULDSB: 6285 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6286 regs[rd] = (int8_t) 6287 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6288 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6289 break; 6290 case DIF_OP_ULDSH: 6291 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6292 regs[rd] = (int16_t) 6293 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6294 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6295 break; 6296 case DIF_OP_ULDSW: 6297 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6298 regs[rd] = (int32_t) 6299 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6300 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6301 break; 6302 case DIF_OP_ULDUB: 6303 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6304 regs[rd] = 6305 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6306 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6307 break; 6308 case DIF_OP_ULDUH: 6309 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6310 regs[rd] = 6311 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6312 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6313 break; 6314 case DIF_OP_ULDUW: 6315 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6316 regs[rd] = 6317 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6318 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6319 break; 6320 case DIF_OP_ULDX: 6321 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6322 regs[rd] = 6323 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 6324 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6325 break; 6326 case DIF_OP_RET: 6327 rval = regs[rd]; 6328 pc = textlen; 6329 break; 6330 case DIF_OP_NOP: 6331 break; 6332 case DIF_OP_SETX: 6333 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 6334 break; 6335 case DIF_OP_SETS: 6336 regs[rd] = (uint64_t)(uintptr_t) 6337 (strtab + DIF_INSTR_STRING(instr)); 6338 break; 6339 case DIF_OP_SCMP: { 6340 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 6341 uintptr_t s1 = regs[r1]; 6342 uintptr_t s2 = regs[r2]; 6343 size_t lim1, lim2; 6344 6345 if (s1 != 0 && 6346 !dtrace_strcanload(s1, sz, &lim1, mstate, vstate)) 6347 break; 6348 if (s2 != 0 && 6349 !dtrace_strcanload(s2, sz, &lim2, mstate, vstate)) 6350 break; 6351 6352 cc_r = dtrace_strncmp((char *)s1, (char *)s2, 6353 MIN(lim1, lim2)); 6354 6355 cc_n = cc_r < 0; 6356 cc_z = cc_r == 0; 6357 cc_v = cc_c = 0; 6358 break; 6359 } 6360 case DIF_OP_LDGA: 6361 regs[rd] = dtrace_dif_variable(mstate, state, 6362 r1, regs[r2]); 6363 break; 6364 case DIF_OP_LDGS: 6365 id = DIF_INSTR_VAR(instr); 6366 6367 if (id >= DIF_VAR_OTHER_UBASE) { 6368 uintptr_t a; 6369 6370 id -= DIF_VAR_OTHER_UBASE; 6371 svar = vstate->dtvs_globals[id]; 6372 ASSERT(svar != NULL); 6373 v = &svar->dtsv_var; 6374 6375 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 6376 regs[rd] = svar->dtsv_data; 6377 break; 6378 } 6379 6380 a = (uintptr_t)svar->dtsv_data; 6381 6382 if (*(uint8_t *)a == UINT8_MAX) { 6383 /* 6384 * If the 0th byte is set to UINT8_MAX 6385 * then this is to be treated as a 6386 * reference to a NULL variable. 6387 */ 6388 regs[rd] = 0; 6389 } else { 6390 regs[rd] = a + sizeof (uint64_t); 6391 } 6392 6393 break; 6394 } 6395 6396 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 6397 break; 6398 6399 case DIF_OP_STGS: 6400 id = DIF_INSTR_VAR(instr); 6401 6402 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6403 id -= DIF_VAR_OTHER_UBASE; 6404 6405 VERIFY(id < vstate->dtvs_nglobals); 6406 svar = vstate->dtvs_globals[id]; 6407 ASSERT(svar != NULL); 6408 v = &svar->dtsv_var; 6409 6410 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6411 uintptr_t a = (uintptr_t)svar->dtsv_data; 6412 size_t lim; 6413 6414 ASSERT(a != 0); 6415 ASSERT(svar->dtsv_size != 0); 6416 6417 if (regs[rd] == 0) { 6418 *(uint8_t *)a = UINT8_MAX; 6419 break; 6420 } else { 6421 *(uint8_t *)a = 0; 6422 a += sizeof (uint64_t); 6423 } 6424 if (!dtrace_vcanload( 6425 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6426 &lim, mstate, vstate)) 6427 break; 6428 6429 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6430 (void *)a, &v->dtdv_type, lim); 6431 break; 6432 } 6433 6434 svar->dtsv_data = regs[rd]; 6435 break; 6436 6437 case DIF_OP_LDTA: 6438 /* 6439 * There are no DTrace built-in thread-local arrays at 6440 * present. This opcode is saved for future work. 6441 */ 6442 *flags |= CPU_DTRACE_ILLOP; 6443 regs[rd] = 0; 6444 break; 6445 6446 case DIF_OP_LDLS: 6447 id = DIF_INSTR_VAR(instr); 6448 6449 if (id < DIF_VAR_OTHER_UBASE) { 6450 /* 6451 * For now, this has no meaning. 6452 */ 6453 regs[rd] = 0; 6454 break; 6455 } 6456 6457 id -= DIF_VAR_OTHER_UBASE; 6458 6459 ASSERT(id < vstate->dtvs_nlocals); 6460 ASSERT(vstate->dtvs_locals != NULL); 6461 6462 svar = vstate->dtvs_locals[id]; 6463 ASSERT(svar != NULL); 6464 v = &svar->dtsv_var; 6465 6466 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6467 uintptr_t a = (uintptr_t)svar->dtsv_data; 6468 size_t sz = v->dtdv_type.dtdt_size; 6469 size_t lim; 6470 6471 sz += sizeof (uint64_t); 6472 ASSERT(svar->dtsv_size == NCPU * sz); 6473 a += curcpu * sz; 6474 6475 if (*(uint8_t *)a == UINT8_MAX) { 6476 /* 6477 * If the 0th byte is set to UINT8_MAX 6478 * then this is to be treated as a 6479 * reference to a NULL variable. 6480 */ 6481 regs[rd] = 0; 6482 } else { 6483 regs[rd] = a + sizeof (uint64_t); 6484 } 6485 6486 break; 6487 } 6488 6489 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6490 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6491 regs[rd] = tmp[curcpu]; 6492 break; 6493 6494 case DIF_OP_STLS: 6495 id = DIF_INSTR_VAR(instr); 6496 6497 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6498 id -= DIF_VAR_OTHER_UBASE; 6499 VERIFY(id < vstate->dtvs_nlocals); 6500 6501 ASSERT(vstate->dtvs_locals != NULL); 6502 svar = vstate->dtvs_locals[id]; 6503 ASSERT(svar != NULL); 6504 v = &svar->dtsv_var; 6505 6506 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6507 uintptr_t a = (uintptr_t)svar->dtsv_data; 6508 size_t sz = v->dtdv_type.dtdt_size; 6509 size_t lim; 6510 6511 sz += sizeof (uint64_t); 6512 ASSERT(svar->dtsv_size == NCPU * sz); 6513 a += curcpu * sz; 6514 6515 if (regs[rd] == 0) { 6516 *(uint8_t *)a = UINT8_MAX; 6517 break; 6518 } else { 6519 *(uint8_t *)a = 0; 6520 a += sizeof (uint64_t); 6521 } 6522 6523 if (!dtrace_vcanload( 6524 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6525 &lim, mstate, vstate)) 6526 break; 6527 6528 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6529 (void *)a, &v->dtdv_type, lim); 6530 break; 6531 } 6532 6533 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6534 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6535 tmp[curcpu] = regs[rd]; 6536 break; 6537 6538 case DIF_OP_LDTS: { 6539 dtrace_dynvar_t *dvar; 6540 dtrace_key_t *key; 6541 6542 id = DIF_INSTR_VAR(instr); 6543 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6544 id -= DIF_VAR_OTHER_UBASE; 6545 v = &vstate->dtvs_tlocals[id]; 6546 6547 key = &tupregs[DIF_DTR_NREGS]; 6548 key[0].dttk_value = (uint64_t)id; 6549 key[0].dttk_size = 0; 6550 DTRACE_TLS_THRKEY(key[1].dttk_value); 6551 key[1].dttk_size = 0; 6552 6553 dvar = dtrace_dynvar(dstate, 2, key, 6554 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 6555 mstate, vstate); 6556 6557 if (dvar == NULL) { 6558 regs[rd] = 0; 6559 break; 6560 } 6561 6562 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6563 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6564 } else { 6565 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6566 } 6567 6568 break; 6569 } 6570 6571 case DIF_OP_STTS: { 6572 dtrace_dynvar_t *dvar; 6573 dtrace_key_t *key; 6574 6575 id = DIF_INSTR_VAR(instr); 6576 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6577 id -= DIF_VAR_OTHER_UBASE; 6578 VERIFY(id < vstate->dtvs_ntlocals); 6579 6580 key = &tupregs[DIF_DTR_NREGS]; 6581 key[0].dttk_value = (uint64_t)id; 6582 key[0].dttk_size = 0; 6583 DTRACE_TLS_THRKEY(key[1].dttk_value); 6584 key[1].dttk_size = 0; 6585 v = &vstate->dtvs_tlocals[id]; 6586 6587 dvar = dtrace_dynvar(dstate, 2, key, 6588 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6589 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6590 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6591 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6592 6593 /* 6594 * Given that we're storing to thread-local data, 6595 * we need to flush our predicate cache. 6596 */ 6597 curthread->t_predcache = 0; 6598 6599 if (dvar == NULL) 6600 break; 6601 6602 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6603 size_t lim; 6604 6605 if (!dtrace_vcanload( 6606 (void *)(uintptr_t)regs[rd], 6607 &v->dtdv_type, &lim, mstate, vstate)) 6608 break; 6609 6610 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6611 dvar->dtdv_data, &v->dtdv_type, lim); 6612 } else { 6613 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6614 } 6615 6616 break; 6617 } 6618 6619 case DIF_OP_SRA: 6620 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 6621 break; 6622 6623 case DIF_OP_CALL: 6624 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 6625 regs, tupregs, ttop, mstate, state); 6626 break; 6627 6628 case DIF_OP_PUSHTR: 6629 if (ttop == DIF_DTR_NREGS) { 6630 *flags |= CPU_DTRACE_TUPOFLOW; 6631 break; 6632 } 6633 6634 if (r1 == DIF_TYPE_STRING) { 6635 /* 6636 * If this is a string type and the size is 0, 6637 * we'll use the system-wide default string 6638 * size. Note that we are _not_ looking at 6639 * the value of the DTRACEOPT_STRSIZE option; 6640 * had this been set, we would expect to have 6641 * a non-zero size value in the "pushtr". 6642 */ 6643 tupregs[ttop].dttk_size = 6644 dtrace_strlen((char *)(uintptr_t)regs[rd], 6645 regs[r2] ? regs[r2] : 6646 dtrace_strsize_default) + 1; 6647 } else { 6648 if (regs[r2] > LONG_MAX) { 6649 *flags |= CPU_DTRACE_ILLOP; 6650 break; 6651 } 6652 6653 tupregs[ttop].dttk_size = regs[r2]; 6654 } 6655 6656 tupregs[ttop++].dttk_value = regs[rd]; 6657 break; 6658 6659 case DIF_OP_PUSHTV: 6660 if (ttop == DIF_DTR_NREGS) { 6661 *flags |= CPU_DTRACE_TUPOFLOW; 6662 break; 6663 } 6664 6665 tupregs[ttop].dttk_value = regs[rd]; 6666 tupregs[ttop++].dttk_size = 0; 6667 break; 6668 6669 case DIF_OP_POPTS: 6670 if (ttop != 0) 6671 ttop--; 6672 break; 6673 6674 case DIF_OP_FLUSHTS: 6675 ttop = 0; 6676 break; 6677 6678 case DIF_OP_LDGAA: 6679 case DIF_OP_LDTAA: { 6680 dtrace_dynvar_t *dvar; 6681 dtrace_key_t *key = tupregs; 6682 uint_t nkeys = ttop; 6683 6684 id = DIF_INSTR_VAR(instr); 6685 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6686 id -= DIF_VAR_OTHER_UBASE; 6687 6688 key[nkeys].dttk_value = (uint64_t)id; 6689 key[nkeys++].dttk_size = 0; 6690 6691 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 6692 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6693 key[nkeys++].dttk_size = 0; 6694 VERIFY(id < vstate->dtvs_ntlocals); 6695 v = &vstate->dtvs_tlocals[id]; 6696 } else { 6697 VERIFY(id < vstate->dtvs_nglobals); 6698 v = &vstate->dtvs_globals[id]->dtsv_var; 6699 } 6700 6701 dvar = dtrace_dynvar(dstate, nkeys, key, 6702 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6703 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6704 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 6705 6706 if (dvar == NULL) { 6707 regs[rd] = 0; 6708 break; 6709 } 6710 6711 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6712 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6713 } else { 6714 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6715 } 6716 6717 break; 6718 } 6719 6720 case DIF_OP_STGAA: 6721 case DIF_OP_STTAA: { 6722 dtrace_dynvar_t *dvar; 6723 dtrace_key_t *key = tupregs; 6724 uint_t nkeys = ttop; 6725 6726 id = DIF_INSTR_VAR(instr); 6727 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6728 id -= DIF_VAR_OTHER_UBASE; 6729 6730 key[nkeys].dttk_value = (uint64_t)id; 6731 key[nkeys++].dttk_size = 0; 6732 6733 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 6734 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6735 key[nkeys++].dttk_size = 0; 6736 VERIFY(id < vstate->dtvs_ntlocals); 6737 v = &vstate->dtvs_tlocals[id]; 6738 } else { 6739 VERIFY(id < vstate->dtvs_nglobals); 6740 v = &vstate->dtvs_globals[id]->dtsv_var; 6741 } 6742 6743 dvar = dtrace_dynvar(dstate, nkeys, key, 6744 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6745 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6746 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6747 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6748 6749 if (dvar == NULL) 6750 break; 6751 6752 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6753 size_t lim; 6754 6755 if (!dtrace_vcanload( 6756 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6757 &lim, mstate, vstate)) 6758 break; 6759 6760 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6761 dvar->dtdv_data, &v->dtdv_type, lim); 6762 } else { 6763 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6764 } 6765 6766 break; 6767 } 6768 6769 case DIF_OP_ALLOCS: { 6770 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6771 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 6772 6773 /* 6774 * Rounding up the user allocation size could have 6775 * overflowed large, bogus allocations (like -1ULL) to 6776 * 0. 6777 */ 6778 if (size < regs[r1] || 6779 !DTRACE_INSCRATCH(mstate, size)) { 6780 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6781 regs[rd] = 0; 6782 break; 6783 } 6784 6785 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 6786 mstate->dtms_scratch_ptr += size; 6787 regs[rd] = ptr; 6788 break; 6789 } 6790 6791 case DIF_OP_COPYS: 6792 if (!dtrace_canstore(regs[rd], regs[r2], 6793 mstate, vstate)) { 6794 *flags |= CPU_DTRACE_BADADDR; 6795 *illval = regs[rd]; 6796 break; 6797 } 6798 6799 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 6800 break; 6801 6802 dtrace_bcopy((void *)(uintptr_t)regs[r1], 6803 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 6804 break; 6805 6806 case DIF_OP_STB: 6807 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 6808 *flags |= CPU_DTRACE_BADADDR; 6809 *illval = regs[rd]; 6810 break; 6811 } 6812 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 6813 break; 6814 6815 case DIF_OP_STH: 6816 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 6817 *flags |= CPU_DTRACE_BADADDR; 6818 *illval = regs[rd]; 6819 break; 6820 } 6821 if (regs[rd] & 1) { 6822 *flags |= CPU_DTRACE_BADALIGN; 6823 *illval = regs[rd]; 6824 break; 6825 } 6826 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 6827 break; 6828 6829 case DIF_OP_STW: 6830 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 6831 *flags |= CPU_DTRACE_BADADDR; 6832 *illval = regs[rd]; 6833 break; 6834 } 6835 if (regs[rd] & 3) { 6836 *flags |= CPU_DTRACE_BADALIGN; 6837 *illval = regs[rd]; 6838 break; 6839 } 6840 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 6841 break; 6842 6843 case DIF_OP_STX: 6844 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 6845 *flags |= CPU_DTRACE_BADADDR; 6846 *illval = regs[rd]; 6847 break; 6848 } 6849 if (regs[rd] & 7) { 6850 *flags |= CPU_DTRACE_BADALIGN; 6851 *illval = regs[rd]; 6852 break; 6853 } 6854 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 6855 break; 6856 } 6857 } 6858 6859 if (!(*flags & CPU_DTRACE_FAULT)) 6860 return (rval); 6861 6862 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 6863 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 6864 6865 return (0); 6866 } 6867 6868 static void 6869 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 6870 { 6871 dtrace_probe_t *probe = ecb->dte_probe; 6872 dtrace_provider_t *prov = probe->dtpr_provider; 6873 char c[DTRACE_FULLNAMELEN + 80], *str; 6874 char *msg = "dtrace: breakpoint action at probe "; 6875 char *ecbmsg = " (ecb "; 6876 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 6877 uintptr_t val = (uintptr_t)ecb; 6878 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 6879 6880 if (dtrace_destructive_disallow) 6881 return; 6882 6883 /* 6884 * It's impossible to be taking action on the NULL probe. 6885 */ 6886 ASSERT(probe != NULL); 6887 6888 /* 6889 * This is a poor man's (destitute man's?) sprintf(): we want to 6890 * print the provider name, module name, function name and name of 6891 * the probe, along with the hex address of the ECB with the breakpoint 6892 * action -- all of which we must place in the character buffer by 6893 * hand. 6894 */ 6895 while (*msg != '\0') 6896 c[i++] = *msg++; 6897 6898 for (str = prov->dtpv_name; *str != '\0'; str++) 6899 c[i++] = *str; 6900 c[i++] = ':'; 6901 6902 for (str = probe->dtpr_mod; *str != '\0'; str++) 6903 c[i++] = *str; 6904 c[i++] = ':'; 6905 6906 for (str = probe->dtpr_func; *str != '\0'; str++) 6907 c[i++] = *str; 6908 c[i++] = ':'; 6909 6910 for (str = probe->dtpr_name; *str != '\0'; str++) 6911 c[i++] = *str; 6912 6913 while (*ecbmsg != '\0') 6914 c[i++] = *ecbmsg++; 6915 6916 while (shift >= 0) { 6917 mask = (uintptr_t)0xf << shift; 6918 6919 if (val >= ((uintptr_t)1 << shift)) 6920 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 6921 shift -= 4; 6922 } 6923 6924 c[i++] = ')'; 6925 c[i] = '\0'; 6926 6927 #ifdef illumos 6928 debug_enter(c); 6929 #else 6930 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 6931 #endif 6932 } 6933 6934 static void 6935 dtrace_action_panic(dtrace_ecb_t *ecb) 6936 { 6937 dtrace_probe_t *probe = ecb->dte_probe; 6938 6939 /* 6940 * It's impossible to be taking action on the NULL probe. 6941 */ 6942 ASSERT(probe != NULL); 6943 6944 if (dtrace_destructive_disallow) 6945 return; 6946 6947 if (dtrace_panicked != NULL) 6948 return; 6949 6950 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 6951 return; 6952 6953 /* 6954 * We won the right to panic. (We want to be sure that only one 6955 * thread calls panic() from dtrace_probe(), and that panic() is 6956 * called exactly once.) 6957 */ 6958 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 6959 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 6960 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 6961 } 6962 6963 static void 6964 dtrace_action_raise(uint64_t sig) 6965 { 6966 if (dtrace_destructive_disallow) 6967 return; 6968 6969 if (sig >= NSIG) { 6970 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 6971 return; 6972 } 6973 6974 #ifdef illumos 6975 /* 6976 * raise() has a queue depth of 1 -- we ignore all subsequent 6977 * invocations of the raise() action. 6978 */ 6979 if (curthread->t_dtrace_sig == 0) 6980 curthread->t_dtrace_sig = (uint8_t)sig; 6981 6982 curthread->t_sig_check = 1; 6983 aston(curthread); 6984 #else 6985 struct proc *p = curproc; 6986 PROC_LOCK(p); 6987 kern_psignal(p, sig); 6988 PROC_UNLOCK(p); 6989 #endif 6990 } 6991 6992 static void 6993 dtrace_action_stop(void) 6994 { 6995 if (dtrace_destructive_disallow) 6996 return; 6997 6998 #ifdef illumos 6999 if (!curthread->t_dtrace_stop) { 7000 curthread->t_dtrace_stop = 1; 7001 curthread->t_sig_check = 1; 7002 aston(curthread); 7003 } 7004 #else 7005 struct proc *p = curproc; 7006 PROC_LOCK(p); 7007 kern_psignal(p, SIGSTOP); 7008 PROC_UNLOCK(p); 7009 #endif 7010 } 7011 7012 static void 7013 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 7014 { 7015 hrtime_t now; 7016 volatile uint16_t *flags; 7017 #ifdef illumos 7018 cpu_t *cpu = CPU; 7019 #else 7020 cpu_t *cpu = &solaris_cpu[curcpu]; 7021 #endif 7022 7023 if (dtrace_destructive_disallow) 7024 return; 7025 7026 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 7027 7028 now = dtrace_gethrtime(); 7029 7030 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 7031 /* 7032 * We need to advance the mark to the current time. 7033 */ 7034 cpu->cpu_dtrace_chillmark = now; 7035 cpu->cpu_dtrace_chilled = 0; 7036 } 7037 7038 /* 7039 * Now check to see if the requested chill time would take us over 7040 * the maximum amount of time allowed in the chill interval. (Or 7041 * worse, if the calculation itself induces overflow.) 7042 */ 7043 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 7044 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 7045 *flags |= CPU_DTRACE_ILLOP; 7046 return; 7047 } 7048 7049 while (dtrace_gethrtime() - now < val) 7050 continue; 7051 7052 /* 7053 * Normally, we assure that the value of the variable "timestamp" does 7054 * not change within an ECB. The presence of chill() represents an 7055 * exception to this rule, however. 7056 */ 7057 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 7058 cpu->cpu_dtrace_chilled += val; 7059 } 7060 7061 static void 7062 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 7063 uint64_t *buf, uint64_t arg) 7064 { 7065 int nframes = DTRACE_USTACK_NFRAMES(arg); 7066 int strsize = DTRACE_USTACK_STRSIZE(arg); 7067 uint64_t *pcs = &buf[1], *fps; 7068 char *str = (char *)&pcs[nframes]; 7069 int size, offs = 0, i, j; 7070 size_t rem; 7071 uintptr_t old = mstate->dtms_scratch_ptr, saved; 7072 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 7073 char *sym; 7074 7075 /* 7076 * Should be taking a faster path if string space has not been 7077 * allocated. 7078 */ 7079 ASSERT(strsize != 0); 7080 7081 /* 7082 * We will first allocate some temporary space for the frame pointers. 7083 */ 7084 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 7085 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 7086 (nframes * sizeof (uint64_t)); 7087 7088 if (!DTRACE_INSCRATCH(mstate, size)) { 7089 /* 7090 * Not enough room for our frame pointers -- need to indicate 7091 * that we ran out of scratch space. 7092 */ 7093 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 7094 return; 7095 } 7096 7097 mstate->dtms_scratch_ptr += size; 7098 saved = mstate->dtms_scratch_ptr; 7099 7100 /* 7101 * Now get a stack with both program counters and frame pointers. 7102 */ 7103 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7104 dtrace_getufpstack(buf, fps, nframes + 1); 7105 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7106 7107 /* 7108 * If that faulted, we're cooked. 7109 */ 7110 if (*flags & CPU_DTRACE_FAULT) 7111 goto out; 7112 7113 /* 7114 * Now we want to walk up the stack, calling the USTACK helper. For 7115 * each iteration, we restore the scratch pointer. 7116 */ 7117 for (i = 0; i < nframes; i++) { 7118 mstate->dtms_scratch_ptr = saved; 7119 7120 if (offs >= strsize) 7121 break; 7122 7123 sym = (char *)(uintptr_t)dtrace_helper( 7124 DTRACE_HELPER_ACTION_USTACK, 7125 mstate, state, pcs[i], fps[i]); 7126 7127 /* 7128 * If we faulted while running the helper, we're going to 7129 * clear the fault and null out the corresponding string. 7130 */ 7131 if (*flags & CPU_DTRACE_FAULT) { 7132 *flags &= ~CPU_DTRACE_FAULT; 7133 str[offs++] = '\0'; 7134 continue; 7135 } 7136 7137 if (sym == NULL) { 7138 str[offs++] = '\0'; 7139 continue; 7140 } 7141 7142 if (!dtrace_strcanload((uintptr_t)sym, strsize, &rem, mstate, 7143 &(state->dts_vstate))) { 7144 str[offs++] = '\0'; 7145 continue; 7146 } 7147 7148 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7149 7150 /* 7151 * Now copy in the string that the helper returned to us. 7152 */ 7153 for (j = 0; offs + j < strsize && j < rem; j++) { 7154 if ((str[offs + j] = sym[j]) == '\0') 7155 break; 7156 } 7157 7158 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7159 7160 offs += j + 1; 7161 } 7162 7163 if (offs >= strsize) { 7164 /* 7165 * If we didn't have room for all of the strings, we don't 7166 * abort processing -- this needn't be a fatal error -- but we 7167 * still want to increment a counter (dts_stkstroverflows) to 7168 * allow this condition to be warned about. (If this is from 7169 * a jstack() action, it is easily tuned via jstackstrsize.) 7170 */ 7171 dtrace_error(&state->dts_stkstroverflows); 7172 } 7173 7174 while (offs < strsize) 7175 str[offs++] = '\0'; 7176 7177 out: 7178 mstate->dtms_scratch_ptr = old; 7179 } 7180 7181 static void 7182 dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size, 7183 size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind) 7184 { 7185 volatile uint16_t *flags; 7186 uint64_t val = *valp; 7187 size_t valoffs = *valoffsp; 7188 7189 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 7190 ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF); 7191 7192 /* 7193 * If this is a string, we're going to only load until we find the zero 7194 * byte -- after which we'll store zero bytes. 7195 */ 7196 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 7197 char c = '\0' + 1; 7198 size_t s; 7199 7200 for (s = 0; s < size; s++) { 7201 if (c != '\0' && dtkind == DIF_TF_BYREF) { 7202 c = dtrace_load8(val++); 7203 } else if (c != '\0' && dtkind == DIF_TF_BYUREF) { 7204 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7205 c = dtrace_fuword8((void *)(uintptr_t)val++); 7206 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7207 if (*flags & CPU_DTRACE_FAULT) 7208 break; 7209 } 7210 7211 DTRACE_STORE(uint8_t, tomax, valoffs++, c); 7212 7213 if (c == '\0' && intuple) 7214 break; 7215 } 7216 } else { 7217 uint8_t c; 7218 while (valoffs < end) { 7219 if (dtkind == DIF_TF_BYREF) { 7220 c = dtrace_load8(val++); 7221 } else if (dtkind == DIF_TF_BYUREF) { 7222 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7223 c = dtrace_fuword8((void *)(uintptr_t)val++); 7224 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7225 if (*flags & CPU_DTRACE_FAULT) 7226 break; 7227 } 7228 7229 DTRACE_STORE(uint8_t, tomax, 7230 valoffs++, c); 7231 } 7232 } 7233 7234 *valp = val; 7235 *valoffsp = valoffs; 7236 } 7237 7238 /* 7239 * If you're looking for the epicenter of DTrace, you just found it. This 7240 * is the function called by the provider to fire a probe -- from which all 7241 * subsequent probe-context DTrace activity emanates. 7242 */ 7243 void 7244 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 7245 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 7246 { 7247 processorid_t cpuid; 7248 dtrace_icookie_t cookie; 7249 dtrace_probe_t *probe; 7250 dtrace_mstate_t mstate; 7251 dtrace_ecb_t *ecb; 7252 dtrace_action_t *act; 7253 intptr_t offs; 7254 size_t size; 7255 int vtime, onintr; 7256 volatile uint16_t *flags; 7257 hrtime_t now; 7258 7259 if (panicstr != NULL) 7260 return; 7261 7262 #ifdef illumos 7263 /* 7264 * Kick out immediately if this CPU is still being born (in which case 7265 * curthread will be set to -1) or the current thread can't allow 7266 * probes in its current context. 7267 */ 7268 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 7269 return; 7270 #endif 7271 7272 cookie = dtrace_interrupt_disable(); 7273 probe = dtrace_probes[id - 1]; 7274 cpuid = curcpu; 7275 onintr = CPU_ON_INTR(CPU); 7276 7277 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 7278 probe->dtpr_predcache == curthread->t_predcache) { 7279 /* 7280 * We have hit in the predicate cache; we know that 7281 * this predicate would evaluate to be false. 7282 */ 7283 dtrace_interrupt_enable(cookie); 7284 return; 7285 } 7286 7287 #ifdef illumos 7288 if (panic_quiesce) { 7289 #else 7290 if (panicstr != NULL) { 7291 #endif 7292 /* 7293 * We don't trace anything if we're panicking. 7294 */ 7295 dtrace_interrupt_enable(cookie); 7296 return; 7297 } 7298 7299 now = mstate.dtms_timestamp = dtrace_gethrtime(); 7300 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7301 vtime = dtrace_vtime_references != 0; 7302 7303 if (vtime && curthread->t_dtrace_start) 7304 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 7305 7306 mstate.dtms_difo = NULL; 7307 mstate.dtms_probe = probe; 7308 mstate.dtms_strtok = 0; 7309 mstate.dtms_arg[0] = arg0; 7310 mstate.dtms_arg[1] = arg1; 7311 mstate.dtms_arg[2] = arg2; 7312 mstate.dtms_arg[3] = arg3; 7313 mstate.dtms_arg[4] = arg4; 7314 7315 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 7316 7317 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 7318 dtrace_predicate_t *pred = ecb->dte_predicate; 7319 dtrace_state_t *state = ecb->dte_state; 7320 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 7321 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 7322 dtrace_vstate_t *vstate = &state->dts_vstate; 7323 dtrace_provider_t *prov = probe->dtpr_provider; 7324 uint64_t tracememsize = 0; 7325 int committed = 0; 7326 caddr_t tomax; 7327 7328 /* 7329 * A little subtlety with the following (seemingly innocuous) 7330 * declaration of the automatic 'val': by looking at the 7331 * code, you might think that it could be declared in the 7332 * action processing loop, below. (That is, it's only used in 7333 * the action processing loop.) However, it must be declared 7334 * out of that scope because in the case of DIF expression 7335 * arguments to aggregating actions, one iteration of the 7336 * action loop will use the last iteration's value. 7337 */ 7338 uint64_t val = 0; 7339 7340 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 7341 mstate.dtms_getf = NULL; 7342 7343 *flags &= ~CPU_DTRACE_ERROR; 7344 7345 if (prov == dtrace_provider) { 7346 /* 7347 * If dtrace itself is the provider of this probe, 7348 * we're only going to continue processing the ECB if 7349 * arg0 (the dtrace_state_t) is equal to the ECB's 7350 * creating state. (This prevents disjoint consumers 7351 * from seeing one another's metaprobes.) 7352 */ 7353 if (arg0 != (uint64_t)(uintptr_t)state) 7354 continue; 7355 } 7356 7357 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 7358 /* 7359 * We're not currently active. If our provider isn't 7360 * the dtrace pseudo provider, we're not interested. 7361 */ 7362 if (prov != dtrace_provider) 7363 continue; 7364 7365 /* 7366 * Now we must further check if we are in the BEGIN 7367 * probe. If we are, we will only continue processing 7368 * if we're still in WARMUP -- if one BEGIN enabling 7369 * has invoked the exit() action, we don't want to 7370 * evaluate subsequent BEGIN enablings. 7371 */ 7372 if (probe->dtpr_id == dtrace_probeid_begin && 7373 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 7374 ASSERT(state->dts_activity == 7375 DTRACE_ACTIVITY_DRAINING); 7376 continue; 7377 } 7378 } 7379 7380 if (ecb->dte_cond) { 7381 /* 7382 * If the dte_cond bits indicate that this 7383 * consumer is only allowed to see user-mode firings 7384 * of this probe, call the provider's dtps_usermode() 7385 * entry point to check that the probe was fired 7386 * while in a user context. Skip this ECB if that's 7387 * not the case. 7388 */ 7389 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 7390 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 7391 probe->dtpr_id, probe->dtpr_arg) == 0) 7392 continue; 7393 7394 #ifdef illumos 7395 /* 7396 * This is more subtle than it looks. We have to be 7397 * absolutely certain that CRED() isn't going to 7398 * change out from under us so it's only legit to 7399 * examine that structure if we're in constrained 7400 * situations. Currently, the only times we'll this 7401 * check is if a non-super-user has enabled the 7402 * profile or syscall providers -- providers that 7403 * allow visibility of all processes. For the 7404 * profile case, the check above will ensure that 7405 * we're examining a user context. 7406 */ 7407 if (ecb->dte_cond & DTRACE_COND_OWNER) { 7408 cred_t *cr; 7409 cred_t *s_cr = 7410 ecb->dte_state->dts_cred.dcr_cred; 7411 proc_t *proc; 7412 7413 ASSERT(s_cr != NULL); 7414 7415 if ((cr = CRED()) == NULL || 7416 s_cr->cr_uid != cr->cr_uid || 7417 s_cr->cr_uid != cr->cr_ruid || 7418 s_cr->cr_uid != cr->cr_suid || 7419 s_cr->cr_gid != cr->cr_gid || 7420 s_cr->cr_gid != cr->cr_rgid || 7421 s_cr->cr_gid != cr->cr_sgid || 7422 (proc = ttoproc(curthread)) == NULL || 7423 (proc->p_flag & SNOCD)) 7424 continue; 7425 } 7426 7427 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 7428 cred_t *cr; 7429 cred_t *s_cr = 7430 ecb->dte_state->dts_cred.dcr_cred; 7431 7432 ASSERT(s_cr != NULL); 7433 7434 if ((cr = CRED()) == NULL || 7435 s_cr->cr_zone->zone_id != 7436 cr->cr_zone->zone_id) 7437 continue; 7438 } 7439 #endif 7440 } 7441 7442 if (now - state->dts_alive > dtrace_deadman_timeout) { 7443 /* 7444 * We seem to be dead. Unless we (a) have kernel 7445 * destructive permissions (b) have explicitly enabled 7446 * destructive actions and (c) destructive actions have 7447 * not been disabled, we're going to transition into 7448 * the KILLED state, from which no further processing 7449 * on this state will be performed. 7450 */ 7451 if (!dtrace_priv_kernel_destructive(state) || 7452 !state->dts_cred.dcr_destructive || 7453 dtrace_destructive_disallow) { 7454 void *activity = &state->dts_activity; 7455 dtrace_activity_t current; 7456 7457 do { 7458 current = state->dts_activity; 7459 } while (dtrace_cas32(activity, current, 7460 DTRACE_ACTIVITY_KILLED) != current); 7461 7462 continue; 7463 } 7464 } 7465 7466 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 7467 ecb->dte_alignment, state, &mstate)) < 0) 7468 continue; 7469 7470 tomax = buf->dtb_tomax; 7471 ASSERT(tomax != NULL); 7472 7473 if (ecb->dte_size != 0) { 7474 dtrace_rechdr_t dtrh; 7475 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 7476 mstate.dtms_timestamp = dtrace_gethrtime(); 7477 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7478 } 7479 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 7480 dtrh.dtrh_epid = ecb->dte_epid; 7481 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 7482 mstate.dtms_timestamp); 7483 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 7484 } 7485 7486 mstate.dtms_epid = ecb->dte_epid; 7487 mstate.dtms_present |= DTRACE_MSTATE_EPID; 7488 7489 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 7490 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 7491 else 7492 mstate.dtms_access = 0; 7493 7494 if (pred != NULL) { 7495 dtrace_difo_t *dp = pred->dtp_difo; 7496 uint64_t rval; 7497 7498 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 7499 7500 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 7501 dtrace_cacheid_t cid = probe->dtpr_predcache; 7502 7503 if (cid != DTRACE_CACHEIDNONE && !onintr) { 7504 /* 7505 * Update the predicate cache... 7506 */ 7507 ASSERT(cid == pred->dtp_cacheid); 7508 curthread->t_predcache = cid; 7509 } 7510 7511 continue; 7512 } 7513 } 7514 7515 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 7516 act != NULL; act = act->dta_next) { 7517 size_t valoffs; 7518 dtrace_difo_t *dp; 7519 dtrace_recdesc_t *rec = &act->dta_rec; 7520 7521 size = rec->dtrd_size; 7522 valoffs = offs + rec->dtrd_offset; 7523 7524 if (DTRACEACT_ISAGG(act->dta_kind)) { 7525 uint64_t v = 0xbad; 7526 dtrace_aggregation_t *agg; 7527 7528 agg = (dtrace_aggregation_t *)act; 7529 7530 if ((dp = act->dta_difo) != NULL) 7531 v = dtrace_dif_emulate(dp, 7532 &mstate, vstate, state); 7533 7534 if (*flags & CPU_DTRACE_ERROR) 7535 continue; 7536 7537 /* 7538 * Note that we always pass the expression 7539 * value from the previous iteration of the 7540 * action loop. This value will only be used 7541 * if there is an expression argument to the 7542 * aggregating action, denoted by the 7543 * dtag_hasarg field. 7544 */ 7545 dtrace_aggregate(agg, buf, 7546 offs, aggbuf, v, val); 7547 continue; 7548 } 7549 7550 switch (act->dta_kind) { 7551 case DTRACEACT_STOP: 7552 if (dtrace_priv_proc_destructive(state)) 7553 dtrace_action_stop(); 7554 continue; 7555 7556 case DTRACEACT_BREAKPOINT: 7557 if (dtrace_priv_kernel_destructive(state)) 7558 dtrace_action_breakpoint(ecb); 7559 continue; 7560 7561 case DTRACEACT_PANIC: 7562 if (dtrace_priv_kernel_destructive(state)) 7563 dtrace_action_panic(ecb); 7564 continue; 7565 7566 case DTRACEACT_STACK: 7567 if (!dtrace_priv_kernel(state)) 7568 continue; 7569 7570 dtrace_getpcstack((pc_t *)(tomax + valoffs), 7571 size / sizeof (pc_t), probe->dtpr_aframes, 7572 DTRACE_ANCHORED(probe) ? NULL : 7573 (uint32_t *)arg0); 7574 continue; 7575 7576 case DTRACEACT_JSTACK: 7577 case DTRACEACT_USTACK: 7578 if (!dtrace_priv_proc(state)) 7579 continue; 7580 7581 /* 7582 * See comment in DIF_VAR_PID. 7583 */ 7584 if (DTRACE_ANCHORED(mstate.dtms_probe) && 7585 CPU_ON_INTR(CPU)) { 7586 int depth = DTRACE_USTACK_NFRAMES( 7587 rec->dtrd_arg) + 1; 7588 7589 dtrace_bzero((void *)(tomax + valoffs), 7590 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 7591 + depth * sizeof (uint64_t)); 7592 7593 continue; 7594 } 7595 7596 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 7597 curproc->p_dtrace_helpers != NULL) { 7598 /* 7599 * This is the slow path -- we have 7600 * allocated string space, and we're 7601 * getting the stack of a process that 7602 * has helpers. Call into a separate 7603 * routine to perform this processing. 7604 */ 7605 dtrace_action_ustack(&mstate, state, 7606 (uint64_t *)(tomax + valoffs), 7607 rec->dtrd_arg); 7608 continue; 7609 } 7610 7611 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7612 dtrace_getupcstack((uint64_t *) 7613 (tomax + valoffs), 7614 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 7615 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7616 continue; 7617 7618 default: 7619 break; 7620 } 7621 7622 dp = act->dta_difo; 7623 ASSERT(dp != NULL); 7624 7625 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 7626 7627 if (*flags & CPU_DTRACE_ERROR) 7628 continue; 7629 7630 switch (act->dta_kind) { 7631 case DTRACEACT_SPECULATE: { 7632 dtrace_rechdr_t *dtrh; 7633 7634 ASSERT(buf == &state->dts_buffer[cpuid]); 7635 buf = dtrace_speculation_buffer(state, 7636 cpuid, val); 7637 7638 if (buf == NULL) { 7639 *flags |= CPU_DTRACE_DROP; 7640 continue; 7641 } 7642 7643 offs = dtrace_buffer_reserve(buf, 7644 ecb->dte_needed, ecb->dte_alignment, 7645 state, NULL); 7646 7647 if (offs < 0) { 7648 *flags |= CPU_DTRACE_DROP; 7649 continue; 7650 } 7651 7652 tomax = buf->dtb_tomax; 7653 ASSERT(tomax != NULL); 7654 7655 if (ecb->dte_size == 0) 7656 continue; 7657 7658 ASSERT3U(ecb->dte_size, >=, 7659 sizeof (dtrace_rechdr_t)); 7660 dtrh = ((void *)(tomax + offs)); 7661 dtrh->dtrh_epid = ecb->dte_epid; 7662 /* 7663 * When the speculation is committed, all of 7664 * the records in the speculative buffer will 7665 * have their timestamps set to the commit 7666 * time. Until then, it is set to a sentinel 7667 * value, for debugability. 7668 */ 7669 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 7670 continue; 7671 } 7672 7673 case DTRACEACT_PRINTM: { 7674 /* The DIF returns a 'memref'. */ 7675 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 7676 7677 /* Get the size from the memref. */ 7678 size = memref[1]; 7679 7680 /* 7681 * Check if the size exceeds the allocated 7682 * buffer size. 7683 */ 7684 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7685 /* Flag a drop! */ 7686 *flags |= CPU_DTRACE_DROP; 7687 continue; 7688 } 7689 7690 /* Store the size in the buffer first. */ 7691 DTRACE_STORE(uintptr_t, tomax, 7692 valoffs, size); 7693 7694 /* 7695 * Offset the buffer address to the start 7696 * of the data. 7697 */ 7698 valoffs += sizeof(uintptr_t); 7699 7700 /* 7701 * Reset to the memory address rather than 7702 * the memref array, then let the BYREF 7703 * code below do the work to store the 7704 * memory data in the buffer. 7705 */ 7706 val = memref[0]; 7707 break; 7708 } 7709 7710 case DTRACEACT_CHILL: 7711 if (dtrace_priv_kernel_destructive(state)) 7712 dtrace_action_chill(&mstate, val); 7713 continue; 7714 7715 case DTRACEACT_RAISE: 7716 if (dtrace_priv_proc_destructive(state)) 7717 dtrace_action_raise(val); 7718 continue; 7719 7720 case DTRACEACT_COMMIT: 7721 ASSERT(!committed); 7722 7723 /* 7724 * We need to commit our buffer state. 7725 */ 7726 if (ecb->dte_size) 7727 buf->dtb_offset = offs + ecb->dte_size; 7728 buf = &state->dts_buffer[cpuid]; 7729 dtrace_speculation_commit(state, cpuid, val); 7730 committed = 1; 7731 continue; 7732 7733 case DTRACEACT_DISCARD: 7734 dtrace_speculation_discard(state, cpuid, val); 7735 continue; 7736 7737 case DTRACEACT_DIFEXPR: 7738 case DTRACEACT_LIBACT: 7739 case DTRACEACT_PRINTF: 7740 case DTRACEACT_PRINTA: 7741 case DTRACEACT_SYSTEM: 7742 case DTRACEACT_FREOPEN: 7743 case DTRACEACT_TRACEMEM: 7744 break; 7745 7746 case DTRACEACT_TRACEMEM_DYNSIZE: 7747 tracememsize = val; 7748 break; 7749 7750 case DTRACEACT_SYM: 7751 case DTRACEACT_MOD: 7752 if (!dtrace_priv_kernel(state)) 7753 continue; 7754 break; 7755 7756 case DTRACEACT_USYM: 7757 case DTRACEACT_UMOD: 7758 case DTRACEACT_UADDR: { 7759 #ifdef illumos 7760 struct pid *pid = curthread->t_procp->p_pidp; 7761 #endif 7762 7763 if (!dtrace_priv_proc(state)) 7764 continue; 7765 7766 DTRACE_STORE(uint64_t, tomax, 7767 #ifdef illumos 7768 valoffs, (uint64_t)pid->pid_id); 7769 #else 7770 valoffs, (uint64_t) curproc->p_pid); 7771 #endif 7772 DTRACE_STORE(uint64_t, tomax, 7773 valoffs + sizeof (uint64_t), val); 7774 7775 continue; 7776 } 7777 7778 case DTRACEACT_EXIT: { 7779 /* 7780 * For the exit action, we are going to attempt 7781 * to atomically set our activity to be 7782 * draining. If this fails (either because 7783 * another CPU has beat us to the exit action, 7784 * or because our current activity is something 7785 * other than ACTIVE or WARMUP), we will 7786 * continue. This assures that the exit action 7787 * can be successfully recorded at most once 7788 * when we're in the ACTIVE state. If we're 7789 * encountering the exit() action while in 7790 * COOLDOWN, however, we want to honor the new 7791 * status code. (We know that we're the only 7792 * thread in COOLDOWN, so there is no race.) 7793 */ 7794 void *activity = &state->dts_activity; 7795 dtrace_activity_t current = state->dts_activity; 7796 7797 if (current == DTRACE_ACTIVITY_COOLDOWN) 7798 break; 7799 7800 if (current != DTRACE_ACTIVITY_WARMUP) 7801 current = DTRACE_ACTIVITY_ACTIVE; 7802 7803 if (dtrace_cas32(activity, current, 7804 DTRACE_ACTIVITY_DRAINING) != current) { 7805 *flags |= CPU_DTRACE_DROP; 7806 continue; 7807 } 7808 7809 break; 7810 } 7811 7812 default: 7813 ASSERT(0); 7814 } 7815 7816 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF || 7817 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) { 7818 uintptr_t end = valoffs + size; 7819 7820 if (tracememsize != 0 && 7821 valoffs + tracememsize < end) { 7822 end = valoffs + tracememsize; 7823 tracememsize = 0; 7824 } 7825 7826 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF && 7827 !dtrace_vcanload((void *)(uintptr_t)val, 7828 &dp->dtdo_rtype, NULL, &mstate, vstate)) 7829 continue; 7830 7831 dtrace_store_by_ref(dp, tomax, size, &valoffs, 7832 &val, end, act->dta_intuple, 7833 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ? 7834 DIF_TF_BYREF: DIF_TF_BYUREF); 7835 continue; 7836 } 7837 7838 switch (size) { 7839 case 0: 7840 break; 7841 7842 case sizeof (uint8_t): 7843 DTRACE_STORE(uint8_t, tomax, valoffs, val); 7844 break; 7845 case sizeof (uint16_t): 7846 DTRACE_STORE(uint16_t, tomax, valoffs, val); 7847 break; 7848 case sizeof (uint32_t): 7849 DTRACE_STORE(uint32_t, tomax, valoffs, val); 7850 break; 7851 case sizeof (uint64_t): 7852 DTRACE_STORE(uint64_t, tomax, valoffs, val); 7853 break; 7854 default: 7855 /* 7856 * Any other size should have been returned by 7857 * reference, not by value. 7858 */ 7859 ASSERT(0); 7860 break; 7861 } 7862 } 7863 7864 if (*flags & CPU_DTRACE_DROP) 7865 continue; 7866 7867 if (*flags & CPU_DTRACE_FAULT) { 7868 int ndx; 7869 dtrace_action_t *err; 7870 7871 buf->dtb_errors++; 7872 7873 if (probe->dtpr_id == dtrace_probeid_error) { 7874 /* 7875 * There's nothing we can do -- we had an 7876 * error on the error probe. We bump an 7877 * error counter to at least indicate that 7878 * this condition happened. 7879 */ 7880 dtrace_error(&state->dts_dblerrors); 7881 continue; 7882 } 7883 7884 if (vtime) { 7885 /* 7886 * Before recursing on dtrace_probe(), we 7887 * need to explicitly clear out our start 7888 * time to prevent it from being accumulated 7889 * into t_dtrace_vtime. 7890 */ 7891 curthread->t_dtrace_start = 0; 7892 } 7893 7894 /* 7895 * Iterate over the actions to figure out which action 7896 * we were processing when we experienced the error. 7897 * Note that act points _past_ the faulting action; if 7898 * act is ecb->dte_action, the fault was in the 7899 * predicate, if it's ecb->dte_action->dta_next it's 7900 * in action #1, and so on. 7901 */ 7902 for (err = ecb->dte_action, ndx = 0; 7903 err != act; err = err->dta_next, ndx++) 7904 continue; 7905 7906 dtrace_probe_error(state, ecb->dte_epid, ndx, 7907 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 7908 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 7909 cpu_core[cpuid].cpuc_dtrace_illval); 7910 7911 continue; 7912 } 7913 7914 if (!committed) 7915 buf->dtb_offset = offs + ecb->dte_size; 7916 } 7917 7918 if (vtime) 7919 curthread->t_dtrace_start = dtrace_gethrtime(); 7920 7921 dtrace_interrupt_enable(cookie); 7922 } 7923 7924 /* 7925 * DTrace Probe Hashing Functions 7926 * 7927 * The functions in this section (and indeed, the functions in remaining 7928 * sections) are not _called_ from probe context. (Any exceptions to this are 7929 * marked with a "Note:".) Rather, they are called from elsewhere in the 7930 * DTrace framework to look-up probes in, add probes to and remove probes from 7931 * the DTrace probe hashes. (Each probe is hashed by each element of the 7932 * probe tuple -- allowing for fast lookups, regardless of what was 7933 * specified.) 7934 */ 7935 static uint_t 7936 dtrace_hash_str(const char *p) 7937 { 7938 unsigned int g; 7939 uint_t hval = 0; 7940 7941 while (*p) { 7942 hval = (hval << 4) + *p++; 7943 if ((g = (hval & 0xf0000000)) != 0) 7944 hval ^= g >> 24; 7945 hval &= ~g; 7946 } 7947 return (hval); 7948 } 7949 7950 static dtrace_hash_t * 7951 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 7952 { 7953 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 7954 7955 hash->dth_stroffs = stroffs; 7956 hash->dth_nextoffs = nextoffs; 7957 hash->dth_prevoffs = prevoffs; 7958 7959 hash->dth_size = 1; 7960 hash->dth_mask = hash->dth_size - 1; 7961 7962 hash->dth_tab = kmem_zalloc(hash->dth_size * 7963 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 7964 7965 return (hash); 7966 } 7967 7968 static void 7969 dtrace_hash_destroy(dtrace_hash_t *hash) 7970 { 7971 #ifdef DEBUG 7972 int i; 7973 7974 for (i = 0; i < hash->dth_size; i++) 7975 ASSERT(hash->dth_tab[i] == NULL); 7976 #endif 7977 7978 kmem_free(hash->dth_tab, 7979 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 7980 kmem_free(hash, sizeof (dtrace_hash_t)); 7981 } 7982 7983 static void 7984 dtrace_hash_resize(dtrace_hash_t *hash) 7985 { 7986 int size = hash->dth_size, i, ndx; 7987 int new_size = hash->dth_size << 1; 7988 int new_mask = new_size - 1; 7989 dtrace_hashbucket_t **new_tab, *bucket, *next; 7990 7991 ASSERT((new_size & new_mask) == 0); 7992 7993 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 7994 7995 for (i = 0; i < size; i++) { 7996 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 7997 dtrace_probe_t *probe = bucket->dthb_chain; 7998 7999 ASSERT(probe != NULL); 8000 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 8001 8002 next = bucket->dthb_next; 8003 bucket->dthb_next = new_tab[ndx]; 8004 new_tab[ndx] = bucket; 8005 } 8006 } 8007 8008 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 8009 hash->dth_tab = new_tab; 8010 hash->dth_size = new_size; 8011 hash->dth_mask = new_mask; 8012 } 8013 8014 static void 8015 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 8016 { 8017 int hashval = DTRACE_HASHSTR(hash, new); 8018 int ndx = hashval & hash->dth_mask; 8019 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8020 dtrace_probe_t **nextp, **prevp; 8021 8022 for (; bucket != NULL; bucket = bucket->dthb_next) { 8023 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 8024 goto add; 8025 } 8026 8027 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 8028 dtrace_hash_resize(hash); 8029 dtrace_hash_add(hash, new); 8030 return; 8031 } 8032 8033 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 8034 bucket->dthb_next = hash->dth_tab[ndx]; 8035 hash->dth_tab[ndx] = bucket; 8036 hash->dth_nbuckets++; 8037 8038 add: 8039 nextp = DTRACE_HASHNEXT(hash, new); 8040 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 8041 *nextp = bucket->dthb_chain; 8042 8043 if (bucket->dthb_chain != NULL) { 8044 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 8045 ASSERT(*prevp == NULL); 8046 *prevp = new; 8047 } 8048 8049 bucket->dthb_chain = new; 8050 bucket->dthb_len++; 8051 } 8052 8053 static dtrace_probe_t * 8054 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 8055 { 8056 int hashval = DTRACE_HASHSTR(hash, template); 8057 int ndx = hashval & hash->dth_mask; 8058 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8059 8060 for (; bucket != NULL; bucket = bucket->dthb_next) { 8061 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 8062 return (bucket->dthb_chain); 8063 } 8064 8065 return (NULL); 8066 } 8067 8068 static int 8069 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 8070 { 8071 int hashval = DTRACE_HASHSTR(hash, template); 8072 int ndx = hashval & hash->dth_mask; 8073 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8074 8075 for (; bucket != NULL; bucket = bucket->dthb_next) { 8076 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 8077 return (bucket->dthb_len); 8078 } 8079 8080 return (0); 8081 } 8082 8083 static void 8084 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 8085 { 8086 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 8087 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8088 8089 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 8090 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 8091 8092 /* 8093 * Find the bucket that we're removing this probe from. 8094 */ 8095 for (; bucket != NULL; bucket = bucket->dthb_next) { 8096 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 8097 break; 8098 } 8099 8100 ASSERT(bucket != NULL); 8101 8102 if (*prevp == NULL) { 8103 if (*nextp == NULL) { 8104 /* 8105 * The removed probe was the only probe on this 8106 * bucket; we need to remove the bucket. 8107 */ 8108 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 8109 8110 ASSERT(bucket->dthb_chain == probe); 8111 ASSERT(b != NULL); 8112 8113 if (b == bucket) { 8114 hash->dth_tab[ndx] = bucket->dthb_next; 8115 } else { 8116 while (b->dthb_next != bucket) 8117 b = b->dthb_next; 8118 b->dthb_next = bucket->dthb_next; 8119 } 8120 8121 ASSERT(hash->dth_nbuckets > 0); 8122 hash->dth_nbuckets--; 8123 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 8124 return; 8125 } 8126 8127 bucket->dthb_chain = *nextp; 8128 } else { 8129 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 8130 } 8131 8132 if (*nextp != NULL) 8133 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 8134 } 8135 8136 /* 8137 * DTrace Utility Functions 8138 * 8139 * These are random utility functions that are _not_ called from probe context. 8140 */ 8141 static int 8142 dtrace_badattr(const dtrace_attribute_t *a) 8143 { 8144 return (a->dtat_name > DTRACE_STABILITY_MAX || 8145 a->dtat_data > DTRACE_STABILITY_MAX || 8146 a->dtat_class > DTRACE_CLASS_MAX); 8147 } 8148 8149 /* 8150 * Return a duplicate copy of a string. If the specified string is NULL, 8151 * this function returns a zero-length string. 8152 */ 8153 static char * 8154 dtrace_strdup(const char *str) 8155 { 8156 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 8157 8158 if (str != NULL) 8159 (void) strcpy(new, str); 8160 8161 return (new); 8162 } 8163 8164 #define DTRACE_ISALPHA(c) \ 8165 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 8166 8167 static int 8168 dtrace_badname(const char *s) 8169 { 8170 char c; 8171 8172 if (s == NULL || (c = *s++) == '\0') 8173 return (0); 8174 8175 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 8176 return (1); 8177 8178 while ((c = *s++) != '\0') { 8179 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 8180 c != '-' && c != '_' && c != '.' && c != '`') 8181 return (1); 8182 } 8183 8184 return (0); 8185 } 8186 8187 static void 8188 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 8189 { 8190 uint32_t priv; 8191 8192 #ifdef illumos 8193 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 8194 /* 8195 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 8196 */ 8197 priv = DTRACE_PRIV_ALL; 8198 } else { 8199 *uidp = crgetuid(cr); 8200 *zoneidp = crgetzoneid(cr); 8201 8202 priv = 0; 8203 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 8204 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 8205 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 8206 priv |= DTRACE_PRIV_USER; 8207 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 8208 priv |= DTRACE_PRIV_PROC; 8209 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 8210 priv |= DTRACE_PRIV_OWNER; 8211 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 8212 priv |= DTRACE_PRIV_ZONEOWNER; 8213 } 8214 #else 8215 priv = DTRACE_PRIV_ALL; 8216 #endif 8217 8218 *privp = priv; 8219 } 8220 8221 #ifdef DTRACE_ERRDEBUG 8222 static void 8223 dtrace_errdebug(const char *str) 8224 { 8225 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 8226 int occupied = 0; 8227 8228 mutex_enter(&dtrace_errlock); 8229 dtrace_errlast = str; 8230 dtrace_errthread = curthread; 8231 8232 while (occupied++ < DTRACE_ERRHASHSZ) { 8233 if (dtrace_errhash[hval].dter_msg == str) { 8234 dtrace_errhash[hval].dter_count++; 8235 goto out; 8236 } 8237 8238 if (dtrace_errhash[hval].dter_msg != NULL) { 8239 hval = (hval + 1) % DTRACE_ERRHASHSZ; 8240 continue; 8241 } 8242 8243 dtrace_errhash[hval].dter_msg = str; 8244 dtrace_errhash[hval].dter_count = 1; 8245 goto out; 8246 } 8247 8248 panic("dtrace: undersized error hash"); 8249 out: 8250 mutex_exit(&dtrace_errlock); 8251 } 8252 #endif 8253 8254 /* 8255 * DTrace Matching Functions 8256 * 8257 * These functions are used to match groups of probes, given some elements of 8258 * a probe tuple, or some globbed expressions for elements of a probe tuple. 8259 */ 8260 static int 8261 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 8262 zoneid_t zoneid) 8263 { 8264 if (priv != DTRACE_PRIV_ALL) { 8265 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 8266 uint32_t match = priv & ppriv; 8267 8268 /* 8269 * No PRIV_DTRACE_* privileges... 8270 */ 8271 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 8272 DTRACE_PRIV_KERNEL)) == 0) 8273 return (0); 8274 8275 /* 8276 * No matching bits, but there were bits to match... 8277 */ 8278 if (match == 0 && ppriv != 0) 8279 return (0); 8280 8281 /* 8282 * Need to have permissions to the process, but don't... 8283 */ 8284 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 8285 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 8286 return (0); 8287 } 8288 8289 /* 8290 * Need to be in the same zone unless we possess the 8291 * privilege to examine all zones. 8292 */ 8293 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 8294 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 8295 return (0); 8296 } 8297 } 8298 8299 return (1); 8300 } 8301 8302 /* 8303 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 8304 * consists of input pattern strings and an ops-vector to evaluate them. 8305 * This function returns >0 for match, 0 for no match, and <0 for error. 8306 */ 8307 static int 8308 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 8309 uint32_t priv, uid_t uid, zoneid_t zoneid) 8310 { 8311 dtrace_provider_t *pvp = prp->dtpr_provider; 8312 int rv; 8313 8314 if (pvp->dtpv_defunct) 8315 return (0); 8316 8317 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 8318 return (rv); 8319 8320 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 8321 return (rv); 8322 8323 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 8324 return (rv); 8325 8326 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 8327 return (rv); 8328 8329 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 8330 return (0); 8331 8332 return (rv); 8333 } 8334 8335 /* 8336 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 8337 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 8338 * libc's version, the kernel version only applies to 8-bit ASCII strings. 8339 * In addition, all of the recursion cases except for '*' matching have been 8340 * unwound. For '*', we still implement recursive evaluation, but a depth 8341 * counter is maintained and matching is aborted if we recurse too deep. 8342 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 8343 */ 8344 static int 8345 dtrace_match_glob(const char *s, const char *p, int depth) 8346 { 8347 const char *olds; 8348 char s1, c; 8349 int gs; 8350 8351 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 8352 return (-1); 8353 8354 if (s == NULL) 8355 s = ""; /* treat NULL as empty string */ 8356 8357 top: 8358 olds = s; 8359 s1 = *s++; 8360 8361 if (p == NULL) 8362 return (0); 8363 8364 if ((c = *p++) == '\0') 8365 return (s1 == '\0'); 8366 8367 switch (c) { 8368 case '[': { 8369 int ok = 0, notflag = 0; 8370 char lc = '\0'; 8371 8372 if (s1 == '\0') 8373 return (0); 8374 8375 if (*p == '!') { 8376 notflag = 1; 8377 p++; 8378 } 8379 8380 if ((c = *p++) == '\0') 8381 return (0); 8382 8383 do { 8384 if (c == '-' && lc != '\0' && *p != ']') { 8385 if ((c = *p++) == '\0') 8386 return (0); 8387 if (c == '\\' && (c = *p++) == '\0') 8388 return (0); 8389 8390 if (notflag) { 8391 if (s1 < lc || s1 > c) 8392 ok++; 8393 else 8394 return (0); 8395 } else if (lc <= s1 && s1 <= c) 8396 ok++; 8397 8398 } else if (c == '\\' && (c = *p++) == '\0') 8399 return (0); 8400 8401 lc = c; /* save left-hand 'c' for next iteration */ 8402 8403 if (notflag) { 8404 if (s1 != c) 8405 ok++; 8406 else 8407 return (0); 8408 } else if (s1 == c) 8409 ok++; 8410 8411 if ((c = *p++) == '\0') 8412 return (0); 8413 8414 } while (c != ']'); 8415 8416 if (ok) 8417 goto top; 8418 8419 return (0); 8420 } 8421 8422 case '\\': 8423 if ((c = *p++) == '\0') 8424 return (0); 8425 /*FALLTHRU*/ 8426 8427 default: 8428 if (c != s1) 8429 return (0); 8430 /*FALLTHRU*/ 8431 8432 case '?': 8433 if (s1 != '\0') 8434 goto top; 8435 return (0); 8436 8437 case '*': 8438 while (*p == '*') 8439 p++; /* consecutive *'s are identical to a single one */ 8440 8441 if (*p == '\0') 8442 return (1); 8443 8444 for (s = olds; *s != '\0'; s++) { 8445 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 8446 return (gs); 8447 } 8448 8449 return (0); 8450 } 8451 } 8452 8453 /*ARGSUSED*/ 8454 static int 8455 dtrace_match_string(const char *s, const char *p, int depth) 8456 { 8457 return (s != NULL && strcmp(s, p) == 0); 8458 } 8459 8460 /*ARGSUSED*/ 8461 static int 8462 dtrace_match_nul(const char *s, const char *p, int depth) 8463 { 8464 return (1); /* always match the empty pattern */ 8465 } 8466 8467 /*ARGSUSED*/ 8468 static int 8469 dtrace_match_nonzero(const char *s, const char *p, int depth) 8470 { 8471 return (s != NULL && s[0] != '\0'); 8472 } 8473 8474 static int 8475 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 8476 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 8477 { 8478 dtrace_probe_t template, *probe; 8479 dtrace_hash_t *hash = NULL; 8480 int len, best = INT_MAX, nmatched = 0; 8481 dtrace_id_t i; 8482 8483 ASSERT(MUTEX_HELD(&dtrace_lock)); 8484 8485 /* 8486 * If the probe ID is specified in the key, just lookup by ID and 8487 * invoke the match callback once if a matching probe is found. 8488 */ 8489 if (pkp->dtpk_id != DTRACE_IDNONE) { 8490 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 8491 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 8492 (void) (*matched)(probe, arg); 8493 nmatched++; 8494 } 8495 return (nmatched); 8496 } 8497 8498 template.dtpr_mod = (char *)pkp->dtpk_mod; 8499 template.dtpr_func = (char *)pkp->dtpk_func; 8500 template.dtpr_name = (char *)pkp->dtpk_name; 8501 8502 /* 8503 * We want to find the most distinct of the module name, function 8504 * name, and name. So for each one that is not a glob pattern or 8505 * empty string, we perform a lookup in the corresponding hash and 8506 * use the hash table with the fewest collisions to do our search. 8507 */ 8508 if (pkp->dtpk_mmatch == &dtrace_match_string && 8509 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 8510 best = len; 8511 hash = dtrace_bymod; 8512 } 8513 8514 if (pkp->dtpk_fmatch == &dtrace_match_string && 8515 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 8516 best = len; 8517 hash = dtrace_byfunc; 8518 } 8519 8520 if (pkp->dtpk_nmatch == &dtrace_match_string && 8521 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 8522 best = len; 8523 hash = dtrace_byname; 8524 } 8525 8526 /* 8527 * If we did not select a hash table, iterate over every probe and 8528 * invoke our callback for each one that matches our input probe key. 8529 */ 8530 if (hash == NULL) { 8531 for (i = 0; i < dtrace_nprobes; i++) { 8532 if ((probe = dtrace_probes[i]) == NULL || 8533 dtrace_match_probe(probe, pkp, priv, uid, 8534 zoneid) <= 0) 8535 continue; 8536 8537 nmatched++; 8538 8539 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8540 break; 8541 } 8542 8543 return (nmatched); 8544 } 8545 8546 /* 8547 * If we selected a hash table, iterate over each probe of the same key 8548 * name and invoke the callback for every probe that matches the other 8549 * attributes of our input probe key. 8550 */ 8551 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 8552 probe = *(DTRACE_HASHNEXT(hash, probe))) { 8553 8554 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 8555 continue; 8556 8557 nmatched++; 8558 8559 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8560 break; 8561 } 8562 8563 return (nmatched); 8564 } 8565 8566 /* 8567 * Return the function pointer dtrace_probecmp() should use to compare the 8568 * specified pattern with a string. For NULL or empty patterns, we select 8569 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 8570 * For non-empty non-glob strings, we use dtrace_match_string(). 8571 */ 8572 static dtrace_probekey_f * 8573 dtrace_probekey_func(const char *p) 8574 { 8575 char c; 8576 8577 if (p == NULL || *p == '\0') 8578 return (&dtrace_match_nul); 8579 8580 while ((c = *p++) != '\0') { 8581 if (c == '[' || c == '?' || c == '*' || c == '\\') 8582 return (&dtrace_match_glob); 8583 } 8584 8585 return (&dtrace_match_string); 8586 } 8587 8588 /* 8589 * Build a probe comparison key for use with dtrace_match_probe() from the 8590 * given probe description. By convention, a null key only matches anchored 8591 * probes: if each field is the empty string, reset dtpk_fmatch to 8592 * dtrace_match_nonzero(). 8593 */ 8594 static void 8595 dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 8596 { 8597 pkp->dtpk_prov = pdp->dtpd_provider; 8598 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 8599 8600 pkp->dtpk_mod = pdp->dtpd_mod; 8601 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 8602 8603 pkp->dtpk_func = pdp->dtpd_func; 8604 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 8605 8606 pkp->dtpk_name = pdp->dtpd_name; 8607 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 8608 8609 pkp->dtpk_id = pdp->dtpd_id; 8610 8611 if (pkp->dtpk_id == DTRACE_IDNONE && 8612 pkp->dtpk_pmatch == &dtrace_match_nul && 8613 pkp->dtpk_mmatch == &dtrace_match_nul && 8614 pkp->dtpk_fmatch == &dtrace_match_nul && 8615 pkp->dtpk_nmatch == &dtrace_match_nul) 8616 pkp->dtpk_fmatch = &dtrace_match_nonzero; 8617 } 8618 8619 /* 8620 * DTrace Provider-to-Framework API Functions 8621 * 8622 * These functions implement much of the Provider-to-Framework API, as 8623 * described in <sys/dtrace.h>. The parts of the API not in this section are 8624 * the functions in the API for probe management (found below), and 8625 * dtrace_probe() itself (found above). 8626 */ 8627 8628 /* 8629 * Register the calling provider with the DTrace framework. This should 8630 * generally be called by DTrace providers in their attach(9E) entry point. 8631 */ 8632 int 8633 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 8634 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 8635 { 8636 dtrace_provider_t *provider; 8637 8638 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 8639 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8640 "arguments", name ? name : "<NULL>"); 8641 return (EINVAL); 8642 } 8643 8644 if (name[0] == '\0' || dtrace_badname(name)) { 8645 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8646 "provider name", name); 8647 return (EINVAL); 8648 } 8649 8650 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 8651 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 8652 pops->dtps_destroy == NULL || 8653 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 8654 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8655 "provider ops", name); 8656 return (EINVAL); 8657 } 8658 8659 if (dtrace_badattr(&pap->dtpa_provider) || 8660 dtrace_badattr(&pap->dtpa_mod) || 8661 dtrace_badattr(&pap->dtpa_func) || 8662 dtrace_badattr(&pap->dtpa_name) || 8663 dtrace_badattr(&pap->dtpa_args)) { 8664 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8665 "provider attributes", name); 8666 return (EINVAL); 8667 } 8668 8669 if (priv & ~DTRACE_PRIV_ALL) { 8670 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8671 "privilege attributes", name); 8672 return (EINVAL); 8673 } 8674 8675 if ((priv & DTRACE_PRIV_KERNEL) && 8676 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 8677 pops->dtps_usermode == NULL) { 8678 cmn_err(CE_WARN, "failed to register provider '%s': need " 8679 "dtps_usermode() op for given privilege attributes", name); 8680 return (EINVAL); 8681 } 8682 8683 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 8684 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8685 (void) strcpy(provider->dtpv_name, name); 8686 8687 provider->dtpv_attr = *pap; 8688 provider->dtpv_priv.dtpp_flags = priv; 8689 if (cr != NULL) { 8690 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 8691 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 8692 } 8693 provider->dtpv_pops = *pops; 8694 8695 if (pops->dtps_provide == NULL) { 8696 ASSERT(pops->dtps_provide_module != NULL); 8697 provider->dtpv_pops.dtps_provide = 8698 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 8699 } 8700 8701 if (pops->dtps_provide_module == NULL) { 8702 ASSERT(pops->dtps_provide != NULL); 8703 provider->dtpv_pops.dtps_provide_module = 8704 (void (*)(void *, modctl_t *))dtrace_nullop; 8705 } 8706 8707 if (pops->dtps_suspend == NULL) { 8708 ASSERT(pops->dtps_resume == NULL); 8709 provider->dtpv_pops.dtps_suspend = 8710 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8711 provider->dtpv_pops.dtps_resume = 8712 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8713 } 8714 8715 provider->dtpv_arg = arg; 8716 *idp = (dtrace_provider_id_t)provider; 8717 8718 if (pops == &dtrace_provider_ops) { 8719 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8720 ASSERT(MUTEX_HELD(&dtrace_lock)); 8721 ASSERT(dtrace_anon.dta_enabling == NULL); 8722 8723 /* 8724 * We make sure that the DTrace provider is at the head of 8725 * the provider chain. 8726 */ 8727 provider->dtpv_next = dtrace_provider; 8728 dtrace_provider = provider; 8729 return (0); 8730 } 8731 8732 mutex_enter(&dtrace_provider_lock); 8733 mutex_enter(&dtrace_lock); 8734 8735 /* 8736 * If there is at least one provider registered, we'll add this 8737 * provider after the first provider. 8738 */ 8739 if (dtrace_provider != NULL) { 8740 provider->dtpv_next = dtrace_provider->dtpv_next; 8741 dtrace_provider->dtpv_next = provider; 8742 } else { 8743 dtrace_provider = provider; 8744 } 8745 8746 if (dtrace_retained != NULL) { 8747 dtrace_enabling_provide(provider); 8748 8749 /* 8750 * Now we need to call dtrace_enabling_matchall() -- which 8751 * will acquire cpu_lock and dtrace_lock. We therefore need 8752 * to drop all of our locks before calling into it... 8753 */ 8754 mutex_exit(&dtrace_lock); 8755 mutex_exit(&dtrace_provider_lock); 8756 dtrace_enabling_matchall(); 8757 8758 return (0); 8759 } 8760 8761 mutex_exit(&dtrace_lock); 8762 mutex_exit(&dtrace_provider_lock); 8763 8764 return (0); 8765 } 8766 8767 /* 8768 * Unregister the specified provider from the DTrace framework. This should 8769 * generally be called by DTrace providers in their detach(9E) entry point. 8770 */ 8771 int 8772 dtrace_unregister(dtrace_provider_id_t id) 8773 { 8774 dtrace_provider_t *old = (dtrace_provider_t *)id; 8775 dtrace_provider_t *prev = NULL; 8776 int i, self = 0, noreap = 0; 8777 dtrace_probe_t *probe, *first = NULL; 8778 8779 if (old->dtpv_pops.dtps_enable == 8780 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 8781 /* 8782 * If DTrace itself is the provider, we're called with locks 8783 * already held. 8784 */ 8785 ASSERT(old == dtrace_provider); 8786 #ifdef illumos 8787 ASSERT(dtrace_devi != NULL); 8788 #endif 8789 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8790 ASSERT(MUTEX_HELD(&dtrace_lock)); 8791 self = 1; 8792 8793 if (dtrace_provider->dtpv_next != NULL) { 8794 /* 8795 * There's another provider here; return failure. 8796 */ 8797 return (EBUSY); 8798 } 8799 } else { 8800 mutex_enter(&dtrace_provider_lock); 8801 #ifdef illumos 8802 mutex_enter(&mod_lock); 8803 #endif 8804 mutex_enter(&dtrace_lock); 8805 } 8806 8807 /* 8808 * If anyone has /dev/dtrace open, or if there are anonymous enabled 8809 * probes, we refuse to let providers slither away, unless this 8810 * provider has already been explicitly invalidated. 8811 */ 8812 if (!old->dtpv_defunct && 8813 (dtrace_opens || (dtrace_anon.dta_state != NULL && 8814 dtrace_anon.dta_state->dts_necbs > 0))) { 8815 if (!self) { 8816 mutex_exit(&dtrace_lock); 8817 #ifdef illumos 8818 mutex_exit(&mod_lock); 8819 #endif 8820 mutex_exit(&dtrace_provider_lock); 8821 } 8822 return (EBUSY); 8823 } 8824 8825 /* 8826 * Attempt to destroy the probes associated with this provider. 8827 */ 8828 for (i = 0; i < dtrace_nprobes; i++) { 8829 if ((probe = dtrace_probes[i]) == NULL) 8830 continue; 8831 8832 if (probe->dtpr_provider != old) 8833 continue; 8834 8835 if (probe->dtpr_ecb == NULL) 8836 continue; 8837 8838 /* 8839 * If we are trying to unregister a defunct provider, and the 8840 * provider was made defunct within the interval dictated by 8841 * dtrace_unregister_defunct_reap, we'll (asynchronously) 8842 * attempt to reap our enablings. To denote that the provider 8843 * should reattempt to unregister itself at some point in the 8844 * future, we will return a differentiable error code (EAGAIN 8845 * instead of EBUSY) in this case. 8846 */ 8847 if (dtrace_gethrtime() - old->dtpv_defunct > 8848 dtrace_unregister_defunct_reap) 8849 noreap = 1; 8850 8851 if (!self) { 8852 mutex_exit(&dtrace_lock); 8853 #ifdef illumos 8854 mutex_exit(&mod_lock); 8855 #endif 8856 mutex_exit(&dtrace_provider_lock); 8857 } 8858 8859 if (noreap) 8860 return (EBUSY); 8861 8862 (void) taskq_dispatch(dtrace_taskq, 8863 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 8864 8865 return (EAGAIN); 8866 } 8867 8868 /* 8869 * All of the probes for this provider are disabled; we can safely 8870 * remove all of them from their hash chains and from the probe array. 8871 */ 8872 for (i = 0; i < dtrace_nprobes; i++) { 8873 if ((probe = dtrace_probes[i]) == NULL) 8874 continue; 8875 8876 if (probe->dtpr_provider != old) 8877 continue; 8878 8879 dtrace_probes[i] = NULL; 8880 8881 dtrace_hash_remove(dtrace_bymod, probe); 8882 dtrace_hash_remove(dtrace_byfunc, probe); 8883 dtrace_hash_remove(dtrace_byname, probe); 8884 8885 if (first == NULL) { 8886 first = probe; 8887 probe->dtpr_nextmod = NULL; 8888 } else { 8889 probe->dtpr_nextmod = first; 8890 first = probe; 8891 } 8892 } 8893 8894 /* 8895 * The provider's probes have been removed from the hash chains and 8896 * from the probe array. Now issue a dtrace_sync() to be sure that 8897 * everyone has cleared out from any probe array processing. 8898 */ 8899 dtrace_sync(); 8900 8901 for (probe = first; probe != NULL; probe = first) { 8902 first = probe->dtpr_nextmod; 8903 8904 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 8905 probe->dtpr_arg); 8906 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8907 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8908 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8909 #ifdef illumos 8910 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 8911 #else 8912 free_unr(dtrace_arena, probe->dtpr_id); 8913 #endif 8914 kmem_free(probe, sizeof (dtrace_probe_t)); 8915 } 8916 8917 if ((prev = dtrace_provider) == old) { 8918 #ifdef illumos 8919 ASSERT(self || dtrace_devi == NULL); 8920 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 8921 #endif 8922 dtrace_provider = old->dtpv_next; 8923 } else { 8924 while (prev != NULL && prev->dtpv_next != old) 8925 prev = prev->dtpv_next; 8926 8927 if (prev == NULL) { 8928 panic("attempt to unregister non-existent " 8929 "dtrace provider %p\n", (void *)id); 8930 } 8931 8932 prev->dtpv_next = old->dtpv_next; 8933 } 8934 8935 if (!self) { 8936 mutex_exit(&dtrace_lock); 8937 #ifdef illumos 8938 mutex_exit(&mod_lock); 8939 #endif 8940 mutex_exit(&dtrace_provider_lock); 8941 } 8942 8943 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 8944 kmem_free(old, sizeof (dtrace_provider_t)); 8945 8946 return (0); 8947 } 8948 8949 /* 8950 * Invalidate the specified provider. All subsequent probe lookups for the 8951 * specified provider will fail, but its probes will not be removed. 8952 */ 8953 void 8954 dtrace_invalidate(dtrace_provider_id_t id) 8955 { 8956 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 8957 8958 ASSERT(pvp->dtpv_pops.dtps_enable != 8959 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8960 8961 mutex_enter(&dtrace_provider_lock); 8962 mutex_enter(&dtrace_lock); 8963 8964 pvp->dtpv_defunct = dtrace_gethrtime(); 8965 8966 mutex_exit(&dtrace_lock); 8967 mutex_exit(&dtrace_provider_lock); 8968 } 8969 8970 /* 8971 * Indicate whether or not DTrace has attached. 8972 */ 8973 int 8974 dtrace_attached(void) 8975 { 8976 /* 8977 * dtrace_provider will be non-NULL iff the DTrace driver has 8978 * attached. (It's non-NULL because DTrace is always itself a 8979 * provider.) 8980 */ 8981 return (dtrace_provider != NULL); 8982 } 8983 8984 /* 8985 * Remove all the unenabled probes for the given provider. This function is 8986 * not unlike dtrace_unregister(), except that it doesn't remove the provider 8987 * -- just as many of its associated probes as it can. 8988 */ 8989 int 8990 dtrace_condense(dtrace_provider_id_t id) 8991 { 8992 dtrace_provider_t *prov = (dtrace_provider_t *)id; 8993 int i; 8994 dtrace_probe_t *probe; 8995 8996 /* 8997 * Make sure this isn't the dtrace provider itself. 8998 */ 8999 ASSERT(prov->dtpv_pops.dtps_enable != 9000 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 9001 9002 mutex_enter(&dtrace_provider_lock); 9003 mutex_enter(&dtrace_lock); 9004 9005 /* 9006 * Attempt to destroy the probes associated with this provider. 9007 */ 9008 for (i = 0; i < dtrace_nprobes; i++) { 9009 if ((probe = dtrace_probes[i]) == NULL) 9010 continue; 9011 9012 if (probe->dtpr_provider != prov) 9013 continue; 9014 9015 if (probe->dtpr_ecb != NULL) 9016 continue; 9017 9018 dtrace_probes[i] = NULL; 9019 9020 dtrace_hash_remove(dtrace_bymod, probe); 9021 dtrace_hash_remove(dtrace_byfunc, probe); 9022 dtrace_hash_remove(dtrace_byname, probe); 9023 9024 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 9025 probe->dtpr_arg); 9026 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 9027 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 9028 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 9029 kmem_free(probe, sizeof (dtrace_probe_t)); 9030 #ifdef illumos 9031 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 9032 #else 9033 free_unr(dtrace_arena, i + 1); 9034 #endif 9035 } 9036 9037 mutex_exit(&dtrace_lock); 9038 mutex_exit(&dtrace_provider_lock); 9039 9040 return (0); 9041 } 9042 9043 /* 9044 * DTrace Probe Management Functions 9045 * 9046 * The functions in this section perform the DTrace probe management, 9047 * including functions to create probes, look-up probes, and call into the 9048 * providers to request that probes be provided. Some of these functions are 9049 * in the Provider-to-Framework API; these functions can be identified by the 9050 * fact that they are not declared "static". 9051 */ 9052 9053 /* 9054 * Create a probe with the specified module name, function name, and name. 9055 */ 9056 dtrace_id_t 9057 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 9058 const char *func, const char *name, int aframes, void *arg) 9059 { 9060 dtrace_probe_t *probe, **probes; 9061 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 9062 dtrace_id_t id; 9063 9064 if (provider == dtrace_provider) { 9065 ASSERT(MUTEX_HELD(&dtrace_lock)); 9066 } else { 9067 mutex_enter(&dtrace_lock); 9068 } 9069 9070 #ifdef illumos 9071 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 9072 VM_BESTFIT | VM_SLEEP); 9073 #else 9074 id = alloc_unr(dtrace_arena); 9075 #endif 9076 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 9077 9078 probe->dtpr_id = id; 9079 probe->dtpr_gen = dtrace_probegen++; 9080 probe->dtpr_mod = dtrace_strdup(mod); 9081 probe->dtpr_func = dtrace_strdup(func); 9082 probe->dtpr_name = dtrace_strdup(name); 9083 probe->dtpr_arg = arg; 9084 probe->dtpr_aframes = aframes; 9085 probe->dtpr_provider = provider; 9086 9087 dtrace_hash_add(dtrace_bymod, probe); 9088 dtrace_hash_add(dtrace_byfunc, probe); 9089 dtrace_hash_add(dtrace_byname, probe); 9090 9091 if (id - 1 >= dtrace_nprobes) { 9092 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 9093 size_t nsize = osize << 1; 9094 9095 if (nsize == 0) { 9096 ASSERT(osize == 0); 9097 ASSERT(dtrace_probes == NULL); 9098 nsize = sizeof (dtrace_probe_t *); 9099 } 9100 9101 probes = kmem_zalloc(nsize, KM_SLEEP); 9102 9103 if (dtrace_probes == NULL) { 9104 ASSERT(osize == 0); 9105 dtrace_probes = probes; 9106 dtrace_nprobes = 1; 9107 } else { 9108 dtrace_probe_t **oprobes = dtrace_probes; 9109 9110 bcopy(oprobes, probes, osize); 9111 dtrace_membar_producer(); 9112 dtrace_probes = probes; 9113 9114 dtrace_sync(); 9115 9116 /* 9117 * All CPUs are now seeing the new probes array; we can 9118 * safely free the old array. 9119 */ 9120 kmem_free(oprobes, osize); 9121 dtrace_nprobes <<= 1; 9122 } 9123 9124 ASSERT(id - 1 < dtrace_nprobes); 9125 } 9126 9127 ASSERT(dtrace_probes[id - 1] == NULL); 9128 dtrace_probes[id - 1] = probe; 9129 9130 if (provider != dtrace_provider) 9131 mutex_exit(&dtrace_lock); 9132 9133 return (id); 9134 } 9135 9136 static dtrace_probe_t * 9137 dtrace_probe_lookup_id(dtrace_id_t id) 9138 { 9139 ASSERT(MUTEX_HELD(&dtrace_lock)); 9140 9141 if (id == 0 || id > dtrace_nprobes) 9142 return (NULL); 9143 9144 return (dtrace_probes[id - 1]); 9145 } 9146 9147 static int 9148 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 9149 { 9150 *((dtrace_id_t *)arg) = probe->dtpr_id; 9151 9152 return (DTRACE_MATCH_DONE); 9153 } 9154 9155 /* 9156 * Look up a probe based on provider and one or more of module name, function 9157 * name and probe name. 9158 */ 9159 dtrace_id_t 9160 dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 9161 char *func, char *name) 9162 { 9163 dtrace_probekey_t pkey; 9164 dtrace_id_t id; 9165 int match; 9166 9167 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 9168 pkey.dtpk_pmatch = &dtrace_match_string; 9169 pkey.dtpk_mod = mod; 9170 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 9171 pkey.dtpk_func = func; 9172 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 9173 pkey.dtpk_name = name; 9174 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 9175 pkey.dtpk_id = DTRACE_IDNONE; 9176 9177 mutex_enter(&dtrace_lock); 9178 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 9179 dtrace_probe_lookup_match, &id); 9180 mutex_exit(&dtrace_lock); 9181 9182 ASSERT(match == 1 || match == 0); 9183 return (match ? id : 0); 9184 } 9185 9186 /* 9187 * Returns the probe argument associated with the specified probe. 9188 */ 9189 void * 9190 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 9191 { 9192 dtrace_probe_t *probe; 9193 void *rval = NULL; 9194 9195 mutex_enter(&dtrace_lock); 9196 9197 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 9198 probe->dtpr_provider == (dtrace_provider_t *)id) 9199 rval = probe->dtpr_arg; 9200 9201 mutex_exit(&dtrace_lock); 9202 9203 return (rval); 9204 } 9205 9206 /* 9207 * Copy a probe into a probe description. 9208 */ 9209 static void 9210 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 9211 { 9212 bzero(pdp, sizeof (dtrace_probedesc_t)); 9213 pdp->dtpd_id = prp->dtpr_id; 9214 9215 (void) strncpy(pdp->dtpd_provider, 9216 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 9217 9218 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 9219 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 9220 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 9221 } 9222 9223 /* 9224 * Called to indicate that a probe -- or probes -- should be provided by a 9225 * specfied provider. If the specified description is NULL, the provider will 9226 * be told to provide all of its probes. (This is done whenever a new 9227 * consumer comes along, or whenever a retained enabling is to be matched.) If 9228 * the specified description is non-NULL, the provider is given the 9229 * opportunity to dynamically provide the specified probe, allowing providers 9230 * to support the creation of probes on-the-fly. (So-called _autocreated_ 9231 * probes.) If the provider is NULL, the operations will be applied to all 9232 * providers; if the provider is non-NULL the operations will only be applied 9233 * to the specified provider. The dtrace_provider_lock must be held, and the 9234 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 9235 * will need to grab the dtrace_lock when it reenters the framework through 9236 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 9237 */ 9238 static void 9239 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 9240 { 9241 #ifdef illumos 9242 modctl_t *ctl; 9243 #endif 9244 int all = 0; 9245 9246 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9247 9248 if (prv == NULL) { 9249 all = 1; 9250 prv = dtrace_provider; 9251 } 9252 9253 do { 9254 /* 9255 * First, call the blanket provide operation. 9256 */ 9257 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 9258 9259 #ifdef illumos 9260 /* 9261 * Now call the per-module provide operation. We will grab 9262 * mod_lock to prevent the list from being modified. Note 9263 * that this also prevents the mod_busy bits from changing. 9264 * (mod_busy can only be changed with mod_lock held.) 9265 */ 9266 mutex_enter(&mod_lock); 9267 9268 ctl = &modules; 9269 do { 9270 if (ctl->mod_busy || ctl->mod_mp == NULL) 9271 continue; 9272 9273 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 9274 9275 } while ((ctl = ctl->mod_next) != &modules); 9276 9277 mutex_exit(&mod_lock); 9278 #endif 9279 } while (all && (prv = prv->dtpv_next) != NULL); 9280 } 9281 9282 #ifdef illumos 9283 /* 9284 * Iterate over each probe, and call the Framework-to-Provider API function 9285 * denoted by offs. 9286 */ 9287 static void 9288 dtrace_probe_foreach(uintptr_t offs) 9289 { 9290 dtrace_provider_t *prov; 9291 void (*func)(void *, dtrace_id_t, void *); 9292 dtrace_probe_t *probe; 9293 dtrace_icookie_t cookie; 9294 int i; 9295 9296 /* 9297 * We disable interrupts to walk through the probe array. This is 9298 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 9299 * won't see stale data. 9300 */ 9301 cookie = dtrace_interrupt_disable(); 9302 9303 for (i = 0; i < dtrace_nprobes; i++) { 9304 if ((probe = dtrace_probes[i]) == NULL) 9305 continue; 9306 9307 if (probe->dtpr_ecb == NULL) { 9308 /* 9309 * This probe isn't enabled -- don't call the function. 9310 */ 9311 continue; 9312 } 9313 9314 prov = probe->dtpr_provider; 9315 func = *((void(**)(void *, dtrace_id_t, void *)) 9316 ((uintptr_t)&prov->dtpv_pops + offs)); 9317 9318 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 9319 } 9320 9321 dtrace_interrupt_enable(cookie); 9322 } 9323 #endif 9324 9325 static int 9326 dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 9327 { 9328 dtrace_probekey_t pkey; 9329 uint32_t priv; 9330 uid_t uid; 9331 zoneid_t zoneid; 9332 9333 ASSERT(MUTEX_HELD(&dtrace_lock)); 9334 dtrace_ecb_create_cache = NULL; 9335 9336 if (desc == NULL) { 9337 /* 9338 * If we're passed a NULL description, we're being asked to 9339 * create an ECB with a NULL probe. 9340 */ 9341 (void) dtrace_ecb_create_enable(NULL, enab); 9342 return (0); 9343 } 9344 9345 dtrace_probekey(desc, &pkey); 9346 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 9347 &priv, &uid, &zoneid); 9348 9349 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 9350 enab)); 9351 } 9352 9353 /* 9354 * DTrace Helper Provider Functions 9355 */ 9356 static void 9357 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 9358 { 9359 attr->dtat_name = DOF_ATTR_NAME(dofattr); 9360 attr->dtat_data = DOF_ATTR_DATA(dofattr); 9361 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 9362 } 9363 9364 static void 9365 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 9366 const dof_provider_t *dofprov, char *strtab) 9367 { 9368 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 9369 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 9370 dofprov->dofpv_provattr); 9371 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 9372 dofprov->dofpv_modattr); 9373 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 9374 dofprov->dofpv_funcattr); 9375 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 9376 dofprov->dofpv_nameattr); 9377 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 9378 dofprov->dofpv_argsattr); 9379 } 9380 9381 static void 9382 dtrace_helper_provide_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, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 9387 dof_provider_t *provider; 9388 dof_probe_t *probe; 9389 uint32_t *off, *enoff; 9390 uint8_t *arg; 9391 char *strtab; 9392 uint_t i, nprobes; 9393 dtrace_helper_provdesc_t dhpv; 9394 dtrace_helper_probedesc_t dhpb; 9395 dtrace_meta_t *meta = dtrace_meta_pid; 9396 dtrace_mops_t *mops = &meta->dtm_mops; 9397 void *parg; 9398 9399 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9400 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9401 provider->dofpv_strtab * dof->dofh_secsize); 9402 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9403 provider->dofpv_probes * dof->dofh_secsize); 9404 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9405 provider->dofpv_prargs * dof->dofh_secsize); 9406 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9407 provider->dofpv_proffs * dof->dofh_secsize); 9408 9409 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9410 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 9411 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 9412 enoff = NULL; 9413 9414 /* 9415 * See dtrace_helper_provider_validate(). 9416 */ 9417 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 9418 provider->dofpv_prenoffs != DOF_SECT_NONE) { 9419 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9420 provider->dofpv_prenoffs * dof->dofh_secsize); 9421 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 9422 } 9423 9424 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 9425 9426 /* 9427 * Create the provider. 9428 */ 9429 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9430 9431 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 9432 return; 9433 9434 meta->dtm_count++; 9435 9436 /* 9437 * Create the probes. 9438 */ 9439 for (i = 0; i < nprobes; i++) { 9440 probe = (dof_probe_t *)(uintptr_t)(daddr + 9441 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 9442 9443 /* See the check in dtrace_helper_provider_validate(). */ 9444 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) 9445 continue; 9446 9447 dhpb.dthpb_mod = dhp->dofhp_mod; 9448 dhpb.dthpb_func = strtab + probe->dofpr_func; 9449 dhpb.dthpb_name = strtab + probe->dofpr_name; 9450 dhpb.dthpb_base = probe->dofpr_addr; 9451 dhpb.dthpb_offs = off + probe->dofpr_offidx; 9452 dhpb.dthpb_noffs = probe->dofpr_noffs; 9453 if (enoff != NULL) { 9454 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 9455 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 9456 } else { 9457 dhpb.dthpb_enoffs = NULL; 9458 dhpb.dthpb_nenoffs = 0; 9459 } 9460 dhpb.dthpb_args = arg + probe->dofpr_argidx; 9461 dhpb.dthpb_nargc = probe->dofpr_nargc; 9462 dhpb.dthpb_xargc = probe->dofpr_xargc; 9463 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 9464 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 9465 9466 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 9467 } 9468 } 9469 9470 static void 9471 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 9472 { 9473 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9474 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9475 int i; 9476 9477 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9478 9479 for (i = 0; i < dof->dofh_secnum; i++) { 9480 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9481 dof->dofh_secoff + i * dof->dofh_secsize); 9482 9483 if (sec->dofs_type != DOF_SECT_PROVIDER) 9484 continue; 9485 9486 dtrace_helper_provide_one(dhp, sec, pid); 9487 } 9488 9489 /* 9490 * We may have just created probes, so we must now rematch against 9491 * any retained enablings. Note that this call will acquire both 9492 * cpu_lock and dtrace_lock; the fact that we are holding 9493 * dtrace_meta_lock now is what defines the ordering with respect to 9494 * these three locks. 9495 */ 9496 dtrace_enabling_matchall(); 9497 } 9498 9499 static void 9500 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9501 { 9502 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9503 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9504 dof_sec_t *str_sec; 9505 dof_provider_t *provider; 9506 char *strtab; 9507 dtrace_helper_provdesc_t dhpv; 9508 dtrace_meta_t *meta = dtrace_meta_pid; 9509 dtrace_mops_t *mops = &meta->dtm_mops; 9510 9511 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9512 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9513 provider->dofpv_strtab * dof->dofh_secsize); 9514 9515 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9516 9517 /* 9518 * Create the provider. 9519 */ 9520 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9521 9522 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 9523 9524 meta->dtm_count--; 9525 } 9526 9527 static void 9528 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 9529 { 9530 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9531 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9532 int i; 9533 9534 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9535 9536 for (i = 0; i < dof->dofh_secnum; i++) { 9537 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9538 dof->dofh_secoff + i * dof->dofh_secsize); 9539 9540 if (sec->dofs_type != DOF_SECT_PROVIDER) 9541 continue; 9542 9543 dtrace_helper_provider_remove_one(dhp, sec, pid); 9544 } 9545 } 9546 9547 /* 9548 * DTrace Meta Provider-to-Framework API Functions 9549 * 9550 * These functions implement the Meta Provider-to-Framework API, as described 9551 * in <sys/dtrace.h>. 9552 */ 9553 int 9554 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 9555 dtrace_meta_provider_id_t *idp) 9556 { 9557 dtrace_meta_t *meta; 9558 dtrace_helpers_t *help, *next; 9559 int i; 9560 9561 *idp = DTRACE_METAPROVNONE; 9562 9563 /* 9564 * We strictly don't need the name, but we hold onto it for 9565 * debuggability. All hail error queues! 9566 */ 9567 if (name == NULL) { 9568 cmn_err(CE_WARN, "failed to register meta-provider: " 9569 "invalid name"); 9570 return (EINVAL); 9571 } 9572 9573 if (mops == NULL || 9574 mops->dtms_create_probe == NULL || 9575 mops->dtms_provide_pid == NULL || 9576 mops->dtms_remove_pid == NULL) { 9577 cmn_err(CE_WARN, "failed to register meta-register %s: " 9578 "invalid ops", name); 9579 return (EINVAL); 9580 } 9581 9582 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 9583 meta->dtm_mops = *mops; 9584 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 9585 (void) strcpy(meta->dtm_name, name); 9586 meta->dtm_arg = arg; 9587 9588 mutex_enter(&dtrace_meta_lock); 9589 mutex_enter(&dtrace_lock); 9590 9591 if (dtrace_meta_pid != NULL) { 9592 mutex_exit(&dtrace_lock); 9593 mutex_exit(&dtrace_meta_lock); 9594 cmn_err(CE_WARN, "failed to register meta-register %s: " 9595 "user-land meta-provider exists", name); 9596 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 9597 kmem_free(meta, sizeof (dtrace_meta_t)); 9598 return (EINVAL); 9599 } 9600 9601 dtrace_meta_pid = meta; 9602 *idp = (dtrace_meta_provider_id_t)meta; 9603 9604 /* 9605 * If there are providers and probes ready to go, pass them 9606 * off to the new meta provider now. 9607 */ 9608 9609 help = dtrace_deferred_pid; 9610 dtrace_deferred_pid = NULL; 9611 9612 mutex_exit(&dtrace_lock); 9613 9614 while (help != NULL) { 9615 for (i = 0; i < help->dthps_nprovs; i++) { 9616 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 9617 help->dthps_pid); 9618 } 9619 9620 next = help->dthps_next; 9621 help->dthps_next = NULL; 9622 help->dthps_prev = NULL; 9623 help->dthps_deferred = 0; 9624 help = next; 9625 } 9626 9627 mutex_exit(&dtrace_meta_lock); 9628 9629 return (0); 9630 } 9631 9632 int 9633 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 9634 { 9635 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 9636 9637 mutex_enter(&dtrace_meta_lock); 9638 mutex_enter(&dtrace_lock); 9639 9640 if (old == dtrace_meta_pid) { 9641 pp = &dtrace_meta_pid; 9642 } else { 9643 panic("attempt to unregister non-existent " 9644 "dtrace meta-provider %p\n", (void *)old); 9645 } 9646 9647 if (old->dtm_count != 0) { 9648 mutex_exit(&dtrace_lock); 9649 mutex_exit(&dtrace_meta_lock); 9650 return (EBUSY); 9651 } 9652 9653 *pp = NULL; 9654 9655 mutex_exit(&dtrace_lock); 9656 mutex_exit(&dtrace_meta_lock); 9657 9658 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 9659 kmem_free(old, sizeof (dtrace_meta_t)); 9660 9661 return (0); 9662 } 9663 9664 9665 /* 9666 * DTrace DIF Object Functions 9667 */ 9668 static int 9669 dtrace_difo_err(uint_t pc, const char *format, ...) 9670 { 9671 if (dtrace_err_verbose) { 9672 va_list alist; 9673 9674 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 9675 va_start(alist, format); 9676 (void) vuprintf(format, alist); 9677 va_end(alist); 9678 } 9679 9680 #ifdef DTRACE_ERRDEBUG 9681 dtrace_errdebug(format); 9682 #endif 9683 return (1); 9684 } 9685 9686 /* 9687 * Validate a DTrace DIF object by checking the IR instructions. The following 9688 * rules are currently enforced by dtrace_difo_validate(): 9689 * 9690 * 1. Each instruction must have a valid opcode 9691 * 2. Each register, string, variable, or subroutine reference must be valid 9692 * 3. No instruction can modify register %r0 (must be zero) 9693 * 4. All instruction reserved bits must be set to zero 9694 * 5. The last instruction must be a "ret" instruction 9695 * 6. All branch targets must reference a valid instruction _after_ the branch 9696 */ 9697 static int 9698 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 9699 cred_t *cr) 9700 { 9701 int err = 0, i; 9702 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9703 int kcheckload; 9704 uint_t pc; 9705 int maxglobal = -1, maxlocal = -1, maxtlocal = -1; 9706 9707 kcheckload = cr == NULL || 9708 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 9709 9710 dp->dtdo_destructive = 0; 9711 9712 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 9713 dif_instr_t instr = dp->dtdo_buf[pc]; 9714 9715 uint_t r1 = DIF_INSTR_R1(instr); 9716 uint_t r2 = DIF_INSTR_R2(instr); 9717 uint_t rd = DIF_INSTR_RD(instr); 9718 uint_t rs = DIF_INSTR_RS(instr); 9719 uint_t label = DIF_INSTR_LABEL(instr); 9720 uint_t v = DIF_INSTR_VAR(instr); 9721 uint_t subr = DIF_INSTR_SUBR(instr); 9722 uint_t type = DIF_INSTR_TYPE(instr); 9723 uint_t op = DIF_INSTR_OP(instr); 9724 9725 switch (op) { 9726 case DIF_OP_OR: 9727 case DIF_OP_XOR: 9728 case DIF_OP_AND: 9729 case DIF_OP_SLL: 9730 case DIF_OP_SRL: 9731 case DIF_OP_SRA: 9732 case DIF_OP_SUB: 9733 case DIF_OP_ADD: 9734 case DIF_OP_MUL: 9735 case DIF_OP_SDIV: 9736 case DIF_OP_UDIV: 9737 case DIF_OP_SREM: 9738 case DIF_OP_UREM: 9739 case DIF_OP_COPYS: 9740 if (r1 >= nregs) 9741 err += efunc(pc, "invalid register %u\n", r1); 9742 if (r2 >= nregs) 9743 err += efunc(pc, "invalid register %u\n", r2); 9744 if (rd >= nregs) 9745 err += efunc(pc, "invalid register %u\n", rd); 9746 if (rd == 0) 9747 err += efunc(pc, "cannot write to %r0\n"); 9748 break; 9749 case DIF_OP_NOT: 9750 case DIF_OP_MOV: 9751 case DIF_OP_ALLOCS: 9752 if (r1 >= nregs) 9753 err += efunc(pc, "invalid register %u\n", r1); 9754 if (r2 != 0) 9755 err += efunc(pc, "non-zero reserved bits\n"); 9756 if (rd >= nregs) 9757 err += efunc(pc, "invalid register %u\n", rd); 9758 if (rd == 0) 9759 err += efunc(pc, "cannot write to %r0\n"); 9760 break; 9761 case DIF_OP_LDSB: 9762 case DIF_OP_LDSH: 9763 case DIF_OP_LDSW: 9764 case DIF_OP_LDUB: 9765 case DIF_OP_LDUH: 9766 case DIF_OP_LDUW: 9767 case DIF_OP_LDX: 9768 if (r1 >= nregs) 9769 err += efunc(pc, "invalid register %u\n", r1); 9770 if (r2 != 0) 9771 err += efunc(pc, "non-zero reserved bits\n"); 9772 if (rd >= nregs) 9773 err += efunc(pc, "invalid register %u\n", rd); 9774 if (rd == 0) 9775 err += efunc(pc, "cannot write to %r0\n"); 9776 if (kcheckload) 9777 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 9778 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 9779 break; 9780 case DIF_OP_RLDSB: 9781 case DIF_OP_RLDSH: 9782 case DIF_OP_RLDSW: 9783 case DIF_OP_RLDUB: 9784 case DIF_OP_RLDUH: 9785 case DIF_OP_RLDUW: 9786 case DIF_OP_RLDX: 9787 if (r1 >= nregs) 9788 err += efunc(pc, "invalid register %u\n", r1); 9789 if (r2 != 0) 9790 err += efunc(pc, "non-zero reserved bits\n"); 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_ULDSB: 9797 case DIF_OP_ULDSH: 9798 case DIF_OP_ULDSW: 9799 case DIF_OP_ULDUB: 9800 case DIF_OP_ULDUH: 9801 case DIF_OP_ULDUW: 9802 case DIF_OP_ULDX: 9803 if (r1 >= nregs) 9804 err += efunc(pc, "invalid register %u\n", r1); 9805 if (r2 != 0) 9806 err += efunc(pc, "non-zero reserved bits\n"); 9807 if (rd >= nregs) 9808 err += efunc(pc, "invalid register %u\n", rd); 9809 if (rd == 0) 9810 err += efunc(pc, "cannot write to %r0\n"); 9811 break; 9812 case DIF_OP_STB: 9813 case DIF_OP_STH: 9814 case DIF_OP_STW: 9815 case DIF_OP_STX: 9816 if (r1 >= nregs) 9817 err += efunc(pc, "invalid register %u\n", r1); 9818 if (r2 != 0) 9819 err += efunc(pc, "non-zero reserved bits\n"); 9820 if (rd >= nregs) 9821 err += efunc(pc, "invalid register %u\n", rd); 9822 if (rd == 0) 9823 err += efunc(pc, "cannot write to 0 address\n"); 9824 break; 9825 case DIF_OP_CMP: 9826 case DIF_OP_SCMP: 9827 if (r1 >= nregs) 9828 err += efunc(pc, "invalid register %u\n", r1); 9829 if (r2 >= nregs) 9830 err += efunc(pc, "invalid register %u\n", r2); 9831 if (rd != 0) 9832 err += efunc(pc, "non-zero reserved bits\n"); 9833 break; 9834 case DIF_OP_TST: 9835 if (r1 >= nregs) 9836 err += efunc(pc, "invalid register %u\n", r1); 9837 if (r2 != 0 || rd != 0) 9838 err += efunc(pc, "non-zero reserved bits\n"); 9839 break; 9840 case DIF_OP_BA: 9841 case DIF_OP_BE: 9842 case DIF_OP_BNE: 9843 case DIF_OP_BG: 9844 case DIF_OP_BGU: 9845 case DIF_OP_BGE: 9846 case DIF_OP_BGEU: 9847 case DIF_OP_BL: 9848 case DIF_OP_BLU: 9849 case DIF_OP_BLE: 9850 case DIF_OP_BLEU: 9851 if (label >= dp->dtdo_len) { 9852 err += efunc(pc, "invalid branch target %u\n", 9853 label); 9854 } 9855 if (label <= pc) { 9856 err += efunc(pc, "backward branch to %u\n", 9857 label); 9858 } 9859 break; 9860 case DIF_OP_RET: 9861 if (r1 != 0 || r2 != 0) 9862 err += efunc(pc, "non-zero reserved bits\n"); 9863 if (rd >= nregs) 9864 err += efunc(pc, "invalid register %u\n", rd); 9865 break; 9866 case DIF_OP_NOP: 9867 case DIF_OP_POPTS: 9868 case DIF_OP_FLUSHTS: 9869 if (r1 != 0 || r2 != 0 || rd != 0) 9870 err += efunc(pc, "non-zero reserved bits\n"); 9871 break; 9872 case DIF_OP_SETX: 9873 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 9874 err += efunc(pc, "invalid integer ref %u\n", 9875 DIF_INSTR_INTEGER(instr)); 9876 } 9877 if (rd >= nregs) 9878 err += efunc(pc, "invalid register %u\n", rd); 9879 if (rd == 0) 9880 err += efunc(pc, "cannot write to %r0\n"); 9881 break; 9882 case DIF_OP_SETS: 9883 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 9884 err += efunc(pc, "invalid string ref %u\n", 9885 DIF_INSTR_STRING(instr)); 9886 } 9887 if (rd >= nregs) 9888 err += efunc(pc, "invalid register %u\n", rd); 9889 if (rd == 0) 9890 err += efunc(pc, "cannot write to %r0\n"); 9891 break; 9892 case DIF_OP_LDGA: 9893 case DIF_OP_LDTA: 9894 if (r1 > DIF_VAR_ARRAY_MAX) 9895 err += efunc(pc, "invalid array %u\n", r1); 9896 if (r2 >= nregs) 9897 err += efunc(pc, "invalid register %u\n", r2); 9898 if (rd >= nregs) 9899 err += efunc(pc, "invalid register %u\n", rd); 9900 if (rd == 0) 9901 err += efunc(pc, "cannot write to %r0\n"); 9902 break; 9903 case DIF_OP_LDGS: 9904 case DIF_OP_LDTS: 9905 case DIF_OP_LDLS: 9906 case DIF_OP_LDGAA: 9907 case DIF_OP_LDTAA: 9908 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 9909 err += efunc(pc, "invalid variable %u\n", v); 9910 if (rd >= nregs) 9911 err += efunc(pc, "invalid register %u\n", rd); 9912 if (rd == 0) 9913 err += efunc(pc, "cannot write to %r0\n"); 9914 break; 9915 case DIF_OP_STGS: 9916 case DIF_OP_STTS: 9917 case DIF_OP_STLS: 9918 case DIF_OP_STGAA: 9919 case DIF_OP_STTAA: 9920 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 9921 err += efunc(pc, "invalid variable %u\n", v); 9922 if (rs >= nregs) 9923 err += efunc(pc, "invalid register %u\n", rd); 9924 break; 9925 case DIF_OP_CALL: 9926 if (subr > DIF_SUBR_MAX) 9927 err += efunc(pc, "invalid subr %u\n", subr); 9928 if (rd >= nregs) 9929 err += efunc(pc, "invalid register %u\n", rd); 9930 if (rd == 0) 9931 err += efunc(pc, "cannot write to %r0\n"); 9932 9933 if (subr == DIF_SUBR_COPYOUT || 9934 subr == DIF_SUBR_COPYOUTSTR) { 9935 dp->dtdo_destructive = 1; 9936 } 9937 9938 if (subr == DIF_SUBR_GETF) { 9939 /* 9940 * If we have a getf() we need to record that 9941 * in our state. Note that our state can be 9942 * NULL if this is a helper -- but in that 9943 * case, the call to getf() is itself illegal, 9944 * and will be caught (slightly later) when 9945 * the helper is validated. 9946 */ 9947 if (vstate->dtvs_state != NULL) 9948 vstate->dtvs_state->dts_getf++; 9949 } 9950 9951 break; 9952 case DIF_OP_PUSHTR: 9953 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 9954 err += efunc(pc, "invalid ref type %u\n", type); 9955 if (r2 >= nregs) 9956 err += efunc(pc, "invalid register %u\n", r2); 9957 if (rs >= nregs) 9958 err += efunc(pc, "invalid register %u\n", rs); 9959 break; 9960 case DIF_OP_PUSHTV: 9961 if (type != DIF_TYPE_CTF) 9962 err += efunc(pc, "invalid val type %u\n", type); 9963 if (r2 >= nregs) 9964 err += efunc(pc, "invalid register %u\n", r2); 9965 if (rs >= nregs) 9966 err += efunc(pc, "invalid register %u\n", rs); 9967 break; 9968 default: 9969 err += efunc(pc, "invalid opcode %u\n", 9970 DIF_INSTR_OP(instr)); 9971 } 9972 } 9973 9974 if (dp->dtdo_len != 0 && 9975 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 9976 err += efunc(dp->dtdo_len - 1, 9977 "expected 'ret' as last DIF instruction\n"); 9978 } 9979 9980 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) { 9981 /* 9982 * If we're not returning by reference, the size must be either 9983 * 0 or the size of one of the base types. 9984 */ 9985 switch (dp->dtdo_rtype.dtdt_size) { 9986 case 0: 9987 case sizeof (uint8_t): 9988 case sizeof (uint16_t): 9989 case sizeof (uint32_t): 9990 case sizeof (uint64_t): 9991 break; 9992 9993 default: 9994 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 9995 } 9996 } 9997 9998 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 9999 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 10000 dtrace_diftype_t *vt, *et; 10001 uint_t id, ndx; 10002 10003 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 10004 v->dtdv_scope != DIFV_SCOPE_THREAD && 10005 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 10006 err += efunc(i, "unrecognized variable scope %d\n", 10007 v->dtdv_scope); 10008 break; 10009 } 10010 10011 if (v->dtdv_kind != DIFV_KIND_ARRAY && 10012 v->dtdv_kind != DIFV_KIND_SCALAR) { 10013 err += efunc(i, "unrecognized variable type %d\n", 10014 v->dtdv_kind); 10015 break; 10016 } 10017 10018 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 10019 err += efunc(i, "%d exceeds variable id limit\n", id); 10020 break; 10021 } 10022 10023 if (id < DIF_VAR_OTHER_UBASE) 10024 continue; 10025 10026 /* 10027 * For user-defined variables, we need to check that this 10028 * definition is identical to any previous definition that we 10029 * encountered. 10030 */ 10031 ndx = id - DIF_VAR_OTHER_UBASE; 10032 10033 switch (v->dtdv_scope) { 10034 case DIFV_SCOPE_GLOBAL: 10035 if (maxglobal == -1 || ndx > maxglobal) 10036 maxglobal = ndx; 10037 10038 if (ndx < vstate->dtvs_nglobals) { 10039 dtrace_statvar_t *svar; 10040 10041 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 10042 existing = &svar->dtsv_var; 10043 } 10044 10045 break; 10046 10047 case DIFV_SCOPE_THREAD: 10048 if (maxtlocal == -1 || ndx > maxtlocal) 10049 maxtlocal = ndx; 10050 10051 if (ndx < vstate->dtvs_ntlocals) 10052 existing = &vstate->dtvs_tlocals[ndx]; 10053 break; 10054 10055 case DIFV_SCOPE_LOCAL: 10056 if (maxlocal == -1 || ndx > maxlocal) 10057 maxlocal = ndx; 10058 10059 if (ndx < vstate->dtvs_nlocals) { 10060 dtrace_statvar_t *svar; 10061 10062 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 10063 existing = &svar->dtsv_var; 10064 } 10065 10066 break; 10067 } 10068 10069 vt = &v->dtdv_type; 10070 10071 if (vt->dtdt_flags & DIF_TF_BYREF) { 10072 if (vt->dtdt_size == 0) { 10073 err += efunc(i, "zero-sized variable\n"); 10074 break; 10075 } 10076 10077 if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL || 10078 v->dtdv_scope == DIFV_SCOPE_LOCAL) && 10079 vt->dtdt_size > dtrace_statvar_maxsize) { 10080 err += efunc(i, "oversized by-ref static\n"); 10081 break; 10082 } 10083 } 10084 10085 if (existing == NULL || existing->dtdv_id == 0) 10086 continue; 10087 10088 ASSERT(existing->dtdv_id == v->dtdv_id); 10089 ASSERT(existing->dtdv_scope == v->dtdv_scope); 10090 10091 if (existing->dtdv_kind != v->dtdv_kind) 10092 err += efunc(i, "%d changed variable kind\n", id); 10093 10094 et = &existing->dtdv_type; 10095 10096 if (vt->dtdt_flags != et->dtdt_flags) { 10097 err += efunc(i, "%d changed variable type flags\n", id); 10098 break; 10099 } 10100 10101 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 10102 err += efunc(i, "%d changed variable type size\n", id); 10103 break; 10104 } 10105 } 10106 10107 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 10108 dif_instr_t instr = dp->dtdo_buf[pc]; 10109 10110 uint_t v = DIF_INSTR_VAR(instr); 10111 uint_t op = DIF_INSTR_OP(instr); 10112 10113 switch (op) { 10114 case DIF_OP_LDGS: 10115 case DIF_OP_LDGAA: 10116 case DIF_OP_STGS: 10117 case DIF_OP_STGAA: 10118 if (v > DIF_VAR_OTHER_UBASE + maxglobal) 10119 err += efunc(pc, "invalid variable %u\n", v); 10120 break; 10121 case DIF_OP_LDTS: 10122 case DIF_OP_LDTAA: 10123 case DIF_OP_STTS: 10124 case DIF_OP_STTAA: 10125 if (v > DIF_VAR_OTHER_UBASE + maxtlocal) 10126 err += efunc(pc, "invalid variable %u\n", v); 10127 break; 10128 case DIF_OP_LDLS: 10129 case DIF_OP_STLS: 10130 if (v > DIF_VAR_OTHER_UBASE + maxlocal) 10131 err += efunc(pc, "invalid variable %u\n", v); 10132 break; 10133 default: 10134 break; 10135 } 10136 } 10137 10138 return (err); 10139 } 10140 10141 /* 10142 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 10143 * are much more constrained than normal DIFOs. Specifically, they may 10144 * not: 10145 * 10146 * 1. Make calls to subroutines other than copyin(), copyinstr() or 10147 * miscellaneous string routines 10148 * 2. Access DTrace variables other than the args[] array, and the 10149 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 10150 * 3. Have thread-local variables. 10151 * 4. Have dynamic variables. 10152 */ 10153 static int 10154 dtrace_difo_validate_helper(dtrace_difo_t *dp) 10155 { 10156 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 10157 int err = 0; 10158 uint_t pc; 10159 10160 for (pc = 0; pc < dp->dtdo_len; pc++) { 10161 dif_instr_t instr = dp->dtdo_buf[pc]; 10162 10163 uint_t v = DIF_INSTR_VAR(instr); 10164 uint_t subr = DIF_INSTR_SUBR(instr); 10165 uint_t op = DIF_INSTR_OP(instr); 10166 10167 switch (op) { 10168 case DIF_OP_OR: 10169 case DIF_OP_XOR: 10170 case DIF_OP_AND: 10171 case DIF_OP_SLL: 10172 case DIF_OP_SRL: 10173 case DIF_OP_SRA: 10174 case DIF_OP_SUB: 10175 case DIF_OP_ADD: 10176 case DIF_OP_MUL: 10177 case DIF_OP_SDIV: 10178 case DIF_OP_UDIV: 10179 case DIF_OP_SREM: 10180 case DIF_OP_UREM: 10181 case DIF_OP_COPYS: 10182 case DIF_OP_NOT: 10183 case DIF_OP_MOV: 10184 case DIF_OP_RLDSB: 10185 case DIF_OP_RLDSH: 10186 case DIF_OP_RLDSW: 10187 case DIF_OP_RLDUB: 10188 case DIF_OP_RLDUH: 10189 case DIF_OP_RLDUW: 10190 case DIF_OP_RLDX: 10191 case DIF_OP_ULDSB: 10192 case DIF_OP_ULDSH: 10193 case DIF_OP_ULDSW: 10194 case DIF_OP_ULDUB: 10195 case DIF_OP_ULDUH: 10196 case DIF_OP_ULDUW: 10197 case DIF_OP_ULDX: 10198 case DIF_OP_STB: 10199 case DIF_OP_STH: 10200 case DIF_OP_STW: 10201 case DIF_OP_STX: 10202 case DIF_OP_ALLOCS: 10203 case DIF_OP_CMP: 10204 case DIF_OP_SCMP: 10205 case DIF_OP_TST: 10206 case DIF_OP_BA: 10207 case DIF_OP_BE: 10208 case DIF_OP_BNE: 10209 case DIF_OP_BG: 10210 case DIF_OP_BGU: 10211 case DIF_OP_BGE: 10212 case DIF_OP_BGEU: 10213 case DIF_OP_BL: 10214 case DIF_OP_BLU: 10215 case DIF_OP_BLE: 10216 case DIF_OP_BLEU: 10217 case DIF_OP_RET: 10218 case DIF_OP_NOP: 10219 case DIF_OP_POPTS: 10220 case DIF_OP_FLUSHTS: 10221 case DIF_OP_SETX: 10222 case DIF_OP_SETS: 10223 case DIF_OP_LDGA: 10224 case DIF_OP_LDLS: 10225 case DIF_OP_STGS: 10226 case DIF_OP_STLS: 10227 case DIF_OP_PUSHTR: 10228 case DIF_OP_PUSHTV: 10229 break; 10230 10231 case DIF_OP_LDGS: 10232 if (v >= DIF_VAR_OTHER_UBASE) 10233 break; 10234 10235 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 10236 break; 10237 10238 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 10239 v == DIF_VAR_PPID || v == DIF_VAR_TID || 10240 v == DIF_VAR_EXECARGS || 10241 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 10242 v == DIF_VAR_UID || v == DIF_VAR_GID) 10243 break; 10244 10245 err += efunc(pc, "illegal variable %u\n", v); 10246 break; 10247 10248 case DIF_OP_LDTA: 10249 case DIF_OP_LDTS: 10250 case DIF_OP_LDGAA: 10251 case DIF_OP_LDTAA: 10252 err += efunc(pc, "illegal dynamic variable load\n"); 10253 break; 10254 10255 case DIF_OP_STTS: 10256 case DIF_OP_STGAA: 10257 case DIF_OP_STTAA: 10258 err += efunc(pc, "illegal dynamic variable store\n"); 10259 break; 10260 10261 case DIF_OP_CALL: 10262 if (subr == DIF_SUBR_ALLOCA || 10263 subr == DIF_SUBR_BCOPY || 10264 subr == DIF_SUBR_COPYIN || 10265 subr == DIF_SUBR_COPYINTO || 10266 subr == DIF_SUBR_COPYINSTR || 10267 subr == DIF_SUBR_INDEX || 10268 subr == DIF_SUBR_INET_NTOA || 10269 subr == DIF_SUBR_INET_NTOA6 || 10270 subr == DIF_SUBR_INET_NTOP || 10271 subr == DIF_SUBR_JSON || 10272 subr == DIF_SUBR_LLTOSTR || 10273 subr == DIF_SUBR_STRTOLL || 10274 subr == DIF_SUBR_RINDEX || 10275 subr == DIF_SUBR_STRCHR || 10276 subr == DIF_SUBR_STRJOIN || 10277 subr == DIF_SUBR_STRRCHR || 10278 subr == DIF_SUBR_STRSTR || 10279 subr == DIF_SUBR_HTONS || 10280 subr == DIF_SUBR_HTONL || 10281 subr == DIF_SUBR_HTONLL || 10282 subr == DIF_SUBR_NTOHS || 10283 subr == DIF_SUBR_NTOHL || 10284 subr == DIF_SUBR_NTOHLL || 10285 subr == DIF_SUBR_MEMREF) 10286 break; 10287 #ifdef __FreeBSD__ 10288 if (subr == DIF_SUBR_MEMSTR) 10289 break; 10290 #endif 10291 10292 err += efunc(pc, "invalid subr %u\n", subr); 10293 break; 10294 10295 default: 10296 err += efunc(pc, "invalid opcode %u\n", 10297 DIF_INSTR_OP(instr)); 10298 } 10299 } 10300 10301 return (err); 10302 } 10303 10304 /* 10305 * Returns 1 if the expression in the DIF object can be cached on a per-thread 10306 * basis; 0 if not. 10307 */ 10308 static int 10309 dtrace_difo_cacheable(dtrace_difo_t *dp) 10310 { 10311 int i; 10312 10313 if (dp == NULL) 10314 return (0); 10315 10316 for (i = 0; i < dp->dtdo_varlen; i++) { 10317 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10318 10319 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 10320 continue; 10321 10322 switch (v->dtdv_id) { 10323 case DIF_VAR_CURTHREAD: 10324 case DIF_VAR_PID: 10325 case DIF_VAR_TID: 10326 case DIF_VAR_EXECARGS: 10327 case DIF_VAR_EXECNAME: 10328 case DIF_VAR_ZONENAME: 10329 break; 10330 10331 default: 10332 return (0); 10333 } 10334 } 10335 10336 /* 10337 * This DIF object may be cacheable. Now we need to look for any 10338 * array loading instructions, any memory loading instructions, or 10339 * any stores to thread-local variables. 10340 */ 10341 for (i = 0; i < dp->dtdo_len; i++) { 10342 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 10343 10344 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 10345 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 10346 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 10347 op == DIF_OP_LDGA || op == DIF_OP_STTS) 10348 return (0); 10349 } 10350 10351 return (1); 10352 } 10353 10354 static void 10355 dtrace_difo_hold(dtrace_difo_t *dp) 10356 { 10357 int i; 10358 10359 ASSERT(MUTEX_HELD(&dtrace_lock)); 10360 10361 dp->dtdo_refcnt++; 10362 ASSERT(dp->dtdo_refcnt != 0); 10363 10364 /* 10365 * We need to check this DIF object for references to the variable 10366 * DIF_VAR_VTIMESTAMP. 10367 */ 10368 for (i = 0; i < dp->dtdo_varlen; i++) { 10369 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10370 10371 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10372 continue; 10373 10374 if (dtrace_vtime_references++ == 0) 10375 dtrace_vtime_enable(); 10376 } 10377 } 10378 10379 /* 10380 * This routine calculates the dynamic variable chunksize for a given DIF 10381 * object. The calculation is not fool-proof, and can probably be tricked by 10382 * malicious DIF -- but it works for all compiler-generated DIF. Because this 10383 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 10384 * if a dynamic variable size exceeds the chunksize. 10385 */ 10386 static void 10387 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10388 { 10389 uint64_t sval = 0; 10390 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 10391 const dif_instr_t *text = dp->dtdo_buf; 10392 uint_t pc, srd = 0; 10393 uint_t ttop = 0; 10394 size_t size, ksize; 10395 uint_t id, i; 10396 10397 for (pc = 0; pc < dp->dtdo_len; pc++) { 10398 dif_instr_t instr = text[pc]; 10399 uint_t op = DIF_INSTR_OP(instr); 10400 uint_t rd = DIF_INSTR_RD(instr); 10401 uint_t r1 = DIF_INSTR_R1(instr); 10402 uint_t nkeys = 0; 10403 uchar_t scope = 0; 10404 10405 dtrace_key_t *key = tupregs; 10406 10407 switch (op) { 10408 case DIF_OP_SETX: 10409 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 10410 srd = rd; 10411 continue; 10412 10413 case DIF_OP_STTS: 10414 key = &tupregs[DIF_DTR_NREGS]; 10415 key[0].dttk_size = 0; 10416 key[1].dttk_size = 0; 10417 nkeys = 2; 10418 scope = DIFV_SCOPE_THREAD; 10419 break; 10420 10421 case DIF_OP_STGAA: 10422 case DIF_OP_STTAA: 10423 nkeys = ttop; 10424 10425 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 10426 key[nkeys++].dttk_size = 0; 10427 10428 key[nkeys++].dttk_size = 0; 10429 10430 if (op == DIF_OP_STTAA) { 10431 scope = DIFV_SCOPE_THREAD; 10432 } else { 10433 scope = DIFV_SCOPE_GLOBAL; 10434 } 10435 10436 break; 10437 10438 case DIF_OP_PUSHTR: 10439 if (ttop == DIF_DTR_NREGS) 10440 return; 10441 10442 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 10443 /* 10444 * If the register for the size of the "pushtr" 10445 * is %r0 (or the value is 0) and the type is 10446 * a string, we'll use the system-wide default 10447 * string size. 10448 */ 10449 tupregs[ttop++].dttk_size = 10450 dtrace_strsize_default; 10451 } else { 10452 if (srd == 0) 10453 return; 10454 10455 if (sval > LONG_MAX) 10456 return; 10457 10458 tupregs[ttop++].dttk_size = sval; 10459 } 10460 10461 break; 10462 10463 case DIF_OP_PUSHTV: 10464 if (ttop == DIF_DTR_NREGS) 10465 return; 10466 10467 tupregs[ttop++].dttk_size = 0; 10468 break; 10469 10470 case DIF_OP_FLUSHTS: 10471 ttop = 0; 10472 break; 10473 10474 case DIF_OP_POPTS: 10475 if (ttop != 0) 10476 ttop--; 10477 break; 10478 } 10479 10480 sval = 0; 10481 srd = 0; 10482 10483 if (nkeys == 0) 10484 continue; 10485 10486 /* 10487 * We have a dynamic variable allocation; calculate its size. 10488 */ 10489 for (ksize = 0, i = 0; i < nkeys; i++) 10490 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 10491 10492 size = sizeof (dtrace_dynvar_t); 10493 size += sizeof (dtrace_key_t) * (nkeys - 1); 10494 size += ksize; 10495 10496 /* 10497 * Now we need to determine the size of the stored data. 10498 */ 10499 id = DIF_INSTR_VAR(instr); 10500 10501 for (i = 0; i < dp->dtdo_varlen; i++) { 10502 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10503 10504 if (v->dtdv_id == id && v->dtdv_scope == scope) { 10505 size += v->dtdv_type.dtdt_size; 10506 break; 10507 } 10508 } 10509 10510 if (i == dp->dtdo_varlen) 10511 return; 10512 10513 /* 10514 * We have the size. If this is larger than the chunk size 10515 * for our dynamic variable state, reset the chunk size. 10516 */ 10517 size = P2ROUNDUP(size, sizeof (uint64_t)); 10518 10519 /* 10520 * Before setting the chunk size, check that we're not going 10521 * to set it to a negative value... 10522 */ 10523 if (size > LONG_MAX) 10524 return; 10525 10526 /* 10527 * ...and make certain that we didn't badly overflow. 10528 */ 10529 if (size < ksize || size < sizeof (dtrace_dynvar_t)) 10530 return; 10531 10532 if (size > vstate->dtvs_dynvars.dtds_chunksize) 10533 vstate->dtvs_dynvars.dtds_chunksize = size; 10534 } 10535 } 10536 10537 static void 10538 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10539 { 10540 int i, oldsvars, osz, nsz, otlocals, ntlocals; 10541 uint_t id; 10542 10543 ASSERT(MUTEX_HELD(&dtrace_lock)); 10544 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 10545 10546 for (i = 0; i < dp->dtdo_varlen; i++) { 10547 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10548 dtrace_statvar_t *svar, ***svarp = NULL; 10549 size_t dsize = 0; 10550 uint8_t scope = v->dtdv_scope; 10551 int *np = NULL; 10552 10553 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10554 continue; 10555 10556 id -= DIF_VAR_OTHER_UBASE; 10557 10558 switch (scope) { 10559 case DIFV_SCOPE_THREAD: 10560 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 10561 dtrace_difv_t *tlocals; 10562 10563 if ((ntlocals = (otlocals << 1)) == 0) 10564 ntlocals = 1; 10565 10566 osz = otlocals * sizeof (dtrace_difv_t); 10567 nsz = ntlocals * sizeof (dtrace_difv_t); 10568 10569 tlocals = kmem_zalloc(nsz, KM_SLEEP); 10570 10571 if (osz != 0) { 10572 bcopy(vstate->dtvs_tlocals, 10573 tlocals, osz); 10574 kmem_free(vstate->dtvs_tlocals, osz); 10575 } 10576 10577 vstate->dtvs_tlocals = tlocals; 10578 vstate->dtvs_ntlocals = ntlocals; 10579 } 10580 10581 vstate->dtvs_tlocals[id] = *v; 10582 continue; 10583 10584 case DIFV_SCOPE_LOCAL: 10585 np = &vstate->dtvs_nlocals; 10586 svarp = &vstate->dtvs_locals; 10587 10588 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10589 dsize = NCPU * (v->dtdv_type.dtdt_size + 10590 sizeof (uint64_t)); 10591 else 10592 dsize = NCPU * sizeof (uint64_t); 10593 10594 break; 10595 10596 case DIFV_SCOPE_GLOBAL: 10597 np = &vstate->dtvs_nglobals; 10598 svarp = &vstate->dtvs_globals; 10599 10600 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10601 dsize = v->dtdv_type.dtdt_size + 10602 sizeof (uint64_t); 10603 10604 break; 10605 10606 default: 10607 ASSERT(0); 10608 } 10609 10610 while (id >= (oldsvars = *np)) { 10611 dtrace_statvar_t **statics; 10612 int newsvars, oldsize, newsize; 10613 10614 if ((newsvars = (oldsvars << 1)) == 0) 10615 newsvars = 1; 10616 10617 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 10618 newsize = newsvars * sizeof (dtrace_statvar_t *); 10619 10620 statics = kmem_zalloc(newsize, KM_SLEEP); 10621 10622 if (oldsize != 0) { 10623 bcopy(*svarp, statics, oldsize); 10624 kmem_free(*svarp, oldsize); 10625 } 10626 10627 *svarp = statics; 10628 *np = newsvars; 10629 } 10630 10631 if ((svar = (*svarp)[id]) == NULL) { 10632 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 10633 svar->dtsv_var = *v; 10634 10635 if ((svar->dtsv_size = dsize) != 0) { 10636 svar->dtsv_data = (uint64_t)(uintptr_t) 10637 kmem_zalloc(dsize, KM_SLEEP); 10638 } 10639 10640 (*svarp)[id] = svar; 10641 } 10642 10643 svar->dtsv_refcnt++; 10644 } 10645 10646 dtrace_difo_chunksize(dp, vstate); 10647 dtrace_difo_hold(dp); 10648 } 10649 10650 static dtrace_difo_t * 10651 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10652 { 10653 dtrace_difo_t *new; 10654 size_t sz; 10655 10656 ASSERT(dp->dtdo_buf != NULL); 10657 ASSERT(dp->dtdo_refcnt != 0); 10658 10659 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10660 10661 ASSERT(dp->dtdo_buf != NULL); 10662 sz = dp->dtdo_len * sizeof (dif_instr_t); 10663 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 10664 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 10665 new->dtdo_len = dp->dtdo_len; 10666 10667 if (dp->dtdo_strtab != NULL) { 10668 ASSERT(dp->dtdo_strlen != 0); 10669 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 10670 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 10671 new->dtdo_strlen = dp->dtdo_strlen; 10672 } 10673 10674 if (dp->dtdo_inttab != NULL) { 10675 ASSERT(dp->dtdo_intlen != 0); 10676 sz = dp->dtdo_intlen * sizeof (uint64_t); 10677 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 10678 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 10679 new->dtdo_intlen = dp->dtdo_intlen; 10680 } 10681 10682 if (dp->dtdo_vartab != NULL) { 10683 ASSERT(dp->dtdo_varlen != 0); 10684 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 10685 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 10686 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 10687 new->dtdo_varlen = dp->dtdo_varlen; 10688 } 10689 10690 dtrace_difo_init(new, vstate); 10691 return (new); 10692 } 10693 10694 static void 10695 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10696 { 10697 int i; 10698 10699 ASSERT(dp->dtdo_refcnt == 0); 10700 10701 for (i = 0; i < dp->dtdo_varlen; i++) { 10702 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10703 dtrace_statvar_t *svar, **svarp = NULL; 10704 uint_t id; 10705 uint8_t scope = v->dtdv_scope; 10706 int *np = NULL; 10707 10708 switch (scope) { 10709 case DIFV_SCOPE_THREAD: 10710 continue; 10711 10712 case DIFV_SCOPE_LOCAL: 10713 np = &vstate->dtvs_nlocals; 10714 svarp = vstate->dtvs_locals; 10715 break; 10716 10717 case DIFV_SCOPE_GLOBAL: 10718 np = &vstate->dtvs_nglobals; 10719 svarp = vstate->dtvs_globals; 10720 break; 10721 10722 default: 10723 ASSERT(0); 10724 } 10725 10726 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10727 continue; 10728 10729 id -= DIF_VAR_OTHER_UBASE; 10730 ASSERT(id < *np); 10731 10732 svar = svarp[id]; 10733 ASSERT(svar != NULL); 10734 ASSERT(svar->dtsv_refcnt > 0); 10735 10736 if (--svar->dtsv_refcnt > 0) 10737 continue; 10738 10739 if (svar->dtsv_size != 0) { 10740 ASSERT(svar->dtsv_data != 0); 10741 kmem_free((void *)(uintptr_t)svar->dtsv_data, 10742 svar->dtsv_size); 10743 } 10744 10745 kmem_free(svar, sizeof (dtrace_statvar_t)); 10746 svarp[id] = NULL; 10747 } 10748 10749 if (dp->dtdo_buf != NULL) 10750 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10751 if (dp->dtdo_inttab != NULL) 10752 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10753 if (dp->dtdo_strtab != NULL) 10754 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10755 if (dp->dtdo_vartab != NULL) 10756 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10757 10758 kmem_free(dp, sizeof (dtrace_difo_t)); 10759 } 10760 10761 static void 10762 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10763 { 10764 int i; 10765 10766 ASSERT(MUTEX_HELD(&dtrace_lock)); 10767 ASSERT(dp->dtdo_refcnt != 0); 10768 10769 for (i = 0; i < dp->dtdo_varlen; i++) { 10770 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10771 10772 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10773 continue; 10774 10775 ASSERT(dtrace_vtime_references > 0); 10776 if (--dtrace_vtime_references == 0) 10777 dtrace_vtime_disable(); 10778 } 10779 10780 if (--dp->dtdo_refcnt == 0) 10781 dtrace_difo_destroy(dp, vstate); 10782 } 10783 10784 /* 10785 * DTrace Format Functions 10786 */ 10787 static uint16_t 10788 dtrace_format_add(dtrace_state_t *state, char *str) 10789 { 10790 char *fmt, **new; 10791 uint16_t ndx, len = strlen(str) + 1; 10792 10793 fmt = kmem_zalloc(len, KM_SLEEP); 10794 bcopy(str, fmt, len); 10795 10796 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 10797 if (state->dts_formats[ndx] == NULL) { 10798 state->dts_formats[ndx] = fmt; 10799 return (ndx + 1); 10800 } 10801 } 10802 10803 if (state->dts_nformats == USHRT_MAX) { 10804 /* 10805 * This is only likely if a denial-of-service attack is being 10806 * attempted. As such, it's okay to fail silently here. 10807 */ 10808 kmem_free(fmt, len); 10809 return (0); 10810 } 10811 10812 /* 10813 * For simplicity, we always resize the formats array to be exactly the 10814 * number of formats. 10815 */ 10816 ndx = state->dts_nformats++; 10817 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 10818 10819 if (state->dts_formats != NULL) { 10820 ASSERT(ndx != 0); 10821 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 10822 kmem_free(state->dts_formats, ndx * sizeof (char *)); 10823 } 10824 10825 state->dts_formats = new; 10826 state->dts_formats[ndx] = fmt; 10827 10828 return (ndx + 1); 10829 } 10830 10831 static void 10832 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 10833 { 10834 char *fmt; 10835 10836 ASSERT(state->dts_formats != NULL); 10837 ASSERT(format <= state->dts_nformats); 10838 ASSERT(state->dts_formats[format - 1] != NULL); 10839 10840 fmt = state->dts_formats[format - 1]; 10841 kmem_free(fmt, strlen(fmt) + 1); 10842 state->dts_formats[format - 1] = NULL; 10843 } 10844 10845 static void 10846 dtrace_format_destroy(dtrace_state_t *state) 10847 { 10848 int i; 10849 10850 if (state->dts_nformats == 0) { 10851 ASSERT(state->dts_formats == NULL); 10852 return; 10853 } 10854 10855 ASSERT(state->dts_formats != NULL); 10856 10857 for (i = 0; i < state->dts_nformats; i++) { 10858 char *fmt = state->dts_formats[i]; 10859 10860 if (fmt == NULL) 10861 continue; 10862 10863 kmem_free(fmt, strlen(fmt) + 1); 10864 } 10865 10866 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 10867 state->dts_nformats = 0; 10868 state->dts_formats = NULL; 10869 } 10870 10871 /* 10872 * DTrace Predicate Functions 10873 */ 10874 static dtrace_predicate_t * 10875 dtrace_predicate_create(dtrace_difo_t *dp) 10876 { 10877 dtrace_predicate_t *pred; 10878 10879 ASSERT(MUTEX_HELD(&dtrace_lock)); 10880 ASSERT(dp->dtdo_refcnt != 0); 10881 10882 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 10883 pred->dtp_difo = dp; 10884 pred->dtp_refcnt = 1; 10885 10886 if (!dtrace_difo_cacheable(dp)) 10887 return (pred); 10888 10889 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 10890 /* 10891 * This is only theoretically possible -- we have had 2^32 10892 * cacheable predicates on this machine. We cannot allow any 10893 * more predicates to become cacheable: as unlikely as it is, 10894 * there may be a thread caching a (now stale) predicate cache 10895 * ID. (N.B.: the temptation is being successfully resisted to 10896 * have this cmn_err() "Holy shit -- we executed this code!") 10897 */ 10898 return (pred); 10899 } 10900 10901 pred->dtp_cacheid = dtrace_predcache_id++; 10902 10903 return (pred); 10904 } 10905 10906 static void 10907 dtrace_predicate_hold(dtrace_predicate_t *pred) 10908 { 10909 ASSERT(MUTEX_HELD(&dtrace_lock)); 10910 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 10911 ASSERT(pred->dtp_refcnt > 0); 10912 10913 pred->dtp_refcnt++; 10914 } 10915 10916 static void 10917 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 10918 { 10919 dtrace_difo_t *dp = pred->dtp_difo; 10920 10921 ASSERT(MUTEX_HELD(&dtrace_lock)); 10922 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 10923 ASSERT(pred->dtp_refcnt > 0); 10924 10925 if (--pred->dtp_refcnt == 0) { 10926 dtrace_difo_release(pred->dtp_difo, vstate); 10927 kmem_free(pred, sizeof (dtrace_predicate_t)); 10928 } 10929 } 10930 10931 /* 10932 * DTrace Action Description Functions 10933 */ 10934 static dtrace_actdesc_t * 10935 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 10936 uint64_t uarg, uint64_t arg) 10937 { 10938 dtrace_actdesc_t *act; 10939 10940 #ifdef illumos 10941 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 10942 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 10943 #endif 10944 10945 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 10946 act->dtad_kind = kind; 10947 act->dtad_ntuple = ntuple; 10948 act->dtad_uarg = uarg; 10949 act->dtad_arg = arg; 10950 act->dtad_refcnt = 1; 10951 10952 return (act); 10953 } 10954 10955 static void 10956 dtrace_actdesc_hold(dtrace_actdesc_t *act) 10957 { 10958 ASSERT(act->dtad_refcnt >= 1); 10959 act->dtad_refcnt++; 10960 } 10961 10962 static void 10963 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 10964 { 10965 dtrace_actkind_t kind = act->dtad_kind; 10966 dtrace_difo_t *dp; 10967 10968 ASSERT(act->dtad_refcnt >= 1); 10969 10970 if (--act->dtad_refcnt != 0) 10971 return; 10972 10973 if ((dp = act->dtad_difo) != NULL) 10974 dtrace_difo_release(dp, vstate); 10975 10976 if (DTRACEACT_ISPRINTFLIKE(kind)) { 10977 char *str = (char *)(uintptr_t)act->dtad_arg; 10978 10979 #ifdef illumos 10980 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 10981 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 10982 #endif 10983 10984 if (str != NULL) 10985 kmem_free(str, strlen(str) + 1); 10986 } 10987 10988 kmem_free(act, sizeof (dtrace_actdesc_t)); 10989 } 10990 10991 /* 10992 * DTrace ECB Functions 10993 */ 10994 static dtrace_ecb_t * 10995 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 10996 { 10997 dtrace_ecb_t *ecb; 10998 dtrace_epid_t epid; 10999 11000 ASSERT(MUTEX_HELD(&dtrace_lock)); 11001 11002 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 11003 ecb->dte_predicate = NULL; 11004 ecb->dte_probe = probe; 11005 11006 /* 11007 * The default size is the size of the default action: recording 11008 * the header. 11009 */ 11010 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 11011 ecb->dte_alignment = sizeof (dtrace_epid_t); 11012 11013 epid = state->dts_epid++; 11014 11015 if (epid - 1 >= state->dts_necbs) { 11016 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 11017 int necbs = state->dts_necbs << 1; 11018 11019 ASSERT(epid == state->dts_necbs + 1); 11020 11021 if (necbs == 0) { 11022 ASSERT(oecbs == NULL); 11023 necbs = 1; 11024 } 11025 11026 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 11027 11028 if (oecbs != NULL) 11029 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 11030 11031 dtrace_membar_producer(); 11032 state->dts_ecbs = ecbs; 11033 11034 if (oecbs != NULL) { 11035 /* 11036 * If this state is active, we must dtrace_sync() 11037 * before we can free the old dts_ecbs array: we're 11038 * coming in hot, and there may be active ring 11039 * buffer processing (which indexes into the dts_ecbs 11040 * array) on another CPU. 11041 */ 11042 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11043 dtrace_sync(); 11044 11045 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 11046 } 11047 11048 dtrace_membar_producer(); 11049 state->dts_necbs = necbs; 11050 } 11051 11052 ecb->dte_state = state; 11053 11054 ASSERT(state->dts_ecbs[epid - 1] == NULL); 11055 dtrace_membar_producer(); 11056 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 11057 11058 return (ecb); 11059 } 11060 11061 static void 11062 dtrace_ecb_enable(dtrace_ecb_t *ecb) 11063 { 11064 dtrace_probe_t *probe = ecb->dte_probe; 11065 11066 ASSERT(MUTEX_HELD(&cpu_lock)); 11067 ASSERT(MUTEX_HELD(&dtrace_lock)); 11068 ASSERT(ecb->dte_next == NULL); 11069 11070 if (probe == NULL) { 11071 /* 11072 * This is the NULL probe -- there's nothing to do. 11073 */ 11074 return; 11075 } 11076 11077 if (probe->dtpr_ecb == NULL) { 11078 dtrace_provider_t *prov = probe->dtpr_provider; 11079 11080 /* 11081 * We're the first ECB on this probe. 11082 */ 11083 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 11084 11085 if (ecb->dte_predicate != NULL) 11086 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 11087 11088 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 11089 probe->dtpr_id, probe->dtpr_arg); 11090 } else { 11091 /* 11092 * This probe is already active. Swing the last pointer to 11093 * point to the new ECB, and issue a dtrace_sync() to assure 11094 * that all CPUs have seen the change. 11095 */ 11096 ASSERT(probe->dtpr_ecb_last != NULL); 11097 probe->dtpr_ecb_last->dte_next = ecb; 11098 probe->dtpr_ecb_last = ecb; 11099 probe->dtpr_predcache = 0; 11100 11101 dtrace_sync(); 11102 } 11103 } 11104 11105 static int 11106 dtrace_ecb_resize(dtrace_ecb_t *ecb) 11107 { 11108 dtrace_action_t *act; 11109 uint32_t curneeded = UINT32_MAX; 11110 uint32_t aggbase = UINT32_MAX; 11111 11112 /* 11113 * If we record anything, we always record the dtrace_rechdr_t. (And 11114 * we always record it first.) 11115 */ 11116 ecb->dte_size = sizeof (dtrace_rechdr_t); 11117 ecb->dte_alignment = sizeof (dtrace_epid_t); 11118 11119 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11120 dtrace_recdesc_t *rec = &act->dta_rec; 11121 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 11122 11123 ecb->dte_alignment = MAX(ecb->dte_alignment, 11124 rec->dtrd_alignment); 11125 11126 if (DTRACEACT_ISAGG(act->dta_kind)) { 11127 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11128 11129 ASSERT(rec->dtrd_size != 0); 11130 ASSERT(agg->dtag_first != NULL); 11131 ASSERT(act->dta_prev->dta_intuple); 11132 ASSERT(aggbase != UINT32_MAX); 11133 ASSERT(curneeded != UINT32_MAX); 11134 11135 agg->dtag_base = aggbase; 11136 11137 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11138 rec->dtrd_offset = curneeded; 11139 if (curneeded + rec->dtrd_size < curneeded) 11140 return (EINVAL); 11141 curneeded += rec->dtrd_size; 11142 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 11143 11144 aggbase = UINT32_MAX; 11145 curneeded = UINT32_MAX; 11146 } else if (act->dta_intuple) { 11147 if (curneeded == UINT32_MAX) { 11148 /* 11149 * This is the first record in a tuple. Align 11150 * curneeded to be at offset 4 in an 8-byte 11151 * aligned block. 11152 */ 11153 ASSERT(act->dta_prev == NULL || 11154 !act->dta_prev->dta_intuple); 11155 ASSERT3U(aggbase, ==, UINT32_MAX); 11156 curneeded = P2PHASEUP(ecb->dte_size, 11157 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 11158 11159 aggbase = curneeded - sizeof (dtrace_aggid_t); 11160 ASSERT(IS_P2ALIGNED(aggbase, 11161 sizeof (uint64_t))); 11162 } 11163 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11164 rec->dtrd_offset = curneeded; 11165 if (curneeded + rec->dtrd_size < curneeded) 11166 return (EINVAL); 11167 curneeded += rec->dtrd_size; 11168 } else { 11169 /* tuples must be followed by an aggregation */ 11170 ASSERT(act->dta_prev == NULL || 11171 !act->dta_prev->dta_intuple); 11172 11173 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 11174 rec->dtrd_alignment); 11175 rec->dtrd_offset = ecb->dte_size; 11176 if (ecb->dte_size + rec->dtrd_size < ecb->dte_size) 11177 return (EINVAL); 11178 ecb->dte_size += rec->dtrd_size; 11179 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 11180 } 11181 } 11182 11183 if ((act = ecb->dte_action) != NULL && 11184 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 11185 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 11186 /* 11187 * If the size is still sizeof (dtrace_rechdr_t), then all 11188 * actions store no data; set the size to 0. 11189 */ 11190 ecb->dte_size = 0; 11191 } 11192 11193 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 11194 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 11195 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 11196 ecb->dte_needed); 11197 return (0); 11198 } 11199 11200 static dtrace_action_t * 11201 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11202 { 11203 dtrace_aggregation_t *agg; 11204 size_t size = sizeof (uint64_t); 11205 int ntuple = desc->dtad_ntuple; 11206 dtrace_action_t *act; 11207 dtrace_recdesc_t *frec; 11208 dtrace_aggid_t aggid; 11209 dtrace_state_t *state = ecb->dte_state; 11210 11211 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 11212 agg->dtag_ecb = ecb; 11213 11214 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 11215 11216 switch (desc->dtad_kind) { 11217 case DTRACEAGG_MIN: 11218 agg->dtag_initial = INT64_MAX; 11219 agg->dtag_aggregate = dtrace_aggregate_min; 11220 break; 11221 11222 case DTRACEAGG_MAX: 11223 agg->dtag_initial = INT64_MIN; 11224 agg->dtag_aggregate = dtrace_aggregate_max; 11225 break; 11226 11227 case DTRACEAGG_COUNT: 11228 agg->dtag_aggregate = dtrace_aggregate_count; 11229 break; 11230 11231 case DTRACEAGG_QUANTIZE: 11232 agg->dtag_aggregate = dtrace_aggregate_quantize; 11233 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 11234 sizeof (uint64_t); 11235 break; 11236 11237 case DTRACEAGG_LQUANTIZE: { 11238 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 11239 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 11240 11241 agg->dtag_initial = desc->dtad_arg; 11242 agg->dtag_aggregate = dtrace_aggregate_lquantize; 11243 11244 if (step == 0 || levels == 0) 11245 goto err; 11246 11247 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 11248 break; 11249 } 11250 11251 case DTRACEAGG_LLQUANTIZE: { 11252 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 11253 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 11254 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 11255 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 11256 int64_t v; 11257 11258 agg->dtag_initial = desc->dtad_arg; 11259 agg->dtag_aggregate = dtrace_aggregate_llquantize; 11260 11261 if (factor < 2 || low >= high || nsteps < factor) 11262 goto err; 11263 11264 /* 11265 * Now check that the number of steps evenly divides a power 11266 * of the factor. (This assures both integer bucket size and 11267 * linearity within each magnitude.) 11268 */ 11269 for (v = factor; v < nsteps; v *= factor) 11270 continue; 11271 11272 if ((v % nsteps) || (nsteps % factor)) 11273 goto err; 11274 11275 size = (dtrace_aggregate_llquantize_bucket(factor, 11276 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 11277 break; 11278 } 11279 11280 case DTRACEAGG_AVG: 11281 agg->dtag_aggregate = dtrace_aggregate_avg; 11282 size = sizeof (uint64_t) * 2; 11283 break; 11284 11285 case DTRACEAGG_STDDEV: 11286 agg->dtag_aggregate = dtrace_aggregate_stddev; 11287 size = sizeof (uint64_t) * 4; 11288 break; 11289 11290 case DTRACEAGG_SUM: 11291 agg->dtag_aggregate = dtrace_aggregate_sum; 11292 break; 11293 11294 default: 11295 goto err; 11296 } 11297 11298 agg->dtag_action.dta_rec.dtrd_size = size; 11299 11300 if (ntuple == 0) 11301 goto err; 11302 11303 /* 11304 * We must make sure that we have enough actions for the n-tuple. 11305 */ 11306 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 11307 if (DTRACEACT_ISAGG(act->dta_kind)) 11308 break; 11309 11310 if (--ntuple == 0) { 11311 /* 11312 * This is the action with which our n-tuple begins. 11313 */ 11314 agg->dtag_first = act; 11315 goto success; 11316 } 11317 } 11318 11319 /* 11320 * This n-tuple is short by ntuple elements. Return failure. 11321 */ 11322 ASSERT(ntuple != 0); 11323 err: 11324 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11325 return (NULL); 11326 11327 success: 11328 /* 11329 * If the last action in the tuple has a size of zero, it's actually 11330 * an expression argument for the aggregating action. 11331 */ 11332 ASSERT(ecb->dte_action_last != NULL); 11333 act = ecb->dte_action_last; 11334 11335 if (act->dta_kind == DTRACEACT_DIFEXPR) { 11336 ASSERT(act->dta_difo != NULL); 11337 11338 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 11339 agg->dtag_hasarg = 1; 11340 } 11341 11342 /* 11343 * We need to allocate an id for this aggregation. 11344 */ 11345 #ifdef illumos 11346 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 11347 VM_BESTFIT | VM_SLEEP); 11348 #else 11349 aggid = alloc_unr(state->dts_aggid_arena); 11350 #endif 11351 11352 if (aggid - 1 >= state->dts_naggregations) { 11353 dtrace_aggregation_t **oaggs = state->dts_aggregations; 11354 dtrace_aggregation_t **aggs; 11355 int naggs = state->dts_naggregations << 1; 11356 int onaggs = state->dts_naggregations; 11357 11358 ASSERT(aggid == state->dts_naggregations + 1); 11359 11360 if (naggs == 0) { 11361 ASSERT(oaggs == NULL); 11362 naggs = 1; 11363 } 11364 11365 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 11366 11367 if (oaggs != NULL) { 11368 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 11369 kmem_free(oaggs, onaggs * sizeof (*aggs)); 11370 } 11371 11372 state->dts_aggregations = aggs; 11373 state->dts_naggregations = naggs; 11374 } 11375 11376 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 11377 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 11378 11379 frec = &agg->dtag_first->dta_rec; 11380 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 11381 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 11382 11383 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 11384 ASSERT(!act->dta_intuple); 11385 act->dta_intuple = 1; 11386 } 11387 11388 return (&agg->dtag_action); 11389 } 11390 11391 static void 11392 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 11393 { 11394 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11395 dtrace_state_t *state = ecb->dte_state; 11396 dtrace_aggid_t aggid = agg->dtag_id; 11397 11398 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 11399 #ifdef illumos 11400 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 11401 #else 11402 free_unr(state->dts_aggid_arena, aggid); 11403 #endif 11404 11405 ASSERT(state->dts_aggregations[aggid - 1] == agg); 11406 state->dts_aggregations[aggid - 1] = NULL; 11407 11408 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11409 } 11410 11411 static int 11412 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11413 { 11414 dtrace_action_t *action, *last; 11415 dtrace_difo_t *dp = desc->dtad_difo; 11416 uint32_t size = 0, align = sizeof (uint8_t), mask; 11417 uint16_t format = 0; 11418 dtrace_recdesc_t *rec; 11419 dtrace_state_t *state = ecb->dte_state; 11420 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 11421 uint64_t arg = desc->dtad_arg; 11422 11423 ASSERT(MUTEX_HELD(&dtrace_lock)); 11424 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 11425 11426 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 11427 /* 11428 * If this is an aggregating action, there must be neither 11429 * a speculate nor a commit on the action chain. 11430 */ 11431 dtrace_action_t *act; 11432 11433 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11434 if (act->dta_kind == DTRACEACT_COMMIT) 11435 return (EINVAL); 11436 11437 if (act->dta_kind == DTRACEACT_SPECULATE) 11438 return (EINVAL); 11439 } 11440 11441 action = dtrace_ecb_aggregation_create(ecb, desc); 11442 11443 if (action == NULL) 11444 return (EINVAL); 11445 } else { 11446 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 11447 (desc->dtad_kind == DTRACEACT_DIFEXPR && 11448 dp != NULL && dp->dtdo_destructive)) { 11449 state->dts_destructive = 1; 11450 } 11451 11452 switch (desc->dtad_kind) { 11453 case DTRACEACT_PRINTF: 11454 case DTRACEACT_PRINTA: 11455 case DTRACEACT_SYSTEM: 11456 case DTRACEACT_FREOPEN: 11457 case DTRACEACT_DIFEXPR: 11458 /* 11459 * We know that our arg is a string -- turn it into a 11460 * format. 11461 */ 11462 if (arg == 0) { 11463 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 11464 desc->dtad_kind == DTRACEACT_DIFEXPR); 11465 format = 0; 11466 } else { 11467 ASSERT(arg != 0); 11468 #ifdef illumos 11469 ASSERT(arg > KERNELBASE); 11470 #endif 11471 format = dtrace_format_add(state, 11472 (char *)(uintptr_t)arg); 11473 } 11474 11475 /*FALLTHROUGH*/ 11476 case DTRACEACT_LIBACT: 11477 case DTRACEACT_TRACEMEM: 11478 case DTRACEACT_TRACEMEM_DYNSIZE: 11479 if (dp == NULL) 11480 return (EINVAL); 11481 11482 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 11483 break; 11484 11485 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 11486 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11487 return (EINVAL); 11488 11489 size = opt[DTRACEOPT_STRSIZE]; 11490 } 11491 11492 break; 11493 11494 case DTRACEACT_STACK: 11495 if ((nframes = arg) == 0) { 11496 nframes = opt[DTRACEOPT_STACKFRAMES]; 11497 ASSERT(nframes > 0); 11498 arg = nframes; 11499 } 11500 11501 size = nframes * sizeof (pc_t); 11502 break; 11503 11504 case DTRACEACT_JSTACK: 11505 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 11506 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 11507 11508 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 11509 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 11510 11511 arg = DTRACE_USTACK_ARG(nframes, strsize); 11512 11513 /*FALLTHROUGH*/ 11514 case DTRACEACT_USTACK: 11515 if (desc->dtad_kind != DTRACEACT_JSTACK && 11516 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 11517 strsize = DTRACE_USTACK_STRSIZE(arg); 11518 nframes = opt[DTRACEOPT_USTACKFRAMES]; 11519 ASSERT(nframes > 0); 11520 arg = DTRACE_USTACK_ARG(nframes, strsize); 11521 } 11522 11523 /* 11524 * Save a slot for the pid. 11525 */ 11526 size = (nframes + 1) * sizeof (uint64_t); 11527 size += DTRACE_USTACK_STRSIZE(arg); 11528 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 11529 11530 break; 11531 11532 case DTRACEACT_SYM: 11533 case DTRACEACT_MOD: 11534 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 11535 sizeof (uint64_t)) || 11536 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11537 return (EINVAL); 11538 break; 11539 11540 case DTRACEACT_USYM: 11541 case DTRACEACT_UMOD: 11542 case DTRACEACT_UADDR: 11543 if (dp == NULL || 11544 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 11545 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11546 return (EINVAL); 11547 11548 /* 11549 * We have a slot for the pid, plus a slot for the 11550 * argument. To keep things simple (aligned with 11551 * bitness-neutral sizing), we store each as a 64-bit 11552 * quantity. 11553 */ 11554 size = 2 * sizeof (uint64_t); 11555 break; 11556 11557 case DTRACEACT_STOP: 11558 case DTRACEACT_BREAKPOINT: 11559 case DTRACEACT_PANIC: 11560 break; 11561 11562 case DTRACEACT_CHILL: 11563 case DTRACEACT_DISCARD: 11564 case DTRACEACT_RAISE: 11565 if (dp == NULL) 11566 return (EINVAL); 11567 break; 11568 11569 case DTRACEACT_EXIT: 11570 if (dp == NULL || 11571 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 11572 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11573 return (EINVAL); 11574 break; 11575 11576 case DTRACEACT_SPECULATE: 11577 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 11578 return (EINVAL); 11579 11580 if (dp == NULL) 11581 return (EINVAL); 11582 11583 state->dts_speculates = 1; 11584 break; 11585 11586 case DTRACEACT_PRINTM: 11587 size = dp->dtdo_rtype.dtdt_size; 11588 break; 11589 11590 case DTRACEACT_COMMIT: { 11591 dtrace_action_t *act = ecb->dte_action; 11592 11593 for (; act != NULL; act = act->dta_next) { 11594 if (act->dta_kind == DTRACEACT_COMMIT) 11595 return (EINVAL); 11596 } 11597 11598 if (dp == NULL) 11599 return (EINVAL); 11600 break; 11601 } 11602 11603 default: 11604 return (EINVAL); 11605 } 11606 11607 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 11608 /* 11609 * If this is a data-storing action or a speculate, 11610 * we must be sure that there isn't a commit on the 11611 * action chain. 11612 */ 11613 dtrace_action_t *act = ecb->dte_action; 11614 11615 for (; act != NULL; act = act->dta_next) { 11616 if (act->dta_kind == DTRACEACT_COMMIT) 11617 return (EINVAL); 11618 } 11619 } 11620 11621 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 11622 action->dta_rec.dtrd_size = size; 11623 } 11624 11625 action->dta_refcnt = 1; 11626 rec = &action->dta_rec; 11627 size = rec->dtrd_size; 11628 11629 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 11630 if (!(size & mask)) { 11631 align = mask + 1; 11632 break; 11633 } 11634 } 11635 11636 action->dta_kind = desc->dtad_kind; 11637 11638 if ((action->dta_difo = dp) != NULL) 11639 dtrace_difo_hold(dp); 11640 11641 rec->dtrd_action = action->dta_kind; 11642 rec->dtrd_arg = arg; 11643 rec->dtrd_uarg = desc->dtad_uarg; 11644 rec->dtrd_alignment = (uint16_t)align; 11645 rec->dtrd_format = format; 11646 11647 if ((last = ecb->dte_action_last) != NULL) { 11648 ASSERT(ecb->dte_action != NULL); 11649 action->dta_prev = last; 11650 last->dta_next = action; 11651 } else { 11652 ASSERT(ecb->dte_action == NULL); 11653 ecb->dte_action = action; 11654 } 11655 11656 ecb->dte_action_last = action; 11657 11658 return (0); 11659 } 11660 11661 static void 11662 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 11663 { 11664 dtrace_action_t *act = ecb->dte_action, *next; 11665 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 11666 dtrace_difo_t *dp; 11667 uint16_t format; 11668 11669 if (act != NULL && act->dta_refcnt > 1) { 11670 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 11671 act->dta_refcnt--; 11672 } else { 11673 for (; act != NULL; act = next) { 11674 next = act->dta_next; 11675 ASSERT(next != NULL || act == ecb->dte_action_last); 11676 ASSERT(act->dta_refcnt == 1); 11677 11678 if ((format = act->dta_rec.dtrd_format) != 0) 11679 dtrace_format_remove(ecb->dte_state, format); 11680 11681 if ((dp = act->dta_difo) != NULL) 11682 dtrace_difo_release(dp, vstate); 11683 11684 if (DTRACEACT_ISAGG(act->dta_kind)) { 11685 dtrace_ecb_aggregation_destroy(ecb, act); 11686 } else { 11687 kmem_free(act, sizeof (dtrace_action_t)); 11688 } 11689 } 11690 } 11691 11692 ecb->dte_action = NULL; 11693 ecb->dte_action_last = NULL; 11694 ecb->dte_size = 0; 11695 } 11696 11697 static void 11698 dtrace_ecb_disable(dtrace_ecb_t *ecb) 11699 { 11700 /* 11701 * We disable the ECB by removing it from its probe. 11702 */ 11703 dtrace_ecb_t *pecb, *prev = NULL; 11704 dtrace_probe_t *probe = ecb->dte_probe; 11705 11706 ASSERT(MUTEX_HELD(&dtrace_lock)); 11707 11708 if (probe == NULL) { 11709 /* 11710 * This is the NULL probe; there is nothing to disable. 11711 */ 11712 return; 11713 } 11714 11715 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 11716 if (pecb == ecb) 11717 break; 11718 prev = pecb; 11719 } 11720 11721 ASSERT(pecb != NULL); 11722 11723 if (prev == NULL) { 11724 probe->dtpr_ecb = ecb->dte_next; 11725 } else { 11726 prev->dte_next = ecb->dte_next; 11727 } 11728 11729 if (ecb == probe->dtpr_ecb_last) { 11730 ASSERT(ecb->dte_next == NULL); 11731 probe->dtpr_ecb_last = prev; 11732 } 11733 11734 /* 11735 * The ECB has been disconnected from the probe; now sync to assure 11736 * that all CPUs have seen the change before returning. 11737 */ 11738 dtrace_sync(); 11739 11740 if (probe->dtpr_ecb == NULL) { 11741 /* 11742 * That was the last ECB on the probe; clear the predicate 11743 * cache ID for the probe, disable it and sync one more time 11744 * to assure that we'll never hit it again. 11745 */ 11746 dtrace_provider_t *prov = probe->dtpr_provider; 11747 11748 ASSERT(ecb->dte_next == NULL); 11749 ASSERT(probe->dtpr_ecb_last == NULL); 11750 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 11751 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 11752 probe->dtpr_id, probe->dtpr_arg); 11753 dtrace_sync(); 11754 } else { 11755 /* 11756 * There is at least one ECB remaining on the probe. If there 11757 * is _exactly_ one, set the probe's predicate cache ID to be 11758 * the predicate cache ID of the remaining ECB. 11759 */ 11760 ASSERT(probe->dtpr_ecb_last != NULL); 11761 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 11762 11763 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 11764 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 11765 11766 ASSERT(probe->dtpr_ecb->dte_next == NULL); 11767 11768 if (p != NULL) 11769 probe->dtpr_predcache = p->dtp_cacheid; 11770 } 11771 11772 ecb->dte_next = NULL; 11773 } 11774 } 11775 11776 static void 11777 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 11778 { 11779 dtrace_state_t *state = ecb->dte_state; 11780 dtrace_vstate_t *vstate = &state->dts_vstate; 11781 dtrace_predicate_t *pred; 11782 dtrace_epid_t epid = ecb->dte_epid; 11783 11784 ASSERT(MUTEX_HELD(&dtrace_lock)); 11785 ASSERT(ecb->dte_next == NULL); 11786 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 11787 11788 if ((pred = ecb->dte_predicate) != NULL) 11789 dtrace_predicate_release(pred, vstate); 11790 11791 dtrace_ecb_action_remove(ecb); 11792 11793 ASSERT(state->dts_ecbs[epid - 1] == ecb); 11794 state->dts_ecbs[epid - 1] = NULL; 11795 11796 kmem_free(ecb, sizeof (dtrace_ecb_t)); 11797 } 11798 11799 static dtrace_ecb_t * 11800 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 11801 dtrace_enabling_t *enab) 11802 { 11803 dtrace_ecb_t *ecb; 11804 dtrace_predicate_t *pred; 11805 dtrace_actdesc_t *act; 11806 dtrace_provider_t *prov; 11807 dtrace_ecbdesc_t *desc = enab->dten_current; 11808 11809 ASSERT(MUTEX_HELD(&dtrace_lock)); 11810 ASSERT(state != NULL); 11811 11812 ecb = dtrace_ecb_add(state, probe); 11813 ecb->dte_uarg = desc->dted_uarg; 11814 11815 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 11816 dtrace_predicate_hold(pred); 11817 ecb->dte_predicate = pred; 11818 } 11819 11820 if (probe != NULL) { 11821 /* 11822 * If the provider shows more leg than the consumer is old 11823 * enough to see, we need to enable the appropriate implicit 11824 * predicate bits to prevent the ecb from activating at 11825 * revealing times. 11826 * 11827 * Providers specifying DTRACE_PRIV_USER at register time 11828 * are stating that they need the /proc-style privilege 11829 * model to be enforced, and this is what DTRACE_COND_OWNER 11830 * and DTRACE_COND_ZONEOWNER will then do at probe time. 11831 */ 11832 prov = probe->dtpr_provider; 11833 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 11834 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11835 ecb->dte_cond |= DTRACE_COND_OWNER; 11836 11837 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 11838 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11839 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 11840 11841 /* 11842 * If the provider shows us kernel innards and the user 11843 * is lacking sufficient privilege, enable the 11844 * DTRACE_COND_USERMODE implicit predicate. 11845 */ 11846 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 11847 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 11848 ecb->dte_cond |= DTRACE_COND_USERMODE; 11849 } 11850 11851 if (dtrace_ecb_create_cache != NULL) { 11852 /* 11853 * If we have a cached ecb, we'll use its action list instead 11854 * of creating our own (saving both time and space). 11855 */ 11856 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 11857 dtrace_action_t *act = cached->dte_action; 11858 11859 if (act != NULL) { 11860 ASSERT(act->dta_refcnt > 0); 11861 act->dta_refcnt++; 11862 ecb->dte_action = act; 11863 ecb->dte_action_last = cached->dte_action_last; 11864 ecb->dte_needed = cached->dte_needed; 11865 ecb->dte_size = cached->dte_size; 11866 ecb->dte_alignment = cached->dte_alignment; 11867 } 11868 11869 return (ecb); 11870 } 11871 11872 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 11873 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 11874 dtrace_ecb_destroy(ecb); 11875 return (NULL); 11876 } 11877 } 11878 11879 if ((enab->dten_error = dtrace_ecb_resize(ecb)) != 0) { 11880 dtrace_ecb_destroy(ecb); 11881 return (NULL); 11882 } 11883 11884 return (dtrace_ecb_create_cache = ecb); 11885 } 11886 11887 static int 11888 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 11889 { 11890 dtrace_ecb_t *ecb; 11891 dtrace_enabling_t *enab = arg; 11892 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 11893 11894 ASSERT(state != NULL); 11895 11896 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 11897 /* 11898 * This probe was created in a generation for which this 11899 * enabling has previously created ECBs; we don't want to 11900 * enable it again, so just kick out. 11901 */ 11902 return (DTRACE_MATCH_NEXT); 11903 } 11904 11905 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 11906 return (DTRACE_MATCH_DONE); 11907 11908 dtrace_ecb_enable(ecb); 11909 return (DTRACE_MATCH_NEXT); 11910 } 11911 11912 static dtrace_ecb_t * 11913 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 11914 { 11915 dtrace_ecb_t *ecb; 11916 11917 ASSERT(MUTEX_HELD(&dtrace_lock)); 11918 11919 if (id == 0 || id > state->dts_necbs) 11920 return (NULL); 11921 11922 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 11923 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 11924 11925 return (state->dts_ecbs[id - 1]); 11926 } 11927 11928 static dtrace_aggregation_t * 11929 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 11930 { 11931 dtrace_aggregation_t *agg; 11932 11933 ASSERT(MUTEX_HELD(&dtrace_lock)); 11934 11935 if (id == 0 || id > state->dts_naggregations) 11936 return (NULL); 11937 11938 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 11939 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 11940 agg->dtag_id == id); 11941 11942 return (state->dts_aggregations[id - 1]); 11943 } 11944 11945 /* 11946 * DTrace Buffer Functions 11947 * 11948 * The following functions manipulate DTrace buffers. Most of these functions 11949 * are called in the context of establishing or processing consumer state; 11950 * exceptions are explicitly noted. 11951 */ 11952 11953 /* 11954 * Note: called from cross call context. This function switches the two 11955 * buffers on a given CPU. The atomicity of this operation is assured by 11956 * disabling interrupts while the actual switch takes place; the disabling of 11957 * interrupts serializes the execution with any execution of dtrace_probe() on 11958 * the same CPU. 11959 */ 11960 static void 11961 dtrace_buffer_switch(dtrace_buffer_t *buf) 11962 { 11963 caddr_t tomax = buf->dtb_tomax; 11964 caddr_t xamot = buf->dtb_xamot; 11965 dtrace_icookie_t cookie; 11966 hrtime_t now; 11967 11968 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11969 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 11970 11971 cookie = dtrace_interrupt_disable(); 11972 now = dtrace_gethrtime(); 11973 buf->dtb_tomax = xamot; 11974 buf->dtb_xamot = tomax; 11975 buf->dtb_xamot_drops = buf->dtb_drops; 11976 buf->dtb_xamot_offset = buf->dtb_offset; 11977 buf->dtb_xamot_errors = buf->dtb_errors; 11978 buf->dtb_xamot_flags = buf->dtb_flags; 11979 buf->dtb_offset = 0; 11980 buf->dtb_drops = 0; 11981 buf->dtb_errors = 0; 11982 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 11983 buf->dtb_interval = now - buf->dtb_switched; 11984 buf->dtb_switched = now; 11985 dtrace_interrupt_enable(cookie); 11986 } 11987 11988 /* 11989 * Note: called from cross call context. This function activates a buffer 11990 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 11991 * is guaranteed by the disabling of interrupts. 11992 */ 11993 static void 11994 dtrace_buffer_activate(dtrace_state_t *state) 11995 { 11996 dtrace_buffer_t *buf; 11997 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 11998 11999 buf = &state->dts_buffer[curcpu]; 12000 12001 if (buf->dtb_tomax != NULL) { 12002 /* 12003 * We might like to assert that the buffer is marked inactive, 12004 * but this isn't necessarily true: the buffer for the CPU 12005 * that processes the BEGIN probe has its buffer activated 12006 * manually. In this case, we take the (harmless) action 12007 * re-clearing the bit INACTIVE bit. 12008 */ 12009 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 12010 } 12011 12012 dtrace_interrupt_enable(cookie); 12013 } 12014 12015 #ifdef __FreeBSD__ 12016 /* 12017 * Activate the specified per-CPU buffer. This is used instead of 12018 * dtrace_buffer_activate() when APs have not yet started, i.e. when 12019 * activating anonymous state. 12020 */ 12021 static void 12022 dtrace_buffer_activate_cpu(dtrace_state_t *state, int cpu) 12023 { 12024 12025 if (state->dts_buffer[cpu].dtb_tomax != NULL) 12026 state->dts_buffer[cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 12027 } 12028 #endif 12029 12030 static int 12031 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 12032 processorid_t cpu, int *factor) 12033 { 12034 #ifdef illumos 12035 cpu_t *cp; 12036 #endif 12037 dtrace_buffer_t *buf; 12038 int allocated = 0, desired = 0; 12039 12040 #ifdef illumos 12041 ASSERT(MUTEX_HELD(&cpu_lock)); 12042 ASSERT(MUTEX_HELD(&dtrace_lock)); 12043 12044 *factor = 1; 12045 12046 if (size > dtrace_nonroot_maxsize && 12047 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 12048 return (EFBIG); 12049 12050 cp = cpu_list; 12051 12052 do { 12053 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 12054 continue; 12055 12056 buf = &bufs[cp->cpu_id]; 12057 12058 /* 12059 * If there is already a buffer allocated for this CPU, it 12060 * is only possible that this is a DR event. In this case, 12061 */ 12062 if (buf->dtb_tomax != NULL) { 12063 ASSERT(buf->dtb_size == size); 12064 continue; 12065 } 12066 12067 ASSERT(buf->dtb_xamot == NULL); 12068 12069 if ((buf->dtb_tomax = kmem_zalloc(size, 12070 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12071 goto err; 12072 12073 buf->dtb_size = size; 12074 buf->dtb_flags = flags; 12075 buf->dtb_offset = 0; 12076 buf->dtb_drops = 0; 12077 12078 if (flags & DTRACEBUF_NOSWITCH) 12079 continue; 12080 12081 if ((buf->dtb_xamot = kmem_zalloc(size, 12082 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12083 goto err; 12084 } while ((cp = cp->cpu_next) != cpu_list); 12085 12086 return (0); 12087 12088 err: 12089 cp = cpu_list; 12090 12091 do { 12092 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 12093 continue; 12094 12095 buf = &bufs[cp->cpu_id]; 12096 desired += 2; 12097 12098 if (buf->dtb_xamot != NULL) { 12099 ASSERT(buf->dtb_tomax != NULL); 12100 ASSERT(buf->dtb_size == size); 12101 kmem_free(buf->dtb_xamot, size); 12102 allocated++; 12103 } 12104 12105 if (buf->dtb_tomax != NULL) { 12106 ASSERT(buf->dtb_size == size); 12107 kmem_free(buf->dtb_tomax, size); 12108 allocated++; 12109 } 12110 12111 buf->dtb_tomax = NULL; 12112 buf->dtb_xamot = NULL; 12113 buf->dtb_size = 0; 12114 } while ((cp = cp->cpu_next) != cpu_list); 12115 #else 12116 int i; 12117 12118 *factor = 1; 12119 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \ 12120 defined(__mips__) || defined(__powerpc__) || defined(__riscv__) 12121 /* 12122 * FreeBSD isn't good at limiting the amount of memory we 12123 * ask to malloc, so let's place a limit here before trying 12124 * to do something that might well end in tears at bedtime. 12125 */ 12126 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 12127 return (ENOMEM); 12128 #endif 12129 12130 ASSERT(MUTEX_HELD(&dtrace_lock)); 12131 CPU_FOREACH(i) { 12132 if (cpu != DTRACE_CPUALL && cpu != i) 12133 continue; 12134 12135 buf = &bufs[i]; 12136 12137 /* 12138 * If there is already a buffer allocated for this CPU, it 12139 * is only possible that this is a DR event. In this case, 12140 * the buffer size must match our specified size. 12141 */ 12142 if (buf->dtb_tomax != NULL) { 12143 ASSERT(buf->dtb_size == size); 12144 continue; 12145 } 12146 12147 ASSERT(buf->dtb_xamot == NULL); 12148 12149 if ((buf->dtb_tomax = kmem_zalloc(size, 12150 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12151 goto err; 12152 12153 buf->dtb_size = size; 12154 buf->dtb_flags = flags; 12155 buf->dtb_offset = 0; 12156 buf->dtb_drops = 0; 12157 12158 if (flags & DTRACEBUF_NOSWITCH) 12159 continue; 12160 12161 if ((buf->dtb_xamot = kmem_zalloc(size, 12162 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12163 goto err; 12164 } 12165 12166 return (0); 12167 12168 err: 12169 /* 12170 * Error allocating memory, so free the buffers that were 12171 * allocated before the failed allocation. 12172 */ 12173 CPU_FOREACH(i) { 12174 if (cpu != DTRACE_CPUALL && cpu != i) 12175 continue; 12176 12177 buf = &bufs[i]; 12178 desired += 2; 12179 12180 if (buf->dtb_xamot != NULL) { 12181 ASSERT(buf->dtb_tomax != NULL); 12182 ASSERT(buf->dtb_size == size); 12183 kmem_free(buf->dtb_xamot, size); 12184 allocated++; 12185 } 12186 12187 if (buf->dtb_tomax != NULL) { 12188 ASSERT(buf->dtb_size == size); 12189 kmem_free(buf->dtb_tomax, size); 12190 allocated++; 12191 } 12192 12193 buf->dtb_tomax = NULL; 12194 buf->dtb_xamot = NULL; 12195 buf->dtb_size = 0; 12196 12197 } 12198 #endif 12199 *factor = desired / (allocated > 0 ? allocated : 1); 12200 12201 return (ENOMEM); 12202 } 12203 12204 /* 12205 * Note: called from probe context. This function just increments the drop 12206 * count on a buffer. It has been made a function to allow for the 12207 * possibility of understanding the source of mysterious drop counts. (A 12208 * problem for which one may be particularly disappointed that DTrace cannot 12209 * be used to understand DTrace.) 12210 */ 12211 static void 12212 dtrace_buffer_drop(dtrace_buffer_t *buf) 12213 { 12214 buf->dtb_drops++; 12215 } 12216 12217 /* 12218 * Note: called from probe context. This function is called to reserve space 12219 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 12220 * mstate. Returns the new offset in the buffer, or a negative value if an 12221 * error has occurred. 12222 */ 12223 static intptr_t 12224 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 12225 dtrace_state_t *state, dtrace_mstate_t *mstate) 12226 { 12227 intptr_t offs = buf->dtb_offset, soffs; 12228 intptr_t woffs; 12229 caddr_t tomax; 12230 size_t total; 12231 12232 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 12233 return (-1); 12234 12235 if ((tomax = buf->dtb_tomax) == NULL) { 12236 dtrace_buffer_drop(buf); 12237 return (-1); 12238 } 12239 12240 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 12241 while (offs & (align - 1)) { 12242 /* 12243 * Assert that our alignment is off by a number which 12244 * is itself sizeof (uint32_t) aligned. 12245 */ 12246 ASSERT(!((align - (offs & (align - 1))) & 12247 (sizeof (uint32_t) - 1))); 12248 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12249 offs += sizeof (uint32_t); 12250 } 12251 12252 if ((soffs = offs + needed) > buf->dtb_size) { 12253 dtrace_buffer_drop(buf); 12254 return (-1); 12255 } 12256 12257 if (mstate == NULL) 12258 return (offs); 12259 12260 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 12261 mstate->dtms_scratch_size = buf->dtb_size - soffs; 12262 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12263 12264 return (offs); 12265 } 12266 12267 if (buf->dtb_flags & DTRACEBUF_FILL) { 12268 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 12269 (buf->dtb_flags & DTRACEBUF_FULL)) 12270 return (-1); 12271 goto out; 12272 } 12273 12274 total = needed + (offs & (align - 1)); 12275 12276 /* 12277 * For a ring buffer, life is quite a bit more complicated. Before 12278 * we can store any padding, we need to adjust our wrapping offset. 12279 * (If we've never before wrapped or we're not about to, no adjustment 12280 * is required.) 12281 */ 12282 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 12283 offs + total > buf->dtb_size) { 12284 woffs = buf->dtb_xamot_offset; 12285 12286 if (offs + total > buf->dtb_size) { 12287 /* 12288 * We can't fit in the end of the buffer. First, a 12289 * sanity check that we can fit in the buffer at all. 12290 */ 12291 if (total > buf->dtb_size) { 12292 dtrace_buffer_drop(buf); 12293 return (-1); 12294 } 12295 12296 /* 12297 * We're going to be storing at the top of the buffer, 12298 * so now we need to deal with the wrapped offset. We 12299 * only reset our wrapped offset to 0 if it is 12300 * currently greater than the current offset. If it 12301 * is less than the current offset, it is because a 12302 * previous allocation induced a wrap -- but the 12303 * allocation didn't subsequently take the space due 12304 * to an error or false predicate evaluation. In this 12305 * case, we'll just leave the wrapped offset alone: if 12306 * the wrapped offset hasn't been advanced far enough 12307 * for this allocation, it will be adjusted in the 12308 * lower loop. 12309 */ 12310 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 12311 if (woffs >= offs) 12312 woffs = 0; 12313 } else { 12314 woffs = 0; 12315 } 12316 12317 /* 12318 * Now we know that we're going to be storing to the 12319 * top of the buffer and that there is room for us 12320 * there. We need to clear the buffer from the current 12321 * offset to the end (there may be old gunk there). 12322 */ 12323 while (offs < buf->dtb_size) 12324 tomax[offs++] = 0; 12325 12326 /* 12327 * We need to set our offset to zero. And because we 12328 * are wrapping, we need to set the bit indicating as 12329 * much. We can also adjust our needed space back 12330 * down to the space required by the ECB -- we know 12331 * that the top of the buffer is aligned. 12332 */ 12333 offs = 0; 12334 total = needed; 12335 buf->dtb_flags |= DTRACEBUF_WRAPPED; 12336 } else { 12337 /* 12338 * There is room for us in the buffer, so we simply 12339 * need to check the wrapped offset. 12340 */ 12341 if (woffs < offs) { 12342 /* 12343 * The wrapped offset is less than the offset. 12344 * This can happen if we allocated buffer space 12345 * that induced a wrap, but then we didn't 12346 * subsequently take the space due to an error 12347 * or false predicate evaluation. This is 12348 * okay; we know that _this_ allocation isn't 12349 * going to induce a wrap. We still can't 12350 * reset the wrapped offset to be zero, 12351 * however: the space may have been trashed in 12352 * the previous failed probe attempt. But at 12353 * least the wrapped offset doesn't need to 12354 * be adjusted at all... 12355 */ 12356 goto out; 12357 } 12358 } 12359 12360 while (offs + total > woffs) { 12361 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 12362 size_t size; 12363 12364 if (epid == DTRACE_EPIDNONE) { 12365 size = sizeof (uint32_t); 12366 } else { 12367 ASSERT3U(epid, <=, state->dts_necbs); 12368 ASSERT(state->dts_ecbs[epid - 1] != NULL); 12369 12370 size = state->dts_ecbs[epid - 1]->dte_size; 12371 } 12372 12373 ASSERT(woffs + size <= buf->dtb_size); 12374 ASSERT(size != 0); 12375 12376 if (woffs + size == buf->dtb_size) { 12377 /* 12378 * We've reached the end of the buffer; we want 12379 * to set the wrapped offset to 0 and break 12380 * out. However, if the offs is 0, then we're 12381 * in a strange edge-condition: the amount of 12382 * space that we want to reserve plus the size 12383 * of the record that we're overwriting is 12384 * greater than the size of the buffer. This 12385 * is problematic because if we reserve the 12386 * space but subsequently don't consume it (due 12387 * to a failed predicate or error) the wrapped 12388 * offset will be 0 -- yet the EPID at offset 0 12389 * will not be committed. This situation is 12390 * relatively easy to deal with: if we're in 12391 * this case, the buffer is indistinguishable 12392 * from one that hasn't wrapped; we need only 12393 * finish the job by clearing the wrapped bit, 12394 * explicitly setting the offset to be 0, and 12395 * zero'ing out the old data in the buffer. 12396 */ 12397 if (offs == 0) { 12398 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 12399 buf->dtb_offset = 0; 12400 woffs = total; 12401 12402 while (woffs < buf->dtb_size) 12403 tomax[woffs++] = 0; 12404 } 12405 12406 woffs = 0; 12407 break; 12408 } 12409 12410 woffs += size; 12411 } 12412 12413 /* 12414 * We have a wrapped offset. It may be that the wrapped offset 12415 * has become zero -- that's okay. 12416 */ 12417 buf->dtb_xamot_offset = woffs; 12418 } 12419 12420 out: 12421 /* 12422 * Now we can plow the buffer with any necessary padding. 12423 */ 12424 while (offs & (align - 1)) { 12425 /* 12426 * Assert that our alignment is off by a number which 12427 * is itself sizeof (uint32_t) aligned. 12428 */ 12429 ASSERT(!((align - (offs & (align - 1))) & 12430 (sizeof (uint32_t) - 1))); 12431 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12432 offs += sizeof (uint32_t); 12433 } 12434 12435 if (buf->dtb_flags & DTRACEBUF_FILL) { 12436 if (offs + needed > buf->dtb_size - state->dts_reserve) { 12437 buf->dtb_flags |= DTRACEBUF_FULL; 12438 return (-1); 12439 } 12440 } 12441 12442 if (mstate == NULL) 12443 return (offs); 12444 12445 /* 12446 * For ring buffers and fill buffers, the scratch space is always 12447 * the inactive buffer. 12448 */ 12449 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 12450 mstate->dtms_scratch_size = buf->dtb_size; 12451 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12452 12453 return (offs); 12454 } 12455 12456 static void 12457 dtrace_buffer_polish(dtrace_buffer_t *buf) 12458 { 12459 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 12460 ASSERT(MUTEX_HELD(&dtrace_lock)); 12461 12462 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 12463 return; 12464 12465 /* 12466 * We need to polish the ring buffer. There are three cases: 12467 * 12468 * - The first (and presumably most common) is that there is no gap 12469 * between the buffer offset and the wrapped offset. In this case, 12470 * there is nothing in the buffer that isn't valid data; we can 12471 * mark the buffer as polished and return. 12472 * 12473 * - The second (less common than the first but still more common 12474 * than the third) is that there is a gap between the buffer offset 12475 * and the wrapped offset, and the wrapped offset is larger than the 12476 * buffer offset. This can happen because of an alignment issue, or 12477 * can happen because of a call to dtrace_buffer_reserve() that 12478 * didn't subsequently consume the buffer space. In this case, 12479 * we need to zero the data from the buffer offset to the wrapped 12480 * offset. 12481 * 12482 * - The third (and least common) is that there is a gap between the 12483 * buffer offset and the wrapped offset, but the wrapped offset is 12484 * _less_ than the buffer offset. This can only happen because a 12485 * call to dtrace_buffer_reserve() induced a wrap, but the space 12486 * was not subsequently consumed. In this case, we need to zero the 12487 * space from the offset to the end of the buffer _and_ from the 12488 * top of the buffer to the wrapped offset. 12489 */ 12490 if (buf->dtb_offset < buf->dtb_xamot_offset) { 12491 bzero(buf->dtb_tomax + buf->dtb_offset, 12492 buf->dtb_xamot_offset - buf->dtb_offset); 12493 } 12494 12495 if (buf->dtb_offset > buf->dtb_xamot_offset) { 12496 bzero(buf->dtb_tomax + buf->dtb_offset, 12497 buf->dtb_size - buf->dtb_offset); 12498 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 12499 } 12500 } 12501 12502 /* 12503 * This routine determines if data generated at the specified time has likely 12504 * been entirely consumed at user-level. This routine is called to determine 12505 * if an ECB on a defunct probe (but for an active enabling) can be safely 12506 * disabled and destroyed. 12507 */ 12508 static int 12509 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 12510 { 12511 int i; 12512 12513 for (i = 0; i < NCPU; i++) { 12514 dtrace_buffer_t *buf = &bufs[i]; 12515 12516 if (buf->dtb_size == 0) 12517 continue; 12518 12519 if (buf->dtb_flags & DTRACEBUF_RING) 12520 return (0); 12521 12522 if (!buf->dtb_switched && buf->dtb_offset != 0) 12523 return (0); 12524 12525 if (buf->dtb_switched - buf->dtb_interval < when) 12526 return (0); 12527 } 12528 12529 return (1); 12530 } 12531 12532 static void 12533 dtrace_buffer_free(dtrace_buffer_t *bufs) 12534 { 12535 int i; 12536 12537 for (i = 0; i < NCPU; i++) { 12538 dtrace_buffer_t *buf = &bufs[i]; 12539 12540 if (buf->dtb_tomax == NULL) { 12541 ASSERT(buf->dtb_xamot == NULL); 12542 ASSERT(buf->dtb_size == 0); 12543 continue; 12544 } 12545 12546 if (buf->dtb_xamot != NULL) { 12547 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 12548 kmem_free(buf->dtb_xamot, buf->dtb_size); 12549 } 12550 12551 kmem_free(buf->dtb_tomax, buf->dtb_size); 12552 buf->dtb_size = 0; 12553 buf->dtb_tomax = NULL; 12554 buf->dtb_xamot = NULL; 12555 } 12556 } 12557 12558 /* 12559 * DTrace Enabling Functions 12560 */ 12561 static dtrace_enabling_t * 12562 dtrace_enabling_create(dtrace_vstate_t *vstate) 12563 { 12564 dtrace_enabling_t *enab; 12565 12566 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 12567 enab->dten_vstate = vstate; 12568 12569 return (enab); 12570 } 12571 12572 static void 12573 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 12574 { 12575 dtrace_ecbdesc_t **ndesc; 12576 size_t osize, nsize; 12577 12578 /* 12579 * We can't add to enablings after we've enabled them, or after we've 12580 * retained them. 12581 */ 12582 ASSERT(enab->dten_probegen == 0); 12583 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12584 12585 if (enab->dten_ndesc < enab->dten_maxdesc) { 12586 enab->dten_desc[enab->dten_ndesc++] = ecb; 12587 return; 12588 } 12589 12590 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12591 12592 if (enab->dten_maxdesc == 0) { 12593 enab->dten_maxdesc = 1; 12594 } else { 12595 enab->dten_maxdesc <<= 1; 12596 } 12597 12598 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 12599 12600 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12601 ndesc = kmem_zalloc(nsize, KM_SLEEP); 12602 bcopy(enab->dten_desc, ndesc, osize); 12603 if (enab->dten_desc != NULL) 12604 kmem_free(enab->dten_desc, osize); 12605 12606 enab->dten_desc = ndesc; 12607 enab->dten_desc[enab->dten_ndesc++] = ecb; 12608 } 12609 12610 static void 12611 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 12612 dtrace_probedesc_t *pd) 12613 { 12614 dtrace_ecbdesc_t *new; 12615 dtrace_predicate_t *pred; 12616 dtrace_actdesc_t *act; 12617 12618 /* 12619 * We're going to create a new ECB description that matches the 12620 * specified ECB in every way, but has the specified probe description. 12621 */ 12622 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12623 12624 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 12625 dtrace_predicate_hold(pred); 12626 12627 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 12628 dtrace_actdesc_hold(act); 12629 12630 new->dted_action = ecb->dted_action; 12631 new->dted_pred = ecb->dted_pred; 12632 new->dted_probe = *pd; 12633 new->dted_uarg = ecb->dted_uarg; 12634 12635 dtrace_enabling_add(enab, new); 12636 } 12637 12638 static void 12639 dtrace_enabling_dump(dtrace_enabling_t *enab) 12640 { 12641 int i; 12642 12643 for (i = 0; i < enab->dten_ndesc; i++) { 12644 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 12645 12646 #ifdef __FreeBSD__ 12647 printf("dtrace: enabling probe %d (%s:%s:%s:%s)\n", i, 12648 desc->dtpd_provider, desc->dtpd_mod, 12649 desc->dtpd_func, desc->dtpd_name); 12650 #else 12651 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 12652 desc->dtpd_provider, desc->dtpd_mod, 12653 desc->dtpd_func, desc->dtpd_name); 12654 #endif 12655 } 12656 } 12657 12658 static void 12659 dtrace_enabling_destroy(dtrace_enabling_t *enab) 12660 { 12661 int i; 12662 dtrace_ecbdesc_t *ep; 12663 dtrace_vstate_t *vstate = enab->dten_vstate; 12664 12665 ASSERT(MUTEX_HELD(&dtrace_lock)); 12666 12667 for (i = 0; i < enab->dten_ndesc; i++) { 12668 dtrace_actdesc_t *act, *next; 12669 dtrace_predicate_t *pred; 12670 12671 ep = enab->dten_desc[i]; 12672 12673 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 12674 dtrace_predicate_release(pred, vstate); 12675 12676 for (act = ep->dted_action; act != NULL; act = next) { 12677 next = act->dtad_next; 12678 dtrace_actdesc_release(act, vstate); 12679 } 12680 12681 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12682 } 12683 12684 if (enab->dten_desc != NULL) 12685 kmem_free(enab->dten_desc, 12686 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 12687 12688 /* 12689 * If this was a retained enabling, decrement the dts_nretained count 12690 * and take it off of the dtrace_retained list. 12691 */ 12692 if (enab->dten_prev != NULL || enab->dten_next != NULL || 12693 dtrace_retained == enab) { 12694 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12695 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 12696 enab->dten_vstate->dtvs_state->dts_nretained--; 12697 dtrace_retained_gen++; 12698 } 12699 12700 if (enab->dten_prev == NULL) { 12701 if (dtrace_retained == enab) { 12702 dtrace_retained = enab->dten_next; 12703 12704 if (dtrace_retained != NULL) 12705 dtrace_retained->dten_prev = NULL; 12706 } 12707 } else { 12708 ASSERT(enab != dtrace_retained); 12709 ASSERT(dtrace_retained != NULL); 12710 enab->dten_prev->dten_next = enab->dten_next; 12711 } 12712 12713 if (enab->dten_next != NULL) { 12714 ASSERT(dtrace_retained != NULL); 12715 enab->dten_next->dten_prev = enab->dten_prev; 12716 } 12717 12718 kmem_free(enab, sizeof (dtrace_enabling_t)); 12719 } 12720 12721 static int 12722 dtrace_enabling_retain(dtrace_enabling_t *enab) 12723 { 12724 dtrace_state_t *state; 12725 12726 ASSERT(MUTEX_HELD(&dtrace_lock)); 12727 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12728 ASSERT(enab->dten_vstate != NULL); 12729 12730 state = enab->dten_vstate->dtvs_state; 12731 ASSERT(state != NULL); 12732 12733 /* 12734 * We only allow each state to retain dtrace_retain_max enablings. 12735 */ 12736 if (state->dts_nretained >= dtrace_retain_max) 12737 return (ENOSPC); 12738 12739 state->dts_nretained++; 12740 dtrace_retained_gen++; 12741 12742 if (dtrace_retained == NULL) { 12743 dtrace_retained = enab; 12744 return (0); 12745 } 12746 12747 enab->dten_next = dtrace_retained; 12748 dtrace_retained->dten_prev = enab; 12749 dtrace_retained = enab; 12750 12751 return (0); 12752 } 12753 12754 static int 12755 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 12756 dtrace_probedesc_t *create) 12757 { 12758 dtrace_enabling_t *new, *enab; 12759 int found = 0, err = ENOENT; 12760 12761 ASSERT(MUTEX_HELD(&dtrace_lock)); 12762 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 12763 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 12764 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 12765 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 12766 12767 new = dtrace_enabling_create(&state->dts_vstate); 12768 12769 /* 12770 * Iterate over all retained enablings, looking for enablings that 12771 * match the specified state. 12772 */ 12773 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12774 int i; 12775 12776 /* 12777 * dtvs_state can only be NULL for helper enablings -- and 12778 * helper enablings can't be retained. 12779 */ 12780 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12781 12782 if (enab->dten_vstate->dtvs_state != state) 12783 continue; 12784 12785 /* 12786 * Now iterate over each probe description; we're looking for 12787 * an exact match to the specified probe description. 12788 */ 12789 for (i = 0; i < enab->dten_ndesc; i++) { 12790 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12791 dtrace_probedesc_t *pd = &ep->dted_probe; 12792 12793 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 12794 continue; 12795 12796 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 12797 continue; 12798 12799 if (strcmp(pd->dtpd_func, match->dtpd_func)) 12800 continue; 12801 12802 if (strcmp(pd->dtpd_name, match->dtpd_name)) 12803 continue; 12804 12805 /* 12806 * We have a winning probe! Add it to our growing 12807 * enabling. 12808 */ 12809 found = 1; 12810 dtrace_enabling_addlike(new, ep, create); 12811 } 12812 } 12813 12814 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 12815 dtrace_enabling_destroy(new); 12816 return (err); 12817 } 12818 12819 return (0); 12820 } 12821 12822 static void 12823 dtrace_enabling_retract(dtrace_state_t *state) 12824 { 12825 dtrace_enabling_t *enab, *next; 12826 12827 ASSERT(MUTEX_HELD(&dtrace_lock)); 12828 12829 /* 12830 * Iterate over all retained enablings, destroy the enablings retained 12831 * for the specified state. 12832 */ 12833 for (enab = dtrace_retained; enab != NULL; enab = next) { 12834 next = enab->dten_next; 12835 12836 /* 12837 * dtvs_state can only be NULL for helper enablings -- and 12838 * helper enablings can't be retained. 12839 */ 12840 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12841 12842 if (enab->dten_vstate->dtvs_state == state) { 12843 ASSERT(state->dts_nretained > 0); 12844 dtrace_enabling_destroy(enab); 12845 } 12846 } 12847 12848 ASSERT(state->dts_nretained == 0); 12849 } 12850 12851 static int 12852 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 12853 { 12854 int i = 0; 12855 int matched = 0; 12856 12857 ASSERT(MUTEX_HELD(&cpu_lock)); 12858 ASSERT(MUTEX_HELD(&dtrace_lock)); 12859 12860 for (i = 0; i < enab->dten_ndesc; i++) { 12861 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12862 12863 enab->dten_current = ep; 12864 enab->dten_error = 0; 12865 12866 matched += dtrace_probe_enable(&ep->dted_probe, enab); 12867 12868 if (enab->dten_error != 0) { 12869 /* 12870 * If we get an error half-way through enabling the 12871 * probes, we kick out -- perhaps with some number of 12872 * them enabled. Leaving enabled probes enabled may 12873 * be slightly confusing for user-level, but we expect 12874 * that no one will attempt to actually drive on in 12875 * the face of such errors. If this is an anonymous 12876 * enabling (indicated with a NULL nmatched pointer), 12877 * we cmn_err() a message. We aren't expecting to 12878 * get such an error -- such as it can exist at all, 12879 * it would be a result of corrupted DOF in the driver 12880 * properties. 12881 */ 12882 if (nmatched == NULL) { 12883 cmn_err(CE_WARN, "dtrace_enabling_match() " 12884 "error on %p: %d", (void *)ep, 12885 enab->dten_error); 12886 } 12887 12888 return (enab->dten_error); 12889 } 12890 } 12891 12892 enab->dten_probegen = dtrace_probegen; 12893 if (nmatched != NULL) 12894 *nmatched = matched; 12895 12896 return (0); 12897 } 12898 12899 static void 12900 dtrace_enabling_matchall(void) 12901 { 12902 dtrace_enabling_t *enab; 12903 12904 mutex_enter(&cpu_lock); 12905 mutex_enter(&dtrace_lock); 12906 12907 /* 12908 * Iterate over all retained enablings to see if any probes match 12909 * against them. We only perform this operation on enablings for which 12910 * we have sufficient permissions by virtue of being in the global zone 12911 * or in the same zone as the DTrace client. Because we can be called 12912 * after dtrace_detach() has been called, we cannot assert that there 12913 * are retained enablings. We can safely load from dtrace_retained, 12914 * however: the taskq_destroy() at the end of dtrace_detach() will 12915 * block pending our completion. 12916 */ 12917 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12918 #ifdef illumos 12919 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 12920 12921 if (INGLOBALZONE(curproc) || 12922 cr != NULL && getzoneid() == crgetzoneid(cr)) 12923 #endif 12924 (void) dtrace_enabling_match(enab, NULL); 12925 } 12926 12927 mutex_exit(&dtrace_lock); 12928 mutex_exit(&cpu_lock); 12929 } 12930 12931 /* 12932 * If an enabling is to be enabled without having matched probes (that is, if 12933 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 12934 * enabling must be _primed_ by creating an ECB for every ECB description. 12935 * This must be done to assure that we know the number of speculations, the 12936 * number of aggregations, the minimum buffer size needed, etc. before we 12937 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 12938 * enabling any probes, we create ECBs for every ECB decription, but with a 12939 * NULL probe -- which is exactly what this function does. 12940 */ 12941 static void 12942 dtrace_enabling_prime(dtrace_state_t *state) 12943 { 12944 dtrace_enabling_t *enab; 12945 int i; 12946 12947 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12948 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12949 12950 if (enab->dten_vstate->dtvs_state != state) 12951 continue; 12952 12953 /* 12954 * We don't want to prime an enabling more than once, lest 12955 * we allow a malicious user to induce resource exhaustion. 12956 * (The ECBs that result from priming an enabling aren't 12957 * leaked -- but they also aren't deallocated until the 12958 * consumer state is destroyed.) 12959 */ 12960 if (enab->dten_primed) 12961 continue; 12962 12963 for (i = 0; i < enab->dten_ndesc; i++) { 12964 enab->dten_current = enab->dten_desc[i]; 12965 (void) dtrace_probe_enable(NULL, enab); 12966 } 12967 12968 enab->dten_primed = 1; 12969 } 12970 } 12971 12972 /* 12973 * Called to indicate that probes should be provided due to retained 12974 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 12975 * must take an initial lap through the enabling calling the dtps_provide() 12976 * entry point explicitly to allow for autocreated probes. 12977 */ 12978 static void 12979 dtrace_enabling_provide(dtrace_provider_t *prv) 12980 { 12981 int i, all = 0; 12982 dtrace_probedesc_t desc; 12983 dtrace_genid_t gen; 12984 12985 ASSERT(MUTEX_HELD(&dtrace_lock)); 12986 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 12987 12988 if (prv == NULL) { 12989 all = 1; 12990 prv = dtrace_provider; 12991 } 12992 12993 do { 12994 dtrace_enabling_t *enab; 12995 void *parg = prv->dtpv_arg; 12996 12997 retry: 12998 gen = dtrace_retained_gen; 12999 for (enab = dtrace_retained; enab != NULL; 13000 enab = enab->dten_next) { 13001 for (i = 0; i < enab->dten_ndesc; i++) { 13002 desc = enab->dten_desc[i]->dted_probe; 13003 mutex_exit(&dtrace_lock); 13004 prv->dtpv_pops.dtps_provide(parg, &desc); 13005 mutex_enter(&dtrace_lock); 13006 /* 13007 * Process the retained enablings again if 13008 * they have changed while we weren't holding 13009 * dtrace_lock. 13010 */ 13011 if (gen != dtrace_retained_gen) 13012 goto retry; 13013 } 13014 } 13015 } while (all && (prv = prv->dtpv_next) != NULL); 13016 13017 mutex_exit(&dtrace_lock); 13018 dtrace_probe_provide(NULL, all ? NULL : prv); 13019 mutex_enter(&dtrace_lock); 13020 } 13021 13022 /* 13023 * Called to reap ECBs that are attached to probes from defunct providers. 13024 */ 13025 static void 13026 dtrace_enabling_reap(void) 13027 { 13028 dtrace_provider_t *prov; 13029 dtrace_probe_t *probe; 13030 dtrace_ecb_t *ecb; 13031 hrtime_t when; 13032 int i; 13033 13034 mutex_enter(&cpu_lock); 13035 mutex_enter(&dtrace_lock); 13036 13037 for (i = 0; i < dtrace_nprobes; i++) { 13038 if ((probe = dtrace_probes[i]) == NULL) 13039 continue; 13040 13041 if (probe->dtpr_ecb == NULL) 13042 continue; 13043 13044 prov = probe->dtpr_provider; 13045 13046 if ((when = prov->dtpv_defunct) == 0) 13047 continue; 13048 13049 /* 13050 * We have ECBs on a defunct provider: we want to reap these 13051 * ECBs to allow the provider to unregister. The destruction 13052 * of these ECBs must be done carefully: if we destroy the ECB 13053 * and the consumer later wishes to consume an EPID that 13054 * corresponds to the destroyed ECB (and if the EPID metadata 13055 * has not been previously consumed), the consumer will abort 13056 * processing on the unknown EPID. To reduce (but not, sadly, 13057 * eliminate) the possibility of this, we will only destroy an 13058 * ECB for a defunct provider if, for the state that 13059 * corresponds to the ECB: 13060 * 13061 * (a) There is no speculative tracing (which can effectively 13062 * cache an EPID for an arbitrary amount of time). 13063 * 13064 * (b) The principal buffers have been switched twice since the 13065 * provider became defunct. 13066 * 13067 * (c) The aggregation buffers are of zero size or have been 13068 * switched twice since the provider became defunct. 13069 * 13070 * We use dts_speculates to determine (a) and call a function 13071 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 13072 * that as soon as we've been unable to destroy one of the ECBs 13073 * associated with the probe, we quit trying -- reaping is only 13074 * fruitful in as much as we can destroy all ECBs associated 13075 * with the defunct provider's probes. 13076 */ 13077 while ((ecb = probe->dtpr_ecb) != NULL) { 13078 dtrace_state_t *state = ecb->dte_state; 13079 dtrace_buffer_t *buf = state->dts_buffer; 13080 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 13081 13082 if (state->dts_speculates) 13083 break; 13084 13085 if (!dtrace_buffer_consumed(buf, when)) 13086 break; 13087 13088 if (!dtrace_buffer_consumed(aggbuf, when)) 13089 break; 13090 13091 dtrace_ecb_disable(ecb); 13092 ASSERT(probe->dtpr_ecb != ecb); 13093 dtrace_ecb_destroy(ecb); 13094 } 13095 } 13096 13097 mutex_exit(&dtrace_lock); 13098 mutex_exit(&cpu_lock); 13099 } 13100 13101 /* 13102 * DTrace DOF Functions 13103 */ 13104 /*ARGSUSED*/ 13105 static void 13106 dtrace_dof_error(dof_hdr_t *dof, const char *str) 13107 { 13108 if (dtrace_err_verbose) 13109 cmn_err(CE_WARN, "failed to process DOF: %s", str); 13110 13111 #ifdef DTRACE_ERRDEBUG 13112 dtrace_errdebug(str); 13113 #endif 13114 } 13115 13116 /* 13117 * Create DOF out of a currently enabled state. Right now, we only create 13118 * DOF containing the run-time options -- but this could be expanded to create 13119 * complete DOF representing the enabled state. 13120 */ 13121 static dof_hdr_t * 13122 dtrace_dof_create(dtrace_state_t *state) 13123 { 13124 dof_hdr_t *dof; 13125 dof_sec_t *sec; 13126 dof_optdesc_t *opt; 13127 int i, len = sizeof (dof_hdr_t) + 13128 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 13129 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 13130 13131 ASSERT(MUTEX_HELD(&dtrace_lock)); 13132 13133 dof = kmem_zalloc(len, KM_SLEEP); 13134 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 13135 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 13136 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 13137 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 13138 13139 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 13140 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 13141 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 13142 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 13143 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 13144 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 13145 13146 dof->dofh_flags = 0; 13147 dof->dofh_hdrsize = sizeof (dof_hdr_t); 13148 dof->dofh_secsize = sizeof (dof_sec_t); 13149 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 13150 dof->dofh_secoff = sizeof (dof_hdr_t); 13151 dof->dofh_loadsz = len; 13152 dof->dofh_filesz = len; 13153 dof->dofh_pad = 0; 13154 13155 /* 13156 * Fill in the option section header... 13157 */ 13158 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 13159 sec->dofs_type = DOF_SECT_OPTDESC; 13160 sec->dofs_align = sizeof (uint64_t); 13161 sec->dofs_flags = DOF_SECF_LOAD; 13162 sec->dofs_entsize = sizeof (dof_optdesc_t); 13163 13164 opt = (dof_optdesc_t *)((uintptr_t)sec + 13165 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 13166 13167 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 13168 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 13169 13170 for (i = 0; i < DTRACEOPT_MAX; i++) { 13171 opt[i].dofo_option = i; 13172 opt[i].dofo_strtab = DOF_SECIDX_NONE; 13173 opt[i].dofo_value = state->dts_options[i]; 13174 } 13175 13176 return (dof); 13177 } 13178 13179 static dof_hdr_t * 13180 dtrace_dof_copyin(uintptr_t uarg, int *errp) 13181 { 13182 dof_hdr_t hdr, *dof; 13183 13184 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13185 13186 /* 13187 * First, we're going to copyin() the sizeof (dof_hdr_t). 13188 */ 13189 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 13190 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13191 *errp = EFAULT; 13192 return (NULL); 13193 } 13194 13195 /* 13196 * Now we'll allocate the entire DOF and copy it in -- provided 13197 * that the length isn't outrageous. 13198 */ 13199 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13200 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13201 *errp = E2BIG; 13202 return (NULL); 13203 } 13204 13205 if (hdr.dofh_loadsz < sizeof (hdr)) { 13206 dtrace_dof_error(&hdr, "invalid load size"); 13207 *errp = EINVAL; 13208 return (NULL); 13209 } 13210 13211 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 13212 13213 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 13214 dof->dofh_loadsz != hdr.dofh_loadsz) { 13215 kmem_free(dof, hdr.dofh_loadsz); 13216 *errp = EFAULT; 13217 return (NULL); 13218 } 13219 13220 return (dof); 13221 } 13222 13223 #ifdef __FreeBSD__ 13224 static dof_hdr_t * 13225 dtrace_dof_copyin_proc(struct proc *p, uintptr_t uarg, int *errp) 13226 { 13227 dof_hdr_t hdr, *dof; 13228 struct thread *td; 13229 size_t loadsz; 13230 13231 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13232 13233 td = curthread; 13234 13235 /* 13236 * First, we're going to copyin() the sizeof (dof_hdr_t). 13237 */ 13238 if (proc_readmem(td, p, uarg, &hdr, sizeof(hdr)) != sizeof(hdr)) { 13239 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13240 *errp = EFAULT; 13241 return (NULL); 13242 } 13243 13244 /* 13245 * Now we'll allocate the entire DOF and copy it in -- provided 13246 * that the length isn't outrageous. 13247 */ 13248 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13249 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13250 *errp = E2BIG; 13251 return (NULL); 13252 } 13253 loadsz = (size_t)hdr.dofh_loadsz; 13254 13255 if (loadsz < sizeof (hdr)) { 13256 dtrace_dof_error(&hdr, "invalid load size"); 13257 *errp = EINVAL; 13258 return (NULL); 13259 } 13260 13261 dof = kmem_alloc(loadsz, KM_SLEEP); 13262 13263 if (proc_readmem(td, p, uarg, dof, loadsz) != loadsz || 13264 dof->dofh_loadsz != loadsz) { 13265 kmem_free(dof, hdr.dofh_loadsz); 13266 *errp = EFAULT; 13267 return (NULL); 13268 } 13269 13270 return (dof); 13271 } 13272 13273 static __inline uchar_t 13274 dtrace_dof_char(char c) 13275 { 13276 13277 switch (c) { 13278 case '0': 13279 case '1': 13280 case '2': 13281 case '3': 13282 case '4': 13283 case '5': 13284 case '6': 13285 case '7': 13286 case '8': 13287 case '9': 13288 return (c - '0'); 13289 case 'A': 13290 case 'B': 13291 case 'C': 13292 case 'D': 13293 case 'E': 13294 case 'F': 13295 return (c - 'A' + 10); 13296 case 'a': 13297 case 'b': 13298 case 'c': 13299 case 'd': 13300 case 'e': 13301 case 'f': 13302 return (c - 'a' + 10); 13303 } 13304 /* Should not reach here. */ 13305 return (UCHAR_MAX); 13306 } 13307 #endif /* __FreeBSD__ */ 13308 13309 static dof_hdr_t * 13310 dtrace_dof_property(const char *name) 13311 { 13312 #ifdef __FreeBSD__ 13313 uint8_t *dofbuf; 13314 u_char *data, *eol; 13315 caddr_t doffile; 13316 size_t bytes, len, i; 13317 dof_hdr_t *dof; 13318 u_char c1, c2; 13319 13320 dof = NULL; 13321 13322 doffile = preload_search_by_type("dtrace_dof"); 13323 if (doffile == NULL) 13324 return (NULL); 13325 13326 data = preload_fetch_addr(doffile); 13327 len = preload_fetch_size(doffile); 13328 for (;;) { 13329 /* Look for the end of the line. All lines end in a newline. */ 13330 eol = memchr(data, '\n', len); 13331 if (eol == NULL) 13332 return (NULL); 13333 13334 if (strncmp(name, data, strlen(name)) == 0) 13335 break; 13336 13337 eol++; /* skip past the newline */ 13338 len -= eol - data; 13339 data = eol; 13340 } 13341 13342 /* We've found the data corresponding to the specified key. */ 13343 13344 data += strlen(name) + 1; /* skip past the '=' */ 13345 len = eol - data; 13346 bytes = len / 2; 13347 13348 if (bytes < sizeof(dof_hdr_t)) { 13349 dtrace_dof_error(NULL, "truncated header"); 13350 goto doferr; 13351 } 13352 13353 /* 13354 * Each byte is represented by the two ASCII characters in its hex 13355 * representation. 13356 */ 13357 dofbuf = malloc(bytes, M_SOLARIS, M_WAITOK); 13358 for (i = 0; i < bytes; i++) { 13359 c1 = dtrace_dof_char(data[i * 2]); 13360 c2 = dtrace_dof_char(data[i * 2 + 1]); 13361 if (c1 == UCHAR_MAX || c2 == UCHAR_MAX) { 13362 dtrace_dof_error(NULL, "invalid hex char in DOF"); 13363 goto doferr; 13364 } 13365 dofbuf[i] = c1 * 16 + c2; 13366 } 13367 13368 dof = (dof_hdr_t *)dofbuf; 13369 if (bytes < dof->dofh_loadsz) { 13370 dtrace_dof_error(NULL, "truncated DOF"); 13371 goto doferr; 13372 } 13373 13374 if (dof->dofh_loadsz >= dtrace_dof_maxsize) { 13375 dtrace_dof_error(NULL, "oversized DOF"); 13376 goto doferr; 13377 } 13378 13379 return (dof); 13380 13381 doferr: 13382 free(dof, M_SOLARIS); 13383 return (NULL); 13384 #else /* __FreeBSD__ */ 13385 uchar_t *buf; 13386 uint64_t loadsz; 13387 unsigned int len, i; 13388 dof_hdr_t *dof; 13389 13390 /* 13391 * Unfortunately, array of values in .conf files are always (and 13392 * only) interpreted to be integer arrays. We must read our DOF 13393 * as an integer array, and then squeeze it into a byte array. 13394 */ 13395 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 13396 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 13397 return (NULL); 13398 13399 for (i = 0; i < len; i++) 13400 buf[i] = (uchar_t)(((int *)buf)[i]); 13401 13402 if (len < sizeof (dof_hdr_t)) { 13403 ddi_prop_free(buf); 13404 dtrace_dof_error(NULL, "truncated header"); 13405 return (NULL); 13406 } 13407 13408 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 13409 ddi_prop_free(buf); 13410 dtrace_dof_error(NULL, "truncated DOF"); 13411 return (NULL); 13412 } 13413 13414 if (loadsz >= dtrace_dof_maxsize) { 13415 ddi_prop_free(buf); 13416 dtrace_dof_error(NULL, "oversized DOF"); 13417 return (NULL); 13418 } 13419 13420 dof = kmem_alloc(loadsz, KM_SLEEP); 13421 bcopy(buf, dof, loadsz); 13422 ddi_prop_free(buf); 13423 13424 return (dof); 13425 #endif /* !__FreeBSD__ */ 13426 } 13427 13428 static void 13429 dtrace_dof_destroy(dof_hdr_t *dof) 13430 { 13431 kmem_free(dof, dof->dofh_loadsz); 13432 } 13433 13434 /* 13435 * Return the dof_sec_t pointer corresponding to a given section index. If the 13436 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 13437 * a type other than DOF_SECT_NONE is specified, the header is checked against 13438 * this type and NULL is returned if the types do not match. 13439 */ 13440 static dof_sec_t * 13441 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 13442 { 13443 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 13444 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 13445 13446 if (i >= dof->dofh_secnum) { 13447 dtrace_dof_error(dof, "referenced section index is invalid"); 13448 return (NULL); 13449 } 13450 13451 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 13452 dtrace_dof_error(dof, "referenced section is not loadable"); 13453 return (NULL); 13454 } 13455 13456 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 13457 dtrace_dof_error(dof, "referenced section is the wrong type"); 13458 return (NULL); 13459 } 13460 13461 return (sec); 13462 } 13463 13464 static dtrace_probedesc_t * 13465 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 13466 { 13467 dof_probedesc_t *probe; 13468 dof_sec_t *strtab; 13469 uintptr_t daddr = (uintptr_t)dof; 13470 uintptr_t str; 13471 size_t size; 13472 13473 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 13474 dtrace_dof_error(dof, "invalid probe section"); 13475 return (NULL); 13476 } 13477 13478 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13479 dtrace_dof_error(dof, "bad alignment in probe description"); 13480 return (NULL); 13481 } 13482 13483 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 13484 dtrace_dof_error(dof, "truncated probe description"); 13485 return (NULL); 13486 } 13487 13488 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 13489 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 13490 13491 if (strtab == NULL) 13492 return (NULL); 13493 13494 str = daddr + strtab->dofs_offset; 13495 size = strtab->dofs_size; 13496 13497 if (probe->dofp_provider >= strtab->dofs_size) { 13498 dtrace_dof_error(dof, "corrupt probe provider"); 13499 return (NULL); 13500 } 13501 13502 (void) strncpy(desc->dtpd_provider, 13503 (char *)(str + probe->dofp_provider), 13504 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 13505 13506 if (probe->dofp_mod >= strtab->dofs_size) { 13507 dtrace_dof_error(dof, "corrupt probe module"); 13508 return (NULL); 13509 } 13510 13511 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 13512 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 13513 13514 if (probe->dofp_func >= strtab->dofs_size) { 13515 dtrace_dof_error(dof, "corrupt probe function"); 13516 return (NULL); 13517 } 13518 13519 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 13520 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 13521 13522 if (probe->dofp_name >= strtab->dofs_size) { 13523 dtrace_dof_error(dof, "corrupt probe name"); 13524 return (NULL); 13525 } 13526 13527 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 13528 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 13529 13530 return (desc); 13531 } 13532 13533 static dtrace_difo_t * 13534 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13535 cred_t *cr) 13536 { 13537 dtrace_difo_t *dp; 13538 size_t ttl = 0; 13539 dof_difohdr_t *dofd; 13540 uintptr_t daddr = (uintptr_t)dof; 13541 size_t max = dtrace_difo_maxsize; 13542 int i, l, n; 13543 13544 static const struct { 13545 int section; 13546 int bufoffs; 13547 int lenoffs; 13548 int entsize; 13549 int align; 13550 const char *msg; 13551 } difo[] = { 13552 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 13553 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 13554 sizeof (dif_instr_t), "multiple DIF sections" }, 13555 13556 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 13557 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 13558 sizeof (uint64_t), "multiple integer tables" }, 13559 13560 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 13561 offsetof(dtrace_difo_t, dtdo_strlen), 0, 13562 sizeof (char), "multiple string tables" }, 13563 13564 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 13565 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 13566 sizeof (uint_t), "multiple variable tables" }, 13567 13568 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 13569 }; 13570 13571 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 13572 dtrace_dof_error(dof, "invalid DIFO header section"); 13573 return (NULL); 13574 } 13575 13576 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13577 dtrace_dof_error(dof, "bad alignment in DIFO header"); 13578 return (NULL); 13579 } 13580 13581 if (sec->dofs_size < sizeof (dof_difohdr_t) || 13582 sec->dofs_size % sizeof (dof_secidx_t)) { 13583 dtrace_dof_error(dof, "bad size in DIFO header"); 13584 return (NULL); 13585 } 13586 13587 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13588 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 13589 13590 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 13591 dp->dtdo_rtype = dofd->dofd_rtype; 13592 13593 for (l = 0; l < n; l++) { 13594 dof_sec_t *subsec; 13595 void **bufp; 13596 uint32_t *lenp; 13597 13598 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 13599 dofd->dofd_links[l])) == NULL) 13600 goto err; /* invalid section link */ 13601 13602 if (ttl + subsec->dofs_size > max) { 13603 dtrace_dof_error(dof, "exceeds maximum size"); 13604 goto err; 13605 } 13606 13607 ttl += subsec->dofs_size; 13608 13609 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 13610 if (subsec->dofs_type != difo[i].section) 13611 continue; 13612 13613 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 13614 dtrace_dof_error(dof, "section not loaded"); 13615 goto err; 13616 } 13617 13618 if (subsec->dofs_align != difo[i].align) { 13619 dtrace_dof_error(dof, "bad alignment"); 13620 goto err; 13621 } 13622 13623 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 13624 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 13625 13626 if (*bufp != NULL) { 13627 dtrace_dof_error(dof, difo[i].msg); 13628 goto err; 13629 } 13630 13631 if (difo[i].entsize != subsec->dofs_entsize) { 13632 dtrace_dof_error(dof, "entry size mismatch"); 13633 goto err; 13634 } 13635 13636 if (subsec->dofs_entsize != 0 && 13637 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 13638 dtrace_dof_error(dof, "corrupt entry size"); 13639 goto err; 13640 } 13641 13642 *lenp = subsec->dofs_size; 13643 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 13644 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 13645 *bufp, subsec->dofs_size); 13646 13647 if (subsec->dofs_entsize != 0) 13648 *lenp /= subsec->dofs_entsize; 13649 13650 break; 13651 } 13652 13653 /* 13654 * If we encounter a loadable DIFO sub-section that is not 13655 * known to us, assume this is a broken program and fail. 13656 */ 13657 if (difo[i].section == DOF_SECT_NONE && 13658 (subsec->dofs_flags & DOF_SECF_LOAD)) { 13659 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 13660 goto err; 13661 } 13662 } 13663 13664 if (dp->dtdo_buf == NULL) { 13665 /* 13666 * We can't have a DIF object without DIF text. 13667 */ 13668 dtrace_dof_error(dof, "missing DIF text"); 13669 goto err; 13670 } 13671 13672 /* 13673 * Before we validate the DIF object, run through the variable table 13674 * looking for the strings -- if any of their size are under, we'll set 13675 * their size to be the system-wide default string size. Note that 13676 * this should _not_ happen if the "strsize" option has been set -- 13677 * in this case, the compiler should have set the size to reflect the 13678 * setting of the option. 13679 */ 13680 for (i = 0; i < dp->dtdo_varlen; i++) { 13681 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 13682 dtrace_diftype_t *t = &v->dtdv_type; 13683 13684 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 13685 continue; 13686 13687 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 13688 t->dtdt_size = dtrace_strsize_default; 13689 } 13690 13691 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 13692 goto err; 13693 13694 dtrace_difo_init(dp, vstate); 13695 return (dp); 13696 13697 err: 13698 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 13699 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 13700 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 13701 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 13702 13703 kmem_free(dp, sizeof (dtrace_difo_t)); 13704 return (NULL); 13705 } 13706 13707 static dtrace_predicate_t * 13708 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13709 cred_t *cr) 13710 { 13711 dtrace_difo_t *dp; 13712 13713 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 13714 return (NULL); 13715 13716 return (dtrace_predicate_create(dp)); 13717 } 13718 13719 static dtrace_actdesc_t * 13720 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13721 cred_t *cr) 13722 { 13723 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 13724 dof_actdesc_t *desc; 13725 dof_sec_t *difosec; 13726 size_t offs; 13727 uintptr_t daddr = (uintptr_t)dof; 13728 uint64_t arg; 13729 dtrace_actkind_t kind; 13730 13731 if (sec->dofs_type != DOF_SECT_ACTDESC) { 13732 dtrace_dof_error(dof, "invalid action section"); 13733 return (NULL); 13734 } 13735 13736 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 13737 dtrace_dof_error(dof, "truncated action description"); 13738 return (NULL); 13739 } 13740 13741 if (sec->dofs_align != sizeof (uint64_t)) { 13742 dtrace_dof_error(dof, "bad alignment in action description"); 13743 return (NULL); 13744 } 13745 13746 if (sec->dofs_size < sec->dofs_entsize) { 13747 dtrace_dof_error(dof, "section entry size exceeds total size"); 13748 return (NULL); 13749 } 13750 13751 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 13752 dtrace_dof_error(dof, "bad entry size in action description"); 13753 return (NULL); 13754 } 13755 13756 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 13757 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 13758 return (NULL); 13759 } 13760 13761 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 13762 desc = (dof_actdesc_t *)(daddr + 13763 (uintptr_t)sec->dofs_offset + offs); 13764 kind = (dtrace_actkind_t)desc->dofa_kind; 13765 13766 if ((DTRACEACT_ISPRINTFLIKE(kind) && 13767 (kind != DTRACEACT_PRINTA || 13768 desc->dofa_strtab != DOF_SECIDX_NONE)) || 13769 (kind == DTRACEACT_DIFEXPR && 13770 desc->dofa_strtab != DOF_SECIDX_NONE)) { 13771 dof_sec_t *strtab; 13772 char *str, *fmt; 13773 uint64_t i; 13774 13775 /* 13776 * The argument to these actions is an index into the 13777 * DOF string table. For printf()-like actions, this 13778 * is the format string. For print(), this is the 13779 * CTF type of the expression result. 13780 */ 13781 if ((strtab = dtrace_dof_sect(dof, 13782 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 13783 goto err; 13784 13785 str = (char *)((uintptr_t)dof + 13786 (uintptr_t)strtab->dofs_offset); 13787 13788 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 13789 if (str[i] == '\0') 13790 break; 13791 } 13792 13793 if (i >= strtab->dofs_size) { 13794 dtrace_dof_error(dof, "bogus format string"); 13795 goto err; 13796 } 13797 13798 if (i == desc->dofa_arg) { 13799 dtrace_dof_error(dof, "empty format string"); 13800 goto err; 13801 } 13802 13803 i -= desc->dofa_arg; 13804 fmt = kmem_alloc(i + 1, KM_SLEEP); 13805 bcopy(&str[desc->dofa_arg], fmt, i + 1); 13806 arg = (uint64_t)(uintptr_t)fmt; 13807 } else { 13808 if (kind == DTRACEACT_PRINTA) { 13809 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 13810 arg = 0; 13811 } else { 13812 arg = desc->dofa_arg; 13813 } 13814 } 13815 13816 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 13817 desc->dofa_uarg, arg); 13818 13819 if (last != NULL) { 13820 last->dtad_next = act; 13821 } else { 13822 first = act; 13823 } 13824 13825 last = act; 13826 13827 if (desc->dofa_difo == DOF_SECIDX_NONE) 13828 continue; 13829 13830 if ((difosec = dtrace_dof_sect(dof, 13831 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 13832 goto err; 13833 13834 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 13835 13836 if (act->dtad_difo == NULL) 13837 goto err; 13838 } 13839 13840 ASSERT(first != NULL); 13841 return (first); 13842 13843 err: 13844 for (act = first; act != NULL; act = next) { 13845 next = act->dtad_next; 13846 dtrace_actdesc_release(act, vstate); 13847 } 13848 13849 return (NULL); 13850 } 13851 13852 static dtrace_ecbdesc_t * 13853 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13854 cred_t *cr) 13855 { 13856 dtrace_ecbdesc_t *ep; 13857 dof_ecbdesc_t *ecb; 13858 dtrace_probedesc_t *desc; 13859 dtrace_predicate_t *pred = NULL; 13860 13861 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 13862 dtrace_dof_error(dof, "truncated ECB description"); 13863 return (NULL); 13864 } 13865 13866 if (sec->dofs_align != sizeof (uint64_t)) { 13867 dtrace_dof_error(dof, "bad alignment in ECB description"); 13868 return (NULL); 13869 } 13870 13871 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 13872 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 13873 13874 if (sec == NULL) 13875 return (NULL); 13876 13877 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 13878 ep->dted_uarg = ecb->dofe_uarg; 13879 desc = &ep->dted_probe; 13880 13881 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 13882 goto err; 13883 13884 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 13885 if ((sec = dtrace_dof_sect(dof, 13886 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 13887 goto err; 13888 13889 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 13890 goto err; 13891 13892 ep->dted_pred.dtpdd_predicate = pred; 13893 } 13894 13895 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 13896 if ((sec = dtrace_dof_sect(dof, 13897 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 13898 goto err; 13899 13900 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 13901 13902 if (ep->dted_action == NULL) 13903 goto err; 13904 } 13905 13906 return (ep); 13907 13908 err: 13909 if (pred != NULL) 13910 dtrace_predicate_release(pred, vstate); 13911 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 13912 return (NULL); 13913 } 13914 13915 /* 13916 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 13917 * specified DOF. At present, this amounts to simply adding 'ubase' to the 13918 * site of any user SETX relocations to account for load object base address. 13919 * In the future, if we need other relocations, this function can be extended. 13920 */ 13921 static int 13922 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 13923 { 13924 uintptr_t daddr = (uintptr_t)dof; 13925 dof_relohdr_t *dofr = 13926 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13927 dof_sec_t *ss, *rs, *ts; 13928 dof_relodesc_t *r; 13929 uint_t i, n; 13930 13931 if (sec->dofs_size < sizeof (dof_relohdr_t) || 13932 sec->dofs_align != sizeof (dof_secidx_t)) { 13933 dtrace_dof_error(dof, "invalid relocation header"); 13934 return (-1); 13935 } 13936 13937 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 13938 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 13939 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 13940 13941 if (ss == NULL || rs == NULL || ts == NULL) 13942 return (-1); /* dtrace_dof_error() has been called already */ 13943 13944 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 13945 rs->dofs_align != sizeof (uint64_t)) { 13946 dtrace_dof_error(dof, "invalid relocation section"); 13947 return (-1); 13948 } 13949 13950 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 13951 n = rs->dofs_size / rs->dofs_entsize; 13952 13953 for (i = 0; i < n; i++) { 13954 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 13955 13956 switch (r->dofr_type) { 13957 case DOF_RELO_NONE: 13958 break; 13959 case DOF_RELO_SETX: 13960 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 13961 sizeof (uint64_t) > ts->dofs_size) { 13962 dtrace_dof_error(dof, "bad relocation offset"); 13963 return (-1); 13964 } 13965 13966 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 13967 dtrace_dof_error(dof, "misaligned setx relo"); 13968 return (-1); 13969 } 13970 13971 *(uint64_t *)taddr += ubase; 13972 break; 13973 default: 13974 dtrace_dof_error(dof, "invalid relocation type"); 13975 return (-1); 13976 } 13977 13978 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 13979 } 13980 13981 return (0); 13982 } 13983 13984 /* 13985 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 13986 * header: it should be at the front of a memory region that is at least 13987 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 13988 * size. It need not be validated in any other way. 13989 */ 13990 static int 13991 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 13992 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 13993 { 13994 uint64_t len = dof->dofh_loadsz, seclen; 13995 uintptr_t daddr = (uintptr_t)dof; 13996 dtrace_ecbdesc_t *ep; 13997 dtrace_enabling_t *enab; 13998 uint_t i; 13999 14000 ASSERT(MUTEX_HELD(&dtrace_lock)); 14001 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 14002 14003 /* 14004 * Check the DOF header identification bytes. In addition to checking 14005 * valid settings, we also verify that unused bits/bytes are zeroed so 14006 * we can use them later without fear of regressing existing binaries. 14007 */ 14008 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 14009 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 14010 dtrace_dof_error(dof, "DOF magic string mismatch"); 14011 return (-1); 14012 } 14013 14014 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 14015 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 14016 dtrace_dof_error(dof, "DOF has invalid data model"); 14017 return (-1); 14018 } 14019 14020 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 14021 dtrace_dof_error(dof, "DOF encoding mismatch"); 14022 return (-1); 14023 } 14024 14025 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14026 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 14027 dtrace_dof_error(dof, "DOF version mismatch"); 14028 return (-1); 14029 } 14030 14031 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 14032 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 14033 return (-1); 14034 } 14035 14036 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 14037 dtrace_dof_error(dof, "DOF uses too many integer registers"); 14038 return (-1); 14039 } 14040 14041 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 14042 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 14043 return (-1); 14044 } 14045 14046 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 14047 if (dof->dofh_ident[i] != 0) { 14048 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 14049 return (-1); 14050 } 14051 } 14052 14053 if (dof->dofh_flags & ~DOF_FL_VALID) { 14054 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 14055 return (-1); 14056 } 14057 14058 if (dof->dofh_secsize == 0) { 14059 dtrace_dof_error(dof, "zero section header size"); 14060 return (-1); 14061 } 14062 14063 /* 14064 * Check that the section headers don't exceed the amount of DOF 14065 * data. Note that we cast the section size and number of sections 14066 * to uint64_t's to prevent possible overflow in the multiplication. 14067 */ 14068 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 14069 14070 if (dof->dofh_secoff > len || seclen > len || 14071 dof->dofh_secoff + seclen > len) { 14072 dtrace_dof_error(dof, "truncated section headers"); 14073 return (-1); 14074 } 14075 14076 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 14077 dtrace_dof_error(dof, "misaligned section headers"); 14078 return (-1); 14079 } 14080 14081 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 14082 dtrace_dof_error(dof, "misaligned section size"); 14083 return (-1); 14084 } 14085 14086 /* 14087 * Take an initial pass through the section headers to be sure that 14088 * the headers don't have stray offsets. If the 'noprobes' flag is 14089 * set, do not permit sections relating to providers, probes, or args. 14090 */ 14091 for (i = 0; i < dof->dofh_secnum; i++) { 14092 dof_sec_t *sec = (dof_sec_t *)(daddr + 14093 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14094 14095 if (noprobes) { 14096 switch (sec->dofs_type) { 14097 case DOF_SECT_PROVIDER: 14098 case DOF_SECT_PROBES: 14099 case DOF_SECT_PRARGS: 14100 case DOF_SECT_PROFFS: 14101 dtrace_dof_error(dof, "illegal sections " 14102 "for enabling"); 14103 return (-1); 14104 } 14105 } 14106 14107 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 14108 !(sec->dofs_flags & DOF_SECF_LOAD)) { 14109 dtrace_dof_error(dof, "loadable section with load " 14110 "flag unset"); 14111 return (-1); 14112 } 14113 14114 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 14115 continue; /* just ignore non-loadable sections */ 14116 14117 if (!ISP2(sec->dofs_align)) { 14118 dtrace_dof_error(dof, "bad section alignment"); 14119 return (-1); 14120 } 14121 14122 if (sec->dofs_offset & (sec->dofs_align - 1)) { 14123 dtrace_dof_error(dof, "misaligned section"); 14124 return (-1); 14125 } 14126 14127 if (sec->dofs_offset > len || sec->dofs_size > len || 14128 sec->dofs_offset + sec->dofs_size > len) { 14129 dtrace_dof_error(dof, "corrupt section header"); 14130 return (-1); 14131 } 14132 14133 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 14134 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 14135 dtrace_dof_error(dof, "non-terminating string table"); 14136 return (-1); 14137 } 14138 } 14139 14140 /* 14141 * Take a second pass through the sections and locate and perform any 14142 * relocations that are present. We do this after the first pass to 14143 * be sure that all sections have had their headers validated. 14144 */ 14145 for (i = 0; i < dof->dofh_secnum; i++) { 14146 dof_sec_t *sec = (dof_sec_t *)(daddr + 14147 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14148 14149 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 14150 continue; /* skip sections that are not loadable */ 14151 14152 switch (sec->dofs_type) { 14153 case DOF_SECT_URELHDR: 14154 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 14155 return (-1); 14156 break; 14157 } 14158 } 14159 14160 if ((enab = *enabp) == NULL) 14161 enab = *enabp = dtrace_enabling_create(vstate); 14162 14163 for (i = 0; i < dof->dofh_secnum; i++) { 14164 dof_sec_t *sec = (dof_sec_t *)(daddr + 14165 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14166 14167 if (sec->dofs_type != DOF_SECT_ECBDESC) 14168 continue; 14169 14170 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 14171 dtrace_enabling_destroy(enab); 14172 *enabp = NULL; 14173 return (-1); 14174 } 14175 14176 dtrace_enabling_add(enab, ep); 14177 } 14178 14179 return (0); 14180 } 14181 14182 /* 14183 * Process DOF for any options. This routine assumes that the DOF has been 14184 * at least processed by dtrace_dof_slurp(). 14185 */ 14186 static int 14187 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 14188 { 14189 int i, rval; 14190 uint32_t entsize; 14191 size_t offs; 14192 dof_optdesc_t *desc; 14193 14194 for (i = 0; i < dof->dofh_secnum; i++) { 14195 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 14196 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14197 14198 if (sec->dofs_type != DOF_SECT_OPTDESC) 14199 continue; 14200 14201 if (sec->dofs_align != sizeof (uint64_t)) { 14202 dtrace_dof_error(dof, "bad alignment in " 14203 "option description"); 14204 return (EINVAL); 14205 } 14206 14207 if ((entsize = sec->dofs_entsize) == 0) { 14208 dtrace_dof_error(dof, "zeroed option entry size"); 14209 return (EINVAL); 14210 } 14211 14212 if (entsize < sizeof (dof_optdesc_t)) { 14213 dtrace_dof_error(dof, "bad option entry size"); 14214 return (EINVAL); 14215 } 14216 14217 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 14218 desc = (dof_optdesc_t *)((uintptr_t)dof + 14219 (uintptr_t)sec->dofs_offset + offs); 14220 14221 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 14222 dtrace_dof_error(dof, "non-zero option string"); 14223 return (EINVAL); 14224 } 14225 14226 if (desc->dofo_value == DTRACEOPT_UNSET) { 14227 dtrace_dof_error(dof, "unset option"); 14228 return (EINVAL); 14229 } 14230 14231 if ((rval = dtrace_state_option(state, 14232 desc->dofo_option, desc->dofo_value)) != 0) { 14233 dtrace_dof_error(dof, "rejected option"); 14234 return (rval); 14235 } 14236 } 14237 } 14238 14239 return (0); 14240 } 14241 14242 /* 14243 * DTrace Consumer State Functions 14244 */ 14245 static int 14246 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 14247 { 14248 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 14249 void *base; 14250 uintptr_t limit; 14251 dtrace_dynvar_t *dvar, *next, *start; 14252 int i; 14253 14254 ASSERT(MUTEX_HELD(&dtrace_lock)); 14255 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 14256 14257 bzero(dstate, sizeof (dtrace_dstate_t)); 14258 14259 if ((dstate->dtds_chunksize = chunksize) == 0) 14260 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 14261 14262 VERIFY(dstate->dtds_chunksize < LONG_MAX); 14263 14264 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 14265 size = min; 14266 14267 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 14268 return (ENOMEM); 14269 14270 dstate->dtds_size = size; 14271 dstate->dtds_base = base; 14272 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 14273 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 14274 14275 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 14276 14277 if (hashsize != 1 && (hashsize & 1)) 14278 hashsize--; 14279 14280 dstate->dtds_hashsize = hashsize; 14281 dstate->dtds_hash = dstate->dtds_base; 14282 14283 /* 14284 * Set all of our hash buckets to point to the single sink, and (if 14285 * it hasn't already been set), set the sink's hash value to be the 14286 * sink sentinel value. The sink is needed for dynamic variable 14287 * lookups to know that they have iterated over an entire, valid hash 14288 * chain. 14289 */ 14290 for (i = 0; i < hashsize; i++) 14291 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 14292 14293 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 14294 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 14295 14296 /* 14297 * Determine number of active CPUs. Divide free list evenly among 14298 * active CPUs. 14299 */ 14300 start = (dtrace_dynvar_t *) 14301 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 14302 limit = (uintptr_t)base + size; 14303 14304 VERIFY((uintptr_t)start < limit); 14305 VERIFY((uintptr_t)start >= (uintptr_t)base); 14306 14307 maxper = (limit - (uintptr_t)start) / NCPU; 14308 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 14309 14310 #ifndef illumos 14311 CPU_FOREACH(i) { 14312 #else 14313 for (i = 0; i < NCPU; i++) { 14314 #endif 14315 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 14316 14317 /* 14318 * If we don't even have enough chunks to make it once through 14319 * NCPUs, we're just going to allocate everything to the first 14320 * CPU. And if we're on the last CPU, we're going to allocate 14321 * whatever is left over. In either case, we set the limit to 14322 * be the limit of the dynamic variable space. 14323 */ 14324 if (maxper == 0 || i == NCPU - 1) { 14325 limit = (uintptr_t)base + size; 14326 start = NULL; 14327 } else { 14328 limit = (uintptr_t)start + maxper; 14329 start = (dtrace_dynvar_t *)limit; 14330 } 14331 14332 VERIFY(limit <= (uintptr_t)base + size); 14333 14334 for (;;) { 14335 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 14336 dstate->dtds_chunksize); 14337 14338 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 14339 break; 14340 14341 VERIFY((uintptr_t)dvar >= (uintptr_t)base && 14342 (uintptr_t)dvar <= (uintptr_t)base + size); 14343 dvar->dtdv_next = next; 14344 dvar = next; 14345 } 14346 14347 if (maxper == 0) 14348 break; 14349 } 14350 14351 return (0); 14352 } 14353 14354 static void 14355 dtrace_dstate_fini(dtrace_dstate_t *dstate) 14356 { 14357 ASSERT(MUTEX_HELD(&cpu_lock)); 14358 14359 if (dstate->dtds_base == NULL) 14360 return; 14361 14362 kmem_free(dstate->dtds_base, dstate->dtds_size); 14363 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 14364 } 14365 14366 static void 14367 dtrace_vstate_fini(dtrace_vstate_t *vstate) 14368 { 14369 /* 14370 * Logical XOR, where are you? 14371 */ 14372 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 14373 14374 if (vstate->dtvs_nglobals > 0) { 14375 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 14376 sizeof (dtrace_statvar_t *)); 14377 } 14378 14379 if (vstate->dtvs_ntlocals > 0) { 14380 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 14381 sizeof (dtrace_difv_t)); 14382 } 14383 14384 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 14385 14386 if (vstate->dtvs_nlocals > 0) { 14387 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 14388 sizeof (dtrace_statvar_t *)); 14389 } 14390 } 14391 14392 #ifdef illumos 14393 static void 14394 dtrace_state_clean(dtrace_state_t *state) 14395 { 14396 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14397 return; 14398 14399 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14400 dtrace_speculation_clean(state); 14401 } 14402 14403 static void 14404 dtrace_state_deadman(dtrace_state_t *state) 14405 { 14406 hrtime_t now; 14407 14408 dtrace_sync(); 14409 14410 now = dtrace_gethrtime(); 14411 14412 if (state != dtrace_anon.dta_state && 14413 now - state->dts_laststatus >= dtrace_deadman_user) 14414 return; 14415 14416 /* 14417 * We must be sure that dts_alive never appears to be less than the 14418 * value upon entry to dtrace_state_deadman(), and because we lack a 14419 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14420 * store INT64_MAX to it, followed by a memory barrier, followed by 14421 * the new value. This assures that dts_alive never appears to be 14422 * less than its true value, regardless of the order in which the 14423 * stores to the underlying storage are issued. 14424 */ 14425 state->dts_alive = INT64_MAX; 14426 dtrace_membar_producer(); 14427 state->dts_alive = now; 14428 } 14429 #else /* !illumos */ 14430 static void 14431 dtrace_state_clean(void *arg) 14432 { 14433 dtrace_state_t *state = arg; 14434 dtrace_optval_t *opt = state->dts_options; 14435 14436 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14437 return; 14438 14439 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14440 dtrace_speculation_clean(state); 14441 14442 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14443 dtrace_state_clean, state); 14444 } 14445 14446 static void 14447 dtrace_state_deadman(void *arg) 14448 { 14449 dtrace_state_t *state = arg; 14450 hrtime_t now; 14451 14452 dtrace_sync(); 14453 14454 dtrace_debug_output(); 14455 14456 now = dtrace_gethrtime(); 14457 14458 if (state != dtrace_anon.dta_state && 14459 now - state->dts_laststatus >= dtrace_deadman_user) 14460 return; 14461 14462 /* 14463 * We must be sure that dts_alive never appears to be less than the 14464 * value upon entry to dtrace_state_deadman(), and because we lack a 14465 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14466 * store INT64_MAX to it, followed by a memory barrier, followed by 14467 * the new value. This assures that dts_alive never appears to be 14468 * less than its true value, regardless of the order in which the 14469 * stores to the underlying storage are issued. 14470 */ 14471 state->dts_alive = INT64_MAX; 14472 dtrace_membar_producer(); 14473 state->dts_alive = now; 14474 14475 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14476 dtrace_state_deadman, state); 14477 } 14478 #endif /* illumos */ 14479 14480 static dtrace_state_t * 14481 #ifdef illumos 14482 dtrace_state_create(dev_t *devp, cred_t *cr) 14483 #else 14484 dtrace_state_create(struct cdev *dev, struct ucred *cred __unused) 14485 #endif 14486 { 14487 #ifdef illumos 14488 minor_t minor; 14489 major_t major; 14490 #else 14491 cred_t *cr = NULL; 14492 int m = 0; 14493 #endif 14494 char c[30]; 14495 dtrace_state_t *state; 14496 dtrace_optval_t *opt; 14497 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 14498 14499 ASSERT(MUTEX_HELD(&dtrace_lock)); 14500 ASSERT(MUTEX_HELD(&cpu_lock)); 14501 14502 #ifdef illumos 14503 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 14504 VM_BESTFIT | VM_SLEEP); 14505 14506 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 14507 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14508 return (NULL); 14509 } 14510 14511 state = ddi_get_soft_state(dtrace_softstate, minor); 14512 #else 14513 if (dev != NULL) { 14514 cr = dev->si_cred; 14515 m = dev2unit(dev); 14516 } 14517 14518 /* Allocate memory for the state. */ 14519 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 14520 #endif 14521 14522 state->dts_epid = DTRACE_EPIDNONE + 1; 14523 14524 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 14525 #ifdef illumos 14526 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 14527 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14528 14529 if (devp != NULL) { 14530 major = getemajor(*devp); 14531 } else { 14532 major = ddi_driver_major(dtrace_devi); 14533 } 14534 14535 state->dts_dev = makedevice(major, minor); 14536 14537 if (devp != NULL) 14538 *devp = state->dts_dev; 14539 #else 14540 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 14541 state->dts_dev = dev; 14542 #endif 14543 14544 /* 14545 * We allocate NCPU buffers. On the one hand, this can be quite 14546 * a bit of memory per instance (nearly 36K on a Starcat). On the 14547 * other hand, it saves an additional memory reference in the probe 14548 * path. 14549 */ 14550 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 14551 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 14552 14553 #ifdef illumos 14554 state->dts_cleaner = CYCLIC_NONE; 14555 state->dts_deadman = CYCLIC_NONE; 14556 #else 14557 callout_init(&state->dts_cleaner, 1); 14558 callout_init(&state->dts_deadman, 1); 14559 #endif 14560 state->dts_vstate.dtvs_state = state; 14561 14562 for (i = 0; i < DTRACEOPT_MAX; i++) 14563 state->dts_options[i] = DTRACEOPT_UNSET; 14564 14565 /* 14566 * Set the default options. 14567 */ 14568 opt = state->dts_options; 14569 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 14570 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 14571 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 14572 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 14573 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 14574 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 14575 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 14576 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 14577 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 14578 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 14579 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 14580 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 14581 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 14582 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 14583 14584 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 14585 14586 /* 14587 * Depending on the user credentials, we set flag bits which alter probe 14588 * visibility or the amount of destructiveness allowed. In the case of 14589 * actual anonymous tracing, or the possession of all privileges, all of 14590 * the normal checks are bypassed. 14591 */ 14592 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 14593 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 14594 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 14595 } else { 14596 /* 14597 * Set up the credentials for this instantiation. We take a 14598 * hold on the credential to prevent it from disappearing on 14599 * us; this in turn prevents the zone_t referenced by this 14600 * credential from disappearing. This means that we can 14601 * examine the credential and the zone from probe context. 14602 */ 14603 crhold(cr); 14604 state->dts_cred.dcr_cred = cr; 14605 14606 /* 14607 * CRA_PROC means "we have *some* privilege for dtrace" and 14608 * unlocks the use of variables like pid, zonename, etc. 14609 */ 14610 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 14611 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14612 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 14613 } 14614 14615 /* 14616 * dtrace_user allows use of syscall and profile providers. 14617 * If the user also has proc_owner and/or proc_zone, we 14618 * extend the scope to include additional visibility and 14619 * destructive power. 14620 */ 14621 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 14622 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 14623 state->dts_cred.dcr_visible |= 14624 DTRACE_CRV_ALLPROC; 14625 14626 state->dts_cred.dcr_action |= 14627 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14628 } 14629 14630 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 14631 state->dts_cred.dcr_visible |= 14632 DTRACE_CRV_ALLZONE; 14633 14634 state->dts_cred.dcr_action |= 14635 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14636 } 14637 14638 /* 14639 * If we have all privs in whatever zone this is, 14640 * we can do destructive things to processes which 14641 * have altered credentials. 14642 */ 14643 #ifdef illumos 14644 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14645 cr->cr_zone->zone_privset)) { 14646 state->dts_cred.dcr_action |= 14647 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14648 } 14649 #endif 14650 } 14651 14652 /* 14653 * Holding the dtrace_kernel privilege also implies that 14654 * the user has the dtrace_user privilege from a visibility 14655 * perspective. But without further privileges, some 14656 * destructive actions are not available. 14657 */ 14658 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 14659 /* 14660 * Make all probes in all zones visible. However, 14661 * this doesn't mean that all actions become available 14662 * to all zones. 14663 */ 14664 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 14665 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 14666 14667 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 14668 DTRACE_CRA_PROC; 14669 /* 14670 * Holding proc_owner means that destructive actions 14671 * for *this* zone are allowed. 14672 */ 14673 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14674 state->dts_cred.dcr_action |= 14675 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14676 14677 /* 14678 * Holding proc_zone means that destructive actions 14679 * for this user/group ID in all zones is allowed. 14680 */ 14681 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14682 state->dts_cred.dcr_action |= 14683 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14684 14685 #ifdef illumos 14686 /* 14687 * If we have all privs in whatever zone this is, 14688 * we can do destructive things to processes which 14689 * have altered credentials. 14690 */ 14691 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14692 cr->cr_zone->zone_privset)) { 14693 state->dts_cred.dcr_action |= 14694 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14695 } 14696 #endif 14697 } 14698 14699 /* 14700 * Holding the dtrace_proc privilege gives control over fasttrap 14701 * and pid providers. We need to grant wider destructive 14702 * privileges in the event that the user has proc_owner and/or 14703 * proc_zone. 14704 */ 14705 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14706 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14707 state->dts_cred.dcr_action |= 14708 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14709 14710 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14711 state->dts_cred.dcr_action |= 14712 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14713 } 14714 } 14715 14716 return (state); 14717 } 14718 14719 static int 14720 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 14721 { 14722 dtrace_optval_t *opt = state->dts_options, size; 14723 processorid_t cpu = 0;; 14724 int flags = 0, rval, factor, divisor = 1; 14725 14726 ASSERT(MUTEX_HELD(&dtrace_lock)); 14727 ASSERT(MUTEX_HELD(&cpu_lock)); 14728 ASSERT(which < DTRACEOPT_MAX); 14729 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 14730 (state == dtrace_anon.dta_state && 14731 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 14732 14733 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 14734 return (0); 14735 14736 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 14737 cpu = opt[DTRACEOPT_CPU]; 14738 14739 if (which == DTRACEOPT_SPECSIZE) 14740 flags |= DTRACEBUF_NOSWITCH; 14741 14742 if (which == DTRACEOPT_BUFSIZE) { 14743 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 14744 flags |= DTRACEBUF_RING; 14745 14746 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 14747 flags |= DTRACEBUF_FILL; 14748 14749 if (state != dtrace_anon.dta_state || 14750 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14751 flags |= DTRACEBUF_INACTIVE; 14752 } 14753 14754 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 14755 /* 14756 * The size must be 8-byte aligned. If the size is not 8-byte 14757 * aligned, drop it down by the difference. 14758 */ 14759 if (size & (sizeof (uint64_t) - 1)) 14760 size -= size & (sizeof (uint64_t) - 1); 14761 14762 if (size < state->dts_reserve) { 14763 /* 14764 * Buffers always must be large enough to accommodate 14765 * their prereserved space. We return E2BIG instead 14766 * of ENOMEM in this case to allow for user-level 14767 * software to differentiate the cases. 14768 */ 14769 return (E2BIG); 14770 } 14771 14772 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 14773 14774 if (rval != ENOMEM) { 14775 opt[which] = size; 14776 return (rval); 14777 } 14778 14779 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14780 return (rval); 14781 14782 for (divisor = 2; divisor < factor; divisor <<= 1) 14783 continue; 14784 } 14785 14786 return (ENOMEM); 14787 } 14788 14789 static int 14790 dtrace_state_buffers(dtrace_state_t *state) 14791 { 14792 dtrace_speculation_t *spec = state->dts_speculations; 14793 int rval, i; 14794 14795 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 14796 DTRACEOPT_BUFSIZE)) != 0) 14797 return (rval); 14798 14799 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 14800 DTRACEOPT_AGGSIZE)) != 0) 14801 return (rval); 14802 14803 for (i = 0; i < state->dts_nspeculations; i++) { 14804 if ((rval = dtrace_state_buffer(state, 14805 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 14806 return (rval); 14807 } 14808 14809 return (0); 14810 } 14811 14812 static void 14813 dtrace_state_prereserve(dtrace_state_t *state) 14814 { 14815 dtrace_ecb_t *ecb; 14816 dtrace_probe_t *probe; 14817 14818 state->dts_reserve = 0; 14819 14820 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 14821 return; 14822 14823 /* 14824 * If our buffer policy is a "fill" buffer policy, we need to set the 14825 * prereserved space to be the space required by the END probes. 14826 */ 14827 probe = dtrace_probes[dtrace_probeid_end - 1]; 14828 ASSERT(probe != NULL); 14829 14830 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 14831 if (ecb->dte_state != state) 14832 continue; 14833 14834 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 14835 } 14836 } 14837 14838 static int 14839 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 14840 { 14841 dtrace_optval_t *opt = state->dts_options, sz, nspec; 14842 dtrace_speculation_t *spec; 14843 dtrace_buffer_t *buf; 14844 #ifdef illumos 14845 cyc_handler_t hdlr; 14846 cyc_time_t when; 14847 #endif 14848 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 14849 dtrace_icookie_t cookie; 14850 14851 mutex_enter(&cpu_lock); 14852 mutex_enter(&dtrace_lock); 14853 14854 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 14855 rval = EBUSY; 14856 goto out; 14857 } 14858 14859 /* 14860 * Before we can perform any checks, we must prime all of the 14861 * retained enablings that correspond to this state. 14862 */ 14863 dtrace_enabling_prime(state); 14864 14865 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 14866 rval = EACCES; 14867 goto out; 14868 } 14869 14870 dtrace_state_prereserve(state); 14871 14872 /* 14873 * Now we want to do is try to allocate our speculations. 14874 * We do not automatically resize the number of speculations; if 14875 * this fails, we will fail the operation. 14876 */ 14877 nspec = opt[DTRACEOPT_NSPEC]; 14878 ASSERT(nspec != DTRACEOPT_UNSET); 14879 14880 if (nspec > INT_MAX) { 14881 rval = ENOMEM; 14882 goto out; 14883 } 14884 14885 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 14886 KM_NOSLEEP | KM_NORMALPRI); 14887 14888 if (spec == NULL) { 14889 rval = ENOMEM; 14890 goto out; 14891 } 14892 14893 state->dts_speculations = spec; 14894 state->dts_nspeculations = (int)nspec; 14895 14896 for (i = 0; i < nspec; i++) { 14897 if ((buf = kmem_zalloc(bufsize, 14898 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 14899 rval = ENOMEM; 14900 goto err; 14901 } 14902 14903 spec[i].dtsp_buffer = buf; 14904 } 14905 14906 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 14907 if (dtrace_anon.dta_state == NULL) { 14908 rval = ENOENT; 14909 goto out; 14910 } 14911 14912 if (state->dts_necbs != 0) { 14913 rval = EALREADY; 14914 goto out; 14915 } 14916 14917 state->dts_anon = dtrace_anon_grab(); 14918 ASSERT(state->dts_anon != NULL); 14919 state = state->dts_anon; 14920 14921 /* 14922 * We want "grabanon" to be set in the grabbed state, so we'll 14923 * copy that option value from the grabbing state into the 14924 * grabbed state. 14925 */ 14926 state->dts_options[DTRACEOPT_GRABANON] = 14927 opt[DTRACEOPT_GRABANON]; 14928 14929 *cpu = dtrace_anon.dta_beganon; 14930 14931 /* 14932 * If the anonymous state is active (as it almost certainly 14933 * is if the anonymous enabling ultimately matched anything), 14934 * we don't allow any further option processing -- but we 14935 * don't return failure. 14936 */ 14937 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 14938 goto out; 14939 } 14940 14941 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 14942 opt[DTRACEOPT_AGGSIZE] != 0) { 14943 if (state->dts_aggregations == NULL) { 14944 /* 14945 * We're not going to create an aggregation buffer 14946 * because we don't have any ECBs that contain 14947 * aggregations -- set this option to 0. 14948 */ 14949 opt[DTRACEOPT_AGGSIZE] = 0; 14950 } else { 14951 /* 14952 * If we have an aggregation buffer, we must also have 14953 * a buffer to use as scratch. 14954 */ 14955 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 14956 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 14957 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 14958 } 14959 } 14960 } 14961 14962 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 14963 opt[DTRACEOPT_SPECSIZE] != 0) { 14964 if (!state->dts_speculates) { 14965 /* 14966 * We're not going to create speculation buffers 14967 * because we don't have any ECBs that actually 14968 * speculate -- set the speculation size to 0. 14969 */ 14970 opt[DTRACEOPT_SPECSIZE] = 0; 14971 } 14972 } 14973 14974 /* 14975 * The bare minimum size for any buffer that we're actually going to 14976 * do anything to is sizeof (uint64_t). 14977 */ 14978 sz = sizeof (uint64_t); 14979 14980 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 14981 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 14982 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 14983 /* 14984 * A buffer size has been explicitly set to 0 (or to a size 14985 * that will be adjusted to 0) and we need the space -- we 14986 * need to return failure. We return ENOSPC to differentiate 14987 * it from failing to allocate a buffer due to failure to meet 14988 * the reserve (for which we return E2BIG). 14989 */ 14990 rval = ENOSPC; 14991 goto out; 14992 } 14993 14994 if ((rval = dtrace_state_buffers(state)) != 0) 14995 goto err; 14996 14997 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 14998 sz = dtrace_dstate_defsize; 14999 15000 do { 15001 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 15002 15003 if (rval == 0) 15004 break; 15005 15006 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 15007 goto err; 15008 } while (sz >>= 1); 15009 15010 opt[DTRACEOPT_DYNVARSIZE] = sz; 15011 15012 if (rval != 0) 15013 goto err; 15014 15015 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 15016 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 15017 15018 if (opt[DTRACEOPT_CLEANRATE] == 0) 15019 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 15020 15021 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 15022 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 15023 15024 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 15025 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 15026 15027 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 15028 #ifdef illumos 15029 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 15030 hdlr.cyh_arg = state; 15031 hdlr.cyh_level = CY_LOW_LEVEL; 15032 15033 when.cyt_when = 0; 15034 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 15035 15036 state->dts_cleaner = cyclic_add(&hdlr, &when); 15037 15038 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 15039 hdlr.cyh_arg = state; 15040 hdlr.cyh_level = CY_LOW_LEVEL; 15041 15042 when.cyt_when = 0; 15043 when.cyt_interval = dtrace_deadman_interval; 15044 15045 state->dts_deadman = cyclic_add(&hdlr, &when); 15046 #else 15047 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 15048 dtrace_state_clean, state); 15049 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 15050 dtrace_state_deadman, state); 15051 #endif 15052 15053 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 15054 15055 #ifdef illumos 15056 if (state->dts_getf != 0 && 15057 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 15058 /* 15059 * We don't have kernel privs but we have at least one call 15060 * to getf(); we need to bump our zone's count, and (if 15061 * this is the first enabling to have an unprivileged call 15062 * to getf()) we need to hook into closef(). 15063 */ 15064 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 15065 15066 if (dtrace_getf++ == 0) { 15067 ASSERT(dtrace_closef == NULL); 15068 dtrace_closef = dtrace_getf_barrier; 15069 } 15070 } 15071 #endif 15072 15073 /* 15074 * Now it's time to actually fire the BEGIN probe. We need to disable 15075 * interrupts here both to record the CPU on which we fired the BEGIN 15076 * probe (the data from this CPU will be processed first at user 15077 * level) and to manually activate the buffer for this CPU. 15078 */ 15079 cookie = dtrace_interrupt_disable(); 15080 *cpu = curcpu; 15081 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 15082 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 15083 15084 dtrace_probe(dtrace_probeid_begin, 15085 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 15086 dtrace_interrupt_enable(cookie); 15087 /* 15088 * We may have had an exit action from a BEGIN probe; only change our 15089 * state to ACTIVE if we're still in WARMUP. 15090 */ 15091 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 15092 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 15093 15094 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 15095 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 15096 15097 #ifdef __FreeBSD__ 15098 /* 15099 * We enable anonymous tracing before APs are started, so we must 15100 * activate buffers using the current CPU. 15101 */ 15102 if (state == dtrace_anon.dta_state) 15103 for (int i = 0; i < NCPU; i++) 15104 dtrace_buffer_activate_cpu(state, i); 15105 else 15106 dtrace_xcall(DTRACE_CPUALL, 15107 (dtrace_xcall_t)dtrace_buffer_activate, state); 15108 #else 15109 /* 15110 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 15111 * want each CPU to transition its principal buffer out of the 15112 * INACTIVE state. Doing this assures that no CPU will suddenly begin 15113 * processing an ECB halfway down a probe's ECB chain; all CPUs will 15114 * atomically transition from processing none of a state's ECBs to 15115 * processing all of them. 15116 */ 15117 dtrace_xcall(DTRACE_CPUALL, 15118 (dtrace_xcall_t)dtrace_buffer_activate, state); 15119 #endif 15120 goto out; 15121 15122 err: 15123 dtrace_buffer_free(state->dts_buffer); 15124 dtrace_buffer_free(state->dts_aggbuffer); 15125 15126 if ((nspec = state->dts_nspeculations) == 0) { 15127 ASSERT(state->dts_speculations == NULL); 15128 goto out; 15129 } 15130 15131 spec = state->dts_speculations; 15132 ASSERT(spec != NULL); 15133 15134 for (i = 0; i < state->dts_nspeculations; i++) { 15135 if ((buf = spec[i].dtsp_buffer) == NULL) 15136 break; 15137 15138 dtrace_buffer_free(buf); 15139 kmem_free(buf, bufsize); 15140 } 15141 15142 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15143 state->dts_nspeculations = 0; 15144 state->dts_speculations = NULL; 15145 15146 out: 15147 mutex_exit(&dtrace_lock); 15148 mutex_exit(&cpu_lock); 15149 15150 return (rval); 15151 } 15152 15153 static int 15154 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 15155 { 15156 dtrace_icookie_t cookie; 15157 15158 ASSERT(MUTEX_HELD(&dtrace_lock)); 15159 15160 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 15161 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 15162 return (EINVAL); 15163 15164 /* 15165 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 15166 * to be sure that every CPU has seen it. See below for the details 15167 * on why this is done. 15168 */ 15169 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 15170 dtrace_sync(); 15171 15172 /* 15173 * By this point, it is impossible for any CPU to be still processing 15174 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 15175 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 15176 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 15177 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 15178 * iff we're in the END probe. 15179 */ 15180 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 15181 dtrace_sync(); 15182 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 15183 15184 /* 15185 * Finally, we can release the reserve and call the END probe. We 15186 * disable interrupts across calling the END probe to allow us to 15187 * return the CPU on which we actually called the END probe. This 15188 * allows user-land to be sure that this CPU's principal buffer is 15189 * processed last. 15190 */ 15191 state->dts_reserve = 0; 15192 15193 cookie = dtrace_interrupt_disable(); 15194 *cpu = curcpu; 15195 dtrace_probe(dtrace_probeid_end, 15196 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 15197 dtrace_interrupt_enable(cookie); 15198 15199 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 15200 dtrace_sync(); 15201 15202 #ifdef illumos 15203 if (state->dts_getf != 0 && 15204 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 15205 /* 15206 * We don't have kernel privs but we have at least one call 15207 * to getf(); we need to lower our zone's count, and (if 15208 * this is the last enabling to have an unprivileged call 15209 * to getf()) we need to clear the closef() hook. 15210 */ 15211 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 15212 ASSERT(dtrace_closef == dtrace_getf_barrier); 15213 ASSERT(dtrace_getf > 0); 15214 15215 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 15216 15217 if (--dtrace_getf == 0) 15218 dtrace_closef = NULL; 15219 } 15220 #endif 15221 15222 return (0); 15223 } 15224 15225 static int 15226 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 15227 dtrace_optval_t val) 15228 { 15229 ASSERT(MUTEX_HELD(&dtrace_lock)); 15230 15231 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 15232 return (EBUSY); 15233 15234 if (option >= DTRACEOPT_MAX) 15235 return (EINVAL); 15236 15237 if (option != DTRACEOPT_CPU && val < 0) 15238 return (EINVAL); 15239 15240 switch (option) { 15241 case DTRACEOPT_DESTRUCTIVE: 15242 if (dtrace_destructive_disallow) 15243 return (EACCES); 15244 15245 state->dts_cred.dcr_destructive = 1; 15246 break; 15247 15248 case DTRACEOPT_BUFSIZE: 15249 case DTRACEOPT_DYNVARSIZE: 15250 case DTRACEOPT_AGGSIZE: 15251 case DTRACEOPT_SPECSIZE: 15252 case DTRACEOPT_STRSIZE: 15253 if (val < 0) 15254 return (EINVAL); 15255 15256 if (val >= LONG_MAX) { 15257 /* 15258 * If this is an otherwise negative value, set it to 15259 * the highest multiple of 128m less than LONG_MAX. 15260 * Technically, we're adjusting the size without 15261 * regard to the buffer resizing policy, but in fact, 15262 * this has no effect -- if we set the buffer size to 15263 * ~LONG_MAX and the buffer policy is ultimately set to 15264 * be "manual", the buffer allocation is guaranteed to 15265 * fail, if only because the allocation requires two 15266 * buffers. (We set the the size to the highest 15267 * multiple of 128m because it ensures that the size 15268 * will remain a multiple of a megabyte when 15269 * repeatedly halved -- all the way down to 15m.) 15270 */ 15271 val = LONG_MAX - (1 << 27) + 1; 15272 } 15273 } 15274 15275 state->dts_options[option] = val; 15276 15277 return (0); 15278 } 15279 15280 static void 15281 dtrace_state_destroy(dtrace_state_t *state) 15282 { 15283 dtrace_ecb_t *ecb; 15284 dtrace_vstate_t *vstate = &state->dts_vstate; 15285 #ifdef illumos 15286 minor_t minor = getminor(state->dts_dev); 15287 #endif 15288 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 15289 dtrace_speculation_t *spec = state->dts_speculations; 15290 int nspec = state->dts_nspeculations; 15291 uint32_t match; 15292 15293 ASSERT(MUTEX_HELD(&dtrace_lock)); 15294 ASSERT(MUTEX_HELD(&cpu_lock)); 15295 15296 /* 15297 * First, retract any retained enablings for this state. 15298 */ 15299 dtrace_enabling_retract(state); 15300 ASSERT(state->dts_nretained == 0); 15301 15302 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 15303 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 15304 /* 15305 * We have managed to come into dtrace_state_destroy() on a 15306 * hot enabling -- almost certainly because of a disorderly 15307 * shutdown of a consumer. (That is, a consumer that is 15308 * exiting without having called dtrace_stop().) In this case, 15309 * we're going to set our activity to be KILLED, and then 15310 * issue a sync to be sure that everyone is out of probe 15311 * context before we start blowing away ECBs. 15312 */ 15313 state->dts_activity = DTRACE_ACTIVITY_KILLED; 15314 dtrace_sync(); 15315 } 15316 15317 /* 15318 * Release the credential hold we took in dtrace_state_create(). 15319 */ 15320 if (state->dts_cred.dcr_cred != NULL) 15321 crfree(state->dts_cred.dcr_cred); 15322 15323 /* 15324 * Now we can safely disable and destroy any enabled probes. Because 15325 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 15326 * (especially if they're all enabled), we take two passes through the 15327 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 15328 * in the second we disable whatever is left over. 15329 */ 15330 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 15331 for (i = 0; i < state->dts_necbs; i++) { 15332 if ((ecb = state->dts_ecbs[i]) == NULL) 15333 continue; 15334 15335 if (match && ecb->dte_probe != NULL) { 15336 dtrace_probe_t *probe = ecb->dte_probe; 15337 dtrace_provider_t *prov = probe->dtpr_provider; 15338 15339 if (!(prov->dtpv_priv.dtpp_flags & match)) 15340 continue; 15341 } 15342 15343 dtrace_ecb_disable(ecb); 15344 dtrace_ecb_destroy(ecb); 15345 } 15346 15347 if (!match) 15348 break; 15349 } 15350 15351 /* 15352 * Before we free the buffers, perform one more sync to assure that 15353 * every CPU is out of probe context. 15354 */ 15355 dtrace_sync(); 15356 15357 dtrace_buffer_free(state->dts_buffer); 15358 dtrace_buffer_free(state->dts_aggbuffer); 15359 15360 for (i = 0; i < nspec; i++) 15361 dtrace_buffer_free(spec[i].dtsp_buffer); 15362 15363 #ifdef illumos 15364 if (state->dts_cleaner != CYCLIC_NONE) 15365 cyclic_remove(state->dts_cleaner); 15366 15367 if (state->dts_deadman != CYCLIC_NONE) 15368 cyclic_remove(state->dts_deadman); 15369 #else 15370 callout_stop(&state->dts_cleaner); 15371 callout_drain(&state->dts_cleaner); 15372 callout_stop(&state->dts_deadman); 15373 callout_drain(&state->dts_deadman); 15374 #endif 15375 15376 dtrace_dstate_fini(&vstate->dtvs_dynvars); 15377 dtrace_vstate_fini(vstate); 15378 if (state->dts_ecbs != NULL) 15379 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 15380 15381 if (state->dts_aggregations != NULL) { 15382 #ifdef DEBUG 15383 for (i = 0; i < state->dts_naggregations; i++) 15384 ASSERT(state->dts_aggregations[i] == NULL); 15385 #endif 15386 ASSERT(state->dts_naggregations > 0); 15387 kmem_free(state->dts_aggregations, 15388 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 15389 } 15390 15391 kmem_free(state->dts_buffer, bufsize); 15392 kmem_free(state->dts_aggbuffer, bufsize); 15393 15394 for (i = 0; i < nspec; i++) 15395 kmem_free(spec[i].dtsp_buffer, bufsize); 15396 15397 if (spec != NULL) 15398 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15399 15400 dtrace_format_destroy(state); 15401 15402 if (state->dts_aggid_arena != NULL) { 15403 #ifdef illumos 15404 vmem_destroy(state->dts_aggid_arena); 15405 #else 15406 delete_unrhdr(state->dts_aggid_arena); 15407 #endif 15408 state->dts_aggid_arena = NULL; 15409 } 15410 #ifdef illumos 15411 ddi_soft_state_free(dtrace_softstate, minor); 15412 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 15413 #endif 15414 } 15415 15416 /* 15417 * DTrace Anonymous Enabling Functions 15418 */ 15419 static dtrace_state_t * 15420 dtrace_anon_grab(void) 15421 { 15422 dtrace_state_t *state; 15423 15424 ASSERT(MUTEX_HELD(&dtrace_lock)); 15425 15426 if ((state = dtrace_anon.dta_state) == NULL) { 15427 ASSERT(dtrace_anon.dta_enabling == NULL); 15428 return (NULL); 15429 } 15430 15431 ASSERT(dtrace_anon.dta_enabling != NULL); 15432 ASSERT(dtrace_retained != NULL); 15433 15434 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 15435 dtrace_anon.dta_enabling = NULL; 15436 dtrace_anon.dta_state = NULL; 15437 15438 return (state); 15439 } 15440 15441 static void 15442 dtrace_anon_property(void) 15443 { 15444 int i, rv; 15445 dtrace_state_t *state; 15446 dof_hdr_t *dof; 15447 char c[32]; /* enough for "dof-data-" + digits */ 15448 15449 ASSERT(MUTEX_HELD(&dtrace_lock)); 15450 ASSERT(MUTEX_HELD(&cpu_lock)); 15451 15452 for (i = 0; ; i++) { 15453 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 15454 15455 dtrace_err_verbose = 1; 15456 15457 if ((dof = dtrace_dof_property(c)) == NULL) { 15458 dtrace_err_verbose = 0; 15459 break; 15460 } 15461 15462 #ifdef illumos 15463 /* 15464 * We want to create anonymous state, so we need to transition 15465 * the kernel debugger to indicate that DTrace is active. If 15466 * this fails (e.g. because the debugger has modified text in 15467 * some way), we won't continue with the processing. 15468 */ 15469 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15470 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 15471 "enabling ignored."); 15472 dtrace_dof_destroy(dof); 15473 break; 15474 } 15475 #endif 15476 15477 /* 15478 * If we haven't allocated an anonymous state, we'll do so now. 15479 */ 15480 if ((state = dtrace_anon.dta_state) == NULL) { 15481 state = dtrace_state_create(NULL, NULL); 15482 dtrace_anon.dta_state = state; 15483 15484 if (state == NULL) { 15485 /* 15486 * This basically shouldn't happen: the only 15487 * failure mode from dtrace_state_create() is a 15488 * failure of ddi_soft_state_zalloc() that 15489 * itself should never happen. Still, the 15490 * interface allows for a failure mode, and 15491 * we want to fail as gracefully as possible: 15492 * we'll emit an error message and cease 15493 * processing anonymous state in this case. 15494 */ 15495 cmn_err(CE_WARN, "failed to create " 15496 "anonymous state"); 15497 dtrace_dof_destroy(dof); 15498 break; 15499 } 15500 } 15501 15502 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 15503 &dtrace_anon.dta_enabling, 0, B_TRUE); 15504 15505 if (rv == 0) 15506 rv = dtrace_dof_options(dof, state); 15507 15508 dtrace_err_verbose = 0; 15509 dtrace_dof_destroy(dof); 15510 15511 if (rv != 0) { 15512 /* 15513 * This is malformed DOF; chuck any anonymous state 15514 * that we created. 15515 */ 15516 ASSERT(dtrace_anon.dta_enabling == NULL); 15517 dtrace_state_destroy(state); 15518 dtrace_anon.dta_state = NULL; 15519 break; 15520 } 15521 15522 ASSERT(dtrace_anon.dta_enabling != NULL); 15523 } 15524 15525 if (dtrace_anon.dta_enabling != NULL) { 15526 int rval; 15527 15528 /* 15529 * dtrace_enabling_retain() can only fail because we are 15530 * trying to retain more enablings than are allowed -- but 15531 * we only have one anonymous enabling, and we are guaranteed 15532 * to be allowed at least one retained enabling; we assert 15533 * that dtrace_enabling_retain() returns success. 15534 */ 15535 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 15536 ASSERT(rval == 0); 15537 15538 dtrace_enabling_dump(dtrace_anon.dta_enabling); 15539 } 15540 } 15541 15542 /* 15543 * DTrace Helper Functions 15544 */ 15545 static void 15546 dtrace_helper_trace(dtrace_helper_action_t *helper, 15547 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 15548 { 15549 uint32_t size, next, nnext, i; 15550 dtrace_helptrace_t *ent, *buffer; 15551 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 15552 15553 if ((buffer = dtrace_helptrace_buffer) == NULL) 15554 return; 15555 15556 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 15557 15558 /* 15559 * What would a tracing framework be without its own tracing 15560 * framework? (Well, a hell of a lot simpler, for starters...) 15561 */ 15562 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 15563 sizeof (uint64_t) - sizeof (uint64_t); 15564 15565 /* 15566 * Iterate until we can allocate a slot in the trace buffer. 15567 */ 15568 do { 15569 next = dtrace_helptrace_next; 15570 15571 if (next + size < dtrace_helptrace_bufsize) { 15572 nnext = next + size; 15573 } else { 15574 nnext = size; 15575 } 15576 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 15577 15578 /* 15579 * We have our slot; fill it in. 15580 */ 15581 if (nnext == size) { 15582 dtrace_helptrace_wrapped++; 15583 next = 0; 15584 } 15585 15586 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next); 15587 ent->dtht_helper = helper; 15588 ent->dtht_where = where; 15589 ent->dtht_nlocals = vstate->dtvs_nlocals; 15590 15591 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 15592 mstate->dtms_fltoffs : -1; 15593 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 15594 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 15595 15596 for (i = 0; i < vstate->dtvs_nlocals; i++) { 15597 dtrace_statvar_t *svar; 15598 15599 if ((svar = vstate->dtvs_locals[i]) == NULL) 15600 continue; 15601 15602 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 15603 ent->dtht_locals[i] = 15604 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 15605 } 15606 } 15607 15608 static uint64_t 15609 dtrace_helper(int which, dtrace_mstate_t *mstate, 15610 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 15611 { 15612 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 15613 uint64_t sarg0 = mstate->dtms_arg[0]; 15614 uint64_t sarg1 = mstate->dtms_arg[1]; 15615 uint64_t rval = 0; 15616 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 15617 dtrace_helper_action_t *helper; 15618 dtrace_vstate_t *vstate; 15619 dtrace_difo_t *pred; 15620 int i, trace = dtrace_helptrace_buffer != NULL; 15621 15622 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 15623 15624 if (helpers == NULL) 15625 return (0); 15626 15627 if ((helper = helpers->dthps_actions[which]) == NULL) 15628 return (0); 15629 15630 vstate = &helpers->dthps_vstate; 15631 mstate->dtms_arg[0] = arg0; 15632 mstate->dtms_arg[1] = arg1; 15633 15634 /* 15635 * Now iterate over each helper. If its predicate evaluates to 'true', 15636 * we'll call the corresponding actions. Note that the below calls 15637 * to dtrace_dif_emulate() may set faults in machine state. This is 15638 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 15639 * the stored DIF offset with its own (which is the desired behavior). 15640 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 15641 * from machine state; this is okay, too. 15642 */ 15643 for (; helper != NULL; helper = helper->dtha_next) { 15644 if ((pred = helper->dtha_predicate) != NULL) { 15645 if (trace) 15646 dtrace_helper_trace(helper, mstate, vstate, 0); 15647 15648 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 15649 goto next; 15650 15651 if (*flags & CPU_DTRACE_FAULT) 15652 goto err; 15653 } 15654 15655 for (i = 0; i < helper->dtha_nactions; i++) { 15656 if (trace) 15657 dtrace_helper_trace(helper, 15658 mstate, vstate, i + 1); 15659 15660 rval = dtrace_dif_emulate(helper->dtha_actions[i], 15661 mstate, vstate, state); 15662 15663 if (*flags & CPU_DTRACE_FAULT) 15664 goto err; 15665 } 15666 15667 next: 15668 if (trace) 15669 dtrace_helper_trace(helper, mstate, vstate, 15670 DTRACE_HELPTRACE_NEXT); 15671 } 15672 15673 if (trace) 15674 dtrace_helper_trace(helper, mstate, vstate, 15675 DTRACE_HELPTRACE_DONE); 15676 15677 /* 15678 * Restore the arg0 that we saved upon entry. 15679 */ 15680 mstate->dtms_arg[0] = sarg0; 15681 mstate->dtms_arg[1] = sarg1; 15682 15683 return (rval); 15684 15685 err: 15686 if (trace) 15687 dtrace_helper_trace(helper, mstate, vstate, 15688 DTRACE_HELPTRACE_ERR); 15689 15690 /* 15691 * Restore the arg0 that we saved upon entry. 15692 */ 15693 mstate->dtms_arg[0] = sarg0; 15694 mstate->dtms_arg[1] = sarg1; 15695 15696 return (0); 15697 } 15698 15699 static void 15700 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 15701 dtrace_vstate_t *vstate) 15702 { 15703 int i; 15704 15705 if (helper->dtha_predicate != NULL) 15706 dtrace_difo_release(helper->dtha_predicate, vstate); 15707 15708 for (i = 0; i < helper->dtha_nactions; i++) { 15709 ASSERT(helper->dtha_actions[i] != NULL); 15710 dtrace_difo_release(helper->dtha_actions[i], vstate); 15711 } 15712 15713 kmem_free(helper->dtha_actions, 15714 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 15715 kmem_free(helper, sizeof (dtrace_helper_action_t)); 15716 } 15717 15718 static int 15719 dtrace_helper_destroygen(dtrace_helpers_t *help, int gen) 15720 { 15721 proc_t *p = curproc; 15722 dtrace_vstate_t *vstate; 15723 int i; 15724 15725 if (help == NULL) 15726 help = p->p_dtrace_helpers; 15727 15728 ASSERT(MUTEX_HELD(&dtrace_lock)); 15729 15730 if (help == NULL || gen > help->dthps_generation) 15731 return (EINVAL); 15732 15733 vstate = &help->dthps_vstate; 15734 15735 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15736 dtrace_helper_action_t *last = NULL, *h, *next; 15737 15738 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15739 next = h->dtha_next; 15740 15741 if (h->dtha_generation == gen) { 15742 if (last != NULL) { 15743 last->dtha_next = next; 15744 } else { 15745 help->dthps_actions[i] = next; 15746 } 15747 15748 dtrace_helper_action_destroy(h, vstate); 15749 } else { 15750 last = h; 15751 } 15752 } 15753 } 15754 15755 /* 15756 * Interate until we've cleared out all helper providers with the 15757 * given generation number. 15758 */ 15759 for (;;) { 15760 dtrace_helper_provider_t *prov; 15761 15762 /* 15763 * Look for a helper provider with the right generation. We 15764 * have to start back at the beginning of the list each time 15765 * because we drop dtrace_lock. It's unlikely that we'll make 15766 * more than two passes. 15767 */ 15768 for (i = 0; i < help->dthps_nprovs; i++) { 15769 prov = help->dthps_provs[i]; 15770 15771 if (prov->dthp_generation == gen) 15772 break; 15773 } 15774 15775 /* 15776 * If there were no matches, we're done. 15777 */ 15778 if (i == help->dthps_nprovs) 15779 break; 15780 15781 /* 15782 * Move the last helper provider into this slot. 15783 */ 15784 help->dthps_nprovs--; 15785 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 15786 help->dthps_provs[help->dthps_nprovs] = NULL; 15787 15788 mutex_exit(&dtrace_lock); 15789 15790 /* 15791 * If we have a meta provider, remove this helper provider. 15792 */ 15793 mutex_enter(&dtrace_meta_lock); 15794 if (dtrace_meta_pid != NULL) { 15795 ASSERT(dtrace_deferred_pid == NULL); 15796 dtrace_helper_provider_remove(&prov->dthp_prov, 15797 p->p_pid); 15798 } 15799 mutex_exit(&dtrace_meta_lock); 15800 15801 dtrace_helper_provider_destroy(prov); 15802 15803 mutex_enter(&dtrace_lock); 15804 } 15805 15806 return (0); 15807 } 15808 15809 static int 15810 dtrace_helper_validate(dtrace_helper_action_t *helper) 15811 { 15812 int err = 0, i; 15813 dtrace_difo_t *dp; 15814 15815 if ((dp = helper->dtha_predicate) != NULL) 15816 err += dtrace_difo_validate_helper(dp); 15817 15818 for (i = 0; i < helper->dtha_nactions; i++) 15819 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 15820 15821 return (err == 0); 15822 } 15823 15824 static int 15825 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep, 15826 dtrace_helpers_t *help) 15827 { 15828 dtrace_helper_action_t *helper, *last; 15829 dtrace_actdesc_t *act; 15830 dtrace_vstate_t *vstate; 15831 dtrace_predicate_t *pred; 15832 int count = 0, nactions = 0, i; 15833 15834 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 15835 return (EINVAL); 15836 15837 last = help->dthps_actions[which]; 15838 vstate = &help->dthps_vstate; 15839 15840 for (count = 0; last != NULL; last = last->dtha_next) { 15841 count++; 15842 if (last->dtha_next == NULL) 15843 break; 15844 } 15845 15846 /* 15847 * If we already have dtrace_helper_actions_max helper actions for this 15848 * helper action type, we'll refuse to add a new one. 15849 */ 15850 if (count >= dtrace_helper_actions_max) 15851 return (ENOSPC); 15852 15853 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 15854 helper->dtha_generation = help->dthps_generation; 15855 15856 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 15857 ASSERT(pred->dtp_difo != NULL); 15858 dtrace_difo_hold(pred->dtp_difo); 15859 helper->dtha_predicate = pred->dtp_difo; 15860 } 15861 15862 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 15863 if (act->dtad_kind != DTRACEACT_DIFEXPR) 15864 goto err; 15865 15866 if (act->dtad_difo == NULL) 15867 goto err; 15868 15869 nactions++; 15870 } 15871 15872 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 15873 (helper->dtha_nactions = nactions), KM_SLEEP); 15874 15875 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 15876 dtrace_difo_hold(act->dtad_difo); 15877 helper->dtha_actions[i++] = act->dtad_difo; 15878 } 15879 15880 if (!dtrace_helper_validate(helper)) 15881 goto err; 15882 15883 if (last == NULL) { 15884 help->dthps_actions[which] = helper; 15885 } else { 15886 last->dtha_next = helper; 15887 } 15888 15889 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 15890 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 15891 dtrace_helptrace_next = 0; 15892 } 15893 15894 return (0); 15895 err: 15896 dtrace_helper_action_destroy(helper, vstate); 15897 return (EINVAL); 15898 } 15899 15900 static void 15901 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 15902 dof_helper_t *dofhp) 15903 { 15904 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 15905 15906 mutex_enter(&dtrace_meta_lock); 15907 mutex_enter(&dtrace_lock); 15908 15909 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 15910 /* 15911 * If the dtrace module is loaded but not attached, or if 15912 * there aren't isn't a meta provider registered to deal with 15913 * these provider descriptions, we need to postpone creating 15914 * the actual providers until later. 15915 */ 15916 15917 if (help->dthps_next == NULL && help->dthps_prev == NULL && 15918 dtrace_deferred_pid != help) { 15919 help->dthps_deferred = 1; 15920 help->dthps_pid = p->p_pid; 15921 help->dthps_next = dtrace_deferred_pid; 15922 help->dthps_prev = NULL; 15923 if (dtrace_deferred_pid != NULL) 15924 dtrace_deferred_pid->dthps_prev = help; 15925 dtrace_deferred_pid = help; 15926 } 15927 15928 mutex_exit(&dtrace_lock); 15929 15930 } else if (dofhp != NULL) { 15931 /* 15932 * If the dtrace module is loaded and we have a particular 15933 * helper provider description, pass that off to the 15934 * meta provider. 15935 */ 15936 15937 mutex_exit(&dtrace_lock); 15938 15939 dtrace_helper_provide(dofhp, p->p_pid); 15940 15941 } else { 15942 /* 15943 * Otherwise, just pass all the helper provider descriptions 15944 * off to the meta provider. 15945 */ 15946 15947 int i; 15948 mutex_exit(&dtrace_lock); 15949 15950 for (i = 0; i < help->dthps_nprovs; i++) { 15951 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 15952 p->p_pid); 15953 } 15954 } 15955 15956 mutex_exit(&dtrace_meta_lock); 15957 } 15958 15959 static int 15960 dtrace_helper_provider_add(dof_helper_t *dofhp, dtrace_helpers_t *help, int gen) 15961 { 15962 dtrace_helper_provider_t *hprov, **tmp_provs; 15963 uint_t tmp_maxprovs, i; 15964 15965 ASSERT(MUTEX_HELD(&dtrace_lock)); 15966 ASSERT(help != NULL); 15967 15968 /* 15969 * If we already have dtrace_helper_providers_max helper providers, 15970 * we're refuse to add a new one. 15971 */ 15972 if (help->dthps_nprovs >= dtrace_helper_providers_max) 15973 return (ENOSPC); 15974 15975 /* 15976 * Check to make sure this isn't a duplicate. 15977 */ 15978 for (i = 0; i < help->dthps_nprovs; i++) { 15979 if (dofhp->dofhp_addr == 15980 help->dthps_provs[i]->dthp_prov.dofhp_addr) 15981 return (EALREADY); 15982 } 15983 15984 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 15985 hprov->dthp_prov = *dofhp; 15986 hprov->dthp_ref = 1; 15987 hprov->dthp_generation = gen; 15988 15989 /* 15990 * Allocate a bigger table for helper providers if it's already full. 15991 */ 15992 if (help->dthps_maxprovs == help->dthps_nprovs) { 15993 tmp_maxprovs = help->dthps_maxprovs; 15994 tmp_provs = help->dthps_provs; 15995 15996 if (help->dthps_maxprovs == 0) 15997 help->dthps_maxprovs = 2; 15998 else 15999 help->dthps_maxprovs *= 2; 16000 if (help->dthps_maxprovs > dtrace_helper_providers_max) 16001 help->dthps_maxprovs = dtrace_helper_providers_max; 16002 16003 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 16004 16005 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 16006 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16007 16008 if (tmp_provs != NULL) { 16009 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 16010 sizeof (dtrace_helper_provider_t *)); 16011 kmem_free(tmp_provs, tmp_maxprovs * 16012 sizeof (dtrace_helper_provider_t *)); 16013 } 16014 } 16015 16016 help->dthps_provs[help->dthps_nprovs] = hprov; 16017 help->dthps_nprovs++; 16018 16019 return (0); 16020 } 16021 16022 static void 16023 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 16024 { 16025 mutex_enter(&dtrace_lock); 16026 16027 if (--hprov->dthp_ref == 0) { 16028 dof_hdr_t *dof; 16029 mutex_exit(&dtrace_lock); 16030 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 16031 dtrace_dof_destroy(dof); 16032 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 16033 } else { 16034 mutex_exit(&dtrace_lock); 16035 } 16036 } 16037 16038 static int 16039 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 16040 { 16041 uintptr_t daddr = (uintptr_t)dof; 16042 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 16043 dof_provider_t *provider; 16044 dof_probe_t *probe; 16045 uint8_t *arg; 16046 char *strtab, *typestr; 16047 dof_stridx_t typeidx; 16048 size_t typesz; 16049 uint_t nprobes, j, k; 16050 16051 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 16052 16053 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 16054 dtrace_dof_error(dof, "misaligned section offset"); 16055 return (-1); 16056 } 16057 16058 /* 16059 * The section needs to be large enough to contain the DOF provider 16060 * structure appropriate for the given version. 16061 */ 16062 if (sec->dofs_size < 16063 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 16064 offsetof(dof_provider_t, dofpv_prenoffs) : 16065 sizeof (dof_provider_t))) { 16066 dtrace_dof_error(dof, "provider section too small"); 16067 return (-1); 16068 } 16069 16070 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 16071 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 16072 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 16073 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 16074 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 16075 16076 if (str_sec == NULL || prb_sec == NULL || 16077 arg_sec == NULL || off_sec == NULL) 16078 return (-1); 16079 16080 enoff_sec = NULL; 16081 16082 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 16083 provider->dofpv_prenoffs != DOF_SECT_NONE && 16084 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 16085 provider->dofpv_prenoffs)) == NULL) 16086 return (-1); 16087 16088 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 16089 16090 if (provider->dofpv_name >= str_sec->dofs_size || 16091 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 16092 dtrace_dof_error(dof, "invalid provider name"); 16093 return (-1); 16094 } 16095 16096 if (prb_sec->dofs_entsize == 0 || 16097 prb_sec->dofs_entsize > prb_sec->dofs_size) { 16098 dtrace_dof_error(dof, "invalid entry size"); 16099 return (-1); 16100 } 16101 16102 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 16103 dtrace_dof_error(dof, "misaligned entry size"); 16104 return (-1); 16105 } 16106 16107 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 16108 dtrace_dof_error(dof, "invalid entry size"); 16109 return (-1); 16110 } 16111 16112 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 16113 dtrace_dof_error(dof, "misaligned section offset"); 16114 return (-1); 16115 } 16116 16117 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 16118 dtrace_dof_error(dof, "invalid entry size"); 16119 return (-1); 16120 } 16121 16122 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 16123 16124 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 16125 16126 /* 16127 * Take a pass through the probes to check for errors. 16128 */ 16129 for (j = 0; j < nprobes; j++) { 16130 probe = (dof_probe_t *)(uintptr_t)(daddr + 16131 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 16132 16133 if (probe->dofpr_func >= str_sec->dofs_size) { 16134 dtrace_dof_error(dof, "invalid function name"); 16135 return (-1); 16136 } 16137 16138 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 16139 dtrace_dof_error(dof, "function name too long"); 16140 /* 16141 * Keep going if the function name is too long. 16142 * Unlike provider and probe names, we cannot reasonably 16143 * impose restrictions on function names, since they're 16144 * a property of the code being instrumented. We will 16145 * skip this probe in dtrace_helper_provide_one(). 16146 */ 16147 } 16148 16149 if (probe->dofpr_name >= str_sec->dofs_size || 16150 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 16151 dtrace_dof_error(dof, "invalid probe name"); 16152 return (-1); 16153 } 16154 16155 /* 16156 * The offset count must not wrap the index, and the offsets 16157 * must also not overflow the section's data. 16158 */ 16159 if (probe->dofpr_offidx + probe->dofpr_noffs < 16160 probe->dofpr_offidx || 16161 (probe->dofpr_offidx + probe->dofpr_noffs) * 16162 off_sec->dofs_entsize > off_sec->dofs_size) { 16163 dtrace_dof_error(dof, "invalid probe offset"); 16164 return (-1); 16165 } 16166 16167 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 16168 /* 16169 * If there's no is-enabled offset section, make sure 16170 * there aren't any is-enabled offsets. Otherwise 16171 * perform the same checks as for probe offsets 16172 * (immediately above). 16173 */ 16174 if (enoff_sec == NULL) { 16175 if (probe->dofpr_enoffidx != 0 || 16176 probe->dofpr_nenoffs != 0) { 16177 dtrace_dof_error(dof, "is-enabled " 16178 "offsets with null section"); 16179 return (-1); 16180 } 16181 } else if (probe->dofpr_enoffidx + 16182 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 16183 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 16184 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 16185 dtrace_dof_error(dof, "invalid is-enabled " 16186 "offset"); 16187 return (-1); 16188 } 16189 16190 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 16191 dtrace_dof_error(dof, "zero probe and " 16192 "is-enabled offsets"); 16193 return (-1); 16194 } 16195 } else if (probe->dofpr_noffs == 0) { 16196 dtrace_dof_error(dof, "zero probe offsets"); 16197 return (-1); 16198 } 16199 16200 if (probe->dofpr_argidx + probe->dofpr_xargc < 16201 probe->dofpr_argidx || 16202 (probe->dofpr_argidx + probe->dofpr_xargc) * 16203 arg_sec->dofs_entsize > arg_sec->dofs_size) { 16204 dtrace_dof_error(dof, "invalid args"); 16205 return (-1); 16206 } 16207 16208 typeidx = probe->dofpr_nargv; 16209 typestr = strtab + probe->dofpr_nargv; 16210 for (k = 0; k < probe->dofpr_nargc; k++) { 16211 if (typeidx >= str_sec->dofs_size) { 16212 dtrace_dof_error(dof, "bad " 16213 "native argument type"); 16214 return (-1); 16215 } 16216 16217 typesz = strlen(typestr) + 1; 16218 if (typesz > DTRACE_ARGTYPELEN) { 16219 dtrace_dof_error(dof, "native " 16220 "argument type too long"); 16221 return (-1); 16222 } 16223 typeidx += typesz; 16224 typestr += typesz; 16225 } 16226 16227 typeidx = probe->dofpr_xargv; 16228 typestr = strtab + probe->dofpr_xargv; 16229 for (k = 0; k < probe->dofpr_xargc; k++) { 16230 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 16231 dtrace_dof_error(dof, "bad " 16232 "native argument index"); 16233 return (-1); 16234 } 16235 16236 if (typeidx >= str_sec->dofs_size) { 16237 dtrace_dof_error(dof, "bad " 16238 "translated argument type"); 16239 return (-1); 16240 } 16241 16242 typesz = strlen(typestr) + 1; 16243 if (typesz > DTRACE_ARGTYPELEN) { 16244 dtrace_dof_error(dof, "translated argument " 16245 "type too long"); 16246 return (-1); 16247 } 16248 16249 typeidx += typesz; 16250 typestr += typesz; 16251 } 16252 } 16253 16254 return (0); 16255 } 16256 16257 static int 16258 #ifdef __FreeBSD__ 16259 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp, struct proc *p) 16260 #else 16261 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 16262 #endif 16263 { 16264 dtrace_helpers_t *help; 16265 dtrace_vstate_t *vstate; 16266 dtrace_enabling_t *enab = NULL; 16267 #ifndef __FreeBSD__ 16268 proc_t *p = curproc; 16269 #endif 16270 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 16271 uintptr_t daddr = (uintptr_t)dof; 16272 16273 ASSERT(MUTEX_HELD(&dtrace_lock)); 16274 16275 if ((help = p->p_dtrace_helpers) == NULL) 16276 help = dtrace_helpers_create(p); 16277 16278 vstate = &help->dthps_vstate; 16279 16280 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 16281 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 16282 dtrace_dof_destroy(dof); 16283 return (rv); 16284 } 16285 16286 /* 16287 * Look for helper providers and validate their descriptions. 16288 */ 16289 if (dhp != NULL) { 16290 for (i = 0; i < dof->dofh_secnum; i++) { 16291 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 16292 dof->dofh_secoff + i * dof->dofh_secsize); 16293 16294 if (sec->dofs_type != DOF_SECT_PROVIDER) 16295 continue; 16296 16297 if (dtrace_helper_provider_validate(dof, sec) != 0) { 16298 dtrace_enabling_destroy(enab); 16299 dtrace_dof_destroy(dof); 16300 return (-1); 16301 } 16302 16303 nprovs++; 16304 } 16305 } 16306 16307 /* 16308 * Now we need to walk through the ECB descriptions in the enabling. 16309 */ 16310 for (i = 0; i < enab->dten_ndesc; i++) { 16311 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 16312 dtrace_probedesc_t *desc = &ep->dted_probe; 16313 16314 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 16315 continue; 16316 16317 if (strcmp(desc->dtpd_mod, "helper") != 0) 16318 continue; 16319 16320 if (strcmp(desc->dtpd_func, "ustack") != 0) 16321 continue; 16322 16323 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 16324 ep, help)) != 0) { 16325 /* 16326 * Adding this helper action failed -- we are now going 16327 * to rip out the entire generation and return failure. 16328 */ 16329 (void) dtrace_helper_destroygen(help, 16330 help->dthps_generation); 16331 dtrace_enabling_destroy(enab); 16332 dtrace_dof_destroy(dof); 16333 return (-1); 16334 } 16335 16336 nhelpers++; 16337 } 16338 16339 if (nhelpers < enab->dten_ndesc) 16340 dtrace_dof_error(dof, "unmatched helpers"); 16341 16342 gen = help->dthps_generation++; 16343 dtrace_enabling_destroy(enab); 16344 16345 if (dhp != NULL && nprovs > 0) { 16346 /* 16347 * Now that this is in-kernel, we change the sense of the 16348 * members: dofhp_dof denotes the in-kernel copy of the DOF 16349 * and dofhp_addr denotes the address at user-level. 16350 */ 16351 dhp->dofhp_addr = dhp->dofhp_dof; 16352 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 16353 16354 if (dtrace_helper_provider_add(dhp, help, gen) == 0) { 16355 mutex_exit(&dtrace_lock); 16356 dtrace_helper_provider_register(p, help, dhp); 16357 mutex_enter(&dtrace_lock); 16358 16359 destroy = 0; 16360 } 16361 } 16362 16363 if (destroy) 16364 dtrace_dof_destroy(dof); 16365 16366 return (gen); 16367 } 16368 16369 static dtrace_helpers_t * 16370 dtrace_helpers_create(proc_t *p) 16371 { 16372 dtrace_helpers_t *help; 16373 16374 ASSERT(MUTEX_HELD(&dtrace_lock)); 16375 ASSERT(p->p_dtrace_helpers == NULL); 16376 16377 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 16378 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 16379 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 16380 16381 p->p_dtrace_helpers = help; 16382 dtrace_helpers++; 16383 16384 return (help); 16385 } 16386 16387 #ifdef illumos 16388 static 16389 #endif 16390 void 16391 dtrace_helpers_destroy(proc_t *p) 16392 { 16393 dtrace_helpers_t *help; 16394 dtrace_vstate_t *vstate; 16395 #ifdef illumos 16396 proc_t *p = curproc; 16397 #endif 16398 int i; 16399 16400 mutex_enter(&dtrace_lock); 16401 16402 ASSERT(p->p_dtrace_helpers != NULL); 16403 ASSERT(dtrace_helpers > 0); 16404 16405 help = p->p_dtrace_helpers; 16406 vstate = &help->dthps_vstate; 16407 16408 /* 16409 * We're now going to lose the help from this process. 16410 */ 16411 p->p_dtrace_helpers = NULL; 16412 dtrace_sync(); 16413 16414 /* 16415 * Destory the helper actions. 16416 */ 16417 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16418 dtrace_helper_action_t *h, *next; 16419 16420 for (h = help->dthps_actions[i]; h != NULL; h = next) { 16421 next = h->dtha_next; 16422 dtrace_helper_action_destroy(h, vstate); 16423 h = next; 16424 } 16425 } 16426 16427 mutex_exit(&dtrace_lock); 16428 16429 /* 16430 * Destroy the helper providers. 16431 */ 16432 if (help->dthps_maxprovs > 0) { 16433 mutex_enter(&dtrace_meta_lock); 16434 if (dtrace_meta_pid != NULL) { 16435 ASSERT(dtrace_deferred_pid == NULL); 16436 16437 for (i = 0; i < help->dthps_nprovs; i++) { 16438 dtrace_helper_provider_remove( 16439 &help->dthps_provs[i]->dthp_prov, p->p_pid); 16440 } 16441 } else { 16442 mutex_enter(&dtrace_lock); 16443 ASSERT(help->dthps_deferred == 0 || 16444 help->dthps_next != NULL || 16445 help->dthps_prev != NULL || 16446 help == dtrace_deferred_pid); 16447 16448 /* 16449 * Remove the helper from the deferred list. 16450 */ 16451 if (help->dthps_next != NULL) 16452 help->dthps_next->dthps_prev = help->dthps_prev; 16453 if (help->dthps_prev != NULL) 16454 help->dthps_prev->dthps_next = help->dthps_next; 16455 if (dtrace_deferred_pid == help) { 16456 dtrace_deferred_pid = help->dthps_next; 16457 ASSERT(help->dthps_prev == NULL); 16458 } 16459 16460 mutex_exit(&dtrace_lock); 16461 } 16462 16463 mutex_exit(&dtrace_meta_lock); 16464 16465 for (i = 0; i < help->dthps_nprovs; i++) { 16466 dtrace_helper_provider_destroy(help->dthps_provs[i]); 16467 } 16468 16469 kmem_free(help->dthps_provs, help->dthps_maxprovs * 16470 sizeof (dtrace_helper_provider_t *)); 16471 } 16472 16473 mutex_enter(&dtrace_lock); 16474 16475 dtrace_vstate_fini(&help->dthps_vstate); 16476 kmem_free(help->dthps_actions, 16477 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 16478 kmem_free(help, sizeof (dtrace_helpers_t)); 16479 16480 --dtrace_helpers; 16481 mutex_exit(&dtrace_lock); 16482 } 16483 16484 #ifdef illumos 16485 static 16486 #endif 16487 void 16488 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 16489 { 16490 dtrace_helpers_t *help, *newhelp; 16491 dtrace_helper_action_t *helper, *new, *last; 16492 dtrace_difo_t *dp; 16493 dtrace_vstate_t *vstate; 16494 int i, j, sz, hasprovs = 0; 16495 16496 mutex_enter(&dtrace_lock); 16497 ASSERT(from->p_dtrace_helpers != NULL); 16498 ASSERT(dtrace_helpers > 0); 16499 16500 help = from->p_dtrace_helpers; 16501 newhelp = dtrace_helpers_create(to); 16502 ASSERT(to->p_dtrace_helpers != NULL); 16503 16504 newhelp->dthps_generation = help->dthps_generation; 16505 vstate = &newhelp->dthps_vstate; 16506 16507 /* 16508 * Duplicate the helper actions. 16509 */ 16510 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16511 if ((helper = help->dthps_actions[i]) == NULL) 16512 continue; 16513 16514 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 16515 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 16516 KM_SLEEP); 16517 new->dtha_generation = helper->dtha_generation; 16518 16519 if ((dp = helper->dtha_predicate) != NULL) { 16520 dp = dtrace_difo_duplicate(dp, vstate); 16521 new->dtha_predicate = dp; 16522 } 16523 16524 new->dtha_nactions = helper->dtha_nactions; 16525 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 16526 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 16527 16528 for (j = 0; j < new->dtha_nactions; j++) { 16529 dtrace_difo_t *dp = helper->dtha_actions[j]; 16530 16531 ASSERT(dp != NULL); 16532 dp = dtrace_difo_duplicate(dp, vstate); 16533 new->dtha_actions[j] = dp; 16534 } 16535 16536 if (last != NULL) { 16537 last->dtha_next = new; 16538 } else { 16539 newhelp->dthps_actions[i] = new; 16540 } 16541 16542 last = new; 16543 } 16544 } 16545 16546 /* 16547 * Duplicate the helper providers and register them with the 16548 * DTrace framework. 16549 */ 16550 if (help->dthps_nprovs > 0) { 16551 newhelp->dthps_nprovs = help->dthps_nprovs; 16552 newhelp->dthps_maxprovs = help->dthps_nprovs; 16553 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 16554 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16555 for (i = 0; i < newhelp->dthps_nprovs; i++) { 16556 newhelp->dthps_provs[i] = help->dthps_provs[i]; 16557 newhelp->dthps_provs[i]->dthp_ref++; 16558 } 16559 16560 hasprovs = 1; 16561 } 16562 16563 mutex_exit(&dtrace_lock); 16564 16565 if (hasprovs) 16566 dtrace_helper_provider_register(to, newhelp, NULL); 16567 } 16568 16569 /* 16570 * DTrace Hook Functions 16571 */ 16572 static void 16573 dtrace_module_loaded(modctl_t *ctl) 16574 { 16575 dtrace_provider_t *prv; 16576 16577 mutex_enter(&dtrace_provider_lock); 16578 #ifdef illumos 16579 mutex_enter(&mod_lock); 16580 #endif 16581 16582 #ifdef illumos 16583 ASSERT(ctl->mod_busy); 16584 #endif 16585 16586 /* 16587 * We're going to call each providers per-module provide operation 16588 * specifying only this module. 16589 */ 16590 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 16591 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 16592 16593 #ifdef illumos 16594 mutex_exit(&mod_lock); 16595 #endif 16596 mutex_exit(&dtrace_provider_lock); 16597 16598 /* 16599 * If we have any retained enablings, we need to match against them. 16600 * Enabling probes requires that cpu_lock be held, and we cannot hold 16601 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 16602 * module. (In particular, this happens when loading scheduling 16603 * classes.) So if we have any retained enablings, we need to dispatch 16604 * our task queue to do the match for us. 16605 */ 16606 mutex_enter(&dtrace_lock); 16607 16608 if (dtrace_retained == NULL) { 16609 mutex_exit(&dtrace_lock); 16610 return; 16611 } 16612 16613 (void) taskq_dispatch(dtrace_taskq, 16614 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 16615 16616 mutex_exit(&dtrace_lock); 16617 16618 /* 16619 * And now, for a little heuristic sleaze: in general, we want to 16620 * match modules as soon as they load. However, we cannot guarantee 16621 * this, because it would lead us to the lock ordering violation 16622 * outlined above. The common case, of course, is that cpu_lock is 16623 * _not_ held -- so we delay here for a clock tick, hoping that that's 16624 * long enough for the task queue to do its work. If it's not, it's 16625 * not a serious problem -- it just means that the module that we 16626 * just loaded may not be immediately instrumentable. 16627 */ 16628 delay(1); 16629 } 16630 16631 static void 16632 #ifdef illumos 16633 dtrace_module_unloaded(modctl_t *ctl) 16634 #else 16635 dtrace_module_unloaded(modctl_t *ctl, int *error) 16636 #endif 16637 { 16638 dtrace_probe_t template, *probe, *first, *next; 16639 dtrace_provider_t *prov; 16640 #ifndef illumos 16641 char modname[DTRACE_MODNAMELEN]; 16642 size_t len; 16643 #endif 16644 16645 #ifdef illumos 16646 template.dtpr_mod = ctl->mod_modname; 16647 #else 16648 /* Handle the fact that ctl->filename may end in ".ko". */ 16649 strlcpy(modname, ctl->filename, sizeof(modname)); 16650 len = strlen(ctl->filename); 16651 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0) 16652 modname[len - 3] = '\0'; 16653 template.dtpr_mod = modname; 16654 #endif 16655 16656 mutex_enter(&dtrace_provider_lock); 16657 #ifdef illumos 16658 mutex_enter(&mod_lock); 16659 #endif 16660 mutex_enter(&dtrace_lock); 16661 16662 #ifndef illumos 16663 if (ctl->nenabled > 0) { 16664 /* Don't allow unloads if a probe is enabled. */ 16665 mutex_exit(&dtrace_provider_lock); 16666 mutex_exit(&dtrace_lock); 16667 *error = -1; 16668 printf( 16669 "kldunload: attempt to unload module that has DTrace probes enabled\n"); 16670 return; 16671 } 16672 #endif 16673 16674 if (dtrace_bymod == NULL) { 16675 /* 16676 * The DTrace module is loaded (obviously) but not attached; 16677 * we don't have any work to do. 16678 */ 16679 mutex_exit(&dtrace_provider_lock); 16680 #ifdef illumos 16681 mutex_exit(&mod_lock); 16682 #endif 16683 mutex_exit(&dtrace_lock); 16684 return; 16685 } 16686 16687 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 16688 probe != NULL; probe = probe->dtpr_nextmod) { 16689 if (probe->dtpr_ecb != NULL) { 16690 mutex_exit(&dtrace_provider_lock); 16691 #ifdef illumos 16692 mutex_exit(&mod_lock); 16693 #endif 16694 mutex_exit(&dtrace_lock); 16695 16696 /* 16697 * This shouldn't _actually_ be possible -- we're 16698 * unloading a module that has an enabled probe in it. 16699 * (It's normally up to the provider to make sure that 16700 * this can't happen.) However, because dtps_enable() 16701 * doesn't have a failure mode, there can be an 16702 * enable/unload race. Upshot: we don't want to 16703 * assert, but we're not going to disable the 16704 * probe, either. 16705 */ 16706 if (dtrace_err_verbose) { 16707 #ifdef illumos 16708 cmn_err(CE_WARN, "unloaded module '%s' had " 16709 "enabled probes", ctl->mod_modname); 16710 #else 16711 cmn_err(CE_WARN, "unloaded module '%s' had " 16712 "enabled probes", modname); 16713 #endif 16714 } 16715 16716 return; 16717 } 16718 } 16719 16720 probe = first; 16721 16722 for (first = NULL; probe != NULL; probe = next) { 16723 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 16724 16725 dtrace_probes[probe->dtpr_id - 1] = NULL; 16726 16727 next = probe->dtpr_nextmod; 16728 dtrace_hash_remove(dtrace_bymod, probe); 16729 dtrace_hash_remove(dtrace_byfunc, probe); 16730 dtrace_hash_remove(dtrace_byname, probe); 16731 16732 if (first == NULL) { 16733 first = probe; 16734 probe->dtpr_nextmod = NULL; 16735 } else { 16736 probe->dtpr_nextmod = first; 16737 first = probe; 16738 } 16739 } 16740 16741 /* 16742 * We've removed all of the module's probes from the hash chains and 16743 * from the probe array. Now issue a dtrace_sync() to be sure that 16744 * everyone has cleared out from any probe array processing. 16745 */ 16746 dtrace_sync(); 16747 16748 for (probe = first; probe != NULL; probe = first) { 16749 first = probe->dtpr_nextmod; 16750 prov = probe->dtpr_provider; 16751 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 16752 probe->dtpr_arg); 16753 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 16754 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 16755 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 16756 #ifdef illumos 16757 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 16758 #else 16759 free_unr(dtrace_arena, probe->dtpr_id); 16760 #endif 16761 kmem_free(probe, sizeof (dtrace_probe_t)); 16762 } 16763 16764 mutex_exit(&dtrace_lock); 16765 #ifdef illumos 16766 mutex_exit(&mod_lock); 16767 #endif 16768 mutex_exit(&dtrace_provider_lock); 16769 } 16770 16771 #ifndef illumos 16772 static void 16773 dtrace_kld_load(void *arg __unused, linker_file_t lf) 16774 { 16775 16776 dtrace_module_loaded(lf); 16777 } 16778 16779 static void 16780 dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error) 16781 { 16782 16783 if (*error != 0) 16784 /* We already have an error, so don't do anything. */ 16785 return; 16786 dtrace_module_unloaded(lf, error); 16787 } 16788 #endif 16789 16790 #ifdef illumos 16791 static void 16792 dtrace_suspend(void) 16793 { 16794 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 16795 } 16796 16797 static void 16798 dtrace_resume(void) 16799 { 16800 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 16801 } 16802 #endif 16803 16804 static int 16805 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 16806 { 16807 ASSERT(MUTEX_HELD(&cpu_lock)); 16808 mutex_enter(&dtrace_lock); 16809 16810 switch (what) { 16811 case CPU_CONFIG: { 16812 dtrace_state_t *state; 16813 dtrace_optval_t *opt, rs, c; 16814 16815 /* 16816 * For now, we only allocate a new buffer for anonymous state. 16817 */ 16818 if ((state = dtrace_anon.dta_state) == NULL) 16819 break; 16820 16821 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 16822 break; 16823 16824 opt = state->dts_options; 16825 c = opt[DTRACEOPT_CPU]; 16826 16827 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 16828 break; 16829 16830 /* 16831 * Regardless of what the actual policy is, we're going to 16832 * temporarily set our resize policy to be manual. We're 16833 * also going to temporarily set our CPU option to denote 16834 * the newly configured CPU. 16835 */ 16836 rs = opt[DTRACEOPT_BUFRESIZE]; 16837 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 16838 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 16839 16840 (void) dtrace_state_buffers(state); 16841 16842 opt[DTRACEOPT_BUFRESIZE] = rs; 16843 opt[DTRACEOPT_CPU] = c; 16844 16845 break; 16846 } 16847 16848 case CPU_UNCONFIG: 16849 /* 16850 * We don't free the buffer in the CPU_UNCONFIG case. (The 16851 * buffer will be freed when the consumer exits.) 16852 */ 16853 break; 16854 16855 default: 16856 break; 16857 } 16858 16859 mutex_exit(&dtrace_lock); 16860 return (0); 16861 } 16862 16863 #ifdef illumos 16864 static void 16865 dtrace_cpu_setup_initial(processorid_t cpu) 16866 { 16867 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 16868 } 16869 #endif 16870 16871 static void 16872 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 16873 { 16874 if (dtrace_toxranges >= dtrace_toxranges_max) { 16875 int osize, nsize; 16876 dtrace_toxrange_t *range; 16877 16878 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16879 16880 if (osize == 0) { 16881 ASSERT(dtrace_toxrange == NULL); 16882 ASSERT(dtrace_toxranges_max == 0); 16883 dtrace_toxranges_max = 1; 16884 } else { 16885 dtrace_toxranges_max <<= 1; 16886 } 16887 16888 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16889 range = kmem_zalloc(nsize, KM_SLEEP); 16890 16891 if (dtrace_toxrange != NULL) { 16892 ASSERT(osize != 0); 16893 bcopy(dtrace_toxrange, range, osize); 16894 kmem_free(dtrace_toxrange, osize); 16895 } 16896 16897 dtrace_toxrange = range; 16898 } 16899 16900 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 16901 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 16902 16903 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 16904 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 16905 dtrace_toxranges++; 16906 } 16907 16908 static void 16909 dtrace_getf_barrier() 16910 { 16911 #ifdef illumos 16912 /* 16913 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 16914 * that contain calls to getf(), this routine will be called on every 16915 * closef() before either the underlying vnode is released or the 16916 * file_t itself is freed. By the time we are here, it is essential 16917 * that the file_t can no longer be accessed from a call to getf() 16918 * in probe context -- that assures that a dtrace_sync() can be used 16919 * to clear out any enablings referring to the old structures. 16920 */ 16921 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 16922 kcred->cr_zone->zone_dtrace_getf != 0) 16923 dtrace_sync(); 16924 #endif 16925 } 16926 16927 /* 16928 * DTrace Driver Cookbook Functions 16929 */ 16930 #ifdef illumos 16931 /*ARGSUSED*/ 16932 static int 16933 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 16934 { 16935 dtrace_provider_id_t id; 16936 dtrace_state_t *state = NULL; 16937 dtrace_enabling_t *enab; 16938 16939 mutex_enter(&cpu_lock); 16940 mutex_enter(&dtrace_provider_lock); 16941 mutex_enter(&dtrace_lock); 16942 16943 if (ddi_soft_state_init(&dtrace_softstate, 16944 sizeof (dtrace_state_t), 0) != 0) { 16945 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 16946 mutex_exit(&cpu_lock); 16947 mutex_exit(&dtrace_provider_lock); 16948 mutex_exit(&dtrace_lock); 16949 return (DDI_FAILURE); 16950 } 16951 16952 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 16953 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 16954 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 16955 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 16956 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 16957 ddi_remove_minor_node(devi, NULL); 16958 ddi_soft_state_fini(&dtrace_softstate); 16959 mutex_exit(&cpu_lock); 16960 mutex_exit(&dtrace_provider_lock); 16961 mutex_exit(&dtrace_lock); 16962 return (DDI_FAILURE); 16963 } 16964 16965 ddi_report_dev(devi); 16966 dtrace_devi = devi; 16967 16968 dtrace_modload = dtrace_module_loaded; 16969 dtrace_modunload = dtrace_module_unloaded; 16970 dtrace_cpu_init = dtrace_cpu_setup_initial; 16971 dtrace_helpers_cleanup = dtrace_helpers_destroy; 16972 dtrace_helpers_fork = dtrace_helpers_duplicate; 16973 dtrace_cpustart_init = dtrace_suspend; 16974 dtrace_cpustart_fini = dtrace_resume; 16975 dtrace_debugger_init = dtrace_suspend; 16976 dtrace_debugger_fini = dtrace_resume; 16977 16978 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16979 16980 ASSERT(MUTEX_HELD(&cpu_lock)); 16981 16982 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 16983 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 16984 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 16985 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 16986 VM_SLEEP | VMC_IDENTIFIER); 16987 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 16988 1, INT_MAX, 0); 16989 16990 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 16991 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 16992 NULL, NULL, NULL, NULL, NULL, 0); 16993 16994 ASSERT(MUTEX_HELD(&cpu_lock)); 16995 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 16996 offsetof(dtrace_probe_t, dtpr_nextmod), 16997 offsetof(dtrace_probe_t, dtpr_prevmod)); 16998 16999 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 17000 offsetof(dtrace_probe_t, dtpr_nextfunc), 17001 offsetof(dtrace_probe_t, dtpr_prevfunc)); 17002 17003 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 17004 offsetof(dtrace_probe_t, dtpr_nextname), 17005 offsetof(dtrace_probe_t, dtpr_prevname)); 17006 17007 if (dtrace_retain_max < 1) { 17008 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 17009 "setting to 1", dtrace_retain_max); 17010 dtrace_retain_max = 1; 17011 } 17012 17013 /* 17014 * Now discover our toxic ranges. 17015 */ 17016 dtrace_toxic_ranges(dtrace_toxrange_add); 17017 17018 /* 17019 * Before we register ourselves as a provider to our own framework, 17020 * we would like to assert that dtrace_provider is NULL -- but that's 17021 * not true if we were loaded as a dependency of a DTrace provider. 17022 * Once we've registered, we can assert that dtrace_provider is our 17023 * pseudo provider. 17024 */ 17025 (void) dtrace_register("dtrace", &dtrace_provider_attr, 17026 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 17027 17028 ASSERT(dtrace_provider != NULL); 17029 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 17030 17031 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 17032 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 17033 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 17034 dtrace_provider, NULL, NULL, "END", 0, NULL); 17035 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 17036 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 17037 17038 dtrace_anon_property(); 17039 mutex_exit(&cpu_lock); 17040 17041 /* 17042 * If there are already providers, we must ask them to provide their 17043 * probes, and then match any anonymous enabling against them. Note 17044 * that there should be no other retained enablings at this time: 17045 * the only retained enablings at this time should be the anonymous 17046 * enabling. 17047 */ 17048 if (dtrace_anon.dta_enabling != NULL) { 17049 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 17050 17051 dtrace_enabling_provide(NULL); 17052 state = dtrace_anon.dta_state; 17053 17054 /* 17055 * We couldn't hold cpu_lock across the above call to 17056 * dtrace_enabling_provide(), but we must hold it to actually 17057 * enable the probes. We have to drop all of our locks, pick 17058 * up cpu_lock, and regain our locks before matching the 17059 * retained anonymous enabling. 17060 */ 17061 mutex_exit(&dtrace_lock); 17062 mutex_exit(&dtrace_provider_lock); 17063 17064 mutex_enter(&cpu_lock); 17065 mutex_enter(&dtrace_provider_lock); 17066 mutex_enter(&dtrace_lock); 17067 17068 if ((enab = dtrace_anon.dta_enabling) != NULL) 17069 (void) dtrace_enabling_match(enab, NULL); 17070 17071 mutex_exit(&cpu_lock); 17072 } 17073 17074 mutex_exit(&dtrace_lock); 17075 mutex_exit(&dtrace_provider_lock); 17076 17077 if (state != NULL) { 17078 /* 17079 * If we created any anonymous state, set it going now. 17080 */ 17081 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 17082 } 17083 17084 return (DDI_SUCCESS); 17085 } 17086 #endif /* illumos */ 17087 17088 #ifndef illumos 17089 static void dtrace_dtr(void *); 17090 #endif 17091 17092 /*ARGSUSED*/ 17093 static int 17094 #ifdef illumos 17095 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 17096 #else 17097 dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 17098 #endif 17099 { 17100 dtrace_state_t *state; 17101 uint32_t priv; 17102 uid_t uid; 17103 zoneid_t zoneid; 17104 17105 #ifdef illumos 17106 if (getminor(*devp) == DTRACEMNRN_HELPER) 17107 return (0); 17108 17109 /* 17110 * If this wasn't an open with the "helper" minor, then it must be 17111 * the "dtrace" minor. 17112 */ 17113 if (getminor(*devp) == DTRACEMNRN_DTRACE) 17114 return (ENXIO); 17115 #else 17116 cred_t *cred_p = NULL; 17117 cred_p = dev->si_cred; 17118 17119 /* 17120 * If no DTRACE_PRIV_* bits are set in the credential, then the 17121 * caller lacks sufficient permission to do anything with DTrace. 17122 */ 17123 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 17124 if (priv == DTRACE_PRIV_NONE) { 17125 #endif 17126 17127 return (EACCES); 17128 } 17129 17130 /* 17131 * Ask all providers to provide all their probes. 17132 */ 17133 mutex_enter(&dtrace_provider_lock); 17134 dtrace_probe_provide(NULL, NULL); 17135 mutex_exit(&dtrace_provider_lock); 17136 17137 mutex_enter(&cpu_lock); 17138 mutex_enter(&dtrace_lock); 17139 dtrace_opens++; 17140 dtrace_membar_producer(); 17141 17142 #ifdef illumos 17143 /* 17144 * If the kernel debugger is active (that is, if the kernel debugger 17145 * modified text in some way), we won't allow the open. 17146 */ 17147 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 17148 dtrace_opens--; 17149 mutex_exit(&cpu_lock); 17150 mutex_exit(&dtrace_lock); 17151 return (EBUSY); 17152 } 17153 17154 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) { 17155 /* 17156 * If DTrace helper tracing is enabled, we need to allocate the 17157 * trace buffer and initialize the values. 17158 */ 17159 dtrace_helptrace_buffer = 17160 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 17161 dtrace_helptrace_next = 0; 17162 dtrace_helptrace_wrapped = 0; 17163 dtrace_helptrace_enable = 0; 17164 } 17165 17166 state = dtrace_state_create(devp, cred_p); 17167 #else 17168 state = dtrace_state_create(dev, NULL); 17169 devfs_set_cdevpriv(state, dtrace_dtr); 17170 #endif 17171 17172 mutex_exit(&cpu_lock); 17173 17174 if (state == NULL) { 17175 #ifdef illumos 17176 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17177 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17178 #else 17179 --dtrace_opens; 17180 #endif 17181 mutex_exit(&dtrace_lock); 17182 return (EAGAIN); 17183 } 17184 17185 mutex_exit(&dtrace_lock); 17186 17187 return (0); 17188 } 17189 17190 /*ARGSUSED*/ 17191 #ifdef illumos 17192 static int 17193 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 17194 #else 17195 static void 17196 dtrace_dtr(void *data) 17197 #endif 17198 { 17199 #ifdef illumos 17200 minor_t minor = getminor(dev); 17201 dtrace_state_t *state; 17202 #endif 17203 dtrace_helptrace_t *buf = NULL; 17204 17205 #ifdef illumos 17206 if (minor == DTRACEMNRN_HELPER) 17207 return (0); 17208 17209 state = ddi_get_soft_state(dtrace_softstate, minor); 17210 #else 17211 dtrace_state_t *state = data; 17212 #endif 17213 17214 mutex_enter(&cpu_lock); 17215 mutex_enter(&dtrace_lock); 17216 17217 #ifdef illumos 17218 if (state->dts_anon) 17219 #else 17220 if (state != NULL && state->dts_anon) 17221 #endif 17222 { 17223 /* 17224 * There is anonymous state. Destroy that first. 17225 */ 17226 ASSERT(dtrace_anon.dta_state == NULL); 17227 dtrace_state_destroy(state->dts_anon); 17228 } 17229 17230 if (dtrace_helptrace_disable) { 17231 /* 17232 * If we have been told to disable helper tracing, set the 17233 * buffer to NULL before calling into dtrace_state_destroy(); 17234 * we take advantage of its dtrace_sync() to know that no 17235 * CPU is in probe context with enabled helper tracing 17236 * after it returns. 17237 */ 17238 buf = dtrace_helptrace_buffer; 17239 dtrace_helptrace_buffer = NULL; 17240 } 17241 17242 #ifdef illumos 17243 dtrace_state_destroy(state); 17244 #else 17245 if (state != NULL) { 17246 dtrace_state_destroy(state); 17247 kmem_free(state, 0); 17248 } 17249 #endif 17250 ASSERT(dtrace_opens > 0); 17251 17252 #ifdef illumos 17253 /* 17254 * Only relinquish control of the kernel debugger interface when there 17255 * are no consumers and no anonymous enablings. 17256 */ 17257 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17258 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17259 #else 17260 --dtrace_opens; 17261 #endif 17262 17263 if (buf != NULL) { 17264 kmem_free(buf, dtrace_helptrace_bufsize); 17265 dtrace_helptrace_disable = 0; 17266 } 17267 17268 mutex_exit(&dtrace_lock); 17269 mutex_exit(&cpu_lock); 17270 17271 #ifdef illumos 17272 return (0); 17273 #endif 17274 } 17275 17276 #ifdef illumos 17277 /*ARGSUSED*/ 17278 static int 17279 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 17280 { 17281 int rval; 17282 dof_helper_t help, *dhp = NULL; 17283 17284 switch (cmd) { 17285 case DTRACEHIOC_ADDDOF: 17286 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 17287 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 17288 return (EFAULT); 17289 } 17290 17291 dhp = &help; 17292 arg = (intptr_t)help.dofhp_dof; 17293 /*FALLTHROUGH*/ 17294 17295 case DTRACEHIOC_ADD: { 17296 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 17297 17298 if (dof == NULL) 17299 return (rval); 17300 17301 mutex_enter(&dtrace_lock); 17302 17303 /* 17304 * dtrace_helper_slurp() takes responsibility for the dof -- 17305 * it may free it now or it may save it and free it later. 17306 */ 17307 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 17308 *rv = rval; 17309 rval = 0; 17310 } else { 17311 rval = EINVAL; 17312 } 17313 17314 mutex_exit(&dtrace_lock); 17315 return (rval); 17316 } 17317 17318 case DTRACEHIOC_REMOVE: { 17319 mutex_enter(&dtrace_lock); 17320 rval = dtrace_helper_destroygen(NULL, arg); 17321 mutex_exit(&dtrace_lock); 17322 17323 return (rval); 17324 } 17325 17326 default: 17327 break; 17328 } 17329 17330 return (ENOTTY); 17331 } 17332 17333 /*ARGSUSED*/ 17334 static int 17335 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 17336 { 17337 minor_t minor = getminor(dev); 17338 dtrace_state_t *state; 17339 int rval; 17340 17341 if (minor == DTRACEMNRN_HELPER) 17342 return (dtrace_ioctl_helper(cmd, arg, rv)); 17343 17344 state = ddi_get_soft_state(dtrace_softstate, minor); 17345 17346 if (state->dts_anon) { 17347 ASSERT(dtrace_anon.dta_state == NULL); 17348 state = state->dts_anon; 17349 } 17350 17351 switch (cmd) { 17352 case DTRACEIOC_PROVIDER: { 17353 dtrace_providerdesc_t pvd; 17354 dtrace_provider_t *pvp; 17355 17356 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 17357 return (EFAULT); 17358 17359 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 17360 mutex_enter(&dtrace_provider_lock); 17361 17362 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 17363 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 17364 break; 17365 } 17366 17367 mutex_exit(&dtrace_provider_lock); 17368 17369 if (pvp == NULL) 17370 return (ESRCH); 17371 17372 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 17373 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 17374 17375 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 17376 return (EFAULT); 17377 17378 return (0); 17379 } 17380 17381 case DTRACEIOC_EPROBE: { 17382 dtrace_eprobedesc_t epdesc; 17383 dtrace_ecb_t *ecb; 17384 dtrace_action_t *act; 17385 void *buf; 17386 size_t size; 17387 uintptr_t dest; 17388 int nrecs; 17389 17390 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 17391 return (EFAULT); 17392 17393 mutex_enter(&dtrace_lock); 17394 17395 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 17396 mutex_exit(&dtrace_lock); 17397 return (EINVAL); 17398 } 17399 17400 if (ecb->dte_probe == NULL) { 17401 mutex_exit(&dtrace_lock); 17402 return (EINVAL); 17403 } 17404 17405 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 17406 epdesc.dtepd_uarg = ecb->dte_uarg; 17407 epdesc.dtepd_size = ecb->dte_size; 17408 17409 nrecs = epdesc.dtepd_nrecs; 17410 epdesc.dtepd_nrecs = 0; 17411 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17412 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17413 continue; 17414 17415 epdesc.dtepd_nrecs++; 17416 } 17417 17418 /* 17419 * Now that we have the size, we need to allocate a temporary 17420 * buffer in which to store the complete description. We need 17421 * the temporary buffer to be able to drop dtrace_lock() 17422 * across the copyout(), below. 17423 */ 17424 size = sizeof (dtrace_eprobedesc_t) + 17425 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 17426 17427 buf = kmem_alloc(size, KM_SLEEP); 17428 dest = (uintptr_t)buf; 17429 17430 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 17431 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 17432 17433 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17434 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17435 continue; 17436 17437 if (nrecs-- == 0) 17438 break; 17439 17440 bcopy(&act->dta_rec, (void *)dest, 17441 sizeof (dtrace_recdesc_t)); 17442 dest += sizeof (dtrace_recdesc_t); 17443 } 17444 17445 mutex_exit(&dtrace_lock); 17446 17447 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17448 kmem_free(buf, size); 17449 return (EFAULT); 17450 } 17451 17452 kmem_free(buf, size); 17453 return (0); 17454 } 17455 17456 case DTRACEIOC_AGGDESC: { 17457 dtrace_aggdesc_t aggdesc; 17458 dtrace_action_t *act; 17459 dtrace_aggregation_t *agg; 17460 int nrecs; 17461 uint32_t offs; 17462 dtrace_recdesc_t *lrec; 17463 void *buf; 17464 size_t size; 17465 uintptr_t dest; 17466 17467 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 17468 return (EFAULT); 17469 17470 mutex_enter(&dtrace_lock); 17471 17472 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 17473 mutex_exit(&dtrace_lock); 17474 return (EINVAL); 17475 } 17476 17477 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 17478 17479 nrecs = aggdesc.dtagd_nrecs; 17480 aggdesc.dtagd_nrecs = 0; 17481 17482 offs = agg->dtag_base; 17483 lrec = &agg->dtag_action.dta_rec; 17484 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 17485 17486 for (act = agg->dtag_first; ; act = act->dta_next) { 17487 ASSERT(act->dta_intuple || 17488 DTRACEACT_ISAGG(act->dta_kind)); 17489 17490 /* 17491 * If this action has a record size of zero, it 17492 * denotes an argument to the aggregating action. 17493 * Because the presence of this record doesn't (or 17494 * shouldn't) affect the way the data is interpreted, 17495 * we don't copy it out to save user-level the 17496 * confusion of dealing with a zero-length record. 17497 */ 17498 if (act->dta_rec.dtrd_size == 0) { 17499 ASSERT(agg->dtag_hasarg); 17500 continue; 17501 } 17502 17503 aggdesc.dtagd_nrecs++; 17504 17505 if (act == &agg->dtag_action) 17506 break; 17507 } 17508 17509 /* 17510 * Now that we have the size, we need to allocate a temporary 17511 * buffer in which to store the complete description. We need 17512 * the temporary buffer to be able to drop dtrace_lock() 17513 * across the copyout(), below. 17514 */ 17515 size = sizeof (dtrace_aggdesc_t) + 17516 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 17517 17518 buf = kmem_alloc(size, KM_SLEEP); 17519 dest = (uintptr_t)buf; 17520 17521 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 17522 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 17523 17524 for (act = agg->dtag_first; ; act = act->dta_next) { 17525 dtrace_recdesc_t rec = act->dta_rec; 17526 17527 /* 17528 * See the comment in the above loop for why we pass 17529 * over zero-length records. 17530 */ 17531 if (rec.dtrd_size == 0) { 17532 ASSERT(agg->dtag_hasarg); 17533 continue; 17534 } 17535 17536 if (nrecs-- == 0) 17537 break; 17538 17539 rec.dtrd_offset -= offs; 17540 bcopy(&rec, (void *)dest, sizeof (rec)); 17541 dest += sizeof (dtrace_recdesc_t); 17542 17543 if (act == &agg->dtag_action) 17544 break; 17545 } 17546 17547 mutex_exit(&dtrace_lock); 17548 17549 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17550 kmem_free(buf, size); 17551 return (EFAULT); 17552 } 17553 17554 kmem_free(buf, size); 17555 return (0); 17556 } 17557 17558 case DTRACEIOC_ENABLE: { 17559 dof_hdr_t *dof; 17560 dtrace_enabling_t *enab = NULL; 17561 dtrace_vstate_t *vstate; 17562 int err = 0; 17563 17564 *rv = 0; 17565 17566 /* 17567 * If a NULL argument has been passed, we take this as our 17568 * cue to reevaluate our enablings. 17569 */ 17570 if (arg == NULL) { 17571 dtrace_enabling_matchall(); 17572 17573 return (0); 17574 } 17575 17576 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 17577 return (rval); 17578 17579 mutex_enter(&cpu_lock); 17580 mutex_enter(&dtrace_lock); 17581 vstate = &state->dts_vstate; 17582 17583 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 17584 mutex_exit(&dtrace_lock); 17585 mutex_exit(&cpu_lock); 17586 dtrace_dof_destroy(dof); 17587 return (EBUSY); 17588 } 17589 17590 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 17591 mutex_exit(&dtrace_lock); 17592 mutex_exit(&cpu_lock); 17593 dtrace_dof_destroy(dof); 17594 return (EINVAL); 17595 } 17596 17597 if ((rval = dtrace_dof_options(dof, state)) != 0) { 17598 dtrace_enabling_destroy(enab); 17599 mutex_exit(&dtrace_lock); 17600 mutex_exit(&cpu_lock); 17601 dtrace_dof_destroy(dof); 17602 return (rval); 17603 } 17604 17605 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 17606 err = dtrace_enabling_retain(enab); 17607 } else { 17608 dtrace_enabling_destroy(enab); 17609 } 17610 17611 mutex_exit(&cpu_lock); 17612 mutex_exit(&dtrace_lock); 17613 dtrace_dof_destroy(dof); 17614 17615 return (err); 17616 } 17617 17618 case DTRACEIOC_REPLICATE: { 17619 dtrace_repldesc_t desc; 17620 dtrace_probedesc_t *match = &desc.dtrpd_match; 17621 dtrace_probedesc_t *create = &desc.dtrpd_create; 17622 int err; 17623 17624 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17625 return (EFAULT); 17626 17627 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17628 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17629 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17630 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17631 17632 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17633 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17634 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17635 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17636 17637 mutex_enter(&dtrace_lock); 17638 err = dtrace_enabling_replicate(state, match, create); 17639 mutex_exit(&dtrace_lock); 17640 17641 return (err); 17642 } 17643 17644 case DTRACEIOC_PROBEMATCH: 17645 case DTRACEIOC_PROBES: { 17646 dtrace_probe_t *probe = NULL; 17647 dtrace_probedesc_t desc; 17648 dtrace_probekey_t pkey; 17649 dtrace_id_t i; 17650 int m = 0; 17651 uint32_t priv; 17652 uid_t uid; 17653 zoneid_t zoneid; 17654 17655 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17656 return (EFAULT); 17657 17658 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17659 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17660 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17661 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17662 17663 /* 17664 * Before we attempt to match this probe, we want to give 17665 * all providers the opportunity to provide it. 17666 */ 17667 if (desc.dtpd_id == DTRACE_IDNONE) { 17668 mutex_enter(&dtrace_provider_lock); 17669 dtrace_probe_provide(&desc, NULL); 17670 mutex_exit(&dtrace_provider_lock); 17671 desc.dtpd_id++; 17672 } 17673 17674 if (cmd == DTRACEIOC_PROBEMATCH) { 17675 dtrace_probekey(&desc, &pkey); 17676 pkey.dtpk_id = DTRACE_IDNONE; 17677 } 17678 17679 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 17680 17681 mutex_enter(&dtrace_lock); 17682 17683 if (cmd == DTRACEIOC_PROBEMATCH) { 17684 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17685 if ((probe = dtrace_probes[i - 1]) != NULL && 17686 (m = dtrace_match_probe(probe, &pkey, 17687 priv, uid, zoneid)) != 0) 17688 break; 17689 } 17690 17691 if (m < 0) { 17692 mutex_exit(&dtrace_lock); 17693 return (EINVAL); 17694 } 17695 17696 } else { 17697 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17698 if ((probe = dtrace_probes[i - 1]) != NULL && 17699 dtrace_match_priv(probe, priv, uid, zoneid)) 17700 break; 17701 } 17702 } 17703 17704 if (probe == NULL) { 17705 mutex_exit(&dtrace_lock); 17706 return (ESRCH); 17707 } 17708 17709 dtrace_probe_description(probe, &desc); 17710 mutex_exit(&dtrace_lock); 17711 17712 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17713 return (EFAULT); 17714 17715 return (0); 17716 } 17717 17718 case DTRACEIOC_PROBEARG: { 17719 dtrace_argdesc_t desc; 17720 dtrace_probe_t *probe; 17721 dtrace_provider_t *prov; 17722 17723 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17724 return (EFAULT); 17725 17726 if (desc.dtargd_id == DTRACE_IDNONE) 17727 return (EINVAL); 17728 17729 if (desc.dtargd_ndx == DTRACE_ARGNONE) 17730 return (EINVAL); 17731 17732 mutex_enter(&dtrace_provider_lock); 17733 mutex_enter(&mod_lock); 17734 mutex_enter(&dtrace_lock); 17735 17736 if (desc.dtargd_id > dtrace_nprobes) { 17737 mutex_exit(&dtrace_lock); 17738 mutex_exit(&mod_lock); 17739 mutex_exit(&dtrace_provider_lock); 17740 return (EINVAL); 17741 } 17742 17743 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 17744 mutex_exit(&dtrace_lock); 17745 mutex_exit(&mod_lock); 17746 mutex_exit(&dtrace_provider_lock); 17747 return (EINVAL); 17748 } 17749 17750 mutex_exit(&dtrace_lock); 17751 17752 prov = probe->dtpr_provider; 17753 17754 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 17755 /* 17756 * There isn't any typed information for this probe. 17757 * Set the argument number to DTRACE_ARGNONE. 17758 */ 17759 desc.dtargd_ndx = DTRACE_ARGNONE; 17760 } else { 17761 desc.dtargd_native[0] = '\0'; 17762 desc.dtargd_xlate[0] = '\0'; 17763 desc.dtargd_mapping = desc.dtargd_ndx; 17764 17765 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 17766 probe->dtpr_id, probe->dtpr_arg, &desc); 17767 } 17768 17769 mutex_exit(&mod_lock); 17770 mutex_exit(&dtrace_provider_lock); 17771 17772 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17773 return (EFAULT); 17774 17775 return (0); 17776 } 17777 17778 case DTRACEIOC_GO: { 17779 processorid_t cpuid; 17780 rval = dtrace_state_go(state, &cpuid); 17781 17782 if (rval != 0) 17783 return (rval); 17784 17785 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17786 return (EFAULT); 17787 17788 return (0); 17789 } 17790 17791 case DTRACEIOC_STOP: { 17792 processorid_t cpuid; 17793 17794 mutex_enter(&dtrace_lock); 17795 rval = dtrace_state_stop(state, &cpuid); 17796 mutex_exit(&dtrace_lock); 17797 17798 if (rval != 0) 17799 return (rval); 17800 17801 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17802 return (EFAULT); 17803 17804 return (0); 17805 } 17806 17807 case DTRACEIOC_DOFGET: { 17808 dof_hdr_t hdr, *dof; 17809 uint64_t len; 17810 17811 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 17812 return (EFAULT); 17813 17814 mutex_enter(&dtrace_lock); 17815 dof = dtrace_dof_create(state); 17816 mutex_exit(&dtrace_lock); 17817 17818 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 17819 rval = copyout(dof, (void *)arg, len); 17820 dtrace_dof_destroy(dof); 17821 17822 return (rval == 0 ? 0 : EFAULT); 17823 } 17824 17825 case DTRACEIOC_AGGSNAP: 17826 case DTRACEIOC_BUFSNAP: { 17827 dtrace_bufdesc_t desc; 17828 caddr_t cached; 17829 dtrace_buffer_t *buf; 17830 17831 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17832 return (EFAULT); 17833 17834 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 17835 return (EINVAL); 17836 17837 mutex_enter(&dtrace_lock); 17838 17839 if (cmd == DTRACEIOC_BUFSNAP) { 17840 buf = &state->dts_buffer[desc.dtbd_cpu]; 17841 } else { 17842 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 17843 } 17844 17845 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 17846 size_t sz = buf->dtb_offset; 17847 17848 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 17849 mutex_exit(&dtrace_lock); 17850 return (EBUSY); 17851 } 17852 17853 /* 17854 * If this buffer has already been consumed, we're 17855 * going to indicate that there's nothing left here 17856 * to consume. 17857 */ 17858 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 17859 mutex_exit(&dtrace_lock); 17860 17861 desc.dtbd_size = 0; 17862 desc.dtbd_drops = 0; 17863 desc.dtbd_errors = 0; 17864 desc.dtbd_oldest = 0; 17865 sz = sizeof (desc); 17866 17867 if (copyout(&desc, (void *)arg, sz) != 0) 17868 return (EFAULT); 17869 17870 return (0); 17871 } 17872 17873 /* 17874 * If this is a ring buffer that has wrapped, we want 17875 * to copy the whole thing out. 17876 */ 17877 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 17878 dtrace_buffer_polish(buf); 17879 sz = buf->dtb_size; 17880 } 17881 17882 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 17883 mutex_exit(&dtrace_lock); 17884 return (EFAULT); 17885 } 17886 17887 desc.dtbd_size = sz; 17888 desc.dtbd_drops = buf->dtb_drops; 17889 desc.dtbd_errors = buf->dtb_errors; 17890 desc.dtbd_oldest = buf->dtb_xamot_offset; 17891 desc.dtbd_timestamp = dtrace_gethrtime(); 17892 17893 mutex_exit(&dtrace_lock); 17894 17895 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17896 return (EFAULT); 17897 17898 buf->dtb_flags |= DTRACEBUF_CONSUMED; 17899 17900 return (0); 17901 } 17902 17903 if (buf->dtb_tomax == NULL) { 17904 ASSERT(buf->dtb_xamot == NULL); 17905 mutex_exit(&dtrace_lock); 17906 return (ENOENT); 17907 } 17908 17909 cached = buf->dtb_tomax; 17910 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 17911 17912 dtrace_xcall(desc.dtbd_cpu, 17913 (dtrace_xcall_t)dtrace_buffer_switch, buf); 17914 17915 state->dts_errors += buf->dtb_xamot_errors; 17916 17917 /* 17918 * If the buffers did not actually switch, then the cross call 17919 * did not take place -- presumably because the given CPU is 17920 * not in the ready set. If this is the case, we'll return 17921 * ENOENT. 17922 */ 17923 if (buf->dtb_tomax == cached) { 17924 ASSERT(buf->dtb_xamot != cached); 17925 mutex_exit(&dtrace_lock); 17926 return (ENOENT); 17927 } 17928 17929 ASSERT(cached == buf->dtb_xamot); 17930 17931 /* 17932 * We have our snapshot; now copy it out. 17933 */ 17934 if (copyout(buf->dtb_xamot, desc.dtbd_data, 17935 buf->dtb_xamot_offset) != 0) { 17936 mutex_exit(&dtrace_lock); 17937 return (EFAULT); 17938 } 17939 17940 desc.dtbd_size = buf->dtb_xamot_offset; 17941 desc.dtbd_drops = buf->dtb_xamot_drops; 17942 desc.dtbd_errors = buf->dtb_xamot_errors; 17943 desc.dtbd_oldest = 0; 17944 desc.dtbd_timestamp = buf->dtb_switched; 17945 17946 mutex_exit(&dtrace_lock); 17947 17948 /* 17949 * Finally, copy out the buffer description. 17950 */ 17951 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17952 return (EFAULT); 17953 17954 return (0); 17955 } 17956 17957 case DTRACEIOC_CONF: { 17958 dtrace_conf_t conf; 17959 17960 bzero(&conf, sizeof (conf)); 17961 conf.dtc_difversion = DIF_VERSION; 17962 conf.dtc_difintregs = DIF_DIR_NREGS; 17963 conf.dtc_diftupregs = DIF_DTR_NREGS; 17964 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 17965 17966 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 17967 return (EFAULT); 17968 17969 return (0); 17970 } 17971 17972 case DTRACEIOC_STATUS: { 17973 dtrace_status_t stat; 17974 dtrace_dstate_t *dstate; 17975 int i, j; 17976 uint64_t nerrs; 17977 17978 /* 17979 * See the comment in dtrace_state_deadman() for the reason 17980 * for setting dts_laststatus to INT64_MAX before setting 17981 * it to the correct value. 17982 */ 17983 state->dts_laststatus = INT64_MAX; 17984 dtrace_membar_producer(); 17985 state->dts_laststatus = dtrace_gethrtime(); 17986 17987 bzero(&stat, sizeof (stat)); 17988 17989 mutex_enter(&dtrace_lock); 17990 17991 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 17992 mutex_exit(&dtrace_lock); 17993 return (ENOENT); 17994 } 17995 17996 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 17997 stat.dtst_exiting = 1; 17998 17999 nerrs = state->dts_errors; 18000 dstate = &state->dts_vstate.dtvs_dynvars; 18001 18002 for (i = 0; i < NCPU; i++) { 18003 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 18004 18005 stat.dtst_dyndrops += dcpu->dtdsc_drops; 18006 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 18007 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 18008 18009 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 18010 stat.dtst_filled++; 18011 18012 nerrs += state->dts_buffer[i].dtb_errors; 18013 18014 for (j = 0; j < state->dts_nspeculations; j++) { 18015 dtrace_speculation_t *spec; 18016 dtrace_buffer_t *buf; 18017 18018 spec = &state->dts_speculations[j]; 18019 buf = &spec->dtsp_buffer[i]; 18020 stat.dtst_specdrops += buf->dtb_xamot_drops; 18021 } 18022 } 18023 18024 stat.dtst_specdrops_busy = state->dts_speculations_busy; 18025 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 18026 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 18027 stat.dtst_dblerrors = state->dts_dblerrors; 18028 stat.dtst_killed = 18029 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 18030 stat.dtst_errors = nerrs; 18031 18032 mutex_exit(&dtrace_lock); 18033 18034 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 18035 return (EFAULT); 18036 18037 return (0); 18038 } 18039 18040 case DTRACEIOC_FORMAT: { 18041 dtrace_fmtdesc_t fmt; 18042 char *str; 18043 int len; 18044 18045 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 18046 return (EFAULT); 18047 18048 mutex_enter(&dtrace_lock); 18049 18050 if (fmt.dtfd_format == 0 || 18051 fmt.dtfd_format > state->dts_nformats) { 18052 mutex_exit(&dtrace_lock); 18053 return (EINVAL); 18054 } 18055 18056 /* 18057 * Format strings are allocated contiguously and they are 18058 * never freed; if a format index is less than the number 18059 * of formats, we can assert that the format map is non-NULL 18060 * and that the format for the specified index is non-NULL. 18061 */ 18062 ASSERT(state->dts_formats != NULL); 18063 str = state->dts_formats[fmt.dtfd_format - 1]; 18064 ASSERT(str != NULL); 18065 18066 len = strlen(str) + 1; 18067 18068 if (len > fmt.dtfd_length) { 18069 fmt.dtfd_length = len; 18070 18071 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 18072 mutex_exit(&dtrace_lock); 18073 return (EINVAL); 18074 } 18075 } else { 18076 if (copyout(str, fmt.dtfd_string, len) != 0) { 18077 mutex_exit(&dtrace_lock); 18078 return (EINVAL); 18079 } 18080 } 18081 18082 mutex_exit(&dtrace_lock); 18083 return (0); 18084 } 18085 18086 default: 18087 break; 18088 } 18089 18090 return (ENOTTY); 18091 } 18092 18093 /*ARGSUSED*/ 18094 static int 18095 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 18096 { 18097 dtrace_state_t *state; 18098 18099 switch (cmd) { 18100 case DDI_DETACH: 18101 break; 18102 18103 case DDI_SUSPEND: 18104 return (DDI_SUCCESS); 18105 18106 default: 18107 return (DDI_FAILURE); 18108 } 18109 18110 mutex_enter(&cpu_lock); 18111 mutex_enter(&dtrace_provider_lock); 18112 mutex_enter(&dtrace_lock); 18113 18114 ASSERT(dtrace_opens == 0); 18115 18116 if (dtrace_helpers > 0) { 18117 mutex_exit(&dtrace_provider_lock); 18118 mutex_exit(&dtrace_lock); 18119 mutex_exit(&cpu_lock); 18120 return (DDI_FAILURE); 18121 } 18122 18123 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 18124 mutex_exit(&dtrace_provider_lock); 18125 mutex_exit(&dtrace_lock); 18126 mutex_exit(&cpu_lock); 18127 return (DDI_FAILURE); 18128 } 18129 18130 dtrace_provider = NULL; 18131 18132 if ((state = dtrace_anon_grab()) != NULL) { 18133 /* 18134 * If there were ECBs on this state, the provider should 18135 * have not been allowed to detach; assert that there is 18136 * none. 18137 */ 18138 ASSERT(state->dts_necbs == 0); 18139 dtrace_state_destroy(state); 18140 18141 /* 18142 * If we're being detached with anonymous state, we need to 18143 * indicate to the kernel debugger that DTrace is now inactive. 18144 */ 18145 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 18146 } 18147 18148 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 18149 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 18150 dtrace_cpu_init = NULL; 18151 dtrace_helpers_cleanup = NULL; 18152 dtrace_helpers_fork = NULL; 18153 dtrace_cpustart_init = NULL; 18154 dtrace_cpustart_fini = NULL; 18155 dtrace_debugger_init = NULL; 18156 dtrace_debugger_fini = NULL; 18157 dtrace_modload = NULL; 18158 dtrace_modunload = NULL; 18159 18160 ASSERT(dtrace_getf == 0); 18161 ASSERT(dtrace_closef == NULL); 18162 18163 mutex_exit(&cpu_lock); 18164 18165 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 18166 dtrace_probes = NULL; 18167 dtrace_nprobes = 0; 18168 18169 dtrace_hash_destroy(dtrace_bymod); 18170 dtrace_hash_destroy(dtrace_byfunc); 18171 dtrace_hash_destroy(dtrace_byname); 18172 dtrace_bymod = NULL; 18173 dtrace_byfunc = NULL; 18174 dtrace_byname = NULL; 18175 18176 kmem_cache_destroy(dtrace_state_cache); 18177 vmem_destroy(dtrace_minor); 18178 vmem_destroy(dtrace_arena); 18179 18180 if (dtrace_toxrange != NULL) { 18181 kmem_free(dtrace_toxrange, 18182 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 18183 dtrace_toxrange = NULL; 18184 dtrace_toxranges = 0; 18185 dtrace_toxranges_max = 0; 18186 } 18187 18188 ddi_remove_minor_node(dtrace_devi, NULL); 18189 dtrace_devi = NULL; 18190 18191 ddi_soft_state_fini(&dtrace_softstate); 18192 18193 ASSERT(dtrace_vtime_references == 0); 18194 ASSERT(dtrace_opens == 0); 18195 ASSERT(dtrace_retained == NULL); 18196 18197 mutex_exit(&dtrace_lock); 18198 mutex_exit(&dtrace_provider_lock); 18199 18200 /* 18201 * We don't destroy the task queue until after we have dropped our 18202 * locks (taskq_destroy() may block on running tasks). To prevent 18203 * attempting to do work after we have effectively detached but before 18204 * the task queue has been destroyed, all tasks dispatched via the 18205 * task queue must check that DTrace is still attached before 18206 * performing any operation. 18207 */ 18208 taskq_destroy(dtrace_taskq); 18209 dtrace_taskq = NULL; 18210 18211 return (DDI_SUCCESS); 18212 } 18213 #endif 18214 18215 #ifdef illumos 18216 /*ARGSUSED*/ 18217 static int 18218 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 18219 { 18220 int error; 18221 18222 switch (infocmd) { 18223 case DDI_INFO_DEVT2DEVINFO: 18224 *result = (void *)dtrace_devi; 18225 error = DDI_SUCCESS; 18226 break; 18227 case DDI_INFO_DEVT2INSTANCE: 18228 *result = (void *)0; 18229 error = DDI_SUCCESS; 18230 break; 18231 default: 18232 error = DDI_FAILURE; 18233 } 18234 return (error); 18235 } 18236 #endif 18237 18238 #ifdef illumos 18239 static struct cb_ops dtrace_cb_ops = { 18240 dtrace_open, /* open */ 18241 dtrace_close, /* close */ 18242 nulldev, /* strategy */ 18243 nulldev, /* print */ 18244 nodev, /* dump */ 18245 nodev, /* read */ 18246 nodev, /* write */ 18247 dtrace_ioctl, /* ioctl */ 18248 nodev, /* devmap */ 18249 nodev, /* mmap */ 18250 nodev, /* segmap */ 18251 nochpoll, /* poll */ 18252 ddi_prop_op, /* cb_prop_op */ 18253 0, /* streamtab */ 18254 D_NEW | D_MP /* Driver compatibility flag */ 18255 }; 18256 18257 static struct dev_ops dtrace_ops = { 18258 DEVO_REV, /* devo_rev */ 18259 0, /* refcnt */ 18260 dtrace_info, /* get_dev_info */ 18261 nulldev, /* identify */ 18262 nulldev, /* probe */ 18263 dtrace_attach, /* attach */ 18264 dtrace_detach, /* detach */ 18265 nodev, /* reset */ 18266 &dtrace_cb_ops, /* driver operations */ 18267 NULL, /* bus operations */ 18268 nodev /* dev power */ 18269 }; 18270 18271 static struct modldrv modldrv = { 18272 &mod_driverops, /* module type (this is a pseudo driver) */ 18273 "Dynamic Tracing", /* name of module */ 18274 &dtrace_ops, /* driver ops */ 18275 }; 18276 18277 static struct modlinkage modlinkage = { 18278 MODREV_1, 18279 (void *)&modldrv, 18280 NULL 18281 }; 18282 18283 int 18284 _init(void) 18285 { 18286 return (mod_install(&modlinkage)); 18287 } 18288 18289 int 18290 _info(struct modinfo *modinfop) 18291 { 18292 return (mod_info(&modlinkage, modinfop)); 18293 } 18294 18295 int 18296 _fini(void) 18297 { 18298 return (mod_remove(&modlinkage)); 18299 } 18300 #else 18301 18302 static d_ioctl_t dtrace_ioctl; 18303 static d_ioctl_t dtrace_ioctl_helper; 18304 static void dtrace_load(void *); 18305 static int dtrace_unload(void); 18306 static struct cdev *dtrace_dev; 18307 static struct cdev *helper_dev; 18308 18309 void dtrace_invop_init(void); 18310 void dtrace_invop_uninit(void); 18311 18312 static struct cdevsw dtrace_cdevsw = { 18313 .d_version = D_VERSION, 18314 .d_ioctl = dtrace_ioctl, 18315 .d_open = dtrace_open, 18316 .d_name = "dtrace", 18317 }; 18318 18319 static struct cdevsw helper_cdevsw = { 18320 .d_version = D_VERSION, 18321 .d_ioctl = dtrace_ioctl_helper, 18322 .d_name = "helper", 18323 }; 18324 18325 #include <dtrace_anon.c> 18326 #include <dtrace_ioctl.c> 18327 #include <dtrace_load.c> 18328 #include <dtrace_modevent.c> 18329 #include <dtrace_sysctl.c> 18330 #include <dtrace_unload.c> 18331 #include <dtrace_vtime.c> 18332 #include <dtrace_hacks.c> 18333 #include <dtrace_isa.c> 18334 18335 SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 18336 SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 18337 SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 18338 18339 DEV_MODULE(dtrace, dtrace_modevent, NULL); 18340 MODULE_VERSION(dtrace, 1); 18341 MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 18342 #endif 18343