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/random.h> 128 #include <sys/rwlock.h> 129 #include <sys/sx.h> 130 #include <sys/sysctl.h> 131 132 #include <sys/dtrace_bsd.h> 133 134 #include <netinet/in.h> 135 136 #include "dtrace_cddl.h" 137 #include "dtrace_debug.c" 138 #endif 139 140 #include "dtrace_xoroshiro128_plus.h" 141 142 /* 143 * DTrace Tunable Variables 144 * 145 * The following variables may be tuned by adding a line to /etc/system that 146 * includes both the name of the DTrace module ("dtrace") and the name of the 147 * variable. For example: 148 * 149 * set dtrace:dtrace_destructive_disallow = 1 150 * 151 * In general, the only variables that one should be tuning this way are those 152 * that affect system-wide DTrace behavior, and for which the default behavior 153 * is undesirable. Most of these variables are tunable on a per-consumer 154 * basis using DTrace options, and need not be tuned on a system-wide basis. 155 * When tuning these variables, avoid pathological values; while some attempt 156 * is made to verify the integrity of these variables, they are not considered 157 * part of the supported interface to DTrace, and they are therefore not 158 * checked comprehensively. Further, these variables should not be tuned 159 * dynamically via "mdb -kw" or other means; they should only be tuned via 160 * /etc/system. 161 */ 162 int dtrace_destructive_disallow = 0; 163 #ifndef illumos 164 /* Positive logic version of dtrace_destructive_disallow for loader tunable */ 165 int dtrace_allow_destructive = 1; 166 #endif 167 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 168 size_t dtrace_difo_maxsize = (256 * 1024); 169 dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 170 size_t dtrace_statvar_maxsize = (16 * 1024); 171 size_t dtrace_actions_max = (16 * 1024); 172 size_t dtrace_retain_max = 1024; 173 dtrace_optval_t dtrace_helper_actions_max = 128; 174 dtrace_optval_t dtrace_helper_providers_max = 32; 175 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 176 size_t dtrace_strsize_default = 256; 177 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 178 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 179 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 180 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 181 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 182 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 183 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 184 dtrace_optval_t dtrace_nspec_default = 1; 185 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 186 dtrace_optval_t dtrace_stackframes_default = 20; 187 dtrace_optval_t dtrace_ustackframes_default = 20; 188 dtrace_optval_t dtrace_jstackframes_default = 50; 189 dtrace_optval_t dtrace_jstackstrsize_default = 512; 190 int dtrace_msgdsize_max = 128; 191 hrtime_t dtrace_chill_max = MSEC2NSEC(500); /* 500 ms */ 192 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 193 int dtrace_devdepth_max = 32; 194 int dtrace_err_verbose; 195 hrtime_t dtrace_deadman_interval = NANOSEC; 196 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 197 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 198 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 199 #ifndef illumos 200 int dtrace_memstr_max = 4096; 201 #endif 202 203 /* 204 * DTrace External Variables 205 * 206 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 207 * available to DTrace consumers via the backtick (`) syntax. One of these, 208 * dtrace_zero, is made deliberately so: it is provided as a source of 209 * well-known, zero-filled memory. While this variable is not documented, 210 * it is used by some translators as an implementation detail. 211 */ 212 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 213 214 /* 215 * DTrace Internal Variables 216 */ 217 #ifdef illumos 218 static dev_info_t *dtrace_devi; /* device info */ 219 #endif 220 #ifdef illumos 221 static vmem_t *dtrace_arena; /* probe ID arena */ 222 static vmem_t *dtrace_minor; /* minor number arena */ 223 #else 224 static taskq_t *dtrace_taskq; /* task queue */ 225 static struct unrhdr *dtrace_arena; /* Probe ID number. */ 226 #endif 227 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 228 static int dtrace_nprobes; /* number of probes */ 229 static dtrace_provider_t *dtrace_provider; /* provider list */ 230 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 231 static int dtrace_opens; /* number of opens */ 232 static int dtrace_helpers; /* number of helpers */ 233 static int dtrace_getf; /* number of unpriv getf()s */ 234 #ifdef illumos 235 static void *dtrace_softstate; /* softstate pointer */ 236 #endif 237 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 238 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 239 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 240 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 241 static int dtrace_toxranges; /* number of toxic ranges */ 242 static int dtrace_toxranges_max; /* size of toxic range array */ 243 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 244 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 245 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 246 static kthread_t *dtrace_panicked; /* panicking thread */ 247 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 248 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 249 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 250 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 251 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 252 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 253 static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 254 #ifndef illumos 255 static struct mtx dtrace_unr_mtx; 256 MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 257 static eventhandler_tag dtrace_kld_load_tag; 258 static eventhandler_tag dtrace_kld_unload_try_tag; 259 #endif 260 261 /* 262 * DTrace Locking 263 * DTrace is protected by three (relatively coarse-grained) locks: 264 * 265 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 266 * including enabling state, probes, ECBs, consumer state, helper state, 267 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 268 * probe context is lock-free -- synchronization is handled via the 269 * dtrace_sync() cross call mechanism. 270 * 271 * (2) dtrace_provider_lock is required when manipulating provider state, or 272 * when provider state must be held constant. 273 * 274 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 275 * when meta provider state must be held constant. 276 * 277 * The lock ordering between these three locks is dtrace_meta_lock before 278 * dtrace_provider_lock before dtrace_lock. (In particular, there are 279 * several places where dtrace_provider_lock is held by the framework as it 280 * calls into the providers -- which then call back into the framework, 281 * grabbing dtrace_lock.) 282 * 283 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 284 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 285 * role as a coarse-grained lock; it is acquired before both of these locks. 286 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 287 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 288 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 289 * acquired _between_ dtrace_provider_lock and dtrace_lock. 290 */ 291 static kmutex_t dtrace_lock; /* probe state lock */ 292 static kmutex_t dtrace_provider_lock; /* provider state lock */ 293 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 294 295 #ifndef illumos 296 /* XXX FreeBSD hacks. */ 297 #define cr_suid cr_svuid 298 #define cr_sgid cr_svgid 299 #define ipaddr_t in_addr_t 300 #define mod_modname pathname 301 #define vuprintf vprintf 302 #define ttoproc(_a) ((_a)->td_proc) 303 #define crgetzoneid(_a) 0 304 #define SNOCD 0 305 #define CPU_ON_INTR(_a) 0 306 307 #define PRIV_EFFECTIVE (1 << 0) 308 #define PRIV_DTRACE_KERNEL (1 << 1) 309 #define PRIV_DTRACE_PROC (1 << 2) 310 #define PRIV_DTRACE_USER (1 << 3) 311 #define PRIV_PROC_OWNER (1 << 4) 312 #define PRIV_PROC_ZONE (1 << 5) 313 #define PRIV_ALL ~0 314 315 SYSCTL_DECL(_debug_dtrace); 316 SYSCTL_DECL(_kern_dtrace); 317 #endif 318 319 #ifdef illumos 320 #define curcpu CPU->cpu_id 321 #endif 322 323 324 /* 325 * DTrace Provider Variables 326 * 327 * These are the variables relating to DTrace as a provider (that is, the 328 * provider of the BEGIN, END, and ERROR probes). 329 */ 330 static dtrace_pattr_t dtrace_provider_attr = { 331 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 332 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 333 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 334 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 335 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 336 }; 337 338 static void 339 dtrace_nullop(void) 340 {} 341 342 static dtrace_pops_t dtrace_provider_ops = { 343 .dtps_provide = (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 344 .dtps_provide_module = (void (*)(void *, modctl_t *))dtrace_nullop, 345 .dtps_enable = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 346 .dtps_disable = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 347 .dtps_suspend = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 348 .dtps_resume = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 349 .dtps_getargdesc = NULL, 350 .dtps_getargval = NULL, 351 .dtps_usermode = NULL, 352 .dtps_destroy = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 353 }; 354 355 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 356 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 357 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 358 359 /* 360 * DTrace Helper Tracing Variables 361 * 362 * These variables should be set dynamically to enable helper tracing. The 363 * only variables that should be set are dtrace_helptrace_enable (which should 364 * be set to a non-zero value to allocate helper tracing buffers on the next 365 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a 366 * non-zero value to deallocate helper tracing buffers on the next close of 367 * /dev/dtrace). When (and only when) helper tracing is disabled, the 368 * buffer size may also be set via dtrace_helptrace_bufsize. 369 */ 370 int dtrace_helptrace_enable = 0; 371 int dtrace_helptrace_disable = 0; 372 int dtrace_helptrace_bufsize = 16 * 1024 * 1024; 373 uint32_t dtrace_helptrace_nlocals; 374 static dtrace_helptrace_t *dtrace_helptrace_buffer; 375 static uint32_t dtrace_helptrace_next = 0; 376 static int dtrace_helptrace_wrapped = 0; 377 378 /* 379 * DTrace Error Hashing 380 * 381 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 382 * table. This is very useful for checking coverage of tests that are 383 * expected to induce DIF or DOF processing errors, and may be useful for 384 * debugging problems in the DIF code generator or in DOF generation . The 385 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 386 */ 387 #ifdef DEBUG 388 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 389 static const char *dtrace_errlast; 390 static kthread_t *dtrace_errthread; 391 static kmutex_t dtrace_errlock; 392 #endif 393 394 /* 395 * DTrace Macros and Constants 396 * 397 * These are various macros that are useful in various spots in the 398 * implementation, along with a few random constants that have no meaning 399 * outside of the implementation. There is no real structure to this cpp 400 * mishmash -- but is there ever? 401 */ 402 #define DTRACE_HASHSTR(hash, probe) \ 403 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 404 405 #define DTRACE_HASHNEXT(hash, probe) \ 406 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 407 408 #define DTRACE_HASHPREV(hash, probe) \ 409 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 410 411 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 412 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 413 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 414 415 #define DTRACE_AGGHASHSIZE_SLEW 17 416 417 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 418 419 /* 420 * The key for a thread-local variable consists of the lower 61 bits of the 421 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 422 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 423 * equal to a variable identifier. This is necessary (but not sufficient) to 424 * assure that global associative arrays never collide with thread-local 425 * variables. To guarantee that they cannot collide, we must also define the 426 * order for keying dynamic variables. That order is: 427 * 428 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 429 * 430 * Because the variable-key and the tls-key are in orthogonal spaces, there is 431 * no way for a global variable key signature to match a thread-local key 432 * signature. 433 */ 434 #ifdef illumos 435 #define DTRACE_TLS_THRKEY(where) { \ 436 uint_t intr = 0; \ 437 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 438 for (; actv; actv >>= 1) \ 439 intr++; \ 440 ASSERT(intr < (1 << 3)); \ 441 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 442 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 443 } 444 #else 445 #define DTRACE_TLS_THRKEY(where) { \ 446 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 447 uint_t intr = 0; \ 448 uint_t actv = _c->cpu_intr_actv; \ 449 for (; actv; actv >>= 1) \ 450 intr++; \ 451 ASSERT(intr < (1 << 3)); \ 452 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 453 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 454 } 455 #endif 456 457 #define DT_BSWAP_8(x) ((x) & 0xff) 458 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 459 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 460 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 461 462 #define DT_MASK_LO 0x00000000FFFFFFFFULL 463 464 #define DTRACE_STORE(type, tomax, offset, what) \ 465 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 466 467 #ifndef __x86 468 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 469 if (addr & (size - 1)) { \ 470 *flags |= CPU_DTRACE_BADALIGN; \ 471 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 472 return (0); \ 473 } 474 #else 475 #define DTRACE_ALIGNCHECK(addr, size, flags) 476 #endif 477 478 /* 479 * Test whether a range of memory starting at testaddr of size testsz falls 480 * within the range of memory described by addr, sz. We take care to avoid 481 * problems with overflow and underflow of the unsigned quantities, and 482 * disallow all negative sizes. Ranges of size 0 are allowed. 483 */ 484 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 485 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 486 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 487 (testaddr) + (testsz) >= (testaddr)) 488 489 #define DTRACE_RANGE_REMAIN(remp, addr, baseaddr, basesz) \ 490 do { \ 491 if ((remp) != NULL) { \ 492 *(remp) = (uintptr_t)(baseaddr) + (basesz) - (addr); \ 493 } \ 494 _NOTE(CONSTCOND) } while (0) 495 496 497 /* 498 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 499 * alloc_sz on the righthand side of the comparison in order to avoid overflow 500 * or underflow in the comparison with it. This is simpler than the INRANGE 501 * check above, because we know that the dtms_scratch_ptr is valid in the 502 * range. Allocations of size zero are allowed. 503 */ 504 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 505 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 506 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 507 508 #define DTRACE_LOADFUNC(bits) \ 509 /*CSTYLED*/ \ 510 uint##bits##_t \ 511 dtrace_load##bits(uintptr_t addr) \ 512 { \ 513 size_t size = bits / NBBY; \ 514 /*CSTYLED*/ \ 515 uint##bits##_t rval; \ 516 int i; \ 517 volatile uint16_t *flags = (volatile uint16_t *) \ 518 &cpu_core[curcpu].cpuc_dtrace_flags; \ 519 \ 520 DTRACE_ALIGNCHECK(addr, size, flags); \ 521 \ 522 for (i = 0; i < dtrace_toxranges; i++) { \ 523 if (addr >= dtrace_toxrange[i].dtt_limit) \ 524 continue; \ 525 \ 526 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 527 continue; \ 528 \ 529 /* \ 530 * This address falls within a toxic region; return 0. \ 531 */ \ 532 *flags |= CPU_DTRACE_BADADDR; \ 533 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 534 return (0); \ 535 } \ 536 \ 537 *flags |= CPU_DTRACE_NOFAULT; \ 538 /*CSTYLED*/ \ 539 rval = *((volatile uint##bits##_t *)addr); \ 540 *flags &= ~CPU_DTRACE_NOFAULT; \ 541 \ 542 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 543 } 544 545 #ifdef _LP64 546 #define dtrace_loadptr dtrace_load64 547 #else 548 #define dtrace_loadptr dtrace_load32 549 #endif 550 551 #define DTRACE_DYNHASH_FREE 0 552 #define DTRACE_DYNHASH_SINK 1 553 #define DTRACE_DYNHASH_VALID 2 554 555 #define DTRACE_MATCH_NEXT 0 556 #define DTRACE_MATCH_DONE 1 557 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 558 #define DTRACE_STATE_ALIGN 64 559 560 #define DTRACE_FLAGS2FLT(flags) \ 561 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 562 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 563 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 564 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 565 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 566 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 567 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 568 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 569 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 570 DTRACEFLT_UNKNOWN) 571 572 #define DTRACEACT_ISSTRING(act) \ 573 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 574 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 575 576 /* Function prototype definitions: */ 577 static size_t dtrace_strlen(const char *, size_t); 578 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 579 static void dtrace_enabling_provide(dtrace_provider_t *); 580 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 581 static void dtrace_enabling_matchall(void); 582 static void dtrace_enabling_reap(void); 583 static dtrace_state_t *dtrace_anon_grab(void); 584 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 585 dtrace_state_t *, uint64_t, uint64_t); 586 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 587 static void dtrace_buffer_drop(dtrace_buffer_t *); 588 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 589 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 590 dtrace_state_t *, dtrace_mstate_t *); 591 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 592 dtrace_optval_t); 593 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 594 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 595 uint16_t dtrace_load16(uintptr_t); 596 uint32_t dtrace_load32(uintptr_t); 597 uint64_t dtrace_load64(uintptr_t); 598 uint8_t dtrace_load8(uintptr_t); 599 void dtrace_dynvar_clean(dtrace_dstate_t *); 600 dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 601 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 602 uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 603 static int dtrace_priv_proc(dtrace_state_t *); 604 static void dtrace_getf_barrier(void); 605 static int dtrace_canload_remains(uint64_t, size_t, size_t *, 606 dtrace_mstate_t *, dtrace_vstate_t *); 607 static int dtrace_canstore_remains(uint64_t, size_t, size_t *, 608 dtrace_mstate_t *, dtrace_vstate_t *); 609 610 /* 611 * DTrace Probe Context Functions 612 * 613 * These functions are called from probe context. Because probe context is 614 * any context in which C may be called, arbitrarily locks may be held, 615 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 616 * As a result, functions called from probe context may only call other DTrace 617 * support functions -- they may not interact at all with the system at large. 618 * (Note that the ASSERT macro is made probe-context safe by redefining it in 619 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 620 * loads are to be performed from probe context, they _must_ be in terms of 621 * the safe dtrace_load*() variants. 622 * 623 * Some functions in this block are not actually called from probe context; 624 * for these functions, there will be a comment above the function reading 625 * "Note: not called from probe context." 626 */ 627 void 628 dtrace_panic(const char *format, ...) 629 { 630 va_list alist; 631 632 va_start(alist, format); 633 #ifdef __FreeBSD__ 634 vpanic(format, alist); 635 #else 636 dtrace_vpanic(format, alist); 637 #endif 638 va_end(alist); 639 } 640 641 int 642 dtrace_assfail(const char *a, const char *f, int l) 643 { 644 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 645 646 /* 647 * We just need something here that even the most clever compiler 648 * cannot optimize away. 649 */ 650 return (a[(uintptr_t)f]); 651 } 652 653 /* 654 * Atomically increment a specified error counter from probe context. 655 */ 656 static void 657 dtrace_error(uint32_t *counter) 658 { 659 /* 660 * Most counters stored to in probe context are per-CPU counters. 661 * However, there are some error conditions that are sufficiently 662 * arcane that they don't merit per-CPU storage. If these counters 663 * are incremented concurrently on different CPUs, scalability will be 664 * adversely affected -- but we don't expect them to be white-hot in a 665 * correctly constructed enabling... 666 */ 667 uint32_t oval, nval; 668 669 do { 670 oval = *counter; 671 672 if ((nval = oval + 1) == 0) { 673 /* 674 * If the counter would wrap, set it to 1 -- assuring 675 * that the counter is never zero when we have seen 676 * errors. (The counter must be 32-bits because we 677 * aren't guaranteed a 64-bit compare&swap operation.) 678 * To save this code both the infamy of being fingered 679 * by a priggish news story and the indignity of being 680 * the target of a neo-puritan witch trial, we're 681 * carefully avoiding any colorful description of the 682 * likelihood of this condition -- but suffice it to 683 * say that it is only slightly more likely than the 684 * overflow of predicate cache IDs, as discussed in 685 * dtrace_predicate_create(). 686 */ 687 nval = 1; 688 } 689 } while (dtrace_cas32(counter, oval, nval) != oval); 690 } 691 692 /* 693 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 694 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 695 */ 696 /* BEGIN CSTYLED */ 697 DTRACE_LOADFUNC(8) 698 DTRACE_LOADFUNC(16) 699 DTRACE_LOADFUNC(32) 700 DTRACE_LOADFUNC(64) 701 /* END CSTYLED */ 702 703 static int 704 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 705 { 706 if (dest < mstate->dtms_scratch_base) 707 return (0); 708 709 if (dest + size < dest) 710 return (0); 711 712 if (dest + size > mstate->dtms_scratch_ptr) 713 return (0); 714 715 return (1); 716 } 717 718 static int 719 dtrace_canstore_statvar(uint64_t addr, size_t sz, size_t *remain, 720 dtrace_statvar_t **svars, int nsvars) 721 { 722 int i; 723 size_t maxglobalsize, maxlocalsize; 724 725 if (nsvars == 0) 726 return (0); 727 728 maxglobalsize = dtrace_statvar_maxsize + sizeof (uint64_t); 729 maxlocalsize = maxglobalsize * NCPU; 730 731 for (i = 0; i < nsvars; i++) { 732 dtrace_statvar_t *svar = svars[i]; 733 uint8_t scope; 734 size_t size; 735 736 if (svar == NULL || (size = svar->dtsv_size) == 0) 737 continue; 738 739 scope = svar->dtsv_var.dtdv_scope; 740 741 /* 742 * We verify that our size is valid in the spirit of providing 743 * defense in depth: we want to prevent attackers from using 744 * DTrace to escalate an orthogonal kernel heap corruption bug 745 * into the ability to store to arbitrary locations in memory. 746 */ 747 VERIFY((scope == DIFV_SCOPE_GLOBAL && size <= maxglobalsize) || 748 (scope == DIFV_SCOPE_LOCAL && size <= maxlocalsize)); 749 750 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, 751 svar->dtsv_size)) { 752 DTRACE_RANGE_REMAIN(remain, addr, svar->dtsv_data, 753 svar->dtsv_size); 754 return (1); 755 } 756 } 757 758 return (0); 759 } 760 761 /* 762 * Check to see if the address is within a memory region to which a store may 763 * be issued. This includes the DTrace scratch areas, and any DTrace variable 764 * region. The caller of dtrace_canstore() is responsible for performing any 765 * alignment checks that are needed before stores are actually executed. 766 */ 767 static int 768 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 769 dtrace_vstate_t *vstate) 770 { 771 return (dtrace_canstore_remains(addr, sz, NULL, mstate, vstate)); 772 } 773 774 /* 775 * Implementation of dtrace_canstore which communicates the upper bound of the 776 * allowed memory region. 777 */ 778 static int 779 dtrace_canstore_remains(uint64_t addr, size_t sz, size_t *remain, 780 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 781 { 782 /* 783 * First, check to see if the address is in scratch space... 784 */ 785 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 786 mstate->dtms_scratch_size)) { 787 DTRACE_RANGE_REMAIN(remain, addr, mstate->dtms_scratch_base, 788 mstate->dtms_scratch_size); 789 return (1); 790 } 791 792 /* 793 * Now check to see if it's a dynamic variable. This check will pick 794 * up both thread-local variables and any global dynamically-allocated 795 * variables. 796 */ 797 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 798 vstate->dtvs_dynvars.dtds_size)) { 799 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 800 uintptr_t base = (uintptr_t)dstate->dtds_base + 801 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 802 uintptr_t chunkoffs; 803 dtrace_dynvar_t *dvar; 804 805 /* 806 * Before we assume that we can store here, we need to make 807 * sure that it isn't in our metadata -- storing to our 808 * dynamic variable metadata would corrupt our state. For 809 * the range to not include any dynamic variable metadata, 810 * it must: 811 * 812 * (1) Start above the hash table that is at the base of 813 * the dynamic variable space 814 * 815 * (2) Have a starting chunk offset that is beyond the 816 * dtrace_dynvar_t that is at the base of every chunk 817 * 818 * (3) Not span a chunk boundary 819 * 820 * (4) Not be in the tuple space of a dynamic variable 821 * 822 */ 823 if (addr < base) 824 return (0); 825 826 chunkoffs = (addr - base) % dstate->dtds_chunksize; 827 828 if (chunkoffs < sizeof (dtrace_dynvar_t)) 829 return (0); 830 831 if (chunkoffs + sz > dstate->dtds_chunksize) 832 return (0); 833 834 dvar = (dtrace_dynvar_t *)((uintptr_t)addr - chunkoffs); 835 836 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) 837 return (0); 838 839 if (chunkoffs < sizeof (dtrace_dynvar_t) + 840 ((dvar->dtdv_tuple.dtt_nkeys - 1) * sizeof (dtrace_key_t))) 841 return (0); 842 843 DTRACE_RANGE_REMAIN(remain, addr, dvar, dstate->dtds_chunksize); 844 return (1); 845 } 846 847 /* 848 * Finally, check the static local and global variables. These checks 849 * take the longest, so we perform them last. 850 */ 851 if (dtrace_canstore_statvar(addr, sz, remain, 852 vstate->dtvs_locals, vstate->dtvs_nlocals)) 853 return (1); 854 855 if (dtrace_canstore_statvar(addr, sz, remain, 856 vstate->dtvs_globals, vstate->dtvs_nglobals)) 857 return (1); 858 859 return (0); 860 } 861 862 863 /* 864 * Convenience routine to check to see if the address is within a memory 865 * region in which a load may be issued given the user's privilege level; 866 * if not, it sets the appropriate error flags and loads 'addr' into the 867 * illegal value slot. 868 * 869 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 870 * appropriate memory access protection. 871 */ 872 static int 873 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 874 dtrace_vstate_t *vstate) 875 { 876 return (dtrace_canload_remains(addr, sz, NULL, mstate, vstate)); 877 } 878 879 /* 880 * Implementation of dtrace_canload which communicates the uppoer bound of the 881 * allowed memory region. 882 */ 883 static int 884 dtrace_canload_remains(uint64_t addr, size_t sz, size_t *remain, 885 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 886 { 887 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 888 file_t *fp; 889 890 /* 891 * If we hold the privilege to read from kernel memory, then 892 * everything is readable. 893 */ 894 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 895 DTRACE_RANGE_REMAIN(remain, addr, addr, sz); 896 return (1); 897 } 898 899 /* 900 * You can obviously read that which you can store. 901 */ 902 if (dtrace_canstore_remains(addr, sz, remain, mstate, vstate)) 903 return (1); 904 905 /* 906 * We're allowed to read from our own string table. 907 */ 908 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 909 mstate->dtms_difo->dtdo_strlen)) { 910 DTRACE_RANGE_REMAIN(remain, addr, 911 mstate->dtms_difo->dtdo_strtab, 912 mstate->dtms_difo->dtdo_strlen); 913 return (1); 914 } 915 916 if (vstate->dtvs_state != NULL && 917 dtrace_priv_proc(vstate->dtvs_state)) { 918 proc_t *p; 919 920 /* 921 * When we have privileges to the current process, there are 922 * several context-related kernel structures that are safe to 923 * read, even absent the privilege to read from kernel memory. 924 * These reads are safe because these structures contain only 925 * state that (1) we're permitted to read, (2) is harmless or 926 * (3) contains pointers to additional kernel state that we're 927 * not permitted to read (and as such, do not present an 928 * opportunity for privilege escalation). Finally (and 929 * critically), because of the nature of their relation with 930 * the current thread context, the memory associated with these 931 * structures cannot change over the duration of probe context, 932 * and it is therefore impossible for this memory to be 933 * deallocated and reallocated as something else while it's 934 * being operated upon. 935 */ 936 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) { 937 DTRACE_RANGE_REMAIN(remain, addr, curthread, 938 sizeof (kthread_t)); 939 return (1); 940 } 941 942 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 943 sz, curthread->t_procp, sizeof (proc_t))) { 944 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_procp, 945 sizeof (proc_t)); 946 return (1); 947 } 948 949 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 950 curthread->t_cred, sizeof (cred_t))) { 951 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cred, 952 sizeof (cred_t)); 953 return (1); 954 } 955 956 #ifdef illumos 957 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 958 &(p->p_pidp->pid_id), sizeof (pid_t))) { 959 DTRACE_RANGE_REMAIN(remain, addr, &(p->p_pidp->pid_id), 960 sizeof (pid_t)); 961 return (1); 962 } 963 964 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 965 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 966 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cpu, 967 offsetof(cpu_t, cpu_pause_thread)); 968 return (1); 969 } 970 #endif 971 } 972 973 if ((fp = mstate->dtms_getf) != NULL) { 974 uintptr_t psz = sizeof (void *); 975 vnode_t *vp; 976 vnodeops_t *op; 977 978 /* 979 * When getf() returns a file_t, the enabling is implicitly 980 * granted the (transient) right to read the returned file_t 981 * as well as the v_path and v_op->vnop_name of the underlying 982 * vnode. These accesses are allowed after a successful 983 * getf() because the members that they refer to cannot change 984 * once set -- and the barrier logic in the kernel's closef() 985 * path assures that the file_t and its referenced vode_t 986 * cannot themselves be stale (that is, it impossible for 987 * either dtms_getf itself or its f_vnode member to reference 988 * freed memory). 989 */ 990 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) { 991 DTRACE_RANGE_REMAIN(remain, addr, fp, sizeof (file_t)); 992 return (1); 993 } 994 995 if ((vp = fp->f_vnode) != NULL) { 996 size_t slen; 997 #ifdef illumos 998 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) { 999 DTRACE_RANGE_REMAIN(remain, addr, &vp->v_path, 1000 psz); 1001 return (1); 1002 } 1003 slen = strlen(vp->v_path) + 1; 1004 if (DTRACE_INRANGE(addr, sz, vp->v_path, slen)) { 1005 DTRACE_RANGE_REMAIN(remain, addr, vp->v_path, 1006 slen); 1007 return (1); 1008 } 1009 #endif 1010 1011 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) { 1012 DTRACE_RANGE_REMAIN(remain, addr, &vp->v_op, 1013 psz); 1014 return (1); 1015 } 1016 1017 #ifdef illumos 1018 if ((op = vp->v_op) != NULL && 1019 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 1020 DTRACE_RANGE_REMAIN(remain, addr, 1021 &op->vnop_name, psz); 1022 return (1); 1023 } 1024 1025 if (op != NULL && op->vnop_name != NULL && 1026 DTRACE_INRANGE(addr, sz, op->vnop_name, 1027 (slen = strlen(op->vnop_name) + 1))) { 1028 DTRACE_RANGE_REMAIN(remain, addr, 1029 op->vnop_name, slen); 1030 return (1); 1031 } 1032 #endif 1033 } 1034 } 1035 1036 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 1037 *illval = addr; 1038 return (0); 1039 } 1040 1041 /* 1042 * Convenience routine to check to see if a given string is within a memory 1043 * region in which a load may be issued given the user's privilege level; 1044 * this exists so that we don't need to issue unnecessary dtrace_strlen() 1045 * calls in the event that the user has all privileges. 1046 */ 1047 static int 1048 dtrace_strcanload(uint64_t addr, size_t sz, size_t *remain, 1049 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1050 { 1051 size_t rsize; 1052 1053 /* 1054 * If we hold the privilege to read from kernel memory, then 1055 * everything is readable. 1056 */ 1057 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 1058 DTRACE_RANGE_REMAIN(remain, addr, addr, sz); 1059 return (1); 1060 } 1061 1062 /* 1063 * Even if the caller is uninterested in querying the remaining valid 1064 * range, it is required to ensure that the access is allowed. 1065 */ 1066 if (remain == NULL) { 1067 remain = &rsize; 1068 } 1069 if (dtrace_canload_remains(addr, 0, remain, mstate, vstate)) { 1070 size_t strsz; 1071 /* 1072 * Perform the strlen after determining the length of the 1073 * memory region which is accessible. This prevents timing 1074 * information from being used to find NULs in memory which is 1075 * not accessible to the caller. 1076 */ 1077 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, 1078 MIN(sz, *remain)); 1079 if (strsz <= *remain) { 1080 return (1); 1081 } 1082 } 1083 1084 return (0); 1085 } 1086 1087 /* 1088 * Convenience routine to check to see if a given variable is within a memory 1089 * region in which a load may be issued given the user's privilege level. 1090 */ 1091 static int 1092 dtrace_vcanload(void *src, dtrace_diftype_t *type, size_t *remain, 1093 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1094 { 1095 size_t sz; 1096 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1097 1098 /* 1099 * Calculate the max size before performing any checks since even 1100 * DTRACE_ACCESS_KERNEL-credentialed callers expect that this function 1101 * return the max length via 'remain'. 1102 */ 1103 if (type->dtdt_kind == DIF_TYPE_STRING) { 1104 dtrace_state_t *state = vstate->dtvs_state; 1105 1106 if (state != NULL) { 1107 sz = state->dts_options[DTRACEOPT_STRSIZE]; 1108 } else { 1109 /* 1110 * In helper context, we have a NULL state; fall back 1111 * to using the system-wide default for the string size 1112 * in this case. 1113 */ 1114 sz = dtrace_strsize_default; 1115 } 1116 } else { 1117 sz = type->dtdt_size; 1118 } 1119 1120 /* 1121 * If we hold the privilege to read from kernel memory, then 1122 * everything is readable. 1123 */ 1124 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) { 1125 DTRACE_RANGE_REMAIN(remain, (uintptr_t)src, src, sz); 1126 return (1); 1127 } 1128 1129 if (type->dtdt_kind == DIF_TYPE_STRING) { 1130 return (dtrace_strcanload((uintptr_t)src, sz, remain, mstate, 1131 vstate)); 1132 } 1133 return (dtrace_canload_remains((uintptr_t)src, sz, remain, mstate, 1134 vstate)); 1135 } 1136 1137 /* 1138 * Convert a string to a signed integer using safe loads. 1139 * 1140 * NOTE: This function uses various macros from strtolctype.h to manipulate 1141 * digit values, etc -- these have all been checked to ensure they make 1142 * no additional function calls. 1143 */ 1144 static int64_t 1145 dtrace_strtoll(char *input, int base, size_t limit) 1146 { 1147 uintptr_t pos = (uintptr_t)input; 1148 int64_t val = 0; 1149 int x; 1150 boolean_t neg = B_FALSE; 1151 char c, cc, ccc; 1152 uintptr_t end = pos + limit; 1153 1154 /* 1155 * Consume any whitespace preceding digits. 1156 */ 1157 while ((c = dtrace_load8(pos)) == ' ' || c == '\t') 1158 pos++; 1159 1160 /* 1161 * Handle an explicit sign if one is present. 1162 */ 1163 if (c == '-' || c == '+') { 1164 if (c == '-') 1165 neg = B_TRUE; 1166 c = dtrace_load8(++pos); 1167 } 1168 1169 /* 1170 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it 1171 * if present. 1172 */ 1173 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' || 1174 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) { 1175 pos += 2; 1176 c = ccc; 1177 } 1178 1179 /* 1180 * Read in contiguous digits until the first non-digit character. 1181 */ 1182 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base; 1183 c = dtrace_load8(++pos)) 1184 val = val * base + x; 1185 1186 return (neg ? -val : val); 1187 } 1188 1189 /* 1190 * Compare two strings using safe loads. 1191 */ 1192 static int 1193 dtrace_strncmp(char *s1, char *s2, size_t limit) 1194 { 1195 uint8_t c1, c2; 1196 volatile uint16_t *flags; 1197 1198 if (s1 == s2 || limit == 0) 1199 return (0); 1200 1201 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1202 1203 do { 1204 if (s1 == NULL) { 1205 c1 = '\0'; 1206 } else { 1207 c1 = dtrace_load8((uintptr_t)s1++); 1208 } 1209 1210 if (s2 == NULL) { 1211 c2 = '\0'; 1212 } else { 1213 c2 = dtrace_load8((uintptr_t)s2++); 1214 } 1215 1216 if (c1 != c2) 1217 return (c1 - c2); 1218 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 1219 1220 return (0); 1221 } 1222 1223 /* 1224 * Compute strlen(s) for a string using safe memory accesses. The additional 1225 * len parameter is used to specify a maximum length to ensure completion. 1226 */ 1227 static size_t 1228 dtrace_strlen(const char *s, size_t lim) 1229 { 1230 uint_t len; 1231 1232 for (len = 0; len != lim; len++) { 1233 if (dtrace_load8((uintptr_t)s++) == '\0') 1234 break; 1235 } 1236 1237 return (len); 1238 } 1239 1240 /* 1241 * Check if an address falls within a toxic region. 1242 */ 1243 static int 1244 dtrace_istoxic(uintptr_t kaddr, size_t size) 1245 { 1246 uintptr_t taddr, tsize; 1247 int i; 1248 1249 for (i = 0; i < dtrace_toxranges; i++) { 1250 taddr = dtrace_toxrange[i].dtt_base; 1251 tsize = dtrace_toxrange[i].dtt_limit - taddr; 1252 1253 if (kaddr - taddr < tsize) { 1254 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1255 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 1256 return (1); 1257 } 1258 1259 if (taddr - kaddr < size) { 1260 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1261 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 1262 return (1); 1263 } 1264 } 1265 1266 return (0); 1267 } 1268 1269 /* 1270 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 1271 * memory specified by the DIF program. The dst is assumed to be safe memory 1272 * that we can store to directly because it is managed by DTrace. As with 1273 * standard bcopy, overlapping copies are handled properly. 1274 */ 1275 static void 1276 dtrace_bcopy(const void *src, void *dst, size_t len) 1277 { 1278 if (len != 0) { 1279 uint8_t *s1 = dst; 1280 const uint8_t *s2 = src; 1281 1282 if (s1 <= s2) { 1283 do { 1284 *s1++ = dtrace_load8((uintptr_t)s2++); 1285 } while (--len != 0); 1286 } else { 1287 s2 += len; 1288 s1 += len; 1289 1290 do { 1291 *--s1 = dtrace_load8((uintptr_t)--s2); 1292 } while (--len != 0); 1293 } 1294 } 1295 } 1296 1297 /* 1298 * Copy src to dst using safe memory accesses, up to either the specified 1299 * length, or the point that a nul byte is encountered. The src is assumed to 1300 * be unsafe memory specified by the DIF program. The dst is assumed to be 1301 * safe memory that we can store to directly because it is managed by DTrace. 1302 * Unlike dtrace_bcopy(), overlapping regions are not handled. 1303 */ 1304 static void 1305 dtrace_strcpy(const void *src, void *dst, size_t len) 1306 { 1307 if (len != 0) { 1308 uint8_t *s1 = dst, c; 1309 const uint8_t *s2 = src; 1310 1311 do { 1312 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1313 } while (--len != 0 && c != '\0'); 1314 } 1315 } 1316 1317 /* 1318 * Copy src to dst, deriving the size and type from the specified (BYREF) 1319 * variable type. The src is assumed to be unsafe memory specified by the DIF 1320 * program. The dst is assumed to be DTrace variable memory that is of the 1321 * specified type; we assume that we can store to directly. 1322 */ 1323 static void 1324 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type, size_t limit) 1325 { 1326 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1327 1328 if (type->dtdt_kind == DIF_TYPE_STRING) { 1329 dtrace_strcpy(src, dst, MIN(type->dtdt_size, limit)); 1330 } else { 1331 dtrace_bcopy(src, dst, MIN(type->dtdt_size, limit)); 1332 } 1333 } 1334 1335 /* 1336 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1337 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1338 * safe memory that we can access directly because it is managed by DTrace. 1339 */ 1340 static int 1341 dtrace_bcmp(const void *s1, const void *s2, size_t len) 1342 { 1343 volatile uint16_t *flags; 1344 1345 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1346 1347 if (s1 == s2) 1348 return (0); 1349 1350 if (s1 == NULL || s2 == NULL) 1351 return (1); 1352 1353 if (s1 != s2 && len != 0) { 1354 const uint8_t *ps1 = s1; 1355 const uint8_t *ps2 = s2; 1356 1357 do { 1358 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1359 return (1); 1360 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1361 } 1362 return (0); 1363 } 1364 1365 /* 1366 * Zero the specified region using a simple byte-by-byte loop. Note that this 1367 * is for safe DTrace-managed memory only. 1368 */ 1369 static void 1370 dtrace_bzero(void *dst, size_t len) 1371 { 1372 uchar_t *cp; 1373 1374 for (cp = dst; len != 0; len--) 1375 *cp++ = 0; 1376 } 1377 1378 static void 1379 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1380 { 1381 uint64_t result[2]; 1382 1383 result[0] = addend1[0] + addend2[0]; 1384 result[1] = addend1[1] + addend2[1] + 1385 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1386 1387 sum[0] = result[0]; 1388 sum[1] = result[1]; 1389 } 1390 1391 /* 1392 * Shift the 128-bit value in a by b. If b is positive, shift left. 1393 * If b is negative, shift right. 1394 */ 1395 static void 1396 dtrace_shift_128(uint64_t *a, int b) 1397 { 1398 uint64_t mask; 1399 1400 if (b == 0) 1401 return; 1402 1403 if (b < 0) { 1404 b = -b; 1405 if (b >= 64) { 1406 a[0] = a[1] >> (b - 64); 1407 a[1] = 0; 1408 } else { 1409 a[0] >>= b; 1410 mask = 1LL << (64 - b); 1411 mask -= 1; 1412 a[0] |= ((a[1] & mask) << (64 - b)); 1413 a[1] >>= b; 1414 } 1415 } else { 1416 if (b >= 64) { 1417 a[1] = a[0] << (b - 64); 1418 a[0] = 0; 1419 } else { 1420 a[1] <<= b; 1421 mask = a[0] >> (64 - b); 1422 a[1] |= mask; 1423 a[0] <<= b; 1424 } 1425 } 1426 } 1427 1428 /* 1429 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1430 * use native multiplication on those, and then re-combine into the 1431 * resulting 128-bit value. 1432 * 1433 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1434 * hi1 * hi2 << 64 + 1435 * hi1 * lo2 << 32 + 1436 * hi2 * lo1 << 32 + 1437 * lo1 * lo2 1438 */ 1439 static void 1440 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1441 { 1442 uint64_t hi1, hi2, lo1, lo2; 1443 uint64_t tmp[2]; 1444 1445 hi1 = factor1 >> 32; 1446 hi2 = factor2 >> 32; 1447 1448 lo1 = factor1 & DT_MASK_LO; 1449 lo2 = factor2 & DT_MASK_LO; 1450 1451 product[0] = lo1 * lo2; 1452 product[1] = hi1 * hi2; 1453 1454 tmp[0] = hi1 * lo2; 1455 tmp[1] = 0; 1456 dtrace_shift_128(tmp, 32); 1457 dtrace_add_128(product, tmp, product); 1458 1459 tmp[0] = hi2 * lo1; 1460 tmp[1] = 0; 1461 dtrace_shift_128(tmp, 32); 1462 dtrace_add_128(product, tmp, product); 1463 } 1464 1465 /* 1466 * This privilege check should be used by actions and subroutines to 1467 * verify that the user credentials of the process that enabled the 1468 * invoking ECB match the target credentials 1469 */ 1470 static int 1471 dtrace_priv_proc_common_user(dtrace_state_t *state) 1472 { 1473 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1474 1475 /* 1476 * We should always have a non-NULL state cred here, since if cred 1477 * is null (anonymous tracing), we fast-path bypass this routine. 1478 */ 1479 ASSERT(s_cr != NULL); 1480 1481 if ((cr = CRED()) != NULL && 1482 s_cr->cr_uid == cr->cr_uid && 1483 s_cr->cr_uid == cr->cr_ruid && 1484 s_cr->cr_uid == cr->cr_suid && 1485 s_cr->cr_gid == cr->cr_gid && 1486 s_cr->cr_gid == cr->cr_rgid && 1487 s_cr->cr_gid == cr->cr_sgid) 1488 return (1); 1489 1490 return (0); 1491 } 1492 1493 /* 1494 * This privilege check should be used by actions and subroutines to 1495 * verify that the zone of the process that enabled the invoking ECB 1496 * matches the target credentials 1497 */ 1498 static int 1499 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1500 { 1501 #ifdef illumos 1502 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1503 1504 /* 1505 * We should always have a non-NULL state cred here, since if cred 1506 * is null (anonymous tracing), we fast-path bypass this routine. 1507 */ 1508 ASSERT(s_cr != NULL); 1509 1510 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1511 return (1); 1512 1513 return (0); 1514 #else 1515 return (1); 1516 #endif 1517 } 1518 1519 /* 1520 * This privilege check should be used by actions and subroutines to 1521 * verify that the process has not setuid or changed credentials. 1522 */ 1523 static int 1524 dtrace_priv_proc_common_nocd(void) 1525 { 1526 proc_t *proc; 1527 1528 if ((proc = ttoproc(curthread)) != NULL && 1529 !(proc->p_flag & SNOCD)) 1530 return (1); 1531 1532 return (0); 1533 } 1534 1535 static int 1536 dtrace_priv_proc_destructive(dtrace_state_t *state) 1537 { 1538 int action = state->dts_cred.dcr_action; 1539 1540 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1541 dtrace_priv_proc_common_zone(state) == 0) 1542 goto bad; 1543 1544 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1545 dtrace_priv_proc_common_user(state) == 0) 1546 goto bad; 1547 1548 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1549 dtrace_priv_proc_common_nocd() == 0) 1550 goto bad; 1551 1552 return (1); 1553 1554 bad: 1555 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1556 1557 return (0); 1558 } 1559 1560 static int 1561 dtrace_priv_proc_control(dtrace_state_t *state) 1562 { 1563 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1564 return (1); 1565 1566 if (dtrace_priv_proc_common_zone(state) && 1567 dtrace_priv_proc_common_user(state) && 1568 dtrace_priv_proc_common_nocd()) 1569 return (1); 1570 1571 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1572 1573 return (0); 1574 } 1575 1576 static int 1577 dtrace_priv_proc(dtrace_state_t *state) 1578 { 1579 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1580 return (1); 1581 1582 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1583 1584 return (0); 1585 } 1586 1587 static int 1588 dtrace_priv_kernel(dtrace_state_t *state) 1589 { 1590 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1591 return (1); 1592 1593 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1594 1595 return (0); 1596 } 1597 1598 static int 1599 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1600 { 1601 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1602 return (1); 1603 1604 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1605 1606 return (0); 1607 } 1608 1609 /* 1610 * Determine if the dte_cond of the specified ECB allows for processing of 1611 * the current probe to continue. Note that this routine may allow continued 1612 * processing, but with access(es) stripped from the mstate's dtms_access 1613 * field. 1614 */ 1615 static int 1616 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1617 dtrace_ecb_t *ecb) 1618 { 1619 dtrace_probe_t *probe = ecb->dte_probe; 1620 dtrace_provider_t *prov = probe->dtpr_provider; 1621 dtrace_pops_t *pops = &prov->dtpv_pops; 1622 int mode = DTRACE_MODE_NOPRIV_DROP; 1623 1624 ASSERT(ecb->dte_cond); 1625 1626 #ifdef illumos 1627 if (pops->dtps_mode != NULL) { 1628 mode = pops->dtps_mode(prov->dtpv_arg, 1629 probe->dtpr_id, probe->dtpr_arg); 1630 1631 ASSERT((mode & DTRACE_MODE_USER) || 1632 (mode & DTRACE_MODE_KERNEL)); 1633 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1634 (mode & DTRACE_MODE_NOPRIV_DROP)); 1635 } 1636 1637 /* 1638 * If the dte_cond bits indicate that this consumer is only allowed to 1639 * see user-mode firings of this probe, call the provider's dtps_mode() 1640 * entry point to check that the probe was fired while in a user 1641 * context. If that's not the case, use the policy specified by the 1642 * provider to determine if we drop the probe or merely restrict 1643 * operation. 1644 */ 1645 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1646 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1647 1648 if (!(mode & DTRACE_MODE_USER)) { 1649 if (mode & DTRACE_MODE_NOPRIV_DROP) 1650 return (0); 1651 1652 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1653 } 1654 } 1655 #endif 1656 1657 /* 1658 * This is more subtle than it looks. We have to be absolutely certain 1659 * that CRED() isn't going to change out from under us so it's only 1660 * legit to examine that structure if we're in constrained situations. 1661 * Currently, the only times we'll this check is if a non-super-user 1662 * has enabled the profile or syscall providers -- providers that 1663 * allow visibility of all processes. For the profile case, the check 1664 * above will ensure that we're examining a user context. 1665 */ 1666 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1667 cred_t *cr; 1668 cred_t *s_cr = state->dts_cred.dcr_cred; 1669 proc_t *proc; 1670 1671 ASSERT(s_cr != NULL); 1672 1673 if ((cr = CRED()) == NULL || 1674 s_cr->cr_uid != cr->cr_uid || 1675 s_cr->cr_uid != cr->cr_ruid || 1676 s_cr->cr_uid != cr->cr_suid || 1677 s_cr->cr_gid != cr->cr_gid || 1678 s_cr->cr_gid != cr->cr_rgid || 1679 s_cr->cr_gid != cr->cr_sgid || 1680 (proc = ttoproc(curthread)) == NULL || 1681 (proc->p_flag & SNOCD)) { 1682 if (mode & DTRACE_MODE_NOPRIV_DROP) 1683 return (0); 1684 1685 #ifdef illumos 1686 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1687 #endif 1688 } 1689 } 1690 1691 #ifdef illumos 1692 /* 1693 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1694 * in our zone, check to see if our mode policy is to restrict rather 1695 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1696 * and DTRACE_ACCESS_ARGS 1697 */ 1698 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1699 cred_t *cr; 1700 cred_t *s_cr = state->dts_cred.dcr_cred; 1701 1702 ASSERT(s_cr != NULL); 1703 1704 if ((cr = CRED()) == NULL || 1705 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1706 if (mode & DTRACE_MODE_NOPRIV_DROP) 1707 return (0); 1708 1709 mstate->dtms_access &= 1710 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1711 } 1712 } 1713 #endif 1714 1715 return (1); 1716 } 1717 1718 /* 1719 * Note: not called from probe context. This function is called 1720 * asynchronously (and at a regular interval) from outside of probe context to 1721 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1722 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1723 */ 1724 void 1725 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1726 { 1727 dtrace_dynvar_t *dirty; 1728 dtrace_dstate_percpu_t *dcpu; 1729 dtrace_dynvar_t **rinsep; 1730 int i, j, work = 0; 1731 1732 for (i = 0; i < NCPU; i++) { 1733 dcpu = &dstate->dtds_percpu[i]; 1734 rinsep = &dcpu->dtdsc_rinsing; 1735 1736 /* 1737 * If the dirty list is NULL, there is no dirty work to do. 1738 */ 1739 if (dcpu->dtdsc_dirty == NULL) 1740 continue; 1741 1742 if (dcpu->dtdsc_rinsing != NULL) { 1743 /* 1744 * If the rinsing list is non-NULL, then it is because 1745 * this CPU was selected to accept another CPU's 1746 * dirty list -- and since that time, dirty buffers 1747 * have accumulated. This is a highly unlikely 1748 * condition, but we choose to ignore the dirty 1749 * buffers -- they'll be picked up a future cleanse. 1750 */ 1751 continue; 1752 } 1753 1754 if (dcpu->dtdsc_clean != NULL) { 1755 /* 1756 * If the clean list is non-NULL, then we're in a 1757 * situation where a CPU has done deallocations (we 1758 * have a non-NULL dirty list) but no allocations (we 1759 * also have a non-NULL clean list). We can't simply 1760 * move the dirty list into the clean list on this 1761 * CPU, yet we also don't want to allow this condition 1762 * to persist, lest a short clean list prevent a 1763 * massive dirty list from being cleaned (which in 1764 * turn could lead to otherwise avoidable dynamic 1765 * drops). To deal with this, we look for some CPU 1766 * with a NULL clean list, NULL dirty list, and NULL 1767 * rinsing list -- and then we borrow this CPU to 1768 * rinse our dirty list. 1769 */ 1770 for (j = 0; j < NCPU; j++) { 1771 dtrace_dstate_percpu_t *rinser; 1772 1773 rinser = &dstate->dtds_percpu[j]; 1774 1775 if (rinser->dtdsc_rinsing != NULL) 1776 continue; 1777 1778 if (rinser->dtdsc_dirty != NULL) 1779 continue; 1780 1781 if (rinser->dtdsc_clean != NULL) 1782 continue; 1783 1784 rinsep = &rinser->dtdsc_rinsing; 1785 break; 1786 } 1787 1788 if (j == NCPU) { 1789 /* 1790 * We were unable to find another CPU that 1791 * could accept this dirty list -- we are 1792 * therefore unable to clean it now. 1793 */ 1794 dtrace_dynvar_failclean++; 1795 continue; 1796 } 1797 } 1798 1799 work = 1; 1800 1801 /* 1802 * Atomically move the dirty list aside. 1803 */ 1804 do { 1805 dirty = dcpu->dtdsc_dirty; 1806 1807 /* 1808 * Before we zap the dirty list, set the rinsing list. 1809 * (This allows for a potential assertion in 1810 * dtrace_dynvar(): if a free dynamic variable appears 1811 * on a hash chain, either the dirty list or the 1812 * rinsing list for some CPU must be non-NULL.) 1813 */ 1814 *rinsep = dirty; 1815 dtrace_membar_producer(); 1816 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1817 dirty, NULL) != dirty); 1818 } 1819 1820 if (!work) { 1821 /* 1822 * We have no work to do; we can simply return. 1823 */ 1824 return; 1825 } 1826 1827 dtrace_sync(); 1828 1829 for (i = 0; i < NCPU; i++) { 1830 dcpu = &dstate->dtds_percpu[i]; 1831 1832 if (dcpu->dtdsc_rinsing == NULL) 1833 continue; 1834 1835 /* 1836 * We are now guaranteed that no hash chain contains a pointer 1837 * into this dirty list; we can make it clean. 1838 */ 1839 ASSERT(dcpu->dtdsc_clean == NULL); 1840 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1841 dcpu->dtdsc_rinsing = NULL; 1842 } 1843 1844 /* 1845 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1846 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1847 * This prevents a race whereby a CPU incorrectly decides that 1848 * the state should be something other than DTRACE_DSTATE_CLEAN 1849 * after dtrace_dynvar_clean() has completed. 1850 */ 1851 dtrace_sync(); 1852 1853 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1854 } 1855 1856 /* 1857 * Depending on the value of the op parameter, this function looks-up, 1858 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1859 * allocation is requested, this function will return a pointer to a 1860 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1861 * variable can be allocated. If NULL is returned, the appropriate counter 1862 * will be incremented. 1863 */ 1864 dtrace_dynvar_t * 1865 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1866 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1867 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1868 { 1869 uint64_t hashval = DTRACE_DYNHASH_VALID; 1870 dtrace_dynhash_t *hash = dstate->dtds_hash; 1871 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1872 processorid_t me = curcpu, cpu = me; 1873 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1874 size_t bucket, ksize; 1875 size_t chunksize = dstate->dtds_chunksize; 1876 uintptr_t kdata, lock, nstate; 1877 uint_t i; 1878 1879 ASSERT(nkeys != 0); 1880 1881 /* 1882 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1883 * algorithm. For the by-value portions, we perform the algorithm in 1884 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1885 * bit, and seems to have only a minute effect on distribution. For 1886 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1887 * over each referenced byte. It's painful to do this, but it's much 1888 * better than pathological hash distribution. The efficacy of the 1889 * hashing algorithm (and a comparison with other algorithms) may be 1890 * found by running the ::dtrace_dynstat MDB dcmd. 1891 */ 1892 for (i = 0; i < nkeys; i++) { 1893 if (key[i].dttk_size == 0) { 1894 uint64_t val = key[i].dttk_value; 1895 1896 hashval += (val >> 48) & 0xffff; 1897 hashval += (hashval << 10); 1898 hashval ^= (hashval >> 6); 1899 1900 hashval += (val >> 32) & 0xffff; 1901 hashval += (hashval << 10); 1902 hashval ^= (hashval >> 6); 1903 1904 hashval += (val >> 16) & 0xffff; 1905 hashval += (hashval << 10); 1906 hashval ^= (hashval >> 6); 1907 1908 hashval += val & 0xffff; 1909 hashval += (hashval << 10); 1910 hashval ^= (hashval >> 6); 1911 } else { 1912 /* 1913 * This is incredibly painful, but it beats the hell 1914 * out of the alternative. 1915 */ 1916 uint64_t j, size = key[i].dttk_size; 1917 uintptr_t base = (uintptr_t)key[i].dttk_value; 1918 1919 if (!dtrace_canload(base, size, mstate, vstate)) 1920 break; 1921 1922 for (j = 0; j < size; j++) { 1923 hashval += dtrace_load8(base + j); 1924 hashval += (hashval << 10); 1925 hashval ^= (hashval >> 6); 1926 } 1927 } 1928 } 1929 1930 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1931 return (NULL); 1932 1933 hashval += (hashval << 3); 1934 hashval ^= (hashval >> 11); 1935 hashval += (hashval << 15); 1936 1937 /* 1938 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1939 * comes out to be one of our two sentinel hash values. If this 1940 * actually happens, we set the hashval to be a value known to be a 1941 * non-sentinel value. 1942 */ 1943 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1944 hashval = DTRACE_DYNHASH_VALID; 1945 1946 /* 1947 * Yes, it's painful to do a divide here. If the cycle count becomes 1948 * important here, tricks can be pulled to reduce it. (However, it's 1949 * critical that hash collisions be kept to an absolute minimum; 1950 * they're much more painful than a divide.) It's better to have a 1951 * solution that generates few collisions and still keeps things 1952 * relatively simple. 1953 */ 1954 bucket = hashval % dstate->dtds_hashsize; 1955 1956 if (op == DTRACE_DYNVAR_DEALLOC) { 1957 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1958 1959 for (;;) { 1960 while ((lock = *lockp) & 1) 1961 continue; 1962 1963 if (dtrace_casptr((volatile void *)lockp, 1964 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1965 break; 1966 } 1967 1968 dtrace_membar_producer(); 1969 } 1970 1971 top: 1972 prev = NULL; 1973 lock = hash[bucket].dtdh_lock; 1974 1975 dtrace_membar_consumer(); 1976 1977 start = hash[bucket].dtdh_chain; 1978 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1979 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1980 op != DTRACE_DYNVAR_DEALLOC)); 1981 1982 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1983 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1984 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1985 1986 if (dvar->dtdv_hashval != hashval) { 1987 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1988 /* 1989 * We've reached the sink, and therefore the 1990 * end of the hash chain; we can kick out of 1991 * the loop knowing that we have seen a valid 1992 * snapshot of state. 1993 */ 1994 ASSERT(dvar->dtdv_next == NULL); 1995 ASSERT(dvar == &dtrace_dynhash_sink); 1996 break; 1997 } 1998 1999 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 2000 /* 2001 * We've gone off the rails: somewhere along 2002 * the line, one of the members of this hash 2003 * chain was deleted. Note that we could also 2004 * detect this by simply letting this loop run 2005 * to completion, as we would eventually hit 2006 * the end of the dirty list. However, we 2007 * want to avoid running the length of the 2008 * dirty list unnecessarily (it might be quite 2009 * long), so we catch this as early as 2010 * possible by detecting the hash marker. In 2011 * this case, we simply set dvar to NULL and 2012 * break; the conditional after the loop will 2013 * send us back to top. 2014 */ 2015 dvar = NULL; 2016 break; 2017 } 2018 2019 goto next; 2020 } 2021 2022 if (dtuple->dtt_nkeys != nkeys) 2023 goto next; 2024 2025 for (i = 0; i < nkeys; i++, dkey++) { 2026 if (dkey->dttk_size != key[i].dttk_size) 2027 goto next; /* size or type mismatch */ 2028 2029 if (dkey->dttk_size != 0) { 2030 if (dtrace_bcmp( 2031 (void *)(uintptr_t)key[i].dttk_value, 2032 (void *)(uintptr_t)dkey->dttk_value, 2033 dkey->dttk_size)) 2034 goto next; 2035 } else { 2036 if (dkey->dttk_value != key[i].dttk_value) 2037 goto next; 2038 } 2039 } 2040 2041 if (op != DTRACE_DYNVAR_DEALLOC) 2042 return (dvar); 2043 2044 ASSERT(dvar->dtdv_next == NULL || 2045 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 2046 2047 if (prev != NULL) { 2048 ASSERT(hash[bucket].dtdh_chain != dvar); 2049 ASSERT(start != dvar); 2050 ASSERT(prev->dtdv_next == dvar); 2051 prev->dtdv_next = dvar->dtdv_next; 2052 } else { 2053 if (dtrace_casptr(&hash[bucket].dtdh_chain, 2054 start, dvar->dtdv_next) != start) { 2055 /* 2056 * We have failed to atomically swing the 2057 * hash table head pointer, presumably because 2058 * of a conflicting allocation on another CPU. 2059 * We need to reread the hash chain and try 2060 * again. 2061 */ 2062 goto top; 2063 } 2064 } 2065 2066 dtrace_membar_producer(); 2067 2068 /* 2069 * Now set the hash value to indicate that it's free. 2070 */ 2071 ASSERT(hash[bucket].dtdh_chain != dvar); 2072 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2073 2074 dtrace_membar_producer(); 2075 2076 /* 2077 * Set the next pointer to point at the dirty list, and 2078 * atomically swing the dirty pointer to the newly freed dvar. 2079 */ 2080 do { 2081 next = dcpu->dtdsc_dirty; 2082 dvar->dtdv_next = next; 2083 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 2084 2085 /* 2086 * Finally, unlock this hash bucket. 2087 */ 2088 ASSERT(hash[bucket].dtdh_lock == lock); 2089 ASSERT(lock & 1); 2090 hash[bucket].dtdh_lock++; 2091 2092 return (NULL); 2093 next: 2094 prev = dvar; 2095 continue; 2096 } 2097 2098 if (dvar == NULL) { 2099 /* 2100 * If dvar is NULL, it is because we went off the rails: 2101 * one of the elements that we traversed in the hash chain 2102 * was deleted while we were traversing it. In this case, 2103 * we assert that we aren't doing a dealloc (deallocs lock 2104 * the hash bucket to prevent themselves from racing with 2105 * one another), and retry the hash chain traversal. 2106 */ 2107 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 2108 goto top; 2109 } 2110 2111 if (op != DTRACE_DYNVAR_ALLOC) { 2112 /* 2113 * If we are not to allocate a new variable, we want to 2114 * return NULL now. Before we return, check that the value 2115 * of the lock word hasn't changed. If it has, we may have 2116 * seen an inconsistent snapshot. 2117 */ 2118 if (op == DTRACE_DYNVAR_NOALLOC) { 2119 if (hash[bucket].dtdh_lock != lock) 2120 goto top; 2121 } else { 2122 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 2123 ASSERT(hash[bucket].dtdh_lock == lock); 2124 ASSERT(lock & 1); 2125 hash[bucket].dtdh_lock++; 2126 } 2127 2128 return (NULL); 2129 } 2130 2131 /* 2132 * We need to allocate a new dynamic variable. The size we need is the 2133 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 2134 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 2135 * the size of any referred-to data (dsize). We then round the final 2136 * size up to the chunksize for allocation. 2137 */ 2138 for (ksize = 0, i = 0; i < nkeys; i++) 2139 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 2140 2141 /* 2142 * This should be pretty much impossible, but could happen if, say, 2143 * strange DIF specified the tuple. Ideally, this should be an 2144 * assertion and not an error condition -- but that requires that the 2145 * chunksize calculation in dtrace_difo_chunksize() be absolutely 2146 * bullet-proof. (That is, it must not be able to be fooled by 2147 * malicious DIF.) Given the lack of backwards branches in DIF, 2148 * solving this would presumably not amount to solving the Halting 2149 * Problem -- but it still seems awfully hard. 2150 */ 2151 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 2152 ksize + dsize > chunksize) { 2153 dcpu->dtdsc_drops++; 2154 return (NULL); 2155 } 2156 2157 nstate = DTRACE_DSTATE_EMPTY; 2158 2159 do { 2160 retry: 2161 free = dcpu->dtdsc_free; 2162 2163 if (free == NULL) { 2164 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 2165 void *rval; 2166 2167 if (clean == NULL) { 2168 /* 2169 * We're out of dynamic variable space on 2170 * this CPU. Unless we have tried all CPUs, 2171 * we'll try to allocate from a different 2172 * CPU. 2173 */ 2174 switch (dstate->dtds_state) { 2175 case DTRACE_DSTATE_CLEAN: { 2176 void *sp = &dstate->dtds_state; 2177 2178 if (++cpu >= NCPU) 2179 cpu = 0; 2180 2181 if (dcpu->dtdsc_dirty != NULL && 2182 nstate == DTRACE_DSTATE_EMPTY) 2183 nstate = DTRACE_DSTATE_DIRTY; 2184 2185 if (dcpu->dtdsc_rinsing != NULL) 2186 nstate = DTRACE_DSTATE_RINSING; 2187 2188 dcpu = &dstate->dtds_percpu[cpu]; 2189 2190 if (cpu != me) 2191 goto retry; 2192 2193 (void) dtrace_cas32(sp, 2194 DTRACE_DSTATE_CLEAN, nstate); 2195 2196 /* 2197 * To increment the correct bean 2198 * counter, take another lap. 2199 */ 2200 goto retry; 2201 } 2202 2203 case DTRACE_DSTATE_DIRTY: 2204 dcpu->dtdsc_dirty_drops++; 2205 break; 2206 2207 case DTRACE_DSTATE_RINSING: 2208 dcpu->dtdsc_rinsing_drops++; 2209 break; 2210 2211 case DTRACE_DSTATE_EMPTY: 2212 dcpu->dtdsc_drops++; 2213 break; 2214 } 2215 2216 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 2217 return (NULL); 2218 } 2219 2220 /* 2221 * The clean list appears to be non-empty. We want to 2222 * move the clean list to the free list; we start by 2223 * moving the clean pointer aside. 2224 */ 2225 if (dtrace_casptr(&dcpu->dtdsc_clean, 2226 clean, NULL) != clean) { 2227 /* 2228 * We are in one of two situations: 2229 * 2230 * (a) The clean list was switched to the 2231 * free list by another CPU. 2232 * 2233 * (b) The clean list was added to by the 2234 * cleansing cyclic. 2235 * 2236 * In either of these situations, we can 2237 * just reattempt the free list allocation. 2238 */ 2239 goto retry; 2240 } 2241 2242 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 2243 2244 /* 2245 * Now we'll move the clean list to our free list. 2246 * It's impossible for this to fail: the only way 2247 * the free list can be updated is through this 2248 * code path, and only one CPU can own the clean list. 2249 * Thus, it would only be possible for this to fail if 2250 * this code were racing with dtrace_dynvar_clean(). 2251 * (That is, if dtrace_dynvar_clean() updated the clean 2252 * list, and we ended up racing to update the free 2253 * list.) This race is prevented by the dtrace_sync() 2254 * in dtrace_dynvar_clean() -- which flushes the 2255 * owners of the clean lists out before resetting 2256 * the clean lists. 2257 */ 2258 dcpu = &dstate->dtds_percpu[me]; 2259 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 2260 ASSERT(rval == NULL); 2261 goto retry; 2262 } 2263 2264 dvar = free; 2265 new_free = dvar->dtdv_next; 2266 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 2267 2268 /* 2269 * We have now allocated a new chunk. We copy the tuple keys into the 2270 * tuple array and copy any referenced key data into the data space 2271 * following the tuple array. As we do this, we relocate dttk_value 2272 * in the final tuple to point to the key data address in the chunk. 2273 */ 2274 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 2275 dvar->dtdv_data = (void *)(kdata + ksize); 2276 dvar->dtdv_tuple.dtt_nkeys = nkeys; 2277 2278 for (i = 0; i < nkeys; i++) { 2279 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 2280 size_t kesize = key[i].dttk_size; 2281 2282 if (kesize != 0) { 2283 dtrace_bcopy( 2284 (const void *)(uintptr_t)key[i].dttk_value, 2285 (void *)kdata, kesize); 2286 dkey->dttk_value = kdata; 2287 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 2288 } else { 2289 dkey->dttk_value = key[i].dttk_value; 2290 } 2291 2292 dkey->dttk_size = kesize; 2293 } 2294 2295 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 2296 dvar->dtdv_hashval = hashval; 2297 dvar->dtdv_next = start; 2298 2299 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 2300 return (dvar); 2301 2302 /* 2303 * The cas has failed. Either another CPU is adding an element to 2304 * this hash chain, or another CPU is deleting an element from this 2305 * hash chain. The simplest way to deal with both of these cases 2306 * (though not necessarily the most efficient) is to free our 2307 * allocated block and re-attempt it all. Note that the free is 2308 * to the dirty list and _not_ to the free list. This is to prevent 2309 * races with allocators, above. 2310 */ 2311 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2312 2313 dtrace_membar_producer(); 2314 2315 do { 2316 free = dcpu->dtdsc_dirty; 2317 dvar->dtdv_next = free; 2318 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 2319 2320 goto top; 2321 } 2322 2323 /*ARGSUSED*/ 2324 static void 2325 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 2326 { 2327 if ((int64_t)nval < (int64_t)*oval) 2328 *oval = nval; 2329 } 2330 2331 /*ARGSUSED*/ 2332 static void 2333 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2334 { 2335 if ((int64_t)nval > (int64_t)*oval) 2336 *oval = nval; 2337 } 2338 2339 static void 2340 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2341 { 2342 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2343 int64_t val = (int64_t)nval; 2344 2345 if (val < 0) { 2346 for (i = 0; i < zero; i++) { 2347 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2348 quanta[i] += incr; 2349 return; 2350 } 2351 } 2352 } else { 2353 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2354 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2355 quanta[i - 1] += incr; 2356 return; 2357 } 2358 } 2359 2360 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2361 return; 2362 } 2363 2364 ASSERT(0); 2365 } 2366 2367 static void 2368 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2369 { 2370 uint64_t arg = *lquanta++; 2371 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2372 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2373 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2374 int32_t val = (int32_t)nval, level; 2375 2376 ASSERT(step != 0); 2377 ASSERT(levels != 0); 2378 2379 if (val < base) { 2380 /* 2381 * This is an underflow. 2382 */ 2383 lquanta[0] += incr; 2384 return; 2385 } 2386 2387 level = (val - base) / step; 2388 2389 if (level < levels) { 2390 lquanta[level + 1] += incr; 2391 return; 2392 } 2393 2394 /* 2395 * This is an overflow. 2396 */ 2397 lquanta[levels + 1] += incr; 2398 } 2399 2400 static int 2401 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2402 uint16_t high, uint16_t nsteps, int64_t value) 2403 { 2404 int64_t this = 1, last, next; 2405 int base = 1, order; 2406 2407 ASSERT(factor <= nsteps); 2408 ASSERT(nsteps % factor == 0); 2409 2410 for (order = 0; order < low; order++) 2411 this *= factor; 2412 2413 /* 2414 * If our value is less than our factor taken to the power of the 2415 * low order of magnitude, it goes into the zeroth bucket. 2416 */ 2417 if (value < (last = this)) 2418 return (0); 2419 2420 for (this *= factor; order <= high; order++) { 2421 int nbuckets = this > nsteps ? nsteps : this; 2422 2423 if ((next = this * factor) < this) { 2424 /* 2425 * We should not generally get log/linear quantizations 2426 * with a high magnitude that allows 64-bits to 2427 * overflow, but we nonetheless protect against this 2428 * by explicitly checking for overflow, and clamping 2429 * our value accordingly. 2430 */ 2431 value = this - 1; 2432 } 2433 2434 if (value < this) { 2435 /* 2436 * If our value lies within this order of magnitude, 2437 * determine its position by taking the offset within 2438 * the order of magnitude, dividing by the bucket 2439 * width, and adding to our (accumulated) base. 2440 */ 2441 return (base + (value - last) / (this / nbuckets)); 2442 } 2443 2444 base += nbuckets - (nbuckets / factor); 2445 last = this; 2446 this = next; 2447 } 2448 2449 /* 2450 * Our value is greater than or equal to our factor taken to the 2451 * power of one plus the high magnitude -- return the top bucket. 2452 */ 2453 return (base); 2454 } 2455 2456 static void 2457 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2458 { 2459 uint64_t arg = *llquanta++; 2460 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2461 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2462 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2463 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2464 2465 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2466 low, high, nsteps, nval)] += incr; 2467 } 2468 2469 /*ARGSUSED*/ 2470 static void 2471 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2472 { 2473 data[0]++; 2474 data[1] += nval; 2475 } 2476 2477 /*ARGSUSED*/ 2478 static void 2479 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2480 { 2481 int64_t snval = (int64_t)nval; 2482 uint64_t tmp[2]; 2483 2484 data[0]++; 2485 data[1] += nval; 2486 2487 /* 2488 * What we want to say here is: 2489 * 2490 * data[2] += nval * nval; 2491 * 2492 * But given that nval is 64-bit, we could easily overflow, so 2493 * we do this as 128-bit arithmetic. 2494 */ 2495 if (snval < 0) 2496 snval = -snval; 2497 2498 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2499 dtrace_add_128(data + 2, tmp, data + 2); 2500 } 2501 2502 /*ARGSUSED*/ 2503 static void 2504 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2505 { 2506 *oval = *oval + 1; 2507 } 2508 2509 /*ARGSUSED*/ 2510 static void 2511 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2512 { 2513 *oval += nval; 2514 } 2515 2516 /* 2517 * Aggregate given the tuple in the principal data buffer, and the aggregating 2518 * action denoted by the specified dtrace_aggregation_t. The aggregation 2519 * buffer is specified as the buf parameter. This routine does not return 2520 * failure; if there is no space in the aggregation buffer, the data will be 2521 * dropped, and a corresponding counter incremented. 2522 */ 2523 static void 2524 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2525 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2526 { 2527 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2528 uint32_t i, ndx, size, fsize; 2529 uint32_t align = sizeof (uint64_t) - 1; 2530 dtrace_aggbuffer_t *agb; 2531 dtrace_aggkey_t *key; 2532 uint32_t hashval = 0, limit, isstr; 2533 caddr_t tomax, data, kdata; 2534 dtrace_actkind_t action; 2535 dtrace_action_t *act; 2536 uintptr_t offs; 2537 2538 if (buf == NULL) 2539 return; 2540 2541 if (!agg->dtag_hasarg) { 2542 /* 2543 * Currently, only quantize() and lquantize() take additional 2544 * arguments, and they have the same semantics: an increment 2545 * value that defaults to 1 when not present. If additional 2546 * aggregating actions take arguments, the setting of the 2547 * default argument value will presumably have to become more 2548 * sophisticated... 2549 */ 2550 arg = 1; 2551 } 2552 2553 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2554 size = rec->dtrd_offset - agg->dtag_base; 2555 fsize = size + rec->dtrd_size; 2556 2557 ASSERT(dbuf->dtb_tomax != NULL); 2558 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2559 2560 if ((tomax = buf->dtb_tomax) == NULL) { 2561 dtrace_buffer_drop(buf); 2562 return; 2563 } 2564 2565 /* 2566 * The metastructure is always at the bottom of the buffer. 2567 */ 2568 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2569 sizeof (dtrace_aggbuffer_t)); 2570 2571 if (buf->dtb_offset == 0) { 2572 /* 2573 * We just kludge up approximately 1/8th of the size to be 2574 * buckets. If this guess ends up being routinely 2575 * off-the-mark, we may need to dynamically readjust this 2576 * based on past performance. 2577 */ 2578 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2579 2580 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2581 (uintptr_t)tomax || hashsize == 0) { 2582 /* 2583 * We've been given a ludicrously small buffer; 2584 * increment our drop count and leave. 2585 */ 2586 dtrace_buffer_drop(buf); 2587 return; 2588 } 2589 2590 /* 2591 * And now, a pathetic attempt to try to get a an odd (or 2592 * perchance, a prime) hash size for better hash distribution. 2593 */ 2594 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2595 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2596 2597 agb->dtagb_hashsize = hashsize; 2598 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2599 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2600 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2601 2602 for (i = 0; i < agb->dtagb_hashsize; i++) 2603 agb->dtagb_hash[i] = NULL; 2604 } 2605 2606 ASSERT(agg->dtag_first != NULL); 2607 ASSERT(agg->dtag_first->dta_intuple); 2608 2609 /* 2610 * Calculate the hash value based on the key. Note that we _don't_ 2611 * include the aggid in the hashing (but we will store it as part of 2612 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2613 * algorithm: a simple, quick algorithm that has no known funnels, and 2614 * gets good distribution in practice. The efficacy of the hashing 2615 * algorithm (and a comparison with other algorithms) may be found by 2616 * running the ::dtrace_aggstat MDB dcmd. 2617 */ 2618 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2619 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2620 limit = i + act->dta_rec.dtrd_size; 2621 ASSERT(limit <= size); 2622 isstr = DTRACEACT_ISSTRING(act); 2623 2624 for (; i < limit; i++) { 2625 hashval += data[i]; 2626 hashval += (hashval << 10); 2627 hashval ^= (hashval >> 6); 2628 2629 if (isstr && data[i] == '\0') 2630 break; 2631 } 2632 } 2633 2634 hashval += (hashval << 3); 2635 hashval ^= (hashval >> 11); 2636 hashval += (hashval << 15); 2637 2638 /* 2639 * Yes, the divide here is expensive -- but it's generally the least 2640 * of the performance issues given the amount of data that we iterate 2641 * over to compute hash values, compare data, etc. 2642 */ 2643 ndx = hashval % agb->dtagb_hashsize; 2644 2645 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2646 ASSERT((caddr_t)key >= tomax); 2647 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2648 2649 if (hashval != key->dtak_hashval || key->dtak_size != size) 2650 continue; 2651 2652 kdata = key->dtak_data; 2653 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2654 2655 for (act = agg->dtag_first; act->dta_intuple; 2656 act = act->dta_next) { 2657 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2658 limit = i + act->dta_rec.dtrd_size; 2659 ASSERT(limit <= size); 2660 isstr = DTRACEACT_ISSTRING(act); 2661 2662 for (; i < limit; i++) { 2663 if (kdata[i] != data[i]) 2664 goto next; 2665 2666 if (isstr && data[i] == '\0') 2667 break; 2668 } 2669 } 2670 2671 if (action != key->dtak_action) { 2672 /* 2673 * We are aggregating on the same value in the same 2674 * aggregation with two different aggregating actions. 2675 * (This should have been picked up in the compiler, 2676 * so we may be dealing with errant or devious DIF.) 2677 * This is an error condition; we indicate as much, 2678 * and return. 2679 */ 2680 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2681 return; 2682 } 2683 2684 /* 2685 * This is a hit: we need to apply the aggregator to 2686 * the value at this key. 2687 */ 2688 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2689 return; 2690 next: 2691 continue; 2692 } 2693 2694 /* 2695 * We didn't find it. We need to allocate some zero-filled space, 2696 * link it into the hash table appropriately, and apply the aggregator 2697 * to the (zero-filled) value. 2698 */ 2699 offs = buf->dtb_offset; 2700 while (offs & (align - 1)) 2701 offs += sizeof (uint32_t); 2702 2703 /* 2704 * If we don't have enough room to both allocate a new key _and_ 2705 * its associated data, increment the drop count and return. 2706 */ 2707 if ((uintptr_t)tomax + offs + fsize > 2708 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2709 dtrace_buffer_drop(buf); 2710 return; 2711 } 2712 2713 /*CONSTCOND*/ 2714 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2715 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2716 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2717 2718 key->dtak_data = kdata = tomax + offs; 2719 buf->dtb_offset = offs + fsize; 2720 2721 /* 2722 * Now copy the data across. 2723 */ 2724 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2725 2726 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2727 kdata[i] = data[i]; 2728 2729 /* 2730 * Because strings are not zeroed out by default, we need to iterate 2731 * looking for actions that store strings, and we need to explicitly 2732 * pad these strings out with zeroes. 2733 */ 2734 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2735 int nul; 2736 2737 if (!DTRACEACT_ISSTRING(act)) 2738 continue; 2739 2740 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2741 limit = i + act->dta_rec.dtrd_size; 2742 ASSERT(limit <= size); 2743 2744 for (nul = 0; i < limit; i++) { 2745 if (nul) { 2746 kdata[i] = '\0'; 2747 continue; 2748 } 2749 2750 if (data[i] != '\0') 2751 continue; 2752 2753 nul = 1; 2754 } 2755 } 2756 2757 for (i = size; i < fsize; i++) 2758 kdata[i] = 0; 2759 2760 key->dtak_hashval = hashval; 2761 key->dtak_size = size; 2762 key->dtak_action = action; 2763 key->dtak_next = agb->dtagb_hash[ndx]; 2764 agb->dtagb_hash[ndx] = key; 2765 2766 /* 2767 * Finally, apply the aggregator. 2768 */ 2769 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2770 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2771 } 2772 2773 /* 2774 * Given consumer state, this routine finds a speculation in the INACTIVE 2775 * state and transitions it into the ACTIVE state. If there is no speculation 2776 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2777 * incremented -- it is up to the caller to take appropriate action. 2778 */ 2779 static int 2780 dtrace_speculation(dtrace_state_t *state) 2781 { 2782 int i = 0; 2783 dtrace_speculation_state_t current; 2784 uint32_t *stat = &state->dts_speculations_unavail, count; 2785 2786 while (i < state->dts_nspeculations) { 2787 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2788 2789 current = spec->dtsp_state; 2790 2791 if (current != DTRACESPEC_INACTIVE) { 2792 if (current == DTRACESPEC_COMMITTINGMANY || 2793 current == DTRACESPEC_COMMITTING || 2794 current == DTRACESPEC_DISCARDING) 2795 stat = &state->dts_speculations_busy; 2796 i++; 2797 continue; 2798 } 2799 2800 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2801 current, DTRACESPEC_ACTIVE) == current) 2802 return (i + 1); 2803 } 2804 2805 /* 2806 * We couldn't find a speculation. If we found as much as a single 2807 * busy speculation buffer, we'll attribute this failure as "busy" 2808 * instead of "unavail". 2809 */ 2810 do { 2811 count = *stat; 2812 } while (dtrace_cas32(stat, count, count + 1) != count); 2813 2814 return (0); 2815 } 2816 2817 /* 2818 * This routine commits an active speculation. If the specified speculation 2819 * is not in a valid state to perform a commit(), this routine will silently do 2820 * nothing. The state of the specified speculation is transitioned according 2821 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2822 */ 2823 static void 2824 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2825 dtrace_specid_t which) 2826 { 2827 dtrace_speculation_t *spec; 2828 dtrace_buffer_t *src, *dest; 2829 uintptr_t daddr, saddr, dlimit, slimit; 2830 dtrace_speculation_state_t current, new = 0; 2831 intptr_t offs; 2832 uint64_t timestamp; 2833 2834 if (which == 0) 2835 return; 2836 2837 if (which > state->dts_nspeculations) { 2838 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2839 return; 2840 } 2841 2842 spec = &state->dts_speculations[which - 1]; 2843 src = &spec->dtsp_buffer[cpu]; 2844 dest = &state->dts_buffer[cpu]; 2845 2846 do { 2847 current = spec->dtsp_state; 2848 2849 if (current == DTRACESPEC_COMMITTINGMANY) 2850 break; 2851 2852 switch (current) { 2853 case DTRACESPEC_INACTIVE: 2854 case DTRACESPEC_DISCARDING: 2855 return; 2856 2857 case DTRACESPEC_COMMITTING: 2858 /* 2859 * This is only possible if we are (a) commit()'ing 2860 * without having done a prior speculate() on this CPU 2861 * and (b) racing with another commit() on a different 2862 * CPU. There's nothing to do -- we just assert that 2863 * our offset is 0. 2864 */ 2865 ASSERT(src->dtb_offset == 0); 2866 return; 2867 2868 case DTRACESPEC_ACTIVE: 2869 new = DTRACESPEC_COMMITTING; 2870 break; 2871 2872 case DTRACESPEC_ACTIVEONE: 2873 /* 2874 * This speculation is active on one CPU. If our 2875 * buffer offset is non-zero, we know that the one CPU 2876 * must be us. Otherwise, we are committing on a 2877 * different CPU from the speculate(), and we must 2878 * rely on being asynchronously cleaned. 2879 */ 2880 if (src->dtb_offset != 0) { 2881 new = DTRACESPEC_COMMITTING; 2882 break; 2883 } 2884 /*FALLTHROUGH*/ 2885 2886 case DTRACESPEC_ACTIVEMANY: 2887 new = DTRACESPEC_COMMITTINGMANY; 2888 break; 2889 2890 default: 2891 ASSERT(0); 2892 } 2893 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2894 current, new) != current); 2895 2896 /* 2897 * We have set the state to indicate that we are committing this 2898 * speculation. Now reserve the necessary space in the destination 2899 * buffer. 2900 */ 2901 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2902 sizeof (uint64_t), state, NULL)) < 0) { 2903 dtrace_buffer_drop(dest); 2904 goto out; 2905 } 2906 2907 /* 2908 * We have sufficient space to copy the speculative buffer into the 2909 * primary buffer. First, modify the speculative buffer, filling 2910 * in the timestamp of all entries with the current time. The data 2911 * must have the commit() time rather than the time it was traced, 2912 * so that all entries in the primary buffer are in timestamp order. 2913 */ 2914 timestamp = dtrace_gethrtime(); 2915 saddr = (uintptr_t)src->dtb_tomax; 2916 slimit = saddr + src->dtb_offset; 2917 while (saddr < slimit) { 2918 size_t size; 2919 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2920 2921 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2922 saddr += sizeof (dtrace_epid_t); 2923 continue; 2924 } 2925 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2926 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2927 2928 ASSERT3U(saddr + size, <=, slimit); 2929 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2930 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2931 2932 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2933 2934 saddr += size; 2935 } 2936 2937 /* 2938 * Copy the buffer across. (Note that this is a 2939 * highly subobtimal bcopy(); in the unlikely event that this becomes 2940 * a serious performance issue, a high-performance DTrace-specific 2941 * bcopy() should obviously be invented.) 2942 */ 2943 daddr = (uintptr_t)dest->dtb_tomax + offs; 2944 dlimit = daddr + src->dtb_offset; 2945 saddr = (uintptr_t)src->dtb_tomax; 2946 2947 /* 2948 * First, the aligned portion. 2949 */ 2950 while (dlimit - daddr >= sizeof (uint64_t)) { 2951 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2952 2953 daddr += sizeof (uint64_t); 2954 saddr += sizeof (uint64_t); 2955 } 2956 2957 /* 2958 * Now any left-over bit... 2959 */ 2960 while (dlimit - daddr) 2961 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2962 2963 /* 2964 * Finally, commit the reserved space in the destination buffer. 2965 */ 2966 dest->dtb_offset = offs + src->dtb_offset; 2967 2968 out: 2969 /* 2970 * If we're lucky enough to be the only active CPU on this speculation 2971 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2972 */ 2973 if (current == DTRACESPEC_ACTIVE || 2974 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2975 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2976 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2977 2978 ASSERT(rval == DTRACESPEC_COMMITTING); 2979 } 2980 2981 src->dtb_offset = 0; 2982 src->dtb_xamot_drops += src->dtb_drops; 2983 src->dtb_drops = 0; 2984 } 2985 2986 /* 2987 * This routine discards an active speculation. If the specified speculation 2988 * is not in a valid state to perform a discard(), this routine will silently 2989 * do nothing. The state of the specified speculation is transitioned 2990 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2991 */ 2992 static void 2993 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2994 dtrace_specid_t which) 2995 { 2996 dtrace_speculation_t *spec; 2997 dtrace_speculation_state_t current, new = 0; 2998 dtrace_buffer_t *buf; 2999 3000 if (which == 0) 3001 return; 3002 3003 if (which > state->dts_nspeculations) { 3004 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 3005 return; 3006 } 3007 3008 spec = &state->dts_speculations[which - 1]; 3009 buf = &spec->dtsp_buffer[cpu]; 3010 3011 do { 3012 current = spec->dtsp_state; 3013 3014 switch (current) { 3015 case DTRACESPEC_INACTIVE: 3016 case DTRACESPEC_COMMITTINGMANY: 3017 case DTRACESPEC_COMMITTING: 3018 case DTRACESPEC_DISCARDING: 3019 return; 3020 3021 case DTRACESPEC_ACTIVE: 3022 case DTRACESPEC_ACTIVEMANY: 3023 new = DTRACESPEC_DISCARDING; 3024 break; 3025 3026 case DTRACESPEC_ACTIVEONE: 3027 if (buf->dtb_offset != 0) { 3028 new = DTRACESPEC_INACTIVE; 3029 } else { 3030 new = DTRACESPEC_DISCARDING; 3031 } 3032 break; 3033 3034 default: 3035 ASSERT(0); 3036 } 3037 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3038 current, new) != current); 3039 3040 buf->dtb_offset = 0; 3041 buf->dtb_drops = 0; 3042 } 3043 3044 /* 3045 * Note: not called from probe context. This function is called 3046 * asynchronously from cross call context to clean any speculations that are 3047 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 3048 * transitioned back to the INACTIVE state until all CPUs have cleaned the 3049 * speculation. 3050 */ 3051 static void 3052 dtrace_speculation_clean_here(dtrace_state_t *state) 3053 { 3054 dtrace_icookie_t cookie; 3055 processorid_t cpu = curcpu; 3056 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 3057 dtrace_specid_t i; 3058 3059 cookie = dtrace_interrupt_disable(); 3060 3061 if (dest->dtb_tomax == NULL) { 3062 dtrace_interrupt_enable(cookie); 3063 return; 3064 } 3065 3066 for (i = 0; i < state->dts_nspeculations; i++) { 3067 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3068 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 3069 3070 if (src->dtb_tomax == NULL) 3071 continue; 3072 3073 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 3074 src->dtb_offset = 0; 3075 continue; 3076 } 3077 3078 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 3079 continue; 3080 3081 if (src->dtb_offset == 0) 3082 continue; 3083 3084 dtrace_speculation_commit(state, cpu, i + 1); 3085 } 3086 3087 dtrace_interrupt_enable(cookie); 3088 } 3089 3090 /* 3091 * Note: not called from probe context. This function is called 3092 * asynchronously (and at a regular interval) to clean any speculations that 3093 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 3094 * is work to be done, it cross calls all CPUs to perform that work; 3095 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 3096 * INACTIVE state until they have been cleaned by all CPUs. 3097 */ 3098 static void 3099 dtrace_speculation_clean(dtrace_state_t *state) 3100 { 3101 int work = 0, rv; 3102 dtrace_specid_t i; 3103 3104 for (i = 0; i < state->dts_nspeculations; i++) { 3105 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3106 3107 ASSERT(!spec->dtsp_cleaning); 3108 3109 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 3110 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 3111 continue; 3112 3113 work++; 3114 spec->dtsp_cleaning = 1; 3115 } 3116 3117 if (!work) 3118 return; 3119 3120 dtrace_xcall(DTRACE_CPUALL, 3121 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 3122 3123 /* 3124 * We now know that all CPUs have committed or discarded their 3125 * speculation buffers, as appropriate. We can now set the state 3126 * to inactive. 3127 */ 3128 for (i = 0; i < state->dts_nspeculations; i++) { 3129 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3130 dtrace_speculation_state_t current, new; 3131 3132 if (!spec->dtsp_cleaning) 3133 continue; 3134 3135 current = spec->dtsp_state; 3136 ASSERT(current == DTRACESPEC_DISCARDING || 3137 current == DTRACESPEC_COMMITTINGMANY); 3138 3139 new = DTRACESPEC_INACTIVE; 3140 3141 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 3142 ASSERT(rv == current); 3143 spec->dtsp_cleaning = 0; 3144 } 3145 } 3146 3147 /* 3148 * Called as part of a speculate() to get the speculative buffer associated 3149 * with a given speculation. Returns NULL if the specified speculation is not 3150 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 3151 * the active CPU is not the specified CPU -- the speculation will be 3152 * atomically transitioned into the ACTIVEMANY state. 3153 */ 3154 static dtrace_buffer_t * 3155 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 3156 dtrace_specid_t which) 3157 { 3158 dtrace_speculation_t *spec; 3159 dtrace_speculation_state_t current, new = 0; 3160 dtrace_buffer_t *buf; 3161 3162 if (which == 0) 3163 return (NULL); 3164 3165 if (which > state->dts_nspeculations) { 3166 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 3167 return (NULL); 3168 } 3169 3170 spec = &state->dts_speculations[which - 1]; 3171 buf = &spec->dtsp_buffer[cpuid]; 3172 3173 do { 3174 current = spec->dtsp_state; 3175 3176 switch (current) { 3177 case DTRACESPEC_INACTIVE: 3178 case DTRACESPEC_COMMITTINGMANY: 3179 case DTRACESPEC_DISCARDING: 3180 return (NULL); 3181 3182 case DTRACESPEC_COMMITTING: 3183 ASSERT(buf->dtb_offset == 0); 3184 return (NULL); 3185 3186 case DTRACESPEC_ACTIVEONE: 3187 /* 3188 * This speculation is currently active on one CPU. 3189 * Check the offset in the buffer; if it's non-zero, 3190 * that CPU must be us (and we leave the state alone). 3191 * If it's zero, assume that we're starting on a new 3192 * CPU -- and change the state to indicate that the 3193 * speculation is active on more than one CPU. 3194 */ 3195 if (buf->dtb_offset != 0) 3196 return (buf); 3197 3198 new = DTRACESPEC_ACTIVEMANY; 3199 break; 3200 3201 case DTRACESPEC_ACTIVEMANY: 3202 return (buf); 3203 3204 case DTRACESPEC_ACTIVE: 3205 new = DTRACESPEC_ACTIVEONE; 3206 break; 3207 3208 default: 3209 ASSERT(0); 3210 } 3211 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3212 current, new) != current); 3213 3214 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 3215 return (buf); 3216 } 3217 3218 /* 3219 * Return a string. In the event that the user lacks the privilege to access 3220 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3221 * don't fail access checking. 3222 * 3223 * dtrace_dif_variable() uses this routine as a helper for various 3224 * builtin values such as 'execname' and 'probefunc.' 3225 */ 3226 uintptr_t 3227 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 3228 dtrace_mstate_t *mstate) 3229 { 3230 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3231 uintptr_t ret; 3232 size_t strsz; 3233 3234 /* 3235 * The easy case: this probe is allowed to read all of memory, so 3236 * we can just return this as a vanilla pointer. 3237 */ 3238 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 3239 return (addr); 3240 3241 /* 3242 * This is the tougher case: we copy the string in question from 3243 * kernel memory into scratch memory and return it that way: this 3244 * ensures that we won't trip up when access checking tests the 3245 * BYREF return value. 3246 */ 3247 strsz = dtrace_strlen((char *)addr, size) + 1; 3248 3249 if (mstate->dtms_scratch_ptr + strsz > 3250 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3251 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3252 return (0); 3253 } 3254 3255 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3256 strsz); 3257 ret = mstate->dtms_scratch_ptr; 3258 mstate->dtms_scratch_ptr += strsz; 3259 return (ret); 3260 } 3261 3262 /* 3263 * Return a string from a memoy address which is known to have one or 3264 * more concatenated, individually zero terminated, sub-strings. 3265 * In the event that the user lacks the privilege to access 3266 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3267 * don't fail access checking. 3268 * 3269 * dtrace_dif_variable() uses this routine as a helper for various 3270 * builtin values such as 'execargs'. 3271 */ 3272 static uintptr_t 3273 dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 3274 dtrace_mstate_t *mstate) 3275 { 3276 char *p; 3277 size_t i; 3278 uintptr_t ret; 3279 3280 if (mstate->dtms_scratch_ptr + strsz > 3281 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3282 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3283 return (0); 3284 } 3285 3286 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3287 strsz); 3288 3289 /* Replace sub-string termination characters with a space. */ 3290 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 3291 p++, i++) 3292 if (*p == '\0') 3293 *p = ' '; 3294 3295 ret = mstate->dtms_scratch_ptr; 3296 mstate->dtms_scratch_ptr += strsz; 3297 return (ret); 3298 } 3299 3300 /* 3301 * This function implements the DIF emulator's variable lookups. The emulator 3302 * passes a reserved variable identifier and optional built-in array index. 3303 */ 3304 static uint64_t 3305 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 3306 uint64_t ndx) 3307 { 3308 /* 3309 * If we're accessing one of the uncached arguments, we'll turn this 3310 * into a reference in the args array. 3311 */ 3312 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 3313 ndx = v - DIF_VAR_ARG0; 3314 v = DIF_VAR_ARGS; 3315 } 3316 3317 switch (v) { 3318 case DIF_VAR_ARGS: 3319 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 3320 if (ndx >= sizeof (mstate->dtms_arg) / 3321 sizeof (mstate->dtms_arg[0])) { 3322 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3323 dtrace_provider_t *pv; 3324 uint64_t val; 3325 3326 pv = mstate->dtms_probe->dtpr_provider; 3327 if (pv->dtpv_pops.dtps_getargval != NULL) 3328 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 3329 mstate->dtms_probe->dtpr_id, 3330 mstate->dtms_probe->dtpr_arg, ndx, aframes); 3331 else 3332 val = dtrace_getarg(ndx, aframes); 3333 3334 /* 3335 * This is regrettably required to keep the compiler 3336 * from tail-optimizing the call to dtrace_getarg(). 3337 * The condition always evaluates to true, but the 3338 * compiler has no way of figuring that out a priori. 3339 * (None of this would be necessary if the compiler 3340 * could be relied upon to _always_ tail-optimize 3341 * the call to dtrace_getarg() -- but it can't.) 3342 */ 3343 if (mstate->dtms_probe != NULL) 3344 return (val); 3345 3346 ASSERT(0); 3347 } 3348 3349 return (mstate->dtms_arg[ndx]); 3350 3351 #ifdef illumos 3352 case DIF_VAR_UREGS: { 3353 klwp_t *lwp; 3354 3355 if (!dtrace_priv_proc(state)) 3356 return (0); 3357 3358 if ((lwp = curthread->t_lwp) == NULL) { 3359 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3360 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 3361 return (0); 3362 } 3363 3364 return (dtrace_getreg(lwp->lwp_regs, ndx)); 3365 return (0); 3366 } 3367 #else 3368 case DIF_VAR_UREGS: { 3369 struct trapframe *tframe; 3370 3371 if (!dtrace_priv_proc(state)) 3372 return (0); 3373 3374 if ((tframe = curthread->td_frame) == NULL) { 3375 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3376 cpu_core[curcpu].cpuc_dtrace_illval = 0; 3377 return (0); 3378 } 3379 3380 return (dtrace_getreg(tframe, ndx)); 3381 } 3382 #endif 3383 3384 case DIF_VAR_CURTHREAD: 3385 if (!dtrace_priv_proc(state)) 3386 return (0); 3387 return ((uint64_t)(uintptr_t)curthread); 3388 3389 case DIF_VAR_TIMESTAMP: 3390 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 3391 mstate->dtms_timestamp = dtrace_gethrtime(); 3392 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 3393 } 3394 return (mstate->dtms_timestamp); 3395 3396 case DIF_VAR_VTIMESTAMP: 3397 ASSERT(dtrace_vtime_references != 0); 3398 return (curthread->t_dtrace_vtime); 3399 3400 case DIF_VAR_WALLTIMESTAMP: 3401 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3402 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3403 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3404 } 3405 return (mstate->dtms_walltimestamp); 3406 3407 #ifdef illumos 3408 case DIF_VAR_IPL: 3409 if (!dtrace_priv_kernel(state)) 3410 return (0); 3411 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3412 mstate->dtms_ipl = dtrace_getipl(); 3413 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3414 } 3415 return (mstate->dtms_ipl); 3416 #endif 3417 3418 case DIF_VAR_EPID: 3419 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3420 return (mstate->dtms_epid); 3421 3422 case DIF_VAR_ID: 3423 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3424 return (mstate->dtms_probe->dtpr_id); 3425 3426 case DIF_VAR_STACKDEPTH: 3427 if (!dtrace_priv_kernel(state)) 3428 return (0); 3429 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3430 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3431 3432 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3433 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3434 } 3435 return (mstate->dtms_stackdepth); 3436 3437 case DIF_VAR_USTACKDEPTH: 3438 if (!dtrace_priv_proc(state)) 3439 return (0); 3440 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3441 /* 3442 * See comment in DIF_VAR_PID. 3443 */ 3444 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3445 CPU_ON_INTR(CPU)) { 3446 mstate->dtms_ustackdepth = 0; 3447 } else { 3448 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3449 mstate->dtms_ustackdepth = 3450 dtrace_getustackdepth(); 3451 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3452 } 3453 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3454 } 3455 return (mstate->dtms_ustackdepth); 3456 3457 case DIF_VAR_CALLER: 3458 if (!dtrace_priv_kernel(state)) 3459 return (0); 3460 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3461 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3462 3463 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3464 /* 3465 * If this is an unanchored probe, we are 3466 * required to go through the slow path: 3467 * dtrace_caller() only guarantees correct 3468 * results for anchored probes. 3469 */ 3470 pc_t caller[2] = {0, 0}; 3471 3472 dtrace_getpcstack(caller, 2, aframes, 3473 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3474 mstate->dtms_caller = caller[1]; 3475 } else if ((mstate->dtms_caller = 3476 dtrace_caller(aframes)) == -1) { 3477 /* 3478 * We have failed to do this the quick way; 3479 * we must resort to the slower approach of 3480 * calling dtrace_getpcstack(). 3481 */ 3482 pc_t caller = 0; 3483 3484 dtrace_getpcstack(&caller, 1, aframes, NULL); 3485 mstate->dtms_caller = caller; 3486 } 3487 3488 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3489 } 3490 return (mstate->dtms_caller); 3491 3492 case DIF_VAR_UCALLER: 3493 if (!dtrace_priv_proc(state)) 3494 return (0); 3495 3496 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3497 uint64_t ustack[3]; 3498 3499 /* 3500 * dtrace_getupcstack() fills in the first uint64_t 3501 * with the current PID. The second uint64_t will 3502 * be the program counter at user-level. The third 3503 * uint64_t will contain the caller, which is what 3504 * we're after. 3505 */ 3506 ustack[2] = 0; 3507 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3508 dtrace_getupcstack(ustack, 3); 3509 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3510 mstate->dtms_ucaller = ustack[2]; 3511 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3512 } 3513 3514 return (mstate->dtms_ucaller); 3515 3516 case DIF_VAR_PROBEPROV: 3517 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3518 return (dtrace_dif_varstr( 3519 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3520 state, mstate)); 3521 3522 case DIF_VAR_PROBEMOD: 3523 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3524 return (dtrace_dif_varstr( 3525 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3526 state, mstate)); 3527 3528 case DIF_VAR_PROBEFUNC: 3529 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3530 return (dtrace_dif_varstr( 3531 (uintptr_t)mstate->dtms_probe->dtpr_func, 3532 state, mstate)); 3533 3534 case DIF_VAR_PROBENAME: 3535 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3536 return (dtrace_dif_varstr( 3537 (uintptr_t)mstate->dtms_probe->dtpr_name, 3538 state, mstate)); 3539 3540 case DIF_VAR_PID: 3541 if (!dtrace_priv_proc(state)) 3542 return (0); 3543 3544 #ifdef illumos 3545 /* 3546 * Note that we are assuming that an unanchored probe is 3547 * always due to a high-level interrupt. (And we're assuming 3548 * that there is only a single high level interrupt.) 3549 */ 3550 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3551 return (pid0.pid_id); 3552 3553 /* 3554 * It is always safe to dereference one's own t_procp pointer: 3555 * it always points to a valid, allocated proc structure. 3556 * Further, it is always safe to dereference the p_pidp member 3557 * of one's own proc structure. (These are truisms becuase 3558 * threads and processes don't clean up their own state -- 3559 * they leave that task to whomever reaps them.) 3560 */ 3561 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3562 #else 3563 return ((uint64_t)curproc->p_pid); 3564 #endif 3565 3566 case DIF_VAR_PPID: 3567 if (!dtrace_priv_proc(state)) 3568 return (0); 3569 3570 #ifdef illumos 3571 /* 3572 * See comment in DIF_VAR_PID. 3573 */ 3574 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3575 return (pid0.pid_id); 3576 3577 /* 3578 * It is always safe to dereference one's own t_procp pointer: 3579 * it always points to a valid, allocated proc structure. 3580 * (This is true because threads don't clean up their own 3581 * state -- they leave that task to whomever reaps them.) 3582 */ 3583 return ((uint64_t)curthread->t_procp->p_ppid); 3584 #else 3585 if (curproc->p_pid == proc0.p_pid) 3586 return (curproc->p_pid); 3587 else 3588 return (curproc->p_pptr->p_pid); 3589 #endif 3590 3591 case DIF_VAR_TID: 3592 #ifdef illumos 3593 /* 3594 * See comment in DIF_VAR_PID. 3595 */ 3596 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3597 return (0); 3598 #endif 3599 3600 return ((uint64_t)curthread->t_tid); 3601 3602 case DIF_VAR_EXECARGS: { 3603 struct pargs *p_args = curthread->td_proc->p_args; 3604 3605 if (p_args == NULL) 3606 return(0); 3607 3608 return (dtrace_dif_varstrz( 3609 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3610 } 3611 3612 case DIF_VAR_EXECNAME: 3613 #ifdef illumos 3614 if (!dtrace_priv_proc(state)) 3615 return (0); 3616 3617 /* 3618 * See comment in DIF_VAR_PID. 3619 */ 3620 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3621 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3622 3623 /* 3624 * It is always safe to dereference one's own t_procp pointer: 3625 * it always points to a valid, allocated proc structure. 3626 * (This is true because threads don't clean up their own 3627 * state -- they leave that task to whomever reaps them.) 3628 */ 3629 return (dtrace_dif_varstr( 3630 (uintptr_t)curthread->t_procp->p_user.u_comm, 3631 state, mstate)); 3632 #else 3633 return (dtrace_dif_varstr( 3634 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3635 #endif 3636 3637 case DIF_VAR_ZONENAME: 3638 #ifdef illumos 3639 if (!dtrace_priv_proc(state)) 3640 return (0); 3641 3642 /* 3643 * See comment in DIF_VAR_PID. 3644 */ 3645 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3646 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3647 3648 /* 3649 * It is always safe to dereference one's own t_procp pointer: 3650 * it always points to a valid, allocated proc structure. 3651 * (This is true because threads don't clean up their own 3652 * state -- they leave that task to whomever reaps them.) 3653 */ 3654 return (dtrace_dif_varstr( 3655 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3656 state, mstate)); 3657 #elif defined(__FreeBSD__) 3658 /* 3659 * On FreeBSD, we introduce compatibility to zonename by falling through 3660 * into jailname. 3661 */ 3662 case DIF_VAR_JAILNAME: 3663 if (!dtrace_priv_kernel(state)) 3664 return (0); 3665 3666 return (dtrace_dif_varstr( 3667 (uintptr_t)curthread->td_ucred->cr_prison->pr_name, 3668 state, mstate)); 3669 3670 case DIF_VAR_JID: 3671 if (!dtrace_priv_kernel(state)) 3672 return (0); 3673 3674 return ((uint64_t)curthread->td_ucred->cr_prison->pr_id); 3675 #else 3676 return (0); 3677 #endif 3678 3679 case DIF_VAR_UID: 3680 if (!dtrace_priv_proc(state)) 3681 return (0); 3682 3683 #ifdef illumos 3684 /* 3685 * See comment in DIF_VAR_PID. 3686 */ 3687 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3688 return ((uint64_t)p0.p_cred->cr_uid); 3689 3690 /* 3691 * It is always safe to dereference one's own t_procp pointer: 3692 * it always points to a valid, allocated proc structure. 3693 * (This is true because threads don't clean up their own 3694 * state -- they leave that task to whomever reaps them.) 3695 * 3696 * Additionally, it is safe to dereference one's own process 3697 * credential, since this is never NULL after process birth. 3698 */ 3699 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3700 #else 3701 return ((uint64_t)curthread->td_ucred->cr_uid); 3702 #endif 3703 3704 case DIF_VAR_GID: 3705 if (!dtrace_priv_proc(state)) 3706 return (0); 3707 3708 #ifdef illumos 3709 /* 3710 * See comment in DIF_VAR_PID. 3711 */ 3712 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3713 return ((uint64_t)p0.p_cred->cr_gid); 3714 3715 /* 3716 * It is always safe to dereference one's own t_procp pointer: 3717 * it always points to a valid, allocated proc structure. 3718 * (This is true because threads don't clean up their own 3719 * state -- they leave that task to whomever reaps them.) 3720 * 3721 * Additionally, it is safe to dereference one's own process 3722 * credential, since this is never NULL after process birth. 3723 */ 3724 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3725 #else 3726 return ((uint64_t)curthread->td_ucred->cr_gid); 3727 #endif 3728 3729 case DIF_VAR_ERRNO: { 3730 #ifdef illumos 3731 klwp_t *lwp; 3732 if (!dtrace_priv_proc(state)) 3733 return (0); 3734 3735 /* 3736 * See comment in DIF_VAR_PID. 3737 */ 3738 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3739 return (0); 3740 3741 /* 3742 * It is always safe to dereference one's own t_lwp pointer in 3743 * the event that this pointer is non-NULL. (This is true 3744 * because threads and lwps don't clean up their own state -- 3745 * they leave that task to whomever reaps them.) 3746 */ 3747 if ((lwp = curthread->t_lwp) == NULL) 3748 return (0); 3749 3750 return ((uint64_t)lwp->lwp_errno); 3751 #else 3752 return (curthread->td_errno); 3753 #endif 3754 } 3755 #ifndef illumos 3756 case DIF_VAR_CPU: { 3757 return curcpu; 3758 } 3759 #endif 3760 default: 3761 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3762 return (0); 3763 } 3764 } 3765 3766 3767 typedef enum dtrace_json_state { 3768 DTRACE_JSON_REST = 1, 3769 DTRACE_JSON_OBJECT, 3770 DTRACE_JSON_STRING, 3771 DTRACE_JSON_STRING_ESCAPE, 3772 DTRACE_JSON_STRING_ESCAPE_UNICODE, 3773 DTRACE_JSON_COLON, 3774 DTRACE_JSON_COMMA, 3775 DTRACE_JSON_VALUE, 3776 DTRACE_JSON_IDENTIFIER, 3777 DTRACE_JSON_NUMBER, 3778 DTRACE_JSON_NUMBER_FRAC, 3779 DTRACE_JSON_NUMBER_EXP, 3780 DTRACE_JSON_COLLECT_OBJECT 3781 } dtrace_json_state_t; 3782 3783 /* 3784 * This function possesses just enough knowledge about JSON to extract a single 3785 * value from a JSON string and store it in the scratch buffer. It is able 3786 * to extract nested object values, and members of arrays by index. 3787 * 3788 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to 3789 * be looked up as we descend into the object tree. e.g. 3790 * 3791 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL 3792 * with nelems = 5. 3793 * 3794 * The run time of this function must be bounded above by strsize to limit the 3795 * amount of work done in probe context. As such, it is implemented as a 3796 * simple state machine, reading one character at a time using safe loads 3797 * until we find the requested element, hit a parsing error or run off the 3798 * end of the object or string. 3799 * 3800 * As there is no way for a subroutine to return an error without interrupting 3801 * clause execution, we simply return NULL in the event of a missing key or any 3802 * other error condition. Each NULL return in this function is commented with 3803 * the error condition it represents -- parsing or otherwise. 3804 * 3805 * The set of states for the state machine closely matches the JSON 3806 * specification (http://json.org/). Briefly: 3807 * 3808 * DTRACE_JSON_REST: 3809 * Skip whitespace until we find either a top-level Object, moving 3810 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE. 3811 * 3812 * DTRACE_JSON_OBJECT: 3813 * Locate the next key String in an Object. Sets a flag to denote 3814 * the next String as a key string and moves to DTRACE_JSON_STRING. 3815 * 3816 * DTRACE_JSON_COLON: 3817 * Skip whitespace until we find the colon that separates key Strings 3818 * from their values. Once found, move to DTRACE_JSON_VALUE. 3819 * 3820 * DTRACE_JSON_VALUE: 3821 * Detects the type of the next value (String, Number, Identifier, Object 3822 * or Array) and routes to the states that process that type. Here we also 3823 * deal with the element selector list if we are requested to traverse down 3824 * into the object tree. 3825 * 3826 * DTRACE_JSON_COMMA: 3827 * Skip whitespace until we find the comma that separates key-value pairs 3828 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays 3829 * (similarly DTRACE_JSON_VALUE). All following literal value processing 3830 * states return to this state at the end of their value, unless otherwise 3831 * noted. 3832 * 3833 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP: 3834 * Processes a Number literal from the JSON, including any exponent 3835 * component that may be present. Numbers are returned as strings, which 3836 * may be passed to strtoll() if an integer is required. 3837 * 3838 * DTRACE_JSON_IDENTIFIER: 3839 * Processes a "true", "false" or "null" literal in the JSON. 3840 * 3841 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE, 3842 * DTRACE_JSON_STRING_ESCAPE_UNICODE: 3843 * Processes a String literal from the JSON, whether the String denotes 3844 * a key, a value or part of a larger Object. Handles all escape sequences 3845 * present in the specification, including four-digit unicode characters, 3846 * but merely includes the escape sequence without converting it to the 3847 * actual escaped character. If the String is flagged as a key, we 3848 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA. 3849 * 3850 * DTRACE_JSON_COLLECT_OBJECT: 3851 * This state collects an entire Object (or Array), correctly handling 3852 * embedded strings. If the full element selector list matches this nested 3853 * object, we return the Object in full as a string. If not, we use this 3854 * state to skip to the next value at this level and continue processing. 3855 * 3856 * NOTE: This function uses various macros from strtolctype.h to manipulate 3857 * digit values, etc -- these have all been checked to ensure they make 3858 * no additional function calls. 3859 */ 3860 static char * 3861 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems, 3862 char *dest) 3863 { 3864 dtrace_json_state_t state = DTRACE_JSON_REST; 3865 int64_t array_elem = INT64_MIN; 3866 int64_t array_pos = 0; 3867 uint8_t escape_unicount = 0; 3868 boolean_t string_is_key = B_FALSE; 3869 boolean_t collect_object = B_FALSE; 3870 boolean_t found_key = B_FALSE; 3871 boolean_t in_array = B_FALSE; 3872 uint32_t braces = 0, brackets = 0; 3873 char *elem = elemlist; 3874 char *dd = dest; 3875 uintptr_t cur; 3876 3877 for (cur = json; cur < json + size; cur++) { 3878 char cc = dtrace_load8(cur); 3879 if (cc == '\0') 3880 return (NULL); 3881 3882 switch (state) { 3883 case DTRACE_JSON_REST: 3884 if (isspace(cc)) 3885 break; 3886 3887 if (cc == '{') { 3888 state = DTRACE_JSON_OBJECT; 3889 break; 3890 } 3891 3892 if (cc == '[') { 3893 in_array = B_TRUE; 3894 array_pos = 0; 3895 array_elem = dtrace_strtoll(elem, 10, size); 3896 found_key = array_elem == 0 ? B_TRUE : B_FALSE; 3897 state = DTRACE_JSON_VALUE; 3898 break; 3899 } 3900 3901 /* 3902 * ERROR: expected to find a top-level object or array. 3903 */ 3904 return (NULL); 3905 case DTRACE_JSON_OBJECT: 3906 if (isspace(cc)) 3907 break; 3908 3909 if (cc == '"') { 3910 state = DTRACE_JSON_STRING; 3911 string_is_key = B_TRUE; 3912 break; 3913 } 3914 3915 /* 3916 * ERROR: either the object did not start with a key 3917 * string, or we've run off the end of the object 3918 * without finding the requested key. 3919 */ 3920 return (NULL); 3921 case DTRACE_JSON_STRING: 3922 if (cc == '\\') { 3923 *dd++ = '\\'; 3924 state = DTRACE_JSON_STRING_ESCAPE; 3925 break; 3926 } 3927 3928 if (cc == '"') { 3929 if (collect_object) { 3930 /* 3931 * We don't reset the dest here, as 3932 * the string is part of a larger 3933 * object being collected. 3934 */ 3935 *dd++ = cc; 3936 collect_object = B_FALSE; 3937 state = DTRACE_JSON_COLLECT_OBJECT; 3938 break; 3939 } 3940 *dd = '\0'; 3941 dd = dest; /* reset string buffer */ 3942 if (string_is_key) { 3943 if (dtrace_strncmp(dest, elem, 3944 size) == 0) 3945 found_key = B_TRUE; 3946 } else if (found_key) { 3947 if (nelems > 1) { 3948 /* 3949 * We expected an object, not 3950 * this string. 3951 */ 3952 return (NULL); 3953 } 3954 return (dest); 3955 } 3956 state = string_is_key ? DTRACE_JSON_COLON : 3957 DTRACE_JSON_COMMA; 3958 string_is_key = B_FALSE; 3959 break; 3960 } 3961 3962 *dd++ = cc; 3963 break; 3964 case DTRACE_JSON_STRING_ESCAPE: 3965 *dd++ = cc; 3966 if (cc == 'u') { 3967 escape_unicount = 0; 3968 state = DTRACE_JSON_STRING_ESCAPE_UNICODE; 3969 } else { 3970 state = DTRACE_JSON_STRING; 3971 } 3972 break; 3973 case DTRACE_JSON_STRING_ESCAPE_UNICODE: 3974 if (!isxdigit(cc)) { 3975 /* 3976 * ERROR: invalid unicode escape, expected 3977 * four valid hexidecimal digits. 3978 */ 3979 return (NULL); 3980 } 3981 3982 *dd++ = cc; 3983 if (++escape_unicount == 4) 3984 state = DTRACE_JSON_STRING; 3985 break; 3986 case DTRACE_JSON_COLON: 3987 if (isspace(cc)) 3988 break; 3989 3990 if (cc == ':') { 3991 state = DTRACE_JSON_VALUE; 3992 break; 3993 } 3994 3995 /* 3996 * ERROR: expected a colon. 3997 */ 3998 return (NULL); 3999 case DTRACE_JSON_COMMA: 4000 if (isspace(cc)) 4001 break; 4002 4003 if (cc == ',') { 4004 if (in_array) { 4005 state = DTRACE_JSON_VALUE; 4006 if (++array_pos == array_elem) 4007 found_key = B_TRUE; 4008 } else { 4009 state = DTRACE_JSON_OBJECT; 4010 } 4011 break; 4012 } 4013 4014 /* 4015 * ERROR: either we hit an unexpected character, or 4016 * we reached the end of the object or array without 4017 * finding the requested key. 4018 */ 4019 return (NULL); 4020 case DTRACE_JSON_IDENTIFIER: 4021 if (islower(cc)) { 4022 *dd++ = cc; 4023 break; 4024 } 4025 4026 *dd = '\0'; 4027 dd = dest; /* reset string buffer */ 4028 4029 if (dtrace_strncmp(dest, "true", 5) == 0 || 4030 dtrace_strncmp(dest, "false", 6) == 0 || 4031 dtrace_strncmp(dest, "null", 5) == 0) { 4032 if (found_key) { 4033 if (nelems > 1) { 4034 /* 4035 * ERROR: We expected an object, 4036 * not this identifier. 4037 */ 4038 return (NULL); 4039 } 4040 return (dest); 4041 } else { 4042 cur--; 4043 state = DTRACE_JSON_COMMA; 4044 break; 4045 } 4046 } 4047 4048 /* 4049 * ERROR: we did not recognise the identifier as one 4050 * of those in the JSON specification. 4051 */ 4052 return (NULL); 4053 case DTRACE_JSON_NUMBER: 4054 if (cc == '.') { 4055 *dd++ = cc; 4056 state = DTRACE_JSON_NUMBER_FRAC; 4057 break; 4058 } 4059 4060 if (cc == 'x' || cc == 'X') { 4061 /* 4062 * ERROR: specification explicitly excludes 4063 * hexidecimal or octal numbers. 4064 */ 4065 return (NULL); 4066 } 4067 4068 /* FALLTHRU */ 4069 case DTRACE_JSON_NUMBER_FRAC: 4070 if (cc == 'e' || cc == 'E') { 4071 *dd++ = cc; 4072 state = DTRACE_JSON_NUMBER_EXP; 4073 break; 4074 } 4075 4076 if (cc == '+' || cc == '-') { 4077 /* 4078 * ERROR: expect sign as part of exponent only. 4079 */ 4080 return (NULL); 4081 } 4082 /* FALLTHRU */ 4083 case DTRACE_JSON_NUMBER_EXP: 4084 if (isdigit(cc) || cc == '+' || cc == '-') { 4085 *dd++ = cc; 4086 break; 4087 } 4088 4089 *dd = '\0'; 4090 dd = dest; /* reset string buffer */ 4091 if (found_key) { 4092 if (nelems > 1) { 4093 /* 4094 * ERROR: We expected an object, not 4095 * this number. 4096 */ 4097 return (NULL); 4098 } 4099 return (dest); 4100 } 4101 4102 cur--; 4103 state = DTRACE_JSON_COMMA; 4104 break; 4105 case DTRACE_JSON_VALUE: 4106 if (isspace(cc)) 4107 break; 4108 4109 if (cc == '{' || cc == '[') { 4110 if (nelems > 1 && found_key) { 4111 in_array = cc == '[' ? B_TRUE : B_FALSE; 4112 /* 4113 * If our element selector directs us 4114 * to descend into this nested object, 4115 * then move to the next selector 4116 * element in the list and restart the 4117 * state machine. 4118 */ 4119 while (*elem != '\0') 4120 elem++; 4121 elem++; /* skip the inter-element NUL */ 4122 nelems--; 4123 dd = dest; 4124 if (in_array) { 4125 state = DTRACE_JSON_VALUE; 4126 array_pos = 0; 4127 array_elem = dtrace_strtoll( 4128 elem, 10, size); 4129 found_key = array_elem == 0 ? 4130 B_TRUE : B_FALSE; 4131 } else { 4132 found_key = B_FALSE; 4133 state = DTRACE_JSON_OBJECT; 4134 } 4135 break; 4136 } 4137 4138 /* 4139 * Otherwise, we wish to either skip this 4140 * nested object or return it in full. 4141 */ 4142 if (cc == '[') 4143 brackets = 1; 4144 else 4145 braces = 1; 4146 *dd++ = cc; 4147 state = DTRACE_JSON_COLLECT_OBJECT; 4148 break; 4149 } 4150 4151 if (cc == '"') { 4152 state = DTRACE_JSON_STRING; 4153 break; 4154 } 4155 4156 if (islower(cc)) { 4157 /* 4158 * Here we deal with true, false and null. 4159 */ 4160 *dd++ = cc; 4161 state = DTRACE_JSON_IDENTIFIER; 4162 break; 4163 } 4164 4165 if (cc == '-' || isdigit(cc)) { 4166 *dd++ = cc; 4167 state = DTRACE_JSON_NUMBER; 4168 break; 4169 } 4170 4171 /* 4172 * ERROR: unexpected character at start of value. 4173 */ 4174 return (NULL); 4175 case DTRACE_JSON_COLLECT_OBJECT: 4176 if (cc == '\0') 4177 /* 4178 * ERROR: unexpected end of input. 4179 */ 4180 return (NULL); 4181 4182 *dd++ = cc; 4183 if (cc == '"') { 4184 collect_object = B_TRUE; 4185 state = DTRACE_JSON_STRING; 4186 break; 4187 } 4188 4189 if (cc == ']') { 4190 if (brackets-- == 0) { 4191 /* 4192 * ERROR: unbalanced brackets. 4193 */ 4194 return (NULL); 4195 } 4196 } else if (cc == '}') { 4197 if (braces-- == 0) { 4198 /* 4199 * ERROR: unbalanced braces. 4200 */ 4201 return (NULL); 4202 } 4203 } else if (cc == '{') { 4204 braces++; 4205 } else if (cc == '[') { 4206 brackets++; 4207 } 4208 4209 if (brackets == 0 && braces == 0) { 4210 if (found_key) { 4211 *dd = '\0'; 4212 return (dest); 4213 } 4214 dd = dest; /* reset string buffer */ 4215 state = DTRACE_JSON_COMMA; 4216 } 4217 break; 4218 } 4219 } 4220 return (NULL); 4221 } 4222 4223 /* 4224 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 4225 * Notice that we don't bother validating the proper number of arguments or 4226 * their types in the tuple stack. This isn't needed because all argument 4227 * interpretation is safe because of our load safety -- the worst that can 4228 * happen is that a bogus program can obtain bogus results. 4229 */ 4230 static void 4231 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 4232 dtrace_key_t *tupregs, int nargs, 4233 dtrace_mstate_t *mstate, dtrace_state_t *state) 4234 { 4235 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4236 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4237 dtrace_vstate_t *vstate = &state->dts_vstate; 4238 4239 #ifdef illumos 4240 union { 4241 mutex_impl_t mi; 4242 uint64_t mx; 4243 } m; 4244 4245 union { 4246 krwlock_t ri; 4247 uintptr_t rw; 4248 } r; 4249 #else 4250 struct thread *lowner; 4251 union { 4252 struct lock_object *li; 4253 uintptr_t lx; 4254 } l; 4255 #endif 4256 4257 switch (subr) { 4258 case DIF_SUBR_RAND: 4259 regs[rd] = dtrace_xoroshiro128_plus_next( 4260 state->dts_rstate[curcpu]); 4261 break; 4262 4263 #ifdef illumos 4264 case DIF_SUBR_MUTEX_OWNED: 4265 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4266 mstate, vstate)) { 4267 regs[rd] = 0; 4268 break; 4269 } 4270 4271 m.mx = dtrace_load64(tupregs[0].dttk_value); 4272 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 4273 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 4274 else 4275 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 4276 break; 4277 4278 case DIF_SUBR_MUTEX_OWNER: 4279 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4280 mstate, vstate)) { 4281 regs[rd] = 0; 4282 break; 4283 } 4284 4285 m.mx = dtrace_load64(tupregs[0].dttk_value); 4286 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 4287 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 4288 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 4289 else 4290 regs[rd] = 0; 4291 break; 4292 4293 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4294 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4295 mstate, vstate)) { 4296 regs[rd] = 0; 4297 break; 4298 } 4299 4300 m.mx = dtrace_load64(tupregs[0].dttk_value); 4301 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 4302 break; 4303 4304 case DIF_SUBR_MUTEX_TYPE_SPIN: 4305 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4306 mstate, vstate)) { 4307 regs[rd] = 0; 4308 break; 4309 } 4310 4311 m.mx = dtrace_load64(tupregs[0].dttk_value); 4312 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 4313 break; 4314 4315 case DIF_SUBR_RW_READ_HELD: { 4316 uintptr_t tmp; 4317 4318 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4319 mstate, vstate)) { 4320 regs[rd] = 0; 4321 break; 4322 } 4323 4324 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4325 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 4326 break; 4327 } 4328 4329 case DIF_SUBR_RW_WRITE_HELD: 4330 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4331 mstate, vstate)) { 4332 regs[rd] = 0; 4333 break; 4334 } 4335 4336 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4337 regs[rd] = _RW_WRITE_HELD(&r.ri); 4338 break; 4339 4340 case DIF_SUBR_RW_ISWRITER: 4341 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4342 mstate, vstate)) { 4343 regs[rd] = 0; 4344 break; 4345 } 4346 4347 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4348 regs[rd] = _RW_ISWRITER(&r.ri); 4349 break; 4350 4351 #else /* !illumos */ 4352 case DIF_SUBR_MUTEX_OWNED: 4353 if (!dtrace_canload(tupregs[0].dttk_value, 4354 sizeof (struct lock_object), mstate, vstate)) { 4355 regs[rd] = 0; 4356 break; 4357 } 4358 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4359 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4360 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4361 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4362 break; 4363 4364 case DIF_SUBR_MUTEX_OWNER: 4365 if (!dtrace_canload(tupregs[0].dttk_value, 4366 sizeof (struct lock_object), mstate, vstate)) { 4367 regs[rd] = 0; 4368 break; 4369 } 4370 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4371 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4372 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4373 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4374 regs[rd] = (uintptr_t)lowner; 4375 break; 4376 4377 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4378 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4379 mstate, vstate)) { 4380 regs[rd] = 0; 4381 break; 4382 } 4383 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4384 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4385 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SLEEPLOCK) != 0; 4386 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4387 break; 4388 4389 case DIF_SUBR_MUTEX_TYPE_SPIN: 4390 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4391 mstate, vstate)) { 4392 regs[rd] = 0; 4393 break; 4394 } 4395 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4396 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4397 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 4398 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4399 break; 4400 4401 case DIF_SUBR_RW_READ_HELD: 4402 case DIF_SUBR_SX_SHARED_HELD: 4403 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4404 mstate, vstate)) { 4405 regs[rd] = 0; 4406 break; 4407 } 4408 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4409 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4410 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4411 lowner == NULL; 4412 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4413 break; 4414 4415 case DIF_SUBR_RW_WRITE_HELD: 4416 case DIF_SUBR_SX_EXCLUSIVE_HELD: 4417 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4418 mstate, vstate)) { 4419 regs[rd] = 0; 4420 break; 4421 } 4422 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4423 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4424 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4425 lowner != NULL; 4426 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4427 break; 4428 4429 case DIF_SUBR_RW_ISWRITER: 4430 case DIF_SUBR_SX_ISEXCLUSIVE: 4431 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4432 mstate, vstate)) { 4433 regs[rd] = 0; 4434 break; 4435 } 4436 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4437 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4438 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4439 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4440 regs[rd] = (lowner == curthread); 4441 break; 4442 #endif /* illumos */ 4443 4444 case DIF_SUBR_BCOPY: { 4445 /* 4446 * We need to be sure that the destination is in the scratch 4447 * region -- no other region is allowed. 4448 */ 4449 uintptr_t src = tupregs[0].dttk_value; 4450 uintptr_t dest = tupregs[1].dttk_value; 4451 size_t size = tupregs[2].dttk_value; 4452 4453 if (!dtrace_inscratch(dest, size, mstate)) { 4454 *flags |= CPU_DTRACE_BADADDR; 4455 *illval = regs[rd]; 4456 break; 4457 } 4458 4459 if (!dtrace_canload(src, size, mstate, vstate)) { 4460 regs[rd] = 0; 4461 break; 4462 } 4463 4464 dtrace_bcopy((void *)src, (void *)dest, size); 4465 break; 4466 } 4467 4468 case DIF_SUBR_ALLOCA: 4469 case DIF_SUBR_COPYIN: { 4470 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4471 uint64_t size = 4472 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 4473 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 4474 4475 /* 4476 * This action doesn't require any credential checks since 4477 * probes will not activate in user contexts to which the 4478 * enabling user does not have permissions. 4479 */ 4480 4481 /* 4482 * Rounding up the user allocation size could have overflowed 4483 * a large, bogus allocation (like -1ULL) to 0. 4484 */ 4485 if (scratch_size < size || 4486 !DTRACE_INSCRATCH(mstate, scratch_size)) { 4487 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4488 regs[rd] = 0; 4489 break; 4490 } 4491 4492 if (subr == DIF_SUBR_COPYIN) { 4493 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4494 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4495 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4496 } 4497 4498 mstate->dtms_scratch_ptr += scratch_size; 4499 regs[rd] = dest; 4500 break; 4501 } 4502 4503 case DIF_SUBR_COPYINTO: { 4504 uint64_t size = tupregs[1].dttk_value; 4505 uintptr_t dest = tupregs[2].dttk_value; 4506 4507 /* 4508 * This action doesn't require any credential checks since 4509 * probes will not activate in user contexts to which the 4510 * enabling user does not have permissions. 4511 */ 4512 if (!dtrace_inscratch(dest, size, mstate)) { 4513 *flags |= CPU_DTRACE_BADADDR; 4514 *illval = regs[rd]; 4515 break; 4516 } 4517 4518 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4519 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4520 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4521 break; 4522 } 4523 4524 case DIF_SUBR_COPYINSTR: { 4525 uintptr_t dest = mstate->dtms_scratch_ptr; 4526 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4527 4528 if (nargs > 1 && tupregs[1].dttk_value < size) 4529 size = tupregs[1].dttk_value + 1; 4530 4531 /* 4532 * This action doesn't require any credential checks since 4533 * probes will not activate in user contexts to which the 4534 * enabling user does not have permissions. 4535 */ 4536 if (!DTRACE_INSCRATCH(mstate, size)) { 4537 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4538 regs[rd] = 0; 4539 break; 4540 } 4541 4542 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4543 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 4544 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4545 4546 ((char *)dest)[size - 1] = '\0'; 4547 mstate->dtms_scratch_ptr += size; 4548 regs[rd] = dest; 4549 break; 4550 } 4551 4552 #ifdef illumos 4553 case DIF_SUBR_MSGSIZE: 4554 case DIF_SUBR_MSGDSIZE: { 4555 uintptr_t baddr = tupregs[0].dttk_value, daddr; 4556 uintptr_t wptr, rptr; 4557 size_t count = 0; 4558 int cont = 0; 4559 4560 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 4561 4562 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 4563 vstate)) { 4564 regs[rd] = 0; 4565 break; 4566 } 4567 4568 wptr = dtrace_loadptr(baddr + 4569 offsetof(mblk_t, b_wptr)); 4570 4571 rptr = dtrace_loadptr(baddr + 4572 offsetof(mblk_t, b_rptr)); 4573 4574 if (wptr < rptr) { 4575 *flags |= CPU_DTRACE_BADADDR; 4576 *illval = tupregs[0].dttk_value; 4577 break; 4578 } 4579 4580 daddr = dtrace_loadptr(baddr + 4581 offsetof(mblk_t, b_datap)); 4582 4583 baddr = dtrace_loadptr(baddr + 4584 offsetof(mblk_t, b_cont)); 4585 4586 /* 4587 * We want to prevent against denial-of-service here, 4588 * so we're only going to search the list for 4589 * dtrace_msgdsize_max mblks. 4590 */ 4591 if (cont++ > dtrace_msgdsize_max) { 4592 *flags |= CPU_DTRACE_ILLOP; 4593 break; 4594 } 4595 4596 if (subr == DIF_SUBR_MSGDSIZE) { 4597 if (dtrace_load8(daddr + 4598 offsetof(dblk_t, db_type)) != M_DATA) 4599 continue; 4600 } 4601 4602 count += wptr - rptr; 4603 } 4604 4605 if (!(*flags & CPU_DTRACE_FAULT)) 4606 regs[rd] = count; 4607 4608 break; 4609 } 4610 #endif 4611 4612 case DIF_SUBR_PROGENYOF: { 4613 pid_t pid = tupregs[0].dttk_value; 4614 proc_t *p; 4615 int rval = 0; 4616 4617 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4618 4619 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 4620 #ifdef illumos 4621 if (p->p_pidp->pid_id == pid) { 4622 #else 4623 if (p->p_pid == pid) { 4624 #endif 4625 rval = 1; 4626 break; 4627 } 4628 } 4629 4630 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4631 4632 regs[rd] = rval; 4633 break; 4634 } 4635 4636 case DIF_SUBR_SPECULATION: 4637 regs[rd] = dtrace_speculation(state); 4638 break; 4639 4640 case DIF_SUBR_COPYOUT: { 4641 uintptr_t kaddr = tupregs[0].dttk_value; 4642 uintptr_t uaddr = tupregs[1].dttk_value; 4643 uint64_t size = tupregs[2].dttk_value; 4644 4645 if (!dtrace_destructive_disallow && 4646 dtrace_priv_proc_control(state) && 4647 !dtrace_istoxic(kaddr, size) && 4648 dtrace_canload(kaddr, size, mstate, vstate)) { 4649 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4650 dtrace_copyout(kaddr, uaddr, size, flags); 4651 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4652 } 4653 break; 4654 } 4655 4656 case DIF_SUBR_COPYOUTSTR: { 4657 uintptr_t kaddr = tupregs[0].dttk_value; 4658 uintptr_t uaddr = tupregs[1].dttk_value; 4659 uint64_t size = tupregs[2].dttk_value; 4660 size_t lim; 4661 4662 if (!dtrace_destructive_disallow && 4663 dtrace_priv_proc_control(state) && 4664 !dtrace_istoxic(kaddr, size) && 4665 dtrace_strcanload(kaddr, size, &lim, mstate, vstate)) { 4666 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4667 dtrace_copyoutstr(kaddr, uaddr, lim, flags); 4668 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4669 } 4670 break; 4671 } 4672 4673 case DIF_SUBR_STRLEN: { 4674 size_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4675 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 4676 size_t lim; 4677 4678 if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) { 4679 regs[rd] = 0; 4680 break; 4681 } 4682 4683 regs[rd] = dtrace_strlen((char *)addr, lim); 4684 break; 4685 } 4686 4687 case DIF_SUBR_STRCHR: 4688 case DIF_SUBR_STRRCHR: { 4689 /* 4690 * We're going to iterate over the string looking for the 4691 * specified character. We will iterate until we have reached 4692 * the string length or we have found the character. If this 4693 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 4694 * of the specified character instead of the first. 4695 */ 4696 uintptr_t addr = tupregs[0].dttk_value; 4697 uintptr_t addr_limit; 4698 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4699 size_t lim; 4700 char c, target = (char)tupregs[1].dttk_value; 4701 4702 if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) { 4703 regs[rd] = 0; 4704 break; 4705 } 4706 addr_limit = addr + lim; 4707 4708 for (regs[rd] = 0; addr < addr_limit; addr++) { 4709 if ((c = dtrace_load8(addr)) == target) { 4710 regs[rd] = addr; 4711 4712 if (subr == DIF_SUBR_STRCHR) 4713 break; 4714 } 4715 4716 if (c == '\0') 4717 break; 4718 } 4719 break; 4720 } 4721 4722 case DIF_SUBR_STRSTR: 4723 case DIF_SUBR_INDEX: 4724 case DIF_SUBR_RINDEX: { 4725 /* 4726 * We're going to iterate over the string looking for the 4727 * specified string. We will iterate until we have reached 4728 * the string length or we have found the string. (Yes, this 4729 * is done in the most naive way possible -- but considering 4730 * that the string we're searching for is likely to be 4731 * relatively short, the complexity of Rabin-Karp or similar 4732 * hardly seems merited.) 4733 */ 4734 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 4735 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 4736 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4737 size_t len = dtrace_strlen(addr, size); 4738 size_t sublen = dtrace_strlen(substr, size); 4739 char *limit = addr + len, *orig = addr; 4740 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 4741 int inc = 1; 4742 4743 regs[rd] = notfound; 4744 4745 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 4746 regs[rd] = 0; 4747 break; 4748 } 4749 4750 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 4751 vstate)) { 4752 regs[rd] = 0; 4753 break; 4754 } 4755 4756 /* 4757 * strstr() and index()/rindex() have similar semantics if 4758 * both strings are the empty string: strstr() returns a 4759 * pointer to the (empty) string, and index() and rindex() 4760 * both return index 0 (regardless of any position argument). 4761 */ 4762 if (sublen == 0 && len == 0) { 4763 if (subr == DIF_SUBR_STRSTR) 4764 regs[rd] = (uintptr_t)addr; 4765 else 4766 regs[rd] = 0; 4767 break; 4768 } 4769 4770 if (subr != DIF_SUBR_STRSTR) { 4771 if (subr == DIF_SUBR_RINDEX) { 4772 limit = orig - 1; 4773 addr += len; 4774 inc = -1; 4775 } 4776 4777 /* 4778 * Both index() and rindex() take an optional position 4779 * argument that denotes the starting position. 4780 */ 4781 if (nargs == 3) { 4782 int64_t pos = (int64_t)tupregs[2].dttk_value; 4783 4784 /* 4785 * If the position argument to index() is 4786 * negative, Perl implicitly clamps it at 4787 * zero. This semantic is a little surprising 4788 * given the special meaning of negative 4789 * positions to similar Perl functions like 4790 * substr(), but it appears to reflect a 4791 * notion that index() can start from a 4792 * negative index and increment its way up to 4793 * the string. Given this notion, Perl's 4794 * rindex() is at least self-consistent in 4795 * that it implicitly clamps positions greater 4796 * than the string length to be the string 4797 * length. Where Perl completely loses 4798 * coherence, however, is when the specified 4799 * substring is the empty string (""). In 4800 * this case, even if the position is 4801 * negative, rindex() returns 0 -- and even if 4802 * the position is greater than the length, 4803 * index() returns the string length. These 4804 * semantics violate the notion that index() 4805 * should never return a value less than the 4806 * specified position and that rindex() should 4807 * never return a value greater than the 4808 * specified position. (One assumes that 4809 * these semantics are artifacts of Perl's 4810 * implementation and not the results of 4811 * deliberate design -- it beggars belief that 4812 * even Larry Wall could desire such oddness.) 4813 * While in the abstract one would wish for 4814 * consistent position semantics across 4815 * substr(), index() and rindex() -- or at the 4816 * very least self-consistent position 4817 * semantics for index() and rindex() -- we 4818 * instead opt to keep with the extant Perl 4819 * semantics, in all their broken glory. (Do 4820 * we have more desire to maintain Perl's 4821 * semantics than Perl does? Probably.) 4822 */ 4823 if (subr == DIF_SUBR_RINDEX) { 4824 if (pos < 0) { 4825 if (sublen == 0) 4826 regs[rd] = 0; 4827 break; 4828 } 4829 4830 if (pos > len) 4831 pos = len; 4832 } else { 4833 if (pos < 0) 4834 pos = 0; 4835 4836 if (pos >= len) { 4837 if (sublen == 0) 4838 regs[rd] = len; 4839 break; 4840 } 4841 } 4842 4843 addr = orig + pos; 4844 } 4845 } 4846 4847 for (regs[rd] = notfound; addr != limit; addr += inc) { 4848 if (dtrace_strncmp(addr, substr, sublen) == 0) { 4849 if (subr != DIF_SUBR_STRSTR) { 4850 /* 4851 * As D index() and rindex() are 4852 * modeled on Perl (and not on awk), 4853 * we return a zero-based (and not a 4854 * one-based) index. (For you Perl 4855 * weenies: no, we're not going to add 4856 * $[ -- and shouldn't you be at a con 4857 * or something?) 4858 */ 4859 regs[rd] = (uintptr_t)(addr - orig); 4860 break; 4861 } 4862 4863 ASSERT(subr == DIF_SUBR_STRSTR); 4864 regs[rd] = (uintptr_t)addr; 4865 break; 4866 } 4867 } 4868 4869 break; 4870 } 4871 4872 case DIF_SUBR_STRTOK: { 4873 uintptr_t addr = tupregs[0].dttk_value; 4874 uintptr_t tokaddr = tupregs[1].dttk_value; 4875 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4876 uintptr_t limit, toklimit; 4877 size_t clim; 4878 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 4879 char *dest = (char *)mstate->dtms_scratch_ptr; 4880 int i; 4881 4882 /* 4883 * Check both the token buffer and (later) the input buffer, 4884 * since both could be non-scratch addresses. 4885 */ 4886 if (!dtrace_strcanload(tokaddr, size, &clim, mstate, vstate)) { 4887 regs[rd] = 0; 4888 break; 4889 } 4890 toklimit = tokaddr + clim; 4891 4892 if (!DTRACE_INSCRATCH(mstate, size)) { 4893 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4894 regs[rd] = 0; 4895 break; 4896 } 4897 4898 if (addr == 0) { 4899 /* 4900 * If the address specified is NULL, we use our saved 4901 * strtok pointer from the mstate. Note that this 4902 * means that the saved strtok pointer is _only_ 4903 * valid within multiple enablings of the same probe -- 4904 * it behaves like an implicit clause-local variable. 4905 */ 4906 addr = mstate->dtms_strtok; 4907 limit = mstate->dtms_strtok_limit; 4908 } else { 4909 /* 4910 * If the user-specified address is non-NULL we must 4911 * access check it. This is the only time we have 4912 * a chance to do so, since this address may reside 4913 * in the string table of this clause-- future calls 4914 * (when we fetch addr from mstate->dtms_strtok) 4915 * would fail this access check. 4916 */ 4917 if (!dtrace_strcanload(addr, size, &clim, mstate, 4918 vstate)) { 4919 regs[rd] = 0; 4920 break; 4921 } 4922 limit = addr + clim; 4923 } 4924 4925 /* 4926 * First, zero the token map, and then process the token 4927 * string -- setting a bit in the map for every character 4928 * found in the token string. 4929 */ 4930 for (i = 0; i < sizeof (tokmap); i++) 4931 tokmap[i] = 0; 4932 4933 for (; tokaddr < toklimit; tokaddr++) { 4934 if ((c = dtrace_load8(tokaddr)) == '\0') 4935 break; 4936 4937 ASSERT((c >> 3) < sizeof (tokmap)); 4938 tokmap[c >> 3] |= (1 << (c & 0x7)); 4939 } 4940 4941 for (; addr < limit; addr++) { 4942 /* 4943 * We're looking for a character that is _not_ 4944 * contained in the token string. 4945 */ 4946 if ((c = dtrace_load8(addr)) == '\0') 4947 break; 4948 4949 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 4950 break; 4951 } 4952 4953 if (c == '\0') { 4954 /* 4955 * We reached the end of the string without finding 4956 * any character that was not in the token string. 4957 * We return NULL in this case, and we set the saved 4958 * address to NULL as well. 4959 */ 4960 regs[rd] = 0; 4961 mstate->dtms_strtok = 0; 4962 mstate->dtms_strtok_limit = 0; 4963 break; 4964 } 4965 4966 /* 4967 * From here on, we're copying into the destination string. 4968 */ 4969 for (i = 0; addr < limit && i < size - 1; addr++) { 4970 if ((c = dtrace_load8(addr)) == '\0') 4971 break; 4972 4973 if (tokmap[c >> 3] & (1 << (c & 0x7))) 4974 break; 4975 4976 ASSERT(i < size); 4977 dest[i++] = c; 4978 } 4979 4980 ASSERT(i < size); 4981 dest[i] = '\0'; 4982 regs[rd] = (uintptr_t)dest; 4983 mstate->dtms_scratch_ptr += size; 4984 mstate->dtms_strtok = addr; 4985 mstate->dtms_strtok_limit = limit; 4986 break; 4987 } 4988 4989 case DIF_SUBR_SUBSTR: { 4990 uintptr_t s = tupregs[0].dttk_value; 4991 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4992 char *d = (char *)mstate->dtms_scratch_ptr; 4993 int64_t index = (int64_t)tupregs[1].dttk_value; 4994 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4995 size_t len = dtrace_strlen((char *)s, size); 4996 int64_t i; 4997 4998 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4999 regs[rd] = 0; 5000 break; 5001 } 5002 5003 if (!DTRACE_INSCRATCH(mstate, size)) { 5004 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5005 regs[rd] = 0; 5006 break; 5007 } 5008 5009 if (nargs <= 2) 5010 remaining = (int64_t)size; 5011 5012 if (index < 0) { 5013 index += len; 5014 5015 if (index < 0 && index + remaining > 0) { 5016 remaining += index; 5017 index = 0; 5018 } 5019 } 5020 5021 if (index >= len || index < 0) { 5022 remaining = 0; 5023 } else if (remaining < 0) { 5024 remaining += len - index; 5025 } else if (index + remaining > size) { 5026 remaining = size - index; 5027 } 5028 5029 for (i = 0; i < remaining; i++) { 5030 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 5031 break; 5032 } 5033 5034 d[i] = '\0'; 5035 5036 mstate->dtms_scratch_ptr += size; 5037 regs[rd] = (uintptr_t)d; 5038 break; 5039 } 5040 5041 case DIF_SUBR_JSON: { 5042 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5043 uintptr_t json = tupregs[0].dttk_value; 5044 size_t jsonlen = dtrace_strlen((char *)json, size); 5045 uintptr_t elem = tupregs[1].dttk_value; 5046 size_t elemlen = dtrace_strlen((char *)elem, size); 5047 5048 char *dest = (char *)mstate->dtms_scratch_ptr; 5049 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1; 5050 char *ee = elemlist; 5051 int nelems = 1; 5052 uintptr_t cur; 5053 5054 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) || 5055 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) { 5056 regs[rd] = 0; 5057 break; 5058 } 5059 5060 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) { 5061 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5062 regs[rd] = 0; 5063 break; 5064 } 5065 5066 /* 5067 * Read the element selector and split it up into a packed list 5068 * of strings. 5069 */ 5070 for (cur = elem; cur < elem + elemlen; cur++) { 5071 char cc = dtrace_load8(cur); 5072 5073 if (cur == elem && cc == '[') { 5074 /* 5075 * If the first element selector key is 5076 * actually an array index then ignore the 5077 * bracket. 5078 */ 5079 continue; 5080 } 5081 5082 if (cc == ']') 5083 continue; 5084 5085 if (cc == '.' || cc == '[') { 5086 nelems++; 5087 cc = '\0'; 5088 } 5089 5090 *ee++ = cc; 5091 } 5092 *ee++ = '\0'; 5093 5094 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist, 5095 nelems, dest)) != 0) 5096 mstate->dtms_scratch_ptr += jsonlen + 1; 5097 break; 5098 } 5099 5100 case DIF_SUBR_TOUPPER: 5101 case DIF_SUBR_TOLOWER: { 5102 uintptr_t s = tupregs[0].dttk_value; 5103 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5104 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5105 size_t len = dtrace_strlen((char *)s, size); 5106 char lower, upper, convert; 5107 int64_t i; 5108 5109 if (subr == DIF_SUBR_TOUPPER) { 5110 lower = 'a'; 5111 upper = 'z'; 5112 convert = 'A'; 5113 } else { 5114 lower = 'A'; 5115 upper = 'Z'; 5116 convert = 'a'; 5117 } 5118 5119 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 5120 regs[rd] = 0; 5121 break; 5122 } 5123 5124 if (!DTRACE_INSCRATCH(mstate, size)) { 5125 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5126 regs[rd] = 0; 5127 break; 5128 } 5129 5130 for (i = 0; i < size - 1; i++) { 5131 if ((c = dtrace_load8(s + i)) == '\0') 5132 break; 5133 5134 if (c >= lower && c <= upper) 5135 c = convert + (c - lower); 5136 5137 dest[i] = c; 5138 } 5139 5140 ASSERT(i < size); 5141 dest[i] = '\0'; 5142 regs[rd] = (uintptr_t)dest; 5143 mstate->dtms_scratch_ptr += size; 5144 break; 5145 } 5146 5147 #ifdef illumos 5148 case DIF_SUBR_GETMAJOR: 5149 #ifdef _LP64 5150 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 5151 #else 5152 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 5153 #endif 5154 break; 5155 5156 case DIF_SUBR_GETMINOR: 5157 #ifdef _LP64 5158 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 5159 #else 5160 regs[rd] = tupregs[0].dttk_value & MAXMIN; 5161 #endif 5162 break; 5163 5164 case DIF_SUBR_DDI_PATHNAME: { 5165 /* 5166 * This one is a galactic mess. We are going to roughly 5167 * emulate ddi_pathname(), but it's made more complicated 5168 * by the fact that we (a) want to include the minor name and 5169 * (b) must proceed iteratively instead of recursively. 5170 */ 5171 uintptr_t dest = mstate->dtms_scratch_ptr; 5172 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5173 char *start = (char *)dest, *end = start + size - 1; 5174 uintptr_t daddr = tupregs[0].dttk_value; 5175 int64_t minor = (int64_t)tupregs[1].dttk_value; 5176 char *s; 5177 int i, len, depth = 0; 5178 5179 /* 5180 * Due to all the pointer jumping we do and context we must 5181 * rely upon, we just mandate that the user must have kernel 5182 * read privileges to use this routine. 5183 */ 5184 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 5185 *flags |= CPU_DTRACE_KPRIV; 5186 *illval = daddr; 5187 regs[rd] = 0; 5188 } 5189 5190 if (!DTRACE_INSCRATCH(mstate, size)) { 5191 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5192 regs[rd] = 0; 5193 break; 5194 } 5195 5196 *end = '\0'; 5197 5198 /* 5199 * We want to have a name for the minor. In order to do this, 5200 * we need to walk the minor list from the devinfo. We want 5201 * to be sure that we don't infinitely walk a circular list, 5202 * so we check for circularity by sending a scout pointer 5203 * ahead two elements for every element that we iterate over; 5204 * if the list is circular, these will ultimately point to the 5205 * same element. You may recognize this little trick as the 5206 * answer to a stupid interview question -- one that always 5207 * seems to be asked by those who had to have it laboriously 5208 * explained to them, and who can't even concisely describe 5209 * the conditions under which one would be forced to resort to 5210 * this technique. Needless to say, those conditions are 5211 * found here -- and probably only here. Is this the only use 5212 * of this infamous trick in shipping, production code? If it 5213 * isn't, it probably should be... 5214 */ 5215 if (minor != -1) { 5216 uintptr_t maddr = dtrace_loadptr(daddr + 5217 offsetof(struct dev_info, devi_minor)); 5218 5219 uintptr_t next = offsetof(struct ddi_minor_data, next); 5220 uintptr_t name = offsetof(struct ddi_minor_data, 5221 d_minor) + offsetof(struct ddi_minor, name); 5222 uintptr_t dev = offsetof(struct ddi_minor_data, 5223 d_minor) + offsetof(struct ddi_minor, dev); 5224 uintptr_t scout; 5225 5226 if (maddr != NULL) 5227 scout = dtrace_loadptr(maddr + next); 5228 5229 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5230 uint64_t m; 5231 #ifdef _LP64 5232 m = dtrace_load64(maddr + dev) & MAXMIN64; 5233 #else 5234 m = dtrace_load32(maddr + dev) & MAXMIN; 5235 #endif 5236 if (m != minor) { 5237 maddr = dtrace_loadptr(maddr + next); 5238 5239 if (scout == NULL) 5240 continue; 5241 5242 scout = dtrace_loadptr(scout + next); 5243 5244 if (scout == NULL) 5245 continue; 5246 5247 scout = dtrace_loadptr(scout + next); 5248 5249 if (scout == NULL) 5250 continue; 5251 5252 if (scout == maddr) { 5253 *flags |= CPU_DTRACE_ILLOP; 5254 break; 5255 } 5256 5257 continue; 5258 } 5259 5260 /* 5261 * We have the minor data. Now we need to 5262 * copy the minor's name into the end of the 5263 * pathname. 5264 */ 5265 s = (char *)dtrace_loadptr(maddr + name); 5266 len = dtrace_strlen(s, size); 5267 5268 if (*flags & CPU_DTRACE_FAULT) 5269 break; 5270 5271 if (len != 0) { 5272 if ((end -= (len + 1)) < start) 5273 break; 5274 5275 *end = ':'; 5276 } 5277 5278 for (i = 1; i <= len; i++) 5279 end[i] = dtrace_load8((uintptr_t)s++); 5280 break; 5281 } 5282 } 5283 5284 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5285 ddi_node_state_t devi_state; 5286 5287 devi_state = dtrace_load32(daddr + 5288 offsetof(struct dev_info, devi_node_state)); 5289 5290 if (*flags & CPU_DTRACE_FAULT) 5291 break; 5292 5293 if (devi_state >= DS_INITIALIZED) { 5294 s = (char *)dtrace_loadptr(daddr + 5295 offsetof(struct dev_info, devi_addr)); 5296 len = dtrace_strlen(s, size); 5297 5298 if (*flags & CPU_DTRACE_FAULT) 5299 break; 5300 5301 if (len != 0) { 5302 if ((end -= (len + 1)) < start) 5303 break; 5304 5305 *end = '@'; 5306 } 5307 5308 for (i = 1; i <= len; i++) 5309 end[i] = dtrace_load8((uintptr_t)s++); 5310 } 5311 5312 /* 5313 * Now for the node name... 5314 */ 5315 s = (char *)dtrace_loadptr(daddr + 5316 offsetof(struct dev_info, devi_node_name)); 5317 5318 daddr = dtrace_loadptr(daddr + 5319 offsetof(struct dev_info, devi_parent)); 5320 5321 /* 5322 * If our parent is NULL (that is, if we're the root 5323 * node), we're going to use the special path 5324 * "devices". 5325 */ 5326 if (daddr == 0) 5327 s = "devices"; 5328 5329 len = dtrace_strlen(s, size); 5330 if (*flags & CPU_DTRACE_FAULT) 5331 break; 5332 5333 if ((end -= (len + 1)) < start) 5334 break; 5335 5336 for (i = 1; i <= len; i++) 5337 end[i] = dtrace_load8((uintptr_t)s++); 5338 *end = '/'; 5339 5340 if (depth++ > dtrace_devdepth_max) { 5341 *flags |= CPU_DTRACE_ILLOP; 5342 break; 5343 } 5344 } 5345 5346 if (end < start) 5347 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5348 5349 if (daddr == 0) { 5350 regs[rd] = (uintptr_t)end; 5351 mstate->dtms_scratch_ptr += size; 5352 } 5353 5354 break; 5355 } 5356 #endif 5357 5358 case DIF_SUBR_STRJOIN: { 5359 char *d = (char *)mstate->dtms_scratch_ptr; 5360 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5361 uintptr_t s1 = tupregs[0].dttk_value; 5362 uintptr_t s2 = tupregs[1].dttk_value; 5363 int i = 0, j = 0; 5364 size_t lim1, lim2; 5365 char c; 5366 5367 if (!dtrace_strcanload(s1, size, &lim1, mstate, vstate) || 5368 !dtrace_strcanload(s2, size, &lim2, mstate, vstate)) { 5369 regs[rd] = 0; 5370 break; 5371 } 5372 5373 if (!DTRACE_INSCRATCH(mstate, size)) { 5374 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5375 regs[rd] = 0; 5376 break; 5377 } 5378 5379 for (;;) { 5380 if (i >= size) { 5381 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5382 regs[rd] = 0; 5383 break; 5384 } 5385 c = (i >= lim1) ? '\0' : dtrace_load8(s1++); 5386 if ((d[i++] = c) == '\0') { 5387 i--; 5388 break; 5389 } 5390 } 5391 5392 for (;;) { 5393 if (i >= size) { 5394 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5395 regs[rd] = 0; 5396 break; 5397 } 5398 5399 c = (j++ >= lim2) ? '\0' : dtrace_load8(s2++); 5400 if ((d[i++] = c) == '\0') 5401 break; 5402 } 5403 5404 if (i < size) { 5405 mstate->dtms_scratch_ptr += i; 5406 regs[rd] = (uintptr_t)d; 5407 } 5408 5409 break; 5410 } 5411 5412 case DIF_SUBR_STRTOLL: { 5413 uintptr_t s = tupregs[0].dttk_value; 5414 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5415 size_t lim; 5416 int base = 10; 5417 5418 if (nargs > 1) { 5419 if ((base = tupregs[1].dttk_value) <= 1 || 5420 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5421 *flags |= CPU_DTRACE_ILLOP; 5422 break; 5423 } 5424 } 5425 5426 if (!dtrace_strcanload(s, size, &lim, mstate, vstate)) { 5427 regs[rd] = INT64_MIN; 5428 break; 5429 } 5430 5431 regs[rd] = dtrace_strtoll((char *)s, base, lim); 5432 break; 5433 } 5434 5435 case DIF_SUBR_LLTOSTR: { 5436 int64_t i = (int64_t)tupregs[0].dttk_value; 5437 uint64_t val, digit; 5438 uint64_t size = 65; /* enough room for 2^64 in binary */ 5439 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 5440 int base = 10; 5441 5442 if (nargs > 1) { 5443 if ((base = tupregs[1].dttk_value) <= 1 || 5444 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5445 *flags |= CPU_DTRACE_ILLOP; 5446 break; 5447 } 5448 } 5449 5450 val = (base == 10 && i < 0) ? i * -1 : i; 5451 5452 if (!DTRACE_INSCRATCH(mstate, size)) { 5453 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5454 regs[rd] = 0; 5455 break; 5456 } 5457 5458 for (*end-- = '\0'; val; val /= base) { 5459 if ((digit = val % base) <= '9' - '0') { 5460 *end-- = '0' + digit; 5461 } else { 5462 *end-- = 'a' + (digit - ('9' - '0') - 1); 5463 } 5464 } 5465 5466 if (i == 0 && base == 16) 5467 *end-- = '0'; 5468 5469 if (base == 16) 5470 *end-- = 'x'; 5471 5472 if (i == 0 || base == 8 || base == 16) 5473 *end-- = '0'; 5474 5475 if (i < 0 && base == 10) 5476 *end-- = '-'; 5477 5478 regs[rd] = (uintptr_t)end + 1; 5479 mstate->dtms_scratch_ptr += size; 5480 break; 5481 } 5482 5483 case DIF_SUBR_HTONS: 5484 case DIF_SUBR_NTOHS: 5485 #if BYTE_ORDER == BIG_ENDIAN 5486 regs[rd] = (uint16_t)tupregs[0].dttk_value; 5487 #else 5488 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 5489 #endif 5490 break; 5491 5492 5493 case DIF_SUBR_HTONL: 5494 case DIF_SUBR_NTOHL: 5495 #if BYTE_ORDER == BIG_ENDIAN 5496 regs[rd] = (uint32_t)tupregs[0].dttk_value; 5497 #else 5498 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 5499 #endif 5500 break; 5501 5502 5503 case DIF_SUBR_HTONLL: 5504 case DIF_SUBR_NTOHLL: 5505 #if BYTE_ORDER == BIG_ENDIAN 5506 regs[rd] = (uint64_t)tupregs[0].dttk_value; 5507 #else 5508 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 5509 #endif 5510 break; 5511 5512 5513 case DIF_SUBR_DIRNAME: 5514 case DIF_SUBR_BASENAME: { 5515 char *dest = (char *)mstate->dtms_scratch_ptr; 5516 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5517 uintptr_t src = tupregs[0].dttk_value; 5518 int i, j, len = dtrace_strlen((char *)src, size); 5519 int lastbase = -1, firstbase = -1, lastdir = -1; 5520 int start, end; 5521 5522 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 5523 regs[rd] = 0; 5524 break; 5525 } 5526 5527 if (!DTRACE_INSCRATCH(mstate, size)) { 5528 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5529 regs[rd] = 0; 5530 break; 5531 } 5532 5533 /* 5534 * The basename and dirname for a zero-length string is 5535 * defined to be "." 5536 */ 5537 if (len == 0) { 5538 len = 1; 5539 src = (uintptr_t)"."; 5540 } 5541 5542 /* 5543 * Start from the back of the string, moving back toward the 5544 * front until we see a character that isn't a slash. That 5545 * character is the last character in the basename. 5546 */ 5547 for (i = len - 1; i >= 0; i--) { 5548 if (dtrace_load8(src + i) != '/') 5549 break; 5550 } 5551 5552 if (i >= 0) 5553 lastbase = i; 5554 5555 /* 5556 * Starting from the last character in the basename, move 5557 * towards the front until we find a slash. The character 5558 * that we processed immediately before that is the first 5559 * character in the basename. 5560 */ 5561 for (; i >= 0; i--) { 5562 if (dtrace_load8(src + i) == '/') 5563 break; 5564 } 5565 5566 if (i >= 0) 5567 firstbase = i + 1; 5568 5569 /* 5570 * Now keep going until we find a non-slash character. That 5571 * character is the last character in the dirname. 5572 */ 5573 for (; i >= 0; i--) { 5574 if (dtrace_load8(src + i) != '/') 5575 break; 5576 } 5577 5578 if (i >= 0) 5579 lastdir = i; 5580 5581 ASSERT(!(lastbase == -1 && firstbase != -1)); 5582 ASSERT(!(firstbase == -1 && lastdir != -1)); 5583 5584 if (lastbase == -1) { 5585 /* 5586 * We didn't find a non-slash character. We know that 5587 * the length is non-zero, so the whole string must be 5588 * slashes. In either the dirname or the basename 5589 * case, we return '/'. 5590 */ 5591 ASSERT(firstbase == -1); 5592 firstbase = lastbase = lastdir = 0; 5593 } 5594 5595 if (firstbase == -1) { 5596 /* 5597 * The entire string consists only of a basename 5598 * component. If we're looking for dirname, we need 5599 * to change our string to be just "."; if we're 5600 * looking for a basename, we'll just set the first 5601 * character of the basename to be 0. 5602 */ 5603 if (subr == DIF_SUBR_DIRNAME) { 5604 ASSERT(lastdir == -1); 5605 src = (uintptr_t)"."; 5606 lastdir = 0; 5607 } else { 5608 firstbase = 0; 5609 } 5610 } 5611 5612 if (subr == DIF_SUBR_DIRNAME) { 5613 if (lastdir == -1) { 5614 /* 5615 * We know that we have a slash in the name -- 5616 * or lastdir would be set to 0, above. And 5617 * because lastdir is -1, we know that this 5618 * slash must be the first character. (That 5619 * is, the full string must be of the form 5620 * "/basename".) In this case, the last 5621 * character of the directory name is 0. 5622 */ 5623 lastdir = 0; 5624 } 5625 5626 start = 0; 5627 end = lastdir; 5628 } else { 5629 ASSERT(subr == DIF_SUBR_BASENAME); 5630 ASSERT(firstbase != -1 && lastbase != -1); 5631 start = firstbase; 5632 end = lastbase; 5633 } 5634 5635 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 5636 dest[j] = dtrace_load8(src + i); 5637 5638 dest[j] = '\0'; 5639 regs[rd] = (uintptr_t)dest; 5640 mstate->dtms_scratch_ptr += size; 5641 break; 5642 } 5643 5644 case DIF_SUBR_GETF: { 5645 uintptr_t fd = tupregs[0].dttk_value; 5646 struct filedesc *fdp; 5647 file_t *fp; 5648 5649 if (!dtrace_priv_proc(state)) { 5650 regs[rd] = 0; 5651 break; 5652 } 5653 fdp = curproc->p_fd; 5654 FILEDESC_SLOCK(fdp); 5655 fp = fget_locked(fdp, fd); 5656 mstate->dtms_getf = fp; 5657 regs[rd] = (uintptr_t)fp; 5658 FILEDESC_SUNLOCK(fdp); 5659 break; 5660 } 5661 5662 case DIF_SUBR_CLEANPATH: { 5663 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5664 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5665 uintptr_t src = tupregs[0].dttk_value; 5666 size_t lim; 5667 int i = 0, j = 0; 5668 #ifdef illumos 5669 zone_t *z; 5670 #endif 5671 5672 if (!dtrace_strcanload(src, size, &lim, mstate, vstate)) { 5673 regs[rd] = 0; 5674 break; 5675 } 5676 5677 if (!DTRACE_INSCRATCH(mstate, size)) { 5678 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5679 regs[rd] = 0; 5680 break; 5681 } 5682 5683 /* 5684 * Move forward, loading each character. 5685 */ 5686 do { 5687 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5688 next: 5689 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 5690 break; 5691 5692 if (c != '/') { 5693 dest[j++] = c; 5694 continue; 5695 } 5696 5697 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5698 5699 if (c == '/') { 5700 /* 5701 * We have two slashes -- we can just advance 5702 * to the next character. 5703 */ 5704 goto next; 5705 } 5706 5707 if (c != '.') { 5708 /* 5709 * This is not "." and it's not ".." -- we can 5710 * just store the "/" and this character and 5711 * drive on. 5712 */ 5713 dest[j++] = '/'; 5714 dest[j++] = c; 5715 continue; 5716 } 5717 5718 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5719 5720 if (c == '/') { 5721 /* 5722 * This is a "/./" component. We're not going 5723 * to store anything in the destination buffer; 5724 * we're just going to go to the next component. 5725 */ 5726 goto next; 5727 } 5728 5729 if (c != '.') { 5730 /* 5731 * This is not ".." -- we can just store the 5732 * "/." and this character and continue 5733 * processing. 5734 */ 5735 dest[j++] = '/'; 5736 dest[j++] = '.'; 5737 dest[j++] = c; 5738 continue; 5739 } 5740 5741 c = (i >= lim) ? '\0' : dtrace_load8(src + i++); 5742 5743 if (c != '/' && c != '\0') { 5744 /* 5745 * This is not ".." -- it's "..[mumble]". 5746 * We'll store the "/.." and this character 5747 * and continue processing. 5748 */ 5749 dest[j++] = '/'; 5750 dest[j++] = '.'; 5751 dest[j++] = '.'; 5752 dest[j++] = c; 5753 continue; 5754 } 5755 5756 /* 5757 * This is "/../" or "/..\0". We need to back up 5758 * our destination pointer until we find a "/". 5759 */ 5760 i--; 5761 while (j != 0 && dest[--j] != '/') 5762 continue; 5763 5764 if (c == '\0') 5765 dest[++j] = '/'; 5766 } while (c != '\0'); 5767 5768 dest[j] = '\0'; 5769 5770 #ifdef illumos 5771 if (mstate->dtms_getf != NULL && 5772 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 5773 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 5774 /* 5775 * If we've done a getf() as a part of this ECB and we 5776 * don't have kernel access (and we're not in the global 5777 * zone), check if the path we cleaned up begins with 5778 * the zone's root path, and trim it off if so. Note 5779 * that this is an output cleanliness issue, not a 5780 * security issue: knowing one's zone root path does 5781 * not enable privilege escalation. 5782 */ 5783 if (strstr(dest, z->zone_rootpath) == dest) 5784 dest += strlen(z->zone_rootpath) - 1; 5785 } 5786 #endif 5787 5788 regs[rd] = (uintptr_t)dest; 5789 mstate->dtms_scratch_ptr += size; 5790 break; 5791 } 5792 5793 case DIF_SUBR_INET_NTOA: 5794 case DIF_SUBR_INET_NTOA6: 5795 case DIF_SUBR_INET_NTOP: { 5796 size_t size; 5797 int af, argi, i; 5798 char *base, *end; 5799 5800 if (subr == DIF_SUBR_INET_NTOP) { 5801 af = (int)tupregs[0].dttk_value; 5802 argi = 1; 5803 } else { 5804 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 5805 argi = 0; 5806 } 5807 5808 if (af == AF_INET) { 5809 ipaddr_t ip4; 5810 uint8_t *ptr8, val; 5811 5812 if (!dtrace_canload(tupregs[argi].dttk_value, 5813 sizeof (ipaddr_t), mstate, vstate)) { 5814 regs[rd] = 0; 5815 break; 5816 } 5817 5818 /* 5819 * Safely load the IPv4 address. 5820 */ 5821 ip4 = dtrace_load32(tupregs[argi].dttk_value); 5822 5823 /* 5824 * Check an IPv4 string will fit in scratch. 5825 */ 5826 size = INET_ADDRSTRLEN; 5827 if (!DTRACE_INSCRATCH(mstate, size)) { 5828 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5829 regs[rd] = 0; 5830 break; 5831 } 5832 base = (char *)mstate->dtms_scratch_ptr; 5833 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5834 5835 /* 5836 * Stringify as a dotted decimal quad. 5837 */ 5838 *end-- = '\0'; 5839 ptr8 = (uint8_t *)&ip4; 5840 for (i = 3; i >= 0; i--) { 5841 val = ptr8[i]; 5842 5843 if (val == 0) { 5844 *end-- = '0'; 5845 } else { 5846 for (; val; val /= 10) { 5847 *end-- = '0' + (val % 10); 5848 } 5849 } 5850 5851 if (i > 0) 5852 *end-- = '.'; 5853 } 5854 ASSERT(end + 1 >= base); 5855 5856 } else if (af == AF_INET6) { 5857 struct in6_addr ip6; 5858 int firstzero, tryzero, numzero, v6end; 5859 uint16_t val; 5860 const char digits[] = "0123456789abcdef"; 5861 5862 /* 5863 * Stringify using RFC 1884 convention 2 - 16 bit 5864 * hexadecimal values with a zero-run compression. 5865 * Lower case hexadecimal digits are used. 5866 * eg, fe80::214:4fff:fe0b:76c8. 5867 * The IPv4 embedded form is returned for inet_ntop, 5868 * just the IPv4 string is returned for inet_ntoa6. 5869 */ 5870 5871 if (!dtrace_canload(tupregs[argi].dttk_value, 5872 sizeof (struct in6_addr), mstate, vstate)) { 5873 regs[rd] = 0; 5874 break; 5875 } 5876 5877 /* 5878 * Safely load the IPv6 address. 5879 */ 5880 dtrace_bcopy( 5881 (void *)(uintptr_t)tupregs[argi].dttk_value, 5882 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 5883 5884 /* 5885 * Check an IPv6 string will fit in scratch. 5886 */ 5887 size = INET6_ADDRSTRLEN; 5888 if (!DTRACE_INSCRATCH(mstate, size)) { 5889 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5890 regs[rd] = 0; 5891 break; 5892 } 5893 base = (char *)mstate->dtms_scratch_ptr; 5894 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5895 *end-- = '\0'; 5896 5897 /* 5898 * Find the longest run of 16 bit zero values 5899 * for the single allowed zero compression - "::". 5900 */ 5901 firstzero = -1; 5902 tryzero = -1; 5903 numzero = 1; 5904 for (i = 0; i < sizeof (struct in6_addr); i++) { 5905 #ifdef illumos 5906 if (ip6._S6_un._S6_u8[i] == 0 && 5907 #else 5908 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5909 #endif 5910 tryzero == -1 && i % 2 == 0) { 5911 tryzero = i; 5912 continue; 5913 } 5914 5915 if (tryzero != -1 && 5916 #ifdef illumos 5917 (ip6._S6_un._S6_u8[i] != 0 || 5918 #else 5919 (ip6.__u6_addr.__u6_addr8[i] != 0 || 5920 #endif 5921 i == sizeof (struct in6_addr) - 1)) { 5922 5923 if (i - tryzero <= numzero) { 5924 tryzero = -1; 5925 continue; 5926 } 5927 5928 firstzero = tryzero; 5929 numzero = i - i % 2 - tryzero; 5930 tryzero = -1; 5931 5932 #ifdef illumos 5933 if (ip6._S6_un._S6_u8[i] == 0 && 5934 #else 5935 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5936 #endif 5937 i == sizeof (struct in6_addr) - 1) 5938 numzero += 2; 5939 } 5940 } 5941 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 5942 5943 /* 5944 * Check for an IPv4 embedded address. 5945 */ 5946 v6end = sizeof (struct in6_addr) - 2; 5947 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 5948 IN6_IS_ADDR_V4COMPAT(&ip6)) { 5949 for (i = sizeof (struct in6_addr) - 1; 5950 i >= DTRACE_V4MAPPED_OFFSET; i--) { 5951 ASSERT(end >= base); 5952 5953 #ifdef illumos 5954 val = ip6._S6_un._S6_u8[i]; 5955 #else 5956 val = ip6.__u6_addr.__u6_addr8[i]; 5957 #endif 5958 5959 if (val == 0) { 5960 *end-- = '0'; 5961 } else { 5962 for (; val; val /= 10) { 5963 *end-- = '0' + val % 10; 5964 } 5965 } 5966 5967 if (i > DTRACE_V4MAPPED_OFFSET) 5968 *end-- = '.'; 5969 } 5970 5971 if (subr == DIF_SUBR_INET_NTOA6) 5972 goto inetout; 5973 5974 /* 5975 * Set v6end to skip the IPv4 address that 5976 * we have already stringified. 5977 */ 5978 v6end = 10; 5979 } 5980 5981 /* 5982 * Build the IPv6 string by working through the 5983 * address in reverse. 5984 */ 5985 for (i = v6end; i >= 0; i -= 2) { 5986 ASSERT(end >= base); 5987 5988 if (i == firstzero + numzero - 2) { 5989 *end-- = ':'; 5990 *end-- = ':'; 5991 i -= numzero - 2; 5992 continue; 5993 } 5994 5995 if (i < 14 && i != firstzero - 2) 5996 *end-- = ':'; 5997 5998 #ifdef illumos 5999 val = (ip6._S6_un._S6_u8[i] << 8) + 6000 ip6._S6_un._S6_u8[i + 1]; 6001 #else 6002 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 6003 ip6.__u6_addr.__u6_addr8[i + 1]; 6004 #endif 6005 6006 if (val == 0) { 6007 *end-- = '0'; 6008 } else { 6009 for (; val; val /= 16) { 6010 *end-- = digits[val % 16]; 6011 } 6012 } 6013 } 6014 ASSERT(end + 1 >= base); 6015 6016 } else { 6017 /* 6018 * The user didn't use AH_INET or AH_INET6. 6019 */ 6020 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 6021 regs[rd] = 0; 6022 break; 6023 } 6024 6025 inetout: regs[rd] = (uintptr_t)end + 1; 6026 mstate->dtms_scratch_ptr += size; 6027 break; 6028 } 6029 6030 case DIF_SUBR_MEMREF: { 6031 uintptr_t size = 2 * sizeof(uintptr_t); 6032 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 6033 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 6034 6035 /* address and length */ 6036 memref[0] = tupregs[0].dttk_value; 6037 memref[1] = tupregs[1].dttk_value; 6038 6039 regs[rd] = (uintptr_t) memref; 6040 mstate->dtms_scratch_ptr += scratch_size; 6041 break; 6042 } 6043 6044 #ifndef illumos 6045 case DIF_SUBR_MEMSTR: { 6046 char *str = (char *)mstate->dtms_scratch_ptr; 6047 uintptr_t mem = tupregs[0].dttk_value; 6048 char c = tupregs[1].dttk_value; 6049 size_t size = tupregs[2].dttk_value; 6050 uint8_t n; 6051 int i; 6052 6053 regs[rd] = 0; 6054 6055 if (size == 0) 6056 break; 6057 6058 if (!dtrace_canload(mem, size - 1, mstate, vstate)) 6059 break; 6060 6061 if (!DTRACE_INSCRATCH(mstate, size)) { 6062 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6063 break; 6064 } 6065 6066 if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) { 6067 *flags |= CPU_DTRACE_ILLOP; 6068 break; 6069 } 6070 6071 for (i = 0; i < size - 1; i++) { 6072 n = dtrace_load8(mem++); 6073 str[i] = (n == 0) ? c : n; 6074 } 6075 str[size - 1] = 0; 6076 6077 regs[rd] = (uintptr_t)str; 6078 mstate->dtms_scratch_ptr += size; 6079 break; 6080 } 6081 #endif 6082 } 6083 } 6084 6085 /* 6086 * Emulate the execution of DTrace IR instructions specified by the given 6087 * DIF object. This function is deliberately void of assertions as all of 6088 * the necessary checks are handled by a call to dtrace_difo_validate(). 6089 */ 6090 static uint64_t 6091 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 6092 dtrace_vstate_t *vstate, dtrace_state_t *state) 6093 { 6094 const dif_instr_t *text = difo->dtdo_buf; 6095 const uint_t textlen = difo->dtdo_len; 6096 const char *strtab = difo->dtdo_strtab; 6097 const uint64_t *inttab = difo->dtdo_inttab; 6098 6099 uint64_t rval = 0; 6100 dtrace_statvar_t *svar; 6101 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 6102 dtrace_difv_t *v; 6103 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 6104 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 6105 6106 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 6107 uint64_t regs[DIF_DIR_NREGS]; 6108 uint64_t *tmp; 6109 6110 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 6111 int64_t cc_r; 6112 uint_t pc = 0, id, opc = 0; 6113 uint8_t ttop = 0; 6114 dif_instr_t instr; 6115 uint_t r1, r2, rd; 6116 6117 /* 6118 * We stash the current DIF object into the machine state: we need it 6119 * for subsequent access checking. 6120 */ 6121 mstate->dtms_difo = difo; 6122 6123 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 6124 6125 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 6126 opc = pc; 6127 6128 instr = text[pc++]; 6129 r1 = DIF_INSTR_R1(instr); 6130 r2 = DIF_INSTR_R2(instr); 6131 rd = DIF_INSTR_RD(instr); 6132 6133 switch (DIF_INSTR_OP(instr)) { 6134 case DIF_OP_OR: 6135 regs[rd] = regs[r1] | regs[r2]; 6136 break; 6137 case DIF_OP_XOR: 6138 regs[rd] = regs[r1] ^ regs[r2]; 6139 break; 6140 case DIF_OP_AND: 6141 regs[rd] = regs[r1] & regs[r2]; 6142 break; 6143 case DIF_OP_SLL: 6144 regs[rd] = regs[r1] << regs[r2]; 6145 break; 6146 case DIF_OP_SRL: 6147 regs[rd] = regs[r1] >> regs[r2]; 6148 break; 6149 case DIF_OP_SUB: 6150 regs[rd] = regs[r1] - regs[r2]; 6151 break; 6152 case DIF_OP_ADD: 6153 regs[rd] = regs[r1] + regs[r2]; 6154 break; 6155 case DIF_OP_MUL: 6156 regs[rd] = regs[r1] * regs[r2]; 6157 break; 6158 case DIF_OP_SDIV: 6159 if (regs[r2] == 0) { 6160 regs[rd] = 0; 6161 *flags |= CPU_DTRACE_DIVZERO; 6162 } else { 6163 regs[rd] = (int64_t)regs[r1] / 6164 (int64_t)regs[r2]; 6165 } 6166 break; 6167 6168 case DIF_OP_UDIV: 6169 if (regs[r2] == 0) { 6170 regs[rd] = 0; 6171 *flags |= CPU_DTRACE_DIVZERO; 6172 } else { 6173 regs[rd] = regs[r1] / regs[r2]; 6174 } 6175 break; 6176 6177 case DIF_OP_SREM: 6178 if (regs[r2] == 0) { 6179 regs[rd] = 0; 6180 *flags |= CPU_DTRACE_DIVZERO; 6181 } else { 6182 regs[rd] = (int64_t)regs[r1] % 6183 (int64_t)regs[r2]; 6184 } 6185 break; 6186 6187 case DIF_OP_UREM: 6188 if (regs[r2] == 0) { 6189 regs[rd] = 0; 6190 *flags |= CPU_DTRACE_DIVZERO; 6191 } else { 6192 regs[rd] = regs[r1] % regs[r2]; 6193 } 6194 break; 6195 6196 case DIF_OP_NOT: 6197 regs[rd] = ~regs[r1]; 6198 break; 6199 case DIF_OP_MOV: 6200 regs[rd] = regs[r1]; 6201 break; 6202 case DIF_OP_CMP: 6203 cc_r = regs[r1] - regs[r2]; 6204 cc_n = cc_r < 0; 6205 cc_z = cc_r == 0; 6206 cc_v = 0; 6207 cc_c = regs[r1] < regs[r2]; 6208 break; 6209 case DIF_OP_TST: 6210 cc_n = cc_v = cc_c = 0; 6211 cc_z = regs[r1] == 0; 6212 break; 6213 case DIF_OP_BA: 6214 pc = DIF_INSTR_LABEL(instr); 6215 break; 6216 case DIF_OP_BE: 6217 if (cc_z) 6218 pc = DIF_INSTR_LABEL(instr); 6219 break; 6220 case DIF_OP_BNE: 6221 if (cc_z == 0) 6222 pc = DIF_INSTR_LABEL(instr); 6223 break; 6224 case DIF_OP_BG: 6225 if ((cc_z | (cc_n ^ cc_v)) == 0) 6226 pc = DIF_INSTR_LABEL(instr); 6227 break; 6228 case DIF_OP_BGU: 6229 if ((cc_c | cc_z) == 0) 6230 pc = DIF_INSTR_LABEL(instr); 6231 break; 6232 case DIF_OP_BGE: 6233 if ((cc_n ^ cc_v) == 0) 6234 pc = DIF_INSTR_LABEL(instr); 6235 break; 6236 case DIF_OP_BGEU: 6237 if (cc_c == 0) 6238 pc = DIF_INSTR_LABEL(instr); 6239 break; 6240 case DIF_OP_BL: 6241 if (cc_n ^ cc_v) 6242 pc = DIF_INSTR_LABEL(instr); 6243 break; 6244 case DIF_OP_BLU: 6245 if (cc_c) 6246 pc = DIF_INSTR_LABEL(instr); 6247 break; 6248 case DIF_OP_BLE: 6249 if (cc_z | (cc_n ^ cc_v)) 6250 pc = DIF_INSTR_LABEL(instr); 6251 break; 6252 case DIF_OP_BLEU: 6253 if (cc_c | cc_z) 6254 pc = DIF_INSTR_LABEL(instr); 6255 break; 6256 case DIF_OP_RLDSB: 6257 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6258 break; 6259 /*FALLTHROUGH*/ 6260 case DIF_OP_LDSB: 6261 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 6262 break; 6263 case DIF_OP_RLDSH: 6264 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6265 break; 6266 /*FALLTHROUGH*/ 6267 case DIF_OP_LDSH: 6268 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 6269 break; 6270 case DIF_OP_RLDSW: 6271 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6272 break; 6273 /*FALLTHROUGH*/ 6274 case DIF_OP_LDSW: 6275 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 6276 break; 6277 case DIF_OP_RLDUB: 6278 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6279 break; 6280 /*FALLTHROUGH*/ 6281 case DIF_OP_LDUB: 6282 regs[rd] = dtrace_load8(regs[r1]); 6283 break; 6284 case DIF_OP_RLDUH: 6285 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6286 break; 6287 /*FALLTHROUGH*/ 6288 case DIF_OP_LDUH: 6289 regs[rd] = dtrace_load16(regs[r1]); 6290 break; 6291 case DIF_OP_RLDUW: 6292 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6293 break; 6294 /*FALLTHROUGH*/ 6295 case DIF_OP_LDUW: 6296 regs[rd] = dtrace_load32(regs[r1]); 6297 break; 6298 case DIF_OP_RLDX: 6299 if (!dtrace_canload(regs[r1], 8, mstate, vstate)) 6300 break; 6301 /*FALLTHROUGH*/ 6302 case DIF_OP_LDX: 6303 regs[rd] = dtrace_load64(regs[r1]); 6304 break; 6305 case DIF_OP_ULDSB: 6306 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6307 regs[rd] = (int8_t) 6308 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6309 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6310 break; 6311 case DIF_OP_ULDSH: 6312 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6313 regs[rd] = (int16_t) 6314 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6315 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6316 break; 6317 case DIF_OP_ULDSW: 6318 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6319 regs[rd] = (int32_t) 6320 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6321 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6322 break; 6323 case DIF_OP_ULDUB: 6324 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6325 regs[rd] = 6326 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6327 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6328 break; 6329 case DIF_OP_ULDUH: 6330 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6331 regs[rd] = 6332 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6333 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6334 break; 6335 case DIF_OP_ULDUW: 6336 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6337 regs[rd] = 6338 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6339 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6340 break; 6341 case DIF_OP_ULDX: 6342 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6343 regs[rd] = 6344 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 6345 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6346 break; 6347 case DIF_OP_RET: 6348 rval = regs[rd]; 6349 pc = textlen; 6350 break; 6351 case DIF_OP_NOP: 6352 break; 6353 case DIF_OP_SETX: 6354 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 6355 break; 6356 case DIF_OP_SETS: 6357 regs[rd] = (uint64_t)(uintptr_t) 6358 (strtab + DIF_INSTR_STRING(instr)); 6359 break; 6360 case DIF_OP_SCMP: { 6361 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 6362 uintptr_t s1 = regs[r1]; 6363 uintptr_t s2 = regs[r2]; 6364 size_t lim1, lim2; 6365 6366 if (s1 != 0 && 6367 !dtrace_strcanload(s1, sz, &lim1, mstate, vstate)) 6368 break; 6369 if (s2 != 0 && 6370 !dtrace_strcanload(s2, sz, &lim2, mstate, vstate)) 6371 break; 6372 6373 cc_r = dtrace_strncmp((char *)s1, (char *)s2, 6374 MIN(lim1, lim2)); 6375 6376 cc_n = cc_r < 0; 6377 cc_z = cc_r == 0; 6378 cc_v = cc_c = 0; 6379 break; 6380 } 6381 case DIF_OP_LDGA: 6382 regs[rd] = dtrace_dif_variable(mstate, state, 6383 r1, regs[r2]); 6384 break; 6385 case DIF_OP_LDGS: 6386 id = DIF_INSTR_VAR(instr); 6387 6388 if (id >= DIF_VAR_OTHER_UBASE) { 6389 uintptr_t a; 6390 6391 id -= DIF_VAR_OTHER_UBASE; 6392 svar = vstate->dtvs_globals[id]; 6393 ASSERT(svar != NULL); 6394 v = &svar->dtsv_var; 6395 6396 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 6397 regs[rd] = svar->dtsv_data; 6398 break; 6399 } 6400 6401 a = (uintptr_t)svar->dtsv_data; 6402 6403 if (*(uint8_t *)a == UINT8_MAX) { 6404 /* 6405 * If the 0th byte is set to UINT8_MAX 6406 * then this is to be treated as a 6407 * reference to a NULL variable. 6408 */ 6409 regs[rd] = 0; 6410 } else { 6411 regs[rd] = a + sizeof (uint64_t); 6412 } 6413 6414 break; 6415 } 6416 6417 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 6418 break; 6419 6420 case DIF_OP_STGS: 6421 id = DIF_INSTR_VAR(instr); 6422 6423 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6424 id -= DIF_VAR_OTHER_UBASE; 6425 6426 VERIFY(id < vstate->dtvs_nglobals); 6427 svar = vstate->dtvs_globals[id]; 6428 ASSERT(svar != NULL); 6429 v = &svar->dtsv_var; 6430 6431 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6432 uintptr_t a = (uintptr_t)svar->dtsv_data; 6433 size_t lim; 6434 6435 ASSERT(a != 0); 6436 ASSERT(svar->dtsv_size != 0); 6437 6438 if (regs[rd] == 0) { 6439 *(uint8_t *)a = UINT8_MAX; 6440 break; 6441 } else { 6442 *(uint8_t *)a = 0; 6443 a += sizeof (uint64_t); 6444 } 6445 if (!dtrace_vcanload( 6446 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6447 &lim, mstate, vstate)) 6448 break; 6449 6450 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6451 (void *)a, &v->dtdv_type, lim); 6452 break; 6453 } 6454 6455 svar->dtsv_data = regs[rd]; 6456 break; 6457 6458 case DIF_OP_LDTA: 6459 /* 6460 * There are no DTrace built-in thread-local arrays at 6461 * present. This opcode is saved for future work. 6462 */ 6463 *flags |= CPU_DTRACE_ILLOP; 6464 regs[rd] = 0; 6465 break; 6466 6467 case DIF_OP_LDLS: 6468 id = DIF_INSTR_VAR(instr); 6469 6470 if (id < DIF_VAR_OTHER_UBASE) { 6471 /* 6472 * For now, this has no meaning. 6473 */ 6474 regs[rd] = 0; 6475 break; 6476 } 6477 6478 id -= DIF_VAR_OTHER_UBASE; 6479 6480 ASSERT(id < vstate->dtvs_nlocals); 6481 ASSERT(vstate->dtvs_locals != NULL); 6482 6483 svar = vstate->dtvs_locals[id]; 6484 ASSERT(svar != NULL); 6485 v = &svar->dtsv_var; 6486 6487 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6488 uintptr_t a = (uintptr_t)svar->dtsv_data; 6489 size_t sz = v->dtdv_type.dtdt_size; 6490 size_t lim; 6491 6492 sz += sizeof (uint64_t); 6493 ASSERT(svar->dtsv_size == NCPU * sz); 6494 a += curcpu * sz; 6495 6496 if (*(uint8_t *)a == UINT8_MAX) { 6497 /* 6498 * If the 0th byte is set to UINT8_MAX 6499 * then this is to be treated as a 6500 * reference to a NULL variable. 6501 */ 6502 regs[rd] = 0; 6503 } else { 6504 regs[rd] = a + sizeof (uint64_t); 6505 } 6506 6507 break; 6508 } 6509 6510 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6511 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6512 regs[rd] = tmp[curcpu]; 6513 break; 6514 6515 case DIF_OP_STLS: 6516 id = DIF_INSTR_VAR(instr); 6517 6518 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6519 id -= DIF_VAR_OTHER_UBASE; 6520 VERIFY(id < vstate->dtvs_nlocals); 6521 6522 ASSERT(vstate->dtvs_locals != NULL); 6523 svar = vstate->dtvs_locals[id]; 6524 ASSERT(svar != NULL); 6525 v = &svar->dtsv_var; 6526 6527 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6528 uintptr_t a = (uintptr_t)svar->dtsv_data; 6529 size_t sz = v->dtdv_type.dtdt_size; 6530 size_t lim; 6531 6532 sz += sizeof (uint64_t); 6533 ASSERT(svar->dtsv_size == NCPU * sz); 6534 a += curcpu * sz; 6535 6536 if (regs[rd] == 0) { 6537 *(uint8_t *)a = UINT8_MAX; 6538 break; 6539 } else { 6540 *(uint8_t *)a = 0; 6541 a += sizeof (uint64_t); 6542 } 6543 6544 if (!dtrace_vcanload( 6545 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6546 &lim, mstate, vstate)) 6547 break; 6548 6549 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6550 (void *)a, &v->dtdv_type, lim); 6551 break; 6552 } 6553 6554 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6555 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6556 tmp[curcpu] = regs[rd]; 6557 break; 6558 6559 case DIF_OP_LDTS: { 6560 dtrace_dynvar_t *dvar; 6561 dtrace_key_t *key; 6562 6563 id = DIF_INSTR_VAR(instr); 6564 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6565 id -= DIF_VAR_OTHER_UBASE; 6566 v = &vstate->dtvs_tlocals[id]; 6567 6568 key = &tupregs[DIF_DTR_NREGS]; 6569 key[0].dttk_value = (uint64_t)id; 6570 key[0].dttk_size = 0; 6571 DTRACE_TLS_THRKEY(key[1].dttk_value); 6572 key[1].dttk_size = 0; 6573 6574 dvar = dtrace_dynvar(dstate, 2, key, 6575 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 6576 mstate, vstate); 6577 6578 if (dvar == NULL) { 6579 regs[rd] = 0; 6580 break; 6581 } 6582 6583 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6584 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6585 } else { 6586 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6587 } 6588 6589 break; 6590 } 6591 6592 case DIF_OP_STTS: { 6593 dtrace_dynvar_t *dvar; 6594 dtrace_key_t *key; 6595 6596 id = DIF_INSTR_VAR(instr); 6597 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6598 id -= DIF_VAR_OTHER_UBASE; 6599 VERIFY(id < vstate->dtvs_ntlocals); 6600 6601 key = &tupregs[DIF_DTR_NREGS]; 6602 key[0].dttk_value = (uint64_t)id; 6603 key[0].dttk_size = 0; 6604 DTRACE_TLS_THRKEY(key[1].dttk_value); 6605 key[1].dttk_size = 0; 6606 v = &vstate->dtvs_tlocals[id]; 6607 6608 dvar = dtrace_dynvar(dstate, 2, key, 6609 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6610 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6611 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6612 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6613 6614 /* 6615 * Given that we're storing to thread-local data, 6616 * we need to flush our predicate cache. 6617 */ 6618 curthread->t_predcache = 0; 6619 6620 if (dvar == NULL) 6621 break; 6622 6623 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6624 size_t lim; 6625 6626 if (!dtrace_vcanload( 6627 (void *)(uintptr_t)regs[rd], 6628 &v->dtdv_type, &lim, mstate, vstate)) 6629 break; 6630 6631 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6632 dvar->dtdv_data, &v->dtdv_type, lim); 6633 } else { 6634 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6635 } 6636 6637 break; 6638 } 6639 6640 case DIF_OP_SRA: 6641 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 6642 break; 6643 6644 case DIF_OP_CALL: 6645 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 6646 regs, tupregs, ttop, mstate, state); 6647 break; 6648 6649 case DIF_OP_PUSHTR: 6650 if (ttop == DIF_DTR_NREGS) { 6651 *flags |= CPU_DTRACE_TUPOFLOW; 6652 break; 6653 } 6654 6655 if (r1 == DIF_TYPE_STRING) { 6656 /* 6657 * If this is a string type and the size is 0, 6658 * we'll use the system-wide default string 6659 * size. Note that we are _not_ looking at 6660 * the value of the DTRACEOPT_STRSIZE option; 6661 * had this been set, we would expect to have 6662 * a non-zero size value in the "pushtr". 6663 */ 6664 tupregs[ttop].dttk_size = 6665 dtrace_strlen((char *)(uintptr_t)regs[rd], 6666 regs[r2] ? regs[r2] : 6667 dtrace_strsize_default) + 1; 6668 } else { 6669 if (regs[r2] > LONG_MAX) { 6670 *flags |= CPU_DTRACE_ILLOP; 6671 break; 6672 } 6673 6674 tupregs[ttop].dttk_size = regs[r2]; 6675 } 6676 6677 tupregs[ttop++].dttk_value = regs[rd]; 6678 break; 6679 6680 case DIF_OP_PUSHTV: 6681 if (ttop == DIF_DTR_NREGS) { 6682 *flags |= CPU_DTRACE_TUPOFLOW; 6683 break; 6684 } 6685 6686 tupregs[ttop].dttk_value = regs[rd]; 6687 tupregs[ttop++].dttk_size = 0; 6688 break; 6689 6690 case DIF_OP_POPTS: 6691 if (ttop != 0) 6692 ttop--; 6693 break; 6694 6695 case DIF_OP_FLUSHTS: 6696 ttop = 0; 6697 break; 6698 6699 case DIF_OP_LDGAA: 6700 case DIF_OP_LDTAA: { 6701 dtrace_dynvar_t *dvar; 6702 dtrace_key_t *key = tupregs; 6703 uint_t nkeys = ttop; 6704 6705 id = DIF_INSTR_VAR(instr); 6706 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6707 id -= DIF_VAR_OTHER_UBASE; 6708 6709 key[nkeys].dttk_value = (uint64_t)id; 6710 key[nkeys++].dttk_size = 0; 6711 6712 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 6713 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6714 key[nkeys++].dttk_size = 0; 6715 VERIFY(id < vstate->dtvs_ntlocals); 6716 v = &vstate->dtvs_tlocals[id]; 6717 } else { 6718 VERIFY(id < vstate->dtvs_nglobals); 6719 v = &vstate->dtvs_globals[id]->dtsv_var; 6720 } 6721 6722 dvar = dtrace_dynvar(dstate, nkeys, key, 6723 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6724 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6725 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 6726 6727 if (dvar == NULL) { 6728 regs[rd] = 0; 6729 break; 6730 } 6731 6732 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6733 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6734 } else { 6735 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6736 } 6737 6738 break; 6739 } 6740 6741 case DIF_OP_STGAA: 6742 case DIF_OP_STTAA: { 6743 dtrace_dynvar_t *dvar; 6744 dtrace_key_t *key = tupregs; 6745 uint_t nkeys = ttop; 6746 6747 id = DIF_INSTR_VAR(instr); 6748 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6749 id -= DIF_VAR_OTHER_UBASE; 6750 6751 key[nkeys].dttk_value = (uint64_t)id; 6752 key[nkeys++].dttk_size = 0; 6753 6754 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 6755 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6756 key[nkeys++].dttk_size = 0; 6757 VERIFY(id < vstate->dtvs_ntlocals); 6758 v = &vstate->dtvs_tlocals[id]; 6759 } else { 6760 VERIFY(id < vstate->dtvs_nglobals); 6761 v = &vstate->dtvs_globals[id]->dtsv_var; 6762 } 6763 6764 dvar = dtrace_dynvar(dstate, nkeys, key, 6765 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6766 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6767 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6768 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6769 6770 if (dvar == NULL) 6771 break; 6772 6773 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6774 size_t lim; 6775 6776 if (!dtrace_vcanload( 6777 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6778 &lim, mstate, vstate)) 6779 break; 6780 6781 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6782 dvar->dtdv_data, &v->dtdv_type, lim); 6783 } else { 6784 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6785 } 6786 6787 break; 6788 } 6789 6790 case DIF_OP_ALLOCS: { 6791 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6792 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 6793 6794 /* 6795 * Rounding up the user allocation size could have 6796 * overflowed large, bogus allocations (like -1ULL) to 6797 * 0. 6798 */ 6799 if (size < regs[r1] || 6800 !DTRACE_INSCRATCH(mstate, size)) { 6801 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6802 regs[rd] = 0; 6803 break; 6804 } 6805 6806 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 6807 mstate->dtms_scratch_ptr += size; 6808 regs[rd] = ptr; 6809 break; 6810 } 6811 6812 case DIF_OP_COPYS: 6813 if (!dtrace_canstore(regs[rd], regs[r2], 6814 mstate, vstate)) { 6815 *flags |= CPU_DTRACE_BADADDR; 6816 *illval = regs[rd]; 6817 break; 6818 } 6819 6820 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 6821 break; 6822 6823 dtrace_bcopy((void *)(uintptr_t)regs[r1], 6824 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 6825 break; 6826 6827 case DIF_OP_STB: 6828 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 6829 *flags |= CPU_DTRACE_BADADDR; 6830 *illval = regs[rd]; 6831 break; 6832 } 6833 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 6834 break; 6835 6836 case DIF_OP_STH: 6837 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 6838 *flags |= CPU_DTRACE_BADADDR; 6839 *illval = regs[rd]; 6840 break; 6841 } 6842 if (regs[rd] & 1) { 6843 *flags |= CPU_DTRACE_BADALIGN; 6844 *illval = regs[rd]; 6845 break; 6846 } 6847 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 6848 break; 6849 6850 case DIF_OP_STW: 6851 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 6852 *flags |= CPU_DTRACE_BADADDR; 6853 *illval = regs[rd]; 6854 break; 6855 } 6856 if (regs[rd] & 3) { 6857 *flags |= CPU_DTRACE_BADALIGN; 6858 *illval = regs[rd]; 6859 break; 6860 } 6861 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 6862 break; 6863 6864 case DIF_OP_STX: 6865 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 6866 *flags |= CPU_DTRACE_BADADDR; 6867 *illval = regs[rd]; 6868 break; 6869 } 6870 if (regs[rd] & 7) { 6871 *flags |= CPU_DTRACE_BADALIGN; 6872 *illval = regs[rd]; 6873 break; 6874 } 6875 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 6876 break; 6877 } 6878 } 6879 6880 if (!(*flags & CPU_DTRACE_FAULT)) 6881 return (rval); 6882 6883 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 6884 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 6885 6886 return (0); 6887 } 6888 6889 static void 6890 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 6891 { 6892 dtrace_probe_t *probe = ecb->dte_probe; 6893 dtrace_provider_t *prov = probe->dtpr_provider; 6894 char c[DTRACE_FULLNAMELEN + 80], *str; 6895 char *msg = "dtrace: breakpoint action at probe "; 6896 char *ecbmsg = " (ecb "; 6897 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 6898 uintptr_t val = (uintptr_t)ecb; 6899 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 6900 6901 if (dtrace_destructive_disallow) 6902 return; 6903 6904 /* 6905 * It's impossible to be taking action on the NULL probe. 6906 */ 6907 ASSERT(probe != NULL); 6908 6909 /* 6910 * This is a poor man's (destitute man's?) sprintf(): we want to 6911 * print the provider name, module name, function name and name of 6912 * the probe, along with the hex address of the ECB with the breakpoint 6913 * action -- all of which we must place in the character buffer by 6914 * hand. 6915 */ 6916 while (*msg != '\0') 6917 c[i++] = *msg++; 6918 6919 for (str = prov->dtpv_name; *str != '\0'; str++) 6920 c[i++] = *str; 6921 c[i++] = ':'; 6922 6923 for (str = probe->dtpr_mod; *str != '\0'; str++) 6924 c[i++] = *str; 6925 c[i++] = ':'; 6926 6927 for (str = probe->dtpr_func; *str != '\0'; str++) 6928 c[i++] = *str; 6929 c[i++] = ':'; 6930 6931 for (str = probe->dtpr_name; *str != '\0'; str++) 6932 c[i++] = *str; 6933 6934 while (*ecbmsg != '\0') 6935 c[i++] = *ecbmsg++; 6936 6937 while (shift >= 0) { 6938 mask = (uintptr_t)0xf << shift; 6939 6940 if (val >= ((uintptr_t)1 << shift)) 6941 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 6942 shift -= 4; 6943 } 6944 6945 c[i++] = ')'; 6946 c[i] = '\0'; 6947 6948 #ifdef illumos 6949 debug_enter(c); 6950 #else 6951 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 6952 #endif 6953 } 6954 6955 static void 6956 dtrace_action_panic(dtrace_ecb_t *ecb) 6957 { 6958 dtrace_probe_t *probe = ecb->dte_probe; 6959 6960 /* 6961 * It's impossible to be taking action on the NULL probe. 6962 */ 6963 ASSERT(probe != NULL); 6964 6965 if (dtrace_destructive_disallow) 6966 return; 6967 6968 if (dtrace_panicked != NULL) 6969 return; 6970 6971 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 6972 return; 6973 6974 /* 6975 * We won the right to panic. (We want to be sure that only one 6976 * thread calls panic() from dtrace_probe(), and that panic() is 6977 * called exactly once.) 6978 */ 6979 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 6980 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 6981 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 6982 } 6983 6984 static void 6985 dtrace_action_raise(uint64_t sig) 6986 { 6987 if (dtrace_destructive_disallow) 6988 return; 6989 6990 if (sig >= NSIG) { 6991 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 6992 return; 6993 } 6994 6995 #ifdef illumos 6996 /* 6997 * raise() has a queue depth of 1 -- we ignore all subsequent 6998 * invocations of the raise() action. 6999 */ 7000 if (curthread->t_dtrace_sig == 0) 7001 curthread->t_dtrace_sig = (uint8_t)sig; 7002 7003 curthread->t_sig_check = 1; 7004 aston(curthread); 7005 #else 7006 struct proc *p = curproc; 7007 PROC_LOCK(p); 7008 kern_psignal(p, sig); 7009 PROC_UNLOCK(p); 7010 #endif 7011 } 7012 7013 static void 7014 dtrace_action_stop(void) 7015 { 7016 if (dtrace_destructive_disallow) 7017 return; 7018 7019 #ifdef illumos 7020 if (!curthread->t_dtrace_stop) { 7021 curthread->t_dtrace_stop = 1; 7022 curthread->t_sig_check = 1; 7023 aston(curthread); 7024 } 7025 #else 7026 struct proc *p = curproc; 7027 PROC_LOCK(p); 7028 kern_psignal(p, SIGSTOP); 7029 PROC_UNLOCK(p); 7030 #endif 7031 } 7032 7033 static void 7034 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 7035 { 7036 hrtime_t now; 7037 volatile uint16_t *flags; 7038 #ifdef illumos 7039 cpu_t *cpu = CPU; 7040 #else 7041 cpu_t *cpu = &solaris_cpu[curcpu]; 7042 #endif 7043 7044 if (dtrace_destructive_disallow) 7045 return; 7046 7047 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 7048 7049 now = dtrace_gethrtime(); 7050 7051 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 7052 /* 7053 * We need to advance the mark to the current time. 7054 */ 7055 cpu->cpu_dtrace_chillmark = now; 7056 cpu->cpu_dtrace_chilled = 0; 7057 } 7058 7059 /* 7060 * Now check to see if the requested chill time would take us over 7061 * the maximum amount of time allowed in the chill interval. (Or 7062 * worse, if the calculation itself induces overflow.) 7063 */ 7064 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 7065 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 7066 *flags |= CPU_DTRACE_ILLOP; 7067 return; 7068 } 7069 7070 while (dtrace_gethrtime() - now < val) 7071 continue; 7072 7073 /* 7074 * Normally, we assure that the value of the variable "timestamp" does 7075 * not change within an ECB. The presence of chill() represents an 7076 * exception to this rule, however. 7077 */ 7078 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 7079 cpu->cpu_dtrace_chilled += val; 7080 } 7081 7082 static void 7083 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 7084 uint64_t *buf, uint64_t arg) 7085 { 7086 int nframes = DTRACE_USTACK_NFRAMES(arg); 7087 int strsize = DTRACE_USTACK_STRSIZE(arg); 7088 uint64_t *pcs = &buf[1], *fps; 7089 char *str = (char *)&pcs[nframes]; 7090 int size, offs = 0, i, j; 7091 size_t rem; 7092 uintptr_t old = mstate->dtms_scratch_ptr, saved; 7093 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 7094 char *sym; 7095 7096 /* 7097 * Should be taking a faster path if string space has not been 7098 * allocated. 7099 */ 7100 ASSERT(strsize != 0); 7101 7102 /* 7103 * We will first allocate some temporary space for the frame pointers. 7104 */ 7105 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 7106 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 7107 (nframes * sizeof (uint64_t)); 7108 7109 if (!DTRACE_INSCRATCH(mstate, size)) { 7110 /* 7111 * Not enough room for our frame pointers -- need to indicate 7112 * that we ran out of scratch space. 7113 */ 7114 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 7115 return; 7116 } 7117 7118 mstate->dtms_scratch_ptr += size; 7119 saved = mstate->dtms_scratch_ptr; 7120 7121 /* 7122 * Now get a stack with both program counters and frame pointers. 7123 */ 7124 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7125 dtrace_getufpstack(buf, fps, nframes + 1); 7126 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7127 7128 /* 7129 * If that faulted, we're cooked. 7130 */ 7131 if (*flags & CPU_DTRACE_FAULT) 7132 goto out; 7133 7134 /* 7135 * Now we want to walk up the stack, calling the USTACK helper. For 7136 * each iteration, we restore the scratch pointer. 7137 */ 7138 for (i = 0; i < nframes; i++) { 7139 mstate->dtms_scratch_ptr = saved; 7140 7141 if (offs >= strsize) 7142 break; 7143 7144 sym = (char *)(uintptr_t)dtrace_helper( 7145 DTRACE_HELPER_ACTION_USTACK, 7146 mstate, state, pcs[i], fps[i]); 7147 7148 /* 7149 * If we faulted while running the helper, we're going to 7150 * clear the fault and null out the corresponding string. 7151 */ 7152 if (*flags & CPU_DTRACE_FAULT) { 7153 *flags &= ~CPU_DTRACE_FAULT; 7154 str[offs++] = '\0'; 7155 continue; 7156 } 7157 7158 if (sym == NULL) { 7159 str[offs++] = '\0'; 7160 continue; 7161 } 7162 7163 if (!dtrace_strcanload((uintptr_t)sym, strsize, &rem, mstate, 7164 &(state->dts_vstate))) { 7165 str[offs++] = '\0'; 7166 continue; 7167 } 7168 7169 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7170 7171 /* 7172 * Now copy in the string that the helper returned to us. 7173 */ 7174 for (j = 0; offs + j < strsize && j < rem; j++) { 7175 if ((str[offs + j] = sym[j]) == '\0') 7176 break; 7177 } 7178 7179 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7180 7181 offs += j + 1; 7182 } 7183 7184 if (offs >= strsize) { 7185 /* 7186 * If we didn't have room for all of the strings, we don't 7187 * abort processing -- this needn't be a fatal error -- but we 7188 * still want to increment a counter (dts_stkstroverflows) to 7189 * allow this condition to be warned about. (If this is from 7190 * a jstack() action, it is easily tuned via jstackstrsize.) 7191 */ 7192 dtrace_error(&state->dts_stkstroverflows); 7193 } 7194 7195 while (offs < strsize) 7196 str[offs++] = '\0'; 7197 7198 out: 7199 mstate->dtms_scratch_ptr = old; 7200 } 7201 7202 static void 7203 dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size, 7204 size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind) 7205 { 7206 volatile uint16_t *flags; 7207 uint64_t val = *valp; 7208 size_t valoffs = *valoffsp; 7209 7210 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 7211 ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF); 7212 7213 /* 7214 * If this is a string, we're going to only load until we find the zero 7215 * byte -- after which we'll store zero bytes. 7216 */ 7217 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 7218 char c = '\0' + 1; 7219 size_t s; 7220 7221 for (s = 0; s < size; s++) { 7222 if (c != '\0' && dtkind == DIF_TF_BYREF) { 7223 c = dtrace_load8(val++); 7224 } else if (c != '\0' && dtkind == DIF_TF_BYUREF) { 7225 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7226 c = dtrace_fuword8((void *)(uintptr_t)val++); 7227 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7228 if (*flags & CPU_DTRACE_FAULT) 7229 break; 7230 } 7231 7232 DTRACE_STORE(uint8_t, tomax, valoffs++, c); 7233 7234 if (c == '\0' && intuple) 7235 break; 7236 } 7237 } else { 7238 uint8_t c; 7239 while (valoffs < end) { 7240 if (dtkind == DIF_TF_BYREF) { 7241 c = dtrace_load8(val++); 7242 } else if (dtkind == DIF_TF_BYUREF) { 7243 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7244 c = dtrace_fuword8((void *)(uintptr_t)val++); 7245 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7246 if (*flags & CPU_DTRACE_FAULT) 7247 break; 7248 } 7249 7250 DTRACE_STORE(uint8_t, tomax, 7251 valoffs++, c); 7252 } 7253 } 7254 7255 *valp = val; 7256 *valoffsp = valoffs; 7257 } 7258 7259 /* 7260 * If you're looking for the epicenter of DTrace, you just found it. This 7261 * is the function called by the provider to fire a probe -- from which all 7262 * subsequent probe-context DTrace activity emanates. 7263 */ 7264 void 7265 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 7266 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 7267 { 7268 processorid_t cpuid; 7269 dtrace_icookie_t cookie; 7270 dtrace_probe_t *probe; 7271 dtrace_mstate_t mstate; 7272 dtrace_ecb_t *ecb; 7273 dtrace_action_t *act; 7274 intptr_t offs; 7275 size_t size; 7276 int vtime, onintr; 7277 volatile uint16_t *flags; 7278 hrtime_t now; 7279 7280 if (panicstr != NULL) 7281 return; 7282 7283 #ifdef illumos 7284 /* 7285 * Kick out immediately if this CPU is still being born (in which case 7286 * curthread will be set to -1) or the current thread can't allow 7287 * probes in its current context. 7288 */ 7289 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 7290 return; 7291 #endif 7292 7293 cookie = dtrace_interrupt_disable(); 7294 probe = dtrace_probes[id - 1]; 7295 cpuid = curcpu; 7296 onintr = CPU_ON_INTR(CPU); 7297 7298 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 7299 probe->dtpr_predcache == curthread->t_predcache) { 7300 /* 7301 * We have hit in the predicate cache; we know that 7302 * this predicate would evaluate to be false. 7303 */ 7304 dtrace_interrupt_enable(cookie); 7305 return; 7306 } 7307 7308 #ifdef illumos 7309 if (panic_quiesce) { 7310 #else 7311 if (panicstr != NULL) { 7312 #endif 7313 /* 7314 * We don't trace anything if we're panicking. 7315 */ 7316 dtrace_interrupt_enable(cookie); 7317 return; 7318 } 7319 7320 now = mstate.dtms_timestamp = dtrace_gethrtime(); 7321 mstate.dtms_present = DTRACE_MSTATE_TIMESTAMP; 7322 vtime = dtrace_vtime_references != 0; 7323 7324 if (vtime && curthread->t_dtrace_start) 7325 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 7326 7327 mstate.dtms_difo = NULL; 7328 mstate.dtms_probe = probe; 7329 mstate.dtms_strtok = 0; 7330 mstate.dtms_arg[0] = arg0; 7331 mstate.dtms_arg[1] = arg1; 7332 mstate.dtms_arg[2] = arg2; 7333 mstate.dtms_arg[3] = arg3; 7334 mstate.dtms_arg[4] = arg4; 7335 7336 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 7337 7338 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 7339 dtrace_predicate_t *pred = ecb->dte_predicate; 7340 dtrace_state_t *state = ecb->dte_state; 7341 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 7342 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 7343 dtrace_vstate_t *vstate = &state->dts_vstate; 7344 dtrace_provider_t *prov = probe->dtpr_provider; 7345 uint64_t tracememsize = 0; 7346 int committed = 0; 7347 caddr_t tomax; 7348 7349 /* 7350 * A little subtlety with the following (seemingly innocuous) 7351 * declaration of the automatic 'val': by looking at the 7352 * code, you might think that it could be declared in the 7353 * action processing loop, below. (That is, it's only used in 7354 * the action processing loop.) However, it must be declared 7355 * out of that scope because in the case of DIF expression 7356 * arguments to aggregating actions, one iteration of the 7357 * action loop will use the last iteration's value. 7358 */ 7359 uint64_t val = 0; 7360 7361 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 7362 mstate.dtms_getf = NULL; 7363 7364 *flags &= ~CPU_DTRACE_ERROR; 7365 7366 if (prov == dtrace_provider) { 7367 /* 7368 * If dtrace itself is the provider of this probe, 7369 * we're only going to continue processing the ECB if 7370 * arg0 (the dtrace_state_t) is equal to the ECB's 7371 * creating state. (This prevents disjoint consumers 7372 * from seeing one another's metaprobes.) 7373 */ 7374 if (arg0 != (uint64_t)(uintptr_t)state) 7375 continue; 7376 } 7377 7378 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 7379 /* 7380 * We're not currently active. If our provider isn't 7381 * the dtrace pseudo provider, we're not interested. 7382 */ 7383 if (prov != dtrace_provider) 7384 continue; 7385 7386 /* 7387 * Now we must further check if we are in the BEGIN 7388 * probe. If we are, we will only continue processing 7389 * if we're still in WARMUP -- if one BEGIN enabling 7390 * has invoked the exit() action, we don't want to 7391 * evaluate subsequent BEGIN enablings. 7392 */ 7393 if (probe->dtpr_id == dtrace_probeid_begin && 7394 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 7395 ASSERT(state->dts_activity == 7396 DTRACE_ACTIVITY_DRAINING); 7397 continue; 7398 } 7399 } 7400 7401 if (ecb->dte_cond) { 7402 /* 7403 * If the dte_cond bits indicate that this 7404 * consumer is only allowed to see user-mode firings 7405 * of this probe, call the provider's dtps_usermode() 7406 * entry point to check that the probe was fired 7407 * while in a user context. Skip this ECB if that's 7408 * not the case. 7409 */ 7410 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 7411 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 7412 probe->dtpr_id, probe->dtpr_arg) == 0) 7413 continue; 7414 7415 #ifdef illumos 7416 /* 7417 * This is more subtle than it looks. We have to be 7418 * absolutely certain that CRED() isn't going to 7419 * change out from under us so it's only legit to 7420 * examine that structure if we're in constrained 7421 * situations. Currently, the only times we'll this 7422 * check is if a non-super-user has enabled the 7423 * profile or syscall providers -- providers that 7424 * allow visibility of all processes. For the 7425 * profile case, the check above will ensure that 7426 * we're examining a user context. 7427 */ 7428 if (ecb->dte_cond & DTRACE_COND_OWNER) { 7429 cred_t *cr; 7430 cred_t *s_cr = 7431 ecb->dte_state->dts_cred.dcr_cred; 7432 proc_t *proc; 7433 7434 ASSERT(s_cr != NULL); 7435 7436 if ((cr = CRED()) == NULL || 7437 s_cr->cr_uid != cr->cr_uid || 7438 s_cr->cr_uid != cr->cr_ruid || 7439 s_cr->cr_uid != cr->cr_suid || 7440 s_cr->cr_gid != cr->cr_gid || 7441 s_cr->cr_gid != cr->cr_rgid || 7442 s_cr->cr_gid != cr->cr_sgid || 7443 (proc = ttoproc(curthread)) == NULL || 7444 (proc->p_flag & SNOCD)) 7445 continue; 7446 } 7447 7448 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 7449 cred_t *cr; 7450 cred_t *s_cr = 7451 ecb->dte_state->dts_cred.dcr_cred; 7452 7453 ASSERT(s_cr != NULL); 7454 7455 if ((cr = CRED()) == NULL || 7456 s_cr->cr_zone->zone_id != 7457 cr->cr_zone->zone_id) 7458 continue; 7459 } 7460 #endif 7461 } 7462 7463 if (now - state->dts_alive > dtrace_deadman_timeout) { 7464 /* 7465 * We seem to be dead. Unless we (a) have kernel 7466 * destructive permissions (b) have explicitly enabled 7467 * destructive actions and (c) destructive actions have 7468 * not been disabled, we're going to transition into 7469 * the KILLED state, from which no further processing 7470 * on this state will be performed. 7471 */ 7472 if (!dtrace_priv_kernel_destructive(state) || 7473 !state->dts_cred.dcr_destructive || 7474 dtrace_destructive_disallow) { 7475 void *activity = &state->dts_activity; 7476 dtrace_activity_t current; 7477 7478 do { 7479 current = state->dts_activity; 7480 } while (dtrace_cas32(activity, current, 7481 DTRACE_ACTIVITY_KILLED) != current); 7482 7483 continue; 7484 } 7485 } 7486 7487 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 7488 ecb->dte_alignment, state, &mstate)) < 0) 7489 continue; 7490 7491 tomax = buf->dtb_tomax; 7492 ASSERT(tomax != NULL); 7493 7494 if (ecb->dte_size != 0) { 7495 dtrace_rechdr_t dtrh; 7496 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 7497 mstate.dtms_timestamp = dtrace_gethrtime(); 7498 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7499 } 7500 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 7501 dtrh.dtrh_epid = ecb->dte_epid; 7502 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 7503 mstate.dtms_timestamp); 7504 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 7505 } 7506 7507 mstate.dtms_epid = ecb->dte_epid; 7508 mstate.dtms_present |= DTRACE_MSTATE_EPID; 7509 7510 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 7511 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 7512 else 7513 mstate.dtms_access = 0; 7514 7515 if (pred != NULL) { 7516 dtrace_difo_t *dp = pred->dtp_difo; 7517 uint64_t rval; 7518 7519 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 7520 7521 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 7522 dtrace_cacheid_t cid = probe->dtpr_predcache; 7523 7524 if (cid != DTRACE_CACHEIDNONE && !onintr) { 7525 /* 7526 * Update the predicate cache... 7527 */ 7528 ASSERT(cid == pred->dtp_cacheid); 7529 curthread->t_predcache = cid; 7530 } 7531 7532 continue; 7533 } 7534 } 7535 7536 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 7537 act != NULL; act = act->dta_next) { 7538 size_t valoffs; 7539 dtrace_difo_t *dp; 7540 dtrace_recdesc_t *rec = &act->dta_rec; 7541 7542 size = rec->dtrd_size; 7543 valoffs = offs + rec->dtrd_offset; 7544 7545 if (DTRACEACT_ISAGG(act->dta_kind)) { 7546 uint64_t v = 0xbad; 7547 dtrace_aggregation_t *agg; 7548 7549 agg = (dtrace_aggregation_t *)act; 7550 7551 if ((dp = act->dta_difo) != NULL) 7552 v = dtrace_dif_emulate(dp, 7553 &mstate, vstate, state); 7554 7555 if (*flags & CPU_DTRACE_ERROR) 7556 continue; 7557 7558 /* 7559 * Note that we always pass the expression 7560 * value from the previous iteration of the 7561 * action loop. This value will only be used 7562 * if there is an expression argument to the 7563 * aggregating action, denoted by the 7564 * dtag_hasarg field. 7565 */ 7566 dtrace_aggregate(agg, buf, 7567 offs, aggbuf, v, val); 7568 continue; 7569 } 7570 7571 switch (act->dta_kind) { 7572 case DTRACEACT_STOP: 7573 if (dtrace_priv_proc_destructive(state)) 7574 dtrace_action_stop(); 7575 continue; 7576 7577 case DTRACEACT_BREAKPOINT: 7578 if (dtrace_priv_kernel_destructive(state)) 7579 dtrace_action_breakpoint(ecb); 7580 continue; 7581 7582 case DTRACEACT_PANIC: 7583 if (dtrace_priv_kernel_destructive(state)) 7584 dtrace_action_panic(ecb); 7585 continue; 7586 7587 case DTRACEACT_STACK: 7588 if (!dtrace_priv_kernel(state)) 7589 continue; 7590 7591 dtrace_getpcstack((pc_t *)(tomax + valoffs), 7592 size / sizeof (pc_t), probe->dtpr_aframes, 7593 DTRACE_ANCHORED(probe) ? NULL : 7594 (uint32_t *)arg0); 7595 continue; 7596 7597 case DTRACEACT_JSTACK: 7598 case DTRACEACT_USTACK: 7599 if (!dtrace_priv_proc(state)) 7600 continue; 7601 7602 /* 7603 * See comment in DIF_VAR_PID. 7604 */ 7605 if (DTRACE_ANCHORED(mstate.dtms_probe) && 7606 CPU_ON_INTR(CPU)) { 7607 int depth = DTRACE_USTACK_NFRAMES( 7608 rec->dtrd_arg) + 1; 7609 7610 dtrace_bzero((void *)(tomax + valoffs), 7611 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 7612 + depth * sizeof (uint64_t)); 7613 7614 continue; 7615 } 7616 7617 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 7618 curproc->p_dtrace_helpers != NULL) { 7619 /* 7620 * This is the slow path -- we have 7621 * allocated string space, and we're 7622 * getting the stack of a process that 7623 * has helpers. Call into a separate 7624 * routine to perform this processing. 7625 */ 7626 dtrace_action_ustack(&mstate, state, 7627 (uint64_t *)(tomax + valoffs), 7628 rec->dtrd_arg); 7629 continue; 7630 } 7631 7632 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7633 dtrace_getupcstack((uint64_t *) 7634 (tomax + valoffs), 7635 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 7636 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7637 continue; 7638 7639 default: 7640 break; 7641 } 7642 7643 dp = act->dta_difo; 7644 ASSERT(dp != NULL); 7645 7646 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 7647 7648 if (*flags & CPU_DTRACE_ERROR) 7649 continue; 7650 7651 switch (act->dta_kind) { 7652 case DTRACEACT_SPECULATE: { 7653 dtrace_rechdr_t *dtrh; 7654 7655 ASSERT(buf == &state->dts_buffer[cpuid]); 7656 buf = dtrace_speculation_buffer(state, 7657 cpuid, val); 7658 7659 if (buf == NULL) { 7660 *flags |= CPU_DTRACE_DROP; 7661 continue; 7662 } 7663 7664 offs = dtrace_buffer_reserve(buf, 7665 ecb->dte_needed, ecb->dte_alignment, 7666 state, NULL); 7667 7668 if (offs < 0) { 7669 *flags |= CPU_DTRACE_DROP; 7670 continue; 7671 } 7672 7673 tomax = buf->dtb_tomax; 7674 ASSERT(tomax != NULL); 7675 7676 if (ecb->dte_size == 0) 7677 continue; 7678 7679 ASSERT3U(ecb->dte_size, >=, 7680 sizeof (dtrace_rechdr_t)); 7681 dtrh = ((void *)(tomax + offs)); 7682 dtrh->dtrh_epid = ecb->dte_epid; 7683 /* 7684 * When the speculation is committed, all of 7685 * the records in the speculative buffer will 7686 * have their timestamps set to the commit 7687 * time. Until then, it is set to a sentinel 7688 * value, for debugability. 7689 */ 7690 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 7691 continue; 7692 } 7693 7694 case DTRACEACT_PRINTM: { 7695 /* The DIF returns a 'memref'. */ 7696 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 7697 7698 /* Get the size from the memref. */ 7699 size = memref[1]; 7700 7701 /* 7702 * Check if the size exceeds the allocated 7703 * buffer size. 7704 */ 7705 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7706 /* Flag a drop! */ 7707 *flags |= CPU_DTRACE_DROP; 7708 continue; 7709 } 7710 7711 /* Store the size in the buffer first. */ 7712 DTRACE_STORE(uintptr_t, tomax, 7713 valoffs, size); 7714 7715 /* 7716 * Offset the buffer address to the start 7717 * of the data. 7718 */ 7719 valoffs += sizeof(uintptr_t); 7720 7721 /* 7722 * Reset to the memory address rather than 7723 * the memref array, then let the BYREF 7724 * code below do the work to store the 7725 * memory data in the buffer. 7726 */ 7727 val = memref[0]; 7728 break; 7729 } 7730 7731 case DTRACEACT_CHILL: 7732 if (dtrace_priv_kernel_destructive(state)) 7733 dtrace_action_chill(&mstate, val); 7734 continue; 7735 7736 case DTRACEACT_RAISE: 7737 if (dtrace_priv_proc_destructive(state)) 7738 dtrace_action_raise(val); 7739 continue; 7740 7741 case DTRACEACT_COMMIT: 7742 ASSERT(!committed); 7743 7744 /* 7745 * We need to commit our buffer state. 7746 */ 7747 if (ecb->dte_size) 7748 buf->dtb_offset = offs + ecb->dte_size; 7749 buf = &state->dts_buffer[cpuid]; 7750 dtrace_speculation_commit(state, cpuid, val); 7751 committed = 1; 7752 continue; 7753 7754 case DTRACEACT_DISCARD: 7755 dtrace_speculation_discard(state, cpuid, val); 7756 continue; 7757 7758 case DTRACEACT_DIFEXPR: 7759 case DTRACEACT_LIBACT: 7760 case DTRACEACT_PRINTF: 7761 case DTRACEACT_PRINTA: 7762 case DTRACEACT_SYSTEM: 7763 case DTRACEACT_FREOPEN: 7764 case DTRACEACT_TRACEMEM: 7765 break; 7766 7767 case DTRACEACT_TRACEMEM_DYNSIZE: 7768 tracememsize = val; 7769 break; 7770 7771 case DTRACEACT_SYM: 7772 case DTRACEACT_MOD: 7773 if (!dtrace_priv_kernel(state)) 7774 continue; 7775 break; 7776 7777 case DTRACEACT_USYM: 7778 case DTRACEACT_UMOD: 7779 case DTRACEACT_UADDR: { 7780 #ifdef illumos 7781 struct pid *pid = curthread->t_procp->p_pidp; 7782 #endif 7783 7784 if (!dtrace_priv_proc(state)) 7785 continue; 7786 7787 DTRACE_STORE(uint64_t, tomax, 7788 #ifdef illumos 7789 valoffs, (uint64_t)pid->pid_id); 7790 #else 7791 valoffs, (uint64_t) curproc->p_pid); 7792 #endif 7793 DTRACE_STORE(uint64_t, tomax, 7794 valoffs + sizeof (uint64_t), val); 7795 7796 continue; 7797 } 7798 7799 case DTRACEACT_EXIT: { 7800 /* 7801 * For the exit action, we are going to attempt 7802 * to atomically set our activity to be 7803 * draining. If this fails (either because 7804 * another CPU has beat us to the exit action, 7805 * or because our current activity is something 7806 * other than ACTIVE or WARMUP), we will 7807 * continue. This assures that the exit action 7808 * can be successfully recorded at most once 7809 * when we're in the ACTIVE state. If we're 7810 * encountering the exit() action while in 7811 * COOLDOWN, however, we want to honor the new 7812 * status code. (We know that we're the only 7813 * thread in COOLDOWN, so there is no race.) 7814 */ 7815 void *activity = &state->dts_activity; 7816 dtrace_activity_t current = state->dts_activity; 7817 7818 if (current == DTRACE_ACTIVITY_COOLDOWN) 7819 break; 7820 7821 if (current != DTRACE_ACTIVITY_WARMUP) 7822 current = DTRACE_ACTIVITY_ACTIVE; 7823 7824 if (dtrace_cas32(activity, current, 7825 DTRACE_ACTIVITY_DRAINING) != current) { 7826 *flags |= CPU_DTRACE_DROP; 7827 continue; 7828 } 7829 7830 break; 7831 } 7832 7833 default: 7834 ASSERT(0); 7835 } 7836 7837 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF || 7838 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) { 7839 uintptr_t end = valoffs + size; 7840 7841 if (tracememsize != 0 && 7842 valoffs + tracememsize < end) { 7843 end = valoffs + tracememsize; 7844 tracememsize = 0; 7845 } 7846 7847 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF && 7848 !dtrace_vcanload((void *)(uintptr_t)val, 7849 &dp->dtdo_rtype, NULL, &mstate, vstate)) 7850 continue; 7851 7852 dtrace_store_by_ref(dp, tomax, size, &valoffs, 7853 &val, end, act->dta_intuple, 7854 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ? 7855 DIF_TF_BYREF: DIF_TF_BYUREF); 7856 continue; 7857 } 7858 7859 switch (size) { 7860 case 0: 7861 break; 7862 7863 case sizeof (uint8_t): 7864 DTRACE_STORE(uint8_t, tomax, valoffs, val); 7865 break; 7866 case sizeof (uint16_t): 7867 DTRACE_STORE(uint16_t, tomax, valoffs, val); 7868 break; 7869 case sizeof (uint32_t): 7870 DTRACE_STORE(uint32_t, tomax, valoffs, val); 7871 break; 7872 case sizeof (uint64_t): 7873 DTRACE_STORE(uint64_t, tomax, valoffs, val); 7874 break; 7875 default: 7876 /* 7877 * Any other size should have been returned by 7878 * reference, not by value. 7879 */ 7880 ASSERT(0); 7881 break; 7882 } 7883 } 7884 7885 if (*flags & CPU_DTRACE_DROP) 7886 continue; 7887 7888 if (*flags & CPU_DTRACE_FAULT) { 7889 int ndx; 7890 dtrace_action_t *err; 7891 7892 buf->dtb_errors++; 7893 7894 if (probe->dtpr_id == dtrace_probeid_error) { 7895 /* 7896 * There's nothing we can do -- we had an 7897 * error on the error probe. We bump an 7898 * error counter to at least indicate that 7899 * this condition happened. 7900 */ 7901 dtrace_error(&state->dts_dblerrors); 7902 continue; 7903 } 7904 7905 if (vtime) { 7906 /* 7907 * Before recursing on dtrace_probe(), we 7908 * need to explicitly clear out our start 7909 * time to prevent it from being accumulated 7910 * into t_dtrace_vtime. 7911 */ 7912 curthread->t_dtrace_start = 0; 7913 } 7914 7915 /* 7916 * Iterate over the actions to figure out which action 7917 * we were processing when we experienced the error. 7918 * Note that act points _past_ the faulting action; if 7919 * act is ecb->dte_action, the fault was in the 7920 * predicate, if it's ecb->dte_action->dta_next it's 7921 * in action #1, and so on. 7922 */ 7923 for (err = ecb->dte_action, ndx = 0; 7924 err != act; err = err->dta_next, ndx++) 7925 continue; 7926 7927 dtrace_probe_error(state, ecb->dte_epid, ndx, 7928 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 7929 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 7930 cpu_core[cpuid].cpuc_dtrace_illval); 7931 7932 continue; 7933 } 7934 7935 if (!committed) 7936 buf->dtb_offset = offs + ecb->dte_size; 7937 } 7938 7939 if (vtime) 7940 curthread->t_dtrace_start = dtrace_gethrtime(); 7941 7942 dtrace_interrupt_enable(cookie); 7943 } 7944 7945 /* 7946 * DTrace Probe Hashing Functions 7947 * 7948 * The functions in this section (and indeed, the functions in remaining 7949 * sections) are not _called_ from probe context. (Any exceptions to this are 7950 * marked with a "Note:".) Rather, they are called from elsewhere in the 7951 * DTrace framework to look-up probes in, add probes to and remove probes from 7952 * the DTrace probe hashes. (Each probe is hashed by each element of the 7953 * probe tuple -- allowing for fast lookups, regardless of what was 7954 * specified.) 7955 */ 7956 static uint_t 7957 dtrace_hash_str(const char *p) 7958 { 7959 unsigned int g; 7960 uint_t hval = 0; 7961 7962 while (*p) { 7963 hval = (hval << 4) + *p++; 7964 if ((g = (hval & 0xf0000000)) != 0) 7965 hval ^= g >> 24; 7966 hval &= ~g; 7967 } 7968 return (hval); 7969 } 7970 7971 static dtrace_hash_t * 7972 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 7973 { 7974 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 7975 7976 hash->dth_stroffs = stroffs; 7977 hash->dth_nextoffs = nextoffs; 7978 hash->dth_prevoffs = prevoffs; 7979 7980 hash->dth_size = 1; 7981 hash->dth_mask = hash->dth_size - 1; 7982 7983 hash->dth_tab = kmem_zalloc(hash->dth_size * 7984 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 7985 7986 return (hash); 7987 } 7988 7989 static void 7990 dtrace_hash_destroy(dtrace_hash_t *hash) 7991 { 7992 #ifdef DEBUG 7993 int i; 7994 7995 for (i = 0; i < hash->dth_size; i++) 7996 ASSERT(hash->dth_tab[i] == NULL); 7997 #endif 7998 7999 kmem_free(hash->dth_tab, 8000 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 8001 kmem_free(hash, sizeof (dtrace_hash_t)); 8002 } 8003 8004 static void 8005 dtrace_hash_resize(dtrace_hash_t *hash) 8006 { 8007 int size = hash->dth_size, i, ndx; 8008 int new_size = hash->dth_size << 1; 8009 int new_mask = new_size - 1; 8010 dtrace_hashbucket_t **new_tab, *bucket, *next; 8011 8012 ASSERT((new_size & new_mask) == 0); 8013 8014 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 8015 8016 for (i = 0; i < size; i++) { 8017 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 8018 dtrace_probe_t *probe = bucket->dthb_chain; 8019 8020 ASSERT(probe != NULL); 8021 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 8022 8023 next = bucket->dthb_next; 8024 bucket->dthb_next = new_tab[ndx]; 8025 new_tab[ndx] = bucket; 8026 } 8027 } 8028 8029 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 8030 hash->dth_tab = new_tab; 8031 hash->dth_size = new_size; 8032 hash->dth_mask = new_mask; 8033 } 8034 8035 static void 8036 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 8037 { 8038 int hashval = DTRACE_HASHSTR(hash, new); 8039 int ndx = hashval & hash->dth_mask; 8040 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8041 dtrace_probe_t **nextp, **prevp; 8042 8043 for (; bucket != NULL; bucket = bucket->dthb_next) { 8044 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 8045 goto add; 8046 } 8047 8048 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 8049 dtrace_hash_resize(hash); 8050 dtrace_hash_add(hash, new); 8051 return; 8052 } 8053 8054 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 8055 bucket->dthb_next = hash->dth_tab[ndx]; 8056 hash->dth_tab[ndx] = bucket; 8057 hash->dth_nbuckets++; 8058 8059 add: 8060 nextp = DTRACE_HASHNEXT(hash, new); 8061 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 8062 *nextp = bucket->dthb_chain; 8063 8064 if (bucket->dthb_chain != NULL) { 8065 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 8066 ASSERT(*prevp == NULL); 8067 *prevp = new; 8068 } 8069 8070 bucket->dthb_chain = new; 8071 bucket->dthb_len++; 8072 } 8073 8074 static dtrace_probe_t * 8075 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 8076 { 8077 int hashval = DTRACE_HASHSTR(hash, template); 8078 int ndx = hashval & hash->dth_mask; 8079 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8080 8081 for (; bucket != NULL; bucket = bucket->dthb_next) { 8082 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 8083 return (bucket->dthb_chain); 8084 } 8085 8086 return (NULL); 8087 } 8088 8089 static int 8090 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 8091 { 8092 int hashval = DTRACE_HASHSTR(hash, template); 8093 int ndx = hashval & hash->dth_mask; 8094 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8095 8096 for (; bucket != NULL; bucket = bucket->dthb_next) { 8097 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 8098 return (bucket->dthb_len); 8099 } 8100 8101 return (0); 8102 } 8103 8104 static void 8105 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 8106 { 8107 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 8108 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8109 8110 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 8111 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 8112 8113 /* 8114 * Find the bucket that we're removing this probe from. 8115 */ 8116 for (; bucket != NULL; bucket = bucket->dthb_next) { 8117 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 8118 break; 8119 } 8120 8121 ASSERT(bucket != NULL); 8122 8123 if (*prevp == NULL) { 8124 if (*nextp == NULL) { 8125 /* 8126 * The removed probe was the only probe on this 8127 * bucket; we need to remove the bucket. 8128 */ 8129 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 8130 8131 ASSERT(bucket->dthb_chain == probe); 8132 ASSERT(b != NULL); 8133 8134 if (b == bucket) { 8135 hash->dth_tab[ndx] = bucket->dthb_next; 8136 } else { 8137 while (b->dthb_next != bucket) 8138 b = b->dthb_next; 8139 b->dthb_next = bucket->dthb_next; 8140 } 8141 8142 ASSERT(hash->dth_nbuckets > 0); 8143 hash->dth_nbuckets--; 8144 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 8145 return; 8146 } 8147 8148 bucket->dthb_chain = *nextp; 8149 } else { 8150 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 8151 } 8152 8153 if (*nextp != NULL) 8154 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 8155 } 8156 8157 /* 8158 * DTrace Utility Functions 8159 * 8160 * These are random utility functions that are _not_ called from probe context. 8161 */ 8162 static int 8163 dtrace_badattr(const dtrace_attribute_t *a) 8164 { 8165 return (a->dtat_name > DTRACE_STABILITY_MAX || 8166 a->dtat_data > DTRACE_STABILITY_MAX || 8167 a->dtat_class > DTRACE_CLASS_MAX); 8168 } 8169 8170 /* 8171 * Return a duplicate copy of a string. If the specified string is NULL, 8172 * this function returns a zero-length string. 8173 */ 8174 static char * 8175 dtrace_strdup(const char *str) 8176 { 8177 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 8178 8179 if (str != NULL) 8180 (void) strcpy(new, str); 8181 8182 return (new); 8183 } 8184 8185 #define DTRACE_ISALPHA(c) \ 8186 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 8187 8188 static int 8189 dtrace_badname(const char *s) 8190 { 8191 char c; 8192 8193 if (s == NULL || (c = *s++) == '\0') 8194 return (0); 8195 8196 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 8197 return (1); 8198 8199 while ((c = *s++) != '\0') { 8200 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 8201 c != '-' && c != '_' && c != '.' && c != '`') 8202 return (1); 8203 } 8204 8205 return (0); 8206 } 8207 8208 static void 8209 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 8210 { 8211 uint32_t priv; 8212 8213 #ifdef illumos 8214 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 8215 /* 8216 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 8217 */ 8218 priv = DTRACE_PRIV_ALL; 8219 } else { 8220 *uidp = crgetuid(cr); 8221 *zoneidp = crgetzoneid(cr); 8222 8223 priv = 0; 8224 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 8225 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 8226 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 8227 priv |= DTRACE_PRIV_USER; 8228 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 8229 priv |= DTRACE_PRIV_PROC; 8230 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 8231 priv |= DTRACE_PRIV_OWNER; 8232 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 8233 priv |= DTRACE_PRIV_ZONEOWNER; 8234 } 8235 #else 8236 priv = DTRACE_PRIV_ALL; 8237 #endif 8238 8239 *privp = priv; 8240 } 8241 8242 #ifdef DTRACE_ERRDEBUG 8243 static void 8244 dtrace_errdebug(const char *str) 8245 { 8246 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 8247 int occupied = 0; 8248 8249 mutex_enter(&dtrace_errlock); 8250 dtrace_errlast = str; 8251 dtrace_errthread = curthread; 8252 8253 while (occupied++ < DTRACE_ERRHASHSZ) { 8254 if (dtrace_errhash[hval].dter_msg == str) { 8255 dtrace_errhash[hval].dter_count++; 8256 goto out; 8257 } 8258 8259 if (dtrace_errhash[hval].dter_msg != NULL) { 8260 hval = (hval + 1) % DTRACE_ERRHASHSZ; 8261 continue; 8262 } 8263 8264 dtrace_errhash[hval].dter_msg = str; 8265 dtrace_errhash[hval].dter_count = 1; 8266 goto out; 8267 } 8268 8269 panic("dtrace: undersized error hash"); 8270 out: 8271 mutex_exit(&dtrace_errlock); 8272 } 8273 #endif 8274 8275 /* 8276 * DTrace Matching Functions 8277 * 8278 * These functions are used to match groups of probes, given some elements of 8279 * a probe tuple, or some globbed expressions for elements of a probe tuple. 8280 */ 8281 static int 8282 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 8283 zoneid_t zoneid) 8284 { 8285 if (priv != DTRACE_PRIV_ALL) { 8286 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 8287 uint32_t match = priv & ppriv; 8288 8289 /* 8290 * No PRIV_DTRACE_* privileges... 8291 */ 8292 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 8293 DTRACE_PRIV_KERNEL)) == 0) 8294 return (0); 8295 8296 /* 8297 * No matching bits, but there were bits to match... 8298 */ 8299 if (match == 0 && ppriv != 0) 8300 return (0); 8301 8302 /* 8303 * Need to have permissions to the process, but don't... 8304 */ 8305 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 8306 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 8307 return (0); 8308 } 8309 8310 /* 8311 * Need to be in the same zone unless we possess the 8312 * privilege to examine all zones. 8313 */ 8314 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 8315 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 8316 return (0); 8317 } 8318 } 8319 8320 return (1); 8321 } 8322 8323 /* 8324 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 8325 * consists of input pattern strings and an ops-vector to evaluate them. 8326 * This function returns >0 for match, 0 for no match, and <0 for error. 8327 */ 8328 static int 8329 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 8330 uint32_t priv, uid_t uid, zoneid_t zoneid) 8331 { 8332 dtrace_provider_t *pvp = prp->dtpr_provider; 8333 int rv; 8334 8335 if (pvp->dtpv_defunct) 8336 return (0); 8337 8338 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 8339 return (rv); 8340 8341 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 8342 return (rv); 8343 8344 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 8345 return (rv); 8346 8347 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 8348 return (rv); 8349 8350 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 8351 return (0); 8352 8353 return (rv); 8354 } 8355 8356 /* 8357 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 8358 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 8359 * libc's version, the kernel version only applies to 8-bit ASCII strings. 8360 * In addition, all of the recursion cases except for '*' matching have been 8361 * unwound. For '*', we still implement recursive evaluation, but a depth 8362 * counter is maintained and matching is aborted if we recurse too deep. 8363 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 8364 */ 8365 static int 8366 dtrace_match_glob(const char *s, const char *p, int depth) 8367 { 8368 const char *olds; 8369 char s1, c; 8370 int gs; 8371 8372 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 8373 return (-1); 8374 8375 if (s == NULL) 8376 s = ""; /* treat NULL as empty string */ 8377 8378 top: 8379 olds = s; 8380 s1 = *s++; 8381 8382 if (p == NULL) 8383 return (0); 8384 8385 if ((c = *p++) == '\0') 8386 return (s1 == '\0'); 8387 8388 switch (c) { 8389 case '[': { 8390 int ok = 0, notflag = 0; 8391 char lc = '\0'; 8392 8393 if (s1 == '\0') 8394 return (0); 8395 8396 if (*p == '!') { 8397 notflag = 1; 8398 p++; 8399 } 8400 8401 if ((c = *p++) == '\0') 8402 return (0); 8403 8404 do { 8405 if (c == '-' && lc != '\0' && *p != ']') { 8406 if ((c = *p++) == '\0') 8407 return (0); 8408 if (c == '\\' && (c = *p++) == '\0') 8409 return (0); 8410 8411 if (notflag) { 8412 if (s1 < lc || s1 > c) 8413 ok++; 8414 else 8415 return (0); 8416 } else if (lc <= s1 && s1 <= c) 8417 ok++; 8418 8419 } else if (c == '\\' && (c = *p++) == '\0') 8420 return (0); 8421 8422 lc = c; /* save left-hand 'c' for next iteration */ 8423 8424 if (notflag) { 8425 if (s1 != c) 8426 ok++; 8427 else 8428 return (0); 8429 } else if (s1 == c) 8430 ok++; 8431 8432 if ((c = *p++) == '\0') 8433 return (0); 8434 8435 } while (c != ']'); 8436 8437 if (ok) 8438 goto top; 8439 8440 return (0); 8441 } 8442 8443 case '\\': 8444 if ((c = *p++) == '\0') 8445 return (0); 8446 /*FALLTHRU*/ 8447 8448 default: 8449 if (c != s1) 8450 return (0); 8451 /*FALLTHRU*/ 8452 8453 case '?': 8454 if (s1 != '\0') 8455 goto top; 8456 return (0); 8457 8458 case '*': 8459 while (*p == '*') 8460 p++; /* consecutive *'s are identical to a single one */ 8461 8462 if (*p == '\0') 8463 return (1); 8464 8465 for (s = olds; *s != '\0'; s++) { 8466 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 8467 return (gs); 8468 } 8469 8470 return (0); 8471 } 8472 } 8473 8474 /*ARGSUSED*/ 8475 static int 8476 dtrace_match_string(const char *s, const char *p, int depth) 8477 { 8478 return (s != NULL && strcmp(s, p) == 0); 8479 } 8480 8481 /*ARGSUSED*/ 8482 static int 8483 dtrace_match_nul(const char *s, const char *p, int depth) 8484 { 8485 return (1); /* always match the empty pattern */ 8486 } 8487 8488 /*ARGSUSED*/ 8489 static int 8490 dtrace_match_nonzero(const char *s, const char *p, int depth) 8491 { 8492 return (s != NULL && s[0] != '\0'); 8493 } 8494 8495 static int 8496 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 8497 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 8498 { 8499 dtrace_probe_t template, *probe; 8500 dtrace_hash_t *hash = NULL; 8501 int len, best = INT_MAX, nmatched = 0; 8502 dtrace_id_t i; 8503 8504 ASSERT(MUTEX_HELD(&dtrace_lock)); 8505 8506 /* 8507 * If the probe ID is specified in the key, just lookup by ID and 8508 * invoke the match callback once if a matching probe is found. 8509 */ 8510 if (pkp->dtpk_id != DTRACE_IDNONE) { 8511 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 8512 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 8513 (void) (*matched)(probe, arg); 8514 nmatched++; 8515 } 8516 return (nmatched); 8517 } 8518 8519 template.dtpr_mod = (char *)pkp->dtpk_mod; 8520 template.dtpr_func = (char *)pkp->dtpk_func; 8521 template.dtpr_name = (char *)pkp->dtpk_name; 8522 8523 /* 8524 * We want to find the most distinct of the module name, function 8525 * name, and name. So for each one that is not a glob pattern or 8526 * empty string, we perform a lookup in the corresponding hash and 8527 * use the hash table with the fewest collisions to do our search. 8528 */ 8529 if (pkp->dtpk_mmatch == &dtrace_match_string && 8530 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 8531 best = len; 8532 hash = dtrace_bymod; 8533 } 8534 8535 if (pkp->dtpk_fmatch == &dtrace_match_string && 8536 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 8537 best = len; 8538 hash = dtrace_byfunc; 8539 } 8540 8541 if (pkp->dtpk_nmatch == &dtrace_match_string && 8542 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 8543 best = len; 8544 hash = dtrace_byname; 8545 } 8546 8547 /* 8548 * If we did not select a hash table, iterate over every probe and 8549 * invoke our callback for each one that matches our input probe key. 8550 */ 8551 if (hash == NULL) { 8552 for (i = 0; i < dtrace_nprobes; i++) { 8553 if ((probe = dtrace_probes[i]) == NULL || 8554 dtrace_match_probe(probe, pkp, priv, uid, 8555 zoneid) <= 0) 8556 continue; 8557 8558 nmatched++; 8559 8560 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8561 break; 8562 } 8563 8564 return (nmatched); 8565 } 8566 8567 /* 8568 * If we selected a hash table, iterate over each probe of the same key 8569 * name and invoke the callback for every probe that matches the other 8570 * attributes of our input probe key. 8571 */ 8572 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 8573 probe = *(DTRACE_HASHNEXT(hash, probe))) { 8574 8575 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 8576 continue; 8577 8578 nmatched++; 8579 8580 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8581 break; 8582 } 8583 8584 return (nmatched); 8585 } 8586 8587 /* 8588 * Return the function pointer dtrace_probecmp() should use to compare the 8589 * specified pattern with a string. For NULL or empty patterns, we select 8590 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 8591 * For non-empty non-glob strings, we use dtrace_match_string(). 8592 */ 8593 static dtrace_probekey_f * 8594 dtrace_probekey_func(const char *p) 8595 { 8596 char c; 8597 8598 if (p == NULL || *p == '\0') 8599 return (&dtrace_match_nul); 8600 8601 while ((c = *p++) != '\0') { 8602 if (c == '[' || c == '?' || c == '*' || c == '\\') 8603 return (&dtrace_match_glob); 8604 } 8605 8606 return (&dtrace_match_string); 8607 } 8608 8609 /* 8610 * Build a probe comparison key for use with dtrace_match_probe() from the 8611 * given probe description. By convention, a null key only matches anchored 8612 * probes: if each field is the empty string, reset dtpk_fmatch to 8613 * dtrace_match_nonzero(). 8614 */ 8615 static void 8616 dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 8617 { 8618 pkp->dtpk_prov = pdp->dtpd_provider; 8619 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 8620 8621 pkp->dtpk_mod = pdp->dtpd_mod; 8622 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 8623 8624 pkp->dtpk_func = pdp->dtpd_func; 8625 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 8626 8627 pkp->dtpk_name = pdp->dtpd_name; 8628 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 8629 8630 pkp->dtpk_id = pdp->dtpd_id; 8631 8632 if (pkp->dtpk_id == DTRACE_IDNONE && 8633 pkp->dtpk_pmatch == &dtrace_match_nul && 8634 pkp->dtpk_mmatch == &dtrace_match_nul && 8635 pkp->dtpk_fmatch == &dtrace_match_nul && 8636 pkp->dtpk_nmatch == &dtrace_match_nul) 8637 pkp->dtpk_fmatch = &dtrace_match_nonzero; 8638 } 8639 8640 /* 8641 * DTrace Provider-to-Framework API Functions 8642 * 8643 * These functions implement much of the Provider-to-Framework API, as 8644 * described in <sys/dtrace.h>. The parts of the API not in this section are 8645 * the functions in the API for probe management (found below), and 8646 * dtrace_probe() itself (found above). 8647 */ 8648 8649 /* 8650 * Register the calling provider with the DTrace framework. This should 8651 * generally be called by DTrace providers in their attach(9E) entry point. 8652 */ 8653 int 8654 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 8655 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 8656 { 8657 dtrace_provider_t *provider; 8658 8659 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 8660 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8661 "arguments", name ? name : "<NULL>"); 8662 return (EINVAL); 8663 } 8664 8665 if (name[0] == '\0' || dtrace_badname(name)) { 8666 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8667 "provider name", name); 8668 return (EINVAL); 8669 } 8670 8671 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 8672 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 8673 pops->dtps_destroy == NULL || 8674 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 8675 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8676 "provider ops", name); 8677 return (EINVAL); 8678 } 8679 8680 if (dtrace_badattr(&pap->dtpa_provider) || 8681 dtrace_badattr(&pap->dtpa_mod) || 8682 dtrace_badattr(&pap->dtpa_func) || 8683 dtrace_badattr(&pap->dtpa_name) || 8684 dtrace_badattr(&pap->dtpa_args)) { 8685 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8686 "provider attributes", name); 8687 return (EINVAL); 8688 } 8689 8690 if (priv & ~DTRACE_PRIV_ALL) { 8691 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8692 "privilege attributes", name); 8693 return (EINVAL); 8694 } 8695 8696 if ((priv & DTRACE_PRIV_KERNEL) && 8697 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 8698 pops->dtps_usermode == NULL) { 8699 cmn_err(CE_WARN, "failed to register provider '%s': need " 8700 "dtps_usermode() op for given privilege attributes", name); 8701 return (EINVAL); 8702 } 8703 8704 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 8705 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8706 (void) strcpy(provider->dtpv_name, name); 8707 8708 provider->dtpv_attr = *pap; 8709 provider->dtpv_priv.dtpp_flags = priv; 8710 if (cr != NULL) { 8711 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 8712 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 8713 } 8714 provider->dtpv_pops = *pops; 8715 8716 if (pops->dtps_provide == NULL) { 8717 ASSERT(pops->dtps_provide_module != NULL); 8718 provider->dtpv_pops.dtps_provide = 8719 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 8720 } 8721 8722 if (pops->dtps_provide_module == NULL) { 8723 ASSERT(pops->dtps_provide != NULL); 8724 provider->dtpv_pops.dtps_provide_module = 8725 (void (*)(void *, modctl_t *))dtrace_nullop; 8726 } 8727 8728 if (pops->dtps_suspend == NULL) { 8729 ASSERT(pops->dtps_resume == NULL); 8730 provider->dtpv_pops.dtps_suspend = 8731 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8732 provider->dtpv_pops.dtps_resume = 8733 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8734 } 8735 8736 provider->dtpv_arg = arg; 8737 *idp = (dtrace_provider_id_t)provider; 8738 8739 if (pops == &dtrace_provider_ops) { 8740 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8741 ASSERT(MUTEX_HELD(&dtrace_lock)); 8742 ASSERT(dtrace_anon.dta_enabling == NULL); 8743 8744 /* 8745 * We make sure that the DTrace provider is at the head of 8746 * the provider chain. 8747 */ 8748 provider->dtpv_next = dtrace_provider; 8749 dtrace_provider = provider; 8750 return (0); 8751 } 8752 8753 mutex_enter(&dtrace_provider_lock); 8754 mutex_enter(&dtrace_lock); 8755 8756 /* 8757 * If there is at least one provider registered, we'll add this 8758 * provider after the first provider. 8759 */ 8760 if (dtrace_provider != NULL) { 8761 provider->dtpv_next = dtrace_provider->dtpv_next; 8762 dtrace_provider->dtpv_next = provider; 8763 } else { 8764 dtrace_provider = provider; 8765 } 8766 8767 if (dtrace_retained != NULL) { 8768 dtrace_enabling_provide(provider); 8769 8770 /* 8771 * Now we need to call dtrace_enabling_matchall() -- which 8772 * will acquire cpu_lock and dtrace_lock. We therefore need 8773 * to drop all of our locks before calling into it... 8774 */ 8775 mutex_exit(&dtrace_lock); 8776 mutex_exit(&dtrace_provider_lock); 8777 dtrace_enabling_matchall(); 8778 8779 return (0); 8780 } 8781 8782 mutex_exit(&dtrace_lock); 8783 mutex_exit(&dtrace_provider_lock); 8784 8785 return (0); 8786 } 8787 8788 /* 8789 * Unregister the specified provider from the DTrace framework. This should 8790 * generally be called by DTrace providers in their detach(9E) entry point. 8791 */ 8792 int 8793 dtrace_unregister(dtrace_provider_id_t id) 8794 { 8795 dtrace_provider_t *old = (dtrace_provider_t *)id; 8796 dtrace_provider_t *prev = NULL; 8797 int i, self = 0, noreap = 0; 8798 dtrace_probe_t *probe, *first = NULL; 8799 8800 if (old->dtpv_pops.dtps_enable == 8801 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 8802 /* 8803 * If DTrace itself is the provider, we're called with locks 8804 * already held. 8805 */ 8806 ASSERT(old == dtrace_provider); 8807 #ifdef illumos 8808 ASSERT(dtrace_devi != NULL); 8809 #endif 8810 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8811 ASSERT(MUTEX_HELD(&dtrace_lock)); 8812 self = 1; 8813 8814 if (dtrace_provider->dtpv_next != NULL) { 8815 /* 8816 * There's another provider here; return failure. 8817 */ 8818 return (EBUSY); 8819 } 8820 } else { 8821 mutex_enter(&dtrace_provider_lock); 8822 #ifdef illumos 8823 mutex_enter(&mod_lock); 8824 #endif 8825 mutex_enter(&dtrace_lock); 8826 } 8827 8828 /* 8829 * If anyone has /dev/dtrace open, or if there are anonymous enabled 8830 * probes, we refuse to let providers slither away, unless this 8831 * provider has already been explicitly invalidated. 8832 */ 8833 if (!old->dtpv_defunct && 8834 (dtrace_opens || (dtrace_anon.dta_state != NULL && 8835 dtrace_anon.dta_state->dts_necbs > 0))) { 8836 if (!self) { 8837 mutex_exit(&dtrace_lock); 8838 #ifdef illumos 8839 mutex_exit(&mod_lock); 8840 #endif 8841 mutex_exit(&dtrace_provider_lock); 8842 } 8843 return (EBUSY); 8844 } 8845 8846 /* 8847 * Attempt to destroy the probes associated with this provider. 8848 */ 8849 for (i = 0; i < dtrace_nprobes; i++) { 8850 if ((probe = dtrace_probes[i]) == NULL) 8851 continue; 8852 8853 if (probe->dtpr_provider != old) 8854 continue; 8855 8856 if (probe->dtpr_ecb == NULL) 8857 continue; 8858 8859 /* 8860 * If we are trying to unregister a defunct provider, and the 8861 * provider was made defunct within the interval dictated by 8862 * dtrace_unregister_defunct_reap, we'll (asynchronously) 8863 * attempt to reap our enablings. To denote that the provider 8864 * should reattempt to unregister itself at some point in the 8865 * future, we will return a differentiable error code (EAGAIN 8866 * instead of EBUSY) in this case. 8867 */ 8868 if (dtrace_gethrtime() - old->dtpv_defunct > 8869 dtrace_unregister_defunct_reap) 8870 noreap = 1; 8871 8872 if (!self) { 8873 mutex_exit(&dtrace_lock); 8874 #ifdef illumos 8875 mutex_exit(&mod_lock); 8876 #endif 8877 mutex_exit(&dtrace_provider_lock); 8878 } 8879 8880 if (noreap) 8881 return (EBUSY); 8882 8883 (void) taskq_dispatch(dtrace_taskq, 8884 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 8885 8886 return (EAGAIN); 8887 } 8888 8889 /* 8890 * All of the probes for this provider are disabled; we can safely 8891 * remove all of them from their hash chains and from the probe array. 8892 */ 8893 for (i = 0; i < dtrace_nprobes; i++) { 8894 if ((probe = dtrace_probes[i]) == NULL) 8895 continue; 8896 8897 if (probe->dtpr_provider != old) 8898 continue; 8899 8900 dtrace_probes[i] = NULL; 8901 8902 dtrace_hash_remove(dtrace_bymod, probe); 8903 dtrace_hash_remove(dtrace_byfunc, probe); 8904 dtrace_hash_remove(dtrace_byname, probe); 8905 8906 if (first == NULL) { 8907 first = probe; 8908 probe->dtpr_nextmod = NULL; 8909 } else { 8910 probe->dtpr_nextmod = first; 8911 first = probe; 8912 } 8913 } 8914 8915 /* 8916 * The provider's probes have been removed from the hash chains and 8917 * from the probe array. Now issue a dtrace_sync() to be sure that 8918 * everyone has cleared out from any probe array processing. 8919 */ 8920 dtrace_sync(); 8921 8922 for (probe = first; probe != NULL; probe = first) { 8923 first = probe->dtpr_nextmod; 8924 8925 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 8926 probe->dtpr_arg); 8927 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8928 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8929 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8930 #ifdef illumos 8931 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 8932 #else 8933 free_unr(dtrace_arena, probe->dtpr_id); 8934 #endif 8935 kmem_free(probe, sizeof (dtrace_probe_t)); 8936 } 8937 8938 if ((prev = dtrace_provider) == old) { 8939 #ifdef illumos 8940 ASSERT(self || dtrace_devi == NULL); 8941 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 8942 #endif 8943 dtrace_provider = old->dtpv_next; 8944 } else { 8945 while (prev != NULL && prev->dtpv_next != old) 8946 prev = prev->dtpv_next; 8947 8948 if (prev == NULL) { 8949 panic("attempt to unregister non-existent " 8950 "dtrace provider %p\n", (void *)id); 8951 } 8952 8953 prev->dtpv_next = old->dtpv_next; 8954 } 8955 8956 if (!self) { 8957 mutex_exit(&dtrace_lock); 8958 #ifdef illumos 8959 mutex_exit(&mod_lock); 8960 #endif 8961 mutex_exit(&dtrace_provider_lock); 8962 } 8963 8964 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 8965 kmem_free(old, sizeof (dtrace_provider_t)); 8966 8967 return (0); 8968 } 8969 8970 /* 8971 * Invalidate the specified provider. All subsequent probe lookups for the 8972 * specified provider will fail, but its probes will not be removed. 8973 */ 8974 void 8975 dtrace_invalidate(dtrace_provider_id_t id) 8976 { 8977 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 8978 8979 ASSERT(pvp->dtpv_pops.dtps_enable != 8980 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8981 8982 mutex_enter(&dtrace_provider_lock); 8983 mutex_enter(&dtrace_lock); 8984 8985 pvp->dtpv_defunct = dtrace_gethrtime(); 8986 8987 mutex_exit(&dtrace_lock); 8988 mutex_exit(&dtrace_provider_lock); 8989 } 8990 8991 /* 8992 * Indicate whether or not DTrace has attached. 8993 */ 8994 int 8995 dtrace_attached(void) 8996 { 8997 /* 8998 * dtrace_provider will be non-NULL iff the DTrace driver has 8999 * attached. (It's non-NULL because DTrace is always itself a 9000 * provider.) 9001 */ 9002 return (dtrace_provider != NULL); 9003 } 9004 9005 /* 9006 * Remove all the unenabled probes for the given provider. This function is 9007 * not unlike dtrace_unregister(), except that it doesn't remove the provider 9008 * -- just as many of its associated probes as it can. 9009 */ 9010 int 9011 dtrace_condense(dtrace_provider_id_t id) 9012 { 9013 dtrace_provider_t *prov = (dtrace_provider_t *)id; 9014 int i; 9015 dtrace_probe_t *probe; 9016 9017 /* 9018 * Make sure this isn't the dtrace provider itself. 9019 */ 9020 ASSERT(prov->dtpv_pops.dtps_enable != 9021 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 9022 9023 mutex_enter(&dtrace_provider_lock); 9024 mutex_enter(&dtrace_lock); 9025 9026 /* 9027 * Attempt to destroy the probes associated with this provider. 9028 */ 9029 for (i = 0; i < dtrace_nprobes; i++) { 9030 if ((probe = dtrace_probes[i]) == NULL) 9031 continue; 9032 9033 if (probe->dtpr_provider != prov) 9034 continue; 9035 9036 if (probe->dtpr_ecb != NULL) 9037 continue; 9038 9039 dtrace_probes[i] = NULL; 9040 9041 dtrace_hash_remove(dtrace_bymod, probe); 9042 dtrace_hash_remove(dtrace_byfunc, probe); 9043 dtrace_hash_remove(dtrace_byname, probe); 9044 9045 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 9046 probe->dtpr_arg); 9047 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 9048 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 9049 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 9050 kmem_free(probe, sizeof (dtrace_probe_t)); 9051 #ifdef illumos 9052 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 9053 #else 9054 free_unr(dtrace_arena, i + 1); 9055 #endif 9056 } 9057 9058 mutex_exit(&dtrace_lock); 9059 mutex_exit(&dtrace_provider_lock); 9060 9061 return (0); 9062 } 9063 9064 /* 9065 * DTrace Probe Management Functions 9066 * 9067 * The functions in this section perform the DTrace probe management, 9068 * including functions to create probes, look-up probes, and call into the 9069 * providers to request that probes be provided. Some of these functions are 9070 * in the Provider-to-Framework API; these functions can be identified by the 9071 * fact that they are not declared "static". 9072 */ 9073 9074 /* 9075 * Create a probe with the specified module name, function name, and name. 9076 */ 9077 dtrace_id_t 9078 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 9079 const char *func, const char *name, int aframes, void *arg) 9080 { 9081 dtrace_probe_t *probe, **probes; 9082 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 9083 dtrace_id_t id; 9084 9085 if (provider == dtrace_provider) { 9086 ASSERT(MUTEX_HELD(&dtrace_lock)); 9087 } else { 9088 mutex_enter(&dtrace_lock); 9089 } 9090 9091 #ifdef illumos 9092 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 9093 VM_BESTFIT | VM_SLEEP); 9094 #else 9095 id = alloc_unr(dtrace_arena); 9096 #endif 9097 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 9098 9099 probe->dtpr_id = id; 9100 probe->dtpr_gen = dtrace_probegen++; 9101 probe->dtpr_mod = dtrace_strdup(mod); 9102 probe->dtpr_func = dtrace_strdup(func); 9103 probe->dtpr_name = dtrace_strdup(name); 9104 probe->dtpr_arg = arg; 9105 probe->dtpr_aframes = aframes; 9106 probe->dtpr_provider = provider; 9107 9108 dtrace_hash_add(dtrace_bymod, probe); 9109 dtrace_hash_add(dtrace_byfunc, probe); 9110 dtrace_hash_add(dtrace_byname, probe); 9111 9112 if (id - 1 >= dtrace_nprobes) { 9113 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 9114 size_t nsize = osize << 1; 9115 9116 if (nsize == 0) { 9117 ASSERT(osize == 0); 9118 ASSERT(dtrace_probes == NULL); 9119 nsize = sizeof (dtrace_probe_t *); 9120 } 9121 9122 probes = kmem_zalloc(nsize, KM_SLEEP); 9123 9124 if (dtrace_probes == NULL) { 9125 ASSERT(osize == 0); 9126 dtrace_probes = probes; 9127 dtrace_nprobes = 1; 9128 } else { 9129 dtrace_probe_t **oprobes = dtrace_probes; 9130 9131 bcopy(oprobes, probes, osize); 9132 dtrace_membar_producer(); 9133 dtrace_probes = probes; 9134 9135 dtrace_sync(); 9136 9137 /* 9138 * All CPUs are now seeing the new probes array; we can 9139 * safely free the old array. 9140 */ 9141 kmem_free(oprobes, osize); 9142 dtrace_nprobes <<= 1; 9143 } 9144 9145 ASSERT(id - 1 < dtrace_nprobes); 9146 } 9147 9148 ASSERT(dtrace_probes[id - 1] == NULL); 9149 dtrace_probes[id - 1] = probe; 9150 9151 if (provider != dtrace_provider) 9152 mutex_exit(&dtrace_lock); 9153 9154 return (id); 9155 } 9156 9157 static dtrace_probe_t * 9158 dtrace_probe_lookup_id(dtrace_id_t id) 9159 { 9160 ASSERT(MUTEX_HELD(&dtrace_lock)); 9161 9162 if (id == 0 || id > dtrace_nprobes) 9163 return (NULL); 9164 9165 return (dtrace_probes[id - 1]); 9166 } 9167 9168 static int 9169 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 9170 { 9171 *((dtrace_id_t *)arg) = probe->dtpr_id; 9172 9173 return (DTRACE_MATCH_DONE); 9174 } 9175 9176 /* 9177 * Look up a probe based on provider and one or more of module name, function 9178 * name and probe name. 9179 */ 9180 dtrace_id_t 9181 dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 9182 char *func, char *name) 9183 { 9184 dtrace_probekey_t pkey; 9185 dtrace_id_t id; 9186 int match; 9187 9188 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 9189 pkey.dtpk_pmatch = &dtrace_match_string; 9190 pkey.dtpk_mod = mod; 9191 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 9192 pkey.dtpk_func = func; 9193 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 9194 pkey.dtpk_name = name; 9195 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 9196 pkey.dtpk_id = DTRACE_IDNONE; 9197 9198 mutex_enter(&dtrace_lock); 9199 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 9200 dtrace_probe_lookup_match, &id); 9201 mutex_exit(&dtrace_lock); 9202 9203 ASSERT(match == 1 || match == 0); 9204 return (match ? id : 0); 9205 } 9206 9207 /* 9208 * Returns the probe argument associated with the specified probe. 9209 */ 9210 void * 9211 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 9212 { 9213 dtrace_probe_t *probe; 9214 void *rval = NULL; 9215 9216 mutex_enter(&dtrace_lock); 9217 9218 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 9219 probe->dtpr_provider == (dtrace_provider_t *)id) 9220 rval = probe->dtpr_arg; 9221 9222 mutex_exit(&dtrace_lock); 9223 9224 return (rval); 9225 } 9226 9227 /* 9228 * Copy a probe into a probe description. 9229 */ 9230 static void 9231 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 9232 { 9233 bzero(pdp, sizeof (dtrace_probedesc_t)); 9234 pdp->dtpd_id = prp->dtpr_id; 9235 9236 (void) strncpy(pdp->dtpd_provider, 9237 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 9238 9239 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 9240 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 9241 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 9242 } 9243 9244 /* 9245 * Called to indicate that a probe -- or probes -- should be provided by a 9246 * specfied provider. If the specified description is NULL, the provider will 9247 * be told to provide all of its probes. (This is done whenever a new 9248 * consumer comes along, or whenever a retained enabling is to be matched.) If 9249 * the specified description is non-NULL, the provider is given the 9250 * opportunity to dynamically provide the specified probe, allowing providers 9251 * to support the creation of probes on-the-fly. (So-called _autocreated_ 9252 * probes.) If the provider is NULL, the operations will be applied to all 9253 * providers; if the provider is non-NULL the operations will only be applied 9254 * to the specified provider. The dtrace_provider_lock must be held, and the 9255 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 9256 * will need to grab the dtrace_lock when it reenters the framework through 9257 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 9258 */ 9259 static void 9260 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 9261 { 9262 #ifdef illumos 9263 modctl_t *ctl; 9264 #endif 9265 int all = 0; 9266 9267 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9268 9269 if (prv == NULL) { 9270 all = 1; 9271 prv = dtrace_provider; 9272 } 9273 9274 do { 9275 /* 9276 * First, call the blanket provide operation. 9277 */ 9278 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 9279 9280 #ifdef illumos 9281 /* 9282 * Now call the per-module provide operation. We will grab 9283 * mod_lock to prevent the list from being modified. Note 9284 * that this also prevents the mod_busy bits from changing. 9285 * (mod_busy can only be changed with mod_lock held.) 9286 */ 9287 mutex_enter(&mod_lock); 9288 9289 ctl = &modules; 9290 do { 9291 if (ctl->mod_busy || ctl->mod_mp == NULL) 9292 continue; 9293 9294 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 9295 9296 } while ((ctl = ctl->mod_next) != &modules); 9297 9298 mutex_exit(&mod_lock); 9299 #endif 9300 } while (all && (prv = prv->dtpv_next) != NULL); 9301 } 9302 9303 #ifdef illumos 9304 /* 9305 * Iterate over each probe, and call the Framework-to-Provider API function 9306 * denoted by offs. 9307 */ 9308 static void 9309 dtrace_probe_foreach(uintptr_t offs) 9310 { 9311 dtrace_provider_t *prov; 9312 void (*func)(void *, dtrace_id_t, void *); 9313 dtrace_probe_t *probe; 9314 dtrace_icookie_t cookie; 9315 int i; 9316 9317 /* 9318 * We disable interrupts to walk through the probe array. This is 9319 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 9320 * won't see stale data. 9321 */ 9322 cookie = dtrace_interrupt_disable(); 9323 9324 for (i = 0; i < dtrace_nprobes; i++) { 9325 if ((probe = dtrace_probes[i]) == NULL) 9326 continue; 9327 9328 if (probe->dtpr_ecb == NULL) { 9329 /* 9330 * This probe isn't enabled -- don't call the function. 9331 */ 9332 continue; 9333 } 9334 9335 prov = probe->dtpr_provider; 9336 func = *((void(**)(void *, dtrace_id_t, void *)) 9337 ((uintptr_t)&prov->dtpv_pops + offs)); 9338 9339 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 9340 } 9341 9342 dtrace_interrupt_enable(cookie); 9343 } 9344 #endif 9345 9346 static int 9347 dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 9348 { 9349 dtrace_probekey_t pkey; 9350 uint32_t priv; 9351 uid_t uid; 9352 zoneid_t zoneid; 9353 9354 ASSERT(MUTEX_HELD(&dtrace_lock)); 9355 dtrace_ecb_create_cache = NULL; 9356 9357 if (desc == NULL) { 9358 /* 9359 * If we're passed a NULL description, we're being asked to 9360 * create an ECB with a NULL probe. 9361 */ 9362 (void) dtrace_ecb_create_enable(NULL, enab); 9363 return (0); 9364 } 9365 9366 dtrace_probekey(desc, &pkey); 9367 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 9368 &priv, &uid, &zoneid); 9369 9370 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 9371 enab)); 9372 } 9373 9374 /* 9375 * DTrace Helper Provider Functions 9376 */ 9377 static void 9378 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 9379 { 9380 attr->dtat_name = DOF_ATTR_NAME(dofattr); 9381 attr->dtat_data = DOF_ATTR_DATA(dofattr); 9382 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 9383 } 9384 9385 static void 9386 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 9387 const dof_provider_t *dofprov, char *strtab) 9388 { 9389 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 9390 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 9391 dofprov->dofpv_provattr); 9392 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 9393 dofprov->dofpv_modattr); 9394 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 9395 dofprov->dofpv_funcattr); 9396 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 9397 dofprov->dofpv_nameattr); 9398 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 9399 dofprov->dofpv_argsattr); 9400 } 9401 9402 static void 9403 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9404 { 9405 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9406 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9407 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 9408 dof_provider_t *provider; 9409 dof_probe_t *probe; 9410 uint32_t *off, *enoff; 9411 uint8_t *arg; 9412 char *strtab; 9413 uint_t i, nprobes; 9414 dtrace_helper_provdesc_t dhpv; 9415 dtrace_helper_probedesc_t dhpb; 9416 dtrace_meta_t *meta = dtrace_meta_pid; 9417 dtrace_mops_t *mops = &meta->dtm_mops; 9418 void *parg; 9419 9420 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9421 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9422 provider->dofpv_strtab * dof->dofh_secsize); 9423 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9424 provider->dofpv_probes * dof->dofh_secsize); 9425 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9426 provider->dofpv_prargs * dof->dofh_secsize); 9427 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9428 provider->dofpv_proffs * dof->dofh_secsize); 9429 9430 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9431 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 9432 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 9433 enoff = NULL; 9434 9435 /* 9436 * See dtrace_helper_provider_validate(). 9437 */ 9438 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 9439 provider->dofpv_prenoffs != DOF_SECT_NONE) { 9440 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9441 provider->dofpv_prenoffs * dof->dofh_secsize); 9442 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 9443 } 9444 9445 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 9446 9447 /* 9448 * Create the provider. 9449 */ 9450 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9451 9452 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 9453 return; 9454 9455 meta->dtm_count++; 9456 9457 /* 9458 * Create the probes. 9459 */ 9460 for (i = 0; i < nprobes; i++) { 9461 probe = (dof_probe_t *)(uintptr_t)(daddr + 9462 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 9463 9464 /* See the check in dtrace_helper_provider_validate(). */ 9465 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) 9466 continue; 9467 9468 dhpb.dthpb_mod = dhp->dofhp_mod; 9469 dhpb.dthpb_func = strtab + probe->dofpr_func; 9470 dhpb.dthpb_name = strtab + probe->dofpr_name; 9471 dhpb.dthpb_base = probe->dofpr_addr; 9472 dhpb.dthpb_offs = off + probe->dofpr_offidx; 9473 dhpb.dthpb_noffs = probe->dofpr_noffs; 9474 if (enoff != NULL) { 9475 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 9476 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 9477 } else { 9478 dhpb.dthpb_enoffs = NULL; 9479 dhpb.dthpb_nenoffs = 0; 9480 } 9481 dhpb.dthpb_args = arg + probe->dofpr_argidx; 9482 dhpb.dthpb_nargc = probe->dofpr_nargc; 9483 dhpb.dthpb_xargc = probe->dofpr_xargc; 9484 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 9485 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 9486 9487 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 9488 } 9489 } 9490 9491 static void 9492 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 9493 { 9494 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9495 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9496 int i; 9497 9498 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9499 9500 for (i = 0; i < dof->dofh_secnum; i++) { 9501 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9502 dof->dofh_secoff + i * dof->dofh_secsize); 9503 9504 if (sec->dofs_type != DOF_SECT_PROVIDER) 9505 continue; 9506 9507 dtrace_helper_provide_one(dhp, sec, pid); 9508 } 9509 9510 /* 9511 * We may have just created probes, so we must now rematch against 9512 * any retained enablings. Note that this call will acquire both 9513 * cpu_lock and dtrace_lock; the fact that we are holding 9514 * dtrace_meta_lock now is what defines the ordering with respect to 9515 * these three locks. 9516 */ 9517 dtrace_enabling_matchall(); 9518 } 9519 9520 static void 9521 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9522 { 9523 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9524 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9525 dof_sec_t *str_sec; 9526 dof_provider_t *provider; 9527 char *strtab; 9528 dtrace_helper_provdesc_t dhpv; 9529 dtrace_meta_t *meta = dtrace_meta_pid; 9530 dtrace_mops_t *mops = &meta->dtm_mops; 9531 9532 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9533 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9534 provider->dofpv_strtab * dof->dofh_secsize); 9535 9536 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9537 9538 /* 9539 * Create the provider. 9540 */ 9541 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9542 9543 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 9544 9545 meta->dtm_count--; 9546 } 9547 9548 static void 9549 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 9550 { 9551 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9552 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9553 int i; 9554 9555 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9556 9557 for (i = 0; i < dof->dofh_secnum; i++) { 9558 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9559 dof->dofh_secoff + i * dof->dofh_secsize); 9560 9561 if (sec->dofs_type != DOF_SECT_PROVIDER) 9562 continue; 9563 9564 dtrace_helper_provider_remove_one(dhp, sec, pid); 9565 } 9566 } 9567 9568 /* 9569 * DTrace Meta Provider-to-Framework API Functions 9570 * 9571 * These functions implement the Meta Provider-to-Framework API, as described 9572 * in <sys/dtrace.h>. 9573 */ 9574 int 9575 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 9576 dtrace_meta_provider_id_t *idp) 9577 { 9578 dtrace_meta_t *meta; 9579 dtrace_helpers_t *help, *next; 9580 int i; 9581 9582 *idp = DTRACE_METAPROVNONE; 9583 9584 /* 9585 * We strictly don't need the name, but we hold onto it for 9586 * debuggability. All hail error queues! 9587 */ 9588 if (name == NULL) { 9589 cmn_err(CE_WARN, "failed to register meta-provider: " 9590 "invalid name"); 9591 return (EINVAL); 9592 } 9593 9594 if (mops == NULL || 9595 mops->dtms_create_probe == NULL || 9596 mops->dtms_provide_pid == NULL || 9597 mops->dtms_remove_pid == NULL) { 9598 cmn_err(CE_WARN, "failed to register meta-register %s: " 9599 "invalid ops", name); 9600 return (EINVAL); 9601 } 9602 9603 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 9604 meta->dtm_mops = *mops; 9605 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 9606 (void) strcpy(meta->dtm_name, name); 9607 meta->dtm_arg = arg; 9608 9609 mutex_enter(&dtrace_meta_lock); 9610 mutex_enter(&dtrace_lock); 9611 9612 if (dtrace_meta_pid != NULL) { 9613 mutex_exit(&dtrace_lock); 9614 mutex_exit(&dtrace_meta_lock); 9615 cmn_err(CE_WARN, "failed to register meta-register %s: " 9616 "user-land meta-provider exists", name); 9617 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 9618 kmem_free(meta, sizeof (dtrace_meta_t)); 9619 return (EINVAL); 9620 } 9621 9622 dtrace_meta_pid = meta; 9623 *idp = (dtrace_meta_provider_id_t)meta; 9624 9625 /* 9626 * If there are providers and probes ready to go, pass them 9627 * off to the new meta provider now. 9628 */ 9629 9630 help = dtrace_deferred_pid; 9631 dtrace_deferred_pid = NULL; 9632 9633 mutex_exit(&dtrace_lock); 9634 9635 while (help != NULL) { 9636 for (i = 0; i < help->dthps_nprovs; i++) { 9637 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 9638 help->dthps_pid); 9639 } 9640 9641 next = help->dthps_next; 9642 help->dthps_next = NULL; 9643 help->dthps_prev = NULL; 9644 help->dthps_deferred = 0; 9645 help = next; 9646 } 9647 9648 mutex_exit(&dtrace_meta_lock); 9649 9650 return (0); 9651 } 9652 9653 int 9654 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 9655 { 9656 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 9657 9658 mutex_enter(&dtrace_meta_lock); 9659 mutex_enter(&dtrace_lock); 9660 9661 if (old == dtrace_meta_pid) { 9662 pp = &dtrace_meta_pid; 9663 } else { 9664 panic("attempt to unregister non-existent " 9665 "dtrace meta-provider %p\n", (void *)old); 9666 } 9667 9668 if (old->dtm_count != 0) { 9669 mutex_exit(&dtrace_lock); 9670 mutex_exit(&dtrace_meta_lock); 9671 return (EBUSY); 9672 } 9673 9674 *pp = NULL; 9675 9676 mutex_exit(&dtrace_lock); 9677 mutex_exit(&dtrace_meta_lock); 9678 9679 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 9680 kmem_free(old, sizeof (dtrace_meta_t)); 9681 9682 return (0); 9683 } 9684 9685 9686 /* 9687 * DTrace DIF Object Functions 9688 */ 9689 static int 9690 dtrace_difo_err(uint_t pc, const char *format, ...) 9691 { 9692 if (dtrace_err_verbose) { 9693 va_list alist; 9694 9695 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 9696 va_start(alist, format); 9697 (void) vuprintf(format, alist); 9698 va_end(alist); 9699 } 9700 9701 #ifdef DTRACE_ERRDEBUG 9702 dtrace_errdebug(format); 9703 #endif 9704 return (1); 9705 } 9706 9707 /* 9708 * Validate a DTrace DIF object by checking the IR instructions. The following 9709 * rules are currently enforced by dtrace_difo_validate(): 9710 * 9711 * 1. Each instruction must have a valid opcode 9712 * 2. Each register, string, variable, or subroutine reference must be valid 9713 * 3. No instruction can modify register %r0 (must be zero) 9714 * 4. All instruction reserved bits must be set to zero 9715 * 5. The last instruction must be a "ret" instruction 9716 * 6. All branch targets must reference a valid instruction _after_ the branch 9717 */ 9718 static int 9719 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 9720 cred_t *cr) 9721 { 9722 int err = 0, i; 9723 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9724 int kcheckload; 9725 uint_t pc; 9726 int maxglobal = -1, maxlocal = -1, maxtlocal = -1; 9727 9728 kcheckload = cr == NULL || 9729 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 9730 9731 dp->dtdo_destructive = 0; 9732 9733 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 9734 dif_instr_t instr = dp->dtdo_buf[pc]; 9735 9736 uint_t r1 = DIF_INSTR_R1(instr); 9737 uint_t r2 = DIF_INSTR_R2(instr); 9738 uint_t rd = DIF_INSTR_RD(instr); 9739 uint_t rs = DIF_INSTR_RS(instr); 9740 uint_t label = DIF_INSTR_LABEL(instr); 9741 uint_t v = DIF_INSTR_VAR(instr); 9742 uint_t subr = DIF_INSTR_SUBR(instr); 9743 uint_t type = DIF_INSTR_TYPE(instr); 9744 uint_t op = DIF_INSTR_OP(instr); 9745 9746 switch (op) { 9747 case DIF_OP_OR: 9748 case DIF_OP_XOR: 9749 case DIF_OP_AND: 9750 case DIF_OP_SLL: 9751 case DIF_OP_SRL: 9752 case DIF_OP_SRA: 9753 case DIF_OP_SUB: 9754 case DIF_OP_ADD: 9755 case DIF_OP_MUL: 9756 case DIF_OP_SDIV: 9757 case DIF_OP_UDIV: 9758 case DIF_OP_SREM: 9759 case DIF_OP_UREM: 9760 case DIF_OP_COPYS: 9761 if (r1 >= nregs) 9762 err += efunc(pc, "invalid register %u\n", r1); 9763 if (r2 >= nregs) 9764 err += efunc(pc, "invalid register %u\n", r2); 9765 if (rd >= nregs) 9766 err += efunc(pc, "invalid register %u\n", rd); 9767 if (rd == 0) 9768 err += efunc(pc, "cannot write to %r0\n"); 9769 break; 9770 case DIF_OP_NOT: 9771 case DIF_OP_MOV: 9772 case DIF_OP_ALLOCS: 9773 if (r1 >= nregs) 9774 err += efunc(pc, "invalid register %u\n", r1); 9775 if (r2 != 0) 9776 err += efunc(pc, "non-zero reserved bits\n"); 9777 if (rd >= nregs) 9778 err += efunc(pc, "invalid register %u\n", rd); 9779 if (rd == 0) 9780 err += efunc(pc, "cannot write to %r0\n"); 9781 break; 9782 case DIF_OP_LDSB: 9783 case DIF_OP_LDSH: 9784 case DIF_OP_LDSW: 9785 case DIF_OP_LDUB: 9786 case DIF_OP_LDUH: 9787 case DIF_OP_LDUW: 9788 case DIF_OP_LDX: 9789 if (r1 >= nregs) 9790 err += efunc(pc, "invalid register %u\n", r1); 9791 if (r2 != 0) 9792 err += efunc(pc, "non-zero reserved bits\n"); 9793 if (rd >= nregs) 9794 err += efunc(pc, "invalid register %u\n", rd); 9795 if (rd == 0) 9796 err += efunc(pc, "cannot write to %r0\n"); 9797 if (kcheckload) 9798 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 9799 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 9800 break; 9801 case DIF_OP_RLDSB: 9802 case DIF_OP_RLDSH: 9803 case DIF_OP_RLDSW: 9804 case DIF_OP_RLDUB: 9805 case DIF_OP_RLDUH: 9806 case DIF_OP_RLDUW: 9807 case DIF_OP_RLDX: 9808 if (r1 >= nregs) 9809 err += efunc(pc, "invalid register %u\n", r1); 9810 if (r2 != 0) 9811 err += efunc(pc, "non-zero reserved bits\n"); 9812 if (rd >= nregs) 9813 err += efunc(pc, "invalid register %u\n", rd); 9814 if (rd == 0) 9815 err += efunc(pc, "cannot write to %r0\n"); 9816 break; 9817 case DIF_OP_ULDSB: 9818 case DIF_OP_ULDSH: 9819 case DIF_OP_ULDSW: 9820 case DIF_OP_ULDUB: 9821 case DIF_OP_ULDUH: 9822 case DIF_OP_ULDUW: 9823 case DIF_OP_ULDX: 9824 if (r1 >= nregs) 9825 err += efunc(pc, "invalid register %u\n", r1); 9826 if (r2 != 0) 9827 err += efunc(pc, "non-zero reserved bits\n"); 9828 if (rd >= nregs) 9829 err += efunc(pc, "invalid register %u\n", rd); 9830 if (rd == 0) 9831 err += efunc(pc, "cannot write to %r0\n"); 9832 break; 9833 case DIF_OP_STB: 9834 case DIF_OP_STH: 9835 case DIF_OP_STW: 9836 case DIF_OP_STX: 9837 if (r1 >= nregs) 9838 err += efunc(pc, "invalid register %u\n", r1); 9839 if (r2 != 0) 9840 err += efunc(pc, "non-zero reserved bits\n"); 9841 if (rd >= nregs) 9842 err += efunc(pc, "invalid register %u\n", rd); 9843 if (rd == 0) 9844 err += efunc(pc, "cannot write to 0 address\n"); 9845 break; 9846 case DIF_OP_CMP: 9847 case DIF_OP_SCMP: 9848 if (r1 >= nregs) 9849 err += efunc(pc, "invalid register %u\n", r1); 9850 if (r2 >= nregs) 9851 err += efunc(pc, "invalid register %u\n", r2); 9852 if (rd != 0) 9853 err += efunc(pc, "non-zero reserved bits\n"); 9854 break; 9855 case DIF_OP_TST: 9856 if (r1 >= nregs) 9857 err += efunc(pc, "invalid register %u\n", r1); 9858 if (r2 != 0 || rd != 0) 9859 err += efunc(pc, "non-zero reserved bits\n"); 9860 break; 9861 case DIF_OP_BA: 9862 case DIF_OP_BE: 9863 case DIF_OP_BNE: 9864 case DIF_OP_BG: 9865 case DIF_OP_BGU: 9866 case DIF_OP_BGE: 9867 case DIF_OP_BGEU: 9868 case DIF_OP_BL: 9869 case DIF_OP_BLU: 9870 case DIF_OP_BLE: 9871 case DIF_OP_BLEU: 9872 if (label >= dp->dtdo_len) { 9873 err += efunc(pc, "invalid branch target %u\n", 9874 label); 9875 } 9876 if (label <= pc) { 9877 err += efunc(pc, "backward branch to %u\n", 9878 label); 9879 } 9880 break; 9881 case DIF_OP_RET: 9882 if (r1 != 0 || r2 != 0) 9883 err += efunc(pc, "non-zero reserved bits\n"); 9884 if (rd >= nregs) 9885 err += efunc(pc, "invalid register %u\n", rd); 9886 break; 9887 case DIF_OP_NOP: 9888 case DIF_OP_POPTS: 9889 case DIF_OP_FLUSHTS: 9890 if (r1 != 0 || r2 != 0 || rd != 0) 9891 err += efunc(pc, "non-zero reserved bits\n"); 9892 break; 9893 case DIF_OP_SETX: 9894 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 9895 err += efunc(pc, "invalid integer ref %u\n", 9896 DIF_INSTR_INTEGER(instr)); 9897 } 9898 if (rd >= nregs) 9899 err += efunc(pc, "invalid register %u\n", rd); 9900 if (rd == 0) 9901 err += efunc(pc, "cannot write to %r0\n"); 9902 break; 9903 case DIF_OP_SETS: 9904 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 9905 err += efunc(pc, "invalid string ref %u\n", 9906 DIF_INSTR_STRING(instr)); 9907 } 9908 if (rd >= nregs) 9909 err += efunc(pc, "invalid register %u\n", rd); 9910 if (rd == 0) 9911 err += efunc(pc, "cannot write to %r0\n"); 9912 break; 9913 case DIF_OP_LDGA: 9914 case DIF_OP_LDTA: 9915 if (r1 > DIF_VAR_ARRAY_MAX) 9916 err += efunc(pc, "invalid array %u\n", r1); 9917 if (r2 >= nregs) 9918 err += efunc(pc, "invalid register %u\n", r2); 9919 if (rd >= nregs) 9920 err += efunc(pc, "invalid register %u\n", rd); 9921 if (rd == 0) 9922 err += efunc(pc, "cannot write to %r0\n"); 9923 break; 9924 case DIF_OP_LDGS: 9925 case DIF_OP_LDTS: 9926 case DIF_OP_LDLS: 9927 case DIF_OP_LDGAA: 9928 case DIF_OP_LDTAA: 9929 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 9930 err += efunc(pc, "invalid variable %u\n", v); 9931 if (rd >= nregs) 9932 err += efunc(pc, "invalid register %u\n", rd); 9933 if (rd == 0) 9934 err += efunc(pc, "cannot write to %r0\n"); 9935 break; 9936 case DIF_OP_STGS: 9937 case DIF_OP_STTS: 9938 case DIF_OP_STLS: 9939 case DIF_OP_STGAA: 9940 case DIF_OP_STTAA: 9941 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 9942 err += efunc(pc, "invalid variable %u\n", v); 9943 if (rs >= nregs) 9944 err += efunc(pc, "invalid register %u\n", rd); 9945 break; 9946 case DIF_OP_CALL: 9947 if (subr > DIF_SUBR_MAX) 9948 err += efunc(pc, "invalid subr %u\n", subr); 9949 if (rd >= nregs) 9950 err += efunc(pc, "invalid register %u\n", rd); 9951 if (rd == 0) 9952 err += efunc(pc, "cannot write to %r0\n"); 9953 9954 if (subr == DIF_SUBR_COPYOUT || 9955 subr == DIF_SUBR_COPYOUTSTR) { 9956 dp->dtdo_destructive = 1; 9957 } 9958 9959 if (subr == DIF_SUBR_GETF) { 9960 /* 9961 * If we have a getf() we need to record that 9962 * in our state. Note that our state can be 9963 * NULL if this is a helper -- but in that 9964 * case, the call to getf() is itself illegal, 9965 * and will be caught (slightly later) when 9966 * the helper is validated. 9967 */ 9968 if (vstate->dtvs_state != NULL) 9969 vstate->dtvs_state->dts_getf++; 9970 } 9971 9972 break; 9973 case DIF_OP_PUSHTR: 9974 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 9975 err += efunc(pc, "invalid ref type %u\n", type); 9976 if (r2 >= nregs) 9977 err += efunc(pc, "invalid register %u\n", r2); 9978 if (rs >= nregs) 9979 err += efunc(pc, "invalid register %u\n", rs); 9980 break; 9981 case DIF_OP_PUSHTV: 9982 if (type != DIF_TYPE_CTF) 9983 err += efunc(pc, "invalid val type %u\n", type); 9984 if (r2 >= nregs) 9985 err += efunc(pc, "invalid register %u\n", r2); 9986 if (rs >= nregs) 9987 err += efunc(pc, "invalid register %u\n", rs); 9988 break; 9989 default: 9990 err += efunc(pc, "invalid opcode %u\n", 9991 DIF_INSTR_OP(instr)); 9992 } 9993 } 9994 9995 if (dp->dtdo_len != 0 && 9996 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 9997 err += efunc(dp->dtdo_len - 1, 9998 "expected 'ret' as last DIF instruction\n"); 9999 } 10000 10001 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) { 10002 /* 10003 * If we're not returning by reference, the size must be either 10004 * 0 or the size of one of the base types. 10005 */ 10006 switch (dp->dtdo_rtype.dtdt_size) { 10007 case 0: 10008 case sizeof (uint8_t): 10009 case sizeof (uint16_t): 10010 case sizeof (uint32_t): 10011 case sizeof (uint64_t): 10012 break; 10013 10014 default: 10015 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 10016 } 10017 } 10018 10019 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 10020 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 10021 dtrace_diftype_t *vt, *et; 10022 uint_t id, ndx; 10023 10024 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 10025 v->dtdv_scope != DIFV_SCOPE_THREAD && 10026 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 10027 err += efunc(i, "unrecognized variable scope %d\n", 10028 v->dtdv_scope); 10029 break; 10030 } 10031 10032 if (v->dtdv_kind != DIFV_KIND_ARRAY && 10033 v->dtdv_kind != DIFV_KIND_SCALAR) { 10034 err += efunc(i, "unrecognized variable type %d\n", 10035 v->dtdv_kind); 10036 break; 10037 } 10038 10039 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 10040 err += efunc(i, "%d exceeds variable id limit\n", id); 10041 break; 10042 } 10043 10044 if (id < DIF_VAR_OTHER_UBASE) 10045 continue; 10046 10047 /* 10048 * For user-defined variables, we need to check that this 10049 * definition is identical to any previous definition that we 10050 * encountered. 10051 */ 10052 ndx = id - DIF_VAR_OTHER_UBASE; 10053 10054 switch (v->dtdv_scope) { 10055 case DIFV_SCOPE_GLOBAL: 10056 if (maxglobal == -1 || ndx > maxglobal) 10057 maxglobal = ndx; 10058 10059 if (ndx < vstate->dtvs_nglobals) { 10060 dtrace_statvar_t *svar; 10061 10062 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 10063 existing = &svar->dtsv_var; 10064 } 10065 10066 break; 10067 10068 case DIFV_SCOPE_THREAD: 10069 if (maxtlocal == -1 || ndx > maxtlocal) 10070 maxtlocal = ndx; 10071 10072 if (ndx < vstate->dtvs_ntlocals) 10073 existing = &vstate->dtvs_tlocals[ndx]; 10074 break; 10075 10076 case DIFV_SCOPE_LOCAL: 10077 if (maxlocal == -1 || ndx > maxlocal) 10078 maxlocal = ndx; 10079 10080 if (ndx < vstate->dtvs_nlocals) { 10081 dtrace_statvar_t *svar; 10082 10083 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 10084 existing = &svar->dtsv_var; 10085 } 10086 10087 break; 10088 } 10089 10090 vt = &v->dtdv_type; 10091 10092 if (vt->dtdt_flags & DIF_TF_BYREF) { 10093 if (vt->dtdt_size == 0) { 10094 err += efunc(i, "zero-sized variable\n"); 10095 break; 10096 } 10097 10098 if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL || 10099 v->dtdv_scope == DIFV_SCOPE_LOCAL) && 10100 vt->dtdt_size > dtrace_statvar_maxsize) { 10101 err += efunc(i, "oversized by-ref static\n"); 10102 break; 10103 } 10104 } 10105 10106 if (existing == NULL || existing->dtdv_id == 0) 10107 continue; 10108 10109 ASSERT(existing->dtdv_id == v->dtdv_id); 10110 ASSERT(existing->dtdv_scope == v->dtdv_scope); 10111 10112 if (existing->dtdv_kind != v->dtdv_kind) 10113 err += efunc(i, "%d changed variable kind\n", id); 10114 10115 et = &existing->dtdv_type; 10116 10117 if (vt->dtdt_flags != et->dtdt_flags) { 10118 err += efunc(i, "%d changed variable type flags\n", id); 10119 break; 10120 } 10121 10122 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 10123 err += efunc(i, "%d changed variable type size\n", id); 10124 break; 10125 } 10126 } 10127 10128 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 10129 dif_instr_t instr = dp->dtdo_buf[pc]; 10130 10131 uint_t v = DIF_INSTR_VAR(instr); 10132 uint_t op = DIF_INSTR_OP(instr); 10133 10134 switch (op) { 10135 case DIF_OP_LDGS: 10136 case DIF_OP_LDGAA: 10137 case DIF_OP_STGS: 10138 case DIF_OP_STGAA: 10139 if (v > DIF_VAR_OTHER_UBASE + maxglobal) 10140 err += efunc(pc, "invalid variable %u\n", v); 10141 break; 10142 case DIF_OP_LDTS: 10143 case DIF_OP_LDTAA: 10144 case DIF_OP_STTS: 10145 case DIF_OP_STTAA: 10146 if (v > DIF_VAR_OTHER_UBASE + maxtlocal) 10147 err += efunc(pc, "invalid variable %u\n", v); 10148 break; 10149 case DIF_OP_LDLS: 10150 case DIF_OP_STLS: 10151 if (v > DIF_VAR_OTHER_UBASE + maxlocal) 10152 err += efunc(pc, "invalid variable %u\n", v); 10153 break; 10154 default: 10155 break; 10156 } 10157 } 10158 10159 return (err); 10160 } 10161 10162 /* 10163 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 10164 * are much more constrained than normal DIFOs. Specifically, they may 10165 * not: 10166 * 10167 * 1. Make calls to subroutines other than copyin(), copyinstr() or 10168 * miscellaneous string routines 10169 * 2. Access DTrace variables other than the args[] array, and the 10170 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 10171 * 3. Have thread-local variables. 10172 * 4. Have dynamic variables. 10173 */ 10174 static int 10175 dtrace_difo_validate_helper(dtrace_difo_t *dp) 10176 { 10177 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 10178 int err = 0; 10179 uint_t pc; 10180 10181 for (pc = 0; pc < dp->dtdo_len; pc++) { 10182 dif_instr_t instr = dp->dtdo_buf[pc]; 10183 10184 uint_t v = DIF_INSTR_VAR(instr); 10185 uint_t subr = DIF_INSTR_SUBR(instr); 10186 uint_t op = DIF_INSTR_OP(instr); 10187 10188 switch (op) { 10189 case DIF_OP_OR: 10190 case DIF_OP_XOR: 10191 case DIF_OP_AND: 10192 case DIF_OP_SLL: 10193 case DIF_OP_SRL: 10194 case DIF_OP_SRA: 10195 case DIF_OP_SUB: 10196 case DIF_OP_ADD: 10197 case DIF_OP_MUL: 10198 case DIF_OP_SDIV: 10199 case DIF_OP_UDIV: 10200 case DIF_OP_SREM: 10201 case DIF_OP_UREM: 10202 case DIF_OP_COPYS: 10203 case DIF_OP_NOT: 10204 case DIF_OP_MOV: 10205 case DIF_OP_RLDSB: 10206 case DIF_OP_RLDSH: 10207 case DIF_OP_RLDSW: 10208 case DIF_OP_RLDUB: 10209 case DIF_OP_RLDUH: 10210 case DIF_OP_RLDUW: 10211 case DIF_OP_RLDX: 10212 case DIF_OP_ULDSB: 10213 case DIF_OP_ULDSH: 10214 case DIF_OP_ULDSW: 10215 case DIF_OP_ULDUB: 10216 case DIF_OP_ULDUH: 10217 case DIF_OP_ULDUW: 10218 case DIF_OP_ULDX: 10219 case DIF_OP_STB: 10220 case DIF_OP_STH: 10221 case DIF_OP_STW: 10222 case DIF_OP_STX: 10223 case DIF_OP_ALLOCS: 10224 case DIF_OP_CMP: 10225 case DIF_OP_SCMP: 10226 case DIF_OP_TST: 10227 case DIF_OP_BA: 10228 case DIF_OP_BE: 10229 case DIF_OP_BNE: 10230 case DIF_OP_BG: 10231 case DIF_OP_BGU: 10232 case DIF_OP_BGE: 10233 case DIF_OP_BGEU: 10234 case DIF_OP_BL: 10235 case DIF_OP_BLU: 10236 case DIF_OP_BLE: 10237 case DIF_OP_BLEU: 10238 case DIF_OP_RET: 10239 case DIF_OP_NOP: 10240 case DIF_OP_POPTS: 10241 case DIF_OP_FLUSHTS: 10242 case DIF_OP_SETX: 10243 case DIF_OP_SETS: 10244 case DIF_OP_LDGA: 10245 case DIF_OP_LDLS: 10246 case DIF_OP_STGS: 10247 case DIF_OP_STLS: 10248 case DIF_OP_PUSHTR: 10249 case DIF_OP_PUSHTV: 10250 break; 10251 10252 case DIF_OP_LDGS: 10253 if (v >= DIF_VAR_OTHER_UBASE) 10254 break; 10255 10256 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 10257 break; 10258 10259 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 10260 v == DIF_VAR_PPID || v == DIF_VAR_TID || 10261 v == DIF_VAR_EXECARGS || 10262 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 10263 v == DIF_VAR_UID || v == DIF_VAR_GID) 10264 break; 10265 10266 err += efunc(pc, "illegal variable %u\n", v); 10267 break; 10268 10269 case DIF_OP_LDTA: 10270 case DIF_OP_LDTS: 10271 case DIF_OP_LDGAA: 10272 case DIF_OP_LDTAA: 10273 err += efunc(pc, "illegal dynamic variable load\n"); 10274 break; 10275 10276 case DIF_OP_STTS: 10277 case DIF_OP_STGAA: 10278 case DIF_OP_STTAA: 10279 err += efunc(pc, "illegal dynamic variable store\n"); 10280 break; 10281 10282 case DIF_OP_CALL: 10283 if (subr == DIF_SUBR_ALLOCA || 10284 subr == DIF_SUBR_BCOPY || 10285 subr == DIF_SUBR_COPYIN || 10286 subr == DIF_SUBR_COPYINTO || 10287 subr == DIF_SUBR_COPYINSTR || 10288 subr == DIF_SUBR_INDEX || 10289 subr == DIF_SUBR_INET_NTOA || 10290 subr == DIF_SUBR_INET_NTOA6 || 10291 subr == DIF_SUBR_INET_NTOP || 10292 subr == DIF_SUBR_JSON || 10293 subr == DIF_SUBR_LLTOSTR || 10294 subr == DIF_SUBR_STRTOLL || 10295 subr == DIF_SUBR_RINDEX || 10296 subr == DIF_SUBR_STRCHR || 10297 subr == DIF_SUBR_STRJOIN || 10298 subr == DIF_SUBR_STRRCHR || 10299 subr == DIF_SUBR_STRSTR || 10300 subr == DIF_SUBR_HTONS || 10301 subr == DIF_SUBR_HTONL || 10302 subr == DIF_SUBR_HTONLL || 10303 subr == DIF_SUBR_NTOHS || 10304 subr == DIF_SUBR_NTOHL || 10305 subr == DIF_SUBR_NTOHLL || 10306 subr == DIF_SUBR_MEMREF) 10307 break; 10308 #ifdef __FreeBSD__ 10309 if (subr == DIF_SUBR_MEMSTR) 10310 break; 10311 #endif 10312 10313 err += efunc(pc, "invalid subr %u\n", subr); 10314 break; 10315 10316 default: 10317 err += efunc(pc, "invalid opcode %u\n", 10318 DIF_INSTR_OP(instr)); 10319 } 10320 } 10321 10322 return (err); 10323 } 10324 10325 /* 10326 * Returns 1 if the expression in the DIF object can be cached on a per-thread 10327 * basis; 0 if not. 10328 */ 10329 static int 10330 dtrace_difo_cacheable(dtrace_difo_t *dp) 10331 { 10332 int i; 10333 10334 if (dp == NULL) 10335 return (0); 10336 10337 for (i = 0; i < dp->dtdo_varlen; i++) { 10338 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10339 10340 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 10341 continue; 10342 10343 switch (v->dtdv_id) { 10344 case DIF_VAR_CURTHREAD: 10345 case DIF_VAR_PID: 10346 case DIF_VAR_TID: 10347 case DIF_VAR_EXECARGS: 10348 case DIF_VAR_EXECNAME: 10349 case DIF_VAR_ZONENAME: 10350 break; 10351 10352 default: 10353 return (0); 10354 } 10355 } 10356 10357 /* 10358 * This DIF object may be cacheable. Now we need to look for any 10359 * array loading instructions, any memory loading instructions, or 10360 * any stores to thread-local variables. 10361 */ 10362 for (i = 0; i < dp->dtdo_len; i++) { 10363 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 10364 10365 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 10366 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 10367 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 10368 op == DIF_OP_LDGA || op == DIF_OP_STTS) 10369 return (0); 10370 } 10371 10372 return (1); 10373 } 10374 10375 static void 10376 dtrace_difo_hold(dtrace_difo_t *dp) 10377 { 10378 int i; 10379 10380 ASSERT(MUTEX_HELD(&dtrace_lock)); 10381 10382 dp->dtdo_refcnt++; 10383 ASSERT(dp->dtdo_refcnt != 0); 10384 10385 /* 10386 * We need to check this DIF object for references to the variable 10387 * DIF_VAR_VTIMESTAMP. 10388 */ 10389 for (i = 0; i < dp->dtdo_varlen; i++) { 10390 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10391 10392 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10393 continue; 10394 10395 if (dtrace_vtime_references++ == 0) 10396 dtrace_vtime_enable(); 10397 } 10398 } 10399 10400 /* 10401 * This routine calculates the dynamic variable chunksize for a given DIF 10402 * object. The calculation is not fool-proof, and can probably be tricked by 10403 * malicious DIF -- but it works for all compiler-generated DIF. Because this 10404 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 10405 * if a dynamic variable size exceeds the chunksize. 10406 */ 10407 static void 10408 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10409 { 10410 uint64_t sval = 0; 10411 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 10412 const dif_instr_t *text = dp->dtdo_buf; 10413 uint_t pc, srd = 0; 10414 uint_t ttop = 0; 10415 size_t size, ksize; 10416 uint_t id, i; 10417 10418 for (pc = 0; pc < dp->dtdo_len; pc++) { 10419 dif_instr_t instr = text[pc]; 10420 uint_t op = DIF_INSTR_OP(instr); 10421 uint_t rd = DIF_INSTR_RD(instr); 10422 uint_t r1 = DIF_INSTR_R1(instr); 10423 uint_t nkeys = 0; 10424 uchar_t scope = 0; 10425 10426 dtrace_key_t *key = tupregs; 10427 10428 switch (op) { 10429 case DIF_OP_SETX: 10430 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 10431 srd = rd; 10432 continue; 10433 10434 case DIF_OP_STTS: 10435 key = &tupregs[DIF_DTR_NREGS]; 10436 key[0].dttk_size = 0; 10437 key[1].dttk_size = 0; 10438 nkeys = 2; 10439 scope = DIFV_SCOPE_THREAD; 10440 break; 10441 10442 case DIF_OP_STGAA: 10443 case DIF_OP_STTAA: 10444 nkeys = ttop; 10445 10446 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 10447 key[nkeys++].dttk_size = 0; 10448 10449 key[nkeys++].dttk_size = 0; 10450 10451 if (op == DIF_OP_STTAA) { 10452 scope = DIFV_SCOPE_THREAD; 10453 } else { 10454 scope = DIFV_SCOPE_GLOBAL; 10455 } 10456 10457 break; 10458 10459 case DIF_OP_PUSHTR: 10460 if (ttop == DIF_DTR_NREGS) 10461 return; 10462 10463 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 10464 /* 10465 * If the register for the size of the "pushtr" 10466 * is %r0 (or the value is 0) and the type is 10467 * a string, we'll use the system-wide default 10468 * string size. 10469 */ 10470 tupregs[ttop++].dttk_size = 10471 dtrace_strsize_default; 10472 } else { 10473 if (srd == 0) 10474 return; 10475 10476 if (sval > LONG_MAX) 10477 return; 10478 10479 tupregs[ttop++].dttk_size = sval; 10480 } 10481 10482 break; 10483 10484 case DIF_OP_PUSHTV: 10485 if (ttop == DIF_DTR_NREGS) 10486 return; 10487 10488 tupregs[ttop++].dttk_size = 0; 10489 break; 10490 10491 case DIF_OP_FLUSHTS: 10492 ttop = 0; 10493 break; 10494 10495 case DIF_OP_POPTS: 10496 if (ttop != 0) 10497 ttop--; 10498 break; 10499 } 10500 10501 sval = 0; 10502 srd = 0; 10503 10504 if (nkeys == 0) 10505 continue; 10506 10507 /* 10508 * We have a dynamic variable allocation; calculate its size. 10509 */ 10510 for (ksize = 0, i = 0; i < nkeys; i++) 10511 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 10512 10513 size = sizeof (dtrace_dynvar_t); 10514 size += sizeof (dtrace_key_t) * (nkeys - 1); 10515 size += ksize; 10516 10517 /* 10518 * Now we need to determine the size of the stored data. 10519 */ 10520 id = DIF_INSTR_VAR(instr); 10521 10522 for (i = 0; i < dp->dtdo_varlen; i++) { 10523 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10524 10525 if (v->dtdv_id == id && v->dtdv_scope == scope) { 10526 size += v->dtdv_type.dtdt_size; 10527 break; 10528 } 10529 } 10530 10531 if (i == dp->dtdo_varlen) 10532 return; 10533 10534 /* 10535 * We have the size. If this is larger than the chunk size 10536 * for our dynamic variable state, reset the chunk size. 10537 */ 10538 size = P2ROUNDUP(size, sizeof (uint64_t)); 10539 10540 /* 10541 * Before setting the chunk size, check that we're not going 10542 * to set it to a negative value... 10543 */ 10544 if (size > LONG_MAX) 10545 return; 10546 10547 /* 10548 * ...and make certain that we didn't badly overflow. 10549 */ 10550 if (size < ksize || size < sizeof (dtrace_dynvar_t)) 10551 return; 10552 10553 if (size > vstate->dtvs_dynvars.dtds_chunksize) 10554 vstate->dtvs_dynvars.dtds_chunksize = size; 10555 } 10556 } 10557 10558 static void 10559 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10560 { 10561 int i, oldsvars, osz, nsz, otlocals, ntlocals; 10562 uint_t id; 10563 10564 ASSERT(MUTEX_HELD(&dtrace_lock)); 10565 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 10566 10567 for (i = 0; i < dp->dtdo_varlen; i++) { 10568 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10569 dtrace_statvar_t *svar, ***svarp = NULL; 10570 size_t dsize = 0; 10571 uint8_t scope = v->dtdv_scope; 10572 int *np = NULL; 10573 10574 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10575 continue; 10576 10577 id -= DIF_VAR_OTHER_UBASE; 10578 10579 switch (scope) { 10580 case DIFV_SCOPE_THREAD: 10581 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 10582 dtrace_difv_t *tlocals; 10583 10584 if ((ntlocals = (otlocals << 1)) == 0) 10585 ntlocals = 1; 10586 10587 osz = otlocals * sizeof (dtrace_difv_t); 10588 nsz = ntlocals * sizeof (dtrace_difv_t); 10589 10590 tlocals = kmem_zalloc(nsz, KM_SLEEP); 10591 10592 if (osz != 0) { 10593 bcopy(vstate->dtvs_tlocals, 10594 tlocals, osz); 10595 kmem_free(vstate->dtvs_tlocals, osz); 10596 } 10597 10598 vstate->dtvs_tlocals = tlocals; 10599 vstate->dtvs_ntlocals = ntlocals; 10600 } 10601 10602 vstate->dtvs_tlocals[id] = *v; 10603 continue; 10604 10605 case DIFV_SCOPE_LOCAL: 10606 np = &vstate->dtvs_nlocals; 10607 svarp = &vstate->dtvs_locals; 10608 10609 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10610 dsize = NCPU * (v->dtdv_type.dtdt_size + 10611 sizeof (uint64_t)); 10612 else 10613 dsize = NCPU * sizeof (uint64_t); 10614 10615 break; 10616 10617 case DIFV_SCOPE_GLOBAL: 10618 np = &vstate->dtvs_nglobals; 10619 svarp = &vstate->dtvs_globals; 10620 10621 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10622 dsize = v->dtdv_type.dtdt_size + 10623 sizeof (uint64_t); 10624 10625 break; 10626 10627 default: 10628 ASSERT(0); 10629 } 10630 10631 while (id >= (oldsvars = *np)) { 10632 dtrace_statvar_t **statics; 10633 int newsvars, oldsize, newsize; 10634 10635 if ((newsvars = (oldsvars << 1)) == 0) 10636 newsvars = 1; 10637 10638 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 10639 newsize = newsvars * sizeof (dtrace_statvar_t *); 10640 10641 statics = kmem_zalloc(newsize, KM_SLEEP); 10642 10643 if (oldsize != 0) { 10644 bcopy(*svarp, statics, oldsize); 10645 kmem_free(*svarp, oldsize); 10646 } 10647 10648 *svarp = statics; 10649 *np = newsvars; 10650 } 10651 10652 if ((svar = (*svarp)[id]) == NULL) { 10653 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 10654 svar->dtsv_var = *v; 10655 10656 if ((svar->dtsv_size = dsize) != 0) { 10657 svar->dtsv_data = (uint64_t)(uintptr_t) 10658 kmem_zalloc(dsize, KM_SLEEP); 10659 } 10660 10661 (*svarp)[id] = svar; 10662 } 10663 10664 svar->dtsv_refcnt++; 10665 } 10666 10667 dtrace_difo_chunksize(dp, vstate); 10668 dtrace_difo_hold(dp); 10669 } 10670 10671 static dtrace_difo_t * 10672 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10673 { 10674 dtrace_difo_t *new; 10675 size_t sz; 10676 10677 ASSERT(dp->dtdo_buf != NULL); 10678 ASSERT(dp->dtdo_refcnt != 0); 10679 10680 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10681 10682 ASSERT(dp->dtdo_buf != NULL); 10683 sz = dp->dtdo_len * sizeof (dif_instr_t); 10684 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 10685 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 10686 new->dtdo_len = dp->dtdo_len; 10687 10688 if (dp->dtdo_strtab != NULL) { 10689 ASSERT(dp->dtdo_strlen != 0); 10690 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 10691 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 10692 new->dtdo_strlen = dp->dtdo_strlen; 10693 } 10694 10695 if (dp->dtdo_inttab != NULL) { 10696 ASSERT(dp->dtdo_intlen != 0); 10697 sz = dp->dtdo_intlen * sizeof (uint64_t); 10698 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 10699 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 10700 new->dtdo_intlen = dp->dtdo_intlen; 10701 } 10702 10703 if (dp->dtdo_vartab != NULL) { 10704 ASSERT(dp->dtdo_varlen != 0); 10705 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 10706 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 10707 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 10708 new->dtdo_varlen = dp->dtdo_varlen; 10709 } 10710 10711 dtrace_difo_init(new, vstate); 10712 return (new); 10713 } 10714 10715 static void 10716 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10717 { 10718 int i; 10719 10720 ASSERT(dp->dtdo_refcnt == 0); 10721 10722 for (i = 0; i < dp->dtdo_varlen; i++) { 10723 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10724 dtrace_statvar_t *svar, **svarp = NULL; 10725 uint_t id; 10726 uint8_t scope = v->dtdv_scope; 10727 int *np = NULL; 10728 10729 switch (scope) { 10730 case DIFV_SCOPE_THREAD: 10731 continue; 10732 10733 case DIFV_SCOPE_LOCAL: 10734 np = &vstate->dtvs_nlocals; 10735 svarp = vstate->dtvs_locals; 10736 break; 10737 10738 case DIFV_SCOPE_GLOBAL: 10739 np = &vstate->dtvs_nglobals; 10740 svarp = vstate->dtvs_globals; 10741 break; 10742 10743 default: 10744 ASSERT(0); 10745 } 10746 10747 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10748 continue; 10749 10750 id -= DIF_VAR_OTHER_UBASE; 10751 ASSERT(id < *np); 10752 10753 svar = svarp[id]; 10754 ASSERT(svar != NULL); 10755 ASSERT(svar->dtsv_refcnt > 0); 10756 10757 if (--svar->dtsv_refcnt > 0) 10758 continue; 10759 10760 if (svar->dtsv_size != 0) { 10761 ASSERT(svar->dtsv_data != 0); 10762 kmem_free((void *)(uintptr_t)svar->dtsv_data, 10763 svar->dtsv_size); 10764 } 10765 10766 kmem_free(svar, sizeof (dtrace_statvar_t)); 10767 svarp[id] = NULL; 10768 } 10769 10770 if (dp->dtdo_buf != NULL) 10771 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10772 if (dp->dtdo_inttab != NULL) 10773 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10774 if (dp->dtdo_strtab != NULL) 10775 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10776 if (dp->dtdo_vartab != NULL) 10777 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10778 10779 kmem_free(dp, sizeof (dtrace_difo_t)); 10780 } 10781 10782 static void 10783 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10784 { 10785 int i; 10786 10787 ASSERT(MUTEX_HELD(&dtrace_lock)); 10788 ASSERT(dp->dtdo_refcnt != 0); 10789 10790 for (i = 0; i < dp->dtdo_varlen; i++) { 10791 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10792 10793 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10794 continue; 10795 10796 ASSERT(dtrace_vtime_references > 0); 10797 if (--dtrace_vtime_references == 0) 10798 dtrace_vtime_disable(); 10799 } 10800 10801 if (--dp->dtdo_refcnt == 0) 10802 dtrace_difo_destroy(dp, vstate); 10803 } 10804 10805 /* 10806 * DTrace Format Functions 10807 */ 10808 static uint16_t 10809 dtrace_format_add(dtrace_state_t *state, char *str) 10810 { 10811 char *fmt, **new; 10812 uint16_t ndx, len = strlen(str) + 1; 10813 10814 fmt = kmem_zalloc(len, KM_SLEEP); 10815 bcopy(str, fmt, len); 10816 10817 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 10818 if (state->dts_formats[ndx] == NULL) { 10819 state->dts_formats[ndx] = fmt; 10820 return (ndx + 1); 10821 } 10822 } 10823 10824 if (state->dts_nformats == USHRT_MAX) { 10825 /* 10826 * This is only likely if a denial-of-service attack is being 10827 * attempted. As such, it's okay to fail silently here. 10828 */ 10829 kmem_free(fmt, len); 10830 return (0); 10831 } 10832 10833 /* 10834 * For simplicity, we always resize the formats array to be exactly the 10835 * number of formats. 10836 */ 10837 ndx = state->dts_nformats++; 10838 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 10839 10840 if (state->dts_formats != NULL) { 10841 ASSERT(ndx != 0); 10842 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 10843 kmem_free(state->dts_formats, ndx * sizeof (char *)); 10844 } 10845 10846 state->dts_formats = new; 10847 state->dts_formats[ndx] = fmt; 10848 10849 return (ndx + 1); 10850 } 10851 10852 static void 10853 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 10854 { 10855 char *fmt; 10856 10857 ASSERT(state->dts_formats != NULL); 10858 ASSERT(format <= state->dts_nformats); 10859 ASSERT(state->dts_formats[format - 1] != NULL); 10860 10861 fmt = state->dts_formats[format - 1]; 10862 kmem_free(fmt, strlen(fmt) + 1); 10863 state->dts_formats[format - 1] = NULL; 10864 } 10865 10866 static void 10867 dtrace_format_destroy(dtrace_state_t *state) 10868 { 10869 int i; 10870 10871 if (state->dts_nformats == 0) { 10872 ASSERT(state->dts_formats == NULL); 10873 return; 10874 } 10875 10876 ASSERT(state->dts_formats != NULL); 10877 10878 for (i = 0; i < state->dts_nformats; i++) { 10879 char *fmt = state->dts_formats[i]; 10880 10881 if (fmt == NULL) 10882 continue; 10883 10884 kmem_free(fmt, strlen(fmt) + 1); 10885 } 10886 10887 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 10888 state->dts_nformats = 0; 10889 state->dts_formats = NULL; 10890 } 10891 10892 /* 10893 * DTrace Predicate Functions 10894 */ 10895 static dtrace_predicate_t * 10896 dtrace_predicate_create(dtrace_difo_t *dp) 10897 { 10898 dtrace_predicate_t *pred; 10899 10900 ASSERT(MUTEX_HELD(&dtrace_lock)); 10901 ASSERT(dp->dtdo_refcnt != 0); 10902 10903 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 10904 pred->dtp_difo = dp; 10905 pred->dtp_refcnt = 1; 10906 10907 if (!dtrace_difo_cacheable(dp)) 10908 return (pred); 10909 10910 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 10911 /* 10912 * This is only theoretically possible -- we have had 2^32 10913 * cacheable predicates on this machine. We cannot allow any 10914 * more predicates to become cacheable: as unlikely as it is, 10915 * there may be a thread caching a (now stale) predicate cache 10916 * ID. (N.B.: the temptation is being successfully resisted to 10917 * have this cmn_err() "Holy shit -- we executed this code!") 10918 */ 10919 return (pred); 10920 } 10921 10922 pred->dtp_cacheid = dtrace_predcache_id++; 10923 10924 return (pred); 10925 } 10926 10927 static void 10928 dtrace_predicate_hold(dtrace_predicate_t *pred) 10929 { 10930 ASSERT(MUTEX_HELD(&dtrace_lock)); 10931 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 10932 ASSERT(pred->dtp_refcnt > 0); 10933 10934 pred->dtp_refcnt++; 10935 } 10936 10937 static void 10938 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 10939 { 10940 dtrace_difo_t *dp = pred->dtp_difo; 10941 10942 ASSERT(MUTEX_HELD(&dtrace_lock)); 10943 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 10944 ASSERT(pred->dtp_refcnt > 0); 10945 10946 if (--pred->dtp_refcnt == 0) { 10947 dtrace_difo_release(pred->dtp_difo, vstate); 10948 kmem_free(pred, sizeof (dtrace_predicate_t)); 10949 } 10950 } 10951 10952 /* 10953 * DTrace Action Description Functions 10954 */ 10955 static dtrace_actdesc_t * 10956 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 10957 uint64_t uarg, uint64_t arg) 10958 { 10959 dtrace_actdesc_t *act; 10960 10961 #ifdef illumos 10962 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 10963 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 10964 #endif 10965 10966 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 10967 act->dtad_kind = kind; 10968 act->dtad_ntuple = ntuple; 10969 act->dtad_uarg = uarg; 10970 act->dtad_arg = arg; 10971 act->dtad_refcnt = 1; 10972 10973 return (act); 10974 } 10975 10976 static void 10977 dtrace_actdesc_hold(dtrace_actdesc_t *act) 10978 { 10979 ASSERT(act->dtad_refcnt >= 1); 10980 act->dtad_refcnt++; 10981 } 10982 10983 static void 10984 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 10985 { 10986 dtrace_actkind_t kind = act->dtad_kind; 10987 dtrace_difo_t *dp; 10988 10989 ASSERT(act->dtad_refcnt >= 1); 10990 10991 if (--act->dtad_refcnt != 0) 10992 return; 10993 10994 if ((dp = act->dtad_difo) != NULL) 10995 dtrace_difo_release(dp, vstate); 10996 10997 if (DTRACEACT_ISPRINTFLIKE(kind)) { 10998 char *str = (char *)(uintptr_t)act->dtad_arg; 10999 11000 #ifdef illumos 11001 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 11002 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 11003 #endif 11004 11005 if (str != NULL) 11006 kmem_free(str, strlen(str) + 1); 11007 } 11008 11009 kmem_free(act, sizeof (dtrace_actdesc_t)); 11010 } 11011 11012 /* 11013 * DTrace ECB Functions 11014 */ 11015 static dtrace_ecb_t * 11016 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 11017 { 11018 dtrace_ecb_t *ecb; 11019 dtrace_epid_t epid; 11020 11021 ASSERT(MUTEX_HELD(&dtrace_lock)); 11022 11023 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 11024 ecb->dte_predicate = NULL; 11025 ecb->dte_probe = probe; 11026 11027 /* 11028 * The default size is the size of the default action: recording 11029 * the header. 11030 */ 11031 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 11032 ecb->dte_alignment = sizeof (dtrace_epid_t); 11033 11034 epid = state->dts_epid++; 11035 11036 if (epid - 1 >= state->dts_necbs) { 11037 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 11038 int necbs = state->dts_necbs << 1; 11039 11040 ASSERT(epid == state->dts_necbs + 1); 11041 11042 if (necbs == 0) { 11043 ASSERT(oecbs == NULL); 11044 necbs = 1; 11045 } 11046 11047 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 11048 11049 if (oecbs != NULL) 11050 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 11051 11052 dtrace_membar_producer(); 11053 state->dts_ecbs = ecbs; 11054 11055 if (oecbs != NULL) { 11056 /* 11057 * If this state is active, we must dtrace_sync() 11058 * before we can free the old dts_ecbs array: we're 11059 * coming in hot, and there may be active ring 11060 * buffer processing (which indexes into the dts_ecbs 11061 * array) on another CPU. 11062 */ 11063 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11064 dtrace_sync(); 11065 11066 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 11067 } 11068 11069 dtrace_membar_producer(); 11070 state->dts_necbs = necbs; 11071 } 11072 11073 ecb->dte_state = state; 11074 11075 ASSERT(state->dts_ecbs[epid - 1] == NULL); 11076 dtrace_membar_producer(); 11077 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 11078 11079 return (ecb); 11080 } 11081 11082 static void 11083 dtrace_ecb_enable(dtrace_ecb_t *ecb) 11084 { 11085 dtrace_probe_t *probe = ecb->dte_probe; 11086 11087 ASSERT(MUTEX_HELD(&cpu_lock)); 11088 ASSERT(MUTEX_HELD(&dtrace_lock)); 11089 ASSERT(ecb->dte_next == NULL); 11090 11091 if (probe == NULL) { 11092 /* 11093 * This is the NULL probe -- there's nothing to do. 11094 */ 11095 return; 11096 } 11097 11098 if (probe->dtpr_ecb == NULL) { 11099 dtrace_provider_t *prov = probe->dtpr_provider; 11100 11101 /* 11102 * We're the first ECB on this probe. 11103 */ 11104 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 11105 11106 if (ecb->dte_predicate != NULL) 11107 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 11108 11109 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 11110 probe->dtpr_id, probe->dtpr_arg); 11111 } else { 11112 /* 11113 * This probe is already active. Swing the last pointer to 11114 * point to the new ECB, and issue a dtrace_sync() to assure 11115 * that all CPUs have seen the change. 11116 */ 11117 ASSERT(probe->dtpr_ecb_last != NULL); 11118 probe->dtpr_ecb_last->dte_next = ecb; 11119 probe->dtpr_ecb_last = ecb; 11120 probe->dtpr_predcache = 0; 11121 11122 dtrace_sync(); 11123 } 11124 } 11125 11126 static int 11127 dtrace_ecb_resize(dtrace_ecb_t *ecb) 11128 { 11129 dtrace_action_t *act; 11130 uint32_t curneeded = UINT32_MAX; 11131 uint32_t aggbase = UINT32_MAX; 11132 11133 /* 11134 * If we record anything, we always record the dtrace_rechdr_t. (And 11135 * we always record it first.) 11136 */ 11137 ecb->dte_size = sizeof (dtrace_rechdr_t); 11138 ecb->dte_alignment = sizeof (dtrace_epid_t); 11139 11140 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11141 dtrace_recdesc_t *rec = &act->dta_rec; 11142 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 11143 11144 ecb->dte_alignment = MAX(ecb->dte_alignment, 11145 rec->dtrd_alignment); 11146 11147 if (DTRACEACT_ISAGG(act->dta_kind)) { 11148 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11149 11150 ASSERT(rec->dtrd_size != 0); 11151 ASSERT(agg->dtag_first != NULL); 11152 ASSERT(act->dta_prev->dta_intuple); 11153 ASSERT(aggbase != UINT32_MAX); 11154 ASSERT(curneeded != UINT32_MAX); 11155 11156 agg->dtag_base = aggbase; 11157 11158 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11159 rec->dtrd_offset = curneeded; 11160 if (curneeded + rec->dtrd_size < curneeded) 11161 return (EINVAL); 11162 curneeded += rec->dtrd_size; 11163 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 11164 11165 aggbase = UINT32_MAX; 11166 curneeded = UINT32_MAX; 11167 } else if (act->dta_intuple) { 11168 if (curneeded == UINT32_MAX) { 11169 /* 11170 * This is the first record in a tuple. Align 11171 * curneeded to be at offset 4 in an 8-byte 11172 * aligned block. 11173 */ 11174 ASSERT(act->dta_prev == NULL || 11175 !act->dta_prev->dta_intuple); 11176 ASSERT3U(aggbase, ==, UINT32_MAX); 11177 curneeded = P2PHASEUP(ecb->dte_size, 11178 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 11179 11180 aggbase = curneeded - sizeof (dtrace_aggid_t); 11181 ASSERT(IS_P2ALIGNED(aggbase, 11182 sizeof (uint64_t))); 11183 } 11184 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11185 rec->dtrd_offset = curneeded; 11186 if (curneeded + rec->dtrd_size < curneeded) 11187 return (EINVAL); 11188 curneeded += rec->dtrd_size; 11189 } else { 11190 /* tuples must be followed by an aggregation */ 11191 ASSERT(act->dta_prev == NULL || 11192 !act->dta_prev->dta_intuple); 11193 11194 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 11195 rec->dtrd_alignment); 11196 rec->dtrd_offset = ecb->dte_size; 11197 if (ecb->dte_size + rec->dtrd_size < ecb->dte_size) 11198 return (EINVAL); 11199 ecb->dte_size += rec->dtrd_size; 11200 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 11201 } 11202 } 11203 11204 if ((act = ecb->dte_action) != NULL && 11205 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 11206 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 11207 /* 11208 * If the size is still sizeof (dtrace_rechdr_t), then all 11209 * actions store no data; set the size to 0. 11210 */ 11211 ecb->dte_size = 0; 11212 } 11213 11214 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 11215 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 11216 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 11217 ecb->dte_needed); 11218 return (0); 11219 } 11220 11221 static dtrace_action_t * 11222 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11223 { 11224 dtrace_aggregation_t *agg; 11225 size_t size = sizeof (uint64_t); 11226 int ntuple = desc->dtad_ntuple; 11227 dtrace_action_t *act; 11228 dtrace_recdesc_t *frec; 11229 dtrace_aggid_t aggid; 11230 dtrace_state_t *state = ecb->dte_state; 11231 11232 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 11233 agg->dtag_ecb = ecb; 11234 11235 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 11236 11237 switch (desc->dtad_kind) { 11238 case DTRACEAGG_MIN: 11239 agg->dtag_initial = INT64_MAX; 11240 agg->dtag_aggregate = dtrace_aggregate_min; 11241 break; 11242 11243 case DTRACEAGG_MAX: 11244 agg->dtag_initial = INT64_MIN; 11245 agg->dtag_aggregate = dtrace_aggregate_max; 11246 break; 11247 11248 case DTRACEAGG_COUNT: 11249 agg->dtag_aggregate = dtrace_aggregate_count; 11250 break; 11251 11252 case DTRACEAGG_QUANTIZE: 11253 agg->dtag_aggregate = dtrace_aggregate_quantize; 11254 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 11255 sizeof (uint64_t); 11256 break; 11257 11258 case DTRACEAGG_LQUANTIZE: { 11259 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 11260 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 11261 11262 agg->dtag_initial = desc->dtad_arg; 11263 agg->dtag_aggregate = dtrace_aggregate_lquantize; 11264 11265 if (step == 0 || levels == 0) 11266 goto err; 11267 11268 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 11269 break; 11270 } 11271 11272 case DTRACEAGG_LLQUANTIZE: { 11273 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 11274 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 11275 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 11276 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 11277 int64_t v; 11278 11279 agg->dtag_initial = desc->dtad_arg; 11280 agg->dtag_aggregate = dtrace_aggregate_llquantize; 11281 11282 if (factor < 2 || low >= high || nsteps < factor) 11283 goto err; 11284 11285 /* 11286 * Now check that the number of steps evenly divides a power 11287 * of the factor. (This assures both integer bucket size and 11288 * linearity within each magnitude.) 11289 */ 11290 for (v = factor; v < nsteps; v *= factor) 11291 continue; 11292 11293 if ((v % nsteps) || (nsteps % factor)) 11294 goto err; 11295 11296 size = (dtrace_aggregate_llquantize_bucket(factor, 11297 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 11298 break; 11299 } 11300 11301 case DTRACEAGG_AVG: 11302 agg->dtag_aggregate = dtrace_aggregate_avg; 11303 size = sizeof (uint64_t) * 2; 11304 break; 11305 11306 case DTRACEAGG_STDDEV: 11307 agg->dtag_aggregate = dtrace_aggregate_stddev; 11308 size = sizeof (uint64_t) * 4; 11309 break; 11310 11311 case DTRACEAGG_SUM: 11312 agg->dtag_aggregate = dtrace_aggregate_sum; 11313 break; 11314 11315 default: 11316 goto err; 11317 } 11318 11319 agg->dtag_action.dta_rec.dtrd_size = size; 11320 11321 if (ntuple == 0) 11322 goto err; 11323 11324 /* 11325 * We must make sure that we have enough actions for the n-tuple. 11326 */ 11327 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 11328 if (DTRACEACT_ISAGG(act->dta_kind)) 11329 break; 11330 11331 if (--ntuple == 0) { 11332 /* 11333 * This is the action with which our n-tuple begins. 11334 */ 11335 agg->dtag_first = act; 11336 goto success; 11337 } 11338 } 11339 11340 /* 11341 * This n-tuple is short by ntuple elements. Return failure. 11342 */ 11343 ASSERT(ntuple != 0); 11344 err: 11345 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11346 return (NULL); 11347 11348 success: 11349 /* 11350 * If the last action in the tuple has a size of zero, it's actually 11351 * an expression argument for the aggregating action. 11352 */ 11353 ASSERT(ecb->dte_action_last != NULL); 11354 act = ecb->dte_action_last; 11355 11356 if (act->dta_kind == DTRACEACT_DIFEXPR) { 11357 ASSERT(act->dta_difo != NULL); 11358 11359 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 11360 agg->dtag_hasarg = 1; 11361 } 11362 11363 /* 11364 * We need to allocate an id for this aggregation. 11365 */ 11366 #ifdef illumos 11367 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 11368 VM_BESTFIT | VM_SLEEP); 11369 #else 11370 aggid = alloc_unr(state->dts_aggid_arena); 11371 #endif 11372 11373 if (aggid - 1 >= state->dts_naggregations) { 11374 dtrace_aggregation_t **oaggs = state->dts_aggregations; 11375 dtrace_aggregation_t **aggs; 11376 int naggs = state->dts_naggregations << 1; 11377 int onaggs = state->dts_naggregations; 11378 11379 ASSERT(aggid == state->dts_naggregations + 1); 11380 11381 if (naggs == 0) { 11382 ASSERT(oaggs == NULL); 11383 naggs = 1; 11384 } 11385 11386 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 11387 11388 if (oaggs != NULL) { 11389 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 11390 kmem_free(oaggs, onaggs * sizeof (*aggs)); 11391 } 11392 11393 state->dts_aggregations = aggs; 11394 state->dts_naggregations = naggs; 11395 } 11396 11397 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 11398 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 11399 11400 frec = &agg->dtag_first->dta_rec; 11401 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 11402 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 11403 11404 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 11405 ASSERT(!act->dta_intuple); 11406 act->dta_intuple = 1; 11407 } 11408 11409 return (&agg->dtag_action); 11410 } 11411 11412 static void 11413 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 11414 { 11415 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11416 dtrace_state_t *state = ecb->dte_state; 11417 dtrace_aggid_t aggid = agg->dtag_id; 11418 11419 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 11420 #ifdef illumos 11421 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 11422 #else 11423 free_unr(state->dts_aggid_arena, aggid); 11424 #endif 11425 11426 ASSERT(state->dts_aggregations[aggid - 1] == agg); 11427 state->dts_aggregations[aggid - 1] = NULL; 11428 11429 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11430 } 11431 11432 static int 11433 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11434 { 11435 dtrace_action_t *action, *last; 11436 dtrace_difo_t *dp = desc->dtad_difo; 11437 uint32_t size = 0, align = sizeof (uint8_t), mask; 11438 uint16_t format = 0; 11439 dtrace_recdesc_t *rec; 11440 dtrace_state_t *state = ecb->dte_state; 11441 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 11442 uint64_t arg = desc->dtad_arg; 11443 11444 ASSERT(MUTEX_HELD(&dtrace_lock)); 11445 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 11446 11447 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 11448 /* 11449 * If this is an aggregating action, there must be neither 11450 * a speculate nor a commit on the action chain. 11451 */ 11452 dtrace_action_t *act; 11453 11454 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11455 if (act->dta_kind == DTRACEACT_COMMIT) 11456 return (EINVAL); 11457 11458 if (act->dta_kind == DTRACEACT_SPECULATE) 11459 return (EINVAL); 11460 } 11461 11462 action = dtrace_ecb_aggregation_create(ecb, desc); 11463 11464 if (action == NULL) 11465 return (EINVAL); 11466 } else { 11467 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 11468 (desc->dtad_kind == DTRACEACT_DIFEXPR && 11469 dp != NULL && dp->dtdo_destructive)) { 11470 state->dts_destructive = 1; 11471 } 11472 11473 switch (desc->dtad_kind) { 11474 case DTRACEACT_PRINTF: 11475 case DTRACEACT_PRINTA: 11476 case DTRACEACT_SYSTEM: 11477 case DTRACEACT_FREOPEN: 11478 case DTRACEACT_DIFEXPR: 11479 /* 11480 * We know that our arg is a string -- turn it into a 11481 * format. 11482 */ 11483 if (arg == 0) { 11484 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 11485 desc->dtad_kind == DTRACEACT_DIFEXPR); 11486 format = 0; 11487 } else { 11488 ASSERT(arg != 0); 11489 #ifdef illumos 11490 ASSERT(arg > KERNELBASE); 11491 #endif 11492 format = dtrace_format_add(state, 11493 (char *)(uintptr_t)arg); 11494 } 11495 11496 /*FALLTHROUGH*/ 11497 case DTRACEACT_LIBACT: 11498 case DTRACEACT_TRACEMEM: 11499 case DTRACEACT_TRACEMEM_DYNSIZE: 11500 if (dp == NULL) 11501 return (EINVAL); 11502 11503 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 11504 break; 11505 11506 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 11507 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11508 return (EINVAL); 11509 11510 size = opt[DTRACEOPT_STRSIZE]; 11511 } 11512 11513 break; 11514 11515 case DTRACEACT_STACK: 11516 if ((nframes = arg) == 0) { 11517 nframes = opt[DTRACEOPT_STACKFRAMES]; 11518 ASSERT(nframes > 0); 11519 arg = nframes; 11520 } 11521 11522 size = nframes * sizeof (pc_t); 11523 break; 11524 11525 case DTRACEACT_JSTACK: 11526 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 11527 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 11528 11529 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 11530 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 11531 11532 arg = DTRACE_USTACK_ARG(nframes, strsize); 11533 11534 /*FALLTHROUGH*/ 11535 case DTRACEACT_USTACK: 11536 if (desc->dtad_kind != DTRACEACT_JSTACK && 11537 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 11538 strsize = DTRACE_USTACK_STRSIZE(arg); 11539 nframes = opt[DTRACEOPT_USTACKFRAMES]; 11540 ASSERT(nframes > 0); 11541 arg = DTRACE_USTACK_ARG(nframes, strsize); 11542 } 11543 11544 /* 11545 * Save a slot for the pid. 11546 */ 11547 size = (nframes + 1) * sizeof (uint64_t); 11548 size += DTRACE_USTACK_STRSIZE(arg); 11549 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 11550 11551 break; 11552 11553 case DTRACEACT_SYM: 11554 case DTRACEACT_MOD: 11555 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 11556 sizeof (uint64_t)) || 11557 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11558 return (EINVAL); 11559 break; 11560 11561 case DTRACEACT_USYM: 11562 case DTRACEACT_UMOD: 11563 case DTRACEACT_UADDR: 11564 if (dp == NULL || 11565 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 11566 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11567 return (EINVAL); 11568 11569 /* 11570 * We have a slot for the pid, plus a slot for the 11571 * argument. To keep things simple (aligned with 11572 * bitness-neutral sizing), we store each as a 64-bit 11573 * quantity. 11574 */ 11575 size = 2 * sizeof (uint64_t); 11576 break; 11577 11578 case DTRACEACT_STOP: 11579 case DTRACEACT_BREAKPOINT: 11580 case DTRACEACT_PANIC: 11581 break; 11582 11583 case DTRACEACT_CHILL: 11584 case DTRACEACT_DISCARD: 11585 case DTRACEACT_RAISE: 11586 if (dp == NULL) 11587 return (EINVAL); 11588 break; 11589 11590 case DTRACEACT_EXIT: 11591 if (dp == NULL || 11592 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 11593 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11594 return (EINVAL); 11595 break; 11596 11597 case DTRACEACT_SPECULATE: 11598 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 11599 return (EINVAL); 11600 11601 if (dp == NULL) 11602 return (EINVAL); 11603 11604 state->dts_speculates = 1; 11605 break; 11606 11607 case DTRACEACT_PRINTM: 11608 size = dp->dtdo_rtype.dtdt_size; 11609 break; 11610 11611 case DTRACEACT_COMMIT: { 11612 dtrace_action_t *act = ecb->dte_action; 11613 11614 for (; act != NULL; act = act->dta_next) { 11615 if (act->dta_kind == DTRACEACT_COMMIT) 11616 return (EINVAL); 11617 } 11618 11619 if (dp == NULL) 11620 return (EINVAL); 11621 break; 11622 } 11623 11624 default: 11625 return (EINVAL); 11626 } 11627 11628 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 11629 /* 11630 * If this is a data-storing action or a speculate, 11631 * we must be sure that there isn't a commit on the 11632 * action chain. 11633 */ 11634 dtrace_action_t *act = ecb->dte_action; 11635 11636 for (; act != NULL; act = act->dta_next) { 11637 if (act->dta_kind == DTRACEACT_COMMIT) 11638 return (EINVAL); 11639 } 11640 } 11641 11642 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 11643 action->dta_rec.dtrd_size = size; 11644 } 11645 11646 action->dta_refcnt = 1; 11647 rec = &action->dta_rec; 11648 size = rec->dtrd_size; 11649 11650 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 11651 if (!(size & mask)) { 11652 align = mask + 1; 11653 break; 11654 } 11655 } 11656 11657 action->dta_kind = desc->dtad_kind; 11658 11659 if ((action->dta_difo = dp) != NULL) 11660 dtrace_difo_hold(dp); 11661 11662 rec->dtrd_action = action->dta_kind; 11663 rec->dtrd_arg = arg; 11664 rec->dtrd_uarg = desc->dtad_uarg; 11665 rec->dtrd_alignment = (uint16_t)align; 11666 rec->dtrd_format = format; 11667 11668 if ((last = ecb->dte_action_last) != NULL) { 11669 ASSERT(ecb->dte_action != NULL); 11670 action->dta_prev = last; 11671 last->dta_next = action; 11672 } else { 11673 ASSERT(ecb->dte_action == NULL); 11674 ecb->dte_action = action; 11675 } 11676 11677 ecb->dte_action_last = action; 11678 11679 return (0); 11680 } 11681 11682 static void 11683 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 11684 { 11685 dtrace_action_t *act = ecb->dte_action, *next; 11686 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 11687 dtrace_difo_t *dp; 11688 uint16_t format; 11689 11690 if (act != NULL && act->dta_refcnt > 1) { 11691 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 11692 act->dta_refcnt--; 11693 } else { 11694 for (; act != NULL; act = next) { 11695 next = act->dta_next; 11696 ASSERT(next != NULL || act == ecb->dte_action_last); 11697 ASSERT(act->dta_refcnt == 1); 11698 11699 if ((format = act->dta_rec.dtrd_format) != 0) 11700 dtrace_format_remove(ecb->dte_state, format); 11701 11702 if ((dp = act->dta_difo) != NULL) 11703 dtrace_difo_release(dp, vstate); 11704 11705 if (DTRACEACT_ISAGG(act->dta_kind)) { 11706 dtrace_ecb_aggregation_destroy(ecb, act); 11707 } else { 11708 kmem_free(act, sizeof (dtrace_action_t)); 11709 } 11710 } 11711 } 11712 11713 ecb->dte_action = NULL; 11714 ecb->dte_action_last = NULL; 11715 ecb->dte_size = 0; 11716 } 11717 11718 static void 11719 dtrace_ecb_disable(dtrace_ecb_t *ecb) 11720 { 11721 /* 11722 * We disable the ECB by removing it from its probe. 11723 */ 11724 dtrace_ecb_t *pecb, *prev = NULL; 11725 dtrace_probe_t *probe = ecb->dte_probe; 11726 11727 ASSERT(MUTEX_HELD(&dtrace_lock)); 11728 11729 if (probe == NULL) { 11730 /* 11731 * This is the NULL probe; there is nothing to disable. 11732 */ 11733 return; 11734 } 11735 11736 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 11737 if (pecb == ecb) 11738 break; 11739 prev = pecb; 11740 } 11741 11742 ASSERT(pecb != NULL); 11743 11744 if (prev == NULL) { 11745 probe->dtpr_ecb = ecb->dte_next; 11746 } else { 11747 prev->dte_next = ecb->dte_next; 11748 } 11749 11750 if (ecb == probe->dtpr_ecb_last) { 11751 ASSERT(ecb->dte_next == NULL); 11752 probe->dtpr_ecb_last = prev; 11753 } 11754 11755 /* 11756 * The ECB has been disconnected from the probe; now sync to assure 11757 * that all CPUs have seen the change before returning. 11758 */ 11759 dtrace_sync(); 11760 11761 if (probe->dtpr_ecb == NULL) { 11762 /* 11763 * That was the last ECB on the probe; clear the predicate 11764 * cache ID for the probe, disable it and sync one more time 11765 * to assure that we'll never hit it again. 11766 */ 11767 dtrace_provider_t *prov = probe->dtpr_provider; 11768 11769 ASSERT(ecb->dte_next == NULL); 11770 ASSERT(probe->dtpr_ecb_last == NULL); 11771 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 11772 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 11773 probe->dtpr_id, probe->dtpr_arg); 11774 dtrace_sync(); 11775 } else { 11776 /* 11777 * There is at least one ECB remaining on the probe. If there 11778 * is _exactly_ one, set the probe's predicate cache ID to be 11779 * the predicate cache ID of the remaining ECB. 11780 */ 11781 ASSERT(probe->dtpr_ecb_last != NULL); 11782 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 11783 11784 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 11785 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 11786 11787 ASSERT(probe->dtpr_ecb->dte_next == NULL); 11788 11789 if (p != NULL) 11790 probe->dtpr_predcache = p->dtp_cacheid; 11791 } 11792 11793 ecb->dte_next = NULL; 11794 } 11795 } 11796 11797 static void 11798 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 11799 { 11800 dtrace_state_t *state = ecb->dte_state; 11801 dtrace_vstate_t *vstate = &state->dts_vstate; 11802 dtrace_predicate_t *pred; 11803 dtrace_epid_t epid = ecb->dte_epid; 11804 11805 ASSERT(MUTEX_HELD(&dtrace_lock)); 11806 ASSERT(ecb->dte_next == NULL); 11807 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 11808 11809 if ((pred = ecb->dte_predicate) != NULL) 11810 dtrace_predicate_release(pred, vstate); 11811 11812 dtrace_ecb_action_remove(ecb); 11813 11814 ASSERT(state->dts_ecbs[epid - 1] == ecb); 11815 state->dts_ecbs[epid - 1] = NULL; 11816 11817 kmem_free(ecb, sizeof (dtrace_ecb_t)); 11818 } 11819 11820 static dtrace_ecb_t * 11821 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 11822 dtrace_enabling_t *enab) 11823 { 11824 dtrace_ecb_t *ecb; 11825 dtrace_predicate_t *pred; 11826 dtrace_actdesc_t *act; 11827 dtrace_provider_t *prov; 11828 dtrace_ecbdesc_t *desc = enab->dten_current; 11829 11830 ASSERT(MUTEX_HELD(&dtrace_lock)); 11831 ASSERT(state != NULL); 11832 11833 ecb = dtrace_ecb_add(state, probe); 11834 ecb->dte_uarg = desc->dted_uarg; 11835 11836 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 11837 dtrace_predicate_hold(pred); 11838 ecb->dte_predicate = pred; 11839 } 11840 11841 if (probe != NULL) { 11842 /* 11843 * If the provider shows more leg than the consumer is old 11844 * enough to see, we need to enable the appropriate implicit 11845 * predicate bits to prevent the ecb from activating at 11846 * revealing times. 11847 * 11848 * Providers specifying DTRACE_PRIV_USER at register time 11849 * are stating that they need the /proc-style privilege 11850 * model to be enforced, and this is what DTRACE_COND_OWNER 11851 * and DTRACE_COND_ZONEOWNER will then do at probe time. 11852 */ 11853 prov = probe->dtpr_provider; 11854 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 11855 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11856 ecb->dte_cond |= DTRACE_COND_OWNER; 11857 11858 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 11859 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11860 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 11861 11862 /* 11863 * If the provider shows us kernel innards and the user 11864 * is lacking sufficient privilege, enable the 11865 * DTRACE_COND_USERMODE implicit predicate. 11866 */ 11867 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 11868 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 11869 ecb->dte_cond |= DTRACE_COND_USERMODE; 11870 } 11871 11872 if (dtrace_ecb_create_cache != NULL) { 11873 /* 11874 * If we have a cached ecb, we'll use its action list instead 11875 * of creating our own (saving both time and space). 11876 */ 11877 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 11878 dtrace_action_t *act = cached->dte_action; 11879 11880 if (act != NULL) { 11881 ASSERT(act->dta_refcnt > 0); 11882 act->dta_refcnt++; 11883 ecb->dte_action = act; 11884 ecb->dte_action_last = cached->dte_action_last; 11885 ecb->dte_needed = cached->dte_needed; 11886 ecb->dte_size = cached->dte_size; 11887 ecb->dte_alignment = cached->dte_alignment; 11888 } 11889 11890 return (ecb); 11891 } 11892 11893 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 11894 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 11895 dtrace_ecb_destroy(ecb); 11896 return (NULL); 11897 } 11898 } 11899 11900 if ((enab->dten_error = dtrace_ecb_resize(ecb)) != 0) { 11901 dtrace_ecb_destroy(ecb); 11902 return (NULL); 11903 } 11904 11905 return (dtrace_ecb_create_cache = ecb); 11906 } 11907 11908 static int 11909 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 11910 { 11911 dtrace_ecb_t *ecb; 11912 dtrace_enabling_t *enab = arg; 11913 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 11914 11915 ASSERT(state != NULL); 11916 11917 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 11918 /* 11919 * This probe was created in a generation for which this 11920 * enabling has previously created ECBs; we don't want to 11921 * enable it again, so just kick out. 11922 */ 11923 return (DTRACE_MATCH_NEXT); 11924 } 11925 11926 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 11927 return (DTRACE_MATCH_DONE); 11928 11929 dtrace_ecb_enable(ecb); 11930 return (DTRACE_MATCH_NEXT); 11931 } 11932 11933 static dtrace_ecb_t * 11934 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 11935 { 11936 dtrace_ecb_t *ecb; 11937 11938 ASSERT(MUTEX_HELD(&dtrace_lock)); 11939 11940 if (id == 0 || id > state->dts_necbs) 11941 return (NULL); 11942 11943 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 11944 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 11945 11946 return (state->dts_ecbs[id - 1]); 11947 } 11948 11949 static dtrace_aggregation_t * 11950 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 11951 { 11952 dtrace_aggregation_t *agg; 11953 11954 ASSERT(MUTEX_HELD(&dtrace_lock)); 11955 11956 if (id == 0 || id > state->dts_naggregations) 11957 return (NULL); 11958 11959 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 11960 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 11961 agg->dtag_id == id); 11962 11963 return (state->dts_aggregations[id - 1]); 11964 } 11965 11966 /* 11967 * DTrace Buffer Functions 11968 * 11969 * The following functions manipulate DTrace buffers. Most of these functions 11970 * are called in the context of establishing or processing consumer state; 11971 * exceptions are explicitly noted. 11972 */ 11973 11974 /* 11975 * Note: called from cross call context. This function switches the two 11976 * buffers on a given CPU. The atomicity of this operation is assured by 11977 * disabling interrupts while the actual switch takes place; the disabling of 11978 * interrupts serializes the execution with any execution of dtrace_probe() on 11979 * the same CPU. 11980 */ 11981 static void 11982 dtrace_buffer_switch(dtrace_buffer_t *buf) 11983 { 11984 caddr_t tomax = buf->dtb_tomax; 11985 caddr_t xamot = buf->dtb_xamot; 11986 dtrace_icookie_t cookie; 11987 hrtime_t now; 11988 11989 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11990 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 11991 11992 cookie = dtrace_interrupt_disable(); 11993 now = dtrace_gethrtime(); 11994 buf->dtb_tomax = xamot; 11995 buf->dtb_xamot = tomax; 11996 buf->dtb_xamot_drops = buf->dtb_drops; 11997 buf->dtb_xamot_offset = buf->dtb_offset; 11998 buf->dtb_xamot_errors = buf->dtb_errors; 11999 buf->dtb_xamot_flags = buf->dtb_flags; 12000 buf->dtb_offset = 0; 12001 buf->dtb_drops = 0; 12002 buf->dtb_errors = 0; 12003 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 12004 buf->dtb_interval = now - buf->dtb_switched; 12005 buf->dtb_switched = now; 12006 dtrace_interrupt_enable(cookie); 12007 } 12008 12009 /* 12010 * Note: called from cross call context. This function activates a buffer 12011 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 12012 * is guaranteed by the disabling of interrupts. 12013 */ 12014 static void 12015 dtrace_buffer_activate(dtrace_state_t *state) 12016 { 12017 dtrace_buffer_t *buf; 12018 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 12019 12020 buf = &state->dts_buffer[curcpu]; 12021 12022 if (buf->dtb_tomax != NULL) { 12023 /* 12024 * We might like to assert that the buffer is marked inactive, 12025 * but this isn't necessarily true: the buffer for the CPU 12026 * that processes the BEGIN probe has its buffer activated 12027 * manually. In this case, we take the (harmless) action 12028 * re-clearing the bit INACTIVE bit. 12029 */ 12030 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 12031 } 12032 12033 dtrace_interrupt_enable(cookie); 12034 } 12035 12036 #ifdef __FreeBSD__ 12037 /* 12038 * Activate the specified per-CPU buffer. This is used instead of 12039 * dtrace_buffer_activate() when APs have not yet started, i.e. when 12040 * activating anonymous state. 12041 */ 12042 static void 12043 dtrace_buffer_activate_cpu(dtrace_state_t *state, int cpu) 12044 { 12045 12046 if (state->dts_buffer[cpu].dtb_tomax != NULL) 12047 state->dts_buffer[cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 12048 } 12049 #endif 12050 12051 static int 12052 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 12053 processorid_t cpu, int *factor) 12054 { 12055 #ifdef illumos 12056 cpu_t *cp; 12057 #endif 12058 dtrace_buffer_t *buf; 12059 int allocated = 0, desired = 0; 12060 12061 #ifdef illumos 12062 ASSERT(MUTEX_HELD(&cpu_lock)); 12063 ASSERT(MUTEX_HELD(&dtrace_lock)); 12064 12065 *factor = 1; 12066 12067 if (size > dtrace_nonroot_maxsize && 12068 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 12069 return (EFBIG); 12070 12071 cp = cpu_list; 12072 12073 do { 12074 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 12075 continue; 12076 12077 buf = &bufs[cp->cpu_id]; 12078 12079 /* 12080 * If there is already a buffer allocated for this CPU, it 12081 * is only possible that this is a DR event. In this case, 12082 */ 12083 if (buf->dtb_tomax != NULL) { 12084 ASSERT(buf->dtb_size == size); 12085 continue; 12086 } 12087 12088 ASSERT(buf->dtb_xamot == NULL); 12089 12090 if ((buf->dtb_tomax = kmem_zalloc(size, 12091 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12092 goto err; 12093 12094 buf->dtb_size = size; 12095 buf->dtb_flags = flags; 12096 buf->dtb_offset = 0; 12097 buf->dtb_drops = 0; 12098 12099 if (flags & DTRACEBUF_NOSWITCH) 12100 continue; 12101 12102 if ((buf->dtb_xamot = kmem_zalloc(size, 12103 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12104 goto err; 12105 } while ((cp = cp->cpu_next) != cpu_list); 12106 12107 return (0); 12108 12109 err: 12110 cp = cpu_list; 12111 12112 do { 12113 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 12114 continue; 12115 12116 buf = &bufs[cp->cpu_id]; 12117 desired += 2; 12118 12119 if (buf->dtb_xamot != NULL) { 12120 ASSERT(buf->dtb_tomax != NULL); 12121 ASSERT(buf->dtb_size == size); 12122 kmem_free(buf->dtb_xamot, size); 12123 allocated++; 12124 } 12125 12126 if (buf->dtb_tomax != NULL) { 12127 ASSERT(buf->dtb_size == size); 12128 kmem_free(buf->dtb_tomax, size); 12129 allocated++; 12130 } 12131 12132 buf->dtb_tomax = NULL; 12133 buf->dtb_xamot = NULL; 12134 buf->dtb_size = 0; 12135 } while ((cp = cp->cpu_next) != cpu_list); 12136 #else 12137 int i; 12138 12139 *factor = 1; 12140 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \ 12141 defined(__mips__) || defined(__powerpc__) || defined(__riscv) 12142 /* 12143 * FreeBSD isn't good at limiting the amount of memory we 12144 * ask to malloc, so let's place a limit here before trying 12145 * to do something that might well end in tears at bedtime. 12146 */ 12147 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 12148 return (ENOMEM); 12149 #endif 12150 12151 ASSERT(MUTEX_HELD(&dtrace_lock)); 12152 CPU_FOREACH(i) { 12153 if (cpu != DTRACE_CPUALL && cpu != i) 12154 continue; 12155 12156 buf = &bufs[i]; 12157 12158 /* 12159 * If there is already a buffer allocated for this CPU, it 12160 * is only possible that this is a DR event. In this case, 12161 * the buffer size must match our specified size. 12162 */ 12163 if (buf->dtb_tomax != NULL) { 12164 ASSERT(buf->dtb_size == size); 12165 continue; 12166 } 12167 12168 ASSERT(buf->dtb_xamot == NULL); 12169 12170 if ((buf->dtb_tomax = kmem_zalloc(size, 12171 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12172 goto err; 12173 12174 buf->dtb_size = size; 12175 buf->dtb_flags = flags; 12176 buf->dtb_offset = 0; 12177 buf->dtb_drops = 0; 12178 12179 if (flags & DTRACEBUF_NOSWITCH) 12180 continue; 12181 12182 if ((buf->dtb_xamot = kmem_zalloc(size, 12183 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12184 goto err; 12185 } 12186 12187 return (0); 12188 12189 err: 12190 /* 12191 * Error allocating memory, so free the buffers that were 12192 * allocated before the failed allocation. 12193 */ 12194 CPU_FOREACH(i) { 12195 if (cpu != DTRACE_CPUALL && cpu != i) 12196 continue; 12197 12198 buf = &bufs[i]; 12199 desired += 2; 12200 12201 if (buf->dtb_xamot != NULL) { 12202 ASSERT(buf->dtb_tomax != NULL); 12203 ASSERT(buf->dtb_size == size); 12204 kmem_free(buf->dtb_xamot, size); 12205 allocated++; 12206 } 12207 12208 if (buf->dtb_tomax != NULL) { 12209 ASSERT(buf->dtb_size == size); 12210 kmem_free(buf->dtb_tomax, size); 12211 allocated++; 12212 } 12213 12214 buf->dtb_tomax = NULL; 12215 buf->dtb_xamot = NULL; 12216 buf->dtb_size = 0; 12217 12218 } 12219 #endif 12220 *factor = desired / (allocated > 0 ? allocated : 1); 12221 12222 return (ENOMEM); 12223 } 12224 12225 /* 12226 * Note: called from probe context. This function just increments the drop 12227 * count on a buffer. It has been made a function to allow for the 12228 * possibility of understanding the source of mysterious drop counts. (A 12229 * problem for which one may be particularly disappointed that DTrace cannot 12230 * be used to understand DTrace.) 12231 */ 12232 static void 12233 dtrace_buffer_drop(dtrace_buffer_t *buf) 12234 { 12235 buf->dtb_drops++; 12236 } 12237 12238 /* 12239 * Note: called from probe context. This function is called to reserve space 12240 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 12241 * mstate. Returns the new offset in the buffer, or a negative value if an 12242 * error has occurred. 12243 */ 12244 static intptr_t 12245 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 12246 dtrace_state_t *state, dtrace_mstate_t *mstate) 12247 { 12248 intptr_t offs = buf->dtb_offset, soffs; 12249 intptr_t woffs; 12250 caddr_t tomax; 12251 size_t total; 12252 12253 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 12254 return (-1); 12255 12256 if ((tomax = buf->dtb_tomax) == NULL) { 12257 dtrace_buffer_drop(buf); 12258 return (-1); 12259 } 12260 12261 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 12262 while (offs & (align - 1)) { 12263 /* 12264 * Assert that our alignment is off by a number which 12265 * is itself sizeof (uint32_t) aligned. 12266 */ 12267 ASSERT(!((align - (offs & (align - 1))) & 12268 (sizeof (uint32_t) - 1))); 12269 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12270 offs += sizeof (uint32_t); 12271 } 12272 12273 if ((soffs = offs + needed) > buf->dtb_size) { 12274 dtrace_buffer_drop(buf); 12275 return (-1); 12276 } 12277 12278 if (mstate == NULL) 12279 return (offs); 12280 12281 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 12282 mstate->dtms_scratch_size = buf->dtb_size - soffs; 12283 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12284 12285 return (offs); 12286 } 12287 12288 if (buf->dtb_flags & DTRACEBUF_FILL) { 12289 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 12290 (buf->dtb_flags & DTRACEBUF_FULL)) 12291 return (-1); 12292 goto out; 12293 } 12294 12295 total = needed + (offs & (align - 1)); 12296 12297 /* 12298 * For a ring buffer, life is quite a bit more complicated. Before 12299 * we can store any padding, we need to adjust our wrapping offset. 12300 * (If we've never before wrapped or we're not about to, no adjustment 12301 * is required.) 12302 */ 12303 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 12304 offs + total > buf->dtb_size) { 12305 woffs = buf->dtb_xamot_offset; 12306 12307 if (offs + total > buf->dtb_size) { 12308 /* 12309 * We can't fit in the end of the buffer. First, a 12310 * sanity check that we can fit in the buffer at all. 12311 */ 12312 if (total > buf->dtb_size) { 12313 dtrace_buffer_drop(buf); 12314 return (-1); 12315 } 12316 12317 /* 12318 * We're going to be storing at the top of the buffer, 12319 * so now we need to deal with the wrapped offset. We 12320 * only reset our wrapped offset to 0 if it is 12321 * currently greater than the current offset. If it 12322 * is less than the current offset, it is because a 12323 * previous allocation induced a wrap -- but the 12324 * allocation didn't subsequently take the space due 12325 * to an error or false predicate evaluation. In this 12326 * case, we'll just leave the wrapped offset alone: if 12327 * the wrapped offset hasn't been advanced far enough 12328 * for this allocation, it will be adjusted in the 12329 * lower loop. 12330 */ 12331 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 12332 if (woffs >= offs) 12333 woffs = 0; 12334 } else { 12335 woffs = 0; 12336 } 12337 12338 /* 12339 * Now we know that we're going to be storing to the 12340 * top of the buffer and that there is room for us 12341 * there. We need to clear the buffer from the current 12342 * offset to the end (there may be old gunk there). 12343 */ 12344 while (offs < buf->dtb_size) 12345 tomax[offs++] = 0; 12346 12347 /* 12348 * We need to set our offset to zero. And because we 12349 * are wrapping, we need to set the bit indicating as 12350 * much. We can also adjust our needed space back 12351 * down to the space required by the ECB -- we know 12352 * that the top of the buffer is aligned. 12353 */ 12354 offs = 0; 12355 total = needed; 12356 buf->dtb_flags |= DTRACEBUF_WRAPPED; 12357 } else { 12358 /* 12359 * There is room for us in the buffer, so we simply 12360 * need to check the wrapped offset. 12361 */ 12362 if (woffs < offs) { 12363 /* 12364 * The wrapped offset is less than the offset. 12365 * This can happen if we allocated buffer space 12366 * that induced a wrap, but then we didn't 12367 * subsequently take the space due to an error 12368 * or false predicate evaluation. This is 12369 * okay; we know that _this_ allocation isn't 12370 * going to induce a wrap. We still can't 12371 * reset the wrapped offset to be zero, 12372 * however: the space may have been trashed in 12373 * the previous failed probe attempt. But at 12374 * least the wrapped offset doesn't need to 12375 * be adjusted at all... 12376 */ 12377 goto out; 12378 } 12379 } 12380 12381 while (offs + total > woffs) { 12382 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 12383 size_t size; 12384 12385 if (epid == DTRACE_EPIDNONE) { 12386 size = sizeof (uint32_t); 12387 } else { 12388 ASSERT3U(epid, <=, state->dts_necbs); 12389 ASSERT(state->dts_ecbs[epid - 1] != NULL); 12390 12391 size = state->dts_ecbs[epid - 1]->dte_size; 12392 } 12393 12394 ASSERT(woffs + size <= buf->dtb_size); 12395 ASSERT(size != 0); 12396 12397 if (woffs + size == buf->dtb_size) { 12398 /* 12399 * We've reached the end of the buffer; we want 12400 * to set the wrapped offset to 0 and break 12401 * out. However, if the offs is 0, then we're 12402 * in a strange edge-condition: the amount of 12403 * space that we want to reserve plus the size 12404 * of the record that we're overwriting is 12405 * greater than the size of the buffer. This 12406 * is problematic because if we reserve the 12407 * space but subsequently don't consume it (due 12408 * to a failed predicate or error) the wrapped 12409 * offset will be 0 -- yet the EPID at offset 0 12410 * will not be committed. This situation is 12411 * relatively easy to deal with: if we're in 12412 * this case, the buffer is indistinguishable 12413 * from one that hasn't wrapped; we need only 12414 * finish the job by clearing the wrapped bit, 12415 * explicitly setting the offset to be 0, and 12416 * zero'ing out the old data in the buffer. 12417 */ 12418 if (offs == 0) { 12419 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 12420 buf->dtb_offset = 0; 12421 woffs = total; 12422 12423 while (woffs < buf->dtb_size) 12424 tomax[woffs++] = 0; 12425 } 12426 12427 woffs = 0; 12428 break; 12429 } 12430 12431 woffs += size; 12432 } 12433 12434 /* 12435 * We have a wrapped offset. It may be that the wrapped offset 12436 * has become zero -- that's okay. 12437 */ 12438 buf->dtb_xamot_offset = woffs; 12439 } 12440 12441 out: 12442 /* 12443 * Now we can plow the buffer with any necessary padding. 12444 */ 12445 while (offs & (align - 1)) { 12446 /* 12447 * Assert that our alignment is off by a number which 12448 * is itself sizeof (uint32_t) aligned. 12449 */ 12450 ASSERT(!((align - (offs & (align - 1))) & 12451 (sizeof (uint32_t) - 1))); 12452 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12453 offs += sizeof (uint32_t); 12454 } 12455 12456 if (buf->dtb_flags & DTRACEBUF_FILL) { 12457 if (offs + needed > buf->dtb_size - state->dts_reserve) { 12458 buf->dtb_flags |= DTRACEBUF_FULL; 12459 return (-1); 12460 } 12461 } 12462 12463 if (mstate == NULL) 12464 return (offs); 12465 12466 /* 12467 * For ring buffers and fill buffers, the scratch space is always 12468 * the inactive buffer. 12469 */ 12470 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 12471 mstate->dtms_scratch_size = buf->dtb_size; 12472 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12473 12474 return (offs); 12475 } 12476 12477 static void 12478 dtrace_buffer_polish(dtrace_buffer_t *buf) 12479 { 12480 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 12481 ASSERT(MUTEX_HELD(&dtrace_lock)); 12482 12483 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 12484 return; 12485 12486 /* 12487 * We need to polish the ring buffer. There are three cases: 12488 * 12489 * - The first (and presumably most common) is that there is no gap 12490 * between the buffer offset and the wrapped offset. In this case, 12491 * there is nothing in the buffer that isn't valid data; we can 12492 * mark the buffer as polished and return. 12493 * 12494 * - The second (less common than the first but still more common 12495 * than the third) is that there is a gap between the buffer offset 12496 * and the wrapped offset, and the wrapped offset is larger than the 12497 * buffer offset. This can happen because of an alignment issue, or 12498 * can happen because of a call to dtrace_buffer_reserve() that 12499 * didn't subsequently consume the buffer space. In this case, 12500 * we need to zero the data from the buffer offset to the wrapped 12501 * offset. 12502 * 12503 * - The third (and least common) is that there is a gap between the 12504 * buffer offset and the wrapped offset, but the wrapped offset is 12505 * _less_ than the buffer offset. This can only happen because a 12506 * call to dtrace_buffer_reserve() induced a wrap, but the space 12507 * was not subsequently consumed. In this case, we need to zero the 12508 * space from the offset to the end of the buffer _and_ from the 12509 * top of the buffer to the wrapped offset. 12510 */ 12511 if (buf->dtb_offset < buf->dtb_xamot_offset) { 12512 bzero(buf->dtb_tomax + buf->dtb_offset, 12513 buf->dtb_xamot_offset - buf->dtb_offset); 12514 } 12515 12516 if (buf->dtb_offset > buf->dtb_xamot_offset) { 12517 bzero(buf->dtb_tomax + buf->dtb_offset, 12518 buf->dtb_size - buf->dtb_offset); 12519 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 12520 } 12521 } 12522 12523 /* 12524 * This routine determines if data generated at the specified time has likely 12525 * been entirely consumed at user-level. This routine is called to determine 12526 * if an ECB on a defunct probe (but for an active enabling) can be safely 12527 * disabled and destroyed. 12528 */ 12529 static int 12530 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 12531 { 12532 int i; 12533 12534 for (i = 0; i < NCPU; i++) { 12535 dtrace_buffer_t *buf = &bufs[i]; 12536 12537 if (buf->dtb_size == 0) 12538 continue; 12539 12540 if (buf->dtb_flags & DTRACEBUF_RING) 12541 return (0); 12542 12543 if (!buf->dtb_switched && buf->dtb_offset != 0) 12544 return (0); 12545 12546 if (buf->dtb_switched - buf->dtb_interval < when) 12547 return (0); 12548 } 12549 12550 return (1); 12551 } 12552 12553 static void 12554 dtrace_buffer_free(dtrace_buffer_t *bufs) 12555 { 12556 int i; 12557 12558 for (i = 0; i < NCPU; i++) { 12559 dtrace_buffer_t *buf = &bufs[i]; 12560 12561 if (buf->dtb_tomax == NULL) { 12562 ASSERT(buf->dtb_xamot == NULL); 12563 ASSERT(buf->dtb_size == 0); 12564 continue; 12565 } 12566 12567 if (buf->dtb_xamot != NULL) { 12568 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 12569 kmem_free(buf->dtb_xamot, buf->dtb_size); 12570 } 12571 12572 kmem_free(buf->dtb_tomax, buf->dtb_size); 12573 buf->dtb_size = 0; 12574 buf->dtb_tomax = NULL; 12575 buf->dtb_xamot = NULL; 12576 } 12577 } 12578 12579 /* 12580 * DTrace Enabling Functions 12581 */ 12582 static dtrace_enabling_t * 12583 dtrace_enabling_create(dtrace_vstate_t *vstate) 12584 { 12585 dtrace_enabling_t *enab; 12586 12587 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 12588 enab->dten_vstate = vstate; 12589 12590 return (enab); 12591 } 12592 12593 static void 12594 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 12595 { 12596 dtrace_ecbdesc_t **ndesc; 12597 size_t osize, nsize; 12598 12599 /* 12600 * We can't add to enablings after we've enabled them, or after we've 12601 * retained them. 12602 */ 12603 ASSERT(enab->dten_probegen == 0); 12604 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12605 12606 if (enab->dten_ndesc < enab->dten_maxdesc) { 12607 enab->dten_desc[enab->dten_ndesc++] = ecb; 12608 return; 12609 } 12610 12611 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12612 12613 if (enab->dten_maxdesc == 0) { 12614 enab->dten_maxdesc = 1; 12615 } else { 12616 enab->dten_maxdesc <<= 1; 12617 } 12618 12619 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 12620 12621 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12622 ndesc = kmem_zalloc(nsize, KM_SLEEP); 12623 bcopy(enab->dten_desc, ndesc, osize); 12624 if (enab->dten_desc != NULL) 12625 kmem_free(enab->dten_desc, osize); 12626 12627 enab->dten_desc = ndesc; 12628 enab->dten_desc[enab->dten_ndesc++] = ecb; 12629 } 12630 12631 static void 12632 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 12633 dtrace_probedesc_t *pd) 12634 { 12635 dtrace_ecbdesc_t *new; 12636 dtrace_predicate_t *pred; 12637 dtrace_actdesc_t *act; 12638 12639 /* 12640 * We're going to create a new ECB description that matches the 12641 * specified ECB in every way, but has the specified probe description. 12642 */ 12643 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12644 12645 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 12646 dtrace_predicate_hold(pred); 12647 12648 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 12649 dtrace_actdesc_hold(act); 12650 12651 new->dted_action = ecb->dted_action; 12652 new->dted_pred = ecb->dted_pred; 12653 new->dted_probe = *pd; 12654 new->dted_uarg = ecb->dted_uarg; 12655 12656 dtrace_enabling_add(enab, new); 12657 } 12658 12659 static void 12660 dtrace_enabling_dump(dtrace_enabling_t *enab) 12661 { 12662 int i; 12663 12664 for (i = 0; i < enab->dten_ndesc; i++) { 12665 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 12666 12667 #ifdef __FreeBSD__ 12668 printf("dtrace: enabling probe %d (%s:%s:%s:%s)\n", i, 12669 desc->dtpd_provider, desc->dtpd_mod, 12670 desc->dtpd_func, desc->dtpd_name); 12671 #else 12672 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 12673 desc->dtpd_provider, desc->dtpd_mod, 12674 desc->dtpd_func, desc->dtpd_name); 12675 #endif 12676 } 12677 } 12678 12679 static void 12680 dtrace_enabling_destroy(dtrace_enabling_t *enab) 12681 { 12682 int i; 12683 dtrace_ecbdesc_t *ep; 12684 dtrace_vstate_t *vstate = enab->dten_vstate; 12685 12686 ASSERT(MUTEX_HELD(&dtrace_lock)); 12687 12688 for (i = 0; i < enab->dten_ndesc; i++) { 12689 dtrace_actdesc_t *act, *next; 12690 dtrace_predicate_t *pred; 12691 12692 ep = enab->dten_desc[i]; 12693 12694 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 12695 dtrace_predicate_release(pred, vstate); 12696 12697 for (act = ep->dted_action; act != NULL; act = next) { 12698 next = act->dtad_next; 12699 dtrace_actdesc_release(act, vstate); 12700 } 12701 12702 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12703 } 12704 12705 if (enab->dten_desc != NULL) 12706 kmem_free(enab->dten_desc, 12707 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 12708 12709 /* 12710 * If this was a retained enabling, decrement the dts_nretained count 12711 * and take it off of the dtrace_retained list. 12712 */ 12713 if (enab->dten_prev != NULL || enab->dten_next != NULL || 12714 dtrace_retained == enab) { 12715 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12716 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 12717 enab->dten_vstate->dtvs_state->dts_nretained--; 12718 dtrace_retained_gen++; 12719 } 12720 12721 if (enab->dten_prev == NULL) { 12722 if (dtrace_retained == enab) { 12723 dtrace_retained = enab->dten_next; 12724 12725 if (dtrace_retained != NULL) 12726 dtrace_retained->dten_prev = NULL; 12727 } 12728 } else { 12729 ASSERT(enab != dtrace_retained); 12730 ASSERT(dtrace_retained != NULL); 12731 enab->dten_prev->dten_next = enab->dten_next; 12732 } 12733 12734 if (enab->dten_next != NULL) { 12735 ASSERT(dtrace_retained != NULL); 12736 enab->dten_next->dten_prev = enab->dten_prev; 12737 } 12738 12739 kmem_free(enab, sizeof (dtrace_enabling_t)); 12740 } 12741 12742 static int 12743 dtrace_enabling_retain(dtrace_enabling_t *enab) 12744 { 12745 dtrace_state_t *state; 12746 12747 ASSERT(MUTEX_HELD(&dtrace_lock)); 12748 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12749 ASSERT(enab->dten_vstate != NULL); 12750 12751 state = enab->dten_vstate->dtvs_state; 12752 ASSERT(state != NULL); 12753 12754 /* 12755 * We only allow each state to retain dtrace_retain_max enablings. 12756 */ 12757 if (state->dts_nretained >= dtrace_retain_max) 12758 return (ENOSPC); 12759 12760 state->dts_nretained++; 12761 dtrace_retained_gen++; 12762 12763 if (dtrace_retained == NULL) { 12764 dtrace_retained = enab; 12765 return (0); 12766 } 12767 12768 enab->dten_next = dtrace_retained; 12769 dtrace_retained->dten_prev = enab; 12770 dtrace_retained = enab; 12771 12772 return (0); 12773 } 12774 12775 static int 12776 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 12777 dtrace_probedesc_t *create) 12778 { 12779 dtrace_enabling_t *new, *enab; 12780 int found = 0, err = ENOENT; 12781 12782 ASSERT(MUTEX_HELD(&dtrace_lock)); 12783 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 12784 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 12785 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 12786 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 12787 12788 new = dtrace_enabling_create(&state->dts_vstate); 12789 12790 /* 12791 * Iterate over all retained enablings, looking for enablings that 12792 * match the specified state. 12793 */ 12794 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12795 int i; 12796 12797 /* 12798 * dtvs_state can only be NULL for helper enablings -- and 12799 * helper enablings can't be retained. 12800 */ 12801 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12802 12803 if (enab->dten_vstate->dtvs_state != state) 12804 continue; 12805 12806 /* 12807 * Now iterate over each probe description; we're looking for 12808 * an exact match to the specified probe description. 12809 */ 12810 for (i = 0; i < enab->dten_ndesc; i++) { 12811 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12812 dtrace_probedesc_t *pd = &ep->dted_probe; 12813 12814 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 12815 continue; 12816 12817 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 12818 continue; 12819 12820 if (strcmp(pd->dtpd_func, match->dtpd_func)) 12821 continue; 12822 12823 if (strcmp(pd->dtpd_name, match->dtpd_name)) 12824 continue; 12825 12826 /* 12827 * We have a winning probe! Add it to our growing 12828 * enabling. 12829 */ 12830 found = 1; 12831 dtrace_enabling_addlike(new, ep, create); 12832 } 12833 } 12834 12835 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 12836 dtrace_enabling_destroy(new); 12837 return (err); 12838 } 12839 12840 return (0); 12841 } 12842 12843 static void 12844 dtrace_enabling_retract(dtrace_state_t *state) 12845 { 12846 dtrace_enabling_t *enab, *next; 12847 12848 ASSERT(MUTEX_HELD(&dtrace_lock)); 12849 12850 /* 12851 * Iterate over all retained enablings, destroy the enablings retained 12852 * for the specified state. 12853 */ 12854 for (enab = dtrace_retained; enab != NULL; enab = next) { 12855 next = enab->dten_next; 12856 12857 /* 12858 * dtvs_state can only be NULL for helper enablings -- and 12859 * helper enablings can't be retained. 12860 */ 12861 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12862 12863 if (enab->dten_vstate->dtvs_state == state) { 12864 ASSERT(state->dts_nretained > 0); 12865 dtrace_enabling_destroy(enab); 12866 } 12867 } 12868 12869 ASSERT(state->dts_nretained == 0); 12870 } 12871 12872 static int 12873 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 12874 { 12875 int i = 0; 12876 int matched = 0; 12877 12878 ASSERT(MUTEX_HELD(&cpu_lock)); 12879 ASSERT(MUTEX_HELD(&dtrace_lock)); 12880 12881 for (i = 0; i < enab->dten_ndesc; i++) { 12882 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12883 12884 enab->dten_current = ep; 12885 enab->dten_error = 0; 12886 12887 matched += dtrace_probe_enable(&ep->dted_probe, enab); 12888 12889 if (enab->dten_error != 0) { 12890 /* 12891 * If we get an error half-way through enabling the 12892 * probes, we kick out -- perhaps with some number of 12893 * them enabled. Leaving enabled probes enabled may 12894 * be slightly confusing for user-level, but we expect 12895 * that no one will attempt to actually drive on in 12896 * the face of such errors. If this is an anonymous 12897 * enabling (indicated with a NULL nmatched pointer), 12898 * we cmn_err() a message. We aren't expecting to 12899 * get such an error -- such as it can exist at all, 12900 * it would be a result of corrupted DOF in the driver 12901 * properties. 12902 */ 12903 if (nmatched == NULL) { 12904 cmn_err(CE_WARN, "dtrace_enabling_match() " 12905 "error on %p: %d", (void *)ep, 12906 enab->dten_error); 12907 } 12908 12909 return (enab->dten_error); 12910 } 12911 } 12912 12913 enab->dten_probegen = dtrace_probegen; 12914 if (nmatched != NULL) 12915 *nmatched = matched; 12916 12917 return (0); 12918 } 12919 12920 static void 12921 dtrace_enabling_matchall(void) 12922 { 12923 dtrace_enabling_t *enab; 12924 12925 mutex_enter(&cpu_lock); 12926 mutex_enter(&dtrace_lock); 12927 12928 /* 12929 * Iterate over all retained enablings to see if any probes match 12930 * against them. We only perform this operation on enablings for which 12931 * we have sufficient permissions by virtue of being in the global zone 12932 * or in the same zone as the DTrace client. Because we can be called 12933 * after dtrace_detach() has been called, we cannot assert that there 12934 * are retained enablings. We can safely load from dtrace_retained, 12935 * however: the taskq_destroy() at the end of dtrace_detach() will 12936 * block pending our completion. 12937 */ 12938 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12939 #ifdef illumos 12940 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 12941 12942 if (INGLOBALZONE(curproc) || 12943 cr != NULL && getzoneid() == crgetzoneid(cr)) 12944 #endif 12945 (void) dtrace_enabling_match(enab, NULL); 12946 } 12947 12948 mutex_exit(&dtrace_lock); 12949 mutex_exit(&cpu_lock); 12950 } 12951 12952 /* 12953 * If an enabling is to be enabled without having matched probes (that is, if 12954 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 12955 * enabling must be _primed_ by creating an ECB for every ECB description. 12956 * This must be done to assure that we know the number of speculations, the 12957 * number of aggregations, the minimum buffer size needed, etc. before we 12958 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 12959 * enabling any probes, we create ECBs for every ECB decription, but with a 12960 * NULL probe -- which is exactly what this function does. 12961 */ 12962 static void 12963 dtrace_enabling_prime(dtrace_state_t *state) 12964 { 12965 dtrace_enabling_t *enab; 12966 int i; 12967 12968 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12969 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12970 12971 if (enab->dten_vstate->dtvs_state != state) 12972 continue; 12973 12974 /* 12975 * We don't want to prime an enabling more than once, lest 12976 * we allow a malicious user to induce resource exhaustion. 12977 * (The ECBs that result from priming an enabling aren't 12978 * leaked -- but they also aren't deallocated until the 12979 * consumer state is destroyed.) 12980 */ 12981 if (enab->dten_primed) 12982 continue; 12983 12984 for (i = 0; i < enab->dten_ndesc; i++) { 12985 enab->dten_current = enab->dten_desc[i]; 12986 (void) dtrace_probe_enable(NULL, enab); 12987 } 12988 12989 enab->dten_primed = 1; 12990 } 12991 } 12992 12993 /* 12994 * Called to indicate that probes should be provided due to retained 12995 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 12996 * must take an initial lap through the enabling calling the dtps_provide() 12997 * entry point explicitly to allow for autocreated probes. 12998 */ 12999 static void 13000 dtrace_enabling_provide(dtrace_provider_t *prv) 13001 { 13002 int i, all = 0; 13003 dtrace_probedesc_t desc; 13004 dtrace_genid_t gen; 13005 13006 ASSERT(MUTEX_HELD(&dtrace_lock)); 13007 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 13008 13009 if (prv == NULL) { 13010 all = 1; 13011 prv = dtrace_provider; 13012 } 13013 13014 do { 13015 dtrace_enabling_t *enab; 13016 void *parg = prv->dtpv_arg; 13017 13018 retry: 13019 gen = dtrace_retained_gen; 13020 for (enab = dtrace_retained; enab != NULL; 13021 enab = enab->dten_next) { 13022 for (i = 0; i < enab->dten_ndesc; i++) { 13023 desc = enab->dten_desc[i]->dted_probe; 13024 mutex_exit(&dtrace_lock); 13025 prv->dtpv_pops.dtps_provide(parg, &desc); 13026 mutex_enter(&dtrace_lock); 13027 /* 13028 * Process the retained enablings again if 13029 * they have changed while we weren't holding 13030 * dtrace_lock. 13031 */ 13032 if (gen != dtrace_retained_gen) 13033 goto retry; 13034 } 13035 } 13036 } while (all && (prv = prv->dtpv_next) != NULL); 13037 13038 mutex_exit(&dtrace_lock); 13039 dtrace_probe_provide(NULL, all ? NULL : prv); 13040 mutex_enter(&dtrace_lock); 13041 } 13042 13043 /* 13044 * Called to reap ECBs that are attached to probes from defunct providers. 13045 */ 13046 static void 13047 dtrace_enabling_reap(void) 13048 { 13049 dtrace_provider_t *prov; 13050 dtrace_probe_t *probe; 13051 dtrace_ecb_t *ecb; 13052 hrtime_t when; 13053 int i; 13054 13055 mutex_enter(&cpu_lock); 13056 mutex_enter(&dtrace_lock); 13057 13058 for (i = 0; i < dtrace_nprobes; i++) { 13059 if ((probe = dtrace_probes[i]) == NULL) 13060 continue; 13061 13062 if (probe->dtpr_ecb == NULL) 13063 continue; 13064 13065 prov = probe->dtpr_provider; 13066 13067 if ((when = prov->dtpv_defunct) == 0) 13068 continue; 13069 13070 /* 13071 * We have ECBs on a defunct provider: we want to reap these 13072 * ECBs to allow the provider to unregister. The destruction 13073 * of these ECBs must be done carefully: if we destroy the ECB 13074 * and the consumer later wishes to consume an EPID that 13075 * corresponds to the destroyed ECB (and if the EPID metadata 13076 * has not been previously consumed), the consumer will abort 13077 * processing on the unknown EPID. To reduce (but not, sadly, 13078 * eliminate) the possibility of this, we will only destroy an 13079 * ECB for a defunct provider if, for the state that 13080 * corresponds to the ECB: 13081 * 13082 * (a) There is no speculative tracing (which can effectively 13083 * cache an EPID for an arbitrary amount of time). 13084 * 13085 * (b) The principal buffers have been switched twice since the 13086 * provider became defunct. 13087 * 13088 * (c) The aggregation buffers are of zero size or have been 13089 * switched twice since the provider became defunct. 13090 * 13091 * We use dts_speculates to determine (a) and call a function 13092 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 13093 * that as soon as we've been unable to destroy one of the ECBs 13094 * associated with the probe, we quit trying -- reaping is only 13095 * fruitful in as much as we can destroy all ECBs associated 13096 * with the defunct provider's probes. 13097 */ 13098 while ((ecb = probe->dtpr_ecb) != NULL) { 13099 dtrace_state_t *state = ecb->dte_state; 13100 dtrace_buffer_t *buf = state->dts_buffer; 13101 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 13102 13103 if (state->dts_speculates) 13104 break; 13105 13106 if (!dtrace_buffer_consumed(buf, when)) 13107 break; 13108 13109 if (!dtrace_buffer_consumed(aggbuf, when)) 13110 break; 13111 13112 dtrace_ecb_disable(ecb); 13113 ASSERT(probe->dtpr_ecb != ecb); 13114 dtrace_ecb_destroy(ecb); 13115 } 13116 } 13117 13118 mutex_exit(&dtrace_lock); 13119 mutex_exit(&cpu_lock); 13120 } 13121 13122 /* 13123 * DTrace DOF Functions 13124 */ 13125 /*ARGSUSED*/ 13126 static void 13127 dtrace_dof_error(dof_hdr_t *dof, const char *str) 13128 { 13129 if (dtrace_err_verbose) 13130 cmn_err(CE_WARN, "failed to process DOF: %s", str); 13131 13132 #ifdef DTRACE_ERRDEBUG 13133 dtrace_errdebug(str); 13134 #endif 13135 } 13136 13137 /* 13138 * Create DOF out of a currently enabled state. Right now, we only create 13139 * DOF containing the run-time options -- but this could be expanded to create 13140 * complete DOF representing the enabled state. 13141 */ 13142 static dof_hdr_t * 13143 dtrace_dof_create(dtrace_state_t *state) 13144 { 13145 dof_hdr_t *dof; 13146 dof_sec_t *sec; 13147 dof_optdesc_t *opt; 13148 int i, len = sizeof (dof_hdr_t) + 13149 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 13150 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 13151 13152 ASSERT(MUTEX_HELD(&dtrace_lock)); 13153 13154 dof = kmem_zalloc(len, KM_SLEEP); 13155 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 13156 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 13157 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 13158 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 13159 13160 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 13161 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 13162 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 13163 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 13164 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 13165 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 13166 13167 dof->dofh_flags = 0; 13168 dof->dofh_hdrsize = sizeof (dof_hdr_t); 13169 dof->dofh_secsize = sizeof (dof_sec_t); 13170 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 13171 dof->dofh_secoff = sizeof (dof_hdr_t); 13172 dof->dofh_loadsz = len; 13173 dof->dofh_filesz = len; 13174 dof->dofh_pad = 0; 13175 13176 /* 13177 * Fill in the option section header... 13178 */ 13179 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 13180 sec->dofs_type = DOF_SECT_OPTDESC; 13181 sec->dofs_align = sizeof (uint64_t); 13182 sec->dofs_flags = DOF_SECF_LOAD; 13183 sec->dofs_entsize = sizeof (dof_optdesc_t); 13184 13185 opt = (dof_optdesc_t *)((uintptr_t)sec + 13186 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 13187 13188 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 13189 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 13190 13191 for (i = 0; i < DTRACEOPT_MAX; i++) { 13192 opt[i].dofo_option = i; 13193 opt[i].dofo_strtab = DOF_SECIDX_NONE; 13194 opt[i].dofo_value = state->dts_options[i]; 13195 } 13196 13197 return (dof); 13198 } 13199 13200 static dof_hdr_t * 13201 dtrace_dof_copyin(uintptr_t uarg, int *errp) 13202 { 13203 dof_hdr_t hdr, *dof; 13204 13205 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13206 13207 /* 13208 * First, we're going to copyin() the sizeof (dof_hdr_t). 13209 */ 13210 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 13211 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13212 *errp = EFAULT; 13213 return (NULL); 13214 } 13215 13216 /* 13217 * Now we'll allocate the entire DOF and copy it in -- provided 13218 * that the length isn't outrageous. 13219 */ 13220 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13221 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13222 *errp = E2BIG; 13223 return (NULL); 13224 } 13225 13226 if (hdr.dofh_loadsz < sizeof (hdr)) { 13227 dtrace_dof_error(&hdr, "invalid load size"); 13228 *errp = EINVAL; 13229 return (NULL); 13230 } 13231 13232 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 13233 13234 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 13235 dof->dofh_loadsz != hdr.dofh_loadsz) { 13236 kmem_free(dof, hdr.dofh_loadsz); 13237 *errp = EFAULT; 13238 return (NULL); 13239 } 13240 13241 return (dof); 13242 } 13243 13244 #ifdef __FreeBSD__ 13245 static dof_hdr_t * 13246 dtrace_dof_copyin_proc(struct proc *p, uintptr_t uarg, int *errp) 13247 { 13248 dof_hdr_t hdr, *dof; 13249 struct thread *td; 13250 size_t loadsz; 13251 13252 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13253 13254 td = curthread; 13255 13256 /* 13257 * First, we're going to copyin() the sizeof (dof_hdr_t). 13258 */ 13259 if (proc_readmem(td, p, uarg, &hdr, sizeof(hdr)) != sizeof(hdr)) { 13260 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13261 *errp = EFAULT; 13262 return (NULL); 13263 } 13264 13265 /* 13266 * Now we'll allocate the entire DOF and copy it in -- provided 13267 * that the length isn't outrageous. 13268 */ 13269 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13270 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13271 *errp = E2BIG; 13272 return (NULL); 13273 } 13274 loadsz = (size_t)hdr.dofh_loadsz; 13275 13276 if (loadsz < sizeof (hdr)) { 13277 dtrace_dof_error(&hdr, "invalid load size"); 13278 *errp = EINVAL; 13279 return (NULL); 13280 } 13281 13282 dof = kmem_alloc(loadsz, KM_SLEEP); 13283 13284 if (proc_readmem(td, p, uarg, dof, loadsz) != loadsz || 13285 dof->dofh_loadsz != loadsz) { 13286 kmem_free(dof, hdr.dofh_loadsz); 13287 *errp = EFAULT; 13288 return (NULL); 13289 } 13290 13291 return (dof); 13292 } 13293 13294 static __inline uchar_t 13295 dtrace_dof_char(char c) 13296 { 13297 13298 switch (c) { 13299 case '0': 13300 case '1': 13301 case '2': 13302 case '3': 13303 case '4': 13304 case '5': 13305 case '6': 13306 case '7': 13307 case '8': 13308 case '9': 13309 return (c - '0'); 13310 case 'A': 13311 case 'B': 13312 case 'C': 13313 case 'D': 13314 case 'E': 13315 case 'F': 13316 return (c - 'A' + 10); 13317 case 'a': 13318 case 'b': 13319 case 'c': 13320 case 'd': 13321 case 'e': 13322 case 'f': 13323 return (c - 'a' + 10); 13324 } 13325 /* Should not reach here. */ 13326 return (UCHAR_MAX); 13327 } 13328 #endif /* __FreeBSD__ */ 13329 13330 static dof_hdr_t * 13331 dtrace_dof_property(const char *name) 13332 { 13333 #ifdef __FreeBSD__ 13334 uint8_t *dofbuf; 13335 u_char *data, *eol; 13336 caddr_t doffile; 13337 size_t bytes, len, i; 13338 dof_hdr_t *dof; 13339 u_char c1, c2; 13340 13341 dof = NULL; 13342 13343 doffile = preload_search_by_type("dtrace_dof"); 13344 if (doffile == NULL) 13345 return (NULL); 13346 13347 data = preload_fetch_addr(doffile); 13348 len = preload_fetch_size(doffile); 13349 for (;;) { 13350 /* Look for the end of the line. All lines end in a newline. */ 13351 eol = memchr(data, '\n', len); 13352 if (eol == NULL) 13353 return (NULL); 13354 13355 if (strncmp(name, data, strlen(name)) == 0) 13356 break; 13357 13358 eol++; /* skip past the newline */ 13359 len -= eol - data; 13360 data = eol; 13361 } 13362 13363 /* We've found the data corresponding to the specified key. */ 13364 13365 data += strlen(name) + 1; /* skip past the '=' */ 13366 len = eol - data; 13367 if (len % 2 != 0) { 13368 dtrace_dof_error(NULL, "invalid DOF encoding length"); 13369 goto doferr; 13370 } 13371 bytes = len / 2; 13372 if (bytes < sizeof(dof_hdr_t)) { 13373 dtrace_dof_error(NULL, "truncated header"); 13374 goto doferr; 13375 } 13376 13377 /* 13378 * Each byte is represented by the two ASCII characters in its hex 13379 * representation. 13380 */ 13381 dofbuf = malloc(bytes, M_SOLARIS, M_WAITOK); 13382 for (i = 0; i < bytes; i++) { 13383 c1 = dtrace_dof_char(data[i * 2]); 13384 c2 = dtrace_dof_char(data[i * 2 + 1]); 13385 if (c1 == UCHAR_MAX || c2 == UCHAR_MAX) { 13386 dtrace_dof_error(NULL, "invalid hex char in DOF"); 13387 goto doferr; 13388 } 13389 dofbuf[i] = c1 * 16 + c2; 13390 } 13391 13392 dof = (dof_hdr_t *)dofbuf; 13393 if (bytes < dof->dofh_loadsz) { 13394 dtrace_dof_error(NULL, "truncated DOF"); 13395 goto doferr; 13396 } 13397 13398 if (dof->dofh_loadsz >= dtrace_dof_maxsize) { 13399 dtrace_dof_error(NULL, "oversized DOF"); 13400 goto doferr; 13401 } 13402 13403 return (dof); 13404 13405 doferr: 13406 free(dof, M_SOLARIS); 13407 return (NULL); 13408 #else /* __FreeBSD__ */ 13409 uchar_t *buf; 13410 uint64_t loadsz; 13411 unsigned int len, i; 13412 dof_hdr_t *dof; 13413 13414 /* 13415 * Unfortunately, array of values in .conf files are always (and 13416 * only) interpreted to be integer arrays. We must read our DOF 13417 * as an integer array, and then squeeze it into a byte array. 13418 */ 13419 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 13420 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 13421 return (NULL); 13422 13423 for (i = 0; i < len; i++) 13424 buf[i] = (uchar_t)(((int *)buf)[i]); 13425 13426 if (len < sizeof (dof_hdr_t)) { 13427 ddi_prop_free(buf); 13428 dtrace_dof_error(NULL, "truncated header"); 13429 return (NULL); 13430 } 13431 13432 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 13433 ddi_prop_free(buf); 13434 dtrace_dof_error(NULL, "truncated DOF"); 13435 return (NULL); 13436 } 13437 13438 if (loadsz >= dtrace_dof_maxsize) { 13439 ddi_prop_free(buf); 13440 dtrace_dof_error(NULL, "oversized DOF"); 13441 return (NULL); 13442 } 13443 13444 dof = kmem_alloc(loadsz, KM_SLEEP); 13445 bcopy(buf, dof, loadsz); 13446 ddi_prop_free(buf); 13447 13448 return (dof); 13449 #endif /* !__FreeBSD__ */ 13450 } 13451 13452 static void 13453 dtrace_dof_destroy(dof_hdr_t *dof) 13454 { 13455 kmem_free(dof, dof->dofh_loadsz); 13456 } 13457 13458 /* 13459 * Return the dof_sec_t pointer corresponding to a given section index. If the 13460 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 13461 * a type other than DOF_SECT_NONE is specified, the header is checked against 13462 * this type and NULL is returned if the types do not match. 13463 */ 13464 static dof_sec_t * 13465 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 13466 { 13467 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 13468 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 13469 13470 if (i >= dof->dofh_secnum) { 13471 dtrace_dof_error(dof, "referenced section index is invalid"); 13472 return (NULL); 13473 } 13474 13475 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 13476 dtrace_dof_error(dof, "referenced section is not loadable"); 13477 return (NULL); 13478 } 13479 13480 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 13481 dtrace_dof_error(dof, "referenced section is the wrong type"); 13482 return (NULL); 13483 } 13484 13485 return (sec); 13486 } 13487 13488 static dtrace_probedesc_t * 13489 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 13490 { 13491 dof_probedesc_t *probe; 13492 dof_sec_t *strtab; 13493 uintptr_t daddr = (uintptr_t)dof; 13494 uintptr_t str; 13495 size_t size; 13496 13497 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 13498 dtrace_dof_error(dof, "invalid probe section"); 13499 return (NULL); 13500 } 13501 13502 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13503 dtrace_dof_error(dof, "bad alignment in probe description"); 13504 return (NULL); 13505 } 13506 13507 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 13508 dtrace_dof_error(dof, "truncated probe description"); 13509 return (NULL); 13510 } 13511 13512 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 13513 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 13514 13515 if (strtab == NULL) 13516 return (NULL); 13517 13518 str = daddr + strtab->dofs_offset; 13519 size = strtab->dofs_size; 13520 13521 if (probe->dofp_provider >= strtab->dofs_size) { 13522 dtrace_dof_error(dof, "corrupt probe provider"); 13523 return (NULL); 13524 } 13525 13526 (void) strncpy(desc->dtpd_provider, 13527 (char *)(str + probe->dofp_provider), 13528 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 13529 13530 if (probe->dofp_mod >= strtab->dofs_size) { 13531 dtrace_dof_error(dof, "corrupt probe module"); 13532 return (NULL); 13533 } 13534 13535 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 13536 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 13537 13538 if (probe->dofp_func >= strtab->dofs_size) { 13539 dtrace_dof_error(dof, "corrupt probe function"); 13540 return (NULL); 13541 } 13542 13543 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 13544 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 13545 13546 if (probe->dofp_name >= strtab->dofs_size) { 13547 dtrace_dof_error(dof, "corrupt probe name"); 13548 return (NULL); 13549 } 13550 13551 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 13552 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 13553 13554 return (desc); 13555 } 13556 13557 static dtrace_difo_t * 13558 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13559 cred_t *cr) 13560 { 13561 dtrace_difo_t *dp; 13562 size_t ttl = 0; 13563 dof_difohdr_t *dofd; 13564 uintptr_t daddr = (uintptr_t)dof; 13565 size_t max = dtrace_difo_maxsize; 13566 int i, l, n; 13567 13568 static const struct { 13569 int section; 13570 int bufoffs; 13571 int lenoffs; 13572 int entsize; 13573 int align; 13574 const char *msg; 13575 } difo[] = { 13576 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 13577 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 13578 sizeof (dif_instr_t), "multiple DIF sections" }, 13579 13580 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 13581 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 13582 sizeof (uint64_t), "multiple integer tables" }, 13583 13584 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 13585 offsetof(dtrace_difo_t, dtdo_strlen), 0, 13586 sizeof (char), "multiple string tables" }, 13587 13588 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 13589 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 13590 sizeof (uint_t), "multiple variable tables" }, 13591 13592 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 13593 }; 13594 13595 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 13596 dtrace_dof_error(dof, "invalid DIFO header section"); 13597 return (NULL); 13598 } 13599 13600 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13601 dtrace_dof_error(dof, "bad alignment in DIFO header"); 13602 return (NULL); 13603 } 13604 13605 if (sec->dofs_size < sizeof (dof_difohdr_t) || 13606 sec->dofs_size % sizeof (dof_secidx_t)) { 13607 dtrace_dof_error(dof, "bad size in DIFO header"); 13608 return (NULL); 13609 } 13610 13611 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13612 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 13613 13614 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 13615 dp->dtdo_rtype = dofd->dofd_rtype; 13616 13617 for (l = 0; l < n; l++) { 13618 dof_sec_t *subsec; 13619 void **bufp; 13620 uint32_t *lenp; 13621 13622 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 13623 dofd->dofd_links[l])) == NULL) 13624 goto err; /* invalid section link */ 13625 13626 if (ttl + subsec->dofs_size > max) { 13627 dtrace_dof_error(dof, "exceeds maximum size"); 13628 goto err; 13629 } 13630 13631 ttl += subsec->dofs_size; 13632 13633 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 13634 if (subsec->dofs_type != difo[i].section) 13635 continue; 13636 13637 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 13638 dtrace_dof_error(dof, "section not loaded"); 13639 goto err; 13640 } 13641 13642 if (subsec->dofs_align != difo[i].align) { 13643 dtrace_dof_error(dof, "bad alignment"); 13644 goto err; 13645 } 13646 13647 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 13648 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 13649 13650 if (*bufp != NULL) { 13651 dtrace_dof_error(dof, difo[i].msg); 13652 goto err; 13653 } 13654 13655 if (difo[i].entsize != subsec->dofs_entsize) { 13656 dtrace_dof_error(dof, "entry size mismatch"); 13657 goto err; 13658 } 13659 13660 if (subsec->dofs_entsize != 0 && 13661 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 13662 dtrace_dof_error(dof, "corrupt entry size"); 13663 goto err; 13664 } 13665 13666 *lenp = subsec->dofs_size; 13667 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 13668 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 13669 *bufp, subsec->dofs_size); 13670 13671 if (subsec->dofs_entsize != 0) 13672 *lenp /= subsec->dofs_entsize; 13673 13674 break; 13675 } 13676 13677 /* 13678 * If we encounter a loadable DIFO sub-section that is not 13679 * known to us, assume this is a broken program and fail. 13680 */ 13681 if (difo[i].section == DOF_SECT_NONE && 13682 (subsec->dofs_flags & DOF_SECF_LOAD)) { 13683 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 13684 goto err; 13685 } 13686 } 13687 13688 if (dp->dtdo_buf == NULL) { 13689 /* 13690 * We can't have a DIF object without DIF text. 13691 */ 13692 dtrace_dof_error(dof, "missing DIF text"); 13693 goto err; 13694 } 13695 13696 /* 13697 * Before we validate the DIF object, run through the variable table 13698 * looking for the strings -- if any of their size are under, we'll set 13699 * their size to be the system-wide default string size. Note that 13700 * this should _not_ happen if the "strsize" option has been set -- 13701 * in this case, the compiler should have set the size to reflect the 13702 * setting of the option. 13703 */ 13704 for (i = 0; i < dp->dtdo_varlen; i++) { 13705 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 13706 dtrace_diftype_t *t = &v->dtdv_type; 13707 13708 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 13709 continue; 13710 13711 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 13712 t->dtdt_size = dtrace_strsize_default; 13713 } 13714 13715 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 13716 goto err; 13717 13718 dtrace_difo_init(dp, vstate); 13719 return (dp); 13720 13721 err: 13722 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 13723 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 13724 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 13725 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 13726 13727 kmem_free(dp, sizeof (dtrace_difo_t)); 13728 return (NULL); 13729 } 13730 13731 static dtrace_predicate_t * 13732 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13733 cred_t *cr) 13734 { 13735 dtrace_difo_t *dp; 13736 13737 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 13738 return (NULL); 13739 13740 return (dtrace_predicate_create(dp)); 13741 } 13742 13743 static dtrace_actdesc_t * 13744 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13745 cred_t *cr) 13746 { 13747 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 13748 dof_actdesc_t *desc; 13749 dof_sec_t *difosec; 13750 size_t offs; 13751 uintptr_t daddr = (uintptr_t)dof; 13752 uint64_t arg; 13753 dtrace_actkind_t kind; 13754 13755 if (sec->dofs_type != DOF_SECT_ACTDESC) { 13756 dtrace_dof_error(dof, "invalid action section"); 13757 return (NULL); 13758 } 13759 13760 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 13761 dtrace_dof_error(dof, "truncated action description"); 13762 return (NULL); 13763 } 13764 13765 if (sec->dofs_align != sizeof (uint64_t)) { 13766 dtrace_dof_error(dof, "bad alignment in action description"); 13767 return (NULL); 13768 } 13769 13770 if (sec->dofs_size < sec->dofs_entsize) { 13771 dtrace_dof_error(dof, "section entry size exceeds total size"); 13772 return (NULL); 13773 } 13774 13775 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 13776 dtrace_dof_error(dof, "bad entry size in action description"); 13777 return (NULL); 13778 } 13779 13780 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 13781 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 13782 return (NULL); 13783 } 13784 13785 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 13786 desc = (dof_actdesc_t *)(daddr + 13787 (uintptr_t)sec->dofs_offset + offs); 13788 kind = (dtrace_actkind_t)desc->dofa_kind; 13789 13790 if ((DTRACEACT_ISPRINTFLIKE(kind) && 13791 (kind != DTRACEACT_PRINTA || 13792 desc->dofa_strtab != DOF_SECIDX_NONE)) || 13793 (kind == DTRACEACT_DIFEXPR && 13794 desc->dofa_strtab != DOF_SECIDX_NONE)) { 13795 dof_sec_t *strtab; 13796 char *str, *fmt; 13797 uint64_t i; 13798 13799 /* 13800 * The argument to these actions is an index into the 13801 * DOF string table. For printf()-like actions, this 13802 * is the format string. For print(), this is the 13803 * CTF type of the expression result. 13804 */ 13805 if ((strtab = dtrace_dof_sect(dof, 13806 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 13807 goto err; 13808 13809 str = (char *)((uintptr_t)dof + 13810 (uintptr_t)strtab->dofs_offset); 13811 13812 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 13813 if (str[i] == '\0') 13814 break; 13815 } 13816 13817 if (i >= strtab->dofs_size) { 13818 dtrace_dof_error(dof, "bogus format string"); 13819 goto err; 13820 } 13821 13822 if (i == desc->dofa_arg) { 13823 dtrace_dof_error(dof, "empty format string"); 13824 goto err; 13825 } 13826 13827 i -= desc->dofa_arg; 13828 fmt = kmem_alloc(i + 1, KM_SLEEP); 13829 bcopy(&str[desc->dofa_arg], fmt, i + 1); 13830 arg = (uint64_t)(uintptr_t)fmt; 13831 } else { 13832 if (kind == DTRACEACT_PRINTA) { 13833 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 13834 arg = 0; 13835 } else { 13836 arg = desc->dofa_arg; 13837 } 13838 } 13839 13840 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 13841 desc->dofa_uarg, arg); 13842 13843 if (last != NULL) { 13844 last->dtad_next = act; 13845 } else { 13846 first = act; 13847 } 13848 13849 last = act; 13850 13851 if (desc->dofa_difo == DOF_SECIDX_NONE) 13852 continue; 13853 13854 if ((difosec = dtrace_dof_sect(dof, 13855 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 13856 goto err; 13857 13858 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 13859 13860 if (act->dtad_difo == NULL) 13861 goto err; 13862 } 13863 13864 ASSERT(first != NULL); 13865 return (first); 13866 13867 err: 13868 for (act = first; act != NULL; act = next) { 13869 next = act->dtad_next; 13870 dtrace_actdesc_release(act, vstate); 13871 } 13872 13873 return (NULL); 13874 } 13875 13876 static dtrace_ecbdesc_t * 13877 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13878 cred_t *cr) 13879 { 13880 dtrace_ecbdesc_t *ep; 13881 dof_ecbdesc_t *ecb; 13882 dtrace_probedesc_t *desc; 13883 dtrace_predicate_t *pred = NULL; 13884 13885 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 13886 dtrace_dof_error(dof, "truncated ECB description"); 13887 return (NULL); 13888 } 13889 13890 if (sec->dofs_align != sizeof (uint64_t)) { 13891 dtrace_dof_error(dof, "bad alignment in ECB description"); 13892 return (NULL); 13893 } 13894 13895 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 13896 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 13897 13898 if (sec == NULL) 13899 return (NULL); 13900 13901 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 13902 ep->dted_uarg = ecb->dofe_uarg; 13903 desc = &ep->dted_probe; 13904 13905 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 13906 goto err; 13907 13908 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 13909 if ((sec = dtrace_dof_sect(dof, 13910 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 13911 goto err; 13912 13913 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 13914 goto err; 13915 13916 ep->dted_pred.dtpdd_predicate = pred; 13917 } 13918 13919 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 13920 if ((sec = dtrace_dof_sect(dof, 13921 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 13922 goto err; 13923 13924 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 13925 13926 if (ep->dted_action == NULL) 13927 goto err; 13928 } 13929 13930 return (ep); 13931 13932 err: 13933 if (pred != NULL) 13934 dtrace_predicate_release(pred, vstate); 13935 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 13936 return (NULL); 13937 } 13938 13939 /* 13940 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 13941 * specified DOF. SETX relocations are computed using 'ubase', the base load 13942 * address of the object containing the DOF, and DOFREL relocations are relative 13943 * to the relocation offset within the DOF. 13944 */ 13945 static int 13946 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase, 13947 uint64_t udaddr) 13948 { 13949 uintptr_t daddr = (uintptr_t)dof; 13950 uintptr_t ts_end; 13951 dof_relohdr_t *dofr = 13952 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13953 dof_sec_t *ss, *rs, *ts; 13954 dof_relodesc_t *r; 13955 uint_t i, n; 13956 13957 if (sec->dofs_size < sizeof (dof_relohdr_t) || 13958 sec->dofs_align != sizeof (dof_secidx_t)) { 13959 dtrace_dof_error(dof, "invalid relocation header"); 13960 return (-1); 13961 } 13962 13963 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 13964 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 13965 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 13966 ts_end = (uintptr_t)ts + sizeof (dof_sec_t); 13967 13968 if (ss == NULL || rs == NULL || ts == NULL) 13969 return (-1); /* dtrace_dof_error() has been called already */ 13970 13971 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 13972 rs->dofs_align != sizeof (uint64_t)) { 13973 dtrace_dof_error(dof, "invalid relocation section"); 13974 return (-1); 13975 } 13976 13977 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 13978 n = rs->dofs_size / rs->dofs_entsize; 13979 13980 for (i = 0; i < n; i++) { 13981 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 13982 13983 switch (r->dofr_type) { 13984 case DOF_RELO_NONE: 13985 break; 13986 case DOF_RELO_SETX: 13987 case DOF_RELO_DOFREL: 13988 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 13989 sizeof (uint64_t) > ts->dofs_size) { 13990 dtrace_dof_error(dof, "bad relocation offset"); 13991 return (-1); 13992 } 13993 13994 if (taddr >= (uintptr_t)ts && taddr < ts_end) { 13995 dtrace_dof_error(dof, "bad relocation offset"); 13996 return (-1); 13997 } 13998 13999 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 14000 dtrace_dof_error(dof, "misaligned setx relo"); 14001 return (-1); 14002 } 14003 14004 if (r->dofr_type == DOF_RELO_SETX) 14005 *(uint64_t *)taddr += ubase; 14006 else 14007 *(uint64_t *)taddr += 14008 udaddr + ts->dofs_offset + r->dofr_offset; 14009 break; 14010 default: 14011 dtrace_dof_error(dof, "invalid relocation type"); 14012 return (-1); 14013 } 14014 14015 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 14016 } 14017 14018 return (0); 14019 } 14020 14021 /* 14022 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 14023 * header: it should be at the front of a memory region that is at least 14024 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 14025 * size. It need not be validated in any other way. 14026 */ 14027 static int 14028 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 14029 dtrace_enabling_t **enabp, uint64_t ubase, uint64_t udaddr, int noprobes) 14030 { 14031 uint64_t len = dof->dofh_loadsz, seclen; 14032 uintptr_t daddr = (uintptr_t)dof; 14033 dtrace_ecbdesc_t *ep; 14034 dtrace_enabling_t *enab; 14035 uint_t i; 14036 14037 ASSERT(MUTEX_HELD(&dtrace_lock)); 14038 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 14039 14040 /* 14041 * Check the DOF header identification bytes. In addition to checking 14042 * valid settings, we also verify that unused bits/bytes are zeroed so 14043 * we can use them later without fear of regressing existing binaries. 14044 */ 14045 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 14046 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 14047 dtrace_dof_error(dof, "DOF magic string mismatch"); 14048 return (-1); 14049 } 14050 14051 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 14052 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 14053 dtrace_dof_error(dof, "DOF has invalid data model"); 14054 return (-1); 14055 } 14056 14057 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 14058 dtrace_dof_error(dof, "DOF encoding mismatch"); 14059 return (-1); 14060 } 14061 14062 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14063 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 14064 dtrace_dof_error(dof, "DOF version mismatch"); 14065 return (-1); 14066 } 14067 14068 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 14069 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 14070 return (-1); 14071 } 14072 14073 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 14074 dtrace_dof_error(dof, "DOF uses too many integer registers"); 14075 return (-1); 14076 } 14077 14078 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 14079 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 14080 return (-1); 14081 } 14082 14083 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 14084 if (dof->dofh_ident[i] != 0) { 14085 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 14086 return (-1); 14087 } 14088 } 14089 14090 if (dof->dofh_flags & ~DOF_FL_VALID) { 14091 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 14092 return (-1); 14093 } 14094 14095 if (dof->dofh_secsize == 0) { 14096 dtrace_dof_error(dof, "zero section header size"); 14097 return (-1); 14098 } 14099 14100 /* 14101 * Check that the section headers don't exceed the amount of DOF 14102 * data. Note that we cast the section size and number of sections 14103 * to uint64_t's to prevent possible overflow in the multiplication. 14104 */ 14105 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 14106 14107 if (dof->dofh_secoff > len || seclen > len || 14108 dof->dofh_secoff + seclen > len) { 14109 dtrace_dof_error(dof, "truncated section headers"); 14110 return (-1); 14111 } 14112 14113 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 14114 dtrace_dof_error(dof, "misaligned section headers"); 14115 return (-1); 14116 } 14117 14118 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 14119 dtrace_dof_error(dof, "misaligned section size"); 14120 return (-1); 14121 } 14122 14123 /* 14124 * Take an initial pass through the section headers to be sure that 14125 * the headers don't have stray offsets. If the 'noprobes' flag is 14126 * set, do not permit sections relating to providers, probes, or args. 14127 */ 14128 for (i = 0; i < dof->dofh_secnum; i++) { 14129 dof_sec_t *sec = (dof_sec_t *)(daddr + 14130 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14131 14132 if (noprobes) { 14133 switch (sec->dofs_type) { 14134 case DOF_SECT_PROVIDER: 14135 case DOF_SECT_PROBES: 14136 case DOF_SECT_PRARGS: 14137 case DOF_SECT_PROFFS: 14138 dtrace_dof_error(dof, "illegal sections " 14139 "for enabling"); 14140 return (-1); 14141 } 14142 } 14143 14144 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 14145 !(sec->dofs_flags & DOF_SECF_LOAD)) { 14146 dtrace_dof_error(dof, "loadable section with load " 14147 "flag unset"); 14148 return (-1); 14149 } 14150 14151 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 14152 continue; /* just ignore non-loadable sections */ 14153 14154 if (!ISP2(sec->dofs_align)) { 14155 dtrace_dof_error(dof, "bad section alignment"); 14156 return (-1); 14157 } 14158 14159 if (sec->dofs_offset & (sec->dofs_align - 1)) { 14160 dtrace_dof_error(dof, "misaligned section"); 14161 return (-1); 14162 } 14163 14164 if (sec->dofs_offset > len || sec->dofs_size > len || 14165 sec->dofs_offset + sec->dofs_size > len) { 14166 dtrace_dof_error(dof, "corrupt section header"); 14167 return (-1); 14168 } 14169 14170 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 14171 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 14172 dtrace_dof_error(dof, "non-terminating string table"); 14173 return (-1); 14174 } 14175 } 14176 14177 /* 14178 * Take a second pass through the sections and locate and perform any 14179 * relocations that are present. We do this after the first pass to 14180 * be sure that all sections have had their headers validated. 14181 */ 14182 for (i = 0; i < dof->dofh_secnum; i++) { 14183 dof_sec_t *sec = (dof_sec_t *)(daddr + 14184 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14185 14186 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 14187 continue; /* skip sections that are not loadable */ 14188 14189 switch (sec->dofs_type) { 14190 case DOF_SECT_URELHDR: 14191 if (dtrace_dof_relocate(dof, sec, ubase, udaddr) != 0) 14192 return (-1); 14193 break; 14194 } 14195 } 14196 14197 if ((enab = *enabp) == NULL) 14198 enab = *enabp = dtrace_enabling_create(vstate); 14199 14200 for (i = 0; i < dof->dofh_secnum; i++) { 14201 dof_sec_t *sec = (dof_sec_t *)(daddr + 14202 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14203 14204 if (sec->dofs_type != DOF_SECT_ECBDESC) 14205 continue; 14206 14207 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 14208 dtrace_enabling_destroy(enab); 14209 *enabp = NULL; 14210 return (-1); 14211 } 14212 14213 dtrace_enabling_add(enab, ep); 14214 } 14215 14216 return (0); 14217 } 14218 14219 /* 14220 * Process DOF for any options. This routine assumes that the DOF has been 14221 * at least processed by dtrace_dof_slurp(). 14222 */ 14223 static int 14224 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 14225 { 14226 int i, rval; 14227 uint32_t entsize; 14228 size_t offs; 14229 dof_optdesc_t *desc; 14230 14231 for (i = 0; i < dof->dofh_secnum; i++) { 14232 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 14233 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14234 14235 if (sec->dofs_type != DOF_SECT_OPTDESC) 14236 continue; 14237 14238 if (sec->dofs_align != sizeof (uint64_t)) { 14239 dtrace_dof_error(dof, "bad alignment in " 14240 "option description"); 14241 return (EINVAL); 14242 } 14243 14244 if ((entsize = sec->dofs_entsize) == 0) { 14245 dtrace_dof_error(dof, "zeroed option entry size"); 14246 return (EINVAL); 14247 } 14248 14249 if (entsize < sizeof (dof_optdesc_t)) { 14250 dtrace_dof_error(dof, "bad option entry size"); 14251 return (EINVAL); 14252 } 14253 14254 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 14255 desc = (dof_optdesc_t *)((uintptr_t)dof + 14256 (uintptr_t)sec->dofs_offset + offs); 14257 14258 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 14259 dtrace_dof_error(dof, "non-zero option string"); 14260 return (EINVAL); 14261 } 14262 14263 if (desc->dofo_value == DTRACEOPT_UNSET) { 14264 dtrace_dof_error(dof, "unset option"); 14265 return (EINVAL); 14266 } 14267 14268 if ((rval = dtrace_state_option(state, 14269 desc->dofo_option, desc->dofo_value)) != 0) { 14270 dtrace_dof_error(dof, "rejected option"); 14271 return (rval); 14272 } 14273 } 14274 } 14275 14276 return (0); 14277 } 14278 14279 /* 14280 * DTrace Consumer State Functions 14281 */ 14282 static int 14283 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 14284 { 14285 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 14286 void *base; 14287 uintptr_t limit; 14288 dtrace_dynvar_t *dvar, *next, *start; 14289 int i; 14290 14291 ASSERT(MUTEX_HELD(&dtrace_lock)); 14292 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 14293 14294 bzero(dstate, sizeof (dtrace_dstate_t)); 14295 14296 if ((dstate->dtds_chunksize = chunksize) == 0) 14297 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 14298 14299 VERIFY(dstate->dtds_chunksize < LONG_MAX); 14300 14301 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 14302 size = min; 14303 14304 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 14305 return (ENOMEM); 14306 14307 dstate->dtds_size = size; 14308 dstate->dtds_base = base; 14309 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 14310 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 14311 14312 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 14313 14314 if (hashsize != 1 && (hashsize & 1)) 14315 hashsize--; 14316 14317 dstate->dtds_hashsize = hashsize; 14318 dstate->dtds_hash = dstate->dtds_base; 14319 14320 /* 14321 * Set all of our hash buckets to point to the single sink, and (if 14322 * it hasn't already been set), set the sink's hash value to be the 14323 * sink sentinel value. The sink is needed for dynamic variable 14324 * lookups to know that they have iterated over an entire, valid hash 14325 * chain. 14326 */ 14327 for (i = 0; i < hashsize; i++) 14328 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 14329 14330 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 14331 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 14332 14333 /* 14334 * Determine number of active CPUs. Divide free list evenly among 14335 * active CPUs. 14336 */ 14337 start = (dtrace_dynvar_t *) 14338 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 14339 limit = (uintptr_t)base + size; 14340 14341 VERIFY((uintptr_t)start < limit); 14342 VERIFY((uintptr_t)start >= (uintptr_t)base); 14343 14344 maxper = (limit - (uintptr_t)start) / NCPU; 14345 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 14346 14347 #ifndef illumos 14348 CPU_FOREACH(i) { 14349 #else 14350 for (i = 0; i < NCPU; i++) { 14351 #endif 14352 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 14353 14354 /* 14355 * If we don't even have enough chunks to make it once through 14356 * NCPUs, we're just going to allocate everything to the first 14357 * CPU. And if we're on the last CPU, we're going to allocate 14358 * whatever is left over. In either case, we set the limit to 14359 * be the limit of the dynamic variable space. 14360 */ 14361 if (maxper == 0 || i == NCPU - 1) { 14362 limit = (uintptr_t)base + size; 14363 start = NULL; 14364 } else { 14365 limit = (uintptr_t)start + maxper; 14366 start = (dtrace_dynvar_t *)limit; 14367 } 14368 14369 VERIFY(limit <= (uintptr_t)base + size); 14370 14371 for (;;) { 14372 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 14373 dstate->dtds_chunksize); 14374 14375 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 14376 break; 14377 14378 VERIFY((uintptr_t)dvar >= (uintptr_t)base && 14379 (uintptr_t)dvar <= (uintptr_t)base + size); 14380 dvar->dtdv_next = next; 14381 dvar = next; 14382 } 14383 14384 if (maxper == 0) 14385 break; 14386 } 14387 14388 return (0); 14389 } 14390 14391 static void 14392 dtrace_dstate_fini(dtrace_dstate_t *dstate) 14393 { 14394 ASSERT(MUTEX_HELD(&cpu_lock)); 14395 14396 if (dstate->dtds_base == NULL) 14397 return; 14398 14399 kmem_free(dstate->dtds_base, dstate->dtds_size); 14400 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 14401 } 14402 14403 static void 14404 dtrace_vstate_fini(dtrace_vstate_t *vstate) 14405 { 14406 /* 14407 * Logical XOR, where are you? 14408 */ 14409 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 14410 14411 if (vstate->dtvs_nglobals > 0) { 14412 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 14413 sizeof (dtrace_statvar_t *)); 14414 } 14415 14416 if (vstate->dtvs_ntlocals > 0) { 14417 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 14418 sizeof (dtrace_difv_t)); 14419 } 14420 14421 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 14422 14423 if (vstate->dtvs_nlocals > 0) { 14424 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 14425 sizeof (dtrace_statvar_t *)); 14426 } 14427 } 14428 14429 #ifdef illumos 14430 static void 14431 dtrace_state_clean(dtrace_state_t *state) 14432 { 14433 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14434 return; 14435 14436 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14437 dtrace_speculation_clean(state); 14438 } 14439 14440 static void 14441 dtrace_state_deadman(dtrace_state_t *state) 14442 { 14443 hrtime_t now; 14444 14445 dtrace_sync(); 14446 14447 now = dtrace_gethrtime(); 14448 14449 if (state != dtrace_anon.dta_state && 14450 now - state->dts_laststatus >= dtrace_deadman_user) 14451 return; 14452 14453 /* 14454 * We must be sure that dts_alive never appears to be less than the 14455 * value upon entry to dtrace_state_deadman(), and because we lack a 14456 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14457 * store INT64_MAX to it, followed by a memory barrier, followed by 14458 * the new value. This assures that dts_alive never appears to be 14459 * less than its true value, regardless of the order in which the 14460 * stores to the underlying storage are issued. 14461 */ 14462 state->dts_alive = INT64_MAX; 14463 dtrace_membar_producer(); 14464 state->dts_alive = now; 14465 } 14466 #else /* !illumos */ 14467 static void 14468 dtrace_state_clean(void *arg) 14469 { 14470 dtrace_state_t *state = arg; 14471 dtrace_optval_t *opt = state->dts_options; 14472 14473 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14474 return; 14475 14476 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14477 dtrace_speculation_clean(state); 14478 14479 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14480 dtrace_state_clean, state); 14481 } 14482 14483 static void 14484 dtrace_state_deadman(void *arg) 14485 { 14486 dtrace_state_t *state = arg; 14487 hrtime_t now; 14488 14489 dtrace_sync(); 14490 14491 dtrace_debug_output(); 14492 14493 now = dtrace_gethrtime(); 14494 14495 if (state != dtrace_anon.dta_state && 14496 now - state->dts_laststatus >= dtrace_deadman_user) 14497 return; 14498 14499 /* 14500 * We must be sure that dts_alive never appears to be less than the 14501 * value upon entry to dtrace_state_deadman(), and because we lack a 14502 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14503 * store INT64_MAX to it, followed by a memory barrier, followed by 14504 * the new value. This assures that dts_alive never appears to be 14505 * less than its true value, regardless of the order in which the 14506 * stores to the underlying storage are issued. 14507 */ 14508 state->dts_alive = INT64_MAX; 14509 dtrace_membar_producer(); 14510 state->dts_alive = now; 14511 14512 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14513 dtrace_state_deadman, state); 14514 } 14515 #endif /* illumos */ 14516 14517 static dtrace_state_t * 14518 #ifdef illumos 14519 dtrace_state_create(dev_t *devp, cred_t *cr) 14520 #else 14521 dtrace_state_create(struct cdev *dev, struct ucred *cred __unused) 14522 #endif 14523 { 14524 #ifdef illumos 14525 minor_t minor; 14526 major_t major; 14527 #else 14528 cred_t *cr = NULL; 14529 int m = 0; 14530 #endif 14531 char c[30]; 14532 dtrace_state_t *state; 14533 dtrace_optval_t *opt; 14534 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 14535 int cpu_it; 14536 14537 ASSERT(MUTEX_HELD(&dtrace_lock)); 14538 ASSERT(MUTEX_HELD(&cpu_lock)); 14539 14540 #ifdef illumos 14541 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 14542 VM_BESTFIT | VM_SLEEP); 14543 14544 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 14545 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14546 return (NULL); 14547 } 14548 14549 state = ddi_get_soft_state(dtrace_softstate, minor); 14550 #else 14551 if (dev != NULL) { 14552 cr = dev->si_cred; 14553 m = dev2unit(dev); 14554 } 14555 14556 /* Allocate memory for the state. */ 14557 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 14558 #endif 14559 14560 state->dts_epid = DTRACE_EPIDNONE + 1; 14561 14562 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 14563 #ifdef illumos 14564 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 14565 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14566 14567 if (devp != NULL) { 14568 major = getemajor(*devp); 14569 } else { 14570 major = ddi_driver_major(dtrace_devi); 14571 } 14572 14573 state->dts_dev = makedevice(major, minor); 14574 14575 if (devp != NULL) 14576 *devp = state->dts_dev; 14577 #else 14578 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 14579 state->dts_dev = dev; 14580 #endif 14581 14582 /* 14583 * We allocate NCPU buffers. On the one hand, this can be quite 14584 * a bit of memory per instance (nearly 36K on a Starcat). On the 14585 * other hand, it saves an additional memory reference in the probe 14586 * path. 14587 */ 14588 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 14589 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 14590 14591 /* 14592 * Allocate and initialise the per-process per-CPU random state. 14593 * SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is 14594 * assumed to be seeded at this point (if from Fortuna seed file). 14595 */ 14596 (void) read_random(&state->dts_rstate[0], 2 * sizeof(uint64_t)); 14597 for (cpu_it = 1; cpu_it < NCPU; cpu_it++) { 14598 /* 14599 * Each CPU is assigned a 2^64 period, non-overlapping 14600 * subsequence. 14601 */ 14602 dtrace_xoroshiro128_plus_jump(state->dts_rstate[cpu_it-1], 14603 state->dts_rstate[cpu_it]); 14604 } 14605 14606 #ifdef illumos 14607 state->dts_cleaner = CYCLIC_NONE; 14608 state->dts_deadman = CYCLIC_NONE; 14609 #else 14610 callout_init(&state->dts_cleaner, 1); 14611 callout_init(&state->dts_deadman, 1); 14612 #endif 14613 state->dts_vstate.dtvs_state = state; 14614 14615 for (i = 0; i < DTRACEOPT_MAX; i++) 14616 state->dts_options[i] = DTRACEOPT_UNSET; 14617 14618 /* 14619 * Set the default options. 14620 */ 14621 opt = state->dts_options; 14622 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 14623 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 14624 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 14625 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 14626 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 14627 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 14628 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 14629 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 14630 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 14631 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 14632 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 14633 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 14634 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 14635 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 14636 14637 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 14638 14639 /* 14640 * Depending on the user credentials, we set flag bits which alter probe 14641 * visibility or the amount of destructiveness allowed. In the case of 14642 * actual anonymous tracing, or the possession of all privileges, all of 14643 * the normal checks are bypassed. 14644 */ 14645 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 14646 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 14647 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 14648 } else { 14649 /* 14650 * Set up the credentials for this instantiation. We take a 14651 * hold on the credential to prevent it from disappearing on 14652 * us; this in turn prevents the zone_t referenced by this 14653 * credential from disappearing. This means that we can 14654 * examine the credential and the zone from probe context. 14655 */ 14656 crhold(cr); 14657 state->dts_cred.dcr_cred = cr; 14658 14659 /* 14660 * CRA_PROC means "we have *some* privilege for dtrace" and 14661 * unlocks the use of variables like pid, zonename, etc. 14662 */ 14663 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 14664 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14665 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 14666 } 14667 14668 /* 14669 * dtrace_user allows use of syscall and profile providers. 14670 * If the user also has proc_owner and/or proc_zone, we 14671 * extend the scope to include additional visibility and 14672 * destructive power. 14673 */ 14674 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 14675 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 14676 state->dts_cred.dcr_visible |= 14677 DTRACE_CRV_ALLPROC; 14678 14679 state->dts_cred.dcr_action |= 14680 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14681 } 14682 14683 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 14684 state->dts_cred.dcr_visible |= 14685 DTRACE_CRV_ALLZONE; 14686 14687 state->dts_cred.dcr_action |= 14688 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14689 } 14690 14691 /* 14692 * If we have all privs in whatever zone this is, 14693 * we can do destructive things to processes which 14694 * have altered credentials. 14695 */ 14696 #ifdef illumos 14697 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14698 cr->cr_zone->zone_privset)) { 14699 state->dts_cred.dcr_action |= 14700 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14701 } 14702 #endif 14703 } 14704 14705 /* 14706 * Holding the dtrace_kernel privilege also implies that 14707 * the user has the dtrace_user privilege from a visibility 14708 * perspective. But without further privileges, some 14709 * destructive actions are not available. 14710 */ 14711 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 14712 /* 14713 * Make all probes in all zones visible. However, 14714 * this doesn't mean that all actions become available 14715 * to all zones. 14716 */ 14717 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 14718 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 14719 14720 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 14721 DTRACE_CRA_PROC; 14722 /* 14723 * Holding proc_owner means that destructive actions 14724 * for *this* zone are allowed. 14725 */ 14726 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14727 state->dts_cred.dcr_action |= 14728 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14729 14730 /* 14731 * Holding proc_zone means that destructive actions 14732 * for this user/group ID in all zones is allowed. 14733 */ 14734 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14735 state->dts_cred.dcr_action |= 14736 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14737 14738 #ifdef illumos 14739 /* 14740 * If we have all privs in whatever zone this is, 14741 * we can do destructive things to processes which 14742 * have altered credentials. 14743 */ 14744 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14745 cr->cr_zone->zone_privset)) { 14746 state->dts_cred.dcr_action |= 14747 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14748 } 14749 #endif 14750 } 14751 14752 /* 14753 * Holding the dtrace_proc privilege gives control over fasttrap 14754 * and pid providers. We need to grant wider destructive 14755 * privileges in the event that the user has proc_owner and/or 14756 * proc_zone. 14757 */ 14758 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14759 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14760 state->dts_cred.dcr_action |= 14761 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14762 14763 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14764 state->dts_cred.dcr_action |= 14765 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14766 } 14767 } 14768 14769 return (state); 14770 } 14771 14772 static int 14773 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 14774 { 14775 dtrace_optval_t *opt = state->dts_options, size; 14776 processorid_t cpu = 0;; 14777 int flags = 0, rval, factor, divisor = 1; 14778 14779 ASSERT(MUTEX_HELD(&dtrace_lock)); 14780 ASSERT(MUTEX_HELD(&cpu_lock)); 14781 ASSERT(which < DTRACEOPT_MAX); 14782 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 14783 (state == dtrace_anon.dta_state && 14784 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 14785 14786 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 14787 return (0); 14788 14789 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 14790 cpu = opt[DTRACEOPT_CPU]; 14791 14792 if (which == DTRACEOPT_SPECSIZE) 14793 flags |= DTRACEBUF_NOSWITCH; 14794 14795 if (which == DTRACEOPT_BUFSIZE) { 14796 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 14797 flags |= DTRACEBUF_RING; 14798 14799 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 14800 flags |= DTRACEBUF_FILL; 14801 14802 if (state != dtrace_anon.dta_state || 14803 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14804 flags |= DTRACEBUF_INACTIVE; 14805 } 14806 14807 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 14808 /* 14809 * The size must be 8-byte aligned. If the size is not 8-byte 14810 * aligned, drop it down by the difference. 14811 */ 14812 if (size & (sizeof (uint64_t) - 1)) 14813 size -= size & (sizeof (uint64_t) - 1); 14814 14815 if (size < state->dts_reserve) { 14816 /* 14817 * Buffers always must be large enough to accommodate 14818 * their prereserved space. We return E2BIG instead 14819 * of ENOMEM in this case to allow for user-level 14820 * software to differentiate the cases. 14821 */ 14822 return (E2BIG); 14823 } 14824 14825 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 14826 14827 if (rval != ENOMEM) { 14828 opt[which] = size; 14829 return (rval); 14830 } 14831 14832 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14833 return (rval); 14834 14835 for (divisor = 2; divisor < factor; divisor <<= 1) 14836 continue; 14837 } 14838 14839 return (ENOMEM); 14840 } 14841 14842 static int 14843 dtrace_state_buffers(dtrace_state_t *state) 14844 { 14845 dtrace_speculation_t *spec = state->dts_speculations; 14846 int rval, i; 14847 14848 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 14849 DTRACEOPT_BUFSIZE)) != 0) 14850 return (rval); 14851 14852 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 14853 DTRACEOPT_AGGSIZE)) != 0) 14854 return (rval); 14855 14856 for (i = 0; i < state->dts_nspeculations; i++) { 14857 if ((rval = dtrace_state_buffer(state, 14858 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 14859 return (rval); 14860 } 14861 14862 return (0); 14863 } 14864 14865 static void 14866 dtrace_state_prereserve(dtrace_state_t *state) 14867 { 14868 dtrace_ecb_t *ecb; 14869 dtrace_probe_t *probe; 14870 14871 state->dts_reserve = 0; 14872 14873 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 14874 return; 14875 14876 /* 14877 * If our buffer policy is a "fill" buffer policy, we need to set the 14878 * prereserved space to be the space required by the END probes. 14879 */ 14880 probe = dtrace_probes[dtrace_probeid_end - 1]; 14881 ASSERT(probe != NULL); 14882 14883 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 14884 if (ecb->dte_state != state) 14885 continue; 14886 14887 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 14888 } 14889 } 14890 14891 static int 14892 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 14893 { 14894 dtrace_optval_t *opt = state->dts_options, sz, nspec; 14895 dtrace_speculation_t *spec; 14896 dtrace_buffer_t *buf; 14897 #ifdef illumos 14898 cyc_handler_t hdlr; 14899 cyc_time_t when; 14900 #endif 14901 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 14902 dtrace_icookie_t cookie; 14903 14904 mutex_enter(&cpu_lock); 14905 mutex_enter(&dtrace_lock); 14906 14907 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 14908 rval = EBUSY; 14909 goto out; 14910 } 14911 14912 /* 14913 * Before we can perform any checks, we must prime all of the 14914 * retained enablings that correspond to this state. 14915 */ 14916 dtrace_enabling_prime(state); 14917 14918 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 14919 rval = EACCES; 14920 goto out; 14921 } 14922 14923 dtrace_state_prereserve(state); 14924 14925 /* 14926 * Now we want to do is try to allocate our speculations. 14927 * We do not automatically resize the number of speculations; if 14928 * this fails, we will fail the operation. 14929 */ 14930 nspec = opt[DTRACEOPT_NSPEC]; 14931 ASSERT(nspec != DTRACEOPT_UNSET); 14932 14933 if (nspec > INT_MAX) { 14934 rval = ENOMEM; 14935 goto out; 14936 } 14937 14938 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 14939 KM_NOSLEEP | KM_NORMALPRI); 14940 14941 if (spec == NULL) { 14942 rval = ENOMEM; 14943 goto out; 14944 } 14945 14946 state->dts_speculations = spec; 14947 state->dts_nspeculations = (int)nspec; 14948 14949 for (i = 0; i < nspec; i++) { 14950 if ((buf = kmem_zalloc(bufsize, 14951 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 14952 rval = ENOMEM; 14953 goto err; 14954 } 14955 14956 spec[i].dtsp_buffer = buf; 14957 } 14958 14959 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 14960 if (dtrace_anon.dta_state == NULL) { 14961 rval = ENOENT; 14962 goto out; 14963 } 14964 14965 if (state->dts_necbs != 0) { 14966 rval = EALREADY; 14967 goto out; 14968 } 14969 14970 state->dts_anon = dtrace_anon_grab(); 14971 ASSERT(state->dts_anon != NULL); 14972 state = state->dts_anon; 14973 14974 /* 14975 * We want "grabanon" to be set in the grabbed state, so we'll 14976 * copy that option value from the grabbing state into the 14977 * grabbed state. 14978 */ 14979 state->dts_options[DTRACEOPT_GRABANON] = 14980 opt[DTRACEOPT_GRABANON]; 14981 14982 *cpu = dtrace_anon.dta_beganon; 14983 14984 /* 14985 * If the anonymous state is active (as it almost certainly 14986 * is if the anonymous enabling ultimately matched anything), 14987 * we don't allow any further option processing -- but we 14988 * don't return failure. 14989 */ 14990 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 14991 goto out; 14992 } 14993 14994 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 14995 opt[DTRACEOPT_AGGSIZE] != 0) { 14996 if (state->dts_aggregations == NULL) { 14997 /* 14998 * We're not going to create an aggregation buffer 14999 * because we don't have any ECBs that contain 15000 * aggregations -- set this option to 0. 15001 */ 15002 opt[DTRACEOPT_AGGSIZE] = 0; 15003 } else { 15004 /* 15005 * If we have an aggregation buffer, we must also have 15006 * a buffer to use as scratch. 15007 */ 15008 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 15009 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 15010 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 15011 } 15012 } 15013 } 15014 15015 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 15016 opt[DTRACEOPT_SPECSIZE] != 0) { 15017 if (!state->dts_speculates) { 15018 /* 15019 * We're not going to create speculation buffers 15020 * because we don't have any ECBs that actually 15021 * speculate -- set the speculation size to 0. 15022 */ 15023 opt[DTRACEOPT_SPECSIZE] = 0; 15024 } 15025 } 15026 15027 /* 15028 * The bare minimum size for any buffer that we're actually going to 15029 * do anything to is sizeof (uint64_t). 15030 */ 15031 sz = sizeof (uint64_t); 15032 15033 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 15034 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 15035 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 15036 /* 15037 * A buffer size has been explicitly set to 0 (or to a size 15038 * that will be adjusted to 0) and we need the space -- we 15039 * need to return failure. We return ENOSPC to differentiate 15040 * it from failing to allocate a buffer due to failure to meet 15041 * the reserve (for which we return E2BIG). 15042 */ 15043 rval = ENOSPC; 15044 goto out; 15045 } 15046 15047 if ((rval = dtrace_state_buffers(state)) != 0) 15048 goto err; 15049 15050 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 15051 sz = dtrace_dstate_defsize; 15052 15053 do { 15054 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 15055 15056 if (rval == 0) 15057 break; 15058 15059 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 15060 goto err; 15061 } while (sz >>= 1); 15062 15063 opt[DTRACEOPT_DYNVARSIZE] = sz; 15064 15065 if (rval != 0) 15066 goto err; 15067 15068 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 15069 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 15070 15071 if (opt[DTRACEOPT_CLEANRATE] == 0) 15072 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 15073 15074 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 15075 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 15076 15077 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 15078 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 15079 15080 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 15081 #ifdef illumos 15082 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 15083 hdlr.cyh_arg = state; 15084 hdlr.cyh_level = CY_LOW_LEVEL; 15085 15086 when.cyt_when = 0; 15087 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 15088 15089 state->dts_cleaner = cyclic_add(&hdlr, &when); 15090 15091 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 15092 hdlr.cyh_arg = state; 15093 hdlr.cyh_level = CY_LOW_LEVEL; 15094 15095 when.cyt_when = 0; 15096 when.cyt_interval = dtrace_deadman_interval; 15097 15098 state->dts_deadman = cyclic_add(&hdlr, &when); 15099 #else 15100 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 15101 dtrace_state_clean, state); 15102 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 15103 dtrace_state_deadman, state); 15104 #endif 15105 15106 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 15107 15108 #ifdef illumos 15109 if (state->dts_getf != 0 && 15110 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 15111 /* 15112 * We don't have kernel privs but we have at least one call 15113 * to getf(); we need to bump our zone's count, and (if 15114 * this is the first enabling to have an unprivileged call 15115 * to getf()) we need to hook into closef(). 15116 */ 15117 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 15118 15119 if (dtrace_getf++ == 0) { 15120 ASSERT(dtrace_closef == NULL); 15121 dtrace_closef = dtrace_getf_barrier; 15122 } 15123 } 15124 #endif 15125 15126 /* 15127 * Now it's time to actually fire the BEGIN probe. We need to disable 15128 * interrupts here both to record the CPU on which we fired the BEGIN 15129 * probe (the data from this CPU will be processed first at user 15130 * level) and to manually activate the buffer for this CPU. 15131 */ 15132 cookie = dtrace_interrupt_disable(); 15133 *cpu = curcpu; 15134 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 15135 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 15136 15137 dtrace_probe(dtrace_probeid_begin, 15138 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 15139 dtrace_interrupt_enable(cookie); 15140 /* 15141 * We may have had an exit action from a BEGIN probe; only change our 15142 * state to ACTIVE if we're still in WARMUP. 15143 */ 15144 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 15145 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 15146 15147 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 15148 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 15149 15150 #ifdef __FreeBSD__ 15151 /* 15152 * We enable anonymous tracing before APs are started, so we must 15153 * activate buffers using the current CPU. 15154 */ 15155 if (state == dtrace_anon.dta_state) 15156 for (int i = 0; i < NCPU; i++) 15157 dtrace_buffer_activate_cpu(state, i); 15158 else 15159 dtrace_xcall(DTRACE_CPUALL, 15160 (dtrace_xcall_t)dtrace_buffer_activate, state); 15161 #else 15162 /* 15163 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 15164 * want each CPU to transition its principal buffer out of the 15165 * INACTIVE state. Doing this assures that no CPU will suddenly begin 15166 * processing an ECB halfway down a probe's ECB chain; all CPUs will 15167 * atomically transition from processing none of a state's ECBs to 15168 * processing all of them. 15169 */ 15170 dtrace_xcall(DTRACE_CPUALL, 15171 (dtrace_xcall_t)dtrace_buffer_activate, state); 15172 #endif 15173 goto out; 15174 15175 err: 15176 dtrace_buffer_free(state->dts_buffer); 15177 dtrace_buffer_free(state->dts_aggbuffer); 15178 15179 if ((nspec = state->dts_nspeculations) == 0) { 15180 ASSERT(state->dts_speculations == NULL); 15181 goto out; 15182 } 15183 15184 spec = state->dts_speculations; 15185 ASSERT(spec != NULL); 15186 15187 for (i = 0; i < state->dts_nspeculations; i++) { 15188 if ((buf = spec[i].dtsp_buffer) == NULL) 15189 break; 15190 15191 dtrace_buffer_free(buf); 15192 kmem_free(buf, bufsize); 15193 } 15194 15195 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15196 state->dts_nspeculations = 0; 15197 state->dts_speculations = NULL; 15198 15199 out: 15200 mutex_exit(&dtrace_lock); 15201 mutex_exit(&cpu_lock); 15202 15203 return (rval); 15204 } 15205 15206 static int 15207 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 15208 { 15209 dtrace_icookie_t cookie; 15210 15211 ASSERT(MUTEX_HELD(&dtrace_lock)); 15212 15213 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 15214 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 15215 return (EINVAL); 15216 15217 /* 15218 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 15219 * to be sure that every CPU has seen it. See below for the details 15220 * on why this is done. 15221 */ 15222 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 15223 dtrace_sync(); 15224 15225 /* 15226 * By this point, it is impossible for any CPU to be still processing 15227 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 15228 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 15229 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 15230 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 15231 * iff we're in the END probe. 15232 */ 15233 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 15234 dtrace_sync(); 15235 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 15236 15237 /* 15238 * Finally, we can release the reserve and call the END probe. We 15239 * disable interrupts across calling the END probe to allow us to 15240 * return the CPU on which we actually called the END probe. This 15241 * allows user-land to be sure that this CPU's principal buffer is 15242 * processed last. 15243 */ 15244 state->dts_reserve = 0; 15245 15246 cookie = dtrace_interrupt_disable(); 15247 *cpu = curcpu; 15248 dtrace_probe(dtrace_probeid_end, 15249 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 15250 dtrace_interrupt_enable(cookie); 15251 15252 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 15253 dtrace_sync(); 15254 15255 #ifdef illumos 15256 if (state->dts_getf != 0 && 15257 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 15258 /* 15259 * We don't have kernel privs but we have at least one call 15260 * to getf(); we need to lower our zone's count, and (if 15261 * this is the last enabling to have an unprivileged call 15262 * to getf()) we need to clear the closef() hook. 15263 */ 15264 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 15265 ASSERT(dtrace_closef == dtrace_getf_barrier); 15266 ASSERT(dtrace_getf > 0); 15267 15268 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 15269 15270 if (--dtrace_getf == 0) 15271 dtrace_closef = NULL; 15272 } 15273 #endif 15274 15275 return (0); 15276 } 15277 15278 static int 15279 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 15280 dtrace_optval_t val) 15281 { 15282 ASSERT(MUTEX_HELD(&dtrace_lock)); 15283 15284 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 15285 return (EBUSY); 15286 15287 if (option >= DTRACEOPT_MAX) 15288 return (EINVAL); 15289 15290 if (option != DTRACEOPT_CPU && val < 0) 15291 return (EINVAL); 15292 15293 switch (option) { 15294 case DTRACEOPT_DESTRUCTIVE: 15295 if (dtrace_destructive_disallow) 15296 return (EACCES); 15297 15298 state->dts_cred.dcr_destructive = 1; 15299 break; 15300 15301 case DTRACEOPT_BUFSIZE: 15302 case DTRACEOPT_DYNVARSIZE: 15303 case DTRACEOPT_AGGSIZE: 15304 case DTRACEOPT_SPECSIZE: 15305 case DTRACEOPT_STRSIZE: 15306 if (val < 0) 15307 return (EINVAL); 15308 15309 if (val >= LONG_MAX) { 15310 /* 15311 * If this is an otherwise negative value, set it to 15312 * the highest multiple of 128m less than LONG_MAX. 15313 * Technically, we're adjusting the size without 15314 * regard to the buffer resizing policy, but in fact, 15315 * this has no effect -- if we set the buffer size to 15316 * ~LONG_MAX and the buffer policy is ultimately set to 15317 * be "manual", the buffer allocation is guaranteed to 15318 * fail, if only because the allocation requires two 15319 * buffers. (We set the the size to the highest 15320 * multiple of 128m because it ensures that the size 15321 * will remain a multiple of a megabyte when 15322 * repeatedly halved -- all the way down to 15m.) 15323 */ 15324 val = LONG_MAX - (1 << 27) + 1; 15325 } 15326 } 15327 15328 state->dts_options[option] = val; 15329 15330 return (0); 15331 } 15332 15333 static void 15334 dtrace_state_destroy(dtrace_state_t *state) 15335 { 15336 dtrace_ecb_t *ecb; 15337 dtrace_vstate_t *vstate = &state->dts_vstate; 15338 #ifdef illumos 15339 minor_t minor = getminor(state->dts_dev); 15340 #endif 15341 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 15342 dtrace_speculation_t *spec = state->dts_speculations; 15343 int nspec = state->dts_nspeculations; 15344 uint32_t match; 15345 15346 ASSERT(MUTEX_HELD(&dtrace_lock)); 15347 ASSERT(MUTEX_HELD(&cpu_lock)); 15348 15349 /* 15350 * First, retract any retained enablings for this state. 15351 */ 15352 dtrace_enabling_retract(state); 15353 ASSERT(state->dts_nretained == 0); 15354 15355 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 15356 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 15357 /* 15358 * We have managed to come into dtrace_state_destroy() on a 15359 * hot enabling -- almost certainly because of a disorderly 15360 * shutdown of a consumer. (That is, a consumer that is 15361 * exiting without having called dtrace_stop().) In this case, 15362 * we're going to set our activity to be KILLED, and then 15363 * issue a sync to be sure that everyone is out of probe 15364 * context before we start blowing away ECBs. 15365 */ 15366 state->dts_activity = DTRACE_ACTIVITY_KILLED; 15367 dtrace_sync(); 15368 } 15369 15370 /* 15371 * Release the credential hold we took in dtrace_state_create(). 15372 */ 15373 if (state->dts_cred.dcr_cred != NULL) 15374 crfree(state->dts_cred.dcr_cred); 15375 15376 /* 15377 * Now we can safely disable and destroy any enabled probes. Because 15378 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 15379 * (especially if they're all enabled), we take two passes through the 15380 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 15381 * in the second we disable whatever is left over. 15382 */ 15383 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 15384 for (i = 0; i < state->dts_necbs; i++) { 15385 if ((ecb = state->dts_ecbs[i]) == NULL) 15386 continue; 15387 15388 if (match && ecb->dte_probe != NULL) { 15389 dtrace_probe_t *probe = ecb->dte_probe; 15390 dtrace_provider_t *prov = probe->dtpr_provider; 15391 15392 if (!(prov->dtpv_priv.dtpp_flags & match)) 15393 continue; 15394 } 15395 15396 dtrace_ecb_disable(ecb); 15397 dtrace_ecb_destroy(ecb); 15398 } 15399 15400 if (!match) 15401 break; 15402 } 15403 15404 /* 15405 * Before we free the buffers, perform one more sync to assure that 15406 * every CPU is out of probe context. 15407 */ 15408 dtrace_sync(); 15409 15410 dtrace_buffer_free(state->dts_buffer); 15411 dtrace_buffer_free(state->dts_aggbuffer); 15412 15413 for (i = 0; i < nspec; i++) 15414 dtrace_buffer_free(spec[i].dtsp_buffer); 15415 15416 #ifdef illumos 15417 if (state->dts_cleaner != CYCLIC_NONE) 15418 cyclic_remove(state->dts_cleaner); 15419 15420 if (state->dts_deadman != CYCLIC_NONE) 15421 cyclic_remove(state->dts_deadman); 15422 #else 15423 callout_stop(&state->dts_cleaner); 15424 callout_drain(&state->dts_cleaner); 15425 callout_stop(&state->dts_deadman); 15426 callout_drain(&state->dts_deadman); 15427 #endif 15428 15429 dtrace_dstate_fini(&vstate->dtvs_dynvars); 15430 dtrace_vstate_fini(vstate); 15431 if (state->dts_ecbs != NULL) 15432 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 15433 15434 if (state->dts_aggregations != NULL) { 15435 #ifdef DEBUG 15436 for (i = 0; i < state->dts_naggregations; i++) 15437 ASSERT(state->dts_aggregations[i] == NULL); 15438 #endif 15439 ASSERT(state->dts_naggregations > 0); 15440 kmem_free(state->dts_aggregations, 15441 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 15442 } 15443 15444 kmem_free(state->dts_buffer, bufsize); 15445 kmem_free(state->dts_aggbuffer, bufsize); 15446 15447 for (i = 0; i < nspec; i++) 15448 kmem_free(spec[i].dtsp_buffer, bufsize); 15449 15450 if (spec != NULL) 15451 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15452 15453 dtrace_format_destroy(state); 15454 15455 if (state->dts_aggid_arena != NULL) { 15456 #ifdef illumos 15457 vmem_destroy(state->dts_aggid_arena); 15458 #else 15459 delete_unrhdr(state->dts_aggid_arena); 15460 #endif 15461 state->dts_aggid_arena = NULL; 15462 } 15463 #ifdef illumos 15464 ddi_soft_state_free(dtrace_softstate, minor); 15465 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 15466 #endif 15467 } 15468 15469 /* 15470 * DTrace Anonymous Enabling Functions 15471 */ 15472 static dtrace_state_t * 15473 dtrace_anon_grab(void) 15474 { 15475 dtrace_state_t *state; 15476 15477 ASSERT(MUTEX_HELD(&dtrace_lock)); 15478 15479 if ((state = dtrace_anon.dta_state) == NULL) { 15480 ASSERT(dtrace_anon.dta_enabling == NULL); 15481 return (NULL); 15482 } 15483 15484 ASSERT(dtrace_anon.dta_enabling != NULL); 15485 ASSERT(dtrace_retained != NULL); 15486 15487 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 15488 dtrace_anon.dta_enabling = NULL; 15489 dtrace_anon.dta_state = NULL; 15490 15491 return (state); 15492 } 15493 15494 static void 15495 dtrace_anon_property(void) 15496 { 15497 int i, rv; 15498 dtrace_state_t *state; 15499 dof_hdr_t *dof; 15500 char c[32]; /* enough for "dof-data-" + digits */ 15501 15502 ASSERT(MUTEX_HELD(&dtrace_lock)); 15503 ASSERT(MUTEX_HELD(&cpu_lock)); 15504 15505 for (i = 0; ; i++) { 15506 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 15507 15508 dtrace_err_verbose = 1; 15509 15510 if ((dof = dtrace_dof_property(c)) == NULL) { 15511 dtrace_err_verbose = 0; 15512 break; 15513 } 15514 15515 #ifdef illumos 15516 /* 15517 * We want to create anonymous state, so we need to transition 15518 * the kernel debugger to indicate that DTrace is active. If 15519 * this fails (e.g. because the debugger has modified text in 15520 * some way), we won't continue with the processing. 15521 */ 15522 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15523 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 15524 "enabling ignored."); 15525 dtrace_dof_destroy(dof); 15526 break; 15527 } 15528 #endif 15529 15530 /* 15531 * If we haven't allocated an anonymous state, we'll do so now. 15532 */ 15533 if ((state = dtrace_anon.dta_state) == NULL) { 15534 state = dtrace_state_create(NULL, NULL); 15535 dtrace_anon.dta_state = state; 15536 15537 if (state == NULL) { 15538 /* 15539 * This basically shouldn't happen: the only 15540 * failure mode from dtrace_state_create() is a 15541 * failure of ddi_soft_state_zalloc() that 15542 * itself should never happen. Still, the 15543 * interface allows for a failure mode, and 15544 * we want to fail as gracefully as possible: 15545 * we'll emit an error message and cease 15546 * processing anonymous state in this case. 15547 */ 15548 cmn_err(CE_WARN, "failed to create " 15549 "anonymous state"); 15550 dtrace_dof_destroy(dof); 15551 break; 15552 } 15553 } 15554 15555 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 15556 &dtrace_anon.dta_enabling, 0, 0, B_TRUE); 15557 15558 if (rv == 0) 15559 rv = dtrace_dof_options(dof, state); 15560 15561 dtrace_err_verbose = 0; 15562 dtrace_dof_destroy(dof); 15563 15564 if (rv != 0) { 15565 /* 15566 * This is malformed DOF; chuck any anonymous state 15567 * that we created. 15568 */ 15569 ASSERT(dtrace_anon.dta_enabling == NULL); 15570 dtrace_state_destroy(state); 15571 dtrace_anon.dta_state = NULL; 15572 break; 15573 } 15574 15575 ASSERT(dtrace_anon.dta_enabling != NULL); 15576 } 15577 15578 if (dtrace_anon.dta_enabling != NULL) { 15579 int rval; 15580 15581 /* 15582 * dtrace_enabling_retain() can only fail because we are 15583 * trying to retain more enablings than are allowed -- but 15584 * we only have one anonymous enabling, and we are guaranteed 15585 * to be allowed at least one retained enabling; we assert 15586 * that dtrace_enabling_retain() returns success. 15587 */ 15588 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 15589 ASSERT(rval == 0); 15590 15591 dtrace_enabling_dump(dtrace_anon.dta_enabling); 15592 } 15593 } 15594 15595 /* 15596 * DTrace Helper Functions 15597 */ 15598 static void 15599 dtrace_helper_trace(dtrace_helper_action_t *helper, 15600 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 15601 { 15602 uint32_t size, next, nnext, i; 15603 dtrace_helptrace_t *ent, *buffer; 15604 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 15605 15606 if ((buffer = dtrace_helptrace_buffer) == NULL) 15607 return; 15608 15609 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 15610 15611 /* 15612 * What would a tracing framework be without its own tracing 15613 * framework? (Well, a hell of a lot simpler, for starters...) 15614 */ 15615 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 15616 sizeof (uint64_t) - sizeof (uint64_t); 15617 15618 /* 15619 * Iterate until we can allocate a slot in the trace buffer. 15620 */ 15621 do { 15622 next = dtrace_helptrace_next; 15623 15624 if (next + size < dtrace_helptrace_bufsize) { 15625 nnext = next + size; 15626 } else { 15627 nnext = size; 15628 } 15629 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 15630 15631 /* 15632 * We have our slot; fill it in. 15633 */ 15634 if (nnext == size) { 15635 dtrace_helptrace_wrapped++; 15636 next = 0; 15637 } 15638 15639 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next); 15640 ent->dtht_helper = helper; 15641 ent->dtht_where = where; 15642 ent->dtht_nlocals = vstate->dtvs_nlocals; 15643 15644 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 15645 mstate->dtms_fltoffs : -1; 15646 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 15647 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 15648 15649 for (i = 0; i < vstate->dtvs_nlocals; i++) { 15650 dtrace_statvar_t *svar; 15651 15652 if ((svar = vstate->dtvs_locals[i]) == NULL) 15653 continue; 15654 15655 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 15656 ent->dtht_locals[i] = 15657 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 15658 } 15659 } 15660 15661 static uint64_t 15662 dtrace_helper(int which, dtrace_mstate_t *mstate, 15663 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 15664 { 15665 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 15666 uint64_t sarg0 = mstate->dtms_arg[0]; 15667 uint64_t sarg1 = mstate->dtms_arg[1]; 15668 uint64_t rval = 0; 15669 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 15670 dtrace_helper_action_t *helper; 15671 dtrace_vstate_t *vstate; 15672 dtrace_difo_t *pred; 15673 int i, trace = dtrace_helptrace_buffer != NULL; 15674 15675 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 15676 15677 if (helpers == NULL) 15678 return (0); 15679 15680 if ((helper = helpers->dthps_actions[which]) == NULL) 15681 return (0); 15682 15683 vstate = &helpers->dthps_vstate; 15684 mstate->dtms_arg[0] = arg0; 15685 mstate->dtms_arg[1] = arg1; 15686 15687 /* 15688 * Now iterate over each helper. If its predicate evaluates to 'true', 15689 * we'll call the corresponding actions. Note that the below calls 15690 * to dtrace_dif_emulate() may set faults in machine state. This is 15691 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 15692 * the stored DIF offset with its own (which is the desired behavior). 15693 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 15694 * from machine state; this is okay, too. 15695 */ 15696 for (; helper != NULL; helper = helper->dtha_next) { 15697 if ((pred = helper->dtha_predicate) != NULL) { 15698 if (trace) 15699 dtrace_helper_trace(helper, mstate, vstate, 0); 15700 15701 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 15702 goto next; 15703 15704 if (*flags & CPU_DTRACE_FAULT) 15705 goto err; 15706 } 15707 15708 for (i = 0; i < helper->dtha_nactions; i++) { 15709 if (trace) 15710 dtrace_helper_trace(helper, 15711 mstate, vstate, i + 1); 15712 15713 rval = dtrace_dif_emulate(helper->dtha_actions[i], 15714 mstate, vstate, state); 15715 15716 if (*flags & CPU_DTRACE_FAULT) 15717 goto err; 15718 } 15719 15720 next: 15721 if (trace) 15722 dtrace_helper_trace(helper, mstate, vstate, 15723 DTRACE_HELPTRACE_NEXT); 15724 } 15725 15726 if (trace) 15727 dtrace_helper_trace(helper, mstate, vstate, 15728 DTRACE_HELPTRACE_DONE); 15729 15730 /* 15731 * Restore the arg0 that we saved upon entry. 15732 */ 15733 mstate->dtms_arg[0] = sarg0; 15734 mstate->dtms_arg[1] = sarg1; 15735 15736 return (rval); 15737 15738 err: 15739 if (trace) 15740 dtrace_helper_trace(helper, mstate, vstate, 15741 DTRACE_HELPTRACE_ERR); 15742 15743 /* 15744 * Restore the arg0 that we saved upon entry. 15745 */ 15746 mstate->dtms_arg[0] = sarg0; 15747 mstate->dtms_arg[1] = sarg1; 15748 15749 return (0); 15750 } 15751 15752 static void 15753 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 15754 dtrace_vstate_t *vstate) 15755 { 15756 int i; 15757 15758 if (helper->dtha_predicate != NULL) 15759 dtrace_difo_release(helper->dtha_predicate, vstate); 15760 15761 for (i = 0; i < helper->dtha_nactions; i++) { 15762 ASSERT(helper->dtha_actions[i] != NULL); 15763 dtrace_difo_release(helper->dtha_actions[i], vstate); 15764 } 15765 15766 kmem_free(helper->dtha_actions, 15767 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 15768 kmem_free(helper, sizeof (dtrace_helper_action_t)); 15769 } 15770 15771 static int 15772 dtrace_helper_destroygen(dtrace_helpers_t *help, int gen) 15773 { 15774 proc_t *p = curproc; 15775 dtrace_vstate_t *vstate; 15776 int i; 15777 15778 if (help == NULL) 15779 help = p->p_dtrace_helpers; 15780 15781 ASSERT(MUTEX_HELD(&dtrace_lock)); 15782 15783 if (help == NULL || gen > help->dthps_generation) 15784 return (EINVAL); 15785 15786 vstate = &help->dthps_vstate; 15787 15788 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15789 dtrace_helper_action_t *last = NULL, *h, *next; 15790 15791 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15792 next = h->dtha_next; 15793 15794 if (h->dtha_generation == gen) { 15795 if (last != NULL) { 15796 last->dtha_next = next; 15797 } else { 15798 help->dthps_actions[i] = next; 15799 } 15800 15801 dtrace_helper_action_destroy(h, vstate); 15802 } else { 15803 last = h; 15804 } 15805 } 15806 } 15807 15808 /* 15809 * Interate until we've cleared out all helper providers with the 15810 * given generation number. 15811 */ 15812 for (;;) { 15813 dtrace_helper_provider_t *prov; 15814 15815 /* 15816 * Look for a helper provider with the right generation. We 15817 * have to start back at the beginning of the list each time 15818 * because we drop dtrace_lock. It's unlikely that we'll make 15819 * more than two passes. 15820 */ 15821 for (i = 0; i < help->dthps_nprovs; i++) { 15822 prov = help->dthps_provs[i]; 15823 15824 if (prov->dthp_generation == gen) 15825 break; 15826 } 15827 15828 /* 15829 * If there were no matches, we're done. 15830 */ 15831 if (i == help->dthps_nprovs) 15832 break; 15833 15834 /* 15835 * Move the last helper provider into this slot. 15836 */ 15837 help->dthps_nprovs--; 15838 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 15839 help->dthps_provs[help->dthps_nprovs] = NULL; 15840 15841 mutex_exit(&dtrace_lock); 15842 15843 /* 15844 * If we have a meta provider, remove this helper provider. 15845 */ 15846 mutex_enter(&dtrace_meta_lock); 15847 if (dtrace_meta_pid != NULL) { 15848 ASSERT(dtrace_deferred_pid == NULL); 15849 dtrace_helper_provider_remove(&prov->dthp_prov, 15850 p->p_pid); 15851 } 15852 mutex_exit(&dtrace_meta_lock); 15853 15854 dtrace_helper_provider_destroy(prov); 15855 15856 mutex_enter(&dtrace_lock); 15857 } 15858 15859 return (0); 15860 } 15861 15862 static int 15863 dtrace_helper_validate(dtrace_helper_action_t *helper) 15864 { 15865 int err = 0, i; 15866 dtrace_difo_t *dp; 15867 15868 if ((dp = helper->dtha_predicate) != NULL) 15869 err += dtrace_difo_validate_helper(dp); 15870 15871 for (i = 0; i < helper->dtha_nactions; i++) 15872 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 15873 15874 return (err == 0); 15875 } 15876 15877 static int 15878 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep, 15879 dtrace_helpers_t *help) 15880 { 15881 dtrace_helper_action_t *helper, *last; 15882 dtrace_actdesc_t *act; 15883 dtrace_vstate_t *vstate; 15884 dtrace_predicate_t *pred; 15885 int count = 0, nactions = 0, i; 15886 15887 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 15888 return (EINVAL); 15889 15890 last = help->dthps_actions[which]; 15891 vstate = &help->dthps_vstate; 15892 15893 for (count = 0; last != NULL; last = last->dtha_next) { 15894 count++; 15895 if (last->dtha_next == NULL) 15896 break; 15897 } 15898 15899 /* 15900 * If we already have dtrace_helper_actions_max helper actions for this 15901 * helper action type, we'll refuse to add a new one. 15902 */ 15903 if (count >= dtrace_helper_actions_max) 15904 return (ENOSPC); 15905 15906 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 15907 helper->dtha_generation = help->dthps_generation; 15908 15909 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 15910 ASSERT(pred->dtp_difo != NULL); 15911 dtrace_difo_hold(pred->dtp_difo); 15912 helper->dtha_predicate = pred->dtp_difo; 15913 } 15914 15915 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 15916 if (act->dtad_kind != DTRACEACT_DIFEXPR) 15917 goto err; 15918 15919 if (act->dtad_difo == NULL) 15920 goto err; 15921 15922 nactions++; 15923 } 15924 15925 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 15926 (helper->dtha_nactions = nactions), KM_SLEEP); 15927 15928 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 15929 dtrace_difo_hold(act->dtad_difo); 15930 helper->dtha_actions[i++] = act->dtad_difo; 15931 } 15932 15933 if (!dtrace_helper_validate(helper)) 15934 goto err; 15935 15936 if (last == NULL) { 15937 help->dthps_actions[which] = helper; 15938 } else { 15939 last->dtha_next = helper; 15940 } 15941 15942 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 15943 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 15944 dtrace_helptrace_next = 0; 15945 } 15946 15947 return (0); 15948 err: 15949 dtrace_helper_action_destroy(helper, vstate); 15950 return (EINVAL); 15951 } 15952 15953 static void 15954 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 15955 dof_helper_t *dofhp) 15956 { 15957 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 15958 15959 mutex_enter(&dtrace_meta_lock); 15960 mutex_enter(&dtrace_lock); 15961 15962 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 15963 /* 15964 * If the dtrace module is loaded but not attached, or if 15965 * there aren't isn't a meta provider registered to deal with 15966 * these provider descriptions, we need to postpone creating 15967 * the actual providers until later. 15968 */ 15969 15970 if (help->dthps_next == NULL && help->dthps_prev == NULL && 15971 dtrace_deferred_pid != help) { 15972 help->dthps_deferred = 1; 15973 help->dthps_pid = p->p_pid; 15974 help->dthps_next = dtrace_deferred_pid; 15975 help->dthps_prev = NULL; 15976 if (dtrace_deferred_pid != NULL) 15977 dtrace_deferred_pid->dthps_prev = help; 15978 dtrace_deferred_pid = help; 15979 } 15980 15981 mutex_exit(&dtrace_lock); 15982 15983 } else if (dofhp != NULL) { 15984 /* 15985 * If the dtrace module is loaded and we have a particular 15986 * helper provider description, pass that off to the 15987 * meta provider. 15988 */ 15989 15990 mutex_exit(&dtrace_lock); 15991 15992 dtrace_helper_provide(dofhp, p->p_pid); 15993 15994 } else { 15995 /* 15996 * Otherwise, just pass all the helper provider descriptions 15997 * off to the meta provider. 15998 */ 15999 16000 int i; 16001 mutex_exit(&dtrace_lock); 16002 16003 for (i = 0; i < help->dthps_nprovs; i++) { 16004 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 16005 p->p_pid); 16006 } 16007 } 16008 16009 mutex_exit(&dtrace_meta_lock); 16010 } 16011 16012 static int 16013 dtrace_helper_provider_add(dof_helper_t *dofhp, dtrace_helpers_t *help, int gen) 16014 { 16015 dtrace_helper_provider_t *hprov, **tmp_provs; 16016 uint_t tmp_maxprovs, i; 16017 16018 ASSERT(MUTEX_HELD(&dtrace_lock)); 16019 ASSERT(help != NULL); 16020 16021 /* 16022 * If we already have dtrace_helper_providers_max helper providers, 16023 * we're refuse to add a new one. 16024 */ 16025 if (help->dthps_nprovs >= dtrace_helper_providers_max) 16026 return (ENOSPC); 16027 16028 /* 16029 * Check to make sure this isn't a duplicate. 16030 */ 16031 for (i = 0; i < help->dthps_nprovs; i++) { 16032 if (dofhp->dofhp_addr == 16033 help->dthps_provs[i]->dthp_prov.dofhp_addr) 16034 return (EALREADY); 16035 } 16036 16037 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 16038 hprov->dthp_prov = *dofhp; 16039 hprov->dthp_ref = 1; 16040 hprov->dthp_generation = gen; 16041 16042 /* 16043 * Allocate a bigger table for helper providers if it's already full. 16044 */ 16045 if (help->dthps_maxprovs == help->dthps_nprovs) { 16046 tmp_maxprovs = help->dthps_maxprovs; 16047 tmp_provs = help->dthps_provs; 16048 16049 if (help->dthps_maxprovs == 0) 16050 help->dthps_maxprovs = 2; 16051 else 16052 help->dthps_maxprovs *= 2; 16053 if (help->dthps_maxprovs > dtrace_helper_providers_max) 16054 help->dthps_maxprovs = dtrace_helper_providers_max; 16055 16056 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 16057 16058 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 16059 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16060 16061 if (tmp_provs != NULL) { 16062 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 16063 sizeof (dtrace_helper_provider_t *)); 16064 kmem_free(tmp_provs, tmp_maxprovs * 16065 sizeof (dtrace_helper_provider_t *)); 16066 } 16067 } 16068 16069 help->dthps_provs[help->dthps_nprovs] = hprov; 16070 help->dthps_nprovs++; 16071 16072 return (0); 16073 } 16074 16075 static void 16076 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 16077 { 16078 mutex_enter(&dtrace_lock); 16079 16080 if (--hprov->dthp_ref == 0) { 16081 dof_hdr_t *dof; 16082 mutex_exit(&dtrace_lock); 16083 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 16084 dtrace_dof_destroy(dof); 16085 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 16086 } else { 16087 mutex_exit(&dtrace_lock); 16088 } 16089 } 16090 16091 static int 16092 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 16093 { 16094 uintptr_t daddr = (uintptr_t)dof; 16095 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 16096 dof_provider_t *provider; 16097 dof_probe_t *probe; 16098 uint8_t *arg; 16099 char *strtab, *typestr; 16100 dof_stridx_t typeidx; 16101 size_t typesz; 16102 uint_t nprobes, j, k; 16103 16104 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 16105 16106 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 16107 dtrace_dof_error(dof, "misaligned section offset"); 16108 return (-1); 16109 } 16110 16111 /* 16112 * The section needs to be large enough to contain the DOF provider 16113 * structure appropriate for the given version. 16114 */ 16115 if (sec->dofs_size < 16116 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 16117 offsetof(dof_provider_t, dofpv_prenoffs) : 16118 sizeof (dof_provider_t))) { 16119 dtrace_dof_error(dof, "provider section too small"); 16120 return (-1); 16121 } 16122 16123 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 16124 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 16125 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 16126 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 16127 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 16128 16129 if (str_sec == NULL || prb_sec == NULL || 16130 arg_sec == NULL || off_sec == NULL) 16131 return (-1); 16132 16133 enoff_sec = NULL; 16134 16135 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 16136 provider->dofpv_prenoffs != DOF_SECT_NONE && 16137 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 16138 provider->dofpv_prenoffs)) == NULL) 16139 return (-1); 16140 16141 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 16142 16143 if (provider->dofpv_name >= str_sec->dofs_size || 16144 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 16145 dtrace_dof_error(dof, "invalid provider name"); 16146 return (-1); 16147 } 16148 16149 if (prb_sec->dofs_entsize == 0 || 16150 prb_sec->dofs_entsize > prb_sec->dofs_size) { 16151 dtrace_dof_error(dof, "invalid entry size"); 16152 return (-1); 16153 } 16154 16155 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 16156 dtrace_dof_error(dof, "misaligned entry size"); 16157 return (-1); 16158 } 16159 16160 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 16161 dtrace_dof_error(dof, "invalid entry size"); 16162 return (-1); 16163 } 16164 16165 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 16166 dtrace_dof_error(dof, "misaligned section offset"); 16167 return (-1); 16168 } 16169 16170 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 16171 dtrace_dof_error(dof, "invalid entry size"); 16172 return (-1); 16173 } 16174 16175 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 16176 16177 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 16178 16179 /* 16180 * Take a pass through the probes to check for errors. 16181 */ 16182 for (j = 0; j < nprobes; j++) { 16183 probe = (dof_probe_t *)(uintptr_t)(daddr + 16184 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 16185 16186 if (probe->dofpr_func >= str_sec->dofs_size) { 16187 dtrace_dof_error(dof, "invalid function name"); 16188 return (-1); 16189 } 16190 16191 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 16192 dtrace_dof_error(dof, "function name too long"); 16193 /* 16194 * Keep going if the function name is too long. 16195 * Unlike provider and probe names, we cannot reasonably 16196 * impose restrictions on function names, since they're 16197 * a property of the code being instrumented. We will 16198 * skip this probe in dtrace_helper_provide_one(). 16199 */ 16200 } 16201 16202 if (probe->dofpr_name >= str_sec->dofs_size || 16203 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 16204 dtrace_dof_error(dof, "invalid probe name"); 16205 return (-1); 16206 } 16207 16208 /* 16209 * The offset count must not wrap the index, and the offsets 16210 * must also not overflow the section's data. 16211 */ 16212 if (probe->dofpr_offidx + probe->dofpr_noffs < 16213 probe->dofpr_offidx || 16214 (probe->dofpr_offidx + probe->dofpr_noffs) * 16215 off_sec->dofs_entsize > off_sec->dofs_size) { 16216 dtrace_dof_error(dof, "invalid probe offset"); 16217 return (-1); 16218 } 16219 16220 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 16221 /* 16222 * If there's no is-enabled offset section, make sure 16223 * there aren't any is-enabled offsets. Otherwise 16224 * perform the same checks as for probe offsets 16225 * (immediately above). 16226 */ 16227 if (enoff_sec == NULL) { 16228 if (probe->dofpr_enoffidx != 0 || 16229 probe->dofpr_nenoffs != 0) { 16230 dtrace_dof_error(dof, "is-enabled " 16231 "offsets with null section"); 16232 return (-1); 16233 } 16234 } else if (probe->dofpr_enoffidx + 16235 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 16236 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 16237 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 16238 dtrace_dof_error(dof, "invalid is-enabled " 16239 "offset"); 16240 return (-1); 16241 } 16242 16243 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 16244 dtrace_dof_error(dof, "zero probe and " 16245 "is-enabled offsets"); 16246 return (-1); 16247 } 16248 } else if (probe->dofpr_noffs == 0) { 16249 dtrace_dof_error(dof, "zero probe offsets"); 16250 return (-1); 16251 } 16252 16253 if (probe->dofpr_argidx + probe->dofpr_xargc < 16254 probe->dofpr_argidx || 16255 (probe->dofpr_argidx + probe->dofpr_xargc) * 16256 arg_sec->dofs_entsize > arg_sec->dofs_size) { 16257 dtrace_dof_error(dof, "invalid args"); 16258 return (-1); 16259 } 16260 16261 typeidx = probe->dofpr_nargv; 16262 typestr = strtab + probe->dofpr_nargv; 16263 for (k = 0; k < probe->dofpr_nargc; k++) { 16264 if (typeidx >= str_sec->dofs_size) { 16265 dtrace_dof_error(dof, "bad " 16266 "native argument type"); 16267 return (-1); 16268 } 16269 16270 typesz = strlen(typestr) + 1; 16271 if (typesz > DTRACE_ARGTYPELEN) { 16272 dtrace_dof_error(dof, "native " 16273 "argument type too long"); 16274 return (-1); 16275 } 16276 typeidx += typesz; 16277 typestr += typesz; 16278 } 16279 16280 typeidx = probe->dofpr_xargv; 16281 typestr = strtab + probe->dofpr_xargv; 16282 for (k = 0; k < probe->dofpr_xargc; k++) { 16283 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 16284 dtrace_dof_error(dof, "bad " 16285 "native argument index"); 16286 return (-1); 16287 } 16288 16289 if (typeidx >= str_sec->dofs_size) { 16290 dtrace_dof_error(dof, "bad " 16291 "translated argument type"); 16292 return (-1); 16293 } 16294 16295 typesz = strlen(typestr) + 1; 16296 if (typesz > DTRACE_ARGTYPELEN) { 16297 dtrace_dof_error(dof, "translated argument " 16298 "type too long"); 16299 return (-1); 16300 } 16301 16302 typeidx += typesz; 16303 typestr += typesz; 16304 } 16305 } 16306 16307 return (0); 16308 } 16309 16310 static int 16311 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp, struct proc *p) 16312 { 16313 dtrace_helpers_t *help; 16314 dtrace_vstate_t *vstate; 16315 dtrace_enabling_t *enab = NULL; 16316 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 16317 uintptr_t daddr = (uintptr_t)dof; 16318 16319 ASSERT(MUTEX_HELD(&dtrace_lock)); 16320 16321 if ((help = p->p_dtrace_helpers) == NULL) 16322 help = dtrace_helpers_create(p); 16323 16324 vstate = &help->dthps_vstate; 16325 16326 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, dhp->dofhp_addr, 16327 dhp->dofhp_dof, B_FALSE)) != 0) { 16328 dtrace_dof_destroy(dof); 16329 return (rv); 16330 } 16331 16332 /* 16333 * Look for helper providers and validate their descriptions. 16334 */ 16335 for (i = 0; i < dof->dofh_secnum; i++) { 16336 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 16337 dof->dofh_secoff + i * dof->dofh_secsize); 16338 16339 if (sec->dofs_type != DOF_SECT_PROVIDER) 16340 continue; 16341 16342 if (dtrace_helper_provider_validate(dof, sec) != 0) { 16343 dtrace_enabling_destroy(enab); 16344 dtrace_dof_destroy(dof); 16345 return (-1); 16346 } 16347 16348 nprovs++; 16349 } 16350 16351 /* 16352 * Now we need to walk through the ECB descriptions in the enabling. 16353 */ 16354 for (i = 0; i < enab->dten_ndesc; i++) { 16355 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 16356 dtrace_probedesc_t *desc = &ep->dted_probe; 16357 16358 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 16359 continue; 16360 16361 if (strcmp(desc->dtpd_mod, "helper") != 0) 16362 continue; 16363 16364 if (strcmp(desc->dtpd_func, "ustack") != 0) 16365 continue; 16366 16367 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 16368 ep, help)) != 0) { 16369 /* 16370 * Adding this helper action failed -- we are now going 16371 * to rip out the entire generation and return failure. 16372 */ 16373 (void) dtrace_helper_destroygen(help, 16374 help->dthps_generation); 16375 dtrace_enabling_destroy(enab); 16376 dtrace_dof_destroy(dof); 16377 return (-1); 16378 } 16379 16380 nhelpers++; 16381 } 16382 16383 if (nhelpers < enab->dten_ndesc) 16384 dtrace_dof_error(dof, "unmatched helpers"); 16385 16386 gen = help->dthps_generation++; 16387 dtrace_enabling_destroy(enab); 16388 16389 if (nprovs > 0) { 16390 /* 16391 * Now that this is in-kernel, we change the sense of the 16392 * members: dofhp_dof denotes the in-kernel copy of the DOF 16393 * and dofhp_addr denotes the address at user-level. 16394 */ 16395 dhp->dofhp_addr = dhp->dofhp_dof; 16396 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 16397 16398 if (dtrace_helper_provider_add(dhp, help, gen) == 0) { 16399 mutex_exit(&dtrace_lock); 16400 dtrace_helper_provider_register(p, help, dhp); 16401 mutex_enter(&dtrace_lock); 16402 16403 destroy = 0; 16404 } 16405 } 16406 16407 if (destroy) 16408 dtrace_dof_destroy(dof); 16409 16410 return (gen); 16411 } 16412 16413 static dtrace_helpers_t * 16414 dtrace_helpers_create(proc_t *p) 16415 { 16416 dtrace_helpers_t *help; 16417 16418 ASSERT(MUTEX_HELD(&dtrace_lock)); 16419 ASSERT(p->p_dtrace_helpers == NULL); 16420 16421 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 16422 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 16423 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 16424 16425 p->p_dtrace_helpers = help; 16426 dtrace_helpers++; 16427 16428 return (help); 16429 } 16430 16431 #ifdef illumos 16432 static 16433 #endif 16434 void 16435 dtrace_helpers_destroy(proc_t *p) 16436 { 16437 dtrace_helpers_t *help; 16438 dtrace_vstate_t *vstate; 16439 #ifdef illumos 16440 proc_t *p = curproc; 16441 #endif 16442 int i; 16443 16444 mutex_enter(&dtrace_lock); 16445 16446 ASSERT(p->p_dtrace_helpers != NULL); 16447 ASSERT(dtrace_helpers > 0); 16448 16449 help = p->p_dtrace_helpers; 16450 vstate = &help->dthps_vstate; 16451 16452 /* 16453 * We're now going to lose the help from this process. 16454 */ 16455 p->p_dtrace_helpers = NULL; 16456 dtrace_sync(); 16457 16458 /* 16459 * Destory the helper actions. 16460 */ 16461 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16462 dtrace_helper_action_t *h, *next; 16463 16464 for (h = help->dthps_actions[i]; h != NULL; h = next) { 16465 next = h->dtha_next; 16466 dtrace_helper_action_destroy(h, vstate); 16467 h = next; 16468 } 16469 } 16470 16471 mutex_exit(&dtrace_lock); 16472 16473 /* 16474 * Destroy the helper providers. 16475 */ 16476 if (help->dthps_maxprovs > 0) { 16477 mutex_enter(&dtrace_meta_lock); 16478 if (dtrace_meta_pid != NULL) { 16479 ASSERT(dtrace_deferred_pid == NULL); 16480 16481 for (i = 0; i < help->dthps_nprovs; i++) { 16482 dtrace_helper_provider_remove( 16483 &help->dthps_provs[i]->dthp_prov, p->p_pid); 16484 } 16485 } else { 16486 mutex_enter(&dtrace_lock); 16487 ASSERT(help->dthps_deferred == 0 || 16488 help->dthps_next != NULL || 16489 help->dthps_prev != NULL || 16490 help == dtrace_deferred_pid); 16491 16492 /* 16493 * Remove the helper from the deferred list. 16494 */ 16495 if (help->dthps_next != NULL) 16496 help->dthps_next->dthps_prev = help->dthps_prev; 16497 if (help->dthps_prev != NULL) 16498 help->dthps_prev->dthps_next = help->dthps_next; 16499 if (dtrace_deferred_pid == help) { 16500 dtrace_deferred_pid = help->dthps_next; 16501 ASSERT(help->dthps_prev == NULL); 16502 } 16503 16504 mutex_exit(&dtrace_lock); 16505 } 16506 16507 mutex_exit(&dtrace_meta_lock); 16508 16509 for (i = 0; i < help->dthps_nprovs; i++) { 16510 dtrace_helper_provider_destroy(help->dthps_provs[i]); 16511 } 16512 16513 kmem_free(help->dthps_provs, help->dthps_maxprovs * 16514 sizeof (dtrace_helper_provider_t *)); 16515 } 16516 16517 mutex_enter(&dtrace_lock); 16518 16519 dtrace_vstate_fini(&help->dthps_vstate); 16520 kmem_free(help->dthps_actions, 16521 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 16522 kmem_free(help, sizeof (dtrace_helpers_t)); 16523 16524 --dtrace_helpers; 16525 mutex_exit(&dtrace_lock); 16526 } 16527 16528 #ifdef illumos 16529 static 16530 #endif 16531 void 16532 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 16533 { 16534 dtrace_helpers_t *help, *newhelp; 16535 dtrace_helper_action_t *helper, *new, *last; 16536 dtrace_difo_t *dp; 16537 dtrace_vstate_t *vstate; 16538 int i, j, sz, hasprovs = 0; 16539 16540 mutex_enter(&dtrace_lock); 16541 ASSERT(from->p_dtrace_helpers != NULL); 16542 ASSERT(dtrace_helpers > 0); 16543 16544 help = from->p_dtrace_helpers; 16545 newhelp = dtrace_helpers_create(to); 16546 ASSERT(to->p_dtrace_helpers != NULL); 16547 16548 newhelp->dthps_generation = help->dthps_generation; 16549 vstate = &newhelp->dthps_vstate; 16550 16551 /* 16552 * Duplicate the helper actions. 16553 */ 16554 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16555 if ((helper = help->dthps_actions[i]) == NULL) 16556 continue; 16557 16558 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 16559 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 16560 KM_SLEEP); 16561 new->dtha_generation = helper->dtha_generation; 16562 16563 if ((dp = helper->dtha_predicate) != NULL) { 16564 dp = dtrace_difo_duplicate(dp, vstate); 16565 new->dtha_predicate = dp; 16566 } 16567 16568 new->dtha_nactions = helper->dtha_nactions; 16569 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 16570 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 16571 16572 for (j = 0; j < new->dtha_nactions; j++) { 16573 dtrace_difo_t *dp = helper->dtha_actions[j]; 16574 16575 ASSERT(dp != NULL); 16576 dp = dtrace_difo_duplicate(dp, vstate); 16577 new->dtha_actions[j] = dp; 16578 } 16579 16580 if (last != NULL) { 16581 last->dtha_next = new; 16582 } else { 16583 newhelp->dthps_actions[i] = new; 16584 } 16585 16586 last = new; 16587 } 16588 } 16589 16590 /* 16591 * Duplicate the helper providers and register them with the 16592 * DTrace framework. 16593 */ 16594 if (help->dthps_nprovs > 0) { 16595 newhelp->dthps_nprovs = help->dthps_nprovs; 16596 newhelp->dthps_maxprovs = help->dthps_nprovs; 16597 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 16598 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16599 for (i = 0; i < newhelp->dthps_nprovs; i++) { 16600 newhelp->dthps_provs[i] = help->dthps_provs[i]; 16601 newhelp->dthps_provs[i]->dthp_ref++; 16602 } 16603 16604 hasprovs = 1; 16605 } 16606 16607 mutex_exit(&dtrace_lock); 16608 16609 if (hasprovs) 16610 dtrace_helper_provider_register(to, newhelp, NULL); 16611 } 16612 16613 /* 16614 * DTrace Hook Functions 16615 */ 16616 static void 16617 dtrace_module_loaded(modctl_t *ctl) 16618 { 16619 dtrace_provider_t *prv; 16620 16621 mutex_enter(&dtrace_provider_lock); 16622 #ifdef illumos 16623 mutex_enter(&mod_lock); 16624 #endif 16625 16626 #ifdef illumos 16627 ASSERT(ctl->mod_busy); 16628 #endif 16629 16630 /* 16631 * We're going to call each providers per-module provide operation 16632 * specifying only this module. 16633 */ 16634 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 16635 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 16636 16637 #ifdef illumos 16638 mutex_exit(&mod_lock); 16639 #endif 16640 mutex_exit(&dtrace_provider_lock); 16641 16642 /* 16643 * If we have any retained enablings, we need to match against them. 16644 * Enabling probes requires that cpu_lock be held, and we cannot hold 16645 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 16646 * module. (In particular, this happens when loading scheduling 16647 * classes.) So if we have any retained enablings, we need to dispatch 16648 * our task queue to do the match for us. 16649 */ 16650 mutex_enter(&dtrace_lock); 16651 16652 if (dtrace_retained == NULL) { 16653 mutex_exit(&dtrace_lock); 16654 return; 16655 } 16656 16657 (void) taskq_dispatch(dtrace_taskq, 16658 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 16659 16660 mutex_exit(&dtrace_lock); 16661 16662 /* 16663 * And now, for a little heuristic sleaze: in general, we want to 16664 * match modules as soon as they load. However, we cannot guarantee 16665 * this, because it would lead us to the lock ordering violation 16666 * outlined above. The common case, of course, is that cpu_lock is 16667 * _not_ held -- so we delay here for a clock tick, hoping that that's 16668 * long enough for the task queue to do its work. If it's not, it's 16669 * not a serious problem -- it just means that the module that we 16670 * just loaded may not be immediately instrumentable. 16671 */ 16672 delay(1); 16673 } 16674 16675 static void 16676 #ifdef illumos 16677 dtrace_module_unloaded(modctl_t *ctl) 16678 #else 16679 dtrace_module_unloaded(modctl_t *ctl, int *error) 16680 #endif 16681 { 16682 dtrace_probe_t template, *probe, *first, *next; 16683 dtrace_provider_t *prov; 16684 #ifndef illumos 16685 char modname[DTRACE_MODNAMELEN]; 16686 size_t len; 16687 #endif 16688 16689 #ifdef illumos 16690 template.dtpr_mod = ctl->mod_modname; 16691 #else 16692 /* Handle the fact that ctl->filename may end in ".ko". */ 16693 strlcpy(modname, ctl->filename, sizeof(modname)); 16694 len = strlen(ctl->filename); 16695 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0) 16696 modname[len - 3] = '\0'; 16697 template.dtpr_mod = modname; 16698 #endif 16699 16700 mutex_enter(&dtrace_provider_lock); 16701 #ifdef illumos 16702 mutex_enter(&mod_lock); 16703 #endif 16704 mutex_enter(&dtrace_lock); 16705 16706 #ifndef illumos 16707 if (ctl->nenabled > 0) { 16708 /* Don't allow unloads if a probe is enabled. */ 16709 mutex_exit(&dtrace_provider_lock); 16710 mutex_exit(&dtrace_lock); 16711 *error = -1; 16712 printf( 16713 "kldunload: attempt to unload module that has DTrace probes enabled\n"); 16714 return; 16715 } 16716 #endif 16717 16718 if (dtrace_bymod == NULL) { 16719 /* 16720 * The DTrace module is loaded (obviously) but not attached; 16721 * we don't have any work to do. 16722 */ 16723 mutex_exit(&dtrace_provider_lock); 16724 #ifdef illumos 16725 mutex_exit(&mod_lock); 16726 #endif 16727 mutex_exit(&dtrace_lock); 16728 return; 16729 } 16730 16731 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 16732 probe != NULL; probe = probe->dtpr_nextmod) { 16733 if (probe->dtpr_ecb != NULL) { 16734 mutex_exit(&dtrace_provider_lock); 16735 #ifdef illumos 16736 mutex_exit(&mod_lock); 16737 #endif 16738 mutex_exit(&dtrace_lock); 16739 16740 /* 16741 * This shouldn't _actually_ be possible -- we're 16742 * unloading a module that has an enabled probe in it. 16743 * (It's normally up to the provider to make sure that 16744 * this can't happen.) However, because dtps_enable() 16745 * doesn't have a failure mode, there can be an 16746 * enable/unload race. Upshot: we don't want to 16747 * assert, but we're not going to disable the 16748 * probe, either. 16749 */ 16750 if (dtrace_err_verbose) { 16751 #ifdef illumos 16752 cmn_err(CE_WARN, "unloaded module '%s' had " 16753 "enabled probes", ctl->mod_modname); 16754 #else 16755 cmn_err(CE_WARN, "unloaded module '%s' had " 16756 "enabled probes", modname); 16757 #endif 16758 } 16759 16760 return; 16761 } 16762 } 16763 16764 probe = first; 16765 16766 for (first = NULL; probe != NULL; probe = next) { 16767 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 16768 16769 dtrace_probes[probe->dtpr_id - 1] = NULL; 16770 16771 next = probe->dtpr_nextmod; 16772 dtrace_hash_remove(dtrace_bymod, probe); 16773 dtrace_hash_remove(dtrace_byfunc, probe); 16774 dtrace_hash_remove(dtrace_byname, probe); 16775 16776 if (first == NULL) { 16777 first = probe; 16778 probe->dtpr_nextmod = NULL; 16779 } else { 16780 probe->dtpr_nextmod = first; 16781 first = probe; 16782 } 16783 } 16784 16785 /* 16786 * We've removed all of the module's probes from the hash chains and 16787 * from the probe array. Now issue a dtrace_sync() to be sure that 16788 * everyone has cleared out from any probe array processing. 16789 */ 16790 dtrace_sync(); 16791 16792 for (probe = first; probe != NULL; probe = first) { 16793 first = probe->dtpr_nextmod; 16794 prov = probe->dtpr_provider; 16795 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 16796 probe->dtpr_arg); 16797 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 16798 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 16799 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 16800 #ifdef illumos 16801 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 16802 #else 16803 free_unr(dtrace_arena, probe->dtpr_id); 16804 #endif 16805 kmem_free(probe, sizeof (dtrace_probe_t)); 16806 } 16807 16808 mutex_exit(&dtrace_lock); 16809 #ifdef illumos 16810 mutex_exit(&mod_lock); 16811 #endif 16812 mutex_exit(&dtrace_provider_lock); 16813 } 16814 16815 #ifndef illumos 16816 static void 16817 dtrace_kld_load(void *arg __unused, linker_file_t lf) 16818 { 16819 16820 dtrace_module_loaded(lf); 16821 } 16822 16823 static void 16824 dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error) 16825 { 16826 16827 if (*error != 0) 16828 /* We already have an error, so don't do anything. */ 16829 return; 16830 dtrace_module_unloaded(lf, error); 16831 } 16832 #endif 16833 16834 #ifdef illumos 16835 static void 16836 dtrace_suspend(void) 16837 { 16838 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 16839 } 16840 16841 static void 16842 dtrace_resume(void) 16843 { 16844 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 16845 } 16846 #endif 16847 16848 static int 16849 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 16850 { 16851 ASSERT(MUTEX_HELD(&cpu_lock)); 16852 mutex_enter(&dtrace_lock); 16853 16854 switch (what) { 16855 case CPU_CONFIG: { 16856 dtrace_state_t *state; 16857 dtrace_optval_t *opt, rs, c; 16858 16859 /* 16860 * For now, we only allocate a new buffer for anonymous state. 16861 */ 16862 if ((state = dtrace_anon.dta_state) == NULL) 16863 break; 16864 16865 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 16866 break; 16867 16868 opt = state->dts_options; 16869 c = opt[DTRACEOPT_CPU]; 16870 16871 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 16872 break; 16873 16874 /* 16875 * Regardless of what the actual policy is, we're going to 16876 * temporarily set our resize policy to be manual. We're 16877 * also going to temporarily set our CPU option to denote 16878 * the newly configured CPU. 16879 */ 16880 rs = opt[DTRACEOPT_BUFRESIZE]; 16881 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 16882 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 16883 16884 (void) dtrace_state_buffers(state); 16885 16886 opt[DTRACEOPT_BUFRESIZE] = rs; 16887 opt[DTRACEOPT_CPU] = c; 16888 16889 break; 16890 } 16891 16892 case CPU_UNCONFIG: 16893 /* 16894 * We don't free the buffer in the CPU_UNCONFIG case. (The 16895 * buffer will be freed when the consumer exits.) 16896 */ 16897 break; 16898 16899 default: 16900 break; 16901 } 16902 16903 mutex_exit(&dtrace_lock); 16904 return (0); 16905 } 16906 16907 #ifdef illumos 16908 static void 16909 dtrace_cpu_setup_initial(processorid_t cpu) 16910 { 16911 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 16912 } 16913 #endif 16914 16915 static void 16916 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 16917 { 16918 if (dtrace_toxranges >= dtrace_toxranges_max) { 16919 int osize, nsize; 16920 dtrace_toxrange_t *range; 16921 16922 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16923 16924 if (osize == 0) { 16925 ASSERT(dtrace_toxrange == NULL); 16926 ASSERT(dtrace_toxranges_max == 0); 16927 dtrace_toxranges_max = 1; 16928 } else { 16929 dtrace_toxranges_max <<= 1; 16930 } 16931 16932 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16933 range = kmem_zalloc(nsize, KM_SLEEP); 16934 16935 if (dtrace_toxrange != NULL) { 16936 ASSERT(osize != 0); 16937 bcopy(dtrace_toxrange, range, osize); 16938 kmem_free(dtrace_toxrange, osize); 16939 } 16940 16941 dtrace_toxrange = range; 16942 } 16943 16944 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 16945 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 16946 16947 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 16948 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 16949 dtrace_toxranges++; 16950 } 16951 16952 static void 16953 dtrace_getf_barrier() 16954 { 16955 #ifdef illumos 16956 /* 16957 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 16958 * that contain calls to getf(), this routine will be called on every 16959 * closef() before either the underlying vnode is released or the 16960 * file_t itself is freed. By the time we are here, it is essential 16961 * that the file_t can no longer be accessed from a call to getf() 16962 * in probe context -- that assures that a dtrace_sync() can be used 16963 * to clear out any enablings referring to the old structures. 16964 */ 16965 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 16966 kcred->cr_zone->zone_dtrace_getf != 0) 16967 dtrace_sync(); 16968 #endif 16969 } 16970 16971 /* 16972 * DTrace Driver Cookbook Functions 16973 */ 16974 #ifdef illumos 16975 /*ARGSUSED*/ 16976 static int 16977 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 16978 { 16979 dtrace_provider_id_t id; 16980 dtrace_state_t *state = NULL; 16981 dtrace_enabling_t *enab; 16982 16983 mutex_enter(&cpu_lock); 16984 mutex_enter(&dtrace_provider_lock); 16985 mutex_enter(&dtrace_lock); 16986 16987 if (ddi_soft_state_init(&dtrace_softstate, 16988 sizeof (dtrace_state_t), 0) != 0) { 16989 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 16990 mutex_exit(&cpu_lock); 16991 mutex_exit(&dtrace_provider_lock); 16992 mutex_exit(&dtrace_lock); 16993 return (DDI_FAILURE); 16994 } 16995 16996 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 16997 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 16998 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 16999 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 17000 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 17001 ddi_remove_minor_node(devi, NULL); 17002 ddi_soft_state_fini(&dtrace_softstate); 17003 mutex_exit(&cpu_lock); 17004 mutex_exit(&dtrace_provider_lock); 17005 mutex_exit(&dtrace_lock); 17006 return (DDI_FAILURE); 17007 } 17008 17009 ddi_report_dev(devi); 17010 dtrace_devi = devi; 17011 17012 dtrace_modload = dtrace_module_loaded; 17013 dtrace_modunload = dtrace_module_unloaded; 17014 dtrace_cpu_init = dtrace_cpu_setup_initial; 17015 dtrace_helpers_cleanup = dtrace_helpers_destroy; 17016 dtrace_helpers_fork = dtrace_helpers_duplicate; 17017 dtrace_cpustart_init = dtrace_suspend; 17018 dtrace_cpustart_fini = dtrace_resume; 17019 dtrace_debugger_init = dtrace_suspend; 17020 dtrace_debugger_fini = dtrace_resume; 17021 17022 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 17023 17024 ASSERT(MUTEX_HELD(&cpu_lock)); 17025 17026 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 17027 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 17028 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 17029 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 17030 VM_SLEEP | VMC_IDENTIFIER); 17031 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 17032 1, INT_MAX, 0); 17033 17034 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 17035 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 17036 NULL, NULL, NULL, NULL, NULL, 0); 17037 17038 ASSERT(MUTEX_HELD(&cpu_lock)); 17039 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 17040 offsetof(dtrace_probe_t, dtpr_nextmod), 17041 offsetof(dtrace_probe_t, dtpr_prevmod)); 17042 17043 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 17044 offsetof(dtrace_probe_t, dtpr_nextfunc), 17045 offsetof(dtrace_probe_t, dtpr_prevfunc)); 17046 17047 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 17048 offsetof(dtrace_probe_t, dtpr_nextname), 17049 offsetof(dtrace_probe_t, dtpr_prevname)); 17050 17051 if (dtrace_retain_max < 1) { 17052 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 17053 "setting to 1", dtrace_retain_max); 17054 dtrace_retain_max = 1; 17055 } 17056 17057 /* 17058 * Now discover our toxic ranges. 17059 */ 17060 dtrace_toxic_ranges(dtrace_toxrange_add); 17061 17062 /* 17063 * Before we register ourselves as a provider to our own framework, 17064 * we would like to assert that dtrace_provider is NULL -- but that's 17065 * not true if we were loaded as a dependency of a DTrace provider. 17066 * Once we've registered, we can assert that dtrace_provider is our 17067 * pseudo provider. 17068 */ 17069 (void) dtrace_register("dtrace", &dtrace_provider_attr, 17070 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 17071 17072 ASSERT(dtrace_provider != NULL); 17073 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 17074 17075 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 17076 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 17077 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 17078 dtrace_provider, NULL, NULL, "END", 0, NULL); 17079 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 17080 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 17081 17082 dtrace_anon_property(); 17083 mutex_exit(&cpu_lock); 17084 17085 /* 17086 * If there are already providers, we must ask them to provide their 17087 * probes, and then match any anonymous enabling against them. Note 17088 * that there should be no other retained enablings at this time: 17089 * the only retained enablings at this time should be the anonymous 17090 * enabling. 17091 */ 17092 if (dtrace_anon.dta_enabling != NULL) { 17093 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 17094 17095 dtrace_enabling_provide(NULL); 17096 state = dtrace_anon.dta_state; 17097 17098 /* 17099 * We couldn't hold cpu_lock across the above call to 17100 * dtrace_enabling_provide(), but we must hold it to actually 17101 * enable the probes. We have to drop all of our locks, pick 17102 * up cpu_lock, and regain our locks before matching the 17103 * retained anonymous enabling. 17104 */ 17105 mutex_exit(&dtrace_lock); 17106 mutex_exit(&dtrace_provider_lock); 17107 17108 mutex_enter(&cpu_lock); 17109 mutex_enter(&dtrace_provider_lock); 17110 mutex_enter(&dtrace_lock); 17111 17112 if ((enab = dtrace_anon.dta_enabling) != NULL) 17113 (void) dtrace_enabling_match(enab, NULL); 17114 17115 mutex_exit(&cpu_lock); 17116 } 17117 17118 mutex_exit(&dtrace_lock); 17119 mutex_exit(&dtrace_provider_lock); 17120 17121 if (state != NULL) { 17122 /* 17123 * If we created any anonymous state, set it going now. 17124 */ 17125 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 17126 } 17127 17128 return (DDI_SUCCESS); 17129 } 17130 #endif /* illumos */ 17131 17132 #ifndef illumos 17133 static void dtrace_dtr(void *); 17134 #endif 17135 17136 /*ARGSUSED*/ 17137 static int 17138 #ifdef illumos 17139 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 17140 #else 17141 dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 17142 #endif 17143 { 17144 dtrace_state_t *state; 17145 uint32_t priv; 17146 uid_t uid; 17147 zoneid_t zoneid; 17148 17149 #ifdef illumos 17150 if (getminor(*devp) == DTRACEMNRN_HELPER) 17151 return (0); 17152 17153 /* 17154 * If this wasn't an open with the "helper" minor, then it must be 17155 * the "dtrace" minor. 17156 */ 17157 if (getminor(*devp) == DTRACEMNRN_DTRACE) 17158 return (ENXIO); 17159 #else 17160 cred_t *cred_p = NULL; 17161 cred_p = dev->si_cred; 17162 17163 /* 17164 * If no DTRACE_PRIV_* bits are set in the credential, then the 17165 * caller lacks sufficient permission to do anything with DTrace. 17166 */ 17167 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 17168 if (priv == DTRACE_PRIV_NONE) { 17169 #endif 17170 17171 return (EACCES); 17172 } 17173 17174 /* 17175 * Ask all providers to provide all their probes. 17176 */ 17177 mutex_enter(&dtrace_provider_lock); 17178 dtrace_probe_provide(NULL, NULL); 17179 mutex_exit(&dtrace_provider_lock); 17180 17181 mutex_enter(&cpu_lock); 17182 mutex_enter(&dtrace_lock); 17183 dtrace_opens++; 17184 dtrace_membar_producer(); 17185 17186 #ifdef illumos 17187 /* 17188 * If the kernel debugger is active (that is, if the kernel debugger 17189 * modified text in some way), we won't allow the open. 17190 */ 17191 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 17192 dtrace_opens--; 17193 mutex_exit(&cpu_lock); 17194 mutex_exit(&dtrace_lock); 17195 return (EBUSY); 17196 } 17197 17198 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) { 17199 /* 17200 * If DTrace helper tracing is enabled, we need to allocate the 17201 * trace buffer and initialize the values. 17202 */ 17203 dtrace_helptrace_buffer = 17204 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 17205 dtrace_helptrace_next = 0; 17206 dtrace_helptrace_wrapped = 0; 17207 dtrace_helptrace_enable = 0; 17208 } 17209 17210 state = dtrace_state_create(devp, cred_p); 17211 #else 17212 state = dtrace_state_create(dev, NULL); 17213 devfs_set_cdevpriv(state, dtrace_dtr); 17214 #endif 17215 17216 mutex_exit(&cpu_lock); 17217 17218 if (state == NULL) { 17219 #ifdef illumos 17220 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17221 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17222 #else 17223 --dtrace_opens; 17224 #endif 17225 mutex_exit(&dtrace_lock); 17226 return (EAGAIN); 17227 } 17228 17229 mutex_exit(&dtrace_lock); 17230 17231 return (0); 17232 } 17233 17234 /*ARGSUSED*/ 17235 #ifdef illumos 17236 static int 17237 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 17238 #else 17239 static void 17240 dtrace_dtr(void *data) 17241 #endif 17242 { 17243 #ifdef illumos 17244 minor_t minor = getminor(dev); 17245 dtrace_state_t *state; 17246 #endif 17247 dtrace_helptrace_t *buf = NULL; 17248 17249 #ifdef illumos 17250 if (minor == DTRACEMNRN_HELPER) 17251 return (0); 17252 17253 state = ddi_get_soft_state(dtrace_softstate, minor); 17254 #else 17255 dtrace_state_t *state = data; 17256 #endif 17257 17258 mutex_enter(&cpu_lock); 17259 mutex_enter(&dtrace_lock); 17260 17261 #ifdef illumos 17262 if (state->dts_anon) 17263 #else 17264 if (state != NULL && state->dts_anon) 17265 #endif 17266 { 17267 /* 17268 * There is anonymous state. Destroy that first. 17269 */ 17270 ASSERT(dtrace_anon.dta_state == NULL); 17271 dtrace_state_destroy(state->dts_anon); 17272 } 17273 17274 if (dtrace_helptrace_disable) { 17275 /* 17276 * If we have been told to disable helper tracing, set the 17277 * buffer to NULL before calling into dtrace_state_destroy(); 17278 * we take advantage of its dtrace_sync() to know that no 17279 * CPU is in probe context with enabled helper tracing 17280 * after it returns. 17281 */ 17282 buf = dtrace_helptrace_buffer; 17283 dtrace_helptrace_buffer = NULL; 17284 } 17285 17286 #ifdef illumos 17287 dtrace_state_destroy(state); 17288 #else 17289 if (state != NULL) { 17290 dtrace_state_destroy(state); 17291 kmem_free(state, 0); 17292 } 17293 #endif 17294 ASSERT(dtrace_opens > 0); 17295 17296 #ifdef illumos 17297 /* 17298 * Only relinquish control of the kernel debugger interface when there 17299 * are no consumers and no anonymous enablings. 17300 */ 17301 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17302 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17303 #else 17304 --dtrace_opens; 17305 #endif 17306 17307 if (buf != NULL) { 17308 kmem_free(buf, dtrace_helptrace_bufsize); 17309 dtrace_helptrace_disable = 0; 17310 } 17311 17312 mutex_exit(&dtrace_lock); 17313 mutex_exit(&cpu_lock); 17314 17315 #ifdef illumos 17316 return (0); 17317 #endif 17318 } 17319 17320 #ifdef illumos 17321 /*ARGSUSED*/ 17322 static int 17323 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 17324 { 17325 int rval; 17326 dof_helper_t help, *dhp = NULL; 17327 17328 switch (cmd) { 17329 case DTRACEHIOC_ADDDOF: 17330 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 17331 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 17332 return (EFAULT); 17333 } 17334 17335 dhp = &help; 17336 arg = (intptr_t)help.dofhp_dof; 17337 /*FALLTHROUGH*/ 17338 17339 case DTRACEHIOC_ADD: { 17340 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 17341 17342 if (dof == NULL) 17343 return (rval); 17344 17345 mutex_enter(&dtrace_lock); 17346 17347 /* 17348 * dtrace_helper_slurp() takes responsibility for the dof -- 17349 * it may free it now or it may save it and free it later. 17350 */ 17351 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 17352 *rv = rval; 17353 rval = 0; 17354 } else { 17355 rval = EINVAL; 17356 } 17357 17358 mutex_exit(&dtrace_lock); 17359 return (rval); 17360 } 17361 17362 case DTRACEHIOC_REMOVE: { 17363 mutex_enter(&dtrace_lock); 17364 rval = dtrace_helper_destroygen(NULL, arg); 17365 mutex_exit(&dtrace_lock); 17366 17367 return (rval); 17368 } 17369 17370 default: 17371 break; 17372 } 17373 17374 return (ENOTTY); 17375 } 17376 17377 /*ARGSUSED*/ 17378 static int 17379 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 17380 { 17381 minor_t minor = getminor(dev); 17382 dtrace_state_t *state; 17383 int rval; 17384 17385 if (minor == DTRACEMNRN_HELPER) 17386 return (dtrace_ioctl_helper(cmd, arg, rv)); 17387 17388 state = ddi_get_soft_state(dtrace_softstate, minor); 17389 17390 if (state->dts_anon) { 17391 ASSERT(dtrace_anon.dta_state == NULL); 17392 state = state->dts_anon; 17393 } 17394 17395 switch (cmd) { 17396 case DTRACEIOC_PROVIDER: { 17397 dtrace_providerdesc_t pvd; 17398 dtrace_provider_t *pvp; 17399 17400 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 17401 return (EFAULT); 17402 17403 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 17404 mutex_enter(&dtrace_provider_lock); 17405 17406 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 17407 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 17408 break; 17409 } 17410 17411 mutex_exit(&dtrace_provider_lock); 17412 17413 if (pvp == NULL) 17414 return (ESRCH); 17415 17416 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 17417 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 17418 17419 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 17420 return (EFAULT); 17421 17422 return (0); 17423 } 17424 17425 case DTRACEIOC_EPROBE: { 17426 dtrace_eprobedesc_t epdesc; 17427 dtrace_ecb_t *ecb; 17428 dtrace_action_t *act; 17429 void *buf; 17430 size_t size; 17431 uintptr_t dest; 17432 int nrecs; 17433 17434 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 17435 return (EFAULT); 17436 17437 mutex_enter(&dtrace_lock); 17438 17439 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 17440 mutex_exit(&dtrace_lock); 17441 return (EINVAL); 17442 } 17443 17444 if (ecb->dte_probe == NULL) { 17445 mutex_exit(&dtrace_lock); 17446 return (EINVAL); 17447 } 17448 17449 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 17450 epdesc.dtepd_uarg = ecb->dte_uarg; 17451 epdesc.dtepd_size = ecb->dte_size; 17452 17453 nrecs = epdesc.dtepd_nrecs; 17454 epdesc.dtepd_nrecs = 0; 17455 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17456 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17457 continue; 17458 17459 epdesc.dtepd_nrecs++; 17460 } 17461 17462 /* 17463 * Now that we have the size, we need to allocate a temporary 17464 * buffer in which to store the complete description. We need 17465 * the temporary buffer to be able to drop dtrace_lock() 17466 * across the copyout(), below. 17467 */ 17468 size = sizeof (dtrace_eprobedesc_t) + 17469 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 17470 17471 buf = kmem_alloc(size, KM_SLEEP); 17472 dest = (uintptr_t)buf; 17473 17474 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 17475 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 17476 17477 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17478 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17479 continue; 17480 17481 if (nrecs-- == 0) 17482 break; 17483 17484 bcopy(&act->dta_rec, (void *)dest, 17485 sizeof (dtrace_recdesc_t)); 17486 dest += sizeof (dtrace_recdesc_t); 17487 } 17488 17489 mutex_exit(&dtrace_lock); 17490 17491 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17492 kmem_free(buf, size); 17493 return (EFAULT); 17494 } 17495 17496 kmem_free(buf, size); 17497 return (0); 17498 } 17499 17500 case DTRACEIOC_AGGDESC: { 17501 dtrace_aggdesc_t aggdesc; 17502 dtrace_action_t *act; 17503 dtrace_aggregation_t *agg; 17504 int nrecs; 17505 uint32_t offs; 17506 dtrace_recdesc_t *lrec; 17507 void *buf; 17508 size_t size; 17509 uintptr_t dest; 17510 17511 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 17512 return (EFAULT); 17513 17514 mutex_enter(&dtrace_lock); 17515 17516 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 17517 mutex_exit(&dtrace_lock); 17518 return (EINVAL); 17519 } 17520 17521 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 17522 17523 nrecs = aggdesc.dtagd_nrecs; 17524 aggdesc.dtagd_nrecs = 0; 17525 17526 offs = agg->dtag_base; 17527 lrec = &agg->dtag_action.dta_rec; 17528 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 17529 17530 for (act = agg->dtag_first; ; act = act->dta_next) { 17531 ASSERT(act->dta_intuple || 17532 DTRACEACT_ISAGG(act->dta_kind)); 17533 17534 /* 17535 * If this action has a record size of zero, it 17536 * denotes an argument to the aggregating action. 17537 * Because the presence of this record doesn't (or 17538 * shouldn't) affect the way the data is interpreted, 17539 * we don't copy it out to save user-level the 17540 * confusion of dealing with a zero-length record. 17541 */ 17542 if (act->dta_rec.dtrd_size == 0) { 17543 ASSERT(agg->dtag_hasarg); 17544 continue; 17545 } 17546 17547 aggdesc.dtagd_nrecs++; 17548 17549 if (act == &agg->dtag_action) 17550 break; 17551 } 17552 17553 /* 17554 * Now that we have the size, we need to allocate a temporary 17555 * buffer in which to store the complete description. We need 17556 * the temporary buffer to be able to drop dtrace_lock() 17557 * across the copyout(), below. 17558 */ 17559 size = sizeof (dtrace_aggdesc_t) + 17560 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 17561 17562 buf = kmem_alloc(size, KM_SLEEP); 17563 dest = (uintptr_t)buf; 17564 17565 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 17566 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 17567 17568 for (act = agg->dtag_first; ; act = act->dta_next) { 17569 dtrace_recdesc_t rec = act->dta_rec; 17570 17571 /* 17572 * See the comment in the above loop for why we pass 17573 * over zero-length records. 17574 */ 17575 if (rec.dtrd_size == 0) { 17576 ASSERT(agg->dtag_hasarg); 17577 continue; 17578 } 17579 17580 if (nrecs-- == 0) 17581 break; 17582 17583 rec.dtrd_offset -= offs; 17584 bcopy(&rec, (void *)dest, sizeof (rec)); 17585 dest += sizeof (dtrace_recdesc_t); 17586 17587 if (act == &agg->dtag_action) 17588 break; 17589 } 17590 17591 mutex_exit(&dtrace_lock); 17592 17593 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17594 kmem_free(buf, size); 17595 return (EFAULT); 17596 } 17597 17598 kmem_free(buf, size); 17599 return (0); 17600 } 17601 17602 case DTRACEIOC_ENABLE: { 17603 dof_hdr_t *dof; 17604 dtrace_enabling_t *enab = NULL; 17605 dtrace_vstate_t *vstate; 17606 int err = 0; 17607 17608 *rv = 0; 17609 17610 /* 17611 * If a NULL argument has been passed, we take this as our 17612 * cue to reevaluate our enablings. 17613 */ 17614 if (arg == NULL) { 17615 dtrace_enabling_matchall(); 17616 17617 return (0); 17618 } 17619 17620 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 17621 return (rval); 17622 17623 mutex_enter(&cpu_lock); 17624 mutex_enter(&dtrace_lock); 17625 vstate = &state->dts_vstate; 17626 17627 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 17628 mutex_exit(&dtrace_lock); 17629 mutex_exit(&cpu_lock); 17630 dtrace_dof_destroy(dof); 17631 return (EBUSY); 17632 } 17633 17634 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 17635 mutex_exit(&dtrace_lock); 17636 mutex_exit(&cpu_lock); 17637 dtrace_dof_destroy(dof); 17638 return (EINVAL); 17639 } 17640 17641 if ((rval = dtrace_dof_options(dof, state)) != 0) { 17642 dtrace_enabling_destroy(enab); 17643 mutex_exit(&dtrace_lock); 17644 mutex_exit(&cpu_lock); 17645 dtrace_dof_destroy(dof); 17646 return (rval); 17647 } 17648 17649 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 17650 err = dtrace_enabling_retain(enab); 17651 } else { 17652 dtrace_enabling_destroy(enab); 17653 } 17654 17655 mutex_exit(&cpu_lock); 17656 mutex_exit(&dtrace_lock); 17657 dtrace_dof_destroy(dof); 17658 17659 return (err); 17660 } 17661 17662 case DTRACEIOC_REPLICATE: { 17663 dtrace_repldesc_t desc; 17664 dtrace_probedesc_t *match = &desc.dtrpd_match; 17665 dtrace_probedesc_t *create = &desc.dtrpd_create; 17666 int err; 17667 17668 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17669 return (EFAULT); 17670 17671 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17672 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17673 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17674 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17675 17676 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17677 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17678 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17679 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17680 17681 mutex_enter(&dtrace_lock); 17682 err = dtrace_enabling_replicate(state, match, create); 17683 mutex_exit(&dtrace_lock); 17684 17685 return (err); 17686 } 17687 17688 case DTRACEIOC_PROBEMATCH: 17689 case DTRACEIOC_PROBES: { 17690 dtrace_probe_t *probe = NULL; 17691 dtrace_probedesc_t desc; 17692 dtrace_probekey_t pkey; 17693 dtrace_id_t i; 17694 int m = 0; 17695 uint32_t priv; 17696 uid_t uid; 17697 zoneid_t zoneid; 17698 17699 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17700 return (EFAULT); 17701 17702 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17703 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17704 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17705 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17706 17707 /* 17708 * Before we attempt to match this probe, we want to give 17709 * all providers the opportunity to provide it. 17710 */ 17711 if (desc.dtpd_id == DTRACE_IDNONE) { 17712 mutex_enter(&dtrace_provider_lock); 17713 dtrace_probe_provide(&desc, NULL); 17714 mutex_exit(&dtrace_provider_lock); 17715 desc.dtpd_id++; 17716 } 17717 17718 if (cmd == DTRACEIOC_PROBEMATCH) { 17719 dtrace_probekey(&desc, &pkey); 17720 pkey.dtpk_id = DTRACE_IDNONE; 17721 } 17722 17723 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 17724 17725 mutex_enter(&dtrace_lock); 17726 17727 if (cmd == DTRACEIOC_PROBEMATCH) { 17728 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17729 if ((probe = dtrace_probes[i - 1]) != NULL && 17730 (m = dtrace_match_probe(probe, &pkey, 17731 priv, uid, zoneid)) != 0) 17732 break; 17733 } 17734 17735 if (m < 0) { 17736 mutex_exit(&dtrace_lock); 17737 return (EINVAL); 17738 } 17739 17740 } else { 17741 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17742 if ((probe = dtrace_probes[i - 1]) != NULL && 17743 dtrace_match_priv(probe, priv, uid, zoneid)) 17744 break; 17745 } 17746 } 17747 17748 if (probe == NULL) { 17749 mutex_exit(&dtrace_lock); 17750 return (ESRCH); 17751 } 17752 17753 dtrace_probe_description(probe, &desc); 17754 mutex_exit(&dtrace_lock); 17755 17756 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17757 return (EFAULT); 17758 17759 return (0); 17760 } 17761 17762 case DTRACEIOC_PROBEARG: { 17763 dtrace_argdesc_t desc; 17764 dtrace_probe_t *probe; 17765 dtrace_provider_t *prov; 17766 17767 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17768 return (EFAULT); 17769 17770 if (desc.dtargd_id == DTRACE_IDNONE) 17771 return (EINVAL); 17772 17773 if (desc.dtargd_ndx == DTRACE_ARGNONE) 17774 return (EINVAL); 17775 17776 mutex_enter(&dtrace_provider_lock); 17777 mutex_enter(&mod_lock); 17778 mutex_enter(&dtrace_lock); 17779 17780 if (desc.dtargd_id > dtrace_nprobes) { 17781 mutex_exit(&dtrace_lock); 17782 mutex_exit(&mod_lock); 17783 mutex_exit(&dtrace_provider_lock); 17784 return (EINVAL); 17785 } 17786 17787 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 17788 mutex_exit(&dtrace_lock); 17789 mutex_exit(&mod_lock); 17790 mutex_exit(&dtrace_provider_lock); 17791 return (EINVAL); 17792 } 17793 17794 mutex_exit(&dtrace_lock); 17795 17796 prov = probe->dtpr_provider; 17797 17798 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 17799 /* 17800 * There isn't any typed information for this probe. 17801 * Set the argument number to DTRACE_ARGNONE. 17802 */ 17803 desc.dtargd_ndx = DTRACE_ARGNONE; 17804 } else { 17805 desc.dtargd_native[0] = '\0'; 17806 desc.dtargd_xlate[0] = '\0'; 17807 desc.dtargd_mapping = desc.dtargd_ndx; 17808 17809 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 17810 probe->dtpr_id, probe->dtpr_arg, &desc); 17811 } 17812 17813 mutex_exit(&mod_lock); 17814 mutex_exit(&dtrace_provider_lock); 17815 17816 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17817 return (EFAULT); 17818 17819 return (0); 17820 } 17821 17822 case DTRACEIOC_GO: { 17823 processorid_t cpuid; 17824 rval = dtrace_state_go(state, &cpuid); 17825 17826 if (rval != 0) 17827 return (rval); 17828 17829 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17830 return (EFAULT); 17831 17832 return (0); 17833 } 17834 17835 case DTRACEIOC_STOP: { 17836 processorid_t cpuid; 17837 17838 mutex_enter(&dtrace_lock); 17839 rval = dtrace_state_stop(state, &cpuid); 17840 mutex_exit(&dtrace_lock); 17841 17842 if (rval != 0) 17843 return (rval); 17844 17845 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17846 return (EFAULT); 17847 17848 return (0); 17849 } 17850 17851 case DTRACEIOC_DOFGET: { 17852 dof_hdr_t hdr, *dof; 17853 uint64_t len; 17854 17855 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 17856 return (EFAULT); 17857 17858 mutex_enter(&dtrace_lock); 17859 dof = dtrace_dof_create(state); 17860 mutex_exit(&dtrace_lock); 17861 17862 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 17863 rval = copyout(dof, (void *)arg, len); 17864 dtrace_dof_destroy(dof); 17865 17866 return (rval == 0 ? 0 : EFAULT); 17867 } 17868 17869 case DTRACEIOC_AGGSNAP: 17870 case DTRACEIOC_BUFSNAP: { 17871 dtrace_bufdesc_t desc; 17872 caddr_t cached; 17873 dtrace_buffer_t *buf; 17874 17875 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17876 return (EFAULT); 17877 17878 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 17879 return (EINVAL); 17880 17881 mutex_enter(&dtrace_lock); 17882 17883 if (cmd == DTRACEIOC_BUFSNAP) { 17884 buf = &state->dts_buffer[desc.dtbd_cpu]; 17885 } else { 17886 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 17887 } 17888 17889 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 17890 size_t sz = buf->dtb_offset; 17891 17892 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 17893 mutex_exit(&dtrace_lock); 17894 return (EBUSY); 17895 } 17896 17897 /* 17898 * If this buffer has already been consumed, we're 17899 * going to indicate that there's nothing left here 17900 * to consume. 17901 */ 17902 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 17903 mutex_exit(&dtrace_lock); 17904 17905 desc.dtbd_size = 0; 17906 desc.dtbd_drops = 0; 17907 desc.dtbd_errors = 0; 17908 desc.dtbd_oldest = 0; 17909 sz = sizeof (desc); 17910 17911 if (copyout(&desc, (void *)arg, sz) != 0) 17912 return (EFAULT); 17913 17914 return (0); 17915 } 17916 17917 /* 17918 * If this is a ring buffer that has wrapped, we want 17919 * to copy the whole thing out. 17920 */ 17921 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 17922 dtrace_buffer_polish(buf); 17923 sz = buf->dtb_size; 17924 } 17925 17926 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 17927 mutex_exit(&dtrace_lock); 17928 return (EFAULT); 17929 } 17930 17931 desc.dtbd_size = sz; 17932 desc.dtbd_drops = buf->dtb_drops; 17933 desc.dtbd_errors = buf->dtb_errors; 17934 desc.dtbd_oldest = buf->dtb_xamot_offset; 17935 desc.dtbd_timestamp = dtrace_gethrtime(); 17936 17937 mutex_exit(&dtrace_lock); 17938 17939 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17940 return (EFAULT); 17941 17942 buf->dtb_flags |= DTRACEBUF_CONSUMED; 17943 17944 return (0); 17945 } 17946 17947 if (buf->dtb_tomax == NULL) { 17948 ASSERT(buf->dtb_xamot == NULL); 17949 mutex_exit(&dtrace_lock); 17950 return (ENOENT); 17951 } 17952 17953 cached = buf->dtb_tomax; 17954 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 17955 17956 dtrace_xcall(desc.dtbd_cpu, 17957 (dtrace_xcall_t)dtrace_buffer_switch, buf); 17958 17959 state->dts_errors += buf->dtb_xamot_errors; 17960 17961 /* 17962 * If the buffers did not actually switch, then the cross call 17963 * did not take place -- presumably because the given CPU is 17964 * not in the ready set. If this is the case, we'll return 17965 * ENOENT. 17966 */ 17967 if (buf->dtb_tomax == cached) { 17968 ASSERT(buf->dtb_xamot != cached); 17969 mutex_exit(&dtrace_lock); 17970 return (ENOENT); 17971 } 17972 17973 ASSERT(cached == buf->dtb_xamot); 17974 17975 /* 17976 * We have our snapshot; now copy it out. 17977 */ 17978 if (copyout(buf->dtb_xamot, desc.dtbd_data, 17979 buf->dtb_xamot_offset) != 0) { 17980 mutex_exit(&dtrace_lock); 17981 return (EFAULT); 17982 } 17983 17984 desc.dtbd_size = buf->dtb_xamot_offset; 17985 desc.dtbd_drops = buf->dtb_xamot_drops; 17986 desc.dtbd_errors = buf->dtb_xamot_errors; 17987 desc.dtbd_oldest = 0; 17988 desc.dtbd_timestamp = buf->dtb_switched; 17989 17990 mutex_exit(&dtrace_lock); 17991 17992 /* 17993 * Finally, copy out the buffer description. 17994 */ 17995 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17996 return (EFAULT); 17997 17998 return (0); 17999 } 18000 18001 case DTRACEIOC_CONF: { 18002 dtrace_conf_t conf; 18003 18004 bzero(&conf, sizeof (conf)); 18005 conf.dtc_difversion = DIF_VERSION; 18006 conf.dtc_difintregs = DIF_DIR_NREGS; 18007 conf.dtc_diftupregs = DIF_DTR_NREGS; 18008 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 18009 18010 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 18011 return (EFAULT); 18012 18013 return (0); 18014 } 18015 18016 case DTRACEIOC_STATUS: { 18017 dtrace_status_t stat; 18018 dtrace_dstate_t *dstate; 18019 int i, j; 18020 uint64_t nerrs; 18021 18022 /* 18023 * See the comment in dtrace_state_deadman() for the reason 18024 * for setting dts_laststatus to INT64_MAX before setting 18025 * it to the correct value. 18026 */ 18027 state->dts_laststatus = INT64_MAX; 18028 dtrace_membar_producer(); 18029 state->dts_laststatus = dtrace_gethrtime(); 18030 18031 bzero(&stat, sizeof (stat)); 18032 18033 mutex_enter(&dtrace_lock); 18034 18035 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 18036 mutex_exit(&dtrace_lock); 18037 return (ENOENT); 18038 } 18039 18040 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 18041 stat.dtst_exiting = 1; 18042 18043 nerrs = state->dts_errors; 18044 dstate = &state->dts_vstate.dtvs_dynvars; 18045 18046 for (i = 0; i < NCPU; i++) { 18047 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 18048 18049 stat.dtst_dyndrops += dcpu->dtdsc_drops; 18050 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 18051 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 18052 18053 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 18054 stat.dtst_filled++; 18055 18056 nerrs += state->dts_buffer[i].dtb_errors; 18057 18058 for (j = 0; j < state->dts_nspeculations; j++) { 18059 dtrace_speculation_t *spec; 18060 dtrace_buffer_t *buf; 18061 18062 spec = &state->dts_speculations[j]; 18063 buf = &spec->dtsp_buffer[i]; 18064 stat.dtst_specdrops += buf->dtb_xamot_drops; 18065 } 18066 } 18067 18068 stat.dtst_specdrops_busy = state->dts_speculations_busy; 18069 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 18070 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 18071 stat.dtst_dblerrors = state->dts_dblerrors; 18072 stat.dtst_killed = 18073 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 18074 stat.dtst_errors = nerrs; 18075 18076 mutex_exit(&dtrace_lock); 18077 18078 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 18079 return (EFAULT); 18080 18081 return (0); 18082 } 18083 18084 case DTRACEIOC_FORMAT: { 18085 dtrace_fmtdesc_t fmt; 18086 char *str; 18087 int len; 18088 18089 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 18090 return (EFAULT); 18091 18092 mutex_enter(&dtrace_lock); 18093 18094 if (fmt.dtfd_format == 0 || 18095 fmt.dtfd_format > state->dts_nformats) { 18096 mutex_exit(&dtrace_lock); 18097 return (EINVAL); 18098 } 18099 18100 /* 18101 * Format strings are allocated contiguously and they are 18102 * never freed; if a format index is less than the number 18103 * of formats, we can assert that the format map is non-NULL 18104 * and that the format for the specified index is non-NULL. 18105 */ 18106 ASSERT(state->dts_formats != NULL); 18107 str = state->dts_formats[fmt.dtfd_format - 1]; 18108 ASSERT(str != NULL); 18109 18110 len = strlen(str) + 1; 18111 18112 if (len > fmt.dtfd_length) { 18113 fmt.dtfd_length = len; 18114 18115 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 18116 mutex_exit(&dtrace_lock); 18117 return (EINVAL); 18118 } 18119 } else { 18120 if (copyout(str, fmt.dtfd_string, len) != 0) { 18121 mutex_exit(&dtrace_lock); 18122 return (EINVAL); 18123 } 18124 } 18125 18126 mutex_exit(&dtrace_lock); 18127 return (0); 18128 } 18129 18130 default: 18131 break; 18132 } 18133 18134 return (ENOTTY); 18135 } 18136 18137 /*ARGSUSED*/ 18138 static int 18139 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 18140 { 18141 dtrace_state_t *state; 18142 18143 switch (cmd) { 18144 case DDI_DETACH: 18145 break; 18146 18147 case DDI_SUSPEND: 18148 return (DDI_SUCCESS); 18149 18150 default: 18151 return (DDI_FAILURE); 18152 } 18153 18154 mutex_enter(&cpu_lock); 18155 mutex_enter(&dtrace_provider_lock); 18156 mutex_enter(&dtrace_lock); 18157 18158 ASSERT(dtrace_opens == 0); 18159 18160 if (dtrace_helpers > 0) { 18161 mutex_exit(&dtrace_provider_lock); 18162 mutex_exit(&dtrace_lock); 18163 mutex_exit(&cpu_lock); 18164 return (DDI_FAILURE); 18165 } 18166 18167 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 18168 mutex_exit(&dtrace_provider_lock); 18169 mutex_exit(&dtrace_lock); 18170 mutex_exit(&cpu_lock); 18171 return (DDI_FAILURE); 18172 } 18173 18174 dtrace_provider = NULL; 18175 18176 if ((state = dtrace_anon_grab()) != NULL) { 18177 /* 18178 * If there were ECBs on this state, the provider should 18179 * have not been allowed to detach; assert that there is 18180 * none. 18181 */ 18182 ASSERT(state->dts_necbs == 0); 18183 dtrace_state_destroy(state); 18184 18185 /* 18186 * If we're being detached with anonymous state, we need to 18187 * indicate to the kernel debugger that DTrace is now inactive. 18188 */ 18189 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 18190 } 18191 18192 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 18193 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 18194 dtrace_cpu_init = NULL; 18195 dtrace_helpers_cleanup = NULL; 18196 dtrace_helpers_fork = NULL; 18197 dtrace_cpustart_init = NULL; 18198 dtrace_cpustart_fini = NULL; 18199 dtrace_debugger_init = NULL; 18200 dtrace_debugger_fini = NULL; 18201 dtrace_modload = NULL; 18202 dtrace_modunload = NULL; 18203 18204 ASSERT(dtrace_getf == 0); 18205 ASSERT(dtrace_closef == NULL); 18206 18207 mutex_exit(&cpu_lock); 18208 18209 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 18210 dtrace_probes = NULL; 18211 dtrace_nprobes = 0; 18212 18213 dtrace_hash_destroy(dtrace_bymod); 18214 dtrace_hash_destroy(dtrace_byfunc); 18215 dtrace_hash_destroy(dtrace_byname); 18216 dtrace_bymod = NULL; 18217 dtrace_byfunc = NULL; 18218 dtrace_byname = NULL; 18219 18220 kmem_cache_destroy(dtrace_state_cache); 18221 vmem_destroy(dtrace_minor); 18222 vmem_destroy(dtrace_arena); 18223 18224 if (dtrace_toxrange != NULL) { 18225 kmem_free(dtrace_toxrange, 18226 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 18227 dtrace_toxrange = NULL; 18228 dtrace_toxranges = 0; 18229 dtrace_toxranges_max = 0; 18230 } 18231 18232 ddi_remove_minor_node(dtrace_devi, NULL); 18233 dtrace_devi = NULL; 18234 18235 ddi_soft_state_fini(&dtrace_softstate); 18236 18237 ASSERT(dtrace_vtime_references == 0); 18238 ASSERT(dtrace_opens == 0); 18239 ASSERT(dtrace_retained == NULL); 18240 18241 mutex_exit(&dtrace_lock); 18242 mutex_exit(&dtrace_provider_lock); 18243 18244 /* 18245 * We don't destroy the task queue until after we have dropped our 18246 * locks (taskq_destroy() may block on running tasks). To prevent 18247 * attempting to do work after we have effectively detached but before 18248 * the task queue has been destroyed, all tasks dispatched via the 18249 * task queue must check that DTrace is still attached before 18250 * performing any operation. 18251 */ 18252 taskq_destroy(dtrace_taskq); 18253 dtrace_taskq = NULL; 18254 18255 return (DDI_SUCCESS); 18256 } 18257 #endif 18258 18259 #ifdef illumos 18260 /*ARGSUSED*/ 18261 static int 18262 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 18263 { 18264 int error; 18265 18266 switch (infocmd) { 18267 case DDI_INFO_DEVT2DEVINFO: 18268 *result = (void *)dtrace_devi; 18269 error = DDI_SUCCESS; 18270 break; 18271 case DDI_INFO_DEVT2INSTANCE: 18272 *result = (void *)0; 18273 error = DDI_SUCCESS; 18274 break; 18275 default: 18276 error = DDI_FAILURE; 18277 } 18278 return (error); 18279 } 18280 #endif 18281 18282 #ifdef illumos 18283 static struct cb_ops dtrace_cb_ops = { 18284 dtrace_open, /* open */ 18285 dtrace_close, /* close */ 18286 nulldev, /* strategy */ 18287 nulldev, /* print */ 18288 nodev, /* dump */ 18289 nodev, /* read */ 18290 nodev, /* write */ 18291 dtrace_ioctl, /* ioctl */ 18292 nodev, /* devmap */ 18293 nodev, /* mmap */ 18294 nodev, /* segmap */ 18295 nochpoll, /* poll */ 18296 ddi_prop_op, /* cb_prop_op */ 18297 0, /* streamtab */ 18298 D_NEW | D_MP /* Driver compatibility flag */ 18299 }; 18300 18301 static struct dev_ops dtrace_ops = { 18302 DEVO_REV, /* devo_rev */ 18303 0, /* refcnt */ 18304 dtrace_info, /* get_dev_info */ 18305 nulldev, /* identify */ 18306 nulldev, /* probe */ 18307 dtrace_attach, /* attach */ 18308 dtrace_detach, /* detach */ 18309 nodev, /* reset */ 18310 &dtrace_cb_ops, /* driver operations */ 18311 NULL, /* bus operations */ 18312 nodev /* dev power */ 18313 }; 18314 18315 static struct modldrv modldrv = { 18316 &mod_driverops, /* module type (this is a pseudo driver) */ 18317 "Dynamic Tracing", /* name of module */ 18318 &dtrace_ops, /* driver ops */ 18319 }; 18320 18321 static struct modlinkage modlinkage = { 18322 MODREV_1, 18323 (void *)&modldrv, 18324 NULL 18325 }; 18326 18327 int 18328 _init(void) 18329 { 18330 return (mod_install(&modlinkage)); 18331 } 18332 18333 int 18334 _info(struct modinfo *modinfop) 18335 { 18336 return (mod_info(&modlinkage, modinfop)); 18337 } 18338 18339 int 18340 _fini(void) 18341 { 18342 return (mod_remove(&modlinkage)); 18343 } 18344 #else 18345 18346 static d_ioctl_t dtrace_ioctl; 18347 static d_ioctl_t dtrace_ioctl_helper; 18348 static void dtrace_load(void *); 18349 static int dtrace_unload(void); 18350 static struct cdev *dtrace_dev; 18351 static struct cdev *helper_dev; 18352 18353 void dtrace_invop_init(void); 18354 void dtrace_invop_uninit(void); 18355 18356 static struct cdevsw dtrace_cdevsw = { 18357 .d_version = D_VERSION, 18358 .d_ioctl = dtrace_ioctl, 18359 .d_open = dtrace_open, 18360 .d_name = "dtrace", 18361 }; 18362 18363 static struct cdevsw helper_cdevsw = { 18364 .d_version = D_VERSION, 18365 .d_ioctl = dtrace_ioctl_helper, 18366 .d_name = "helper", 18367 }; 18368 18369 #include <dtrace_anon.c> 18370 #include <dtrace_ioctl.c> 18371 #include <dtrace_load.c> 18372 #include <dtrace_modevent.c> 18373 #include <dtrace_sysctl.c> 18374 #include <dtrace_unload.c> 18375 #include <dtrace_vtime.c> 18376 #include <dtrace_hacks.c> 18377 #include <dtrace_isa.c> 18378 18379 SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 18380 SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 18381 SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 18382 18383 DEV_MODULE(dtrace, dtrace_modevent, NULL); 18384 MODULE_VERSION(dtrace, 1); 18385 MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 18386 #endif 18387