dtrace.c revision 266667
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 266667 2014-05-25 18:19:57Z markj $ 22 */ 23 24/* 25 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2012 by Delphix. All rights reserved. 28 */ 29 30#pragma ident "%Z%%M% %I% %E% SMI" 31 32/* 33 * DTrace - Dynamic Tracing for Solaris 34 * 35 * This is the implementation of the Solaris Dynamic Tracing framework 36 * (DTrace). The user-visible interface to DTrace is described at length in 37 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 38 * library, the in-kernel DTrace framework, and the DTrace providers are 39 * described in the block comments in the <sys/dtrace.h> header file. The 40 * internal architecture of DTrace is described in the block comments in the 41 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 42 * implementation very much assume mastery of all of these sources; if one has 43 * an unanswered question about the implementation, one should consult them 44 * first. 45 * 46 * The functions here are ordered roughly as follows: 47 * 48 * - Probe context functions 49 * - Probe hashing functions 50 * - Non-probe context utility functions 51 * - Matching functions 52 * - Provider-to-Framework API functions 53 * - Probe management functions 54 * - DIF object functions 55 * - Format functions 56 * - Predicate functions 57 * - ECB functions 58 * - Buffer functions 59 * - Enabling functions 60 * - DOF functions 61 * - Anonymous enabling functions 62 * - Consumer state functions 63 * - Helper functions 64 * - Hook functions 65 * - Driver cookbook functions 66 * 67 * Each group of functions begins with a block comment labelled the "DTrace 68 * [Group] Functions", allowing one to find each block by searching forward 69 * on capital-f functions. 70 */ 71#include <sys/errno.h> 72#if !defined(sun) 73#include <sys/time.h> 74#endif 75#include <sys/stat.h> 76#include <sys/modctl.h> 77#include <sys/conf.h> 78#include <sys/systm.h> 79#if defined(sun) 80#include <sys/ddi.h> 81#include <sys/sunddi.h> 82#endif 83#include <sys/cpuvar.h> 84#include <sys/kmem.h> 85#if defined(sun) 86#include <sys/strsubr.h> 87#endif 88#include <sys/sysmacros.h> 89#include <sys/dtrace_impl.h> 90#include <sys/atomic.h> 91#include <sys/cmn_err.h> 92#if defined(sun) 93#include <sys/mutex_impl.h> 94#include <sys/rwlock_impl.h> 95#endif 96#include <sys/ctf_api.h> 97#if defined(sun) 98#include <sys/panic.h> 99#include <sys/priv_impl.h> 100#endif 101#include <sys/policy.h> 102#if defined(sun) 103#include <sys/cred_impl.h> 104#include <sys/procfs_isa.h> 105#endif 106#include <sys/taskq.h> 107#if defined(sun) 108#include <sys/mkdev.h> 109#include <sys/kdi.h> 110#endif 111#include <sys/zone.h> 112#include <sys/socket.h> 113#include <netinet/in.h> 114 115/* FreeBSD includes: */ 116#if !defined(sun) 117#include <sys/callout.h> 118#include <sys/ctype.h> 119#include <sys/eventhandler.h> 120#include <sys/limits.h> 121#include <sys/kdb.h> 122#include <sys/kernel.h> 123#include <sys/malloc.h> 124#include <sys/sysctl.h> 125#include <sys/lock.h> 126#include <sys/mutex.h> 127#include <sys/rwlock.h> 128#include <sys/sx.h> 129#include <sys/dtrace_bsd.h> 130#include <netinet/in.h> 131#include "dtrace_cddl.h" 132#include "dtrace_debug.c" 133#endif 134 135/* 136 * DTrace Tunable Variables 137 * 138 * The following variables may be tuned by adding a line to /etc/system that 139 * includes both the name of the DTrace module ("dtrace") and the name of the 140 * variable. For example: 141 * 142 * set dtrace:dtrace_destructive_disallow = 1 143 * 144 * In general, the only variables that one should be tuning this way are those 145 * that affect system-wide DTrace behavior, and for which the default behavior 146 * is undesirable. Most of these variables are tunable on a per-consumer 147 * basis using DTrace options, and need not be tuned on a system-wide basis. 148 * When tuning these variables, avoid pathological values; while some attempt 149 * is made to verify the integrity of these variables, they are not considered 150 * part of the supported interface to DTrace, and they are therefore not 151 * checked comprehensively. Further, these variables should not be tuned 152 * dynamically via "mdb -kw" or other means; they should only be tuned via 153 * /etc/system. 154 */ 155int dtrace_destructive_disallow = 0; 156dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 157size_t dtrace_difo_maxsize = (256 * 1024); 158dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 159size_t dtrace_global_maxsize = (16 * 1024); 160size_t dtrace_actions_max = (16 * 1024); 161size_t dtrace_retain_max = 1024; 162dtrace_optval_t dtrace_helper_actions_max = 128; 163dtrace_optval_t dtrace_helper_providers_max = 32; 164dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 165size_t dtrace_strsize_default = 256; 166dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 167dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 168dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 169dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 170dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 171dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 172dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 173dtrace_optval_t dtrace_nspec_default = 1; 174dtrace_optval_t dtrace_specsize_default = 32 * 1024; 175dtrace_optval_t dtrace_stackframes_default = 20; 176dtrace_optval_t dtrace_ustackframes_default = 20; 177dtrace_optval_t dtrace_jstackframes_default = 50; 178dtrace_optval_t dtrace_jstackstrsize_default = 512; 179int dtrace_msgdsize_max = 128; 180hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 181hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 182int dtrace_devdepth_max = 32; 183int dtrace_err_verbose; 184hrtime_t dtrace_deadman_interval = NANOSEC; 185hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 186hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 187hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 188 189/* 190 * DTrace External Variables 191 * 192 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 193 * available to DTrace consumers via the backtick (`) syntax. One of these, 194 * dtrace_zero, is made deliberately so: it is provided as a source of 195 * well-known, zero-filled memory. While this variable is not documented, 196 * it is used by some translators as an implementation detail. 197 */ 198const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 199 200/* 201 * DTrace Internal Variables 202 */ 203#if defined(sun) 204static dev_info_t *dtrace_devi; /* device info */ 205#endif 206#if defined(sun) 207static vmem_t *dtrace_arena; /* probe ID arena */ 208static vmem_t *dtrace_minor; /* minor number arena */ 209#else 210static taskq_t *dtrace_taskq; /* task queue */ 211static struct unrhdr *dtrace_arena; /* Probe ID number. */ 212#endif 213static dtrace_probe_t **dtrace_probes; /* array of all probes */ 214static int dtrace_nprobes; /* number of probes */ 215static dtrace_provider_t *dtrace_provider; /* provider list */ 216static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 217static int dtrace_opens; /* number of opens */ 218static int dtrace_helpers; /* number of helpers */ 219#if defined(sun) 220static void *dtrace_softstate; /* softstate pointer */ 221#endif 222static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 223static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 224static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 225static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 226static int dtrace_toxranges; /* number of toxic ranges */ 227static int dtrace_toxranges_max; /* size of toxic range array */ 228static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 229static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 230static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 231static kthread_t *dtrace_panicked; /* panicking thread */ 232static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 233static dtrace_genid_t dtrace_probegen; /* current probe generation */ 234static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 235static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 236static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 237#if !defined(sun) 238static struct mtx dtrace_unr_mtx; 239MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 240int dtrace_in_probe; /* non-zero if executing a probe */ 241#if defined(__i386__) || defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 242uintptr_t dtrace_in_probe_addr; /* Address of invop when already in probe */ 243#endif 244static eventhandler_tag dtrace_kld_load_tag; 245static eventhandler_tag dtrace_kld_unload_try_tag; 246#endif 247 248/* 249 * DTrace Locking 250 * DTrace is protected by three (relatively coarse-grained) locks: 251 * 252 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 253 * including enabling state, probes, ECBs, consumer state, helper state, 254 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 255 * probe context is lock-free -- synchronization is handled via the 256 * dtrace_sync() cross call mechanism. 257 * 258 * (2) dtrace_provider_lock is required when manipulating provider state, or 259 * when provider state must be held constant. 260 * 261 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 262 * when meta provider state must be held constant. 263 * 264 * The lock ordering between these three locks is dtrace_meta_lock before 265 * dtrace_provider_lock before dtrace_lock. (In particular, there are 266 * several places where dtrace_provider_lock is held by the framework as it 267 * calls into the providers -- which then call back into the framework, 268 * grabbing dtrace_lock.) 269 * 270 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 271 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 272 * role as a coarse-grained lock; it is acquired before both of these locks. 273 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 274 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 275 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 276 * acquired _between_ dtrace_provider_lock and dtrace_lock. 277 */ 278static kmutex_t dtrace_lock; /* probe state lock */ 279static kmutex_t dtrace_provider_lock; /* provider state lock */ 280static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 281 282#if !defined(sun) 283/* XXX FreeBSD hacks. */ 284#define cr_suid cr_svuid 285#define cr_sgid cr_svgid 286#define ipaddr_t in_addr_t 287#define mod_modname pathname 288#define vuprintf vprintf 289#define ttoproc(_a) ((_a)->td_proc) 290#define crgetzoneid(_a) 0 291#define NCPU MAXCPU 292#define SNOCD 0 293#define CPU_ON_INTR(_a) 0 294 295#define PRIV_EFFECTIVE (1 << 0) 296#define PRIV_DTRACE_KERNEL (1 << 1) 297#define PRIV_DTRACE_PROC (1 << 2) 298#define PRIV_DTRACE_USER (1 << 3) 299#define PRIV_PROC_OWNER (1 << 4) 300#define PRIV_PROC_ZONE (1 << 5) 301#define PRIV_ALL ~0 302 303SYSCTL_DECL(_debug_dtrace); 304SYSCTL_DECL(_kern_dtrace); 305#endif 306 307#if defined(sun) 308#define curcpu CPU->cpu_id 309#endif 310 311 312/* 313 * DTrace Provider Variables 314 * 315 * These are the variables relating to DTrace as a provider (that is, the 316 * provider of the BEGIN, END, and ERROR probes). 317 */ 318static dtrace_pattr_t dtrace_provider_attr = { 319{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 320{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 321{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 322{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 323{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 324}; 325 326static void 327dtrace_nullop(void) 328{} 329 330static dtrace_pops_t dtrace_provider_ops = { 331 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 332 (void (*)(void *, modctl_t *))dtrace_nullop, 333 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 334 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 335 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 336 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 337 NULL, 338 NULL, 339 NULL, 340 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 341}; 342 343static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 344static dtrace_id_t dtrace_probeid_end; /* special END probe */ 345dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 346 347/* 348 * DTrace Helper Tracing Variables 349 */ 350uint32_t dtrace_helptrace_next = 0; 351uint32_t dtrace_helptrace_nlocals; 352char *dtrace_helptrace_buffer; 353int dtrace_helptrace_bufsize = 512 * 1024; 354 355#ifdef DEBUG 356int dtrace_helptrace_enabled = 1; 357#else 358int dtrace_helptrace_enabled = 0; 359#endif 360 361/* 362 * DTrace Error Hashing 363 * 364 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 365 * table. This is very useful for checking coverage of tests that are 366 * expected to induce DIF or DOF processing errors, and may be useful for 367 * debugging problems in the DIF code generator or in DOF generation . The 368 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 369 */ 370#ifdef DEBUG 371static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 372static const char *dtrace_errlast; 373static kthread_t *dtrace_errthread; 374static kmutex_t dtrace_errlock; 375#endif 376 377/* 378 * DTrace Macros and Constants 379 * 380 * These are various macros that are useful in various spots in the 381 * implementation, along with a few random constants that have no meaning 382 * outside of the implementation. There is no real structure to this cpp 383 * mishmash -- but is there ever? 384 */ 385#define DTRACE_HASHSTR(hash, probe) \ 386 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 387 388#define DTRACE_HASHNEXT(hash, probe) \ 389 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 390 391#define DTRACE_HASHPREV(hash, probe) \ 392 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 393 394#define DTRACE_HASHEQ(hash, lhs, rhs) \ 395 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 396 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 397 398#define DTRACE_AGGHASHSIZE_SLEW 17 399 400#define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 401 402/* 403 * The key for a thread-local variable consists of the lower 61 bits of the 404 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 405 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 406 * equal to a variable identifier. This is necessary (but not sufficient) to 407 * assure that global associative arrays never collide with thread-local 408 * variables. To guarantee that they cannot collide, we must also define the 409 * order for keying dynamic variables. That order is: 410 * 411 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 412 * 413 * Because the variable-key and the tls-key are in orthogonal spaces, there is 414 * no way for a global variable key signature to match a thread-local key 415 * signature. 416 */ 417#if defined(sun) 418#define DTRACE_TLS_THRKEY(where) { \ 419 uint_t intr = 0; \ 420 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 421 for (; actv; actv >>= 1) \ 422 intr++; \ 423 ASSERT(intr < (1 << 3)); \ 424 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 425 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 426} 427#else 428#define DTRACE_TLS_THRKEY(where) { \ 429 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 430 uint_t intr = 0; \ 431 uint_t actv = _c->cpu_intr_actv; \ 432 for (; actv; actv >>= 1) \ 433 intr++; \ 434 ASSERT(intr < (1 << 3)); \ 435 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 436 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 437} 438#endif 439 440#define DT_BSWAP_8(x) ((x) & 0xff) 441#define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 442#define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 443#define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 444 445#define DT_MASK_LO 0x00000000FFFFFFFFULL 446 447#define DTRACE_STORE(type, tomax, offset, what) \ 448 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 449 450#ifndef __x86 451#define DTRACE_ALIGNCHECK(addr, size, flags) \ 452 if (addr & (size - 1)) { \ 453 *flags |= CPU_DTRACE_BADALIGN; \ 454 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 455 return (0); \ 456 } 457#else 458#define DTRACE_ALIGNCHECK(addr, size, flags) 459#endif 460 461/* 462 * Test whether a range of memory starting at testaddr of size testsz falls 463 * within the range of memory described by addr, sz. We take care to avoid 464 * problems with overflow and underflow of the unsigned quantities, and 465 * disallow all negative sizes. Ranges of size 0 are allowed. 466 */ 467#define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 468 ((testaddr) - (baseaddr) < (basesz) && \ 469 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \ 470 (testaddr) + (testsz) >= (testaddr)) 471 472/* 473 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 474 * alloc_sz on the righthand side of the comparison in order to avoid overflow 475 * or underflow in the comparison with it. This is simpler than the INRANGE 476 * check above, because we know that the dtms_scratch_ptr is valid in the 477 * range. Allocations of size zero are allowed. 478 */ 479#define DTRACE_INSCRATCH(mstate, alloc_sz) \ 480 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 481 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 482 483#define DTRACE_LOADFUNC(bits) \ 484/*CSTYLED*/ \ 485uint##bits##_t \ 486dtrace_load##bits(uintptr_t addr) \ 487{ \ 488 size_t size = bits / NBBY; \ 489 /*CSTYLED*/ \ 490 uint##bits##_t rval; \ 491 int i; \ 492 volatile uint16_t *flags = (volatile uint16_t *) \ 493 &cpu_core[curcpu].cpuc_dtrace_flags; \ 494 \ 495 DTRACE_ALIGNCHECK(addr, size, flags); \ 496 \ 497 for (i = 0; i < dtrace_toxranges; i++) { \ 498 if (addr >= dtrace_toxrange[i].dtt_limit) \ 499 continue; \ 500 \ 501 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 502 continue; \ 503 \ 504 /* \ 505 * This address falls within a toxic region; return 0. \ 506 */ \ 507 *flags |= CPU_DTRACE_BADADDR; \ 508 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 509 return (0); \ 510 } \ 511 \ 512 *flags |= CPU_DTRACE_NOFAULT; \ 513 /*CSTYLED*/ \ 514 rval = *((volatile uint##bits##_t *)addr); \ 515 *flags &= ~CPU_DTRACE_NOFAULT; \ 516 \ 517 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 518} 519 520#ifdef _LP64 521#define dtrace_loadptr dtrace_load64 522#else 523#define dtrace_loadptr dtrace_load32 524#endif 525 526#define DTRACE_DYNHASH_FREE 0 527#define DTRACE_DYNHASH_SINK 1 528#define DTRACE_DYNHASH_VALID 2 529 530#define DTRACE_MATCH_NEXT 0 531#define DTRACE_MATCH_DONE 1 532#define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 533#define DTRACE_STATE_ALIGN 64 534 535#define DTRACE_FLAGS2FLT(flags) \ 536 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 537 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 538 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 539 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 540 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 541 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 542 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 543 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 544 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 545 DTRACEFLT_UNKNOWN) 546 547#define DTRACEACT_ISSTRING(act) \ 548 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 549 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 550 551/* Function prototype definitions: */ 552static size_t dtrace_strlen(const char *, size_t); 553static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 554static void dtrace_enabling_provide(dtrace_provider_t *); 555static int dtrace_enabling_match(dtrace_enabling_t *, int *); 556static void dtrace_enabling_matchall(void); 557static void dtrace_enabling_reap(void); 558static dtrace_state_t *dtrace_anon_grab(void); 559static uint64_t dtrace_helper(int, dtrace_mstate_t *, 560 dtrace_state_t *, uint64_t, uint64_t); 561static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 562static void dtrace_buffer_drop(dtrace_buffer_t *); 563static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 564static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 565 dtrace_state_t *, dtrace_mstate_t *); 566static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 567 dtrace_optval_t); 568static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 569static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 570uint16_t dtrace_load16(uintptr_t); 571uint32_t dtrace_load32(uintptr_t); 572uint64_t dtrace_load64(uintptr_t); 573uint8_t dtrace_load8(uintptr_t); 574void dtrace_dynvar_clean(dtrace_dstate_t *); 575dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 576 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 577uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 578 579/* 580 * DTrace Probe Context Functions 581 * 582 * These functions are called from probe context. Because probe context is 583 * any context in which C may be called, arbitrarily locks may be held, 584 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 585 * As a result, functions called from probe context may only call other DTrace 586 * support functions -- they may not interact at all with the system at large. 587 * (Note that the ASSERT macro is made probe-context safe by redefining it in 588 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 589 * loads are to be performed from probe context, they _must_ be in terms of 590 * the safe dtrace_load*() variants. 591 * 592 * Some functions in this block are not actually called from probe context; 593 * for these functions, there will be a comment above the function reading 594 * "Note: not called from probe context." 595 */ 596void 597dtrace_panic(const char *format, ...) 598{ 599 va_list alist; 600 601 va_start(alist, format); 602 dtrace_vpanic(format, alist); 603 va_end(alist); 604} 605 606int 607dtrace_assfail(const char *a, const char *f, int l) 608{ 609 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 610 611 /* 612 * We just need something here that even the most clever compiler 613 * cannot optimize away. 614 */ 615 return (a[(uintptr_t)f]); 616} 617 618/* 619 * Atomically increment a specified error counter from probe context. 620 */ 621static void 622dtrace_error(uint32_t *counter) 623{ 624 /* 625 * Most counters stored to in probe context are per-CPU counters. 626 * However, there are some error conditions that are sufficiently 627 * arcane that they don't merit per-CPU storage. If these counters 628 * are incremented concurrently on different CPUs, scalability will be 629 * adversely affected -- but we don't expect them to be white-hot in a 630 * correctly constructed enabling... 631 */ 632 uint32_t oval, nval; 633 634 do { 635 oval = *counter; 636 637 if ((nval = oval + 1) == 0) { 638 /* 639 * If the counter would wrap, set it to 1 -- assuring 640 * that the counter is never zero when we have seen 641 * errors. (The counter must be 32-bits because we 642 * aren't guaranteed a 64-bit compare&swap operation.) 643 * To save this code both the infamy of being fingered 644 * by a priggish news story and the indignity of being 645 * the target of a neo-puritan witch trial, we're 646 * carefully avoiding any colorful description of the 647 * likelihood of this condition -- but suffice it to 648 * say that it is only slightly more likely than the 649 * overflow of predicate cache IDs, as discussed in 650 * dtrace_predicate_create(). 651 */ 652 nval = 1; 653 } 654 } while (dtrace_cas32(counter, oval, nval) != oval); 655} 656 657/* 658 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 659 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 660 */ 661DTRACE_LOADFUNC(8) 662DTRACE_LOADFUNC(16) 663DTRACE_LOADFUNC(32) 664DTRACE_LOADFUNC(64) 665 666static int 667dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 668{ 669 if (dest < mstate->dtms_scratch_base) 670 return (0); 671 672 if (dest + size < dest) 673 return (0); 674 675 if (dest + size > mstate->dtms_scratch_ptr) 676 return (0); 677 678 return (1); 679} 680 681static int 682dtrace_canstore_statvar(uint64_t addr, size_t sz, 683 dtrace_statvar_t **svars, int nsvars) 684{ 685 int i; 686 687 for (i = 0; i < nsvars; i++) { 688 dtrace_statvar_t *svar = svars[i]; 689 690 if (svar == NULL || svar->dtsv_size == 0) 691 continue; 692 693 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 694 return (1); 695 } 696 697 return (0); 698} 699 700/* 701 * Check to see if the address is within a memory region to which a store may 702 * be issued. This includes the DTrace scratch areas, and any DTrace variable 703 * region. The caller of dtrace_canstore() is responsible for performing any 704 * alignment checks that are needed before stores are actually executed. 705 */ 706static int 707dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 708 dtrace_vstate_t *vstate) 709{ 710 /* 711 * First, check to see if the address is in scratch space... 712 */ 713 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 714 mstate->dtms_scratch_size)) 715 return (1); 716 717 /* 718 * Now check to see if it's a dynamic variable. This check will pick 719 * up both thread-local variables and any global dynamically-allocated 720 * variables. 721 */ 722 if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base, 723 vstate->dtvs_dynvars.dtds_size)) { 724 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 725 uintptr_t base = (uintptr_t)dstate->dtds_base + 726 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 727 uintptr_t chunkoffs; 728 729 /* 730 * Before we assume that we can store here, we need to make 731 * sure that it isn't in our metadata -- storing to our 732 * dynamic variable metadata would corrupt our state. For 733 * the range to not include any dynamic variable metadata, 734 * it must: 735 * 736 * (1) Start above the hash table that is at the base of 737 * the dynamic variable space 738 * 739 * (2) Have a starting chunk offset that is beyond the 740 * dtrace_dynvar_t that is at the base of every chunk 741 * 742 * (3) Not span a chunk boundary 743 * 744 */ 745 if (addr < base) 746 return (0); 747 748 chunkoffs = (addr - base) % dstate->dtds_chunksize; 749 750 if (chunkoffs < sizeof (dtrace_dynvar_t)) 751 return (0); 752 753 if (chunkoffs + sz > dstate->dtds_chunksize) 754 return (0); 755 756 return (1); 757 } 758 759 /* 760 * Finally, check the static local and global variables. These checks 761 * take the longest, so we perform them last. 762 */ 763 if (dtrace_canstore_statvar(addr, sz, 764 vstate->dtvs_locals, vstate->dtvs_nlocals)) 765 return (1); 766 767 if (dtrace_canstore_statvar(addr, sz, 768 vstate->dtvs_globals, vstate->dtvs_nglobals)) 769 return (1); 770 771 return (0); 772} 773 774 775/* 776 * Convenience routine to check to see if the address is within a memory 777 * region in which a load may be issued given the user's privilege level; 778 * if not, it sets the appropriate error flags and loads 'addr' into the 779 * illegal value slot. 780 * 781 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 782 * appropriate memory access protection. 783 */ 784static int 785dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 786 dtrace_vstate_t *vstate) 787{ 788 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 789 790 /* 791 * If we hold the privilege to read from kernel memory, then 792 * everything is readable. 793 */ 794 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 795 return (1); 796 797 /* 798 * You can obviously read that which you can store. 799 */ 800 if (dtrace_canstore(addr, sz, mstate, vstate)) 801 return (1); 802 803 /* 804 * We're allowed to read from our own string table. 805 */ 806 if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab, 807 mstate->dtms_difo->dtdo_strlen)) 808 return (1); 809 810 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 811 *illval = addr; 812 return (0); 813} 814 815/* 816 * Convenience routine to check to see if a given string is within a memory 817 * region in which a load may be issued given the user's privilege level; 818 * this exists so that we don't need to issue unnecessary dtrace_strlen() 819 * calls in the event that the user has all privileges. 820 */ 821static int 822dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 823 dtrace_vstate_t *vstate) 824{ 825 size_t strsz; 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 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 835 if (dtrace_canload(addr, strsz, mstate, vstate)) 836 return (1); 837 838 return (0); 839} 840 841/* 842 * Convenience routine to check to see if a given variable is within a memory 843 * region in which a load may be issued given the user's privilege level. 844 */ 845static int 846dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 847 dtrace_vstate_t *vstate) 848{ 849 size_t sz; 850 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 851 852 /* 853 * If we hold the privilege to read from kernel memory, then 854 * everything is readable. 855 */ 856 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 857 return (1); 858 859 if (type->dtdt_kind == DIF_TYPE_STRING) 860 sz = dtrace_strlen(src, 861 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 862 else 863 sz = type->dtdt_size; 864 865 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 866} 867 868/* 869 * Compare two strings using safe loads. 870 */ 871static int 872dtrace_strncmp(char *s1, char *s2, size_t limit) 873{ 874 uint8_t c1, c2; 875 volatile uint16_t *flags; 876 877 if (s1 == s2 || limit == 0) 878 return (0); 879 880 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 881 882 do { 883 if (s1 == NULL) { 884 c1 = '\0'; 885 } else { 886 c1 = dtrace_load8((uintptr_t)s1++); 887 } 888 889 if (s2 == NULL) { 890 c2 = '\0'; 891 } else { 892 c2 = dtrace_load8((uintptr_t)s2++); 893 } 894 895 if (c1 != c2) 896 return (c1 - c2); 897 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 898 899 return (0); 900} 901 902/* 903 * Compute strlen(s) for a string using safe memory accesses. The additional 904 * len parameter is used to specify a maximum length to ensure completion. 905 */ 906static size_t 907dtrace_strlen(const char *s, size_t lim) 908{ 909 uint_t len; 910 911 for (len = 0; len != lim; len++) { 912 if (dtrace_load8((uintptr_t)s++) == '\0') 913 break; 914 } 915 916 return (len); 917} 918 919/* 920 * Check if an address falls within a toxic region. 921 */ 922static int 923dtrace_istoxic(uintptr_t kaddr, size_t size) 924{ 925 uintptr_t taddr, tsize; 926 int i; 927 928 for (i = 0; i < dtrace_toxranges; i++) { 929 taddr = dtrace_toxrange[i].dtt_base; 930 tsize = dtrace_toxrange[i].dtt_limit - taddr; 931 932 if (kaddr - taddr < tsize) { 933 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 934 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 935 return (1); 936 } 937 938 if (taddr - kaddr < size) { 939 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 940 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 941 return (1); 942 } 943 } 944 945 return (0); 946} 947 948/* 949 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 950 * memory specified by the DIF program. The dst is assumed to be safe memory 951 * that we can store to directly because it is managed by DTrace. As with 952 * standard bcopy, overlapping copies are handled properly. 953 */ 954static void 955dtrace_bcopy(const void *src, void *dst, size_t len) 956{ 957 if (len != 0) { 958 uint8_t *s1 = dst; 959 const uint8_t *s2 = src; 960 961 if (s1 <= s2) { 962 do { 963 *s1++ = dtrace_load8((uintptr_t)s2++); 964 } while (--len != 0); 965 } else { 966 s2 += len; 967 s1 += len; 968 969 do { 970 *--s1 = dtrace_load8((uintptr_t)--s2); 971 } while (--len != 0); 972 } 973 } 974} 975 976/* 977 * Copy src to dst using safe memory accesses, up to either the specified 978 * length, or the point that a nul byte is encountered. The src is assumed to 979 * be unsafe memory specified by the DIF program. The dst is assumed to be 980 * safe memory that we can store to directly because it is managed by DTrace. 981 * Unlike dtrace_bcopy(), overlapping regions are not handled. 982 */ 983static void 984dtrace_strcpy(const void *src, void *dst, size_t len) 985{ 986 if (len != 0) { 987 uint8_t *s1 = dst, c; 988 const uint8_t *s2 = src; 989 990 do { 991 *s1++ = c = dtrace_load8((uintptr_t)s2++); 992 } while (--len != 0 && c != '\0'); 993 } 994} 995 996/* 997 * Copy src to dst, deriving the size and type from the specified (BYREF) 998 * variable type. The src is assumed to be unsafe memory specified by the DIF 999 * program. The dst is assumed to be DTrace variable memory that is of the 1000 * specified type; we assume that we can store to directly. 1001 */ 1002static void 1003dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 1004{ 1005 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1006 1007 if (type->dtdt_kind == DIF_TYPE_STRING) { 1008 dtrace_strcpy(src, dst, type->dtdt_size); 1009 } else { 1010 dtrace_bcopy(src, dst, type->dtdt_size); 1011 } 1012} 1013 1014/* 1015 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1016 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1017 * safe memory that we can access directly because it is managed by DTrace. 1018 */ 1019static int 1020dtrace_bcmp(const void *s1, const void *s2, size_t len) 1021{ 1022 volatile uint16_t *flags; 1023 1024 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1025 1026 if (s1 == s2) 1027 return (0); 1028 1029 if (s1 == NULL || s2 == NULL) 1030 return (1); 1031 1032 if (s1 != s2 && len != 0) { 1033 const uint8_t *ps1 = s1; 1034 const uint8_t *ps2 = s2; 1035 1036 do { 1037 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1038 return (1); 1039 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1040 } 1041 return (0); 1042} 1043 1044/* 1045 * Zero the specified region using a simple byte-by-byte loop. Note that this 1046 * is for safe DTrace-managed memory only. 1047 */ 1048static void 1049dtrace_bzero(void *dst, size_t len) 1050{ 1051 uchar_t *cp; 1052 1053 for (cp = dst; len != 0; len--) 1054 *cp++ = 0; 1055} 1056 1057static void 1058dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1059{ 1060 uint64_t result[2]; 1061 1062 result[0] = addend1[0] + addend2[0]; 1063 result[1] = addend1[1] + addend2[1] + 1064 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1065 1066 sum[0] = result[0]; 1067 sum[1] = result[1]; 1068} 1069 1070/* 1071 * Shift the 128-bit value in a by b. If b is positive, shift left. 1072 * If b is negative, shift right. 1073 */ 1074static void 1075dtrace_shift_128(uint64_t *a, int b) 1076{ 1077 uint64_t mask; 1078 1079 if (b == 0) 1080 return; 1081 1082 if (b < 0) { 1083 b = -b; 1084 if (b >= 64) { 1085 a[0] = a[1] >> (b - 64); 1086 a[1] = 0; 1087 } else { 1088 a[0] >>= b; 1089 mask = 1LL << (64 - b); 1090 mask -= 1; 1091 a[0] |= ((a[1] & mask) << (64 - b)); 1092 a[1] >>= b; 1093 } 1094 } else { 1095 if (b >= 64) { 1096 a[1] = a[0] << (b - 64); 1097 a[0] = 0; 1098 } else { 1099 a[1] <<= b; 1100 mask = a[0] >> (64 - b); 1101 a[1] |= mask; 1102 a[0] <<= b; 1103 } 1104 } 1105} 1106 1107/* 1108 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1109 * use native multiplication on those, and then re-combine into the 1110 * resulting 128-bit value. 1111 * 1112 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1113 * hi1 * hi2 << 64 + 1114 * hi1 * lo2 << 32 + 1115 * hi2 * lo1 << 32 + 1116 * lo1 * lo2 1117 */ 1118static void 1119dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1120{ 1121 uint64_t hi1, hi2, lo1, lo2; 1122 uint64_t tmp[2]; 1123 1124 hi1 = factor1 >> 32; 1125 hi2 = factor2 >> 32; 1126 1127 lo1 = factor1 & DT_MASK_LO; 1128 lo2 = factor2 & DT_MASK_LO; 1129 1130 product[0] = lo1 * lo2; 1131 product[1] = hi1 * hi2; 1132 1133 tmp[0] = hi1 * lo2; 1134 tmp[1] = 0; 1135 dtrace_shift_128(tmp, 32); 1136 dtrace_add_128(product, tmp, product); 1137 1138 tmp[0] = hi2 * lo1; 1139 tmp[1] = 0; 1140 dtrace_shift_128(tmp, 32); 1141 dtrace_add_128(product, tmp, product); 1142} 1143 1144/* 1145 * This privilege check should be used by actions and subroutines to 1146 * verify that the user credentials of the process that enabled the 1147 * invoking ECB match the target credentials 1148 */ 1149static int 1150dtrace_priv_proc_common_user(dtrace_state_t *state) 1151{ 1152 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1153 1154 /* 1155 * We should always have a non-NULL state cred here, since if cred 1156 * is null (anonymous tracing), we fast-path bypass this routine. 1157 */ 1158 ASSERT(s_cr != NULL); 1159 1160 if ((cr = CRED()) != NULL && 1161 s_cr->cr_uid == cr->cr_uid && 1162 s_cr->cr_uid == cr->cr_ruid && 1163 s_cr->cr_uid == cr->cr_suid && 1164 s_cr->cr_gid == cr->cr_gid && 1165 s_cr->cr_gid == cr->cr_rgid && 1166 s_cr->cr_gid == cr->cr_sgid) 1167 return (1); 1168 1169 return (0); 1170} 1171 1172/* 1173 * This privilege check should be used by actions and subroutines to 1174 * verify that the zone of the process that enabled the invoking ECB 1175 * matches the target credentials 1176 */ 1177static int 1178dtrace_priv_proc_common_zone(dtrace_state_t *state) 1179{ 1180#if defined(sun) 1181 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1182 1183 /* 1184 * We should always have a non-NULL state cred here, since if cred 1185 * is null (anonymous tracing), we fast-path bypass this routine. 1186 */ 1187 ASSERT(s_cr != NULL); 1188 1189 if ((cr = CRED()) != NULL && 1190 s_cr->cr_zone == cr->cr_zone) 1191 return (1); 1192 1193 return (0); 1194#else 1195 return (1); 1196#endif 1197} 1198 1199/* 1200 * This privilege check should be used by actions and subroutines to 1201 * verify that the process has not setuid or changed credentials. 1202 */ 1203static int 1204dtrace_priv_proc_common_nocd(void) 1205{ 1206 proc_t *proc; 1207 1208 if ((proc = ttoproc(curthread)) != NULL && 1209 !(proc->p_flag & SNOCD)) 1210 return (1); 1211 1212 return (0); 1213} 1214 1215static int 1216dtrace_priv_proc_destructive(dtrace_state_t *state) 1217{ 1218 int action = state->dts_cred.dcr_action; 1219 1220 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1221 dtrace_priv_proc_common_zone(state) == 0) 1222 goto bad; 1223 1224 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1225 dtrace_priv_proc_common_user(state) == 0) 1226 goto bad; 1227 1228 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1229 dtrace_priv_proc_common_nocd() == 0) 1230 goto bad; 1231 1232 return (1); 1233 1234bad: 1235 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1236 1237 return (0); 1238} 1239 1240static int 1241dtrace_priv_proc_control(dtrace_state_t *state) 1242{ 1243 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1244 return (1); 1245 1246 if (dtrace_priv_proc_common_zone(state) && 1247 dtrace_priv_proc_common_user(state) && 1248 dtrace_priv_proc_common_nocd()) 1249 return (1); 1250 1251 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1252 1253 return (0); 1254} 1255 1256static int 1257dtrace_priv_proc(dtrace_state_t *state) 1258{ 1259 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1260 return (1); 1261 1262 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1263 1264 return (0); 1265} 1266 1267static int 1268dtrace_priv_kernel(dtrace_state_t *state) 1269{ 1270 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1271 return (1); 1272 1273 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1274 1275 return (0); 1276} 1277 1278static int 1279dtrace_priv_kernel_destructive(dtrace_state_t *state) 1280{ 1281 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1282 return (1); 1283 1284 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1285 1286 return (0); 1287} 1288 1289/* 1290 * Note: not called from probe context. This function is called 1291 * asynchronously (and at a regular interval) from outside of probe context to 1292 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1293 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1294 */ 1295void 1296dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1297{ 1298 dtrace_dynvar_t *dirty; 1299 dtrace_dstate_percpu_t *dcpu; 1300 int i, work = 0; 1301 1302 for (i = 0; i < NCPU; i++) { 1303 dcpu = &dstate->dtds_percpu[i]; 1304 1305 ASSERT(dcpu->dtdsc_rinsing == NULL); 1306 1307 /* 1308 * If the dirty list is NULL, there is no dirty work to do. 1309 */ 1310 if (dcpu->dtdsc_dirty == NULL) 1311 continue; 1312 1313 /* 1314 * If the clean list is non-NULL, then we're not going to do 1315 * any work for this CPU -- it means that there has not been 1316 * a dtrace_dynvar() allocation on this CPU (or from this CPU) 1317 * since the last time we cleaned house. 1318 */ 1319 if (dcpu->dtdsc_clean != NULL) 1320 continue; 1321 1322 work = 1; 1323 1324 /* 1325 * Atomically move the dirty list aside. 1326 */ 1327 do { 1328 dirty = dcpu->dtdsc_dirty; 1329 1330 /* 1331 * Before we zap the dirty list, set the rinsing list. 1332 * (This allows for a potential assertion in 1333 * dtrace_dynvar(): if a free dynamic variable appears 1334 * on a hash chain, either the dirty list or the 1335 * rinsing list for some CPU must be non-NULL.) 1336 */ 1337 dcpu->dtdsc_rinsing = dirty; 1338 dtrace_membar_producer(); 1339 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1340 dirty, NULL) != dirty); 1341 } 1342 1343 if (!work) { 1344 /* 1345 * We have no work to do; we can simply return. 1346 */ 1347 return; 1348 } 1349 1350 dtrace_sync(); 1351 1352 for (i = 0; i < NCPU; i++) { 1353 dcpu = &dstate->dtds_percpu[i]; 1354 1355 if (dcpu->dtdsc_rinsing == NULL) 1356 continue; 1357 1358 /* 1359 * We are now guaranteed that no hash chain contains a pointer 1360 * into this dirty list; we can make it clean. 1361 */ 1362 ASSERT(dcpu->dtdsc_clean == NULL); 1363 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1364 dcpu->dtdsc_rinsing = NULL; 1365 } 1366 1367 /* 1368 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1369 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1370 * This prevents a race whereby a CPU incorrectly decides that 1371 * the state should be something other than DTRACE_DSTATE_CLEAN 1372 * after dtrace_dynvar_clean() has completed. 1373 */ 1374 dtrace_sync(); 1375 1376 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1377} 1378 1379/* 1380 * Depending on the value of the op parameter, this function looks-up, 1381 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1382 * allocation is requested, this function will return a pointer to a 1383 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1384 * variable can be allocated. If NULL is returned, the appropriate counter 1385 * will be incremented. 1386 */ 1387dtrace_dynvar_t * 1388dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1389 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1390 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1391{ 1392 uint64_t hashval = DTRACE_DYNHASH_VALID; 1393 dtrace_dynhash_t *hash = dstate->dtds_hash; 1394 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1395 processorid_t me = curcpu, cpu = me; 1396 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1397 size_t bucket, ksize; 1398 size_t chunksize = dstate->dtds_chunksize; 1399 uintptr_t kdata, lock, nstate; 1400 uint_t i; 1401 1402 ASSERT(nkeys != 0); 1403 1404 /* 1405 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1406 * algorithm. For the by-value portions, we perform the algorithm in 1407 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1408 * bit, and seems to have only a minute effect on distribution. For 1409 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1410 * over each referenced byte. It's painful to do this, but it's much 1411 * better than pathological hash distribution. The efficacy of the 1412 * hashing algorithm (and a comparison with other algorithms) may be 1413 * found by running the ::dtrace_dynstat MDB dcmd. 1414 */ 1415 for (i = 0; i < nkeys; i++) { 1416 if (key[i].dttk_size == 0) { 1417 uint64_t val = key[i].dttk_value; 1418 1419 hashval += (val >> 48) & 0xffff; 1420 hashval += (hashval << 10); 1421 hashval ^= (hashval >> 6); 1422 1423 hashval += (val >> 32) & 0xffff; 1424 hashval += (hashval << 10); 1425 hashval ^= (hashval >> 6); 1426 1427 hashval += (val >> 16) & 0xffff; 1428 hashval += (hashval << 10); 1429 hashval ^= (hashval >> 6); 1430 1431 hashval += val & 0xffff; 1432 hashval += (hashval << 10); 1433 hashval ^= (hashval >> 6); 1434 } else { 1435 /* 1436 * This is incredibly painful, but it beats the hell 1437 * out of the alternative. 1438 */ 1439 uint64_t j, size = key[i].dttk_size; 1440 uintptr_t base = (uintptr_t)key[i].dttk_value; 1441 1442 if (!dtrace_canload(base, size, mstate, vstate)) 1443 break; 1444 1445 for (j = 0; j < size; j++) { 1446 hashval += dtrace_load8(base + j); 1447 hashval += (hashval << 10); 1448 hashval ^= (hashval >> 6); 1449 } 1450 } 1451 } 1452 1453 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1454 return (NULL); 1455 1456 hashval += (hashval << 3); 1457 hashval ^= (hashval >> 11); 1458 hashval += (hashval << 15); 1459 1460 /* 1461 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1462 * comes out to be one of our two sentinel hash values. If this 1463 * actually happens, we set the hashval to be a value known to be a 1464 * non-sentinel value. 1465 */ 1466 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1467 hashval = DTRACE_DYNHASH_VALID; 1468 1469 /* 1470 * Yes, it's painful to do a divide here. If the cycle count becomes 1471 * important here, tricks can be pulled to reduce it. (However, it's 1472 * critical that hash collisions be kept to an absolute minimum; 1473 * they're much more painful than a divide.) It's better to have a 1474 * solution that generates few collisions and still keeps things 1475 * relatively simple. 1476 */ 1477 bucket = hashval % dstate->dtds_hashsize; 1478 1479 if (op == DTRACE_DYNVAR_DEALLOC) { 1480 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1481 1482 for (;;) { 1483 while ((lock = *lockp) & 1) 1484 continue; 1485 1486 if (dtrace_casptr((volatile void *)lockp, 1487 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1488 break; 1489 } 1490 1491 dtrace_membar_producer(); 1492 } 1493 1494top: 1495 prev = NULL; 1496 lock = hash[bucket].dtdh_lock; 1497 1498 dtrace_membar_consumer(); 1499 1500 start = hash[bucket].dtdh_chain; 1501 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1502 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1503 op != DTRACE_DYNVAR_DEALLOC)); 1504 1505 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1506 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1507 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1508 1509 if (dvar->dtdv_hashval != hashval) { 1510 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1511 /* 1512 * We've reached the sink, and therefore the 1513 * end of the hash chain; we can kick out of 1514 * the loop knowing that we have seen a valid 1515 * snapshot of state. 1516 */ 1517 ASSERT(dvar->dtdv_next == NULL); 1518 ASSERT(dvar == &dtrace_dynhash_sink); 1519 break; 1520 } 1521 1522 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1523 /* 1524 * We've gone off the rails: somewhere along 1525 * the line, one of the members of this hash 1526 * chain was deleted. Note that we could also 1527 * detect this by simply letting this loop run 1528 * to completion, as we would eventually hit 1529 * the end of the dirty list. However, we 1530 * want to avoid running the length of the 1531 * dirty list unnecessarily (it might be quite 1532 * long), so we catch this as early as 1533 * possible by detecting the hash marker. In 1534 * this case, we simply set dvar to NULL and 1535 * break; the conditional after the loop will 1536 * send us back to top. 1537 */ 1538 dvar = NULL; 1539 break; 1540 } 1541 1542 goto next; 1543 } 1544 1545 if (dtuple->dtt_nkeys != nkeys) 1546 goto next; 1547 1548 for (i = 0; i < nkeys; i++, dkey++) { 1549 if (dkey->dttk_size != key[i].dttk_size) 1550 goto next; /* size or type mismatch */ 1551 1552 if (dkey->dttk_size != 0) { 1553 if (dtrace_bcmp( 1554 (void *)(uintptr_t)key[i].dttk_value, 1555 (void *)(uintptr_t)dkey->dttk_value, 1556 dkey->dttk_size)) 1557 goto next; 1558 } else { 1559 if (dkey->dttk_value != key[i].dttk_value) 1560 goto next; 1561 } 1562 } 1563 1564 if (op != DTRACE_DYNVAR_DEALLOC) 1565 return (dvar); 1566 1567 ASSERT(dvar->dtdv_next == NULL || 1568 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1569 1570 if (prev != NULL) { 1571 ASSERT(hash[bucket].dtdh_chain != dvar); 1572 ASSERT(start != dvar); 1573 ASSERT(prev->dtdv_next == dvar); 1574 prev->dtdv_next = dvar->dtdv_next; 1575 } else { 1576 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1577 start, dvar->dtdv_next) != start) { 1578 /* 1579 * We have failed to atomically swing the 1580 * hash table head pointer, presumably because 1581 * of a conflicting allocation on another CPU. 1582 * We need to reread the hash chain and try 1583 * again. 1584 */ 1585 goto top; 1586 } 1587 } 1588 1589 dtrace_membar_producer(); 1590 1591 /* 1592 * Now set the hash value to indicate that it's free. 1593 */ 1594 ASSERT(hash[bucket].dtdh_chain != dvar); 1595 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1596 1597 dtrace_membar_producer(); 1598 1599 /* 1600 * Set the next pointer to point at the dirty list, and 1601 * atomically swing the dirty pointer to the newly freed dvar. 1602 */ 1603 do { 1604 next = dcpu->dtdsc_dirty; 1605 dvar->dtdv_next = next; 1606 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1607 1608 /* 1609 * Finally, unlock this hash bucket. 1610 */ 1611 ASSERT(hash[bucket].dtdh_lock == lock); 1612 ASSERT(lock & 1); 1613 hash[bucket].dtdh_lock++; 1614 1615 return (NULL); 1616next: 1617 prev = dvar; 1618 continue; 1619 } 1620 1621 if (dvar == NULL) { 1622 /* 1623 * If dvar is NULL, it is because we went off the rails: 1624 * one of the elements that we traversed in the hash chain 1625 * was deleted while we were traversing it. In this case, 1626 * we assert that we aren't doing a dealloc (deallocs lock 1627 * the hash bucket to prevent themselves from racing with 1628 * one another), and retry the hash chain traversal. 1629 */ 1630 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1631 goto top; 1632 } 1633 1634 if (op != DTRACE_DYNVAR_ALLOC) { 1635 /* 1636 * If we are not to allocate a new variable, we want to 1637 * return NULL now. Before we return, check that the value 1638 * of the lock word hasn't changed. If it has, we may have 1639 * seen an inconsistent snapshot. 1640 */ 1641 if (op == DTRACE_DYNVAR_NOALLOC) { 1642 if (hash[bucket].dtdh_lock != lock) 1643 goto top; 1644 } else { 1645 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1646 ASSERT(hash[bucket].dtdh_lock == lock); 1647 ASSERT(lock & 1); 1648 hash[bucket].dtdh_lock++; 1649 } 1650 1651 return (NULL); 1652 } 1653 1654 /* 1655 * We need to allocate a new dynamic variable. The size we need is the 1656 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1657 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1658 * the size of any referred-to data (dsize). We then round the final 1659 * size up to the chunksize for allocation. 1660 */ 1661 for (ksize = 0, i = 0; i < nkeys; i++) 1662 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1663 1664 /* 1665 * This should be pretty much impossible, but could happen if, say, 1666 * strange DIF specified the tuple. Ideally, this should be an 1667 * assertion and not an error condition -- but that requires that the 1668 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1669 * bullet-proof. (That is, it must not be able to be fooled by 1670 * malicious DIF.) Given the lack of backwards branches in DIF, 1671 * solving this would presumably not amount to solving the Halting 1672 * Problem -- but it still seems awfully hard. 1673 */ 1674 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1675 ksize + dsize > chunksize) { 1676 dcpu->dtdsc_drops++; 1677 return (NULL); 1678 } 1679 1680 nstate = DTRACE_DSTATE_EMPTY; 1681 1682 do { 1683retry: 1684 free = dcpu->dtdsc_free; 1685 1686 if (free == NULL) { 1687 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1688 void *rval; 1689 1690 if (clean == NULL) { 1691 /* 1692 * We're out of dynamic variable space on 1693 * this CPU. Unless we have tried all CPUs, 1694 * we'll try to allocate from a different 1695 * CPU. 1696 */ 1697 switch (dstate->dtds_state) { 1698 case DTRACE_DSTATE_CLEAN: { 1699 void *sp = &dstate->dtds_state; 1700 1701 if (++cpu >= NCPU) 1702 cpu = 0; 1703 1704 if (dcpu->dtdsc_dirty != NULL && 1705 nstate == DTRACE_DSTATE_EMPTY) 1706 nstate = DTRACE_DSTATE_DIRTY; 1707 1708 if (dcpu->dtdsc_rinsing != NULL) 1709 nstate = DTRACE_DSTATE_RINSING; 1710 1711 dcpu = &dstate->dtds_percpu[cpu]; 1712 1713 if (cpu != me) 1714 goto retry; 1715 1716 (void) dtrace_cas32(sp, 1717 DTRACE_DSTATE_CLEAN, nstate); 1718 1719 /* 1720 * To increment the correct bean 1721 * counter, take another lap. 1722 */ 1723 goto retry; 1724 } 1725 1726 case DTRACE_DSTATE_DIRTY: 1727 dcpu->dtdsc_dirty_drops++; 1728 break; 1729 1730 case DTRACE_DSTATE_RINSING: 1731 dcpu->dtdsc_rinsing_drops++; 1732 break; 1733 1734 case DTRACE_DSTATE_EMPTY: 1735 dcpu->dtdsc_drops++; 1736 break; 1737 } 1738 1739 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1740 return (NULL); 1741 } 1742 1743 /* 1744 * The clean list appears to be non-empty. We want to 1745 * move the clean list to the free list; we start by 1746 * moving the clean pointer aside. 1747 */ 1748 if (dtrace_casptr(&dcpu->dtdsc_clean, 1749 clean, NULL) != clean) { 1750 /* 1751 * We are in one of two situations: 1752 * 1753 * (a) The clean list was switched to the 1754 * free list by another CPU. 1755 * 1756 * (b) The clean list was added to by the 1757 * cleansing cyclic. 1758 * 1759 * In either of these situations, we can 1760 * just reattempt the free list allocation. 1761 */ 1762 goto retry; 1763 } 1764 1765 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1766 1767 /* 1768 * Now we'll move the clean list to the free list. 1769 * It's impossible for this to fail: the only way 1770 * the free list can be updated is through this 1771 * code path, and only one CPU can own the clean list. 1772 * Thus, it would only be possible for this to fail if 1773 * this code were racing with dtrace_dynvar_clean(). 1774 * (That is, if dtrace_dynvar_clean() updated the clean 1775 * list, and we ended up racing to update the free 1776 * list.) This race is prevented by the dtrace_sync() 1777 * in dtrace_dynvar_clean() -- which flushes the 1778 * owners of the clean lists out before resetting 1779 * the clean lists. 1780 */ 1781 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1782 ASSERT(rval == NULL); 1783 goto retry; 1784 } 1785 1786 dvar = free; 1787 new_free = dvar->dtdv_next; 1788 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1789 1790 /* 1791 * We have now allocated a new chunk. We copy the tuple keys into the 1792 * tuple array and copy any referenced key data into the data space 1793 * following the tuple array. As we do this, we relocate dttk_value 1794 * in the final tuple to point to the key data address in the chunk. 1795 */ 1796 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1797 dvar->dtdv_data = (void *)(kdata + ksize); 1798 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1799 1800 for (i = 0; i < nkeys; i++) { 1801 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1802 size_t kesize = key[i].dttk_size; 1803 1804 if (kesize != 0) { 1805 dtrace_bcopy( 1806 (const void *)(uintptr_t)key[i].dttk_value, 1807 (void *)kdata, kesize); 1808 dkey->dttk_value = kdata; 1809 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1810 } else { 1811 dkey->dttk_value = key[i].dttk_value; 1812 } 1813 1814 dkey->dttk_size = kesize; 1815 } 1816 1817 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1818 dvar->dtdv_hashval = hashval; 1819 dvar->dtdv_next = start; 1820 1821 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1822 return (dvar); 1823 1824 /* 1825 * The cas has failed. Either another CPU is adding an element to 1826 * this hash chain, or another CPU is deleting an element from this 1827 * hash chain. The simplest way to deal with both of these cases 1828 * (though not necessarily the most efficient) is to free our 1829 * allocated block and tail-call ourselves. Note that the free is 1830 * to the dirty list and _not_ to the free list. This is to prevent 1831 * races with allocators, above. 1832 */ 1833 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1834 1835 dtrace_membar_producer(); 1836 1837 do { 1838 free = dcpu->dtdsc_dirty; 1839 dvar->dtdv_next = free; 1840 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1841 1842 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 1843} 1844 1845/*ARGSUSED*/ 1846static void 1847dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1848{ 1849 if ((int64_t)nval < (int64_t)*oval) 1850 *oval = nval; 1851} 1852 1853/*ARGSUSED*/ 1854static void 1855dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1856{ 1857 if ((int64_t)nval > (int64_t)*oval) 1858 *oval = nval; 1859} 1860 1861static void 1862dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1863{ 1864 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1865 int64_t val = (int64_t)nval; 1866 1867 if (val < 0) { 1868 for (i = 0; i < zero; i++) { 1869 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1870 quanta[i] += incr; 1871 return; 1872 } 1873 } 1874 } else { 1875 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1876 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1877 quanta[i - 1] += incr; 1878 return; 1879 } 1880 } 1881 1882 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1883 return; 1884 } 1885 1886 ASSERT(0); 1887} 1888 1889static void 1890dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1891{ 1892 uint64_t arg = *lquanta++; 1893 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1894 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1895 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1896 int32_t val = (int32_t)nval, level; 1897 1898 ASSERT(step != 0); 1899 ASSERT(levels != 0); 1900 1901 if (val < base) { 1902 /* 1903 * This is an underflow. 1904 */ 1905 lquanta[0] += incr; 1906 return; 1907 } 1908 1909 level = (val - base) / step; 1910 1911 if (level < levels) { 1912 lquanta[level + 1] += incr; 1913 return; 1914 } 1915 1916 /* 1917 * This is an overflow. 1918 */ 1919 lquanta[levels + 1] += incr; 1920} 1921 1922static int 1923dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 1924 uint16_t high, uint16_t nsteps, int64_t value) 1925{ 1926 int64_t this = 1, last, next; 1927 int base = 1, order; 1928 1929 ASSERT(factor <= nsteps); 1930 ASSERT(nsteps % factor == 0); 1931 1932 for (order = 0; order < low; order++) 1933 this *= factor; 1934 1935 /* 1936 * If our value is less than our factor taken to the power of the 1937 * low order of magnitude, it goes into the zeroth bucket. 1938 */ 1939 if (value < (last = this)) 1940 return (0); 1941 1942 for (this *= factor; order <= high; order++) { 1943 int nbuckets = this > nsteps ? nsteps : this; 1944 1945 if ((next = this * factor) < this) { 1946 /* 1947 * We should not generally get log/linear quantizations 1948 * with a high magnitude that allows 64-bits to 1949 * overflow, but we nonetheless protect against this 1950 * by explicitly checking for overflow, and clamping 1951 * our value accordingly. 1952 */ 1953 value = this - 1; 1954 } 1955 1956 if (value < this) { 1957 /* 1958 * If our value lies within this order of magnitude, 1959 * determine its position by taking the offset within 1960 * the order of magnitude, dividing by the bucket 1961 * width, and adding to our (accumulated) base. 1962 */ 1963 return (base + (value - last) / (this / nbuckets)); 1964 } 1965 1966 base += nbuckets - (nbuckets / factor); 1967 last = this; 1968 this = next; 1969 } 1970 1971 /* 1972 * Our value is greater than or equal to our factor taken to the 1973 * power of one plus the high magnitude -- return the top bucket. 1974 */ 1975 return (base); 1976} 1977 1978static void 1979dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 1980{ 1981 uint64_t arg = *llquanta++; 1982 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 1983 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 1984 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 1985 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 1986 1987 llquanta[dtrace_aggregate_llquantize_bucket(factor, 1988 low, high, nsteps, nval)] += incr; 1989} 1990 1991/*ARGSUSED*/ 1992static void 1993dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 1994{ 1995 data[0]++; 1996 data[1] += nval; 1997} 1998 1999/*ARGSUSED*/ 2000static void 2001dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2002{ 2003 int64_t snval = (int64_t)nval; 2004 uint64_t tmp[2]; 2005 2006 data[0]++; 2007 data[1] += nval; 2008 2009 /* 2010 * What we want to say here is: 2011 * 2012 * data[2] += nval * nval; 2013 * 2014 * But given that nval is 64-bit, we could easily overflow, so 2015 * we do this as 128-bit arithmetic. 2016 */ 2017 if (snval < 0) 2018 snval = -snval; 2019 2020 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2021 dtrace_add_128(data + 2, tmp, data + 2); 2022} 2023 2024/*ARGSUSED*/ 2025static void 2026dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2027{ 2028 *oval = *oval + 1; 2029} 2030 2031/*ARGSUSED*/ 2032static void 2033dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2034{ 2035 *oval += nval; 2036} 2037 2038/* 2039 * Aggregate given the tuple in the principal data buffer, and the aggregating 2040 * action denoted by the specified dtrace_aggregation_t. The aggregation 2041 * buffer is specified as the buf parameter. This routine does not return 2042 * failure; if there is no space in the aggregation buffer, the data will be 2043 * dropped, and a corresponding counter incremented. 2044 */ 2045static void 2046dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2047 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2048{ 2049 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2050 uint32_t i, ndx, size, fsize; 2051 uint32_t align = sizeof (uint64_t) - 1; 2052 dtrace_aggbuffer_t *agb; 2053 dtrace_aggkey_t *key; 2054 uint32_t hashval = 0, limit, isstr; 2055 caddr_t tomax, data, kdata; 2056 dtrace_actkind_t action; 2057 dtrace_action_t *act; 2058 uintptr_t offs; 2059 2060 if (buf == NULL) 2061 return; 2062 2063 if (!agg->dtag_hasarg) { 2064 /* 2065 * Currently, only quantize() and lquantize() take additional 2066 * arguments, and they have the same semantics: an increment 2067 * value that defaults to 1 when not present. If additional 2068 * aggregating actions take arguments, the setting of the 2069 * default argument value will presumably have to become more 2070 * sophisticated... 2071 */ 2072 arg = 1; 2073 } 2074 2075 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2076 size = rec->dtrd_offset - agg->dtag_base; 2077 fsize = size + rec->dtrd_size; 2078 2079 ASSERT(dbuf->dtb_tomax != NULL); 2080 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2081 2082 if ((tomax = buf->dtb_tomax) == NULL) { 2083 dtrace_buffer_drop(buf); 2084 return; 2085 } 2086 2087 /* 2088 * The metastructure is always at the bottom of the buffer. 2089 */ 2090 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2091 sizeof (dtrace_aggbuffer_t)); 2092 2093 if (buf->dtb_offset == 0) { 2094 /* 2095 * We just kludge up approximately 1/8th of the size to be 2096 * buckets. If this guess ends up being routinely 2097 * off-the-mark, we may need to dynamically readjust this 2098 * based on past performance. 2099 */ 2100 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2101 2102 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2103 (uintptr_t)tomax || hashsize == 0) { 2104 /* 2105 * We've been given a ludicrously small buffer; 2106 * increment our drop count and leave. 2107 */ 2108 dtrace_buffer_drop(buf); 2109 return; 2110 } 2111 2112 /* 2113 * And now, a pathetic attempt to try to get a an odd (or 2114 * perchance, a prime) hash size for better hash distribution. 2115 */ 2116 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2117 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2118 2119 agb->dtagb_hashsize = hashsize; 2120 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2121 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2122 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2123 2124 for (i = 0; i < agb->dtagb_hashsize; i++) 2125 agb->dtagb_hash[i] = NULL; 2126 } 2127 2128 ASSERT(agg->dtag_first != NULL); 2129 ASSERT(agg->dtag_first->dta_intuple); 2130 2131 /* 2132 * Calculate the hash value based on the key. Note that we _don't_ 2133 * include the aggid in the hashing (but we will store it as part of 2134 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2135 * algorithm: a simple, quick algorithm that has no known funnels, and 2136 * gets good distribution in practice. The efficacy of the hashing 2137 * algorithm (and a comparison with other algorithms) may be found by 2138 * running the ::dtrace_aggstat MDB dcmd. 2139 */ 2140 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2141 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2142 limit = i + act->dta_rec.dtrd_size; 2143 ASSERT(limit <= size); 2144 isstr = DTRACEACT_ISSTRING(act); 2145 2146 for (; i < limit; i++) { 2147 hashval += data[i]; 2148 hashval += (hashval << 10); 2149 hashval ^= (hashval >> 6); 2150 2151 if (isstr && data[i] == '\0') 2152 break; 2153 } 2154 } 2155 2156 hashval += (hashval << 3); 2157 hashval ^= (hashval >> 11); 2158 hashval += (hashval << 15); 2159 2160 /* 2161 * Yes, the divide here is expensive -- but it's generally the least 2162 * of the performance issues given the amount of data that we iterate 2163 * over to compute hash values, compare data, etc. 2164 */ 2165 ndx = hashval % agb->dtagb_hashsize; 2166 2167 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2168 ASSERT((caddr_t)key >= tomax); 2169 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2170 2171 if (hashval != key->dtak_hashval || key->dtak_size != size) 2172 continue; 2173 2174 kdata = key->dtak_data; 2175 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2176 2177 for (act = agg->dtag_first; act->dta_intuple; 2178 act = act->dta_next) { 2179 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2180 limit = i + act->dta_rec.dtrd_size; 2181 ASSERT(limit <= size); 2182 isstr = DTRACEACT_ISSTRING(act); 2183 2184 for (; i < limit; i++) { 2185 if (kdata[i] != data[i]) 2186 goto next; 2187 2188 if (isstr && data[i] == '\0') 2189 break; 2190 } 2191 } 2192 2193 if (action != key->dtak_action) { 2194 /* 2195 * We are aggregating on the same value in the same 2196 * aggregation with two different aggregating actions. 2197 * (This should have been picked up in the compiler, 2198 * so we may be dealing with errant or devious DIF.) 2199 * This is an error condition; we indicate as much, 2200 * and return. 2201 */ 2202 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2203 return; 2204 } 2205 2206 /* 2207 * This is a hit: we need to apply the aggregator to 2208 * the value at this key. 2209 */ 2210 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2211 return; 2212next: 2213 continue; 2214 } 2215 2216 /* 2217 * We didn't find it. We need to allocate some zero-filled space, 2218 * link it into the hash table appropriately, and apply the aggregator 2219 * to the (zero-filled) value. 2220 */ 2221 offs = buf->dtb_offset; 2222 while (offs & (align - 1)) 2223 offs += sizeof (uint32_t); 2224 2225 /* 2226 * If we don't have enough room to both allocate a new key _and_ 2227 * its associated data, increment the drop count and return. 2228 */ 2229 if ((uintptr_t)tomax + offs + fsize > 2230 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2231 dtrace_buffer_drop(buf); 2232 return; 2233 } 2234 2235 /*CONSTCOND*/ 2236 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2237 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2238 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2239 2240 key->dtak_data = kdata = tomax + offs; 2241 buf->dtb_offset = offs + fsize; 2242 2243 /* 2244 * Now copy the data across. 2245 */ 2246 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2247 2248 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2249 kdata[i] = data[i]; 2250 2251 /* 2252 * Because strings are not zeroed out by default, we need to iterate 2253 * looking for actions that store strings, and we need to explicitly 2254 * pad these strings out with zeroes. 2255 */ 2256 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2257 int nul; 2258 2259 if (!DTRACEACT_ISSTRING(act)) 2260 continue; 2261 2262 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2263 limit = i + act->dta_rec.dtrd_size; 2264 ASSERT(limit <= size); 2265 2266 for (nul = 0; i < limit; i++) { 2267 if (nul) { 2268 kdata[i] = '\0'; 2269 continue; 2270 } 2271 2272 if (data[i] != '\0') 2273 continue; 2274 2275 nul = 1; 2276 } 2277 } 2278 2279 for (i = size; i < fsize; i++) 2280 kdata[i] = 0; 2281 2282 key->dtak_hashval = hashval; 2283 key->dtak_size = size; 2284 key->dtak_action = action; 2285 key->dtak_next = agb->dtagb_hash[ndx]; 2286 agb->dtagb_hash[ndx] = key; 2287 2288 /* 2289 * Finally, apply the aggregator. 2290 */ 2291 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2292 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2293} 2294 2295/* 2296 * Given consumer state, this routine finds a speculation in the INACTIVE 2297 * state and transitions it into the ACTIVE state. If there is no speculation 2298 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2299 * incremented -- it is up to the caller to take appropriate action. 2300 */ 2301static int 2302dtrace_speculation(dtrace_state_t *state) 2303{ 2304 int i = 0; 2305 dtrace_speculation_state_t current; 2306 uint32_t *stat = &state->dts_speculations_unavail, count; 2307 2308 while (i < state->dts_nspeculations) { 2309 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2310 2311 current = spec->dtsp_state; 2312 2313 if (current != DTRACESPEC_INACTIVE) { 2314 if (current == DTRACESPEC_COMMITTINGMANY || 2315 current == DTRACESPEC_COMMITTING || 2316 current == DTRACESPEC_DISCARDING) 2317 stat = &state->dts_speculations_busy; 2318 i++; 2319 continue; 2320 } 2321 2322 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2323 current, DTRACESPEC_ACTIVE) == current) 2324 return (i + 1); 2325 } 2326 2327 /* 2328 * We couldn't find a speculation. If we found as much as a single 2329 * busy speculation buffer, we'll attribute this failure as "busy" 2330 * instead of "unavail". 2331 */ 2332 do { 2333 count = *stat; 2334 } while (dtrace_cas32(stat, count, count + 1) != count); 2335 2336 return (0); 2337} 2338 2339/* 2340 * This routine commits an active speculation. If the specified speculation 2341 * is not in a valid state to perform a commit(), this routine will silently do 2342 * nothing. The state of the specified speculation is transitioned according 2343 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2344 */ 2345static void 2346dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2347 dtrace_specid_t which) 2348{ 2349 dtrace_speculation_t *spec; 2350 dtrace_buffer_t *src, *dest; 2351 uintptr_t daddr, saddr, dlimit, slimit; 2352 dtrace_speculation_state_t current, new = 0; 2353 intptr_t offs; 2354 uint64_t timestamp; 2355 2356 if (which == 0) 2357 return; 2358 2359 if (which > state->dts_nspeculations) { 2360 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2361 return; 2362 } 2363 2364 spec = &state->dts_speculations[which - 1]; 2365 src = &spec->dtsp_buffer[cpu]; 2366 dest = &state->dts_buffer[cpu]; 2367 2368 do { 2369 current = spec->dtsp_state; 2370 2371 if (current == DTRACESPEC_COMMITTINGMANY) 2372 break; 2373 2374 switch (current) { 2375 case DTRACESPEC_INACTIVE: 2376 case DTRACESPEC_DISCARDING: 2377 return; 2378 2379 case DTRACESPEC_COMMITTING: 2380 /* 2381 * This is only possible if we are (a) commit()'ing 2382 * without having done a prior speculate() on this CPU 2383 * and (b) racing with another commit() on a different 2384 * CPU. There's nothing to do -- we just assert that 2385 * our offset is 0. 2386 */ 2387 ASSERT(src->dtb_offset == 0); 2388 return; 2389 2390 case DTRACESPEC_ACTIVE: 2391 new = DTRACESPEC_COMMITTING; 2392 break; 2393 2394 case DTRACESPEC_ACTIVEONE: 2395 /* 2396 * This speculation is active on one CPU. If our 2397 * buffer offset is non-zero, we know that the one CPU 2398 * must be us. Otherwise, we are committing on a 2399 * different CPU from the speculate(), and we must 2400 * rely on being asynchronously cleaned. 2401 */ 2402 if (src->dtb_offset != 0) { 2403 new = DTRACESPEC_COMMITTING; 2404 break; 2405 } 2406 /*FALLTHROUGH*/ 2407 2408 case DTRACESPEC_ACTIVEMANY: 2409 new = DTRACESPEC_COMMITTINGMANY; 2410 break; 2411 2412 default: 2413 ASSERT(0); 2414 } 2415 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2416 current, new) != current); 2417 2418 /* 2419 * We have set the state to indicate that we are committing this 2420 * speculation. Now reserve the necessary space in the destination 2421 * buffer. 2422 */ 2423 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2424 sizeof (uint64_t), state, NULL)) < 0) { 2425 dtrace_buffer_drop(dest); 2426 goto out; 2427 } 2428 2429 /* 2430 * We have sufficient space to copy the speculative buffer into the 2431 * primary buffer. First, modify the speculative buffer, filling 2432 * in the timestamp of all entries with the current time. The data 2433 * must have the commit() time rather than the time it was traced, 2434 * so that all entries in the primary buffer are in timestamp order. 2435 */ 2436 timestamp = dtrace_gethrtime(); 2437 saddr = (uintptr_t)src->dtb_tomax; 2438 slimit = saddr + src->dtb_offset; 2439 while (saddr < slimit) { 2440 size_t size; 2441 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2442 2443 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2444 saddr += sizeof (dtrace_epid_t); 2445 continue; 2446 } 2447 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2448 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2449 2450 ASSERT3U(saddr + size, <=, slimit); 2451 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2452 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2453 2454 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2455 2456 saddr += size; 2457 } 2458 2459 /* 2460 * Copy the buffer across. (Note that this is a 2461 * highly subobtimal bcopy(); in the unlikely event that this becomes 2462 * a serious performance issue, a high-performance DTrace-specific 2463 * bcopy() should obviously be invented.) 2464 */ 2465 daddr = (uintptr_t)dest->dtb_tomax + offs; 2466 dlimit = daddr + src->dtb_offset; 2467 saddr = (uintptr_t)src->dtb_tomax; 2468 2469 /* 2470 * First, the aligned portion. 2471 */ 2472 while (dlimit - daddr >= sizeof (uint64_t)) { 2473 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2474 2475 daddr += sizeof (uint64_t); 2476 saddr += sizeof (uint64_t); 2477 } 2478 2479 /* 2480 * Now any left-over bit... 2481 */ 2482 while (dlimit - daddr) 2483 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2484 2485 /* 2486 * Finally, commit the reserved space in the destination buffer. 2487 */ 2488 dest->dtb_offset = offs + src->dtb_offset; 2489 2490out: 2491 /* 2492 * If we're lucky enough to be the only active CPU on this speculation 2493 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2494 */ 2495 if (current == DTRACESPEC_ACTIVE || 2496 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2497 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2498 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2499 2500 ASSERT(rval == DTRACESPEC_COMMITTING); 2501 } 2502 2503 src->dtb_offset = 0; 2504 src->dtb_xamot_drops += src->dtb_drops; 2505 src->dtb_drops = 0; 2506} 2507 2508/* 2509 * This routine discards an active speculation. If the specified speculation 2510 * is not in a valid state to perform a discard(), this routine will silently 2511 * do nothing. The state of the specified speculation is transitioned 2512 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2513 */ 2514static void 2515dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2516 dtrace_specid_t which) 2517{ 2518 dtrace_speculation_t *spec; 2519 dtrace_speculation_state_t current, new = 0; 2520 dtrace_buffer_t *buf; 2521 2522 if (which == 0) 2523 return; 2524 2525 if (which > state->dts_nspeculations) { 2526 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2527 return; 2528 } 2529 2530 spec = &state->dts_speculations[which - 1]; 2531 buf = &spec->dtsp_buffer[cpu]; 2532 2533 do { 2534 current = spec->dtsp_state; 2535 2536 switch (current) { 2537 case DTRACESPEC_INACTIVE: 2538 case DTRACESPEC_COMMITTINGMANY: 2539 case DTRACESPEC_COMMITTING: 2540 case DTRACESPEC_DISCARDING: 2541 return; 2542 2543 case DTRACESPEC_ACTIVE: 2544 case DTRACESPEC_ACTIVEMANY: 2545 new = DTRACESPEC_DISCARDING; 2546 break; 2547 2548 case DTRACESPEC_ACTIVEONE: 2549 if (buf->dtb_offset != 0) { 2550 new = DTRACESPEC_INACTIVE; 2551 } else { 2552 new = DTRACESPEC_DISCARDING; 2553 } 2554 break; 2555 2556 default: 2557 ASSERT(0); 2558 } 2559 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2560 current, new) != current); 2561 2562 buf->dtb_offset = 0; 2563 buf->dtb_drops = 0; 2564} 2565 2566/* 2567 * Note: not called from probe context. This function is called 2568 * asynchronously from cross call context to clean any speculations that are 2569 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2570 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2571 * speculation. 2572 */ 2573static void 2574dtrace_speculation_clean_here(dtrace_state_t *state) 2575{ 2576 dtrace_icookie_t cookie; 2577 processorid_t cpu = curcpu; 2578 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2579 dtrace_specid_t i; 2580 2581 cookie = dtrace_interrupt_disable(); 2582 2583 if (dest->dtb_tomax == NULL) { 2584 dtrace_interrupt_enable(cookie); 2585 return; 2586 } 2587 2588 for (i = 0; i < state->dts_nspeculations; i++) { 2589 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2590 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2591 2592 if (src->dtb_tomax == NULL) 2593 continue; 2594 2595 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2596 src->dtb_offset = 0; 2597 continue; 2598 } 2599 2600 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2601 continue; 2602 2603 if (src->dtb_offset == 0) 2604 continue; 2605 2606 dtrace_speculation_commit(state, cpu, i + 1); 2607 } 2608 2609 dtrace_interrupt_enable(cookie); 2610} 2611 2612/* 2613 * Note: not called from probe context. This function is called 2614 * asynchronously (and at a regular interval) to clean any speculations that 2615 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2616 * is work to be done, it cross calls all CPUs to perform that work; 2617 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2618 * INACTIVE state until they have been cleaned by all CPUs. 2619 */ 2620static void 2621dtrace_speculation_clean(dtrace_state_t *state) 2622{ 2623 int work = 0, rv; 2624 dtrace_specid_t i; 2625 2626 for (i = 0; i < state->dts_nspeculations; i++) { 2627 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2628 2629 ASSERT(!spec->dtsp_cleaning); 2630 2631 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2632 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2633 continue; 2634 2635 work++; 2636 spec->dtsp_cleaning = 1; 2637 } 2638 2639 if (!work) 2640 return; 2641 2642 dtrace_xcall(DTRACE_CPUALL, 2643 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2644 2645 /* 2646 * We now know that all CPUs have committed or discarded their 2647 * speculation buffers, as appropriate. We can now set the state 2648 * to inactive. 2649 */ 2650 for (i = 0; i < state->dts_nspeculations; i++) { 2651 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2652 dtrace_speculation_state_t current, new; 2653 2654 if (!spec->dtsp_cleaning) 2655 continue; 2656 2657 current = spec->dtsp_state; 2658 ASSERT(current == DTRACESPEC_DISCARDING || 2659 current == DTRACESPEC_COMMITTINGMANY); 2660 2661 new = DTRACESPEC_INACTIVE; 2662 2663 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2664 ASSERT(rv == current); 2665 spec->dtsp_cleaning = 0; 2666 } 2667} 2668 2669/* 2670 * Called as part of a speculate() to get the speculative buffer associated 2671 * with a given speculation. Returns NULL if the specified speculation is not 2672 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2673 * the active CPU is not the specified CPU -- the speculation will be 2674 * atomically transitioned into the ACTIVEMANY state. 2675 */ 2676static dtrace_buffer_t * 2677dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2678 dtrace_specid_t which) 2679{ 2680 dtrace_speculation_t *spec; 2681 dtrace_speculation_state_t current, new = 0; 2682 dtrace_buffer_t *buf; 2683 2684 if (which == 0) 2685 return (NULL); 2686 2687 if (which > state->dts_nspeculations) { 2688 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2689 return (NULL); 2690 } 2691 2692 spec = &state->dts_speculations[which - 1]; 2693 buf = &spec->dtsp_buffer[cpuid]; 2694 2695 do { 2696 current = spec->dtsp_state; 2697 2698 switch (current) { 2699 case DTRACESPEC_INACTIVE: 2700 case DTRACESPEC_COMMITTINGMANY: 2701 case DTRACESPEC_DISCARDING: 2702 return (NULL); 2703 2704 case DTRACESPEC_COMMITTING: 2705 ASSERT(buf->dtb_offset == 0); 2706 return (NULL); 2707 2708 case DTRACESPEC_ACTIVEONE: 2709 /* 2710 * This speculation is currently active on one CPU. 2711 * Check the offset in the buffer; if it's non-zero, 2712 * that CPU must be us (and we leave the state alone). 2713 * If it's zero, assume that we're starting on a new 2714 * CPU -- and change the state to indicate that the 2715 * speculation is active on more than one CPU. 2716 */ 2717 if (buf->dtb_offset != 0) 2718 return (buf); 2719 2720 new = DTRACESPEC_ACTIVEMANY; 2721 break; 2722 2723 case DTRACESPEC_ACTIVEMANY: 2724 return (buf); 2725 2726 case DTRACESPEC_ACTIVE: 2727 new = DTRACESPEC_ACTIVEONE; 2728 break; 2729 2730 default: 2731 ASSERT(0); 2732 } 2733 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2734 current, new) != current); 2735 2736 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2737 return (buf); 2738} 2739 2740/* 2741 * Return a string. In the event that the user lacks the privilege to access 2742 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2743 * don't fail access checking. 2744 * 2745 * dtrace_dif_variable() uses this routine as a helper for various 2746 * builtin values such as 'execname' and 'probefunc.' 2747 */ 2748uintptr_t 2749dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 2750 dtrace_mstate_t *mstate) 2751{ 2752 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2753 uintptr_t ret; 2754 size_t strsz; 2755 2756 /* 2757 * The easy case: this probe is allowed to read all of memory, so 2758 * we can just return this as a vanilla pointer. 2759 */ 2760 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 2761 return (addr); 2762 2763 /* 2764 * This is the tougher case: we copy the string in question from 2765 * kernel memory into scratch memory and return it that way: this 2766 * ensures that we won't trip up when access checking tests the 2767 * BYREF return value. 2768 */ 2769 strsz = dtrace_strlen((char *)addr, size) + 1; 2770 2771 if (mstate->dtms_scratch_ptr + strsz > 2772 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2773 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2774 return (0); 2775 } 2776 2777 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2778 strsz); 2779 ret = mstate->dtms_scratch_ptr; 2780 mstate->dtms_scratch_ptr += strsz; 2781 return (ret); 2782} 2783 2784/* 2785 * Return a string from a memoy address which is known to have one or 2786 * more concatenated, individually zero terminated, sub-strings. 2787 * In the event that the user lacks the privilege to access 2788 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2789 * don't fail access checking. 2790 * 2791 * dtrace_dif_variable() uses this routine as a helper for various 2792 * builtin values such as 'execargs'. 2793 */ 2794static uintptr_t 2795dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 2796 dtrace_mstate_t *mstate) 2797{ 2798 char *p; 2799 size_t i; 2800 uintptr_t ret; 2801 2802 if (mstate->dtms_scratch_ptr + strsz > 2803 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2804 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2805 return (0); 2806 } 2807 2808 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2809 strsz); 2810 2811 /* Replace sub-string termination characters with a space. */ 2812 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 2813 p++, i++) 2814 if (*p == '\0') 2815 *p = ' '; 2816 2817 ret = mstate->dtms_scratch_ptr; 2818 mstate->dtms_scratch_ptr += strsz; 2819 return (ret); 2820} 2821 2822/* 2823 * This function implements the DIF emulator's variable lookups. The emulator 2824 * passes a reserved variable identifier and optional built-in array index. 2825 */ 2826static uint64_t 2827dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2828 uint64_t ndx) 2829{ 2830 /* 2831 * If we're accessing one of the uncached arguments, we'll turn this 2832 * into a reference in the args array. 2833 */ 2834 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2835 ndx = v - DIF_VAR_ARG0; 2836 v = DIF_VAR_ARGS; 2837 } 2838 2839 switch (v) { 2840 case DIF_VAR_ARGS: 2841 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2842 if (ndx >= sizeof (mstate->dtms_arg) / 2843 sizeof (mstate->dtms_arg[0])) { 2844 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2845 dtrace_provider_t *pv; 2846 uint64_t val; 2847 2848 pv = mstate->dtms_probe->dtpr_provider; 2849 if (pv->dtpv_pops.dtps_getargval != NULL) 2850 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2851 mstate->dtms_probe->dtpr_id, 2852 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2853 else 2854 val = dtrace_getarg(ndx, aframes); 2855 2856 /* 2857 * This is regrettably required to keep the compiler 2858 * from tail-optimizing the call to dtrace_getarg(). 2859 * The condition always evaluates to true, but the 2860 * compiler has no way of figuring that out a priori. 2861 * (None of this would be necessary if the compiler 2862 * could be relied upon to _always_ tail-optimize 2863 * the call to dtrace_getarg() -- but it can't.) 2864 */ 2865 if (mstate->dtms_probe != NULL) 2866 return (val); 2867 2868 ASSERT(0); 2869 } 2870 2871 return (mstate->dtms_arg[ndx]); 2872 2873#if defined(sun) 2874 case DIF_VAR_UREGS: { 2875 klwp_t *lwp; 2876 2877 if (!dtrace_priv_proc(state)) 2878 return (0); 2879 2880 if ((lwp = curthread->t_lwp) == NULL) { 2881 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2882 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 2883 return (0); 2884 } 2885 2886 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2887 return (0); 2888 } 2889#else 2890 case DIF_VAR_UREGS: { 2891 struct trapframe *tframe; 2892 2893 if (!dtrace_priv_proc(state)) 2894 return (0); 2895 2896 if ((tframe = curthread->td_frame) == NULL) { 2897 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2898 cpu_core[curcpu].cpuc_dtrace_illval = 0; 2899 return (0); 2900 } 2901 2902 return (dtrace_getreg(tframe, ndx)); 2903 } 2904#endif 2905 2906 case DIF_VAR_CURTHREAD: 2907 if (!dtrace_priv_kernel(state)) 2908 return (0); 2909 return ((uint64_t)(uintptr_t)curthread); 2910 2911 case DIF_VAR_TIMESTAMP: 2912 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2913 mstate->dtms_timestamp = dtrace_gethrtime(); 2914 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2915 } 2916 return (mstate->dtms_timestamp); 2917 2918 case DIF_VAR_VTIMESTAMP: 2919 ASSERT(dtrace_vtime_references != 0); 2920 return (curthread->t_dtrace_vtime); 2921 2922 case DIF_VAR_WALLTIMESTAMP: 2923 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2924 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2925 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2926 } 2927 return (mstate->dtms_walltimestamp); 2928 2929#if defined(sun) 2930 case DIF_VAR_IPL: 2931 if (!dtrace_priv_kernel(state)) 2932 return (0); 2933 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2934 mstate->dtms_ipl = dtrace_getipl(); 2935 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2936 } 2937 return (mstate->dtms_ipl); 2938#endif 2939 2940 case DIF_VAR_EPID: 2941 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2942 return (mstate->dtms_epid); 2943 2944 case DIF_VAR_ID: 2945 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2946 return (mstate->dtms_probe->dtpr_id); 2947 2948 case DIF_VAR_STACKDEPTH: 2949 if (!dtrace_priv_kernel(state)) 2950 return (0); 2951 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2952 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2953 2954 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2955 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2956 } 2957 return (mstate->dtms_stackdepth); 2958 2959 case DIF_VAR_USTACKDEPTH: 2960 if (!dtrace_priv_proc(state)) 2961 return (0); 2962 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2963 /* 2964 * See comment in DIF_VAR_PID. 2965 */ 2966 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2967 CPU_ON_INTR(CPU)) { 2968 mstate->dtms_ustackdepth = 0; 2969 } else { 2970 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2971 mstate->dtms_ustackdepth = 2972 dtrace_getustackdepth(); 2973 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2974 } 2975 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2976 } 2977 return (mstate->dtms_ustackdepth); 2978 2979 case DIF_VAR_CALLER: 2980 if (!dtrace_priv_kernel(state)) 2981 return (0); 2982 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2983 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2984 2985 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2986 /* 2987 * If this is an unanchored probe, we are 2988 * required to go through the slow path: 2989 * dtrace_caller() only guarantees correct 2990 * results for anchored probes. 2991 */ 2992 pc_t caller[2] = {0, 0}; 2993 2994 dtrace_getpcstack(caller, 2, aframes, 2995 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2996 mstate->dtms_caller = caller[1]; 2997 } else if ((mstate->dtms_caller = 2998 dtrace_caller(aframes)) == -1) { 2999 /* 3000 * We have failed to do this the quick way; 3001 * we must resort to the slower approach of 3002 * calling dtrace_getpcstack(). 3003 */ 3004 pc_t caller = 0; 3005 3006 dtrace_getpcstack(&caller, 1, aframes, NULL); 3007 mstate->dtms_caller = caller; 3008 } 3009 3010 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3011 } 3012 return (mstate->dtms_caller); 3013 3014 case DIF_VAR_UCALLER: 3015 if (!dtrace_priv_proc(state)) 3016 return (0); 3017 3018 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3019 uint64_t ustack[3]; 3020 3021 /* 3022 * dtrace_getupcstack() fills in the first uint64_t 3023 * with the current PID. The second uint64_t will 3024 * be the program counter at user-level. The third 3025 * uint64_t will contain the caller, which is what 3026 * we're after. 3027 */ 3028 ustack[2] = 0; 3029 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3030 dtrace_getupcstack(ustack, 3); 3031 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3032 mstate->dtms_ucaller = ustack[2]; 3033 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3034 } 3035 3036 return (mstate->dtms_ucaller); 3037 3038 case DIF_VAR_PROBEPROV: 3039 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3040 return (dtrace_dif_varstr( 3041 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3042 state, mstate)); 3043 3044 case DIF_VAR_PROBEMOD: 3045 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3046 return (dtrace_dif_varstr( 3047 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3048 state, mstate)); 3049 3050 case DIF_VAR_PROBEFUNC: 3051 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3052 return (dtrace_dif_varstr( 3053 (uintptr_t)mstate->dtms_probe->dtpr_func, 3054 state, mstate)); 3055 3056 case DIF_VAR_PROBENAME: 3057 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3058 return (dtrace_dif_varstr( 3059 (uintptr_t)mstate->dtms_probe->dtpr_name, 3060 state, mstate)); 3061 3062 case DIF_VAR_PID: 3063 if (!dtrace_priv_proc(state)) 3064 return (0); 3065 3066#if defined(sun) 3067 /* 3068 * Note that we are assuming that an unanchored probe is 3069 * always due to a high-level interrupt. (And we're assuming 3070 * that there is only a single high level interrupt.) 3071 */ 3072 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3073 return (pid0.pid_id); 3074 3075 /* 3076 * It is always safe to dereference one's own t_procp pointer: 3077 * it always points to a valid, allocated proc structure. 3078 * Further, it is always safe to dereference the p_pidp member 3079 * of one's own proc structure. (These are truisms becuase 3080 * threads and processes don't clean up their own state -- 3081 * they leave that task to whomever reaps them.) 3082 */ 3083 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3084#else 3085 return ((uint64_t)curproc->p_pid); 3086#endif 3087 3088 case DIF_VAR_PPID: 3089 if (!dtrace_priv_proc(state)) 3090 return (0); 3091 3092#if defined(sun) 3093 /* 3094 * See comment in DIF_VAR_PID. 3095 */ 3096 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3097 return (pid0.pid_id); 3098 3099 /* 3100 * It is always safe to dereference one's own t_procp pointer: 3101 * it always points to a valid, allocated proc structure. 3102 * (This is true because threads don't clean up their own 3103 * state -- they leave that task to whomever reaps them.) 3104 */ 3105 return ((uint64_t)curthread->t_procp->p_ppid); 3106#else 3107 return ((uint64_t)curproc->p_pptr->p_pid); 3108#endif 3109 3110 case DIF_VAR_TID: 3111#if defined(sun) 3112 /* 3113 * See comment in DIF_VAR_PID. 3114 */ 3115 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3116 return (0); 3117#endif 3118 3119 return ((uint64_t)curthread->t_tid); 3120 3121 case DIF_VAR_EXECARGS: { 3122 struct pargs *p_args = curthread->td_proc->p_args; 3123 3124 if (p_args == NULL) 3125 return(0); 3126 3127 return (dtrace_dif_varstrz( 3128 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3129 } 3130 3131 case DIF_VAR_EXECNAME: 3132#if defined(sun) 3133 if (!dtrace_priv_proc(state)) 3134 return (0); 3135 3136 /* 3137 * See comment in DIF_VAR_PID. 3138 */ 3139 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3140 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3141 3142 /* 3143 * It is always safe to dereference one's own t_procp pointer: 3144 * it always points to a valid, allocated proc structure. 3145 * (This is true because threads don't clean up their own 3146 * state -- they leave that task to whomever reaps them.) 3147 */ 3148 return (dtrace_dif_varstr( 3149 (uintptr_t)curthread->t_procp->p_user.u_comm, 3150 state, mstate)); 3151#else 3152 return (dtrace_dif_varstr( 3153 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3154#endif 3155 3156 case DIF_VAR_ZONENAME: 3157#if defined(sun) 3158 if (!dtrace_priv_proc(state)) 3159 return (0); 3160 3161 /* 3162 * See comment in DIF_VAR_PID. 3163 */ 3164 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3165 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3166 3167 /* 3168 * It is always safe to dereference one's own t_procp pointer: 3169 * it always points to a valid, allocated proc structure. 3170 * (This is true because threads don't clean up their own 3171 * state -- they leave that task to whomever reaps them.) 3172 */ 3173 return (dtrace_dif_varstr( 3174 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3175 state, mstate)); 3176#else 3177 return (0); 3178#endif 3179 3180 case DIF_VAR_UID: 3181 if (!dtrace_priv_proc(state)) 3182 return (0); 3183 3184#if defined(sun) 3185 /* 3186 * See comment in DIF_VAR_PID. 3187 */ 3188 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3189 return ((uint64_t)p0.p_cred->cr_uid); 3190#endif 3191 3192 /* 3193 * It is always safe to dereference one's own t_procp pointer: 3194 * it always points to a valid, allocated proc structure. 3195 * (This is true because threads don't clean up their own 3196 * state -- they leave that task to whomever reaps them.) 3197 * 3198 * Additionally, it is safe to dereference one's own process 3199 * credential, since this is never NULL after process birth. 3200 */ 3201 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3202 3203 case DIF_VAR_GID: 3204 if (!dtrace_priv_proc(state)) 3205 return (0); 3206 3207#if defined(sun) 3208 /* 3209 * See comment in DIF_VAR_PID. 3210 */ 3211 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3212 return ((uint64_t)p0.p_cred->cr_gid); 3213#endif 3214 3215 /* 3216 * It is always safe to dereference one's own t_procp pointer: 3217 * it always points to a valid, allocated proc structure. 3218 * (This is true because threads don't clean up their own 3219 * state -- they leave that task to whomever reaps them.) 3220 * 3221 * Additionally, it is safe to dereference one's own process 3222 * credential, since this is never NULL after process birth. 3223 */ 3224 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3225 3226 case DIF_VAR_ERRNO: { 3227#if defined(sun) 3228 klwp_t *lwp; 3229 if (!dtrace_priv_proc(state)) 3230 return (0); 3231 3232 /* 3233 * See comment in DIF_VAR_PID. 3234 */ 3235 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3236 return (0); 3237 3238 /* 3239 * It is always safe to dereference one's own t_lwp pointer in 3240 * the event that this pointer is non-NULL. (This is true 3241 * because threads and lwps don't clean up their own state -- 3242 * they leave that task to whomever reaps them.) 3243 */ 3244 if ((lwp = curthread->t_lwp) == NULL) 3245 return (0); 3246 3247 return ((uint64_t)lwp->lwp_errno); 3248#else 3249 return (curthread->td_errno); 3250#endif 3251 } 3252#if !defined(sun) 3253 case DIF_VAR_CPU: { 3254 return curcpu; 3255 } 3256#endif 3257 default: 3258 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3259 return (0); 3260 } 3261} 3262 3263/* 3264 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 3265 * Notice that we don't bother validating the proper number of arguments or 3266 * their types in the tuple stack. This isn't needed because all argument 3267 * interpretation is safe because of our load safety -- the worst that can 3268 * happen is that a bogus program can obtain bogus results. 3269 */ 3270static void 3271dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 3272 dtrace_key_t *tupregs, int nargs, 3273 dtrace_mstate_t *mstate, dtrace_state_t *state) 3274{ 3275 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 3276 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 3277 dtrace_vstate_t *vstate = &state->dts_vstate; 3278 3279#if defined(sun) 3280 union { 3281 mutex_impl_t mi; 3282 uint64_t mx; 3283 } m; 3284 3285 union { 3286 krwlock_t ri; 3287 uintptr_t rw; 3288 } r; 3289#else 3290 struct thread *lowner; 3291 union { 3292 struct lock_object *li; 3293 uintptr_t lx; 3294 } l; 3295#endif 3296 3297 switch (subr) { 3298 case DIF_SUBR_RAND: 3299 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 3300 break; 3301 3302#if defined(sun) 3303 case DIF_SUBR_MUTEX_OWNED: 3304 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3305 mstate, vstate)) { 3306 regs[rd] = 0; 3307 break; 3308 } 3309 3310 m.mx = dtrace_load64(tupregs[0].dttk_value); 3311 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3312 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3313 else 3314 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 3315 break; 3316 3317 case DIF_SUBR_MUTEX_OWNER: 3318 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3319 mstate, vstate)) { 3320 regs[rd] = 0; 3321 break; 3322 } 3323 3324 m.mx = dtrace_load64(tupregs[0].dttk_value); 3325 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3326 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3327 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3328 else 3329 regs[rd] = 0; 3330 break; 3331 3332 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3333 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3334 mstate, vstate)) { 3335 regs[rd] = 0; 3336 break; 3337 } 3338 3339 m.mx = dtrace_load64(tupregs[0].dttk_value); 3340 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3341 break; 3342 3343 case DIF_SUBR_MUTEX_TYPE_SPIN: 3344 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3345 mstate, vstate)) { 3346 regs[rd] = 0; 3347 break; 3348 } 3349 3350 m.mx = dtrace_load64(tupregs[0].dttk_value); 3351 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3352 break; 3353 3354 case DIF_SUBR_RW_READ_HELD: { 3355 uintptr_t tmp; 3356 3357 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3358 mstate, vstate)) { 3359 regs[rd] = 0; 3360 break; 3361 } 3362 3363 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3364 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 3365 break; 3366 } 3367 3368 case DIF_SUBR_RW_WRITE_HELD: 3369 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3370 mstate, vstate)) { 3371 regs[rd] = 0; 3372 break; 3373 } 3374 3375 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3376 regs[rd] = _RW_WRITE_HELD(&r.ri); 3377 break; 3378 3379 case DIF_SUBR_RW_ISWRITER: 3380 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3381 mstate, vstate)) { 3382 regs[rd] = 0; 3383 break; 3384 } 3385 3386 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3387 regs[rd] = _RW_ISWRITER(&r.ri); 3388 break; 3389 3390#else 3391 case DIF_SUBR_MUTEX_OWNED: 3392 if (!dtrace_canload(tupregs[0].dttk_value, 3393 sizeof (struct lock_object), mstate, vstate)) { 3394 regs[rd] = 0; 3395 break; 3396 } 3397 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3398 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 3399 break; 3400 3401 case DIF_SUBR_MUTEX_OWNER: 3402 if (!dtrace_canload(tupregs[0].dttk_value, 3403 sizeof (struct lock_object), mstate, vstate)) { 3404 regs[rd] = 0; 3405 break; 3406 } 3407 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3408 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 3409 regs[rd] = (uintptr_t)lowner; 3410 break; 3411 3412 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3413 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 3414 mstate, vstate)) { 3415 regs[rd] = 0; 3416 break; 3417 } 3418 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3419 /* XXX - should be only LC_SLEEPABLE? */ 3420 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & 3421 (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0; 3422 break; 3423 3424 case DIF_SUBR_MUTEX_TYPE_SPIN: 3425 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 3426 mstate, vstate)) { 3427 regs[rd] = 0; 3428 break; 3429 } 3430 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3431 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 3432 break; 3433 3434 case DIF_SUBR_RW_READ_HELD: 3435 case DIF_SUBR_SX_SHARED_HELD: 3436 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3437 mstate, vstate)) { 3438 regs[rd] = 0; 3439 break; 3440 } 3441 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 3442 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 3443 lowner == NULL; 3444 break; 3445 3446 case DIF_SUBR_RW_WRITE_HELD: 3447 case DIF_SUBR_SX_EXCLUSIVE_HELD: 3448 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3449 mstate, vstate)) { 3450 regs[rd] = 0; 3451 break; 3452 } 3453 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 3454 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 3455 regs[rd] = (lowner == curthread); 3456 break; 3457 3458 case DIF_SUBR_RW_ISWRITER: 3459 case DIF_SUBR_SX_ISEXCLUSIVE: 3460 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3461 mstate, vstate)) { 3462 regs[rd] = 0; 3463 break; 3464 } 3465 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 3466 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 3467 lowner != NULL; 3468 break; 3469#endif /* ! defined(sun) */ 3470 3471 case DIF_SUBR_BCOPY: { 3472 /* 3473 * We need to be sure that the destination is in the scratch 3474 * region -- no other region is allowed. 3475 */ 3476 uintptr_t src = tupregs[0].dttk_value; 3477 uintptr_t dest = tupregs[1].dttk_value; 3478 size_t size = tupregs[2].dttk_value; 3479 3480 if (!dtrace_inscratch(dest, size, mstate)) { 3481 *flags |= CPU_DTRACE_BADADDR; 3482 *illval = regs[rd]; 3483 break; 3484 } 3485 3486 if (!dtrace_canload(src, size, mstate, vstate)) { 3487 regs[rd] = 0; 3488 break; 3489 } 3490 3491 dtrace_bcopy((void *)src, (void *)dest, size); 3492 break; 3493 } 3494 3495 case DIF_SUBR_ALLOCA: 3496 case DIF_SUBR_COPYIN: { 3497 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 3498 uint64_t size = 3499 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 3500 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 3501 3502 /* 3503 * This action doesn't require any credential checks since 3504 * probes will not activate in user contexts to which the 3505 * enabling user does not have permissions. 3506 */ 3507 3508 /* 3509 * Rounding up the user allocation size could have overflowed 3510 * a large, bogus allocation (like -1ULL) to 0. 3511 */ 3512 if (scratch_size < size || 3513 !DTRACE_INSCRATCH(mstate, scratch_size)) { 3514 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3515 regs[rd] = 0; 3516 break; 3517 } 3518 3519 if (subr == DIF_SUBR_COPYIN) { 3520 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3521 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3522 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3523 } 3524 3525 mstate->dtms_scratch_ptr += scratch_size; 3526 regs[rd] = dest; 3527 break; 3528 } 3529 3530 case DIF_SUBR_COPYINTO: { 3531 uint64_t size = tupregs[1].dttk_value; 3532 uintptr_t dest = tupregs[2].dttk_value; 3533 3534 /* 3535 * This action doesn't require any credential checks since 3536 * probes will not activate in user contexts to which the 3537 * enabling user does not have permissions. 3538 */ 3539 if (!dtrace_inscratch(dest, size, mstate)) { 3540 *flags |= CPU_DTRACE_BADADDR; 3541 *illval = regs[rd]; 3542 break; 3543 } 3544 3545 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3546 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3547 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3548 break; 3549 } 3550 3551 case DIF_SUBR_COPYINSTR: { 3552 uintptr_t dest = mstate->dtms_scratch_ptr; 3553 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3554 3555 if (nargs > 1 && tupregs[1].dttk_value < size) 3556 size = tupregs[1].dttk_value + 1; 3557 3558 /* 3559 * This action doesn't require any credential checks since 3560 * probes will not activate in user contexts to which the 3561 * enabling user does not have permissions. 3562 */ 3563 if (!DTRACE_INSCRATCH(mstate, size)) { 3564 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3565 regs[rd] = 0; 3566 break; 3567 } 3568 3569 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3570 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 3571 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3572 3573 ((char *)dest)[size - 1] = '\0'; 3574 mstate->dtms_scratch_ptr += size; 3575 regs[rd] = dest; 3576 break; 3577 } 3578 3579#if defined(sun) 3580 case DIF_SUBR_MSGSIZE: 3581 case DIF_SUBR_MSGDSIZE: { 3582 uintptr_t baddr = tupregs[0].dttk_value, daddr; 3583 uintptr_t wptr, rptr; 3584 size_t count = 0; 3585 int cont = 0; 3586 3587 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 3588 3589 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 3590 vstate)) { 3591 regs[rd] = 0; 3592 break; 3593 } 3594 3595 wptr = dtrace_loadptr(baddr + 3596 offsetof(mblk_t, b_wptr)); 3597 3598 rptr = dtrace_loadptr(baddr + 3599 offsetof(mblk_t, b_rptr)); 3600 3601 if (wptr < rptr) { 3602 *flags |= CPU_DTRACE_BADADDR; 3603 *illval = tupregs[0].dttk_value; 3604 break; 3605 } 3606 3607 daddr = dtrace_loadptr(baddr + 3608 offsetof(mblk_t, b_datap)); 3609 3610 baddr = dtrace_loadptr(baddr + 3611 offsetof(mblk_t, b_cont)); 3612 3613 /* 3614 * We want to prevent against denial-of-service here, 3615 * so we're only going to search the list for 3616 * dtrace_msgdsize_max mblks. 3617 */ 3618 if (cont++ > dtrace_msgdsize_max) { 3619 *flags |= CPU_DTRACE_ILLOP; 3620 break; 3621 } 3622 3623 if (subr == DIF_SUBR_MSGDSIZE) { 3624 if (dtrace_load8(daddr + 3625 offsetof(dblk_t, db_type)) != M_DATA) 3626 continue; 3627 } 3628 3629 count += wptr - rptr; 3630 } 3631 3632 if (!(*flags & CPU_DTRACE_FAULT)) 3633 regs[rd] = count; 3634 3635 break; 3636 } 3637#endif 3638 3639 case DIF_SUBR_PROGENYOF: { 3640 pid_t pid = tupregs[0].dttk_value; 3641 proc_t *p; 3642 int rval = 0; 3643 3644 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3645 3646 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 3647#if defined(sun) 3648 if (p->p_pidp->pid_id == pid) { 3649#else 3650 if (p->p_pid == pid) { 3651#endif 3652 rval = 1; 3653 break; 3654 } 3655 } 3656 3657 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3658 3659 regs[rd] = rval; 3660 break; 3661 } 3662 3663 case DIF_SUBR_SPECULATION: 3664 regs[rd] = dtrace_speculation(state); 3665 break; 3666 3667 case DIF_SUBR_COPYOUT: { 3668 uintptr_t kaddr = tupregs[0].dttk_value; 3669 uintptr_t uaddr = tupregs[1].dttk_value; 3670 uint64_t size = tupregs[2].dttk_value; 3671 3672 if (!dtrace_destructive_disallow && 3673 dtrace_priv_proc_control(state) && 3674 !dtrace_istoxic(kaddr, size)) { 3675 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3676 dtrace_copyout(kaddr, uaddr, size, flags); 3677 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3678 } 3679 break; 3680 } 3681 3682 case DIF_SUBR_COPYOUTSTR: { 3683 uintptr_t kaddr = tupregs[0].dttk_value; 3684 uintptr_t uaddr = tupregs[1].dttk_value; 3685 uint64_t size = tupregs[2].dttk_value; 3686 3687 if (!dtrace_destructive_disallow && 3688 dtrace_priv_proc_control(state) && 3689 !dtrace_istoxic(kaddr, size)) { 3690 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3691 dtrace_copyoutstr(kaddr, uaddr, size, flags); 3692 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3693 } 3694 break; 3695 } 3696 3697 case DIF_SUBR_STRLEN: { 3698 size_t sz; 3699 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 3700 sz = dtrace_strlen((char *)addr, 3701 state->dts_options[DTRACEOPT_STRSIZE]); 3702 3703 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 3704 regs[rd] = 0; 3705 break; 3706 } 3707 3708 regs[rd] = sz; 3709 3710 break; 3711 } 3712 3713 case DIF_SUBR_STRCHR: 3714 case DIF_SUBR_STRRCHR: { 3715 /* 3716 * We're going to iterate over the string looking for the 3717 * specified character. We will iterate until we have reached 3718 * the string length or we have found the character. If this 3719 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 3720 * of the specified character instead of the first. 3721 */ 3722 uintptr_t saddr = tupregs[0].dttk_value; 3723 uintptr_t addr = tupregs[0].dttk_value; 3724 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 3725 char c, target = (char)tupregs[1].dttk_value; 3726 3727 for (regs[rd] = 0; addr < limit; addr++) { 3728 if ((c = dtrace_load8(addr)) == target) { 3729 regs[rd] = addr; 3730 3731 if (subr == DIF_SUBR_STRCHR) 3732 break; 3733 } 3734 3735 if (c == '\0') 3736 break; 3737 } 3738 3739 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 3740 regs[rd] = 0; 3741 break; 3742 } 3743 3744 break; 3745 } 3746 3747 case DIF_SUBR_STRSTR: 3748 case DIF_SUBR_INDEX: 3749 case DIF_SUBR_RINDEX: { 3750 /* 3751 * We're going to iterate over the string looking for the 3752 * specified string. We will iterate until we have reached 3753 * the string length or we have found the string. (Yes, this 3754 * is done in the most naive way possible -- but considering 3755 * that the string we're searching for is likely to be 3756 * relatively short, the complexity of Rabin-Karp or similar 3757 * hardly seems merited.) 3758 */ 3759 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 3760 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 3761 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3762 size_t len = dtrace_strlen(addr, size); 3763 size_t sublen = dtrace_strlen(substr, size); 3764 char *limit = addr + len, *orig = addr; 3765 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 3766 int inc = 1; 3767 3768 regs[rd] = notfound; 3769 3770 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 3771 regs[rd] = 0; 3772 break; 3773 } 3774 3775 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 3776 vstate)) { 3777 regs[rd] = 0; 3778 break; 3779 } 3780 3781 /* 3782 * strstr() and index()/rindex() have similar semantics if 3783 * both strings are the empty string: strstr() returns a 3784 * pointer to the (empty) string, and index() and rindex() 3785 * both return index 0 (regardless of any position argument). 3786 */ 3787 if (sublen == 0 && len == 0) { 3788 if (subr == DIF_SUBR_STRSTR) 3789 regs[rd] = (uintptr_t)addr; 3790 else 3791 regs[rd] = 0; 3792 break; 3793 } 3794 3795 if (subr != DIF_SUBR_STRSTR) { 3796 if (subr == DIF_SUBR_RINDEX) { 3797 limit = orig - 1; 3798 addr += len; 3799 inc = -1; 3800 } 3801 3802 /* 3803 * Both index() and rindex() take an optional position 3804 * argument that denotes the starting position. 3805 */ 3806 if (nargs == 3) { 3807 int64_t pos = (int64_t)tupregs[2].dttk_value; 3808 3809 /* 3810 * If the position argument to index() is 3811 * negative, Perl implicitly clamps it at 3812 * zero. This semantic is a little surprising 3813 * given the special meaning of negative 3814 * positions to similar Perl functions like 3815 * substr(), but it appears to reflect a 3816 * notion that index() can start from a 3817 * negative index and increment its way up to 3818 * the string. Given this notion, Perl's 3819 * rindex() is at least self-consistent in 3820 * that it implicitly clamps positions greater 3821 * than the string length to be the string 3822 * length. Where Perl completely loses 3823 * coherence, however, is when the specified 3824 * substring is the empty string (""). In 3825 * this case, even if the position is 3826 * negative, rindex() returns 0 -- and even if 3827 * the position is greater than the length, 3828 * index() returns the string length. These 3829 * semantics violate the notion that index() 3830 * should never return a value less than the 3831 * specified position and that rindex() should 3832 * never return a value greater than the 3833 * specified position. (One assumes that 3834 * these semantics are artifacts of Perl's 3835 * implementation and not the results of 3836 * deliberate design -- it beggars belief that 3837 * even Larry Wall could desire such oddness.) 3838 * While in the abstract one would wish for 3839 * consistent position semantics across 3840 * substr(), index() and rindex() -- or at the 3841 * very least self-consistent position 3842 * semantics for index() and rindex() -- we 3843 * instead opt to keep with the extant Perl 3844 * semantics, in all their broken glory. (Do 3845 * we have more desire to maintain Perl's 3846 * semantics than Perl does? Probably.) 3847 */ 3848 if (subr == DIF_SUBR_RINDEX) { 3849 if (pos < 0) { 3850 if (sublen == 0) 3851 regs[rd] = 0; 3852 break; 3853 } 3854 3855 if (pos > len) 3856 pos = len; 3857 } else { 3858 if (pos < 0) 3859 pos = 0; 3860 3861 if (pos >= len) { 3862 if (sublen == 0) 3863 regs[rd] = len; 3864 break; 3865 } 3866 } 3867 3868 addr = orig + pos; 3869 } 3870 } 3871 3872 for (regs[rd] = notfound; addr != limit; addr += inc) { 3873 if (dtrace_strncmp(addr, substr, sublen) == 0) { 3874 if (subr != DIF_SUBR_STRSTR) { 3875 /* 3876 * As D index() and rindex() are 3877 * modeled on Perl (and not on awk), 3878 * we return a zero-based (and not a 3879 * one-based) index. (For you Perl 3880 * weenies: no, we're not going to add 3881 * $[ -- and shouldn't you be at a con 3882 * or something?) 3883 */ 3884 regs[rd] = (uintptr_t)(addr - orig); 3885 break; 3886 } 3887 3888 ASSERT(subr == DIF_SUBR_STRSTR); 3889 regs[rd] = (uintptr_t)addr; 3890 break; 3891 } 3892 } 3893 3894 break; 3895 } 3896 3897 case DIF_SUBR_STRTOK: { 3898 uintptr_t addr = tupregs[0].dttk_value; 3899 uintptr_t tokaddr = tupregs[1].dttk_value; 3900 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3901 uintptr_t limit, toklimit = tokaddr + size; 3902 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 3903 char *dest = (char *)mstate->dtms_scratch_ptr; 3904 int i; 3905 3906 /* 3907 * Check both the token buffer and (later) the input buffer, 3908 * since both could be non-scratch addresses. 3909 */ 3910 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 3911 regs[rd] = 0; 3912 break; 3913 } 3914 3915 if (!DTRACE_INSCRATCH(mstate, size)) { 3916 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3917 regs[rd] = 0; 3918 break; 3919 } 3920 3921 if (addr == 0) { 3922 /* 3923 * If the address specified is NULL, we use our saved 3924 * strtok pointer from the mstate. Note that this 3925 * means that the saved strtok pointer is _only_ 3926 * valid within multiple enablings of the same probe -- 3927 * it behaves like an implicit clause-local variable. 3928 */ 3929 addr = mstate->dtms_strtok; 3930 } else { 3931 /* 3932 * If the user-specified address is non-NULL we must 3933 * access check it. This is the only time we have 3934 * a chance to do so, since this address may reside 3935 * in the string table of this clause-- future calls 3936 * (when we fetch addr from mstate->dtms_strtok) 3937 * would fail this access check. 3938 */ 3939 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 3940 regs[rd] = 0; 3941 break; 3942 } 3943 } 3944 3945 /* 3946 * First, zero the token map, and then process the token 3947 * string -- setting a bit in the map for every character 3948 * found in the token string. 3949 */ 3950 for (i = 0; i < sizeof (tokmap); i++) 3951 tokmap[i] = 0; 3952 3953 for (; tokaddr < toklimit; tokaddr++) { 3954 if ((c = dtrace_load8(tokaddr)) == '\0') 3955 break; 3956 3957 ASSERT((c >> 3) < sizeof (tokmap)); 3958 tokmap[c >> 3] |= (1 << (c & 0x7)); 3959 } 3960 3961 for (limit = addr + size; addr < limit; addr++) { 3962 /* 3963 * We're looking for a character that is _not_ contained 3964 * in the token string. 3965 */ 3966 if ((c = dtrace_load8(addr)) == '\0') 3967 break; 3968 3969 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3970 break; 3971 } 3972 3973 if (c == '\0') { 3974 /* 3975 * We reached the end of the string without finding 3976 * any character that was not in the token string. 3977 * We return NULL in this case, and we set the saved 3978 * address to NULL as well. 3979 */ 3980 regs[rd] = 0; 3981 mstate->dtms_strtok = 0; 3982 break; 3983 } 3984 3985 /* 3986 * From here on, we're copying into the destination string. 3987 */ 3988 for (i = 0; addr < limit && i < size - 1; addr++) { 3989 if ((c = dtrace_load8(addr)) == '\0') 3990 break; 3991 3992 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3993 break; 3994 3995 ASSERT(i < size); 3996 dest[i++] = c; 3997 } 3998 3999 ASSERT(i < size); 4000 dest[i] = '\0'; 4001 regs[rd] = (uintptr_t)dest; 4002 mstate->dtms_scratch_ptr += size; 4003 mstate->dtms_strtok = addr; 4004 break; 4005 } 4006 4007 case DIF_SUBR_SUBSTR: { 4008 uintptr_t s = tupregs[0].dttk_value; 4009 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4010 char *d = (char *)mstate->dtms_scratch_ptr; 4011 int64_t index = (int64_t)tupregs[1].dttk_value; 4012 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4013 size_t len = dtrace_strlen((char *)s, size); 4014 int64_t i = 0; 4015 4016 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4017 regs[rd] = 0; 4018 break; 4019 } 4020 4021 if (!DTRACE_INSCRATCH(mstate, size)) { 4022 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4023 regs[rd] = 0; 4024 break; 4025 } 4026 4027 if (nargs <= 2) 4028 remaining = (int64_t)size; 4029 4030 if (index < 0) { 4031 index += len; 4032 4033 if (index < 0 && index + remaining > 0) { 4034 remaining += index; 4035 index = 0; 4036 } 4037 } 4038 4039 if (index >= len || index < 0) { 4040 remaining = 0; 4041 } else if (remaining < 0) { 4042 remaining += len - index; 4043 } else if (index + remaining > size) { 4044 remaining = size - index; 4045 } 4046 4047 for (i = 0; i < remaining; i++) { 4048 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4049 break; 4050 } 4051 4052 d[i] = '\0'; 4053 4054 mstate->dtms_scratch_ptr += size; 4055 regs[rd] = (uintptr_t)d; 4056 break; 4057 } 4058 4059 case DIF_SUBR_TOUPPER: 4060 case DIF_SUBR_TOLOWER: { 4061 uintptr_t s = tupregs[0].dttk_value; 4062 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4063 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4064 size_t len = dtrace_strlen((char *)s, size); 4065 char lower, upper, convert; 4066 int64_t i; 4067 4068 if (subr == DIF_SUBR_TOUPPER) { 4069 lower = 'a'; 4070 upper = 'z'; 4071 convert = 'A'; 4072 } else { 4073 lower = 'A'; 4074 upper = 'Z'; 4075 convert = 'a'; 4076 } 4077 4078 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4079 regs[rd] = 0; 4080 break; 4081 } 4082 4083 if (!DTRACE_INSCRATCH(mstate, size)) { 4084 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4085 regs[rd] = 0; 4086 break; 4087 } 4088 4089 for (i = 0; i < size - 1; i++) { 4090 if ((c = dtrace_load8(s + i)) == '\0') 4091 break; 4092 4093 if (c >= lower && c <= upper) 4094 c = convert + (c - lower); 4095 4096 dest[i] = c; 4097 } 4098 4099 ASSERT(i < size); 4100 dest[i] = '\0'; 4101 regs[rd] = (uintptr_t)dest; 4102 mstate->dtms_scratch_ptr += size; 4103 break; 4104 } 4105 4106#if defined(sun) 4107 case DIF_SUBR_GETMAJOR: 4108#ifdef _LP64 4109 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 4110#else 4111 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 4112#endif 4113 break; 4114 4115 case DIF_SUBR_GETMINOR: 4116#ifdef _LP64 4117 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 4118#else 4119 regs[rd] = tupregs[0].dttk_value & MAXMIN; 4120#endif 4121 break; 4122 4123 case DIF_SUBR_DDI_PATHNAME: { 4124 /* 4125 * This one is a galactic mess. We are going to roughly 4126 * emulate ddi_pathname(), but it's made more complicated 4127 * by the fact that we (a) want to include the minor name and 4128 * (b) must proceed iteratively instead of recursively. 4129 */ 4130 uintptr_t dest = mstate->dtms_scratch_ptr; 4131 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4132 char *start = (char *)dest, *end = start + size - 1; 4133 uintptr_t daddr = tupregs[0].dttk_value; 4134 int64_t minor = (int64_t)tupregs[1].dttk_value; 4135 char *s; 4136 int i, len, depth = 0; 4137 4138 /* 4139 * Due to all the pointer jumping we do and context we must 4140 * rely upon, we just mandate that the user must have kernel 4141 * read privileges to use this routine. 4142 */ 4143 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 4144 *flags |= CPU_DTRACE_KPRIV; 4145 *illval = daddr; 4146 regs[rd] = 0; 4147 } 4148 4149 if (!DTRACE_INSCRATCH(mstate, size)) { 4150 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4151 regs[rd] = 0; 4152 break; 4153 } 4154 4155 *end = '\0'; 4156 4157 /* 4158 * We want to have a name for the minor. In order to do this, 4159 * we need to walk the minor list from the devinfo. We want 4160 * to be sure that we don't infinitely walk a circular list, 4161 * so we check for circularity by sending a scout pointer 4162 * ahead two elements for every element that we iterate over; 4163 * if the list is circular, these will ultimately point to the 4164 * same element. You may recognize this little trick as the 4165 * answer to a stupid interview question -- one that always 4166 * seems to be asked by those who had to have it laboriously 4167 * explained to them, and who can't even concisely describe 4168 * the conditions under which one would be forced to resort to 4169 * this technique. Needless to say, those conditions are 4170 * found here -- and probably only here. Is this the only use 4171 * of this infamous trick in shipping, production code? If it 4172 * isn't, it probably should be... 4173 */ 4174 if (minor != -1) { 4175 uintptr_t maddr = dtrace_loadptr(daddr + 4176 offsetof(struct dev_info, devi_minor)); 4177 4178 uintptr_t next = offsetof(struct ddi_minor_data, next); 4179 uintptr_t name = offsetof(struct ddi_minor_data, 4180 d_minor) + offsetof(struct ddi_minor, name); 4181 uintptr_t dev = offsetof(struct ddi_minor_data, 4182 d_minor) + offsetof(struct ddi_minor, dev); 4183 uintptr_t scout; 4184 4185 if (maddr != NULL) 4186 scout = dtrace_loadptr(maddr + next); 4187 4188 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4189 uint64_t m; 4190#ifdef _LP64 4191 m = dtrace_load64(maddr + dev) & MAXMIN64; 4192#else 4193 m = dtrace_load32(maddr + dev) & MAXMIN; 4194#endif 4195 if (m != minor) { 4196 maddr = dtrace_loadptr(maddr + next); 4197 4198 if (scout == NULL) 4199 continue; 4200 4201 scout = dtrace_loadptr(scout + next); 4202 4203 if (scout == NULL) 4204 continue; 4205 4206 scout = dtrace_loadptr(scout + next); 4207 4208 if (scout == NULL) 4209 continue; 4210 4211 if (scout == maddr) { 4212 *flags |= CPU_DTRACE_ILLOP; 4213 break; 4214 } 4215 4216 continue; 4217 } 4218 4219 /* 4220 * We have the minor data. Now we need to 4221 * copy the minor's name into the end of the 4222 * pathname. 4223 */ 4224 s = (char *)dtrace_loadptr(maddr + name); 4225 len = dtrace_strlen(s, size); 4226 4227 if (*flags & CPU_DTRACE_FAULT) 4228 break; 4229 4230 if (len != 0) { 4231 if ((end -= (len + 1)) < start) 4232 break; 4233 4234 *end = ':'; 4235 } 4236 4237 for (i = 1; i <= len; i++) 4238 end[i] = dtrace_load8((uintptr_t)s++); 4239 break; 4240 } 4241 } 4242 4243 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4244 ddi_node_state_t devi_state; 4245 4246 devi_state = dtrace_load32(daddr + 4247 offsetof(struct dev_info, devi_node_state)); 4248 4249 if (*flags & CPU_DTRACE_FAULT) 4250 break; 4251 4252 if (devi_state >= DS_INITIALIZED) { 4253 s = (char *)dtrace_loadptr(daddr + 4254 offsetof(struct dev_info, devi_addr)); 4255 len = dtrace_strlen(s, size); 4256 4257 if (*flags & CPU_DTRACE_FAULT) 4258 break; 4259 4260 if (len != 0) { 4261 if ((end -= (len + 1)) < start) 4262 break; 4263 4264 *end = '@'; 4265 } 4266 4267 for (i = 1; i <= len; i++) 4268 end[i] = dtrace_load8((uintptr_t)s++); 4269 } 4270 4271 /* 4272 * Now for the node name... 4273 */ 4274 s = (char *)dtrace_loadptr(daddr + 4275 offsetof(struct dev_info, devi_node_name)); 4276 4277 daddr = dtrace_loadptr(daddr + 4278 offsetof(struct dev_info, devi_parent)); 4279 4280 /* 4281 * If our parent is NULL (that is, if we're the root 4282 * node), we're going to use the special path 4283 * "devices". 4284 */ 4285 if (daddr == 0) 4286 s = "devices"; 4287 4288 len = dtrace_strlen(s, size); 4289 if (*flags & CPU_DTRACE_FAULT) 4290 break; 4291 4292 if ((end -= (len + 1)) < start) 4293 break; 4294 4295 for (i = 1; i <= len; i++) 4296 end[i] = dtrace_load8((uintptr_t)s++); 4297 *end = '/'; 4298 4299 if (depth++ > dtrace_devdepth_max) { 4300 *flags |= CPU_DTRACE_ILLOP; 4301 break; 4302 } 4303 } 4304 4305 if (end < start) 4306 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4307 4308 if (daddr == 0) { 4309 regs[rd] = (uintptr_t)end; 4310 mstate->dtms_scratch_ptr += size; 4311 } 4312 4313 break; 4314 } 4315#endif 4316 4317 case DIF_SUBR_STRJOIN: { 4318 char *d = (char *)mstate->dtms_scratch_ptr; 4319 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4320 uintptr_t s1 = tupregs[0].dttk_value; 4321 uintptr_t s2 = tupregs[1].dttk_value; 4322 int i = 0; 4323 4324 if (!dtrace_strcanload(s1, size, mstate, vstate) || 4325 !dtrace_strcanload(s2, size, mstate, vstate)) { 4326 regs[rd] = 0; 4327 break; 4328 } 4329 4330 if (!DTRACE_INSCRATCH(mstate, size)) { 4331 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4332 regs[rd] = 0; 4333 break; 4334 } 4335 4336 for (;;) { 4337 if (i >= size) { 4338 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4339 regs[rd] = 0; 4340 break; 4341 } 4342 4343 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 4344 i--; 4345 break; 4346 } 4347 } 4348 4349 for (;;) { 4350 if (i >= size) { 4351 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4352 regs[rd] = 0; 4353 break; 4354 } 4355 4356 if ((d[i++] = dtrace_load8(s2++)) == '\0') 4357 break; 4358 } 4359 4360 if (i < size) { 4361 mstate->dtms_scratch_ptr += i; 4362 regs[rd] = (uintptr_t)d; 4363 } 4364 4365 break; 4366 } 4367 4368 case DIF_SUBR_LLTOSTR: { 4369 int64_t i = (int64_t)tupregs[0].dttk_value; 4370 uint64_t val, digit; 4371 uint64_t size = 65; /* enough room for 2^64 in binary */ 4372 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 4373 int base = 10; 4374 4375 if (nargs > 1) { 4376 if ((base = tupregs[1].dttk_value) <= 1 || 4377 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 4378 *flags |= CPU_DTRACE_ILLOP; 4379 break; 4380 } 4381 } 4382 4383 val = (base == 10 && i < 0) ? i * -1 : i; 4384 4385 if (!DTRACE_INSCRATCH(mstate, size)) { 4386 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4387 regs[rd] = 0; 4388 break; 4389 } 4390 4391 for (*end-- = '\0'; val; val /= base) { 4392 if ((digit = val % base) <= '9' - '0') { 4393 *end-- = '0' + digit; 4394 } else { 4395 *end-- = 'a' + (digit - ('9' - '0') - 1); 4396 } 4397 } 4398 4399 if (i == 0 && base == 16) 4400 *end-- = '0'; 4401 4402 if (base == 16) 4403 *end-- = 'x'; 4404 4405 if (i == 0 || base == 8 || base == 16) 4406 *end-- = '0'; 4407 4408 if (i < 0 && base == 10) 4409 *end-- = '-'; 4410 4411 regs[rd] = (uintptr_t)end + 1; 4412 mstate->dtms_scratch_ptr += size; 4413 break; 4414 } 4415 4416 case DIF_SUBR_HTONS: 4417 case DIF_SUBR_NTOHS: 4418#if BYTE_ORDER == BIG_ENDIAN 4419 regs[rd] = (uint16_t)tupregs[0].dttk_value; 4420#else 4421 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 4422#endif 4423 break; 4424 4425 4426 case DIF_SUBR_HTONL: 4427 case DIF_SUBR_NTOHL: 4428#if BYTE_ORDER == BIG_ENDIAN 4429 regs[rd] = (uint32_t)tupregs[0].dttk_value; 4430#else 4431 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 4432#endif 4433 break; 4434 4435 4436 case DIF_SUBR_HTONLL: 4437 case DIF_SUBR_NTOHLL: 4438#if BYTE_ORDER == BIG_ENDIAN 4439 regs[rd] = (uint64_t)tupregs[0].dttk_value; 4440#else 4441 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 4442#endif 4443 break; 4444 4445 4446 case DIF_SUBR_DIRNAME: 4447 case DIF_SUBR_BASENAME: { 4448 char *dest = (char *)mstate->dtms_scratch_ptr; 4449 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4450 uintptr_t src = tupregs[0].dttk_value; 4451 int i, j, len = dtrace_strlen((char *)src, size); 4452 int lastbase = -1, firstbase = -1, lastdir = -1; 4453 int start, end; 4454 4455 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 4456 regs[rd] = 0; 4457 break; 4458 } 4459 4460 if (!DTRACE_INSCRATCH(mstate, size)) { 4461 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4462 regs[rd] = 0; 4463 break; 4464 } 4465 4466 /* 4467 * The basename and dirname for a zero-length string is 4468 * defined to be "." 4469 */ 4470 if (len == 0) { 4471 len = 1; 4472 src = (uintptr_t)"."; 4473 } 4474 4475 /* 4476 * Start from the back of the string, moving back toward the 4477 * front until we see a character that isn't a slash. That 4478 * character is the last character in the basename. 4479 */ 4480 for (i = len - 1; i >= 0; i--) { 4481 if (dtrace_load8(src + i) != '/') 4482 break; 4483 } 4484 4485 if (i >= 0) 4486 lastbase = i; 4487 4488 /* 4489 * Starting from the last character in the basename, move 4490 * towards the front until we find a slash. The character 4491 * that we processed immediately before that is the first 4492 * character in the basename. 4493 */ 4494 for (; i >= 0; i--) { 4495 if (dtrace_load8(src + i) == '/') 4496 break; 4497 } 4498 4499 if (i >= 0) 4500 firstbase = i + 1; 4501 4502 /* 4503 * Now keep going until we find a non-slash character. That 4504 * character is the last character in the dirname. 4505 */ 4506 for (; i >= 0; i--) { 4507 if (dtrace_load8(src + i) != '/') 4508 break; 4509 } 4510 4511 if (i >= 0) 4512 lastdir = i; 4513 4514 ASSERT(!(lastbase == -1 && firstbase != -1)); 4515 ASSERT(!(firstbase == -1 && lastdir != -1)); 4516 4517 if (lastbase == -1) { 4518 /* 4519 * We didn't find a non-slash character. We know that 4520 * the length is non-zero, so the whole string must be 4521 * slashes. In either the dirname or the basename 4522 * case, we return '/'. 4523 */ 4524 ASSERT(firstbase == -1); 4525 firstbase = lastbase = lastdir = 0; 4526 } 4527 4528 if (firstbase == -1) { 4529 /* 4530 * The entire string consists only of a basename 4531 * component. If we're looking for dirname, we need 4532 * to change our string to be just "."; if we're 4533 * looking for a basename, we'll just set the first 4534 * character of the basename to be 0. 4535 */ 4536 if (subr == DIF_SUBR_DIRNAME) { 4537 ASSERT(lastdir == -1); 4538 src = (uintptr_t)"."; 4539 lastdir = 0; 4540 } else { 4541 firstbase = 0; 4542 } 4543 } 4544 4545 if (subr == DIF_SUBR_DIRNAME) { 4546 if (lastdir == -1) { 4547 /* 4548 * We know that we have a slash in the name -- 4549 * or lastdir would be set to 0, above. And 4550 * because lastdir is -1, we know that this 4551 * slash must be the first character. (That 4552 * is, the full string must be of the form 4553 * "/basename".) In this case, the last 4554 * character of the directory name is 0. 4555 */ 4556 lastdir = 0; 4557 } 4558 4559 start = 0; 4560 end = lastdir; 4561 } else { 4562 ASSERT(subr == DIF_SUBR_BASENAME); 4563 ASSERT(firstbase != -1 && lastbase != -1); 4564 start = firstbase; 4565 end = lastbase; 4566 } 4567 4568 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 4569 dest[j] = dtrace_load8(src + i); 4570 4571 dest[j] = '\0'; 4572 regs[rd] = (uintptr_t)dest; 4573 mstate->dtms_scratch_ptr += size; 4574 break; 4575 } 4576 4577 case DIF_SUBR_CLEANPATH: { 4578 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4579 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4580 uintptr_t src = tupregs[0].dttk_value; 4581 int i = 0, j = 0; 4582 4583 if (!dtrace_strcanload(src, size, mstate, vstate)) { 4584 regs[rd] = 0; 4585 break; 4586 } 4587 4588 if (!DTRACE_INSCRATCH(mstate, size)) { 4589 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4590 regs[rd] = 0; 4591 break; 4592 } 4593 4594 /* 4595 * Move forward, loading each character. 4596 */ 4597 do { 4598 c = dtrace_load8(src + i++); 4599next: 4600 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 4601 break; 4602 4603 if (c != '/') { 4604 dest[j++] = c; 4605 continue; 4606 } 4607 4608 c = dtrace_load8(src + i++); 4609 4610 if (c == '/') { 4611 /* 4612 * We have two slashes -- we can just advance 4613 * to the next character. 4614 */ 4615 goto next; 4616 } 4617 4618 if (c != '.') { 4619 /* 4620 * This is not "." and it's not ".." -- we can 4621 * just store the "/" and this character and 4622 * drive on. 4623 */ 4624 dest[j++] = '/'; 4625 dest[j++] = c; 4626 continue; 4627 } 4628 4629 c = dtrace_load8(src + i++); 4630 4631 if (c == '/') { 4632 /* 4633 * This is a "/./" component. We're not going 4634 * to store anything in the destination buffer; 4635 * we're just going to go to the next component. 4636 */ 4637 goto next; 4638 } 4639 4640 if (c != '.') { 4641 /* 4642 * This is not ".." -- we can just store the 4643 * "/." and this character and continue 4644 * processing. 4645 */ 4646 dest[j++] = '/'; 4647 dest[j++] = '.'; 4648 dest[j++] = c; 4649 continue; 4650 } 4651 4652 c = dtrace_load8(src + i++); 4653 4654 if (c != '/' && c != '\0') { 4655 /* 4656 * This is not ".." -- it's "..[mumble]". 4657 * We'll store the "/.." and this character 4658 * and continue processing. 4659 */ 4660 dest[j++] = '/'; 4661 dest[j++] = '.'; 4662 dest[j++] = '.'; 4663 dest[j++] = c; 4664 continue; 4665 } 4666 4667 /* 4668 * This is "/../" or "/..\0". We need to back up 4669 * our destination pointer until we find a "/". 4670 */ 4671 i--; 4672 while (j != 0 && dest[--j] != '/') 4673 continue; 4674 4675 if (c == '\0') 4676 dest[++j] = '/'; 4677 } while (c != '\0'); 4678 4679 dest[j] = '\0'; 4680 regs[rd] = (uintptr_t)dest; 4681 mstate->dtms_scratch_ptr += size; 4682 break; 4683 } 4684 4685 case DIF_SUBR_INET_NTOA: 4686 case DIF_SUBR_INET_NTOA6: 4687 case DIF_SUBR_INET_NTOP: { 4688 size_t size; 4689 int af, argi, i; 4690 char *base, *end; 4691 4692 if (subr == DIF_SUBR_INET_NTOP) { 4693 af = (int)tupregs[0].dttk_value; 4694 argi = 1; 4695 } else { 4696 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 4697 argi = 0; 4698 } 4699 4700 if (af == AF_INET) { 4701 ipaddr_t ip4; 4702 uint8_t *ptr8, val; 4703 4704 /* 4705 * Safely load the IPv4 address. 4706 */ 4707 ip4 = dtrace_load32(tupregs[argi].dttk_value); 4708 4709 /* 4710 * Check an IPv4 string will fit in scratch. 4711 */ 4712 size = INET_ADDRSTRLEN; 4713 if (!DTRACE_INSCRATCH(mstate, size)) { 4714 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4715 regs[rd] = 0; 4716 break; 4717 } 4718 base = (char *)mstate->dtms_scratch_ptr; 4719 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4720 4721 /* 4722 * Stringify as a dotted decimal quad. 4723 */ 4724 *end-- = '\0'; 4725 ptr8 = (uint8_t *)&ip4; 4726 for (i = 3; i >= 0; i--) { 4727 val = ptr8[i]; 4728 4729 if (val == 0) { 4730 *end-- = '0'; 4731 } else { 4732 for (; val; val /= 10) { 4733 *end-- = '0' + (val % 10); 4734 } 4735 } 4736 4737 if (i > 0) 4738 *end-- = '.'; 4739 } 4740 ASSERT(end + 1 >= base); 4741 4742 } else if (af == AF_INET6) { 4743 struct in6_addr ip6; 4744 int firstzero, tryzero, numzero, v6end; 4745 uint16_t val; 4746 const char digits[] = "0123456789abcdef"; 4747 4748 /* 4749 * Stringify using RFC 1884 convention 2 - 16 bit 4750 * hexadecimal values with a zero-run compression. 4751 * Lower case hexadecimal digits are used. 4752 * eg, fe80::214:4fff:fe0b:76c8. 4753 * The IPv4 embedded form is returned for inet_ntop, 4754 * just the IPv4 string is returned for inet_ntoa6. 4755 */ 4756 4757 /* 4758 * Safely load the IPv6 address. 4759 */ 4760 dtrace_bcopy( 4761 (void *)(uintptr_t)tupregs[argi].dttk_value, 4762 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 4763 4764 /* 4765 * Check an IPv6 string will fit in scratch. 4766 */ 4767 size = INET6_ADDRSTRLEN; 4768 if (!DTRACE_INSCRATCH(mstate, size)) { 4769 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4770 regs[rd] = 0; 4771 break; 4772 } 4773 base = (char *)mstate->dtms_scratch_ptr; 4774 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4775 *end-- = '\0'; 4776 4777 /* 4778 * Find the longest run of 16 bit zero values 4779 * for the single allowed zero compression - "::". 4780 */ 4781 firstzero = -1; 4782 tryzero = -1; 4783 numzero = 1; 4784 for (i = 0; i < sizeof (struct in6_addr); i++) { 4785#if defined(sun) 4786 if (ip6._S6_un._S6_u8[i] == 0 && 4787#else 4788 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 4789#endif 4790 tryzero == -1 && i % 2 == 0) { 4791 tryzero = i; 4792 continue; 4793 } 4794 4795 if (tryzero != -1 && 4796#if defined(sun) 4797 (ip6._S6_un._S6_u8[i] != 0 || 4798#else 4799 (ip6.__u6_addr.__u6_addr8[i] != 0 || 4800#endif 4801 i == sizeof (struct in6_addr) - 1)) { 4802 4803 if (i - tryzero <= numzero) { 4804 tryzero = -1; 4805 continue; 4806 } 4807 4808 firstzero = tryzero; 4809 numzero = i - i % 2 - tryzero; 4810 tryzero = -1; 4811 4812#if defined(sun) 4813 if (ip6._S6_un._S6_u8[i] == 0 && 4814#else 4815 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 4816#endif 4817 i == sizeof (struct in6_addr) - 1) 4818 numzero += 2; 4819 } 4820 } 4821 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 4822 4823 /* 4824 * Check for an IPv4 embedded address. 4825 */ 4826 v6end = sizeof (struct in6_addr) - 2; 4827 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 4828 IN6_IS_ADDR_V4COMPAT(&ip6)) { 4829 for (i = sizeof (struct in6_addr) - 1; 4830 i >= DTRACE_V4MAPPED_OFFSET; i--) { 4831 ASSERT(end >= base); 4832 4833#if defined(sun) 4834 val = ip6._S6_un._S6_u8[i]; 4835#else 4836 val = ip6.__u6_addr.__u6_addr8[i]; 4837#endif 4838 4839 if (val == 0) { 4840 *end-- = '0'; 4841 } else { 4842 for (; val; val /= 10) { 4843 *end-- = '0' + val % 10; 4844 } 4845 } 4846 4847 if (i > DTRACE_V4MAPPED_OFFSET) 4848 *end-- = '.'; 4849 } 4850 4851 if (subr == DIF_SUBR_INET_NTOA6) 4852 goto inetout; 4853 4854 /* 4855 * Set v6end to skip the IPv4 address that 4856 * we have already stringified. 4857 */ 4858 v6end = 10; 4859 } 4860 4861 /* 4862 * Build the IPv6 string by working through the 4863 * address in reverse. 4864 */ 4865 for (i = v6end; i >= 0; i -= 2) { 4866 ASSERT(end >= base); 4867 4868 if (i == firstzero + numzero - 2) { 4869 *end-- = ':'; 4870 *end-- = ':'; 4871 i -= numzero - 2; 4872 continue; 4873 } 4874 4875 if (i < 14 && i != firstzero - 2) 4876 *end-- = ':'; 4877 4878#if defined(sun) 4879 val = (ip6._S6_un._S6_u8[i] << 8) + 4880 ip6._S6_un._S6_u8[i + 1]; 4881#else 4882 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 4883 ip6.__u6_addr.__u6_addr8[i + 1]; 4884#endif 4885 4886 if (val == 0) { 4887 *end-- = '0'; 4888 } else { 4889 for (; val; val /= 16) { 4890 *end-- = digits[val % 16]; 4891 } 4892 } 4893 } 4894 ASSERT(end + 1 >= base); 4895 4896 } else { 4897 /* 4898 * The user didn't use AH_INET or AH_INET6. 4899 */ 4900 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4901 regs[rd] = 0; 4902 break; 4903 } 4904 4905inetout: regs[rd] = (uintptr_t)end + 1; 4906 mstate->dtms_scratch_ptr += size; 4907 break; 4908 } 4909 4910 case DIF_SUBR_MEMREF: { 4911 uintptr_t size = 2 * sizeof(uintptr_t); 4912 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 4913 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 4914 4915 /* address and length */ 4916 memref[0] = tupregs[0].dttk_value; 4917 memref[1] = tupregs[1].dttk_value; 4918 4919 regs[rd] = (uintptr_t) memref; 4920 mstate->dtms_scratch_ptr += scratch_size; 4921 break; 4922 } 4923 4924 case DIF_SUBR_TYPEREF: { 4925 uintptr_t size = 4 * sizeof(uintptr_t); 4926 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 4927 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 4928 4929 /* address, num_elements, type_str, type_len */ 4930 typeref[0] = tupregs[0].dttk_value; 4931 typeref[1] = tupregs[1].dttk_value; 4932 typeref[2] = tupregs[2].dttk_value; 4933 typeref[3] = tupregs[3].dttk_value; 4934 4935 regs[rd] = (uintptr_t) typeref; 4936 mstate->dtms_scratch_ptr += scratch_size; 4937 break; 4938 } 4939 } 4940} 4941 4942/* 4943 * Emulate the execution of DTrace IR instructions specified by the given 4944 * DIF object. This function is deliberately void of assertions as all of 4945 * the necessary checks are handled by a call to dtrace_difo_validate(). 4946 */ 4947static uint64_t 4948dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 4949 dtrace_vstate_t *vstate, dtrace_state_t *state) 4950{ 4951 const dif_instr_t *text = difo->dtdo_buf; 4952 const uint_t textlen = difo->dtdo_len; 4953 const char *strtab = difo->dtdo_strtab; 4954 const uint64_t *inttab = difo->dtdo_inttab; 4955 4956 uint64_t rval = 0; 4957 dtrace_statvar_t *svar; 4958 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 4959 dtrace_difv_t *v; 4960 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4961 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4962 4963 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 4964 uint64_t regs[DIF_DIR_NREGS]; 4965 uint64_t *tmp; 4966 4967 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 4968 int64_t cc_r; 4969 uint_t pc = 0, id, opc = 0; 4970 uint8_t ttop = 0; 4971 dif_instr_t instr; 4972 uint_t r1, r2, rd; 4973 4974 /* 4975 * We stash the current DIF object into the machine state: we need it 4976 * for subsequent access checking. 4977 */ 4978 mstate->dtms_difo = difo; 4979 4980 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 4981 4982 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 4983 opc = pc; 4984 4985 instr = text[pc++]; 4986 r1 = DIF_INSTR_R1(instr); 4987 r2 = DIF_INSTR_R2(instr); 4988 rd = DIF_INSTR_RD(instr); 4989 4990 switch (DIF_INSTR_OP(instr)) { 4991 case DIF_OP_OR: 4992 regs[rd] = regs[r1] | regs[r2]; 4993 break; 4994 case DIF_OP_XOR: 4995 regs[rd] = regs[r1] ^ regs[r2]; 4996 break; 4997 case DIF_OP_AND: 4998 regs[rd] = regs[r1] & regs[r2]; 4999 break; 5000 case DIF_OP_SLL: 5001 regs[rd] = regs[r1] << regs[r2]; 5002 break; 5003 case DIF_OP_SRL: 5004 regs[rd] = regs[r1] >> regs[r2]; 5005 break; 5006 case DIF_OP_SUB: 5007 regs[rd] = regs[r1] - regs[r2]; 5008 break; 5009 case DIF_OP_ADD: 5010 regs[rd] = regs[r1] + regs[r2]; 5011 break; 5012 case DIF_OP_MUL: 5013 regs[rd] = regs[r1] * regs[r2]; 5014 break; 5015 case DIF_OP_SDIV: 5016 if (regs[r2] == 0) { 5017 regs[rd] = 0; 5018 *flags |= CPU_DTRACE_DIVZERO; 5019 } else { 5020 regs[rd] = (int64_t)regs[r1] / 5021 (int64_t)regs[r2]; 5022 } 5023 break; 5024 5025 case DIF_OP_UDIV: 5026 if (regs[r2] == 0) { 5027 regs[rd] = 0; 5028 *flags |= CPU_DTRACE_DIVZERO; 5029 } else { 5030 regs[rd] = regs[r1] / regs[r2]; 5031 } 5032 break; 5033 5034 case DIF_OP_SREM: 5035 if (regs[r2] == 0) { 5036 regs[rd] = 0; 5037 *flags |= CPU_DTRACE_DIVZERO; 5038 } else { 5039 regs[rd] = (int64_t)regs[r1] % 5040 (int64_t)regs[r2]; 5041 } 5042 break; 5043 5044 case DIF_OP_UREM: 5045 if (regs[r2] == 0) { 5046 regs[rd] = 0; 5047 *flags |= CPU_DTRACE_DIVZERO; 5048 } else { 5049 regs[rd] = regs[r1] % regs[r2]; 5050 } 5051 break; 5052 5053 case DIF_OP_NOT: 5054 regs[rd] = ~regs[r1]; 5055 break; 5056 case DIF_OP_MOV: 5057 regs[rd] = regs[r1]; 5058 break; 5059 case DIF_OP_CMP: 5060 cc_r = regs[r1] - regs[r2]; 5061 cc_n = cc_r < 0; 5062 cc_z = cc_r == 0; 5063 cc_v = 0; 5064 cc_c = regs[r1] < regs[r2]; 5065 break; 5066 case DIF_OP_TST: 5067 cc_n = cc_v = cc_c = 0; 5068 cc_z = regs[r1] == 0; 5069 break; 5070 case DIF_OP_BA: 5071 pc = DIF_INSTR_LABEL(instr); 5072 break; 5073 case DIF_OP_BE: 5074 if (cc_z) 5075 pc = DIF_INSTR_LABEL(instr); 5076 break; 5077 case DIF_OP_BNE: 5078 if (cc_z == 0) 5079 pc = DIF_INSTR_LABEL(instr); 5080 break; 5081 case DIF_OP_BG: 5082 if ((cc_z | (cc_n ^ cc_v)) == 0) 5083 pc = DIF_INSTR_LABEL(instr); 5084 break; 5085 case DIF_OP_BGU: 5086 if ((cc_c | cc_z) == 0) 5087 pc = DIF_INSTR_LABEL(instr); 5088 break; 5089 case DIF_OP_BGE: 5090 if ((cc_n ^ cc_v) == 0) 5091 pc = DIF_INSTR_LABEL(instr); 5092 break; 5093 case DIF_OP_BGEU: 5094 if (cc_c == 0) 5095 pc = DIF_INSTR_LABEL(instr); 5096 break; 5097 case DIF_OP_BL: 5098 if (cc_n ^ cc_v) 5099 pc = DIF_INSTR_LABEL(instr); 5100 break; 5101 case DIF_OP_BLU: 5102 if (cc_c) 5103 pc = DIF_INSTR_LABEL(instr); 5104 break; 5105 case DIF_OP_BLE: 5106 if (cc_z | (cc_n ^ cc_v)) 5107 pc = DIF_INSTR_LABEL(instr); 5108 break; 5109 case DIF_OP_BLEU: 5110 if (cc_c | cc_z) 5111 pc = DIF_INSTR_LABEL(instr); 5112 break; 5113 case DIF_OP_RLDSB: 5114 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 5115 *flags |= CPU_DTRACE_KPRIV; 5116 *illval = regs[r1]; 5117 break; 5118 } 5119 /*FALLTHROUGH*/ 5120 case DIF_OP_LDSB: 5121 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 5122 break; 5123 case DIF_OP_RLDSH: 5124 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 5125 *flags |= CPU_DTRACE_KPRIV; 5126 *illval = regs[r1]; 5127 break; 5128 } 5129 /*FALLTHROUGH*/ 5130 case DIF_OP_LDSH: 5131 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 5132 break; 5133 case DIF_OP_RLDSW: 5134 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 5135 *flags |= CPU_DTRACE_KPRIV; 5136 *illval = regs[r1]; 5137 break; 5138 } 5139 /*FALLTHROUGH*/ 5140 case DIF_OP_LDSW: 5141 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 5142 break; 5143 case DIF_OP_RLDUB: 5144 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 5145 *flags |= CPU_DTRACE_KPRIV; 5146 *illval = regs[r1]; 5147 break; 5148 } 5149 /*FALLTHROUGH*/ 5150 case DIF_OP_LDUB: 5151 regs[rd] = dtrace_load8(regs[r1]); 5152 break; 5153 case DIF_OP_RLDUH: 5154 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 5155 *flags |= CPU_DTRACE_KPRIV; 5156 *illval = regs[r1]; 5157 break; 5158 } 5159 /*FALLTHROUGH*/ 5160 case DIF_OP_LDUH: 5161 regs[rd] = dtrace_load16(regs[r1]); 5162 break; 5163 case DIF_OP_RLDUW: 5164 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 5165 *flags |= CPU_DTRACE_KPRIV; 5166 *illval = regs[r1]; 5167 break; 5168 } 5169 /*FALLTHROUGH*/ 5170 case DIF_OP_LDUW: 5171 regs[rd] = dtrace_load32(regs[r1]); 5172 break; 5173 case DIF_OP_RLDX: 5174 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 5175 *flags |= CPU_DTRACE_KPRIV; 5176 *illval = regs[r1]; 5177 break; 5178 } 5179 /*FALLTHROUGH*/ 5180 case DIF_OP_LDX: 5181 regs[rd] = dtrace_load64(regs[r1]); 5182 break; 5183 case DIF_OP_ULDSB: 5184 regs[rd] = (int8_t) 5185 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5186 break; 5187 case DIF_OP_ULDSH: 5188 regs[rd] = (int16_t) 5189 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5190 break; 5191 case DIF_OP_ULDSW: 5192 regs[rd] = (int32_t) 5193 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5194 break; 5195 case DIF_OP_ULDUB: 5196 regs[rd] = 5197 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5198 break; 5199 case DIF_OP_ULDUH: 5200 regs[rd] = 5201 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5202 break; 5203 case DIF_OP_ULDUW: 5204 regs[rd] = 5205 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5206 break; 5207 case DIF_OP_ULDX: 5208 regs[rd] = 5209 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 5210 break; 5211 case DIF_OP_RET: 5212 rval = regs[rd]; 5213 pc = textlen; 5214 break; 5215 case DIF_OP_NOP: 5216 break; 5217 case DIF_OP_SETX: 5218 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 5219 break; 5220 case DIF_OP_SETS: 5221 regs[rd] = (uint64_t)(uintptr_t) 5222 (strtab + DIF_INSTR_STRING(instr)); 5223 break; 5224 case DIF_OP_SCMP: { 5225 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 5226 uintptr_t s1 = regs[r1]; 5227 uintptr_t s2 = regs[r2]; 5228 5229 if (s1 != 0 && 5230 !dtrace_strcanload(s1, sz, mstate, vstate)) 5231 break; 5232 if (s2 != 0 && 5233 !dtrace_strcanload(s2, sz, mstate, vstate)) 5234 break; 5235 5236 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 5237 5238 cc_n = cc_r < 0; 5239 cc_z = cc_r == 0; 5240 cc_v = cc_c = 0; 5241 break; 5242 } 5243 case DIF_OP_LDGA: 5244 regs[rd] = dtrace_dif_variable(mstate, state, 5245 r1, regs[r2]); 5246 break; 5247 case DIF_OP_LDGS: 5248 id = DIF_INSTR_VAR(instr); 5249 5250 if (id >= DIF_VAR_OTHER_UBASE) { 5251 uintptr_t a; 5252 5253 id -= DIF_VAR_OTHER_UBASE; 5254 svar = vstate->dtvs_globals[id]; 5255 ASSERT(svar != NULL); 5256 v = &svar->dtsv_var; 5257 5258 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 5259 regs[rd] = svar->dtsv_data; 5260 break; 5261 } 5262 5263 a = (uintptr_t)svar->dtsv_data; 5264 5265 if (*(uint8_t *)a == UINT8_MAX) { 5266 /* 5267 * If the 0th byte is set to UINT8_MAX 5268 * then this is to be treated as a 5269 * reference to a NULL variable. 5270 */ 5271 regs[rd] = 0; 5272 } else { 5273 regs[rd] = a + sizeof (uint64_t); 5274 } 5275 5276 break; 5277 } 5278 5279 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 5280 break; 5281 5282 case DIF_OP_STGS: 5283 id = DIF_INSTR_VAR(instr); 5284 5285 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5286 id -= DIF_VAR_OTHER_UBASE; 5287 5288 svar = vstate->dtvs_globals[id]; 5289 ASSERT(svar != NULL); 5290 v = &svar->dtsv_var; 5291 5292 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5293 uintptr_t a = (uintptr_t)svar->dtsv_data; 5294 5295 ASSERT(a != 0); 5296 ASSERT(svar->dtsv_size != 0); 5297 5298 if (regs[rd] == 0) { 5299 *(uint8_t *)a = UINT8_MAX; 5300 break; 5301 } else { 5302 *(uint8_t *)a = 0; 5303 a += sizeof (uint64_t); 5304 } 5305 if (!dtrace_vcanload( 5306 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5307 mstate, vstate)) 5308 break; 5309 5310 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5311 (void *)a, &v->dtdv_type); 5312 break; 5313 } 5314 5315 svar->dtsv_data = regs[rd]; 5316 break; 5317 5318 case DIF_OP_LDTA: 5319 /* 5320 * There are no DTrace built-in thread-local arrays at 5321 * present. This opcode is saved for future work. 5322 */ 5323 *flags |= CPU_DTRACE_ILLOP; 5324 regs[rd] = 0; 5325 break; 5326 5327 case DIF_OP_LDLS: 5328 id = DIF_INSTR_VAR(instr); 5329 5330 if (id < DIF_VAR_OTHER_UBASE) { 5331 /* 5332 * For now, this has no meaning. 5333 */ 5334 regs[rd] = 0; 5335 break; 5336 } 5337 5338 id -= DIF_VAR_OTHER_UBASE; 5339 5340 ASSERT(id < vstate->dtvs_nlocals); 5341 ASSERT(vstate->dtvs_locals != NULL); 5342 5343 svar = vstate->dtvs_locals[id]; 5344 ASSERT(svar != NULL); 5345 v = &svar->dtsv_var; 5346 5347 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5348 uintptr_t a = (uintptr_t)svar->dtsv_data; 5349 size_t sz = v->dtdv_type.dtdt_size; 5350 5351 sz += sizeof (uint64_t); 5352 ASSERT(svar->dtsv_size == NCPU * sz); 5353 a += curcpu * sz; 5354 5355 if (*(uint8_t *)a == UINT8_MAX) { 5356 /* 5357 * If the 0th byte is set to UINT8_MAX 5358 * then this is to be treated as a 5359 * reference to a NULL variable. 5360 */ 5361 regs[rd] = 0; 5362 } else { 5363 regs[rd] = a + sizeof (uint64_t); 5364 } 5365 5366 break; 5367 } 5368 5369 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5370 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5371 regs[rd] = tmp[curcpu]; 5372 break; 5373 5374 case DIF_OP_STLS: 5375 id = DIF_INSTR_VAR(instr); 5376 5377 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5378 id -= DIF_VAR_OTHER_UBASE; 5379 ASSERT(id < vstate->dtvs_nlocals); 5380 5381 ASSERT(vstate->dtvs_locals != NULL); 5382 svar = vstate->dtvs_locals[id]; 5383 ASSERT(svar != NULL); 5384 v = &svar->dtsv_var; 5385 5386 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5387 uintptr_t a = (uintptr_t)svar->dtsv_data; 5388 size_t sz = v->dtdv_type.dtdt_size; 5389 5390 sz += sizeof (uint64_t); 5391 ASSERT(svar->dtsv_size == NCPU * sz); 5392 a += curcpu * sz; 5393 5394 if (regs[rd] == 0) { 5395 *(uint8_t *)a = UINT8_MAX; 5396 break; 5397 } else { 5398 *(uint8_t *)a = 0; 5399 a += sizeof (uint64_t); 5400 } 5401 5402 if (!dtrace_vcanload( 5403 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5404 mstate, vstate)) 5405 break; 5406 5407 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5408 (void *)a, &v->dtdv_type); 5409 break; 5410 } 5411 5412 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5413 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5414 tmp[curcpu] = regs[rd]; 5415 break; 5416 5417 case DIF_OP_LDTS: { 5418 dtrace_dynvar_t *dvar; 5419 dtrace_key_t *key; 5420 5421 id = DIF_INSTR_VAR(instr); 5422 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5423 id -= DIF_VAR_OTHER_UBASE; 5424 v = &vstate->dtvs_tlocals[id]; 5425 5426 key = &tupregs[DIF_DTR_NREGS]; 5427 key[0].dttk_value = (uint64_t)id; 5428 key[0].dttk_size = 0; 5429 DTRACE_TLS_THRKEY(key[1].dttk_value); 5430 key[1].dttk_size = 0; 5431 5432 dvar = dtrace_dynvar(dstate, 2, key, 5433 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 5434 mstate, vstate); 5435 5436 if (dvar == NULL) { 5437 regs[rd] = 0; 5438 break; 5439 } 5440 5441 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5442 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5443 } else { 5444 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5445 } 5446 5447 break; 5448 } 5449 5450 case DIF_OP_STTS: { 5451 dtrace_dynvar_t *dvar; 5452 dtrace_key_t *key; 5453 5454 id = DIF_INSTR_VAR(instr); 5455 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5456 id -= DIF_VAR_OTHER_UBASE; 5457 5458 key = &tupregs[DIF_DTR_NREGS]; 5459 key[0].dttk_value = (uint64_t)id; 5460 key[0].dttk_size = 0; 5461 DTRACE_TLS_THRKEY(key[1].dttk_value); 5462 key[1].dttk_size = 0; 5463 v = &vstate->dtvs_tlocals[id]; 5464 5465 dvar = dtrace_dynvar(dstate, 2, key, 5466 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5467 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5468 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5469 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5470 5471 /* 5472 * Given that we're storing to thread-local data, 5473 * we need to flush our predicate cache. 5474 */ 5475 curthread->t_predcache = 0; 5476 5477 if (dvar == NULL) 5478 break; 5479 5480 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5481 if (!dtrace_vcanload( 5482 (void *)(uintptr_t)regs[rd], 5483 &v->dtdv_type, mstate, vstate)) 5484 break; 5485 5486 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5487 dvar->dtdv_data, &v->dtdv_type); 5488 } else { 5489 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5490 } 5491 5492 break; 5493 } 5494 5495 case DIF_OP_SRA: 5496 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 5497 break; 5498 5499 case DIF_OP_CALL: 5500 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 5501 regs, tupregs, ttop, mstate, state); 5502 break; 5503 5504 case DIF_OP_PUSHTR: 5505 if (ttop == DIF_DTR_NREGS) { 5506 *flags |= CPU_DTRACE_TUPOFLOW; 5507 break; 5508 } 5509 5510 if (r1 == DIF_TYPE_STRING) { 5511 /* 5512 * If this is a string type and the size is 0, 5513 * we'll use the system-wide default string 5514 * size. Note that we are _not_ looking at 5515 * the value of the DTRACEOPT_STRSIZE option; 5516 * had this been set, we would expect to have 5517 * a non-zero size value in the "pushtr". 5518 */ 5519 tupregs[ttop].dttk_size = 5520 dtrace_strlen((char *)(uintptr_t)regs[rd], 5521 regs[r2] ? regs[r2] : 5522 dtrace_strsize_default) + 1; 5523 } else { 5524 tupregs[ttop].dttk_size = regs[r2]; 5525 } 5526 5527 tupregs[ttop++].dttk_value = regs[rd]; 5528 break; 5529 5530 case DIF_OP_PUSHTV: 5531 if (ttop == DIF_DTR_NREGS) { 5532 *flags |= CPU_DTRACE_TUPOFLOW; 5533 break; 5534 } 5535 5536 tupregs[ttop].dttk_value = regs[rd]; 5537 tupregs[ttop++].dttk_size = 0; 5538 break; 5539 5540 case DIF_OP_POPTS: 5541 if (ttop != 0) 5542 ttop--; 5543 break; 5544 5545 case DIF_OP_FLUSHTS: 5546 ttop = 0; 5547 break; 5548 5549 case DIF_OP_LDGAA: 5550 case DIF_OP_LDTAA: { 5551 dtrace_dynvar_t *dvar; 5552 dtrace_key_t *key = tupregs; 5553 uint_t nkeys = ttop; 5554 5555 id = DIF_INSTR_VAR(instr); 5556 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5557 id -= DIF_VAR_OTHER_UBASE; 5558 5559 key[nkeys].dttk_value = (uint64_t)id; 5560 key[nkeys++].dttk_size = 0; 5561 5562 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 5563 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5564 key[nkeys++].dttk_size = 0; 5565 v = &vstate->dtvs_tlocals[id]; 5566 } else { 5567 v = &vstate->dtvs_globals[id]->dtsv_var; 5568 } 5569 5570 dvar = dtrace_dynvar(dstate, nkeys, key, 5571 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5572 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5573 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 5574 5575 if (dvar == NULL) { 5576 regs[rd] = 0; 5577 break; 5578 } 5579 5580 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5581 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5582 } else { 5583 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5584 } 5585 5586 break; 5587 } 5588 5589 case DIF_OP_STGAA: 5590 case DIF_OP_STTAA: { 5591 dtrace_dynvar_t *dvar; 5592 dtrace_key_t *key = tupregs; 5593 uint_t nkeys = ttop; 5594 5595 id = DIF_INSTR_VAR(instr); 5596 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5597 id -= DIF_VAR_OTHER_UBASE; 5598 5599 key[nkeys].dttk_value = (uint64_t)id; 5600 key[nkeys++].dttk_size = 0; 5601 5602 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 5603 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5604 key[nkeys++].dttk_size = 0; 5605 v = &vstate->dtvs_tlocals[id]; 5606 } else { 5607 v = &vstate->dtvs_globals[id]->dtsv_var; 5608 } 5609 5610 dvar = dtrace_dynvar(dstate, nkeys, key, 5611 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5612 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5613 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5614 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5615 5616 if (dvar == NULL) 5617 break; 5618 5619 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5620 if (!dtrace_vcanload( 5621 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5622 mstate, vstate)) 5623 break; 5624 5625 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5626 dvar->dtdv_data, &v->dtdv_type); 5627 } else { 5628 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5629 } 5630 5631 break; 5632 } 5633 5634 case DIF_OP_ALLOCS: { 5635 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5636 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 5637 5638 /* 5639 * Rounding up the user allocation size could have 5640 * overflowed large, bogus allocations (like -1ULL) to 5641 * 0. 5642 */ 5643 if (size < regs[r1] || 5644 !DTRACE_INSCRATCH(mstate, size)) { 5645 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5646 regs[rd] = 0; 5647 break; 5648 } 5649 5650 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 5651 mstate->dtms_scratch_ptr += size; 5652 regs[rd] = ptr; 5653 break; 5654 } 5655 5656 case DIF_OP_COPYS: 5657 if (!dtrace_canstore(regs[rd], regs[r2], 5658 mstate, vstate)) { 5659 *flags |= CPU_DTRACE_BADADDR; 5660 *illval = regs[rd]; 5661 break; 5662 } 5663 5664 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 5665 break; 5666 5667 dtrace_bcopy((void *)(uintptr_t)regs[r1], 5668 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 5669 break; 5670 5671 case DIF_OP_STB: 5672 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 5673 *flags |= CPU_DTRACE_BADADDR; 5674 *illval = regs[rd]; 5675 break; 5676 } 5677 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 5678 break; 5679 5680 case DIF_OP_STH: 5681 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 5682 *flags |= CPU_DTRACE_BADADDR; 5683 *illval = regs[rd]; 5684 break; 5685 } 5686 if (regs[rd] & 1) { 5687 *flags |= CPU_DTRACE_BADALIGN; 5688 *illval = regs[rd]; 5689 break; 5690 } 5691 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 5692 break; 5693 5694 case DIF_OP_STW: 5695 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 5696 *flags |= CPU_DTRACE_BADADDR; 5697 *illval = regs[rd]; 5698 break; 5699 } 5700 if (regs[rd] & 3) { 5701 *flags |= CPU_DTRACE_BADALIGN; 5702 *illval = regs[rd]; 5703 break; 5704 } 5705 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 5706 break; 5707 5708 case DIF_OP_STX: 5709 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 5710 *flags |= CPU_DTRACE_BADADDR; 5711 *illval = regs[rd]; 5712 break; 5713 } 5714 if (regs[rd] & 7) { 5715 *flags |= CPU_DTRACE_BADALIGN; 5716 *illval = regs[rd]; 5717 break; 5718 } 5719 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 5720 break; 5721 } 5722 } 5723 5724 if (!(*flags & CPU_DTRACE_FAULT)) 5725 return (rval); 5726 5727 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 5728 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 5729 5730 return (0); 5731} 5732 5733static void 5734dtrace_action_breakpoint(dtrace_ecb_t *ecb) 5735{ 5736 dtrace_probe_t *probe = ecb->dte_probe; 5737 dtrace_provider_t *prov = probe->dtpr_provider; 5738 char c[DTRACE_FULLNAMELEN + 80], *str; 5739 char *msg = "dtrace: breakpoint action at probe "; 5740 char *ecbmsg = " (ecb "; 5741 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 5742 uintptr_t val = (uintptr_t)ecb; 5743 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 5744 5745 if (dtrace_destructive_disallow) 5746 return; 5747 5748 /* 5749 * It's impossible to be taking action on the NULL probe. 5750 */ 5751 ASSERT(probe != NULL); 5752 5753 /* 5754 * This is a poor man's (destitute man's?) sprintf(): we want to 5755 * print the provider name, module name, function name and name of 5756 * the probe, along with the hex address of the ECB with the breakpoint 5757 * action -- all of which we must place in the character buffer by 5758 * hand. 5759 */ 5760 while (*msg != '\0') 5761 c[i++] = *msg++; 5762 5763 for (str = prov->dtpv_name; *str != '\0'; str++) 5764 c[i++] = *str; 5765 c[i++] = ':'; 5766 5767 for (str = probe->dtpr_mod; *str != '\0'; str++) 5768 c[i++] = *str; 5769 c[i++] = ':'; 5770 5771 for (str = probe->dtpr_func; *str != '\0'; str++) 5772 c[i++] = *str; 5773 c[i++] = ':'; 5774 5775 for (str = probe->dtpr_name; *str != '\0'; str++) 5776 c[i++] = *str; 5777 5778 while (*ecbmsg != '\0') 5779 c[i++] = *ecbmsg++; 5780 5781 while (shift >= 0) { 5782 mask = (uintptr_t)0xf << shift; 5783 5784 if (val >= ((uintptr_t)1 << shift)) 5785 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 5786 shift -= 4; 5787 } 5788 5789 c[i++] = ')'; 5790 c[i] = '\0'; 5791 5792#if defined(sun) 5793 debug_enter(c); 5794#else 5795 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 5796#endif 5797} 5798 5799static void 5800dtrace_action_panic(dtrace_ecb_t *ecb) 5801{ 5802 dtrace_probe_t *probe = ecb->dte_probe; 5803 5804 /* 5805 * It's impossible to be taking action on the NULL probe. 5806 */ 5807 ASSERT(probe != NULL); 5808 5809 if (dtrace_destructive_disallow) 5810 return; 5811 5812 if (dtrace_panicked != NULL) 5813 return; 5814 5815 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 5816 return; 5817 5818 /* 5819 * We won the right to panic. (We want to be sure that only one 5820 * thread calls panic() from dtrace_probe(), and that panic() is 5821 * called exactly once.) 5822 */ 5823 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 5824 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 5825 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 5826} 5827 5828static void 5829dtrace_action_raise(uint64_t sig) 5830{ 5831 if (dtrace_destructive_disallow) 5832 return; 5833 5834 if (sig >= NSIG) { 5835 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5836 return; 5837 } 5838 5839#if defined(sun) 5840 /* 5841 * raise() has a queue depth of 1 -- we ignore all subsequent 5842 * invocations of the raise() action. 5843 */ 5844 if (curthread->t_dtrace_sig == 0) 5845 curthread->t_dtrace_sig = (uint8_t)sig; 5846 5847 curthread->t_sig_check = 1; 5848 aston(curthread); 5849#else 5850 struct proc *p = curproc; 5851 PROC_LOCK(p); 5852 kern_psignal(p, sig); 5853 PROC_UNLOCK(p); 5854#endif 5855} 5856 5857static void 5858dtrace_action_stop(void) 5859{ 5860 if (dtrace_destructive_disallow) 5861 return; 5862 5863#if defined(sun) 5864 if (!curthread->t_dtrace_stop) { 5865 curthread->t_dtrace_stop = 1; 5866 curthread->t_sig_check = 1; 5867 aston(curthread); 5868 } 5869#else 5870 struct proc *p = curproc; 5871 PROC_LOCK(p); 5872 kern_psignal(p, SIGSTOP); 5873 PROC_UNLOCK(p); 5874#endif 5875} 5876 5877static void 5878dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 5879{ 5880 hrtime_t now; 5881 volatile uint16_t *flags; 5882#if defined(sun) 5883 cpu_t *cpu = CPU; 5884#else 5885 cpu_t *cpu = &solaris_cpu[curcpu]; 5886#endif 5887 5888 if (dtrace_destructive_disallow) 5889 return; 5890 5891 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 5892 5893 now = dtrace_gethrtime(); 5894 5895 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 5896 /* 5897 * We need to advance the mark to the current time. 5898 */ 5899 cpu->cpu_dtrace_chillmark = now; 5900 cpu->cpu_dtrace_chilled = 0; 5901 } 5902 5903 /* 5904 * Now check to see if the requested chill time would take us over 5905 * the maximum amount of time allowed in the chill interval. (Or 5906 * worse, if the calculation itself induces overflow.) 5907 */ 5908 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 5909 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 5910 *flags |= CPU_DTRACE_ILLOP; 5911 return; 5912 } 5913 5914 while (dtrace_gethrtime() - now < val) 5915 continue; 5916 5917 /* 5918 * Normally, we assure that the value of the variable "timestamp" does 5919 * not change within an ECB. The presence of chill() represents an 5920 * exception to this rule, however. 5921 */ 5922 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 5923 cpu->cpu_dtrace_chilled += val; 5924} 5925 5926static void 5927dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 5928 uint64_t *buf, uint64_t arg) 5929{ 5930 int nframes = DTRACE_USTACK_NFRAMES(arg); 5931 int strsize = DTRACE_USTACK_STRSIZE(arg); 5932 uint64_t *pcs = &buf[1], *fps; 5933 char *str = (char *)&pcs[nframes]; 5934 int size, offs = 0, i, j; 5935 uintptr_t old = mstate->dtms_scratch_ptr, saved; 5936 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 5937 char *sym; 5938 5939 /* 5940 * Should be taking a faster path if string space has not been 5941 * allocated. 5942 */ 5943 ASSERT(strsize != 0); 5944 5945 /* 5946 * We will first allocate some temporary space for the frame pointers. 5947 */ 5948 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5949 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 5950 (nframes * sizeof (uint64_t)); 5951 5952 if (!DTRACE_INSCRATCH(mstate, size)) { 5953 /* 5954 * Not enough room for our frame pointers -- need to indicate 5955 * that we ran out of scratch space. 5956 */ 5957 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5958 return; 5959 } 5960 5961 mstate->dtms_scratch_ptr += size; 5962 saved = mstate->dtms_scratch_ptr; 5963 5964 /* 5965 * Now get a stack with both program counters and frame pointers. 5966 */ 5967 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5968 dtrace_getufpstack(buf, fps, nframes + 1); 5969 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5970 5971 /* 5972 * If that faulted, we're cooked. 5973 */ 5974 if (*flags & CPU_DTRACE_FAULT) 5975 goto out; 5976 5977 /* 5978 * Now we want to walk up the stack, calling the USTACK helper. For 5979 * each iteration, we restore the scratch pointer. 5980 */ 5981 for (i = 0; i < nframes; i++) { 5982 mstate->dtms_scratch_ptr = saved; 5983 5984 if (offs >= strsize) 5985 break; 5986 5987 sym = (char *)(uintptr_t)dtrace_helper( 5988 DTRACE_HELPER_ACTION_USTACK, 5989 mstate, state, pcs[i], fps[i]); 5990 5991 /* 5992 * If we faulted while running the helper, we're going to 5993 * clear the fault and null out the corresponding string. 5994 */ 5995 if (*flags & CPU_DTRACE_FAULT) { 5996 *flags &= ~CPU_DTRACE_FAULT; 5997 str[offs++] = '\0'; 5998 continue; 5999 } 6000 6001 if (sym == NULL) { 6002 str[offs++] = '\0'; 6003 continue; 6004 } 6005 6006 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6007 6008 /* 6009 * Now copy in the string that the helper returned to us. 6010 */ 6011 for (j = 0; offs + j < strsize; j++) { 6012 if ((str[offs + j] = sym[j]) == '\0') 6013 break; 6014 } 6015 6016 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6017 6018 offs += j + 1; 6019 } 6020 6021 if (offs >= strsize) { 6022 /* 6023 * If we didn't have room for all of the strings, we don't 6024 * abort processing -- this needn't be a fatal error -- but we 6025 * still want to increment a counter (dts_stkstroverflows) to 6026 * allow this condition to be warned about. (If this is from 6027 * a jstack() action, it is easily tuned via jstackstrsize.) 6028 */ 6029 dtrace_error(&state->dts_stkstroverflows); 6030 } 6031 6032 while (offs < strsize) 6033 str[offs++] = '\0'; 6034 6035out: 6036 mstate->dtms_scratch_ptr = old; 6037} 6038 6039/* 6040 * If you're looking for the epicenter of DTrace, you just found it. This 6041 * is the function called by the provider to fire a probe -- from which all 6042 * subsequent probe-context DTrace activity emanates. 6043 */ 6044void 6045dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 6046 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 6047{ 6048 processorid_t cpuid; 6049 dtrace_icookie_t cookie; 6050 dtrace_probe_t *probe; 6051 dtrace_mstate_t mstate; 6052 dtrace_ecb_t *ecb; 6053 dtrace_action_t *act; 6054 intptr_t offs; 6055 size_t size; 6056 int vtime, onintr; 6057 volatile uint16_t *flags; 6058 hrtime_t now; 6059 6060 if (panicstr != NULL) 6061 return; 6062 6063#if defined(sun) 6064 /* 6065 * Kick out immediately if this CPU is still being born (in which case 6066 * curthread will be set to -1) or the current thread can't allow 6067 * probes in its current context. 6068 */ 6069 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 6070 return; 6071#endif 6072 6073 cookie = dtrace_interrupt_disable(); 6074 probe = dtrace_probes[id - 1]; 6075 cpuid = curcpu; 6076 onintr = CPU_ON_INTR(CPU); 6077 6078 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 6079 probe->dtpr_predcache == curthread->t_predcache) { 6080 /* 6081 * We have hit in the predicate cache; we know that 6082 * this predicate would evaluate to be false. 6083 */ 6084 dtrace_interrupt_enable(cookie); 6085 return; 6086 } 6087 6088#if defined(sun) 6089 if (panic_quiesce) { 6090#else 6091 if (panicstr != NULL) { 6092#endif 6093 /* 6094 * We don't trace anything if we're panicking. 6095 */ 6096 dtrace_interrupt_enable(cookie); 6097 return; 6098 } 6099 6100 now = dtrace_gethrtime(); 6101 vtime = dtrace_vtime_references != 0; 6102 6103 if (vtime && curthread->t_dtrace_start) 6104 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 6105 6106 mstate.dtms_difo = NULL; 6107 mstate.dtms_probe = probe; 6108 mstate.dtms_strtok = 0; 6109 mstate.dtms_arg[0] = arg0; 6110 mstate.dtms_arg[1] = arg1; 6111 mstate.dtms_arg[2] = arg2; 6112 mstate.dtms_arg[3] = arg3; 6113 mstate.dtms_arg[4] = arg4; 6114 6115 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 6116 6117 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 6118 dtrace_predicate_t *pred = ecb->dte_predicate; 6119 dtrace_state_t *state = ecb->dte_state; 6120 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 6121 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 6122 dtrace_vstate_t *vstate = &state->dts_vstate; 6123 dtrace_provider_t *prov = probe->dtpr_provider; 6124 uint64_t tracememsize = 0; 6125 int committed = 0; 6126 caddr_t tomax; 6127 6128 /* 6129 * A little subtlety with the following (seemingly innocuous) 6130 * declaration of the automatic 'val': by looking at the 6131 * code, you might think that it could be declared in the 6132 * action processing loop, below. (That is, it's only used in 6133 * the action processing loop.) However, it must be declared 6134 * out of that scope because in the case of DIF expression 6135 * arguments to aggregating actions, one iteration of the 6136 * action loop will use the last iteration's value. 6137 */ 6138 uint64_t val = 0; 6139 6140 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 6141 *flags &= ~CPU_DTRACE_ERROR; 6142 6143 if (prov == dtrace_provider) { 6144 /* 6145 * If dtrace itself is the provider of this probe, 6146 * we're only going to continue processing the ECB if 6147 * arg0 (the dtrace_state_t) is equal to the ECB's 6148 * creating state. (This prevents disjoint consumers 6149 * from seeing one another's metaprobes.) 6150 */ 6151 if (arg0 != (uint64_t)(uintptr_t)state) 6152 continue; 6153 } 6154 6155 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 6156 /* 6157 * We're not currently active. If our provider isn't 6158 * the dtrace pseudo provider, we're not interested. 6159 */ 6160 if (prov != dtrace_provider) 6161 continue; 6162 6163 /* 6164 * Now we must further check if we are in the BEGIN 6165 * probe. If we are, we will only continue processing 6166 * if we're still in WARMUP -- if one BEGIN enabling 6167 * has invoked the exit() action, we don't want to 6168 * evaluate subsequent BEGIN enablings. 6169 */ 6170 if (probe->dtpr_id == dtrace_probeid_begin && 6171 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 6172 ASSERT(state->dts_activity == 6173 DTRACE_ACTIVITY_DRAINING); 6174 continue; 6175 } 6176 } 6177 6178 if (ecb->dte_cond) { 6179 /* 6180 * If the dte_cond bits indicate that this 6181 * consumer is only allowed to see user-mode firings 6182 * of this probe, call the provider's dtps_usermode() 6183 * entry point to check that the probe was fired 6184 * while in a user context. Skip this ECB if that's 6185 * not the case. 6186 */ 6187 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 6188 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 6189 probe->dtpr_id, probe->dtpr_arg) == 0) 6190 continue; 6191 6192#if defined(sun) 6193 /* 6194 * This is more subtle than it looks. We have to be 6195 * absolutely certain that CRED() isn't going to 6196 * change out from under us so it's only legit to 6197 * examine that structure if we're in constrained 6198 * situations. Currently, the only times we'll this 6199 * check is if a non-super-user has enabled the 6200 * profile or syscall providers -- providers that 6201 * allow visibility of all processes. For the 6202 * profile case, the check above will ensure that 6203 * we're examining a user context. 6204 */ 6205 if (ecb->dte_cond & DTRACE_COND_OWNER) { 6206 cred_t *cr; 6207 cred_t *s_cr = 6208 ecb->dte_state->dts_cred.dcr_cred; 6209 proc_t *proc; 6210 6211 ASSERT(s_cr != NULL); 6212 6213 if ((cr = CRED()) == NULL || 6214 s_cr->cr_uid != cr->cr_uid || 6215 s_cr->cr_uid != cr->cr_ruid || 6216 s_cr->cr_uid != cr->cr_suid || 6217 s_cr->cr_gid != cr->cr_gid || 6218 s_cr->cr_gid != cr->cr_rgid || 6219 s_cr->cr_gid != cr->cr_sgid || 6220 (proc = ttoproc(curthread)) == NULL || 6221 (proc->p_flag & SNOCD)) 6222 continue; 6223 } 6224 6225 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 6226 cred_t *cr; 6227 cred_t *s_cr = 6228 ecb->dte_state->dts_cred.dcr_cred; 6229 6230 ASSERT(s_cr != NULL); 6231 6232 if ((cr = CRED()) == NULL || 6233 s_cr->cr_zone->zone_id != 6234 cr->cr_zone->zone_id) 6235 continue; 6236 } 6237#endif 6238 } 6239 6240 if (now - state->dts_alive > dtrace_deadman_timeout) { 6241 /* 6242 * We seem to be dead. Unless we (a) have kernel 6243 * destructive permissions (b) have explicitly enabled 6244 * destructive actions and (c) destructive actions have 6245 * not been disabled, we're going to transition into 6246 * the KILLED state, from which no further processing 6247 * on this state will be performed. 6248 */ 6249 if (!dtrace_priv_kernel_destructive(state) || 6250 !state->dts_cred.dcr_destructive || 6251 dtrace_destructive_disallow) { 6252 void *activity = &state->dts_activity; 6253 dtrace_activity_t current; 6254 6255 do { 6256 current = state->dts_activity; 6257 } while (dtrace_cas32(activity, current, 6258 DTRACE_ACTIVITY_KILLED) != current); 6259 6260 continue; 6261 } 6262 } 6263 6264 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 6265 ecb->dte_alignment, state, &mstate)) < 0) 6266 continue; 6267 6268 tomax = buf->dtb_tomax; 6269 ASSERT(tomax != NULL); 6270 6271 if (ecb->dte_size != 0) { 6272 dtrace_rechdr_t dtrh; 6273 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 6274 mstate.dtms_timestamp = dtrace_gethrtime(); 6275 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 6276 } 6277 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 6278 dtrh.dtrh_epid = ecb->dte_epid; 6279 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 6280 mstate.dtms_timestamp); 6281 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 6282 } 6283 6284 mstate.dtms_epid = ecb->dte_epid; 6285 mstate.dtms_present |= DTRACE_MSTATE_EPID; 6286 6287 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 6288 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 6289 else 6290 mstate.dtms_access = 0; 6291 6292 if (pred != NULL) { 6293 dtrace_difo_t *dp = pred->dtp_difo; 6294 int rval; 6295 6296 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 6297 6298 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 6299 dtrace_cacheid_t cid = probe->dtpr_predcache; 6300 6301 if (cid != DTRACE_CACHEIDNONE && !onintr) { 6302 /* 6303 * Update the predicate cache... 6304 */ 6305 ASSERT(cid == pred->dtp_cacheid); 6306 curthread->t_predcache = cid; 6307 } 6308 6309 continue; 6310 } 6311 } 6312 6313 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 6314 act != NULL; act = act->dta_next) { 6315 size_t valoffs; 6316 dtrace_difo_t *dp; 6317 dtrace_recdesc_t *rec = &act->dta_rec; 6318 6319 size = rec->dtrd_size; 6320 valoffs = offs + rec->dtrd_offset; 6321 6322 if (DTRACEACT_ISAGG(act->dta_kind)) { 6323 uint64_t v = 0xbad; 6324 dtrace_aggregation_t *agg; 6325 6326 agg = (dtrace_aggregation_t *)act; 6327 6328 if ((dp = act->dta_difo) != NULL) 6329 v = dtrace_dif_emulate(dp, 6330 &mstate, vstate, state); 6331 6332 if (*flags & CPU_DTRACE_ERROR) 6333 continue; 6334 6335 /* 6336 * Note that we always pass the expression 6337 * value from the previous iteration of the 6338 * action loop. This value will only be used 6339 * if there is an expression argument to the 6340 * aggregating action, denoted by the 6341 * dtag_hasarg field. 6342 */ 6343 dtrace_aggregate(agg, buf, 6344 offs, aggbuf, v, val); 6345 continue; 6346 } 6347 6348 switch (act->dta_kind) { 6349 case DTRACEACT_STOP: 6350 if (dtrace_priv_proc_destructive(state)) 6351 dtrace_action_stop(); 6352 continue; 6353 6354 case DTRACEACT_BREAKPOINT: 6355 if (dtrace_priv_kernel_destructive(state)) 6356 dtrace_action_breakpoint(ecb); 6357 continue; 6358 6359 case DTRACEACT_PANIC: 6360 if (dtrace_priv_kernel_destructive(state)) 6361 dtrace_action_panic(ecb); 6362 continue; 6363 6364 case DTRACEACT_STACK: 6365 if (!dtrace_priv_kernel(state)) 6366 continue; 6367 6368 dtrace_getpcstack((pc_t *)(tomax + valoffs), 6369 size / sizeof (pc_t), probe->dtpr_aframes, 6370 DTRACE_ANCHORED(probe) ? NULL : 6371 (uint32_t *)arg0); 6372 continue; 6373 6374 case DTRACEACT_JSTACK: 6375 case DTRACEACT_USTACK: 6376 if (!dtrace_priv_proc(state)) 6377 continue; 6378 6379 /* 6380 * See comment in DIF_VAR_PID. 6381 */ 6382 if (DTRACE_ANCHORED(mstate.dtms_probe) && 6383 CPU_ON_INTR(CPU)) { 6384 int depth = DTRACE_USTACK_NFRAMES( 6385 rec->dtrd_arg) + 1; 6386 6387 dtrace_bzero((void *)(tomax + valoffs), 6388 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 6389 + depth * sizeof (uint64_t)); 6390 6391 continue; 6392 } 6393 6394 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 6395 curproc->p_dtrace_helpers != NULL) { 6396 /* 6397 * This is the slow path -- we have 6398 * allocated string space, and we're 6399 * getting the stack of a process that 6400 * has helpers. Call into a separate 6401 * routine to perform this processing. 6402 */ 6403 dtrace_action_ustack(&mstate, state, 6404 (uint64_t *)(tomax + valoffs), 6405 rec->dtrd_arg); 6406 continue; 6407 } 6408 6409 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6410 dtrace_getupcstack((uint64_t *) 6411 (tomax + valoffs), 6412 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 6413 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6414 continue; 6415 6416 default: 6417 break; 6418 } 6419 6420 dp = act->dta_difo; 6421 ASSERT(dp != NULL); 6422 6423 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 6424 6425 if (*flags & CPU_DTRACE_ERROR) 6426 continue; 6427 6428 switch (act->dta_kind) { 6429 case DTRACEACT_SPECULATE: { 6430 dtrace_rechdr_t *dtrh; 6431 6432 ASSERT(buf == &state->dts_buffer[cpuid]); 6433 buf = dtrace_speculation_buffer(state, 6434 cpuid, val); 6435 6436 if (buf == NULL) { 6437 *flags |= CPU_DTRACE_DROP; 6438 continue; 6439 } 6440 6441 offs = dtrace_buffer_reserve(buf, 6442 ecb->dte_needed, ecb->dte_alignment, 6443 state, NULL); 6444 6445 if (offs < 0) { 6446 *flags |= CPU_DTRACE_DROP; 6447 continue; 6448 } 6449 6450 tomax = buf->dtb_tomax; 6451 ASSERT(tomax != NULL); 6452 6453 if (ecb->dte_size == 0) 6454 continue; 6455 6456 ASSERT3U(ecb->dte_size, >=, 6457 sizeof (dtrace_rechdr_t)); 6458 dtrh = ((void *)(tomax + offs)); 6459 dtrh->dtrh_epid = ecb->dte_epid; 6460 /* 6461 * When the speculation is committed, all of 6462 * the records in the speculative buffer will 6463 * have their timestamps set to the commit 6464 * time. Until then, it is set to a sentinel 6465 * value, for debugability. 6466 */ 6467 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 6468 continue; 6469 } 6470 6471 case DTRACEACT_PRINTM: { 6472 /* The DIF returns a 'memref'. */ 6473 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 6474 6475 /* Get the size from the memref. */ 6476 size = memref[1]; 6477 6478 /* 6479 * Check if the size exceeds the allocated 6480 * buffer size. 6481 */ 6482 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 6483 /* Flag a drop! */ 6484 *flags |= CPU_DTRACE_DROP; 6485 continue; 6486 } 6487 6488 /* Store the size in the buffer first. */ 6489 DTRACE_STORE(uintptr_t, tomax, 6490 valoffs, size); 6491 6492 /* 6493 * Offset the buffer address to the start 6494 * of the data. 6495 */ 6496 valoffs += sizeof(uintptr_t); 6497 6498 /* 6499 * Reset to the memory address rather than 6500 * the memref array, then let the BYREF 6501 * code below do the work to store the 6502 * memory data in the buffer. 6503 */ 6504 val = memref[0]; 6505 break; 6506 } 6507 6508 case DTRACEACT_PRINTT: { 6509 /* The DIF returns a 'typeref'. */ 6510 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 6511 char c = '\0' + 1; 6512 size_t s; 6513 6514 /* 6515 * Get the type string length and round it 6516 * up so that the data that follows is 6517 * aligned for easy access. 6518 */ 6519 size_t typs = strlen((char *) typeref[2]) + 1; 6520 typs = roundup(typs, sizeof(uintptr_t)); 6521 6522 /* 6523 *Get the size from the typeref using the 6524 * number of elements and the type size. 6525 */ 6526 size = typeref[1] * typeref[3]; 6527 6528 /* 6529 * Check if the size exceeds the allocated 6530 * buffer size. 6531 */ 6532 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 6533 /* Flag a drop! */ 6534 *flags |= CPU_DTRACE_DROP; 6535 6536 } 6537 6538 /* Store the size in the buffer first. */ 6539 DTRACE_STORE(uintptr_t, tomax, 6540 valoffs, size); 6541 valoffs += sizeof(uintptr_t); 6542 6543 /* Store the type size in the buffer. */ 6544 DTRACE_STORE(uintptr_t, tomax, 6545 valoffs, typeref[3]); 6546 valoffs += sizeof(uintptr_t); 6547 6548 val = typeref[2]; 6549 6550 for (s = 0; s < typs; s++) { 6551 if (c != '\0') 6552 c = dtrace_load8(val++); 6553 6554 DTRACE_STORE(uint8_t, tomax, 6555 valoffs++, c); 6556 } 6557 6558 /* 6559 * Reset to the memory address rather than 6560 * the typeref array, then let the BYREF 6561 * code below do the work to store the 6562 * memory data in the buffer. 6563 */ 6564 val = typeref[0]; 6565 break; 6566 } 6567 6568 case DTRACEACT_CHILL: 6569 if (dtrace_priv_kernel_destructive(state)) 6570 dtrace_action_chill(&mstate, val); 6571 continue; 6572 6573 case DTRACEACT_RAISE: 6574 if (dtrace_priv_proc_destructive(state)) 6575 dtrace_action_raise(val); 6576 continue; 6577 6578 case DTRACEACT_COMMIT: 6579 ASSERT(!committed); 6580 6581 /* 6582 * We need to commit our buffer state. 6583 */ 6584 if (ecb->dte_size) 6585 buf->dtb_offset = offs + ecb->dte_size; 6586 buf = &state->dts_buffer[cpuid]; 6587 dtrace_speculation_commit(state, cpuid, val); 6588 committed = 1; 6589 continue; 6590 6591 case DTRACEACT_DISCARD: 6592 dtrace_speculation_discard(state, cpuid, val); 6593 continue; 6594 6595 case DTRACEACT_DIFEXPR: 6596 case DTRACEACT_LIBACT: 6597 case DTRACEACT_PRINTF: 6598 case DTRACEACT_PRINTA: 6599 case DTRACEACT_SYSTEM: 6600 case DTRACEACT_FREOPEN: 6601 case DTRACEACT_TRACEMEM: 6602 break; 6603 6604 case DTRACEACT_TRACEMEM_DYNSIZE: 6605 tracememsize = val; 6606 break; 6607 6608 case DTRACEACT_SYM: 6609 case DTRACEACT_MOD: 6610 if (!dtrace_priv_kernel(state)) 6611 continue; 6612 break; 6613 6614 case DTRACEACT_USYM: 6615 case DTRACEACT_UMOD: 6616 case DTRACEACT_UADDR: { 6617#if defined(sun) 6618 struct pid *pid = curthread->t_procp->p_pidp; 6619#endif 6620 6621 if (!dtrace_priv_proc(state)) 6622 continue; 6623 6624 DTRACE_STORE(uint64_t, tomax, 6625#if defined(sun) 6626 valoffs, (uint64_t)pid->pid_id); 6627#else 6628 valoffs, (uint64_t) curproc->p_pid); 6629#endif 6630 DTRACE_STORE(uint64_t, tomax, 6631 valoffs + sizeof (uint64_t), val); 6632 6633 continue; 6634 } 6635 6636 case DTRACEACT_EXIT: { 6637 /* 6638 * For the exit action, we are going to attempt 6639 * to atomically set our activity to be 6640 * draining. If this fails (either because 6641 * another CPU has beat us to the exit action, 6642 * or because our current activity is something 6643 * other than ACTIVE or WARMUP), we will 6644 * continue. This assures that the exit action 6645 * can be successfully recorded at most once 6646 * when we're in the ACTIVE state. If we're 6647 * encountering the exit() action while in 6648 * COOLDOWN, however, we want to honor the new 6649 * status code. (We know that we're the only 6650 * thread in COOLDOWN, so there is no race.) 6651 */ 6652 void *activity = &state->dts_activity; 6653 dtrace_activity_t current = state->dts_activity; 6654 6655 if (current == DTRACE_ACTIVITY_COOLDOWN) 6656 break; 6657 6658 if (current != DTRACE_ACTIVITY_WARMUP) 6659 current = DTRACE_ACTIVITY_ACTIVE; 6660 6661 if (dtrace_cas32(activity, current, 6662 DTRACE_ACTIVITY_DRAINING) != current) { 6663 *flags |= CPU_DTRACE_DROP; 6664 continue; 6665 } 6666 6667 break; 6668 } 6669 6670 default: 6671 ASSERT(0); 6672 } 6673 6674 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 6675 uintptr_t end = valoffs + size; 6676 6677 if (tracememsize != 0 && 6678 valoffs + tracememsize < end) { 6679 end = valoffs + tracememsize; 6680 tracememsize = 0; 6681 } 6682 6683 if (!dtrace_vcanload((void *)(uintptr_t)val, 6684 &dp->dtdo_rtype, &mstate, vstate)) 6685 continue; 6686 6687 /* 6688 * If this is a string, we're going to only 6689 * load until we find the zero byte -- after 6690 * which we'll store zero bytes. 6691 */ 6692 if (dp->dtdo_rtype.dtdt_kind == 6693 DIF_TYPE_STRING) { 6694 char c = '\0' + 1; 6695 int intuple = act->dta_intuple; 6696 size_t s; 6697 6698 for (s = 0; s < size; s++) { 6699 if (c != '\0') 6700 c = dtrace_load8(val++); 6701 6702 DTRACE_STORE(uint8_t, tomax, 6703 valoffs++, c); 6704 6705 if (c == '\0' && intuple) 6706 break; 6707 } 6708 6709 continue; 6710 } 6711 6712 while (valoffs < end) { 6713 DTRACE_STORE(uint8_t, tomax, valoffs++, 6714 dtrace_load8(val++)); 6715 } 6716 6717 continue; 6718 } 6719 6720 switch (size) { 6721 case 0: 6722 break; 6723 6724 case sizeof (uint8_t): 6725 DTRACE_STORE(uint8_t, tomax, valoffs, val); 6726 break; 6727 case sizeof (uint16_t): 6728 DTRACE_STORE(uint16_t, tomax, valoffs, val); 6729 break; 6730 case sizeof (uint32_t): 6731 DTRACE_STORE(uint32_t, tomax, valoffs, val); 6732 break; 6733 case sizeof (uint64_t): 6734 DTRACE_STORE(uint64_t, tomax, valoffs, val); 6735 break; 6736 default: 6737 /* 6738 * Any other size should have been returned by 6739 * reference, not by value. 6740 */ 6741 ASSERT(0); 6742 break; 6743 } 6744 } 6745 6746 if (*flags & CPU_DTRACE_DROP) 6747 continue; 6748 6749 if (*flags & CPU_DTRACE_FAULT) { 6750 int ndx; 6751 dtrace_action_t *err; 6752 6753 buf->dtb_errors++; 6754 6755 if (probe->dtpr_id == dtrace_probeid_error) { 6756 /* 6757 * There's nothing we can do -- we had an 6758 * error on the error probe. We bump an 6759 * error counter to at least indicate that 6760 * this condition happened. 6761 */ 6762 dtrace_error(&state->dts_dblerrors); 6763 continue; 6764 } 6765 6766 if (vtime) { 6767 /* 6768 * Before recursing on dtrace_probe(), we 6769 * need to explicitly clear out our start 6770 * time to prevent it from being accumulated 6771 * into t_dtrace_vtime. 6772 */ 6773 curthread->t_dtrace_start = 0; 6774 } 6775 6776 /* 6777 * Iterate over the actions to figure out which action 6778 * we were processing when we experienced the error. 6779 * Note that act points _past_ the faulting action; if 6780 * act is ecb->dte_action, the fault was in the 6781 * predicate, if it's ecb->dte_action->dta_next it's 6782 * in action #1, and so on. 6783 */ 6784 for (err = ecb->dte_action, ndx = 0; 6785 err != act; err = err->dta_next, ndx++) 6786 continue; 6787 6788 dtrace_probe_error(state, ecb->dte_epid, ndx, 6789 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 6790 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 6791 cpu_core[cpuid].cpuc_dtrace_illval); 6792 6793 continue; 6794 } 6795 6796 if (!committed) 6797 buf->dtb_offset = offs + ecb->dte_size; 6798 } 6799 6800 if (vtime) 6801 curthread->t_dtrace_start = dtrace_gethrtime(); 6802 6803 dtrace_interrupt_enable(cookie); 6804} 6805 6806/* 6807 * DTrace Probe Hashing Functions 6808 * 6809 * The functions in this section (and indeed, the functions in remaining 6810 * sections) are not _called_ from probe context. (Any exceptions to this are 6811 * marked with a "Note:".) Rather, they are called from elsewhere in the 6812 * DTrace framework to look-up probes in, add probes to and remove probes from 6813 * the DTrace probe hashes. (Each probe is hashed by each element of the 6814 * probe tuple -- allowing for fast lookups, regardless of what was 6815 * specified.) 6816 */ 6817static uint_t 6818dtrace_hash_str(const char *p) 6819{ 6820 unsigned int g; 6821 uint_t hval = 0; 6822 6823 while (*p) { 6824 hval = (hval << 4) + *p++; 6825 if ((g = (hval & 0xf0000000)) != 0) 6826 hval ^= g >> 24; 6827 hval &= ~g; 6828 } 6829 return (hval); 6830} 6831 6832static dtrace_hash_t * 6833dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 6834{ 6835 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 6836 6837 hash->dth_stroffs = stroffs; 6838 hash->dth_nextoffs = nextoffs; 6839 hash->dth_prevoffs = prevoffs; 6840 6841 hash->dth_size = 1; 6842 hash->dth_mask = hash->dth_size - 1; 6843 6844 hash->dth_tab = kmem_zalloc(hash->dth_size * 6845 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 6846 6847 return (hash); 6848} 6849 6850static void 6851dtrace_hash_destroy(dtrace_hash_t *hash) 6852{ 6853#ifdef DEBUG 6854 int i; 6855 6856 for (i = 0; i < hash->dth_size; i++) 6857 ASSERT(hash->dth_tab[i] == NULL); 6858#endif 6859 6860 kmem_free(hash->dth_tab, 6861 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 6862 kmem_free(hash, sizeof (dtrace_hash_t)); 6863} 6864 6865static void 6866dtrace_hash_resize(dtrace_hash_t *hash) 6867{ 6868 int size = hash->dth_size, i, ndx; 6869 int new_size = hash->dth_size << 1; 6870 int new_mask = new_size - 1; 6871 dtrace_hashbucket_t **new_tab, *bucket, *next; 6872 6873 ASSERT((new_size & new_mask) == 0); 6874 6875 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 6876 6877 for (i = 0; i < size; i++) { 6878 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 6879 dtrace_probe_t *probe = bucket->dthb_chain; 6880 6881 ASSERT(probe != NULL); 6882 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 6883 6884 next = bucket->dthb_next; 6885 bucket->dthb_next = new_tab[ndx]; 6886 new_tab[ndx] = bucket; 6887 } 6888 } 6889 6890 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 6891 hash->dth_tab = new_tab; 6892 hash->dth_size = new_size; 6893 hash->dth_mask = new_mask; 6894} 6895 6896static void 6897dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 6898{ 6899 int hashval = DTRACE_HASHSTR(hash, new); 6900 int ndx = hashval & hash->dth_mask; 6901 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6902 dtrace_probe_t **nextp, **prevp; 6903 6904 for (; bucket != NULL; bucket = bucket->dthb_next) { 6905 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 6906 goto add; 6907 } 6908 6909 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 6910 dtrace_hash_resize(hash); 6911 dtrace_hash_add(hash, new); 6912 return; 6913 } 6914 6915 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 6916 bucket->dthb_next = hash->dth_tab[ndx]; 6917 hash->dth_tab[ndx] = bucket; 6918 hash->dth_nbuckets++; 6919 6920add: 6921 nextp = DTRACE_HASHNEXT(hash, new); 6922 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 6923 *nextp = bucket->dthb_chain; 6924 6925 if (bucket->dthb_chain != NULL) { 6926 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 6927 ASSERT(*prevp == NULL); 6928 *prevp = new; 6929 } 6930 6931 bucket->dthb_chain = new; 6932 bucket->dthb_len++; 6933} 6934 6935static dtrace_probe_t * 6936dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 6937{ 6938 int hashval = DTRACE_HASHSTR(hash, template); 6939 int ndx = hashval & hash->dth_mask; 6940 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6941 6942 for (; bucket != NULL; bucket = bucket->dthb_next) { 6943 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6944 return (bucket->dthb_chain); 6945 } 6946 6947 return (NULL); 6948} 6949 6950static int 6951dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 6952{ 6953 int hashval = DTRACE_HASHSTR(hash, template); 6954 int ndx = hashval & hash->dth_mask; 6955 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6956 6957 for (; bucket != NULL; bucket = bucket->dthb_next) { 6958 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6959 return (bucket->dthb_len); 6960 } 6961 6962 return (0); 6963} 6964 6965static void 6966dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 6967{ 6968 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 6969 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6970 6971 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 6972 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 6973 6974 /* 6975 * Find the bucket that we're removing this probe from. 6976 */ 6977 for (; bucket != NULL; bucket = bucket->dthb_next) { 6978 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 6979 break; 6980 } 6981 6982 ASSERT(bucket != NULL); 6983 6984 if (*prevp == NULL) { 6985 if (*nextp == NULL) { 6986 /* 6987 * The removed probe was the only probe on this 6988 * bucket; we need to remove the bucket. 6989 */ 6990 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 6991 6992 ASSERT(bucket->dthb_chain == probe); 6993 ASSERT(b != NULL); 6994 6995 if (b == bucket) { 6996 hash->dth_tab[ndx] = bucket->dthb_next; 6997 } else { 6998 while (b->dthb_next != bucket) 6999 b = b->dthb_next; 7000 b->dthb_next = bucket->dthb_next; 7001 } 7002 7003 ASSERT(hash->dth_nbuckets > 0); 7004 hash->dth_nbuckets--; 7005 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 7006 return; 7007 } 7008 7009 bucket->dthb_chain = *nextp; 7010 } else { 7011 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 7012 } 7013 7014 if (*nextp != NULL) 7015 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 7016} 7017 7018/* 7019 * DTrace Utility Functions 7020 * 7021 * These are random utility functions that are _not_ called from probe context. 7022 */ 7023static int 7024dtrace_badattr(const dtrace_attribute_t *a) 7025{ 7026 return (a->dtat_name > DTRACE_STABILITY_MAX || 7027 a->dtat_data > DTRACE_STABILITY_MAX || 7028 a->dtat_class > DTRACE_CLASS_MAX); 7029} 7030 7031/* 7032 * Return a duplicate copy of a string. If the specified string is NULL, 7033 * this function returns a zero-length string. 7034 */ 7035static char * 7036dtrace_strdup(const char *str) 7037{ 7038 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 7039 7040 if (str != NULL) 7041 (void) strcpy(new, str); 7042 7043 return (new); 7044} 7045 7046#define DTRACE_ISALPHA(c) \ 7047 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 7048 7049static int 7050dtrace_badname(const char *s) 7051{ 7052 char c; 7053 7054 if (s == NULL || (c = *s++) == '\0') 7055 return (0); 7056 7057 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 7058 return (1); 7059 7060 while ((c = *s++) != '\0') { 7061 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 7062 c != '-' && c != '_' && c != '.' && c != '`') 7063 return (1); 7064 } 7065 7066 return (0); 7067} 7068 7069static void 7070dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 7071{ 7072 uint32_t priv; 7073 7074#if defined(sun) 7075 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 7076 /* 7077 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 7078 */ 7079 priv = DTRACE_PRIV_ALL; 7080 } else { 7081 *uidp = crgetuid(cr); 7082 *zoneidp = crgetzoneid(cr); 7083 7084 priv = 0; 7085 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 7086 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 7087 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 7088 priv |= DTRACE_PRIV_USER; 7089 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 7090 priv |= DTRACE_PRIV_PROC; 7091 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 7092 priv |= DTRACE_PRIV_OWNER; 7093 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 7094 priv |= DTRACE_PRIV_ZONEOWNER; 7095 } 7096#else 7097 priv = DTRACE_PRIV_ALL; 7098#endif 7099 7100 *privp = priv; 7101} 7102 7103#ifdef DTRACE_ERRDEBUG 7104static void 7105dtrace_errdebug(const char *str) 7106{ 7107 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 7108 int occupied = 0; 7109 7110 mutex_enter(&dtrace_errlock); 7111 dtrace_errlast = str; 7112 dtrace_errthread = curthread; 7113 7114 while (occupied++ < DTRACE_ERRHASHSZ) { 7115 if (dtrace_errhash[hval].dter_msg == str) { 7116 dtrace_errhash[hval].dter_count++; 7117 goto out; 7118 } 7119 7120 if (dtrace_errhash[hval].dter_msg != NULL) { 7121 hval = (hval + 1) % DTRACE_ERRHASHSZ; 7122 continue; 7123 } 7124 7125 dtrace_errhash[hval].dter_msg = str; 7126 dtrace_errhash[hval].dter_count = 1; 7127 goto out; 7128 } 7129 7130 panic("dtrace: undersized error hash"); 7131out: 7132 mutex_exit(&dtrace_errlock); 7133} 7134#endif 7135 7136/* 7137 * DTrace Matching Functions 7138 * 7139 * These functions are used to match groups of probes, given some elements of 7140 * a probe tuple, or some globbed expressions for elements of a probe tuple. 7141 */ 7142static int 7143dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 7144 zoneid_t zoneid) 7145{ 7146 if (priv != DTRACE_PRIV_ALL) { 7147 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 7148 uint32_t match = priv & ppriv; 7149 7150 /* 7151 * No PRIV_DTRACE_* privileges... 7152 */ 7153 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 7154 DTRACE_PRIV_KERNEL)) == 0) 7155 return (0); 7156 7157 /* 7158 * No matching bits, but there were bits to match... 7159 */ 7160 if (match == 0 && ppriv != 0) 7161 return (0); 7162 7163 /* 7164 * Need to have permissions to the process, but don't... 7165 */ 7166 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 7167 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 7168 return (0); 7169 } 7170 7171 /* 7172 * Need to be in the same zone unless we possess the 7173 * privilege to examine all zones. 7174 */ 7175 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 7176 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 7177 return (0); 7178 } 7179 } 7180 7181 return (1); 7182} 7183 7184/* 7185 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 7186 * consists of input pattern strings and an ops-vector to evaluate them. 7187 * This function returns >0 for match, 0 for no match, and <0 for error. 7188 */ 7189static int 7190dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 7191 uint32_t priv, uid_t uid, zoneid_t zoneid) 7192{ 7193 dtrace_provider_t *pvp = prp->dtpr_provider; 7194 int rv; 7195 7196 if (pvp->dtpv_defunct) 7197 return (0); 7198 7199 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 7200 return (rv); 7201 7202 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 7203 return (rv); 7204 7205 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 7206 return (rv); 7207 7208 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 7209 return (rv); 7210 7211 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 7212 return (0); 7213 7214 return (rv); 7215} 7216 7217/* 7218 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 7219 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 7220 * libc's version, the kernel version only applies to 8-bit ASCII strings. 7221 * In addition, all of the recursion cases except for '*' matching have been 7222 * unwound. For '*', we still implement recursive evaluation, but a depth 7223 * counter is maintained and matching is aborted if we recurse too deep. 7224 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 7225 */ 7226static int 7227dtrace_match_glob(const char *s, const char *p, int depth) 7228{ 7229 const char *olds; 7230 char s1, c; 7231 int gs; 7232 7233 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 7234 return (-1); 7235 7236 if (s == NULL) 7237 s = ""; /* treat NULL as empty string */ 7238 7239top: 7240 olds = s; 7241 s1 = *s++; 7242 7243 if (p == NULL) 7244 return (0); 7245 7246 if ((c = *p++) == '\0') 7247 return (s1 == '\0'); 7248 7249 switch (c) { 7250 case '[': { 7251 int ok = 0, notflag = 0; 7252 char lc = '\0'; 7253 7254 if (s1 == '\0') 7255 return (0); 7256 7257 if (*p == '!') { 7258 notflag = 1; 7259 p++; 7260 } 7261 7262 if ((c = *p++) == '\0') 7263 return (0); 7264 7265 do { 7266 if (c == '-' && lc != '\0' && *p != ']') { 7267 if ((c = *p++) == '\0') 7268 return (0); 7269 if (c == '\\' && (c = *p++) == '\0') 7270 return (0); 7271 7272 if (notflag) { 7273 if (s1 < lc || s1 > c) 7274 ok++; 7275 else 7276 return (0); 7277 } else if (lc <= s1 && s1 <= c) 7278 ok++; 7279 7280 } else if (c == '\\' && (c = *p++) == '\0') 7281 return (0); 7282 7283 lc = c; /* save left-hand 'c' for next iteration */ 7284 7285 if (notflag) { 7286 if (s1 != c) 7287 ok++; 7288 else 7289 return (0); 7290 } else if (s1 == c) 7291 ok++; 7292 7293 if ((c = *p++) == '\0') 7294 return (0); 7295 7296 } while (c != ']'); 7297 7298 if (ok) 7299 goto top; 7300 7301 return (0); 7302 } 7303 7304 case '\\': 7305 if ((c = *p++) == '\0') 7306 return (0); 7307 /*FALLTHRU*/ 7308 7309 default: 7310 if (c != s1) 7311 return (0); 7312 /*FALLTHRU*/ 7313 7314 case '?': 7315 if (s1 != '\0') 7316 goto top; 7317 return (0); 7318 7319 case '*': 7320 while (*p == '*') 7321 p++; /* consecutive *'s are identical to a single one */ 7322 7323 if (*p == '\0') 7324 return (1); 7325 7326 for (s = olds; *s != '\0'; s++) { 7327 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 7328 return (gs); 7329 } 7330 7331 return (0); 7332 } 7333} 7334 7335/*ARGSUSED*/ 7336static int 7337dtrace_match_string(const char *s, const char *p, int depth) 7338{ 7339 return (s != NULL && strcmp(s, p) == 0); 7340} 7341 7342/*ARGSUSED*/ 7343static int 7344dtrace_match_nul(const char *s, const char *p, int depth) 7345{ 7346 return (1); /* always match the empty pattern */ 7347} 7348 7349/*ARGSUSED*/ 7350static int 7351dtrace_match_nonzero(const char *s, const char *p, int depth) 7352{ 7353 return (s != NULL && s[0] != '\0'); 7354} 7355 7356static int 7357dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 7358 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 7359{ 7360 dtrace_probe_t template, *probe; 7361 dtrace_hash_t *hash = NULL; 7362 int len, best = INT_MAX, nmatched = 0; 7363 dtrace_id_t i; 7364 7365 ASSERT(MUTEX_HELD(&dtrace_lock)); 7366 7367 /* 7368 * If the probe ID is specified in the key, just lookup by ID and 7369 * invoke the match callback once if a matching probe is found. 7370 */ 7371 if (pkp->dtpk_id != DTRACE_IDNONE) { 7372 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 7373 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 7374 (void) (*matched)(probe, arg); 7375 nmatched++; 7376 } 7377 return (nmatched); 7378 } 7379 7380 template.dtpr_mod = (char *)pkp->dtpk_mod; 7381 template.dtpr_func = (char *)pkp->dtpk_func; 7382 template.dtpr_name = (char *)pkp->dtpk_name; 7383 7384 /* 7385 * We want to find the most distinct of the module name, function 7386 * name, and name. So for each one that is not a glob pattern or 7387 * empty string, we perform a lookup in the corresponding hash and 7388 * use the hash table with the fewest collisions to do our search. 7389 */ 7390 if (pkp->dtpk_mmatch == &dtrace_match_string && 7391 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 7392 best = len; 7393 hash = dtrace_bymod; 7394 } 7395 7396 if (pkp->dtpk_fmatch == &dtrace_match_string && 7397 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 7398 best = len; 7399 hash = dtrace_byfunc; 7400 } 7401 7402 if (pkp->dtpk_nmatch == &dtrace_match_string && 7403 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 7404 best = len; 7405 hash = dtrace_byname; 7406 } 7407 7408 /* 7409 * If we did not select a hash table, iterate over every probe and 7410 * invoke our callback for each one that matches our input probe key. 7411 */ 7412 if (hash == NULL) { 7413 for (i = 0; i < dtrace_nprobes; i++) { 7414 if ((probe = dtrace_probes[i]) == NULL || 7415 dtrace_match_probe(probe, pkp, priv, uid, 7416 zoneid) <= 0) 7417 continue; 7418 7419 nmatched++; 7420 7421 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 7422 break; 7423 } 7424 7425 return (nmatched); 7426 } 7427 7428 /* 7429 * If we selected a hash table, iterate over each probe of the same key 7430 * name and invoke the callback for every probe that matches the other 7431 * attributes of our input probe key. 7432 */ 7433 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 7434 probe = *(DTRACE_HASHNEXT(hash, probe))) { 7435 7436 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 7437 continue; 7438 7439 nmatched++; 7440 7441 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 7442 break; 7443 } 7444 7445 return (nmatched); 7446} 7447 7448/* 7449 * Return the function pointer dtrace_probecmp() should use to compare the 7450 * specified pattern with a string. For NULL or empty patterns, we select 7451 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 7452 * For non-empty non-glob strings, we use dtrace_match_string(). 7453 */ 7454static dtrace_probekey_f * 7455dtrace_probekey_func(const char *p) 7456{ 7457 char c; 7458 7459 if (p == NULL || *p == '\0') 7460 return (&dtrace_match_nul); 7461 7462 while ((c = *p++) != '\0') { 7463 if (c == '[' || c == '?' || c == '*' || c == '\\') 7464 return (&dtrace_match_glob); 7465 } 7466 7467 return (&dtrace_match_string); 7468} 7469 7470/* 7471 * Build a probe comparison key for use with dtrace_match_probe() from the 7472 * given probe description. By convention, a null key only matches anchored 7473 * probes: if each field is the empty string, reset dtpk_fmatch to 7474 * dtrace_match_nonzero(). 7475 */ 7476static void 7477dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 7478{ 7479 pkp->dtpk_prov = pdp->dtpd_provider; 7480 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 7481 7482 pkp->dtpk_mod = pdp->dtpd_mod; 7483 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 7484 7485 pkp->dtpk_func = pdp->dtpd_func; 7486 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 7487 7488 pkp->dtpk_name = pdp->dtpd_name; 7489 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 7490 7491 pkp->dtpk_id = pdp->dtpd_id; 7492 7493 if (pkp->dtpk_id == DTRACE_IDNONE && 7494 pkp->dtpk_pmatch == &dtrace_match_nul && 7495 pkp->dtpk_mmatch == &dtrace_match_nul && 7496 pkp->dtpk_fmatch == &dtrace_match_nul && 7497 pkp->dtpk_nmatch == &dtrace_match_nul) 7498 pkp->dtpk_fmatch = &dtrace_match_nonzero; 7499} 7500 7501/* 7502 * DTrace Provider-to-Framework API Functions 7503 * 7504 * These functions implement much of the Provider-to-Framework API, as 7505 * described in <sys/dtrace.h>. The parts of the API not in this section are 7506 * the functions in the API for probe management (found below), and 7507 * dtrace_probe() itself (found above). 7508 */ 7509 7510/* 7511 * Register the calling provider with the DTrace framework. This should 7512 * generally be called by DTrace providers in their attach(9E) entry point. 7513 */ 7514int 7515dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 7516 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 7517{ 7518 dtrace_provider_t *provider; 7519 7520 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 7521 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7522 "arguments", name ? name : "<NULL>"); 7523 return (EINVAL); 7524 } 7525 7526 if (name[0] == '\0' || dtrace_badname(name)) { 7527 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7528 "provider name", name); 7529 return (EINVAL); 7530 } 7531 7532 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 7533 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 7534 pops->dtps_destroy == NULL || 7535 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 7536 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7537 "provider ops", name); 7538 return (EINVAL); 7539 } 7540 7541 if (dtrace_badattr(&pap->dtpa_provider) || 7542 dtrace_badattr(&pap->dtpa_mod) || 7543 dtrace_badattr(&pap->dtpa_func) || 7544 dtrace_badattr(&pap->dtpa_name) || 7545 dtrace_badattr(&pap->dtpa_args)) { 7546 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7547 "provider attributes", name); 7548 return (EINVAL); 7549 } 7550 7551 if (priv & ~DTRACE_PRIV_ALL) { 7552 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7553 "privilege attributes", name); 7554 return (EINVAL); 7555 } 7556 7557 if ((priv & DTRACE_PRIV_KERNEL) && 7558 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 7559 pops->dtps_usermode == NULL) { 7560 cmn_err(CE_WARN, "failed to register provider '%s': need " 7561 "dtps_usermode() op for given privilege attributes", name); 7562 return (EINVAL); 7563 } 7564 7565 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 7566 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7567 (void) strcpy(provider->dtpv_name, name); 7568 7569 provider->dtpv_attr = *pap; 7570 provider->dtpv_priv.dtpp_flags = priv; 7571 if (cr != NULL) { 7572 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 7573 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 7574 } 7575 provider->dtpv_pops = *pops; 7576 7577 if (pops->dtps_provide == NULL) { 7578 ASSERT(pops->dtps_provide_module != NULL); 7579 provider->dtpv_pops.dtps_provide = 7580 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 7581 } 7582 7583 if (pops->dtps_provide_module == NULL) { 7584 ASSERT(pops->dtps_provide != NULL); 7585 provider->dtpv_pops.dtps_provide_module = 7586 (void (*)(void *, modctl_t *))dtrace_nullop; 7587 } 7588 7589 if (pops->dtps_suspend == NULL) { 7590 ASSERT(pops->dtps_resume == NULL); 7591 provider->dtpv_pops.dtps_suspend = 7592 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7593 provider->dtpv_pops.dtps_resume = 7594 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7595 } 7596 7597 provider->dtpv_arg = arg; 7598 *idp = (dtrace_provider_id_t)provider; 7599 7600 if (pops == &dtrace_provider_ops) { 7601 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7602 ASSERT(MUTEX_HELD(&dtrace_lock)); 7603 ASSERT(dtrace_anon.dta_enabling == NULL); 7604 7605 /* 7606 * We make sure that the DTrace provider is at the head of 7607 * the provider chain. 7608 */ 7609 provider->dtpv_next = dtrace_provider; 7610 dtrace_provider = provider; 7611 return (0); 7612 } 7613 7614 mutex_enter(&dtrace_provider_lock); 7615 mutex_enter(&dtrace_lock); 7616 7617 /* 7618 * If there is at least one provider registered, we'll add this 7619 * provider after the first provider. 7620 */ 7621 if (dtrace_provider != NULL) { 7622 provider->dtpv_next = dtrace_provider->dtpv_next; 7623 dtrace_provider->dtpv_next = provider; 7624 } else { 7625 dtrace_provider = provider; 7626 } 7627 7628 if (dtrace_retained != NULL) { 7629 dtrace_enabling_provide(provider); 7630 7631 /* 7632 * Now we need to call dtrace_enabling_matchall() -- which 7633 * will acquire cpu_lock and dtrace_lock. We therefore need 7634 * to drop all of our locks before calling into it... 7635 */ 7636 mutex_exit(&dtrace_lock); 7637 mutex_exit(&dtrace_provider_lock); 7638 dtrace_enabling_matchall(); 7639 7640 return (0); 7641 } 7642 7643 mutex_exit(&dtrace_lock); 7644 mutex_exit(&dtrace_provider_lock); 7645 7646 return (0); 7647} 7648 7649/* 7650 * Unregister the specified provider from the DTrace framework. This should 7651 * generally be called by DTrace providers in their detach(9E) entry point. 7652 */ 7653int 7654dtrace_unregister(dtrace_provider_id_t id) 7655{ 7656 dtrace_provider_t *old = (dtrace_provider_t *)id; 7657 dtrace_provider_t *prev = NULL; 7658 int i, self = 0, noreap = 0; 7659 dtrace_probe_t *probe, *first = NULL; 7660 7661 if (old->dtpv_pops.dtps_enable == 7662 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 7663 /* 7664 * If DTrace itself is the provider, we're called with locks 7665 * already held. 7666 */ 7667 ASSERT(old == dtrace_provider); 7668#if defined(sun) 7669 ASSERT(dtrace_devi != NULL); 7670#endif 7671 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7672 ASSERT(MUTEX_HELD(&dtrace_lock)); 7673 self = 1; 7674 7675 if (dtrace_provider->dtpv_next != NULL) { 7676 /* 7677 * There's another provider here; return failure. 7678 */ 7679 return (EBUSY); 7680 } 7681 } else { 7682 mutex_enter(&dtrace_provider_lock); 7683#if defined(sun) 7684 mutex_enter(&mod_lock); 7685#endif 7686 mutex_enter(&dtrace_lock); 7687 } 7688 7689 /* 7690 * If anyone has /dev/dtrace open, or if there are anonymous enabled 7691 * probes, we refuse to let providers slither away, unless this 7692 * provider has already been explicitly invalidated. 7693 */ 7694 if (!old->dtpv_defunct && 7695 (dtrace_opens || (dtrace_anon.dta_state != NULL && 7696 dtrace_anon.dta_state->dts_necbs > 0))) { 7697 if (!self) { 7698 mutex_exit(&dtrace_lock); 7699#if defined(sun) 7700 mutex_exit(&mod_lock); 7701#endif 7702 mutex_exit(&dtrace_provider_lock); 7703 } 7704 return (EBUSY); 7705 } 7706 7707 /* 7708 * Attempt to destroy the probes associated with this provider. 7709 */ 7710 for (i = 0; i < dtrace_nprobes; i++) { 7711 if ((probe = dtrace_probes[i]) == NULL) 7712 continue; 7713 7714 if (probe->dtpr_provider != old) 7715 continue; 7716 7717 if (probe->dtpr_ecb == NULL) 7718 continue; 7719 7720 /* 7721 * If we are trying to unregister a defunct provider, and the 7722 * provider was made defunct within the interval dictated by 7723 * dtrace_unregister_defunct_reap, we'll (asynchronously) 7724 * attempt to reap our enablings. To denote that the provider 7725 * should reattempt to unregister itself at some point in the 7726 * future, we will return a differentiable error code (EAGAIN 7727 * instead of EBUSY) in this case. 7728 */ 7729 if (dtrace_gethrtime() - old->dtpv_defunct > 7730 dtrace_unregister_defunct_reap) 7731 noreap = 1; 7732 7733 if (!self) { 7734 mutex_exit(&dtrace_lock); 7735#if defined(sun) 7736 mutex_exit(&mod_lock); 7737#endif 7738 mutex_exit(&dtrace_provider_lock); 7739 } 7740 7741 if (noreap) 7742 return (EBUSY); 7743 7744 (void) taskq_dispatch(dtrace_taskq, 7745 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 7746 7747 return (EAGAIN); 7748 } 7749 7750 /* 7751 * All of the probes for this provider are disabled; we can safely 7752 * remove all of them from their hash chains and from the probe array. 7753 */ 7754 for (i = 0; i < dtrace_nprobes; i++) { 7755 if ((probe = dtrace_probes[i]) == NULL) 7756 continue; 7757 7758 if (probe->dtpr_provider != old) 7759 continue; 7760 7761 dtrace_probes[i] = NULL; 7762 7763 dtrace_hash_remove(dtrace_bymod, probe); 7764 dtrace_hash_remove(dtrace_byfunc, probe); 7765 dtrace_hash_remove(dtrace_byname, probe); 7766 7767 if (first == NULL) { 7768 first = probe; 7769 probe->dtpr_nextmod = NULL; 7770 } else { 7771 probe->dtpr_nextmod = first; 7772 first = probe; 7773 } 7774 } 7775 7776 /* 7777 * The provider's probes have been removed from the hash chains and 7778 * from the probe array. Now issue a dtrace_sync() to be sure that 7779 * everyone has cleared out from any probe array processing. 7780 */ 7781 dtrace_sync(); 7782 7783 for (probe = first; probe != NULL; probe = first) { 7784 first = probe->dtpr_nextmod; 7785 7786 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 7787 probe->dtpr_arg); 7788 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7789 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7790 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7791#if defined(sun) 7792 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 7793#else 7794 free_unr(dtrace_arena, probe->dtpr_id); 7795#endif 7796 kmem_free(probe, sizeof (dtrace_probe_t)); 7797 } 7798 7799 if ((prev = dtrace_provider) == old) { 7800#if defined(sun) 7801 ASSERT(self || dtrace_devi == NULL); 7802 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 7803#endif 7804 dtrace_provider = old->dtpv_next; 7805 } else { 7806 while (prev != NULL && prev->dtpv_next != old) 7807 prev = prev->dtpv_next; 7808 7809 if (prev == NULL) { 7810 panic("attempt to unregister non-existent " 7811 "dtrace provider %p\n", (void *)id); 7812 } 7813 7814 prev->dtpv_next = old->dtpv_next; 7815 } 7816 7817 if (!self) { 7818 mutex_exit(&dtrace_lock); 7819#if defined(sun) 7820 mutex_exit(&mod_lock); 7821#endif 7822 mutex_exit(&dtrace_provider_lock); 7823 } 7824 7825 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 7826 kmem_free(old, sizeof (dtrace_provider_t)); 7827 7828 return (0); 7829} 7830 7831/* 7832 * Invalidate the specified provider. All subsequent probe lookups for the 7833 * specified provider will fail, but its probes will not be removed. 7834 */ 7835void 7836dtrace_invalidate(dtrace_provider_id_t id) 7837{ 7838 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 7839 7840 ASSERT(pvp->dtpv_pops.dtps_enable != 7841 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 7842 7843 mutex_enter(&dtrace_provider_lock); 7844 mutex_enter(&dtrace_lock); 7845 7846 pvp->dtpv_defunct = dtrace_gethrtime(); 7847 7848 mutex_exit(&dtrace_lock); 7849 mutex_exit(&dtrace_provider_lock); 7850} 7851 7852/* 7853 * Indicate whether or not DTrace has attached. 7854 */ 7855int 7856dtrace_attached(void) 7857{ 7858 /* 7859 * dtrace_provider will be non-NULL iff the DTrace driver has 7860 * attached. (It's non-NULL because DTrace is always itself a 7861 * provider.) 7862 */ 7863 return (dtrace_provider != NULL); 7864} 7865 7866/* 7867 * Remove all the unenabled probes for the given provider. This function is 7868 * not unlike dtrace_unregister(), except that it doesn't remove the provider 7869 * -- just as many of its associated probes as it can. 7870 */ 7871int 7872dtrace_condense(dtrace_provider_id_t id) 7873{ 7874 dtrace_provider_t *prov = (dtrace_provider_t *)id; 7875 int i; 7876 dtrace_probe_t *probe; 7877 7878 /* 7879 * Make sure this isn't the dtrace provider itself. 7880 */ 7881 ASSERT(prov->dtpv_pops.dtps_enable != 7882 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 7883 7884 mutex_enter(&dtrace_provider_lock); 7885 mutex_enter(&dtrace_lock); 7886 7887 /* 7888 * Attempt to destroy the probes associated with this provider. 7889 */ 7890 for (i = 0; i < dtrace_nprobes; i++) { 7891 if ((probe = dtrace_probes[i]) == NULL) 7892 continue; 7893 7894 if (probe->dtpr_provider != prov) 7895 continue; 7896 7897 if (probe->dtpr_ecb != NULL) 7898 continue; 7899 7900 dtrace_probes[i] = NULL; 7901 7902 dtrace_hash_remove(dtrace_bymod, probe); 7903 dtrace_hash_remove(dtrace_byfunc, probe); 7904 dtrace_hash_remove(dtrace_byname, probe); 7905 7906 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 7907 probe->dtpr_arg); 7908 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7909 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7910 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7911 kmem_free(probe, sizeof (dtrace_probe_t)); 7912#if defined(sun) 7913 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 7914#else 7915 free_unr(dtrace_arena, i + 1); 7916#endif 7917 } 7918 7919 mutex_exit(&dtrace_lock); 7920 mutex_exit(&dtrace_provider_lock); 7921 7922 return (0); 7923} 7924 7925/* 7926 * DTrace Probe Management Functions 7927 * 7928 * The functions in this section perform the DTrace probe management, 7929 * including functions to create probes, look-up probes, and call into the 7930 * providers to request that probes be provided. Some of these functions are 7931 * in the Provider-to-Framework API; these functions can be identified by the 7932 * fact that they are not declared "static". 7933 */ 7934 7935/* 7936 * Create a probe with the specified module name, function name, and name. 7937 */ 7938dtrace_id_t 7939dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 7940 const char *func, const char *name, int aframes, void *arg) 7941{ 7942 dtrace_probe_t *probe, **probes; 7943 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 7944 dtrace_id_t id; 7945 7946 if (provider == dtrace_provider) { 7947 ASSERT(MUTEX_HELD(&dtrace_lock)); 7948 } else { 7949 mutex_enter(&dtrace_lock); 7950 } 7951 7952#if defined(sun) 7953 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 7954 VM_BESTFIT | VM_SLEEP); 7955#else 7956 id = alloc_unr(dtrace_arena); 7957#endif 7958 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 7959 7960 probe->dtpr_id = id; 7961 probe->dtpr_gen = dtrace_probegen++; 7962 probe->dtpr_mod = dtrace_strdup(mod); 7963 probe->dtpr_func = dtrace_strdup(func); 7964 probe->dtpr_name = dtrace_strdup(name); 7965 probe->dtpr_arg = arg; 7966 probe->dtpr_aframes = aframes; 7967 probe->dtpr_provider = provider; 7968 7969 dtrace_hash_add(dtrace_bymod, probe); 7970 dtrace_hash_add(dtrace_byfunc, probe); 7971 dtrace_hash_add(dtrace_byname, probe); 7972 7973 if (id - 1 >= dtrace_nprobes) { 7974 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 7975 size_t nsize = osize << 1; 7976 7977 if (nsize == 0) { 7978 ASSERT(osize == 0); 7979 ASSERT(dtrace_probes == NULL); 7980 nsize = sizeof (dtrace_probe_t *); 7981 } 7982 7983 probes = kmem_zalloc(nsize, KM_SLEEP); 7984 7985 if (dtrace_probes == NULL) { 7986 ASSERT(osize == 0); 7987 dtrace_probes = probes; 7988 dtrace_nprobes = 1; 7989 } else { 7990 dtrace_probe_t **oprobes = dtrace_probes; 7991 7992 bcopy(oprobes, probes, osize); 7993 dtrace_membar_producer(); 7994 dtrace_probes = probes; 7995 7996 dtrace_sync(); 7997 7998 /* 7999 * All CPUs are now seeing the new probes array; we can 8000 * safely free the old array. 8001 */ 8002 kmem_free(oprobes, osize); 8003 dtrace_nprobes <<= 1; 8004 } 8005 8006 ASSERT(id - 1 < dtrace_nprobes); 8007 } 8008 8009 ASSERT(dtrace_probes[id - 1] == NULL); 8010 dtrace_probes[id - 1] = probe; 8011 8012 if (provider != dtrace_provider) 8013 mutex_exit(&dtrace_lock); 8014 8015 return (id); 8016} 8017 8018static dtrace_probe_t * 8019dtrace_probe_lookup_id(dtrace_id_t id) 8020{ 8021 ASSERT(MUTEX_HELD(&dtrace_lock)); 8022 8023 if (id == 0 || id > dtrace_nprobes) 8024 return (NULL); 8025 8026 return (dtrace_probes[id - 1]); 8027} 8028 8029static int 8030dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 8031{ 8032 *((dtrace_id_t *)arg) = probe->dtpr_id; 8033 8034 return (DTRACE_MATCH_DONE); 8035} 8036 8037/* 8038 * Look up a probe based on provider and one or more of module name, function 8039 * name and probe name. 8040 */ 8041dtrace_id_t 8042dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 8043 char *func, char *name) 8044{ 8045 dtrace_probekey_t pkey; 8046 dtrace_id_t id; 8047 int match; 8048 8049 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 8050 pkey.dtpk_pmatch = &dtrace_match_string; 8051 pkey.dtpk_mod = mod; 8052 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 8053 pkey.dtpk_func = func; 8054 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 8055 pkey.dtpk_name = name; 8056 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 8057 pkey.dtpk_id = DTRACE_IDNONE; 8058 8059 mutex_enter(&dtrace_lock); 8060 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 8061 dtrace_probe_lookup_match, &id); 8062 mutex_exit(&dtrace_lock); 8063 8064 ASSERT(match == 1 || match == 0); 8065 return (match ? id : 0); 8066} 8067 8068/* 8069 * Returns the probe argument associated with the specified probe. 8070 */ 8071void * 8072dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 8073{ 8074 dtrace_probe_t *probe; 8075 void *rval = NULL; 8076 8077 mutex_enter(&dtrace_lock); 8078 8079 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 8080 probe->dtpr_provider == (dtrace_provider_t *)id) 8081 rval = probe->dtpr_arg; 8082 8083 mutex_exit(&dtrace_lock); 8084 8085 return (rval); 8086} 8087 8088/* 8089 * Copy a probe into a probe description. 8090 */ 8091static void 8092dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 8093{ 8094 bzero(pdp, sizeof (dtrace_probedesc_t)); 8095 pdp->dtpd_id = prp->dtpr_id; 8096 8097 (void) strncpy(pdp->dtpd_provider, 8098 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 8099 8100 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 8101 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 8102 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 8103} 8104 8105/* 8106 * Called to indicate that a probe -- or probes -- should be provided by a 8107 * specfied provider. If the specified description is NULL, the provider will 8108 * be told to provide all of its probes. (This is done whenever a new 8109 * consumer comes along, or whenever a retained enabling is to be matched.) If 8110 * the specified description is non-NULL, the provider is given the 8111 * opportunity to dynamically provide the specified probe, allowing providers 8112 * to support the creation of probes on-the-fly. (So-called _autocreated_ 8113 * probes.) If the provider is NULL, the operations will be applied to all 8114 * providers; if the provider is non-NULL the operations will only be applied 8115 * to the specified provider. The dtrace_provider_lock must be held, and the 8116 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 8117 * will need to grab the dtrace_lock when it reenters the framework through 8118 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 8119 */ 8120static void 8121dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 8122{ 8123#if defined(sun) 8124 modctl_t *ctl; 8125#endif 8126 int all = 0; 8127 8128 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8129 8130 if (prv == NULL) { 8131 all = 1; 8132 prv = dtrace_provider; 8133 } 8134 8135 do { 8136 /* 8137 * First, call the blanket provide operation. 8138 */ 8139 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 8140 8141#if defined(sun) 8142 /* 8143 * Now call the per-module provide operation. We will grab 8144 * mod_lock to prevent the list from being modified. Note 8145 * that this also prevents the mod_busy bits from changing. 8146 * (mod_busy can only be changed with mod_lock held.) 8147 */ 8148 mutex_enter(&mod_lock); 8149 8150 ctl = &modules; 8151 do { 8152 if (ctl->mod_busy || ctl->mod_mp == NULL) 8153 continue; 8154 8155 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 8156 8157 } while ((ctl = ctl->mod_next) != &modules); 8158 8159 mutex_exit(&mod_lock); 8160#endif 8161 } while (all && (prv = prv->dtpv_next) != NULL); 8162} 8163 8164#if defined(sun) 8165/* 8166 * Iterate over each probe, and call the Framework-to-Provider API function 8167 * denoted by offs. 8168 */ 8169static void 8170dtrace_probe_foreach(uintptr_t offs) 8171{ 8172 dtrace_provider_t *prov; 8173 void (*func)(void *, dtrace_id_t, void *); 8174 dtrace_probe_t *probe; 8175 dtrace_icookie_t cookie; 8176 int i; 8177 8178 /* 8179 * We disable interrupts to walk through the probe array. This is 8180 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 8181 * won't see stale data. 8182 */ 8183 cookie = dtrace_interrupt_disable(); 8184 8185 for (i = 0; i < dtrace_nprobes; i++) { 8186 if ((probe = dtrace_probes[i]) == NULL) 8187 continue; 8188 8189 if (probe->dtpr_ecb == NULL) { 8190 /* 8191 * This probe isn't enabled -- don't call the function. 8192 */ 8193 continue; 8194 } 8195 8196 prov = probe->dtpr_provider; 8197 func = *((void(**)(void *, dtrace_id_t, void *)) 8198 ((uintptr_t)&prov->dtpv_pops + offs)); 8199 8200 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 8201 } 8202 8203 dtrace_interrupt_enable(cookie); 8204} 8205#endif 8206 8207static int 8208dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 8209{ 8210 dtrace_probekey_t pkey; 8211 uint32_t priv; 8212 uid_t uid; 8213 zoneid_t zoneid; 8214 8215 ASSERT(MUTEX_HELD(&dtrace_lock)); 8216 dtrace_ecb_create_cache = NULL; 8217 8218 if (desc == NULL) { 8219 /* 8220 * If we're passed a NULL description, we're being asked to 8221 * create an ECB with a NULL probe. 8222 */ 8223 (void) dtrace_ecb_create_enable(NULL, enab); 8224 return (0); 8225 } 8226 8227 dtrace_probekey(desc, &pkey); 8228 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 8229 &priv, &uid, &zoneid); 8230 8231 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 8232 enab)); 8233} 8234 8235/* 8236 * DTrace Helper Provider Functions 8237 */ 8238static void 8239dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 8240{ 8241 attr->dtat_name = DOF_ATTR_NAME(dofattr); 8242 attr->dtat_data = DOF_ATTR_DATA(dofattr); 8243 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 8244} 8245 8246static void 8247dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 8248 const dof_provider_t *dofprov, char *strtab) 8249{ 8250 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 8251 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 8252 dofprov->dofpv_provattr); 8253 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 8254 dofprov->dofpv_modattr); 8255 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 8256 dofprov->dofpv_funcattr); 8257 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 8258 dofprov->dofpv_nameattr); 8259 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 8260 dofprov->dofpv_argsattr); 8261} 8262 8263static void 8264dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8265{ 8266 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8267 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8268 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 8269 dof_provider_t *provider; 8270 dof_probe_t *probe; 8271 uint32_t *off, *enoff; 8272 uint8_t *arg; 8273 char *strtab; 8274 uint_t i, nprobes; 8275 dtrace_helper_provdesc_t dhpv; 8276 dtrace_helper_probedesc_t dhpb; 8277 dtrace_meta_t *meta = dtrace_meta_pid; 8278 dtrace_mops_t *mops = &meta->dtm_mops; 8279 void *parg; 8280 8281 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8282 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8283 provider->dofpv_strtab * dof->dofh_secsize); 8284 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8285 provider->dofpv_probes * dof->dofh_secsize); 8286 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8287 provider->dofpv_prargs * dof->dofh_secsize); 8288 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8289 provider->dofpv_proffs * dof->dofh_secsize); 8290 8291 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8292 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 8293 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 8294 enoff = NULL; 8295 8296 /* 8297 * See dtrace_helper_provider_validate(). 8298 */ 8299 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 8300 provider->dofpv_prenoffs != DOF_SECT_NONE) { 8301 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8302 provider->dofpv_prenoffs * dof->dofh_secsize); 8303 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 8304 } 8305 8306 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 8307 8308 /* 8309 * Create the provider. 8310 */ 8311 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8312 8313 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 8314 return; 8315 8316 meta->dtm_count++; 8317 8318 /* 8319 * Create the probes. 8320 */ 8321 for (i = 0; i < nprobes; i++) { 8322 probe = (dof_probe_t *)(uintptr_t)(daddr + 8323 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 8324 8325 dhpb.dthpb_mod = dhp->dofhp_mod; 8326 dhpb.dthpb_func = strtab + probe->dofpr_func; 8327 dhpb.dthpb_name = strtab + probe->dofpr_name; 8328 dhpb.dthpb_base = probe->dofpr_addr; 8329 dhpb.dthpb_offs = off + probe->dofpr_offidx; 8330 dhpb.dthpb_noffs = probe->dofpr_noffs; 8331 if (enoff != NULL) { 8332 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 8333 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 8334 } else { 8335 dhpb.dthpb_enoffs = NULL; 8336 dhpb.dthpb_nenoffs = 0; 8337 } 8338 dhpb.dthpb_args = arg + probe->dofpr_argidx; 8339 dhpb.dthpb_nargc = probe->dofpr_nargc; 8340 dhpb.dthpb_xargc = probe->dofpr_xargc; 8341 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 8342 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 8343 8344 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 8345 } 8346} 8347 8348static void 8349dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 8350{ 8351 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8352 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8353 int i; 8354 8355 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8356 8357 for (i = 0; i < dof->dofh_secnum; i++) { 8358 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8359 dof->dofh_secoff + i * dof->dofh_secsize); 8360 8361 if (sec->dofs_type != DOF_SECT_PROVIDER) 8362 continue; 8363 8364 dtrace_helper_provide_one(dhp, sec, pid); 8365 } 8366 8367 /* 8368 * We may have just created probes, so we must now rematch against 8369 * any retained enablings. Note that this call will acquire both 8370 * cpu_lock and dtrace_lock; the fact that we are holding 8371 * dtrace_meta_lock now is what defines the ordering with respect to 8372 * these three locks. 8373 */ 8374 dtrace_enabling_matchall(); 8375} 8376 8377static void 8378dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8379{ 8380 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8381 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8382 dof_sec_t *str_sec; 8383 dof_provider_t *provider; 8384 char *strtab; 8385 dtrace_helper_provdesc_t dhpv; 8386 dtrace_meta_t *meta = dtrace_meta_pid; 8387 dtrace_mops_t *mops = &meta->dtm_mops; 8388 8389 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8390 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8391 provider->dofpv_strtab * dof->dofh_secsize); 8392 8393 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8394 8395 /* 8396 * Create the provider. 8397 */ 8398 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8399 8400 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 8401 8402 meta->dtm_count--; 8403} 8404 8405static void 8406dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 8407{ 8408 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8409 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8410 int i; 8411 8412 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8413 8414 for (i = 0; i < dof->dofh_secnum; i++) { 8415 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8416 dof->dofh_secoff + i * dof->dofh_secsize); 8417 8418 if (sec->dofs_type != DOF_SECT_PROVIDER) 8419 continue; 8420 8421 dtrace_helper_provider_remove_one(dhp, sec, pid); 8422 } 8423} 8424 8425/* 8426 * DTrace Meta Provider-to-Framework API Functions 8427 * 8428 * These functions implement the Meta Provider-to-Framework API, as described 8429 * in <sys/dtrace.h>. 8430 */ 8431int 8432dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 8433 dtrace_meta_provider_id_t *idp) 8434{ 8435 dtrace_meta_t *meta; 8436 dtrace_helpers_t *help, *next; 8437 int i; 8438 8439 *idp = DTRACE_METAPROVNONE; 8440 8441 /* 8442 * We strictly don't need the name, but we hold onto it for 8443 * debuggability. All hail error queues! 8444 */ 8445 if (name == NULL) { 8446 cmn_err(CE_WARN, "failed to register meta-provider: " 8447 "invalid name"); 8448 return (EINVAL); 8449 } 8450 8451 if (mops == NULL || 8452 mops->dtms_create_probe == NULL || 8453 mops->dtms_provide_pid == NULL || 8454 mops->dtms_remove_pid == NULL) { 8455 cmn_err(CE_WARN, "failed to register meta-register %s: " 8456 "invalid ops", name); 8457 return (EINVAL); 8458 } 8459 8460 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 8461 meta->dtm_mops = *mops; 8462 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8463 (void) strcpy(meta->dtm_name, name); 8464 meta->dtm_arg = arg; 8465 8466 mutex_enter(&dtrace_meta_lock); 8467 mutex_enter(&dtrace_lock); 8468 8469 if (dtrace_meta_pid != NULL) { 8470 mutex_exit(&dtrace_lock); 8471 mutex_exit(&dtrace_meta_lock); 8472 cmn_err(CE_WARN, "failed to register meta-register %s: " 8473 "user-land meta-provider exists", name); 8474 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 8475 kmem_free(meta, sizeof (dtrace_meta_t)); 8476 return (EINVAL); 8477 } 8478 8479 dtrace_meta_pid = meta; 8480 *idp = (dtrace_meta_provider_id_t)meta; 8481 8482 /* 8483 * If there are providers and probes ready to go, pass them 8484 * off to the new meta provider now. 8485 */ 8486 8487 help = dtrace_deferred_pid; 8488 dtrace_deferred_pid = NULL; 8489 8490 mutex_exit(&dtrace_lock); 8491 8492 while (help != NULL) { 8493 for (i = 0; i < help->dthps_nprovs; i++) { 8494 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 8495 help->dthps_pid); 8496 } 8497 8498 next = help->dthps_next; 8499 help->dthps_next = NULL; 8500 help->dthps_prev = NULL; 8501 help->dthps_deferred = 0; 8502 help = next; 8503 } 8504 8505 mutex_exit(&dtrace_meta_lock); 8506 8507 return (0); 8508} 8509 8510int 8511dtrace_meta_unregister(dtrace_meta_provider_id_t id) 8512{ 8513 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 8514 8515 mutex_enter(&dtrace_meta_lock); 8516 mutex_enter(&dtrace_lock); 8517 8518 if (old == dtrace_meta_pid) { 8519 pp = &dtrace_meta_pid; 8520 } else { 8521 panic("attempt to unregister non-existent " 8522 "dtrace meta-provider %p\n", (void *)old); 8523 } 8524 8525 if (old->dtm_count != 0) { 8526 mutex_exit(&dtrace_lock); 8527 mutex_exit(&dtrace_meta_lock); 8528 return (EBUSY); 8529 } 8530 8531 *pp = NULL; 8532 8533 mutex_exit(&dtrace_lock); 8534 mutex_exit(&dtrace_meta_lock); 8535 8536 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 8537 kmem_free(old, sizeof (dtrace_meta_t)); 8538 8539 return (0); 8540} 8541 8542 8543/* 8544 * DTrace DIF Object Functions 8545 */ 8546static int 8547dtrace_difo_err(uint_t pc, const char *format, ...) 8548{ 8549 if (dtrace_err_verbose) { 8550 va_list alist; 8551 8552 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 8553 va_start(alist, format); 8554 (void) vuprintf(format, alist); 8555 va_end(alist); 8556 } 8557 8558#ifdef DTRACE_ERRDEBUG 8559 dtrace_errdebug(format); 8560#endif 8561 return (1); 8562} 8563 8564/* 8565 * Validate a DTrace DIF object by checking the IR instructions. The following 8566 * rules are currently enforced by dtrace_difo_validate(): 8567 * 8568 * 1. Each instruction must have a valid opcode 8569 * 2. Each register, string, variable, or subroutine reference must be valid 8570 * 3. No instruction can modify register %r0 (must be zero) 8571 * 4. All instruction reserved bits must be set to zero 8572 * 5. The last instruction must be a "ret" instruction 8573 * 6. All branch targets must reference a valid instruction _after_ the branch 8574 */ 8575static int 8576dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 8577 cred_t *cr) 8578{ 8579 int err = 0, i; 8580 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8581 int kcheckload; 8582 uint_t pc; 8583 8584 kcheckload = cr == NULL || 8585 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 8586 8587 dp->dtdo_destructive = 0; 8588 8589 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 8590 dif_instr_t instr = dp->dtdo_buf[pc]; 8591 8592 uint_t r1 = DIF_INSTR_R1(instr); 8593 uint_t r2 = DIF_INSTR_R2(instr); 8594 uint_t rd = DIF_INSTR_RD(instr); 8595 uint_t rs = DIF_INSTR_RS(instr); 8596 uint_t label = DIF_INSTR_LABEL(instr); 8597 uint_t v = DIF_INSTR_VAR(instr); 8598 uint_t subr = DIF_INSTR_SUBR(instr); 8599 uint_t type = DIF_INSTR_TYPE(instr); 8600 uint_t op = DIF_INSTR_OP(instr); 8601 8602 switch (op) { 8603 case DIF_OP_OR: 8604 case DIF_OP_XOR: 8605 case DIF_OP_AND: 8606 case DIF_OP_SLL: 8607 case DIF_OP_SRL: 8608 case DIF_OP_SRA: 8609 case DIF_OP_SUB: 8610 case DIF_OP_ADD: 8611 case DIF_OP_MUL: 8612 case DIF_OP_SDIV: 8613 case DIF_OP_UDIV: 8614 case DIF_OP_SREM: 8615 case DIF_OP_UREM: 8616 case DIF_OP_COPYS: 8617 if (r1 >= nregs) 8618 err += efunc(pc, "invalid register %u\n", r1); 8619 if (r2 >= nregs) 8620 err += efunc(pc, "invalid register %u\n", r2); 8621 if (rd >= nregs) 8622 err += efunc(pc, "invalid register %u\n", rd); 8623 if (rd == 0) 8624 err += efunc(pc, "cannot write to %r0\n"); 8625 break; 8626 case DIF_OP_NOT: 8627 case DIF_OP_MOV: 8628 case DIF_OP_ALLOCS: 8629 if (r1 >= nregs) 8630 err += efunc(pc, "invalid register %u\n", r1); 8631 if (r2 != 0) 8632 err += efunc(pc, "non-zero reserved bits\n"); 8633 if (rd >= nregs) 8634 err += efunc(pc, "invalid register %u\n", rd); 8635 if (rd == 0) 8636 err += efunc(pc, "cannot write to %r0\n"); 8637 break; 8638 case DIF_OP_LDSB: 8639 case DIF_OP_LDSH: 8640 case DIF_OP_LDSW: 8641 case DIF_OP_LDUB: 8642 case DIF_OP_LDUH: 8643 case DIF_OP_LDUW: 8644 case DIF_OP_LDX: 8645 if (r1 >= nregs) 8646 err += efunc(pc, "invalid register %u\n", r1); 8647 if (r2 != 0) 8648 err += efunc(pc, "non-zero reserved bits\n"); 8649 if (rd >= nregs) 8650 err += efunc(pc, "invalid register %u\n", rd); 8651 if (rd == 0) 8652 err += efunc(pc, "cannot write to %r0\n"); 8653 if (kcheckload) 8654 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 8655 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 8656 break; 8657 case DIF_OP_RLDSB: 8658 case DIF_OP_RLDSH: 8659 case DIF_OP_RLDSW: 8660 case DIF_OP_RLDUB: 8661 case DIF_OP_RLDUH: 8662 case DIF_OP_RLDUW: 8663 case DIF_OP_RLDX: 8664 if (r1 >= nregs) 8665 err += efunc(pc, "invalid register %u\n", r1); 8666 if (r2 != 0) 8667 err += efunc(pc, "non-zero reserved bits\n"); 8668 if (rd >= nregs) 8669 err += efunc(pc, "invalid register %u\n", rd); 8670 if (rd == 0) 8671 err += efunc(pc, "cannot write to %r0\n"); 8672 break; 8673 case DIF_OP_ULDSB: 8674 case DIF_OP_ULDSH: 8675 case DIF_OP_ULDSW: 8676 case DIF_OP_ULDUB: 8677 case DIF_OP_ULDUH: 8678 case DIF_OP_ULDUW: 8679 case DIF_OP_ULDX: 8680 if (r1 >= nregs) 8681 err += efunc(pc, "invalid register %u\n", r1); 8682 if (r2 != 0) 8683 err += efunc(pc, "non-zero reserved bits\n"); 8684 if (rd >= nregs) 8685 err += efunc(pc, "invalid register %u\n", rd); 8686 if (rd == 0) 8687 err += efunc(pc, "cannot write to %r0\n"); 8688 break; 8689 case DIF_OP_STB: 8690 case DIF_OP_STH: 8691 case DIF_OP_STW: 8692 case DIF_OP_STX: 8693 if (r1 >= nregs) 8694 err += efunc(pc, "invalid register %u\n", r1); 8695 if (r2 != 0) 8696 err += efunc(pc, "non-zero reserved bits\n"); 8697 if (rd >= nregs) 8698 err += efunc(pc, "invalid register %u\n", rd); 8699 if (rd == 0) 8700 err += efunc(pc, "cannot write to 0 address\n"); 8701 break; 8702 case DIF_OP_CMP: 8703 case DIF_OP_SCMP: 8704 if (r1 >= nregs) 8705 err += efunc(pc, "invalid register %u\n", r1); 8706 if (r2 >= nregs) 8707 err += efunc(pc, "invalid register %u\n", r2); 8708 if (rd != 0) 8709 err += efunc(pc, "non-zero reserved bits\n"); 8710 break; 8711 case DIF_OP_TST: 8712 if (r1 >= nregs) 8713 err += efunc(pc, "invalid register %u\n", r1); 8714 if (r2 != 0 || rd != 0) 8715 err += efunc(pc, "non-zero reserved bits\n"); 8716 break; 8717 case DIF_OP_BA: 8718 case DIF_OP_BE: 8719 case DIF_OP_BNE: 8720 case DIF_OP_BG: 8721 case DIF_OP_BGU: 8722 case DIF_OP_BGE: 8723 case DIF_OP_BGEU: 8724 case DIF_OP_BL: 8725 case DIF_OP_BLU: 8726 case DIF_OP_BLE: 8727 case DIF_OP_BLEU: 8728 if (label >= dp->dtdo_len) { 8729 err += efunc(pc, "invalid branch target %u\n", 8730 label); 8731 } 8732 if (label <= pc) { 8733 err += efunc(pc, "backward branch to %u\n", 8734 label); 8735 } 8736 break; 8737 case DIF_OP_RET: 8738 if (r1 != 0 || r2 != 0) 8739 err += efunc(pc, "non-zero reserved bits\n"); 8740 if (rd >= nregs) 8741 err += efunc(pc, "invalid register %u\n", rd); 8742 break; 8743 case DIF_OP_NOP: 8744 case DIF_OP_POPTS: 8745 case DIF_OP_FLUSHTS: 8746 if (r1 != 0 || r2 != 0 || rd != 0) 8747 err += efunc(pc, "non-zero reserved bits\n"); 8748 break; 8749 case DIF_OP_SETX: 8750 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 8751 err += efunc(pc, "invalid integer ref %u\n", 8752 DIF_INSTR_INTEGER(instr)); 8753 } 8754 if (rd >= nregs) 8755 err += efunc(pc, "invalid register %u\n", rd); 8756 if (rd == 0) 8757 err += efunc(pc, "cannot write to %r0\n"); 8758 break; 8759 case DIF_OP_SETS: 8760 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 8761 err += efunc(pc, "invalid string ref %u\n", 8762 DIF_INSTR_STRING(instr)); 8763 } 8764 if (rd >= nregs) 8765 err += efunc(pc, "invalid register %u\n", rd); 8766 if (rd == 0) 8767 err += efunc(pc, "cannot write to %r0\n"); 8768 break; 8769 case DIF_OP_LDGA: 8770 case DIF_OP_LDTA: 8771 if (r1 > DIF_VAR_ARRAY_MAX) 8772 err += efunc(pc, "invalid array %u\n", r1); 8773 if (r2 >= nregs) 8774 err += efunc(pc, "invalid register %u\n", r2); 8775 if (rd >= nregs) 8776 err += efunc(pc, "invalid register %u\n", rd); 8777 if (rd == 0) 8778 err += efunc(pc, "cannot write to %r0\n"); 8779 break; 8780 case DIF_OP_LDGS: 8781 case DIF_OP_LDTS: 8782 case DIF_OP_LDLS: 8783 case DIF_OP_LDGAA: 8784 case DIF_OP_LDTAA: 8785 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 8786 err += efunc(pc, "invalid variable %u\n", v); 8787 if (rd >= nregs) 8788 err += efunc(pc, "invalid register %u\n", rd); 8789 if (rd == 0) 8790 err += efunc(pc, "cannot write to %r0\n"); 8791 break; 8792 case DIF_OP_STGS: 8793 case DIF_OP_STTS: 8794 case DIF_OP_STLS: 8795 case DIF_OP_STGAA: 8796 case DIF_OP_STTAA: 8797 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 8798 err += efunc(pc, "invalid variable %u\n", v); 8799 if (rs >= nregs) 8800 err += efunc(pc, "invalid register %u\n", rd); 8801 break; 8802 case DIF_OP_CALL: 8803 if (subr > DIF_SUBR_MAX) 8804 err += efunc(pc, "invalid subr %u\n", subr); 8805 if (rd >= nregs) 8806 err += efunc(pc, "invalid register %u\n", rd); 8807 if (rd == 0) 8808 err += efunc(pc, "cannot write to %r0\n"); 8809 8810 if (subr == DIF_SUBR_COPYOUT || 8811 subr == DIF_SUBR_COPYOUTSTR) { 8812 dp->dtdo_destructive = 1; 8813 } 8814 break; 8815 case DIF_OP_PUSHTR: 8816 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 8817 err += efunc(pc, "invalid ref type %u\n", type); 8818 if (r2 >= nregs) 8819 err += efunc(pc, "invalid register %u\n", r2); 8820 if (rs >= nregs) 8821 err += efunc(pc, "invalid register %u\n", rs); 8822 break; 8823 case DIF_OP_PUSHTV: 8824 if (type != DIF_TYPE_CTF) 8825 err += efunc(pc, "invalid val type %u\n", type); 8826 if (r2 >= nregs) 8827 err += efunc(pc, "invalid register %u\n", r2); 8828 if (rs >= nregs) 8829 err += efunc(pc, "invalid register %u\n", rs); 8830 break; 8831 default: 8832 err += efunc(pc, "invalid opcode %u\n", 8833 DIF_INSTR_OP(instr)); 8834 } 8835 } 8836 8837 if (dp->dtdo_len != 0 && 8838 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 8839 err += efunc(dp->dtdo_len - 1, 8840 "expected 'ret' as last DIF instruction\n"); 8841 } 8842 8843 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 8844 /* 8845 * If we're not returning by reference, the size must be either 8846 * 0 or the size of one of the base types. 8847 */ 8848 switch (dp->dtdo_rtype.dtdt_size) { 8849 case 0: 8850 case sizeof (uint8_t): 8851 case sizeof (uint16_t): 8852 case sizeof (uint32_t): 8853 case sizeof (uint64_t): 8854 break; 8855 8856 default: 8857 err += efunc(dp->dtdo_len - 1, "bad return size"); 8858 } 8859 } 8860 8861 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 8862 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 8863 dtrace_diftype_t *vt, *et; 8864 uint_t id, ndx; 8865 8866 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 8867 v->dtdv_scope != DIFV_SCOPE_THREAD && 8868 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 8869 err += efunc(i, "unrecognized variable scope %d\n", 8870 v->dtdv_scope); 8871 break; 8872 } 8873 8874 if (v->dtdv_kind != DIFV_KIND_ARRAY && 8875 v->dtdv_kind != DIFV_KIND_SCALAR) { 8876 err += efunc(i, "unrecognized variable type %d\n", 8877 v->dtdv_kind); 8878 break; 8879 } 8880 8881 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 8882 err += efunc(i, "%d exceeds variable id limit\n", id); 8883 break; 8884 } 8885 8886 if (id < DIF_VAR_OTHER_UBASE) 8887 continue; 8888 8889 /* 8890 * For user-defined variables, we need to check that this 8891 * definition is identical to any previous definition that we 8892 * encountered. 8893 */ 8894 ndx = id - DIF_VAR_OTHER_UBASE; 8895 8896 switch (v->dtdv_scope) { 8897 case DIFV_SCOPE_GLOBAL: 8898 if (ndx < vstate->dtvs_nglobals) { 8899 dtrace_statvar_t *svar; 8900 8901 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 8902 existing = &svar->dtsv_var; 8903 } 8904 8905 break; 8906 8907 case DIFV_SCOPE_THREAD: 8908 if (ndx < vstate->dtvs_ntlocals) 8909 existing = &vstate->dtvs_tlocals[ndx]; 8910 break; 8911 8912 case DIFV_SCOPE_LOCAL: 8913 if (ndx < vstate->dtvs_nlocals) { 8914 dtrace_statvar_t *svar; 8915 8916 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 8917 existing = &svar->dtsv_var; 8918 } 8919 8920 break; 8921 } 8922 8923 vt = &v->dtdv_type; 8924 8925 if (vt->dtdt_flags & DIF_TF_BYREF) { 8926 if (vt->dtdt_size == 0) { 8927 err += efunc(i, "zero-sized variable\n"); 8928 break; 8929 } 8930 8931 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 8932 vt->dtdt_size > dtrace_global_maxsize) { 8933 err += efunc(i, "oversized by-ref global\n"); 8934 break; 8935 } 8936 } 8937 8938 if (existing == NULL || existing->dtdv_id == 0) 8939 continue; 8940 8941 ASSERT(existing->dtdv_id == v->dtdv_id); 8942 ASSERT(existing->dtdv_scope == v->dtdv_scope); 8943 8944 if (existing->dtdv_kind != v->dtdv_kind) 8945 err += efunc(i, "%d changed variable kind\n", id); 8946 8947 et = &existing->dtdv_type; 8948 8949 if (vt->dtdt_flags != et->dtdt_flags) { 8950 err += efunc(i, "%d changed variable type flags\n", id); 8951 break; 8952 } 8953 8954 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 8955 err += efunc(i, "%d changed variable type size\n", id); 8956 break; 8957 } 8958 } 8959 8960 return (err); 8961} 8962 8963/* 8964 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 8965 * are much more constrained than normal DIFOs. Specifically, they may 8966 * not: 8967 * 8968 * 1. Make calls to subroutines other than copyin(), copyinstr() or 8969 * miscellaneous string routines 8970 * 2. Access DTrace variables other than the args[] array, and the 8971 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 8972 * 3. Have thread-local variables. 8973 * 4. Have dynamic variables. 8974 */ 8975static int 8976dtrace_difo_validate_helper(dtrace_difo_t *dp) 8977{ 8978 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8979 int err = 0; 8980 uint_t pc; 8981 8982 for (pc = 0; pc < dp->dtdo_len; pc++) { 8983 dif_instr_t instr = dp->dtdo_buf[pc]; 8984 8985 uint_t v = DIF_INSTR_VAR(instr); 8986 uint_t subr = DIF_INSTR_SUBR(instr); 8987 uint_t op = DIF_INSTR_OP(instr); 8988 8989 switch (op) { 8990 case DIF_OP_OR: 8991 case DIF_OP_XOR: 8992 case DIF_OP_AND: 8993 case DIF_OP_SLL: 8994 case DIF_OP_SRL: 8995 case DIF_OP_SRA: 8996 case DIF_OP_SUB: 8997 case DIF_OP_ADD: 8998 case DIF_OP_MUL: 8999 case DIF_OP_SDIV: 9000 case DIF_OP_UDIV: 9001 case DIF_OP_SREM: 9002 case DIF_OP_UREM: 9003 case DIF_OP_COPYS: 9004 case DIF_OP_NOT: 9005 case DIF_OP_MOV: 9006 case DIF_OP_RLDSB: 9007 case DIF_OP_RLDSH: 9008 case DIF_OP_RLDSW: 9009 case DIF_OP_RLDUB: 9010 case DIF_OP_RLDUH: 9011 case DIF_OP_RLDUW: 9012 case DIF_OP_RLDX: 9013 case DIF_OP_ULDSB: 9014 case DIF_OP_ULDSH: 9015 case DIF_OP_ULDSW: 9016 case DIF_OP_ULDUB: 9017 case DIF_OP_ULDUH: 9018 case DIF_OP_ULDUW: 9019 case DIF_OP_ULDX: 9020 case DIF_OP_STB: 9021 case DIF_OP_STH: 9022 case DIF_OP_STW: 9023 case DIF_OP_STX: 9024 case DIF_OP_ALLOCS: 9025 case DIF_OP_CMP: 9026 case DIF_OP_SCMP: 9027 case DIF_OP_TST: 9028 case DIF_OP_BA: 9029 case DIF_OP_BE: 9030 case DIF_OP_BNE: 9031 case DIF_OP_BG: 9032 case DIF_OP_BGU: 9033 case DIF_OP_BGE: 9034 case DIF_OP_BGEU: 9035 case DIF_OP_BL: 9036 case DIF_OP_BLU: 9037 case DIF_OP_BLE: 9038 case DIF_OP_BLEU: 9039 case DIF_OP_RET: 9040 case DIF_OP_NOP: 9041 case DIF_OP_POPTS: 9042 case DIF_OP_FLUSHTS: 9043 case DIF_OP_SETX: 9044 case DIF_OP_SETS: 9045 case DIF_OP_LDGA: 9046 case DIF_OP_LDLS: 9047 case DIF_OP_STGS: 9048 case DIF_OP_STLS: 9049 case DIF_OP_PUSHTR: 9050 case DIF_OP_PUSHTV: 9051 break; 9052 9053 case DIF_OP_LDGS: 9054 if (v >= DIF_VAR_OTHER_UBASE) 9055 break; 9056 9057 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 9058 break; 9059 9060 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 9061 v == DIF_VAR_PPID || v == DIF_VAR_TID || 9062 v == DIF_VAR_EXECARGS || 9063 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 9064 v == DIF_VAR_UID || v == DIF_VAR_GID) 9065 break; 9066 9067 err += efunc(pc, "illegal variable %u\n", v); 9068 break; 9069 9070 case DIF_OP_LDTA: 9071 case DIF_OP_LDTS: 9072 case DIF_OP_LDGAA: 9073 case DIF_OP_LDTAA: 9074 err += efunc(pc, "illegal dynamic variable load\n"); 9075 break; 9076 9077 case DIF_OP_STTS: 9078 case DIF_OP_STGAA: 9079 case DIF_OP_STTAA: 9080 err += efunc(pc, "illegal dynamic variable store\n"); 9081 break; 9082 9083 case DIF_OP_CALL: 9084 if (subr == DIF_SUBR_ALLOCA || 9085 subr == DIF_SUBR_BCOPY || 9086 subr == DIF_SUBR_COPYIN || 9087 subr == DIF_SUBR_COPYINTO || 9088 subr == DIF_SUBR_COPYINSTR || 9089 subr == DIF_SUBR_INDEX || 9090 subr == DIF_SUBR_INET_NTOA || 9091 subr == DIF_SUBR_INET_NTOA6 || 9092 subr == DIF_SUBR_INET_NTOP || 9093 subr == DIF_SUBR_LLTOSTR || 9094 subr == DIF_SUBR_RINDEX || 9095 subr == DIF_SUBR_STRCHR || 9096 subr == DIF_SUBR_STRJOIN || 9097 subr == DIF_SUBR_STRRCHR || 9098 subr == DIF_SUBR_STRSTR || 9099 subr == DIF_SUBR_HTONS || 9100 subr == DIF_SUBR_HTONL || 9101 subr == DIF_SUBR_HTONLL || 9102 subr == DIF_SUBR_NTOHS || 9103 subr == DIF_SUBR_NTOHL || 9104 subr == DIF_SUBR_NTOHLL || 9105 subr == DIF_SUBR_MEMREF || 9106 subr == DIF_SUBR_TYPEREF) 9107 break; 9108 9109 err += efunc(pc, "invalid subr %u\n", subr); 9110 break; 9111 9112 default: 9113 err += efunc(pc, "invalid opcode %u\n", 9114 DIF_INSTR_OP(instr)); 9115 } 9116 } 9117 9118 return (err); 9119} 9120 9121/* 9122 * Returns 1 if the expression in the DIF object can be cached on a per-thread 9123 * basis; 0 if not. 9124 */ 9125static int 9126dtrace_difo_cacheable(dtrace_difo_t *dp) 9127{ 9128 int i; 9129 9130 if (dp == NULL) 9131 return (0); 9132 9133 for (i = 0; i < dp->dtdo_varlen; i++) { 9134 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9135 9136 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 9137 continue; 9138 9139 switch (v->dtdv_id) { 9140 case DIF_VAR_CURTHREAD: 9141 case DIF_VAR_PID: 9142 case DIF_VAR_TID: 9143 case DIF_VAR_EXECARGS: 9144 case DIF_VAR_EXECNAME: 9145 case DIF_VAR_ZONENAME: 9146 break; 9147 9148 default: 9149 return (0); 9150 } 9151 } 9152 9153 /* 9154 * This DIF object may be cacheable. Now we need to look for any 9155 * array loading instructions, any memory loading instructions, or 9156 * any stores to thread-local variables. 9157 */ 9158 for (i = 0; i < dp->dtdo_len; i++) { 9159 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 9160 9161 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 9162 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 9163 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 9164 op == DIF_OP_LDGA || op == DIF_OP_STTS) 9165 return (0); 9166 } 9167 9168 return (1); 9169} 9170 9171static void 9172dtrace_difo_hold(dtrace_difo_t *dp) 9173{ 9174 int i; 9175 9176 ASSERT(MUTEX_HELD(&dtrace_lock)); 9177 9178 dp->dtdo_refcnt++; 9179 ASSERT(dp->dtdo_refcnt != 0); 9180 9181 /* 9182 * We need to check this DIF object for references to the variable 9183 * DIF_VAR_VTIMESTAMP. 9184 */ 9185 for (i = 0; i < dp->dtdo_varlen; i++) { 9186 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9187 9188 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9189 continue; 9190 9191 if (dtrace_vtime_references++ == 0) 9192 dtrace_vtime_enable(); 9193 } 9194} 9195 9196/* 9197 * This routine calculates the dynamic variable chunksize for a given DIF 9198 * object. The calculation is not fool-proof, and can probably be tricked by 9199 * malicious DIF -- but it works for all compiler-generated DIF. Because this 9200 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 9201 * if a dynamic variable size exceeds the chunksize. 9202 */ 9203static void 9204dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9205{ 9206 uint64_t sval = 0; 9207 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 9208 const dif_instr_t *text = dp->dtdo_buf; 9209 uint_t pc, srd = 0; 9210 uint_t ttop = 0; 9211 size_t size, ksize; 9212 uint_t id, i; 9213 9214 for (pc = 0; pc < dp->dtdo_len; pc++) { 9215 dif_instr_t instr = text[pc]; 9216 uint_t op = DIF_INSTR_OP(instr); 9217 uint_t rd = DIF_INSTR_RD(instr); 9218 uint_t r1 = DIF_INSTR_R1(instr); 9219 uint_t nkeys = 0; 9220 uchar_t scope = 0; 9221 9222 dtrace_key_t *key = tupregs; 9223 9224 switch (op) { 9225 case DIF_OP_SETX: 9226 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 9227 srd = rd; 9228 continue; 9229 9230 case DIF_OP_STTS: 9231 key = &tupregs[DIF_DTR_NREGS]; 9232 key[0].dttk_size = 0; 9233 key[1].dttk_size = 0; 9234 nkeys = 2; 9235 scope = DIFV_SCOPE_THREAD; 9236 break; 9237 9238 case DIF_OP_STGAA: 9239 case DIF_OP_STTAA: 9240 nkeys = ttop; 9241 9242 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 9243 key[nkeys++].dttk_size = 0; 9244 9245 key[nkeys++].dttk_size = 0; 9246 9247 if (op == DIF_OP_STTAA) { 9248 scope = DIFV_SCOPE_THREAD; 9249 } else { 9250 scope = DIFV_SCOPE_GLOBAL; 9251 } 9252 9253 break; 9254 9255 case DIF_OP_PUSHTR: 9256 if (ttop == DIF_DTR_NREGS) 9257 return; 9258 9259 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 9260 /* 9261 * If the register for the size of the "pushtr" 9262 * is %r0 (or the value is 0) and the type is 9263 * a string, we'll use the system-wide default 9264 * string size. 9265 */ 9266 tupregs[ttop++].dttk_size = 9267 dtrace_strsize_default; 9268 } else { 9269 if (srd == 0) 9270 return; 9271 9272 tupregs[ttop++].dttk_size = sval; 9273 } 9274 9275 break; 9276 9277 case DIF_OP_PUSHTV: 9278 if (ttop == DIF_DTR_NREGS) 9279 return; 9280 9281 tupregs[ttop++].dttk_size = 0; 9282 break; 9283 9284 case DIF_OP_FLUSHTS: 9285 ttop = 0; 9286 break; 9287 9288 case DIF_OP_POPTS: 9289 if (ttop != 0) 9290 ttop--; 9291 break; 9292 } 9293 9294 sval = 0; 9295 srd = 0; 9296 9297 if (nkeys == 0) 9298 continue; 9299 9300 /* 9301 * We have a dynamic variable allocation; calculate its size. 9302 */ 9303 for (ksize = 0, i = 0; i < nkeys; i++) 9304 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 9305 9306 size = sizeof (dtrace_dynvar_t); 9307 size += sizeof (dtrace_key_t) * (nkeys - 1); 9308 size += ksize; 9309 9310 /* 9311 * Now we need to determine the size of the stored data. 9312 */ 9313 id = DIF_INSTR_VAR(instr); 9314 9315 for (i = 0; i < dp->dtdo_varlen; i++) { 9316 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9317 9318 if (v->dtdv_id == id && v->dtdv_scope == scope) { 9319 size += v->dtdv_type.dtdt_size; 9320 break; 9321 } 9322 } 9323 9324 if (i == dp->dtdo_varlen) 9325 return; 9326 9327 /* 9328 * We have the size. If this is larger than the chunk size 9329 * for our dynamic variable state, reset the chunk size. 9330 */ 9331 size = P2ROUNDUP(size, sizeof (uint64_t)); 9332 9333 if (size > vstate->dtvs_dynvars.dtds_chunksize) 9334 vstate->dtvs_dynvars.dtds_chunksize = size; 9335 } 9336} 9337 9338static void 9339dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9340{ 9341 int i, oldsvars, osz, nsz, otlocals, ntlocals; 9342 uint_t id; 9343 9344 ASSERT(MUTEX_HELD(&dtrace_lock)); 9345 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 9346 9347 for (i = 0; i < dp->dtdo_varlen; i++) { 9348 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9349 dtrace_statvar_t *svar, ***svarp = NULL; 9350 size_t dsize = 0; 9351 uint8_t scope = v->dtdv_scope; 9352 int *np = NULL; 9353 9354 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9355 continue; 9356 9357 id -= DIF_VAR_OTHER_UBASE; 9358 9359 switch (scope) { 9360 case DIFV_SCOPE_THREAD: 9361 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 9362 dtrace_difv_t *tlocals; 9363 9364 if ((ntlocals = (otlocals << 1)) == 0) 9365 ntlocals = 1; 9366 9367 osz = otlocals * sizeof (dtrace_difv_t); 9368 nsz = ntlocals * sizeof (dtrace_difv_t); 9369 9370 tlocals = kmem_zalloc(nsz, KM_SLEEP); 9371 9372 if (osz != 0) { 9373 bcopy(vstate->dtvs_tlocals, 9374 tlocals, osz); 9375 kmem_free(vstate->dtvs_tlocals, osz); 9376 } 9377 9378 vstate->dtvs_tlocals = tlocals; 9379 vstate->dtvs_ntlocals = ntlocals; 9380 } 9381 9382 vstate->dtvs_tlocals[id] = *v; 9383 continue; 9384 9385 case DIFV_SCOPE_LOCAL: 9386 np = &vstate->dtvs_nlocals; 9387 svarp = &vstate->dtvs_locals; 9388 9389 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9390 dsize = NCPU * (v->dtdv_type.dtdt_size + 9391 sizeof (uint64_t)); 9392 else 9393 dsize = NCPU * sizeof (uint64_t); 9394 9395 break; 9396 9397 case DIFV_SCOPE_GLOBAL: 9398 np = &vstate->dtvs_nglobals; 9399 svarp = &vstate->dtvs_globals; 9400 9401 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9402 dsize = v->dtdv_type.dtdt_size + 9403 sizeof (uint64_t); 9404 9405 break; 9406 9407 default: 9408 ASSERT(0); 9409 } 9410 9411 while (id >= (oldsvars = *np)) { 9412 dtrace_statvar_t **statics; 9413 int newsvars, oldsize, newsize; 9414 9415 if ((newsvars = (oldsvars << 1)) == 0) 9416 newsvars = 1; 9417 9418 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 9419 newsize = newsvars * sizeof (dtrace_statvar_t *); 9420 9421 statics = kmem_zalloc(newsize, KM_SLEEP); 9422 9423 if (oldsize != 0) { 9424 bcopy(*svarp, statics, oldsize); 9425 kmem_free(*svarp, oldsize); 9426 } 9427 9428 *svarp = statics; 9429 *np = newsvars; 9430 } 9431 9432 if ((svar = (*svarp)[id]) == NULL) { 9433 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 9434 svar->dtsv_var = *v; 9435 9436 if ((svar->dtsv_size = dsize) != 0) { 9437 svar->dtsv_data = (uint64_t)(uintptr_t) 9438 kmem_zalloc(dsize, KM_SLEEP); 9439 } 9440 9441 (*svarp)[id] = svar; 9442 } 9443 9444 svar->dtsv_refcnt++; 9445 } 9446 9447 dtrace_difo_chunksize(dp, vstate); 9448 dtrace_difo_hold(dp); 9449} 9450 9451static dtrace_difo_t * 9452dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9453{ 9454 dtrace_difo_t *new; 9455 size_t sz; 9456 9457 ASSERT(dp->dtdo_buf != NULL); 9458 ASSERT(dp->dtdo_refcnt != 0); 9459 9460 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 9461 9462 ASSERT(dp->dtdo_buf != NULL); 9463 sz = dp->dtdo_len * sizeof (dif_instr_t); 9464 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 9465 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 9466 new->dtdo_len = dp->dtdo_len; 9467 9468 if (dp->dtdo_strtab != NULL) { 9469 ASSERT(dp->dtdo_strlen != 0); 9470 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 9471 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 9472 new->dtdo_strlen = dp->dtdo_strlen; 9473 } 9474 9475 if (dp->dtdo_inttab != NULL) { 9476 ASSERT(dp->dtdo_intlen != 0); 9477 sz = dp->dtdo_intlen * sizeof (uint64_t); 9478 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 9479 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 9480 new->dtdo_intlen = dp->dtdo_intlen; 9481 } 9482 9483 if (dp->dtdo_vartab != NULL) { 9484 ASSERT(dp->dtdo_varlen != 0); 9485 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 9486 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 9487 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 9488 new->dtdo_varlen = dp->dtdo_varlen; 9489 } 9490 9491 dtrace_difo_init(new, vstate); 9492 return (new); 9493} 9494 9495static void 9496dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9497{ 9498 int i; 9499 9500 ASSERT(dp->dtdo_refcnt == 0); 9501 9502 for (i = 0; i < dp->dtdo_varlen; i++) { 9503 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9504 dtrace_statvar_t *svar, **svarp = NULL; 9505 uint_t id; 9506 uint8_t scope = v->dtdv_scope; 9507 int *np = NULL; 9508 9509 switch (scope) { 9510 case DIFV_SCOPE_THREAD: 9511 continue; 9512 9513 case DIFV_SCOPE_LOCAL: 9514 np = &vstate->dtvs_nlocals; 9515 svarp = vstate->dtvs_locals; 9516 break; 9517 9518 case DIFV_SCOPE_GLOBAL: 9519 np = &vstate->dtvs_nglobals; 9520 svarp = vstate->dtvs_globals; 9521 break; 9522 9523 default: 9524 ASSERT(0); 9525 } 9526 9527 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9528 continue; 9529 9530 id -= DIF_VAR_OTHER_UBASE; 9531 ASSERT(id < *np); 9532 9533 svar = svarp[id]; 9534 ASSERT(svar != NULL); 9535 ASSERT(svar->dtsv_refcnt > 0); 9536 9537 if (--svar->dtsv_refcnt > 0) 9538 continue; 9539 9540 if (svar->dtsv_size != 0) { 9541 ASSERT(svar->dtsv_data != 0); 9542 kmem_free((void *)(uintptr_t)svar->dtsv_data, 9543 svar->dtsv_size); 9544 } 9545 9546 kmem_free(svar, sizeof (dtrace_statvar_t)); 9547 svarp[id] = NULL; 9548 } 9549 9550 if (dp->dtdo_buf != NULL) 9551 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 9552 if (dp->dtdo_inttab != NULL) 9553 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 9554 if (dp->dtdo_strtab != NULL) 9555 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 9556 if (dp->dtdo_vartab != NULL) 9557 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 9558 9559 kmem_free(dp, sizeof (dtrace_difo_t)); 9560} 9561 9562static void 9563dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9564{ 9565 int i; 9566 9567 ASSERT(MUTEX_HELD(&dtrace_lock)); 9568 ASSERT(dp->dtdo_refcnt != 0); 9569 9570 for (i = 0; i < dp->dtdo_varlen; i++) { 9571 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9572 9573 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9574 continue; 9575 9576 ASSERT(dtrace_vtime_references > 0); 9577 if (--dtrace_vtime_references == 0) 9578 dtrace_vtime_disable(); 9579 } 9580 9581 if (--dp->dtdo_refcnt == 0) 9582 dtrace_difo_destroy(dp, vstate); 9583} 9584 9585/* 9586 * DTrace Format Functions 9587 */ 9588static uint16_t 9589dtrace_format_add(dtrace_state_t *state, char *str) 9590{ 9591 char *fmt, **new; 9592 uint16_t ndx, len = strlen(str) + 1; 9593 9594 fmt = kmem_zalloc(len, KM_SLEEP); 9595 bcopy(str, fmt, len); 9596 9597 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 9598 if (state->dts_formats[ndx] == NULL) { 9599 state->dts_formats[ndx] = fmt; 9600 return (ndx + 1); 9601 } 9602 } 9603 9604 if (state->dts_nformats == USHRT_MAX) { 9605 /* 9606 * This is only likely if a denial-of-service attack is being 9607 * attempted. As such, it's okay to fail silently here. 9608 */ 9609 kmem_free(fmt, len); 9610 return (0); 9611 } 9612 9613 /* 9614 * For simplicity, we always resize the formats array to be exactly the 9615 * number of formats. 9616 */ 9617 ndx = state->dts_nformats++; 9618 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 9619 9620 if (state->dts_formats != NULL) { 9621 ASSERT(ndx != 0); 9622 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 9623 kmem_free(state->dts_formats, ndx * sizeof (char *)); 9624 } 9625 9626 state->dts_formats = new; 9627 state->dts_formats[ndx] = fmt; 9628 9629 return (ndx + 1); 9630} 9631 9632static void 9633dtrace_format_remove(dtrace_state_t *state, uint16_t format) 9634{ 9635 char *fmt; 9636 9637 ASSERT(state->dts_formats != NULL); 9638 ASSERT(format <= state->dts_nformats); 9639 ASSERT(state->dts_formats[format - 1] != NULL); 9640 9641 fmt = state->dts_formats[format - 1]; 9642 kmem_free(fmt, strlen(fmt) + 1); 9643 state->dts_formats[format - 1] = NULL; 9644} 9645 9646static void 9647dtrace_format_destroy(dtrace_state_t *state) 9648{ 9649 int i; 9650 9651 if (state->dts_nformats == 0) { 9652 ASSERT(state->dts_formats == NULL); 9653 return; 9654 } 9655 9656 ASSERT(state->dts_formats != NULL); 9657 9658 for (i = 0; i < state->dts_nformats; i++) { 9659 char *fmt = state->dts_formats[i]; 9660 9661 if (fmt == NULL) 9662 continue; 9663 9664 kmem_free(fmt, strlen(fmt) + 1); 9665 } 9666 9667 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 9668 state->dts_nformats = 0; 9669 state->dts_formats = NULL; 9670} 9671 9672/* 9673 * DTrace Predicate Functions 9674 */ 9675static dtrace_predicate_t * 9676dtrace_predicate_create(dtrace_difo_t *dp) 9677{ 9678 dtrace_predicate_t *pred; 9679 9680 ASSERT(MUTEX_HELD(&dtrace_lock)); 9681 ASSERT(dp->dtdo_refcnt != 0); 9682 9683 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 9684 pred->dtp_difo = dp; 9685 pred->dtp_refcnt = 1; 9686 9687 if (!dtrace_difo_cacheable(dp)) 9688 return (pred); 9689 9690 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 9691 /* 9692 * This is only theoretically possible -- we have had 2^32 9693 * cacheable predicates on this machine. We cannot allow any 9694 * more predicates to become cacheable: as unlikely as it is, 9695 * there may be a thread caching a (now stale) predicate cache 9696 * ID. (N.B.: the temptation is being successfully resisted to 9697 * have this cmn_err() "Holy shit -- we executed this code!") 9698 */ 9699 return (pred); 9700 } 9701 9702 pred->dtp_cacheid = dtrace_predcache_id++; 9703 9704 return (pred); 9705} 9706 9707static void 9708dtrace_predicate_hold(dtrace_predicate_t *pred) 9709{ 9710 ASSERT(MUTEX_HELD(&dtrace_lock)); 9711 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 9712 ASSERT(pred->dtp_refcnt > 0); 9713 9714 pred->dtp_refcnt++; 9715} 9716 9717static void 9718dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 9719{ 9720 dtrace_difo_t *dp = pred->dtp_difo; 9721 9722 ASSERT(MUTEX_HELD(&dtrace_lock)); 9723 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 9724 ASSERT(pred->dtp_refcnt > 0); 9725 9726 if (--pred->dtp_refcnt == 0) { 9727 dtrace_difo_release(pred->dtp_difo, vstate); 9728 kmem_free(pred, sizeof (dtrace_predicate_t)); 9729 } 9730} 9731 9732/* 9733 * DTrace Action Description Functions 9734 */ 9735static dtrace_actdesc_t * 9736dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 9737 uint64_t uarg, uint64_t arg) 9738{ 9739 dtrace_actdesc_t *act; 9740 9741#if defined(sun) 9742 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 9743 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 9744#endif 9745 9746 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 9747 act->dtad_kind = kind; 9748 act->dtad_ntuple = ntuple; 9749 act->dtad_uarg = uarg; 9750 act->dtad_arg = arg; 9751 act->dtad_refcnt = 1; 9752 9753 return (act); 9754} 9755 9756static void 9757dtrace_actdesc_hold(dtrace_actdesc_t *act) 9758{ 9759 ASSERT(act->dtad_refcnt >= 1); 9760 act->dtad_refcnt++; 9761} 9762 9763static void 9764dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 9765{ 9766 dtrace_actkind_t kind = act->dtad_kind; 9767 dtrace_difo_t *dp; 9768 9769 ASSERT(act->dtad_refcnt >= 1); 9770 9771 if (--act->dtad_refcnt != 0) 9772 return; 9773 9774 if ((dp = act->dtad_difo) != NULL) 9775 dtrace_difo_release(dp, vstate); 9776 9777 if (DTRACEACT_ISPRINTFLIKE(kind)) { 9778 char *str = (char *)(uintptr_t)act->dtad_arg; 9779 9780#if defined(sun) 9781 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 9782 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 9783#endif 9784 9785 if (str != NULL) 9786 kmem_free(str, strlen(str) + 1); 9787 } 9788 9789 kmem_free(act, sizeof (dtrace_actdesc_t)); 9790} 9791 9792/* 9793 * DTrace ECB Functions 9794 */ 9795static dtrace_ecb_t * 9796dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 9797{ 9798 dtrace_ecb_t *ecb; 9799 dtrace_epid_t epid; 9800 9801 ASSERT(MUTEX_HELD(&dtrace_lock)); 9802 9803 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 9804 ecb->dte_predicate = NULL; 9805 ecb->dte_probe = probe; 9806 9807 /* 9808 * The default size is the size of the default action: recording 9809 * the header. 9810 */ 9811 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 9812 ecb->dte_alignment = sizeof (dtrace_epid_t); 9813 9814 epid = state->dts_epid++; 9815 9816 if (epid - 1 >= state->dts_necbs) { 9817 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 9818 int necbs = state->dts_necbs << 1; 9819 9820 ASSERT(epid == state->dts_necbs + 1); 9821 9822 if (necbs == 0) { 9823 ASSERT(oecbs == NULL); 9824 necbs = 1; 9825 } 9826 9827 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 9828 9829 if (oecbs != NULL) 9830 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 9831 9832 dtrace_membar_producer(); 9833 state->dts_ecbs = ecbs; 9834 9835 if (oecbs != NULL) { 9836 /* 9837 * If this state is active, we must dtrace_sync() 9838 * before we can free the old dts_ecbs array: we're 9839 * coming in hot, and there may be active ring 9840 * buffer processing (which indexes into the dts_ecbs 9841 * array) on another CPU. 9842 */ 9843 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 9844 dtrace_sync(); 9845 9846 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 9847 } 9848 9849 dtrace_membar_producer(); 9850 state->dts_necbs = necbs; 9851 } 9852 9853 ecb->dte_state = state; 9854 9855 ASSERT(state->dts_ecbs[epid - 1] == NULL); 9856 dtrace_membar_producer(); 9857 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 9858 9859 return (ecb); 9860} 9861 9862static void 9863dtrace_ecb_enable(dtrace_ecb_t *ecb) 9864{ 9865 dtrace_probe_t *probe = ecb->dte_probe; 9866 9867 ASSERT(MUTEX_HELD(&cpu_lock)); 9868 ASSERT(MUTEX_HELD(&dtrace_lock)); 9869 ASSERT(ecb->dte_next == NULL); 9870 9871 if (probe == NULL) { 9872 /* 9873 * This is the NULL probe -- there's nothing to do. 9874 */ 9875 return; 9876 } 9877 9878 if (probe->dtpr_ecb == NULL) { 9879 dtrace_provider_t *prov = probe->dtpr_provider; 9880 9881 /* 9882 * We're the first ECB on this probe. 9883 */ 9884 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 9885 9886 if (ecb->dte_predicate != NULL) 9887 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 9888 9889 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 9890 probe->dtpr_id, probe->dtpr_arg); 9891 } else { 9892 /* 9893 * This probe is already active. Swing the last pointer to 9894 * point to the new ECB, and issue a dtrace_sync() to assure 9895 * that all CPUs have seen the change. 9896 */ 9897 ASSERT(probe->dtpr_ecb_last != NULL); 9898 probe->dtpr_ecb_last->dte_next = ecb; 9899 probe->dtpr_ecb_last = ecb; 9900 probe->dtpr_predcache = 0; 9901 9902 dtrace_sync(); 9903 } 9904} 9905 9906static void 9907dtrace_ecb_resize(dtrace_ecb_t *ecb) 9908{ 9909 dtrace_action_t *act; 9910 uint32_t curneeded = UINT32_MAX; 9911 uint32_t aggbase = UINT32_MAX; 9912 9913 /* 9914 * If we record anything, we always record the dtrace_rechdr_t. (And 9915 * we always record it first.) 9916 */ 9917 ecb->dte_size = sizeof (dtrace_rechdr_t); 9918 ecb->dte_alignment = sizeof (dtrace_epid_t); 9919 9920 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9921 dtrace_recdesc_t *rec = &act->dta_rec; 9922 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 9923 9924 ecb->dte_alignment = MAX(ecb->dte_alignment, 9925 rec->dtrd_alignment); 9926 9927 if (DTRACEACT_ISAGG(act->dta_kind)) { 9928 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9929 9930 ASSERT(rec->dtrd_size != 0); 9931 ASSERT(agg->dtag_first != NULL); 9932 ASSERT(act->dta_prev->dta_intuple); 9933 ASSERT(aggbase != UINT32_MAX); 9934 ASSERT(curneeded != UINT32_MAX); 9935 9936 agg->dtag_base = aggbase; 9937 9938 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 9939 rec->dtrd_offset = curneeded; 9940 curneeded += rec->dtrd_size; 9941 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 9942 9943 aggbase = UINT32_MAX; 9944 curneeded = UINT32_MAX; 9945 } else if (act->dta_intuple) { 9946 if (curneeded == UINT32_MAX) { 9947 /* 9948 * This is the first record in a tuple. Align 9949 * curneeded to be at offset 4 in an 8-byte 9950 * aligned block. 9951 */ 9952 ASSERT(act->dta_prev == NULL || 9953 !act->dta_prev->dta_intuple); 9954 ASSERT3U(aggbase, ==, UINT32_MAX); 9955 curneeded = P2PHASEUP(ecb->dte_size, 9956 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 9957 9958 aggbase = curneeded - sizeof (dtrace_aggid_t); 9959 ASSERT(IS_P2ALIGNED(aggbase, 9960 sizeof (uint64_t))); 9961 } 9962 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 9963 rec->dtrd_offset = curneeded; 9964 curneeded += rec->dtrd_size; 9965 } else { 9966 /* tuples must be followed by an aggregation */ 9967 ASSERT(act->dta_prev == NULL || 9968 !act->dta_prev->dta_intuple); 9969 9970 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 9971 rec->dtrd_alignment); 9972 rec->dtrd_offset = ecb->dte_size; 9973 ecb->dte_size += rec->dtrd_size; 9974 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 9975 } 9976 } 9977 9978 if ((act = ecb->dte_action) != NULL && 9979 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 9980 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 9981 /* 9982 * If the size is still sizeof (dtrace_rechdr_t), then all 9983 * actions store no data; set the size to 0. 9984 */ 9985 ecb->dte_size = 0; 9986 } 9987 9988 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 9989 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 9990 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 9991 ecb->dte_needed); 9992} 9993 9994static dtrace_action_t * 9995dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9996{ 9997 dtrace_aggregation_t *agg; 9998 size_t size = sizeof (uint64_t); 9999 int ntuple = desc->dtad_ntuple; 10000 dtrace_action_t *act; 10001 dtrace_recdesc_t *frec; 10002 dtrace_aggid_t aggid; 10003 dtrace_state_t *state = ecb->dte_state; 10004 10005 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 10006 agg->dtag_ecb = ecb; 10007 10008 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 10009 10010 switch (desc->dtad_kind) { 10011 case DTRACEAGG_MIN: 10012 agg->dtag_initial = INT64_MAX; 10013 agg->dtag_aggregate = dtrace_aggregate_min; 10014 break; 10015 10016 case DTRACEAGG_MAX: 10017 agg->dtag_initial = INT64_MIN; 10018 agg->dtag_aggregate = dtrace_aggregate_max; 10019 break; 10020 10021 case DTRACEAGG_COUNT: 10022 agg->dtag_aggregate = dtrace_aggregate_count; 10023 break; 10024 10025 case DTRACEAGG_QUANTIZE: 10026 agg->dtag_aggregate = dtrace_aggregate_quantize; 10027 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 10028 sizeof (uint64_t); 10029 break; 10030 10031 case DTRACEAGG_LQUANTIZE: { 10032 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 10033 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 10034 10035 agg->dtag_initial = desc->dtad_arg; 10036 agg->dtag_aggregate = dtrace_aggregate_lquantize; 10037 10038 if (step == 0 || levels == 0) 10039 goto err; 10040 10041 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 10042 break; 10043 } 10044 10045 case DTRACEAGG_LLQUANTIZE: { 10046 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 10047 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 10048 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 10049 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 10050 int64_t v; 10051 10052 agg->dtag_initial = desc->dtad_arg; 10053 agg->dtag_aggregate = dtrace_aggregate_llquantize; 10054 10055 if (factor < 2 || low >= high || nsteps < factor) 10056 goto err; 10057 10058 /* 10059 * Now check that the number of steps evenly divides a power 10060 * of the factor. (This assures both integer bucket size and 10061 * linearity within each magnitude.) 10062 */ 10063 for (v = factor; v < nsteps; v *= factor) 10064 continue; 10065 10066 if ((v % nsteps) || (nsteps % factor)) 10067 goto err; 10068 10069 size = (dtrace_aggregate_llquantize_bucket(factor, 10070 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 10071 break; 10072 } 10073 10074 case DTRACEAGG_AVG: 10075 agg->dtag_aggregate = dtrace_aggregate_avg; 10076 size = sizeof (uint64_t) * 2; 10077 break; 10078 10079 case DTRACEAGG_STDDEV: 10080 agg->dtag_aggregate = dtrace_aggregate_stddev; 10081 size = sizeof (uint64_t) * 4; 10082 break; 10083 10084 case DTRACEAGG_SUM: 10085 agg->dtag_aggregate = dtrace_aggregate_sum; 10086 break; 10087 10088 default: 10089 goto err; 10090 } 10091 10092 agg->dtag_action.dta_rec.dtrd_size = size; 10093 10094 if (ntuple == 0) 10095 goto err; 10096 10097 /* 10098 * We must make sure that we have enough actions for the n-tuple. 10099 */ 10100 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 10101 if (DTRACEACT_ISAGG(act->dta_kind)) 10102 break; 10103 10104 if (--ntuple == 0) { 10105 /* 10106 * This is the action with which our n-tuple begins. 10107 */ 10108 agg->dtag_first = act; 10109 goto success; 10110 } 10111 } 10112 10113 /* 10114 * This n-tuple is short by ntuple elements. Return failure. 10115 */ 10116 ASSERT(ntuple != 0); 10117err: 10118 kmem_free(agg, sizeof (dtrace_aggregation_t)); 10119 return (NULL); 10120 10121success: 10122 /* 10123 * If the last action in the tuple has a size of zero, it's actually 10124 * an expression argument for the aggregating action. 10125 */ 10126 ASSERT(ecb->dte_action_last != NULL); 10127 act = ecb->dte_action_last; 10128 10129 if (act->dta_kind == DTRACEACT_DIFEXPR) { 10130 ASSERT(act->dta_difo != NULL); 10131 10132 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 10133 agg->dtag_hasarg = 1; 10134 } 10135 10136 /* 10137 * We need to allocate an id for this aggregation. 10138 */ 10139#if defined(sun) 10140 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 10141 VM_BESTFIT | VM_SLEEP); 10142#else 10143 aggid = alloc_unr(state->dts_aggid_arena); 10144#endif 10145 10146 if (aggid - 1 >= state->dts_naggregations) { 10147 dtrace_aggregation_t **oaggs = state->dts_aggregations; 10148 dtrace_aggregation_t **aggs; 10149 int naggs = state->dts_naggregations << 1; 10150 int onaggs = state->dts_naggregations; 10151 10152 ASSERT(aggid == state->dts_naggregations + 1); 10153 10154 if (naggs == 0) { 10155 ASSERT(oaggs == NULL); 10156 naggs = 1; 10157 } 10158 10159 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 10160 10161 if (oaggs != NULL) { 10162 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 10163 kmem_free(oaggs, onaggs * sizeof (*aggs)); 10164 } 10165 10166 state->dts_aggregations = aggs; 10167 state->dts_naggregations = naggs; 10168 } 10169 10170 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 10171 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 10172 10173 frec = &agg->dtag_first->dta_rec; 10174 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 10175 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 10176 10177 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 10178 ASSERT(!act->dta_intuple); 10179 act->dta_intuple = 1; 10180 } 10181 10182 return (&agg->dtag_action); 10183} 10184 10185static void 10186dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 10187{ 10188 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 10189 dtrace_state_t *state = ecb->dte_state; 10190 dtrace_aggid_t aggid = agg->dtag_id; 10191 10192 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 10193#if defined(sun) 10194 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 10195#else 10196 free_unr(state->dts_aggid_arena, aggid); 10197#endif 10198 10199 ASSERT(state->dts_aggregations[aggid - 1] == agg); 10200 state->dts_aggregations[aggid - 1] = NULL; 10201 10202 kmem_free(agg, sizeof (dtrace_aggregation_t)); 10203} 10204 10205static int 10206dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 10207{ 10208 dtrace_action_t *action, *last; 10209 dtrace_difo_t *dp = desc->dtad_difo; 10210 uint32_t size = 0, align = sizeof (uint8_t), mask; 10211 uint16_t format = 0; 10212 dtrace_recdesc_t *rec; 10213 dtrace_state_t *state = ecb->dte_state; 10214 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 10215 uint64_t arg = desc->dtad_arg; 10216 10217 ASSERT(MUTEX_HELD(&dtrace_lock)); 10218 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 10219 10220 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 10221 /* 10222 * If this is an aggregating action, there must be neither 10223 * a speculate nor a commit on the action chain. 10224 */ 10225 dtrace_action_t *act; 10226 10227 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 10228 if (act->dta_kind == DTRACEACT_COMMIT) 10229 return (EINVAL); 10230 10231 if (act->dta_kind == DTRACEACT_SPECULATE) 10232 return (EINVAL); 10233 } 10234 10235 action = dtrace_ecb_aggregation_create(ecb, desc); 10236 10237 if (action == NULL) 10238 return (EINVAL); 10239 } else { 10240 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 10241 (desc->dtad_kind == DTRACEACT_DIFEXPR && 10242 dp != NULL && dp->dtdo_destructive)) { 10243 state->dts_destructive = 1; 10244 } 10245 10246 switch (desc->dtad_kind) { 10247 case DTRACEACT_PRINTF: 10248 case DTRACEACT_PRINTA: 10249 case DTRACEACT_SYSTEM: 10250 case DTRACEACT_FREOPEN: 10251 case DTRACEACT_DIFEXPR: 10252 /* 10253 * We know that our arg is a string -- turn it into a 10254 * format. 10255 */ 10256 if (arg == 0) { 10257 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 10258 desc->dtad_kind == DTRACEACT_DIFEXPR); 10259 format = 0; 10260 } else { 10261 ASSERT(arg != 0); 10262#if defined(sun) 10263 ASSERT(arg > KERNELBASE); 10264#endif 10265 format = dtrace_format_add(state, 10266 (char *)(uintptr_t)arg); 10267 } 10268 10269 /*FALLTHROUGH*/ 10270 case DTRACEACT_LIBACT: 10271 case DTRACEACT_TRACEMEM: 10272 case DTRACEACT_TRACEMEM_DYNSIZE: 10273 if (dp == NULL) 10274 return (EINVAL); 10275 10276 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 10277 break; 10278 10279 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 10280 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10281 return (EINVAL); 10282 10283 size = opt[DTRACEOPT_STRSIZE]; 10284 } 10285 10286 break; 10287 10288 case DTRACEACT_STACK: 10289 if ((nframes = arg) == 0) { 10290 nframes = opt[DTRACEOPT_STACKFRAMES]; 10291 ASSERT(nframes > 0); 10292 arg = nframes; 10293 } 10294 10295 size = nframes * sizeof (pc_t); 10296 break; 10297 10298 case DTRACEACT_JSTACK: 10299 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 10300 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 10301 10302 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 10303 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 10304 10305 arg = DTRACE_USTACK_ARG(nframes, strsize); 10306 10307 /*FALLTHROUGH*/ 10308 case DTRACEACT_USTACK: 10309 if (desc->dtad_kind != DTRACEACT_JSTACK && 10310 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 10311 strsize = DTRACE_USTACK_STRSIZE(arg); 10312 nframes = opt[DTRACEOPT_USTACKFRAMES]; 10313 ASSERT(nframes > 0); 10314 arg = DTRACE_USTACK_ARG(nframes, strsize); 10315 } 10316 10317 /* 10318 * Save a slot for the pid. 10319 */ 10320 size = (nframes + 1) * sizeof (uint64_t); 10321 size += DTRACE_USTACK_STRSIZE(arg); 10322 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 10323 10324 break; 10325 10326 case DTRACEACT_SYM: 10327 case DTRACEACT_MOD: 10328 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 10329 sizeof (uint64_t)) || 10330 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10331 return (EINVAL); 10332 break; 10333 10334 case DTRACEACT_USYM: 10335 case DTRACEACT_UMOD: 10336 case DTRACEACT_UADDR: 10337 if (dp == NULL || 10338 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 10339 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10340 return (EINVAL); 10341 10342 /* 10343 * We have a slot for the pid, plus a slot for the 10344 * argument. To keep things simple (aligned with 10345 * bitness-neutral sizing), we store each as a 64-bit 10346 * quantity. 10347 */ 10348 size = 2 * sizeof (uint64_t); 10349 break; 10350 10351 case DTRACEACT_STOP: 10352 case DTRACEACT_BREAKPOINT: 10353 case DTRACEACT_PANIC: 10354 break; 10355 10356 case DTRACEACT_CHILL: 10357 case DTRACEACT_DISCARD: 10358 case DTRACEACT_RAISE: 10359 if (dp == NULL) 10360 return (EINVAL); 10361 break; 10362 10363 case DTRACEACT_EXIT: 10364 if (dp == NULL || 10365 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 10366 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10367 return (EINVAL); 10368 break; 10369 10370 case DTRACEACT_SPECULATE: 10371 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 10372 return (EINVAL); 10373 10374 if (dp == NULL) 10375 return (EINVAL); 10376 10377 state->dts_speculates = 1; 10378 break; 10379 10380 case DTRACEACT_PRINTM: 10381 size = dp->dtdo_rtype.dtdt_size; 10382 break; 10383 10384 case DTRACEACT_PRINTT: 10385 size = dp->dtdo_rtype.dtdt_size; 10386 break; 10387 10388 case DTRACEACT_COMMIT: { 10389 dtrace_action_t *act = ecb->dte_action; 10390 10391 for (; act != NULL; act = act->dta_next) { 10392 if (act->dta_kind == DTRACEACT_COMMIT) 10393 return (EINVAL); 10394 } 10395 10396 if (dp == NULL) 10397 return (EINVAL); 10398 break; 10399 } 10400 10401 default: 10402 return (EINVAL); 10403 } 10404 10405 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 10406 /* 10407 * If this is a data-storing action or a speculate, 10408 * we must be sure that there isn't a commit on the 10409 * action chain. 10410 */ 10411 dtrace_action_t *act = ecb->dte_action; 10412 10413 for (; act != NULL; act = act->dta_next) { 10414 if (act->dta_kind == DTRACEACT_COMMIT) 10415 return (EINVAL); 10416 } 10417 } 10418 10419 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 10420 action->dta_rec.dtrd_size = size; 10421 } 10422 10423 action->dta_refcnt = 1; 10424 rec = &action->dta_rec; 10425 size = rec->dtrd_size; 10426 10427 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 10428 if (!(size & mask)) { 10429 align = mask + 1; 10430 break; 10431 } 10432 } 10433 10434 action->dta_kind = desc->dtad_kind; 10435 10436 if ((action->dta_difo = dp) != NULL) 10437 dtrace_difo_hold(dp); 10438 10439 rec->dtrd_action = action->dta_kind; 10440 rec->dtrd_arg = arg; 10441 rec->dtrd_uarg = desc->dtad_uarg; 10442 rec->dtrd_alignment = (uint16_t)align; 10443 rec->dtrd_format = format; 10444 10445 if ((last = ecb->dte_action_last) != NULL) { 10446 ASSERT(ecb->dte_action != NULL); 10447 action->dta_prev = last; 10448 last->dta_next = action; 10449 } else { 10450 ASSERT(ecb->dte_action == NULL); 10451 ecb->dte_action = action; 10452 } 10453 10454 ecb->dte_action_last = action; 10455 10456 return (0); 10457} 10458 10459static void 10460dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 10461{ 10462 dtrace_action_t *act = ecb->dte_action, *next; 10463 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 10464 dtrace_difo_t *dp; 10465 uint16_t format; 10466 10467 if (act != NULL && act->dta_refcnt > 1) { 10468 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 10469 act->dta_refcnt--; 10470 } else { 10471 for (; act != NULL; act = next) { 10472 next = act->dta_next; 10473 ASSERT(next != NULL || act == ecb->dte_action_last); 10474 ASSERT(act->dta_refcnt == 1); 10475 10476 if ((format = act->dta_rec.dtrd_format) != 0) 10477 dtrace_format_remove(ecb->dte_state, format); 10478 10479 if ((dp = act->dta_difo) != NULL) 10480 dtrace_difo_release(dp, vstate); 10481 10482 if (DTRACEACT_ISAGG(act->dta_kind)) { 10483 dtrace_ecb_aggregation_destroy(ecb, act); 10484 } else { 10485 kmem_free(act, sizeof (dtrace_action_t)); 10486 } 10487 } 10488 } 10489 10490 ecb->dte_action = NULL; 10491 ecb->dte_action_last = NULL; 10492 ecb->dte_size = 0; 10493} 10494 10495static void 10496dtrace_ecb_disable(dtrace_ecb_t *ecb) 10497{ 10498 /* 10499 * We disable the ECB by removing it from its probe. 10500 */ 10501 dtrace_ecb_t *pecb, *prev = NULL; 10502 dtrace_probe_t *probe = ecb->dte_probe; 10503 10504 ASSERT(MUTEX_HELD(&dtrace_lock)); 10505 10506 if (probe == NULL) { 10507 /* 10508 * This is the NULL probe; there is nothing to disable. 10509 */ 10510 return; 10511 } 10512 10513 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 10514 if (pecb == ecb) 10515 break; 10516 prev = pecb; 10517 } 10518 10519 ASSERT(pecb != NULL); 10520 10521 if (prev == NULL) { 10522 probe->dtpr_ecb = ecb->dte_next; 10523 } else { 10524 prev->dte_next = ecb->dte_next; 10525 } 10526 10527 if (ecb == probe->dtpr_ecb_last) { 10528 ASSERT(ecb->dte_next == NULL); 10529 probe->dtpr_ecb_last = prev; 10530 } 10531 10532 /* 10533 * The ECB has been disconnected from the probe; now sync to assure 10534 * that all CPUs have seen the change before returning. 10535 */ 10536 dtrace_sync(); 10537 10538 if (probe->dtpr_ecb == NULL) { 10539 /* 10540 * That was the last ECB on the probe; clear the predicate 10541 * cache ID for the probe, disable it and sync one more time 10542 * to assure that we'll never hit it again. 10543 */ 10544 dtrace_provider_t *prov = probe->dtpr_provider; 10545 10546 ASSERT(ecb->dte_next == NULL); 10547 ASSERT(probe->dtpr_ecb_last == NULL); 10548 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 10549 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 10550 probe->dtpr_id, probe->dtpr_arg); 10551 dtrace_sync(); 10552 } else { 10553 /* 10554 * There is at least one ECB remaining on the probe. If there 10555 * is _exactly_ one, set the probe's predicate cache ID to be 10556 * the predicate cache ID of the remaining ECB. 10557 */ 10558 ASSERT(probe->dtpr_ecb_last != NULL); 10559 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 10560 10561 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 10562 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 10563 10564 ASSERT(probe->dtpr_ecb->dte_next == NULL); 10565 10566 if (p != NULL) 10567 probe->dtpr_predcache = p->dtp_cacheid; 10568 } 10569 10570 ecb->dte_next = NULL; 10571 } 10572} 10573 10574static void 10575dtrace_ecb_destroy(dtrace_ecb_t *ecb) 10576{ 10577 dtrace_state_t *state = ecb->dte_state; 10578 dtrace_vstate_t *vstate = &state->dts_vstate; 10579 dtrace_predicate_t *pred; 10580 dtrace_epid_t epid = ecb->dte_epid; 10581 10582 ASSERT(MUTEX_HELD(&dtrace_lock)); 10583 ASSERT(ecb->dte_next == NULL); 10584 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 10585 10586 if ((pred = ecb->dte_predicate) != NULL) 10587 dtrace_predicate_release(pred, vstate); 10588 10589 dtrace_ecb_action_remove(ecb); 10590 10591 ASSERT(state->dts_ecbs[epid - 1] == ecb); 10592 state->dts_ecbs[epid - 1] = NULL; 10593 10594 kmem_free(ecb, sizeof (dtrace_ecb_t)); 10595} 10596 10597static dtrace_ecb_t * 10598dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 10599 dtrace_enabling_t *enab) 10600{ 10601 dtrace_ecb_t *ecb; 10602 dtrace_predicate_t *pred; 10603 dtrace_actdesc_t *act; 10604 dtrace_provider_t *prov; 10605 dtrace_ecbdesc_t *desc = enab->dten_current; 10606 10607 ASSERT(MUTEX_HELD(&dtrace_lock)); 10608 ASSERT(state != NULL); 10609 10610 ecb = dtrace_ecb_add(state, probe); 10611 ecb->dte_uarg = desc->dted_uarg; 10612 10613 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 10614 dtrace_predicate_hold(pred); 10615 ecb->dte_predicate = pred; 10616 } 10617 10618 if (probe != NULL) { 10619 /* 10620 * If the provider shows more leg than the consumer is old 10621 * enough to see, we need to enable the appropriate implicit 10622 * predicate bits to prevent the ecb from activating at 10623 * revealing times. 10624 * 10625 * Providers specifying DTRACE_PRIV_USER at register time 10626 * are stating that they need the /proc-style privilege 10627 * model to be enforced, and this is what DTRACE_COND_OWNER 10628 * and DTRACE_COND_ZONEOWNER will then do at probe time. 10629 */ 10630 prov = probe->dtpr_provider; 10631 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 10632 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10633 ecb->dte_cond |= DTRACE_COND_OWNER; 10634 10635 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 10636 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10637 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 10638 10639 /* 10640 * If the provider shows us kernel innards and the user 10641 * is lacking sufficient privilege, enable the 10642 * DTRACE_COND_USERMODE implicit predicate. 10643 */ 10644 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 10645 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 10646 ecb->dte_cond |= DTRACE_COND_USERMODE; 10647 } 10648 10649 if (dtrace_ecb_create_cache != NULL) { 10650 /* 10651 * If we have a cached ecb, we'll use its action list instead 10652 * of creating our own (saving both time and space). 10653 */ 10654 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 10655 dtrace_action_t *act = cached->dte_action; 10656 10657 if (act != NULL) { 10658 ASSERT(act->dta_refcnt > 0); 10659 act->dta_refcnt++; 10660 ecb->dte_action = act; 10661 ecb->dte_action_last = cached->dte_action_last; 10662 ecb->dte_needed = cached->dte_needed; 10663 ecb->dte_size = cached->dte_size; 10664 ecb->dte_alignment = cached->dte_alignment; 10665 } 10666 10667 return (ecb); 10668 } 10669 10670 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 10671 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 10672 dtrace_ecb_destroy(ecb); 10673 return (NULL); 10674 } 10675 } 10676 10677 dtrace_ecb_resize(ecb); 10678 10679 return (dtrace_ecb_create_cache = ecb); 10680} 10681 10682static int 10683dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 10684{ 10685 dtrace_ecb_t *ecb; 10686 dtrace_enabling_t *enab = arg; 10687 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 10688 10689 ASSERT(state != NULL); 10690 10691 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 10692 /* 10693 * This probe was created in a generation for which this 10694 * enabling has previously created ECBs; we don't want to 10695 * enable it again, so just kick out. 10696 */ 10697 return (DTRACE_MATCH_NEXT); 10698 } 10699 10700 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 10701 return (DTRACE_MATCH_DONE); 10702 10703 dtrace_ecb_enable(ecb); 10704 return (DTRACE_MATCH_NEXT); 10705} 10706 10707static dtrace_ecb_t * 10708dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 10709{ 10710 dtrace_ecb_t *ecb; 10711 10712 ASSERT(MUTEX_HELD(&dtrace_lock)); 10713 10714 if (id == 0 || id > state->dts_necbs) 10715 return (NULL); 10716 10717 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 10718 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 10719 10720 return (state->dts_ecbs[id - 1]); 10721} 10722 10723static dtrace_aggregation_t * 10724dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 10725{ 10726 dtrace_aggregation_t *agg; 10727 10728 ASSERT(MUTEX_HELD(&dtrace_lock)); 10729 10730 if (id == 0 || id > state->dts_naggregations) 10731 return (NULL); 10732 10733 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 10734 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 10735 agg->dtag_id == id); 10736 10737 return (state->dts_aggregations[id - 1]); 10738} 10739 10740/* 10741 * DTrace Buffer Functions 10742 * 10743 * The following functions manipulate DTrace buffers. Most of these functions 10744 * are called in the context of establishing or processing consumer state; 10745 * exceptions are explicitly noted. 10746 */ 10747 10748/* 10749 * Note: called from cross call context. This function switches the two 10750 * buffers on a given CPU. The atomicity of this operation is assured by 10751 * disabling interrupts while the actual switch takes place; the disabling of 10752 * interrupts serializes the execution with any execution of dtrace_probe() on 10753 * the same CPU. 10754 */ 10755static void 10756dtrace_buffer_switch(dtrace_buffer_t *buf) 10757{ 10758 caddr_t tomax = buf->dtb_tomax; 10759 caddr_t xamot = buf->dtb_xamot; 10760 dtrace_icookie_t cookie; 10761 hrtime_t now; 10762 10763 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10764 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 10765 10766 cookie = dtrace_interrupt_disable(); 10767 now = dtrace_gethrtime(); 10768 buf->dtb_tomax = xamot; 10769 buf->dtb_xamot = tomax; 10770 buf->dtb_xamot_drops = buf->dtb_drops; 10771 buf->dtb_xamot_offset = buf->dtb_offset; 10772 buf->dtb_xamot_errors = buf->dtb_errors; 10773 buf->dtb_xamot_flags = buf->dtb_flags; 10774 buf->dtb_offset = 0; 10775 buf->dtb_drops = 0; 10776 buf->dtb_errors = 0; 10777 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 10778 buf->dtb_interval = now - buf->dtb_switched; 10779 buf->dtb_switched = now; 10780 dtrace_interrupt_enable(cookie); 10781} 10782 10783/* 10784 * Note: called from cross call context. This function activates a buffer 10785 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 10786 * is guaranteed by the disabling of interrupts. 10787 */ 10788static void 10789dtrace_buffer_activate(dtrace_state_t *state) 10790{ 10791 dtrace_buffer_t *buf; 10792 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 10793 10794 buf = &state->dts_buffer[curcpu]; 10795 10796 if (buf->dtb_tomax != NULL) { 10797 /* 10798 * We might like to assert that the buffer is marked inactive, 10799 * but this isn't necessarily true: the buffer for the CPU 10800 * that processes the BEGIN probe has its buffer activated 10801 * manually. In this case, we take the (harmless) action 10802 * re-clearing the bit INACTIVE bit. 10803 */ 10804 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 10805 } 10806 10807 dtrace_interrupt_enable(cookie); 10808} 10809 10810static int 10811dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 10812 processorid_t cpu, int *factor) 10813{ 10814#if defined(sun) 10815 cpu_t *cp; 10816#endif 10817 dtrace_buffer_t *buf; 10818 int allocated = 0, desired = 0; 10819 10820#if defined(sun) 10821 ASSERT(MUTEX_HELD(&cpu_lock)); 10822 ASSERT(MUTEX_HELD(&dtrace_lock)); 10823 10824 *factor = 1; 10825 10826 if (size > dtrace_nonroot_maxsize && 10827 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 10828 return (EFBIG); 10829 10830 cp = cpu_list; 10831 10832 do { 10833 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10834 continue; 10835 10836 buf = &bufs[cp->cpu_id]; 10837 10838 /* 10839 * If there is already a buffer allocated for this CPU, it 10840 * is only possible that this is a DR event. In this case, 10841 */ 10842 if (buf->dtb_tomax != NULL) { 10843 ASSERT(buf->dtb_size == size); 10844 continue; 10845 } 10846 10847 ASSERT(buf->dtb_xamot == NULL); 10848 10849 if ((buf->dtb_tomax = kmem_zalloc(size, 10850 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10851 goto err; 10852 10853 buf->dtb_size = size; 10854 buf->dtb_flags = flags; 10855 buf->dtb_offset = 0; 10856 buf->dtb_drops = 0; 10857 10858 if (flags & DTRACEBUF_NOSWITCH) 10859 continue; 10860 10861 if ((buf->dtb_xamot = kmem_zalloc(size, 10862 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10863 goto err; 10864 } while ((cp = cp->cpu_next) != cpu_list); 10865 10866 return (0); 10867 10868err: 10869 cp = cpu_list; 10870 10871 do { 10872 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10873 continue; 10874 10875 buf = &bufs[cp->cpu_id]; 10876 desired += 2; 10877 10878 if (buf->dtb_xamot != NULL) { 10879 ASSERT(buf->dtb_tomax != NULL); 10880 ASSERT(buf->dtb_size == size); 10881 kmem_free(buf->dtb_xamot, size); 10882 allocated++; 10883 } 10884 10885 if (buf->dtb_tomax != NULL) { 10886 ASSERT(buf->dtb_size == size); 10887 kmem_free(buf->dtb_tomax, size); 10888 allocated++; 10889 } 10890 10891 buf->dtb_tomax = NULL; 10892 buf->dtb_xamot = NULL; 10893 buf->dtb_size = 0; 10894 } while ((cp = cp->cpu_next) != cpu_list); 10895#else 10896 int i; 10897 10898 *factor = 1; 10899#if defined(__amd64__) || defined(__mips__) || defined(__powerpc__) 10900 /* 10901 * FreeBSD isn't good at limiting the amount of memory we 10902 * ask to malloc, so let's place a limit here before trying 10903 * to do something that might well end in tears at bedtime. 10904 */ 10905 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 10906 return (ENOMEM); 10907#endif 10908 10909 ASSERT(MUTEX_HELD(&dtrace_lock)); 10910 CPU_FOREACH(i) { 10911 if (cpu != DTRACE_CPUALL && cpu != i) 10912 continue; 10913 10914 buf = &bufs[i]; 10915 10916 /* 10917 * If there is already a buffer allocated for this CPU, it 10918 * is only possible that this is a DR event. In this case, 10919 * the buffer size must match our specified size. 10920 */ 10921 if (buf->dtb_tomax != NULL) { 10922 ASSERT(buf->dtb_size == size); 10923 continue; 10924 } 10925 10926 ASSERT(buf->dtb_xamot == NULL); 10927 10928 if ((buf->dtb_tomax = kmem_zalloc(size, 10929 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10930 goto err; 10931 10932 buf->dtb_size = size; 10933 buf->dtb_flags = flags; 10934 buf->dtb_offset = 0; 10935 buf->dtb_drops = 0; 10936 10937 if (flags & DTRACEBUF_NOSWITCH) 10938 continue; 10939 10940 if ((buf->dtb_xamot = kmem_zalloc(size, 10941 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 10942 goto err; 10943 } 10944 10945 return (0); 10946 10947err: 10948 /* 10949 * Error allocating memory, so free the buffers that were 10950 * allocated before the failed allocation. 10951 */ 10952 CPU_FOREACH(i) { 10953 if (cpu != DTRACE_CPUALL && cpu != i) 10954 continue; 10955 10956 buf = &bufs[i]; 10957 desired += 2; 10958 10959 if (buf->dtb_xamot != NULL) { 10960 ASSERT(buf->dtb_tomax != NULL); 10961 ASSERT(buf->dtb_size == size); 10962 kmem_free(buf->dtb_xamot, size); 10963 allocated++; 10964 } 10965 10966 if (buf->dtb_tomax != NULL) { 10967 ASSERT(buf->dtb_size == size); 10968 kmem_free(buf->dtb_tomax, size); 10969 allocated++; 10970 } 10971 10972 buf->dtb_tomax = NULL; 10973 buf->dtb_xamot = NULL; 10974 buf->dtb_size = 0; 10975 10976 } 10977#endif 10978 *factor = desired / (allocated > 0 ? allocated : 1); 10979 10980 return (ENOMEM); 10981} 10982 10983/* 10984 * Note: called from probe context. This function just increments the drop 10985 * count on a buffer. It has been made a function to allow for the 10986 * possibility of understanding the source of mysterious drop counts. (A 10987 * problem for which one may be particularly disappointed that DTrace cannot 10988 * be used to understand DTrace.) 10989 */ 10990static void 10991dtrace_buffer_drop(dtrace_buffer_t *buf) 10992{ 10993 buf->dtb_drops++; 10994} 10995 10996/* 10997 * Note: called from probe context. This function is called to reserve space 10998 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 10999 * mstate. Returns the new offset in the buffer, or a negative value if an 11000 * error has occurred. 11001 */ 11002static intptr_t 11003dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 11004 dtrace_state_t *state, dtrace_mstate_t *mstate) 11005{ 11006 intptr_t offs = buf->dtb_offset, soffs; 11007 intptr_t woffs; 11008 caddr_t tomax; 11009 size_t total; 11010 11011 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 11012 return (-1); 11013 11014 if ((tomax = buf->dtb_tomax) == NULL) { 11015 dtrace_buffer_drop(buf); 11016 return (-1); 11017 } 11018 11019 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 11020 while (offs & (align - 1)) { 11021 /* 11022 * Assert that our alignment is off by a number which 11023 * is itself sizeof (uint32_t) aligned. 11024 */ 11025 ASSERT(!((align - (offs & (align - 1))) & 11026 (sizeof (uint32_t) - 1))); 11027 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 11028 offs += sizeof (uint32_t); 11029 } 11030 11031 if ((soffs = offs + needed) > buf->dtb_size) { 11032 dtrace_buffer_drop(buf); 11033 return (-1); 11034 } 11035 11036 if (mstate == NULL) 11037 return (offs); 11038 11039 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 11040 mstate->dtms_scratch_size = buf->dtb_size - soffs; 11041 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 11042 11043 return (offs); 11044 } 11045 11046 if (buf->dtb_flags & DTRACEBUF_FILL) { 11047 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 11048 (buf->dtb_flags & DTRACEBUF_FULL)) 11049 return (-1); 11050 goto out; 11051 } 11052 11053 total = needed + (offs & (align - 1)); 11054 11055 /* 11056 * For a ring buffer, life is quite a bit more complicated. Before 11057 * we can store any padding, we need to adjust our wrapping offset. 11058 * (If we've never before wrapped or we're not about to, no adjustment 11059 * is required.) 11060 */ 11061 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 11062 offs + total > buf->dtb_size) { 11063 woffs = buf->dtb_xamot_offset; 11064 11065 if (offs + total > buf->dtb_size) { 11066 /* 11067 * We can't fit in the end of the buffer. First, a 11068 * sanity check that we can fit in the buffer at all. 11069 */ 11070 if (total > buf->dtb_size) { 11071 dtrace_buffer_drop(buf); 11072 return (-1); 11073 } 11074 11075 /* 11076 * We're going to be storing at the top of the buffer, 11077 * so now we need to deal with the wrapped offset. We 11078 * only reset our wrapped offset to 0 if it is 11079 * currently greater than the current offset. If it 11080 * is less than the current offset, it is because a 11081 * previous allocation induced a wrap -- but the 11082 * allocation didn't subsequently take the space due 11083 * to an error or false predicate evaluation. In this 11084 * case, we'll just leave the wrapped offset alone: if 11085 * the wrapped offset hasn't been advanced far enough 11086 * for this allocation, it will be adjusted in the 11087 * lower loop. 11088 */ 11089 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 11090 if (woffs >= offs) 11091 woffs = 0; 11092 } else { 11093 woffs = 0; 11094 } 11095 11096 /* 11097 * Now we know that we're going to be storing to the 11098 * top of the buffer and that there is room for us 11099 * there. We need to clear the buffer from the current 11100 * offset to the end (there may be old gunk there). 11101 */ 11102 while (offs < buf->dtb_size) 11103 tomax[offs++] = 0; 11104 11105 /* 11106 * We need to set our offset to zero. And because we 11107 * are wrapping, we need to set the bit indicating as 11108 * much. We can also adjust our needed space back 11109 * down to the space required by the ECB -- we know 11110 * that the top of the buffer is aligned. 11111 */ 11112 offs = 0; 11113 total = needed; 11114 buf->dtb_flags |= DTRACEBUF_WRAPPED; 11115 } else { 11116 /* 11117 * There is room for us in the buffer, so we simply 11118 * need to check the wrapped offset. 11119 */ 11120 if (woffs < offs) { 11121 /* 11122 * The wrapped offset is less than the offset. 11123 * This can happen if we allocated buffer space 11124 * that induced a wrap, but then we didn't 11125 * subsequently take the space due to an error 11126 * or false predicate evaluation. This is 11127 * okay; we know that _this_ allocation isn't 11128 * going to induce a wrap. We still can't 11129 * reset the wrapped offset to be zero, 11130 * however: the space may have been trashed in 11131 * the previous failed probe attempt. But at 11132 * least the wrapped offset doesn't need to 11133 * be adjusted at all... 11134 */ 11135 goto out; 11136 } 11137 } 11138 11139 while (offs + total > woffs) { 11140 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 11141 size_t size; 11142 11143 if (epid == DTRACE_EPIDNONE) { 11144 size = sizeof (uint32_t); 11145 } else { 11146 ASSERT3U(epid, <=, state->dts_necbs); 11147 ASSERT(state->dts_ecbs[epid - 1] != NULL); 11148 11149 size = state->dts_ecbs[epid - 1]->dte_size; 11150 } 11151 11152 ASSERT(woffs + size <= buf->dtb_size); 11153 ASSERT(size != 0); 11154 11155 if (woffs + size == buf->dtb_size) { 11156 /* 11157 * We've reached the end of the buffer; we want 11158 * to set the wrapped offset to 0 and break 11159 * out. However, if the offs is 0, then we're 11160 * in a strange edge-condition: the amount of 11161 * space that we want to reserve plus the size 11162 * of the record that we're overwriting is 11163 * greater than the size of the buffer. This 11164 * is problematic because if we reserve the 11165 * space but subsequently don't consume it (due 11166 * to a failed predicate or error) the wrapped 11167 * offset will be 0 -- yet the EPID at offset 0 11168 * will not be committed. This situation is 11169 * relatively easy to deal with: if we're in 11170 * this case, the buffer is indistinguishable 11171 * from one that hasn't wrapped; we need only 11172 * finish the job by clearing the wrapped bit, 11173 * explicitly setting the offset to be 0, and 11174 * zero'ing out the old data in the buffer. 11175 */ 11176 if (offs == 0) { 11177 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 11178 buf->dtb_offset = 0; 11179 woffs = total; 11180 11181 while (woffs < buf->dtb_size) 11182 tomax[woffs++] = 0; 11183 } 11184 11185 woffs = 0; 11186 break; 11187 } 11188 11189 woffs += size; 11190 } 11191 11192 /* 11193 * We have a wrapped offset. It may be that the wrapped offset 11194 * has become zero -- that's okay. 11195 */ 11196 buf->dtb_xamot_offset = woffs; 11197 } 11198 11199out: 11200 /* 11201 * Now we can plow the buffer with any necessary padding. 11202 */ 11203 while (offs & (align - 1)) { 11204 /* 11205 * Assert that our alignment is off by a number which 11206 * is itself sizeof (uint32_t) aligned. 11207 */ 11208 ASSERT(!((align - (offs & (align - 1))) & 11209 (sizeof (uint32_t) - 1))); 11210 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 11211 offs += sizeof (uint32_t); 11212 } 11213 11214 if (buf->dtb_flags & DTRACEBUF_FILL) { 11215 if (offs + needed > buf->dtb_size - state->dts_reserve) { 11216 buf->dtb_flags |= DTRACEBUF_FULL; 11217 return (-1); 11218 } 11219 } 11220 11221 if (mstate == NULL) 11222 return (offs); 11223 11224 /* 11225 * For ring buffers and fill buffers, the scratch space is always 11226 * the inactive buffer. 11227 */ 11228 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 11229 mstate->dtms_scratch_size = buf->dtb_size; 11230 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 11231 11232 return (offs); 11233} 11234 11235static void 11236dtrace_buffer_polish(dtrace_buffer_t *buf) 11237{ 11238 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 11239 ASSERT(MUTEX_HELD(&dtrace_lock)); 11240 11241 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 11242 return; 11243 11244 /* 11245 * We need to polish the ring buffer. There are three cases: 11246 * 11247 * - The first (and presumably most common) is that there is no gap 11248 * between the buffer offset and the wrapped offset. In this case, 11249 * there is nothing in the buffer that isn't valid data; we can 11250 * mark the buffer as polished and return. 11251 * 11252 * - The second (less common than the first but still more common 11253 * than the third) is that there is a gap between the buffer offset 11254 * and the wrapped offset, and the wrapped offset is larger than the 11255 * buffer offset. This can happen because of an alignment issue, or 11256 * can happen because of a call to dtrace_buffer_reserve() that 11257 * didn't subsequently consume the buffer space. In this case, 11258 * we need to zero the data from the buffer offset to the wrapped 11259 * offset. 11260 * 11261 * - The third (and least common) is that there is a gap between the 11262 * buffer offset and the wrapped offset, but the wrapped offset is 11263 * _less_ than the buffer offset. This can only happen because a 11264 * call to dtrace_buffer_reserve() induced a wrap, but the space 11265 * was not subsequently consumed. In this case, we need to zero the 11266 * space from the offset to the end of the buffer _and_ from the 11267 * top of the buffer to the wrapped offset. 11268 */ 11269 if (buf->dtb_offset < buf->dtb_xamot_offset) { 11270 bzero(buf->dtb_tomax + buf->dtb_offset, 11271 buf->dtb_xamot_offset - buf->dtb_offset); 11272 } 11273 11274 if (buf->dtb_offset > buf->dtb_xamot_offset) { 11275 bzero(buf->dtb_tomax + buf->dtb_offset, 11276 buf->dtb_size - buf->dtb_offset); 11277 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 11278 } 11279} 11280 11281/* 11282 * This routine determines if data generated at the specified time has likely 11283 * been entirely consumed at user-level. This routine is called to determine 11284 * if an ECB on a defunct probe (but for an active enabling) can be safely 11285 * disabled and destroyed. 11286 */ 11287static int 11288dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 11289{ 11290 int i; 11291 11292 for (i = 0; i < NCPU; i++) { 11293 dtrace_buffer_t *buf = &bufs[i]; 11294 11295 if (buf->dtb_size == 0) 11296 continue; 11297 11298 if (buf->dtb_flags & DTRACEBUF_RING) 11299 return (0); 11300 11301 if (!buf->dtb_switched && buf->dtb_offset != 0) 11302 return (0); 11303 11304 if (buf->dtb_switched - buf->dtb_interval < when) 11305 return (0); 11306 } 11307 11308 return (1); 11309} 11310 11311static void 11312dtrace_buffer_free(dtrace_buffer_t *bufs) 11313{ 11314 int i; 11315 11316 for (i = 0; i < NCPU; i++) { 11317 dtrace_buffer_t *buf = &bufs[i]; 11318 11319 if (buf->dtb_tomax == NULL) { 11320 ASSERT(buf->dtb_xamot == NULL); 11321 ASSERT(buf->dtb_size == 0); 11322 continue; 11323 } 11324 11325 if (buf->dtb_xamot != NULL) { 11326 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11327 kmem_free(buf->dtb_xamot, buf->dtb_size); 11328 } 11329 11330 kmem_free(buf->dtb_tomax, buf->dtb_size); 11331 buf->dtb_size = 0; 11332 buf->dtb_tomax = NULL; 11333 buf->dtb_xamot = NULL; 11334 } 11335} 11336 11337/* 11338 * DTrace Enabling Functions 11339 */ 11340static dtrace_enabling_t * 11341dtrace_enabling_create(dtrace_vstate_t *vstate) 11342{ 11343 dtrace_enabling_t *enab; 11344 11345 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 11346 enab->dten_vstate = vstate; 11347 11348 return (enab); 11349} 11350 11351static void 11352dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 11353{ 11354 dtrace_ecbdesc_t **ndesc; 11355 size_t osize, nsize; 11356 11357 /* 11358 * We can't add to enablings after we've enabled them, or after we've 11359 * retained them. 11360 */ 11361 ASSERT(enab->dten_probegen == 0); 11362 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11363 11364 if (enab->dten_ndesc < enab->dten_maxdesc) { 11365 enab->dten_desc[enab->dten_ndesc++] = ecb; 11366 return; 11367 } 11368 11369 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11370 11371 if (enab->dten_maxdesc == 0) { 11372 enab->dten_maxdesc = 1; 11373 } else { 11374 enab->dten_maxdesc <<= 1; 11375 } 11376 11377 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 11378 11379 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11380 ndesc = kmem_zalloc(nsize, KM_SLEEP); 11381 bcopy(enab->dten_desc, ndesc, osize); 11382 if (enab->dten_desc != NULL) 11383 kmem_free(enab->dten_desc, osize); 11384 11385 enab->dten_desc = ndesc; 11386 enab->dten_desc[enab->dten_ndesc++] = ecb; 11387} 11388 11389static void 11390dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 11391 dtrace_probedesc_t *pd) 11392{ 11393 dtrace_ecbdesc_t *new; 11394 dtrace_predicate_t *pred; 11395 dtrace_actdesc_t *act; 11396 11397 /* 11398 * We're going to create a new ECB description that matches the 11399 * specified ECB in every way, but has the specified probe description. 11400 */ 11401 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 11402 11403 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 11404 dtrace_predicate_hold(pred); 11405 11406 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 11407 dtrace_actdesc_hold(act); 11408 11409 new->dted_action = ecb->dted_action; 11410 new->dted_pred = ecb->dted_pred; 11411 new->dted_probe = *pd; 11412 new->dted_uarg = ecb->dted_uarg; 11413 11414 dtrace_enabling_add(enab, new); 11415} 11416 11417static void 11418dtrace_enabling_dump(dtrace_enabling_t *enab) 11419{ 11420 int i; 11421 11422 for (i = 0; i < enab->dten_ndesc; i++) { 11423 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 11424 11425 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 11426 desc->dtpd_provider, desc->dtpd_mod, 11427 desc->dtpd_func, desc->dtpd_name); 11428 } 11429} 11430 11431static void 11432dtrace_enabling_destroy(dtrace_enabling_t *enab) 11433{ 11434 int i; 11435 dtrace_ecbdesc_t *ep; 11436 dtrace_vstate_t *vstate = enab->dten_vstate; 11437 11438 ASSERT(MUTEX_HELD(&dtrace_lock)); 11439 11440 for (i = 0; i < enab->dten_ndesc; i++) { 11441 dtrace_actdesc_t *act, *next; 11442 dtrace_predicate_t *pred; 11443 11444 ep = enab->dten_desc[i]; 11445 11446 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 11447 dtrace_predicate_release(pred, vstate); 11448 11449 for (act = ep->dted_action; act != NULL; act = next) { 11450 next = act->dtad_next; 11451 dtrace_actdesc_release(act, vstate); 11452 } 11453 11454 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 11455 } 11456 11457 if (enab->dten_desc != NULL) 11458 kmem_free(enab->dten_desc, 11459 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 11460 11461 /* 11462 * If this was a retained enabling, decrement the dts_nretained count 11463 * and take it off of the dtrace_retained list. 11464 */ 11465 if (enab->dten_prev != NULL || enab->dten_next != NULL || 11466 dtrace_retained == enab) { 11467 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11468 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 11469 enab->dten_vstate->dtvs_state->dts_nretained--; 11470 } 11471 11472 if (enab->dten_prev == NULL) { 11473 if (dtrace_retained == enab) { 11474 dtrace_retained = enab->dten_next; 11475 11476 if (dtrace_retained != NULL) 11477 dtrace_retained->dten_prev = NULL; 11478 } 11479 } else { 11480 ASSERT(enab != dtrace_retained); 11481 ASSERT(dtrace_retained != NULL); 11482 enab->dten_prev->dten_next = enab->dten_next; 11483 } 11484 11485 if (enab->dten_next != NULL) { 11486 ASSERT(dtrace_retained != NULL); 11487 enab->dten_next->dten_prev = enab->dten_prev; 11488 } 11489 11490 kmem_free(enab, sizeof (dtrace_enabling_t)); 11491} 11492 11493static int 11494dtrace_enabling_retain(dtrace_enabling_t *enab) 11495{ 11496 dtrace_state_t *state; 11497 11498 ASSERT(MUTEX_HELD(&dtrace_lock)); 11499 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11500 ASSERT(enab->dten_vstate != NULL); 11501 11502 state = enab->dten_vstate->dtvs_state; 11503 ASSERT(state != NULL); 11504 11505 /* 11506 * We only allow each state to retain dtrace_retain_max enablings. 11507 */ 11508 if (state->dts_nretained >= dtrace_retain_max) 11509 return (ENOSPC); 11510 11511 state->dts_nretained++; 11512 11513 if (dtrace_retained == NULL) { 11514 dtrace_retained = enab; 11515 return (0); 11516 } 11517 11518 enab->dten_next = dtrace_retained; 11519 dtrace_retained->dten_prev = enab; 11520 dtrace_retained = enab; 11521 11522 return (0); 11523} 11524 11525static int 11526dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 11527 dtrace_probedesc_t *create) 11528{ 11529 dtrace_enabling_t *new, *enab; 11530 int found = 0, err = ENOENT; 11531 11532 ASSERT(MUTEX_HELD(&dtrace_lock)); 11533 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 11534 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 11535 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 11536 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 11537 11538 new = dtrace_enabling_create(&state->dts_vstate); 11539 11540 /* 11541 * Iterate over all retained enablings, looking for enablings that 11542 * match the specified state. 11543 */ 11544 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11545 int i; 11546 11547 /* 11548 * dtvs_state can only be NULL for helper enablings -- and 11549 * helper enablings can't be retained. 11550 */ 11551 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11552 11553 if (enab->dten_vstate->dtvs_state != state) 11554 continue; 11555 11556 /* 11557 * Now iterate over each probe description; we're looking for 11558 * an exact match to the specified probe description. 11559 */ 11560 for (i = 0; i < enab->dten_ndesc; i++) { 11561 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11562 dtrace_probedesc_t *pd = &ep->dted_probe; 11563 11564 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 11565 continue; 11566 11567 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 11568 continue; 11569 11570 if (strcmp(pd->dtpd_func, match->dtpd_func)) 11571 continue; 11572 11573 if (strcmp(pd->dtpd_name, match->dtpd_name)) 11574 continue; 11575 11576 /* 11577 * We have a winning probe! Add it to our growing 11578 * enabling. 11579 */ 11580 found = 1; 11581 dtrace_enabling_addlike(new, ep, create); 11582 } 11583 } 11584 11585 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 11586 dtrace_enabling_destroy(new); 11587 return (err); 11588 } 11589 11590 return (0); 11591} 11592 11593static void 11594dtrace_enabling_retract(dtrace_state_t *state) 11595{ 11596 dtrace_enabling_t *enab, *next; 11597 11598 ASSERT(MUTEX_HELD(&dtrace_lock)); 11599 11600 /* 11601 * Iterate over all retained enablings, destroy the enablings retained 11602 * for the specified state. 11603 */ 11604 for (enab = dtrace_retained; enab != NULL; enab = next) { 11605 next = enab->dten_next; 11606 11607 /* 11608 * dtvs_state can only be NULL for helper enablings -- and 11609 * helper enablings can't be retained. 11610 */ 11611 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11612 11613 if (enab->dten_vstate->dtvs_state == state) { 11614 ASSERT(state->dts_nretained > 0); 11615 dtrace_enabling_destroy(enab); 11616 } 11617 } 11618 11619 ASSERT(state->dts_nretained == 0); 11620} 11621 11622static int 11623dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 11624{ 11625 int i = 0; 11626 int matched = 0; 11627 11628 ASSERT(MUTEX_HELD(&cpu_lock)); 11629 ASSERT(MUTEX_HELD(&dtrace_lock)); 11630 11631 for (i = 0; i < enab->dten_ndesc; i++) { 11632 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11633 11634 enab->dten_current = ep; 11635 enab->dten_error = 0; 11636 11637 matched += dtrace_probe_enable(&ep->dted_probe, enab); 11638 11639 if (enab->dten_error != 0) { 11640 /* 11641 * If we get an error half-way through enabling the 11642 * probes, we kick out -- perhaps with some number of 11643 * them enabled. Leaving enabled probes enabled may 11644 * be slightly confusing for user-level, but we expect 11645 * that no one will attempt to actually drive on in 11646 * the face of such errors. If this is an anonymous 11647 * enabling (indicated with a NULL nmatched pointer), 11648 * we cmn_err() a message. We aren't expecting to 11649 * get such an error -- such as it can exist at all, 11650 * it would be a result of corrupted DOF in the driver 11651 * properties. 11652 */ 11653 if (nmatched == NULL) { 11654 cmn_err(CE_WARN, "dtrace_enabling_match() " 11655 "error on %p: %d", (void *)ep, 11656 enab->dten_error); 11657 } 11658 11659 return (enab->dten_error); 11660 } 11661 } 11662 11663 enab->dten_probegen = dtrace_probegen; 11664 if (nmatched != NULL) 11665 *nmatched = matched; 11666 11667 return (0); 11668} 11669 11670static void 11671dtrace_enabling_matchall(void) 11672{ 11673 dtrace_enabling_t *enab; 11674 11675 mutex_enter(&cpu_lock); 11676 mutex_enter(&dtrace_lock); 11677 11678 /* 11679 * Iterate over all retained enablings to see if any probes match 11680 * against them. We only perform this operation on enablings for which 11681 * we have sufficient permissions by virtue of being in the global zone 11682 * or in the same zone as the DTrace client. Because we can be called 11683 * after dtrace_detach() has been called, we cannot assert that there 11684 * are retained enablings. We can safely load from dtrace_retained, 11685 * however: the taskq_destroy() at the end of dtrace_detach() will 11686 * block pending our completion. 11687 */ 11688 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11689#if defined(sun) 11690 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 11691 11692 if (INGLOBALZONE(curproc) || getzoneid() == crgetzoneid(cr)) 11693#endif 11694 (void) dtrace_enabling_match(enab, NULL); 11695 } 11696 11697 mutex_exit(&dtrace_lock); 11698 mutex_exit(&cpu_lock); 11699} 11700 11701/* 11702 * If an enabling is to be enabled without having matched probes (that is, if 11703 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 11704 * enabling must be _primed_ by creating an ECB for every ECB description. 11705 * This must be done to assure that we know the number of speculations, the 11706 * number of aggregations, the minimum buffer size needed, etc. before we 11707 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 11708 * enabling any probes, we create ECBs for every ECB decription, but with a 11709 * NULL probe -- which is exactly what this function does. 11710 */ 11711static void 11712dtrace_enabling_prime(dtrace_state_t *state) 11713{ 11714 dtrace_enabling_t *enab; 11715 int i; 11716 11717 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11718 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11719 11720 if (enab->dten_vstate->dtvs_state != state) 11721 continue; 11722 11723 /* 11724 * We don't want to prime an enabling more than once, lest 11725 * we allow a malicious user to induce resource exhaustion. 11726 * (The ECBs that result from priming an enabling aren't 11727 * leaked -- but they also aren't deallocated until the 11728 * consumer state is destroyed.) 11729 */ 11730 if (enab->dten_primed) 11731 continue; 11732 11733 for (i = 0; i < enab->dten_ndesc; i++) { 11734 enab->dten_current = enab->dten_desc[i]; 11735 (void) dtrace_probe_enable(NULL, enab); 11736 } 11737 11738 enab->dten_primed = 1; 11739 } 11740} 11741 11742/* 11743 * Called to indicate that probes should be provided due to retained 11744 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 11745 * must take an initial lap through the enabling calling the dtps_provide() 11746 * entry point explicitly to allow for autocreated probes. 11747 */ 11748static void 11749dtrace_enabling_provide(dtrace_provider_t *prv) 11750{ 11751 int i, all = 0; 11752 dtrace_probedesc_t desc; 11753 11754 ASSERT(MUTEX_HELD(&dtrace_lock)); 11755 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 11756 11757 if (prv == NULL) { 11758 all = 1; 11759 prv = dtrace_provider; 11760 } 11761 11762 do { 11763 dtrace_enabling_t *enab = dtrace_retained; 11764 void *parg = prv->dtpv_arg; 11765 11766 for (; enab != NULL; enab = enab->dten_next) { 11767 for (i = 0; i < enab->dten_ndesc; i++) { 11768 desc = enab->dten_desc[i]->dted_probe; 11769 mutex_exit(&dtrace_lock); 11770 prv->dtpv_pops.dtps_provide(parg, &desc); 11771 mutex_enter(&dtrace_lock); 11772 } 11773 } 11774 } while (all && (prv = prv->dtpv_next) != NULL); 11775 11776 mutex_exit(&dtrace_lock); 11777 dtrace_probe_provide(NULL, all ? NULL : prv); 11778 mutex_enter(&dtrace_lock); 11779} 11780 11781/* 11782 * Called to reap ECBs that are attached to probes from defunct providers. 11783 */ 11784static void 11785dtrace_enabling_reap(void) 11786{ 11787 dtrace_provider_t *prov; 11788 dtrace_probe_t *probe; 11789 dtrace_ecb_t *ecb; 11790 hrtime_t when; 11791 int i; 11792 11793 mutex_enter(&cpu_lock); 11794 mutex_enter(&dtrace_lock); 11795 11796 for (i = 0; i < dtrace_nprobes; i++) { 11797 if ((probe = dtrace_probes[i]) == NULL) 11798 continue; 11799 11800 if (probe->dtpr_ecb == NULL) 11801 continue; 11802 11803 prov = probe->dtpr_provider; 11804 11805 if ((when = prov->dtpv_defunct) == 0) 11806 continue; 11807 11808 /* 11809 * We have ECBs on a defunct provider: we want to reap these 11810 * ECBs to allow the provider to unregister. The destruction 11811 * of these ECBs must be done carefully: if we destroy the ECB 11812 * and the consumer later wishes to consume an EPID that 11813 * corresponds to the destroyed ECB (and if the EPID metadata 11814 * has not been previously consumed), the consumer will abort 11815 * processing on the unknown EPID. To reduce (but not, sadly, 11816 * eliminate) the possibility of this, we will only destroy an 11817 * ECB for a defunct provider if, for the state that 11818 * corresponds to the ECB: 11819 * 11820 * (a) There is no speculative tracing (which can effectively 11821 * cache an EPID for an arbitrary amount of time). 11822 * 11823 * (b) The principal buffers have been switched twice since the 11824 * provider became defunct. 11825 * 11826 * (c) The aggregation buffers are of zero size or have been 11827 * switched twice since the provider became defunct. 11828 * 11829 * We use dts_speculates to determine (a) and call a function 11830 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 11831 * that as soon as we've been unable to destroy one of the ECBs 11832 * associated with the probe, we quit trying -- reaping is only 11833 * fruitful in as much as we can destroy all ECBs associated 11834 * with the defunct provider's probes. 11835 */ 11836 while ((ecb = probe->dtpr_ecb) != NULL) { 11837 dtrace_state_t *state = ecb->dte_state; 11838 dtrace_buffer_t *buf = state->dts_buffer; 11839 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 11840 11841 if (state->dts_speculates) 11842 break; 11843 11844 if (!dtrace_buffer_consumed(buf, when)) 11845 break; 11846 11847 if (!dtrace_buffer_consumed(aggbuf, when)) 11848 break; 11849 11850 dtrace_ecb_disable(ecb); 11851 ASSERT(probe->dtpr_ecb != ecb); 11852 dtrace_ecb_destroy(ecb); 11853 } 11854 } 11855 11856 mutex_exit(&dtrace_lock); 11857 mutex_exit(&cpu_lock); 11858} 11859 11860/* 11861 * DTrace DOF Functions 11862 */ 11863/*ARGSUSED*/ 11864static void 11865dtrace_dof_error(dof_hdr_t *dof, const char *str) 11866{ 11867 if (dtrace_err_verbose) 11868 cmn_err(CE_WARN, "failed to process DOF: %s", str); 11869 11870#ifdef DTRACE_ERRDEBUG 11871 dtrace_errdebug(str); 11872#endif 11873} 11874 11875/* 11876 * Create DOF out of a currently enabled state. Right now, we only create 11877 * DOF containing the run-time options -- but this could be expanded to create 11878 * complete DOF representing the enabled state. 11879 */ 11880static dof_hdr_t * 11881dtrace_dof_create(dtrace_state_t *state) 11882{ 11883 dof_hdr_t *dof; 11884 dof_sec_t *sec; 11885 dof_optdesc_t *opt; 11886 int i, len = sizeof (dof_hdr_t) + 11887 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 11888 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11889 11890 ASSERT(MUTEX_HELD(&dtrace_lock)); 11891 11892 dof = kmem_zalloc(len, KM_SLEEP); 11893 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 11894 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 11895 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 11896 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 11897 11898 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 11899 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 11900 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 11901 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 11902 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 11903 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 11904 11905 dof->dofh_flags = 0; 11906 dof->dofh_hdrsize = sizeof (dof_hdr_t); 11907 dof->dofh_secsize = sizeof (dof_sec_t); 11908 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 11909 dof->dofh_secoff = sizeof (dof_hdr_t); 11910 dof->dofh_loadsz = len; 11911 dof->dofh_filesz = len; 11912 dof->dofh_pad = 0; 11913 11914 /* 11915 * Fill in the option section header... 11916 */ 11917 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 11918 sec->dofs_type = DOF_SECT_OPTDESC; 11919 sec->dofs_align = sizeof (uint64_t); 11920 sec->dofs_flags = DOF_SECF_LOAD; 11921 sec->dofs_entsize = sizeof (dof_optdesc_t); 11922 11923 opt = (dof_optdesc_t *)((uintptr_t)sec + 11924 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 11925 11926 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 11927 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11928 11929 for (i = 0; i < DTRACEOPT_MAX; i++) { 11930 opt[i].dofo_option = i; 11931 opt[i].dofo_strtab = DOF_SECIDX_NONE; 11932 opt[i].dofo_value = state->dts_options[i]; 11933 } 11934 11935 return (dof); 11936} 11937 11938static dof_hdr_t * 11939dtrace_dof_copyin(uintptr_t uarg, int *errp) 11940{ 11941 dof_hdr_t hdr, *dof; 11942 11943 ASSERT(!MUTEX_HELD(&dtrace_lock)); 11944 11945 /* 11946 * First, we're going to copyin() the sizeof (dof_hdr_t). 11947 */ 11948 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 11949 dtrace_dof_error(NULL, "failed to copyin DOF header"); 11950 *errp = EFAULT; 11951 return (NULL); 11952 } 11953 11954 /* 11955 * Now we'll allocate the entire DOF and copy it in -- provided 11956 * that the length isn't outrageous. 11957 */ 11958 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 11959 dtrace_dof_error(&hdr, "load size exceeds maximum"); 11960 *errp = E2BIG; 11961 return (NULL); 11962 } 11963 11964 if (hdr.dofh_loadsz < sizeof (hdr)) { 11965 dtrace_dof_error(&hdr, "invalid load size"); 11966 *errp = EINVAL; 11967 return (NULL); 11968 } 11969 11970 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 11971 11972 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) { 11973 kmem_free(dof, hdr.dofh_loadsz); 11974 *errp = EFAULT; 11975 return (NULL); 11976 } 11977 11978 return (dof); 11979} 11980 11981#if !defined(sun) 11982static __inline uchar_t 11983dtrace_dof_char(char c) { 11984 switch (c) { 11985 case '0': 11986 case '1': 11987 case '2': 11988 case '3': 11989 case '4': 11990 case '5': 11991 case '6': 11992 case '7': 11993 case '8': 11994 case '9': 11995 return (c - '0'); 11996 case 'A': 11997 case 'B': 11998 case 'C': 11999 case 'D': 12000 case 'E': 12001 case 'F': 12002 return (c - 'A' + 10); 12003 case 'a': 12004 case 'b': 12005 case 'c': 12006 case 'd': 12007 case 'e': 12008 case 'f': 12009 return (c - 'a' + 10); 12010 } 12011 /* Should not reach here. */ 12012 return (0); 12013} 12014#endif 12015 12016static dof_hdr_t * 12017dtrace_dof_property(const char *name) 12018{ 12019 uchar_t *buf; 12020 uint64_t loadsz; 12021 unsigned int len, i; 12022 dof_hdr_t *dof; 12023 12024#if defined(sun) 12025 /* 12026 * Unfortunately, array of values in .conf files are always (and 12027 * only) interpreted to be integer arrays. We must read our DOF 12028 * as an integer array, and then squeeze it into a byte array. 12029 */ 12030 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 12031 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 12032 return (NULL); 12033 12034 for (i = 0; i < len; i++) 12035 buf[i] = (uchar_t)(((int *)buf)[i]); 12036 12037 if (len < sizeof (dof_hdr_t)) { 12038 ddi_prop_free(buf); 12039 dtrace_dof_error(NULL, "truncated header"); 12040 return (NULL); 12041 } 12042 12043 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 12044 ddi_prop_free(buf); 12045 dtrace_dof_error(NULL, "truncated DOF"); 12046 return (NULL); 12047 } 12048 12049 if (loadsz >= dtrace_dof_maxsize) { 12050 ddi_prop_free(buf); 12051 dtrace_dof_error(NULL, "oversized DOF"); 12052 return (NULL); 12053 } 12054 12055 dof = kmem_alloc(loadsz, KM_SLEEP); 12056 bcopy(buf, dof, loadsz); 12057 ddi_prop_free(buf); 12058#else 12059 char *p; 12060 char *p_env; 12061 12062 if ((p_env = getenv(name)) == NULL) 12063 return (NULL); 12064 12065 len = strlen(p_env) / 2; 12066 12067 buf = kmem_alloc(len, KM_SLEEP); 12068 12069 dof = (dof_hdr_t *) buf; 12070 12071 p = p_env; 12072 12073 for (i = 0; i < len; i++) { 12074 buf[i] = (dtrace_dof_char(p[0]) << 4) | 12075 dtrace_dof_char(p[1]); 12076 p += 2; 12077 } 12078 12079 freeenv(p_env); 12080 12081 if (len < sizeof (dof_hdr_t)) { 12082 kmem_free(buf, 0); 12083 dtrace_dof_error(NULL, "truncated header"); 12084 return (NULL); 12085 } 12086 12087 if (len < (loadsz = dof->dofh_loadsz)) { 12088 kmem_free(buf, 0); 12089 dtrace_dof_error(NULL, "truncated DOF"); 12090 return (NULL); 12091 } 12092 12093 if (loadsz >= dtrace_dof_maxsize) { 12094 kmem_free(buf, 0); 12095 dtrace_dof_error(NULL, "oversized DOF"); 12096 return (NULL); 12097 } 12098#endif 12099 12100 return (dof); 12101} 12102 12103static void 12104dtrace_dof_destroy(dof_hdr_t *dof) 12105{ 12106 kmem_free(dof, dof->dofh_loadsz); 12107} 12108 12109/* 12110 * Return the dof_sec_t pointer corresponding to a given section index. If the 12111 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 12112 * a type other than DOF_SECT_NONE is specified, the header is checked against 12113 * this type and NULL is returned if the types do not match. 12114 */ 12115static dof_sec_t * 12116dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 12117{ 12118 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 12119 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 12120 12121 if (i >= dof->dofh_secnum) { 12122 dtrace_dof_error(dof, "referenced section index is invalid"); 12123 return (NULL); 12124 } 12125 12126 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 12127 dtrace_dof_error(dof, "referenced section is not loadable"); 12128 return (NULL); 12129 } 12130 12131 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 12132 dtrace_dof_error(dof, "referenced section is the wrong type"); 12133 return (NULL); 12134 } 12135 12136 return (sec); 12137} 12138 12139static dtrace_probedesc_t * 12140dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 12141{ 12142 dof_probedesc_t *probe; 12143 dof_sec_t *strtab; 12144 uintptr_t daddr = (uintptr_t)dof; 12145 uintptr_t str; 12146 size_t size; 12147 12148 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 12149 dtrace_dof_error(dof, "invalid probe section"); 12150 return (NULL); 12151 } 12152 12153 if (sec->dofs_align != sizeof (dof_secidx_t)) { 12154 dtrace_dof_error(dof, "bad alignment in probe description"); 12155 return (NULL); 12156 } 12157 12158 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 12159 dtrace_dof_error(dof, "truncated probe description"); 12160 return (NULL); 12161 } 12162 12163 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 12164 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 12165 12166 if (strtab == NULL) 12167 return (NULL); 12168 12169 str = daddr + strtab->dofs_offset; 12170 size = strtab->dofs_size; 12171 12172 if (probe->dofp_provider >= strtab->dofs_size) { 12173 dtrace_dof_error(dof, "corrupt probe provider"); 12174 return (NULL); 12175 } 12176 12177 (void) strncpy(desc->dtpd_provider, 12178 (char *)(str + probe->dofp_provider), 12179 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 12180 12181 if (probe->dofp_mod >= strtab->dofs_size) { 12182 dtrace_dof_error(dof, "corrupt probe module"); 12183 return (NULL); 12184 } 12185 12186 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 12187 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 12188 12189 if (probe->dofp_func >= strtab->dofs_size) { 12190 dtrace_dof_error(dof, "corrupt probe function"); 12191 return (NULL); 12192 } 12193 12194 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 12195 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 12196 12197 if (probe->dofp_name >= strtab->dofs_size) { 12198 dtrace_dof_error(dof, "corrupt probe name"); 12199 return (NULL); 12200 } 12201 12202 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 12203 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 12204 12205 return (desc); 12206} 12207 12208static dtrace_difo_t * 12209dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12210 cred_t *cr) 12211{ 12212 dtrace_difo_t *dp; 12213 size_t ttl = 0; 12214 dof_difohdr_t *dofd; 12215 uintptr_t daddr = (uintptr_t)dof; 12216 size_t max = dtrace_difo_maxsize; 12217 int i, l, n; 12218 12219 static const struct { 12220 int section; 12221 int bufoffs; 12222 int lenoffs; 12223 int entsize; 12224 int align; 12225 const char *msg; 12226 } difo[] = { 12227 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 12228 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 12229 sizeof (dif_instr_t), "multiple DIF sections" }, 12230 12231 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 12232 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 12233 sizeof (uint64_t), "multiple integer tables" }, 12234 12235 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 12236 offsetof(dtrace_difo_t, dtdo_strlen), 0, 12237 sizeof (char), "multiple string tables" }, 12238 12239 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 12240 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 12241 sizeof (uint_t), "multiple variable tables" }, 12242 12243 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 12244 }; 12245 12246 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 12247 dtrace_dof_error(dof, "invalid DIFO header section"); 12248 return (NULL); 12249 } 12250 12251 if (sec->dofs_align != sizeof (dof_secidx_t)) { 12252 dtrace_dof_error(dof, "bad alignment in DIFO header"); 12253 return (NULL); 12254 } 12255 12256 if (sec->dofs_size < sizeof (dof_difohdr_t) || 12257 sec->dofs_size % sizeof (dof_secidx_t)) { 12258 dtrace_dof_error(dof, "bad size in DIFO header"); 12259 return (NULL); 12260 } 12261 12262 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12263 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 12264 12265 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 12266 dp->dtdo_rtype = dofd->dofd_rtype; 12267 12268 for (l = 0; l < n; l++) { 12269 dof_sec_t *subsec; 12270 void **bufp; 12271 uint32_t *lenp; 12272 12273 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 12274 dofd->dofd_links[l])) == NULL) 12275 goto err; /* invalid section link */ 12276 12277 if (ttl + subsec->dofs_size > max) { 12278 dtrace_dof_error(dof, "exceeds maximum size"); 12279 goto err; 12280 } 12281 12282 ttl += subsec->dofs_size; 12283 12284 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 12285 if (subsec->dofs_type != difo[i].section) 12286 continue; 12287 12288 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 12289 dtrace_dof_error(dof, "section not loaded"); 12290 goto err; 12291 } 12292 12293 if (subsec->dofs_align != difo[i].align) { 12294 dtrace_dof_error(dof, "bad alignment"); 12295 goto err; 12296 } 12297 12298 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 12299 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 12300 12301 if (*bufp != NULL) { 12302 dtrace_dof_error(dof, difo[i].msg); 12303 goto err; 12304 } 12305 12306 if (difo[i].entsize != subsec->dofs_entsize) { 12307 dtrace_dof_error(dof, "entry size mismatch"); 12308 goto err; 12309 } 12310 12311 if (subsec->dofs_entsize != 0 && 12312 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 12313 dtrace_dof_error(dof, "corrupt entry size"); 12314 goto err; 12315 } 12316 12317 *lenp = subsec->dofs_size; 12318 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 12319 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 12320 *bufp, subsec->dofs_size); 12321 12322 if (subsec->dofs_entsize != 0) 12323 *lenp /= subsec->dofs_entsize; 12324 12325 break; 12326 } 12327 12328 /* 12329 * If we encounter a loadable DIFO sub-section that is not 12330 * known to us, assume this is a broken program and fail. 12331 */ 12332 if (difo[i].section == DOF_SECT_NONE && 12333 (subsec->dofs_flags & DOF_SECF_LOAD)) { 12334 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 12335 goto err; 12336 } 12337 } 12338 12339 if (dp->dtdo_buf == NULL) { 12340 /* 12341 * We can't have a DIF object without DIF text. 12342 */ 12343 dtrace_dof_error(dof, "missing DIF text"); 12344 goto err; 12345 } 12346 12347 /* 12348 * Before we validate the DIF object, run through the variable table 12349 * looking for the strings -- if any of their size are under, we'll set 12350 * their size to be the system-wide default string size. Note that 12351 * this should _not_ happen if the "strsize" option has been set -- 12352 * in this case, the compiler should have set the size to reflect the 12353 * setting of the option. 12354 */ 12355 for (i = 0; i < dp->dtdo_varlen; i++) { 12356 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 12357 dtrace_diftype_t *t = &v->dtdv_type; 12358 12359 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 12360 continue; 12361 12362 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 12363 t->dtdt_size = dtrace_strsize_default; 12364 } 12365 12366 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 12367 goto err; 12368 12369 dtrace_difo_init(dp, vstate); 12370 return (dp); 12371 12372err: 12373 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 12374 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 12375 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 12376 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 12377 12378 kmem_free(dp, sizeof (dtrace_difo_t)); 12379 return (NULL); 12380} 12381 12382static dtrace_predicate_t * 12383dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12384 cred_t *cr) 12385{ 12386 dtrace_difo_t *dp; 12387 12388 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 12389 return (NULL); 12390 12391 return (dtrace_predicate_create(dp)); 12392} 12393 12394static dtrace_actdesc_t * 12395dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12396 cred_t *cr) 12397{ 12398 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 12399 dof_actdesc_t *desc; 12400 dof_sec_t *difosec; 12401 size_t offs; 12402 uintptr_t daddr = (uintptr_t)dof; 12403 uint64_t arg; 12404 dtrace_actkind_t kind; 12405 12406 if (sec->dofs_type != DOF_SECT_ACTDESC) { 12407 dtrace_dof_error(dof, "invalid action section"); 12408 return (NULL); 12409 } 12410 12411 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 12412 dtrace_dof_error(dof, "truncated action description"); 12413 return (NULL); 12414 } 12415 12416 if (sec->dofs_align != sizeof (uint64_t)) { 12417 dtrace_dof_error(dof, "bad alignment in action description"); 12418 return (NULL); 12419 } 12420 12421 if (sec->dofs_size < sec->dofs_entsize) { 12422 dtrace_dof_error(dof, "section entry size exceeds total size"); 12423 return (NULL); 12424 } 12425 12426 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 12427 dtrace_dof_error(dof, "bad entry size in action description"); 12428 return (NULL); 12429 } 12430 12431 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 12432 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 12433 return (NULL); 12434 } 12435 12436 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 12437 desc = (dof_actdesc_t *)(daddr + 12438 (uintptr_t)sec->dofs_offset + offs); 12439 kind = (dtrace_actkind_t)desc->dofa_kind; 12440 12441 if ((DTRACEACT_ISPRINTFLIKE(kind) && 12442 (kind != DTRACEACT_PRINTA || 12443 desc->dofa_strtab != DOF_SECIDX_NONE)) || 12444 (kind == DTRACEACT_DIFEXPR && 12445 desc->dofa_strtab != DOF_SECIDX_NONE)) { 12446 dof_sec_t *strtab; 12447 char *str, *fmt; 12448 uint64_t i; 12449 12450 /* 12451 * The argument to these actions is an index into the 12452 * DOF string table. For printf()-like actions, this 12453 * is the format string. For print(), this is the 12454 * CTF type of the expression result. 12455 */ 12456 if ((strtab = dtrace_dof_sect(dof, 12457 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 12458 goto err; 12459 12460 str = (char *)((uintptr_t)dof + 12461 (uintptr_t)strtab->dofs_offset); 12462 12463 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 12464 if (str[i] == '\0') 12465 break; 12466 } 12467 12468 if (i >= strtab->dofs_size) { 12469 dtrace_dof_error(dof, "bogus format string"); 12470 goto err; 12471 } 12472 12473 if (i == desc->dofa_arg) { 12474 dtrace_dof_error(dof, "empty format string"); 12475 goto err; 12476 } 12477 12478 i -= desc->dofa_arg; 12479 fmt = kmem_alloc(i + 1, KM_SLEEP); 12480 bcopy(&str[desc->dofa_arg], fmt, i + 1); 12481 arg = (uint64_t)(uintptr_t)fmt; 12482 } else { 12483 if (kind == DTRACEACT_PRINTA) { 12484 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 12485 arg = 0; 12486 } else { 12487 arg = desc->dofa_arg; 12488 } 12489 } 12490 12491 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 12492 desc->dofa_uarg, arg); 12493 12494 if (last != NULL) { 12495 last->dtad_next = act; 12496 } else { 12497 first = act; 12498 } 12499 12500 last = act; 12501 12502 if (desc->dofa_difo == DOF_SECIDX_NONE) 12503 continue; 12504 12505 if ((difosec = dtrace_dof_sect(dof, 12506 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 12507 goto err; 12508 12509 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 12510 12511 if (act->dtad_difo == NULL) 12512 goto err; 12513 } 12514 12515 ASSERT(first != NULL); 12516 return (first); 12517 12518err: 12519 for (act = first; act != NULL; act = next) { 12520 next = act->dtad_next; 12521 dtrace_actdesc_release(act, vstate); 12522 } 12523 12524 return (NULL); 12525} 12526 12527static dtrace_ecbdesc_t * 12528dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12529 cred_t *cr) 12530{ 12531 dtrace_ecbdesc_t *ep; 12532 dof_ecbdesc_t *ecb; 12533 dtrace_probedesc_t *desc; 12534 dtrace_predicate_t *pred = NULL; 12535 12536 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 12537 dtrace_dof_error(dof, "truncated ECB description"); 12538 return (NULL); 12539 } 12540 12541 if (sec->dofs_align != sizeof (uint64_t)) { 12542 dtrace_dof_error(dof, "bad alignment in ECB description"); 12543 return (NULL); 12544 } 12545 12546 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 12547 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 12548 12549 if (sec == NULL) 12550 return (NULL); 12551 12552 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12553 ep->dted_uarg = ecb->dofe_uarg; 12554 desc = &ep->dted_probe; 12555 12556 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 12557 goto err; 12558 12559 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 12560 if ((sec = dtrace_dof_sect(dof, 12561 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 12562 goto err; 12563 12564 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 12565 goto err; 12566 12567 ep->dted_pred.dtpdd_predicate = pred; 12568 } 12569 12570 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 12571 if ((sec = dtrace_dof_sect(dof, 12572 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 12573 goto err; 12574 12575 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 12576 12577 if (ep->dted_action == NULL) 12578 goto err; 12579 } 12580 12581 return (ep); 12582 12583err: 12584 if (pred != NULL) 12585 dtrace_predicate_release(pred, vstate); 12586 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12587 return (NULL); 12588} 12589 12590/* 12591 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 12592 * specified DOF. At present, this amounts to simply adding 'ubase' to the 12593 * site of any user SETX relocations to account for load object base address. 12594 * In the future, if we need other relocations, this function can be extended. 12595 */ 12596static int 12597dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 12598{ 12599 uintptr_t daddr = (uintptr_t)dof; 12600 dof_relohdr_t *dofr = 12601 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12602 dof_sec_t *ss, *rs, *ts; 12603 dof_relodesc_t *r; 12604 uint_t i, n; 12605 12606 if (sec->dofs_size < sizeof (dof_relohdr_t) || 12607 sec->dofs_align != sizeof (dof_secidx_t)) { 12608 dtrace_dof_error(dof, "invalid relocation header"); 12609 return (-1); 12610 } 12611 12612 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 12613 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 12614 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 12615 12616 if (ss == NULL || rs == NULL || ts == NULL) 12617 return (-1); /* dtrace_dof_error() has been called already */ 12618 12619 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 12620 rs->dofs_align != sizeof (uint64_t)) { 12621 dtrace_dof_error(dof, "invalid relocation section"); 12622 return (-1); 12623 } 12624 12625 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 12626 n = rs->dofs_size / rs->dofs_entsize; 12627 12628 for (i = 0; i < n; i++) { 12629 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 12630 12631 switch (r->dofr_type) { 12632 case DOF_RELO_NONE: 12633 break; 12634 case DOF_RELO_SETX: 12635 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 12636 sizeof (uint64_t) > ts->dofs_size) { 12637 dtrace_dof_error(dof, "bad relocation offset"); 12638 return (-1); 12639 } 12640 12641 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 12642 dtrace_dof_error(dof, "misaligned setx relo"); 12643 return (-1); 12644 } 12645 12646 *(uint64_t *)taddr += ubase; 12647 break; 12648 default: 12649 dtrace_dof_error(dof, "invalid relocation type"); 12650 return (-1); 12651 } 12652 12653 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 12654 } 12655 12656 return (0); 12657} 12658 12659/* 12660 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 12661 * header: it should be at the front of a memory region that is at least 12662 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 12663 * size. It need not be validated in any other way. 12664 */ 12665static int 12666dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 12667 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 12668{ 12669 uint64_t len = dof->dofh_loadsz, seclen; 12670 uintptr_t daddr = (uintptr_t)dof; 12671 dtrace_ecbdesc_t *ep; 12672 dtrace_enabling_t *enab; 12673 uint_t i; 12674 12675 ASSERT(MUTEX_HELD(&dtrace_lock)); 12676 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 12677 12678 /* 12679 * Check the DOF header identification bytes. In addition to checking 12680 * valid settings, we also verify that unused bits/bytes are zeroed so 12681 * we can use them later without fear of regressing existing binaries. 12682 */ 12683 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 12684 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 12685 dtrace_dof_error(dof, "DOF magic string mismatch"); 12686 return (-1); 12687 } 12688 12689 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 12690 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 12691 dtrace_dof_error(dof, "DOF has invalid data model"); 12692 return (-1); 12693 } 12694 12695 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 12696 dtrace_dof_error(dof, "DOF encoding mismatch"); 12697 return (-1); 12698 } 12699 12700 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 12701 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 12702 dtrace_dof_error(dof, "DOF version mismatch"); 12703 return (-1); 12704 } 12705 12706 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 12707 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 12708 return (-1); 12709 } 12710 12711 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 12712 dtrace_dof_error(dof, "DOF uses too many integer registers"); 12713 return (-1); 12714 } 12715 12716 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 12717 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 12718 return (-1); 12719 } 12720 12721 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 12722 if (dof->dofh_ident[i] != 0) { 12723 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 12724 return (-1); 12725 } 12726 } 12727 12728 if (dof->dofh_flags & ~DOF_FL_VALID) { 12729 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 12730 return (-1); 12731 } 12732 12733 if (dof->dofh_secsize == 0) { 12734 dtrace_dof_error(dof, "zero section header size"); 12735 return (-1); 12736 } 12737 12738 /* 12739 * Check that the section headers don't exceed the amount of DOF 12740 * data. Note that we cast the section size and number of sections 12741 * to uint64_t's to prevent possible overflow in the multiplication. 12742 */ 12743 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 12744 12745 if (dof->dofh_secoff > len || seclen > len || 12746 dof->dofh_secoff + seclen > len) { 12747 dtrace_dof_error(dof, "truncated section headers"); 12748 return (-1); 12749 } 12750 12751 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 12752 dtrace_dof_error(dof, "misaligned section headers"); 12753 return (-1); 12754 } 12755 12756 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 12757 dtrace_dof_error(dof, "misaligned section size"); 12758 return (-1); 12759 } 12760 12761 /* 12762 * Take an initial pass through the section headers to be sure that 12763 * the headers don't have stray offsets. If the 'noprobes' flag is 12764 * set, do not permit sections relating to providers, probes, or args. 12765 */ 12766 for (i = 0; i < dof->dofh_secnum; i++) { 12767 dof_sec_t *sec = (dof_sec_t *)(daddr + 12768 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12769 12770 if (noprobes) { 12771 switch (sec->dofs_type) { 12772 case DOF_SECT_PROVIDER: 12773 case DOF_SECT_PROBES: 12774 case DOF_SECT_PRARGS: 12775 case DOF_SECT_PROFFS: 12776 dtrace_dof_error(dof, "illegal sections " 12777 "for enabling"); 12778 return (-1); 12779 } 12780 } 12781 12782 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12783 continue; /* just ignore non-loadable sections */ 12784 12785 if (sec->dofs_align & (sec->dofs_align - 1)) { 12786 dtrace_dof_error(dof, "bad section alignment"); 12787 return (-1); 12788 } 12789 12790 if (sec->dofs_offset & (sec->dofs_align - 1)) { 12791 dtrace_dof_error(dof, "misaligned section"); 12792 return (-1); 12793 } 12794 12795 if (sec->dofs_offset > len || sec->dofs_size > len || 12796 sec->dofs_offset + sec->dofs_size > len) { 12797 dtrace_dof_error(dof, "corrupt section header"); 12798 return (-1); 12799 } 12800 12801 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 12802 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 12803 dtrace_dof_error(dof, "non-terminating string table"); 12804 return (-1); 12805 } 12806 } 12807 12808 /* 12809 * Take a second pass through the sections and locate and perform any 12810 * relocations that are present. We do this after the first pass to 12811 * be sure that all sections have had their headers validated. 12812 */ 12813 for (i = 0; i < dof->dofh_secnum; i++) { 12814 dof_sec_t *sec = (dof_sec_t *)(daddr + 12815 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12816 12817 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12818 continue; /* skip sections that are not loadable */ 12819 12820 switch (sec->dofs_type) { 12821 case DOF_SECT_URELHDR: 12822 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 12823 return (-1); 12824 break; 12825 } 12826 } 12827 12828 if ((enab = *enabp) == NULL) 12829 enab = *enabp = dtrace_enabling_create(vstate); 12830 12831 for (i = 0; i < dof->dofh_secnum; i++) { 12832 dof_sec_t *sec = (dof_sec_t *)(daddr + 12833 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12834 12835 if (sec->dofs_type != DOF_SECT_ECBDESC) 12836 continue; 12837 12838 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 12839 dtrace_enabling_destroy(enab); 12840 *enabp = NULL; 12841 return (-1); 12842 } 12843 12844 dtrace_enabling_add(enab, ep); 12845 } 12846 12847 return (0); 12848} 12849 12850/* 12851 * Process DOF for any options. This routine assumes that the DOF has been 12852 * at least processed by dtrace_dof_slurp(). 12853 */ 12854static int 12855dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 12856{ 12857 int i, rval; 12858 uint32_t entsize; 12859 size_t offs; 12860 dof_optdesc_t *desc; 12861 12862 for (i = 0; i < dof->dofh_secnum; i++) { 12863 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 12864 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12865 12866 if (sec->dofs_type != DOF_SECT_OPTDESC) 12867 continue; 12868 12869 if (sec->dofs_align != sizeof (uint64_t)) { 12870 dtrace_dof_error(dof, "bad alignment in " 12871 "option description"); 12872 return (EINVAL); 12873 } 12874 12875 if ((entsize = sec->dofs_entsize) == 0) { 12876 dtrace_dof_error(dof, "zeroed option entry size"); 12877 return (EINVAL); 12878 } 12879 12880 if (entsize < sizeof (dof_optdesc_t)) { 12881 dtrace_dof_error(dof, "bad option entry size"); 12882 return (EINVAL); 12883 } 12884 12885 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 12886 desc = (dof_optdesc_t *)((uintptr_t)dof + 12887 (uintptr_t)sec->dofs_offset + offs); 12888 12889 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 12890 dtrace_dof_error(dof, "non-zero option string"); 12891 return (EINVAL); 12892 } 12893 12894 if (desc->dofo_value == DTRACEOPT_UNSET) { 12895 dtrace_dof_error(dof, "unset option"); 12896 return (EINVAL); 12897 } 12898 12899 if ((rval = dtrace_state_option(state, 12900 desc->dofo_option, desc->dofo_value)) != 0) { 12901 dtrace_dof_error(dof, "rejected option"); 12902 return (rval); 12903 } 12904 } 12905 } 12906 12907 return (0); 12908} 12909 12910/* 12911 * DTrace Consumer State Functions 12912 */ 12913static int 12914dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 12915{ 12916 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 12917 void *base; 12918 uintptr_t limit; 12919 dtrace_dynvar_t *dvar, *next, *start; 12920 int i; 12921 12922 ASSERT(MUTEX_HELD(&dtrace_lock)); 12923 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 12924 12925 bzero(dstate, sizeof (dtrace_dstate_t)); 12926 12927 if ((dstate->dtds_chunksize = chunksize) == 0) 12928 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 12929 12930 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 12931 size = min; 12932 12933 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12934 return (ENOMEM); 12935 12936 dstate->dtds_size = size; 12937 dstate->dtds_base = base; 12938 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 12939 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 12940 12941 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 12942 12943 if (hashsize != 1 && (hashsize & 1)) 12944 hashsize--; 12945 12946 dstate->dtds_hashsize = hashsize; 12947 dstate->dtds_hash = dstate->dtds_base; 12948 12949 /* 12950 * Set all of our hash buckets to point to the single sink, and (if 12951 * it hasn't already been set), set the sink's hash value to be the 12952 * sink sentinel value. The sink is needed for dynamic variable 12953 * lookups to know that they have iterated over an entire, valid hash 12954 * chain. 12955 */ 12956 for (i = 0; i < hashsize; i++) 12957 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 12958 12959 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 12960 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 12961 12962 /* 12963 * Determine number of active CPUs. Divide free list evenly among 12964 * active CPUs. 12965 */ 12966 start = (dtrace_dynvar_t *) 12967 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 12968 limit = (uintptr_t)base + size; 12969 12970 maxper = (limit - (uintptr_t)start) / NCPU; 12971 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 12972 12973#if !defined(sun) 12974 CPU_FOREACH(i) { 12975#else 12976 for (i = 0; i < NCPU; i++) { 12977#endif 12978 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 12979 12980 /* 12981 * If we don't even have enough chunks to make it once through 12982 * NCPUs, we're just going to allocate everything to the first 12983 * CPU. And if we're on the last CPU, we're going to allocate 12984 * whatever is left over. In either case, we set the limit to 12985 * be the limit of the dynamic variable space. 12986 */ 12987 if (maxper == 0 || i == NCPU - 1) { 12988 limit = (uintptr_t)base + size; 12989 start = NULL; 12990 } else { 12991 limit = (uintptr_t)start + maxper; 12992 start = (dtrace_dynvar_t *)limit; 12993 } 12994 12995 ASSERT(limit <= (uintptr_t)base + size); 12996 12997 for (;;) { 12998 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 12999 dstate->dtds_chunksize); 13000 13001 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 13002 break; 13003 13004 dvar->dtdv_next = next; 13005 dvar = next; 13006 } 13007 13008 if (maxper == 0) 13009 break; 13010 } 13011 13012 return (0); 13013} 13014 13015static void 13016dtrace_dstate_fini(dtrace_dstate_t *dstate) 13017{ 13018 ASSERT(MUTEX_HELD(&cpu_lock)); 13019 13020 if (dstate->dtds_base == NULL) 13021 return; 13022 13023 kmem_free(dstate->dtds_base, dstate->dtds_size); 13024 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 13025} 13026 13027static void 13028dtrace_vstate_fini(dtrace_vstate_t *vstate) 13029{ 13030 /* 13031 * Logical XOR, where are you? 13032 */ 13033 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 13034 13035 if (vstate->dtvs_nglobals > 0) { 13036 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 13037 sizeof (dtrace_statvar_t *)); 13038 } 13039 13040 if (vstate->dtvs_ntlocals > 0) { 13041 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 13042 sizeof (dtrace_difv_t)); 13043 } 13044 13045 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 13046 13047 if (vstate->dtvs_nlocals > 0) { 13048 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 13049 sizeof (dtrace_statvar_t *)); 13050 } 13051} 13052 13053#if defined(sun) 13054static void 13055dtrace_state_clean(dtrace_state_t *state) 13056{ 13057 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 13058 return; 13059 13060 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 13061 dtrace_speculation_clean(state); 13062} 13063 13064static void 13065dtrace_state_deadman(dtrace_state_t *state) 13066{ 13067 hrtime_t now; 13068 13069 dtrace_sync(); 13070 13071 now = dtrace_gethrtime(); 13072 13073 if (state != dtrace_anon.dta_state && 13074 now - state->dts_laststatus >= dtrace_deadman_user) 13075 return; 13076 13077 /* 13078 * We must be sure that dts_alive never appears to be less than the 13079 * value upon entry to dtrace_state_deadman(), and because we lack a 13080 * dtrace_cas64(), we cannot store to it atomically. We thus instead 13081 * store INT64_MAX to it, followed by a memory barrier, followed by 13082 * the new value. This assures that dts_alive never appears to be 13083 * less than its true value, regardless of the order in which the 13084 * stores to the underlying storage are issued. 13085 */ 13086 state->dts_alive = INT64_MAX; 13087 dtrace_membar_producer(); 13088 state->dts_alive = now; 13089} 13090#else 13091static void 13092dtrace_state_clean(void *arg) 13093{ 13094 dtrace_state_t *state = arg; 13095 dtrace_optval_t *opt = state->dts_options; 13096 13097 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 13098 return; 13099 13100 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 13101 dtrace_speculation_clean(state); 13102 13103 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 13104 dtrace_state_clean, state); 13105} 13106 13107static void 13108dtrace_state_deadman(void *arg) 13109{ 13110 dtrace_state_t *state = arg; 13111 hrtime_t now; 13112 13113 dtrace_sync(); 13114 13115 dtrace_debug_output(); 13116 13117 now = dtrace_gethrtime(); 13118 13119 if (state != dtrace_anon.dta_state && 13120 now - state->dts_laststatus >= dtrace_deadman_user) 13121 return; 13122 13123 /* 13124 * We must be sure that dts_alive never appears to be less than the 13125 * value upon entry to dtrace_state_deadman(), and because we lack a 13126 * dtrace_cas64(), we cannot store to it atomically. We thus instead 13127 * store INT64_MAX to it, followed by a memory barrier, followed by 13128 * the new value. This assures that dts_alive never appears to be 13129 * less than its true value, regardless of the order in which the 13130 * stores to the underlying storage are issued. 13131 */ 13132 state->dts_alive = INT64_MAX; 13133 dtrace_membar_producer(); 13134 state->dts_alive = now; 13135 13136 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 13137 dtrace_state_deadman, state); 13138} 13139#endif 13140 13141static dtrace_state_t * 13142#if defined(sun) 13143dtrace_state_create(dev_t *devp, cred_t *cr) 13144#else 13145dtrace_state_create(struct cdev *dev) 13146#endif 13147{ 13148#if defined(sun) 13149 minor_t minor; 13150 major_t major; 13151#else 13152 cred_t *cr = NULL; 13153 int m = 0; 13154#endif 13155 char c[30]; 13156 dtrace_state_t *state; 13157 dtrace_optval_t *opt; 13158 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 13159 13160 ASSERT(MUTEX_HELD(&dtrace_lock)); 13161 ASSERT(MUTEX_HELD(&cpu_lock)); 13162 13163#if defined(sun) 13164 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 13165 VM_BESTFIT | VM_SLEEP); 13166 13167 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 13168 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 13169 return (NULL); 13170 } 13171 13172 state = ddi_get_soft_state(dtrace_softstate, minor); 13173#else 13174 if (dev != NULL) { 13175 cr = dev->si_cred; 13176 m = dev2unit(dev); 13177 } 13178 13179 /* Allocate memory for the state. */ 13180 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 13181#endif 13182 13183 state->dts_epid = DTRACE_EPIDNONE + 1; 13184 13185 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 13186#if defined(sun) 13187 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 13188 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 13189 13190 if (devp != NULL) { 13191 major = getemajor(*devp); 13192 } else { 13193 major = ddi_driver_major(dtrace_devi); 13194 } 13195 13196 state->dts_dev = makedevice(major, minor); 13197 13198 if (devp != NULL) 13199 *devp = state->dts_dev; 13200#else 13201 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 13202 state->dts_dev = dev; 13203#endif 13204 13205 /* 13206 * We allocate NCPU buffers. On the one hand, this can be quite 13207 * a bit of memory per instance (nearly 36K on a Starcat). On the 13208 * other hand, it saves an additional memory reference in the probe 13209 * path. 13210 */ 13211 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 13212 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 13213 13214#if defined(sun) 13215 state->dts_cleaner = CYCLIC_NONE; 13216 state->dts_deadman = CYCLIC_NONE; 13217#else 13218 callout_init(&state->dts_cleaner, CALLOUT_MPSAFE); 13219 callout_init(&state->dts_deadman, CALLOUT_MPSAFE); 13220#endif 13221 state->dts_vstate.dtvs_state = state; 13222 13223 for (i = 0; i < DTRACEOPT_MAX; i++) 13224 state->dts_options[i] = DTRACEOPT_UNSET; 13225 13226 /* 13227 * Set the default options. 13228 */ 13229 opt = state->dts_options; 13230 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 13231 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 13232 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 13233 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 13234 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 13235 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 13236 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 13237 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 13238 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 13239 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 13240 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 13241 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 13242 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 13243 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 13244 13245 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 13246 13247 /* 13248 * Depending on the user credentials, we set flag bits which alter probe 13249 * visibility or the amount of destructiveness allowed. In the case of 13250 * actual anonymous tracing, or the possession of all privileges, all of 13251 * the normal checks are bypassed. 13252 */ 13253 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 13254 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 13255 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 13256 } else { 13257 /* 13258 * Set up the credentials for this instantiation. We take a 13259 * hold on the credential to prevent it from disappearing on 13260 * us; this in turn prevents the zone_t referenced by this 13261 * credential from disappearing. This means that we can 13262 * examine the credential and the zone from probe context. 13263 */ 13264 crhold(cr); 13265 state->dts_cred.dcr_cred = cr; 13266 13267 /* 13268 * CRA_PROC means "we have *some* privilege for dtrace" and 13269 * unlocks the use of variables like pid, zonename, etc. 13270 */ 13271 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 13272 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 13273 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 13274 } 13275 13276 /* 13277 * dtrace_user allows use of syscall and profile providers. 13278 * If the user also has proc_owner and/or proc_zone, we 13279 * extend the scope to include additional visibility and 13280 * destructive power. 13281 */ 13282 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 13283 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 13284 state->dts_cred.dcr_visible |= 13285 DTRACE_CRV_ALLPROC; 13286 13287 state->dts_cred.dcr_action |= 13288 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13289 } 13290 13291 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 13292 state->dts_cred.dcr_visible |= 13293 DTRACE_CRV_ALLZONE; 13294 13295 state->dts_cred.dcr_action |= 13296 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13297 } 13298 13299 /* 13300 * If we have all privs in whatever zone this is, 13301 * we can do destructive things to processes which 13302 * have altered credentials. 13303 */ 13304#if defined(sun) 13305 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 13306 cr->cr_zone->zone_privset)) { 13307 state->dts_cred.dcr_action |= 13308 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 13309 } 13310#endif 13311 } 13312 13313 /* 13314 * Holding the dtrace_kernel privilege also implies that 13315 * the user has the dtrace_user privilege from a visibility 13316 * perspective. But without further privileges, some 13317 * destructive actions are not available. 13318 */ 13319 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 13320 /* 13321 * Make all probes in all zones visible. However, 13322 * this doesn't mean that all actions become available 13323 * to all zones. 13324 */ 13325 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 13326 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 13327 13328 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 13329 DTRACE_CRA_PROC; 13330 /* 13331 * Holding proc_owner means that destructive actions 13332 * for *this* zone are allowed. 13333 */ 13334 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 13335 state->dts_cred.dcr_action |= 13336 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13337 13338 /* 13339 * Holding proc_zone means that destructive actions 13340 * for this user/group ID in all zones is allowed. 13341 */ 13342 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 13343 state->dts_cred.dcr_action |= 13344 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13345 13346#if defined(sun) 13347 /* 13348 * If we have all privs in whatever zone this is, 13349 * we can do destructive things to processes which 13350 * have altered credentials. 13351 */ 13352 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 13353 cr->cr_zone->zone_privset)) { 13354 state->dts_cred.dcr_action |= 13355 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 13356 } 13357#endif 13358 } 13359 13360 /* 13361 * Holding the dtrace_proc privilege gives control over fasttrap 13362 * and pid providers. We need to grant wider destructive 13363 * privileges in the event that the user has proc_owner and/or 13364 * proc_zone. 13365 */ 13366 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 13367 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 13368 state->dts_cred.dcr_action |= 13369 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13370 13371 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 13372 state->dts_cred.dcr_action |= 13373 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13374 } 13375 } 13376 13377 return (state); 13378} 13379 13380static int 13381dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 13382{ 13383 dtrace_optval_t *opt = state->dts_options, size; 13384 processorid_t cpu = 0;; 13385 int flags = 0, rval, factor, divisor = 1; 13386 13387 ASSERT(MUTEX_HELD(&dtrace_lock)); 13388 ASSERT(MUTEX_HELD(&cpu_lock)); 13389 ASSERT(which < DTRACEOPT_MAX); 13390 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 13391 (state == dtrace_anon.dta_state && 13392 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 13393 13394 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 13395 return (0); 13396 13397 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 13398 cpu = opt[DTRACEOPT_CPU]; 13399 13400 if (which == DTRACEOPT_SPECSIZE) 13401 flags |= DTRACEBUF_NOSWITCH; 13402 13403 if (which == DTRACEOPT_BUFSIZE) { 13404 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 13405 flags |= DTRACEBUF_RING; 13406 13407 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 13408 flags |= DTRACEBUF_FILL; 13409 13410 if (state != dtrace_anon.dta_state || 13411 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 13412 flags |= DTRACEBUF_INACTIVE; 13413 } 13414 13415 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 13416 /* 13417 * The size must be 8-byte aligned. If the size is not 8-byte 13418 * aligned, drop it down by the difference. 13419 */ 13420 if (size & (sizeof (uint64_t) - 1)) 13421 size -= size & (sizeof (uint64_t) - 1); 13422 13423 if (size < state->dts_reserve) { 13424 /* 13425 * Buffers always must be large enough to accommodate 13426 * their prereserved space. We return E2BIG instead 13427 * of ENOMEM in this case to allow for user-level 13428 * software to differentiate the cases. 13429 */ 13430 return (E2BIG); 13431 } 13432 13433 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 13434 13435 if (rval != ENOMEM) { 13436 opt[which] = size; 13437 return (rval); 13438 } 13439 13440 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13441 return (rval); 13442 13443 for (divisor = 2; divisor < factor; divisor <<= 1) 13444 continue; 13445 } 13446 13447 return (ENOMEM); 13448} 13449 13450static int 13451dtrace_state_buffers(dtrace_state_t *state) 13452{ 13453 dtrace_speculation_t *spec = state->dts_speculations; 13454 int rval, i; 13455 13456 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 13457 DTRACEOPT_BUFSIZE)) != 0) 13458 return (rval); 13459 13460 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 13461 DTRACEOPT_AGGSIZE)) != 0) 13462 return (rval); 13463 13464 for (i = 0; i < state->dts_nspeculations; i++) { 13465 if ((rval = dtrace_state_buffer(state, 13466 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 13467 return (rval); 13468 } 13469 13470 return (0); 13471} 13472 13473static void 13474dtrace_state_prereserve(dtrace_state_t *state) 13475{ 13476 dtrace_ecb_t *ecb; 13477 dtrace_probe_t *probe; 13478 13479 state->dts_reserve = 0; 13480 13481 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 13482 return; 13483 13484 /* 13485 * If our buffer policy is a "fill" buffer policy, we need to set the 13486 * prereserved space to be the space required by the END probes. 13487 */ 13488 probe = dtrace_probes[dtrace_probeid_end - 1]; 13489 ASSERT(probe != NULL); 13490 13491 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 13492 if (ecb->dte_state != state) 13493 continue; 13494 13495 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 13496 } 13497} 13498 13499static int 13500dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 13501{ 13502 dtrace_optval_t *opt = state->dts_options, sz, nspec; 13503 dtrace_speculation_t *spec; 13504 dtrace_buffer_t *buf; 13505#if defined(sun) 13506 cyc_handler_t hdlr; 13507 cyc_time_t when; 13508#endif 13509 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13510 dtrace_icookie_t cookie; 13511 13512 mutex_enter(&cpu_lock); 13513 mutex_enter(&dtrace_lock); 13514 13515 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13516 rval = EBUSY; 13517 goto out; 13518 } 13519 13520 /* 13521 * Before we can perform any checks, we must prime all of the 13522 * retained enablings that correspond to this state. 13523 */ 13524 dtrace_enabling_prime(state); 13525 13526 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 13527 rval = EACCES; 13528 goto out; 13529 } 13530 13531 dtrace_state_prereserve(state); 13532 13533 /* 13534 * Now we want to do is try to allocate our speculations. 13535 * We do not automatically resize the number of speculations; if 13536 * this fails, we will fail the operation. 13537 */ 13538 nspec = opt[DTRACEOPT_NSPEC]; 13539 ASSERT(nspec != DTRACEOPT_UNSET); 13540 13541 if (nspec > INT_MAX) { 13542 rval = ENOMEM; 13543 goto out; 13544 } 13545 13546 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 13547 KM_NOSLEEP | KM_NORMALPRI); 13548 13549 if (spec == NULL) { 13550 rval = ENOMEM; 13551 goto out; 13552 } 13553 13554 state->dts_speculations = spec; 13555 state->dts_nspeculations = (int)nspec; 13556 13557 for (i = 0; i < nspec; i++) { 13558 if ((buf = kmem_zalloc(bufsize, 13559 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 13560 rval = ENOMEM; 13561 goto err; 13562 } 13563 13564 spec[i].dtsp_buffer = buf; 13565 } 13566 13567 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 13568 if (dtrace_anon.dta_state == NULL) { 13569 rval = ENOENT; 13570 goto out; 13571 } 13572 13573 if (state->dts_necbs != 0) { 13574 rval = EALREADY; 13575 goto out; 13576 } 13577 13578 state->dts_anon = dtrace_anon_grab(); 13579 ASSERT(state->dts_anon != NULL); 13580 state = state->dts_anon; 13581 13582 /* 13583 * We want "grabanon" to be set in the grabbed state, so we'll 13584 * copy that option value from the grabbing state into the 13585 * grabbed state. 13586 */ 13587 state->dts_options[DTRACEOPT_GRABANON] = 13588 opt[DTRACEOPT_GRABANON]; 13589 13590 *cpu = dtrace_anon.dta_beganon; 13591 13592 /* 13593 * If the anonymous state is active (as it almost certainly 13594 * is if the anonymous enabling ultimately matched anything), 13595 * we don't allow any further option processing -- but we 13596 * don't return failure. 13597 */ 13598 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13599 goto out; 13600 } 13601 13602 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 13603 opt[DTRACEOPT_AGGSIZE] != 0) { 13604 if (state->dts_aggregations == NULL) { 13605 /* 13606 * We're not going to create an aggregation buffer 13607 * because we don't have any ECBs that contain 13608 * aggregations -- set this option to 0. 13609 */ 13610 opt[DTRACEOPT_AGGSIZE] = 0; 13611 } else { 13612 /* 13613 * If we have an aggregation buffer, we must also have 13614 * a buffer to use as scratch. 13615 */ 13616 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 13617 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 13618 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 13619 } 13620 } 13621 } 13622 13623 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 13624 opt[DTRACEOPT_SPECSIZE] != 0) { 13625 if (!state->dts_speculates) { 13626 /* 13627 * We're not going to create speculation buffers 13628 * because we don't have any ECBs that actually 13629 * speculate -- set the speculation size to 0. 13630 */ 13631 opt[DTRACEOPT_SPECSIZE] = 0; 13632 } 13633 } 13634 13635 /* 13636 * The bare minimum size for any buffer that we're actually going to 13637 * do anything to is sizeof (uint64_t). 13638 */ 13639 sz = sizeof (uint64_t); 13640 13641 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 13642 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 13643 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 13644 /* 13645 * A buffer size has been explicitly set to 0 (or to a size 13646 * that will be adjusted to 0) and we need the space -- we 13647 * need to return failure. We return ENOSPC to differentiate 13648 * it from failing to allocate a buffer due to failure to meet 13649 * the reserve (for which we return E2BIG). 13650 */ 13651 rval = ENOSPC; 13652 goto out; 13653 } 13654 13655 if ((rval = dtrace_state_buffers(state)) != 0) 13656 goto err; 13657 13658 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 13659 sz = dtrace_dstate_defsize; 13660 13661 do { 13662 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 13663 13664 if (rval == 0) 13665 break; 13666 13667 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13668 goto err; 13669 } while (sz >>= 1); 13670 13671 opt[DTRACEOPT_DYNVARSIZE] = sz; 13672 13673 if (rval != 0) 13674 goto err; 13675 13676 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 13677 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 13678 13679 if (opt[DTRACEOPT_CLEANRATE] == 0) 13680 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13681 13682 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 13683 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 13684 13685 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 13686 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13687 13688 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 13689#if defined(sun) 13690 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 13691 hdlr.cyh_arg = state; 13692 hdlr.cyh_level = CY_LOW_LEVEL; 13693 13694 when.cyt_when = 0; 13695 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 13696 13697 state->dts_cleaner = cyclic_add(&hdlr, &when); 13698 13699 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 13700 hdlr.cyh_arg = state; 13701 hdlr.cyh_level = CY_LOW_LEVEL; 13702 13703 when.cyt_when = 0; 13704 when.cyt_interval = dtrace_deadman_interval; 13705 13706 state->dts_deadman = cyclic_add(&hdlr, &when); 13707#else 13708 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 13709 dtrace_state_clean, state); 13710 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 13711 dtrace_state_deadman, state); 13712#endif 13713 13714 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 13715 13716 /* 13717 * Now it's time to actually fire the BEGIN probe. We need to disable 13718 * interrupts here both to record the CPU on which we fired the BEGIN 13719 * probe (the data from this CPU will be processed first at user 13720 * level) and to manually activate the buffer for this CPU. 13721 */ 13722 cookie = dtrace_interrupt_disable(); 13723 *cpu = curcpu; 13724 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 13725 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 13726 13727 dtrace_probe(dtrace_probeid_begin, 13728 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13729 dtrace_interrupt_enable(cookie); 13730 /* 13731 * We may have had an exit action from a BEGIN probe; only change our 13732 * state to ACTIVE if we're still in WARMUP. 13733 */ 13734 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 13735 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 13736 13737 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 13738 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 13739 13740 /* 13741 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 13742 * want each CPU to transition its principal buffer out of the 13743 * INACTIVE state. Doing this assures that no CPU will suddenly begin 13744 * processing an ECB halfway down a probe's ECB chain; all CPUs will 13745 * atomically transition from processing none of a state's ECBs to 13746 * processing all of them. 13747 */ 13748 dtrace_xcall(DTRACE_CPUALL, 13749 (dtrace_xcall_t)dtrace_buffer_activate, state); 13750 goto out; 13751 13752err: 13753 dtrace_buffer_free(state->dts_buffer); 13754 dtrace_buffer_free(state->dts_aggbuffer); 13755 13756 if ((nspec = state->dts_nspeculations) == 0) { 13757 ASSERT(state->dts_speculations == NULL); 13758 goto out; 13759 } 13760 13761 spec = state->dts_speculations; 13762 ASSERT(spec != NULL); 13763 13764 for (i = 0; i < state->dts_nspeculations; i++) { 13765 if ((buf = spec[i].dtsp_buffer) == NULL) 13766 break; 13767 13768 dtrace_buffer_free(buf); 13769 kmem_free(buf, bufsize); 13770 } 13771 13772 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13773 state->dts_nspeculations = 0; 13774 state->dts_speculations = NULL; 13775 13776out: 13777 mutex_exit(&dtrace_lock); 13778 mutex_exit(&cpu_lock); 13779 13780 return (rval); 13781} 13782 13783static int 13784dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 13785{ 13786 dtrace_icookie_t cookie; 13787 13788 ASSERT(MUTEX_HELD(&dtrace_lock)); 13789 13790 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 13791 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 13792 return (EINVAL); 13793 13794 /* 13795 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 13796 * to be sure that every CPU has seen it. See below for the details 13797 * on why this is done. 13798 */ 13799 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 13800 dtrace_sync(); 13801 13802 /* 13803 * By this point, it is impossible for any CPU to be still processing 13804 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 13805 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 13806 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 13807 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 13808 * iff we're in the END probe. 13809 */ 13810 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 13811 dtrace_sync(); 13812 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 13813 13814 /* 13815 * Finally, we can release the reserve and call the END probe. We 13816 * disable interrupts across calling the END probe to allow us to 13817 * return the CPU on which we actually called the END probe. This 13818 * allows user-land to be sure that this CPU's principal buffer is 13819 * processed last. 13820 */ 13821 state->dts_reserve = 0; 13822 13823 cookie = dtrace_interrupt_disable(); 13824 *cpu = curcpu; 13825 dtrace_probe(dtrace_probeid_end, 13826 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13827 dtrace_interrupt_enable(cookie); 13828 13829 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 13830 dtrace_sync(); 13831 13832 return (0); 13833} 13834 13835static int 13836dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 13837 dtrace_optval_t val) 13838{ 13839 ASSERT(MUTEX_HELD(&dtrace_lock)); 13840 13841 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13842 return (EBUSY); 13843 13844 if (option >= DTRACEOPT_MAX) 13845 return (EINVAL); 13846 13847 if (option != DTRACEOPT_CPU && val < 0) 13848 return (EINVAL); 13849 13850 switch (option) { 13851 case DTRACEOPT_DESTRUCTIVE: 13852 if (dtrace_destructive_disallow) 13853 return (EACCES); 13854 13855 state->dts_cred.dcr_destructive = 1; 13856 break; 13857 13858 case DTRACEOPT_BUFSIZE: 13859 case DTRACEOPT_DYNVARSIZE: 13860 case DTRACEOPT_AGGSIZE: 13861 case DTRACEOPT_SPECSIZE: 13862 case DTRACEOPT_STRSIZE: 13863 if (val < 0) 13864 return (EINVAL); 13865 13866 if (val >= LONG_MAX) { 13867 /* 13868 * If this is an otherwise negative value, set it to 13869 * the highest multiple of 128m less than LONG_MAX. 13870 * Technically, we're adjusting the size without 13871 * regard to the buffer resizing policy, but in fact, 13872 * this has no effect -- if we set the buffer size to 13873 * ~LONG_MAX and the buffer policy is ultimately set to 13874 * be "manual", the buffer allocation is guaranteed to 13875 * fail, if only because the allocation requires two 13876 * buffers. (We set the the size to the highest 13877 * multiple of 128m because it ensures that the size 13878 * will remain a multiple of a megabyte when 13879 * repeatedly halved -- all the way down to 15m.) 13880 */ 13881 val = LONG_MAX - (1 << 27) + 1; 13882 } 13883 } 13884 13885 state->dts_options[option] = val; 13886 13887 return (0); 13888} 13889 13890static void 13891dtrace_state_destroy(dtrace_state_t *state) 13892{ 13893 dtrace_ecb_t *ecb; 13894 dtrace_vstate_t *vstate = &state->dts_vstate; 13895#if defined(sun) 13896 minor_t minor = getminor(state->dts_dev); 13897#endif 13898 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13899 dtrace_speculation_t *spec = state->dts_speculations; 13900 int nspec = state->dts_nspeculations; 13901 uint32_t match; 13902 13903 ASSERT(MUTEX_HELD(&dtrace_lock)); 13904 ASSERT(MUTEX_HELD(&cpu_lock)); 13905 13906 /* 13907 * First, retract any retained enablings for this state. 13908 */ 13909 dtrace_enabling_retract(state); 13910 ASSERT(state->dts_nretained == 0); 13911 13912 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 13913 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 13914 /* 13915 * We have managed to come into dtrace_state_destroy() on a 13916 * hot enabling -- almost certainly because of a disorderly 13917 * shutdown of a consumer. (That is, a consumer that is 13918 * exiting without having called dtrace_stop().) In this case, 13919 * we're going to set our activity to be KILLED, and then 13920 * issue a sync to be sure that everyone is out of probe 13921 * context before we start blowing away ECBs. 13922 */ 13923 state->dts_activity = DTRACE_ACTIVITY_KILLED; 13924 dtrace_sync(); 13925 } 13926 13927 /* 13928 * Release the credential hold we took in dtrace_state_create(). 13929 */ 13930 if (state->dts_cred.dcr_cred != NULL) 13931 crfree(state->dts_cred.dcr_cred); 13932 13933 /* 13934 * Now we can safely disable and destroy any enabled probes. Because 13935 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 13936 * (especially if they're all enabled), we take two passes through the 13937 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 13938 * in the second we disable whatever is left over. 13939 */ 13940 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 13941 for (i = 0; i < state->dts_necbs; i++) { 13942 if ((ecb = state->dts_ecbs[i]) == NULL) 13943 continue; 13944 13945 if (match && ecb->dte_probe != NULL) { 13946 dtrace_probe_t *probe = ecb->dte_probe; 13947 dtrace_provider_t *prov = probe->dtpr_provider; 13948 13949 if (!(prov->dtpv_priv.dtpp_flags & match)) 13950 continue; 13951 } 13952 13953 dtrace_ecb_disable(ecb); 13954 dtrace_ecb_destroy(ecb); 13955 } 13956 13957 if (!match) 13958 break; 13959 } 13960 13961 /* 13962 * Before we free the buffers, perform one more sync to assure that 13963 * every CPU is out of probe context. 13964 */ 13965 dtrace_sync(); 13966 13967 dtrace_buffer_free(state->dts_buffer); 13968 dtrace_buffer_free(state->dts_aggbuffer); 13969 13970 for (i = 0; i < nspec; i++) 13971 dtrace_buffer_free(spec[i].dtsp_buffer); 13972 13973#if defined(sun) 13974 if (state->dts_cleaner != CYCLIC_NONE) 13975 cyclic_remove(state->dts_cleaner); 13976 13977 if (state->dts_deadman != CYCLIC_NONE) 13978 cyclic_remove(state->dts_deadman); 13979#else 13980 callout_stop(&state->dts_cleaner); 13981 callout_drain(&state->dts_cleaner); 13982 callout_stop(&state->dts_deadman); 13983 callout_drain(&state->dts_deadman); 13984#endif 13985 13986 dtrace_dstate_fini(&vstate->dtvs_dynvars); 13987 dtrace_vstate_fini(vstate); 13988 if (state->dts_ecbs != NULL) 13989 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 13990 13991 if (state->dts_aggregations != NULL) { 13992#ifdef DEBUG 13993 for (i = 0; i < state->dts_naggregations; i++) 13994 ASSERT(state->dts_aggregations[i] == NULL); 13995#endif 13996 ASSERT(state->dts_naggregations > 0); 13997 kmem_free(state->dts_aggregations, 13998 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 13999 } 14000 14001 kmem_free(state->dts_buffer, bufsize); 14002 kmem_free(state->dts_aggbuffer, bufsize); 14003 14004 for (i = 0; i < nspec; i++) 14005 kmem_free(spec[i].dtsp_buffer, bufsize); 14006 14007 if (spec != NULL) 14008 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 14009 14010 dtrace_format_destroy(state); 14011 14012 if (state->dts_aggid_arena != NULL) { 14013#if defined(sun) 14014 vmem_destroy(state->dts_aggid_arena); 14015#else 14016 delete_unrhdr(state->dts_aggid_arena); 14017#endif 14018 state->dts_aggid_arena = NULL; 14019 } 14020#if defined(sun) 14021 ddi_soft_state_free(dtrace_softstate, minor); 14022 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14023#endif 14024} 14025 14026/* 14027 * DTrace Anonymous Enabling Functions 14028 */ 14029static dtrace_state_t * 14030dtrace_anon_grab(void) 14031{ 14032 dtrace_state_t *state; 14033 14034 ASSERT(MUTEX_HELD(&dtrace_lock)); 14035 14036 if ((state = dtrace_anon.dta_state) == NULL) { 14037 ASSERT(dtrace_anon.dta_enabling == NULL); 14038 return (NULL); 14039 } 14040 14041 ASSERT(dtrace_anon.dta_enabling != NULL); 14042 ASSERT(dtrace_retained != NULL); 14043 14044 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 14045 dtrace_anon.dta_enabling = NULL; 14046 dtrace_anon.dta_state = NULL; 14047 14048 return (state); 14049} 14050 14051static void 14052dtrace_anon_property(void) 14053{ 14054 int i, rv; 14055 dtrace_state_t *state; 14056 dof_hdr_t *dof; 14057 char c[32]; /* enough for "dof-data-" + digits */ 14058 14059 ASSERT(MUTEX_HELD(&dtrace_lock)); 14060 ASSERT(MUTEX_HELD(&cpu_lock)); 14061 14062 for (i = 0; ; i++) { 14063 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 14064 14065 dtrace_err_verbose = 1; 14066 14067 if ((dof = dtrace_dof_property(c)) == NULL) { 14068 dtrace_err_verbose = 0; 14069 break; 14070 } 14071 14072#if defined(sun) 14073 /* 14074 * We want to create anonymous state, so we need to transition 14075 * the kernel debugger to indicate that DTrace is active. If 14076 * this fails (e.g. because the debugger has modified text in 14077 * some way), we won't continue with the processing. 14078 */ 14079 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 14080 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 14081 "enabling ignored."); 14082 dtrace_dof_destroy(dof); 14083 break; 14084 } 14085#endif 14086 14087 /* 14088 * If we haven't allocated an anonymous state, we'll do so now. 14089 */ 14090 if ((state = dtrace_anon.dta_state) == NULL) { 14091#if defined(sun) 14092 state = dtrace_state_create(NULL, NULL); 14093#else 14094 state = dtrace_state_create(NULL); 14095#endif 14096 dtrace_anon.dta_state = state; 14097 14098 if (state == NULL) { 14099 /* 14100 * This basically shouldn't happen: the only 14101 * failure mode from dtrace_state_create() is a 14102 * failure of ddi_soft_state_zalloc() that 14103 * itself should never happen. Still, the 14104 * interface allows for a failure mode, and 14105 * we want to fail as gracefully as possible: 14106 * we'll emit an error message and cease 14107 * processing anonymous state in this case. 14108 */ 14109 cmn_err(CE_WARN, "failed to create " 14110 "anonymous state"); 14111 dtrace_dof_destroy(dof); 14112 break; 14113 } 14114 } 14115 14116 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 14117 &dtrace_anon.dta_enabling, 0, B_TRUE); 14118 14119 if (rv == 0) 14120 rv = dtrace_dof_options(dof, state); 14121 14122 dtrace_err_verbose = 0; 14123 dtrace_dof_destroy(dof); 14124 14125 if (rv != 0) { 14126 /* 14127 * This is malformed DOF; chuck any anonymous state 14128 * that we created. 14129 */ 14130 ASSERT(dtrace_anon.dta_enabling == NULL); 14131 dtrace_state_destroy(state); 14132 dtrace_anon.dta_state = NULL; 14133 break; 14134 } 14135 14136 ASSERT(dtrace_anon.dta_enabling != NULL); 14137 } 14138 14139 if (dtrace_anon.dta_enabling != NULL) { 14140 int rval; 14141 14142 /* 14143 * dtrace_enabling_retain() can only fail because we are 14144 * trying to retain more enablings than are allowed -- but 14145 * we only have one anonymous enabling, and we are guaranteed 14146 * to be allowed at least one retained enabling; we assert 14147 * that dtrace_enabling_retain() returns success. 14148 */ 14149 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 14150 ASSERT(rval == 0); 14151 14152 dtrace_enabling_dump(dtrace_anon.dta_enabling); 14153 } 14154} 14155 14156/* 14157 * DTrace Helper Functions 14158 */ 14159static void 14160dtrace_helper_trace(dtrace_helper_action_t *helper, 14161 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 14162{ 14163 uint32_t size, next, nnext, i; 14164 dtrace_helptrace_t *ent; 14165 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 14166 14167 if (!dtrace_helptrace_enabled) 14168 return; 14169 14170 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 14171 14172 /* 14173 * What would a tracing framework be without its own tracing 14174 * framework? (Well, a hell of a lot simpler, for starters...) 14175 */ 14176 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 14177 sizeof (uint64_t) - sizeof (uint64_t); 14178 14179 /* 14180 * Iterate until we can allocate a slot in the trace buffer. 14181 */ 14182 do { 14183 next = dtrace_helptrace_next; 14184 14185 if (next + size < dtrace_helptrace_bufsize) { 14186 nnext = next + size; 14187 } else { 14188 nnext = size; 14189 } 14190 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 14191 14192 /* 14193 * We have our slot; fill it in. 14194 */ 14195 if (nnext == size) 14196 next = 0; 14197 14198 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 14199 ent->dtht_helper = helper; 14200 ent->dtht_where = where; 14201 ent->dtht_nlocals = vstate->dtvs_nlocals; 14202 14203 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 14204 mstate->dtms_fltoffs : -1; 14205 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 14206 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 14207 14208 for (i = 0; i < vstate->dtvs_nlocals; i++) { 14209 dtrace_statvar_t *svar; 14210 14211 if ((svar = vstate->dtvs_locals[i]) == NULL) 14212 continue; 14213 14214 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 14215 ent->dtht_locals[i] = 14216 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 14217 } 14218} 14219 14220static uint64_t 14221dtrace_helper(int which, dtrace_mstate_t *mstate, 14222 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 14223{ 14224 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 14225 uint64_t sarg0 = mstate->dtms_arg[0]; 14226 uint64_t sarg1 = mstate->dtms_arg[1]; 14227 uint64_t rval = 0; 14228 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 14229 dtrace_helper_action_t *helper; 14230 dtrace_vstate_t *vstate; 14231 dtrace_difo_t *pred; 14232 int i, trace = dtrace_helptrace_enabled; 14233 14234 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 14235 14236 if (helpers == NULL) 14237 return (0); 14238 14239 if ((helper = helpers->dthps_actions[which]) == NULL) 14240 return (0); 14241 14242 vstate = &helpers->dthps_vstate; 14243 mstate->dtms_arg[0] = arg0; 14244 mstate->dtms_arg[1] = arg1; 14245 14246 /* 14247 * Now iterate over each helper. If its predicate evaluates to 'true', 14248 * we'll call the corresponding actions. Note that the below calls 14249 * to dtrace_dif_emulate() may set faults in machine state. This is 14250 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 14251 * the stored DIF offset with its own (which is the desired behavior). 14252 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 14253 * from machine state; this is okay, too. 14254 */ 14255 for (; helper != NULL; helper = helper->dtha_next) { 14256 if ((pred = helper->dtha_predicate) != NULL) { 14257 if (trace) 14258 dtrace_helper_trace(helper, mstate, vstate, 0); 14259 14260 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 14261 goto next; 14262 14263 if (*flags & CPU_DTRACE_FAULT) 14264 goto err; 14265 } 14266 14267 for (i = 0; i < helper->dtha_nactions; i++) { 14268 if (trace) 14269 dtrace_helper_trace(helper, 14270 mstate, vstate, i + 1); 14271 14272 rval = dtrace_dif_emulate(helper->dtha_actions[i], 14273 mstate, vstate, state); 14274 14275 if (*flags & CPU_DTRACE_FAULT) 14276 goto err; 14277 } 14278 14279next: 14280 if (trace) 14281 dtrace_helper_trace(helper, mstate, vstate, 14282 DTRACE_HELPTRACE_NEXT); 14283 } 14284 14285 if (trace) 14286 dtrace_helper_trace(helper, mstate, vstate, 14287 DTRACE_HELPTRACE_DONE); 14288 14289 /* 14290 * Restore the arg0 that we saved upon entry. 14291 */ 14292 mstate->dtms_arg[0] = sarg0; 14293 mstate->dtms_arg[1] = sarg1; 14294 14295 return (rval); 14296 14297err: 14298 if (trace) 14299 dtrace_helper_trace(helper, mstate, vstate, 14300 DTRACE_HELPTRACE_ERR); 14301 14302 /* 14303 * Restore the arg0 that we saved upon entry. 14304 */ 14305 mstate->dtms_arg[0] = sarg0; 14306 mstate->dtms_arg[1] = sarg1; 14307 14308 return (0); 14309} 14310 14311static void 14312dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 14313 dtrace_vstate_t *vstate) 14314{ 14315 int i; 14316 14317 if (helper->dtha_predicate != NULL) 14318 dtrace_difo_release(helper->dtha_predicate, vstate); 14319 14320 for (i = 0; i < helper->dtha_nactions; i++) { 14321 ASSERT(helper->dtha_actions[i] != NULL); 14322 dtrace_difo_release(helper->dtha_actions[i], vstate); 14323 } 14324 14325 kmem_free(helper->dtha_actions, 14326 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 14327 kmem_free(helper, sizeof (dtrace_helper_action_t)); 14328} 14329 14330static int 14331dtrace_helper_destroygen(int gen) 14332{ 14333 proc_t *p = curproc; 14334 dtrace_helpers_t *help = p->p_dtrace_helpers; 14335 dtrace_vstate_t *vstate; 14336 int i; 14337 14338 ASSERT(MUTEX_HELD(&dtrace_lock)); 14339 14340 if (help == NULL || gen > help->dthps_generation) 14341 return (EINVAL); 14342 14343 vstate = &help->dthps_vstate; 14344 14345 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14346 dtrace_helper_action_t *last = NULL, *h, *next; 14347 14348 for (h = help->dthps_actions[i]; h != NULL; h = next) { 14349 next = h->dtha_next; 14350 14351 if (h->dtha_generation == gen) { 14352 if (last != NULL) { 14353 last->dtha_next = next; 14354 } else { 14355 help->dthps_actions[i] = next; 14356 } 14357 14358 dtrace_helper_action_destroy(h, vstate); 14359 } else { 14360 last = h; 14361 } 14362 } 14363 } 14364 14365 /* 14366 * Interate until we've cleared out all helper providers with the 14367 * given generation number. 14368 */ 14369 for (;;) { 14370 dtrace_helper_provider_t *prov; 14371 14372 /* 14373 * Look for a helper provider with the right generation. We 14374 * have to start back at the beginning of the list each time 14375 * because we drop dtrace_lock. It's unlikely that we'll make 14376 * more than two passes. 14377 */ 14378 for (i = 0; i < help->dthps_nprovs; i++) { 14379 prov = help->dthps_provs[i]; 14380 14381 if (prov->dthp_generation == gen) 14382 break; 14383 } 14384 14385 /* 14386 * If there were no matches, we're done. 14387 */ 14388 if (i == help->dthps_nprovs) 14389 break; 14390 14391 /* 14392 * Move the last helper provider into this slot. 14393 */ 14394 help->dthps_nprovs--; 14395 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 14396 help->dthps_provs[help->dthps_nprovs] = NULL; 14397 14398 mutex_exit(&dtrace_lock); 14399 14400 /* 14401 * If we have a meta provider, remove this helper provider. 14402 */ 14403 mutex_enter(&dtrace_meta_lock); 14404 if (dtrace_meta_pid != NULL) { 14405 ASSERT(dtrace_deferred_pid == NULL); 14406 dtrace_helper_provider_remove(&prov->dthp_prov, 14407 p->p_pid); 14408 } 14409 mutex_exit(&dtrace_meta_lock); 14410 14411 dtrace_helper_provider_destroy(prov); 14412 14413 mutex_enter(&dtrace_lock); 14414 } 14415 14416 return (0); 14417} 14418 14419static int 14420dtrace_helper_validate(dtrace_helper_action_t *helper) 14421{ 14422 int err = 0, i; 14423 dtrace_difo_t *dp; 14424 14425 if ((dp = helper->dtha_predicate) != NULL) 14426 err += dtrace_difo_validate_helper(dp); 14427 14428 for (i = 0; i < helper->dtha_nactions; i++) 14429 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 14430 14431 return (err == 0); 14432} 14433 14434static int 14435dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 14436{ 14437 dtrace_helpers_t *help; 14438 dtrace_helper_action_t *helper, *last; 14439 dtrace_actdesc_t *act; 14440 dtrace_vstate_t *vstate; 14441 dtrace_predicate_t *pred; 14442 int count = 0, nactions = 0, i; 14443 14444 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 14445 return (EINVAL); 14446 14447 help = curproc->p_dtrace_helpers; 14448 last = help->dthps_actions[which]; 14449 vstate = &help->dthps_vstate; 14450 14451 for (count = 0; last != NULL; last = last->dtha_next) { 14452 count++; 14453 if (last->dtha_next == NULL) 14454 break; 14455 } 14456 14457 /* 14458 * If we already have dtrace_helper_actions_max helper actions for this 14459 * helper action type, we'll refuse to add a new one. 14460 */ 14461 if (count >= dtrace_helper_actions_max) 14462 return (ENOSPC); 14463 14464 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 14465 helper->dtha_generation = help->dthps_generation; 14466 14467 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 14468 ASSERT(pred->dtp_difo != NULL); 14469 dtrace_difo_hold(pred->dtp_difo); 14470 helper->dtha_predicate = pred->dtp_difo; 14471 } 14472 14473 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 14474 if (act->dtad_kind != DTRACEACT_DIFEXPR) 14475 goto err; 14476 14477 if (act->dtad_difo == NULL) 14478 goto err; 14479 14480 nactions++; 14481 } 14482 14483 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 14484 (helper->dtha_nactions = nactions), KM_SLEEP); 14485 14486 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 14487 dtrace_difo_hold(act->dtad_difo); 14488 helper->dtha_actions[i++] = act->dtad_difo; 14489 } 14490 14491 if (!dtrace_helper_validate(helper)) 14492 goto err; 14493 14494 if (last == NULL) { 14495 help->dthps_actions[which] = helper; 14496 } else { 14497 last->dtha_next = helper; 14498 } 14499 14500 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 14501 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 14502 dtrace_helptrace_next = 0; 14503 } 14504 14505 return (0); 14506err: 14507 dtrace_helper_action_destroy(helper, vstate); 14508 return (EINVAL); 14509} 14510 14511static void 14512dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 14513 dof_helper_t *dofhp) 14514{ 14515 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 14516 14517 mutex_enter(&dtrace_meta_lock); 14518 mutex_enter(&dtrace_lock); 14519 14520 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 14521 /* 14522 * If the dtrace module is loaded but not attached, or if 14523 * there aren't isn't a meta provider registered to deal with 14524 * these provider descriptions, we need to postpone creating 14525 * the actual providers until later. 14526 */ 14527 14528 if (help->dthps_next == NULL && help->dthps_prev == NULL && 14529 dtrace_deferred_pid != help) { 14530 help->dthps_deferred = 1; 14531 help->dthps_pid = p->p_pid; 14532 help->dthps_next = dtrace_deferred_pid; 14533 help->dthps_prev = NULL; 14534 if (dtrace_deferred_pid != NULL) 14535 dtrace_deferred_pid->dthps_prev = help; 14536 dtrace_deferred_pid = help; 14537 } 14538 14539 mutex_exit(&dtrace_lock); 14540 14541 } else if (dofhp != NULL) { 14542 /* 14543 * If the dtrace module is loaded and we have a particular 14544 * helper provider description, pass that off to the 14545 * meta provider. 14546 */ 14547 14548 mutex_exit(&dtrace_lock); 14549 14550 dtrace_helper_provide(dofhp, p->p_pid); 14551 14552 } else { 14553 /* 14554 * Otherwise, just pass all the helper provider descriptions 14555 * off to the meta provider. 14556 */ 14557 14558 int i; 14559 mutex_exit(&dtrace_lock); 14560 14561 for (i = 0; i < help->dthps_nprovs; i++) { 14562 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 14563 p->p_pid); 14564 } 14565 } 14566 14567 mutex_exit(&dtrace_meta_lock); 14568} 14569 14570static int 14571dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 14572{ 14573 dtrace_helpers_t *help; 14574 dtrace_helper_provider_t *hprov, **tmp_provs; 14575 uint_t tmp_maxprovs, i; 14576 14577 ASSERT(MUTEX_HELD(&dtrace_lock)); 14578 14579 help = curproc->p_dtrace_helpers; 14580 ASSERT(help != NULL); 14581 14582 /* 14583 * If we already have dtrace_helper_providers_max helper providers, 14584 * we're refuse to add a new one. 14585 */ 14586 if (help->dthps_nprovs >= dtrace_helper_providers_max) 14587 return (ENOSPC); 14588 14589 /* 14590 * Check to make sure this isn't a duplicate. 14591 */ 14592 for (i = 0; i < help->dthps_nprovs; i++) { 14593 if (dofhp->dofhp_dof == 14594 help->dthps_provs[i]->dthp_prov.dofhp_dof) 14595 return (EALREADY); 14596 } 14597 14598 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 14599 hprov->dthp_prov = *dofhp; 14600 hprov->dthp_ref = 1; 14601 hprov->dthp_generation = gen; 14602 14603 /* 14604 * Allocate a bigger table for helper providers if it's already full. 14605 */ 14606 if (help->dthps_maxprovs == help->dthps_nprovs) { 14607 tmp_maxprovs = help->dthps_maxprovs; 14608 tmp_provs = help->dthps_provs; 14609 14610 if (help->dthps_maxprovs == 0) 14611 help->dthps_maxprovs = 2; 14612 else 14613 help->dthps_maxprovs *= 2; 14614 if (help->dthps_maxprovs > dtrace_helper_providers_max) 14615 help->dthps_maxprovs = dtrace_helper_providers_max; 14616 14617 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 14618 14619 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 14620 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14621 14622 if (tmp_provs != NULL) { 14623 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 14624 sizeof (dtrace_helper_provider_t *)); 14625 kmem_free(tmp_provs, tmp_maxprovs * 14626 sizeof (dtrace_helper_provider_t *)); 14627 } 14628 } 14629 14630 help->dthps_provs[help->dthps_nprovs] = hprov; 14631 help->dthps_nprovs++; 14632 14633 return (0); 14634} 14635 14636static void 14637dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 14638{ 14639 mutex_enter(&dtrace_lock); 14640 14641 if (--hprov->dthp_ref == 0) { 14642 dof_hdr_t *dof; 14643 mutex_exit(&dtrace_lock); 14644 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 14645 dtrace_dof_destroy(dof); 14646 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 14647 } else { 14648 mutex_exit(&dtrace_lock); 14649 } 14650} 14651 14652static int 14653dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 14654{ 14655 uintptr_t daddr = (uintptr_t)dof; 14656 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 14657 dof_provider_t *provider; 14658 dof_probe_t *probe; 14659 uint8_t *arg; 14660 char *strtab, *typestr; 14661 dof_stridx_t typeidx; 14662 size_t typesz; 14663 uint_t nprobes, j, k; 14664 14665 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 14666 14667 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 14668 dtrace_dof_error(dof, "misaligned section offset"); 14669 return (-1); 14670 } 14671 14672 /* 14673 * The section needs to be large enough to contain the DOF provider 14674 * structure appropriate for the given version. 14675 */ 14676 if (sec->dofs_size < 14677 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 14678 offsetof(dof_provider_t, dofpv_prenoffs) : 14679 sizeof (dof_provider_t))) { 14680 dtrace_dof_error(dof, "provider section too small"); 14681 return (-1); 14682 } 14683 14684 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 14685 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 14686 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 14687 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 14688 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 14689 14690 if (str_sec == NULL || prb_sec == NULL || 14691 arg_sec == NULL || off_sec == NULL) 14692 return (-1); 14693 14694 enoff_sec = NULL; 14695 14696 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14697 provider->dofpv_prenoffs != DOF_SECT_NONE && 14698 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 14699 provider->dofpv_prenoffs)) == NULL) 14700 return (-1); 14701 14702 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 14703 14704 if (provider->dofpv_name >= str_sec->dofs_size || 14705 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 14706 dtrace_dof_error(dof, "invalid provider name"); 14707 return (-1); 14708 } 14709 14710 if (prb_sec->dofs_entsize == 0 || 14711 prb_sec->dofs_entsize > prb_sec->dofs_size) { 14712 dtrace_dof_error(dof, "invalid entry size"); 14713 return (-1); 14714 } 14715 14716 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 14717 dtrace_dof_error(dof, "misaligned entry size"); 14718 return (-1); 14719 } 14720 14721 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 14722 dtrace_dof_error(dof, "invalid entry size"); 14723 return (-1); 14724 } 14725 14726 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 14727 dtrace_dof_error(dof, "misaligned section offset"); 14728 return (-1); 14729 } 14730 14731 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 14732 dtrace_dof_error(dof, "invalid entry size"); 14733 return (-1); 14734 } 14735 14736 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 14737 14738 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 14739 14740 /* 14741 * Take a pass through the probes to check for errors. 14742 */ 14743 for (j = 0; j < nprobes; j++) { 14744 probe = (dof_probe_t *)(uintptr_t)(daddr + 14745 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 14746 14747 if (probe->dofpr_func >= str_sec->dofs_size) { 14748 dtrace_dof_error(dof, "invalid function name"); 14749 return (-1); 14750 } 14751 14752 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 14753 dtrace_dof_error(dof, "function name too long"); 14754 return (-1); 14755 } 14756 14757 if (probe->dofpr_name >= str_sec->dofs_size || 14758 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 14759 dtrace_dof_error(dof, "invalid probe name"); 14760 return (-1); 14761 } 14762 14763 /* 14764 * The offset count must not wrap the index, and the offsets 14765 * must also not overflow the section's data. 14766 */ 14767 if (probe->dofpr_offidx + probe->dofpr_noffs < 14768 probe->dofpr_offidx || 14769 (probe->dofpr_offidx + probe->dofpr_noffs) * 14770 off_sec->dofs_entsize > off_sec->dofs_size) { 14771 dtrace_dof_error(dof, "invalid probe offset"); 14772 return (-1); 14773 } 14774 14775 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 14776 /* 14777 * If there's no is-enabled offset section, make sure 14778 * there aren't any is-enabled offsets. Otherwise 14779 * perform the same checks as for probe offsets 14780 * (immediately above). 14781 */ 14782 if (enoff_sec == NULL) { 14783 if (probe->dofpr_enoffidx != 0 || 14784 probe->dofpr_nenoffs != 0) { 14785 dtrace_dof_error(dof, "is-enabled " 14786 "offsets with null section"); 14787 return (-1); 14788 } 14789 } else if (probe->dofpr_enoffidx + 14790 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 14791 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 14792 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 14793 dtrace_dof_error(dof, "invalid is-enabled " 14794 "offset"); 14795 return (-1); 14796 } 14797 14798 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 14799 dtrace_dof_error(dof, "zero probe and " 14800 "is-enabled offsets"); 14801 return (-1); 14802 } 14803 } else if (probe->dofpr_noffs == 0) { 14804 dtrace_dof_error(dof, "zero probe offsets"); 14805 return (-1); 14806 } 14807 14808 if (probe->dofpr_argidx + probe->dofpr_xargc < 14809 probe->dofpr_argidx || 14810 (probe->dofpr_argidx + probe->dofpr_xargc) * 14811 arg_sec->dofs_entsize > arg_sec->dofs_size) { 14812 dtrace_dof_error(dof, "invalid args"); 14813 return (-1); 14814 } 14815 14816 typeidx = probe->dofpr_nargv; 14817 typestr = strtab + probe->dofpr_nargv; 14818 for (k = 0; k < probe->dofpr_nargc; k++) { 14819 if (typeidx >= str_sec->dofs_size) { 14820 dtrace_dof_error(dof, "bad " 14821 "native argument type"); 14822 return (-1); 14823 } 14824 14825 typesz = strlen(typestr) + 1; 14826 if (typesz > DTRACE_ARGTYPELEN) { 14827 dtrace_dof_error(dof, "native " 14828 "argument type too long"); 14829 return (-1); 14830 } 14831 typeidx += typesz; 14832 typestr += typesz; 14833 } 14834 14835 typeidx = probe->dofpr_xargv; 14836 typestr = strtab + probe->dofpr_xargv; 14837 for (k = 0; k < probe->dofpr_xargc; k++) { 14838 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 14839 dtrace_dof_error(dof, "bad " 14840 "native argument index"); 14841 return (-1); 14842 } 14843 14844 if (typeidx >= str_sec->dofs_size) { 14845 dtrace_dof_error(dof, "bad " 14846 "translated argument type"); 14847 return (-1); 14848 } 14849 14850 typesz = strlen(typestr) + 1; 14851 if (typesz > DTRACE_ARGTYPELEN) { 14852 dtrace_dof_error(dof, "translated argument " 14853 "type too long"); 14854 return (-1); 14855 } 14856 14857 typeidx += typesz; 14858 typestr += typesz; 14859 } 14860 } 14861 14862 return (0); 14863} 14864 14865static int 14866dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 14867{ 14868 dtrace_helpers_t *help; 14869 dtrace_vstate_t *vstate; 14870 dtrace_enabling_t *enab = NULL; 14871 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 14872 uintptr_t daddr = (uintptr_t)dof; 14873 14874 ASSERT(MUTEX_HELD(&dtrace_lock)); 14875 14876 if ((help = curproc->p_dtrace_helpers) == NULL) 14877 help = dtrace_helpers_create(curproc); 14878 14879 vstate = &help->dthps_vstate; 14880 14881 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 14882 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 14883 dtrace_dof_destroy(dof); 14884 return (rv); 14885 } 14886 14887 /* 14888 * Look for helper providers and validate their descriptions. 14889 */ 14890 if (dhp != NULL) { 14891 for (i = 0; i < dof->dofh_secnum; i++) { 14892 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 14893 dof->dofh_secoff + i * dof->dofh_secsize); 14894 14895 if (sec->dofs_type != DOF_SECT_PROVIDER) 14896 continue; 14897 14898 if (dtrace_helper_provider_validate(dof, sec) != 0) { 14899 dtrace_enabling_destroy(enab); 14900 dtrace_dof_destroy(dof); 14901 return (-1); 14902 } 14903 14904 nprovs++; 14905 } 14906 } 14907 14908 /* 14909 * Now we need to walk through the ECB descriptions in the enabling. 14910 */ 14911 for (i = 0; i < enab->dten_ndesc; i++) { 14912 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 14913 dtrace_probedesc_t *desc = &ep->dted_probe; 14914 14915 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 14916 continue; 14917 14918 if (strcmp(desc->dtpd_mod, "helper") != 0) 14919 continue; 14920 14921 if (strcmp(desc->dtpd_func, "ustack") != 0) 14922 continue; 14923 14924 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 14925 ep)) != 0) { 14926 /* 14927 * Adding this helper action failed -- we are now going 14928 * to rip out the entire generation and return failure. 14929 */ 14930 (void) dtrace_helper_destroygen(help->dthps_generation); 14931 dtrace_enabling_destroy(enab); 14932 dtrace_dof_destroy(dof); 14933 return (-1); 14934 } 14935 14936 nhelpers++; 14937 } 14938 14939 if (nhelpers < enab->dten_ndesc) 14940 dtrace_dof_error(dof, "unmatched helpers"); 14941 14942 gen = help->dthps_generation++; 14943 dtrace_enabling_destroy(enab); 14944 14945 if (dhp != NULL && nprovs > 0) { 14946 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 14947 if (dtrace_helper_provider_add(dhp, gen) == 0) { 14948 mutex_exit(&dtrace_lock); 14949 dtrace_helper_provider_register(curproc, help, dhp); 14950 mutex_enter(&dtrace_lock); 14951 14952 destroy = 0; 14953 } 14954 } 14955 14956 if (destroy) 14957 dtrace_dof_destroy(dof); 14958 14959 return (gen); 14960} 14961 14962static dtrace_helpers_t * 14963dtrace_helpers_create(proc_t *p) 14964{ 14965 dtrace_helpers_t *help; 14966 14967 ASSERT(MUTEX_HELD(&dtrace_lock)); 14968 ASSERT(p->p_dtrace_helpers == NULL); 14969 14970 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 14971 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 14972 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 14973 14974 p->p_dtrace_helpers = help; 14975 dtrace_helpers++; 14976 14977 return (help); 14978} 14979 14980#if defined(sun) 14981static 14982#endif 14983void 14984dtrace_helpers_destroy(proc_t *p) 14985{ 14986 dtrace_helpers_t *help; 14987 dtrace_vstate_t *vstate; 14988#if defined(sun) 14989 proc_t *p = curproc; 14990#endif 14991 int i; 14992 14993 mutex_enter(&dtrace_lock); 14994 14995 ASSERT(p->p_dtrace_helpers != NULL); 14996 ASSERT(dtrace_helpers > 0); 14997 14998 help = p->p_dtrace_helpers; 14999 vstate = &help->dthps_vstate; 15000 15001 /* 15002 * We're now going to lose the help from this process. 15003 */ 15004 p->p_dtrace_helpers = NULL; 15005 dtrace_sync(); 15006 15007 /* 15008 * Destory the helper actions. 15009 */ 15010 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15011 dtrace_helper_action_t *h, *next; 15012 15013 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15014 next = h->dtha_next; 15015 dtrace_helper_action_destroy(h, vstate); 15016 h = next; 15017 } 15018 } 15019 15020 mutex_exit(&dtrace_lock); 15021 15022 /* 15023 * Destroy the helper providers. 15024 */ 15025 if (help->dthps_maxprovs > 0) { 15026 mutex_enter(&dtrace_meta_lock); 15027 if (dtrace_meta_pid != NULL) { 15028 ASSERT(dtrace_deferred_pid == NULL); 15029 15030 for (i = 0; i < help->dthps_nprovs; i++) { 15031 dtrace_helper_provider_remove( 15032 &help->dthps_provs[i]->dthp_prov, p->p_pid); 15033 } 15034 } else { 15035 mutex_enter(&dtrace_lock); 15036 ASSERT(help->dthps_deferred == 0 || 15037 help->dthps_next != NULL || 15038 help->dthps_prev != NULL || 15039 help == dtrace_deferred_pid); 15040 15041 /* 15042 * Remove the helper from the deferred list. 15043 */ 15044 if (help->dthps_next != NULL) 15045 help->dthps_next->dthps_prev = help->dthps_prev; 15046 if (help->dthps_prev != NULL) 15047 help->dthps_prev->dthps_next = help->dthps_next; 15048 if (dtrace_deferred_pid == help) { 15049 dtrace_deferred_pid = help->dthps_next; 15050 ASSERT(help->dthps_prev == NULL); 15051 } 15052 15053 mutex_exit(&dtrace_lock); 15054 } 15055 15056 mutex_exit(&dtrace_meta_lock); 15057 15058 for (i = 0; i < help->dthps_nprovs; i++) { 15059 dtrace_helper_provider_destroy(help->dthps_provs[i]); 15060 } 15061 15062 kmem_free(help->dthps_provs, help->dthps_maxprovs * 15063 sizeof (dtrace_helper_provider_t *)); 15064 } 15065 15066 mutex_enter(&dtrace_lock); 15067 15068 dtrace_vstate_fini(&help->dthps_vstate); 15069 kmem_free(help->dthps_actions, 15070 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 15071 kmem_free(help, sizeof (dtrace_helpers_t)); 15072 15073 --dtrace_helpers; 15074 mutex_exit(&dtrace_lock); 15075} 15076 15077#if defined(sun) 15078static 15079#endif 15080void 15081dtrace_helpers_duplicate(proc_t *from, proc_t *to) 15082{ 15083 dtrace_helpers_t *help, *newhelp; 15084 dtrace_helper_action_t *helper, *new, *last; 15085 dtrace_difo_t *dp; 15086 dtrace_vstate_t *vstate; 15087 int i, j, sz, hasprovs = 0; 15088 15089 mutex_enter(&dtrace_lock); 15090 ASSERT(from->p_dtrace_helpers != NULL); 15091 ASSERT(dtrace_helpers > 0); 15092 15093 help = from->p_dtrace_helpers; 15094 newhelp = dtrace_helpers_create(to); 15095 ASSERT(to->p_dtrace_helpers != NULL); 15096 15097 newhelp->dthps_generation = help->dthps_generation; 15098 vstate = &newhelp->dthps_vstate; 15099 15100 /* 15101 * Duplicate the helper actions. 15102 */ 15103 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15104 if ((helper = help->dthps_actions[i]) == NULL) 15105 continue; 15106 15107 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 15108 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 15109 KM_SLEEP); 15110 new->dtha_generation = helper->dtha_generation; 15111 15112 if ((dp = helper->dtha_predicate) != NULL) { 15113 dp = dtrace_difo_duplicate(dp, vstate); 15114 new->dtha_predicate = dp; 15115 } 15116 15117 new->dtha_nactions = helper->dtha_nactions; 15118 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 15119 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 15120 15121 for (j = 0; j < new->dtha_nactions; j++) { 15122 dtrace_difo_t *dp = helper->dtha_actions[j]; 15123 15124 ASSERT(dp != NULL); 15125 dp = dtrace_difo_duplicate(dp, vstate); 15126 new->dtha_actions[j] = dp; 15127 } 15128 15129 if (last != NULL) { 15130 last->dtha_next = new; 15131 } else { 15132 newhelp->dthps_actions[i] = new; 15133 } 15134 15135 last = new; 15136 } 15137 } 15138 15139 /* 15140 * Duplicate the helper providers and register them with the 15141 * DTrace framework. 15142 */ 15143 if (help->dthps_nprovs > 0) { 15144 newhelp->dthps_nprovs = help->dthps_nprovs; 15145 newhelp->dthps_maxprovs = help->dthps_nprovs; 15146 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 15147 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 15148 for (i = 0; i < newhelp->dthps_nprovs; i++) { 15149 newhelp->dthps_provs[i] = help->dthps_provs[i]; 15150 newhelp->dthps_provs[i]->dthp_ref++; 15151 } 15152 15153 hasprovs = 1; 15154 } 15155 15156 mutex_exit(&dtrace_lock); 15157 15158 if (hasprovs) 15159 dtrace_helper_provider_register(to, newhelp, NULL); 15160} 15161 15162/* 15163 * DTrace Hook Functions 15164 */ 15165static void 15166dtrace_module_loaded(modctl_t *ctl) 15167{ 15168 dtrace_provider_t *prv; 15169 15170 mutex_enter(&dtrace_provider_lock); 15171#if defined(sun) 15172 mutex_enter(&mod_lock); 15173#endif 15174 15175#if defined(sun) 15176 ASSERT(ctl->mod_busy); 15177#endif 15178 15179 /* 15180 * We're going to call each providers per-module provide operation 15181 * specifying only this module. 15182 */ 15183 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 15184 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 15185 15186#if defined(sun) 15187 mutex_exit(&mod_lock); 15188#endif 15189 mutex_exit(&dtrace_provider_lock); 15190 15191 /* 15192 * If we have any retained enablings, we need to match against them. 15193 * Enabling probes requires that cpu_lock be held, and we cannot hold 15194 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 15195 * module. (In particular, this happens when loading scheduling 15196 * classes.) So if we have any retained enablings, we need to dispatch 15197 * our task queue to do the match for us. 15198 */ 15199 mutex_enter(&dtrace_lock); 15200 15201 if (dtrace_retained == NULL) { 15202 mutex_exit(&dtrace_lock); 15203 return; 15204 } 15205 15206 (void) taskq_dispatch(dtrace_taskq, 15207 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 15208 15209 mutex_exit(&dtrace_lock); 15210 15211 /* 15212 * And now, for a little heuristic sleaze: in general, we want to 15213 * match modules as soon as they load. However, we cannot guarantee 15214 * this, because it would lead us to the lock ordering violation 15215 * outlined above. The common case, of course, is that cpu_lock is 15216 * _not_ held -- so we delay here for a clock tick, hoping that that's 15217 * long enough for the task queue to do its work. If it's not, it's 15218 * not a serious problem -- it just means that the module that we 15219 * just loaded may not be immediately instrumentable. 15220 */ 15221 delay(1); 15222} 15223 15224static void 15225#if defined(sun) 15226dtrace_module_unloaded(modctl_t *ctl) 15227#else 15228dtrace_module_unloaded(modctl_t *ctl, int *error) 15229#endif 15230{ 15231 dtrace_probe_t template, *probe, *first, *next; 15232 dtrace_provider_t *prov; 15233#if !defined(sun) 15234 char modname[DTRACE_MODNAMELEN]; 15235 size_t len; 15236#endif 15237 15238#if defined(sun) 15239 template.dtpr_mod = ctl->mod_modname; 15240#else 15241 /* Handle the fact that ctl->filename may end in ".ko". */ 15242 strlcpy(modname, ctl->filename, sizeof(modname)); 15243 len = strlen(ctl->filename); 15244 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0) 15245 modname[len - 3] = '\0'; 15246 template.dtpr_mod = modname; 15247#endif 15248 15249 mutex_enter(&dtrace_provider_lock); 15250#if defined(sun) 15251 mutex_enter(&mod_lock); 15252#endif 15253 mutex_enter(&dtrace_lock); 15254 15255#if !defined(sun) 15256 if (ctl->nenabled > 0) { 15257 /* Don't allow unloads if a probe is enabled. */ 15258 mutex_exit(&dtrace_provider_lock); 15259 mutex_exit(&dtrace_lock); 15260 *error = -1; 15261 printf( 15262 "kldunload: attempt to unload module that has DTrace probes enabled\n"); 15263 return; 15264 } 15265#endif 15266 15267 if (dtrace_bymod == NULL) { 15268 /* 15269 * The DTrace module is loaded (obviously) but not attached; 15270 * we don't have any work to do. 15271 */ 15272 mutex_exit(&dtrace_provider_lock); 15273#if defined(sun) 15274 mutex_exit(&mod_lock); 15275#endif 15276 mutex_exit(&dtrace_lock); 15277 return; 15278 } 15279 15280 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 15281 probe != NULL; probe = probe->dtpr_nextmod) { 15282 if (probe->dtpr_ecb != NULL) { 15283 mutex_exit(&dtrace_provider_lock); 15284#if defined(sun) 15285 mutex_exit(&mod_lock); 15286#endif 15287 mutex_exit(&dtrace_lock); 15288 15289 /* 15290 * This shouldn't _actually_ be possible -- we're 15291 * unloading a module that has an enabled probe in it. 15292 * (It's normally up to the provider to make sure that 15293 * this can't happen.) However, because dtps_enable() 15294 * doesn't have a failure mode, there can be an 15295 * enable/unload race. Upshot: we don't want to 15296 * assert, but we're not going to disable the 15297 * probe, either. 15298 */ 15299 if (dtrace_err_verbose) { 15300#if defined(sun) 15301 cmn_err(CE_WARN, "unloaded module '%s' had " 15302 "enabled probes", ctl->mod_modname); 15303#else 15304 cmn_err(CE_WARN, "unloaded module '%s' had " 15305 "enabled probes", modname); 15306#endif 15307 } 15308 15309 return; 15310 } 15311 } 15312 15313 probe = first; 15314 15315 for (first = NULL; probe != NULL; probe = next) { 15316 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 15317 15318 dtrace_probes[probe->dtpr_id - 1] = NULL; 15319 15320 next = probe->dtpr_nextmod; 15321 dtrace_hash_remove(dtrace_bymod, probe); 15322 dtrace_hash_remove(dtrace_byfunc, probe); 15323 dtrace_hash_remove(dtrace_byname, probe); 15324 15325 if (first == NULL) { 15326 first = probe; 15327 probe->dtpr_nextmod = NULL; 15328 } else { 15329 probe->dtpr_nextmod = first; 15330 first = probe; 15331 } 15332 } 15333 15334 /* 15335 * We've removed all of the module's probes from the hash chains and 15336 * from the probe array. Now issue a dtrace_sync() to be sure that 15337 * everyone has cleared out from any probe array processing. 15338 */ 15339 dtrace_sync(); 15340 15341 for (probe = first; probe != NULL; probe = first) { 15342 first = probe->dtpr_nextmod; 15343 prov = probe->dtpr_provider; 15344 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 15345 probe->dtpr_arg); 15346 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 15347 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 15348 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 15349#if defined(sun) 15350 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 15351#else 15352 free_unr(dtrace_arena, probe->dtpr_id); 15353#endif 15354 kmem_free(probe, sizeof (dtrace_probe_t)); 15355 } 15356 15357 mutex_exit(&dtrace_lock); 15358#if defined(sun) 15359 mutex_exit(&mod_lock); 15360#endif 15361 mutex_exit(&dtrace_provider_lock); 15362} 15363 15364#if !defined(sun) 15365static void 15366dtrace_kld_load(void *arg __unused, linker_file_t lf) 15367{ 15368 15369 dtrace_module_loaded(lf); 15370} 15371 15372static void 15373dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error) 15374{ 15375 15376 if (*error != 0) 15377 /* We already have an error, so don't do anything. */ 15378 return; 15379 dtrace_module_unloaded(lf, error); 15380} 15381#endif 15382 15383#if defined(sun) 15384static void 15385dtrace_suspend(void) 15386{ 15387 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 15388} 15389 15390static void 15391dtrace_resume(void) 15392{ 15393 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 15394} 15395#endif 15396 15397static int 15398dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 15399{ 15400 ASSERT(MUTEX_HELD(&cpu_lock)); 15401 mutex_enter(&dtrace_lock); 15402 15403 switch (what) { 15404 case CPU_CONFIG: { 15405 dtrace_state_t *state; 15406 dtrace_optval_t *opt, rs, c; 15407 15408 /* 15409 * For now, we only allocate a new buffer for anonymous state. 15410 */ 15411 if ((state = dtrace_anon.dta_state) == NULL) 15412 break; 15413 15414 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 15415 break; 15416 15417 opt = state->dts_options; 15418 c = opt[DTRACEOPT_CPU]; 15419 15420 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 15421 break; 15422 15423 /* 15424 * Regardless of what the actual policy is, we're going to 15425 * temporarily set our resize policy to be manual. We're 15426 * also going to temporarily set our CPU option to denote 15427 * the newly configured CPU. 15428 */ 15429 rs = opt[DTRACEOPT_BUFRESIZE]; 15430 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 15431 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 15432 15433 (void) dtrace_state_buffers(state); 15434 15435 opt[DTRACEOPT_BUFRESIZE] = rs; 15436 opt[DTRACEOPT_CPU] = c; 15437 15438 break; 15439 } 15440 15441 case CPU_UNCONFIG: 15442 /* 15443 * We don't free the buffer in the CPU_UNCONFIG case. (The 15444 * buffer will be freed when the consumer exits.) 15445 */ 15446 break; 15447 15448 default: 15449 break; 15450 } 15451 15452 mutex_exit(&dtrace_lock); 15453 return (0); 15454} 15455 15456#if defined(sun) 15457static void 15458dtrace_cpu_setup_initial(processorid_t cpu) 15459{ 15460 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 15461} 15462#endif 15463 15464static void 15465dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 15466{ 15467 if (dtrace_toxranges >= dtrace_toxranges_max) { 15468 int osize, nsize; 15469 dtrace_toxrange_t *range; 15470 15471 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 15472 15473 if (osize == 0) { 15474 ASSERT(dtrace_toxrange == NULL); 15475 ASSERT(dtrace_toxranges_max == 0); 15476 dtrace_toxranges_max = 1; 15477 } else { 15478 dtrace_toxranges_max <<= 1; 15479 } 15480 15481 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 15482 range = kmem_zalloc(nsize, KM_SLEEP); 15483 15484 if (dtrace_toxrange != NULL) { 15485 ASSERT(osize != 0); 15486 bcopy(dtrace_toxrange, range, osize); 15487 kmem_free(dtrace_toxrange, osize); 15488 } 15489 15490 dtrace_toxrange = range; 15491 } 15492 15493 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 15494 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 15495 15496 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 15497 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 15498 dtrace_toxranges++; 15499} 15500 15501/* 15502 * DTrace Driver Cookbook Functions 15503 */ 15504#if defined(sun) 15505/*ARGSUSED*/ 15506static int 15507dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 15508{ 15509 dtrace_provider_id_t id; 15510 dtrace_state_t *state = NULL; 15511 dtrace_enabling_t *enab; 15512 15513 mutex_enter(&cpu_lock); 15514 mutex_enter(&dtrace_provider_lock); 15515 mutex_enter(&dtrace_lock); 15516 15517 if (ddi_soft_state_init(&dtrace_softstate, 15518 sizeof (dtrace_state_t), 0) != 0) { 15519 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 15520 mutex_exit(&cpu_lock); 15521 mutex_exit(&dtrace_provider_lock); 15522 mutex_exit(&dtrace_lock); 15523 return (DDI_FAILURE); 15524 } 15525 15526 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 15527 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 15528 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 15529 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 15530 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 15531 ddi_remove_minor_node(devi, NULL); 15532 ddi_soft_state_fini(&dtrace_softstate); 15533 mutex_exit(&cpu_lock); 15534 mutex_exit(&dtrace_provider_lock); 15535 mutex_exit(&dtrace_lock); 15536 return (DDI_FAILURE); 15537 } 15538 15539 ddi_report_dev(devi); 15540 dtrace_devi = devi; 15541 15542 dtrace_modload = dtrace_module_loaded; 15543 dtrace_modunload = dtrace_module_unloaded; 15544 dtrace_cpu_init = dtrace_cpu_setup_initial; 15545 dtrace_helpers_cleanup = dtrace_helpers_destroy; 15546 dtrace_helpers_fork = dtrace_helpers_duplicate; 15547 dtrace_cpustart_init = dtrace_suspend; 15548 dtrace_cpustart_fini = dtrace_resume; 15549 dtrace_debugger_init = dtrace_suspend; 15550 dtrace_debugger_fini = dtrace_resume; 15551 15552 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 15553 15554 ASSERT(MUTEX_HELD(&cpu_lock)); 15555 15556 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 15557 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 15558 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 15559 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 15560 VM_SLEEP | VMC_IDENTIFIER); 15561 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 15562 1, INT_MAX, 0); 15563 15564 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 15565 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 15566 NULL, NULL, NULL, NULL, NULL, 0); 15567 15568 ASSERT(MUTEX_HELD(&cpu_lock)); 15569 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 15570 offsetof(dtrace_probe_t, dtpr_nextmod), 15571 offsetof(dtrace_probe_t, dtpr_prevmod)); 15572 15573 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 15574 offsetof(dtrace_probe_t, dtpr_nextfunc), 15575 offsetof(dtrace_probe_t, dtpr_prevfunc)); 15576 15577 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 15578 offsetof(dtrace_probe_t, dtpr_nextname), 15579 offsetof(dtrace_probe_t, dtpr_prevname)); 15580 15581 if (dtrace_retain_max < 1) { 15582 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 15583 "setting to 1", dtrace_retain_max); 15584 dtrace_retain_max = 1; 15585 } 15586 15587 /* 15588 * Now discover our toxic ranges. 15589 */ 15590 dtrace_toxic_ranges(dtrace_toxrange_add); 15591 15592 /* 15593 * Before we register ourselves as a provider to our own framework, 15594 * we would like to assert that dtrace_provider is NULL -- but that's 15595 * not true if we were loaded as a dependency of a DTrace provider. 15596 * Once we've registered, we can assert that dtrace_provider is our 15597 * pseudo provider. 15598 */ 15599 (void) dtrace_register("dtrace", &dtrace_provider_attr, 15600 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 15601 15602 ASSERT(dtrace_provider != NULL); 15603 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 15604 15605 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 15606 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 15607 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 15608 dtrace_provider, NULL, NULL, "END", 0, NULL); 15609 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 15610 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 15611 15612 dtrace_anon_property(); 15613 mutex_exit(&cpu_lock); 15614 15615 /* 15616 * If DTrace helper tracing is enabled, we need to allocate the 15617 * trace buffer and initialize the values. 15618 */ 15619 if (dtrace_helptrace_enabled) { 15620 ASSERT(dtrace_helptrace_buffer == NULL); 15621 dtrace_helptrace_buffer = 15622 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 15623 dtrace_helptrace_next = 0; 15624 } 15625 15626 /* 15627 * If there are already providers, we must ask them to provide their 15628 * probes, and then match any anonymous enabling against them. Note 15629 * that there should be no other retained enablings at this time: 15630 * the only retained enablings at this time should be the anonymous 15631 * enabling. 15632 */ 15633 if (dtrace_anon.dta_enabling != NULL) { 15634 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 15635 15636 dtrace_enabling_provide(NULL); 15637 state = dtrace_anon.dta_state; 15638 15639 /* 15640 * We couldn't hold cpu_lock across the above call to 15641 * dtrace_enabling_provide(), but we must hold it to actually 15642 * enable the probes. We have to drop all of our locks, pick 15643 * up cpu_lock, and regain our locks before matching the 15644 * retained anonymous enabling. 15645 */ 15646 mutex_exit(&dtrace_lock); 15647 mutex_exit(&dtrace_provider_lock); 15648 15649 mutex_enter(&cpu_lock); 15650 mutex_enter(&dtrace_provider_lock); 15651 mutex_enter(&dtrace_lock); 15652 15653 if ((enab = dtrace_anon.dta_enabling) != NULL) 15654 (void) dtrace_enabling_match(enab, NULL); 15655 15656 mutex_exit(&cpu_lock); 15657 } 15658 15659 mutex_exit(&dtrace_lock); 15660 mutex_exit(&dtrace_provider_lock); 15661 15662 if (state != NULL) { 15663 /* 15664 * If we created any anonymous state, set it going now. 15665 */ 15666 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 15667 } 15668 15669 return (DDI_SUCCESS); 15670} 15671#endif 15672 15673#if !defined(sun) 15674#if __FreeBSD_version >= 800039 15675static void dtrace_dtr(void *); 15676#endif 15677#endif 15678 15679/*ARGSUSED*/ 15680static int 15681#if defined(sun) 15682dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 15683#else 15684dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 15685#endif 15686{ 15687 dtrace_state_t *state; 15688 uint32_t priv; 15689 uid_t uid; 15690 zoneid_t zoneid; 15691 15692#if defined(sun) 15693 if (getminor(*devp) == DTRACEMNRN_HELPER) 15694 return (0); 15695 15696 /* 15697 * If this wasn't an open with the "helper" minor, then it must be 15698 * the "dtrace" minor. 15699 */ 15700 ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE); 15701#else 15702 cred_t *cred_p = NULL; 15703 15704#if __FreeBSD_version < 800039 15705 /* 15706 * The first minor device is the one that is cloned so there is 15707 * nothing more to do here. 15708 */ 15709 if (dev2unit(dev) == 0) 15710 return 0; 15711 15712 /* 15713 * Devices are cloned, so if the DTrace state has already 15714 * been allocated, that means this device belongs to a 15715 * different client. Each client should open '/dev/dtrace' 15716 * to get a cloned device. 15717 */ 15718 if (dev->si_drv1 != NULL) 15719 return (EBUSY); 15720#endif 15721 15722 cred_p = dev->si_cred; 15723#endif 15724 15725 /* 15726 * If no DTRACE_PRIV_* bits are set in the credential, then the 15727 * caller lacks sufficient permission to do anything with DTrace. 15728 */ 15729 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 15730 if (priv == DTRACE_PRIV_NONE) { 15731#if !defined(sun) 15732#if __FreeBSD_version < 800039 15733 /* Destroy the cloned device. */ 15734 destroy_dev(dev); 15735#endif 15736#endif 15737 15738 return (EACCES); 15739 } 15740 15741 /* 15742 * Ask all providers to provide all their probes. 15743 */ 15744 mutex_enter(&dtrace_provider_lock); 15745 dtrace_probe_provide(NULL, NULL); 15746 mutex_exit(&dtrace_provider_lock); 15747 15748 mutex_enter(&cpu_lock); 15749 mutex_enter(&dtrace_lock); 15750 dtrace_opens++; 15751 dtrace_membar_producer(); 15752 15753#if defined(sun) 15754 /* 15755 * If the kernel debugger is active (that is, if the kernel debugger 15756 * modified text in some way), we won't allow the open. 15757 */ 15758 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15759 dtrace_opens--; 15760 mutex_exit(&cpu_lock); 15761 mutex_exit(&dtrace_lock); 15762 return (EBUSY); 15763 } 15764 15765 state = dtrace_state_create(devp, cred_p); 15766#else 15767 state = dtrace_state_create(dev); 15768#if __FreeBSD_version < 800039 15769 dev->si_drv1 = state; 15770#else 15771 devfs_set_cdevpriv(state, dtrace_dtr); 15772#endif 15773#endif 15774 15775 mutex_exit(&cpu_lock); 15776 15777 if (state == NULL) { 15778#if defined(sun) 15779 if (--dtrace_opens == 0) 15780 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15781#else 15782 --dtrace_opens; 15783#endif 15784 mutex_exit(&dtrace_lock); 15785#if !defined(sun) 15786#if __FreeBSD_version < 800039 15787 /* Destroy the cloned device. */ 15788 destroy_dev(dev); 15789#endif 15790#endif 15791 return (EAGAIN); 15792 } 15793 15794 mutex_exit(&dtrace_lock); 15795 15796 return (0); 15797} 15798 15799/*ARGSUSED*/ 15800#if defined(sun) 15801static int 15802dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 15803#elif __FreeBSD_version < 800039 15804static int 15805dtrace_close(struct cdev *dev, int flags, int fmt __unused, struct thread *td) 15806#else 15807static void 15808dtrace_dtr(void *data) 15809#endif 15810{ 15811#if defined(sun) 15812 minor_t minor = getminor(dev); 15813 dtrace_state_t *state; 15814 15815 if (minor == DTRACEMNRN_HELPER) 15816 return (0); 15817 15818 state = ddi_get_soft_state(dtrace_softstate, minor); 15819#else 15820#if __FreeBSD_version < 800039 15821 dtrace_state_t *state = dev->si_drv1; 15822 15823 /* Check if this is not a cloned device. */ 15824 if (dev2unit(dev) == 0) 15825 return (0); 15826#else 15827 dtrace_state_t *state = data; 15828#endif 15829 15830#endif 15831 15832 mutex_enter(&cpu_lock); 15833 mutex_enter(&dtrace_lock); 15834 15835 if (state != NULL) { 15836 if (state->dts_anon) { 15837 /* 15838 * There is anonymous state. Destroy that first. 15839 */ 15840 ASSERT(dtrace_anon.dta_state == NULL); 15841 dtrace_state_destroy(state->dts_anon); 15842 } 15843 15844 dtrace_state_destroy(state); 15845 15846#if !defined(sun) 15847 kmem_free(state, 0); 15848#if __FreeBSD_version < 800039 15849 dev->si_drv1 = NULL; 15850#endif 15851#endif 15852 } 15853 15854 ASSERT(dtrace_opens > 0); 15855#if defined(sun) 15856 if (--dtrace_opens == 0) 15857 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15858#else 15859 --dtrace_opens; 15860#endif 15861 15862 mutex_exit(&dtrace_lock); 15863 mutex_exit(&cpu_lock); 15864 15865#if __FreeBSD_version < 800039 15866 /* Schedule this cloned device to be destroyed. */ 15867 destroy_dev_sched(dev); 15868#endif 15869 15870#if defined(sun) || __FreeBSD_version < 800039 15871 return (0); 15872#endif 15873} 15874 15875#if defined(sun) 15876/*ARGSUSED*/ 15877static int 15878dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 15879{ 15880 int rval; 15881 dof_helper_t help, *dhp = NULL; 15882 15883 switch (cmd) { 15884 case DTRACEHIOC_ADDDOF: 15885 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 15886 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 15887 return (EFAULT); 15888 } 15889 15890 dhp = &help; 15891 arg = (intptr_t)help.dofhp_dof; 15892 /*FALLTHROUGH*/ 15893 15894 case DTRACEHIOC_ADD: { 15895 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 15896 15897 if (dof == NULL) 15898 return (rval); 15899 15900 mutex_enter(&dtrace_lock); 15901 15902 /* 15903 * dtrace_helper_slurp() takes responsibility for the dof -- 15904 * it may free it now or it may save it and free it later. 15905 */ 15906 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 15907 *rv = rval; 15908 rval = 0; 15909 } else { 15910 rval = EINVAL; 15911 } 15912 15913 mutex_exit(&dtrace_lock); 15914 return (rval); 15915 } 15916 15917 case DTRACEHIOC_REMOVE: { 15918 mutex_enter(&dtrace_lock); 15919 rval = dtrace_helper_destroygen(arg); 15920 mutex_exit(&dtrace_lock); 15921 15922 return (rval); 15923 } 15924 15925 default: 15926 break; 15927 } 15928 15929 return (ENOTTY); 15930} 15931 15932/*ARGSUSED*/ 15933static int 15934dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 15935{ 15936 minor_t minor = getminor(dev); 15937 dtrace_state_t *state; 15938 int rval; 15939 15940 if (minor == DTRACEMNRN_HELPER) 15941 return (dtrace_ioctl_helper(cmd, arg, rv)); 15942 15943 state = ddi_get_soft_state(dtrace_softstate, minor); 15944 15945 if (state->dts_anon) { 15946 ASSERT(dtrace_anon.dta_state == NULL); 15947 state = state->dts_anon; 15948 } 15949 15950 switch (cmd) { 15951 case DTRACEIOC_PROVIDER: { 15952 dtrace_providerdesc_t pvd; 15953 dtrace_provider_t *pvp; 15954 15955 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 15956 return (EFAULT); 15957 15958 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 15959 mutex_enter(&dtrace_provider_lock); 15960 15961 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 15962 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 15963 break; 15964 } 15965 15966 mutex_exit(&dtrace_provider_lock); 15967 15968 if (pvp == NULL) 15969 return (ESRCH); 15970 15971 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 15972 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 15973 15974 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 15975 return (EFAULT); 15976 15977 return (0); 15978 } 15979 15980 case DTRACEIOC_EPROBE: { 15981 dtrace_eprobedesc_t epdesc; 15982 dtrace_ecb_t *ecb; 15983 dtrace_action_t *act; 15984 void *buf; 15985 size_t size; 15986 uintptr_t dest; 15987 int nrecs; 15988 15989 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 15990 return (EFAULT); 15991 15992 mutex_enter(&dtrace_lock); 15993 15994 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 15995 mutex_exit(&dtrace_lock); 15996 return (EINVAL); 15997 } 15998 15999 if (ecb->dte_probe == NULL) { 16000 mutex_exit(&dtrace_lock); 16001 return (EINVAL); 16002 } 16003 16004 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 16005 epdesc.dtepd_uarg = ecb->dte_uarg; 16006 epdesc.dtepd_size = ecb->dte_size; 16007 16008 nrecs = epdesc.dtepd_nrecs; 16009 epdesc.dtepd_nrecs = 0; 16010 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 16011 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 16012 continue; 16013 16014 epdesc.dtepd_nrecs++; 16015 } 16016 16017 /* 16018 * Now that we have the size, we need to allocate a temporary 16019 * buffer in which to store the complete description. We need 16020 * the temporary buffer to be able to drop dtrace_lock() 16021 * across the copyout(), below. 16022 */ 16023 size = sizeof (dtrace_eprobedesc_t) + 16024 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 16025 16026 buf = kmem_alloc(size, KM_SLEEP); 16027 dest = (uintptr_t)buf; 16028 16029 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 16030 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 16031 16032 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 16033 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 16034 continue; 16035 16036 if (nrecs-- == 0) 16037 break; 16038 16039 bcopy(&act->dta_rec, (void *)dest, 16040 sizeof (dtrace_recdesc_t)); 16041 dest += sizeof (dtrace_recdesc_t); 16042 } 16043 16044 mutex_exit(&dtrace_lock); 16045 16046 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 16047 kmem_free(buf, size); 16048 return (EFAULT); 16049 } 16050 16051 kmem_free(buf, size); 16052 return (0); 16053 } 16054 16055 case DTRACEIOC_AGGDESC: { 16056 dtrace_aggdesc_t aggdesc; 16057 dtrace_action_t *act; 16058 dtrace_aggregation_t *agg; 16059 int nrecs; 16060 uint32_t offs; 16061 dtrace_recdesc_t *lrec; 16062 void *buf; 16063 size_t size; 16064 uintptr_t dest; 16065 16066 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 16067 return (EFAULT); 16068 16069 mutex_enter(&dtrace_lock); 16070 16071 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 16072 mutex_exit(&dtrace_lock); 16073 return (EINVAL); 16074 } 16075 16076 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 16077 16078 nrecs = aggdesc.dtagd_nrecs; 16079 aggdesc.dtagd_nrecs = 0; 16080 16081 offs = agg->dtag_base; 16082 lrec = &agg->dtag_action.dta_rec; 16083 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 16084 16085 for (act = agg->dtag_first; ; act = act->dta_next) { 16086 ASSERT(act->dta_intuple || 16087 DTRACEACT_ISAGG(act->dta_kind)); 16088 16089 /* 16090 * If this action has a record size of zero, it 16091 * denotes an argument to the aggregating action. 16092 * Because the presence of this record doesn't (or 16093 * shouldn't) affect the way the data is interpreted, 16094 * we don't copy it out to save user-level the 16095 * confusion of dealing with a zero-length record. 16096 */ 16097 if (act->dta_rec.dtrd_size == 0) { 16098 ASSERT(agg->dtag_hasarg); 16099 continue; 16100 } 16101 16102 aggdesc.dtagd_nrecs++; 16103 16104 if (act == &agg->dtag_action) 16105 break; 16106 } 16107 16108 /* 16109 * Now that we have the size, we need to allocate a temporary 16110 * buffer in which to store the complete description. We need 16111 * the temporary buffer to be able to drop dtrace_lock() 16112 * across the copyout(), below. 16113 */ 16114 size = sizeof (dtrace_aggdesc_t) + 16115 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 16116 16117 buf = kmem_alloc(size, KM_SLEEP); 16118 dest = (uintptr_t)buf; 16119 16120 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 16121 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 16122 16123 for (act = agg->dtag_first; ; act = act->dta_next) { 16124 dtrace_recdesc_t rec = act->dta_rec; 16125 16126 /* 16127 * See the comment in the above loop for why we pass 16128 * over zero-length records. 16129 */ 16130 if (rec.dtrd_size == 0) { 16131 ASSERT(agg->dtag_hasarg); 16132 continue; 16133 } 16134 16135 if (nrecs-- == 0) 16136 break; 16137 16138 rec.dtrd_offset -= offs; 16139 bcopy(&rec, (void *)dest, sizeof (rec)); 16140 dest += sizeof (dtrace_recdesc_t); 16141 16142 if (act == &agg->dtag_action) 16143 break; 16144 } 16145 16146 mutex_exit(&dtrace_lock); 16147 16148 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 16149 kmem_free(buf, size); 16150 return (EFAULT); 16151 } 16152 16153 kmem_free(buf, size); 16154 return (0); 16155 } 16156 16157 case DTRACEIOC_ENABLE: { 16158 dof_hdr_t *dof; 16159 dtrace_enabling_t *enab = NULL; 16160 dtrace_vstate_t *vstate; 16161 int err = 0; 16162 16163 *rv = 0; 16164 16165 /* 16166 * If a NULL argument has been passed, we take this as our 16167 * cue to reevaluate our enablings. 16168 */ 16169 if (arg == NULL) { 16170 dtrace_enabling_matchall(); 16171 16172 return (0); 16173 } 16174 16175 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 16176 return (rval); 16177 16178 mutex_enter(&cpu_lock); 16179 mutex_enter(&dtrace_lock); 16180 vstate = &state->dts_vstate; 16181 16182 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 16183 mutex_exit(&dtrace_lock); 16184 mutex_exit(&cpu_lock); 16185 dtrace_dof_destroy(dof); 16186 return (EBUSY); 16187 } 16188 16189 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 16190 mutex_exit(&dtrace_lock); 16191 mutex_exit(&cpu_lock); 16192 dtrace_dof_destroy(dof); 16193 return (EINVAL); 16194 } 16195 16196 if ((rval = dtrace_dof_options(dof, state)) != 0) { 16197 dtrace_enabling_destroy(enab); 16198 mutex_exit(&dtrace_lock); 16199 mutex_exit(&cpu_lock); 16200 dtrace_dof_destroy(dof); 16201 return (rval); 16202 } 16203 16204 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 16205 err = dtrace_enabling_retain(enab); 16206 } else { 16207 dtrace_enabling_destroy(enab); 16208 } 16209 16210 mutex_exit(&cpu_lock); 16211 mutex_exit(&dtrace_lock); 16212 dtrace_dof_destroy(dof); 16213 16214 return (err); 16215 } 16216 16217 case DTRACEIOC_REPLICATE: { 16218 dtrace_repldesc_t desc; 16219 dtrace_probedesc_t *match = &desc.dtrpd_match; 16220 dtrace_probedesc_t *create = &desc.dtrpd_create; 16221 int err; 16222 16223 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16224 return (EFAULT); 16225 16226 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 16227 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 16228 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 16229 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 16230 16231 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 16232 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 16233 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 16234 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 16235 16236 mutex_enter(&dtrace_lock); 16237 err = dtrace_enabling_replicate(state, match, create); 16238 mutex_exit(&dtrace_lock); 16239 16240 return (err); 16241 } 16242 16243 case DTRACEIOC_PROBEMATCH: 16244 case DTRACEIOC_PROBES: { 16245 dtrace_probe_t *probe = NULL; 16246 dtrace_probedesc_t desc; 16247 dtrace_probekey_t pkey; 16248 dtrace_id_t i; 16249 int m = 0; 16250 uint32_t priv; 16251 uid_t uid; 16252 zoneid_t zoneid; 16253 16254 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16255 return (EFAULT); 16256 16257 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 16258 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 16259 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 16260 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 16261 16262 /* 16263 * Before we attempt to match this probe, we want to give 16264 * all providers the opportunity to provide it. 16265 */ 16266 if (desc.dtpd_id == DTRACE_IDNONE) { 16267 mutex_enter(&dtrace_provider_lock); 16268 dtrace_probe_provide(&desc, NULL); 16269 mutex_exit(&dtrace_provider_lock); 16270 desc.dtpd_id++; 16271 } 16272 16273 if (cmd == DTRACEIOC_PROBEMATCH) { 16274 dtrace_probekey(&desc, &pkey); 16275 pkey.dtpk_id = DTRACE_IDNONE; 16276 } 16277 16278 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 16279 16280 mutex_enter(&dtrace_lock); 16281 16282 if (cmd == DTRACEIOC_PROBEMATCH) { 16283 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 16284 if ((probe = dtrace_probes[i - 1]) != NULL && 16285 (m = dtrace_match_probe(probe, &pkey, 16286 priv, uid, zoneid)) != 0) 16287 break; 16288 } 16289 16290 if (m < 0) { 16291 mutex_exit(&dtrace_lock); 16292 return (EINVAL); 16293 } 16294 16295 } else { 16296 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 16297 if ((probe = dtrace_probes[i - 1]) != NULL && 16298 dtrace_match_priv(probe, priv, uid, zoneid)) 16299 break; 16300 } 16301 } 16302 16303 if (probe == NULL) { 16304 mutex_exit(&dtrace_lock); 16305 return (ESRCH); 16306 } 16307 16308 dtrace_probe_description(probe, &desc); 16309 mutex_exit(&dtrace_lock); 16310 16311 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16312 return (EFAULT); 16313 16314 return (0); 16315 } 16316 16317 case DTRACEIOC_PROBEARG: { 16318 dtrace_argdesc_t desc; 16319 dtrace_probe_t *probe; 16320 dtrace_provider_t *prov; 16321 16322 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16323 return (EFAULT); 16324 16325 if (desc.dtargd_id == DTRACE_IDNONE) 16326 return (EINVAL); 16327 16328 if (desc.dtargd_ndx == DTRACE_ARGNONE) 16329 return (EINVAL); 16330 16331 mutex_enter(&dtrace_provider_lock); 16332 mutex_enter(&mod_lock); 16333 mutex_enter(&dtrace_lock); 16334 16335 if (desc.dtargd_id > dtrace_nprobes) { 16336 mutex_exit(&dtrace_lock); 16337 mutex_exit(&mod_lock); 16338 mutex_exit(&dtrace_provider_lock); 16339 return (EINVAL); 16340 } 16341 16342 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 16343 mutex_exit(&dtrace_lock); 16344 mutex_exit(&mod_lock); 16345 mutex_exit(&dtrace_provider_lock); 16346 return (EINVAL); 16347 } 16348 16349 mutex_exit(&dtrace_lock); 16350 16351 prov = probe->dtpr_provider; 16352 16353 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 16354 /* 16355 * There isn't any typed information for this probe. 16356 * Set the argument number to DTRACE_ARGNONE. 16357 */ 16358 desc.dtargd_ndx = DTRACE_ARGNONE; 16359 } else { 16360 desc.dtargd_native[0] = '\0'; 16361 desc.dtargd_xlate[0] = '\0'; 16362 desc.dtargd_mapping = desc.dtargd_ndx; 16363 16364 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 16365 probe->dtpr_id, probe->dtpr_arg, &desc); 16366 } 16367 16368 mutex_exit(&mod_lock); 16369 mutex_exit(&dtrace_provider_lock); 16370 16371 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16372 return (EFAULT); 16373 16374 return (0); 16375 } 16376 16377 case DTRACEIOC_GO: { 16378 processorid_t cpuid; 16379 rval = dtrace_state_go(state, &cpuid); 16380 16381 if (rval != 0) 16382 return (rval); 16383 16384 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 16385 return (EFAULT); 16386 16387 return (0); 16388 } 16389 16390 case DTRACEIOC_STOP: { 16391 processorid_t cpuid; 16392 16393 mutex_enter(&dtrace_lock); 16394 rval = dtrace_state_stop(state, &cpuid); 16395 mutex_exit(&dtrace_lock); 16396 16397 if (rval != 0) 16398 return (rval); 16399 16400 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 16401 return (EFAULT); 16402 16403 return (0); 16404 } 16405 16406 case DTRACEIOC_DOFGET: { 16407 dof_hdr_t hdr, *dof; 16408 uint64_t len; 16409 16410 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 16411 return (EFAULT); 16412 16413 mutex_enter(&dtrace_lock); 16414 dof = dtrace_dof_create(state); 16415 mutex_exit(&dtrace_lock); 16416 16417 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 16418 rval = copyout(dof, (void *)arg, len); 16419 dtrace_dof_destroy(dof); 16420 16421 return (rval == 0 ? 0 : EFAULT); 16422 } 16423 16424 case DTRACEIOC_AGGSNAP: 16425 case DTRACEIOC_BUFSNAP: { 16426 dtrace_bufdesc_t desc; 16427 caddr_t cached; 16428 dtrace_buffer_t *buf; 16429 16430 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16431 return (EFAULT); 16432 16433 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 16434 return (EINVAL); 16435 16436 mutex_enter(&dtrace_lock); 16437 16438 if (cmd == DTRACEIOC_BUFSNAP) { 16439 buf = &state->dts_buffer[desc.dtbd_cpu]; 16440 } else { 16441 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 16442 } 16443 16444 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 16445 size_t sz = buf->dtb_offset; 16446 16447 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 16448 mutex_exit(&dtrace_lock); 16449 return (EBUSY); 16450 } 16451 16452 /* 16453 * If this buffer has already been consumed, we're 16454 * going to indicate that there's nothing left here 16455 * to consume. 16456 */ 16457 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 16458 mutex_exit(&dtrace_lock); 16459 16460 desc.dtbd_size = 0; 16461 desc.dtbd_drops = 0; 16462 desc.dtbd_errors = 0; 16463 desc.dtbd_oldest = 0; 16464 sz = sizeof (desc); 16465 16466 if (copyout(&desc, (void *)arg, sz) != 0) 16467 return (EFAULT); 16468 16469 return (0); 16470 } 16471 16472 /* 16473 * If this is a ring buffer that has wrapped, we want 16474 * to copy the whole thing out. 16475 */ 16476 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 16477 dtrace_buffer_polish(buf); 16478 sz = buf->dtb_size; 16479 } 16480 16481 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 16482 mutex_exit(&dtrace_lock); 16483 return (EFAULT); 16484 } 16485 16486 desc.dtbd_size = sz; 16487 desc.dtbd_drops = buf->dtb_drops; 16488 desc.dtbd_errors = buf->dtb_errors; 16489 desc.dtbd_oldest = buf->dtb_xamot_offset; 16490 desc.dtbd_timestamp = dtrace_gethrtime(); 16491 16492 mutex_exit(&dtrace_lock); 16493 16494 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16495 return (EFAULT); 16496 16497 buf->dtb_flags |= DTRACEBUF_CONSUMED; 16498 16499 return (0); 16500 } 16501 16502 if (buf->dtb_tomax == NULL) { 16503 ASSERT(buf->dtb_xamot == NULL); 16504 mutex_exit(&dtrace_lock); 16505 return (ENOENT); 16506 } 16507 16508 cached = buf->dtb_tomax; 16509 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 16510 16511 dtrace_xcall(desc.dtbd_cpu, 16512 (dtrace_xcall_t)dtrace_buffer_switch, buf); 16513 16514 state->dts_errors += buf->dtb_xamot_errors; 16515 16516 /* 16517 * If the buffers did not actually switch, then the cross call 16518 * did not take place -- presumably because the given CPU is 16519 * not in the ready set. If this is the case, we'll return 16520 * ENOENT. 16521 */ 16522 if (buf->dtb_tomax == cached) { 16523 ASSERT(buf->dtb_xamot != cached); 16524 mutex_exit(&dtrace_lock); 16525 return (ENOENT); 16526 } 16527 16528 ASSERT(cached == buf->dtb_xamot); 16529 16530 /* 16531 * We have our snapshot; now copy it out. 16532 */ 16533 if (copyout(buf->dtb_xamot, desc.dtbd_data, 16534 buf->dtb_xamot_offset) != 0) { 16535 mutex_exit(&dtrace_lock); 16536 return (EFAULT); 16537 } 16538 16539 desc.dtbd_size = buf->dtb_xamot_offset; 16540 desc.dtbd_drops = buf->dtb_xamot_drops; 16541 desc.dtbd_errors = buf->dtb_xamot_errors; 16542 desc.dtbd_oldest = 0; 16543 desc.dtbd_timestamp = buf->dtb_switched; 16544 16545 mutex_exit(&dtrace_lock); 16546 16547 /* 16548 * Finally, copy out the buffer description. 16549 */ 16550 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16551 return (EFAULT); 16552 16553 return (0); 16554 } 16555 16556 case DTRACEIOC_CONF: { 16557 dtrace_conf_t conf; 16558 16559 bzero(&conf, sizeof (conf)); 16560 conf.dtc_difversion = DIF_VERSION; 16561 conf.dtc_difintregs = DIF_DIR_NREGS; 16562 conf.dtc_diftupregs = DIF_DTR_NREGS; 16563 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 16564 16565 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 16566 return (EFAULT); 16567 16568 return (0); 16569 } 16570 16571 case DTRACEIOC_STATUS: { 16572 dtrace_status_t stat; 16573 dtrace_dstate_t *dstate; 16574 int i, j; 16575 uint64_t nerrs; 16576 16577 /* 16578 * See the comment in dtrace_state_deadman() for the reason 16579 * for setting dts_laststatus to INT64_MAX before setting 16580 * it to the correct value. 16581 */ 16582 state->dts_laststatus = INT64_MAX; 16583 dtrace_membar_producer(); 16584 state->dts_laststatus = dtrace_gethrtime(); 16585 16586 bzero(&stat, sizeof (stat)); 16587 16588 mutex_enter(&dtrace_lock); 16589 16590 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 16591 mutex_exit(&dtrace_lock); 16592 return (ENOENT); 16593 } 16594 16595 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 16596 stat.dtst_exiting = 1; 16597 16598 nerrs = state->dts_errors; 16599 dstate = &state->dts_vstate.dtvs_dynvars; 16600 16601 for (i = 0; i < NCPU; i++) { 16602 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 16603 16604 stat.dtst_dyndrops += dcpu->dtdsc_drops; 16605 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 16606 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 16607 16608 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 16609 stat.dtst_filled++; 16610 16611 nerrs += state->dts_buffer[i].dtb_errors; 16612 16613 for (j = 0; j < state->dts_nspeculations; j++) { 16614 dtrace_speculation_t *spec; 16615 dtrace_buffer_t *buf; 16616 16617 spec = &state->dts_speculations[j]; 16618 buf = &spec->dtsp_buffer[i]; 16619 stat.dtst_specdrops += buf->dtb_xamot_drops; 16620 } 16621 } 16622 16623 stat.dtst_specdrops_busy = state->dts_speculations_busy; 16624 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 16625 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 16626 stat.dtst_dblerrors = state->dts_dblerrors; 16627 stat.dtst_killed = 16628 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 16629 stat.dtst_errors = nerrs; 16630 16631 mutex_exit(&dtrace_lock); 16632 16633 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 16634 return (EFAULT); 16635 16636 return (0); 16637 } 16638 16639 case DTRACEIOC_FORMAT: { 16640 dtrace_fmtdesc_t fmt; 16641 char *str; 16642 int len; 16643 16644 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 16645 return (EFAULT); 16646 16647 mutex_enter(&dtrace_lock); 16648 16649 if (fmt.dtfd_format == 0 || 16650 fmt.dtfd_format > state->dts_nformats) { 16651 mutex_exit(&dtrace_lock); 16652 return (EINVAL); 16653 } 16654 16655 /* 16656 * Format strings are allocated contiguously and they are 16657 * never freed; if a format index is less than the number 16658 * of formats, we can assert that the format map is non-NULL 16659 * and that the format for the specified index is non-NULL. 16660 */ 16661 ASSERT(state->dts_formats != NULL); 16662 str = state->dts_formats[fmt.dtfd_format - 1]; 16663 ASSERT(str != NULL); 16664 16665 len = strlen(str) + 1; 16666 16667 if (len > fmt.dtfd_length) { 16668 fmt.dtfd_length = len; 16669 16670 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 16671 mutex_exit(&dtrace_lock); 16672 return (EINVAL); 16673 } 16674 } else { 16675 if (copyout(str, fmt.dtfd_string, len) != 0) { 16676 mutex_exit(&dtrace_lock); 16677 return (EINVAL); 16678 } 16679 } 16680 16681 mutex_exit(&dtrace_lock); 16682 return (0); 16683 } 16684 16685 default: 16686 break; 16687 } 16688 16689 return (ENOTTY); 16690} 16691 16692/*ARGSUSED*/ 16693static int 16694dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 16695{ 16696 dtrace_state_t *state; 16697 16698 switch (cmd) { 16699 case DDI_DETACH: 16700 break; 16701 16702 case DDI_SUSPEND: 16703 return (DDI_SUCCESS); 16704 16705 default: 16706 return (DDI_FAILURE); 16707 } 16708 16709 mutex_enter(&cpu_lock); 16710 mutex_enter(&dtrace_provider_lock); 16711 mutex_enter(&dtrace_lock); 16712 16713 ASSERT(dtrace_opens == 0); 16714 16715 if (dtrace_helpers > 0) { 16716 mutex_exit(&dtrace_provider_lock); 16717 mutex_exit(&dtrace_lock); 16718 mutex_exit(&cpu_lock); 16719 return (DDI_FAILURE); 16720 } 16721 16722 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 16723 mutex_exit(&dtrace_provider_lock); 16724 mutex_exit(&dtrace_lock); 16725 mutex_exit(&cpu_lock); 16726 return (DDI_FAILURE); 16727 } 16728 16729 dtrace_provider = NULL; 16730 16731 if ((state = dtrace_anon_grab()) != NULL) { 16732 /* 16733 * If there were ECBs on this state, the provider should 16734 * have not been allowed to detach; assert that there is 16735 * none. 16736 */ 16737 ASSERT(state->dts_necbs == 0); 16738 dtrace_state_destroy(state); 16739 16740 /* 16741 * If we're being detached with anonymous state, we need to 16742 * indicate to the kernel debugger that DTrace is now inactive. 16743 */ 16744 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16745 } 16746 16747 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 16748 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16749 dtrace_cpu_init = NULL; 16750 dtrace_helpers_cleanup = NULL; 16751 dtrace_helpers_fork = NULL; 16752 dtrace_cpustart_init = NULL; 16753 dtrace_cpustart_fini = NULL; 16754 dtrace_debugger_init = NULL; 16755 dtrace_debugger_fini = NULL; 16756 dtrace_modload = NULL; 16757 dtrace_modunload = NULL; 16758 16759 mutex_exit(&cpu_lock); 16760 16761 if (dtrace_helptrace_enabled) { 16762 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 16763 dtrace_helptrace_buffer = NULL; 16764 } 16765 16766 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 16767 dtrace_probes = NULL; 16768 dtrace_nprobes = 0; 16769 16770 dtrace_hash_destroy(dtrace_bymod); 16771 dtrace_hash_destroy(dtrace_byfunc); 16772 dtrace_hash_destroy(dtrace_byname); 16773 dtrace_bymod = NULL; 16774 dtrace_byfunc = NULL; 16775 dtrace_byname = NULL; 16776 16777 kmem_cache_destroy(dtrace_state_cache); 16778 vmem_destroy(dtrace_minor); 16779 vmem_destroy(dtrace_arena); 16780 16781 if (dtrace_toxrange != NULL) { 16782 kmem_free(dtrace_toxrange, 16783 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 16784 dtrace_toxrange = NULL; 16785 dtrace_toxranges = 0; 16786 dtrace_toxranges_max = 0; 16787 } 16788 16789 ddi_remove_minor_node(dtrace_devi, NULL); 16790 dtrace_devi = NULL; 16791 16792 ddi_soft_state_fini(&dtrace_softstate); 16793 16794 ASSERT(dtrace_vtime_references == 0); 16795 ASSERT(dtrace_opens == 0); 16796 ASSERT(dtrace_retained == NULL); 16797 16798 mutex_exit(&dtrace_lock); 16799 mutex_exit(&dtrace_provider_lock); 16800 16801 /* 16802 * We don't destroy the task queue until after we have dropped our 16803 * locks (taskq_destroy() may block on running tasks). To prevent 16804 * attempting to do work after we have effectively detached but before 16805 * the task queue has been destroyed, all tasks dispatched via the 16806 * task queue must check that DTrace is still attached before 16807 * performing any operation. 16808 */ 16809 taskq_destroy(dtrace_taskq); 16810 dtrace_taskq = NULL; 16811 16812 return (DDI_SUCCESS); 16813} 16814#endif 16815 16816#if defined(sun) 16817/*ARGSUSED*/ 16818static int 16819dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 16820{ 16821 int error; 16822 16823 switch (infocmd) { 16824 case DDI_INFO_DEVT2DEVINFO: 16825 *result = (void *)dtrace_devi; 16826 error = DDI_SUCCESS; 16827 break; 16828 case DDI_INFO_DEVT2INSTANCE: 16829 *result = (void *)0; 16830 error = DDI_SUCCESS; 16831 break; 16832 default: 16833 error = DDI_FAILURE; 16834 } 16835 return (error); 16836} 16837#endif 16838 16839#if defined(sun) 16840static struct cb_ops dtrace_cb_ops = { 16841 dtrace_open, /* open */ 16842 dtrace_close, /* close */ 16843 nulldev, /* strategy */ 16844 nulldev, /* print */ 16845 nodev, /* dump */ 16846 nodev, /* read */ 16847 nodev, /* write */ 16848 dtrace_ioctl, /* ioctl */ 16849 nodev, /* devmap */ 16850 nodev, /* mmap */ 16851 nodev, /* segmap */ 16852 nochpoll, /* poll */ 16853 ddi_prop_op, /* cb_prop_op */ 16854 0, /* streamtab */ 16855 D_NEW | D_MP /* Driver compatibility flag */ 16856}; 16857 16858static struct dev_ops dtrace_ops = { 16859 DEVO_REV, /* devo_rev */ 16860 0, /* refcnt */ 16861 dtrace_info, /* get_dev_info */ 16862 nulldev, /* identify */ 16863 nulldev, /* probe */ 16864 dtrace_attach, /* attach */ 16865 dtrace_detach, /* detach */ 16866 nodev, /* reset */ 16867 &dtrace_cb_ops, /* driver operations */ 16868 NULL, /* bus operations */ 16869 nodev /* dev power */ 16870}; 16871 16872static struct modldrv modldrv = { 16873 &mod_driverops, /* module type (this is a pseudo driver) */ 16874 "Dynamic Tracing", /* name of module */ 16875 &dtrace_ops, /* driver ops */ 16876}; 16877 16878static struct modlinkage modlinkage = { 16879 MODREV_1, 16880 (void *)&modldrv, 16881 NULL 16882}; 16883 16884int 16885_init(void) 16886{ 16887 return (mod_install(&modlinkage)); 16888} 16889 16890int 16891_info(struct modinfo *modinfop) 16892{ 16893 return (mod_info(&modlinkage, modinfop)); 16894} 16895 16896int 16897_fini(void) 16898{ 16899 return (mod_remove(&modlinkage)); 16900} 16901#else 16902 16903static d_ioctl_t dtrace_ioctl; 16904static d_ioctl_t dtrace_ioctl_helper; 16905static void dtrace_load(void *); 16906static int dtrace_unload(void); 16907#if __FreeBSD_version < 800039 16908static void dtrace_clone(void *, struct ucred *, char *, int , struct cdev **); 16909static struct clonedevs *dtrace_clones; /* Ptr to the array of cloned devices. */ 16910static eventhandler_tag eh_tag; /* Event handler tag. */ 16911#else 16912static struct cdev *dtrace_dev; 16913static struct cdev *helper_dev; 16914#endif 16915 16916void dtrace_invop_init(void); 16917void dtrace_invop_uninit(void); 16918 16919static struct cdevsw dtrace_cdevsw = { 16920 .d_version = D_VERSION, 16921#if __FreeBSD_version < 800039 16922 .d_flags = D_TRACKCLOSE | D_NEEDMINOR, 16923 .d_close = dtrace_close, 16924#endif 16925 .d_ioctl = dtrace_ioctl, 16926 .d_open = dtrace_open, 16927 .d_name = "dtrace", 16928}; 16929 16930static struct cdevsw helper_cdevsw = { 16931 .d_version = D_VERSION, 16932 .d_ioctl = dtrace_ioctl_helper, 16933 .d_name = "helper", 16934}; 16935 16936#include <dtrace_anon.c> 16937#if __FreeBSD_version < 800039 16938#include <dtrace_clone.c> 16939#endif 16940#include <dtrace_ioctl.c> 16941#include <dtrace_load.c> 16942#include <dtrace_modevent.c> 16943#include <dtrace_sysctl.c> 16944#include <dtrace_unload.c> 16945#include <dtrace_vtime.c> 16946#include <dtrace_hacks.c> 16947#include <dtrace_isa.c> 16948 16949SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 16950SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 16951SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 16952 16953DEV_MODULE(dtrace, dtrace_modevent, NULL); 16954MODULE_VERSION(dtrace, 1); 16955MODULE_DEPEND(dtrace, cyclic, 1, 1, 1); 16956MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 16957#endif 16958