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 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 161 size_t dtrace_difo_maxsize = (256 * 1024); 162 dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 163 size_t dtrace_statvar_maxsize = (16 * 1024); 164 size_t dtrace_actions_max = (16 * 1024); 165 size_t dtrace_retain_max = 1024; 166 dtrace_optval_t dtrace_helper_actions_max = 128; 167 dtrace_optval_t dtrace_helper_providers_max = 32; 168 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 169 size_t dtrace_strsize_default = 256; 170 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 171 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 172 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 173 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 174 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 175 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 176 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 177 dtrace_optval_t dtrace_nspec_default = 1; 178 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 179 dtrace_optval_t dtrace_stackframes_default = 20; 180 dtrace_optval_t dtrace_ustackframes_default = 20; 181 dtrace_optval_t dtrace_jstackframes_default = 50; 182 dtrace_optval_t dtrace_jstackstrsize_default = 512; 183 int dtrace_msgdsize_max = 128; 184 hrtime_t dtrace_chill_max = MSEC2NSEC(500); /* 500 ms */ 185 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 186 int dtrace_devdepth_max = 32; 187 int dtrace_err_verbose; 188 hrtime_t dtrace_deadman_interval = NANOSEC; 189 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 190 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 191 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 192 #ifndef illumos 193 int dtrace_memstr_max = 4096; 194 #endif 195 196 /* 197 * DTrace External Variables 198 * 199 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 200 * available to DTrace consumers via the backtick (`) syntax. One of these, 201 * dtrace_zero, is made deliberately so: it is provided as a source of 202 * well-known, zero-filled memory. While this variable is not documented, 203 * it is used by some translators as an implementation detail. 204 */ 205 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 206 207 /* 208 * DTrace Internal Variables 209 */ 210 #ifdef illumos 211 static dev_info_t *dtrace_devi; /* device info */ 212 #endif 213 #ifdef illumos 214 static vmem_t *dtrace_arena; /* probe ID arena */ 215 static vmem_t *dtrace_minor; /* minor number arena */ 216 #else 217 static taskq_t *dtrace_taskq; /* task queue */ 218 static struct unrhdr *dtrace_arena; /* Probe ID number. */ 219 #endif 220 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 221 static int dtrace_nprobes; /* number of probes */ 222 static dtrace_provider_t *dtrace_provider; /* provider list */ 223 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 224 static int dtrace_opens; /* number of opens */ 225 static int dtrace_helpers; /* number of helpers */ 226 static int dtrace_getf; /* number of unpriv getf()s */ 227 #ifdef illumos 228 static void *dtrace_softstate; /* softstate pointer */ 229 #endif 230 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 231 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 232 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 233 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 234 static int dtrace_toxranges; /* number of toxic ranges */ 235 static int dtrace_toxranges_max; /* size of toxic range array */ 236 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 237 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 238 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 239 static kthread_t *dtrace_panicked; /* panicking thread */ 240 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 241 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 242 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 243 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 244 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 245 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 246 static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 247 #ifndef illumos 248 static struct mtx dtrace_unr_mtx; 249 MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 250 static eventhandler_tag dtrace_kld_load_tag; 251 static eventhandler_tag dtrace_kld_unload_try_tag; 252 #endif 253 254 /* 255 * DTrace Locking 256 * DTrace is protected by three (relatively coarse-grained) locks: 257 * 258 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 259 * including enabling state, probes, ECBs, consumer state, helper state, 260 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 261 * probe context is lock-free -- synchronization is handled via the 262 * dtrace_sync() cross call mechanism. 263 * 264 * (2) dtrace_provider_lock is required when manipulating provider state, or 265 * when provider state must be held constant. 266 * 267 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 268 * when meta provider state must be held constant. 269 * 270 * The lock ordering between these three locks is dtrace_meta_lock before 271 * dtrace_provider_lock before dtrace_lock. (In particular, there are 272 * several places where dtrace_provider_lock is held by the framework as it 273 * calls into the providers -- which then call back into the framework, 274 * grabbing dtrace_lock.) 275 * 276 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 277 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 278 * role as a coarse-grained lock; it is acquired before both of these locks. 279 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 280 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 281 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 282 * acquired _between_ dtrace_provider_lock and dtrace_lock. 283 */ 284 static kmutex_t dtrace_lock; /* probe state lock */ 285 static kmutex_t dtrace_provider_lock; /* provider state lock */ 286 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 287 288 #ifndef illumos 289 /* XXX FreeBSD hacks. */ 290 #define cr_suid cr_svuid 291 #define cr_sgid cr_svgid 292 #define ipaddr_t in_addr_t 293 #define mod_modname pathname 294 #define vuprintf vprintf 295 #define ttoproc(_a) ((_a)->td_proc) 296 #define crgetzoneid(_a) 0 297 #define NCPU MAXCPU 298 #define SNOCD 0 299 #define CPU_ON_INTR(_a) 0 300 301 #define PRIV_EFFECTIVE (1 << 0) 302 #define PRIV_DTRACE_KERNEL (1 << 1) 303 #define PRIV_DTRACE_PROC (1 << 2) 304 #define PRIV_DTRACE_USER (1 << 3) 305 #define PRIV_PROC_OWNER (1 << 4) 306 #define PRIV_PROC_ZONE (1 << 5) 307 #define PRIV_ALL ~0 308 309 SYSCTL_DECL(_debug_dtrace); 310 SYSCTL_DECL(_kern_dtrace); 311 #endif 312 313 #ifdef illumos 314 #define curcpu CPU->cpu_id 315 #endif 316 317 318 /* 319 * DTrace Provider Variables 320 * 321 * These are the variables relating to DTrace as a provider (that is, the 322 * provider of the BEGIN, END, and ERROR probes). 323 */ 324 static dtrace_pattr_t dtrace_provider_attr = { 325 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 326 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 327 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 328 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 329 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 330 }; 331 332 static void 333 dtrace_nullop(void) 334 {} 335 336 static dtrace_pops_t dtrace_provider_ops = { 337 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 338 (void (*)(void *, modctl_t *))dtrace_nullop, 339 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 340 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 341 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 342 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 343 NULL, 344 NULL, 345 NULL, 346 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 347 }; 348 349 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 350 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 351 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 352 353 /* 354 * DTrace Helper Tracing Variables 355 * 356 * These variables should be set dynamically to enable helper tracing. The 357 * only variables that should be set are dtrace_helptrace_enable (which should 358 * be set to a non-zero value to allocate helper tracing buffers on the next 359 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a 360 * non-zero value to deallocate helper tracing buffers on the next close of 361 * /dev/dtrace). When (and only when) helper tracing is disabled, the 362 * buffer size may also be set via dtrace_helptrace_bufsize. 363 */ 364 int dtrace_helptrace_enable = 0; 365 int dtrace_helptrace_disable = 0; 366 int dtrace_helptrace_bufsize = 16 * 1024 * 1024; 367 uint32_t dtrace_helptrace_nlocals; 368 static dtrace_helptrace_t *dtrace_helptrace_buffer; 369 static uint32_t dtrace_helptrace_next = 0; 370 static int dtrace_helptrace_wrapped = 0; 371 372 /* 373 * DTrace Error Hashing 374 * 375 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 376 * table. This is very useful for checking coverage of tests that are 377 * expected to induce DIF or DOF processing errors, and may be useful for 378 * debugging problems in the DIF code generator or in DOF generation . The 379 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 380 */ 381 #ifdef DEBUG 382 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 383 static const char *dtrace_errlast; 384 static kthread_t *dtrace_errthread; 385 static kmutex_t dtrace_errlock; 386 #endif 387 388 /* 389 * DTrace Macros and Constants 390 * 391 * These are various macros that are useful in various spots in the 392 * implementation, along with a few random constants that have no meaning 393 * outside of the implementation. There is no real structure to this cpp 394 * mishmash -- but is there ever? 395 */ 396 #define DTRACE_HASHSTR(hash, probe) \ 397 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 398 399 #define DTRACE_HASHNEXT(hash, probe) \ 400 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 401 402 #define DTRACE_HASHPREV(hash, probe) \ 403 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 404 405 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 406 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 407 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 408 409 #define DTRACE_AGGHASHSIZE_SLEW 17 410 411 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 412 413 /* 414 * The key for a thread-local variable consists of the lower 61 bits of the 415 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 416 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 417 * equal to a variable identifier. This is necessary (but not sufficient) to 418 * assure that global associative arrays never collide with thread-local 419 * variables. To guarantee that they cannot collide, we must also define the 420 * order for keying dynamic variables. That order is: 421 * 422 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 423 * 424 * Because the variable-key and the tls-key are in orthogonal spaces, there is 425 * no way for a global variable key signature to match a thread-local key 426 * signature. 427 */ 428 #ifdef illumos 429 #define DTRACE_TLS_THRKEY(where) { \ 430 uint_t intr = 0; \ 431 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 432 for (; actv; actv >>= 1) \ 433 intr++; \ 434 ASSERT(intr < (1 << 3)); \ 435 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 436 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 437 } 438 #else 439 #define DTRACE_TLS_THRKEY(where) { \ 440 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 441 uint_t intr = 0; \ 442 uint_t actv = _c->cpu_intr_actv; \ 443 for (; actv; actv >>= 1) \ 444 intr++; \ 445 ASSERT(intr < (1 << 3)); \ 446 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 447 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 448 } 449 #endif 450 451 #define DT_BSWAP_8(x) ((x) & 0xff) 452 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 453 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 454 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 455 456 #define DT_MASK_LO 0x00000000FFFFFFFFULL 457 458 #define DTRACE_STORE(type, tomax, offset, what) \ 459 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 460 461 #ifndef __x86 462 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 463 if (addr & (size - 1)) { \ 464 *flags |= CPU_DTRACE_BADALIGN; \ 465 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 466 return (0); \ 467 } 468 #else 469 #define DTRACE_ALIGNCHECK(addr, size, flags) 470 #endif 471 472 /* 473 * Test whether a range of memory starting at testaddr of size testsz falls 474 * within the range of memory described by addr, sz. We take care to avoid 475 * problems with overflow and underflow of the unsigned quantities, and 476 * disallow all negative sizes. Ranges of size 0 are allowed. 477 */ 478 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 479 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 480 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 481 (testaddr) + (testsz) >= (testaddr)) 482 483 #define DTRACE_RANGE_REMAIN(remp, addr, baseaddr, basesz) \ 484 do { \ 485 if ((remp) != NULL) { \ 486 *(remp) = (uintptr_t)(baseaddr) + (basesz) - (addr); \ 487 } \ 488 _NOTE(CONSTCOND) } while (0) 489 490 491 /* 492 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 493 * alloc_sz on the righthand side of the comparison in order to avoid overflow 494 * or underflow in the comparison with it. This is simpler than the INRANGE 495 * check above, because we know that the dtms_scratch_ptr is valid in the 496 * range. Allocations of size zero are allowed. 497 */ 498 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 499 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 500 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 501 502 #define DTRACE_LOADFUNC(bits) \ 503 /*CSTYLED*/ \ 504 uint##bits##_t \ 505 dtrace_load##bits(uintptr_t addr) \ 506 { \ 507 size_t size = bits / NBBY; \ 508 /*CSTYLED*/ \ 509 uint##bits##_t rval; \ 510 int i; \ 511 volatile uint16_t *flags = (volatile uint16_t *) \ 512 &cpu_core[curcpu].cpuc_dtrace_flags; \ 513 \ 514 DTRACE_ALIGNCHECK(addr, size, flags); \ 515 \ 516 for (i = 0; i < dtrace_toxranges; i++) { \ 517 if (addr >= dtrace_toxrange[i].dtt_limit) \ 518 continue; \ 519 \ 520 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 521 continue; \ 522 \ 523 /* \ 524 * This address falls within a toxic region; return 0. \ 525 */ \ 526 *flags |= CPU_DTRACE_BADADDR; \ 527 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 528 return (0); \ 529 } \ 530 \ 531 *flags |= CPU_DTRACE_NOFAULT; \ 532 /*CSTYLED*/ \ 533 rval = *((volatile uint##bits##_t *)addr); \ 534 *flags &= ~CPU_DTRACE_NOFAULT; \ 535 \ 536 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 537 } 538 539 #ifdef _LP64 540 #define dtrace_loadptr dtrace_load64 541 #else 542 #define dtrace_loadptr dtrace_load32 543 #endif 544 545 #define DTRACE_DYNHASH_FREE 0 546 #define DTRACE_DYNHASH_SINK 1 547 #define DTRACE_DYNHASH_VALID 2 548 549 #define DTRACE_MATCH_NEXT 0 550 #define DTRACE_MATCH_DONE 1 551 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 552 #define DTRACE_STATE_ALIGN 64 553 554 #define DTRACE_FLAGS2FLT(flags) \ 555 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 556 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 557 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 558 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 559 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 560 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 561 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 562 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 563 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 564 DTRACEFLT_UNKNOWN) 565 566 #define DTRACEACT_ISSTRING(act) \ 567 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 568 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 569 570 /* Function prototype definitions: */ 571 static size_t dtrace_strlen(const char *, size_t); 572 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 573 static void dtrace_enabling_provide(dtrace_provider_t *); 574 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 575 static void dtrace_enabling_matchall(void); 576 static void dtrace_enabling_reap(void); 577 static dtrace_state_t *dtrace_anon_grab(void); 578 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 579 dtrace_state_t *, uint64_t, uint64_t); 580 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 581 static void dtrace_buffer_drop(dtrace_buffer_t *); 582 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 583 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 584 dtrace_state_t *, dtrace_mstate_t *); 585 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 586 dtrace_optval_t); 587 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 588 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 589 uint16_t dtrace_load16(uintptr_t); 590 uint32_t dtrace_load32(uintptr_t); 591 uint64_t dtrace_load64(uintptr_t); 592 uint8_t dtrace_load8(uintptr_t); 593 void dtrace_dynvar_clean(dtrace_dstate_t *); 594 dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 595 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 596 uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 597 static int dtrace_priv_proc(dtrace_state_t *); 598 static void dtrace_getf_barrier(void); 599 static int dtrace_canload_remains(uint64_t, size_t, size_t *, 600 dtrace_mstate_t *, dtrace_vstate_t *); 601 static int dtrace_canstore_remains(uint64_t, size_t, size_t *, 602 dtrace_mstate_t *, dtrace_vstate_t *); 603 604 /* 605 * DTrace Probe Context Functions 606 * 607 * These functions are called from probe context. Because probe context is 608 * any context in which C may be called, arbitrarily locks may be held, 609 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 610 * As a result, functions called from probe context may only call other DTrace 611 * support functions -- they may not interact at all with the system at large. 612 * (Note that the ASSERT macro is made probe-context safe by redefining it in 613 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 614 * loads are to be performed from probe context, they _must_ be in terms of 615 * the safe dtrace_load*() variants. 616 * 617 * Some functions in this block are not actually called from probe context; 618 * for these functions, there will be a comment above the function reading 619 * "Note: not called from probe context." 620 */ 621 void 622 dtrace_panic(const char *format, ...) 623 { 624 va_list alist; 625 626 va_start(alist, format); 627 #ifdef __FreeBSD__ 628 vpanic(format, alist); 629 #else 630 dtrace_vpanic(format, alist); 631 #endif 632 va_end(alist); 633 } 634 635 int 636 dtrace_assfail(const char *a, const char *f, int l) 637 { 638 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 639 640 /* 641 * We just need something here that even the most clever compiler 642 * cannot optimize away. 643 */ 644 return (a[(uintptr_t)f]); 645 } 646 647 /* 648 * Atomically increment a specified error counter from probe context. 649 */ 650 static void 651 dtrace_error(uint32_t *counter) 652 { 653 /* 654 * Most counters stored to in probe context are per-CPU counters. 655 * However, there are some error conditions that are sufficiently 656 * arcane that they don't merit per-CPU storage. If these counters 657 * are incremented concurrently on different CPUs, scalability will be 658 * adversely affected -- but we don't expect them to be white-hot in a 659 * correctly constructed enabling... 660 */ 661 uint32_t oval, nval; 662 663 do { 664 oval = *counter; 665 666 if ((nval = oval + 1) == 0) { 667 /* 668 * If the counter would wrap, set it to 1 -- assuring 669 * that the counter is never zero when we have seen 670 * errors. (The counter must be 32-bits because we 671 * aren't guaranteed a 64-bit compare&swap operation.) 672 * To save this code both the infamy of being fingered 673 * by a priggish news story and the indignity of being 674 * the target of a neo-puritan witch trial, we're 675 * carefully avoiding any colorful description of the 676 * likelihood of this condition -- but suffice it to 677 * say that it is only slightly more likely than the 678 * overflow of predicate cache IDs, as discussed in 679 * dtrace_predicate_create(). 680 */ 681 nval = 1; 682 } 683 } while (dtrace_cas32(counter, oval, nval) != oval); 684 } 685 686 /* 687 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 688 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 689 */ 690 /* BEGIN CSTYLED */ 691 DTRACE_LOADFUNC(8) 692 DTRACE_LOADFUNC(16) 693 DTRACE_LOADFUNC(32) 694 DTRACE_LOADFUNC(64) 695 /* END CSTYLED */ 696 697 static int 698 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 699 { 700 if (dest < mstate->dtms_scratch_base) 701 return (0); 702 703 if (dest + size < dest) 704 return (0); 705 706 if (dest + size > mstate->dtms_scratch_ptr) 707 return (0); 708 709 return (1); 710 } 711 712 static int 713 dtrace_canstore_statvar(uint64_t addr, size_t sz, size_t *remain, 714 dtrace_statvar_t **svars, int nsvars) 715 { 716 int i; 717 size_t maxglobalsize, maxlocalsize; 718 719 if (nsvars == 0) 720 return (0); 721 722 maxglobalsize = dtrace_statvar_maxsize + sizeof (uint64_t); 723 maxlocalsize = maxglobalsize * NCPU; 724 725 for (i = 0; i < nsvars; i++) { 726 dtrace_statvar_t *svar = svars[i]; 727 uint8_t scope; 728 size_t size; 729 730 if (svar == NULL || (size = svar->dtsv_size) == 0) 731 continue; 732 733 scope = svar->dtsv_var.dtdv_scope; 734 735 /* 736 * We verify that our size is valid in the spirit of providing 737 * defense in depth: we want to prevent attackers from using 738 * DTrace to escalate an orthogonal kernel heap corruption bug 739 * into the ability to store to arbitrary locations in memory. 740 */ 741 VERIFY((scope == DIFV_SCOPE_GLOBAL && size <= maxglobalsize) || 742 (scope == DIFV_SCOPE_LOCAL && size <= maxlocalsize)); 743 744 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, 745 svar->dtsv_size)) { 746 DTRACE_RANGE_REMAIN(remain, addr, svar->dtsv_data, 747 svar->dtsv_size); 748 return (1); 749 } 750 } 751 752 return (0); 753 } 754 755 /* 756 * Check to see if the address is within a memory region to which a store may 757 * be issued. This includes the DTrace scratch areas, and any DTrace variable 758 * region. The caller of dtrace_canstore() is responsible for performing any 759 * alignment checks that are needed before stores are actually executed. 760 */ 761 static int 762 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 763 dtrace_vstate_t *vstate) 764 { 765 return (dtrace_canstore_remains(addr, sz, NULL, mstate, vstate)); 766 } 767 768 /* 769 * Implementation of dtrace_canstore which communicates the upper bound of the 770 * allowed memory region. 771 */ 772 static int 773 dtrace_canstore_remains(uint64_t addr, size_t sz, size_t *remain, 774 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 775 { 776 /* 777 * First, check to see if the address is in scratch space... 778 */ 779 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 780 mstate->dtms_scratch_size)) { 781 DTRACE_RANGE_REMAIN(remain, addr, mstate->dtms_scratch_base, 782 mstate->dtms_scratch_size); 783 return (1); 784 } 785 786 /* 787 * Now check to see if it's a dynamic variable. This check will pick 788 * up both thread-local variables and any global dynamically-allocated 789 * variables. 790 */ 791 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 792 vstate->dtvs_dynvars.dtds_size)) { 793 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 794 uintptr_t base = (uintptr_t)dstate->dtds_base + 795 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 796 uintptr_t chunkoffs; 797 dtrace_dynvar_t *dvar; 798 799 /* 800 * Before we assume that we can store here, we need to make 801 * sure that it isn't in our metadata -- storing to our 802 * dynamic variable metadata would corrupt our state. For 803 * the range to not include any dynamic variable metadata, 804 * it must: 805 * 806 * (1) Start above the hash table that is at the base of 807 * the dynamic variable space 808 * 809 * (2) Have a starting chunk offset that is beyond the 810 * dtrace_dynvar_t that is at the base of every chunk 811 * 812 * (3) Not span a chunk boundary 813 * 814 * (4) Not be in the tuple space of a dynamic variable 815 * 816 */ 817 if (addr < base) 818 return (0); 819 820 chunkoffs = (addr - base) % dstate->dtds_chunksize; 821 822 if (chunkoffs < sizeof (dtrace_dynvar_t)) 823 return (0); 824 825 if (chunkoffs + sz > dstate->dtds_chunksize) 826 return (0); 827 828 dvar = (dtrace_dynvar_t *)((uintptr_t)addr - chunkoffs); 829 830 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) 831 return (0); 832 833 if (chunkoffs < sizeof (dtrace_dynvar_t) + 834 ((dvar->dtdv_tuple.dtt_nkeys - 1) * sizeof (dtrace_key_t))) 835 return (0); 836 837 DTRACE_RANGE_REMAIN(remain, addr, dvar, dstate->dtds_chunksize); 838 return (1); 839 } 840 841 /* 842 * Finally, check the static local and global variables. These checks 843 * take the longest, so we perform them last. 844 */ 845 if (dtrace_canstore_statvar(addr, sz, remain, 846 vstate->dtvs_locals, vstate->dtvs_nlocals)) 847 return (1); 848 849 if (dtrace_canstore_statvar(addr, sz, remain, 850 vstate->dtvs_globals, vstate->dtvs_nglobals)) 851 return (1); 852 853 return (0); 854 } 855 856 857 /* 858 * Convenience routine to check to see if the address is within a memory 859 * region in which a load may be issued given the user's privilege level; 860 * if not, it sets the appropriate error flags and loads 'addr' into the 861 * illegal value slot. 862 * 863 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 864 * appropriate memory access protection. 865 */ 866 static int 867 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 868 dtrace_vstate_t *vstate) 869 { 870 return (dtrace_canload_remains(addr, sz, NULL, mstate, vstate)); 871 } 872 873 /* 874 * Implementation of dtrace_canload which communicates the uppoer bound of the 875 * allowed memory region. 876 */ 877 static int 878 dtrace_canload_remains(uint64_t addr, size_t sz, size_t *remain, 879 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 880 { 881 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 882 file_t *fp; 883 884 /* 885 * If we hold the privilege to read from kernel memory, then 886 * everything is readable. 887 */ 888 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 889 DTRACE_RANGE_REMAIN(remain, addr, addr, sz); 890 return (1); 891 } 892 893 /* 894 * You can obviously read that which you can store. 895 */ 896 if (dtrace_canstore_remains(addr, sz, remain, mstate, vstate)) 897 return (1); 898 899 /* 900 * We're allowed to read from our own string table. 901 */ 902 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 903 mstate->dtms_difo->dtdo_strlen)) { 904 DTRACE_RANGE_REMAIN(remain, addr, 905 mstate->dtms_difo->dtdo_strtab, 906 mstate->dtms_difo->dtdo_strlen); 907 return (1); 908 } 909 910 if (vstate->dtvs_state != NULL && 911 dtrace_priv_proc(vstate->dtvs_state)) { 912 proc_t *p; 913 914 /* 915 * When we have privileges to the current process, there are 916 * several context-related kernel structures that are safe to 917 * read, even absent the privilege to read from kernel memory. 918 * These reads are safe because these structures contain only 919 * state that (1) we're permitted to read, (2) is harmless or 920 * (3) contains pointers to additional kernel state that we're 921 * not permitted to read (and as such, do not present an 922 * opportunity for privilege escalation). Finally (and 923 * critically), because of the nature of their relation with 924 * the current thread context, the memory associated with these 925 * structures cannot change over the duration of probe context, 926 * and it is therefore impossible for this memory to be 927 * deallocated and reallocated as something else while it's 928 * being operated upon. 929 */ 930 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) { 931 DTRACE_RANGE_REMAIN(remain, addr, curthread, 932 sizeof (kthread_t)); 933 return (1); 934 } 935 936 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 937 sz, curthread->t_procp, sizeof (proc_t))) { 938 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_procp, 939 sizeof (proc_t)); 940 return (1); 941 } 942 943 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 944 curthread->t_cred, sizeof (cred_t))) { 945 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cred, 946 sizeof (cred_t)); 947 return (1); 948 } 949 950 #ifdef illumos 951 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 952 &(p->p_pidp->pid_id), sizeof (pid_t))) { 953 DTRACE_RANGE_REMAIN(remain, addr, &(p->p_pidp->pid_id), 954 sizeof (pid_t)); 955 return (1); 956 } 957 958 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 959 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 960 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cpu, 961 offsetof(cpu_t, cpu_pause_thread)); 962 return (1); 963 } 964 #endif 965 } 966 967 if ((fp = mstate->dtms_getf) != NULL) { 968 uintptr_t psz = sizeof (void *); 969 vnode_t *vp; 970 vnodeops_t *op; 971 972 /* 973 * When getf() returns a file_t, the enabling is implicitly 974 * granted the (transient) right to read the returned file_t 975 * as well as the v_path and v_op->vnop_name of the underlying 976 * vnode. These accesses are allowed after a successful 977 * getf() because the members that they refer to cannot change 978 * once set -- and the barrier logic in the kernel's closef() 979 * path assures that the file_t and its referenced vode_t 980 * cannot themselves be stale (that is, it impossible for 981 * either dtms_getf itself or its f_vnode member to reference 982 * freed memory). 983 */ 984 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) { 985 DTRACE_RANGE_REMAIN(remain, addr, fp, sizeof (file_t)); 986 return (1); 987 } 988 989 if ((vp = fp->f_vnode) != NULL) { 990 size_t slen; 991 #ifdef illumos 992 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) { 993 DTRACE_RANGE_REMAIN(remain, addr, &vp->v_path, 994 psz); 995 return (1); 996 } 997 slen = strlen(vp->v_path) + 1; 998 if (DTRACE_INRANGE(addr, sz, vp->v_path, slen)) { 999 DTRACE_RANGE_REMAIN(remain, addr, vp->v_path, 1000 slen); 1001 return (1); 1002 } 1003 #endif 1004 1005 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) { 1006 DTRACE_RANGE_REMAIN(remain, addr, &vp->v_op, 1007 psz); 1008 return (1); 1009 } 1010 1011 #ifdef illumos 1012 if ((op = vp->v_op) != NULL && 1013 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 1014 DTRACE_RANGE_REMAIN(remain, addr, 1015 &op->vnop_name, psz); 1016 return (1); 1017 } 1018 1019 if (op != NULL && op->vnop_name != NULL && 1020 DTRACE_INRANGE(addr, sz, op->vnop_name, 1021 (slen = strlen(op->vnop_name) + 1))) { 1022 DTRACE_RANGE_REMAIN(remain, addr, 1023 op->vnop_name, slen); 1024 return (1); 1025 } 1026 #endif 1027 } 1028 } 1029 1030 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 1031 *illval = addr; 1032 return (0); 1033 } 1034 1035 /* 1036 * Convenience routine to check to see if a given string is within a memory 1037 * region in which a load may be issued given the user's privilege level; 1038 * this exists so that we don't need to issue unnecessary dtrace_strlen() 1039 * calls in the event that the user has all privileges. 1040 */ 1041 static int 1042 dtrace_strcanload(uint64_t addr, size_t sz, size_t *remain, 1043 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1044 { 1045 size_t rsize; 1046 1047 /* 1048 * If we hold the privilege to read from kernel memory, then 1049 * everything is readable. 1050 */ 1051 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 1052 DTRACE_RANGE_REMAIN(remain, addr, addr, sz); 1053 return (1); 1054 } 1055 1056 /* 1057 * Even if the caller is uninterested in querying the remaining valid 1058 * range, it is required to ensure that the access is allowed. 1059 */ 1060 if (remain == NULL) { 1061 remain = &rsize; 1062 } 1063 if (dtrace_canload_remains(addr, 0, remain, mstate, vstate)) { 1064 size_t strsz; 1065 /* 1066 * Perform the strlen after determining the length of the 1067 * memory region which is accessible. This prevents timing 1068 * information from being used to find NULs in memory which is 1069 * not accessible to the caller. 1070 */ 1071 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, 1072 MIN(sz, *remain)); 1073 if (strsz <= *remain) { 1074 return (1); 1075 } 1076 } 1077 1078 return (0); 1079 } 1080 1081 /* 1082 * Convenience routine to check to see if a given variable is within a memory 1083 * region in which a load may be issued given the user's privilege level. 1084 */ 1085 static int 1086 dtrace_vcanload(void *src, dtrace_diftype_t *type, size_t *remain, 1087 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1088 { 1089 size_t sz; 1090 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1091 1092 /* 1093 * Calculate the max size before performing any checks since even 1094 * DTRACE_ACCESS_KERNEL-credentialed callers expect that this function 1095 * return the max length via 'remain'. 1096 */ 1097 if (type->dtdt_kind == DIF_TYPE_STRING) { 1098 dtrace_state_t *state = vstate->dtvs_state; 1099 1100 if (state != NULL) { 1101 sz = state->dts_options[DTRACEOPT_STRSIZE]; 1102 } else { 1103 /* 1104 * In helper context, we have a NULL state; fall back 1105 * to using the system-wide default for the string size 1106 * in this case. 1107 */ 1108 sz = dtrace_strsize_default; 1109 } 1110 } else { 1111 sz = type->dtdt_size; 1112 } 1113 1114 /* 1115 * If we hold the privilege to read from kernel memory, then 1116 * everything is readable. 1117 */ 1118 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 1119 DTRACE_RANGE_REMAIN(remain, (uintptr_t)src, src, sz); 1120 return (1); 1121 } 1122 1123 if (type->dtdt_kind == DIF_TYPE_STRING) { 1124 return (dtrace_strcanload((uintptr_t)src, sz, remain, mstate, 1125 vstate)); 1126 } 1127 return (dtrace_canload_remains((uintptr_t)src, sz, remain, mstate, 1128 vstate)); 1129 } 1130 1131 /* 1132 * Convert a string to a signed integer using safe loads. 1133 * 1134 * NOTE: This function uses various macros from strtolctype.h to manipulate 1135 * digit values, etc -- these have all been checked to ensure they make 1136 * no additional function calls. 1137 */ 1138 static int64_t 1139 dtrace_strtoll(char *input, int base, size_t limit) 1140 { 1141 uintptr_t pos = (uintptr_t)input; 1142 int64_t val = 0; 1143 int x; 1144 boolean_t neg = B_FALSE; 1145 char c, cc, ccc; 1146 uintptr_t end = pos + limit; 1147 1148 /* 1149 * Consume any whitespace preceding digits. 1150 */ 1151 while ((c = dtrace_load8(pos)) == ' ' || c == '\t') 1152 pos++; 1153 1154 /* 1155 * Handle an explicit sign if one is present. 1156 */ 1157 if (c == '-' || c == '+') { 1158 if (c == '-') 1159 neg = B_TRUE; 1160 c = dtrace_load8(++pos); 1161 } 1162 1163 /* 1164 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it 1165 * if present. 1166 */ 1167 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' || 1168 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) { 1169 pos += 2; 1170 c = ccc; 1171 } 1172 1173 /* 1174 * Read in contiguous digits until the first non-digit character. 1175 */ 1176 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base; 1177 c = dtrace_load8(++pos)) 1178 val = val * base + x; 1179 1180 return (neg ? -val : val); 1181 } 1182 1183 /* 1184 * Compare two strings using safe loads. 1185 */ 1186 static int 1187 dtrace_strncmp(char *s1, char *s2, size_t limit) 1188 { 1189 uint8_t c1, c2; 1190 volatile uint16_t *flags; 1191 1192 if (s1 == s2 || limit == 0) 1193 return (0); 1194 1195 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1196 1197 do { 1198 if (s1 == NULL) { 1199 c1 = '\0'; 1200 } else { 1201 c1 = dtrace_load8((uintptr_t)s1++); 1202 } 1203 1204 if (s2 == NULL) { 1205 c2 = '\0'; 1206 } else { 1207 c2 = dtrace_load8((uintptr_t)s2++); 1208 } 1209 1210 if (c1 != c2) 1211 return (c1 - c2); 1212 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 1213 1214 return (0); 1215 } 1216 1217 /* 1218 * Compute strlen(s) for a string using safe memory accesses. The additional 1219 * len parameter is used to specify a maximum length to ensure completion. 1220 */ 1221 static size_t 1222 dtrace_strlen(const char *s, size_t lim) 1223 { 1224 uint_t len; 1225 1226 for (len = 0; len != lim; len++) { 1227 if (dtrace_load8((uintptr_t)s++) == '\0') 1228 break; 1229 } 1230 1231 return (len); 1232 } 1233 1234 /* 1235 * Check if an address falls within a toxic region. 1236 */ 1237 static int 1238 dtrace_istoxic(uintptr_t kaddr, size_t size) 1239 { 1240 uintptr_t taddr, tsize; 1241 int i; 1242 1243 for (i = 0; i < dtrace_toxranges; i++) { 1244 taddr = dtrace_toxrange[i].dtt_base; 1245 tsize = dtrace_toxrange[i].dtt_limit - taddr; 1246 1247 if (kaddr - taddr < tsize) { 1248 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1249 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 1250 return (1); 1251 } 1252 1253 if (taddr - kaddr < size) { 1254 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1255 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 1256 return (1); 1257 } 1258 } 1259 1260 return (0); 1261 } 1262 1263 /* 1264 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 1265 * memory specified by the DIF program. The dst is assumed to be safe memory 1266 * that we can store to directly because it is managed by DTrace. As with 1267 * standard bcopy, overlapping copies are handled properly. 1268 */ 1269 static void 1270 dtrace_bcopy(const void *src, void *dst, size_t len) 1271 { 1272 if (len != 0) { 1273 uint8_t *s1 = dst; 1274 const uint8_t *s2 = src; 1275 1276 if (s1 <= s2) { 1277 do { 1278 *s1++ = dtrace_load8((uintptr_t)s2++); 1279 } while (--len != 0); 1280 } else { 1281 s2 += len; 1282 s1 += len; 1283 1284 do { 1285 *--s1 = dtrace_load8((uintptr_t)--s2); 1286 } while (--len != 0); 1287 } 1288 } 1289 } 1290 1291 /* 1292 * Copy src to dst using safe memory accesses, up to either the specified 1293 * length, or the point that a nul byte is encountered. The src is assumed to 1294 * be unsafe memory specified by the DIF program. The dst is assumed to be 1295 * safe memory that we can store to directly because it is managed by DTrace. 1296 * Unlike dtrace_bcopy(), overlapping regions are not handled. 1297 */ 1298 static void 1299 dtrace_strcpy(const void *src, void *dst, size_t len) 1300 { 1301 if (len != 0) { 1302 uint8_t *s1 = dst, c; 1303 const uint8_t *s2 = src; 1304 1305 do { 1306 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1307 } while (--len != 0 && c != '\0'); 1308 } 1309 } 1310 1311 /* 1312 * Copy src to dst, deriving the size and type from the specified (BYREF) 1313 * variable type. The src is assumed to be unsafe memory specified by the DIF 1314 * program. The dst is assumed to be DTrace variable memory that is of the 1315 * specified type; we assume that we can store to directly. 1316 */ 1317 static void 1318 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type, size_t limit) 1319 { 1320 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1321 1322 if (type->dtdt_kind == DIF_TYPE_STRING) { 1323 dtrace_strcpy(src, dst, MIN(type->dtdt_size, limit)); 1324 } else { 1325 dtrace_bcopy(src, dst, MIN(type->dtdt_size, limit)); 1326 } 1327 } 1328 1329 /* 1330 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1331 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1332 * safe memory that we can access directly because it is managed by DTrace. 1333 */ 1334 static int 1335 dtrace_bcmp(const void *s1, const void *s2, size_t len) 1336 { 1337 volatile uint16_t *flags; 1338 1339 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1340 1341 if (s1 == s2) 1342 return (0); 1343 1344 if (s1 == NULL || s2 == NULL) 1345 return (1); 1346 1347 if (s1 != s2 && len != 0) { 1348 const uint8_t *ps1 = s1; 1349 const uint8_t *ps2 = s2; 1350 1351 do { 1352 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1353 return (1); 1354 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1355 } 1356 return (0); 1357 } 1358 1359 /* 1360 * Zero the specified region using a simple byte-by-byte loop. Note that this 1361 * is for safe DTrace-managed memory only. 1362 */ 1363 static void 1364 dtrace_bzero(void *dst, size_t len) 1365 { 1366 uchar_t *cp; 1367 1368 for (cp = dst; len != 0; len--) 1369 *cp++ = 0; 1370 } 1371 1372 static void 1373 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1374 { 1375 uint64_t result[2]; 1376 1377 result[0] = addend1[0] + addend2[0]; 1378 result[1] = addend1[1] + addend2[1] + 1379 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1380 1381 sum[0] = result[0]; 1382 sum[1] = result[1]; 1383 } 1384 1385 /* 1386 * Shift the 128-bit value in a by b. If b is positive, shift left. 1387 * If b is negative, shift right. 1388 */ 1389 static void 1390 dtrace_shift_128(uint64_t *a, int b) 1391 { 1392 uint64_t mask; 1393 1394 if (b == 0) 1395 return; 1396 1397 if (b < 0) { 1398 b = -b; 1399 if (b >= 64) { 1400 a[0] = a[1] >> (b - 64); 1401 a[1] = 0; 1402 } else { 1403 a[0] >>= b; 1404 mask = 1LL << (64 - b); 1405 mask -= 1; 1406 a[0] |= ((a[1] & mask) << (64 - b)); 1407 a[1] >>= b; 1408 } 1409 } else { 1410 if (b >= 64) { 1411 a[1] = a[0] << (b - 64); 1412 a[0] = 0; 1413 } else { 1414 a[1] <<= b; 1415 mask = a[0] >> (64 - b); 1416 a[1] |= mask; 1417 a[0] <<= b; 1418 } 1419 } 1420 } 1421 1422 /* 1423 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1424 * use native multiplication on those, and then re-combine into the 1425 * resulting 128-bit value. 1426 * 1427 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1428 * hi1 * hi2 << 64 + 1429 * hi1 * lo2 << 32 + 1430 * hi2 * lo1 << 32 + 1431 * lo1 * lo2 1432 */ 1433 static void 1434 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1435 { 1436 uint64_t hi1, hi2, lo1, lo2; 1437 uint64_t tmp[2]; 1438 1439 hi1 = factor1 >> 32; 1440 hi2 = factor2 >> 32; 1441 1442 lo1 = factor1 & DT_MASK_LO; 1443 lo2 = factor2 & DT_MASK_LO; 1444 1445 product[0] = lo1 * lo2; 1446 product[1] = hi1 * hi2; 1447 1448 tmp[0] = hi1 * lo2; 1449 tmp[1] = 0; 1450 dtrace_shift_128(tmp, 32); 1451 dtrace_add_128(product, tmp, product); 1452 1453 tmp[0] = hi2 * lo1; 1454 tmp[1] = 0; 1455 dtrace_shift_128(tmp, 32); 1456 dtrace_add_128(product, tmp, product); 1457 } 1458 1459 /* 1460 * This privilege check should be used by actions and subroutines to 1461 * verify that the user credentials of the process that enabled the 1462 * invoking ECB match the target credentials 1463 */ 1464 static int 1465 dtrace_priv_proc_common_user(dtrace_state_t *state) 1466 { 1467 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1468 1469 /* 1470 * We should always have a non-NULL state cred here, since if cred 1471 * is null (anonymous tracing), we fast-path bypass this routine. 1472 */ 1473 ASSERT(s_cr != NULL); 1474 1475 if ((cr = CRED()) != NULL && 1476 s_cr->cr_uid == cr->cr_uid && 1477 s_cr->cr_uid == cr->cr_ruid && 1478 s_cr->cr_uid == cr->cr_suid && 1479 s_cr->cr_gid == cr->cr_gid && 1480 s_cr->cr_gid == cr->cr_rgid && 1481 s_cr->cr_gid == cr->cr_sgid) 1482 return (1); 1483 1484 return (0); 1485 } 1486 1487 /* 1488 * This privilege check should be used by actions and subroutines to 1489 * verify that the zone of the process that enabled the invoking ECB 1490 * matches the target credentials 1491 */ 1492 static int 1493 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1494 { 1495 #ifdef illumos 1496 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1497 1498 /* 1499 * We should always have a non-NULL state cred here, since if cred 1500 * is null (anonymous tracing), we fast-path bypass this routine. 1501 */ 1502 ASSERT(s_cr != NULL); 1503 1504 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1505 return (1); 1506 1507 return (0); 1508 #else 1509 return (1); 1510 #endif 1511 } 1512 1513 /* 1514 * This privilege check should be used by actions and subroutines to 1515 * verify that the process has not setuid or changed credentials. 1516 */ 1517 static int 1518 dtrace_priv_proc_common_nocd(void) 1519 { 1520 proc_t *proc; 1521 1522 if ((proc = ttoproc(curthread)) != NULL && 1523 !(proc->p_flag & SNOCD)) 1524 return (1); 1525 1526 return (0); 1527 } 1528 1529 static int 1530 dtrace_priv_proc_destructive(dtrace_state_t *state) 1531 { 1532 int action = state->dts_cred.dcr_action; 1533 1534 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1535 dtrace_priv_proc_common_zone(state) == 0) 1536 goto bad; 1537 1538 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1539 dtrace_priv_proc_common_user(state) == 0) 1540 goto bad; 1541 1542 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1543 dtrace_priv_proc_common_nocd() == 0) 1544 goto bad; 1545 1546 return (1); 1547 1548 bad: 1549 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1550 1551 return (0); 1552 } 1553 1554 static int 1555 dtrace_priv_proc_control(dtrace_state_t *state) 1556 { 1557 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1558 return (1); 1559 1560 if (dtrace_priv_proc_common_zone(state) && 1561 dtrace_priv_proc_common_user(state) && 1562 dtrace_priv_proc_common_nocd()) 1563 return (1); 1564 1565 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1566 1567 return (0); 1568 } 1569 1570 static int 1571 dtrace_priv_proc(dtrace_state_t *state) 1572 { 1573 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1574 return (1); 1575 1576 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1577 1578 return (0); 1579 } 1580 1581 static int 1582 dtrace_priv_kernel(dtrace_state_t *state) 1583 { 1584 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1585 return (1); 1586 1587 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1588 1589 return (0); 1590 } 1591 1592 static int 1593 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1594 { 1595 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1596 return (1); 1597 1598 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1599 1600 return (0); 1601 } 1602 1603 /* 1604 * Determine if the dte_cond of the specified ECB allows for processing of 1605 * the current probe to continue. Note that this routine may allow continued 1606 * processing, but with access(es) stripped from the mstate's dtms_access 1607 * field. 1608 */ 1609 static int 1610 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1611 dtrace_ecb_t *ecb) 1612 { 1613 dtrace_probe_t *probe = ecb->dte_probe; 1614 dtrace_provider_t *prov = probe->dtpr_provider; 1615 dtrace_pops_t *pops = &prov->dtpv_pops; 1616 int mode = DTRACE_MODE_NOPRIV_DROP; 1617 1618 ASSERT(ecb->dte_cond); 1619 1620 #ifdef illumos 1621 if (pops->dtps_mode != NULL) { 1622 mode = pops->dtps_mode(prov->dtpv_arg, 1623 probe->dtpr_id, probe->dtpr_arg); 1624 1625 ASSERT((mode & DTRACE_MODE_USER) || 1626 (mode & DTRACE_MODE_KERNEL)); 1627 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1628 (mode & DTRACE_MODE_NOPRIV_DROP)); 1629 } 1630 1631 /* 1632 * If the dte_cond bits indicate that this consumer is only allowed to 1633 * see user-mode firings of this probe, call the provider's dtps_mode() 1634 * entry point to check that the probe was fired while in a user 1635 * context. If that's not the case, use the policy specified by the 1636 * provider to determine if we drop the probe or merely restrict 1637 * operation. 1638 */ 1639 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1640 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1641 1642 if (!(mode & DTRACE_MODE_USER)) { 1643 if (mode & DTRACE_MODE_NOPRIV_DROP) 1644 return (0); 1645 1646 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1647 } 1648 } 1649 #endif 1650 1651 /* 1652 * This is more subtle than it looks. We have to be absolutely certain 1653 * that CRED() isn't going to change out from under us so it's only 1654 * legit to examine that structure if we're in constrained situations. 1655 * Currently, the only times we'll this check is if a non-super-user 1656 * has enabled the profile or syscall providers -- providers that 1657 * allow visibility of all processes. For the profile case, the check 1658 * above will ensure that we're examining a user context. 1659 */ 1660 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1661 cred_t *cr; 1662 cred_t *s_cr = state->dts_cred.dcr_cred; 1663 proc_t *proc; 1664 1665 ASSERT(s_cr != NULL); 1666 1667 if ((cr = CRED()) == NULL || 1668 s_cr->cr_uid != cr->cr_uid || 1669 s_cr->cr_uid != cr->cr_ruid || 1670 s_cr->cr_uid != cr->cr_suid || 1671 s_cr->cr_gid != cr->cr_gid || 1672 s_cr->cr_gid != cr->cr_rgid || 1673 s_cr->cr_gid != cr->cr_sgid || 1674 (proc = ttoproc(curthread)) == NULL || 1675 (proc->p_flag & SNOCD)) { 1676 if (mode & DTRACE_MODE_NOPRIV_DROP) 1677 return (0); 1678 1679 #ifdef illumos 1680 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1681 #endif 1682 } 1683 } 1684 1685 #ifdef illumos 1686 /* 1687 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1688 * in our zone, check to see if our mode policy is to restrict rather 1689 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1690 * and DTRACE_ACCESS_ARGS 1691 */ 1692 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1693 cred_t *cr; 1694 cred_t *s_cr = state->dts_cred.dcr_cred; 1695 1696 ASSERT(s_cr != NULL); 1697 1698 if ((cr = CRED()) == NULL || 1699 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1700 if (mode & DTRACE_MODE_NOPRIV_DROP) 1701 return (0); 1702 1703 mstate->dtms_access &= 1704 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1705 } 1706 } 1707 #endif 1708 1709 return (1); 1710 } 1711 1712 /* 1713 * Note: not called from probe context. This function is called 1714 * asynchronously (and at a regular interval) from outside of probe context to 1715 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1716 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1717 */ 1718 void 1719 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1720 { 1721 dtrace_dynvar_t *dirty; 1722 dtrace_dstate_percpu_t *dcpu; 1723 dtrace_dynvar_t **rinsep; 1724 int i, j, work = 0; 1725 1726 for (i = 0; i < NCPU; i++) { 1727 dcpu = &dstate->dtds_percpu[i]; 1728 rinsep = &dcpu->dtdsc_rinsing; 1729 1730 /* 1731 * If the dirty list is NULL, there is no dirty work to do. 1732 */ 1733 if (dcpu->dtdsc_dirty == NULL) 1734 continue; 1735 1736 if (dcpu->dtdsc_rinsing != NULL) { 1737 /* 1738 * If the rinsing list is non-NULL, then it is because 1739 * this CPU was selected to accept another CPU's 1740 * dirty list -- and since that time, dirty buffers 1741 * have accumulated. This is a highly unlikely 1742 * condition, but we choose to ignore the dirty 1743 * buffers -- they'll be picked up a future cleanse. 1744 */ 1745 continue; 1746 } 1747 1748 if (dcpu->dtdsc_clean != NULL) { 1749 /* 1750 * If the clean list is non-NULL, then we're in a 1751 * situation where a CPU has done deallocations (we 1752 * have a non-NULL dirty list) but no allocations (we 1753 * also have a non-NULL clean list). We can't simply 1754 * move the dirty list into the clean list on this 1755 * CPU, yet we also don't want to allow this condition 1756 * to persist, lest a short clean list prevent a 1757 * massive dirty list from being cleaned (which in 1758 * turn could lead to otherwise avoidable dynamic 1759 * drops). To deal with this, we look for some CPU 1760 * with a NULL clean list, NULL dirty list, and NULL 1761 * rinsing list -- and then we borrow this CPU to 1762 * rinse our dirty list. 1763 */ 1764 for (j = 0; j < NCPU; j++) { 1765 dtrace_dstate_percpu_t *rinser; 1766 1767 rinser = &dstate->dtds_percpu[j]; 1768 1769 if (rinser->dtdsc_rinsing != NULL) 1770 continue; 1771 1772 if (rinser->dtdsc_dirty != NULL) 1773 continue; 1774 1775 if (rinser->dtdsc_clean != NULL) 1776 continue; 1777 1778 rinsep = &rinser->dtdsc_rinsing; 1779 break; 1780 } 1781 1782 if (j == NCPU) { 1783 /* 1784 * We were unable to find another CPU that 1785 * could accept this dirty list -- we are 1786 * therefore unable to clean it now. 1787 */ 1788 dtrace_dynvar_failclean++; 1789 continue; 1790 } 1791 } 1792 1793 work = 1; 1794 1795 /* 1796 * Atomically move the dirty list aside. 1797 */ 1798 do { 1799 dirty = dcpu->dtdsc_dirty; 1800 1801 /* 1802 * Before we zap the dirty list, set the rinsing list. 1803 * (This allows for a potential assertion in 1804 * dtrace_dynvar(): if a free dynamic variable appears 1805 * on a hash chain, either the dirty list or the 1806 * rinsing list for some CPU must be non-NULL.) 1807 */ 1808 *rinsep = dirty; 1809 dtrace_membar_producer(); 1810 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1811 dirty, NULL) != dirty); 1812 } 1813 1814 if (!work) { 1815 /* 1816 * We have no work to do; we can simply return. 1817 */ 1818 return; 1819 } 1820 1821 dtrace_sync(); 1822 1823 for (i = 0; i < NCPU; i++) { 1824 dcpu = &dstate->dtds_percpu[i]; 1825 1826 if (dcpu->dtdsc_rinsing == NULL) 1827 continue; 1828 1829 /* 1830 * We are now guaranteed that no hash chain contains a pointer 1831 * into this dirty list; we can make it clean. 1832 */ 1833 ASSERT(dcpu->dtdsc_clean == NULL); 1834 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1835 dcpu->dtdsc_rinsing = NULL; 1836 } 1837 1838 /* 1839 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1840 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1841 * This prevents a race whereby a CPU incorrectly decides that 1842 * the state should be something other than DTRACE_DSTATE_CLEAN 1843 * after dtrace_dynvar_clean() has completed. 1844 */ 1845 dtrace_sync(); 1846 1847 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1848 } 1849 1850 /* 1851 * Depending on the value of the op parameter, this function looks-up, 1852 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1853 * allocation is requested, this function will return a pointer to a 1854 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1855 * variable can be allocated. If NULL is returned, the appropriate counter 1856 * will be incremented. 1857 */ 1858 dtrace_dynvar_t * 1859 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1860 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1861 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1862 { 1863 uint64_t hashval = DTRACE_DYNHASH_VALID; 1864 dtrace_dynhash_t *hash = dstate->dtds_hash; 1865 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1866 processorid_t me = curcpu, cpu = me; 1867 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1868 size_t bucket, ksize; 1869 size_t chunksize = dstate->dtds_chunksize; 1870 uintptr_t kdata, lock, nstate; 1871 uint_t i; 1872 1873 ASSERT(nkeys != 0); 1874 1875 /* 1876 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1877 * algorithm. For the by-value portions, we perform the algorithm in 1878 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1879 * bit, and seems to have only a minute effect on distribution. For 1880 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1881 * over each referenced byte. It's painful to do this, but it's much 1882 * better than pathological hash distribution. The efficacy of the 1883 * hashing algorithm (and a comparison with other algorithms) may be 1884 * found by running the ::dtrace_dynstat MDB dcmd. 1885 */ 1886 for (i = 0; i < nkeys; i++) { 1887 if (key[i].dttk_size == 0) { 1888 uint64_t val = key[i].dttk_value; 1889 1890 hashval += (val >> 48) & 0xffff; 1891 hashval += (hashval << 10); 1892 hashval ^= (hashval >> 6); 1893 1894 hashval += (val >> 32) & 0xffff; 1895 hashval += (hashval << 10); 1896 hashval ^= (hashval >> 6); 1897 1898 hashval += (val >> 16) & 0xffff; 1899 hashval += (hashval << 10); 1900 hashval ^= (hashval >> 6); 1901 1902 hashval += val & 0xffff; 1903 hashval += (hashval << 10); 1904 hashval ^= (hashval >> 6); 1905 } else { 1906 /* 1907 * This is incredibly painful, but it beats the hell 1908 * out of the alternative. 1909 */ 1910 uint64_t j, size = key[i].dttk_size; 1911 uintptr_t base = (uintptr_t)key[i].dttk_value; 1912 1913 if (!dtrace_canload(base, size, mstate, vstate)) 1914 break; 1915 1916 for (j = 0; j < size; j++) { 1917 hashval += dtrace_load8(base + j); 1918 hashval += (hashval << 10); 1919 hashval ^= (hashval >> 6); 1920 } 1921 } 1922 } 1923 1924 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1925 return (NULL); 1926 1927 hashval += (hashval << 3); 1928 hashval ^= (hashval >> 11); 1929 hashval += (hashval << 15); 1930 1931 /* 1932 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1933 * comes out to be one of our two sentinel hash values. If this 1934 * actually happens, we set the hashval to be a value known to be a 1935 * non-sentinel value. 1936 */ 1937 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1938 hashval = DTRACE_DYNHASH_VALID; 1939 1940 /* 1941 * Yes, it's painful to do a divide here. If the cycle count becomes 1942 * important here, tricks can be pulled to reduce it. (However, it's 1943 * critical that hash collisions be kept to an absolute minimum; 1944 * they're much more painful than a divide.) It's better to have a 1945 * solution that generates few collisions and still keeps things 1946 * relatively simple. 1947 */ 1948 bucket = hashval % dstate->dtds_hashsize; 1949 1950 if (op == DTRACE_DYNVAR_DEALLOC) { 1951 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1952 1953 for (;;) { 1954 while ((lock = *lockp) & 1) 1955 continue; 1956 1957 if (dtrace_casptr((volatile void *)lockp, 1958 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1959 break; 1960 } 1961 1962 dtrace_membar_producer(); 1963 } 1964 1965 top: 1966 prev = NULL; 1967 lock = hash[bucket].dtdh_lock; 1968 1969 dtrace_membar_consumer(); 1970 1971 start = hash[bucket].dtdh_chain; 1972 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1973 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1974 op != DTRACE_DYNVAR_DEALLOC)); 1975 1976 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1977 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1978 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1979 1980 if (dvar->dtdv_hashval != hashval) { 1981 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1982 /* 1983 * We've reached the sink, and therefore the 1984 * end of the hash chain; we can kick out of 1985 * the loop knowing that we have seen a valid 1986 * snapshot of state. 1987 */ 1988 ASSERT(dvar->dtdv_next == NULL); 1989 ASSERT(dvar == &dtrace_dynhash_sink); 1990 break; 1991 } 1992 1993 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1994 /* 1995 * We've gone off the rails: somewhere along 1996 * the line, one of the members of this hash 1997 * chain was deleted. Note that we could also 1998 * detect this by simply letting this loop run 1999 * to completion, as we would eventually hit 2000 * the end of the dirty list. However, we 2001 * want to avoid running the length of the 2002 * dirty list unnecessarily (it might be quite 2003 * long), so we catch this as early as 2004 * possible by detecting the hash marker. In 2005 * this case, we simply set dvar to NULL and 2006 * break; the conditional after the loop will 2007 * send us back to top. 2008 */ 2009 dvar = NULL; 2010 break; 2011 } 2012 2013 goto next; 2014 } 2015 2016 if (dtuple->dtt_nkeys != nkeys) 2017 goto next; 2018 2019 for (i = 0; i < nkeys; i++, dkey++) { 2020 if (dkey->dttk_size != key[i].dttk_size) 2021 goto next; /* size or type mismatch */ 2022 2023 if (dkey->dttk_size != 0) { 2024 if (dtrace_bcmp( 2025 (void *)(uintptr_t)key[i].dttk_value, 2026 (void *)(uintptr_t)dkey->dttk_value, 2027 dkey->dttk_size)) 2028 goto next; 2029 } else { 2030 if (dkey->dttk_value != key[i].dttk_value) 2031 goto next; 2032 } 2033 } 2034 2035 if (op != DTRACE_DYNVAR_DEALLOC) 2036 return (dvar); 2037 2038 ASSERT(dvar->dtdv_next == NULL || 2039 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 2040 2041 if (prev != NULL) { 2042 ASSERT(hash[bucket].dtdh_chain != dvar); 2043 ASSERT(start != dvar); 2044 ASSERT(prev->dtdv_next == dvar); 2045 prev->dtdv_next = dvar->dtdv_next; 2046 } else { 2047 if (dtrace_casptr(&hash[bucket].dtdh_chain, 2048 start, dvar->dtdv_next) != start) { 2049 /* 2050 * We have failed to atomically swing the 2051 * hash table head pointer, presumably because 2052 * of a conflicting allocation on another CPU. 2053 * We need to reread the hash chain and try 2054 * again. 2055 */ 2056 goto top; 2057 } 2058 } 2059 2060 dtrace_membar_producer(); 2061 2062 /* 2063 * Now set the hash value to indicate that it's free. 2064 */ 2065 ASSERT(hash[bucket].dtdh_chain != dvar); 2066 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2067 2068 dtrace_membar_producer(); 2069 2070 /* 2071 * Set the next pointer to point at the dirty list, and 2072 * atomically swing the dirty pointer to the newly freed dvar. 2073 */ 2074 do { 2075 next = dcpu->dtdsc_dirty; 2076 dvar->dtdv_next = next; 2077 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 2078 2079 /* 2080 * Finally, unlock this hash bucket. 2081 */ 2082 ASSERT(hash[bucket].dtdh_lock == lock); 2083 ASSERT(lock & 1); 2084 hash[bucket].dtdh_lock++; 2085 2086 return (NULL); 2087 next: 2088 prev = dvar; 2089 continue; 2090 } 2091 2092 if (dvar == NULL) { 2093 /* 2094 * If dvar is NULL, it is because we went off the rails: 2095 * one of the elements that we traversed in the hash chain 2096 * was deleted while we were traversing it. In this case, 2097 * we assert that we aren't doing a dealloc (deallocs lock 2098 * the hash bucket to prevent themselves from racing with 2099 * one another), and retry the hash chain traversal. 2100 */ 2101 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 2102 goto top; 2103 } 2104 2105 if (op != DTRACE_DYNVAR_ALLOC) { 2106 /* 2107 * If we are not to allocate a new variable, we want to 2108 * return NULL now. Before we return, check that the value 2109 * of the lock word hasn't changed. If it has, we may have 2110 * seen an inconsistent snapshot. 2111 */ 2112 if (op == DTRACE_DYNVAR_NOALLOC) { 2113 if (hash[bucket].dtdh_lock != lock) 2114 goto top; 2115 } else { 2116 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 2117 ASSERT(hash[bucket].dtdh_lock == lock); 2118 ASSERT(lock & 1); 2119 hash[bucket].dtdh_lock++; 2120 } 2121 2122 return (NULL); 2123 } 2124 2125 /* 2126 * We need to allocate a new dynamic variable. The size we need is the 2127 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 2128 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 2129 * the size of any referred-to data (dsize). We then round the final 2130 * size up to the chunksize for allocation. 2131 */ 2132 for (ksize = 0, i = 0; i < nkeys; i++) 2133 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 2134 2135 /* 2136 * This should be pretty much impossible, but could happen if, say, 2137 * strange DIF specified the tuple. Ideally, this should be an 2138 * assertion and not an error condition -- but that requires that the 2139 * chunksize calculation in dtrace_difo_chunksize() be absolutely 2140 * bullet-proof. (That is, it must not be able to be fooled by 2141 * malicious DIF.) Given the lack of backwards branches in DIF, 2142 * solving this would presumably not amount to solving the Halting 2143 * Problem -- but it still seems awfully hard. 2144 */ 2145 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 2146 ksize + dsize > chunksize) { 2147 dcpu->dtdsc_drops++; 2148 return (NULL); 2149 } 2150 2151 nstate = DTRACE_DSTATE_EMPTY; 2152 2153 do { 2154 retry: 2155 free = dcpu->dtdsc_free; 2156 2157 if (free == NULL) { 2158 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 2159 void *rval; 2160 2161 if (clean == NULL) { 2162 /* 2163 * We're out of dynamic variable space on 2164 * this CPU. Unless we have tried all CPUs, 2165 * we'll try to allocate from a different 2166 * CPU. 2167 */ 2168 switch (dstate->dtds_state) { 2169 case DTRACE_DSTATE_CLEAN: { 2170 void *sp = &dstate->dtds_state; 2171 2172 if (++cpu >= NCPU) 2173 cpu = 0; 2174 2175 if (dcpu->dtdsc_dirty != NULL && 2176 nstate == DTRACE_DSTATE_EMPTY) 2177 nstate = DTRACE_DSTATE_DIRTY; 2178 2179 if (dcpu->dtdsc_rinsing != NULL) 2180 nstate = DTRACE_DSTATE_RINSING; 2181 2182 dcpu = &dstate->dtds_percpu[cpu]; 2183 2184 if (cpu != me) 2185 goto retry; 2186 2187 (void) dtrace_cas32(sp, 2188 DTRACE_DSTATE_CLEAN, nstate); 2189 2190 /* 2191 * To increment the correct bean 2192 * counter, take another lap. 2193 */ 2194 goto retry; 2195 } 2196 2197 case DTRACE_DSTATE_DIRTY: 2198 dcpu->dtdsc_dirty_drops++; 2199 break; 2200 2201 case DTRACE_DSTATE_RINSING: 2202 dcpu->dtdsc_rinsing_drops++; 2203 break; 2204 2205 case DTRACE_DSTATE_EMPTY: 2206 dcpu->dtdsc_drops++; 2207 break; 2208 } 2209 2210 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 2211 return (NULL); 2212 } 2213 2214 /* 2215 * The clean list appears to be non-empty. We want to 2216 * move the clean list to the free list; we start by 2217 * moving the clean pointer aside. 2218 */ 2219 if (dtrace_casptr(&dcpu->dtdsc_clean, 2220 clean, NULL) != clean) { 2221 /* 2222 * We are in one of two situations: 2223 * 2224 * (a) The clean list was switched to the 2225 * free list by another CPU. 2226 * 2227 * (b) The clean list was added to by the 2228 * cleansing cyclic. 2229 * 2230 * In either of these situations, we can 2231 * just reattempt the free list allocation. 2232 */ 2233 goto retry; 2234 } 2235 2236 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 2237 2238 /* 2239 * Now we'll move the clean list to our free list. 2240 * It's impossible for this to fail: the only way 2241 * the free list can be updated is through this 2242 * code path, and only one CPU can own the clean list. 2243 * Thus, it would only be possible for this to fail if 2244 * this code were racing with dtrace_dynvar_clean(). 2245 * (That is, if dtrace_dynvar_clean() updated the clean 2246 * list, and we ended up racing to update the free 2247 * list.) This race is prevented by the dtrace_sync() 2248 * in dtrace_dynvar_clean() -- which flushes the 2249 * owners of the clean lists out before resetting 2250 * the clean lists. 2251 */ 2252 dcpu = &dstate->dtds_percpu[me]; 2253 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 2254 ASSERT(rval == NULL); 2255 goto retry; 2256 } 2257 2258 dvar = free; 2259 new_free = dvar->dtdv_next; 2260 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 2261 2262 /* 2263 * We have now allocated a new chunk. We copy the tuple keys into the 2264 * tuple array and copy any referenced key data into the data space 2265 * following the tuple array. As we do this, we relocate dttk_value 2266 * in the final tuple to point to the key data address in the chunk. 2267 */ 2268 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 2269 dvar->dtdv_data = (void *)(kdata + ksize); 2270 dvar->dtdv_tuple.dtt_nkeys = nkeys; 2271 2272 for (i = 0; i < nkeys; i++) { 2273 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 2274 size_t kesize = key[i].dttk_size; 2275 2276 if (kesize != 0) { 2277 dtrace_bcopy( 2278 (const void *)(uintptr_t)key[i].dttk_value, 2279 (void *)kdata, kesize); 2280 dkey->dttk_value = kdata; 2281 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 2282 } else { 2283 dkey->dttk_value = key[i].dttk_value; 2284 } 2285 2286 dkey->dttk_size = kesize; 2287 } 2288 2289 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 2290 dvar->dtdv_hashval = hashval; 2291 dvar->dtdv_next = start; 2292 2293 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 2294 return (dvar); 2295 2296 /* 2297 * The cas has failed. Either another CPU is adding an element to 2298 * this hash chain, or another CPU is deleting an element from this 2299 * hash chain. The simplest way to deal with both of these cases 2300 * (though not necessarily the most efficient) is to free our 2301 * allocated block and re-attempt it all. Note that the free is 2302 * to the dirty list and _not_ to the free list. This is to prevent 2303 * races with allocators, above. 2304 */ 2305 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2306 2307 dtrace_membar_producer(); 2308 2309 do { 2310 free = dcpu->dtdsc_dirty; 2311 dvar->dtdv_next = free; 2312 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 2313 2314 goto top; 2315 } 2316 2317 /*ARGSUSED*/ 2318 static void 2319 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 2320 { 2321 if ((int64_t)nval < (int64_t)*oval) 2322 *oval = nval; 2323 } 2324 2325 /*ARGSUSED*/ 2326 static void 2327 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2328 { 2329 if ((int64_t)nval > (int64_t)*oval) 2330 *oval = nval; 2331 } 2332 2333 static void 2334 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2335 { 2336 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2337 int64_t val = (int64_t)nval; 2338 2339 if (val < 0) { 2340 for (i = 0; i < zero; i++) { 2341 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2342 quanta[i] += incr; 2343 return; 2344 } 2345 } 2346 } else { 2347 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2348 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2349 quanta[i - 1] += incr; 2350 return; 2351 } 2352 } 2353 2354 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2355 return; 2356 } 2357 2358 ASSERT(0); 2359 } 2360 2361 static void 2362 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2363 { 2364 uint64_t arg = *lquanta++; 2365 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2366 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2367 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2368 int32_t val = (int32_t)nval, level; 2369 2370 ASSERT(step != 0); 2371 ASSERT(levels != 0); 2372 2373 if (val < base) { 2374 /* 2375 * This is an underflow. 2376 */ 2377 lquanta[0] += incr; 2378 return; 2379 } 2380 2381 level = (val - base) / step; 2382 2383 if (level < levels) { 2384 lquanta[level + 1] += incr; 2385 return; 2386 } 2387 2388 /* 2389 * This is an overflow. 2390 */ 2391 lquanta[levels + 1] += incr; 2392 } 2393 2394 static int 2395 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2396 uint16_t high, uint16_t nsteps, int64_t value) 2397 { 2398 int64_t this = 1, last, next; 2399 int base = 1, order; 2400 2401 ASSERT(factor <= nsteps); 2402 ASSERT(nsteps % factor == 0); 2403 2404 for (order = 0; order < low; order++) 2405 this *= factor; 2406 2407 /* 2408 * If our value is less than our factor taken to the power of the 2409 * low order of magnitude, it goes into the zeroth bucket. 2410 */ 2411 if (value < (last = this)) 2412 return (0); 2413 2414 for (this *= factor; order <= high; order++) { 2415 int nbuckets = this > nsteps ? nsteps : this; 2416 2417 if ((next = this * factor) < this) { 2418 /* 2419 * We should not generally get log/linear quantizations 2420 * with a high magnitude that allows 64-bits to 2421 * overflow, but we nonetheless protect against this 2422 * by explicitly checking for overflow, and clamping 2423 * our value accordingly. 2424 */ 2425 value = this - 1; 2426 } 2427 2428 if (value < this) { 2429 /* 2430 * If our value lies within this order of magnitude, 2431 * determine its position by taking the offset within 2432 * the order of magnitude, dividing by the bucket 2433 * width, and adding to our (accumulated) base. 2434 */ 2435 return (base + (value - last) / (this / nbuckets)); 2436 } 2437 2438 base += nbuckets - (nbuckets / factor); 2439 last = this; 2440 this = next; 2441 } 2442 2443 /* 2444 * Our value is greater than or equal to our factor taken to the 2445 * power of one plus the high magnitude -- return the top bucket. 2446 */ 2447 return (base); 2448 } 2449 2450 static void 2451 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2452 { 2453 uint64_t arg = *llquanta++; 2454 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2455 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2456 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2457 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2458 2459 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2460 low, high, nsteps, nval)] += incr; 2461 } 2462 2463 /*ARGSUSED*/ 2464 static void 2465 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2466 { 2467 data[0]++; 2468 data[1] += nval; 2469 } 2470 2471 /*ARGSUSED*/ 2472 static void 2473 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2474 { 2475 int64_t snval = (int64_t)nval; 2476 uint64_t tmp[2]; 2477 2478 data[0]++; 2479 data[1] += nval; 2480 2481 /* 2482 * What we want to say here is: 2483 * 2484 * data[2] += nval * nval; 2485 * 2486 * But given that nval is 64-bit, we could easily overflow, so 2487 * we do this as 128-bit arithmetic. 2488 */ 2489 if (snval < 0) 2490 snval = -snval; 2491 2492 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2493 dtrace_add_128(data + 2, tmp, data + 2); 2494 } 2495 2496 /*ARGSUSED*/ 2497 static void 2498 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2499 { 2500 *oval = *oval + 1; 2501 } 2502 2503 /*ARGSUSED*/ 2504 static void 2505 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2506 { 2507 *oval += nval; 2508 } 2509 2510 /* 2511 * Aggregate given the tuple in the principal data buffer, and the aggregating 2512 * action denoted by the specified dtrace_aggregation_t. The aggregation 2513 * buffer is specified as the buf parameter. This routine does not return 2514 * failure; if there is no space in the aggregation buffer, the data will be 2515 * dropped, and a corresponding counter incremented. 2516 */ 2517 static void 2518 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2519 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2520 { 2521 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2522 uint32_t i, ndx, size, fsize; 2523 uint32_t align = sizeof (uint64_t) - 1; 2524 dtrace_aggbuffer_t *agb; 2525 dtrace_aggkey_t *key; 2526 uint32_t hashval = 0, limit, isstr; 2527 caddr_t tomax, data, kdata; 2528 dtrace_actkind_t action; 2529 dtrace_action_t *act; 2530 uintptr_t offs; 2531 2532 if (buf == NULL) 2533 return; 2534 2535 if (!agg->dtag_hasarg) { 2536 /* 2537 * Currently, only quantize() and lquantize() take additional 2538 * arguments, and they have the same semantics: an increment 2539 * value that defaults to 1 when not present. If additional 2540 * aggregating actions take arguments, the setting of the 2541 * default argument value will presumably have to become more 2542 * sophisticated... 2543 */ 2544 arg = 1; 2545 } 2546 2547 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2548 size = rec->dtrd_offset - agg->dtag_base; 2549 fsize = size + rec->dtrd_size; 2550 2551 ASSERT(dbuf->dtb_tomax != NULL); 2552 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2553 2554 if ((tomax = buf->dtb_tomax) == NULL) { 2555 dtrace_buffer_drop(buf); 2556 return; 2557 } 2558 2559 /* 2560 * The metastructure is always at the bottom of the buffer. 2561 */ 2562 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2563 sizeof (dtrace_aggbuffer_t)); 2564 2565 if (buf->dtb_offset == 0) { 2566 /* 2567 * We just kludge up approximately 1/8th of the size to be 2568 * buckets. If this guess ends up being routinely 2569 * off-the-mark, we may need to dynamically readjust this 2570 * based on past performance. 2571 */ 2572 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2573 2574 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2575 (uintptr_t)tomax || hashsize == 0) { 2576 /* 2577 * We've been given a ludicrously small buffer; 2578 * increment our drop count and leave. 2579 */ 2580 dtrace_buffer_drop(buf); 2581 return; 2582 } 2583 2584 /* 2585 * And now, a pathetic attempt to try to get a an odd (or 2586 * perchance, a prime) hash size for better hash distribution. 2587 */ 2588 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2589 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2590 2591 agb->dtagb_hashsize = hashsize; 2592 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2593 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2594 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2595 2596 for (i = 0; i < agb->dtagb_hashsize; i++) 2597 agb->dtagb_hash[i] = NULL; 2598 } 2599 2600 ASSERT(agg->dtag_first != NULL); 2601 ASSERT(agg->dtag_first->dta_intuple); 2602 2603 /* 2604 * Calculate the hash value based on the key. Note that we _don't_ 2605 * include the aggid in the hashing (but we will store it as part of 2606 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2607 * algorithm: a simple, quick algorithm that has no known funnels, and 2608 * gets good distribution in practice. The efficacy of the hashing 2609 * algorithm (and a comparison with other algorithms) may be found by 2610 * running the ::dtrace_aggstat MDB dcmd. 2611 */ 2612 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2613 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2614 limit = i + act->dta_rec.dtrd_size; 2615 ASSERT(limit <= size); 2616 isstr = DTRACEACT_ISSTRING(act); 2617 2618 for (; i < limit; i++) { 2619 hashval += data[i]; 2620 hashval += (hashval << 10); 2621 hashval ^= (hashval >> 6); 2622 2623 if (isstr && data[i] == '\0') 2624 break; 2625 } 2626 } 2627 2628 hashval += (hashval << 3); 2629 hashval ^= (hashval >> 11); 2630 hashval += (hashval << 15); 2631 2632 /* 2633 * Yes, the divide here is expensive -- but it's generally the least 2634 * of the performance issues given the amount of data that we iterate 2635 * over to compute hash values, compare data, etc. 2636 */ 2637 ndx = hashval % agb->dtagb_hashsize; 2638 2639 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2640 ASSERT((caddr_t)key >= tomax); 2641 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2642 2643 if (hashval != key->dtak_hashval || key->dtak_size != size) 2644 continue; 2645 2646 kdata = key->dtak_data; 2647 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2648 2649 for (act = agg->dtag_first; act->dta_intuple; 2650 act = act->dta_next) { 2651 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2652 limit = i + act->dta_rec.dtrd_size; 2653 ASSERT(limit <= size); 2654 isstr = DTRACEACT_ISSTRING(act); 2655 2656 for (; i < limit; i++) { 2657 if (kdata[i] != data[i]) 2658 goto next; 2659 2660 if (isstr && data[i] == '\0') 2661 break; 2662 } 2663 } 2664 2665 if (action != key->dtak_action) { 2666 /* 2667 * We are aggregating on the same value in the same 2668 * aggregation with two different aggregating actions. 2669 * (This should have been picked up in the compiler, 2670 * so we may be dealing with errant or devious DIF.) 2671 * This is an error condition; we indicate as much, 2672 * and return. 2673 */ 2674 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2675 return; 2676 } 2677 2678 /* 2679 * This is a hit: we need to apply the aggregator to 2680 * the value at this key. 2681 */ 2682 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2683 return; 2684 next: 2685 continue; 2686 } 2687 2688 /* 2689 * We didn't find it. We need to allocate some zero-filled space, 2690 * link it into the hash table appropriately, and apply the aggregator 2691 * to the (zero-filled) value. 2692 */ 2693 offs = buf->dtb_offset; 2694 while (offs & (align - 1)) 2695 offs += sizeof (uint32_t); 2696 2697 /* 2698 * If we don't have enough room to both allocate a new key _and_ 2699 * its associated data, increment the drop count and return. 2700 */ 2701 if ((uintptr_t)tomax + offs + fsize > 2702 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2703 dtrace_buffer_drop(buf); 2704 return; 2705 } 2706 2707 /*CONSTCOND*/ 2708 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2709 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2710 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2711 2712 key->dtak_data = kdata = tomax + offs; 2713 buf->dtb_offset = offs + fsize; 2714 2715 /* 2716 * Now copy the data across. 2717 */ 2718 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2719 2720 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2721 kdata[i] = data[i]; 2722 2723 /* 2724 * Because strings are not zeroed out by default, we need to iterate 2725 * looking for actions that store strings, and we need to explicitly 2726 * pad these strings out with zeroes. 2727 */ 2728 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2729 int nul; 2730 2731 if (!DTRACEACT_ISSTRING(act)) 2732 continue; 2733 2734 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2735 limit = i + act->dta_rec.dtrd_size; 2736 ASSERT(limit <= size); 2737 2738 for (nul = 0; i < limit; i++) { 2739 if (nul) { 2740 kdata[i] = '\0'; 2741 continue; 2742 } 2743 2744 if (data[i] != '\0') 2745 continue; 2746 2747 nul = 1; 2748 } 2749 } 2750 2751 for (i = size; i < fsize; i++) 2752 kdata[i] = 0; 2753 2754 key->dtak_hashval = hashval; 2755 key->dtak_size = size; 2756 key->dtak_action = action; 2757 key->dtak_next = agb->dtagb_hash[ndx]; 2758 agb->dtagb_hash[ndx] = key; 2759 2760 /* 2761 * Finally, apply the aggregator. 2762 */ 2763 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2764 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2765 } 2766 2767 /* 2768 * Given consumer state, this routine finds a speculation in the INACTIVE 2769 * state and transitions it into the ACTIVE state. If there is no speculation 2770 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2771 * incremented -- it is up to the caller to take appropriate action. 2772 */ 2773 static int 2774 dtrace_speculation(dtrace_state_t *state) 2775 { 2776 int i = 0; 2777 dtrace_speculation_state_t current; 2778 uint32_t *stat = &state->dts_speculations_unavail, count; 2779 2780 while (i < state->dts_nspeculations) { 2781 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2782 2783 current = spec->dtsp_state; 2784 2785 if (current != DTRACESPEC_INACTIVE) { 2786 if (current == DTRACESPEC_COMMITTINGMANY || 2787 current == DTRACESPEC_COMMITTING || 2788 current == DTRACESPEC_DISCARDING) 2789 stat = &state->dts_speculations_busy; 2790 i++; 2791 continue; 2792 } 2793 2794 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2795 current, DTRACESPEC_ACTIVE) == current) 2796 return (i + 1); 2797 } 2798 2799 /* 2800 * We couldn't find a speculation. If we found as much as a single 2801 * busy speculation buffer, we'll attribute this failure as "busy" 2802 * instead of "unavail". 2803 */ 2804 do { 2805 count = *stat; 2806 } while (dtrace_cas32(stat, count, count + 1) != count); 2807 2808 return (0); 2809 } 2810 2811 /* 2812 * This routine commits an active speculation. If the specified speculation 2813 * is not in a valid state to perform a commit(), this routine will silently do 2814 * nothing. The state of the specified speculation is transitioned according 2815 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2816 */ 2817 static void 2818 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2819 dtrace_specid_t which) 2820 { 2821 dtrace_speculation_t *spec; 2822 dtrace_buffer_t *src, *dest; 2823 uintptr_t daddr, saddr, dlimit, slimit; 2824 dtrace_speculation_state_t current, new = 0; 2825 intptr_t offs; 2826 uint64_t timestamp; 2827 2828 if (which == 0) 2829 return; 2830 2831 if (which > state->dts_nspeculations) { 2832 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2833 return; 2834 } 2835 2836 spec = &state->dts_speculations[which - 1]; 2837 src = &spec->dtsp_buffer[cpu]; 2838 dest = &state->dts_buffer[cpu]; 2839 2840 do { 2841 current = spec->dtsp_state; 2842 2843 if (current == DTRACESPEC_COMMITTINGMANY) 2844 break; 2845 2846 switch (current) { 2847 case DTRACESPEC_INACTIVE: 2848 case DTRACESPEC_DISCARDING: 2849 return; 2850 2851 case DTRACESPEC_COMMITTING: 2852 /* 2853 * This is only possible if we are (a) commit()'ing 2854 * without having done a prior speculate() on this CPU 2855 * and (b) racing with another commit() on a different 2856 * CPU. There's nothing to do -- we just assert that 2857 * our offset is 0. 2858 */ 2859 ASSERT(src->dtb_offset == 0); 2860 return; 2861 2862 case DTRACESPEC_ACTIVE: 2863 new = DTRACESPEC_COMMITTING; 2864 break; 2865 2866 case DTRACESPEC_ACTIVEONE: 2867 /* 2868 * This speculation is active on one CPU. If our 2869 * buffer offset is non-zero, we know that the one CPU 2870 * must be us. Otherwise, we are committing on a 2871 * different CPU from the speculate(), and we must 2872 * rely on being asynchronously cleaned. 2873 */ 2874 if (src->dtb_offset != 0) { 2875 new = DTRACESPEC_COMMITTING; 2876 break; 2877 } 2878 /*FALLTHROUGH*/ 2879 2880 case DTRACESPEC_ACTIVEMANY: 2881 new = DTRACESPEC_COMMITTINGMANY; 2882 break; 2883 2884 default: 2885 ASSERT(0); 2886 } 2887 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2888 current, new) != current); 2889 2890 /* 2891 * We have set the state to indicate that we are committing this 2892 * speculation. Now reserve the necessary space in the destination 2893 * buffer. 2894 */ 2895 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2896 sizeof (uint64_t), state, NULL)) < 0) { 2897 dtrace_buffer_drop(dest); 2898 goto out; 2899 } 2900 2901 /* 2902 * We have sufficient space to copy the speculative buffer into the 2903 * primary buffer. First, modify the speculative buffer, filling 2904 * in the timestamp of all entries with the current time. The data 2905 * must have the commit() time rather than the time it was traced, 2906 * so that all entries in the primary buffer are in timestamp order. 2907 */ 2908 timestamp = dtrace_gethrtime(); 2909 saddr = (uintptr_t)src->dtb_tomax; 2910 slimit = saddr + src->dtb_offset; 2911 while (saddr < slimit) { 2912 size_t size; 2913 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2914 2915 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2916 saddr += sizeof (dtrace_epid_t); 2917 continue; 2918 } 2919 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2920 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2921 2922 ASSERT3U(saddr + size, <=, slimit); 2923 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2924 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2925 2926 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2927 2928 saddr += size; 2929 } 2930 2931 /* 2932 * Copy the buffer across. (Note that this is a 2933 * highly subobtimal bcopy(); in the unlikely event that this becomes 2934 * a serious performance issue, a high-performance DTrace-specific 2935 * bcopy() should obviously be invented.) 2936 */ 2937 daddr = (uintptr_t)dest->dtb_tomax + offs; 2938 dlimit = daddr + src->dtb_offset; 2939 saddr = (uintptr_t)src->dtb_tomax; 2940 2941 /* 2942 * First, the aligned portion. 2943 */ 2944 while (dlimit - daddr >= sizeof (uint64_t)) { 2945 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2946 2947 daddr += sizeof (uint64_t); 2948 saddr += sizeof (uint64_t); 2949 } 2950 2951 /* 2952 * Now any left-over bit... 2953 */ 2954 while (dlimit - daddr) 2955 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2956 2957 /* 2958 * Finally, commit the reserved space in the destination buffer. 2959 */ 2960 dest->dtb_offset = offs + src->dtb_offset; 2961 2962 out: 2963 /* 2964 * If we're lucky enough to be the only active CPU on this speculation 2965 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2966 */ 2967 if (current == DTRACESPEC_ACTIVE || 2968 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2969 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2970 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2971 2972 ASSERT(rval == DTRACESPEC_COMMITTING); 2973 } 2974 2975 src->dtb_offset = 0; 2976 src->dtb_xamot_drops += src->dtb_drops; 2977 src->dtb_drops = 0; 2978 } 2979 2980 /* 2981 * This routine discards an active speculation. If the specified speculation 2982 * is not in a valid state to perform a discard(), this routine will silently 2983 * do nothing. The state of the specified speculation is transitioned 2984 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2985 */ 2986 static void 2987 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2988 dtrace_specid_t which) 2989 { 2990 dtrace_speculation_t *spec; 2991 dtrace_speculation_state_t current, new = 0; 2992 dtrace_buffer_t *buf; 2993 2994 if (which == 0) 2995 return; 2996 2997 if (which > state->dts_nspeculations) { 2998 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2999 return; 3000 } 3001 3002 spec = &state->dts_speculations[which - 1]; 3003 buf = &spec->dtsp_buffer[cpu]; 3004 3005 do { 3006 current = spec->dtsp_state; 3007 3008 switch (current) { 3009 case DTRACESPEC_INACTIVE: 3010 case DTRACESPEC_COMMITTINGMANY: 3011 case DTRACESPEC_COMMITTING: 3012 case DTRACESPEC_DISCARDING: 3013 return; 3014 3015 case DTRACESPEC_ACTIVE: 3016 case DTRACESPEC_ACTIVEMANY: 3017 new = DTRACESPEC_DISCARDING; 3018 break; 3019 3020 case DTRACESPEC_ACTIVEONE: 3021 if (buf->dtb_offset != 0) { 3022 new = DTRACESPEC_INACTIVE; 3023 } else { 3024 new = DTRACESPEC_DISCARDING; 3025 } 3026 break; 3027 3028 default: 3029 ASSERT(0); 3030 } 3031 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3032 current, new) != current); 3033 3034 buf->dtb_offset = 0; 3035 buf->dtb_drops = 0; 3036 } 3037 3038 /* 3039 * Note: not called from probe context. This function is called 3040 * asynchronously from cross call context to clean any speculations that are 3041 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 3042 * transitioned back to the INACTIVE state until all CPUs have cleaned the 3043 * speculation. 3044 */ 3045 static void 3046 dtrace_speculation_clean_here(dtrace_state_t *state) 3047 { 3048 dtrace_icookie_t cookie; 3049 processorid_t cpu = curcpu; 3050 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 3051 dtrace_specid_t i; 3052 3053 cookie = dtrace_interrupt_disable(); 3054 3055 if (dest->dtb_tomax == NULL) { 3056 dtrace_interrupt_enable(cookie); 3057 return; 3058 } 3059 3060 for (i = 0; i < state->dts_nspeculations; i++) { 3061 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3062 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 3063 3064 if (src->dtb_tomax == NULL) 3065 continue; 3066 3067 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 3068 src->dtb_offset = 0; 3069 continue; 3070 } 3071 3072 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 3073 continue; 3074 3075 if (src->dtb_offset == 0) 3076 continue; 3077 3078 dtrace_speculation_commit(state, cpu, i + 1); 3079 } 3080 3081 dtrace_interrupt_enable(cookie); 3082 } 3083 3084 /* 3085 * Note: not called from probe context. This function is called 3086 * asynchronously (and at a regular interval) to clean any speculations that 3087 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 3088 * is work to be done, it cross calls all CPUs to perform that work; 3089 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 3090 * INACTIVE state until they have been cleaned by all CPUs. 3091 */ 3092 static void 3093 dtrace_speculation_clean(dtrace_state_t *state) 3094 { 3095 int work = 0, rv; 3096 dtrace_specid_t i; 3097 3098 for (i = 0; i < state->dts_nspeculations; i++) { 3099 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3100 3101 ASSERT(!spec->dtsp_cleaning); 3102 3103 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 3104 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 3105 continue; 3106 3107 work++; 3108 spec->dtsp_cleaning = 1; 3109 } 3110 3111 if (!work) 3112 return; 3113 3114 dtrace_xcall(DTRACE_CPUALL, 3115 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 3116 3117 /* 3118 * We now know that all CPUs have committed or discarded their 3119 * speculation buffers, as appropriate. We can now set the state 3120 * to inactive. 3121 */ 3122 for (i = 0; i < state->dts_nspeculations; i++) { 3123 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3124 dtrace_speculation_state_t current, new; 3125 3126 if (!spec->dtsp_cleaning) 3127 continue; 3128 3129 current = spec->dtsp_state; 3130 ASSERT(current == DTRACESPEC_DISCARDING || 3131 current == DTRACESPEC_COMMITTINGMANY); 3132 3133 new = DTRACESPEC_INACTIVE; 3134 3135 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 3136 ASSERT(rv == current); 3137 spec->dtsp_cleaning = 0; 3138 } 3139 } 3140 3141 /* 3142 * Called as part of a speculate() to get the speculative buffer associated 3143 * with a given speculation. Returns NULL if the specified speculation is not 3144 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 3145 * the active CPU is not the specified CPU -- the speculation will be 3146 * atomically transitioned into the ACTIVEMANY state. 3147 */ 3148 static dtrace_buffer_t * 3149 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 3150 dtrace_specid_t which) 3151 { 3152 dtrace_speculation_t *spec; 3153 dtrace_speculation_state_t current, new = 0; 3154 dtrace_buffer_t *buf; 3155 3156 if (which == 0) 3157 return (NULL); 3158 3159 if (which > state->dts_nspeculations) { 3160 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 3161 return (NULL); 3162 } 3163 3164 spec = &state->dts_speculations[which - 1]; 3165 buf = &spec->dtsp_buffer[cpuid]; 3166 3167 do { 3168 current = spec->dtsp_state; 3169 3170 switch (current) { 3171 case DTRACESPEC_INACTIVE: 3172 case DTRACESPEC_COMMITTINGMANY: 3173 case DTRACESPEC_DISCARDING: 3174 return (NULL); 3175 3176 case DTRACESPEC_COMMITTING: 3177 ASSERT(buf->dtb_offset == 0); 3178 return (NULL); 3179 3180 case DTRACESPEC_ACTIVEONE: 3181 /* 3182 * This speculation is currently active on one CPU. 3183 * Check the offset in the buffer; if it's non-zero, 3184 * that CPU must be us (and we leave the state alone). 3185 * If it's zero, assume that we're starting on a new 3186 * CPU -- and change the state to indicate that the 3187 * speculation is active on more than one CPU. 3188 */ 3189 if (buf->dtb_offset != 0) 3190 return (buf); 3191 3192 new = DTRACESPEC_ACTIVEMANY; 3193 break; 3194 3195 case DTRACESPEC_ACTIVEMANY: 3196 return (buf); 3197 3198 case DTRACESPEC_ACTIVE: 3199 new = DTRACESPEC_ACTIVEONE; 3200 break; 3201 3202 default: 3203 ASSERT(0); 3204 } 3205 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3206 current, new) != current); 3207 3208 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 3209 return (buf); 3210 } 3211 3212 /* 3213 * Return a string. In the event that the user lacks the privilege to access 3214 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3215 * don't fail access checking. 3216 * 3217 * dtrace_dif_variable() uses this routine as a helper for various 3218 * builtin values such as 'execname' and 'probefunc.' 3219 */ 3220 uintptr_t 3221 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 3222 dtrace_mstate_t *mstate) 3223 { 3224 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3225 uintptr_t ret; 3226 size_t strsz; 3227 3228 /* 3229 * The easy case: this probe is allowed to read all of memory, so 3230 * we can just return this as a vanilla pointer. 3231 */ 3232 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 3233 return (addr); 3234 3235 /* 3236 * This is the tougher case: we copy the string in question from 3237 * kernel memory into scratch memory and return it that way: this 3238 * ensures that we won't trip up when access checking tests the 3239 * BYREF return value. 3240 */ 3241 strsz = dtrace_strlen((char *)addr, size) + 1; 3242 3243 if (mstate->dtms_scratch_ptr + strsz > 3244 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3245 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3246 return (0); 3247 } 3248 3249 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3250 strsz); 3251 ret = mstate->dtms_scratch_ptr; 3252 mstate->dtms_scratch_ptr += strsz; 3253 return (ret); 3254 } 3255 3256 /* 3257 * Return a string from a memoy address which is known to have one or 3258 * more concatenated, individually zero terminated, sub-strings. 3259 * In the event that the user lacks the privilege to access 3260 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3261 * don't fail access checking. 3262 * 3263 * dtrace_dif_variable() uses this routine as a helper for various 3264 * builtin values such as 'execargs'. 3265 */ 3266 static uintptr_t 3267 dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 3268 dtrace_mstate_t *mstate) 3269 { 3270 char *p; 3271 size_t i; 3272 uintptr_t ret; 3273 3274 if (mstate->dtms_scratch_ptr + strsz > 3275 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3276 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3277 return (0); 3278 } 3279 3280 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3281 strsz); 3282 3283 /* Replace sub-string termination characters with a space. */ 3284 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 3285 p++, i++) 3286 if (*p == '\0') 3287 *p = ' '; 3288 3289 ret = mstate->dtms_scratch_ptr; 3290 mstate->dtms_scratch_ptr += strsz; 3291 return (ret); 3292 } 3293 3294 /* 3295 * This function implements the DIF emulator's variable lookups. The emulator 3296 * passes a reserved variable identifier and optional built-in array index. 3297 */ 3298 static uint64_t 3299 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 3300 uint64_t ndx) 3301 { 3302 /* 3303 * If we're accessing one of the uncached arguments, we'll turn this 3304 * into a reference in the args array. 3305 */ 3306 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 3307 ndx = v - DIF_VAR_ARG0; 3308 v = DIF_VAR_ARGS; 3309 } 3310 3311 switch (v) { 3312 case DIF_VAR_ARGS: 3313 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 3314 if (ndx >= sizeof (mstate->dtms_arg) / 3315 sizeof (mstate->dtms_arg[0])) { 3316 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3317 dtrace_provider_t *pv; 3318 uint64_t val; 3319 3320 pv = mstate->dtms_probe->dtpr_provider; 3321 if (pv->dtpv_pops.dtps_getargval != NULL) 3322 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 3323 mstate->dtms_probe->dtpr_id, 3324 mstate->dtms_probe->dtpr_arg, ndx, aframes); 3325 else 3326 val = dtrace_getarg(ndx, aframes); 3327 3328 /* 3329 * This is regrettably required to keep the compiler 3330 * from tail-optimizing the call to dtrace_getarg(). 3331 * The condition always evaluates to true, but the 3332 * compiler has no way of figuring that out a priori. 3333 * (None of this would be necessary if the compiler 3334 * could be relied upon to _always_ tail-optimize 3335 * the call to dtrace_getarg() -- but it can't.) 3336 */ 3337 if (mstate->dtms_probe != NULL) 3338 return (val); 3339 3340 ASSERT(0); 3341 } 3342 3343 return (mstate->dtms_arg[ndx]); 3344 3345 #ifdef illumos 3346 case DIF_VAR_UREGS: { 3347 klwp_t *lwp; 3348 3349 if (!dtrace_priv_proc(state)) 3350 return (0); 3351 3352 if ((lwp = curthread->t_lwp) == NULL) { 3353 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3354 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 3355 return (0); 3356 } 3357 3358 return (dtrace_getreg(lwp->lwp_regs, ndx)); 3359 return (0); 3360 } 3361 #else 3362 case DIF_VAR_UREGS: { 3363 struct trapframe *tframe; 3364 3365 if (!dtrace_priv_proc(state)) 3366 return (0); 3367 3368 if ((tframe = curthread->td_frame) == NULL) { 3369 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3370 cpu_core[curcpu].cpuc_dtrace_illval = 0; 3371 return (0); 3372 } 3373 3374 return (dtrace_getreg(tframe, ndx)); 3375 } 3376 #endif 3377 3378 case DIF_VAR_CURTHREAD: 3379 if (!dtrace_priv_proc(state)) 3380 return (0); 3381 return ((uint64_t)(uintptr_t)curthread); 3382 3383 case DIF_VAR_TIMESTAMP: 3384 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 3385 mstate->dtms_timestamp = dtrace_gethrtime(); 3386 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 3387 } 3388 return (mstate->dtms_timestamp); 3389 3390 case DIF_VAR_VTIMESTAMP: 3391 ASSERT(dtrace_vtime_references != 0); 3392 return (curthread->t_dtrace_vtime); 3393 3394 case DIF_VAR_WALLTIMESTAMP: 3395 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3396 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3397 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3398 } 3399 return (mstate->dtms_walltimestamp); 3400 3401 #ifdef illumos 3402 case DIF_VAR_IPL: 3403 if (!dtrace_priv_kernel(state)) 3404 return (0); 3405 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3406 mstate->dtms_ipl = dtrace_getipl(); 3407 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3408 } 3409 return (mstate->dtms_ipl); 3410 #endif 3411 3412 case DIF_VAR_EPID: 3413 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3414 return (mstate->dtms_epid); 3415 3416 case DIF_VAR_ID: 3417 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3418 return (mstate->dtms_probe->dtpr_id); 3419 3420 case DIF_VAR_STACKDEPTH: 3421 if (!dtrace_priv_kernel(state)) 3422 return (0); 3423 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3424 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3425 3426 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3427 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3428 } 3429 return (mstate->dtms_stackdepth); 3430 3431 case DIF_VAR_USTACKDEPTH: 3432 if (!dtrace_priv_proc(state)) 3433 return (0); 3434 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3435 /* 3436 * See comment in DIF_VAR_PID. 3437 */ 3438 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3439 CPU_ON_INTR(CPU)) { 3440 mstate->dtms_ustackdepth = 0; 3441 } else { 3442 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3443 mstate->dtms_ustackdepth = 3444 dtrace_getustackdepth(); 3445 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3446 } 3447 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3448 } 3449 return (mstate->dtms_ustackdepth); 3450 3451 case DIF_VAR_CALLER: 3452 if (!dtrace_priv_kernel(state)) 3453 return (0); 3454 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3455 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3456 3457 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3458 /* 3459 * If this is an unanchored probe, we are 3460 * required to go through the slow path: 3461 * dtrace_caller() only guarantees correct 3462 * results for anchored probes. 3463 */ 3464 pc_t caller[2] = {0, 0}; 3465 3466 dtrace_getpcstack(caller, 2, aframes, 3467 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3468 mstate->dtms_caller = caller[1]; 3469 } else if ((mstate->dtms_caller = 3470 dtrace_caller(aframes)) == -1) { 3471 /* 3472 * We have failed to do this the quick way; 3473 * we must resort to the slower approach of 3474 * calling dtrace_getpcstack(). 3475 */ 3476 pc_t caller = 0; 3477 3478 dtrace_getpcstack(&caller, 1, aframes, NULL); 3479 mstate->dtms_caller = caller; 3480 } 3481 3482 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3483 } 3484 return (mstate->dtms_caller); 3485 3486 case DIF_VAR_UCALLER: 3487 if (!dtrace_priv_proc(state)) 3488 return (0); 3489 3490 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3491 uint64_t ustack[3]; 3492 3493 /* 3494 * dtrace_getupcstack() fills in the first uint64_t 3495 * with the current PID. The second uint64_t will 3496 * be the program counter at user-level. The third 3497 * uint64_t will contain the caller, which is what 3498 * we're after. 3499 */ 3500 ustack[2] = 0; 3501 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3502 dtrace_getupcstack(ustack, 3); 3503 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3504 mstate->dtms_ucaller = ustack[2]; 3505 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3506 } 3507 3508 return (mstate->dtms_ucaller); 3509 3510 case DIF_VAR_PROBEPROV: 3511 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3512 return (dtrace_dif_varstr( 3513 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3514 state, mstate)); 3515 3516 case DIF_VAR_PROBEMOD: 3517 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3518 return (dtrace_dif_varstr( 3519 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3520 state, mstate)); 3521 3522 case DIF_VAR_PROBEFUNC: 3523 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3524 return (dtrace_dif_varstr( 3525 (uintptr_t)mstate->dtms_probe->dtpr_func, 3526 state, mstate)); 3527 3528 case DIF_VAR_PROBENAME: 3529 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3530 return (dtrace_dif_varstr( 3531 (uintptr_t)mstate->dtms_probe->dtpr_name, 3532 state, mstate)); 3533 3534 case DIF_VAR_PID: 3535 if (!dtrace_priv_proc(state)) 3536 return (0); 3537 3538 #ifdef illumos 3539 /* 3540 * Note that we are assuming that an unanchored probe is 3541 * always due to a high-level interrupt. (And we're assuming 3542 * that there is only a single high level interrupt.) 3543 */ 3544 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3545 return (pid0.pid_id); 3546 3547 /* 3548 * It is always safe to dereference one's own t_procp pointer: 3549 * it always points to a valid, allocated proc structure. 3550 * Further, it is always safe to dereference the p_pidp member 3551 * of one's own proc structure. (These are truisms becuase 3552 * threads and processes don't clean up their own state -- 3553 * they leave that task to whomever reaps them.) 3554 */ 3555 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3556 #else 3557 return ((uint64_t)curproc->p_pid); 3558 #endif 3559 3560 case DIF_VAR_PPID: 3561 if (!dtrace_priv_proc(state)) 3562 return (0); 3563 3564 #ifdef illumos 3565 /* 3566 * See comment in DIF_VAR_PID. 3567 */ 3568 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3569 return (pid0.pid_id); 3570 3571 /* 3572 * It is always safe to dereference one's own t_procp pointer: 3573 * it always points to a valid, allocated proc structure. 3574 * (This is true because threads don't clean up their own 3575 * state -- they leave that task to whomever reaps them.) 3576 */ 3577 return ((uint64_t)curthread->t_procp->p_ppid); 3578 #else 3579 if (curproc->p_pid == proc0.p_pid) 3580 return (curproc->p_pid); 3581 else 3582 return (curproc->p_pptr->p_pid); 3583 #endif 3584 3585 case DIF_VAR_TID: 3586 #ifdef illumos 3587 /* 3588 * See comment in DIF_VAR_PID. 3589 */ 3590 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3591 return (0); 3592 #endif 3593 3594 return ((uint64_t)curthread->t_tid); 3595 3596 case DIF_VAR_EXECARGS: { 3597 struct pargs *p_args = curthread->td_proc->p_args; 3598 3599 if (p_args == NULL) 3600 return(0); 3601 3602 return (dtrace_dif_varstrz( 3603 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3604 } 3605 3606 case DIF_VAR_EXECNAME: 3607 #ifdef illumos 3608 if (!dtrace_priv_proc(state)) 3609 return (0); 3610 3611 /* 3612 * See comment in DIF_VAR_PID. 3613 */ 3614 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3615 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3616 3617 /* 3618 * It is always safe to dereference one's own t_procp pointer: 3619 * it always points to a valid, allocated proc structure. 3620 * (This is true because threads don't clean up their own 3621 * state -- they leave that task to whomever reaps them.) 3622 */ 3623 return (dtrace_dif_varstr( 3624 (uintptr_t)curthread->t_procp->p_user.u_comm, 3625 state, mstate)); 3626 #else 3627 return (dtrace_dif_varstr( 3628 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3629 #endif 3630 3631 case DIF_VAR_ZONENAME: 3632 #ifdef illumos 3633 if (!dtrace_priv_proc(state)) 3634 return (0); 3635 3636 /* 3637 * See comment in DIF_VAR_PID. 3638 */ 3639 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3640 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3641 3642 /* 3643 * It is always safe to dereference one's own t_procp pointer: 3644 * it always points to a valid, allocated proc structure. 3645 * (This is true because threads don't clean up their own 3646 * state -- they leave that task to whomever reaps them.) 3647 */ 3648 return (dtrace_dif_varstr( 3649 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3650 state, mstate)); 3651 #else 3652 return (0); 3653 #endif 3654 3655 case DIF_VAR_UID: 3656 if (!dtrace_priv_proc(state)) 3657 return (0); 3658 3659 #ifdef illumos 3660 /* 3661 * See comment in DIF_VAR_PID. 3662 */ 3663 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3664 return ((uint64_t)p0.p_cred->cr_uid); 3665 3666 /* 3667 * It is always safe to dereference one's own t_procp pointer: 3668 * it always points to a valid, allocated proc structure. 3669 * (This is true because threads don't clean up their own 3670 * state -- they leave that task to whomever reaps them.) 3671 * 3672 * Additionally, it is safe to dereference one's own process 3673 * credential, since this is never NULL after process birth. 3674 */ 3675 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3676 #else 3677 return ((uint64_t)curthread->td_ucred->cr_uid); 3678 #endif 3679 3680 case DIF_VAR_GID: 3681 if (!dtrace_priv_proc(state)) 3682 return (0); 3683 3684 #ifdef illumos 3685 /* 3686 * See comment in DIF_VAR_PID. 3687 */ 3688 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3689 return ((uint64_t)p0.p_cred->cr_gid); 3690 3691 /* 3692 * It is always safe to dereference one's own t_procp pointer: 3693 * it always points to a valid, allocated proc structure. 3694 * (This is true because threads don't clean up their own 3695 * state -- they leave that task to whomever reaps them.) 3696 * 3697 * Additionally, it is safe to dereference one's own process 3698 * credential, since this is never NULL after process birth. 3699 */ 3700 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3701 #else 3702 return ((uint64_t)curthread->td_ucred->cr_gid); 3703 #endif 3704 3705 case DIF_VAR_ERRNO: { 3706 #ifdef illumos 3707 klwp_t *lwp; 3708 if (!dtrace_priv_proc(state)) 3709 return (0); 3710 3711 /* 3712 * See comment in DIF_VAR_PID. 3713 */ 3714 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3715 return (0); 3716 3717 /* 3718 * It is always safe to dereference one's own t_lwp pointer in 3719 * the event that this pointer is non-NULL. (This is true 3720 * because threads and lwps don't clean up their own state -- 3721 * they leave that task to whomever reaps them.) 3722 */ 3723 if ((lwp = curthread->t_lwp) == NULL) 3724 return (0); 3725 3726 return ((uint64_t)lwp->lwp_errno); 3727 #else 3728 return (curthread->td_errno); 3729 #endif 3730 } 3731 #ifndef illumos 3732 case DIF_VAR_CPU: { 3733 return curcpu; 3734 } 3735 #endif 3736 default: 3737 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3738 return (0); 3739 } 3740 } 3741 3742 3743 typedef enum dtrace_json_state { 3744 DTRACE_JSON_REST = 1, 3745 DTRACE_JSON_OBJECT, 3746 DTRACE_JSON_STRING, 3747 DTRACE_JSON_STRING_ESCAPE, 3748 DTRACE_JSON_STRING_ESCAPE_UNICODE, 3749 DTRACE_JSON_COLON, 3750 DTRACE_JSON_COMMA, 3751 DTRACE_JSON_VALUE, 3752 DTRACE_JSON_IDENTIFIER, 3753 DTRACE_JSON_NUMBER, 3754 DTRACE_JSON_NUMBER_FRAC, 3755 DTRACE_JSON_NUMBER_EXP, 3756 DTRACE_JSON_COLLECT_OBJECT 3757 } dtrace_json_state_t; 3758 3759 /* 3760 * This function possesses just enough knowledge about JSON to extract a single 3761 * value from a JSON string and store it in the scratch buffer. It is able 3762 * to extract nested object values, and members of arrays by index. 3763 * 3764 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to 3765 * be looked up as we descend into the object tree. e.g. 3766 * 3767 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL 3768 * with nelems = 5. 3769 * 3770 * The run time of this function must be bounded above by strsize to limit the 3771 * amount of work done in probe context. As such, it is implemented as a 3772 * simple state machine, reading one character at a time using safe loads 3773 * until we find the requested element, hit a parsing error or run off the 3774 * end of the object or string. 3775 * 3776 * As there is no way for a subroutine to return an error without interrupting 3777 * clause execution, we simply return NULL in the event of a missing key or any 3778 * other error condition. Each NULL return in this function is commented with 3779 * the error condition it represents -- parsing or otherwise. 3780 * 3781 * The set of states for the state machine closely matches the JSON 3782 * specification (http://json.org/). Briefly: 3783 * 3784 * DTRACE_JSON_REST: 3785 * Skip whitespace until we find either a top-level Object, moving 3786 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE. 3787 * 3788 * DTRACE_JSON_OBJECT: 3789 * Locate the next key String in an Object. Sets a flag to denote 3790 * the next String as a key string and moves to DTRACE_JSON_STRING. 3791 * 3792 * DTRACE_JSON_COLON: 3793 * Skip whitespace until we find the colon that separates key Strings 3794 * from their values. Once found, move to DTRACE_JSON_VALUE. 3795 * 3796 * DTRACE_JSON_VALUE: 3797 * Detects the type of the next value (String, Number, Identifier, Object 3798 * or Array) and routes to the states that process that type. Here we also 3799 * deal with the element selector list if we are requested to traverse down 3800 * into the object tree. 3801 * 3802 * DTRACE_JSON_COMMA: 3803 * Skip whitespace until we find the comma that separates key-value pairs 3804 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays 3805 * (similarly DTRACE_JSON_VALUE). All following literal value processing 3806 * states return to this state at the end of their value, unless otherwise 3807 * noted. 3808 * 3809 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP: 3810 * Processes a Number literal from the JSON, including any exponent 3811 * component that may be present. Numbers are returned as strings, which 3812 * may be passed to strtoll() if an integer is required. 3813 * 3814 * DTRACE_JSON_IDENTIFIER: 3815 * Processes a "true", "false" or "null" literal in the JSON. 3816 * 3817 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE, 3818 * DTRACE_JSON_STRING_ESCAPE_UNICODE: 3819 * Processes a String literal from the JSON, whether the String denotes 3820 * a key, a value or part of a larger Object. Handles all escape sequences 3821 * present in the specification, including four-digit unicode characters, 3822 * but merely includes the escape sequence without converting it to the 3823 * actual escaped character. If the String is flagged as a key, we 3824 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA. 3825 * 3826 * DTRACE_JSON_COLLECT_OBJECT: 3827 * This state collects an entire Object (or Array), correctly handling 3828 * embedded strings. If the full element selector list matches this nested 3829 * object, we return the Object in full as a string. If not, we use this 3830 * state to skip to the next value at this level and continue processing. 3831 * 3832 * NOTE: This function uses various macros from strtolctype.h to manipulate 3833 * digit values, etc -- these have all been checked to ensure they make 3834 * no additional function calls. 3835 */ 3836 static char * 3837 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems, 3838 char *dest) 3839 { 3840 dtrace_json_state_t state = DTRACE_JSON_REST; 3841 int64_t array_elem = INT64_MIN; 3842 int64_t array_pos = 0; 3843 uint8_t escape_unicount = 0; 3844 boolean_t string_is_key = B_FALSE; 3845 boolean_t collect_object = B_FALSE; 3846 boolean_t found_key = B_FALSE; 3847 boolean_t in_array = B_FALSE; 3848 uint32_t braces = 0, brackets = 0; 3849 char *elem = elemlist; 3850 char *dd = dest; 3851 uintptr_t cur; 3852 3853 for (cur = json; cur < json + size; cur++) { 3854 char cc = dtrace_load8(cur); 3855 if (cc == '\0') 3856 return (NULL); 3857 3858 switch (state) { 3859 case DTRACE_JSON_REST: 3860 if (isspace(cc)) 3861 break; 3862 3863 if (cc == '{') { 3864 state = DTRACE_JSON_OBJECT; 3865 break; 3866 } 3867 3868 if (cc == '[') { 3869 in_array = B_TRUE; 3870 array_pos = 0; 3871 array_elem = dtrace_strtoll(elem, 10, size); 3872 found_key = array_elem == 0 ? B_TRUE : B_FALSE; 3873 state = DTRACE_JSON_VALUE; 3874 break; 3875 } 3876 3877 /* 3878 * ERROR: expected to find a top-level object or array. 3879 */ 3880 return (NULL); 3881 case DTRACE_JSON_OBJECT: 3882 if (isspace(cc)) 3883 break; 3884 3885 if (cc == '"') { 3886 state = DTRACE_JSON_STRING; 3887 string_is_key = B_TRUE; 3888 break; 3889 } 3890 3891 /* 3892 * ERROR: either the object did not start with a key 3893 * string, or we've run off the end of the object 3894 * without finding the requested key. 3895 */ 3896 return (NULL); 3897 case DTRACE_JSON_STRING: 3898 if (cc == '\\') { 3899 *dd++ = '\\'; 3900 state = DTRACE_JSON_STRING_ESCAPE; 3901 break; 3902 } 3903 3904 if (cc == '"') { 3905 if (collect_object) { 3906 /* 3907 * We don't reset the dest here, as 3908 * the string is part of a larger 3909 * object being collected. 3910 */ 3911 *dd++ = cc; 3912 collect_object = B_FALSE; 3913 state = DTRACE_JSON_COLLECT_OBJECT; 3914 break; 3915 } 3916 *dd = '\0'; 3917 dd = dest; /* reset string buffer */ 3918 if (string_is_key) { 3919 if (dtrace_strncmp(dest, elem, 3920 size) == 0) 3921 found_key = B_TRUE; 3922 } else if (found_key) { 3923 if (nelems > 1) { 3924 /* 3925 * We expected an object, not 3926 * this string. 3927 */ 3928 return (NULL); 3929 } 3930 return (dest); 3931 } 3932 state = string_is_key ? DTRACE_JSON_COLON : 3933 DTRACE_JSON_COMMA; 3934 string_is_key = B_FALSE; 3935 break; 3936 } 3937 3938 *dd++ = cc; 3939 break; 3940 case DTRACE_JSON_STRING_ESCAPE: 3941 *dd++ = cc; 3942 if (cc == 'u') { 3943 escape_unicount = 0; 3944 state = DTRACE_JSON_STRING_ESCAPE_UNICODE; 3945 } else { 3946 state = DTRACE_JSON_STRING; 3947 } 3948 break; 3949 case DTRACE_JSON_STRING_ESCAPE_UNICODE: 3950 if (!isxdigit(cc)) { 3951 /* 3952 * ERROR: invalid unicode escape, expected 3953 * four valid hexidecimal digits. 3954 */ 3955 return (NULL); 3956 } 3957 3958 *dd++ = cc; 3959 if (++escape_unicount == 4) 3960 state = DTRACE_JSON_STRING; 3961 break; 3962 case DTRACE_JSON_COLON: 3963 if (isspace(cc)) 3964 break; 3965 3966 if (cc == ':') { 3967 state = DTRACE_JSON_VALUE; 3968 break; 3969 } 3970 3971 /* 3972 * ERROR: expected a colon. 3973 */ 3974 return (NULL); 3975 case DTRACE_JSON_COMMA: 3976 if (isspace(cc)) 3977 break; 3978 3979 if (cc == ',') { 3980 if (in_array) { 3981 state = DTRACE_JSON_VALUE; 3982 if (++array_pos == array_elem) 3983 found_key = B_TRUE; 3984 } else { 3985 state = DTRACE_JSON_OBJECT; 3986 } 3987 break; 3988 } 3989 3990 /* 3991 * ERROR: either we hit an unexpected character, or 3992 * we reached the end of the object or array without 3993 * finding the requested key. 3994 */ 3995 return (NULL); 3996 case DTRACE_JSON_IDENTIFIER: 3997 if (islower(cc)) { 3998 *dd++ = cc; 3999 break; 4000 } 4001 4002 *dd = '\0'; 4003 dd = dest; /* reset string buffer */ 4004 4005 if (dtrace_strncmp(dest, "true", 5) == 0 || 4006 dtrace_strncmp(dest, "false", 6) == 0 || 4007 dtrace_strncmp(dest, "null", 5) == 0) { 4008 if (found_key) { 4009 if (nelems > 1) { 4010 /* 4011 * ERROR: We expected an object, 4012 * not this identifier. 4013 */ 4014 return (NULL); 4015 } 4016 return (dest); 4017 } else { 4018 cur--; 4019 state = DTRACE_JSON_COMMA; 4020 break; 4021 } 4022 } 4023 4024 /* 4025 * ERROR: we did not recognise the identifier as one 4026 * of those in the JSON specification. 4027 */ 4028 return (NULL); 4029 case DTRACE_JSON_NUMBER: 4030 if (cc == '.') { 4031 *dd++ = cc; 4032 state = DTRACE_JSON_NUMBER_FRAC; 4033 break; 4034 } 4035 4036 if (cc == 'x' || cc == 'X') { 4037 /* 4038 * ERROR: specification explicitly excludes 4039 * hexidecimal or octal numbers. 4040 */ 4041 return (NULL); 4042 } 4043 4044 /* FALLTHRU */ 4045 case DTRACE_JSON_NUMBER_FRAC: 4046 if (cc == 'e' || cc == 'E') { 4047 *dd++ = cc; 4048 state = DTRACE_JSON_NUMBER_EXP; 4049 break; 4050 } 4051 4052 if (cc == '+' || cc == '-') { 4053 /* 4054 * ERROR: expect sign as part of exponent only. 4055 */ 4056 return (NULL); 4057 } 4058 /* FALLTHRU */ 4059 case DTRACE_JSON_NUMBER_EXP: 4060 if (isdigit(cc) || cc == '+' || cc == '-') { 4061 *dd++ = cc; 4062 break; 4063 } 4064 4065 *dd = '\0'; 4066 dd = dest; /* reset string buffer */ 4067 if (found_key) { 4068 if (nelems > 1) { 4069 /* 4070 * ERROR: We expected an object, not 4071 * this number. 4072 */ 4073 return (NULL); 4074 } 4075 return (dest); 4076 } 4077 4078 cur--; 4079 state = DTRACE_JSON_COMMA; 4080 break; 4081 case DTRACE_JSON_VALUE: 4082 if (isspace(cc)) 4083 break; 4084 4085 if (cc == '{' || cc == '[') { 4086 if (nelems > 1 && found_key) { 4087 in_array = cc == '[' ? B_TRUE : B_FALSE; 4088 /* 4089 * If our element selector directs us 4090 * to descend into this nested object, 4091 * then move to the next selector 4092 * element in the list and restart the 4093 * state machine. 4094 */ 4095 while (*elem != '\0') 4096 elem++; 4097 elem++; /* skip the inter-element NUL */ 4098 nelems--; 4099 dd = dest; 4100 if (in_array) { 4101 state = DTRACE_JSON_VALUE; 4102 array_pos = 0; 4103 array_elem = dtrace_strtoll( 4104 elem, 10, size); 4105 found_key = array_elem == 0 ? 4106 B_TRUE : B_FALSE; 4107 } else { 4108 found_key = B_FALSE; 4109 state = DTRACE_JSON_OBJECT; 4110 } 4111 break; 4112 } 4113 4114 /* 4115 * Otherwise, we wish to either skip this 4116 * nested object or return it in full. 4117 */ 4118 if (cc == '[') 4119 brackets = 1; 4120 else 4121 braces = 1; 4122 *dd++ = cc; 4123 state = DTRACE_JSON_COLLECT_OBJECT; 4124 break; 4125 } 4126 4127 if (cc == '"') { 4128 state = DTRACE_JSON_STRING; 4129 break; 4130 } 4131 4132 if (islower(cc)) { 4133 /* 4134 * Here we deal with true, false and null. 4135 */ 4136 *dd++ = cc; 4137 state = DTRACE_JSON_IDENTIFIER; 4138 break; 4139 } 4140 4141 if (cc == '-' || isdigit(cc)) { 4142 *dd++ = cc; 4143 state = DTRACE_JSON_NUMBER; 4144 break; 4145 } 4146 4147 /* 4148 * ERROR: unexpected character at start of value. 4149 */ 4150 return (NULL); 4151 case DTRACE_JSON_COLLECT_OBJECT: 4152 if (cc == '\0') 4153 /* 4154 * ERROR: unexpected end of input. 4155 */ 4156 return (NULL); 4157 4158 *dd++ = cc; 4159 if (cc == '"') { 4160 collect_object = B_TRUE; 4161 state = DTRACE_JSON_STRING; 4162 break; 4163 } 4164 4165 if (cc == ']') { 4166 if (brackets-- == 0) { 4167 /* 4168 * ERROR: unbalanced brackets. 4169 */ 4170 return (NULL); 4171 } 4172 } else if (cc == '}') { 4173 if (braces-- == 0) { 4174 /* 4175 * ERROR: unbalanced braces. 4176 */ 4177 return (NULL); 4178 } 4179 } else if (cc == '{') { 4180 braces++; 4181 } else if (cc == '[') { 4182 brackets++; 4183 } 4184 4185 if (brackets == 0 && braces == 0) { 4186 if (found_key) { 4187 *dd = '\0'; 4188 return (dest); 4189 } 4190 dd = dest; /* reset string buffer */ 4191 state = DTRACE_JSON_COMMA; 4192 } 4193 break; 4194 } 4195 } 4196 return (NULL); 4197 } 4198 4199 /* 4200 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 4201 * Notice that we don't bother validating the proper number of arguments or 4202 * their types in the tuple stack. This isn't needed because all argument 4203 * interpretation is safe because of our load safety -- the worst that can 4204 * happen is that a bogus program can obtain bogus results. 4205 */ 4206 static void 4207 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 4208 dtrace_key_t *tupregs, int nargs, 4209 dtrace_mstate_t *mstate, dtrace_state_t *state) 4210 { 4211 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4212 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4213 dtrace_vstate_t *vstate = &state->dts_vstate; 4214 4215 #ifdef illumos 4216 union { 4217 mutex_impl_t mi; 4218 uint64_t mx; 4219 } m; 4220 4221 union { 4222 krwlock_t ri; 4223 uintptr_t rw; 4224 } r; 4225 #else 4226 struct thread *lowner; 4227 union { 4228 struct lock_object *li; 4229 uintptr_t lx; 4230 } l; 4231 #endif 4232 4233 switch (subr) { 4234 case DIF_SUBR_RAND: 4235 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 4236 break; 4237 4238 #ifdef illumos 4239 case DIF_SUBR_MUTEX_OWNED: 4240 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4241 mstate, vstate)) { 4242 regs[rd] = 0; 4243 break; 4244 } 4245 4246 m.mx = dtrace_load64(tupregs[0].dttk_value); 4247 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 4248 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 4249 else 4250 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 4251 break; 4252 4253 case DIF_SUBR_MUTEX_OWNER: 4254 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4255 mstate, vstate)) { 4256 regs[rd] = 0; 4257 break; 4258 } 4259 4260 m.mx = dtrace_load64(tupregs[0].dttk_value); 4261 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 4262 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 4263 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 4264 else 4265 regs[rd] = 0; 4266 break; 4267 4268 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4269 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4270 mstate, vstate)) { 4271 regs[rd] = 0; 4272 break; 4273 } 4274 4275 m.mx = dtrace_load64(tupregs[0].dttk_value); 4276 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 4277 break; 4278 4279 case DIF_SUBR_MUTEX_TYPE_SPIN: 4280 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4281 mstate, vstate)) { 4282 regs[rd] = 0; 4283 break; 4284 } 4285 4286 m.mx = dtrace_load64(tupregs[0].dttk_value); 4287 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 4288 break; 4289 4290 case DIF_SUBR_RW_READ_HELD: { 4291 uintptr_t tmp; 4292 4293 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4294 mstate, vstate)) { 4295 regs[rd] = 0; 4296 break; 4297 } 4298 4299 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4300 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 4301 break; 4302 } 4303 4304 case DIF_SUBR_RW_WRITE_HELD: 4305 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4306 mstate, vstate)) { 4307 regs[rd] = 0; 4308 break; 4309 } 4310 4311 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4312 regs[rd] = _RW_WRITE_HELD(&r.ri); 4313 break; 4314 4315 case DIF_SUBR_RW_ISWRITER: 4316 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4317 mstate, vstate)) { 4318 regs[rd] = 0; 4319 break; 4320 } 4321 4322 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4323 regs[rd] = _RW_ISWRITER(&r.ri); 4324 break; 4325 4326 #else /* !illumos */ 4327 case DIF_SUBR_MUTEX_OWNED: 4328 if (!dtrace_canload(tupregs[0].dttk_value, 4329 sizeof (struct lock_object), mstate, vstate)) { 4330 regs[rd] = 0; 4331 break; 4332 } 4333 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4334 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4335 break; 4336 4337 case DIF_SUBR_MUTEX_OWNER: 4338 if (!dtrace_canload(tupregs[0].dttk_value, 4339 sizeof (struct lock_object), mstate, vstate)) { 4340 regs[rd] = 0; 4341 break; 4342 } 4343 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4344 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4345 regs[rd] = (uintptr_t)lowner; 4346 break; 4347 4348 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4349 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4350 mstate, vstate)) { 4351 regs[rd] = 0; 4352 break; 4353 } 4354 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4355 /* XXX - should be only LC_SLEEPABLE? */ 4356 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & 4357 (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0; 4358 break; 4359 4360 case DIF_SUBR_MUTEX_TYPE_SPIN: 4361 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4362 mstate, vstate)) { 4363 regs[rd] = 0; 4364 break; 4365 } 4366 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4367 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 4368 break; 4369 4370 case DIF_SUBR_RW_READ_HELD: 4371 case DIF_SUBR_SX_SHARED_HELD: 4372 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4373 mstate, vstate)) { 4374 regs[rd] = 0; 4375 break; 4376 } 4377 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4378 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4379 lowner == NULL; 4380 break; 4381 4382 case DIF_SUBR_RW_WRITE_HELD: 4383 case DIF_SUBR_SX_EXCLUSIVE_HELD: 4384 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4385 mstate, vstate)) { 4386 regs[rd] = 0; 4387 break; 4388 } 4389 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4390 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4391 regs[rd] = (lowner == curthread); 4392 break; 4393 4394 case DIF_SUBR_RW_ISWRITER: 4395 case DIF_SUBR_SX_ISEXCLUSIVE: 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 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4403 lowner != NULL; 4404 break; 4405 #endif /* illumos */ 4406 4407 case DIF_SUBR_BCOPY: { 4408 /* 4409 * We need to be sure that the destination is in the scratch 4410 * region -- no other region is allowed. 4411 */ 4412 uintptr_t src = tupregs[0].dttk_value; 4413 uintptr_t dest = tupregs[1].dttk_value; 4414 size_t size = tupregs[2].dttk_value; 4415 4416 if (!dtrace_inscratch(dest, size, mstate)) { 4417 *flags |= CPU_DTRACE_BADADDR; 4418 *illval = regs[rd]; 4419 break; 4420 } 4421 4422 if (!dtrace_canload(src, size, mstate, vstate)) { 4423 regs[rd] = 0; 4424 break; 4425 } 4426 4427 dtrace_bcopy((void *)src, (void *)dest, size); 4428 break; 4429 } 4430 4431 case DIF_SUBR_ALLOCA: 4432 case DIF_SUBR_COPYIN: { 4433 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4434 uint64_t size = 4435 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 4436 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 4437 4438 /* 4439 * This action doesn't require any credential checks since 4440 * probes will not activate in user contexts to which the 4441 * enabling user does not have permissions. 4442 */ 4443 4444 /* 4445 * Rounding up the user allocation size could have overflowed 4446 * a large, bogus allocation (like -1ULL) to 0. 4447 */ 4448 if (scratch_size < size || 4449 !DTRACE_INSCRATCH(mstate, scratch_size)) { 4450 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4451 regs[rd] = 0; 4452 break; 4453 } 4454 4455 if (subr == DIF_SUBR_COPYIN) { 4456 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4457 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4458 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4459 } 4460 4461 mstate->dtms_scratch_ptr += scratch_size; 4462 regs[rd] = dest; 4463 break; 4464 } 4465 4466 case DIF_SUBR_COPYINTO: { 4467 uint64_t size = tupregs[1].dttk_value; 4468 uintptr_t dest = tupregs[2].dttk_value; 4469 4470 /* 4471 * This action doesn't require any credential checks since 4472 * probes will not activate in user contexts to which the 4473 * enabling user does not have permissions. 4474 */ 4475 if (!dtrace_inscratch(dest, size, mstate)) { 4476 *flags |= CPU_DTRACE_BADADDR; 4477 *illval = regs[rd]; 4478 break; 4479 } 4480 4481 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4482 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4483 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4484 break; 4485 } 4486 4487 case DIF_SUBR_COPYINSTR: { 4488 uintptr_t dest = mstate->dtms_scratch_ptr; 4489 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4490 4491 if (nargs > 1 && tupregs[1].dttk_value < size) 4492 size = tupregs[1].dttk_value + 1; 4493 4494 /* 4495 * This action doesn't require any credential checks since 4496 * probes will not activate in user contexts to which the 4497 * enabling user does not have permissions. 4498 */ 4499 if (!DTRACE_INSCRATCH(mstate, size)) { 4500 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4501 regs[rd] = 0; 4502 break; 4503 } 4504 4505 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4506 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 4507 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4508 4509 ((char *)dest)[size - 1] = '\0'; 4510 mstate->dtms_scratch_ptr += size; 4511 regs[rd] = dest; 4512 break; 4513 } 4514 4515 #ifdef illumos 4516 case DIF_SUBR_MSGSIZE: 4517 case DIF_SUBR_MSGDSIZE: { 4518 uintptr_t baddr = tupregs[0].dttk_value, daddr; 4519 uintptr_t wptr, rptr; 4520 size_t count = 0; 4521 int cont = 0; 4522 4523 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 4524 4525 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 4526 vstate)) { 4527 regs[rd] = 0; 4528 break; 4529 } 4530 4531 wptr = dtrace_loadptr(baddr + 4532 offsetof(mblk_t, b_wptr)); 4533 4534 rptr = dtrace_loadptr(baddr + 4535 offsetof(mblk_t, b_rptr)); 4536 4537 if (wptr < rptr) { 4538 *flags |= CPU_DTRACE_BADADDR; 4539 *illval = tupregs[0].dttk_value; 4540 break; 4541 } 4542 4543 daddr = dtrace_loadptr(baddr + 4544 offsetof(mblk_t, b_datap)); 4545 4546 baddr = dtrace_loadptr(baddr + 4547 offsetof(mblk_t, b_cont)); 4548 4549 /* 4550 * We want to prevent against denial-of-service here, 4551 * so we're only going to search the list for 4552 * dtrace_msgdsize_max mblks. 4553 */ 4554 if (cont++ > dtrace_msgdsize_max) { 4555 *flags |= CPU_DTRACE_ILLOP; 4556 break; 4557 } 4558 4559 if (subr == DIF_SUBR_MSGDSIZE) { 4560 if (dtrace_load8(daddr + 4561 offsetof(dblk_t, db_type)) != M_DATA) 4562 continue; 4563 } 4564 4565 count += wptr - rptr; 4566 } 4567 4568 if (!(*flags & CPU_DTRACE_FAULT)) 4569 regs[rd] = count; 4570 4571 break; 4572 } 4573 #endif 4574 4575 case DIF_SUBR_PROGENYOF: { 4576 pid_t pid = tupregs[0].dttk_value; 4577 proc_t *p; 4578 int rval = 0; 4579 4580 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4581 4582 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 4583 #ifdef illumos 4584 if (p->p_pidp->pid_id == pid) { 4585 #else 4586 if (p->p_pid == pid) { 4587 #endif 4588 rval = 1; 4589 break; 4590 } 4591 } 4592 4593 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4594 4595 regs[rd] = rval; 4596 break; 4597 } 4598 4599 case DIF_SUBR_SPECULATION: 4600 regs[rd] = dtrace_speculation(state); 4601 break; 4602 4603 case DIF_SUBR_COPYOUT: { 4604 uintptr_t kaddr = tupregs[0].dttk_value; 4605 uintptr_t uaddr = tupregs[1].dttk_value; 4606 uint64_t size = tupregs[2].dttk_value; 4607 4608 if (!dtrace_destructive_disallow && 4609 dtrace_priv_proc_control(state) && 4610 !dtrace_istoxic(kaddr, size) && 4611 dtrace_canload(kaddr, size, mstate, vstate)) { 4612 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4613 dtrace_copyout(kaddr, uaddr, size, flags); 4614 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4615 } 4616 break; 4617 } 4618 4619 case DIF_SUBR_COPYOUTSTR: { 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 size_t lim; 4624 4625 if (!dtrace_destructive_disallow && 4626 dtrace_priv_proc_control(state) && 4627 !dtrace_istoxic(kaddr, size) && 4628 dtrace_strcanload(kaddr, size, &lim, mstate, vstate)) { 4629 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4630 dtrace_copyoutstr(kaddr, uaddr, lim, flags); 4631 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4632 } 4633 break; 4634 } 4635 4636 case DIF_SUBR_STRLEN: { 4637 size_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4638 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 4639 size_t lim; 4640 4641 if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) { 4642 regs[rd] = 0; 4643 break; 4644 } 4645 4646 regs[rd] = dtrace_strlen((char *)addr, lim); 4647 break; 4648 } 4649 4650 case DIF_SUBR_STRCHR: 4651 case DIF_SUBR_STRRCHR: { 4652 /* 4653 * We're going to iterate over the string looking for the 4654 * specified character. We will iterate until we have reached 4655 * the string length or we have found the character. If this 4656 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 4657 * of the specified character instead of the first. 4658 */ 4659 uintptr_t addr = tupregs[0].dttk_value; 4660 uintptr_t addr_limit; 4661 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4662 size_t lim; 4663 char c, target = (char)tupregs[1].dttk_value; 4664 4665 if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) { 4666 regs[rd] = 0; 4667 break; 4668 } 4669 addr_limit = addr + lim; 4670 4671 for (regs[rd] = 0; addr < addr_limit; addr++) { 4672 if ((c = dtrace_load8(addr)) == target) { 4673 regs[rd] = addr; 4674 4675 if (subr == DIF_SUBR_STRCHR) 4676 break; 4677 } 4678 4679 if (c == '\0') 4680 break; 4681 } 4682 break; 4683 } 4684 4685 case DIF_SUBR_STRSTR: 4686 case DIF_SUBR_INDEX: 4687 case DIF_SUBR_RINDEX: { 4688 /* 4689 * We're going to iterate over the string looking for the 4690 * specified string. We will iterate until we have reached 4691 * the string length or we have found the string. (Yes, this 4692 * is done in the most naive way possible -- but considering 4693 * that the string we're searching for is likely to be 4694 * relatively short, the complexity of Rabin-Karp or similar 4695 * hardly seems merited.) 4696 */ 4697 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 4698 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 4699 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4700 size_t len = dtrace_strlen(addr, size); 4701 size_t sublen = dtrace_strlen(substr, size); 4702 char *limit = addr + len, *orig = addr; 4703 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 4704 int inc = 1; 4705 4706 regs[rd] = notfound; 4707 4708 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 4709 regs[rd] = 0; 4710 break; 4711 } 4712 4713 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 4714 vstate)) { 4715 regs[rd] = 0; 4716 break; 4717 } 4718 4719 /* 4720 * strstr() and index()/rindex() have similar semantics if 4721 * both strings are the empty string: strstr() returns a 4722 * pointer to the (empty) string, and index() and rindex() 4723 * both return index 0 (regardless of any position argument). 4724 */ 4725 if (sublen == 0 && len == 0) { 4726 if (subr == DIF_SUBR_STRSTR) 4727 regs[rd] = (uintptr_t)addr; 4728 else 4729 regs[rd] = 0; 4730 break; 4731 } 4732 4733 if (subr != DIF_SUBR_STRSTR) { 4734 if (subr == DIF_SUBR_RINDEX) { 4735 limit = orig - 1; 4736 addr += len; 4737 inc = -1; 4738 } 4739 4740 /* 4741 * Both index() and rindex() take an optional position 4742 * argument that denotes the starting position. 4743 */ 4744 if (nargs == 3) { 4745 int64_t pos = (int64_t)tupregs[2].dttk_value; 4746 4747 /* 4748 * If the position argument to index() is 4749 * negative, Perl implicitly clamps it at 4750 * zero. This semantic is a little surprising 4751 * given the special meaning of negative 4752 * positions to similar Perl functions like 4753 * substr(), but it appears to reflect a 4754 * notion that index() can start from a 4755 * negative index and increment its way up to 4756 * the string. Given this notion, Perl's 4757 * rindex() is at least self-consistent in 4758 * that it implicitly clamps positions greater 4759 * than the string length to be the string 4760 * length. Where Perl completely loses 4761 * coherence, however, is when the specified 4762 * substring is the empty string (""). In 4763 * this case, even if the position is 4764 * negative, rindex() returns 0 -- and even if 4765 * the position is greater than the length, 4766 * index() returns the string length. These 4767 * semantics violate the notion that index() 4768 * should never return a value less than the 4769 * specified position and that rindex() should 4770 * never return a value greater than the 4771 * specified position. (One assumes that 4772 * these semantics are artifacts of Perl's 4773 * implementation and not the results of 4774 * deliberate design -- it beggars belief that 4775 * even Larry Wall could desire such oddness.) 4776 * While in the abstract one would wish for 4777 * consistent position semantics across 4778 * substr(), index() and rindex() -- or at the 4779 * very least self-consistent position 4780 * semantics for index() and rindex() -- we 4781 * instead opt to keep with the extant Perl 4782 * semantics, in all their broken glory. (Do 4783 * we have more desire to maintain Perl's 4784 * semantics than Perl does? Probably.) 4785 */ 4786 if (subr == DIF_SUBR_RINDEX) { 4787 if (pos < 0) { 4788 if (sublen == 0) 4789 regs[rd] = 0; 4790 break; 4791 } 4792 4793 if (pos > len) 4794 pos = len; 4795 } else { 4796 if (pos < 0) 4797 pos = 0; 4798 4799 if (pos >= len) { 4800 if (sublen == 0) 4801 regs[rd] = len; 4802 break; 4803 } 4804 } 4805 4806 addr = orig + pos; 4807 } 4808 } 4809 4810 for (regs[rd] = notfound; addr != limit; addr += inc) { 4811 if (dtrace_strncmp(addr, substr, sublen) == 0) { 4812 if (subr != DIF_SUBR_STRSTR) { 4813 /* 4814 * As D index() and rindex() are 4815 * modeled on Perl (and not on awk), 4816 * we return a zero-based (and not a 4817 * one-based) index. (For you Perl 4818 * weenies: no, we're not going to add 4819 * $[ -- and shouldn't you be at a con 4820 * or something?) 4821 */ 4822 regs[rd] = (uintptr_t)(addr - orig); 4823 break; 4824 } 4825 4826 ASSERT(subr == DIF_SUBR_STRSTR); 4827 regs[rd] = (uintptr_t)addr; 4828 break; 4829 } 4830 } 4831 4832 break; 4833 } 4834 4835 case DIF_SUBR_STRTOK: { 4836 uintptr_t addr = tupregs[0].dttk_value; 4837 uintptr_t tokaddr = tupregs[1].dttk_value; 4838 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4839 uintptr_t limit, toklimit; 4840 size_t clim; 4841 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 4842 char *dest = (char *)mstate->dtms_scratch_ptr; 4843 int i; 4844 4845 /* 4846 * Check both the token buffer and (later) the input buffer, 4847 * since both could be non-scratch addresses. 4848 */ 4849 if (!dtrace_strcanload(tokaddr, size, &clim, mstate, vstate)) { 4850 regs[rd] = 0; 4851 break; 4852 } 4853 toklimit = tokaddr + clim; 4854 4855 if (!DTRACE_INSCRATCH(mstate, size)) { 4856 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4857 regs[rd] = 0; 4858 break; 4859 } 4860 4861 if (addr == 0) { 4862 /* 4863 * If the address specified is NULL, we use our saved 4864 * strtok pointer from the mstate. Note that this 4865 * means that the saved strtok pointer is _only_ 4866 * valid within multiple enablings of the same probe -- 4867 * it behaves like an implicit clause-local variable. 4868 */ 4869 addr = mstate->dtms_strtok; 4870 limit = mstate->dtms_strtok_limit; 4871 } else { 4872 /* 4873 * If the user-specified address is non-NULL we must 4874 * access check it. This is the only time we have 4875 * a chance to do so, since this address may reside 4876 * in the string table of this clause-- future calls 4877 * (when we fetch addr from mstate->dtms_strtok) 4878 * would fail this access check. 4879 */ 4880 if (!dtrace_strcanload(addr, size, &clim, mstate, 4881 vstate)) { 4882 regs[rd] = 0; 4883 break; 4884 } 4885 limit = addr + clim; 4886 } 4887 4888 /* 4889 * First, zero the token map, and then process the token 4890 * string -- setting a bit in the map for every character 4891 * found in the token string. 4892 */ 4893 for (i = 0; i < sizeof (tokmap); i++) 4894 tokmap[i] = 0; 4895 4896 for (; tokaddr < toklimit; tokaddr++) { 4897 if ((c = dtrace_load8(tokaddr)) == '\0') 4898 break; 4899 4900 ASSERT((c >> 3) < sizeof (tokmap)); 4901 tokmap[c >> 3] |= (1 << (c & 0x7)); 4902 } 4903 4904 for (; addr < limit; addr++) { 4905 /* 4906 * We're looking for a character that is _not_ 4907 * contained in the token string. 4908 */ 4909 if ((c = dtrace_load8(addr)) == '\0') 4910 break; 4911 4912 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 4913 break; 4914 } 4915 4916 if (c == '\0') { 4917 /* 4918 * We reached the end of the string without finding 4919 * any character that was not in the token string. 4920 * We return NULL in this case, and we set the saved 4921 * address to NULL as well. 4922 */ 4923 regs[rd] = 0; 4924 mstate->dtms_strtok = 0; 4925 mstate->dtms_strtok_limit = 0; 4926 break; 4927 } 4928 4929 /* 4930 * From here on, we're copying into the destination string. 4931 */ 4932 for (i = 0; addr < limit && i < size - 1; addr++) { 4933 if ((c = dtrace_load8(addr)) == '\0') 4934 break; 4935 4936 if (tokmap[c >> 3] & (1 << (c & 0x7))) 4937 break; 4938 4939 ASSERT(i < size); 4940 dest[i++] = c; 4941 } 4942 4943 ASSERT(i < size); 4944 dest[i] = '\0'; 4945 regs[rd] = (uintptr_t)dest; 4946 mstate->dtms_scratch_ptr += size; 4947 mstate->dtms_strtok = addr; 4948 mstate->dtms_strtok_limit = limit; 4949 break; 4950 } 4951 4952 case DIF_SUBR_SUBSTR: { 4953 uintptr_t s = tupregs[0].dttk_value; 4954 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4955 char *d = (char *)mstate->dtms_scratch_ptr; 4956 int64_t index = (int64_t)tupregs[1].dttk_value; 4957 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4958 size_t len = dtrace_strlen((char *)s, size); 4959 int64_t i; 4960 4961 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4962 regs[rd] = 0; 4963 break; 4964 } 4965 4966 if (!DTRACE_INSCRATCH(mstate, size)) { 4967 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4968 regs[rd] = 0; 4969 break; 4970 } 4971 4972 if (nargs <= 2) 4973 remaining = (int64_t)size; 4974 4975 if (index < 0) { 4976 index += len; 4977 4978 if (index < 0 && index + remaining > 0) { 4979 remaining += index; 4980 index = 0; 4981 } 4982 } 4983 4984 if (index >= len || index < 0) { 4985 remaining = 0; 4986 } else if (remaining < 0) { 4987 remaining += len - index; 4988 } else if (index + remaining > size) { 4989 remaining = size - index; 4990 } 4991 4992 for (i = 0; i < remaining; i++) { 4993 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4994 break; 4995 } 4996 4997 d[i] = '\0'; 4998 4999 mstate->dtms_scratch_ptr += size; 5000 regs[rd] = (uintptr_t)d; 5001 break; 5002 } 5003 5004 case DIF_SUBR_JSON: { 5005 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5006 uintptr_t json = tupregs[0].dttk_value; 5007 size_t jsonlen = dtrace_strlen((char *)json, size); 5008 uintptr_t elem = tupregs[1].dttk_value; 5009 size_t elemlen = dtrace_strlen((char *)elem, size); 5010 5011 char *dest = (char *)mstate->dtms_scratch_ptr; 5012 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1; 5013 char *ee = elemlist; 5014 int nelems = 1; 5015 uintptr_t cur; 5016 5017 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) || 5018 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) { 5019 regs[rd] = 0; 5020 break; 5021 } 5022 5023 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) { 5024 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5025 regs[rd] = 0; 5026 break; 5027 } 5028 5029 /* 5030 * Read the element selector and split it up into a packed list 5031 * of strings. 5032 */ 5033 for (cur = elem; cur < elem + elemlen; cur++) { 5034 char cc = dtrace_load8(cur); 5035 5036 if (cur == elem && cc == '[') { 5037 /* 5038 * If the first element selector key is 5039 * actually an array index then ignore the 5040 * bracket. 5041 */ 5042 continue; 5043 } 5044 5045 if (cc == ']') 5046 continue; 5047 5048 if (cc == '.' || cc == '[') { 5049 nelems++; 5050 cc = '\0'; 5051 } 5052 5053 *ee++ = cc; 5054 } 5055 *ee++ = '\0'; 5056 5057 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist, 5058 nelems, dest)) != 0) 5059 mstate->dtms_scratch_ptr += jsonlen + 1; 5060 break; 5061 } 5062 5063 case DIF_SUBR_TOUPPER: 5064 case DIF_SUBR_TOLOWER: { 5065 uintptr_t s = tupregs[0].dttk_value; 5066 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5067 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5068 size_t len = dtrace_strlen((char *)s, size); 5069 char lower, upper, convert; 5070 int64_t i; 5071 5072 if (subr == DIF_SUBR_TOUPPER) { 5073 lower = 'a'; 5074 upper = 'z'; 5075 convert = 'A'; 5076 } else { 5077 lower = 'A'; 5078 upper = 'Z'; 5079 convert = 'a'; 5080 } 5081 5082 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 5083 regs[rd] = 0; 5084 break; 5085 } 5086 5087 if (!DTRACE_INSCRATCH(mstate, size)) { 5088 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5089 regs[rd] = 0; 5090 break; 5091 } 5092 5093 for (i = 0; i < size - 1; i++) { 5094 if ((c = dtrace_load8(s + i)) == '\0') 5095 break; 5096 5097 if (c >= lower && c <= upper) 5098 c = convert + (c - lower); 5099 5100 dest[i] = c; 5101 } 5102 5103 ASSERT(i < size); 5104 dest[i] = '\0'; 5105 regs[rd] = (uintptr_t)dest; 5106 mstate->dtms_scratch_ptr += size; 5107 break; 5108 } 5109 5110 #ifdef illumos 5111 case DIF_SUBR_GETMAJOR: 5112 #ifdef _LP64 5113 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 5114 #else 5115 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 5116 #endif 5117 break; 5118 5119 case DIF_SUBR_GETMINOR: 5120 #ifdef _LP64 5121 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 5122 #else 5123 regs[rd] = tupregs[0].dttk_value & MAXMIN; 5124 #endif 5125 break; 5126 5127 case DIF_SUBR_DDI_PATHNAME: { 5128 /* 5129 * This one is a galactic mess. We are going to roughly 5130 * emulate ddi_pathname(), but it's made more complicated 5131 * by the fact that we (a) want to include the minor name and 5132 * (b) must proceed iteratively instead of recursively. 5133 */ 5134 uintptr_t dest = mstate->dtms_scratch_ptr; 5135 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5136 char *start = (char *)dest, *end = start + size - 1; 5137 uintptr_t daddr = tupregs[0].dttk_value; 5138 int64_t minor = (int64_t)tupregs[1].dttk_value; 5139 char *s; 5140 int i, len, depth = 0; 5141 5142 /* 5143 * Due to all the pointer jumping we do and context we must 5144 * rely upon, we just mandate that the user must have kernel 5145 * read privileges to use this routine. 5146 */ 5147 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 5148 *flags |= CPU_DTRACE_KPRIV; 5149 *illval = daddr; 5150 regs[rd] = 0; 5151 } 5152 5153 if (!DTRACE_INSCRATCH(mstate, size)) { 5154 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5155 regs[rd] = 0; 5156 break; 5157 } 5158 5159 *end = '\0'; 5160 5161 /* 5162 * We want to have a name for the minor. In order to do this, 5163 * we need to walk the minor list from the devinfo. We want 5164 * to be sure that we don't infinitely walk a circular list, 5165 * so we check for circularity by sending a scout pointer 5166 * ahead two elements for every element that we iterate over; 5167 * if the list is circular, these will ultimately point to the 5168 * same element. You may recognize this little trick as the 5169 * answer to a stupid interview question -- one that always 5170 * seems to be asked by those who had to have it laboriously 5171 * explained to them, and who can't even concisely describe 5172 * the conditions under which one would be forced to resort to 5173 * this technique. Needless to say, those conditions are 5174 * found here -- and probably only here. Is this the only use 5175 * of this infamous trick in shipping, production code? If it 5176 * isn't, it probably should be... 5177 */ 5178 if (minor != -1) { 5179 uintptr_t maddr = dtrace_loadptr(daddr + 5180 offsetof(struct dev_info, devi_minor)); 5181 5182 uintptr_t next = offsetof(struct ddi_minor_data, next); 5183 uintptr_t name = offsetof(struct ddi_minor_data, 5184 d_minor) + offsetof(struct ddi_minor, name); 5185 uintptr_t dev = offsetof(struct ddi_minor_data, 5186 d_minor) + offsetof(struct ddi_minor, dev); 5187 uintptr_t scout; 5188 5189 if (maddr != NULL) 5190 scout = dtrace_loadptr(maddr + next); 5191 5192 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5193 uint64_t m; 5194 #ifdef _LP64 5195 m = dtrace_load64(maddr + dev) & MAXMIN64; 5196 #else 5197 m = dtrace_load32(maddr + dev) & MAXMIN; 5198 #endif 5199 if (m != minor) { 5200 maddr = dtrace_loadptr(maddr + next); 5201 5202 if (scout == NULL) 5203 continue; 5204 5205 scout = dtrace_loadptr(scout + next); 5206 5207 if (scout == NULL) 5208 continue; 5209 5210 scout = dtrace_loadptr(scout + next); 5211 5212 if (scout == NULL) 5213 continue; 5214 5215 if (scout == maddr) { 5216 *flags |= CPU_DTRACE_ILLOP; 5217 break; 5218 } 5219 5220 continue; 5221 } 5222 5223 /* 5224 * We have the minor data. Now we need to 5225 * copy the minor's name into the end of the 5226 * pathname. 5227 */ 5228 s = (char *)dtrace_loadptr(maddr + name); 5229 len = dtrace_strlen(s, size); 5230 5231 if (*flags & CPU_DTRACE_FAULT) 5232 break; 5233 5234 if (len != 0) { 5235 if ((end -= (len + 1)) < start) 5236 break; 5237 5238 *end = ':'; 5239 } 5240 5241 for (i = 1; i <= len; i++) 5242 end[i] = dtrace_load8((uintptr_t)s++); 5243 break; 5244 } 5245 } 5246 5247 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5248 ddi_node_state_t devi_state; 5249 5250 devi_state = dtrace_load32(daddr + 5251 offsetof(struct dev_info, devi_node_state)); 5252 5253 if (*flags & CPU_DTRACE_FAULT) 5254 break; 5255 5256 if (devi_state >= DS_INITIALIZED) { 5257 s = (char *)dtrace_loadptr(daddr + 5258 offsetof(struct dev_info, devi_addr)); 5259 len = dtrace_strlen(s, size); 5260 5261 if (*flags & CPU_DTRACE_FAULT) 5262 break; 5263 5264 if (len != 0) { 5265 if ((end -= (len + 1)) < start) 5266 break; 5267 5268 *end = '@'; 5269 } 5270 5271 for (i = 1; i <= len; i++) 5272 end[i] = dtrace_load8((uintptr_t)s++); 5273 } 5274 5275 /* 5276 * Now for the node name... 5277 */ 5278 s = (char *)dtrace_loadptr(daddr + 5279 offsetof(struct dev_info, devi_node_name)); 5280 5281 daddr = dtrace_loadptr(daddr + 5282 offsetof(struct dev_info, devi_parent)); 5283 5284 /* 5285 * If our parent is NULL (that is, if we're the root 5286 * node), we're going to use the special path 5287 * "devices". 5288 */ 5289 if (daddr == 0) 5290 s = "devices"; 5291 5292 len = dtrace_strlen(s, size); 5293 if (*flags & CPU_DTRACE_FAULT) 5294 break; 5295 5296 if ((end -= (len + 1)) < start) 5297 break; 5298 5299 for (i = 1; i <= len; i++) 5300 end[i] = dtrace_load8((uintptr_t)s++); 5301 *end = '/'; 5302 5303 if (depth++ > dtrace_devdepth_max) { 5304 *flags |= CPU_DTRACE_ILLOP; 5305 break; 5306 } 5307 } 5308 5309 if (end < start) 5310 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5311 5312 if (daddr == 0) { 5313 regs[rd] = (uintptr_t)end; 5314 mstate->dtms_scratch_ptr += size; 5315 } 5316 5317 break; 5318 } 5319 #endif 5320 5321 case DIF_SUBR_STRJOIN: { 5322 char *d = (char *)mstate->dtms_scratch_ptr; 5323 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5324 uintptr_t s1 = tupregs[0].dttk_value; 5325 uintptr_t s2 = tupregs[1].dttk_value; 5326 int i = 0, j = 0; 5327 size_t lim1, lim2; 5328 char c; 5329 5330 if (!dtrace_strcanload(s1, size, &lim1, mstate, vstate) || 5331 !dtrace_strcanload(s2, size, &lim2, mstate, vstate)) { 5332 regs[rd] = 0; 5333 break; 5334 } 5335 5336 if (!DTRACE_INSCRATCH(mstate, size)) { 5337 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5338 regs[rd] = 0; 5339 break; 5340 } 5341 5342 for (;;) { 5343 if (i >= size) { 5344 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5345 regs[rd] = 0; 5346 break; 5347 } 5348 c = (i >= lim1) ? '\0' : dtrace_load8(s1++); 5349 if ((d[i++] = c) == '\0') { 5350 i--; 5351 break; 5352 } 5353 } 5354 5355 for (;;) { 5356 if (i >= size) { 5357 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5358 regs[rd] = 0; 5359 break; 5360 } 5361 5362 c = (j++ >= lim2) ? '\0' : dtrace_load8(s2++); 5363 if ((d[i++] = c) == '\0') 5364 break; 5365 } 5366 5367 if (i < size) { 5368 mstate->dtms_scratch_ptr += i; 5369 regs[rd] = (uintptr_t)d; 5370 } 5371 5372 break; 5373 } 5374 5375 case DIF_SUBR_STRTOLL: { 5376 uintptr_t s = tupregs[0].dttk_value; 5377 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5378 size_t lim; 5379 int base = 10; 5380 5381 if (nargs > 1) { 5382 if ((base = tupregs[1].dttk_value) <= 1 || 5383 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5384 *flags |= CPU_DTRACE_ILLOP; 5385 break; 5386 } 5387 } 5388 5389 if (!dtrace_strcanload(s, size, &lim, mstate, vstate)) { 5390 regs[rd] = INT64_MIN; 5391 break; 5392 } 5393 5394 regs[rd] = dtrace_strtoll((char *)s, base, lim); 5395 break; 5396 } 5397 5398 case DIF_SUBR_LLTOSTR: { 5399 int64_t i = (int64_t)tupregs[0].dttk_value; 5400 uint64_t val, digit; 5401 uint64_t size = 65; /* enough room for 2^64 in binary */ 5402 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 5403 int base = 10; 5404 5405 if (nargs > 1) { 5406 if ((base = tupregs[1].dttk_value) <= 1 || 5407 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5408 *flags |= CPU_DTRACE_ILLOP; 5409 break; 5410 } 5411 } 5412 5413 val = (base == 10 && i < 0) ? i * -1 : i; 5414 5415 if (!DTRACE_INSCRATCH(mstate, size)) { 5416 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5417 regs[rd] = 0; 5418 break; 5419 } 5420 5421 for (*end-- = '\0'; val; val /= base) { 5422 if ((digit = val % base) <= '9' - '0') { 5423 *end-- = '0' + digit; 5424 } else { 5425 *end-- = 'a' + (digit - ('9' - '0') - 1); 5426 } 5427 } 5428 5429 if (i == 0 && base == 16) 5430 *end-- = '0'; 5431 5432 if (base == 16) 5433 *end-- = 'x'; 5434 5435 if (i == 0 || base == 8 || base == 16) 5436 *end-- = '0'; 5437 5438 if (i < 0 && base == 10) 5439 *end-- = '-'; 5440 5441 regs[rd] = (uintptr_t)end + 1; 5442 mstate->dtms_scratch_ptr += size; 5443 break; 5444 } 5445 5446 case DIF_SUBR_HTONS: 5447 case DIF_SUBR_NTOHS: 5448 #if BYTE_ORDER == BIG_ENDIAN 5449 regs[rd] = (uint16_t)tupregs[0].dttk_value; 5450 #else 5451 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 5452 #endif 5453 break; 5454 5455 5456 case DIF_SUBR_HTONL: 5457 case DIF_SUBR_NTOHL: 5458 #if BYTE_ORDER == BIG_ENDIAN 5459 regs[rd] = (uint32_t)tupregs[0].dttk_value; 5460 #else 5461 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 5462 #endif 5463 break; 5464 5465 5466 case DIF_SUBR_HTONLL: 5467 case DIF_SUBR_NTOHLL: 5468 #if BYTE_ORDER == BIG_ENDIAN 5469 regs[rd] = (uint64_t)tupregs[0].dttk_value; 5470 #else 5471 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 5472 #endif 5473 break; 5474 5475 5476 case DIF_SUBR_DIRNAME: 5477 case DIF_SUBR_BASENAME: { 5478 char *dest = (char *)mstate->dtms_scratch_ptr; 5479 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5480 uintptr_t src = tupregs[0].dttk_value; 5481 int i, j, len = dtrace_strlen((char *)src, size); 5482 int lastbase = -1, firstbase = -1, lastdir = -1; 5483 int start, end; 5484 5485 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 5486 regs[rd] = 0; 5487 break; 5488 } 5489 5490 if (!DTRACE_INSCRATCH(mstate, size)) { 5491 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5492 regs[rd] = 0; 5493 break; 5494 } 5495 5496 /* 5497 * The basename and dirname for a zero-length string is 5498 * defined to be "." 5499 */ 5500 if (len == 0) { 5501 len = 1; 5502 src = (uintptr_t)"."; 5503 } 5504 5505 /* 5506 * Start from the back of the string, moving back toward the 5507 * front until we see a character that isn't a slash. That 5508 * character is the last character in the basename. 5509 */ 5510 for (i = len - 1; i >= 0; i--) { 5511 if (dtrace_load8(src + i) != '/') 5512 break; 5513 } 5514 5515 if (i >= 0) 5516 lastbase = i; 5517 5518 /* 5519 * Starting from the last character in the basename, move 5520 * towards the front until we find a slash. The character 5521 * that we processed immediately before that is the first 5522 * character in the basename. 5523 */ 5524 for (; i >= 0; i--) { 5525 if (dtrace_load8(src + i) == '/') 5526 break; 5527 } 5528 5529 if (i >= 0) 5530 firstbase = i + 1; 5531 5532 /* 5533 * Now keep going until we find a non-slash character. That 5534 * character is the last character in the dirname. 5535 */ 5536 for (; i >= 0; i--) { 5537 if (dtrace_load8(src + i) != '/') 5538 break; 5539 } 5540 5541 if (i >= 0) 5542 lastdir = i; 5543 5544 ASSERT(!(lastbase == -1 && firstbase != -1)); 5545 ASSERT(!(firstbase == -1 && lastdir != -1)); 5546 5547 if (lastbase == -1) { 5548 /* 5549 * We didn't find a non-slash character. We know that 5550 * the length is non-zero, so the whole string must be 5551 * slashes. In either the dirname or the basename 5552 * case, we return '/'. 5553 */ 5554 ASSERT(firstbase == -1); 5555 firstbase = lastbase = lastdir = 0; 5556 } 5557 5558 if (firstbase == -1) { 5559 /* 5560 * The entire string consists only of a basename 5561 * component. If we're looking for dirname, we need 5562 * to change our string to be just "."; if we're 5563 * looking for a basename, we'll just set the first 5564 * character of the basename to be 0. 5565 */ 5566 if (subr == DIF_SUBR_DIRNAME) { 5567 ASSERT(lastdir == -1); 5568 src = (uintptr_t)"."; 5569 lastdir = 0; 5570 } else { 5571 firstbase = 0; 5572 } 5573 } 5574 5575 if (subr == DIF_SUBR_DIRNAME) { 5576 if (lastdir == -1) { 5577 /* 5578 * We know that we have a slash in the name -- 5579 * or lastdir would be set to 0, above. And 5580 * because lastdir is -1, we know that this 5581 * slash must be the first character. (That 5582 * is, the full string must be of the form 5583 * "/basename".) In this case, the last 5584 * character of the directory name is 0. 5585 */ 5586 lastdir = 0; 5587 } 5588 5589 start = 0; 5590 end = lastdir; 5591 } else { 5592 ASSERT(subr == DIF_SUBR_BASENAME); 5593 ASSERT(firstbase != -1 && lastbase != -1); 5594 start = firstbase; 5595 end = lastbase; 5596 } 5597 5598 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 5599 dest[j] = dtrace_load8(src + i); 5600 5601 dest[j] = '\0'; 5602 regs[rd] = (uintptr_t)dest; 5603 mstate->dtms_scratch_ptr += size; 5604 break; 5605 } 5606 5607 case DIF_SUBR_GETF: { 5608 uintptr_t fd = tupregs[0].dttk_value; 5609 struct filedesc *fdp; 5610 file_t *fp; 5611 5612 if (!dtrace_priv_proc(state)) { 5613 regs[rd] = 0; 5614 break; 5615 } 5616 fdp = curproc->p_fd; 5617 FILEDESC_SLOCK(fdp); 5618 fp = fget_locked(fdp, fd); 5619 mstate->dtms_getf = fp; 5620 regs[rd] = (uintptr_t)fp; 5621 FILEDESC_SUNLOCK(fdp); 5622 break; 5623 } 5624 5625 case DIF_SUBR_CLEANPATH: { 5626 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5627 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5628 uintptr_t src = tupregs[0].dttk_value; 5629 size_t lim; 5630 int i = 0, j = 0; 5631 #ifdef illumos 5632 zone_t *z; 5633 #endif 5634 5635 if (!dtrace_strcanload(src, size, &lim, mstate, vstate)) { 5636 regs[rd] = 0; 5637 break; 5638 } 5639 5640 if (!DTRACE_INSCRATCH(mstate, size)) { 5641 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5642 regs[rd] = 0; 5643 break; 5644 } 5645 5646 /* 5647 * Move forward, loading each character. 5648 */ 5649 do { 5650 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5651 next: 5652 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 5653 break; 5654 5655 if (c != '/') { 5656 dest[j++] = c; 5657 continue; 5658 } 5659 5660 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5661 5662 if (c == '/') { 5663 /* 5664 * We have two slashes -- we can just advance 5665 * to the next character. 5666 */ 5667 goto next; 5668 } 5669 5670 if (c != '.') { 5671 /* 5672 * This is not "." and it's not ".." -- we can 5673 * just store the "/" and this character and 5674 * drive on. 5675 */ 5676 dest[j++] = '/'; 5677 dest[j++] = c; 5678 continue; 5679 } 5680 5681 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5682 5683 if (c == '/') { 5684 /* 5685 * This is a "/./" component. We're not going 5686 * to store anything in the destination buffer; 5687 * we're just going to go to the next component. 5688 */ 5689 goto next; 5690 } 5691 5692 if (c != '.') { 5693 /* 5694 * This is not ".." -- we can just store the 5695 * "/." and this character and continue 5696 * processing. 5697 */ 5698 dest[j++] = '/'; 5699 dest[j++] = '.'; 5700 dest[j++] = c; 5701 continue; 5702 } 5703 5704 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5705 5706 if (c != '/' && c != '\0') { 5707 /* 5708 * This is not ".." -- it's "..[mumble]". 5709 * We'll store the "/.." and this character 5710 * and continue processing. 5711 */ 5712 dest[j++] = '/'; 5713 dest[j++] = '.'; 5714 dest[j++] = '.'; 5715 dest[j++] = c; 5716 continue; 5717 } 5718 5719 /* 5720 * This is "/../" or "/..\0". We need to back up 5721 * our destination pointer until we find a "/". 5722 */ 5723 i--; 5724 while (j != 0 && dest[--j] != '/') 5725 continue; 5726 5727 if (c == '\0') 5728 dest[++j] = '/'; 5729 } while (c != '\0'); 5730 5731 dest[j] = '\0'; 5732 5733 #ifdef illumos 5734 if (mstate->dtms_getf != NULL && 5735 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 5736 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 5737 /* 5738 * If we've done a getf() as a part of this ECB and we 5739 * don't have kernel access (and we're not in the global 5740 * zone), check if the path we cleaned up begins with 5741 * the zone's root path, and trim it off if so. Note 5742 * that this is an output cleanliness issue, not a 5743 * security issue: knowing one's zone root path does 5744 * not enable privilege escalation. 5745 */ 5746 if (strstr(dest, z->zone_rootpath) == dest) 5747 dest += strlen(z->zone_rootpath) - 1; 5748 } 5749 #endif 5750 5751 regs[rd] = (uintptr_t)dest; 5752 mstate->dtms_scratch_ptr += size; 5753 break; 5754 } 5755 5756 case DIF_SUBR_INET_NTOA: 5757 case DIF_SUBR_INET_NTOA6: 5758 case DIF_SUBR_INET_NTOP: { 5759 size_t size; 5760 int af, argi, i; 5761 char *base, *end; 5762 5763 if (subr == DIF_SUBR_INET_NTOP) { 5764 af = (int)tupregs[0].dttk_value; 5765 argi = 1; 5766 } else { 5767 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 5768 argi = 0; 5769 } 5770 5771 if (af == AF_INET) { 5772 ipaddr_t ip4; 5773 uint8_t *ptr8, val; 5774 5775 if (!dtrace_canload(tupregs[argi].dttk_value, 5776 sizeof (ipaddr_t), mstate, vstate)) { 5777 regs[rd] = 0; 5778 break; 5779 } 5780 5781 /* 5782 * Safely load the IPv4 address. 5783 */ 5784 ip4 = dtrace_load32(tupregs[argi].dttk_value); 5785 5786 /* 5787 * Check an IPv4 string will fit in scratch. 5788 */ 5789 size = INET_ADDRSTRLEN; 5790 if (!DTRACE_INSCRATCH(mstate, size)) { 5791 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5792 regs[rd] = 0; 5793 break; 5794 } 5795 base = (char *)mstate->dtms_scratch_ptr; 5796 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5797 5798 /* 5799 * Stringify as a dotted decimal quad. 5800 */ 5801 *end-- = '\0'; 5802 ptr8 = (uint8_t *)&ip4; 5803 for (i = 3; i >= 0; i--) { 5804 val = ptr8[i]; 5805 5806 if (val == 0) { 5807 *end-- = '0'; 5808 } else { 5809 for (; val; val /= 10) { 5810 *end-- = '0' + (val % 10); 5811 } 5812 } 5813 5814 if (i > 0) 5815 *end-- = '.'; 5816 } 5817 ASSERT(end + 1 >= base); 5818 5819 } else if (af == AF_INET6) { 5820 struct in6_addr ip6; 5821 int firstzero, tryzero, numzero, v6end; 5822 uint16_t val; 5823 const char digits[] = "0123456789abcdef"; 5824 5825 /* 5826 * Stringify using RFC 1884 convention 2 - 16 bit 5827 * hexadecimal values with a zero-run compression. 5828 * Lower case hexadecimal digits are used. 5829 * eg, fe80::214:4fff:fe0b:76c8. 5830 * The IPv4 embedded form is returned for inet_ntop, 5831 * just the IPv4 string is returned for inet_ntoa6. 5832 */ 5833 5834 if (!dtrace_canload(tupregs[argi].dttk_value, 5835 sizeof (struct in6_addr), mstate, vstate)) { 5836 regs[rd] = 0; 5837 break; 5838 } 5839 5840 /* 5841 * Safely load the IPv6 address. 5842 */ 5843 dtrace_bcopy( 5844 (void *)(uintptr_t)tupregs[argi].dttk_value, 5845 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 5846 5847 /* 5848 * Check an IPv6 string will fit in scratch. 5849 */ 5850 size = INET6_ADDRSTRLEN; 5851 if (!DTRACE_INSCRATCH(mstate, size)) { 5852 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5853 regs[rd] = 0; 5854 break; 5855 } 5856 base = (char *)mstate->dtms_scratch_ptr; 5857 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5858 *end-- = '\0'; 5859 5860 /* 5861 * Find the longest run of 16 bit zero values 5862 * for the single allowed zero compression - "::". 5863 */ 5864 firstzero = -1; 5865 tryzero = -1; 5866 numzero = 1; 5867 for (i = 0; i < sizeof (struct in6_addr); i++) { 5868 #ifdef illumos 5869 if (ip6._S6_un._S6_u8[i] == 0 && 5870 #else 5871 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5872 #endif 5873 tryzero == -1 && i % 2 == 0) { 5874 tryzero = i; 5875 continue; 5876 } 5877 5878 if (tryzero != -1 && 5879 #ifdef illumos 5880 (ip6._S6_un._S6_u8[i] != 0 || 5881 #else 5882 (ip6.__u6_addr.__u6_addr8[i] != 0 || 5883 #endif 5884 i == sizeof (struct in6_addr) - 1)) { 5885 5886 if (i - tryzero <= numzero) { 5887 tryzero = -1; 5888 continue; 5889 } 5890 5891 firstzero = tryzero; 5892 numzero = i - i % 2 - tryzero; 5893 tryzero = -1; 5894 5895 #ifdef illumos 5896 if (ip6._S6_un._S6_u8[i] == 0 && 5897 #else 5898 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5899 #endif 5900 i == sizeof (struct in6_addr) - 1) 5901 numzero += 2; 5902 } 5903 } 5904 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 5905 5906 /* 5907 * Check for an IPv4 embedded address. 5908 */ 5909 v6end = sizeof (struct in6_addr) - 2; 5910 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 5911 IN6_IS_ADDR_V4COMPAT(&ip6)) { 5912 for (i = sizeof (struct in6_addr) - 1; 5913 i >= DTRACE_V4MAPPED_OFFSET; i--) { 5914 ASSERT(end >= base); 5915 5916 #ifdef illumos 5917 val = ip6._S6_un._S6_u8[i]; 5918 #else 5919 val = ip6.__u6_addr.__u6_addr8[i]; 5920 #endif 5921 5922 if (val == 0) { 5923 *end-- = '0'; 5924 } else { 5925 for (; val; val /= 10) { 5926 *end-- = '0' + val % 10; 5927 } 5928 } 5929 5930 if (i > DTRACE_V4MAPPED_OFFSET) 5931 *end-- = '.'; 5932 } 5933 5934 if (subr == DIF_SUBR_INET_NTOA6) 5935 goto inetout; 5936 5937 /* 5938 * Set v6end to skip the IPv4 address that 5939 * we have already stringified. 5940 */ 5941 v6end = 10; 5942 } 5943 5944 /* 5945 * Build the IPv6 string by working through the 5946 * address in reverse. 5947 */ 5948 for (i = v6end; i >= 0; i -= 2) { 5949 ASSERT(end >= base); 5950 5951 if (i == firstzero + numzero - 2) { 5952 *end-- = ':'; 5953 *end-- = ':'; 5954 i -= numzero - 2; 5955 continue; 5956 } 5957 5958 if (i < 14 && i != firstzero - 2) 5959 *end-- = ':'; 5960 5961 #ifdef illumos 5962 val = (ip6._S6_un._S6_u8[i] << 8) + 5963 ip6._S6_un._S6_u8[i + 1]; 5964 #else 5965 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 5966 ip6.__u6_addr.__u6_addr8[i + 1]; 5967 #endif 5968 5969 if (val == 0) { 5970 *end-- = '0'; 5971 } else { 5972 for (; val; val /= 16) { 5973 *end-- = digits[val % 16]; 5974 } 5975 } 5976 } 5977 ASSERT(end + 1 >= base); 5978 5979 } else { 5980 /* 5981 * The user didn't use AH_INET or AH_INET6. 5982 */ 5983 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5984 regs[rd] = 0; 5985 break; 5986 } 5987 5988 inetout: regs[rd] = (uintptr_t)end + 1; 5989 mstate->dtms_scratch_ptr += size; 5990 break; 5991 } 5992 5993 case DIF_SUBR_MEMREF: { 5994 uintptr_t size = 2 * sizeof(uintptr_t); 5995 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5996 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 5997 5998 /* address and length */ 5999 memref[0] = tupregs[0].dttk_value; 6000 memref[1] = tupregs[1].dttk_value; 6001 6002 regs[rd] = (uintptr_t) memref; 6003 mstate->dtms_scratch_ptr += scratch_size; 6004 break; 6005 } 6006 6007 #ifndef illumos 6008 case DIF_SUBR_MEMSTR: { 6009 char *str = (char *)mstate->dtms_scratch_ptr; 6010 uintptr_t mem = tupregs[0].dttk_value; 6011 char c = tupregs[1].dttk_value; 6012 size_t size = tupregs[2].dttk_value; 6013 uint8_t n; 6014 int i; 6015 6016 regs[rd] = 0; 6017 6018 if (size == 0) 6019 break; 6020 6021 if (!dtrace_canload(mem, size - 1, mstate, vstate)) 6022 break; 6023 6024 if (!DTRACE_INSCRATCH(mstate, size)) { 6025 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6026 break; 6027 } 6028 6029 if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) { 6030 *flags |= CPU_DTRACE_ILLOP; 6031 break; 6032 } 6033 6034 for (i = 0; i < size - 1; i++) { 6035 n = dtrace_load8(mem++); 6036 str[i] = (n == 0) ? c : n; 6037 } 6038 str[size - 1] = 0; 6039 6040 regs[rd] = (uintptr_t)str; 6041 mstate->dtms_scratch_ptr += size; 6042 break; 6043 } 6044 #endif 6045 6046 case DIF_SUBR_TYPEREF: { 6047 uintptr_t size = 4 * sizeof(uintptr_t); 6048 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 6049 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 6050 6051 /* address, num_elements, type_str, type_len */ 6052 typeref[0] = tupregs[0].dttk_value; 6053 typeref[1] = tupregs[1].dttk_value; 6054 typeref[2] = tupregs[2].dttk_value; 6055 typeref[3] = tupregs[3].dttk_value; 6056 6057 regs[rd] = (uintptr_t) typeref; 6058 mstate->dtms_scratch_ptr += scratch_size; 6059 break; 6060 } 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_PRINTT: { 7711 /* The DIF returns a 'typeref'. */ 7712 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 7713 char c = '\0' + 1; 7714 size_t s; 7715 7716 /* 7717 * Get the type string length and round it 7718 * up so that the data that follows is 7719 * aligned for easy access. 7720 */ 7721 size_t typs = strlen((char *) typeref[2]) + 1; 7722 typs = roundup(typs, sizeof(uintptr_t)); 7723 7724 /* 7725 *Get the size from the typeref using the 7726 * number of elements and the type size. 7727 */ 7728 size = typeref[1] * typeref[3]; 7729 7730 /* 7731 * Check if the size exceeds the allocated 7732 * buffer size. 7733 */ 7734 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7735 /* Flag a drop! */ 7736 *flags |= CPU_DTRACE_DROP; 7737 7738 } 7739 7740 /* Store the size in the buffer first. */ 7741 DTRACE_STORE(uintptr_t, tomax, 7742 valoffs, size); 7743 valoffs += sizeof(uintptr_t); 7744 7745 /* Store the type size in the buffer. */ 7746 DTRACE_STORE(uintptr_t, tomax, 7747 valoffs, typeref[3]); 7748 valoffs += sizeof(uintptr_t); 7749 7750 val = typeref[2]; 7751 7752 for (s = 0; s < typs; s++) { 7753 if (c != '\0') 7754 c = dtrace_load8(val++); 7755 7756 DTRACE_STORE(uint8_t, tomax, 7757 valoffs++, c); 7758 } 7759 7760 /* 7761 * Reset to the memory address rather than 7762 * the typeref array, then let the BYREF 7763 * code below do the work to store the 7764 * memory data in the buffer. 7765 */ 7766 val = typeref[0]; 7767 break; 7768 } 7769 7770 case DTRACEACT_CHILL: 7771 if (dtrace_priv_kernel_destructive(state)) 7772 dtrace_action_chill(&mstate, val); 7773 continue; 7774 7775 case DTRACEACT_RAISE: 7776 if (dtrace_priv_proc_destructive(state)) 7777 dtrace_action_raise(val); 7778 continue; 7779 7780 case DTRACEACT_COMMIT: 7781 ASSERT(!committed); 7782 7783 /* 7784 * We need to commit our buffer state. 7785 */ 7786 if (ecb->dte_size) 7787 buf->dtb_offset = offs + ecb->dte_size; 7788 buf = &state->dts_buffer[cpuid]; 7789 dtrace_speculation_commit(state, cpuid, val); 7790 committed = 1; 7791 continue; 7792 7793 case DTRACEACT_DISCARD: 7794 dtrace_speculation_discard(state, cpuid, val); 7795 continue; 7796 7797 case DTRACEACT_DIFEXPR: 7798 case DTRACEACT_LIBACT: 7799 case DTRACEACT_PRINTF: 7800 case DTRACEACT_PRINTA: 7801 case DTRACEACT_SYSTEM: 7802 case DTRACEACT_FREOPEN: 7803 case DTRACEACT_TRACEMEM: 7804 break; 7805 7806 case DTRACEACT_TRACEMEM_DYNSIZE: 7807 tracememsize = val; 7808 break; 7809 7810 case DTRACEACT_SYM: 7811 case DTRACEACT_MOD: 7812 if (!dtrace_priv_kernel(state)) 7813 continue; 7814 break; 7815 7816 case DTRACEACT_USYM: 7817 case DTRACEACT_UMOD: 7818 case DTRACEACT_UADDR: { 7819 #ifdef illumos 7820 struct pid *pid = curthread->t_procp->p_pidp; 7821 #endif 7822 7823 if (!dtrace_priv_proc(state)) 7824 continue; 7825 7826 DTRACE_STORE(uint64_t, tomax, 7827 #ifdef illumos 7828 valoffs, (uint64_t)pid->pid_id); 7829 #else 7830 valoffs, (uint64_t) curproc->p_pid); 7831 #endif 7832 DTRACE_STORE(uint64_t, tomax, 7833 valoffs + sizeof (uint64_t), val); 7834 7835 continue; 7836 } 7837 7838 case DTRACEACT_EXIT: { 7839 /* 7840 * For the exit action, we are going to attempt 7841 * to atomically set our activity to be 7842 * draining. If this fails (either because 7843 * another CPU has beat us to the exit action, 7844 * or because our current activity is something 7845 * other than ACTIVE or WARMUP), we will 7846 * continue. This assures that the exit action 7847 * can be successfully recorded at most once 7848 * when we're in the ACTIVE state. If we're 7849 * encountering the exit() action while in 7850 * COOLDOWN, however, we want to honor the new 7851 * status code. (We know that we're the only 7852 * thread in COOLDOWN, so there is no race.) 7853 */ 7854 void *activity = &state->dts_activity; 7855 dtrace_activity_t current = state->dts_activity; 7856 7857 if (current == DTRACE_ACTIVITY_COOLDOWN) 7858 break; 7859 7860 if (current != DTRACE_ACTIVITY_WARMUP) 7861 current = DTRACE_ACTIVITY_ACTIVE; 7862 7863 if (dtrace_cas32(activity, current, 7864 DTRACE_ACTIVITY_DRAINING) != current) { 7865 *flags |= CPU_DTRACE_DROP; 7866 continue; 7867 } 7868 7869 break; 7870 } 7871 7872 default: 7873 ASSERT(0); 7874 } 7875 7876 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF || 7877 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) { 7878 uintptr_t end = valoffs + size; 7879 7880 if (tracememsize != 0 && 7881 valoffs + tracememsize < end) { 7882 end = valoffs + tracememsize; 7883 tracememsize = 0; 7884 } 7885 7886 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF && 7887 !dtrace_vcanload((void *)(uintptr_t)val, 7888 &dp->dtdo_rtype, NULL, &mstate, vstate)) 7889 continue; 7890 7891 dtrace_store_by_ref(dp, tomax, size, &valoffs, 7892 &val, end, act->dta_intuple, 7893 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ? 7894 DIF_TF_BYREF: DIF_TF_BYUREF); 7895 continue; 7896 } 7897 7898 switch (size) { 7899 case 0: 7900 break; 7901 7902 case sizeof (uint8_t): 7903 DTRACE_STORE(uint8_t, tomax, valoffs, val); 7904 break; 7905 case sizeof (uint16_t): 7906 DTRACE_STORE(uint16_t, tomax, valoffs, val); 7907 break; 7908 case sizeof (uint32_t): 7909 DTRACE_STORE(uint32_t, tomax, valoffs, val); 7910 break; 7911 case sizeof (uint64_t): 7912 DTRACE_STORE(uint64_t, tomax, valoffs, val); 7913 break; 7914 default: 7915 /* 7916 * Any other size should have been returned by 7917 * reference, not by value. 7918 */ 7919 ASSERT(0); 7920 break; 7921 } 7922 } 7923 7924 if (*flags & CPU_DTRACE_DROP) 7925 continue; 7926 7927 if (*flags & CPU_DTRACE_FAULT) { 7928 int ndx; 7929 dtrace_action_t *err; 7930 7931 buf->dtb_errors++; 7932 7933 if (probe->dtpr_id == dtrace_probeid_error) { 7934 /* 7935 * There's nothing we can do -- we had an 7936 * error on the error probe. We bump an 7937 * error counter to at least indicate that 7938 * this condition happened. 7939 */ 7940 dtrace_error(&state->dts_dblerrors); 7941 continue; 7942 } 7943 7944 if (vtime) { 7945 /* 7946 * Before recursing on dtrace_probe(), we 7947 * need to explicitly clear out our start 7948 * time to prevent it from being accumulated 7949 * into t_dtrace_vtime. 7950 */ 7951 curthread->t_dtrace_start = 0; 7952 } 7953 7954 /* 7955 * Iterate over the actions to figure out which action 7956 * we were processing when we experienced the error. 7957 * Note that act points _past_ the faulting action; if 7958 * act is ecb->dte_action, the fault was in the 7959 * predicate, if it's ecb->dte_action->dta_next it's 7960 * in action #1, and so on. 7961 */ 7962 for (err = ecb->dte_action, ndx = 0; 7963 err != act; err = err->dta_next, ndx++) 7964 continue; 7965 7966 dtrace_probe_error(state, ecb->dte_epid, ndx, 7967 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 7968 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 7969 cpu_core[cpuid].cpuc_dtrace_illval); 7970 7971 continue; 7972 } 7973 7974 if (!committed) 7975 buf->dtb_offset = offs + ecb->dte_size; 7976 } 7977 7978 if (vtime) 7979 curthread->t_dtrace_start = dtrace_gethrtime(); 7980 7981 dtrace_interrupt_enable(cookie); 7982 } 7983 7984 /* 7985 * DTrace Probe Hashing Functions 7986 * 7987 * The functions in this section (and indeed, the functions in remaining 7988 * sections) are not _called_ from probe context. (Any exceptions to this are 7989 * marked with a "Note:".) Rather, they are called from elsewhere in the 7990 * DTrace framework to look-up probes in, add probes to and remove probes from 7991 * the DTrace probe hashes. (Each probe is hashed by each element of the 7992 * probe tuple -- allowing for fast lookups, regardless of what was 7993 * specified.) 7994 */ 7995 static uint_t 7996 dtrace_hash_str(const char *p) 7997 { 7998 unsigned int g; 7999 uint_t hval = 0; 8000 8001 while (*p) { 8002 hval = (hval << 4) + *p++; 8003 if ((g = (hval & 0xf0000000)) != 0) 8004 hval ^= g >> 24; 8005 hval &= ~g; 8006 } 8007 return (hval); 8008 } 8009 8010 static dtrace_hash_t * 8011 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 8012 { 8013 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 8014 8015 hash->dth_stroffs = stroffs; 8016 hash->dth_nextoffs = nextoffs; 8017 hash->dth_prevoffs = prevoffs; 8018 8019 hash->dth_size = 1; 8020 hash->dth_mask = hash->dth_size - 1; 8021 8022 hash->dth_tab = kmem_zalloc(hash->dth_size * 8023 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 8024 8025 return (hash); 8026 } 8027 8028 static void 8029 dtrace_hash_destroy(dtrace_hash_t *hash) 8030 { 8031 #ifdef DEBUG 8032 int i; 8033 8034 for (i = 0; i < hash->dth_size; i++) 8035 ASSERT(hash->dth_tab[i] == NULL); 8036 #endif 8037 8038 kmem_free(hash->dth_tab, 8039 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 8040 kmem_free(hash, sizeof (dtrace_hash_t)); 8041 } 8042 8043 static void 8044 dtrace_hash_resize(dtrace_hash_t *hash) 8045 { 8046 int size = hash->dth_size, i, ndx; 8047 int new_size = hash->dth_size << 1; 8048 int new_mask = new_size - 1; 8049 dtrace_hashbucket_t **new_tab, *bucket, *next; 8050 8051 ASSERT((new_size & new_mask) == 0); 8052 8053 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 8054 8055 for (i = 0; i < size; i++) { 8056 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 8057 dtrace_probe_t *probe = bucket->dthb_chain; 8058 8059 ASSERT(probe != NULL); 8060 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 8061 8062 next = bucket->dthb_next; 8063 bucket->dthb_next = new_tab[ndx]; 8064 new_tab[ndx] = bucket; 8065 } 8066 } 8067 8068 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 8069 hash->dth_tab = new_tab; 8070 hash->dth_size = new_size; 8071 hash->dth_mask = new_mask; 8072 } 8073 8074 static void 8075 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 8076 { 8077 int hashval = DTRACE_HASHSTR(hash, new); 8078 int ndx = hashval & hash->dth_mask; 8079 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8080 dtrace_probe_t **nextp, **prevp; 8081 8082 for (; bucket != NULL; bucket = bucket->dthb_next) { 8083 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 8084 goto add; 8085 } 8086 8087 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 8088 dtrace_hash_resize(hash); 8089 dtrace_hash_add(hash, new); 8090 return; 8091 } 8092 8093 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 8094 bucket->dthb_next = hash->dth_tab[ndx]; 8095 hash->dth_tab[ndx] = bucket; 8096 hash->dth_nbuckets++; 8097 8098 add: 8099 nextp = DTRACE_HASHNEXT(hash, new); 8100 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 8101 *nextp = bucket->dthb_chain; 8102 8103 if (bucket->dthb_chain != NULL) { 8104 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 8105 ASSERT(*prevp == NULL); 8106 *prevp = new; 8107 } 8108 8109 bucket->dthb_chain = new; 8110 bucket->dthb_len++; 8111 } 8112 8113 static dtrace_probe_t * 8114 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 8115 { 8116 int hashval = DTRACE_HASHSTR(hash, template); 8117 int ndx = hashval & hash->dth_mask; 8118 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8119 8120 for (; bucket != NULL; bucket = bucket->dthb_next) { 8121 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 8122 return (bucket->dthb_chain); 8123 } 8124 8125 return (NULL); 8126 } 8127 8128 static int 8129 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 8130 { 8131 int hashval = DTRACE_HASHSTR(hash, template); 8132 int ndx = hashval & hash->dth_mask; 8133 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8134 8135 for (; bucket != NULL; bucket = bucket->dthb_next) { 8136 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 8137 return (bucket->dthb_len); 8138 } 8139 8140 return (0); 8141 } 8142 8143 static void 8144 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 8145 { 8146 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 8147 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8148 8149 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 8150 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 8151 8152 /* 8153 * Find the bucket that we're removing this probe from. 8154 */ 8155 for (; bucket != NULL; bucket = bucket->dthb_next) { 8156 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 8157 break; 8158 } 8159 8160 ASSERT(bucket != NULL); 8161 8162 if (*prevp == NULL) { 8163 if (*nextp == NULL) { 8164 /* 8165 * The removed probe was the only probe on this 8166 * bucket; we need to remove the bucket. 8167 */ 8168 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 8169 8170 ASSERT(bucket->dthb_chain == probe); 8171 ASSERT(b != NULL); 8172 8173 if (b == bucket) { 8174 hash->dth_tab[ndx] = bucket->dthb_next; 8175 } else { 8176 while (b->dthb_next != bucket) 8177 b = b->dthb_next; 8178 b->dthb_next = bucket->dthb_next; 8179 } 8180 8181 ASSERT(hash->dth_nbuckets > 0); 8182 hash->dth_nbuckets--; 8183 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 8184 return; 8185 } 8186 8187 bucket->dthb_chain = *nextp; 8188 } else { 8189 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 8190 } 8191 8192 if (*nextp != NULL) 8193 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 8194 } 8195 8196 /* 8197 * DTrace Utility Functions 8198 * 8199 * These are random utility functions that are _not_ called from probe context. 8200 */ 8201 static int 8202 dtrace_badattr(const dtrace_attribute_t *a) 8203 { 8204 return (a->dtat_name > DTRACE_STABILITY_MAX || 8205 a->dtat_data > DTRACE_STABILITY_MAX || 8206 a->dtat_class > DTRACE_CLASS_MAX); 8207 } 8208 8209 /* 8210 * Return a duplicate copy of a string. If the specified string is NULL, 8211 * this function returns a zero-length string. 8212 */ 8213 static char * 8214 dtrace_strdup(const char *str) 8215 { 8216 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 8217 8218 if (str != NULL) 8219 (void) strcpy(new, str); 8220 8221 return (new); 8222 } 8223 8224 #define DTRACE_ISALPHA(c) \ 8225 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 8226 8227 static int 8228 dtrace_badname(const char *s) 8229 { 8230 char c; 8231 8232 if (s == NULL || (c = *s++) == '\0') 8233 return (0); 8234 8235 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 8236 return (1); 8237 8238 while ((c = *s++) != '\0') { 8239 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 8240 c != '-' && c != '_' && c != '.' && c != '`') 8241 return (1); 8242 } 8243 8244 return (0); 8245 } 8246 8247 static void 8248 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 8249 { 8250 uint32_t priv; 8251 8252 #ifdef illumos 8253 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 8254 /* 8255 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 8256 */ 8257 priv = DTRACE_PRIV_ALL; 8258 } else { 8259 *uidp = crgetuid(cr); 8260 *zoneidp = crgetzoneid(cr); 8261 8262 priv = 0; 8263 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 8264 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 8265 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 8266 priv |= DTRACE_PRIV_USER; 8267 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 8268 priv |= DTRACE_PRIV_PROC; 8269 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 8270 priv |= DTRACE_PRIV_OWNER; 8271 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 8272 priv |= DTRACE_PRIV_ZONEOWNER; 8273 } 8274 #else 8275 priv = DTRACE_PRIV_ALL; 8276 #endif 8277 8278 *privp = priv; 8279 } 8280 8281 #ifdef DTRACE_ERRDEBUG 8282 static void 8283 dtrace_errdebug(const char *str) 8284 { 8285 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 8286 int occupied = 0; 8287 8288 mutex_enter(&dtrace_errlock); 8289 dtrace_errlast = str; 8290 dtrace_errthread = curthread; 8291 8292 while (occupied++ < DTRACE_ERRHASHSZ) { 8293 if (dtrace_errhash[hval].dter_msg == str) { 8294 dtrace_errhash[hval].dter_count++; 8295 goto out; 8296 } 8297 8298 if (dtrace_errhash[hval].dter_msg != NULL) { 8299 hval = (hval + 1) % DTRACE_ERRHASHSZ; 8300 continue; 8301 } 8302 8303 dtrace_errhash[hval].dter_msg = str; 8304 dtrace_errhash[hval].dter_count = 1; 8305 goto out; 8306 } 8307 8308 panic("dtrace: undersized error hash"); 8309 out: 8310 mutex_exit(&dtrace_errlock); 8311 } 8312 #endif 8313 8314 /* 8315 * DTrace Matching Functions 8316 * 8317 * These functions are used to match groups of probes, given some elements of 8318 * a probe tuple, or some globbed expressions for elements of a probe tuple. 8319 */ 8320 static int 8321 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 8322 zoneid_t zoneid) 8323 { 8324 if (priv != DTRACE_PRIV_ALL) { 8325 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 8326 uint32_t match = priv & ppriv; 8327 8328 /* 8329 * No PRIV_DTRACE_* privileges... 8330 */ 8331 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 8332 DTRACE_PRIV_KERNEL)) == 0) 8333 return (0); 8334 8335 /* 8336 * No matching bits, but there were bits to match... 8337 */ 8338 if (match == 0 && ppriv != 0) 8339 return (0); 8340 8341 /* 8342 * Need to have permissions to the process, but don't... 8343 */ 8344 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 8345 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 8346 return (0); 8347 } 8348 8349 /* 8350 * Need to be in the same zone unless we possess the 8351 * privilege to examine all zones. 8352 */ 8353 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 8354 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 8355 return (0); 8356 } 8357 } 8358 8359 return (1); 8360 } 8361 8362 /* 8363 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 8364 * consists of input pattern strings and an ops-vector to evaluate them. 8365 * This function returns >0 for match, 0 for no match, and <0 for error. 8366 */ 8367 static int 8368 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 8369 uint32_t priv, uid_t uid, zoneid_t zoneid) 8370 { 8371 dtrace_provider_t *pvp = prp->dtpr_provider; 8372 int rv; 8373 8374 if (pvp->dtpv_defunct) 8375 return (0); 8376 8377 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 8378 return (rv); 8379 8380 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 8381 return (rv); 8382 8383 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 8384 return (rv); 8385 8386 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 8387 return (rv); 8388 8389 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 8390 return (0); 8391 8392 return (rv); 8393 } 8394 8395 /* 8396 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 8397 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 8398 * libc's version, the kernel version only applies to 8-bit ASCII strings. 8399 * In addition, all of the recursion cases except for '*' matching have been 8400 * unwound. For '*', we still implement recursive evaluation, but a depth 8401 * counter is maintained and matching is aborted if we recurse too deep. 8402 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 8403 */ 8404 static int 8405 dtrace_match_glob(const char *s, const char *p, int depth) 8406 { 8407 const char *olds; 8408 char s1, c; 8409 int gs; 8410 8411 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 8412 return (-1); 8413 8414 if (s == NULL) 8415 s = ""; /* treat NULL as empty string */ 8416 8417 top: 8418 olds = s; 8419 s1 = *s++; 8420 8421 if (p == NULL) 8422 return (0); 8423 8424 if ((c = *p++) == '\0') 8425 return (s1 == '\0'); 8426 8427 switch (c) { 8428 case '[': { 8429 int ok = 0, notflag = 0; 8430 char lc = '\0'; 8431 8432 if (s1 == '\0') 8433 return (0); 8434 8435 if (*p == '!') { 8436 notflag = 1; 8437 p++; 8438 } 8439 8440 if ((c = *p++) == '\0') 8441 return (0); 8442 8443 do { 8444 if (c == '-' && lc != '\0' && *p != ']') { 8445 if ((c = *p++) == '\0') 8446 return (0); 8447 if (c == '\\' && (c = *p++) == '\0') 8448 return (0); 8449 8450 if (notflag) { 8451 if (s1 < lc || s1 > c) 8452 ok++; 8453 else 8454 return (0); 8455 } else if (lc <= s1 && s1 <= c) 8456 ok++; 8457 8458 } else if (c == '\\' && (c = *p++) == '\0') 8459 return (0); 8460 8461 lc = c; /* save left-hand 'c' for next iteration */ 8462 8463 if (notflag) { 8464 if (s1 != c) 8465 ok++; 8466 else 8467 return (0); 8468 } else if (s1 == c) 8469 ok++; 8470 8471 if ((c = *p++) == '\0') 8472 return (0); 8473 8474 } while (c != ']'); 8475 8476 if (ok) 8477 goto top; 8478 8479 return (0); 8480 } 8481 8482 case '\\': 8483 if ((c = *p++) == '\0') 8484 return (0); 8485 /*FALLTHRU*/ 8486 8487 default: 8488 if (c != s1) 8489 return (0); 8490 /*FALLTHRU*/ 8491 8492 case '?': 8493 if (s1 != '\0') 8494 goto top; 8495 return (0); 8496 8497 case '*': 8498 while (*p == '*') 8499 p++; /* consecutive *'s are identical to a single one */ 8500 8501 if (*p == '\0') 8502 return (1); 8503 8504 for (s = olds; *s != '\0'; s++) { 8505 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 8506 return (gs); 8507 } 8508 8509 return (0); 8510 } 8511 } 8512 8513 /*ARGSUSED*/ 8514 static int 8515 dtrace_match_string(const char *s, const char *p, int depth) 8516 { 8517 return (s != NULL && strcmp(s, p) == 0); 8518 } 8519 8520 /*ARGSUSED*/ 8521 static int 8522 dtrace_match_nul(const char *s, const char *p, int depth) 8523 { 8524 return (1); /* always match the empty pattern */ 8525 } 8526 8527 /*ARGSUSED*/ 8528 static int 8529 dtrace_match_nonzero(const char *s, const char *p, int depth) 8530 { 8531 return (s != NULL && s[0] != '\0'); 8532 } 8533 8534 static int 8535 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 8536 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 8537 { 8538 dtrace_probe_t template, *probe; 8539 dtrace_hash_t *hash = NULL; 8540 int len, best = INT_MAX, nmatched = 0; 8541 dtrace_id_t i; 8542 8543 ASSERT(MUTEX_HELD(&dtrace_lock)); 8544 8545 /* 8546 * If the probe ID is specified in the key, just lookup by ID and 8547 * invoke the match callback once if a matching probe is found. 8548 */ 8549 if (pkp->dtpk_id != DTRACE_IDNONE) { 8550 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 8551 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 8552 (void) (*matched)(probe, arg); 8553 nmatched++; 8554 } 8555 return (nmatched); 8556 } 8557 8558 template.dtpr_mod = (char *)pkp->dtpk_mod; 8559 template.dtpr_func = (char *)pkp->dtpk_func; 8560 template.dtpr_name = (char *)pkp->dtpk_name; 8561 8562 /* 8563 * We want to find the most distinct of the module name, function 8564 * name, and name. So for each one that is not a glob pattern or 8565 * empty string, we perform a lookup in the corresponding hash and 8566 * use the hash table with the fewest collisions to do our search. 8567 */ 8568 if (pkp->dtpk_mmatch == &dtrace_match_string && 8569 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 8570 best = len; 8571 hash = dtrace_bymod; 8572 } 8573 8574 if (pkp->dtpk_fmatch == &dtrace_match_string && 8575 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 8576 best = len; 8577 hash = dtrace_byfunc; 8578 } 8579 8580 if (pkp->dtpk_nmatch == &dtrace_match_string && 8581 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 8582 best = len; 8583 hash = dtrace_byname; 8584 } 8585 8586 /* 8587 * If we did not select a hash table, iterate over every probe and 8588 * invoke our callback for each one that matches our input probe key. 8589 */ 8590 if (hash == NULL) { 8591 for (i = 0; i < dtrace_nprobes; i++) { 8592 if ((probe = dtrace_probes[i]) == NULL || 8593 dtrace_match_probe(probe, pkp, priv, uid, 8594 zoneid) <= 0) 8595 continue; 8596 8597 nmatched++; 8598 8599 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8600 break; 8601 } 8602 8603 return (nmatched); 8604 } 8605 8606 /* 8607 * If we selected a hash table, iterate over each probe of the same key 8608 * name and invoke the callback for every probe that matches the other 8609 * attributes of our input probe key. 8610 */ 8611 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 8612 probe = *(DTRACE_HASHNEXT(hash, probe))) { 8613 8614 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 8615 continue; 8616 8617 nmatched++; 8618 8619 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8620 break; 8621 } 8622 8623 return (nmatched); 8624 } 8625 8626 /* 8627 * Return the function pointer dtrace_probecmp() should use to compare the 8628 * specified pattern with a string. For NULL or empty patterns, we select 8629 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 8630 * For non-empty non-glob strings, we use dtrace_match_string(). 8631 */ 8632 static dtrace_probekey_f * 8633 dtrace_probekey_func(const char *p) 8634 { 8635 char c; 8636 8637 if (p == NULL || *p == '\0') 8638 return (&dtrace_match_nul); 8639 8640 while ((c = *p++) != '\0') { 8641 if (c == '[' || c == '?' || c == '*' || c == '\\') 8642 return (&dtrace_match_glob); 8643 } 8644 8645 return (&dtrace_match_string); 8646 } 8647 8648 /* 8649 * Build a probe comparison key for use with dtrace_match_probe() from the 8650 * given probe description. By convention, a null key only matches anchored 8651 * probes: if each field is the empty string, reset dtpk_fmatch to 8652 * dtrace_match_nonzero(). 8653 */ 8654 static void 8655 dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 8656 { 8657 pkp->dtpk_prov = pdp->dtpd_provider; 8658 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 8659 8660 pkp->dtpk_mod = pdp->dtpd_mod; 8661 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 8662 8663 pkp->dtpk_func = pdp->dtpd_func; 8664 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 8665 8666 pkp->dtpk_name = pdp->dtpd_name; 8667 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 8668 8669 pkp->dtpk_id = pdp->dtpd_id; 8670 8671 if (pkp->dtpk_id == DTRACE_IDNONE && 8672 pkp->dtpk_pmatch == &dtrace_match_nul && 8673 pkp->dtpk_mmatch == &dtrace_match_nul && 8674 pkp->dtpk_fmatch == &dtrace_match_nul && 8675 pkp->dtpk_nmatch == &dtrace_match_nul) 8676 pkp->dtpk_fmatch = &dtrace_match_nonzero; 8677 } 8678 8679 /* 8680 * DTrace Provider-to-Framework API Functions 8681 * 8682 * These functions implement much of the Provider-to-Framework API, as 8683 * described in <sys/dtrace.h>. The parts of the API not in this section are 8684 * the functions in the API for probe management (found below), and 8685 * dtrace_probe() itself (found above). 8686 */ 8687 8688 /* 8689 * Register the calling provider with the DTrace framework. This should 8690 * generally be called by DTrace providers in their attach(9E) entry point. 8691 */ 8692 int 8693 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 8694 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 8695 { 8696 dtrace_provider_t *provider; 8697 8698 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 8699 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8700 "arguments", name ? name : "<NULL>"); 8701 return (EINVAL); 8702 } 8703 8704 if (name[0] == '\0' || dtrace_badname(name)) { 8705 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8706 "provider name", name); 8707 return (EINVAL); 8708 } 8709 8710 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 8711 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 8712 pops->dtps_destroy == NULL || 8713 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 8714 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8715 "provider ops", name); 8716 return (EINVAL); 8717 } 8718 8719 if (dtrace_badattr(&pap->dtpa_provider) || 8720 dtrace_badattr(&pap->dtpa_mod) || 8721 dtrace_badattr(&pap->dtpa_func) || 8722 dtrace_badattr(&pap->dtpa_name) || 8723 dtrace_badattr(&pap->dtpa_args)) { 8724 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8725 "provider attributes", name); 8726 return (EINVAL); 8727 } 8728 8729 if (priv & ~DTRACE_PRIV_ALL) { 8730 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8731 "privilege attributes", name); 8732 return (EINVAL); 8733 } 8734 8735 if ((priv & DTRACE_PRIV_KERNEL) && 8736 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 8737 pops->dtps_usermode == NULL) { 8738 cmn_err(CE_WARN, "failed to register provider '%s': need " 8739 "dtps_usermode() op for given privilege attributes", name); 8740 return (EINVAL); 8741 } 8742 8743 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 8744 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8745 (void) strcpy(provider->dtpv_name, name); 8746 8747 provider->dtpv_attr = *pap; 8748 provider->dtpv_priv.dtpp_flags = priv; 8749 if (cr != NULL) { 8750 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 8751 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 8752 } 8753 provider->dtpv_pops = *pops; 8754 8755 if (pops->dtps_provide == NULL) { 8756 ASSERT(pops->dtps_provide_module != NULL); 8757 provider->dtpv_pops.dtps_provide = 8758 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 8759 } 8760 8761 if (pops->dtps_provide_module == NULL) { 8762 ASSERT(pops->dtps_provide != NULL); 8763 provider->dtpv_pops.dtps_provide_module = 8764 (void (*)(void *, modctl_t *))dtrace_nullop; 8765 } 8766 8767 if (pops->dtps_suspend == NULL) { 8768 ASSERT(pops->dtps_resume == NULL); 8769 provider->dtpv_pops.dtps_suspend = 8770 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8771 provider->dtpv_pops.dtps_resume = 8772 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8773 } 8774 8775 provider->dtpv_arg = arg; 8776 *idp = (dtrace_provider_id_t)provider; 8777 8778 if (pops == &dtrace_provider_ops) { 8779 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8780 ASSERT(MUTEX_HELD(&dtrace_lock)); 8781 ASSERT(dtrace_anon.dta_enabling == NULL); 8782 8783 /* 8784 * We make sure that the DTrace provider is at the head of 8785 * the provider chain. 8786 */ 8787 provider->dtpv_next = dtrace_provider; 8788 dtrace_provider = provider; 8789 return (0); 8790 } 8791 8792 mutex_enter(&dtrace_provider_lock); 8793 mutex_enter(&dtrace_lock); 8794 8795 /* 8796 * If there is at least one provider registered, we'll add this 8797 * provider after the first provider. 8798 */ 8799 if (dtrace_provider != NULL) { 8800 provider->dtpv_next = dtrace_provider->dtpv_next; 8801 dtrace_provider->dtpv_next = provider; 8802 } else { 8803 dtrace_provider = provider; 8804 } 8805 8806 if (dtrace_retained != NULL) { 8807 dtrace_enabling_provide(provider); 8808 8809 /* 8810 * Now we need to call dtrace_enabling_matchall() -- which 8811 * will acquire cpu_lock and dtrace_lock. We therefore need 8812 * to drop all of our locks before calling into it... 8813 */ 8814 mutex_exit(&dtrace_lock); 8815 mutex_exit(&dtrace_provider_lock); 8816 dtrace_enabling_matchall(); 8817 8818 return (0); 8819 } 8820 8821 mutex_exit(&dtrace_lock); 8822 mutex_exit(&dtrace_provider_lock); 8823 8824 return (0); 8825 } 8826 8827 /* 8828 * Unregister the specified provider from the DTrace framework. This should 8829 * generally be called by DTrace providers in their detach(9E) entry point. 8830 */ 8831 int 8832 dtrace_unregister(dtrace_provider_id_t id) 8833 { 8834 dtrace_provider_t *old = (dtrace_provider_t *)id; 8835 dtrace_provider_t *prev = NULL; 8836 int i, self = 0, noreap = 0; 8837 dtrace_probe_t *probe, *first = NULL; 8838 8839 if (old->dtpv_pops.dtps_enable == 8840 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 8841 /* 8842 * If DTrace itself is the provider, we're called with locks 8843 * already held. 8844 */ 8845 ASSERT(old == dtrace_provider); 8846 #ifdef illumos 8847 ASSERT(dtrace_devi != NULL); 8848 #endif 8849 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8850 ASSERT(MUTEX_HELD(&dtrace_lock)); 8851 self = 1; 8852 8853 if (dtrace_provider->dtpv_next != NULL) { 8854 /* 8855 * There's another provider here; return failure. 8856 */ 8857 return (EBUSY); 8858 } 8859 } else { 8860 mutex_enter(&dtrace_provider_lock); 8861 #ifdef illumos 8862 mutex_enter(&mod_lock); 8863 #endif 8864 mutex_enter(&dtrace_lock); 8865 } 8866 8867 /* 8868 * If anyone has /dev/dtrace open, or if there are anonymous enabled 8869 * probes, we refuse to let providers slither away, unless this 8870 * provider has already been explicitly invalidated. 8871 */ 8872 if (!old->dtpv_defunct && 8873 (dtrace_opens || (dtrace_anon.dta_state != NULL && 8874 dtrace_anon.dta_state->dts_necbs > 0))) { 8875 if (!self) { 8876 mutex_exit(&dtrace_lock); 8877 #ifdef illumos 8878 mutex_exit(&mod_lock); 8879 #endif 8880 mutex_exit(&dtrace_provider_lock); 8881 } 8882 return (EBUSY); 8883 } 8884 8885 /* 8886 * Attempt to destroy the probes associated with this provider. 8887 */ 8888 for (i = 0; i < dtrace_nprobes; i++) { 8889 if ((probe = dtrace_probes[i]) == NULL) 8890 continue; 8891 8892 if (probe->dtpr_provider != old) 8893 continue; 8894 8895 if (probe->dtpr_ecb == NULL) 8896 continue; 8897 8898 /* 8899 * If we are trying to unregister a defunct provider, and the 8900 * provider was made defunct within the interval dictated by 8901 * dtrace_unregister_defunct_reap, we'll (asynchronously) 8902 * attempt to reap our enablings. To denote that the provider 8903 * should reattempt to unregister itself at some point in the 8904 * future, we will return a differentiable error code (EAGAIN 8905 * instead of EBUSY) in this case. 8906 */ 8907 if (dtrace_gethrtime() - old->dtpv_defunct > 8908 dtrace_unregister_defunct_reap) 8909 noreap = 1; 8910 8911 if (!self) { 8912 mutex_exit(&dtrace_lock); 8913 #ifdef illumos 8914 mutex_exit(&mod_lock); 8915 #endif 8916 mutex_exit(&dtrace_provider_lock); 8917 } 8918 8919 if (noreap) 8920 return (EBUSY); 8921 8922 (void) taskq_dispatch(dtrace_taskq, 8923 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 8924 8925 return (EAGAIN); 8926 } 8927 8928 /* 8929 * All of the probes for this provider are disabled; we can safely 8930 * remove all of them from their hash chains and from the probe array. 8931 */ 8932 for (i = 0; i < dtrace_nprobes; i++) { 8933 if ((probe = dtrace_probes[i]) == NULL) 8934 continue; 8935 8936 if (probe->dtpr_provider != old) 8937 continue; 8938 8939 dtrace_probes[i] = NULL; 8940 8941 dtrace_hash_remove(dtrace_bymod, probe); 8942 dtrace_hash_remove(dtrace_byfunc, probe); 8943 dtrace_hash_remove(dtrace_byname, probe); 8944 8945 if (first == NULL) { 8946 first = probe; 8947 probe->dtpr_nextmod = NULL; 8948 } else { 8949 probe->dtpr_nextmod = first; 8950 first = probe; 8951 } 8952 } 8953 8954 /* 8955 * The provider's probes have been removed from the hash chains and 8956 * from the probe array. Now issue a dtrace_sync() to be sure that 8957 * everyone has cleared out from any probe array processing. 8958 */ 8959 dtrace_sync(); 8960 8961 for (probe = first; probe != NULL; probe = first) { 8962 first = probe->dtpr_nextmod; 8963 8964 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 8965 probe->dtpr_arg); 8966 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8967 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8968 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8969 #ifdef illumos 8970 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 8971 #else 8972 free_unr(dtrace_arena, probe->dtpr_id); 8973 #endif 8974 kmem_free(probe, sizeof (dtrace_probe_t)); 8975 } 8976 8977 if ((prev = dtrace_provider) == old) { 8978 #ifdef illumos 8979 ASSERT(self || dtrace_devi == NULL); 8980 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 8981 #endif 8982 dtrace_provider = old->dtpv_next; 8983 } else { 8984 while (prev != NULL && prev->dtpv_next != old) 8985 prev = prev->dtpv_next; 8986 8987 if (prev == NULL) { 8988 panic("attempt to unregister non-existent " 8989 "dtrace provider %p\n", (void *)id); 8990 } 8991 8992 prev->dtpv_next = old->dtpv_next; 8993 } 8994 8995 if (!self) { 8996 mutex_exit(&dtrace_lock); 8997 #ifdef illumos 8998 mutex_exit(&mod_lock); 8999 #endif 9000 mutex_exit(&dtrace_provider_lock); 9001 } 9002 9003 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 9004 kmem_free(old, sizeof (dtrace_provider_t)); 9005 9006 return (0); 9007 } 9008 9009 /* 9010 * Invalidate the specified provider. All subsequent probe lookups for the 9011 * specified provider will fail, but its probes will not be removed. 9012 */ 9013 void 9014 dtrace_invalidate(dtrace_provider_id_t id) 9015 { 9016 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 9017 9018 ASSERT(pvp->dtpv_pops.dtps_enable != 9019 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 9020 9021 mutex_enter(&dtrace_provider_lock); 9022 mutex_enter(&dtrace_lock); 9023 9024 pvp->dtpv_defunct = dtrace_gethrtime(); 9025 9026 mutex_exit(&dtrace_lock); 9027 mutex_exit(&dtrace_provider_lock); 9028 } 9029 9030 /* 9031 * Indicate whether or not DTrace has attached. 9032 */ 9033 int 9034 dtrace_attached(void) 9035 { 9036 /* 9037 * dtrace_provider will be non-NULL iff the DTrace driver has 9038 * attached. (It's non-NULL because DTrace is always itself a 9039 * provider.) 9040 */ 9041 return (dtrace_provider != NULL); 9042 } 9043 9044 /* 9045 * Remove all the unenabled probes for the given provider. This function is 9046 * not unlike dtrace_unregister(), except that it doesn't remove the provider 9047 * -- just as many of its associated probes as it can. 9048 */ 9049 int 9050 dtrace_condense(dtrace_provider_id_t id) 9051 { 9052 dtrace_provider_t *prov = (dtrace_provider_t *)id; 9053 int i; 9054 dtrace_probe_t *probe; 9055 9056 /* 9057 * Make sure this isn't the dtrace provider itself. 9058 */ 9059 ASSERT(prov->dtpv_pops.dtps_enable != 9060 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 9061 9062 mutex_enter(&dtrace_provider_lock); 9063 mutex_enter(&dtrace_lock); 9064 9065 /* 9066 * Attempt to destroy the probes associated with this provider. 9067 */ 9068 for (i = 0; i < dtrace_nprobes; i++) { 9069 if ((probe = dtrace_probes[i]) == NULL) 9070 continue; 9071 9072 if (probe->dtpr_provider != prov) 9073 continue; 9074 9075 if (probe->dtpr_ecb != NULL) 9076 continue; 9077 9078 dtrace_probes[i] = NULL; 9079 9080 dtrace_hash_remove(dtrace_bymod, probe); 9081 dtrace_hash_remove(dtrace_byfunc, probe); 9082 dtrace_hash_remove(dtrace_byname, probe); 9083 9084 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 9085 probe->dtpr_arg); 9086 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 9087 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 9088 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 9089 kmem_free(probe, sizeof (dtrace_probe_t)); 9090 #ifdef illumos 9091 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 9092 #else 9093 free_unr(dtrace_arena, i + 1); 9094 #endif 9095 } 9096 9097 mutex_exit(&dtrace_lock); 9098 mutex_exit(&dtrace_provider_lock); 9099 9100 return (0); 9101 } 9102 9103 /* 9104 * DTrace Probe Management Functions 9105 * 9106 * The functions in this section perform the DTrace probe management, 9107 * including functions to create probes, look-up probes, and call into the 9108 * providers to request that probes be provided. Some of these functions are 9109 * in the Provider-to-Framework API; these functions can be identified by the 9110 * fact that they are not declared "static". 9111 */ 9112 9113 /* 9114 * Create a probe with the specified module name, function name, and name. 9115 */ 9116 dtrace_id_t 9117 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 9118 const char *func, const char *name, int aframes, void *arg) 9119 { 9120 dtrace_probe_t *probe, **probes; 9121 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 9122 dtrace_id_t id; 9123 9124 if (provider == dtrace_provider) { 9125 ASSERT(MUTEX_HELD(&dtrace_lock)); 9126 } else { 9127 mutex_enter(&dtrace_lock); 9128 } 9129 9130 #ifdef illumos 9131 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 9132 VM_BESTFIT | VM_SLEEP); 9133 #else 9134 id = alloc_unr(dtrace_arena); 9135 #endif 9136 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 9137 9138 probe->dtpr_id = id; 9139 probe->dtpr_gen = dtrace_probegen++; 9140 probe->dtpr_mod = dtrace_strdup(mod); 9141 probe->dtpr_func = dtrace_strdup(func); 9142 probe->dtpr_name = dtrace_strdup(name); 9143 probe->dtpr_arg = arg; 9144 probe->dtpr_aframes = aframes; 9145 probe->dtpr_provider = provider; 9146 9147 dtrace_hash_add(dtrace_bymod, probe); 9148 dtrace_hash_add(dtrace_byfunc, probe); 9149 dtrace_hash_add(dtrace_byname, probe); 9150 9151 if (id - 1 >= dtrace_nprobes) { 9152 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 9153 size_t nsize = osize << 1; 9154 9155 if (nsize == 0) { 9156 ASSERT(osize == 0); 9157 ASSERT(dtrace_probes == NULL); 9158 nsize = sizeof (dtrace_probe_t *); 9159 } 9160 9161 probes = kmem_zalloc(nsize, KM_SLEEP); 9162 9163 if (dtrace_probes == NULL) { 9164 ASSERT(osize == 0); 9165 dtrace_probes = probes; 9166 dtrace_nprobes = 1; 9167 } else { 9168 dtrace_probe_t **oprobes = dtrace_probes; 9169 9170 bcopy(oprobes, probes, osize); 9171 dtrace_membar_producer(); 9172 dtrace_probes = probes; 9173 9174 dtrace_sync(); 9175 9176 /* 9177 * All CPUs are now seeing the new probes array; we can 9178 * safely free the old array. 9179 */ 9180 kmem_free(oprobes, osize); 9181 dtrace_nprobes <<= 1; 9182 } 9183 9184 ASSERT(id - 1 < dtrace_nprobes); 9185 } 9186 9187 ASSERT(dtrace_probes[id - 1] == NULL); 9188 dtrace_probes[id - 1] = probe; 9189 9190 if (provider != dtrace_provider) 9191 mutex_exit(&dtrace_lock); 9192 9193 return (id); 9194 } 9195 9196 static dtrace_probe_t * 9197 dtrace_probe_lookup_id(dtrace_id_t id) 9198 { 9199 ASSERT(MUTEX_HELD(&dtrace_lock)); 9200 9201 if (id == 0 || id > dtrace_nprobes) 9202 return (NULL); 9203 9204 return (dtrace_probes[id - 1]); 9205 } 9206 9207 static int 9208 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 9209 { 9210 *((dtrace_id_t *)arg) = probe->dtpr_id; 9211 9212 return (DTRACE_MATCH_DONE); 9213 } 9214 9215 /* 9216 * Look up a probe based on provider and one or more of module name, function 9217 * name and probe name. 9218 */ 9219 dtrace_id_t 9220 dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 9221 char *func, char *name) 9222 { 9223 dtrace_probekey_t pkey; 9224 dtrace_id_t id; 9225 int match; 9226 9227 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 9228 pkey.dtpk_pmatch = &dtrace_match_string; 9229 pkey.dtpk_mod = mod; 9230 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 9231 pkey.dtpk_func = func; 9232 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 9233 pkey.dtpk_name = name; 9234 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 9235 pkey.dtpk_id = DTRACE_IDNONE; 9236 9237 mutex_enter(&dtrace_lock); 9238 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 9239 dtrace_probe_lookup_match, &id); 9240 mutex_exit(&dtrace_lock); 9241 9242 ASSERT(match == 1 || match == 0); 9243 return (match ? id : 0); 9244 } 9245 9246 /* 9247 * Returns the probe argument associated with the specified probe. 9248 */ 9249 void * 9250 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 9251 { 9252 dtrace_probe_t *probe; 9253 void *rval = NULL; 9254 9255 mutex_enter(&dtrace_lock); 9256 9257 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 9258 probe->dtpr_provider == (dtrace_provider_t *)id) 9259 rval = probe->dtpr_arg; 9260 9261 mutex_exit(&dtrace_lock); 9262 9263 return (rval); 9264 } 9265 9266 /* 9267 * Copy a probe into a probe description. 9268 */ 9269 static void 9270 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 9271 { 9272 bzero(pdp, sizeof (dtrace_probedesc_t)); 9273 pdp->dtpd_id = prp->dtpr_id; 9274 9275 (void) strncpy(pdp->dtpd_provider, 9276 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 9277 9278 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 9279 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 9280 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 9281 } 9282 9283 /* 9284 * Called to indicate that a probe -- or probes -- should be provided by a 9285 * specfied provider. If the specified description is NULL, the provider will 9286 * be told to provide all of its probes. (This is done whenever a new 9287 * consumer comes along, or whenever a retained enabling is to be matched.) If 9288 * the specified description is non-NULL, the provider is given the 9289 * opportunity to dynamically provide the specified probe, allowing providers 9290 * to support the creation of probes on-the-fly. (So-called _autocreated_ 9291 * probes.) If the provider is NULL, the operations will be applied to all 9292 * providers; if the provider is non-NULL the operations will only be applied 9293 * to the specified provider. The dtrace_provider_lock must be held, and the 9294 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 9295 * will need to grab the dtrace_lock when it reenters the framework through 9296 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 9297 */ 9298 static void 9299 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 9300 { 9301 #ifdef illumos 9302 modctl_t *ctl; 9303 #endif 9304 int all = 0; 9305 9306 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9307 9308 if (prv == NULL) { 9309 all = 1; 9310 prv = dtrace_provider; 9311 } 9312 9313 do { 9314 /* 9315 * First, call the blanket provide operation. 9316 */ 9317 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 9318 9319 #ifdef illumos 9320 /* 9321 * Now call the per-module provide operation. We will grab 9322 * mod_lock to prevent the list from being modified. Note 9323 * that this also prevents the mod_busy bits from changing. 9324 * (mod_busy can only be changed with mod_lock held.) 9325 */ 9326 mutex_enter(&mod_lock); 9327 9328 ctl = &modules; 9329 do { 9330 if (ctl->mod_busy || ctl->mod_mp == NULL) 9331 continue; 9332 9333 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 9334 9335 } while ((ctl = ctl->mod_next) != &modules); 9336 9337 mutex_exit(&mod_lock); 9338 #endif 9339 } while (all && (prv = prv->dtpv_next) != NULL); 9340 } 9341 9342 #ifdef illumos 9343 /* 9344 * Iterate over each probe, and call the Framework-to-Provider API function 9345 * denoted by offs. 9346 */ 9347 static void 9348 dtrace_probe_foreach(uintptr_t offs) 9349 { 9350 dtrace_provider_t *prov; 9351 void (*func)(void *, dtrace_id_t, void *); 9352 dtrace_probe_t *probe; 9353 dtrace_icookie_t cookie; 9354 int i; 9355 9356 /* 9357 * We disable interrupts to walk through the probe array. This is 9358 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 9359 * won't see stale data. 9360 */ 9361 cookie = dtrace_interrupt_disable(); 9362 9363 for (i = 0; i < dtrace_nprobes; i++) { 9364 if ((probe = dtrace_probes[i]) == NULL) 9365 continue; 9366 9367 if (probe->dtpr_ecb == NULL) { 9368 /* 9369 * This probe isn't enabled -- don't call the function. 9370 */ 9371 continue; 9372 } 9373 9374 prov = probe->dtpr_provider; 9375 func = *((void(**)(void *, dtrace_id_t, void *)) 9376 ((uintptr_t)&prov->dtpv_pops + offs)); 9377 9378 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 9379 } 9380 9381 dtrace_interrupt_enable(cookie); 9382 } 9383 #endif 9384 9385 static int 9386 dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 9387 { 9388 dtrace_probekey_t pkey; 9389 uint32_t priv; 9390 uid_t uid; 9391 zoneid_t zoneid; 9392 9393 ASSERT(MUTEX_HELD(&dtrace_lock)); 9394 dtrace_ecb_create_cache = NULL; 9395 9396 if (desc == NULL) { 9397 /* 9398 * If we're passed a NULL description, we're being asked to 9399 * create an ECB with a NULL probe. 9400 */ 9401 (void) dtrace_ecb_create_enable(NULL, enab); 9402 return (0); 9403 } 9404 9405 dtrace_probekey(desc, &pkey); 9406 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 9407 &priv, &uid, &zoneid); 9408 9409 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 9410 enab)); 9411 } 9412 9413 /* 9414 * DTrace Helper Provider Functions 9415 */ 9416 static void 9417 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 9418 { 9419 attr->dtat_name = DOF_ATTR_NAME(dofattr); 9420 attr->dtat_data = DOF_ATTR_DATA(dofattr); 9421 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 9422 } 9423 9424 static void 9425 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 9426 const dof_provider_t *dofprov, char *strtab) 9427 { 9428 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 9429 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 9430 dofprov->dofpv_provattr); 9431 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 9432 dofprov->dofpv_modattr); 9433 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 9434 dofprov->dofpv_funcattr); 9435 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 9436 dofprov->dofpv_nameattr); 9437 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 9438 dofprov->dofpv_argsattr); 9439 } 9440 9441 static void 9442 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9443 { 9444 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9445 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9446 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 9447 dof_provider_t *provider; 9448 dof_probe_t *probe; 9449 uint32_t *off, *enoff; 9450 uint8_t *arg; 9451 char *strtab; 9452 uint_t i, nprobes; 9453 dtrace_helper_provdesc_t dhpv; 9454 dtrace_helper_probedesc_t dhpb; 9455 dtrace_meta_t *meta = dtrace_meta_pid; 9456 dtrace_mops_t *mops = &meta->dtm_mops; 9457 void *parg; 9458 9459 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9460 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9461 provider->dofpv_strtab * dof->dofh_secsize); 9462 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9463 provider->dofpv_probes * dof->dofh_secsize); 9464 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9465 provider->dofpv_prargs * dof->dofh_secsize); 9466 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9467 provider->dofpv_proffs * dof->dofh_secsize); 9468 9469 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9470 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 9471 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 9472 enoff = NULL; 9473 9474 /* 9475 * See dtrace_helper_provider_validate(). 9476 */ 9477 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 9478 provider->dofpv_prenoffs != DOF_SECT_NONE) { 9479 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9480 provider->dofpv_prenoffs * dof->dofh_secsize); 9481 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 9482 } 9483 9484 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 9485 9486 /* 9487 * Create the provider. 9488 */ 9489 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9490 9491 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 9492 return; 9493 9494 meta->dtm_count++; 9495 9496 /* 9497 * Create the probes. 9498 */ 9499 for (i = 0; i < nprobes; i++) { 9500 probe = (dof_probe_t *)(uintptr_t)(daddr + 9501 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 9502 9503 /* See the check in dtrace_helper_provider_validate(). */ 9504 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) 9505 continue; 9506 9507 dhpb.dthpb_mod = dhp->dofhp_mod; 9508 dhpb.dthpb_func = strtab + probe->dofpr_func; 9509 dhpb.dthpb_name = strtab + probe->dofpr_name; 9510 dhpb.dthpb_base = probe->dofpr_addr; 9511 dhpb.dthpb_offs = off + probe->dofpr_offidx; 9512 dhpb.dthpb_noffs = probe->dofpr_noffs; 9513 if (enoff != NULL) { 9514 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 9515 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 9516 } else { 9517 dhpb.dthpb_enoffs = NULL; 9518 dhpb.dthpb_nenoffs = 0; 9519 } 9520 dhpb.dthpb_args = arg + probe->dofpr_argidx; 9521 dhpb.dthpb_nargc = probe->dofpr_nargc; 9522 dhpb.dthpb_xargc = probe->dofpr_xargc; 9523 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 9524 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 9525 9526 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 9527 } 9528 } 9529 9530 static void 9531 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 9532 { 9533 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9534 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9535 int i; 9536 9537 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9538 9539 for (i = 0; i < dof->dofh_secnum; i++) { 9540 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9541 dof->dofh_secoff + i * dof->dofh_secsize); 9542 9543 if (sec->dofs_type != DOF_SECT_PROVIDER) 9544 continue; 9545 9546 dtrace_helper_provide_one(dhp, sec, pid); 9547 } 9548 9549 /* 9550 * We may have just created probes, so we must now rematch against 9551 * any retained enablings. Note that this call will acquire both 9552 * cpu_lock and dtrace_lock; the fact that we are holding 9553 * dtrace_meta_lock now is what defines the ordering with respect to 9554 * these three locks. 9555 */ 9556 dtrace_enabling_matchall(); 9557 } 9558 9559 static void 9560 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9561 { 9562 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9563 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9564 dof_sec_t *str_sec; 9565 dof_provider_t *provider; 9566 char *strtab; 9567 dtrace_helper_provdesc_t dhpv; 9568 dtrace_meta_t *meta = dtrace_meta_pid; 9569 dtrace_mops_t *mops = &meta->dtm_mops; 9570 9571 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9572 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9573 provider->dofpv_strtab * dof->dofh_secsize); 9574 9575 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9576 9577 /* 9578 * Create the provider. 9579 */ 9580 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9581 9582 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 9583 9584 meta->dtm_count--; 9585 } 9586 9587 static void 9588 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 9589 { 9590 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9591 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9592 int i; 9593 9594 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9595 9596 for (i = 0; i < dof->dofh_secnum; i++) { 9597 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9598 dof->dofh_secoff + i * dof->dofh_secsize); 9599 9600 if (sec->dofs_type != DOF_SECT_PROVIDER) 9601 continue; 9602 9603 dtrace_helper_provider_remove_one(dhp, sec, pid); 9604 } 9605 } 9606 9607 /* 9608 * DTrace Meta Provider-to-Framework API Functions 9609 * 9610 * These functions implement the Meta Provider-to-Framework API, as described 9611 * in <sys/dtrace.h>. 9612 */ 9613 int 9614 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 9615 dtrace_meta_provider_id_t *idp) 9616 { 9617 dtrace_meta_t *meta; 9618 dtrace_helpers_t *help, *next; 9619 int i; 9620 9621 *idp = DTRACE_METAPROVNONE; 9622 9623 /* 9624 * We strictly don't need the name, but we hold onto it for 9625 * debuggability. All hail error queues! 9626 */ 9627 if (name == NULL) { 9628 cmn_err(CE_WARN, "failed to register meta-provider: " 9629 "invalid name"); 9630 return (EINVAL); 9631 } 9632 9633 if (mops == NULL || 9634 mops->dtms_create_probe == NULL || 9635 mops->dtms_provide_pid == NULL || 9636 mops->dtms_remove_pid == NULL) { 9637 cmn_err(CE_WARN, "failed to register meta-register %s: " 9638 "invalid ops", name); 9639 return (EINVAL); 9640 } 9641 9642 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 9643 meta->dtm_mops = *mops; 9644 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 9645 (void) strcpy(meta->dtm_name, name); 9646 meta->dtm_arg = arg; 9647 9648 mutex_enter(&dtrace_meta_lock); 9649 mutex_enter(&dtrace_lock); 9650 9651 if (dtrace_meta_pid != NULL) { 9652 mutex_exit(&dtrace_lock); 9653 mutex_exit(&dtrace_meta_lock); 9654 cmn_err(CE_WARN, "failed to register meta-register %s: " 9655 "user-land meta-provider exists", name); 9656 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 9657 kmem_free(meta, sizeof (dtrace_meta_t)); 9658 return (EINVAL); 9659 } 9660 9661 dtrace_meta_pid = meta; 9662 *idp = (dtrace_meta_provider_id_t)meta; 9663 9664 /* 9665 * If there are providers and probes ready to go, pass them 9666 * off to the new meta provider now. 9667 */ 9668 9669 help = dtrace_deferred_pid; 9670 dtrace_deferred_pid = NULL; 9671 9672 mutex_exit(&dtrace_lock); 9673 9674 while (help != NULL) { 9675 for (i = 0; i < help->dthps_nprovs; i++) { 9676 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 9677 help->dthps_pid); 9678 } 9679 9680 next = help->dthps_next; 9681 help->dthps_next = NULL; 9682 help->dthps_prev = NULL; 9683 help->dthps_deferred = 0; 9684 help = next; 9685 } 9686 9687 mutex_exit(&dtrace_meta_lock); 9688 9689 return (0); 9690 } 9691 9692 int 9693 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 9694 { 9695 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 9696 9697 mutex_enter(&dtrace_meta_lock); 9698 mutex_enter(&dtrace_lock); 9699 9700 if (old == dtrace_meta_pid) { 9701 pp = &dtrace_meta_pid; 9702 } else { 9703 panic("attempt to unregister non-existent " 9704 "dtrace meta-provider %p\n", (void *)old); 9705 } 9706 9707 if (old->dtm_count != 0) { 9708 mutex_exit(&dtrace_lock); 9709 mutex_exit(&dtrace_meta_lock); 9710 return (EBUSY); 9711 } 9712 9713 *pp = NULL; 9714 9715 mutex_exit(&dtrace_lock); 9716 mutex_exit(&dtrace_meta_lock); 9717 9718 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 9719 kmem_free(old, sizeof (dtrace_meta_t)); 9720 9721 return (0); 9722 } 9723 9724 9725 /* 9726 * DTrace DIF Object Functions 9727 */ 9728 static int 9729 dtrace_difo_err(uint_t pc, const char *format, ...) 9730 { 9731 if (dtrace_err_verbose) { 9732 va_list alist; 9733 9734 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 9735 va_start(alist, format); 9736 (void) vuprintf(format, alist); 9737 va_end(alist); 9738 } 9739 9740 #ifdef DTRACE_ERRDEBUG 9741 dtrace_errdebug(format); 9742 #endif 9743 return (1); 9744 } 9745 9746 /* 9747 * Validate a DTrace DIF object by checking the IR instructions. The following 9748 * rules are currently enforced by dtrace_difo_validate(): 9749 * 9750 * 1. Each instruction must have a valid opcode 9751 * 2. Each register, string, variable, or subroutine reference must be valid 9752 * 3. No instruction can modify register %r0 (must be zero) 9753 * 4. All instruction reserved bits must be set to zero 9754 * 5. The last instruction must be a "ret" instruction 9755 * 6. All branch targets must reference a valid instruction _after_ the branch 9756 */ 9757 static int 9758 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 9759 cred_t *cr) 9760 { 9761 int err = 0, i; 9762 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9763 int kcheckload; 9764 uint_t pc; 9765 int maxglobal = -1, maxlocal = -1, maxtlocal = -1; 9766 9767 kcheckload = cr == NULL || 9768 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 9769 9770 dp->dtdo_destructive = 0; 9771 9772 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 9773 dif_instr_t instr = dp->dtdo_buf[pc]; 9774 9775 uint_t r1 = DIF_INSTR_R1(instr); 9776 uint_t r2 = DIF_INSTR_R2(instr); 9777 uint_t rd = DIF_INSTR_RD(instr); 9778 uint_t rs = DIF_INSTR_RS(instr); 9779 uint_t label = DIF_INSTR_LABEL(instr); 9780 uint_t v = DIF_INSTR_VAR(instr); 9781 uint_t subr = DIF_INSTR_SUBR(instr); 9782 uint_t type = DIF_INSTR_TYPE(instr); 9783 uint_t op = DIF_INSTR_OP(instr); 9784 9785 switch (op) { 9786 case DIF_OP_OR: 9787 case DIF_OP_XOR: 9788 case DIF_OP_AND: 9789 case DIF_OP_SLL: 9790 case DIF_OP_SRL: 9791 case DIF_OP_SRA: 9792 case DIF_OP_SUB: 9793 case DIF_OP_ADD: 9794 case DIF_OP_MUL: 9795 case DIF_OP_SDIV: 9796 case DIF_OP_UDIV: 9797 case DIF_OP_SREM: 9798 case DIF_OP_UREM: 9799 case DIF_OP_COPYS: 9800 if (r1 >= nregs) 9801 err += efunc(pc, "invalid register %u\n", r1); 9802 if (r2 >= nregs) 9803 err += efunc(pc, "invalid register %u\n", r2); 9804 if (rd >= nregs) 9805 err += efunc(pc, "invalid register %u\n", rd); 9806 if (rd == 0) 9807 err += efunc(pc, "cannot write to %r0\n"); 9808 break; 9809 case DIF_OP_NOT: 9810 case DIF_OP_MOV: 9811 case DIF_OP_ALLOCS: 9812 if (r1 >= nregs) 9813 err += efunc(pc, "invalid register %u\n", r1); 9814 if (r2 != 0) 9815 err += efunc(pc, "non-zero reserved bits\n"); 9816 if (rd >= nregs) 9817 err += efunc(pc, "invalid register %u\n", rd); 9818 if (rd == 0) 9819 err += efunc(pc, "cannot write to %r0\n"); 9820 break; 9821 case DIF_OP_LDSB: 9822 case DIF_OP_LDSH: 9823 case DIF_OP_LDSW: 9824 case DIF_OP_LDUB: 9825 case DIF_OP_LDUH: 9826 case DIF_OP_LDUW: 9827 case DIF_OP_LDX: 9828 if (r1 >= nregs) 9829 err += efunc(pc, "invalid register %u\n", r1); 9830 if (r2 != 0) 9831 err += efunc(pc, "non-zero reserved bits\n"); 9832 if (rd >= nregs) 9833 err += efunc(pc, "invalid register %u\n", rd); 9834 if (rd == 0) 9835 err += efunc(pc, "cannot write to %r0\n"); 9836 if (kcheckload) 9837 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 9838 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 9839 break; 9840 case DIF_OP_RLDSB: 9841 case DIF_OP_RLDSH: 9842 case DIF_OP_RLDSW: 9843 case DIF_OP_RLDUB: 9844 case DIF_OP_RLDUH: 9845 case DIF_OP_RLDUW: 9846 case DIF_OP_RLDX: 9847 if (r1 >= nregs) 9848 err += efunc(pc, "invalid register %u\n", r1); 9849 if (r2 != 0) 9850 err += efunc(pc, "non-zero reserved bits\n"); 9851 if (rd >= nregs) 9852 err += efunc(pc, "invalid register %u\n", rd); 9853 if (rd == 0) 9854 err += efunc(pc, "cannot write to %r0\n"); 9855 break; 9856 case DIF_OP_ULDSB: 9857 case DIF_OP_ULDSH: 9858 case DIF_OP_ULDSW: 9859 case DIF_OP_ULDUB: 9860 case DIF_OP_ULDUH: 9861 case DIF_OP_ULDUW: 9862 case DIF_OP_ULDX: 9863 if (r1 >= nregs) 9864 err += efunc(pc, "invalid register %u\n", r1); 9865 if (r2 != 0) 9866 err += efunc(pc, "non-zero reserved bits\n"); 9867 if (rd >= nregs) 9868 err += efunc(pc, "invalid register %u\n", rd); 9869 if (rd == 0) 9870 err += efunc(pc, "cannot write to %r0\n"); 9871 break; 9872 case DIF_OP_STB: 9873 case DIF_OP_STH: 9874 case DIF_OP_STW: 9875 case DIF_OP_STX: 9876 if (r1 >= nregs) 9877 err += efunc(pc, "invalid register %u\n", r1); 9878 if (r2 != 0) 9879 err += efunc(pc, "non-zero reserved bits\n"); 9880 if (rd >= nregs) 9881 err += efunc(pc, "invalid register %u\n", rd); 9882 if (rd == 0) 9883 err += efunc(pc, "cannot write to 0 address\n"); 9884 break; 9885 case DIF_OP_CMP: 9886 case DIF_OP_SCMP: 9887 if (r1 >= nregs) 9888 err += efunc(pc, "invalid register %u\n", r1); 9889 if (r2 >= nregs) 9890 err += efunc(pc, "invalid register %u\n", r2); 9891 if (rd != 0) 9892 err += efunc(pc, "non-zero reserved bits\n"); 9893 break; 9894 case DIF_OP_TST: 9895 if (r1 >= nregs) 9896 err += efunc(pc, "invalid register %u\n", r1); 9897 if (r2 != 0 || rd != 0) 9898 err += efunc(pc, "non-zero reserved bits\n"); 9899 break; 9900 case DIF_OP_BA: 9901 case DIF_OP_BE: 9902 case DIF_OP_BNE: 9903 case DIF_OP_BG: 9904 case DIF_OP_BGU: 9905 case DIF_OP_BGE: 9906 case DIF_OP_BGEU: 9907 case DIF_OP_BL: 9908 case DIF_OP_BLU: 9909 case DIF_OP_BLE: 9910 case DIF_OP_BLEU: 9911 if (label >= dp->dtdo_len) { 9912 err += efunc(pc, "invalid branch target %u\n", 9913 label); 9914 } 9915 if (label <= pc) { 9916 err += efunc(pc, "backward branch to %u\n", 9917 label); 9918 } 9919 break; 9920 case DIF_OP_RET: 9921 if (r1 != 0 || r2 != 0) 9922 err += efunc(pc, "non-zero reserved bits\n"); 9923 if (rd >= nregs) 9924 err += efunc(pc, "invalid register %u\n", rd); 9925 break; 9926 case DIF_OP_NOP: 9927 case DIF_OP_POPTS: 9928 case DIF_OP_FLUSHTS: 9929 if (r1 != 0 || r2 != 0 || rd != 0) 9930 err += efunc(pc, "non-zero reserved bits\n"); 9931 break; 9932 case DIF_OP_SETX: 9933 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 9934 err += efunc(pc, "invalid integer ref %u\n", 9935 DIF_INSTR_INTEGER(instr)); 9936 } 9937 if (rd >= nregs) 9938 err += efunc(pc, "invalid register %u\n", rd); 9939 if (rd == 0) 9940 err += efunc(pc, "cannot write to %r0\n"); 9941 break; 9942 case DIF_OP_SETS: 9943 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 9944 err += efunc(pc, "invalid string ref %u\n", 9945 DIF_INSTR_STRING(instr)); 9946 } 9947 if (rd >= nregs) 9948 err += efunc(pc, "invalid register %u\n", rd); 9949 if (rd == 0) 9950 err += efunc(pc, "cannot write to %r0\n"); 9951 break; 9952 case DIF_OP_LDGA: 9953 case DIF_OP_LDTA: 9954 if (r1 > DIF_VAR_ARRAY_MAX) 9955 err += efunc(pc, "invalid array %u\n", r1); 9956 if (r2 >= nregs) 9957 err += efunc(pc, "invalid register %u\n", r2); 9958 if (rd >= nregs) 9959 err += efunc(pc, "invalid register %u\n", rd); 9960 if (rd == 0) 9961 err += efunc(pc, "cannot write to %r0\n"); 9962 break; 9963 case DIF_OP_LDGS: 9964 case DIF_OP_LDTS: 9965 case DIF_OP_LDLS: 9966 case DIF_OP_LDGAA: 9967 case DIF_OP_LDTAA: 9968 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 9969 err += efunc(pc, "invalid variable %u\n", v); 9970 if (rd >= nregs) 9971 err += efunc(pc, "invalid register %u\n", rd); 9972 if (rd == 0) 9973 err += efunc(pc, "cannot write to %r0\n"); 9974 break; 9975 case DIF_OP_STGS: 9976 case DIF_OP_STTS: 9977 case DIF_OP_STLS: 9978 case DIF_OP_STGAA: 9979 case DIF_OP_STTAA: 9980 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 9981 err += efunc(pc, "invalid variable %u\n", v); 9982 if (rs >= nregs) 9983 err += efunc(pc, "invalid register %u\n", rd); 9984 break; 9985 case DIF_OP_CALL: 9986 if (subr > DIF_SUBR_MAX) 9987 err += efunc(pc, "invalid subr %u\n", subr); 9988 if (rd >= nregs) 9989 err += efunc(pc, "invalid register %u\n", rd); 9990 if (rd == 0) 9991 err += efunc(pc, "cannot write to %r0\n"); 9992 9993 if (subr == DIF_SUBR_COPYOUT || 9994 subr == DIF_SUBR_COPYOUTSTR) { 9995 dp->dtdo_destructive = 1; 9996 } 9997 9998 if (subr == DIF_SUBR_GETF) { 9999 /* 10000 * If we have a getf() we need to record that 10001 * in our state. Note that our state can be 10002 * NULL if this is a helper -- but in that 10003 * case, the call to getf() is itself illegal, 10004 * and will be caught (slightly later) when 10005 * the helper is validated. 10006 */ 10007 if (vstate->dtvs_state != NULL) 10008 vstate->dtvs_state->dts_getf++; 10009 } 10010 10011 break; 10012 case DIF_OP_PUSHTR: 10013 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 10014 err += efunc(pc, "invalid ref type %u\n", type); 10015 if (r2 >= nregs) 10016 err += efunc(pc, "invalid register %u\n", r2); 10017 if (rs >= nregs) 10018 err += efunc(pc, "invalid register %u\n", rs); 10019 break; 10020 case DIF_OP_PUSHTV: 10021 if (type != DIF_TYPE_CTF) 10022 err += efunc(pc, "invalid val type %u\n", type); 10023 if (r2 >= nregs) 10024 err += efunc(pc, "invalid register %u\n", r2); 10025 if (rs >= nregs) 10026 err += efunc(pc, "invalid register %u\n", rs); 10027 break; 10028 default: 10029 err += efunc(pc, "invalid opcode %u\n", 10030 DIF_INSTR_OP(instr)); 10031 } 10032 } 10033 10034 if (dp->dtdo_len != 0 && 10035 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 10036 err += efunc(dp->dtdo_len - 1, 10037 "expected 'ret' as last DIF instruction\n"); 10038 } 10039 10040 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) { 10041 /* 10042 * If we're not returning by reference, the size must be either 10043 * 0 or the size of one of the base types. 10044 */ 10045 switch (dp->dtdo_rtype.dtdt_size) { 10046 case 0: 10047 case sizeof (uint8_t): 10048 case sizeof (uint16_t): 10049 case sizeof (uint32_t): 10050 case sizeof (uint64_t): 10051 break; 10052 10053 default: 10054 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 10055 } 10056 } 10057 10058 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 10059 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 10060 dtrace_diftype_t *vt, *et; 10061 uint_t id, ndx; 10062 10063 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 10064 v->dtdv_scope != DIFV_SCOPE_THREAD && 10065 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 10066 err += efunc(i, "unrecognized variable scope %d\n", 10067 v->dtdv_scope); 10068 break; 10069 } 10070 10071 if (v->dtdv_kind != DIFV_KIND_ARRAY && 10072 v->dtdv_kind != DIFV_KIND_SCALAR) { 10073 err += efunc(i, "unrecognized variable type %d\n", 10074 v->dtdv_kind); 10075 break; 10076 } 10077 10078 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 10079 err += efunc(i, "%d exceeds variable id limit\n", id); 10080 break; 10081 } 10082 10083 if (id < DIF_VAR_OTHER_UBASE) 10084 continue; 10085 10086 /* 10087 * For user-defined variables, we need to check that this 10088 * definition is identical to any previous definition that we 10089 * encountered. 10090 */ 10091 ndx = id - DIF_VAR_OTHER_UBASE; 10092 10093 switch (v->dtdv_scope) { 10094 case DIFV_SCOPE_GLOBAL: 10095 if (maxglobal == -1 || ndx > maxglobal) 10096 maxglobal = ndx; 10097 10098 if (ndx < vstate->dtvs_nglobals) { 10099 dtrace_statvar_t *svar; 10100 10101 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 10102 existing = &svar->dtsv_var; 10103 } 10104 10105 break; 10106 10107 case DIFV_SCOPE_THREAD: 10108 if (maxtlocal == -1 || ndx > maxtlocal) 10109 maxtlocal = ndx; 10110 10111 if (ndx < vstate->dtvs_ntlocals) 10112 existing = &vstate->dtvs_tlocals[ndx]; 10113 break; 10114 10115 case DIFV_SCOPE_LOCAL: 10116 if (maxlocal == -1 || ndx > maxlocal) 10117 maxlocal = ndx; 10118 10119 if (ndx < vstate->dtvs_nlocals) { 10120 dtrace_statvar_t *svar; 10121 10122 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 10123 existing = &svar->dtsv_var; 10124 } 10125 10126 break; 10127 } 10128 10129 vt = &v->dtdv_type; 10130 10131 if (vt->dtdt_flags & DIF_TF_BYREF) { 10132 if (vt->dtdt_size == 0) { 10133 err += efunc(i, "zero-sized variable\n"); 10134 break; 10135 } 10136 10137 if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL || 10138 v->dtdv_scope == DIFV_SCOPE_LOCAL) && 10139 vt->dtdt_size > dtrace_statvar_maxsize) { 10140 err += efunc(i, "oversized by-ref static\n"); 10141 break; 10142 } 10143 } 10144 10145 if (existing == NULL || existing->dtdv_id == 0) 10146 continue; 10147 10148 ASSERT(existing->dtdv_id == v->dtdv_id); 10149 ASSERT(existing->dtdv_scope == v->dtdv_scope); 10150 10151 if (existing->dtdv_kind != v->dtdv_kind) 10152 err += efunc(i, "%d changed variable kind\n", id); 10153 10154 et = &existing->dtdv_type; 10155 10156 if (vt->dtdt_flags != et->dtdt_flags) { 10157 err += efunc(i, "%d changed variable type flags\n", id); 10158 break; 10159 } 10160 10161 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 10162 err += efunc(i, "%d changed variable type size\n", id); 10163 break; 10164 } 10165 } 10166 10167 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 10168 dif_instr_t instr = dp->dtdo_buf[pc]; 10169 10170 uint_t v = DIF_INSTR_VAR(instr); 10171 uint_t op = DIF_INSTR_OP(instr); 10172 10173 switch (op) { 10174 case DIF_OP_LDGS: 10175 case DIF_OP_LDGAA: 10176 case DIF_OP_STGS: 10177 case DIF_OP_STGAA: 10178 if (v > DIF_VAR_OTHER_UBASE + maxglobal) 10179 err += efunc(pc, "invalid variable %u\n", v); 10180 break; 10181 case DIF_OP_LDTS: 10182 case DIF_OP_LDTAA: 10183 case DIF_OP_STTS: 10184 case DIF_OP_STTAA: 10185 if (v > DIF_VAR_OTHER_UBASE + maxtlocal) 10186 err += efunc(pc, "invalid variable %u\n", v); 10187 break; 10188 case DIF_OP_LDLS: 10189 case DIF_OP_STLS: 10190 if (v > DIF_VAR_OTHER_UBASE + maxlocal) 10191 err += efunc(pc, "invalid variable %u\n", v); 10192 break; 10193 default: 10194 break; 10195 } 10196 } 10197 10198 return (err); 10199 } 10200 10201 /* 10202 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 10203 * are much more constrained than normal DIFOs. Specifically, they may 10204 * not: 10205 * 10206 * 1. Make calls to subroutines other than copyin(), copyinstr() or 10207 * miscellaneous string routines 10208 * 2. Access DTrace variables other than the args[] array, and the 10209 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 10210 * 3. Have thread-local variables. 10211 * 4. Have dynamic variables. 10212 */ 10213 static int 10214 dtrace_difo_validate_helper(dtrace_difo_t *dp) 10215 { 10216 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 10217 int err = 0; 10218 uint_t pc; 10219 10220 for (pc = 0; pc < dp->dtdo_len; pc++) { 10221 dif_instr_t instr = dp->dtdo_buf[pc]; 10222 10223 uint_t v = DIF_INSTR_VAR(instr); 10224 uint_t subr = DIF_INSTR_SUBR(instr); 10225 uint_t op = DIF_INSTR_OP(instr); 10226 10227 switch (op) { 10228 case DIF_OP_OR: 10229 case DIF_OP_XOR: 10230 case DIF_OP_AND: 10231 case DIF_OP_SLL: 10232 case DIF_OP_SRL: 10233 case DIF_OP_SRA: 10234 case DIF_OP_SUB: 10235 case DIF_OP_ADD: 10236 case DIF_OP_MUL: 10237 case DIF_OP_SDIV: 10238 case DIF_OP_UDIV: 10239 case DIF_OP_SREM: 10240 case DIF_OP_UREM: 10241 case DIF_OP_COPYS: 10242 case DIF_OP_NOT: 10243 case DIF_OP_MOV: 10244 case DIF_OP_RLDSB: 10245 case DIF_OP_RLDSH: 10246 case DIF_OP_RLDSW: 10247 case DIF_OP_RLDUB: 10248 case DIF_OP_RLDUH: 10249 case DIF_OP_RLDUW: 10250 case DIF_OP_RLDX: 10251 case DIF_OP_ULDSB: 10252 case DIF_OP_ULDSH: 10253 case DIF_OP_ULDSW: 10254 case DIF_OP_ULDUB: 10255 case DIF_OP_ULDUH: 10256 case DIF_OP_ULDUW: 10257 case DIF_OP_ULDX: 10258 case DIF_OP_STB: 10259 case DIF_OP_STH: 10260 case DIF_OP_STW: 10261 case DIF_OP_STX: 10262 case DIF_OP_ALLOCS: 10263 case DIF_OP_CMP: 10264 case DIF_OP_SCMP: 10265 case DIF_OP_TST: 10266 case DIF_OP_BA: 10267 case DIF_OP_BE: 10268 case DIF_OP_BNE: 10269 case DIF_OP_BG: 10270 case DIF_OP_BGU: 10271 case DIF_OP_BGE: 10272 case DIF_OP_BGEU: 10273 case DIF_OP_BL: 10274 case DIF_OP_BLU: 10275 case DIF_OP_BLE: 10276 case DIF_OP_BLEU: 10277 case DIF_OP_RET: 10278 case DIF_OP_NOP: 10279 case DIF_OP_POPTS: 10280 case DIF_OP_FLUSHTS: 10281 case DIF_OP_SETX: 10282 case DIF_OP_SETS: 10283 case DIF_OP_LDGA: 10284 case DIF_OP_LDLS: 10285 case DIF_OP_STGS: 10286 case DIF_OP_STLS: 10287 case DIF_OP_PUSHTR: 10288 case DIF_OP_PUSHTV: 10289 break; 10290 10291 case DIF_OP_LDGS: 10292 if (v >= DIF_VAR_OTHER_UBASE) 10293 break; 10294 10295 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 10296 break; 10297 10298 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 10299 v == DIF_VAR_PPID || v == DIF_VAR_TID || 10300 v == DIF_VAR_EXECARGS || 10301 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 10302 v == DIF_VAR_UID || v == DIF_VAR_GID) 10303 break; 10304 10305 err += efunc(pc, "illegal variable %u\n", v); 10306 break; 10307 10308 case DIF_OP_LDTA: 10309 case DIF_OP_LDTS: 10310 case DIF_OP_LDGAA: 10311 case DIF_OP_LDTAA: 10312 err += efunc(pc, "illegal dynamic variable load\n"); 10313 break; 10314 10315 case DIF_OP_STTS: 10316 case DIF_OP_STGAA: 10317 case DIF_OP_STTAA: 10318 err += efunc(pc, "illegal dynamic variable store\n"); 10319 break; 10320 10321 case DIF_OP_CALL: 10322 if (subr == DIF_SUBR_ALLOCA || 10323 subr == DIF_SUBR_BCOPY || 10324 subr == DIF_SUBR_COPYIN || 10325 subr == DIF_SUBR_COPYINTO || 10326 subr == DIF_SUBR_COPYINSTR || 10327 subr == DIF_SUBR_INDEX || 10328 subr == DIF_SUBR_INET_NTOA || 10329 subr == DIF_SUBR_INET_NTOA6 || 10330 subr == DIF_SUBR_INET_NTOP || 10331 subr == DIF_SUBR_JSON || 10332 subr == DIF_SUBR_LLTOSTR || 10333 subr == DIF_SUBR_STRTOLL || 10334 subr == DIF_SUBR_RINDEX || 10335 subr == DIF_SUBR_STRCHR || 10336 subr == DIF_SUBR_STRJOIN || 10337 subr == DIF_SUBR_STRRCHR || 10338 subr == DIF_SUBR_STRSTR || 10339 subr == DIF_SUBR_HTONS || 10340 subr == DIF_SUBR_HTONL || 10341 subr == DIF_SUBR_HTONLL || 10342 subr == DIF_SUBR_NTOHS || 10343 subr == DIF_SUBR_NTOHL || 10344 subr == DIF_SUBR_NTOHLL || 10345 subr == DIF_SUBR_MEMREF || 10346 #ifndef illumos 10347 subr == DIF_SUBR_MEMSTR || 10348 #endif 10349 subr == DIF_SUBR_TYPEREF) 10350 break; 10351 10352 err += efunc(pc, "invalid subr %u\n", subr); 10353 break; 10354 10355 default: 10356 err += efunc(pc, "invalid opcode %u\n", 10357 DIF_INSTR_OP(instr)); 10358 } 10359 } 10360 10361 return (err); 10362 } 10363 10364 /* 10365 * Returns 1 if the expression in the DIF object can be cached on a per-thread 10366 * basis; 0 if not. 10367 */ 10368 static int 10369 dtrace_difo_cacheable(dtrace_difo_t *dp) 10370 { 10371 int i; 10372 10373 if (dp == NULL) 10374 return (0); 10375 10376 for (i = 0; i < dp->dtdo_varlen; i++) { 10377 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10378 10379 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 10380 continue; 10381 10382 switch (v->dtdv_id) { 10383 case DIF_VAR_CURTHREAD: 10384 case DIF_VAR_PID: 10385 case DIF_VAR_TID: 10386 case DIF_VAR_EXECARGS: 10387 case DIF_VAR_EXECNAME: 10388 case DIF_VAR_ZONENAME: 10389 break; 10390 10391 default: 10392 return (0); 10393 } 10394 } 10395 10396 /* 10397 * This DIF object may be cacheable. Now we need to look for any 10398 * array loading instructions, any memory loading instructions, or 10399 * any stores to thread-local variables. 10400 */ 10401 for (i = 0; i < dp->dtdo_len; i++) { 10402 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 10403 10404 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 10405 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 10406 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 10407 op == DIF_OP_LDGA || op == DIF_OP_STTS) 10408 return (0); 10409 } 10410 10411 return (1); 10412 } 10413 10414 static void 10415 dtrace_difo_hold(dtrace_difo_t *dp) 10416 { 10417 int i; 10418 10419 ASSERT(MUTEX_HELD(&dtrace_lock)); 10420 10421 dp->dtdo_refcnt++; 10422 ASSERT(dp->dtdo_refcnt != 0); 10423 10424 /* 10425 * We need to check this DIF object for references to the variable 10426 * DIF_VAR_VTIMESTAMP. 10427 */ 10428 for (i = 0; i < dp->dtdo_varlen; i++) { 10429 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10430 10431 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10432 continue; 10433 10434 if (dtrace_vtime_references++ == 0) 10435 dtrace_vtime_enable(); 10436 } 10437 } 10438 10439 /* 10440 * This routine calculates the dynamic variable chunksize for a given DIF 10441 * object. The calculation is not fool-proof, and can probably be tricked by 10442 * malicious DIF -- but it works for all compiler-generated DIF. Because this 10443 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 10444 * if a dynamic variable size exceeds the chunksize. 10445 */ 10446 static void 10447 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10448 { 10449 uint64_t sval = 0; 10450 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 10451 const dif_instr_t *text = dp->dtdo_buf; 10452 uint_t pc, srd = 0; 10453 uint_t ttop = 0; 10454 size_t size, ksize; 10455 uint_t id, i; 10456 10457 for (pc = 0; pc < dp->dtdo_len; pc++) { 10458 dif_instr_t instr = text[pc]; 10459 uint_t op = DIF_INSTR_OP(instr); 10460 uint_t rd = DIF_INSTR_RD(instr); 10461 uint_t r1 = DIF_INSTR_R1(instr); 10462 uint_t nkeys = 0; 10463 uchar_t scope = 0; 10464 10465 dtrace_key_t *key = tupregs; 10466 10467 switch (op) { 10468 case DIF_OP_SETX: 10469 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 10470 srd = rd; 10471 continue; 10472 10473 case DIF_OP_STTS: 10474 key = &tupregs[DIF_DTR_NREGS]; 10475 key[0].dttk_size = 0; 10476 key[1].dttk_size = 0; 10477 nkeys = 2; 10478 scope = DIFV_SCOPE_THREAD; 10479 break; 10480 10481 case DIF_OP_STGAA: 10482 case DIF_OP_STTAA: 10483 nkeys = ttop; 10484 10485 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 10486 key[nkeys++].dttk_size = 0; 10487 10488 key[nkeys++].dttk_size = 0; 10489 10490 if (op == DIF_OP_STTAA) { 10491 scope = DIFV_SCOPE_THREAD; 10492 } else { 10493 scope = DIFV_SCOPE_GLOBAL; 10494 } 10495 10496 break; 10497 10498 case DIF_OP_PUSHTR: 10499 if (ttop == DIF_DTR_NREGS) 10500 return; 10501 10502 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 10503 /* 10504 * If the register for the size of the "pushtr" 10505 * is %r0 (or the value is 0) and the type is 10506 * a string, we'll use the system-wide default 10507 * string size. 10508 */ 10509 tupregs[ttop++].dttk_size = 10510 dtrace_strsize_default; 10511 } else { 10512 if (srd == 0) 10513 return; 10514 10515 if (sval > LONG_MAX) 10516 return; 10517 10518 tupregs[ttop++].dttk_size = sval; 10519 } 10520 10521 break; 10522 10523 case DIF_OP_PUSHTV: 10524 if (ttop == DIF_DTR_NREGS) 10525 return; 10526 10527 tupregs[ttop++].dttk_size = 0; 10528 break; 10529 10530 case DIF_OP_FLUSHTS: 10531 ttop = 0; 10532 break; 10533 10534 case DIF_OP_POPTS: 10535 if (ttop != 0) 10536 ttop--; 10537 break; 10538 } 10539 10540 sval = 0; 10541 srd = 0; 10542 10543 if (nkeys == 0) 10544 continue; 10545 10546 /* 10547 * We have a dynamic variable allocation; calculate its size. 10548 */ 10549 for (ksize = 0, i = 0; i < nkeys; i++) 10550 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 10551 10552 size = sizeof (dtrace_dynvar_t); 10553 size += sizeof (dtrace_key_t) * (nkeys - 1); 10554 size += ksize; 10555 10556 /* 10557 * Now we need to determine the size of the stored data. 10558 */ 10559 id = DIF_INSTR_VAR(instr); 10560 10561 for (i = 0; i < dp->dtdo_varlen; i++) { 10562 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10563 10564 if (v->dtdv_id == id && v->dtdv_scope == scope) { 10565 size += v->dtdv_type.dtdt_size; 10566 break; 10567 } 10568 } 10569 10570 if (i == dp->dtdo_varlen) 10571 return; 10572 10573 /* 10574 * We have the size. If this is larger than the chunk size 10575 * for our dynamic variable state, reset the chunk size. 10576 */ 10577 size = P2ROUNDUP(size, sizeof (uint64_t)); 10578 10579 /* 10580 * Before setting the chunk size, check that we're not going 10581 * to set it to a negative value... 10582 */ 10583 if (size > LONG_MAX) 10584 return; 10585 10586 /* 10587 * ...and make certain that we didn't badly overflow. 10588 */ 10589 if (size < ksize || size < sizeof (dtrace_dynvar_t)) 10590 return; 10591 10592 if (size > vstate->dtvs_dynvars.dtds_chunksize) 10593 vstate->dtvs_dynvars.dtds_chunksize = size; 10594 } 10595 } 10596 10597 static void 10598 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10599 { 10600 int i, oldsvars, osz, nsz, otlocals, ntlocals; 10601 uint_t id; 10602 10603 ASSERT(MUTEX_HELD(&dtrace_lock)); 10604 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 10605 10606 for (i = 0; i < dp->dtdo_varlen; i++) { 10607 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10608 dtrace_statvar_t *svar, ***svarp = NULL; 10609 size_t dsize = 0; 10610 uint8_t scope = v->dtdv_scope; 10611 int *np = NULL; 10612 10613 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10614 continue; 10615 10616 id -= DIF_VAR_OTHER_UBASE; 10617 10618 switch (scope) { 10619 case DIFV_SCOPE_THREAD: 10620 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 10621 dtrace_difv_t *tlocals; 10622 10623 if ((ntlocals = (otlocals << 1)) == 0) 10624 ntlocals = 1; 10625 10626 osz = otlocals * sizeof (dtrace_difv_t); 10627 nsz = ntlocals * sizeof (dtrace_difv_t); 10628 10629 tlocals = kmem_zalloc(nsz, KM_SLEEP); 10630 10631 if (osz != 0) { 10632 bcopy(vstate->dtvs_tlocals, 10633 tlocals, osz); 10634 kmem_free(vstate->dtvs_tlocals, osz); 10635 } 10636 10637 vstate->dtvs_tlocals = tlocals; 10638 vstate->dtvs_ntlocals = ntlocals; 10639 } 10640 10641 vstate->dtvs_tlocals[id] = *v; 10642 continue; 10643 10644 case DIFV_SCOPE_LOCAL: 10645 np = &vstate->dtvs_nlocals; 10646 svarp = &vstate->dtvs_locals; 10647 10648 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10649 dsize = NCPU * (v->dtdv_type.dtdt_size + 10650 sizeof (uint64_t)); 10651 else 10652 dsize = NCPU * sizeof (uint64_t); 10653 10654 break; 10655 10656 case DIFV_SCOPE_GLOBAL: 10657 np = &vstate->dtvs_nglobals; 10658 svarp = &vstate->dtvs_globals; 10659 10660 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10661 dsize = v->dtdv_type.dtdt_size + 10662 sizeof (uint64_t); 10663 10664 break; 10665 10666 default: 10667 ASSERT(0); 10668 } 10669 10670 while (id >= (oldsvars = *np)) { 10671 dtrace_statvar_t **statics; 10672 int newsvars, oldsize, newsize; 10673 10674 if ((newsvars = (oldsvars << 1)) == 0) 10675 newsvars = 1; 10676 10677 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 10678 newsize = newsvars * sizeof (dtrace_statvar_t *); 10679 10680 statics = kmem_zalloc(newsize, KM_SLEEP); 10681 10682 if (oldsize != 0) { 10683 bcopy(*svarp, statics, oldsize); 10684 kmem_free(*svarp, oldsize); 10685 } 10686 10687 *svarp = statics; 10688 *np = newsvars; 10689 } 10690 10691 if ((svar = (*svarp)[id]) == NULL) { 10692 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 10693 svar->dtsv_var = *v; 10694 10695 if ((svar->dtsv_size = dsize) != 0) { 10696 svar->dtsv_data = (uint64_t)(uintptr_t) 10697 kmem_zalloc(dsize, KM_SLEEP); 10698 } 10699 10700 (*svarp)[id] = svar; 10701 } 10702 10703 svar->dtsv_refcnt++; 10704 } 10705 10706 dtrace_difo_chunksize(dp, vstate); 10707 dtrace_difo_hold(dp); 10708 } 10709 10710 static dtrace_difo_t * 10711 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10712 { 10713 dtrace_difo_t *new; 10714 size_t sz; 10715 10716 ASSERT(dp->dtdo_buf != NULL); 10717 ASSERT(dp->dtdo_refcnt != 0); 10718 10719 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10720 10721 ASSERT(dp->dtdo_buf != NULL); 10722 sz = dp->dtdo_len * sizeof (dif_instr_t); 10723 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 10724 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 10725 new->dtdo_len = dp->dtdo_len; 10726 10727 if (dp->dtdo_strtab != NULL) { 10728 ASSERT(dp->dtdo_strlen != 0); 10729 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 10730 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 10731 new->dtdo_strlen = dp->dtdo_strlen; 10732 } 10733 10734 if (dp->dtdo_inttab != NULL) { 10735 ASSERT(dp->dtdo_intlen != 0); 10736 sz = dp->dtdo_intlen * sizeof (uint64_t); 10737 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 10738 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 10739 new->dtdo_intlen = dp->dtdo_intlen; 10740 } 10741 10742 if (dp->dtdo_vartab != NULL) { 10743 ASSERT(dp->dtdo_varlen != 0); 10744 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 10745 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 10746 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 10747 new->dtdo_varlen = dp->dtdo_varlen; 10748 } 10749 10750 dtrace_difo_init(new, vstate); 10751 return (new); 10752 } 10753 10754 static void 10755 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10756 { 10757 int i; 10758 10759 ASSERT(dp->dtdo_refcnt == 0); 10760 10761 for (i = 0; i < dp->dtdo_varlen; i++) { 10762 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10763 dtrace_statvar_t *svar, **svarp = NULL; 10764 uint_t id; 10765 uint8_t scope = v->dtdv_scope; 10766 int *np = NULL; 10767 10768 switch (scope) { 10769 case DIFV_SCOPE_THREAD: 10770 continue; 10771 10772 case DIFV_SCOPE_LOCAL: 10773 np = &vstate->dtvs_nlocals; 10774 svarp = vstate->dtvs_locals; 10775 break; 10776 10777 case DIFV_SCOPE_GLOBAL: 10778 np = &vstate->dtvs_nglobals; 10779 svarp = vstate->dtvs_globals; 10780 break; 10781 10782 default: 10783 ASSERT(0); 10784 } 10785 10786 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10787 continue; 10788 10789 id -= DIF_VAR_OTHER_UBASE; 10790 ASSERT(id < *np); 10791 10792 svar = svarp[id]; 10793 ASSERT(svar != NULL); 10794 ASSERT(svar->dtsv_refcnt > 0); 10795 10796 if (--svar->dtsv_refcnt > 0) 10797 continue; 10798 10799 if (svar->dtsv_size != 0) { 10800 ASSERT(svar->dtsv_data != 0); 10801 kmem_free((void *)(uintptr_t)svar->dtsv_data, 10802 svar->dtsv_size); 10803 } 10804 10805 kmem_free(svar, sizeof (dtrace_statvar_t)); 10806 svarp[id] = NULL; 10807 } 10808 10809 if (dp->dtdo_buf != NULL) 10810 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10811 if (dp->dtdo_inttab != NULL) 10812 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10813 if (dp->dtdo_strtab != NULL) 10814 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10815 if (dp->dtdo_vartab != NULL) 10816 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10817 10818 kmem_free(dp, sizeof (dtrace_difo_t)); 10819 } 10820 10821 static void 10822 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10823 { 10824 int i; 10825 10826 ASSERT(MUTEX_HELD(&dtrace_lock)); 10827 ASSERT(dp->dtdo_refcnt != 0); 10828 10829 for (i = 0; i < dp->dtdo_varlen; i++) { 10830 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10831 10832 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10833 continue; 10834 10835 ASSERT(dtrace_vtime_references > 0); 10836 if (--dtrace_vtime_references == 0) 10837 dtrace_vtime_disable(); 10838 } 10839 10840 if (--dp->dtdo_refcnt == 0) 10841 dtrace_difo_destroy(dp, vstate); 10842 } 10843 10844 /* 10845 * DTrace Format Functions 10846 */ 10847 static uint16_t 10848 dtrace_format_add(dtrace_state_t *state, char *str) 10849 { 10850 char *fmt, **new; 10851 uint16_t ndx, len = strlen(str) + 1; 10852 10853 fmt = kmem_zalloc(len, KM_SLEEP); 10854 bcopy(str, fmt, len); 10855 10856 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 10857 if (state->dts_formats[ndx] == NULL) { 10858 state->dts_formats[ndx] = fmt; 10859 return (ndx + 1); 10860 } 10861 } 10862 10863 if (state->dts_nformats == USHRT_MAX) { 10864 /* 10865 * This is only likely if a denial-of-service attack is being 10866 * attempted. As such, it's okay to fail silently here. 10867 */ 10868 kmem_free(fmt, len); 10869 return (0); 10870 } 10871 10872 /* 10873 * For simplicity, we always resize the formats array to be exactly the 10874 * number of formats. 10875 */ 10876 ndx = state->dts_nformats++; 10877 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 10878 10879 if (state->dts_formats != NULL) { 10880 ASSERT(ndx != 0); 10881 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 10882 kmem_free(state->dts_formats, ndx * sizeof (char *)); 10883 } 10884 10885 state->dts_formats = new; 10886 state->dts_formats[ndx] = fmt; 10887 10888 return (ndx + 1); 10889 } 10890 10891 static void 10892 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 10893 { 10894 char *fmt; 10895 10896 ASSERT(state->dts_formats != NULL); 10897 ASSERT(format <= state->dts_nformats); 10898 ASSERT(state->dts_formats[format - 1] != NULL); 10899 10900 fmt = state->dts_formats[format - 1]; 10901 kmem_free(fmt, strlen(fmt) + 1); 10902 state->dts_formats[format - 1] = NULL; 10903 } 10904 10905 static void 10906 dtrace_format_destroy(dtrace_state_t *state) 10907 { 10908 int i; 10909 10910 if (state->dts_nformats == 0) { 10911 ASSERT(state->dts_formats == NULL); 10912 return; 10913 } 10914 10915 ASSERT(state->dts_formats != NULL); 10916 10917 for (i = 0; i < state->dts_nformats; i++) { 10918 char *fmt = state->dts_formats[i]; 10919 10920 if (fmt == NULL) 10921 continue; 10922 10923 kmem_free(fmt, strlen(fmt) + 1); 10924 } 10925 10926 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 10927 state->dts_nformats = 0; 10928 state->dts_formats = NULL; 10929 } 10930 10931 /* 10932 * DTrace Predicate Functions 10933 */ 10934 static dtrace_predicate_t * 10935 dtrace_predicate_create(dtrace_difo_t *dp) 10936 { 10937 dtrace_predicate_t *pred; 10938 10939 ASSERT(MUTEX_HELD(&dtrace_lock)); 10940 ASSERT(dp->dtdo_refcnt != 0); 10941 10942 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 10943 pred->dtp_difo = dp; 10944 pred->dtp_refcnt = 1; 10945 10946 if (!dtrace_difo_cacheable(dp)) 10947 return (pred); 10948 10949 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 10950 /* 10951 * This is only theoretically possible -- we have had 2^32 10952 * cacheable predicates on this machine. We cannot allow any 10953 * more predicates to become cacheable: as unlikely as it is, 10954 * there may be a thread caching a (now stale) predicate cache 10955 * ID. (N.B.: the temptation is being successfully resisted to 10956 * have this cmn_err() "Holy shit -- we executed this code!") 10957 */ 10958 return (pred); 10959 } 10960 10961 pred->dtp_cacheid = dtrace_predcache_id++; 10962 10963 return (pred); 10964 } 10965 10966 static void 10967 dtrace_predicate_hold(dtrace_predicate_t *pred) 10968 { 10969 ASSERT(MUTEX_HELD(&dtrace_lock)); 10970 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 10971 ASSERT(pred->dtp_refcnt > 0); 10972 10973 pred->dtp_refcnt++; 10974 } 10975 10976 static void 10977 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 10978 { 10979 dtrace_difo_t *dp = pred->dtp_difo; 10980 10981 ASSERT(MUTEX_HELD(&dtrace_lock)); 10982 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 10983 ASSERT(pred->dtp_refcnt > 0); 10984 10985 if (--pred->dtp_refcnt == 0) { 10986 dtrace_difo_release(pred->dtp_difo, vstate); 10987 kmem_free(pred, sizeof (dtrace_predicate_t)); 10988 } 10989 } 10990 10991 /* 10992 * DTrace Action Description Functions 10993 */ 10994 static dtrace_actdesc_t * 10995 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 10996 uint64_t uarg, uint64_t arg) 10997 { 10998 dtrace_actdesc_t *act; 10999 11000 #ifdef illumos 11001 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 11002 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 11003 #endif 11004 11005 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 11006 act->dtad_kind = kind; 11007 act->dtad_ntuple = ntuple; 11008 act->dtad_uarg = uarg; 11009 act->dtad_arg = arg; 11010 act->dtad_refcnt = 1; 11011 11012 return (act); 11013 } 11014 11015 static void 11016 dtrace_actdesc_hold(dtrace_actdesc_t *act) 11017 { 11018 ASSERT(act->dtad_refcnt >= 1); 11019 act->dtad_refcnt++; 11020 } 11021 11022 static void 11023 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 11024 { 11025 dtrace_actkind_t kind = act->dtad_kind; 11026 dtrace_difo_t *dp; 11027 11028 ASSERT(act->dtad_refcnt >= 1); 11029 11030 if (--act->dtad_refcnt != 0) 11031 return; 11032 11033 if ((dp = act->dtad_difo) != NULL) 11034 dtrace_difo_release(dp, vstate); 11035 11036 if (DTRACEACT_ISPRINTFLIKE(kind)) { 11037 char *str = (char *)(uintptr_t)act->dtad_arg; 11038 11039 #ifdef illumos 11040 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 11041 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 11042 #endif 11043 11044 if (str != NULL) 11045 kmem_free(str, strlen(str) + 1); 11046 } 11047 11048 kmem_free(act, sizeof (dtrace_actdesc_t)); 11049 } 11050 11051 /* 11052 * DTrace ECB Functions 11053 */ 11054 static dtrace_ecb_t * 11055 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 11056 { 11057 dtrace_ecb_t *ecb; 11058 dtrace_epid_t epid; 11059 11060 ASSERT(MUTEX_HELD(&dtrace_lock)); 11061 11062 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 11063 ecb->dte_predicate = NULL; 11064 ecb->dte_probe = probe; 11065 11066 /* 11067 * The default size is the size of the default action: recording 11068 * the header. 11069 */ 11070 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 11071 ecb->dte_alignment = sizeof (dtrace_epid_t); 11072 11073 epid = state->dts_epid++; 11074 11075 if (epid - 1 >= state->dts_necbs) { 11076 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 11077 int necbs = state->dts_necbs << 1; 11078 11079 ASSERT(epid == state->dts_necbs + 1); 11080 11081 if (necbs == 0) { 11082 ASSERT(oecbs == NULL); 11083 necbs = 1; 11084 } 11085 11086 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 11087 11088 if (oecbs != NULL) 11089 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 11090 11091 dtrace_membar_producer(); 11092 state->dts_ecbs = ecbs; 11093 11094 if (oecbs != NULL) { 11095 /* 11096 * If this state is active, we must dtrace_sync() 11097 * before we can free the old dts_ecbs array: we're 11098 * coming in hot, and there may be active ring 11099 * buffer processing (which indexes into the dts_ecbs 11100 * array) on another CPU. 11101 */ 11102 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11103 dtrace_sync(); 11104 11105 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 11106 } 11107 11108 dtrace_membar_producer(); 11109 state->dts_necbs = necbs; 11110 } 11111 11112 ecb->dte_state = state; 11113 11114 ASSERT(state->dts_ecbs[epid - 1] == NULL); 11115 dtrace_membar_producer(); 11116 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 11117 11118 return (ecb); 11119 } 11120 11121 static void 11122 dtrace_ecb_enable(dtrace_ecb_t *ecb) 11123 { 11124 dtrace_probe_t *probe = ecb->dte_probe; 11125 11126 ASSERT(MUTEX_HELD(&cpu_lock)); 11127 ASSERT(MUTEX_HELD(&dtrace_lock)); 11128 ASSERT(ecb->dte_next == NULL); 11129 11130 if (probe == NULL) { 11131 /* 11132 * This is the NULL probe -- there's nothing to do. 11133 */ 11134 return; 11135 } 11136 11137 if (probe->dtpr_ecb == NULL) { 11138 dtrace_provider_t *prov = probe->dtpr_provider; 11139 11140 /* 11141 * We're the first ECB on this probe. 11142 */ 11143 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 11144 11145 if (ecb->dte_predicate != NULL) 11146 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 11147 11148 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 11149 probe->dtpr_id, probe->dtpr_arg); 11150 } else { 11151 /* 11152 * This probe is already active. Swing the last pointer to 11153 * point to the new ECB, and issue a dtrace_sync() to assure 11154 * that all CPUs have seen the change. 11155 */ 11156 ASSERT(probe->dtpr_ecb_last != NULL); 11157 probe->dtpr_ecb_last->dte_next = ecb; 11158 probe->dtpr_ecb_last = ecb; 11159 probe->dtpr_predcache = 0; 11160 11161 dtrace_sync(); 11162 } 11163 } 11164 11165 static int 11166 dtrace_ecb_resize(dtrace_ecb_t *ecb) 11167 { 11168 dtrace_action_t *act; 11169 uint32_t curneeded = UINT32_MAX; 11170 uint32_t aggbase = UINT32_MAX; 11171 11172 /* 11173 * If we record anything, we always record the dtrace_rechdr_t. (And 11174 * we always record it first.) 11175 */ 11176 ecb->dte_size = sizeof (dtrace_rechdr_t); 11177 ecb->dte_alignment = sizeof (dtrace_epid_t); 11178 11179 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11180 dtrace_recdesc_t *rec = &act->dta_rec; 11181 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 11182 11183 ecb->dte_alignment = MAX(ecb->dte_alignment, 11184 rec->dtrd_alignment); 11185 11186 if (DTRACEACT_ISAGG(act->dta_kind)) { 11187 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11188 11189 ASSERT(rec->dtrd_size != 0); 11190 ASSERT(agg->dtag_first != NULL); 11191 ASSERT(act->dta_prev->dta_intuple); 11192 ASSERT(aggbase != UINT32_MAX); 11193 ASSERT(curneeded != UINT32_MAX); 11194 11195 agg->dtag_base = aggbase; 11196 11197 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11198 rec->dtrd_offset = curneeded; 11199 if (curneeded + rec->dtrd_size < curneeded) 11200 return (EINVAL); 11201 curneeded += rec->dtrd_size; 11202 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 11203 11204 aggbase = UINT32_MAX; 11205 curneeded = UINT32_MAX; 11206 } else if (act->dta_intuple) { 11207 if (curneeded == UINT32_MAX) { 11208 /* 11209 * This is the first record in a tuple. Align 11210 * curneeded to be at offset 4 in an 8-byte 11211 * aligned block. 11212 */ 11213 ASSERT(act->dta_prev == NULL || 11214 !act->dta_prev->dta_intuple); 11215 ASSERT3U(aggbase, ==, UINT32_MAX); 11216 curneeded = P2PHASEUP(ecb->dte_size, 11217 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 11218 11219 aggbase = curneeded - sizeof (dtrace_aggid_t); 11220 ASSERT(IS_P2ALIGNED(aggbase, 11221 sizeof (uint64_t))); 11222 } 11223 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11224 rec->dtrd_offset = curneeded; 11225 if (curneeded + rec->dtrd_size < curneeded) 11226 return (EINVAL); 11227 curneeded += rec->dtrd_size; 11228 } else { 11229 /* tuples must be followed by an aggregation */ 11230 ASSERT(act->dta_prev == NULL || 11231 !act->dta_prev->dta_intuple); 11232 11233 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 11234 rec->dtrd_alignment); 11235 rec->dtrd_offset = ecb->dte_size; 11236 if (ecb->dte_size + rec->dtrd_size < ecb->dte_size) 11237 return (EINVAL); 11238 ecb->dte_size += rec->dtrd_size; 11239 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 11240 } 11241 } 11242 11243 if ((act = ecb->dte_action) != NULL && 11244 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 11245 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 11246 /* 11247 * If the size is still sizeof (dtrace_rechdr_t), then all 11248 * actions store no data; set the size to 0. 11249 */ 11250 ecb->dte_size = 0; 11251 } 11252 11253 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 11254 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 11255 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 11256 ecb->dte_needed); 11257 return (0); 11258 } 11259 11260 static dtrace_action_t * 11261 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11262 { 11263 dtrace_aggregation_t *agg; 11264 size_t size = sizeof (uint64_t); 11265 int ntuple = desc->dtad_ntuple; 11266 dtrace_action_t *act; 11267 dtrace_recdesc_t *frec; 11268 dtrace_aggid_t aggid; 11269 dtrace_state_t *state = ecb->dte_state; 11270 11271 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 11272 agg->dtag_ecb = ecb; 11273 11274 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 11275 11276 switch (desc->dtad_kind) { 11277 case DTRACEAGG_MIN: 11278 agg->dtag_initial = INT64_MAX; 11279 agg->dtag_aggregate = dtrace_aggregate_min; 11280 break; 11281 11282 case DTRACEAGG_MAX: 11283 agg->dtag_initial = INT64_MIN; 11284 agg->dtag_aggregate = dtrace_aggregate_max; 11285 break; 11286 11287 case DTRACEAGG_COUNT: 11288 agg->dtag_aggregate = dtrace_aggregate_count; 11289 break; 11290 11291 case DTRACEAGG_QUANTIZE: 11292 agg->dtag_aggregate = dtrace_aggregate_quantize; 11293 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 11294 sizeof (uint64_t); 11295 break; 11296 11297 case DTRACEAGG_LQUANTIZE: { 11298 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 11299 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 11300 11301 agg->dtag_initial = desc->dtad_arg; 11302 agg->dtag_aggregate = dtrace_aggregate_lquantize; 11303 11304 if (step == 0 || levels == 0) 11305 goto err; 11306 11307 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 11308 break; 11309 } 11310 11311 case DTRACEAGG_LLQUANTIZE: { 11312 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 11313 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 11314 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 11315 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 11316 int64_t v; 11317 11318 agg->dtag_initial = desc->dtad_arg; 11319 agg->dtag_aggregate = dtrace_aggregate_llquantize; 11320 11321 if (factor < 2 || low >= high || nsteps < factor) 11322 goto err; 11323 11324 /* 11325 * Now check that the number of steps evenly divides a power 11326 * of the factor. (This assures both integer bucket size and 11327 * linearity within each magnitude.) 11328 */ 11329 for (v = factor; v < nsteps; v *= factor) 11330 continue; 11331 11332 if ((v % nsteps) || (nsteps % factor)) 11333 goto err; 11334 11335 size = (dtrace_aggregate_llquantize_bucket(factor, 11336 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 11337 break; 11338 } 11339 11340 case DTRACEAGG_AVG: 11341 agg->dtag_aggregate = dtrace_aggregate_avg; 11342 size = sizeof (uint64_t) * 2; 11343 break; 11344 11345 case DTRACEAGG_STDDEV: 11346 agg->dtag_aggregate = dtrace_aggregate_stddev; 11347 size = sizeof (uint64_t) * 4; 11348 break; 11349 11350 case DTRACEAGG_SUM: 11351 agg->dtag_aggregate = dtrace_aggregate_sum; 11352 break; 11353 11354 default: 11355 goto err; 11356 } 11357 11358 agg->dtag_action.dta_rec.dtrd_size = size; 11359 11360 if (ntuple == 0) 11361 goto err; 11362 11363 /* 11364 * We must make sure that we have enough actions for the n-tuple. 11365 */ 11366 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 11367 if (DTRACEACT_ISAGG(act->dta_kind)) 11368 break; 11369 11370 if (--ntuple == 0) { 11371 /* 11372 * This is the action with which our n-tuple begins. 11373 */ 11374 agg->dtag_first = act; 11375 goto success; 11376 } 11377 } 11378 11379 /* 11380 * This n-tuple is short by ntuple elements. Return failure. 11381 */ 11382 ASSERT(ntuple != 0); 11383 err: 11384 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11385 return (NULL); 11386 11387 success: 11388 /* 11389 * If the last action in the tuple has a size of zero, it's actually 11390 * an expression argument for the aggregating action. 11391 */ 11392 ASSERT(ecb->dte_action_last != NULL); 11393 act = ecb->dte_action_last; 11394 11395 if (act->dta_kind == DTRACEACT_DIFEXPR) { 11396 ASSERT(act->dta_difo != NULL); 11397 11398 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 11399 agg->dtag_hasarg = 1; 11400 } 11401 11402 /* 11403 * We need to allocate an id for this aggregation. 11404 */ 11405 #ifdef illumos 11406 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 11407 VM_BESTFIT | VM_SLEEP); 11408 #else 11409 aggid = alloc_unr(state->dts_aggid_arena); 11410 #endif 11411 11412 if (aggid - 1 >= state->dts_naggregations) { 11413 dtrace_aggregation_t **oaggs = state->dts_aggregations; 11414 dtrace_aggregation_t **aggs; 11415 int naggs = state->dts_naggregations << 1; 11416 int onaggs = state->dts_naggregations; 11417 11418 ASSERT(aggid == state->dts_naggregations + 1); 11419 11420 if (naggs == 0) { 11421 ASSERT(oaggs == NULL); 11422 naggs = 1; 11423 } 11424 11425 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 11426 11427 if (oaggs != NULL) { 11428 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 11429 kmem_free(oaggs, onaggs * sizeof (*aggs)); 11430 } 11431 11432 state->dts_aggregations = aggs; 11433 state->dts_naggregations = naggs; 11434 } 11435 11436 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 11437 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 11438 11439 frec = &agg->dtag_first->dta_rec; 11440 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 11441 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 11442 11443 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 11444 ASSERT(!act->dta_intuple); 11445 act->dta_intuple = 1; 11446 } 11447 11448 return (&agg->dtag_action); 11449 } 11450 11451 static void 11452 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 11453 { 11454 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11455 dtrace_state_t *state = ecb->dte_state; 11456 dtrace_aggid_t aggid = agg->dtag_id; 11457 11458 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 11459 #ifdef illumos 11460 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 11461 #else 11462 free_unr(state->dts_aggid_arena, aggid); 11463 #endif 11464 11465 ASSERT(state->dts_aggregations[aggid - 1] == agg); 11466 state->dts_aggregations[aggid - 1] = NULL; 11467 11468 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11469 } 11470 11471 static int 11472 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11473 { 11474 dtrace_action_t *action, *last; 11475 dtrace_difo_t *dp = desc->dtad_difo; 11476 uint32_t size = 0, align = sizeof (uint8_t), mask; 11477 uint16_t format = 0; 11478 dtrace_recdesc_t *rec; 11479 dtrace_state_t *state = ecb->dte_state; 11480 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 11481 uint64_t arg = desc->dtad_arg; 11482 11483 ASSERT(MUTEX_HELD(&dtrace_lock)); 11484 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 11485 11486 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 11487 /* 11488 * If this is an aggregating action, there must be neither 11489 * a speculate nor a commit on the action chain. 11490 */ 11491 dtrace_action_t *act; 11492 11493 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11494 if (act->dta_kind == DTRACEACT_COMMIT) 11495 return (EINVAL); 11496 11497 if (act->dta_kind == DTRACEACT_SPECULATE) 11498 return (EINVAL); 11499 } 11500 11501 action = dtrace_ecb_aggregation_create(ecb, desc); 11502 11503 if (action == NULL) 11504 return (EINVAL); 11505 } else { 11506 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 11507 (desc->dtad_kind == DTRACEACT_DIFEXPR && 11508 dp != NULL && dp->dtdo_destructive)) { 11509 state->dts_destructive = 1; 11510 } 11511 11512 switch (desc->dtad_kind) { 11513 case DTRACEACT_PRINTF: 11514 case DTRACEACT_PRINTA: 11515 case DTRACEACT_SYSTEM: 11516 case DTRACEACT_FREOPEN: 11517 case DTRACEACT_DIFEXPR: 11518 /* 11519 * We know that our arg is a string -- turn it into a 11520 * format. 11521 */ 11522 if (arg == 0) { 11523 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 11524 desc->dtad_kind == DTRACEACT_DIFEXPR); 11525 format = 0; 11526 } else { 11527 ASSERT(arg != 0); 11528 #ifdef illumos 11529 ASSERT(arg > KERNELBASE); 11530 #endif 11531 format = dtrace_format_add(state, 11532 (char *)(uintptr_t)arg); 11533 } 11534 11535 /*FALLTHROUGH*/ 11536 case DTRACEACT_LIBACT: 11537 case DTRACEACT_TRACEMEM: 11538 case DTRACEACT_TRACEMEM_DYNSIZE: 11539 if (dp == NULL) 11540 return (EINVAL); 11541 11542 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 11543 break; 11544 11545 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 11546 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11547 return (EINVAL); 11548 11549 size = opt[DTRACEOPT_STRSIZE]; 11550 } 11551 11552 break; 11553 11554 case DTRACEACT_STACK: 11555 if ((nframes = arg) == 0) { 11556 nframes = opt[DTRACEOPT_STACKFRAMES]; 11557 ASSERT(nframes > 0); 11558 arg = nframes; 11559 } 11560 11561 size = nframes * sizeof (pc_t); 11562 break; 11563 11564 case DTRACEACT_JSTACK: 11565 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 11566 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 11567 11568 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 11569 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 11570 11571 arg = DTRACE_USTACK_ARG(nframes, strsize); 11572 11573 /*FALLTHROUGH*/ 11574 case DTRACEACT_USTACK: 11575 if (desc->dtad_kind != DTRACEACT_JSTACK && 11576 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 11577 strsize = DTRACE_USTACK_STRSIZE(arg); 11578 nframes = opt[DTRACEOPT_USTACKFRAMES]; 11579 ASSERT(nframes > 0); 11580 arg = DTRACE_USTACK_ARG(nframes, strsize); 11581 } 11582 11583 /* 11584 * Save a slot for the pid. 11585 */ 11586 size = (nframes + 1) * sizeof (uint64_t); 11587 size += DTRACE_USTACK_STRSIZE(arg); 11588 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 11589 11590 break; 11591 11592 case DTRACEACT_SYM: 11593 case DTRACEACT_MOD: 11594 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 11595 sizeof (uint64_t)) || 11596 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11597 return (EINVAL); 11598 break; 11599 11600 case DTRACEACT_USYM: 11601 case DTRACEACT_UMOD: 11602 case DTRACEACT_UADDR: 11603 if (dp == NULL || 11604 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 11605 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11606 return (EINVAL); 11607 11608 /* 11609 * We have a slot for the pid, plus a slot for the 11610 * argument. To keep things simple (aligned with 11611 * bitness-neutral sizing), we store each as a 64-bit 11612 * quantity. 11613 */ 11614 size = 2 * sizeof (uint64_t); 11615 break; 11616 11617 case DTRACEACT_STOP: 11618 case DTRACEACT_BREAKPOINT: 11619 case DTRACEACT_PANIC: 11620 break; 11621 11622 case DTRACEACT_CHILL: 11623 case DTRACEACT_DISCARD: 11624 case DTRACEACT_RAISE: 11625 if (dp == NULL) 11626 return (EINVAL); 11627 break; 11628 11629 case DTRACEACT_EXIT: 11630 if (dp == NULL || 11631 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 11632 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11633 return (EINVAL); 11634 break; 11635 11636 case DTRACEACT_SPECULATE: 11637 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 11638 return (EINVAL); 11639 11640 if (dp == NULL) 11641 return (EINVAL); 11642 11643 state->dts_speculates = 1; 11644 break; 11645 11646 case DTRACEACT_PRINTM: 11647 size = dp->dtdo_rtype.dtdt_size; 11648 break; 11649 11650 case DTRACEACT_PRINTT: 11651 size = dp->dtdo_rtype.dtdt_size; 11652 break; 11653 11654 case DTRACEACT_COMMIT: { 11655 dtrace_action_t *act = ecb->dte_action; 11656 11657 for (; act != NULL; act = act->dta_next) { 11658 if (act->dta_kind == DTRACEACT_COMMIT) 11659 return (EINVAL); 11660 } 11661 11662 if (dp == NULL) 11663 return (EINVAL); 11664 break; 11665 } 11666 11667 default: 11668 return (EINVAL); 11669 } 11670 11671 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 11672 /* 11673 * If this is a data-storing action or a speculate, 11674 * we must be sure that there isn't a commit on the 11675 * action chain. 11676 */ 11677 dtrace_action_t *act = ecb->dte_action; 11678 11679 for (; act != NULL; act = act->dta_next) { 11680 if (act->dta_kind == DTRACEACT_COMMIT) 11681 return (EINVAL); 11682 } 11683 } 11684 11685 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 11686 action->dta_rec.dtrd_size = size; 11687 } 11688 11689 action->dta_refcnt = 1; 11690 rec = &action->dta_rec; 11691 size = rec->dtrd_size; 11692 11693 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 11694 if (!(size & mask)) { 11695 align = mask + 1; 11696 break; 11697 } 11698 } 11699 11700 action->dta_kind = desc->dtad_kind; 11701 11702 if ((action->dta_difo = dp) != NULL) 11703 dtrace_difo_hold(dp); 11704 11705 rec->dtrd_action = action->dta_kind; 11706 rec->dtrd_arg = arg; 11707 rec->dtrd_uarg = desc->dtad_uarg; 11708 rec->dtrd_alignment = (uint16_t)align; 11709 rec->dtrd_format = format; 11710 11711 if ((last = ecb->dte_action_last) != NULL) { 11712 ASSERT(ecb->dte_action != NULL); 11713 action->dta_prev = last; 11714 last->dta_next = action; 11715 } else { 11716 ASSERT(ecb->dte_action == NULL); 11717 ecb->dte_action = action; 11718 } 11719 11720 ecb->dte_action_last = action; 11721 11722 return (0); 11723 } 11724 11725 static void 11726 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 11727 { 11728 dtrace_action_t *act = ecb->dte_action, *next; 11729 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 11730 dtrace_difo_t *dp; 11731 uint16_t format; 11732 11733 if (act != NULL && act->dta_refcnt > 1) { 11734 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 11735 act->dta_refcnt--; 11736 } else { 11737 for (; act != NULL; act = next) { 11738 next = act->dta_next; 11739 ASSERT(next != NULL || act == ecb->dte_action_last); 11740 ASSERT(act->dta_refcnt == 1); 11741 11742 if ((format = act->dta_rec.dtrd_format) != 0) 11743 dtrace_format_remove(ecb->dte_state, format); 11744 11745 if ((dp = act->dta_difo) != NULL) 11746 dtrace_difo_release(dp, vstate); 11747 11748 if (DTRACEACT_ISAGG(act->dta_kind)) { 11749 dtrace_ecb_aggregation_destroy(ecb, act); 11750 } else { 11751 kmem_free(act, sizeof (dtrace_action_t)); 11752 } 11753 } 11754 } 11755 11756 ecb->dte_action = NULL; 11757 ecb->dte_action_last = NULL; 11758 ecb->dte_size = 0; 11759 } 11760 11761 static void 11762 dtrace_ecb_disable(dtrace_ecb_t *ecb) 11763 { 11764 /* 11765 * We disable the ECB by removing it from its probe. 11766 */ 11767 dtrace_ecb_t *pecb, *prev = NULL; 11768 dtrace_probe_t *probe = ecb->dte_probe; 11769 11770 ASSERT(MUTEX_HELD(&dtrace_lock)); 11771 11772 if (probe == NULL) { 11773 /* 11774 * This is the NULL probe; there is nothing to disable. 11775 */ 11776 return; 11777 } 11778 11779 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 11780 if (pecb == ecb) 11781 break; 11782 prev = pecb; 11783 } 11784 11785 ASSERT(pecb != NULL); 11786 11787 if (prev == NULL) { 11788 probe->dtpr_ecb = ecb->dte_next; 11789 } else { 11790 prev->dte_next = ecb->dte_next; 11791 } 11792 11793 if (ecb == probe->dtpr_ecb_last) { 11794 ASSERT(ecb->dte_next == NULL); 11795 probe->dtpr_ecb_last = prev; 11796 } 11797 11798 /* 11799 * The ECB has been disconnected from the probe; now sync to assure 11800 * that all CPUs have seen the change before returning. 11801 */ 11802 dtrace_sync(); 11803 11804 if (probe->dtpr_ecb == NULL) { 11805 /* 11806 * That was the last ECB on the probe; clear the predicate 11807 * cache ID for the probe, disable it and sync one more time 11808 * to assure that we'll never hit it again. 11809 */ 11810 dtrace_provider_t *prov = probe->dtpr_provider; 11811 11812 ASSERT(ecb->dte_next == NULL); 11813 ASSERT(probe->dtpr_ecb_last == NULL); 11814 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 11815 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 11816 probe->dtpr_id, probe->dtpr_arg); 11817 dtrace_sync(); 11818 } else { 11819 /* 11820 * There is at least one ECB remaining on the probe. If there 11821 * is _exactly_ one, set the probe's predicate cache ID to be 11822 * the predicate cache ID of the remaining ECB. 11823 */ 11824 ASSERT(probe->dtpr_ecb_last != NULL); 11825 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 11826 11827 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 11828 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 11829 11830 ASSERT(probe->dtpr_ecb->dte_next == NULL); 11831 11832 if (p != NULL) 11833 probe->dtpr_predcache = p->dtp_cacheid; 11834 } 11835 11836 ecb->dte_next = NULL; 11837 } 11838 } 11839 11840 static void 11841 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 11842 { 11843 dtrace_state_t *state = ecb->dte_state; 11844 dtrace_vstate_t *vstate = &state->dts_vstate; 11845 dtrace_predicate_t *pred; 11846 dtrace_epid_t epid = ecb->dte_epid; 11847 11848 ASSERT(MUTEX_HELD(&dtrace_lock)); 11849 ASSERT(ecb->dte_next == NULL); 11850 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 11851 11852 if ((pred = ecb->dte_predicate) != NULL) 11853 dtrace_predicate_release(pred, vstate); 11854 11855 dtrace_ecb_action_remove(ecb); 11856 11857 ASSERT(state->dts_ecbs[epid - 1] == ecb); 11858 state->dts_ecbs[epid - 1] = NULL; 11859 11860 kmem_free(ecb, sizeof (dtrace_ecb_t)); 11861 } 11862 11863 static dtrace_ecb_t * 11864 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 11865 dtrace_enabling_t *enab) 11866 { 11867 dtrace_ecb_t *ecb; 11868 dtrace_predicate_t *pred; 11869 dtrace_actdesc_t *act; 11870 dtrace_provider_t *prov; 11871 dtrace_ecbdesc_t *desc = enab->dten_current; 11872 11873 ASSERT(MUTEX_HELD(&dtrace_lock)); 11874 ASSERT(state != NULL); 11875 11876 ecb = dtrace_ecb_add(state, probe); 11877 ecb->dte_uarg = desc->dted_uarg; 11878 11879 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 11880 dtrace_predicate_hold(pred); 11881 ecb->dte_predicate = pred; 11882 } 11883 11884 if (probe != NULL) { 11885 /* 11886 * If the provider shows more leg than the consumer is old 11887 * enough to see, we need to enable the appropriate implicit 11888 * predicate bits to prevent the ecb from activating at 11889 * revealing times. 11890 * 11891 * Providers specifying DTRACE_PRIV_USER at register time 11892 * are stating that they need the /proc-style privilege 11893 * model to be enforced, and this is what DTRACE_COND_OWNER 11894 * and DTRACE_COND_ZONEOWNER will then do at probe time. 11895 */ 11896 prov = probe->dtpr_provider; 11897 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 11898 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11899 ecb->dte_cond |= DTRACE_COND_OWNER; 11900 11901 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 11902 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11903 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 11904 11905 /* 11906 * If the provider shows us kernel innards and the user 11907 * is lacking sufficient privilege, enable the 11908 * DTRACE_COND_USERMODE implicit predicate. 11909 */ 11910 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 11911 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 11912 ecb->dte_cond |= DTRACE_COND_USERMODE; 11913 } 11914 11915 if (dtrace_ecb_create_cache != NULL) { 11916 /* 11917 * If we have a cached ecb, we'll use its action list instead 11918 * of creating our own (saving both time and space). 11919 */ 11920 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 11921 dtrace_action_t *act = cached->dte_action; 11922 11923 if (act != NULL) { 11924 ASSERT(act->dta_refcnt > 0); 11925 act->dta_refcnt++; 11926 ecb->dte_action = act; 11927 ecb->dte_action_last = cached->dte_action_last; 11928 ecb->dte_needed = cached->dte_needed; 11929 ecb->dte_size = cached->dte_size; 11930 ecb->dte_alignment = cached->dte_alignment; 11931 } 11932 11933 return (ecb); 11934 } 11935 11936 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 11937 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 11938 dtrace_ecb_destroy(ecb); 11939 return (NULL); 11940 } 11941 } 11942 11943 if ((enab->dten_error = dtrace_ecb_resize(ecb)) != 0) { 11944 dtrace_ecb_destroy(ecb); 11945 return (NULL); 11946 } 11947 11948 return (dtrace_ecb_create_cache = ecb); 11949 } 11950 11951 static int 11952 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 11953 { 11954 dtrace_ecb_t *ecb; 11955 dtrace_enabling_t *enab = arg; 11956 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 11957 11958 ASSERT(state != NULL); 11959 11960 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 11961 /* 11962 * This probe was created in a generation for which this 11963 * enabling has previously created ECBs; we don't want to 11964 * enable it again, so just kick out. 11965 */ 11966 return (DTRACE_MATCH_NEXT); 11967 } 11968 11969 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 11970 return (DTRACE_MATCH_DONE); 11971 11972 dtrace_ecb_enable(ecb); 11973 return (DTRACE_MATCH_NEXT); 11974 } 11975 11976 static dtrace_ecb_t * 11977 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 11978 { 11979 dtrace_ecb_t *ecb; 11980 11981 ASSERT(MUTEX_HELD(&dtrace_lock)); 11982 11983 if (id == 0 || id > state->dts_necbs) 11984 return (NULL); 11985 11986 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 11987 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 11988 11989 return (state->dts_ecbs[id - 1]); 11990 } 11991 11992 static dtrace_aggregation_t * 11993 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 11994 { 11995 dtrace_aggregation_t *agg; 11996 11997 ASSERT(MUTEX_HELD(&dtrace_lock)); 11998 11999 if (id == 0 || id > state->dts_naggregations) 12000 return (NULL); 12001 12002 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 12003 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 12004 agg->dtag_id == id); 12005 12006 return (state->dts_aggregations[id - 1]); 12007 } 12008 12009 /* 12010 * DTrace Buffer Functions 12011 * 12012 * The following functions manipulate DTrace buffers. Most of these functions 12013 * are called in the context of establishing or processing consumer state; 12014 * exceptions are explicitly noted. 12015 */ 12016 12017 /* 12018 * Note: called from cross call context. This function switches the two 12019 * buffers on a given CPU. The atomicity of this operation is assured by 12020 * disabling interrupts while the actual switch takes place; the disabling of 12021 * interrupts serializes the execution with any execution of dtrace_probe() on 12022 * the same CPU. 12023 */ 12024 static void 12025 dtrace_buffer_switch(dtrace_buffer_t *buf) 12026 { 12027 caddr_t tomax = buf->dtb_tomax; 12028 caddr_t xamot = buf->dtb_xamot; 12029 dtrace_icookie_t cookie; 12030 hrtime_t now; 12031 12032 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 12033 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 12034 12035 cookie = dtrace_interrupt_disable(); 12036 now = dtrace_gethrtime(); 12037 buf->dtb_tomax = xamot; 12038 buf->dtb_xamot = tomax; 12039 buf->dtb_xamot_drops = buf->dtb_drops; 12040 buf->dtb_xamot_offset = buf->dtb_offset; 12041 buf->dtb_xamot_errors = buf->dtb_errors; 12042 buf->dtb_xamot_flags = buf->dtb_flags; 12043 buf->dtb_offset = 0; 12044 buf->dtb_drops = 0; 12045 buf->dtb_errors = 0; 12046 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 12047 buf->dtb_interval = now - buf->dtb_switched; 12048 buf->dtb_switched = now; 12049 dtrace_interrupt_enable(cookie); 12050 } 12051 12052 /* 12053 * Note: called from cross call context. This function activates a buffer 12054 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 12055 * is guaranteed by the disabling of interrupts. 12056 */ 12057 static void 12058 dtrace_buffer_activate(dtrace_state_t *state) 12059 { 12060 dtrace_buffer_t *buf; 12061 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 12062 12063 buf = &state->dts_buffer[curcpu]; 12064 12065 if (buf->dtb_tomax != NULL) { 12066 /* 12067 * We might like to assert that the buffer is marked inactive, 12068 * but this isn't necessarily true: the buffer for the CPU 12069 * that processes the BEGIN probe has its buffer activated 12070 * manually. In this case, we take the (harmless) action 12071 * re-clearing the bit INACTIVE bit. 12072 */ 12073 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 12074 } 12075 12076 dtrace_interrupt_enable(cookie); 12077 } 12078 12079 #ifdef __FreeBSD__ 12080 /* 12081 * Activate the specified per-CPU buffer. This is used instead of 12082 * dtrace_buffer_activate() when APs have not yet started, i.e. when 12083 * activating anonymous state. 12084 */ 12085 static void 12086 dtrace_buffer_activate_cpu(dtrace_state_t *state, int cpu) 12087 { 12088 12089 if (state->dts_buffer[cpu].dtb_tomax != NULL) 12090 state->dts_buffer[cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 12091 } 12092 #endif 12093 12094 static int 12095 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 12096 processorid_t cpu, int *factor) 12097 { 12098 #ifdef illumos 12099 cpu_t *cp; 12100 #endif 12101 dtrace_buffer_t *buf; 12102 int allocated = 0, desired = 0; 12103 12104 #ifdef illumos 12105 ASSERT(MUTEX_HELD(&cpu_lock)); 12106 ASSERT(MUTEX_HELD(&dtrace_lock)); 12107 12108 *factor = 1; 12109 12110 if (size > dtrace_nonroot_maxsize && 12111 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 12112 return (EFBIG); 12113 12114 cp = cpu_list; 12115 12116 do { 12117 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 12118 continue; 12119 12120 buf = &bufs[cp->cpu_id]; 12121 12122 /* 12123 * If there is already a buffer allocated for this CPU, it 12124 * is only possible that this is a DR event. In this case, 12125 */ 12126 if (buf->dtb_tomax != NULL) { 12127 ASSERT(buf->dtb_size == size); 12128 continue; 12129 } 12130 12131 ASSERT(buf->dtb_xamot == NULL); 12132 12133 if ((buf->dtb_tomax = kmem_zalloc(size, 12134 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12135 goto err; 12136 12137 buf->dtb_size = size; 12138 buf->dtb_flags = flags; 12139 buf->dtb_offset = 0; 12140 buf->dtb_drops = 0; 12141 12142 if (flags & DTRACEBUF_NOSWITCH) 12143 continue; 12144 12145 if ((buf->dtb_xamot = kmem_zalloc(size, 12146 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12147 goto err; 12148 } while ((cp = cp->cpu_next) != cpu_list); 12149 12150 return (0); 12151 12152 err: 12153 cp = cpu_list; 12154 12155 do { 12156 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 12157 continue; 12158 12159 buf = &bufs[cp->cpu_id]; 12160 desired += 2; 12161 12162 if (buf->dtb_xamot != NULL) { 12163 ASSERT(buf->dtb_tomax != NULL); 12164 ASSERT(buf->dtb_size == size); 12165 kmem_free(buf->dtb_xamot, size); 12166 allocated++; 12167 } 12168 12169 if (buf->dtb_tomax != NULL) { 12170 ASSERT(buf->dtb_size == size); 12171 kmem_free(buf->dtb_tomax, size); 12172 allocated++; 12173 } 12174 12175 buf->dtb_tomax = NULL; 12176 buf->dtb_xamot = NULL; 12177 buf->dtb_size = 0; 12178 } while ((cp = cp->cpu_next) != cpu_list); 12179 #else 12180 int i; 12181 12182 *factor = 1; 12183 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \ 12184 defined(__mips__) || defined(__powerpc__) || defined(__riscv__) 12185 /* 12186 * FreeBSD isn't good at limiting the amount of memory we 12187 * ask to malloc, so let's place a limit here before trying 12188 * to do something that might well end in tears at bedtime. 12189 */ 12190 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 12191 return (ENOMEM); 12192 #endif 12193 12194 ASSERT(MUTEX_HELD(&dtrace_lock)); 12195 CPU_FOREACH(i) { 12196 if (cpu != DTRACE_CPUALL && cpu != i) 12197 continue; 12198 12199 buf = &bufs[i]; 12200 12201 /* 12202 * If there is already a buffer allocated for this CPU, it 12203 * is only possible that this is a DR event. In this case, 12204 * the buffer size must match our specified size. 12205 */ 12206 if (buf->dtb_tomax != NULL) { 12207 ASSERT(buf->dtb_size == size); 12208 continue; 12209 } 12210 12211 ASSERT(buf->dtb_xamot == NULL); 12212 12213 if ((buf->dtb_tomax = kmem_zalloc(size, 12214 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12215 goto err; 12216 12217 buf->dtb_size = size; 12218 buf->dtb_flags = flags; 12219 buf->dtb_offset = 0; 12220 buf->dtb_drops = 0; 12221 12222 if (flags & DTRACEBUF_NOSWITCH) 12223 continue; 12224 12225 if ((buf->dtb_xamot = kmem_zalloc(size, 12226 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12227 goto err; 12228 } 12229 12230 return (0); 12231 12232 err: 12233 /* 12234 * Error allocating memory, so free the buffers that were 12235 * allocated before the failed allocation. 12236 */ 12237 CPU_FOREACH(i) { 12238 if (cpu != DTRACE_CPUALL && cpu != i) 12239 continue; 12240 12241 buf = &bufs[i]; 12242 desired += 2; 12243 12244 if (buf->dtb_xamot != NULL) { 12245 ASSERT(buf->dtb_tomax != NULL); 12246 ASSERT(buf->dtb_size == size); 12247 kmem_free(buf->dtb_xamot, size); 12248 allocated++; 12249 } 12250 12251 if (buf->dtb_tomax != NULL) { 12252 ASSERT(buf->dtb_size == size); 12253 kmem_free(buf->dtb_tomax, size); 12254 allocated++; 12255 } 12256 12257 buf->dtb_tomax = NULL; 12258 buf->dtb_xamot = NULL; 12259 buf->dtb_size = 0; 12260 12261 } 12262 #endif 12263 *factor = desired / (allocated > 0 ? allocated : 1); 12264 12265 return (ENOMEM); 12266 } 12267 12268 /* 12269 * Note: called from probe context. This function just increments the drop 12270 * count on a buffer. It has been made a function to allow for the 12271 * possibility of understanding the source of mysterious drop counts. (A 12272 * problem for which one may be particularly disappointed that DTrace cannot 12273 * be used to understand DTrace.) 12274 */ 12275 static void 12276 dtrace_buffer_drop(dtrace_buffer_t *buf) 12277 { 12278 buf->dtb_drops++; 12279 } 12280 12281 /* 12282 * Note: called from probe context. This function is called to reserve space 12283 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 12284 * mstate. Returns the new offset in the buffer, or a negative value if an 12285 * error has occurred. 12286 */ 12287 static intptr_t 12288 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 12289 dtrace_state_t *state, dtrace_mstate_t *mstate) 12290 { 12291 intptr_t offs = buf->dtb_offset, soffs; 12292 intptr_t woffs; 12293 caddr_t tomax; 12294 size_t total; 12295 12296 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 12297 return (-1); 12298 12299 if ((tomax = buf->dtb_tomax) == NULL) { 12300 dtrace_buffer_drop(buf); 12301 return (-1); 12302 } 12303 12304 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 12305 while (offs & (align - 1)) { 12306 /* 12307 * Assert that our alignment is off by a number which 12308 * is itself sizeof (uint32_t) aligned. 12309 */ 12310 ASSERT(!((align - (offs & (align - 1))) & 12311 (sizeof (uint32_t) - 1))); 12312 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12313 offs += sizeof (uint32_t); 12314 } 12315 12316 if ((soffs = offs + needed) > buf->dtb_size) { 12317 dtrace_buffer_drop(buf); 12318 return (-1); 12319 } 12320 12321 if (mstate == NULL) 12322 return (offs); 12323 12324 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 12325 mstate->dtms_scratch_size = buf->dtb_size - soffs; 12326 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12327 12328 return (offs); 12329 } 12330 12331 if (buf->dtb_flags & DTRACEBUF_FILL) { 12332 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 12333 (buf->dtb_flags & DTRACEBUF_FULL)) 12334 return (-1); 12335 goto out; 12336 } 12337 12338 total = needed + (offs & (align - 1)); 12339 12340 /* 12341 * For a ring buffer, life is quite a bit more complicated. Before 12342 * we can store any padding, we need to adjust our wrapping offset. 12343 * (If we've never before wrapped or we're not about to, no adjustment 12344 * is required.) 12345 */ 12346 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 12347 offs + total > buf->dtb_size) { 12348 woffs = buf->dtb_xamot_offset; 12349 12350 if (offs + total > buf->dtb_size) { 12351 /* 12352 * We can't fit in the end of the buffer. First, a 12353 * sanity check that we can fit in the buffer at all. 12354 */ 12355 if (total > buf->dtb_size) { 12356 dtrace_buffer_drop(buf); 12357 return (-1); 12358 } 12359 12360 /* 12361 * We're going to be storing at the top of the buffer, 12362 * so now we need to deal with the wrapped offset. We 12363 * only reset our wrapped offset to 0 if it is 12364 * currently greater than the current offset. If it 12365 * is less than the current offset, it is because a 12366 * previous allocation induced a wrap -- but the 12367 * allocation didn't subsequently take the space due 12368 * to an error or false predicate evaluation. In this 12369 * case, we'll just leave the wrapped offset alone: if 12370 * the wrapped offset hasn't been advanced far enough 12371 * for this allocation, it will be adjusted in the 12372 * lower loop. 12373 */ 12374 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 12375 if (woffs >= offs) 12376 woffs = 0; 12377 } else { 12378 woffs = 0; 12379 } 12380 12381 /* 12382 * Now we know that we're going to be storing to the 12383 * top of the buffer and that there is room for us 12384 * there. We need to clear the buffer from the current 12385 * offset to the end (there may be old gunk there). 12386 */ 12387 while (offs < buf->dtb_size) 12388 tomax[offs++] = 0; 12389 12390 /* 12391 * We need to set our offset to zero. And because we 12392 * are wrapping, we need to set the bit indicating as 12393 * much. We can also adjust our needed space back 12394 * down to the space required by the ECB -- we know 12395 * that the top of the buffer is aligned. 12396 */ 12397 offs = 0; 12398 total = needed; 12399 buf->dtb_flags |= DTRACEBUF_WRAPPED; 12400 } else { 12401 /* 12402 * There is room for us in the buffer, so we simply 12403 * need to check the wrapped offset. 12404 */ 12405 if (woffs < offs) { 12406 /* 12407 * The wrapped offset is less than the offset. 12408 * This can happen if we allocated buffer space 12409 * that induced a wrap, but then we didn't 12410 * subsequently take the space due to an error 12411 * or false predicate evaluation. This is 12412 * okay; we know that _this_ allocation isn't 12413 * going to induce a wrap. We still can't 12414 * reset the wrapped offset to be zero, 12415 * however: the space may have been trashed in 12416 * the previous failed probe attempt. But at 12417 * least the wrapped offset doesn't need to 12418 * be adjusted at all... 12419 */ 12420 goto out; 12421 } 12422 } 12423 12424 while (offs + total > woffs) { 12425 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 12426 size_t size; 12427 12428 if (epid == DTRACE_EPIDNONE) { 12429 size = sizeof (uint32_t); 12430 } else { 12431 ASSERT3U(epid, <=, state->dts_necbs); 12432 ASSERT(state->dts_ecbs[epid - 1] != NULL); 12433 12434 size = state->dts_ecbs[epid - 1]->dte_size; 12435 } 12436 12437 ASSERT(woffs + size <= buf->dtb_size); 12438 ASSERT(size != 0); 12439 12440 if (woffs + size == buf->dtb_size) { 12441 /* 12442 * We've reached the end of the buffer; we want 12443 * to set the wrapped offset to 0 and break 12444 * out. However, if the offs is 0, then we're 12445 * in a strange edge-condition: the amount of 12446 * space that we want to reserve plus the size 12447 * of the record that we're overwriting is 12448 * greater than the size of the buffer. This 12449 * is problematic because if we reserve the 12450 * space but subsequently don't consume it (due 12451 * to a failed predicate or error) the wrapped 12452 * offset will be 0 -- yet the EPID at offset 0 12453 * will not be committed. This situation is 12454 * relatively easy to deal with: if we're in 12455 * this case, the buffer is indistinguishable 12456 * from one that hasn't wrapped; we need only 12457 * finish the job by clearing the wrapped bit, 12458 * explicitly setting the offset to be 0, and 12459 * zero'ing out the old data in the buffer. 12460 */ 12461 if (offs == 0) { 12462 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 12463 buf->dtb_offset = 0; 12464 woffs = total; 12465 12466 while (woffs < buf->dtb_size) 12467 tomax[woffs++] = 0; 12468 } 12469 12470 woffs = 0; 12471 break; 12472 } 12473 12474 woffs += size; 12475 } 12476 12477 /* 12478 * We have a wrapped offset. It may be that the wrapped offset 12479 * has become zero -- that's okay. 12480 */ 12481 buf->dtb_xamot_offset = woffs; 12482 } 12483 12484 out: 12485 /* 12486 * Now we can plow the buffer with any necessary padding. 12487 */ 12488 while (offs & (align - 1)) { 12489 /* 12490 * Assert that our alignment is off by a number which 12491 * is itself sizeof (uint32_t) aligned. 12492 */ 12493 ASSERT(!((align - (offs & (align - 1))) & 12494 (sizeof (uint32_t) - 1))); 12495 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12496 offs += sizeof (uint32_t); 12497 } 12498 12499 if (buf->dtb_flags & DTRACEBUF_FILL) { 12500 if (offs + needed > buf->dtb_size - state->dts_reserve) { 12501 buf->dtb_flags |= DTRACEBUF_FULL; 12502 return (-1); 12503 } 12504 } 12505 12506 if (mstate == NULL) 12507 return (offs); 12508 12509 /* 12510 * For ring buffers and fill buffers, the scratch space is always 12511 * the inactive buffer. 12512 */ 12513 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 12514 mstate->dtms_scratch_size = buf->dtb_size; 12515 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12516 12517 return (offs); 12518 } 12519 12520 static void 12521 dtrace_buffer_polish(dtrace_buffer_t *buf) 12522 { 12523 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 12524 ASSERT(MUTEX_HELD(&dtrace_lock)); 12525 12526 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 12527 return; 12528 12529 /* 12530 * We need to polish the ring buffer. There are three cases: 12531 * 12532 * - The first (and presumably most common) is that there is no gap 12533 * between the buffer offset and the wrapped offset. In this case, 12534 * there is nothing in the buffer that isn't valid data; we can 12535 * mark the buffer as polished and return. 12536 * 12537 * - The second (less common than the first but still more common 12538 * than the third) is that there is a gap between the buffer offset 12539 * and the wrapped offset, and the wrapped offset is larger than the 12540 * buffer offset. This can happen because of an alignment issue, or 12541 * can happen because of a call to dtrace_buffer_reserve() that 12542 * didn't subsequently consume the buffer space. In this case, 12543 * we need to zero the data from the buffer offset to the wrapped 12544 * offset. 12545 * 12546 * - The third (and least common) is that there is a gap between the 12547 * buffer offset and the wrapped offset, but the wrapped offset is 12548 * _less_ than the buffer offset. This can only happen because a 12549 * call to dtrace_buffer_reserve() induced a wrap, but the space 12550 * was not subsequently consumed. In this case, we need to zero the 12551 * space from the offset to the end of the buffer _and_ from the 12552 * top of the buffer to the wrapped offset. 12553 */ 12554 if (buf->dtb_offset < buf->dtb_xamot_offset) { 12555 bzero(buf->dtb_tomax + buf->dtb_offset, 12556 buf->dtb_xamot_offset - buf->dtb_offset); 12557 } 12558 12559 if (buf->dtb_offset > buf->dtb_xamot_offset) { 12560 bzero(buf->dtb_tomax + buf->dtb_offset, 12561 buf->dtb_size - buf->dtb_offset); 12562 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 12563 } 12564 } 12565 12566 /* 12567 * This routine determines if data generated at the specified time has likely 12568 * been entirely consumed at user-level. This routine is called to determine 12569 * if an ECB on a defunct probe (but for an active enabling) can be safely 12570 * disabled and destroyed. 12571 */ 12572 static int 12573 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 12574 { 12575 int i; 12576 12577 for (i = 0; i < NCPU; i++) { 12578 dtrace_buffer_t *buf = &bufs[i]; 12579 12580 if (buf->dtb_size == 0) 12581 continue; 12582 12583 if (buf->dtb_flags & DTRACEBUF_RING) 12584 return (0); 12585 12586 if (!buf->dtb_switched && buf->dtb_offset != 0) 12587 return (0); 12588 12589 if (buf->dtb_switched - buf->dtb_interval < when) 12590 return (0); 12591 } 12592 12593 return (1); 12594 } 12595 12596 static void 12597 dtrace_buffer_free(dtrace_buffer_t *bufs) 12598 { 12599 int i; 12600 12601 for (i = 0; i < NCPU; i++) { 12602 dtrace_buffer_t *buf = &bufs[i]; 12603 12604 if (buf->dtb_tomax == NULL) { 12605 ASSERT(buf->dtb_xamot == NULL); 12606 ASSERT(buf->dtb_size == 0); 12607 continue; 12608 } 12609 12610 if (buf->dtb_xamot != NULL) { 12611 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 12612 kmem_free(buf->dtb_xamot, buf->dtb_size); 12613 } 12614 12615 kmem_free(buf->dtb_tomax, buf->dtb_size); 12616 buf->dtb_size = 0; 12617 buf->dtb_tomax = NULL; 12618 buf->dtb_xamot = NULL; 12619 } 12620 } 12621 12622 /* 12623 * DTrace Enabling Functions 12624 */ 12625 static dtrace_enabling_t * 12626 dtrace_enabling_create(dtrace_vstate_t *vstate) 12627 { 12628 dtrace_enabling_t *enab; 12629 12630 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 12631 enab->dten_vstate = vstate; 12632 12633 return (enab); 12634 } 12635 12636 static void 12637 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 12638 { 12639 dtrace_ecbdesc_t **ndesc; 12640 size_t osize, nsize; 12641 12642 /* 12643 * We can't add to enablings after we've enabled them, or after we've 12644 * retained them. 12645 */ 12646 ASSERT(enab->dten_probegen == 0); 12647 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12648 12649 if (enab->dten_ndesc < enab->dten_maxdesc) { 12650 enab->dten_desc[enab->dten_ndesc++] = ecb; 12651 return; 12652 } 12653 12654 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12655 12656 if (enab->dten_maxdesc == 0) { 12657 enab->dten_maxdesc = 1; 12658 } else { 12659 enab->dten_maxdesc <<= 1; 12660 } 12661 12662 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 12663 12664 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12665 ndesc = kmem_zalloc(nsize, KM_SLEEP); 12666 bcopy(enab->dten_desc, ndesc, osize); 12667 if (enab->dten_desc != NULL) 12668 kmem_free(enab->dten_desc, osize); 12669 12670 enab->dten_desc = ndesc; 12671 enab->dten_desc[enab->dten_ndesc++] = ecb; 12672 } 12673 12674 static void 12675 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 12676 dtrace_probedesc_t *pd) 12677 { 12678 dtrace_ecbdesc_t *new; 12679 dtrace_predicate_t *pred; 12680 dtrace_actdesc_t *act; 12681 12682 /* 12683 * We're going to create a new ECB description that matches the 12684 * specified ECB in every way, but has the specified probe description. 12685 */ 12686 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12687 12688 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 12689 dtrace_predicate_hold(pred); 12690 12691 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 12692 dtrace_actdesc_hold(act); 12693 12694 new->dted_action = ecb->dted_action; 12695 new->dted_pred = ecb->dted_pred; 12696 new->dted_probe = *pd; 12697 new->dted_uarg = ecb->dted_uarg; 12698 12699 dtrace_enabling_add(enab, new); 12700 } 12701 12702 static void 12703 dtrace_enabling_dump(dtrace_enabling_t *enab) 12704 { 12705 int i; 12706 12707 for (i = 0; i < enab->dten_ndesc; i++) { 12708 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 12709 12710 #ifdef __FreeBSD__ 12711 printf("dtrace: enabling probe %d (%s:%s:%s:%s)\n", i, 12712 desc->dtpd_provider, desc->dtpd_mod, 12713 desc->dtpd_func, desc->dtpd_name); 12714 #else 12715 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 12716 desc->dtpd_provider, desc->dtpd_mod, 12717 desc->dtpd_func, desc->dtpd_name); 12718 #endif 12719 } 12720 } 12721 12722 static void 12723 dtrace_enabling_destroy(dtrace_enabling_t *enab) 12724 { 12725 int i; 12726 dtrace_ecbdesc_t *ep; 12727 dtrace_vstate_t *vstate = enab->dten_vstate; 12728 12729 ASSERT(MUTEX_HELD(&dtrace_lock)); 12730 12731 for (i = 0; i < enab->dten_ndesc; i++) { 12732 dtrace_actdesc_t *act, *next; 12733 dtrace_predicate_t *pred; 12734 12735 ep = enab->dten_desc[i]; 12736 12737 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 12738 dtrace_predicate_release(pred, vstate); 12739 12740 for (act = ep->dted_action; act != NULL; act = next) { 12741 next = act->dtad_next; 12742 dtrace_actdesc_release(act, vstate); 12743 } 12744 12745 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12746 } 12747 12748 if (enab->dten_desc != NULL) 12749 kmem_free(enab->dten_desc, 12750 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 12751 12752 /* 12753 * If this was a retained enabling, decrement the dts_nretained count 12754 * and take it off of the dtrace_retained list. 12755 */ 12756 if (enab->dten_prev != NULL || enab->dten_next != NULL || 12757 dtrace_retained == enab) { 12758 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12759 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 12760 enab->dten_vstate->dtvs_state->dts_nretained--; 12761 dtrace_retained_gen++; 12762 } 12763 12764 if (enab->dten_prev == NULL) { 12765 if (dtrace_retained == enab) { 12766 dtrace_retained = enab->dten_next; 12767 12768 if (dtrace_retained != NULL) 12769 dtrace_retained->dten_prev = NULL; 12770 } 12771 } else { 12772 ASSERT(enab != dtrace_retained); 12773 ASSERT(dtrace_retained != NULL); 12774 enab->dten_prev->dten_next = enab->dten_next; 12775 } 12776 12777 if (enab->dten_next != NULL) { 12778 ASSERT(dtrace_retained != NULL); 12779 enab->dten_next->dten_prev = enab->dten_prev; 12780 } 12781 12782 kmem_free(enab, sizeof (dtrace_enabling_t)); 12783 } 12784 12785 static int 12786 dtrace_enabling_retain(dtrace_enabling_t *enab) 12787 { 12788 dtrace_state_t *state; 12789 12790 ASSERT(MUTEX_HELD(&dtrace_lock)); 12791 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12792 ASSERT(enab->dten_vstate != NULL); 12793 12794 state = enab->dten_vstate->dtvs_state; 12795 ASSERT(state != NULL); 12796 12797 /* 12798 * We only allow each state to retain dtrace_retain_max enablings. 12799 */ 12800 if (state->dts_nretained >= dtrace_retain_max) 12801 return (ENOSPC); 12802 12803 state->dts_nretained++; 12804 dtrace_retained_gen++; 12805 12806 if (dtrace_retained == NULL) { 12807 dtrace_retained = enab; 12808 return (0); 12809 } 12810 12811 enab->dten_next = dtrace_retained; 12812 dtrace_retained->dten_prev = enab; 12813 dtrace_retained = enab; 12814 12815 return (0); 12816 } 12817 12818 static int 12819 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 12820 dtrace_probedesc_t *create) 12821 { 12822 dtrace_enabling_t *new, *enab; 12823 int found = 0, err = ENOENT; 12824 12825 ASSERT(MUTEX_HELD(&dtrace_lock)); 12826 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 12827 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 12828 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 12829 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 12830 12831 new = dtrace_enabling_create(&state->dts_vstate); 12832 12833 /* 12834 * Iterate over all retained enablings, looking for enablings that 12835 * match the specified state. 12836 */ 12837 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12838 int i; 12839 12840 /* 12841 * dtvs_state can only be NULL for helper enablings -- and 12842 * helper enablings can't be retained. 12843 */ 12844 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12845 12846 if (enab->dten_vstate->dtvs_state != state) 12847 continue; 12848 12849 /* 12850 * Now iterate over each probe description; we're looking for 12851 * an exact match to the specified probe description. 12852 */ 12853 for (i = 0; i < enab->dten_ndesc; i++) { 12854 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12855 dtrace_probedesc_t *pd = &ep->dted_probe; 12856 12857 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 12858 continue; 12859 12860 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 12861 continue; 12862 12863 if (strcmp(pd->dtpd_func, match->dtpd_func)) 12864 continue; 12865 12866 if (strcmp(pd->dtpd_name, match->dtpd_name)) 12867 continue; 12868 12869 /* 12870 * We have a winning probe! Add it to our growing 12871 * enabling. 12872 */ 12873 found = 1; 12874 dtrace_enabling_addlike(new, ep, create); 12875 } 12876 } 12877 12878 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 12879 dtrace_enabling_destroy(new); 12880 return (err); 12881 } 12882 12883 return (0); 12884 } 12885 12886 static void 12887 dtrace_enabling_retract(dtrace_state_t *state) 12888 { 12889 dtrace_enabling_t *enab, *next; 12890 12891 ASSERT(MUTEX_HELD(&dtrace_lock)); 12892 12893 /* 12894 * Iterate over all retained enablings, destroy the enablings retained 12895 * for the specified state. 12896 */ 12897 for (enab = dtrace_retained; enab != NULL; enab = next) { 12898 next = enab->dten_next; 12899 12900 /* 12901 * dtvs_state can only be NULL for helper enablings -- and 12902 * helper enablings can't be retained. 12903 */ 12904 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12905 12906 if (enab->dten_vstate->dtvs_state == state) { 12907 ASSERT(state->dts_nretained > 0); 12908 dtrace_enabling_destroy(enab); 12909 } 12910 } 12911 12912 ASSERT(state->dts_nretained == 0); 12913 } 12914 12915 static int 12916 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 12917 { 12918 int i = 0; 12919 int matched = 0; 12920 12921 ASSERT(MUTEX_HELD(&cpu_lock)); 12922 ASSERT(MUTEX_HELD(&dtrace_lock)); 12923 12924 for (i = 0; i < enab->dten_ndesc; i++) { 12925 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12926 12927 enab->dten_current = ep; 12928 enab->dten_error = 0; 12929 12930 matched += dtrace_probe_enable(&ep->dted_probe, enab); 12931 12932 if (enab->dten_error != 0) { 12933 /* 12934 * If we get an error half-way through enabling the 12935 * probes, we kick out -- perhaps with some number of 12936 * them enabled. Leaving enabled probes enabled may 12937 * be slightly confusing for user-level, but we expect 12938 * that no one will attempt to actually drive on in 12939 * the face of such errors. If this is an anonymous 12940 * enabling (indicated with a NULL nmatched pointer), 12941 * we cmn_err() a message. We aren't expecting to 12942 * get such an error -- such as it can exist at all, 12943 * it would be a result of corrupted DOF in the driver 12944 * properties. 12945 */ 12946 if (nmatched == NULL) { 12947 cmn_err(CE_WARN, "dtrace_enabling_match() " 12948 "error on %p: %d", (void *)ep, 12949 enab->dten_error); 12950 } 12951 12952 return (enab->dten_error); 12953 } 12954 } 12955 12956 enab->dten_probegen = dtrace_probegen; 12957 if (nmatched != NULL) 12958 *nmatched = matched; 12959 12960 return (0); 12961 } 12962 12963 static void 12964 dtrace_enabling_matchall(void) 12965 { 12966 dtrace_enabling_t *enab; 12967 12968 mutex_enter(&cpu_lock); 12969 mutex_enter(&dtrace_lock); 12970 12971 /* 12972 * Iterate over all retained enablings to see if any probes match 12973 * against them. We only perform this operation on enablings for which 12974 * we have sufficient permissions by virtue of being in the global zone 12975 * or in the same zone as the DTrace client. Because we can be called 12976 * after dtrace_detach() has been called, we cannot assert that there 12977 * are retained enablings. We can safely load from dtrace_retained, 12978 * however: the taskq_destroy() at the end of dtrace_detach() will 12979 * block pending our completion. 12980 */ 12981 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12982 #ifdef illumos 12983 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 12984 12985 if (INGLOBALZONE(curproc) || 12986 cr != NULL && getzoneid() == crgetzoneid(cr)) 12987 #endif 12988 (void) dtrace_enabling_match(enab, NULL); 12989 } 12990 12991 mutex_exit(&dtrace_lock); 12992 mutex_exit(&cpu_lock); 12993 } 12994 12995 /* 12996 * If an enabling is to be enabled without having matched probes (that is, if 12997 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 12998 * enabling must be _primed_ by creating an ECB for every ECB description. 12999 * This must be done to assure that we know the number of speculations, the 13000 * number of aggregations, the minimum buffer size needed, etc. before we 13001 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 13002 * enabling any probes, we create ECBs for every ECB decription, but with a 13003 * NULL probe -- which is exactly what this function does. 13004 */ 13005 static void 13006 dtrace_enabling_prime(dtrace_state_t *state) 13007 { 13008 dtrace_enabling_t *enab; 13009 int i; 13010 13011 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 13012 ASSERT(enab->dten_vstate->dtvs_state != NULL); 13013 13014 if (enab->dten_vstate->dtvs_state != state) 13015 continue; 13016 13017 /* 13018 * We don't want to prime an enabling more than once, lest 13019 * we allow a malicious user to induce resource exhaustion. 13020 * (The ECBs that result from priming an enabling aren't 13021 * leaked -- but they also aren't deallocated until the 13022 * consumer state is destroyed.) 13023 */ 13024 if (enab->dten_primed) 13025 continue; 13026 13027 for (i = 0; i < enab->dten_ndesc; i++) { 13028 enab->dten_current = enab->dten_desc[i]; 13029 (void) dtrace_probe_enable(NULL, enab); 13030 } 13031 13032 enab->dten_primed = 1; 13033 } 13034 } 13035 13036 /* 13037 * Called to indicate that probes should be provided due to retained 13038 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 13039 * must take an initial lap through the enabling calling the dtps_provide() 13040 * entry point explicitly to allow for autocreated probes. 13041 */ 13042 static void 13043 dtrace_enabling_provide(dtrace_provider_t *prv) 13044 { 13045 int i, all = 0; 13046 dtrace_probedesc_t desc; 13047 dtrace_genid_t gen; 13048 13049 ASSERT(MUTEX_HELD(&dtrace_lock)); 13050 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 13051 13052 if (prv == NULL) { 13053 all = 1; 13054 prv = dtrace_provider; 13055 } 13056 13057 do { 13058 dtrace_enabling_t *enab; 13059 void *parg = prv->dtpv_arg; 13060 13061 retry: 13062 gen = dtrace_retained_gen; 13063 for (enab = dtrace_retained; enab != NULL; 13064 enab = enab->dten_next) { 13065 for (i = 0; i < enab->dten_ndesc; i++) { 13066 desc = enab->dten_desc[i]->dted_probe; 13067 mutex_exit(&dtrace_lock); 13068 prv->dtpv_pops.dtps_provide(parg, &desc); 13069 mutex_enter(&dtrace_lock); 13070 /* 13071 * Process the retained enablings again if 13072 * they have changed while we weren't holding 13073 * dtrace_lock. 13074 */ 13075 if (gen != dtrace_retained_gen) 13076 goto retry; 13077 } 13078 } 13079 } while (all && (prv = prv->dtpv_next) != NULL); 13080 13081 mutex_exit(&dtrace_lock); 13082 dtrace_probe_provide(NULL, all ? NULL : prv); 13083 mutex_enter(&dtrace_lock); 13084 } 13085 13086 /* 13087 * Called to reap ECBs that are attached to probes from defunct providers. 13088 */ 13089 static void 13090 dtrace_enabling_reap(void) 13091 { 13092 dtrace_provider_t *prov; 13093 dtrace_probe_t *probe; 13094 dtrace_ecb_t *ecb; 13095 hrtime_t when; 13096 int i; 13097 13098 mutex_enter(&cpu_lock); 13099 mutex_enter(&dtrace_lock); 13100 13101 for (i = 0; i < dtrace_nprobes; i++) { 13102 if ((probe = dtrace_probes[i]) == NULL) 13103 continue; 13104 13105 if (probe->dtpr_ecb == NULL) 13106 continue; 13107 13108 prov = probe->dtpr_provider; 13109 13110 if ((when = prov->dtpv_defunct) == 0) 13111 continue; 13112 13113 /* 13114 * We have ECBs on a defunct provider: we want to reap these 13115 * ECBs to allow the provider to unregister. The destruction 13116 * of these ECBs must be done carefully: if we destroy the ECB 13117 * and the consumer later wishes to consume an EPID that 13118 * corresponds to the destroyed ECB (and if the EPID metadata 13119 * has not been previously consumed), the consumer will abort 13120 * processing on the unknown EPID. To reduce (but not, sadly, 13121 * eliminate) the possibility of this, we will only destroy an 13122 * ECB for a defunct provider if, for the state that 13123 * corresponds to the ECB: 13124 * 13125 * (a) There is no speculative tracing (which can effectively 13126 * cache an EPID for an arbitrary amount of time). 13127 * 13128 * (b) The principal buffers have been switched twice since the 13129 * provider became defunct. 13130 * 13131 * (c) The aggregation buffers are of zero size or have been 13132 * switched twice since the provider became defunct. 13133 * 13134 * We use dts_speculates to determine (a) and call a function 13135 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 13136 * that as soon as we've been unable to destroy one of the ECBs 13137 * associated with the probe, we quit trying -- reaping is only 13138 * fruitful in as much as we can destroy all ECBs associated 13139 * with the defunct provider's probes. 13140 */ 13141 while ((ecb = probe->dtpr_ecb) != NULL) { 13142 dtrace_state_t *state = ecb->dte_state; 13143 dtrace_buffer_t *buf = state->dts_buffer; 13144 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 13145 13146 if (state->dts_speculates) 13147 break; 13148 13149 if (!dtrace_buffer_consumed(buf, when)) 13150 break; 13151 13152 if (!dtrace_buffer_consumed(aggbuf, when)) 13153 break; 13154 13155 dtrace_ecb_disable(ecb); 13156 ASSERT(probe->dtpr_ecb != ecb); 13157 dtrace_ecb_destroy(ecb); 13158 } 13159 } 13160 13161 mutex_exit(&dtrace_lock); 13162 mutex_exit(&cpu_lock); 13163 } 13164 13165 /* 13166 * DTrace DOF Functions 13167 */ 13168 /*ARGSUSED*/ 13169 static void 13170 dtrace_dof_error(dof_hdr_t *dof, const char *str) 13171 { 13172 if (dtrace_err_verbose) 13173 cmn_err(CE_WARN, "failed to process DOF: %s", str); 13174 13175 #ifdef DTRACE_ERRDEBUG 13176 dtrace_errdebug(str); 13177 #endif 13178 } 13179 13180 /* 13181 * Create DOF out of a currently enabled state. Right now, we only create 13182 * DOF containing the run-time options -- but this could be expanded to create 13183 * complete DOF representing the enabled state. 13184 */ 13185 static dof_hdr_t * 13186 dtrace_dof_create(dtrace_state_t *state) 13187 { 13188 dof_hdr_t *dof; 13189 dof_sec_t *sec; 13190 dof_optdesc_t *opt; 13191 int i, len = sizeof (dof_hdr_t) + 13192 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 13193 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 13194 13195 ASSERT(MUTEX_HELD(&dtrace_lock)); 13196 13197 dof = kmem_zalloc(len, KM_SLEEP); 13198 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 13199 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 13200 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 13201 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 13202 13203 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 13204 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 13205 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 13206 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 13207 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 13208 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 13209 13210 dof->dofh_flags = 0; 13211 dof->dofh_hdrsize = sizeof (dof_hdr_t); 13212 dof->dofh_secsize = sizeof (dof_sec_t); 13213 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 13214 dof->dofh_secoff = sizeof (dof_hdr_t); 13215 dof->dofh_loadsz = len; 13216 dof->dofh_filesz = len; 13217 dof->dofh_pad = 0; 13218 13219 /* 13220 * Fill in the option section header... 13221 */ 13222 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 13223 sec->dofs_type = DOF_SECT_OPTDESC; 13224 sec->dofs_align = sizeof (uint64_t); 13225 sec->dofs_flags = DOF_SECF_LOAD; 13226 sec->dofs_entsize = sizeof (dof_optdesc_t); 13227 13228 opt = (dof_optdesc_t *)((uintptr_t)sec + 13229 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 13230 13231 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 13232 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 13233 13234 for (i = 0; i < DTRACEOPT_MAX; i++) { 13235 opt[i].dofo_option = i; 13236 opt[i].dofo_strtab = DOF_SECIDX_NONE; 13237 opt[i].dofo_value = state->dts_options[i]; 13238 } 13239 13240 return (dof); 13241 } 13242 13243 static dof_hdr_t * 13244 dtrace_dof_copyin(uintptr_t uarg, int *errp) 13245 { 13246 dof_hdr_t hdr, *dof; 13247 13248 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13249 13250 /* 13251 * First, we're going to copyin() the sizeof (dof_hdr_t). 13252 */ 13253 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 13254 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13255 *errp = EFAULT; 13256 return (NULL); 13257 } 13258 13259 /* 13260 * Now we'll allocate the entire DOF and copy it in -- provided 13261 * that the length isn't outrageous. 13262 */ 13263 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13264 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13265 *errp = E2BIG; 13266 return (NULL); 13267 } 13268 13269 if (hdr.dofh_loadsz < sizeof (hdr)) { 13270 dtrace_dof_error(&hdr, "invalid load size"); 13271 *errp = EINVAL; 13272 return (NULL); 13273 } 13274 13275 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 13276 13277 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 13278 dof->dofh_loadsz != hdr.dofh_loadsz) { 13279 kmem_free(dof, hdr.dofh_loadsz); 13280 *errp = EFAULT; 13281 return (NULL); 13282 } 13283 13284 return (dof); 13285 } 13286 13287 #ifdef __FreeBSD__ 13288 static dof_hdr_t * 13289 dtrace_dof_copyin_proc(struct proc *p, uintptr_t uarg, int *errp) 13290 { 13291 dof_hdr_t hdr, *dof; 13292 struct thread *td; 13293 size_t loadsz; 13294 13295 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13296 13297 td = curthread; 13298 13299 /* 13300 * First, we're going to copyin() the sizeof (dof_hdr_t). 13301 */ 13302 if (proc_readmem(td, p, uarg, &hdr, sizeof(hdr)) != sizeof(hdr)) { 13303 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13304 *errp = EFAULT; 13305 return (NULL); 13306 } 13307 13308 /* 13309 * Now we'll allocate the entire DOF and copy it in -- provided 13310 * that the length isn't outrageous. 13311 */ 13312 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13313 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13314 *errp = E2BIG; 13315 return (NULL); 13316 } 13317 loadsz = (size_t)hdr.dofh_loadsz; 13318 13319 if (loadsz < sizeof (hdr)) { 13320 dtrace_dof_error(&hdr, "invalid load size"); 13321 *errp = EINVAL; 13322 return (NULL); 13323 } 13324 13325 dof = kmem_alloc(loadsz, KM_SLEEP); 13326 13327 if (proc_readmem(td, p, uarg, dof, loadsz) != loadsz || 13328 dof->dofh_loadsz != loadsz) { 13329 kmem_free(dof, hdr.dofh_loadsz); 13330 *errp = EFAULT; 13331 return (NULL); 13332 } 13333 13334 return (dof); 13335 } 13336 13337 static __inline uchar_t 13338 dtrace_dof_char(char c) 13339 { 13340 13341 switch (c) { 13342 case '0': 13343 case '1': 13344 case '2': 13345 case '3': 13346 case '4': 13347 case '5': 13348 case '6': 13349 case '7': 13350 case '8': 13351 case '9': 13352 return (c - '0'); 13353 case 'A': 13354 case 'B': 13355 case 'C': 13356 case 'D': 13357 case 'E': 13358 case 'F': 13359 return (c - 'A' + 10); 13360 case 'a': 13361 case 'b': 13362 case 'c': 13363 case 'd': 13364 case 'e': 13365 case 'f': 13366 return (c - 'a' + 10); 13367 } 13368 /* Should not reach here. */ 13369 return (UCHAR_MAX); 13370 } 13371 #endif /* __FreeBSD__ */ 13372 13373 static dof_hdr_t * 13374 dtrace_dof_property(const char *name) 13375 { 13376 #ifdef __FreeBSD__ 13377 uint8_t *dofbuf; 13378 u_char *data, *eol; 13379 caddr_t doffile; 13380 size_t bytes, len, i; 13381 dof_hdr_t *dof; 13382 u_char c1, c2; 13383 13384 dof = NULL; 13385 13386 doffile = preload_search_by_type("dtrace_dof"); 13387 if (doffile == NULL) 13388 return (NULL); 13389 13390 data = preload_fetch_addr(doffile); 13391 len = preload_fetch_size(doffile); 13392 for (;;) { 13393 /* Look for the end of the line. All lines end in a newline. */ 13394 eol = memchr(data, '\n', len); 13395 if (eol == NULL) 13396 return (NULL); 13397 13398 if (strncmp(name, data, strlen(name)) == 0) 13399 break; 13400 13401 eol++; /* skip past the newline */ 13402 len -= eol - data; 13403 data = eol; 13404 } 13405 13406 /* We've found the data corresponding to the specified key. */ 13407 13408 data += strlen(name) + 1; /* skip past the '=' */ 13409 len = eol - data; 13410 bytes = len / 2; 13411 13412 if (bytes < sizeof(dof_hdr_t)) { 13413 dtrace_dof_error(NULL, "truncated header"); 13414 goto doferr; 13415 } 13416 13417 /* 13418 * Each byte is represented by the two ASCII characters in its hex 13419 * representation. 13420 */ 13421 dofbuf = malloc(bytes, M_SOLARIS, M_WAITOK); 13422 for (i = 0; i < bytes; i++) { 13423 c1 = dtrace_dof_char(data[i * 2]); 13424 c2 = dtrace_dof_char(data[i * 2 + 1]); 13425 if (c1 == UCHAR_MAX || c2 == UCHAR_MAX) { 13426 dtrace_dof_error(NULL, "invalid hex char in DOF"); 13427 goto doferr; 13428 } 13429 dofbuf[i] = c1 * 16 + c2; 13430 } 13431 13432 dof = (dof_hdr_t *)dofbuf; 13433 if (bytes < dof->dofh_loadsz) { 13434 dtrace_dof_error(NULL, "truncated DOF"); 13435 goto doferr; 13436 } 13437 13438 if (dof->dofh_loadsz >= dtrace_dof_maxsize) { 13439 dtrace_dof_error(NULL, "oversized DOF"); 13440 goto doferr; 13441 } 13442 13443 return (dof); 13444 13445 doferr: 13446 free(dof, M_SOLARIS); 13447 return (NULL); 13448 #else /* __FreeBSD__ */ 13449 uchar_t *buf; 13450 uint64_t loadsz; 13451 unsigned int len, i; 13452 dof_hdr_t *dof; 13453 13454 /* 13455 * Unfortunately, array of values in .conf files are always (and 13456 * only) interpreted to be integer arrays. We must read our DOF 13457 * as an integer array, and then squeeze it into a byte array. 13458 */ 13459 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 13460 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 13461 return (NULL); 13462 13463 for (i = 0; i < len; i++) 13464 buf[i] = (uchar_t)(((int *)buf)[i]); 13465 13466 if (len < sizeof (dof_hdr_t)) { 13467 ddi_prop_free(buf); 13468 dtrace_dof_error(NULL, "truncated header"); 13469 return (NULL); 13470 } 13471 13472 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 13473 ddi_prop_free(buf); 13474 dtrace_dof_error(NULL, "truncated DOF"); 13475 return (NULL); 13476 } 13477 13478 if (loadsz >= dtrace_dof_maxsize) { 13479 ddi_prop_free(buf); 13480 dtrace_dof_error(NULL, "oversized DOF"); 13481 return (NULL); 13482 } 13483 13484 dof = kmem_alloc(loadsz, KM_SLEEP); 13485 bcopy(buf, dof, loadsz); 13486 ddi_prop_free(buf); 13487 13488 return (dof); 13489 #endif /* !__FreeBSD__ */ 13490 } 13491 13492 static void 13493 dtrace_dof_destroy(dof_hdr_t *dof) 13494 { 13495 kmem_free(dof, dof->dofh_loadsz); 13496 } 13497 13498 /* 13499 * Return the dof_sec_t pointer corresponding to a given section index. If the 13500 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 13501 * a type other than DOF_SECT_NONE is specified, the header is checked against 13502 * this type and NULL is returned if the types do not match. 13503 */ 13504 static dof_sec_t * 13505 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 13506 { 13507 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 13508 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 13509 13510 if (i >= dof->dofh_secnum) { 13511 dtrace_dof_error(dof, "referenced section index is invalid"); 13512 return (NULL); 13513 } 13514 13515 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 13516 dtrace_dof_error(dof, "referenced section is not loadable"); 13517 return (NULL); 13518 } 13519 13520 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 13521 dtrace_dof_error(dof, "referenced section is the wrong type"); 13522 return (NULL); 13523 } 13524 13525 return (sec); 13526 } 13527 13528 static dtrace_probedesc_t * 13529 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 13530 { 13531 dof_probedesc_t *probe; 13532 dof_sec_t *strtab; 13533 uintptr_t daddr = (uintptr_t)dof; 13534 uintptr_t str; 13535 size_t size; 13536 13537 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 13538 dtrace_dof_error(dof, "invalid probe section"); 13539 return (NULL); 13540 } 13541 13542 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13543 dtrace_dof_error(dof, "bad alignment in probe description"); 13544 return (NULL); 13545 } 13546 13547 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 13548 dtrace_dof_error(dof, "truncated probe description"); 13549 return (NULL); 13550 } 13551 13552 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 13553 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 13554 13555 if (strtab == NULL) 13556 return (NULL); 13557 13558 str = daddr + strtab->dofs_offset; 13559 size = strtab->dofs_size; 13560 13561 if (probe->dofp_provider >= strtab->dofs_size) { 13562 dtrace_dof_error(dof, "corrupt probe provider"); 13563 return (NULL); 13564 } 13565 13566 (void) strncpy(desc->dtpd_provider, 13567 (char *)(str + probe->dofp_provider), 13568 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 13569 13570 if (probe->dofp_mod >= strtab->dofs_size) { 13571 dtrace_dof_error(dof, "corrupt probe module"); 13572 return (NULL); 13573 } 13574 13575 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 13576 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 13577 13578 if (probe->dofp_func >= strtab->dofs_size) { 13579 dtrace_dof_error(dof, "corrupt probe function"); 13580 return (NULL); 13581 } 13582 13583 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 13584 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 13585 13586 if (probe->dofp_name >= strtab->dofs_size) { 13587 dtrace_dof_error(dof, "corrupt probe name"); 13588 return (NULL); 13589 } 13590 13591 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 13592 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 13593 13594 return (desc); 13595 } 13596 13597 static dtrace_difo_t * 13598 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13599 cred_t *cr) 13600 { 13601 dtrace_difo_t *dp; 13602 size_t ttl = 0; 13603 dof_difohdr_t *dofd; 13604 uintptr_t daddr = (uintptr_t)dof; 13605 size_t max = dtrace_difo_maxsize; 13606 int i, l, n; 13607 13608 static const struct { 13609 int section; 13610 int bufoffs; 13611 int lenoffs; 13612 int entsize; 13613 int align; 13614 const char *msg; 13615 } difo[] = { 13616 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 13617 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 13618 sizeof (dif_instr_t), "multiple DIF sections" }, 13619 13620 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 13621 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 13622 sizeof (uint64_t), "multiple integer tables" }, 13623 13624 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 13625 offsetof(dtrace_difo_t, dtdo_strlen), 0, 13626 sizeof (char), "multiple string tables" }, 13627 13628 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 13629 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 13630 sizeof (uint_t), "multiple variable tables" }, 13631 13632 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 13633 }; 13634 13635 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 13636 dtrace_dof_error(dof, "invalid DIFO header section"); 13637 return (NULL); 13638 } 13639 13640 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13641 dtrace_dof_error(dof, "bad alignment in DIFO header"); 13642 return (NULL); 13643 } 13644 13645 if (sec->dofs_size < sizeof (dof_difohdr_t) || 13646 sec->dofs_size % sizeof (dof_secidx_t)) { 13647 dtrace_dof_error(dof, "bad size in DIFO header"); 13648 return (NULL); 13649 } 13650 13651 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13652 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 13653 13654 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 13655 dp->dtdo_rtype = dofd->dofd_rtype; 13656 13657 for (l = 0; l < n; l++) { 13658 dof_sec_t *subsec; 13659 void **bufp; 13660 uint32_t *lenp; 13661 13662 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 13663 dofd->dofd_links[l])) == NULL) 13664 goto err; /* invalid section link */ 13665 13666 if (ttl + subsec->dofs_size > max) { 13667 dtrace_dof_error(dof, "exceeds maximum size"); 13668 goto err; 13669 } 13670 13671 ttl += subsec->dofs_size; 13672 13673 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 13674 if (subsec->dofs_type != difo[i].section) 13675 continue; 13676 13677 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 13678 dtrace_dof_error(dof, "section not loaded"); 13679 goto err; 13680 } 13681 13682 if (subsec->dofs_align != difo[i].align) { 13683 dtrace_dof_error(dof, "bad alignment"); 13684 goto err; 13685 } 13686 13687 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 13688 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 13689 13690 if (*bufp != NULL) { 13691 dtrace_dof_error(dof, difo[i].msg); 13692 goto err; 13693 } 13694 13695 if (difo[i].entsize != subsec->dofs_entsize) { 13696 dtrace_dof_error(dof, "entry size mismatch"); 13697 goto err; 13698 } 13699 13700 if (subsec->dofs_entsize != 0 && 13701 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 13702 dtrace_dof_error(dof, "corrupt entry size"); 13703 goto err; 13704 } 13705 13706 *lenp = subsec->dofs_size; 13707 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 13708 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 13709 *bufp, subsec->dofs_size); 13710 13711 if (subsec->dofs_entsize != 0) 13712 *lenp /= subsec->dofs_entsize; 13713 13714 break; 13715 } 13716 13717 /* 13718 * If we encounter a loadable DIFO sub-section that is not 13719 * known to us, assume this is a broken program and fail. 13720 */ 13721 if (difo[i].section == DOF_SECT_NONE && 13722 (subsec->dofs_flags & DOF_SECF_LOAD)) { 13723 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 13724 goto err; 13725 } 13726 } 13727 13728 if (dp->dtdo_buf == NULL) { 13729 /* 13730 * We can't have a DIF object without DIF text. 13731 */ 13732 dtrace_dof_error(dof, "missing DIF text"); 13733 goto err; 13734 } 13735 13736 /* 13737 * Before we validate the DIF object, run through the variable table 13738 * looking for the strings -- if any of their size are under, we'll set 13739 * their size to be the system-wide default string size. Note that 13740 * this should _not_ happen if the "strsize" option has been set -- 13741 * in this case, the compiler should have set the size to reflect the 13742 * setting of the option. 13743 */ 13744 for (i = 0; i < dp->dtdo_varlen; i++) { 13745 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 13746 dtrace_diftype_t *t = &v->dtdv_type; 13747 13748 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 13749 continue; 13750 13751 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 13752 t->dtdt_size = dtrace_strsize_default; 13753 } 13754 13755 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 13756 goto err; 13757 13758 dtrace_difo_init(dp, vstate); 13759 return (dp); 13760 13761 err: 13762 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 13763 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 13764 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 13765 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 13766 13767 kmem_free(dp, sizeof (dtrace_difo_t)); 13768 return (NULL); 13769 } 13770 13771 static dtrace_predicate_t * 13772 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13773 cred_t *cr) 13774 { 13775 dtrace_difo_t *dp; 13776 13777 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 13778 return (NULL); 13779 13780 return (dtrace_predicate_create(dp)); 13781 } 13782 13783 static dtrace_actdesc_t * 13784 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13785 cred_t *cr) 13786 { 13787 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 13788 dof_actdesc_t *desc; 13789 dof_sec_t *difosec; 13790 size_t offs; 13791 uintptr_t daddr = (uintptr_t)dof; 13792 uint64_t arg; 13793 dtrace_actkind_t kind; 13794 13795 if (sec->dofs_type != DOF_SECT_ACTDESC) { 13796 dtrace_dof_error(dof, "invalid action section"); 13797 return (NULL); 13798 } 13799 13800 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 13801 dtrace_dof_error(dof, "truncated action description"); 13802 return (NULL); 13803 } 13804 13805 if (sec->dofs_align != sizeof (uint64_t)) { 13806 dtrace_dof_error(dof, "bad alignment in action description"); 13807 return (NULL); 13808 } 13809 13810 if (sec->dofs_size < sec->dofs_entsize) { 13811 dtrace_dof_error(dof, "section entry size exceeds total size"); 13812 return (NULL); 13813 } 13814 13815 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 13816 dtrace_dof_error(dof, "bad entry size in action description"); 13817 return (NULL); 13818 } 13819 13820 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 13821 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 13822 return (NULL); 13823 } 13824 13825 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 13826 desc = (dof_actdesc_t *)(daddr + 13827 (uintptr_t)sec->dofs_offset + offs); 13828 kind = (dtrace_actkind_t)desc->dofa_kind; 13829 13830 if ((DTRACEACT_ISPRINTFLIKE(kind) && 13831 (kind != DTRACEACT_PRINTA || 13832 desc->dofa_strtab != DOF_SECIDX_NONE)) || 13833 (kind == DTRACEACT_DIFEXPR && 13834 desc->dofa_strtab != DOF_SECIDX_NONE)) { 13835 dof_sec_t *strtab; 13836 char *str, *fmt; 13837 uint64_t i; 13838 13839 /* 13840 * The argument to these actions is an index into the 13841 * DOF string table. For printf()-like actions, this 13842 * is the format string. For print(), this is the 13843 * CTF type of the expression result. 13844 */ 13845 if ((strtab = dtrace_dof_sect(dof, 13846 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 13847 goto err; 13848 13849 str = (char *)((uintptr_t)dof + 13850 (uintptr_t)strtab->dofs_offset); 13851 13852 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 13853 if (str[i] == '\0') 13854 break; 13855 } 13856 13857 if (i >= strtab->dofs_size) { 13858 dtrace_dof_error(dof, "bogus format string"); 13859 goto err; 13860 } 13861 13862 if (i == desc->dofa_arg) { 13863 dtrace_dof_error(dof, "empty format string"); 13864 goto err; 13865 } 13866 13867 i -= desc->dofa_arg; 13868 fmt = kmem_alloc(i + 1, KM_SLEEP); 13869 bcopy(&str[desc->dofa_arg], fmt, i + 1); 13870 arg = (uint64_t)(uintptr_t)fmt; 13871 } else { 13872 if (kind == DTRACEACT_PRINTA) { 13873 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 13874 arg = 0; 13875 } else { 13876 arg = desc->dofa_arg; 13877 } 13878 } 13879 13880 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 13881 desc->dofa_uarg, arg); 13882 13883 if (last != NULL) { 13884 last->dtad_next = act; 13885 } else { 13886 first = act; 13887 } 13888 13889 last = act; 13890 13891 if (desc->dofa_difo == DOF_SECIDX_NONE) 13892 continue; 13893 13894 if ((difosec = dtrace_dof_sect(dof, 13895 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 13896 goto err; 13897 13898 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 13899 13900 if (act->dtad_difo == NULL) 13901 goto err; 13902 } 13903 13904 ASSERT(first != NULL); 13905 return (first); 13906 13907 err: 13908 for (act = first; act != NULL; act = next) { 13909 next = act->dtad_next; 13910 dtrace_actdesc_release(act, vstate); 13911 } 13912 13913 return (NULL); 13914 } 13915 13916 static dtrace_ecbdesc_t * 13917 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13918 cred_t *cr) 13919 { 13920 dtrace_ecbdesc_t *ep; 13921 dof_ecbdesc_t *ecb; 13922 dtrace_probedesc_t *desc; 13923 dtrace_predicate_t *pred = NULL; 13924 13925 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 13926 dtrace_dof_error(dof, "truncated ECB description"); 13927 return (NULL); 13928 } 13929 13930 if (sec->dofs_align != sizeof (uint64_t)) { 13931 dtrace_dof_error(dof, "bad alignment in ECB description"); 13932 return (NULL); 13933 } 13934 13935 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 13936 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 13937 13938 if (sec == NULL) 13939 return (NULL); 13940 13941 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 13942 ep->dted_uarg = ecb->dofe_uarg; 13943 desc = &ep->dted_probe; 13944 13945 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 13946 goto err; 13947 13948 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 13949 if ((sec = dtrace_dof_sect(dof, 13950 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 13951 goto err; 13952 13953 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 13954 goto err; 13955 13956 ep->dted_pred.dtpdd_predicate = pred; 13957 } 13958 13959 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 13960 if ((sec = dtrace_dof_sect(dof, 13961 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 13962 goto err; 13963 13964 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 13965 13966 if (ep->dted_action == NULL) 13967 goto err; 13968 } 13969 13970 return (ep); 13971 13972 err: 13973 if (pred != NULL) 13974 dtrace_predicate_release(pred, vstate); 13975 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 13976 return (NULL); 13977 } 13978 13979 /* 13980 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 13981 * specified DOF. At present, this amounts to simply adding 'ubase' to the 13982 * site of any user SETX relocations to account for load object base address. 13983 * In the future, if we need other relocations, this function can be extended. 13984 */ 13985 static int 13986 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 13987 { 13988 uintptr_t daddr = (uintptr_t)dof; 13989 dof_relohdr_t *dofr = 13990 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13991 dof_sec_t *ss, *rs, *ts; 13992 dof_relodesc_t *r; 13993 uint_t i, n; 13994 13995 if (sec->dofs_size < sizeof (dof_relohdr_t) || 13996 sec->dofs_align != sizeof (dof_secidx_t)) { 13997 dtrace_dof_error(dof, "invalid relocation header"); 13998 return (-1); 13999 } 14000 14001 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 14002 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 14003 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 14004 14005 if (ss == NULL || rs == NULL || ts == NULL) 14006 return (-1); /* dtrace_dof_error() has been called already */ 14007 14008 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 14009 rs->dofs_align != sizeof (uint64_t)) { 14010 dtrace_dof_error(dof, "invalid relocation section"); 14011 return (-1); 14012 } 14013 14014 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 14015 n = rs->dofs_size / rs->dofs_entsize; 14016 14017 for (i = 0; i < n; i++) { 14018 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 14019 14020 switch (r->dofr_type) { 14021 case DOF_RELO_NONE: 14022 break; 14023 case DOF_RELO_SETX: 14024 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 14025 sizeof (uint64_t) > ts->dofs_size) { 14026 dtrace_dof_error(dof, "bad relocation offset"); 14027 return (-1); 14028 } 14029 14030 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 14031 dtrace_dof_error(dof, "misaligned setx relo"); 14032 return (-1); 14033 } 14034 14035 *(uint64_t *)taddr += ubase; 14036 break; 14037 default: 14038 dtrace_dof_error(dof, "invalid relocation type"); 14039 return (-1); 14040 } 14041 14042 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 14043 } 14044 14045 return (0); 14046 } 14047 14048 /* 14049 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 14050 * header: it should be at the front of a memory region that is at least 14051 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 14052 * size. It need not be validated in any other way. 14053 */ 14054 static int 14055 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 14056 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 14057 { 14058 uint64_t len = dof->dofh_loadsz, seclen; 14059 uintptr_t daddr = (uintptr_t)dof; 14060 dtrace_ecbdesc_t *ep; 14061 dtrace_enabling_t *enab; 14062 uint_t i; 14063 14064 ASSERT(MUTEX_HELD(&dtrace_lock)); 14065 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 14066 14067 /* 14068 * Check the DOF header identification bytes. In addition to checking 14069 * valid settings, we also verify that unused bits/bytes are zeroed so 14070 * we can use them later without fear of regressing existing binaries. 14071 */ 14072 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 14073 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 14074 dtrace_dof_error(dof, "DOF magic string mismatch"); 14075 return (-1); 14076 } 14077 14078 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 14079 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 14080 dtrace_dof_error(dof, "DOF has invalid data model"); 14081 return (-1); 14082 } 14083 14084 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 14085 dtrace_dof_error(dof, "DOF encoding mismatch"); 14086 return (-1); 14087 } 14088 14089 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14090 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 14091 dtrace_dof_error(dof, "DOF version mismatch"); 14092 return (-1); 14093 } 14094 14095 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 14096 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 14097 return (-1); 14098 } 14099 14100 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 14101 dtrace_dof_error(dof, "DOF uses too many integer registers"); 14102 return (-1); 14103 } 14104 14105 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 14106 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 14107 return (-1); 14108 } 14109 14110 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 14111 if (dof->dofh_ident[i] != 0) { 14112 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 14113 return (-1); 14114 } 14115 } 14116 14117 if (dof->dofh_flags & ~DOF_FL_VALID) { 14118 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 14119 return (-1); 14120 } 14121 14122 if (dof->dofh_secsize == 0) { 14123 dtrace_dof_error(dof, "zero section header size"); 14124 return (-1); 14125 } 14126 14127 /* 14128 * Check that the section headers don't exceed the amount of DOF 14129 * data. Note that we cast the section size and number of sections 14130 * to uint64_t's to prevent possible overflow in the multiplication. 14131 */ 14132 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 14133 14134 if (dof->dofh_secoff > len || seclen > len || 14135 dof->dofh_secoff + seclen > len) { 14136 dtrace_dof_error(dof, "truncated section headers"); 14137 return (-1); 14138 } 14139 14140 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 14141 dtrace_dof_error(dof, "misaligned section headers"); 14142 return (-1); 14143 } 14144 14145 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 14146 dtrace_dof_error(dof, "misaligned section size"); 14147 return (-1); 14148 } 14149 14150 /* 14151 * Take an initial pass through the section headers to be sure that 14152 * the headers don't have stray offsets. If the 'noprobes' flag is 14153 * set, do not permit sections relating to providers, probes, or args. 14154 */ 14155 for (i = 0; i < dof->dofh_secnum; i++) { 14156 dof_sec_t *sec = (dof_sec_t *)(daddr + 14157 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14158 14159 if (noprobes) { 14160 switch (sec->dofs_type) { 14161 case DOF_SECT_PROVIDER: 14162 case DOF_SECT_PROBES: 14163 case DOF_SECT_PRARGS: 14164 case DOF_SECT_PROFFS: 14165 dtrace_dof_error(dof, "illegal sections " 14166 "for enabling"); 14167 return (-1); 14168 } 14169 } 14170 14171 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 14172 !(sec->dofs_flags & DOF_SECF_LOAD)) { 14173 dtrace_dof_error(dof, "loadable section with load " 14174 "flag unset"); 14175 return (-1); 14176 } 14177 14178 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 14179 continue; /* just ignore non-loadable sections */ 14180 14181 if (!ISP2(sec->dofs_align)) { 14182 dtrace_dof_error(dof, "bad section alignment"); 14183 return (-1); 14184 } 14185 14186 if (sec->dofs_offset & (sec->dofs_align - 1)) { 14187 dtrace_dof_error(dof, "misaligned section"); 14188 return (-1); 14189 } 14190 14191 if (sec->dofs_offset > len || sec->dofs_size > len || 14192 sec->dofs_offset + sec->dofs_size > len) { 14193 dtrace_dof_error(dof, "corrupt section header"); 14194 return (-1); 14195 } 14196 14197 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 14198 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 14199 dtrace_dof_error(dof, "non-terminating string table"); 14200 return (-1); 14201 } 14202 } 14203 14204 /* 14205 * Take a second pass through the sections and locate and perform any 14206 * relocations that are present. We do this after the first pass to 14207 * be sure that all sections have had their headers validated. 14208 */ 14209 for (i = 0; i < dof->dofh_secnum; i++) { 14210 dof_sec_t *sec = (dof_sec_t *)(daddr + 14211 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14212 14213 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 14214 continue; /* skip sections that are not loadable */ 14215 14216 switch (sec->dofs_type) { 14217 case DOF_SECT_URELHDR: 14218 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 14219 return (-1); 14220 break; 14221 } 14222 } 14223 14224 if ((enab = *enabp) == NULL) 14225 enab = *enabp = dtrace_enabling_create(vstate); 14226 14227 for (i = 0; i < dof->dofh_secnum; i++) { 14228 dof_sec_t *sec = (dof_sec_t *)(daddr + 14229 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14230 14231 if (sec->dofs_type != DOF_SECT_ECBDESC) 14232 continue; 14233 14234 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 14235 dtrace_enabling_destroy(enab); 14236 *enabp = NULL; 14237 return (-1); 14238 } 14239 14240 dtrace_enabling_add(enab, ep); 14241 } 14242 14243 return (0); 14244 } 14245 14246 /* 14247 * Process DOF for any options. This routine assumes that the DOF has been 14248 * at least processed by dtrace_dof_slurp(). 14249 */ 14250 static int 14251 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 14252 { 14253 int i, rval; 14254 uint32_t entsize; 14255 size_t offs; 14256 dof_optdesc_t *desc; 14257 14258 for (i = 0; i < dof->dofh_secnum; i++) { 14259 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 14260 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14261 14262 if (sec->dofs_type != DOF_SECT_OPTDESC) 14263 continue; 14264 14265 if (sec->dofs_align != sizeof (uint64_t)) { 14266 dtrace_dof_error(dof, "bad alignment in " 14267 "option description"); 14268 return (EINVAL); 14269 } 14270 14271 if ((entsize = sec->dofs_entsize) == 0) { 14272 dtrace_dof_error(dof, "zeroed option entry size"); 14273 return (EINVAL); 14274 } 14275 14276 if (entsize < sizeof (dof_optdesc_t)) { 14277 dtrace_dof_error(dof, "bad option entry size"); 14278 return (EINVAL); 14279 } 14280 14281 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 14282 desc = (dof_optdesc_t *)((uintptr_t)dof + 14283 (uintptr_t)sec->dofs_offset + offs); 14284 14285 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 14286 dtrace_dof_error(dof, "non-zero option string"); 14287 return (EINVAL); 14288 } 14289 14290 if (desc->dofo_value == DTRACEOPT_UNSET) { 14291 dtrace_dof_error(dof, "unset option"); 14292 return (EINVAL); 14293 } 14294 14295 if ((rval = dtrace_state_option(state, 14296 desc->dofo_option, desc->dofo_value)) != 0) { 14297 dtrace_dof_error(dof, "rejected option"); 14298 return (rval); 14299 } 14300 } 14301 } 14302 14303 return (0); 14304 } 14305 14306 /* 14307 * DTrace Consumer State Functions 14308 */ 14309 static int 14310 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 14311 { 14312 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 14313 void *base; 14314 uintptr_t limit; 14315 dtrace_dynvar_t *dvar, *next, *start; 14316 int i; 14317 14318 ASSERT(MUTEX_HELD(&dtrace_lock)); 14319 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 14320 14321 bzero(dstate, sizeof (dtrace_dstate_t)); 14322 14323 if ((dstate->dtds_chunksize = chunksize) == 0) 14324 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 14325 14326 VERIFY(dstate->dtds_chunksize < LONG_MAX); 14327 14328 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 14329 size = min; 14330 14331 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 14332 return (ENOMEM); 14333 14334 dstate->dtds_size = size; 14335 dstate->dtds_base = base; 14336 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 14337 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 14338 14339 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 14340 14341 if (hashsize != 1 && (hashsize & 1)) 14342 hashsize--; 14343 14344 dstate->dtds_hashsize = hashsize; 14345 dstate->dtds_hash = dstate->dtds_base; 14346 14347 /* 14348 * Set all of our hash buckets to point to the single sink, and (if 14349 * it hasn't already been set), set the sink's hash value to be the 14350 * sink sentinel value. The sink is needed for dynamic variable 14351 * lookups to know that they have iterated over an entire, valid hash 14352 * chain. 14353 */ 14354 for (i = 0; i < hashsize; i++) 14355 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 14356 14357 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 14358 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 14359 14360 /* 14361 * Determine number of active CPUs. Divide free list evenly among 14362 * active CPUs. 14363 */ 14364 start = (dtrace_dynvar_t *) 14365 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 14366 limit = (uintptr_t)base + size; 14367 14368 VERIFY((uintptr_t)start < limit); 14369 VERIFY((uintptr_t)start >= (uintptr_t)base); 14370 14371 maxper = (limit - (uintptr_t)start) / NCPU; 14372 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 14373 14374 #ifndef illumos 14375 CPU_FOREACH(i) { 14376 #else 14377 for (i = 0; i < NCPU; i++) { 14378 #endif 14379 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 14380 14381 /* 14382 * If we don't even have enough chunks to make it once through 14383 * NCPUs, we're just going to allocate everything to the first 14384 * CPU. And if we're on the last CPU, we're going to allocate 14385 * whatever is left over. In either case, we set the limit to 14386 * be the limit of the dynamic variable space. 14387 */ 14388 if (maxper == 0 || i == NCPU - 1) { 14389 limit = (uintptr_t)base + size; 14390 start = NULL; 14391 } else { 14392 limit = (uintptr_t)start + maxper; 14393 start = (dtrace_dynvar_t *)limit; 14394 } 14395 14396 VERIFY(limit <= (uintptr_t)base + size); 14397 14398 for (;;) { 14399 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 14400 dstate->dtds_chunksize); 14401 14402 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 14403 break; 14404 14405 VERIFY((uintptr_t)dvar >= (uintptr_t)base && 14406 (uintptr_t)dvar <= (uintptr_t)base + size); 14407 dvar->dtdv_next = next; 14408 dvar = next; 14409 } 14410 14411 if (maxper == 0) 14412 break; 14413 } 14414 14415 return (0); 14416 } 14417 14418 static void 14419 dtrace_dstate_fini(dtrace_dstate_t *dstate) 14420 { 14421 ASSERT(MUTEX_HELD(&cpu_lock)); 14422 14423 if (dstate->dtds_base == NULL) 14424 return; 14425 14426 kmem_free(dstate->dtds_base, dstate->dtds_size); 14427 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 14428 } 14429 14430 static void 14431 dtrace_vstate_fini(dtrace_vstate_t *vstate) 14432 { 14433 /* 14434 * Logical XOR, where are you? 14435 */ 14436 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 14437 14438 if (vstate->dtvs_nglobals > 0) { 14439 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 14440 sizeof (dtrace_statvar_t *)); 14441 } 14442 14443 if (vstate->dtvs_ntlocals > 0) { 14444 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 14445 sizeof (dtrace_difv_t)); 14446 } 14447 14448 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 14449 14450 if (vstate->dtvs_nlocals > 0) { 14451 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 14452 sizeof (dtrace_statvar_t *)); 14453 } 14454 } 14455 14456 #ifdef illumos 14457 static void 14458 dtrace_state_clean(dtrace_state_t *state) 14459 { 14460 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14461 return; 14462 14463 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14464 dtrace_speculation_clean(state); 14465 } 14466 14467 static void 14468 dtrace_state_deadman(dtrace_state_t *state) 14469 { 14470 hrtime_t now; 14471 14472 dtrace_sync(); 14473 14474 now = dtrace_gethrtime(); 14475 14476 if (state != dtrace_anon.dta_state && 14477 now - state->dts_laststatus >= dtrace_deadman_user) 14478 return; 14479 14480 /* 14481 * We must be sure that dts_alive never appears to be less than the 14482 * value upon entry to dtrace_state_deadman(), and because we lack a 14483 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14484 * store INT64_MAX to it, followed by a memory barrier, followed by 14485 * the new value. This assures that dts_alive never appears to be 14486 * less than its true value, regardless of the order in which the 14487 * stores to the underlying storage are issued. 14488 */ 14489 state->dts_alive = INT64_MAX; 14490 dtrace_membar_producer(); 14491 state->dts_alive = now; 14492 } 14493 #else /* !illumos */ 14494 static void 14495 dtrace_state_clean(void *arg) 14496 { 14497 dtrace_state_t *state = arg; 14498 dtrace_optval_t *opt = state->dts_options; 14499 14500 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14501 return; 14502 14503 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14504 dtrace_speculation_clean(state); 14505 14506 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14507 dtrace_state_clean, state); 14508 } 14509 14510 static void 14511 dtrace_state_deadman(void *arg) 14512 { 14513 dtrace_state_t *state = arg; 14514 hrtime_t now; 14515 14516 dtrace_sync(); 14517 14518 dtrace_debug_output(); 14519 14520 now = dtrace_gethrtime(); 14521 14522 if (state != dtrace_anon.dta_state && 14523 now - state->dts_laststatus >= dtrace_deadman_user) 14524 return; 14525 14526 /* 14527 * We must be sure that dts_alive never appears to be less than the 14528 * value upon entry to dtrace_state_deadman(), and because we lack a 14529 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14530 * store INT64_MAX to it, followed by a memory barrier, followed by 14531 * the new value. This assures that dts_alive never appears to be 14532 * less than its true value, regardless of the order in which the 14533 * stores to the underlying storage are issued. 14534 */ 14535 state->dts_alive = INT64_MAX; 14536 dtrace_membar_producer(); 14537 state->dts_alive = now; 14538 14539 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14540 dtrace_state_deadman, state); 14541 } 14542 #endif /* illumos */ 14543 14544 static dtrace_state_t * 14545 #ifdef illumos 14546 dtrace_state_create(dev_t *devp, cred_t *cr) 14547 #else 14548 dtrace_state_create(struct cdev *dev, struct ucred *cred __unused) 14549 #endif 14550 { 14551 #ifdef illumos 14552 minor_t minor; 14553 major_t major; 14554 #else 14555 cred_t *cr = NULL; 14556 int m = 0; 14557 #endif 14558 char c[30]; 14559 dtrace_state_t *state; 14560 dtrace_optval_t *opt; 14561 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 14562 14563 ASSERT(MUTEX_HELD(&dtrace_lock)); 14564 ASSERT(MUTEX_HELD(&cpu_lock)); 14565 14566 #ifdef illumos 14567 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 14568 VM_BESTFIT | VM_SLEEP); 14569 14570 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 14571 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14572 return (NULL); 14573 } 14574 14575 state = ddi_get_soft_state(dtrace_softstate, minor); 14576 #else 14577 if (dev != NULL) { 14578 cr = dev->si_cred; 14579 m = dev2unit(dev); 14580 } 14581 14582 /* Allocate memory for the state. */ 14583 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 14584 #endif 14585 14586 state->dts_epid = DTRACE_EPIDNONE + 1; 14587 14588 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 14589 #ifdef illumos 14590 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 14591 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14592 14593 if (devp != NULL) { 14594 major = getemajor(*devp); 14595 } else { 14596 major = ddi_driver_major(dtrace_devi); 14597 } 14598 14599 state->dts_dev = makedevice(major, minor); 14600 14601 if (devp != NULL) 14602 *devp = state->dts_dev; 14603 #else 14604 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 14605 state->dts_dev = dev; 14606 #endif 14607 14608 /* 14609 * We allocate NCPU buffers. On the one hand, this can be quite 14610 * a bit of memory per instance (nearly 36K on a Starcat). On the 14611 * other hand, it saves an additional memory reference in the probe 14612 * path. 14613 */ 14614 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 14615 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 14616 14617 #ifdef illumos 14618 state->dts_cleaner = CYCLIC_NONE; 14619 state->dts_deadman = CYCLIC_NONE; 14620 #else 14621 callout_init(&state->dts_cleaner, 1); 14622 callout_init(&state->dts_deadman, 1); 14623 #endif 14624 state->dts_vstate.dtvs_state = state; 14625 14626 for (i = 0; i < DTRACEOPT_MAX; i++) 14627 state->dts_options[i] = DTRACEOPT_UNSET; 14628 14629 /* 14630 * Set the default options. 14631 */ 14632 opt = state->dts_options; 14633 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 14634 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 14635 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 14636 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 14637 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 14638 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 14639 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 14640 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 14641 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 14642 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 14643 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 14644 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 14645 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 14646 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 14647 14648 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 14649 14650 /* 14651 * Depending on the user credentials, we set flag bits which alter probe 14652 * visibility or the amount of destructiveness allowed. In the case of 14653 * actual anonymous tracing, or the possession of all privileges, all of 14654 * the normal checks are bypassed. 14655 */ 14656 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 14657 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 14658 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 14659 } else { 14660 /* 14661 * Set up the credentials for this instantiation. We take a 14662 * hold on the credential to prevent it from disappearing on 14663 * us; this in turn prevents the zone_t referenced by this 14664 * credential from disappearing. This means that we can 14665 * examine the credential and the zone from probe context. 14666 */ 14667 crhold(cr); 14668 state->dts_cred.dcr_cred = cr; 14669 14670 /* 14671 * CRA_PROC means "we have *some* privilege for dtrace" and 14672 * unlocks the use of variables like pid, zonename, etc. 14673 */ 14674 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 14675 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14676 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 14677 } 14678 14679 /* 14680 * dtrace_user allows use of syscall and profile providers. 14681 * If the user also has proc_owner and/or proc_zone, we 14682 * extend the scope to include additional visibility and 14683 * destructive power. 14684 */ 14685 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 14686 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 14687 state->dts_cred.dcr_visible |= 14688 DTRACE_CRV_ALLPROC; 14689 14690 state->dts_cred.dcr_action |= 14691 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14692 } 14693 14694 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 14695 state->dts_cred.dcr_visible |= 14696 DTRACE_CRV_ALLZONE; 14697 14698 state->dts_cred.dcr_action |= 14699 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14700 } 14701 14702 /* 14703 * If we have all privs in whatever zone this is, 14704 * we can do destructive things to processes which 14705 * have altered credentials. 14706 */ 14707 #ifdef illumos 14708 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14709 cr->cr_zone->zone_privset)) { 14710 state->dts_cred.dcr_action |= 14711 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14712 } 14713 #endif 14714 } 14715 14716 /* 14717 * Holding the dtrace_kernel privilege also implies that 14718 * the user has the dtrace_user privilege from a visibility 14719 * perspective. But without further privileges, some 14720 * destructive actions are not available. 14721 */ 14722 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 14723 /* 14724 * Make all probes in all zones visible. However, 14725 * this doesn't mean that all actions become available 14726 * to all zones. 14727 */ 14728 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 14729 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 14730 14731 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 14732 DTRACE_CRA_PROC; 14733 /* 14734 * Holding proc_owner means that destructive actions 14735 * for *this* zone are allowed. 14736 */ 14737 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14738 state->dts_cred.dcr_action |= 14739 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14740 14741 /* 14742 * Holding proc_zone means that destructive actions 14743 * for this user/group ID in all zones is allowed. 14744 */ 14745 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14746 state->dts_cred.dcr_action |= 14747 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14748 14749 #ifdef illumos 14750 /* 14751 * If we have all privs in whatever zone this is, 14752 * we can do destructive things to processes which 14753 * have altered credentials. 14754 */ 14755 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14756 cr->cr_zone->zone_privset)) { 14757 state->dts_cred.dcr_action |= 14758 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14759 } 14760 #endif 14761 } 14762 14763 /* 14764 * Holding the dtrace_proc privilege gives control over fasttrap 14765 * and pid providers. We need to grant wider destructive 14766 * privileges in the event that the user has proc_owner and/or 14767 * proc_zone. 14768 */ 14769 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14770 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14771 state->dts_cred.dcr_action |= 14772 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14773 14774 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14775 state->dts_cred.dcr_action |= 14776 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14777 } 14778 } 14779 14780 return (state); 14781 } 14782 14783 static int 14784 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 14785 { 14786 dtrace_optval_t *opt = state->dts_options, size; 14787 processorid_t cpu = 0;; 14788 int flags = 0, rval, factor, divisor = 1; 14789 14790 ASSERT(MUTEX_HELD(&dtrace_lock)); 14791 ASSERT(MUTEX_HELD(&cpu_lock)); 14792 ASSERT(which < DTRACEOPT_MAX); 14793 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 14794 (state == dtrace_anon.dta_state && 14795 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 14796 14797 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 14798 return (0); 14799 14800 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 14801 cpu = opt[DTRACEOPT_CPU]; 14802 14803 if (which == DTRACEOPT_SPECSIZE) 14804 flags |= DTRACEBUF_NOSWITCH; 14805 14806 if (which == DTRACEOPT_BUFSIZE) { 14807 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 14808 flags |= DTRACEBUF_RING; 14809 14810 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 14811 flags |= DTRACEBUF_FILL; 14812 14813 if (state != dtrace_anon.dta_state || 14814 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14815 flags |= DTRACEBUF_INACTIVE; 14816 } 14817 14818 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 14819 /* 14820 * The size must be 8-byte aligned. If the size is not 8-byte 14821 * aligned, drop it down by the difference. 14822 */ 14823 if (size & (sizeof (uint64_t) - 1)) 14824 size -= size & (sizeof (uint64_t) - 1); 14825 14826 if (size < state->dts_reserve) { 14827 /* 14828 * Buffers always must be large enough to accommodate 14829 * their prereserved space. We return E2BIG instead 14830 * of ENOMEM in this case to allow for user-level 14831 * software to differentiate the cases. 14832 */ 14833 return (E2BIG); 14834 } 14835 14836 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 14837 14838 if (rval != ENOMEM) { 14839 opt[which] = size; 14840 return (rval); 14841 } 14842 14843 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14844 return (rval); 14845 14846 for (divisor = 2; divisor < factor; divisor <<= 1) 14847 continue; 14848 } 14849 14850 return (ENOMEM); 14851 } 14852 14853 static int 14854 dtrace_state_buffers(dtrace_state_t *state) 14855 { 14856 dtrace_speculation_t *spec = state->dts_speculations; 14857 int rval, i; 14858 14859 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 14860 DTRACEOPT_BUFSIZE)) != 0) 14861 return (rval); 14862 14863 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 14864 DTRACEOPT_AGGSIZE)) != 0) 14865 return (rval); 14866 14867 for (i = 0; i < state->dts_nspeculations; i++) { 14868 if ((rval = dtrace_state_buffer(state, 14869 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 14870 return (rval); 14871 } 14872 14873 return (0); 14874 } 14875 14876 static void 14877 dtrace_state_prereserve(dtrace_state_t *state) 14878 { 14879 dtrace_ecb_t *ecb; 14880 dtrace_probe_t *probe; 14881 14882 state->dts_reserve = 0; 14883 14884 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 14885 return; 14886 14887 /* 14888 * If our buffer policy is a "fill" buffer policy, we need to set the 14889 * prereserved space to be the space required by the END probes. 14890 */ 14891 probe = dtrace_probes[dtrace_probeid_end - 1]; 14892 ASSERT(probe != NULL); 14893 14894 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 14895 if (ecb->dte_state != state) 14896 continue; 14897 14898 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 14899 } 14900 } 14901 14902 static int 14903 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 14904 { 14905 dtrace_optval_t *opt = state->dts_options, sz, nspec; 14906 dtrace_speculation_t *spec; 14907 dtrace_buffer_t *buf; 14908 #ifdef illumos 14909 cyc_handler_t hdlr; 14910 cyc_time_t when; 14911 #endif 14912 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 14913 dtrace_icookie_t cookie; 14914 14915 mutex_enter(&cpu_lock); 14916 mutex_enter(&dtrace_lock); 14917 14918 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 14919 rval = EBUSY; 14920 goto out; 14921 } 14922 14923 /* 14924 * Before we can perform any checks, we must prime all of the 14925 * retained enablings that correspond to this state. 14926 */ 14927 dtrace_enabling_prime(state); 14928 14929 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 14930 rval = EACCES; 14931 goto out; 14932 } 14933 14934 dtrace_state_prereserve(state); 14935 14936 /* 14937 * Now we want to do is try to allocate our speculations. 14938 * We do not automatically resize the number of speculations; if 14939 * this fails, we will fail the operation. 14940 */ 14941 nspec = opt[DTRACEOPT_NSPEC]; 14942 ASSERT(nspec != DTRACEOPT_UNSET); 14943 14944 if (nspec > INT_MAX) { 14945 rval = ENOMEM; 14946 goto out; 14947 } 14948 14949 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 14950 KM_NOSLEEP | KM_NORMALPRI); 14951 14952 if (spec == NULL) { 14953 rval = ENOMEM; 14954 goto out; 14955 } 14956 14957 state->dts_speculations = spec; 14958 state->dts_nspeculations = (int)nspec; 14959 14960 for (i = 0; i < nspec; i++) { 14961 if ((buf = kmem_zalloc(bufsize, 14962 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 14963 rval = ENOMEM; 14964 goto err; 14965 } 14966 14967 spec[i].dtsp_buffer = buf; 14968 } 14969 14970 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 14971 if (dtrace_anon.dta_state == NULL) { 14972 rval = ENOENT; 14973 goto out; 14974 } 14975 14976 if (state->dts_necbs != 0) { 14977 rval = EALREADY; 14978 goto out; 14979 } 14980 14981 state->dts_anon = dtrace_anon_grab(); 14982 ASSERT(state->dts_anon != NULL); 14983 state = state->dts_anon; 14984 14985 /* 14986 * We want "grabanon" to be set in the grabbed state, so we'll 14987 * copy that option value from the grabbing state into the 14988 * grabbed state. 14989 */ 14990 state->dts_options[DTRACEOPT_GRABANON] = 14991 opt[DTRACEOPT_GRABANON]; 14992 14993 *cpu = dtrace_anon.dta_beganon; 14994 14995 /* 14996 * If the anonymous state is active (as it almost certainly 14997 * is if the anonymous enabling ultimately matched anything), 14998 * we don't allow any further option processing -- but we 14999 * don't return failure. 15000 */ 15001 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 15002 goto out; 15003 } 15004 15005 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 15006 opt[DTRACEOPT_AGGSIZE] != 0) { 15007 if (state->dts_aggregations == NULL) { 15008 /* 15009 * We're not going to create an aggregation buffer 15010 * because we don't have any ECBs that contain 15011 * aggregations -- set this option to 0. 15012 */ 15013 opt[DTRACEOPT_AGGSIZE] = 0; 15014 } else { 15015 /* 15016 * If we have an aggregation buffer, we must also have 15017 * a buffer to use as scratch. 15018 */ 15019 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 15020 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 15021 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 15022 } 15023 } 15024 } 15025 15026 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 15027 opt[DTRACEOPT_SPECSIZE] != 0) { 15028 if (!state->dts_speculates) { 15029 /* 15030 * We're not going to create speculation buffers 15031 * because we don't have any ECBs that actually 15032 * speculate -- set the speculation size to 0. 15033 */ 15034 opt[DTRACEOPT_SPECSIZE] = 0; 15035 } 15036 } 15037 15038 /* 15039 * The bare minimum size for any buffer that we're actually going to 15040 * do anything to is sizeof (uint64_t). 15041 */ 15042 sz = sizeof (uint64_t); 15043 15044 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 15045 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 15046 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 15047 /* 15048 * A buffer size has been explicitly set to 0 (or to a size 15049 * that will be adjusted to 0) and we need the space -- we 15050 * need to return failure. We return ENOSPC to differentiate 15051 * it from failing to allocate a buffer due to failure to meet 15052 * the reserve (for which we return E2BIG). 15053 */ 15054 rval = ENOSPC; 15055 goto out; 15056 } 15057 15058 if ((rval = dtrace_state_buffers(state)) != 0) 15059 goto err; 15060 15061 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 15062 sz = dtrace_dstate_defsize; 15063 15064 do { 15065 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 15066 15067 if (rval == 0) 15068 break; 15069 15070 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 15071 goto err; 15072 } while (sz >>= 1); 15073 15074 opt[DTRACEOPT_DYNVARSIZE] = sz; 15075 15076 if (rval != 0) 15077 goto err; 15078 15079 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 15080 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 15081 15082 if (opt[DTRACEOPT_CLEANRATE] == 0) 15083 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 15084 15085 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 15086 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 15087 15088 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 15089 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 15090 15091 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 15092 #ifdef illumos 15093 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 15094 hdlr.cyh_arg = state; 15095 hdlr.cyh_level = CY_LOW_LEVEL; 15096 15097 when.cyt_when = 0; 15098 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 15099 15100 state->dts_cleaner = cyclic_add(&hdlr, &when); 15101 15102 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 15103 hdlr.cyh_arg = state; 15104 hdlr.cyh_level = CY_LOW_LEVEL; 15105 15106 when.cyt_when = 0; 15107 when.cyt_interval = dtrace_deadman_interval; 15108 15109 state->dts_deadman = cyclic_add(&hdlr, &when); 15110 #else 15111 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 15112 dtrace_state_clean, state); 15113 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 15114 dtrace_state_deadman, state); 15115 #endif 15116 15117 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 15118 15119 #ifdef illumos 15120 if (state->dts_getf != 0 && 15121 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 15122 /* 15123 * We don't have kernel privs but we have at least one call 15124 * to getf(); we need to bump our zone's count, and (if 15125 * this is the first enabling to have an unprivileged call 15126 * to getf()) we need to hook into closef(). 15127 */ 15128 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 15129 15130 if (dtrace_getf++ == 0) { 15131 ASSERT(dtrace_closef == NULL); 15132 dtrace_closef = dtrace_getf_barrier; 15133 } 15134 } 15135 #endif 15136 15137 /* 15138 * Now it's time to actually fire the BEGIN probe. We need to disable 15139 * interrupts here both to record the CPU on which we fired the BEGIN 15140 * probe (the data from this CPU will be processed first at user 15141 * level) and to manually activate the buffer for this CPU. 15142 */ 15143 cookie = dtrace_interrupt_disable(); 15144 *cpu = curcpu; 15145 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 15146 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 15147 15148 dtrace_probe(dtrace_probeid_begin, 15149 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 15150 dtrace_interrupt_enable(cookie); 15151 /* 15152 * We may have had an exit action from a BEGIN probe; only change our 15153 * state to ACTIVE if we're still in WARMUP. 15154 */ 15155 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 15156 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 15157 15158 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 15159 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 15160 15161 #ifdef __FreeBSD__ 15162 /* 15163 * We enable anonymous tracing before APs are started, so we must 15164 * activate buffers using the current CPU. 15165 */ 15166 if (state == dtrace_anon.dta_state) 15167 for (int i = 0; i < NCPU; i++) 15168 dtrace_buffer_activate_cpu(state, i); 15169 else 15170 dtrace_xcall(DTRACE_CPUALL, 15171 (dtrace_xcall_t)dtrace_buffer_activate, state); 15172 #else 15173 /* 15174 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 15175 * want each CPU to transition its principal buffer out of the 15176 * INACTIVE state. Doing this assures that no CPU will suddenly begin 15177 * processing an ECB halfway down a probe's ECB chain; all CPUs will 15178 * atomically transition from processing none of a state's ECBs to 15179 * processing all of them. 15180 */ 15181 dtrace_xcall(DTRACE_CPUALL, 15182 (dtrace_xcall_t)dtrace_buffer_activate, state); 15183 #endif 15184 goto out; 15185 15186 err: 15187 dtrace_buffer_free(state->dts_buffer); 15188 dtrace_buffer_free(state->dts_aggbuffer); 15189 15190 if ((nspec = state->dts_nspeculations) == 0) { 15191 ASSERT(state->dts_speculations == NULL); 15192 goto out; 15193 } 15194 15195 spec = state->dts_speculations; 15196 ASSERT(spec != NULL); 15197 15198 for (i = 0; i < state->dts_nspeculations; i++) { 15199 if ((buf = spec[i].dtsp_buffer) == NULL) 15200 break; 15201 15202 dtrace_buffer_free(buf); 15203 kmem_free(buf, bufsize); 15204 } 15205 15206 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15207 state->dts_nspeculations = 0; 15208 state->dts_speculations = NULL; 15209 15210 out: 15211 mutex_exit(&dtrace_lock); 15212 mutex_exit(&cpu_lock); 15213 15214 return (rval); 15215 } 15216 15217 static int 15218 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 15219 { 15220 dtrace_icookie_t cookie; 15221 15222 ASSERT(MUTEX_HELD(&dtrace_lock)); 15223 15224 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 15225 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 15226 return (EINVAL); 15227 15228 /* 15229 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 15230 * to be sure that every CPU has seen it. See below for the details 15231 * on why this is done. 15232 */ 15233 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 15234 dtrace_sync(); 15235 15236 /* 15237 * By this point, it is impossible for any CPU to be still processing 15238 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 15239 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 15240 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 15241 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 15242 * iff we're in the END probe. 15243 */ 15244 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 15245 dtrace_sync(); 15246 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 15247 15248 /* 15249 * Finally, we can release the reserve and call the END probe. We 15250 * disable interrupts across calling the END probe to allow us to 15251 * return the CPU on which we actually called the END probe. This 15252 * allows user-land to be sure that this CPU's principal buffer is 15253 * processed last. 15254 */ 15255 state->dts_reserve = 0; 15256 15257 cookie = dtrace_interrupt_disable(); 15258 *cpu = curcpu; 15259 dtrace_probe(dtrace_probeid_end, 15260 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 15261 dtrace_interrupt_enable(cookie); 15262 15263 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 15264 dtrace_sync(); 15265 15266 #ifdef illumos 15267 if (state->dts_getf != 0 && 15268 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 15269 /* 15270 * We don't have kernel privs but we have at least one call 15271 * to getf(); we need to lower our zone's count, and (if 15272 * this is the last enabling to have an unprivileged call 15273 * to getf()) we need to clear the closef() hook. 15274 */ 15275 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 15276 ASSERT(dtrace_closef == dtrace_getf_barrier); 15277 ASSERT(dtrace_getf > 0); 15278 15279 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 15280 15281 if (--dtrace_getf == 0) 15282 dtrace_closef = NULL; 15283 } 15284 #endif 15285 15286 return (0); 15287 } 15288 15289 static int 15290 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 15291 dtrace_optval_t val) 15292 { 15293 ASSERT(MUTEX_HELD(&dtrace_lock)); 15294 15295 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 15296 return (EBUSY); 15297 15298 if (option >= DTRACEOPT_MAX) 15299 return (EINVAL); 15300 15301 if (option != DTRACEOPT_CPU && val < 0) 15302 return (EINVAL); 15303 15304 switch (option) { 15305 case DTRACEOPT_DESTRUCTIVE: 15306 if (dtrace_destructive_disallow) 15307 return (EACCES); 15308 15309 state->dts_cred.dcr_destructive = 1; 15310 break; 15311 15312 case DTRACEOPT_BUFSIZE: 15313 case DTRACEOPT_DYNVARSIZE: 15314 case DTRACEOPT_AGGSIZE: 15315 case DTRACEOPT_SPECSIZE: 15316 case DTRACEOPT_STRSIZE: 15317 if (val < 0) 15318 return (EINVAL); 15319 15320 if (val >= LONG_MAX) { 15321 /* 15322 * If this is an otherwise negative value, set it to 15323 * the highest multiple of 128m less than LONG_MAX. 15324 * Technically, we're adjusting the size without 15325 * regard to the buffer resizing policy, but in fact, 15326 * this has no effect -- if we set the buffer size to 15327 * ~LONG_MAX and the buffer policy is ultimately set to 15328 * be "manual", the buffer allocation is guaranteed to 15329 * fail, if only because the allocation requires two 15330 * buffers. (We set the the size to the highest 15331 * multiple of 128m because it ensures that the size 15332 * will remain a multiple of a megabyte when 15333 * repeatedly halved -- all the way down to 15m.) 15334 */ 15335 val = LONG_MAX - (1 << 27) + 1; 15336 } 15337 } 15338 15339 state->dts_options[option] = val; 15340 15341 return (0); 15342 } 15343 15344 static void 15345 dtrace_state_destroy(dtrace_state_t *state) 15346 { 15347 dtrace_ecb_t *ecb; 15348 dtrace_vstate_t *vstate = &state->dts_vstate; 15349 #ifdef illumos 15350 minor_t minor = getminor(state->dts_dev); 15351 #endif 15352 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 15353 dtrace_speculation_t *spec = state->dts_speculations; 15354 int nspec = state->dts_nspeculations; 15355 uint32_t match; 15356 15357 ASSERT(MUTEX_HELD(&dtrace_lock)); 15358 ASSERT(MUTEX_HELD(&cpu_lock)); 15359 15360 /* 15361 * First, retract any retained enablings for this state. 15362 */ 15363 dtrace_enabling_retract(state); 15364 ASSERT(state->dts_nretained == 0); 15365 15366 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 15367 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 15368 /* 15369 * We have managed to come into dtrace_state_destroy() on a 15370 * hot enabling -- almost certainly because of a disorderly 15371 * shutdown of a consumer. (That is, a consumer that is 15372 * exiting without having called dtrace_stop().) In this case, 15373 * we're going to set our activity to be KILLED, and then 15374 * issue a sync to be sure that everyone is out of probe 15375 * context before we start blowing away ECBs. 15376 */ 15377 state->dts_activity = DTRACE_ACTIVITY_KILLED; 15378 dtrace_sync(); 15379 } 15380 15381 /* 15382 * Release the credential hold we took in dtrace_state_create(). 15383 */ 15384 if (state->dts_cred.dcr_cred != NULL) 15385 crfree(state->dts_cred.dcr_cred); 15386 15387 /* 15388 * Now we can safely disable and destroy any enabled probes. Because 15389 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 15390 * (especially if they're all enabled), we take two passes through the 15391 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 15392 * in the second we disable whatever is left over. 15393 */ 15394 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 15395 for (i = 0; i < state->dts_necbs; i++) { 15396 if ((ecb = state->dts_ecbs[i]) == NULL) 15397 continue; 15398 15399 if (match && ecb->dte_probe != NULL) { 15400 dtrace_probe_t *probe = ecb->dte_probe; 15401 dtrace_provider_t *prov = probe->dtpr_provider; 15402 15403 if (!(prov->dtpv_priv.dtpp_flags & match)) 15404 continue; 15405 } 15406 15407 dtrace_ecb_disable(ecb); 15408 dtrace_ecb_destroy(ecb); 15409 } 15410 15411 if (!match) 15412 break; 15413 } 15414 15415 /* 15416 * Before we free the buffers, perform one more sync to assure that 15417 * every CPU is out of probe context. 15418 */ 15419 dtrace_sync(); 15420 15421 dtrace_buffer_free(state->dts_buffer); 15422 dtrace_buffer_free(state->dts_aggbuffer); 15423 15424 for (i = 0; i < nspec; i++) 15425 dtrace_buffer_free(spec[i].dtsp_buffer); 15426 15427 #ifdef illumos 15428 if (state->dts_cleaner != CYCLIC_NONE) 15429 cyclic_remove(state->dts_cleaner); 15430 15431 if (state->dts_deadman != CYCLIC_NONE) 15432 cyclic_remove(state->dts_deadman); 15433 #else 15434 callout_stop(&state->dts_cleaner); 15435 callout_drain(&state->dts_cleaner); 15436 callout_stop(&state->dts_deadman); 15437 callout_drain(&state->dts_deadman); 15438 #endif 15439 15440 dtrace_dstate_fini(&vstate->dtvs_dynvars); 15441 dtrace_vstate_fini(vstate); 15442 if (state->dts_ecbs != NULL) 15443 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 15444 15445 if (state->dts_aggregations != NULL) { 15446 #ifdef DEBUG 15447 for (i = 0; i < state->dts_naggregations; i++) 15448 ASSERT(state->dts_aggregations[i] == NULL); 15449 #endif 15450 ASSERT(state->dts_naggregations > 0); 15451 kmem_free(state->dts_aggregations, 15452 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 15453 } 15454 15455 kmem_free(state->dts_buffer, bufsize); 15456 kmem_free(state->dts_aggbuffer, bufsize); 15457 15458 for (i = 0; i < nspec; i++) 15459 kmem_free(spec[i].dtsp_buffer, bufsize); 15460 15461 if (spec != NULL) 15462 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15463 15464 dtrace_format_destroy(state); 15465 15466 if (state->dts_aggid_arena != NULL) { 15467 #ifdef illumos 15468 vmem_destroy(state->dts_aggid_arena); 15469 #else 15470 delete_unrhdr(state->dts_aggid_arena); 15471 #endif 15472 state->dts_aggid_arena = NULL; 15473 } 15474 #ifdef illumos 15475 ddi_soft_state_free(dtrace_softstate, minor); 15476 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 15477 #endif 15478 } 15479 15480 /* 15481 * DTrace Anonymous Enabling Functions 15482 */ 15483 static dtrace_state_t * 15484 dtrace_anon_grab(void) 15485 { 15486 dtrace_state_t *state; 15487 15488 ASSERT(MUTEX_HELD(&dtrace_lock)); 15489 15490 if ((state = dtrace_anon.dta_state) == NULL) { 15491 ASSERT(dtrace_anon.dta_enabling == NULL); 15492 return (NULL); 15493 } 15494 15495 ASSERT(dtrace_anon.dta_enabling != NULL); 15496 ASSERT(dtrace_retained != NULL); 15497 15498 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 15499 dtrace_anon.dta_enabling = NULL; 15500 dtrace_anon.dta_state = NULL; 15501 15502 return (state); 15503 } 15504 15505 static void 15506 dtrace_anon_property(void) 15507 { 15508 int i, rv; 15509 dtrace_state_t *state; 15510 dof_hdr_t *dof; 15511 char c[32]; /* enough for "dof-data-" + digits */ 15512 15513 ASSERT(MUTEX_HELD(&dtrace_lock)); 15514 ASSERT(MUTEX_HELD(&cpu_lock)); 15515 15516 for (i = 0; ; i++) { 15517 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 15518 15519 dtrace_err_verbose = 1; 15520 15521 if ((dof = dtrace_dof_property(c)) == NULL) { 15522 dtrace_err_verbose = 0; 15523 break; 15524 } 15525 15526 #ifdef illumos 15527 /* 15528 * We want to create anonymous state, so we need to transition 15529 * the kernel debugger to indicate that DTrace is active. If 15530 * this fails (e.g. because the debugger has modified text in 15531 * some way), we won't continue with the processing. 15532 */ 15533 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15534 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 15535 "enabling ignored."); 15536 dtrace_dof_destroy(dof); 15537 break; 15538 } 15539 #endif 15540 15541 /* 15542 * If we haven't allocated an anonymous state, we'll do so now. 15543 */ 15544 if ((state = dtrace_anon.dta_state) == NULL) { 15545 state = dtrace_state_create(NULL, NULL); 15546 dtrace_anon.dta_state = state; 15547 15548 if (state == NULL) { 15549 /* 15550 * This basically shouldn't happen: the only 15551 * failure mode from dtrace_state_create() is a 15552 * failure of ddi_soft_state_zalloc() that 15553 * itself should never happen. Still, the 15554 * interface allows for a failure mode, and 15555 * we want to fail as gracefully as possible: 15556 * we'll emit an error message and cease 15557 * processing anonymous state in this case. 15558 */ 15559 cmn_err(CE_WARN, "failed to create " 15560 "anonymous state"); 15561 dtrace_dof_destroy(dof); 15562 break; 15563 } 15564 } 15565 15566 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 15567 &dtrace_anon.dta_enabling, 0, B_TRUE); 15568 15569 if (rv == 0) 15570 rv = dtrace_dof_options(dof, state); 15571 15572 dtrace_err_verbose = 0; 15573 dtrace_dof_destroy(dof); 15574 15575 if (rv != 0) { 15576 /* 15577 * This is malformed DOF; chuck any anonymous state 15578 * that we created. 15579 */ 15580 ASSERT(dtrace_anon.dta_enabling == NULL); 15581 dtrace_state_destroy(state); 15582 dtrace_anon.dta_state = NULL; 15583 break; 15584 } 15585 15586 ASSERT(dtrace_anon.dta_enabling != NULL); 15587 } 15588 15589 if (dtrace_anon.dta_enabling != NULL) { 15590 int rval; 15591 15592 /* 15593 * dtrace_enabling_retain() can only fail because we are 15594 * trying to retain more enablings than are allowed -- but 15595 * we only have one anonymous enabling, and we are guaranteed 15596 * to be allowed at least one retained enabling; we assert 15597 * that dtrace_enabling_retain() returns success. 15598 */ 15599 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 15600 ASSERT(rval == 0); 15601 15602 dtrace_enabling_dump(dtrace_anon.dta_enabling); 15603 } 15604 } 15605 15606 /* 15607 * DTrace Helper Functions 15608 */ 15609 static void 15610 dtrace_helper_trace(dtrace_helper_action_t *helper, 15611 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 15612 { 15613 uint32_t size, next, nnext, i; 15614 dtrace_helptrace_t *ent, *buffer; 15615 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 15616 15617 if ((buffer = dtrace_helptrace_buffer) == NULL) 15618 return; 15619 15620 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 15621 15622 /* 15623 * What would a tracing framework be without its own tracing 15624 * framework? (Well, a hell of a lot simpler, for starters...) 15625 */ 15626 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 15627 sizeof (uint64_t) - sizeof (uint64_t); 15628 15629 /* 15630 * Iterate until we can allocate a slot in the trace buffer. 15631 */ 15632 do { 15633 next = dtrace_helptrace_next; 15634 15635 if (next + size < dtrace_helptrace_bufsize) { 15636 nnext = next + size; 15637 } else { 15638 nnext = size; 15639 } 15640 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 15641 15642 /* 15643 * We have our slot; fill it in. 15644 */ 15645 if (nnext == size) { 15646 dtrace_helptrace_wrapped++; 15647 next = 0; 15648 } 15649 15650 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next); 15651 ent->dtht_helper = helper; 15652 ent->dtht_where = where; 15653 ent->dtht_nlocals = vstate->dtvs_nlocals; 15654 15655 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 15656 mstate->dtms_fltoffs : -1; 15657 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 15658 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 15659 15660 for (i = 0; i < vstate->dtvs_nlocals; i++) { 15661 dtrace_statvar_t *svar; 15662 15663 if ((svar = vstate->dtvs_locals[i]) == NULL) 15664 continue; 15665 15666 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 15667 ent->dtht_locals[i] = 15668 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 15669 } 15670 } 15671 15672 static uint64_t 15673 dtrace_helper(int which, dtrace_mstate_t *mstate, 15674 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 15675 { 15676 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 15677 uint64_t sarg0 = mstate->dtms_arg[0]; 15678 uint64_t sarg1 = mstate->dtms_arg[1]; 15679 uint64_t rval = 0; 15680 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 15681 dtrace_helper_action_t *helper; 15682 dtrace_vstate_t *vstate; 15683 dtrace_difo_t *pred; 15684 int i, trace = dtrace_helptrace_buffer != NULL; 15685 15686 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 15687 15688 if (helpers == NULL) 15689 return (0); 15690 15691 if ((helper = helpers->dthps_actions[which]) == NULL) 15692 return (0); 15693 15694 vstate = &helpers->dthps_vstate; 15695 mstate->dtms_arg[0] = arg0; 15696 mstate->dtms_arg[1] = arg1; 15697 15698 /* 15699 * Now iterate over each helper. If its predicate evaluates to 'true', 15700 * we'll call the corresponding actions. Note that the below calls 15701 * to dtrace_dif_emulate() may set faults in machine state. This is 15702 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 15703 * the stored DIF offset with its own (which is the desired behavior). 15704 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 15705 * from machine state; this is okay, too. 15706 */ 15707 for (; helper != NULL; helper = helper->dtha_next) { 15708 if ((pred = helper->dtha_predicate) != NULL) { 15709 if (trace) 15710 dtrace_helper_trace(helper, mstate, vstate, 0); 15711 15712 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 15713 goto next; 15714 15715 if (*flags & CPU_DTRACE_FAULT) 15716 goto err; 15717 } 15718 15719 for (i = 0; i < helper->dtha_nactions; i++) { 15720 if (trace) 15721 dtrace_helper_trace(helper, 15722 mstate, vstate, i + 1); 15723 15724 rval = dtrace_dif_emulate(helper->dtha_actions[i], 15725 mstate, vstate, state); 15726 15727 if (*flags & CPU_DTRACE_FAULT) 15728 goto err; 15729 } 15730 15731 next: 15732 if (trace) 15733 dtrace_helper_trace(helper, mstate, vstate, 15734 DTRACE_HELPTRACE_NEXT); 15735 } 15736 15737 if (trace) 15738 dtrace_helper_trace(helper, mstate, vstate, 15739 DTRACE_HELPTRACE_DONE); 15740 15741 /* 15742 * Restore the arg0 that we saved upon entry. 15743 */ 15744 mstate->dtms_arg[0] = sarg0; 15745 mstate->dtms_arg[1] = sarg1; 15746 15747 return (rval); 15748 15749 err: 15750 if (trace) 15751 dtrace_helper_trace(helper, mstate, vstate, 15752 DTRACE_HELPTRACE_ERR); 15753 15754 /* 15755 * Restore the arg0 that we saved upon entry. 15756 */ 15757 mstate->dtms_arg[0] = sarg0; 15758 mstate->dtms_arg[1] = sarg1; 15759 15760 return (0); 15761 } 15762 15763 static void 15764 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 15765 dtrace_vstate_t *vstate) 15766 { 15767 int i; 15768 15769 if (helper->dtha_predicate != NULL) 15770 dtrace_difo_release(helper->dtha_predicate, vstate); 15771 15772 for (i = 0; i < helper->dtha_nactions; i++) { 15773 ASSERT(helper->dtha_actions[i] != NULL); 15774 dtrace_difo_release(helper->dtha_actions[i], vstate); 15775 } 15776 15777 kmem_free(helper->dtha_actions, 15778 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 15779 kmem_free(helper, sizeof (dtrace_helper_action_t)); 15780 } 15781 15782 static int 15783 dtrace_helper_destroygen(dtrace_helpers_t *help, int gen) 15784 { 15785 proc_t *p = curproc; 15786 dtrace_vstate_t *vstate; 15787 int i; 15788 15789 if (help == NULL) 15790 help = p->p_dtrace_helpers; 15791 15792 ASSERT(MUTEX_HELD(&dtrace_lock)); 15793 15794 if (help == NULL || gen > help->dthps_generation) 15795 return (EINVAL); 15796 15797 vstate = &help->dthps_vstate; 15798 15799 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15800 dtrace_helper_action_t *last = NULL, *h, *next; 15801 15802 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15803 next = h->dtha_next; 15804 15805 if (h->dtha_generation == gen) { 15806 if (last != NULL) { 15807 last->dtha_next = next; 15808 } else { 15809 help->dthps_actions[i] = next; 15810 } 15811 15812 dtrace_helper_action_destroy(h, vstate); 15813 } else { 15814 last = h; 15815 } 15816 } 15817 } 15818 15819 /* 15820 * Interate until we've cleared out all helper providers with the 15821 * given generation number. 15822 */ 15823 for (;;) { 15824 dtrace_helper_provider_t *prov; 15825 15826 /* 15827 * Look for a helper provider with the right generation. We 15828 * have to start back at the beginning of the list each time 15829 * because we drop dtrace_lock. It's unlikely that we'll make 15830 * more than two passes. 15831 */ 15832 for (i = 0; i < help->dthps_nprovs; i++) { 15833 prov = help->dthps_provs[i]; 15834 15835 if (prov->dthp_generation == gen) 15836 break; 15837 } 15838 15839 /* 15840 * If there were no matches, we're done. 15841 */ 15842 if (i == help->dthps_nprovs) 15843 break; 15844 15845 /* 15846 * Move the last helper provider into this slot. 15847 */ 15848 help->dthps_nprovs--; 15849 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 15850 help->dthps_provs[help->dthps_nprovs] = NULL; 15851 15852 mutex_exit(&dtrace_lock); 15853 15854 /* 15855 * If we have a meta provider, remove this helper provider. 15856 */ 15857 mutex_enter(&dtrace_meta_lock); 15858 if (dtrace_meta_pid != NULL) { 15859 ASSERT(dtrace_deferred_pid == NULL); 15860 dtrace_helper_provider_remove(&prov->dthp_prov, 15861 p->p_pid); 15862 } 15863 mutex_exit(&dtrace_meta_lock); 15864 15865 dtrace_helper_provider_destroy(prov); 15866 15867 mutex_enter(&dtrace_lock); 15868 } 15869 15870 return (0); 15871 } 15872 15873 static int 15874 dtrace_helper_validate(dtrace_helper_action_t *helper) 15875 { 15876 int err = 0, i; 15877 dtrace_difo_t *dp; 15878 15879 if ((dp = helper->dtha_predicate) != NULL) 15880 err += dtrace_difo_validate_helper(dp); 15881 15882 for (i = 0; i < helper->dtha_nactions; i++) 15883 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 15884 15885 return (err == 0); 15886 } 15887 15888 static int 15889 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep, 15890 dtrace_helpers_t *help) 15891 { 15892 dtrace_helper_action_t *helper, *last; 15893 dtrace_actdesc_t *act; 15894 dtrace_vstate_t *vstate; 15895 dtrace_predicate_t *pred; 15896 int count = 0, nactions = 0, i; 15897 15898 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 15899 return (EINVAL); 15900 15901 last = help->dthps_actions[which]; 15902 vstate = &help->dthps_vstate; 15903 15904 for (count = 0; last != NULL; last = last->dtha_next) { 15905 count++; 15906 if (last->dtha_next == NULL) 15907 break; 15908 } 15909 15910 /* 15911 * If we already have dtrace_helper_actions_max helper actions for this 15912 * helper action type, we'll refuse to add a new one. 15913 */ 15914 if (count >= dtrace_helper_actions_max) 15915 return (ENOSPC); 15916 15917 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 15918 helper->dtha_generation = help->dthps_generation; 15919 15920 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 15921 ASSERT(pred->dtp_difo != NULL); 15922 dtrace_difo_hold(pred->dtp_difo); 15923 helper->dtha_predicate = pred->dtp_difo; 15924 } 15925 15926 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 15927 if (act->dtad_kind != DTRACEACT_DIFEXPR) 15928 goto err; 15929 15930 if (act->dtad_difo == NULL) 15931 goto err; 15932 15933 nactions++; 15934 } 15935 15936 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 15937 (helper->dtha_nactions = nactions), KM_SLEEP); 15938 15939 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 15940 dtrace_difo_hold(act->dtad_difo); 15941 helper->dtha_actions[i++] = act->dtad_difo; 15942 } 15943 15944 if (!dtrace_helper_validate(helper)) 15945 goto err; 15946 15947 if (last == NULL) { 15948 help->dthps_actions[which] = helper; 15949 } else { 15950 last->dtha_next = helper; 15951 } 15952 15953 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 15954 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 15955 dtrace_helptrace_next = 0; 15956 } 15957 15958 return (0); 15959 err: 15960 dtrace_helper_action_destroy(helper, vstate); 15961 return (EINVAL); 15962 } 15963 15964 static void 15965 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 15966 dof_helper_t *dofhp) 15967 { 15968 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 15969 15970 mutex_enter(&dtrace_meta_lock); 15971 mutex_enter(&dtrace_lock); 15972 15973 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 15974 /* 15975 * If the dtrace module is loaded but not attached, or if 15976 * there aren't isn't a meta provider registered to deal with 15977 * these provider descriptions, we need to postpone creating 15978 * the actual providers until later. 15979 */ 15980 15981 if (help->dthps_next == NULL && help->dthps_prev == NULL && 15982 dtrace_deferred_pid != help) { 15983 help->dthps_deferred = 1; 15984 help->dthps_pid = p->p_pid; 15985 help->dthps_next = dtrace_deferred_pid; 15986 help->dthps_prev = NULL; 15987 if (dtrace_deferred_pid != NULL) 15988 dtrace_deferred_pid->dthps_prev = help; 15989 dtrace_deferred_pid = help; 15990 } 15991 15992 mutex_exit(&dtrace_lock); 15993 15994 } else if (dofhp != NULL) { 15995 /* 15996 * If the dtrace module is loaded and we have a particular 15997 * helper provider description, pass that off to the 15998 * meta provider. 15999 */ 16000 16001 mutex_exit(&dtrace_lock); 16002 16003 dtrace_helper_provide(dofhp, p->p_pid); 16004 16005 } else { 16006 /* 16007 * Otherwise, just pass all the helper provider descriptions 16008 * off to the meta provider. 16009 */ 16010 16011 int i; 16012 mutex_exit(&dtrace_lock); 16013 16014 for (i = 0; i < help->dthps_nprovs; i++) { 16015 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 16016 p->p_pid); 16017 } 16018 } 16019 16020 mutex_exit(&dtrace_meta_lock); 16021 } 16022 16023 static int 16024 dtrace_helper_provider_add(dof_helper_t *dofhp, dtrace_helpers_t *help, int gen) 16025 { 16026 dtrace_helper_provider_t *hprov, **tmp_provs; 16027 uint_t tmp_maxprovs, i; 16028 16029 ASSERT(MUTEX_HELD(&dtrace_lock)); 16030 ASSERT(help != NULL); 16031 16032 /* 16033 * If we already have dtrace_helper_providers_max helper providers, 16034 * we're refuse to add a new one. 16035 */ 16036 if (help->dthps_nprovs >= dtrace_helper_providers_max) 16037 return (ENOSPC); 16038 16039 /* 16040 * Check to make sure this isn't a duplicate. 16041 */ 16042 for (i = 0; i < help->dthps_nprovs; i++) { 16043 if (dofhp->dofhp_addr == 16044 help->dthps_provs[i]->dthp_prov.dofhp_addr) 16045 return (EALREADY); 16046 } 16047 16048 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 16049 hprov->dthp_prov = *dofhp; 16050 hprov->dthp_ref = 1; 16051 hprov->dthp_generation = gen; 16052 16053 /* 16054 * Allocate a bigger table for helper providers if it's already full. 16055 */ 16056 if (help->dthps_maxprovs == help->dthps_nprovs) { 16057 tmp_maxprovs = help->dthps_maxprovs; 16058 tmp_provs = help->dthps_provs; 16059 16060 if (help->dthps_maxprovs == 0) 16061 help->dthps_maxprovs = 2; 16062 else 16063 help->dthps_maxprovs *= 2; 16064 if (help->dthps_maxprovs > dtrace_helper_providers_max) 16065 help->dthps_maxprovs = dtrace_helper_providers_max; 16066 16067 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 16068 16069 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 16070 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16071 16072 if (tmp_provs != NULL) { 16073 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 16074 sizeof (dtrace_helper_provider_t *)); 16075 kmem_free(tmp_provs, tmp_maxprovs * 16076 sizeof (dtrace_helper_provider_t *)); 16077 } 16078 } 16079 16080 help->dthps_provs[help->dthps_nprovs] = hprov; 16081 help->dthps_nprovs++; 16082 16083 return (0); 16084 } 16085 16086 static void 16087 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 16088 { 16089 mutex_enter(&dtrace_lock); 16090 16091 if (--hprov->dthp_ref == 0) { 16092 dof_hdr_t *dof; 16093 mutex_exit(&dtrace_lock); 16094 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 16095 dtrace_dof_destroy(dof); 16096 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 16097 } else { 16098 mutex_exit(&dtrace_lock); 16099 } 16100 } 16101 16102 static int 16103 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 16104 { 16105 uintptr_t daddr = (uintptr_t)dof; 16106 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 16107 dof_provider_t *provider; 16108 dof_probe_t *probe; 16109 uint8_t *arg; 16110 char *strtab, *typestr; 16111 dof_stridx_t typeidx; 16112 size_t typesz; 16113 uint_t nprobes, j, k; 16114 16115 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 16116 16117 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 16118 dtrace_dof_error(dof, "misaligned section offset"); 16119 return (-1); 16120 } 16121 16122 /* 16123 * The section needs to be large enough to contain the DOF provider 16124 * structure appropriate for the given version. 16125 */ 16126 if (sec->dofs_size < 16127 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 16128 offsetof(dof_provider_t, dofpv_prenoffs) : 16129 sizeof (dof_provider_t))) { 16130 dtrace_dof_error(dof, "provider section too small"); 16131 return (-1); 16132 } 16133 16134 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 16135 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 16136 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 16137 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 16138 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 16139 16140 if (str_sec == NULL || prb_sec == NULL || 16141 arg_sec == NULL || off_sec == NULL) 16142 return (-1); 16143 16144 enoff_sec = NULL; 16145 16146 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 16147 provider->dofpv_prenoffs != DOF_SECT_NONE && 16148 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 16149 provider->dofpv_prenoffs)) == NULL) 16150 return (-1); 16151 16152 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 16153 16154 if (provider->dofpv_name >= str_sec->dofs_size || 16155 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 16156 dtrace_dof_error(dof, "invalid provider name"); 16157 return (-1); 16158 } 16159 16160 if (prb_sec->dofs_entsize == 0 || 16161 prb_sec->dofs_entsize > prb_sec->dofs_size) { 16162 dtrace_dof_error(dof, "invalid entry size"); 16163 return (-1); 16164 } 16165 16166 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 16167 dtrace_dof_error(dof, "misaligned entry size"); 16168 return (-1); 16169 } 16170 16171 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 16172 dtrace_dof_error(dof, "invalid entry size"); 16173 return (-1); 16174 } 16175 16176 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 16177 dtrace_dof_error(dof, "misaligned section offset"); 16178 return (-1); 16179 } 16180 16181 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 16182 dtrace_dof_error(dof, "invalid entry size"); 16183 return (-1); 16184 } 16185 16186 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 16187 16188 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 16189 16190 /* 16191 * Take a pass through the probes to check for errors. 16192 */ 16193 for (j = 0; j < nprobes; j++) { 16194 probe = (dof_probe_t *)(uintptr_t)(daddr + 16195 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 16196 16197 if (probe->dofpr_func >= str_sec->dofs_size) { 16198 dtrace_dof_error(dof, "invalid function name"); 16199 return (-1); 16200 } 16201 16202 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 16203 dtrace_dof_error(dof, "function name too long"); 16204 /* 16205 * Keep going if the function name is too long. 16206 * Unlike provider and probe names, we cannot reasonably 16207 * impose restrictions on function names, since they're 16208 * a property of the code being instrumented. We will 16209 * skip this probe in dtrace_helper_provide_one(). 16210 */ 16211 } 16212 16213 if (probe->dofpr_name >= str_sec->dofs_size || 16214 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 16215 dtrace_dof_error(dof, "invalid probe name"); 16216 return (-1); 16217 } 16218 16219 /* 16220 * The offset count must not wrap the index, and the offsets 16221 * must also not overflow the section's data. 16222 */ 16223 if (probe->dofpr_offidx + probe->dofpr_noffs < 16224 probe->dofpr_offidx || 16225 (probe->dofpr_offidx + probe->dofpr_noffs) * 16226 off_sec->dofs_entsize > off_sec->dofs_size) { 16227 dtrace_dof_error(dof, "invalid probe offset"); 16228 return (-1); 16229 } 16230 16231 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 16232 /* 16233 * If there's no is-enabled offset section, make sure 16234 * there aren't any is-enabled offsets. Otherwise 16235 * perform the same checks as for probe offsets 16236 * (immediately above). 16237 */ 16238 if (enoff_sec == NULL) { 16239 if (probe->dofpr_enoffidx != 0 || 16240 probe->dofpr_nenoffs != 0) { 16241 dtrace_dof_error(dof, "is-enabled " 16242 "offsets with null section"); 16243 return (-1); 16244 } 16245 } else if (probe->dofpr_enoffidx + 16246 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 16247 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 16248 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 16249 dtrace_dof_error(dof, "invalid is-enabled " 16250 "offset"); 16251 return (-1); 16252 } 16253 16254 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 16255 dtrace_dof_error(dof, "zero probe and " 16256 "is-enabled offsets"); 16257 return (-1); 16258 } 16259 } else if (probe->dofpr_noffs == 0) { 16260 dtrace_dof_error(dof, "zero probe offsets"); 16261 return (-1); 16262 } 16263 16264 if (probe->dofpr_argidx + probe->dofpr_xargc < 16265 probe->dofpr_argidx || 16266 (probe->dofpr_argidx + probe->dofpr_xargc) * 16267 arg_sec->dofs_entsize > arg_sec->dofs_size) { 16268 dtrace_dof_error(dof, "invalid args"); 16269 return (-1); 16270 } 16271 16272 typeidx = probe->dofpr_nargv; 16273 typestr = strtab + probe->dofpr_nargv; 16274 for (k = 0; k < probe->dofpr_nargc; k++) { 16275 if (typeidx >= str_sec->dofs_size) { 16276 dtrace_dof_error(dof, "bad " 16277 "native argument type"); 16278 return (-1); 16279 } 16280 16281 typesz = strlen(typestr) + 1; 16282 if (typesz > DTRACE_ARGTYPELEN) { 16283 dtrace_dof_error(dof, "native " 16284 "argument type too long"); 16285 return (-1); 16286 } 16287 typeidx += typesz; 16288 typestr += typesz; 16289 } 16290 16291 typeidx = probe->dofpr_xargv; 16292 typestr = strtab + probe->dofpr_xargv; 16293 for (k = 0; k < probe->dofpr_xargc; k++) { 16294 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 16295 dtrace_dof_error(dof, "bad " 16296 "native argument index"); 16297 return (-1); 16298 } 16299 16300 if (typeidx >= str_sec->dofs_size) { 16301 dtrace_dof_error(dof, "bad " 16302 "translated argument type"); 16303 return (-1); 16304 } 16305 16306 typesz = strlen(typestr) + 1; 16307 if (typesz > DTRACE_ARGTYPELEN) { 16308 dtrace_dof_error(dof, "translated argument " 16309 "type too long"); 16310 return (-1); 16311 } 16312 16313 typeidx += typesz; 16314 typestr += typesz; 16315 } 16316 } 16317 16318 return (0); 16319 } 16320 16321 static int 16322 #ifdef __FreeBSD__ 16323 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp, struct proc *p) 16324 #else 16325 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 16326 #endif 16327 { 16328 dtrace_helpers_t *help; 16329 dtrace_vstate_t *vstate; 16330 dtrace_enabling_t *enab = NULL; 16331 #ifndef __FreeBSD__ 16332 proc_t *p = curproc; 16333 #endif 16334 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 16335 uintptr_t daddr = (uintptr_t)dof; 16336 16337 ASSERT(MUTEX_HELD(&dtrace_lock)); 16338 16339 if ((help = p->p_dtrace_helpers) == NULL) 16340 help = dtrace_helpers_create(p); 16341 16342 vstate = &help->dthps_vstate; 16343 16344 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 16345 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 16346 dtrace_dof_destroy(dof); 16347 return (rv); 16348 } 16349 16350 /* 16351 * Look for helper providers and validate their descriptions. 16352 */ 16353 if (dhp != NULL) { 16354 for (i = 0; i < dof->dofh_secnum; i++) { 16355 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 16356 dof->dofh_secoff + i * dof->dofh_secsize); 16357 16358 if (sec->dofs_type != DOF_SECT_PROVIDER) 16359 continue; 16360 16361 if (dtrace_helper_provider_validate(dof, sec) != 0) { 16362 dtrace_enabling_destroy(enab); 16363 dtrace_dof_destroy(dof); 16364 return (-1); 16365 } 16366 16367 nprovs++; 16368 } 16369 } 16370 16371 /* 16372 * Now we need to walk through the ECB descriptions in the enabling. 16373 */ 16374 for (i = 0; i < enab->dten_ndesc; i++) { 16375 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 16376 dtrace_probedesc_t *desc = &ep->dted_probe; 16377 16378 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 16379 continue; 16380 16381 if (strcmp(desc->dtpd_mod, "helper") != 0) 16382 continue; 16383 16384 if (strcmp(desc->dtpd_func, "ustack") != 0) 16385 continue; 16386 16387 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 16388 ep, help)) != 0) { 16389 /* 16390 * Adding this helper action failed -- we are now going 16391 * to rip out the entire generation and return failure. 16392 */ 16393 (void) dtrace_helper_destroygen(help, 16394 help->dthps_generation); 16395 dtrace_enabling_destroy(enab); 16396 dtrace_dof_destroy(dof); 16397 return (-1); 16398 } 16399 16400 nhelpers++; 16401 } 16402 16403 if (nhelpers < enab->dten_ndesc) 16404 dtrace_dof_error(dof, "unmatched helpers"); 16405 16406 gen = help->dthps_generation++; 16407 dtrace_enabling_destroy(enab); 16408 16409 if (dhp != NULL && nprovs > 0) { 16410 /* 16411 * Now that this is in-kernel, we change the sense of the 16412 * members: dofhp_dof denotes the in-kernel copy of the DOF 16413 * and dofhp_addr denotes the address at user-level. 16414 */ 16415 dhp->dofhp_addr = dhp->dofhp_dof; 16416 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 16417 16418 if (dtrace_helper_provider_add(dhp, help, gen) == 0) { 16419 mutex_exit(&dtrace_lock); 16420 dtrace_helper_provider_register(p, help, dhp); 16421 mutex_enter(&dtrace_lock); 16422 16423 destroy = 0; 16424 } 16425 } 16426 16427 if (destroy) 16428 dtrace_dof_destroy(dof); 16429 16430 return (gen); 16431 } 16432 16433 static dtrace_helpers_t * 16434 dtrace_helpers_create(proc_t *p) 16435 { 16436 dtrace_helpers_t *help; 16437 16438 ASSERT(MUTEX_HELD(&dtrace_lock)); 16439 ASSERT(p->p_dtrace_helpers == NULL); 16440 16441 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 16442 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 16443 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 16444 16445 p->p_dtrace_helpers = help; 16446 dtrace_helpers++; 16447 16448 return (help); 16449 } 16450 16451 #ifdef illumos 16452 static 16453 #endif 16454 void 16455 dtrace_helpers_destroy(proc_t *p) 16456 { 16457 dtrace_helpers_t *help; 16458 dtrace_vstate_t *vstate; 16459 #ifdef illumos 16460 proc_t *p = curproc; 16461 #endif 16462 int i; 16463 16464 mutex_enter(&dtrace_lock); 16465 16466 ASSERT(p->p_dtrace_helpers != NULL); 16467 ASSERT(dtrace_helpers > 0); 16468 16469 help = p->p_dtrace_helpers; 16470 vstate = &help->dthps_vstate; 16471 16472 /* 16473 * We're now going to lose the help from this process. 16474 */ 16475 p->p_dtrace_helpers = NULL; 16476 dtrace_sync(); 16477 16478 /* 16479 * Destory the helper actions. 16480 */ 16481 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16482 dtrace_helper_action_t *h, *next; 16483 16484 for (h = help->dthps_actions[i]; h != NULL; h = next) { 16485 next = h->dtha_next; 16486 dtrace_helper_action_destroy(h, vstate); 16487 h = next; 16488 } 16489 } 16490 16491 mutex_exit(&dtrace_lock); 16492 16493 /* 16494 * Destroy the helper providers. 16495 */ 16496 if (help->dthps_maxprovs > 0) { 16497 mutex_enter(&dtrace_meta_lock); 16498 if (dtrace_meta_pid != NULL) { 16499 ASSERT(dtrace_deferred_pid == NULL); 16500 16501 for (i = 0; i < help->dthps_nprovs; i++) { 16502 dtrace_helper_provider_remove( 16503 &help->dthps_provs[i]->dthp_prov, p->p_pid); 16504 } 16505 } else { 16506 mutex_enter(&dtrace_lock); 16507 ASSERT(help->dthps_deferred == 0 || 16508 help->dthps_next != NULL || 16509 help->dthps_prev != NULL || 16510 help == dtrace_deferred_pid); 16511 16512 /* 16513 * Remove the helper from the deferred list. 16514 */ 16515 if (help->dthps_next != NULL) 16516 help->dthps_next->dthps_prev = help->dthps_prev; 16517 if (help->dthps_prev != NULL) 16518 help->dthps_prev->dthps_next = help->dthps_next; 16519 if (dtrace_deferred_pid == help) { 16520 dtrace_deferred_pid = help->dthps_next; 16521 ASSERT(help->dthps_prev == NULL); 16522 } 16523 16524 mutex_exit(&dtrace_lock); 16525 } 16526 16527 mutex_exit(&dtrace_meta_lock); 16528 16529 for (i = 0; i < help->dthps_nprovs; i++) { 16530 dtrace_helper_provider_destroy(help->dthps_provs[i]); 16531 } 16532 16533 kmem_free(help->dthps_provs, help->dthps_maxprovs * 16534 sizeof (dtrace_helper_provider_t *)); 16535 } 16536 16537 mutex_enter(&dtrace_lock); 16538 16539 dtrace_vstate_fini(&help->dthps_vstate); 16540 kmem_free(help->dthps_actions, 16541 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 16542 kmem_free(help, sizeof (dtrace_helpers_t)); 16543 16544 --dtrace_helpers; 16545 mutex_exit(&dtrace_lock); 16546 } 16547 16548 #ifdef illumos 16549 static 16550 #endif 16551 void 16552 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 16553 { 16554 dtrace_helpers_t *help, *newhelp; 16555 dtrace_helper_action_t *helper, *new, *last; 16556 dtrace_difo_t *dp; 16557 dtrace_vstate_t *vstate; 16558 int i, j, sz, hasprovs = 0; 16559 16560 mutex_enter(&dtrace_lock); 16561 ASSERT(from->p_dtrace_helpers != NULL); 16562 ASSERT(dtrace_helpers > 0); 16563 16564 help = from->p_dtrace_helpers; 16565 newhelp = dtrace_helpers_create(to); 16566 ASSERT(to->p_dtrace_helpers != NULL); 16567 16568 newhelp->dthps_generation = help->dthps_generation; 16569 vstate = &newhelp->dthps_vstate; 16570 16571 /* 16572 * Duplicate the helper actions. 16573 */ 16574 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16575 if ((helper = help->dthps_actions[i]) == NULL) 16576 continue; 16577 16578 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 16579 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 16580 KM_SLEEP); 16581 new->dtha_generation = helper->dtha_generation; 16582 16583 if ((dp = helper->dtha_predicate) != NULL) { 16584 dp = dtrace_difo_duplicate(dp, vstate); 16585 new->dtha_predicate = dp; 16586 } 16587 16588 new->dtha_nactions = helper->dtha_nactions; 16589 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 16590 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 16591 16592 for (j = 0; j < new->dtha_nactions; j++) { 16593 dtrace_difo_t *dp = helper->dtha_actions[j]; 16594 16595 ASSERT(dp != NULL); 16596 dp = dtrace_difo_duplicate(dp, vstate); 16597 new->dtha_actions[j] = dp; 16598 } 16599 16600 if (last != NULL) { 16601 last->dtha_next = new; 16602 } else { 16603 newhelp->dthps_actions[i] = new; 16604 } 16605 16606 last = new; 16607 } 16608 } 16609 16610 /* 16611 * Duplicate the helper providers and register them with the 16612 * DTrace framework. 16613 */ 16614 if (help->dthps_nprovs > 0) { 16615 newhelp->dthps_nprovs = help->dthps_nprovs; 16616 newhelp->dthps_maxprovs = help->dthps_nprovs; 16617 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 16618 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16619 for (i = 0; i < newhelp->dthps_nprovs; i++) { 16620 newhelp->dthps_provs[i] = help->dthps_provs[i]; 16621 newhelp->dthps_provs[i]->dthp_ref++; 16622 } 16623 16624 hasprovs = 1; 16625 } 16626 16627 mutex_exit(&dtrace_lock); 16628 16629 if (hasprovs) 16630 dtrace_helper_provider_register(to, newhelp, NULL); 16631 } 16632 16633 /* 16634 * DTrace Hook Functions 16635 */ 16636 static void 16637 dtrace_module_loaded(modctl_t *ctl) 16638 { 16639 dtrace_provider_t *prv; 16640 16641 mutex_enter(&dtrace_provider_lock); 16642 #ifdef illumos 16643 mutex_enter(&mod_lock); 16644 #endif 16645 16646 #ifdef illumos 16647 ASSERT(ctl->mod_busy); 16648 #endif 16649 16650 /* 16651 * We're going to call each providers per-module provide operation 16652 * specifying only this module. 16653 */ 16654 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 16655 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 16656 16657 #ifdef illumos 16658 mutex_exit(&mod_lock); 16659 #endif 16660 mutex_exit(&dtrace_provider_lock); 16661 16662 /* 16663 * If we have any retained enablings, we need to match against them. 16664 * Enabling probes requires that cpu_lock be held, and we cannot hold 16665 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 16666 * module. (In particular, this happens when loading scheduling 16667 * classes.) So if we have any retained enablings, we need to dispatch 16668 * our task queue to do the match for us. 16669 */ 16670 mutex_enter(&dtrace_lock); 16671 16672 if (dtrace_retained == NULL) { 16673 mutex_exit(&dtrace_lock); 16674 return; 16675 } 16676 16677 (void) taskq_dispatch(dtrace_taskq, 16678 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 16679 16680 mutex_exit(&dtrace_lock); 16681 16682 /* 16683 * And now, for a little heuristic sleaze: in general, we want to 16684 * match modules as soon as they load. However, we cannot guarantee 16685 * this, because it would lead us to the lock ordering violation 16686 * outlined above. The common case, of course, is that cpu_lock is 16687 * _not_ held -- so we delay here for a clock tick, hoping that that's 16688 * long enough for the task queue to do its work. If it's not, it's 16689 * not a serious problem -- it just means that the module that we 16690 * just loaded may not be immediately instrumentable. 16691 */ 16692 delay(1); 16693 } 16694 16695 static void 16696 #ifdef illumos 16697 dtrace_module_unloaded(modctl_t *ctl) 16698 #else 16699 dtrace_module_unloaded(modctl_t *ctl, int *error) 16700 #endif 16701 { 16702 dtrace_probe_t template, *probe, *first, *next; 16703 dtrace_provider_t *prov; 16704 #ifndef illumos 16705 char modname[DTRACE_MODNAMELEN]; 16706 size_t len; 16707 #endif 16708 16709 #ifdef illumos 16710 template.dtpr_mod = ctl->mod_modname; 16711 #else 16712 /* Handle the fact that ctl->filename may end in ".ko". */ 16713 strlcpy(modname, ctl->filename, sizeof(modname)); 16714 len = strlen(ctl->filename); 16715 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0) 16716 modname[len - 3] = '\0'; 16717 template.dtpr_mod = modname; 16718 #endif 16719 16720 mutex_enter(&dtrace_provider_lock); 16721 #ifdef illumos 16722 mutex_enter(&mod_lock); 16723 #endif 16724 mutex_enter(&dtrace_lock); 16725 16726 #ifndef illumos 16727 if (ctl->nenabled > 0) { 16728 /* Don't allow unloads if a probe is enabled. */ 16729 mutex_exit(&dtrace_provider_lock); 16730 mutex_exit(&dtrace_lock); 16731 *error = -1; 16732 printf( 16733 "kldunload: attempt to unload module that has DTrace probes enabled\n"); 16734 return; 16735 } 16736 #endif 16737 16738 if (dtrace_bymod == NULL) { 16739 /* 16740 * The DTrace module is loaded (obviously) but not attached; 16741 * we don't have any work to do. 16742 */ 16743 mutex_exit(&dtrace_provider_lock); 16744 #ifdef illumos 16745 mutex_exit(&mod_lock); 16746 #endif 16747 mutex_exit(&dtrace_lock); 16748 return; 16749 } 16750 16751 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 16752 probe != NULL; probe = probe->dtpr_nextmod) { 16753 if (probe->dtpr_ecb != NULL) { 16754 mutex_exit(&dtrace_provider_lock); 16755 #ifdef illumos 16756 mutex_exit(&mod_lock); 16757 #endif 16758 mutex_exit(&dtrace_lock); 16759 16760 /* 16761 * This shouldn't _actually_ be possible -- we're 16762 * unloading a module that has an enabled probe in it. 16763 * (It's normally up to the provider to make sure that 16764 * this can't happen.) However, because dtps_enable() 16765 * doesn't have a failure mode, there can be an 16766 * enable/unload race. Upshot: we don't want to 16767 * assert, but we're not going to disable the 16768 * probe, either. 16769 */ 16770 if (dtrace_err_verbose) { 16771 #ifdef illumos 16772 cmn_err(CE_WARN, "unloaded module '%s' had " 16773 "enabled probes", ctl->mod_modname); 16774 #else 16775 cmn_err(CE_WARN, "unloaded module '%s' had " 16776 "enabled probes", modname); 16777 #endif 16778 } 16779 16780 return; 16781 } 16782 } 16783 16784 probe = first; 16785 16786 for (first = NULL; probe != NULL; probe = next) { 16787 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 16788 16789 dtrace_probes[probe->dtpr_id - 1] = NULL; 16790 16791 next = probe->dtpr_nextmod; 16792 dtrace_hash_remove(dtrace_bymod, probe); 16793 dtrace_hash_remove(dtrace_byfunc, probe); 16794 dtrace_hash_remove(dtrace_byname, probe); 16795 16796 if (first == NULL) { 16797 first = probe; 16798 probe->dtpr_nextmod = NULL; 16799 } else { 16800 probe->dtpr_nextmod = first; 16801 first = probe; 16802 } 16803 } 16804 16805 /* 16806 * We've removed all of the module's probes from the hash chains and 16807 * from the probe array. Now issue a dtrace_sync() to be sure that 16808 * everyone has cleared out from any probe array processing. 16809 */ 16810 dtrace_sync(); 16811 16812 for (probe = first; probe != NULL; probe = first) { 16813 first = probe->dtpr_nextmod; 16814 prov = probe->dtpr_provider; 16815 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 16816 probe->dtpr_arg); 16817 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 16818 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 16819 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 16820 #ifdef illumos 16821 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 16822 #else 16823 free_unr(dtrace_arena, probe->dtpr_id); 16824 #endif 16825 kmem_free(probe, sizeof (dtrace_probe_t)); 16826 } 16827 16828 mutex_exit(&dtrace_lock); 16829 #ifdef illumos 16830 mutex_exit(&mod_lock); 16831 #endif 16832 mutex_exit(&dtrace_provider_lock); 16833 } 16834 16835 #ifndef illumos 16836 static void 16837 dtrace_kld_load(void *arg __unused, linker_file_t lf) 16838 { 16839 16840 dtrace_module_loaded(lf); 16841 } 16842 16843 static void 16844 dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error) 16845 { 16846 16847 if (*error != 0) 16848 /* We already have an error, so don't do anything. */ 16849 return; 16850 dtrace_module_unloaded(lf, error); 16851 } 16852 #endif 16853 16854 #ifdef illumos 16855 static void 16856 dtrace_suspend(void) 16857 { 16858 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 16859 } 16860 16861 static void 16862 dtrace_resume(void) 16863 { 16864 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 16865 } 16866 #endif 16867 16868 static int 16869 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 16870 { 16871 ASSERT(MUTEX_HELD(&cpu_lock)); 16872 mutex_enter(&dtrace_lock); 16873 16874 switch (what) { 16875 case CPU_CONFIG: { 16876 dtrace_state_t *state; 16877 dtrace_optval_t *opt, rs, c; 16878 16879 /* 16880 * For now, we only allocate a new buffer for anonymous state. 16881 */ 16882 if ((state = dtrace_anon.dta_state) == NULL) 16883 break; 16884 16885 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 16886 break; 16887 16888 opt = state->dts_options; 16889 c = opt[DTRACEOPT_CPU]; 16890 16891 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 16892 break; 16893 16894 /* 16895 * Regardless of what the actual policy is, we're going to 16896 * temporarily set our resize policy to be manual. We're 16897 * also going to temporarily set our CPU option to denote 16898 * the newly configured CPU. 16899 */ 16900 rs = opt[DTRACEOPT_BUFRESIZE]; 16901 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 16902 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 16903 16904 (void) dtrace_state_buffers(state); 16905 16906 opt[DTRACEOPT_BUFRESIZE] = rs; 16907 opt[DTRACEOPT_CPU] = c; 16908 16909 break; 16910 } 16911 16912 case CPU_UNCONFIG: 16913 /* 16914 * We don't free the buffer in the CPU_UNCONFIG case. (The 16915 * buffer will be freed when the consumer exits.) 16916 */ 16917 break; 16918 16919 default: 16920 break; 16921 } 16922 16923 mutex_exit(&dtrace_lock); 16924 return (0); 16925 } 16926 16927 #ifdef illumos 16928 static void 16929 dtrace_cpu_setup_initial(processorid_t cpu) 16930 { 16931 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 16932 } 16933 #endif 16934 16935 static void 16936 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 16937 { 16938 if (dtrace_toxranges >= dtrace_toxranges_max) { 16939 int osize, nsize; 16940 dtrace_toxrange_t *range; 16941 16942 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16943 16944 if (osize == 0) { 16945 ASSERT(dtrace_toxrange == NULL); 16946 ASSERT(dtrace_toxranges_max == 0); 16947 dtrace_toxranges_max = 1; 16948 } else { 16949 dtrace_toxranges_max <<= 1; 16950 } 16951 16952 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16953 range = kmem_zalloc(nsize, KM_SLEEP); 16954 16955 if (dtrace_toxrange != NULL) { 16956 ASSERT(osize != 0); 16957 bcopy(dtrace_toxrange, range, osize); 16958 kmem_free(dtrace_toxrange, osize); 16959 } 16960 16961 dtrace_toxrange = range; 16962 } 16963 16964 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 16965 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 16966 16967 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 16968 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 16969 dtrace_toxranges++; 16970 } 16971 16972 static void 16973 dtrace_getf_barrier() 16974 { 16975 #ifdef illumos 16976 /* 16977 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 16978 * that contain calls to getf(), this routine will be called on every 16979 * closef() before either the underlying vnode is released or the 16980 * file_t itself is freed. By the time we are here, it is essential 16981 * that the file_t can no longer be accessed from a call to getf() 16982 * in probe context -- that assures that a dtrace_sync() can be used 16983 * to clear out any enablings referring to the old structures. 16984 */ 16985 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 16986 kcred->cr_zone->zone_dtrace_getf != 0) 16987 dtrace_sync(); 16988 #endif 16989 } 16990 16991 /* 16992 * DTrace Driver Cookbook Functions 16993 */ 16994 #ifdef illumos 16995 /*ARGSUSED*/ 16996 static int 16997 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 16998 { 16999 dtrace_provider_id_t id; 17000 dtrace_state_t *state = NULL; 17001 dtrace_enabling_t *enab; 17002 17003 mutex_enter(&cpu_lock); 17004 mutex_enter(&dtrace_provider_lock); 17005 mutex_enter(&dtrace_lock); 17006 17007 if (ddi_soft_state_init(&dtrace_softstate, 17008 sizeof (dtrace_state_t), 0) != 0) { 17009 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 17010 mutex_exit(&cpu_lock); 17011 mutex_exit(&dtrace_provider_lock); 17012 mutex_exit(&dtrace_lock); 17013 return (DDI_FAILURE); 17014 } 17015 17016 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 17017 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 17018 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 17019 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 17020 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 17021 ddi_remove_minor_node(devi, NULL); 17022 ddi_soft_state_fini(&dtrace_softstate); 17023 mutex_exit(&cpu_lock); 17024 mutex_exit(&dtrace_provider_lock); 17025 mutex_exit(&dtrace_lock); 17026 return (DDI_FAILURE); 17027 } 17028 17029 ddi_report_dev(devi); 17030 dtrace_devi = devi; 17031 17032 dtrace_modload = dtrace_module_loaded; 17033 dtrace_modunload = dtrace_module_unloaded; 17034 dtrace_cpu_init = dtrace_cpu_setup_initial; 17035 dtrace_helpers_cleanup = dtrace_helpers_destroy; 17036 dtrace_helpers_fork = dtrace_helpers_duplicate; 17037 dtrace_cpustart_init = dtrace_suspend; 17038 dtrace_cpustart_fini = dtrace_resume; 17039 dtrace_debugger_init = dtrace_suspend; 17040 dtrace_debugger_fini = dtrace_resume; 17041 17042 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 17043 17044 ASSERT(MUTEX_HELD(&cpu_lock)); 17045 17046 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 17047 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 17048 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 17049 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 17050 VM_SLEEP | VMC_IDENTIFIER); 17051 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 17052 1, INT_MAX, 0); 17053 17054 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 17055 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 17056 NULL, NULL, NULL, NULL, NULL, 0); 17057 17058 ASSERT(MUTEX_HELD(&cpu_lock)); 17059 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 17060 offsetof(dtrace_probe_t, dtpr_nextmod), 17061 offsetof(dtrace_probe_t, dtpr_prevmod)); 17062 17063 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 17064 offsetof(dtrace_probe_t, dtpr_nextfunc), 17065 offsetof(dtrace_probe_t, dtpr_prevfunc)); 17066 17067 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 17068 offsetof(dtrace_probe_t, dtpr_nextname), 17069 offsetof(dtrace_probe_t, dtpr_prevname)); 17070 17071 if (dtrace_retain_max < 1) { 17072 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 17073 "setting to 1", dtrace_retain_max); 17074 dtrace_retain_max = 1; 17075 } 17076 17077 /* 17078 * Now discover our toxic ranges. 17079 */ 17080 dtrace_toxic_ranges(dtrace_toxrange_add); 17081 17082 /* 17083 * Before we register ourselves as a provider to our own framework, 17084 * we would like to assert that dtrace_provider is NULL -- but that's 17085 * not true if we were loaded as a dependency of a DTrace provider. 17086 * Once we've registered, we can assert that dtrace_provider is our 17087 * pseudo provider. 17088 */ 17089 (void) dtrace_register("dtrace", &dtrace_provider_attr, 17090 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 17091 17092 ASSERT(dtrace_provider != NULL); 17093 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 17094 17095 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 17096 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 17097 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 17098 dtrace_provider, NULL, NULL, "END", 0, NULL); 17099 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 17100 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 17101 17102 dtrace_anon_property(); 17103 mutex_exit(&cpu_lock); 17104 17105 /* 17106 * If there are already providers, we must ask them to provide their 17107 * probes, and then match any anonymous enabling against them. Note 17108 * that there should be no other retained enablings at this time: 17109 * the only retained enablings at this time should be the anonymous 17110 * enabling. 17111 */ 17112 if (dtrace_anon.dta_enabling != NULL) { 17113 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 17114 17115 dtrace_enabling_provide(NULL); 17116 state = dtrace_anon.dta_state; 17117 17118 /* 17119 * We couldn't hold cpu_lock across the above call to 17120 * dtrace_enabling_provide(), but we must hold it to actually 17121 * enable the probes. We have to drop all of our locks, pick 17122 * up cpu_lock, and regain our locks before matching the 17123 * retained anonymous enabling. 17124 */ 17125 mutex_exit(&dtrace_lock); 17126 mutex_exit(&dtrace_provider_lock); 17127 17128 mutex_enter(&cpu_lock); 17129 mutex_enter(&dtrace_provider_lock); 17130 mutex_enter(&dtrace_lock); 17131 17132 if ((enab = dtrace_anon.dta_enabling) != NULL) 17133 (void) dtrace_enabling_match(enab, NULL); 17134 17135 mutex_exit(&cpu_lock); 17136 } 17137 17138 mutex_exit(&dtrace_lock); 17139 mutex_exit(&dtrace_provider_lock); 17140 17141 if (state != NULL) { 17142 /* 17143 * If we created any anonymous state, set it going now. 17144 */ 17145 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 17146 } 17147 17148 return (DDI_SUCCESS); 17149 } 17150 #endif /* illumos */ 17151 17152 #ifndef illumos 17153 static void dtrace_dtr(void *); 17154 #endif 17155 17156 /*ARGSUSED*/ 17157 static int 17158 #ifdef illumos 17159 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 17160 #else 17161 dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 17162 #endif 17163 { 17164 dtrace_state_t *state; 17165 uint32_t priv; 17166 uid_t uid; 17167 zoneid_t zoneid; 17168 17169 #ifdef illumos 17170 if (getminor(*devp) == DTRACEMNRN_HELPER) 17171 return (0); 17172 17173 /* 17174 * If this wasn't an open with the "helper" minor, then it must be 17175 * the "dtrace" minor. 17176 */ 17177 if (getminor(*devp) == DTRACEMNRN_DTRACE) 17178 return (ENXIO); 17179 #else 17180 cred_t *cred_p = NULL; 17181 cred_p = dev->si_cred; 17182 17183 /* 17184 * If no DTRACE_PRIV_* bits are set in the credential, then the 17185 * caller lacks sufficient permission to do anything with DTrace. 17186 */ 17187 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 17188 if (priv == DTRACE_PRIV_NONE) { 17189 #endif 17190 17191 return (EACCES); 17192 } 17193 17194 /* 17195 * Ask all providers to provide all their probes. 17196 */ 17197 mutex_enter(&dtrace_provider_lock); 17198 dtrace_probe_provide(NULL, NULL); 17199 mutex_exit(&dtrace_provider_lock); 17200 17201 mutex_enter(&cpu_lock); 17202 mutex_enter(&dtrace_lock); 17203 dtrace_opens++; 17204 dtrace_membar_producer(); 17205 17206 #ifdef illumos 17207 /* 17208 * If the kernel debugger is active (that is, if the kernel debugger 17209 * modified text in some way), we won't allow the open. 17210 */ 17211 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 17212 dtrace_opens--; 17213 mutex_exit(&cpu_lock); 17214 mutex_exit(&dtrace_lock); 17215 return (EBUSY); 17216 } 17217 17218 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) { 17219 /* 17220 * If DTrace helper tracing is enabled, we need to allocate the 17221 * trace buffer and initialize the values. 17222 */ 17223 dtrace_helptrace_buffer = 17224 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 17225 dtrace_helptrace_next = 0; 17226 dtrace_helptrace_wrapped = 0; 17227 dtrace_helptrace_enable = 0; 17228 } 17229 17230 state = dtrace_state_create(devp, cred_p); 17231 #else 17232 state = dtrace_state_create(dev, NULL); 17233 devfs_set_cdevpriv(state, dtrace_dtr); 17234 #endif 17235 17236 mutex_exit(&cpu_lock); 17237 17238 if (state == NULL) { 17239 #ifdef illumos 17240 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17241 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17242 #else 17243 --dtrace_opens; 17244 #endif 17245 mutex_exit(&dtrace_lock); 17246 return (EAGAIN); 17247 } 17248 17249 mutex_exit(&dtrace_lock); 17250 17251 return (0); 17252 } 17253 17254 /*ARGSUSED*/ 17255 #ifdef illumos 17256 static int 17257 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 17258 #else 17259 static void 17260 dtrace_dtr(void *data) 17261 #endif 17262 { 17263 #ifdef illumos 17264 minor_t minor = getminor(dev); 17265 dtrace_state_t *state; 17266 #endif 17267 dtrace_helptrace_t *buf = NULL; 17268 17269 #ifdef illumos 17270 if (minor == DTRACEMNRN_HELPER) 17271 return (0); 17272 17273 state = ddi_get_soft_state(dtrace_softstate, minor); 17274 #else 17275 dtrace_state_t *state = data; 17276 #endif 17277 17278 mutex_enter(&cpu_lock); 17279 mutex_enter(&dtrace_lock); 17280 17281 #ifdef illumos 17282 if (state->dts_anon) 17283 #else 17284 if (state != NULL && state->dts_anon) 17285 #endif 17286 { 17287 /* 17288 * There is anonymous state. Destroy that first. 17289 */ 17290 ASSERT(dtrace_anon.dta_state == NULL); 17291 dtrace_state_destroy(state->dts_anon); 17292 } 17293 17294 if (dtrace_helptrace_disable) { 17295 /* 17296 * If we have been told to disable helper tracing, set the 17297 * buffer to NULL before calling into dtrace_state_destroy(); 17298 * we take advantage of its dtrace_sync() to know that no 17299 * CPU is in probe context with enabled helper tracing 17300 * after it returns. 17301 */ 17302 buf = dtrace_helptrace_buffer; 17303 dtrace_helptrace_buffer = NULL; 17304 } 17305 17306 #ifdef illumos 17307 dtrace_state_destroy(state); 17308 #else 17309 if (state != NULL) { 17310 dtrace_state_destroy(state); 17311 kmem_free(state, 0); 17312 } 17313 #endif 17314 ASSERT(dtrace_opens > 0); 17315 17316 #ifdef illumos 17317 /* 17318 * Only relinquish control of the kernel debugger interface when there 17319 * are no consumers and no anonymous enablings. 17320 */ 17321 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17322 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17323 #else 17324 --dtrace_opens; 17325 #endif 17326 17327 if (buf != NULL) { 17328 kmem_free(buf, dtrace_helptrace_bufsize); 17329 dtrace_helptrace_disable = 0; 17330 } 17331 17332 mutex_exit(&dtrace_lock); 17333 mutex_exit(&cpu_lock); 17334 17335 #ifdef illumos 17336 return (0); 17337 #endif 17338 } 17339 17340 #ifdef illumos 17341 /*ARGSUSED*/ 17342 static int 17343 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 17344 { 17345 int rval; 17346 dof_helper_t help, *dhp = NULL; 17347 17348 switch (cmd) { 17349 case DTRACEHIOC_ADDDOF: 17350 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 17351 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 17352 return (EFAULT); 17353 } 17354 17355 dhp = &help; 17356 arg = (intptr_t)help.dofhp_dof; 17357 /*FALLTHROUGH*/ 17358 17359 case DTRACEHIOC_ADD: { 17360 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 17361 17362 if (dof == NULL) 17363 return (rval); 17364 17365 mutex_enter(&dtrace_lock); 17366 17367 /* 17368 * dtrace_helper_slurp() takes responsibility for the dof -- 17369 * it may free it now or it may save it and free it later. 17370 */ 17371 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 17372 *rv = rval; 17373 rval = 0; 17374 } else { 17375 rval = EINVAL; 17376 } 17377 17378 mutex_exit(&dtrace_lock); 17379 return (rval); 17380 } 17381 17382 case DTRACEHIOC_REMOVE: { 17383 mutex_enter(&dtrace_lock); 17384 rval = dtrace_helper_destroygen(NULL, arg); 17385 mutex_exit(&dtrace_lock); 17386 17387 return (rval); 17388 } 17389 17390 default: 17391 break; 17392 } 17393 17394 return (ENOTTY); 17395 } 17396 17397 /*ARGSUSED*/ 17398 static int 17399 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 17400 { 17401 minor_t minor = getminor(dev); 17402 dtrace_state_t *state; 17403 int rval; 17404 17405 if (minor == DTRACEMNRN_HELPER) 17406 return (dtrace_ioctl_helper(cmd, arg, rv)); 17407 17408 state = ddi_get_soft_state(dtrace_softstate, minor); 17409 17410 if (state->dts_anon) { 17411 ASSERT(dtrace_anon.dta_state == NULL); 17412 state = state->dts_anon; 17413 } 17414 17415 switch (cmd) { 17416 case DTRACEIOC_PROVIDER: { 17417 dtrace_providerdesc_t pvd; 17418 dtrace_provider_t *pvp; 17419 17420 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 17421 return (EFAULT); 17422 17423 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 17424 mutex_enter(&dtrace_provider_lock); 17425 17426 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 17427 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 17428 break; 17429 } 17430 17431 mutex_exit(&dtrace_provider_lock); 17432 17433 if (pvp == NULL) 17434 return (ESRCH); 17435 17436 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 17437 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 17438 17439 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 17440 return (EFAULT); 17441 17442 return (0); 17443 } 17444 17445 case DTRACEIOC_EPROBE: { 17446 dtrace_eprobedesc_t epdesc; 17447 dtrace_ecb_t *ecb; 17448 dtrace_action_t *act; 17449 void *buf; 17450 size_t size; 17451 uintptr_t dest; 17452 int nrecs; 17453 17454 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 17455 return (EFAULT); 17456 17457 mutex_enter(&dtrace_lock); 17458 17459 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 17460 mutex_exit(&dtrace_lock); 17461 return (EINVAL); 17462 } 17463 17464 if (ecb->dte_probe == NULL) { 17465 mutex_exit(&dtrace_lock); 17466 return (EINVAL); 17467 } 17468 17469 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 17470 epdesc.dtepd_uarg = ecb->dte_uarg; 17471 epdesc.dtepd_size = ecb->dte_size; 17472 17473 nrecs = epdesc.dtepd_nrecs; 17474 epdesc.dtepd_nrecs = 0; 17475 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17476 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17477 continue; 17478 17479 epdesc.dtepd_nrecs++; 17480 } 17481 17482 /* 17483 * Now that we have the size, we need to allocate a temporary 17484 * buffer in which to store the complete description. We need 17485 * the temporary buffer to be able to drop dtrace_lock() 17486 * across the copyout(), below. 17487 */ 17488 size = sizeof (dtrace_eprobedesc_t) + 17489 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 17490 17491 buf = kmem_alloc(size, KM_SLEEP); 17492 dest = (uintptr_t)buf; 17493 17494 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 17495 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 17496 17497 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17498 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17499 continue; 17500 17501 if (nrecs-- == 0) 17502 break; 17503 17504 bcopy(&act->dta_rec, (void *)dest, 17505 sizeof (dtrace_recdesc_t)); 17506 dest += sizeof (dtrace_recdesc_t); 17507 } 17508 17509 mutex_exit(&dtrace_lock); 17510 17511 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17512 kmem_free(buf, size); 17513 return (EFAULT); 17514 } 17515 17516 kmem_free(buf, size); 17517 return (0); 17518 } 17519 17520 case DTRACEIOC_AGGDESC: { 17521 dtrace_aggdesc_t aggdesc; 17522 dtrace_action_t *act; 17523 dtrace_aggregation_t *agg; 17524 int nrecs; 17525 uint32_t offs; 17526 dtrace_recdesc_t *lrec; 17527 void *buf; 17528 size_t size; 17529 uintptr_t dest; 17530 17531 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 17532 return (EFAULT); 17533 17534 mutex_enter(&dtrace_lock); 17535 17536 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 17537 mutex_exit(&dtrace_lock); 17538 return (EINVAL); 17539 } 17540 17541 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 17542 17543 nrecs = aggdesc.dtagd_nrecs; 17544 aggdesc.dtagd_nrecs = 0; 17545 17546 offs = agg->dtag_base; 17547 lrec = &agg->dtag_action.dta_rec; 17548 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 17549 17550 for (act = agg->dtag_first; ; act = act->dta_next) { 17551 ASSERT(act->dta_intuple || 17552 DTRACEACT_ISAGG(act->dta_kind)); 17553 17554 /* 17555 * If this action has a record size of zero, it 17556 * denotes an argument to the aggregating action. 17557 * Because the presence of this record doesn't (or 17558 * shouldn't) affect the way the data is interpreted, 17559 * we don't copy it out to save user-level the 17560 * confusion of dealing with a zero-length record. 17561 */ 17562 if (act->dta_rec.dtrd_size == 0) { 17563 ASSERT(agg->dtag_hasarg); 17564 continue; 17565 } 17566 17567 aggdesc.dtagd_nrecs++; 17568 17569 if (act == &agg->dtag_action) 17570 break; 17571 } 17572 17573 /* 17574 * Now that we have the size, we need to allocate a temporary 17575 * buffer in which to store the complete description. We need 17576 * the temporary buffer to be able to drop dtrace_lock() 17577 * across the copyout(), below. 17578 */ 17579 size = sizeof (dtrace_aggdesc_t) + 17580 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 17581 17582 buf = kmem_alloc(size, KM_SLEEP); 17583 dest = (uintptr_t)buf; 17584 17585 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 17586 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 17587 17588 for (act = agg->dtag_first; ; act = act->dta_next) { 17589 dtrace_recdesc_t rec = act->dta_rec; 17590 17591 /* 17592 * See the comment in the above loop for why we pass 17593 * over zero-length records. 17594 */ 17595 if (rec.dtrd_size == 0) { 17596 ASSERT(agg->dtag_hasarg); 17597 continue; 17598 } 17599 17600 if (nrecs-- == 0) 17601 break; 17602 17603 rec.dtrd_offset -= offs; 17604 bcopy(&rec, (void *)dest, sizeof (rec)); 17605 dest += sizeof (dtrace_recdesc_t); 17606 17607 if (act == &agg->dtag_action) 17608 break; 17609 } 17610 17611 mutex_exit(&dtrace_lock); 17612 17613 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17614 kmem_free(buf, size); 17615 return (EFAULT); 17616 } 17617 17618 kmem_free(buf, size); 17619 return (0); 17620 } 17621 17622 case DTRACEIOC_ENABLE: { 17623 dof_hdr_t *dof; 17624 dtrace_enabling_t *enab = NULL; 17625 dtrace_vstate_t *vstate; 17626 int err = 0; 17627 17628 *rv = 0; 17629 17630 /* 17631 * If a NULL argument has been passed, we take this as our 17632 * cue to reevaluate our enablings. 17633 */ 17634 if (arg == NULL) { 17635 dtrace_enabling_matchall(); 17636 17637 return (0); 17638 } 17639 17640 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 17641 return (rval); 17642 17643 mutex_enter(&cpu_lock); 17644 mutex_enter(&dtrace_lock); 17645 vstate = &state->dts_vstate; 17646 17647 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 17648 mutex_exit(&dtrace_lock); 17649 mutex_exit(&cpu_lock); 17650 dtrace_dof_destroy(dof); 17651 return (EBUSY); 17652 } 17653 17654 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 17655 mutex_exit(&dtrace_lock); 17656 mutex_exit(&cpu_lock); 17657 dtrace_dof_destroy(dof); 17658 return (EINVAL); 17659 } 17660 17661 if ((rval = dtrace_dof_options(dof, state)) != 0) { 17662 dtrace_enabling_destroy(enab); 17663 mutex_exit(&dtrace_lock); 17664 mutex_exit(&cpu_lock); 17665 dtrace_dof_destroy(dof); 17666 return (rval); 17667 } 17668 17669 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 17670 err = dtrace_enabling_retain(enab); 17671 } else { 17672 dtrace_enabling_destroy(enab); 17673 } 17674 17675 mutex_exit(&cpu_lock); 17676 mutex_exit(&dtrace_lock); 17677 dtrace_dof_destroy(dof); 17678 17679 return (err); 17680 } 17681 17682 case DTRACEIOC_REPLICATE: { 17683 dtrace_repldesc_t desc; 17684 dtrace_probedesc_t *match = &desc.dtrpd_match; 17685 dtrace_probedesc_t *create = &desc.dtrpd_create; 17686 int err; 17687 17688 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17689 return (EFAULT); 17690 17691 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17692 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17693 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17694 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17695 17696 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17697 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17698 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17699 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17700 17701 mutex_enter(&dtrace_lock); 17702 err = dtrace_enabling_replicate(state, match, create); 17703 mutex_exit(&dtrace_lock); 17704 17705 return (err); 17706 } 17707 17708 case DTRACEIOC_PROBEMATCH: 17709 case DTRACEIOC_PROBES: { 17710 dtrace_probe_t *probe = NULL; 17711 dtrace_probedesc_t desc; 17712 dtrace_probekey_t pkey; 17713 dtrace_id_t i; 17714 int m = 0; 17715 uint32_t priv; 17716 uid_t uid; 17717 zoneid_t zoneid; 17718 17719 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17720 return (EFAULT); 17721 17722 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17723 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17724 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17725 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17726 17727 /* 17728 * Before we attempt to match this probe, we want to give 17729 * all providers the opportunity to provide it. 17730 */ 17731 if (desc.dtpd_id == DTRACE_IDNONE) { 17732 mutex_enter(&dtrace_provider_lock); 17733 dtrace_probe_provide(&desc, NULL); 17734 mutex_exit(&dtrace_provider_lock); 17735 desc.dtpd_id++; 17736 } 17737 17738 if (cmd == DTRACEIOC_PROBEMATCH) { 17739 dtrace_probekey(&desc, &pkey); 17740 pkey.dtpk_id = DTRACE_IDNONE; 17741 } 17742 17743 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 17744 17745 mutex_enter(&dtrace_lock); 17746 17747 if (cmd == DTRACEIOC_PROBEMATCH) { 17748 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17749 if ((probe = dtrace_probes[i - 1]) != NULL && 17750 (m = dtrace_match_probe(probe, &pkey, 17751 priv, uid, zoneid)) != 0) 17752 break; 17753 } 17754 17755 if (m < 0) { 17756 mutex_exit(&dtrace_lock); 17757 return (EINVAL); 17758 } 17759 17760 } else { 17761 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17762 if ((probe = dtrace_probes[i - 1]) != NULL && 17763 dtrace_match_priv(probe, priv, uid, zoneid)) 17764 break; 17765 } 17766 } 17767 17768 if (probe == NULL) { 17769 mutex_exit(&dtrace_lock); 17770 return (ESRCH); 17771 } 17772 17773 dtrace_probe_description(probe, &desc); 17774 mutex_exit(&dtrace_lock); 17775 17776 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17777 return (EFAULT); 17778 17779 return (0); 17780 } 17781 17782 case DTRACEIOC_PROBEARG: { 17783 dtrace_argdesc_t desc; 17784 dtrace_probe_t *probe; 17785 dtrace_provider_t *prov; 17786 17787 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17788 return (EFAULT); 17789 17790 if (desc.dtargd_id == DTRACE_IDNONE) 17791 return (EINVAL); 17792 17793 if (desc.dtargd_ndx == DTRACE_ARGNONE) 17794 return (EINVAL); 17795 17796 mutex_enter(&dtrace_provider_lock); 17797 mutex_enter(&mod_lock); 17798 mutex_enter(&dtrace_lock); 17799 17800 if (desc.dtargd_id > dtrace_nprobes) { 17801 mutex_exit(&dtrace_lock); 17802 mutex_exit(&mod_lock); 17803 mutex_exit(&dtrace_provider_lock); 17804 return (EINVAL); 17805 } 17806 17807 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 17808 mutex_exit(&dtrace_lock); 17809 mutex_exit(&mod_lock); 17810 mutex_exit(&dtrace_provider_lock); 17811 return (EINVAL); 17812 } 17813 17814 mutex_exit(&dtrace_lock); 17815 17816 prov = probe->dtpr_provider; 17817 17818 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 17819 /* 17820 * There isn't any typed information for this probe. 17821 * Set the argument number to DTRACE_ARGNONE. 17822 */ 17823 desc.dtargd_ndx = DTRACE_ARGNONE; 17824 } else { 17825 desc.dtargd_native[0] = '\0'; 17826 desc.dtargd_xlate[0] = '\0'; 17827 desc.dtargd_mapping = desc.dtargd_ndx; 17828 17829 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 17830 probe->dtpr_id, probe->dtpr_arg, &desc); 17831 } 17832 17833 mutex_exit(&mod_lock); 17834 mutex_exit(&dtrace_provider_lock); 17835 17836 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17837 return (EFAULT); 17838 17839 return (0); 17840 } 17841 17842 case DTRACEIOC_GO: { 17843 processorid_t cpuid; 17844 rval = dtrace_state_go(state, &cpuid); 17845 17846 if (rval != 0) 17847 return (rval); 17848 17849 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17850 return (EFAULT); 17851 17852 return (0); 17853 } 17854 17855 case DTRACEIOC_STOP: { 17856 processorid_t cpuid; 17857 17858 mutex_enter(&dtrace_lock); 17859 rval = dtrace_state_stop(state, &cpuid); 17860 mutex_exit(&dtrace_lock); 17861 17862 if (rval != 0) 17863 return (rval); 17864 17865 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17866 return (EFAULT); 17867 17868 return (0); 17869 } 17870 17871 case DTRACEIOC_DOFGET: { 17872 dof_hdr_t hdr, *dof; 17873 uint64_t len; 17874 17875 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 17876 return (EFAULT); 17877 17878 mutex_enter(&dtrace_lock); 17879 dof = dtrace_dof_create(state); 17880 mutex_exit(&dtrace_lock); 17881 17882 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 17883 rval = copyout(dof, (void *)arg, len); 17884 dtrace_dof_destroy(dof); 17885 17886 return (rval == 0 ? 0 : EFAULT); 17887 } 17888 17889 case DTRACEIOC_AGGSNAP: 17890 case DTRACEIOC_BUFSNAP: { 17891 dtrace_bufdesc_t desc; 17892 caddr_t cached; 17893 dtrace_buffer_t *buf; 17894 17895 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17896 return (EFAULT); 17897 17898 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 17899 return (EINVAL); 17900 17901 mutex_enter(&dtrace_lock); 17902 17903 if (cmd == DTRACEIOC_BUFSNAP) { 17904 buf = &state->dts_buffer[desc.dtbd_cpu]; 17905 } else { 17906 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 17907 } 17908 17909 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 17910 size_t sz = buf->dtb_offset; 17911 17912 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 17913 mutex_exit(&dtrace_lock); 17914 return (EBUSY); 17915 } 17916 17917 /* 17918 * If this buffer has already been consumed, we're 17919 * going to indicate that there's nothing left here 17920 * to consume. 17921 */ 17922 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 17923 mutex_exit(&dtrace_lock); 17924 17925 desc.dtbd_size = 0; 17926 desc.dtbd_drops = 0; 17927 desc.dtbd_errors = 0; 17928 desc.dtbd_oldest = 0; 17929 sz = sizeof (desc); 17930 17931 if (copyout(&desc, (void *)arg, sz) != 0) 17932 return (EFAULT); 17933 17934 return (0); 17935 } 17936 17937 /* 17938 * If this is a ring buffer that has wrapped, we want 17939 * to copy the whole thing out. 17940 */ 17941 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 17942 dtrace_buffer_polish(buf); 17943 sz = buf->dtb_size; 17944 } 17945 17946 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 17947 mutex_exit(&dtrace_lock); 17948 return (EFAULT); 17949 } 17950 17951 desc.dtbd_size = sz; 17952 desc.dtbd_drops = buf->dtb_drops; 17953 desc.dtbd_errors = buf->dtb_errors; 17954 desc.dtbd_oldest = buf->dtb_xamot_offset; 17955 desc.dtbd_timestamp = dtrace_gethrtime(); 17956 17957 mutex_exit(&dtrace_lock); 17958 17959 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17960 return (EFAULT); 17961 17962 buf->dtb_flags |= DTRACEBUF_CONSUMED; 17963 17964 return (0); 17965 } 17966 17967 if (buf->dtb_tomax == NULL) { 17968 ASSERT(buf->dtb_xamot == NULL); 17969 mutex_exit(&dtrace_lock); 17970 return (ENOENT); 17971 } 17972 17973 cached = buf->dtb_tomax; 17974 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 17975 17976 dtrace_xcall(desc.dtbd_cpu, 17977 (dtrace_xcall_t)dtrace_buffer_switch, buf); 17978 17979 state->dts_errors += buf->dtb_xamot_errors; 17980 17981 /* 17982 * If the buffers did not actually switch, then the cross call 17983 * did not take place -- presumably because the given CPU is 17984 * not in the ready set. If this is the case, we'll return 17985 * ENOENT. 17986 */ 17987 if (buf->dtb_tomax == cached) { 17988 ASSERT(buf->dtb_xamot != cached); 17989 mutex_exit(&dtrace_lock); 17990 return (ENOENT); 17991 } 17992 17993 ASSERT(cached == buf->dtb_xamot); 17994 17995 /* 17996 * We have our snapshot; now copy it out. 17997 */ 17998 if (copyout(buf->dtb_xamot, desc.dtbd_data, 17999 buf->dtb_xamot_offset) != 0) { 18000 mutex_exit(&dtrace_lock); 18001 return (EFAULT); 18002 } 18003 18004 desc.dtbd_size = buf->dtb_xamot_offset; 18005 desc.dtbd_drops = buf->dtb_xamot_drops; 18006 desc.dtbd_errors = buf->dtb_xamot_errors; 18007 desc.dtbd_oldest = 0; 18008 desc.dtbd_timestamp = buf->dtb_switched; 18009 18010 mutex_exit(&dtrace_lock); 18011 18012 /* 18013 * Finally, copy out the buffer description. 18014 */ 18015 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 18016 return (EFAULT); 18017 18018 return (0); 18019 } 18020 18021 case DTRACEIOC_CONF: { 18022 dtrace_conf_t conf; 18023 18024 bzero(&conf, sizeof (conf)); 18025 conf.dtc_difversion = DIF_VERSION; 18026 conf.dtc_difintregs = DIF_DIR_NREGS; 18027 conf.dtc_diftupregs = DIF_DTR_NREGS; 18028 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 18029 18030 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 18031 return (EFAULT); 18032 18033 return (0); 18034 } 18035 18036 case DTRACEIOC_STATUS: { 18037 dtrace_status_t stat; 18038 dtrace_dstate_t *dstate; 18039 int i, j; 18040 uint64_t nerrs; 18041 18042 /* 18043 * See the comment in dtrace_state_deadman() for the reason 18044 * for setting dts_laststatus to INT64_MAX before setting 18045 * it to the correct value. 18046 */ 18047 state->dts_laststatus = INT64_MAX; 18048 dtrace_membar_producer(); 18049 state->dts_laststatus = dtrace_gethrtime(); 18050 18051 bzero(&stat, sizeof (stat)); 18052 18053 mutex_enter(&dtrace_lock); 18054 18055 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 18056 mutex_exit(&dtrace_lock); 18057 return (ENOENT); 18058 } 18059 18060 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 18061 stat.dtst_exiting = 1; 18062 18063 nerrs = state->dts_errors; 18064 dstate = &state->dts_vstate.dtvs_dynvars; 18065 18066 for (i = 0; i < NCPU; i++) { 18067 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 18068 18069 stat.dtst_dyndrops += dcpu->dtdsc_drops; 18070 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 18071 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 18072 18073 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 18074 stat.dtst_filled++; 18075 18076 nerrs += state->dts_buffer[i].dtb_errors; 18077 18078 for (j = 0; j < state->dts_nspeculations; j++) { 18079 dtrace_speculation_t *spec; 18080 dtrace_buffer_t *buf; 18081 18082 spec = &state->dts_speculations[j]; 18083 buf = &spec->dtsp_buffer[i]; 18084 stat.dtst_specdrops += buf->dtb_xamot_drops; 18085 } 18086 } 18087 18088 stat.dtst_specdrops_busy = state->dts_speculations_busy; 18089 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 18090 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 18091 stat.dtst_dblerrors = state->dts_dblerrors; 18092 stat.dtst_killed = 18093 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 18094 stat.dtst_errors = nerrs; 18095 18096 mutex_exit(&dtrace_lock); 18097 18098 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 18099 return (EFAULT); 18100 18101 return (0); 18102 } 18103 18104 case DTRACEIOC_FORMAT: { 18105 dtrace_fmtdesc_t fmt; 18106 char *str; 18107 int len; 18108 18109 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 18110 return (EFAULT); 18111 18112 mutex_enter(&dtrace_lock); 18113 18114 if (fmt.dtfd_format == 0 || 18115 fmt.dtfd_format > state->dts_nformats) { 18116 mutex_exit(&dtrace_lock); 18117 return (EINVAL); 18118 } 18119 18120 /* 18121 * Format strings are allocated contiguously and they are 18122 * never freed; if a format index is less than the number 18123 * of formats, we can assert that the format map is non-NULL 18124 * and that the format for the specified index is non-NULL. 18125 */ 18126 ASSERT(state->dts_formats != NULL); 18127 str = state->dts_formats[fmt.dtfd_format - 1]; 18128 ASSERT(str != NULL); 18129 18130 len = strlen(str) + 1; 18131 18132 if (len > fmt.dtfd_length) { 18133 fmt.dtfd_length = len; 18134 18135 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 18136 mutex_exit(&dtrace_lock); 18137 return (EINVAL); 18138 } 18139 } else { 18140 if (copyout(str, fmt.dtfd_string, len) != 0) { 18141 mutex_exit(&dtrace_lock); 18142 return (EINVAL); 18143 } 18144 } 18145 18146 mutex_exit(&dtrace_lock); 18147 return (0); 18148 } 18149 18150 default: 18151 break; 18152 } 18153 18154 return (ENOTTY); 18155 } 18156 18157 /*ARGSUSED*/ 18158 static int 18159 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 18160 { 18161 dtrace_state_t *state; 18162 18163 switch (cmd) { 18164 case DDI_DETACH: 18165 break; 18166 18167 case DDI_SUSPEND: 18168 return (DDI_SUCCESS); 18169 18170 default: 18171 return (DDI_FAILURE); 18172 } 18173 18174 mutex_enter(&cpu_lock); 18175 mutex_enter(&dtrace_provider_lock); 18176 mutex_enter(&dtrace_lock); 18177 18178 ASSERT(dtrace_opens == 0); 18179 18180 if (dtrace_helpers > 0) { 18181 mutex_exit(&dtrace_provider_lock); 18182 mutex_exit(&dtrace_lock); 18183 mutex_exit(&cpu_lock); 18184 return (DDI_FAILURE); 18185 } 18186 18187 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 18188 mutex_exit(&dtrace_provider_lock); 18189 mutex_exit(&dtrace_lock); 18190 mutex_exit(&cpu_lock); 18191 return (DDI_FAILURE); 18192 } 18193 18194 dtrace_provider = NULL; 18195 18196 if ((state = dtrace_anon_grab()) != NULL) { 18197 /* 18198 * If there were ECBs on this state, the provider should 18199 * have not been allowed to detach; assert that there is 18200 * none. 18201 */ 18202 ASSERT(state->dts_necbs == 0); 18203 dtrace_state_destroy(state); 18204 18205 /* 18206 * If we're being detached with anonymous state, we need to 18207 * indicate to the kernel debugger that DTrace is now inactive. 18208 */ 18209 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 18210 } 18211 18212 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 18213 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 18214 dtrace_cpu_init = NULL; 18215 dtrace_helpers_cleanup = NULL; 18216 dtrace_helpers_fork = NULL; 18217 dtrace_cpustart_init = NULL; 18218 dtrace_cpustart_fini = NULL; 18219 dtrace_debugger_init = NULL; 18220 dtrace_debugger_fini = NULL; 18221 dtrace_modload = NULL; 18222 dtrace_modunload = NULL; 18223 18224 ASSERT(dtrace_getf == 0); 18225 ASSERT(dtrace_closef == NULL); 18226 18227 mutex_exit(&cpu_lock); 18228 18229 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 18230 dtrace_probes = NULL; 18231 dtrace_nprobes = 0; 18232 18233 dtrace_hash_destroy(dtrace_bymod); 18234 dtrace_hash_destroy(dtrace_byfunc); 18235 dtrace_hash_destroy(dtrace_byname); 18236 dtrace_bymod = NULL; 18237 dtrace_byfunc = NULL; 18238 dtrace_byname = NULL; 18239 18240 kmem_cache_destroy(dtrace_state_cache); 18241 vmem_destroy(dtrace_minor); 18242 vmem_destroy(dtrace_arena); 18243 18244 if (dtrace_toxrange != NULL) { 18245 kmem_free(dtrace_toxrange, 18246 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 18247 dtrace_toxrange = NULL; 18248 dtrace_toxranges = 0; 18249 dtrace_toxranges_max = 0; 18250 } 18251 18252 ddi_remove_minor_node(dtrace_devi, NULL); 18253 dtrace_devi = NULL; 18254 18255 ddi_soft_state_fini(&dtrace_softstate); 18256 18257 ASSERT(dtrace_vtime_references == 0); 18258 ASSERT(dtrace_opens == 0); 18259 ASSERT(dtrace_retained == NULL); 18260 18261 mutex_exit(&dtrace_lock); 18262 mutex_exit(&dtrace_provider_lock); 18263 18264 /* 18265 * We don't destroy the task queue until after we have dropped our 18266 * locks (taskq_destroy() may block on running tasks). To prevent 18267 * attempting to do work after we have effectively detached but before 18268 * the task queue has been destroyed, all tasks dispatched via the 18269 * task queue must check that DTrace is still attached before 18270 * performing any operation. 18271 */ 18272 taskq_destroy(dtrace_taskq); 18273 dtrace_taskq = NULL; 18274 18275 return (DDI_SUCCESS); 18276 } 18277 #endif 18278 18279 #ifdef illumos 18280 /*ARGSUSED*/ 18281 static int 18282 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 18283 { 18284 int error; 18285 18286 switch (infocmd) { 18287 case DDI_INFO_DEVT2DEVINFO: 18288 *result = (void *)dtrace_devi; 18289 error = DDI_SUCCESS; 18290 break; 18291 case DDI_INFO_DEVT2INSTANCE: 18292 *result = (void *)0; 18293 error = DDI_SUCCESS; 18294 break; 18295 default: 18296 error = DDI_FAILURE; 18297 } 18298 return (error); 18299 } 18300 #endif 18301 18302 #ifdef illumos 18303 static struct cb_ops dtrace_cb_ops = { 18304 dtrace_open, /* open */ 18305 dtrace_close, /* close */ 18306 nulldev, /* strategy */ 18307 nulldev, /* print */ 18308 nodev, /* dump */ 18309 nodev, /* read */ 18310 nodev, /* write */ 18311 dtrace_ioctl, /* ioctl */ 18312 nodev, /* devmap */ 18313 nodev, /* mmap */ 18314 nodev, /* segmap */ 18315 nochpoll, /* poll */ 18316 ddi_prop_op, /* cb_prop_op */ 18317 0, /* streamtab */ 18318 D_NEW | D_MP /* Driver compatibility flag */ 18319 }; 18320 18321 static struct dev_ops dtrace_ops = { 18322 DEVO_REV, /* devo_rev */ 18323 0, /* refcnt */ 18324 dtrace_info, /* get_dev_info */ 18325 nulldev, /* identify */ 18326 nulldev, /* probe */ 18327 dtrace_attach, /* attach */ 18328 dtrace_detach, /* detach */ 18329 nodev, /* reset */ 18330 &dtrace_cb_ops, /* driver operations */ 18331 NULL, /* bus operations */ 18332 nodev /* dev power */ 18333 }; 18334 18335 static struct modldrv modldrv = { 18336 &mod_driverops, /* module type (this is a pseudo driver) */ 18337 "Dynamic Tracing", /* name of module */ 18338 &dtrace_ops, /* driver ops */ 18339 }; 18340 18341 static struct modlinkage modlinkage = { 18342 MODREV_1, 18343 (void *)&modldrv, 18344 NULL 18345 }; 18346 18347 int 18348 _init(void) 18349 { 18350 return (mod_install(&modlinkage)); 18351 } 18352 18353 int 18354 _info(struct modinfo *modinfop) 18355 { 18356 return (mod_info(&modlinkage, modinfop)); 18357 } 18358 18359 int 18360 _fini(void) 18361 { 18362 return (mod_remove(&modlinkage)); 18363 } 18364 #else 18365 18366 static d_ioctl_t dtrace_ioctl; 18367 static d_ioctl_t dtrace_ioctl_helper; 18368 static void dtrace_load(void *); 18369 static int dtrace_unload(void); 18370 static struct cdev *dtrace_dev; 18371 static struct cdev *helper_dev; 18372 18373 void dtrace_invop_init(void); 18374 void dtrace_invop_uninit(void); 18375 18376 static struct cdevsw dtrace_cdevsw = { 18377 .d_version = D_VERSION, 18378 .d_ioctl = dtrace_ioctl, 18379 .d_open = dtrace_open, 18380 .d_name = "dtrace", 18381 }; 18382 18383 static struct cdevsw helper_cdevsw = { 18384 .d_version = D_VERSION, 18385 .d_ioctl = dtrace_ioctl_helper, 18386 .d_name = "helper", 18387 }; 18388 18389 #include <dtrace_anon.c> 18390 #include <dtrace_ioctl.c> 18391 #include <dtrace_load.c> 18392 #include <dtrace_modevent.c> 18393 #include <dtrace_sysctl.c> 18394 #include <dtrace_unload.c> 18395 #include <dtrace_vtime.c> 18396 #include <dtrace_hacks.c> 18397 #include <dtrace_isa.c> 18398 18399 SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 18400 SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 18401 SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 18402 18403 DEV_MODULE(dtrace, dtrace_modevent, NULL); 18404 MODULE_VERSION(dtrace, 1); 18405 MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 18406 #endif 18407