dtrace.c revision 313486
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: stable/10/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c 313486 2017-02-09 22:04:56Z ngie $ 22 */ 23 24/* 25 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 28 */ 29 30/* 31 * DTrace - Dynamic Tracing for Solaris 32 * 33 * This is the implementation of the Solaris Dynamic Tracing framework 34 * (DTrace). The user-visible interface to DTrace is described at length in 35 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 36 * library, the in-kernel DTrace framework, and the DTrace providers are 37 * described in the block comments in the <sys/dtrace.h> header file. The 38 * internal architecture of DTrace is described in the block comments in the 39 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 40 * implementation very much assume mastery of all of these sources; if one has 41 * an unanswered question about the implementation, one should consult them 42 * first. 43 * 44 * The functions here are ordered roughly as follows: 45 * 46 * - Probe context functions 47 * - Probe hashing functions 48 * - Non-probe context utility functions 49 * - Matching functions 50 * - Provider-to-Framework API functions 51 * - Probe management functions 52 * - DIF object functions 53 * - Format functions 54 * - Predicate functions 55 * - ECB functions 56 * - Buffer functions 57 * - Enabling functions 58 * - DOF functions 59 * - Anonymous enabling functions 60 * - Consumer state functions 61 * - Helper functions 62 * - Hook functions 63 * - Driver cookbook functions 64 * 65 * Each group of functions begins with a block comment labelled the "DTrace 66 * [Group] Functions", allowing one to find each block by searching forward 67 * on capital-f functions. 68 */ 69#include <sys/errno.h> 70#ifndef illumos 71#include <sys/time.h> 72#endif 73#include <sys/stat.h> 74#include <sys/modctl.h> 75#include <sys/conf.h> 76#include <sys/systm.h> 77#ifdef illumos 78#include <sys/ddi.h> 79#include <sys/sunddi.h> 80#endif 81#include <sys/cpuvar.h> 82#include <sys/kmem.h> 83#ifdef illumos 84#include <sys/strsubr.h> 85#endif 86#include <sys/sysmacros.h> 87#include <sys/dtrace_impl.h> 88#include <sys/atomic.h> 89#include <sys/cmn_err.h> 90#ifdef illumos 91#include <sys/mutex_impl.h> 92#include <sys/rwlock_impl.h> 93#endif 94#include <sys/ctf_api.h> 95#ifdef illumos 96#include <sys/panic.h> 97#include <sys/priv_impl.h> 98#endif 99#include <sys/policy.h> 100#ifdef illumos 101#include <sys/cred_impl.h> 102#include <sys/procfs_isa.h> 103#endif 104#include <sys/taskq.h> 105#ifdef illumos 106#include <sys/mkdev.h> 107#include <sys/kdi.h> 108#endif 109#include <sys/zone.h> 110#include <sys/socket.h> 111#include <netinet/in.h> 112#include "strtolctype.h" 113 114/* FreeBSD includes: */ 115#ifndef illumos 116#include <sys/callout.h> 117#include <sys/ctype.h> 118#include <sys/eventhandler.h> 119#include <sys/limits.h> 120#include <sys/kdb.h> 121#include <sys/kernel.h> 122#include <sys/malloc.h> 123#include <sys/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 */ 154int dtrace_destructive_disallow = 0; 155dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 156size_t dtrace_difo_maxsize = (256 * 1024); 157dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 158size_t dtrace_statvar_maxsize = (16 * 1024); 159size_t dtrace_actions_max = (16 * 1024); 160size_t dtrace_retain_max = 1024; 161dtrace_optval_t dtrace_helper_actions_max = 128; 162dtrace_optval_t dtrace_helper_providers_max = 32; 163dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 164size_t dtrace_strsize_default = 256; 165dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 166dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 167dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 168dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 169dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 170dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 171dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 172dtrace_optval_t dtrace_nspec_default = 1; 173dtrace_optval_t dtrace_specsize_default = 32 * 1024; 174dtrace_optval_t dtrace_stackframes_default = 20; 175dtrace_optval_t dtrace_ustackframes_default = 20; 176dtrace_optval_t dtrace_jstackframes_default = 50; 177dtrace_optval_t dtrace_jstackstrsize_default = 512; 178int dtrace_msgdsize_max = 128; 179hrtime_t dtrace_chill_max = MSEC2NSEC(500); /* 500 ms */ 180hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 181int dtrace_devdepth_max = 32; 182int dtrace_err_verbose; 183hrtime_t dtrace_deadman_interval = NANOSEC; 184hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 185hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 186hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 187#ifndef illumos 188int dtrace_memstr_max = 4096; 189#endif 190 191/* 192 * DTrace External Variables 193 * 194 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 195 * available to DTrace consumers via the backtick (`) syntax. One of these, 196 * dtrace_zero, is made deliberately so: it is provided as a source of 197 * well-known, zero-filled memory. While this variable is not documented, 198 * it is used by some translators as an implementation detail. 199 */ 200const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 201 202/* 203 * DTrace Internal Variables 204 */ 205#ifdef illumos 206static dev_info_t *dtrace_devi; /* device info */ 207#endif 208#ifdef illumos 209static vmem_t *dtrace_arena; /* probe ID arena */ 210static vmem_t *dtrace_minor; /* minor number arena */ 211#else 212static taskq_t *dtrace_taskq; /* task queue */ 213static struct unrhdr *dtrace_arena; /* Probe ID number. */ 214#endif 215static dtrace_probe_t **dtrace_probes; /* array of all probes */ 216static int dtrace_nprobes; /* number of probes */ 217static dtrace_provider_t *dtrace_provider; /* provider list */ 218static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 219static int dtrace_opens; /* number of opens */ 220static int dtrace_helpers; /* number of helpers */ 221static int dtrace_getf; /* number of unpriv getf()s */ 222#ifdef illumos 223static void *dtrace_softstate; /* softstate pointer */ 224#endif 225static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 226static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 227static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 228static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 229static int dtrace_toxranges; /* number of toxic ranges */ 230static int dtrace_toxranges_max; /* size of toxic range array */ 231static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 232static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 233static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 234static kthread_t *dtrace_panicked; /* panicking thread */ 235static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 236static dtrace_genid_t dtrace_probegen; /* current probe generation */ 237static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 238static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 239static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 240static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 241static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 242#ifndef illumos 243static struct mtx dtrace_unr_mtx; 244MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 245int dtrace_in_probe; /* non-zero if executing a probe */ 246#if defined(__i386__) || defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 247uintptr_t dtrace_in_probe_addr; /* Address of invop when already in probe */ 248#endif 249static eventhandler_tag dtrace_kld_load_tag; 250static eventhandler_tag dtrace_kld_unload_try_tag; 251#endif 252 253/* 254 * DTrace Locking 255 * DTrace is protected by three (relatively coarse-grained) locks: 256 * 257 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 258 * including enabling state, probes, ECBs, consumer state, helper state, 259 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 260 * probe context is lock-free -- synchronization is handled via the 261 * dtrace_sync() cross call mechanism. 262 * 263 * (2) dtrace_provider_lock is required when manipulating provider state, or 264 * when provider state must be held constant. 265 * 266 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 267 * when meta provider state must be held constant. 268 * 269 * The lock ordering between these three locks is dtrace_meta_lock before 270 * dtrace_provider_lock before dtrace_lock. (In particular, there are 271 * several places where dtrace_provider_lock is held by the framework as it 272 * calls into the providers -- which then call back into the framework, 273 * grabbing dtrace_lock.) 274 * 275 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 276 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 277 * role as a coarse-grained lock; it is acquired before both of these locks. 278 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 279 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 280 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 281 * acquired _between_ dtrace_provider_lock and dtrace_lock. 282 */ 283static kmutex_t dtrace_lock; /* probe state lock */ 284static kmutex_t dtrace_provider_lock; /* provider state lock */ 285static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 286 287#ifndef illumos 288/* XXX FreeBSD hacks. */ 289#define cr_suid cr_svuid 290#define cr_sgid cr_svgid 291#define ipaddr_t in_addr_t 292#define mod_modname pathname 293#define vuprintf vprintf 294#define ttoproc(_a) ((_a)->td_proc) 295#define crgetzoneid(_a) 0 296#define NCPU MAXCPU 297#define SNOCD 0 298#define CPU_ON_INTR(_a) 0 299 300#define PRIV_EFFECTIVE (1 << 0) 301#define PRIV_DTRACE_KERNEL (1 << 1) 302#define PRIV_DTRACE_PROC (1 << 2) 303#define PRIV_DTRACE_USER (1 << 3) 304#define PRIV_PROC_OWNER (1 << 4) 305#define PRIV_PROC_ZONE (1 << 5) 306#define PRIV_ALL ~0 307 308SYSCTL_DECL(_debug_dtrace); 309SYSCTL_DECL(_kern_dtrace); 310#endif 311 312#ifdef illumos 313#define curcpu CPU->cpu_id 314#endif 315 316 317/* 318 * DTrace Provider Variables 319 * 320 * These are the variables relating to DTrace as a provider (that is, the 321 * provider of the BEGIN, END, and ERROR probes). 322 */ 323static dtrace_pattr_t dtrace_provider_attr = { 324{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 325{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 326{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 327{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 328{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 329}; 330 331static void 332dtrace_nullop(void) 333{} 334 335static dtrace_pops_t dtrace_provider_ops = { 336 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 337 (void (*)(void *, modctl_t *))dtrace_nullop, 338 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 339 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 340 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 341 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 342 NULL, 343 NULL, 344 NULL, 345 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 346}; 347 348static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 349static dtrace_id_t dtrace_probeid_end; /* special END probe */ 350dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 351 352/* 353 * DTrace Helper Tracing Variables 354 * 355 * These variables should be set dynamically to enable helper tracing. The 356 * only variables that should be set are dtrace_helptrace_enable (which should 357 * be set to a non-zero value to allocate helper tracing buffers on the next 358 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a 359 * non-zero value to deallocate helper tracing buffers on the next close of 360 * /dev/dtrace). When (and only when) helper tracing is disabled, the 361 * buffer size may also be set via dtrace_helptrace_bufsize. 362 */ 363int dtrace_helptrace_enable = 0; 364int dtrace_helptrace_disable = 0; 365int dtrace_helptrace_bufsize = 16 * 1024 * 1024; 366uint32_t dtrace_helptrace_nlocals; 367static dtrace_helptrace_t *dtrace_helptrace_buffer; 368static uint32_t dtrace_helptrace_next = 0; 369static int dtrace_helptrace_wrapped = 0; 370 371/* 372 * DTrace Error Hashing 373 * 374 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 375 * table. This is very useful for checking coverage of tests that are 376 * expected to induce DIF or DOF processing errors, and may be useful for 377 * debugging problems in the DIF code generator or in DOF generation . The 378 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 379 */ 380#ifdef DEBUG 381static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 382static const char *dtrace_errlast; 383static kthread_t *dtrace_errthread; 384static kmutex_t dtrace_errlock; 385#endif 386 387/* 388 * DTrace Macros and Constants 389 * 390 * These are various macros that are useful in various spots in the 391 * implementation, along with a few random constants that have no meaning 392 * outside of the implementation. There is no real structure to this cpp 393 * mishmash -- but is there ever? 394 */ 395#define DTRACE_HASHSTR(hash, probe) \ 396 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 397 398#define DTRACE_HASHNEXT(hash, probe) \ 399 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 400 401#define DTRACE_HASHPREV(hash, probe) \ 402 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 403 404#define DTRACE_HASHEQ(hash, lhs, rhs) \ 405 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 406 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 407 408#define DTRACE_AGGHASHSIZE_SLEW 17 409 410#define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 411 412/* 413 * The key for a thread-local variable consists of the lower 61 bits of the 414 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 415 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 416 * equal to a variable identifier. This is necessary (but not sufficient) to 417 * assure that global associative arrays never collide with thread-local 418 * variables. To guarantee that they cannot collide, we must also define the 419 * order for keying dynamic variables. That order is: 420 * 421 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 422 * 423 * Because the variable-key and the tls-key are in orthogonal spaces, there is 424 * no way for a global variable key signature to match a thread-local key 425 * signature. 426 */ 427#ifdef illumos 428#define DTRACE_TLS_THRKEY(where) { \ 429 uint_t intr = 0; \ 430 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 431 for (; actv; actv >>= 1) \ 432 intr++; \ 433 ASSERT(intr < (1 << 3)); \ 434 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 435 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 436} 437#else 438#define DTRACE_TLS_THRKEY(where) { \ 439 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 440 uint_t intr = 0; \ 441 uint_t actv = _c->cpu_intr_actv; \ 442 for (; actv; actv >>= 1) \ 443 intr++; \ 444 ASSERT(intr < (1 << 3)); \ 445 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 446 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 447} 448#endif 449 450#define DT_BSWAP_8(x) ((x) & 0xff) 451#define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 452#define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 453#define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 454 455#define DT_MASK_LO 0x00000000FFFFFFFFULL 456 457#define DTRACE_STORE(type, tomax, offset, what) \ 458 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 459 460#ifndef __x86 461#define DTRACE_ALIGNCHECK(addr, size, flags) \ 462 if (addr & (size - 1)) { \ 463 *flags |= CPU_DTRACE_BADALIGN; \ 464 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 465 return (0); \ 466 } 467#else 468#define DTRACE_ALIGNCHECK(addr, size, flags) 469#endif 470 471/* 472 * Test whether a range of memory starting at testaddr of size testsz falls 473 * within the range of memory described by addr, sz. We take care to avoid 474 * problems with overflow and underflow of the unsigned quantities, and 475 * disallow all negative sizes. Ranges of size 0 are allowed. 476 */ 477#define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 478 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 479 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 480 (testaddr) + (testsz) >= (testaddr)) 481 482/* 483 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 484 * alloc_sz on the righthand side of the comparison in order to avoid overflow 485 * or underflow in the comparison with it. This is simpler than the INRANGE 486 * check above, because we know that the dtms_scratch_ptr is valid in the 487 * range. Allocations of size zero are allowed. 488 */ 489#define DTRACE_INSCRATCH(mstate, alloc_sz) \ 490 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 491 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 492 493#define DTRACE_LOADFUNC(bits) \ 494/*CSTYLED*/ \ 495uint##bits##_t \ 496dtrace_load##bits(uintptr_t addr) \ 497{ \ 498 size_t size = bits / NBBY; \ 499 /*CSTYLED*/ \ 500 uint##bits##_t rval; \ 501 int i; \ 502 volatile uint16_t *flags = (volatile uint16_t *) \ 503 &cpu_core[curcpu].cpuc_dtrace_flags; \ 504 \ 505 DTRACE_ALIGNCHECK(addr, size, flags); \ 506 \ 507 for (i = 0; i < dtrace_toxranges; i++) { \ 508 if (addr >= dtrace_toxrange[i].dtt_limit) \ 509 continue; \ 510 \ 511 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 512 continue; \ 513 \ 514 /* \ 515 * This address falls within a toxic region; return 0. \ 516 */ \ 517 *flags |= CPU_DTRACE_BADADDR; \ 518 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 519 return (0); \ 520 } \ 521 \ 522 *flags |= CPU_DTRACE_NOFAULT; \ 523 /*CSTYLED*/ \ 524 rval = *((volatile uint##bits##_t *)addr); \ 525 *flags &= ~CPU_DTRACE_NOFAULT; \ 526 \ 527 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 528} 529 530#ifdef _LP64 531#define dtrace_loadptr dtrace_load64 532#else 533#define dtrace_loadptr dtrace_load32 534#endif 535 536#define DTRACE_DYNHASH_FREE 0 537#define DTRACE_DYNHASH_SINK 1 538#define DTRACE_DYNHASH_VALID 2 539 540#define DTRACE_MATCH_NEXT 0 541#define DTRACE_MATCH_DONE 1 542#define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 543#define DTRACE_STATE_ALIGN 64 544 545#define DTRACE_FLAGS2FLT(flags) \ 546 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 547 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 548 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 549 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 550 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 551 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 552 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 553 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 554 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 555 DTRACEFLT_UNKNOWN) 556 557#define DTRACEACT_ISSTRING(act) \ 558 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 559 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 560 561/* Function prototype definitions: */ 562static size_t dtrace_strlen(const char *, size_t); 563static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 564static void dtrace_enabling_provide(dtrace_provider_t *); 565static int dtrace_enabling_match(dtrace_enabling_t *, int *); 566static void dtrace_enabling_matchall(void); 567static void dtrace_enabling_reap(void); 568static dtrace_state_t *dtrace_anon_grab(void); 569static uint64_t dtrace_helper(int, dtrace_mstate_t *, 570 dtrace_state_t *, uint64_t, uint64_t); 571static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 572static void dtrace_buffer_drop(dtrace_buffer_t *); 573static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 574static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 575 dtrace_state_t *, dtrace_mstate_t *); 576static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 577 dtrace_optval_t); 578static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 579static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 580uint16_t dtrace_load16(uintptr_t); 581uint32_t dtrace_load32(uintptr_t); 582uint64_t dtrace_load64(uintptr_t); 583uint8_t dtrace_load8(uintptr_t); 584void dtrace_dynvar_clean(dtrace_dstate_t *); 585dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 586 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 587uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 588static int dtrace_priv_proc(dtrace_state_t *); 589static void dtrace_getf_barrier(void); 590 591/* 592 * DTrace Probe Context Functions 593 * 594 * These functions are called from probe context. Because probe context is 595 * any context in which C may be called, arbitrarily locks may be held, 596 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 597 * As a result, functions called from probe context may only call other DTrace 598 * support functions -- they may not interact at all with the system at large. 599 * (Note that the ASSERT macro is made probe-context safe by redefining it in 600 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 601 * loads are to be performed from probe context, they _must_ be in terms of 602 * the safe dtrace_load*() variants. 603 * 604 * Some functions in this block are not actually called from probe context; 605 * for these functions, there will be a comment above the function reading 606 * "Note: not called from probe context." 607 */ 608void 609dtrace_panic(const char *format, ...) 610{ 611 va_list alist; 612 613 va_start(alist, format); 614#ifdef __FreeBSD__ 615 vpanic(format, alist); 616#else 617 dtrace_vpanic(format, alist); 618#endif 619 va_end(alist); 620} 621 622int 623dtrace_assfail(const char *a, const char *f, int l) 624{ 625 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 626 627 /* 628 * We just need something here that even the most clever compiler 629 * cannot optimize away. 630 */ 631 return (a[(uintptr_t)f]); 632} 633 634/* 635 * Atomically increment a specified error counter from probe context. 636 */ 637static void 638dtrace_error(uint32_t *counter) 639{ 640 /* 641 * Most counters stored to in probe context are per-CPU counters. 642 * However, there are some error conditions that are sufficiently 643 * arcane that they don't merit per-CPU storage. If these counters 644 * are incremented concurrently on different CPUs, scalability will be 645 * adversely affected -- but we don't expect them to be white-hot in a 646 * correctly constructed enabling... 647 */ 648 uint32_t oval, nval; 649 650 do { 651 oval = *counter; 652 653 if ((nval = oval + 1) == 0) { 654 /* 655 * If the counter would wrap, set it to 1 -- assuring 656 * that the counter is never zero when we have seen 657 * errors. (The counter must be 32-bits because we 658 * aren't guaranteed a 64-bit compare&swap operation.) 659 * To save this code both the infamy of being fingered 660 * by a priggish news story and the indignity of being 661 * the target of a neo-puritan witch trial, we're 662 * carefully avoiding any colorful description of the 663 * likelihood of this condition -- but suffice it to 664 * say that it is only slightly more likely than the 665 * overflow of predicate cache IDs, as discussed in 666 * dtrace_predicate_create(). 667 */ 668 nval = 1; 669 } 670 } while (dtrace_cas32(counter, oval, nval) != oval); 671} 672 673/* 674 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 675 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 676 */ 677DTRACE_LOADFUNC(8) 678DTRACE_LOADFUNC(16) 679DTRACE_LOADFUNC(32) 680DTRACE_LOADFUNC(64) 681 682static int 683dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 684{ 685 if (dest < mstate->dtms_scratch_base) 686 return (0); 687 688 if (dest + size < dest) 689 return (0); 690 691 if (dest + size > mstate->dtms_scratch_ptr) 692 return (0); 693 694 return (1); 695} 696 697static int 698dtrace_canstore_statvar(uint64_t addr, size_t sz, 699 dtrace_statvar_t **svars, int nsvars) 700{ 701 int i; 702 size_t maxglobalsize, maxlocalsize; 703 704 if (nsvars == 0) 705 return (0); 706 707 maxglobalsize = dtrace_statvar_maxsize; 708 maxlocalsize = (maxglobalsize + sizeof (uint64_t)) * NCPU; 709 710 for (i = 0; i < nsvars; i++) { 711 dtrace_statvar_t *svar = svars[i]; 712 uint8_t scope; 713 size_t size; 714 715 if (svar == NULL || (size = svar->dtsv_size) == 0) 716 continue; 717 718 scope = svar->dtsv_var.dtdv_scope; 719 720 /* 721 * We verify that our size is valid in the spirit of providing 722 * defense in depth: we want to prevent attackers from using 723 * DTrace to escalate an orthogonal kernel heap corruption bug 724 * into the ability to store to arbitrary locations in memory. 725 */ 726 VERIFY((scope == DIFV_SCOPE_GLOBAL && size < maxglobalsize) || 727 (scope == DIFV_SCOPE_LOCAL && size < maxlocalsize)); 728 729 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 730 return (1); 731 } 732 733 return (0); 734} 735 736/* 737 * Check to see if the address is within a memory region to which a store may 738 * be issued. This includes the DTrace scratch areas, and any DTrace variable 739 * region. The caller of dtrace_canstore() is responsible for performing any 740 * alignment checks that are needed before stores are actually executed. 741 */ 742static int 743dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 744 dtrace_vstate_t *vstate) 745{ 746 /* 747 * First, check to see if the address is in scratch space... 748 */ 749 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 750 mstate->dtms_scratch_size)) 751 return (1); 752 753 /* 754 * Now check to see if it's a dynamic variable. This check will pick 755 * up both thread-local variables and any global dynamically-allocated 756 * variables. 757 */ 758 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 759 vstate->dtvs_dynvars.dtds_size)) { 760 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 761 uintptr_t base = (uintptr_t)dstate->dtds_base + 762 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 763 uintptr_t chunkoffs; 764 765 /* 766 * Before we assume that we can store here, we need to make 767 * sure that it isn't in our metadata -- storing to our 768 * dynamic variable metadata would corrupt our state. For 769 * the range to not include any dynamic variable metadata, 770 * it must: 771 * 772 * (1) Start above the hash table that is at the base of 773 * the dynamic variable space 774 * 775 * (2) Have a starting chunk offset that is beyond the 776 * dtrace_dynvar_t that is at the base of every chunk 777 * 778 * (3) Not span a chunk boundary 779 * 780 */ 781 if (addr < base) 782 return (0); 783 784 chunkoffs = (addr - base) % dstate->dtds_chunksize; 785 786 if (chunkoffs < sizeof (dtrace_dynvar_t)) 787 return (0); 788 789 if (chunkoffs + sz > dstate->dtds_chunksize) 790 return (0); 791 792 return (1); 793 } 794 795 /* 796 * Finally, check the static local and global variables. These checks 797 * take the longest, so we perform them last. 798 */ 799 if (dtrace_canstore_statvar(addr, sz, 800 vstate->dtvs_locals, vstate->dtvs_nlocals)) 801 return (1); 802 803 if (dtrace_canstore_statvar(addr, sz, 804 vstate->dtvs_globals, vstate->dtvs_nglobals)) 805 return (1); 806 807 return (0); 808} 809 810 811/* 812 * Convenience routine to check to see if the address is within a memory 813 * region in which a load may be issued given the user's privilege level; 814 * if not, it sets the appropriate error flags and loads 'addr' into the 815 * illegal value slot. 816 * 817 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 818 * appropriate memory access protection. 819 */ 820static int 821dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 822 dtrace_vstate_t *vstate) 823{ 824 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 825 file_t *fp; 826 827 /* 828 * If we hold the privilege to read from kernel memory, then 829 * everything is readable. 830 */ 831 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 832 return (1); 833 834 /* 835 * You can obviously read that which you can store. 836 */ 837 if (dtrace_canstore(addr, sz, mstate, vstate)) 838 return (1); 839 840 /* 841 * We're allowed to read from our own string table. 842 */ 843 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 844 mstate->dtms_difo->dtdo_strlen)) 845 return (1); 846 847 if (vstate->dtvs_state != NULL && 848 dtrace_priv_proc(vstate->dtvs_state)) { 849 proc_t *p; 850 851 /* 852 * When we have privileges to the current process, there are 853 * several context-related kernel structures that are safe to 854 * read, even absent the privilege to read from kernel memory. 855 * These reads are safe because these structures contain only 856 * state that (1) we're permitted to read, (2) is harmless or 857 * (3) contains pointers to additional kernel state that we're 858 * not permitted to read (and as such, do not present an 859 * opportunity for privilege escalation). Finally (and 860 * critically), because of the nature of their relation with 861 * the current thread context, the memory associated with these 862 * structures cannot change over the duration of probe context, 863 * and it is therefore impossible for this memory to be 864 * deallocated and reallocated as something else while it's 865 * being operated upon. 866 */ 867 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) 868 return (1); 869 870 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 871 sz, curthread->t_procp, sizeof (proc_t))) { 872 return (1); 873 } 874 875 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 876 curthread->t_cred, sizeof (cred_t))) { 877 return (1); 878 } 879 880#ifdef illumos 881 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 882 &(p->p_pidp->pid_id), sizeof (pid_t))) { 883 return (1); 884 } 885 886 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 887 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 888 return (1); 889 } 890#endif 891 } 892 893 if ((fp = mstate->dtms_getf) != NULL) { 894 uintptr_t psz = sizeof (void *); 895 vnode_t *vp; 896 vnodeops_t *op; 897 898 /* 899 * When getf() returns a file_t, the enabling is implicitly 900 * granted the (transient) right to read the returned file_t 901 * as well as the v_path and v_op->vnop_name of the underlying 902 * vnode. These accesses are allowed after a successful 903 * getf() because the members that they refer to cannot change 904 * once set -- and the barrier logic in the kernel's closef() 905 * path assures that the file_t and its referenced vode_t 906 * cannot themselves be stale (that is, it impossible for 907 * either dtms_getf itself or its f_vnode member to reference 908 * freed memory). 909 */ 910 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) 911 return (1); 912 913 if ((vp = fp->f_vnode) != NULL) { 914#ifdef illumos 915 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) 916 return (1); 917 if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz, 918 vp->v_path, strlen(vp->v_path) + 1)) { 919 return (1); 920 } 921#endif 922 923 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) 924 return (1); 925 926#ifdef illumos 927 if ((op = vp->v_op) != NULL && 928 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 929 return (1); 930 } 931 932 if (op != NULL && op->vnop_name != NULL && 933 DTRACE_INRANGE(addr, sz, op->vnop_name, 934 strlen(op->vnop_name) + 1)) { 935 return (1); 936 } 937#endif 938 } 939 } 940 941 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 942 *illval = addr; 943 return (0); 944} 945 946/* 947 * Convenience routine to check to see if a given string is within a memory 948 * region in which a load may be issued given the user's privilege level; 949 * this exists so that we don't need to issue unnecessary dtrace_strlen() 950 * calls in the event that the user has all privileges. 951 */ 952static int 953dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 954 dtrace_vstate_t *vstate) 955{ 956 size_t strsz; 957 958 /* 959 * If we hold the privilege to read from kernel memory, then 960 * everything is readable. 961 */ 962 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 963 return (1); 964 965 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 966 if (dtrace_canload(addr, strsz, mstate, vstate)) 967 return (1); 968 969 return (0); 970} 971 972/* 973 * Convenience routine to check to see if a given variable is within a memory 974 * region in which a load may be issued given the user's privilege level. 975 */ 976static int 977dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 978 dtrace_vstate_t *vstate) 979{ 980 size_t sz; 981 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 982 983 /* 984 * If we hold the privilege to read from kernel memory, then 985 * everything is readable. 986 */ 987 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 988 return (1); 989 990 if (type->dtdt_kind == DIF_TYPE_STRING) 991 sz = dtrace_strlen(src, 992 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 993 else 994 sz = type->dtdt_size; 995 996 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 997} 998 999/* 1000 * Convert a string to a signed integer using safe loads. 1001 * 1002 * NOTE: This function uses various macros from strtolctype.h to manipulate 1003 * digit values, etc -- these have all been checked to ensure they make 1004 * no additional function calls. 1005 */ 1006static int64_t 1007dtrace_strtoll(char *input, int base, size_t limit) 1008{ 1009 uintptr_t pos = (uintptr_t)input; 1010 int64_t val = 0; 1011 int x; 1012 boolean_t neg = B_FALSE; 1013 char c, cc, ccc; 1014 uintptr_t end = pos + limit; 1015 1016 /* 1017 * Consume any whitespace preceding digits. 1018 */ 1019 while ((c = dtrace_load8(pos)) == ' ' || c == '\t') 1020 pos++; 1021 1022 /* 1023 * Handle an explicit sign if one is present. 1024 */ 1025 if (c == '-' || c == '+') { 1026 if (c == '-') 1027 neg = B_TRUE; 1028 c = dtrace_load8(++pos); 1029 } 1030 1031 /* 1032 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it 1033 * if present. 1034 */ 1035 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' || 1036 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) { 1037 pos += 2; 1038 c = ccc; 1039 } 1040 1041 /* 1042 * Read in contiguous digits until the first non-digit character. 1043 */ 1044 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base; 1045 c = dtrace_load8(++pos)) 1046 val = val * base + x; 1047 1048 return (neg ? -val : val); 1049} 1050 1051/* 1052 * Compare two strings using safe loads. 1053 */ 1054static int 1055dtrace_strncmp(char *s1, char *s2, size_t limit) 1056{ 1057 uint8_t c1, c2; 1058 volatile uint16_t *flags; 1059 1060 if (s1 == s2 || limit == 0) 1061 return (0); 1062 1063 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1064 1065 do { 1066 if (s1 == NULL) { 1067 c1 = '\0'; 1068 } else { 1069 c1 = dtrace_load8((uintptr_t)s1++); 1070 } 1071 1072 if (s2 == NULL) { 1073 c2 = '\0'; 1074 } else { 1075 c2 = dtrace_load8((uintptr_t)s2++); 1076 } 1077 1078 if (c1 != c2) 1079 return (c1 - c2); 1080 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 1081 1082 return (0); 1083} 1084 1085/* 1086 * Compute strlen(s) for a string using safe memory accesses. The additional 1087 * len parameter is used to specify a maximum length to ensure completion. 1088 */ 1089static size_t 1090dtrace_strlen(const char *s, size_t lim) 1091{ 1092 uint_t len; 1093 1094 for (len = 0; len != lim; len++) { 1095 if (dtrace_load8((uintptr_t)s++) == '\0') 1096 break; 1097 } 1098 1099 return (len); 1100} 1101 1102/* 1103 * Check if an address falls within a toxic region. 1104 */ 1105static int 1106dtrace_istoxic(uintptr_t kaddr, size_t size) 1107{ 1108 uintptr_t taddr, tsize; 1109 int i; 1110 1111 for (i = 0; i < dtrace_toxranges; i++) { 1112 taddr = dtrace_toxrange[i].dtt_base; 1113 tsize = dtrace_toxrange[i].dtt_limit - taddr; 1114 1115 if (kaddr - taddr < tsize) { 1116 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1117 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 1118 return (1); 1119 } 1120 1121 if (taddr - kaddr < size) { 1122 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1123 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 1124 return (1); 1125 } 1126 } 1127 1128 return (0); 1129} 1130 1131/* 1132 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 1133 * memory specified by the DIF program. The dst is assumed to be safe memory 1134 * that we can store to directly because it is managed by DTrace. As with 1135 * standard bcopy, overlapping copies are handled properly. 1136 */ 1137static void 1138dtrace_bcopy(const void *src, void *dst, size_t len) 1139{ 1140 if (len != 0) { 1141 uint8_t *s1 = dst; 1142 const uint8_t *s2 = src; 1143 1144 if (s1 <= s2) { 1145 do { 1146 *s1++ = dtrace_load8((uintptr_t)s2++); 1147 } while (--len != 0); 1148 } else { 1149 s2 += len; 1150 s1 += len; 1151 1152 do { 1153 *--s1 = dtrace_load8((uintptr_t)--s2); 1154 } while (--len != 0); 1155 } 1156 } 1157} 1158 1159/* 1160 * Copy src to dst using safe memory accesses, up to either the specified 1161 * length, or the point that a nul byte is encountered. The src is assumed to 1162 * be unsafe memory specified by the DIF program. The dst is assumed to be 1163 * safe memory that we can store to directly because it is managed by DTrace. 1164 * Unlike dtrace_bcopy(), overlapping regions are not handled. 1165 */ 1166static void 1167dtrace_strcpy(const void *src, void *dst, size_t len) 1168{ 1169 if (len != 0) { 1170 uint8_t *s1 = dst, c; 1171 const uint8_t *s2 = src; 1172 1173 do { 1174 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1175 } while (--len != 0 && c != '\0'); 1176 } 1177} 1178 1179/* 1180 * Copy src to dst, deriving the size and type from the specified (BYREF) 1181 * variable type. The src is assumed to be unsafe memory specified by the DIF 1182 * program. The dst is assumed to be DTrace variable memory that is of the 1183 * specified type; we assume that we can store to directly. 1184 */ 1185static void 1186dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 1187{ 1188 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1189 1190 if (type->dtdt_kind == DIF_TYPE_STRING) { 1191 dtrace_strcpy(src, dst, type->dtdt_size); 1192 } else { 1193 dtrace_bcopy(src, dst, type->dtdt_size); 1194 } 1195} 1196 1197/* 1198 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1199 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1200 * safe memory that we can access directly because it is managed by DTrace. 1201 */ 1202static int 1203dtrace_bcmp(const void *s1, const void *s2, size_t len) 1204{ 1205 volatile uint16_t *flags; 1206 1207 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1208 1209 if (s1 == s2) 1210 return (0); 1211 1212 if (s1 == NULL || s2 == NULL) 1213 return (1); 1214 1215 if (s1 != s2 && len != 0) { 1216 const uint8_t *ps1 = s1; 1217 const uint8_t *ps2 = s2; 1218 1219 do { 1220 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1221 return (1); 1222 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1223 } 1224 return (0); 1225} 1226 1227/* 1228 * Zero the specified region using a simple byte-by-byte loop. Note that this 1229 * is for safe DTrace-managed memory only. 1230 */ 1231static void 1232dtrace_bzero(void *dst, size_t len) 1233{ 1234 uchar_t *cp; 1235 1236 for (cp = dst; len != 0; len--) 1237 *cp++ = 0; 1238} 1239 1240static void 1241dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1242{ 1243 uint64_t result[2]; 1244 1245 result[0] = addend1[0] + addend2[0]; 1246 result[1] = addend1[1] + addend2[1] + 1247 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1248 1249 sum[0] = result[0]; 1250 sum[1] = result[1]; 1251} 1252 1253/* 1254 * Shift the 128-bit value in a by b. If b is positive, shift left. 1255 * If b is negative, shift right. 1256 */ 1257static void 1258dtrace_shift_128(uint64_t *a, int b) 1259{ 1260 uint64_t mask; 1261 1262 if (b == 0) 1263 return; 1264 1265 if (b < 0) { 1266 b = -b; 1267 if (b >= 64) { 1268 a[0] = a[1] >> (b - 64); 1269 a[1] = 0; 1270 } else { 1271 a[0] >>= b; 1272 mask = 1LL << (64 - b); 1273 mask -= 1; 1274 a[0] |= ((a[1] & mask) << (64 - b)); 1275 a[1] >>= b; 1276 } 1277 } else { 1278 if (b >= 64) { 1279 a[1] = a[0] << (b - 64); 1280 a[0] = 0; 1281 } else { 1282 a[1] <<= b; 1283 mask = a[0] >> (64 - b); 1284 a[1] |= mask; 1285 a[0] <<= b; 1286 } 1287 } 1288} 1289 1290/* 1291 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1292 * use native multiplication on those, and then re-combine into the 1293 * resulting 128-bit value. 1294 * 1295 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1296 * hi1 * hi2 << 64 + 1297 * hi1 * lo2 << 32 + 1298 * hi2 * lo1 << 32 + 1299 * lo1 * lo2 1300 */ 1301static void 1302dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1303{ 1304 uint64_t hi1, hi2, lo1, lo2; 1305 uint64_t tmp[2]; 1306 1307 hi1 = factor1 >> 32; 1308 hi2 = factor2 >> 32; 1309 1310 lo1 = factor1 & DT_MASK_LO; 1311 lo2 = factor2 & DT_MASK_LO; 1312 1313 product[0] = lo1 * lo2; 1314 product[1] = hi1 * hi2; 1315 1316 tmp[0] = hi1 * lo2; 1317 tmp[1] = 0; 1318 dtrace_shift_128(tmp, 32); 1319 dtrace_add_128(product, tmp, product); 1320 1321 tmp[0] = hi2 * lo1; 1322 tmp[1] = 0; 1323 dtrace_shift_128(tmp, 32); 1324 dtrace_add_128(product, tmp, product); 1325} 1326 1327/* 1328 * This privilege check should be used by actions and subroutines to 1329 * verify that the user credentials of the process that enabled the 1330 * invoking ECB match the target credentials 1331 */ 1332static int 1333dtrace_priv_proc_common_user(dtrace_state_t *state) 1334{ 1335 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1336 1337 /* 1338 * We should always have a non-NULL state cred here, since if cred 1339 * is null (anonymous tracing), we fast-path bypass this routine. 1340 */ 1341 ASSERT(s_cr != NULL); 1342 1343 if ((cr = CRED()) != NULL && 1344 s_cr->cr_uid == cr->cr_uid && 1345 s_cr->cr_uid == cr->cr_ruid && 1346 s_cr->cr_uid == cr->cr_suid && 1347 s_cr->cr_gid == cr->cr_gid && 1348 s_cr->cr_gid == cr->cr_rgid && 1349 s_cr->cr_gid == cr->cr_sgid) 1350 return (1); 1351 1352 return (0); 1353} 1354 1355/* 1356 * This privilege check should be used by actions and subroutines to 1357 * verify that the zone of the process that enabled the invoking ECB 1358 * matches the target credentials 1359 */ 1360static int 1361dtrace_priv_proc_common_zone(dtrace_state_t *state) 1362{ 1363#ifdef illumos 1364 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1365 1366 /* 1367 * We should always have a non-NULL state cred here, since if cred 1368 * is null (anonymous tracing), we fast-path bypass this routine. 1369 */ 1370 ASSERT(s_cr != NULL); 1371 1372 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1373 return (1); 1374 1375 return (0); 1376#else 1377 return (1); 1378#endif 1379} 1380 1381/* 1382 * This privilege check should be used by actions and subroutines to 1383 * verify that the process has not setuid or changed credentials. 1384 */ 1385static int 1386dtrace_priv_proc_common_nocd(void) 1387{ 1388 proc_t *proc; 1389 1390 if ((proc = ttoproc(curthread)) != NULL && 1391 !(proc->p_flag & SNOCD)) 1392 return (1); 1393 1394 return (0); 1395} 1396 1397static int 1398dtrace_priv_proc_destructive(dtrace_state_t *state) 1399{ 1400 int action = state->dts_cred.dcr_action; 1401 1402 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1403 dtrace_priv_proc_common_zone(state) == 0) 1404 goto bad; 1405 1406 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1407 dtrace_priv_proc_common_user(state) == 0) 1408 goto bad; 1409 1410 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1411 dtrace_priv_proc_common_nocd() == 0) 1412 goto bad; 1413 1414 return (1); 1415 1416bad: 1417 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1418 1419 return (0); 1420} 1421 1422static int 1423dtrace_priv_proc_control(dtrace_state_t *state) 1424{ 1425 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1426 return (1); 1427 1428 if (dtrace_priv_proc_common_zone(state) && 1429 dtrace_priv_proc_common_user(state) && 1430 dtrace_priv_proc_common_nocd()) 1431 return (1); 1432 1433 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1434 1435 return (0); 1436} 1437 1438static int 1439dtrace_priv_proc(dtrace_state_t *state) 1440{ 1441 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1442 return (1); 1443 1444 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1445 1446 return (0); 1447} 1448 1449static int 1450dtrace_priv_kernel(dtrace_state_t *state) 1451{ 1452 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1453 return (1); 1454 1455 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1456 1457 return (0); 1458} 1459 1460static int 1461dtrace_priv_kernel_destructive(dtrace_state_t *state) 1462{ 1463 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1464 return (1); 1465 1466 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1467 1468 return (0); 1469} 1470 1471/* 1472 * Determine if the dte_cond of the specified ECB allows for processing of 1473 * the current probe to continue. Note that this routine may allow continued 1474 * processing, but with access(es) stripped from the mstate's dtms_access 1475 * field. 1476 */ 1477static int 1478dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1479 dtrace_ecb_t *ecb) 1480{ 1481 dtrace_probe_t *probe = ecb->dte_probe; 1482 dtrace_provider_t *prov = probe->dtpr_provider; 1483 dtrace_pops_t *pops = &prov->dtpv_pops; 1484 int mode = DTRACE_MODE_NOPRIV_DROP; 1485 1486 ASSERT(ecb->dte_cond); 1487 1488#ifdef illumos 1489 if (pops->dtps_mode != NULL) { 1490 mode = pops->dtps_mode(prov->dtpv_arg, 1491 probe->dtpr_id, probe->dtpr_arg); 1492 1493 ASSERT((mode & DTRACE_MODE_USER) || 1494 (mode & DTRACE_MODE_KERNEL)); 1495 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1496 (mode & DTRACE_MODE_NOPRIV_DROP)); 1497 } 1498 1499 /* 1500 * If the dte_cond bits indicate that this consumer is only allowed to 1501 * see user-mode firings of this probe, call the provider's dtps_mode() 1502 * entry point to check that the probe was fired while in a user 1503 * context. If that's not the case, use the policy specified by the 1504 * provider to determine if we drop the probe or merely restrict 1505 * operation. 1506 */ 1507 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1508 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1509 1510 if (!(mode & DTRACE_MODE_USER)) { 1511 if (mode & DTRACE_MODE_NOPRIV_DROP) 1512 return (0); 1513 1514 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1515 } 1516 } 1517#endif 1518 1519 /* 1520 * This is more subtle than it looks. We have to be absolutely certain 1521 * that CRED() isn't going to change out from under us so it's only 1522 * legit to examine that structure if we're in constrained situations. 1523 * Currently, the only times we'll this check is if a non-super-user 1524 * has enabled the profile or syscall providers -- providers that 1525 * allow visibility of all processes. For the profile case, the check 1526 * above will ensure that we're examining a user context. 1527 */ 1528 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1529 cred_t *cr; 1530 cred_t *s_cr = state->dts_cred.dcr_cred; 1531 proc_t *proc; 1532 1533 ASSERT(s_cr != NULL); 1534 1535 if ((cr = CRED()) == NULL || 1536 s_cr->cr_uid != cr->cr_uid || 1537 s_cr->cr_uid != cr->cr_ruid || 1538 s_cr->cr_uid != cr->cr_suid || 1539 s_cr->cr_gid != cr->cr_gid || 1540 s_cr->cr_gid != cr->cr_rgid || 1541 s_cr->cr_gid != cr->cr_sgid || 1542 (proc = ttoproc(curthread)) == NULL || 1543 (proc->p_flag & SNOCD)) { 1544 if (mode & DTRACE_MODE_NOPRIV_DROP) 1545 return (0); 1546 1547#ifdef illumos 1548 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1549#endif 1550 } 1551 } 1552 1553#ifdef illumos 1554 /* 1555 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1556 * in our zone, check to see if our mode policy is to restrict rather 1557 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1558 * and DTRACE_ACCESS_ARGS 1559 */ 1560 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1561 cred_t *cr; 1562 cred_t *s_cr = state->dts_cred.dcr_cred; 1563 1564 ASSERT(s_cr != NULL); 1565 1566 if ((cr = CRED()) == NULL || 1567 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1568 if (mode & DTRACE_MODE_NOPRIV_DROP) 1569 return (0); 1570 1571 mstate->dtms_access &= 1572 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1573 } 1574 } 1575#endif 1576 1577 return (1); 1578} 1579 1580/* 1581 * Note: not called from probe context. This function is called 1582 * asynchronously (and at a regular interval) from outside of probe context to 1583 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1584 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1585 */ 1586void 1587dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1588{ 1589 dtrace_dynvar_t *dirty; 1590 dtrace_dstate_percpu_t *dcpu; 1591 dtrace_dynvar_t **rinsep; 1592 int i, j, work = 0; 1593 1594 for (i = 0; i < NCPU; i++) { 1595 dcpu = &dstate->dtds_percpu[i]; 1596 rinsep = &dcpu->dtdsc_rinsing; 1597 1598 /* 1599 * If the dirty list is NULL, there is no dirty work to do. 1600 */ 1601 if (dcpu->dtdsc_dirty == NULL) 1602 continue; 1603 1604 if (dcpu->dtdsc_rinsing != NULL) { 1605 /* 1606 * If the rinsing list is non-NULL, then it is because 1607 * this CPU was selected to accept another CPU's 1608 * dirty list -- and since that time, dirty buffers 1609 * have accumulated. This is a highly unlikely 1610 * condition, but we choose to ignore the dirty 1611 * buffers -- they'll be picked up a future cleanse. 1612 */ 1613 continue; 1614 } 1615 1616 if (dcpu->dtdsc_clean != NULL) { 1617 /* 1618 * If the clean list is non-NULL, then we're in a 1619 * situation where a CPU has done deallocations (we 1620 * have a non-NULL dirty list) but no allocations (we 1621 * also have a non-NULL clean list). We can't simply 1622 * move the dirty list into the clean list on this 1623 * CPU, yet we also don't want to allow this condition 1624 * to persist, lest a short clean list prevent a 1625 * massive dirty list from being cleaned (which in 1626 * turn could lead to otherwise avoidable dynamic 1627 * drops). To deal with this, we look for some CPU 1628 * with a NULL clean list, NULL dirty list, and NULL 1629 * rinsing list -- and then we borrow this CPU to 1630 * rinse our dirty list. 1631 */ 1632 for (j = 0; j < NCPU; j++) { 1633 dtrace_dstate_percpu_t *rinser; 1634 1635 rinser = &dstate->dtds_percpu[j]; 1636 1637 if (rinser->dtdsc_rinsing != NULL) 1638 continue; 1639 1640 if (rinser->dtdsc_dirty != NULL) 1641 continue; 1642 1643 if (rinser->dtdsc_clean != NULL) 1644 continue; 1645 1646 rinsep = &rinser->dtdsc_rinsing; 1647 break; 1648 } 1649 1650 if (j == NCPU) { 1651 /* 1652 * We were unable to find another CPU that 1653 * could accept this dirty list -- we are 1654 * therefore unable to clean it now. 1655 */ 1656 dtrace_dynvar_failclean++; 1657 continue; 1658 } 1659 } 1660 1661 work = 1; 1662 1663 /* 1664 * Atomically move the dirty list aside. 1665 */ 1666 do { 1667 dirty = dcpu->dtdsc_dirty; 1668 1669 /* 1670 * Before we zap the dirty list, set the rinsing list. 1671 * (This allows for a potential assertion in 1672 * dtrace_dynvar(): if a free dynamic variable appears 1673 * on a hash chain, either the dirty list or the 1674 * rinsing list for some CPU must be non-NULL.) 1675 */ 1676 *rinsep = dirty; 1677 dtrace_membar_producer(); 1678 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1679 dirty, NULL) != dirty); 1680 } 1681 1682 if (!work) { 1683 /* 1684 * We have no work to do; we can simply return. 1685 */ 1686 return; 1687 } 1688 1689 dtrace_sync(); 1690 1691 for (i = 0; i < NCPU; i++) { 1692 dcpu = &dstate->dtds_percpu[i]; 1693 1694 if (dcpu->dtdsc_rinsing == NULL) 1695 continue; 1696 1697 /* 1698 * We are now guaranteed that no hash chain contains a pointer 1699 * into this dirty list; we can make it clean. 1700 */ 1701 ASSERT(dcpu->dtdsc_clean == NULL); 1702 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1703 dcpu->dtdsc_rinsing = NULL; 1704 } 1705 1706 /* 1707 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1708 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1709 * This prevents a race whereby a CPU incorrectly decides that 1710 * the state should be something other than DTRACE_DSTATE_CLEAN 1711 * after dtrace_dynvar_clean() has completed. 1712 */ 1713 dtrace_sync(); 1714 1715 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1716} 1717 1718/* 1719 * Depending on the value of the op parameter, this function looks-up, 1720 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1721 * allocation is requested, this function will return a pointer to a 1722 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1723 * variable can be allocated. If NULL is returned, the appropriate counter 1724 * will be incremented. 1725 */ 1726dtrace_dynvar_t * 1727dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1728 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1729 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1730{ 1731 uint64_t hashval = DTRACE_DYNHASH_VALID; 1732 dtrace_dynhash_t *hash = dstate->dtds_hash; 1733 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1734 processorid_t me = curcpu, cpu = me; 1735 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1736 size_t bucket, ksize; 1737 size_t chunksize = dstate->dtds_chunksize; 1738 uintptr_t kdata, lock, nstate; 1739 uint_t i; 1740 1741 ASSERT(nkeys != 0); 1742 1743 /* 1744 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1745 * algorithm. For the by-value portions, we perform the algorithm in 1746 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1747 * bit, and seems to have only a minute effect on distribution. For 1748 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1749 * over each referenced byte. It's painful to do this, but it's much 1750 * better than pathological hash distribution. The efficacy of the 1751 * hashing algorithm (and a comparison with other algorithms) may be 1752 * found by running the ::dtrace_dynstat MDB dcmd. 1753 */ 1754 for (i = 0; i < nkeys; i++) { 1755 if (key[i].dttk_size == 0) { 1756 uint64_t val = key[i].dttk_value; 1757 1758 hashval += (val >> 48) & 0xffff; 1759 hashval += (hashval << 10); 1760 hashval ^= (hashval >> 6); 1761 1762 hashval += (val >> 32) & 0xffff; 1763 hashval += (hashval << 10); 1764 hashval ^= (hashval >> 6); 1765 1766 hashval += (val >> 16) & 0xffff; 1767 hashval += (hashval << 10); 1768 hashval ^= (hashval >> 6); 1769 1770 hashval += val & 0xffff; 1771 hashval += (hashval << 10); 1772 hashval ^= (hashval >> 6); 1773 } else { 1774 /* 1775 * This is incredibly painful, but it beats the hell 1776 * out of the alternative. 1777 */ 1778 uint64_t j, size = key[i].dttk_size; 1779 uintptr_t base = (uintptr_t)key[i].dttk_value; 1780 1781 if (!dtrace_canload(base, size, mstate, vstate)) 1782 break; 1783 1784 for (j = 0; j < size; j++) { 1785 hashval += dtrace_load8(base + j); 1786 hashval += (hashval << 10); 1787 hashval ^= (hashval >> 6); 1788 } 1789 } 1790 } 1791 1792 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1793 return (NULL); 1794 1795 hashval += (hashval << 3); 1796 hashval ^= (hashval >> 11); 1797 hashval += (hashval << 15); 1798 1799 /* 1800 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1801 * comes out to be one of our two sentinel hash values. If this 1802 * actually happens, we set the hashval to be a value known to be a 1803 * non-sentinel value. 1804 */ 1805 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1806 hashval = DTRACE_DYNHASH_VALID; 1807 1808 /* 1809 * Yes, it's painful to do a divide here. If the cycle count becomes 1810 * important here, tricks can be pulled to reduce it. (However, it's 1811 * critical that hash collisions be kept to an absolute minimum; 1812 * they're much more painful than a divide.) It's better to have a 1813 * solution that generates few collisions and still keeps things 1814 * relatively simple. 1815 */ 1816 bucket = hashval % dstate->dtds_hashsize; 1817 1818 if (op == DTRACE_DYNVAR_DEALLOC) { 1819 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1820 1821 for (;;) { 1822 while ((lock = *lockp) & 1) 1823 continue; 1824 1825 if (dtrace_casptr((volatile void *)lockp, 1826 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1827 break; 1828 } 1829 1830 dtrace_membar_producer(); 1831 } 1832 1833top: 1834 prev = NULL; 1835 lock = hash[bucket].dtdh_lock; 1836 1837 dtrace_membar_consumer(); 1838 1839 start = hash[bucket].dtdh_chain; 1840 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1841 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1842 op != DTRACE_DYNVAR_DEALLOC)); 1843 1844 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1845 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1846 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1847 1848 if (dvar->dtdv_hashval != hashval) { 1849 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1850 /* 1851 * We've reached the sink, and therefore the 1852 * end of the hash chain; we can kick out of 1853 * the loop knowing that we have seen a valid 1854 * snapshot of state. 1855 */ 1856 ASSERT(dvar->dtdv_next == NULL); 1857 ASSERT(dvar == &dtrace_dynhash_sink); 1858 break; 1859 } 1860 1861 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1862 /* 1863 * We've gone off the rails: somewhere along 1864 * the line, one of the members of this hash 1865 * chain was deleted. Note that we could also 1866 * detect this by simply letting this loop run 1867 * to completion, as we would eventually hit 1868 * the end of the dirty list. However, we 1869 * want to avoid running the length of the 1870 * dirty list unnecessarily (it might be quite 1871 * long), so we catch this as early as 1872 * possible by detecting the hash marker. In 1873 * this case, we simply set dvar to NULL and 1874 * break; the conditional after the loop will 1875 * send us back to top. 1876 */ 1877 dvar = NULL; 1878 break; 1879 } 1880 1881 goto next; 1882 } 1883 1884 if (dtuple->dtt_nkeys != nkeys) 1885 goto next; 1886 1887 for (i = 0; i < nkeys; i++, dkey++) { 1888 if (dkey->dttk_size != key[i].dttk_size) 1889 goto next; /* size or type mismatch */ 1890 1891 if (dkey->dttk_size != 0) { 1892 if (dtrace_bcmp( 1893 (void *)(uintptr_t)key[i].dttk_value, 1894 (void *)(uintptr_t)dkey->dttk_value, 1895 dkey->dttk_size)) 1896 goto next; 1897 } else { 1898 if (dkey->dttk_value != key[i].dttk_value) 1899 goto next; 1900 } 1901 } 1902 1903 if (op != DTRACE_DYNVAR_DEALLOC) 1904 return (dvar); 1905 1906 ASSERT(dvar->dtdv_next == NULL || 1907 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1908 1909 if (prev != NULL) { 1910 ASSERT(hash[bucket].dtdh_chain != dvar); 1911 ASSERT(start != dvar); 1912 ASSERT(prev->dtdv_next == dvar); 1913 prev->dtdv_next = dvar->dtdv_next; 1914 } else { 1915 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1916 start, dvar->dtdv_next) != start) { 1917 /* 1918 * We have failed to atomically swing the 1919 * hash table head pointer, presumably because 1920 * of a conflicting allocation on another CPU. 1921 * We need to reread the hash chain and try 1922 * again. 1923 */ 1924 goto top; 1925 } 1926 } 1927 1928 dtrace_membar_producer(); 1929 1930 /* 1931 * Now set the hash value to indicate that it's free. 1932 */ 1933 ASSERT(hash[bucket].dtdh_chain != dvar); 1934 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1935 1936 dtrace_membar_producer(); 1937 1938 /* 1939 * Set the next pointer to point at the dirty list, and 1940 * atomically swing the dirty pointer to the newly freed dvar. 1941 */ 1942 do { 1943 next = dcpu->dtdsc_dirty; 1944 dvar->dtdv_next = next; 1945 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1946 1947 /* 1948 * Finally, unlock this hash bucket. 1949 */ 1950 ASSERT(hash[bucket].dtdh_lock == lock); 1951 ASSERT(lock & 1); 1952 hash[bucket].dtdh_lock++; 1953 1954 return (NULL); 1955next: 1956 prev = dvar; 1957 continue; 1958 } 1959 1960 if (dvar == NULL) { 1961 /* 1962 * If dvar is NULL, it is because we went off the rails: 1963 * one of the elements that we traversed in the hash chain 1964 * was deleted while we were traversing it. In this case, 1965 * we assert that we aren't doing a dealloc (deallocs lock 1966 * the hash bucket to prevent themselves from racing with 1967 * one another), and retry the hash chain traversal. 1968 */ 1969 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1970 goto top; 1971 } 1972 1973 if (op != DTRACE_DYNVAR_ALLOC) { 1974 /* 1975 * If we are not to allocate a new variable, we want to 1976 * return NULL now. Before we return, check that the value 1977 * of the lock word hasn't changed. If it has, we may have 1978 * seen an inconsistent snapshot. 1979 */ 1980 if (op == DTRACE_DYNVAR_NOALLOC) { 1981 if (hash[bucket].dtdh_lock != lock) 1982 goto top; 1983 } else { 1984 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1985 ASSERT(hash[bucket].dtdh_lock == lock); 1986 ASSERT(lock & 1); 1987 hash[bucket].dtdh_lock++; 1988 } 1989 1990 return (NULL); 1991 } 1992 1993 /* 1994 * We need to allocate a new dynamic variable. The size we need is the 1995 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1996 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1997 * the size of any referred-to data (dsize). We then round the final 1998 * size up to the chunksize for allocation. 1999 */ 2000 for (ksize = 0, i = 0; i < nkeys; i++) 2001 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 2002 2003 /* 2004 * This should be pretty much impossible, but could happen if, say, 2005 * strange DIF specified the tuple. Ideally, this should be an 2006 * assertion and not an error condition -- but that requires that the 2007 * chunksize calculation in dtrace_difo_chunksize() be absolutely 2008 * bullet-proof. (That is, it must not be able to be fooled by 2009 * malicious DIF.) Given the lack of backwards branches in DIF, 2010 * solving this would presumably not amount to solving the Halting 2011 * Problem -- but it still seems awfully hard. 2012 */ 2013 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 2014 ksize + dsize > chunksize) { 2015 dcpu->dtdsc_drops++; 2016 return (NULL); 2017 } 2018 2019 nstate = DTRACE_DSTATE_EMPTY; 2020 2021 do { 2022retry: 2023 free = dcpu->dtdsc_free; 2024 2025 if (free == NULL) { 2026 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 2027 void *rval; 2028 2029 if (clean == NULL) { 2030 /* 2031 * We're out of dynamic variable space on 2032 * this CPU. Unless we have tried all CPUs, 2033 * we'll try to allocate from a different 2034 * CPU. 2035 */ 2036 switch (dstate->dtds_state) { 2037 case DTRACE_DSTATE_CLEAN: { 2038 void *sp = &dstate->dtds_state; 2039 2040 if (++cpu >= NCPU) 2041 cpu = 0; 2042 2043 if (dcpu->dtdsc_dirty != NULL && 2044 nstate == DTRACE_DSTATE_EMPTY) 2045 nstate = DTRACE_DSTATE_DIRTY; 2046 2047 if (dcpu->dtdsc_rinsing != NULL) 2048 nstate = DTRACE_DSTATE_RINSING; 2049 2050 dcpu = &dstate->dtds_percpu[cpu]; 2051 2052 if (cpu != me) 2053 goto retry; 2054 2055 (void) dtrace_cas32(sp, 2056 DTRACE_DSTATE_CLEAN, nstate); 2057 2058 /* 2059 * To increment the correct bean 2060 * counter, take another lap. 2061 */ 2062 goto retry; 2063 } 2064 2065 case DTRACE_DSTATE_DIRTY: 2066 dcpu->dtdsc_dirty_drops++; 2067 break; 2068 2069 case DTRACE_DSTATE_RINSING: 2070 dcpu->dtdsc_rinsing_drops++; 2071 break; 2072 2073 case DTRACE_DSTATE_EMPTY: 2074 dcpu->dtdsc_drops++; 2075 break; 2076 } 2077 2078 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 2079 return (NULL); 2080 } 2081 2082 /* 2083 * The clean list appears to be non-empty. We want to 2084 * move the clean list to the free list; we start by 2085 * moving the clean pointer aside. 2086 */ 2087 if (dtrace_casptr(&dcpu->dtdsc_clean, 2088 clean, NULL) != clean) { 2089 /* 2090 * We are in one of two situations: 2091 * 2092 * (a) The clean list was switched to the 2093 * free list by another CPU. 2094 * 2095 * (b) The clean list was added to by the 2096 * cleansing cyclic. 2097 * 2098 * In either of these situations, we can 2099 * just reattempt the free list allocation. 2100 */ 2101 goto retry; 2102 } 2103 2104 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 2105 2106 /* 2107 * Now we'll move the clean list to our free list. 2108 * It's impossible for this to fail: the only way 2109 * the free list can be updated is through this 2110 * code path, and only one CPU can own the clean list. 2111 * Thus, it would only be possible for this to fail if 2112 * this code were racing with dtrace_dynvar_clean(). 2113 * (That is, if dtrace_dynvar_clean() updated the clean 2114 * list, and we ended up racing to update the free 2115 * list.) This race is prevented by the dtrace_sync() 2116 * in dtrace_dynvar_clean() -- which flushes the 2117 * owners of the clean lists out before resetting 2118 * the clean lists. 2119 */ 2120 dcpu = &dstate->dtds_percpu[me]; 2121 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 2122 ASSERT(rval == NULL); 2123 goto retry; 2124 } 2125 2126 dvar = free; 2127 new_free = dvar->dtdv_next; 2128 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 2129 2130 /* 2131 * We have now allocated a new chunk. We copy the tuple keys into the 2132 * tuple array and copy any referenced key data into the data space 2133 * following the tuple array. As we do this, we relocate dttk_value 2134 * in the final tuple to point to the key data address in the chunk. 2135 */ 2136 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 2137 dvar->dtdv_data = (void *)(kdata + ksize); 2138 dvar->dtdv_tuple.dtt_nkeys = nkeys; 2139 2140 for (i = 0; i < nkeys; i++) { 2141 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 2142 size_t kesize = key[i].dttk_size; 2143 2144 if (kesize != 0) { 2145 dtrace_bcopy( 2146 (const void *)(uintptr_t)key[i].dttk_value, 2147 (void *)kdata, kesize); 2148 dkey->dttk_value = kdata; 2149 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 2150 } else { 2151 dkey->dttk_value = key[i].dttk_value; 2152 } 2153 2154 dkey->dttk_size = kesize; 2155 } 2156 2157 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 2158 dvar->dtdv_hashval = hashval; 2159 dvar->dtdv_next = start; 2160 2161 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 2162 return (dvar); 2163 2164 /* 2165 * The cas has failed. Either another CPU is adding an element to 2166 * this hash chain, or another CPU is deleting an element from this 2167 * hash chain. The simplest way to deal with both of these cases 2168 * (though not necessarily the most efficient) is to free our 2169 * allocated block and tail-call ourselves. Note that the free is 2170 * to the dirty list and _not_ to the free list. This is to prevent 2171 * races with allocators, above. 2172 */ 2173 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2174 2175 dtrace_membar_producer(); 2176 2177 do { 2178 free = dcpu->dtdsc_dirty; 2179 dvar->dtdv_next = free; 2180 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 2181 2182 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 2183} 2184 2185/*ARGSUSED*/ 2186static void 2187dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 2188{ 2189 if ((int64_t)nval < (int64_t)*oval) 2190 *oval = nval; 2191} 2192 2193/*ARGSUSED*/ 2194static void 2195dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2196{ 2197 if ((int64_t)nval > (int64_t)*oval) 2198 *oval = nval; 2199} 2200 2201static void 2202dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2203{ 2204 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2205 int64_t val = (int64_t)nval; 2206 2207 if (val < 0) { 2208 for (i = 0; i < zero; i++) { 2209 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2210 quanta[i] += incr; 2211 return; 2212 } 2213 } 2214 } else { 2215 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2216 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2217 quanta[i - 1] += incr; 2218 return; 2219 } 2220 } 2221 2222 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2223 return; 2224 } 2225 2226 ASSERT(0); 2227} 2228 2229static void 2230dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2231{ 2232 uint64_t arg = *lquanta++; 2233 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2234 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2235 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2236 int32_t val = (int32_t)nval, level; 2237 2238 ASSERT(step != 0); 2239 ASSERT(levels != 0); 2240 2241 if (val < base) { 2242 /* 2243 * This is an underflow. 2244 */ 2245 lquanta[0] += incr; 2246 return; 2247 } 2248 2249 level = (val - base) / step; 2250 2251 if (level < levels) { 2252 lquanta[level + 1] += incr; 2253 return; 2254 } 2255 2256 /* 2257 * This is an overflow. 2258 */ 2259 lquanta[levels + 1] += incr; 2260} 2261 2262static int 2263dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2264 uint16_t high, uint16_t nsteps, int64_t value) 2265{ 2266 int64_t this = 1, last, next; 2267 int base = 1, order; 2268 2269 ASSERT(factor <= nsteps); 2270 ASSERT(nsteps % factor == 0); 2271 2272 for (order = 0; order < low; order++) 2273 this *= factor; 2274 2275 /* 2276 * If our value is less than our factor taken to the power of the 2277 * low order of magnitude, it goes into the zeroth bucket. 2278 */ 2279 if (value < (last = this)) 2280 return (0); 2281 2282 for (this *= factor; order <= high; order++) { 2283 int nbuckets = this > nsteps ? nsteps : this; 2284 2285 if ((next = this * factor) < this) { 2286 /* 2287 * We should not generally get log/linear quantizations 2288 * with a high magnitude that allows 64-bits to 2289 * overflow, but we nonetheless protect against this 2290 * by explicitly checking for overflow, and clamping 2291 * our value accordingly. 2292 */ 2293 value = this - 1; 2294 } 2295 2296 if (value < this) { 2297 /* 2298 * If our value lies within this order of magnitude, 2299 * determine its position by taking the offset within 2300 * the order of magnitude, dividing by the bucket 2301 * width, and adding to our (accumulated) base. 2302 */ 2303 return (base + (value - last) / (this / nbuckets)); 2304 } 2305 2306 base += nbuckets - (nbuckets / factor); 2307 last = this; 2308 this = next; 2309 } 2310 2311 /* 2312 * Our value is greater than or equal to our factor taken to the 2313 * power of one plus the high magnitude -- return the top bucket. 2314 */ 2315 return (base); 2316} 2317 2318static void 2319dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2320{ 2321 uint64_t arg = *llquanta++; 2322 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2323 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2324 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2325 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2326 2327 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2328 low, high, nsteps, nval)] += incr; 2329} 2330 2331/*ARGSUSED*/ 2332static void 2333dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2334{ 2335 data[0]++; 2336 data[1] += nval; 2337} 2338 2339/*ARGSUSED*/ 2340static void 2341dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2342{ 2343 int64_t snval = (int64_t)nval; 2344 uint64_t tmp[2]; 2345 2346 data[0]++; 2347 data[1] += nval; 2348 2349 /* 2350 * What we want to say here is: 2351 * 2352 * data[2] += nval * nval; 2353 * 2354 * But given that nval is 64-bit, we could easily overflow, so 2355 * we do this as 128-bit arithmetic. 2356 */ 2357 if (snval < 0) 2358 snval = -snval; 2359 2360 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2361 dtrace_add_128(data + 2, tmp, data + 2); 2362} 2363 2364/*ARGSUSED*/ 2365static void 2366dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2367{ 2368 *oval = *oval + 1; 2369} 2370 2371/*ARGSUSED*/ 2372static void 2373dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2374{ 2375 *oval += nval; 2376} 2377 2378/* 2379 * Aggregate given the tuple in the principal data buffer, and the aggregating 2380 * action denoted by the specified dtrace_aggregation_t. The aggregation 2381 * buffer is specified as the buf parameter. This routine does not return 2382 * failure; if there is no space in the aggregation buffer, the data will be 2383 * dropped, and a corresponding counter incremented. 2384 */ 2385static void 2386dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2387 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2388{ 2389 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2390 uint32_t i, ndx, size, fsize; 2391 uint32_t align = sizeof (uint64_t) - 1; 2392 dtrace_aggbuffer_t *agb; 2393 dtrace_aggkey_t *key; 2394 uint32_t hashval = 0, limit, isstr; 2395 caddr_t tomax, data, kdata; 2396 dtrace_actkind_t action; 2397 dtrace_action_t *act; 2398 uintptr_t offs; 2399 2400 if (buf == NULL) 2401 return; 2402 2403 if (!agg->dtag_hasarg) { 2404 /* 2405 * Currently, only quantize() and lquantize() take additional 2406 * arguments, and they have the same semantics: an increment 2407 * value that defaults to 1 when not present. If additional 2408 * aggregating actions take arguments, the setting of the 2409 * default argument value will presumably have to become more 2410 * sophisticated... 2411 */ 2412 arg = 1; 2413 } 2414 2415 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2416 size = rec->dtrd_offset - agg->dtag_base; 2417 fsize = size + rec->dtrd_size; 2418 2419 ASSERT(dbuf->dtb_tomax != NULL); 2420 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2421 2422 if ((tomax = buf->dtb_tomax) == NULL) { 2423 dtrace_buffer_drop(buf); 2424 return; 2425 } 2426 2427 /* 2428 * The metastructure is always at the bottom of the buffer. 2429 */ 2430 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2431 sizeof (dtrace_aggbuffer_t)); 2432 2433 if (buf->dtb_offset == 0) { 2434 /* 2435 * We just kludge up approximately 1/8th of the size to be 2436 * buckets. If this guess ends up being routinely 2437 * off-the-mark, we may need to dynamically readjust this 2438 * based on past performance. 2439 */ 2440 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2441 2442 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2443 (uintptr_t)tomax || hashsize == 0) { 2444 /* 2445 * We've been given a ludicrously small buffer; 2446 * increment our drop count and leave. 2447 */ 2448 dtrace_buffer_drop(buf); 2449 return; 2450 } 2451 2452 /* 2453 * And now, a pathetic attempt to try to get a an odd (or 2454 * perchance, a prime) hash size for better hash distribution. 2455 */ 2456 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2457 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2458 2459 agb->dtagb_hashsize = hashsize; 2460 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2461 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2462 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2463 2464 for (i = 0; i < agb->dtagb_hashsize; i++) 2465 agb->dtagb_hash[i] = NULL; 2466 } 2467 2468 ASSERT(agg->dtag_first != NULL); 2469 ASSERT(agg->dtag_first->dta_intuple); 2470 2471 /* 2472 * Calculate the hash value based on the key. Note that we _don't_ 2473 * include the aggid in the hashing (but we will store it as part of 2474 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2475 * algorithm: a simple, quick algorithm that has no known funnels, and 2476 * gets good distribution in practice. The efficacy of the hashing 2477 * algorithm (and a comparison with other algorithms) may be found by 2478 * running the ::dtrace_aggstat MDB dcmd. 2479 */ 2480 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2481 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2482 limit = i + act->dta_rec.dtrd_size; 2483 ASSERT(limit <= size); 2484 isstr = DTRACEACT_ISSTRING(act); 2485 2486 for (; i < limit; i++) { 2487 hashval += data[i]; 2488 hashval += (hashval << 10); 2489 hashval ^= (hashval >> 6); 2490 2491 if (isstr && data[i] == '\0') 2492 break; 2493 } 2494 } 2495 2496 hashval += (hashval << 3); 2497 hashval ^= (hashval >> 11); 2498 hashval += (hashval << 15); 2499 2500 /* 2501 * Yes, the divide here is expensive -- but it's generally the least 2502 * of the performance issues given the amount of data that we iterate 2503 * over to compute hash values, compare data, etc. 2504 */ 2505 ndx = hashval % agb->dtagb_hashsize; 2506 2507 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2508 ASSERT((caddr_t)key >= tomax); 2509 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2510 2511 if (hashval != key->dtak_hashval || key->dtak_size != size) 2512 continue; 2513 2514 kdata = key->dtak_data; 2515 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2516 2517 for (act = agg->dtag_first; act->dta_intuple; 2518 act = act->dta_next) { 2519 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2520 limit = i + act->dta_rec.dtrd_size; 2521 ASSERT(limit <= size); 2522 isstr = DTRACEACT_ISSTRING(act); 2523 2524 for (; i < limit; i++) { 2525 if (kdata[i] != data[i]) 2526 goto next; 2527 2528 if (isstr && data[i] == '\0') 2529 break; 2530 } 2531 } 2532 2533 if (action != key->dtak_action) { 2534 /* 2535 * We are aggregating on the same value in the same 2536 * aggregation with two different aggregating actions. 2537 * (This should have been picked up in the compiler, 2538 * so we may be dealing with errant or devious DIF.) 2539 * This is an error condition; we indicate as much, 2540 * and return. 2541 */ 2542 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2543 return; 2544 } 2545 2546 /* 2547 * This is a hit: we need to apply the aggregator to 2548 * the value at this key. 2549 */ 2550 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2551 return; 2552next: 2553 continue; 2554 } 2555 2556 /* 2557 * We didn't find it. We need to allocate some zero-filled space, 2558 * link it into the hash table appropriately, and apply the aggregator 2559 * to the (zero-filled) value. 2560 */ 2561 offs = buf->dtb_offset; 2562 while (offs & (align - 1)) 2563 offs += sizeof (uint32_t); 2564 2565 /* 2566 * If we don't have enough room to both allocate a new key _and_ 2567 * its associated data, increment the drop count and return. 2568 */ 2569 if ((uintptr_t)tomax + offs + fsize > 2570 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2571 dtrace_buffer_drop(buf); 2572 return; 2573 } 2574 2575 /*CONSTCOND*/ 2576 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2577 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2578 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2579 2580 key->dtak_data = kdata = tomax + offs; 2581 buf->dtb_offset = offs + fsize; 2582 2583 /* 2584 * Now copy the data across. 2585 */ 2586 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2587 2588 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2589 kdata[i] = data[i]; 2590 2591 /* 2592 * Because strings are not zeroed out by default, we need to iterate 2593 * looking for actions that store strings, and we need to explicitly 2594 * pad these strings out with zeroes. 2595 */ 2596 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2597 int nul; 2598 2599 if (!DTRACEACT_ISSTRING(act)) 2600 continue; 2601 2602 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2603 limit = i + act->dta_rec.dtrd_size; 2604 ASSERT(limit <= size); 2605 2606 for (nul = 0; i < limit; i++) { 2607 if (nul) { 2608 kdata[i] = '\0'; 2609 continue; 2610 } 2611 2612 if (data[i] != '\0') 2613 continue; 2614 2615 nul = 1; 2616 } 2617 } 2618 2619 for (i = size; i < fsize; i++) 2620 kdata[i] = 0; 2621 2622 key->dtak_hashval = hashval; 2623 key->dtak_size = size; 2624 key->dtak_action = action; 2625 key->dtak_next = agb->dtagb_hash[ndx]; 2626 agb->dtagb_hash[ndx] = key; 2627 2628 /* 2629 * Finally, apply the aggregator. 2630 */ 2631 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2632 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2633} 2634 2635/* 2636 * Given consumer state, this routine finds a speculation in the INACTIVE 2637 * state and transitions it into the ACTIVE state. If there is no speculation 2638 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2639 * incremented -- it is up to the caller to take appropriate action. 2640 */ 2641static int 2642dtrace_speculation(dtrace_state_t *state) 2643{ 2644 int i = 0; 2645 dtrace_speculation_state_t current; 2646 uint32_t *stat = &state->dts_speculations_unavail, count; 2647 2648 while (i < state->dts_nspeculations) { 2649 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2650 2651 current = spec->dtsp_state; 2652 2653 if (current != DTRACESPEC_INACTIVE) { 2654 if (current == DTRACESPEC_COMMITTINGMANY || 2655 current == DTRACESPEC_COMMITTING || 2656 current == DTRACESPEC_DISCARDING) 2657 stat = &state->dts_speculations_busy; 2658 i++; 2659 continue; 2660 } 2661 2662 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2663 current, DTRACESPEC_ACTIVE) == current) 2664 return (i + 1); 2665 } 2666 2667 /* 2668 * We couldn't find a speculation. If we found as much as a single 2669 * busy speculation buffer, we'll attribute this failure as "busy" 2670 * instead of "unavail". 2671 */ 2672 do { 2673 count = *stat; 2674 } while (dtrace_cas32(stat, count, count + 1) != count); 2675 2676 return (0); 2677} 2678 2679/* 2680 * This routine commits an active speculation. If the specified speculation 2681 * is not in a valid state to perform a commit(), this routine will silently do 2682 * nothing. The state of the specified speculation is transitioned according 2683 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2684 */ 2685static void 2686dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2687 dtrace_specid_t which) 2688{ 2689 dtrace_speculation_t *spec; 2690 dtrace_buffer_t *src, *dest; 2691 uintptr_t daddr, saddr, dlimit, slimit; 2692 dtrace_speculation_state_t current, new = 0; 2693 intptr_t offs; 2694 uint64_t timestamp; 2695 2696 if (which == 0) 2697 return; 2698 2699 if (which > state->dts_nspeculations) { 2700 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2701 return; 2702 } 2703 2704 spec = &state->dts_speculations[which - 1]; 2705 src = &spec->dtsp_buffer[cpu]; 2706 dest = &state->dts_buffer[cpu]; 2707 2708 do { 2709 current = spec->dtsp_state; 2710 2711 if (current == DTRACESPEC_COMMITTINGMANY) 2712 break; 2713 2714 switch (current) { 2715 case DTRACESPEC_INACTIVE: 2716 case DTRACESPEC_DISCARDING: 2717 return; 2718 2719 case DTRACESPEC_COMMITTING: 2720 /* 2721 * This is only possible if we are (a) commit()'ing 2722 * without having done a prior speculate() on this CPU 2723 * and (b) racing with another commit() on a different 2724 * CPU. There's nothing to do -- we just assert that 2725 * our offset is 0. 2726 */ 2727 ASSERT(src->dtb_offset == 0); 2728 return; 2729 2730 case DTRACESPEC_ACTIVE: 2731 new = DTRACESPEC_COMMITTING; 2732 break; 2733 2734 case DTRACESPEC_ACTIVEONE: 2735 /* 2736 * This speculation is active on one CPU. If our 2737 * buffer offset is non-zero, we know that the one CPU 2738 * must be us. Otherwise, we are committing on a 2739 * different CPU from the speculate(), and we must 2740 * rely on being asynchronously cleaned. 2741 */ 2742 if (src->dtb_offset != 0) { 2743 new = DTRACESPEC_COMMITTING; 2744 break; 2745 } 2746 /*FALLTHROUGH*/ 2747 2748 case DTRACESPEC_ACTIVEMANY: 2749 new = DTRACESPEC_COMMITTINGMANY; 2750 break; 2751 2752 default: 2753 ASSERT(0); 2754 } 2755 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2756 current, new) != current); 2757 2758 /* 2759 * We have set the state to indicate that we are committing this 2760 * speculation. Now reserve the necessary space in the destination 2761 * buffer. 2762 */ 2763 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2764 sizeof (uint64_t), state, NULL)) < 0) { 2765 dtrace_buffer_drop(dest); 2766 goto out; 2767 } 2768 2769 /* 2770 * We have sufficient space to copy the speculative buffer into the 2771 * primary buffer. First, modify the speculative buffer, filling 2772 * in the timestamp of all entries with the current time. The data 2773 * must have the commit() time rather than the time it was traced, 2774 * so that all entries in the primary buffer are in timestamp order. 2775 */ 2776 timestamp = dtrace_gethrtime(); 2777 saddr = (uintptr_t)src->dtb_tomax; 2778 slimit = saddr + src->dtb_offset; 2779 while (saddr < slimit) { 2780 size_t size; 2781 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2782 2783 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2784 saddr += sizeof (dtrace_epid_t); 2785 continue; 2786 } 2787 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2788 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2789 2790 ASSERT3U(saddr + size, <=, slimit); 2791 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2792 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2793 2794 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2795 2796 saddr += size; 2797 } 2798 2799 /* 2800 * Copy the buffer across. (Note that this is a 2801 * highly subobtimal bcopy(); in the unlikely event that this becomes 2802 * a serious performance issue, a high-performance DTrace-specific 2803 * bcopy() should obviously be invented.) 2804 */ 2805 daddr = (uintptr_t)dest->dtb_tomax + offs; 2806 dlimit = daddr + src->dtb_offset; 2807 saddr = (uintptr_t)src->dtb_tomax; 2808 2809 /* 2810 * First, the aligned portion. 2811 */ 2812 while (dlimit - daddr >= sizeof (uint64_t)) { 2813 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2814 2815 daddr += sizeof (uint64_t); 2816 saddr += sizeof (uint64_t); 2817 } 2818 2819 /* 2820 * Now any left-over bit... 2821 */ 2822 while (dlimit - daddr) 2823 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2824 2825 /* 2826 * Finally, commit the reserved space in the destination buffer. 2827 */ 2828 dest->dtb_offset = offs + src->dtb_offset; 2829 2830out: 2831 /* 2832 * If we're lucky enough to be the only active CPU on this speculation 2833 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2834 */ 2835 if (current == DTRACESPEC_ACTIVE || 2836 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2837 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2838 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2839 2840 ASSERT(rval == DTRACESPEC_COMMITTING); 2841 } 2842 2843 src->dtb_offset = 0; 2844 src->dtb_xamot_drops += src->dtb_drops; 2845 src->dtb_drops = 0; 2846} 2847 2848/* 2849 * This routine discards an active speculation. If the specified speculation 2850 * is not in a valid state to perform a discard(), this routine will silently 2851 * do nothing. The state of the specified speculation is transitioned 2852 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2853 */ 2854static void 2855dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2856 dtrace_specid_t which) 2857{ 2858 dtrace_speculation_t *spec; 2859 dtrace_speculation_state_t current, new = 0; 2860 dtrace_buffer_t *buf; 2861 2862 if (which == 0) 2863 return; 2864 2865 if (which > state->dts_nspeculations) { 2866 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2867 return; 2868 } 2869 2870 spec = &state->dts_speculations[which - 1]; 2871 buf = &spec->dtsp_buffer[cpu]; 2872 2873 do { 2874 current = spec->dtsp_state; 2875 2876 switch (current) { 2877 case DTRACESPEC_INACTIVE: 2878 case DTRACESPEC_COMMITTINGMANY: 2879 case DTRACESPEC_COMMITTING: 2880 case DTRACESPEC_DISCARDING: 2881 return; 2882 2883 case DTRACESPEC_ACTIVE: 2884 case DTRACESPEC_ACTIVEMANY: 2885 new = DTRACESPEC_DISCARDING; 2886 break; 2887 2888 case DTRACESPEC_ACTIVEONE: 2889 if (buf->dtb_offset != 0) { 2890 new = DTRACESPEC_INACTIVE; 2891 } else { 2892 new = DTRACESPEC_DISCARDING; 2893 } 2894 break; 2895 2896 default: 2897 ASSERT(0); 2898 } 2899 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2900 current, new) != current); 2901 2902 buf->dtb_offset = 0; 2903 buf->dtb_drops = 0; 2904} 2905 2906/* 2907 * Note: not called from probe context. This function is called 2908 * asynchronously from cross call context to clean any speculations that are 2909 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2910 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2911 * speculation. 2912 */ 2913static void 2914dtrace_speculation_clean_here(dtrace_state_t *state) 2915{ 2916 dtrace_icookie_t cookie; 2917 processorid_t cpu = curcpu; 2918 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2919 dtrace_specid_t i; 2920 2921 cookie = dtrace_interrupt_disable(); 2922 2923 if (dest->dtb_tomax == NULL) { 2924 dtrace_interrupt_enable(cookie); 2925 return; 2926 } 2927 2928 for (i = 0; i < state->dts_nspeculations; i++) { 2929 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2930 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2931 2932 if (src->dtb_tomax == NULL) 2933 continue; 2934 2935 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2936 src->dtb_offset = 0; 2937 continue; 2938 } 2939 2940 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2941 continue; 2942 2943 if (src->dtb_offset == 0) 2944 continue; 2945 2946 dtrace_speculation_commit(state, cpu, i + 1); 2947 } 2948 2949 dtrace_interrupt_enable(cookie); 2950} 2951 2952/* 2953 * Note: not called from probe context. This function is called 2954 * asynchronously (and at a regular interval) to clean any speculations that 2955 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2956 * is work to be done, it cross calls all CPUs to perform that work; 2957 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2958 * INACTIVE state until they have been cleaned by all CPUs. 2959 */ 2960static void 2961dtrace_speculation_clean(dtrace_state_t *state) 2962{ 2963 int work = 0, rv; 2964 dtrace_specid_t i; 2965 2966 for (i = 0; i < state->dts_nspeculations; i++) { 2967 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2968 2969 ASSERT(!spec->dtsp_cleaning); 2970 2971 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2972 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2973 continue; 2974 2975 work++; 2976 spec->dtsp_cleaning = 1; 2977 } 2978 2979 if (!work) 2980 return; 2981 2982 dtrace_xcall(DTRACE_CPUALL, 2983 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2984 2985 /* 2986 * We now know that all CPUs have committed or discarded their 2987 * speculation buffers, as appropriate. We can now set the state 2988 * to inactive. 2989 */ 2990 for (i = 0; i < state->dts_nspeculations; i++) { 2991 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2992 dtrace_speculation_state_t current, new; 2993 2994 if (!spec->dtsp_cleaning) 2995 continue; 2996 2997 current = spec->dtsp_state; 2998 ASSERT(current == DTRACESPEC_DISCARDING || 2999 current == DTRACESPEC_COMMITTINGMANY); 3000 3001 new = DTRACESPEC_INACTIVE; 3002 3003 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 3004 ASSERT(rv == current); 3005 spec->dtsp_cleaning = 0; 3006 } 3007} 3008 3009/* 3010 * Called as part of a speculate() to get the speculative buffer associated 3011 * with a given speculation. Returns NULL if the specified speculation is not 3012 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 3013 * the active CPU is not the specified CPU -- the speculation will be 3014 * atomically transitioned into the ACTIVEMANY state. 3015 */ 3016static dtrace_buffer_t * 3017dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 3018 dtrace_specid_t which) 3019{ 3020 dtrace_speculation_t *spec; 3021 dtrace_speculation_state_t current, new = 0; 3022 dtrace_buffer_t *buf; 3023 3024 if (which == 0) 3025 return (NULL); 3026 3027 if (which > state->dts_nspeculations) { 3028 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 3029 return (NULL); 3030 } 3031 3032 spec = &state->dts_speculations[which - 1]; 3033 buf = &spec->dtsp_buffer[cpuid]; 3034 3035 do { 3036 current = spec->dtsp_state; 3037 3038 switch (current) { 3039 case DTRACESPEC_INACTIVE: 3040 case DTRACESPEC_COMMITTINGMANY: 3041 case DTRACESPEC_DISCARDING: 3042 return (NULL); 3043 3044 case DTRACESPEC_COMMITTING: 3045 ASSERT(buf->dtb_offset == 0); 3046 return (NULL); 3047 3048 case DTRACESPEC_ACTIVEONE: 3049 /* 3050 * This speculation is currently active on one CPU. 3051 * Check the offset in the buffer; if it's non-zero, 3052 * that CPU must be us (and we leave the state alone). 3053 * If it's zero, assume that we're starting on a new 3054 * CPU -- and change the state to indicate that the 3055 * speculation is active on more than one CPU. 3056 */ 3057 if (buf->dtb_offset != 0) 3058 return (buf); 3059 3060 new = DTRACESPEC_ACTIVEMANY; 3061 break; 3062 3063 case DTRACESPEC_ACTIVEMANY: 3064 return (buf); 3065 3066 case DTRACESPEC_ACTIVE: 3067 new = DTRACESPEC_ACTIVEONE; 3068 break; 3069 3070 default: 3071 ASSERT(0); 3072 } 3073 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3074 current, new) != current); 3075 3076 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 3077 return (buf); 3078} 3079 3080/* 3081 * Return a string. In the event that the user lacks the privilege to access 3082 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3083 * don't fail access checking. 3084 * 3085 * dtrace_dif_variable() uses this routine as a helper for various 3086 * builtin values such as 'execname' and 'probefunc.' 3087 */ 3088uintptr_t 3089dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 3090 dtrace_mstate_t *mstate) 3091{ 3092 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3093 uintptr_t ret; 3094 size_t strsz; 3095 3096 /* 3097 * The easy case: this probe is allowed to read all of memory, so 3098 * we can just return this as a vanilla pointer. 3099 */ 3100 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 3101 return (addr); 3102 3103 /* 3104 * This is the tougher case: we copy the string in question from 3105 * kernel memory into scratch memory and return it that way: this 3106 * ensures that we won't trip up when access checking tests the 3107 * BYREF return value. 3108 */ 3109 strsz = dtrace_strlen((char *)addr, size) + 1; 3110 3111 if (mstate->dtms_scratch_ptr + strsz > 3112 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3113 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3114 return (0); 3115 } 3116 3117 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3118 strsz); 3119 ret = mstate->dtms_scratch_ptr; 3120 mstate->dtms_scratch_ptr += strsz; 3121 return (ret); 3122} 3123 3124/* 3125 * Return a string from a memoy address which is known to have one or 3126 * more concatenated, individually zero terminated, sub-strings. 3127 * In the event that the user lacks the privilege to access 3128 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3129 * don't fail access checking. 3130 * 3131 * dtrace_dif_variable() uses this routine as a helper for various 3132 * builtin values such as 'execargs'. 3133 */ 3134static uintptr_t 3135dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 3136 dtrace_mstate_t *mstate) 3137{ 3138 char *p; 3139 size_t i; 3140 uintptr_t ret; 3141 3142 if (mstate->dtms_scratch_ptr + strsz > 3143 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3144 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3145 return (0); 3146 } 3147 3148 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3149 strsz); 3150 3151 /* Replace sub-string termination characters with a space. */ 3152 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 3153 p++, i++) 3154 if (*p == '\0') 3155 *p = ' '; 3156 3157 ret = mstate->dtms_scratch_ptr; 3158 mstate->dtms_scratch_ptr += strsz; 3159 return (ret); 3160} 3161 3162/* 3163 * This function implements the DIF emulator's variable lookups. The emulator 3164 * passes a reserved variable identifier and optional built-in array index. 3165 */ 3166static uint64_t 3167dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 3168 uint64_t ndx) 3169{ 3170 /* 3171 * If we're accessing one of the uncached arguments, we'll turn this 3172 * into a reference in the args array. 3173 */ 3174 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 3175 ndx = v - DIF_VAR_ARG0; 3176 v = DIF_VAR_ARGS; 3177 } 3178 3179 switch (v) { 3180 case DIF_VAR_ARGS: 3181 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 3182 if (ndx >= sizeof (mstate->dtms_arg) / 3183 sizeof (mstate->dtms_arg[0])) { 3184 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3185 dtrace_provider_t *pv; 3186 uint64_t val; 3187 3188 pv = mstate->dtms_probe->dtpr_provider; 3189 if (pv->dtpv_pops.dtps_getargval != NULL) 3190 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 3191 mstate->dtms_probe->dtpr_id, 3192 mstate->dtms_probe->dtpr_arg, ndx, aframes); 3193 else 3194 val = dtrace_getarg(ndx, aframes); 3195 3196 /* 3197 * This is regrettably required to keep the compiler 3198 * from tail-optimizing the call to dtrace_getarg(). 3199 * The condition always evaluates to true, but the 3200 * compiler has no way of figuring that out a priori. 3201 * (None of this would be necessary if the compiler 3202 * could be relied upon to _always_ tail-optimize 3203 * the call to dtrace_getarg() -- but it can't.) 3204 */ 3205 if (mstate->dtms_probe != NULL) 3206 return (val); 3207 3208 ASSERT(0); 3209 } 3210 3211 return (mstate->dtms_arg[ndx]); 3212 3213#ifdef illumos 3214 case DIF_VAR_UREGS: { 3215 klwp_t *lwp; 3216 3217 if (!dtrace_priv_proc(state)) 3218 return (0); 3219 3220 if ((lwp = curthread->t_lwp) == NULL) { 3221 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3222 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 3223 return (0); 3224 } 3225 3226 return (dtrace_getreg(lwp->lwp_regs, ndx)); 3227 return (0); 3228 } 3229#else 3230 case DIF_VAR_UREGS: { 3231 struct trapframe *tframe; 3232 3233 if (!dtrace_priv_proc(state)) 3234 return (0); 3235 3236 if ((tframe = curthread->td_frame) == NULL) { 3237 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3238 cpu_core[curcpu].cpuc_dtrace_illval = 0; 3239 return (0); 3240 } 3241 3242 return (dtrace_getreg(tframe, ndx)); 3243 } 3244#endif 3245 3246 case DIF_VAR_CURTHREAD: 3247 if (!dtrace_priv_proc(state)) 3248 return (0); 3249 return ((uint64_t)(uintptr_t)curthread); 3250 3251 case DIF_VAR_TIMESTAMP: 3252 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 3253 mstate->dtms_timestamp = dtrace_gethrtime(); 3254 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 3255 } 3256 return (mstate->dtms_timestamp); 3257 3258 case DIF_VAR_VTIMESTAMP: 3259 ASSERT(dtrace_vtime_references != 0); 3260 return (curthread->t_dtrace_vtime); 3261 3262 case DIF_VAR_WALLTIMESTAMP: 3263 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3264 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3265 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3266 } 3267 return (mstate->dtms_walltimestamp); 3268 3269#ifdef illumos 3270 case DIF_VAR_IPL: 3271 if (!dtrace_priv_kernel(state)) 3272 return (0); 3273 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3274 mstate->dtms_ipl = dtrace_getipl(); 3275 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3276 } 3277 return (mstate->dtms_ipl); 3278#endif 3279 3280 case DIF_VAR_EPID: 3281 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3282 return (mstate->dtms_epid); 3283 3284 case DIF_VAR_ID: 3285 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3286 return (mstate->dtms_probe->dtpr_id); 3287 3288 case DIF_VAR_STACKDEPTH: 3289 if (!dtrace_priv_kernel(state)) 3290 return (0); 3291 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3292 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3293 3294 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3295 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3296 } 3297 return (mstate->dtms_stackdepth); 3298 3299 case DIF_VAR_USTACKDEPTH: 3300 if (!dtrace_priv_proc(state)) 3301 return (0); 3302 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3303 /* 3304 * See comment in DIF_VAR_PID. 3305 */ 3306 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3307 CPU_ON_INTR(CPU)) { 3308 mstate->dtms_ustackdepth = 0; 3309 } else { 3310 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3311 mstate->dtms_ustackdepth = 3312 dtrace_getustackdepth(); 3313 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3314 } 3315 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3316 } 3317 return (mstate->dtms_ustackdepth); 3318 3319 case DIF_VAR_CALLER: 3320 if (!dtrace_priv_kernel(state)) 3321 return (0); 3322 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3323 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3324 3325 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3326 /* 3327 * If this is an unanchored probe, we are 3328 * required to go through the slow path: 3329 * dtrace_caller() only guarantees correct 3330 * results for anchored probes. 3331 */ 3332 pc_t caller[2] = {0, 0}; 3333 3334 dtrace_getpcstack(caller, 2, aframes, 3335 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3336 mstate->dtms_caller = caller[1]; 3337 } else if ((mstate->dtms_caller = 3338 dtrace_caller(aframes)) == -1) { 3339 /* 3340 * We have failed to do this the quick way; 3341 * we must resort to the slower approach of 3342 * calling dtrace_getpcstack(). 3343 */ 3344 pc_t caller = 0; 3345 3346 dtrace_getpcstack(&caller, 1, aframes, NULL); 3347 mstate->dtms_caller = caller; 3348 } 3349 3350 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3351 } 3352 return (mstate->dtms_caller); 3353 3354 case DIF_VAR_UCALLER: 3355 if (!dtrace_priv_proc(state)) 3356 return (0); 3357 3358 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3359 uint64_t ustack[3]; 3360 3361 /* 3362 * dtrace_getupcstack() fills in the first uint64_t 3363 * with the current PID. The second uint64_t will 3364 * be the program counter at user-level. The third 3365 * uint64_t will contain the caller, which is what 3366 * we're after. 3367 */ 3368 ustack[2] = 0; 3369 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3370 dtrace_getupcstack(ustack, 3); 3371 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3372 mstate->dtms_ucaller = ustack[2]; 3373 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3374 } 3375 3376 return (mstate->dtms_ucaller); 3377 3378 case DIF_VAR_PROBEPROV: 3379 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3380 return (dtrace_dif_varstr( 3381 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3382 state, mstate)); 3383 3384 case DIF_VAR_PROBEMOD: 3385 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3386 return (dtrace_dif_varstr( 3387 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3388 state, mstate)); 3389 3390 case DIF_VAR_PROBEFUNC: 3391 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3392 return (dtrace_dif_varstr( 3393 (uintptr_t)mstate->dtms_probe->dtpr_func, 3394 state, mstate)); 3395 3396 case DIF_VAR_PROBENAME: 3397 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3398 return (dtrace_dif_varstr( 3399 (uintptr_t)mstate->dtms_probe->dtpr_name, 3400 state, mstate)); 3401 3402 case DIF_VAR_PID: 3403 if (!dtrace_priv_proc(state)) 3404 return (0); 3405 3406#ifdef illumos 3407 /* 3408 * Note that we are assuming that an unanchored probe is 3409 * always due to a high-level interrupt. (And we're assuming 3410 * that there is only a single high level interrupt.) 3411 */ 3412 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3413 return (pid0.pid_id); 3414 3415 /* 3416 * It is always safe to dereference one's own t_procp pointer: 3417 * it always points to a valid, allocated proc structure. 3418 * Further, it is always safe to dereference the p_pidp member 3419 * of one's own proc structure. (These are truisms becuase 3420 * threads and processes don't clean up their own state -- 3421 * they leave that task to whomever reaps them.) 3422 */ 3423 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3424#else 3425 return ((uint64_t)curproc->p_pid); 3426#endif 3427 3428 case DIF_VAR_PPID: 3429 if (!dtrace_priv_proc(state)) 3430 return (0); 3431 3432#ifdef illumos 3433 /* 3434 * See comment in DIF_VAR_PID. 3435 */ 3436 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3437 return (pid0.pid_id); 3438 3439 /* 3440 * It is always safe to dereference one's own t_procp pointer: 3441 * it always points to a valid, allocated proc structure. 3442 * (This is true because threads don't clean up their own 3443 * state -- they leave that task to whomever reaps them.) 3444 */ 3445 return ((uint64_t)curthread->t_procp->p_ppid); 3446#else 3447 if (curproc->p_pid == proc0.p_pid) 3448 return (curproc->p_pid); 3449 else 3450 return (curproc->p_pptr->p_pid); 3451#endif 3452 3453 case DIF_VAR_TID: 3454#ifdef illumos 3455 /* 3456 * See comment in DIF_VAR_PID. 3457 */ 3458 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3459 return (0); 3460#endif 3461 3462 return ((uint64_t)curthread->t_tid); 3463 3464 case DIF_VAR_EXECARGS: { 3465 struct pargs *p_args = curthread->td_proc->p_args; 3466 3467 if (p_args == NULL) 3468 return(0); 3469 3470 return (dtrace_dif_varstrz( 3471 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3472 } 3473 3474 case DIF_VAR_EXECNAME: 3475#ifdef illumos 3476 if (!dtrace_priv_proc(state)) 3477 return (0); 3478 3479 /* 3480 * See comment in DIF_VAR_PID. 3481 */ 3482 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3483 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3484 3485 /* 3486 * It is always safe to dereference one's own t_procp pointer: 3487 * it always points to a valid, allocated proc structure. 3488 * (This is true because threads don't clean up their own 3489 * state -- they leave that task to whomever reaps them.) 3490 */ 3491 return (dtrace_dif_varstr( 3492 (uintptr_t)curthread->t_procp->p_user.u_comm, 3493 state, mstate)); 3494#else 3495 return (dtrace_dif_varstr( 3496 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3497#endif 3498 3499 case DIF_VAR_ZONENAME: 3500#ifdef illumos 3501 if (!dtrace_priv_proc(state)) 3502 return (0); 3503 3504 /* 3505 * See comment in DIF_VAR_PID. 3506 */ 3507 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3508 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3509 3510 /* 3511 * It is always safe to dereference one's own t_procp pointer: 3512 * it always points to a valid, allocated proc structure. 3513 * (This is true because threads don't clean up their own 3514 * state -- they leave that task to whomever reaps them.) 3515 */ 3516 return (dtrace_dif_varstr( 3517 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3518 state, mstate)); 3519#else 3520 return (0); 3521#endif 3522 3523 case DIF_VAR_UID: 3524 if (!dtrace_priv_proc(state)) 3525 return (0); 3526 3527#ifdef illumos 3528 /* 3529 * See comment in DIF_VAR_PID. 3530 */ 3531 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3532 return ((uint64_t)p0.p_cred->cr_uid); 3533 3534 /* 3535 * It is always safe to dereference one's own t_procp pointer: 3536 * it always points to a valid, allocated proc structure. 3537 * (This is true because threads don't clean up their own 3538 * state -- they leave that task to whomever reaps them.) 3539 * 3540 * Additionally, it is safe to dereference one's own process 3541 * credential, since this is never NULL after process birth. 3542 */ 3543 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3544#else 3545 return ((uint64_t)curthread->td_ucred->cr_uid); 3546#endif 3547 3548 case DIF_VAR_GID: 3549 if (!dtrace_priv_proc(state)) 3550 return (0); 3551 3552#ifdef illumos 3553 /* 3554 * See comment in DIF_VAR_PID. 3555 */ 3556 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3557 return ((uint64_t)p0.p_cred->cr_gid); 3558 3559 /* 3560 * It is always safe to dereference one's own t_procp pointer: 3561 * it always points to a valid, allocated proc structure. 3562 * (This is true because threads don't clean up their own 3563 * state -- they leave that task to whomever reaps them.) 3564 * 3565 * Additionally, it is safe to dereference one's own process 3566 * credential, since this is never NULL after process birth. 3567 */ 3568 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3569#else 3570 return ((uint64_t)curthread->td_ucred->cr_gid); 3571#endif 3572 3573 case DIF_VAR_ERRNO: { 3574#ifdef illumos 3575 klwp_t *lwp; 3576 if (!dtrace_priv_proc(state)) 3577 return (0); 3578 3579 /* 3580 * See comment in DIF_VAR_PID. 3581 */ 3582 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3583 return (0); 3584 3585 /* 3586 * It is always safe to dereference one's own t_lwp pointer in 3587 * the event that this pointer is non-NULL. (This is true 3588 * because threads and lwps don't clean up their own state -- 3589 * they leave that task to whomever reaps them.) 3590 */ 3591 if ((lwp = curthread->t_lwp) == NULL) 3592 return (0); 3593 3594 return ((uint64_t)lwp->lwp_errno); 3595#else 3596 return (curthread->td_errno); 3597#endif 3598 } 3599#ifndef illumos 3600 case DIF_VAR_CPU: { 3601 return curcpu; 3602 } 3603#endif 3604 default: 3605 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3606 return (0); 3607 } 3608} 3609 3610 3611typedef enum dtrace_json_state { 3612 DTRACE_JSON_REST = 1, 3613 DTRACE_JSON_OBJECT, 3614 DTRACE_JSON_STRING, 3615 DTRACE_JSON_STRING_ESCAPE, 3616 DTRACE_JSON_STRING_ESCAPE_UNICODE, 3617 DTRACE_JSON_COLON, 3618 DTRACE_JSON_COMMA, 3619 DTRACE_JSON_VALUE, 3620 DTRACE_JSON_IDENTIFIER, 3621 DTRACE_JSON_NUMBER, 3622 DTRACE_JSON_NUMBER_FRAC, 3623 DTRACE_JSON_NUMBER_EXP, 3624 DTRACE_JSON_COLLECT_OBJECT 3625} dtrace_json_state_t; 3626 3627/* 3628 * This function possesses just enough knowledge about JSON to extract a single 3629 * value from a JSON string and store it in the scratch buffer. It is able 3630 * to extract nested object values, and members of arrays by index. 3631 * 3632 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to 3633 * be looked up as we descend into the object tree. e.g. 3634 * 3635 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL 3636 * with nelems = 5. 3637 * 3638 * The run time of this function must be bounded above by strsize to limit the 3639 * amount of work done in probe context. As such, it is implemented as a 3640 * simple state machine, reading one character at a time using safe loads 3641 * until we find the requested element, hit a parsing error or run off the 3642 * end of the object or string. 3643 * 3644 * As there is no way for a subroutine to return an error without interrupting 3645 * clause execution, we simply return NULL in the event of a missing key or any 3646 * other error condition. Each NULL return in this function is commented with 3647 * the error condition it represents -- parsing or otherwise. 3648 * 3649 * The set of states for the state machine closely matches the JSON 3650 * specification (http://json.org/). Briefly: 3651 * 3652 * DTRACE_JSON_REST: 3653 * Skip whitespace until we find either a top-level Object, moving 3654 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE. 3655 * 3656 * DTRACE_JSON_OBJECT: 3657 * Locate the next key String in an Object. Sets a flag to denote 3658 * the next String as a key string and moves to DTRACE_JSON_STRING. 3659 * 3660 * DTRACE_JSON_COLON: 3661 * Skip whitespace until we find the colon that separates key Strings 3662 * from their values. Once found, move to DTRACE_JSON_VALUE. 3663 * 3664 * DTRACE_JSON_VALUE: 3665 * Detects the type of the next value (String, Number, Identifier, Object 3666 * or Array) and routes to the states that process that type. Here we also 3667 * deal with the element selector list if we are requested to traverse down 3668 * into the object tree. 3669 * 3670 * DTRACE_JSON_COMMA: 3671 * Skip whitespace until we find the comma that separates key-value pairs 3672 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays 3673 * (similarly DTRACE_JSON_VALUE). All following literal value processing 3674 * states return to this state at the end of their value, unless otherwise 3675 * noted. 3676 * 3677 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP: 3678 * Processes a Number literal from the JSON, including any exponent 3679 * component that may be present. Numbers are returned as strings, which 3680 * may be passed to strtoll() if an integer is required. 3681 * 3682 * DTRACE_JSON_IDENTIFIER: 3683 * Processes a "true", "false" or "null" literal in the JSON. 3684 * 3685 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE, 3686 * DTRACE_JSON_STRING_ESCAPE_UNICODE: 3687 * Processes a String literal from the JSON, whether the String denotes 3688 * a key, a value or part of a larger Object. Handles all escape sequences 3689 * present in the specification, including four-digit unicode characters, 3690 * but merely includes the escape sequence without converting it to the 3691 * actual escaped character. If the String is flagged as a key, we 3692 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA. 3693 * 3694 * DTRACE_JSON_COLLECT_OBJECT: 3695 * This state collects an entire Object (or Array), correctly handling 3696 * embedded strings. If the full element selector list matches this nested 3697 * object, we return the Object in full as a string. If not, we use this 3698 * state to skip to the next value at this level and continue processing. 3699 * 3700 * NOTE: This function uses various macros from strtolctype.h to manipulate 3701 * digit values, etc -- these have all been checked to ensure they make 3702 * no additional function calls. 3703 */ 3704static char * 3705dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems, 3706 char *dest) 3707{ 3708 dtrace_json_state_t state = DTRACE_JSON_REST; 3709 int64_t array_elem = INT64_MIN; 3710 int64_t array_pos = 0; 3711 uint8_t escape_unicount = 0; 3712 boolean_t string_is_key = B_FALSE; 3713 boolean_t collect_object = B_FALSE; 3714 boolean_t found_key = B_FALSE; 3715 boolean_t in_array = B_FALSE; 3716 uint32_t braces = 0, brackets = 0; 3717 char *elem = elemlist; 3718 char *dd = dest; 3719 uintptr_t cur; 3720 3721 for (cur = json; cur < json + size; cur++) { 3722 char cc = dtrace_load8(cur); 3723 if (cc == '\0') 3724 return (NULL); 3725 3726 switch (state) { 3727 case DTRACE_JSON_REST: 3728 if (isspace(cc)) 3729 break; 3730 3731 if (cc == '{') { 3732 state = DTRACE_JSON_OBJECT; 3733 break; 3734 } 3735 3736 if (cc == '[') { 3737 in_array = B_TRUE; 3738 array_pos = 0; 3739 array_elem = dtrace_strtoll(elem, 10, size); 3740 found_key = array_elem == 0 ? B_TRUE : B_FALSE; 3741 state = DTRACE_JSON_VALUE; 3742 break; 3743 } 3744 3745 /* 3746 * ERROR: expected to find a top-level object or array. 3747 */ 3748 return (NULL); 3749 case DTRACE_JSON_OBJECT: 3750 if (isspace(cc)) 3751 break; 3752 3753 if (cc == '"') { 3754 state = DTRACE_JSON_STRING; 3755 string_is_key = B_TRUE; 3756 break; 3757 } 3758 3759 /* 3760 * ERROR: either the object did not start with a key 3761 * string, or we've run off the end of the object 3762 * without finding the requested key. 3763 */ 3764 return (NULL); 3765 case DTRACE_JSON_STRING: 3766 if (cc == '\\') { 3767 *dd++ = '\\'; 3768 state = DTRACE_JSON_STRING_ESCAPE; 3769 break; 3770 } 3771 3772 if (cc == '"') { 3773 if (collect_object) { 3774 /* 3775 * We don't reset the dest here, as 3776 * the string is part of a larger 3777 * object being collected. 3778 */ 3779 *dd++ = cc; 3780 collect_object = B_FALSE; 3781 state = DTRACE_JSON_COLLECT_OBJECT; 3782 break; 3783 } 3784 *dd = '\0'; 3785 dd = dest; /* reset string buffer */ 3786 if (string_is_key) { 3787 if (dtrace_strncmp(dest, elem, 3788 size) == 0) 3789 found_key = B_TRUE; 3790 } else if (found_key) { 3791 if (nelems > 1) { 3792 /* 3793 * We expected an object, not 3794 * this string. 3795 */ 3796 return (NULL); 3797 } 3798 return (dest); 3799 } 3800 state = string_is_key ? DTRACE_JSON_COLON : 3801 DTRACE_JSON_COMMA; 3802 string_is_key = B_FALSE; 3803 break; 3804 } 3805 3806 *dd++ = cc; 3807 break; 3808 case DTRACE_JSON_STRING_ESCAPE: 3809 *dd++ = cc; 3810 if (cc == 'u') { 3811 escape_unicount = 0; 3812 state = DTRACE_JSON_STRING_ESCAPE_UNICODE; 3813 } else { 3814 state = DTRACE_JSON_STRING; 3815 } 3816 break; 3817 case DTRACE_JSON_STRING_ESCAPE_UNICODE: 3818 if (!isxdigit(cc)) { 3819 /* 3820 * ERROR: invalid unicode escape, expected 3821 * four valid hexidecimal digits. 3822 */ 3823 return (NULL); 3824 } 3825 3826 *dd++ = cc; 3827 if (++escape_unicount == 4) 3828 state = DTRACE_JSON_STRING; 3829 break; 3830 case DTRACE_JSON_COLON: 3831 if (isspace(cc)) 3832 break; 3833 3834 if (cc == ':') { 3835 state = DTRACE_JSON_VALUE; 3836 break; 3837 } 3838 3839 /* 3840 * ERROR: expected a colon. 3841 */ 3842 return (NULL); 3843 case DTRACE_JSON_COMMA: 3844 if (isspace(cc)) 3845 break; 3846 3847 if (cc == ',') { 3848 if (in_array) { 3849 state = DTRACE_JSON_VALUE; 3850 if (++array_pos == array_elem) 3851 found_key = B_TRUE; 3852 } else { 3853 state = DTRACE_JSON_OBJECT; 3854 } 3855 break; 3856 } 3857 3858 /* 3859 * ERROR: either we hit an unexpected character, or 3860 * we reached the end of the object or array without 3861 * finding the requested key. 3862 */ 3863 return (NULL); 3864 case DTRACE_JSON_IDENTIFIER: 3865 if (islower(cc)) { 3866 *dd++ = cc; 3867 break; 3868 } 3869 3870 *dd = '\0'; 3871 dd = dest; /* reset string buffer */ 3872 3873 if (dtrace_strncmp(dest, "true", 5) == 0 || 3874 dtrace_strncmp(dest, "false", 6) == 0 || 3875 dtrace_strncmp(dest, "null", 5) == 0) { 3876 if (found_key) { 3877 if (nelems > 1) { 3878 /* 3879 * ERROR: We expected an object, 3880 * not this identifier. 3881 */ 3882 return (NULL); 3883 } 3884 return (dest); 3885 } else { 3886 cur--; 3887 state = DTRACE_JSON_COMMA; 3888 break; 3889 } 3890 } 3891 3892 /* 3893 * ERROR: we did not recognise the identifier as one 3894 * of those in the JSON specification. 3895 */ 3896 return (NULL); 3897 case DTRACE_JSON_NUMBER: 3898 if (cc == '.') { 3899 *dd++ = cc; 3900 state = DTRACE_JSON_NUMBER_FRAC; 3901 break; 3902 } 3903 3904 if (cc == 'x' || cc == 'X') { 3905 /* 3906 * ERROR: specification explicitly excludes 3907 * hexidecimal or octal numbers. 3908 */ 3909 return (NULL); 3910 } 3911 3912 /* FALLTHRU */ 3913 case DTRACE_JSON_NUMBER_FRAC: 3914 if (cc == 'e' || cc == 'E') { 3915 *dd++ = cc; 3916 state = DTRACE_JSON_NUMBER_EXP; 3917 break; 3918 } 3919 3920 if (cc == '+' || cc == '-') { 3921 /* 3922 * ERROR: expect sign as part of exponent only. 3923 */ 3924 return (NULL); 3925 } 3926 /* FALLTHRU */ 3927 case DTRACE_JSON_NUMBER_EXP: 3928 if (isdigit(cc) || cc == '+' || cc == '-') { 3929 *dd++ = cc; 3930 break; 3931 } 3932 3933 *dd = '\0'; 3934 dd = dest; /* reset string buffer */ 3935 if (found_key) { 3936 if (nelems > 1) { 3937 /* 3938 * ERROR: We expected an object, not 3939 * this number. 3940 */ 3941 return (NULL); 3942 } 3943 return (dest); 3944 } 3945 3946 cur--; 3947 state = DTRACE_JSON_COMMA; 3948 break; 3949 case DTRACE_JSON_VALUE: 3950 if (isspace(cc)) 3951 break; 3952 3953 if (cc == '{' || cc == '[') { 3954 if (nelems > 1 && found_key) { 3955 in_array = cc == '[' ? B_TRUE : B_FALSE; 3956 /* 3957 * If our element selector directs us 3958 * to descend into this nested object, 3959 * then move to the next selector 3960 * element in the list and restart the 3961 * state machine. 3962 */ 3963 while (*elem != '\0') 3964 elem++; 3965 elem++; /* skip the inter-element NUL */ 3966 nelems--; 3967 dd = dest; 3968 if (in_array) { 3969 state = DTRACE_JSON_VALUE; 3970 array_pos = 0; 3971 array_elem = dtrace_strtoll( 3972 elem, 10, size); 3973 found_key = array_elem == 0 ? 3974 B_TRUE : B_FALSE; 3975 } else { 3976 found_key = B_FALSE; 3977 state = DTRACE_JSON_OBJECT; 3978 } 3979 break; 3980 } 3981 3982 /* 3983 * Otherwise, we wish to either skip this 3984 * nested object or return it in full. 3985 */ 3986 if (cc == '[') 3987 brackets = 1; 3988 else 3989 braces = 1; 3990 *dd++ = cc; 3991 state = DTRACE_JSON_COLLECT_OBJECT; 3992 break; 3993 } 3994 3995 if (cc == '"') { 3996 state = DTRACE_JSON_STRING; 3997 break; 3998 } 3999 4000 if (islower(cc)) { 4001 /* 4002 * Here we deal with true, false and null. 4003 */ 4004 *dd++ = cc; 4005 state = DTRACE_JSON_IDENTIFIER; 4006 break; 4007 } 4008 4009 if (cc == '-' || isdigit(cc)) { 4010 *dd++ = cc; 4011 state = DTRACE_JSON_NUMBER; 4012 break; 4013 } 4014 4015 /* 4016 * ERROR: unexpected character at start of value. 4017 */ 4018 return (NULL); 4019 case DTRACE_JSON_COLLECT_OBJECT: 4020 if (cc == '\0') 4021 /* 4022 * ERROR: unexpected end of input. 4023 */ 4024 return (NULL); 4025 4026 *dd++ = cc; 4027 if (cc == '"') { 4028 collect_object = B_TRUE; 4029 state = DTRACE_JSON_STRING; 4030 break; 4031 } 4032 4033 if (cc == ']') { 4034 if (brackets-- == 0) { 4035 /* 4036 * ERROR: unbalanced brackets. 4037 */ 4038 return (NULL); 4039 } 4040 } else if (cc == '}') { 4041 if (braces-- == 0) { 4042 /* 4043 * ERROR: unbalanced braces. 4044 */ 4045 return (NULL); 4046 } 4047 } else if (cc == '{') { 4048 braces++; 4049 } else if (cc == '[') { 4050 brackets++; 4051 } 4052 4053 if (brackets == 0 && braces == 0) { 4054 if (found_key) { 4055 *dd = '\0'; 4056 return (dest); 4057 } 4058 dd = dest; /* reset string buffer */ 4059 state = DTRACE_JSON_COMMA; 4060 } 4061 break; 4062 } 4063 } 4064 return (NULL); 4065} 4066 4067/* 4068 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 4069 * Notice that we don't bother validating the proper number of arguments or 4070 * their types in the tuple stack. This isn't needed because all argument 4071 * interpretation is safe because of our load safety -- the worst that can 4072 * happen is that a bogus program can obtain bogus results. 4073 */ 4074static void 4075dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 4076 dtrace_key_t *tupregs, int nargs, 4077 dtrace_mstate_t *mstate, dtrace_state_t *state) 4078{ 4079 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4080 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4081 dtrace_vstate_t *vstate = &state->dts_vstate; 4082 4083#ifdef illumos 4084 union { 4085 mutex_impl_t mi; 4086 uint64_t mx; 4087 } m; 4088 4089 union { 4090 krwlock_t ri; 4091 uintptr_t rw; 4092 } r; 4093#else 4094 struct thread *lowner; 4095 union { 4096 struct lock_object *li; 4097 uintptr_t lx; 4098 } l; 4099#endif 4100 4101 switch (subr) { 4102 case DIF_SUBR_RAND: 4103 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 4104 break; 4105 4106#ifdef illumos 4107 case DIF_SUBR_MUTEX_OWNED: 4108 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4109 mstate, vstate)) { 4110 regs[rd] = 0; 4111 break; 4112 } 4113 4114 m.mx = dtrace_load64(tupregs[0].dttk_value); 4115 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 4116 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 4117 else 4118 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 4119 break; 4120 4121 case DIF_SUBR_MUTEX_OWNER: 4122 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4123 mstate, vstate)) { 4124 regs[rd] = 0; 4125 break; 4126 } 4127 4128 m.mx = dtrace_load64(tupregs[0].dttk_value); 4129 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 4130 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 4131 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 4132 else 4133 regs[rd] = 0; 4134 break; 4135 4136 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4137 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4138 mstate, vstate)) { 4139 regs[rd] = 0; 4140 break; 4141 } 4142 4143 m.mx = dtrace_load64(tupregs[0].dttk_value); 4144 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 4145 break; 4146 4147 case DIF_SUBR_MUTEX_TYPE_SPIN: 4148 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4149 mstate, vstate)) { 4150 regs[rd] = 0; 4151 break; 4152 } 4153 4154 m.mx = dtrace_load64(tupregs[0].dttk_value); 4155 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 4156 break; 4157 4158 case DIF_SUBR_RW_READ_HELD: { 4159 uintptr_t tmp; 4160 4161 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4162 mstate, vstate)) { 4163 regs[rd] = 0; 4164 break; 4165 } 4166 4167 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4168 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 4169 break; 4170 } 4171 4172 case DIF_SUBR_RW_WRITE_HELD: 4173 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4174 mstate, vstate)) { 4175 regs[rd] = 0; 4176 break; 4177 } 4178 4179 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4180 regs[rd] = _RW_WRITE_HELD(&r.ri); 4181 break; 4182 4183 case DIF_SUBR_RW_ISWRITER: 4184 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4185 mstate, vstate)) { 4186 regs[rd] = 0; 4187 break; 4188 } 4189 4190 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4191 regs[rd] = _RW_ISWRITER(&r.ri); 4192 break; 4193 4194#else /* !illumos */ 4195 case DIF_SUBR_MUTEX_OWNED: 4196 if (!dtrace_canload(tupregs[0].dttk_value, 4197 sizeof (struct lock_object), mstate, vstate)) { 4198 regs[rd] = 0; 4199 break; 4200 } 4201 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4202 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4203 break; 4204 4205 case DIF_SUBR_MUTEX_OWNER: 4206 if (!dtrace_canload(tupregs[0].dttk_value, 4207 sizeof (struct lock_object), mstate, vstate)) { 4208 regs[rd] = 0; 4209 break; 4210 } 4211 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4212 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4213 regs[rd] = (uintptr_t)lowner; 4214 break; 4215 4216 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4217 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4218 mstate, vstate)) { 4219 regs[rd] = 0; 4220 break; 4221 } 4222 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4223 /* XXX - should be only LC_SLEEPABLE? */ 4224 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & 4225 (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0; 4226 break; 4227 4228 case DIF_SUBR_MUTEX_TYPE_SPIN: 4229 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4230 mstate, vstate)) { 4231 regs[rd] = 0; 4232 break; 4233 } 4234 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4235 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 4236 break; 4237 4238 case DIF_SUBR_RW_READ_HELD: 4239 case DIF_SUBR_SX_SHARED_HELD: 4240 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4241 mstate, vstate)) { 4242 regs[rd] = 0; 4243 break; 4244 } 4245 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4246 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4247 lowner == NULL; 4248 break; 4249 4250 case DIF_SUBR_RW_WRITE_HELD: 4251 case DIF_SUBR_SX_EXCLUSIVE_HELD: 4252 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4253 mstate, vstate)) { 4254 regs[rd] = 0; 4255 break; 4256 } 4257 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4258 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4259 regs[rd] = (lowner == curthread); 4260 break; 4261 4262 case DIF_SUBR_RW_ISWRITER: 4263 case DIF_SUBR_SX_ISEXCLUSIVE: 4264 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4265 mstate, vstate)) { 4266 regs[rd] = 0; 4267 break; 4268 } 4269 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4270 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4271 lowner != NULL; 4272 break; 4273#endif /* illumos */ 4274 4275 case DIF_SUBR_BCOPY: { 4276 /* 4277 * We need to be sure that the destination is in the scratch 4278 * region -- no other region is allowed. 4279 */ 4280 uintptr_t src = tupregs[0].dttk_value; 4281 uintptr_t dest = tupregs[1].dttk_value; 4282 size_t size = tupregs[2].dttk_value; 4283 4284 if (!dtrace_inscratch(dest, size, mstate)) { 4285 *flags |= CPU_DTRACE_BADADDR; 4286 *illval = regs[rd]; 4287 break; 4288 } 4289 4290 if (!dtrace_canload(src, size, mstate, vstate)) { 4291 regs[rd] = 0; 4292 break; 4293 } 4294 4295 dtrace_bcopy((void *)src, (void *)dest, size); 4296 break; 4297 } 4298 4299 case DIF_SUBR_ALLOCA: 4300 case DIF_SUBR_COPYIN: { 4301 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4302 uint64_t size = 4303 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 4304 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 4305 4306 /* 4307 * This action doesn't require any credential checks since 4308 * probes will not activate in user contexts to which the 4309 * enabling user does not have permissions. 4310 */ 4311 4312 /* 4313 * Rounding up the user allocation size could have overflowed 4314 * a large, bogus allocation (like -1ULL) to 0. 4315 */ 4316 if (scratch_size < size || 4317 !DTRACE_INSCRATCH(mstate, scratch_size)) { 4318 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4319 regs[rd] = 0; 4320 break; 4321 } 4322 4323 if (subr == DIF_SUBR_COPYIN) { 4324 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4325 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4326 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4327 } 4328 4329 mstate->dtms_scratch_ptr += scratch_size; 4330 regs[rd] = dest; 4331 break; 4332 } 4333 4334 case DIF_SUBR_COPYINTO: { 4335 uint64_t size = tupregs[1].dttk_value; 4336 uintptr_t dest = tupregs[2].dttk_value; 4337 4338 /* 4339 * This action doesn't require any credential checks since 4340 * probes will not activate in user contexts to which the 4341 * enabling user does not have permissions. 4342 */ 4343 if (!dtrace_inscratch(dest, size, mstate)) { 4344 *flags |= CPU_DTRACE_BADADDR; 4345 *illval = regs[rd]; 4346 break; 4347 } 4348 4349 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4350 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4351 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4352 break; 4353 } 4354 4355 case DIF_SUBR_COPYINSTR: { 4356 uintptr_t dest = mstate->dtms_scratch_ptr; 4357 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4358 4359 if (nargs > 1 && tupregs[1].dttk_value < size) 4360 size = tupregs[1].dttk_value + 1; 4361 4362 /* 4363 * This action doesn't require any credential checks since 4364 * probes will not activate in user contexts to which the 4365 * enabling user does not have permissions. 4366 */ 4367 if (!DTRACE_INSCRATCH(mstate, size)) { 4368 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4369 regs[rd] = 0; 4370 break; 4371 } 4372 4373 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4374 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 4375 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4376 4377 ((char *)dest)[size - 1] = '\0'; 4378 mstate->dtms_scratch_ptr += size; 4379 regs[rd] = dest; 4380 break; 4381 } 4382 4383#ifdef illumos 4384 case DIF_SUBR_MSGSIZE: 4385 case DIF_SUBR_MSGDSIZE: { 4386 uintptr_t baddr = tupregs[0].dttk_value, daddr; 4387 uintptr_t wptr, rptr; 4388 size_t count = 0; 4389 int cont = 0; 4390 4391 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 4392 4393 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 4394 vstate)) { 4395 regs[rd] = 0; 4396 break; 4397 } 4398 4399 wptr = dtrace_loadptr(baddr + 4400 offsetof(mblk_t, b_wptr)); 4401 4402 rptr = dtrace_loadptr(baddr + 4403 offsetof(mblk_t, b_rptr)); 4404 4405 if (wptr < rptr) { 4406 *flags |= CPU_DTRACE_BADADDR; 4407 *illval = tupregs[0].dttk_value; 4408 break; 4409 } 4410 4411 daddr = dtrace_loadptr(baddr + 4412 offsetof(mblk_t, b_datap)); 4413 4414 baddr = dtrace_loadptr(baddr + 4415 offsetof(mblk_t, b_cont)); 4416 4417 /* 4418 * We want to prevent against denial-of-service here, 4419 * so we're only going to search the list for 4420 * dtrace_msgdsize_max mblks. 4421 */ 4422 if (cont++ > dtrace_msgdsize_max) { 4423 *flags |= CPU_DTRACE_ILLOP; 4424 break; 4425 } 4426 4427 if (subr == DIF_SUBR_MSGDSIZE) { 4428 if (dtrace_load8(daddr + 4429 offsetof(dblk_t, db_type)) != M_DATA) 4430 continue; 4431 } 4432 4433 count += wptr - rptr; 4434 } 4435 4436 if (!(*flags & CPU_DTRACE_FAULT)) 4437 regs[rd] = count; 4438 4439 break; 4440 } 4441#endif 4442 4443 case DIF_SUBR_PROGENYOF: { 4444 pid_t pid = tupregs[0].dttk_value; 4445 proc_t *p; 4446 int rval = 0; 4447 4448 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4449 4450 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 4451#ifdef illumos 4452 if (p->p_pidp->pid_id == pid) { 4453#else 4454 if (p->p_pid == pid) { 4455#endif 4456 rval = 1; 4457 break; 4458 } 4459 } 4460 4461 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4462 4463 regs[rd] = rval; 4464 break; 4465 } 4466 4467 case DIF_SUBR_SPECULATION: 4468 regs[rd] = dtrace_speculation(state); 4469 break; 4470 4471 case DIF_SUBR_COPYOUT: { 4472 uintptr_t kaddr = tupregs[0].dttk_value; 4473 uintptr_t uaddr = tupregs[1].dttk_value; 4474 uint64_t size = tupregs[2].dttk_value; 4475 4476 if (!dtrace_destructive_disallow && 4477 dtrace_priv_proc_control(state) && 4478 !dtrace_istoxic(kaddr, size) && 4479 dtrace_canload(kaddr, size, mstate, vstate)) { 4480 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4481 dtrace_copyout(kaddr, uaddr, size, flags); 4482 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4483 } 4484 break; 4485 } 4486 4487 case DIF_SUBR_COPYOUTSTR: { 4488 uintptr_t kaddr = tupregs[0].dttk_value; 4489 uintptr_t uaddr = tupregs[1].dttk_value; 4490 uint64_t size = tupregs[2].dttk_value; 4491 4492 if (!dtrace_destructive_disallow && 4493 dtrace_priv_proc_control(state) && 4494 !dtrace_istoxic(kaddr, size) && 4495 dtrace_strcanload(kaddr, size, mstate, vstate)) { 4496 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4497 dtrace_copyoutstr(kaddr, uaddr, size, flags); 4498 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4499 } 4500 break; 4501 } 4502 4503 case DIF_SUBR_STRLEN: { 4504 size_t sz; 4505 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 4506 sz = dtrace_strlen((char *)addr, 4507 state->dts_options[DTRACEOPT_STRSIZE]); 4508 4509 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 4510 regs[rd] = 0; 4511 break; 4512 } 4513 4514 regs[rd] = sz; 4515 4516 break; 4517 } 4518 4519 case DIF_SUBR_STRCHR: 4520 case DIF_SUBR_STRRCHR: { 4521 /* 4522 * We're going to iterate over the string looking for the 4523 * specified character. We will iterate until we have reached 4524 * the string length or we have found the character. If this 4525 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 4526 * of the specified character instead of the first. 4527 */ 4528 uintptr_t saddr = tupregs[0].dttk_value; 4529 uintptr_t addr = tupregs[0].dttk_value; 4530 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 4531 char c, target = (char)tupregs[1].dttk_value; 4532 4533 for (regs[rd] = 0; addr < limit; addr++) { 4534 if ((c = dtrace_load8(addr)) == target) { 4535 regs[rd] = addr; 4536 4537 if (subr == DIF_SUBR_STRCHR) 4538 break; 4539 } 4540 4541 if (c == '\0') 4542 break; 4543 } 4544 4545 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 4546 regs[rd] = 0; 4547 break; 4548 } 4549 4550 break; 4551 } 4552 4553 case DIF_SUBR_STRSTR: 4554 case DIF_SUBR_INDEX: 4555 case DIF_SUBR_RINDEX: { 4556 /* 4557 * We're going to iterate over the string looking for the 4558 * specified string. We will iterate until we have reached 4559 * the string length or we have found the string. (Yes, this 4560 * is done in the most naive way possible -- but considering 4561 * that the string we're searching for is likely to be 4562 * relatively short, the complexity of Rabin-Karp or similar 4563 * hardly seems merited.) 4564 */ 4565 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 4566 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 4567 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4568 size_t len = dtrace_strlen(addr, size); 4569 size_t sublen = dtrace_strlen(substr, size); 4570 char *limit = addr + len, *orig = addr; 4571 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 4572 int inc = 1; 4573 4574 regs[rd] = notfound; 4575 4576 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 4577 regs[rd] = 0; 4578 break; 4579 } 4580 4581 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 4582 vstate)) { 4583 regs[rd] = 0; 4584 break; 4585 } 4586 4587 /* 4588 * strstr() and index()/rindex() have similar semantics if 4589 * both strings are the empty string: strstr() returns a 4590 * pointer to the (empty) string, and index() and rindex() 4591 * both return index 0 (regardless of any position argument). 4592 */ 4593 if (sublen == 0 && len == 0) { 4594 if (subr == DIF_SUBR_STRSTR) 4595 regs[rd] = (uintptr_t)addr; 4596 else 4597 regs[rd] = 0; 4598 break; 4599 } 4600 4601 if (subr != DIF_SUBR_STRSTR) { 4602 if (subr == DIF_SUBR_RINDEX) { 4603 limit = orig - 1; 4604 addr += len; 4605 inc = -1; 4606 } 4607 4608 /* 4609 * Both index() and rindex() take an optional position 4610 * argument that denotes the starting position. 4611 */ 4612 if (nargs == 3) { 4613 int64_t pos = (int64_t)tupregs[2].dttk_value; 4614 4615 /* 4616 * If the position argument to index() is 4617 * negative, Perl implicitly clamps it at 4618 * zero. This semantic is a little surprising 4619 * given the special meaning of negative 4620 * positions to similar Perl functions like 4621 * substr(), but it appears to reflect a 4622 * notion that index() can start from a 4623 * negative index and increment its way up to 4624 * the string. Given this notion, Perl's 4625 * rindex() is at least self-consistent in 4626 * that it implicitly clamps positions greater 4627 * than the string length to be the string 4628 * length. Where Perl completely loses 4629 * coherence, however, is when the specified 4630 * substring is the empty string (""). In 4631 * this case, even if the position is 4632 * negative, rindex() returns 0 -- and even if 4633 * the position is greater than the length, 4634 * index() returns the string length. These 4635 * semantics violate the notion that index() 4636 * should never return a value less than the 4637 * specified position and that rindex() should 4638 * never return a value greater than the 4639 * specified position. (One assumes that 4640 * these semantics are artifacts of Perl's 4641 * implementation and not the results of 4642 * deliberate design -- it beggars belief that 4643 * even Larry Wall could desire such oddness.) 4644 * While in the abstract one would wish for 4645 * consistent position semantics across 4646 * substr(), index() and rindex() -- or at the 4647 * very least self-consistent position 4648 * semantics for index() and rindex() -- we 4649 * instead opt to keep with the extant Perl 4650 * semantics, in all their broken glory. (Do 4651 * we have more desire to maintain Perl's 4652 * semantics than Perl does? Probably.) 4653 */ 4654 if (subr == DIF_SUBR_RINDEX) { 4655 if (pos < 0) { 4656 if (sublen == 0) 4657 regs[rd] = 0; 4658 break; 4659 } 4660 4661 if (pos > len) 4662 pos = len; 4663 } else { 4664 if (pos < 0) 4665 pos = 0; 4666 4667 if (pos >= len) { 4668 if (sublen == 0) 4669 regs[rd] = len; 4670 break; 4671 } 4672 } 4673 4674 addr = orig + pos; 4675 } 4676 } 4677 4678 for (regs[rd] = notfound; addr != limit; addr += inc) { 4679 if (dtrace_strncmp(addr, substr, sublen) == 0) { 4680 if (subr != DIF_SUBR_STRSTR) { 4681 /* 4682 * As D index() and rindex() are 4683 * modeled on Perl (and not on awk), 4684 * we return a zero-based (and not a 4685 * one-based) index. (For you Perl 4686 * weenies: no, we're not going to add 4687 * $[ -- and shouldn't you be at a con 4688 * or something?) 4689 */ 4690 regs[rd] = (uintptr_t)(addr - orig); 4691 break; 4692 } 4693 4694 ASSERT(subr == DIF_SUBR_STRSTR); 4695 regs[rd] = (uintptr_t)addr; 4696 break; 4697 } 4698 } 4699 4700 break; 4701 } 4702 4703 case DIF_SUBR_STRTOK: { 4704 uintptr_t addr = tupregs[0].dttk_value; 4705 uintptr_t tokaddr = tupregs[1].dttk_value; 4706 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4707 uintptr_t limit, toklimit = tokaddr + size; 4708 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 4709 char *dest = (char *)mstate->dtms_scratch_ptr; 4710 int i; 4711 4712 /* 4713 * Check both the token buffer and (later) the input buffer, 4714 * since both could be non-scratch addresses. 4715 */ 4716 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 4717 regs[rd] = 0; 4718 break; 4719 } 4720 4721 if (!DTRACE_INSCRATCH(mstate, size)) { 4722 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4723 regs[rd] = 0; 4724 break; 4725 } 4726 4727 if (addr == 0) { 4728 /* 4729 * If the address specified is NULL, we use our saved 4730 * strtok pointer from the mstate. Note that this 4731 * means that the saved strtok pointer is _only_ 4732 * valid within multiple enablings of the same probe -- 4733 * it behaves like an implicit clause-local variable. 4734 */ 4735 addr = mstate->dtms_strtok; 4736 } else { 4737 /* 4738 * If the user-specified address is non-NULL we must 4739 * access check it. This is the only time we have 4740 * a chance to do so, since this address may reside 4741 * in the string table of this clause-- future calls 4742 * (when we fetch addr from mstate->dtms_strtok) 4743 * would fail this access check. 4744 */ 4745 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 4746 regs[rd] = 0; 4747 break; 4748 } 4749 } 4750 4751 /* 4752 * First, zero the token map, and then process the token 4753 * string -- setting a bit in the map for every character 4754 * found in the token string. 4755 */ 4756 for (i = 0; i < sizeof (tokmap); i++) 4757 tokmap[i] = 0; 4758 4759 for (; tokaddr < toklimit; tokaddr++) { 4760 if ((c = dtrace_load8(tokaddr)) == '\0') 4761 break; 4762 4763 ASSERT((c >> 3) < sizeof (tokmap)); 4764 tokmap[c >> 3] |= (1 << (c & 0x7)); 4765 } 4766 4767 for (limit = addr + size; addr < limit; addr++) { 4768 /* 4769 * We're looking for a character that is _not_ contained 4770 * in the token string. 4771 */ 4772 if ((c = dtrace_load8(addr)) == '\0') 4773 break; 4774 4775 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 4776 break; 4777 } 4778 4779 if (c == '\0') { 4780 /* 4781 * We reached the end of the string without finding 4782 * any character that was not in the token string. 4783 * We return NULL in this case, and we set the saved 4784 * address to NULL as well. 4785 */ 4786 regs[rd] = 0; 4787 mstate->dtms_strtok = 0; 4788 break; 4789 } 4790 4791 /* 4792 * From here on, we're copying into the destination string. 4793 */ 4794 for (i = 0; addr < limit && i < size - 1; addr++) { 4795 if ((c = dtrace_load8(addr)) == '\0') 4796 break; 4797 4798 if (tokmap[c >> 3] & (1 << (c & 0x7))) 4799 break; 4800 4801 ASSERT(i < size); 4802 dest[i++] = c; 4803 } 4804 4805 ASSERT(i < size); 4806 dest[i] = '\0'; 4807 regs[rd] = (uintptr_t)dest; 4808 mstate->dtms_scratch_ptr += size; 4809 mstate->dtms_strtok = addr; 4810 break; 4811 } 4812 4813 case DIF_SUBR_SUBSTR: { 4814 uintptr_t s = tupregs[0].dttk_value; 4815 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4816 char *d = (char *)mstate->dtms_scratch_ptr; 4817 int64_t index = (int64_t)tupregs[1].dttk_value; 4818 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4819 size_t len = dtrace_strlen((char *)s, size); 4820 int64_t i; 4821 4822 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4823 regs[rd] = 0; 4824 break; 4825 } 4826 4827 if (!DTRACE_INSCRATCH(mstate, size)) { 4828 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4829 regs[rd] = 0; 4830 break; 4831 } 4832 4833 if (nargs <= 2) 4834 remaining = (int64_t)size; 4835 4836 if (index < 0) { 4837 index += len; 4838 4839 if (index < 0 && index + remaining > 0) { 4840 remaining += index; 4841 index = 0; 4842 } 4843 } 4844 4845 if (index >= len || index < 0) { 4846 remaining = 0; 4847 } else if (remaining < 0) { 4848 remaining += len - index; 4849 } else if (index + remaining > size) { 4850 remaining = size - index; 4851 } 4852 4853 for (i = 0; i < remaining; i++) { 4854 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4855 break; 4856 } 4857 4858 d[i] = '\0'; 4859 4860 mstate->dtms_scratch_ptr += size; 4861 regs[rd] = (uintptr_t)d; 4862 break; 4863 } 4864 4865 case DIF_SUBR_JSON: { 4866 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4867 uintptr_t json = tupregs[0].dttk_value; 4868 size_t jsonlen = dtrace_strlen((char *)json, size); 4869 uintptr_t elem = tupregs[1].dttk_value; 4870 size_t elemlen = dtrace_strlen((char *)elem, size); 4871 4872 char *dest = (char *)mstate->dtms_scratch_ptr; 4873 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1; 4874 char *ee = elemlist; 4875 int nelems = 1; 4876 uintptr_t cur; 4877 4878 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) || 4879 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) { 4880 regs[rd] = 0; 4881 break; 4882 } 4883 4884 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) { 4885 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4886 regs[rd] = 0; 4887 break; 4888 } 4889 4890 /* 4891 * Read the element selector and split it up into a packed list 4892 * of strings. 4893 */ 4894 for (cur = elem; cur < elem + elemlen; cur++) { 4895 char cc = dtrace_load8(cur); 4896 4897 if (cur == elem && cc == '[') { 4898 /* 4899 * If the first element selector key is 4900 * actually an array index then ignore the 4901 * bracket. 4902 */ 4903 continue; 4904 } 4905 4906 if (cc == ']') 4907 continue; 4908 4909 if (cc == '.' || cc == '[') { 4910 nelems++; 4911 cc = '\0'; 4912 } 4913 4914 *ee++ = cc; 4915 } 4916 *ee++ = '\0'; 4917 4918 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist, 4919 nelems, dest)) != 0) 4920 mstate->dtms_scratch_ptr += jsonlen + 1; 4921 break; 4922 } 4923 4924 case DIF_SUBR_TOUPPER: 4925 case DIF_SUBR_TOLOWER: { 4926 uintptr_t s = tupregs[0].dttk_value; 4927 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4928 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4929 size_t len = dtrace_strlen((char *)s, size); 4930 char lower, upper, convert; 4931 int64_t i; 4932 4933 if (subr == DIF_SUBR_TOUPPER) { 4934 lower = 'a'; 4935 upper = 'z'; 4936 convert = 'A'; 4937 } else { 4938 lower = 'A'; 4939 upper = 'Z'; 4940 convert = 'a'; 4941 } 4942 4943 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4944 regs[rd] = 0; 4945 break; 4946 } 4947 4948 if (!DTRACE_INSCRATCH(mstate, size)) { 4949 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4950 regs[rd] = 0; 4951 break; 4952 } 4953 4954 for (i = 0; i < size - 1; i++) { 4955 if ((c = dtrace_load8(s + i)) == '\0') 4956 break; 4957 4958 if (c >= lower && c <= upper) 4959 c = convert + (c - lower); 4960 4961 dest[i] = c; 4962 } 4963 4964 ASSERT(i < size); 4965 dest[i] = '\0'; 4966 regs[rd] = (uintptr_t)dest; 4967 mstate->dtms_scratch_ptr += size; 4968 break; 4969 } 4970 4971#ifdef illumos 4972 case DIF_SUBR_GETMAJOR: 4973#ifdef _LP64 4974 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 4975#else 4976 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 4977#endif 4978 break; 4979 4980 case DIF_SUBR_GETMINOR: 4981#ifdef _LP64 4982 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 4983#else 4984 regs[rd] = tupregs[0].dttk_value & MAXMIN; 4985#endif 4986 break; 4987 4988 case DIF_SUBR_DDI_PATHNAME: { 4989 /* 4990 * This one is a galactic mess. We are going to roughly 4991 * emulate ddi_pathname(), but it's made more complicated 4992 * by the fact that we (a) want to include the minor name and 4993 * (b) must proceed iteratively instead of recursively. 4994 */ 4995 uintptr_t dest = mstate->dtms_scratch_ptr; 4996 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4997 char *start = (char *)dest, *end = start + size - 1; 4998 uintptr_t daddr = tupregs[0].dttk_value; 4999 int64_t minor = (int64_t)tupregs[1].dttk_value; 5000 char *s; 5001 int i, len, depth = 0; 5002 5003 /* 5004 * Due to all the pointer jumping we do and context we must 5005 * rely upon, we just mandate that the user must have kernel 5006 * read privileges to use this routine. 5007 */ 5008 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 5009 *flags |= CPU_DTRACE_KPRIV; 5010 *illval = daddr; 5011 regs[rd] = 0; 5012 } 5013 5014 if (!DTRACE_INSCRATCH(mstate, size)) { 5015 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5016 regs[rd] = 0; 5017 break; 5018 } 5019 5020 *end = '\0'; 5021 5022 /* 5023 * We want to have a name for the minor. In order to do this, 5024 * we need to walk the minor list from the devinfo. We want 5025 * to be sure that we don't infinitely walk a circular list, 5026 * so we check for circularity by sending a scout pointer 5027 * ahead two elements for every element that we iterate over; 5028 * if the list is circular, these will ultimately point to the 5029 * same element. You may recognize this little trick as the 5030 * answer to a stupid interview question -- one that always 5031 * seems to be asked by those who had to have it laboriously 5032 * explained to them, and who can't even concisely describe 5033 * the conditions under which one would be forced to resort to 5034 * this technique. Needless to say, those conditions are 5035 * found here -- and probably only here. Is this the only use 5036 * of this infamous trick in shipping, production code? If it 5037 * isn't, it probably should be... 5038 */ 5039 if (minor != -1) { 5040 uintptr_t maddr = dtrace_loadptr(daddr + 5041 offsetof(struct dev_info, devi_minor)); 5042 5043 uintptr_t next = offsetof(struct ddi_minor_data, next); 5044 uintptr_t name = offsetof(struct ddi_minor_data, 5045 d_minor) + offsetof(struct ddi_minor, name); 5046 uintptr_t dev = offsetof(struct ddi_minor_data, 5047 d_minor) + offsetof(struct ddi_minor, dev); 5048 uintptr_t scout; 5049 5050 if (maddr != NULL) 5051 scout = dtrace_loadptr(maddr + next); 5052 5053 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5054 uint64_t m; 5055#ifdef _LP64 5056 m = dtrace_load64(maddr + dev) & MAXMIN64; 5057#else 5058 m = dtrace_load32(maddr + dev) & MAXMIN; 5059#endif 5060 if (m != minor) { 5061 maddr = dtrace_loadptr(maddr + next); 5062 5063 if (scout == NULL) 5064 continue; 5065 5066 scout = dtrace_loadptr(scout + next); 5067 5068 if (scout == NULL) 5069 continue; 5070 5071 scout = dtrace_loadptr(scout + next); 5072 5073 if (scout == NULL) 5074 continue; 5075 5076 if (scout == maddr) { 5077 *flags |= CPU_DTRACE_ILLOP; 5078 break; 5079 } 5080 5081 continue; 5082 } 5083 5084 /* 5085 * We have the minor data. Now we need to 5086 * copy the minor's name into the end of the 5087 * pathname. 5088 */ 5089 s = (char *)dtrace_loadptr(maddr + name); 5090 len = dtrace_strlen(s, size); 5091 5092 if (*flags & CPU_DTRACE_FAULT) 5093 break; 5094 5095 if (len != 0) { 5096 if ((end -= (len + 1)) < start) 5097 break; 5098 5099 *end = ':'; 5100 } 5101 5102 for (i = 1; i <= len; i++) 5103 end[i] = dtrace_load8((uintptr_t)s++); 5104 break; 5105 } 5106 } 5107 5108 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5109 ddi_node_state_t devi_state; 5110 5111 devi_state = dtrace_load32(daddr + 5112 offsetof(struct dev_info, devi_node_state)); 5113 5114 if (*flags & CPU_DTRACE_FAULT) 5115 break; 5116 5117 if (devi_state >= DS_INITIALIZED) { 5118 s = (char *)dtrace_loadptr(daddr + 5119 offsetof(struct dev_info, devi_addr)); 5120 len = dtrace_strlen(s, size); 5121 5122 if (*flags & CPU_DTRACE_FAULT) 5123 break; 5124 5125 if (len != 0) { 5126 if ((end -= (len + 1)) < start) 5127 break; 5128 5129 *end = '@'; 5130 } 5131 5132 for (i = 1; i <= len; i++) 5133 end[i] = dtrace_load8((uintptr_t)s++); 5134 } 5135 5136 /* 5137 * Now for the node name... 5138 */ 5139 s = (char *)dtrace_loadptr(daddr + 5140 offsetof(struct dev_info, devi_node_name)); 5141 5142 daddr = dtrace_loadptr(daddr + 5143 offsetof(struct dev_info, devi_parent)); 5144 5145 /* 5146 * If our parent is NULL (that is, if we're the root 5147 * node), we're going to use the special path 5148 * "devices". 5149 */ 5150 if (daddr == 0) 5151 s = "devices"; 5152 5153 len = dtrace_strlen(s, size); 5154 if (*flags & CPU_DTRACE_FAULT) 5155 break; 5156 5157 if ((end -= (len + 1)) < start) 5158 break; 5159 5160 for (i = 1; i <= len; i++) 5161 end[i] = dtrace_load8((uintptr_t)s++); 5162 *end = '/'; 5163 5164 if (depth++ > dtrace_devdepth_max) { 5165 *flags |= CPU_DTRACE_ILLOP; 5166 break; 5167 } 5168 } 5169 5170 if (end < start) 5171 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5172 5173 if (daddr == 0) { 5174 regs[rd] = (uintptr_t)end; 5175 mstate->dtms_scratch_ptr += size; 5176 } 5177 5178 break; 5179 } 5180#endif 5181 5182 case DIF_SUBR_STRJOIN: { 5183 char *d = (char *)mstate->dtms_scratch_ptr; 5184 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5185 uintptr_t s1 = tupregs[0].dttk_value; 5186 uintptr_t s2 = tupregs[1].dttk_value; 5187 int i = 0; 5188 5189 if (!dtrace_strcanload(s1, size, mstate, vstate) || 5190 !dtrace_strcanload(s2, size, mstate, vstate)) { 5191 regs[rd] = 0; 5192 break; 5193 } 5194 5195 if (!DTRACE_INSCRATCH(mstate, size)) { 5196 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5197 regs[rd] = 0; 5198 break; 5199 } 5200 5201 for (;;) { 5202 if (i >= size) { 5203 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5204 regs[rd] = 0; 5205 break; 5206 } 5207 5208 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 5209 i--; 5210 break; 5211 } 5212 } 5213 5214 for (;;) { 5215 if (i >= size) { 5216 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5217 regs[rd] = 0; 5218 break; 5219 } 5220 5221 if ((d[i++] = dtrace_load8(s2++)) == '\0') 5222 break; 5223 } 5224 5225 if (i < size) { 5226 mstate->dtms_scratch_ptr += i; 5227 regs[rd] = (uintptr_t)d; 5228 } 5229 5230 break; 5231 } 5232 5233 case DIF_SUBR_STRTOLL: { 5234 uintptr_t s = tupregs[0].dttk_value; 5235 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5236 int base = 10; 5237 5238 if (nargs > 1) { 5239 if ((base = tupregs[1].dttk_value) <= 1 || 5240 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5241 *flags |= CPU_DTRACE_ILLOP; 5242 break; 5243 } 5244 } 5245 5246 if (!dtrace_strcanload(s, size, mstate, vstate)) { 5247 regs[rd] = INT64_MIN; 5248 break; 5249 } 5250 5251 regs[rd] = dtrace_strtoll((char *)s, base, size); 5252 break; 5253 } 5254 5255 case DIF_SUBR_LLTOSTR: { 5256 int64_t i = (int64_t)tupregs[0].dttk_value; 5257 uint64_t val, digit; 5258 uint64_t size = 65; /* enough room for 2^64 in binary */ 5259 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 5260 int base = 10; 5261 5262 if (nargs > 1) { 5263 if ((base = tupregs[1].dttk_value) <= 1 || 5264 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5265 *flags |= CPU_DTRACE_ILLOP; 5266 break; 5267 } 5268 } 5269 5270 val = (base == 10 && i < 0) ? i * -1 : i; 5271 5272 if (!DTRACE_INSCRATCH(mstate, size)) { 5273 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5274 regs[rd] = 0; 5275 break; 5276 } 5277 5278 for (*end-- = '\0'; val; val /= base) { 5279 if ((digit = val % base) <= '9' - '0') { 5280 *end-- = '0' + digit; 5281 } else { 5282 *end-- = 'a' + (digit - ('9' - '0') - 1); 5283 } 5284 } 5285 5286 if (i == 0 && base == 16) 5287 *end-- = '0'; 5288 5289 if (base == 16) 5290 *end-- = 'x'; 5291 5292 if (i == 0 || base == 8 || base == 16) 5293 *end-- = '0'; 5294 5295 if (i < 0 && base == 10) 5296 *end-- = '-'; 5297 5298 regs[rd] = (uintptr_t)end + 1; 5299 mstate->dtms_scratch_ptr += size; 5300 break; 5301 } 5302 5303 case DIF_SUBR_HTONS: 5304 case DIF_SUBR_NTOHS: 5305#if BYTE_ORDER == BIG_ENDIAN 5306 regs[rd] = (uint16_t)tupregs[0].dttk_value; 5307#else 5308 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 5309#endif 5310 break; 5311 5312 5313 case DIF_SUBR_HTONL: 5314 case DIF_SUBR_NTOHL: 5315#if BYTE_ORDER == BIG_ENDIAN 5316 regs[rd] = (uint32_t)tupregs[0].dttk_value; 5317#else 5318 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 5319#endif 5320 break; 5321 5322 5323 case DIF_SUBR_HTONLL: 5324 case DIF_SUBR_NTOHLL: 5325#if BYTE_ORDER == BIG_ENDIAN 5326 regs[rd] = (uint64_t)tupregs[0].dttk_value; 5327#else 5328 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 5329#endif 5330 break; 5331 5332 5333 case DIF_SUBR_DIRNAME: 5334 case DIF_SUBR_BASENAME: { 5335 char *dest = (char *)mstate->dtms_scratch_ptr; 5336 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5337 uintptr_t src = tupregs[0].dttk_value; 5338 int i, j, len = dtrace_strlen((char *)src, size); 5339 int lastbase = -1, firstbase = -1, lastdir = -1; 5340 int start, end; 5341 5342 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 5343 regs[rd] = 0; 5344 break; 5345 } 5346 5347 if (!DTRACE_INSCRATCH(mstate, size)) { 5348 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5349 regs[rd] = 0; 5350 break; 5351 } 5352 5353 /* 5354 * The basename and dirname for a zero-length string is 5355 * defined to be "." 5356 */ 5357 if (len == 0) { 5358 len = 1; 5359 src = (uintptr_t)"."; 5360 } 5361 5362 /* 5363 * Start from the back of the string, moving back toward the 5364 * front until we see a character that isn't a slash. That 5365 * character is the last character in the basename. 5366 */ 5367 for (i = len - 1; i >= 0; i--) { 5368 if (dtrace_load8(src + i) != '/') 5369 break; 5370 } 5371 5372 if (i >= 0) 5373 lastbase = i; 5374 5375 /* 5376 * Starting from the last character in the basename, move 5377 * towards the front until we find a slash. The character 5378 * that we processed immediately before that is the first 5379 * character in the basename. 5380 */ 5381 for (; i >= 0; i--) { 5382 if (dtrace_load8(src + i) == '/') 5383 break; 5384 } 5385 5386 if (i >= 0) 5387 firstbase = i + 1; 5388 5389 /* 5390 * Now keep going until we find a non-slash character. That 5391 * character is the last character in the dirname. 5392 */ 5393 for (; i >= 0; i--) { 5394 if (dtrace_load8(src + i) != '/') 5395 break; 5396 } 5397 5398 if (i >= 0) 5399 lastdir = i; 5400 5401 ASSERT(!(lastbase == -1 && firstbase != -1)); 5402 ASSERT(!(firstbase == -1 && lastdir != -1)); 5403 5404 if (lastbase == -1) { 5405 /* 5406 * We didn't find a non-slash character. We know that 5407 * the length is non-zero, so the whole string must be 5408 * slashes. In either the dirname or the basename 5409 * case, we return '/'. 5410 */ 5411 ASSERT(firstbase == -1); 5412 firstbase = lastbase = lastdir = 0; 5413 } 5414 5415 if (firstbase == -1) { 5416 /* 5417 * The entire string consists only of a basename 5418 * component. If we're looking for dirname, we need 5419 * to change our string to be just "."; if we're 5420 * looking for a basename, we'll just set the first 5421 * character of the basename to be 0. 5422 */ 5423 if (subr == DIF_SUBR_DIRNAME) { 5424 ASSERT(lastdir == -1); 5425 src = (uintptr_t)"."; 5426 lastdir = 0; 5427 } else { 5428 firstbase = 0; 5429 } 5430 } 5431 5432 if (subr == DIF_SUBR_DIRNAME) { 5433 if (lastdir == -1) { 5434 /* 5435 * We know that we have a slash in the name -- 5436 * or lastdir would be set to 0, above. And 5437 * because lastdir is -1, we know that this 5438 * slash must be the first character. (That 5439 * is, the full string must be of the form 5440 * "/basename".) In this case, the last 5441 * character of the directory name is 0. 5442 */ 5443 lastdir = 0; 5444 } 5445 5446 start = 0; 5447 end = lastdir; 5448 } else { 5449 ASSERT(subr == DIF_SUBR_BASENAME); 5450 ASSERT(firstbase != -1 && lastbase != -1); 5451 start = firstbase; 5452 end = lastbase; 5453 } 5454 5455 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 5456 dest[j] = dtrace_load8(src + i); 5457 5458 dest[j] = '\0'; 5459 regs[rd] = (uintptr_t)dest; 5460 mstate->dtms_scratch_ptr += size; 5461 break; 5462 } 5463 5464 case DIF_SUBR_GETF: { 5465 uintptr_t fd = tupregs[0].dttk_value; 5466 struct filedesc *fdp; 5467 file_t *fp; 5468 5469 if (!dtrace_priv_proc(state)) { 5470 regs[rd] = 0; 5471 break; 5472 } 5473 fdp = curproc->p_fd; 5474 FILEDESC_SLOCK(fdp); 5475 fp = fget_locked(fdp, fd); 5476 mstate->dtms_getf = fp; 5477 regs[rd] = (uintptr_t)fp; 5478 FILEDESC_SUNLOCK(fdp); 5479 break; 5480 } 5481 5482 case DIF_SUBR_CLEANPATH: { 5483 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5484 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5485 uintptr_t src = tupregs[0].dttk_value; 5486 int i = 0, j = 0; 5487#ifdef illumos 5488 zone_t *z; 5489#endif 5490 5491 if (!dtrace_strcanload(src, size, mstate, vstate)) { 5492 regs[rd] = 0; 5493 break; 5494 } 5495 5496 if (!DTRACE_INSCRATCH(mstate, size)) { 5497 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5498 regs[rd] = 0; 5499 break; 5500 } 5501 5502 /* 5503 * Move forward, loading each character. 5504 */ 5505 do { 5506 c = dtrace_load8(src + i++); 5507next: 5508 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 5509 break; 5510 5511 if (c != '/') { 5512 dest[j++] = c; 5513 continue; 5514 } 5515 5516 c = dtrace_load8(src + i++); 5517 5518 if (c == '/') { 5519 /* 5520 * We have two slashes -- we can just advance 5521 * to the next character. 5522 */ 5523 goto next; 5524 } 5525 5526 if (c != '.') { 5527 /* 5528 * This is not "." and it's not ".." -- we can 5529 * just store the "/" and this character and 5530 * drive on. 5531 */ 5532 dest[j++] = '/'; 5533 dest[j++] = c; 5534 continue; 5535 } 5536 5537 c = dtrace_load8(src + i++); 5538 5539 if (c == '/') { 5540 /* 5541 * This is a "/./" component. We're not going 5542 * to store anything in the destination buffer; 5543 * we're just going to go to the next component. 5544 */ 5545 goto next; 5546 } 5547 5548 if (c != '.') { 5549 /* 5550 * This is not ".." -- we can just store the 5551 * "/." and this character and continue 5552 * processing. 5553 */ 5554 dest[j++] = '/'; 5555 dest[j++] = '.'; 5556 dest[j++] = c; 5557 continue; 5558 } 5559 5560 c = dtrace_load8(src + i++); 5561 5562 if (c != '/' && c != '\0') { 5563 /* 5564 * This is not ".." -- it's "..[mumble]". 5565 * We'll store the "/.." and this character 5566 * and continue processing. 5567 */ 5568 dest[j++] = '/'; 5569 dest[j++] = '.'; 5570 dest[j++] = '.'; 5571 dest[j++] = c; 5572 continue; 5573 } 5574 5575 /* 5576 * This is "/../" or "/..\0". We need to back up 5577 * our destination pointer until we find a "/". 5578 */ 5579 i--; 5580 while (j != 0 && dest[--j] != '/') 5581 continue; 5582 5583 if (c == '\0') 5584 dest[++j] = '/'; 5585 } while (c != '\0'); 5586 5587 dest[j] = '\0'; 5588 5589#ifdef illumos 5590 if (mstate->dtms_getf != NULL && 5591 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 5592 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 5593 /* 5594 * If we've done a getf() as a part of this ECB and we 5595 * don't have kernel access (and we're not in the global 5596 * zone), check if the path we cleaned up begins with 5597 * the zone's root path, and trim it off if so. Note 5598 * that this is an output cleanliness issue, not a 5599 * security issue: knowing one's zone root path does 5600 * not enable privilege escalation. 5601 */ 5602 if (strstr(dest, z->zone_rootpath) == dest) 5603 dest += strlen(z->zone_rootpath) - 1; 5604 } 5605#endif 5606 5607 regs[rd] = (uintptr_t)dest; 5608 mstate->dtms_scratch_ptr += size; 5609 break; 5610 } 5611 5612 case DIF_SUBR_INET_NTOA: 5613 case DIF_SUBR_INET_NTOA6: 5614 case DIF_SUBR_INET_NTOP: { 5615 size_t size; 5616 int af, argi, i; 5617 char *base, *end; 5618 5619 if (subr == DIF_SUBR_INET_NTOP) { 5620 af = (int)tupregs[0].dttk_value; 5621 argi = 1; 5622 } else { 5623 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 5624 argi = 0; 5625 } 5626 5627 if (af == AF_INET) { 5628 ipaddr_t ip4; 5629 uint8_t *ptr8, val; 5630 5631 /* 5632 * Safely load the IPv4 address. 5633 */ 5634 ip4 = dtrace_load32(tupregs[argi].dttk_value); 5635 5636 /* 5637 * Check an IPv4 string will fit in scratch. 5638 */ 5639 size = INET_ADDRSTRLEN; 5640 if (!DTRACE_INSCRATCH(mstate, size)) { 5641 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5642 regs[rd] = 0; 5643 break; 5644 } 5645 base = (char *)mstate->dtms_scratch_ptr; 5646 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5647 5648 /* 5649 * Stringify as a dotted decimal quad. 5650 */ 5651 *end-- = '\0'; 5652 ptr8 = (uint8_t *)&ip4; 5653 for (i = 3; i >= 0; i--) { 5654 val = ptr8[i]; 5655 5656 if (val == 0) { 5657 *end-- = '0'; 5658 } else { 5659 for (; val; val /= 10) { 5660 *end-- = '0' + (val % 10); 5661 } 5662 } 5663 5664 if (i > 0) 5665 *end-- = '.'; 5666 } 5667 ASSERT(end + 1 >= base); 5668 5669 } else if (af == AF_INET6) { 5670 struct in6_addr ip6; 5671 int firstzero, tryzero, numzero, v6end; 5672 uint16_t val; 5673 const char digits[] = "0123456789abcdef"; 5674 5675 /* 5676 * Stringify using RFC 1884 convention 2 - 16 bit 5677 * hexadecimal values with a zero-run compression. 5678 * Lower case hexadecimal digits are used. 5679 * eg, fe80::214:4fff:fe0b:76c8. 5680 * The IPv4 embedded form is returned for inet_ntop, 5681 * just the IPv4 string is returned for inet_ntoa6. 5682 */ 5683 5684 /* 5685 * Safely load the IPv6 address. 5686 */ 5687 dtrace_bcopy( 5688 (void *)(uintptr_t)tupregs[argi].dttk_value, 5689 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 5690 5691 /* 5692 * Check an IPv6 string will fit in scratch. 5693 */ 5694 size = INET6_ADDRSTRLEN; 5695 if (!DTRACE_INSCRATCH(mstate, size)) { 5696 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5697 regs[rd] = 0; 5698 break; 5699 } 5700 base = (char *)mstate->dtms_scratch_ptr; 5701 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5702 *end-- = '\0'; 5703 5704 /* 5705 * Find the longest run of 16 bit zero values 5706 * for the single allowed zero compression - "::". 5707 */ 5708 firstzero = -1; 5709 tryzero = -1; 5710 numzero = 1; 5711 for (i = 0; i < sizeof (struct in6_addr); i++) { 5712#ifdef illumos 5713 if (ip6._S6_un._S6_u8[i] == 0 && 5714#else 5715 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5716#endif 5717 tryzero == -1 && i % 2 == 0) { 5718 tryzero = i; 5719 continue; 5720 } 5721 5722 if (tryzero != -1 && 5723#ifdef illumos 5724 (ip6._S6_un._S6_u8[i] != 0 || 5725#else 5726 (ip6.__u6_addr.__u6_addr8[i] != 0 || 5727#endif 5728 i == sizeof (struct in6_addr) - 1)) { 5729 5730 if (i - tryzero <= numzero) { 5731 tryzero = -1; 5732 continue; 5733 } 5734 5735 firstzero = tryzero; 5736 numzero = i - i % 2 - tryzero; 5737 tryzero = -1; 5738 5739#ifdef illumos 5740 if (ip6._S6_un._S6_u8[i] == 0 && 5741#else 5742 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5743#endif 5744 i == sizeof (struct in6_addr) - 1) 5745 numzero += 2; 5746 } 5747 } 5748 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 5749 5750 /* 5751 * Check for an IPv4 embedded address. 5752 */ 5753 v6end = sizeof (struct in6_addr) - 2; 5754 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 5755 IN6_IS_ADDR_V4COMPAT(&ip6)) { 5756 for (i = sizeof (struct in6_addr) - 1; 5757 i >= DTRACE_V4MAPPED_OFFSET; i--) { 5758 ASSERT(end >= base); 5759 5760#ifdef illumos 5761 val = ip6._S6_un._S6_u8[i]; 5762#else 5763 val = ip6.__u6_addr.__u6_addr8[i]; 5764#endif 5765 5766 if (val == 0) { 5767 *end-- = '0'; 5768 } else { 5769 for (; val; val /= 10) { 5770 *end-- = '0' + val % 10; 5771 } 5772 } 5773 5774 if (i > DTRACE_V4MAPPED_OFFSET) 5775 *end-- = '.'; 5776 } 5777 5778 if (subr == DIF_SUBR_INET_NTOA6) 5779 goto inetout; 5780 5781 /* 5782 * Set v6end to skip the IPv4 address that 5783 * we have already stringified. 5784 */ 5785 v6end = 10; 5786 } 5787 5788 /* 5789 * Build the IPv6 string by working through the 5790 * address in reverse. 5791 */ 5792 for (i = v6end; i >= 0; i -= 2) { 5793 ASSERT(end >= base); 5794 5795 if (i == firstzero + numzero - 2) { 5796 *end-- = ':'; 5797 *end-- = ':'; 5798 i -= numzero - 2; 5799 continue; 5800 } 5801 5802 if (i < 14 && i != firstzero - 2) 5803 *end-- = ':'; 5804 5805#ifdef illumos 5806 val = (ip6._S6_un._S6_u8[i] << 8) + 5807 ip6._S6_un._S6_u8[i + 1]; 5808#else 5809 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 5810 ip6.__u6_addr.__u6_addr8[i + 1]; 5811#endif 5812 5813 if (val == 0) { 5814 *end-- = '0'; 5815 } else { 5816 for (; val; val /= 16) { 5817 *end-- = digits[val % 16]; 5818 } 5819 } 5820 } 5821 ASSERT(end + 1 >= base); 5822 5823 } else { 5824 /* 5825 * The user didn't use AH_INET or AH_INET6. 5826 */ 5827 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5828 regs[rd] = 0; 5829 break; 5830 } 5831 5832inetout: regs[rd] = (uintptr_t)end + 1; 5833 mstate->dtms_scratch_ptr += size; 5834 break; 5835 } 5836 5837 case DIF_SUBR_MEMREF: { 5838 uintptr_t size = 2 * sizeof(uintptr_t); 5839 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5840 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 5841 5842 /* address and length */ 5843 memref[0] = tupregs[0].dttk_value; 5844 memref[1] = tupregs[1].dttk_value; 5845 5846 regs[rd] = (uintptr_t) memref; 5847 mstate->dtms_scratch_ptr += scratch_size; 5848 break; 5849 } 5850 5851#ifndef illumos 5852 case DIF_SUBR_MEMSTR: { 5853 char *str = (char *)mstate->dtms_scratch_ptr; 5854 uintptr_t mem = tupregs[0].dttk_value; 5855 char c = tupregs[1].dttk_value; 5856 size_t size = tupregs[2].dttk_value; 5857 uint8_t n; 5858 int i; 5859 5860 regs[rd] = 0; 5861 5862 if (size == 0) 5863 break; 5864 5865 if (!dtrace_canload(mem, size - 1, mstate, vstate)) 5866 break; 5867 5868 if (!DTRACE_INSCRATCH(mstate, size)) { 5869 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5870 break; 5871 } 5872 5873 if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) { 5874 *flags |= CPU_DTRACE_ILLOP; 5875 break; 5876 } 5877 5878 for (i = 0; i < size - 1; i++) { 5879 n = dtrace_load8(mem++); 5880 str[i] = (n == 0) ? c : n; 5881 } 5882 str[size - 1] = 0; 5883 5884 regs[rd] = (uintptr_t)str; 5885 mstate->dtms_scratch_ptr += size; 5886 break; 5887 } 5888#endif 5889 5890 case DIF_SUBR_TYPEREF: { 5891 uintptr_t size = 4 * sizeof(uintptr_t); 5892 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5893 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 5894 5895 /* address, num_elements, type_str, type_len */ 5896 typeref[0] = tupregs[0].dttk_value; 5897 typeref[1] = tupregs[1].dttk_value; 5898 typeref[2] = tupregs[2].dttk_value; 5899 typeref[3] = tupregs[3].dttk_value; 5900 5901 regs[rd] = (uintptr_t) typeref; 5902 mstate->dtms_scratch_ptr += scratch_size; 5903 break; 5904 } 5905 } 5906} 5907 5908/* 5909 * Emulate the execution of DTrace IR instructions specified by the given 5910 * DIF object. This function is deliberately void of assertions as all of 5911 * the necessary checks are handled by a call to dtrace_difo_validate(). 5912 */ 5913static uint64_t 5914dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 5915 dtrace_vstate_t *vstate, dtrace_state_t *state) 5916{ 5917 const dif_instr_t *text = difo->dtdo_buf; 5918 const uint_t textlen = difo->dtdo_len; 5919 const char *strtab = difo->dtdo_strtab; 5920 const uint64_t *inttab = difo->dtdo_inttab; 5921 5922 uint64_t rval = 0; 5923 dtrace_statvar_t *svar; 5924 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 5925 dtrace_difv_t *v; 5926 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 5927 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 5928 5929 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 5930 uint64_t regs[DIF_DIR_NREGS]; 5931 uint64_t *tmp; 5932 5933 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 5934 int64_t cc_r; 5935 uint_t pc = 0, id, opc = 0; 5936 uint8_t ttop = 0; 5937 dif_instr_t instr; 5938 uint_t r1, r2, rd; 5939 5940 /* 5941 * We stash the current DIF object into the machine state: we need it 5942 * for subsequent access checking. 5943 */ 5944 mstate->dtms_difo = difo; 5945 5946 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 5947 5948 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 5949 opc = pc; 5950 5951 instr = text[pc++]; 5952 r1 = DIF_INSTR_R1(instr); 5953 r2 = DIF_INSTR_R2(instr); 5954 rd = DIF_INSTR_RD(instr); 5955 5956 switch (DIF_INSTR_OP(instr)) { 5957 case DIF_OP_OR: 5958 regs[rd] = regs[r1] | regs[r2]; 5959 break; 5960 case DIF_OP_XOR: 5961 regs[rd] = regs[r1] ^ regs[r2]; 5962 break; 5963 case DIF_OP_AND: 5964 regs[rd] = regs[r1] & regs[r2]; 5965 break; 5966 case DIF_OP_SLL: 5967 regs[rd] = regs[r1] << regs[r2]; 5968 break; 5969 case DIF_OP_SRL: 5970 regs[rd] = regs[r1] >> regs[r2]; 5971 break; 5972 case DIF_OP_SUB: 5973 regs[rd] = regs[r1] - regs[r2]; 5974 break; 5975 case DIF_OP_ADD: 5976 regs[rd] = regs[r1] + regs[r2]; 5977 break; 5978 case DIF_OP_MUL: 5979 regs[rd] = regs[r1] * regs[r2]; 5980 break; 5981 case DIF_OP_SDIV: 5982 if (regs[r2] == 0) { 5983 regs[rd] = 0; 5984 *flags |= CPU_DTRACE_DIVZERO; 5985 } else { 5986 regs[rd] = (int64_t)regs[r1] / 5987 (int64_t)regs[r2]; 5988 } 5989 break; 5990 5991 case DIF_OP_UDIV: 5992 if (regs[r2] == 0) { 5993 regs[rd] = 0; 5994 *flags |= CPU_DTRACE_DIVZERO; 5995 } else { 5996 regs[rd] = regs[r1] / regs[r2]; 5997 } 5998 break; 5999 6000 case DIF_OP_SREM: 6001 if (regs[r2] == 0) { 6002 regs[rd] = 0; 6003 *flags |= CPU_DTRACE_DIVZERO; 6004 } else { 6005 regs[rd] = (int64_t)regs[r1] % 6006 (int64_t)regs[r2]; 6007 } 6008 break; 6009 6010 case DIF_OP_UREM: 6011 if (regs[r2] == 0) { 6012 regs[rd] = 0; 6013 *flags |= CPU_DTRACE_DIVZERO; 6014 } else { 6015 regs[rd] = regs[r1] % regs[r2]; 6016 } 6017 break; 6018 6019 case DIF_OP_NOT: 6020 regs[rd] = ~regs[r1]; 6021 break; 6022 case DIF_OP_MOV: 6023 regs[rd] = regs[r1]; 6024 break; 6025 case DIF_OP_CMP: 6026 cc_r = regs[r1] - regs[r2]; 6027 cc_n = cc_r < 0; 6028 cc_z = cc_r == 0; 6029 cc_v = 0; 6030 cc_c = regs[r1] < regs[r2]; 6031 break; 6032 case DIF_OP_TST: 6033 cc_n = cc_v = cc_c = 0; 6034 cc_z = regs[r1] == 0; 6035 break; 6036 case DIF_OP_BA: 6037 pc = DIF_INSTR_LABEL(instr); 6038 break; 6039 case DIF_OP_BE: 6040 if (cc_z) 6041 pc = DIF_INSTR_LABEL(instr); 6042 break; 6043 case DIF_OP_BNE: 6044 if (cc_z == 0) 6045 pc = DIF_INSTR_LABEL(instr); 6046 break; 6047 case DIF_OP_BG: 6048 if ((cc_z | (cc_n ^ cc_v)) == 0) 6049 pc = DIF_INSTR_LABEL(instr); 6050 break; 6051 case DIF_OP_BGU: 6052 if ((cc_c | cc_z) == 0) 6053 pc = DIF_INSTR_LABEL(instr); 6054 break; 6055 case DIF_OP_BGE: 6056 if ((cc_n ^ cc_v) == 0) 6057 pc = DIF_INSTR_LABEL(instr); 6058 break; 6059 case DIF_OP_BGEU: 6060 if (cc_c == 0) 6061 pc = DIF_INSTR_LABEL(instr); 6062 break; 6063 case DIF_OP_BL: 6064 if (cc_n ^ cc_v) 6065 pc = DIF_INSTR_LABEL(instr); 6066 break; 6067 case DIF_OP_BLU: 6068 if (cc_c) 6069 pc = DIF_INSTR_LABEL(instr); 6070 break; 6071 case DIF_OP_BLE: 6072 if (cc_z | (cc_n ^ cc_v)) 6073 pc = DIF_INSTR_LABEL(instr); 6074 break; 6075 case DIF_OP_BLEU: 6076 if (cc_c | cc_z) 6077 pc = DIF_INSTR_LABEL(instr); 6078 break; 6079 case DIF_OP_RLDSB: 6080 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6081 break; 6082 /*FALLTHROUGH*/ 6083 case DIF_OP_LDSB: 6084 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 6085 break; 6086 case DIF_OP_RLDSH: 6087 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6088 break; 6089 /*FALLTHROUGH*/ 6090 case DIF_OP_LDSH: 6091 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 6092 break; 6093 case DIF_OP_RLDSW: 6094 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6095 break; 6096 /*FALLTHROUGH*/ 6097 case DIF_OP_LDSW: 6098 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 6099 break; 6100 case DIF_OP_RLDUB: 6101 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6102 break; 6103 /*FALLTHROUGH*/ 6104 case DIF_OP_LDUB: 6105 regs[rd] = dtrace_load8(regs[r1]); 6106 break; 6107 case DIF_OP_RLDUH: 6108 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6109 break; 6110 /*FALLTHROUGH*/ 6111 case DIF_OP_LDUH: 6112 regs[rd] = dtrace_load16(regs[r1]); 6113 break; 6114 case DIF_OP_RLDUW: 6115 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6116 break; 6117 /*FALLTHROUGH*/ 6118 case DIF_OP_LDUW: 6119 regs[rd] = dtrace_load32(regs[r1]); 6120 break; 6121 case DIF_OP_RLDX: 6122 if (!dtrace_canload(regs[r1], 8, mstate, vstate)) 6123 break; 6124 /*FALLTHROUGH*/ 6125 case DIF_OP_LDX: 6126 regs[rd] = dtrace_load64(regs[r1]); 6127 break; 6128 case DIF_OP_ULDSB: 6129 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6130 regs[rd] = (int8_t) 6131 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6132 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6133 break; 6134 case DIF_OP_ULDSH: 6135 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6136 regs[rd] = (int16_t) 6137 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6138 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6139 break; 6140 case DIF_OP_ULDSW: 6141 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6142 regs[rd] = (int32_t) 6143 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6144 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6145 break; 6146 case DIF_OP_ULDUB: 6147 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6148 regs[rd] = 6149 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6150 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6151 break; 6152 case DIF_OP_ULDUH: 6153 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6154 regs[rd] = 6155 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6156 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6157 break; 6158 case DIF_OP_ULDUW: 6159 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6160 regs[rd] = 6161 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6162 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6163 break; 6164 case DIF_OP_ULDX: 6165 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6166 regs[rd] = 6167 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 6168 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6169 break; 6170 case DIF_OP_RET: 6171 rval = regs[rd]; 6172 pc = textlen; 6173 break; 6174 case DIF_OP_NOP: 6175 break; 6176 case DIF_OP_SETX: 6177 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 6178 break; 6179 case DIF_OP_SETS: 6180 regs[rd] = (uint64_t)(uintptr_t) 6181 (strtab + DIF_INSTR_STRING(instr)); 6182 break; 6183 case DIF_OP_SCMP: { 6184 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 6185 uintptr_t s1 = regs[r1]; 6186 uintptr_t s2 = regs[r2]; 6187 6188 if (s1 != 0 && 6189 !dtrace_strcanload(s1, sz, mstate, vstate)) 6190 break; 6191 if (s2 != 0 && 6192 !dtrace_strcanload(s2, sz, mstate, vstate)) 6193 break; 6194 6195 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 6196 6197 cc_n = cc_r < 0; 6198 cc_z = cc_r == 0; 6199 cc_v = cc_c = 0; 6200 break; 6201 } 6202 case DIF_OP_LDGA: 6203 regs[rd] = dtrace_dif_variable(mstate, state, 6204 r1, regs[r2]); 6205 break; 6206 case DIF_OP_LDGS: 6207 id = DIF_INSTR_VAR(instr); 6208 6209 if (id >= DIF_VAR_OTHER_UBASE) { 6210 uintptr_t a; 6211 6212 id -= DIF_VAR_OTHER_UBASE; 6213 svar = vstate->dtvs_globals[id]; 6214 ASSERT(svar != NULL); 6215 v = &svar->dtsv_var; 6216 6217 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 6218 regs[rd] = svar->dtsv_data; 6219 break; 6220 } 6221 6222 a = (uintptr_t)svar->dtsv_data; 6223 6224 if (*(uint8_t *)a == UINT8_MAX) { 6225 /* 6226 * If the 0th byte is set to UINT8_MAX 6227 * then this is to be treated as a 6228 * reference to a NULL variable. 6229 */ 6230 regs[rd] = 0; 6231 } else { 6232 regs[rd] = a + sizeof (uint64_t); 6233 } 6234 6235 break; 6236 } 6237 6238 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 6239 break; 6240 6241 case DIF_OP_STGS: 6242 id = DIF_INSTR_VAR(instr); 6243 6244 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6245 id -= DIF_VAR_OTHER_UBASE; 6246 6247 svar = vstate->dtvs_globals[id]; 6248 ASSERT(svar != NULL); 6249 v = &svar->dtsv_var; 6250 6251 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6252 uintptr_t a = (uintptr_t)svar->dtsv_data; 6253 6254 ASSERT(a != 0); 6255 ASSERT(svar->dtsv_size != 0); 6256 6257 if (regs[rd] == 0) { 6258 *(uint8_t *)a = UINT8_MAX; 6259 break; 6260 } else { 6261 *(uint8_t *)a = 0; 6262 a += sizeof (uint64_t); 6263 } 6264 if (!dtrace_vcanload( 6265 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6266 mstate, vstate)) 6267 break; 6268 6269 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6270 (void *)a, &v->dtdv_type); 6271 break; 6272 } 6273 6274 svar->dtsv_data = regs[rd]; 6275 break; 6276 6277 case DIF_OP_LDTA: 6278 /* 6279 * There are no DTrace built-in thread-local arrays at 6280 * present. This opcode is saved for future work. 6281 */ 6282 *flags |= CPU_DTRACE_ILLOP; 6283 regs[rd] = 0; 6284 break; 6285 6286 case DIF_OP_LDLS: 6287 id = DIF_INSTR_VAR(instr); 6288 6289 if (id < DIF_VAR_OTHER_UBASE) { 6290 /* 6291 * For now, this has no meaning. 6292 */ 6293 regs[rd] = 0; 6294 break; 6295 } 6296 6297 id -= DIF_VAR_OTHER_UBASE; 6298 6299 ASSERT(id < vstate->dtvs_nlocals); 6300 ASSERT(vstate->dtvs_locals != NULL); 6301 6302 svar = vstate->dtvs_locals[id]; 6303 ASSERT(svar != NULL); 6304 v = &svar->dtsv_var; 6305 6306 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6307 uintptr_t a = (uintptr_t)svar->dtsv_data; 6308 size_t sz = v->dtdv_type.dtdt_size; 6309 6310 sz += sizeof (uint64_t); 6311 ASSERT(svar->dtsv_size == NCPU * sz); 6312 a += curcpu * sz; 6313 6314 if (*(uint8_t *)a == UINT8_MAX) { 6315 /* 6316 * If the 0th byte is set to UINT8_MAX 6317 * then this is to be treated as a 6318 * reference to a NULL variable. 6319 */ 6320 regs[rd] = 0; 6321 } else { 6322 regs[rd] = a + sizeof (uint64_t); 6323 } 6324 6325 break; 6326 } 6327 6328 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6329 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6330 regs[rd] = tmp[curcpu]; 6331 break; 6332 6333 case DIF_OP_STLS: 6334 id = DIF_INSTR_VAR(instr); 6335 6336 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6337 id -= DIF_VAR_OTHER_UBASE; 6338 ASSERT(id < vstate->dtvs_nlocals); 6339 6340 ASSERT(vstate->dtvs_locals != NULL); 6341 svar = vstate->dtvs_locals[id]; 6342 ASSERT(svar != NULL); 6343 v = &svar->dtsv_var; 6344 6345 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6346 uintptr_t a = (uintptr_t)svar->dtsv_data; 6347 size_t sz = v->dtdv_type.dtdt_size; 6348 6349 sz += sizeof (uint64_t); 6350 ASSERT(svar->dtsv_size == NCPU * sz); 6351 a += curcpu * sz; 6352 6353 if (regs[rd] == 0) { 6354 *(uint8_t *)a = UINT8_MAX; 6355 break; 6356 } else { 6357 *(uint8_t *)a = 0; 6358 a += sizeof (uint64_t); 6359 } 6360 6361 if (!dtrace_vcanload( 6362 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6363 mstate, vstate)) 6364 break; 6365 6366 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6367 (void *)a, &v->dtdv_type); 6368 break; 6369 } 6370 6371 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6372 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6373 tmp[curcpu] = regs[rd]; 6374 break; 6375 6376 case DIF_OP_LDTS: { 6377 dtrace_dynvar_t *dvar; 6378 dtrace_key_t *key; 6379 6380 id = DIF_INSTR_VAR(instr); 6381 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6382 id -= DIF_VAR_OTHER_UBASE; 6383 v = &vstate->dtvs_tlocals[id]; 6384 6385 key = &tupregs[DIF_DTR_NREGS]; 6386 key[0].dttk_value = (uint64_t)id; 6387 key[0].dttk_size = 0; 6388 DTRACE_TLS_THRKEY(key[1].dttk_value); 6389 key[1].dttk_size = 0; 6390 6391 dvar = dtrace_dynvar(dstate, 2, key, 6392 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 6393 mstate, vstate); 6394 6395 if (dvar == NULL) { 6396 regs[rd] = 0; 6397 break; 6398 } 6399 6400 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6401 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6402 } else { 6403 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6404 } 6405 6406 break; 6407 } 6408 6409 case DIF_OP_STTS: { 6410 dtrace_dynvar_t *dvar; 6411 dtrace_key_t *key; 6412 6413 id = DIF_INSTR_VAR(instr); 6414 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6415 id -= DIF_VAR_OTHER_UBASE; 6416 6417 key = &tupregs[DIF_DTR_NREGS]; 6418 key[0].dttk_value = (uint64_t)id; 6419 key[0].dttk_size = 0; 6420 DTRACE_TLS_THRKEY(key[1].dttk_value); 6421 key[1].dttk_size = 0; 6422 v = &vstate->dtvs_tlocals[id]; 6423 6424 dvar = dtrace_dynvar(dstate, 2, key, 6425 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6426 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6427 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6428 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6429 6430 /* 6431 * Given that we're storing to thread-local data, 6432 * we need to flush our predicate cache. 6433 */ 6434 curthread->t_predcache = 0; 6435 6436 if (dvar == NULL) 6437 break; 6438 6439 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6440 if (!dtrace_vcanload( 6441 (void *)(uintptr_t)regs[rd], 6442 &v->dtdv_type, mstate, vstate)) 6443 break; 6444 6445 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6446 dvar->dtdv_data, &v->dtdv_type); 6447 } else { 6448 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6449 } 6450 6451 break; 6452 } 6453 6454 case DIF_OP_SRA: 6455 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 6456 break; 6457 6458 case DIF_OP_CALL: 6459 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 6460 regs, tupregs, ttop, mstate, state); 6461 break; 6462 6463 case DIF_OP_PUSHTR: 6464 if (ttop == DIF_DTR_NREGS) { 6465 *flags |= CPU_DTRACE_TUPOFLOW; 6466 break; 6467 } 6468 6469 if (r1 == DIF_TYPE_STRING) { 6470 /* 6471 * If this is a string type and the size is 0, 6472 * we'll use the system-wide default string 6473 * size. Note that we are _not_ looking at 6474 * the value of the DTRACEOPT_STRSIZE option; 6475 * had this been set, we would expect to have 6476 * a non-zero size value in the "pushtr". 6477 */ 6478 tupregs[ttop].dttk_size = 6479 dtrace_strlen((char *)(uintptr_t)regs[rd], 6480 regs[r2] ? regs[r2] : 6481 dtrace_strsize_default) + 1; 6482 } else { 6483 if (regs[r2] > LONG_MAX) { 6484 *flags |= CPU_DTRACE_ILLOP; 6485 break; 6486 } 6487 6488 tupregs[ttop].dttk_size = regs[r2]; 6489 } 6490 6491 tupregs[ttop++].dttk_value = regs[rd]; 6492 break; 6493 6494 case DIF_OP_PUSHTV: 6495 if (ttop == DIF_DTR_NREGS) { 6496 *flags |= CPU_DTRACE_TUPOFLOW; 6497 break; 6498 } 6499 6500 tupregs[ttop].dttk_value = regs[rd]; 6501 tupregs[ttop++].dttk_size = 0; 6502 break; 6503 6504 case DIF_OP_POPTS: 6505 if (ttop != 0) 6506 ttop--; 6507 break; 6508 6509 case DIF_OP_FLUSHTS: 6510 ttop = 0; 6511 break; 6512 6513 case DIF_OP_LDGAA: 6514 case DIF_OP_LDTAA: { 6515 dtrace_dynvar_t *dvar; 6516 dtrace_key_t *key = tupregs; 6517 uint_t nkeys = ttop; 6518 6519 id = DIF_INSTR_VAR(instr); 6520 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6521 id -= DIF_VAR_OTHER_UBASE; 6522 6523 key[nkeys].dttk_value = (uint64_t)id; 6524 key[nkeys++].dttk_size = 0; 6525 6526 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 6527 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6528 key[nkeys++].dttk_size = 0; 6529 v = &vstate->dtvs_tlocals[id]; 6530 } else { 6531 v = &vstate->dtvs_globals[id]->dtsv_var; 6532 } 6533 6534 dvar = dtrace_dynvar(dstate, nkeys, key, 6535 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6536 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6537 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 6538 6539 if (dvar == NULL) { 6540 regs[rd] = 0; 6541 break; 6542 } 6543 6544 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6545 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6546 } else { 6547 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6548 } 6549 6550 break; 6551 } 6552 6553 case DIF_OP_STGAA: 6554 case DIF_OP_STTAA: { 6555 dtrace_dynvar_t *dvar; 6556 dtrace_key_t *key = tupregs; 6557 uint_t nkeys = ttop; 6558 6559 id = DIF_INSTR_VAR(instr); 6560 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6561 id -= DIF_VAR_OTHER_UBASE; 6562 6563 key[nkeys].dttk_value = (uint64_t)id; 6564 key[nkeys++].dttk_size = 0; 6565 6566 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 6567 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6568 key[nkeys++].dttk_size = 0; 6569 v = &vstate->dtvs_tlocals[id]; 6570 } else { 6571 v = &vstate->dtvs_globals[id]->dtsv_var; 6572 } 6573 6574 dvar = dtrace_dynvar(dstate, nkeys, key, 6575 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6576 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6577 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6578 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6579 6580 if (dvar == NULL) 6581 break; 6582 6583 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6584 if (!dtrace_vcanload( 6585 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6586 mstate, vstate)) 6587 break; 6588 6589 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6590 dvar->dtdv_data, &v->dtdv_type); 6591 } else { 6592 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6593 } 6594 6595 break; 6596 } 6597 6598 case DIF_OP_ALLOCS: { 6599 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6600 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 6601 6602 /* 6603 * Rounding up the user allocation size could have 6604 * overflowed large, bogus allocations (like -1ULL) to 6605 * 0. 6606 */ 6607 if (size < regs[r1] || 6608 !DTRACE_INSCRATCH(mstate, size)) { 6609 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6610 regs[rd] = 0; 6611 break; 6612 } 6613 6614 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 6615 mstate->dtms_scratch_ptr += size; 6616 regs[rd] = ptr; 6617 break; 6618 } 6619 6620 case DIF_OP_COPYS: 6621 if (!dtrace_canstore(regs[rd], regs[r2], 6622 mstate, vstate)) { 6623 *flags |= CPU_DTRACE_BADADDR; 6624 *illval = regs[rd]; 6625 break; 6626 } 6627 6628 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 6629 break; 6630 6631 dtrace_bcopy((void *)(uintptr_t)regs[r1], 6632 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 6633 break; 6634 6635 case DIF_OP_STB: 6636 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 6637 *flags |= CPU_DTRACE_BADADDR; 6638 *illval = regs[rd]; 6639 break; 6640 } 6641 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 6642 break; 6643 6644 case DIF_OP_STH: 6645 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 6646 *flags |= CPU_DTRACE_BADADDR; 6647 *illval = regs[rd]; 6648 break; 6649 } 6650 if (regs[rd] & 1) { 6651 *flags |= CPU_DTRACE_BADALIGN; 6652 *illval = regs[rd]; 6653 break; 6654 } 6655 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 6656 break; 6657 6658 case DIF_OP_STW: 6659 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 6660 *flags |= CPU_DTRACE_BADADDR; 6661 *illval = regs[rd]; 6662 break; 6663 } 6664 if (regs[rd] & 3) { 6665 *flags |= CPU_DTRACE_BADALIGN; 6666 *illval = regs[rd]; 6667 break; 6668 } 6669 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 6670 break; 6671 6672 case DIF_OP_STX: 6673 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 6674 *flags |= CPU_DTRACE_BADADDR; 6675 *illval = regs[rd]; 6676 break; 6677 } 6678 if (regs[rd] & 7) { 6679 *flags |= CPU_DTRACE_BADALIGN; 6680 *illval = regs[rd]; 6681 break; 6682 } 6683 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 6684 break; 6685 } 6686 } 6687 6688 if (!(*flags & CPU_DTRACE_FAULT)) 6689 return (rval); 6690 6691 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 6692 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 6693 6694 return (0); 6695} 6696 6697static void 6698dtrace_action_breakpoint(dtrace_ecb_t *ecb) 6699{ 6700 dtrace_probe_t *probe = ecb->dte_probe; 6701 dtrace_provider_t *prov = probe->dtpr_provider; 6702 char c[DTRACE_FULLNAMELEN + 80], *str; 6703 char *msg = "dtrace: breakpoint action at probe "; 6704 char *ecbmsg = " (ecb "; 6705 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 6706 uintptr_t val = (uintptr_t)ecb; 6707 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 6708 6709 if (dtrace_destructive_disallow) 6710 return; 6711 6712 /* 6713 * It's impossible to be taking action on the NULL probe. 6714 */ 6715 ASSERT(probe != NULL); 6716 6717 /* 6718 * This is a poor man's (destitute man's?) sprintf(): we want to 6719 * print the provider name, module name, function name and name of 6720 * the probe, along with the hex address of the ECB with the breakpoint 6721 * action -- all of which we must place in the character buffer by 6722 * hand. 6723 */ 6724 while (*msg != '\0') 6725 c[i++] = *msg++; 6726 6727 for (str = prov->dtpv_name; *str != '\0'; str++) 6728 c[i++] = *str; 6729 c[i++] = ':'; 6730 6731 for (str = probe->dtpr_mod; *str != '\0'; str++) 6732 c[i++] = *str; 6733 c[i++] = ':'; 6734 6735 for (str = probe->dtpr_func; *str != '\0'; str++) 6736 c[i++] = *str; 6737 c[i++] = ':'; 6738 6739 for (str = probe->dtpr_name; *str != '\0'; str++) 6740 c[i++] = *str; 6741 6742 while (*ecbmsg != '\0') 6743 c[i++] = *ecbmsg++; 6744 6745 while (shift >= 0) { 6746 mask = (uintptr_t)0xf << shift; 6747 6748 if (val >= ((uintptr_t)1 << shift)) 6749 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 6750 shift -= 4; 6751 } 6752 6753 c[i++] = ')'; 6754 c[i] = '\0'; 6755 6756#ifdef illumos 6757 debug_enter(c); 6758#else 6759 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 6760#endif 6761} 6762 6763static void 6764dtrace_action_panic(dtrace_ecb_t *ecb) 6765{ 6766 dtrace_probe_t *probe = ecb->dte_probe; 6767 6768 /* 6769 * It's impossible to be taking action on the NULL probe. 6770 */ 6771 ASSERT(probe != NULL); 6772 6773 if (dtrace_destructive_disallow) 6774 return; 6775 6776 if (dtrace_panicked != NULL) 6777 return; 6778 6779 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 6780 return; 6781 6782 /* 6783 * We won the right to panic. (We want to be sure that only one 6784 * thread calls panic() from dtrace_probe(), and that panic() is 6785 * called exactly once.) 6786 */ 6787 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 6788 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 6789 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 6790} 6791 6792static void 6793dtrace_action_raise(uint64_t sig) 6794{ 6795 if (dtrace_destructive_disallow) 6796 return; 6797 6798 if (sig >= NSIG) { 6799 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 6800 return; 6801 } 6802 6803#ifdef illumos 6804 /* 6805 * raise() has a queue depth of 1 -- we ignore all subsequent 6806 * invocations of the raise() action. 6807 */ 6808 if (curthread->t_dtrace_sig == 0) 6809 curthread->t_dtrace_sig = (uint8_t)sig; 6810 6811 curthread->t_sig_check = 1; 6812 aston(curthread); 6813#else 6814 struct proc *p = curproc; 6815 PROC_LOCK(p); 6816 kern_psignal(p, sig); 6817 PROC_UNLOCK(p); 6818#endif 6819} 6820 6821static void 6822dtrace_action_stop(void) 6823{ 6824 if (dtrace_destructive_disallow) 6825 return; 6826 6827#ifdef illumos 6828 if (!curthread->t_dtrace_stop) { 6829 curthread->t_dtrace_stop = 1; 6830 curthread->t_sig_check = 1; 6831 aston(curthread); 6832 } 6833#else 6834 struct proc *p = curproc; 6835 PROC_LOCK(p); 6836 kern_psignal(p, SIGSTOP); 6837 PROC_UNLOCK(p); 6838#endif 6839} 6840 6841static void 6842dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 6843{ 6844 hrtime_t now; 6845 volatile uint16_t *flags; 6846#ifdef illumos 6847 cpu_t *cpu = CPU; 6848#else 6849 cpu_t *cpu = &solaris_cpu[curcpu]; 6850#endif 6851 6852 if (dtrace_destructive_disallow) 6853 return; 6854 6855 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 6856 6857 now = dtrace_gethrtime(); 6858 6859 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 6860 /* 6861 * We need to advance the mark to the current time. 6862 */ 6863 cpu->cpu_dtrace_chillmark = now; 6864 cpu->cpu_dtrace_chilled = 0; 6865 } 6866 6867 /* 6868 * Now check to see if the requested chill time would take us over 6869 * the maximum amount of time allowed in the chill interval. (Or 6870 * worse, if the calculation itself induces overflow.) 6871 */ 6872 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 6873 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 6874 *flags |= CPU_DTRACE_ILLOP; 6875 return; 6876 } 6877 6878 while (dtrace_gethrtime() - now < val) 6879 continue; 6880 6881 /* 6882 * Normally, we assure that the value of the variable "timestamp" does 6883 * not change within an ECB. The presence of chill() represents an 6884 * exception to this rule, however. 6885 */ 6886 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 6887 cpu->cpu_dtrace_chilled += val; 6888} 6889 6890static void 6891dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 6892 uint64_t *buf, uint64_t arg) 6893{ 6894 int nframes = DTRACE_USTACK_NFRAMES(arg); 6895 int strsize = DTRACE_USTACK_STRSIZE(arg); 6896 uint64_t *pcs = &buf[1], *fps; 6897 char *str = (char *)&pcs[nframes]; 6898 int size, offs = 0, i, j; 6899 uintptr_t old = mstate->dtms_scratch_ptr, saved; 6900 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 6901 char *sym; 6902 6903 /* 6904 * Should be taking a faster path if string space has not been 6905 * allocated. 6906 */ 6907 ASSERT(strsize != 0); 6908 6909 /* 6910 * We will first allocate some temporary space for the frame pointers. 6911 */ 6912 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6913 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 6914 (nframes * sizeof (uint64_t)); 6915 6916 if (!DTRACE_INSCRATCH(mstate, size)) { 6917 /* 6918 * Not enough room for our frame pointers -- need to indicate 6919 * that we ran out of scratch space. 6920 */ 6921 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6922 return; 6923 } 6924 6925 mstate->dtms_scratch_ptr += size; 6926 saved = mstate->dtms_scratch_ptr; 6927 6928 /* 6929 * Now get a stack with both program counters and frame pointers. 6930 */ 6931 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6932 dtrace_getufpstack(buf, fps, nframes + 1); 6933 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6934 6935 /* 6936 * If that faulted, we're cooked. 6937 */ 6938 if (*flags & CPU_DTRACE_FAULT) 6939 goto out; 6940 6941 /* 6942 * Now we want to walk up the stack, calling the USTACK helper. For 6943 * each iteration, we restore the scratch pointer. 6944 */ 6945 for (i = 0; i < nframes; i++) { 6946 mstate->dtms_scratch_ptr = saved; 6947 6948 if (offs >= strsize) 6949 break; 6950 6951 sym = (char *)(uintptr_t)dtrace_helper( 6952 DTRACE_HELPER_ACTION_USTACK, 6953 mstate, state, pcs[i], fps[i]); 6954 6955 /* 6956 * If we faulted while running the helper, we're going to 6957 * clear the fault and null out the corresponding string. 6958 */ 6959 if (*flags & CPU_DTRACE_FAULT) { 6960 *flags &= ~CPU_DTRACE_FAULT; 6961 str[offs++] = '\0'; 6962 continue; 6963 } 6964 6965 if (sym == NULL) { 6966 str[offs++] = '\0'; 6967 continue; 6968 } 6969 6970 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6971 6972 /* 6973 * Now copy in the string that the helper returned to us. 6974 */ 6975 for (j = 0; offs + j < strsize; j++) { 6976 if ((str[offs + j] = sym[j]) == '\0') 6977 break; 6978 } 6979 6980 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6981 6982 offs += j + 1; 6983 } 6984 6985 if (offs >= strsize) { 6986 /* 6987 * If we didn't have room for all of the strings, we don't 6988 * abort processing -- this needn't be a fatal error -- but we 6989 * still want to increment a counter (dts_stkstroverflows) to 6990 * allow this condition to be warned about. (If this is from 6991 * a jstack() action, it is easily tuned via jstackstrsize.) 6992 */ 6993 dtrace_error(&state->dts_stkstroverflows); 6994 } 6995 6996 while (offs < strsize) 6997 str[offs++] = '\0'; 6998 6999out: 7000 mstate->dtms_scratch_ptr = old; 7001} 7002 7003static void 7004dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size, 7005 size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind) 7006{ 7007 volatile uint16_t *flags; 7008 uint64_t val = *valp; 7009 size_t valoffs = *valoffsp; 7010 7011 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 7012 ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF); 7013 7014 /* 7015 * If this is a string, we're going to only load until we find the zero 7016 * byte -- after which we'll store zero bytes. 7017 */ 7018 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 7019 char c = '\0' + 1; 7020 size_t s; 7021 7022 for (s = 0; s < size; s++) { 7023 if (c != '\0' && dtkind == DIF_TF_BYREF) { 7024 c = dtrace_load8(val++); 7025 } else if (c != '\0' && dtkind == DIF_TF_BYUREF) { 7026 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7027 c = dtrace_fuword8((void *)(uintptr_t)val++); 7028 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7029 if (*flags & CPU_DTRACE_FAULT) 7030 break; 7031 } 7032 7033 DTRACE_STORE(uint8_t, tomax, valoffs++, c); 7034 7035 if (c == '\0' && intuple) 7036 break; 7037 } 7038 } else { 7039 uint8_t c; 7040 while (valoffs < end) { 7041 if (dtkind == DIF_TF_BYREF) { 7042 c = dtrace_load8(val++); 7043 } else if (dtkind == DIF_TF_BYUREF) { 7044 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7045 c = dtrace_fuword8((void *)(uintptr_t)val++); 7046 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7047 if (*flags & CPU_DTRACE_FAULT) 7048 break; 7049 } 7050 7051 DTRACE_STORE(uint8_t, tomax, 7052 valoffs++, c); 7053 } 7054 } 7055 7056 *valp = val; 7057 *valoffsp = valoffs; 7058} 7059 7060/* 7061 * If you're looking for the epicenter of DTrace, you just found it. This 7062 * is the function called by the provider to fire a probe -- from which all 7063 * subsequent probe-context DTrace activity emanates. 7064 */ 7065void 7066dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 7067 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 7068{ 7069 processorid_t cpuid; 7070 dtrace_icookie_t cookie; 7071 dtrace_probe_t *probe; 7072 dtrace_mstate_t mstate; 7073 dtrace_ecb_t *ecb; 7074 dtrace_action_t *act; 7075 intptr_t offs; 7076 size_t size; 7077 int vtime, onintr; 7078 volatile uint16_t *flags; 7079 hrtime_t now; 7080 7081 if (panicstr != NULL) 7082 return; 7083 7084#ifdef illumos 7085 /* 7086 * Kick out immediately if this CPU is still being born (in which case 7087 * curthread will be set to -1) or the current thread can't allow 7088 * probes in its current context. 7089 */ 7090 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 7091 return; 7092#endif 7093 7094 cookie = dtrace_interrupt_disable(); 7095 probe = dtrace_probes[id - 1]; 7096 cpuid = curcpu; 7097 onintr = CPU_ON_INTR(CPU); 7098 7099 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 7100 probe->dtpr_predcache == curthread->t_predcache) { 7101 /* 7102 * We have hit in the predicate cache; we know that 7103 * this predicate would evaluate to be false. 7104 */ 7105 dtrace_interrupt_enable(cookie); 7106 return; 7107 } 7108 7109#ifdef illumos 7110 if (panic_quiesce) { 7111#else 7112 if (panicstr != NULL) { 7113#endif 7114 /* 7115 * We don't trace anything if we're panicking. 7116 */ 7117 dtrace_interrupt_enable(cookie); 7118 return; 7119 } 7120 7121 now = mstate.dtms_timestamp = dtrace_gethrtime(); 7122 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7123 vtime = dtrace_vtime_references != 0; 7124 7125 if (vtime && curthread->t_dtrace_start) 7126 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 7127 7128 mstate.dtms_difo = NULL; 7129 mstate.dtms_probe = probe; 7130 mstate.dtms_strtok = 0; 7131 mstate.dtms_arg[0] = arg0; 7132 mstate.dtms_arg[1] = arg1; 7133 mstate.dtms_arg[2] = arg2; 7134 mstate.dtms_arg[3] = arg3; 7135 mstate.dtms_arg[4] = arg4; 7136 7137 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 7138 7139 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 7140 dtrace_predicate_t *pred = ecb->dte_predicate; 7141 dtrace_state_t *state = ecb->dte_state; 7142 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 7143 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 7144 dtrace_vstate_t *vstate = &state->dts_vstate; 7145 dtrace_provider_t *prov = probe->dtpr_provider; 7146 uint64_t tracememsize = 0; 7147 int committed = 0; 7148 caddr_t tomax; 7149 7150 /* 7151 * A little subtlety with the following (seemingly innocuous) 7152 * declaration of the automatic 'val': by looking at the 7153 * code, you might think that it could be declared in the 7154 * action processing loop, below. (That is, it's only used in 7155 * the action processing loop.) However, it must be declared 7156 * out of that scope because in the case of DIF expression 7157 * arguments to aggregating actions, one iteration of the 7158 * action loop will use the last iteration's value. 7159 */ 7160 uint64_t val = 0; 7161 7162 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 7163 mstate.dtms_getf = NULL; 7164 7165 *flags &= ~CPU_DTRACE_ERROR; 7166 7167 if (prov == dtrace_provider) { 7168 /* 7169 * If dtrace itself is the provider of this probe, 7170 * we're only going to continue processing the ECB if 7171 * arg0 (the dtrace_state_t) is equal to the ECB's 7172 * creating state. (This prevents disjoint consumers 7173 * from seeing one another's metaprobes.) 7174 */ 7175 if (arg0 != (uint64_t)(uintptr_t)state) 7176 continue; 7177 } 7178 7179 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 7180 /* 7181 * We're not currently active. If our provider isn't 7182 * the dtrace pseudo provider, we're not interested. 7183 */ 7184 if (prov != dtrace_provider) 7185 continue; 7186 7187 /* 7188 * Now we must further check if we are in the BEGIN 7189 * probe. If we are, we will only continue processing 7190 * if we're still in WARMUP -- if one BEGIN enabling 7191 * has invoked the exit() action, we don't want to 7192 * evaluate subsequent BEGIN enablings. 7193 */ 7194 if (probe->dtpr_id == dtrace_probeid_begin && 7195 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 7196 ASSERT(state->dts_activity == 7197 DTRACE_ACTIVITY_DRAINING); 7198 continue; 7199 } 7200 } 7201 7202 if (ecb->dte_cond) { 7203 /* 7204 * If the dte_cond bits indicate that this 7205 * consumer is only allowed to see user-mode firings 7206 * of this probe, call the provider's dtps_usermode() 7207 * entry point to check that the probe was fired 7208 * while in a user context. Skip this ECB if that's 7209 * not the case. 7210 */ 7211 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 7212 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 7213 probe->dtpr_id, probe->dtpr_arg) == 0) 7214 continue; 7215 7216#ifdef illumos 7217 /* 7218 * This is more subtle than it looks. We have to be 7219 * absolutely certain that CRED() isn't going to 7220 * change out from under us so it's only legit to 7221 * examine that structure if we're in constrained 7222 * situations. Currently, the only times we'll this 7223 * check is if a non-super-user has enabled the 7224 * profile or syscall providers -- providers that 7225 * allow visibility of all processes. For the 7226 * profile case, the check above will ensure that 7227 * we're examining a user context. 7228 */ 7229 if (ecb->dte_cond & DTRACE_COND_OWNER) { 7230 cred_t *cr; 7231 cred_t *s_cr = 7232 ecb->dte_state->dts_cred.dcr_cred; 7233 proc_t *proc; 7234 7235 ASSERT(s_cr != NULL); 7236 7237 if ((cr = CRED()) == NULL || 7238 s_cr->cr_uid != cr->cr_uid || 7239 s_cr->cr_uid != cr->cr_ruid || 7240 s_cr->cr_uid != cr->cr_suid || 7241 s_cr->cr_gid != cr->cr_gid || 7242 s_cr->cr_gid != cr->cr_rgid || 7243 s_cr->cr_gid != cr->cr_sgid || 7244 (proc = ttoproc(curthread)) == NULL || 7245 (proc->p_flag & SNOCD)) 7246 continue; 7247 } 7248 7249 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 7250 cred_t *cr; 7251 cred_t *s_cr = 7252 ecb->dte_state->dts_cred.dcr_cred; 7253 7254 ASSERT(s_cr != NULL); 7255 7256 if ((cr = CRED()) == NULL || 7257 s_cr->cr_zone->zone_id != 7258 cr->cr_zone->zone_id) 7259 continue; 7260 } 7261#endif 7262 } 7263 7264 if (now - state->dts_alive > dtrace_deadman_timeout) { 7265 /* 7266 * We seem to be dead. Unless we (a) have kernel 7267 * destructive permissions (b) have explicitly enabled 7268 * destructive actions and (c) destructive actions have 7269 * not been disabled, we're going to transition into 7270 * the KILLED state, from which no further processing 7271 * on this state will be performed. 7272 */ 7273 if (!dtrace_priv_kernel_destructive(state) || 7274 !state->dts_cred.dcr_destructive || 7275 dtrace_destructive_disallow) { 7276 void *activity = &state->dts_activity; 7277 dtrace_activity_t current; 7278 7279 do { 7280 current = state->dts_activity; 7281 } while (dtrace_cas32(activity, current, 7282 DTRACE_ACTIVITY_KILLED) != current); 7283 7284 continue; 7285 } 7286 } 7287 7288 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 7289 ecb->dte_alignment, state, &mstate)) < 0) 7290 continue; 7291 7292 tomax = buf->dtb_tomax; 7293 ASSERT(tomax != NULL); 7294 7295 if (ecb->dte_size != 0) { 7296 dtrace_rechdr_t dtrh; 7297 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 7298 mstate.dtms_timestamp = dtrace_gethrtime(); 7299 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7300 } 7301 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 7302 dtrh.dtrh_epid = ecb->dte_epid; 7303 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 7304 mstate.dtms_timestamp); 7305 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 7306 } 7307 7308 mstate.dtms_epid = ecb->dte_epid; 7309 mstate.dtms_present |= DTRACE_MSTATE_EPID; 7310 7311 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 7312 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 7313 else 7314 mstate.dtms_access = 0; 7315 7316 if (pred != NULL) { 7317 dtrace_difo_t *dp = pred->dtp_difo; 7318 uint64_t rval; 7319 7320 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 7321 7322 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 7323 dtrace_cacheid_t cid = probe->dtpr_predcache; 7324 7325 if (cid != DTRACE_CACHEIDNONE && !onintr) { 7326 /* 7327 * Update the predicate cache... 7328 */ 7329 ASSERT(cid == pred->dtp_cacheid); 7330 curthread->t_predcache = cid; 7331 } 7332 7333 continue; 7334 } 7335 } 7336 7337 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 7338 act != NULL; act = act->dta_next) { 7339 size_t valoffs; 7340 dtrace_difo_t *dp; 7341 dtrace_recdesc_t *rec = &act->dta_rec; 7342 7343 size = rec->dtrd_size; 7344 valoffs = offs + rec->dtrd_offset; 7345 7346 if (DTRACEACT_ISAGG(act->dta_kind)) { 7347 uint64_t v = 0xbad; 7348 dtrace_aggregation_t *agg; 7349 7350 agg = (dtrace_aggregation_t *)act; 7351 7352 if ((dp = act->dta_difo) != NULL) 7353 v = dtrace_dif_emulate(dp, 7354 &mstate, vstate, state); 7355 7356 if (*flags & CPU_DTRACE_ERROR) 7357 continue; 7358 7359 /* 7360 * Note that we always pass the expression 7361 * value from the previous iteration of the 7362 * action loop. This value will only be used 7363 * if there is an expression argument to the 7364 * aggregating action, denoted by the 7365 * dtag_hasarg field. 7366 */ 7367 dtrace_aggregate(agg, buf, 7368 offs, aggbuf, v, val); 7369 continue; 7370 } 7371 7372 switch (act->dta_kind) { 7373 case DTRACEACT_STOP: 7374 if (dtrace_priv_proc_destructive(state)) 7375 dtrace_action_stop(); 7376 continue; 7377 7378 case DTRACEACT_BREAKPOINT: 7379 if (dtrace_priv_kernel_destructive(state)) 7380 dtrace_action_breakpoint(ecb); 7381 continue; 7382 7383 case DTRACEACT_PANIC: 7384 if (dtrace_priv_kernel_destructive(state)) 7385 dtrace_action_panic(ecb); 7386 continue; 7387 7388 case DTRACEACT_STACK: 7389 if (!dtrace_priv_kernel(state)) 7390 continue; 7391 7392 dtrace_getpcstack((pc_t *)(tomax + valoffs), 7393 size / sizeof (pc_t), probe->dtpr_aframes, 7394 DTRACE_ANCHORED(probe) ? NULL : 7395 (uint32_t *)arg0); 7396 continue; 7397 7398 case DTRACEACT_JSTACK: 7399 case DTRACEACT_USTACK: 7400 if (!dtrace_priv_proc(state)) 7401 continue; 7402 7403 /* 7404 * See comment in DIF_VAR_PID. 7405 */ 7406 if (DTRACE_ANCHORED(mstate.dtms_probe) && 7407 CPU_ON_INTR(CPU)) { 7408 int depth = DTRACE_USTACK_NFRAMES( 7409 rec->dtrd_arg) + 1; 7410 7411 dtrace_bzero((void *)(tomax + valoffs), 7412 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 7413 + depth * sizeof (uint64_t)); 7414 7415 continue; 7416 } 7417 7418 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 7419 curproc->p_dtrace_helpers != NULL) { 7420 /* 7421 * This is the slow path -- we have 7422 * allocated string space, and we're 7423 * getting the stack of a process that 7424 * has helpers. Call into a separate 7425 * routine to perform this processing. 7426 */ 7427 dtrace_action_ustack(&mstate, state, 7428 (uint64_t *)(tomax + valoffs), 7429 rec->dtrd_arg); 7430 continue; 7431 } 7432 7433 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7434 dtrace_getupcstack((uint64_t *) 7435 (tomax + valoffs), 7436 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 7437 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7438 continue; 7439 7440 default: 7441 break; 7442 } 7443 7444 dp = act->dta_difo; 7445 ASSERT(dp != NULL); 7446 7447 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 7448 7449 if (*flags & CPU_DTRACE_ERROR) 7450 continue; 7451 7452 switch (act->dta_kind) { 7453 case DTRACEACT_SPECULATE: { 7454 dtrace_rechdr_t *dtrh; 7455 7456 ASSERT(buf == &state->dts_buffer[cpuid]); 7457 buf = dtrace_speculation_buffer(state, 7458 cpuid, val); 7459 7460 if (buf == NULL) { 7461 *flags |= CPU_DTRACE_DROP; 7462 continue; 7463 } 7464 7465 offs = dtrace_buffer_reserve(buf, 7466 ecb->dte_needed, ecb->dte_alignment, 7467 state, NULL); 7468 7469 if (offs < 0) { 7470 *flags |= CPU_DTRACE_DROP; 7471 continue; 7472 } 7473 7474 tomax = buf->dtb_tomax; 7475 ASSERT(tomax != NULL); 7476 7477 if (ecb->dte_size == 0) 7478 continue; 7479 7480 ASSERT3U(ecb->dte_size, >=, 7481 sizeof (dtrace_rechdr_t)); 7482 dtrh = ((void *)(tomax + offs)); 7483 dtrh->dtrh_epid = ecb->dte_epid; 7484 /* 7485 * When the speculation is committed, all of 7486 * the records in the speculative buffer will 7487 * have their timestamps set to the commit 7488 * time. Until then, it is set to a sentinel 7489 * value, for debugability. 7490 */ 7491 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 7492 continue; 7493 } 7494 7495 case DTRACEACT_PRINTM: { 7496 /* The DIF returns a 'memref'. */ 7497 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 7498 7499 /* Get the size from the memref. */ 7500 size = memref[1]; 7501 7502 /* 7503 * Check if the size exceeds the allocated 7504 * buffer size. 7505 */ 7506 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7507 /* Flag a drop! */ 7508 *flags |= CPU_DTRACE_DROP; 7509 continue; 7510 } 7511 7512 /* Store the size in the buffer first. */ 7513 DTRACE_STORE(uintptr_t, tomax, 7514 valoffs, size); 7515 7516 /* 7517 * Offset the buffer address to the start 7518 * of the data. 7519 */ 7520 valoffs += sizeof(uintptr_t); 7521 7522 /* 7523 * Reset to the memory address rather than 7524 * the memref array, then let the BYREF 7525 * code below do the work to store the 7526 * memory data in the buffer. 7527 */ 7528 val = memref[0]; 7529 break; 7530 } 7531 7532 case DTRACEACT_PRINTT: { 7533 /* The DIF returns a 'typeref'. */ 7534 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 7535 char c = '\0' + 1; 7536 size_t s; 7537 7538 /* 7539 * Get the type string length and round it 7540 * up so that the data that follows is 7541 * aligned for easy access. 7542 */ 7543 size_t typs = strlen((char *) typeref[2]) + 1; 7544 typs = roundup(typs, sizeof(uintptr_t)); 7545 7546 /* 7547 *Get the size from the typeref using the 7548 * number of elements and the type size. 7549 */ 7550 size = typeref[1] * typeref[3]; 7551 7552 /* 7553 * Check if the size exceeds the allocated 7554 * buffer size. 7555 */ 7556 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7557 /* Flag a drop! */ 7558 *flags |= CPU_DTRACE_DROP; 7559 7560 } 7561 7562 /* Store the size in the buffer first. */ 7563 DTRACE_STORE(uintptr_t, tomax, 7564 valoffs, size); 7565 valoffs += sizeof(uintptr_t); 7566 7567 /* Store the type size in the buffer. */ 7568 DTRACE_STORE(uintptr_t, tomax, 7569 valoffs, typeref[3]); 7570 valoffs += sizeof(uintptr_t); 7571 7572 val = typeref[2]; 7573 7574 for (s = 0; s < typs; s++) { 7575 if (c != '\0') 7576 c = dtrace_load8(val++); 7577 7578 DTRACE_STORE(uint8_t, tomax, 7579 valoffs++, c); 7580 } 7581 7582 /* 7583 * Reset to the memory address rather than 7584 * the typeref array, then let the BYREF 7585 * code below do the work to store the 7586 * memory data in the buffer. 7587 */ 7588 val = typeref[0]; 7589 break; 7590 } 7591 7592 case DTRACEACT_CHILL: 7593 if (dtrace_priv_kernel_destructive(state)) 7594 dtrace_action_chill(&mstate, val); 7595 continue; 7596 7597 case DTRACEACT_RAISE: 7598 if (dtrace_priv_proc_destructive(state)) 7599 dtrace_action_raise(val); 7600 continue; 7601 7602 case DTRACEACT_COMMIT: 7603 ASSERT(!committed); 7604 7605 /* 7606 * We need to commit our buffer state. 7607 */ 7608 if (ecb->dte_size) 7609 buf->dtb_offset = offs + ecb->dte_size; 7610 buf = &state->dts_buffer[cpuid]; 7611 dtrace_speculation_commit(state, cpuid, val); 7612 committed = 1; 7613 continue; 7614 7615 case DTRACEACT_DISCARD: 7616 dtrace_speculation_discard(state, cpuid, val); 7617 continue; 7618 7619 case DTRACEACT_DIFEXPR: 7620 case DTRACEACT_LIBACT: 7621 case DTRACEACT_PRINTF: 7622 case DTRACEACT_PRINTA: 7623 case DTRACEACT_SYSTEM: 7624 case DTRACEACT_FREOPEN: 7625 case DTRACEACT_TRACEMEM: 7626 break; 7627 7628 case DTRACEACT_TRACEMEM_DYNSIZE: 7629 tracememsize = val; 7630 break; 7631 7632 case DTRACEACT_SYM: 7633 case DTRACEACT_MOD: 7634 if (!dtrace_priv_kernel(state)) 7635 continue; 7636 break; 7637 7638 case DTRACEACT_USYM: 7639 case DTRACEACT_UMOD: 7640 case DTRACEACT_UADDR: { 7641#ifdef illumos 7642 struct pid *pid = curthread->t_procp->p_pidp; 7643#endif 7644 7645 if (!dtrace_priv_proc(state)) 7646 continue; 7647 7648 DTRACE_STORE(uint64_t, tomax, 7649#ifdef illumos 7650 valoffs, (uint64_t)pid->pid_id); 7651#else 7652 valoffs, (uint64_t) curproc->p_pid); 7653#endif 7654 DTRACE_STORE(uint64_t, tomax, 7655 valoffs + sizeof (uint64_t), val); 7656 7657 continue; 7658 } 7659 7660 case DTRACEACT_EXIT: { 7661 /* 7662 * For the exit action, we are going to attempt 7663 * to atomically set our activity to be 7664 * draining. If this fails (either because 7665 * another CPU has beat us to the exit action, 7666 * or because our current activity is something 7667 * other than ACTIVE or WARMUP), we will 7668 * continue. This assures that the exit action 7669 * can be successfully recorded at most once 7670 * when we're in the ACTIVE state. If we're 7671 * encountering the exit() action while in 7672 * COOLDOWN, however, we want to honor the new 7673 * status code. (We know that we're the only 7674 * thread in COOLDOWN, so there is no race.) 7675 */ 7676 void *activity = &state->dts_activity; 7677 dtrace_activity_t current = state->dts_activity; 7678 7679 if (current == DTRACE_ACTIVITY_COOLDOWN) 7680 break; 7681 7682 if (current != DTRACE_ACTIVITY_WARMUP) 7683 current = DTRACE_ACTIVITY_ACTIVE; 7684 7685 if (dtrace_cas32(activity, current, 7686 DTRACE_ACTIVITY_DRAINING) != current) { 7687 *flags |= CPU_DTRACE_DROP; 7688 continue; 7689 } 7690 7691 break; 7692 } 7693 7694 default: 7695 ASSERT(0); 7696 } 7697 7698 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF || 7699 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) { 7700 uintptr_t end = valoffs + size; 7701 7702 if (tracememsize != 0 && 7703 valoffs + tracememsize < end) { 7704 end = valoffs + tracememsize; 7705 tracememsize = 0; 7706 } 7707 7708 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF && 7709 !dtrace_vcanload((void *)(uintptr_t)val, 7710 &dp->dtdo_rtype, &mstate, vstate)) 7711 continue; 7712 7713 dtrace_store_by_ref(dp, tomax, size, &valoffs, 7714 &val, end, act->dta_intuple, 7715 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ? 7716 DIF_TF_BYREF: DIF_TF_BYUREF); 7717 continue; 7718 } 7719 7720 switch (size) { 7721 case 0: 7722 break; 7723 7724 case sizeof (uint8_t): 7725 DTRACE_STORE(uint8_t, tomax, valoffs, val); 7726 break; 7727 case sizeof (uint16_t): 7728 DTRACE_STORE(uint16_t, tomax, valoffs, val); 7729 break; 7730 case sizeof (uint32_t): 7731 DTRACE_STORE(uint32_t, tomax, valoffs, val); 7732 break; 7733 case sizeof (uint64_t): 7734 DTRACE_STORE(uint64_t, tomax, valoffs, val); 7735 break; 7736 default: 7737 /* 7738 * Any other size should have been returned by 7739 * reference, not by value. 7740 */ 7741 ASSERT(0); 7742 break; 7743 } 7744 } 7745 7746 if (*flags & CPU_DTRACE_DROP) 7747 continue; 7748 7749 if (*flags & CPU_DTRACE_FAULT) { 7750 int ndx; 7751 dtrace_action_t *err; 7752 7753 buf->dtb_errors++; 7754 7755 if (probe->dtpr_id == dtrace_probeid_error) { 7756 /* 7757 * There's nothing we can do -- we had an 7758 * error on the error probe. We bump an 7759 * error counter to at least indicate that 7760 * this condition happened. 7761 */ 7762 dtrace_error(&state->dts_dblerrors); 7763 continue; 7764 } 7765 7766 if (vtime) { 7767 /* 7768 * Before recursing on dtrace_probe(), we 7769 * need to explicitly clear out our start 7770 * time to prevent it from being accumulated 7771 * into t_dtrace_vtime. 7772 */ 7773 curthread->t_dtrace_start = 0; 7774 } 7775 7776 /* 7777 * Iterate over the actions to figure out which action 7778 * we were processing when we experienced the error. 7779 * Note that act points _past_ the faulting action; if 7780 * act is ecb->dte_action, the fault was in the 7781 * predicate, if it's ecb->dte_action->dta_next it's 7782 * in action #1, and so on. 7783 */ 7784 for (err = ecb->dte_action, ndx = 0; 7785 err != act; err = err->dta_next, ndx++) 7786 continue; 7787 7788 dtrace_probe_error(state, ecb->dte_epid, ndx, 7789 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 7790 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 7791 cpu_core[cpuid].cpuc_dtrace_illval); 7792 7793 continue; 7794 } 7795 7796 if (!committed) 7797 buf->dtb_offset = offs + ecb->dte_size; 7798 } 7799 7800 if (vtime) 7801 curthread->t_dtrace_start = dtrace_gethrtime(); 7802 7803 dtrace_interrupt_enable(cookie); 7804} 7805 7806/* 7807 * DTrace Probe Hashing Functions 7808 * 7809 * The functions in this section (and indeed, the functions in remaining 7810 * sections) are not _called_ from probe context. (Any exceptions to this are 7811 * marked with a "Note:".) Rather, they are called from elsewhere in the 7812 * DTrace framework to look-up probes in, add probes to and remove probes from 7813 * the DTrace probe hashes. (Each probe is hashed by each element of the 7814 * probe tuple -- allowing for fast lookups, regardless of what was 7815 * specified.) 7816 */ 7817static uint_t 7818dtrace_hash_str(const char *p) 7819{ 7820 unsigned int g; 7821 uint_t hval = 0; 7822 7823 while (*p) { 7824 hval = (hval << 4) + *p++; 7825 if ((g = (hval & 0xf0000000)) != 0) 7826 hval ^= g >> 24; 7827 hval &= ~g; 7828 } 7829 return (hval); 7830} 7831 7832static dtrace_hash_t * 7833dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 7834{ 7835 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 7836 7837 hash->dth_stroffs = stroffs; 7838 hash->dth_nextoffs = nextoffs; 7839 hash->dth_prevoffs = prevoffs; 7840 7841 hash->dth_size = 1; 7842 hash->dth_mask = hash->dth_size - 1; 7843 7844 hash->dth_tab = kmem_zalloc(hash->dth_size * 7845 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 7846 7847 return (hash); 7848} 7849 7850static void 7851dtrace_hash_destroy(dtrace_hash_t *hash) 7852{ 7853#ifdef DEBUG 7854 int i; 7855 7856 for (i = 0; i < hash->dth_size; i++) 7857 ASSERT(hash->dth_tab[i] == NULL); 7858#endif 7859 7860 kmem_free(hash->dth_tab, 7861 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 7862 kmem_free(hash, sizeof (dtrace_hash_t)); 7863} 7864 7865static void 7866dtrace_hash_resize(dtrace_hash_t *hash) 7867{ 7868 int size = hash->dth_size, i, ndx; 7869 int new_size = hash->dth_size << 1; 7870 int new_mask = new_size - 1; 7871 dtrace_hashbucket_t **new_tab, *bucket, *next; 7872 7873 ASSERT((new_size & new_mask) == 0); 7874 7875 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 7876 7877 for (i = 0; i < size; i++) { 7878 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 7879 dtrace_probe_t *probe = bucket->dthb_chain; 7880 7881 ASSERT(probe != NULL); 7882 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 7883 7884 next = bucket->dthb_next; 7885 bucket->dthb_next = new_tab[ndx]; 7886 new_tab[ndx] = bucket; 7887 } 7888 } 7889 7890 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 7891 hash->dth_tab = new_tab; 7892 hash->dth_size = new_size; 7893 hash->dth_mask = new_mask; 7894} 7895 7896static void 7897dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 7898{ 7899 int hashval = DTRACE_HASHSTR(hash, new); 7900 int ndx = hashval & hash->dth_mask; 7901 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7902 dtrace_probe_t **nextp, **prevp; 7903 7904 for (; bucket != NULL; bucket = bucket->dthb_next) { 7905 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 7906 goto add; 7907 } 7908 7909 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 7910 dtrace_hash_resize(hash); 7911 dtrace_hash_add(hash, new); 7912 return; 7913 } 7914 7915 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 7916 bucket->dthb_next = hash->dth_tab[ndx]; 7917 hash->dth_tab[ndx] = bucket; 7918 hash->dth_nbuckets++; 7919 7920add: 7921 nextp = DTRACE_HASHNEXT(hash, new); 7922 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 7923 *nextp = bucket->dthb_chain; 7924 7925 if (bucket->dthb_chain != NULL) { 7926 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 7927 ASSERT(*prevp == NULL); 7928 *prevp = new; 7929 } 7930 7931 bucket->dthb_chain = new; 7932 bucket->dthb_len++; 7933} 7934 7935static dtrace_probe_t * 7936dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 7937{ 7938 int hashval = DTRACE_HASHSTR(hash, template); 7939 int ndx = hashval & hash->dth_mask; 7940 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7941 7942 for (; bucket != NULL; bucket = bucket->dthb_next) { 7943 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 7944 return (bucket->dthb_chain); 7945 } 7946 7947 return (NULL); 7948} 7949 7950static int 7951dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 7952{ 7953 int hashval = DTRACE_HASHSTR(hash, template); 7954 int ndx = hashval & hash->dth_mask; 7955 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7956 7957 for (; bucket != NULL; bucket = bucket->dthb_next) { 7958 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 7959 return (bucket->dthb_len); 7960 } 7961 7962 return (0); 7963} 7964 7965static void 7966dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 7967{ 7968 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 7969 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7970 7971 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 7972 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 7973 7974 /* 7975 * Find the bucket that we're removing this probe from. 7976 */ 7977 for (; bucket != NULL; bucket = bucket->dthb_next) { 7978 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 7979 break; 7980 } 7981 7982 ASSERT(bucket != NULL); 7983 7984 if (*prevp == NULL) { 7985 if (*nextp == NULL) { 7986 /* 7987 * The removed probe was the only probe on this 7988 * bucket; we need to remove the bucket. 7989 */ 7990 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 7991 7992 ASSERT(bucket->dthb_chain == probe); 7993 ASSERT(b != NULL); 7994 7995 if (b == bucket) { 7996 hash->dth_tab[ndx] = bucket->dthb_next; 7997 } else { 7998 while (b->dthb_next != bucket) 7999 b = b->dthb_next; 8000 b->dthb_next = bucket->dthb_next; 8001 } 8002 8003 ASSERT(hash->dth_nbuckets > 0); 8004 hash->dth_nbuckets--; 8005 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 8006 return; 8007 } 8008 8009 bucket->dthb_chain = *nextp; 8010 } else { 8011 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 8012 } 8013 8014 if (*nextp != NULL) 8015 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 8016} 8017 8018/* 8019 * DTrace Utility Functions 8020 * 8021 * These are random utility functions that are _not_ called from probe context. 8022 */ 8023static int 8024dtrace_badattr(const dtrace_attribute_t *a) 8025{ 8026 return (a->dtat_name > DTRACE_STABILITY_MAX || 8027 a->dtat_data > DTRACE_STABILITY_MAX || 8028 a->dtat_class > DTRACE_CLASS_MAX); 8029} 8030 8031/* 8032 * Return a duplicate copy of a string. If the specified string is NULL, 8033 * this function returns a zero-length string. 8034 */ 8035static char * 8036dtrace_strdup(const char *str) 8037{ 8038 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 8039 8040 if (str != NULL) 8041 (void) strcpy(new, str); 8042 8043 return (new); 8044} 8045 8046#define DTRACE_ISALPHA(c) \ 8047 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 8048 8049static int 8050dtrace_badname(const char *s) 8051{ 8052 char c; 8053 8054 if (s == NULL || (c = *s++) == '\0') 8055 return (0); 8056 8057 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 8058 return (1); 8059 8060 while ((c = *s++) != '\0') { 8061 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 8062 c != '-' && c != '_' && c != '.' && c != '`') 8063 return (1); 8064 } 8065 8066 return (0); 8067} 8068 8069static void 8070dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 8071{ 8072 uint32_t priv; 8073 8074#ifdef illumos 8075 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 8076 /* 8077 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 8078 */ 8079 priv = DTRACE_PRIV_ALL; 8080 } else { 8081 *uidp = crgetuid(cr); 8082 *zoneidp = crgetzoneid(cr); 8083 8084 priv = 0; 8085 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 8086 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 8087 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 8088 priv |= DTRACE_PRIV_USER; 8089 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 8090 priv |= DTRACE_PRIV_PROC; 8091 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 8092 priv |= DTRACE_PRIV_OWNER; 8093 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 8094 priv |= DTRACE_PRIV_ZONEOWNER; 8095 } 8096#else 8097 priv = DTRACE_PRIV_ALL; 8098#endif 8099 8100 *privp = priv; 8101} 8102 8103#ifdef DTRACE_ERRDEBUG 8104static void 8105dtrace_errdebug(const char *str) 8106{ 8107 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 8108 int occupied = 0; 8109 8110 mutex_enter(&dtrace_errlock); 8111 dtrace_errlast = str; 8112 dtrace_errthread = curthread; 8113 8114 while (occupied++ < DTRACE_ERRHASHSZ) { 8115 if (dtrace_errhash[hval].dter_msg == str) { 8116 dtrace_errhash[hval].dter_count++; 8117 goto out; 8118 } 8119 8120 if (dtrace_errhash[hval].dter_msg != NULL) { 8121 hval = (hval + 1) % DTRACE_ERRHASHSZ; 8122 continue; 8123 } 8124 8125 dtrace_errhash[hval].dter_msg = str; 8126 dtrace_errhash[hval].dter_count = 1; 8127 goto out; 8128 } 8129 8130 panic("dtrace: undersized error hash"); 8131out: 8132 mutex_exit(&dtrace_errlock); 8133} 8134#endif 8135 8136/* 8137 * DTrace Matching Functions 8138 * 8139 * These functions are used to match groups of probes, given some elements of 8140 * a probe tuple, or some globbed expressions for elements of a probe tuple. 8141 */ 8142static int 8143dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 8144 zoneid_t zoneid) 8145{ 8146 if (priv != DTRACE_PRIV_ALL) { 8147 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 8148 uint32_t match = priv & ppriv; 8149 8150 /* 8151 * No PRIV_DTRACE_* privileges... 8152 */ 8153 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 8154 DTRACE_PRIV_KERNEL)) == 0) 8155 return (0); 8156 8157 /* 8158 * No matching bits, but there were bits to match... 8159 */ 8160 if (match == 0 && ppriv != 0) 8161 return (0); 8162 8163 /* 8164 * Need to have permissions to the process, but don't... 8165 */ 8166 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 8167 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 8168 return (0); 8169 } 8170 8171 /* 8172 * Need to be in the same zone unless we possess the 8173 * privilege to examine all zones. 8174 */ 8175 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 8176 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 8177 return (0); 8178 } 8179 } 8180 8181 return (1); 8182} 8183 8184/* 8185 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 8186 * consists of input pattern strings and an ops-vector to evaluate them. 8187 * This function returns >0 for match, 0 for no match, and <0 for error. 8188 */ 8189static int 8190dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 8191 uint32_t priv, uid_t uid, zoneid_t zoneid) 8192{ 8193 dtrace_provider_t *pvp = prp->dtpr_provider; 8194 int rv; 8195 8196 if (pvp->dtpv_defunct) 8197 return (0); 8198 8199 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 8200 return (rv); 8201 8202 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 8203 return (rv); 8204 8205 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 8206 return (rv); 8207 8208 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 8209 return (rv); 8210 8211 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 8212 return (0); 8213 8214 return (rv); 8215} 8216 8217/* 8218 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 8219 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 8220 * libc's version, the kernel version only applies to 8-bit ASCII strings. 8221 * In addition, all of the recursion cases except for '*' matching have been 8222 * unwound. For '*', we still implement recursive evaluation, but a depth 8223 * counter is maintained and matching is aborted if we recurse too deep. 8224 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 8225 */ 8226static int 8227dtrace_match_glob(const char *s, const char *p, int depth) 8228{ 8229 const char *olds; 8230 char s1, c; 8231 int gs; 8232 8233 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 8234 return (-1); 8235 8236 if (s == NULL) 8237 s = ""; /* treat NULL as empty string */ 8238 8239top: 8240 olds = s; 8241 s1 = *s++; 8242 8243 if (p == NULL) 8244 return (0); 8245 8246 if ((c = *p++) == '\0') 8247 return (s1 == '\0'); 8248 8249 switch (c) { 8250 case '[': { 8251 int ok = 0, notflag = 0; 8252 char lc = '\0'; 8253 8254 if (s1 == '\0') 8255 return (0); 8256 8257 if (*p == '!') { 8258 notflag = 1; 8259 p++; 8260 } 8261 8262 if ((c = *p++) == '\0') 8263 return (0); 8264 8265 do { 8266 if (c == '-' && lc != '\0' && *p != ']') { 8267 if ((c = *p++) == '\0') 8268 return (0); 8269 if (c == '\\' && (c = *p++) == '\0') 8270 return (0); 8271 8272 if (notflag) { 8273 if (s1 < lc || s1 > c) 8274 ok++; 8275 else 8276 return (0); 8277 } else if (lc <= s1 && s1 <= c) 8278 ok++; 8279 8280 } else if (c == '\\' && (c = *p++) == '\0') 8281 return (0); 8282 8283 lc = c; /* save left-hand 'c' for next iteration */ 8284 8285 if (notflag) { 8286 if (s1 != c) 8287 ok++; 8288 else 8289 return (0); 8290 } else if (s1 == c) 8291 ok++; 8292 8293 if ((c = *p++) == '\0') 8294 return (0); 8295 8296 } while (c != ']'); 8297 8298 if (ok) 8299 goto top; 8300 8301 return (0); 8302 } 8303 8304 case '\\': 8305 if ((c = *p++) == '\0') 8306 return (0); 8307 /*FALLTHRU*/ 8308 8309 default: 8310 if (c != s1) 8311 return (0); 8312 /*FALLTHRU*/ 8313 8314 case '?': 8315 if (s1 != '\0') 8316 goto top; 8317 return (0); 8318 8319 case '*': 8320 while (*p == '*') 8321 p++; /* consecutive *'s are identical to a single one */ 8322 8323 if (*p == '\0') 8324 return (1); 8325 8326 for (s = olds; *s != '\0'; s++) { 8327 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 8328 return (gs); 8329 } 8330 8331 return (0); 8332 } 8333} 8334 8335/*ARGSUSED*/ 8336static int 8337dtrace_match_string(const char *s, const char *p, int depth) 8338{ 8339 return (s != NULL && strcmp(s, p) == 0); 8340} 8341 8342/*ARGSUSED*/ 8343static int 8344dtrace_match_nul(const char *s, const char *p, int depth) 8345{ 8346 return (1); /* always match the empty pattern */ 8347} 8348 8349/*ARGSUSED*/ 8350static int 8351dtrace_match_nonzero(const char *s, const char *p, int depth) 8352{ 8353 return (s != NULL && s[0] != '\0'); 8354} 8355 8356static int 8357dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 8358 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 8359{ 8360 dtrace_probe_t template, *probe; 8361 dtrace_hash_t *hash = NULL; 8362 int len, best = INT_MAX, nmatched = 0; 8363 dtrace_id_t i; 8364 8365 ASSERT(MUTEX_HELD(&dtrace_lock)); 8366 8367 /* 8368 * If the probe ID is specified in the key, just lookup by ID and 8369 * invoke the match callback once if a matching probe is found. 8370 */ 8371 if (pkp->dtpk_id != DTRACE_IDNONE) { 8372 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 8373 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 8374 (void) (*matched)(probe, arg); 8375 nmatched++; 8376 } 8377 return (nmatched); 8378 } 8379 8380 template.dtpr_mod = (char *)pkp->dtpk_mod; 8381 template.dtpr_func = (char *)pkp->dtpk_func; 8382 template.dtpr_name = (char *)pkp->dtpk_name; 8383 8384 /* 8385 * We want to find the most distinct of the module name, function 8386 * name, and name. So for each one that is not a glob pattern or 8387 * empty string, we perform a lookup in the corresponding hash and 8388 * use the hash table with the fewest collisions to do our search. 8389 */ 8390 if (pkp->dtpk_mmatch == &dtrace_match_string && 8391 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 8392 best = len; 8393 hash = dtrace_bymod; 8394 } 8395 8396 if (pkp->dtpk_fmatch == &dtrace_match_string && 8397 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 8398 best = len; 8399 hash = dtrace_byfunc; 8400 } 8401 8402 if (pkp->dtpk_nmatch == &dtrace_match_string && 8403 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 8404 best = len; 8405 hash = dtrace_byname; 8406 } 8407 8408 /* 8409 * If we did not select a hash table, iterate over every probe and 8410 * invoke our callback for each one that matches our input probe key. 8411 */ 8412 if (hash == NULL) { 8413 for (i = 0; i < dtrace_nprobes; i++) { 8414 if ((probe = dtrace_probes[i]) == NULL || 8415 dtrace_match_probe(probe, pkp, priv, uid, 8416 zoneid) <= 0) 8417 continue; 8418 8419 nmatched++; 8420 8421 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8422 break; 8423 } 8424 8425 return (nmatched); 8426 } 8427 8428 /* 8429 * If we selected a hash table, iterate over each probe of the same key 8430 * name and invoke the callback for every probe that matches the other 8431 * attributes of our input probe key. 8432 */ 8433 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 8434 probe = *(DTRACE_HASHNEXT(hash, probe))) { 8435 8436 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 8437 continue; 8438 8439 nmatched++; 8440 8441 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8442 break; 8443 } 8444 8445 return (nmatched); 8446} 8447 8448/* 8449 * Return the function pointer dtrace_probecmp() should use to compare the 8450 * specified pattern with a string. For NULL or empty patterns, we select 8451 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 8452 * For non-empty non-glob strings, we use dtrace_match_string(). 8453 */ 8454static dtrace_probekey_f * 8455dtrace_probekey_func(const char *p) 8456{ 8457 char c; 8458 8459 if (p == NULL || *p == '\0') 8460 return (&dtrace_match_nul); 8461 8462 while ((c = *p++) != '\0') { 8463 if (c == '[' || c == '?' || c == '*' || c == '\\') 8464 return (&dtrace_match_glob); 8465 } 8466 8467 return (&dtrace_match_string); 8468} 8469 8470/* 8471 * Build a probe comparison key for use with dtrace_match_probe() from the 8472 * given probe description. By convention, a null key only matches anchored 8473 * probes: if each field is the empty string, reset dtpk_fmatch to 8474 * dtrace_match_nonzero(). 8475 */ 8476static void 8477dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 8478{ 8479 pkp->dtpk_prov = pdp->dtpd_provider; 8480 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 8481 8482 pkp->dtpk_mod = pdp->dtpd_mod; 8483 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 8484 8485 pkp->dtpk_func = pdp->dtpd_func; 8486 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 8487 8488 pkp->dtpk_name = pdp->dtpd_name; 8489 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 8490 8491 pkp->dtpk_id = pdp->dtpd_id; 8492 8493 if (pkp->dtpk_id == DTRACE_IDNONE && 8494 pkp->dtpk_pmatch == &dtrace_match_nul && 8495 pkp->dtpk_mmatch == &dtrace_match_nul && 8496 pkp->dtpk_fmatch == &dtrace_match_nul && 8497 pkp->dtpk_nmatch == &dtrace_match_nul) 8498 pkp->dtpk_fmatch = &dtrace_match_nonzero; 8499} 8500 8501/* 8502 * DTrace Provider-to-Framework API Functions 8503 * 8504 * These functions implement much of the Provider-to-Framework API, as 8505 * described in <sys/dtrace.h>. The parts of the API not in this section are 8506 * the functions in the API for probe management (found below), and 8507 * dtrace_probe() itself (found above). 8508 */ 8509 8510/* 8511 * Register the calling provider with the DTrace framework. This should 8512 * generally be called by DTrace providers in their attach(9E) entry point. 8513 */ 8514int 8515dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 8516 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 8517{ 8518 dtrace_provider_t *provider; 8519 8520 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 8521 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8522 "arguments", name ? name : "<NULL>"); 8523 return (EINVAL); 8524 } 8525 8526 if (name[0] == '\0' || dtrace_badname(name)) { 8527 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8528 "provider name", name); 8529 return (EINVAL); 8530 } 8531 8532 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 8533 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 8534 pops->dtps_destroy == NULL || 8535 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 8536 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8537 "provider ops", name); 8538 return (EINVAL); 8539 } 8540 8541 if (dtrace_badattr(&pap->dtpa_provider) || 8542 dtrace_badattr(&pap->dtpa_mod) || 8543 dtrace_badattr(&pap->dtpa_func) || 8544 dtrace_badattr(&pap->dtpa_name) || 8545 dtrace_badattr(&pap->dtpa_args)) { 8546 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8547 "provider attributes", name); 8548 return (EINVAL); 8549 } 8550 8551 if (priv & ~DTRACE_PRIV_ALL) { 8552 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8553 "privilege attributes", name); 8554 return (EINVAL); 8555 } 8556 8557 if ((priv & DTRACE_PRIV_KERNEL) && 8558 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 8559 pops->dtps_usermode == NULL) { 8560 cmn_err(CE_WARN, "failed to register provider '%s': need " 8561 "dtps_usermode() op for given privilege attributes", name); 8562 return (EINVAL); 8563 } 8564 8565 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 8566 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8567 (void) strcpy(provider->dtpv_name, name); 8568 8569 provider->dtpv_attr = *pap; 8570 provider->dtpv_priv.dtpp_flags = priv; 8571 if (cr != NULL) { 8572 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 8573 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 8574 } 8575 provider->dtpv_pops = *pops; 8576 8577 if (pops->dtps_provide == NULL) { 8578 ASSERT(pops->dtps_provide_module != NULL); 8579 provider->dtpv_pops.dtps_provide = 8580 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 8581 } 8582 8583 if (pops->dtps_provide_module == NULL) { 8584 ASSERT(pops->dtps_provide != NULL); 8585 provider->dtpv_pops.dtps_provide_module = 8586 (void (*)(void *, modctl_t *))dtrace_nullop; 8587 } 8588 8589 if (pops->dtps_suspend == NULL) { 8590 ASSERT(pops->dtps_resume == NULL); 8591 provider->dtpv_pops.dtps_suspend = 8592 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8593 provider->dtpv_pops.dtps_resume = 8594 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8595 } 8596 8597 provider->dtpv_arg = arg; 8598 *idp = (dtrace_provider_id_t)provider; 8599 8600 if (pops == &dtrace_provider_ops) { 8601 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8602 ASSERT(MUTEX_HELD(&dtrace_lock)); 8603 ASSERT(dtrace_anon.dta_enabling == NULL); 8604 8605 /* 8606 * We make sure that the DTrace provider is at the head of 8607 * the provider chain. 8608 */ 8609 provider->dtpv_next = dtrace_provider; 8610 dtrace_provider = provider; 8611 return (0); 8612 } 8613 8614 mutex_enter(&dtrace_provider_lock); 8615 mutex_enter(&dtrace_lock); 8616 8617 /* 8618 * If there is at least one provider registered, we'll add this 8619 * provider after the first provider. 8620 */ 8621 if (dtrace_provider != NULL) { 8622 provider->dtpv_next = dtrace_provider->dtpv_next; 8623 dtrace_provider->dtpv_next = provider; 8624 } else { 8625 dtrace_provider = provider; 8626 } 8627 8628 if (dtrace_retained != NULL) { 8629 dtrace_enabling_provide(provider); 8630 8631 /* 8632 * Now we need to call dtrace_enabling_matchall() -- which 8633 * will acquire cpu_lock and dtrace_lock. We therefore need 8634 * to drop all of our locks before calling into it... 8635 */ 8636 mutex_exit(&dtrace_lock); 8637 mutex_exit(&dtrace_provider_lock); 8638 dtrace_enabling_matchall(); 8639 8640 return (0); 8641 } 8642 8643 mutex_exit(&dtrace_lock); 8644 mutex_exit(&dtrace_provider_lock); 8645 8646 return (0); 8647} 8648 8649/* 8650 * Unregister the specified provider from the DTrace framework. This should 8651 * generally be called by DTrace providers in their detach(9E) entry point. 8652 */ 8653int 8654dtrace_unregister(dtrace_provider_id_t id) 8655{ 8656 dtrace_provider_t *old = (dtrace_provider_t *)id; 8657 dtrace_provider_t *prev = NULL; 8658 int i, self = 0, noreap = 0; 8659 dtrace_probe_t *probe, *first = NULL; 8660 8661 if (old->dtpv_pops.dtps_enable == 8662 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 8663 /* 8664 * If DTrace itself is the provider, we're called with locks 8665 * already held. 8666 */ 8667 ASSERT(old == dtrace_provider); 8668#ifdef illumos 8669 ASSERT(dtrace_devi != NULL); 8670#endif 8671 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8672 ASSERT(MUTEX_HELD(&dtrace_lock)); 8673 self = 1; 8674 8675 if (dtrace_provider->dtpv_next != NULL) { 8676 /* 8677 * There's another provider here; return failure. 8678 */ 8679 return (EBUSY); 8680 } 8681 } else { 8682 mutex_enter(&dtrace_provider_lock); 8683#ifdef illumos 8684 mutex_enter(&mod_lock); 8685#endif 8686 mutex_enter(&dtrace_lock); 8687 } 8688 8689 /* 8690 * If anyone has /dev/dtrace open, or if there are anonymous enabled 8691 * probes, we refuse to let providers slither away, unless this 8692 * provider has already been explicitly invalidated. 8693 */ 8694 if (!old->dtpv_defunct && 8695 (dtrace_opens || (dtrace_anon.dta_state != NULL && 8696 dtrace_anon.dta_state->dts_necbs > 0))) { 8697 if (!self) { 8698 mutex_exit(&dtrace_lock); 8699#ifdef illumos 8700 mutex_exit(&mod_lock); 8701#endif 8702 mutex_exit(&dtrace_provider_lock); 8703 } 8704 return (EBUSY); 8705 } 8706 8707 /* 8708 * Attempt to destroy the probes associated with this provider. 8709 */ 8710 for (i = 0; i < dtrace_nprobes; i++) { 8711 if ((probe = dtrace_probes[i]) == NULL) 8712 continue; 8713 8714 if (probe->dtpr_provider != old) 8715 continue; 8716 8717 if (probe->dtpr_ecb == NULL) 8718 continue; 8719 8720 /* 8721 * If we are trying to unregister a defunct provider, and the 8722 * provider was made defunct within the interval dictated by 8723 * dtrace_unregister_defunct_reap, we'll (asynchronously) 8724 * attempt to reap our enablings. To denote that the provider 8725 * should reattempt to unregister itself at some point in the 8726 * future, we will return a differentiable error code (EAGAIN 8727 * instead of EBUSY) in this case. 8728 */ 8729 if (dtrace_gethrtime() - old->dtpv_defunct > 8730 dtrace_unregister_defunct_reap) 8731 noreap = 1; 8732 8733 if (!self) { 8734 mutex_exit(&dtrace_lock); 8735#ifdef illumos 8736 mutex_exit(&mod_lock); 8737#endif 8738 mutex_exit(&dtrace_provider_lock); 8739 } 8740 8741 if (noreap) 8742 return (EBUSY); 8743 8744 (void) taskq_dispatch(dtrace_taskq, 8745 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 8746 8747 return (EAGAIN); 8748 } 8749 8750 /* 8751 * All of the probes for this provider are disabled; we can safely 8752 * remove all of them from their hash chains and from the probe array. 8753 */ 8754 for (i = 0; i < dtrace_nprobes; i++) { 8755 if ((probe = dtrace_probes[i]) == NULL) 8756 continue; 8757 8758 if (probe->dtpr_provider != old) 8759 continue; 8760 8761 dtrace_probes[i] = NULL; 8762 8763 dtrace_hash_remove(dtrace_bymod, probe); 8764 dtrace_hash_remove(dtrace_byfunc, probe); 8765 dtrace_hash_remove(dtrace_byname, probe); 8766 8767 if (first == NULL) { 8768 first = probe; 8769 probe->dtpr_nextmod = NULL; 8770 } else { 8771 probe->dtpr_nextmod = first; 8772 first = probe; 8773 } 8774 } 8775 8776 /* 8777 * The provider's probes have been removed from the hash chains and 8778 * from the probe array. Now issue a dtrace_sync() to be sure that 8779 * everyone has cleared out from any probe array processing. 8780 */ 8781 dtrace_sync(); 8782 8783 for (probe = first; probe != NULL; probe = first) { 8784 first = probe->dtpr_nextmod; 8785 8786 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 8787 probe->dtpr_arg); 8788 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8789 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8790 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8791#ifdef illumos 8792 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 8793#else 8794 free_unr(dtrace_arena, probe->dtpr_id); 8795#endif 8796 kmem_free(probe, sizeof (dtrace_probe_t)); 8797 } 8798 8799 if ((prev = dtrace_provider) == old) { 8800#ifdef illumos 8801 ASSERT(self || dtrace_devi == NULL); 8802 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 8803#endif 8804 dtrace_provider = old->dtpv_next; 8805 } else { 8806 while (prev != NULL && prev->dtpv_next != old) 8807 prev = prev->dtpv_next; 8808 8809 if (prev == NULL) { 8810 panic("attempt to unregister non-existent " 8811 "dtrace provider %p\n", (void *)id); 8812 } 8813 8814 prev->dtpv_next = old->dtpv_next; 8815 } 8816 8817 if (!self) { 8818 mutex_exit(&dtrace_lock); 8819#ifdef illumos 8820 mutex_exit(&mod_lock); 8821#endif 8822 mutex_exit(&dtrace_provider_lock); 8823 } 8824 8825 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 8826 kmem_free(old, sizeof (dtrace_provider_t)); 8827 8828 return (0); 8829} 8830 8831/* 8832 * Invalidate the specified provider. All subsequent probe lookups for the 8833 * specified provider will fail, but its probes will not be removed. 8834 */ 8835void 8836dtrace_invalidate(dtrace_provider_id_t id) 8837{ 8838 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 8839 8840 ASSERT(pvp->dtpv_pops.dtps_enable != 8841 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8842 8843 mutex_enter(&dtrace_provider_lock); 8844 mutex_enter(&dtrace_lock); 8845 8846 pvp->dtpv_defunct = dtrace_gethrtime(); 8847 8848 mutex_exit(&dtrace_lock); 8849 mutex_exit(&dtrace_provider_lock); 8850} 8851 8852/* 8853 * Indicate whether or not DTrace has attached. 8854 */ 8855int 8856dtrace_attached(void) 8857{ 8858 /* 8859 * dtrace_provider will be non-NULL iff the DTrace driver has 8860 * attached. (It's non-NULL because DTrace is always itself a 8861 * provider.) 8862 */ 8863 return (dtrace_provider != NULL); 8864} 8865 8866/* 8867 * Remove all the unenabled probes for the given provider. This function is 8868 * not unlike dtrace_unregister(), except that it doesn't remove the provider 8869 * -- just as many of its associated probes as it can. 8870 */ 8871int 8872dtrace_condense(dtrace_provider_id_t id) 8873{ 8874 dtrace_provider_t *prov = (dtrace_provider_t *)id; 8875 int i; 8876 dtrace_probe_t *probe; 8877 8878 /* 8879 * Make sure this isn't the dtrace provider itself. 8880 */ 8881 ASSERT(prov->dtpv_pops.dtps_enable != 8882 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8883 8884 mutex_enter(&dtrace_provider_lock); 8885 mutex_enter(&dtrace_lock); 8886 8887 /* 8888 * Attempt to destroy the probes associated with this provider. 8889 */ 8890 for (i = 0; i < dtrace_nprobes; i++) { 8891 if ((probe = dtrace_probes[i]) == NULL) 8892 continue; 8893 8894 if (probe->dtpr_provider != prov) 8895 continue; 8896 8897 if (probe->dtpr_ecb != NULL) 8898 continue; 8899 8900 dtrace_probes[i] = NULL; 8901 8902 dtrace_hash_remove(dtrace_bymod, probe); 8903 dtrace_hash_remove(dtrace_byfunc, probe); 8904 dtrace_hash_remove(dtrace_byname, probe); 8905 8906 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 8907 probe->dtpr_arg); 8908 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8909 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8910 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8911 kmem_free(probe, sizeof (dtrace_probe_t)); 8912#ifdef illumos 8913 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 8914#else 8915 free_unr(dtrace_arena, i + 1); 8916#endif 8917 } 8918 8919 mutex_exit(&dtrace_lock); 8920 mutex_exit(&dtrace_provider_lock); 8921 8922 return (0); 8923} 8924 8925/* 8926 * DTrace Probe Management Functions 8927 * 8928 * The functions in this section perform the DTrace probe management, 8929 * including functions to create probes, look-up probes, and call into the 8930 * providers to request that probes be provided. Some of these functions are 8931 * in the Provider-to-Framework API; these functions can be identified by the 8932 * fact that they are not declared "static". 8933 */ 8934 8935/* 8936 * Create a probe with the specified module name, function name, and name. 8937 */ 8938dtrace_id_t 8939dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 8940 const char *func, const char *name, int aframes, void *arg) 8941{ 8942 dtrace_probe_t *probe, **probes; 8943 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 8944 dtrace_id_t id; 8945 8946 if (provider == dtrace_provider) { 8947 ASSERT(MUTEX_HELD(&dtrace_lock)); 8948 } else { 8949 mutex_enter(&dtrace_lock); 8950 } 8951 8952#ifdef illumos 8953 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 8954 VM_BESTFIT | VM_SLEEP); 8955#else 8956 id = alloc_unr(dtrace_arena); 8957#endif 8958 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 8959 8960 probe->dtpr_id = id; 8961 probe->dtpr_gen = dtrace_probegen++; 8962 probe->dtpr_mod = dtrace_strdup(mod); 8963 probe->dtpr_func = dtrace_strdup(func); 8964 probe->dtpr_name = dtrace_strdup(name); 8965 probe->dtpr_arg = arg; 8966 probe->dtpr_aframes = aframes; 8967 probe->dtpr_provider = provider; 8968 8969 dtrace_hash_add(dtrace_bymod, probe); 8970 dtrace_hash_add(dtrace_byfunc, probe); 8971 dtrace_hash_add(dtrace_byname, probe); 8972 8973 if (id - 1 >= dtrace_nprobes) { 8974 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 8975 size_t nsize = osize << 1; 8976 8977 if (nsize == 0) { 8978 ASSERT(osize == 0); 8979 ASSERT(dtrace_probes == NULL); 8980 nsize = sizeof (dtrace_probe_t *); 8981 } 8982 8983 probes = kmem_zalloc(nsize, KM_SLEEP); 8984 8985 if (dtrace_probes == NULL) { 8986 ASSERT(osize == 0); 8987 dtrace_probes = probes; 8988 dtrace_nprobes = 1; 8989 } else { 8990 dtrace_probe_t **oprobes = dtrace_probes; 8991 8992 bcopy(oprobes, probes, osize); 8993 dtrace_membar_producer(); 8994 dtrace_probes = probes; 8995 8996 dtrace_sync(); 8997 8998 /* 8999 * All CPUs are now seeing the new probes array; we can 9000 * safely free the old array. 9001 */ 9002 kmem_free(oprobes, osize); 9003 dtrace_nprobes <<= 1; 9004 } 9005 9006 ASSERT(id - 1 < dtrace_nprobes); 9007 } 9008 9009 ASSERT(dtrace_probes[id - 1] == NULL); 9010 dtrace_probes[id - 1] = probe; 9011 9012 if (provider != dtrace_provider) 9013 mutex_exit(&dtrace_lock); 9014 9015 return (id); 9016} 9017 9018static dtrace_probe_t * 9019dtrace_probe_lookup_id(dtrace_id_t id) 9020{ 9021 ASSERT(MUTEX_HELD(&dtrace_lock)); 9022 9023 if (id == 0 || id > dtrace_nprobes) 9024 return (NULL); 9025 9026 return (dtrace_probes[id - 1]); 9027} 9028 9029static int 9030dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 9031{ 9032 *((dtrace_id_t *)arg) = probe->dtpr_id; 9033 9034 return (DTRACE_MATCH_DONE); 9035} 9036 9037/* 9038 * Look up a probe based on provider and one or more of module name, function 9039 * name and probe name. 9040 */ 9041dtrace_id_t 9042dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 9043 char *func, char *name) 9044{ 9045 dtrace_probekey_t pkey; 9046 dtrace_id_t id; 9047 int match; 9048 9049 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 9050 pkey.dtpk_pmatch = &dtrace_match_string; 9051 pkey.dtpk_mod = mod; 9052 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 9053 pkey.dtpk_func = func; 9054 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 9055 pkey.dtpk_name = name; 9056 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 9057 pkey.dtpk_id = DTRACE_IDNONE; 9058 9059 mutex_enter(&dtrace_lock); 9060 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 9061 dtrace_probe_lookup_match, &id); 9062 mutex_exit(&dtrace_lock); 9063 9064 ASSERT(match == 1 || match == 0); 9065 return (match ? id : 0); 9066} 9067 9068/* 9069 * Returns the probe argument associated with the specified probe. 9070 */ 9071void * 9072dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 9073{ 9074 dtrace_probe_t *probe; 9075 void *rval = NULL; 9076 9077 mutex_enter(&dtrace_lock); 9078 9079 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 9080 probe->dtpr_provider == (dtrace_provider_t *)id) 9081 rval = probe->dtpr_arg; 9082 9083 mutex_exit(&dtrace_lock); 9084 9085 return (rval); 9086} 9087 9088/* 9089 * Copy a probe into a probe description. 9090 */ 9091static void 9092dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 9093{ 9094 bzero(pdp, sizeof (dtrace_probedesc_t)); 9095 pdp->dtpd_id = prp->dtpr_id; 9096 9097 (void) strncpy(pdp->dtpd_provider, 9098 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 9099 9100 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 9101 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 9102 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 9103} 9104 9105/* 9106 * Called to indicate that a probe -- or probes -- should be provided by a 9107 * specfied provider. If the specified description is NULL, the provider will 9108 * be told to provide all of its probes. (This is done whenever a new 9109 * consumer comes along, or whenever a retained enabling is to be matched.) If 9110 * the specified description is non-NULL, the provider is given the 9111 * opportunity to dynamically provide the specified probe, allowing providers 9112 * to support the creation of probes on-the-fly. (So-called _autocreated_ 9113 * probes.) If the provider is NULL, the operations will be applied to all 9114 * providers; if the provider is non-NULL the operations will only be applied 9115 * to the specified provider. The dtrace_provider_lock must be held, and the 9116 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 9117 * will need to grab the dtrace_lock when it reenters the framework through 9118 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 9119 */ 9120static void 9121dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 9122{ 9123#ifdef illumos 9124 modctl_t *ctl; 9125#endif 9126 int all = 0; 9127 9128 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9129 9130 if (prv == NULL) { 9131 all = 1; 9132 prv = dtrace_provider; 9133 } 9134 9135 do { 9136 /* 9137 * First, call the blanket provide operation. 9138 */ 9139 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 9140 9141#ifdef illumos 9142 /* 9143 * Now call the per-module provide operation. We will grab 9144 * mod_lock to prevent the list from being modified. Note 9145 * that this also prevents the mod_busy bits from changing. 9146 * (mod_busy can only be changed with mod_lock held.) 9147 */ 9148 mutex_enter(&mod_lock); 9149 9150 ctl = &modules; 9151 do { 9152 if (ctl->mod_busy || ctl->mod_mp == NULL) 9153 continue; 9154 9155 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 9156 9157 } while ((ctl = ctl->mod_next) != &modules); 9158 9159 mutex_exit(&mod_lock); 9160#endif 9161 } while (all && (prv = prv->dtpv_next) != NULL); 9162} 9163 9164#ifdef illumos 9165/* 9166 * Iterate over each probe, and call the Framework-to-Provider API function 9167 * denoted by offs. 9168 */ 9169static void 9170dtrace_probe_foreach(uintptr_t offs) 9171{ 9172 dtrace_provider_t *prov; 9173 void (*func)(void *, dtrace_id_t, void *); 9174 dtrace_probe_t *probe; 9175 dtrace_icookie_t cookie; 9176 int i; 9177 9178 /* 9179 * We disable interrupts to walk through the probe array. This is 9180 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 9181 * won't see stale data. 9182 */ 9183 cookie = dtrace_interrupt_disable(); 9184 9185 for (i = 0; i < dtrace_nprobes; i++) { 9186 if ((probe = dtrace_probes[i]) == NULL) 9187 continue; 9188 9189 if (probe->dtpr_ecb == NULL) { 9190 /* 9191 * This probe isn't enabled -- don't call the function. 9192 */ 9193 continue; 9194 } 9195 9196 prov = probe->dtpr_provider; 9197 func = *((void(**)(void *, dtrace_id_t, void *)) 9198 ((uintptr_t)&prov->dtpv_pops + offs)); 9199 9200 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 9201 } 9202 9203 dtrace_interrupt_enable(cookie); 9204} 9205#endif 9206 9207static int 9208dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 9209{ 9210 dtrace_probekey_t pkey; 9211 uint32_t priv; 9212 uid_t uid; 9213 zoneid_t zoneid; 9214 9215 ASSERT(MUTEX_HELD(&dtrace_lock)); 9216 dtrace_ecb_create_cache = NULL; 9217 9218 if (desc == NULL) { 9219 /* 9220 * If we're passed a NULL description, we're being asked to 9221 * create an ECB with a NULL probe. 9222 */ 9223 (void) dtrace_ecb_create_enable(NULL, enab); 9224 return (0); 9225 } 9226 9227 dtrace_probekey(desc, &pkey); 9228 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 9229 &priv, &uid, &zoneid); 9230 9231 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 9232 enab)); 9233} 9234 9235/* 9236 * DTrace Helper Provider Functions 9237 */ 9238static void 9239dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 9240{ 9241 attr->dtat_name = DOF_ATTR_NAME(dofattr); 9242 attr->dtat_data = DOF_ATTR_DATA(dofattr); 9243 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 9244} 9245 9246static void 9247dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 9248 const dof_provider_t *dofprov, char *strtab) 9249{ 9250 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 9251 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 9252 dofprov->dofpv_provattr); 9253 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 9254 dofprov->dofpv_modattr); 9255 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 9256 dofprov->dofpv_funcattr); 9257 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 9258 dofprov->dofpv_nameattr); 9259 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 9260 dofprov->dofpv_argsattr); 9261} 9262 9263static void 9264dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9265{ 9266 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9267 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9268 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 9269 dof_provider_t *provider; 9270 dof_probe_t *probe; 9271 uint32_t *off, *enoff; 9272 uint8_t *arg; 9273 char *strtab; 9274 uint_t i, nprobes; 9275 dtrace_helper_provdesc_t dhpv; 9276 dtrace_helper_probedesc_t dhpb; 9277 dtrace_meta_t *meta = dtrace_meta_pid; 9278 dtrace_mops_t *mops = &meta->dtm_mops; 9279 void *parg; 9280 9281 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9282 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9283 provider->dofpv_strtab * dof->dofh_secsize); 9284 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9285 provider->dofpv_probes * dof->dofh_secsize); 9286 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9287 provider->dofpv_prargs * dof->dofh_secsize); 9288 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9289 provider->dofpv_proffs * dof->dofh_secsize); 9290 9291 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9292 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 9293 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 9294 enoff = NULL; 9295 9296 /* 9297 * See dtrace_helper_provider_validate(). 9298 */ 9299 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 9300 provider->dofpv_prenoffs != DOF_SECT_NONE) { 9301 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9302 provider->dofpv_prenoffs * dof->dofh_secsize); 9303 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 9304 } 9305 9306 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 9307 9308 /* 9309 * Create the provider. 9310 */ 9311 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9312 9313 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 9314 return; 9315 9316 meta->dtm_count++; 9317 9318 /* 9319 * Create the probes. 9320 */ 9321 for (i = 0; i < nprobes; i++) { 9322 probe = (dof_probe_t *)(uintptr_t)(daddr + 9323 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 9324 9325 /* See the check in dtrace_helper_provider_validate(). */ 9326 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) 9327 continue; 9328 9329 dhpb.dthpb_mod = dhp->dofhp_mod; 9330 dhpb.dthpb_func = strtab + probe->dofpr_func; 9331 dhpb.dthpb_name = strtab + probe->dofpr_name; 9332 dhpb.dthpb_base = probe->dofpr_addr; 9333 dhpb.dthpb_offs = off + probe->dofpr_offidx; 9334 dhpb.dthpb_noffs = probe->dofpr_noffs; 9335 if (enoff != NULL) { 9336 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 9337 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 9338 } else { 9339 dhpb.dthpb_enoffs = NULL; 9340 dhpb.dthpb_nenoffs = 0; 9341 } 9342 dhpb.dthpb_args = arg + probe->dofpr_argidx; 9343 dhpb.dthpb_nargc = probe->dofpr_nargc; 9344 dhpb.dthpb_xargc = probe->dofpr_xargc; 9345 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 9346 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 9347 9348 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 9349 } 9350} 9351 9352static void 9353dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 9354{ 9355 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9356 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9357 int i; 9358 9359 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9360 9361 for (i = 0; i < dof->dofh_secnum; i++) { 9362 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9363 dof->dofh_secoff + i * dof->dofh_secsize); 9364 9365 if (sec->dofs_type != DOF_SECT_PROVIDER) 9366 continue; 9367 9368 dtrace_helper_provide_one(dhp, sec, pid); 9369 } 9370 9371 /* 9372 * We may have just created probes, so we must now rematch against 9373 * any retained enablings. Note that this call will acquire both 9374 * cpu_lock and dtrace_lock; the fact that we are holding 9375 * dtrace_meta_lock now is what defines the ordering with respect to 9376 * these three locks. 9377 */ 9378 dtrace_enabling_matchall(); 9379} 9380 9381static void 9382dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9383{ 9384 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9385 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9386 dof_sec_t *str_sec; 9387 dof_provider_t *provider; 9388 char *strtab; 9389 dtrace_helper_provdesc_t dhpv; 9390 dtrace_meta_t *meta = dtrace_meta_pid; 9391 dtrace_mops_t *mops = &meta->dtm_mops; 9392 9393 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9394 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9395 provider->dofpv_strtab * dof->dofh_secsize); 9396 9397 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9398 9399 /* 9400 * Create the provider. 9401 */ 9402 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9403 9404 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 9405 9406 meta->dtm_count--; 9407} 9408 9409static void 9410dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 9411{ 9412 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9413 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9414 int i; 9415 9416 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9417 9418 for (i = 0; i < dof->dofh_secnum; i++) { 9419 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9420 dof->dofh_secoff + i * dof->dofh_secsize); 9421 9422 if (sec->dofs_type != DOF_SECT_PROVIDER) 9423 continue; 9424 9425 dtrace_helper_provider_remove_one(dhp, sec, pid); 9426 } 9427} 9428 9429/* 9430 * DTrace Meta Provider-to-Framework API Functions 9431 * 9432 * These functions implement the Meta Provider-to-Framework API, as described 9433 * in <sys/dtrace.h>. 9434 */ 9435int 9436dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 9437 dtrace_meta_provider_id_t *idp) 9438{ 9439 dtrace_meta_t *meta; 9440 dtrace_helpers_t *help, *next; 9441 int i; 9442 9443 *idp = DTRACE_METAPROVNONE; 9444 9445 /* 9446 * We strictly don't need the name, but we hold onto it for 9447 * debuggability. All hail error queues! 9448 */ 9449 if (name == NULL) { 9450 cmn_err(CE_WARN, "failed to register meta-provider: " 9451 "invalid name"); 9452 return (EINVAL); 9453 } 9454 9455 if (mops == NULL || 9456 mops->dtms_create_probe == NULL || 9457 mops->dtms_provide_pid == NULL || 9458 mops->dtms_remove_pid == NULL) { 9459 cmn_err(CE_WARN, "failed to register meta-register %s: " 9460 "invalid ops", name); 9461 return (EINVAL); 9462 } 9463 9464 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 9465 meta->dtm_mops = *mops; 9466 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 9467 (void) strcpy(meta->dtm_name, name); 9468 meta->dtm_arg = arg; 9469 9470 mutex_enter(&dtrace_meta_lock); 9471 mutex_enter(&dtrace_lock); 9472 9473 if (dtrace_meta_pid != NULL) { 9474 mutex_exit(&dtrace_lock); 9475 mutex_exit(&dtrace_meta_lock); 9476 cmn_err(CE_WARN, "failed to register meta-register %s: " 9477 "user-land meta-provider exists", name); 9478 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 9479 kmem_free(meta, sizeof (dtrace_meta_t)); 9480 return (EINVAL); 9481 } 9482 9483 dtrace_meta_pid = meta; 9484 *idp = (dtrace_meta_provider_id_t)meta; 9485 9486 /* 9487 * If there are providers and probes ready to go, pass them 9488 * off to the new meta provider now. 9489 */ 9490 9491 help = dtrace_deferred_pid; 9492 dtrace_deferred_pid = NULL; 9493 9494 mutex_exit(&dtrace_lock); 9495 9496 while (help != NULL) { 9497 for (i = 0; i < help->dthps_nprovs; i++) { 9498 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 9499 help->dthps_pid); 9500 } 9501 9502 next = help->dthps_next; 9503 help->dthps_next = NULL; 9504 help->dthps_prev = NULL; 9505 help->dthps_deferred = 0; 9506 help = next; 9507 } 9508 9509 mutex_exit(&dtrace_meta_lock); 9510 9511 return (0); 9512} 9513 9514int 9515dtrace_meta_unregister(dtrace_meta_provider_id_t id) 9516{ 9517 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 9518 9519 mutex_enter(&dtrace_meta_lock); 9520 mutex_enter(&dtrace_lock); 9521 9522 if (old == dtrace_meta_pid) { 9523 pp = &dtrace_meta_pid; 9524 } else { 9525 panic("attempt to unregister non-existent " 9526 "dtrace meta-provider %p\n", (void *)old); 9527 } 9528 9529 if (old->dtm_count != 0) { 9530 mutex_exit(&dtrace_lock); 9531 mutex_exit(&dtrace_meta_lock); 9532 return (EBUSY); 9533 } 9534 9535 *pp = NULL; 9536 9537 mutex_exit(&dtrace_lock); 9538 mutex_exit(&dtrace_meta_lock); 9539 9540 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 9541 kmem_free(old, sizeof (dtrace_meta_t)); 9542 9543 return (0); 9544} 9545 9546 9547/* 9548 * DTrace DIF Object Functions 9549 */ 9550static int 9551dtrace_difo_err(uint_t pc, const char *format, ...) 9552{ 9553 if (dtrace_err_verbose) { 9554 va_list alist; 9555 9556 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 9557 va_start(alist, format); 9558 (void) vuprintf(format, alist); 9559 va_end(alist); 9560 } 9561 9562#ifdef DTRACE_ERRDEBUG 9563 dtrace_errdebug(format); 9564#endif 9565 return (1); 9566} 9567 9568/* 9569 * Validate a DTrace DIF object by checking the IR instructions. The following 9570 * rules are currently enforced by dtrace_difo_validate(): 9571 * 9572 * 1. Each instruction must have a valid opcode 9573 * 2. Each register, string, variable, or subroutine reference must be valid 9574 * 3. No instruction can modify register %r0 (must be zero) 9575 * 4. All instruction reserved bits must be set to zero 9576 * 5. The last instruction must be a "ret" instruction 9577 * 6. All branch targets must reference a valid instruction _after_ the branch 9578 */ 9579static int 9580dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 9581 cred_t *cr) 9582{ 9583 int err = 0, i; 9584 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9585 int kcheckload; 9586 uint_t pc; 9587 9588 kcheckload = cr == NULL || 9589 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 9590 9591 dp->dtdo_destructive = 0; 9592 9593 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 9594 dif_instr_t instr = dp->dtdo_buf[pc]; 9595 9596 uint_t r1 = DIF_INSTR_R1(instr); 9597 uint_t r2 = DIF_INSTR_R2(instr); 9598 uint_t rd = DIF_INSTR_RD(instr); 9599 uint_t rs = DIF_INSTR_RS(instr); 9600 uint_t label = DIF_INSTR_LABEL(instr); 9601 uint_t v = DIF_INSTR_VAR(instr); 9602 uint_t subr = DIF_INSTR_SUBR(instr); 9603 uint_t type = DIF_INSTR_TYPE(instr); 9604 uint_t op = DIF_INSTR_OP(instr); 9605 9606 switch (op) { 9607 case DIF_OP_OR: 9608 case DIF_OP_XOR: 9609 case DIF_OP_AND: 9610 case DIF_OP_SLL: 9611 case DIF_OP_SRL: 9612 case DIF_OP_SRA: 9613 case DIF_OP_SUB: 9614 case DIF_OP_ADD: 9615 case DIF_OP_MUL: 9616 case DIF_OP_SDIV: 9617 case DIF_OP_UDIV: 9618 case DIF_OP_SREM: 9619 case DIF_OP_UREM: 9620 case DIF_OP_COPYS: 9621 if (r1 >= nregs) 9622 err += efunc(pc, "invalid register %u\n", r1); 9623 if (r2 >= nregs) 9624 err += efunc(pc, "invalid register %u\n", r2); 9625 if (rd >= nregs) 9626 err += efunc(pc, "invalid register %u\n", rd); 9627 if (rd == 0) 9628 err += efunc(pc, "cannot write to %r0\n"); 9629 break; 9630 case DIF_OP_NOT: 9631 case DIF_OP_MOV: 9632 case DIF_OP_ALLOCS: 9633 if (r1 >= nregs) 9634 err += efunc(pc, "invalid register %u\n", r1); 9635 if (r2 != 0) 9636 err += efunc(pc, "non-zero reserved bits\n"); 9637 if (rd >= nregs) 9638 err += efunc(pc, "invalid register %u\n", rd); 9639 if (rd == 0) 9640 err += efunc(pc, "cannot write to %r0\n"); 9641 break; 9642 case DIF_OP_LDSB: 9643 case DIF_OP_LDSH: 9644 case DIF_OP_LDSW: 9645 case DIF_OP_LDUB: 9646 case DIF_OP_LDUH: 9647 case DIF_OP_LDUW: 9648 case DIF_OP_LDX: 9649 if (r1 >= nregs) 9650 err += efunc(pc, "invalid register %u\n", r1); 9651 if (r2 != 0) 9652 err += efunc(pc, "non-zero reserved bits\n"); 9653 if (rd >= nregs) 9654 err += efunc(pc, "invalid register %u\n", rd); 9655 if (rd == 0) 9656 err += efunc(pc, "cannot write to %r0\n"); 9657 if (kcheckload) 9658 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 9659 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 9660 break; 9661 case DIF_OP_RLDSB: 9662 case DIF_OP_RLDSH: 9663 case DIF_OP_RLDSW: 9664 case DIF_OP_RLDUB: 9665 case DIF_OP_RLDUH: 9666 case DIF_OP_RLDUW: 9667 case DIF_OP_RLDX: 9668 if (r1 >= nregs) 9669 err += efunc(pc, "invalid register %u\n", r1); 9670 if (r2 != 0) 9671 err += efunc(pc, "non-zero reserved bits\n"); 9672 if (rd >= nregs) 9673 err += efunc(pc, "invalid register %u\n", rd); 9674 if (rd == 0) 9675 err += efunc(pc, "cannot write to %r0\n"); 9676 break; 9677 case DIF_OP_ULDSB: 9678 case DIF_OP_ULDSH: 9679 case DIF_OP_ULDSW: 9680 case DIF_OP_ULDUB: 9681 case DIF_OP_ULDUH: 9682 case DIF_OP_ULDUW: 9683 case DIF_OP_ULDX: 9684 if (r1 >= nregs) 9685 err += efunc(pc, "invalid register %u\n", r1); 9686 if (r2 != 0) 9687 err += efunc(pc, "non-zero reserved bits\n"); 9688 if (rd >= nregs) 9689 err += efunc(pc, "invalid register %u\n", rd); 9690 if (rd == 0) 9691 err += efunc(pc, "cannot write to %r0\n"); 9692 break; 9693 case DIF_OP_STB: 9694 case DIF_OP_STH: 9695 case DIF_OP_STW: 9696 case DIF_OP_STX: 9697 if (r1 >= nregs) 9698 err += efunc(pc, "invalid register %u\n", r1); 9699 if (r2 != 0) 9700 err += efunc(pc, "non-zero reserved bits\n"); 9701 if (rd >= nregs) 9702 err += efunc(pc, "invalid register %u\n", rd); 9703 if (rd == 0) 9704 err += efunc(pc, "cannot write to 0 address\n"); 9705 break; 9706 case DIF_OP_CMP: 9707 case DIF_OP_SCMP: 9708 if (r1 >= nregs) 9709 err += efunc(pc, "invalid register %u\n", r1); 9710 if (r2 >= nregs) 9711 err += efunc(pc, "invalid register %u\n", r2); 9712 if (rd != 0) 9713 err += efunc(pc, "non-zero reserved bits\n"); 9714 break; 9715 case DIF_OP_TST: 9716 if (r1 >= nregs) 9717 err += efunc(pc, "invalid register %u\n", r1); 9718 if (r2 != 0 || rd != 0) 9719 err += efunc(pc, "non-zero reserved bits\n"); 9720 break; 9721 case DIF_OP_BA: 9722 case DIF_OP_BE: 9723 case DIF_OP_BNE: 9724 case DIF_OP_BG: 9725 case DIF_OP_BGU: 9726 case DIF_OP_BGE: 9727 case DIF_OP_BGEU: 9728 case DIF_OP_BL: 9729 case DIF_OP_BLU: 9730 case DIF_OP_BLE: 9731 case DIF_OP_BLEU: 9732 if (label >= dp->dtdo_len) { 9733 err += efunc(pc, "invalid branch target %u\n", 9734 label); 9735 } 9736 if (label <= pc) { 9737 err += efunc(pc, "backward branch to %u\n", 9738 label); 9739 } 9740 break; 9741 case DIF_OP_RET: 9742 if (r1 != 0 || r2 != 0) 9743 err += efunc(pc, "non-zero reserved bits\n"); 9744 if (rd >= nregs) 9745 err += efunc(pc, "invalid register %u\n", rd); 9746 break; 9747 case DIF_OP_NOP: 9748 case DIF_OP_POPTS: 9749 case DIF_OP_FLUSHTS: 9750 if (r1 != 0 || r2 != 0 || rd != 0) 9751 err += efunc(pc, "non-zero reserved bits\n"); 9752 break; 9753 case DIF_OP_SETX: 9754 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 9755 err += efunc(pc, "invalid integer ref %u\n", 9756 DIF_INSTR_INTEGER(instr)); 9757 } 9758 if (rd >= nregs) 9759 err += efunc(pc, "invalid register %u\n", rd); 9760 if (rd == 0) 9761 err += efunc(pc, "cannot write to %r0\n"); 9762 break; 9763 case DIF_OP_SETS: 9764 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 9765 err += efunc(pc, "invalid string ref %u\n", 9766 DIF_INSTR_STRING(instr)); 9767 } 9768 if (rd >= nregs) 9769 err += efunc(pc, "invalid register %u\n", rd); 9770 if (rd == 0) 9771 err += efunc(pc, "cannot write to %r0\n"); 9772 break; 9773 case DIF_OP_LDGA: 9774 case DIF_OP_LDTA: 9775 if (r1 > DIF_VAR_ARRAY_MAX) 9776 err += efunc(pc, "invalid array %u\n", r1); 9777 if (r2 >= nregs) 9778 err += efunc(pc, "invalid register %u\n", r2); 9779 if (rd >= nregs) 9780 err += efunc(pc, "invalid register %u\n", rd); 9781 if (rd == 0) 9782 err += efunc(pc, "cannot write to %r0\n"); 9783 break; 9784 case DIF_OP_LDGS: 9785 case DIF_OP_LDTS: 9786 case DIF_OP_LDLS: 9787 case DIF_OP_LDGAA: 9788 case DIF_OP_LDTAA: 9789 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 9790 err += efunc(pc, "invalid variable %u\n", v); 9791 if (rd >= nregs) 9792 err += efunc(pc, "invalid register %u\n", rd); 9793 if (rd == 0) 9794 err += efunc(pc, "cannot write to %r0\n"); 9795 break; 9796 case DIF_OP_STGS: 9797 case DIF_OP_STTS: 9798 case DIF_OP_STLS: 9799 case DIF_OP_STGAA: 9800 case DIF_OP_STTAA: 9801 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 9802 err += efunc(pc, "invalid variable %u\n", v); 9803 if (rs >= nregs) 9804 err += efunc(pc, "invalid register %u\n", rd); 9805 break; 9806 case DIF_OP_CALL: 9807 if (subr > DIF_SUBR_MAX) 9808 err += efunc(pc, "invalid subr %u\n", subr); 9809 if (rd >= nregs) 9810 err += efunc(pc, "invalid register %u\n", rd); 9811 if (rd == 0) 9812 err += efunc(pc, "cannot write to %r0\n"); 9813 9814 if (subr == DIF_SUBR_COPYOUT || 9815 subr == DIF_SUBR_COPYOUTSTR) { 9816 dp->dtdo_destructive = 1; 9817 } 9818 9819 if (subr == DIF_SUBR_GETF) { 9820 /* 9821 * If we have a getf() we need to record that 9822 * in our state. Note that our state can be 9823 * NULL if this is a helper -- but in that 9824 * case, the call to getf() is itself illegal, 9825 * and will be caught (slightly later) when 9826 * the helper is validated. 9827 */ 9828 if (vstate->dtvs_state != NULL) 9829 vstate->dtvs_state->dts_getf++; 9830 } 9831 9832 break; 9833 case DIF_OP_PUSHTR: 9834 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 9835 err += efunc(pc, "invalid ref type %u\n", type); 9836 if (r2 >= nregs) 9837 err += efunc(pc, "invalid register %u\n", r2); 9838 if (rs >= nregs) 9839 err += efunc(pc, "invalid register %u\n", rs); 9840 break; 9841 case DIF_OP_PUSHTV: 9842 if (type != DIF_TYPE_CTF) 9843 err += efunc(pc, "invalid val type %u\n", type); 9844 if (r2 >= nregs) 9845 err += efunc(pc, "invalid register %u\n", r2); 9846 if (rs >= nregs) 9847 err += efunc(pc, "invalid register %u\n", rs); 9848 break; 9849 default: 9850 err += efunc(pc, "invalid opcode %u\n", 9851 DIF_INSTR_OP(instr)); 9852 } 9853 } 9854 9855 if (dp->dtdo_len != 0 && 9856 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 9857 err += efunc(dp->dtdo_len - 1, 9858 "expected 'ret' as last DIF instruction\n"); 9859 } 9860 9861 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) { 9862 /* 9863 * If we're not returning by reference, the size must be either 9864 * 0 or the size of one of the base types. 9865 */ 9866 switch (dp->dtdo_rtype.dtdt_size) { 9867 case 0: 9868 case sizeof (uint8_t): 9869 case sizeof (uint16_t): 9870 case sizeof (uint32_t): 9871 case sizeof (uint64_t): 9872 break; 9873 9874 default: 9875 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 9876 } 9877 } 9878 9879 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 9880 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 9881 dtrace_diftype_t *vt, *et; 9882 uint_t id, ndx; 9883 9884 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 9885 v->dtdv_scope != DIFV_SCOPE_THREAD && 9886 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 9887 err += efunc(i, "unrecognized variable scope %d\n", 9888 v->dtdv_scope); 9889 break; 9890 } 9891 9892 if (v->dtdv_kind != DIFV_KIND_ARRAY && 9893 v->dtdv_kind != DIFV_KIND_SCALAR) { 9894 err += efunc(i, "unrecognized variable type %d\n", 9895 v->dtdv_kind); 9896 break; 9897 } 9898 9899 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 9900 err += efunc(i, "%d exceeds variable id limit\n", id); 9901 break; 9902 } 9903 9904 if (id < DIF_VAR_OTHER_UBASE) 9905 continue; 9906 9907 /* 9908 * For user-defined variables, we need to check that this 9909 * definition is identical to any previous definition that we 9910 * encountered. 9911 */ 9912 ndx = id - DIF_VAR_OTHER_UBASE; 9913 9914 switch (v->dtdv_scope) { 9915 case DIFV_SCOPE_GLOBAL: 9916 if (ndx < vstate->dtvs_nglobals) { 9917 dtrace_statvar_t *svar; 9918 9919 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 9920 existing = &svar->dtsv_var; 9921 } 9922 9923 break; 9924 9925 case DIFV_SCOPE_THREAD: 9926 if (ndx < vstate->dtvs_ntlocals) 9927 existing = &vstate->dtvs_tlocals[ndx]; 9928 break; 9929 9930 case DIFV_SCOPE_LOCAL: 9931 if (ndx < vstate->dtvs_nlocals) { 9932 dtrace_statvar_t *svar; 9933 9934 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 9935 existing = &svar->dtsv_var; 9936 } 9937 9938 break; 9939 } 9940 9941 vt = &v->dtdv_type; 9942 9943 if (vt->dtdt_flags & DIF_TF_BYREF) { 9944 if (vt->dtdt_size == 0) { 9945 err += efunc(i, "zero-sized variable\n"); 9946 break; 9947 } 9948 9949 if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL || 9950 v->dtdv_scope == DIFV_SCOPE_LOCAL) && 9951 vt->dtdt_size > dtrace_statvar_maxsize) { 9952 err += efunc(i, "oversized by-ref static\n"); 9953 break; 9954 } 9955 } 9956 9957 if (existing == NULL || existing->dtdv_id == 0) 9958 continue; 9959 9960 ASSERT(existing->dtdv_id == v->dtdv_id); 9961 ASSERT(existing->dtdv_scope == v->dtdv_scope); 9962 9963 if (existing->dtdv_kind != v->dtdv_kind) 9964 err += efunc(i, "%d changed variable kind\n", id); 9965 9966 et = &existing->dtdv_type; 9967 9968 if (vt->dtdt_flags != et->dtdt_flags) { 9969 err += efunc(i, "%d changed variable type flags\n", id); 9970 break; 9971 } 9972 9973 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 9974 err += efunc(i, "%d changed variable type size\n", id); 9975 break; 9976 } 9977 } 9978 9979 return (err); 9980} 9981 9982/* 9983 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 9984 * are much more constrained than normal DIFOs. Specifically, they may 9985 * not: 9986 * 9987 * 1. Make calls to subroutines other than copyin(), copyinstr() or 9988 * miscellaneous string routines 9989 * 2. Access DTrace variables other than the args[] array, and the 9990 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 9991 * 3. Have thread-local variables. 9992 * 4. Have dynamic variables. 9993 */ 9994static int 9995dtrace_difo_validate_helper(dtrace_difo_t *dp) 9996{ 9997 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9998 int err = 0; 9999 uint_t pc; 10000 10001 for (pc = 0; pc < dp->dtdo_len; pc++) { 10002 dif_instr_t instr = dp->dtdo_buf[pc]; 10003 10004 uint_t v = DIF_INSTR_VAR(instr); 10005 uint_t subr = DIF_INSTR_SUBR(instr); 10006 uint_t op = DIF_INSTR_OP(instr); 10007 10008 switch (op) { 10009 case DIF_OP_OR: 10010 case DIF_OP_XOR: 10011 case DIF_OP_AND: 10012 case DIF_OP_SLL: 10013 case DIF_OP_SRL: 10014 case DIF_OP_SRA: 10015 case DIF_OP_SUB: 10016 case DIF_OP_ADD: 10017 case DIF_OP_MUL: 10018 case DIF_OP_SDIV: 10019 case DIF_OP_UDIV: 10020 case DIF_OP_SREM: 10021 case DIF_OP_UREM: 10022 case DIF_OP_COPYS: 10023 case DIF_OP_NOT: 10024 case DIF_OP_MOV: 10025 case DIF_OP_RLDSB: 10026 case DIF_OP_RLDSH: 10027 case DIF_OP_RLDSW: 10028 case DIF_OP_RLDUB: 10029 case DIF_OP_RLDUH: 10030 case DIF_OP_RLDUW: 10031 case DIF_OP_RLDX: 10032 case DIF_OP_ULDSB: 10033 case DIF_OP_ULDSH: 10034 case DIF_OP_ULDSW: 10035 case DIF_OP_ULDUB: 10036 case DIF_OP_ULDUH: 10037 case DIF_OP_ULDUW: 10038 case DIF_OP_ULDX: 10039 case DIF_OP_STB: 10040 case DIF_OP_STH: 10041 case DIF_OP_STW: 10042 case DIF_OP_STX: 10043 case DIF_OP_ALLOCS: 10044 case DIF_OP_CMP: 10045 case DIF_OP_SCMP: 10046 case DIF_OP_TST: 10047 case DIF_OP_BA: 10048 case DIF_OP_BE: 10049 case DIF_OP_BNE: 10050 case DIF_OP_BG: 10051 case DIF_OP_BGU: 10052 case DIF_OP_BGE: 10053 case DIF_OP_BGEU: 10054 case DIF_OP_BL: 10055 case DIF_OP_BLU: 10056 case DIF_OP_BLE: 10057 case DIF_OP_BLEU: 10058 case DIF_OP_RET: 10059 case DIF_OP_NOP: 10060 case DIF_OP_POPTS: 10061 case DIF_OP_FLUSHTS: 10062 case DIF_OP_SETX: 10063 case DIF_OP_SETS: 10064 case DIF_OP_LDGA: 10065 case DIF_OP_LDLS: 10066 case DIF_OP_STGS: 10067 case DIF_OP_STLS: 10068 case DIF_OP_PUSHTR: 10069 case DIF_OP_PUSHTV: 10070 break; 10071 10072 case DIF_OP_LDGS: 10073 if (v >= DIF_VAR_OTHER_UBASE) 10074 break; 10075 10076 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 10077 break; 10078 10079 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 10080 v == DIF_VAR_PPID || v == DIF_VAR_TID || 10081 v == DIF_VAR_EXECARGS || 10082 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 10083 v == DIF_VAR_UID || v == DIF_VAR_GID) 10084 break; 10085 10086 err += efunc(pc, "illegal variable %u\n", v); 10087 break; 10088 10089 case DIF_OP_LDTA: 10090 case DIF_OP_LDTS: 10091 case DIF_OP_LDGAA: 10092 case DIF_OP_LDTAA: 10093 err += efunc(pc, "illegal dynamic variable load\n"); 10094 break; 10095 10096 case DIF_OP_STTS: 10097 case DIF_OP_STGAA: 10098 case DIF_OP_STTAA: 10099 err += efunc(pc, "illegal dynamic variable store\n"); 10100 break; 10101 10102 case DIF_OP_CALL: 10103 if (subr == DIF_SUBR_ALLOCA || 10104 subr == DIF_SUBR_BCOPY || 10105 subr == DIF_SUBR_COPYIN || 10106 subr == DIF_SUBR_COPYINTO || 10107 subr == DIF_SUBR_COPYINSTR || 10108 subr == DIF_SUBR_INDEX || 10109 subr == DIF_SUBR_INET_NTOA || 10110 subr == DIF_SUBR_INET_NTOA6 || 10111 subr == DIF_SUBR_INET_NTOP || 10112 subr == DIF_SUBR_JSON || 10113 subr == DIF_SUBR_LLTOSTR || 10114 subr == DIF_SUBR_STRTOLL || 10115 subr == DIF_SUBR_RINDEX || 10116 subr == DIF_SUBR_STRCHR || 10117 subr == DIF_SUBR_STRJOIN || 10118 subr == DIF_SUBR_STRRCHR || 10119 subr == DIF_SUBR_STRSTR || 10120 subr == DIF_SUBR_HTONS || 10121 subr == DIF_SUBR_HTONL || 10122 subr == DIF_SUBR_HTONLL || 10123 subr == DIF_SUBR_NTOHS || 10124 subr == DIF_SUBR_NTOHL || 10125 subr == DIF_SUBR_NTOHLL || 10126 subr == DIF_SUBR_MEMREF || 10127#ifndef illumos 10128 subr == DIF_SUBR_MEMSTR || 10129#endif 10130 subr == DIF_SUBR_TYPEREF) 10131 break; 10132 10133 err += efunc(pc, "invalid subr %u\n", subr); 10134 break; 10135 10136 default: 10137 err += efunc(pc, "invalid opcode %u\n", 10138 DIF_INSTR_OP(instr)); 10139 } 10140 } 10141 10142 return (err); 10143} 10144 10145/* 10146 * Returns 1 if the expression in the DIF object can be cached on a per-thread 10147 * basis; 0 if not. 10148 */ 10149static int 10150dtrace_difo_cacheable(dtrace_difo_t *dp) 10151{ 10152 int i; 10153 10154 if (dp == NULL) 10155 return (0); 10156 10157 for (i = 0; i < dp->dtdo_varlen; i++) { 10158 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10159 10160 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 10161 continue; 10162 10163 switch (v->dtdv_id) { 10164 case DIF_VAR_CURTHREAD: 10165 case DIF_VAR_PID: 10166 case DIF_VAR_TID: 10167 case DIF_VAR_EXECARGS: 10168 case DIF_VAR_EXECNAME: 10169 case DIF_VAR_ZONENAME: 10170 break; 10171 10172 default: 10173 return (0); 10174 } 10175 } 10176 10177 /* 10178 * This DIF object may be cacheable. Now we need to look for any 10179 * array loading instructions, any memory loading instructions, or 10180 * any stores to thread-local variables. 10181 */ 10182 for (i = 0; i < dp->dtdo_len; i++) { 10183 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 10184 10185 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 10186 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 10187 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 10188 op == DIF_OP_LDGA || op == DIF_OP_STTS) 10189 return (0); 10190 } 10191 10192 return (1); 10193} 10194 10195static void 10196dtrace_difo_hold(dtrace_difo_t *dp) 10197{ 10198 int i; 10199 10200 ASSERT(MUTEX_HELD(&dtrace_lock)); 10201 10202 dp->dtdo_refcnt++; 10203 ASSERT(dp->dtdo_refcnt != 0); 10204 10205 /* 10206 * We need to check this DIF object for references to the variable 10207 * DIF_VAR_VTIMESTAMP. 10208 */ 10209 for (i = 0; i < dp->dtdo_varlen; i++) { 10210 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10211 10212 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10213 continue; 10214 10215 if (dtrace_vtime_references++ == 0) 10216 dtrace_vtime_enable(); 10217 } 10218} 10219 10220/* 10221 * This routine calculates the dynamic variable chunksize for a given DIF 10222 * object. The calculation is not fool-proof, and can probably be tricked by 10223 * malicious DIF -- but it works for all compiler-generated DIF. Because this 10224 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 10225 * if a dynamic variable size exceeds the chunksize. 10226 */ 10227static void 10228dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10229{ 10230 uint64_t sval = 0; 10231 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 10232 const dif_instr_t *text = dp->dtdo_buf; 10233 uint_t pc, srd = 0; 10234 uint_t ttop = 0; 10235 size_t size, ksize; 10236 uint_t id, i; 10237 10238 for (pc = 0; pc < dp->dtdo_len; pc++) { 10239 dif_instr_t instr = text[pc]; 10240 uint_t op = DIF_INSTR_OP(instr); 10241 uint_t rd = DIF_INSTR_RD(instr); 10242 uint_t r1 = DIF_INSTR_R1(instr); 10243 uint_t nkeys = 0; 10244 uchar_t scope = 0; 10245 10246 dtrace_key_t *key = tupregs; 10247 10248 switch (op) { 10249 case DIF_OP_SETX: 10250 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 10251 srd = rd; 10252 continue; 10253 10254 case DIF_OP_STTS: 10255 key = &tupregs[DIF_DTR_NREGS]; 10256 key[0].dttk_size = 0; 10257 key[1].dttk_size = 0; 10258 nkeys = 2; 10259 scope = DIFV_SCOPE_THREAD; 10260 break; 10261 10262 case DIF_OP_STGAA: 10263 case DIF_OP_STTAA: 10264 nkeys = ttop; 10265 10266 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 10267 key[nkeys++].dttk_size = 0; 10268 10269 key[nkeys++].dttk_size = 0; 10270 10271 if (op == DIF_OP_STTAA) { 10272 scope = DIFV_SCOPE_THREAD; 10273 } else { 10274 scope = DIFV_SCOPE_GLOBAL; 10275 } 10276 10277 break; 10278 10279 case DIF_OP_PUSHTR: 10280 if (ttop == DIF_DTR_NREGS) 10281 return; 10282 10283 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 10284 /* 10285 * If the register for the size of the "pushtr" 10286 * is %r0 (or the value is 0) and the type is 10287 * a string, we'll use the system-wide default 10288 * string size. 10289 */ 10290 tupregs[ttop++].dttk_size = 10291 dtrace_strsize_default; 10292 } else { 10293 if (srd == 0) 10294 return; 10295 10296 if (sval > LONG_MAX) 10297 return; 10298 10299 tupregs[ttop++].dttk_size = sval; 10300 } 10301 10302 break; 10303 10304 case DIF_OP_PUSHTV: 10305 if (ttop == DIF_DTR_NREGS) 10306 return; 10307 10308 tupregs[ttop++].dttk_size = 0; 10309 break; 10310 10311 case DIF_OP_FLUSHTS: 10312 ttop = 0; 10313 break; 10314 10315 case DIF_OP_POPTS: 10316 if (ttop != 0) 10317 ttop--; 10318 break; 10319 } 10320 10321 sval = 0; 10322 srd = 0; 10323 10324 if (nkeys == 0) 10325 continue; 10326 10327 /* 10328 * We have a dynamic variable allocation; calculate its size. 10329 */ 10330 for (ksize = 0, i = 0; i < nkeys; i++) 10331 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 10332 10333 size = sizeof (dtrace_dynvar_t); 10334 size += sizeof (dtrace_key_t) * (nkeys - 1); 10335 size += ksize; 10336 10337 /* 10338 * Now we need to determine the size of the stored data. 10339 */ 10340 id = DIF_INSTR_VAR(instr); 10341 10342 for (i = 0; i < dp->dtdo_varlen; i++) { 10343 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10344 10345 if (v->dtdv_id == id && v->dtdv_scope == scope) { 10346 size += v->dtdv_type.dtdt_size; 10347 break; 10348 } 10349 } 10350 10351 if (i == dp->dtdo_varlen) 10352 return; 10353 10354 /* 10355 * We have the size. If this is larger than the chunk size 10356 * for our dynamic variable state, reset the chunk size. 10357 */ 10358 size = P2ROUNDUP(size, sizeof (uint64_t)); 10359 10360 /* 10361 * Before setting the chunk size, check that we're not going 10362 * to set it to a negative value... 10363 */ 10364 if (size > LONG_MAX) 10365 return; 10366 10367 /* 10368 * ...and make certain that we didn't badly overflow. 10369 */ 10370 if (size < ksize || size < sizeof (dtrace_dynvar_t)) 10371 return; 10372 10373 if (size > vstate->dtvs_dynvars.dtds_chunksize) 10374 vstate->dtvs_dynvars.dtds_chunksize = size; 10375 } 10376} 10377 10378static void 10379dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10380{ 10381 int i, oldsvars, osz, nsz, otlocals, ntlocals; 10382 uint_t id; 10383 10384 ASSERT(MUTEX_HELD(&dtrace_lock)); 10385 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 10386 10387 for (i = 0; i < dp->dtdo_varlen; i++) { 10388 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10389 dtrace_statvar_t *svar, ***svarp = NULL; 10390 size_t dsize = 0; 10391 uint8_t scope = v->dtdv_scope; 10392 int *np = NULL; 10393 10394 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10395 continue; 10396 10397 id -= DIF_VAR_OTHER_UBASE; 10398 10399 switch (scope) { 10400 case DIFV_SCOPE_THREAD: 10401 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 10402 dtrace_difv_t *tlocals; 10403 10404 if ((ntlocals = (otlocals << 1)) == 0) 10405 ntlocals = 1; 10406 10407 osz = otlocals * sizeof (dtrace_difv_t); 10408 nsz = ntlocals * sizeof (dtrace_difv_t); 10409 10410 tlocals = kmem_zalloc(nsz, KM_SLEEP); 10411 10412 if (osz != 0) { 10413 bcopy(vstate->dtvs_tlocals, 10414 tlocals, osz); 10415 kmem_free(vstate->dtvs_tlocals, osz); 10416 } 10417 10418 vstate->dtvs_tlocals = tlocals; 10419 vstate->dtvs_ntlocals = ntlocals; 10420 } 10421 10422 vstate->dtvs_tlocals[id] = *v; 10423 continue; 10424 10425 case DIFV_SCOPE_LOCAL: 10426 np = &vstate->dtvs_nlocals; 10427 svarp = &vstate->dtvs_locals; 10428 10429 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10430 dsize = NCPU * (v->dtdv_type.dtdt_size + 10431 sizeof (uint64_t)); 10432 else 10433 dsize = NCPU * sizeof (uint64_t); 10434 10435 break; 10436 10437 case DIFV_SCOPE_GLOBAL: 10438 np = &vstate->dtvs_nglobals; 10439 svarp = &vstate->dtvs_globals; 10440 10441 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10442 dsize = v->dtdv_type.dtdt_size + 10443 sizeof (uint64_t); 10444 10445 break; 10446 10447 default: 10448 ASSERT(0); 10449 } 10450 10451 while (id >= (oldsvars = *np)) { 10452 dtrace_statvar_t **statics; 10453 int newsvars, oldsize, newsize; 10454 10455 if ((newsvars = (oldsvars << 1)) == 0) 10456 newsvars = 1; 10457 10458 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 10459 newsize = newsvars * sizeof (dtrace_statvar_t *); 10460 10461 statics = kmem_zalloc(newsize, KM_SLEEP); 10462 10463 if (oldsize != 0) { 10464 bcopy(*svarp, statics, oldsize); 10465 kmem_free(*svarp, oldsize); 10466 } 10467 10468 *svarp = statics; 10469 *np = newsvars; 10470 } 10471 10472 if ((svar = (*svarp)[id]) == NULL) { 10473 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 10474 svar->dtsv_var = *v; 10475 10476 if ((svar->dtsv_size = dsize) != 0) { 10477 svar->dtsv_data = (uint64_t)(uintptr_t) 10478 kmem_zalloc(dsize, KM_SLEEP); 10479 } 10480 10481 (*svarp)[id] = svar; 10482 } 10483 10484 svar->dtsv_refcnt++; 10485 } 10486 10487 dtrace_difo_chunksize(dp, vstate); 10488 dtrace_difo_hold(dp); 10489} 10490 10491static dtrace_difo_t * 10492dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10493{ 10494 dtrace_difo_t *new; 10495 size_t sz; 10496 10497 ASSERT(dp->dtdo_buf != NULL); 10498 ASSERT(dp->dtdo_refcnt != 0); 10499 10500 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10501 10502 ASSERT(dp->dtdo_buf != NULL); 10503 sz = dp->dtdo_len * sizeof (dif_instr_t); 10504 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 10505 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 10506 new->dtdo_len = dp->dtdo_len; 10507 10508 if (dp->dtdo_strtab != NULL) { 10509 ASSERT(dp->dtdo_strlen != 0); 10510 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 10511 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 10512 new->dtdo_strlen = dp->dtdo_strlen; 10513 } 10514 10515 if (dp->dtdo_inttab != NULL) { 10516 ASSERT(dp->dtdo_intlen != 0); 10517 sz = dp->dtdo_intlen * sizeof (uint64_t); 10518 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 10519 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 10520 new->dtdo_intlen = dp->dtdo_intlen; 10521 } 10522 10523 if (dp->dtdo_vartab != NULL) { 10524 ASSERT(dp->dtdo_varlen != 0); 10525 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 10526 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 10527 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 10528 new->dtdo_varlen = dp->dtdo_varlen; 10529 } 10530 10531 dtrace_difo_init(new, vstate); 10532 return (new); 10533} 10534 10535static void 10536dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10537{ 10538 int i; 10539 10540 ASSERT(dp->dtdo_refcnt == 0); 10541 10542 for (i = 0; i < dp->dtdo_varlen; i++) { 10543 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10544 dtrace_statvar_t *svar, **svarp = NULL; 10545 uint_t id; 10546 uint8_t scope = v->dtdv_scope; 10547 int *np = NULL; 10548 10549 switch (scope) { 10550 case DIFV_SCOPE_THREAD: 10551 continue; 10552 10553 case DIFV_SCOPE_LOCAL: 10554 np = &vstate->dtvs_nlocals; 10555 svarp = vstate->dtvs_locals; 10556 break; 10557 10558 case DIFV_SCOPE_GLOBAL: 10559 np = &vstate->dtvs_nglobals; 10560 svarp = vstate->dtvs_globals; 10561 break; 10562 10563 default: 10564 ASSERT(0); 10565 } 10566 10567 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10568 continue; 10569 10570 id -= DIF_VAR_OTHER_UBASE; 10571 ASSERT(id < *np); 10572 10573 svar = svarp[id]; 10574 ASSERT(svar != NULL); 10575 ASSERT(svar->dtsv_refcnt > 0); 10576 10577 if (--svar->dtsv_refcnt > 0) 10578 continue; 10579 10580 if (svar->dtsv_size != 0) { 10581 ASSERT(svar->dtsv_data != 0); 10582 kmem_free((void *)(uintptr_t)svar->dtsv_data, 10583 svar->dtsv_size); 10584 } 10585 10586 kmem_free(svar, sizeof (dtrace_statvar_t)); 10587 svarp[id] = NULL; 10588 } 10589 10590 if (dp->dtdo_buf != NULL) 10591 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10592 if (dp->dtdo_inttab != NULL) 10593 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10594 if (dp->dtdo_strtab != NULL) 10595 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10596 if (dp->dtdo_vartab != NULL) 10597 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10598 10599 kmem_free(dp, sizeof (dtrace_difo_t)); 10600} 10601 10602static void 10603dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10604{ 10605 int i; 10606 10607 ASSERT(MUTEX_HELD(&dtrace_lock)); 10608 ASSERT(dp->dtdo_refcnt != 0); 10609 10610 for (i = 0; i < dp->dtdo_varlen; i++) { 10611 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10612 10613 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10614 continue; 10615 10616 ASSERT(dtrace_vtime_references > 0); 10617 if (--dtrace_vtime_references == 0) 10618 dtrace_vtime_disable(); 10619 } 10620 10621 if (--dp->dtdo_refcnt == 0) 10622 dtrace_difo_destroy(dp, vstate); 10623} 10624 10625/* 10626 * DTrace Format Functions 10627 */ 10628static uint16_t 10629dtrace_format_add(dtrace_state_t *state, char *str) 10630{ 10631 char *fmt, **new; 10632 uint16_t ndx, len = strlen(str) + 1; 10633 10634 fmt = kmem_zalloc(len, KM_SLEEP); 10635 bcopy(str, fmt, len); 10636 10637 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 10638 if (state->dts_formats[ndx] == NULL) { 10639 state->dts_formats[ndx] = fmt; 10640 return (ndx + 1); 10641 } 10642 } 10643 10644 if (state->dts_nformats == USHRT_MAX) { 10645 /* 10646 * This is only likely if a denial-of-service attack is being 10647 * attempted. As such, it's okay to fail silently here. 10648 */ 10649 kmem_free(fmt, len); 10650 return (0); 10651 } 10652 10653 /* 10654 * For simplicity, we always resize the formats array to be exactly the 10655 * number of formats. 10656 */ 10657 ndx = state->dts_nformats++; 10658 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 10659 10660 if (state->dts_formats != NULL) { 10661 ASSERT(ndx != 0); 10662 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 10663 kmem_free(state->dts_formats, ndx * sizeof (char *)); 10664 } 10665 10666 state->dts_formats = new; 10667 state->dts_formats[ndx] = fmt; 10668 10669 return (ndx + 1); 10670} 10671 10672static void 10673dtrace_format_remove(dtrace_state_t *state, uint16_t format) 10674{ 10675 char *fmt; 10676 10677 ASSERT(state->dts_formats != NULL); 10678 ASSERT(format <= state->dts_nformats); 10679 ASSERT(state->dts_formats[format - 1] != NULL); 10680 10681 fmt = state->dts_formats[format - 1]; 10682 kmem_free(fmt, strlen(fmt) + 1); 10683 state->dts_formats[format - 1] = NULL; 10684} 10685 10686static void 10687dtrace_format_destroy(dtrace_state_t *state) 10688{ 10689 int i; 10690 10691 if (state->dts_nformats == 0) { 10692 ASSERT(state->dts_formats == NULL); 10693 return; 10694 } 10695 10696 ASSERT(state->dts_formats != NULL); 10697 10698 for (i = 0; i < state->dts_nformats; i++) { 10699 char *fmt = state->dts_formats[i]; 10700 10701 if (fmt == NULL) 10702 continue; 10703 10704 kmem_free(fmt, strlen(fmt) + 1); 10705 } 10706 10707 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 10708 state->dts_nformats = 0; 10709 state->dts_formats = NULL; 10710} 10711 10712/* 10713 * DTrace Predicate Functions 10714 */ 10715static dtrace_predicate_t * 10716dtrace_predicate_create(dtrace_difo_t *dp) 10717{ 10718 dtrace_predicate_t *pred; 10719 10720 ASSERT(MUTEX_HELD(&dtrace_lock)); 10721 ASSERT(dp->dtdo_refcnt != 0); 10722 10723 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 10724 pred->dtp_difo = dp; 10725 pred->dtp_refcnt = 1; 10726 10727 if (!dtrace_difo_cacheable(dp)) 10728 return (pred); 10729 10730 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 10731 /* 10732 * This is only theoretically possible -- we have had 2^32 10733 * cacheable predicates on this machine. We cannot allow any 10734 * more predicates to become cacheable: as unlikely as it is, 10735 * there may be a thread caching a (now stale) predicate cache 10736 * ID. (N.B.: the temptation is being successfully resisted to 10737 * have this cmn_err() "Holy shit -- we executed this code!") 10738 */ 10739 return (pred); 10740 } 10741 10742 pred->dtp_cacheid = dtrace_predcache_id++; 10743 10744 return (pred); 10745} 10746 10747static void 10748dtrace_predicate_hold(dtrace_predicate_t *pred) 10749{ 10750 ASSERT(MUTEX_HELD(&dtrace_lock)); 10751 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 10752 ASSERT(pred->dtp_refcnt > 0); 10753 10754 pred->dtp_refcnt++; 10755} 10756 10757static void 10758dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 10759{ 10760 dtrace_difo_t *dp = pred->dtp_difo; 10761 10762 ASSERT(MUTEX_HELD(&dtrace_lock)); 10763 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 10764 ASSERT(pred->dtp_refcnt > 0); 10765 10766 if (--pred->dtp_refcnt == 0) { 10767 dtrace_difo_release(pred->dtp_difo, vstate); 10768 kmem_free(pred, sizeof (dtrace_predicate_t)); 10769 } 10770} 10771 10772/* 10773 * DTrace Action Description Functions 10774 */ 10775static dtrace_actdesc_t * 10776dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 10777 uint64_t uarg, uint64_t arg) 10778{ 10779 dtrace_actdesc_t *act; 10780 10781#ifdef illumos 10782 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 10783 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 10784#endif 10785 10786 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 10787 act->dtad_kind = kind; 10788 act->dtad_ntuple = ntuple; 10789 act->dtad_uarg = uarg; 10790 act->dtad_arg = arg; 10791 act->dtad_refcnt = 1; 10792 10793 return (act); 10794} 10795 10796static void 10797dtrace_actdesc_hold(dtrace_actdesc_t *act) 10798{ 10799 ASSERT(act->dtad_refcnt >= 1); 10800 act->dtad_refcnt++; 10801} 10802 10803static void 10804dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 10805{ 10806 dtrace_actkind_t kind = act->dtad_kind; 10807 dtrace_difo_t *dp; 10808 10809 ASSERT(act->dtad_refcnt >= 1); 10810 10811 if (--act->dtad_refcnt != 0) 10812 return; 10813 10814 if ((dp = act->dtad_difo) != NULL) 10815 dtrace_difo_release(dp, vstate); 10816 10817 if (DTRACEACT_ISPRINTFLIKE(kind)) { 10818 char *str = (char *)(uintptr_t)act->dtad_arg; 10819 10820#ifdef illumos 10821 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 10822 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 10823#endif 10824 10825 if (str != NULL) 10826 kmem_free(str, strlen(str) + 1); 10827 } 10828 10829 kmem_free(act, sizeof (dtrace_actdesc_t)); 10830} 10831 10832/* 10833 * DTrace ECB Functions 10834 */ 10835static dtrace_ecb_t * 10836dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 10837{ 10838 dtrace_ecb_t *ecb; 10839 dtrace_epid_t epid; 10840 10841 ASSERT(MUTEX_HELD(&dtrace_lock)); 10842 10843 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 10844 ecb->dte_predicate = NULL; 10845 ecb->dte_probe = probe; 10846 10847 /* 10848 * The default size is the size of the default action: recording 10849 * the header. 10850 */ 10851 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 10852 ecb->dte_alignment = sizeof (dtrace_epid_t); 10853 10854 epid = state->dts_epid++; 10855 10856 if (epid - 1 >= state->dts_necbs) { 10857 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 10858 int necbs = state->dts_necbs << 1; 10859 10860 ASSERT(epid == state->dts_necbs + 1); 10861 10862 if (necbs == 0) { 10863 ASSERT(oecbs == NULL); 10864 necbs = 1; 10865 } 10866 10867 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 10868 10869 if (oecbs != NULL) 10870 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 10871 10872 dtrace_membar_producer(); 10873 state->dts_ecbs = ecbs; 10874 10875 if (oecbs != NULL) { 10876 /* 10877 * If this state is active, we must dtrace_sync() 10878 * before we can free the old dts_ecbs array: we're 10879 * coming in hot, and there may be active ring 10880 * buffer processing (which indexes into the dts_ecbs 10881 * array) on another CPU. 10882 */ 10883 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 10884 dtrace_sync(); 10885 10886 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 10887 } 10888 10889 dtrace_membar_producer(); 10890 state->dts_necbs = necbs; 10891 } 10892 10893 ecb->dte_state = state; 10894 10895 ASSERT(state->dts_ecbs[epid - 1] == NULL); 10896 dtrace_membar_producer(); 10897 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 10898 10899 return (ecb); 10900} 10901 10902static void 10903dtrace_ecb_enable(dtrace_ecb_t *ecb) 10904{ 10905 dtrace_probe_t *probe = ecb->dte_probe; 10906 10907 ASSERT(MUTEX_HELD(&cpu_lock)); 10908 ASSERT(MUTEX_HELD(&dtrace_lock)); 10909 ASSERT(ecb->dte_next == NULL); 10910 10911 if (probe == NULL) { 10912 /* 10913 * This is the NULL probe -- there's nothing to do. 10914 */ 10915 return; 10916 } 10917 10918 if (probe->dtpr_ecb == NULL) { 10919 dtrace_provider_t *prov = probe->dtpr_provider; 10920 10921 /* 10922 * We're the first ECB on this probe. 10923 */ 10924 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 10925 10926 if (ecb->dte_predicate != NULL) 10927 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 10928 10929 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 10930 probe->dtpr_id, probe->dtpr_arg); 10931 } else { 10932 /* 10933 * This probe is already active. Swing the last pointer to 10934 * point to the new ECB, and issue a dtrace_sync() to assure 10935 * that all CPUs have seen the change. 10936 */ 10937 ASSERT(probe->dtpr_ecb_last != NULL); 10938 probe->dtpr_ecb_last->dte_next = ecb; 10939 probe->dtpr_ecb_last = ecb; 10940 probe->dtpr_predcache = 0; 10941 10942 dtrace_sync(); 10943 } 10944} 10945 10946static void 10947dtrace_ecb_resize(dtrace_ecb_t *ecb) 10948{ 10949 dtrace_action_t *act; 10950 uint32_t curneeded = UINT32_MAX; 10951 uint32_t aggbase = UINT32_MAX; 10952 10953 /* 10954 * If we record anything, we always record the dtrace_rechdr_t. (And 10955 * we always record it first.) 10956 */ 10957 ecb->dte_size = sizeof (dtrace_rechdr_t); 10958 ecb->dte_alignment = sizeof (dtrace_epid_t); 10959 10960 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 10961 dtrace_recdesc_t *rec = &act->dta_rec; 10962 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 10963 10964 ecb->dte_alignment = MAX(ecb->dte_alignment, 10965 rec->dtrd_alignment); 10966 10967 if (DTRACEACT_ISAGG(act->dta_kind)) { 10968 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 10969 10970 ASSERT(rec->dtrd_size != 0); 10971 ASSERT(agg->dtag_first != NULL); 10972 ASSERT(act->dta_prev->dta_intuple); 10973 ASSERT(aggbase != UINT32_MAX); 10974 ASSERT(curneeded != UINT32_MAX); 10975 10976 agg->dtag_base = aggbase; 10977 10978 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 10979 rec->dtrd_offset = curneeded; 10980 curneeded += rec->dtrd_size; 10981 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 10982 10983 aggbase = UINT32_MAX; 10984 curneeded = UINT32_MAX; 10985 } else if (act->dta_intuple) { 10986 if (curneeded == UINT32_MAX) { 10987 /* 10988 * This is the first record in a tuple. Align 10989 * curneeded to be at offset 4 in an 8-byte 10990 * aligned block. 10991 */ 10992 ASSERT(act->dta_prev == NULL || 10993 !act->dta_prev->dta_intuple); 10994 ASSERT3U(aggbase, ==, UINT32_MAX); 10995 curneeded = P2PHASEUP(ecb->dte_size, 10996 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 10997 10998 aggbase = curneeded - sizeof (dtrace_aggid_t); 10999 ASSERT(IS_P2ALIGNED(aggbase, 11000 sizeof (uint64_t))); 11001 } 11002 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11003 rec->dtrd_offset = curneeded; 11004 curneeded += rec->dtrd_size; 11005 } else { 11006 /* tuples must be followed by an aggregation */ 11007 ASSERT(act->dta_prev == NULL || 11008 !act->dta_prev->dta_intuple); 11009 11010 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 11011 rec->dtrd_alignment); 11012 rec->dtrd_offset = ecb->dte_size; 11013 ecb->dte_size += rec->dtrd_size; 11014 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 11015 } 11016 } 11017 11018 if ((act = ecb->dte_action) != NULL && 11019 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 11020 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 11021 /* 11022 * If the size is still sizeof (dtrace_rechdr_t), then all 11023 * actions store no data; set the size to 0. 11024 */ 11025 ecb->dte_size = 0; 11026 } 11027 11028 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 11029 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 11030 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 11031 ecb->dte_needed); 11032} 11033 11034static dtrace_action_t * 11035dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11036{ 11037 dtrace_aggregation_t *agg; 11038 size_t size = sizeof (uint64_t); 11039 int ntuple = desc->dtad_ntuple; 11040 dtrace_action_t *act; 11041 dtrace_recdesc_t *frec; 11042 dtrace_aggid_t aggid; 11043 dtrace_state_t *state = ecb->dte_state; 11044 11045 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 11046 agg->dtag_ecb = ecb; 11047 11048 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 11049 11050 switch (desc->dtad_kind) { 11051 case DTRACEAGG_MIN: 11052 agg->dtag_initial = INT64_MAX; 11053 agg->dtag_aggregate = dtrace_aggregate_min; 11054 break; 11055 11056 case DTRACEAGG_MAX: 11057 agg->dtag_initial = INT64_MIN; 11058 agg->dtag_aggregate = dtrace_aggregate_max; 11059 break; 11060 11061 case DTRACEAGG_COUNT: 11062 agg->dtag_aggregate = dtrace_aggregate_count; 11063 break; 11064 11065 case DTRACEAGG_QUANTIZE: 11066 agg->dtag_aggregate = dtrace_aggregate_quantize; 11067 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 11068 sizeof (uint64_t); 11069 break; 11070 11071 case DTRACEAGG_LQUANTIZE: { 11072 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 11073 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 11074 11075 agg->dtag_initial = desc->dtad_arg; 11076 agg->dtag_aggregate = dtrace_aggregate_lquantize; 11077 11078 if (step == 0 || levels == 0) 11079 goto err; 11080 11081 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 11082 break; 11083 } 11084 11085 case DTRACEAGG_LLQUANTIZE: { 11086 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 11087 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 11088 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 11089 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 11090 int64_t v; 11091 11092 agg->dtag_initial = desc->dtad_arg; 11093 agg->dtag_aggregate = dtrace_aggregate_llquantize; 11094 11095 if (factor < 2 || low >= high || nsteps < factor) 11096 goto err; 11097 11098 /* 11099 * Now check that the number of steps evenly divides a power 11100 * of the factor. (This assures both integer bucket size and 11101 * linearity within each magnitude.) 11102 */ 11103 for (v = factor; v < nsteps; v *= factor) 11104 continue; 11105 11106 if ((v % nsteps) || (nsteps % factor)) 11107 goto err; 11108 11109 size = (dtrace_aggregate_llquantize_bucket(factor, 11110 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 11111 break; 11112 } 11113 11114 case DTRACEAGG_AVG: 11115 agg->dtag_aggregate = dtrace_aggregate_avg; 11116 size = sizeof (uint64_t) * 2; 11117 break; 11118 11119 case DTRACEAGG_STDDEV: 11120 agg->dtag_aggregate = dtrace_aggregate_stddev; 11121 size = sizeof (uint64_t) * 4; 11122 break; 11123 11124 case DTRACEAGG_SUM: 11125 agg->dtag_aggregate = dtrace_aggregate_sum; 11126 break; 11127 11128 default: 11129 goto err; 11130 } 11131 11132 agg->dtag_action.dta_rec.dtrd_size = size; 11133 11134 if (ntuple == 0) 11135 goto err; 11136 11137 /* 11138 * We must make sure that we have enough actions for the n-tuple. 11139 */ 11140 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 11141 if (DTRACEACT_ISAGG(act->dta_kind)) 11142 break; 11143 11144 if (--ntuple == 0) { 11145 /* 11146 * This is the action with which our n-tuple begins. 11147 */ 11148 agg->dtag_first = act; 11149 goto success; 11150 } 11151 } 11152 11153 /* 11154 * This n-tuple is short by ntuple elements. Return failure. 11155 */ 11156 ASSERT(ntuple != 0); 11157err: 11158 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11159 return (NULL); 11160 11161success: 11162 /* 11163 * If the last action in the tuple has a size of zero, it's actually 11164 * an expression argument for the aggregating action. 11165 */ 11166 ASSERT(ecb->dte_action_last != NULL); 11167 act = ecb->dte_action_last; 11168 11169 if (act->dta_kind == DTRACEACT_DIFEXPR) { 11170 ASSERT(act->dta_difo != NULL); 11171 11172 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 11173 agg->dtag_hasarg = 1; 11174 } 11175 11176 /* 11177 * We need to allocate an id for this aggregation. 11178 */ 11179#ifdef illumos 11180 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 11181 VM_BESTFIT | VM_SLEEP); 11182#else 11183 aggid = alloc_unr(state->dts_aggid_arena); 11184#endif 11185 11186 if (aggid - 1 >= state->dts_naggregations) { 11187 dtrace_aggregation_t **oaggs = state->dts_aggregations; 11188 dtrace_aggregation_t **aggs; 11189 int naggs = state->dts_naggregations << 1; 11190 int onaggs = state->dts_naggregations; 11191 11192 ASSERT(aggid == state->dts_naggregations + 1); 11193 11194 if (naggs == 0) { 11195 ASSERT(oaggs == NULL); 11196 naggs = 1; 11197 } 11198 11199 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 11200 11201 if (oaggs != NULL) { 11202 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 11203 kmem_free(oaggs, onaggs * sizeof (*aggs)); 11204 } 11205 11206 state->dts_aggregations = aggs; 11207 state->dts_naggregations = naggs; 11208 } 11209 11210 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 11211 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 11212 11213 frec = &agg->dtag_first->dta_rec; 11214 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 11215 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 11216 11217 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 11218 ASSERT(!act->dta_intuple); 11219 act->dta_intuple = 1; 11220 } 11221 11222 return (&agg->dtag_action); 11223} 11224 11225static void 11226dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 11227{ 11228 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11229 dtrace_state_t *state = ecb->dte_state; 11230 dtrace_aggid_t aggid = agg->dtag_id; 11231 11232 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 11233#ifdef illumos 11234 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 11235#else 11236 free_unr(state->dts_aggid_arena, aggid); 11237#endif 11238 11239 ASSERT(state->dts_aggregations[aggid - 1] == agg); 11240 state->dts_aggregations[aggid - 1] = NULL; 11241 11242 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11243} 11244 11245static int 11246dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11247{ 11248 dtrace_action_t *action, *last; 11249 dtrace_difo_t *dp = desc->dtad_difo; 11250 uint32_t size = 0, align = sizeof (uint8_t), mask; 11251 uint16_t format = 0; 11252 dtrace_recdesc_t *rec; 11253 dtrace_state_t *state = ecb->dte_state; 11254 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 11255 uint64_t arg = desc->dtad_arg; 11256 11257 ASSERT(MUTEX_HELD(&dtrace_lock)); 11258 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 11259 11260 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 11261 /* 11262 * If this is an aggregating action, there must be neither 11263 * a speculate nor a commit on the action chain. 11264 */ 11265 dtrace_action_t *act; 11266 11267 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11268 if (act->dta_kind == DTRACEACT_COMMIT) 11269 return (EINVAL); 11270 11271 if (act->dta_kind == DTRACEACT_SPECULATE) 11272 return (EINVAL); 11273 } 11274 11275 action = dtrace_ecb_aggregation_create(ecb, desc); 11276 11277 if (action == NULL) 11278 return (EINVAL); 11279 } else { 11280 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 11281 (desc->dtad_kind == DTRACEACT_DIFEXPR && 11282 dp != NULL && dp->dtdo_destructive)) { 11283 state->dts_destructive = 1; 11284 } 11285 11286 switch (desc->dtad_kind) { 11287 case DTRACEACT_PRINTF: 11288 case DTRACEACT_PRINTA: 11289 case DTRACEACT_SYSTEM: 11290 case DTRACEACT_FREOPEN: 11291 case DTRACEACT_DIFEXPR: 11292 /* 11293 * We know that our arg is a string -- turn it into a 11294 * format. 11295 */ 11296 if (arg == 0) { 11297 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 11298 desc->dtad_kind == DTRACEACT_DIFEXPR); 11299 format = 0; 11300 } else { 11301 ASSERT(arg != 0); 11302#ifdef illumos 11303 ASSERT(arg > KERNELBASE); 11304#endif 11305 format = dtrace_format_add(state, 11306 (char *)(uintptr_t)arg); 11307 } 11308 11309 /*FALLTHROUGH*/ 11310 case DTRACEACT_LIBACT: 11311 case DTRACEACT_TRACEMEM: 11312 case DTRACEACT_TRACEMEM_DYNSIZE: 11313 if (dp == NULL) 11314 return (EINVAL); 11315 11316 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 11317 break; 11318 11319 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 11320 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11321 return (EINVAL); 11322 11323 size = opt[DTRACEOPT_STRSIZE]; 11324 } 11325 11326 break; 11327 11328 case DTRACEACT_STACK: 11329 if ((nframes = arg) == 0) { 11330 nframes = opt[DTRACEOPT_STACKFRAMES]; 11331 ASSERT(nframes > 0); 11332 arg = nframes; 11333 } 11334 11335 size = nframes * sizeof (pc_t); 11336 break; 11337 11338 case DTRACEACT_JSTACK: 11339 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 11340 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 11341 11342 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 11343 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 11344 11345 arg = DTRACE_USTACK_ARG(nframes, strsize); 11346 11347 /*FALLTHROUGH*/ 11348 case DTRACEACT_USTACK: 11349 if (desc->dtad_kind != DTRACEACT_JSTACK && 11350 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 11351 strsize = DTRACE_USTACK_STRSIZE(arg); 11352 nframes = opt[DTRACEOPT_USTACKFRAMES]; 11353 ASSERT(nframes > 0); 11354 arg = DTRACE_USTACK_ARG(nframes, strsize); 11355 } 11356 11357 /* 11358 * Save a slot for the pid. 11359 */ 11360 size = (nframes + 1) * sizeof (uint64_t); 11361 size += DTRACE_USTACK_STRSIZE(arg); 11362 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 11363 11364 break; 11365 11366 case DTRACEACT_SYM: 11367 case DTRACEACT_MOD: 11368 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 11369 sizeof (uint64_t)) || 11370 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11371 return (EINVAL); 11372 break; 11373 11374 case DTRACEACT_USYM: 11375 case DTRACEACT_UMOD: 11376 case DTRACEACT_UADDR: 11377 if (dp == NULL || 11378 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 11379 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11380 return (EINVAL); 11381 11382 /* 11383 * We have a slot for the pid, plus a slot for the 11384 * argument. To keep things simple (aligned with 11385 * bitness-neutral sizing), we store each as a 64-bit 11386 * quantity. 11387 */ 11388 size = 2 * sizeof (uint64_t); 11389 break; 11390 11391 case DTRACEACT_STOP: 11392 case DTRACEACT_BREAKPOINT: 11393 case DTRACEACT_PANIC: 11394 break; 11395 11396 case DTRACEACT_CHILL: 11397 case DTRACEACT_DISCARD: 11398 case DTRACEACT_RAISE: 11399 if (dp == NULL) 11400 return (EINVAL); 11401 break; 11402 11403 case DTRACEACT_EXIT: 11404 if (dp == NULL || 11405 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 11406 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11407 return (EINVAL); 11408 break; 11409 11410 case DTRACEACT_SPECULATE: 11411 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 11412 return (EINVAL); 11413 11414 if (dp == NULL) 11415 return (EINVAL); 11416 11417 state->dts_speculates = 1; 11418 break; 11419 11420 case DTRACEACT_PRINTM: 11421 size = dp->dtdo_rtype.dtdt_size; 11422 break; 11423 11424 case DTRACEACT_PRINTT: 11425 size = dp->dtdo_rtype.dtdt_size; 11426 break; 11427 11428 case DTRACEACT_COMMIT: { 11429 dtrace_action_t *act = ecb->dte_action; 11430 11431 for (; act != NULL; act = act->dta_next) { 11432 if (act->dta_kind == DTRACEACT_COMMIT) 11433 return (EINVAL); 11434 } 11435 11436 if (dp == NULL) 11437 return (EINVAL); 11438 break; 11439 } 11440 11441 default: 11442 return (EINVAL); 11443 } 11444 11445 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 11446 /* 11447 * If this is a data-storing action or a speculate, 11448 * we must be sure that there isn't a commit on the 11449 * action chain. 11450 */ 11451 dtrace_action_t *act = ecb->dte_action; 11452 11453 for (; act != NULL; act = act->dta_next) { 11454 if (act->dta_kind == DTRACEACT_COMMIT) 11455 return (EINVAL); 11456 } 11457 } 11458 11459 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 11460 action->dta_rec.dtrd_size = size; 11461 } 11462 11463 action->dta_refcnt = 1; 11464 rec = &action->dta_rec; 11465 size = rec->dtrd_size; 11466 11467 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 11468 if (!(size & mask)) { 11469 align = mask + 1; 11470 break; 11471 } 11472 } 11473 11474 action->dta_kind = desc->dtad_kind; 11475 11476 if ((action->dta_difo = dp) != NULL) 11477 dtrace_difo_hold(dp); 11478 11479 rec->dtrd_action = action->dta_kind; 11480 rec->dtrd_arg = arg; 11481 rec->dtrd_uarg = desc->dtad_uarg; 11482 rec->dtrd_alignment = (uint16_t)align; 11483 rec->dtrd_format = format; 11484 11485 if ((last = ecb->dte_action_last) != NULL) { 11486 ASSERT(ecb->dte_action != NULL); 11487 action->dta_prev = last; 11488 last->dta_next = action; 11489 } else { 11490 ASSERT(ecb->dte_action == NULL); 11491 ecb->dte_action = action; 11492 } 11493 11494 ecb->dte_action_last = action; 11495 11496 return (0); 11497} 11498 11499static void 11500dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 11501{ 11502 dtrace_action_t *act = ecb->dte_action, *next; 11503 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 11504 dtrace_difo_t *dp; 11505 uint16_t format; 11506 11507 if (act != NULL && act->dta_refcnt > 1) { 11508 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 11509 act->dta_refcnt--; 11510 } else { 11511 for (; act != NULL; act = next) { 11512 next = act->dta_next; 11513 ASSERT(next != NULL || act == ecb->dte_action_last); 11514 ASSERT(act->dta_refcnt == 1); 11515 11516 if ((format = act->dta_rec.dtrd_format) != 0) 11517 dtrace_format_remove(ecb->dte_state, format); 11518 11519 if ((dp = act->dta_difo) != NULL) 11520 dtrace_difo_release(dp, vstate); 11521 11522 if (DTRACEACT_ISAGG(act->dta_kind)) { 11523 dtrace_ecb_aggregation_destroy(ecb, act); 11524 } else { 11525 kmem_free(act, sizeof (dtrace_action_t)); 11526 } 11527 } 11528 } 11529 11530 ecb->dte_action = NULL; 11531 ecb->dte_action_last = NULL; 11532 ecb->dte_size = 0; 11533} 11534 11535static void 11536dtrace_ecb_disable(dtrace_ecb_t *ecb) 11537{ 11538 /* 11539 * We disable the ECB by removing it from its probe. 11540 */ 11541 dtrace_ecb_t *pecb, *prev = NULL; 11542 dtrace_probe_t *probe = ecb->dte_probe; 11543 11544 ASSERT(MUTEX_HELD(&dtrace_lock)); 11545 11546 if (probe == NULL) { 11547 /* 11548 * This is the NULL probe; there is nothing to disable. 11549 */ 11550 return; 11551 } 11552 11553 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 11554 if (pecb == ecb) 11555 break; 11556 prev = pecb; 11557 } 11558 11559 ASSERT(pecb != NULL); 11560 11561 if (prev == NULL) { 11562 probe->dtpr_ecb = ecb->dte_next; 11563 } else { 11564 prev->dte_next = ecb->dte_next; 11565 } 11566 11567 if (ecb == probe->dtpr_ecb_last) { 11568 ASSERT(ecb->dte_next == NULL); 11569 probe->dtpr_ecb_last = prev; 11570 } 11571 11572 /* 11573 * The ECB has been disconnected from the probe; now sync to assure 11574 * that all CPUs have seen the change before returning. 11575 */ 11576 dtrace_sync(); 11577 11578 if (probe->dtpr_ecb == NULL) { 11579 /* 11580 * That was the last ECB on the probe; clear the predicate 11581 * cache ID for the probe, disable it and sync one more time 11582 * to assure that we'll never hit it again. 11583 */ 11584 dtrace_provider_t *prov = probe->dtpr_provider; 11585 11586 ASSERT(ecb->dte_next == NULL); 11587 ASSERT(probe->dtpr_ecb_last == NULL); 11588 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 11589 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 11590 probe->dtpr_id, probe->dtpr_arg); 11591 dtrace_sync(); 11592 } else { 11593 /* 11594 * There is at least one ECB remaining on the probe. If there 11595 * is _exactly_ one, set the probe's predicate cache ID to be 11596 * the predicate cache ID of the remaining ECB. 11597 */ 11598 ASSERT(probe->dtpr_ecb_last != NULL); 11599 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 11600 11601 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 11602 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 11603 11604 ASSERT(probe->dtpr_ecb->dte_next == NULL); 11605 11606 if (p != NULL) 11607 probe->dtpr_predcache = p->dtp_cacheid; 11608 } 11609 11610 ecb->dte_next = NULL; 11611 } 11612} 11613 11614static void 11615dtrace_ecb_destroy(dtrace_ecb_t *ecb) 11616{ 11617 dtrace_state_t *state = ecb->dte_state; 11618 dtrace_vstate_t *vstate = &state->dts_vstate; 11619 dtrace_predicate_t *pred; 11620 dtrace_epid_t epid = ecb->dte_epid; 11621 11622 ASSERT(MUTEX_HELD(&dtrace_lock)); 11623 ASSERT(ecb->dte_next == NULL); 11624 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 11625 11626 if ((pred = ecb->dte_predicate) != NULL) 11627 dtrace_predicate_release(pred, vstate); 11628 11629 dtrace_ecb_action_remove(ecb); 11630 11631 ASSERT(state->dts_ecbs[epid - 1] == ecb); 11632 state->dts_ecbs[epid - 1] = NULL; 11633 11634 kmem_free(ecb, sizeof (dtrace_ecb_t)); 11635} 11636 11637static dtrace_ecb_t * 11638dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 11639 dtrace_enabling_t *enab) 11640{ 11641 dtrace_ecb_t *ecb; 11642 dtrace_predicate_t *pred; 11643 dtrace_actdesc_t *act; 11644 dtrace_provider_t *prov; 11645 dtrace_ecbdesc_t *desc = enab->dten_current; 11646 11647 ASSERT(MUTEX_HELD(&dtrace_lock)); 11648 ASSERT(state != NULL); 11649 11650 ecb = dtrace_ecb_add(state, probe); 11651 ecb->dte_uarg = desc->dted_uarg; 11652 11653 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 11654 dtrace_predicate_hold(pred); 11655 ecb->dte_predicate = pred; 11656 } 11657 11658 if (probe != NULL) { 11659 /* 11660 * If the provider shows more leg than the consumer is old 11661 * enough to see, we need to enable the appropriate implicit 11662 * predicate bits to prevent the ecb from activating at 11663 * revealing times. 11664 * 11665 * Providers specifying DTRACE_PRIV_USER at register time 11666 * are stating that they need the /proc-style privilege 11667 * model to be enforced, and this is what DTRACE_COND_OWNER 11668 * and DTRACE_COND_ZONEOWNER will then do at probe time. 11669 */ 11670 prov = probe->dtpr_provider; 11671 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 11672 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11673 ecb->dte_cond |= DTRACE_COND_OWNER; 11674 11675 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 11676 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11677 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 11678 11679 /* 11680 * If the provider shows us kernel innards and the user 11681 * is lacking sufficient privilege, enable the 11682 * DTRACE_COND_USERMODE implicit predicate. 11683 */ 11684 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 11685 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 11686 ecb->dte_cond |= DTRACE_COND_USERMODE; 11687 } 11688 11689 if (dtrace_ecb_create_cache != NULL) { 11690 /* 11691 * If we have a cached ecb, we'll use its action list instead 11692 * of creating our own (saving both time and space). 11693 */ 11694 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 11695 dtrace_action_t *act = cached->dte_action; 11696 11697 if (act != NULL) { 11698 ASSERT(act->dta_refcnt > 0); 11699 act->dta_refcnt++; 11700 ecb->dte_action = act; 11701 ecb->dte_action_last = cached->dte_action_last; 11702 ecb->dte_needed = cached->dte_needed; 11703 ecb->dte_size = cached->dte_size; 11704 ecb->dte_alignment = cached->dte_alignment; 11705 } 11706 11707 return (ecb); 11708 } 11709 11710 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 11711 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 11712 dtrace_ecb_destroy(ecb); 11713 return (NULL); 11714 } 11715 } 11716 11717 dtrace_ecb_resize(ecb); 11718 11719 return (dtrace_ecb_create_cache = ecb); 11720} 11721 11722static int 11723dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 11724{ 11725 dtrace_ecb_t *ecb; 11726 dtrace_enabling_t *enab = arg; 11727 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 11728 11729 ASSERT(state != NULL); 11730 11731 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 11732 /* 11733 * This probe was created in a generation for which this 11734 * enabling has previously created ECBs; we don't want to 11735 * enable it again, so just kick out. 11736 */ 11737 return (DTRACE_MATCH_NEXT); 11738 } 11739 11740 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 11741 return (DTRACE_MATCH_DONE); 11742 11743 dtrace_ecb_enable(ecb); 11744 return (DTRACE_MATCH_NEXT); 11745} 11746 11747static dtrace_ecb_t * 11748dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 11749{ 11750 dtrace_ecb_t *ecb; 11751 11752 ASSERT(MUTEX_HELD(&dtrace_lock)); 11753 11754 if (id == 0 || id > state->dts_necbs) 11755 return (NULL); 11756 11757 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 11758 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 11759 11760 return (state->dts_ecbs[id - 1]); 11761} 11762 11763static dtrace_aggregation_t * 11764dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 11765{ 11766 dtrace_aggregation_t *agg; 11767 11768 ASSERT(MUTEX_HELD(&dtrace_lock)); 11769 11770 if (id == 0 || id > state->dts_naggregations) 11771 return (NULL); 11772 11773 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 11774 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 11775 agg->dtag_id == id); 11776 11777 return (state->dts_aggregations[id - 1]); 11778} 11779 11780/* 11781 * DTrace Buffer Functions 11782 * 11783 * The following functions manipulate DTrace buffers. Most of these functions 11784 * are called in the context of establishing or processing consumer state; 11785 * exceptions are explicitly noted. 11786 */ 11787 11788/* 11789 * Note: called from cross call context. This function switches the two 11790 * buffers on a given CPU. The atomicity of this operation is assured by 11791 * disabling interrupts while the actual switch takes place; the disabling of 11792 * interrupts serializes the execution with any execution of dtrace_probe() on 11793 * the same CPU. 11794 */ 11795static void 11796dtrace_buffer_switch(dtrace_buffer_t *buf) 11797{ 11798 caddr_t tomax = buf->dtb_tomax; 11799 caddr_t xamot = buf->dtb_xamot; 11800 dtrace_icookie_t cookie; 11801 hrtime_t now; 11802 11803 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11804 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 11805 11806 cookie = dtrace_interrupt_disable(); 11807 now = dtrace_gethrtime(); 11808 buf->dtb_tomax = xamot; 11809 buf->dtb_xamot = tomax; 11810 buf->dtb_xamot_drops = buf->dtb_drops; 11811 buf->dtb_xamot_offset = buf->dtb_offset; 11812 buf->dtb_xamot_errors = buf->dtb_errors; 11813 buf->dtb_xamot_flags = buf->dtb_flags; 11814 buf->dtb_offset = 0; 11815 buf->dtb_drops = 0; 11816 buf->dtb_errors = 0; 11817 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 11818 buf->dtb_interval = now - buf->dtb_switched; 11819 buf->dtb_switched = now; 11820 dtrace_interrupt_enable(cookie); 11821} 11822 11823/* 11824 * Note: called from cross call context. This function activates a buffer 11825 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 11826 * is guaranteed by the disabling of interrupts. 11827 */ 11828static void 11829dtrace_buffer_activate(dtrace_state_t *state) 11830{ 11831 dtrace_buffer_t *buf; 11832 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 11833 11834 buf = &state->dts_buffer[curcpu]; 11835 11836 if (buf->dtb_tomax != NULL) { 11837 /* 11838 * We might like to assert that the buffer is marked inactive, 11839 * but this isn't necessarily true: the buffer for the CPU 11840 * that processes the BEGIN probe has its buffer activated 11841 * manually. In this case, we take the (harmless) action 11842 * re-clearing the bit INACTIVE bit. 11843 */ 11844 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 11845 } 11846 11847 dtrace_interrupt_enable(cookie); 11848} 11849 11850static int 11851dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 11852 processorid_t cpu, int *factor) 11853{ 11854#ifdef illumos 11855 cpu_t *cp; 11856#endif 11857 dtrace_buffer_t *buf; 11858 int allocated = 0, desired = 0; 11859 11860#ifdef illumos 11861 ASSERT(MUTEX_HELD(&cpu_lock)); 11862 ASSERT(MUTEX_HELD(&dtrace_lock)); 11863 11864 *factor = 1; 11865 11866 if (size > dtrace_nonroot_maxsize && 11867 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 11868 return (EFBIG); 11869 11870 cp = cpu_list; 11871 11872 do { 11873 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 11874 continue; 11875 11876 buf = &bufs[cp->cpu_id]; 11877 11878 /* 11879 * If there is already a buffer allocated for this CPU, it 11880 * is only possible that this is a DR event. In this case, 11881 */ 11882 if (buf->dtb_tomax != NULL) { 11883 ASSERT(buf->dtb_size == size); 11884 continue; 11885 } 11886 11887 ASSERT(buf->dtb_xamot == NULL); 11888 11889 if ((buf->dtb_tomax = kmem_zalloc(size, 11890 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11891 goto err; 11892 11893 buf->dtb_size = size; 11894 buf->dtb_flags = flags; 11895 buf->dtb_offset = 0; 11896 buf->dtb_drops = 0; 11897 11898 if (flags & DTRACEBUF_NOSWITCH) 11899 continue; 11900 11901 if ((buf->dtb_xamot = kmem_zalloc(size, 11902 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11903 goto err; 11904 } while ((cp = cp->cpu_next) != cpu_list); 11905 11906 return (0); 11907 11908err: 11909 cp = cpu_list; 11910 11911 do { 11912 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 11913 continue; 11914 11915 buf = &bufs[cp->cpu_id]; 11916 desired += 2; 11917 11918 if (buf->dtb_xamot != NULL) { 11919 ASSERT(buf->dtb_tomax != NULL); 11920 ASSERT(buf->dtb_size == size); 11921 kmem_free(buf->dtb_xamot, size); 11922 allocated++; 11923 } 11924 11925 if (buf->dtb_tomax != NULL) { 11926 ASSERT(buf->dtb_size == size); 11927 kmem_free(buf->dtb_tomax, size); 11928 allocated++; 11929 } 11930 11931 buf->dtb_tomax = NULL; 11932 buf->dtb_xamot = NULL; 11933 buf->dtb_size = 0; 11934 } while ((cp = cp->cpu_next) != cpu_list); 11935#else 11936 int i; 11937 11938 *factor = 1; 11939#if defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 11940 /* 11941 * FreeBSD isn't good at limiting the amount of memory we 11942 * ask to malloc, so let's place a limit here before trying 11943 * to do something that might well end in tears at bedtime. 11944 */ 11945 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 11946 return (ENOMEM); 11947#endif 11948 11949 ASSERT(MUTEX_HELD(&dtrace_lock)); 11950 CPU_FOREACH(i) { 11951 if (cpu != DTRACE_CPUALL && cpu != i) 11952 continue; 11953 11954 buf = &bufs[i]; 11955 11956 /* 11957 * If there is already a buffer allocated for this CPU, it 11958 * is only possible that this is a DR event. In this case, 11959 * the buffer size must match our specified size. 11960 */ 11961 if (buf->dtb_tomax != NULL) { 11962 ASSERT(buf->dtb_size == size); 11963 continue; 11964 } 11965 11966 ASSERT(buf->dtb_xamot == NULL); 11967 11968 if ((buf->dtb_tomax = kmem_zalloc(size, 11969 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11970 goto err; 11971 11972 buf->dtb_size = size; 11973 buf->dtb_flags = flags; 11974 buf->dtb_offset = 0; 11975 buf->dtb_drops = 0; 11976 11977 if (flags & DTRACEBUF_NOSWITCH) 11978 continue; 11979 11980 if ((buf->dtb_xamot = kmem_zalloc(size, 11981 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 11982 goto err; 11983 } 11984 11985 return (0); 11986 11987err: 11988 /* 11989 * Error allocating memory, so free the buffers that were 11990 * allocated before the failed allocation. 11991 */ 11992 CPU_FOREACH(i) { 11993 if (cpu != DTRACE_CPUALL && cpu != i) 11994 continue; 11995 11996 buf = &bufs[i]; 11997 desired += 2; 11998 11999 if (buf->dtb_xamot != NULL) { 12000 ASSERT(buf->dtb_tomax != NULL); 12001 ASSERT(buf->dtb_size == size); 12002 kmem_free(buf->dtb_xamot, size); 12003 allocated++; 12004 } 12005 12006 if (buf->dtb_tomax != NULL) { 12007 ASSERT(buf->dtb_size == size); 12008 kmem_free(buf->dtb_tomax, size); 12009 allocated++; 12010 } 12011 12012 buf->dtb_tomax = NULL; 12013 buf->dtb_xamot = NULL; 12014 buf->dtb_size = 0; 12015 12016 } 12017#endif 12018 *factor = desired / (allocated > 0 ? allocated : 1); 12019 12020 return (ENOMEM); 12021} 12022 12023/* 12024 * Note: called from probe context. This function just increments the drop 12025 * count on a buffer. It has been made a function to allow for the 12026 * possibility of understanding the source of mysterious drop counts. (A 12027 * problem for which one may be particularly disappointed that DTrace cannot 12028 * be used to understand DTrace.) 12029 */ 12030static void 12031dtrace_buffer_drop(dtrace_buffer_t *buf) 12032{ 12033 buf->dtb_drops++; 12034} 12035 12036/* 12037 * Note: called from probe context. This function is called to reserve space 12038 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 12039 * mstate. Returns the new offset in the buffer, or a negative value if an 12040 * error has occurred. 12041 */ 12042static intptr_t 12043dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 12044 dtrace_state_t *state, dtrace_mstate_t *mstate) 12045{ 12046 intptr_t offs = buf->dtb_offset, soffs; 12047 intptr_t woffs; 12048 caddr_t tomax; 12049 size_t total; 12050 12051 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 12052 return (-1); 12053 12054 if ((tomax = buf->dtb_tomax) == NULL) { 12055 dtrace_buffer_drop(buf); 12056 return (-1); 12057 } 12058 12059 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 12060 while (offs & (align - 1)) { 12061 /* 12062 * Assert that our alignment is off by a number which 12063 * is itself sizeof (uint32_t) aligned. 12064 */ 12065 ASSERT(!((align - (offs & (align - 1))) & 12066 (sizeof (uint32_t) - 1))); 12067 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12068 offs += sizeof (uint32_t); 12069 } 12070 12071 if ((soffs = offs + needed) > buf->dtb_size) { 12072 dtrace_buffer_drop(buf); 12073 return (-1); 12074 } 12075 12076 if (mstate == NULL) 12077 return (offs); 12078 12079 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 12080 mstate->dtms_scratch_size = buf->dtb_size - soffs; 12081 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12082 12083 return (offs); 12084 } 12085 12086 if (buf->dtb_flags & DTRACEBUF_FILL) { 12087 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 12088 (buf->dtb_flags & DTRACEBUF_FULL)) 12089 return (-1); 12090 goto out; 12091 } 12092 12093 total = needed + (offs & (align - 1)); 12094 12095 /* 12096 * For a ring buffer, life is quite a bit more complicated. Before 12097 * we can store any padding, we need to adjust our wrapping offset. 12098 * (If we've never before wrapped or we're not about to, no adjustment 12099 * is required.) 12100 */ 12101 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 12102 offs + total > buf->dtb_size) { 12103 woffs = buf->dtb_xamot_offset; 12104 12105 if (offs + total > buf->dtb_size) { 12106 /* 12107 * We can't fit in the end of the buffer. First, a 12108 * sanity check that we can fit in the buffer at all. 12109 */ 12110 if (total > buf->dtb_size) { 12111 dtrace_buffer_drop(buf); 12112 return (-1); 12113 } 12114 12115 /* 12116 * We're going to be storing at the top of the buffer, 12117 * so now we need to deal with the wrapped offset. We 12118 * only reset our wrapped offset to 0 if it is 12119 * currently greater than the current offset. If it 12120 * is less than the current offset, it is because a 12121 * previous allocation induced a wrap -- but the 12122 * allocation didn't subsequently take the space due 12123 * to an error or false predicate evaluation. In this 12124 * case, we'll just leave the wrapped offset alone: if 12125 * the wrapped offset hasn't been advanced far enough 12126 * for this allocation, it will be adjusted in the 12127 * lower loop. 12128 */ 12129 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 12130 if (woffs >= offs) 12131 woffs = 0; 12132 } else { 12133 woffs = 0; 12134 } 12135 12136 /* 12137 * Now we know that we're going to be storing to the 12138 * top of the buffer and that there is room for us 12139 * there. We need to clear the buffer from the current 12140 * offset to the end (there may be old gunk there). 12141 */ 12142 while (offs < buf->dtb_size) 12143 tomax[offs++] = 0; 12144 12145 /* 12146 * We need to set our offset to zero. And because we 12147 * are wrapping, we need to set the bit indicating as 12148 * much. We can also adjust our needed space back 12149 * down to the space required by the ECB -- we know 12150 * that the top of the buffer is aligned. 12151 */ 12152 offs = 0; 12153 total = needed; 12154 buf->dtb_flags |= DTRACEBUF_WRAPPED; 12155 } else { 12156 /* 12157 * There is room for us in the buffer, so we simply 12158 * need to check the wrapped offset. 12159 */ 12160 if (woffs < offs) { 12161 /* 12162 * The wrapped offset is less than the offset. 12163 * This can happen if we allocated buffer space 12164 * that induced a wrap, but then we didn't 12165 * subsequently take the space due to an error 12166 * or false predicate evaluation. This is 12167 * okay; we know that _this_ allocation isn't 12168 * going to induce a wrap. We still can't 12169 * reset the wrapped offset to be zero, 12170 * however: the space may have been trashed in 12171 * the previous failed probe attempt. But at 12172 * least the wrapped offset doesn't need to 12173 * be adjusted at all... 12174 */ 12175 goto out; 12176 } 12177 } 12178 12179 while (offs + total > woffs) { 12180 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 12181 size_t size; 12182 12183 if (epid == DTRACE_EPIDNONE) { 12184 size = sizeof (uint32_t); 12185 } else { 12186 ASSERT3U(epid, <=, state->dts_necbs); 12187 ASSERT(state->dts_ecbs[epid - 1] != NULL); 12188 12189 size = state->dts_ecbs[epid - 1]->dte_size; 12190 } 12191 12192 ASSERT(woffs + size <= buf->dtb_size); 12193 ASSERT(size != 0); 12194 12195 if (woffs + size == buf->dtb_size) { 12196 /* 12197 * We've reached the end of the buffer; we want 12198 * to set the wrapped offset to 0 and break 12199 * out. However, if the offs is 0, then we're 12200 * in a strange edge-condition: the amount of 12201 * space that we want to reserve plus the size 12202 * of the record that we're overwriting is 12203 * greater than the size of the buffer. This 12204 * is problematic because if we reserve the 12205 * space but subsequently don't consume it (due 12206 * to a failed predicate or error) the wrapped 12207 * offset will be 0 -- yet the EPID at offset 0 12208 * will not be committed. This situation is 12209 * relatively easy to deal with: if we're in 12210 * this case, the buffer is indistinguishable 12211 * from one that hasn't wrapped; we need only 12212 * finish the job by clearing the wrapped bit, 12213 * explicitly setting the offset to be 0, and 12214 * zero'ing out the old data in the buffer. 12215 */ 12216 if (offs == 0) { 12217 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 12218 buf->dtb_offset = 0; 12219 woffs = total; 12220 12221 while (woffs < buf->dtb_size) 12222 tomax[woffs++] = 0; 12223 } 12224 12225 woffs = 0; 12226 break; 12227 } 12228 12229 woffs += size; 12230 } 12231 12232 /* 12233 * We have a wrapped offset. It may be that the wrapped offset 12234 * has become zero -- that's okay. 12235 */ 12236 buf->dtb_xamot_offset = woffs; 12237 } 12238 12239out: 12240 /* 12241 * Now we can plow the buffer with any necessary padding. 12242 */ 12243 while (offs & (align - 1)) { 12244 /* 12245 * Assert that our alignment is off by a number which 12246 * is itself sizeof (uint32_t) aligned. 12247 */ 12248 ASSERT(!((align - (offs & (align - 1))) & 12249 (sizeof (uint32_t) - 1))); 12250 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12251 offs += sizeof (uint32_t); 12252 } 12253 12254 if (buf->dtb_flags & DTRACEBUF_FILL) { 12255 if (offs + needed > buf->dtb_size - state->dts_reserve) { 12256 buf->dtb_flags |= DTRACEBUF_FULL; 12257 return (-1); 12258 } 12259 } 12260 12261 if (mstate == NULL) 12262 return (offs); 12263 12264 /* 12265 * For ring buffers and fill buffers, the scratch space is always 12266 * the inactive buffer. 12267 */ 12268 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 12269 mstate->dtms_scratch_size = buf->dtb_size; 12270 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12271 12272 return (offs); 12273} 12274 12275static void 12276dtrace_buffer_polish(dtrace_buffer_t *buf) 12277{ 12278 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 12279 ASSERT(MUTEX_HELD(&dtrace_lock)); 12280 12281 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 12282 return; 12283 12284 /* 12285 * We need to polish the ring buffer. There are three cases: 12286 * 12287 * - The first (and presumably most common) is that there is no gap 12288 * between the buffer offset and the wrapped offset. In this case, 12289 * there is nothing in the buffer that isn't valid data; we can 12290 * mark the buffer as polished and return. 12291 * 12292 * - The second (less common than the first but still more common 12293 * than the third) is that there is a gap between the buffer offset 12294 * and the wrapped offset, and the wrapped offset is larger than the 12295 * buffer offset. This can happen because of an alignment issue, or 12296 * can happen because of a call to dtrace_buffer_reserve() that 12297 * didn't subsequently consume the buffer space. In this case, 12298 * we need to zero the data from the buffer offset to the wrapped 12299 * offset. 12300 * 12301 * - The third (and least common) is that there is a gap between the 12302 * buffer offset and the wrapped offset, but the wrapped offset is 12303 * _less_ than the buffer offset. This can only happen because a 12304 * call to dtrace_buffer_reserve() induced a wrap, but the space 12305 * was not subsequently consumed. In this case, we need to zero the 12306 * space from the offset to the end of the buffer _and_ from the 12307 * top of the buffer to the wrapped offset. 12308 */ 12309 if (buf->dtb_offset < buf->dtb_xamot_offset) { 12310 bzero(buf->dtb_tomax + buf->dtb_offset, 12311 buf->dtb_xamot_offset - buf->dtb_offset); 12312 } 12313 12314 if (buf->dtb_offset > buf->dtb_xamot_offset) { 12315 bzero(buf->dtb_tomax + buf->dtb_offset, 12316 buf->dtb_size - buf->dtb_offset); 12317 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 12318 } 12319} 12320 12321/* 12322 * This routine determines if data generated at the specified time has likely 12323 * been entirely consumed at user-level. This routine is called to determine 12324 * if an ECB on a defunct probe (but for an active enabling) can be safely 12325 * disabled and destroyed. 12326 */ 12327static int 12328dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 12329{ 12330 int i; 12331 12332 for (i = 0; i < NCPU; i++) { 12333 dtrace_buffer_t *buf = &bufs[i]; 12334 12335 if (buf->dtb_size == 0) 12336 continue; 12337 12338 if (buf->dtb_flags & DTRACEBUF_RING) 12339 return (0); 12340 12341 if (!buf->dtb_switched && buf->dtb_offset != 0) 12342 return (0); 12343 12344 if (buf->dtb_switched - buf->dtb_interval < when) 12345 return (0); 12346 } 12347 12348 return (1); 12349} 12350 12351static void 12352dtrace_buffer_free(dtrace_buffer_t *bufs) 12353{ 12354 int i; 12355 12356 for (i = 0; i < NCPU; i++) { 12357 dtrace_buffer_t *buf = &bufs[i]; 12358 12359 if (buf->dtb_tomax == NULL) { 12360 ASSERT(buf->dtb_xamot == NULL); 12361 ASSERT(buf->dtb_size == 0); 12362 continue; 12363 } 12364 12365 if (buf->dtb_xamot != NULL) { 12366 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 12367 kmem_free(buf->dtb_xamot, buf->dtb_size); 12368 } 12369 12370 kmem_free(buf->dtb_tomax, buf->dtb_size); 12371 buf->dtb_size = 0; 12372 buf->dtb_tomax = NULL; 12373 buf->dtb_xamot = NULL; 12374 } 12375} 12376 12377/* 12378 * DTrace Enabling Functions 12379 */ 12380static dtrace_enabling_t * 12381dtrace_enabling_create(dtrace_vstate_t *vstate) 12382{ 12383 dtrace_enabling_t *enab; 12384 12385 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 12386 enab->dten_vstate = vstate; 12387 12388 return (enab); 12389} 12390 12391static void 12392dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 12393{ 12394 dtrace_ecbdesc_t **ndesc; 12395 size_t osize, nsize; 12396 12397 /* 12398 * We can't add to enablings after we've enabled them, or after we've 12399 * retained them. 12400 */ 12401 ASSERT(enab->dten_probegen == 0); 12402 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12403 12404 if (enab->dten_ndesc < enab->dten_maxdesc) { 12405 enab->dten_desc[enab->dten_ndesc++] = ecb; 12406 return; 12407 } 12408 12409 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12410 12411 if (enab->dten_maxdesc == 0) { 12412 enab->dten_maxdesc = 1; 12413 } else { 12414 enab->dten_maxdesc <<= 1; 12415 } 12416 12417 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 12418 12419 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12420 ndesc = kmem_zalloc(nsize, KM_SLEEP); 12421 bcopy(enab->dten_desc, ndesc, osize); 12422 if (enab->dten_desc != NULL) 12423 kmem_free(enab->dten_desc, osize); 12424 12425 enab->dten_desc = ndesc; 12426 enab->dten_desc[enab->dten_ndesc++] = ecb; 12427} 12428 12429static void 12430dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 12431 dtrace_probedesc_t *pd) 12432{ 12433 dtrace_ecbdesc_t *new; 12434 dtrace_predicate_t *pred; 12435 dtrace_actdesc_t *act; 12436 12437 /* 12438 * We're going to create a new ECB description that matches the 12439 * specified ECB in every way, but has the specified probe description. 12440 */ 12441 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12442 12443 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 12444 dtrace_predicate_hold(pred); 12445 12446 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 12447 dtrace_actdesc_hold(act); 12448 12449 new->dted_action = ecb->dted_action; 12450 new->dted_pred = ecb->dted_pred; 12451 new->dted_probe = *pd; 12452 new->dted_uarg = ecb->dted_uarg; 12453 12454 dtrace_enabling_add(enab, new); 12455} 12456 12457static void 12458dtrace_enabling_dump(dtrace_enabling_t *enab) 12459{ 12460 int i; 12461 12462 for (i = 0; i < enab->dten_ndesc; i++) { 12463 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 12464 12465 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 12466 desc->dtpd_provider, desc->dtpd_mod, 12467 desc->dtpd_func, desc->dtpd_name); 12468 } 12469} 12470 12471static void 12472dtrace_enabling_destroy(dtrace_enabling_t *enab) 12473{ 12474 int i; 12475 dtrace_ecbdesc_t *ep; 12476 dtrace_vstate_t *vstate = enab->dten_vstate; 12477 12478 ASSERT(MUTEX_HELD(&dtrace_lock)); 12479 12480 for (i = 0; i < enab->dten_ndesc; i++) { 12481 dtrace_actdesc_t *act, *next; 12482 dtrace_predicate_t *pred; 12483 12484 ep = enab->dten_desc[i]; 12485 12486 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 12487 dtrace_predicate_release(pred, vstate); 12488 12489 for (act = ep->dted_action; act != NULL; act = next) { 12490 next = act->dtad_next; 12491 dtrace_actdesc_release(act, vstate); 12492 } 12493 12494 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12495 } 12496 12497 if (enab->dten_desc != NULL) 12498 kmem_free(enab->dten_desc, 12499 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 12500 12501 /* 12502 * If this was a retained enabling, decrement the dts_nretained count 12503 * and take it off of the dtrace_retained list. 12504 */ 12505 if (enab->dten_prev != NULL || enab->dten_next != NULL || 12506 dtrace_retained == enab) { 12507 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12508 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 12509 enab->dten_vstate->dtvs_state->dts_nretained--; 12510 dtrace_retained_gen++; 12511 } 12512 12513 if (enab->dten_prev == NULL) { 12514 if (dtrace_retained == enab) { 12515 dtrace_retained = enab->dten_next; 12516 12517 if (dtrace_retained != NULL) 12518 dtrace_retained->dten_prev = NULL; 12519 } 12520 } else { 12521 ASSERT(enab != dtrace_retained); 12522 ASSERT(dtrace_retained != NULL); 12523 enab->dten_prev->dten_next = enab->dten_next; 12524 } 12525 12526 if (enab->dten_next != NULL) { 12527 ASSERT(dtrace_retained != NULL); 12528 enab->dten_next->dten_prev = enab->dten_prev; 12529 } 12530 12531 kmem_free(enab, sizeof (dtrace_enabling_t)); 12532} 12533 12534static int 12535dtrace_enabling_retain(dtrace_enabling_t *enab) 12536{ 12537 dtrace_state_t *state; 12538 12539 ASSERT(MUTEX_HELD(&dtrace_lock)); 12540 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12541 ASSERT(enab->dten_vstate != NULL); 12542 12543 state = enab->dten_vstate->dtvs_state; 12544 ASSERT(state != NULL); 12545 12546 /* 12547 * We only allow each state to retain dtrace_retain_max enablings. 12548 */ 12549 if (state->dts_nretained >= dtrace_retain_max) 12550 return (ENOSPC); 12551 12552 state->dts_nretained++; 12553 dtrace_retained_gen++; 12554 12555 if (dtrace_retained == NULL) { 12556 dtrace_retained = enab; 12557 return (0); 12558 } 12559 12560 enab->dten_next = dtrace_retained; 12561 dtrace_retained->dten_prev = enab; 12562 dtrace_retained = enab; 12563 12564 return (0); 12565} 12566 12567static int 12568dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 12569 dtrace_probedesc_t *create) 12570{ 12571 dtrace_enabling_t *new, *enab; 12572 int found = 0, err = ENOENT; 12573 12574 ASSERT(MUTEX_HELD(&dtrace_lock)); 12575 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 12576 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 12577 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 12578 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 12579 12580 new = dtrace_enabling_create(&state->dts_vstate); 12581 12582 /* 12583 * Iterate over all retained enablings, looking for enablings that 12584 * match the specified state. 12585 */ 12586 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12587 int i; 12588 12589 /* 12590 * dtvs_state can only be NULL for helper enablings -- and 12591 * helper enablings can't be retained. 12592 */ 12593 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12594 12595 if (enab->dten_vstate->dtvs_state != state) 12596 continue; 12597 12598 /* 12599 * Now iterate over each probe description; we're looking for 12600 * an exact match to the specified probe description. 12601 */ 12602 for (i = 0; i < enab->dten_ndesc; i++) { 12603 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12604 dtrace_probedesc_t *pd = &ep->dted_probe; 12605 12606 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 12607 continue; 12608 12609 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 12610 continue; 12611 12612 if (strcmp(pd->dtpd_func, match->dtpd_func)) 12613 continue; 12614 12615 if (strcmp(pd->dtpd_name, match->dtpd_name)) 12616 continue; 12617 12618 /* 12619 * We have a winning probe! Add it to our growing 12620 * enabling. 12621 */ 12622 found = 1; 12623 dtrace_enabling_addlike(new, ep, create); 12624 } 12625 } 12626 12627 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 12628 dtrace_enabling_destroy(new); 12629 return (err); 12630 } 12631 12632 return (0); 12633} 12634 12635static void 12636dtrace_enabling_retract(dtrace_state_t *state) 12637{ 12638 dtrace_enabling_t *enab, *next; 12639 12640 ASSERT(MUTEX_HELD(&dtrace_lock)); 12641 12642 /* 12643 * Iterate over all retained enablings, destroy the enablings retained 12644 * for the specified state. 12645 */ 12646 for (enab = dtrace_retained; enab != NULL; enab = next) { 12647 next = enab->dten_next; 12648 12649 /* 12650 * dtvs_state can only be NULL for helper enablings -- and 12651 * helper enablings can't be retained. 12652 */ 12653 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12654 12655 if (enab->dten_vstate->dtvs_state == state) { 12656 ASSERT(state->dts_nretained > 0); 12657 dtrace_enabling_destroy(enab); 12658 } 12659 } 12660 12661 ASSERT(state->dts_nretained == 0); 12662} 12663 12664static int 12665dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 12666{ 12667 int i = 0; 12668 int matched = 0; 12669 12670 ASSERT(MUTEX_HELD(&cpu_lock)); 12671 ASSERT(MUTEX_HELD(&dtrace_lock)); 12672 12673 for (i = 0; i < enab->dten_ndesc; i++) { 12674 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12675 12676 enab->dten_current = ep; 12677 enab->dten_error = 0; 12678 12679 matched += dtrace_probe_enable(&ep->dted_probe, enab); 12680 12681 if (enab->dten_error != 0) { 12682 /* 12683 * If we get an error half-way through enabling the 12684 * probes, we kick out -- perhaps with some number of 12685 * them enabled. Leaving enabled probes enabled may 12686 * be slightly confusing for user-level, but we expect 12687 * that no one will attempt to actually drive on in 12688 * the face of such errors. If this is an anonymous 12689 * enabling (indicated with a NULL nmatched pointer), 12690 * we cmn_err() a message. We aren't expecting to 12691 * get such an error -- such as it can exist at all, 12692 * it would be a result of corrupted DOF in the driver 12693 * properties. 12694 */ 12695 if (nmatched == NULL) { 12696 cmn_err(CE_WARN, "dtrace_enabling_match() " 12697 "error on %p: %d", (void *)ep, 12698 enab->dten_error); 12699 } 12700 12701 return (enab->dten_error); 12702 } 12703 } 12704 12705 enab->dten_probegen = dtrace_probegen; 12706 if (nmatched != NULL) 12707 *nmatched = matched; 12708 12709 return (0); 12710} 12711 12712static void 12713dtrace_enabling_matchall(void) 12714{ 12715 dtrace_enabling_t *enab; 12716 12717 mutex_enter(&cpu_lock); 12718 mutex_enter(&dtrace_lock); 12719 12720 /* 12721 * Iterate over all retained enablings to see if any probes match 12722 * against them. We only perform this operation on enablings for which 12723 * we have sufficient permissions by virtue of being in the global zone 12724 * or in the same zone as the DTrace client. Because we can be called 12725 * after dtrace_detach() has been called, we cannot assert that there 12726 * are retained enablings. We can safely load from dtrace_retained, 12727 * however: the taskq_destroy() at the end of dtrace_detach() will 12728 * block pending our completion. 12729 */ 12730 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12731#ifdef illumos 12732 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 12733 12734 if (INGLOBALZONE(curproc) || 12735 cr != NULL && getzoneid() == crgetzoneid(cr)) 12736#endif 12737 (void) dtrace_enabling_match(enab, NULL); 12738 } 12739 12740 mutex_exit(&dtrace_lock); 12741 mutex_exit(&cpu_lock); 12742} 12743 12744/* 12745 * If an enabling is to be enabled without having matched probes (that is, if 12746 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 12747 * enabling must be _primed_ by creating an ECB for every ECB description. 12748 * This must be done to assure that we know the number of speculations, the 12749 * number of aggregations, the minimum buffer size needed, etc. before we 12750 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 12751 * enabling any probes, we create ECBs for every ECB decription, but with a 12752 * NULL probe -- which is exactly what this function does. 12753 */ 12754static void 12755dtrace_enabling_prime(dtrace_state_t *state) 12756{ 12757 dtrace_enabling_t *enab; 12758 int i; 12759 12760 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12761 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12762 12763 if (enab->dten_vstate->dtvs_state != state) 12764 continue; 12765 12766 /* 12767 * We don't want to prime an enabling more than once, lest 12768 * we allow a malicious user to induce resource exhaustion. 12769 * (The ECBs that result from priming an enabling aren't 12770 * leaked -- but they also aren't deallocated until the 12771 * consumer state is destroyed.) 12772 */ 12773 if (enab->dten_primed) 12774 continue; 12775 12776 for (i = 0; i < enab->dten_ndesc; i++) { 12777 enab->dten_current = enab->dten_desc[i]; 12778 (void) dtrace_probe_enable(NULL, enab); 12779 } 12780 12781 enab->dten_primed = 1; 12782 } 12783} 12784 12785/* 12786 * Called to indicate that probes should be provided due to retained 12787 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 12788 * must take an initial lap through the enabling calling the dtps_provide() 12789 * entry point explicitly to allow for autocreated probes. 12790 */ 12791static void 12792dtrace_enabling_provide(dtrace_provider_t *prv) 12793{ 12794 int i, all = 0; 12795 dtrace_probedesc_t desc; 12796 dtrace_genid_t gen; 12797 12798 ASSERT(MUTEX_HELD(&dtrace_lock)); 12799 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 12800 12801 if (prv == NULL) { 12802 all = 1; 12803 prv = dtrace_provider; 12804 } 12805 12806 do { 12807 dtrace_enabling_t *enab; 12808 void *parg = prv->dtpv_arg; 12809 12810retry: 12811 gen = dtrace_retained_gen; 12812 for (enab = dtrace_retained; enab != NULL; 12813 enab = enab->dten_next) { 12814 for (i = 0; i < enab->dten_ndesc; i++) { 12815 desc = enab->dten_desc[i]->dted_probe; 12816 mutex_exit(&dtrace_lock); 12817 prv->dtpv_pops.dtps_provide(parg, &desc); 12818 mutex_enter(&dtrace_lock); 12819 /* 12820 * Process the retained enablings again if 12821 * they have changed while we weren't holding 12822 * dtrace_lock. 12823 */ 12824 if (gen != dtrace_retained_gen) 12825 goto retry; 12826 } 12827 } 12828 } while (all && (prv = prv->dtpv_next) != NULL); 12829 12830 mutex_exit(&dtrace_lock); 12831 dtrace_probe_provide(NULL, all ? NULL : prv); 12832 mutex_enter(&dtrace_lock); 12833} 12834 12835/* 12836 * Called to reap ECBs that are attached to probes from defunct providers. 12837 */ 12838static void 12839dtrace_enabling_reap(void) 12840{ 12841 dtrace_provider_t *prov; 12842 dtrace_probe_t *probe; 12843 dtrace_ecb_t *ecb; 12844 hrtime_t when; 12845 int i; 12846 12847 mutex_enter(&cpu_lock); 12848 mutex_enter(&dtrace_lock); 12849 12850 for (i = 0; i < dtrace_nprobes; i++) { 12851 if ((probe = dtrace_probes[i]) == NULL) 12852 continue; 12853 12854 if (probe->dtpr_ecb == NULL) 12855 continue; 12856 12857 prov = probe->dtpr_provider; 12858 12859 if ((when = prov->dtpv_defunct) == 0) 12860 continue; 12861 12862 /* 12863 * We have ECBs on a defunct provider: we want to reap these 12864 * ECBs to allow the provider to unregister. The destruction 12865 * of these ECBs must be done carefully: if we destroy the ECB 12866 * and the consumer later wishes to consume an EPID that 12867 * corresponds to the destroyed ECB (and if the EPID metadata 12868 * has not been previously consumed), the consumer will abort 12869 * processing on the unknown EPID. To reduce (but not, sadly, 12870 * eliminate) the possibility of this, we will only destroy an 12871 * ECB for a defunct provider if, for the state that 12872 * corresponds to the ECB: 12873 * 12874 * (a) There is no speculative tracing (which can effectively 12875 * cache an EPID for an arbitrary amount of time). 12876 * 12877 * (b) The principal buffers have been switched twice since the 12878 * provider became defunct. 12879 * 12880 * (c) The aggregation buffers are of zero size or have been 12881 * switched twice since the provider became defunct. 12882 * 12883 * We use dts_speculates to determine (a) and call a function 12884 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 12885 * that as soon as we've been unable to destroy one of the ECBs 12886 * associated with the probe, we quit trying -- reaping is only 12887 * fruitful in as much as we can destroy all ECBs associated 12888 * with the defunct provider's probes. 12889 */ 12890 while ((ecb = probe->dtpr_ecb) != NULL) { 12891 dtrace_state_t *state = ecb->dte_state; 12892 dtrace_buffer_t *buf = state->dts_buffer; 12893 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 12894 12895 if (state->dts_speculates) 12896 break; 12897 12898 if (!dtrace_buffer_consumed(buf, when)) 12899 break; 12900 12901 if (!dtrace_buffer_consumed(aggbuf, when)) 12902 break; 12903 12904 dtrace_ecb_disable(ecb); 12905 ASSERT(probe->dtpr_ecb != ecb); 12906 dtrace_ecb_destroy(ecb); 12907 } 12908 } 12909 12910 mutex_exit(&dtrace_lock); 12911 mutex_exit(&cpu_lock); 12912} 12913 12914/* 12915 * DTrace DOF Functions 12916 */ 12917/*ARGSUSED*/ 12918static void 12919dtrace_dof_error(dof_hdr_t *dof, const char *str) 12920{ 12921 if (dtrace_err_verbose) 12922 cmn_err(CE_WARN, "failed to process DOF: %s", str); 12923 12924#ifdef DTRACE_ERRDEBUG 12925 dtrace_errdebug(str); 12926#endif 12927} 12928 12929/* 12930 * Create DOF out of a currently enabled state. Right now, we only create 12931 * DOF containing the run-time options -- but this could be expanded to create 12932 * complete DOF representing the enabled state. 12933 */ 12934static dof_hdr_t * 12935dtrace_dof_create(dtrace_state_t *state) 12936{ 12937 dof_hdr_t *dof; 12938 dof_sec_t *sec; 12939 dof_optdesc_t *opt; 12940 int i, len = sizeof (dof_hdr_t) + 12941 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 12942 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 12943 12944 ASSERT(MUTEX_HELD(&dtrace_lock)); 12945 12946 dof = kmem_zalloc(len, KM_SLEEP); 12947 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 12948 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 12949 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 12950 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 12951 12952 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 12953 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 12954 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 12955 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 12956 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 12957 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 12958 12959 dof->dofh_flags = 0; 12960 dof->dofh_hdrsize = sizeof (dof_hdr_t); 12961 dof->dofh_secsize = sizeof (dof_sec_t); 12962 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 12963 dof->dofh_secoff = sizeof (dof_hdr_t); 12964 dof->dofh_loadsz = len; 12965 dof->dofh_filesz = len; 12966 dof->dofh_pad = 0; 12967 12968 /* 12969 * Fill in the option section header... 12970 */ 12971 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 12972 sec->dofs_type = DOF_SECT_OPTDESC; 12973 sec->dofs_align = sizeof (uint64_t); 12974 sec->dofs_flags = DOF_SECF_LOAD; 12975 sec->dofs_entsize = sizeof (dof_optdesc_t); 12976 12977 opt = (dof_optdesc_t *)((uintptr_t)sec + 12978 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 12979 12980 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 12981 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 12982 12983 for (i = 0; i < DTRACEOPT_MAX; i++) { 12984 opt[i].dofo_option = i; 12985 opt[i].dofo_strtab = DOF_SECIDX_NONE; 12986 opt[i].dofo_value = state->dts_options[i]; 12987 } 12988 12989 return (dof); 12990} 12991 12992static dof_hdr_t * 12993dtrace_dof_copyin(uintptr_t uarg, int *errp) 12994{ 12995 dof_hdr_t hdr, *dof; 12996 12997 ASSERT(!MUTEX_HELD(&dtrace_lock)); 12998 12999 /* 13000 * First, we're going to copyin() the sizeof (dof_hdr_t). 13001 */ 13002 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 13003 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13004 *errp = EFAULT; 13005 return (NULL); 13006 } 13007 13008 /* 13009 * Now we'll allocate the entire DOF and copy it in -- provided 13010 * that the length isn't outrageous. 13011 */ 13012 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13013 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13014 *errp = E2BIG; 13015 return (NULL); 13016 } 13017 13018 if (hdr.dofh_loadsz < sizeof (hdr)) { 13019 dtrace_dof_error(&hdr, "invalid load size"); 13020 *errp = EINVAL; 13021 return (NULL); 13022 } 13023 13024 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 13025 13026 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 13027 dof->dofh_loadsz != hdr.dofh_loadsz) { 13028 kmem_free(dof, hdr.dofh_loadsz); 13029 *errp = EFAULT; 13030 return (NULL); 13031 } 13032 13033 return (dof); 13034} 13035 13036#ifndef illumos 13037static __inline uchar_t 13038dtrace_dof_char(char c) { 13039 switch (c) { 13040 case '0': 13041 case '1': 13042 case '2': 13043 case '3': 13044 case '4': 13045 case '5': 13046 case '6': 13047 case '7': 13048 case '8': 13049 case '9': 13050 return (c - '0'); 13051 case 'A': 13052 case 'B': 13053 case 'C': 13054 case 'D': 13055 case 'E': 13056 case 'F': 13057 return (c - 'A' + 10); 13058 case 'a': 13059 case 'b': 13060 case 'c': 13061 case 'd': 13062 case 'e': 13063 case 'f': 13064 return (c - 'a' + 10); 13065 } 13066 /* Should not reach here. */ 13067 return (0); 13068} 13069#endif 13070 13071static dof_hdr_t * 13072dtrace_dof_property(const char *name) 13073{ 13074 uchar_t *buf; 13075 uint64_t loadsz; 13076 unsigned int len, i; 13077 dof_hdr_t *dof; 13078 13079#ifdef illumos 13080 /* 13081 * Unfortunately, array of values in .conf files are always (and 13082 * only) interpreted to be integer arrays. We must read our DOF 13083 * as an integer array, and then squeeze it into a byte array. 13084 */ 13085 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 13086 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 13087 return (NULL); 13088 13089 for (i = 0; i < len; i++) 13090 buf[i] = (uchar_t)(((int *)buf)[i]); 13091 13092 if (len < sizeof (dof_hdr_t)) { 13093 ddi_prop_free(buf); 13094 dtrace_dof_error(NULL, "truncated header"); 13095 return (NULL); 13096 } 13097 13098 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 13099 ddi_prop_free(buf); 13100 dtrace_dof_error(NULL, "truncated DOF"); 13101 return (NULL); 13102 } 13103 13104 if (loadsz >= dtrace_dof_maxsize) { 13105 ddi_prop_free(buf); 13106 dtrace_dof_error(NULL, "oversized DOF"); 13107 return (NULL); 13108 } 13109 13110 dof = kmem_alloc(loadsz, KM_SLEEP); 13111 bcopy(buf, dof, loadsz); 13112 ddi_prop_free(buf); 13113#else 13114 char *p; 13115 char *p_env; 13116 13117 if ((p_env = getenv(name)) == NULL) 13118 return (NULL); 13119 13120 len = strlen(p_env) / 2; 13121 13122 buf = kmem_alloc(len, KM_SLEEP); 13123 13124 dof = (dof_hdr_t *) buf; 13125 13126 p = p_env; 13127 13128 for (i = 0; i < len; i++) { 13129 buf[i] = (dtrace_dof_char(p[0]) << 4) | 13130 dtrace_dof_char(p[1]); 13131 p += 2; 13132 } 13133 13134 freeenv(p_env); 13135 13136 if (len < sizeof (dof_hdr_t)) { 13137 kmem_free(buf, 0); 13138 dtrace_dof_error(NULL, "truncated header"); 13139 return (NULL); 13140 } 13141 13142 if (len < (loadsz = dof->dofh_loadsz)) { 13143 kmem_free(buf, 0); 13144 dtrace_dof_error(NULL, "truncated DOF"); 13145 return (NULL); 13146 } 13147 13148 if (loadsz >= dtrace_dof_maxsize) { 13149 kmem_free(buf, 0); 13150 dtrace_dof_error(NULL, "oversized DOF"); 13151 return (NULL); 13152 } 13153#endif 13154 13155 return (dof); 13156} 13157 13158static void 13159dtrace_dof_destroy(dof_hdr_t *dof) 13160{ 13161 kmem_free(dof, dof->dofh_loadsz); 13162} 13163 13164/* 13165 * Return the dof_sec_t pointer corresponding to a given section index. If the 13166 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 13167 * a type other than DOF_SECT_NONE is specified, the header is checked against 13168 * this type and NULL is returned if the types do not match. 13169 */ 13170static dof_sec_t * 13171dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 13172{ 13173 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 13174 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 13175 13176 if (i >= dof->dofh_secnum) { 13177 dtrace_dof_error(dof, "referenced section index is invalid"); 13178 return (NULL); 13179 } 13180 13181 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 13182 dtrace_dof_error(dof, "referenced section is not loadable"); 13183 return (NULL); 13184 } 13185 13186 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 13187 dtrace_dof_error(dof, "referenced section is the wrong type"); 13188 return (NULL); 13189 } 13190 13191 return (sec); 13192} 13193 13194static dtrace_probedesc_t * 13195dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 13196{ 13197 dof_probedesc_t *probe; 13198 dof_sec_t *strtab; 13199 uintptr_t daddr = (uintptr_t)dof; 13200 uintptr_t str; 13201 size_t size; 13202 13203 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 13204 dtrace_dof_error(dof, "invalid probe section"); 13205 return (NULL); 13206 } 13207 13208 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13209 dtrace_dof_error(dof, "bad alignment in probe description"); 13210 return (NULL); 13211 } 13212 13213 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 13214 dtrace_dof_error(dof, "truncated probe description"); 13215 return (NULL); 13216 } 13217 13218 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 13219 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 13220 13221 if (strtab == NULL) 13222 return (NULL); 13223 13224 str = daddr + strtab->dofs_offset; 13225 size = strtab->dofs_size; 13226 13227 if (probe->dofp_provider >= strtab->dofs_size) { 13228 dtrace_dof_error(dof, "corrupt probe provider"); 13229 return (NULL); 13230 } 13231 13232 (void) strncpy(desc->dtpd_provider, 13233 (char *)(str + probe->dofp_provider), 13234 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 13235 13236 if (probe->dofp_mod >= strtab->dofs_size) { 13237 dtrace_dof_error(dof, "corrupt probe module"); 13238 return (NULL); 13239 } 13240 13241 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 13242 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 13243 13244 if (probe->dofp_func >= strtab->dofs_size) { 13245 dtrace_dof_error(dof, "corrupt probe function"); 13246 return (NULL); 13247 } 13248 13249 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 13250 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 13251 13252 if (probe->dofp_name >= strtab->dofs_size) { 13253 dtrace_dof_error(dof, "corrupt probe name"); 13254 return (NULL); 13255 } 13256 13257 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 13258 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 13259 13260 return (desc); 13261} 13262 13263static dtrace_difo_t * 13264dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13265 cred_t *cr) 13266{ 13267 dtrace_difo_t *dp; 13268 size_t ttl = 0; 13269 dof_difohdr_t *dofd; 13270 uintptr_t daddr = (uintptr_t)dof; 13271 size_t max = dtrace_difo_maxsize; 13272 int i, l, n; 13273 13274 static const struct { 13275 int section; 13276 int bufoffs; 13277 int lenoffs; 13278 int entsize; 13279 int align; 13280 const char *msg; 13281 } difo[] = { 13282 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 13283 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 13284 sizeof (dif_instr_t), "multiple DIF sections" }, 13285 13286 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 13287 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 13288 sizeof (uint64_t), "multiple integer tables" }, 13289 13290 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 13291 offsetof(dtrace_difo_t, dtdo_strlen), 0, 13292 sizeof (char), "multiple string tables" }, 13293 13294 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 13295 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 13296 sizeof (uint_t), "multiple variable tables" }, 13297 13298 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 13299 }; 13300 13301 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 13302 dtrace_dof_error(dof, "invalid DIFO header section"); 13303 return (NULL); 13304 } 13305 13306 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13307 dtrace_dof_error(dof, "bad alignment in DIFO header"); 13308 return (NULL); 13309 } 13310 13311 if (sec->dofs_size < sizeof (dof_difohdr_t) || 13312 sec->dofs_size % sizeof (dof_secidx_t)) { 13313 dtrace_dof_error(dof, "bad size in DIFO header"); 13314 return (NULL); 13315 } 13316 13317 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13318 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 13319 13320 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 13321 dp->dtdo_rtype = dofd->dofd_rtype; 13322 13323 for (l = 0; l < n; l++) { 13324 dof_sec_t *subsec; 13325 void **bufp; 13326 uint32_t *lenp; 13327 13328 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 13329 dofd->dofd_links[l])) == NULL) 13330 goto err; /* invalid section link */ 13331 13332 if (ttl + subsec->dofs_size > max) { 13333 dtrace_dof_error(dof, "exceeds maximum size"); 13334 goto err; 13335 } 13336 13337 ttl += subsec->dofs_size; 13338 13339 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 13340 if (subsec->dofs_type != difo[i].section) 13341 continue; 13342 13343 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 13344 dtrace_dof_error(dof, "section not loaded"); 13345 goto err; 13346 } 13347 13348 if (subsec->dofs_align != difo[i].align) { 13349 dtrace_dof_error(dof, "bad alignment"); 13350 goto err; 13351 } 13352 13353 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 13354 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 13355 13356 if (*bufp != NULL) { 13357 dtrace_dof_error(dof, difo[i].msg); 13358 goto err; 13359 } 13360 13361 if (difo[i].entsize != subsec->dofs_entsize) { 13362 dtrace_dof_error(dof, "entry size mismatch"); 13363 goto err; 13364 } 13365 13366 if (subsec->dofs_entsize != 0 && 13367 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 13368 dtrace_dof_error(dof, "corrupt entry size"); 13369 goto err; 13370 } 13371 13372 *lenp = subsec->dofs_size; 13373 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 13374 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 13375 *bufp, subsec->dofs_size); 13376 13377 if (subsec->dofs_entsize != 0) 13378 *lenp /= subsec->dofs_entsize; 13379 13380 break; 13381 } 13382 13383 /* 13384 * If we encounter a loadable DIFO sub-section that is not 13385 * known to us, assume this is a broken program and fail. 13386 */ 13387 if (difo[i].section == DOF_SECT_NONE && 13388 (subsec->dofs_flags & DOF_SECF_LOAD)) { 13389 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 13390 goto err; 13391 } 13392 } 13393 13394 if (dp->dtdo_buf == NULL) { 13395 /* 13396 * We can't have a DIF object without DIF text. 13397 */ 13398 dtrace_dof_error(dof, "missing DIF text"); 13399 goto err; 13400 } 13401 13402 /* 13403 * Before we validate the DIF object, run through the variable table 13404 * looking for the strings -- if any of their size are under, we'll set 13405 * their size to be the system-wide default string size. Note that 13406 * this should _not_ happen if the "strsize" option has been set -- 13407 * in this case, the compiler should have set the size to reflect the 13408 * setting of the option. 13409 */ 13410 for (i = 0; i < dp->dtdo_varlen; i++) { 13411 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 13412 dtrace_diftype_t *t = &v->dtdv_type; 13413 13414 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 13415 continue; 13416 13417 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 13418 t->dtdt_size = dtrace_strsize_default; 13419 } 13420 13421 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 13422 goto err; 13423 13424 dtrace_difo_init(dp, vstate); 13425 return (dp); 13426 13427err: 13428 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 13429 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 13430 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 13431 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 13432 13433 kmem_free(dp, sizeof (dtrace_difo_t)); 13434 return (NULL); 13435} 13436 13437static dtrace_predicate_t * 13438dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13439 cred_t *cr) 13440{ 13441 dtrace_difo_t *dp; 13442 13443 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 13444 return (NULL); 13445 13446 return (dtrace_predicate_create(dp)); 13447} 13448 13449static dtrace_actdesc_t * 13450dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13451 cred_t *cr) 13452{ 13453 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 13454 dof_actdesc_t *desc; 13455 dof_sec_t *difosec; 13456 size_t offs; 13457 uintptr_t daddr = (uintptr_t)dof; 13458 uint64_t arg; 13459 dtrace_actkind_t kind; 13460 13461 if (sec->dofs_type != DOF_SECT_ACTDESC) { 13462 dtrace_dof_error(dof, "invalid action section"); 13463 return (NULL); 13464 } 13465 13466 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 13467 dtrace_dof_error(dof, "truncated action description"); 13468 return (NULL); 13469 } 13470 13471 if (sec->dofs_align != sizeof (uint64_t)) { 13472 dtrace_dof_error(dof, "bad alignment in action description"); 13473 return (NULL); 13474 } 13475 13476 if (sec->dofs_size < sec->dofs_entsize) { 13477 dtrace_dof_error(dof, "section entry size exceeds total size"); 13478 return (NULL); 13479 } 13480 13481 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 13482 dtrace_dof_error(dof, "bad entry size in action description"); 13483 return (NULL); 13484 } 13485 13486 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 13487 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 13488 return (NULL); 13489 } 13490 13491 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 13492 desc = (dof_actdesc_t *)(daddr + 13493 (uintptr_t)sec->dofs_offset + offs); 13494 kind = (dtrace_actkind_t)desc->dofa_kind; 13495 13496 if ((DTRACEACT_ISPRINTFLIKE(kind) && 13497 (kind != DTRACEACT_PRINTA || 13498 desc->dofa_strtab != DOF_SECIDX_NONE)) || 13499 (kind == DTRACEACT_DIFEXPR && 13500 desc->dofa_strtab != DOF_SECIDX_NONE)) { 13501 dof_sec_t *strtab; 13502 char *str, *fmt; 13503 uint64_t i; 13504 13505 /* 13506 * The argument to these actions is an index into the 13507 * DOF string table. For printf()-like actions, this 13508 * is the format string. For print(), this is the 13509 * CTF type of the expression result. 13510 */ 13511 if ((strtab = dtrace_dof_sect(dof, 13512 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 13513 goto err; 13514 13515 str = (char *)((uintptr_t)dof + 13516 (uintptr_t)strtab->dofs_offset); 13517 13518 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 13519 if (str[i] == '\0') 13520 break; 13521 } 13522 13523 if (i >= strtab->dofs_size) { 13524 dtrace_dof_error(dof, "bogus format string"); 13525 goto err; 13526 } 13527 13528 if (i == desc->dofa_arg) { 13529 dtrace_dof_error(dof, "empty format string"); 13530 goto err; 13531 } 13532 13533 i -= desc->dofa_arg; 13534 fmt = kmem_alloc(i + 1, KM_SLEEP); 13535 bcopy(&str[desc->dofa_arg], fmt, i + 1); 13536 arg = (uint64_t)(uintptr_t)fmt; 13537 } else { 13538 if (kind == DTRACEACT_PRINTA) { 13539 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 13540 arg = 0; 13541 } else { 13542 arg = desc->dofa_arg; 13543 } 13544 } 13545 13546 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 13547 desc->dofa_uarg, arg); 13548 13549 if (last != NULL) { 13550 last->dtad_next = act; 13551 } else { 13552 first = act; 13553 } 13554 13555 last = act; 13556 13557 if (desc->dofa_difo == DOF_SECIDX_NONE) 13558 continue; 13559 13560 if ((difosec = dtrace_dof_sect(dof, 13561 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 13562 goto err; 13563 13564 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 13565 13566 if (act->dtad_difo == NULL) 13567 goto err; 13568 } 13569 13570 ASSERT(first != NULL); 13571 return (first); 13572 13573err: 13574 for (act = first; act != NULL; act = next) { 13575 next = act->dtad_next; 13576 dtrace_actdesc_release(act, vstate); 13577 } 13578 13579 return (NULL); 13580} 13581 13582static dtrace_ecbdesc_t * 13583dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13584 cred_t *cr) 13585{ 13586 dtrace_ecbdesc_t *ep; 13587 dof_ecbdesc_t *ecb; 13588 dtrace_probedesc_t *desc; 13589 dtrace_predicate_t *pred = NULL; 13590 13591 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 13592 dtrace_dof_error(dof, "truncated ECB description"); 13593 return (NULL); 13594 } 13595 13596 if (sec->dofs_align != sizeof (uint64_t)) { 13597 dtrace_dof_error(dof, "bad alignment in ECB description"); 13598 return (NULL); 13599 } 13600 13601 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 13602 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 13603 13604 if (sec == NULL) 13605 return (NULL); 13606 13607 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 13608 ep->dted_uarg = ecb->dofe_uarg; 13609 desc = &ep->dted_probe; 13610 13611 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 13612 goto err; 13613 13614 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 13615 if ((sec = dtrace_dof_sect(dof, 13616 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 13617 goto err; 13618 13619 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 13620 goto err; 13621 13622 ep->dted_pred.dtpdd_predicate = pred; 13623 } 13624 13625 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 13626 if ((sec = dtrace_dof_sect(dof, 13627 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 13628 goto err; 13629 13630 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 13631 13632 if (ep->dted_action == NULL) 13633 goto err; 13634 } 13635 13636 return (ep); 13637 13638err: 13639 if (pred != NULL) 13640 dtrace_predicate_release(pred, vstate); 13641 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 13642 return (NULL); 13643} 13644 13645/* 13646 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 13647 * specified DOF. At present, this amounts to simply adding 'ubase' to the 13648 * site of any user SETX relocations to account for load object base address. 13649 * In the future, if we need other relocations, this function can be extended. 13650 */ 13651static int 13652dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 13653{ 13654 uintptr_t daddr = (uintptr_t)dof; 13655 dof_relohdr_t *dofr = 13656 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13657 dof_sec_t *ss, *rs, *ts; 13658 dof_relodesc_t *r; 13659 uint_t i, n; 13660 13661 if (sec->dofs_size < sizeof (dof_relohdr_t) || 13662 sec->dofs_align != sizeof (dof_secidx_t)) { 13663 dtrace_dof_error(dof, "invalid relocation header"); 13664 return (-1); 13665 } 13666 13667 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 13668 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 13669 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 13670 13671 if (ss == NULL || rs == NULL || ts == NULL) 13672 return (-1); /* dtrace_dof_error() has been called already */ 13673 13674 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 13675 rs->dofs_align != sizeof (uint64_t)) { 13676 dtrace_dof_error(dof, "invalid relocation section"); 13677 return (-1); 13678 } 13679 13680 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 13681 n = rs->dofs_size / rs->dofs_entsize; 13682 13683 for (i = 0; i < n; i++) { 13684 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 13685 13686 switch (r->dofr_type) { 13687 case DOF_RELO_NONE: 13688 break; 13689 case DOF_RELO_SETX: 13690 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 13691 sizeof (uint64_t) > ts->dofs_size) { 13692 dtrace_dof_error(dof, "bad relocation offset"); 13693 return (-1); 13694 } 13695 13696 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 13697 dtrace_dof_error(dof, "misaligned setx relo"); 13698 return (-1); 13699 } 13700 13701 *(uint64_t *)taddr += ubase; 13702 break; 13703 default: 13704 dtrace_dof_error(dof, "invalid relocation type"); 13705 return (-1); 13706 } 13707 13708 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 13709 } 13710 13711 return (0); 13712} 13713 13714/* 13715 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 13716 * header: it should be at the front of a memory region that is at least 13717 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 13718 * size. It need not be validated in any other way. 13719 */ 13720static int 13721dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 13722 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 13723{ 13724 uint64_t len = dof->dofh_loadsz, seclen; 13725 uintptr_t daddr = (uintptr_t)dof; 13726 dtrace_ecbdesc_t *ep; 13727 dtrace_enabling_t *enab; 13728 uint_t i; 13729 13730 ASSERT(MUTEX_HELD(&dtrace_lock)); 13731 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 13732 13733 /* 13734 * Check the DOF header identification bytes. In addition to checking 13735 * valid settings, we also verify that unused bits/bytes are zeroed so 13736 * we can use them later without fear of regressing existing binaries. 13737 */ 13738 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 13739 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 13740 dtrace_dof_error(dof, "DOF magic string mismatch"); 13741 return (-1); 13742 } 13743 13744 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 13745 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 13746 dtrace_dof_error(dof, "DOF has invalid data model"); 13747 return (-1); 13748 } 13749 13750 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 13751 dtrace_dof_error(dof, "DOF encoding mismatch"); 13752 return (-1); 13753 } 13754 13755 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 13756 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 13757 dtrace_dof_error(dof, "DOF version mismatch"); 13758 return (-1); 13759 } 13760 13761 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 13762 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 13763 return (-1); 13764 } 13765 13766 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 13767 dtrace_dof_error(dof, "DOF uses too many integer registers"); 13768 return (-1); 13769 } 13770 13771 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 13772 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 13773 return (-1); 13774 } 13775 13776 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 13777 if (dof->dofh_ident[i] != 0) { 13778 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 13779 return (-1); 13780 } 13781 } 13782 13783 if (dof->dofh_flags & ~DOF_FL_VALID) { 13784 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 13785 return (-1); 13786 } 13787 13788 if (dof->dofh_secsize == 0) { 13789 dtrace_dof_error(dof, "zero section header size"); 13790 return (-1); 13791 } 13792 13793 /* 13794 * Check that the section headers don't exceed the amount of DOF 13795 * data. Note that we cast the section size and number of sections 13796 * to uint64_t's to prevent possible overflow in the multiplication. 13797 */ 13798 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 13799 13800 if (dof->dofh_secoff > len || seclen > len || 13801 dof->dofh_secoff + seclen > len) { 13802 dtrace_dof_error(dof, "truncated section headers"); 13803 return (-1); 13804 } 13805 13806 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 13807 dtrace_dof_error(dof, "misaligned section headers"); 13808 return (-1); 13809 } 13810 13811 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 13812 dtrace_dof_error(dof, "misaligned section size"); 13813 return (-1); 13814 } 13815 13816 /* 13817 * Take an initial pass through the section headers to be sure that 13818 * the headers don't have stray offsets. If the 'noprobes' flag is 13819 * set, do not permit sections relating to providers, probes, or args. 13820 */ 13821 for (i = 0; i < dof->dofh_secnum; i++) { 13822 dof_sec_t *sec = (dof_sec_t *)(daddr + 13823 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13824 13825 if (noprobes) { 13826 switch (sec->dofs_type) { 13827 case DOF_SECT_PROVIDER: 13828 case DOF_SECT_PROBES: 13829 case DOF_SECT_PRARGS: 13830 case DOF_SECT_PROFFS: 13831 dtrace_dof_error(dof, "illegal sections " 13832 "for enabling"); 13833 return (-1); 13834 } 13835 } 13836 13837 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 13838 !(sec->dofs_flags & DOF_SECF_LOAD)) { 13839 dtrace_dof_error(dof, "loadable section with load " 13840 "flag unset"); 13841 return (-1); 13842 } 13843 13844 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 13845 continue; /* just ignore non-loadable sections */ 13846 13847 if (!ISP2(sec->dofs_align)) { 13848 dtrace_dof_error(dof, "bad section alignment"); 13849 return (-1); 13850 } 13851 13852 if (sec->dofs_offset & (sec->dofs_align - 1)) { 13853 dtrace_dof_error(dof, "misaligned section"); 13854 return (-1); 13855 } 13856 13857 if (sec->dofs_offset > len || sec->dofs_size > len || 13858 sec->dofs_offset + sec->dofs_size > len) { 13859 dtrace_dof_error(dof, "corrupt section header"); 13860 return (-1); 13861 } 13862 13863 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 13864 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 13865 dtrace_dof_error(dof, "non-terminating string table"); 13866 return (-1); 13867 } 13868 } 13869 13870 /* 13871 * Take a second pass through the sections and locate and perform any 13872 * relocations that are present. We do this after the first pass to 13873 * be sure that all sections have had their headers validated. 13874 */ 13875 for (i = 0; i < dof->dofh_secnum; i++) { 13876 dof_sec_t *sec = (dof_sec_t *)(daddr + 13877 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13878 13879 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 13880 continue; /* skip sections that are not loadable */ 13881 13882 switch (sec->dofs_type) { 13883 case DOF_SECT_URELHDR: 13884 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 13885 return (-1); 13886 break; 13887 } 13888 } 13889 13890 if ((enab = *enabp) == NULL) 13891 enab = *enabp = dtrace_enabling_create(vstate); 13892 13893 for (i = 0; i < dof->dofh_secnum; i++) { 13894 dof_sec_t *sec = (dof_sec_t *)(daddr + 13895 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13896 13897 if (sec->dofs_type != DOF_SECT_ECBDESC) 13898 continue; 13899 13900 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 13901 dtrace_enabling_destroy(enab); 13902 *enabp = NULL; 13903 return (-1); 13904 } 13905 13906 dtrace_enabling_add(enab, ep); 13907 } 13908 13909 return (0); 13910} 13911 13912/* 13913 * Process DOF for any options. This routine assumes that the DOF has been 13914 * at least processed by dtrace_dof_slurp(). 13915 */ 13916static int 13917dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 13918{ 13919 int i, rval; 13920 uint32_t entsize; 13921 size_t offs; 13922 dof_optdesc_t *desc; 13923 13924 for (i = 0; i < dof->dofh_secnum; i++) { 13925 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 13926 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 13927 13928 if (sec->dofs_type != DOF_SECT_OPTDESC) 13929 continue; 13930 13931 if (sec->dofs_align != sizeof (uint64_t)) { 13932 dtrace_dof_error(dof, "bad alignment in " 13933 "option description"); 13934 return (EINVAL); 13935 } 13936 13937 if ((entsize = sec->dofs_entsize) == 0) { 13938 dtrace_dof_error(dof, "zeroed option entry size"); 13939 return (EINVAL); 13940 } 13941 13942 if (entsize < sizeof (dof_optdesc_t)) { 13943 dtrace_dof_error(dof, "bad option entry size"); 13944 return (EINVAL); 13945 } 13946 13947 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 13948 desc = (dof_optdesc_t *)((uintptr_t)dof + 13949 (uintptr_t)sec->dofs_offset + offs); 13950 13951 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 13952 dtrace_dof_error(dof, "non-zero option string"); 13953 return (EINVAL); 13954 } 13955 13956 if (desc->dofo_value == DTRACEOPT_UNSET) { 13957 dtrace_dof_error(dof, "unset option"); 13958 return (EINVAL); 13959 } 13960 13961 if ((rval = dtrace_state_option(state, 13962 desc->dofo_option, desc->dofo_value)) != 0) { 13963 dtrace_dof_error(dof, "rejected option"); 13964 return (rval); 13965 } 13966 } 13967 } 13968 13969 return (0); 13970} 13971 13972/* 13973 * DTrace Consumer State Functions 13974 */ 13975static int 13976dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 13977{ 13978 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 13979 void *base; 13980 uintptr_t limit; 13981 dtrace_dynvar_t *dvar, *next, *start; 13982 int i; 13983 13984 ASSERT(MUTEX_HELD(&dtrace_lock)); 13985 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 13986 13987 bzero(dstate, sizeof (dtrace_dstate_t)); 13988 13989 if ((dstate->dtds_chunksize = chunksize) == 0) 13990 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 13991 13992 VERIFY(dstate->dtds_chunksize < LONG_MAX); 13993 13994 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 13995 size = min; 13996 13997 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 13998 return (ENOMEM); 13999 14000 dstate->dtds_size = size; 14001 dstate->dtds_base = base; 14002 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 14003 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 14004 14005 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 14006 14007 if (hashsize != 1 && (hashsize & 1)) 14008 hashsize--; 14009 14010 dstate->dtds_hashsize = hashsize; 14011 dstate->dtds_hash = dstate->dtds_base; 14012 14013 /* 14014 * Set all of our hash buckets to point to the single sink, and (if 14015 * it hasn't already been set), set the sink's hash value to be the 14016 * sink sentinel value. The sink is needed for dynamic variable 14017 * lookups to know that they have iterated over an entire, valid hash 14018 * chain. 14019 */ 14020 for (i = 0; i < hashsize; i++) 14021 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 14022 14023 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 14024 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 14025 14026 /* 14027 * Determine number of active CPUs. Divide free list evenly among 14028 * active CPUs. 14029 */ 14030 start = (dtrace_dynvar_t *) 14031 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 14032 limit = (uintptr_t)base + size; 14033 14034 VERIFY((uintptr_t)start < limit); 14035 VERIFY((uintptr_t)start >= (uintptr_t)base); 14036 14037 maxper = (limit - (uintptr_t)start) / NCPU; 14038 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 14039 14040#ifndef illumos 14041 CPU_FOREACH(i) { 14042#else 14043 for (i = 0; i < NCPU; i++) { 14044#endif 14045 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 14046 14047 /* 14048 * If we don't even have enough chunks to make it once through 14049 * NCPUs, we're just going to allocate everything to the first 14050 * CPU. And if we're on the last CPU, we're going to allocate 14051 * whatever is left over. In either case, we set the limit to 14052 * be the limit of the dynamic variable space. 14053 */ 14054 if (maxper == 0 || i == NCPU - 1) { 14055 limit = (uintptr_t)base + size; 14056 start = NULL; 14057 } else { 14058 limit = (uintptr_t)start + maxper; 14059 start = (dtrace_dynvar_t *)limit; 14060 } 14061 14062 VERIFY(limit <= (uintptr_t)base + size); 14063 14064 for (;;) { 14065 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 14066 dstate->dtds_chunksize); 14067 14068 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 14069 break; 14070 14071 VERIFY((uintptr_t)dvar >= (uintptr_t)base && 14072 (uintptr_t)dvar <= (uintptr_t)base + size); 14073 dvar->dtdv_next = next; 14074 dvar = next; 14075 } 14076 14077 if (maxper == 0) 14078 break; 14079 } 14080 14081 return (0); 14082} 14083 14084static void 14085dtrace_dstate_fini(dtrace_dstate_t *dstate) 14086{ 14087 ASSERT(MUTEX_HELD(&cpu_lock)); 14088 14089 if (dstate->dtds_base == NULL) 14090 return; 14091 14092 kmem_free(dstate->dtds_base, dstate->dtds_size); 14093 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 14094} 14095 14096static void 14097dtrace_vstate_fini(dtrace_vstate_t *vstate) 14098{ 14099 /* 14100 * Logical XOR, where are you? 14101 */ 14102 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 14103 14104 if (vstate->dtvs_nglobals > 0) { 14105 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 14106 sizeof (dtrace_statvar_t *)); 14107 } 14108 14109 if (vstate->dtvs_ntlocals > 0) { 14110 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 14111 sizeof (dtrace_difv_t)); 14112 } 14113 14114 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 14115 14116 if (vstate->dtvs_nlocals > 0) { 14117 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 14118 sizeof (dtrace_statvar_t *)); 14119 } 14120} 14121 14122#ifdef illumos 14123static void 14124dtrace_state_clean(dtrace_state_t *state) 14125{ 14126 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14127 return; 14128 14129 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14130 dtrace_speculation_clean(state); 14131} 14132 14133static void 14134dtrace_state_deadman(dtrace_state_t *state) 14135{ 14136 hrtime_t now; 14137 14138 dtrace_sync(); 14139 14140 now = dtrace_gethrtime(); 14141 14142 if (state != dtrace_anon.dta_state && 14143 now - state->dts_laststatus >= dtrace_deadman_user) 14144 return; 14145 14146 /* 14147 * We must be sure that dts_alive never appears to be less than the 14148 * value upon entry to dtrace_state_deadman(), and because we lack a 14149 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14150 * store INT64_MAX to it, followed by a memory barrier, followed by 14151 * the new value. This assures that dts_alive never appears to be 14152 * less than its true value, regardless of the order in which the 14153 * stores to the underlying storage are issued. 14154 */ 14155 state->dts_alive = INT64_MAX; 14156 dtrace_membar_producer(); 14157 state->dts_alive = now; 14158} 14159#else /* !illumos */ 14160static void 14161dtrace_state_clean(void *arg) 14162{ 14163 dtrace_state_t *state = arg; 14164 dtrace_optval_t *opt = state->dts_options; 14165 14166 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14167 return; 14168 14169 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14170 dtrace_speculation_clean(state); 14171 14172 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14173 dtrace_state_clean, state); 14174} 14175 14176static void 14177dtrace_state_deadman(void *arg) 14178{ 14179 dtrace_state_t *state = arg; 14180 hrtime_t now; 14181 14182 dtrace_sync(); 14183 14184 dtrace_debug_output(); 14185 14186 now = dtrace_gethrtime(); 14187 14188 if (state != dtrace_anon.dta_state && 14189 now - state->dts_laststatus >= dtrace_deadman_user) 14190 return; 14191 14192 /* 14193 * We must be sure that dts_alive never appears to be less than the 14194 * value upon entry to dtrace_state_deadman(), and because we lack a 14195 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14196 * store INT64_MAX to it, followed by a memory barrier, followed by 14197 * the new value. This assures that dts_alive never appears to be 14198 * less than its true value, regardless of the order in which the 14199 * stores to the underlying storage are issued. 14200 */ 14201 state->dts_alive = INT64_MAX; 14202 dtrace_membar_producer(); 14203 state->dts_alive = now; 14204 14205 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14206 dtrace_state_deadman, state); 14207} 14208#endif /* illumos */ 14209 14210static dtrace_state_t * 14211#ifdef illumos 14212dtrace_state_create(dev_t *devp, cred_t *cr) 14213#else 14214dtrace_state_create(struct cdev *dev) 14215#endif 14216{ 14217#ifdef illumos 14218 minor_t minor; 14219 major_t major; 14220#else 14221 cred_t *cr = NULL; 14222 int m = 0; 14223#endif 14224 char c[30]; 14225 dtrace_state_t *state; 14226 dtrace_optval_t *opt; 14227 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 14228 14229 ASSERT(MUTEX_HELD(&dtrace_lock)); 14230 ASSERT(MUTEX_HELD(&cpu_lock)); 14231 14232#ifdef illumos 14233 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 14234 VM_BESTFIT | VM_SLEEP); 14235 14236 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 14237 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14238 return (NULL); 14239 } 14240 14241 state = ddi_get_soft_state(dtrace_softstate, minor); 14242#else 14243 if (dev != NULL) { 14244 cr = dev->si_cred; 14245 m = dev2unit(dev); 14246 } 14247 14248 /* Allocate memory for the state. */ 14249 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 14250#endif 14251 14252 state->dts_epid = DTRACE_EPIDNONE + 1; 14253 14254 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 14255#ifdef illumos 14256 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 14257 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14258 14259 if (devp != NULL) { 14260 major = getemajor(*devp); 14261 } else { 14262 major = ddi_driver_major(dtrace_devi); 14263 } 14264 14265 state->dts_dev = makedevice(major, minor); 14266 14267 if (devp != NULL) 14268 *devp = state->dts_dev; 14269#else 14270 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 14271 state->dts_dev = dev; 14272#endif 14273 14274 /* 14275 * We allocate NCPU buffers. On the one hand, this can be quite 14276 * a bit of memory per instance (nearly 36K on a Starcat). On the 14277 * other hand, it saves an additional memory reference in the probe 14278 * path. 14279 */ 14280 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 14281 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 14282 14283#ifdef illumos 14284 state->dts_cleaner = CYCLIC_NONE; 14285 state->dts_deadman = CYCLIC_NONE; 14286#else 14287 callout_init(&state->dts_cleaner, CALLOUT_MPSAFE); 14288 callout_init(&state->dts_deadman, CALLOUT_MPSAFE); 14289#endif 14290 state->dts_vstate.dtvs_state = state; 14291 14292 for (i = 0; i < DTRACEOPT_MAX; i++) 14293 state->dts_options[i] = DTRACEOPT_UNSET; 14294 14295 /* 14296 * Set the default options. 14297 */ 14298 opt = state->dts_options; 14299 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 14300 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 14301 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 14302 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 14303 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 14304 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 14305 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 14306 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 14307 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 14308 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 14309 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 14310 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 14311 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 14312 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 14313 14314 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 14315 14316 /* 14317 * Depending on the user credentials, we set flag bits which alter probe 14318 * visibility or the amount of destructiveness allowed. In the case of 14319 * actual anonymous tracing, or the possession of all privileges, all of 14320 * the normal checks are bypassed. 14321 */ 14322 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 14323 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 14324 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 14325 } else { 14326 /* 14327 * Set up the credentials for this instantiation. We take a 14328 * hold on the credential to prevent it from disappearing on 14329 * us; this in turn prevents the zone_t referenced by this 14330 * credential from disappearing. This means that we can 14331 * examine the credential and the zone from probe context. 14332 */ 14333 crhold(cr); 14334 state->dts_cred.dcr_cred = cr; 14335 14336 /* 14337 * CRA_PROC means "we have *some* privilege for dtrace" and 14338 * unlocks the use of variables like pid, zonename, etc. 14339 */ 14340 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 14341 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14342 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 14343 } 14344 14345 /* 14346 * dtrace_user allows use of syscall and profile providers. 14347 * If the user also has proc_owner and/or proc_zone, we 14348 * extend the scope to include additional visibility and 14349 * destructive power. 14350 */ 14351 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 14352 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 14353 state->dts_cred.dcr_visible |= 14354 DTRACE_CRV_ALLPROC; 14355 14356 state->dts_cred.dcr_action |= 14357 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14358 } 14359 14360 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 14361 state->dts_cred.dcr_visible |= 14362 DTRACE_CRV_ALLZONE; 14363 14364 state->dts_cred.dcr_action |= 14365 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14366 } 14367 14368 /* 14369 * If we have all privs in whatever zone this is, 14370 * we can do destructive things to processes which 14371 * have altered credentials. 14372 */ 14373#ifdef illumos 14374 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14375 cr->cr_zone->zone_privset)) { 14376 state->dts_cred.dcr_action |= 14377 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14378 } 14379#endif 14380 } 14381 14382 /* 14383 * Holding the dtrace_kernel privilege also implies that 14384 * the user has the dtrace_user privilege from a visibility 14385 * perspective. But without further privileges, some 14386 * destructive actions are not available. 14387 */ 14388 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 14389 /* 14390 * Make all probes in all zones visible. However, 14391 * this doesn't mean that all actions become available 14392 * to all zones. 14393 */ 14394 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 14395 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 14396 14397 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 14398 DTRACE_CRA_PROC; 14399 /* 14400 * Holding proc_owner means that destructive actions 14401 * for *this* zone are allowed. 14402 */ 14403 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14404 state->dts_cred.dcr_action |= 14405 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14406 14407 /* 14408 * Holding proc_zone means that destructive actions 14409 * for this user/group ID in all zones is allowed. 14410 */ 14411 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14412 state->dts_cred.dcr_action |= 14413 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14414 14415#ifdef illumos 14416 /* 14417 * If we have all privs in whatever zone this is, 14418 * we can do destructive things to processes which 14419 * have altered credentials. 14420 */ 14421 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14422 cr->cr_zone->zone_privset)) { 14423 state->dts_cred.dcr_action |= 14424 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14425 } 14426#endif 14427 } 14428 14429 /* 14430 * Holding the dtrace_proc privilege gives control over fasttrap 14431 * and pid providers. We need to grant wider destructive 14432 * privileges in the event that the user has proc_owner and/or 14433 * proc_zone. 14434 */ 14435 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14436 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14437 state->dts_cred.dcr_action |= 14438 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14439 14440 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14441 state->dts_cred.dcr_action |= 14442 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14443 } 14444 } 14445 14446 return (state); 14447} 14448 14449static int 14450dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 14451{ 14452 dtrace_optval_t *opt = state->dts_options, size; 14453 processorid_t cpu = 0;; 14454 int flags = 0, rval, factor, divisor = 1; 14455 14456 ASSERT(MUTEX_HELD(&dtrace_lock)); 14457 ASSERT(MUTEX_HELD(&cpu_lock)); 14458 ASSERT(which < DTRACEOPT_MAX); 14459 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 14460 (state == dtrace_anon.dta_state && 14461 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 14462 14463 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 14464 return (0); 14465 14466 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 14467 cpu = opt[DTRACEOPT_CPU]; 14468 14469 if (which == DTRACEOPT_SPECSIZE) 14470 flags |= DTRACEBUF_NOSWITCH; 14471 14472 if (which == DTRACEOPT_BUFSIZE) { 14473 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 14474 flags |= DTRACEBUF_RING; 14475 14476 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 14477 flags |= DTRACEBUF_FILL; 14478 14479 if (state != dtrace_anon.dta_state || 14480 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14481 flags |= DTRACEBUF_INACTIVE; 14482 } 14483 14484 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 14485 /* 14486 * The size must be 8-byte aligned. If the size is not 8-byte 14487 * aligned, drop it down by the difference. 14488 */ 14489 if (size & (sizeof (uint64_t) - 1)) 14490 size -= size & (sizeof (uint64_t) - 1); 14491 14492 if (size < state->dts_reserve) { 14493 /* 14494 * Buffers always must be large enough to accommodate 14495 * their prereserved space. We return E2BIG instead 14496 * of ENOMEM in this case to allow for user-level 14497 * software to differentiate the cases. 14498 */ 14499 return (E2BIG); 14500 } 14501 14502 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 14503 14504 if (rval != ENOMEM) { 14505 opt[which] = size; 14506 return (rval); 14507 } 14508 14509 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14510 return (rval); 14511 14512 for (divisor = 2; divisor < factor; divisor <<= 1) 14513 continue; 14514 } 14515 14516 return (ENOMEM); 14517} 14518 14519static int 14520dtrace_state_buffers(dtrace_state_t *state) 14521{ 14522 dtrace_speculation_t *spec = state->dts_speculations; 14523 int rval, i; 14524 14525 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 14526 DTRACEOPT_BUFSIZE)) != 0) 14527 return (rval); 14528 14529 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 14530 DTRACEOPT_AGGSIZE)) != 0) 14531 return (rval); 14532 14533 for (i = 0; i < state->dts_nspeculations; i++) { 14534 if ((rval = dtrace_state_buffer(state, 14535 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 14536 return (rval); 14537 } 14538 14539 return (0); 14540} 14541 14542static void 14543dtrace_state_prereserve(dtrace_state_t *state) 14544{ 14545 dtrace_ecb_t *ecb; 14546 dtrace_probe_t *probe; 14547 14548 state->dts_reserve = 0; 14549 14550 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 14551 return; 14552 14553 /* 14554 * If our buffer policy is a "fill" buffer policy, we need to set the 14555 * prereserved space to be the space required by the END probes. 14556 */ 14557 probe = dtrace_probes[dtrace_probeid_end - 1]; 14558 ASSERT(probe != NULL); 14559 14560 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 14561 if (ecb->dte_state != state) 14562 continue; 14563 14564 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 14565 } 14566} 14567 14568static int 14569dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 14570{ 14571 dtrace_optval_t *opt = state->dts_options, sz, nspec; 14572 dtrace_speculation_t *spec; 14573 dtrace_buffer_t *buf; 14574#ifdef illumos 14575 cyc_handler_t hdlr; 14576 cyc_time_t when; 14577#endif 14578 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 14579 dtrace_icookie_t cookie; 14580 14581 mutex_enter(&cpu_lock); 14582 mutex_enter(&dtrace_lock); 14583 14584 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 14585 rval = EBUSY; 14586 goto out; 14587 } 14588 14589 /* 14590 * Before we can perform any checks, we must prime all of the 14591 * retained enablings that correspond to this state. 14592 */ 14593 dtrace_enabling_prime(state); 14594 14595 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 14596 rval = EACCES; 14597 goto out; 14598 } 14599 14600 dtrace_state_prereserve(state); 14601 14602 /* 14603 * Now we want to do is try to allocate our speculations. 14604 * We do not automatically resize the number of speculations; if 14605 * this fails, we will fail the operation. 14606 */ 14607 nspec = opt[DTRACEOPT_NSPEC]; 14608 ASSERT(nspec != DTRACEOPT_UNSET); 14609 14610 if (nspec > INT_MAX) { 14611 rval = ENOMEM; 14612 goto out; 14613 } 14614 14615 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 14616 KM_NOSLEEP | KM_NORMALPRI); 14617 14618 if (spec == NULL) { 14619 rval = ENOMEM; 14620 goto out; 14621 } 14622 14623 state->dts_speculations = spec; 14624 state->dts_nspeculations = (int)nspec; 14625 14626 for (i = 0; i < nspec; i++) { 14627 if ((buf = kmem_zalloc(bufsize, 14628 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 14629 rval = ENOMEM; 14630 goto err; 14631 } 14632 14633 spec[i].dtsp_buffer = buf; 14634 } 14635 14636 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 14637 if (dtrace_anon.dta_state == NULL) { 14638 rval = ENOENT; 14639 goto out; 14640 } 14641 14642 if (state->dts_necbs != 0) { 14643 rval = EALREADY; 14644 goto out; 14645 } 14646 14647 state->dts_anon = dtrace_anon_grab(); 14648 ASSERT(state->dts_anon != NULL); 14649 state = state->dts_anon; 14650 14651 /* 14652 * We want "grabanon" to be set in the grabbed state, so we'll 14653 * copy that option value from the grabbing state into the 14654 * grabbed state. 14655 */ 14656 state->dts_options[DTRACEOPT_GRABANON] = 14657 opt[DTRACEOPT_GRABANON]; 14658 14659 *cpu = dtrace_anon.dta_beganon; 14660 14661 /* 14662 * If the anonymous state is active (as it almost certainly 14663 * is if the anonymous enabling ultimately matched anything), 14664 * we don't allow any further option processing -- but we 14665 * don't return failure. 14666 */ 14667 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 14668 goto out; 14669 } 14670 14671 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 14672 opt[DTRACEOPT_AGGSIZE] != 0) { 14673 if (state->dts_aggregations == NULL) { 14674 /* 14675 * We're not going to create an aggregation buffer 14676 * because we don't have any ECBs that contain 14677 * aggregations -- set this option to 0. 14678 */ 14679 opt[DTRACEOPT_AGGSIZE] = 0; 14680 } else { 14681 /* 14682 * If we have an aggregation buffer, we must also have 14683 * a buffer to use as scratch. 14684 */ 14685 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 14686 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 14687 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 14688 } 14689 } 14690 } 14691 14692 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 14693 opt[DTRACEOPT_SPECSIZE] != 0) { 14694 if (!state->dts_speculates) { 14695 /* 14696 * We're not going to create speculation buffers 14697 * because we don't have any ECBs that actually 14698 * speculate -- set the speculation size to 0. 14699 */ 14700 opt[DTRACEOPT_SPECSIZE] = 0; 14701 } 14702 } 14703 14704 /* 14705 * The bare minimum size for any buffer that we're actually going to 14706 * do anything to is sizeof (uint64_t). 14707 */ 14708 sz = sizeof (uint64_t); 14709 14710 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 14711 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 14712 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 14713 /* 14714 * A buffer size has been explicitly set to 0 (or to a size 14715 * that will be adjusted to 0) and we need the space -- we 14716 * need to return failure. We return ENOSPC to differentiate 14717 * it from failing to allocate a buffer due to failure to meet 14718 * the reserve (for which we return E2BIG). 14719 */ 14720 rval = ENOSPC; 14721 goto out; 14722 } 14723 14724 if ((rval = dtrace_state_buffers(state)) != 0) 14725 goto err; 14726 14727 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 14728 sz = dtrace_dstate_defsize; 14729 14730 do { 14731 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 14732 14733 if (rval == 0) 14734 break; 14735 14736 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14737 goto err; 14738 } while (sz >>= 1); 14739 14740 opt[DTRACEOPT_DYNVARSIZE] = sz; 14741 14742 if (rval != 0) 14743 goto err; 14744 14745 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 14746 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 14747 14748 if (opt[DTRACEOPT_CLEANRATE] == 0) 14749 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 14750 14751 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 14752 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 14753 14754 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 14755 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 14756 14757 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 14758#ifdef illumos 14759 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 14760 hdlr.cyh_arg = state; 14761 hdlr.cyh_level = CY_LOW_LEVEL; 14762 14763 when.cyt_when = 0; 14764 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 14765 14766 state->dts_cleaner = cyclic_add(&hdlr, &when); 14767 14768 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 14769 hdlr.cyh_arg = state; 14770 hdlr.cyh_level = CY_LOW_LEVEL; 14771 14772 when.cyt_when = 0; 14773 when.cyt_interval = dtrace_deadman_interval; 14774 14775 state->dts_deadman = cyclic_add(&hdlr, &when); 14776#else 14777 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14778 dtrace_state_clean, state); 14779 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14780 dtrace_state_deadman, state); 14781#endif 14782 14783 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 14784 14785#ifdef illumos 14786 if (state->dts_getf != 0 && 14787 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 14788 /* 14789 * We don't have kernel privs but we have at least one call 14790 * to getf(); we need to bump our zone's count, and (if 14791 * this is the first enabling to have an unprivileged call 14792 * to getf()) we need to hook into closef(). 14793 */ 14794 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 14795 14796 if (dtrace_getf++ == 0) { 14797 ASSERT(dtrace_closef == NULL); 14798 dtrace_closef = dtrace_getf_barrier; 14799 } 14800 } 14801#endif 14802 14803 /* 14804 * Now it's time to actually fire the BEGIN probe. We need to disable 14805 * interrupts here both to record the CPU on which we fired the BEGIN 14806 * probe (the data from this CPU will be processed first at user 14807 * level) and to manually activate the buffer for this CPU. 14808 */ 14809 cookie = dtrace_interrupt_disable(); 14810 *cpu = curcpu; 14811 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 14812 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 14813 14814 dtrace_probe(dtrace_probeid_begin, 14815 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 14816 dtrace_interrupt_enable(cookie); 14817 /* 14818 * We may have had an exit action from a BEGIN probe; only change our 14819 * state to ACTIVE if we're still in WARMUP. 14820 */ 14821 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 14822 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 14823 14824 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 14825 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 14826 14827 /* 14828 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 14829 * want each CPU to transition its principal buffer out of the 14830 * INACTIVE state. Doing this assures that no CPU will suddenly begin 14831 * processing an ECB halfway down a probe's ECB chain; all CPUs will 14832 * atomically transition from processing none of a state's ECBs to 14833 * processing all of them. 14834 */ 14835 dtrace_xcall(DTRACE_CPUALL, 14836 (dtrace_xcall_t)dtrace_buffer_activate, state); 14837 goto out; 14838 14839err: 14840 dtrace_buffer_free(state->dts_buffer); 14841 dtrace_buffer_free(state->dts_aggbuffer); 14842 14843 if ((nspec = state->dts_nspeculations) == 0) { 14844 ASSERT(state->dts_speculations == NULL); 14845 goto out; 14846 } 14847 14848 spec = state->dts_speculations; 14849 ASSERT(spec != NULL); 14850 14851 for (i = 0; i < state->dts_nspeculations; i++) { 14852 if ((buf = spec[i].dtsp_buffer) == NULL) 14853 break; 14854 14855 dtrace_buffer_free(buf); 14856 kmem_free(buf, bufsize); 14857 } 14858 14859 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 14860 state->dts_nspeculations = 0; 14861 state->dts_speculations = NULL; 14862 14863out: 14864 mutex_exit(&dtrace_lock); 14865 mutex_exit(&cpu_lock); 14866 14867 return (rval); 14868} 14869 14870static int 14871dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 14872{ 14873 dtrace_icookie_t cookie; 14874 14875 ASSERT(MUTEX_HELD(&dtrace_lock)); 14876 14877 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 14878 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 14879 return (EINVAL); 14880 14881 /* 14882 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 14883 * to be sure that every CPU has seen it. See below for the details 14884 * on why this is done. 14885 */ 14886 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 14887 dtrace_sync(); 14888 14889 /* 14890 * By this point, it is impossible for any CPU to be still processing 14891 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 14892 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 14893 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 14894 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 14895 * iff we're in the END probe. 14896 */ 14897 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 14898 dtrace_sync(); 14899 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 14900 14901 /* 14902 * Finally, we can release the reserve and call the END probe. We 14903 * disable interrupts across calling the END probe to allow us to 14904 * return the CPU on which we actually called the END probe. This 14905 * allows user-land to be sure that this CPU's principal buffer is 14906 * processed last. 14907 */ 14908 state->dts_reserve = 0; 14909 14910 cookie = dtrace_interrupt_disable(); 14911 *cpu = curcpu; 14912 dtrace_probe(dtrace_probeid_end, 14913 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 14914 dtrace_interrupt_enable(cookie); 14915 14916 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 14917 dtrace_sync(); 14918 14919#ifdef illumos 14920 if (state->dts_getf != 0 && 14921 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 14922 /* 14923 * We don't have kernel privs but we have at least one call 14924 * to getf(); we need to lower our zone's count, and (if 14925 * this is the last enabling to have an unprivileged call 14926 * to getf()) we need to clear the closef() hook. 14927 */ 14928 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 14929 ASSERT(dtrace_closef == dtrace_getf_barrier); 14930 ASSERT(dtrace_getf > 0); 14931 14932 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 14933 14934 if (--dtrace_getf == 0) 14935 dtrace_closef = NULL; 14936 } 14937#endif 14938 14939 return (0); 14940} 14941 14942static int 14943dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 14944 dtrace_optval_t val) 14945{ 14946 ASSERT(MUTEX_HELD(&dtrace_lock)); 14947 14948 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 14949 return (EBUSY); 14950 14951 if (option >= DTRACEOPT_MAX) 14952 return (EINVAL); 14953 14954 if (option != DTRACEOPT_CPU && val < 0) 14955 return (EINVAL); 14956 14957 switch (option) { 14958 case DTRACEOPT_DESTRUCTIVE: 14959 if (dtrace_destructive_disallow) 14960 return (EACCES); 14961 14962 state->dts_cred.dcr_destructive = 1; 14963 break; 14964 14965 case DTRACEOPT_BUFSIZE: 14966 case DTRACEOPT_DYNVARSIZE: 14967 case DTRACEOPT_AGGSIZE: 14968 case DTRACEOPT_SPECSIZE: 14969 case DTRACEOPT_STRSIZE: 14970 if (val < 0) 14971 return (EINVAL); 14972 14973 if (val >= LONG_MAX) { 14974 /* 14975 * If this is an otherwise negative value, set it to 14976 * the highest multiple of 128m less than LONG_MAX. 14977 * Technically, we're adjusting the size without 14978 * regard to the buffer resizing policy, but in fact, 14979 * this has no effect -- if we set the buffer size to 14980 * ~LONG_MAX and the buffer policy is ultimately set to 14981 * be "manual", the buffer allocation is guaranteed to 14982 * fail, if only because the allocation requires two 14983 * buffers. (We set the the size to the highest 14984 * multiple of 128m because it ensures that the size 14985 * will remain a multiple of a megabyte when 14986 * repeatedly halved -- all the way down to 15m.) 14987 */ 14988 val = LONG_MAX - (1 << 27) + 1; 14989 } 14990 } 14991 14992 state->dts_options[option] = val; 14993 14994 return (0); 14995} 14996 14997static void 14998dtrace_state_destroy(dtrace_state_t *state) 14999{ 15000 dtrace_ecb_t *ecb; 15001 dtrace_vstate_t *vstate = &state->dts_vstate; 15002#ifdef illumos 15003 minor_t minor = getminor(state->dts_dev); 15004#endif 15005 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 15006 dtrace_speculation_t *spec = state->dts_speculations; 15007 int nspec = state->dts_nspeculations; 15008 uint32_t match; 15009 15010 ASSERT(MUTEX_HELD(&dtrace_lock)); 15011 ASSERT(MUTEX_HELD(&cpu_lock)); 15012 15013 /* 15014 * First, retract any retained enablings for this state. 15015 */ 15016 dtrace_enabling_retract(state); 15017 ASSERT(state->dts_nretained == 0); 15018 15019 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 15020 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 15021 /* 15022 * We have managed to come into dtrace_state_destroy() on a 15023 * hot enabling -- almost certainly because of a disorderly 15024 * shutdown of a consumer. (That is, a consumer that is 15025 * exiting without having called dtrace_stop().) In this case, 15026 * we're going to set our activity to be KILLED, and then 15027 * issue a sync to be sure that everyone is out of probe 15028 * context before we start blowing away ECBs. 15029 */ 15030 state->dts_activity = DTRACE_ACTIVITY_KILLED; 15031 dtrace_sync(); 15032 } 15033 15034 /* 15035 * Release the credential hold we took in dtrace_state_create(). 15036 */ 15037 if (state->dts_cred.dcr_cred != NULL) 15038 crfree(state->dts_cred.dcr_cred); 15039 15040 /* 15041 * Now we can safely disable and destroy any enabled probes. Because 15042 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 15043 * (especially if they're all enabled), we take two passes through the 15044 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 15045 * in the second we disable whatever is left over. 15046 */ 15047 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 15048 for (i = 0; i < state->dts_necbs; i++) { 15049 if ((ecb = state->dts_ecbs[i]) == NULL) 15050 continue; 15051 15052 if (match && ecb->dte_probe != NULL) { 15053 dtrace_probe_t *probe = ecb->dte_probe; 15054 dtrace_provider_t *prov = probe->dtpr_provider; 15055 15056 if (!(prov->dtpv_priv.dtpp_flags & match)) 15057 continue; 15058 } 15059 15060 dtrace_ecb_disable(ecb); 15061 dtrace_ecb_destroy(ecb); 15062 } 15063 15064 if (!match) 15065 break; 15066 } 15067 15068 /* 15069 * Before we free the buffers, perform one more sync to assure that 15070 * every CPU is out of probe context. 15071 */ 15072 dtrace_sync(); 15073 15074 dtrace_buffer_free(state->dts_buffer); 15075 dtrace_buffer_free(state->dts_aggbuffer); 15076 15077 for (i = 0; i < nspec; i++) 15078 dtrace_buffer_free(spec[i].dtsp_buffer); 15079 15080#ifdef illumos 15081 if (state->dts_cleaner != CYCLIC_NONE) 15082 cyclic_remove(state->dts_cleaner); 15083 15084 if (state->dts_deadman != CYCLIC_NONE) 15085 cyclic_remove(state->dts_deadman); 15086#else 15087 callout_stop(&state->dts_cleaner); 15088 callout_drain(&state->dts_cleaner); 15089 callout_stop(&state->dts_deadman); 15090 callout_drain(&state->dts_deadman); 15091#endif 15092 15093 dtrace_dstate_fini(&vstate->dtvs_dynvars); 15094 dtrace_vstate_fini(vstate); 15095 if (state->dts_ecbs != NULL) 15096 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 15097 15098 if (state->dts_aggregations != NULL) { 15099#ifdef DEBUG 15100 for (i = 0; i < state->dts_naggregations; i++) 15101 ASSERT(state->dts_aggregations[i] == NULL); 15102#endif 15103 ASSERT(state->dts_naggregations > 0); 15104 kmem_free(state->dts_aggregations, 15105 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 15106 } 15107 15108 kmem_free(state->dts_buffer, bufsize); 15109 kmem_free(state->dts_aggbuffer, bufsize); 15110 15111 for (i = 0; i < nspec; i++) 15112 kmem_free(spec[i].dtsp_buffer, bufsize); 15113 15114 if (spec != NULL) 15115 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15116 15117 dtrace_format_destroy(state); 15118 15119 if (state->dts_aggid_arena != NULL) { 15120#ifdef illumos 15121 vmem_destroy(state->dts_aggid_arena); 15122#else 15123 delete_unrhdr(state->dts_aggid_arena); 15124#endif 15125 state->dts_aggid_arena = NULL; 15126 } 15127#ifdef illumos 15128 ddi_soft_state_free(dtrace_softstate, minor); 15129 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 15130#endif 15131} 15132 15133/* 15134 * DTrace Anonymous Enabling Functions 15135 */ 15136static dtrace_state_t * 15137dtrace_anon_grab(void) 15138{ 15139 dtrace_state_t *state; 15140 15141 ASSERT(MUTEX_HELD(&dtrace_lock)); 15142 15143 if ((state = dtrace_anon.dta_state) == NULL) { 15144 ASSERT(dtrace_anon.dta_enabling == NULL); 15145 return (NULL); 15146 } 15147 15148 ASSERT(dtrace_anon.dta_enabling != NULL); 15149 ASSERT(dtrace_retained != NULL); 15150 15151 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 15152 dtrace_anon.dta_enabling = NULL; 15153 dtrace_anon.dta_state = NULL; 15154 15155 return (state); 15156} 15157 15158static void 15159dtrace_anon_property(void) 15160{ 15161 int i, rv; 15162 dtrace_state_t *state; 15163 dof_hdr_t *dof; 15164 char c[32]; /* enough for "dof-data-" + digits */ 15165 15166 ASSERT(MUTEX_HELD(&dtrace_lock)); 15167 ASSERT(MUTEX_HELD(&cpu_lock)); 15168 15169 for (i = 0; ; i++) { 15170 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 15171 15172 dtrace_err_verbose = 1; 15173 15174 if ((dof = dtrace_dof_property(c)) == NULL) { 15175 dtrace_err_verbose = 0; 15176 break; 15177 } 15178 15179#ifdef illumos 15180 /* 15181 * We want to create anonymous state, so we need to transition 15182 * the kernel debugger to indicate that DTrace is active. If 15183 * this fails (e.g. because the debugger has modified text in 15184 * some way), we won't continue with the processing. 15185 */ 15186 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15187 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 15188 "enabling ignored."); 15189 dtrace_dof_destroy(dof); 15190 break; 15191 } 15192#endif 15193 15194 /* 15195 * If we haven't allocated an anonymous state, we'll do so now. 15196 */ 15197 if ((state = dtrace_anon.dta_state) == NULL) { 15198#ifdef illumos 15199 state = dtrace_state_create(NULL, NULL); 15200#else 15201 state = dtrace_state_create(NULL); 15202#endif 15203 dtrace_anon.dta_state = state; 15204 15205 if (state == NULL) { 15206 /* 15207 * This basically shouldn't happen: the only 15208 * failure mode from dtrace_state_create() is a 15209 * failure of ddi_soft_state_zalloc() that 15210 * itself should never happen. Still, the 15211 * interface allows for a failure mode, and 15212 * we want to fail as gracefully as possible: 15213 * we'll emit an error message and cease 15214 * processing anonymous state in this case. 15215 */ 15216 cmn_err(CE_WARN, "failed to create " 15217 "anonymous state"); 15218 dtrace_dof_destroy(dof); 15219 break; 15220 } 15221 } 15222 15223 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 15224 &dtrace_anon.dta_enabling, 0, B_TRUE); 15225 15226 if (rv == 0) 15227 rv = dtrace_dof_options(dof, state); 15228 15229 dtrace_err_verbose = 0; 15230 dtrace_dof_destroy(dof); 15231 15232 if (rv != 0) { 15233 /* 15234 * This is malformed DOF; chuck any anonymous state 15235 * that we created. 15236 */ 15237 ASSERT(dtrace_anon.dta_enabling == NULL); 15238 dtrace_state_destroy(state); 15239 dtrace_anon.dta_state = NULL; 15240 break; 15241 } 15242 15243 ASSERT(dtrace_anon.dta_enabling != NULL); 15244 } 15245 15246 if (dtrace_anon.dta_enabling != NULL) { 15247 int rval; 15248 15249 /* 15250 * dtrace_enabling_retain() can only fail because we are 15251 * trying to retain more enablings than are allowed -- but 15252 * we only have one anonymous enabling, and we are guaranteed 15253 * to be allowed at least one retained enabling; we assert 15254 * that dtrace_enabling_retain() returns success. 15255 */ 15256 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 15257 ASSERT(rval == 0); 15258 15259 dtrace_enabling_dump(dtrace_anon.dta_enabling); 15260 } 15261} 15262 15263/* 15264 * DTrace Helper Functions 15265 */ 15266static void 15267dtrace_helper_trace(dtrace_helper_action_t *helper, 15268 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 15269{ 15270 uint32_t size, next, nnext, i; 15271 dtrace_helptrace_t *ent, *buffer; 15272 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 15273 15274 if ((buffer = dtrace_helptrace_buffer) == NULL) 15275 return; 15276 15277 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 15278 15279 /* 15280 * What would a tracing framework be without its own tracing 15281 * framework? (Well, a hell of a lot simpler, for starters...) 15282 */ 15283 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 15284 sizeof (uint64_t) - sizeof (uint64_t); 15285 15286 /* 15287 * Iterate until we can allocate a slot in the trace buffer. 15288 */ 15289 do { 15290 next = dtrace_helptrace_next; 15291 15292 if (next + size < dtrace_helptrace_bufsize) { 15293 nnext = next + size; 15294 } else { 15295 nnext = size; 15296 } 15297 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 15298 15299 /* 15300 * We have our slot; fill it in. 15301 */ 15302 if (nnext == size) { 15303 dtrace_helptrace_wrapped++; 15304 next = 0; 15305 } 15306 15307 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next); 15308 ent->dtht_helper = helper; 15309 ent->dtht_where = where; 15310 ent->dtht_nlocals = vstate->dtvs_nlocals; 15311 15312 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 15313 mstate->dtms_fltoffs : -1; 15314 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 15315 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 15316 15317 for (i = 0; i < vstate->dtvs_nlocals; i++) { 15318 dtrace_statvar_t *svar; 15319 15320 if ((svar = vstate->dtvs_locals[i]) == NULL) 15321 continue; 15322 15323 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 15324 ent->dtht_locals[i] = 15325 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 15326 } 15327} 15328 15329static uint64_t 15330dtrace_helper(int which, dtrace_mstate_t *mstate, 15331 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 15332{ 15333 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 15334 uint64_t sarg0 = mstate->dtms_arg[0]; 15335 uint64_t sarg1 = mstate->dtms_arg[1]; 15336 uint64_t rval = 0; 15337 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 15338 dtrace_helper_action_t *helper; 15339 dtrace_vstate_t *vstate; 15340 dtrace_difo_t *pred; 15341 int i, trace = dtrace_helptrace_buffer != NULL; 15342 15343 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 15344 15345 if (helpers == NULL) 15346 return (0); 15347 15348 if ((helper = helpers->dthps_actions[which]) == NULL) 15349 return (0); 15350 15351 vstate = &helpers->dthps_vstate; 15352 mstate->dtms_arg[0] = arg0; 15353 mstate->dtms_arg[1] = arg1; 15354 15355 /* 15356 * Now iterate over each helper. If its predicate evaluates to 'true', 15357 * we'll call the corresponding actions. Note that the below calls 15358 * to dtrace_dif_emulate() may set faults in machine state. This is 15359 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 15360 * the stored DIF offset with its own (which is the desired behavior). 15361 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 15362 * from machine state; this is okay, too. 15363 */ 15364 for (; helper != NULL; helper = helper->dtha_next) { 15365 if ((pred = helper->dtha_predicate) != NULL) { 15366 if (trace) 15367 dtrace_helper_trace(helper, mstate, vstate, 0); 15368 15369 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 15370 goto next; 15371 15372 if (*flags & CPU_DTRACE_FAULT) 15373 goto err; 15374 } 15375 15376 for (i = 0; i < helper->dtha_nactions; i++) { 15377 if (trace) 15378 dtrace_helper_trace(helper, 15379 mstate, vstate, i + 1); 15380 15381 rval = dtrace_dif_emulate(helper->dtha_actions[i], 15382 mstate, vstate, state); 15383 15384 if (*flags & CPU_DTRACE_FAULT) 15385 goto err; 15386 } 15387 15388next: 15389 if (trace) 15390 dtrace_helper_trace(helper, mstate, vstate, 15391 DTRACE_HELPTRACE_NEXT); 15392 } 15393 15394 if (trace) 15395 dtrace_helper_trace(helper, mstate, vstate, 15396 DTRACE_HELPTRACE_DONE); 15397 15398 /* 15399 * Restore the arg0 that we saved upon entry. 15400 */ 15401 mstate->dtms_arg[0] = sarg0; 15402 mstate->dtms_arg[1] = sarg1; 15403 15404 return (rval); 15405 15406err: 15407 if (trace) 15408 dtrace_helper_trace(helper, mstate, vstate, 15409 DTRACE_HELPTRACE_ERR); 15410 15411 /* 15412 * Restore the arg0 that we saved upon entry. 15413 */ 15414 mstate->dtms_arg[0] = sarg0; 15415 mstate->dtms_arg[1] = sarg1; 15416 15417 return (0); 15418} 15419 15420static void 15421dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 15422 dtrace_vstate_t *vstate) 15423{ 15424 int i; 15425 15426 if (helper->dtha_predicate != NULL) 15427 dtrace_difo_release(helper->dtha_predicate, vstate); 15428 15429 for (i = 0; i < helper->dtha_nactions; i++) { 15430 ASSERT(helper->dtha_actions[i] != NULL); 15431 dtrace_difo_release(helper->dtha_actions[i], vstate); 15432 } 15433 15434 kmem_free(helper->dtha_actions, 15435 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 15436 kmem_free(helper, sizeof (dtrace_helper_action_t)); 15437} 15438 15439static int 15440dtrace_helper_destroygen(int gen) 15441{ 15442 proc_t *p = curproc; 15443 dtrace_helpers_t *help = p->p_dtrace_helpers; 15444 dtrace_vstate_t *vstate; 15445 int i; 15446 15447 ASSERT(MUTEX_HELD(&dtrace_lock)); 15448 15449 if (help == NULL || gen > help->dthps_generation) 15450 return (EINVAL); 15451 15452 vstate = &help->dthps_vstate; 15453 15454 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15455 dtrace_helper_action_t *last = NULL, *h, *next; 15456 15457 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15458 next = h->dtha_next; 15459 15460 if (h->dtha_generation == gen) { 15461 if (last != NULL) { 15462 last->dtha_next = next; 15463 } else { 15464 help->dthps_actions[i] = next; 15465 } 15466 15467 dtrace_helper_action_destroy(h, vstate); 15468 } else { 15469 last = h; 15470 } 15471 } 15472 } 15473 15474 /* 15475 * Interate until we've cleared out all helper providers with the 15476 * given generation number. 15477 */ 15478 for (;;) { 15479 dtrace_helper_provider_t *prov; 15480 15481 /* 15482 * Look for a helper provider with the right generation. We 15483 * have to start back at the beginning of the list each time 15484 * because we drop dtrace_lock. It's unlikely that we'll make 15485 * more than two passes. 15486 */ 15487 for (i = 0; i < help->dthps_nprovs; i++) { 15488 prov = help->dthps_provs[i]; 15489 15490 if (prov->dthp_generation == gen) 15491 break; 15492 } 15493 15494 /* 15495 * If there were no matches, we're done. 15496 */ 15497 if (i == help->dthps_nprovs) 15498 break; 15499 15500 /* 15501 * Move the last helper provider into this slot. 15502 */ 15503 help->dthps_nprovs--; 15504 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 15505 help->dthps_provs[help->dthps_nprovs] = NULL; 15506 15507 mutex_exit(&dtrace_lock); 15508 15509 /* 15510 * If we have a meta provider, remove this helper provider. 15511 */ 15512 mutex_enter(&dtrace_meta_lock); 15513 if (dtrace_meta_pid != NULL) { 15514 ASSERT(dtrace_deferred_pid == NULL); 15515 dtrace_helper_provider_remove(&prov->dthp_prov, 15516 p->p_pid); 15517 } 15518 mutex_exit(&dtrace_meta_lock); 15519 15520 dtrace_helper_provider_destroy(prov); 15521 15522 mutex_enter(&dtrace_lock); 15523 } 15524 15525 return (0); 15526} 15527 15528static int 15529dtrace_helper_validate(dtrace_helper_action_t *helper) 15530{ 15531 int err = 0, i; 15532 dtrace_difo_t *dp; 15533 15534 if ((dp = helper->dtha_predicate) != NULL) 15535 err += dtrace_difo_validate_helper(dp); 15536 15537 for (i = 0; i < helper->dtha_nactions; i++) 15538 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 15539 15540 return (err == 0); 15541} 15542 15543static int 15544dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 15545{ 15546 dtrace_helpers_t *help; 15547 dtrace_helper_action_t *helper, *last; 15548 dtrace_actdesc_t *act; 15549 dtrace_vstate_t *vstate; 15550 dtrace_predicate_t *pred; 15551 int count = 0, nactions = 0, i; 15552 15553 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 15554 return (EINVAL); 15555 15556 help = curproc->p_dtrace_helpers; 15557 last = help->dthps_actions[which]; 15558 vstate = &help->dthps_vstate; 15559 15560 for (count = 0; last != NULL; last = last->dtha_next) { 15561 count++; 15562 if (last->dtha_next == NULL) 15563 break; 15564 } 15565 15566 /* 15567 * If we already have dtrace_helper_actions_max helper actions for this 15568 * helper action type, we'll refuse to add a new one. 15569 */ 15570 if (count >= dtrace_helper_actions_max) 15571 return (ENOSPC); 15572 15573 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 15574 helper->dtha_generation = help->dthps_generation; 15575 15576 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 15577 ASSERT(pred->dtp_difo != NULL); 15578 dtrace_difo_hold(pred->dtp_difo); 15579 helper->dtha_predicate = pred->dtp_difo; 15580 } 15581 15582 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 15583 if (act->dtad_kind != DTRACEACT_DIFEXPR) 15584 goto err; 15585 15586 if (act->dtad_difo == NULL) 15587 goto err; 15588 15589 nactions++; 15590 } 15591 15592 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 15593 (helper->dtha_nactions = nactions), KM_SLEEP); 15594 15595 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 15596 dtrace_difo_hold(act->dtad_difo); 15597 helper->dtha_actions[i++] = act->dtad_difo; 15598 } 15599 15600 if (!dtrace_helper_validate(helper)) 15601 goto err; 15602 15603 if (last == NULL) { 15604 help->dthps_actions[which] = helper; 15605 } else { 15606 last->dtha_next = helper; 15607 } 15608 15609 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 15610 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 15611 dtrace_helptrace_next = 0; 15612 } 15613 15614 return (0); 15615err: 15616 dtrace_helper_action_destroy(helper, vstate); 15617 return (EINVAL); 15618} 15619 15620static void 15621dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 15622 dof_helper_t *dofhp) 15623{ 15624 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 15625 15626 mutex_enter(&dtrace_meta_lock); 15627 mutex_enter(&dtrace_lock); 15628 15629 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 15630 /* 15631 * If the dtrace module is loaded but not attached, or if 15632 * there aren't isn't a meta provider registered to deal with 15633 * these provider descriptions, we need to postpone creating 15634 * the actual providers until later. 15635 */ 15636 15637 if (help->dthps_next == NULL && help->dthps_prev == NULL && 15638 dtrace_deferred_pid != help) { 15639 help->dthps_deferred = 1; 15640 help->dthps_pid = p->p_pid; 15641 help->dthps_next = dtrace_deferred_pid; 15642 help->dthps_prev = NULL; 15643 if (dtrace_deferred_pid != NULL) 15644 dtrace_deferred_pid->dthps_prev = help; 15645 dtrace_deferred_pid = help; 15646 } 15647 15648 mutex_exit(&dtrace_lock); 15649 15650 } else if (dofhp != NULL) { 15651 /* 15652 * If the dtrace module is loaded and we have a particular 15653 * helper provider description, pass that off to the 15654 * meta provider. 15655 */ 15656 15657 mutex_exit(&dtrace_lock); 15658 15659 dtrace_helper_provide(dofhp, p->p_pid); 15660 15661 } else { 15662 /* 15663 * Otherwise, just pass all the helper provider descriptions 15664 * off to the meta provider. 15665 */ 15666 15667 int i; 15668 mutex_exit(&dtrace_lock); 15669 15670 for (i = 0; i < help->dthps_nprovs; i++) { 15671 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 15672 p->p_pid); 15673 } 15674 } 15675 15676 mutex_exit(&dtrace_meta_lock); 15677} 15678 15679static int 15680dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 15681{ 15682 dtrace_helpers_t *help; 15683 dtrace_helper_provider_t *hprov, **tmp_provs; 15684 uint_t tmp_maxprovs, i; 15685 15686 ASSERT(MUTEX_HELD(&dtrace_lock)); 15687 15688 help = curproc->p_dtrace_helpers; 15689 ASSERT(help != NULL); 15690 15691 /* 15692 * If we already have dtrace_helper_providers_max helper providers, 15693 * we're refuse to add a new one. 15694 */ 15695 if (help->dthps_nprovs >= dtrace_helper_providers_max) 15696 return (ENOSPC); 15697 15698 /* 15699 * Check to make sure this isn't a duplicate. 15700 */ 15701 for (i = 0; i < help->dthps_nprovs; i++) { 15702 if (dofhp->dofhp_dof == 15703 help->dthps_provs[i]->dthp_prov.dofhp_dof) 15704 return (EALREADY); 15705 } 15706 15707 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 15708 hprov->dthp_prov = *dofhp; 15709 hprov->dthp_ref = 1; 15710 hprov->dthp_generation = gen; 15711 15712 /* 15713 * Allocate a bigger table for helper providers if it's already full. 15714 */ 15715 if (help->dthps_maxprovs == help->dthps_nprovs) { 15716 tmp_maxprovs = help->dthps_maxprovs; 15717 tmp_provs = help->dthps_provs; 15718 15719 if (help->dthps_maxprovs == 0) 15720 help->dthps_maxprovs = 2; 15721 else 15722 help->dthps_maxprovs *= 2; 15723 if (help->dthps_maxprovs > dtrace_helper_providers_max) 15724 help->dthps_maxprovs = dtrace_helper_providers_max; 15725 15726 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 15727 15728 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 15729 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 15730 15731 if (tmp_provs != NULL) { 15732 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 15733 sizeof (dtrace_helper_provider_t *)); 15734 kmem_free(tmp_provs, tmp_maxprovs * 15735 sizeof (dtrace_helper_provider_t *)); 15736 } 15737 } 15738 15739 help->dthps_provs[help->dthps_nprovs] = hprov; 15740 help->dthps_nprovs++; 15741 15742 return (0); 15743} 15744 15745static void 15746dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 15747{ 15748 mutex_enter(&dtrace_lock); 15749 15750 if (--hprov->dthp_ref == 0) { 15751 dof_hdr_t *dof; 15752 mutex_exit(&dtrace_lock); 15753 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 15754 dtrace_dof_destroy(dof); 15755 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 15756 } else { 15757 mutex_exit(&dtrace_lock); 15758 } 15759} 15760 15761static int 15762dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 15763{ 15764 uintptr_t daddr = (uintptr_t)dof; 15765 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 15766 dof_provider_t *provider; 15767 dof_probe_t *probe; 15768 uint8_t *arg; 15769 char *strtab, *typestr; 15770 dof_stridx_t typeidx; 15771 size_t typesz; 15772 uint_t nprobes, j, k; 15773 15774 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 15775 15776 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 15777 dtrace_dof_error(dof, "misaligned section offset"); 15778 return (-1); 15779 } 15780 15781 /* 15782 * The section needs to be large enough to contain the DOF provider 15783 * structure appropriate for the given version. 15784 */ 15785 if (sec->dofs_size < 15786 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 15787 offsetof(dof_provider_t, dofpv_prenoffs) : 15788 sizeof (dof_provider_t))) { 15789 dtrace_dof_error(dof, "provider section too small"); 15790 return (-1); 15791 } 15792 15793 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 15794 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 15795 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 15796 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 15797 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 15798 15799 if (str_sec == NULL || prb_sec == NULL || 15800 arg_sec == NULL || off_sec == NULL) 15801 return (-1); 15802 15803 enoff_sec = NULL; 15804 15805 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 15806 provider->dofpv_prenoffs != DOF_SECT_NONE && 15807 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 15808 provider->dofpv_prenoffs)) == NULL) 15809 return (-1); 15810 15811 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 15812 15813 if (provider->dofpv_name >= str_sec->dofs_size || 15814 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 15815 dtrace_dof_error(dof, "invalid provider name"); 15816 return (-1); 15817 } 15818 15819 if (prb_sec->dofs_entsize == 0 || 15820 prb_sec->dofs_entsize > prb_sec->dofs_size) { 15821 dtrace_dof_error(dof, "invalid entry size"); 15822 return (-1); 15823 } 15824 15825 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 15826 dtrace_dof_error(dof, "misaligned entry size"); 15827 return (-1); 15828 } 15829 15830 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 15831 dtrace_dof_error(dof, "invalid entry size"); 15832 return (-1); 15833 } 15834 15835 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 15836 dtrace_dof_error(dof, "misaligned section offset"); 15837 return (-1); 15838 } 15839 15840 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 15841 dtrace_dof_error(dof, "invalid entry size"); 15842 return (-1); 15843 } 15844 15845 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 15846 15847 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 15848 15849 /* 15850 * Take a pass through the probes to check for errors. 15851 */ 15852 for (j = 0; j < nprobes; j++) { 15853 probe = (dof_probe_t *)(uintptr_t)(daddr + 15854 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 15855 15856 if (probe->dofpr_func >= str_sec->dofs_size) { 15857 dtrace_dof_error(dof, "invalid function name"); 15858 return (-1); 15859 } 15860 15861 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 15862 dtrace_dof_error(dof, "function name too long"); 15863 /* 15864 * Keep going if the function name is too long. 15865 * Unlike provider and probe names, we cannot reasonably 15866 * impose restrictions on function names, since they're 15867 * a property of the code being instrumented. We will 15868 * skip this probe in dtrace_helper_provide_one(). 15869 */ 15870 } 15871 15872 if (probe->dofpr_name >= str_sec->dofs_size || 15873 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 15874 dtrace_dof_error(dof, "invalid probe name"); 15875 return (-1); 15876 } 15877 15878 /* 15879 * The offset count must not wrap the index, and the offsets 15880 * must also not overflow the section's data. 15881 */ 15882 if (probe->dofpr_offidx + probe->dofpr_noffs < 15883 probe->dofpr_offidx || 15884 (probe->dofpr_offidx + probe->dofpr_noffs) * 15885 off_sec->dofs_entsize > off_sec->dofs_size) { 15886 dtrace_dof_error(dof, "invalid probe offset"); 15887 return (-1); 15888 } 15889 15890 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 15891 /* 15892 * If there's no is-enabled offset section, make sure 15893 * there aren't any is-enabled offsets. Otherwise 15894 * perform the same checks as for probe offsets 15895 * (immediately above). 15896 */ 15897 if (enoff_sec == NULL) { 15898 if (probe->dofpr_enoffidx != 0 || 15899 probe->dofpr_nenoffs != 0) { 15900 dtrace_dof_error(dof, "is-enabled " 15901 "offsets with null section"); 15902 return (-1); 15903 } 15904 } else if (probe->dofpr_enoffidx + 15905 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 15906 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 15907 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 15908 dtrace_dof_error(dof, "invalid is-enabled " 15909 "offset"); 15910 return (-1); 15911 } 15912 15913 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 15914 dtrace_dof_error(dof, "zero probe and " 15915 "is-enabled offsets"); 15916 return (-1); 15917 } 15918 } else if (probe->dofpr_noffs == 0) { 15919 dtrace_dof_error(dof, "zero probe offsets"); 15920 return (-1); 15921 } 15922 15923 if (probe->dofpr_argidx + probe->dofpr_xargc < 15924 probe->dofpr_argidx || 15925 (probe->dofpr_argidx + probe->dofpr_xargc) * 15926 arg_sec->dofs_entsize > arg_sec->dofs_size) { 15927 dtrace_dof_error(dof, "invalid args"); 15928 return (-1); 15929 } 15930 15931 typeidx = probe->dofpr_nargv; 15932 typestr = strtab + probe->dofpr_nargv; 15933 for (k = 0; k < probe->dofpr_nargc; k++) { 15934 if (typeidx >= str_sec->dofs_size) { 15935 dtrace_dof_error(dof, "bad " 15936 "native argument type"); 15937 return (-1); 15938 } 15939 15940 typesz = strlen(typestr) + 1; 15941 if (typesz > DTRACE_ARGTYPELEN) { 15942 dtrace_dof_error(dof, "native " 15943 "argument type too long"); 15944 return (-1); 15945 } 15946 typeidx += typesz; 15947 typestr += typesz; 15948 } 15949 15950 typeidx = probe->dofpr_xargv; 15951 typestr = strtab + probe->dofpr_xargv; 15952 for (k = 0; k < probe->dofpr_xargc; k++) { 15953 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 15954 dtrace_dof_error(dof, "bad " 15955 "native argument index"); 15956 return (-1); 15957 } 15958 15959 if (typeidx >= str_sec->dofs_size) { 15960 dtrace_dof_error(dof, "bad " 15961 "translated argument type"); 15962 return (-1); 15963 } 15964 15965 typesz = strlen(typestr) + 1; 15966 if (typesz > DTRACE_ARGTYPELEN) { 15967 dtrace_dof_error(dof, "translated argument " 15968 "type too long"); 15969 return (-1); 15970 } 15971 15972 typeidx += typesz; 15973 typestr += typesz; 15974 } 15975 } 15976 15977 return (0); 15978} 15979 15980static int 15981dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 15982{ 15983 dtrace_helpers_t *help; 15984 dtrace_vstate_t *vstate; 15985 dtrace_enabling_t *enab = NULL; 15986 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 15987 uintptr_t daddr = (uintptr_t)dof; 15988 15989 ASSERT(MUTEX_HELD(&dtrace_lock)); 15990 15991 if ((help = curproc->p_dtrace_helpers) == NULL) 15992 help = dtrace_helpers_create(curproc); 15993 15994 vstate = &help->dthps_vstate; 15995 15996 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 15997 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 15998 dtrace_dof_destroy(dof); 15999 return (rv); 16000 } 16001 16002 /* 16003 * Look for helper providers and validate their descriptions. 16004 */ 16005 if (dhp != NULL) { 16006 for (i = 0; i < dof->dofh_secnum; i++) { 16007 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 16008 dof->dofh_secoff + i * dof->dofh_secsize); 16009 16010 if (sec->dofs_type != DOF_SECT_PROVIDER) 16011 continue; 16012 16013 if (dtrace_helper_provider_validate(dof, sec) != 0) { 16014 dtrace_enabling_destroy(enab); 16015 dtrace_dof_destroy(dof); 16016 return (-1); 16017 } 16018 16019 nprovs++; 16020 } 16021 } 16022 16023 /* 16024 * Now we need to walk through the ECB descriptions in the enabling. 16025 */ 16026 for (i = 0; i < enab->dten_ndesc; i++) { 16027 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 16028 dtrace_probedesc_t *desc = &ep->dted_probe; 16029 16030 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 16031 continue; 16032 16033 if (strcmp(desc->dtpd_mod, "helper") != 0) 16034 continue; 16035 16036 if (strcmp(desc->dtpd_func, "ustack") != 0) 16037 continue; 16038 16039 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 16040 ep)) != 0) { 16041 /* 16042 * Adding this helper action failed -- we are now going 16043 * to rip out the entire generation and return failure. 16044 */ 16045 (void) dtrace_helper_destroygen(help->dthps_generation); 16046 dtrace_enabling_destroy(enab); 16047 dtrace_dof_destroy(dof); 16048 return (-1); 16049 } 16050 16051 nhelpers++; 16052 } 16053 16054 if (nhelpers < enab->dten_ndesc) 16055 dtrace_dof_error(dof, "unmatched helpers"); 16056 16057 gen = help->dthps_generation++; 16058 dtrace_enabling_destroy(enab); 16059 16060 if (dhp != NULL && nprovs > 0) { 16061 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 16062 if (dtrace_helper_provider_add(dhp, gen) == 0) { 16063 mutex_exit(&dtrace_lock); 16064 dtrace_helper_provider_register(curproc, help, dhp); 16065 mutex_enter(&dtrace_lock); 16066 16067 destroy = 0; 16068 } 16069 } 16070 16071 if (destroy) 16072 dtrace_dof_destroy(dof); 16073 16074 return (gen); 16075} 16076 16077static dtrace_helpers_t * 16078dtrace_helpers_create(proc_t *p) 16079{ 16080 dtrace_helpers_t *help; 16081 16082 ASSERT(MUTEX_HELD(&dtrace_lock)); 16083 ASSERT(p->p_dtrace_helpers == NULL); 16084 16085 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 16086 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 16087 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 16088 16089 p->p_dtrace_helpers = help; 16090 dtrace_helpers++; 16091 16092 return (help); 16093} 16094 16095#ifdef illumos 16096static 16097#endif 16098void 16099dtrace_helpers_destroy(proc_t *p) 16100{ 16101 dtrace_helpers_t *help; 16102 dtrace_vstate_t *vstate; 16103#ifdef illumos 16104 proc_t *p = curproc; 16105#endif 16106 int i; 16107 16108 mutex_enter(&dtrace_lock); 16109 16110 ASSERT(p->p_dtrace_helpers != NULL); 16111 ASSERT(dtrace_helpers > 0); 16112 16113 help = p->p_dtrace_helpers; 16114 vstate = &help->dthps_vstate; 16115 16116 /* 16117 * We're now going to lose the help from this process. 16118 */ 16119 p->p_dtrace_helpers = NULL; 16120 dtrace_sync(); 16121 16122 /* 16123 * Destory the helper actions. 16124 */ 16125 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16126 dtrace_helper_action_t *h, *next; 16127 16128 for (h = help->dthps_actions[i]; h != NULL; h = next) { 16129 next = h->dtha_next; 16130 dtrace_helper_action_destroy(h, vstate); 16131 h = next; 16132 } 16133 } 16134 16135 mutex_exit(&dtrace_lock); 16136 16137 /* 16138 * Destroy the helper providers. 16139 */ 16140 if (help->dthps_maxprovs > 0) { 16141 mutex_enter(&dtrace_meta_lock); 16142 if (dtrace_meta_pid != NULL) { 16143 ASSERT(dtrace_deferred_pid == NULL); 16144 16145 for (i = 0; i < help->dthps_nprovs; i++) { 16146 dtrace_helper_provider_remove( 16147 &help->dthps_provs[i]->dthp_prov, p->p_pid); 16148 } 16149 } else { 16150 mutex_enter(&dtrace_lock); 16151 ASSERT(help->dthps_deferred == 0 || 16152 help->dthps_next != NULL || 16153 help->dthps_prev != NULL || 16154 help == dtrace_deferred_pid); 16155 16156 /* 16157 * Remove the helper from the deferred list. 16158 */ 16159 if (help->dthps_next != NULL) 16160 help->dthps_next->dthps_prev = help->dthps_prev; 16161 if (help->dthps_prev != NULL) 16162 help->dthps_prev->dthps_next = help->dthps_next; 16163 if (dtrace_deferred_pid == help) { 16164 dtrace_deferred_pid = help->dthps_next; 16165 ASSERT(help->dthps_prev == NULL); 16166 } 16167 16168 mutex_exit(&dtrace_lock); 16169 } 16170 16171 mutex_exit(&dtrace_meta_lock); 16172 16173 for (i = 0; i < help->dthps_nprovs; i++) { 16174 dtrace_helper_provider_destroy(help->dthps_provs[i]); 16175 } 16176 16177 kmem_free(help->dthps_provs, help->dthps_maxprovs * 16178 sizeof (dtrace_helper_provider_t *)); 16179 } 16180 16181 mutex_enter(&dtrace_lock); 16182 16183 dtrace_vstate_fini(&help->dthps_vstate); 16184 kmem_free(help->dthps_actions, 16185 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 16186 kmem_free(help, sizeof (dtrace_helpers_t)); 16187 16188 --dtrace_helpers; 16189 mutex_exit(&dtrace_lock); 16190} 16191 16192#ifdef illumos 16193static 16194#endif 16195void 16196dtrace_helpers_duplicate(proc_t *from, proc_t *to) 16197{ 16198 dtrace_helpers_t *help, *newhelp; 16199 dtrace_helper_action_t *helper, *new, *last; 16200 dtrace_difo_t *dp; 16201 dtrace_vstate_t *vstate; 16202 int i, j, sz, hasprovs = 0; 16203 16204 mutex_enter(&dtrace_lock); 16205 ASSERT(from->p_dtrace_helpers != NULL); 16206 ASSERT(dtrace_helpers > 0); 16207 16208 help = from->p_dtrace_helpers; 16209 newhelp = dtrace_helpers_create(to); 16210 ASSERT(to->p_dtrace_helpers != NULL); 16211 16212 newhelp->dthps_generation = help->dthps_generation; 16213 vstate = &newhelp->dthps_vstate; 16214 16215 /* 16216 * Duplicate the helper actions. 16217 */ 16218 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16219 if ((helper = help->dthps_actions[i]) == NULL) 16220 continue; 16221 16222 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 16223 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 16224 KM_SLEEP); 16225 new->dtha_generation = helper->dtha_generation; 16226 16227 if ((dp = helper->dtha_predicate) != NULL) { 16228 dp = dtrace_difo_duplicate(dp, vstate); 16229 new->dtha_predicate = dp; 16230 } 16231 16232 new->dtha_nactions = helper->dtha_nactions; 16233 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 16234 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 16235 16236 for (j = 0; j < new->dtha_nactions; j++) { 16237 dtrace_difo_t *dp = helper->dtha_actions[j]; 16238 16239 ASSERT(dp != NULL); 16240 dp = dtrace_difo_duplicate(dp, vstate); 16241 new->dtha_actions[j] = dp; 16242 } 16243 16244 if (last != NULL) { 16245 last->dtha_next = new; 16246 } else { 16247 newhelp->dthps_actions[i] = new; 16248 } 16249 16250 last = new; 16251 } 16252 } 16253 16254 /* 16255 * Duplicate the helper providers and register them with the 16256 * DTrace framework. 16257 */ 16258 if (help->dthps_nprovs > 0) { 16259 newhelp->dthps_nprovs = help->dthps_nprovs; 16260 newhelp->dthps_maxprovs = help->dthps_nprovs; 16261 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 16262 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16263 for (i = 0; i < newhelp->dthps_nprovs; i++) { 16264 newhelp->dthps_provs[i] = help->dthps_provs[i]; 16265 newhelp->dthps_provs[i]->dthp_ref++; 16266 } 16267 16268 hasprovs = 1; 16269 } 16270 16271 mutex_exit(&dtrace_lock); 16272 16273 if (hasprovs) 16274 dtrace_helper_provider_register(to, newhelp, NULL); 16275} 16276 16277/* 16278 * DTrace Hook Functions 16279 */ 16280static void 16281dtrace_module_loaded(modctl_t *ctl) 16282{ 16283 dtrace_provider_t *prv; 16284 16285 mutex_enter(&dtrace_provider_lock); 16286#ifdef illumos 16287 mutex_enter(&mod_lock); 16288#endif 16289 16290#ifdef illumos 16291 ASSERT(ctl->mod_busy); 16292#endif 16293 16294 /* 16295 * We're going to call each providers per-module provide operation 16296 * specifying only this module. 16297 */ 16298 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 16299 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 16300 16301#ifdef illumos 16302 mutex_exit(&mod_lock); 16303#endif 16304 mutex_exit(&dtrace_provider_lock); 16305 16306 /* 16307 * If we have any retained enablings, we need to match against them. 16308 * Enabling probes requires that cpu_lock be held, and we cannot hold 16309 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 16310 * module. (In particular, this happens when loading scheduling 16311 * classes.) So if we have any retained enablings, we need to dispatch 16312 * our task queue to do the match for us. 16313 */ 16314 mutex_enter(&dtrace_lock); 16315 16316 if (dtrace_retained == NULL) { 16317 mutex_exit(&dtrace_lock); 16318 return; 16319 } 16320 16321 (void) taskq_dispatch(dtrace_taskq, 16322 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 16323 16324 mutex_exit(&dtrace_lock); 16325 16326 /* 16327 * And now, for a little heuristic sleaze: in general, we want to 16328 * match modules as soon as they load. However, we cannot guarantee 16329 * this, because it would lead us to the lock ordering violation 16330 * outlined above. The common case, of course, is that cpu_lock is 16331 * _not_ held -- so we delay here for a clock tick, hoping that that's 16332 * long enough for the task queue to do its work. If it's not, it's 16333 * not a serious problem -- it just means that the module that we 16334 * just loaded may not be immediately instrumentable. 16335 */ 16336 delay(1); 16337} 16338 16339static void 16340#ifdef illumos 16341dtrace_module_unloaded(modctl_t *ctl) 16342#else 16343dtrace_module_unloaded(modctl_t *ctl, int *error) 16344#endif 16345{ 16346 dtrace_probe_t template, *probe, *first, *next; 16347 dtrace_provider_t *prov; 16348#ifndef illumos 16349 char modname[DTRACE_MODNAMELEN]; 16350 size_t len; 16351#endif 16352 16353#ifdef illumos 16354 template.dtpr_mod = ctl->mod_modname; 16355#else 16356 /* Handle the fact that ctl->filename may end in ".ko". */ 16357 strlcpy(modname, ctl->filename, sizeof(modname)); 16358 len = strlen(ctl->filename); 16359 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0) 16360 modname[len - 3] = '\0'; 16361 template.dtpr_mod = modname; 16362#endif 16363 16364 mutex_enter(&dtrace_provider_lock); 16365#ifdef illumos 16366 mutex_enter(&mod_lock); 16367#endif 16368 mutex_enter(&dtrace_lock); 16369 16370#ifndef illumos 16371 if (ctl->nenabled > 0) { 16372 /* Don't allow unloads if a probe is enabled. */ 16373 mutex_exit(&dtrace_provider_lock); 16374 mutex_exit(&dtrace_lock); 16375 *error = -1; 16376 printf( 16377 "kldunload: attempt to unload module that has DTrace probes enabled\n"); 16378 return; 16379 } 16380#endif 16381 16382 if (dtrace_bymod == NULL) { 16383 /* 16384 * The DTrace module is loaded (obviously) but not attached; 16385 * we don't have any work to do. 16386 */ 16387 mutex_exit(&dtrace_provider_lock); 16388#ifdef illumos 16389 mutex_exit(&mod_lock); 16390#endif 16391 mutex_exit(&dtrace_lock); 16392 return; 16393 } 16394 16395 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 16396 probe != NULL; probe = probe->dtpr_nextmod) { 16397 if (probe->dtpr_ecb != NULL) { 16398 mutex_exit(&dtrace_provider_lock); 16399#ifdef illumos 16400 mutex_exit(&mod_lock); 16401#endif 16402 mutex_exit(&dtrace_lock); 16403 16404 /* 16405 * This shouldn't _actually_ be possible -- we're 16406 * unloading a module that has an enabled probe in it. 16407 * (It's normally up to the provider to make sure that 16408 * this can't happen.) However, because dtps_enable() 16409 * doesn't have a failure mode, there can be an 16410 * enable/unload race. Upshot: we don't want to 16411 * assert, but we're not going to disable the 16412 * probe, either. 16413 */ 16414 if (dtrace_err_verbose) { 16415#ifdef illumos 16416 cmn_err(CE_WARN, "unloaded module '%s' had " 16417 "enabled probes", ctl->mod_modname); 16418#else 16419 cmn_err(CE_WARN, "unloaded module '%s' had " 16420 "enabled probes", modname); 16421#endif 16422 } 16423 16424 return; 16425 } 16426 } 16427 16428 probe = first; 16429 16430 for (first = NULL; probe != NULL; probe = next) { 16431 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 16432 16433 dtrace_probes[probe->dtpr_id - 1] = NULL; 16434 16435 next = probe->dtpr_nextmod; 16436 dtrace_hash_remove(dtrace_bymod, probe); 16437 dtrace_hash_remove(dtrace_byfunc, probe); 16438 dtrace_hash_remove(dtrace_byname, probe); 16439 16440 if (first == NULL) { 16441 first = probe; 16442 probe->dtpr_nextmod = NULL; 16443 } else { 16444 probe->dtpr_nextmod = first; 16445 first = probe; 16446 } 16447 } 16448 16449 /* 16450 * We've removed all of the module's probes from the hash chains and 16451 * from the probe array. Now issue a dtrace_sync() to be sure that 16452 * everyone has cleared out from any probe array processing. 16453 */ 16454 dtrace_sync(); 16455 16456 for (probe = first; probe != NULL; probe = first) { 16457 first = probe->dtpr_nextmod; 16458 prov = probe->dtpr_provider; 16459 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 16460 probe->dtpr_arg); 16461 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 16462 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 16463 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 16464#ifdef illumos 16465 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 16466#else 16467 free_unr(dtrace_arena, probe->dtpr_id); 16468#endif 16469 kmem_free(probe, sizeof (dtrace_probe_t)); 16470 } 16471 16472 mutex_exit(&dtrace_lock); 16473#ifdef illumos 16474 mutex_exit(&mod_lock); 16475#endif 16476 mutex_exit(&dtrace_provider_lock); 16477} 16478 16479#ifndef illumos 16480static void 16481dtrace_kld_load(void *arg __unused, linker_file_t lf) 16482{ 16483 16484 dtrace_module_loaded(lf); 16485} 16486 16487static void 16488dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error) 16489{ 16490 16491 if (*error != 0) 16492 /* We already have an error, so don't do anything. */ 16493 return; 16494 dtrace_module_unloaded(lf, error); 16495} 16496#endif 16497 16498#ifdef illumos 16499static void 16500dtrace_suspend(void) 16501{ 16502 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 16503} 16504 16505static void 16506dtrace_resume(void) 16507{ 16508 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 16509} 16510#endif 16511 16512static int 16513dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 16514{ 16515 ASSERT(MUTEX_HELD(&cpu_lock)); 16516 mutex_enter(&dtrace_lock); 16517 16518 switch (what) { 16519 case CPU_CONFIG: { 16520 dtrace_state_t *state; 16521 dtrace_optval_t *opt, rs, c; 16522 16523 /* 16524 * For now, we only allocate a new buffer for anonymous state. 16525 */ 16526 if ((state = dtrace_anon.dta_state) == NULL) 16527 break; 16528 16529 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 16530 break; 16531 16532 opt = state->dts_options; 16533 c = opt[DTRACEOPT_CPU]; 16534 16535 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 16536 break; 16537 16538 /* 16539 * Regardless of what the actual policy is, we're going to 16540 * temporarily set our resize policy to be manual. We're 16541 * also going to temporarily set our CPU option to denote 16542 * the newly configured CPU. 16543 */ 16544 rs = opt[DTRACEOPT_BUFRESIZE]; 16545 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 16546 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 16547 16548 (void) dtrace_state_buffers(state); 16549 16550 opt[DTRACEOPT_BUFRESIZE] = rs; 16551 opt[DTRACEOPT_CPU] = c; 16552 16553 break; 16554 } 16555 16556 case CPU_UNCONFIG: 16557 /* 16558 * We don't free the buffer in the CPU_UNCONFIG case. (The 16559 * buffer will be freed when the consumer exits.) 16560 */ 16561 break; 16562 16563 default: 16564 break; 16565 } 16566 16567 mutex_exit(&dtrace_lock); 16568 return (0); 16569} 16570 16571#ifdef illumos 16572static void 16573dtrace_cpu_setup_initial(processorid_t cpu) 16574{ 16575 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 16576} 16577#endif 16578 16579static void 16580dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 16581{ 16582 if (dtrace_toxranges >= dtrace_toxranges_max) { 16583 int osize, nsize; 16584 dtrace_toxrange_t *range; 16585 16586 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16587 16588 if (osize == 0) { 16589 ASSERT(dtrace_toxrange == NULL); 16590 ASSERT(dtrace_toxranges_max == 0); 16591 dtrace_toxranges_max = 1; 16592 } else { 16593 dtrace_toxranges_max <<= 1; 16594 } 16595 16596 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16597 range = kmem_zalloc(nsize, KM_SLEEP); 16598 16599 if (dtrace_toxrange != NULL) { 16600 ASSERT(osize != 0); 16601 bcopy(dtrace_toxrange, range, osize); 16602 kmem_free(dtrace_toxrange, osize); 16603 } 16604 16605 dtrace_toxrange = range; 16606 } 16607 16608 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 16609 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 16610 16611 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 16612 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 16613 dtrace_toxranges++; 16614} 16615 16616static void 16617dtrace_getf_barrier() 16618{ 16619#ifdef illumos 16620 /* 16621 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 16622 * that contain calls to getf(), this routine will be called on every 16623 * closef() before either the underlying vnode is released or the 16624 * file_t itself is freed. By the time we are here, it is essential 16625 * that the file_t can no longer be accessed from a call to getf() 16626 * in probe context -- that assures that a dtrace_sync() can be used 16627 * to clear out any enablings referring to the old structures. 16628 */ 16629 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 16630 kcred->cr_zone->zone_dtrace_getf != 0) 16631 dtrace_sync(); 16632#endif 16633} 16634 16635/* 16636 * DTrace Driver Cookbook Functions 16637 */ 16638#ifdef illumos 16639/*ARGSUSED*/ 16640static int 16641dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 16642{ 16643 dtrace_provider_id_t id; 16644 dtrace_state_t *state = NULL; 16645 dtrace_enabling_t *enab; 16646 16647 mutex_enter(&cpu_lock); 16648 mutex_enter(&dtrace_provider_lock); 16649 mutex_enter(&dtrace_lock); 16650 16651 if (ddi_soft_state_init(&dtrace_softstate, 16652 sizeof (dtrace_state_t), 0) != 0) { 16653 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 16654 mutex_exit(&cpu_lock); 16655 mutex_exit(&dtrace_provider_lock); 16656 mutex_exit(&dtrace_lock); 16657 return (DDI_FAILURE); 16658 } 16659 16660 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 16661 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 16662 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 16663 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 16664 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 16665 ddi_remove_minor_node(devi, NULL); 16666 ddi_soft_state_fini(&dtrace_softstate); 16667 mutex_exit(&cpu_lock); 16668 mutex_exit(&dtrace_provider_lock); 16669 mutex_exit(&dtrace_lock); 16670 return (DDI_FAILURE); 16671 } 16672 16673 ddi_report_dev(devi); 16674 dtrace_devi = devi; 16675 16676 dtrace_modload = dtrace_module_loaded; 16677 dtrace_modunload = dtrace_module_unloaded; 16678 dtrace_cpu_init = dtrace_cpu_setup_initial; 16679 dtrace_helpers_cleanup = dtrace_helpers_destroy; 16680 dtrace_helpers_fork = dtrace_helpers_duplicate; 16681 dtrace_cpustart_init = dtrace_suspend; 16682 dtrace_cpustart_fini = dtrace_resume; 16683 dtrace_debugger_init = dtrace_suspend; 16684 dtrace_debugger_fini = dtrace_resume; 16685 16686 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16687 16688 ASSERT(MUTEX_HELD(&cpu_lock)); 16689 16690 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 16691 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 16692 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 16693 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 16694 VM_SLEEP | VMC_IDENTIFIER); 16695 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 16696 1, INT_MAX, 0); 16697 16698 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 16699 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 16700 NULL, NULL, NULL, NULL, NULL, 0); 16701 16702 ASSERT(MUTEX_HELD(&cpu_lock)); 16703 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 16704 offsetof(dtrace_probe_t, dtpr_nextmod), 16705 offsetof(dtrace_probe_t, dtpr_prevmod)); 16706 16707 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 16708 offsetof(dtrace_probe_t, dtpr_nextfunc), 16709 offsetof(dtrace_probe_t, dtpr_prevfunc)); 16710 16711 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 16712 offsetof(dtrace_probe_t, dtpr_nextname), 16713 offsetof(dtrace_probe_t, dtpr_prevname)); 16714 16715 if (dtrace_retain_max < 1) { 16716 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 16717 "setting to 1", dtrace_retain_max); 16718 dtrace_retain_max = 1; 16719 } 16720 16721 /* 16722 * Now discover our toxic ranges. 16723 */ 16724 dtrace_toxic_ranges(dtrace_toxrange_add); 16725 16726 /* 16727 * Before we register ourselves as a provider to our own framework, 16728 * we would like to assert that dtrace_provider is NULL -- but that's 16729 * not true if we were loaded as a dependency of a DTrace provider. 16730 * Once we've registered, we can assert that dtrace_provider is our 16731 * pseudo provider. 16732 */ 16733 (void) dtrace_register("dtrace", &dtrace_provider_attr, 16734 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 16735 16736 ASSERT(dtrace_provider != NULL); 16737 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 16738 16739 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 16740 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 16741 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 16742 dtrace_provider, NULL, NULL, "END", 0, NULL); 16743 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 16744 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 16745 16746 dtrace_anon_property(); 16747 mutex_exit(&cpu_lock); 16748 16749 /* 16750 * If there are already providers, we must ask them to provide their 16751 * probes, and then match any anonymous enabling against them. Note 16752 * that there should be no other retained enablings at this time: 16753 * the only retained enablings at this time should be the anonymous 16754 * enabling. 16755 */ 16756 if (dtrace_anon.dta_enabling != NULL) { 16757 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 16758 16759 dtrace_enabling_provide(NULL); 16760 state = dtrace_anon.dta_state; 16761 16762 /* 16763 * We couldn't hold cpu_lock across the above call to 16764 * dtrace_enabling_provide(), but we must hold it to actually 16765 * enable the probes. We have to drop all of our locks, pick 16766 * up cpu_lock, and regain our locks before matching the 16767 * retained anonymous enabling. 16768 */ 16769 mutex_exit(&dtrace_lock); 16770 mutex_exit(&dtrace_provider_lock); 16771 16772 mutex_enter(&cpu_lock); 16773 mutex_enter(&dtrace_provider_lock); 16774 mutex_enter(&dtrace_lock); 16775 16776 if ((enab = dtrace_anon.dta_enabling) != NULL) 16777 (void) dtrace_enabling_match(enab, NULL); 16778 16779 mutex_exit(&cpu_lock); 16780 } 16781 16782 mutex_exit(&dtrace_lock); 16783 mutex_exit(&dtrace_provider_lock); 16784 16785 if (state != NULL) { 16786 /* 16787 * If we created any anonymous state, set it going now. 16788 */ 16789 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 16790 } 16791 16792 return (DDI_SUCCESS); 16793} 16794#endif /* illumos */ 16795 16796#ifndef illumos 16797static void dtrace_dtr(void *); 16798#endif 16799 16800/*ARGSUSED*/ 16801static int 16802#ifdef illumos 16803dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 16804#else 16805dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 16806#endif 16807{ 16808 dtrace_state_t *state; 16809 uint32_t priv; 16810 uid_t uid; 16811 zoneid_t zoneid; 16812 16813#ifdef illumos 16814 if (getminor(*devp) == DTRACEMNRN_HELPER) 16815 return (0); 16816 16817 /* 16818 * If this wasn't an open with the "helper" minor, then it must be 16819 * the "dtrace" minor. 16820 */ 16821 if (getminor(*devp) == DTRACEMNRN_DTRACE) 16822 return (ENXIO); 16823#else 16824 cred_t *cred_p = NULL; 16825 cred_p = dev->si_cred; 16826 16827 /* 16828 * If no DTRACE_PRIV_* bits are set in the credential, then the 16829 * caller lacks sufficient permission to do anything with DTrace. 16830 */ 16831 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 16832 if (priv == DTRACE_PRIV_NONE) { 16833#endif 16834 16835 return (EACCES); 16836 } 16837 16838 /* 16839 * Ask all providers to provide all their probes. 16840 */ 16841 mutex_enter(&dtrace_provider_lock); 16842 dtrace_probe_provide(NULL, NULL); 16843 mutex_exit(&dtrace_provider_lock); 16844 16845 mutex_enter(&cpu_lock); 16846 mutex_enter(&dtrace_lock); 16847 dtrace_opens++; 16848 dtrace_membar_producer(); 16849 16850#ifdef illumos 16851 /* 16852 * If the kernel debugger is active (that is, if the kernel debugger 16853 * modified text in some way), we won't allow the open. 16854 */ 16855 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 16856 dtrace_opens--; 16857 mutex_exit(&cpu_lock); 16858 mutex_exit(&dtrace_lock); 16859 return (EBUSY); 16860 } 16861 16862 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) { 16863 /* 16864 * If DTrace helper tracing is enabled, we need to allocate the 16865 * trace buffer and initialize the values. 16866 */ 16867 dtrace_helptrace_buffer = 16868 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 16869 dtrace_helptrace_next = 0; 16870 dtrace_helptrace_wrapped = 0; 16871 dtrace_helptrace_enable = 0; 16872 } 16873 16874 state = dtrace_state_create(devp, cred_p); 16875#else 16876 state = dtrace_state_create(dev); 16877 devfs_set_cdevpriv(state, dtrace_dtr); 16878#endif 16879 16880 mutex_exit(&cpu_lock); 16881 16882 if (state == NULL) { 16883#ifdef illumos 16884 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 16885 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16886#else 16887 --dtrace_opens; 16888#endif 16889 mutex_exit(&dtrace_lock); 16890 return (EAGAIN); 16891 } 16892 16893 mutex_exit(&dtrace_lock); 16894 16895 return (0); 16896} 16897 16898/*ARGSUSED*/ 16899#ifdef illumos 16900static int 16901dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 16902#else 16903static void 16904dtrace_dtr(void *data) 16905#endif 16906{ 16907#ifdef illumos 16908 minor_t minor = getminor(dev); 16909 dtrace_state_t *state; 16910#endif 16911 dtrace_helptrace_t *buf = NULL; 16912 16913#ifdef illumos 16914 if (minor == DTRACEMNRN_HELPER) 16915 return (0); 16916 16917 state = ddi_get_soft_state(dtrace_softstate, minor); 16918#else 16919 dtrace_state_t *state = data; 16920#endif 16921 16922 mutex_enter(&cpu_lock); 16923 mutex_enter(&dtrace_lock); 16924 16925#ifdef illumos 16926 if (state->dts_anon) 16927#else 16928 if (state != NULL && state->dts_anon) 16929#endif 16930 { 16931 /* 16932 * There is anonymous state. Destroy that first. 16933 */ 16934 ASSERT(dtrace_anon.dta_state == NULL); 16935 dtrace_state_destroy(state->dts_anon); 16936 } 16937 16938 if (dtrace_helptrace_disable) { 16939 /* 16940 * If we have been told to disable helper tracing, set the 16941 * buffer to NULL before calling into dtrace_state_destroy(); 16942 * we take advantage of its dtrace_sync() to know that no 16943 * CPU is in probe context with enabled helper tracing 16944 * after it returns. 16945 */ 16946 buf = dtrace_helptrace_buffer; 16947 dtrace_helptrace_buffer = NULL; 16948 } 16949 16950#ifdef illumos 16951 dtrace_state_destroy(state); 16952#else 16953 if (state != NULL) { 16954 dtrace_state_destroy(state); 16955 kmem_free(state, 0); 16956 } 16957#endif 16958 ASSERT(dtrace_opens > 0); 16959 16960#ifdef illumos 16961 /* 16962 * Only relinquish control of the kernel debugger interface when there 16963 * are no consumers and no anonymous enablings. 16964 */ 16965 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 16966 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16967#else 16968 --dtrace_opens; 16969#endif 16970 16971 if (buf != NULL) { 16972 kmem_free(buf, dtrace_helptrace_bufsize); 16973 dtrace_helptrace_disable = 0; 16974 } 16975 16976 mutex_exit(&dtrace_lock); 16977 mutex_exit(&cpu_lock); 16978 16979#ifdef illumos 16980 return (0); 16981#endif 16982} 16983 16984#ifdef illumos 16985/*ARGSUSED*/ 16986static int 16987dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 16988{ 16989 int rval; 16990 dof_helper_t help, *dhp = NULL; 16991 16992 switch (cmd) { 16993 case DTRACEHIOC_ADDDOF: 16994 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 16995 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 16996 return (EFAULT); 16997 } 16998 16999 dhp = &help; 17000 arg = (intptr_t)help.dofhp_dof; 17001 /*FALLTHROUGH*/ 17002 17003 case DTRACEHIOC_ADD: { 17004 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 17005 17006 if (dof == NULL) 17007 return (rval); 17008 17009 mutex_enter(&dtrace_lock); 17010 17011 /* 17012 * dtrace_helper_slurp() takes responsibility for the dof -- 17013 * it may free it now or it may save it and free it later. 17014 */ 17015 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 17016 *rv = rval; 17017 rval = 0; 17018 } else { 17019 rval = EINVAL; 17020 } 17021 17022 mutex_exit(&dtrace_lock); 17023 return (rval); 17024 } 17025 17026 case DTRACEHIOC_REMOVE: { 17027 mutex_enter(&dtrace_lock); 17028 rval = dtrace_helper_destroygen(arg); 17029 mutex_exit(&dtrace_lock); 17030 17031 return (rval); 17032 } 17033 17034 default: 17035 break; 17036 } 17037 17038 return (ENOTTY); 17039} 17040 17041/*ARGSUSED*/ 17042static int 17043dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 17044{ 17045 minor_t minor = getminor(dev); 17046 dtrace_state_t *state; 17047 int rval; 17048 17049 if (minor == DTRACEMNRN_HELPER) 17050 return (dtrace_ioctl_helper(cmd, arg, rv)); 17051 17052 state = ddi_get_soft_state(dtrace_softstate, minor); 17053 17054 if (state->dts_anon) { 17055 ASSERT(dtrace_anon.dta_state == NULL); 17056 state = state->dts_anon; 17057 } 17058 17059 switch (cmd) { 17060 case DTRACEIOC_PROVIDER: { 17061 dtrace_providerdesc_t pvd; 17062 dtrace_provider_t *pvp; 17063 17064 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 17065 return (EFAULT); 17066 17067 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 17068 mutex_enter(&dtrace_provider_lock); 17069 17070 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 17071 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 17072 break; 17073 } 17074 17075 mutex_exit(&dtrace_provider_lock); 17076 17077 if (pvp == NULL) 17078 return (ESRCH); 17079 17080 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 17081 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 17082 17083 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 17084 return (EFAULT); 17085 17086 return (0); 17087 } 17088 17089 case DTRACEIOC_EPROBE: { 17090 dtrace_eprobedesc_t epdesc; 17091 dtrace_ecb_t *ecb; 17092 dtrace_action_t *act; 17093 void *buf; 17094 size_t size; 17095 uintptr_t dest; 17096 int nrecs; 17097 17098 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 17099 return (EFAULT); 17100 17101 mutex_enter(&dtrace_lock); 17102 17103 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 17104 mutex_exit(&dtrace_lock); 17105 return (EINVAL); 17106 } 17107 17108 if (ecb->dte_probe == NULL) { 17109 mutex_exit(&dtrace_lock); 17110 return (EINVAL); 17111 } 17112 17113 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 17114 epdesc.dtepd_uarg = ecb->dte_uarg; 17115 epdesc.dtepd_size = ecb->dte_size; 17116 17117 nrecs = epdesc.dtepd_nrecs; 17118 epdesc.dtepd_nrecs = 0; 17119 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17120 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17121 continue; 17122 17123 epdesc.dtepd_nrecs++; 17124 } 17125 17126 /* 17127 * Now that we have the size, we need to allocate a temporary 17128 * buffer in which to store the complete description. We need 17129 * the temporary buffer to be able to drop dtrace_lock() 17130 * across the copyout(), below. 17131 */ 17132 size = sizeof (dtrace_eprobedesc_t) + 17133 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 17134 17135 buf = kmem_alloc(size, KM_SLEEP); 17136 dest = (uintptr_t)buf; 17137 17138 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 17139 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 17140 17141 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17142 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17143 continue; 17144 17145 if (nrecs-- == 0) 17146 break; 17147 17148 bcopy(&act->dta_rec, (void *)dest, 17149 sizeof (dtrace_recdesc_t)); 17150 dest += sizeof (dtrace_recdesc_t); 17151 } 17152 17153 mutex_exit(&dtrace_lock); 17154 17155 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17156 kmem_free(buf, size); 17157 return (EFAULT); 17158 } 17159 17160 kmem_free(buf, size); 17161 return (0); 17162 } 17163 17164 case DTRACEIOC_AGGDESC: { 17165 dtrace_aggdesc_t aggdesc; 17166 dtrace_action_t *act; 17167 dtrace_aggregation_t *agg; 17168 int nrecs; 17169 uint32_t offs; 17170 dtrace_recdesc_t *lrec; 17171 void *buf; 17172 size_t size; 17173 uintptr_t dest; 17174 17175 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 17176 return (EFAULT); 17177 17178 mutex_enter(&dtrace_lock); 17179 17180 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 17181 mutex_exit(&dtrace_lock); 17182 return (EINVAL); 17183 } 17184 17185 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 17186 17187 nrecs = aggdesc.dtagd_nrecs; 17188 aggdesc.dtagd_nrecs = 0; 17189 17190 offs = agg->dtag_base; 17191 lrec = &agg->dtag_action.dta_rec; 17192 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 17193 17194 for (act = agg->dtag_first; ; act = act->dta_next) { 17195 ASSERT(act->dta_intuple || 17196 DTRACEACT_ISAGG(act->dta_kind)); 17197 17198 /* 17199 * If this action has a record size of zero, it 17200 * denotes an argument to the aggregating action. 17201 * Because the presence of this record doesn't (or 17202 * shouldn't) affect the way the data is interpreted, 17203 * we don't copy it out to save user-level the 17204 * confusion of dealing with a zero-length record. 17205 */ 17206 if (act->dta_rec.dtrd_size == 0) { 17207 ASSERT(agg->dtag_hasarg); 17208 continue; 17209 } 17210 17211 aggdesc.dtagd_nrecs++; 17212 17213 if (act == &agg->dtag_action) 17214 break; 17215 } 17216 17217 /* 17218 * Now that we have the size, we need to allocate a temporary 17219 * buffer in which to store the complete description. We need 17220 * the temporary buffer to be able to drop dtrace_lock() 17221 * across the copyout(), below. 17222 */ 17223 size = sizeof (dtrace_aggdesc_t) + 17224 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 17225 17226 buf = kmem_alloc(size, KM_SLEEP); 17227 dest = (uintptr_t)buf; 17228 17229 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 17230 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 17231 17232 for (act = agg->dtag_first; ; act = act->dta_next) { 17233 dtrace_recdesc_t rec = act->dta_rec; 17234 17235 /* 17236 * See the comment in the above loop for why we pass 17237 * over zero-length records. 17238 */ 17239 if (rec.dtrd_size == 0) { 17240 ASSERT(agg->dtag_hasarg); 17241 continue; 17242 } 17243 17244 if (nrecs-- == 0) 17245 break; 17246 17247 rec.dtrd_offset -= offs; 17248 bcopy(&rec, (void *)dest, sizeof (rec)); 17249 dest += sizeof (dtrace_recdesc_t); 17250 17251 if (act == &agg->dtag_action) 17252 break; 17253 } 17254 17255 mutex_exit(&dtrace_lock); 17256 17257 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17258 kmem_free(buf, size); 17259 return (EFAULT); 17260 } 17261 17262 kmem_free(buf, size); 17263 return (0); 17264 } 17265 17266 case DTRACEIOC_ENABLE: { 17267 dof_hdr_t *dof; 17268 dtrace_enabling_t *enab = NULL; 17269 dtrace_vstate_t *vstate; 17270 int err = 0; 17271 17272 *rv = 0; 17273 17274 /* 17275 * If a NULL argument has been passed, we take this as our 17276 * cue to reevaluate our enablings. 17277 */ 17278 if (arg == NULL) { 17279 dtrace_enabling_matchall(); 17280 17281 return (0); 17282 } 17283 17284 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 17285 return (rval); 17286 17287 mutex_enter(&cpu_lock); 17288 mutex_enter(&dtrace_lock); 17289 vstate = &state->dts_vstate; 17290 17291 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 17292 mutex_exit(&dtrace_lock); 17293 mutex_exit(&cpu_lock); 17294 dtrace_dof_destroy(dof); 17295 return (EBUSY); 17296 } 17297 17298 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 17299 mutex_exit(&dtrace_lock); 17300 mutex_exit(&cpu_lock); 17301 dtrace_dof_destroy(dof); 17302 return (EINVAL); 17303 } 17304 17305 if ((rval = dtrace_dof_options(dof, state)) != 0) { 17306 dtrace_enabling_destroy(enab); 17307 mutex_exit(&dtrace_lock); 17308 mutex_exit(&cpu_lock); 17309 dtrace_dof_destroy(dof); 17310 return (rval); 17311 } 17312 17313 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 17314 err = dtrace_enabling_retain(enab); 17315 } else { 17316 dtrace_enabling_destroy(enab); 17317 } 17318 17319 mutex_exit(&cpu_lock); 17320 mutex_exit(&dtrace_lock); 17321 dtrace_dof_destroy(dof); 17322 17323 return (err); 17324 } 17325 17326 case DTRACEIOC_REPLICATE: { 17327 dtrace_repldesc_t desc; 17328 dtrace_probedesc_t *match = &desc.dtrpd_match; 17329 dtrace_probedesc_t *create = &desc.dtrpd_create; 17330 int err; 17331 17332 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17333 return (EFAULT); 17334 17335 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17336 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17337 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17338 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17339 17340 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17341 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17342 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17343 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17344 17345 mutex_enter(&dtrace_lock); 17346 err = dtrace_enabling_replicate(state, match, create); 17347 mutex_exit(&dtrace_lock); 17348 17349 return (err); 17350 } 17351 17352 case DTRACEIOC_PROBEMATCH: 17353 case DTRACEIOC_PROBES: { 17354 dtrace_probe_t *probe = NULL; 17355 dtrace_probedesc_t desc; 17356 dtrace_probekey_t pkey; 17357 dtrace_id_t i; 17358 int m = 0; 17359 uint32_t priv; 17360 uid_t uid; 17361 zoneid_t zoneid; 17362 17363 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17364 return (EFAULT); 17365 17366 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17367 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17368 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17369 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17370 17371 /* 17372 * Before we attempt to match this probe, we want to give 17373 * all providers the opportunity to provide it. 17374 */ 17375 if (desc.dtpd_id == DTRACE_IDNONE) { 17376 mutex_enter(&dtrace_provider_lock); 17377 dtrace_probe_provide(&desc, NULL); 17378 mutex_exit(&dtrace_provider_lock); 17379 desc.dtpd_id++; 17380 } 17381 17382 if (cmd == DTRACEIOC_PROBEMATCH) { 17383 dtrace_probekey(&desc, &pkey); 17384 pkey.dtpk_id = DTRACE_IDNONE; 17385 } 17386 17387 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 17388 17389 mutex_enter(&dtrace_lock); 17390 17391 if (cmd == DTRACEIOC_PROBEMATCH) { 17392 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17393 if ((probe = dtrace_probes[i - 1]) != NULL && 17394 (m = dtrace_match_probe(probe, &pkey, 17395 priv, uid, zoneid)) != 0) 17396 break; 17397 } 17398 17399 if (m < 0) { 17400 mutex_exit(&dtrace_lock); 17401 return (EINVAL); 17402 } 17403 17404 } else { 17405 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17406 if ((probe = dtrace_probes[i - 1]) != NULL && 17407 dtrace_match_priv(probe, priv, uid, zoneid)) 17408 break; 17409 } 17410 } 17411 17412 if (probe == NULL) { 17413 mutex_exit(&dtrace_lock); 17414 return (ESRCH); 17415 } 17416 17417 dtrace_probe_description(probe, &desc); 17418 mutex_exit(&dtrace_lock); 17419 17420 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17421 return (EFAULT); 17422 17423 return (0); 17424 } 17425 17426 case DTRACEIOC_PROBEARG: { 17427 dtrace_argdesc_t desc; 17428 dtrace_probe_t *probe; 17429 dtrace_provider_t *prov; 17430 17431 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17432 return (EFAULT); 17433 17434 if (desc.dtargd_id == DTRACE_IDNONE) 17435 return (EINVAL); 17436 17437 if (desc.dtargd_ndx == DTRACE_ARGNONE) 17438 return (EINVAL); 17439 17440 mutex_enter(&dtrace_provider_lock); 17441 mutex_enter(&mod_lock); 17442 mutex_enter(&dtrace_lock); 17443 17444 if (desc.dtargd_id > dtrace_nprobes) { 17445 mutex_exit(&dtrace_lock); 17446 mutex_exit(&mod_lock); 17447 mutex_exit(&dtrace_provider_lock); 17448 return (EINVAL); 17449 } 17450 17451 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 17452 mutex_exit(&dtrace_lock); 17453 mutex_exit(&mod_lock); 17454 mutex_exit(&dtrace_provider_lock); 17455 return (EINVAL); 17456 } 17457 17458 mutex_exit(&dtrace_lock); 17459 17460 prov = probe->dtpr_provider; 17461 17462 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 17463 /* 17464 * There isn't any typed information for this probe. 17465 * Set the argument number to DTRACE_ARGNONE. 17466 */ 17467 desc.dtargd_ndx = DTRACE_ARGNONE; 17468 } else { 17469 desc.dtargd_native[0] = '\0'; 17470 desc.dtargd_xlate[0] = '\0'; 17471 desc.dtargd_mapping = desc.dtargd_ndx; 17472 17473 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 17474 probe->dtpr_id, probe->dtpr_arg, &desc); 17475 } 17476 17477 mutex_exit(&mod_lock); 17478 mutex_exit(&dtrace_provider_lock); 17479 17480 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17481 return (EFAULT); 17482 17483 return (0); 17484 } 17485 17486 case DTRACEIOC_GO: { 17487 processorid_t cpuid; 17488 rval = dtrace_state_go(state, &cpuid); 17489 17490 if (rval != 0) 17491 return (rval); 17492 17493 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17494 return (EFAULT); 17495 17496 return (0); 17497 } 17498 17499 case DTRACEIOC_STOP: { 17500 processorid_t cpuid; 17501 17502 mutex_enter(&dtrace_lock); 17503 rval = dtrace_state_stop(state, &cpuid); 17504 mutex_exit(&dtrace_lock); 17505 17506 if (rval != 0) 17507 return (rval); 17508 17509 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17510 return (EFAULT); 17511 17512 return (0); 17513 } 17514 17515 case DTRACEIOC_DOFGET: { 17516 dof_hdr_t hdr, *dof; 17517 uint64_t len; 17518 17519 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 17520 return (EFAULT); 17521 17522 mutex_enter(&dtrace_lock); 17523 dof = dtrace_dof_create(state); 17524 mutex_exit(&dtrace_lock); 17525 17526 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 17527 rval = copyout(dof, (void *)arg, len); 17528 dtrace_dof_destroy(dof); 17529 17530 return (rval == 0 ? 0 : EFAULT); 17531 } 17532 17533 case DTRACEIOC_AGGSNAP: 17534 case DTRACEIOC_BUFSNAP: { 17535 dtrace_bufdesc_t desc; 17536 caddr_t cached; 17537 dtrace_buffer_t *buf; 17538 17539 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17540 return (EFAULT); 17541 17542 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 17543 return (EINVAL); 17544 17545 mutex_enter(&dtrace_lock); 17546 17547 if (cmd == DTRACEIOC_BUFSNAP) { 17548 buf = &state->dts_buffer[desc.dtbd_cpu]; 17549 } else { 17550 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 17551 } 17552 17553 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 17554 size_t sz = buf->dtb_offset; 17555 17556 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 17557 mutex_exit(&dtrace_lock); 17558 return (EBUSY); 17559 } 17560 17561 /* 17562 * If this buffer has already been consumed, we're 17563 * going to indicate that there's nothing left here 17564 * to consume. 17565 */ 17566 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 17567 mutex_exit(&dtrace_lock); 17568 17569 desc.dtbd_size = 0; 17570 desc.dtbd_drops = 0; 17571 desc.dtbd_errors = 0; 17572 desc.dtbd_oldest = 0; 17573 sz = sizeof (desc); 17574 17575 if (copyout(&desc, (void *)arg, sz) != 0) 17576 return (EFAULT); 17577 17578 return (0); 17579 } 17580 17581 /* 17582 * If this is a ring buffer that has wrapped, we want 17583 * to copy the whole thing out. 17584 */ 17585 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 17586 dtrace_buffer_polish(buf); 17587 sz = buf->dtb_size; 17588 } 17589 17590 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 17591 mutex_exit(&dtrace_lock); 17592 return (EFAULT); 17593 } 17594 17595 desc.dtbd_size = sz; 17596 desc.dtbd_drops = buf->dtb_drops; 17597 desc.dtbd_errors = buf->dtb_errors; 17598 desc.dtbd_oldest = buf->dtb_xamot_offset; 17599 desc.dtbd_timestamp = dtrace_gethrtime(); 17600 17601 mutex_exit(&dtrace_lock); 17602 17603 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17604 return (EFAULT); 17605 17606 buf->dtb_flags |= DTRACEBUF_CONSUMED; 17607 17608 return (0); 17609 } 17610 17611 if (buf->dtb_tomax == NULL) { 17612 ASSERT(buf->dtb_xamot == NULL); 17613 mutex_exit(&dtrace_lock); 17614 return (ENOENT); 17615 } 17616 17617 cached = buf->dtb_tomax; 17618 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 17619 17620 dtrace_xcall(desc.dtbd_cpu, 17621 (dtrace_xcall_t)dtrace_buffer_switch, buf); 17622 17623 state->dts_errors += buf->dtb_xamot_errors; 17624 17625 /* 17626 * If the buffers did not actually switch, then the cross call 17627 * did not take place -- presumably because the given CPU is 17628 * not in the ready set. If this is the case, we'll return 17629 * ENOENT. 17630 */ 17631 if (buf->dtb_tomax == cached) { 17632 ASSERT(buf->dtb_xamot != cached); 17633 mutex_exit(&dtrace_lock); 17634 return (ENOENT); 17635 } 17636 17637 ASSERT(cached == buf->dtb_xamot); 17638 17639 /* 17640 * We have our snapshot; now copy it out. 17641 */ 17642 if (copyout(buf->dtb_xamot, desc.dtbd_data, 17643 buf->dtb_xamot_offset) != 0) { 17644 mutex_exit(&dtrace_lock); 17645 return (EFAULT); 17646 } 17647 17648 desc.dtbd_size = buf->dtb_xamot_offset; 17649 desc.dtbd_drops = buf->dtb_xamot_drops; 17650 desc.dtbd_errors = buf->dtb_xamot_errors; 17651 desc.dtbd_oldest = 0; 17652 desc.dtbd_timestamp = buf->dtb_switched; 17653 17654 mutex_exit(&dtrace_lock); 17655 17656 /* 17657 * Finally, copy out the buffer description. 17658 */ 17659 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17660 return (EFAULT); 17661 17662 return (0); 17663 } 17664 17665 case DTRACEIOC_CONF: { 17666 dtrace_conf_t conf; 17667 17668 bzero(&conf, sizeof (conf)); 17669 conf.dtc_difversion = DIF_VERSION; 17670 conf.dtc_difintregs = DIF_DIR_NREGS; 17671 conf.dtc_diftupregs = DIF_DTR_NREGS; 17672 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 17673 17674 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 17675 return (EFAULT); 17676 17677 return (0); 17678 } 17679 17680 case DTRACEIOC_STATUS: { 17681 dtrace_status_t stat; 17682 dtrace_dstate_t *dstate; 17683 int i, j; 17684 uint64_t nerrs; 17685 17686 /* 17687 * See the comment in dtrace_state_deadman() for the reason 17688 * for setting dts_laststatus to INT64_MAX before setting 17689 * it to the correct value. 17690 */ 17691 state->dts_laststatus = INT64_MAX; 17692 dtrace_membar_producer(); 17693 state->dts_laststatus = dtrace_gethrtime(); 17694 17695 bzero(&stat, sizeof (stat)); 17696 17697 mutex_enter(&dtrace_lock); 17698 17699 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 17700 mutex_exit(&dtrace_lock); 17701 return (ENOENT); 17702 } 17703 17704 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 17705 stat.dtst_exiting = 1; 17706 17707 nerrs = state->dts_errors; 17708 dstate = &state->dts_vstate.dtvs_dynvars; 17709 17710 for (i = 0; i < NCPU; i++) { 17711 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 17712 17713 stat.dtst_dyndrops += dcpu->dtdsc_drops; 17714 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 17715 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 17716 17717 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 17718 stat.dtst_filled++; 17719 17720 nerrs += state->dts_buffer[i].dtb_errors; 17721 17722 for (j = 0; j < state->dts_nspeculations; j++) { 17723 dtrace_speculation_t *spec; 17724 dtrace_buffer_t *buf; 17725 17726 spec = &state->dts_speculations[j]; 17727 buf = &spec->dtsp_buffer[i]; 17728 stat.dtst_specdrops += buf->dtb_xamot_drops; 17729 } 17730 } 17731 17732 stat.dtst_specdrops_busy = state->dts_speculations_busy; 17733 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 17734 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 17735 stat.dtst_dblerrors = state->dts_dblerrors; 17736 stat.dtst_killed = 17737 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 17738 stat.dtst_errors = nerrs; 17739 17740 mutex_exit(&dtrace_lock); 17741 17742 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 17743 return (EFAULT); 17744 17745 return (0); 17746 } 17747 17748 case DTRACEIOC_FORMAT: { 17749 dtrace_fmtdesc_t fmt; 17750 char *str; 17751 int len; 17752 17753 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 17754 return (EFAULT); 17755 17756 mutex_enter(&dtrace_lock); 17757 17758 if (fmt.dtfd_format == 0 || 17759 fmt.dtfd_format > state->dts_nformats) { 17760 mutex_exit(&dtrace_lock); 17761 return (EINVAL); 17762 } 17763 17764 /* 17765 * Format strings are allocated contiguously and they are 17766 * never freed; if a format index is less than the number 17767 * of formats, we can assert that the format map is non-NULL 17768 * and that the format for the specified index is non-NULL. 17769 */ 17770 ASSERT(state->dts_formats != NULL); 17771 str = state->dts_formats[fmt.dtfd_format - 1]; 17772 ASSERT(str != NULL); 17773 17774 len = strlen(str) + 1; 17775 17776 if (len > fmt.dtfd_length) { 17777 fmt.dtfd_length = len; 17778 17779 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 17780 mutex_exit(&dtrace_lock); 17781 return (EINVAL); 17782 } 17783 } else { 17784 if (copyout(str, fmt.dtfd_string, len) != 0) { 17785 mutex_exit(&dtrace_lock); 17786 return (EINVAL); 17787 } 17788 } 17789 17790 mutex_exit(&dtrace_lock); 17791 return (0); 17792 } 17793 17794 default: 17795 break; 17796 } 17797 17798 return (ENOTTY); 17799} 17800 17801/*ARGSUSED*/ 17802static int 17803dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 17804{ 17805 dtrace_state_t *state; 17806 17807 switch (cmd) { 17808 case DDI_DETACH: 17809 break; 17810 17811 case DDI_SUSPEND: 17812 return (DDI_SUCCESS); 17813 17814 default: 17815 return (DDI_FAILURE); 17816 } 17817 17818 mutex_enter(&cpu_lock); 17819 mutex_enter(&dtrace_provider_lock); 17820 mutex_enter(&dtrace_lock); 17821 17822 ASSERT(dtrace_opens == 0); 17823 17824 if (dtrace_helpers > 0) { 17825 mutex_exit(&dtrace_provider_lock); 17826 mutex_exit(&dtrace_lock); 17827 mutex_exit(&cpu_lock); 17828 return (DDI_FAILURE); 17829 } 17830 17831 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 17832 mutex_exit(&dtrace_provider_lock); 17833 mutex_exit(&dtrace_lock); 17834 mutex_exit(&cpu_lock); 17835 return (DDI_FAILURE); 17836 } 17837 17838 dtrace_provider = NULL; 17839 17840 if ((state = dtrace_anon_grab()) != NULL) { 17841 /* 17842 * If there were ECBs on this state, the provider should 17843 * have not been allowed to detach; assert that there is 17844 * none. 17845 */ 17846 ASSERT(state->dts_necbs == 0); 17847 dtrace_state_destroy(state); 17848 17849 /* 17850 * If we're being detached with anonymous state, we need to 17851 * indicate to the kernel debugger that DTrace is now inactive. 17852 */ 17853 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17854 } 17855 17856 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 17857 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 17858 dtrace_cpu_init = NULL; 17859 dtrace_helpers_cleanup = NULL; 17860 dtrace_helpers_fork = NULL; 17861 dtrace_cpustart_init = NULL; 17862 dtrace_cpustart_fini = NULL; 17863 dtrace_debugger_init = NULL; 17864 dtrace_debugger_fini = NULL; 17865 dtrace_modload = NULL; 17866 dtrace_modunload = NULL; 17867 17868 ASSERT(dtrace_getf == 0); 17869 ASSERT(dtrace_closef == NULL); 17870 17871 mutex_exit(&cpu_lock); 17872 17873 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 17874 dtrace_probes = NULL; 17875 dtrace_nprobes = 0; 17876 17877 dtrace_hash_destroy(dtrace_bymod); 17878 dtrace_hash_destroy(dtrace_byfunc); 17879 dtrace_hash_destroy(dtrace_byname); 17880 dtrace_bymod = NULL; 17881 dtrace_byfunc = NULL; 17882 dtrace_byname = NULL; 17883 17884 kmem_cache_destroy(dtrace_state_cache); 17885 vmem_destroy(dtrace_minor); 17886 vmem_destroy(dtrace_arena); 17887 17888 if (dtrace_toxrange != NULL) { 17889 kmem_free(dtrace_toxrange, 17890 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 17891 dtrace_toxrange = NULL; 17892 dtrace_toxranges = 0; 17893 dtrace_toxranges_max = 0; 17894 } 17895 17896 ddi_remove_minor_node(dtrace_devi, NULL); 17897 dtrace_devi = NULL; 17898 17899 ddi_soft_state_fini(&dtrace_softstate); 17900 17901 ASSERT(dtrace_vtime_references == 0); 17902 ASSERT(dtrace_opens == 0); 17903 ASSERT(dtrace_retained == NULL); 17904 17905 mutex_exit(&dtrace_lock); 17906 mutex_exit(&dtrace_provider_lock); 17907 17908 /* 17909 * We don't destroy the task queue until after we have dropped our 17910 * locks (taskq_destroy() may block on running tasks). To prevent 17911 * attempting to do work after we have effectively detached but before 17912 * the task queue has been destroyed, all tasks dispatched via the 17913 * task queue must check that DTrace is still attached before 17914 * performing any operation. 17915 */ 17916 taskq_destroy(dtrace_taskq); 17917 dtrace_taskq = NULL; 17918 17919 return (DDI_SUCCESS); 17920} 17921#endif 17922 17923#ifdef illumos 17924/*ARGSUSED*/ 17925static int 17926dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 17927{ 17928 int error; 17929 17930 switch (infocmd) { 17931 case DDI_INFO_DEVT2DEVINFO: 17932 *result = (void *)dtrace_devi; 17933 error = DDI_SUCCESS; 17934 break; 17935 case DDI_INFO_DEVT2INSTANCE: 17936 *result = (void *)0; 17937 error = DDI_SUCCESS; 17938 break; 17939 default: 17940 error = DDI_FAILURE; 17941 } 17942 return (error); 17943} 17944#endif 17945 17946#ifdef illumos 17947static struct cb_ops dtrace_cb_ops = { 17948 dtrace_open, /* open */ 17949 dtrace_close, /* close */ 17950 nulldev, /* strategy */ 17951 nulldev, /* print */ 17952 nodev, /* dump */ 17953 nodev, /* read */ 17954 nodev, /* write */ 17955 dtrace_ioctl, /* ioctl */ 17956 nodev, /* devmap */ 17957 nodev, /* mmap */ 17958 nodev, /* segmap */ 17959 nochpoll, /* poll */ 17960 ddi_prop_op, /* cb_prop_op */ 17961 0, /* streamtab */ 17962 D_NEW | D_MP /* Driver compatibility flag */ 17963}; 17964 17965static struct dev_ops dtrace_ops = { 17966 DEVO_REV, /* devo_rev */ 17967 0, /* refcnt */ 17968 dtrace_info, /* get_dev_info */ 17969 nulldev, /* identify */ 17970 nulldev, /* probe */ 17971 dtrace_attach, /* attach */ 17972 dtrace_detach, /* detach */ 17973 nodev, /* reset */ 17974 &dtrace_cb_ops, /* driver operations */ 17975 NULL, /* bus operations */ 17976 nodev /* dev power */ 17977}; 17978 17979static struct modldrv modldrv = { 17980 &mod_driverops, /* module type (this is a pseudo driver) */ 17981 "Dynamic Tracing", /* name of module */ 17982 &dtrace_ops, /* driver ops */ 17983}; 17984 17985static struct modlinkage modlinkage = { 17986 MODREV_1, 17987 (void *)&modldrv, 17988 NULL 17989}; 17990 17991int 17992_init(void) 17993{ 17994 return (mod_install(&modlinkage)); 17995} 17996 17997int 17998_info(struct modinfo *modinfop) 17999{ 18000 return (mod_info(&modlinkage, modinfop)); 18001} 18002 18003int 18004_fini(void) 18005{ 18006 return (mod_remove(&modlinkage)); 18007} 18008#else 18009 18010static d_ioctl_t dtrace_ioctl; 18011static d_ioctl_t dtrace_ioctl_helper; 18012static void dtrace_load(void *); 18013static int dtrace_unload(void); 18014static struct cdev *dtrace_dev; 18015static struct cdev *helper_dev; 18016 18017void dtrace_invop_init(void); 18018void dtrace_invop_uninit(void); 18019 18020static struct cdevsw dtrace_cdevsw = { 18021 .d_version = D_VERSION, 18022 .d_ioctl = dtrace_ioctl, 18023 .d_open = dtrace_open, 18024 .d_name = "dtrace", 18025}; 18026 18027static struct cdevsw helper_cdevsw = { 18028 .d_version = D_VERSION, 18029 .d_ioctl = dtrace_ioctl_helper, 18030 .d_name = "helper", 18031}; 18032 18033#include <dtrace_anon.c> 18034#include <dtrace_ioctl.c> 18035#include <dtrace_load.c> 18036#include <dtrace_modevent.c> 18037#include <dtrace_sysctl.c> 18038#include <dtrace_unload.c> 18039#include <dtrace_vtime.c> 18040#include <dtrace_hacks.c> 18041#include <dtrace_isa.c> 18042 18043SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 18044SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 18045SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 18046 18047DEV_MODULE(dtrace, dtrace_modevent, NULL); 18048MODULE_VERSION(dtrace, 1); 18049MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 18050#endif 18051