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 2008 Sun Microsystems, Inc. All rights reserved. 26 * Copyright (c) 2012 by Delphix. All rights reserved 27 * Use is subject to license terms. 28 */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 /* 33 * DTrace - Dynamic Tracing for Solaris 34 * 35 * This is the implementation of the Solaris Dynamic Tracing framework 36 * (DTrace). The user-visible interface to DTrace is described at length in 37 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 38 * library, the in-kernel DTrace framework, and the DTrace providers are 39 * described in the block comments in the <sys/dtrace.h> header file. The 40 * internal architecture of DTrace is described in the block comments in the 41 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 42 * implementation very much assume mastery of all of these sources; if one has 43 * an unanswered question about the implementation, one should consult them 44 * first. 45 * 46 * The functions here are ordered roughly as follows: 47 * 48 * - Probe context functions 49 * - Probe hashing functions 50 * - Non-probe context utility functions 51 * - Matching functions 52 * - Provider-to-Framework API functions 53 * - Probe management functions 54 * - DIF object functions 55 * - Format functions 56 * - Predicate functions 57 * - ECB functions 58 * - Buffer functions 59 * - Enabling functions 60 * - DOF functions 61 * - Anonymous enabling functions 62 * - Consumer state functions 63 * - Helper functions 64 * - Hook functions 65 * - Driver cookbook functions 66 * 67 * Each group of functions begins with a block comment labelled the "DTrace 68 * [Group] Functions", allowing one to find each block by searching forward 69 * on capital-f functions. 70 */ 71 #include <sys/errno.h> 72 #if !defined(sun) 73 #include <sys/time.h> 74 #endif 75 #include <sys/stat.h> 76 #include <sys/modctl.h> 77 #include <sys/conf.h> 78 #include <sys/systm.h> 79 #if defined(sun) 80 #include <sys/ddi.h> 81 #include <sys/sunddi.h> 82 #endif 83 #include <sys/cpuvar.h> 84 #include <sys/kmem.h> 85 #if defined(sun) 86 #include <sys/strsubr.h> 87 #endif 88 #include <sys/sysmacros.h> 89 #include <sys/dtrace_impl.h> 90 #include <sys/atomic.h> 91 #include <sys/cmn_err.h> 92 #if defined(sun) 93 #include <sys/mutex_impl.h> 94 #include <sys/rwlock_impl.h> 95 #endif 96 #include <sys/ctf_api.h> 97 #if defined(sun) 98 #include <sys/panic.h> 99 #include <sys/priv_impl.h> 100 #endif 101 #include <sys/policy.h> 102 #if defined(sun) 103 #include <sys/cred_impl.h> 104 #include <sys/procfs_isa.h> 105 #endif 106 #include <sys/taskq.h> 107 #if defined(sun) 108 #include <sys/mkdev.h> 109 #include <sys/kdi.h> 110 #endif 111 #include <sys/zone.h> 112 #include <sys/socket.h> 113 #include <netinet/in.h> 114 115 /* FreeBSD includes: */ 116 #if !defined(sun) 117 #include <sys/callout.h> 118 #include <sys/ctype.h> 119 #include <sys/limits.h> 120 #include <sys/kdb.h> 121 #include <sys/kernel.h> 122 #include <sys/malloc.h> 123 #include <sys/sysctl.h> 124 #include <sys/lock.h> 125 #include <sys/mutex.h> 126 #include <sys/rwlock.h> 127 #include <sys/sx.h> 128 #include <sys/dtrace_bsd.h> 129 #include <netinet/in.h> 130 #include "dtrace_cddl.h" 131 #include "dtrace_debug.c" 132 #endif 133 134 /* 135 * DTrace Tunable Variables 136 * 137 * The following variables may be tuned by adding a line to /etc/system that 138 * includes both the name of the DTrace module ("dtrace") and the name of the 139 * variable. For example: 140 * 141 * set dtrace:dtrace_destructive_disallow = 1 142 * 143 * In general, the only variables that one should be tuning this way are those 144 * that affect system-wide DTrace behavior, and for which the default behavior 145 * is undesirable. Most of these variables are tunable on a per-consumer 146 * basis using DTrace options, and need not be tuned on a system-wide basis. 147 * When tuning these variables, avoid pathological values; while some attempt 148 * is made to verify the integrity of these variables, they are not considered 149 * part of the supported interface to DTrace, and they are therefore not 150 * checked comprehensively. Further, these variables should not be tuned 151 * dynamically via "mdb -kw" or other means; they should only be tuned via 152 * /etc/system. 153 */ 154 int dtrace_destructive_disallow = 0; 155 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 156 size_t dtrace_difo_maxsize = (256 * 1024); 157 dtrace_optval_t dtrace_dof_maxsize = (256 * 1024); 158 size_t dtrace_global_maxsize = (16 * 1024); 159 size_t dtrace_actions_max = (16 * 1024); 160 size_t dtrace_retain_max = 1024; 161 dtrace_optval_t dtrace_helper_actions_max = 128; 162 dtrace_optval_t dtrace_helper_providers_max = 32; 163 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 164 size_t dtrace_strsize_default = 256; 165 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 166 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 167 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 168 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 169 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 170 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 171 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 172 dtrace_optval_t dtrace_nspec_default = 1; 173 dtrace_optval_t dtrace_specsize_default = 32 * 1024; 174 dtrace_optval_t dtrace_stackframes_default = 20; 175 dtrace_optval_t dtrace_ustackframes_default = 20; 176 dtrace_optval_t dtrace_jstackframes_default = 50; 177 dtrace_optval_t dtrace_jstackstrsize_default = 512; 178 int dtrace_msgdsize_max = 128; 179 hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 180 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 181 int dtrace_devdepth_max = 32; 182 int dtrace_err_verbose; 183 hrtime_t dtrace_deadman_interval = NANOSEC; 184 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 185 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 186 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 187 188 /* 189 * DTrace External Variables 190 * 191 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 192 * available to DTrace consumers via the backtick (`) syntax. One of these, 193 * dtrace_zero, is made deliberately so: it is provided as a source of 194 * well-known, zero-filled memory. While this variable is not documented, 195 * it is used by some translators as an implementation detail. 196 */ 197 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 198 199 /* 200 * DTrace Internal Variables 201 */ 202 #if defined(sun) 203 static dev_info_t *dtrace_devi; /* device info */ 204 #endif 205 #if defined(sun) 206 static vmem_t *dtrace_arena; /* probe ID arena */ 207 static vmem_t *dtrace_minor; /* minor number arena */ 208 #else 209 static taskq_t *dtrace_taskq; /* task queue */ 210 static struct unrhdr *dtrace_arena; /* Probe ID number. */ 211 #endif 212 static dtrace_probe_t **dtrace_probes; /* array of all probes */ 213 static int dtrace_nprobes; /* number of probes */ 214 static dtrace_provider_t *dtrace_provider; /* provider list */ 215 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 216 static int dtrace_opens; /* number of opens */ 217 static int dtrace_helpers; /* number of helpers */ 218 #if defined(sun) 219 static void *dtrace_softstate; /* softstate pointer */ 220 #endif 221 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 222 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 223 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 224 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 225 static int dtrace_toxranges; /* number of toxic ranges */ 226 static int dtrace_toxranges_max; /* size of toxic range array */ 227 static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 228 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 229 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 230 static kthread_t *dtrace_panicked; /* panicking thread */ 231 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 232 static dtrace_genid_t dtrace_probegen; /* current probe generation */ 233 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 234 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 235 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 236 #if !defined(sun) 237 static struct mtx dtrace_unr_mtx; 238 MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 239 int dtrace_in_probe; /* non-zero if executing a probe */ 240 #if defined(__i386__) || defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 241 uintptr_t dtrace_in_probe_addr; /* Address of invop when already in probe */ 242 #endif 243 #endif 244 245 /* 246 * DTrace Locking 247 * DTrace is protected by three (relatively coarse-grained) locks: 248 * 249 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 250 * including enabling state, probes, ECBs, consumer state, helper state, 251 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 252 * probe context is lock-free -- synchronization is handled via the 253 * dtrace_sync() cross call mechanism. 254 * 255 * (2) dtrace_provider_lock is required when manipulating provider state, or 256 * when provider state must be held constant. 257 * 258 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 259 * when meta provider state must be held constant. 260 * 261 * The lock ordering between these three locks is dtrace_meta_lock before 262 * dtrace_provider_lock before dtrace_lock. (In particular, there are 263 * several places where dtrace_provider_lock is held by the framework as it 264 * calls into the providers -- which then call back into the framework, 265 * grabbing dtrace_lock.) 266 * 267 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 268 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 269 * role as a coarse-grained lock; it is acquired before both of these locks. 270 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 271 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 272 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 273 * acquired _between_ dtrace_provider_lock and dtrace_lock. 274 */ 275 static kmutex_t dtrace_lock; /* probe state lock */ 276 static kmutex_t dtrace_provider_lock; /* provider state lock */ 277 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 278 279 #if !defined(sun) 280 /* XXX FreeBSD hacks. */ 281 static kmutex_t mod_lock; 282 283 #define cr_suid cr_svuid 284 #define cr_sgid cr_svgid 285 #define ipaddr_t in_addr_t 286 #define mod_modname pathname 287 #define vuprintf vprintf 288 #define ttoproc(_a) ((_a)->td_proc) 289 #define crgetzoneid(_a) 0 290 #define NCPU MAXCPU 291 #define SNOCD 0 292 #define CPU_ON_INTR(_a) 0 293 294 #define PRIV_EFFECTIVE (1 << 0) 295 #define PRIV_DTRACE_KERNEL (1 << 1) 296 #define PRIV_DTRACE_PROC (1 << 2) 297 #define PRIV_DTRACE_USER (1 << 3) 298 #define PRIV_PROC_OWNER (1 << 4) 299 #define PRIV_PROC_ZONE (1 << 5) 300 #define PRIV_ALL ~0 301 302 SYSCTL_NODE(_debug, OID_AUTO, dtrace, CTLFLAG_RD, 0, "DTrace Information"); 303 #endif 304 305 #if defined(sun) 306 #define curcpu CPU->cpu_id 307 #endif 308 309 310 /* 311 * DTrace Provider Variables 312 * 313 * These are the variables relating to DTrace as a provider (that is, the 314 * provider of the BEGIN, END, and ERROR probes). 315 */ 316 static dtrace_pattr_t dtrace_provider_attr = { 317 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 318 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 319 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 320 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 321 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 322 }; 323 324 static void 325 dtrace_nullop(void) 326 {} 327 328 static dtrace_pops_t dtrace_provider_ops = { 329 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 330 (void (*)(void *, modctl_t *))dtrace_nullop, 331 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 332 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 333 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 334 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 335 NULL, 336 NULL, 337 NULL, 338 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 339 }; 340 341 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 342 static dtrace_id_t dtrace_probeid_end; /* special END probe */ 343 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 344 345 /* 346 * DTrace Helper Tracing Variables 347 */ 348 uint32_t dtrace_helptrace_next = 0; 349 uint32_t dtrace_helptrace_nlocals; 350 char *dtrace_helptrace_buffer; 351 int dtrace_helptrace_bufsize = 512 * 1024; 352 353 #ifdef DEBUG 354 int dtrace_helptrace_enabled = 1; 355 #else 356 int dtrace_helptrace_enabled = 0; 357 #endif 358 359 /* 360 * DTrace Error Hashing 361 * 362 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 363 * table. This is very useful for checking coverage of tests that are 364 * expected to induce DIF or DOF processing errors, and may be useful for 365 * debugging problems in the DIF code generator or in DOF generation . The 366 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 367 */ 368 #ifdef DEBUG 369 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 370 static const char *dtrace_errlast; 371 static kthread_t *dtrace_errthread; 372 static kmutex_t dtrace_errlock; 373 #endif 374 375 /* 376 * DTrace Macros and Constants 377 * 378 * These are various macros that are useful in various spots in the 379 * implementation, along with a few random constants that have no meaning 380 * outside of the implementation. There is no real structure to this cpp 381 * mishmash -- but is there ever? 382 */ 383 #define DTRACE_HASHSTR(hash, probe) \ 384 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 385 386 #define DTRACE_HASHNEXT(hash, probe) \ 387 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 388 389 #define DTRACE_HASHPREV(hash, probe) \ 390 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 391 392 #define DTRACE_HASHEQ(hash, lhs, rhs) \ 393 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 394 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 395 396 #define DTRACE_AGGHASHSIZE_SLEW 17 397 398 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 399 400 /* 401 * The key for a thread-local variable consists of the lower 61 bits of the 402 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 403 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 404 * equal to a variable identifier. This is necessary (but not sufficient) to 405 * assure that global associative arrays never collide with thread-local 406 * variables. To guarantee that they cannot collide, we must also define the 407 * order for keying dynamic variables. That order is: 408 * 409 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 410 * 411 * Because the variable-key and the tls-key are in orthogonal spaces, there is 412 * no way for a global variable key signature to match a thread-local key 413 * signature. 414 */ 415 #if defined(sun) 416 #define DTRACE_TLS_THRKEY(where) { \ 417 uint_t intr = 0; \ 418 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 419 for (; actv; actv >>= 1) \ 420 intr++; \ 421 ASSERT(intr < (1 << 3)); \ 422 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 423 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 424 } 425 #else 426 #define DTRACE_TLS_THRKEY(where) { \ 427 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 428 uint_t intr = 0; \ 429 uint_t actv = _c->cpu_intr_actv; \ 430 for (; actv; actv >>= 1) \ 431 intr++; \ 432 ASSERT(intr < (1 << 3)); \ 433 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 434 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 435 } 436 #endif 437 438 #define DT_BSWAP_8(x) ((x) & 0xff) 439 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 440 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 441 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 442 443 #define DT_MASK_LO 0x00000000FFFFFFFFULL 444 445 #define DTRACE_STORE(type, tomax, offset, what) \ 446 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 447 448 #ifndef __x86 449 #define DTRACE_ALIGNCHECK(addr, size, flags) \ 450 if (addr & (size - 1)) { \ 451 *flags |= CPU_DTRACE_BADALIGN; \ 452 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 453 return (0); \ 454 } 455 #else 456 #define DTRACE_ALIGNCHECK(addr, size, flags) 457 #endif 458 459 /* 460 * Test whether a range of memory starting at testaddr of size testsz falls 461 * within the range of memory described by addr, sz. We take care to avoid 462 * problems with overflow and underflow of the unsigned quantities, and 463 * disallow all negative sizes. Ranges of size 0 are allowed. 464 */ 465 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 466 ((testaddr) - (baseaddr) < (basesz) && \ 467 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \ 468 (testaddr) + (testsz) >= (testaddr)) 469 470 /* 471 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 472 * alloc_sz on the righthand side of the comparison in order to avoid overflow 473 * or underflow in the comparison with it. This is simpler than the INRANGE 474 * check above, because we know that the dtms_scratch_ptr is valid in the 475 * range. Allocations of size zero are allowed. 476 */ 477 #define DTRACE_INSCRATCH(mstate, alloc_sz) \ 478 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 479 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 480 481 #define DTRACE_LOADFUNC(bits) \ 482 /*CSTYLED*/ \ 483 uint##bits##_t \ 484 dtrace_load##bits(uintptr_t addr) \ 485 { \ 486 size_t size = bits / NBBY; \ 487 /*CSTYLED*/ \ 488 uint##bits##_t rval; \ 489 int i; \ 490 volatile uint16_t *flags = (volatile uint16_t *) \ 491 &cpu_core[curcpu].cpuc_dtrace_flags; \ 492 \ 493 DTRACE_ALIGNCHECK(addr, size, flags); \ 494 \ 495 for (i = 0; i < dtrace_toxranges; i++) { \ 496 if (addr >= dtrace_toxrange[i].dtt_limit) \ 497 continue; \ 498 \ 499 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 500 continue; \ 501 \ 502 /* \ 503 * This address falls within a toxic region; return 0. \ 504 */ \ 505 *flags |= CPU_DTRACE_BADADDR; \ 506 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 507 return (0); \ 508 } \ 509 \ 510 *flags |= CPU_DTRACE_NOFAULT; \ 511 /*CSTYLED*/ \ 512 rval = *((volatile uint##bits##_t *)addr); \ 513 *flags &= ~CPU_DTRACE_NOFAULT; \ 514 \ 515 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 516 } 517 518 #ifdef _LP64 519 #define dtrace_loadptr dtrace_load64 520 #else 521 #define dtrace_loadptr dtrace_load32 522 #endif 523 524 #define DTRACE_DYNHASH_FREE 0 525 #define DTRACE_DYNHASH_SINK 1 526 #define DTRACE_DYNHASH_VALID 2 527 528 #define DTRACE_MATCH_NEXT 0 529 #define DTRACE_MATCH_DONE 1 530 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 531 #define DTRACE_STATE_ALIGN 64 532 533 #define DTRACE_FLAGS2FLT(flags) \ 534 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 535 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 536 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 537 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 538 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 539 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 540 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 541 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 542 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 543 DTRACEFLT_UNKNOWN) 544 545 #define DTRACEACT_ISSTRING(act) \ 546 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 547 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 548 549 /* Function prototype definitions: */ 550 static size_t dtrace_strlen(const char *, size_t); 551 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 552 static void dtrace_enabling_provide(dtrace_provider_t *); 553 static int dtrace_enabling_match(dtrace_enabling_t *, int *); 554 static void dtrace_enabling_matchall(void); 555 static void dtrace_enabling_reap(void); 556 static dtrace_state_t *dtrace_anon_grab(void); 557 static uint64_t dtrace_helper(int, dtrace_mstate_t *, 558 dtrace_state_t *, uint64_t, uint64_t); 559 static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 560 static void dtrace_buffer_drop(dtrace_buffer_t *); 561 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 562 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 563 dtrace_state_t *, dtrace_mstate_t *); 564 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 565 dtrace_optval_t); 566 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 567 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 568 uint16_t dtrace_load16(uintptr_t); 569 uint32_t dtrace_load32(uintptr_t); 570 uint64_t dtrace_load64(uintptr_t); 571 uint8_t dtrace_load8(uintptr_t); 572 void dtrace_dynvar_clean(dtrace_dstate_t *); 573 dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 574 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 575 uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 576 577 /* 578 * DTrace Probe Context Functions 579 * 580 * These functions are called from probe context. Because probe context is 581 * any context in which C may be called, arbitrarily locks may be held, 582 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 583 * As a result, functions called from probe context may only call other DTrace 584 * support functions -- they may not interact at all with the system at large. 585 * (Note that the ASSERT macro is made probe-context safe by redefining it in 586 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 587 * loads are to be performed from probe context, they _must_ be in terms of 588 * the safe dtrace_load*() variants. 589 * 590 * Some functions in this block are not actually called from probe context; 591 * for these functions, there will be a comment above the function reading 592 * "Note: not called from probe context." 593 */ 594 void 595 dtrace_panic(const char *format, ...) 596 { 597 va_list alist; 598 599 va_start(alist, format); 600 dtrace_vpanic(format, alist); 601 va_end(alist); 602 } 603 604 int 605 dtrace_assfail(const char *a, const char *f, int l) 606 { 607 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 608 609 /* 610 * We just need something here that even the most clever compiler 611 * cannot optimize away. 612 */ 613 return (a[(uintptr_t)f]); 614 } 615 616 /* 617 * Atomically increment a specified error counter from probe context. 618 */ 619 static void 620 dtrace_error(uint32_t *counter) 621 { 622 /* 623 * Most counters stored to in probe context are per-CPU counters. 624 * However, there are some error conditions that are sufficiently 625 * arcane that they don't merit per-CPU storage. If these counters 626 * are incremented concurrently on different CPUs, scalability will be 627 * adversely affected -- but we don't expect them to be white-hot in a 628 * correctly constructed enabling... 629 */ 630 uint32_t oval, nval; 631 632 do { 633 oval = *counter; 634 635 if ((nval = oval + 1) == 0) { 636 /* 637 * If the counter would wrap, set it to 1 -- assuring 638 * that the counter is never zero when we have seen 639 * errors. (The counter must be 32-bits because we 640 * aren't guaranteed a 64-bit compare&swap operation.) 641 * To save this code both the infamy of being fingered 642 * by a priggish news story and the indignity of being 643 * the target of a neo-puritan witch trial, we're 644 * carefully avoiding any colorful description of the 645 * likelihood of this condition -- but suffice it to 646 * say that it is only slightly more likely than the 647 * overflow of predicate cache IDs, as discussed in 648 * dtrace_predicate_create(). 649 */ 650 nval = 1; 651 } 652 } while (dtrace_cas32(counter, oval, nval) != oval); 653 } 654 655 /* 656 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 657 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 658 */ 659 DTRACE_LOADFUNC(8) 660 DTRACE_LOADFUNC(16) 661 DTRACE_LOADFUNC(32) 662 DTRACE_LOADFUNC(64) 663 664 static int 665 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 666 { 667 if (dest < mstate->dtms_scratch_base) 668 return (0); 669 670 if (dest + size < dest) 671 return (0); 672 673 if (dest + size > mstate->dtms_scratch_ptr) 674 return (0); 675 676 return (1); 677 } 678 679 static int 680 dtrace_canstore_statvar(uint64_t addr, size_t sz, 681 dtrace_statvar_t **svars, int nsvars) 682 { 683 int i; 684 685 for (i = 0; i < nsvars; i++) { 686 dtrace_statvar_t *svar = svars[i]; 687 688 if (svar == NULL || svar->dtsv_size == 0) 689 continue; 690 691 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 692 return (1); 693 } 694 695 return (0); 696 } 697 698 /* 699 * Check to see if the address is within a memory region to which a store may 700 * be issued. This includes the DTrace scratch areas, and any DTrace variable 701 * region. The caller of dtrace_canstore() is responsible for performing any 702 * alignment checks that are needed before stores are actually executed. 703 */ 704 static int 705 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 706 dtrace_vstate_t *vstate) 707 { 708 /* 709 * First, check to see if the address is in scratch space... 710 */ 711 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 712 mstate->dtms_scratch_size)) 713 return (1); 714 715 /* 716 * Now check to see if it's a dynamic variable. This check will pick 717 * up both thread-local variables and any global dynamically-allocated 718 * variables. 719 */ 720 if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base, 721 vstate->dtvs_dynvars.dtds_size)) { 722 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 723 uintptr_t base = (uintptr_t)dstate->dtds_base + 724 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 725 uintptr_t chunkoffs; 726 727 /* 728 * Before we assume that we can store here, we need to make 729 * sure that it isn't in our metadata -- storing to our 730 * dynamic variable metadata would corrupt our state. For 731 * the range to not include any dynamic variable metadata, 732 * it must: 733 * 734 * (1) Start above the hash table that is at the base of 735 * the dynamic variable space 736 * 737 * (2) Have a starting chunk offset that is beyond the 738 * dtrace_dynvar_t that is at the base of every chunk 739 * 740 * (3) Not span a chunk boundary 741 * 742 */ 743 if (addr < base) 744 return (0); 745 746 chunkoffs = (addr - base) % dstate->dtds_chunksize; 747 748 if (chunkoffs < sizeof (dtrace_dynvar_t)) 749 return (0); 750 751 if (chunkoffs + sz > dstate->dtds_chunksize) 752 return (0); 753 754 return (1); 755 } 756 757 /* 758 * Finally, check the static local and global variables. These checks 759 * take the longest, so we perform them last. 760 */ 761 if (dtrace_canstore_statvar(addr, sz, 762 vstate->dtvs_locals, vstate->dtvs_nlocals)) 763 return (1); 764 765 if (dtrace_canstore_statvar(addr, sz, 766 vstate->dtvs_globals, vstate->dtvs_nglobals)) 767 return (1); 768 769 return (0); 770 } 771 772 773 /* 774 * Convenience routine to check to see if the address is within a memory 775 * region in which a load may be issued given the user's privilege level; 776 * if not, it sets the appropriate error flags and loads 'addr' into the 777 * illegal value slot. 778 * 779 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 780 * appropriate memory access protection. 781 */ 782 static int 783 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 784 dtrace_vstate_t *vstate) 785 { 786 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 787 788 /* 789 * If we hold the privilege to read from kernel memory, then 790 * everything is readable. 791 */ 792 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 793 return (1); 794 795 /* 796 * You can obviously read that which you can store. 797 */ 798 if (dtrace_canstore(addr, sz, mstate, vstate)) 799 return (1); 800 801 /* 802 * We're allowed to read from our own string table. 803 */ 804 if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab, 805 mstate->dtms_difo->dtdo_strlen)) 806 return (1); 807 808 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 809 *illval = addr; 810 return (0); 811 } 812 813 /* 814 * Convenience routine to check to see if a given string is within a memory 815 * region in which a load may be issued given the user's privilege level; 816 * this exists so that we don't need to issue unnecessary dtrace_strlen() 817 * calls in the event that the user has all privileges. 818 */ 819 static int 820 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 821 dtrace_vstate_t *vstate) 822 { 823 size_t strsz; 824 825 /* 826 * If we hold the privilege to read from kernel memory, then 827 * everything is readable. 828 */ 829 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 830 return (1); 831 832 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 833 if (dtrace_canload(addr, strsz, mstate, vstate)) 834 return (1); 835 836 return (0); 837 } 838 839 /* 840 * Convenience routine to check to see if a given variable is within a memory 841 * region in which a load may be issued given the user's privilege level. 842 */ 843 static int 844 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 845 dtrace_vstate_t *vstate) 846 { 847 size_t sz; 848 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 849 850 /* 851 * If we hold the privilege to read from kernel memory, then 852 * everything is readable. 853 */ 854 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 855 return (1); 856 857 if (type->dtdt_kind == DIF_TYPE_STRING) 858 sz = dtrace_strlen(src, 859 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 860 else 861 sz = type->dtdt_size; 862 863 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 864 } 865 866 /* 867 * Compare two strings using safe loads. 868 */ 869 static int 870 dtrace_strncmp(char *s1, char *s2, size_t limit) 871 { 872 uint8_t c1, c2; 873 volatile uint16_t *flags; 874 875 if (s1 == s2 || limit == 0) 876 return (0); 877 878 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 879 880 do { 881 if (s1 == NULL) { 882 c1 = '\0'; 883 } else { 884 c1 = dtrace_load8((uintptr_t)s1++); 885 } 886 887 if (s2 == NULL) { 888 c2 = '\0'; 889 } else { 890 c2 = dtrace_load8((uintptr_t)s2++); 891 } 892 893 if (c1 != c2) 894 return (c1 - c2); 895 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 896 897 return (0); 898 } 899 900 /* 901 * Compute strlen(s) for a string using safe memory accesses. The additional 902 * len parameter is used to specify a maximum length to ensure completion. 903 */ 904 static size_t 905 dtrace_strlen(const char *s, size_t lim) 906 { 907 uint_t len; 908 909 for (len = 0; len != lim; len++) { 910 if (dtrace_load8((uintptr_t)s++) == '\0') 911 break; 912 } 913 914 return (len); 915 } 916 917 /* 918 * Check if an address falls within a toxic region. 919 */ 920 static int 921 dtrace_istoxic(uintptr_t kaddr, size_t size) 922 { 923 uintptr_t taddr, tsize; 924 int i; 925 926 for (i = 0; i < dtrace_toxranges; i++) { 927 taddr = dtrace_toxrange[i].dtt_base; 928 tsize = dtrace_toxrange[i].dtt_limit - taddr; 929 930 if (kaddr - taddr < tsize) { 931 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 932 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 933 return (1); 934 } 935 936 if (taddr - kaddr < size) { 937 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 938 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 939 return (1); 940 } 941 } 942 943 return (0); 944 } 945 946 /* 947 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 948 * memory specified by the DIF program. The dst is assumed to be safe memory 949 * that we can store to directly because it is managed by DTrace. As with 950 * standard bcopy, overlapping copies are handled properly. 951 */ 952 static void 953 dtrace_bcopy(const void *src, void *dst, size_t len) 954 { 955 if (len != 0) { 956 uint8_t *s1 = dst; 957 const uint8_t *s2 = src; 958 959 if (s1 <= s2) { 960 do { 961 *s1++ = dtrace_load8((uintptr_t)s2++); 962 } while (--len != 0); 963 } else { 964 s2 += len; 965 s1 += len; 966 967 do { 968 *--s1 = dtrace_load8((uintptr_t)--s2); 969 } while (--len != 0); 970 } 971 } 972 } 973 974 /* 975 * Copy src to dst using safe memory accesses, up to either the specified 976 * length, or the point that a nul byte is encountered. The src is assumed to 977 * be unsafe memory specified by the DIF program. The dst is assumed to be 978 * safe memory that we can store to directly because it is managed by DTrace. 979 * Unlike dtrace_bcopy(), overlapping regions are not handled. 980 */ 981 static void 982 dtrace_strcpy(const void *src, void *dst, size_t len) 983 { 984 if (len != 0) { 985 uint8_t *s1 = dst, c; 986 const uint8_t *s2 = src; 987 988 do { 989 *s1++ = c = dtrace_load8((uintptr_t)s2++); 990 } while (--len != 0 && c != '\0'); 991 } 992 } 993 994 /* 995 * Copy src to dst, deriving the size and type from the specified (BYREF) 996 * variable type. The src is assumed to be unsafe memory specified by the DIF 997 * program. The dst is assumed to be DTrace variable memory that is of the 998 * specified type; we assume that we can store to directly. 999 */ 1000 static void 1001 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 1002 { 1003 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1004 1005 if (type->dtdt_kind == DIF_TYPE_STRING) { 1006 dtrace_strcpy(src, dst, type->dtdt_size); 1007 } else { 1008 dtrace_bcopy(src, dst, type->dtdt_size); 1009 } 1010 } 1011 1012 /* 1013 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1014 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1015 * safe memory that we can access directly because it is managed by DTrace. 1016 */ 1017 static int 1018 dtrace_bcmp(const void *s1, const void *s2, size_t len) 1019 { 1020 volatile uint16_t *flags; 1021 1022 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1023 1024 if (s1 == s2) 1025 return (0); 1026 1027 if (s1 == NULL || s2 == NULL) 1028 return (1); 1029 1030 if (s1 != s2 && len != 0) { 1031 const uint8_t *ps1 = s1; 1032 const uint8_t *ps2 = s2; 1033 1034 do { 1035 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1036 return (1); 1037 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1038 } 1039 return (0); 1040 } 1041 1042 /* 1043 * Zero the specified region using a simple byte-by-byte loop. Note that this 1044 * is for safe DTrace-managed memory only. 1045 */ 1046 static void 1047 dtrace_bzero(void *dst, size_t len) 1048 { 1049 uchar_t *cp; 1050 1051 for (cp = dst; len != 0; len--) 1052 *cp++ = 0; 1053 } 1054 1055 static void 1056 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1057 { 1058 uint64_t result[2]; 1059 1060 result[0] = addend1[0] + addend2[0]; 1061 result[1] = addend1[1] + addend2[1] + 1062 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1063 1064 sum[0] = result[0]; 1065 sum[1] = result[1]; 1066 } 1067 1068 /* 1069 * Shift the 128-bit value in a by b. If b is positive, shift left. 1070 * If b is negative, shift right. 1071 */ 1072 static void 1073 dtrace_shift_128(uint64_t *a, int b) 1074 { 1075 uint64_t mask; 1076 1077 if (b == 0) 1078 return; 1079 1080 if (b < 0) { 1081 b = -b; 1082 if (b >= 64) { 1083 a[0] = a[1] >> (b - 64); 1084 a[1] = 0; 1085 } else { 1086 a[0] >>= b; 1087 mask = 1LL << (64 - b); 1088 mask -= 1; 1089 a[0] |= ((a[1] & mask) << (64 - b)); 1090 a[1] >>= b; 1091 } 1092 } else { 1093 if (b >= 64) { 1094 a[1] = a[0] << (b - 64); 1095 a[0] = 0; 1096 } else { 1097 a[1] <<= b; 1098 mask = a[0] >> (64 - b); 1099 a[1] |= mask; 1100 a[0] <<= b; 1101 } 1102 } 1103 } 1104 1105 /* 1106 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1107 * use native multiplication on those, and then re-combine into the 1108 * resulting 128-bit value. 1109 * 1110 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1111 * hi1 * hi2 << 64 + 1112 * hi1 * lo2 << 32 + 1113 * hi2 * lo1 << 32 + 1114 * lo1 * lo2 1115 */ 1116 static void 1117 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1118 { 1119 uint64_t hi1, hi2, lo1, lo2; 1120 uint64_t tmp[2]; 1121 1122 hi1 = factor1 >> 32; 1123 hi2 = factor2 >> 32; 1124 1125 lo1 = factor1 & DT_MASK_LO; 1126 lo2 = factor2 & DT_MASK_LO; 1127 1128 product[0] = lo1 * lo2; 1129 product[1] = hi1 * hi2; 1130 1131 tmp[0] = hi1 * lo2; 1132 tmp[1] = 0; 1133 dtrace_shift_128(tmp, 32); 1134 dtrace_add_128(product, tmp, product); 1135 1136 tmp[0] = hi2 * lo1; 1137 tmp[1] = 0; 1138 dtrace_shift_128(tmp, 32); 1139 dtrace_add_128(product, tmp, product); 1140 } 1141 1142 /* 1143 * This privilege check should be used by actions and subroutines to 1144 * verify that the user credentials of the process that enabled the 1145 * invoking ECB match the target credentials 1146 */ 1147 static int 1148 dtrace_priv_proc_common_user(dtrace_state_t *state) 1149 { 1150 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1151 1152 /* 1153 * We should always have a non-NULL state cred here, since if cred 1154 * is null (anonymous tracing), we fast-path bypass this routine. 1155 */ 1156 ASSERT(s_cr != NULL); 1157 1158 if ((cr = CRED()) != NULL && 1159 s_cr->cr_uid == cr->cr_uid && 1160 s_cr->cr_uid == cr->cr_ruid && 1161 s_cr->cr_uid == cr->cr_suid && 1162 s_cr->cr_gid == cr->cr_gid && 1163 s_cr->cr_gid == cr->cr_rgid && 1164 s_cr->cr_gid == cr->cr_sgid) 1165 return (1); 1166 1167 return (0); 1168 } 1169 1170 /* 1171 * This privilege check should be used by actions and subroutines to 1172 * verify that the zone of the process that enabled the invoking ECB 1173 * matches the target credentials 1174 */ 1175 static int 1176 dtrace_priv_proc_common_zone(dtrace_state_t *state) 1177 { 1178 #if defined(sun) 1179 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1180 1181 /* 1182 * We should always have a non-NULL state cred here, since if cred 1183 * is null (anonymous tracing), we fast-path bypass this routine. 1184 */ 1185 ASSERT(s_cr != NULL); 1186 1187 if ((cr = CRED()) != NULL && 1188 s_cr->cr_zone == cr->cr_zone) 1189 return (1); 1190 1191 return (0); 1192 #else 1193 return (1); 1194 #endif 1195 } 1196 1197 /* 1198 * This privilege check should be used by actions and subroutines to 1199 * verify that the process has not setuid or changed credentials. 1200 */ 1201 static int 1202 dtrace_priv_proc_common_nocd(void) 1203 { 1204 proc_t *proc; 1205 1206 if ((proc = ttoproc(curthread)) != NULL && 1207 !(proc->p_flag & SNOCD)) 1208 return (1); 1209 1210 return (0); 1211 } 1212 1213 static int 1214 dtrace_priv_proc_destructive(dtrace_state_t *state) 1215 { 1216 int action = state->dts_cred.dcr_action; 1217 1218 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1219 dtrace_priv_proc_common_zone(state) == 0) 1220 goto bad; 1221 1222 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1223 dtrace_priv_proc_common_user(state) == 0) 1224 goto bad; 1225 1226 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1227 dtrace_priv_proc_common_nocd() == 0) 1228 goto bad; 1229 1230 return (1); 1231 1232 bad: 1233 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1234 1235 return (0); 1236 } 1237 1238 static int 1239 dtrace_priv_proc_control(dtrace_state_t *state) 1240 { 1241 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1242 return (1); 1243 1244 if (dtrace_priv_proc_common_zone(state) && 1245 dtrace_priv_proc_common_user(state) && 1246 dtrace_priv_proc_common_nocd()) 1247 return (1); 1248 1249 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1250 1251 return (0); 1252 } 1253 1254 static int 1255 dtrace_priv_proc(dtrace_state_t *state) 1256 { 1257 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1258 return (1); 1259 1260 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1261 1262 return (0); 1263 } 1264 1265 static int 1266 dtrace_priv_kernel(dtrace_state_t *state) 1267 { 1268 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1269 return (1); 1270 1271 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1272 1273 return (0); 1274 } 1275 1276 static int 1277 dtrace_priv_kernel_destructive(dtrace_state_t *state) 1278 { 1279 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1280 return (1); 1281 1282 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1283 1284 return (0); 1285 } 1286 1287 /* 1288 * Note: not called from probe context. This function is called 1289 * asynchronously (and at a regular interval) from outside of probe context to 1290 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1291 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1292 */ 1293 void 1294 dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1295 { 1296 dtrace_dynvar_t *dirty; 1297 dtrace_dstate_percpu_t *dcpu; 1298 int i, work = 0; 1299 1300 for (i = 0; i < NCPU; i++) { 1301 dcpu = &dstate->dtds_percpu[i]; 1302 1303 ASSERT(dcpu->dtdsc_rinsing == NULL); 1304 1305 /* 1306 * If the dirty list is NULL, there is no dirty work to do. 1307 */ 1308 if (dcpu->dtdsc_dirty == NULL) 1309 continue; 1310 1311 /* 1312 * If the clean list is non-NULL, then we're not going to do 1313 * any work for this CPU -- it means that there has not been 1314 * a dtrace_dynvar() allocation on this CPU (or from this CPU) 1315 * since the last time we cleaned house. 1316 */ 1317 if (dcpu->dtdsc_clean != NULL) 1318 continue; 1319 1320 work = 1; 1321 1322 /* 1323 * Atomically move the dirty list aside. 1324 */ 1325 do { 1326 dirty = dcpu->dtdsc_dirty; 1327 1328 /* 1329 * Before we zap the dirty list, set the rinsing list. 1330 * (This allows for a potential assertion in 1331 * dtrace_dynvar(): if a free dynamic variable appears 1332 * on a hash chain, either the dirty list or the 1333 * rinsing list for some CPU must be non-NULL.) 1334 */ 1335 dcpu->dtdsc_rinsing = dirty; 1336 dtrace_membar_producer(); 1337 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1338 dirty, NULL) != dirty); 1339 } 1340 1341 if (!work) { 1342 /* 1343 * We have no work to do; we can simply return. 1344 */ 1345 return; 1346 } 1347 1348 dtrace_sync(); 1349 1350 for (i = 0; i < NCPU; i++) { 1351 dcpu = &dstate->dtds_percpu[i]; 1352 1353 if (dcpu->dtdsc_rinsing == NULL) 1354 continue; 1355 1356 /* 1357 * We are now guaranteed that no hash chain contains a pointer 1358 * into this dirty list; we can make it clean. 1359 */ 1360 ASSERT(dcpu->dtdsc_clean == NULL); 1361 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1362 dcpu->dtdsc_rinsing = NULL; 1363 } 1364 1365 /* 1366 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1367 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1368 * This prevents a race whereby a CPU incorrectly decides that 1369 * the state should be something other than DTRACE_DSTATE_CLEAN 1370 * after dtrace_dynvar_clean() has completed. 1371 */ 1372 dtrace_sync(); 1373 1374 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1375 } 1376 1377 /* 1378 * Depending on the value of the op parameter, this function looks-up, 1379 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1380 * allocation is requested, this function will return a pointer to a 1381 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1382 * variable can be allocated. If NULL is returned, the appropriate counter 1383 * will be incremented. 1384 */ 1385 dtrace_dynvar_t * 1386 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1387 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1388 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1389 { 1390 uint64_t hashval = DTRACE_DYNHASH_VALID; 1391 dtrace_dynhash_t *hash = dstate->dtds_hash; 1392 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1393 processorid_t me = curcpu, cpu = me; 1394 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1395 size_t bucket, ksize; 1396 size_t chunksize = dstate->dtds_chunksize; 1397 uintptr_t kdata, lock, nstate; 1398 uint_t i; 1399 1400 ASSERT(nkeys != 0); 1401 1402 /* 1403 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1404 * algorithm. For the by-value portions, we perform the algorithm in 1405 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1406 * bit, and seems to have only a minute effect on distribution. For 1407 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1408 * over each referenced byte. It's painful to do this, but it's much 1409 * better than pathological hash distribution. The efficacy of the 1410 * hashing algorithm (and a comparison with other algorithms) may be 1411 * found by running the ::dtrace_dynstat MDB dcmd. 1412 */ 1413 for (i = 0; i < nkeys; i++) { 1414 if (key[i].dttk_size == 0) { 1415 uint64_t val = key[i].dttk_value; 1416 1417 hashval += (val >> 48) & 0xffff; 1418 hashval += (hashval << 10); 1419 hashval ^= (hashval >> 6); 1420 1421 hashval += (val >> 32) & 0xffff; 1422 hashval += (hashval << 10); 1423 hashval ^= (hashval >> 6); 1424 1425 hashval += (val >> 16) & 0xffff; 1426 hashval += (hashval << 10); 1427 hashval ^= (hashval >> 6); 1428 1429 hashval += val & 0xffff; 1430 hashval += (hashval << 10); 1431 hashval ^= (hashval >> 6); 1432 } else { 1433 /* 1434 * This is incredibly painful, but it beats the hell 1435 * out of the alternative. 1436 */ 1437 uint64_t j, size = key[i].dttk_size; 1438 uintptr_t base = (uintptr_t)key[i].dttk_value; 1439 1440 if (!dtrace_canload(base, size, mstate, vstate)) 1441 break; 1442 1443 for (j = 0; j < size; j++) { 1444 hashval += dtrace_load8(base + j); 1445 hashval += (hashval << 10); 1446 hashval ^= (hashval >> 6); 1447 } 1448 } 1449 } 1450 1451 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1452 return (NULL); 1453 1454 hashval += (hashval << 3); 1455 hashval ^= (hashval >> 11); 1456 hashval += (hashval << 15); 1457 1458 /* 1459 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1460 * comes out to be one of our two sentinel hash values. If this 1461 * actually happens, we set the hashval to be a value known to be a 1462 * non-sentinel value. 1463 */ 1464 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1465 hashval = DTRACE_DYNHASH_VALID; 1466 1467 /* 1468 * Yes, it's painful to do a divide here. If the cycle count becomes 1469 * important here, tricks can be pulled to reduce it. (However, it's 1470 * critical that hash collisions be kept to an absolute minimum; 1471 * they're much more painful than a divide.) It's better to have a 1472 * solution that generates few collisions and still keeps things 1473 * relatively simple. 1474 */ 1475 bucket = hashval % dstate->dtds_hashsize; 1476 1477 if (op == DTRACE_DYNVAR_DEALLOC) { 1478 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1479 1480 for (;;) { 1481 while ((lock = *lockp) & 1) 1482 continue; 1483 1484 if (dtrace_casptr((volatile void *)lockp, 1485 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1486 break; 1487 } 1488 1489 dtrace_membar_producer(); 1490 } 1491 1492 top: 1493 prev = NULL; 1494 lock = hash[bucket].dtdh_lock; 1495 1496 dtrace_membar_consumer(); 1497 1498 start = hash[bucket].dtdh_chain; 1499 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1500 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1501 op != DTRACE_DYNVAR_DEALLOC)); 1502 1503 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1504 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1505 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1506 1507 if (dvar->dtdv_hashval != hashval) { 1508 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1509 /* 1510 * We've reached the sink, and therefore the 1511 * end of the hash chain; we can kick out of 1512 * the loop knowing that we have seen a valid 1513 * snapshot of state. 1514 */ 1515 ASSERT(dvar->dtdv_next == NULL); 1516 ASSERT(dvar == &dtrace_dynhash_sink); 1517 break; 1518 } 1519 1520 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1521 /* 1522 * We've gone off the rails: somewhere along 1523 * the line, one of the members of this hash 1524 * chain was deleted. Note that we could also 1525 * detect this by simply letting this loop run 1526 * to completion, as we would eventually hit 1527 * the end of the dirty list. However, we 1528 * want to avoid running the length of the 1529 * dirty list unnecessarily (it might be quite 1530 * long), so we catch this as early as 1531 * possible by detecting the hash marker. In 1532 * this case, we simply set dvar to NULL and 1533 * break; the conditional after the loop will 1534 * send us back to top. 1535 */ 1536 dvar = NULL; 1537 break; 1538 } 1539 1540 goto next; 1541 } 1542 1543 if (dtuple->dtt_nkeys != nkeys) 1544 goto next; 1545 1546 for (i = 0; i < nkeys; i++, dkey++) { 1547 if (dkey->dttk_size != key[i].dttk_size) 1548 goto next; /* size or type mismatch */ 1549 1550 if (dkey->dttk_size != 0) { 1551 if (dtrace_bcmp( 1552 (void *)(uintptr_t)key[i].dttk_value, 1553 (void *)(uintptr_t)dkey->dttk_value, 1554 dkey->dttk_size)) 1555 goto next; 1556 } else { 1557 if (dkey->dttk_value != key[i].dttk_value) 1558 goto next; 1559 } 1560 } 1561 1562 if (op != DTRACE_DYNVAR_DEALLOC) 1563 return (dvar); 1564 1565 ASSERT(dvar->dtdv_next == NULL || 1566 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1567 1568 if (prev != NULL) { 1569 ASSERT(hash[bucket].dtdh_chain != dvar); 1570 ASSERT(start != dvar); 1571 ASSERT(prev->dtdv_next == dvar); 1572 prev->dtdv_next = dvar->dtdv_next; 1573 } else { 1574 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1575 start, dvar->dtdv_next) != start) { 1576 /* 1577 * We have failed to atomically swing the 1578 * hash table head pointer, presumably because 1579 * of a conflicting allocation on another CPU. 1580 * We need to reread the hash chain and try 1581 * again. 1582 */ 1583 goto top; 1584 } 1585 } 1586 1587 dtrace_membar_producer(); 1588 1589 /* 1590 * Now set the hash value to indicate that it's free. 1591 */ 1592 ASSERT(hash[bucket].dtdh_chain != dvar); 1593 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1594 1595 dtrace_membar_producer(); 1596 1597 /* 1598 * Set the next pointer to point at the dirty list, and 1599 * atomically swing the dirty pointer to the newly freed dvar. 1600 */ 1601 do { 1602 next = dcpu->dtdsc_dirty; 1603 dvar->dtdv_next = next; 1604 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1605 1606 /* 1607 * Finally, unlock this hash bucket. 1608 */ 1609 ASSERT(hash[bucket].dtdh_lock == lock); 1610 ASSERT(lock & 1); 1611 hash[bucket].dtdh_lock++; 1612 1613 return (NULL); 1614 next: 1615 prev = dvar; 1616 continue; 1617 } 1618 1619 if (dvar == NULL) { 1620 /* 1621 * If dvar is NULL, it is because we went off the rails: 1622 * one of the elements that we traversed in the hash chain 1623 * was deleted while we were traversing it. In this case, 1624 * we assert that we aren't doing a dealloc (deallocs lock 1625 * the hash bucket to prevent themselves from racing with 1626 * one another), and retry the hash chain traversal. 1627 */ 1628 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1629 goto top; 1630 } 1631 1632 if (op != DTRACE_DYNVAR_ALLOC) { 1633 /* 1634 * If we are not to allocate a new variable, we want to 1635 * return NULL now. Before we return, check that the value 1636 * of the lock word hasn't changed. If it has, we may have 1637 * seen an inconsistent snapshot. 1638 */ 1639 if (op == DTRACE_DYNVAR_NOALLOC) { 1640 if (hash[bucket].dtdh_lock != lock) 1641 goto top; 1642 } else { 1643 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1644 ASSERT(hash[bucket].dtdh_lock == lock); 1645 ASSERT(lock & 1); 1646 hash[bucket].dtdh_lock++; 1647 } 1648 1649 return (NULL); 1650 } 1651 1652 /* 1653 * We need to allocate a new dynamic variable. The size we need is the 1654 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1655 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1656 * the size of any referred-to data (dsize). We then round the final 1657 * size up to the chunksize for allocation. 1658 */ 1659 for (ksize = 0, i = 0; i < nkeys; i++) 1660 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1661 1662 /* 1663 * This should be pretty much impossible, but could happen if, say, 1664 * strange DIF specified the tuple. Ideally, this should be an 1665 * assertion and not an error condition -- but that requires that the 1666 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1667 * bullet-proof. (That is, it must not be able to be fooled by 1668 * malicious DIF.) Given the lack of backwards branches in DIF, 1669 * solving this would presumably not amount to solving the Halting 1670 * Problem -- but it still seems awfully hard. 1671 */ 1672 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1673 ksize + dsize > chunksize) { 1674 dcpu->dtdsc_drops++; 1675 return (NULL); 1676 } 1677 1678 nstate = DTRACE_DSTATE_EMPTY; 1679 1680 do { 1681 retry: 1682 free = dcpu->dtdsc_free; 1683 1684 if (free == NULL) { 1685 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1686 void *rval; 1687 1688 if (clean == NULL) { 1689 /* 1690 * We're out of dynamic variable space on 1691 * this CPU. Unless we have tried all CPUs, 1692 * we'll try to allocate from a different 1693 * CPU. 1694 */ 1695 switch (dstate->dtds_state) { 1696 case DTRACE_DSTATE_CLEAN: { 1697 void *sp = &dstate->dtds_state; 1698 1699 if (++cpu >= NCPU) 1700 cpu = 0; 1701 1702 if (dcpu->dtdsc_dirty != NULL && 1703 nstate == DTRACE_DSTATE_EMPTY) 1704 nstate = DTRACE_DSTATE_DIRTY; 1705 1706 if (dcpu->dtdsc_rinsing != NULL) 1707 nstate = DTRACE_DSTATE_RINSING; 1708 1709 dcpu = &dstate->dtds_percpu[cpu]; 1710 1711 if (cpu != me) 1712 goto retry; 1713 1714 (void) dtrace_cas32(sp, 1715 DTRACE_DSTATE_CLEAN, nstate); 1716 1717 /* 1718 * To increment the correct bean 1719 * counter, take another lap. 1720 */ 1721 goto retry; 1722 } 1723 1724 case DTRACE_DSTATE_DIRTY: 1725 dcpu->dtdsc_dirty_drops++; 1726 break; 1727 1728 case DTRACE_DSTATE_RINSING: 1729 dcpu->dtdsc_rinsing_drops++; 1730 break; 1731 1732 case DTRACE_DSTATE_EMPTY: 1733 dcpu->dtdsc_drops++; 1734 break; 1735 } 1736 1737 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1738 return (NULL); 1739 } 1740 1741 /* 1742 * The clean list appears to be non-empty. We want to 1743 * move the clean list to the free list; we start by 1744 * moving the clean pointer aside. 1745 */ 1746 if (dtrace_casptr(&dcpu->dtdsc_clean, 1747 clean, NULL) != clean) { 1748 /* 1749 * We are in one of two situations: 1750 * 1751 * (a) The clean list was switched to the 1752 * free list by another CPU. 1753 * 1754 * (b) The clean list was added to by the 1755 * cleansing cyclic. 1756 * 1757 * In either of these situations, we can 1758 * just reattempt the free list allocation. 1759 */ 1760 goto retry; 1761 } 1762 1763 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1764 1765 /* 1766 * Now we'll move the clean list to the free list. 1767 * It's impossible for this to fail: the only way 1768 * the free list can be updated is through this 1769 * code path, and only one CPU can own the clean list. 1770 * Thus, it would only be possible for this to fail if 1771 * this code were racing with dtrace_dynvar_clean(). 1772 * (That is, if dtrace_dynvar_clean() updated the clean 1773 * list, and we ended up racing to update the free 1774 * list.) This race is prevented by the dtrace_sync() 1775 * in dtrace_dynvar_clean() -- which flushes the 1776 * owners of the clean lists out before resetting 1777 * the clean lists. 1778 */ 1779 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1780 ASSERT(rval == NULL); 1781 goto retry; 1782 } 1783 1784 dvar = free; 1785 new_free = dvar->dtdv_next; 1786 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1787 1788 /* 1789 * We have now allocated a new chunk. We copy the tuple keys into the 1790 * tuple array and copy any referenced key data into the data space 1791 * following the tuple array. As we do this, we relocate dttk_value 1792 * in the final tuple to point to the key data address in the chunk. 1793 */ 1794 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1795 dvar->dtdv_data = (void *)(kdata + ksize); 1796 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1797 1798 for (i = 0; i < nkeys; i++) { 1799 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1800 size_t kesize = key[i].dttk_size; 1801 1802 if (kesize != 0) { 1803 dtrace_bcopy( 1804 (const void *)(uintptr_t)key[i].dttk_value, 1805 (void *)kdata, kesize); 1806 dkey->dttk_value = kdata; 1807 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1808 } else { 1809 dkey->dttk_value = key[i].dttk_value; 1810 } 1811 1812 dkey->dttk_size = kesize; 1813 } 1814 1815 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1816 dvar->dtdv_hashval = hashval; 1817 dvar->dtdv_next = start; 1818 1819 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1820 return (dvar); 1821 1822 /* 1823 * The cas has failed. Either another CPU is adding an element to 1824 * this hash chain, or another CPU is deleting an element from this 1825 * hash chain. The simplest way to deal with both of these cases 1826 * (though not necessarily the most efficient) is to free our 1827 * allocated block and tail-call ourselves. Note that the free is 1828 * to the dirty list and _not_ to the free list. This is to prevent 1829 * races with allocators, above. 1830 */ 1831 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1832 1833 dtrace_membar_producer(); 1834 1835 do { 1836 free = dcpu->dtdsc_dirty; 1837 dvar->dtdv_next = free; 1838 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1839 1840 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 1841 } 1842 1843 /*ARGSUSED*/ 1844 static void 1845 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1846 { 1847 if ((int64_t)nval < (int64_t)*oval) 1848 *oval = nval; 1849 } 1850 1851 /*ARGSUSED*/ 1852 static void 1853 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1854 { 1855 if ((int64_t)nval > (int64_t)*oval) 1856 *oval = nval; 1857 } 1858 1859 static void 1860 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1861 { 1862 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1863 int64_t val = (int64_t)nval; 1864 1865 if (val < 0) { 1866 for (i = 0; i < zero; i++) { 1867 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1868 quanta[i] += incr; 1869 return; 1870 } 1871 } 1872 } else { 1873 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1874 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1875 quanta[i - 1] += incr; 1876 return; 1877 } 1878 } 1879 1880 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1881 return; 1882 } 1883 1884 ASSERT(0); 1885 } 1886 1887 static void 1888 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1889 { 1890 uint64_t arg = *lquanta++; 1891 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1892 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1893 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1894 int32_t val = (int32_t)nval, level; 1895 1896 ASSERT(step != 0); 1897 ASSERT(levels != 0); 1898 1899 if (val < base) { 1900 /* 1901 * This is an underflow. 1902 */ 1903 lquanta[0] += incr; 1904 return; 1905 } 1906 1907 level = (val - base) / step; 1908 1909 if (level < levels) { 1910 lquanta[level + 1] += incr; 1911 return; 1912 } 1913 1914 /* 1915 * This is an overflow. 1916 */ 1917 lquanta[levels + 1] += incr; 1918 } 1919 1920 static int 1921 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 1922 uint16_t high, uint16_t nsteps, int64_t value) 1923 { 1924 int64_t this = 1, last, next; 1925 int base = 1, order; 1926 1927 ASSERT(factor <= nsteps); 1928 ASSERT(nsteps % factor == 0); 1929 1930 for (order = 0; order < low; order++) 1931 this *= factor; 1932 1933 /* 1934 * If our value is less than our factor taken to the power of the 1935 * low order of magnitude, it goes into the zeroth bucket. 1936 */ 1937 if (value < (last = this)) 1938 return (0); 1939 1940 for (this *= factor; order <= high; order++) { 1941 int nbuckets = this > nsteps ? nsteps : this; 1942 1943 if ((next = this * factor) < this) { 1944 /* 1945 * We should not generally get log/linear quantizations 1946 * with a high magnitude that allows 64-bits to 1947 * overflow, but we nonetheless protect against this 1948 * by explicitly checking for overflow, and clamping 1949 * our value accordingly. 1950 */ 1951 value = this - 1; 1952 } 1953 1954 if (value < this) { 1955 /* 1956 * If our value lies within this order of magnitude, 1957 * determine its position by taking the offset within 1958 * the order of magnitude, dividing by the bucket 1959 * width, and adding to our (accumulated) base. 1960 */ 1961 return (base + (value - last) / (this / nbuckets)); 1962 } 1963 1964 base += nbuckets - (nbuckets / factor); 1965 last = this; 1966 this = next; 1967 } 1968 1969 /* 1970 * Our value is greater than or equal to our factor taken to the 1971 * power of one plus the high magnitude -- return the top bucket. 1972 */ 1973 return (base); 1974 } 1975 1976 static void 1977 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 1978 { 1979 uint64_t arg = *llquanta++; 1980 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 1981 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 1982 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 1983 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 1984 1985 llquanta[dtrace_aggregate_llquantize_bucket(factor, 1986 low, high, nsteps, nval)] += incr; 1987 } 1988 1989 /*ARGSUSED*/ 1990 static void 1991 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 1992 { 1993 data[0]++; 1994 data[1] += nval; 1995 } 1996 1997 /*ARGSUSED*/ 1998 static void 1999 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2000 { 2001 int64_t snval = (int64_t)nval; 2002 uint64_t tmp[2]; 2003 2004 data[0]++; 2005 data[1] += nval; 2006 2007 /* 2008 * What we want to say here is: 2009 * 2010 * data[2] += nval * nval; 2011 * 2012 * But given that nval is 64-bit, we could easily overflow, so 2013 * we do this as 128-bit arithmetic. 2014 */ 2015 if (snval < 0) 2016 snval = -snval; 2017 2018 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2019 dtrace_add_128(data + 2, tmp, data + 2); 2020 } 2021 2022 /*ARGSUSED*/ 2023 static void 2024 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2025 { 2026 *oval = *oval + 1; 2027 } 2028 2029 /*ARGSUSED*/ 2030 static void 2031 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2032 { 2033 *oval += nval; 2034 } 2035 2036 /* 2037 * Aggregate given the tuple in the principal data buffer, and the aggregating 2038 * action denoted by the specified dtrace_aggregation_t. The aggregation 2039 * buffer is specified as the buf parameter. This routine does not return 2040 * failure; if there is no space in the aggregation buffer, the data will be 2041 * dropped, and a corresponding counter incremented. 2042 */ 2043 static void 2044 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2045 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2046 { 2047 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2048 uint32_t i, ndx, size, fsize; 2049 uint32_t align = sizeof (uint64_t) - 1; 2050 dtrace_aggbuffer_t *agb; 2051 dtrace_aggkey_t *key; 2052 uint32_t hashval = 0, limit, isstr; 2053 caddr_t tomax, data, kdata; 2054 dtrace_actkind_t action; 2055 dtrace_action_t *act; 2056 uintptr_t offs; 2057 2058 if (buf == NULL) 2059 return; 2060 2061 if (!agg->dtag_hasarg) { 2062 /* 2063 * Currently, only quantize() and lquantize() take additional 2064 * arguments, and they have the same semantics: an increment 2065 * value that defaults to 1 when not present. If additional 2066 * aggregating actions take arguments, the setting of the 2067 * default argument value will presumably have to become more 2068 * sophisticated... 2069 */ 2070 arg = 1; 2071 } 2072 2073 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2074 size = rec->dtrd_offset - agg->dtag_base; 2075 fsize = size + rec->dtrd_size; 2076 2077 ASSERT(dbuf->dtb_tomax != NULL); 2078 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2079 2080 if ((tomax = buf->dtb_tomax) == NULL) { 2081 dtrace_buffer_drop(buf); 2082 return; 2083 } 2084 2085 /* 2086 * The metastructure is always at the bottom of the buffer. 2087 */ 2088 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2089 sizeof (dtrace_aggbuffer_t)); 2090 2091 if (buf->dtb_offset == 0) { 2092 /* 2093 * We just kludge up approximately 1/8th of the size to be 2094 * buckets. If this guess ends up being routinely 2095 * off-the-mark, we may need to dynamically readjust this 2096 * based on past performance. 2097 */ 2098 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2099 2100 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2101 (uintptr_t)tomax || hashsize == 0) { 2102 /* 2103 * We've been given a ludicrously small buffer; 2104 * increment our drop count and leave. 2105 */ 2106 dtrace_buffer_drop(buf); 2107 return; 2108 } 2109 2110 /* 2111 * And now, a pathetic attempt to try to get a an odd (or 2112 * perchance, a prime) hash size for better hash distribution. 2113 */ 2114 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2115 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2116 2117 agb->dtagb_hashsize = hashsize; 2118 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2119 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2120 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2121 2122 for (i = 0; i < agb->dtagb_hashsize; i++) 2123 agb->dtagb_hash[i] = NULL; 2124 } 2125 2126 ASSERT(agg->dtag_first != NULL); 2127 ASSERT(agg->dtag_first->dta_intuple); 2128 2129 /* 2130 * Calculate the hash value based on the key. Note that we _don't_ 2131 * include the aggid in the hashing (but we will store it as part of 2132 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2133 * algorithm: a simple, quick algorithm that has no known funnels, and 2134 * gets good distribution in practice. The efficacy of the hashing 2135 * algorithm (and a comparison with other algorithms) may be found by 2136 * running the ::dtrace_aggstat MDB dcmd. 2137 */ 2138 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2139 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2140 limit = i + act->dta_rec.dtrd_size; 2141 ASSERT(limit <= size); 2142 isstr = DTRACEACT_ISSTRING(act); 2143 2144 for (; i < limit; i++) { 2145 hashval += data[i]; 2146 hashval += (hashval << 10); 2147 hashval ^= (hashval >> 6); 2148 2149 if (isstr && data[i] == '\0') 2150 break; 2151 } 2152 } 2153 2154 hashval += (hashval << 3); 2155 hashval ^= (hashval >> 11); 2156 hashval += (hashval << 15); 2157 2158 /* 2159 * Yes, the divide here is expensive -- but it's generally the least 2160 * of the performance issues given the amount of data that we iterate 2161 * over to compute hash values, compare data, etc. 2162 */ 2163 ndx = hashval % agb->dtagb_hashsize; 2164 2165 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2166 ASSERT((caddr_t)key >= tomax); 2167 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2168 2169 if (hashval != key->dtak_hashval || key->dtak_size != size) 2170 continue; 2171 2172 kdata = key->dtak_data; 2173 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2174 2175 for (act = agg->dtag_first; act->dta_intuple; 2176 act = act->dta_next) { 2177 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2178 limit = i + act->dta_rec.dtrd_size; 2179 ASSERT(limit <= size); 2180 isstr = DTRACEACT_ISSTRING(act); 2181 2182 for (; i < limit; i++) { 2183 if (kdata[i] != data[i]) 2184 goto next; 2185 2186 if (isstr && data[i] == '\0') 2187 break; 2188 } 2189 } 2190 2191 if (action != key->dtak_action) { 2192 /* 2193 * We are aggregating on the same value in the same 2194 * aggregation with two different aggregating actions. 2195 * (This should have been picked up in the compiler, 2196 * so we may be dealing with errant or devious DIF.) 2197 * This is an error condition; we indicate as much, 2198 * and return. 2199 */ 2200 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2201 return; 2202 } 2203 2204 /* 2205 * This is a hit: we need to apply the aggregator to 2206 * the value at this key. 2207 */ 2208 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2209 return; 2210 next: 2211 continue; 2212 } 2213 2214 /* 2215 * We didn't find it. We need to allocate some zero-filled space, 2216 * link it into the hash table appropriately, and apply the aggregator 2217 * to the (zero-filled) value. 2218 */ 2219 offs = buf->dtb_offset; 2220 while (offs & (align - 1)) 2221 offs += sizeof (uint32_t); 2222 2223 /* 2224 * If we don't have enough room to both allocate a new key _and_ 2225 * its associated data, increment the drop count and return. 2226 */ 2227 if ((uintptr_t)tomax + offs + fsize > 2228 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2229 dtrace_buffer_drop(buf); 2230 return; 2231 } 2232 2233 /*CONSTCOND*/ 2234 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2235 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2236 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2237 2238 key->dtak_data = kdata = tomax + offs; 2239 buf->dtb_offset = offs + fsize; 2240 2241 /* 2242 * Now copy the data across. 2243 */ 2244 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2245 2246 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2247 kdata[i] = data[i]; 2248 2249 /* 2250 * Because strings are not zeroed out by default, we need to iterate 2251 * looking for actions that store strings, and we need to explicitly 2252 * pad these strings out with zeroes. 2253 */ 2254 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2255 int nul; 2256 2257 if (!DTRACEACT_ISSTRING(act)) 2258 continue; 2259 2260 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2261 limit = i + act->dta_rec.dtrd_size; 2262 ASSERT(limit <= size); 2263 2264 for (nul = 0; i < limit; i++) { 2265 if (nul) { 2266 kdata[i] = '\0'; 2267 continue; 2268 } 2269 2270 if (data[i] != '\0') 2271 continue; 2272 2273 nul = 1; 2274 } 2275 } 2276 2277 for (i = size; i < fsize; i++) 2278 kdata[i] = 0; 2279 2280 key->dtak_hashval = hashval; 2281 key->dtak_size = size; 2282 key->dtak_action = action; 2283 key->dtak_next = agb->dtagb_hash[ndx]; 2284 agb->dtagb_hash[ndx] = key; 2285 2286 /* 2287 * Finally, apply the aggregator. 2288 */ 2289 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2290 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2291 } 2292 2293 /* 2294 * Given consumer state, this routine finds a speculation in the INACTIVE 2295 * state and transitions it into the ACTIVE state. If there is no speculation 2296 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2297 * incremented -- it is up to the caller to take appropriate action. 2298 */ 2299 static int 2300 dtrace_speculation(dtrace_state_t *state) 2301 { 2302 int i = 0; 2303 dtrace_speculation_state_t current; 2304 uint32_t *stat = &state->dts_speculations_unavail, count; 2305 2306 while (i < state->dts_nspeculations) { 2307 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2308 2309 current = spec->dtsp_state; 2310 2311 if (current != DTRACESPEC_INACTIVE) { 2312 if (current == DTRACESPEC_COMMITTINGMANY || 2313 current == DTRACESPEC_COMMITTING || 2314 current == DTRACESPEC_DISCARDING) 2315 stat = &state->dts_speculations_busy; 2316 i++; 2317 continue; 2318 } 2319 2320 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2321 current, DTRACESPEC_ACTIVE) == current) 2322 return (i + 1); 2323 } 2324 2325 /* 2326 * We couldn't find a speculation. If we found as much as a single 2327 * busy speculation buffer, we'll attribute this failure as "busy" 2328 * instead of "unavail". 2329 */ 2330 do { 2331 count = *stat; 2332 } while (dtrace_cas32(stat, count, count + 1) != count); 2333 2334 return (0); 2335 } 2336 2337 /* 2338 * This routine commits an active speculation. If the specified speculation 2339 * is not in a valid state to perform a commit(), this routine will silently do 2340 * nothing. The state of the specified speculation is transitioned according 2341 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2342 */ 2343 static void 2344 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2345 dtrace_specid_t which) 2346 { 2347 dtrace_speculation_t *spec; 2348 dtrace_buffer_t *src, *dest; 2349 uintptr_t daddr, saddr, dlimit, slimit; 2350 dtrace_speculation_state_t current, new = 0; 2351 intptr_t offs; 2352 uint64_t timestamp; 2353 2354 if (which == 0) 2355 return; 2356 2357 if (which > state->dts_nspeculations) { 2358 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2359 return; 2360 } 2361 2362 spec = &state->dts_speculations[which - 1]; 2363 src = &spec->dtsp_buffer[cpu]; 2364 dest = &state->dts_buffer[cpu]; 2365 2366 do { 2367 current = spec->dtsp_state; 2368 2369 if (current == DTRACESPEC_COMMITTINGMANY) 2370 break; 2371 2372 switch (current) { 2373 case DTRACESPEC_INACTIVE: 2374 case DTRACESPEC_DISCARDING: 2375 return; 2376 2377 case DTRACESPEC_COMMITTING: 2378 /* 2379 * This is only possible if we are (a) commit()'ing 2380 * without having done a prior speculate() on this CPU 2381 * and (b) racing with another commit() on a different 2382 * CPU. There's nothing to do -- we just assert that 2383 * our offset is 0. 2384 */ 2385 ASSERT(src->dtb_offset == 0); 2386 return; 2387 2388 case DTRACESPEC_ACTIVE: 2389 new = DTRACESPEC_COMMITTING; 2390 break; 2391 2392 case DTRACESPEC_ACTIVEONE: 2393 /* 2394 * This speculation is active on one CPU. If our 2395 * buffer offset is non-zero, we know that the one CPU 2396 * must be us. Otherwise, we are committing on a 2397 * different CPU from the speculate(), and we must 2398 * rely on being asynchronously cleaned. 2399 */ 2400 if (src->dtb_offset != 0) { 2401 new = DTRACESPEC_COMMITTING; 2402 break; 2403 } 2404 /*FALLTHROUGH*/ 2405 2406 case DTRACESPEC_ACTIVEMANY: 2407 new = DTRACESPEC_COMMITTINGMANY; 2408 break; 2409 2410 default: 2411 ASSERT(0); 2412 } 2413 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2414 current, new) != current); 2415 2416 /* 2417 * We have set the state to indicate that we are committing this 2418 * speculation. Now reserve the necessary space in the destination 2419 * buffer. 2420 */ 2421 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2422 sizeof (uint64_t), state, NULL)) < 0) { 2423 dtrace_buffer_drop(dest); 2424 goto out; 2425 } 2426 2427 /* 2428 * We have sufficient space to copy the speculative buffer into the 2429 * primary buffer. First, modify the speculative buffer, filling 2430 * in the timestamp of all entries with the current time. The data 2431 * must have the commit() time rather than the time it was traced, 2432 * so that all entries in the primary buffer are in timestamp order. 2433 */ 2434 timestamp = dtrace_gethrtime(); 2435 saddr = (uintptr_t)src->dtb_tomax; 2436 slimit = saddr + src->dtb_offset; 2437 while (saddr < slimit) { 2438 size_t size; 2439 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2440 2441 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2442 saddr += sizeof (dtrace_epid_t); 2443 continue; 2444 } 2445 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2446 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2447 2448 ASSERT3U(saddr + size, <=, slimit); 2449 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2450 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2451 2452 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2453 2454 saddr += size; 2455 } 2456 2457 /* 2458 * Copy the buffer across. (Note that this is a 2459 * highly subobtimal bcopy(); in the unlikely event that this becomes 2460 * a serious performance issue, a high-performance DTrace-specific 2461 * bcopy() should obviously be invented.) 2462 */ 2463 daddr = (uintptr_t)dest->dtb_tomax + offs; 2464 dlimit = daddr + src->dtb_offset; 2465 saddr = (uintptr_t)src->dtb_tomax; 2466 2467 /* 2468 * First, the aligned portion. 2469 */ 2470 while (dlimit - daddr >= sizeof (uint64_t)) { 2471 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2472 2473 daddr += sizeof (uint64_t); 2474 saddr += sizeof (uint64_t); 2475 } 2476 2477 /* 2478 * Now any left-over bit... 2479 */ 2480 while (dlimit - daddr) 2481 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2482 2483 /* 2484 * Finally, commit the reserved space in the destination buffer. 2485 */ 2486 dest->dtb_offset = offs + src->dtb_offset; 2487 2488 out: 2489 /* 2490 * If we're lucky enough to be the only active CPU on this speculation 2491 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2492 */ 2493 if (current == DTRACESPEC_ACTIVE || 2494 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2495 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2496 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2497 2498 ASSERT(rval == DTRACESPEC_COMMITTING); 2499 } 2500 2501 src->dtb_offset = 0; 2502 src->dtb_xamot_drops += src->dtb_drops; 2503 src->dtb_drops = 0; 2504 } 2505 2506 /* 2507 * This routine discards an active speculation. If the specified speculation 2508 * is not in a valid state to perform a discard(), this routine will silently 2509 * do nothing. The state of the specified speculation is transitioned 2510 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2511 */ 2512 static void 2513 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2514 dtrace_specid_t which) 2515 { 2516 dtrace_speculation_t *spec; 2517 dtrace_speculation_state_t current, new = 0; 2518 dtrace_buffer_t *buf; 2519 2520 if (which == 0) 2521 return; 2522 2523 if (which > state->dts_nspeculations) { 2524 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2525 return; 2526 } 2527 2528 spec = &state->dts_speculations[which - 1]; 2529 buf = &spec->dtsp_buffer[cpu]; 2530 2531 do { 2532 current = spec->dtsp_state; 2533 2534 switch (current) { 2535 case DTRACESPEC_INACTIVE: 2536 case DTRACESPEC_COMMITTINGMANY: 2537 case DTRACESPEC_COMMITTING: 2538 case DTRACESPEC_DISCARDING: 2539 return; 2540 2541 case DTRACESPEC_ACTIVE: 2542 case DTRACESPEC_ACTIVEMANY: 2543 new = DTRACESPEC_DISCARDING; 2544 break; 2545 2546 case DTRACESPEC_ACTIVEONE: 2547 if (buf->dtb_offset != 0) { 2548 new = DTRACESPEC_INACTIVE; 2549 } else { 2550 new = DTRACESPEC_DISCARDING; 2551 } 2552 break; 2553 2554 default: 2555 ASSERT(0); 2556 } 2557 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2558 current, new) != current); 2559 2560 buf->dtb_offset = 0; 2561 buf->dtb_drops = 0; 2562 } 2563 2564 /* 2565 * Note: not called from probe context. This function is called 2566 * asynchronously from cross call context to clean any speculations that are 2567 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2568 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2569 * speculation. 2570 */ 2571 static void 2572 dtrace_speculation_clean_here(dtrace_state_t *state) 2573 { 2574 dtrace_icookie_t cookie; 2575 processorid_t cpu = curcpu; 2576 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2577 dtrace_specid_t i; 2578 2579 cookie = dtrace_interrupt_disable(); 2580 2581 if (dest->dtb_tomax == NULL) { 2582 dtrace_interrupt_enable(cookie); 2583 return; 2584 } 2585 2586 for (i = 0; i < state->dts_nspeculations; i++) { 2587 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2588 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2589 2590 if (src->dtb_tomax == NULL) 2591 continue; 2592 2593 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2594 src->dtb_offset = 0; 2595 continue; 2596 } 2597 2598 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2599 continue; 2600 2601 if (src->dtb_offset == 0) 2602 continue; 2603 2604 dtrace_speculation_commit(state, cpu, i + 1); 2605 } 2606 2607 dtrace_interrupt_enable(cookie); 2608 } 2609 2610 /* 2611 * Note: not called from probe context. This function is called 2612 * asynchronously (and at a regular interval) to clean any speculations that 2613 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2614 * is work to be done, it cross calls all CPUs to perform that work; 2615 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2616 * INACTIVE state until they have been cleaned by all CPUs. 2617 */ 2618 static void 2619 dtrace_speculation_clean(dtrace_state_t *state) 2620 { 2621 int work = 0, rv; 2622 dtrace_specid_t i; 2623 2624 for (i = 0; i < state->dts_nspeculations; i++) { 2625 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2626 2627 ASSERT(!spec->dtsp_cleaning); 2628 2629 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2630 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2631 continue; 2632 2633 work++; 2634 spec->dtsp_cleaning = 1; 2635 } 2636 2637 if (!work) 2638 return; 2639 2640 dtrace_xcall(DTRACE_CPUALL, 2641 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2642 2643 /* 2644 * We now know that all CPUs have committed or discarded their 2645 * speculation buffers, as appropriate. We can now set the state 2646 * to inactive. 2647 */ 2648 for (i = 0; i < state->dts_nspeculations; i++) { 2649 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2650 dtrace_speculation_state_t current, new; 2651 2652 if (!spec->dtsp_cleaning) 2653 continue; 2654 2655 current = spec->dtsp_state; 2656 ASSERT(current == DTRACESPEC_DISCARDING || 2657 current == DTRACESPEC_COMMITTINGMANY); 2658 2659 new = DTRACESPEC_INACTIVE; 2660 2661 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2662 ASSERT(rv == current); 2663 spec->dtsp_cleaning = 0; 2664 } 2665 } 2666 2667 /* 2668 * Called as part of a speculate() to get the speculative buffer associated 2669 * with a given speculation. Returns NULL if the specified speculation is not 2670 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2671 * the active CPU is not the specified CPU -- the speculation will be 2672 * atomically transitioned into the ACTIVEMANY state. 2673 */ 2674 static dtrace_buffer_t * 2675 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2676 dtrace_specid_t which) 2677 { 2678 dtrace_speculation_t *spec; 2679 dtrace_speculation_state_t current, new = 0; 2680 dtrace_buffer_t *buf; 2681 2682 if (which == 0) 2683 return (NULL); 2684 2685 if (which > state->dts_nspeculations) { 2686 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2687 return (NULL); 2688 } 2689 2690 spec = &state->dts_speculations[which - 1]; 2691 buf = &spec->dtsp_buffer[cpuid]; 2692 2693 do { 2694 current = spec->dtsp_state; 2695 2696 switch (current) { 2697 case DTRACESPEC_INACTIVE: 2698 case DTRACESPEC_COMMITTINGMANY: 2699 case DTRACESPEC_DISCARDING: 2700 return (NULL); 2701 2702 case DTRACESPEC_COMMITTING: 2703 ASSERT(buf->dtb_offset == 0); 2704 return (NULL); 2705 2706 case DTRACESPEC_ACTIVEONE: 2707 /* 2708 * This speculation is currently active on one CPU. 2709 * Check the offset in the buffer; if it's non-zero, 2710 * that CPU must be us (and we leave the state alone). 2711 * If it's zero, assume that we're starting on a new 2712 * CPU -- and change the state to indicate that the 2713 * speculation is active on more than one CPU. 2714 */ 2715 if (buf->dtb_offset != 0) 2716 return (buf); 2717 2718 new = DTRACESPEC_ACTIVEMANY; 2719 break; 2720 2721 case DTRACESPEC_ACTIVEMANY: 2722 return (buf); 2723 2724 case DTRACESPEC_ACTIVE: 2725 new = DTRACESPEC_ACTIVEONE; 2726 break; 2727 2728 default: 2729 ASSERT(0); 2730 } 2731 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2732 current, new) != current); 2733 2734 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2735 return (buf); 2736 } 2737 2738 /* 2739 * Return a string. In the event that the user lacks the privilege to access 2740 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2741 * don't fail access checking. 2742 * 2743 * dtrace_dif_variable() uses this routine as a helper for various 2744 * builtin values such as 'execname' and 'probefunc.' 2745 */ 2746 uintptr_t 2747 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 2748 dtrace_mstate_t *mstate) 2749 { 2750 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2751 uintptr_t ret; 2752 size_t strsz; 2753 2754 /* 2755 * The easy case: this probe is allowed to read all of memory, so 2756 * we can just return this as a vanilla pointer. 2757 */ 2758 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 2759 return (addr); 2760 2761 /* 2762 * This is the tougher case: we copy the string in question from 2763 * kernel memory into scratch memory and return it that way: this 2764 * ensures that we won't trip up when access checking tests the 2765 * BYREF return value. 2766 */ 2767 strsz = dtrace_strlen((char *)addr, size) + 1; 2768 2769 if (mstate->dtms_scratch_ptr + strsz > 2770 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2771 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2772 return (0); 2773 } 2774 2775 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2776 strsz); 2777 ret = mstate->dtms_scratch_ptr; 2778 mstate->dtms_scratch_ptr += strsz; 2779 return (ret); 2780 } 2781 2782 /* 2783 * Return a string from a memoy address which is known to have one or 2784 * more concatenated, individually zero terminated, sub-strings. 2785 * In the event that the user lacks the privilege to access 2786 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2787 * don't fail access checking. 2788 * 2789 * dtrace_dif_variable() uses this routine as a helper for various 2790 * builtin values such as 'execargs'. 2791 */ 2792 static uintptr_t 2793 dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 2794 dtrace_mstate_t *mstate) 2795 { 2796 char *p; 2797 size_t i; 2798 uintptr_t ret; 2799 2800 if (mstate->dtms_scratch_ptr + strsz > 2801 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2802 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2803 return (0); 2804 } 2805 2806 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2807 strsz); 2808 2809 /* Replace sub-string termination characters with a space. */ 2810 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 2811 p++, i++) 2812 if (*p == '\0') 2813 *p = ' '; 2814 2815 ret = mstate->dtms_scratch_ptr; 2816 mstate->dtms_scratch_ptr += strsz; 2817 return (ret); 2818 } 2819 2820 /* 2821 * This function implements the DIF emulator's variable lookups. The emulator 2822 * passes a reserved variable identifier and optional built-in array index. 2823 */ 2824 static uint64_t 2825 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2826 uint64_t ndx) 2827 { 2828 /* 2829 * If we're accessing one of the uncached arguments, we'll turn this 2830 * into a reference in the args array. 2831 */ 2832 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2833 ndx = v - DIF_VAR_ARG0; 2834 v = DIF_VAR_ARGS; 2835 } 2836 2837 switch (v) { 2838 case DIF_VAR_ARGS: 2839 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2840 if (ndx >= sizeof (mstate->dtms_arg) / 2841 sizeof (mstate->dtms_arg[0])) { 2842 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2843 dtrace_provider_t *pv; 2844 uint64_t val; 2845 2846 pv = mstate->dtms_probe->dtpr_provider; 2847 if (pv->dtpv_pops.dtps_getargval != NULL) 2848 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2849 mstate->dtms_probe->dtpr_id, 2850 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2851 else 2852 val = dtrace_getarg(ndx, aframes); 2853 2854 /* 2855 * This is regrettably required to keep the compiler 2856 * from tail-optimizing the call to dtrace_getarg(). 2857 * The condition always evaluates to true, but the 2858 * compiler has no way of figuring that out a priori. 2859 * (None of this would be necessary if the compiler 2860 * could be relied upon to _always_ tail-optimize 2861 * the call to dtrace_getarg() -- but it can't.) 2862 */ 2863 if (mstate->dtms_probe != NULL) 2864 return (val); 2865 2866 ASSERT(0); 2867 } 2868 2869 return (mstate->dtms_arg[ndx]); 2870 2871 #if defined(sun) 2872 case DIF_VAR_UREGS: { 2873 klwp_t *lwp; 2874 2875 if (!dtrace_priv_proc(state)) 2876 return (0); 2877 2878 if ((lwp = curthread->t_lwp) == NULL) { 2879 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2880 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 2881 return (0); 2882 } 2883 2884 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2885 return (0); 2886 } 2887 #else 2888 case DIF_VAR_UREGS: { 2889 struct trapframe *tframe; 2890 2891 if (!dtrace_priv_proc(state)) 2892 return (0); 2893 2894 if ((tframe = curthread->td_frame) == NULL) { 2895 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2896 cpu_core[curcpu].cpuc_dtrace_illval = 0; 2897 return (0); 2898 } 2899 2900 return (dtrace_getreg(tframe, ndx)); 2901 } 2902 #endif 2903 2904 case DIF_VAR_CURTHREAD: 2905 if (!dtrace_priv_kernel(state)) 2906 return (0); 2907 return ((uint64_t)(uintptr_t)curthread); 2908 2909 case DIF_VAR_TIMESTAMP: 2910 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2911 mstate->dtms_timestamp = dtrace_gethrtime(); 2912 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2913 } 2914 return (mstate->dtms_timestamp); 2915 2916 case DIF_VAR_VTIMESTAMP: 2917 ASSERT(dtrace_vtime_references != 0); 2918 return (curthread->t_dtrace_vtime); 2919 2920 case DIF_VAR_WALLTIMESTAMP: 2921 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2922 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2923 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2924 } 2925 return (mstate->dtms_walltimestamp); 2926 2927 #if defined(sun) 2928 case DIF_VAR_IPL: 2929 if (!dtrace_priv_kernel(state)) 2930 return (0); 2931 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2932 mstate->dtms_ipl = dtrace_getipl(); 2933 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2934 } 2935 return (mstate->dtms_ipl); 2936 #endif 2937 2938 case DIF_VAR_EPID: 2939 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2940 return (mstate->dtms_epid); 2941 2942 case DIF_VAR_ID: 2943 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2944 return (mstate->dtms_probe->dtpr_id); 2945 2946 case DIF_VAR_STACKDEPTH: 2947 if (!dtrace_priv_kernel(state)) 2948 return (0); 2949 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2950 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2951 2952 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2953 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2954 } 2955 return (mstate->dtms_stackdepth); 2956 2957 case DIF_VAR_USTACKDEPTH: 2958 if (!dtrace_priv_proc(state)) 2959 return (0); 2960 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2961 /* 2962 * See comment in DIF_VAR_PID. 2963 */ 2964 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2965 CPU_ON_INTR(CPU)) { 2966 mstate->dtms_ustackdepth = 0; 2967 } else { 2968 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2969 mstate->dtms_ustackdepth = 2970 dtrace_getustackdepth(); 2971 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2972 } 2973 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2974 } 2975 return (mstate->dtms_ustackdepth); 2976 2977 case DIF_VAR_CALLER: 2978 if (!dtrace_priv_kernel(state)) 2979 return (0); 2980 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2981 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2982 2983 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2984 /* 2985 * If this is an unanchored probe, we are 2986 * required to go through the slow path: 2987 * dtrace_caller() only guarantees correct 2988 * results for anchored probes. 2989 */ 2990 pc_t caller[2] = {0, 0}; 2991 2992 dtrace_getpcstack(caller, 2, aframes, 2993 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2994 mstate->dtms_caller = caller[1]; 2995 } else if ((mstate->dtms_caller = 2996 dtrace_caller(aframes)) == -1) { 2997 /* 2998 * We have failed to do this the quick way; 2999 * we must resort to the slower approach of 3000 * calling dtrace_getpcstack(). 3001 */ 3002 pc_t caller = 0; 3003 3004 dtrace_getpcstack(&caller, 1, aframes, NULL); 3005 mstate->dtms_caller = caller; 3006 } 3007 3008 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3009 } 3010 return (mstate->dtms_caller); 3011 3012 case DIF_VAR_UCALLER: 3013 if (!dtrace_priv_proc(state)) 3014 return (0); 3015 3016 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3017 uint64_t ustack[3]; 3018 3019 /* 3020 * dtrace_getupcstack() fills in the first uint64_t 3021 * with the current PID. The second uint64_t will 3022 * be the program counter at user-level. The third 3023 * uint64_t will contain the caller, which is what 3024 * we're after. 3025 */ 3026 ustack[2] = 0; 3027 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3028 dtrace_getupcstack(ustack, 3); 3029 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3030 mstate->dtms_ucaller = ustack[2]; 3031 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3032 } 3033 3034 return (mstate->dtms_ucaller); 3035 3036 case DIF_VAR_PROBEPROV: 3037 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3038 return (dtrace_dif_varstr( 3039 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3040 state, mstate)); 3041 3042 case DIF_VAR_PROBEMOD: 3043 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3044 return (dtrace_dif_varstr( 3045 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3046 state, mstate)); 3047 3048 case DIF_VAR_PROBEFUNC: 3049 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3050 return (dtrace_dif_varstr( 3051 (uintptr_t)mstate->dtms_probe->dtpr_func, 3052 state, mstate)); 3053 3054 case DIF_VAR_PROBENAME: 3055 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3056 return (dtrace_dif_varstr( 3057 (uintptr_t)mstate->dtms_probe->dtpr_name, 3058 state, mstate)); 3059 3060 case DIF_VAR_PID: 3061 if (!dtrace_priv_proc(state)) 3062 return (0); 3063 3064 #if defined(sun) 3065 /* 3066 * Note that we are assuming that an unanchored probe is 3067 * always due to a high-level interrupt. (And we're assuming 3068 * that there is only a single high level interrupt.) 3069 */ 3070 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3071 return (pid0.pid_id); 3072 3073 /* 3074 * It is always safe to dereference one's own t_procp pointer: 3075 * it always points to a valid, allocated proc structure. 3076 * Further, it is always safe to dereference the p_pidp member 3077 * of one's own proc structure. (These are truisms becuase 3078 * threads and processes don't clean up their own state -- 3079 * they leave that task to whomever reaps them.) 3080 */ 3081 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3082 #else 3083 return ((uint64_t)curproc->p_pid); 3084 #endif 3085 3086 case DIF_VAR_PPID: 3087 if (!dtrace_priv_proc(state)) 3088 return (0); 3089 3090 #if defined(sun) 3091 /* 3092 * See comment in DIF_VAR_PID. 3093 */ 3094 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3095 return (pid0.pid_id); 3096 3097 /* 3098 * It is always safe to dereference one's own t_procp pointer: 3099 * it always points to a valid, allocated proc structure. 3100 * (This is true because threads don't clean up their own 3101 * state -- they leave that task to whomever reaps them.) 3102 */ 3103 return ((uint64_t)curthread->t_procp->p_ppid); 3104 #else 3105 return ((uint64_t)curproc->p_pptr->p_pid); 3106 #endif 3107 3108 case DIF_VAR_TID: 3109 #if defined(sun) 3110 /* 3111 * See comment in DIF_VAR_PID. 3112 */ 3113 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3114 return (0); 3115 #endif 3116 3117 return ((uint64_t)curthread->t_tid); 3118 3119 case DIF_VAR_EXECARGS: { 3120 struct pargs *p_args = curthread->td_proc->p_args; 3121 3122 if (p_args == NULL) 3123 return(0); 3124 3125 return (dtrace_dif_varstrz( 3126 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3127 } 3128 3129 case DIF_VAR_EXECNAME: 3130 #if defined(sun) 3131 if (!dtrace_priv_proc(state)) 3132 return (0); 3133 3134 /* 3135 * See comment in DIF_VAR_PID. 3136 */ 3137 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3138 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3139 3140 /* 3141 * It is always safe to dereference one's own t_procp pointer: 3142 * it always points to a valid, allocated proc structure. 3143 * (This is true because threads don't clean up their own 3144 * state -- they leave that task to whomever reaps them.) 3145 */ 3146 return (dtrace_dif_varstr( 3147 (uintptr_t)curthread->t_procp->p_user.u_comm, 3148 state, mstate)); 3149 #else 3150 return (dtrace_dif_varstr( 3151 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3152 #endif 3153 3154 case DIF_VAR_ZONENAME: 3155 #if defined(sun) 3156 if (!dtrace_priv_proc(state)) 3157 return (0); 3158 3159 /* 3160 * See comment in DIF_VAR_PID. 3161 */ 3162 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3163 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3164 3165 /* 3166 * It is always safe to dereference one's own t_procp pointer: 3167 * it always points to a valid, allocated proc structure. 3168 * (This is true because threads don't clean up their own 3169 * state -- they leave that task to whomever reaps them.) 3170 */ 3171 return (dtrace_dif_varstr( 3172 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3173 state, mstate)); 3174 #else 3175 return (0); 3176 #endif 3177 3178 case DIF_VAR_UID: 3179 if (!dtrace_priv_proc(state)) 3180 return (0); 3181 3182 #if defined(sun) 3183 /* 3184 * See comment in DIF_VAR_PID. 3185 */ 3186 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3187 return ((uint64_t)p0.p_cred->cr_uid); 3188 #endif 3189 3190 /* 3191 * It is always safe to dereference one's own t_procp pointer: 3192 * it always points to a valid, allocated proc structure. 3193 * (This is true because threads don't clean up their own 3194 * state -- they leave that task to whomever reaps them.) 3195 * 3196 * Additionally, it is safe to dereference one's own process 3197 * credential, since this is never NULL after process birth. 3198 */ 3199 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3200 3201 case DIF_VAR_GID: 3202 if (!dtrace_priv_proc(state)) 3203 return (0); 3204 3205 #if defined(sun) 3206 /* 3207 * See comment in DIF_VAR_PID. 3208 */ 3209 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3210 return ((uint64_t)p0.p_cred->cr_gid); 3211 #endif 3212 3213 /* 3214 * It is always safe to dereference one's own t_procp pointer: 3215 * it always points to a valid, allocated proc structure. 3216 * (This is true because threads don't clean up their own 3217 * state -- they leave that task to whomever reaps them.) 3218 * 3219 * Additionally, it is safe to dereference one's own process 3220 * credential, since this is never NULL after process birth. 3221 */ 3222 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3223 3224 case DIF_VAR_ERRNO: { 3225 #if defined(sun) 3226 klwp_t *lwp; 3227 if (!dtrace_priv_proc(state)) 3228 return (0); 3229 3230 /* 3231 * See comment in DIF_VAR_PID. 3232 */ 3233 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3234 return (0); 3235 3236 /* 3237 * It is always safe to dereference one's own t_lwp pointer in 3238 * the event that this pointer is non-NULL. (This is true 3239 * because threads and lwps don't clean up their own state -- 3240 * they leave that task to whomever reaps them.) 3241 */ 3242 if ((lwp = curthread->t_lwp) == NULL) 3243 return (0); 3244 3245 return ((uint64_t)lwp->lwp_errno); 3246 #else 3247 return (curthread->td_errno); 3248 #endif 3249 } 3250 #if !defined(sun) 3251 case DIF_VAR_CPU: { 3252 return curcpu; 3253 } 3254 #endif 3255 default: 3256 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3257 return (0); 3258 } 3259 } 3260 3261 /* 3262 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 3263 * Notice that we don't bother validating the proper number of arguments or 3264 * their types in the tuple stack. This isn't needed because all argument 3265 * interpretation is safe because of our load safety -- the worst that can 3266 * happen is that a bogus program can obtain bogus results. 3267 */ 3268 static void 3269 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 3270 dtrace_key_t *tupregs, int nargs, 3271 dtrace_mstate_t *mstate, dtrace_state_t *state) 3272 { 3273 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 3274 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 3275 dtrace_vstate_t *vstate = &state->dts_vstate; 3276 3277 #if defined(sun) 3278 union { 3279 mutex_impl_t mi; 3280 uint64_t mx; 3281 } m; 3282 3283 union { 3284 krwlock_t ri; 3285 uintptr_t rw; 3286 } r; 3287 #else 3288 struct thread *lowner; 3289 union { 3290 struct lock_object *li; 3291 uintptr_t lx; 3292 } l; 3293 #endif 3294 3295 switch (subr) { 3296 case DIF_SUBR_RAND: 3297 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 3298 break; 3299 3300 #if defined(sun) 3301 case DIF_SUBR_MUTEX_OWNED: 3302 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3303 mstate, vstate)) { 3304 regs[rd] = 0; 3305 break; 3306 } 3307 3308 m.mx = dtrace_load64(tupregs[0].dttk_value); 3309 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3310 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3311 else 3312 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 3313 break; 3314 3315 case DIF_SUBR_MUTEX_OWNER: 3316 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3317 mstate, vstate)) { 3318 regs[rd] = 0; 3319 break; 3320 } 3321 3322 m.mx = dtrace_load64(tupregs[0].dttk_value); 3323 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3324 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3325 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3326 else 3327 regs[rd] = 0; 3328 break; 3329 3330 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3331 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3332 mstate, vstate)) { 3333 regs[rd] = 0; 3334 break; 3335 } 3336 3337 m.mx = dtrace_load64(tupregs[0].dttk_value); 3338 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3339 break; 3340 3341 case DIF_SUBR_MUTEX_TYPE_SPIN: 3342 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3343 mstate, vstate)) { 3344 regs[rd] = 0; 3345 break; 3346 } 3347 3348 m.mx = dtrace_load64(tupregs[0].dttk_value); 3349 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3350 break; 3351 3352 case DIF_SUBR_RW_READ_HELD: { 3353 uintptr_t tmp; 3354 3355 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3356 mstate, vstate)) { 3357 regs[rd] = 0; 3358 break; 3359 } 3360 3361 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3362 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 3363 break; 3364 } 3365 3366 case DIF_SUBR_RW_WRITE_HELD: 3367 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3368 mstate, vstate)) { 3369 regs[rd] = 0; 3370 break; 3371 } 3372 3373 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3374 regs[rd] = _RW_WRITE_HELD(&r.ri); 3375 break; 3376 3377 case DIF_SUBR_RW_ISWRITER: 3378 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3379 mstate, vstate)) { 3380 regs[rd] = 0; 3381 break; 3382 } 3383 3384 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3385 regs[rd] = _RW_ISWRITER(&r.ri); 3386 break; 3387 3388 #else 3389 case DIF_SUBR_MUTEX_OWNED: 3390 if (!dtrace_canload(tupregs[0].dttk_value, 3391 sizeof (struct lock_object), mstate, vstate)) { 3392 regs[rd] = 0; 3393 break; 3394 } 3395 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3396 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 3397 break; 3398 3399 case DIF_SUBR_MUTEX_OWNER: 3400 if (!dtrace_canload(tupregs[0].dttk_value, 3401 sizeof (struct lock_object), mstate, vstate)) { 3402 regs[rd] = 0; 3403 break; 3404 } 3405 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3406 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 3407 regs[rd] = (uintptr_t)lowner; 3408 break; 3409 3410 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3411 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 3412 mstate, vstate)) { 3413 regs[rd] = 0; 3414 break; 3415 } 3416 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3417 /* XXX - should be only LC_SLEEPABLE? */ 3418 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & 3419 (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0; 3420 break; 3421 3422 case DIF_SUBR_MUTEX_TYPE_SPIN: 3423 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 3424 mstate, vstate)) { 3425 regs[rd] = 0; 3426 break; 3427 } 3428 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3429 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 3430 break; 3431 3432 case DIF_SUBR_RW_READ_HELD: 3433 case DIF_SUBR_SX_SHARED_HELD: 3434 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3435 mstate, vstate)) { 3436 regs[rd] = 0; 3437 break; 3438 } 3439 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3440 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 3441 lowner == NULL; 3442 break; 3443 3444 case DIF_SUBR_RW_WRITE_HELD: 3445 case DIF_SUBR_SX_EXCLUSIVE_HELD: 3446 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3447 mstate, vstate)) { 3448 regs[rd] = 0; 3449 break; 3450 } 3451 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 3452 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 3453 regs[rd] = (lowner == curthread); 3454 break; 3455 3456 case DIF_SUBR_RW_ISWRITER: 3457 case DIF_SUBR_SX_ISEXCLUSIVE: 3458 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3459 mstate, vstate)) { 3460 regs[rd] = 0; 3461 break; 3462 } 3463 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 3464 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 3465 lowner != NULL; 3466 break; 3467 #endif /* ! defined(sun) */ 3468 3469 case DIF_SUBR_BCOPY: { 3470 /* 3471 * We need to be sure that the destination is in the scratch 3472 * region -- no other region is allowed. 3473 */ 3474 uintptr_t src = tupregs[0].dttk_value; 3475 uintptr_t dest = tupregs[1].dttk_value; 3476 size_t size = tupregs[2].dttk_value; 3477 3478 if (!dtrace_inscratch(dest, size, mstate)) { 3479 *flags |= CPU_DTRACE_BADADDR; 3480 *illval = regs[rd]; 3481 break; 3482 } 3483 3484 if (!dtrace_canload(src, size, mstate, vstate)) { 3485 regs[rd] = 0; 3486 break; 3487 } 3488 3489 dtrace_bcopy((void *)src, (void *)dest, size); 3490 break; 3491 } 3492 3493 case DIF_SUBR_ALLOCA: 3494 case DIF_SUBR_COPYIN: { 3495 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 3496 uint64_t size = 3497 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 3498 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 3499 3500 /* 3501 * This action doesn't require any credential checks since 3502 * probes will not activate in user contexts to which the 3503 * enabling user does not have permissions. 3504 */ 3505 3506 /* 3507 * Rounding up the user allocation size could have overflowed 3508 * a large, bogus allocation (like -1ULL) to 0. 3509 */ 3510 if (scratch_size < size || 3511 !DTRACE_INSCRATCH(mstate, scratch_size)) { 3512 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3513 regs[rd] = 0; 3514 break; 3515 } 3516 3517 if (subr == DIF_SUBR_COPYIN) { 3518 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3519 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3520 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3521 } 3522 3523 mstate->dtms_scratch_ptr += scratch_size; 3524 regs[rd] = dest; 3525 break; 3526 } 3527 3528 case DIF_SUBR_COPYINTO: { 3529 uint64_t size = tupregs[1].dttk_value; 3530 uintptr_t dest = tupregs[2].dttk_value; 3531 3532 /* 3533 * This action doesn't require any credential checks since 3534 * probes will not activate in user contexts to which the 3535 * enabling user does not have permissions. 3536 */ 3537 if (!dtrace_inscratch(dest, size, mstate)) { 3538 *flags |= CPU_DTRACE_BADADDR; 3539 *illval = regs[rd]; 3540 break; 3541 } 3542 3543 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3544 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3545 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3546 break; 3547 } 3548 3549 case DIF_SUBR_COPYINSTR: { 3550 uintptr_t dest = mstate->dtms_scratch_ptr; 3551 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3552 3553 if (nargs > 1 && tupregs[1].dttk_value < size) 3554 size = tupregs[1].dttk_value + 1; 3555 3556 /* 3557 * This action doesn't require any credential checks since 3558 * probes will not activate in user contexts to which the 3559 * enabling user does not have permissions. 3560 */ 3561 if (!DTRACE_INSCRATCH(mstate, size)) { 3562 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3563 regs[rd] = 0; 3564 break; 3565 } 3566 3567 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3568 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 3569 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3570 3571 ((char *)dest)[size - 1] = '\0'; 3572 mstate->dtms_scratch_ptr += size; 3573 regs[rd] = dest; 3574 break; 3575 } 3576 3577 #if defined(sun) 3578 case DIF_SUBR_MSGSIZE: 3579 case DIF_SUBR_MSGDSIZE: { 3580 uintptr_t baddr = tupregs[0].dttk_value, daddr; 3581 uintptr_t wptr, rptr; 3582 size_t count = 0; 3583 int cont = 0; 3584 3585 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 3586 3587 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 3588 vstate)) { 3589 regs[rd] = 0; 3590 break; 3591 } 3592 3593 wptr = dtrace_loadptr(baddr + 3594 offsetof(mblk_t, b_wptr)); 3595 3596 rptr = dtrace_loadptr(baddr + 3597 offsetof(mblk_t, b_rptr)); 3598 3599 if (wptr < rptr) { 3600 *flags |= CPU_DTRACE_BADADDR; 3601 *illval = tupregs[0].dttk_value; 3602 break; 3603 } 3604 3605 daddr = dtrace_loadptr(baddr + 3606 offsetof(mblk_t, b_datap)); 3607 3608 baddr = dtrace_loadptr(baddr + 3609 offsetof(mblk_t, b_cont)); 3610 3611 /* 3612 * We want to prevent against denial-of-service here, 3613 * so we're only going to search the list for 3614 * dtrace_msgdsize_max mblks. 3615 */ 3616 if (cont++ > dtrace_msgdsize_max) { 3617 *flags |= CPU_DTRACE_ILLOP; 3618 break; 3619 } 3620 3621 if (subr == DIF_SUBR_MSGDSIZE) { 3622 if (dtrace_load8(daddr + 3623 offsetof(dblk_t, db_type)) != M_DATA) 3624 continue; 3625 } 3626 3627 count += wptr - rptr; 3628 } 3629 3630 if (!(*flags & CPU_DTRACE_FAULT)) 3631 regs[rd] = count; 3632 3633 break; 3634 } 3635 #endif 3636 3637 case DIF_SUBR_PROGENYOF: { 3638 pid_t pid = tupregs[0].dttk_value; 3639 proc_t *p; 3640 int rval = 0; 3641 3642 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3643 3644 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 3645 #if defined(sun) 3646 if (p->p_pidp->pid_id == pid) { 3647 #else 3648 if (p->p_pid == pid) { 3649 #endif 3650 rval = 1; 3651 break; 3652 } 3653 } 3654 3655 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3656 3657 regs[rd] = rval; 3658 break; 3659 } 3660 3661 case DIF_SUBR_SPECULATION: 3662 regs[rd] = dtrace_speculation(state); 3663 break; 3664 3665 case DIF_SUBR_COPYOUT: { 3666 uintptr_t kaddr = tupregs[0].dttk_value; 3667 uintptr_t uaddr = tupregs[1].dttk_value; 3668 uint64_t size = tupregs[2].dttk_value; 3669 3670 if (!dtrace_destructive_disallow && 3671 dtrace_priv_proc_control(state) && 3672 !dtrace_istoxic(kaddr, size)) { 3673 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3674 dtrace_copyout(kaddr, uaddr, size, flags); 3675 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3676 } 3677 break; 3678 } 3679 3680 case DIF_SUBR_COPYOUTSTR: { 3681 uintptr_t kaddr = tupregs[0].dttk_value; 3682 uintptr_t uaddr = tupregs[1].dttk_value; 3683 uint64_t size = tupregs[2].dttk_value; 3684 3685 if (!dtrace_destructive_disallow && 3686 dtrace_priv_proc_control(state) && 3687 !dtrace_istoxic(kaddr, size)) { 3688 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3689 dtrace_copyoutstr(kaddr, uaddr, size, flags); 3690 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3691 } 3692 break; 3693 } 3694 3695 case DIF_SUBR_STRLEN: { 3696 size_t sz; 3697 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 3698 sz = dtrace_strlen((char *)addr, 3699 state->dts_options[DTRACEOPT_STRSIZE]); 3700 3701 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 3702 regs[rd] = 0; 3703 break; 3704 } 3705 3706 regs[rd] = sz; 3707 3708 break; 3709 } 3710 3711 case DIF_SUBR_STRCHR: 3712 case DIF_SUBR_STRRCHR: { 3713 /* 3714 * We're going to iterate over the string looking for the 3715 * specified character. We will iterate until we have reached 3716 * the string length or we have found the character. If this 3717 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 3718 * of the specified character instead of the first. 3719 */ 3720 uintptr_t saddr = tupregs[0].dttk_value; 3721 uintptr_t addr = tupregs[0].dttk_value; 3722 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 3723 char c, target = (char)tupregs[1].dttk_value; 3724 3725 for (regs[rd] = 0; addr < limit; addr++) { 3726 if ((c = dtrace_load8(addr)) == target) { 3727 regs[rd] = addr; 3728 3729 if (subr == DIF_SUBR_STRCHR) 3730 break; 3731 } 3732 3733 if (c == '\0') 3734 break; 3735 } 3736 3737 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 3738 regs[rd] = 0; 3739 break; 3740 } 3741 3742 break; 3743 } 3744 3745 case DIF_SUBR_STRSTR: 3746 case DIF_SUBR_INDEX: 3747 case DIF_SUBR_RINDEX: { 3748 /* 3749 * We're going to iterate over the string looking for the 3750 * specified string. We will iterate until we have reached 3751 * the string length or we have found the string. (Yes, this 3752 * is done in the most naive way possible -- but considering 3753 * that the string we're searching for is likely to be 3754 * relatively short, the complexity of Rabin-Karp or similar 3755 * hardly seems merited.) 3756 */ 3757 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 3758 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 3759 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3760 size_t len = dtrace_strlen(addr, size); 3761 size_t sublen = dtrace_strlen(substr, size); 3762 char *limit = addr + len, *orig = addr; 3763 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 3764 int inc = 1; 3765 3766 regs[rd] = notfound; 3767 3768 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 3769 regs[rd] = 0; 3770 break; 3771 } 3772 3773 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 3774 vstate)) { 3775 regs[rd] = 0; 3776 break; 3777 } 3778 3779 /* 3780 * strstr() and index()/rindex() have similar semantics if 3781 * both strings are the empty string: strstr() returns a 3782 * pointer to the (empty) string, and index() and rindex() 3783 * both return index 0 (regardless of any position argument). 3784 */ 3785 if (sublen == 0 && len == 0) { 3786 if (subr == DIF_SUBR_STRSTR) 3787 regs[rd] = (uintptr_t)addr; 3788 else 3789 regs[rd] = 0; 3790 break; 3791 } 3792 3793 if (subr != DIF_SUBR_STRSTR) { 3794 if (subr == DIF_SUBR_RINDEX) { 3795 limit = orig - 1; 3796 addr += len; 3797 inc = -1; 3798 } 3799 3800 /* 3801 * Both index() and rindex() take an optional position 3802 * argument that denotes the starting position. 3803 */ 3804 if (nargs == 3) { 3805 int64_t pos = (int64_t)tupregs[2].dttk_value; 3806 3807 /* 3808 * If the position argument to index() is 3809 * negative, Perl implicitly clamps it at 3810 * zero. This semantic is a little surprising 3811 * given the special meaning of negative 3812 * positions to similar Perl functions like 3813 * substr(), but it appears to reflect a 3814 * notion that index() can start from a 3815 * negative index and increment its way up to 3816 * the string. Given this notion, Perl's 3817 * rindex() is at least self-consistent in 3818 * that it implicitly clamps positions greater 3819 * than the string length to be the string 3820 * length. Where Perl completely loses 3821 * coherence, however, is when the specified 3822 * substring is the empty string (""). In 3823 * this case, even if the position is 3824 * negative, rindex() returns 0 -- and even if 3825 * the position is greater than the length, 3826 * index() returns the string length. These 3827 * semantics violate the notion that index() 3828 * should never return a value less than the 3829 * specified position and that rindex() should 3830 * never return a value greater than the 3831 * specified position. (One assumes that 3832 * these semantics are artifacts of Perl's 3833 * implementation and not the results of 3834 * deliberate design -- it beggars belief that 3835 * even Larry Wall could desire such oddness.) 3836 * While in the abstract one would wish for 3837 * consistent position semantics across 3838 * substr(), index() and rindex() -- or at the 3839 * very least self-consistent position 3840 * semantics for index() and rindex() -- we 3841 * instead opt to keep with the extant Perl 3842 * semantics, in all their broken glory. (Do 3843 * we have more desire to maintain Perl's 3844 * semantics than Perl does? Probably.) 3845 */ 3846 if (subr == DIF_SUBR_RINDEX) { 3847 if (pos < 0) { 3848 if (sublen == 0) 3849 regs[rd] = 0; 3850 break; 3851 } 3852 3853 if (pos > len) 3854 pos = len; 3855 } else { 3856 if (pos < 0) 3857 pos = 0; 3858 3859 if (pos >= len) { 3860 if (sublen == 0) 3861 regs[rd] = len; 3862 break; 3863 } 3864 } 3865 3866 addr = orig + pos; 3867 } 3868 } 3869 3870 for (regs[rd] = notfound; addr != limit; addr += inc) { 3871 if (dtrace_strncmp(addr, substr, sublen) == 0) { 3872 if (subr != DIF_SUBR_STRSTR) { 3873 /* 3874 * As D index() and rindex() are 3875 * modeled on Perl (and not on awk), 3876 * we return a zero-based (and not a 3877 * one-based) index. (For you Perl 3878 * weenies: no, we're not going to add 3879 * $[ -- and shouldn't you be at a con 3880 * or something?) 3881 */ 3882 regs[rd] = (uintptr_t)(addr - orig); 3883 break; 3884 } 3885 3886 ASSERT(subr == DIF_SUBR_STRSTR); 3887 regs[rd] = (uintptr_t)addr; 3888 break; 3889 } 3890 } 3891 3892 break; 3893 } 3894 3895 case DIF_SUBR_STRTOK: { 3896 uintptr_t addr = tupregs[0].dttk_value; 3897 uintptr_t tokaddr = tupregs[1].dttk_value; 3898 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3899 uintptr_t limit, toklimit = tokaddr + size; 3900 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 3901 char *dest = (char *)mstate->dtms_scratch_ptr; 3902 int i; 3903 3904 /* 3905 * Check both the token buffer and (later) the input buffer, 3906 * since both could be non-scratch addresses. 3907 */ 3908 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 3909 regs[rd] = 0; 3910 break; 3911 } 3912 3913 if (!DTRACE_INSCRATCH(mstate, size)) { 3914 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3915 regs[rd] = 0; 3916 break; 3917 } 3918 3919 if (addr == 0) { 3920 /* 3921 * If the address specified is NULL, we use our saved 3922 * strtok pointer from the mstate. Note that this 3923 * means that the saved strtok pointer is _only_ 3924 * valid within multiple enablings of the same probe -- 3925 * it behaves like an implicit clause-local variable. 3926 */ 3927 addr = mstate->dtms_strtok; 3928 } else { 3929 /* 3930 * If the user-specified address is non-NULL we must 3931 * access check it. This is the only time we have 3932 * a chance to do so, since this address may reside 3933 * in the string table of this clause-- future calls 3934 * (when we fetch addr from mstate->dtms_strtok) 3935 * would fail this access check. 3936 */ 3937 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 3938 regs[rd] = 0; 3939 break; 3940 } 3941 } 3942 3943 /* 3944 * First, zero the token map, and then process the token 3945 * string -- setting a bit in the map for every character 3946 * found in the token string. 3947 */ 3948 for (i = 0; i < sizeof (tokmap); i++) 3949 tokmap[i] = 0; 3950 3951 for (; tokaddr < toklimit; tokaddr++) { 3952 if ((c = dtrace_load8(tokaddr)) == '\0') 3953 break; 3954 3955 ASSERT((c >> 3) < sizeof (tokmap)); 3956 tokmap[c >> 3] |= (1 << (c & 0x7)); 3957 } 3958 3959 for (limit = addr + size; addr < limit; addr++) { 3960 /* 3961 * We're looking for a character that is _not_ contained 3962 * in the token string. 3963 */ 3964 if ((c = dtrace_load8(addr)) == '\0') 3965 break; 3966 3967 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3968 break; 3969 } 3970 3971 if (c == '\0') { 3972 /* 3973 * We reached the end of the string without finding 3974 * any character that was not in the token string. 3975 * We return NULL in this case, and we set the saved 3976 * address to NULL as well. 3977 */ 3978 regs[rd] = 0; 3979 mstate->dtms_strtok = 0; 3980 break; 3981 } 3982 3983 /* 3984 * From here on, we're copying into the destination string. 3985 */ 3986 for (i = 0; addr < limit && i < size - 1; addr++) { 3987 if ((c = dtrace_load8(addr)) == '\0') 3988 break; 3989 3990 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3991 break; 3992 3993 ASSERT(i < size); 3994 dest[i++] = c; 3995 } 3996 3997 ASSERT(i < size); 3998 dest[i] = '\0'; 3999 regs[rd] = (uintptr_t)dest; 4000 mstate->dtms_scratch_ptr += size; 4001 mstate->dtms_strtok = addr; 4002 break; 4003 } 4004 4005 case DIF_SUBR_SUBSTR: { 4006 uintptr_t s = tupregs[0].dttk_value; 4007 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4008 char *d = (char *)mstate->dtms_scratch_ptr; 4009 int64_t index = (int64_t)tupregs[1].dttk_value; 4010 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4011 size_t len = dtrace_strlen((char *)s, size); 4012 int64_t i = 0; 4013 4014 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4015 regs[rd] = 0; 4016 break; 4017 } 4018 4019 if (!DTRACE_INSCRATCH(mstate, size)) { 4020 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4021 regs[rd] = 0; 4022 break; 4023 } 4024 4025 if (nargs <= 2) 4026 remaining = (int64_t)size; 4027 4028 if (index < 0) { 4029 index += len; 4030 4031 if (index < 0 && index + remaining > 0) { 4032 remaining += index; 4033 index = 0; 4034 } 4035 } 4036 4037 if (index >= len || index < 0) { 4038 remaining = 0; 4039 } else if (remaining < 0) { 4040 remaining += len - index; 4041 } else if (index + remaining > size) { 4042 remaining = size - index; 4043 } 4044 4045 for (i = 0; i < remaining; i++) { 4046 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4047 break; 4048 } 4049 4050 d[i] = '\0'; 4051 4052 mstate->dtms_scratch_ptr += size; 4053 regs[rd] = (uintptr_t)d; 4054 break; 4055 } 4056 4057 case DIF_SUBR_TOUPPER: 4058 case DIF_SUBR_TOLOWER: { 4059 uintptr_t s = tupregs[0].dttk_value; 4060 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4061 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4062 size_t len = dtrace_strlen((char *)s, size); 4063 char lower, upper, convert; 4064 int64_t i; 4065 4066 if (subr == DIF_SUBR_TOUPPER) { 4067 lower = 'a'; 4068 upper = 'z'; 4069 convert = 'A'; 4070 } else { 4071 lower = 'A'; 4072 upper = 'Z'; 4073 convert = 'a'; 4074 } 4075 4076 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4077 regs[rd] = 0; 4078 break; 4079 } 4080 4081 if (!DTRACE_INSCRATCH(mstate, size)) { 4082 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4083 regs[rd] = 0; 4084 break; 4085 } 4086 4087 for (i = 0; i < size - 1; i++) { 4088 if ((c = dtrace_load8(s + i)) == '\0') 4089 break; 4090 4091 if (c >= lower && c <= upper) 4092 c = convert + (c - lower); 4093 4094 dest[i] = c; 4095 } 4096 4097 ASSERT(i < size); 4098 dest[i] = '\0'; 4099 regs[rd] = (uintptr_t)dest; 4100 mstate->dtms_scratch_ptr += size; 4101 break; 4102 } 4103 4104 #if defined(sun) 4105 case DIF_SUBR_GETMAJOR: 4106 #ifdef _LP64 4107 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 4108 #else 4109 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 4110 #endif 4111 break; 4112 4113 case DIF_SUBR_GETMINOR: 4114 #ifdef _LP64 4115 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 4116 #else 4117 regs[rd] = tupregs[0].dttk_value & MAXMIN; 4118 #endif 4119 break; 4120 4121 case DIF_SUBR_DDI_PATHNAME: { 4122 /* 4123 * This one is a galactic mess. We are going to roughly 4124 * emulate ddi_pathname(), but it's made more complicated 4125 * by the fact that we (a) want to include the minor name and 4126 * (b) must proceed iteratively instead of recursively. 4127 */ 4128 uintptr_t dest = mstate->dtms_scratch_ptr; 4129 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4130 char *start = (char *)dest, *end = start + size - 1; 4131 uintptr_t daddr = tupregs[0].dttk_value; 4132 int64_t minor = (int64_t)tupregs[1].dttk_value; 4133 char *s; 4134 int i, len, depth = 0; 4135 4136 /* 4137 * Due to all the pointer jumping we do and context we must 4138 * rely upon, we just mandate that the user must have kernel 4139 * read privileges to use this routine. 4140 */ 4141 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 4142 *flags |= CPU_DTRACE_KPRIV; 4143 *illval = daddr; 4144 regs[rd] = 0; 4145 } 4146 4147 if (!DTRACE_INSCRATCH(mstate, size)) { 4148 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4149 regs[rd] = 0; 4150 break; 4151 } 4152 4153 *end = '\0'; 4154 4155 /* 4156 * We want to have a name for the minor. In order to do this, 4157 * we need to walk the minor list from the devinfo. We want 4158 * to be sure that we don't infinitely walk a circular list, 4159 * so we check for circularity by sending a scout pointer 4160 * ahead two elements for every element that we iterate over; 4161 * if the list is circular, these will ultimately point to the 4162 * same element. You may recognize this little trick as the 4163 * answer to a stupid interview question -- one that always 4164 * seems to be asked by those who had to have it laboriously 4165 * explained to them, and who can't even concisely describe 4166 * the conditions under which one would be forced to resort to 4167 * this technique. Needless to say, those conditions are 4168 * found here -- and probably only here. Is this the only use 4169 * of this infamous trick in shipping, production code? If it 4170 * isn't, it probably should be... 4171 */ 4172 if (minor != -1) { 4173 uintptr_t maddr = dtrace_loadptr(daddr + 4174 offsetof(struct dev_info, devi_minor)); 4175 4176 uintptr_t next = offsetof(struct ddi_minor_data, next); 4177 uintptr_t name = offsetof(struct ddi_minor_data, 4178 d_minor) + offsetof(struct ddi_minor, name); 4179 uintptr_t dev = offsetof(struct ddi_minor_data, 4180 d_minor) + offsetof(struct ddi_minor, dev); 4181 uintptr_t scout; 4182 4183 if (maddr != NULL) 4184 scout = dtrace_loadptr(maddr + next); 4185 4186 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4187 uint64_t m; 4188 #ifdef _LP64 4189 m = dtrace_load64(maddr + dev) & MAXMIN64; 4190 #else 4191 m = dtrace_load32(maddr + dev) & MAXMIN; 4192 #endif 4193 if (m != minor) { 4194 maddr = dtrace_loadptr(maddr + next); 4195 4196 if (scout == NULL) 4197 continue; 4198 4199 scout = dtrace_loadptr(scout + next); 4200 4201 if (scout == NULL) 4202 continue; 4203 4204 scout = dtrace_loadptr(scout + next); 4205 4206 if (scout == NULL) 4207 continue; 4208 4209 if (scout == maddr) { 4210 *flags |= CPU_DTRACE_ILLOP; 4211 break; 4212 } 4213 4214 continue; 4215 } 4216 4217 /* 4218 * We have the minor data. Now we need to 4219 * copy the minor's name into the end of the 4220 * pathname. 4221 */ 4222 s = (char *)dtrace_loadptr(maddr + name); 4223 len = dtrace_strlen(s, size); 4224 4225 if (*flags & CPU_DTRACE_FAULT) 4226 break; 4227 4228 if (len != 0) { 4229 if ((end -= (len + 1)) < start) 4230 break; 4231 4232 *end = ':'; 4233 } 4234 4235 for (i = 1; i <= len; i++) 4236 end[i] = dtrace_load8((uintptr_t)s++); 4237 break; 4238 } 4239 } 4240 4241 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4242 ddi_node_state_t devi_state; 4243 4244 devi_state = dtrace_load32(daddr + 4245 offsetof(struct dev_info, devi_node_state)); 4246 4247 if (*flags & CPU_DTRACE_FAULT) 4248 break; 4249 4250 if (devi_state >= DS_INITIALIZED) { 4251 s = (char *)dtrace_loadptr(daddr + 4252 offsetof(struct dev_info, devi_addr)); 4253 len = dtrace_strlen(s, size); 4254 4255 if (*flags & CPU_DTRACE_FAULT) 4256 break; 4257 4258 if (len != 0) { 4259 if ((end -= (len + 1)) < start) 4260 break; 4261 4262 *end = '@'; 4263 } 4264 4265 for (i = 1; i <= len; i++) 4266 end[i] = dtrace_load8((uintptr_t)s++); 4267 } 4268 4269 /* 4270 * Now for the node name... 4271 */ 4272 s = (char *)dtrace_loadptr(daddr + 4273 offsetof(struct dev_info, devi_node_name)); 4274 4275 daddr = dtrace_loadptr(daddr + 4276 offsetof(struct dev_info, devi_parent)); 4277 4278 /* 4279 * If our parent is NULL (that is, if we're the root 4280 * node), we're going to use the special path 4281 * "devices". 4282 */ 4283 if (daddr == 0) 4284 s = "devices"; 4285 4286 len = dtrace_strlen(s, size); 4287 if (*flags & CPU_DTRACE_FAULT) 4288 break; 4289 4290 if ((end -= (len + 1)) < start) 4291 break; 4292 4293 for (i = 1; i <= len; i++) 4294 end[i] = dtrace_load8((uintptr_t)s++); 4295 *end = '/'; 4296 4297 if (depth++ > dtrace_devdepth_max) { 4298 *flags |= CPU_DTRACE_ILLOP; 4299 break; 4300 } 4301 } 4302 4303 if (end < start) 4304 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4305 4306 if (daddr == 0) { 4307 regs[rd] = (uintptr_t)end; 4308 mstate->dtms_scratch_ptr += size; 4309 } 4310 4311 break; 4312 } 4313 #endif 4314 4315 case DIF_SUBR_STRJOIN: { 4316 char *d = (char *)mstate->dtms_scratch_ptr; 4317 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4318 uintptr_t s1 = tupregs[0].dttk_value; 4319 uintptr_t s2 = tupregs[1].dttk_value; 4320 int i = 0; 4321 4322 if (!dtrace_strcanload(s1, size, mstate, vstate) || 4323 !dtrace_strcanload(s2, size, mstate, vstate)) { 4324 regs[rd] = 0; 4325 break; 4326 } 4327 4328 if (!DTRACE_INSCRATCH(mstate, size)) { 4329 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4330 regs[rd] = 0; 4331 break; 4332 } 4333 4334 for (;;) { 4335 if (i >= size) { 4336 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4337 regs[rd] = 0; 4338 break; 4339 } 4340 4341 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 4342 i--; 4343 break; 4344 } 4345 } 4346 4347 for (;;) { 4348 if (i >= size) { 4349 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4350 regs[rd] = 0; 4351 break; 4352 } 4353 4354 if ((d[i++] = dtrace_load8(s2++)) == '\0') 4355 break; 4356 } 4357 4358 if (i < size) { 4359 mstate->dtms_scratch_ptr += i; 4360 regs[rd] = (uintptr_t)d; 4361 } 4362 4363 break; 4364 } 4365 4366 case DIF_SUBR_LLTOSTR: { 4367 int64_t i = (int64_t)tupregs[0].dttk_value; 4368 uint64_t val, digit; 4369 uint64_t size = 65; /* enough room for 2^64 in binary */ 4370 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 4371 int base = 10; 4372 4373 if (nargs > 1) { 4374 if ((base = tupregs[1].dttk_value) <= 1 || 4375 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 4376 *flags |= CPU_DTRACE_ILLOP; 4377 break; 4378 } 4379 } 4380 4381 val = (base == 10 && i < 0) ? i * -1 : i; 4382 4383 if (!DTRACE_INSCRATCH(mstate, size)) { 4384 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4385 regs[rd] = 0; 4386 break; 4387 } 4388 4389 for (*end-- = '\0'; val; val /= base) { 4390 if ((digit = val % base) <= '9' - '0') { 4391 *end-- = '0' + digit; 4392 } else { 4393 *end-- = 'a' + (digit - ('9' - '0') - 1); 4394 } 4395 } 4396 4397 if (i == 0 && base == 16) 4398 *end-- = '0'; 4399 4400 if (base == 16) 4401 *end-- = 'x'; 4402 4403 if (i == 0 || base == 8 || base == 16) 4404 *end-- = '0'; 4405 4406 if (i < 0 && base == 10) 4407 *end-- = '-'; 4408 4409 regs[rd] = (uintptr_t)end + 1; 4410 mstate->dtms_scratch_ptr += size; 4411 break; 4412 } 4413 4414 case DIF_SUBR_HTONS: 4415 case DIF_SUBR_NTOHS: 4416 #if BYTE_ORDER == BIG_ENDIAN 4417 regs[rd] = (uint16_t)tupregs[0].dttk_value; 4418 #else 4419 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 4420 #endif 4421 break; 4422 4423 4424 case DIF_SUBR_HTONL: 4425 case DIF_SUBR_NTOHL: 4426 #if BYTE_ORDER == BIG_ENDIAN 4427 regs[rd] = (uint32_t)tupregs[0].dttk_value; 4428 #else 4429 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 4430 #endif 4431 break; 4432 4433 4434 case DIF_SUBR_HTONLL: 4435 case DIF_SUBR_NTOHLL: 4436 #if BYTE_ORDER == BIG_ENDIAN 4437 regs[rd] = (uint64_t)tupregs[0].dttk_value; 4438 #else 4439 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 4440 #endif 4441 break; 4442 4443 4444 case DIF_SUBR_DIRNAME: 4445 case DIF_SUBR_BASENAME: { 4446 char *dest = (char *)mstate->dtms_scratch_ptr; 4447 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4448 uintptr_t src = tupregs[0].dttk_value; 4449 int i, j, len = dtrace_strlen((char *)src, size); 4450 int lastbase = -1, firstbase = -1, lastdir = -1; 4451 int start, end; 4452 4453 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 4454 regs[rd] = 0; 4455 break; 4456 } 4457 4458 if (!DTRACE_INSCRATCH(mstate, size)) { 4459 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4460 regs[rd] = 0; 4461 break; 4462 } 4463 4464 /* 4465 * The basename and dirname for a zero-length string is 4466 * defined to be "." 4467 */ 4468 if (len == 0) { 4469 len = 1; 4470 src = (uintptr_t)"."; 4471 } 4472 4473 /* 4474 * Start from the back of the string, moving back toward the 4475 * front until we see a character that isn't a slash. That 4476 * character is the last character in the basename. 4477 */ 4478 for (i = len - 1; i >= 0; i--) { 4479 if (dtrace_load8(src + i) != '/') 4480 break; 4481 } 4482 4483 if (i >= 0) 4484 lastbase = i; 4485 4486 /* 4487 * Starting from the last character in the basename, move 4488 * towards the front until we find a slash. The character 4489 * that we processed immediately before that is the first 4490 * character in the basename. 4491 */ 4492 for (; i >= 0; i--) { 4493 if (dtrace_load8(src + i) == '/') 4494 break; 4495 } 4496 4497 if (i >= 0) 4498 firstbase = i + 1; 4499 4500 /* 4501 * Now keep going until we find a non-slash character. That 4502 * character is the last character in the dirname. 4503 */ 4504 for (; i >= 0; i--) { 4505 if (dtrace_load8(src + i) != '/') 4506 break; 4507 } 4508 4509 if (i >= 0) 4510 lastdir = i; 4511 4512 ASSERT(!(lastbase == -1 && firstbase != -1)); 4513 ASSERT(!(firstbase == -1 && lastdir != -1)); 4514 4515 if (lastbase == -1) { 4516 /* 4517 * We didn't find a non-slash character. We know that 4518 * the length is non-zero, so the whole string must be 4519 * slashes. In either the dirname or the basename 4520 * case, we return '/'. 4521 */ 4522 ASSERT(firstbase == -1); 4523 firstbase = lastbase = lastdir = 0; 4524 } 4525 4526 if (firstbase == -1) { 4527 /* 4528 * The entire string consists only of a basename 4529 * component. If we're looking for dirname, we need 4530 * to change our string to be just "."; if we're 4531 * looking for a basename, we'll just set the first 4532 * character of the basename to be 0. 4533 */ 4534 if (subr == DIF_SUBR_DIRNAME) { 4535 ASSERT(lastdir == -1); 4536 src = (uintptr_t)"."; 4537 lastdir = 0; 4538 } else { 4539 firstbase = 0; 4540 } 4541 } 4542 4543 if (subr == DIF_SUBR_DIRNAME) { 4544 if (lastdir == -1) { 4545 /* 4546 * We know that we have a slash in the name -- 4547 * or lastdir would be set to 0, above. And 4548 * because lastdir is -1, we know that this 4549 * slash must be the first character. (That 4550 * is, the full string must be of the form 4551 * "/basename".) In this case, the last 4552 * character of the directory name is 0. 4553 */ 4554 lastdir = 0; 4555 } 4556 4557 start = 0; 4558 end = lastdir; 4559 } else { 4560 ASSERT(subr == DIF_SUBR_BASENAME); 4561 ASSERT(firstbase != -1 && lastbase != -1); 4562 start = firstbase; 4563 end = lastbase; 4564 } 4565 4566 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 4567 dest[j] = dtrace_load8(src + i); 4568 4569 dest[j] = '\0'; 4570 regs[rd] = (uintptr_t)dest; 4571 mstate->dtms_scratch_ptr += size; 4572 break; 4573 } 4574 4575 case DIF_SUBR_CLEANPATH: { 4576 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4577 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4578 uintptr_t src = tupregs[0].dttk_value; 4579 int i = 0, j = 0; 4580 4581 if (!dtrace_strcanload(src, size, mstate, vstate)) { 4582 regs[rd] = 0; 4583 break; 4584 } 4585 4586 if (!DTRACE_INSCRATCH(mstate, size)) { 4587 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4588 regs[rd] = 0; 4589 break; 4590 } 4591 4592 /* 4593 * Move forward, loading each character. 4594 */ 4595 do { 4596 c = dtrace_load8(src + i++); 4597 next: 4598 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 4599 break; 4600 4601 if (c != '/') { 4602 dest[j++] = c; 4603 continue; 4604 } 4605 4606 c = dtrace_load8(src + i++); 4607 4608 if (c == '/') { 4609 /* 4610 * We have two slashes -- we can just advance 4611 * to the next character. 4612 */ 4613 goto next; 4614 } 4615 4616 if (c != '.') { 4617 /* 4618 * This is not "." and it's not ".." -- we can 4619 * just store the "/" and this character and 4620 * drive on. 4621 */ 4622 dest[j++] = '/'; 4623 dest[j++] = c; 4624 continue; 4625 } 4626 4627 c = dtrace_load8(src + i++); 4628 4629 if (c == '/') { 4630 /* 4631 * This is a "/./" component. We're not going 4632 * to store anything in the destination buffer; 4633 * we're just going to go to the next component. 4634 */ 4635 goto next; 4636 } 4637 4638 if (c != '.') { 4639 /* 4640 * This is not ".." -- we can just store the 4641 * "/." and this character and continue 4642 * processing. 4643 */ 4644 dest[j++] = '/'; 4645 dest[j++] = '.'; 4646 dest[j++] = c; 4647 continue; 4648 } 4649 4650 c = dtrace_load8(src + i++); 4651 4652 if (c != '/' && c != '\0') { 4653 /* 4654 * This is not ".." -- it's "..[mumble]". 4655 * We'll store the "/.." and this character 4656 * and continue processing. 4657 */ 4658 dest[j++] = '/'; 4659 dest[j++] = '.'; 4660 dest[j++] = '.'; 4661 dest[j++] = c; 4662 continue; 4663 } 4664 4665 /* 4666 * This is "/../" or "/..\0". We need to back up 4667 * our destination pointer until we find a "/". 4668 */ 4669 i--; 4670 while (j != 0 && dest[--j] != '/') 4671 continue; 4672 4673 if (c == '\0') 4674 dest[++j] = '/'; 4675 } while (c != '\0'); 4676 4677 dest[j] = '\0'; 4678 regs[rd] = (uintptr_t)dest; 4679 mstate->dtms_scratch_ptr += size; 4680 break; 4681 } 4682 4683 case DIF_SUBR_INET_NTOA: 4684 case DIF_SUBR_INET_NTOA6: 4685 case DIF_SUBR_INET_NTOP: { 4686 size_t size; 4687 int af, argi, i; 4688 char *base, *end; 4689 4690 if (subr == DIF_SUBR_INET_NTOP) { 4691 af = (int)tupregs[0].dttk_value; 4692 argi = 1; 4693 } else { 4694 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 4695 argi = 0; 4696 } 4697 4698 if (af == AF_INET) { 4699 ipaddr_t ip4; 4700 uint8_t *ptr8, val; 4701 4702 /* 4703 * Safely load the IPv4 address. 4704 */ 4705 ip4 = dtrace_load32(tupregs[argi].dttk_value); 4706 4707 /* 4708 * Check an IPv4 string will fit in scratch. 4709 */ 4710 size = INET_ADDRSTRLEN; 4711 if (!DTRACE_INSCRATCH(mstate, size)) { 4712 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4713 regs[rd] = 0; 4714 break; 4715 } 4716 base = (char *)mstate->dtms_scratch_ptr; 4717 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4718 4719 /* 4720 * Stringify as a dotted decimal quad. 4721 */ 4722 *end-- = '\0'; 4723 ptr8 = (uint8_t *)&ip4; 4724 for (i = 3; i >= 0; i--) { 4725 val = ptr8[i]; 4726 4727 if (val == 0) { 4728 *end-- = '0'; 4729 } else { 4730 for (; val; val /= 10) { 4731 *end-- = '0' + (val % 10); 4732 } 4733 } 4734 4735 if (i > 0) 4736 *end-- = '.'; 4737 } 4738 ASSERT(end + 1 >= base); 4739 4740 } else if (af == AF_INET6) { 4741 struct in6_addr ip6; 4742 int firstzero, tryzero, numzero, v6end; 4743 uint16_t val; 4744 const char digits[] = "0123456789abcdef"; 4745 4746 /* 4747 * Stringify using RFC 1884 convention 2 - 16 bit 4748 * hexadecimal values with a zero-run compression. 4749 * Lower case hexadecimal digits are used. 4750 * eg, fe80::214:4fff:fe0b:76c8. 4751 * The IPv4 embedded form is returned for inet_ntop, 4752 * just the IPv4 string is returned for inet_ntoa6. 4753 */ 4754 4755 /* 4756 * Safely load the IPv6 address. 4757 */ 4758 dtrace_bcopy( 4759 (void *)(uintptr_t)tupregs[argi].dttk_value, 4760 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 4761 4762 /* 4763 * Check an IPv6 string will fit in scratch. 4764 */ 4765 size = INET6_ADDRSTRLEN; 4766 if (!DTRACE_INSCRATCH(mstate, size)) { 4767 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4768 regs[rd] = 0; 4769 break; 4770 } 4771 base = (char *)mstate->dtms_scratch_ptr; 4772 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4773 *end-- = '\0'; 4774 4775 /* 4776 * Find the longest run of 16 bit zero values 4777 * for the single allowed zero compression - "::". 4778 */ 4779 firstzero = -1; 4780 tryzero = -1; 4781 numzero = 1; 4782 for (i = 0; i < sizeof (struct in6_addr); i++) { 4783 #if defined(sun) 4784 if (ip6._S6_un._S6_u8[i] == 0 && 4785 #else 4786 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 4787 #endif 4788 tryzero == -1 && i % 2 == 0) { 4789 tryzero = i; 4790 continue; 4791 } 4792 4793 if (tryzero != -1 && 4794 #if defined(sun) 4795 (ip6._S6_un._S6_u8[i] != 0 || 4796 #else 4797 (ip6.__u6_addr.__u6_addr8[i] != 0 || 4798 #endif 4799 i == sizeof (struct in6_addr) - 1)) { 4800 4801 if (i - tryzero <= numzero) { 4802 tryzero = -1; 4803 continue; 4804 } 4805 4806 firstzero = tryzero; 4807 numzero = i - i % 2 - tryzero; 4808 tryzero = -1; 4809 4810 #if defined(sun) 4811 if (ip6._S6_un._S6_u8[i] == 0 && 4812 #else 4813 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 4814 #endif 4815 i == sizeof (struct in6_addr) - 1) 4816 numzero += 2; 4817 } 4818 } 4819 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 4820 4821 /* 4822 * Check for an IPv4 embedded address. 4823 */ 4824 v6end = sizeof (struct in6_addr) - 2; 4825 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 4826 IN6_IS_ADDR_V4COMPAT(&ip6)) { 4827 for (i = sizeof (struct in6_addr) - 1; 4828 i >= DTRACE_V4MAPPED_OFFSET; i--) { 4829 ASSERT(end >= base); 4830 4831 #if defined(sun) 4832 val = ip6._S6_un._S6_u8[i]; 4833 #else 4834 val = ip6.__u6_addr.__u6_addr8[i]; 4835 #endif 4836 4837 if (val == 0) { 4838 *end-- = '0'; 4839 } else { 4840 for (; val; val /= 10) { 4841 *end-- = '0' + val % 10; 4842 } 4843 } 4844 4845 if (i > DTRACE_V4MAPPED_OFFSET) 4846 *end-- = '.'; 4847 } 4848 4849 if (subr == DIF_SUBR_INET_NTOA6) 4850 goto inetout; 4851 4852 /* 4853 * Set v6end to skip the IPv4 address that 4854 * we have already stringified. 4855 */ 4856 v6end = 10; 4857 } 4858 4859 /* 4860 * Build the IPv6 string by working through the 4861 * address in reverse. 4862 */ 4863 for (i = v6end; i >= 0; i -= 2) { 4864 ASSERT(end >= base); 4865 4866 if (i == firstzero + numzero - 2) { 4867 *end-- = ':'; 4868 *end-- = ':'; 4869 i -= numzero - 2; 4870 continue; 4871 } 4872 4873 if (i < 14 && i != firstzero - 2) 4874 *end-- = ':'; 4875 4876 #if defined(sun) 4877 val = (ip6._S6_un._S6_u8[i] << 8) + 4878 ip6._S6_un._S6_u8[i + 1]; 4879 #else 4880 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 4881 ip6.__u6_addr.__u6_addr8[i + 1]; 4882 #endif 4883 4884 if (val == 0) { 4885 *end-- = '0'; 4886 } else { 4887 for (; val; val /= 16) { 4888 *end-- = digits[val % 16]; 4889 } 4890 } 4891 } 4892 ASSERT(end + 1 >= base); 4893 4894 } else { 4895 /* 4896 * The user didn't use AH_INET or AH_INET6. 4897 */ 4898 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4899 regs[rd] = 0; 4900 break; 4901 } 4902 4903 inetout: regs[rd] = (uintptr_t)end + 1; 4904 mstate->dtms_scratch_ptr += size; 4905 break; 4906 } 4907 4908 case DIF_SUBR_MEMREF: { 4909 uintptr_t size = 2 * sizeof(uintptr_t); 4910 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 4911 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 4912 4913 /* address and length */ 4914 memref[0] = tupregs[0].dttk_value; 4915 memref[1] = tupregs[1].dttk_value; 4916 4917 regs[rd] = (uintptr_t) memref; 4918 mstate->dtms_scratch_ptr += scratch_size; 4919 break; 4920 } 4921 4922 case DIF_SUBR_TYPEREF: { 4923 uintptr_t size = 4 * sizeof(uintptr_t); 4924 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 4925 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 4926 4927 /* address, num_elements, type_str, type_len */ 4928 typeref[0] = tupregs[0].dttk_value; 4929 typeref[1] = tupregs[1].dttk_value; 4930 typeref[2] = tupregs[2].dttk_value; 4931 typeref[3] = tupregs[3].dttk_value; 4932 4933 regs[rd] = (uintptr_t) typeref; 4934 mstate->dtms_scratch_ptr += scratch_size; 4935 break; 4936 } 4937 } 4938 } 4939 4940 /* 4941 * Emulate the execution of DTrace IR instructions specified by the given 4942 * DIF object. This function is deliberately void of assertions as all of 4943 * the necessary checks are handled by a call to dtrace_difo_validate(). 4944 */ 4945 static uint64_t 4946 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 4947 dtrace_vstate_t *vstate, dtrace_state_t *state) 4948 { 4949 const dif_instr_t *text = difo->dtdo_buf; 4950 const uint_t textlen = difo->dtdo_len; 4951 const char *strtab = difo->dtdo_strtab; 4952 const uint64_t *inttab = difo->dtdo_inttab; 4953 4954 uint64_t rval = 0; 4955 dtrace_statvar_t *svar; 4956 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 4957 dtrace_difv_t *v; 4958 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4959 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4960 4961 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 4962 uint64_t regs[DIF_DIR_NREGS]; 4963 uint64_t *tmp; 4964 4965 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 4966 int64_t cc_r; 4967 uint_t pc = 0, id, opc = 0; 4968 uint8_t ttop = 0; 4969 dif_instr_t instr; 4970 uint_t r1, r2, rd; 4971 4972 /* 4973 * We stash the current DIF object into the machine state: we need it 4974 * for subsequent access checking. 4975 */ 4976 mstate->dtms_difo = difo; 4977 4978 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 4979 4980 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 4981 opc = pc; 4982 4983 instr = text[pc++]; 4984 r1 = DIF_INSTR_R1(instr); 4985 r2 = DIF_INSTR_R2(instr); 4986 rd = DIF_INSTR_RD(instr); 4987 4988 switch (DIF_INSTR_OP(instr)) { 4989 case DIF_OP_OR: 4990 regs[rd] = regs[r1] | regs[r2]; 4991 break; 4992 case DIF_OP_XOR: 4993 regs[rd] = regs[r1] ^ regs[r2]; 4994 break; 4995 case DIF_OP_AND: 4996 regs[rd] = regs[r1] & regs[r2]; 4997 break; 4998 case DIF_OP_SLL: 4999 regs[rd] = regs[r1] << regs[r2]; 5000 break; 5001 case DIF_OP_SRL: 5002 regs[rd] = regs[r1] >> regs[r2]; 5003 break; 5004 case DIF_OP_SUB: 5005 regs[rd] = regs[r1] - regs[r2]; 5006 break; 5007 case DIF_OP_ADD: 5008 regs[rd] = regs[r1] + regs[r2]; 5009 break; 5010 case DIF_OP_MUL: 5011 regs[rd] = regs[r1] * regs[r2]; 5012 break; 5013 case DIF_OP_SDIV: 5014 if (regs[r2] == 0) { 5015 regs[rd] = 0; 5016 *flags |= CPU_DTRACE_DIVZERO; 5017 } else { 5018 regs[rd] = (int64_t)regs[r1] / 5019 (int64_t)regs[r2]; 5020 } 5021 break; 5022 5023 case DIF_OP_UDIV: 5024 if (regs[r2] == 0) { 5025 regs[rd] = 0; 5026 *flags |= CPU_DTRACE_DIVZERO; 5027 } else { 5028 regs[rd] = regs[r1] / regs[r2]; 5029 } 5030 break; 5031 5032 case DIF_OP_SREM: 5033 if (regs[r2] == 0) { 5034 regs[rd] = 0; 5035 *flags |= CPU_DTRACE_DIVZERO; 5036 } else { 5037 regs[rd] = (int64_t)regs[r1] % 5038 (int64_t)regs[r2]; 5039 } 5040 break; 5041 5042 case DIF_OP_UREM: 5043 if (regs[r2] == 0) { 5044 regs[rd] = 0; 5045 *flags |= CPU_DTRACE_DIVZERO; 5046 } else { 5047 regs[rd] = regs[r1] % regs[r2]; 5048 } 5049 break; 5050 5051 case DIF_OP_NOT: 5052 regs[rd] = ~regs[r1]; 5053 break; 5054 case DIF_OP_MOV: 5055 regs[rd] = regs[r1]; 5056 break; 5057 case DIF_OP_CMP: 5058 cc_r = regs[r1] - regs[r2]; 5059 cc_n = cc_r < 0; 5060 cc_z = cc_r == 0; 5061 cc_v = 0; 5062 cc_c = regs[r1] < regs[r2]; 5063 break; 5064 case DIF_OP_TST: 5065 cc_n = cc_v = cc_c = 0; 5066 cc_z = regs[r1] == 0; 5067 break; 5068 case DIF_OP_BA: 5069 pc = DIF_INSTR_LABEL(instr); 5070 break; 5071 case DIF_OP_BE: 5072 if (cc_z) 5073 pc = DIF_INSTR_LABEL(instr); 5074 break; 5075 case DIF_OP_BNE: 5076 if (cc_z == 0) 5077 pc = DIF_INSTR_LABEL(instr); 5078 break; 5079 case DIF_OP_BG: 5080 if ((cc_z | (cc_n ^ cc_v)) == 0) 5081 pc = DIF_INSTR_LABEL(instr); 5082 break; 5083 case DIF_OP_BGU: 5084 if ((cc_c | cc_z) == 0) 5085 pc = DIF_INSTR_LABEL(instr); 5086 break; 5087 case DIF_OP_BGE: 5088 if ((cc_n ^ cc_v) == 0) 5089 pc = DIF_INSTR_LABEL(instr); 5090 break; 5091 case DIF_OP_BGEU: 5092 if (cc_c == 0) 5093 pc = DIF_INSTR_LABEL(instr); 5094 break; 5095 case DIF_OP_BL: 5096 if (cc_n ^ cc_v) 5097 pc = DIF_INSTR_LABEL(instr); 5098 break; 5099 case DIF_OP_BLU: 5100 if (cc_c) 5101 pc = DIF_INSTR_LABEL(instr); 5102 break; 5103 case DIF_OP_BLE: 5104 if (cc_z | (cc_n ^ cc_v)) 5105 pc = DIF_INSTR_LABEL(instr); 5106 break; 5107 case DIF_OP_BLEU: 5108 if (cc_c | cc_z) 5109 pc = DIF_INSTR_LABEL(instr); 5110 break; 5111 case DIF_OP_RLDSB: 5112 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 5113 *flags |= CPU_DTRACE_KPRIV; 5114 *illval = regs[r1]; 5115 break; 5116 } 5117 /*FALLTHROUGH*/ 5118 case DIF_OP_LDSB: 5119 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 5120 break; 5121 case DIF_OP_RLDSH: 5122 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 5123 *flags |= CPU_DTRACE_KPRIV; 5124 *illval = regs[r1]; 5125 break; 5126 } 5127 /*FALLTHROUGH*/ 5128 case DIF_OP_LDSH: 5129 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 5130 break; 5131 case DIF_OP_RLDSW: 5132 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 5133 *flags |= CPU_DTRACE_KPRIV; 5134 *illval = regs[r1]; 5135 break; 5136 } 5137 /*FALLTHROUGH*/ 5138 case DIF_OP_LDSW: 5139 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 5140 break; 5141 case DIF_OP_RLDUB: 5142 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 5143 *flags |= CPU_DTRACE_KPRIV; 5144 *illval = regs[r1]; 5145 break; 5146 } 5147 /*FALLTHROUGH*/ 5148 case DIF_OP_LDUB: 5149 regs[rd] = dtrace_load8(regs[r1]); 5150 break; 5151 case DIF_OP_RLDUH: 5152 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 5153 *flags |= CPU_DTRACE_KPRIV; 5154 *illval = regs[r1]; 5155 break; 5156 } 5157 /*FALLTHROUGH*/ 5158 case DIF_OP_LDUH: 5159 regs[rd] = dtrace_load16(regs[r1]); 5160 break; 5161 case DIF_OP_RLDUW: 5162 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 5163 *flags |= CPU_DTRACE_KPRIV; 5164 *illval = regs[r1]; 5165 break; 5166 } 5167 /*FALLTHROUGH*/ 5168 case DIF_OP_LDUW: 5169 regs[rd] = dtrace_load32(regs[r1]); 5170 break; 5171 case DIF_OP_RLDX: 5172 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 5173 *flags |= CPU_DTRACE_KPRIV; 5174 *illval = regs[r1]; 5175 break; 5176 } 5177 /*FALLTHROUGH*/ 5178 case DIF_OP_LDX: 5179 regs[rd] = dtrace_load64(regs[r1]); 5180 break; 5181 case DIF_OP_ULDSB: 5182 regs[rd] = (int8_t) 5183 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5184 break; 5185 case DIF_OP_ULDSH: 5186 regs[rd] = (int16_t) 5187 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5188 break; 5189 case DIF_OP_ULDSW: 5190 regs[rd] = (int32_t) 5191 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5192 break; 5193 case DIF_OP_ULDUB: 5194 regs[rd] = 5195 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5196 break; 5197 case DIF_OP_ULDUH: 5198 regs[rd] = 5199 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5200 break; 5201 case DIF_OP_ULDUW: 5202 regs[rd] = 5203 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5204 break; 5205 case DIF_OP_ULDX: 5206 regs[rd] = 5207 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 5208 break; 5209 case DIF_OP_RET: 5210 rval = regs[rd]; 5211 pc = textlen; 5212 break; 5213 case DIF_OP_NOP: 5214 break; 5215 case DIF_OP_SETX: 5216 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 5217 break; 5218 case DIF_OP_SETS: 5219 regs[rd] = (uint64_t)(uintptr_t) 5220 (strtab + DIF_INSTR_STRING(instr)); 5221 break; 5222 case DIF_OP_SCMP: { 5223 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 5224 uintptr_t s1 = regs[r1]; 5225 uintptr_t s2 = regs[r2]; 5226 5227 if (s1 != 0 && 5228 !dtrace_strcanload(s1, sz, mstate, vstate)) 5229 break; 5230 if (s2 != 0 && 5231 !dtrace_strcanload(s2, sz, mstate, vstate)) 5232 break; 5233 5234 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 5235 5236 cc_n = cc_r < 0; 5237 cc_z = cc_r == 0; 5238 cc_v = cc_c = 0; 5239 break; 5240 } 5241 case DIF_OP_LDGA: 5242 regs[rd] = dtrace_dif_variable(mstate, state, 5243 r1, regs[r2]); 5244 break; 5245 case DIF_OP_LDGS: 5246 id = DIF_INSTR_VAR(instr); 5247 5248 if (id >= DIF_VAR_OTHER_UBASE) { 5249 uintptr_t a; 5250 5251 id -= DIF_VAR_OTHER_UBASE; 5252 svar = vstate->dtvs_globals[id]; 5253 ASSERT(svar != NULL); 5254 v = &svar->dtsv_var; 5255 5256 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 5257 regs[rd] = svar->dtsv_data; 5258 break; 5259 } 5260 5261 a = (uintptr_t)svar->dtsv_data; 5262 5263 if (*(uint8_t *)a == UINT8_MAX) { 5264 /* 5265 * If the 0th byte is set to UINT8_MAX 5266 * then this is to be treated as a 5267 * reference to a NULL variable. 5268 */ 5269 regs[rd] = 0; 5270 } else { 5271 regs[rd] = a + sizeof (uint64_t); 5272 } 5273 5274 break; 5275 } 5276 5277 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 5278 break; 5279 5280 case DIF_OP_STGS: 5281 id = DIF_INSTR_VAR(instr); 5282 5283 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5284 id -= DIF_VAR_OTHER_UBASE; 5285 5286 svar = vstate->dtvs_globals[id]; 5287 ASSERT(svar != NULL); 5288 v = &svar->dtsv_var; 5289 5290 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5291 uintptr_t a = (uintptr_t)svar->dtsv_data; 5292 5293 ASSERT(a != 0); 5294 ASSERT(svar->dtsv_size != 0); 5295 5296 if (regs[rd] == 0) { 5297 *(uint8_t *)a = UINT8_MAX; 5298 break; 5299 } else { 5300 *(uint8_t *)a = 0; 5301 a += sizeof (uint64_t); 5302 } 5303 if (!dtrace_vcanload( 5304 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5305 mstate, vstate)) 5306 break; 5307 5308 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5309 (void *)a, &v->dtdv_type); 5310 break; 5311 } 5312 5313 svar->dtsv_data = regs[rd]; 5314 break; 5315 5316 case DIF_OP_LDTA: 5317 /* 5318 * There are no DTrace built-in thread-local arrays at 5319 * present. This opcode is saved for future work. 5320 */ 5321 *flags |= CPU_DTRACE_ILLOP; 5322 regs[rd] = 0; 5323 break; 5324 5325 case DIF_OP_LDLS: 5326 id = DIF_INSTR_VAR(instr); 5327 5328 if (id < DIF_VAR_OTHER_UBASE) { 5329 /* 5330 * For now, this has no meaning. 5331 */ 5332 regs[rd] = 0; 5333 break; 5334 } 5335 5336 id -= DIF_VAR_OTHER_UBASE; 5337 5338 ASSERT(id < vstate->dtvs_nlocals); 5339 ASSERT(vstate->dtvs_locals != NULL); 5340 5341 svar = vstate->dtvs_locals[id]; 5342 ASSERT(svar != NULL); 5343 v = &svar->dtsv_var; 5344 5345 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5346 uintptr_t a = (uintptr_t)svar->dtsv_data; 5347 size_t sz = v->dtdv_type.dtdt_size; 5348 5349 sz += sizeof (uint64_t); 5350 ASSERT(svar->dtsv_size == NCPU * sz); 5351 a += curcpu * sz; 5352 5353 if (*(uint8_t *)a == UINT8_MAX) { 5354 /* 5355 * If the 0th byte is set to UINT8_MAX 5356 * then this is to be treated as a 5357 * reference to a NULL variable. 5358 */ 5359 regs[rd] = 0; 5360 } else { 5361 regs[rd] = a + sizeof (uint64_t); 5362 } 5363 5364 break; 5365 } 5366 5367 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5368 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5369 regs[rd] = tmp[curcpu]; 5370 break; 5371 5372 case DIF_OP_STLS: 5373 id = DIF_INSTR_VAR(instr); 5374 5375 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5376 id -= DIF_VAR_OTHER_UBASE; 5377 ASSERT(id < vstate->dtvs_nlocals); 5378 5379 ASSERT(vstate->dtvs_locals != NULL); 5380 svar = vstate->dtvs_locals[id]; 5381 ASSERT(svar != NULL); 5382 v = &svar->dtsv_var; 5383 5384 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5385 uintptr_t a = (uintptr_t)svar->dtsv_data; 5386 size_t sz = v->dtdv_type.dtdt_size; 5387 5388 sz += sizeof (uint64_t); 5389 ASSERT(svar->dtsv_size == NCPU * sz); 5390 a += curcpu * sz; 5391 5392 if (regs[rd] == 0) { 5393 *(uint8_t *)a = UINT8_MAX; 5394 break; 5395 } else { 5396 *(uint8_t *)a = 0; 5397 a += sizeof (uint64_t); 5398 } 5399 5400 if (!dtrace_vcanload( 5401 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5402 mstate, vstate)) 5403 break; 5404 5405 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5406 (void *)a, &v->dtdv_type); 5407 break; 5408 } 5409 5410 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5411 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5412 tmp[curcpu] = regs[rd]; 5413 break; 5414 5415 case DIF_OP_LDTS: { 5416 dtrace_dynvar_t *dvar; 5417 dtrace_key_t *key; 5418 5419 id = DIF_INSTR_VAR(instr); 5420 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5421 id -= DIF_VAR_OTHER_UBASE; 5422 v = &vstate->dtvs_tlocals[id]; 5423 5424 key = &tupregs[DIF_DTR_NREGS]; 5425 key[0].dttk_value = (uint64_t)id; 5426 key[0].dttk_size = 0; 5427 DTRACE_TLS_THRKEY(key[1].dttk_value); 5428 key[1].dttk_size = 0; 5429 5430 dvar = dtrace_dynvar(dstate, 2, key, 5431 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 5432 mstate, vstate); 5433 5434 if (dvar == NULL) { 5435 regs[rd] = 0; 5436 break; 5437 } 5438 5439 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5440 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5441 } else { 5442 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5443 } 5444 5445 break; 5446 } 5447 5448 case DIF_OP_STTS: { 5449 dtrace_dynvar_t *dvar; 5450 dtrace_key_t *key; 5451 5452 id = DIF_INSTR_VAR(instr); 5453 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5454 id -= DIF_VAR_OTHER_UBASE; 5455 5456 key = &tupregs[DIF_DTR_NREGS]; 5457 key[0].dttk_value = (uint64_t)id; 5458 key[0].dttk_size = 0; 5459 DTRACE_TLS_THRKEY(key[1].dttk_value); 5460 key[1].dttk_size = 0; 5461 v = &vstate->dtvs_tlocals[id]; 5462 5463 dvar = dtrace_dynvar(dstate, 2, key, 5464 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5465 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5466 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5467 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5468 5469 /* 5470 * Given that we're storing to thread-local data, 5471 * we need to flush our predicate cache. 5472 */ 5473 curthread->t_predcache = 0; 5474 5475 if (dvar == NULL) 5476 break; 5477 5478 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5479 if (!dtrace_vcanload( 5480 (void *)(uintptr_t)regs[rd], 5481 &v->dtdv_type, mstate, vstate)) 5482 break; 5483 5484 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5485 dvar->dtdv_data, &v->dtdv_type); 5486 } else { 5487 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5488 } 5489 5490 break; 5491 } 5492 5493 case DIF_OP_SRA: 5494 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 5495 break; 5496 5497 case DIF_OP_CALL: 5498 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 5499 regs, tupregs, ttop, mstate, state); 5500 break; 5501 5502 case DIF_OP_PUSHTR: 5503 if (ttop == DIF_DTR_NREGS) { 5504 *flags |= CPU_DTRACE_TUPOFLOW; 5505 break; 5506 } 5507 5508 if (r1 == DIF_TYPE_STRING) { 5509 /* 5510 * If this is a string type and the size is 0, 5511 * we'll use the system-wide default string 5512 * size. Note that we are _not_ looking at 5513 * the value of the DTRACEOPT_STRSIZE option; 5514 * had this been set, we would expect to have 5515 * a non-zero size value in the "pushtr". 5516 */ 5517 tupregs[ttop].dttk_size = 5518 dtrace_strlen((char *)(uintptr_t)regs[rd], 5519 regs[r2] ? regs[r2] : 5520 dtrace_strsize_default) + 1; 5521 } else { 5522 tupregs[ttop].dttk_size = regs[r2]; 5523 } 5524 5525 tupregs[ttop++].dttk_value = regs[rd]; 5526 break; 5527 5528 case DIF_OP_PUSHTV: 5529 if (ttop == DIF_DTR_NREGS) { 5530 *flags |= CPU_DTRACE_TUPOFLOW; 5531 break; 5532 } 5533 5534 tupregs[ttop].dttk_value = regs[rd]; 5535 tupregs[ttop++].dttk_size = 0; 5536 break; 5537 5538 case DIF_OP_POPTS: 5539 if (ttop != 0) 5540 ttop--; 5541 break; 5542 5543 case DIF_OP_FLUSHTS: 5544 ttop = 0; 5545 break; 5546 5547 case DIF_OP_LDGAA: 5548 case DIF_OP_LDTAA: { 5549 dtrace_dynvar_t *dvar; 5550 dtrace_key_t *key = tupregs; 5551 uint_t nkeys = ttop; 5552 5553 id = DIF_INSTR_VAR(instr); 5554 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5555 id -= DIF_VAR_OTHER_UBASE; 5556 5557 key[nkeys].dttk_value = (uint64_t)id; 5558 key[nkeys++].dttk_size = 0; 5559 5560 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 5561 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5562 key[nkeys++].dttk_size = 0; 5563 v = &vstate->dtvs_tlocals[id]; 5564 } else { 5565 v = &vstate->dtvs_globals[id]->dtsv_var; 5566 } 5567 5568 dvar = dtrace_dynvar(dstate, nkeys, key, 5569 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5570 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5571 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 5572 5573 if (dvar == NULL) { 5574 regs[rd] = 0; 5575 break; 5576 } 5577 5578 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5579 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5580 } else { 5581 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5582 } 5583 5584 break; 5585 } 5586 5587 case DIF_OP_STGAA: 5588 case DIF_OP_STTAA: { 5589 dtrace_dynvar_t *dvar; 5590 dtrace_key_t *key = tupregs; 5591 uint_t nkeys = ttop; 5592 5593 id = DIF_INSTR_VAR(instr); 5594 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5595 id -= DIF_VAR_OTHER_UBASE; 5596 5597 key[nkeys].dttk_value = (uint64_t)id; 5598 key[nkeys++].dttk_size = 0; 5599 5600 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 5601 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5602 key[nkeys++].dttk_size = 0; 5603 v = &vstate->dtvs_tlocals[id]; 5604 } else { 5605 v = &vstate->dtvs_globals[id]->dtsv_var; 5606 } 5607 5608 dvar = dtrace_dynvar(dstate, nkeys, key, 5609 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5610 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5611 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5612 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5613 5614 if (dvar == NULL) 5615 break; 5616 5617 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5618 if (!dtrace_vcanload( 5619 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5620 mstate, vstate)) 5621 break; 5622 5623 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5624 dvar->dtdv_data, &v->dtdv_type); 5625 } else { 5626 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5627 } 5628 5629 break; 5630 } 5631 5632 case DIF_OP_ALLOCS: { 5633 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5634 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 5635 5636 /* 5637 * Rounding up the user allocation size could have 5638 * overflowed large, bogus allocations (like -1ULL) to 5639 * 0. 5640 */ 5641 if (size < regs[r1] || 5642 !DTRACE_INSCRATCH(mstate, size)) { 5643 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5644 regs[rd] = 0; 5645 break; 5646 } 5647 5648 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 5649 mstate->dtms_scratch_ptr += size; 5650 regs[rd] = ptr; 5651 break; 5652 } 5653 5654 case DIF_OP_COPYS: 5655 if (!dtrace_canstore(regs[rd], regs[r2], 5656 mstate, vstate)) { 5657 *flags |= CPU_DTRACE_BADADDR; 5658 *illval = regs[rd]; 5659 break; 5660 } 5661 5662 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 5663 break; 5664 5665 dtrace_bcopy((void *)(uintptr_t)regs[r1], 5666 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 5667 break; 5668 5669 case DIF_OP_STB: 5670 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 5671 *flags |= CPU_DTRACE_BADADDR; 5672 *illval = regs[rd]; 5673 break; 5674 } 5675 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 5676 break; 5677 5678 case DIF_OP_STH: 5679 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 5680 *flags |= CPU_DTRACE_BADADDR; 5681 *illval = regs[rd]; 5682 break; 5683 } 5684 if (regs[rd] & 1) { 5685 *flags |= CPU_DTRACE_BADALIGN; 5686 *illval = regs[rd]; 5687 break; 5688 } 5689 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 5690 break; 5691 5692 case DIF_OP_STW: 5693 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 5694 *flags |= CPU_DTRACE_BADADDR; 5695 *illval = regs[rd]; 5696 break; 5697 } 5698 if (regs[rd] & 3) { 5699 *flags |= CPU_DTRACE_BADALIGN; 5700 *illval = regs[rd]; 5701 break; 5702 } 5703 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 5704 break; 5705 5706 case DIF_OP_STX: 5707 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 5708 *flags |= CPU_DTRACE_BADADDR; 5709 *illval = regs[rd]; 5710 break; 5711 } 5712 if (regs[rd] & 7) { 5713 *flags |= CPU_DTRACE_BADALIGN; 5714 *illval = regs[rd]; 5715 break; 5716 } 5717 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 5718 break; 5719 } 5720 } 5721 5722 if (!(*flags & CPU_DTRACE_FAULT)) 5723 return (rval); 5724 5725 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 5726 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 5727 5728 return (0); 5729 } 5730 5731 static void 5732 dtrace_action_breakpoint(dtrace_ecb_t *ecb) 5733 { 5734 dtrace_probe_t *probe = ecb->dte_probe; 5735 dtrace_provider_t *prov = probe->dtpr_provider; 5736 char c[DTRACE_FULLNAMELEN + 80], *str; 5737 char *msg = "dtrace: breakpoint action at probe "; 5738 char *ecbmsg = " (ecb "; 5739 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 5740 uintptr_t val = (uintptr_t)ecb; 5741 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 5742 5743 if (dtrace_destructive_disallow) 5744 return; 5745 5746 /* 5747 * It's impossible to be taking action on the NULL probe. 5748 */ 5749 ASSERT(probe != NULL); 5750 5751 /* 5752 * This is a poor man's (destitute man's?) sprintf(): we want to 5753 * print the provider name, module name, function name and name of 5754 * the probe, along with the hex address of the ECB with the breakpoint 5755 * action -- all of which we must place in the character buffer by 5756 * hand. 5757 */ 5758 while (*msg != '\0') 5759 c[i++] = *msg++; 5760 5761 for (str = prov->dtpv_name; *str != '\0'; str++) 5762 c[i++] = *str; 5763 c[i++] = ':'; 5764 5765 for (str = probe->dtpr_mod; *str != '\0'; str++) 5766 c[i++] = *str; 5767 c[i++] = ':'; 5768 5769 for (str = probe->dtpr_func; *str != '\0'; str++) 5770 c[i++] = *str; 5771 c[i++] = ':'; 5772 5773 for (str = probe->dtpr_name; *str != '\0'; str++) 5774 c[i++] = *str; 5775 5776 while (*ecbmsg != '\0') 5777 c[i++] = *ecbmsg++; 5778 5779 while (shift >= 0) { 5780 mask = (uintptr_t)0xf << shift; 5781 5782 if (val >= ((uintptr_t)1 << shift)) 5783 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 5784 shift -= 4; 5785 } 5786 5787 c[i++] = ')'; 5788 c[i] = '\0'; 5789 5790 #if defined(sun) 5791 debug_enter(c); 5792 #else 5793 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 5794 #endif 5795 } 5796 5797 static void 5798 dtrace_action_panic(dtrace_ecb_t *ecb) 5799 { 5800 dtrace_probe_t *probe = ecb->dte_probe; 5801 5802 /* 5803 * It's impossible to be taking action on the NULL probe. 5804 */ 5805 ASSERT(probe != NULL); 5806 5807 if (dtrace_destructive_disallow) 5808 return; 5809 5810 if (dtrace_panicked != NULL) 5811 return; 5812 5813 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 5814 return; 5815 5816 /* 5817 * We won the right to panic. (We want to be sure that only one 5818 * thread calls panic() from dtrace_probe(), and that panic() is 5819 * called exactly once.) 5820 */ 5821 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 5822 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 5823 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 5824 } 5825 5826 static void 5827 dtrace_action_raise(uint64_t sig) 5828 { 5829 if (dtrace_destructive_disallow) 5830 return; 5831 5832 if (sig >= NSIG) { 5833 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5834 return; 5835 } 5836 5837 #if defined(sun) 5838 /* 5839 * raise() has a queue depth of 1 -- we ignore all subsequent 5840 * invocations of the raise() action. 5841 */ 5842 if (curthread->t_dtrace_sig == 0) 5843 curthread->t_dtrace_sig = (uint8_t)sig; 5844 5845 curthread->t_sig_check = 1; 5846 aston(curthread); 5847 #else 5848 struct proc *p = curproc; 5849 PROC_LOCK(p); 5850 kern_psignal(p, sig); 5851 PROC_UNLOCK(p); 5852 #endif 5853 } 5854 5855 static void 5856 dtrace_action_stop(void) 5857 { 5858 if (dtrace_destructive_disallow) 5859 return; 5860 5861 #if defined(sun) 5862 if (!curthread->t_dtrace_stop) { 5863 curthread->t_dtrace_stop = 1; 5864 curthread->t_sig_check = 1; 5865 aston(curthread); 5866 } 5867 #else 5868 struct proc *p = curproc; 5869 PROC_LOCK(p); 5870 kern_psignal(p, SIGSTOP); 5871 PROC_UNLOCK(p); 5872 #endif 5873 } 5874 5875 static void 5876 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 5877 { 5878 hrtime_t now; 5879 volatile uint16_t *flags; 5880 #if defined(sun) 5881 cpu_t *cpu = CPU; 5882 #else 5883 cpu_t *cpu = &solaris_cpu[curcpu]; 5884 #endif 5885 5886 if (dtrace_destructive_disallow) 5887 return; 5888 5889 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 5890 5891 now = dtrace_gethrtime(); 5892 5893 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 5894 /* 5895 * We need to advance the mark to the current time. 5896 */ 5897 cpu->cpu_dtrace_chillmark = now; 5898 cpu->cpu_dtrace_chilled = 0; 5899 } 5900 5901 /* 5902 * Now check to see if the requested chill time would take us over 5903 * the maximum amount of time allowed in the chill interval. (Or 5904 * worse, if the calculation itself induces overflow.) 5905 */ 5906 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 5907 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 5908 *flags |= CPU_DTRACE_ILLOP; 5909 return; 5910 } 5911 5912 while (dtrace_gethrtime() - now < val) 5913 continue; 5914 5915 /* 5916 * Normally, we assure that the value of the variable "timestamp" does 5917 * not change within an ECB. The presence of chill() represents an 5918 * exception to this rule, however. 5919 */ 5920 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 5921 cpu->cpu_dtrace_chilled += val; 5922 } 5923 5924 static void 5925 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 5926 uint64_t *buf, uint64_t arg) 5927 { 5928 int nframes = DTRACE_USTACK_NFRAMES(arg); 5929 int strsize = DTRACE_USTACK_STRSIZE(arg); 5930 uint64_t *pcs = &buf[1], *fps; 5931 char *str = (char *)&pcs[nframes]; 5932 int size, offs = 0, i, j; 5933 uintptr_t old = mstate->dtms_scratch_ptr, saved; 5934 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 5935 char *sym; 5936 5937 /* 5938 * Should be taking a faster path if string space has not been 5939 * allocated. 5940 */ 5941 ASSERT(strsize != 0); 5942 5943 /* 5944 * We will first allocate some temporary space for the frame pointers. 5945 */ 5946 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5947 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 5948 (nframes * sizeof (uint64_t)); 5949 5950 if (!DTRACE_INSCRATCH(mstate, size)) { 5951 /* 5952 * Not enough room for our frame pointers -- need to indicate 5953 * that we ran out of scratch space. 5954 */ 5955 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5956 return; 5957 } 5958 5959 mstate->dtms_scratch_ptr += size; 5960 saved = mstate->dtms_scratch_ptr; 5961 5962 /* 5963 * Now get a stack with both program counters and frame pointers. 5964 */ 5965 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5966 dtrace_getufpstack(buf, fps, nframes + 1); 5967 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5968 5969 /* 5970 * If that faulted, we're cooked. 5971 */ 5972 if (*flags & CPU_DTRACE_FAULT) 5973 goto out; 5974 5975 /* 5976 * Now we want to walk up the stack, calling the USTACK helper. For 5977 * each iteration, we restore the scratch pointer. 5978 */ 5979 for (i = 0; i < nframes; i++) { 5980 mstate->dtms_scratch_ptr = saved; 5981 5982 if (offs >= strsize) 5983 break; 5984 5985 sym = (char *)(uintptr_t)dtrace_helper( 5986 DTRACE_HELPER_ACTION_USTACK, 5987 mstate, state, pcs[i], fps[i]); 5988 5989 /* 5990 * If we faulted while running the helper, we're going to 5991 * clear the fault and null out the corresponding string. 5992 */ 5993 if (*flags & CPU_DTRACE_FAULT) { 5994 *flags &= ~CPU_DTRACE_FAULT; 5995 str[offs++] = '\0'; 5996 continue; 5997 } 5998 5999 if (sym == NULL) { 6000 str[offs++] = '\0'; 6001 continue; 6002 } 6003 6004 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6005 6006 /* 6007 * Now copy in the string that the helper returned to us. 6008 */ 6009 for (j = 0; offs + j < strsize; j++) { 6010 if ((str[offs + j] = sym[j]) == '\0') 6011 break; 6012 } 6013 6014 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6015 6016 offs += j + 1; 6017 } 6018 6019 if (offs >= strsize) { 6020 /* 6021 * If we didn't have room for all of the strings, we don't 6022 * abort processing -- this needn't be a fatal error -- but we 6023 * still want to increment a counter (dts_stkstroverflows) to 6024 * allow this condition to be warned about. (If this is from 6025 * a jstack() action, it is easily tuned via jstackstrsize.) 6026 */ 6027 dtrace_error(&state->dts_stkstroverflows); 6028 } 6029 6030 while (offs < strsize) 6031 str[offs++] = '\0'; 6032 6033 out: 6034 mstate->dtms_scratch_ptr = old; 6035 } 6036 6037 /* 6038 * If you're looking for the epicenter of DTrace, you just found it. This 6039 * is the function called by the provider to fire a probe -- from which all 6040 * subsequent probe-context DTrace activity emanates. 6041 */ 6042 void 6043 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 6044 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 6045 { 6046 processorid_t cpuid; 6047 dtrace_icookie_t cookie; 6048 dtrace_probe_t *probe; 6049 dtrace_mstate_t mstate; 6050 dtrace_ecb_t *ecb; 6051 dtrace_action_t *act; 6052 intptr_t offs; 6053 size_t size; 6054 int vtime, onintr; 6055 volatile uint16_t *flags; 6056 hrtime_t now; 6057 6058 if (panicstr != NULL) 6059 return; 6060 6061 #if defined(sun) 6062 /* 6063 * Kick out immediately if this CPU is still being born (in which case 6064 * curthread will be set to -1) or the current thread can't allow 6065 * probes in its current context. 6066 */ 6067 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 6068 return; 6069 #endif 6070 6071 cookie = dtrace_interrupt_disable(); 6072 probe = dtrace_probes[id - 1]; 6073 cpuid = curcpu; 6074 onintr = CPU_ON_INTR(CPU); 6075 6076 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 6077 probe->dtpr_predcache == curthread->t_predcache) { 6078 /* 6079 * We have hit in the predicate cache; we know that 6080 * this predicate would evaluate to be false. 6081 */ 6082 dtrace_interrupt_enable(cookie); 6083 return; 6084 } 6085 6086 #if defined(sun) 6087 if (panic_quiesce) { 6088 #else 6089 if (panicstr != NULL) { 6090 #endif 6091 /* 6092 * We don't trace anything if we're panicking. 6093 */ 6094 dtrace_interrupt_enable(cookie); 6095 return; 6096 } 6097 6098 now = dtrace_gethrtime(); 6099 vtime = dtrace_vtime_references != 0; 6100 6101 if (vtime && curthread->t_dtrace_start) 6102 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 6103 6104 mstate.dtms_difo = NULL; 6105 mstate.dtms_probe = probe; 6106 mstate.dtms_strtok = 0; 6107 mstate.dtms_arg[0] = arg0; 6108 mstate.dtms_arg[1] = arg1; 6109 mstate.dtms_arg[2] = arg2; 6110 mstate.dtms_arg[3] = arg3; 6111 mstate.dtms_arg[4] = arg4; 6112 6113 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 6114 6115 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 6116 dtrace_predicate_t *pred = ecb->dte_predicate; 6117 dtrace_state_t *state = ecb->dte_state; 6118 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 6119 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 6120 dtrace_vstate_t *vstate = &state->dts_vstate; 6121 dtrace_provider_t *prov = probe->dtpr_provider; 6122 uint64_t tracememsize = 0; 6123 int committed = 0; 6124 caddr_t tomax; 6125 6126 /* 6127 * A little subtlety with the following (seemingly innocuous) 6128 * declaration of the automatic 'val': by looking at the 6129 * code, you might think that it could be declared in the 6130 * action processing loop, below. (That is, it's only used in 6131 * the action processing loop.) However, it must be declared 6132 * out of that scope because in the case of DIF expression 6133 * arguments to aggregating actions, one iteration of the 6134 * action loop will use the last iteration's value. 6135 */ 6136 uint64_t val = 0; 6137 6138 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 6139 *flags &= ~CPU_DTRACE_ERROR; 6140 6141 if (prov == dtrace_provider) { 6142 /* 6143 * If dtrace itself is the provider of this probe, 6144 * we're only going to continue processing the ECB if 6145 * arg0 (the dtrace_state_t) is equal to the ECB's 6146 * creating state. (This prevents disjoint consumers 6147 * from seeing one another's metaprobes.) 6148 */ 6149 if (arg0 != (uint64_t)(uintptr_t)state) 6150 continue; 6151 } 6152 6153 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 6154 /* 6155 * We're not currently active. If our provider isn't 6156 * the dtrace pseudo provider, we're not interested. 6157 */ 6158 if (prov != dtrace_provider) 6159 continue; 6160 6161 /* 6162 * Now we must further check if we are in the BEGIN 6163 * probe. If we are, we will only continue processing 6164 * if we're still in WARMUP -- if one BEGIN enabling 6165 * has invoked the exit() action, we don't want to 6166 * evaluate subsequent BEGIN enablings. 6167 */ 6168 if (probe->dtpr_id == dtrace_probeid_begin && 6169 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 6170 ASSERT(state->dts_activity == 6171 DTRACE_ACTIVITY_DRAINING); 6172 continue; 6173 } 6174 } 6175 6176 if (ecb->dte_cond) { 6177 /* 6178 * If the dte_cond bits indicate that this 6179 * consumer is only allowed to see user-mode firings 6180 * of this probe, call the provider's dtps_usermode() 6181 * entry point to check that the probe was fired 6182 * while in a user context. Skip this ECB if that's 6183 * not the case. 6184 */ 6185 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 6186 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 6187 probe->dtpr_id, probe->dtpr_arg) == 0) 6188 continue; 6189 6190 #if defined(sun) 6191 /* 6192 * This is more subtle than it looks. We have to be 6193 * absolutely certain that CRED() isn't going to 6194 * change out from under us so it's only legit to 6195 * examine that structure if we're in constrained 6196 * situations. Currently, the only times we'll this 6197 * check is if a non-super-user has enabled the 6198 * profile or syscall providers -- providers that 6199 * allow visibility of all processes. For the 6200 * profile case, the check above will ensure that 6201 * we're examining a user context. 6202 */ 6203 if (ecb->dte_cond & DTRACE_COND_OWNER) { 6204 cred_t *cr; 6205 cred_t *s_cr = 6206 ecb->dte_state->dts_cred.dcr_cred; 6207 proc_t *proc; 6208 6209 ASSERT(s_cr != NULL); 6210 6211 if ((cr = CRED()) == NULL || 6212 s_cr->cr_uid != cr->cr_uid || 6213 s_cr->cr_uid != cr->cr_ruid || 6214 s_cr->cr_uid != cr->cr_suid || 6215 s_cr->cr_gid != cr->cr_gid || 6216 s_cr->cr_gid != cr->cr_rgid || 6217 s_cr->cr_gid != cr->cr_sgid || 6218 (proc = ttoproc(curthread)) == NULL || 6219 (proc->p_flag & SNOCD)) 6220 continue; 6221 } 6222 6223 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 6224 cred_t *cr; 6225 cred_t *s_cr = 6226 ecb->dte_state->dts_cred.dcr_cred; 6227 6228 ASSERT(s_cr != NULL); 6229 6230 if ((cr = CRED()) == NULL || 6231 s_cr->cr_zone->zone_id != 6232 cr->cr_zone->zone_id) 6233 continue; 6234 } 6235 #endif 6236 } 6237 6238 if (now - state->dts_alive > dtrace_deadman_timeout) { 6239 /* 6240 * We seem to be dead. Unless we (a) have kernel 6241 * destructive permissions (b) have explicitly enabled 6242 * destructive actions and (c) destructive actions have 6243 * not been disabled, we're going to transition into 6244 * the KILLED state, from which no further processing 6245 * on this state will be performed. 6246 */ 6247 if (!dtrace_priv_kernel_destructive(state) || 6248 !state->dts_cred.dcr_destructive || 6249 dtrace_destructive_disallow) { 6250 void *activity = &state->dts_activity; 6251 dtrace_activity_t current; 6252 6253 do { 6254 current = state->dts_activity; 6255 } while (dtrace_cas32(activity, current, 6256 DTRACE_ACTIVITY_KILLED) != current); 6257 6258 continue; 6259 } 6260 } 6261 6262 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 6263 ecb->dte_alignment, state, &mstate)) < 0) 6264 continue; 6265 6266 tomax = buf->dtb_tomax; 6267 ASSERT(tomax != NULL); 6268 6269 if (ecb->dte_size != 0) { 6270 dtrace_rechdr_t dtrh; 6271 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 6272 mstate.dtms_timestamp = dtrace_gethrtime(); 6273 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 6274 } 6275 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 6276 dtrh.dtrh_epid = ecb->dte_epid; 6277 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 6278 mstate.dtms_timestamp); 6279 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 6280 } 6281 6282 mstate.dtms_epid = ecb->dte_epid; 6283 mstate.dtms_present |= DTRACE_MSTATE_EPID; 6284 6285 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 6286 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 6287 else 6288 mstate.dtms_access = 0; 6289 6290 if (pred != NULL) { 6291 dtrace_difo_t *dp = pred->dtp_difo; 6292 int rval; 6293 6294 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 6295 6296 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 6297 dtrace_cacheid_t cid = probe->dtpr_predcache; 6298 6299 if (cid != DTRACE_CACHEIDNONE && !onintr) { 6300 /* 6301 * Update the predicate cache... 6302 */ 6303 ASSERT(cid == pred->dtp_cacheid); 6304 curthread->t_predcache = cid; 6305 } 6306 6307 continue; 6308 } 6309 } 6310 6311 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 6312 act != NULL; act = act->dta_next) { 6313 size_t valoffs; 6314 dtrace_difo_t *dp; 6315 dtrace_recdesc_t *rec = &act->dta_rec; 6316 6317 size = rec->dtrd_size; 6318 valoffs = offs + rec->dtrd_offset; 6319 6320 if (DTRACEACT_ISAGG(act->dta_kind)) { 6321 uint64_t v = 0xbad; 6322 dtrace_aggregation_t *agg; 6323 6324 agg = (dtrace_aggregation_t *)act; 6325 6326 if ((dp = act->dta_difo) != NULL) 6327 v = dtrace_dif_emulate(dp, 6328 &mstate, vstate, state); 6329 6330 if (*flags & CPU_DTRACE_ERROR) 6331 continue; 6332 6333 /* 6334 * Note that we always pass the expression 6335 * value from the previous iteration of the 6336 * action loop. This value will only be used 6337 * if there is an expression argument to the 6338 * aggregating action, denoted by the 6339 * dtag_hasarg field. 6340 */ 6341 dtrace_aggregate(agg, buf, 6342 offs, aggbuf, v, val); 6343 continue; 6344 } 6345 6346 switch (act->dta_kind) { 6347 case DTRACEACT_STOP: 6348 if (dtrace_priv_proc_destructive(state)) 6349 dtrace_action_stop(); 6350 continue; 6351 6352 case DTRACEACT_BREAKPOINT: 6353 if (dtrace_priv_kernel_destructive(state)) 6354 dtrace_action_breakpoint(ecb); 6355 continue; 6356 6357 case DTRACEACT_PANIC: 6358 if (dtrace_priv_kernel_destructive(state)) 6359 dtrace_action_panic(ecb); 6360 continue; 6361 6362 case DTRACEACT_STACK: 6363 if (!dtrace_priv_kernel(state)) 6364 continue; 6365 6366 dtrace_getpcstack((pc_t *)(tomax + valoffs), 6367 size / sizeof (pc_t), probe->dtpr_aframes, 6368 DTRACE_ANCHORED(probe) ? NULL : 6369 (uint32_t *)arg0); 6370 continue; 6371 6372 case DTRACEACT_JSTACK: 6373 case DTRACEACT_USTACK: 6374 if (!dtrace_priv_proc(state)) 6375 continue; 6376 6377 /* 6378 * See comment in DIF_VAR_PID. 6379 */ 6380 if (DTRACE_ANCHORED(mstate.dtms_probe) && 6381 CPU_ON_INTR(CPU)) { 6382 int depth = DTRACE_USTACK_NFRAMES( 6383 rec->dtrd_arg) + 1; 6384 6385 dtrace_bzero((void *)(tomax + valoffs), 6386 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 6387 + depth * sizeof (uint64_t)); 6388 6389 continue; 6390 } 6391 6392 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 6393 curproc->p_dtrace_helpers != NULL) { 6394 /* 6395 * This is the slow path -- we have 6396 * allocated string space, and we're 6397 * getting the stack of a process that 6398 * has helpers. Call into a separate 6399 * routine to perform this processing. 6400 */ 6401 dtrace_action_ustack(&mstate, state, 6402 (uint64_t *)(tomax + valoffs), 6403 rec->dtrd_arg); 6404 continue; 6405 } 6406 6407 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6408 dtrace_getupcstack((uint64_t *) 6409 (tomax + valoffs), 6410 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 6411 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6412 continue; 6413 6414 default: 6415 break; 6416 } 6417 6418 dp = act->dta_difo; 6419 ASSERT(dp != NULL); 6420 6421 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 6422 6423 if (*flags & CPU_DTRACE_ERROR) 6424 continue; 6425 6426 switch (act->dta_kind) { 6427 case DTRACEACT_SPECULATE: { 6428 dtrace_rechdr_t *dtrh; 6429 6430 ASSERT(buf == &state->dts_buffer[cpuid]); 6431 buf = dtrace_speculation_buffer(state, 6432 cpuid, val); 6433 6434 if (buf == NULL) { 6435 *flags |= CPU_DTRACE_DROP; 6436 continue; 6437 } 6438 6439 offs = dtrace_buffer_reserve(buf, 6440 ecb->dte_needed, ecb->dte_alignment, 6441 state, NULL); 6442 6443 if (offs < 0) { 6444 *flags |= CPU_DTRACE_DROP; 6445 continue; 6446 } 6447 6448 tomax = buf->dtb_tomax; 6449 ASSERT(tomax != NULL); 6450 6451 if (ecb->dte_size == 0) 6452 continue; 6453 6454 ASSERT3U(ecb->dte_size, >=, 6455 sizeof (dtrace_rechdr_t)); 6456 dtrh = ((void *)(tomax + offs)); 6457 dtrh->dtrh_epid = ecb->dte_epid; 6458 /* 6459 * When the speculation is committed, all of 6460 * the records in the speculative buffer will 6461 * have their timestamps set to the commit 6462 * time. Until then, it is set to a sentinel 6463 * value, for debugability. 6464 */ 6465 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 6466 continue; 6467 } 6468 6469 case DTRACEACT_PRINTM: { 6470 /* The DIF returns a 'memref'. */ 6471 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 6472 6473 /* Get the size from the memref. */ 6474 size = memref[1]; 6475 6476 /* 6477 * Check if the size exceeds the allocated 6478 * buffer size. 6479 */ 6480 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 6481 /* Flag a drop! */ 6482 *flags |= CPU_DTRACE_DROP; 6483 continue; 6484 } 6485 6486 /* Store the size in the buffer first. */ 6487 DTRACE_STORE(uintptr_t, tomax, 6488 valoffs, size); 6489 6490 /* 6491 * Offset the buffer address to the start 6492 * of the data. 6493 */ 6494 valoffs += sizeof(uintptr_t); 6495 6496 /* 6497 * Reset to the memory address rather than 6498 * the memref array, then let the BYREF 6499 * code below do the work to store the 6500 * memory data in the buffer. 6501 */ 6502 val = memref[0]; 6503 break; 6504 } 6505 6506 case DTRACEACT_PRINTT: { 6507 /* The DIF returns a 'typeref'. */ 6508 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 6509 char c = '\0' + 1; 6510 size_t s; 6511 6512 /* 6513 * Get the type string length and round it 6514 * up so that the data that follows is 6515 * aligned for easy access. 6516 */ 6517 size_t typs = strlen((char *) typeref[2]) + 1; 6518 typs = roundup(typs, sizeof(uintptr_t)); 6519 6520 /* 6521 *Get the size from the typeref using the 6522 * number of elements and the type size. 6523 */ 6524 size = typeref[1] * typeref[3]; 6525 6526 /* 6527 * Check if the size exceeds the allocated 6528 * buffer size. 6529 */ 6530 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 6531 /* Flag a drop! */ 6532 *flags |= CPU_DTRACE_DROP; 6533 6534 } 6535 6536 /* Store the size in the buffer first. */ 6537 DTRACE_STORE(uintptr_t, tomax, 6538 valoffs, size); 6539 valoffs += sizeof(uintptr_t); 6540 6541 /* Store the type size in the buffer. */ 6542 DTRACE_STORE(uintptr_t, tomax, 6543 valoffs, typeref[3]); 6544 valoffs += sizeof(uintptr_t); 6545 6546 val = typeref[2]; 6547 6548 for (s = 0; s < typs; s++) { 6549 if (c != '\0') 6550 c = dtrace_load8(val++); 6551 6552 DTRACE_STORE(uint8_t, tomax, 6553 valoffs++, c); 6554 } 6555 6556 /* 6557 * Reset to the memory address rather than 6558 * the typeref array, then let the BYREF 6559 * code below do the work to store the 6560 * memory data in the buffer. 6561 */ 6562 val = typeref[0]; 6563 break; 6564 } 6565 6566 case DTRACEACT_CHILL: 6567 if (dtrace_priv_kernel_destructive(state)) 6568 dtrace_action_chill(&mstate, val); 6569 continue; 6570 6571 case DTRACEACT_RAISE: 6572 if (dtrace_priv_proc_destructive(state)) 6573 dtrace_action_raise(val); 6574 continue; 6575 6576 case DTRACEACT_COMMIT: 6577 ASSERT(!committed); 6578 6579 /* 6580 * We need to commit our buffer state. 6581 */ 6582 if (ecb->dte_size) 6583 buf->dtb_offset = offs + ecb->dte_size; 6584 buf = &state->dts_buffer[cpuid]; 6585 dtrace_speculation_commit(state, cpuid, val); 6586 committed = 1; 6587 continue; 6588 6589 case DTRACEACT_DISCARD: 6590 dtrace_speculation_discard(state, cpuid, val); 6591 continue; 6592 6593 case DTRACEACT_DIFEXPR: 6594 case DTRACEACT_LIBACT: 6595 case DTRACEACT_PRINTF: 6596 case DTRACEACT_PRINTA: 6597 case DTRACEACT_SYSTEM: 6598 case DTRACEACT_FREOPEN: 6599 case DTRACEACT_TRACEMEM: 6600 break; 6601 6602 case DTRACEACT_TRACEMEM_DYNSIZE: 6603 tracememsize = val; 6604 break; 6605 6606 case DTRACEACT_SYM: 6607 case DTRACEACT_MOD: 6608 if (!dtrace_priv_kernel(state)) 6609 continue; 6610 break; 6611 6612 case DTRACEACT_USYM: 6613 case DTRACEACT_UMOD: 6614 case DTRACEACT_UADDR: { 6615 #if defined(sun) 6616 struct pid *pid = curthread->t_procp->p_pidp; 6617 #endif 6618 6619 if (!dtrace_priv_proc(state)) 6620 continue; 6621 6622 DTRACE_STORE(uint64_t, tomax, 6623 #if defined(sun) 6624 valoffs, (uint64_t)pid->pid_id); 6625 #else 6626 valoffs, (uint64_t) curproc->p_pid); 6627 #endif 6628 DTRACE_STORE(uint64_t, tomax, 6629 valoffs + sizeof (uint64_t), val); 6630 6631 continue; 6632 } 6633 6634 case DTRACEACT_EXIT: { 6635 /* 6636 * For the exit action, we are going to attempt 6637 * to atomically set our activity to be 6638 * draining. If this fails (either because 6639 * another CPU has beat us to the exit action, 6640 * or because our current activity is something 6641 * other than ACTIVE or WARMUP), we will 6642 * continue. This assures that the exit action 6643 * can be successfully recorded at most once 6644 * when we're in the ACTIVE state. If we're 6645 * encountering the exit() action while in 6646 * COOLDOWN, however, we want to honor the new 6647 * status code. (We know that we're the only 6648 * thread in COOLDOWN, so there is no race.) 6649 */ 6650 void *activity = &state->dts_activity; 6651 dtrace_activity_t current = state->dts_activity; 6652 6653 if (current == DTRACE_ACTIVITY_COOLDOWN) 6654 break; 6655 6656 if (current != DTRACE_ACTIVITY_WARMUP) 6657 current = DTRACE_ACTIVITY_ACTIVE; 6658 6659 if (dtrace_cas32(activity, current, 6660 DTRACE_ACTIVITY_DRAINING) != current) { 6661 *flags |= CPU_DTRACE_DROP; 6662 continue; 6663 } 6664 6665 break; 6666 } 6667 6668 default: 6669 ASSERT(0); 6670 } 6671 6672 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 6673 uintptr_t end = valoffs + size; 6674 6675 if (tracememsize != 0 && 6676 valoffs + tracememsize < end) { 6677 end = valoffs + tracememsize; 6678 tracememsize = 0; 6679 } 6680 6681 if (!dtrace_vcanload((void *)(uintptr_t)val, 6682 &dp->dtdo_rtype, &mstate, vstate)) 6683 continue; 6684 6685 /* 6686 * If this is a string, we're going to only 6687 * load until we find the zero byte -- after 6688 * which we'll store zero bytes. 6689 */ 6690 if (dp->dtdo_rtype.dtdt_kind == 6691 DIF_TYPE_STRING) { 6692 char c = '\0' + 1; 6693 int intuple = act->dta_intuple; 6694 size_t s; 6695 6696 for (s = 0; s < size; s++) { 6697 if (c != '\0') 6698 c = dtrace_load8(val++); 6699 6700 DTRACE_STORE(uint8_t, tomax, 6701 valoffs++, c); 6702 6703 if (c == '\0' && intuple) 6704 break; 6705 } 6706 6707 continue; 6708 } 6709 6710 while (valoffs < end) { 6711 DTRACE_STORE(uint8_t, tomax, valoffs++, 6712 dtrace_load8(val++)); 6713 } 6714 6715 continue; 6716 } 6717 6718 switch (size) { 6719 case 0: 6720 break; 6721 6722 case sizeof (uint8_t): 6723 DTRACE_STORE(uint8_t, tomax, valoffs, val); 6724 break; 6725 case sizeof (uint16_t): 6726 DTRACE_STORE(uint16_t, tomax, valoffs, val); 6727 break; 6728 case sizeof (uint32_t): 6729 DTRACE_STORE(uint32_t, tomax, valoffs, val); 6730 break; 6731 case sizeof (uint64_t): 6732 DTRACE_STORE(uint64_t, tomax, valoffs, val); 6733 break; 6734 default: 6735 /* 6736 * Any other size should have been returned by 6737 * reference, not by value. 6738 */ 6739 ASSERT(0); 6740 break; 6741 } 6742 } 6743 6744 if (*flags & CPU_DTRACE_DROP) 6745 continue; 6746 6747 if (*flags & CPU_DTRACE_FAULT) { 6748 int ndx; 6749 dtrace_action_t *err; 6750 6751 buf->dtb_errors++; 6752 6753 if (probe->dtpr_id == dtrace_probeid_error) { 6754 /* 6755 * There's nothing we can do -- we had an 6756 * error on the error probe. We bump an 6757 * error counter to at least indicate that 6758 * this condition happened. 6759 */ 6760 dtrace_error(&state->dts_dblerrors); 6761 continue; 6762 } 6763 6764 if (vtime) { 6765 /* 6766 * Before recursing on dtrace_probe(), we 6767 * need to explicitly clear out our start 6768 * time to prevent it from being accumulated 6769 * into t_dtrace_vtime. 6770 */ 6771 curthread->t_dtrace_start = 0; 6772 } 6773 6774 /* 6775 * Iterate over the actions to figure out which action 6776 * we were processing when we experienced the error. 6777 * Note that act points _past_ the faulting action; if 6778 * act is ecb->dte_action, the fault was in the 6779 * predicate, if it's ecb->dte_action->dta_next it's 6780 * in action #1, and so on. 6781 */ 6782 for (err = ecb->dte_action, ndx = 0; 6783 err != act; err = err->dta_next, ndx++) 6784 continue; 6785 6786 dtrace_probe_error(state, ecb->dte_epid, ndx, 6787 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 6788 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 6789 cpu_core[cpuid].cpuc_dtrace_illval); 6790 6791 continue; 6792 } 6793 6794 if (!committed) 6795 buf->dtb_offset = offs + ecb->dte_size; 6796 } 6797 6798 if (vtime) 6799 curthread->t_dtrace_start = dtrace_gethrtime(); 6800 6801 dtrace_interrupt_enable(cookie); 6802 } 6803 6804 /* 6805 * DTrace Probe Hashing Functions 6806 * 6807 * The functions in this section (and indeed, the functions in remaining 6808 * sections) are not _called_ from probe context. (Any exceptions to this are 6809 * marked with a "Note:".) Rather, they are called from elsewhere in the 6810 * DTrace framework to look-up probes in, add probes to and remove probes from 6811 * the DTrace probe hashes. (Each probe is hashed by each element of the 6812 * probe tuple -- allowing for fast lookups, regardless of what was 6813 * specified.) 6814 */ 6815 static uint_t 6816 dtrace_hash_str(const char *p) 6817 { 6818 unsigned int g; 6819 uint_t hval = 0; 6820 6821 while (*p) { 6822 hval = (hval << 4) + *p++; 6823 if ((g = (hval & 0xf0000000)) != 0) 6824 hval ^= g >> 24; 6825 hval &= ~g; 6826 } 6827 return (hval); 6828 } 6829 6830 static dtrace_hash_t * 6831 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 6832 { 6833 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 6834 6835 hash->dth_stroffs = stroffs; 6836 hash->dth_nextoffs = nextoffs; 6837 hash->dth_prevoffs = prevoffs; 6838 6839 hash->dth_size = 1; 6840 hash->dth_mask = hash->dth_size - 1; 6841 6842 hash->dth_tab = kmem_zalloc(hash->dth_size * 6843 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 6844 6845 return (hash); 6846 } 6847 6848 static void 6849 dtrace_hash_destroy(dtrace_hash_t *hash) 6850 { 6851 #ifdef DEBUG 6852 int i; 6853 6854 for (i = 0; i < hash->dth_size; i++) 6855 ASSERT(hash->dth_tab[i] == NULL); 6856 #endif 6857 6858 kmem_free(hash->dth_tab, 6859 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 6860 kmem_free(hash, sizeof (dtrace_hash_t)); 6861 } 6862 6863 static void 6864 dtrace_hash_resize(dtrace_hash_t *hash) 6865 { 6866 int size = hash->dth_size, i, ndx; 6867 int new_size = hash->dth_size << 1; 6868 int new_mask = new_size - 1; 6869 dtrace_hashbucket_t **new_tab, *bucket, *next; 6870 6871 ASSERT((new_size & new_mask) == 0); 6872 6873 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 6874 6875 for (i = 0; i < size; i++) { 6876 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 6877 dtrace_probe_t *probe = bucket->dthb_chain; 6878 6879 ASSERT(probe != NULL); 6880 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 6881 6882 next = bucket->dthb_next; 6883 bucket->dthb_next = new_tab[ndx]; 6884 new_tab[ndx] = bucket; 6885 } 6886 } 6887 6888 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 6889 hash->dth_tab = new_tab; 6890 hash->dth_size = new_size; 6891 hash->dth_mask = new_mask; 6892 } 6893 6894 static void 6895 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 6896 { 6897 int hashval = DTRACE_HASHSTR(hash, new); 6898 int ndx = hashval & hash->dth_mask; 6899 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6900 dtrace_probe_t **nextp, **prevp; 6901 6902 for (; bucket != NULL; bucket = bucket->dthb_next) { 6903 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 6904 goto add; 6905 } 6906 6907 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 6908 dtrace_hash_resize(hash); 6909 dtrace_hash_add(hash, new); 6910 return; 6911 } 6912 6913 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 6914 bucket->dthb_next = hash->dth_tab[ndx]; 6915 hash->dth_tab[ndx] = bucket; 6916 hash->dth_nbuckets++; 6917 6918 add: 6919 nextp = DTRACE_HASHNEXT(hash, new); 6920 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 6921 *nextp = bucket->dthb_chain; 6922 6923 if (bucket->dthb_chain != NULL) { 6924 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 6925 ASSERT(*prevp == NULL); 6926 *prevp = new; 6927 } 6928 6929 bucket->dthb_chain = new; 6930 bucket->dthb_len++; 6931 } 6932 6933 static dtrace_probe_t * 6934 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 6935 { 6936 int hashval = DTRACE_HASHSTR(hash, template); 6937 int ndx = hashval & hash->dth_mask; 6938 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6939 6940 for (; bucket != NULL; bucket = bucket->dthb_next) { 6941 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6942 return (bucket->dthb_chain); 6943 } 6944 6945 return (NULL); 6946 } 6947 6948 static int 6949 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 6950 { 6951 int hashval = DTRACE_HASHSTR(hash, template); 6952 int ndx = hashval & hash->dth_mask; 6953 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6954 6955 for (; bucket != NULL; bucket = bucket->dthb_next) { 6956 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6957 return (bucket->dthb_len); 6958 } 6959 6960 return (0); 6961 } 6962 6963 static void 6964 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 6965 { 6966 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 6967 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6968 6969 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 6970 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 6971 6972 /* 6973 * Find the bucket that we're removing this probe from. 6974 */ 6975 for (; bucket != NULL; bucket = bucket->dthb_next) { 6976 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 6977 break; 6978 } 6979 6980 ASSERT(bucket != NULL); 6981 6982 if (*prevp == NULL) { 6983 if (*nextp == NULL) { 6984 /* 6985 * The removed probe was the only probe on this 6986 * bucket; we need to remove the bucket. 6987 */ 6988 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 6989 6990 ASSERT(bucket->dthb_chain == probe); 6991 ASSERT(b != NULL); 6992 6993 if (b == bucket) { 6994 hash->dth_tab[ndx] = bucket->dthb_next; 6995 } else { 6996 while (b->dthb_next != bucket) 6997 b = b->dthb_next; 6998 b->dthb_next = bucket->dthb_next; 6999 } 7000 7001 ASSERT(hash->dth_nbuckets > 0); 7002 hash->dth_nbuckets--; 7003 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 7004 return; 7005 } 7006 7007 bucket->dthb_chain = *nextp; 7008 } else { 7009 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 7010 } 7011 7012 if (*nextp != NULL) 7013 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 7014 } 7015 7016 /* 7017 * DTrace Utility Functions 7018 * 7019 * These are random utility functions that are _not_ called from probe context. 7020 */ 7021 static int 7022 dtrace_badattr(const dtrace_attribute_t *a) 7023 { 7024 return (a->dtat_name > DTRACE_STABILITY_MAX || 7025 a->dtat_data > DTRACE_STABILITY_MAX || 7026 a->dtat_class > DTRACE_CLASS_MAX); 7027 } 7028 7029 /* 7030 * Return a duplicate copy of a string. If the specified string is NULL, 7031 * this function returns a zero-length string. 7032 */ 7033 static char * 7034 dtrace_strdup(const char *str) 7035 { 7036 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 7037 7038 if (str != NULL) 7039 (void) strcpy(new, str); 7040 7041 return (new); 7042 } 7043 7044 #define DTRACE_ISALPHA(c) \ 7045 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 7046 7047 static int 7048 dtrace_badname(const char *s) 7049 { 7050 char c; 7051 7052 if (s == NULL || (c = *s++) == '\0') 7053 return (0); 7054 7055 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 7056 return (1); 7057 7058 while ((c = *s++) != '\0') { 7059 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 7060 c != '-' && c != '_' && c != '.' && c != '`') 7061 return (1); 7062 } 7063 7064 return (0); 7065 } 7066 7067 static void 7068 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 7069 { 7070 uint32_t priv; 7071 7072 #if defined(sun) 7073 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 7074 /* 7075 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 7076 */ 7077 priv = DTRACE_PRIV_ALL; 7078 } else { 7079 *uidp = crgetuid(cr); 7080 *zoneidp = crgetzoneid(cr); 7081 7082 priv = 0; 7083 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 7084 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 7085 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 7086 priv |= DTRACE_PRIV_USER; 7087 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 7088 priv |= DTRACE_PRIV_PROC; 7089 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 7090 priv |= DTRACE_PRIV_OWNER; 7091 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 7092 priv |= DTRACE_PRIV_ZONEOWNER; 7093 } 7094 #else 7095 priv = DTRACE_PRIV_ALL; 7096 #endif 7097 7098 *privp = priv; 7099 } 7100 7101 #ifdef DTRACE_ERRDEBUG 7102 static void 7103 dtrace_errdebug(const char *str) 7104 { 7105 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 7106 int occupied = 0; 7107 7108 mutex_enter(&dtrace_errlock); 7109 dtrace_errlast = str; 7110 dtrace_errthread = curthread; 7111 7112 while (occupied++ < DTRACE_ERRHASHSZ) { 7113 if (dtrace_errhash[hval].dter_msg == str) { 7114 dtrace_errhash[hval].dter_count++; 7115 goto out; 7116 } 7117 7118 if (dtrace_errhash[hval].dter_msg != NULL) { 7119 hval = (hval + 1) % DTRACE_ERRHASHSZ; 7120 continue; 7121 } 7122 7123 dtrace_errhash[hval].dter_msg = str; 7124 dtrace_errhash[hval].dter_count = 1; 7125 goto out; 7126 } 7127 7128 panic("dtrace: undersized error hash"); 7129 out: 7130 mutex_exit(&dtrace_errlock); 7131 } 7132 #endif 7133 7134 /* 7135 * DTrace Matching Functions 7136 * 7137 * These functions are used to match groups of probes, given some elements of 7138 * a probe tuple, or some globbed expressions for elements of a probe tuple. 7139 */ 7140 static int 7141 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 7142 zoneid_t zoneid) 7143 { 7144 if (priv != DTRACE_PRIV_ALL) { 7145 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 7146 uint32_t match = priv & ppriv; 7147 7148 /* 7149 * No PRIV_DTRACE_* privileges... 7150 */ 7151 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 7152 DTRACE_PRIV_KERNEL)) == 0) 7153 return (0); 7154 7155 /* 7156 * No matching bits, but there were bits to match... 7157 */ 7158 if (match == 0 && ppriv != 0) 7159 return (0); 7160 7161 /* 7162 * Need to have permissions to the process, but don't... 7163 */ 7164 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 7165 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 7166 return (0); 7167 } 7168 7169 /* 7170 * Need to be in the same zone unless we possess the 7171 * privilege to examine all zones. 7172 */ 7173 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 7174 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 7175 return (0); 7176 } 7177 } 7178 7179 return (1); 7180 } 7181 7182 /* 7183 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 7184 * consists of input pattern strings and an ops-vector to evaluate them. 7185 * This function returns >0 for match, 0 for no match, and <0 for error. 7186 */ 7187 static int 7188 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 7189 uint32_t priv, uid_t uid, zoneid_t zoneid) 7190 { 7191 dtrace_provider_t *pvp = prp->dtpr_provider; 7192 int rv; 7193 7194 if (pvp->dtpv_defunct) 7195 return (0); 7196 7197 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 7198 return (rv); 7199 7200 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 7201 return (rv); 7202 7203 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 7204 return (rv); 7205 7206 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 7207 return (rv); 7208 7209 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 7210 return (0); 7211 7212 return (rv); 7213 } 7214 7215 /* 7216 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 7217 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 7218 * libc's version, the kernel version only applies to 8-bit ASCII strings. 7219 * In addition, all of the recursion cases except for '*' matching have been 7220 * unwound. For '*', we still implement recursive evaluation, but a depth 7221 * counter is maintained and matching is aborted if we recurse too deep. 7222 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 7223 */ 7224 static int 7225 dtrace_match_glob(const char *s, const char *p, int depth) 7226 { 7227 const char *olds; 7228 char s1, c; 7229 int gs; 7230 7231 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 7232 return (-1); 7233 7234 if (s == NULL) 7235 s = ""; /* treat NULL as empty string */ 7236 7237 top: 7238 olds = s; 7239 s1 = *s++; 7240 7241 if (p == NULL) 7242 return (0); 7243 7244 if ((c = *p++) == '\0') 7245 return (s1 == '\0'); 7246 7247 switch (c) { 7248 case '[': { 7249 int ok = 0, notflag = 0; 7250 char lc = '\0'; 7251 7252 if (s1 == '\0') 7253 return (0); 7254 7255 if (*p == '!') { 7256 notflag = 1; 7257 p++; 7258 } 7259 7260 if ((c = *p++) == '\0') 7261 return (0); 7262 7263 do { 7264 if (c == '-' && lc != '\0' && *p != ']') { 7265 if ((c = *p++) == '\0') 7266 return (0); 7267 if (c == '\\' && (c = *p++) == '\0') 7268 return (0); 7269 7270 if (notflag) { 7271 if (s1 < lc || s1 > c) 7272 ok++; 7273 else 7274 return (0); 7275 } else if (lc <= s1 && s1 <= c) 7276 ok++; 7277 7278 } else if (c == '\\' && (c = *p++) == '\0') 7279 return (0); 7280 7281 lc = c; /* save left-hand 'c' for next iteration */ 7282 7283 if (notflag) { 7284 if (s1 != c) 7285 ok++; 7286 else 7287 return (0); 7288 } else if (s1 == c) 7289 ok++; 7290 7291 if ((c = *p++) == '\0') 7292 return (0); 7293 7294 } while (c != ']'); 7295 7296 if (ok) 7297 goto top; 7298 7299 return (0); 7300 } 7301 7302 case '\\': 7303 if ((c = *p++) == '\0') 7304 return (0); 7305 /*FALLTHRU*/ 7306 7307 default: 7308 if (c != s1) 7309 return (0); 7310 /*FALLTHRU*/ 7311 7312 case '?': 7313 if (s1 != '\0') 7314 goto top; 7315 return (0); 7316 7317 case '*': 7318 while (*p == '*') 7319 p++; /* consecutive *'s are identical to a single one */ 7320 7321 if (*p == '\0') 7322 return (1); 7323 7324 for (s = olds; *s != '\0'; s++) { 7325 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 7326 return (gs); 7327 } 7328 7329 return (0); 7330 } 7331 } 7332 7333 /*ARGSUSED*/ 7334 static int 7335 dtrace_match_string(const char *s, const char *p, int depth) 7336 { 7337 return (s != NULL && strcmp(s, p) == 0); 7338 } 7339 7340 /*ARGSUSED*/ 7341 static int 7342 dtrace_match_nul(const char *s, const char *p, int depth) 7343 { 7344 return (1); /* always match the empty pattern */ 7345 } 7346 7347 /*ARGSUSED*/ 7348 static int 7349 dtrace_match_nonzero(const char *s, const char *p, int depth) 7350 { 7351 return (s != NULL && s[0] != '\0'); 7352 } 7353 7354 static int 7355 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 7356 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 7357 { 7358 dtrace_probe_t template, *probe; 7359 dtrace_hash_t *hash = NULL; 7360 int len, best = INT_MAX, nmatched = 0; 7361 dtrace_id_t i; 7362 7363 ASSERT(MUTEX_HELD(&dtrace_lock)); 7364 7365 /* 7366 * If the probe ID is specified in the key, just lookup by ID and 7367 * invoke the match callback once if a matching probe is found. 7368 */ 7369 if (pkp->dtpk_id != DTRACE_IDNONE) { 7370 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 7371 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 7372 (void) (*matched)(probe, arg); 7373 nmatched++; 7374 } 7375 return (nmatched); 7376 } 7377 7378 template.dtpr_mod = (char *)pkp->dtpk_mod; 7379 template.dtpr_func = (char *)pkp->dtpk_func; 7380 template.dtpr_name = (char *)pkp->dtpk_name; 7381 7382 /* 7383 * We want to find the most distinct of the module name, function 7384 * name, and name. So for each one that is not a glob pattern or 7385 * empty string, we perform a lookup in the corresponding hash and 7386 * use the hash table with the fewest collisions to do our search. 7387 */ 7388 if (pkp->dtpk_mmatch == &dtrace_match_string && 7389 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 7390 best = len; 7391 hash = dtrace_bymod; 7392 } 7393 7394 if (pkp->dtpk_fmatch == &dtrace_match_string && 7395 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 7396 best = len; 7397 hash = dtrace_byfunc; 7398 } 7399 7400 if (pkp->dtpk_nmatch == &dtrace_match_string && 7401 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 7402 best = len; 7403 hash = dtrace_byname; 7404 } 7405 7406 /* 7407 * If we did not select a hash table, iterate over every probe and 7408 * invoke our callback for each one that matches our input probe key. 7409 */ 7410 if (hash == NULL) { 7411 for (i = 0; i < dtrace_nprobes; i++) { 7412 if ((probe = dtrace_probes[i]) == NULL || 7413 dtrace_match_probe(probe, pkp, priv, uid, 7414 zoneid) <= 0) 7415 continue; 7416 7417 nmatched++; 7418 7419 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 7420 break; 7421 } 7422 7423 return (nmatched); 7424 } 7425 7426 /* 7427 * If we selected a hash table, iterate over each probe of the same key 7428 * name and invoke the callback for every probe that matches the other 7429 * attributes of our input probe key. 7430 */ 7431 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 7432 probe = *(DTRACE_HASHNEXT(hash, probe))) { 7433 7434 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 7435 continue; 7436 7437 nmatched++; 7438 7439 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 7440 break; 7441 } 7442 7443 return (nmatched); 7444 } 7445 7446 /* 7447 * Return the function pointer dtrace_probecmp() should use to compare the 7448 * specified pattern with a string. For NULL or empty patterns, we select 7449 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 7450 * For non-empty non-glob strings, we use dtrace_match_string(). 7451 */ 7452 static dtrace_probekey_f * 7453 dtrace_probekey_func(const char *p) 7454 { 7455 char c; 7456 7457 if (p == NULL || *p == '\0') 7458 return (&dtrace_match_nul); 7459 7460 while ((c = *p++) != '\0') { 7461 if (c == '[' || c == '?' || c == '*' || c == '\\') 7462 return (&dtrace_match_glob); 7463 } 7464 7465 return (&dtrace_match_string); 7466 } 7467 7468 /* 7469 * Build a probe comparison key for use with dtrace_match_probe() from the 7470 * given probe description. By convention, a null key only matches anchored 7471 * probes: if each field is the empty string, reset dtpk_fmatch to 7472 * dtrace_match_nonzero(). 7473 */ 7474 static void 7475 dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 7476 { 7477 pkp->dtpk_prov = pdp->dtpd_provider; 7478 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 7479 7480 pkp->dtpk_mod = pdp->dtpd_mod; 7481 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 7482 7483 pkp->dtpk_func = pdp->dtpd_func; 7484 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 7485 7486 pkp->dtpk_name = pdp->dtpd_name; 7487 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 7488 7489 pkp->dtpk_id = pdp->dtpd_id; 7490 7491 if (pkp->dtpk_id == DTRACE_IDNONE && 7492 pkp->dtpk_pmatch == &dtrace_match_nul && 7493 pkp->dtpk_mmatch == &dtrace_match_nul && 7494 pkp->dtpk_fmatch == &dtrace_match_nul && 7495 pkp->dtpk_nmatch == &dtrace_match_nul) 7496 pkp->dtpk_fmatch = &dtrace_match_nonzero; 7497 } 7498 7499 /* 7500 * DTrace Provider-to-Framework API Functions 7501 * 7502 * These functions implement much of the Provider-to-Framework API, as 7503 * described in <sys/dtrace.h>. The parts of the API not in this section are 7504 * the functions in the API for probe management (found below), and 7505 * dtrace_probe() itself (found above). 7506 */ 7507 7508 /* 7509 * Register the calling provider with the DTrace framework. This should 7510 * generally be called by DTrace providers in their attach(9E) entry point. 7511 */ 7512 int 7513 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 7514 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 7515 { 7516 dtrace_provider_t *provider; 7517 7518 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 7519 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7520 "arguments", name ? name : "<NULL>"); 7521 return (EINVAL); 7522 } 7523 7524 if (name[0] == '\0' || dtrace_badname(name)) { 7525 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7526 "provider name", name); 7527 return (EINVAL); 7528 } 7529 7530 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 7531 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 7532 pops->dtps_destroy == NULL || 7533 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 7534 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7535 "provider ops", name); 7536 return (EINVAL); 7537 } 7538 7539 if (dtrace_badattr(&pap->dtpa_provider) || 7540 dtrace_badattr(&pap->dtpa_mod) || 7541 dtrace_badattr(&pap->dtpa_func) || 7542 dtrace_badattr(&pap->dtpa_name) || 7543 dtrace_badattr(&pap->dtpa_args)) { 7544 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7545 "provider attributes", name); 7546 return (EINVAL); 7547 } 7548 7549 if (priv & ~DTRACE_PRIV_ALL) { 7550 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7551 "privilege attributes", name); 7552 return (EINVAL); 7553 } 7554 7555 if ((priv & DTRACE_PRIV_KERNEL) && 7556 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 7557 pops->dtps_usermode == NULL) { 7558 cmn_err(CE_WARN, "failed to register provider '%s': need " 7559 "dtps_usermode() op for given privilege attributes", name); 7560 return (EINVAL); 7561 } 7562 7563 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 7564 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7565 (void) strcpy(provider->dtpv_name, name); 7566 7567 provider->dtpv_attr = *pap; 7568 provider->dtpv_priv.dtpp_flags = priv; 7569 if (cr != NULL) { 7570 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 7571 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 7572 } 7573 provider->dtpv_pops = *pops; 7574 7575 if (pops->dtps_provide == NULL) { 7576 ASSERT(pops->dtps_provide_module != NULL); 7577 provider->dtpv_pops.dtps_provide = 7578 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 7579 } 7580 7581 if (pops->dtps_provide_module == NULL) { 7582 ASSERT(pops->dtps_provide != NULL); 7583 provider->dtpv_pops.dtps_provide_module = 7584 (void (*)(void *, modctl_t *))dtrace_nullop; 7585 } 7586 7587 if (pops->dtps_suspend == NULL) { 7588 ASSERT(pops->dtps_resume == NULL); 7589 provider->dtpv_pops.dtps_suspend = 7590 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7591 provider->dtpv_pops.dtps_resume = 7592 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7593 } 7594 7595 provider->dtpv_arg = arg; 7596 *idp = (dtrace_provider_id_t)provider; 7597 7598 if (pops == &dtrace_provider_ops) { 7599 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7600 ASSERT(MUTEX_HELD(&dtrace_lock)); 7601 ASSERT(dtrace_anon.dta_enabling == NULL); 7602 7603 /* 7604 * We make sure that the DTrace provider is at the head of 7605 * the provider chain. 7606 */ 7607 provider->dtpv_next = dtrace_provider; 7608 dtrace_provider = provider; 7609 return (0); 7610 } 7611 7612 mutex_enter(&dtrace_provider_lock); 7613 mutex_enter(&dtrace_lock); 7614 7615 /* 7616 * If there is at least one provider registered, we'll add this 7617 * provider after the first provider. 7618 */ 7619 if (dtrace_provider != NULL) { 7620 provider->dtpv_next = dtrace_provider->dtpv_next; 7621 dtrace_provider->dtpv_next = provider; 7622 } else { 7623 dtrace_provider = provider; 7624 } 7625 7626 if (dtrace_retained != NULL) { 7627 dtrace_enabling_provide(provider); 7628 7629 /* 7630 * Now we need to call dtrace_enabling_matchall() -- which 7631 * will acquire cpu_lock and dtrace_lock. We therefore need 7632 * to drop all of our locks before calling into it... 7633 */ 7634 mutex_exit(&dtrace_lock); 7635 mutex_exit(&dtrace_provider_lock); 7636 dtrace_enabling_matchall(); 7637 7638 return (0); 7639 } 7640 7641 mutex_exit(&dtrace_lock); 7642 mutex_exit(&dtrace_provider_lock); 7643 7644 return (0); 7645 } 7646 7647 /* 7648 * Unregister the specified provider from the DTrace framework. This should 7649 * generally be called by DTrace providers in their detach(9E) entry point. 7650 */ 7651 int 7652 dtrace_unregister(dtrace_provider_id_t id) 7653 { 7654 dtrace_provider_t *old = (dtrace_provider_t *)id; 7655 dtrace_provider_t *prev = NULL; 7656 int i, self = 0, noreap = 0; 7657 dtrace_probe_t *probe, *first = NULL; 7658 7659 if (old->dtpv_pops.dtps_enable == 7660 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 7661 /* 7662 * If DTrace itself is the provider, we're called with locks 7663 * already held. 7664 */ 7665 ASSERT(old == dtrace_provider); 7666 #if defined(sun) 7667 ASSERT(dtrace_devi != NULL); 7668 #endif 7669 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7670 ASSERT(MUTEX_HELD(&dtrace_lock)); 7671 self = 1; 7672 7673 if (dtrace_provider->dtpv_next != NULL) { 7674 /* 7675 * There's another provider here; return failure. 7676 */ 7677 return (EBUSY); 7678 } 7679 } else { 7680 mutex_enter(&dtrace_provider_lock); 7681 mutex_enter(&mod_lock); 7682 mutex_enter(&dtrace_lock); 7683 } 7684 7685 /* 7686 * If anyone has /dev/dtrace open, or if there are anonymous enabled 7687 * probes, we refuse to let providers slither away, unless this 7688 * provider has already been explicitly invalidated. 7689 */ 7690 if (!old->dtpv_defunct && 7691 (dtrace_opens || (dtrace_anon.dta_state != NULL && 7692 dtrace_anon.dta_state->dts_necbs > 0))) { 7693 if (!self) { 7694 mutex_exit(&dtrace_lock); 7695 mutex_exit(&mod_lock); 7696 mutex_exit(&dtrace_provider_lock); 7697 } 7698 return (EBUSY); 7699 } 7700 7701 /* 7702 * Attempt to destroy the probes associated with this provider. 7703 */ 7704 for (i = 0; i < dtrace_nprobes; i++) { 7705 if ((probe = dtrace_probes[i]) == NULL) 7706 continue; 7707 7708 if (probe->dtpr_provider != old) 7709 continue; 7710 7711 if (probe->dtpr_ecb == NULL) 7712 continue; 7713 7714 /* 7715 * If we are trying to unregister a defunct provider, and the 7716 * provider was made defunct within the interval dictated by 7717 * dtrace_unregister_defunct_reap, we'll (asynchronously) 7718 * attempt to reap our enablings. To denote that the provider 7719 * should reattempt to unregister itself at some point in the 7720 * future, we will return a differentiable error code (EAGAIN 7721 * instead of EBUSY) in this case. 7722 */ 7723 if (dtrace_gethrtime() - old->dtpv_defunct > 7724 dtrace_unregister_defunct_reap) 7725 noreap = 1; 7726 7727 if (!self) { 7728 mutex_exit(&dtrace_lock); 7729 mutex_exit(&mod_lock); 7730 mutex_exit(&dtrace_provider_lock); 7731 } 7732 7733 if (noreap) 7734 return (EBUSY); 7735 7736 (void) taskq_dispatch(dtrace_taskq, 7737 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 7738 7739 return (EAGAIN); 7740 } 7741 7742 /* 7743 * All of the probes for this provider are disabled; we can safely 7744 * remove all of them from their hash chains and from the probe array. 7745 */ 7746 for (i = 0; i < dtrace_nprobes; i++) { 7747 if ((probe = dtrace_probes[i]) == NULL) 7748 continue; 7749 7750 if (probe->dtpr_provider != old) 7751 continue; 7752 7753 dtrace_probes[i] = NULL; 7754 7755 dtrace_hash_remove(dtrace_bymod, probe); 7756 dtrace_hash_remove(dtrace_byfunc, probe); 7757 dtrace_hash_remove(dtrace_byname, probe); 7758 7759 if (first == NULL) { 7760 first = probe; 7761 probe->dtpr_nextmod = NULL; 7762 } else { 7763 probe->dtpr_nextmod = first; 7764 first = probe; 7765 } 7766 } 7767 7768 /* 7769 * The provider's probes have been removed from the hash chains and 7770 * from the probe array. Now issue a dtrace_sync() to be sure that 7771 * everyone has cleared out from any probe array processing. 7772 */ 7773 dtrace_sync(); 7774 7775 for (probe = first; probe != NULL; probe = first) { 7776 first = probe->dtpr_nextmod; 7777 7778 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 7779 probe->dtpr_arg); 7780 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7781 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7782 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7783 #if defined(sun) 7784 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 7785 #else 7786 free_unr(dtrace_arena, probe->dtpr_id); 7787 #endif 7788 kmem_free(probe, sizeof (dtrace_probe_t)); 7789 } 7790 7791 if ((prev = dtrace_provider) == old) { 7792 #if defined(sun) 7793 ASSERT(self || dtrace_devi == NULL); 7794 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 7795 #endif 7796 dtrace_provider = old->dtpv_next; 7797 } else { 7798 while (prev != NULL && prev->dtpv_next != old) 7799 prev = prev->dtpv_next; 7800 7801 if (prev == NULL) { 7802 panic("attempt to unregister non-existent " 7803 "dtrace provider %p\n", (void *)id); 7804 } 7805 7806 prev->dtpv_next = old->dtpv_next; 7807 } 7808 7809 if (!self) { 7810 mutex_exit(&dtrace_lock); 7811 mutex_exit(&mod_lock); 7812 mutex_exit(&dtrace_provider_lock); 7813 } 7814 7815 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 7816 kmem_free(old, sizeof (dtrace_provider_t)); 7817 7818 return (0); 7819 } 7820 7821 /* 7822 * Invalidate the specified provider. All subsequent probe lookups for the 7823 * specified provider will fail, but its probes will not be removed. 7824 */ 7825 void 7826 dtrace_invalidate(dtrace_provider_id_t id) 7827 { 7828 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 7829 7830 ASSERT(pvp->dtpv_pops.dtps_enable != 7831 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 7832 7833 mutex_enter(&dtrace_provider_lock); 7834 mutex_enter(&dtrace_lock); 7835 7836 pvp->dtpv_defunct = dtrace_gethrtime(); 7837 7838 mutex_exit(&dtrace_lock); 7839 mutex_exit(&dtrace_provider_lock); 7840 } 7841 7842 /* 7843 * Indicate whether or not DTrace has attached. 7844 */ 7845 int 7846 dtrace_attached(void) 7847 { 7848 /* 7849 * dtrace_provider will be non-NULL iff the DTrace driver has 7850 * attached. (It's non-NULL because DTrace is always itself a 7851 * provider.) 7852 */ 7853 return (dtrace_provider != NULL); 7854 } 7855 7856 /* 7857 * Remove all the unenabled probes for the given provider. This function is 7858 * not unlike dtrace_unregister(), except that it doesn't remove the provider 7859 * -- just as many of its associated probes as it can. 7860 */ 7861 int 7862 dtrace_condense(dtrace_provider_id_t id) 7863 { 7864 dtrace_provider_t *prov = (dtrace_provider_t *)id; 7865 int i; 7866 dtrace_probe_t *probe; 7867 7868 /* 7869 * Make sure this isn't the dtrace provider itself. 7870 */ 7871 ASSERT(prov->dtpv_pops.dtps_enable != 7872 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 7873 7874 mutex_enter(&dtrace_provider_lock); 7875 mutex_enter(&dtrace_lock); 7876 7877 /* 7878 * Attempt to destroy the probes associated with this provider. 7879 */ 7880 for (i = 0; i < dtrace_nprobes; i++) { 7881 if ((probe = dtrace_probes[i]) == NULL) 7882 continue; 7883 7884 if (probe->dtpr_provider != prov) 7885 continue; 7886 7887 if (probe->dtpr_ecb != NULL) 7888 continue; 7889 7890 dtrace_probes[i] = NULL; 7891 7892 dtrace_hash_remove(dtrace_bymod, probe); 7893 dtrace_hash_remove(dtrace_byfunc, probe); 7894 dtrace_hash_remove(dtrace_byname, probe); 7895 7896 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 7897 probe->dtpr_arg); 7898 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7899 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7900 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7901 kmem_free(probe, sizeof (dtrace_probe_t)); 7902 #if defined(sun) 7903 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 7904 #else 7905 free_unr(dtrace_arena, i + 1); 7906 #endif 7907 } 7908 7909 mutex_exit(&dtrace_lock); 7910 mutex_exit(&dtrace_provider_lock); 7911 7912 return (0); 7913 } 7914 7915 /* 7916 * DTrace Probe Management Functions 7917 * 7918 * The functions in this section perform the DTrace probe management, 7919 * including functions to create probes, look-up probes, and call into the 7920 * providers to request that probes be provided. Some of these functions are 7921 * in the Provider-to-Framework API; these functions can be identified by the 7922 * fact that they are not declared "static". 7923 */ 7924 7925 /* 7926 * Create a probe with the specified module name, function name, and name. 7927 */ 7928 dtrace_id_t 7929 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 7930 const char *func, const char *name, int aframes, void *arg) 7931 { 7932 dtrace_probe_t *probe, **probes; 7933 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 7934 dtrace_id_t id; 7935 7936 if (provider == dtrace_provider) { 7937 ASSERT(MUTEX_HELD(&dtrace_lock)); 7938 } else { 7939 mutex_enter(&dtrace_lock); 7940 } 7941 7942 #if defined(sun) 7943 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 7944 VM_BESTFIT | VM_SLEEP); 7945 #else 7946 id = alloc_unr(dtrace_arena); 7947 #endif 7948 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 7949 7950 probe->dtpr_id = id; 7951 probe->dtpr_gen = dtrace_probegen++; 7952 probe->dtpr_mod = dtrace_strdup(mod); 7953 probe->dtpr_func = dtrace_strdup(func); 7954 probe->dtpr_name = dtrace_strdup(name); 7955 probe->dtpr_arg = arg; 7956 probe->dtpr_aframes = aframes; 7957 probe->dtpr_provider = provider; 7958 7959 dtrace_hash_add(dtrace_bymod, probe); 7960 dtrace_hash_add(dtrace_byfunc, probe); 7961 dtrace_hash_add(dtrace_byname, probe); 7962 7963 if (id - 1 >= dtrace_nprobes) { 7964 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 7965 size_t nsize = osize << 1; 7966 7967 if (nsize == 0) { 7968 ASSERT(osize == 0); 7969 ASSERT(dtrace_probes == NULL); 7970 nsize = sizeof (dtrace_probe_t *); 7971 } 7972 7973 probes = kmem_zalloc(nsize, KM_SLEEP); 7974 7975 if (dtrace_probes == NULL) { 7976 ASSERT(osize == 0); 7977 dtrace_probes = probes; 7978 dtrace_nprobes = 1; 7979 } else { 7980 dtrace_probe_t **oprobes = dtrace_probes; 7981 7982 bcopy(oprobes, probes, osize); 7983 dtrace_membar_producer(); 7984 dtrace_probes = probes; 7985 7986 dtrace_sync(); 7987 7988 /* 7989 * All CPUs are now seeing the new probes array; we can 7990 * safely free the old array. 7991 */ 7992 kmem_free(oprobes, osize); 7993 dtrace_nprobes <<= 1; 7994 } 7995 7996 ASSERT(id - 1 < dtrace_nprobes); 7997 } 7998 7999 ASSERT(dtrace_probes[id - 1] == NULL); 8000 dtrace_probes[id - 1] = probe; 8001 8002 if (provider != dtrace_provider) 8003 mutex_exit(&dtrace_lock); 8004 8005 return (id); 8006 } 8007 8008 static dtrace_probe_t * 8009 dtrace_probe_lookup_id(dtrace_id_t id) 8010 { 8011 ASSERT(MUTEX_HELD(&dtrace_lock)); 8012 8013 if (id == 0 || id > dtrace_nprobes) 8014 return (NULL); 8015 8016 return (dtrace_probes[id - 1]); 8017 } 8018 8019 static int 8020 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 8021 { 8022 *((dtrace_id_t *)arg) = probe->dtpr_id; 8023 8024 return (DTRACE_MATCH_DONE); 8025 } 8026 8027 /* 8028 * Look up a probe based on provider and one or more of module name, function 8029 * name and probe name. 8030 */ 8031 dtrace_id_t 8032 dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 8033 char *func, char *name) 8034 { 8035 dtrace_probekey_t pkey; 8036 dtrace_id_t id; 8037 int match; 8038 8039 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 8040 pkey.dtpk_pmatch = &dtrace_match_string; 8041 pkey.dtpk_mod = mod; 8042 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 8043 pkey.dtpk_func = func; 8044 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 8045 pkey.dtpk_name = name; 8046 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 8047 pkey.dtpk_id = DTRACE_IDNONE; 8048 8049 mutex_enter(&dtrace_lock); 8050 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 8051 dtrace_probe_lookup_match, &id); 8052 mutex_exit(&dtrace_lock); 8053 8054 ASSERT(match == 1 || match == 0); 8055 return (match ? id : 0); 8056 } 8057 8058 /* 8059 * Returns the probe argument associated with the specified probe. 8060 */ 8061 void * 8062 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 8063 { 8064 dtrace_probe_t *probe; 8065 void *rval = NULL; 8066 8067 mutex_enter(&dtrace_lock); 8068 8069 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 8070 probe->dtpr_provider == (dtrace_provider_t *)id) 8071 rval = probe->dtpr_arg; 8072 8073 mutex_exit(&dtrace_lock); 8074 8075 return (rval); 8076 } 8077 8078 /* 8079 * Copy a probe into a probe description. 8080 */ 8081 static void 8082 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 8083 { 8084 bzero(pdp, sizeof (dtrace_probedesc_t)); 8085 pdp->dtpd_id = prp->dtpr_id; 8086 8087 (void) strncpy(pdp->dtpd_provider, 8088 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 8089 8090 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 8091 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 8092 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 8093 } 8094 8095 #if !defined(sun) 8096 static int 8097 dtrace_probe_provide_cb(linker_file_t lf, void *arg) 8098 { 8099 dtrace_provider_t *prv = (dtrace_provider_t *) arg; 8100 8101 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, lf); 8102 8103 return(0); 8104 } 8105 #endif 8106 8107 8108 /* 8109 * Called to indicate that a probe -- or probes -- should be provided by a 8110 * specfied provider. If the specified description is NULL, the provider will 8111 * be told to provide all of its probes. (This is done whenever a new 8112 * consumer comes along, or whenever a retained enabling is to be matched.) If 8113 * the specified description is non-NULL, the provider is given the 8114 * opportunity to dynamically provide the specified probe, allowing providers 8115 * to support the creation of probes on-the-fly. (So-called _autocreated_ 8116 * probes.) If the provider is NULL, the operations will be applied to all 8117 * providers; if the provider is non-NULL the operations will only be applied 8118 * to the specified provider. The dtrace_provider_lock must be held, and the 8119 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 8120 * will need to grab the dtrace_lock when it reenters the framework through 8121 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 8122 */ 8123 static void 8124 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 8125 { 8126 #if defined(sun) 8127 modctl_t *ctl; 8128 #endif 8129 int all = 0; 8130 8131 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8132 8133 if (prv == NULL) { 8134 all = 1; 8135 prv = dtrace_provider; 8136 } 8137 8138 do { 8139 /* 8140 * First, call the blanket provide operation. 8141 */ 8142 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 8143 8144 /* 8145 * Now call the per-module provide operation. We will grab 8146 * mod_lock to prevent the list from being modified. Note 8147 * that this also prevents the mod_busy bits from changing. 8148 * (mod_busy can only be changed with mod_lock held.) 8149 */ 8150 mutex_enter(&mod_lock); 8151 8152 #if defined(sun) 8153 ctl = &modules; 8154 do { 8155 if (ctl->mod_busy || ctl->mod_mp == NULL) 8156 continue; 8157 8158 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 8159 8160 } while ((ctl = ctl->mod_next) != &modules); 8161 #else 8162 (void) linker_file_foreach(dtrace_probe_provide_cb, prv); 8163 #endif 8164 8165 mutex_exit(&mod_lock); 8166 } while (all && (prv = prv->dtpv_next) != NULL); 8167 } 8168 8169 #if defined(sun) 8170 /* 8171 * Iterate over each probe, and call the Framework-to-Provider API function 8172 * denoted by offs. 8173 */ 8174 static void 8175 dtrace_probe_foreach(uintptr_t offs) 8176 { 8177 dtrace_provider_t *prov; 8178 void (*func)(void *, dtrace_id_t, void *); 8179 dtrace_probe_t *probe; 8180 dtrace_icookie_t cookie; 8181 int i; 8182 8183 /* 8184 * We disable interrupts to walk through the probe array. This is 8185 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 8186 * won't see stale data. 8187 */ 8188 cookie = dtrace_interrupt_disable(); 8189 8190 for (i = 0; i < dtrace_nprobes; i++) { 8191 if ((probe = dtrace_probes[i]) == NULL) 8192 continue; 8193 8194 if (probe->dtpr_ecb == NULL) { 8195 /* 8196 * This probe isn't enabled -- don't call the function. 8197 */ 8198 continue; 8199 } 8200 8201 prov = probe->dtpr_provider; 8202 func = *((void(**)(void *, dtrace_id_t, void *)) 8203 ((uintptr_t)&prov->dtpv_pops + offs)); 8204 8205 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 8206 } 8207 8208 dtrace_interrupt_enable(cookie); 8209 } 8210 #endif 8211 8212 static int 8213 dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 8214 { 8215 dtrace_probekey_t pkey; 8216 uint32_t priv; 8217 uid_t uid; 8218 zoneid_t zoneid; 8219 8220 ASSERT(MUTEX_HELD(&dtrace_lock)); 8221 dtrace_ecb_create_cache = NULL; 8222 8223 if (desc == NULL) { 8224 /* 8225 * If we're passed a NULL description, we're being asked to 8226 * create an ECB with a NULL probe. 8227 */ 8228 (void) dtrace_ecb_create_enable(NULL, enab); 8229 return (0); 8230 } 8231 8232 dtrace_probekey(desc, &pkey); 8233 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 8234 &priv, &uid, &zoneid); 8235 8236 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 8237 enab)); 8238 } 8239 8240 /* 8241 * DTrace Helper Provider Functions 8242 */ 8243 static void 8244 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 8245 { 8246 attr->dtat_name = DOF_ATTR_NAME(dofattr); 8247 attr->dtat_data = DOF_ATTR_DATA(dofattr); 8248 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 8249 } 8250 8251 static void 8252 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 8253 const dof_provider_t *dofprov, char *strtab) 8254 { 8255 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 8256 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 8257 dofprov->dofpv_provattr); 8258 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 8259 dofprov->dofpv_modattr); 8260 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 8261 dofprov->dofpv_funcattr); 8262 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 8263 dofprov->dofpv_nameattr); 8264 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 8265 dofprov->dofpv_argsattr); 8266 } 8267 8268 static void 8269 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8270 { 8271 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8272 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8273 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 8274 dof_provider_t *provider; 8275 dof_probe_t *probe; 8276 uint32_t *off, *enoff; 8277 uint8_t *arg; 8278 char *strtab; 8279 uint_t i, nprobes; 8280 dtrace_helper_provdesc_t dhpv; 8281 dtrace_helper_probedesc_t dhpb; 8282 dtrace_meta_t *meta = dtrace_meta_pid; 8283 dtrace_mops_t *mops = &meta->dtm_mops; 8284 void *parg; 8285 8286 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8287 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8288 provider->dofpv_strtab * dof->dofh_secsize); 8289 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8290 provider->dofpv_probes * dof->dofh_secsize); 8291 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8292 provider->dofpv_prargs * dof->dofh_secsize); 8293 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8294 provider->dofpv_proffs * dof->dofh_secsize); 8295 8296 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8297 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 8298 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 8299 enoff = NULL; 8300 8301 /* 8302 * See dtrace_helper_provider_validate(). 8303 */ 8304 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 8305 provider->dofpv_prenoffs != DOF_SECT_NONE) { 8306 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8307 provider->dofpv_prenoffs * dof->dofh_secsize); 8308 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 8309 } 8310 8311 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 8312 8313 /* 8314 * Create the provider. 8315 */ 8316 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8317 8318 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 8319 return; 8320 8321 meta->dtm_count++; 8322 8323 /* 8324 * Create the probes. 8325 */ 8326 for (i = 0; i < nprobes; i++) { 8327 probe = (dof_probe_t *)(uintptr_t)(daddr + 8328 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 8329 8330 dhpb.dthpb_mod = dhp->dofhp_mod; 8331 dhpb.dthpb_func = strtab + probe->dofpr_func; 8332 dhpb.dthpb_name = strtab + probe->dofpr_name; 8333 dhpb.dthpb_base = probe->dofpr_addr; 8334 dhpb.dthpb_offs = off + probe->dofpr_offidx; 8335 dhpb.dthpb_noffs = probe->dofpr_noffs; 8336 if (enoff != NULL) { 8337 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 8338 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 8339 } else { 8340 dhpb.dthpb_enoffs = NULL; 8341 dhpb.dthpb_nenoffs = 0; 8342 } 8343 dhpb.dthpb_args = arg + probe->dofpr_argidx; 8344 dhpb.dthpb_nargc = probe->dofpr_nargc; 8345 dhpb.dthpb_xargc = probe->dofpr_xargc; 8346 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 8347 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 8348 8349 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 8350 } 8351 } 8352 8353 static void 8354 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 8355 { 8356 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8357 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8358 int i; 8359 8360 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8361 8362 for (i = 0; i < dof->dofh_secnum; i++) { 8363 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8364 dof->dofh_secoff + i * dof->dofh_secsize); 8365 8366 if (sec->dofs_type != DOF_SECT_PROVIDER) 8367 continue; 8368 8369 dtrace_helper_provide_one(dhp, sec, pid); 8370 } 8371 8372 /* 8373 * We may have just created probes, so we must now rematch against 8374 * any retained enablings. Note that this call will acquire both 8375 * cpu_lock and dtrace_lock; the fact that we are holding 8376 * dtrace_meta_lock now is what defines the ordering with respect to 8377 * these three locks. 8378 */ 8379 dtrace_enabling_matchall(); 8380 } 8381 8382 static void 8383 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8384 { 8385 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8386 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8387 dof_sec_t *str_sec; 8388 dof_provider_t *provider; 8389 char *strtab; 8390 dtrace_helper_provdesc_t dhpv; 8391 dtrace_meta_t *meta = dtrace_meta_pid; 8392 dtrace_mops_t *mops = &meta->dtm_mops; 8393 8394 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8395 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8396 provider->dofpv_strtab * dof->dofh_secsize); 8397 8398 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8399 8400 /* 8401 * Create the provider. 8402 */ 8403 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8404 8405 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 8406 8407 meta->dtm_count--; 8408 } 8409 8410 static void 8411 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 8412 { 8413 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8414 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8415 int i; 8416 8417 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8418 8419 for (i = 0; i < dof->dofh_secnum; i++) { 8420 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8421 dof->dofh_secoff + i * dof->dofh_secsize); 8422 8423 if (sec->dofs_type != DOF_SECT_PROVIDER) 8424 continue; 8425 8426 dtrace_helper_provider_remove_one(dhp, sec, pid); 8427 } 8428 } 8429 8430 /* 8431 * DTrace Meta Provider-to-Framework API Functions 8432 * 8433 * These functions implement the Meta Provider-to-Framework API, as described 8434 * in <sys/dtrace.h>. 8435 */ 8436 int 8437 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 8438 dtrace_meta_provider_id_t *idp) 8439 { 8440 dtrace_meta_t *meta; 8441 dtrace_helpers_t *help, *next; 8442 int i; 8443 8444 *idp = DTRACE_METAPROVNONE; 8445 8446 /* 8447 * We strictly don't need the name, but we hold onto it for 8448 * debuggability. All hail error queues! 8449 */ 8450 if (name == NULL) { 8451 cmn_err(CE_WARN, "failed to register meta-provider: " 8452 "invalid name"); 8453 return (EINVAL); 8454 } 8455 8456 if (mops == NULL || 8457 mops->dtms_create_probe == NULL || 8458 mops->dtms_provide_pid == NULL || 8459 mops->dtms_remove_pid == NULL) { 8460 cmn_err(CE_WARN, "failed to register meta-register %s: " 8461 "invalid ops", name); 8462 return (EINVAL); 8463 } 8464 8465 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 8466 meta->dtm_mops = *mops; 8467 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8468 (void) strcpy(meta->dtm_name, name); 8469 meta->dtm_arg = arg; 8470 8471 mutex_enter(&dtrace_meta_lock); 8472 mutex_enter(&dtrace_lock); 8473 8474 if (dtrace_meta_pid != NULL) { 8475 mutex_exit(&dtrace_lock); 8476 mutex_exit(&dtrace_meta_lock); 8477 cmn_err(CE_WARN, "failed to register meta-register %s: " 8478 "user-land meta-provider exists", name); 8479 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 8480 kmem_free(meta, sizeof (dtrace_meta_t)); 8481 return (EINVAL); 8482 } 8483 8484 dtrace_meta_pid = meta; 8485 *idp = (dtrace_meta_provider_id_t)meta; 8486 8487 /* 8488 * If there are providers and probes ready to go, pass them 8489 * off to the new meta provider now. 8490 */ 8491 8492 help = dtrace_deferred_pid; 8493 dtrace_deferred_pid = NULL; 8494 8495 mutex_exit(&dtrace_lock); 8496 8497 while (help != NULL) { 8498 for (i = 0; i < help->dthps_nprovs; i++) { 8499 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 8500 help->dthps_pid); 8501 } 8502 8503 next = help->dthps_next; 8504 help->dthps_next = NULL; 8505 help->dthps_prev = NULL; 8506 help->dthps_deferred = 0; 8507 help = next; 8508 } 8509 8510 mutex_exit(&dtrace_meta_lock); 8511 8512 return (0); 8513 } 8514 8515 int 8516 dtrace_meta_unregister(dtrace_meta_provider_id_t id) 8517 { 8518 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 8519 8520 mutex_enter(&dtrace_meta_lock); 8521 mutex_enter(&dtrace_lock); 8522 8523 if (old == dtrace_meta_pid) { 8524 pp = &dtrace_meta_pid; 8525 } else { 8526 panic("attempt to unregister non-existent " 8527 "dtrace meta-provider %p\n", (void *)old); 8528 } 8529 8530 if (old->dtm_count != 0) { 8531 mutex_exit(&dtrace_lock); 8532 mutex_exit(&dtrace_meta_lock); 8533 return (EBUSY); 8534 } 8535 8536 *pp = NULL; 8537 8538 mutex_exit(&dtrace_lock); 8539 mutex_exit(&dtrace_meta_lock); 8540 8541 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 8542 kmem_free(old, sizeof (dtrace_meta_t)); 8543 8544 return (0); 8545 } 8546 8547 8548 /* 8549 * DTrace DIF Object Functions 8550 */ 8551 static int 8552 dtrace_difo_err(uint_t pc, const char *format, ...) 8553 { 8554 if (dtrace_err_verbose) { 8555 va_list alist; 8556 8557 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 8558 va_start(alist, format); 8559 (void) vuprintf(format, alist); 8560 va_end(alist); 8561 } 8562 8563 #ifdef DTRACE_ERRDEBUG 8564 dtrace_errdebug(format); 8565 #endif 8566 return (1); 8567 } 8568 8569 /* 8570 * Validate a DTrace DIF object by checking the IR instructions. The following 8571 * rules are currently enforced by dtrace_difo_validate(): 8572 * 8573 * 1. Each instruction must have a valid opcode 8574 * 2. Each register, string, variable, or subroutine reference must be valid 8575 * 3. No instruction can modify register %r0 (must be zero) 8576 * 4. All instruction reserved bits must be set to zero 8577 * 5. The last instruction must be a "ret" instruction 8578 * 6. All branch targets must reference a valid instruction _after_ the branch 8579 */ 8580 static int 8581 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 8582 cred_t *cr) 8583 { 8584 int err = 0, i; 8585 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8586 int kcheckload; 8587 uint_t pc; 8588 8589 kcheckload = cr == NULL || 8590 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 8591 8592 dp->dtdo_destructive = 0; 8593 8594 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 8595 dif_instr_t instr = dp->dtdo_buf[pc]; 8596 8597 uint_t r1 = DIF_INSTR_R1(instr); 8598 uint_t r2 = DIF_INSTR_R2(instr); 8599 uint_t rd = DIF_INSTR_RD(instr); 8600 uint_t rs = DIF_INSTR_RS(instr); 8601 uint_t label = DIF_INSTR_LABEL(instr); 8602 uint_t v = DIF_INSTR_VAR(instr); 8603 uint_t subr = DIF_INSTR_SUBR(instr); 8604 uint_t type = DIF_INSTR_TYPE(instr); 8605 uint_t op = DIF_INSTR_OP(instr); 8606 8607 switch (op) { 8608 case DIF_OP_OR: 8609 case DIF_OP_XOR: 8610 case DIF_OP_AND: 8611 case DIF_OP_SLL: 8612 case DIF_OP_SRL: 8613 case DIF_OP_SRA: 8614 case DIF_OP_SUB: 8615 case DIF_OP_ADD: 8616 case DIF_OP_MUL: 8617 case DIF_OP_SDIV: 8618 case DIF_OP_UDIV: 8619 case DIF_OP_SREM: 8620 case DIF_OP_UREM: 8621 case DIF_OP_COPYS: 8622 if (r1 >= nregs) 8623 err += efunc(pc, "invalid register %u\n", r1); 8624 if (r2 >= nregs) 8625 err += efunc(pc, "invalid register %u\n", r2); 8626 if (rd >= nregs) 8627 err += efunc(pc, "invalid register %u\n", rd); 8628 if (rd == 0) 8629 err += efunc(pc, "cannot write to %r0\n"); 8630 break; 8631 case DIF_OP_NOT: 8632 case DIF_OP_MOV: 8633 case DIF_OP_ALLOCS: 8634 if (r1 >= nregs) 8635 err += efunc(pc, "invalid register %u\n", r1); 8636 if (r2 != 0) 8637 err += efunc(pc, "non-zero reserved bits\n"); 8638 if (rd >= nregs) 8639 err += efunc(pc, "invalid register %u\n", rd); 8640 if (rd == 0) 8641 err += efunc(pc, "cannot write to %r0\n"); 8642 break; 8643 case DIF_OP_LDSB: 8644 case DIF_OP_LDSH: 8645 case DIF_OP_LDSW: 8646 case DIF_OP_LDUB: 8647 case DIF_OP_LDUH: 8648 case DIF_OP_LDUW: 8649 case DIF_OP_LDX: 8650 if (r1 >= nregs) 8651 err += efunc(pc, "invalid register %u\n", r1); 8652 if (r2 != 0) 8653 err += efunc(pc, "non-zero reserved bits\n"); 8654 if (rd >= nregs) 8655 err += efunc(pc, "invalid register %u\n", rd); 8656 if (rd == 0) 8657 err += efunc(pc, "cannot write to %r0\n"); 8658 if (kcheckload) 8659 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 8660 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 8661 break; 8662 case DIF_OP_RLDSB: 8663 case DIF_OP_RLDSH: 8664 case DIF_OP_RLDSW: 8665 case DIF_OP_RLDUB: 8666 case DIF_OP_RLDUH: 8667 case DIF_OP_RLDUW: 8668 case DIF_OP_RLDX: 8669 if (r1 >= nregs) 8670 err += efunc(pc, "invalid register %u\n", r1); 8671 if (r2 != 0) 8672 err += efunc(pc, "non-zero reserved bits\n"); 8673 if (rd >= nregs) 8674 err += efunc(pc, "invalid register %u\n", rd); 8675 if (rd == 0) 8676 err += efunc(pc, "cannot write to %r0\n"); 8677 break; 8678 case DIF_OP_ULDSB: 8679 case DIF_OP_ULDSH: 8680 case DIF_OP_ULDSW: 8681 case DIF_OP_ULDUB: 8682 case DIF_OP_ULDUH: 8683 case DIF_OP_ULDUW: 8684 case DIF_OP_ULDX: 8685 if (r1 >= nregs) 8686 err += efunc(pc, "invalid register %u\n", r1); 8687 if (r2 != 0) 8688 err += efunc(pc, "non-zero reserved bits\n"); 8689 if (rd >= nregs) 8690 err += efunc(pc, "invalid register %u\n", rd); 8691 if (rd == 0) 8692 err += efunc(pc, "cannot write to %r0\n"); 8693 break; 8694 case DIF_OP_STB: 8695 case DIF_OP_STH: 8696 case DIF_OP_STW: 8697 case DIF_OP_STX: 8698 if (r1 >= nregs) 8699 err += efunc(pc, "invalid register %u\n", r1); 8700 if (r2 != 0) 8701 err += efunc(pc, "non-zero reserved bits\n"); 8702 if (rd >= nregs) 8703 err += efunc(pc, "invalid register %u\n", rd); 8704 if (rd == 0) 8705 err += efunc(pc, "cannot write to 0 address\n"); 8706 break; 8707 case DIF_OP_CMP: 8708 case DIF_OP_SCMP: 8709 if (r1 >= nregs) 8710 err += efunc(pc, "invalid register %u\n", r1); 8711 if (r2 >= nregs) 8712 err += efunc(pc, "invalid register %u\n", r2); 8713 if (rd != 0) 8714 err += efunc(pc, "non-zero reserved bits\n"); 8715 break; 8716 case DIF_OP_TST: 8717 if (r1 >= nregs) 8718 err += efunc(pc, "invalid register %u\n", r1); 8719 if (r2 != 0 || rd != 0) 8720 err += efunc(pc, "non-zero reserved bits\n"); 8721 break; 8722 case DIF_OP_BA: 8723 case DIF_OP_BE: 8724 case DIF_OP_BNE: 8725 case DIF_OP_BG: 8726 case DIF_OP_BGU: 8727 case DIF_OP_BGE: 8728 case DIF_OP_BGEU: 8729 case DIF_OP_BL: 8730 case DIF_OP_BLU: 8731 case DIF_OP_BLE: 8732 case DIF_OP_BLEU: 8733 if (label >= dp->dtdo_len) { 8734 err += efunc(pc, "invalid branch target %u\n", 8735 label); 8736 } 8737 if (label <= pc) { 8738 err += efunc(pc, "backward branch to %u\n", 8739 label); 8740 } 8741 break; 8742 case DIF_OP_RET: 8743 if (r1 != 0 || r2 != 0) 8744 err += efunc(pc, "non-zero reserved bits\n"); 8745 if (rd >= nregs) 8746 err += efunc(pc, "invalid register %u\n", rd); 8747 break; 8748 case DIF_OP_NOP: 8749 case DIF_OP_POPTS: 8750 case DIF_OP_FLUSHTS: 8751 if (r1 != 0 || r2 != 0 || rd != 0) 8752 err += efunc(pc, "non-zero reserved bits\n"); 8753 break; 8754 case DIF_OP_SETX: 8755 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 8756 err += efunc(pc, "invalid integer ref %u\n", 8757 DIF_INSTR_INTEGER(instr)); 8758 } 8759 if (rd >= nregs) 8760 err += efunc(pc, "invalid register %u\n", rd); 8761 if (rd == 0) 8762 err += efunc(pc, "cannot write to %r0\n"); 8763 break; 8764 case DIF_OP_SETS: 8765 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 8766 err += efunc(pc, "invalid string ref %u\n", 8767 DIF_INSTR_STRING(instr)); 8768 } 8769 if (rd >= nregs) 8770 err += efunc(pc, "invalid register %u\n", rd); 8771 if (rd == 0) 8772 err += efunc(pc, "cannot write to %r0\n"); 8773 break; 8774 case DIF_OP_LDGA: 8775 case DIF_OP_LDTA: 8776 if (r1 > DIF_VAR_ARRAY_MAX) 8777 err += efunc(pc, "invalid array %u\n", r1); 8778 if (r2 >= nregs) 8779 err += efunc(pc, "invalid register %u\n", r2); 8780 if (rd >= nregs) 8781 err += efunc(pc, "invalid register %u\n", rd); 8782 if (rd == 0) 8783 err += efunc(pc, "cannot write to %r0\n"); 8784 break; 8785 case DIF_OP_LDGS: 8786 case DIF_OP_LDTS: 8787 case DIF_OP_LDLS: 8788 case DIF_OP_LDGAA: 8789 case DIF_OP_LDTAA: 8790 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 8791 err += efunc(pc, "invalid variable %u\n", v); 8792 if (rd >= nregs) 8793 err += efunc(pc, "invalid register %u\n", rd); 8794 if (rd == 0) 8795 err += efunc(pc, "cannot write to %r0\n"); 8796 break; 8797 case DIF_OP_STGS: 8798 case DIF_OP_STTS: 8799 case DIF_OP_STLS: 8800 case DIF_OP_STGAA: 8801 case DIF_OP_STTAA: 8802 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 8803 err += efunc(pc, "invalid variable %u\n", v); 8804 if (rs >= nregs) 8805 err += efunc(pc, "invalid register %u\n", rd); 8806 break; 8807 case DIF_OP_CALL: 8808 if (subr > DIF_SUBR_MAX) 8809 err += efunc(pc, "invalid subr %u\n", subr); 8810 if (rd >= nregs) 8811 err += efunc(pc, "invalid register %u\n", rd); 8812 if (rd == 0) 8813 err += efunc(pc, "cannot write to %r0\n"); 8814 8815 if (subr == DIF_SUBR_COPYOUT || 8816 subr == DIF_SUBR_COPYOUTSTR) { 8817 dp->dtdo_destructive = 1; 8818 } 8819 break; 8820 case DIF_OP_PUSHTR: 8821 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 8822 err += efunc(pc, "invalid ref type %u\n", type); 8823 if (r2 >= nregs) 8824 err += efunc(pc, "invalid register %u\n", r2); 8825 if (rs >= nregs) 8826 err += efunc(pc, "invalid register %u\n", rs); 8827 break; 8828 case DIF_OP_PUSHTV: 8829 if (type != DIF_TYPE_CTF) 8830 err += efunc(pc, "invalid val type %u\n", type); 8831 if (r2 >= nregs) 8832 err += efunc(pc, "invalid register %u\n", r2); 8833 if (rs >= nregs) 8834 err += efunc(pc, "invalid register %u\n", rs); 8835 break; 8836 default: 8837 err += efunc(pc, "invalid opcode %u\n", 8838 DIF_INSTR_OP(instr)); 8839 } 8840 } 8841 8842 if (dp->dtdo_len != 0 && 8843 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 8844 err += efunc(dp->dtdo_len - 1, 8845 "expected 'ret' as last DIF instruction\n"); 8846 } 8847 8848 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 8849 /* 8850 * If we're not returning by reference, the size must be either 8851 * 0 or the size of one of the base types. 8852 */ 8853 switch (dp->dtdo_rtype.dtdt_size) { 8854 case 0: 8855 case sizeof (uint8_t): 8856 case sizeof (uint16_t): 8857 case sizeof (uint32_t): 8858 case sizeof (uint64_t): 8859 break; 8860 8861 default: 8862 err += efunc(dp->dtdo_len - 1, "bad return size"); 8863 } 8864 } 8865 8866 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 8867 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 8868 dtrace_diftype_t *vt, *et; 8869 uint_t id, ndx; 8870 8871 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 8872 v->dtdv_scope != DIFV_SCOPE_THREAD && 8873 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 8874 err += efunc(i, "unrecognized variable scope %d\n", 8875 v->dtdv_scope); 8876 break; 8877 } 8878 8879 if (v->dtdv_kind != DIFV_KIND_ARRAY && 8880 v->dtdv_kind != DIFV_KIND_SCALAR) { 8881 err += efunc(i, "unrecognized variable type %d\n", 8882 v->dtdv_kind); 8883 break; 8884 } 8885 8886 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 8887 err += efunc(i, "%d exceeds variable id limit\n", id); 8888 break; 8889 } 8890 8891 if (id < DIF_VAR_OTHER_UBASE) 8892 continue; 8893 8894 /* 8895 * For user-defined variables, we need to check that this 8896 * definition is identical to any previous definition that we 8897 * encountered. 8898 */ 8899 ndx = id - DIF_VAR_OTHER_UBASE; 8900 8901 switch (v->dtdv_scope) { 8902 case DIFV_SCOPE_GLOBAL: 8903 if (ndx < vstate->dtvs_nglobals) { 8904 dtrace_statvar_t *svar; 8905 8906 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 8907 existing = &svar->dtsv_var; 8908 } 8909 8910 break; 8911 8912 case DIFV_SCOPE_THREAD: 8913 if (ndx < vstate->dtvs_ntlocals) 8914 existing = &vstate->dtvs_tlocals[ndx]; 8915 break; 8916 8917 case DIFV_SCOPE_LOCAL: 8918 if (ndx < vstate->dtvs_nlocals) { 8919 dtrace_statvar_t *svar; 8920 8921 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 8922 existing = &svar->dtsv_var; 8923 } 8924 8925 break; 8926 } 8927 8928 vt = &v->dtdv_type; 8929 8930 if (vt->dtdt_flags & DIF_TF_BYREF) { 8931 if (vt->dtdt_size == 0) { 8932 err += efunc(i, "zero-sized variable\n"); 8933 break; 8934 } 8935 8936 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 8937 vt->dtdt_size > dtrace_global_maxsize) { 8938 err += efunc(i, "oversized by-ref global\n"); 8939 break; 8940 } 8941 } 8942 8943 if (existing == NULL || existing->dtdv_id == 0) 8944 continue; 8945 8946 ASSERT(existing->dtdv_id == v->dtdv_id); 8947 ASSERT(existing->dtdv_scope == v->dtdv_scope); 8948 8949 if (existing->dtdv_kind != v->dtdv_kind) 8950 err += efunc(i, "%d changed variable kind\n", id); 8951 8952 et = &existing->dtdv_type; 8953 8954 if (vt->dtdt_flags != et->dtdt_flags) { 8955 err += efunc(i, "%d changed variable type flags\n", id); 8956 break; 8957 } 8958 8959 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 8960 err += efunc(i, "%d changed variable type size\n", id); 8961 break; 8962 } 8963 } 8964 8965 return (err); 8966 } 8967 8968 /* 8969 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 8970 * are much more constrained than normal DIFOs. Specifically, they may 8971 * not: 8972 * 8973 * 1. Make calls to subroutines other than copyin(), copyinstr() or 8974 * miscellaneous string routines 8975 * 2. Access DTrace variables other than the args[] array, and the 8976 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 8977 * 3. Have thread-local variables. 8978 * 4. Have dynamic variables. 8979 */ 8980 static int 8981 dtrace_difo_validate_helper(dtrace_difo_t *dp) 8982 { 8983 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8984 int err = 0; 8985 uint_t pc; 8986 8987 for (pc = 0; pc < dp->dtdo_len; pc++) { 8988 dif_instr_t instr = dp->dtdo_buf[pc]; 8989 8990 uint_t v = DIF_INSTR_VAR(instr); 8991 uint_t subr = DIF_INSTR_SUBR(instr); 8992 uint_t op = DIF_INSTR_OP(instr); 8993 8994 switch (op) { 8995 case DIF_OP_OR: 8996 case DIF_OP_XOR: 8997 case DIF_OP_AND: 8998 case DIF_OP_SLL: 8999 case DIF_OP_SRL: 9000 case DIF_OP_SRA: 9001 case DIF_OP_SUB: 9002 case DIF_OP_ADD: 9003 case DIF_OP_MUL: 9004 case DIF_OP_SDIV: 9005 case DIF_OP_UDIV: 9006 case DIF_OP_SREM: 9007 case DIF_OP_UREM: 9008 case DIF_OP_COPYS: 9009 case DIF_OP_NOT: 9010 case DIF_OP_MOV: 9011 case DIF_OP_RLDSB: 9012 case DIF_OP_RLDSH: 9013 case DIF_OP_RLDSW: 9014 case DIF_OP_RLDUB: 9015 case DIF_OP_RLDUH: 9016 case DIF_OP_RLDUW: 9017 case DIF_OP_RLDX: 9018 case DIF_OP_ULDSB: 9019 case DIF_OP_ULDSH: 9020 case DIF_OP_ULDSW: 9021 case DIF_OP_ULDUB: 9022 case DIF_OP_ULDUH: 9023 case DIF_OP_ULDUW: 9024 case DIF_OP_ULDX: 9025 case DIF_OP_STB: 9026 case DIF_OP_STH: 9027 case DIF_OP_STW: 9028 case DIF_OP_STX: 9029 case DIF_OP_ALLOCS: 9030 case DIF_OP_CMP: 9031 case DIF_OP_SCMP: 9032 case DIF_OP_TST: 9033 case DIF_OP_BA: 9034 case DIF_OP_BE: 9035 case DIF_OP_BNE: 9036 case DIF_OP_BG: 9037 case DIF_OP_BGU: 9038 case DIF_OP_BGE: 9039 case DIF_OP_BGEU: 9040 case DIF_OP_BL: 9041 case DIF_OP_BLU: 9042 case DIF_OP_BLE: 9043 case DIF_OP_BLEU: 9044 case DIF_OP_RET: 9045 case DIF_OP_NOP: 9046 case DIF_OP_POPTS: 9047 case DIF_OP_FLUSHTS: 9048 case DIF_OP_SETX: 9049 case DIF_OP_SETS: 9050 case DIF_OP_LDGA: 9051 case DIF_OP_LDLS: 9052 case DIF_OP_STGS: 9053 case DIF_OP_STLS: 9054 case DIF_OP_PUSHTR: 9055 case DIF_OP_PUSHTV: 9056 break; 9057 9058 case DIF_OP_LDGS: 9059 if (v >= DIF_VAR_OTHER_UBASE) 9060 break; 9061 9062 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 9063 break; 9064 9065 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 9066 v == DIF_VAR_PPID || v == DIF_VAR_TID || 9067 v == DIF_VAR_EXECARGS || 9068 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 9069 v == DIF_VAR_UID || v == DIF_VAR_GID) 9070 break; 9071 9072 err += efunc(pc, "illegal variable %u\n", v); 9073 break; 9074 9075 case DIF_OP_LDTA: 9076 case DIF_OP_LDTS: 9077 case DIF_OP_LDGAA: 9078 case DIF_OP_LDTAA: 9079 err += efunc(pc, "illegal dynamic variable load\n"); 9080 break; 9081 9082 case DIF_OP_STTS: 9083 case DIF_OP_STGAA: 9084 case DIF_OP_STTAA: 9085 err += efunc(pc, "illegal dynamic variable store\n"); 9086 break; 9087 9088 case DIF_OP_CALL: 9089 if (subr == DIF_SUBR_ALLOCA || 9090 subr == DIF_SUBR_BCOPY || 9091 subr == DIF_SUBR_COPYIN || 9092 subr == DIF_SUBR_COPYINTO || 9093 subr == DIF_SUBR_COPYINSTR || 9094 subr == DIF_SUBR_INDEX || 9095 subr == DIF_SUBR_INET_NTOA || 9096 subr == DIF_SUBR_INET_NTOA6 || 9097 subr == DIF_SUBR_INET_NTOP || 9098 subr == DIF_SUBR_LLTOSTR || 9099 subr == DIF_SUBR_RINDEX || 9100 subr == DIF_SUBR_STRCHR || 9101 subr == DIF_SUBR_STRJOIN || 9102 subr == DIF_SUBR_STRRCHR || 9103 subr == DIF_SUBR_STRSTR || 9104 subr == DIF_SUBR_HTONS || 9105 subr == DIF_SUBR_HTONL || 9106 subr == DIF_SUBR_HTONLL || 9107 subr == DIF_SUBR_NTOHS || 9108 subr == DIF_SUBR_NTOHL || 9109 subr == DIF_SUBR_NTOHLL || 9110 subr == DIF_SUBR_MEMREF || 9111 subr == DIF_SUBR_TYPEREF) 9112 break; 9113 9114 err += efunc(pc, "invalid subr %u\n", subr); 9115 break; 9116 9117 default: 9118 err += efunc(pc, "invalid opcode %u\n", 9119 DIF_INSTR_OP(instr)); 9120 } 9121 } 9122 9123 return (err); 9124 } 9125 9126 /* 9127 * Returns 1 if the expression in the DIF object can be cached on a per-thread 9128 * basis; 0 if not. 9129 */ 9130 static int 9131 dtrace_difo_cacheable(dtrace_difo_t *dp) 9132 { 9133 int i; 9134 9135 if (dp == NULL) 9136 return (0); 9137 9138 for (i = 0; i < dp->dtdo_varlen; i++) { 9139 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9140 9141 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 9142 continue; 9143 9144 switch (v->dtdv_id) { 9145 case DIF_VAR_CURTHREAD: 9146 case DIF_VAR_PID: 9147 case DIF_VAR_TID: 9148 case DIF_VAR_EXECARGS: 9149 case DIF_VAR_EXECNAME: 9150 case DIF_VAR_ZONENAME: 9151 break; 9152 9153 default: 9154 return (0); 9155 } 9156 } 9157 9158 /* 9159 * This DIF object may be cacheable. Now we need to look for any 9160 * array loading instructions, any memory loading instructions, or 9161 * any stores to thread-local variables. 9162 */ 9163 for (i = 0; i < dp->dtdo_len; i++) { 9164 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 9165 9166 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 9167 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 9168 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 9169 op == DIF_OP_LDGA || op == DIF_OP_STTS) 9170 return (0); 9171 } 9172 9173 return (1); 9174 } 9175 9176 static void 9177 dtrace_difo_hold(dtrace_difo_t *dp) 9178 { 9179 int i; 9180 9181 ASSERT(MUTEX_HELD(&dtrace_lock)); 9182 9183 dp->dtdo_refcnt++; 9184 ASSERT(dp->dtdo_refcnt != 0); 9185 9186 /* 9187 * We need to check this DIF object for references to the variable 9188 * DIF_VAR_VTIMESTAMP. 9189 */ 9190 for (i = 0; i < dp->dtdo_varlen; i++) { 9191 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9192 9193 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9194 continue; 9195 9196 if (dtrace_vtime_references++ == 0) 9197 dtrace_vtime_enable(); 9198 } 9199 } 9200 9201 /* 9202 * This routine calculates the dynamic variable chunksize for a given DIF 9203 * object. The calculation is not fool-proof, and can probably be tricked by 9204 * malicious DIF -- but it works for all compiler-generated DIF. Because this 9205 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 9206 * if a dynamic variable size exceeds the chunksize. 9207 */ 9208 static void 9209 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9210 { 9211 uint64_t sval = 0; 9212 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 9213 const dif_instr_t *text = dp->dtdo_buf; 9214 uint_t pc, srd = 0; 9215 uint_t ttop = 0; 9216 size_t size, ksize; 9217 uint_t id, i; 9218 9219 for (pc = 0; pc < dp->dtdo_len; pc++) { 9220 dif_instr_t instr = text[pc]; 9221 uint_t op = DIF_INSTR_OP(instr); 9222 uint_t rd = DIF_INSTR_RD(instr); 9223 uint_t r1 = DIF_INSTR_R1(instr); 9224 uint_t nkeys = 0; 9225 uchar_t scope = 0; 9226 9227 dtrace_key_t *key = tupregs; 9228 9229 switch (op) { 9230 case DIF_OP_SETX: 9231 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 9232 srd = rd; 9233 continue; 9234 9235 case DIF_OP_STTS: 9236 key = &tupregs[DIF_DTR_NREGS]; 9237 key[0].dttk_size = 0; 9238 key[1].dttk_size = 0; 9239 nkeys = 2; 9240 scope = DIFV_SCOPE_THREAD; 9241 break; 9242 9243 case DIF_OP_STGAA: 9244 case DIF_OP_STTAA: 9245 nkeys = ttop; 9246 9247 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 9248 key[nkeys++].dttk_size = 0; 9249 9250 key[nkeys++].dttk_size = 0; 9251 9252 if (op == DIF_OP_STTAA) { 9253 scope = DIFV_SCOPE_THREAD; 9254 } else { 9255 scope = DIFV_SCOPE_GLOBAL; 9256 } 9257 9258 break; 9259 9260 case DIF_OP_PUSHTR: 9261 if (ttop == DIF_DTR_NREGS) 9262 return; 9263 9264 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 9265 /* 9266 * If the register for the size of the "pushtr" 9267 * is %r0 (or the value is 0) and the type is 9268 * a string, we'll use the system-wide default 9269 * string size. 9270 */ 9271 tupregs[ttop++].dttk_size = 9272 dtrace_strsize_default; 9273 } else { 9274 if (srd == 0) 9275 return; 9276 9277 tupregs[ttop++].dttk_size = sval; 9278 } 9279 9280 break; 9281 9282 case DIF_OP_PUSHTV: 9283 if (ttop == DIF_DTR_NREGS) 9284 return; 9285 9286 tupregs[ttop++].dttk_size = 0; 9287 break; 9288 9289 case DIF_OP_FLUSHTS: 9290 ttop = 0; 9291 break; 9292 9293 case DIF_OP_POPTS: 9294 if (ttop != 0) 9295 ttop--; 9296 break; 9297 } 9298 9299 sval = 0; 9300 srd = 0; 9301 9302 if (nkeys == 0) 9303 continue; 9304 9305 /* 9306 * We have a dynamic variable allocation; calculate its size. 9307 */ 9308 for (ksize = 0, i = 0; i < nkeys; i++) 9309 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 9310 9311 size = sizeof (dtrace_dynvar_t); 9312 size += sizeof (dtrace_key_t) * (nkeys - 1); 9313 size += ksize; 9314 9315 /* 9316 * Now we need to determine the size of the stored data. 9317 */ 9318 id = DIF_INSTR_VAR(instr); 9319 9320 for (i = 0; i < dp->dtdo_varlen; i++) { 9321 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9322 9323 if (v->dtdv_id == id && v->dtdv_scope == scope) { 9324 size += v->dtdv_type.dtdt_size; 9325 break; 9326 } 9327 } 9328 9329 if (i == dp->dtdo_varlen) 9330 return; 9331 9332 /* 9333 * We have the size. If this is larger than the chunk size 9334 * for our dynamic variable state, reset the chunk size. 9335 */ 9336 size = P2ROUNDUP(size, sizeof (uint64_t)); 9337 9338 if (size > vstate->dtvs_dynvars.dtds_chunksize) 9339 vstate->dtvs_dynvars.dtds_chunksize = size; 9340 } 9341 } 9342 9343 static void 9344 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9345 { 9346 int i, oldsvars, osz, nsz, otlocals, ntlocals; 9347 uint_t id; 9348 9349 ASSERT(MUTEX_HELD(&dtrace_lock)); 9350 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 9351 9352 for (i = 0; i < dp->dtdo_varlen; i++) { 9353 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9354 dtrace_statvar_t *svar, ***svarp = NULL; 9355 size_t dsize = 0; 9356 uint8_t scope = v->dtdv_scope; 9357 int *np = NULL; 9358 9359 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9360 continue; 9361 9362 id -= DIF_VAR_OTHER_UBASE; 9363 9364 switch (scope) { 9365 case DIFV_SCOPE_THREAD: 9366 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 9367 dtrace_difv_t *tlocals; 9368 9369 if ((ntlocals = (otlocals << 1)) == 0) 9370 ntlocals = 1; 9371 9372 osz = otlocals * sizeof (dtrace_difv_t); 9373 nsz = ntlocals * sizeof (dtrace_difv_t); 9374 9375 tlocals = kmem_zalloc(nsz, KM_SLEEP); 9376 9377 if (osz != 0) { 9378 bcopy(vstate->dtvs_tlocals, 9379 tlocals, osz); 9380 kmem_free(vstate->dtvs_tlocals, osz); 9381 } 9382 9383 vstate->dtvs_tlocals = tlocals; 9384 vstate->dtvs_ntlocals = ntlocals; 9385 } 9386 9387 vstate->dtvs_tlocals[id] = *v; 9388 continue; 9389 9390 case DIFV_SCOPE_LOCAL: 9391 np = &vstate->dtvs_nlocals; 9392 svarp = &vstate->dtvs_locals; 9393 9394 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9395 dsize = NCPU * (v->dtdv_type.dtdt_size + 9396 sizeof (uint64_t)); 9397 else 9398 dsize = NCPU * sizeof (uint64_t); 9399 9400 break; 9401 9402 case DIFV_SCOPE_GLOBAL: 9403 np = &vstate->dtvs_nglobals; 9404 svarp = &vstate->dtvs_globals; 9405 9406 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9407 dsize = v->dtdv_type.dtdt_size + 9408 sizeof (uint64_t); 9409 9410 break; 9411 9412 default: 9413 ASSERT(0); 9414 } 9415 9416 while (id >= (oldsvars = *np)) { 9417 dtrace_statvar_t **statics; 9418 int newsvars, oldsize, newsize; 9419 9420 if ((newsvars = (oldsvars << 1)) == 0) 9421 newsvars = 1; 9422 9423 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 9424 newsize = newsvars * sizeof (dtrace_statvar_t *); 9425 9426 statics = kmem_zalloc(newsize, KM_SLEEP); 9427 9428 if (oldsize != 0) { 9429 bcopy(*svarp, statics, oldsize); 9430 kmem_free(*svarp, oldsize); 9431 } 9432 9433 *svarp = statics; 9434 *np = newsvars; 9435 } 9436 9437 if ((svar = (*svarp)[id]) == NULL) { 9438 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 9439 svar->dtsv_var = *v; 9440 9441 if ((svar->dtsv_size = dsize) != 0) { 9442 svar->dtsv_data = (uint64_t)(uintptr_t) 9443 kmem_zalloc(dsize, KM_SLEEP); 9444 } 9445 9446 (*svarp)[id] = svar; 9447 } 9448 9449 svar->dtsv_refcnt++; 9450 } 9451 9452 dtrace_difo_chunksize(dp, vstate); 9453 dtrace_difo_hold(dp); 9454 } 9455 9456 static dtrace_difo_t * 9457 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9458 { 9459 dtrace_difo_t *new; 9460 size_t sz; 9461 9462 ASSERT(dp->dtdo_buf != NULL); 9463 ASSERT(dp->dtdo_refcnt != 0); 9464 9465 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 9466 9467 ASSERT(dp->dtdo_buf != NULL); 9468 sz = dp->dtdo_len * sizeof (dif_instr_t); 9469 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 9470 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 9471 new->dtdo_len = dp->dtdo_len; 9472 9473 if (dp->dtdo_strtab != NULL) { 9474 ASSERT(dp->dtdo_strlen != 0); 9475 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 9476 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 9477 new->dtdo_strlen = dp->dtdo_strlen; 9478 } 9479 9480 if (dp->dtdo_inttab != NULL) { 9481 ASSERT(dp->dtdo_intlen != 0); 9482 sz = dp->dtdo_intlen * sizeof (uint64_t); 9483 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 9484 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 9485 new->dtdo_intlen = dp->dtdo_intlen; 9486 } 9487 9488 if (dp->dtdo_vartab != NULL) { 9489 ASSERT(dp->dtdo_varlen != 0); 9490 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 9491 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 9492 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 9493 new->dtdo_varlen = dp->dtdo_varlen; 9494 } 9495 9496 dtrace_difo_init(new, vstate); 9497 return (new); 9498 } 9499 9500 static void 9501 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9502 { 9503 int i; 9504 9505 ASSERT(dp->dtdo_refcnt == 0); 9506 9507 for (i = 0; i < dp->dtdo_varlen; i++) { 9508 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9509 dtrace_statvar_t *svar, **svarp = NULL; 9510 uint_t id; 9511 uint8_t scope = v->dtdv_scope; 9512 int *np = NULL; 9513 9514 switch (scope) { 9515 case DIFV_SCOPE_THREAD: 9516 continue; 9517 9518 case DIFV_SCOPE_LOCAL: 9519 np = &vstate->dtvs_nlocals; 9520 svarp = vstate->dtvs_locals; 9521 break; 9522 9523 case DIFV_SCOPE_GLOBAL: 9524 np = &vstate->dtvs_nglobals; 9525 svarp = vstate->dtvs_globals; 9526 break; 9527 9528 default: 9529 ASSERT(0); 9530 } 9531 9532 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9533 continue; 9534 9535 id -= DIF_VAR_OTHER_UBASE; 9536 ASSERT(id < *np); 9537 9538 svar = svarp[id]; 9539 ASSERT(svar != NULL); 9540 ASSERT(svar->dtsv_refcnt > 0); 9541 9542 if (--svar->dtsv_refcnt > 0) 9543 continue; 9544 9545 if (svar->dtsv_size != 0) { 9546 ASSERT(svar->dtsv_data != 0); 9547 kmem_free((void *)(uintptr_t)svar->dtsv_data, 9548 svar->dtsv_size); 9549 } 9550 9551 kmem_free(svar, sizeof (dtrace_statvar_t)); 9552 svarp[id] = NULL; 9553 } 9554 9555 if (dp->dtdo_buf != NULL) 9556 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 9557 if (dp->dtdo_inttab != NULL) 9558 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 9559 if (dp->dtdo_strtab != NULL) 9560 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 9561 if (dp->dtdo_vartab != NULL) 9562 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 9563 9564 kmem_free(dp, sizeof (dtrace_difo_t)); 9565 } 9566 9567 static void 9568 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9569 { 9570 int i; 9571 9572 ASSERT(MUTEX_HELD(&dtrace_lock)); 9573 ASSERT(dp->dtdo_refcnt != 0); 9574 9575 for (i = 0; i < dp->dtdo_varlen; i++) { 9576 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9577 9578 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9579 continue; 9580 9581 ASSERT(dtrace_vtime_references > 0); 9582 if (--dtrace_vtime_references == 0) 9583 dtrace_vtime_disable(); 9584 } 9585 9586 if (--dp->dtdo_refcnt == 0) 9587 dtrace_difo_destroy(dp, vstate); 9588 } 9589 9590 /* 9591 * DTrace Format Functions 9592 */ 9593 static uint16_t 9594 dtrace_format_add(dtrace_state_t *state, char *str) 9595 { 9596 char *fmt, **new; 9597 uint16_t ndx, len = strlen(str) + 1; 9598 9599 fmt = kmem_zalloc(len, KM_SLEEP); 9600 bcopy(str, fmt, len); 9601 9602 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 9603 if (state->dts_formats[ndx] == NULL) { 9604 state->dts_formats[ndx] = fmt; 9605 return (ndx + 1); 9606 } 9607 } 9608 9609 if (state->dts_nformats == USHRT_MAX) { 9610 /* 9611 * This is only likely if a denial-of-service attack is being 9612 * attempted. As such, it's okay to fail silently here. 9613 */ 9614 kmem_free(fmt, len); 9615 return (0); 9616 } 9617 9618 /* 9619 * For simplicity, we always resize the formats array to be exactly the 9620 * number of formats. 9621 */ 9622 ndx = state->dts_nformats++; 9623 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 9624 9625 if (state->dts_formats != NULL) { 9626 ASSERT(ndx != 0); 9627 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 9628 kmem_free(state->dts_formats, ndx * sizeof (char *)); 9629 } 9630 9631 state->dts_formats = new; 9632 state->dts_formats[ndx] = fmt; 9633 9634 return (ndx + 1); 9635 } 9636 9637 static void 9638 dtrace_format_remove(dtrace_state_t *state, uint16_t format) 9639 { 9640 char *fmt; 9641 9642 ASSERT(state->dts_formats != NULL); 9643 ASSERT(format <= state->dts_nformats); 9644 ASSERT(state->dts_formats[format - 1] != NULL); 9645 9646 fmt = state->dts_formats[format - 1]; 9647 kmem_free(fmt, strlen(fmt) + 1); 9648 state->dts_formats[format - 1] = NULL; 9649 } 9650 9651 static void 9652 dtrace_format_destroy(dtrace_state_t *state) 9653 { 9654 int i; 9655 9656 if (state->dts_nformats == 0) { 9657 ASSERT(state->dts_formats == NULL); 9658 return; 9659 } 9660 9661 ASSERT(state->dts_formats != NULL); 9662 9663 for (i = 0; i < state->dts_nformats; i++) { 9664 char *fmt = state->dts_formats[i]; 9665 9666 if (fmt == NULL) 9667 continue; 9668 9669 kmem_free(fmt, strlen(fmt) + 1); 9670 } 9671 9672 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 9673 state->dts_nformats = 0; 9674 state->dts_formats = NULL; 9675 } 9676 9677 /* 9678 * DTrace Predicate Functions 9679 */ 9680 static dtrace_predicate_t * 9681 dtrace_predicate_create(dtrace_difo_t *dp) 9682 { 9683 dtrace_predicate_t *pred; 9684 9685 ASSERT(MUTEX_HELD(&dtrace_lock)); 9686 ASSERT(dp->dtdo_refcnt != 0); 9687 9688 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 9689 pred->dtp_difo = dp; 9690 pred->dtp_refcnt = 1; 9691 9692 if (!dtrace_difo_cacheable(dp)) 9693 return (pred); 9694 9695 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 9696 /* 9697 * This is only theoretically possible -- we have had 2^32 9698 * cacheable predicates on this machine. We cannot allow any 9699 * more predicates to become cacheable: as unlikely as it is, 9700 * there may be a thread caching a (now stale) predicate cache 9701 * ID. (N.B.: the temptation is being successfully resisted to 9702 * have this cmn_err() "Holy shit -- we executed this code!") 9703 */ 9704 return (pred); 9705 } 9706 9707 pred->dtp_cacheid = dtrace_predcache_id++; 9708 9709 return (pred); 9710 } 9711 9712 static void 9713 dtrace_predicate_hold(dtrace_predicate_t *pred) 9714 { 9715 ASSERT(MUTEX_HELD(&dtrace_lock)); 9716 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 9717 ASSERT(pred->dtp_refcnt > 0); 9718 9719 pred->dtp_refcnt++; 9720 } 9721 9722 static void 9723 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 9724 { 9725 dtrace_difo_t *dp = pred->dtp_difo; 9726 9727 ASSERT(MUTEX_HELD(&dtrace_lock)); 9728 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 9729 ASSERT(pred->dtp_refcnt > 0); 9730 9731 if (--pred->dtp_refcnt == 0) { 9732 dtrace_difo_release(pred->dtp_difo, vstate); 9733 kmem_free(pred, sizeof (dtrace_predicate_t)); 9734 } 9735 } 9736 9737 /* 9738 * DTrace Action Description Functions 9739 */ 9740 static dtrace_actdesc_t * 9741 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 9742 uint64_t uarg, uint64_t arg) 9743 { 9744 dtrace_actdesc_t *act; 9745 9746 #if defined(sun) 9747 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 9748 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 9749 #endif 9750 9751 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 9752 act->dtad_kind = kind; 9753 act->dtad_ntuple = ntuple; 9754 act->dtad_uarg = uarg; 9755 act->dtad_arg = arg; 9756 act->dtad_refcnt = 1; 9757 9758 return (act); 9759 } 9760 9761 static void 9762 dtrace_actdesc_hold(dtrace_actdesc_t *act) 9763 { 9764 ASSERT(act->dtad_refcnt >= 1); 9765 act->dtad_refcnt++; 9766 } 9767 9768 static void 9769 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 9770 { 9771 dtrace_actkind_t kind = act->dtad_kind; 9772 dtrace_difo_t *dp; 9773 9774 ASSERT(act->dtad_refcnt >= 1); 9775 9776 if (--act->dtad_refcnt != 0) 9777 return; 9778 9779 if ((dp = act->dtad_difo) != NULL) 9780 dtrace_difo_release(dp, vstate); 9781 9782 if (DTRACEACT_ISPRINTFLIKE(kind)) { 9783 char *str = (char *)(uintptr_t)act->dtad_arg; 9784 9785 #if defined(sun) 9786 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 9787 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 9788 #endif 9789 9790 if (str != NULL) 9791 kmem_free(str, strlen(str) + 1); 9792 } 9793 9794 kmem_free(act, sizeof (dtrace_actdesc_t)); 9795 } 9796 9797 /* 9798 * DTrace ECB Functions 9799 */ 9800 static dtrace_ecb_t * 9801 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 9802 { 9803 dtrace_ecb_t *ecb; 9804 dtrace_epid_t epid; 9805 9806 ASSERT(MUTEX_HELD(&dtrace_lock)); 9807 9808 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 9809 ecb->dte_predicate = NULL; 9810 ecb->dte_probe = probe; 9811 9812 /* 9813 * The default size is the size of the default action: recording 9814 * the header. 9815 */ 9816 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 9817 ecb->dte_alignment = sizeof (dtrace_epid_t); 9818 9819 epid = state->dts_epid++; 9820 9821 if (epid - 1 >= state->dts_necbs) { 9822 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 9823 int necbs = state->dts_necbs << 1; 9824 9825 ASSERT(epid == state->dts_necbs + 1); 9826 9827 if (necbs == 0) { 9828 ASSERT(oecbs == NULL); 9829 necbs = 1; 9830 } 9831 9832 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 9833 9834 if (oecbs != NULL) 9835 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 9836 9837 dtrace_membar_producer(); 9838 state->dts_ecbs = ecbs; 9839 9840 if (oecbs != NULL) { 9841 /* 9842 * If this state is active, we must dtrace_sync() 9843 * before we can free the old dts_ecbs array: we're 9844 * coming in hot, and there may be active ring 9845 * buffer processing (which indexes into the dts_ecbs 9846 * array) on another CPU. 9847 */ 9848 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 9849 dtrace_sync(); 9850 9851 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 9852 } 9853 9854 dtrace_membar_producer(); 9855 state->dts_necbs = necbs; 9856 } 9857 9858 ecb->dte_state = state; 9859 9860 ASSERT(state->dts_ecbs[epid - 1] == NULL); 9861 dtrace_membar_producer(); 9862 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 9863 9864 return (ecb); 9865 } 9866 9867 static void 9868 dtrace_ecb_enable(dtrace_ecb_t *ecb) 9869 { 9870 dtrace_probe_t *probe = ecb->dte_probe; 9871 9872 ASSERT(MUTEX_HELD(&cpu_lock)); 9873 ASSERT(MUTEX_HELD(&dtrace_lock)); 9874 ASSERT(ecb->dte_next == NULL); 9875 9876 if (probe == NULL) { 9877 /* 9878 * This is the NULL probe -- there's nothing to do. 9879 */ 9880 return; 9881 } 9882 9883 if (probe->dtpr_ecb == NULL) { 9884 dtrace_provider_t *prov = probe->dtpr_provider; 9885 9886 /* 9887 * We're the first ECB on this probe. 9888 */ 9889 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 9890 9891 if (ecb->dte_predicate != NULL) 9892 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 9893 9894 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 9895 probe->dtpr_id, probe->dtpr_arg); 9896 } else { 9897 /* 9898 * This probe is already active. Swing the last pointer to 9899 * point to the new ECB, and issue a dtrace_sync() to assure 9900 * that all CPUs have seen the change. 9901 */ 9902 ASSERT(probe->dtpr_ecb_last != NULL); 9903 probe->dtpr_ecb_last->dte_next = ecb; 9904 probe->dtpr_ecb_last = ecb; 9905 probe->dtpr_predcache = 0; 9906 9907 dtrace_sync(); 9908 } 9909 } 9910 9911 static void 9912 dtrace_ecb_resize(dtrace_ecb_t *ecb) 9913 { 9914 dtrace_action_t *act; 9915 uint32_t curneeded = UINT32_MAX; 9916 uint32_t aggbase = UINT32_MAX; 9917 9918 /* 9919 * If we record anything, we always record the dtrace_rechdr_t. (And 9920 * we always record it first.) 9921 */ 9922 ecb->dte_size = sizeof (dtrace_rechdr_t); 9923 ecb->dte_alignment = sizeof (dtrace_epid_t); 9924 9925 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9926 dtrace_recdesc_t *rec = &act->dta_rec; 9927 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 9928 9929 ecb->dte_alignment = MAX(ecb->dte_alignment, 9930 rec->dtrd_alignment); 9931 9932 if (DTRACEACT_ISAGG(act->dta_kind)) { 9933 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9934 9935 ASSERT(rec->dtrd_size != 0); 9936 ASSERT(agg->dtag_first != NULL); 9937 ASSERT(act->dta_prev->dta_intuple); 9938 ASSERT(aggbase != UINT32_MAX); 9939 ASSERT(curneeded != UINT32_MAX); 9940 9941 agg->dtag_base = aggbase; 9942 9943 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 9944 rec->dtrd_offset = curneeded; 9945 curneeded += rec->dtrd_size; 9946 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 9947 9948 aggbase = UINT32_MAX; 9949 curneeded = UINT32_MAX; 9950 } else if (act->dta_intuple) { 9951 if (curneeded == UINT32_MAX) { 9952 /* 9953 * This is the first record in a tuple. Align 9954 * curneeded to be at offset 4 in an 8-byte 9955 * aligned block. 9956 */ 9957 ASSERT(act->dta_prev == NULL || 9958 !act->dta_prev->dta_intuple); 9959 ASSERT3U(aggbase, ==, UINT32_MAX); 9960 curneeded = P2PHASEUP(ecb->dte_size, 9961 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 9962 9963 aggbase = curneeded - sizeof (dtrace_aggid_t); 9964 ASSERT(IS_P2ALIGNED(aggbase, 9965 sizeof (uint64_t))); 9966 } 9967 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 9968 rec->dtrd_offset = curneeded; 9969 curneeded += rec->dtrd_size; 9970 } else { 9971 /* tuples must be followed by an aggregation */ 9972 ASSERT(act->dta_prev == NULL || 9973 !act->dta_prev->dta_intuple); 9974 9975 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 9976 rec->dtrd_alignment); 9977 rec->dtrd_offset = ecb->dte_size; 9978 ecb->dte_size += rec->dtrd_size; 9979 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 9980 } 9981 } 9982 9983 if ((act = ecb->dte_action) != NULL && 9984 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 9985 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 9986 /* 9987 * If the size is still sizeof (dtrace_rechdr_t), then all 9988 * actions store no data; set the size to 0. 9989 */ 9990 ecb->dte_size = 0; 9991 } 9992 9993 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 9994 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 9995 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 9996 ecb->dte_needed); 9997 } 9998 9999 static dtrace_action_t * 10000 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 10001 { 10002 dtrace_aggregation_t *agg; 10003 size_t size = sizeof (uint64_t); 10004 int ntuple = desc->dtad_ntuple; 10005 dtrace_action_t *act; 10006 dtrace_recdesc_t *frec; 10007 dtrace_aggid_t aggid; 10008 dtrace_state_t *state = ecb->dte_state; 10009 10010 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 10011 agg->dtag_ecb = ecb; 10012 10013 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 10014 10015 switch (desc->dtad_kind) { 10016 case DTRACEAGG_MIN: 10017 agg->dtag_initial = INT64_MAX; 10018 agg->dtag_aggregate = dtrace_aggregate_min; 10019 break; 10020 10021 case DTRACEAGG_MAX: 10022 agg->dtag_initial = INT64_MIN; 10023 agg->dtag_aggregate = dtrace_aggregate_max; 10024 break; 10025 10026 case DTRACEAGG_COUNT: 10027 agg->dtag_aggregate = dtrace_aggregate_count; 10028 break; 10029 10030 case DTRACEAGG_QUANTIZE: 10031 agg->dtag_aggregate = dtrace_aggregate_quantize; 10032 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 10033 sizeof (uint64_t); 10034 break; 10035 10036 case DTRACEAGG_LQUANTIZE: { 10037 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 10038 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 10039 10040 agg->dtag_initial = desc->dtad_arg; 10041 agg->dtag_aggregate = dtrace_aggregate_lquantize; 10042 10043 if (step == 0 || levels == 0) 10044 goto err; 10045 10046 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 10047 break; 10048 } 10049 10050 case DTRACEAGG_LLQUANTIZE: { 10051 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 10052 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 10053 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 10054 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 10055 int64_t v; 10056 10057 agg->dtag_initial = desc->dtad_arg; 10058 agg->dtag_aggregate = dtrace_aggregate_llquantize; 10059 10060 if (factor < 2 || low >= high || nsteps < factor) 10061 goto err; 10062 10063 /* 10064 * Now check that the number of steps evenly divides a power 10065 * of the factor. (This assures both integer bucket size and 10066 * linearity within each magnitude.) 10067 */ 10068 for (v = factor; v < nsteps; v *= factor) 10069 continue; 10070 10071 if ((v % nsteps) || (nsteps % factor)) 10072 goto err; 10073 10074 size = (dtrace_aggregate_llquantize_bucket(factor, 10075 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 10076 break; 10077 } 10078 10079 case DTRACEAGG_AVG: 10080 agg->dtag_aggregate = dtrace_aggregate_avg; 10081 size = sizeof (uint64_t) * 2; 10082 break; 10083 10084 case DTRACEAGG_STDDEV: 10085 agg->dtag_aggregate = dtrace_aggregate_stddev; 10086 size = sizeof (uint64_t) * 4; 10087 break; 10088 10089 case DTRACEAGG_SUM: 10090 agg->dtag_aggregate = dtrace_aggregate_sum; 10091 break; 10092 10093 default: 10094 goto err; 10095 } 10096 10097 agg->dtag_action.dta_rec.dtrd_size = size; 10098 10099 if (ntuple == 0) 10100 goto err; 10101 10102 /* 10103 * We must make sure that we have enough actions for the n-tuple. 10104 */ 10105 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 10106 if (DTRACEACT_ISAGG(act->dta_kind)) 10107 break; 10108 10109 if (--ntuple == 0) { 10110 /* 10111 * This is the action with which our n-tuple begins. 10112 */ 10113 agg->dtag_first = act; 10114 goto success; 10115 } 10116 } 10117 10118 /* 10119 * This n-tuple is short by ntuple elements. Return failure. 10120 */ 10121 ASSERT(ntuple != 0); 10122 err: 10123 kmem_free(agg, sizeof (dtrace_aggregation_t)); 10124 return (NULL); 10125 10126 success: 10127 /* 10128 * If the last action in the tuple has a size of zero, it's actually 10129 * an expression argument for the aggregating action. 10130 */ 10131 ASSERT(ecb->dte_action_last != NULL); 10132 act = ecb->dte_action_last; 10133 10134 if (act->dta_kind == DTRACEACT_DIFEXPR) { 10135 ASSERT(act->dta_difo != NULL); 10136 10137 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 10138 agg->dtag_hasarg = 1; 10139 } 10140 10141 /* 10142 * We need to allocate an id for this aggregation. 10143 */ 10144 #if defined(sun) 10145 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 10146 VM_BESTFIT | VM_SLEEP); 10147 #else 10148 aggid = alloc_unr(state->dts_aggid_arena); 10149 #endif 10150 10151 if (aggid - 1 >= state->dts_naggregations) { 10152 dtrace_aggregation_t **oaggs = state->dts_aggregations; 10153 dtrace_aggregation_t **aggs; 10154 int naggs = state->dts_naggregations << 1; 10155 int onaggs = state->dts_naggregations; 10156 10157 ASSERT(aggid == state->dts_naggregations + 1); 10158 10159 if (naggs == 0) { 10160 ASSERT(oaggs == NULL); 10161 naggs = 1; 10162 } 10163 10164 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 10165 10166 if (oaggs != NULL) { 10167 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 10168 kmem_free(oaggs, onaggs * sizeof (*aggs)); 10169 } 10170 10171 state->dts_aggregations = aggs; 10172 state->dts_naggregations = naggs; 10173 } 10174 10175 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 10176 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 10177 10178 frec = &agg->dtag_first->dta_rec; 10179 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 10180 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 10181 10182 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 10183 ASSERT(!act->dta_intuple); 10184 act->dta_intuple = 1; 10185 } 10186 10187 return (&agg->dtag_action); 10188 } 10189 10190 static void 10191 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 10192 { 10193 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 10194 dtrace_state_t *state = ecb->dte_state; 10195 dtrace_aggid_t aggid = agg->dtag_id; 10196 10197 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 10198 #if defined(sun) 10199 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 10200 #else 10201 free_unr(state->dts_aggid_arena, aggid); 10202 #endif 10203 10204 ASSERT(state->dts_aggregations[aggid - 1] == agg); 10205 state->dts_aggregations[aggid - 1] = NULL; 10206 10207 kmem_free(agg, sizeof (dtrace_aggregation_t)); 10208 } 10209 10210 static int 10211 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 10212 { 10213 dtrace_action_t *action, *last; 10214 dtrace_difo_t *dp = desc->dtad_difo; 10215 uint32_t size = 0, align = sizeof (uint8_t), mask; 10216 uint16_t format = 0; 10217 dtrace_recdesc_t *rec; 10218 dtrace_state_t *state = ecb->dte_state; 10219 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 10220 uint64_t arg = desc->dtad_arg; 10221 10222 ASSERT(MUTEX_HELD(&dtrace_lock)); 10223 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 10224 10225 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 10226 /* 10227 * If this is an aggregating action, there must be neither 10228 * a speculate nor a commit on the action chain. 10229 */ 10230 dtrace_action_t *act; 10231 10232 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 10233 if (act->dta_kind == DTRACEACT_COMMIT) 10234 return (EINVAL); 10235 10236 if (act->dta_kind == DTRACEACT_SPECULATE) 10237 return (EINVAL); 10238 } 10239 10240 action = dtrace_ecb_aggregation_create(ecb, desc); 10241 10242 if (action == NULL) 10243 return (EINVAL); 10244 } else { 10245 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 10246 (desc->dtad_kind == DTRACEACT_DIFEXPR && 10247 dp != NULL && dp->dtdo_destructive)) { 10248 state->dts_destructive = 1; 10249 } 10250 10251 switch (desc->dtad_kind) { 10252 case DTRACEACT_PRINTF: 10253 case DTRACEACT_PRINTA: 10254 case DTRACEACT_SYSTEM: 10255 case DTRACEACT_FREOPEN: 10256 case DTRACEACT_DIFEXPR: 10257 /* 10258 * We know that our arg is a string -- turn it into a 10259 * format. 10260 */ 10261 if (arg == 0) { 10262 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 10263 desc->dtad_kind == DTRACEACT_DIFEXPR); 10264 format = 0; 10265 } else { 10266 ASSERT(arg != 0); 10267 #if defined(sun) 10268 ASSERT(arg > KERNELBASE); 10269 #endif 10270 format = dtrace_format_add(state, 10271 (char *)(uintptr_t)arg); 10272 } 10273 10274 /*FALLTHROUGH*/ 10275 case DTRACEACT_LIBACT: 10276 case DTRACEACT_TRACEMEM: 10277 case DTRACEACT_TRACEMEM_DYNSIZE: 10278 if (dp == NULL) 10279 return (EINVAL); 10280 10281 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 10282 break; 10283 10284 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 10285 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10286 return (EINVAL); 10287 10288 size = opt[DTRACEOPT_STRSIZE]; 10289 } 10290 10291 break; 10292 10293 case DTRACEACT_STACK: 10294 if ((nframes = arg) == 0) { 10295 nframes = opt[DTRACEOPT_STACKFRAMES]; 10296 ASSERT(nframes > 0); 10297 arg = nframes; 10298 } 10299 10300 size = nframes * sizeof (pc_t); 10301 break; 10302 10303 case DTRACEACT_JSTACK: 10304 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 10305 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 10306 10307 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 10308 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 10309 10310 arg = DTRACE_USTACK_ARG(nframes, strsize); 10311 10312 /*FALLTHROUGH*/ 10313 case DTRACEACT_USTACK: 10314 if (desc->dtad_kind != DTRACEACT_JSTACK && 10315 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 10316 strsize = DTRACE_USTACK_STRSIZE(arg); 10317 nframes = opt[DTRACEOPT_USTACKFRAMES]; 10318 ASSERT(nframes > 0); 10319 arg = DTRACE_USTACK_ARG(nframes, strsize); 10320 } 10321 10322 /* 10323 * Save a slot for the pid. 10324 */ 10325 size = (nframes + 1) * sizeof (uint64_t); 10326 size += DTRACE_USTACK_STRSIZE(arg); 10327 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 10328 10329 break; 10330 10331 case DTRACEACT_SYM: 10332 case DTRACEACT_MOD: 10333 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 10334 sizeof (uint64_t)) || 10335 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10336 return (EINVAL); 10337 break; 10338 10339 case DTRACEACT_USYM: 10340 case DTRACEACT_UMOD: 10341 case DTRACEACT_UADDR: 10342 if (dp == NULL || 10343 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 10344 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10345 return (EINVAL); 10346 10347 /* 10348 * We have a slot for the pid, plus a slot for the 10349 * argument. To keep things simple (aligned with 10350 * bitness-neutral sizing), we store each as a 64-bit 10351 * quantity. 10352 */ 10353 size = 2 * sizeof (uint64_t); 10354 break; 10355 10356 case DTRACEACT_STOP: 10357 case DTRACEACT_BREAKPOINT: 10358 case DTRACEACT_PANIC: 10359 break; 10360 10361 case DTRACEACT_CHILL: 10362 case DTRACEACT_DISCARD: 10363 case DTRACEACT_RAISE: 10364 if (dp == NULL) 10365 return (EINVAL); 10366 break; 10367 10368 case DTRACEACT_EXIT: 10369 if (dp == NULL || 10370 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 10371 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10372 return (EINVAL); 10373 break; 10374 10375 case DTRACEACT_SPECULATE: 10376 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 10377 return (EINVAL); 10378 10379 if (dp == NULL) 10380 return (EINVAL); 10381 10382 state->dts_speculates = 1; 10383 break; 10384 10385 case DTRACEACT_PRINTM: 10386 size = dp->dtdo_rtype.dtdt_size; 10387 break; 10388 10389 case DTRACEACT_PRINTT: 10390 size = dp->dtdo_rtype.dtdt_size; 10391 break; 10392 10393 case DTRACEACT_COMMIT: { 10394 dtrace_action_t *act = ecb->dte_action; 10395 10396 for (; act != NULL; act = act->dta_next) { 10397 if (act->dta_kind == DTRACEACT_COMMIT) 10398 return (EINVAL); 10399 } 10400 10401 if (dp == NULL) 10402 return (EINVAL); 10403 break; 10404 } 10405 10406 default: 10407 return (EINVAL); 10408 } 10409 10410 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 10411 /* 10412 * If this is a data-storing action or a speculate, 10413 * we must be sure that there isn't a commit on the 10414 * action chain. 10415 */ 10416 dtrace_action_t *act = ecb->dte_action; 10417 10418 for (; act != NULL; act = act->dta_next) { 10419 if (act->dta_kind == DTRACEACT_COMMIT) 10420 return (EINVAL); 10421 } 10422 } 10423 10424 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 10425 action->dta_rec.dtrd_size = size; 10426 } 10427 10428 action->dta_refcnt = 1; 10429 rec = &action->dta_rec; 10430 size = rec->dtrd_size; 10431 10432 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 10433 if (!(size & mask)) { 10434 align = mask + 1; 10435 break; 10436 } 10437 } 10438 10439 action->dta_kind = desc->dtad_kind; 10440 10441 if ((action->dta_difo = dp) != NULL) 10442 dtrace_difo_hold(dp); 10443 10444 rec->dtrd_action = action->dta_kind; 10445 rec->dtrd_arg = arg; 10446 rec->dtrd_uarg = desc->dtad_uarg; 10447 rec->dtrd_alignment = (uint16_t)align; 10448 rec->dtrd_format = format; 10449 10450 if ((last = ecb->dte_action_last) != NULL) { 10451 ASSERT(ecb->dte_action != NULL); 10452 action->dta_prev = last; 10453 last->dta_next = action; 10454 } else { 10455 ASSERT(ecb->dte_action == NULL); 10456 ecb->dte_action = action; 10457 } 10458 10459 ecb->dte_action_last = action; 10460 10461 return (0); 10462 } 10463 10464 static void 10465 dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 10466 { 10467 dtrace_action_t *act = ecb->dte_action, *next; 10468 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 10469 dtrace_difo_t *dp; 10470 uint16_t format; 10471 10472 if (act != NULL && act->dta_refcnt > 1) { 10473 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 10474 act->dta_refcnt--; 10475 } else { 10476 for (; act != NULL; act = next) { 10477 next = act->dta_next; 10478 ASSERT(next != NULL || act == ecb->dte_action_last); 10479 ASSERT(act->dta_refcnt == 1); 10480 10481 if ((format = act->dta_rec.dtrd_format) != 0) 10482 dtrace_format_remove(ecb->dte_state, format); 10483 10484 if ((dp = act->dta_difo) != NULL) 10485 dtrace_difo_release(dp, vstate); 10486 10487 if (DTRACEACT_ISAGG(act->dta_kind)) { 10488 dtrace_ecb_aggregation_destroy(ecb, act); 10489 } else { 10490 kmem_free(act, sizeof (dtrace_action_t)); 10491 } 10492 } 10493 } 10494 10495 ecb->dte_action = NULL; 10496 ecb->dte_action_last = NULL; 10497 ecb->dte_size = 0; 10498 } 10499 10500 static void 10501 dtrace_ecb_disable(dtrace_ecb_t *ecb) 10502 { 10503 /* 10504 * We disable the ECB by removing it from its probe. 10505 */ 10506 dtrace_ecb_t *pecb, *prev = NULL; 10507 dtrace_probe_t *probe = ecb->dte_probe; 10508 10509 ASSERT(MUTEX_HELD(&dtrace_lock)); 10510 10511 if (probe == NULL) { 10512 /* 10513 * This is the NULL probe; there is nothing to disable. 10514 */ 10515 return; 10516 } 10517 10518 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 10519 if (pecb == ecb) 10520 break; 10521 prev = pecb; 10522 } 10523 10524 ASSERT(pecb != NULL); 10525 10526 if (prev == NULL) { 10527 probe->dtpr_ecb = ecb->dte_next; 10528 } else { 10529 prev->dte_next = ecb->dte_next; 10530 } 10531 10532 if (ecb == probe->dtpr_ecb_last) { 10533 ASSERT(ecb->dte_next == NULL); 10534 probe->dtpr_ecb_last = prev; 10535 } 10536 10537 /* 10538 * The ECB has been disconnected from the probe; now sync to assure 10539 * that all CPUs have seen the change before returning. 10540 */ 10541 dtrace_sync(); 10542 10543 if (probe->dtpr_ecb == NULL) { 10544 /* 10545 * That was the last ECB on the probe; clear the predicate 10546 * cache ID for the probe, disable it and sync one more time 10547 * to assure that we'll never hit it again. 10548 */ 10549 dtrace_provider_t *prov = probe->dtpr_provider; 10550 10551 ASSERT(ecb->dte_next == NULL); 10552 ASSERT(probe->dtpr_ecb_last == NULL); 10553 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 10554 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 10555 probe->dtpr_id, probe->dtpr_arg); 10556 dtrace_sync(); 10557 } else { 10558 /* 10559 * There is at least one ECB remaining on the probe. If there 10560 * is _exactly_ one, set the probe's predicate cache ID to be 10561 * the predicate cache ID of the remaining ECB. 10562 */ 10563 ASSERT(probe->dtpr_ecb_last != NULL); 10564 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 10565 10566 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 10567 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 10568 10569 ASSERT(probe->dtpr_ecb->dte_next == NULL); 10570 10571 if (p != NULL) 10572 probe->dtpr_predcache = p->dtp_cacheid; 10573 } 10574 10575 ecb->dte_next = NULL; 10576 } 10577 } 10578 10579 static void 10580 dtrace_ecb_destroy(dtrace_ecb_t *ecb) 10581 { 10582 dtrace_state_t *state = ecb->dte_state; 10583 dtrace_vstate_t *vstate = &state->dts_vstate; 10584 dtrace_predicate_t *pred; 10585 dtrace_epid_t epid = ecb->dte_epid; 10586 10587 ASSERT(MUTEX_HELD(&dtrace_lock)); 10588 ASSERT(ecb->dte_next == NULL); 10589 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 10590 10591 if ((pred = ecb->dte_predicate) != NULL) 10592 dtrace_predicate_release(pred, vstate); 10593 10594 dtrace_ecb_action_remove(ecb); 10595 10596 ASSERT(state->dts_ecbs[epid - 1] == ecb); 10597 state->dts_ecbs[epid - 1] = NULL; 10598 10599 kmem_free(ecb, sizeof (dtrace_ecb_t)); 10600 } 10601 10602 static dtrace_ecb_t * 10603 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 10604 dtrace_enabling_t *enab) 10605 { 10606 dtrace_ecb_t *ecb; 10607 dtrace_predicate_t *pred; 10608 dtrace_actdesc_t *act; 10609 dtrace_provider_t *prov; 10610 dtrace_ecbdesc_t *desc = enab->dten_current; 10611 10612 ASSERT(MUTEX_HELD(&dtrace_lock)); 10613 ASSERT(state != NULL); 10614 10615 ecb = dtrace_ecb_add(state, probe); 10616 ecb->dte_uarg = desc->dted_uarg; 10617 10618 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 10619 dtrace_predicate_hold(pred); 10620 ecb->dte_predicate = pred; 10621 } 10622 10623 if (probe != NULL) { 10624 /* 10625 * If the provider shows more leg than the consumer is old 10626 * enough to see, we need to enable the appropriate implicit 10627 * predicate bits to prevent the ecb from activating at 10628 * revealing times. 10629 * 10630 * Providers specifying DTRACE_PRIV_USER at register time 10631 * are stating that they need the /proc-style privilege 10632 * model to be enforced, and this is what DTRACE_COND_OWNER 10633 * and DTRACE_COND_ZONEOWNER will then do at probe time. 10634 */ 10635 prov = probe->dtpr_provider; 10636 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 10637 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10638 ecb->dte_cond |= DTRACE_COND_OWNER; 10639 10640 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 10641 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10642 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 10643 10644 /* 10645 * If the provider shows us kernel innards and the user 10646 * is lacking sufficient privilege, enable the 10647 * DTRACE_COND_USERMODE implicit predicate. 10648 */ 10649 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 10650 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 10651 ecb->dte_cond |= DTRACE_COND_USERMODE; 10652 } 10653 10654 if (dtrace_ecb_create_cache != NULL) { 10655 /* 10656 * If we have a cached ecb, we'll use its action list instead 10657 * of creating our own (saving both time and space). 10658 */ 10659 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 10660 dtrace_action_t *act = cached->dte_action; 10661 10662 if (act != NULL) { 10663 ASSERT(act->dta_refcnt > 0); 10664 act->dta_refcnt++; 10665 ecb->dte_action = act; 10666 ecb->dte_action_last = cached->dte_action_last; 10667 ecb->dte_needed = cached->dte_needed; 10668 ecb->dte_size = cached->dte_size; 10669 ecb->dte_alignment = cached->dte_alignment; 10670 } 10671 10672 return (ecb); 10673 } 10674 10675 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 10676 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 10677 dtrace_ecb_destroy(ecb); 10678 return (NULL); 10679 } 10680 } 10681 10682 dtrace_ecb_resize(ecb); 10683 10684 return (dtrace_ecb_create_cache = ecb); 10685 } 10686 10687 static int 10688 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 10689 { 10690 dtrace_ecb_t *ecb; 10691 dtrace_enabling_t *enab = arg; 10692 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 10693 10694 ASSERT(state != NULL); 10695 10696 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 10697 /* 10698 * This probe was created in a generation for which this 10699 * enabling has previously created ECBs; we don't want to 10700 * enable it again, so just kick out. 10701 */ 10702 return (DTRACE_MATCH_NEXT); 10703 } 10704 10705 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 10706 return (DTRACE_MATCH_DONE); 10707 10708 dtrace_ecb_enable(ecb); 10709 return (DTRACE_MATCH_NEXT); 10710 } 10711 10712 static dtrace_ecb_t * 10713 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 10714 { 10715 dtrace_ecb_t *ecb; 10716 10717 ASSERT(MUTEX_HELD(&dtrace_lock)); 10718 10719 if (id == 0 || id > state->dts_necbs) 10720 return (NULL); 10721 10722 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 10723 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 10724 10725 return (state->dts_ecbs[id - 1]); 10726 } 10727 10728 static dtrace_aggregation_t * 10729 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 10730 { 10731 dtrace_aggregation_t *agg; 10732 10733 ASSERT(MUTEX_HELD(&dtrace_lock)); 10734 10735 if (id == 0 || id > state->dts_naggregations) 10736 return (NULL); 10737 10738 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 10739 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 10740 agg->dtag_id == id); 10741 10742 return (state->dts_aggregations[id - 1]); 10743 } 10744 10745 /* 10746 * DTrace Buffer Functions 10747 * 10748 * The following functions manipulate DTrace buffers. Most of these functions 10749 * are called in the context of establishing or processing consumer state; 10750 * exceptions are explicitly noted. 10751 */ 10752 10753 /* 10754 * Note: called from cross call context. This function switches the two 10755 * buffers on a given CPU. The atomicity of this operation is assured by 10756 * disabling interrupts while the actual switch takes place; the disabling of 10757 * interrupts serializes the execution with any execution of dtrace_probe() on 10758 * the same CPU. 10759 */ 10760 static void 10761 dtrace_buffer_switch(dtrace_buffer_t *buf) 10762 { 10763 caddr_t tomax = buf->dtb_tomax; 10764 caddr_t xamot = buf->dtb_xamot; 10765 dtrace_icookie_t cookie; 10766 hrtime_t now; 10767 10768 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10769 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 10770 10771 cookie = dtrace_interrupt_disable(); 10772 now = dtrace_gethrtime(); 10773 buf->dtb_tomax = xamot; 10774 buf->dtb_xamot = tomax; 10775 buf->dtb_xamot_drops = buf->dtb_drops; 10776 buf->dtb_xamot_offset = buf->dtb_offset; 10777 buf->dtb_xamot_errors = buf->dtb_errors; 10778 buf->dtb_xamot_flags = buf->dtb_flags; 10779 buf->dtb_offset = 0; 10780 buf->dtb_drops = 0; 10781 buf->dtb_errors = 0; 10782 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 10783 buf->dtb_interval = now - buf->dtb_switched; 10784 buf->dtb_switched = now; 10785 dtrace_interrupt_enable(cookie); 10786 } 10787 10788 /* 10789 * Note: called from cross call context. This function activates a buffer 10790 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 10791 * is guaranteed by the disabling of interrupts. 10792 */ 10793 static void 10794 dtrace_buffer_activate(dtrace_state_t *state) 10795 { 10796 dtrace_buffer_t *buf; 10797 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 10798 10799 buf = &state->dts_buffer[curcpu]; 10800 10801 if (buf->dtb_tomax != NULL) { 10802 /* 10803 * We might like to assert that the buffer is marked inactive, 10804 * but this isn't necessarily true: the buffer for the CPU 10805 * that processes the BEGIN probe has its buffer activated 10806 * manually. In this case, we take the (harmless) action 10807 * re-clearing the bit INACTIVE bit. 10808 */ 10809 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 10810 } 10811 10812 dtrace_interrupt_enable(cookie); 10813 } 10814 10815 static int 10816 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 10817 processorid_t cpu) 10818 { 10819 #if defined(sun) 10820 cpu_t *cp; 10821 #endif 10822 dtrace_buffer_t *buf; 10823 10824 #if defined(sun) 10825 ASSERT(MUTEX_HELD(&cpu_lock)); 10826 ASSERT(MUTEX_HELD(&dtrace_lock)); 10827 10828 if (size > dtrace_nonroot_maxsize && 10829 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 10830 return (EFBIG); 10831 10832 cp = cpu_list; 10833 10834 do { 10835 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10836 continue; 10837 10838 buf = &bufs[cp->cpu_id]; 10839 10840 /* 10841 * If there is already a buffer allocated for this CPU, it 10842 * is only possible that this is a DR event. In this case, 10843 */ 10844 if (buf->dtb_tomax != NULL) { 10845 ASSERT(buf->dtb_size == size); 10846 continue; 10847 } 10848 10849 ASSERT(buf->dtb_xamot == NULL); 10850 10851 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10852 goto err; 10853 10854 buf->dtb_size = size; 10855 buf->dtb_flags = flags; 10856 buf->dtb_offset = 0; 10857 buf->dtb_drops = 0; 10858 10859 if (flags & DTRACEBUF_NOSWITCH) 10860 continue; 10861 10862 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10863 goto err; 10864 } while ((cp = cp->cpu_next) != cpu_list); 10865 10866 return (0); 10867 10868 err: 10869 cp = cpu_list; 10870 10871 do { 10872 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10873 continue; 10874 10875 buf = &bufs[cp->cpu_id]; 10876 10877 if (buf->dtb_xamot != NULL) { 10878 ASSERT(buf->dtb_tomax != NULL); 10879 ASSERT(buf->dtb_size == size); 10880 kmem_free(buf->dtb_xamot, size); 10881 } 10882 10883 if (buf->dtb_tomax != NULL) { 10884 ASSERT(buf->dtb_size == size); 10885 kmem_free(buf->dtb_tomax, size); 10886 } 10887 10888 buf->dtb_tomax = NULL; 10889 buf->dtb_xamot = NULL; 10890 buf->dtb_size = 0; 10891 } while ((cp = cp->cpu_next) != cpu_list); 10892 10893 return (ENOMEM); 10894 #else 10895 int i; 10896 10897 #if defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 10898 /* 10899 * FreeBSD isn't good at limiting the amount of memory we 10900 * ask to malloc, so let's place a limit here before trying 10901 * to do something that might well end in tears at bedtime. 10902 */ 10903 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 10904 return(ENOMEM); 10905 #endif 10906 10907 ASSERT(MUTEX_HELD(&dtrace_lock)); 10908 CPU_FOREACH(i) { 10909 if (cpu != DTRACE_CPUALL && cpu != i) 10910 continue; 10911 10912 buf = &bufs[i]; 10913 10914 /* 10915 * If there is already a buffer allocated for this CPU, it 10916 * is only possible that this is a DR event. In this case, 10917 * the buffer size must match our specified size. 10918 */ 10919 if (buf->dtb_tomax != NULL) { 10920 ASSERT(buf->dtb_size == size); 10921 continue; 10922 } 10923 10924 ASSERT(buf->dtb_xamot == NULL); 10925 10926 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10927 goto err; 10928 10929 buf->dtb_size = size; 10930 buf->dtb_flags = flags; 10931 buf->dtb_offset = 0; 10932 buf->dtb_drops = 0; 10933 10934 if (flags & DTRACEBUF_NOSWITCH) 10935 continue; 10936 10937 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10938 goto err; 10939 } 10940 10941 return (0); 10942 10943 err: 10944 /* 10945 * Error allocating memory, so free the buffers that were 10946 * allocated before the failed allocation. 10947 */ 10948 CPU_FOREACH(i) { 10949 if (cpu != DTRACE_CPUALL && cpu != i) 10950 continue; 10951 10952 buf = &bufs[i]; 10953 10954 if (buf->dtb_xamot != NULL) { 10955 ASSERT(buf->dtb_tomax != NULL); 10956 ASSERT(buf->dtb_size == size); 10957 kmem_free(buf->dtb_xamot, size); 10958 } 10959 10960 if (buf->dtb_tomax != NULL) { 10961 ASSERT(buf->dtb_size == size); 10962 kmem_free(buf->dtb_tomax, size); 10963 } 10964 10965 buf->dtb_tomax = NULL; 10966 buf->dtb_xamot = NULL; 10967 buf->dtb_size = 0; 10968 10969 } 10970 10971 return (ENOMEM); 10972 #endif 10973 } 10974 10975 /* 10976 * Note: called from probe context. This function just increments the drop 10977 * count on a buffer. It has been made a function to allow for the 10978 * possibility of understanding the source of mysterious drop counts. (A 10979 * problem for which one may be particularly disappointed that DTrace cannot 10980 * be used to understand DTrace.) 10981 */ 10982 static void 10983 dtrace_buffer_drop(dtrace_buffer_t *buf) 10984 { 10985 buf->dtb_drops++; 10986 } 10987 10988 /* 10989 * Note: called from probe context. This function is called to reserve space 10990 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 10991 * mstate. Returns the new offset in the buffer, or a negative value if an 10992 * error has occurred. 10993 */ 10994 static intptr_t 10995 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 10996 dtrace_state_t *state, dtrace_mstate_t *mstate) 10997 { 10998 intptr_t offs = buf->dtb_offset, soffs; 10999 intptr_t woffs; 11000 caddr_t tomax; 11001 size_t total; 11002 11003 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 11004 return (-1); 11005 11006 if ((tomax = buf->dtb_tomax) == NULL) { 11007 dtrace_buffer_drop(buf); 11008 return (-1); 11009 } 11010 11011 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 11012 while (offs & (align - 1)) { 11013 /* 11014 * Assert that our alignment is off by a number which 11015 * is itself sizeof (uint32_t) aligned. 11016 */ 11017 ASSERT(!((align - (offs & (align - 1))) & 11018 (sizeof (uint32_t) - 1))); 11019 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 11020 offs += sizeof (uint32_t); 11021 } 11022 11023 if ((soffs = offs + needed) > buf->dtb_size) { 11024 dtrace_buffer_drop(buf); 11025 return (-1); 11026 } 11027 11028 if (mstate == NULL) 11029 return (offs); 11030 11031 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 11032 mstate->dtms_scratch_size = buf->dtb_size - soffs; 11033 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 11034 11035 return (offs); 11036 } 11037 11038 if (buf->dtb_flags & DTRACEBUF_FILL) { 11039 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 11040 (buf->dtb_flags & DTRACEBUF_FULL)) 11041 return (-1); 11042 goto out; 11043 } 11044 11045 total = needed + (offs & (align - 1)); 11046 11047 /* 11048 * For a ring buffer, life is quite a bit more complicated. Before 11049 * we can store any padding, we need to adjust our wrapping offset. 11050 * (If we've never before wrapped or we're not about to, no adjustment 11051 * is required.) 11052 */ 11053 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 11054 offs + total > buf->dtb_size) { 11055 woffs = buf->dtb_xamot_offset; 11056 11057 if (offs + total > buf->dtb_size) { 11058 /* 11059 * We can't fit in the end of the buffer. First, a 11060 * sanity check that we can fit in the buffer at all. 11061 */ 11062 if (total > buf->dtb_size) { 11063 dtrace_buffer_drop(buf); 11064 return (-1); 11065 } 11066 11067 /* 11068 * We're going to be storing at the top of the buffer, 11069 * so now we need to deal with the wrapped offset. We 11070 * only reset our wrapped offset to 0 if it is 11071 * currently greater than the current offset. If it 11072 * is less than the current offset, it is because a 11073 * previous allocation induced a wrap -- but the 11074 * allocation didn't subsequently take the space due 11075 * to an error or false predicate evaluation. In this 11076 * case, we'll just leave the wrapped offset alone: if 11077 * the wrapped offset hasn't been advanced far enough 11078 * for this allocation, it will be adjusted in the 11079 * lower loop. 11080 */ 11081 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 11082 if (woffs >= offs) 11083 woffs = 0; 11084 } else { 11085 woffs = 0; 11086 } 11087 11088 /* 11089 * Now we know that we're going to be storing to the 11090 * top of the buffer and that there is room for us 11091 * there. We need to clear the buffer from the current 11092 * offset to the end (there may be old gunk there). 11093 */ 11094 while (offs < buf->dtb_size) 11095 tomax[offs++] = 0; 11096 11097 /* 11098 * We need to set our offset to zero. And because we 11099 * are wrapping, we need to set the bit indicating as 11100 * much. We can also adjust our needed space back 11101 * down to the space required by the ECB -- we know 11102 * that the top of the buffer is aligned. 11103 */ 11104 offs = 0; 11105 total = needed; 11106 buf->dtb_flags |= DTRACEBUF_WRAPPED; 11107 } else { 11108 /* 11109 * There is room for us in the buffer, so we simply 11110 * need to check the wrapped offset. 11111 */ 11112 if (woffs < offs) { 11113 /* 11114 * The wrapped offset is less than the offset. 11115 * This can happen if we allocated buffer space 11116 * that induced a wrap, but then we didn't 11117 * subsequently take the space due to an error 11118 * or false predicate evaluation. This is 11119 * okay; we know that _this_ allocation isn't 11120 * going to induce a wrap. We still can't 11121 * reset the wrapped offset to be zero, 11122 * however: the space may have been trashed in 11123 * the previous failed probe attempt. But at 11124 * least the wrapped offset doesn't need to 11125 * be adjusted at all... 11126 */ 11127 goto out; 11128 } 11129 } 11130 11131 while (offs + total > woffs) { 11132 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 11133 size_t size; 11134 11135 if (epid == DTRACE_EPIDNONE) { 11136 size = sizeof (uint32_t); 11137 } else { 11138 ASSERT3U(epid, <=, state->dts_necbs); 11139 ASSERT(state->dts_ecbs[epid - 1] != NULL); 11140 11141 size = state->dts_ecbs[epid - 1]->dte_size; 11142 } 11143 11144 ASSERT(woffs + size <= buf->dtb_size); 11145 ASSERT(size != 0); 11146 11147 if (woffs + size == buf->dtb_size) { 11148 /* 11149 * We've reached the end of the buffer; we want 11150 * to set the wrapped offset to 0 and break 11151 * out. However, if the offs is 0, then we're 11152 * in a strange edge-condition: the amount of 11153 * space that we want to reserve plus the size 11154 * of the record that we're overwriting is 11155 * greater than the size of the buffer. This 11156 * is problematic because if we reserve the 11157 * space but subsequently don't consume it (due 11158 * to a failed predicate or error) the wrapped 11159 * offset will be 0 -- yet the EPID at offset 0 11160 * will not be committed. This situation is 11161 * relatively easy to deal with: if we're in 11162 * this case, the buffer is indistinguishable 11163 * from one that hasn't wrapped; we need only 11164 * finish the job by clearing the wrapped bit, 11165 * explicitly setting the offset to be 0, and 11166 * zero'ing out the old data in the buffer. 11167 */ 11168 if (offs == 0) { 11169 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 11170 buf->dtb_offset = 0; 11171 woffs = total; 11172 11173 while (woffs < buf->dtb_size) 11174 tomax[woffs++] = 0; 11175 } 11176 11177 woffs = 0; 11178 break; 11179 } 11180 11181 woffs += size; 11182 } 11183 11184 /* 11185 * We have a wrapped offset. It may be that the wrapped offset 11186 * has become zero -- that's okay. 11187 */ 11188 buf->dtb_xamot_offset = woffs; 11189 } 11190 11191 out: 11192 /* 11193 * Now we can plow the buffer with any necessary padding. 11194 */ 11195 while (offs & (align - 1)) { 11196 /* 11197 * Assert that our alignment is off by a number which 11198 * is itself sizeof (uint32_t) aligned. 11199 */ 11200 ASSERT(!((align - (offs & (align - 1))) & 11201 (sizeof (uint32_t) - 1))); 11202 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 11203 offs += sizeof (uint32_t); 11204 } 11205 11206 if (buf->dtb_flags & DTRACEBUF_FILL) { 11207 if (offs + needed > buf->dtb_size - state->dts_reserve) { 11208 buf->dtb_flags |= DTRACEBUF_FULL; 11209 return (-1); 11210 } 11211 } 11212 11213 if (mstate == NULL) 11214 return (offs); 11215 11216 /* 11217 * For ring buffers and fill buffers, the scratch space is always 11218 * the inactive buffer. 11219 */ 11220 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 11221 mstate->dtms_scratch_size = buf->dtb_size; 11222 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 11223 11224 return (offs); 11225 } 11226 11227 static void 11228 dtrace_buffer_polish(dtrace_buffer_t *buf) 11229 { 11230 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 11231 ASSERT(MUTEX_HELD(&dtrace_lock)); 11232 11233 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 11234 return; 11235 11236 /* 11237 * We need to polish the ring buffer. There are three cases: 11238 * 11239 * - The first (and presumably most common) is that there is no gap 11240 * between the buffer offset and the wrapped offset. In this case, 11241 * there is nothing in the buffer that isn't valid data; we can 11242 * mark the buffer as polished and return. 11243 * 11244 * - The second (less common than the first but still more common 11245 * than the third) is that there is a gap between the buffer offset 11246 * and the wrapped offset, and the wrapped offset is larger than the 11247 * buffer offset. This can happen because of an alignment issue, or 11248 * can happen because of a call to dtrace_buffer_reserve() that 11249 * didn't subsequently consume the buffer space. In this case, 11250 * we need to zero the data from the buffer offset to the wrapped 11251 * offset. 11252 * 11253 * - The third (and least common) is that there is a gap between the 11254 * buffer offset and the wrapped offset, but the wrapped offset is 11255 * _less_ than the buffer offset. This can only happen because a 11256 * call to dtrace_buffer_reserve() induced a wrap, but the space 11257 * was not subsequently consumed. In this case, we need to zero the 11258 * space from the offset to the end of the buffer _and_ from the 11259 * top of the buffer to the wrapped offset. 11260 */ 11261 if (buf->dtb_offset < buf->dtb_xamot_offset) { 11262 bzero(buf->dtb_tomax + buf->dtb_offset, 11263 buf->dtb_xamot_offset - buf->dtb_offset); 11264 } 11265 11266 if (buf->dtb_offset > buf->dtb_xamot_offset) { 11267 bzero(buf->dtb_tomax + buf->dtb_offset, 11268 buf->dtb_size - buf->dtb_offset); 11269 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 11270 } 11271 } 11272 11273 /* 11274 * This routine determines if data generated at the specified time has likely 11275 * been entirely consumed at user-level. This routine is called to determine 11276 * if an ECB on a defunct probe (but for an active enabling) can be safely 11277 * disabled and destroyed. 11278 */ 11279 static int 11280 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 11281 { 11282 int i; 11283 11284 for (i = 0; i < NCPU; i++) { 11285 dtrace_buffer_t *buf = &bufs[i]; 11286 11287 if (buf->dtb_size == 0) 11288 continue; 11289 11290 if (buf->dtb_flags & DTRACEBUF_RING) 11291 return (0); 11292 11293 if (!buf->dtb_switched && buf->dtb_offset != 0) 11294 return (0); 11295 11296 if (buf->dtb_switched - buf->dtb_interval < when) 11297 return (0); 11298 } 11299 11300 return (1); 11301 } 11302 11303 static void 11304 dtrace_buffer_free(dtrace_buffer_t *bufs) 11305 { 11306 int i; 11307 11308 for (i = 0; i < NCPU; i++) { 11309 dtrace_buffer_t *buf = &bufs[i]; 11310 11311 if (buf->dtb_tomax == NULL) { 11312 ASSERT(buf->dtb_xamot == NULL); 11313 ASSERT(buf->dtb_size == 0); 11314 continue; 11315 } 11316 11317 if (buf->dtb_xamot != NULL) { 11318 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11319 kmem_free(buf->dtb_xamot, buf->dtb_size); 11320 } 11321 11322 kmem_free(buf->dtb_tomax, buf->dtb_size); 11323 buf->dtb_size = 0; 11324 buf->dtb_tomax = NULL; 11325 buf->dtb_xamot = NULL; 11326 } 11327 } 11328 11329 /* 11330 * DTrace Enabling Functions 11331 */ 11332 static dtrace_enabling_t * 11333 dtrace_enabling_create(dtrace_vstate_t *vstate) 11334 { 11335 dtrace_enabling_t *enab; 11336 11337 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 11338 enab->dten_vstate = vstate; 11339 11340 return (enab); 11341 } 11342 11343 static void 11344 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 11345 { 11346 dtrace_ecbdesc_t **ndesc; 11347 size_t osize, nsize; 11348 11349 /* 11350 * We can't add to enablings after we've enabled them, or after we've 11351 * retained them. 11352 */ 11353 ASSERT(enab->dten_probegen == 0); 11354 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11355 11356 if (enab->dten_ndesc < enab->dten_maxdesc) { 11357 enab->dten_desc[enab->dten_ndesc++] = ecb; 11358 return; 11359 } 11360 11361 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11362 11363 if (enab->dten_maxdesc == 0) { 11364 enab->dten_maxdesc = 1; 11365 } else { 11366 enab->dten_maxdesc <<= 1; 11367 } 11368 11369 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 11370 11371 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11372 ndesc = kmem_zalloc(nsize, KM_SLEEP); 11373 bcopy(enab->dten_desc, ndesc, osize); 11374 if (enab->dten_desc != NULL) 11375 kmem_free(enab->dten_desc, osize); 11376 11377 enab->dten_desc = ndesc; 11378 enab->dten_desc[enab->dten_ndesc++] = ecb; 11379 } 11380 11381 static void 11382 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 11383 dtrace_probedesc_t *pd) 11384 { 11385 dtrace_ecbdesc_t *new; 11386 dtrace_predicate_t *pred; 11387 dtrace_actdesc_t *act; 11388 11389 /* 11390 * We're going to create a new ECB description that matches the 11391 * specified ECB in every way, but has the specified probe description. 11392 */ 11393 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 11394 11395 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 11396 dtrace_predicate_hold(pred); 11397 11398 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 11399 dtrace_actdesc_hold(act); 11400 11401 new->dted_action = ecb->dted_action; 11402 new->dted_pred = ecb->dted_pred; 11403 new->dted_probe = *pd; 11404 new->dted_uarg = ecb->dted_uarg; 11405 11406 dtrace_enabling_add(enab, new); 11407 } 11408 11409 static void 11410 dtrace_enabling_dump(dtrace_enabling_t *enab) 11411 { 11412 int i; 11413 11414 for (i = 0; i < enab->dten_ndesc; i++) { 11415 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 11416 11417 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 11418 desc->dtpd_provider, desc->dtpd_mod, 11419 desc->dtpd_func, desc->dtpd_name); 11420 } 11421 } 11422 11423 static void 11424 dtrace_enabling_destroy(dtrace_enabling_t *enab) 11425 { 11426 int i; 11427 dtrace_ecbdesc_t *ep; 11428 dtrace_vstate_t *vstate = enab->dten_vstate; 11429 11430 ASSERT(MUTEX_HELD(&dtrace_lock)); 11431 11432 for (i = 0; i < enab->dten_ndesc; i++) { 11433 dtrace_actdesc_t *act, *next; 11434 dtrace_predicate_t *pred; 11435 11436 ep = enab->dten_desc[i]; 11437 11438 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 11439 dtrace_predicate_release(pred, vstate); 11440 11441 for (act = ep->dted_action; act != NULL; act = next) { 11442 next = act->dtad_next; 11443 dtrace_actdesc_release(act, vstate); 11444 } 11445 11446 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 11447 } 11448 11449 if (enab->dten_desc != NULL) 11450 kmem_free(enab->dten_desc, 11451 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 11452 11453 /* 11454 * If this was a retained enabling, decrement the dts_nretained count 11455 * and take it off of the dtrace_retained list. 11456 */ 11457 if (enab->dten_prev != NULL || enab->dten_next != NULL || 11458 dtrace_retained == enab) { 11459 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11460 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 11461 enab->dten_vstate->dtvs_state->dts_nretained--; 11462 } 11463 11464 if (enab->dten_prev == NULL) { 11465 if (dtrace_retained == enab) { 11466 dtrace_retained = enab->dten_next; 11467 11468 if (dtrace_retained != NULL) 11469 dtrace_retained->dten_prev = NULL; 11470 } 11471 } else { 11472 ASSERT(enab != dtrace_retained); 11473 ASSERT(dtrace_retained != NULL); 11474 enab->dten_prev->dten_next = enab->dten_next; 11475 } 11476 11477 if (enab->dten_next != NULL) { 11478 ASSERT(dtrace_retained != NULL); 11479 enab->dten_next->dten_prev = enab->dten_prev; 11480 } 11481 11482 kmem_free(enab, sizeof (dtrace_enabling_t)); 11483 } 11484 11485 static int 11486 dtrace_enabling_retain(dtrace_enabling_t *enab) 11487 { 11488 dtrace_state_t *state; 11489 11490 ASSERT(MUTEX_HELD(&dtrace_lock)); 11491 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11492 ASSERT(enab->dten_vstate != NULL); 11493 11494 state = enab->dten_vstate->dtvs_state; 11495 ASSERT(state != NULL); 11496 11497 /* 11498 * We only allow each state to retain dtrace_retain_max enablings. 11499 */ 11500 if (state->dts_nretained >= dtrace_retain_max) 11501 return (ENOSPC); 11502 11503 state->dts_nretained++; 11504 11505 if (dtrace_retained == NULL) { 11506 dtrace_retained = enab; 11507 return (0); 11508 } 11509 11510 enab->dten_next = dtrace_retained; 11511 dtrace_retained->dten_prev = enab; 11512 dtrace_retained = enab; 11513 11514 return (0); 11515 } 11516 11517 static int 11518 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 11519 dtrace_probedesc_t *create) 11520 { 11521 dtrace_enabling_t *new, *enab; 11522 int found = 0, err = ENOENT; 11523 11524 ASSERT(MUTEX_HELD(&dtrace_lock)); 11525 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 11526 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 11527 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 11528 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 11529 11530 new = dtrace_enabling_create(&state->dts_vstate); 11531 11532 /* 11533 * Iterate over all retained enablings, looking for enablings that 11534 * match the specified state. 11535 */ 11536 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11537 int i; 11538 11539 /* 11540 * dtvs_state can only be NULL for helper enablings -- and 11541 * helper enablings can't be retained. 11542 */ 11543 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11544 11545 if (enab->dten_vstate->dtvs_state != state) 11546 continue; 11547 11548 /* 11549 * Now iterate over each probe description; we're looking for 11550 * an exact match to the specified probe description. 11551 */ 11552 for (i = 0; i < enab->dten_ndesc; i++) { 11553 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11554 dtrace_probedesc_t *pd = &ep->dted_probe; 11555 11556 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 11557 continue; 11558 11559 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 11560 continue; 11561 11562 if (strcmp(pd->dtpd_func, match->dtpd_func)) 11563 continue; 11564 11565 if (strcmp(pd->dtpd_name, match->dtpd_name)) 11566 continue; 11567 11568 /* 11569 * We have a winning probe! Add it to our growing 11570 * enabling. 11571 */ 11572 found = 1; 11573 dtrace_enabling_addlike(new, ep, create); 11574 } 11575 } 11576 11577 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 11578 dtrace_enabling_destroy(new); 11579 return (err); 11580 } 11581 11582 return (0); 11583 } 11584 11585 static void 11586 dtrace_enabling_retract(dtrace_state_t *state) 11587 { 11588 dtrace_enabling_t *enab, *next; 11589 11590 ASSERT(MUTEX_HELD(&dtrace_lock)); 11591 11592 /* 11593 * Iterate over all retained enablings, destroy the enablings retained 11594 * for the specified state. 11595 */ 11596 for (enab = dtrace_retained; enab != NULL; enab = next) { 11597 next = enab->dten_next; 11598 11599 /* 11600 * dtvs_state can only be NULL for helper enablings -- and 11601 * helper enablings can't be retained. 11602 */ 11603 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11604 11605 if (enab->dten_vstate->dtvs_state == state) { 11606 ASSERT(state->dts_nretained > 0); 11607 dtrace_enabling_destroy(enab); 11608 } 11609 } 11610 11611 ASSERT(state->dts_nretained == 0); 11612 } 11613 11614 static int 11615 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 11616 { 11617 int i = 0; 11618 int matched = 0; 11619 11620 ASSERT(MUTEX_HELD(&cpu_lock)); 11621 ASSERT(MUTEX_HELD(&dtrace_lock)); 11622 11623 for (i = 0; i < enab->dten_ndesc; i++) { 11624 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11625 11626 enab->dten_current = ep; 11627 enab->dten_error = 0; 11628 11629 matched += dtrace_probe_enable(&ep->dted_probe, enab); 11630 11631 if (enab->dten_error != 0) { 11632 /* 11633 * If we get an error half-way through enabling the 11634 * probes, we kick out -- perhaps with some number of 11635 * them enabled. Leaving enabled probes enabled may 11636 * be slightly confusing for user-level, but we expect 11637 * that no one will attempt to actually drive on in 11638 * the face of such errors. If this is an anonymous 11639 * enabling (indicated with a NULL nmatched pointer), 11640 * we cmn_err() a message. We aren't expecting to 11641 * get such an error -- such as it can exist at all, 11642 * it would be a result of corrupted DOF in the driver 11643 * properties. 11644 */ 11645 if (nmatched == NULL) { 11646 cmn_err(CE_WARN, "dtrace_enabling_match() " 11647 "error on %p: %d", (void *)ep, 11648 enab->dten_error); 11649 } 11650 11651 return (enab->dten_error); 11652 } 11653 } 11654 11655 enab->dten_probegen = dtrace_probegen; 11656 if (nmatched != NULL) 11657 *nmatched = matched; 11658 11659 return (0); 11660 } 11661 11662 static void 11663 dtrace_enabling_matchall(void) 11664 { 11665 dtrace_enabling_t *enab; 11666 11667 mutex_enter(&cpu_lock); 11668 mutex_enter(&dtrace_lock); 11669 11670 /* 11671 * Iterate over all retained enablings to see if any probes match 11672 * against them. We only perform this operation on enablings for which 11673 * we have sufficient permissions by virtue of being in the global zone 11674 * or in the same zone as the DTrace client. Because we can be called 11675 * after dtrace_detach() has been called, we cannot assert that there 11676 * are retained enablings. We can safely load from dtrace_retained, 11677 * however: the taskq_destroy() at the end of dtrace_detach() will 11678 * block pending our completion. 11679 */ 11680 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11681 #if defined(sun) 11682 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 11683 11684 if (INGLOBALZONE(curproc) || getzoneid() == crgetzoneid(cr)) 11685 #endif 11686 (void) dtrace_enabling_match(enab, NULL); 11687 } 11688 11689 mutex_exit(&dtrace_lock); 11690 mutex_exit(&cpu_lock); 11691 } 11692 11693 /* 11694 * If an enabling is to be enabled without having matched probes (that is, if 11695 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 11696 * enabling must be _primed_ by creating an ECB for every ECB description. 11697 * This must be done to assure that we know the number of speculations, the 11698 * number of aggregations, the minimum buffer size needed, etc. before we 11699 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 11700 * enabling any probes, we create ECBs for every ECB decription, but with a 11701 * NULL probe -- which is exactly what this function does. 11702 */ 11703 static void 11704 dtrace_enabling_prime(dtrace_state_t *state) 11705 { 11706 dtrace_enabling_t *enab; 11707 int i; 11708 11709 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11710 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11711 11712 if (enab->dten_vstate->dtvs_state != state) 11713 continue; 11714 11715 /* 11716 * We don't want to prime an enabling more than once, lest 11717 * we allow a malicious user to induce resource exhaustion. 11718 * (The ECBs that result from priming an enabling aren't 11719 * leaked -- but they also aren't deallocated until the 11720 * consumer state is destroyed.) 11721 */ 11722 if (enab->dten_primed) 11723 continue; 11724 11725 for (i = 0; i < enab->dten_ndesc; i++) { 11726 enab->dten_current = enab->dten_desc[i]; 11727 (void) dtrace_probe_enable(NULL, enab); 11728 } 11729 11730 enab->dten_primed = 1; 11731 } 11732 } 11733 11734 /* 11735 * Called to indicate that probes should be provided due to retained 11736 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 11737 * must take an initial lap through the enabling calling the dtps_provide() 11738 * entry point explicitly to allow for autocreated probes. 11739 */ 11740 static void 11741 dtrace_enabling_provide(dtrace_provider_t *prv) 11742 { 11743 int i, all = 0; 11744 dtrace_probedesc_t desc; 11745 11746 ASSERT(MUTEX_HELD(&dtrace_lock)); 11747 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 11748 11749 if (prv == NULL) { 11750 all = 1; 11751 prv = dtrace_provider; 11752 } 11753 11754 do { 11755 dtrace_enabling_t *enab = dtrace_retained; 11756 void *parg = prv->dtpv_arg; 11757 11758 for (; enab != NULL; enab = enab->dten_next) { 11759 for (i = 0; i < enab->dten_ndesc; i++) { 11760 desc = enab->dten_desc[i]->dted_probe; 11761 mutex_exit(&dtrace_lock); 11762 prv->dtpv_pops.dtps_provide(parg, &desc); 11763 mutex_enter(&dtrace_lock); 11764 } 11765 } 11766 } while (all && (prv = prv->dtpv_next) != NULL); 11767 11768 mutex_exit(&dtrace_lock); 11769 dtrace_probe_provide(NULL, all ? NULL : prv); 11770 mutex_enter(&dtrace_lock); 11771 } 11772 11773 /* 11774 * Called to reap ECBs that are attached to probes from defunct providers. 11775 */ 11776 static void 11777 dtrace_enabling_reap(void) 11778 { 11779 dtrace_provider_t *prov; 11780 dtrace_probe_t *probe; 11781 dtrace_ecb_t *ecb; 11782 hrtime_t when; 11783 int i; 11784 11785 mutex_enter(&cpu_lock); 11786 mutex_enter(&dtrace_lock); 11787 11788 for (i = 0; i < dtrace_nprobes; i++) { 11789 if ((probe = dtrace_probes[i]) == NULL) 11790 continue; 11791 11792 if (probe->dtpr_ecb == NULL) 11793 continue; 11794 11795 prov = probe->dtpr_provider; 11796 11797 if ((when = prov->dtpv_defunct) == 0) 11798 continue; 11799 11800 /* 11801 * We have ECBs on a defunct provider: we want to reap these 11802 * ECBs to allow the provider to unregister. The destruction 11803 * of these ECBs must be done carefully: if we destroy the ECB 11804 * and the consumer later wishes to consume an EPID that 11805 * corresponds to the destroyed ECB (and if the EPID metadata 11806 * has not been previously consumed), the consumer will abort 11807 * processing on the unknown EPID. To reduce (but not, sadly, 11808 * eliminate) the possibility of this, we will only destroy an 11809 * ECB for a defunct provider if, for the state that 11810 * corresponds to the ECB: 11811 * 11812 * (a) There is no speculative tracing (which can effectively 11813 * cache an EPID for an arbitrary amount of time). 11814 * 11815 * (b) The principal buffers have been switched twice since the 11816 * provider became defunct. 11817 * 11818 * (c) The aggregation buffers are of zero size or have been 11819 * switched twice since the provider became defunct. 11820 * 11821 * We use dts_speculates to determine (a) and call a function 11822 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 11823 * that as soon as we've been unable to destroy one of the ECBs 11824 * associated with the probe, we quit trying -- reaping is only 11825 * fruitful in as much as we can destroy all ECBs associated 11826 * with the defunct provider's probes. 11827 */ 11828 while ((ecb = probe->dtpr_ecb) != NULL) { 11829 dtrace_state_t *state = ecb->dte_state; 11830 dtrace_buffer_t *buf = state->dts_buffer; 11831 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 11832 11833 if (state->dts_speculates) 11834 break; 11835 11836 if (!dtrace_buffer_consumed(buf, when)) 11837 break; 11838 11839 if (!dtrace_buffer_consumed(aggbuf, when)) 11840 break; 11841 11842 dtrace_ecb_disable(ecb); 11843 ASSERT(probe->dtpr_ecb != ecb); 11844 dtrace_ecb_destroy(ecb); 11845 } 11846 } 11847 11848 mutex_exit(&dtrace_lock); 11849 mutex_exit(&cpu_lock); 11850 } 11851 11852 /* 11853 * DTrace DOF Functions 11854 */ 11855 /*ARGSUSED*/ 11856 static void 11857 dtrace_dof_error(dof_hdr_t *dof, const char *str) 11858 { 11859 if (dtrace_err_verbose) 11860 cmn_err(CE_WARN, "failed to process DOF: %s", str); 11861 11862 #ifdef DTRACE_ERRDEBUG 11863 dtrace_errdebug(str); 11864 #endif 11865 } 11866 11867 /* 11868 * Create DOF out of a currently enabled state. Right now, we only create 11869 * DOF containing the run-time options -- but this could be expanded to create 11870 * complete DOF representing the enabled state. 11871 */ 11872 static dof_hdr_t * 11873 dtrace_dof_create(dtrace_state_t *state) 11874 { 11875 dof_hdr_t *dof; 11876 dof_sec_t *sec; 11877 dof_optdesc_t *opt; 11878 int i, len = sizeof (dof_hdr_t) + 11879 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 11880 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11881 11882 ASSERT(MUTEX_HELD(&dtrace_lock)); 11883 11884 dof = kmem_zalloc(len, KM_SLEEP); 11885 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 11886 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 11887 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 11888 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 11889 11890 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 11891 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 11892 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 11893 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 11894 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 11895 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 11896 11897 dof->dofh_flags = 0; 11898 dof->dofh_hdrsize = sizeof (dof_hdr_t); 11899 dof->dofh_secsize = sizeof (dof_sec_t); 11900 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 11901 dof->dofh_secoff = sizeof (dof_hdr_t); 11902 dof->dofh_loadsz = len; 11903 dof->dofh_filesz = len; 11904 dof->dofh_pad = 0; 11905 11906 /* 11907 * Fill in the option section header... 11908 */ 11909 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 11910 sec->dofs_type = DOF_SECT_OPTDESC; 11911 sec->dofs_align = sizeof (uint64_t); 11912 sec->dofs_flags = DOF_SECF_LOAD; 11913 sec->dofs_entsize = sizeof (dof_optdesc_t); 11914 11915 opt = (dof_optdesc_t *)((uintptr_t)sec + 11916 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 11917 11918 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 11919 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11920 11921 for (i = 0; i < DTRACEOPT_MAX; i++) { 11922 opt[i].dofo_option = i; 11923 opt[i].dofo_strtab = DOF_SECIDX_NONE; 11924 opt[i].dofo_value = state->dts_options[i]; 11925 } 11926 11927 return (dof); 11928 } 11929 11930 static dof_hdr_t * 11931 dtrace_dof_copyin(uintptr_t uarg, int *errp) 11932 { 11933 dof_hdr_t hdr, *dof; 11934 11935 ASSERT(!MUTEX_HELD(&dtrace_lock)); 11936 11937 /* 11938 * First, we're going to copyin() the sizeof (dof_hdr_t). 11939 */ 11940 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 11941 dtrace_dof_error(NULL, "failed to copyin DOF header"); 11942 *errp = EFAULT; 11943 return (NULL); 11944 } 11945 11946 /* 11947 * Now we'll allocate the entire DOF and copy it in -- provided 11948 * that the length isn't outrageous. 11949 */ 11950 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 11951 dtrace_dof_error(&hdr, "load size exceeds maximum"); 11952 *errp = E2BIG; 11953 return (NULL); 11954 } 11955 11956 if (hdr.dofh_loadsz < sizeof (hdr)) { 11957 dtrace_dof_error(&hdr, "invalid load size"); 11958 *errp = EINVAL; 11959 return (NULL); 11960 } 11961 11962 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 11963 11964 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) { 11965 kmem_free(dof, hdr.dofh_loadsz); 11966 *errp = EFAULT; 11967 return (NULL); 11968 } 11969 11970 return (dof); 11971 } 11972 11973 #if !defined(sun) 11974 static __inline uchar_t 11975 dtrace_dof_char(char c) { 11976 switch (c) { 11977 case '0': 11978 case '1': 11979 case '2': 11980 case '3': 11981 case '4': 11982 case '5': 11983 case '6': 11984 case '7': 11985 case '8': 11986 case '9': 11987 return (c - '0'); 11988 case 'A': 11989 case 'B': 11990 case 'C': 11991 case 'D': 11992 case 'E': 11993 case 'F': 11994 return (c - 'A' + 10); 11995 case 'a': 11996 case 'b': 11997 case 'c': 11998 case 'd': 11999 case 'e': 12000 case 'f': 12001 return (c - 'a' + 10); 12002 } 12003 /* Should not reach here. */ 12004 return (0); 12005 } 12006 #endif 12007 12008 static dof_hdr_t * 12009 dtrace_dof_property(const char *name) 12010 { 12011 uchar_t *buf; 12012 uint64_t loadsz; 12013 unsigned int len, i; 12014 dof_hdr_t *dof; 12015 12016 #if defined(sun) 12017 /* 12018 * Unfortunately, array of values in .conf files are always (and 12019 * only) interpreted to be integer arrays. We must read our DOF 12020 * as an integer array, and then squeeze it into a byte array. 12021 */ 12022 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 12023 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 12024 return (NULL); 12025 12026 for (i = 0; i < len; i++) 12027 buf[i] = (uchar_t)(((int *)buf)[i]); 12028 12029 if (len < sizeof (dof_hdr_t)) { 12030 ddi_prop_free(buf); 12031 dtrace_dof_error(NULL, "truncated header"); 12032 return (NULL); 12033 } 12034 12035 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 12036 ddi_prop_free(buf); 12037 dtrace_dof_error(NULL, "truncated DOF"); 12038 return (NULL); 12039 } 12040 12041 if (loadsz >= dtrace_dof_maxsize) { 12042 ddi_prop_free(buf); 12043 dtrace_dof_error(NULL, "oversized DOF"); 12044 return (NULL); 12045 } 12046 12047 dof = kmem_alloc(loadsz, KM_SLEEP); 12048 bcopy(buf, dof, loadsz); 12049 ddi_prop_free(buf); 12050 #else 12051 char *p; 12052 char *p_env; 12053 12054 if ((p_env = getenv(name)) == NULL) 12055 return (NULL); 12056 12057 len = strlen(p_env) / 2; 12058 12059 buf = kmem_alloc(len, KM_SLEEP); 12060 12061 dof = (dof_hdr_t *) buf; 12062 12063 p = p_env; 12064 12065 for (i = 0; i < len; i++) { 12066 buf[i] = (dtrace_dof_char(p[0]) << 4) | 12067 dtrace_dof_char(p[1]); 12068 p += 2; 12069 } 12070 12071 freeenv(p_env); 12072 12073 if (len < sizeof (dof_hdr_t)) { 12074 kmem_free(buf, 0); 12075 dtrace_dof_error(NULL, "truncated header"); 12076 return (NULL); 12077 } 12078 12079 if (len < (loadsz = dof->dofh_loadsz)) { 12080 kmem_free(buf, 0); 12081 dtrace_dof_error(NULL, "truncated DOF"); 12082 return (NULL); 12083 } 12084 12085 if (loadsz >= dtrace_dof_maxsize) { 12086 kmem_free(buf, 0); 12087 dtrace_dof_error(NULL, "oversized DOF"); 12088 return (NULL); 12089 } 12090 #endif 12091 12092 return (dof); 12093 } 12094 12095 static void 12096 dtrace_dof_destroy(dof_hdr_t *dof) 12097 { 12098 kmem_free(dof, dof->dofh_loadsz); 12099 } 12100 12101 /* 12102 * Return the dof_sec_t pointer corresponding to a given section index. If the 12103 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 12104 * a type other than DOF_SECT_NONE is specified, the header is checked against 12105 * this type and NULL is returned if the types do not match. 12106 */ 12107 static dof_sec_t * 12108 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 12109 { 12110 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 12111 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 12112 12113 if (i >= dof->dofh_secnum) { 12114 dtrace_dof_error(dof, "referenced section index is invalid"); 12115 return (NULL); 12116 } 12117 12118 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 12119 dtrace_dof_error(dof, "referenced section is not loadable"); 12120 return (NULL); 12121 } 12122 12123 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 12124 dtrace_dof_error(dof, "referenced section is the wrong type"); 12125 return (NULL); 12126 } 12127 12128 return (sec); 12129 } 12130 12131 static dtrace_probedesc_t * 12132 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 12133 { 12134 dof_probedesc_t *probe; 12135 dof_sec_t *strtab; 12136 uintptr_t daddr = (uintptr_t)dof; 12137 uintptr_t str; 12138 size_t size; 12139 12140 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 12141 dtrace_dof_error(dof, "invalid probe section"); 12142 return (NULL); 12143 } 12144 12145 if (sec->dofs_align != sizeof (dof_secidx_t)) { 12146 dtrace_dof_error(dof, "bad alignment in probe description"); 12147 return (NULL); 12148 } 12149 12150 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 12151 dtrace_dof_error(dof, "truncated probe description"); 12152 return (NULL); 12153 } 12154 12155 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 12156 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 12157 12158 if (strtab == NULL) 12159 return (NULL); 12160 12161 str = daddr + strtab->dofs_offset; 12162 size = strtab->dofs_size; 12163 12164 if (probe->dofp_provider >= strtab->dofs_size) { 12165 dtrace_dof_error(dof, "corrupt probe provider"); 12166 return (NULL); 12167 } 12168 12169 (void) strncpy(desc->dtpd_provider, 12170 (char *)(str + probe->dofp_provider), 12171 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 12172 12173 if (probe->dofp_mod >= strtab->dofs_size) { 12174 dtrace_dof_error(dof, "corrupt probe module"); 12175 return (NULL); 12176 } 12177 12178 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 12179 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 12180 12181 if (probe->dofp_func >= strtab->dofs_size) { 12182 dtrace_dof_error(dof, "corrupt probe function"); 12183 return (NULL); 12184 } 12185 12186 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 12187 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 12188 12189 if (probe->dofp_name >= strtab->dofs_size) { 12190 dtrace_dof_error(dof, "corrupt probe name"); 12191 return (NULL); 12192 } 12193 12194 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 12195 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 12196 12197 return (desc); 12198 } 12199 12200 static dtrace_difo_t * 12201 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12202 cred_t *cr) 12203 { 12204 dtrace_difo_t *dp; 12205 size_t ttl = 0; 12206 dof_difohdr_t *dofd; 12207 uintptr_t daddr = (uintptr_t)dof; 12208 size_t max = dtrace_difo_maxsize; 12209 int i, l, n; 12210 12211 static const struct { 12212 int section; 12213 int bufoffs; 12214 int lenoffs; 12215 int entsize; 12216 int align; 12217 const char *msg; 12218 } difo[] = { 12219 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 12220 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 12221 sizeof (dif_instr_t), "multiple DIF sections" }, 12222 12223 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 12224 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 12225 sizeof (uint64_t), "multiple integer tables" }, 12226 12227 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 12228 offsetof(dtrace_difo_t, dtdo_strlen), 0, 12229 sizeof (char), "multiple string tables" }, 12230 12231 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 12232 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 12233 sizeof (uint_t), "multiple variable tables" }, 12234 12235 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 12236 }; 12237 12238 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 12239 dtrace_dof_error(dof, "invalid DIFO header section"); 12240 return (NULL); 12241 } 12242 12243 if (sec->dofs_align != sizeof (dof_secidx_t)) { 12244 dtrace_dof_error(dof, "bad alignment in DIFO header"); 12245 return (NULL); 12246 } 12247 12248 if (sec->dofs_size < sizeof (dof_difohdr_t) || 12249 sec->dofs_size % sizeof (dof_secidx_t)) { 12250 dtrace_dof_error(dof, "bad size in DIFO header"); 12251 return (NULL); 12252 } 12253 12254 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12255 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 12256 12257 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 12258 dp->dtdo_rtype = dofd->dofd_rtype; 12259 12260 for (l = 0; l < n; l++) { 12261 dof_sec_t *subsec; 12262 void **bufp; 12263 uint32_t *lenp; 12264 12265 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 12266 dofd->dofd_links[l])) == NULL) 12267 goto err; /* invalid section link */ 12268 12269 if (ttl + subsec->dofs_size > max) { 12270 dtrace_dof_error(dof, "exceeds maximum size"); 12271 goto err; 12272 } 12273 12274 ttl += subsec->dofs_size; 12275 12276 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 12277 if (subsec->dofs_type != difo[i].section) 12278 continue; 12279 12280 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 12281 dtrace_dof_error(dof, "section not loaded"); 12282 goto err; 12283 } 12284 12285 if (subsec->dofs_align != difo[i].align) { 12286 dtrace_dof_error(dof, "bad alignment"); 12287 goto err; 12288 } 12289 12290 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 12291 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 12292 12293 if (*bufp != NULL) { 12294 dtrace_dof_error(dof, difo[i].msg); 12295 goto err; 12296 } 12297 12298 if (difo[i].entsize != subsec->dofs_entsize) { 12299 dtrace_dof_error(dof, "entry size mismatch"); 12300 goto err; 12301 } 12302 12303 if (subsec->dofs_entsize != 0 && 12304 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 12305 dtrace_dof_error(dof, "corrupt entry size"); 12306 goto err; 12307 } 12308 12309 *lenp = subsec->dofs_size; 12310 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 12311 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 12312 *bufp, subsec->dofs_size); 12313 12314 if (subsec->dofs_entsize != 0) 12315 *lenp /= subsec->dofs_entsize; 12316 12317 break; 12318 } 12319 12320 /* 12321 * If we encounter a loadable DIFO sub-section that is not 12322 * known to us, assume this is a broken program and fail. 12323 */ 12324 if (difo[i].section == DOF_SECT_NONE && 12325 (subsec->dofs_flags & DOF_SECF_LOAD)) { 12326 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 12327 goto err; 12328 } 12329 } 12330 12331 if (dp->dtdo_buf == NULL) { 12332 /* 12333 * We can't have a DIF object without DIF text. 12334 */ 12335 dtrace_dof_error(dof, "missing DIF text"); 12336 goto err; 12337 } 12338 12339 /* 12340 * Before we validate the DIF object, run through the variable table 12341 * looking for the strings -- if any of their size are under, we'll set 12342 * their size to be the system-wide default string size. Note that 12343 * this should _not_ happen if the "strsize" option has been set -- 12344 * in this case, the compiler should have set the size to reflect the 12345 * setting of the option. 12346 */ 12347 for (i = 0; i < dp->dtdo_varlen; i++) { 12348 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 12349 dtrace_diftype_t *t = &v->dtdv_type; 12350 12351 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 12352 continue; 12353 12354 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 12355 t->dtdt_size = dtrace_strsize_default; 12356 } 12357 12358 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 12359 goto err; 12360 12361 dtrace_difo_init(dp, vstate); 12362 return (dp); 12363 12364 err: 12365 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 12366 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 12367 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 12368 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 12369 12370 kmem_free(dp, sizeof (dtrace_difo_t)); 12371 return (NULL); 12372 } 12373 12374 static dtrace_predicate_t * 12375 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12376 cred_t *cr) 12377 { 12378 dtrace_difo_t *dp; 12379 12380 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 12381 return (NULL); 12382 12383 return (dtrace_predicate_create(dp)); 12384 } 12385 12386 static dtrace_actdesc_t * 12387 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12388 cred_t *cr) 12389 { 12390 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 12391 dof_actdesc_t *desc; 12392 dof_sec_t *difosec; 12393 size_t offs; 12394 uintptr_t daddr = (uintptr_t)dof; 12395 uint64_t arg; 12396 dtrace_actkind_t kind; 12397 12398 if (sec->dofs_type != DOF_SECT_ACTDESC) { 12399 dtrace_dof_error(dof, "invalid action section"); 12400 return (NULL); 12401 } 12402 12403 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 12404 dtrace_dof_error(dof, "truncated action description"); 12405 return (NULL); 12406 } 12407 12408 if (sec->dofs_align != sizeof (uint64_t)) { 12409 dtrace_dof_error(dof, "bad alignment in action description"); 12410 return (NULL); 12411 } 12412 12413 if (sec->dofs_size < sec->dofs_entsize) { 12414 dtrace_dof_error(dof, "section entry size exceeds total size"); 12415 return (NULL); 12416 } 12417 12418 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 12419 dtrace_dof_error(dof, "bad entry size in action description"); 12420 return (NULL); 12421 } 12422 12423 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 12424 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 12425 return (NULL); 12426 } 12427 12428 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 12429 desc = (dof_actdesc_t *)(daddr + 12430 (uintptr_t)sec->dofs_offset + offs); 12431 kind = (dtrace_actkind_t)desc->dofa_kind; 12432 12433 if ((DTRACEACT_ISPRINTFLIKE(kind) && 12434 (kind != DTRACEACT_PRINTA || 12435 desc->dofa_strtab != DOF_SECIDX_NONE)) || 12436 (kind == DTRACEACT_DIFEXPR && 12437 desc->dofa_strtab != DOF_SECIDX_NONE)) { 12438 dof_sec_t *strtab; 12439 char *str, *fmt; 12440 uint64_t i; 12441 12442 /* 12443 * The argument to these actions is an index into the 12444 * DOF string table. For printf()-like actions, this 12445 * is the format string. For print(), this is the 12446 * CTF type of the expression result. 12447 */ 12448 if ((strtab = dtrace_dof_sect(dof, 12449 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 12450 goto err; 12451 12452 str = (char *)((uintptr_t)dof + 12453 (uintptr_t)strtab->dofs_offset); 12454 12455 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 12456 if (str[i] == '\0') 12457 break; 12458 } 12459 12460 if (i >= strtab->dofs_size) { 12461 dtrace_dof_error(dof, "bogus format string"); 12462 goto err; 12463 } 12464 12465 if (i == desc->dofa_arg) { 12466 dtrace_dof_error(dof, "empty format string"); 12467 goto err; 12468 } 12469 12470 i -= desc->dofa_arg; 12471 fmt = kmem_alloc(i + 1, KM_SLEEP); 12472 bcopy(&str[desc->dofa_arg], fmt, i + 1); 12473 arg = (uint64_t)(uintptr_t)fmt; 12474 } else { 12475 if (kind == DTRACEACT_PRINTA) { 12476 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 12477 arg = 0; 12478 } else { 12479 arg = desc->dofa_arg; 12480 } 12481 } 12482 12483 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 12484 desc->dofa_uarg, arg); 12485 12486 if (last != NULL) { 12487 last->dtad_next = act; 12488 } else { 12489 first = act; 12490 } 12491 12492 last = act; 12493 12494 if (desc->dofa_difo == DOF_SECIDX_NONE) 12495 continue; 12496 12497 if ((difosec = dtrace_dof_sect(dof, 12498 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 12499 goto err; 12500 12501 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 12502 12503 if (act->dtad_difo == NULL) 12504 goto err; 12505 } 12506 12507 ASSERT(first != NULL); 12508 return (first); 12509 12510 err: 12511 for (act = first; act != NULL; act = next) { 12512 next = act->dtad_next; 12513 dtrace_actdesc_release(act, vstate); 12514 } 12515 12516 return (NULL); 12517 } 12518 12519 static dtrace_ecbdesc_t * 12520 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12521 cred_t *cr) 12522 { 12523 dtrace_ecbdesc_t *ep; 12524 dof_ecbdesc_t *ecb; 12525 dtrace_probedesc_t *desc; 12526 dtrace_predicate_t *pred = NULL; 12527 12528 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 12529 dtrace_dof_error(dof, "truncated ECB description"); 12530 return (NULL); 12531 } 12532 12533 if (sec->dofs_align != sizeof (uint64_t)) { 12534 dtrace_dof_error(dof, "bad alignment in ECB description"); 12535 return (NULL); 12536 } 12537 12538 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 12539 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 12540 12541 if (sec == NULL) 12542 return (NULL); 12543 12544 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12545 ep->dted_uarg = ecb->dofe_uarg; 12546 desc = &ep->dted_probe; 12547 12548 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 12549 goto err; 12550 12551 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 12552 if ((sec = dtrace_dof_sect(dof, 12553 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 12554 goto err; 12555 12556 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 12557 goto err; 12558 12559 ep->dted_pred.dtpdd_predicate = pred; 12560 } 12561 12562 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 12563 if ((sec = dtrace_dof_sect(dof, 12564 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 12565 goto err; 12566 12567 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 12568 12569 if (ep->dted_action == NULL) 12570 goto err; 12571 } 12572 12573 return (ep); 12574 12575 err: 12576 if (pred != NULL) 12577 dtrace_predicate_release(pred, vstate); 12578 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12579 return (NULL); 12580 } 12581 12582 /* 12583 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 12584 * specified DOF. At present, this amounts to simply adding 'ubase' to the 12585 * site of any user SETX relocations to account for load object base address. 12586 * In the future, if we need other relocations, this function can be extended. 12587 */ 12588 static int 12589 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 12590 { 12591 uintptr_t daddr = (uintptr_t)dof; 12592 dof_relohdr_t *dofr = 12593 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12594 dof_sec_t *ss, *rs, *ts; 12595 dof_relodesc_t *r; 12596 uint_t i, n; 12597 12598 if (sec->dofs_size < sizeof (dof_relohdr_t) || 12599 sec->dofs_align != sizeof (dof_secidx_t)) { 12600 dtrace_dof_error(dof, "invalid relocation header"); 12601 return (-1); 12602 } 12603 12604 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 12605 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 12606 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 12607 12608 if (ss == NULL || rs == NULL || ts == NULL) 12609 return (-1); /* dtrace_dof_error() has been called already */ 12610 12611 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 12612 rs->dofs_align != sizeof (uint64_t)) { 12613 dtrace_dof_error(dof, "invalid relocation section"); 12614 return (-1); 12615 } 12616 12617 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 12618 n = rs->dofs_size / rs->dofs_entsize; 12619 12620 for (i = 0; i < n; i++) { 12621 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 12622 12623 switch (r->dofr_type) { 12624 case DOF_RELO_NONE: 12625 break; 12626 case DOF_RELO_SETX: 12627 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 12628 sizeof (uint64_t) > ts->dofs_size) { 12629 dtrace_dof_error(dof, "bad relocation offset"); 12630 return (-1); 12631 } 12632 12633 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 12634 dtrace_dof_error(dof, "misaligned setx relo"); 12635 return (-1); 12636 } 12637 12638 *(uint64_t *)taddr += ubase; 12639 break; 12640 default: 12641 dtrace_dof_error(dof, "invalid relocation type"); 12642 return (-1); 12643 } 12644 12645 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 12646 } 12647 12648 return (0); 12649 } 12650 12651 /* 12652 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 12653 * header: it should be at the front of a memory region that is at least 12654 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 12655 * size. It need not be validated in any other way. 12656 */ 12657 static int 12658 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 12659 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 12660 { 12661 uint64_t len = dof->dofh_loadsz, seclen; 12662 uintptr_t daddr = (uintptr_t)dof; 12663 dtrace_ecbdesc_t *ep; 12664 dtrace_enabling_t *enab; 12665 uint_t i; 12666 12667 ASSERT(MUTEX_HELD(&dtrace_lock)); 12668 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 12669 12670 /* 12671 * Check the DOF header identification bytes. In addition to checking 12672 * valid settings, we also verify that unused bits/bytes are zeroed so 12673 * we can use them later without fear of regressing existing binaries. 12674 */ 12675 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 12676 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 12677 dtrace_dof_error(dof, "DOF magic string mismatch"); 12678 return (-1); 12679 } 12680 12681 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 12682 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 12683 dtrace_dof_error(dof, "DOF has invalid data model"); 12684 return (-1); 12685 } 12686 12687 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 12688 dtrace_dof_error(dof, "DOF encoding mismatch"); 12689 return (-1); 12690 } 12691 12692 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 12693 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 12694 dtrace_dof_error(dof, "DOF version mismatch"); 12695 return (-1); 12696 } 12697 12698 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 12699 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 12700 return (-1); 12701 } 12702 12703 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 12704 dtrace_dof_error(dof, "DOF uses too many integer registers"); 12705 return (-1); 12706 } 12707 12708 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 12709 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 12710 return (-1); 12711 } 12712 12713 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 12714 if (dof->dofh_ident[i] != 0) { 12715 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 12716 return (-1); 12717 } 12718 } 12719 12720 if (dof->dofh_flags & ~DOF_FL_VALID) { 12721 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 12722 return (-1); 12723 } 12724 12725 if (dof->dofh_secsize == 0) { 12726 dtrace_dof_error(dof, "zero section header size"); 12727 return (-1); 12728 } 12729 12730 /* 12731 * Check that the section headers don't exceed the amount of DOF 12732 * data. Note that we cast the section size and number of sections 12733 * to uint64_t's to prevent possible overflow in the multiplication. 12734 */ 12735 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 12736 12737 if (dof->dofh_secoff > len || seclen > len || 12738 dof->dofh_secoff + seclen > len) { 12739 dtrace_dof_error(dof, "truncated section headers"); 12740 return (-1); 12741 } 12742 12743 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 12744 dtrace_dof_error(dof, "misaligned section headers"); 12745 return (-1); 12746 } 12747 12748 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 12749 dtrace_dof_error(dof, "misaligned section size"); 12750 return (-1); 12751 } 12752 12753 /* 12754 * Take an initial pass through the section headers to be sure that 12755 * the headers don't have stray offsets. If the 'noprobes' flag is 12756 * set, do not permit sections relating to providers, probes, or args. 12757 */ 12758 for (i = 0; i < dof->dofh_secnum; i++) { 12759 dof_sec_t *sec = (dof_sec_t *)(daddr + 12760 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12761 12762 if (noprobes) { 12763 switch (sec->dofs_type) { 12764 case DOF_SECT_PROVIDER: 12765 case DOF_SECT_PROBES: 12766 case DOF_SECT_PRARGS: 12767 case DOF_SECT_PROFFS: 12768 dtrace_dof_error(dof, "illegal sections " 12769 "for enabling"); 12770 return (-1); 12771 } 12772 } 12773 12774 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12775 continue; /* just ignore non-loadable sections */ 12776 12777 if (sec->dofs_align & (sec->dofs_align - 1)) { 12778 dtrace_dof_error(dof, "bad section alignment"); 12779 return (-1); 12780 } 12781 12782 if (sec->dofs_offset & (sec->dofs_align - 1)) { 12783 dtrace_dof_error(dof, "misaligned section"); 12784 return (-1); 12785 } 12786 12787 if (sec->dofs_offset > len || sec->dofs_size > len || 12788 sec->dofs_offset + sec->dofs_size > len) { 12789 dtrace_dof_error(dof, "corrupt section header"); 12790 return (-1); 12791 } 12792 12793 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 12794 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 12795 dtrace_dof_error(dof, "non-terminating string table"); 12796 return (-1); 12797 } 12798 } 12799 12800 /* 12801 * Take a second pass through the sections and locate and perform any 12802 * relocations that are present. We do this after the first pass to 12803 * be sure that all sections have had their headers validated. 12804 */ 12805 for (i = 0; i < dof->dofh_secnum; i++) { 12806 dof_sec_t *sec = (dof_sec_t *)(daddr + 12807 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12808 12809 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12810 continue; /* skip sections that are not loadable */ 12811 12812 switch (sec->dofs_type) { 12813 case DOF_SECT_URELHDR: 12814 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 12815 return (-1); 12816 break; 12817 } 12818 } 12819 12820 if ((enab = *enabp) == NULL) 12821 enab = *enabp = dtrace_enabling_create(vstate); 12822 12823 for (i = 0; i < dof->dofh_secnum; i++) { 12824 dof_sec_t *sec = (dof_sec_t *)(daddr + 12825 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12826 12827 if (sec->dofs_type != DOF_SECT_ECBDESC) 12828 continue; 12829 12830 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 12831 dtrace_enabling_destroy(enab); 12832 *enabp = NULL; 12833 return (-1); 12834 } 12835 12836 dtrace_enabling_add(enab, ep); 12837 } 12838 12839 return (0); 12840 } 12841 12842 /* 12843 * Process DOF for any options. This routine assumes that the DOF has been 12844 * at least processed by dtrace_dof_slurp(). 12845 */ 12846 static int 12847 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 12848 { 12849 int i, rval; 12850 uint32_t entsize; 12851 size_t offs; 12852 dof_optdesc_t *desc; 12853 12854 for (i = 0; i < dof->dofh_secnum; i++) { 12855 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 12856 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12857 12858 if (sec->dofs_type != DOF_SECT_OPTDESC) 12859 continue; 12860 12861 if (sec->dofs_align != sizeof (uint64_t)) { 12862 dtrace_dof_error(dof, "bad alignment in " 12863 "option description"); 12864 return (EINVAL); 12865 } 12866 12867 if ((entsize = sec->dofs_entsize) == 0) { 12868 dtrace_dof_error(dof, "zeroed option entry size"); 12869 return (EINVAL); 12870 } 12871 12872 if (entsize < sizeof (dof_optdesc_t)) { 12873 dtrace_dof_error(dof, "bad option entry size"); 12874 return (EINVAL); 12875 } 12876 12877 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 12878 desc = (dof_optdesc_t *)((uintptr_t)dof + 12879 (uintptr_t)sec->dofs_offset + offs); 12880 12881 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 12882 dtrace_dof_error(dof, "non-zero option string"); 12883 return (EINVAL); 12884 } 12885 12886 if (desc->dofo_value == DTRACEOPT_UNSET) { 12887 dtrace_dof_error(dof, "unset option"); 12888 return (EINVAL); 12889 } 12890 12891 if ((rval = dtrace_state_option(state, 12892 desc->dofo_option, desc->dofo_value)) != 0) { 12893 dtrace_dof_error(dof, "rejected option"); 12894 return (rval); 12895 } 12896 } 12897 } 12898 12899 return (0); 12900 } 12901 12902 /* 12903 * DTrace Consumer State Functions 12904 */ 12905 static int 12906 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 12907 { 12908 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 12909 void *base; 12910 uintptr_t limit; 12911 dtrace_dynvar_t *dvar, *next, *start; 12912 int i; 12913 12914 ASSERT(MUTEX_HELD(&dtrace_lock)); 12915 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 12916 12917 bzero(dstate, sizeof (dtrace_dstate_t)); 12918 12919 if ((dstate->dtds_chunksize = chunksize) == 0) 12920 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 12921 12922 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 12923 size = min; 12924 12925 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 12926 return (ENOMEM); 12927 12928 dstate->dtds_size = size; 12929 dstate->dtds_base = base; 12930 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 12931 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 12932 12933 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 12934 12935 if (hashsize != 1 && (hashsize & 1)) 12936 hashsize--; 12937 12938 dstate->dtds_hashsize = hashsize; 12939 dstate->dtds_hash = dstate->dtds_base; 12940 12941 /* 12942 * Set all of our hash buckets to point to the single sink, and (if 12943 * it hasn't already been set), set the sink's hash value to be the 12944 * sink sentinel value. The sink is needed for dynamic variable 12945 * lookups to know that they have iterated over an entire, valid hash 12946 * chain. 12947 */ 12948 for (i = 0; i < hashsize; i++) 12949 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 12950 12951 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 12952 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 12953 12954 /* 12955 * Determine number of active CPUs. Divide free list evenly among 12956 * active CPUs. 12957 */ 12958 start = (dtrace_dynvar_t *) 12959 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 12960 limit = (uintptr_t)base + size; 12961 12962 maxper = (limit - (uintptr_t)start) / NCPU; 12963 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 12964 12965 #if !defined(sun) 12966 CPU_FOREACH(i) { 12967 #else 12968 for (i = 0; i < NCPU; i++) { 12969 #endif 12970 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 12971 12972 /* 12973 * If we don't even have enough chunks to make it once through 12974 * NCPUs, we're just going to allocate everything to the first 12975 * CPU. And if we're on the last CPU, we're going to allocate 12976 * whatever is left over. In either case, we set the limit to 12977 * be the limit of the dynamic variable space. 12978 */ 12979 if (maxper == 0 || i == NCPU - 1) { 12980 limit = (uintptr_t)base + size; 12981 start = NULL; 12982 } else { 12983 limit = (uintptr_t)start + maxper; 12984 start = (dtrace_dynvar_t *)limit; 12985 } 12986 12987 ASSERT(limit <= (uintptr_t)base + size); 12988 12989 for (;;) { 12990 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 12991 dstate->dtds_chunksize); 12992 12993 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 12994 break; 12995 12996 dvar->dtdv_next = next; 12997 dvar = next; 12998 } 12999 13000 if (maxper == 0) 13001 break; 13002 } 13003 13004 return (0); 13005 } 13006 13007 static void 13008 dtrace_dstate_fini(dtrace_dstate_t *dstate) 13009 { 13010 ASSERT(MUTEX_HELD(&cpu_lock)); 13011 13012 if (dstate->dtds_base == NULL) 13013 return; 13014 13015 kmem_free(dstate->dtds_base, dstate->dtds_size); 13016 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 13017 } 13018 13019 static void 13020 dtrace_vstate_fini(dtrace_vstate_t *vstate) 13021 { 13022 /* 13023 * Logical XOR, where are you? 13024 */ 13025 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 13026 13027 if (vstate->dtvs_nglobals > 0) { 13028 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 13029 sizeof (dtrace_statvar_t *)); 13030 } 13031 13032 if (vstate->dtvs_ntlocals > 0) { 13033 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 13034 sizeof (dtrace_difv_t)); 13035 } 13036 13037 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 13038 13039 if (vstate->dtvs_nlocals > 0) { 13040 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 13041 sizeof (dtrace_statvar_t *)); 13042 } 13043 } 13044 13045 #if defined(sun) 13046 static void 13047 dtrace_state_clean(dtrace_state_t *state) 13048 { 13049 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 13050 return; 13051 13052 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 13053 dtrace_speculation_clean(state); 13054 } 13055 13056 static void 13057 dtrace_state_deadman(dtrace_state_t *state) 13058 { 13059 hrtime_t now; 13060 13061 dtrace_sync(); 13062 13063 now = dtrace_gethrtime(); 13064 13065 if (state != dtrace_anon.dta_state && 13066 now - state->dts_laststatus >= dtrace_deadman_user) 13067 return; 13068 13069 /* 13070 * We must be sure that dts_alive never appears to be less than the 13071 * value upon entry to dtrace_state_deadman(), and because we lack a 13072 * dtrace_cas64(), we cannot store to it atomically. We thus instead 13073 * store INT64_MAX to it, followed by a memory barrier, followed by 13074 * the new value. This assures that dts_alive never appears to be 13075 * less than its true value, regardless of the order in which the 13076 * stores to the underlying storage are issued. 13077 */ 13078 state->dts_alive = INT64_MAX; 13079 dtrace_membar_producer(); 13080 state->dts_alive = now; 13081 } 13082 #else 13083 static void 13084 dtrace_state_clean(void *arg) 13085 { 13086 dtrace_state_t *state = arg; 13087 dtrace_optval_t *opt = state->dts_options; 13088 13089 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 13090 return; 13091 13092 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 13093 dtrace_speculation_clean(state); 13094 13095 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 13096 dtrace_state_clean, state); 13097 } 13098 13099 static void 13100 dtrace_state_deadman(void *arg) 13101 { 13102 dtrace_state_t *state = arg; 13103 hrtime_t now; 13104 13105 dtrace_sync(); 13106 13107 dtrace_debug_output(); 13108 13109 now = dtrace_gethrtime(); 13110 13111 if (state != dtrace_anon.dta_state && 13112 now - state->dts_laststatus >= dtrace_deadman_user) 13113 return; 13114 13115 /* 13116 * We must be sure that dts_alive never appears to be less than the 13117 * value upon entry to dtrace_state_deadman(), and because we lack a 13118 * dtrace_cas64(), we cannot store to it atomically. We thus instead 13119 * store INT64_MAX to it, followed by a memory barrier, followed by 13120 * the new value. This assures that dts_alive never appears to be 13121 * less than its true value, regardless of the order in which the 13122 * stores to the underlying storage are issued. 13123 */ 13124 state->dts_alive = INT64_MAX; 13125 dtrace_membar_producer(); 13126 state->dts_alive = now; 13127 13128 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 13129 dtrace_state_deadman, state); 13130 } 13131 #endif 13132 13133 static dtrace_state_t * 13134 #if defined(sun) 13135 dtrace_state_create(dev_t *devp, cred_t *cr) 13136 #else 13137 dtrace_state_create(struct cdev *dev) 13138 #endif 13139 { 13140 #if defined(sun) 13141 minor_t minor; 13142 major_t major; 13143 #else 13144 cred_t *cr = NULL; 13145 int m = 0; 13146 #endif 13147 char c[30]; 13148 dtrace_state_t *state; 13149 dtrace_optval_t *opt; 13150 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 13151 13152 ASSERT(MUTEX_HELD(&dtrace_lock)); 13153 ASSERT(MUTEX_HELD(&cpu_lock)); 13154 13155 #if defined(sun) 13156 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 13157 VM_BESTFIT | VM_SLEEP); 13158 13159 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 13160 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 13161 return (NULL); 13162 } 13163 13164 state = ddi_get_soft_state(dtrace_softstate, minor); 13165 #else 13166 if (dev != NULL) { 13167 cr = dev->si_cred; 13168 m = dev2unit(dev); 13169 } 13170 13171 /* Allocate memory for the state. */ 13172 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 13173 #endif 13174 13175 state->dts_epid = DTRACE_EPIDNONE + 1; 13176 13177 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 13178 #if defined(sun) 13179 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 13180 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 13181 13182 if (devp != NULL) { 13183 major = getemajor(*devp); 13184 } else { 13185 major = ddi_driver_major(dtrace_devi); 13186 } 13187 13188 state->dts_dev = makedevice(major, minor); 13189 13190 if (devp != NULL) 13191 *devp = state->dts_dev; 13192 #else 13193 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 13194 state->dts_dev = dev; 13195 #endif 13196 13197 /* 13198 * We allocate NCPU buffers. On the one hand, this can be quite 13199 * a bit of memory per instance (nearly 36K on a Starcat). On the 13200 * other hand, it saves an additional memory reference in the probe 13201 * path. 13202 */ 13203 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 13204 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 13205 13206 #if defined(sun) 13207 state->dts_cleaner = CYCLIC_NONE; 13208 state->dts_deadman = CYCLIC_NONE; 13209 #else 13210 callout_init(&state->dts_cleaner, CALLOUT_MPSAFE); 13211 callout_init(&state->dts_deadman, CALLOUT_MPSAFE); 13212 #endif 13213 state->dts_vstate.dtvs_state = state; 13214 13215 for (i = 0; i < DTRACEOPT_MAX; i++) 13216 state->dts_options[i] = DTRACEOPT_UNSET; 13217 13218 /* 13219 * Set the default options. 13220 */ 13221 opt = state->dts_options; 13222 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 13223 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 13224 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 13225 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 13226 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 13227 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 13228 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 13229 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 13230 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 13231 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 13232 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 13233 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 13234 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 13235 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 13236 13237 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 13238 13239 /* 13240 * Depending on the user credentials, we set flag bits which alter probe 13241 * visibility or the amount of destructiveness allowed. In the case of 13242 * actual anonymous tracing, or the possession of all privileges, all of 13243 * the normal checks are bypassed. 13244 */ 13245 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 13246 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 13247 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 13248 } else { 13249 /* 13250 * Set up the credentials for this instantiation. We take a 13251 * hold on the credential to prevent it from disappearing on 13252 * us; this in turn prevents the zone_t referenced by this 13253 * credential from disappearing. This means that we can 13254 * examine the credential and the zone from probe context. 13255 */ 13256 crhold(cr); 13257 state->dts_cred.dcr_cred = cr; 13258 13259 /* 13260 * CRA_PROC means "we have *some* privilege for dtrace" and 13261 * unlocks the use of variables like pid, zonename, etc. 13262 */ 13263 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 13264 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 13265 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 13266 } 13267 13268 /* 13269 * dtrace_user allows use of syscall and profile providers. 13270 * If the user also has proc_owner and/or proc_zone, we 13271 * extend the scope to include additional visibility and 13272 * destructive power. 13273 */ 13274 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 13275 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 13276 state->dts_cred.dcr_visible |= 13277 DTRACE_CRV_ALLPROC; 13278 13279 state->dts_cred.dcr_action |= 13280 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13281 } 13282 13283 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 13284 state->dts_cred.dcr_visible |= 13285 DTRACE_CRV_ALLZONE; 13286 13287 state->dts_cred.dcr_action |= 13288 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13289 } 13290 13291 /* 13292 * If we have all privs in whatever zone this is, 13293 * we can do destructive things to processes which 13294 * have altered credentials. 13295 */ 13296 #if defined(sun) 13297 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 13298 cr->cr_zone->zone_privset)) { 13299 state->dts_cred.dcr_action |= 13300 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 13301 } 13302 #endif 13303 } 13304 13305 /* 13306 * Holding the dtrace_kernel privilege also implies that 13307 * the user has the dtrace_user privilege from a visibility 13308 * perspective. But without further privileges, some 13309 * destructive actions are not available. 13310 */ 13311 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 13312 /* 13313 * Make all probes in all zones visible. However, 13314 * this doesn't mean that all actions become available 13315 * to all zones. 13316 */ 13317 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 13318 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 13319 13320 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 13321 DTRACE_CRA_PROC; 13322 /* 13323 * Holding proc_owner means that destructive actions 13324 * for *this* zone are allowed. 13325 */ 13326 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 13327 state->dts_cred.dcr_action |= 13328 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13329 13330 /* 13331 * Holding proc_zone means that destructive actions 13332 * for this user/group ID in all zones is allowed. 13333 */ 13334 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 13335 state->dts_cred.dcr_action |= 13336 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13337 13338 #if defined(sun) 13339 /* 13340 * If we have all privs in whatever zone this is, 13341 * we can do destructive things to processes which 13342 * have altered credentials. 13343 */ 13344 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 13345 cr->cr_zone->zone_privset)) { 13346 state->dts_cred.dcr_action |= 13347 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 13348 } 13349 #endif 13350 } 13351 13352 /* 13353 * Holding the dtrace_proc privilege gives control over fasttrap 13354 * and pid providers. We need to grant wider destructive 13355 * privileges in the event that the user has proc_owner and/or 13356 * proc_zone. 13357 */ 13358 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 13359 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 13360 state->dts_cred.dcr_action |= 13361 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13362 13363 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 13364 state->dts_cred.dcr_action |= 13365 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13366 } 13367 } 13368 13369 return (state); 13370 } 13371 13372 static int 13373 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 13374 { 13375 dtrace_optval_t *opt = state->dts_options, size; 13376 processorid_t cpu = 0;; 13377 int flags = 0, rval; 13378 13379 ASSERT(MUTEX_HELD(&dtrace_lock)); 13380 ASSERT(MUTEX_HELD(&cpu_lock)); 13381 ASSERT(which < DTRACEOPT_MAX); 13382 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 13383 (state == dtrace_anon.dta_state && 13384 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 13385 13386 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 13387 return (0); 13388 13389 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 13390 cpu = opt[DTRACEOPT_CPU]; 13391 13392 if (which == DTRACEOPT_SPECSIZE) 13393 flags |= DTRACEBUF_NOSWITCH; 13394 13395 if (which == DTRACEOPT_BUFSIZE) { 13396 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 13397 flags |= DTRACEBUF_RING; 13398 13399 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 13400 flags |= DTRACEBUF_FILL; 13401 13402 if (state != dtrace_anon.dta_state || 13403 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 13404 flags |= DTRACEBUF_INACTIVE; 13405 } 13406 13407 for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { 13408 /* 13409 * The size must be 8-byte aligned. If the size is not 8-byte 13410 * aligned, drop it down by the difference. 13411 */ 13412 if (size & (sizeof (uint64_t) - 1)) 13413 size -= size & (sizeof (uint64_t) - 1); 13414 13415 if (size < state->dts_reserve) { 13416 /* 13417 * Buffers always must be large enough to accommodate 13418 * their prereserved space. We return E2BIG instead 13419 * of ENOMEM in this case to allow for user-level 13420 * software to differentiate the cases. 13421 */ 13422 return (E2BIG); 13423 } 13424 13425 rval = dtrace_buffer_alloc(buf, size, flags, cpu); 13426 13427 if (rval != ENOMEM) { 13428 opt[which] = size; 13429 return (rval); 13430 } 13431 13432 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13433 return (rval); 13434 } 13435 13436 return (ENOMEM); 13437 } 13438 13439 static int 13440 dtrace_state_buffers(dtrace_state_t *state) 13441 { 13442 dtrace_speculation_t *spec = state->dts_speculations; 13443 int rval, i; 13444 13445 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 13446 DTRACEOPT_BUFSIZE)) != 0) 13447 return (rval); 13448 13449 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 13450 DTRACEOPT_AGGSIZE)) != 0) 13451 return (rval); 13452 13453 for (i = 0; i < state->dts_nspeculations; i++) { 13454 if ((rval = dtrace_state_buffer(state, 13455 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 13456 return (rval); 13457 } 13458 13459 return (0); 13460 } 13461 13462 static void 13463 dtrace_state_prereserve(dtrace_state_t *state) 13464 { 13465 dtrace_ecb_t *ecb; 13466 dtrace_probe_t *probe; 13467 13468 state->dts_reserve = 0; 13469 13470 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 13471 return; 13472 13473 /* 13474 * If our buffer policy is a "fill" buffer policy, we need to set the 13475 * prereserved space to be the space required by the END probes. 13476 */ 13477 probe = dtrace_probes[dtrace_probeid_end - 1]; 13478 ASSERT(probe != NULL); 13479 13480 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 13481 if (ecb->dte_state != state) 13482 continue; 13483 13484 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 13485 } 13486 } 13487 13488 static int 13489 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 13490 { 13491 dtrace_optval_t *opt = state->dts_options, sz, nspec; 13492 dtrace_speculation_t *spec; 13493 dtrace_buffer_t *buf; 13494 #if defined(sun) 13495 cyc_handler_t hdlr; 13496 cyc_time_t when; 13497 #endif 13498 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13499 dtrace_icookie_t cookie; 13500 13501 mutex_enter(&cpu_lock); 13502 mutex_enter(&dtrace_lock); 13503 13504 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13505 rval = EBUSY; 13506 goto out; 13507 } 13508 13509 /* 13510 * Before we can perform any checks, we must prime all of the 13511 * retained enablings that correspond to this state. 13512 */ 13513 dtrace_enabling_prime(state); 13514 13515 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 13516 rval = EACCES; 13517 goto out; 13518 } 13519 13520 dtrace_state_prereserve(state); 13521 13522 /* 13523 * Now we want to do is try to allocate our speculations. 13524 * We do not automatically resize the number of speculations; if 13525 * this fails, we will fail the operation. 13526 */ 13527 nspec = opt[DTRACEOPT_NSPEC]; 13528 ASSERT(nspec != DTRACEOPT_UNSET); 13529 13530 if (nspec > INT_MAX) { 13531 rval = ENOMEM; 13532 goto out; 13533 } 13534 13535 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); 13536 13537 if (spec == NULL) { 13538 rval = ENOMEM; 13539 goto out; 13540 } 13541 13542 state->dts_speculations = spec; 13543 state->dts_nspeculations = (int)nspec; 13544 13545 for (i = 0; i < nspec; i++) { 13546 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { 13547 rval = ENOMEM; 13548 goto err; 13549 } 13550 13551 spec[i].dtsp_buffer = buf; 13552 } 13553 13554 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 13555 if (dtrace_anon.dta_state == NULL) { 13556 rval = ENOENT; 13557 goto out; 13558 } 13559 13560 if (state->dts_necbs != 0) { 13561 rval = EALREADY; 13562 goto out; 13563 } 13564 13565 state->dts_anon = dtrace_anon_grab(); 13566 ASSERT(state->dts_anon != NULL); 13567 state = state->dts_anon; 13568 13569 /* 13570 * We want "grabanon" to be set in the grabbed state, so we'll 13571 * copy that option value from the grabbing state into the 13572 * grabbed state. 13573 */ 13574 state->dts_options[DTRACEOPT_GRABANON] = 13575 opt[DTRACEOPT_GRABANON]; 13576 13577 *cpu = dtrace_anon.dta_beganon; 13578 13579 /* 13580 * If the anonymous state is active (as it almost certainly 13581 * is if the anonymous enabling ultimately matched anything), 13582 * we don't allow any further option processing -- but we 13583 * don't return failure. 13584 */ 13585 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13586 goto out; 13587 } 13588 13589 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 13590 opt[DTRACEOPT_AGGSIZE] != 0) { 13591 if (state->dts_aggregations == NULL) { 13592 /* 13593 * We're not going to create an aggregation buffer 13594 * because we don't have any ECBs that contain 13595 * aggregations -- set this option to 0. 13596 */ 13597 opt[DTRACEOPT_AGGSIZE] = 0; 13598 } else { 13599 /* 13600 * If we have an aggregation buffer, we must also have 13601 * a buffer to use as scratch. 13602 */ 13603 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 13604 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 13605 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 13606 } 13607 } 13608 } 13609 13610 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 13611 opt[DTRACEOPT_SPECSIZE] != 0) { 13612 if (!state->dts_speculates) { 13613 /* 13614 * We're not going to create speculation buffers 13615 * because we don't have any ECBs that actually 13616 * speculate -- set the speculation size to 0. 13617 */ 13618 opt[DTRACEOPT_SPECSIZE] = 0; 13619 } 13620 } 13621 13622 /* 13623 * The bare minimum size for any buffer that we're actually going to 13624 * do anything to is sizeof (uint64_t). 13625 */ 13626 sz = sizeof (uint64_t); 13627 13628 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 13629 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 13630 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 13631 /* 13632 * A buffer size has been explicitly set to 0 (or to a size 13633 * that will be adjusted to 0) and we need the space -- we 13634 * need to return failure. We return ENOSPC to differentiate 13635 * it from failing to allocate a buffer due to failure to meet 13636 * the reserve (for which we return E2BIG). 13637 */ 13638 rval = ENOSPC; 13639 goto out; 13640 } 13641 13642 if ((rval = dtrace_state_buffers(state)) != 0) 13643 goto err; 13644 13645 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 13646 sz = dtrace_dstate_defsize; 13647 13648 do { 13649 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 13650 13651 if (rval == 0) 13652 break; 13653 13654 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13655 goto err; 13656 } while (sz >>= 1); 13657 13658 opt[DTRACEOPT_DYNVARSIZE] = sz; 13659 13660 if (rval != 0) 13661 goto err; 13662 13663 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 13664 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 13665 13666 if (opt[DTRACEOPT_CLEANRATE] == 0) 13667 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13668 13669 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 13670 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 13671 13672 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 13673 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13674 13675 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 13676 #if defined(sun) 13677 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 13678 hdlr.cyh_arg = state; 13679 hdlr.cyh_level = CY_LOW_LEVEL; 13680 13681 when.cyt_when = 0; 13682 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 13683 13684 state->dts_cleaner = cyclic_add(&hdlr, &when); 13685 13686 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 13687 hdlr.cyh_arg = state; 13688 hdlr.cyh_level = CY_LOW_LEVEL; 13689 13690 when.cyt_when = 0; 13691 when.cyt_interval = dtrace_deadman_interval; 13692 13693 state->dts_deadman = cyclic_add(&hdlr, &when); 13694 #else 13695 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 13696 dtrace_state_clean, state); 13697 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 13698 dtrace_state_deadman, state); 13699 #endif 13700 13701 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 13702 13703 /* 13704 * Now it's time to actually fire the BEGIN probe. We need to disable 13705 * interrupts here both to record the CPU on which we fired the BEGIN 13706 * probe (the data from this CPU will be processed first at user 13707 * level) and to manually activate the buffer for this CPU. 13708 */ 13709 cookie = dtrace_interrupt_disable(); 13710 *cpu = curcpu; 13711 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 13712 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 13713 13714 dtrace_probe(dtrace_probeid_begin, 13715 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13716 dtrace_interrupt_enable(cookie); 13717 /* 13718 * We may have had an exit action from a BEGIN probe; only change our 13719 * state to ACTIVE if we're still in WARMUP. 13720 */ 13721 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 13722 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 13723 13724 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 13725 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 13726 13727 /* 13728 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 13729 * want each CPU to transition its principal buffer out of the 13730 * INACTIVE state. Doing this assures that no CPU will suddenly begin 13731 * processing an ECB halfway down a probe's ECB chain; all CPUs will 13732 * atomically transition from processing none of a state's ECBs to 13733 * processing all of them. 13734 */ 13735 dtrace_xcall(DTRACE_CPUALL, 13736 (dtrace_xcall_t)dtrace_buffer_activate, state); 13737 goto out; 13738 13739 err: 13740 dtrace_buffer_free(state->dts_buffer); 13741 dtrace_buffer_free(state->dts_aggbuffer); 13742 13743 if ((nspec = state->dts_nspeculations) == 0) { 13744 ASSERT(state->dts_speculations == NULL); 13745 goto out; 13746 } 13747 13748 spec = state->dts_speculations; 13749 ASSERT(spec != NULL); 13750 13751 for (i = 0; i < state->dts_nspeculations; i++) { 13752 if ((buf = spec[i].dtsp_buffer) == NULL) 13753 break; 13754 13755 dtrace_buffer_free(buf); 13756 kmem_free(buf, bufsize); 13757 } 13758 13759 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13760 state->dts_nspeculations = 0; 13761 state->dts_speculations = NULL; 13762 13763 out: 13764 mutex_exit(&dtrace_lock); 13765 mutex_exit(&cpu_lock); 13766 13767 return (rval); 13768 } 13769 13770 static int 13771 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 13772 { 13773 dtrace_icookie_t cookie; 13774 13775 ASSERT(MUTEX_HELD(&dtrace_lock)); 13776 13777 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 13778 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 13779 return (EINVAL); 13780 13781 /* 13782 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 13783 * to be sure that every CPU has seen it. See below for the details 13784 * on why this is done. 13785 */ 13786 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 13787 dtrace_sync(); 13788 13789 /* 13790 * By this point, it is impossible for any CPU to be still processing 13791 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 13792 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 13793 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 13794 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 13795 * iff we're in the END probe. 13796 */ 13797 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 13798 dtrace_sync(); 13799 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 13800 13801 /* 13802 * Finally, we can release the reserve and call the END probe. We 13803 * disable interrupts across calling the END probe to allow us to 13804 * return the CPU on which we actually called the END probe. This 13805 * allows user-land to be sure that this CPU's principal buffer is 13806 * processed last. 13807 */ 13808 state->dts_reserve = 0; 13809 13810 cookie = dtrace_interrupt_disable(); 13811 *cpu = curcpu; 13812 dtrace_probe(dtrace_probeid_end, 13813 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13814 dtrace_interrupt_enable(cookie); 13815 13816 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 13817 dtrace_sync(); 13818 13819 return (0); 13820 } 13821 13822 static int 13823 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 13824 dtrace_optval_t val) 13825 { 13826 ASSERT(MUTEX_HELD(&dtrace_lock)); 13827 13828 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13829 return (EBUSY); 13830 13831 if (option >= DTRACEOPT_MAX) 13832 return (EINVAL); 13833 13834 if (option != DTRACEOPT_CPU && val < 0) 13835 return (EINVAL); 13836 13837 switch (option) { 13838 case DTRACEOPT_DESTRUCTIVE: 13839 if (dtrace_destructive_disallow) 13840 return (EACCES); 13841 13842 state->dts_cred.dcr_destructive = 1; 13843 break; 13844 13845 case DTRACEOPT_BUFSIZE: 13846 case DTRACEOPT_DYNVARSIZE: 13847 case DTRACEOPT_AGGSIZE: 13848 case DTRACEOPT_SPECSIZE: 13849 case DTRACEOPT_STRSIZE: 13850 if (val < 0) 13851 return (EINVAL); 13852 13853 if (val >= LONG_MAX) { 13854 /* 13855 * If this is an otherwise negative value, set it to 13856 * the highest multiple of 128m less than LONG_MAX. 13857 * Technically, we're adjusting the size without 13858 * regard to the buffer resizing policy, but in fact, 13859 * this has no effect -- if we set the buffer size to 13860 * ~LONG_MAX and the buffer policy is ultimately set to 13861 * be "manual", the buffer allocation is guaranteed to 13862 * fail, if only because the allocation requires two 13863 * buffers. (We set the the size to the highest 13864 * multiple of 128m because it ensures that the size 13865 * will remain a multiple of a megabyte when 13866 * repeatedly halved -- all the way down to 15m.) 13867 */ 13868 val = LONG_MAX - (1 << 27) + 1; 13869 } 13870 } 13871 13872 state->dts_options[option] = val; 13873 13874 return (0); 13875 } 13876 13877 static void 13878 dtrace_state_destroy(dtrace_state_t *state) 13879 { 13880 dtrace_ecb_t *ecb; 13881 dtrace_vstate_t *vstate = &state->dts_vstate; 13882 #if defined(sun) 13883 minor_t minor = getminor(state->dts_dev); 13884 #endif 13885 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13886 dtrace_speculation_t *spec = state->dts_speculations; 13887 int nspec = state->dts_nspeculations; 13888 uint32_t match; 13889 13890 ASSERT(MUTEX_HELD(&dtrace_lock)); 13891 ASSERT(MUTEX_HELD(&cpu_lock)); 13892 13893 /* 13894 * First, retract any retained enablings for this state. 13895 */ 13896 dtrace_enabling_retract(state); 13897 ASSERT(state->dts_nretained == 0); 13898 13899 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 13900 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 13901 /* 13902 * We have managed to come into dtrace_state_destroy() on a 13903 * hot enabling -- almost certainly because of a disorderly 13904 * shutdown of a consumer. (That is, a consumer that is 13905 * exiting without having called dtrace_stop().) In this case, 13906 * we're going to set our activity to be KILLED, and then 13907 * issue a sync to be sure that everyone is out of probe 13908 * context before we start blowing away ECBs. 13909 */ 13910 state->dts_activity = DTRACE_ACTIVITY_KILLED; 13911 dtrace_sync(); 13912 } 13913 13914 /* 13915 * Release the credential hold we took in dtrace_state_create(). 13916 */ 13917 if (state->dts_cred.dcr_cred != NULL) 13918 crfree(state->dts_cred.dcr_cred); 13919 13920 /* 13921 * Now we can safely disable and destroy any enabled probes. Because 13922 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 13923 * (especially if they're all enabled), we take two passes through the 13924 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 13925 * in the second we disable whatever is left over. 13926 */ 13927 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 13928 for (i = 0; i < state->dts_necbs; i++) { 13929 if ((ecb = state->dts_ecbs[i]) == NULL) 13930 continue; 13931 13932 if (match && ecb->dte_probe != NULL) { 13933 dtrace_probe_t *probe = ecb->dte_probe; 13934 dtrace_provider_t *prov = probe->dtpr_provider; 13935 13936 if (!(prov->dtpv_priv.dtpp_flags & match)) 13937 continue; 13938 } 13939 13940 dtrace_ecb_disable(ecb); 13941 dtrace_ecb_destroy(ecb); 13942 } 13943 13944 if (!match) 13945 break; 13946 } 13947 13948 /* 13949 * Before we free the buffers, perform one more sync to assure that 13950 * every CPU is out of probe context. 13951 */ 13952 dtrace_sync(); 13953 13954 dtrace_buffer_free(state->dts_buffer); 13955 dtrace_buffer_free(state->dts_aggbuffer); 13956 13957 for (i = 0; i < nspec; i++) 13958 dtrace_buffer_free(spec[i].dtsp_buffer); 13959 13960 #if defined(sun) 13961 if (state->dts_cleaner != CYCLIC_NONE) 13962 cyclic_remove(state->dts_cleaner); 13963 13964 if (state->dts_deadman != CYCLIC_NONE) 13965 cyclic_remove(state->dts_deadman); 13966 #else 13967 callout_stop(&state->dts_cleaner); 13968 callout_drain(&state->dts_cleaner); 13969 callout_stop(&state->dts_deadman); 13970 callout_drain(&state->dts_deadman); 13971 #endif 13972 13973 dtrace_dstate_fini(&vstate->dtvs_dynvars); 13974 dtrace_vstate_fini(vstate); 13975 if (state->dts_ecbs != NULL) 13976 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 13977 13978 if (state->dts_aggregations != NULL) { 13979 #ifdef DEBUG 13980 for (i = 0; i < state->dts_naggregations; i++) 13981 ASSERT(state->dts_aggregations[i] == NULL); 13982 #endif 13983 ASSERT(state->dts_naggregations > 0); 13984 kmem_free(state->dts_aggregations, 13985 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 13986 } 13987 13988 kmem_free(state->dts_buffer, bufsize); 13989 kmem_free(state->dts_aggbuffer, bufsize); 13990 13991 for (i = 0; i < nspec; i++) 13992 kmem_free(spec[i].dtsp_buffer, bufsize); 13993 13994 if (spec != NULL) 13995 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13996 13997 dtrace_format_destroy(state); 13998 13999 if (state->dts_aggid_arena != NULL) { 14000 #if defined(sun) 14001 vmem_destroy(state->dts_aggid_arena); 14002 #else 14003 delete_unrhdr(state->dts_aggid_arena); 14004 #endif 14005 state->dts_aggid_arena = NULL; 14006 } 14007 #if defined(sun) 14008 ddi_soft_state_free(dtrace_softstate, minor); 14009 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14010 #endif 14011 } 14012 14013 /* 14014 * DTrace Anonymous Enabling Functions 14015 */ 14016 static dtrace_state_t * 14017 dtrace_anon_grab(void) 14018 { 14019 dtrace_state_t *state; 14020 14021 ASSERT(MUTEX_HELD(&dtrace_lock)); 14022 14023 if ((state = dtrace_anon.dta_state) == NULL) { 14024 ASSERT(dtrace_anon.dta_enabling == NULL); 14025 return (NULL); 14026 } 14027 14028 ASSERT(dtrace_anon.dta_enabling != NULL); 14029 ASSERT(dtrace_retained != NULL); 14030 14031 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 14032 dtrace_anon.dta_enabling = NULL; 14033 dtrace_anon.dta_state = NULL; 14034 14035 return (state); 14036 } 14037 14038 static void 14039 dtrace_anon_property(void) 14040 { 14041 int i, rv; 14042 dtrace_state_t *state; 14043 dof_hdr_t *dof; 14044 char c[32]; /* enough for "dof-data-" + digits */ 14045 14046 ASSERT(MUTEX_HELD(&dtrace_lock)); 14047 ASSERT(MUTEX_HELD(&cpu_lock)); 14048 14049 for (i = 0; ; i++) { 14050 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 14051 14052 dtrace_err_verbose = 1; 14053 14054 if ((dof = dtrace_dof_property(c)) == NULL) { 14055 dtrace_err_verbose = 0; 14056 break; 14057 } 14058 14059 #if defined(sun) 14060 /* 14061 * We want to create anonymous state, so we need to transition 14062 * the kernel debugger to indicate that DTrace is active. If 14063 * this fails (e.g. because the debugger has modified text in 14064 * some way), we won't continue with the processing. 14065 */ 14066 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 14067 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 14068 "enabling ignored."); 14069 dtrace_dof_destroy(dof); 14070 break; 14071 } 14072 #endif 14073 14074 /* 14075 * If we haven't allocated an anonymous state, we'll do so now. 14076 */ 14077 if ((state = dtrace_anon.dta_state) == NULL) { 14078 #if defined(sun) 14079 state = dtrace_state_create(NULL, NULL); 14080 #else 14081 state = dtrace_state_create(NULL); 14082 #endif 14083 dtrace_anon.dta_state = state; 14084 14085 if (state == NULL) { 14086 /* 14087 * This basically shouldn't happen: the only 14088 * failure mode from dtrace_state_create() is a 14089 * failure of ddi_soft_state_zalloc() that 14090 * itself should never happen. Still, the 14091 * interface allows for a failure mode, and 14092 * we want to fail as gracefully as possible: 14093 * we'll emit an error message and cease 14094 * processing anonymous state in this case. 14095 */ 14096 cmn_err(CE_WARN, "failed to create " 14097 "anonymous state"); 14098 dtrace_dof_destroy(dof); 14099 break; 14100 } 14101 } 14102 14103 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 14104 &dtrace_anon.dta_enabling, 0, B_TRUE); 14105 14106 if (rv == 0) 14107 rv = dtrace_dof_options(dof, state); 14108 14109 dtrace_err_verbose = 0; 14110 dtrace_dof_destroy(dof); 14111 14112 if (rv != 0) { 14113 /* 14114 * This is malformed DOF; chuck any anonymous state 14115 * that we created. 14116 */ 14117 ASSERT(dtrace_anon.dta_enabling == NULL); 14118 dtrace_state_destroy(state); 14119 dtrace_anon.dta_state = NULL; 14120 break; 14121 } 14122 14123 ASSERT(dtrace_anon.dta_enabling != NULL); 14124 } 14125 14126 if (dtrace_anon.dta_enabling != NULL) { 14127 int rval; 14128 14129 /* 14130 * dtrace_enabling_retain() can only fail because we are 14131 * trying to retain more enablings than are allowed -- but 14132 * we only have one anonymous enabling, and we are guaranteed 14133 * to be allowed at least one retained enabling; we assert 14134 * that dtrace_enabling_retain() returns success. 14135 */ 14136 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 14137 ASSERT(rval == 0); 14138 14139 dtrace_enabling_dump(dtrace_anon.dta_enabling); 14140 } 14141 } 14142 14143 /* 14144 * DTrace Helper Functions 14145 */ 14146 static void 14147 dtrace_helper_trace(dtrace_helper_action_t *helper, 14148 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 14149 { 14150 uint32_t size, next, nnext, i; 14151 dtrace_helptrace_t *ent; 14152 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 14153 14154 if (!dtrace_helptrace_enabled) 14155 return; 14156 14157 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 14158 14159 /* 14160 * What would a tracing framework be without its own tracing 14161 * framework? (Well, a hell of a lot simpler, for starters...) 14162 */ 14163 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 14164 sizeof (uint64_t) - sizeof (uint64_t); 14165 14166 /* 14167 * Iterate until we can allocate a slot in the trace buffer. 14168 */ 14169 do { 14170 next = dtrace_helptrace_next; 14171 14172 if (next + size < dtrace_helptrace_bufsize) { 14173 nnext = next + size; 14174 } else { 14175 nnext = size; 14176 } 14177 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 14178 14179 /* 14180 * We have our slot; fill it in. 14181 */ 14182 if (nnext == size) 14183 next = 0; 14184 14185 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 14186 ent->dtht_helper = helper; 14187 ent->dtht_where = where; 14188 ent->dtht_nlocals = vstate->dtvs_nlocals; 14189 14190 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 14191 mstate->dtms_fltoffs : -1; 14192 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 14193 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 14194 14195 for (i = 0; i < vstate->dtvs_nlocals; i++) { 14196 dtrace_statvar_t *svar; 14197 14198 if ((svar = vstate->dtvs_locals[i]) == NULL) 14199 continue; 14200 14201 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 14202 ent->dtht_locals[i] = 14203 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 14204 } 14205 } 14206 14207 static uint64_t 14208 dtrace_helper(int which, dtrace_mstate_t *mstate, 14209 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 14210 { 14211 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 14212 uint64_t sarg0 = mstate->dtms_arg[0]; 14213 uint64_t sarg1 = mstate->dtms_arg[1]; 14214 uint64_t rval = 0; 14215 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 14216 dtrace_helper_action_t *helper; 14217 dtrace_vstate_t *vstate; 14218 dtrace_difo_t *pred; 14219 int i, trace = dtrace_helptrace_enabled; 14220 14221 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 14222 14223 if (helpers == NULL) 14224 return (0); 14225 14226 if ((helper = helpers->dthps_actions[which]) == NULL) 14227 return (0); 14228 14229 vstate = &helpers->dthps_vstate; 14230 mstate->dtms_arg[0] = arg0; 14231 mstate->dtms_arg[1] = arg1; 14232 14233 /* 14234 * Now iterate over each helper. If its predicate evaluates to 'true', 14235 * we'll call the corresponding actions. Note that the below calls 14236 * to dtrace_dif_emulate() may set faults in machine state. This is 14237 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 14238 * the stored DIF offset with its own (which is the desired behavior). 14239 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 14240 * from machine state; this is okay, too. 14241 */ 14242 for (; helper != NULL; helper = helper->dtha_next) { 14243 if ((pred = helper->dtha_predicate) != NULL) { 14244 if (trace) 14245 dtrace_helper_trace(helper, mstate, vstate, 0); 14246 14247 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 14248 goto next; 14249 14250 if (*flags & CPU_DTRACE_FAULT) 14251 goto err; 14252 } 14253 14254 for (i = 0; i < helper->dtha_nactions; i++) { 14255 if (trace) 14256 dtrace_helper_trace(helper, 14257 mstate, vstate, i + 1); 14258 14259 rval = dtrace_dif_emulate(helper->dtha_actions[i], 14260 mstate, vstate, state); 14261 14262 if (*flags & CPU_DTRACE_FAULT) 14263 goto err; 14264 } 14265 14266 next: 14267 if (trace) 14268 dtrace_helper_trace(helper, mstate, vstate, 14269 DTRACE_HELPTRACE_NEXT); 14270 } 14271 14272 if (trace) 14273 dtrace_helper_trace(helper, mstate, vstate, 14274 DTRACE_HELPTRACE_DONE); 14275 14276 /* 14277 * Restore the arg0 that we saved upon entry. 14278 */ 14279 mstate->dtms_arg[0] = sarg0; 14280 mstate->dtms_arg[1] = sarg1; 14281 14282 return (rval); 14283 14284 err: 14285 if (trace) 14286 dtrace_helper_trace(helper, mstate, vstate, 14287 DTRACE_HELPTRACE_ERR); 14288 14289 /* 14290 * Restore the arg0 that we saved upon entry. 14291 */ 14292 mstate->dtms_arg[0] = sarg0; 14293 mstate->dtms_arg[1] = sarg1; 14294 14295 return (0); 14296 } 14297 14298 static void 14299 dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 14300 dtrace_vstate_t *vstate) 14301 { 14302 int i; 14303 14304 if (helper->dtha_predicate != NULL) 14305 dtrace_difo_release(helper->dtha_predicate, vstate); 14306 14307 for (i = 0; i < helper->dtha_nactions; i++) { 14308 ASSERT(helper->dtha_actions[i] != NULL); 14309 dtrace_difo_release(helper->dtha_actions[i], vstate); 14310 } 14311 14312 kmem_free(helper->dtha_actions, 14313 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 14314 kmem_free(helper, sizeof (dtrace_helper_action_t)); 14315 } 14316 14317 static int 14318 dtrace_helper_destroygen(int gen) 14319 { 14320 proc_t *p = curproc; 14321 dtrace_helpers_t *help = p->p_dtrace_helpers; 14322 dtrace_vstate_t *vstate; 14323 int i; 14324 14325 ASSERT(MUTEX_HELD(&dtrace_lock)); 14326 14327 if (help == NULL || gen > help->dthps_generation) 14328 return (EINVAL); 14329 14330 vstate = &help->dthps_vstate; 14331 14332 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14333 dtrace_helper_action_t *last = NULL, *h, *next; 14334 14335 for (h = help->dthps_actions[i]; h != NULL; h = next) { 14336 next = h->dtha_next; 14337 14338 if (h->dtha_generation == gen) { 14339 if (last != NULL) { 14340 last->dtha_next = next; 14341 } else { 14342 help->dthps_actions[i] = next; 14343 } 14344 14345 dtrace_helper_action_destroy(h, vstate); 14346 } else { 14347 last = h; 14348 } 14349 } 14350 } 14351 14352 /* 14353 * Interate until we've cleared out all helper providers with the 14354 * given generation number. 14355 */ 14356 for (;;) { 14357 dtrace_helper_provider_t *prov; 14358 14359 /* 14360 * Look for a helper provider with the right generation. We 14361 * have to start back at the beginning of the list each time 14362 * because we drop dtrace_lock. It's unlikely that we'll make 14363 * more than two passes. 14364 */ 14365 for (i = 0; i < help->dthps_nprovs; i++) { 14366 prov = help->dthps_provs[i]; 14367 14368 if (prov->dthp_generation == gen) 14369 break; 14370 } 14371 14372 /* 14373 * If there were no matches, we're done. 14374 */ 14375 if (i == help->dthps_nprovs) 14376 break; 14377 14378 /* 14379 * Move the last helper provider into this slot. 14380 */ 14381 help->dthps_nprovs--; 14382 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 14383 help->dthps_provs[help->dthps_nprovs] = NULL; 14384 14385 mutex_exit(&dtrace_lock); 14386 14387 /* 14388 * If we have a meta provider, remove this helper provider. 14389 */ 14390 mutex_enter(&dtrace_meta_lock); 14391 if (dtrace_meta_pid != NULL) { 14392 ASSERT(dtrace_deferred_pid == NULL); 14393 dtrace_helper_provider_remove(&prov->dthp_prov, 14394 p->p_pid); 14395 } 14396 mutex_exit(&dtrace_meta_lock); 14397 14398 dtrace_helper_provider_destroy(prov); 14399 14400 mutex_enter(&dtrace_lock); 14401 } 14402 14403 return (0); 14404 } 14405 14406 static int 14407 dtrace_helper_validate(dtrace_helper_action_t *helper) 14408 { 14409 int err = 0, i; 14410 dtrace_difo_t *dp; 14411 14412 if ((dp = helper->dtha_predicate) != NULL) 14413 err += dtrace_difo_validate_helper(dp); 14414 14415 for (i = 0; i < helper->dtha_nactions; i++) 14416 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 14417 14418 return (err == 0); 14419 } 14420 14421 static int 14422 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 14423 { 14424 dtrace_helpers_t *help; 14425 dtrace_helper_action_t *helper, *last; 14426 dtrace_actdesc_t *act; 14427 dtrace_vstate_t *vstate; 14428 dtrace_predicate_t *pred; 14429 int count = 0, nactions = 0, i; 14430 14431 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 14432 return (EINVAL); 14433 14434 help = curproc->p_dtrace_helpers; 14435 last = help->dthps_actions[which]; 14436 vstate = &help->dthps_vstate; 14437 14438 for (count = 0; last != NULL; last = last->dtha_next) { 14439 count++; 14440 if (last->dtha_next == NULL) 14441 break; 14442 } 14443 14444 /* 14445 * If we already have dtrace_helper_actions_max helper actions for this 14446 * helper action type, we'll refuse to add a new one. 14447 */ 14448 if (count >= dtrace_helper_actions_max) 14449 return (ENOSPC); 14450 14451 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 14452 helper->dtha_generation = help->dthps_generation; 14453 14454 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 14455 ASSERT(pred->dtp_difo != NULL); 14456 dtrace_difo_hold(pred->dtp_difo); 14457 helper->dtha_predicate = pred->dtp_difo; 14458 } 14459 14460 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 14461 if (act->dtad_kind != DTRACEACT_DIFEXPR) 14462 goto err; 14463 14464 if (act->dtad_difo == NULL) 14465 goto err; 14466 14467 nactions++; 14468 } 14469 14470 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 14471 (helper->dtha_nactions = nactions), KM_SLEEP); 14472 14473 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 14474 dtrace_difo_hold(act->dtad_difo); 14475 helper->dtha_actions[i++] = act->dtad_difo; 14476 } 14477 14478 if (!dtrace_helper_validate(helper)) 14479 goto err; 14480 14481 if (last == NULL) { 14482 help->dthps_actions[which] = helper; 14483 } else { 14484 last->dtha_next = helper; 14485 } 14486 14487 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 14488 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 14489 dtrace_helptrace_next = 0; 14490 } 14491 14492 return (0); 14493 err: 14494 dtrace_helper_action_destroy(helper, vstate); 14495 return (EINVAL); 14496 } 14497 14498 static void 14499 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 14500 dof_helper_t *dofhp) 14501 { 14502 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 14503 14504 mutex_enter(&dtrace_meta_lock); 14505 mutex_enter(&dtrace_lock); 14506 14507 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 14508 /* 14509 * If the dtrace module is loaded but not attached, or if 14510 * there aren't isn't a meta provider registered to deal with 14511 * these provider descriptions, we need to postpone creating 14512 * the actual providers until later. 14513 */ 14514 14515 if (help->dthps_next == NULL && help->dthps_prev == NULL && 14516 dtrace_deferred_pid != help) { 14517 help->dthps_deferred = 1; 14518 help->dthps_pid = p->p_pid; 14519 help->dthps_next = dtrace_deferred_pid; 14520 help->dthps_prev = NULL; 14521 if (dtrace_deferred_pid != NULL) 14522 dtrace_deferred_pid->dthps_prev = help; 14523 dtrace_deferred_pid = help; 14524 } 14525 14526 mutex_exit(&dtrace_lock); 14527 14528 } else if (dofhp != NULL) { 14529 /* 14530 * If the dtrace module is loaded and we have a particular 14531 * helper provider description, pass that off to the 14532 * meta provider. 14533 */ 14534 14535 mutex_exit(&dtrace_lock); 14536 14537 dtrace_helper_provide(dofhp, p->p_pid); 14538 14539 } else { 14540 /* 14541 * Otherwise, just pass all the helper provider descriptions 14542 * off to the meta provider. 14543 */ 14544 14545 int i; 14546 mutex_exit(&dtrace_lock); 14547 14548 for (i = 0; i < help->dthps_nprovs; i++) { 14549 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 14550 p->p_pid); 14551 } 14552 } 14553 14554 mutex_exit(&dtrace_meta_lock); 14555 } 14556 14557 static int 14558 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 14559 { 14560 dtrace_helpers_t *help; 14561 dtrace_helper_provider_t *hprov, **tmp_provs; 14562 uint_t tmp_maxprovs, i; 14563 14564 ASSERT(MUTEX_HELD(&dtrace_lock)); 14565 14566 help = curproc->p_dtrace_helpers; 14567 ASSERT(help != NULL); 14568 14569 /* 14570 * If we already have dtrace_helper_providers_max helper providers, 14571 * we're refuse to add a new one. 14572 */ 14573 if (help->dthps_nprovs >= dtrace_helper_providers_max) 14574 return (ENOSPC); 14575 14576 /* 14577 * Check to make sure this isn't a duplicate. 14578 */ 14579 for (i = 0; i < help->dthps_nprovs; i++) { 14580 if (dofhp->dofhp_addr == 14581 help->dthps_provs[i]->dthp_prov.dofhp_addr) 14582 return (EALREADY); 14583 } 14584 14585 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 14586 hprov->dthp_prov = *dofhp; 14587 hprov->dthp_ref = 1; 14588 hprov->dthp_generation = gen; 14589 14590 /* 14591 * Allocate a bigger table for helper providers if it's already full. 14592 */ 14593 if (help->dthps_maxprovs == help->dthps_nprovs) { 14594 tmp_maxprovs = help->dthps_maxprovs; 14595 tmp_provs = help->dthps_provs; 14596 14597 if (help->dthps_maxprovs == 0) 14598 help->dthps_maxprovs = 2; 14599 else 14600 help->dthps_maxprovs *= 2; 14601 if (help->dthps_maxprovs > dtrace_helper_providers_max) 14602 help->dthps_maxprovs = dtrace_helper_providers_max; 14603 14604 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 14605 14606 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 14607 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14608 14609 if (tmp_provs != NULL) { 14610 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 14611 sizeof (dtrace_helper_provider_t *)); 14612 kmem_free(tmp_provs, tmp_maxprovs * 14613 sizeof (dtrace_helper_provider_t *)); 14614 } 14615 } 14616 14617 help->dthps_provs[help->dthps_nprovs] = hprov; 14618 help->dthps_nprovs++; 14619 14620 return (0); 14621 } 14622 14623 static void 14624 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 14625 { 14626 mutex_enter(&dtrace_lock); 14627 14628 if (--hprov->dthp_ref == 0) { 14629 dof_hdr_t *dof; 14630 mutex_exit(&dtrace_lock); 14631 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 14632 dtrace_dof_destroy(dof); 14633 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 14634 } else { 14635 mutex_exit(&dtrace_lock); 14636 } 14637 } 14638 14639 static int 14640 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 14641 { 14642 uintptr_t daddr = (uintptr_t)dof; 14643 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 14644 dof_provider_t *provider; 14645 dof_probe_t *probe; 14646 uint8_t *arg; 14647 char *strtab, *typestr; 14648 dof_stridx_t typeidx; 14649 size_t typesz; 14650 uint_t nprobes, j, k; 14651 14652 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 14653 14654 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 14655 dtrace_dof_error(dof, "misaligned section offset"); 14656 return (-1); 14657 } 14658 14659 /* 14660 * The section needs to be large enough to contain the DOF provider 14661 * structure appropriate for the given version. 14662 */ 14663 if (sec->dofs_size < 14664 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 14665 offsetof(dof_provider_t, dofpv_prenoffs) : 14666 sizeof (dof_provider_t))) { 14667 dtrace_dof_error(dof, "provider section too small"); 14668 return (-1); 14669 } 14670 14671 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 14672 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 14673 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 14674 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 14675 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 14676 14677 if (str_sec == NULL || prb_sec == NULL || 14678 arg_sec == NULL || off_sec == NULL) 14679 return (-1); 14680 14681 enoff_sec = NULL; 14682 14683 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14684 provider->dofpv_prenoffs != DOF_SECT_NONE && 14685 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 14686 provider->dofpv_prenoffs)) == NULL) 14687 return (-1); 14688 14689 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 14690 14691 if (provider->dofpv_name >= str_sec->dofs_size || 14692 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 14693 dtrace_dof_error(dof, "invalid provider name"); 14694 return (-1); 14695 } 14696 14697 if (prb_sec->dofs_entsize == 0 || 14698 prb_sec->dofs_entsize > prb_sec->dofs_size) { 14699 dtrace_dof_error(dof, "invalid entry size"); 14700 return (-1); 14701 } 14702 14703 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 14704 dtrace_dof_error(dof, "misaligned entry size"); 14705 return (-1); 14706 } 14707 14708 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 14709 dtrace_dof_error(dof, "invalid entry size"); 14710 return (-1); 14711 } 14712 14713 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 14714 dtrace_dof_error(dof, "misaligned section offset"); 14715 return (-1); 14716 } 14717 14718 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 14719 dtrace_dof_error(dof, "invalid entry size"); 14720 return (-1); 14721 } 14722 14723 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 14724 14725 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 14726 14727 /* 14728 * Take a pass through the probes to check for errors. 14729 */ 14730 for (j = 0; j < nprobes; j++) { 14731 probe = (dof_probe_t *)(uintptr_t)(daddr + 14732 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 14733 14734 if (probe->dofpr_func >= str_sec->dofs_size) { 14735 dtrace_dof_error(dof, "invalid function name"); 14736 return (-1); 14737 } 14738 14739 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 14740 dtrace_dof_error(dof, "function name too long"); 14741 return (-1); 14742 } 14743 14744 if (probe->dofpr_name >= str_sec->dofs_size || 14745 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 14746 dtrace_dof_error(dof, "invalid probe name"); 14747 return (-1); 14748 } 14749 14750 /* 14751 * The offset count must not wrap the index, and the offsets 14752 * must also not overflow the section's data. 14753 */ 14754 if (probe->dofpr_offidx + probe->dofpr_noffs < 14755 probe->dofpr_offidx || 14756 (probe->dofpr_offidx + probe->dofpr_noffs) * 14757 off_sec->dofs_entsize > off_sec->dofs_size) { 14758 dtrace_dof_error(dof, "invalid probe offset"); 14759 return (-1); 14760 } 14761 14762 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 14763 /* 14764 * If there's no is-enabled offset section, make sure 14765 * there aren't any is-enabled offsets. Otherwise 14766 * perform the same checks as for probe offsets 14767 * (immediately above). 14768 */ 14769 if (enoff_sec == NULL) { 14770 if (probe->dofpr_enoffidx != 0 || 14771 probe->dofpr_nenoffs != 0) { 14772 dtrace_dof_error(dof, "is-enabled " 14773 "offsets with null section"); 14774 return (-1); 14775 } 14776 } else if (probe->dofpr_enoffidx + 14777 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 14778 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 14779 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 14780 dtrace_dof_error(dof, "invalid is-enabled " 14781 "offset"); 14782 return (-1); 14783 } 14784 14785 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 14786 dtrace_dof_error(dof, "zero probe and " 14787 "is-enabled offsets"); 14788 return (-1); 14789 } 14790 } else if (probe->dofpr_noffs == 0) { 14791 dtrace_dof_error(dof, "zero probe offsets"); 14792 return (-1); 14793 } 14794 14795 if (probe->dofpr_argidx + probe->dofpr_xargc < 14796 probe->dofpr_argidx || 14797 (probe->dofpr_argidx + probe->dofpr_xargc) * 14798 arg_sec->dofs_entsize > arg_sec->dofs_size) { 14799 dtrace_dof_error(dof, "invalid args"); 14800 return (-1); 14801 } 14802 14803 typeidx = probe->dofpr_nargv; 14804 typestr = strtab + probe->dofpr_nargv; 14805 for (k = 0; k < probe->dofpr_nargc; k++) { 14806 if (typeidx >= str_sec->dofs_size) { 14807 dtrace_dof_error(dof, "bad " 14808 "native argument type"); 14809 return (-1); 14810 } 14811 14812 typesz = strlen(typestr) + 1; 14813 if (typesz > DTRACE_ARGTYPELEN) { 14814 dtrace_dof_error(dof, "native " 14815 "argument type too long"); 14816 return (-1); 14817 } 14818 typeidx += typesz; 14819 typestr += typesz; 14820 } 14821 14822 typeidx = probe->dofpr_xargv; 14823 typestr = strtab + probe->dofpr_xargv; 14824 for (k = 0; k < probe->dofpr_xargc; k++) { 14825 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 14826 dtrace_dof_error(dof, "bad " 14827 "native argument index"); 14828 return (-1); 14829 } 14830 14831 if (typeidx >= str_sec->dofs_size) { 14832 dtrace_dof_error(dof, "bad " 14833 "translated argument type"); 14834 return (-1); 14835 } 14836 14837 typesz = strlen(typestr) + 1; 14838 if (typesz > DTRACE_ARGTYPELEN) { 14839 dtrace_dof_error(dof, "translated argument " 14840 "type too long"); 14841 return (-1); 14842 } 14843 14844 typeidx += typesz; 14845 typestr += typesz; 14846 } 14847 } 14848 14849 return (0); 14850 } 14851 14852 static int 14853 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 14854 { 14855 dtrace_helpers_t *help; 14856 dtrace_vstate_t *vstate; 14857 dtrace_enabling_t *enab = NULL; 14858 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 14859 uintptr_t daddr = (uintptr_t)dof; 14860 14861 ASSERT(MUTEX_HELD(&dtrace_lock)); 14862 14863 if ((help = curproc->p_dtrace_helpers) == NULL) 14864 help = dtrace_helpers_create(curproc); 14865 14866 vstate = &help->dthps_vstate; 14867 14868 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 14869 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 14870 dtrace_dof_destroy(dof); 14871 return (rv); 14872 } 14873 14874 /* 14875 * Look for helper providers and validate their descriptions. 14876 */ 14877 if (dhp != NULL) { 14878 for (i = 0; i < dof->dofh_secnum; i++) { 14879 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 14880 dof->dofh_secoff + i * dof->dofh_secsize); 14881 14882 if (sec->dofs_type != DOF_SECT_PROVIDER) 14883 continue; 14884 14885 if (dtrace_helper_provider_validate(dof, sec) != 0) { 14886 dtrace_enabling_destroy(enab); 14887 dtrace_dof_destroy(dof); 14888 return (-1); 14889 } 14890 14891 nprovs++; 14892 } 14893 } 14894 14895 /* 14896 * Now we need to walk through the ECB descriptions in the enabling. 14897 */ 14898 for (i = 0; i < enab->dten_ndesc; i++) { 14899 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 14900 dtrace_probedesc_t *desc = &ep->dted_probe; 14901 14902 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 14903 continue; 14904 14905 if (strcmp(desc->dtpd_mod, "helper") != 0) 14906 continue; 14907 14908 if (strcmp(desc->dtpd_func, "ustack") != 0) 14909 continue; 14910 14911 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 14912 ep)) != 0) { 14913 /* 14914 * Adding this helper action failed -- we are now going 14915 * to rip out the entire generation and return failure. 14916 */ 14917 (void) dtrace_helper_destroygen(help->dthps_generation); 14918 dtrace_enabling_destroy(enab); 14919 dtrace_dof_destroy(dof); 14920 return (-1); 14921 } 14922 14923 nhelpers++; 14924 } 14925 14926 if (nhelpers < enab->dten_ndesc) 14927 dtrace_dof_error(dof, "unmatched helpers"); 14928 14929 gen = help->dthps_generation++; 14930 dtrace_enabling_destroy(enab); 14931 14932 if (dhp != NULL && nprovs > 0) { 14933 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 14934 if (dtrace_helper_provider_add(dhp, gen) == 0) { 14935 mutex_exit(&dtrace_lock); 14936 dtrace_helper_provider_register(curproc, help, dhp); 14937 mutex_enter(&dtrace_lock); 14938 14939 destroy = 0; 14940 } 14941 } 14942 14943 if (destroy) 14944 dtrace_dof_destroy(dof); 14945 14946 return (gen); 14947 } 14948 14949 static dtrace_helpers_t * 14950 dtrace_helpers_create(proc_t *p) 14951 { 14952 dtrace_helpers_t *help; 14953 14954 ASSERT(MUTEX_HELD(&dtrace_lock)); 14955 ASSERT(p->p_dtrace_helpers == NULL); 14956 14957 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 14958 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 14959 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 14960 14961 p->p_dtrace_helpers = help; 14962 dtrace_helpers++; 14963 14964 return (help); 14965 } 14966 14967 #if defined(sun) 14968 static 14969 #endif 14970 void 14971 dtrace_helpers_destroy(proc_t *p) 14972 { 14973 dtrace_helpers_t *help; 14974 dtrace_vstate_t *vstate; 14975 #if defined(sun) 14976 proc_t *p = curproc; 14977 #endif 14978 int i; 14979 14980 mutex_enter(&dtrace_lock); 14981 14982 ASSERT(p->p_dtrace_helpers != NULL); 14983 ASSERT(dtrace_helpers > 0); 14984 14985 help = p->p_dtrace_helpers; 14986 vstate = &help->dthps_vstate; 14987 14988 /* 14989 * We're now going to lose the help from this process. 14990 */ 14991 p->p_dtrace_helpers = NULL; 14992 dtrace_sync(); 14993 14994 /* 14995 * Destory the helper actions. 14996 */ 14997 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14998 dtrace_helper_action_t *h, *next; 14999 15000 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15001 next = h->dtha_next; 15002 dtrace_helper_action_destroy(h, vstate); 15003 h = next; 15004 } 15005 } 15006 15007 mutex_exit(&dtrace_lock); 15008 15009 /* 15010 * Destroy the helper providers. 15011 */ 15012 if (help->dthps_maxprovs > 0) { 15013 mutex_enter(&dtrace_meta_lock); 15014 if (dtrace_meta_pid != NULL) { 15015 ASSERT(dtrace_deferred_pid == NULL); 15016 15017 for (i = 0; i < help->dthps_nprovs; i++) { 15018 dtrace_helper_provider_remove( 15019 &help->dthps_provs[i]->dthp_prov, p->p_pid); 15020 } 15021 } else { 15022 mutex_enter(&dtrace_lock); 15023 ASSERT(help->dthps_deferred == 0 || 15024 help->dthps_next != NULL || 15025 help->dthps_prev != NULL || 15026 help == dtrace_deferred_pid); 15027 15028 /* 15029 * Remove the helper from the deferred list. 15030 */ 15031 if (help->dthps_next != NULL) 15032 help->dthps_next->dthps_prev = help->dthps_prev; 15033 if (help->dthps_prev != NULL) 15034 help->dthps_prev->dthps_next = help->dthps_next; 15035 if (dtrace_deferred_pid == help) { 15036 dtrace_deferred_pid = help->dthps_next; 15037 ASSERT(help->dthps_prev == NULL); 15038 } 15039 15040 mutex_exit(&dtrace_lock); 15041 } 15042 15043 mutex_exit(&dtrace_meta_lock); 15044 15045 for (i = 0; i < help->dthps_nprovs; i++) { 15046 dtrace_helper_provider_destroy(help->dthps_provs[i]); 15047 } 15048 15049 kmem_free(help->dthps_provs, help->dthps_maxprovs * 15050 sizeof (dtrace_helper_provider_t *)); 15051 } 15052 15053 mutex_enter(&dtrace_lock); 15054 15055 dtrace_vstate_fini(&help->dthps_vstate); 15056 kmem_free(help->dthps_actions, 15057 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 15058 kmem_free(help, sizeof (dtrace_helpers_t)); 15059 15060 --dtrace_helpers; 15061 mutex_exit(&dtrace_lock); 15062 } 15063 15064 #if defined(sun) 15065 static 15066 #endif 15067 void 15068 dtrace_helpers_duplicate(proc_t *from, proc_t *to) 15069 { 15070 dtrace_helpers_t *help, *newhelp; 15071 dtrace_helper_action_t *helper, *new, *last; 15072 dtrace_difo_t *dp; 15073 dtrace_vstate_t *vstate; 15074 int i, j, sz, hasprovs = 0; 15075 15076 mutex_enter(&dtrace_lock); 15077 ASSERT(from->p_dtrace_helpers != NULL); 15078 ASSERT(dtrace_helpers > 0); 15079 15080 help = from->p_dtrace_helpers; 15081 newhelp = dtrace_helpers_create(to); 15082 ASSERT(to->p_dtrace_helpers != NULL); 15083 15084 newhelp->dthps_generation = help->dthps_generation; 15085 vstate = &newhelp->dthps_vstate; 15086 15087 /* 15088 * Duplicate the helper actions. 15089 */ 15090 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15091 if ((helper = help->dthps_actions[i]) == NULL) 15092 continue; 15093 15094 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 15095 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 15096 KM_SLEEP); 15097 new->dtha_generation = helper->dtha_generation; 15098 15099 if ((dp = helper->dtha_predicate) != NULL) { 15100 dp = dtrace_difo_duplicate(dp, vstate); 15101 new->dtha_predicate = dp; 15102 } 15103 15104 new->dtha_nactions = helper->dtha_nactions; 15105 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 15106 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 15107 15108 for (j = 0; j < new->dtha_nactions; j++) { 15109 dtrace_difo_t *dp = helper->dtha_actions[j]; 15110 15111 ASSERT(dp != NULL); 15112 dp = dtrace_difo_duplicate(dp, vstate); 15113 new->dtha_actions[j] = dp; 15114 } 15115 15116 if (last != NULL) { 15117 last->dtha_next = new; 15118 } else { 15119 newhelp->dthps_actions[i] = new; 15120 } 15121 15122 last = new; 15123 } 15124 } 15125 15126 /* 15127 * Duplicate the helper providers and register them with the 15128 * DTrace framework. 15129 */ 15130 if (help->dthps_nprovs > 0) { 15131 newhelp->dthps_nprovs = help->dthps_nprovs; 15132 newhelp->dthps_maxprovs = help->dthps_nprovs; 15133 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 15134 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 15135 for (i = 0; i < newhelp->dthps_nprovs; i++) { 15136 newhelp->dthps_provs[i] = help->dthps_provs[i]; 15137 newhelp->dthps_provs[i]->dthp_ref++; 15138 } 15139 15140 hasprovs = 1; 15141 } 15142 15143 mutex_exit(&dtrace_lock); 15144 15145 if (hasprovs) 15146 dtrace_helper_provider_register(to, newhelp, NULL); 15147 } 15148 15149 #if defined(sun) 15150 /* 15151 * DTrace Hook Functions 15152 */ 15153 static void 15154 dtrace_module_loaded(modctl_t *ctl) 15155 { 15156 dtrace_provider_t *prv; 15157 15158 mutex_enter(&dtrace_provider_lock); 15159 mutex_enter(&mod_lock); 15160 15161 ASSERT(ctl->mod_busy); 15162 15163 /* 15164 * We're going to call each providers per-module provide operation 15165 * specifying only this module. 15166 */ 15167 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 15168 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 15169 15170 mutex_exit(&mod_lock); 15171 mutex_exit(&dtrace_provider_lock); 15172 15173 /* 15174 * If we have any retained enablings, we need to match against them. 15175 * Enabling probes requires that cpu_lock be held, and we cannot hold 15176 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 15177 * module. (In particular, this happens when loading scheduling 15178 * classes.) So if we have any retained enablings, we need to dispatch 15179 * our task queue to do the match for us. 15180 */ 15181 mutex_enter(&dtrace_lock); 15182 15183 if (dtrace_retained == NULL) { 15184 mutex_exit(&dtrace_lock); 15185 return; 15186 } 15187 15188 (void) taskq_dispatch(dtrace_taskq, 15189 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 15190 15191 mutex_exit(&dtrace_lock); 15192 15193 /* 15194 * And now, for a little heuristic sleaze: in general, we want to 15195 * match modules as soon as they load. However, we cannot guarantee 15196 * this, because it would lead us to the lock ordering violation 15197 * outlined above. The common case, of course, is that cpu_lock is 15198 * _not_ held -- so we delay here for a clock tick, hoping that that's 15199 * long enough for the task queue to do its work. If it's not, it's 15200 * not a serious problem -- it just means that the module that we 15201 * just loaded may not be immediately instrumentable. 15202 */ 15203 delay(1); 15204 } 15205 15206 static void 15207 dtrace_module_unloaded(modctl_t *ctl) 15208 { 15209 dtrace_probe_t template, *probe, *first, *next; 15210 dtrace_provider_t *prov; 15211 15212 template.dtpr_mod = ctl->mod_modname; 15213 15214 mutex_enter(&dtrace_provider_lock); 15215 mutex_enter(&mod_lock); 15216 mutex_enter(&dtrace_lock); 15217 15218 if (dtrace_bymod == NULL) { 15219 /* 15220 * The DTrace module is loaded (obviously) but not attached; 15221 * we don't have any work to do. 15222 */ 15223 mutex_exit(&dtrace_provider_lock); 15224 mutex_exit(&mod_lock); 15225 mutex_exit(&dtrace_lock); 15226 return; 15227 } 15228 15229 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 15230 probe != NULL; probe = probe->dtpr_nextmod) { 15231 if (probe->dtpr_ecb != NULL) { 15232 mutex_exit(&dtrace_provider_lock); 15233 mutex_exit(&mod_lock); 15234 mutex_exit(&dtrace_lock); 15235 15236 /* 15237 * This shouldn't _actually_ be possible -- we're 15238 * unloading a module that has an enabled probe in it. 15239 * (It's normally up to the provider to make sure that 15240 * this can't happen.) However, because dtps_enable() 15241 * doesn't have a failure mode, there can be an 15242 * enable/unload race. Upshot: we don't want to 15243 * assert, but we're not going to disable the 15244 * probe, either. 15245 */ 15246 if (dtrace_err_verbose) { 15247 cmn_err(CE_WARN, "unloaded module '%s' had " 15248 "enabled probes", ctl->mod_modname); 15249 } 15250 15251 return; 15252 } 15253 } 15254 15255 probe = first; 15256 15257 for (first = NULL; probe != NULL; probe = next) { 15258 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 15259 15260 dtrace_probes[probe->dtpr_id - 1] = NULL; 15261 15262 next = probe->dtpr_nextmod; 15263 dtrace_hash_remove(dtrace_bymod, probe); 15264 dtrace_hash_remove(dtrace_byfunc, probe); 15265 dtrace_hash_remove(dtrace_byname, probe); 15266 15267 if (first == NULL) { 15268 first = probe; 15269 probe->dtpr_nextmod = NULL; 15270 } else { 15271 probe->dtpr_nextmod = first; 15272 first = probe; 15273 } 15274 } 15275 15276 /* 15277 * We've removed all of the module's probes from the hash chains and 15278 * from the probe array. Now issue a dtrace_sync() to be sure that 15279 * everyone has cleared out from any probe array processing. 15280 */ 15281 dtrace_sync(); 15282 15283 for (probe = first; probe != NULL; probe = first) { 15284 first = probe->dtpr_nextmod; 15285 prov = probe->dtpr_provider; 15286 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 15287 probe->dtpr_arg); 15288 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 15289 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 15290 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 15291 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 15292 kmem_free(probe, sizeof (dtrace_probe_t)); 15293 } 15294 15295 mutex_exit(&dtrace_lock); 15296 mutex_exit(&mod_lock); 15297 mutex_exit(&dtrace_provider_lock); 15298 } 15299 15300 static void 15301 dtrace_suspend(void) 15302 { 15303 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 15304 } 15305 15306 static void 15307 dtrace_resume(void) 15308 { 15309 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 15310 } 15311 #endif 15312 15313 static int 15314 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 15315 { 15316 ASSERT(MUTEX_HELD(&cpu_lock)); 15317 mutex_enter(&dtrace_lock); 15318 15319 switch (what) { 15320 case CPU_CONFIG: { 15321 dtrace_state_t *state; 15322 dtrace_optval_t *opt, rs, c; 15323 15324 /* 15325 * For now, we only allocate a new buffer for anonymous state. 15326 */ 15327 if ((state = dtrace_anon.dta_state) == NULL) 15328 break; 15329 15330 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 15331 break; 15332 15333 opt = state->dts_options; 15334 c = opt[DTRACEOPT_CPU]; 15335 15336 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 15337 break; 15338 15339 /* 15340 * Regardless of what the actual policy is, we're going to 15341 * temporarily set our resize policy to be manual. We're 15342 * also going to temporarily set our CPU option to denote 15343 * the newly configured CPU. 15344 */ 15345 rs = opt[DTRACEOPT_BUFRESIZE]; 15346 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 15347 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 15348 15349 (void) dtrace_state_buffers(state); 15350 15351 opt[DTRACEOPT_BUFRESIZE] = rs; 15352 opt[DTRACEOPT_CPU] = c; 15353 15354 break; 15355 } 15356 15357 case CPU_UNCONFIG: 15358 /* 15359 * We don't free the buffer in the CPU_UNCONFIG case. (The 15360 * buffer will be freed when the consumer exits.) 15361 */ 15362 break; 15363 15364 default: 15365 break; 15366 } 15367 15368 mutex_exit(&dtrace_lock); 15369 return (0); 15370 } 15371 15372 #if defined(sun) 15373 static void 15374 dtrace_cpu_setup_initial(processorid_t cpu) 15375 { 15376 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 15377 } 15378 #endif 15379 15380 static void 15381 dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 15382 { 15383 if (dtrace_toxranges >= dtrace_toxranges_max) { 15384 int osize, nsize; 15385 dtrace_toxrange_t *range; 15386 15387 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 15388 15389 if (osize == 0) { 15390 ASSERT(dtrace_toxrange == NULL); 15391 ASSERT(dtrace_toxranges_max == 0); 15392 dtrace_toxranges_max = 1; 15393 } else { 15394 dtrace_toxranges_max <<= 1; 15395 } 15396 15397 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 15398 range = kmem_zalloc(nsize, KM_SLEEP); 15399 15400 if (dtrace_toxrange != NULL) { 15401 ASSERT(osize != 0); 15402 bcopy(dtrace_toxrange, range, osize); 15403 kmem_free(dtrace_toxrange, osize); 15404 } 15405 15406 dtrace_toxrange = range; 15407 } 15408 15409 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 15410 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 15411 15412 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 15413 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 15414 dtrace_toxranges++; 15415 } 15416 15417 /* 15418 * DTrace Driver Cookbook Functions 15419 */ 15420 #if defined(sun) 15421 /*ARGSUSED*/ 15422 static int 15423 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 15424 { 15425 dtrace_provider_id_t id; 15426 dtrace_state_t *state = NULL; 15427 dtrace_enabling_t *enab; 15428 15429 mutex_enter(&cpu_lock); 15430 mutex_enter(&dtrace_provider_lock); 15431 mutex_enter(&dtrace_lock); 15432 15433 if (ddi_soft_state_init(&dtrace_softstate, 15434 sizeof (dtrace_state_t), 0) != 0) { 15435 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 15436 mutex_exit(&cpu_lock); 15437 mutex_exit(&dtrace_provider_lock); 15438 mutex_exit(&dtrace_lock); 15439 return (DDI_FAILURE); 15440 } 15441 15442 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 15443 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 15444 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 15445 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 15446 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 15447 ddi_remove_minor_node(devi, NULL); 15448 ddi_soft_state_fini(&dtrace_softstate); 15449 mutex_exit(&cpu_lock); 15450 mutex_exit(&dtrace_provider_lock); 15451 mutex_exit(&dtrace_lock); 15452 return (DDI_FAILURE); 15453 } 15454 15455 ddi_report_dev(devi); 15456 dtrace_devi = devi; 15457 15458 dtrace_modload = dtrace_module_loaded; 15459 dtrace_modunload = dtrace_module_unloaded; 15460 dtrace_cpu_init = dtrace_cpu_setup_initial; 15461 dtrace_helpers_cleanup = dtrace_helpers_destroy; 15462 dtrace_helpers_fork = dtrace_helpers_duplicate; 15463 dtrace_cpustart_init = dtrace_suspend; 15464 dtrace_cpustart_fini = dtrace_resume; 15465 dtrace_debugger_init = dtrace_suspend; 15466 dtrace_debugger_fini = dtrace_resume; 15467 15468 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 15469 15470 ASSERT(MUTEX_HELD(&cpu_lock)); 15471 15472 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 15473 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 15474 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 15475 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 15476 VM_SLEEP | VMC_IDENTIFIER); 15477 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 15478 1, INT_MAX, 0); 15479 15480 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 15481 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 15482 NULL, NULL, NULL, NULL, NULL, 0); 15483 15484 ASSERT(MUTEX_HELD(&cpu_lock)); 15485 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 15486 offsetof(dtrace_probe_t, dtpr_nextmod), 15487 offsetof(dtrace_probe_t, dtpr_prevmod)); 15488 15489 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 15490 offsetof(dtrace_probe_t, dtpr_nextfunc), 15491 offsetof(dtrace_probe_t, dtpr_prevfunc)); 15492 15493 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 15494 offsetof(dtrace_probe_t, dtpr_nextname), 15495 offsetof(dtrace_probe_t, dtpr_prevname)); 15496 15497 if (dtrace_retain_max < 1) { 15498 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 15499 "setting to 1", dtrace_retain_max); 15500 dtrace_retain_max = 1; 15501 } 15502 15503 /* 15504 * Now discover our toxic ranges. 15505 */ 15506 dtrace_toxic_ranges(dtrace_toxrange_add); 15507 15508 /* 15509 * Before we register ourselves as a provider to our own framework, 15510 * we would like to assert that dtrace_provider is NULL -- but that's 15511 * not true if we were loaded as a dependency of a DTrace provider. 15512 * Once we've registered, we can assert that dtrace_provider is our 15513 * pseudo provider. 15514 */ 15515 (void) dtrace_register("dtrace", &dtrace_provider_attr, 15516 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 15517 15518 ASSERT(dtrace_provider != NULL); 15519 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 15520 15521 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 15522 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 15523 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 15524 dtrace_provider, NULL, NULL, "END", 0, NULL); 15525 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 15526 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 15527 15528 dtrace_anon_property(); 15529 mutex_exit(&cpu_lock); 15530 15531 /* 15532 * If DTrace helper tracing is enabled, we need to allocate the 15533 * trace buffer and initialize the values. 15534 */ 15535 if (dtrace_helptrace_enabled) { 15536 ASSERT(dtrace_helptrace_buffer == NULL); 15537 dtrace_helptrace_buffer = 15538 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 15539 dtrace_helptrace_next = 0; 15540 } 15541 15542 /* 15543 * If there are already providers, we must ask them to provide their 15544 * probes, and then match any anonymous enabling against them. Note 15545 * that there should be no other retained enablings at this time: 15546 * the only retained enablings at this time should be the anonymous 15547 * enabling. 15548 */ 15549 if (dtrace_anon.dta_enabling != NULL) { 15550 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 15551 15552 dtrace_enabling_provide(NULL); 15553 state = dtrace_anon.dta_state; 15554 15555 /* 15556 * We couldn't hold cpu_lock across the above call to 15557 * dtrace_enabling_provide(), but we must hold it to actually 15558 * enable the probes. We have to drop all of our locks, pick 15559 * up cpu_lock, and regain our locks before matching the 15560 * retained anonymous enabling. 15561 */ 15562 mutex_exit(&dtrace_lock); 15563 mutex_exit(&dtrace_provider_lock); 15564 15565 mutex_enter(&cpu_lock); 15566 mutex_enter(&dtrace_provider_lock); 15567 mutex_enter(&dtrace_lock); 15568 15569 if ((enab = dtrace_anon.dta_enabling) != NULL) 15570 (void) dtrace_enabling_match(enab, NULL); 15571 15572 mutex_exit(&cpu_lock); 15573 } 15574 15575 mutex_exit(&dtrace_lock); 15576 mutex_exit(&dtrace_provider_lock); 15577 15578 if (state != NULL) { 15579 /* 15580 * If we created any anonymous state, set it going now. 15581 */ 15582 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 15583 } 15584 15585 return (DDI_SUCCESS); 15586 } 15587 #endif 15588 15589 #if !defined(sun) 15590 #if __FreeBSD_version >= 800039 15591 static void dtrace_dtr(void *); 15592 #endif 15593 #endif 15594 15595 /*ARGSUSED*/ 15596 static int 15597 #if defined(sun) 15598 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 15599 #else 15600 dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 15601 #endif 15602 { 15603 dtrace_state_t *state; 15604 uint32_t priv; 15605 uid_t uid; 15606 zoneid_t zoneid; 15607 15608 #if defined(sun) 15609 if (getminor(*devp) == DTRACEMNRN_HELPER) 15610 return (0); 15611 15612 /* 15613 * If this wasn't an open with the "helper" minor, then it must be 15614 * the "dtrace" minor. 15615 */ 15616 ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE); 15617 #else 15618 cred_t *cred_p = NULL; 15619 15620 #if __FreeBSD_version < 800039 15621 /* 15622 * The first minor device is the one that is cloned so there is 15623 * nothing more to do here. 15624 */ 15625 if (dev2unit(dev) == 0) 15626 return 0; 15627 15628 /* 15629 * Devices are cloned, so if the DTrace state has already 15630 * been allocated, that means this device belongs to a 15631 * different client. Each client should open '/dev/dtrace' 15632 * to get a cloned device. 15633 */ 15634 if (dev->si_drv1 != NULL) 15635 return (EBUSY); 15636 #endif 15637 15638 cred_p = dev->si_cred; 15639 #endif 15640 15641 /* 15642 * If no DTRACE_PRIV_* bits are set in the credential, then the 15643 * caller lacks sufficient permission to do anything with DTrace. 15644 */ 15645 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 15646 if (priv == DTRACE_PRIV_NONE) { 15647 #if !defined(sun) 15648 #if __FreeBSD_version < 800039 15649 /* Destroy the cloned device. */ 15650 destroy_dev(dev); 15651 #endif 15652 #endif 15653 15654 return (EACCES); 15655 } 15656 15657 /* 15658 * Ask all providers to provide all their probes. 15659 */ 15660 mutex_enter(&dtrace_provider_lock); 15661 dtrace_probe_provide(NULL, NULL); 15662 mutex_exit(&dtrace_provider_lock); 15663 15664 mutex_enter(&cpu_lock); 15665 mutex_enter(&dtrace_lock); 15666 dtrace_opens++; 15667 dtrace_membar_producer(); 15668 15669 #if defined(sun) 15670 /* 15671 * If the kernel debugger is active (that is, if the kernel debugger 15672 * modified text in some way), we won't allow the open. 15673 */ 15674 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15675 dtrace_opens--; 15676 mutex_exit(&cpu_lock); 15677 mutex_exit(&dtrace_lock); 15678 return (EBUSY); 15679 } 15680 15681 state = dtrace_state_create(devp, cred_p); 15682 #else 15683 state = dtrace_state_create(dev); 15684 #if __FreeBSD_version < 800039 15685 dev->si_drv1 = state; 15686 #else 15687 devfs_set_cdevpriv(state, dtrace_dtr); 15688 #endif 15689 /* This code actually belongs in dtrace_attach() */ 15690 if (dtrace_opens == 1) 15691 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 15692 1, INT_MAX, 0); 15693 #endif 15694 15695 mutex_exit(&cpu_lock); 15696 15697 if (state == NULL) { 15698 #if defined(sun) 15699 if (--dtrace_opens == 0) 15700 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15701 #else 15702 --dtrace_opens; 15703 #endif 15704 mutex_exit(&dtrace_lock); 15705 #if !defined(sun) 15706 #if __FreeBSD_version < 800039 15707 /* Destroy the cloned device. */ 15708 destroy_dev(dev); 15709 #endif 15710 #endif 15711 return (EAGAIN); 15712 } 15713 15714 mutex_exit(&dtrace_lock); 15715 15716 return (0); 15717 } 15718 15719 /*ARGSUSED*/ 15720 #if defined(sun) 15721 static int 15722 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 15723 #elif __FreeBSD_version < 800039 15724 static int 15725 dtrace_close(struct cdev *dev, int flags, int fmt __unused, struct thread *td) 15726 #else 15727 static void 15728 dtrace_dtr(void *data) 15729 #endif 15730 { 15731 #if defined(sun) 15732 minor_t minor = getminor(dev); 15733 dtrace_state_t *state; 15734 15735 if (minor == DTRACEMNRN_HELPER) 15736 return (0); 15737 15738 state = ddi_get_soft_state(dtrace_softstate, minor); 15739 #else 15740 #if __FreeBSD_version < 800039 15741 dtrace_state_t *state = dev->si_drv1; 15742 15743 /* Check if this is not a cloned device. */ 15744 if (dev2unit(dev) == 0) 15745 return (0); 15746 #else 15747 dtrace_state_t *state = data; 15748 #endif 15749 15750 #endif 15751 15752 mutex_enter(&cpu_lock); 15753 mutex_enter(&dtrace_lock); 15754 15755 if (state != NULL) { 15756 if (state->dts_anon) { 15757 /* 15758 * There is anonymous state. Destroy that first. 15759 */ 15760 ASSERT(dtrace_anon.dta_state == NULL); 15761 dtrace_state_destroy(state->dts_anon); 15762 } 15763 15764 dtrace_state_destroy(state); 15765 15766 #if !defined(sun) 15767 kmem_free(state, 0); 15768 #if __FreeBSD_version < 800039 15769 dev->si_drv1 = NULL; 15770 #endif 15771 #endif 15772 } 15773 15774 ASSERT(dtrace_opens > 0); 15775 #if defined(sun) 15776 if (--dtrace_opens == 0) 15777 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15778 #else 15779 --dtrace_opens; 15780 /* This code actually belongs in dtrace_detach() */ 15781 if ((dtrace_opens == 0) && (dtrace_taskq != NULL)) { 15782 taskq_destroy(dtrace_taskq); 15783 dtrace_taskq = NULL; 15784 } 15785 #endif 15786 15787 mutex_exit(&dtrace_lock); 15788 mutex_exit(&cpu_lock); 15789 15790 #if __FreeBSD_version < 800039 15791 /* Schedule this cloned device to be destroyed. */ 15792 destroy_dev_sched(dev); 15793 #endif 15794 15795 #if defined(sun) || __FreeBSD_version < 800039 15796 return (0); 15797 #endif 15798 } 15799 15800 #if defined(sun) 15801 /*ARGSUSED*/ 15802 static int 15803 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 15804 { 15805 int rval; 15806 dof_helper_t help, *dhp = NULL; 15807 15808 switch (cmd) { 15809 case DTRACEHIOC_ADDDOF: 15810 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 15811 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 15812 return (EFAULT); 15813 } 15814 15815 dhp = &help; 15816 arg = (intptr_t)help.dofhp_dof; 15817 /*FALLTHROUGH*/ 15818 15819 case DTRACEHIOC_ADD: { 15820 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 15821 15822 if (dof == NULL) 15823 return (rval); 15824 15825 mutex_enter(&dtrace_lock); 15826 15827 /* 15828 * dtrace_helper_slurp() takes responsibility for the dof -- 15829 * it may free it now or it may save it and free it later. 15830 */ 15831 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 15832 *rv = rval; 15833 rval = 0; 15834 } else { 15835 rval = EINVAL; 15836 } 15837 15838 mutex_exit(&dtrace_lock); 15839 return (rval); 15840 } 15841 15842 case DTRACEHIOC_REMOVE: { 15843 mutex_enter(&dtrace_lock); 15844 rval = dtrace_helper_destroygen(arg); 15845 mutex_exit(&dtrace_lock); 15846 15847 return (rval); 15848 } 15849 15850 default: 15851 break; 15852 } 15853 15854 return (ENOTTY); 15855 } 15856 15857 /*ARGSUSED*/ 15858 static int 15859 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 15860 { 15861 minor_t minor = getminor(dev); 15862 dtrace_state_t *state; 15863 int rval; 15864 15865 if (minor == DTRACEMNRN_HELPER) 15866 return (dtrace_ioctl_helper(cmd, arg, rv)); 15867 15868 state = ddi_get_soft_state(dtrace_softstate, minor); 15869 15870 if (state->dts_anon) { 15871 ASSERT(dtrace_anon.dta_state == NULL); 15872 state = state->dts_anon; 15873 } 15874 15875 switch (cmd) { 15876 case DTRACEIOC_PROVIDER: { 15877 dtrace_providerdesc_t pvd; 15878 dtrace_provider_t *pvp; 15879 15880 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 15881 return (EFAULT); 15882 15883 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 15884 mutex_enter(&dtrace_provider_lock); 15885 15886 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 15887 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 15888 break; 15889 } 15890 15891 mutex_exit(&dtrace_provider_lock); 15892 15893 if (pvp == NULL) 15894 return (ESRCH); 15895 15896 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 15897 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 15898 15899 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 15900 return (EFAULT); 15901 15902 return (0); 15903 } 15904 15905 case DTRACEIOC_EPROBE: { 15906 dtrace_eprobedesc_t epdesc; 15907 dtrace_ecb_t *ecb; 15908 dtrace_action_t *act; 15909 void *buf; 15910 size_t size; 15911 uintptr_t dest; 15912 int nrecs; 15913 15914 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 15915 return (EFAULT); 15916 15917 mutex_enter(&dtrace_lock); 15918 15919 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 15920 mutex_exit(&dtrace_lock); 15921 return (EINVAL); 15922 } 15923 15924 if (ecb->dte_probe == NULL) { 15925 mutex_exit(&dtrace_lock); 15926 return (EINVAL); 15927 } 15928 15929 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 15930 epdesc.dtepd_uarg = ecb->dte_uarg; 15931 epdesc.dtepd_size = ecb->dte_size; 15932 15933 nrecs = epdesc.dtepd_nrecs; 15934 epdesc.dtepd_nrecs = 0; 15935 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15936 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15937 continue; 15938 15939 epdesc.dtepd_nrecs++; 15940 } 15941 15942 /* 15943 * Now that we have the size, we need to allocate a temporary 15944 * buffer in which to store the complete description. We need 15945 * the temporary buffer to be able to drop dtrace_lock() 15946 * across the copyout(), below. 15947 */ 15948 size = sizeof (dtrace_eprobedesc_t) + 15949 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 15950 15951 buf = kmem_alloc(size, KM_SLEEP); 15952 dest = (uintptr_t)buf; 15953 15954 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 15955 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 15956 15957 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15958 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15959 continue; 15960 15961 if (nrecs-- == 0) 15962 break; 15963 15964 bcopy(&act->dta_rec, (void *)dest, 15965 sizeof (dtrace_recdesc_t)); 15966 dest += sizeof (dtrace_recdesc_t); 15967 } 15968 15969 mutex_exit(&dtrace_lock); 15970 15971 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15972 kmem_free(buf, size); 15973 return (EFAULT); 15974 } 15975 15976 kmem_free(buf, size); 15977 return (0); 15978 } 15979 15980 case DTRACEIOC_AGGDESC: { 15981 dtrace_aggdesc_t aggdesc; 15982 dtrace_action_t *act; 15983 dtrace_aggregation_t *agg; 15984 int nrecs; 15985 uint32_t offs; 15986 dtrace_recdesc_t *lrec; 15987 void *buf; 15988 size_t size; 15989 uintptr_t dest; 15990 15991 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 15992 return (EFAULT); 15993 15994 mutex_enter(&dtrace_lock); 15995 15996 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 15997 mutex_exit(&dtrace_lock); 15998 return (EINVAL); 15999 } 16000 16001 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 16002 16003 nrecs = aggdesc.dtagd_nrecs; 16004 aggdesc.dtagd_nrecs = 0; 16005 16006 offs = agg->dtag_base; 16007 lrec = &agg->dtag_action.dta_rec; 16008 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 16009 16010 for (act = agg->dtag_first; ; act = act->dta_next) { 16011 ASSERT(act->dta_intuple || 16012 DTRACEACT_ISAGG(act->dta_kind)); 16013 16014 /* 16015 * If this action has a record size of zero, it 16016 * denotes an argument to the aggregating action. 16017 * Because the presence of this record doesn't (or 16018 * shouldn't) affect the way the data is interpreted, 16019 * we don't copy it out to save user-level the 16020 * confusion of dealing with a zero-length record. 16021 */ 16022 if (act->dta_rec.dtrd_size == 0) { 16023 ASSERT(agg->dtag_hasarg); 16024 continue; 16025 } 16026 16027 aggdesc.dtagd_nrecs++; 16028 16029 if (act == &agg->dtag_action) 16030 break; 16031 } 16032 16033 /* 16034 * Now that we have the size, we need to allocate a temporary 16035 * buffer in which to store the complete description. We need 16036 * the temporary buffer to be able to drop dtrace_lock() 16037 * across the copyout(), below. 16038 */ 16039 size = sizeof (dtrace_aggdesc_t) + 16040 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 16041 16042 buf = kmem_alloc(size, KM_SLEEP); 16043 dest = (uintptr_t)buf; 16044 16045 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 16046 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 16047 16048 for (act = agg->dtag_first; ; act = act->dta_next) { 16049 dtrace_recdesc_t rec = act->dta_rec; 16050 16051 /* 16052 * See the comment in the above loop for why we pass 16053 * over zero-length records. 16054 */ 16055 if (rec.dtrd_size == 0) { 16056 ASSERT(agg->dtag_hasarg); 16057 continue; 16058 } 16059 16060 if (nrecs-- == 0) 16061 break; 16062 16063 rec.dtrd_offset -= offs; 16064 bcopy(&rec, (void *)dest, sizeof (rec)); 16065 dest += sizeof (dtrace_recdesc_t); 16066 16067 if (act == &agg->dtag_action) 16068 break; 16069 } 16070 16071 mutex_exit(&dtrace_lock); 16072 16073 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 16074 kmem_free(buf, size); 16075 return (EFAULT); 16076 } 16077 16078 kmem_free(buf, size); 16079 return (0); 16080 } 16081 16082 case DTRACEIOC_ENABLE: { 16083 dof_hdr_t *dof; 16084 dtrace_enabling_t *enab = NULL; 16085 dtrace_vstate_t *vstate; 16086 int err = 0; 16087 16088 *rv = 0; 16089 16090 /* 16091 * If a NULL argument has been passed, we take this as our 16092 * cue to reevaluate our enablings. 16093 */ 16094 if (arg == NULL) { 16095 dtrace_enabling_matchall(); 16096 16097 return (0); 16098 } 16099 16100 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 16101 return (rval); 16102 16103 mutex_enter(&cpu_lock); 16104 mutex_enter(&dtrace_lock); 16105 vstate = &state->dts_vstate; 16106 16107 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 16108 mutex_exit(&dtrace_lock); 16109 mutex_exit(&cpu_lock); 16110 dtrace_dof_destroy(dof); 16111 return (EBUSY); 16112 } 16113 16114 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 16115 mutex_exit(&dtrace_lock); 16116 mutex_exit(&cpu_lock); 16117 dtrace_dof_destroy(dof); 16118 return (EINVAL); 16119 } 16120 16121 if ((rval = dtrace_dof_options(dof, state)) != 0) { 16122 dtrace_enabling_destroy(enab); 16123 mutex_exit(&dtrace_lock); 16124 mutex_exit(&cpu_lock); 16125 dtrace_dof_destroy(dof); 16126 return (rval); 16127 } 16128 16129 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 16130 err = dtrace_enabling_retain(enab); 16131 } else { 16132 dtrace_enabling_destroy(enab); 16133 } 16134 16135 mutex_exit(&cpu_lock); 16136 mutex_exit(&dtrace_lock); 16137 dtrace_dof_destroy(dof); 16138 16139 return (err); 16140 } 16141 16142 case DTRACEIOC_REPLICATE: { 16143 dtrace_repldesc_t desc; 16144 dtrace_probedesc_t *match = &desc.dtrpd_match; 16145 dtrace_probedesc_t *create = &desc.dtrpd_create; 16146 int err; 16147 16148 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16149 return (EFAULT); 16150 16151 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 16152 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 16153 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 16154 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 16155 16156 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 16157 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 16158 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 16159 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 16160 16161 mutex_enter(&dtrace_lock); 16162 err = dtrace_enabling_replicate(state, match, create); 16163 mutex_exit(&dtrace_lock); 16164 16165 return (err); 16166 } 16167 16168 case DTRACEIOC_PROBEMATCH: 16169 case DTRACEIOC_PROBES: { 16170 dtrace_probe_t *probe = NULL; 16171 dtrace_probedesc_t desc; 16172 dtrace_probekey_t pkey; 16173 dtrace_id_t i; 16174 int m = 0; 16175 uint32_t priv; 16176 uid_t uid; 16177 zoneid_t zoneid; 16178 16179 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16180 return (EFAULT); 16181 16182 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 16183 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 16184 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 16185 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 16186 16187 /* 16188 * Before we attempt to match this probe, we want to give 16189 * all providers the opportunity to provide it. 16190 */ 16191 if (desc.dtpd_id == DTRACE_IDNONE) { 16192 mutex_enter(&dtrace_provider_lock); 16193 dtrace_probe_provide(&desc, NULL); 16194 mutex_exit(&dtrace_provider_lock); 16195 desc.dtpd_id++; 16196 } 16197 16198 if (cmd == DTRACEIOC_PROBEMATCH) { 16199 dtrace_probekey(&desc, &pkey); 16200 pkey.dtpk_id = DTRACE_IDNONE; 16201 } 16202 16203 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 16204 16205 mutex_enter(&dtrace_lock); 16206 16207 if (cmd == DTRACEIOC_PROBEMATCH) { 16208 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 16209 if ((probe = dtrace_probes[i - 1]) != NULL && 16210 (m = dtrace_match_probe(probe, &pkey, 16211 priv, uid, zoneid)) != 0) 16212 break; 16213 } 16214 16215 if (m < 0) { 16216 mutex_exit(&dtrace_lock); 16217 return (EINVAL); 16218 } 16219 16220 } else { 16221 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 16222 if ((probe = dtrace_probes[i - 1]) != NULL && 16223 dtrace_match_priv(probe, priv, uid, zoneid)) 16224 break; 16225 } 16226 } 16227 16228 if (probe == NULL) { 16229 mutex_exit(&dtrace_lock); 16230 return (ESRCH); 16231 } 16232 16233 dtrace_probe_description(probe, &desc); 16234 mutex_exit(&dtrace_lock); 16235 16236 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16237 return (EFAULT); 16238 16239 return (0); 16240 } 16241 16242 case DTRACEIOC_PROBEARG: { 16243 dtrace_argdesc_t desc; 16244 dtrace_probe_t *probe; 16245 dtrace_provider_t *prov; 16246 16247 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16248 return (EFAULT); 16249 16250 if (desc.dtargd_id == DTRACE_IDNONE) 16251 return (EINVAL); 16252 16253 if (desc.dtargd_ndx == DTRACE_ARGNONE) 16254 return (EINVAL); 16255 16256 mutex_enter(&dtrace_provider_lock); 16257 mutex_enter(&mod_lock); 16258 mutex_enter(&dtrace_lock); 16259 16260 if (desc.dtargd_id > dtrace_nprobes) { 16261 mutex_exit(&dtrace_lock); 16262 mutex_exit(&mod_lock); 16263 mutex_exit(&dtrace_provider_lock); 16264 return (EINVAL); 16265 } 16266 16267 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 16268 mutex_exit(&dtrace_lock); 16269 mutex_exit(&mod_lock); 16270 mutex_exit(&dtrace_provider_lock); 16271 return (EINVAL); 16272 } 16273 16274 mutex_exit(&dtrace_lock); 16275 16276 prov = probe->dtpr_provider; 16277 16278 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 16279 /* 16280 * There isn't any typed information for this probe. 16281 * Set the argument number to DTRACE_ARGNONE. 16282 */ 16283 desc.dtargd_ndx = DTRACE_ARGNONE; 16284 } else { 16285 desc.dtargd_native[0] = '\0'; 16286 desc.dtargd_xlate[0] = '\0'; 16287 desc.dtargd_mapping = desc.dtargd_ndx; 16288 16289 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 16290 probe->dtpr_id, probe->dtpr_arg, &desc); 16291 } 16292 16293 mutex_exit(&mod_lock); 16294 mutex_exit(&dtrace_provider_lock); 16295 16296 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16297 return (EFAULT); 16298 16299 return (0); 16300 } 16301 16302 case DTRACEIOC_GO: { 16303 processorid_t cpuid; 16304 rval = dtrace_state_go(state, &cpuid); 16305 16306 if (rval != 0) 16307 return (rval); 16308 16309 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 16310 return (EFAULT); 16311 16312 return (0); 16313 } 16314 16315 case DTRACEIOC_STOP: { 16316 processorid_t cpuid; 16317 16318 mutex_enter(&dtrace_lock); 16319 rval = dtrace_state_stop(state, &cpuid); 16320 mutex_exit(&dtrace_lock); 16321 16322 if (rval != 0) 16323 return (rval); 16324 16325 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 16326 return (EFAULT); 16327 16328 return (0); 16329 } 16330 16331 case DTRACEIOC_DOFGET: { 16332 dof_hdr_t hdr, *dof; 16333 uint64_t len; 16334 16335 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 16336 return (EFAULT); 16337 16338 mutex_enter(&dtrace_lock); 16339 dof = dtrace_dof_create(state); 16340 mutex_exit(&dtrace_lock); 16341 16342 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 16343 rval = copyout(dof, (void *)arg, len); 16344 dtrace_dof_destroy(dof); 16345 16346 return (rval == 0 ? 0 : EFAULT); 16347 } 16348 16349 case DTRACEIOC_AGGSNAP: 16350 case DTRACEIOC_BUFSNAP: { 16351 dtrace_bufdesc_t desc; 16352 caddr_t cached; 16353 dtrace_buffer_t *buf; 16354 16355 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16356 return (EFAULT); 16357 16358 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 16359 return (EINVAL); 16360 16361 mutex_enter(&dtrace_lock); 16362 16363 if (cmd == DTRACEIOC_BUFSNAP) { 16364 buf = &state->dts_buffer[desc.dtbd_cpu]; 16365 } else { 16366 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 16367 } 16368 16369 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 16370 size_t sz = buf->dtb_offset; 16371 16372 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 16373 mutex_exit(&dtrace_lock); 16374 return (EBUSY); 16375 } 16376 16377 /* 16378 * If this buffer has already been consumed, we're 16379 * going to indicate that there's nothing left here 16380 * to consume. 16381 */ 16382 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 16383 mutex_exit(&dtrace_lock); 16384 16385 desc.dtbd_size = 0; 16386 desc.dtbd_drops = 0; 16387 desc.dtbd_errors = 0; 16388 desc.dtbd_oldest = 0; 16389 sz = sizeof (desc); 16390 16391 if (copyout(&desc, (void *)arg, sz) != 0) 16392 return (EFAULT); 16393 16394 return (0); 16395 } 16396 16397 /* 16398 * If this is a ring buffer that has wrapped, we want 16399 * to copy the whole thing out. 16400 */ 16401 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 16402 dtrace_buffer_polish(buf); 16403 sz = buf->dtb_size; 16404 } 16405 16406 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 16407 mutex_exit(&dtrace_lock); 16408 return (EFAULT); 16409 } 16410 16411 desc.dtbd_size = sz; 16412 desc.dtbd_drops = buf->dtb_drops; 16413 desc.dtbd_errors = buf->dtb_errors; 16414 desc.dtbd_oldest = buf->dtb_xamot_offset; 16415 desc.dtbd_timestamp = dtrace_gethrtime(); 16416 16417 mutex_exit(&dtrace_lock); 16418 16419 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16420 return (EFAULT); 16421 16422 buf->dtb_flags |= DTRACEBUF_CONSUMED; 16423 16424 return (0); 16425 } 16426 16427 if (buf->dtb_tomax == NULL) { 16428 ASSERT(buf->dtb_xamot == NULL); 16429 mutex_exit(&dtrace_lock); 16430 return (ENOENT); 16431 } 16432 16433 cached = buf->dtb_tomax; 16434 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 16435 16436 dtrace_xcall(desc.dtbd_cpu, 16437 (dtrace_xcall_t)dtrace_buffer_switch, buf); 16438 16439 state->dts_errors += buf->dtb_xamot_errors; 16440 16441 /* 16442 * If the buffers did not actually switch, then the cross call 16443 * did not take place -- presumably because the given CPU is 16444 * not in the ready set. If this is the case, we'll return 16445 * ENOENT. 16446 */ 16447 if (buf->dtb_tomax == cached) { 16448 ASSERT(buf->dtb_xamot != cached); 16449 mutex_exit(&dtrace_lock); 16450 return (ENOENT); 16451 } 16452 16453 ASSERT(cached == buf->dtb_xamot); 16454 16455 /* 16456 * We have our snapshot; now copy it out. 16457 */ 16458 if (copyout(buf->dtb_xamot, desc.dtbd_data, 16459 buf->dtb_xamot_offset) != 0) { 16460 mutex_exit(&dtrace_lock); 16461 return (EFAULT); 16462 } 16463 16464 desc.dtbd_size = buf->dtb_xamot_offset; 16465 desc.dtbd_drops = buf->dtb_xamot_drops; 16466 desc.dtbd_errors = buf->dtb_xamot_errors; 16467 desc.dtbd_oldest = 0; 16468 desc.dtbd_timestamp = buf->dtb_switched; 16469 16470 mutex_exit(&dtrace_lock); 16471 16472 /* 16473 * Finally, copy out the buffer description. 16474 */ 16475 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16476 return (EFAULT); 16477 16478 return (0); 16479 } 16480 16481 case DTRACEIOC_CONF: { 16482 dtrace_conf_t conf; 16483 16484 bzero(&conf, sizeof (conf)); 16485 conf.dtc_difversion = DIF_VERSION; 16486 conf.dtc_difintregs = DIF_DIR_NREGS; 16487 conf.dtc_diftupregs = DIF_DTR_NREGS; 16488 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 16489 16490 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 16491 return (EFAULT); 16492 16493 return (0); 16494 } 16495 16496 case DTRACEIOC_STATUS: { 16497 dtrace_status_t stat; 16498 dtrace_dstate_t *dstate; 16499 int i, j; 16500 uint64_t nerrs; 16501 16502 /* 16503 * See the comment in dtrace_state_deadman() for the reason 16504 * for setting dts_laststatus to INT64_MAX before setting 16505 * it to the correct value. 16506 */ 16507 state->dts_laststatus = INT64_MAX; 16508 dtrace_membar_producer(); 16509 state->dts_laststatus = dtrace_gethrtime(); 16510 16511 bzero(&stat, sizeof (stat)); 16512 16513 mutex_enter(&dtrace_lock); 16514 16515 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 16516 mutex_exit(&dtrace_lock); 16517 return (ENOENT); 16518 } 16519 16520 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 16521 stat.dtst_exiting = 1; 16522 16523 nerrs = state->dts_errors; 16524 dstate = &state->dts_vstate.dtvs_dynvars; 16525 16526 for (i = 0; i < NCPU; i++) { 16527 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 16528 16529 stat.dtst_dyndrops += dcpu->dtdsc_drops; 16530 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 16531 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 16532 16533 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 16534 stat.dtst_filled++; 16535 16536 nerrs += state->dts_buffer[i].dtb_errors; 16537 16538 for (j = 0; j < state->dts_nspeculations; j++) { 16539 dtrace_speculation_t *spec; 16540 dtrace_buffer_t *buf; 16541 16542 spec = &state->dts_speculations[j]; 16543 buf = &spec->dtsp_buffer[i]; 16544 stat.dtst_specdrops += buf->dtb_xamot_drops; 16545 } 16546 } 16547 16548 stat.dtst_specdrops_busy = state->dts_speculations_busy; 16549 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 16550 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 16551 stat.dtst_dblerrors = state->dts_dblerrors; 16552 stat.dtst_killed = 16553 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 16554 stat.dtst_errors = nerrs; 16555 16556 mutex_exit(&dtrace_lock); 16557 16558 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 16559 return (EFAULT); 16560 16561 return (0); 16562 } 16563 16564 case DTRACEIOC_FORMAT: { 16565 dtrace_fmtdesc_t fmt; 16566 char *str; 16567 int len; 16568 16569 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 16570 return (EFAULT); 16571 16572 mutex_enter(&dtrace_lock); 16573 16574 if (fmt.dtfd_format == 0 || 16575 fmt.dtfd_format > state->dts_nformats) { 16576 mutex_exit(&dtrace_lock); 16577 return (EINVAL); 16578 } 16579 16580 /* 16581 * Format strings are allocated contiguously and they are 16582 * never freed; if a format index is less than the number 16583 * of formats, we can assert that the format map is non-NULL 16584 * and that the format for the specified index is non-NULL. 16585 */ 16586 ASSERT(state->dts_formats != NULL); 16587 str = state->dts_formats[fmt.dtfd_format - 1]; 16588 ASSERT(str != NULL); 16589 16590 len = strlen(str) + 1; 16591 16592 if (len > fmt.dtfd_length) { 16593 fmt.dtfd_length = len; 16594 16595 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 16596 mutex_exit(&dtrace_lock); 16597 return (EINVAL); 16598 } 16599 } else { 16600 if (copyout(str, fmt.dtfd_string, len) != 0) { 16601 mutex_exit(&dtrace_lock); 16602 return (EINVAL); 16603 } 16604 } 16605 16606 mutex_exit(&dtrace_lock); 16607 return (0); 16608 } 16609 16610 default: 16611 break; 16612 } 16613 16614 return (ENOTTY); 16615 } 16616 16617 /*ARGSUSED*/ 16618 static int 16619 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 16620 { 16621 dtrace_state_t *state; 16622 16623 switch (cmd) { 16624 case DDI_DETACH: 16625 break; 16626 16627 case DDI_SUSPEND: 16628 return (DDI_SUCCESS); 16629 16630 default: 16631 return (DDI_FAILURE); 16632 } 16633 16634 mutex_enter(&cpu_lock); 16635 mutex_enter(&dtrace_provider_lock); 16636 mutex_enter(&dtrace_lock); 16637 16638 ASSERT(dtrace_opens == 0); 16639 16640 if (dtrace_helpers > 0) { 16641 mutex_exit(&dtrace_provider_lock); 16642 mutex_exit(&dtrace_lock); 16643 mutex_exit(&cpu_lock); 16644 return (DDI_FAILURE); 16645 } 16646 16647 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 16648 mutex_exit(&dtrace_provider_lock); 16649 mutex_exit(&dtrace_lock); 16650 mutex_exit(&cpu_lock); 16651 return (DDI_FAILURE); 16652 } 16653 16654 dtrace_provider = NULL; 16655 16656 if ((state = dtrace_anon_grab()) != NULL) { 16657 /* 16658 * If there were ECBs on this state, the provider should 16659 * have not been allowed to detach; assert that there is 16660 * none. 16661 */ 16662 ASSERT(state->dts_necbs == 0); 16663 dtrace_state_destroy(state); 16664 16665 /* 16666 * If we're being detached with anonymous state, we need to 16667 * indicate to the kernel debugger that DTrace is now inactive. 16668 */ 16669 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16670 } 16671 16672 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 16673 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16674 dtrace_cpu_init = NULL; 16675 dtrace_helpers_cleanup = NULL; 16676 dtrace_helpers_fork = NULL; 16677 dtrace_cpustart_init = NULL; 16678 dtrace_cpustart_fini = NULL; 16679 dtrace_debugger_init = NULL; 16680 dtrace_debugger_fini = NULL; 16681 dtrace_modload = NULL; 16682 dtrace_modunload = NULL; 16683 16684 mutex_exit(&cpu_lock); 16685 16686 if (dtrace_helptrace_enabled) { 16687 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 16688 dtrace_helptrace_buffer = NULL; 16689 } 16690 16691 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 16692 dtrace_probes = NULL; 16693 dtrace_nprobes = 0; 16694 16695 dtrace_hash_destroy(dtrace_bymod); 16696 dtrace_hash_destroy(dtrace_byfunc); 16697 dtrace_hash_destroy(dtrace_byname); 16698 dtrace_bymod = NULL; 16699 dtrace_byfunc = NULL; 16700 dtrace_byname = NULL; 16701 16702 kmem_cache_destroy(dtrace_state_cache); 16703 vmem_destroy(dtrace_minor); 16704 vmem_destroy(dtrace_arena); 16705 16706 if (dtrace_toxrange != NULL) { 16707 kmem_free(dtrace_toxrange, 16708 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 16709 dtrace_toxrange = NULL; 16710 dtrace_toxranges = 0; 16711 dtrace_toxranges_max = 0; 16712 } 16713 16714 ddi_remove_minor_node(dtrace_devi, NULL); 16715 dtrace_devi = NULL; 16716 16717 ddi_soft_state_fini(&dtrace_softstate); 16718 16719 ASSERT(dtrace_vtime_references == 0); 16720 ASSERT(dtrace_opens == 0); 16721 ASSERT(dtrace_retained == NULL); 16722 16723 mutex_exit(&dtrace_lock); 16724 mutex_exit(&dtrace_provider_lock); 16725 16726 /* 16727 * We don't destroy the task queue until after we have dropped our 16728 * locks (taskq_destroy() may block on running tasks). To prevent 16729 * attempting to do work after we have effectively detached but before 16730 * the task queue has been destroyed, all tasks dispatched via the 16731 * task queue must check that DTrace is still attached before 16732 * performing any operation. 16733 */ 16734 taskq_destroy(dtrace_taskq); 16735 dtrace_taskq = NULL; 16736 16737 return (DDI_SUCCESS); 16738 } 16739 #endif 16740 16741 #if defined(sun) 16742 /*ARGSUSED*/ 16743 static int 16744 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 16745 { 16746 int error; 16747 16748 switch (infocmd) { 16749 case DDI_INFO_DEVT2DEVINFO: 16750 *result = (void *)dtrace_devi; 16751 error = DDI_SUCCESS; 16752 break; 16753 case DDI_INFO_DEVT2INSTANCE: 16754 *result = (void *)0; 16755 error = DDI_SUCCESS; 16756 break; 16757 default: 16758 error = DDI_FAILURE; 16759 } 16760 return (error); 16761 } 16762 #endif 16763 16764 #if defined(sun) 16765 static struct cb_ops dtrace_cb_ops = { 16766 dtrace_open, /* open */ 16767 dtrace_close, /* close */ 16768 nulldev, /* strategy */ 16769 nulldev, /* print */ 16770 nodev, /* dump */ 16771 nodev, /* read */ 16772 nodev, /* write */ 16773 dtrace_ioctl, /* ioctl */ 16774 nodev, /* devmap */ 16775 nodev, /* mmap */ 16776 nodev, /* segmap */ 16777 nochpoll, /* poll */ 16778 ddi_prop_op, /* cb_prop_op */ 16779 0, /* streamtab */ 16780 D_NEW | D_MP /* Driver compatibility flag */ 16781 }; 16782 16783 static struct dev_ops dtrace_ops = { 16784 DEVO_REV, /* devo_rev */ 16785 0, /* refcnt */ 16786 dtrace_info, /* get_dev_info */ 16787 nulldev, /* identify */ 16788 nulldev, /* probe */ 16789 dtrace_attach, /* attach */ 16790 dtrace_detach, /* detach */ 16791 nodev, /* reset */ 16792 &dtrace_cb_ops, /* driver operations */ 16793 NULL, /* bus operations */ 16794 nodev /* dev power */ 16795 }; 16796 16797 static struct modldrv modldrv = { 16798 &mod_driverops, /* module type (this is a pseudo driver) */ 16799 "Dynamic Tracing", /* name of module */ 16800 &dtrace_ops, /* driver ops */ 16801 }; 16802 16803 static struct modlinkage modlinkage = { 16804 MODREV_1, 16805 (void *)&modldrv, 16806 NULL 16807 }; 16808 16809 int 16810 _init(void) 16811 { 16812 return (mod_install(&modlinkage)); 16813 } 16814 16815 int 16816 _info(struct modinfo *modinfop) 16817 { 16818 return (mod_info(&modlinkage, modinfop)); 16819 } 16820 16821 int 16822 _fini(void) 16823 { 16824 return (mod_remove(&modlinkage)); 16825 } 16826 #else 16827 16828 static d_ioctl_t dtrace_ioctl; 16829 static d_ioctl_t dtrace_ioctl_helper; 16830 static void dtrace_load(void *); 16831 static int dtrace_unload(void); 16832 #if __FreeBSD_version < 800039 16833 static void dtrace_clone(void *, struct ucred *, char *, int , struct cdev **); 16834 static struct clonedevs *dtrace_clones; /* Ptr to the array of cloned devices. */ 16835 static eventhandler_tag eh_tag; /* Event handler tag. */ 16836 #else 16837 static struct cdev *dtrace_dev; 16838 static struct cdev *helper_dev; 16839 #endif 16840 16841 void dtrace_invop_init(void); 16842 void dtrace_invop_uninit(void); 16843 16844 static struct cdevsw dtrace_cdevsw = { 16845 .d_version = D_VERSION, 16846 #if __FreeBSD_version < 800039 16847 .d_flags = D_TRACKCLOSE | D_NEEDMINOR, 16848 .d_close = dtrace_close, 16849 #endif 16850 .d_ioctl = dtrace_ioctl, 16851 .d_open = dtrace_open, 16852 .d_name = "dtrace", 16853 }; 16854 16855 static struct cdevsw helper_cdevsw = { 16856 .d_version = D_VERSION, 16857 .d_ioctl = dtrace_ioctl_helper, 16858 .d_name = "helper", 16859 }; 16860 16861 #include <dtrace_anon.c> 16862 #if __FreeBSD_version < 800039 16863 #include <dtrace_clone.c> 16864 #endif 16865 #include <dtrace_ioctl.c> 16866 #include <dtrace_load.c> 16867 #include <dtrace_modevent.c> 16868 #include <dtrace_sysctl.c> 16869 #include <dtrace_unload.c> 16870 #include <dtrace_vtime.c> 16871 #include <dtrace_hacks.c> 16872 #include <dtrace_isa.c> 16873 16874 SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 16875 SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 16876 SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 16877 16878 DEV_MODULE(dtrace, dtrace_modevent, NULL); 16879 MODULE_VERSION(dtrace, 1); 16880 MODULE_DEPEND(dtrace, cyclic, 1, 1, 1); 16881 MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 16882 #endif 16883