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.
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.
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]
25 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright (c) 2016, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
31 * DTrace - Dynamic Tracing for Solaris
33 * This is the implementation of the Solaris Dynamic Tracing framework
34 * (DTrace). The user-visible interface to DTrace is described at length in
35 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace
36 * library, the in-kernel DTrace framework, and the DTrace providers are
37 * described in the block comments in the <sys/dtrace.h> header file. The
38 * internal architecture of DTrace is described in the block comments in the
39 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace
40 * implementation very much assume mastery of all of these sources; if one has
41 * an unanswered question about the implementation, one should consult them
44 * The functions here are ordered roughly as follows:
46 * - Probe context functions
47 * - Probe hashing functions
48 * - Non-probe context utility functions
49 * - Matching functions
50 * - Provider-to-Framework API functions
51 * - Probe management functions
52 * - DIF object functions
54 * - Predicate functions
57 * - Enabling functions
59 * - Anonymous enabling functions
60 * - Consumer state functions
63 * - Driver cookbook functions
65 * Each group of functions begins with a block comment labelled the "DTrace
66 * [Group] Functions", allowing one to find each block by searching forward
67 * on capital-f functions.
69 #include <sys/errno.h>
74 #include <sys/modctl.h>
76 #include <sys/systm.h>
79 #include <sys/sunddi.h>
81 #include <sys/cpuvar.h>
84 #include <sys/strsubr.h>
86 #include <sys/sysmacros.h>
87 #include <sys/dtrace_impl.h>
88 #include <sys/atomic.h>
89 #include <sys/cmn_err.h>
91 #include <sys/mutex_impl.h>
92 #include <sys/rwlock_impl.h>
94 #include <sys/ctf_api.h>
96 #include <sys/panic.h>
97 #include <sys/priv_impl.h>
99 #include <sys/policy.h>
101 #include <sys/cred_impl.h>
102 #include <sys/procfs_isa.h>
104 #include <sys/taskq.h>
106 #include <sys/mkdev.h>
109 #include <sys/zone.h>
110 #include <sys/socket.h>
111 #include <netinet/in.h>
112 #include "strtolctype.h"
114 /* FreeBSD includes: */
116 #include <sys/callout.h>
117 #include <sys/ctype.h>
118 #include <sys/eventhandler.h>
119 #include <sys/limits.h>
120 #include <sys/linker.h>
122 #include <sys/kernel.h>
123 #include <sys/malloc.h>
124 #include <sys/lock.h>
125 #include <sys/mutex.h>
126 #include <sys/ptrace.h>
127 #include <sys/random.h>
128 #include <sys/rwlock.h>
130 #include <sys/sysctl.h>
132 #include <sys/dtrace_bsd.h>
134 #include <netinet/in.h>
136 #include "dtrace_cddl.h"
137 #include "dtrace_debug.c"
140 #include "dtrace_xoroshiro128_plus.h"
143 * DTrace Tunable Variables
145 * The following variables may be tuned by adding a line to /etc/system that
146 * includes both the name of the DTrace module ("dtrace") and the name of the
147 * variable. For example:
149 * set dtrace:dtrace_destructive_disallow = 1
151 * In general, the only variables that one should be tuning this way are those
152 * that affect system-wide DTrace behavior, and for which the default behavior
153 * is undesirable. Most of these variables are tunable on a per-consumer
154 * basis using DTrace options, and need not be tuned on a system-wide basis.
155 * When tuning these variables, avoid pathological values; while some attempt
156 * is made to verify the integrity of these variables, they are not considered
157 * part of the supported interface to DTrace, and they are therefore not
158 * checked comprehensively. Further, these variables should not be tuned
159 * dynamically via "mdb -kw" or other means; they should only be tuned via
162 int dtrace_destructive_disallow = 0;
164 /* Positive logic version of dtrace_destructive_disallow for loader tunable */
165 int dtrace_allow_destructive = 1;
167 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024);
168 size_t dtrace_difo_maxsize = (256 * 1024);
169 dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024);
170 size_t dtrace_statvar_maxsize = (16 * 1024);
171 size_t dtrace_actions_max = (16 * 1024);
172 size_t dtrace_retain_max = 1024;
173 dtrace_optval_t dtrace_helper_actions_max = 128;
174 dtrace_optval_t dtrace_helper_providers_max = 32;
175 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024);
176 size_t dtrace_strsize_default = 256;
177 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */
178 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */
179 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */
180 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */
181 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */
182 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */
183 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */
184 dtrace_optval_t dtrace_nspec_default = 1;
185 dtrace_optval_t dtrace_specsize_default = 32 * 1024;
186 dtrace_optval_t dtrace_stackframes_default = 20;
187 dtrace_optval_t dtrace_ustackframes_default = 20;
188 dtrace_optval_t dtrace_jstackframes_default = 50;
189 dtrace_optval_t dtrace_jstackstrsize_default = 512;
190 int dtrace_msgdsize_max = 128;
191 hrtime_t dtrace_chill_max = MSEC2NSEC(500); /* 500 ms */
192 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */
193 int dtrace_devdepth_max = 32;
194 int dtrace_err_verbose;
195 hrtime_t dtrace_deadman_interval = NANOSEC;
196 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC;
197 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC;
198 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC;
200 int dtrace_memstr_max = 4096;
204 * DTrace External Variables
206 * As dtrace(7D) is a kernel module, any DTrace variables are obviously
207 * available to DTrace consumers via the backtick (`) syntax. One of these,
208 * dtrace_zero, is made deliberately so: it is provided as a source of
209 * well-known, zero-filled memory. While this variable is not documented,
210 * it is used by some translators as an implementation detail.
212 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */
215 * DTrace Internal Variables
218 static dev_info_t *dtrace_devi; /* device info */
221 static vmem_t *dtrace_arena; /* probe ID arena */
222 static vmem_t *dtrace_minor; /* minor number arena */
224 static taskq_t *dtrace_taskq; /* task queue */
225 static struct unrhdr *dtrace_arena; /* Probe ID number. */
227 static dtrace_probe_t **dtrace_probes; /* array of all probes */
228 static int dtrace_nprobes; /* number of probes */
229 static dtrace_provider_t *dtrace_provider; /* provider list */
230 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */
231 static int dtrace_opens; /* number of opens */
232 static int dtrace_helpers; /* number of helpers */
233 static int dtrace_getf; /* number of unpriv getf()s */
235 static void *dtrace_softstate; /* softstate pointer */
237 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */
238 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */
239 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */
240 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */
241 static int dtrace_toxranges; /* number of toxic ranges */
242 static int dtrace_toxranges_max; /* size of toxic range array */
243 static dtrace_anon_t dtrace_anon; /* anonymous enabling */
244 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */
245 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */
246 static kthread_t *dtrace_panicked; /* panicking thread */
247 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */
248 static dtrace_genid_t dtrace_probegen; /* current probe generation */
249 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */
250 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */
251 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */
252 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */
253 static int dtrace_dynvar_failclean; /* dynvars failed to clean */
255 static struct mtx dtrace_unr_mtx;
256 MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF);
257 static eventhandler_tag dtrace_kld_load_tag;
258 static eventhandler_tag dtrace_kld_unload_try_tag;
263 * DTrace is protected by three (relatively coarse-grained) locks:
265 * (1) dtrace_lock is required to manipulate essentially any DTrace state,
266 * including enabling state, probes, ECBs, consumer state, helper state,
267 * etc. Importantly, dtrace_lock is _not_ required when in probe context;
268 * probe context is lock-free -- synchronization is handled via the
269 * dtrace_sync() cross call mechanism.
271 * (2) dtrace_provider_lock is required when manipulating provider state, or
272 * when provider state must be held constant.
274 * (3) dtrace_meta_lock is required when manipulating meta provider state, or
275 * when meta provider state must be held constant.
277 * The lock ordering between these three locks is dtrace_meta_lock before
278 * dtrace_provider_lock before dtrace_lock. (In particular, there are
279 * several places where dtrace_provider_lock is held by the framework as it
280 * calls into the providers -- which then call back into the framework,
281 * grabbing dtrace_lock.)
283 * There are two other locks in the mix: mod_lock and cpu_lock. With respect
284 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
285 * role as a coarse-grained lock; it is acquired before both of these locks.
286 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must
287 * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
288 * mod_lock is similar with respect to dtrace_provider_lock in that it must be
289 * acquired _between_ dtrace_provider_lock and dtrace_lock.
291 static kmutex_t dtrace_lock; /* probe state lock */
292 static kmutex_t dtrace_provider_lock; /* provider state lock */
293 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */
296 /* XXX FreeBSD hacks. */
297 #define cr_suid cr_svuid
298 #define cr_sgid cr_svgid
299 #define ipaddr_t in_addr_t
300 #define mod_modname pathname
301 #define vuprintf vprintf
302 #define ttoproc(_a) ((_a)->td_proc)
303 #define crgetzoneid(_a) 0
305 #define CPU_ON_INTR(_a) 0
307 #define PRIV_EFFECTIVE (1 << 0)
308 #define PRIV_DTRACE_KERNEL (1 << 1)
309 #define PRIV_DTRACE_PROC (1 << 2)
310 #define PRIV_DTRACE_USER (1 << 3)
311 #define PRIV_PROC_OWNER (1 << 4)
312 #define PRIV_PROC_ZONE (1 << 5)
315 SYSCTL_DECL(_debug_dtrace);
316 SYSCTL_DECL(_kern_dtrace);
320 #define curcpu CPU->cpu_id
325 * DTrace Provider Variables
327 * These are the variables relating to DTrace as a provider (that is, the
328 * provider of the BEGIN, END, and ERROR probes).
330 static dtrace_pattr_t dtrace_provider_attr = {
331 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
332 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
333 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
334 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
335 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
342 static dtrace_pops_t dtrace_provider_ops = {
343 .dtps_provide = (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop,
344 .dtps_provide_module = (void (*)(void *, modctl_t *))dtrace_nullop,
345 .dtps_enable = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
346 .dtps_disable = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
347 .dtps_suspend = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
348 .dtps_resume = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
349 .dtps_getargdesc = NULL,
350 .dtps_getargval = NULL,
351 .dtps_usermode = NULL,
352 .dtps_destroy = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
355 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */
356 static dtrace_id_t dtrace_probeid_end; /* special END probe */
357 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */
360 * DTrace Helper Tracing Variables
362 * These variables should be set dynamically to enable helper tracing. The
363 * only variables that should be set are dtrace_helptrace_enable (which should
364 * be set to a non-zero value to allocate helper tracing buffers on the next
365 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a
366 * non-zero value to deallocate helper tracing buffers on the next close of
367 * /dev/dtrace). When (and only when) helper tracing is disabled, the
368 * buffer size may also be set via dtrace_helptrace_bufsize.
370 int dtrace_helptrace_enable = 0;
371 int dtrace_helptrace_disable = 0;
372 int dtrace_helptrace_bufsize = 16 * 1024 * 1024;
373 uint32_t dtrace_helptrace_nlocals;
374 static dtrace_helptrace_t *dtrace_helptrace_buffer;
375 static uint32_t dtrace_helptrace_next = 0;
376 static int dtrace_helptrace_wrapped = 0;
379 * DTrace Error Hashing
381 * On DEBUG kernels, DTrace will track the errors that has seen in a hash
382 * table. This is very useful for checking coverage of tests that are
383 * expected to induce DIF or DOF processing errors, and may be useful for
384 * debugging problems in the DIF code generator or in DOF generation . The
385 * error hash may be examined with the ::dtrace_errhash MDB dcmd.
388 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ];
389 static const char *dtrace_errlast;
390 static kthread_t *dtrace_errthread;
391 static kmutex_t dtrace_errlock;
395 * DTrace Macros and Constants
397 * These are various macros that are useful in various spots in the
398 * implementation, along with a few random constants that have no meaning
399 * outside of the implementation. There is no real structure to this cpp
400 * mishmash -- but is there ever?
402 #define DTRACE_HASHSTR(hash, probe) \
403 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
405 #define DTRACE_HASHNEXT(hash, probe) \
406 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
408 #define DTRACE_HASHPREV(hash, probe) \
409 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
411 #define DTRACE_HASHEQ(hash, lhs, rhs) \
412 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
413 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
415 #define DTRACE_AGGHASHSIZE_SLEW 17
417 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3)
420 * The key for a thread-local variable consists of the lower 61 bits of the
421 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
422 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
423 * equal to a variable identifier. This is necessary (but not sufficient) to
424 * assure that global associative arrays never collide with thread-local
425 * variables. To guarantee that they cannot collide, we must also define the
426 * order for keying dynamic variables. That order is:
428 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
430 * Because the variable-key and the tls-key are in orthogonal spaces, there is
431 * no way for a global variable key signature to match a thread-local key
435 #define DTRACE_TLS_THRKEY(where) { \
437 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \
438 for (; actv; actv >>= 1) \
440 ASSERT(intr < (1 << 3)); \
441 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \
442 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
445 #define DTRACE_TLS_THRKEY(where) { \
446 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \
448 uint_t actv = _c->cpu_intr_actv; \
449 for (; actv; actv >>= 1) \
451 ASSERT(intr < (1 << 3)); \
452 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \
453 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
457 #define DT_BSWAP_8(x) ((x) & 0xff)
458 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
459 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
460 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
462 #define DT_MASK_LO 0x00000000FFFFFFFFULL
464 #define DTRACE_STORE(type, tomax, offset, what) \
465 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
468 #define DTRACE_ALIGNCHECK(addr, size, flags) \
469 if (addr & (size - 1)) { \
470 *flags |= CPU_DTRACE_BADALIGN; \
471 cpu_core[curcpu].cpuc_dtrace_illval = addr; \
475 #define DTRACE_ALIGNCHECK(addr, size, flags)
479 * Test whether a range of memory starting at testaddr of size testsz falls
480 * within the range of memory described by addr, sz. We take care to avoid
481 * problems with overflow and underflow of the unsigned quantities, and
482 * disallow all negative sizes. Ranges of size 0 are allowed.
484 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
485 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \
486 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \
487 (testaddr) + (testsz) >= (testaddr))
489 #define DTRACE_RANGE_REMAIN(remp, addr, baseaddr, basesz) \
491 if ((remp) != NULL) { \
492 *(remp) = (uintptr_t)(baseaddr) + (basesz) - (addr); \
494 _NOTE(CONSTCOND) } while (0)
498 * Test whether alloc_sz bytes will fit in the scratch region. We isolate
499 * alloc_sz on the righthand side of the comparison in order to avoid overflow
500 * or underflow in the comparison with it. This is simpler than the INRANGE
501 * check above, because we know that the dtms_scratch_ptr is valid in the
502 * range. Allocations of size zero are allowed.
504 #define DTRACE_INSCRATCH(mstate, alloc_sz) \
505 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
506 (mstate)->dtms_scratch_ptr >= (alloc_sz))
508 #define DTRACE_LOADFUNC(bits) \
511 dtrace_load##bits(uintptr_t addr) \
513 size_t size = bits / NBBY; \
515 uint##bits##_t rval; \
517 volatile uint16_t *flags = (volatile uint16_t *) \
518 &cpu_core[curcpu].cpuc_dtrace_flags; \
520 DTRACE_ALIGNCHECK(addr, size, flags); \
522 for (i = 0; i < dtrace_toxranges; i++) { \
523 if (addr >= dtrace_toxrange[i].dtt_limit) \
526 if (addr + size <= dtrace_toxrange[i].dtt_base) \
530 * This address falls within a toxic region; return 0. \
532 *flags |= CPU_DTRACE_BADADDR; \
533 cpu_core[curcpu].cpuc_dtrace_illval = addr; \
537 *flags |= CPU_DTRACE_NOFAULT; \
539 rval = *((volatile uint##bits##_t *)addr); \
540 *flags &= ~CPU_DTRACE_NOFAULT; \
542 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \
546 #define dtrace_loadptr dtrace_load64
548 #define dtrace_loadptr dtrace_load32
551 #define DTRACE_DYNHASH_FREE 0
552 #define DTRACE_DYNHASH_SINK 1
553 #define DTRACE_DYNHASH_VALID 2
555 #define DTRACE_MATCH_NEXT 0
556 #define DTRACE_MATCH_DONE 1
557 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0')
558 #define DTRACE_STATE_ALIGN 64
560 #define DTRACE_FLAGS2FLT(flags) \
561 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \
562 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \
563 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \
564 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \
565 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \
566 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \
567 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \
568 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \
569 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \
572 #define DTRACEACT_ISSTRING(act) \
573 ((act)->dta_kind == DTRACEACT_DIFEXPR && \
574 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
576 /* Function prototype definitions: */
577 static size_t dtrace_strlen(const char *, size_t);
578 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id);
579 static void dtrace_enabling_provide(dtrace_provider_t *);
580 static int dtrace_enabling_match(dtrace_enabling_t *, int *);
581 static void dtrace_enabling_matchall(void);
582 static void dtrace_enabling_reap(void);
583 static dtrace_state_t *dtrace_anon_grab(void);
584 static uint64_t dtrace_helper(int, dtrace_mstate_t *,
585 dtrace_state_t *, uint64_t, uint64_t);
586 static dtrace_helpers_t *dtrace_helpers_create(proc_t *);
587 static void dtrace_buffer_drop(dtrace_buffer_t *);
588 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when);
589 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t,
590 dtrace_state_t *, dtrace_mstate_t *);
591 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t,
593 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *);
594 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *);
595 uint16_t dtrace_load16(uintptr_t);
596 uint32_t dtrace_load32(uintptr_t);
597 uint64_t dtrace_load64(uintptr_t);
598 uint8_t dtrace_load8(uintptr_t);
599 void dtrace_dynvar_clean(dtrace_dstate_t *);
600 dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *,
601 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *);
602 uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *);
603 static int dtrace_priv_proc(dtrace_state_t *);
604 static void dtrace_getf_barrier(void);
605 static int dtrace_canload_remains(uint64_t, size_t, size_t *,
606 dtrace_mstate_t *, dtrace_vstate_t *);
607 static int dtrace_canstore_remains(uint64_t, size_t, size_t *,
608 dtrace_mstate_t *, dtrace_vstate_t *);
611 * DTrace Probe Context Functions
613 * These functions are called from probe context. Because probe context is
614 * any context in which C may be called, arbitrarily locks may be held,
615 * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
616 * As a result, functions called from probe context may only call other DTrace
617 * support functions -- they may not interact at all with the system at large.
618 * (Note that the ASSERT macro is made probe-context safe by redefining it in
619 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
620 * loads are to be performed from probe context, they _must_ be in terms of
621 * the safe dtrace_load*() variants.
623 * Some functions in this block are not actually called from probe context;
624 * for these functions, there will be a comment above the function reading
625 * "Note: not called from probe context."
628 dtrace_panic(const char *format, ...)
632 va_start(alist, format);
634 vpanic(format, alist);
636 dtrace_vpanic(format, alist);
642 dtrace_assfail(const char *a, const char *f, int l)
644 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l);
647 * We just need something here that even the most clever compiler
648 * cannot optimize away.
650 return (a[(uintptr_t)f]);
654 * Atomically increment a specified error counter from probe context.
657 dtrace_error(uint32_t *counter)
660 * Most counters stored to in probe context are per-CPU counters.
661 * However, there are some error conditions that are sufficiently
662 * arcane that they don't merit per-CPU storage. If these counters
663 * are incremented concurrently on different CPUs, scalability will be
664 * adversely affected -- but we don't expect them to be white-hot in a
665 * correctly constructed enabling...
672 if ((nval = oval + 1) == 0) {
674 * If the counter would wrap, set it to 1 -- assuring
675 * that the counter is never zero when we have seen
676 * errors. (The counter must be 32-bits because we
677 * aren't guaranteed a 64-bit compare&swap operation.)
678 * To save this code both the infamy of being fingered
679 * by a priggish news story and the indignity of being
680 * the target of a neo-puritan witch trial, we're
681 * carefully avoiding any colorful description of the
682 * likelihood of this condition -- but suffice it to
683 * say that it is only slightly more likely than the
684 * overflow of predicate cache IDs, as discussed in
685 * dtrace_predicate_create().
689 } while (dtrace_cas32(counter, oval, nval) != oval);
693 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
694 * uint8_t, a uint16_t, a uint32_t and a uint64_t.
704 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate)
706 if (dest < mstate->dtms_scratch_base)
709 if (dest + size < dest)
712 if (dest + size > mstate->dtms_scratch_ptr)
719 dtrace_canstore_statvar(uint64_t addr, size_t sz, size_t *remain,
720 dtrace_statvar_t **svars, int nsvars)
723 size_t maxglobalsize, maxlocalsize;
728 maxglobalsize = dtrace_statvar_maxsize + sizeof (uint64_t);
729 maxlocalsize = maxglobalsize * NCPU;
731 for (i = 0; i < nsvars; i++) {
732 dtrace_statvar_t *svar = svars[i];
736 if (svar == NULL || (size = svar->dtsv_size) == 0)
739 scope = svar->dtsv_var.dtdv_scope;
742 * We verify that our size is valid in the spirit of providing
743 * defense in depth: we want to prevent attackers from using
744 * DTrace to escalate an orthogonal kernel heap corruption bug
745 * into the ability to store to arbitrary locations in memory.
747 VERIFY((scope == DIFV_SCOPE_GLOBAL && size <= maxglobalsize) ||
748 (scope == DIFV_SCOPE_LOCAL && size <= maxlocalsize));
750 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data,
752 DTRACE_RANGE_REMAIN(remain, addr, svar->dtsv_data,
762 * Check to see if the address is within a memory region to which a store may
763 * be issued. This includes the DTrace scratch areas, and any DTrace variable
764 * region. The caller of dtrace_canstore() is responsible for performing any
765 * alignment checks that are needed before stores are actually executed.
768 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
769 dtrace_vstate_t *vstate)
771 return (dtrace_canstore_remains(addr, sz, NULL, mstate, vstate));
775 * Implementation of dtrace_canstore which communicates the upper bound of the
776 * allowed memory region.
779 dtrace_canstore_remains(uint64_t addr, size_t sz, size_t *remain,
780 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
783 * First, check to see if the address is in scratch space...
785 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base,
786 mstate->dtms_scratch_size)) {
787 DTRACE_RANGE_REMAIN(remain, addr, mstate->dtms_scratch_base,
788 mstate->dtms_scratch_size);
793 * Now check to see if it's a dynamic variable. This check will pick
794 * up both thread-local variables and any global dynamically-allocated
797 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base,
798 vstate->dtvs_dynvars.dtds_size)) {
799 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
800 uintptr_t base = (uintptr_t)dstate->dtds_base +
801 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t));
803 dtrace_dynvar_t *dvar;
806 * Before we assume that we can store here, we need to make
807 * sure that it isn't in our metadata -- storing to our
808 * dynamic variable metadata would corrupt our state. For
809 * the range to not include any dynamic variable metadata,
812 * (1) Start above the hash table that is at the base of
813 * the dynamic variable space
815 * (2) Have a starting chunk offset that is beyond the
816 * dtrace_dynvar_t that is at the base of every chunk
818 * (3) Not span a chunk boundary
820 * (4) Not be in the tuple space of a dynamic variable
826 chunkoffs = (addr - base) % dstate->dtds_chunksize;
828 if (chunkoffs < sizeof (dtrace_dynvar_t))
831 if (chunkoffs + sz > dstate->dtds_chunksize)
834 dvar = (dtrace_dynvar_t *)((uintptr_t)addr - chunkoffs);
836 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE)
839 if (chunkoffs < sizeof (dtrace_dynvar_t) +
840 ((dvar->dtdv_tuple.dtt_nkeys - 1) * sizeof (dtrace_key_t)))
843 DTRACE_RANGE_REMAIN(remain, addr, dvar, dstate->dtds_chunksize);
848 * Finally, check the static local and global variables. These checks
849 * take the longest, so we perform them last.
851 if (dtrace_canstore_statvar(addr, sz, remain,
852 vstate->dtvs_locals, vstate->dtvs_nlocals))
855 if (dtrace_canstore_statvar(addr, sz, remain,
856 vstate->dtvs_globals, vstate->dtvs_nglobals))
864 * Convenience routine to check to see if the address is within a memory
865 * region in which a load may be issued given the user's privilege level;
866 * if not, it sets the appropriate error flags and loads 'addr' into the
867 * illegal value slot.
869 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
870 * appropriate memory access protection.
873 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
874 dtrace_vstate_t *vstate)
876 return (dtrace_canload_remains(addr, sz, NULL, mstate, vstate));
880 * Implementation of dtrace_canload which communicates the uppoer bound of the
881 * allowed memory region.
884 dtrace_canload_remains(uint64_t addr, size_t sz, size_t *remain,
885 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
887 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
891 * If we hold the privilege to read from kernel memory, then
892 * everything is readable.
894 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
895 DTRACE_RANGE_REMAIN(remain, addr, addr, sz);
900 * You can obviously read that which you can store.
902 if (dtrace_canstore_remains(addr, sz, remain, mstate, vstate))
906 * We're allowed to read from our own string table.
908 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab,
909 mstate->dtms_difo->dtdo_strlen)) {
910 DTRACE_RANGE_REMAIN(remain, addr,
911 mstate->dtms_difo->dtdo_strtab,
912 mstate->dtms_difo->dtdo_strlen);
916 if (vstate->dtvs_state != NULL &&
917 dtrace_priv_proc(vstate->dtvs_state)) {
921 * When we have privileges to the current process, there are
922 * several context-related kernel structures that are safe to
923 * read, even absent the privilege to read from kernel memory.
924 * These reads are safe because these structures contain only
925 * state that (1) we're permitted to read, (2) is harmless or
926 * (3) contains pointers to additional kernel state that we're
927 * not permitted to read (and as such, do not present an
928 * opportunity for privilege escalation). Finally (and
929 * critically), because of the nature of their relation with
930 * the current thread context, the memory associated with these
931 * structures cannot change over the duration of probe context,
932 * and it is therefore impossible for this memory to be
933 * deallocated and reallocated as something else while it's
934 * being operated upon.
936 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) {
937 DTRACE_RANGE_REMAIN(remain, addr, curthread,
942 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr,
943 sz, curthread->t_procp, sizeof (proc_t))) {
944 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_procp,
949 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz,
950 curthread->t_cred, sizeof (cred_t))) {
951 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cred,
957 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz,
958 &(p->p_pidp->pid_id), sizeof (pid_t))) {
959 DTRACE_RANGE_REMAIN(remain, addr, &(p->p_pidp->pid_id),
964 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz,
965 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) {
966 DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cpu,
967 offsetof(cpu_t, cpu_pause_thread));
973 if ((fp = mstate->dtms_getf) != NULL) {
974 uintptr_t psz = sizeof (void *);
979 * When getf() returns a file_t, the enabling is implicitly
980 * granted the (transient) right to read the returned file_t
981 * as well as the v_path and v_op->vnop_name of the underlying
982 * vnode. These accesses are allowed after a successful
983 * getf() because the members that they refer to cannot change
984 * once set -- and the barrier logic in the kernel's closef()
985 * path assures that the file_t and its referenced vode_t
986 * cannot themselves be stale (that is, it impossible for
987 * either dtms_getf itself or its f_vnode member to reference
990 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) {
991 DTRACE_RANGE_REMAIN(remain, addr, fp, sizeof (file_t));
995 if ((vp = fp->f_vnode) != NULL) {
998 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) {
999 DTRACE_RANGE_REMAIN(remain, addr, &vp->v_path,
1003 slen = strlen(vp->v_path) + 1;
1004 if (DTRACE_INRANGE(addr, sz, vp->v_path, slen)) {
1005 DTRACE_RANGE_REMAIN(remain, addr, vp->v_path,
1011 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) {
1012 DTRACE_RANGE_REMAIN(remain, addr, &vp->v_op,
1018 if ((op = vp->v_op) != NULL &&
1019 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) {
1020 DTRACE_RANGE_REMAIN(remain, addr,
1021 &op->vnop_name, psz);
1025 if (op != NULL && op->vnop_name != NULL &&
1026 DTRACE_INRANGE(addr, sz, op->vnop_name,
1027 (slen = strlen(op->vnop_name) + 1))) {
1028 DTRACE_RANGE_REMAIN(remain, addr,
1029 op->vnop_name, slen);
1036 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
1042 * Convenience routine to check to see if a given string is within a memory
1043 * region in which a load may be issued given the user's privilege level;
1044 * this exists so that we don't need to issue unnecessary dtrace_strlen()
1045 * calls in the event that the user has all privileges.
1048 dtrace_strcanload(uint64_t addr, size_t sz, size_t *remain,
1049 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1054 * If we hold the privilege to read from kernel memory, then
1055 * everything is readable.
1057 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
1058 DTRACE_RANGE_REMAIN(remain, addr, addr, sz);
1063 * Even if the caller is uninterested in querying the remaining valid
1064 * range, it is required to ensure that the access is allowed.
1066 if (remain == NULL) {
1069 if (dtrace_canload_remains(addr, 0, remain, mstate, vstate)) {
1072 * Perform the strlen after determining the length of the
1073 * memory region which is accessible. This prevents timing
1074 * information from being used to find NULs in memory which is
1075 * not accessible to the caller.
1077 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr,
1079 if (strsz <= *remain) {
1088 * Convenience routine to check to see if a given variable is within a memory
1089 * region in which a load may be issued given the user's privilege level.
1092 dtrace_vcanload(void *src, dtrace_diftype_t *type, size_t *remain,
1093 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1096 ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1099 * Calculate the max size before performing any checks since even
1100 * DTRACE_ACCESS_KERNEL-credentialed callers expect that this function
1101 * return the max length via 'remain'.
1103 if (type->dtdt_kind == DIF_TYPE_STRING) {
1104 dtrace_state_t *state = vstate->dtvs_state;
1106 if (state != NULL) {
1107 sz = state->dts_options[DTRACEOPT_STRSIZE];
1110 * In helper context, we have a NULL state; fall back
1111 * to using the system-wide default for the string size
1114 sz = dtrace_strsize_default;
1117 sz = type->dtdt_size;
1121 * If we hold the privilege to read from kernel memory, then
1122 * everything is readable.
1124 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
1125 DTRACE_RANGE_REMAIN(remain, (uintptr_t)src, src, sz);
1129 if (type->dtdt_kind == DIF_TYPE_STRING) {
1130 return (dtrace_strcanload((uintptr_t)src, sz, remain, mstate,
1133 return (dtrace_canload_remains((uintptr_t)src, sz, remain, mstate,
1138 * Convert a string to a signed integer using safe loads.
1140 * NOTE: This function uses various macros from strtolctype.h to manipulate
1141 * digit values, etc -- these have all been checked to ensure they make
1142 * no additional function calls.
1145 dtrace_strtoll(char *input, int base, size_t limit)
1147 uintptr_t pos = (uintptr_t)input;
1150 boolean_t neg = B_FALSE;
1152 uintptr_t end = pos + limit;
1155 * Consume any whitespace preceding digits.
1157 while ((c = dtrace_load8(pos)) == ' ' || c == '\t')
1161 * Handle an explicit sign if one is present.
1163 if (c == '-' || c == '+') {
1166 c = dtrace_load8(++pos);
1170 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it
1173 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' ||
1174 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) {
1180 * Read in contiguous digits until the first non-digit character.
1182 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base;
1183 c = dtrace_load8(++pos))
1184 val = val * base + x;
1186 return (neg ? -val : val);
1190 * Compare two strings using safe loads.
1193 dtrace_strncmp(char *s1, char *s2, size_t limit)
1196 volatile uint16_t *flags;
1198 if (s1 == s2 || limit == 0)
1201 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
1207 c1 = dtrace_load8((uintptr_t)s1++);
1213 c2 = dtrace_load8((uintptr_t)s2++);
1218 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT));
1224 * Compute strlen(s) for a string using safe memory accesses. The additional
1225 * len parameter is used to specify a maximum length to ensure completion.
1228 dtrace_strlen(const char *s, size_t lim)
1232 for (len = 0; len != lim; len++) {
1233 if (dtrace_load8((uintptr_t)s++) == '\0')
1241 * Check if an address falls within a toxic region.
1244 dtrace_istoxic(uintptr_t kaddr, size_t size)
1246 uintptr_t taddr, tsize;
1249 for (i = 0; i < dtrace_toxranges; i++) {
1250 taddr = dtrace_toxrange[i].dtt_base;
1251 tsize = dtrace_toxrange[i].dtt_limit - taddr;
1253 if (kaddr - taddr < tsize) {
1254 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1255 cpu_core[curcpu].cpuc_dtrace_illval = kaddr;
1259 if (taddr - kaddr < size) {
1260 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1261 cpu_core[curcpu].cpuc_dtrace_illval = taddr;
1270 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe
1271 * memory specified by the DIF program. The dst is assumed to be safe memory
1272 * that we can store to directly because it is managed by DTrace. As with
1273 * standard bcopy, overlapping copies are handled properly.
1276 dtrace_bcopy(const void *src, void *dst, size_t len)
1280 const uint8_t *s2 = src;
1284 *s1++ = dtrace_load8((uintptr_t)s2++);
1285 } while (--len != 0);
1291 *--s1 = dtrace_load8((uintptr_t)--s2);
1292 } while (--len != 0);
1298 * Copy src to dst using safe memory accesses, up to either the specified
1299 * length, or the point that a nul byte is encountered. The src is assumed to
1300 * be unsafe memory specified by the DIF program. The dst is assumed to be
1301 * safe memory that we can store to directly because it is managed by DTrace.
1302 * Unlike dtrace_bcopy(), overlapping regions are not handled.
1305 dtrace_strcpy(const void *src, void *dst, size_t len)
1308 uint8_t *s1 = dst, c;
1309 const uint8_t *s2 = src;
1312 *s1++ = c = dtrace_load8((uintptr_t)s2++);
1313 } while (--len != 0 && c != '\0');
1318 * Copy src to dst, deriving the size and type from the specified (BYREF)
1319 * variable type. The src is assumed to be unsafe memory specified by the DIF
1320 * program. The dst is assumed to be DTrace variable memory that is of the
1321 * specified type; we assume that we can store to directly.
1324 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type, size_t limit)
1326 ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1328 if (type->dtdt_kind == DIF_TYPE_STRING) {
1329 dtrace_strcpy(src, dst, MIN(type->dtdt_size, limit));
1331 dtrace_bcopy(src, dst, MIN(type->dtdt_size, limit));
1336 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be
1337 * unsafe memory specified by the DIF program. The s2 data is assumed to be
1338 * safe memory that we can access directly because it is managed by DTrace.
1341 dtrace_bcmp(const void *s1, const void *s2, size_t len)
1343 volatile uint16_t *flags;
1345 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
1350 if (s1 == NULL || s2 == NULL)
1353 if (s1 != s2 && len != 0) {
1354 const uint8_t *ps1 = s1;
1355 const uint8_t *ps2 = s2;
1358 if (dtrace_load8((uintptr_t)ps1++) != *ps2++)
1360 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT));
1366 * Zero the specified region using a simple byte-by-byte loop. Note that this
1367 * is for safe DTrace-managed memory only.
1370 dtrace_bzero(void *dst, size_t len)
1374 for (cp = dst; len != 0; len--)
1379 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
1383 result[0] = addend1[0] + addend2[0];
1384 result[1] = addend1[1] + addend2[1] +
1385 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
1392 * Shift the 128-bit value in a by b. If b is positive, shift left.
1393 * If b is negative, shift right.
1396 dtrace_shift_128(uint64_t *a, int b)
1406 a[0] = a[1] >> (b - 64);
1410 mask = 1LL << (64 - b);
1412 a[0] |= ((a[1] & mask) << (64 - b));
1417 a[1] = a[0] << (b - 64);
1421 mask = a[0] >> (64 - b);
1429 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1430 * use native multiplication on those, and then re-combine into the
1431 * resulting 128-bit value.
1433 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1440 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
1442 uint64_t hi1, hi2, lo1, lo2;
1445 hi1 = factor1 >> 32;
1446 hi2 = factor2 >> 32;
1448 lo1 = factor1 & DT_MASK_LO;
1449 lo2 = factor2 & DT_MASK_LO;
1451 product[0] = lo1 * lo2;
1452 product[1] = hi1 * hi2;
1456 dtrace_shift_128(tmp, 32);
1457 dtrace_add_128(product, tmp, product);
1461 dtrace_shift_128(tmp, 32);
1462 dtrace_add_128(product, tmp, product);
1466 * This privilege check should be used by actions and subroutines to
1467 * verify that the user credentials of the process that enabled the
1468 * invoking ECB match the target credentials
1471 dtrace_priv_proc_common_user(dtrace_state_t *state)
1473 cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1476 * We should always have a non-NULL state cred here, since if cred
1477 * is null (anonymous tracing), we fast-path bypass this routine.
1479 ASSERT(s_cr != NULL);
1481 if ((cr = CRED()) != NULL &&
1482 s_cr->cr_uid == cr->cr_uid &&
1483 s_cr->cr_uid == cr->cr_ruid &&
1484 s_cr->cr_uid == cr->cr_suid &&
1485 s_cr->cr_gid == cr->cr_gid &&
1486 s_cr->cr_gid == cr->cr_rgid &&
1487 s_cr->cr_gid == cr->cr_sgid)
1494 * This privilege check should be used by actions and subroutines to
1495 * verify that the zone of the process that enabled the invoking ECB
1496 * matches the target credentials
1499 dtrace_priv_proc_common_zone(dtrace_state_t *state)
1502 cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1505 * We should always have a non-NULL state cred here, since if cred
1506 * is null (anonymous tracing), we fast-path bypass this routine.
1508 ASSERT(s_cr != NULL);
1510 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone)
1520 * This privilege check should be used by actions and subroutines to
1521 * verify that the process has not setuid or changed credentials.
1524 dtrace_priv_proc_common_nocd(void)
1528 if ((proc = ttoproc(curthread)) != NULL &&
1529 !(proc->p_flag & SNOCD))
1536 dtrace_priv_proc_destructive(dtrace_state_t *state)
1538 int action = state->dts_cred.dcr_action;
1540 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) &&
1541 dtrace_priv_proc_common_zone(state) == 0)
1544 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) &&
1545 dtrace_priv_proc_common_user(state) == 0)
1548 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) &&
1549 dtrace_priv_proc_common_nocd() == 0)
1555 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1561 dtrace_priv_proc_control(dtrace_state_t *state)
1563 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL)
1566 if (dtrace_priv_proc_common_zone(state) &&
1567 dtrace_priv_proc_common_user(state) &&
1568 dtrace_priv_proc_common_nocd())
1571 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1577 dtrace_priv_proc(dtrace_state_t *state)
1579 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC)
1582 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1588 dtrace_priv_kernel(dtrace_state_t *state)
1590 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL)
1593 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1599 dtrace_priv_kernel_destructive(dtrace_state_t *state)
1601 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE)
1604 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1610 * Determine if the dte_cond of the specified ECB allows for processing of
1611 * the current probe to continue. Note that this routine may allow continued
1612 * processing, but with access(es) stripped from the mstate's dtms_access
1616 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate,
1619 dtrace_probe_t *probe = ecb->dte_probe;
1620 dtrace_provider_t *prov = probe->dtpr_provider;
1621 dtrace_pops_t *pops = &prov->dtpv_pops;
1622 int mode = DTRACE_MODE_NOPRIV_DROP;
1624 ASSERT(ecb->dte_cond);
1627 if (pops->dtps_mode != NULL) {
1628 mode = pops->dtps_mode(prov->dtpv_arg,
1629 probe->dtpr_id, probe->dtpr_arg);
1631 ASSERT((mode & DTRACE_MODE_USER) ||
1632 (mode & DTRACE_MODE_KERNEL));
1633 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) ||
1634 (mode & DTRACE_MODE_NOPRIV_DROP));
1638 * If the dte_cond bits indicate that this consumer is only allowed to
1639 * see user-mode firings of this probe, call the provider's dtps_mode()
1640 * entry point to check that the probe was fired while in a user
1641 * context. If that's not the case, use the policy specified by the
1642 * provider to determine if we drop the probe or merely restrict
1645 if (ecb->dte_cond & DTRACE_COND_USERMODE) {
1646 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP);
1648 if (!(mode & DTRACE_MODE_USER)) {
1649 if (mode & DTRACE_MODE_NOPRIV_DROP)
1652 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS;
1658 * This is more subtle than it looks. We have to be absolutely certain
1659 * that CRED() isn't going to change out from under us so it's only
1660 * legit to examine that structure if we're in constrained situations.
1661 * Currently, the only times we'll this check is if a non-super-user
1662 * has enabled the profile or syscall providers -- providers that
1663 * allow visibility of all processes. For the profile case, the check
1664 * above will ensure that we're examining a user context.
1666 if (ecb->dte_cond & DTRACE_COND_OWNER) {
1668 cred_t *s_cr = state->dts_cred.dcr_cred;
1671 ASSERT(s_cr != NULL);
1673 if ((cr = CRED()) == NULL ||
1674 s_cr->cr_uid != cr->cr_uid ||
1675 s_cr->cr_uid != cr->cr_ruid ||
1676 s_cr->cr_uid != cr->cr_suid ||
1677 s_cr->cr_gid != cr->cr_gid ||
1678 s_cr->cr_gid != cr->cr_rgid ||
1679 s_cr->cr_gid != cr->cr_sgid ||
1680 (proc = ttoproc(curthread)) == NULL ||
1681 (proc->p_flag & SNOCD)) {
1682 if (mode & DTRACE_MODE_NOPRIV_DROP)
1686 mstate->dtms_access &= ~DTRACE_ACCESS_PROC;
1693 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not
1694 * in our zone, check to see if our mode policy is to restrict rather
1695 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC
1696 * and DTRACE_ACCESS_ARGS
1698 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
1700 cred_t *s_cr = state->dts_cred.dcr_cred;
1702 ASSERT(s_cr != NULL);
1704 if ((cr = CRED()) == NULL ||
1705 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) {
1706 if (mode & DTRACE_MODE_NOPRIV_DROP)
1709 mstate->dtms_access &=
1710 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS);
1719 * Note: not called from probe context. This function is called
1720 * asynchronously (and at a regular interval) from outside of probe context to
1721 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable
1722 * cleaning is explained in detail in <sys/dtrace_impl.h>.
1725 dtrace_dynvar_clean(dtrace_dstate_t *dstate)
1727 dtrace_dynvar_t *dirty;
1728 dtrace_dstate_percpu_t *dcpu;
1729 dtrace_dynvar_t **rinsep;
1732 for (i = 0; i < NCPU; i++) {
1733 dcpu = &dstate->dtds_percpu[i];
1734 rinsep = &dcpu->dtdsc_rinsing;
1737 * If the dirty list is NULL, there is no dirty work to do.
1739 if (dcpu->dtdsc_dirty == NULL)
1742 if (dcpu->dtdsc_rinsing != NULL) {
1744 * If the rinsing list is non-NULL, then it is because
1745 * this CPU was selected to accept another CPU's
1746 * dirty list -- and since that time, dirty buffers
1747 * have accumulated. This is a highly unlikely
1748 * condition, but we choose to ignore the dirty
1749 * buffers -- they'll be picked up a future cleanse.
1754 if (dcpu->dtdsc_clean != NULL) {
1756 * If the clean list is non-NULL, then we're in a
1757 * situation where a CPU has done deallocations (we
1758 * have a non-NULL dirty list) but no allocations (we
1759 * also have a non-NULL clean list). We can't simply
1760 * move the dirty list into the clean list on this
1761 * CPU, yet we also don't want to allow this condition
1762 * to persist, lest a short clean list prevent a
1763 * massive dirty list from being cleaned (which in
1764 * turn could lead to otherwise avoidable dynamic
1765 * drops). To deal with this, we look for some CPU
1766 * with a NULL clean list, NULL dirty list, and NULL
1767 * rinsing list -- and then we borrow this CPU to
1768 * rinse our dirty list.
1770 for (j = 0; j < NCPU; j++) {
1771 dtrace_dstate_percpu_t *rinser;
1773 rinser = &dstate->dtds_percpu[j];
1775 if (rinser->dtdsc_rinsing != NULL)
1778 if (rinser->dtdsc_dirty != NULL)
1781 if (rinser->dtdsc_clean != NULL)
1784 rinsep = &rinser->dtdsc_rinsing;
1790 * We were unable to find another CPU that
1791 * could accept this dirty list -- we are
1792 * therefore unable to clean it now.
1794 dtrace_dynvar_failclean++;
1802 * Atomically move the dirty list aside.
1805 dirty = dcpu->dtdsc_dirty;
1808 * Before we zap the dirty list, set the rinsing list.
1809 * (This allows for a potential assertion in
1810 * dtrace_dynvar(): if a free dynamic variable appears
1811 * on a hash chain, either the dirty list or the
1812 * rinsing list for some CPU must be non-NULL.)
1815 dtrace_membar_producer();
1816 } while (dtrace_casptr(&dcpu->dtdsc_dirty,
1817 dirty, NULL) != dirty);
1822 * We have no work to do; we can simply return.
1829 for (i = 0; i < NCPU; i++) {
1830 dcpu = &dstate->dtds_percpu[i];
1832 if (dcpu->dtdsc_rinsing == NULL)
1836 * We are now guaranteed that no hash chain contains a pointer
1837 * into this dirty list; we can make it clean.
1839 ASSERT(dcpu->dtdsc_clean == NULL);
1840 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing;
1841 dcpu->dtdsc_rinsing = NULL;
1845 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1846 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1847 * This prevents a race whereby a CPU incorrectly decides that
1848 * the state should be something other than DTRACE_DSTATE_CLEAN
1849 * after dtrace_dynvar_clean() has completed.
1853 dstate->dtds_state = DTRACE_DSTATE_CLEAN;
1857 * Depending on the value of the op parameter, this function looks-up,
1858 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an
1859 * allocation is requested, this function will return a pointer to a
1860 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1861 * variable can be allocated. If NULL is returned, the appropriate counter
1862 * will be incremented.
1865 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys,
1866 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op,
1867 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1869 uint64_t hashval = DTRACE_DYNHASH_VALID;
1870 dtrace_dynhash_t *hash = dstate->dtds_hash;
1871 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL;
1872 processorid_t me = curcpu, cpu = me;
1873 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me];
1874 size_t bucket, ksize;
1875 size_t chunksize = dstate->dtds_chunksize;
1876 uintptr_t kdata, lock, nstate;
1882 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time"
1883 * algorithm. For the by-value portions, we perform the algorithm in
1884 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a
1885 * bit, and seems to have only a minute effect on distribution. For
1886 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1887 * over each referenced byte. It's painful to do this, but it's much
1888 * better than pathological hash distribution. The efficacy of the
1889 * hashing algorithm (and a comparison with other algorithms) may be
1890 * found by running the ::dtrace_dynstat MDB dcmd.
1892 for (i = 0; i < nkeys; i++) {
1893 if (key[i].dttk_size == 0) {
1894 uint64_t val = key[i].dttk_value;
1896 hashval += (val >> 48) & 0xffff;
1897 hashval += (hashval << 10);
1898 hashval ^= (hashval >> 6);
1900 hashval += (val >> 32) & 0xffff;
1901 hashval += (hashval << 10);
1902 hashval ^= (hashval >> 6);
1904 hashval += (val >> 16) & 0xffff;
1905 hashval += (hashval << 10);
1906 hashval ^= (hashval >> 6);
1908 hashval += val & 0xffff;
1909 hashval += (hashval << 10);
1910 hashval ^= (hashval >> 6);
1913 * This is incredibly painful, but it beats the hell
1914 * out of the alternative.
1916 uint64_t j, size = key[i].dttk_size;
1917 uintptr_t base = (uintptr_t)key[i].dttk_value;
1919 if (!dtrace_canload(base, size, mstate, vstate))
1922 for (j = 0; j < size; j++) {
1923 hashval += dtrace_load8(base + j);
1924 hashval += (hashval << 10);
1925 hashval ^= (hashval >> 6);
1930 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
1933 hashval += (hashval << 3);
1934 hashval ^= (hashval >> 11);
1935 hashval += (hashval << 15);
1938 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1939 * comes out to be one of our two sentinel hash values. If this
1940 * actually happens, we set the hashval to be a value known to be a
1941 * non-sentinel value.
1943 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK)
1944 hashval = DTRACE_DYNHASH_VALID;
1947 * Yes, it's painful to do a divide here. If the cycle count becomes
1948 * important here, tricks can be pulled to reduce it. (However, it's
1949 * critical that hash collisions be kept to an absolute minimum;
1950 * they're much more painful than a divide.) It's better to have a
1951 * solution that generates few collisions and still keeps things
1952 * relatively simple.
1954 bucket = hashval % dstate->dtds_hashsize;
1956 if (op == DTRACE_DYNVAR_DEALLOC) {
1957 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock;
1960 while ((lock = *lockp) & 1)
1963 if (dtrace_casptr((volatile void *)lockp,
1964 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock)
1968 dtrace_membar_producer();
1973 lock = hash[bucket].dtdh_lock;
1975 dtrace_membar_consumer();
1977 start = hash[bucket].dtdh_chain;
1978 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK ||
1979 start->dtdv_hashval != DTRACE_DYNHASH_FREE ||
1980 op != DTRACE_DYNVAR_DEALLOC));
1982 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) {
1983 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple;
1984 dtrace_key_t *dkey = &dtuple->dtt_key[0];
1986 if (dvar->dtdv_hashval != hashval) {
1987 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) {
1989 * We've reached the sink, and therefore the
1990 * end of the hash chain; we can kick out of
1991 * the loop knowing that we have seen a valid
1992 * snapshot of state.
1994 ASSERT(dvar->dtdv_next == NULL);
1995 ASSERT(dvar == &dtrace_dynhash_sink);
1999 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) {
2001 * We've gone off the rails: somewhere along
2002 * the line, one of the members of this hash
2003 * chain was deleted. Note that we could also
2004 * detect this by simply letting this loop run
2005 * to completion, as we would eventually hit
2006 * the end of the dirty list. However, we
2007 * want to avoid running the length of the
2008 * dirty list unnecessarily (it might be quite
2009 * long), so we catch this as early as
2010 * possible by detecting the hash marker. In
2011 * this case, we simply set dvar to NULL and
2012 * break; the conditional after the loop will
2013 * send us back to top.
2022 if (dtuple->dtt_nkeys != nkeys)
2025 for (i = 0; i < nkeys; i++, dkey++) {
2026 if (dkey->dttk_size != key[i].dttk_size)
2027 goto next; /* size or type mismatch */
2029 if (dkey->dttk_size != 0) {
2031 (void *)(uintptr_t)key[i].dttk_value,
2032 (void *)(uintptr_t)dkey->dttk_value,
2036 if (dkey->dttk_value != key[i].dttk_value)
2041 if (op != DTRACE_DYNVAR_DEALLOC)
2044 ASSERT(dvar->dtdv_next == NULL ||
2045 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE);
2048 ASSERT(hash[bucket].dtdh_chain != dvar);
2049 ASSERT(start != dvar);
2050 ASSERT(prev->dtdv_next == dvar);
2051 prev->dtdv_next = dvar->dtdv_next;
2053 if (dtrace_casptr(&hash[bucket].dtdh_chain,
2054 start, dvar->dtdv_next) != start) {
2056 * We have failed to atomically swing the
2057 * hash table head pointer, presumably because
2058 * of a conflicting allocation on another CPU.
2059 * We need to reread the hash chain and try
2066 dtrace_membar_producer();
2069 * Now set the hash value to indicate that it's free.
2071 ASSERT(hash[bucket].dtdh_chain != dvar);
2072 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
2074 dtrace_membar_producer();
2077 * Set the next pointer to point at the dirty list, and
2078 * atomically swing the dirty pointer to the newly freed dvar.
2081 next = dcpu->dtdsc_dirty;
2082 dvar->dtdv_next = next;
2083 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next);
2086 * Finally, unlock this hash bucket.
2088 ASSERT(hash[bucket].dtdh_lock == lock);
2090 hash[bucket].dtdh_lock++;
2100 * If dvar is NULL, it is because we went off the rails:
2101 * one of the elements that we traversed in the hash chain
2102 * was deleted while we were traversing it. In this case,
2103 * we assert that we aren't doing a dealloc (deallocs lock
2104 * the hash bucket to prevent themselves from racing with
2105 * one another), and retry the hash chain traversal.
2107 ASSERT(op != DTRACE_DYNVAR_DEALLOC);
2111 if (op != DTRACE_DYNVAR_ALLOC) {
2113 * If we are not to allocate a new variable, we want to
2114 * return NULL now. Before we return, check that the value
2115 * of the lock word hasn't changed. If it has, we may have
2116 * seen an inconsistent snapshot.
2118 if (op == DTRACE_DYNVAR_NOALLOC) {
2119 if (hash[bucket].dtdh_lock != lock)
2122 ASSERT(op == DTRACE_DYNVAR_DEALLOC);
2123 ASSERT(hash[bucket].dtdh_lock == lock);
2125 hash[bucket].dtdh_lock++;
2132 * We need to allocate a new dynamic variable. The size we need is the
2133 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
2134 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
2135 * the size of any referred-to data (dsize). We then round the final
2136 * size up to the chunksize for allocation.
2138 for (ksize = 0, i = 0; i < nkeys; i++)
2139 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
2142 * This should be pretty much impossible, but could happen if, say,
2143 * strange DIF specified the tuple. Ideally, this should be an
2144 * assertion and not an error condition -- but that requires that the
2145 * chunksize calculation in dtrace_difo_chunksize() be absolutely
2146 * bullet-proof. (That is, it must not be able to be fooled by
2147 * malicious DIF.) Given the lack of backwards branches in DIF,
2148 * solving this would presumably not amount to solving the Halting
2149 * Problem -- but it still seems awfully hard.
2151 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) +
2152 ksize + dsize > chunksize) {
2153 dcpu->dtdsc_drops++;
2157 nstate = DTRACE_DSTATE_EMPTY;
2161 free = dcpu->dtdsc_free;
2164 dtrace_dynvar_t *clean = dcpu->dtdsc_clean;
2167 if (clean == NULL) {
2169 * We're out of dynamic variable space on
2170 * this CPU. Unless we have tried all CPUs,
2171 * we'll try to allocate from a different
2174 switch (dstate->dtds_state) {
2175 case DTRACE_DSTATE_CLEAN: {
2176 void *sp = &dstate->dtds_state;
2181 if (dcpu->dtdsc_dirty != NULL &&
2182 nstate == DTRACE_DSTATE_EMPTY)
2183 nstate = DTRACE_DSTATE_DIRTY;
2185 if (dcpu->dtdsc_rinsing != NULL)
2186 nstate = DTRACE_DSTATE_RINSING;
2188 dcpu = &dstate->dtds_percpu[cpu];
2193 (void) dtrace_cas32(sp,
2194 DTRACE_DSTATE_CLEAN, nstate);
2197 * To increment the correct bean
2198 * counter, take another lap.
2203 case DTRACE_DSTATE_DIRTY:
2204 dcpu->dtdsc_dirty_drops++;
2207 case DTRACE_DSTATE_RINSING:
2208 dcpu->dtdsc_rinsing_drops++;
2211 case DTRACE_DSTATE_EMPTY:
2212 dcpu->dtdsc_drops++;
2216 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP);
2221 * The clean list appears to be non-empty. We want to
2222 * move the clean list to the free list; we start by
2223 * moving the clean pointer aside.
2225 if (dtrace_casptr(&dcpu->dtdsc_clean,
2226 clean, NULL) != clean) {
2228 * We are in one of two situations:
2230 * (a) The clean list was switched to the
2231 * free list by another CPU.
2233 * (b) The clean list was added to by the
2236 * In either of these situations, we can
2237 * just reattempt the free list allocation.
2242 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE);
2245 * Now we'll move the clean list to our free list.
2246 * It's impossible for this to fail: the only way
2247 * the free list can be updated is through this
2248 * code path, and only one CPU can own the clean list.
2249 * Thus, it would only be possible for this to fail if
2250 * this code were racing with dtrace_dynvar_clean().
2251 * (That is, if dtrace_dynvar_clean() updated the clean
2252 * list, and we ended up racing to update the free
2253 * list.) This race is prevented by the dtrace_sync()
2254 * in dtrace_dynvar_clean() -- which flushes the
2255 * owners of the clean lists out before resetting
2258 dcpu = &dstate->dtds_percpu[me];
2259 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean);
2260 ASSERT(rval == NULL);
2265 new_free = dvar->dtdv_next;
2266 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free);
2269 * We have now allocated a new chunk. We copy the tuple keys into the
2270 * tuple array and copy any referenced key data into the data space
2271 * following the tuple array. As we do this, we relocate dttk_value
2272 * in the final tuple to point to the key data address in the chunk.
2274 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys];
2275 dvar->dtdv_data = (void *)(kdata + ksize);
2276 dvar->dtdv_tuple.dtt_nkeys = nkeys;
2278 for (i = 0; i < nkeys; i++) {
2279 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i];
2280 size_t kesize = key[i].dttk_size;
2284 (const void *)(uintptr_t)key[i].dttk_value,
2285 (void *)kdata, kesize);
2286 dkey->dttk_value = kdata;
2287 kdata += P2ROUNDUP(kesize, sizeof (uint64_t));
2289 dkey->dttk_value = key[i].dttk_value;
2292 dkey->dttk_size = kesize;
2295 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE);
2296 dvar->dtdv_hashval = hashval;
2297 dvar->dtdv_next = start;
2299 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start)
2303 * The cas has failed. Either another CPU is adding an element to
2304 * this hash chain, or another CPU is deleting an element from this
2305 * hash chain. The simplest way to deal with both of these cases
2306 * (though not necessarily the most efficient) is to free our
2307 * allocated block and re-attempt it all. Note that the free is
2308 * to the dirty list and _not_ to the free list. This is to prevent
2309 * races with allocators, above.
2311 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
2313 dtrace_membar_producer();
2316 free = dcpu->dtdsc_dirty;
2317 dvar->dtdv_next = free;
2318 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free);
2325 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg)
2327 if ((int64_t)nval < (int64_t)*oval)
2333 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg)
2335 if ((int64_t)nval > (int64_t)*oval)
2340 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr)
2342 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET;
2343 int64_t val = (int64_t)nval;
2346 for (i = 0; i < zero; i++) {
2347 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) {
2353 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) {
2354 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) {
2355 quanta[i - 1] += incr;
2360 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr;
2368 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr)
2370 uint64_t arg = *lquanta++;
2371 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
2372 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
2373 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
2374 int32_t val = (int32_t)nval, level;
2377 ASSERT(levels != 0);
2381 * This is an underflow.
2387 level = (val - base) / step;
2389 if (level < levels) {
2390 lquanta[level + 1] += incr;
2395 * This is an overflow.
2397 lquanta[levels + 1] += incr;
2401 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low,
2402 uint16_t high, uint16_t nsteps, int64_t value)
2404 int64_t this = 1, last, next;
2405 int base = 1, order;
2407 ASSERT(factor <= nsteps);
2408 ASSERT(nsteps % factor == 0);
2410 for (order = 0; order < low; order++)
2414 * If our value is less than our factor taken to the power of the
2415 * low order of magnitude, it goes into the zeroth bucket.
2417 if (value < (last = this))
2420 for (this *= factor; order <= high; order++) {
2421 int nbuckets = this > nsteps ? nsteps : this;
2423 if ((next = this * factor) < this) {
2425 * We should not generally get log/linear quantizations
2426 * with a high magnitude that allows 64-bits to
2427 * overflow, but we nonetheless protect against this
2428 * by explicitly checking for overflow, and clamping
2429 * our value accordingly.
2436 * If our value lies within this order of magnitude,
2437 * determine its position by taking the offset within
2438 * the order of magnitude, dividing by the bucket
2439 * width, and adding to our (accumulated) base.
2441 return (base + (value - last) / (this / nbuckets));
2444 base += nbuckets - (nbuckets / factor);
2450 * Our value is greater than or equal to our factor taken to the
2451 * power of one plus the high magnitude -- return the top bucket.
2457 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr)
2459 uint64_t arg = *llquanta++;
2460 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
2461 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
2462 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
2463 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
2465 llquanta[dtrace_aggregate_llquantize_bucket(factor,
2466 low, high, nsteps, nval)] += incr;
2471 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg)
2479 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg)
2481 int64_t snval = (int64_t)nval;
2488 * What we want to say here is:
2490 * data[2] += nval * nval;
2492 * But given that nval is 64-bit, we could easily overflow, so
2493 * we do this as 128-bit arithmetic.
2498 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp);
2499 dtrace_add_128(data + 2, tmp, data + 2);
2504 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg)
2511 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg)
2517 * Aggregate given the tuple in the principal data buffer, and the aggregating
2518 * action denoted by the specified dtrace_aggregation_t. The aggregation
2519 * buffer is specified as the buf parameter. This routine does not return
2520 * failure; if there is no space in the aggregation buffer, the data will be
2521 * dropped, and a corresponding counter incremented.
2524 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf,
2525 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg)
2527 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec;
2528 uint32_t i, ndx, size, fsize;
2529 uint32_t align = sizeof (uint64_t) - 1;
2530 dtrace_aggbuffer_t *agb;
2531 dtrace_aggkey_t *key;
2532 uint32_t hashval = 0, limit, isstr;
2533 caddr_t tomax, data, kdata;
2534 dtrace_actkind_t action;
2535 dtrace_action_t *act;
2541 if (!agg->dtag_hasarg) {
2543 * Currently, only quantize() and lquantize() take additional
2544 * arguments, and they have the same semantics: an increment
2545 * value that defaults to 1 when not present. If additional
2546 * aggregating actions take arguments, the setting of the
2547 * default argument value will presumably have to become more
2553 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION;
2554 size = rec->dtrd_offset - agg->dtag_base;
2555 fsize = size + rec->dtrd_size;
2557 ASSERT(dbuf->dtb_tomax != NULL);
2558 data = dbuf->dtb_tomax + offset + agg->dtag_base;
2560 if ((tomax = buf->dtb_tomax) == NULL) {
2561 dtrace_buffer_drop(buf);
2566 * The metastructure is always at the bottom of the buffer.
2568 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size -
2569 sizeof (dtrace_aggbuffer_t));
2571 if (buf->dtb_offset == 0) {
2573 * We just kludge up approximately 1/8th of the size to be
2574 * buckets. If this guess ends up being routinely
2575 * off-the-mark, we may need to dynamically readjust this
2576 * based on past performance.
2578 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t);
2580 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) <
2581 (uintptr_t)tomax || hashsize == 0) {
2583 * We've been given a ludicrously small buffer;
2584 * increment our drop count and leave.
2586 dtrace_buffer_drop(buf);
2591 * And now, a pathetic attempt to try to get a an odd (or
2592 * perchance, a prime) hash size for better hash distribution.
2594 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3))
2595 hashsize -= DTRACE_AGGHASHSIZE_SLEW;
2597 agb->dtagb_hashsize = hashsize;
2598 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb -
2599 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *));
2600 agb->dtagb_free = (uintptr_t)agb->dtagb_hash;
2602 for (i = 0; i < agb->dtagb_hashsize; i++)
2603 agb->dtagb_hash[i] = NULL;
2606 ASSERT(agg->dtag_first != NULL);
2607 ASSERT(agg->dtag_first->dta_intuple);
2610 * Calculate the hash value based on the key. Note that we _don't_
2611 * include the aggid in the hashing (but we will store it as part of
2612 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time"
2613 * algorithm: a simple, quick algorithm that has no known funnels, and
2614 * gets good distribution in practice. The efficacy of the hashing
2615 * algorithm (and a comparison with other algorithms) may be found by
2616 * running the ::dtrace_aggstat MDB dcmd.
2618 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2619 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2620 limit = i + act->dta_rec.dtrd_size;
2621 ASSERT(limit <= size);
2622 isstr = DTRACEACT_ISSTRING(act);
2624 for (; i < limit; i++) {
2626 hashval += (hashval << 10);
2627 hashval ^= (hashval >> 6);
2629 if (isstr && data[i] == '\0')
2634 hashval += (hashval << 3);
2635 hashval ^= (hashval >> 11);
2636 hashval += (hashval << 15);
2639 * Yes, the divide here is expensive -- but it's generally the least
2640 * of the performance issues given the amount of data that we iterate
2641 * over to compute hash values, compare data, etc.
2643 ndx = hashval % agb->dtagb_hashsize;
2645 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) {
2646 ASSERT((caddr_t)key >= tomax);
2647 ASSERT((caddr_t)key < tomax + buf->dtb_size);
2649 if (hashval != key->dtak_hashval || key->dtak_size != size)
2652 kdata = key->dtak_data;
2653 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size);
2655 for (act = agg->dtag_first; act->dta_intuple;
2656 act = act->dta_next) {
2657 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2658 limit = i + act->dta_rec.dtrd_size;
2659 ASSERT(limit <= size);
2660 isstr = DTRACEACT_ISSTRING(act);
2662 for (; i < limit; i++) {
2663 if (kdata[i] != data[i])
2666 if (isstr && data[i] == '\0')
2671 if (action != key->dtak_action) {
2673 * We are aggregating on the same value in the same
2674 * aggregation with two different aggregating actions.
2675 * (This should have been picked up in the compiler,
2676 * so we may be dealing with errant or devious DIF.)
2677 * This is an error condition; we indicate as much,
2680 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
2685 * This is a hit: we need to apply the aggregator to
2686 * the value at this key.
2688 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg);
2695 * We didn't find it. We need to allocate some zero-filled space,
2696 * link it into the hash table appropriately, and apply the aggregator
2697 * to the (zero-filled) value.
2699 offs = buf->dtb_offset;
2700 while (offs & (align - 1))
2701 offs += sizeof (uint32_t);
2704 * If we don't have enough room to both allocate a new key _and_
2705 * its associated data, increment the drop count and return.
2707 if ((uintptr_t)tomax + offs + fsize >
2708 agb->dtagb_free - sizeof (dtrace_aggkey_t)) {
2709 dtrace_buffer_drop(buf);
2714 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1)));
2715 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t));
2716 agb->dtagb_free -= sizeof (dtrace_aggkey_t);
2718 key->dtak_data = kdata = tomax + offs;
2719 buf->dtb_offset = offs + fsize;
2722 * Now copy the data across.
2724 *((dtrace_aggid_t *)kdata) = agg->dtag_id;
2726 for (i = sizeof (dtrace_aggid_t); i < size; i++)
2730 * Because strings are not zeroed out by default, we need to iterate
2731 * looking for actions that store strings, and we need to explicitly
2732 * pad these strings out with zeroes.
2734 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2737 if (!DTRACEACT_ISSTRING(act))
2740 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2741 limit = i + act->dta_rec.dtrd_size;
2742 ASSERT(limit <= size);
2744 for (nul = 0; i < limit; i++) {
2750 if (data[i] != '\0')
2757 for (i = size; i < fsize; i++)
2760 key->dtak_hashval = hashval;
2761 key->dtak_size = size;
2762 key->dtak_action = action;
2763 key->dtak_next = agb->dtagb_hash[ndx];
2764 agb->dtagb_hash[ndx] = key;
2767 * Finally, apply the aggregator.
2769 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial;
2770 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg);
2774 * Given consumer state, this routine finds a speculation in the INACTIVE
2775 * state and transitions it into the ACTIVE state. If there is no speculation
2776 * in the INACTIVE state, 0 is returned. In this case, no error counter is
2777 * incremented -- it is up to the caller to take appropriate action.
2780 dtrace_speculation(dtrace_state_t *state)
2783 dtrace_speculation_state_t current;
2784 uint32_t *stat = &state->dts_speculations_unavail, count;
2786 while (i < state->dts_nspeculations) {
2787 dtrace_speculation_t *spec = &state->dts_speculations[i];
2789 current = spec->dtsp_state;
2791 if (current != DTRACESPEC_INACTIVE) {
2792 if (current == DTRACESPEC_COMMITTINGMANY ||
2793 current == DTRACESPEC_COMMITTING ||
2794 current == DTRACESPEC_DISCARDING)
2795 stat = &state->dts_speculations_busy;
2800 if (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2801 current, DTRACESPEC_ACTIVE) == current)
2806 * We couldn't find a speculation. If we found as much as a single
2807 * busy speculation buffer, we'll attribute this failure as "busy"
2808 * instead of "unavail".
2812 } while (dtrace_cas32(stat, count, count + 1) != count);
2818 * This routine commits an active speculation. If the specified speculation
2819 * is not in a valid state to perform a commit(), this routine will silently do
2820 * nothing. The state of the specified speculation is transitioned according
2821 * to the state transition diagram outlined in <sys/dtrace_impl.h>
2824 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu,
2825 dtrace_specid_t which)
2827 dtrace_speculation_t *spec;
2828 dtrace_buffer_t *src, *dest;
2829 uintptr_t daddr, saddr, dlimit, slimit;
2830 dtrace_speculation_state_t current, new = 0;
2837 if (which > state->dts_nspeculations) {
2838 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2842 spec = &state->dts_speculations[which - 1];
2843 src = &spec->dtsp_buffer[cpu];
2844 dest = &state->dts_buffer[cpu];
2847 current = spec->dtsp_state;
2849 if (current == DTRACESPEC_COMMITTINGMANY)
2853 case DTRACESPEC_INACTIVE:
2854 case DTRACESPEC_DISCARDING:
2857 case DTRACESPEC_COMMITTING:
2859 * This is only possible if we are (a) commit()'ing
2860 * without having done a prior speculate() on this CPU
2861 * and (b) racing with another commit() on a different
2862 * CPU. There's nothing to do -- we just assert that
2865 ASSERT(src->dtb_offset == 0);
2868 case DTRACESPEC_ACTIVE:
2869 new = DTRACESPEC_COMMITTING;
2872 case DTRACESPEC_ACTIVEONE:
2874 * This speculation is active on one CPU. If our
2875 * buffer offset is non-zero, we know that the one CPU
2876 * must be us. Otherwise, we are committing on a
2877 * different CPU from the speculate(), and we must
2878 * rely on being asynchronously cleaned.
2880 if (src->dtb_offset != 0) {
2881 new = DTRACESPEC_COMMITTING;
2886 case DTRACESPEC_ACTIVEMANY:
2887 new = DTRACESPEC_COMMITTINGMANY;
2893 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2894 current, new) != current);
2897 * We have set the state to indicate that we are committing this
2898 * speculation. Now reserve the necessary space in the destination
2901 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset,
2902 sizeof (uint64_t), state, NULL)) < 0) {
2903 dtrace_buffer_drop(dest);
2908 * We have sufficient space to copy the speculative buffer into the
2909 * primary buffer. First, modify the speculative buffer, filling
2910 * in the timestamp of all entries with the current time. The data
2911 * must have the commit() time rather than the time it was traced,
2912 * so that all entries in the primary buffer are in timestamp order.
2914 timestamp = dtrace_gethrtime();
2915 saddr = (uintptr_t)src->dtb_tomax;
2916 slimit = saddr + src->dtb_offset;
2917 while (saddr < slimit) {
2919 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr;
2921 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
2922 saddr += sizeof (dtrace_epid_t);
2925 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs);
2926 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size;
2928 ASSERT3U(saddr + size, <=, slimit);
2929 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t));
2930 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX);
2932 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp);
2938 * Copy the buffer across. (Note that this is a
2939 * highly subobtimal bcopy(); in the unlikely event that this becomes
2940 * a serious performance issue, a high-performance DTrace-specific
2941 * bcopy() should obviously be invented.)
2943 daddr = (uintptr_t)dest->dtb_tomax + offs;
2944 dlimit = daddr + src->dtb_offset;
2945 saddr = (uintptr_t)src->dtb_tomax;
2948 * First, the aligned portion.
2950 while (dlimit - daddr >= sizeof (uint64_t)) {
2951 *((uint64_t *)daddr) = *((uint64_t *)saddr);
2953 daddr += sizeof (uint64_t);
2954 saddr += sizeof (uint64_t);
2958 * Now any left-over bit...
2960 while (dlimit - daddr)
2961 *((uint8_t *)daddr++) = *((uint8_t *)saddr++);
2964 * Finally, commit the reserved space in the destination buffer.
2966 dest->dtb_offset = offs + src->dtb_offset;
2970 * If we're lucky enough to be the only active CPU on this speculation
2971 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2973 if (current == DTRACESPEC_ACTIVE ||
2974 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) {
2975 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state,
2976 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE);
2978 ASSERT(rval == DTRACESPEC_COMMITTING);
2981 src->dtb_offset = 0;
2982 src->dtb_xamot_drops += src->dtb_drops;
2987 * This routine discards an active speculation. If the specified speculation
2988 * is not in a valid state to perform a discard(), this routine will silently
2989 * do nothing. The state of the specified speculation is transitioned
2990 * according to the state transition diagram outlined in <sys/dtrace_impl.h>
2993 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu,
2994 dtrace_specid_t which)
2996 dtrace_speculation_t *spec;
2997 dtrace_speculation_state_t current, new = 0;
2998 dtrace_buffer_t *buf;
3003 if (which > state->dts_nspeculations) {
3004 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3008 spec = &state->dts_speculations[which - 1];
3009 buf = &spec->dtsp_buffer[cpu];
3012 current = spec->dtsp_state;
3015 case DTRACESPEC_INACTIVE:
3016 case DTRACESPEC_COMMITTINGMANY:
3017 case DTRACESPEC_COMMITTING:
3018 case DTRACESPEC_DISCARDING:
3021 case DTRACESPEC_ACTIVE:
3022 case DTRACESPEC_ACTIVEMANY:
3023 new = DTRACESPEC_DISCARDING;
3026 case DTRACESPEC_ACTIVEONE:
3027 if (buf->dtb_offset != 0) {
3028 new = DTRACESPEC_INACTIVE;
3030 new = DTRACESPEC_DISCARDING;
3037 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
3038 current, new) != current);
3040 buf->dtb_offset = 0;
3045 * Note: not called from probe context. This function is called
3046 * asynchronously from cross call context to clean any speculations that are
3047 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be
3048 * transitioned back to the INACTIVE state until all CPUs have cleaned the
3052 dtrace_speculation_clean_here(dtrace_state_t *state)
3054 dtrace_icookie_t cookie;
3055 processorid_t cpu = curcpu;
3056 dtrace_buffer_t *dest = &state->dts_buffer[cpu];
3059 cookie = dtrace_interrupt_disable();
3061 if (dest->dtb_tomax == NULL) {
3062 dtrace_interrupt_enable(cookie);
3066 for (i = 0; i < state->dts_nspeculations; i++) {
3067 dtrace_speculation_t *spec = &state->dts_speculations[i];
3068 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu];
3070 if (src->dtb_tomax == NULL)
3073 if (spec->dtsp_state == DTRACESPEC_DISCARDING) {
3074 src->dtb_offset = 0;
3078 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
3081 if (src->dtb_offset == 0)
3084 dtrace_speculation_commit(state, cpu, i + 1);
3087 dtrace_interrupt_enable(cookie);
3091 * Note: not called from probe context. This function is called
3092 * asynchronously (and at a regular interval) to clean any speculations that
3093 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there
3094 * is work to be done, it cross calls all CPUs to perform that work;
3095 * COMMITMANY and DISCARDING speculations may not be transitioned back to the
3096 * INACTIVE state until they have been cleaned by all CPUs.
3099 dtrace_speculation_clean(dtrace_state_t *state)
3104 for (i = 0; i < state->dts_nspeculations; i++) {
3105 dtrace_speculation_t *spec = &state->dts_speculations[i];
3107 ASSERT(!spec->dtsp_cleaning);
3109 if (spec->dtsp_state != DTRACESPEC_DISCARDING &&
3110 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
3114 spec->dtsp_cleaning = 1;
3120 dtrace_xcall(DTRACE_CPUALL,
3121 (dtrace_xcall_t)dtrace_speculation_clean_here, state);
3124 * We now know that all CPUs have committed or discarded their
3125 * speculation buffers, as appropriate. We can now set the state
3128 for (i = 0; i < state->dts_nspeculations; i++) {
3129 dtrace_speculation_t *spec = &state->dts_speculations[i];
3130 dtrace_speculation_state_t current, new;
3132 if (!spec->dtsp_cleaning)
3135 current = spec->dtsp_state;
3136 ASSERT(current == DTRACESPEC_DISCARDING ||
3137 current == DTRACESPEC_COMMITTINGMANY);
3139 new = DTRACESPEC_INACTIVE;
3141 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new);
3142 ASSERT(rv == current);
3143 spec->dtsp_cleaning = 0;
3148 * Called as part of a speculate() to get the speculative buffer associated
3149 * with a given speculation. Returns NULL if the specified speculation is not
3150 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and
3151 * the active CPU is not the specified CPU -- the speculation will be
3152 * atomically transitioned into the ACTIVEMANY state.
3154 static dtrace_buffer_t *
3155 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid,
3156 dtrace_specid_t which)
3158 dtrace_speculation_t *spec;
3159 dtrace_speculation_state_t current, new = 0;
3160 dtrace_buffer_t *buf;
3165 if (which > state->dts_nspeculations) {
3166 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3170 spec = &state->dts_speculations[which - 1];
3171 buf = &spec->dtsp_buffer[cpuid];
3174 current = spec->dtsp_state;
3177 case DTRACESPEC_INACTIVE:
3178 case DTRACESPEC_COMMITTINGMANY:
3179 case DTRACESPEC_DISCARDING:
3182 case DTRACESPEC_COMMITTING:
3183 ASSERT(buf->dtb_offset == 0);
3186 case DTRACESPEC_ACTIVEONE:
3188 * This speculation is currently active on one CPU.
3189 * Check the offset in the buffer; if it's non-zero,
3190 * that CPU must be us (and we leave the state alone).
3191 * If it's zero, assume that we're starting on a new
3192 * CPU -- and change the state to indicate that the
3193 * speculation is active on more than one CPU.
3195 if (buf->dtb_offset != 0)
3198 new = DTRACESPEC_ACTIVEMANY;
3201 case DTRACESPEC_ACTIVEMANY:
3204 case DTRACESPEC_ACTIVE:
3205 new = DTRACESPEC_ACTIVEONE;
3211 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
3212 current, new) != current);
3214 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY);
3219 * Return a string. In the event that the user lacks the privilege to access
3220 * arbitrary kernel memory, we copy the string out to scratch memory so that we
3221 * don't fail access checking.
3223 * dtrace_dif_variable() uses this routine as a helper for various
3224 * builtin values such as 'execname' and 'probefunc.'
3227 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state,
3228 dtrace_mstate_t *mstate)
3230 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3235 * The easy case: this probe is allowed to read all of memory, so
3236 * we can just return this as a vanilla pointer.
3238 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
3242 * This is the tougher case: we copy the string in question from
3243 * kernel memory into scratch memory and return it that way: this
3244 * ensures that we won't trip up when access checking tests the
3245 * BYREF return value.
3247 strsz = dtrace_strlen((char *)addr, size) + 1;
3249 if (mstate->dtms_scratch_ptr + strsz >
3250 mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
3251 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3255 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
3257 ret = mstate->dtms_scratch_ptr;
3258 mstate->dtms_scratch_ptr += strsz;
3263 * Return a string from a memoy address which is known to have one or
3264 * more concatenated, individually zero terminated, sub-strings.
3265 * In the event that the user lacks the privilege to access
3266 * arbitrary kernel memory, we copy the string out to scratch memory so that we
3267 * don't fail access checking.
3269 * dtrace_dif_variable() uses this routine as a helper for various
3270 * builtin values such as 'execargs'.
3273 dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state,
3274 dtrace_mstate_t *mstate)
3280 if (mstate->dtms_scratch_ptr + strsz >
3281 mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
3282 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3286 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
3289 /* Replace sub-string termination characters with a space. */
3290 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1;
3295 ret = mstate->dtms_scratch_ptr;
3296 mstate->dtms_scratch_ptr += strsz;
3301 * This function implements the DIF emulator's variable lookups. The emulator
3302 * passes a reserved variable identifier and optional built-in array index.
3305 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v,
3309 * If we're accessing one of the uncached arguments, we'll turn this
3310 * into a reference in the args array.
3312 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) {
3313 ndx = v - DIF_VAR_ARG0;
3319 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS);
3320 if (ndx >= sizeof (mstate->dtms_arg) /
3321 sizeof (mstate->dtms_arg[0])) {
3322 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3323 dtrace_provider_t *pv;
3326 pv = mstate->dtms_probe->dtpr_provider;
3327 if (pv->dtpv_pops.dtps_getargval != NULL)
3328 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg,
3329 mstate->dtms_probe->dtpr_id,
3330 mstate->dtms_probe->dtpr_arg, ndx, aframes);
3332 val = dtrace_getarg(ndx, aframes);
3335 * This is regrettably required to keep the compiler
3336 * from tail-optimizing the call to dtrace_getarg().
3337 * The condition always evaluates to true, but the
3338 * compiler has no way of figuring that out a priori.
3339 * (None of this would be necessary if the compiler
3340 * could be relied upon to _always_ tail-optimize
3341 * the call to dtrace_getarg() -- but it can't.)
3343 if (mstate->dtms_probe != NULL)
3349 return (mstate->dtms_arg[ndx]);
3352 case DIF_VAR_UREGS: {
3355 if (!dtrace_priv_proc(state))
3358 if ((lwp = curthread->t_lwp) == NULL) {
3359 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
3360 cpu_core[curcpu].cpuc_dtrace_illval = NULL;
3364 return (dtrace_getreg(lwp->lwp_regs, ndx));
3368 case DIF_VAR_UREGS: {
3369 struct trapframe *tframe;
3371 if (!dtrace_priv_proc(state))
3374 if ((tframe = curthread->td_frame) == NULL) {
3375 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
3376 cpu_core[curcpu].cpuc_dtrace_illval = 0;
3380 return (dtrace_getreg(tframe, ndx));
3384 case DIF_VAR_CURTHREAD:
3385 if (!dtrace_priv_proc(state))
3387 return ((uint64_t)(uintptr_t)curthread);
3389 case DIF_VAR_TIMESTAMP:
3390 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
3391 mstate->dtms_timestamp = dtrace_gethrtime();
3392 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP;
3394 return (mstate->dtms_timestamp);
3396 case DIF_VAR_VTIMESTAMP:
3397 ASSERT(dtrace_vtime_references != 0);
3398 return (curthread->t_dtrace_vtime);
3400 case DIF_VAR_WALLTIMESTAMP:
3401 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) {
3402 mstate->dtms_walltimestamp = dtrace_gethrestime();
3403 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP;
3405 return (mstate->dtms_walltimestamp);
3409 if (!dtrace_priv_kernel(state))
3411 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) {
3412 mstate->dtms_ipl = dtrace_getipl();
3413 mstate->dtms_present |= DTRACE_MSTATE_IPL;
3415 return (mstate->dtms_ipl);
3419 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID);
3420 return (mstate->dtms_epid);
3423 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3424 return (mstate->dtms_probe->dtpr_id);
3426 case DIF_VAR_STACKDEPTH:
3427 if (!dtrace_priv_kernel(state))
3429 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) {
3430 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3432 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes);
3433 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH;
3435 return (mstate->dtms_stackdepth);
3437 case DIF_VAR_USTACKDEPTH:
3438 if (!dtrace_priv_proc(state))
3440 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) {
3442 * See comment in DIF_VAR_PID.
3444 if (DTRACE_ANCHORED(mstate->dtms_probe) &&
3446 mstate->dtms_ustackdepth = 0;
3448 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3449 mstate->dtms_ustackdepth =
3450 dtrace_getustackdepth();
3451 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3453 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH;
3455 return (mstate->dtms_ustackdepth);
3457 case DIF_VAR_CALLER:
3458 if (!dtrace_priv_kernel(state))
3460 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) {
3461 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3463 if (!DTRACE_ANCHORED(mstate->dtms_probe)) {
3465 * If this is an unanchored probe, we are
3466 * required to go through the slow path:
3467 * dtrace_caller() only guarantees correct
3468 * results for anchored probes.
3470 pc_t caller[2] = {0, 0};
3472 dtrace_getpcstack(caller, 2, aframes,
3473 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]);
3474 mstate->dtms_caller = caller[1];
3475 } else if ((mstate->dtms_caller =
3476 dtrace_caller(aframes)) == -1) {
3478 * We have failed to do this the quick way;
3479 * we must resort to the slower approach of
3480 * calling dtrace_getpcstack().
3484 dtrace_getpcstack(&caller, 1, aframes, NULL);
3485 mstate->dtms_caller = caller;
3488 mstate->dtms_present |= DTRACE_MSTATE_CALLER;
3490 return (mstate->dtms_caller);
3492 case DIF_VAR_UCALLER:
3493 if (!dtrace_priv_proc(state))
3496 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) {
3500 * dtrace_getupcstack() fills in the first uint64_t
3501 * with the current PID. The second uint64_t will
3502 * be the program counter at user-level. The third
3503 * uint64_t will contain the caller, which is what
3507 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3508 dtrace_getupcstack(ustack, 3);
3509 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3510 mstate->dtms_ucaller = ustack[2];
3511 mstate->dtms_present |= DTRACE_MSTATE_UCALLER;
3514 return (mstate->dtms_ucaller);
3516 case DIF_VAR_PROBEPROV:
3517 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3518 return (dtrace_dif_varstr(
3519 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name,
3522 case DIF_VAR_PROBEMOD:
3523 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3524 return (dtrace_dif_varstr(
3525 (uintptr_t)mstate->dtms_probe->dtpr_mod,
3528 case DIF_VAR_PROBEFUNC:
3529 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3530 return (dtrace_dif_varstr(
3531 (uintptr_t)mstate->dtms_probe->dtpr_func,
3534 case DIF_VAR_PROBENAME:
3535 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3536 return (dtrace_dif_varstr(
3537 (uintptr_t)mstate->dtms_probe->dtpr_name,
3541 if (!dtrace_priv_proc(state))
3546 * Note that we are assuming that an unanchored probe is
3547 * always due to a high-level interrupt. (And we're assuming
3548 * that there is only a single high level interrupt.)
3550 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3551 return (pid0.pid_id);
3554 * It is always safe to dereference one's own t_procp pointer:
3555 * it always points to a valid, allocated proc structure.
3556 * Further, it is always safe to dereference the p_pidp member
3557 * of one's own proc structure. (These are truisms becuase
3558 * threads and processes don't clean up their own state --
3559 * they leave that task to whomever reaps them.)
3561 return ((uint64_t)curthread->t_procp->p_pidp->pid_id);
3563 return ((uint64_t)curproc->p_pid);
3567 if (!dtrace_priv_proc(state))
3572 * See comment in DIF_VAR_PID.
3574 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3575 return (pid0.pid_id);
3578 * It is always safe to dereference one's own t_procp pointer:
3579 * it always points to a valid, allocated proc structure.
3580 * (This is true because threads don't clean up their own
3581 * state -- they leave that task to whomever reaps them.)
3583 return ((uint64_t)curthread->t_procp->p_ppid);
3585 if (curproc->p_pid == proc0.p_pid)
3586 return (curproc->p_pid);
3588 return (curproc->p_pptr->p_pid);
3594 * See comment in DIF_VAR_PID.
3596 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3600 return ((uint64_t)curthread->t_tid);
3602 case DIF_VAR_EXECARGS: {
3603 struct pargs *p_args = curthread->td_proc->p_args;
3608 return (dtrace_dif_varstrz(
3609 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate));
3612 case DIF_VAR_EXECNAME:
3614 if (!dtrace_priv_proc(state))
3618 * See comment in DIF_VAR_PID.
3620 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3621 return ((uint64_t)(uintptr_t)p0.p_user.u_comm);
3624 * It is always safe to dereference one's own t_procp pointer:
3625 * it always points to a valid, allocated proc structure.
3626 * (This is true because threads don't clean up their own
3627 * state -- they leave that task to whomever reaps them.)
3629 return (dtrace_dif_varstr(
3630 (uintptr_t)curthread->t_procp->p_user.u_comm,
3633 return (dtrace_dif_varstr(
3634 (uintptr_t) curthread->td_proc->p_comm, state, mstate));
3637 case DIF_VAR_ZONENAME:
3639 if (!dtrace_priv_proc(state))
3643 * See comment in DIF_VAR_PID.
3645 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3646 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name);
3649 * It is always safe to dereference one's own t_procp pointer:
3650 * it always points to a valid, allocated proc structure.
3651 * (This is true because threads don't clean up their own
3652 * state -- they leave that task to whomever reaps them.)
3654 return (dtrace_dif_varstr(
3655 (uintptr_t)curthread->t_procp->p_zone->zone_name,
3662 if (!dtrace_priv_proc(state))
3667 * See comment in DIF_VAR_PID.
3669 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3670 return ((uint64_t)p0.p_cred->cr_uid);
3673 * It is always safe to dereference one's own t_procp pointer:
3674 * it always points to a valid, allocated proc structure.
3675 * (This is true because threads don't clean up their own
3676 * state -- they leave that task to whomever reaps them.)
3678 * Additionally, it is safe to dereference one's own process
3679 * credential, since this is never NULL after process birth.
3681 return ((uint64_t)curthread->t_procp->p_cred->cr_uid);
3683 return ((uint64_t)curthread->td_ucred->cr_uid);
3687 if (!dtrace_priv_proc(state))
3692 * See comment in DIF_VAR_PID.
3694 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3695 return ((uint64_t)p0.p_cred->cr_gid);
3698 * It is always safe to dereference one's own t_procp pointer:
3699 * it always points to a valid, allocated proc structure.
3700 * (This is true because threads don't clean up their own
3701 * state -- they leave that task to whomever reaps them.)
3703 * Additionally, it is safe to dereference one's own process
3704 * credential, since this is never NULL after process birth.
3706 return ((uint64_t)curthread->t_procp->p_cred->cr_gid);
3708 return ((uint64_t)curthread->td_ucred->cr_gid);
3711 case DIF_VAR_ERRNO: {
3714 if (!dtrace_priv_proc(state))
3718 * See comment in DIF_VAR_PID.
3720 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3724 * It is always safe to dereference one's own t_lwp pointer in
3725 * the event that this pointer is non-NULL. (This is true
3726 * because threads and lwps don't clean up their own state --
3727 * they leave that task to whomever reaps them.)
3729 if ((lwp = curthread->t_lwp) == NULL)
3732 return ((uint64_t)lwp->lwp_errno);
3734 return (curthread->td_errno);
3743 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3749 typedef enum dtrace_json_state {
3750 DTRACE_JSON_REST = 1,
3753 DTRACE_JSON_STRING_ESCAPE,
3754 DTRACE_JSON_STRING_ESCAPE_UNICODE,
3758 DTRACE_JSON_IDENTIFIER,
3760 DTRACE_JSON_NUMBER_FRAC,
3761 DTRACE_JSON_NUMBER_EXP,
3762 DTRACE_JSON_COLLECT_OBJECT
3763 } dtrace_json_state_t;
3766 * This function possesses just enough knowledge about JSON to extract a single
3767 * value from a JSON string and store it in the scratch buffer. It is able
3768 * to extract nested object values, and members of arrays by index.
3770 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to
3771 * be looked up as we descend into the object tree. e.g.
3773 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL
3776 * The run time of this function must be bounded above by strsize to limit the
3777 * amount of work done in probe context. As such, it is implemented as a
3778 * simple state machine, reading one character at a time using safe loads
3779 * until we find the requested element, hit a parsing error or run off the
3780 * end of the object or string.
3782 * As there is no way for a subroutine to return an error without interrupting
3783 * clause execution, we simply return NULL in the event of a missing key or any
3784 * other error condition. Each NULL return in this function is commented with
3785 * the error condition it represents -- parsing or otherwise.
3787 * The set of states for the state machine closely matches the JSON
3788 * specification (http://json.org/). Briefly:
3791 * Skip whitespace until we find either a top-level Object, moving
3792 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE.
3794 * DTRACE_JSON_OBJECT:
3795 * Locate the next key String in an Object. Sets a flag to denote
3796 * the next String as a key string and moves to DTRACE_JSON_STRING.
3798 * DTRACE_JSON_COLON:
3799 * Skip whitespace until we find the colon that separates key Strings
3800 * from their values. Once found, move to DTRACE_JSON_VALUE.
3802 * DTRACE_JSON_VALUE:
3803 * Detects the type of the next value (String, Number, Identifier, Object
3804 * or Array) and routes to the states that process that type. Here we also
3805 * deal with the element selector list if we are requested to traverse down
3806 * into the object tree.
3808 * DTRACE_JSON_COMMA:
3809 * Skip whitespace until we find the comma that separates key-value pairs
3810 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays
3811 * (similarly DTRACE_JSON_VALUE). All following literal value processing
3812 * states return to this state at the end of their value, unless otherwise
3815 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP:
3816 * Processes a Number literal from the JSON, including any exponent
3817 * component that may be present. Numbers are returned as strings, which
3818 * may be passed to strtoll() if an integer is required.
3820 * DTRACE_JSON_IDENTIFIER:
3821 * Processes a "true", "false" or "null" literal in the JSON.
3823 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE,
3824 * DTRACE_JSON_STRING_ESCAPE_UNICODE:
3825 * Processes a String literal from the JSON, whether the String denotes
3826 * a key, a value or part of a larger Object. Handles all escape sequences
3827 * present in the specification, including four-digit unicode characters,
3828 * but merely includes the escape sequence without converting it to the
3829 * actual escaped character. If the String is flagged as a key, we
3830 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA.
3832 * DTRACE_JSON_COLLECT_OBJECT:
3833 * This state collects an entire Object (or Array), correctly handling
3834 * embedded strings. If the full element selector list matches this nested
3835 * object, we return the Object in full as a string. If not, we use this
3836 * state to skip to the next value at this level and continue processing.
3838 * NOTE: This function uses various macros from strtolctype.h to manipulate
3839 * digit values, etc -- these have all been checked to ensure they make
3840 * no additional function calls.
3843 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems,
3846 dtrace_json_state_t state = DTRACE_JSON_REST;
3847 int64_t array_elem = INT64_MIN;
3848 int64_t array_pos = 0;
3849 uint8_t escape_unicount = 0;
3850 boolean_t string_is_key = B_FALSE;
3851 boolean_t collect_object = B_FALSE;
3852 boolean_t found_key = B_FALSE;
3853 boolean_t in_array = B_FALSE;
3854 uint32_t braces = 0, brackets = 0;
3855 char *elem = elemlist;
3859 for (cur = json; cur < json + size; cur++) {
3860 char cc = dtrace_load8(cur);
3865 case DTRACE_JSON_REST:
3870 state = DTRACE_JSON_OBJECT;
3877 array_elem = dtrace_strtoll(elem, 10, size);
3878 found_key = array_elem == 0 ? B_TRUE : B_FALSE;
3879 state = DTRACE_JSON_VALUE;
3884 * ERROR: expected to find a top-level object or array.
3887 case DTRACE_JSON_OBJECT:
3892 state = DTRACE_JSON_STRING;
3893 string_is_key = B_TRUE;
3898 * ERROR: either the object did not start with a key
3899 * string, or we've run off the end of the object
3900 * without finding the requested key.
3903 case DTRACE_JSON_STRING:
3906 state = DTRACE_JSON_STRING_ESCAPE;
3911 if (collect_object) {
3913 * We don't reset the dest here, as
3914 * the string is part of a larger
3915 * object being collected.
3918 collect_object = B_FALSE;
3919 state = DTRACE_JSON_COLLECT_OBJECT;
3923 dd = dest; /* reset string buffer */
3924 if (string_is_key) {
3925 if (dtrace_strncmp(dest, elem,
3928 } else if (found_key) {
3931 * We expected an object, not
3938 state = string_is_key ? DTRACE_JSON_COLON :
3940 string_is_key = B_FALSE;
3946 case DTRACE_JSON_STRING_ESCAPE:
3949 escape_unicount = 0;
3950 state = DTRACE_JSON_STRING_ESCAPE_UNICODE;
3952 state = DTRACE_JSON_STRING;
3955 case DTRACE_JSON_STRING_ESCAPE_UNICODE:
3956 if (!isxdigit(cc)) {
3958 * ERROR: invalid unicode escape, expected
3959 * four valid hexidecimal digits.
3965 if (++escape_unicount == 4)
3966 state = DTRACE_JSON_STRING;
3968 case DTRACE_JSON_COLON:
3973 state = DTRACE_JSON_VALUE;
3978 * ERROR: expected a colon.
3981 case DTRACE_JSON_COMMA:
3987 state = DTRACE_JSON_VALUE;
3988 if (++array_pos == array_elem)
3991 state = DTRACE_JSON_OBJECT;
3997 * ERROR: either we hit an unexpected character, or
3998 * we reached the end of the object or array without
3999 * finding the requested key.
4002 case DTRACE_JSON_IDENTIFIER:
4009 dd = dest; /* reset string buffer */
4011 if (dtrace_strncmp(dest, "true", 5) == 0 ||
4012 dtrace_strncmp(dest, "false", 6) == 0 ||
4013 dtrace_strncmp(dest, "null", 5) == 0) {
4017 * ERROR: We expected an object,
4018 * not this identifier.
4025 state = DTRACE_JSON_COMMA;
4031 * ERROR: we did not recognise the identifier as one
4032 * of those in the JSON specification.
4035 case DTRACE_JSON_NUMBER:
4038 state = DTRACE_JSON_NUMBER_FRAC;
4042 if (cc == 'x' || cc == 'X') {
4044 * ERROR: specification explicitly excludes
4045 * hexidecimal or octal numbers.
4051 case DTRACE_JSON_NUMBER_FRAC:
4052 if (cc == 'e' || cc == 'E') {
4054 state = DTRACE_JSON_NUMBER_EXP;
4058 if (cc == '+' || cc == '-') {
4060 * ERROR: expect sign as part of exponent only.
4065 case DTRACE_JSON_NUMBER_EXP:
4066 if (isdigit(cc) || cc == '+' || cc == '-') {
4072 dd = dest; /* reset string buffer */
4076 * ERROR: We expected an object, not
4085 state = DTRACE_JSON_COMMA;
4087 case DTRACE_JSON_VALUE:
4091 if (cc == '{' || cc == '[') {
4092 if (nelems > 1 && found_key) {
4093 in_array = cc == '[' ? B_TRUE : B_FALSE;
4095 * If our element selector directs us
4096 * to descend into this nested object,
4097 * then move to the next selector
4098 * element in the list and restart the
4101 while (*elem != '\0')
4103 elem++; /* skip the inter-element NUL */
4107 state = DTRACE_JSON_VALUE;
4109 array_elem = dtrace_strtoll(
4111 found_key = array_elem == 0 ?
4114 found_key = B_FALSE;
4115 state = DTRACE_JSON_OBJECT;
4121 * Otherwise, we wish to either skip this
4122 * nested object or return it in full.
4129 state = DTRACE_JSON_COLLECT_OBJECT;
4134 state = DTRACE_JSON_STRING;
4140 * Here we deal with true, false and null.
4143 state = DTRACE_JSON_IDENTIFIER;
4147 if (cc == '-' || isdigit(cc)) {
4149 state = DTRACE_JSON_NUMBER;
4154 * ERROR: unexpected character at start of value.
4157 case DTRACE_JSON_COLLECT_OBJECT:
4160 * ERROR: unexpected end of input.
4166 collect_object = B_TRUE;
4167 state = DTRACE_JSON_STRING;
4172 if (brackets-- == 0) {
4174 * ERROR: unbalanced brackets.
4178 } else if (cc == '}') {
4179 if (braces-- == 0) {
4181 * ERROR: unbalanced braces.
4185 } else if (cc == '{') {
4187 } else if (cc == '[') {
4191 if (brackets == 0 && braces == 0) {
4196 dd = dest; /* reset string buffer */
4197 state = DTRACE_JSON_COMMA;
4206 * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
4207 * Notice that we don't bother validating the proper number of arguments or
4208 * their types in the tuple stack. This isn't needed because all argument
4209 * interpretation is safe because of our load safety -- the worst that can
4210 * happen is that a bogus program can obtain bogus results.
4213 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
4214 dtrace_key_t *tupregs, int nargs,
4215 dtrace_mstate_t *mstate, dtrace_state_t *state)
4217 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
4218 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
4219 dtrace_vstate_t *vstate = &state->dts_vstate;
4232 struct thread *lowner;
4234 struct lock_object *li;
4241 regs[rd] = dtrace_xoroshiro128_plus_next(
4242 state->dts_rstate[curcpu]);
4246 case DIF_SUBR_MUTEX_OWNED:
4247 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4253 m.mx = dtrace_load64(tupregs[0].dttk_value);
4254 if (MUTEX_TYPE_ADAPTIVE(&m.mi))
4255 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER;
4257 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock);
4260 case DIF_SUBR_MUTEX_OWNER:
4261 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4267 m.mx = dtrace_load64(tupregs[0].dttk_value);
4268 if (MUTEX_TYPE_ADAPTIVE(&m.mi) &&
4269 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER)
4270 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi);
4275 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
4276 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4282 m.mx = dtrace_load64(tupregs[0].dttk_value);
4283 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi);
4286 case DIF_SUBR_MUTEX_TYPE_SPIN:
4287 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4293 m.mx = dtrace_load64(tupregs[0].dttk_value);
4294 regs[rd] = MUTEX_TYPE_SPIN(&m.mi);
4297 case DIF_SUBR_RW_READ_HELD: {
4300 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4306 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4307 regs[rd] = _RW_READ_HELD(&r.ri, tmp);
4311 case DIF_SUBR_RW_WRITE_HELD:
4312 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
4318 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4319 regs[rd] = _RW_WRITE_HELD(&r.ri);
4322 case DIF_SUBR_RW_ISWRITER:
4323 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
4329 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4330 regs[rd] = _RW_ISWRITER(&r.ri);
4333 #else /* !illumos */
4334 case DIF_SUBR_MUTEX_OWNED:
4335 if (!dtrace_canload(tupregs[0].dttk_value,
4336 sizeof (struct lock_object), mstate, vstate)) {
4340 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4341 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4342 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
4343 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4346 case DIF_SUBR_MUTEX_OWNER:
4347 if (!dtrace_canload(tupregs[0].dttk_value,
4348 sizeof (struct lock_object), mstate, vstate)) {
4352 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4353 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4354 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
4355 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4356 regs[rd] = (uintptr_t)lowner;
4359 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
4360 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx),
4365 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4366 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4367 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SLEEPLOCK) != 0;
4368 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4371 case DIF_SUBR_MUTEX_TYPE_SPIN:
4372 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx),
4377 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4378 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4379 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0;
4380 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4383 case DIF_SUBR_RW_READ_HELD:
4384 case DIF_SUBR_SX_SHARED_HELD:
4385 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4390 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4391 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4392 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) &&
4394 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4397 case DIF_SUBR_RW_WRITE_HELD:
4398 case DIF_SUBR_SX_EXCLUSIVE_HELD:
4399 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4404 l.lx = dtrace_loadptr(tupregs[0].dttk_value);
4405 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4406 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) &&
4408 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4411 case DIF_SUBR_RW_ISWRITER:
4412 case DIF_SUBR_SX_ISEXCLUSIVE:
4413 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4418 l.lx = dtrace_loadptr(tupregs[0].dttk_value);
4419 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4420 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
4421 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4422 regs[rd] = (lowner == curthread);
4424 #endif /* illumos */
4426 case DIF_SUBR_BCOPY: {
4428 * We need to be sure that the destination is in the scratch
4429 * region -- no other region is allowed.
4431 uintptr_t src = tupregs[0].dttk_value;
4432 uintptr_t dest = tupregs[1].dttk_value;
4433 size_t size = tupregs[2].dttk_value;
4435 if (!dtrace_inscratch(dest, size, mstate)) {
4436 *flags |= CPU_DTRACE_BADADDR;
4441 if (!dtrace_canload(src, size, mstate, vstate)) {
4446 dtrace_bcopy((void *)src, (void *)dest, size);
4450 case DIF_SUBR_ALLOCA:
4451 case DIF_SUBR_COPYIN: {
4452 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
4454 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value;
4455 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size;
4458 * This action doesn't require any credential checks since
4459 * probes will not activate in user contexts to which the
4460 * enabling user does not have permissions.
4464 * Rounding up the user allocation size could have overflowed
4465 * a large, bogus allocation (like -1ULL) to 0.
4467 if (scratch_size < size ||
4468 !DTRACE_INSCRATCH(mstate, scratch_size)) {
4469 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4474 if (subr == DIF_SUBR_COPYIN) {
4475 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4476 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
4477 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4480 mstate->dtms_scratch_ptr += scratch_size;
4485 case DIF_SUBR_COPYINTO: {
4486 uint64_t size = tupregs[1].dttk_value;
4487 uintptr_t dest = tupregs[2].dttk_value;
4490 * This action doesn't require any credential checks since
4491 * probes will not activate in user contexts to which the
4492 * enabling user does not have permissions.
4494 if (!dtrace_inscratch(dest, size, mstate)) {
4495 *flags |= CPU_DTRACE_BADADDR;
4500 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4501 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
4502 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4506 case DIF_SUBR_COPYINSTR: {
4507 uintptr_t dest = mstate->dtms_scratch_ptr;
4508 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4510 if (nargs > 1 && tupregs[1].dttk_value < size)
4511 size = tupregs[1].dttk_value + 1;
4514 * This action doesn't require any credential checks since
4515 * probes will not activate in user contexts to which the
4516 * enabling user does not have permissions.
4518 if (!DTRACE_INSCRATCH(mstate, size)) {
4519 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4524 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4525 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags);
4526 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4528 ((char *)dest)[size - 1] = '\0';
4529 mstate->dtms_scratch_ptr += size;
4535 case DIF_SUBR_MSGSIZE:
4536 case DIF_SUBR_MSGDSIZE: {
4537 uintptr_t baddr = tupregs[0].dttk_value, daddr;
4538 uintptr_t wptr, rptr;
4542 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) {
4544 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate,
4550 wptr = dtrace_loadptr(baddr +
4551 offsetof(mblk_t, b_wptr));
4553 rptr = dtrace_loadptr(baddr +
4554 offsetof(mblk_t, b_rptr));
4557 *flags |= CPU_DTRACE_BADADDR;
4558 *illval = tupregs[0].dttk_value;
4562 daddr = dtrace_loadptr(baddr +
4563 offsetof(mblk_t, b_datap));
4565 baddr = dtrace_loadptr(baddr +
4566 offsetof(mblk_t, b_cont));
4569 * We want to prevent against denial-of-service here,
4570 * so we're only going to search the list for
4571 * dtrace_msgdsize_max mblks.
4573 if (cont++ > dtrace_msgdsize_max) {
4574 *flags |= CPU_DTRACE_ILLOP;
4578 if (subr == DIF_SUBR_MSGDSIZE) {
4579 if (dtrace_load8(daddr +
4580 offsetof(dblk_t, db_type)) != M_DATA)
4584 count += wptr - rptr;
4587 if (!(*flags & CPU_DTRACE_FAULT))
4594 case DIF_SUBR_PROGENYOF: {
4595 pid_t pid = tupregs[0].dttk_value;
4599 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4601 for (p = curthread->t_procp; p != NULL; p = p->p_parent) {
4603 if (p->p_pidp->pid_id == pid) {
4605 if (p->p_pid == pid) {
4612 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4618 case DIF_SUBR_SPECULATION:
4619 regs[rd] = dtrace_speculation(state);
4622 case DIF_SUBR_COPYOUT: {
4623 uintptr_t kaddr = tupregs[0].dttk_value;
4624 uintptr_t uaddr = tupregs[1].dttk_value;
4625 uint64_t size = tupregs[2].dttk_value;
4627 if (!dtrace_destructive_disallow &&
4628 dtrace_priv_proc_control(state) &&
4629 !dtrace_istoxic(kaddr, size) &&
4630 dtrace_canload(kaddr, size, mstate, vstate)) {
4631 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4632 dtrace_copyout(kaddr, uaddr, size, flags);
4633 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4638 case DIF_SUBR_COPYOUTSTR: {
4639 uintptr_t kaddr = tupregs[0].dttk_value;
4640 uintptr_t uaddr = tupregs[1].dttk_value;
4641 uint64_t size = tupregs[2].dttk_value;
4644 if (!dtrace_destructive_disallow &&
4645 dtrace_priv_proc_control(state) &&
4646 !dtrace_istoxic(kaddr, size) &&
4647 dtrace_strcanload(kaddr, size, &lim, mstate, vstate)) {
4648 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4649 dtrace_copyoutstr(kaddr, uaddr, lim, flags);
4650 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4655 case DIF_SUBR_STRLEN: {
4656 size_t size = state->dts_options[DTRACEOPT_STRSIZE];
4657 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value;
4660 if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) {
4665 regs[rd] = dtrace_strlen((char *)addr, lim);
4669 case DIF_SUBR_STRCHR:
4670 case DIF_SUBR_STRRCHR: {
4672 * We're going to iterate over the string looking for the
4673 * specified character. We will iterate until we have reached
4674 * the string length or we have found the character. If this
4675 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
4676 * of the specified character instead of the first.
4678 uintptr_t addr = tupregs[0].dttk_value;
4679 uintptr_t addr_limit;
4680 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4682 char c, target = (char)tupregs[1].dttk_value;
4684 if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) {
4688 addr_limit = addr + lim;
4690 for (regs[rd] = 0; addr < addr_limit; addr++) {
4691 if ((c = dtrace_load8(addr)) == target) {
4694 if (subr == DIF_SUBR_STRCHR)
4704 case DIF_SUBR_STRSTR:
4705 case DIF_SUBR_INDEX:
4706 case DIF_SUBR_RINDEX: {
4708 * We're going to iterate over the string looking for the
4709 * specified string. We will iterate until we have reached
4710 * the string length or we have found the string. (Yes, this
4711 * is done in the most naive way possible -- but considering
4712 * that the string we're searching for is likely to be
4713 * relatively short, the complexity of Rabin-Karp or similar
4714 * hardly seems merited.)
4716 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value;
4717 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value;
4718 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4719 size_t len = dtrace_strlen(addr, size);
4720 size_t sublen = dtrace_strlen(substr, size);
4721 char *limit = addr + len, *orig = addr;
4722 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1;
4725 regs[rd] = notfound;
4727 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) {
4732 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate,
4739 * strstr() and index()/rindex() have similar semantics if
4740 * both strings are the empty string: strstr() returns a
4741 * pointer to the (empty) string, and index() and rindex()
4742 * both return index 0 (regardless of any position argument).
4744 if (sublen == 0 && len == 0) {
4745 if (subr == DIF_SUBR_STRSTR)
4746 regs[rd] = (uintptr_t)addr;
4752 if (subr != DIF_SUBR_STRSTR) {
4753 if (subr == DIF_SUBR_RINDEX) {
4760 * Both index() and rindex() take an optional position
4761 * argument that denotes the starting position.
4764 int64_t pos = (int64_t)tupregs[2].dttk_value;
4767 * If the position argument to index() is
4768 * negative, Perl implicitly clamps it at
4769 * zero. This semantic is a little surprising
4770 * given the special meaning of negative
4771 * positions to similar Perl functions like
4772 * substr(), but it appears to reflect a
4773 * notion that index() can start from a
4774 * negative index and increment its way up to
4775 * the string. Given this notion, Perl's
4776 * rindex() is at least self-consistent in
4777 * that it implicitly clamps positions greater
4778 * than the string length to be the string
4779 * length. Where Perl completely loses
4780 * coherence, however, is when the specified
4781 * substring is the empty string (""). In
4782 * this case, even if the position is
4783 * negative, rindex() returns 0 -- and even if
4784 * the position is greater than the length,
4785 * index() returns the string length. These
4786 * semantics violate the notion that index()
4787 * should never return a value less than the
4788 * specified position and that rindex() should
4789 * never return a value greater than the
4790 * specified position. (One assumes that
4791 * these semantics are artifacts of Perl's
4792 * implementation and not the results of
4793 * deliberate design -- it beggars belief that
4794 * even Larry Wall could desire such oddness.)
4795 * While in the abstract one would wish for
4796 * consistent position semantics across
4797 * substr(), index() and rindex() -- or at the
4798 * very least self-consistent position
4799 * semantics for index() and rindex() -- we
4800 * instead opt to keep with the extant Perl
4801 * semantics, in all their broken glory. (Do
4802 * we have more desire to maintain Perl's
4803 * semantics than Perl does? Probably.)
4805 if (subr == DIF_SUBR_RINDEX) {
4829 for (regs[rd] = notfound; addr != limit; addr += inc) {
4830 if (dtrace_strncmp(addr, substr, sublen) == 0) {
4831 if (subr != DIF_SUBR_STRSTR) {
4833 * As D index() and rindex() are
4834 * modeled on Perl (and not on awk),
4835 * we return a zero-based (and not a
4836 * one-based) index. (For you Perl
4837 * weenies: no, we're not going to add
4838 * $[ -- and shouldn't you be at a con
4841 regs[rd] = (uintptr_t)(addr - orig);
4845 ASSERT(subr == DIF_SUBR_STRSTR);
4846 regs[rd] = (uintptr_t)addr;
4854 case DIF_SUBR_STRTOK: {
4855 uintptr_t addr = tupregs[0].dttk_value;
4856 uintptr_t tokaddr = tupregs[1].dttk_value;
4857 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4858 uintptr_t limit, toklimit;
4860 uint8_t c = 0, tokmap[32]; /* 256 / 8 */
4861 char *dest = (char *)mstate->dtms_scratch_ptr;
4865 * Check both the token buffer and (later) the input buffer,
4866 * since both could be non-scratch addresses.
4868 if (!dtrace_strcanload(tokaddr, size, &clim, mstate, vstate)) {
4872 toklimit = tokaddr + clim;
4874 if (!DTRACE_INSCRATCH(mstate, size)) {
4875 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4882 * If the address specified is NULL, we use our saved
4883 * strtok pointer from the mstate. Note that this
4884 * means that the saved strtok pointer is _only_
4885 * valid within multiple enablings of the same probe --
4886 * it behaves like an implicit clause-local variable.
4888 addr = mstate->dtms_strtok;
4889 limit = mstate->dtms_strtok_limit;
4892 * If the user-specified address is non-NULL we must
4893 * access check it. This is the only time we have
4894 * a chance to do so, since this address may reside
4895 * in the string table of this clause-- future calls
4896 * (when we fetch addr from mstate->dtms_strtok)
4897 * would fail this access check.
4899 if (!dtrace_strcanload(addr, size, &clim, mstate,
4904 limit = addr + clim;
4908 * First, zero the token map, and then process the token
4909 * string -- setting a bit in the map for every character
4910 * found in the token string.
4912 for (i = 0; i < sizeof (tokmap); i++)
4915 for (; tokaddr < toklimit; tokaddr++) {
4916 if ((c = dtrace_load8(tokaddr)) == '\0')
4919 ASSERT((c >> 3) < sizeof (tokmap));
4920 tokmap[c >> 3] |= (1 << (c & 0x7));
4923 for (; addr < limit; addr++) {
4925 * We're looking for a character that is _not_
4926 * contained in the token string.
4928 if ((c = dtrace_load8(addr)) == '\0')
4931 if (!(tokmap[c >> 3] & (1 << (c & 0x7))))
4937 * We reached the end of the string without finding
4938 * any character that was not in the token string.
4939 * We return NULL in this case, and we set the saved
4940 * address to NULL as well.
4943 mstate->dtms_strtok = 0;
4944 mstate->dtms_strtok_limit = 0;
4949 * From here on, we're copying into the destination string.
4951 for (i = 0; addr < limit && i < size - 1; addr++) {
4952 if ((c = dtrace_load8(addr)) == '\0')
4955 if (tokmap[c >> 3] & (1 << (c & 0x7)))
4964 regs[rd] = (uintptr_t)dest;
4965 mstate->dtms_scratch_ptr += size;
4966 mstate->dtms_strtok = addr;
4967 mstate->dtms_strtok_limit = limit;
4971 case DIF_SUBR_SUBSTR: {
4972 uintptr_t s = tupregs[0].dttk_value;
4973 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4974 char *d = (char *)mstate->dtms_scratch_ptr;
4975 int64_t index = (int64_t)tupregs[1].dttk_value;
4976 int64_t remaining = (int64_t)tupregs[2].dttk_value;
4977 size_t len = dtrace_strlen((char *)s, size);
4980 if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4985 if (!DTRACE_INSCRATCH(mstate, size)) {
4986 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4992 remaining = (int64_t)size;
4997 if (index < 0 && index + remaining > 0) {
5003 if (index >= len || index < 0) {
5005 } else if (remaining < 0) {
5006 remaining += len - index;
5007 } else if (index + remaining > size) {
5008 remaining = size - index;
5011 for (i = 0; i < remaining; i++) {
5012 if ((d[i] = dtrace_load8(s + index + i)) == '\0')
5018 mstate->dtms_scratch_ptr += size;
5019 regs[rd] = (uintptr_t)d;
5023 case DIF_SUBR_JSON: {
5024 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5025 uintptr_t json = tupregs[0].dttk_value;
5026 size_t jsonlen = dtrace_strlen((char *)json, size);
5027 uintptr_t elem = tupregs[1].dttk_value;
5028 size_t elemlen = dtrace_strlen((char *)elem, size);
5030 char *dest = (char *)mstate->dtms_scratch_ptr;
5031 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1;
5032 char *ee = elemlist;
5036 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) ||
5037 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) {
5042 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) {
5043 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5049 * Read the element selector and split it up into a packed list
5052 for (cur = elem; cur < elem + elemlen; cur++) {
5053 char cc = dtrace_load8(cur);
5055 if (cur == elem && cc == '[') {
5057 * If the first element selector key is
5058 * actually an array index then ignore the
5067 if (cc == '.' || cc == '[') {
5076 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist,
5077 nelems, dest)) != 0)
5078 mstate->dtms_scratch_ptr += jsonlen + 1;
5082 case DIF_SUBR_TOUPPER:
5083 case DIF_SUBR_TOLOWER: {
5084 uintptr_t s = tupregs[0].dttk_value;
5085 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5086 char *dest = (char *)mstate->dtms_scratch_ptr, c;
5087 size_t len = dtrace_strlen((char *)s, size);
5088 char lower, upper, convert;
5091 if (subr == DIF_SUBR_TOUPPER) {
5101 if (!dtrace_canload(s, len + 1, mstate, vstate)) {
5106 if (!DTRACE_INSCRATCH(mstate, size)) {
5107 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5112 for (i = 0; i < size - 1; i++) {
5113 if ((c = dtrace_load8(s + i)) == '\0')
5116 if (c >= lower && c <= upper)
5117 c = convert + (c - lower);
5124 regs[rd] = (uintptr_t)dest;
5125 mstate->dtms_scratch_ptr += size;
5130 case DIF_SUBR_GETMAJOR:
5132 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64;
5134 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ;
5138 case DIF_SUBR_GETMINOR:
5140 regs[rd] = tupregs[0].dttk_value & MAXMIN64;
5142 regs[rd] = tupregs[0].dttk_value & MAXMIN;
5146 case DIF_SUBR_DDI_PATHNAME: {
5148 * This one is a galactic mess. We are going to roughly
5149 * emulate ddi_pathname(), but it's made more complicated
5150 * by the fact that we (a) want to include the minor name and
5151 * (b) must proceed iteratively instead of recursively.
5153 uintptr_t dest = mstate->dtms_scratch_ptr;
5154 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5155 char *start = (char *)dest, *end = start + size - 1;
5156 uintptr_t daddr = tupregs[0].dttk_value;
5157 int64_t minor = (int64_t)tupregs[1].dttk_value;
5159 int i, len, depth = 0;
5162 * Due to all the pointer jumping we do and context we must
5163 * rely upon, we just mandate that the user must have kernel
5164 * read privileges to use this routine.
5166 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) {
5167 *flags |= CPU_DTRACE_KPRIV;
5172 if (!DTRACE_INSCRATCH(mstate, size)) {
5173 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5181 * We want to have a name for the minor. In order to do this,
5182 * we need to walk the minor list from the devinfo. We want
5183 * to be sure that we don't infinitely walk a circular list,
5184 * so we check for circularity by sending a scout pointer
5185 * ahead two elements for every element that we iterate over;
5186 * if the list is circular, these will ultimately point to the
5187 * same element. You may recognize this little trick as the
5188 * answer to a stupid interview question -- one that always
5189 * seems to be asked by those who had to have it laboriously
5190 * explained to them, and who can't even concisely describe
5191 * the conditions under which one would be forced to resort to
5192 * this technique. Needless to say, those conditions are
5193 * found here -- and probably only here. Is this the only use
5194 * of this infamous trick in shipping, production code? If it
5195 * isn't, it probably should be...
5198 uintptr_t maddr = dtrace_loadptr(daddr +
5199 offsetof(struct dev_info, devi_minor));
5201 uintptr_t next = offsetof(struct ddi_minor_data, next);
5202 uintptr_t name = offsetof(struct ddi_minor_data,
5203 d_minor) + offsetof(struct ddi_minor, name);
5204 uintptr_t dev = offsetof(struct ddi_minor_data,
5205 d_minor) + offsetof(struct ddi_minor, dev);
5209 scout = dtrace_loadptr(maddr + next);
5211 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
5214 m = dtrace_load64(maddr + dev) & MAXMIN64;
5216 m = dtrace_load32(maddr + dev) & MAXMIN;
5219 maddr = dtrace_loadptr(maddr + next);
5224 scout = dtrace_loadptr(scout + next);
5229 scout = dtrace_loadptr(scout + next);
5234 if (scout == maddr) {
5235 *flags |= CPU_DTRACE_ILLOP;
5243 * We have the minor data. Now we need to
5244 * copy the minor's name into the end of the
5247 s = (char *)dtrace_loadptr(maddr + name);
5248 len = dtrace_strlen(s, size);
5250 if (*flags & CPU_DTRACE_FAULT)
5254 if ((end -= (len + 1)) < start)
5260 for (i = 1; i <= len; i++)
5261 end[i] = dtrace_load8((uintptr_t)s++);
5266 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
5267 ddi_node_state_t devi_state;
5269 devi_state = dtrace_load32(daddr +
5270 offsetof(struct dev_info, devi_node_state));
5272 if (*flags & CPU_DTRACE_FAULT)
5275 if (devi_state >= DS_INITIALIZED) {
5276 s = (char *)dtrace_loadptr(daddr +
5277 offsetof(struct dev_info, devi_addr));
5278 len = dtrace_strlen(s, size);
5280 if (*flags & CPU_DTRACE_FAULT)
5284 if ((end -= (len + 1)) < start)
5290 for (i = 1; i <= len; i++)
5291 end[i] = dtrace_load8((uintptr_t)s++);
5295 * Now for the node name...
5297 s = (char *)dtrace_loadptr(daddr +
5298 offsetof(struct dev_info, devi_node_name));
5300 daddr = dtrace_loadptr(daddr +
5301 offsetof(struct dev_info, devi_parent));
5304 * If our parent is NULL (that is, if we're the root
5305 * node), we're going to use the special path
5311 len = dtrace_strlen(s, size);
5312 if (*flags & CPU_DTRACE_FAULT)
5315 if ((end -= (len + 1)) < start)
5318 for (i = 1; i <= len; i++)
5319 end[i] = dtrace_load8((uintptr_t)s++);
5322 if (depth++ > dtrace_devdepth_max) {
5323 *flags |= CPU_DTRACE_ILLOP;
5329 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5332 regs[rd] = (uintptr_t)end;
5333 mstate->dtms_scratch_ptr += size;
5340 case DIF_SUBR_STRJOIN: {
5341 char *d = (char *)mstate->dtms_scratch_ptr;
5342 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5343 uintptr_t s1 = tupregs[0].dttk_value;
5344 uintptr_t s2 = tupregs[1].dttk_value;
5349 if (!dtrace_strcanload(s1, size, &lim1, mstate, vstate) ||
5350 !dtrace_strcanload(s2, size, &lim2, mstate, vstate)) {
5355 if (!DTRACE_INSCRATCH(mstate, size)) {
5356 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5363 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5367 c = (i >= lim1) ? '\0' : dtrace_load8(s1++);
5368 if ((d[i++] = c) == '\0') {
5376 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5381 c = (j++ >= lim2) ? '\0' : dtrace_load8(s2++);
5382 if ((d[i++] = c) == '\0')
5387 mstate->dtms_scratch_ptr += i;
5388 regs[rd] = (uintptr_t)d;
5394 case DIF_SUBR_STRTOLL: {
5395 uintptr_t s = tupregs[0].dttk_value;
5396 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5401 if ((base = tupregs[1].dttk_value) <= 1 ||
5402 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
5403 *flags |= CPU_DTRACE_ILLOP;
5408 if (!dtrace_strcanload(s, size, &lim, mstate, vstate)) {
5409 regs[rd] = INT64_MIN;
5413 regs[rd] = dtrace_strtoll((char *)s, base, lim);
5417 case DIF_SUBR_LLTOSTR: {
5418 int64_t i = (int64_t)tupregs[0].dttk_value;
5419 uint64_t val, digit;
5420 uint64_t size = 65; /* enough room for 2^64 in binary */
5421 char *end = (char *)mstate->dtms_scratch_ptr + size - 1;
5425 if ((base = tupregs[1].dttk_value) <= 1 ||
5426 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
5427 *flags |= CPU_DTRACE_ILLOP;
5432 val = (base == 10 && i < 0) ? i * -1 : i;
5434 if (!DTRACE_INSCRATCH(mstate, size)) {
5435 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5440 for (*end-- = '\0'; val; val /= base) {
5441 if ((digit = val % base) <= '9' - '0') {
5442 *end-- = '0' + digit;
5444 *end-- = 'a' + (digit - ('9' - '0') - 1);
5448 if (i == 0 && base == 16)
5454 if (i == 0 || base == 8 || base == 16)
5457 if (i < 0 && base == 10)
5460 regs[rd] = (uintptr_t)end + 1;
5461 mstate->dtms_scratch_ptr += size;
5465 case DIF_SUBR_HTONS:
5466 case DIF_SUBR_NTOHS:
5467 #if BYTE_ORDER == BIG_ENDIAN
5468 regs[rd] = (uint16_t)tupregs[0].dttk_value;
5470 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value);
5475 case DIF_SUBR_HTONL:
5476 case DIF_SUBR_NTOHL:
5477 #if BYTE_ORDER == BIG_ENDIAN
5478 regs[rd] = (uint32_t)tupregs[0].dttk_value;
5480 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value);
5485 case DIF_SUBR_HTONLL:
5486 case DIF_SUBR_NTOHLL:
5487 #if BYTE_ORDER == BIG_ENDIAN
5488 regs[rd] = (uint64_t)tupregs[0].dttk_value;
5490 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value);
5495 case DIF_SUBR_DIRNAME:
5496 case DIF_SUBR_BASENAME: {
5497 char *dest = (char *)mstate->dtms_scratch_ptr;
5498 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5499 uintptr_t src = tupregs[0].dttk_value;
5500 int i, j, len = dtrace_strlen((char *)src, size);
5501 int lastbase = -1, firstbase = -1, lastdir = -1;
5504 if (!dtrace_canload(src, len + 1, mstate, vstate)) {
5509 if (!DTRACE_INSCRATCH(mstate, size)) {
5510 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5516 * The basename and dirname for a zero-length string is
5521 src = (uintptr_t)".";
5525 * Start from the back of the string, moving back toward the
5526 * front until we see a character that isn't a slash. That
5527 * character is the last character in the basename.
5529 for (i = len - 1; i >= 0; i--) {
5530 if (dtrace_load8(src + i) != '/')
5538 * Starting from the last character in the basename, move
5539 * towards the front until we find a slash. The character
5540 * that we processed immediately before that is the first
5541 * character in the basename.
5543 for (; i >= 0; i--) {
5544 if (dtrace_load8(src + i) == '/')
5552 * Now keep going until we find a non-slash character. That
5553 * character is the last character in the dirname.
5555 for (; i >= 0; i--) {
5556 if (dtrace_load8(src + i) != '/')
5563 ASSERT(!(lastbase == -1 && firstbase != -1));
5564 ASSERT(!(firstbase == -1 && lastdir != -1));
5566 if (lastbase == -1) {
5568 * We didn't find a non-slash character. We know that
5569 * the length is non-zero, so the whole string must be
5570 * slashes. In either the dirname or the basename
5571 * case, we return '/'.
5573 ASSERT(firstbase == -1);
5574 firstbase = lastbase = lastdir = 0;
5577 if (firstbase == -1) {
5579 * The entire string consists only of a basename
5580 * component. If we're looking for dirname, we need
5581 * to change our string to be just "."; if we're
5582 * looking for a basename, we'll just set the first
5583 * character of the basename to be 0.
5585 if (subr == DIF_SUBR_DIRNAME) {
5586 ASSERT(lastdir == -1);
5587 src = (uintptr_t)".";
5594 if (subr == DIF_SUBR_DIRNAME) {
5595 if (lastdir == -1) {
5597 * We know that we have a slash in the name --
5598 * or lastdir would be set to 0, above. And
5599 * because lastdir is -1, we know that this
5600 * slash must be the first character. (That
5601 * is, the full string must be of the form
5602 * "/basename".) In this case, the last
5603 * character of the directory name is 0.
5611 ASSERT(subr == DIF_SUBR_BASENAME);
5612 ASSERT(firstbase != -1 && lastbase != -1);
5617 for (i = start, j = 0; i <= end && j < size - 1; i++, j++)
5618 dest[j] = dtrace_load8(src + i);
5621 regs[rd] = (uintptr_t)dest;
5622 mstate->dtms_scratch_ptr += size;
5626 case DIF_SUBR_GETF: {
5627 uintptr_t fd = tupregs[0].dttk_value;
5628 struct filedesc *fdp;
5631 if (!dtrace_priv_proc(state)) {
5635 fdp = curproc->p_fd;
5636 FILEDESC_SLOCK(fdp);
5637 fp = fget_locked(fdp, fd);
5638 mstate->dtms_getf = fp;
5639 regs[rd] = (uintptr_t)fp;
5640 FILEDESC_SUNLOCK(fdp);
5644 case DIF_SUBR_CLEANPATH: {
5645 char *dest = (char *)mstate->dtms_scratch_ptr, c;
5646 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5647 uintptr_t src = tupregs[0].dttk_value;
5654 if (!dtrace_strcanload(src, size, &lim, mstate, vstate)) {
5659 if (!DTRACE_INSCRATCH(mstate, size)) {
5660 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5666 * Move forward, loading each character.
5669 c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
5671 if (j + 5 >= size) /* 5 = strlen("/..c\0") */
5679 c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
5683 * We have two slashes -- we can just advance
5684 * to the next character.
5691 * This is not "." and it's not ".." -- we can
5692 * just store the "/" and this character and
5700 c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
5704 * This is a "/./" component. We're not going
5705 * to store anything in the destination buffer;
5706 * we're just going to go to the next component.
5713 * This is not ".." -- we can just store the
5714 * "/." and this character and continue
5723 c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
5725 if (c != '/' && c != '\0') {
5727 * This is not ".." -- it's "..[mumble]".
5728 * We'll store the "/.." and this character
5729 * and continue processing.
5739 * This is "/../" or "/..\0". We need to back up
5740 * our destination pointer until we find a "/".
5743 while (j != 0 && dest[--j] != '/')
5748 } while (c != '\0');
5753 if (mstate->dtms_getf != NULL &&
5754 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) &&
5755 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) {
5757 * If we've done a getf() as a part of this ECB and we
5758 * don't have kernel access (and we're not in the global
5759 * zone), check if the path we cleaned up begins with
5760 * the zone's root path, and trim it off if so. Note
5761 * that this is an output cleanliness issue, not a
5762 * security issue: knowing one's zone root path does
5763 * not enable privilege escalation.
5765 if (strstr(dest, z->zone_rootpath) == dest)
5766 dest += strlen(z->zone_rootpath) - 1;
5770 regs[rd] = (uintptr_t)dest;
5771 mstate->dtms_scratch_ptr += size;
5775 case DIF_SUBR_INET_NTOA:
5776 case DIF_SUBR_INET_NTOA6:
5777 case DIF_SUBR_INET_NTOP: {
5782 if (subr == DIF_SUBR_INET_NTOP) {
5783 af = (int)tupregs[0].dttk_value;
5786 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6;
5790 if (af == AF_INET) {
5794 if (!dtrace_canload(tupregs[argi].dttk_value,
5795 sizeof (ipaddr_t), mstate, vstate)) {
5801 * Safely load the IPv4 address.
5803 ip4 = dtrace_load32(tupregs[argi].dttk_value);
5806 * Check an IPv4 string will fit in scratch.
5808 size = INET_ADDRSTRLEN;
5809 if (!DTRACE_INSCRATCH(mstate, size)) {
5810 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5814 base = (char *)mstate->dtms_scratch_ptr;
5815 end = (char *)mstate->dtms_scratch_ptr + size - 1;
5818 * Stringify as a dotted decimal quad.
5821 ptr8 = (uint8_t *)&ip4;
5822 for (i = 3; i >= 0; i--) {
5828 for (; val; val /= 10) {
5829 *end-- = '0' + (val % 10);
5836 ASSERT(end + 1 >= base);
5838 } else if (af == AF_INET6) {
5839 struct in6_addr ip6;
5840 int firstzero, tryzero, numzero, v6end;
5842 const char digits[] = "0123456789abcdef";
5845 * Stringify using RFC 1884 convention 2 - 16 bit
5846 * hexadecimal values with a zero-run compression.
5847 * Lower case hexadecimal digits are used.
5848 * eg, fe80::214:4fff:fe0b:76c8.
5849 * The IPv4 embedded form is returned for inet_ntop,
5850 * just the IPv4 string is returned for inet_ntoa6.
5853 if (!dtrace_canload(tupregs[argi].dttk_value,
5854 sizeof (struct in6_addr), mstate, vstate)) {
5860 * Safely load the IPv6 address.
5863 (void *)(uintptr_t)tupregs[argi].dttk_value,
5864 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr));
5867 * Check an IPv6 string will fit in scratch.
5869 size = INET6_ADDRSTRLEN;
5870 if (!DTRACE_INSCRATCH(mstate, size)) {
5871 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5875 base = (char *)mstate->dtms_scratch_ptr;
5876 end = (char *)mstate->dtms_scratch_ptr + size - 1;
5880 * Find the longest run of 16 bit zero values
5881 * for the single allowed zero compression - "::".
5886 for (i = 0; i < sizeof (struct in6_addr); i++) {
5888 if (ip6._S6_un._S6_u8[i] == 0 &&
5890 if (ip6.__u6_addr.__u6_addr8[i] == 0 &&
5892 tryzero == -1 && i % 2 == 0) {
5897 if (tryzero != -1 &&
5899 (ip6._S6_un._S6_u8[i] != 0 ||
5901 (ip6.__u6_addr.__u6_addr8[i] != 0 ||
5903 i == sizeof (struct in6_addr) - 1)) {
5905 if (i - tryzero <= numzero) {
5910 firstzero = tryzero;
5911 numzero = i - i % 2 - tryzero;
5915 if (ip6._S6_un._S6_u8[i] == 0 &&
5917 if (ip6.__u6_addr.__u6_addr8[i] == 0 &&
5919 i == sizeof (struct in6_addr) - 1)
5923 ASSERT(firstzero + numzero <= sizeof (struct in6_addr));
5926 * Check for an IPv4 embedded address.
5928 v6end = sizeof (struct in6_addr) - 2;
5929 if (IN6_IS_ADDR_V4MAPPED(&ip6) ||
5930 IN6_IS_ADDR_V4COMPAT(&ip6)) {
5931 for (i = sizeof (struct in6_addr) - 1;
5932 i >= DTRACE_V4MAPPED_OFFSET; i--) {
5933 ASSERT(end >= base);
5936 val = ip6._S6_un._S6_u8[i];
5938 val = ip6.__u6_addr.__u6_addr8[i];
5944 for (; val; val /= 10) {
5945 *end-- = '0' + val % 10;
5949 if (i > DTRACE_V4MAPPED_OFFSET)
5953 if (subr == DIF_SUBR_INET_NTOA6)
5957 * Set v6end to skip the IPv4 address that
5958 * we have already stringified.
5964 * Build the IPv6 string by working through the
5965 * address in reverse.
5967 for (i = v6end; i >= 0; i -= 2) {
5968 ASSERT(end >= base);
5970 if (i == firstzero + numzero - 2) {
5977 if (i < 14 && i != firstzero - 2)
5981 val = (ip6._S6_un._S6_u8[i] << 8) +
5982 ip6._S6_un._S6_u8[i + 1];
5984 val = (ip6.__u6_addr.__u6_addr8[i] << 8) +
5985 ip6.__u6_addr.__u6_addr8[i + 1];
5991 for (; val; val /= 16) {
5992 *end-- = digits[val % 16];
5996 ASSERT(end + 1 >= base);
6000 * The user didn't use AH_INET or AH_INET6.
6002 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6007 inetout: regs[rd] = (uintptr_t)end + 1;
6008 mstate->dtms_scratch_ptr += size;
6012 case DIF_SUBR_MEMREF: {
6013 uintptr_t size = 2 * sizeof(uintptr_t);
6014 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t));
6015 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size;
6017 /* address and length */
6018 memref[0] = tupregs[0].dttk_value;
6019 memref[1] = tupregs[1].dttk_value;
6021 regs[rd] = (uintptr_t) memref;
6022 mstate->dtms_scratch_ptr += scratch_size;
6027 case DIF_SUBR_MEMSTR: {
6028 char *str = (char *)mstate->dtms_scratch_ptr;
6029 uintptr_t mem = tupregs[0].dttk_value;
6030 char c = tupregs[1].dttk_value;
6031 size_t size = tupregs[2].dttk_value;
6040 if (!dtrace_canload(mem, size - 1, mstate, vstate))
6043 if (!DTRACE_INSCRATCH(mstate, size)) {
6044 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6048 if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) {
6049 *flags |= CPU_DTRACE_ILLOP;
6053 for (i = 0; i < size - 1; i++) {
6054 n = dtrace_load8(mem++);
6055 str[i] = (n == 0) ? c : n;
6059 regs[rd] = (uintptr_t)str;
6060 mstate->dtms_scratch_ptr += size;
6068 * Emulate the execution of DTrace IR instructions specified by the given
6069 * DIF object. This function is deliberately void of assertions as all of
6070 * the necessary checks are handled by a call to dtrace_difo_validate().
6073 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate,
6074 dtrace_vstate_t *vstate, dtrace_state_t *state)
6076 const dif_instr_t *text = difo->dtdo_buf;
6077 const uint_t textlen = difo->dtdo_len;
6078 const char *strtab = difo->dtdo_strtab;
6079 const uint64_t *inttab = difo->dtdo_inttab;
6082 dtrace_statvar_t *svar;
6083 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
6085 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
6086 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
6088 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
6089 uint64_t regs[DIF_DIR_NREGS];
6092 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0;
6094 uint_t pc = 0, id, opc = 0;
6100 * We stash the current DIF object into the machine state: we need it
6101 * for subsequent access checking.
6103 mstate->dtms_difo = difo;
6105 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */
6107 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) {
6111 r1 = DIF_INSTR_R1(instr);
6112 r2 = DIF_INSTR_R2(instr);
6113 rd = DIF_INSTR_RD(instr);
6115 switch (DIF_INSTR_OP(instr)) {
6117 regs[rd] = regs[r1] | regs[r2];
6120 regs[rd] = regs[r1] ^ regs[r2];
6123 regs[rd] = regs[r1] & regs[r2];
6126 regs[rd] = regs[r1] << regs[r2];
6129 regs[rd] = regs[r1] >> regs[r2];
6132 regs[rd] = regs[r1] - regs[r2];
6135 regs[rd] = regs[r1] + regs[r2];
6138 regs[rd] = regs[r1] * regs[r2];
6141 if (regs[r2] == 0) {
6143 *flags |= CPU_DTRACE_DIVZERO;
6145 regs[rd] = (int64_t)regs[r1] /
6151 if (regs[r2] == 0) {
6153 *flags |= CPU_DTRACE_DIVZERO;
6155 regs[rd] = regs[r1] / regs[r2];
6160 if (regs[r2] == 0) {
6162 *flags |= CPU_DTRACE_DIVZERO;
6164 regs[rd] = (int64_t)regs[r1] %
6170 if (regs[r2] == 0) {
6172 *flags |= CPU_DTRACE_DIVZERO;
6174 regs[rd] = regs[r1] % regs[r2];
6179 regs[rd] = ~regs[r1];
6182 regs[rd] = regs[r1];
6185 cc_r = regs[r1] - regs[r2];
6189 cc_c = regs[r1] < regs[r2];
6192 cc_n = cc_v = cc_c = 0;
6193 cc_z = regs[r1] == 0;
6196 pc = DIF_INSTR_LABEL(instr);
6200 pc = DIF_INSTR_LABEL(instr);
6204 pc = DIF_INSTR_LABEL(instr);
6207 if ((cc_z | (cc_n ^ cc_v)) == 0)
6208 pc = DIF_INSTR_LABEL(instr);
6211 if ((cc_c | cc_z) == 0)
6212 pc = DIF_INSTR_LABEL(instr);
6215 if ((cc_n ^ cc_v) == 0)
6216 pc = DIF_INSTR_LABEL(instr);
6220 pc = DIF_INSTR_LABEL(instr);
6224 pc = DIF_INSTR_LABEL(instr);
6228 pc = DIF_INSTR_LABEL(instr);
6231 if (cc_z | (cc_n ^ cc_v))
6232 pc = DIF_INSTR_LABEL(instr);
6236 pc = DIF_INSTR_LABEL(instr);
6239 if (!dtrace_canload(regs[r1], 1, mstate, vstate))
6243 regs[rd] = (int8_t)dtrace_load8(regs[r1]);
6246 if (!dtrace_canload(regs[r1], 2, mstate, vstate))
6250 regs[rd] = (int16_t)dtrace_load16(regs[r1]);
6253 if (!dtrace_canload(regs[r1], 4, mstate, vstate))
6257 regs[rd] = (int32_t)dtrace_load32(regs[r1]);
6260 if (!dtrace_canload(regs[r1], 1, mstate, vstate))
6264 regs[rd] = dtrace_load8(regs[r1]);
6267 if (!dtrace_canload(regs[r1], 2, mstate, vstate))
6271 regs[rd] = dtrace_load16(regs[r1]);
6274 if (!dtrace_canload(regs[r1], 4, mstate, vstate))
6278 regs[rd] = dtrace_load32(regs[r1]);
6281 if (!dtrace_canload(regs[r1], 8, mstate, vstate))
6285 regs[rd] = dtrace_load64(regs[r1]);
6288 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6290 dtrace_fuword8((void *)(uintptr_t)regs[r1]);
6291 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6294 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6295 regs[rd] = (int16_t)
6296 dtrace_fuword16((void *)(uintptr_t)regs[r1]);
6297 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6300 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6301 regs[rd] = (int32_t)
6302 dtrace_fuword32((void *)(uintptr_t)regs[r1]);
6303 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6306 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6308 dtrace_fuword8((void *)(uintptr_t)regs[r1]);
6309 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6312 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6314 dtrace_fuword16((void *)(uintptr_t)regs[r1]);
6315 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6318 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6320 dtrace_fuword32((void *)(uintptr_t)regs[r1]);
6321 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6324 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6326 dtrace_fuword64((void *)(uintptr_t)regs[r1]);
6327 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6336 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)];
6339 regs[rd] = (uint64_t)(uintptr_t)
6340 (strtab + DIF_INSTR_STRING(instr));
6343 size_t sz = state->dts_options[DTRACEOPT_STRSIZE];
6344 uintptr_t s1 = regs[r1];
6345 uintptr_t s2 = regs[r2];
6349 !dtrace_strcanload(s1, sz, &lim1, mstate, vstate))
6352 !dtrace_strcanload(s2, sz, &lim2, mstate, vstate))
6355 cc_r = dtrace_strncmp((char *)s1, (char *)s2,
6364 regs[rd] = dtrace_dif_variable(mstate, state,
6368 id = DIF_INSTR_VAR(instr);
6370 if (id >= DIF_VAR_OTHER_UBASE) {
6373 id -= DIF_VAR_OTHER_UBASE;
6374 svar = vstate->dtvs_globals[id];
6375 ASSERT(svar != NULL);
6376 v = &svar->dtsv_var;
6378 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) {
6379 regs[rd] = svar->dtsv_data;
6383 a = (uintptr_t)svar->dtsv_data;
6385 if (*(uint8_t *)a == UINT8_MAX) {
6387 * If the 0th byte is set to UINT8_MAX
6388 * then this is to be treated as a
6389 * reference to a NULL variable.
6393 regs[rd] = a + sizeof (uint64_t);
6399 regs[rd] = dtrace_dif_variable(mstate, state, id, 0);
6403 id = DIF_INSTR_VAR(instr);
6405 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6406 id -= DIF_VAR_OTHER_UBASE;
6408 VERIFY(id < vstate->dtvs_nglobals);
6409 svar = vstate->dtvs_globals[id];
6410 ASSERT(svar != NULL);
6411 v = &svar->dtsv_var;
6413 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6414 uintptr_t a = (uintptr_t)svar->dtsv_data;
6418 ASSERT(svar->dtsv_size != 0);
6420 if (regs[rd] == 0) {
6421 *(uint8_t *)a = UINT8_MAX;
6425 a += sizeof (uint64_t);
6427 if (!dtrace_vcanload(
6428 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6429 &lim, mstate, vstate))
6432 dtrace_vcopy((void *)(uintptr_t)regs[rd],
6433 (void *)a, &v->dtdv_type, lim);
6437 svar->dtsv_data = regs[rd];
6442 * There are no DTrace built-in thread-local arrays at
6443 * present. This opcode is saved for future work.
6445 *flags |= CPU_DTRACE_ILLOP;
6450 id = DIF_INSTR_VAR(instr);
6452 if (id < DIF_VAR_OTHER_UBASE) {
6454 * For now, this has no meaning.
6460 id -= DIF_VAR_OTHER_UBASE;
6462 ASSERT(id < vstate->dtvs_nlocals);
6463 ASSERT(vstate->dtvs_locals != NULL);
6465 svar = vstate->dtvs_locals[id];
6466 ASSERT(svar != NULL);
6467 v = &svar->dtsv_var;
6469 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6470 uintptr_t a = (uintptr_t)svar->dtsv_data;
6471 size_t sz = v->dtdv_type.dtdt_size;
6474 sz += sizeof (uint64_t);
6475 ASSERT(svar->dtsv_size == NCPU * sz);
6478 if (*(uint8_t *)a == UINT8_MAX) {
6480 * If the 0th byte is set to UINT8_MAX
6481 * then this is to be treated as a
6482 * reference to a NULL variable.
6486 regs[rd] = a + sizeof (uint64_t);
6492 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
6493 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
6494 regs[rd] = tmp[curcpu];
6498 id = DIF_INSTR_VAR(instr);
6500 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6501 id -= DIF_VAR_OTHER_UBASE;
6502 VERIFY(id < vstate->dtvs_nlocals);
6504 ASSERT(vstate->dtvs_locals != NULL);
6505 svar = vstate->dtvs_locals[id];
6506 ASSERT(svar != NULL);
6507 v = &svar->dtsv_var;
6509 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6510 uintptr_t a = (uintptr_t)svar->dtsv_data;
6511 size_t sz = v->dtdv_type.dtdt_size;
6514 sz += sizeof (uint64_t);
6515 ASSERT(svar->dtsv_size == NCPU * sz);
6518 if (regs[rd] == 0) {
6519 *(uint8_t *)a = UINT8_MAX;
6523 a += sizeof (uint64_t);
6526 if (!dtrace_vcanload(
6527 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6528 &lim, mstate, vstate))
6531 dtrace_vcopy((void *)(uintptr_t)regs[rd],
6532 (void *)a, &v->dtdv_type, lim);
6536 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
6537 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
6538 tmp[curcpu] = regs[rd];
6542 dtrace_dynvar_t *dvar;
6545 id = DIF_INSTR_VAR(instr);
6546 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6547 id -= DIF_VAR_OTHER_UBASE;
6548 v = &vstate->dtvs_tlocals[id];
6550 key = &tupregs[DIF_DTR_NREGS];
6551 key[0].dttk_value = (uint64_t)id;
6552 key[0].dttk_size = 0;
6553 DTRACE_TLS_THRKEY(key[1].dttk_value);
6554 key[1].dttk_size = 0;
6556 dvar = dtrace_dynvar(dstate, 2, key,
6557 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC,
6565 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6566 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
6568 regs[rd] = *((uint64_t *)dvar->dtdv_data);
6575 dtrace_dynvar_t *dvar;
6578 id = DIF_INSTR_VAR(instr);
6579 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6580 id -= DIF_VAR_OTHER_UBASE;
6581 VERIFY(id < vstate->dtvs_ntlocals);
6583 key = &tupregs[DIF_DTR_NREGS];
6584 key[0].dttk_value = (uint64_t)id;
6585 key[0].dttk_size = 0;
6586 DTRACE_TLS_THRKEY(key[1].dttk_value);
6587 key[1].dttk_size = 0;
6588 v = &vstate->dtvs_tlocals[id];
6590 dvar = dtrace_dynvar(dstate, 2, key,
6591 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6592 v->dtdv_type.dtdt_size : sizeof (uint64_t),
6593 regs[rd] ? DTRACE_DYNVAR_ALLOC :
6594 DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6597 * Given that we're storing to thread-local data,
6598 * we need to flush our predicate cache.
6600 curthread->t_predcache = 0;
6605 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6608 if (!dtrace_vcanload(
6609 (void *)(uintptr_t)regs[rd],
6610 &v->dtdv_type, &lim, mstate, vstate))
6613 dtrace_vcopy((void *)(uintptr_t)regs[rd],
6614 dvar->dtdv_data, &v->dtdv_type, lim);
6616 *((uint64_t *)dvar->dtdv_data) = regs[rd];
6623 regs[rd] = (int64_t)regs[r1] >> regs[r2];
6627 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd,
6628 regs, tupregs, ttop, mstate, state);
6632 if (ttop == DIF_DTR_NREGS) {
6633 *flags |= CPU_DTRACE_TUPOFLOW;
6637 if (r1 == DIF_TYPE_STRING) {
6639 * If this is a string type and the size is 0,
6640 * we'll use the system-wide default string
6641 * size. Note that we are _not_ looking at
6642 * the value of the DTRACEOPT_STRSIZE option;
6643 * had this been set, we would expect to have
6644 * a non-zero size value in the "pushtr".
6646 tupregs[ttop].dttk_size =
6647 dtrace_strlen((char *)(uintptr_t)regs[rd],
6648 regs[r2] ? regs[r2] :
6649 dtrace_strsize_default) + 1;
6651 if (regs[r2] > LONG_MAX) {
6652 *flags |= CPU_DTRACE_ILLOP;
6656 tupregs[ttop].dttk_size = regs[r2];
6659 tupregs[ttop++].dttk_value = regs[rd];
6663 if (ttop == DIF_DTR_NREGS) {
6664 *flags |= CPU_DTRACE_TUPOFLOW;
6668 tupregs[ttop].dttk_value = regs[rd];
6669 tupregs[ttop++].dttk_size = 0;
6677 case DIF_OP_FLUSHTS:
6682 case DIF_OP_LDTAA: {
6683 dtrace_dynvar_t *dvar;
6684 dtrace_key_t *key = tupregs;
6685 uint_t nkeys = ttop;
6687 id = DIF_INSTR_VAR(instr);
6688 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6689 id -= DIF_VAR_OTHER_UBASE;
6691 key[nkeys].dttk_value = (uint64_t)id;
6692 key[nkeys++].dttk_size = 0;
6694 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) {
6695 DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6696 key[nkeys++].dttk_size = 0;
6697 VERIFY(id < vstate->dtvs_ntlocals);
6698 v = &vstate->dtvs_tlocals[id];
6700 VERIFY(id < vstate->dtvs_nglobals);
6701 v = &vstate->dtvs_globals[id]->dtsv_var;
6704 dvar = dtrace_dynvar(dstate, nkeys, key,
6705 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6706 v->dtdv_type.dtdt_size : sizeof (uint64_t),
6707 DTRACE_DYNVAR_NOALLOC, mstate, vstate);
6714 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6715 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
6717 regs[rd] = *((uint64_t *)dvar->dtdv_data);
6724 case DIF_OP_STTAA: {
6725 dtrace_dynvar_t *dvar;
6726 dtrace_key_t *key = tupregs;
6727 uint_t nkeys = ttop;
6729 id = DIF_INSTR_VAR(instr);
6730 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6731 id -= DIF_VAR_OTHER_UBASE;
6733 key[nkeys].dttk_value = (uint64_t)id;
6734 key[nkeys++].dttk_size = 0;
6736 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) {
6737 DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6738 key[nkeys++].dttk_size = 0;
6739 VERIFY(id < vstate->dtvs_ntlocals);
6740 v = &vstate->dtvs_tlocals[id];
6742 VERIFY(id < vstate->dtvs_nglobals);
6743 v = &vstate->dtvs_globals[id]->dtsv_var;
6746 dvar = dtrace_dynvar(dstate, nkeys, key,
6747 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6748 v->dtdv_type.dtdt_size : sizeof (uint64_t),
6749 regs[rd] ? DTRACE_DYNVAR_ALLOC :
6750 DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6755 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6758 if (!dtrace_vcanload(
6759 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6760 &lim, mstate, vstate))
6763 dtrace_vcopy((void *)(uintptr_t)regs[rd],
6764 dvar->dtdv_data, &v->dtdv_type, lim);
6766 *((uint64_t *)dvar->dtdv_data) = regs[rd];
6772 case DIF_OP_ALLOCS: {
6773 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6774 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1];
6777 * Rounding up the user allocation size could have
6778 * overflowed large, bogus allocations (like -1ULL) to
6781 if (size < regs[r1] ||
6782 !DTRACE_INSCRATCH(mstate, size)) {
6783 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6788 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size);
6789 mstate->dtms_scratch_ptr += size;
6795 if (!dtrace_canstore(regs[rd], regs[r2],
6797 *flags |= CPU_DTRACE_BADADDR;
6802 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate))
6805 dtrace_bcopy((void *)(uintptr_t)regs[r1],
6806 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]);
6810 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) {
6811 *flags |= CPU_DTRACE_BADADDR;
6815 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1];
6819 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) {
6820 *flags |= CPU_DTRACE_BADADDR;
6825 *flags |= CPU_DTRACE_BADALIGN;
6829 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1];
6833 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) {
6834 *flags |= CPU_DTRACE_BADADDR;
6839 *flags |= CPU_DTRACE_BADALIGN;
6843 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1];
6847 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) {
6848 *flags |= CPU_DTRACE_BADADDR;
6853 *flags |= CPU_DTRACE_BADALIGN;
6857 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1];
6862 if (!(*flags & CPU_DTRACE_FAULT))
6865 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t);
6866 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS;
6872 dtrace_action_breakpoint(dtrace_ecb_t *ecb)
6874 dtrace_probe_t *probe = ecb->dte_probe;
6875 dtrace_provider_t *prov = probe->dtpr_provider;
6876 char c[DTRACE_FULLNAMELEN + 80], *str;
6877 char *msg = "dtrace: breakpoint action at probe ";
6878 char *ecbmsg = " (ecb ";
6879 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4));
6880 uintptr_t val = (uintptr_t)ecb;
6881 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0;
6883 if (dtrace_destructive_disallow)
6887 * It's impossible to be taking action on the NULL probe.
6889 ASSERT(probe != NULL);
6892 * This is a poor man's (destitute man's?) sprintf(): we want to
6893 * print the provider name, module name, function name and name of
6894 * the probe, along with the hex address of the ECB with the breakpoint
6895 * action -- all of which we must place in the character buffer by
6898 while (*msg != '\0')
6901 for (str = prov->dtpv_name; *str != '\0'; str++)
6905 for (str = probe->dtpr_mod; *str != '\0'; str++)
6909 for (str = probe->dtpr_func; *str != '\0'; str++)
6913 for (str = probe->dtpr_name; *str != '\0'; str++)
6916 while (*ecbmsg != '\0')
6919 while (shift >= 0) {
6920 mask = (uintptr_t)0xf << shift;
6922 if (val >= ((uintptr_t)1 << shift))
6923 c[i++] = "0123456789abcdef"[(val & mask) >> shift];
6933 kdb_enter(KDB_WHY_DTRACE, "breakpoint action");
6938 dtrace_action_panic(dtrace_ecb_t *ecb)
6940 dtrace_probe_t *probe = ecb->dte_probe;
6943 * It's impossible to be taking action on the NULL probe.
6945 ASSERT(probe != NULL);
6947 if (dtrace_destructive_disallow)
6950 if (dtrace_panicked != NULL)
6953 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL)
6957 * We won the right to panic. (We want to be sure that only one
6958 * thread calls panic() from dtrace_probe(), and that panic() is
6959 * called exactly once.)
6961 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
6962 probe->dtpr_provider->dtpv_name, probe->dtpr_mod,
6963 probe->dtpr_func, probe->dtpr_name, (void *)ecb);
6967 dtrace_action_raise(uint64_t sig)
6969 if (dtrace_destructive_disallow)
6973 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6979 * raise() has a queue depth of 1 -- we ignore all subsequent
6980 * invocations of the raise() action.
6982 if (curthread->t_dtrace_sig == 0)
6983 curthread->t_dtrace_sig = (uint8_t)sig;
6985 curthread->t_sig_check = 1;
6988 struct proc *p = curproc;
6990 kern_psignal(p, sig);
6996 dtrace_action_stop(void)
6998 if (dtrace_destructive_disallow)
7002 if (!curthread->t_dtrace_stop) {
7003 curthread->t_dtrace_stop = 1;
7004 curthread->t_sig_check = 1;
7008 struct proc *p = curproc;
7010 kern_psignal(p, SIGSTOP);
7016 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val)
7019 volatile uint16_t *flags;
7023 cpu_t *cpu = &solaris_cpu[curcpu];
7026 if (dtrace_destructive_disallow)
7029 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
7031 now = dtrace_gethrtime();
7033 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) {
7035 * We need to advance the mark to the current time.
7037 cpu->cpu_dtrace_chillmark = now;
7038 cpu->cpu_dtrace_chilled = 0;
7042 * Now check to see if the requested chill time would take us over
7043 * the maximum amount of time allowed in the chill interval. (Or
7044 * worse, if the calculation itself induces overflow.)
7046 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max ||
7047 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) {
7048 *flags |= CPU_DTRACE_ILLOP;
7052 while (dtrace_gethrtime() - now < val)
7056 * Normally, we assure that the value of the variable "timestamp" does
7057 * not change within an ECB. The presence of chill() represents an
7058 * exception to this rule, however.
7060 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP;
7061 cpu->cpu_dtrace_chilled += val;
7065 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state,
7066 uint64_t *buf, uint64_t arg)
7068 int nframes = DTRACE_USTACK_NFRAMES(arg);
7069 int strsize = DTRACE_USTACK_STRSIZE(arg);
7070 uint64_t *pcs = &buf[1], *fps;
7071 char *str = (char *)&pcs[nframes];
7072 int size, offs = 0, i, j;
7074 uintptr_t old = mstate->dtms_scratch_ptr, saved;
7075 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
7079 * Should be taking a faster path if string space has not been
7082 ASSERT(strsize != 0);
7085 * We will first allocate some temporary space for the frame pointers.
7087 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
7088 size = (uintptr_t)fps - mstate->dtms_scratch_ptr +
7089 (nframes * sizeof (uint64_t));
7091 if (!DTRACE_INSCRATCH(mstate, size)) {
7093 * Not enough room for our frame pointers -- need to indicate
7094 * that we ran out of scratch space.
7096 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
7100 mstate->dtms_scratch_ptr += size;
7101 saved = mstate->dtms_scratch_ptr;
7104 * Now get a stack with both program counters and frame pointers.
7106 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7107 dtrace_getufpstack(buf, fps, nframes + 1);
7108 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7111 * If that faulted, we're cooked.
7113 if (*flags & CPU_DTRACE_FAULT)
7117 * Now we want to walk up the stack, calling the USTACK helper. For
7118 * each iteration, we restore the scratch pointer.
7120 for (i = 0; i < nframes; i++) {
7121 mstate->dtms_scratch_ptr = saved;
7123 if (offs >= strsize)
7126 sym = (char *)(uintptr_t)dtrace_helper(
7127 DTRACE_HELPER_ACTION_USTACK,
7128 mstate, state, pcs[i], fps[i]);
7131 * If we faulted while running the helper, we're going to
7132 * clear the fault and null out the corresponding string.
7134 if (*flags & CPU_DTRACE_FAULT) {
7135 *flags &= ~CPU_DTRACE_FAULT;
7145 if (!dtrace_strcanload((uintptr_t)sym, strsize, &rem, mstate,
7146 &(state->dts_vstate))) {
7151 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7154 * Now copy in the string that the helper returned to us.
7156 for (j = 0; offs + j < strsize && j < rem; j++) {
7157 if ((str[offs + j] = sym[j]) == '\0')
7161 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7166 if (offs >= strsize) {
7168 * If we didn't have room for all of the strings, we don't
7169 * abort processing -- this needn't be a fatal error -- but we
7170 * still want to increment a counter (dts_stkstroverflows) to
7171 * allow this condition to be warned about. (If this is from
7172 * a jstack() action, it is easily tuned via jstackstrsize.)
7174 dtrace_error(&state->dts_stkstroverflows);
7177 while (offs < strsize)
7181 mstate->dtms_scratch_ptr = old;
7185 dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size,
7186 size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind)
7188 volatile uint16_t *flags;
7189 uint64_t val = *valp;
7190 size_t valoffs = *valoffsp;
7192 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
7193 ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF);
7196 * If this is a string, we're going to only load until we find the zero
7197 * byte -- after which we'll store zero bytes.
7199 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
7203 for (s = 0; s < size; s++) {
7204 if (c != '\0' && dtkind == DIF_TF_BYREF) {
7205 c = dtrace_load8(val++);
7206 } else if (c != '\0' && dtkind == DIF_TF_BYUREF) {
7207 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7208 c = dtrace_fuword8((void *)(uintptr_t)val++);
7209 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7210 if (*flags & CPU_DTRACE_FAULT)
7214 DTRACE_STORE(uint8_t, tomax, valoffs++, c);
7216 if (c == '\0' && intuple)
7221 while (valoffs < end) {
7222 if (dtkind == DIF_TF_BYREF) {
7223 c = dtrace_load8(val++);
7224 } else if (dtkind == DIF_TF_BYUREF) {
7225 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7226 c = dtrace_fuword8((void *)(uintptr_t)val++);
7227 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7228 if (*flags & CPU_DTRACE_FAULT)
7232 DTRACE_STORE(uint8_t, tomax,
7238 *valoffsp = valoffs;
7242 * If you're looking for the epicenter of DTrace, you just found it. This
7243 * is the function called by the provider to fire a probe -- from which all
7244 * subsequent probe-context DTrace activity emanates.
7247 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
7248 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
7250 processorid_t cpuid;
7251 dtrace_icookie_t cookie;
7252 dtrace_probe_t *probe;
7253 dtrace_mstate_t mstate;
7255 dtrace_action_t *act;
7259 volatile uint16_t *flags;
7262 if (panicstr != NULL)
7267 * Kick out immediately if this CPU is still being born (in which case
7268 * curthread will be set to -1) or the current thread can't allow
7269 * probes in its current context.
7271 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE))
7275 cookie = dtrace_interrupt_disable();
7276 probe = dtrace_probes[id - 1];
7278 onintr = CPU_ON_INTR(CPU);
7280 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE &&
7281 probe->dtpr_predcache == curthread->t_predcache) {
7283 * We have hit in the predicate cache; we know that
7284 * this predicate would evaluate to be false.
7286 dtrace_interrupt_enable(cookie);
7291 if (panic_quiesce) {
7293 if (panicstr != NULL) {
7296 * We don't trace anything if we're panicking.
7298 dtrace_interrupt_enable(cookie);
7302 now = mstate.dtms_timestamp = dtrace_gethrtime();
7303 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP;
7304 vtime = dtrace_vtime_references != 0;
7306 if (vtime && curthread->t_dtrace_start)
7307 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start;
7309 mstate.dtms_difo = NULL;
7310 mstate.dtms_probe = probe;
7311 mstate.dtms_strtok = 0;
7312 mstate.dtms_arg[0] = arg0;
7313 mstate.dtms_arg[1] = arg1;
7314 mstate.dtms_arg[2] = arg2;
7315 mstate.dtms_arg[3] = arg3;
7316 mstate.dtms_arg[4] = arg4;
7318 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags;
7320 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
7321 dtrace_predicate_t *pred = ecb->dte_predicate;
7322 dtrace_state_t *state = ecb->dte_state;
7323 dtrace_buffer_t *buf = &state->dts_buffer[cpuid];
7324 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid];
7325 dtrace_vstate_t *vstate = &state->dts_vstate;
7326 dtrace_provider_t *prov = probe->dtpr_provider;
7327 uint64_t tracememsize = 0;
7332 * A little subtlety with the following (seemingly innocuous)
7333 * declaration of the automatic 'val': by looking at the
7334 * code, you might think that it could be declared in the
7335 * action processing loop, below. (That is, it's only used in
7336 * the action processing loop.) However, it must be declared
7337 * out of that scope because in the case of DIF expression
7338 * arguments to aggregating actions, one iteration of the
7339 * action loop will use the last iteration's value.
7343 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE;
7344 mstate.dtms_getf = NULL;
7346 *flags &= ~CPU_DTRACE_ERROR;
7348 if (prov == dtrace_provider) {
7350 * If dtrace itself is the provider of this probe,
7351 * we're only going to continue processing the ECB if
7352 * arg0 (the dtrace_state_t) is equal to the ECB's
7353 * creating state. (This prevents disjoint consumers
7354 * from seeing one another's metaprobes.)
7356 if (arg0 != (uint64_t)(uintptr_t)state)
7360 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) {
7362 * We're not currently active. If our provider isn't
7363 * the dtrace pseudo provider, we're not interested.
7365 if (prov != dtrace_provider)
7369 * Now we must further check if we are in the BEGIN
7370 * probe. If we are, we will only continue processing
7371 * if we're still in WARMUP -- if one BEGIN enabling
7372 * has invoked the exit() action, we don't want to
7373 * evaluate subsequent BEGIN enablings.
7375 if (probe->dtpr_id == dtrace_probeid_begin &&
7376 state->dts_activity != DTRACE_ACTIVITY_WARMUP) {
7377 ASSERT(state->dts_activity ==
7378 DTRACE_ACTIVITY_DRAINING);
7383 if (ecb->dte_cond) {
7385 * If the dte_cond bits indicate that this
7386 * consumer is only allowed to see user-mode firings
7387 * of this probe, call the provider's dtps_usermode()
7388 * entry point to check that the probe was fired
7389 * while in a user context. Skip this ECB if that's
7392 if ((ecb->dte_cond & DTRACE_COND_USERMODE) &&
7393 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg,
7394 probe->dtpr_id, probe->dtpr_arg) == 0)
7399 * This is more subtle than it looks. We have to be
7400 * absolutely certain that CRED() isn't going to
7401 * change out from under us so it's only legit to
7402 * examine that structure if we're in constrained
7403 * situations. Currently, the only times we'll this
7404 * check is if a non-super-user has enabled the
7405 * profile or syscall providers -- providers that
7406 * allow visibility of all processes. For the
7407 * profile case, the check above will ensure that
7408 * we're examining a user context.
7410 if (ecb->dte_cond & DTRACE_COND_OWNER) {
7413 ecb->dte_state->dts_cred.dcr_cred;
7416 ASSERT(s_cr != NULL);
7418 if ((cr = CRED()) == NULL ||
7419 s_cr->cr_uid != cr->cr_uid ||
7420 s_cr->cr_uid != cr->cr_ruid ||
7421 s_cr->cr_uid != cr->cr_suid ||
7422 s_cr->cr_gid != cr->cr_gid ||
7423 s_cr->cr_gid != cr->cr_rgid ||
7424 s_cr->cr_gid != cr->cr_sgid ||
7425 (proc = ttoproc(curthread)) == NULL ||
7426 (proc->p_flag & SNOCD))
7430 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
7433 ecb->dte_state->dts_cred.dcr_cred;
7435 ASSERT(s_cr != NULL);
7437 if ((cr = CRED()) == NULL ||
7438 s_cr->cr_zone->zone_id !=
7439 cr->cr_zone->zone_id)
7445 if (now - state->dts_alive > dtrace_deadman_timeout) {
7447 * We seem to be dead. Unless we (a) have kernel
7448 * destructive permissions (b) have explicitly enabled
7449 * destructive actions and (c) destructive actions have
7450 * not been disabled, we're going to transition into
7451 * the KILLED state, from which no further processing
7452 * on this state will be performed.
7454 if (!dtrace_priv_kernel_destructive(state) ||
7455 !state->dts_cred.dcr_destructive ||
7456 dtrace_destructive_disallow) {
7457 void *activity = &state->dts_activity;
7458 dtrace_activity_t current;
7461 current = state->dts_activity;
7462 } while (dtrace_cas32(activity, current,
7463 DTRACE_ACTIVITY_KILLED) != current);
7469 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed,
7470 ecb->dte_alignment, state, &mstate)) < 0)
7473 tomax = buf->dtb_tomax;
7474 ASSERT(tomax != NULL);
7476 if (ecb->dte_size != 0) {
7477 dtrace_rechdr_t dtrh;
7478 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
7479 mstate.dtms_timestamp = dtrace_gethrtime();
7480 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP;
7482 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t));
7483 dtrh.dtrh_epid = ecb->dte_epid;
7484 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh,
7485 mstate.dtms_timestamp);
7486 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh;
7489 mstate.dtms_epid = ecb->dte_epid;
7490 mstate.dtms_present |= DTRACE_MSTATE_EPID;
7492 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)
7493 mstate.dtms_access = DTRACE_ACCESS_KERNEL;
7495 mstate.dtms_access = 0;
7498 dtrace_difo_t *dp = pred->dtp_difo;
7501 rval = dtrace_dif_emulate(dp, &mstate, vstate, state);
7503 if (!(*flags & CPU_DTRACE_ERROR) && !rval) {
7504 dtrace_cacheid_t cid = probe->dtpr_predcache;
7506 if (cid != DTRACE_CACHEIDNONE && !onintr) {
7508 * Update the predicate cache...
7510 ASSERT(cid == pred->dtp_cacheid);
7511 curthread->t_predcache = cid;
7518 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) &&
7519 act != NULL; act = act->dta_next) {
7522 dtrace_recdesc_t *rec = &act->dta_rec;
7524 size = rec->dtrd_size;
7525 valoffs = offs + rec->dtrd_offset;
7527 if (DTRACEACT_ISAGG(act->dta_kind)) {
7529 dtrace_aggregation_t *agg;
7531 agg = (dtrace_aggregation_t *)act;
7533 if ((dp = act->dta_difo) != NULL)
7534 v = dtrace_dif_emulate(dp,
7535 &mstate, vstate, state);
7537 if (*flags & CPU_DTRACE_ERROR)
7541 * Note that we always pass the expression
7542 * value from the previous iteration of the
7543 * action loop. This value will only be used
7544 * if there is an expression argument to the
7545 * aggregating action, denoted by the
7546 * dtag_hasarg field.
7548 dtrace_aggregate(agg, buf,
7549 offs, aggbuf, v, val);
7553 switch (act->dta_kind) {
7554 case DTRACEACT_STOP:
7555 if (dtrace_priv_proc_destructive(state))
7556 dtrace_action_stop();
7559 case DTRACEACT_BREAKPOINT:
7560 if (dtrace_priv_kernel_destructive(state))
7561 dtrace_action_breakpoint(ecb);
7564 case DTRACEACT_PANIC:
7565 if (dtrace_priv_kernel_destructive(state))
7566 dtrace_action_panic(ecb);
7569 case DTRACEACT_STACK:
7570 if (!dtrace_priv_kernel(state))
7573 dtrace_getpcstack((pc_t *)(tomax + valoffs),
7574 size / sizeof (pc_t), probe->dtpr_aframes,
7575 DTRACE_ANCHORED(probe) ? NULL :
7579 case DTRACEACT_JSTACK:
7580 case DTRACEACT_USTACK:
7581 if (!dtrace_priv_proc(state))
7585 * See comment in DIF_VAR_PID.
7587 if (DTRACE_ANCHORED(mstate.dtms_probe) &&
7589 int depth = DTRACE_USTACK_NFRAMES(
7592 dtrace_bzero((void *)(tomax + valoffs),
7593 DTRACE_USTACK_STRSIZE(rec->dtrd_arg)
7594 + depth * sizeof (uint64_t));
7599 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 &&
7600 curproc->p_dtrace_helpers != NULL) {
7602 * This is the slow path -- we have
7603 * allocated string space, and we're
7604 * getting the stack of a process that
7605 * has helpers. Call into a separate
7606 * routine to perform this processing.
7608 dtrace_action_ustack(&mstate, state,
7609 (uint64_t *)(tomax + valoffs),
7614 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7615 dtrace_getupcstack((uint64_t *)
7617 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1);
7618 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7628 val = dtrace_dif_emulate(dp, &mstate, vstate, state);
7630 if (*flags & CPU_DTRACE_ERROR)
7633 switch (act->dta_kind) {
7634 case DTRACEACT_SPECULATE: {
7635 dtrace_rechdr_t *dtrh;
7637 ASSERT(buf == &state->dts_buffer[cpuid]);
7638 buf = dtrace_speculation_buffer(state,
7642 *flags |= CPU_DTRACE_DROP;
7646 offs = dtrace_buffer_reserve(buf,
7647 ecb->dte_needed, ecb->dte_alignment,
7651 *flags |= CPU_DTRACE_DROP;
7655 tomax = buf->dtb_tomax;
7656 ASSERT(tomax != NULL);
7658 if (ecb->dte_size == 0)
7661 ASSERT3U(ecb->dte_size, >=,
7662 sizeof (dtrace_rechdr_t));
7663 dtrh = ((void *)(tomax + offs));
7664 dtrh->dtrh_epid = ecb->dte_epid;
7666 * When the speculation is committed, all of
7667 * the records in the speculative buffer will
7668 * have their timestamps set to the commit
7669 * time. Until then, it is set to a sentinel
7670 * value, for debugability.
7672 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX);
7676 case DTRACEACT_PRINTM: {
7677 /* The DIF returns a 'memref'. */
7678 uintptr_t *memref = (uintptr_t *)(uintptr_t) val;
7680 /* Get the size from the memref. */
7684 * Check if the size exceeds the allocated
7687 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) {
7689 *flags |= CPU_DTRACE_DROP;
7693 /* Store the size in the buffer first. */
7694 DTRACE_STORE(uintptr_t, tomax,
7698 * Offset the buffer address to the start
7701 valoffs += sizeof(uintptr_t);
7704 * Reset to the memory address rather than
7705 * the memref array, then let the BYREF
7706 * code below do the work to store the
7707 * memory data in the buffer.
7713 case DTRACEACT_CHILL:
7714 if (dtrace_priv_kernel_destructive(state))
7715 dtrace_action_chill(&mstate, val);
7718 case DTRACEACT_RAISE:
7719 if (dtrace_priv_proc_destructive(state))
7720 dtrace_action_raise(val);
7723 case DTRACEACT_COMMIT:
7727 * We need to commit our buffer state.
7730 buf->dtb_offset = offs + ecb->dte_size;
7731 buf = &state->dts_buffer[cpuid];
7732 dtrace_speculation_commit(state, cpuid, val);
7736 case DTRACEACT_DISCARD:
7737 dtrace_speculation_discard(state, cpuid, val);
7740 case DTRACEACT_DIFEXPR:
7741 case DTRACEACT_LIBACT:
7742 case DTRACEACT_PRINTF:
7743 case DTRACEACT_PRINTA:
7744 case DTRACEACT_SYSTEM:
7745 case DTRACEACT_FREOPEN:
7746 case DTRACEACT_TRACEMEM:
7749 case DTRACEACT_TRACEMEM_DYNSIZE:
7755 if (!dtrace_priv_kernel(state))
7759 case DTRACEACT_USYM:
7760 case DTRACEACT_UMOD:
7761 case DTRACEACT_UADDR: {
7763 struct pid *pid = curthread->t_procp->p_pidp;
7766 if (!dtrace_priv_proc(state))
7769 DTRACE_STORE(uint64_t, tomax,
7771 valoffs, (uint64_t)pid->pid_id);
7773 valoffs, (uint64_t) curproc->p_pid);
7775 DTRACE_STORE(uint64_t, tomax,
7776 valoffs + sizeof (uint64_t), val);
7781 case DTRACEACT_EXIT: {
7783 * For the exit action, we are going to attempt
7784 * to atomically set our activity to be
7785 * draining. If this fails (either because
7786 * another CPU has beat us to the exit action,
7787 * or because our current activity is something
7788 * other than ACTIVE or WARMUP), we will
7789 * continue. This assures that the exit action
7790 * can be successfully recorded at most once
7791 * when we're in the ACTIVE state. If we're
7792 * encountering the exit() action while in
7793 * COOLDOWN, however, we want to honor the new
7794 * status code. (We know that we're the only
7795 * thread in COOLDOWN, so there is no race.)
7797 void *activity = &state->dts_activity;
7798 dtrace_activity_t current = state->dts_activity;
7800 if (current == DTRACE_ACTIVITY_COOLDOWN)
7803 if (current != DTRACE_ACTIVITY_WARMUP)
7804 current = DTRACE_ACTIVITY_ACTIVE;
7806 if (dtrace_cas32(activity, current,
7807 DTRACE_ACTIVITY_DRAINING) != current) {
7808 *flags |= CPU_DTRACE_DROP;
7819 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ||
7820 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) {
7821 uintptr_t end = valoffs + size;
7823 if (tracememsize != 0 &&
7824 valoffs + tracememsize < end) {
7825 end = valoffs + tracememsize;
7829 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF &&
7830 !dtrace_vcanload((void *)(uintptr_t)val,
7831 &dp->dtdo_rtype, NULL, &mstate, vstate))
7834 dtrace_store_by_ref(dp, tomax, size, &valoffs,
7835 &val, end, act->dta_intuple,
7836 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ?
7837 DIF_TF_BYREF: DIF_TF_BYUREF);
7845 case sizeof (uint8_t):
7846 DTRACE_STORE(uint8_t, tomax, valoffs, val);
7848 case sizeof (uint16_t):
7849 DTRACE_STORE(uint16_t, tomax, valoffs, val);
7851 case sizeof (uint32_t):
7852 DTRACE_STORE(uint32_t, tomax, valoffs, val);
7854 case sizeof (uint64_t):
7855 DTRACE_STORE(uint64_t, tomax, valoffs, val);
7859 * Any other size should have been returned by
7860 * reference, not by value.
7867 if (*flags & CPU_DTRACE_DROP)
7870 if (*flags & CPU_DTRACE_FAULT) {
7872 dtrace_action_t *err;
7876 if (probe->dtpr_id == dtrace_probeid_error) {
7878 * There's nothing we can do -- we had an
7879 * error on the error probe. We bump an
7880 * error counter to at least indicate that
7881 * this condition happened.
7883 dtrace_error(&state->dts_dblerrors);
7889 * Before recursing on dtrace_probe(), we
7890 * need to explicitly clear out our start
7891 * time to prevent it from being accumulated
7892 * into t_dtrace_vtime.
7894 curthread->t_dtrace_start = 0;
7898 * Iterate over the actions to figure out which action
7899 * we were processing when we experienced the error.
7900 * Note that act points _past_ the faulting action; if
7901 * act is ecb->dte_action, the fault was in the
7902 * predicate, if it's ecb->dte_action->dta_next it's
7903 * in action #1, and so on.
7905 for (err = ecb->dte_action, ndx = 0;
7906 err != act; err = err->dta_next, ndx++)
7909 dtrace_probe_error(state, ecb->dte_epid, ndx,
7910 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ?
7911 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags),
7912 cpu_core[cpuid].cpuc_dtrace_illval);
7918 buf->dtb_offset = offs + ecb->dte_size;
7922 curthread->t_dtrace_start = dtrace_gethrtime();
7924 dtrace_interrupt_enable(cookie);
7928 * DTrace Probe Hashing Functions
7930 * The functions in this section (and indeed, the functions in remaining
7931 * sections) are not _called_ from probe context. (Any exceptions to this are
7932 * marked with a "Note:".) Rather, they are called from elsewhere in the
7933 * DTrace framework to look-up probes in, add probes to and remove probes from
7934 * the DTrace probe hashes. (Each probe is hashed by each element of the
7935 * probe tuple -- allowing for fast lookups, regardless of what was
7939 dtrace_hash_str(const char *p)
7945 hval = (hval << 4) + *p++;
7946 if ((g = (hval & 0xf0000000)) != 0)
7953 static dtrace_hash_t *
7954 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs)
7956 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP);
7958 hash->dth_stroffs = stroffs;
7959 hash->dth_nextoffs = nextoffs;
7960 hash->dth_prevoffs = prevoffs;
7963 hash->dth_mask = hash->dth_size - 1;
7965 hash->dth_tab = kmem_zalloc(hash->dth_size *
7966 sizeof (dtrace_hashbucket_t *), KM_SLEEP);
7972 dtrace_hash_destroy(dtrace_hash_t *hash)
7977 for (i = 0; i < hash->dth_size; i++)
7978 ASSERT(hash->dth_tab[i] == NULL);
7981 kmem_free(hash->dth_tab,
7982 hash->dth_size * sizeof (dtrace_hashbucket_t *));
7983 kmem_free(hash, sizeof (dtrace_hash_t));
7987 dtrace_hash_resize(dtrace_hash_t *hash)
7989 int size = hash->dth_size, i, ndx;
7990 int new_size = hash->dth_size << 1;
7991 int new_mask = new_size - 1;
7992 dtrace_hashbucket_t **new_tab, *bucket, *next;
7994 ASSERT((new_size & new_mask) == 0);
7996 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP);
7998 for (i = 0; i < size; i++) {
7999 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) {
8000 dtrace_probe_t *probe = bucket->dthb_chain;
8002 ASSERT(probe != NULL);
8003 ndx = DTRACE_HASHSTR(hash, probe) & new_mask;
8005 next = bucket->dthb_next;
8006 bucket->dthb_next = new_tab[ndx];
8007 new_tab[ndx] = bucket;
8011 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *));
8012 hash->dth_tab = new_tab;
8013 hash->dth_size = new_size;
8014 hash->dth_mask = new_mask;
8018 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new)
8020 int hashval = DTRACE_HASHSTR(hash, new);
8021 int ndx = hashval & hash->dth_mask;
8022 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
8023 dtrace_probe_t **nextp, **prevp;
8025 for (; bucket != NULL; bucket = bucket->dthb_next) {
8026 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new))
8030 if ((hash->dth_nbuckets >> 1) > hash->dth_size) {
8031 dtrace_hash_resize(hash);
8032 dtrace_hash_add(hash, new);
8036 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP);
8037 bucket->dthb_next = hash->dth_tab[ndx];
8038 hash->dth_tab[ndx] = bucket;
8039 hash->dth_nbuckets++;
8042 nextp = DTRACE_HASHNEXT(hash, new);
8043 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL);
8044 *nextp = bucket->dthb_chain;
8046 if (bucket->dthb_chain != NULL) {
8047 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain);
8048 ASSERT(*prevp == NULL);
8052 bucket->dthb_chain = new;
8056 static dtrace_probe_t *
8057 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template)
8059 int hashval = DTRACE_HASHSTR(hash, template);
8060 int ndx = hashval & hash->dth_mask;
8061 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
8063 for (; bucket != NULL; bucket = bucket->dthb_next) {
8064 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
8065 return (bucket->dthb_chain);
8072 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template)
8074 int hashval = DTRACE_HASHSTR(hash, template);
8075 int ndx = hashval & hash->dth_mask;
8076 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
8078 for (; bucket != NULL; bucket = bucket->dthb_next) {
8079 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
8080 return (bucket->dthb_len);
8087 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe)
8089 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask;
8090 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
8092 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe);
8093 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe);
8096 * Find the bucket that we're removing this probe from.
8098 for (; bucket != NULL; bucket = bucket->dthb_next) {
8099 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe))
8103 ASSERT(bucket != NULL);
8105 if (*prevp == NULL) {
8106 if (*nextp == NULL) {
8108 * The removed probe was the only probe on this
8109 * bucket; we need to remove the bucket.
8111 dtrace_hashbucket_t *b = hash->dth_tab[ndx];
8113 ASSERT(bucket->dthb_chain == probe);
8117 hash->dth_tab[ndx] = bucket->dthb_next;
8119 while (b->dthb_next != bucket)
8121 b->dthb_next = bucket->dthb_next;
8124 ASSERT(hash->dth_nbuckets > 0);
8125 hash->dth_nbuckets--;
8126 kmem_free(bucket, sizeof (dtrace_hashbucket_t));
8130 bucket->dthb_chain = *nextp;
8132 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp;
8136 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp;
8140 * DTrace Utility Functions
8142 * These are random utility functions that are _not_ called from probe context.
8145 dtrace_badattr(const dtrace_attribute_t *a)
8147 return (a->dtat_name > DTRACE_STABILITY_MAX ||
8148 a->dtat_data > DTRACE_STABILITY_MAX ||
8149 a->dtat_class > DTRACE_CLASS_MAX);
8153 * Return a duplicate copy of a string. If the specified string is NULL,
8154 * this function returns a zero-length string.
8157 dtrace_strdup(const char *str)
8159 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP);
8162 (void) strcpy(new, str);
8167 #define DTRACE_ISALPHA(c) \
8168 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
8171 dtrace_badname(const char *s)
8175 if (s == NULL || (c = *s++) == '\0')
8178 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.')
8181 while ((c = *s++) != '\0') {
8182 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') &&
8183 c != '-' && c != '_' && c != '.' && c != '`')
8191 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp)
8196 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
8198 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
8200 priv = DTRACE_PRIV_ALL;
8202 *uidp = crgetuid(cr);
8203 *zoneidp = crgetzoneid(cr);
8206 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE))
8207 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER;
8208 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE))
8209 priv |= DTRACE_PRIV_USER;
8210 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE))
8211 priv |= DTRACE_PRIV_PROC;
8212 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
8213 priv |= DTRACE_PRIV_OWNER;
8214 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
8215 priv |= DTRACE_PRIV_ZONEOWNER;
8218 priv = DTRACE_PRIV_ALL;
8224 #ifdef DTRACE_ERRDEBUG
8226 dtrace_errdebug(const char *str)
8228 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ;
8231 mutex_enter(&dtrace_errlock);
8232 dtrace_errlast = str;
8233 dtrace_errthread = curthread;
8235 while (occupied++ < DTRACE_ERRHASHSZ) {
8236 if (dtrace_errhash[hval].dter_msg == str) {
8237 dtrace_errhash[hval].dter_count++;
8241 if (dtrace_errhash[hval].dter_msg != NULL) {
8242 hval = (hval + 1) % DTRACE_ERRHASHSZ;
8246 dtrace_errhash[hval].dter_msg = str;
8247 dtrace_errhash[hval].dter_count = 1;
8251 panic("dtrace: undersized error hash");
8253 mutex_exit(&dtrace_errlock);
8258 * DTrace Matching Functions
8260 * These functions are used to match groups of probes, given some elements of
8261 * a probe tuple, or some globbed expressions for elements of a probe tuple.
8264 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid,
8267 if (priv != DTRACE_PRIV_ALL) {
8268 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags;
8269 uint32_t match = priv & ppriv;
8272 * No PRIV_DTRACE_* privileges...
8274 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER |
8275 DTRACE_PRIV_KERNEL)) == 0)
8279 * No matching bits, but there were bits to match...
8281 if (match == 0 && ppriv != 0)
8285 * Need to have permissions to the process, but don't...
8287 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 &&
8288 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) {
8293 * Need to be in the same zone unless we possess the
8294 * privilege to examine all zones.
8296 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 &&
8297 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) {
8306 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
8307 * consists of input pattern strings and an ops-vector to evaluate them.
8308 * This function returns >0 for match, 0 for no match, and <0 for error.
8311 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp,
8312 uint32_t priv, uid_t uid, zoneid_t zoneid)
8314 dtrace_provider_t *pvp = prp->dtpr_provider;
8317 if (pvp->dtpv_defunct)
8320 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0)
8323 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0)
8326 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0)
8329 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0)
8332 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0)
8339 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
8340 * interface for matching a glob pattern 'p' to an input string 's'. Unlike
8341 * libc's version, the kernel version only applies to 8-bit ASCII strings.
8342 * In addition, all of the recursion cases except for '*' matching have been
8343 * unwound. For '*', we still implement recursive evaluation, but a depth
8344 * counter is maintained and matching is aborted if we recurse too deep.
8345 * The function returns 0 if no match, >0 if match, and <0 if recursion error.
8348 dtrace_match_glob(const char *s, const char *p, int depth)
8354 if (depth > DTRACE_PROBEKEY_MAXDEPTH)
8358 s = ""; /* treat NULL as empty string */
8367 if ((c = *p++) == '\0')
8368 return (s1 == '\0');
8372 int ok = 0, notflag = 0;
8383 if ((c = *p++) == '\0')
8387 if (c == '-' && lc != '\0' && *p != ']') {
8388 if ((c = *p++) == '\0')
8390 if (c == '\\' && (c = *p++) == '\0')
8394 if (s1 < lc || s1 > c)
8398 } else if (lc <= s1 && s1 <= c)
8401 } else if (c == '\\' && (c = *p++) == '\0')
8404 lc = c; /* save left-hand 'c' for next iteration */
8414 if ((c = *p++) == '\0')
8426 if ((c = *p++) == '\0')
8442 p++; /* consecutive *'s are identical to a single one */
8447 for (s = olds; *s != '\0'; s++) {
8448 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0)
8458 dtrace_match_string(const char *s, const char *p, int depth)
8460 return (s != NULL && strcmp(s, p) == 0);
8465 dtrace_match_nul(const char *s, const char *p, int depth)
8467 return (1); /* always match the empty pattern */
8472 dtrace_match_nonzero(const char *s, const char *p, int depth)
8474 return (s != NULL && s[0] != '\0');
8478 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid,
8479 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg)
8481 dtrace_probe_t template, *probe;
8482 dtrace_hash_t *hash = NULL;
8483 int len, best = INT_MAX, nmatched = 0;
8486 ASSERT(MUTEX_HELD(&dtrace_lock));
8489 * If the probe ID is specified in the key, just lookup by ID and
8490 * invoke the match callback once if a matching probe is found.
8492 if (pkp->dtpk_id != DTRACE_IDNONE) {
8493 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL &&
8494 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) {
8495 (void) (*matched)(probe, arg);
8501 template.dtpr_mod = (char *)pkp->dtpk_mod;
8502 template.dtpr_func = (char *)pkp->dtpk_func;
8503 template.dtpr_name = (char *)pkp->dtpk_name;
8506 * We want to find the most distinct of the module name, function
8507 * name, and name. So for each one that is not a glob pattern or
8508 * empty string, we perform a lookup in the corresponding hash and
8509 * use the hash table with the fewest collisions to do our search.
8511 if (pkp->dtpk_mmatch == &dtrace_match_string &&
8512 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) {
8514 hash = dtrace_bymod;
8517 if (pkp->dtpk_fmatch == &dtrace_match_string &&
8518 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) {
8520 hash = dtrace_byfunc;
8523 if (pkp->dtpk_nmatch == &dtrace_match_string &&
8524 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) {
8526 hash = dtrace_byname;
8530 * If we did not select a hash table, iterate over every probe and
8531 * invoke our callback for each one that matches our input probe key.
8534 for (i = 0; i < dtrace_nprobes; i++) {
8535 if ((probe = dtrace_probes[i]) == NULL ||
8536 dtrace_match_probe(probe, pkp, priv, uid,
8542 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT)
8550 * If we selected a hash table, iterate over each probe of the same key
8551 * name and invoke the callback for every probe that matches the other
8552 * attributes of our input probe key.
8554 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL;
8555 probe = *(DTRACE_HASHNEXT(hash, probe))) {
8557 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0)
8562 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT)
8570 * Return the function pointer dtrace_probecmp() should use to compare the
8571 * specified pattern with a string. For NULL or empty patterns, we select
8572 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob().
8573 * For non-empty non-glob strings, we use dtrace_match_string().
8575 static dtrace_probekey_f *
8576 dtrace_probekey_func(const char *p)
8580 if (p == NULL || *p == '\0')
8581 return (&dtrace_match_nul);
8583 while ((c = *p++) != '\0') {
8584 if (c == '[' || c == '?' || c == '*' || c == '\\')
8585 return (&dtrace_match_glob);
8588 return (&dtrace_match_string);
8592 * Build a probe comparison key for use with dtrace_match_probe() from the
8593 * given probe description. By convention, a null key only matches anchored
8594 * probes: if each field is the empty string, reset dtpk_fmatch to
8595 * dtrace_match_nonzero().
8598 dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp)
8600 pkp->dtpk_prov = pdp->dtpd_provider;
8601 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider);
8603 pkp->dtpk_mod = pdp->dtpd_mod;
8604 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod);
8606 pkp->dtpk_func = pdp->dtpd_func;
8607 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func);
8609 pkp->dtpk_name = pdp->dtpd_name;
8610 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name);
8612 pkp->dtpk_id = pdp->dtpd_id;
8614 if (pkp->dtpk_id == DTRACE_IDNONE &&
8615 pkp->dtpk_pmatch == &dtrace_match_nul &&
8616 pkp->dtpk_mmatch == &dtrace_match_nul &&
8617 pkp->dtpk_fmatch == &dtrace_match_nul &&
8618 pkp->dtpk_nmatch == &dtrace_match_nul)
8619 pkp->dtpk_fmatch = &dtrace_match_nonzero;
8623 * DTrace Provider-to-Framework API Functions
8625 * These functions implement much of the Provider-to-Framework API, as
8626 * described in <sys/dtrace.h>. The parts of the API not in this section are
8627 * the functions in the API for probe management (found below), and
8628 * dtrace_probe() itself (found above).
8632 * Register the calling provider with the DTrace framework. This should
8633 * generally be called by DTrace providers in their attach(9E) entry point.
8636 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv,
8637 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp)
8639 dtrace_provider_t *provider;
8641 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) {
8642 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8643 "arguments", name ? name : "<NULL>");
8647 if (name[0] == '\0' || dtrace_badname(name)) {
8648 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8649 "provider name", name);
8653 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) ||
8654 pops->dtps_enable == NULL || pops->dtps_disable == NULL ||
8655 pops->dtps_destroy == NULL ||
8656 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) {
8657 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8658 "provider ops", name);
8662 if (dtrace_badattr(&pap->dtpa_provider) ||
8663 dtrace_badattr(&pap->dtpa_mod) ||
8664 dtrace_badattr(&pap->dtpa_func) ||
8665 dtrace_badattr(&pap->dtpa_name) ||
8666 dtrace_badattr(&pap->dtpa_args)) {
8667 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8668 "provider attributes", name);
8672 if (priv & ~DTRACE_PRIV_ALL) {
8673 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8674 "privilege attributes", name);
8678 if ((priv & DTRACE_PRIV_KERNEL) &&
8679 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) &&
8680 pops->dtps_usermode == NULL) {
8681 cmn_err(CE_WARN, "failed to register provider '%s': need "
8682 "dtps_usermode() op for given privilege attributes", name);
8686 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP);
8687 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8688 (void) strcpy(provider->dtpv_name, name);
8690 provider->dtpv_attr = *pap;
8691 provider->dtpv_priv.dtpp_flags = priv;
8693 provider->dtpv_priv.dtpp_uid = crgetuid(cr);
8694 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr);
8696 provider->dtpv_pops = *pops;
8698 if (pops->dtps_provide == NULL) {
8699 ASSERT(pops->dtps_provide_module != NULL);
8700 provider->dtpv_pops.dtps_provide =
8701 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop;
8704 if (pops->dtps_provide_module == NULL) {
8705 ASSERT(pops->dtps_provide != NULL);
8706 provider->dtpv_pops.dtps_provide_module =
8707 (void (*)(void *, modctl_t *))dtrace_nullop;
8710 if (pops->dtps_suspend == NULL) {
8711 ASSERT(pops->dtps_resume == NULL);
8712 provider->dtpv_pops.dtps_suspend =
8713 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
8714 provider->dtpv_pops.dtps_resume =
8715 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
8718 provider->dtpv_arg = arg;
8719 *idp = (dtrace_provider_id_t)provider;
8721 if (pops == &dtrace_provider_ops) {
8722 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8723 ASSERT(MUTEX_HELD(&dtrace_lock));
8724 ASSERT(dtrace_anon.dta_enabling == NULL);
8727 * We make sure that the DTrace provider is at the head of
8728 * the provider chain.
8730 provider->dtpv_next = dtrace_provider;
8731 dtrace_provider = provider;
8735 mutex_enter(&dtrace_provider_lock);
8736 mutex_enter(&dtrace_lock);
8739 * If there is at least one provider registered, we'll add this
8740 * provider after the first provider.
8742 if (dtrace_provider != NULL) {
8743 provider->dtpv_next = dtrace_provider->dtpv_next;
8744 dtrace_provider->dtpv_next = provider;
8746 dtrace_provider = provider;
8749 if (dtrace_retained != NULL) {
8750 dtrace_enabling_provide(provider);
8753 * Now we need to call dtrace_enabling_matchall() -- which
8754 * will acquire cpu_lock and dtrace_lock. We therefore need
8755 * to drop all of our locks before calling into it...
8757 mutex_exit(&dtrace_lock);
8758 mutex_exit(&dtrace_provider_lock);
8759 dtrace_enabling_matchall();
8764 mutex_exit(&dtrace_lock);
8765 mutex_exit(&dtrace_provider_lock);
8771 * Unregister the specified provider from the DTrace framework. This should
8772 * generally be called by DTrace providers in their detach(9E) entry point.
8775 dtrace_unregister(dtrace_provider_id_t id)
8777 dtrace_provider_t *old = (dtrace_provider_t *)id;
8778 dtrace_provider_t *prev = NULL;
8779 int i, self = 0, noreap = 0;
8780 dtrace_probe_t *probe, *first = NULL;
8782 if (old->dtpv_pops.dtps_enable ==
8783 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) {
8785 * If DTrace itself is the provider, we're called with locks
8788 ASSERT(old == dtrace_provider);
8790 ASSERT(dtrace_devi != NULL);
8792 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8793 ASSERT(MUTEX_HELD(&dtrace_lock));
8796 if (dtrace_provider->dtpv_next != NULL) {
8798 * There's another provider here; return failure.
8803 mutex_enter(&dtrace_provider_lock);
8805 mutex_enter(&mod_lock);
8807 mutex_enter(&dtrace_lock);
8811 * If anyone has /dev/dtrace open, or if there are anonymous enabled
8812 * probes, we refuse to let providers slither away, unless this
8813 * provider has already been explicitly invalidated.
8815 if (!old->dtpv_defunct &&
8816 (dtrace_opens || (dtrace_anon.dta_state != NULL &&
8817 dtrace_anon.dta_state->dts_necbs > 0))) {
8819 mutex_exit(&dtrace_lock);
8821 mutex_exit(&mod_lock);
8823 mutex_exit(&dtrace_provider_lock);
8829 * Attempt to destroy the probes associated with this provider.
8831 for (i = 0; i < dtrace_nprobes; i++) {
8832 if ((probe = dtrace_probes[i]) == NULL)
8835 if (probe->dtpr_provider != old)
8838 if (probe->dtpr_ecb == NULL)
8842 * If we are trying to unregister a defunct provider, and the
8843 * provider was made defunct within the interval dictated by
8844 * dtrace_unregister_defunct_reap, we'll (asynchronously)
8845 * attempt to reap our enablings. To denote that the provider
8846 * should reattempt to unregister itself at some point in the
8847 * future, we will return a differentiable error code (EAGAIN
8848 * instead of EBUSY) in this case.
8850 if (dtrace_gethrtime() - old->dtpv_defunct >
8851 dtrace_unregister_defunct_reap)
8855 mutex_exit(&dtrace_lock);
8857 mutex_exit(&mod_lock);
8859 mutex_exit(&dtrace_provider_lock);
8865 (void) taskq_dispatch(dtrace_taskq,
8866 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP);
8872 * All of the probes for this provider are disabled; we can safely
8873 * remove all of them from their hash chains and from the probe array.
8875 for (i = 0; i < dtrace_nprobes; i++) {
8876 if ((probe = dtrace_probes[i]) == NULL)
8879 if (probe->dtpr_provider != old)
8882 dtrace_probes[i] = NULL;
8884 dtrace_hash_remove(dtrace_bymod, probe);
8885 dtrace_hash_remove(dtrace_byfunc, probe);
8886 dtrace_hash_remove(dtrace_byname, probe);
8888 if (first == NULL) {
8890 probe->dtpr_nextmod = NULL;
8892 probe->dtpr_nextmod = first;
8898 * The provider's probes have been removed from the hash chains and
8899 * from the probe array. Now issue a dtrace_sync() to be sure that
8900 * everyone has cleared out from any probe array processing.
8904 for (probe = first; probe != NULL; probe = first) {
8905 first = probe->dtpr_nextmod;
8907 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id,
8909 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
8910 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
8911 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
8913 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1);
8915 free_unr(dtrace_arena, probe->dtpr_id);
8917 kmem_free(probe, sizeof (dtrace_probe_t));
8920 if ((prev = dtrace_provider) == old) {
8922 ASSERT(self || dtrace_devi == NULL);
8923 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL);
8925 dtrace_provider = old->dtpv_next;
8927 while (prev != NULL && prev->dtpv_next != old)
8928 prev = prev->dtpv_next;
8931 panic("attempt to unregister non-existent "
8932 "dtrace provider %p\n", (void *)id);
8935 prev->dtpv_next = old->dtpv_next;
8939 mutex_exit(&dtrace_lock);
8941 mutex_exit(&mod_lock);
8943 mutex_exit(&dtrace_provider_lock);
8946 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1);
8947 kmem_free(old, sizeof (dtrace_provider_t));
8953 * Invalidate the specified provider. All subsequent probe lookups for the
8954 * specified provider will fail, but its probes will not be removed.
8957 dtrace_invalidate(dtrace_provider_id_t id)
8959 dtrace_provider_t *pvp = (dtrace_provider_t *)id;
8961 ASSERT(pvp->dtpv_pops.dtps_enable !=
8962 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop);
8964 mutex_enter(&dtrace_provider_lock);
8965 mutex_enter(&dtrace_lock);
8967 pvp->dtpv_defunct = dtrace_gethrtime();
8969 mutex_exit(&dtrace_lock);
8970 mutex_exit(&dtrace_provider_lock);
8974 * Indicate whether or not DTrace has attached.
8977 dtrace_attached(void)
8980 * dtrace_provider will be non-NULL iff the DTrace driver has
8981 * attached. (It's non-NULL because DTrace is always itself a
8984 return (dtrace_provider != NULL);
8988 * Remove all the unenabled probes for the given provider. This function is
8989 * not unlike dtrace_unregister(), except that it doesn't remove the provider
8990 * -- just as many of its associated probes as it can.
8993 dtrace_condense(dtrace_provider_id_t id)
8995 dtrace_provider_t *prov = (dtrace_provider_t *)id;
8997 dtrace_probe_t *probe;
9000 * Make sure this isn't the dtrace provider itself.
9002 ASSERT(prov->dtpv_pops.dtps_enable !=
9003 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop);
9005 mutex_enter(&dtrace_provider_lock);
9006 mutex_enter(&dtrace_lock);
9009 * Attempt to destroy the probes associated with this provider.
9011 for (i = 0; i < dtrace_nprobes; i++) {
9012 if ((probe = dtrace_probes[i]) == NULL)
9015 if (probe->dtpr_provider != prov)
9018 if (probe->dtpr_ecb != NULL)
9021 dtrace_probes[i] = NULL;
9023 dtrace_hash_remove(dtrace_bymod, probe);
9024 dtrace_hash_remove(dtrace_byfunc, probe);
9025 dtrace_hash_remove(dtrace_byname, probe);
9027 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1,
9029 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
9030 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
9031 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
9032 kmem_free(probe, sizeof (dtrace_probe_t));
9034 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1);
9036 free_unr(dtrace_arena, i + 1);
9040 mutex_exit(&dtrace_lock);
9041 mutex_exit(&dtrace_provider_lock);
9047 * DTrace Probe Management Functions
9049 * The functions in this section perform the DTrace probe management,
9050 * including functions to create probes, look-up probes, and call into the
9051 * providers to request that probes be provided. Some of these functions are
9052 * in the Provider-to-Framework API; these functions can be identified by the
9053 * fact that they are not declared "static".
9057 * Create a probe with the specified module name, function name, and name.
9060 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod,
9061 const char *func, const char *name, int aframes, void *arg)
9063 dtrace_probe_t *probe, **probes;
9064 dtrace_provider_t *provider = (dtrace_provider_t *)prov;
9067 if (provider == dtrace_provider) {
9068 ASSERT(MUTEX_HELD(&dtrace_lock));
9070 mutex_enter(&dtrace_lock);
9074 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1,
9075 VM_BESTFIT | VM_SLEEP);
9077 id = alloc_unr(dtrace_arena);
9079 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP);
9081 probe->dtpr_id = id;
9082 probe->dtpr_gen = dtrace_probegen++;
9083 probe->dtpr_mod = dtrace_strdup(mod);
9084 probe->dtpr_func = dtrace_strdup(func);
9085 probe->dtpr_name = dtrace_strdup(name);
9086 probe->dtpr_arg = arg;
9087 probe->dtpr_aframes = aframes;
9088 probe->dtpr_provider = provider;
9090 dtrace_hash_add(dtrace_bymod, probe);
9091 dtrace_hash_add(dtrace_byfunc, probe);
9092 dtrace_hash_add(dtrace_byname, probe);
9094 if (id - 1 >= dtrace_nprobes) {
9095 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *);
9096 size_t nsize = osize << 1;
9100 ASSERT(dtrace_probes == NULL);
9101 nsize = sizeof (dtrace_probe_t *);
9104 probes = kmem_zalloc(nsize, KM_SLEEP);
9106 if (dtrace_probes == NULL) {
9108 dtrace_probes = probes;
9111 dtrace_probe_t **oprobes = dtrace_probes;
9113 bcopy(oprobes, probes, osize);
9114 dtrace_membar_producer();
9115 dtrace_probes = probes;
9120 * All CPUs are now seeing the new probes array; we can
9121 * safely free the old array.
9123 kmem_free(oprobes, osize);
9124 dtrace_nprobes <<= 1;
9127 ASSERT(id - 1 < dtrace_nprobes);
9130 ASSERT(dtrace_probes[id - 1] == NULL);
9131 dtrace_probes[id - 1] = probe;
9133 if (provider != dtrace_provider)
9134 mutex_exit(&dtrace_lock);
9139 static dtrace_probe_t *
9140 dtrace_probe_lookup_id(dtrace_id_t id)
9142 ASSERT(MUTEX_HELD(&dtrace_lock));
9144 if (id == 0 || id > dtrace_nprobes)
9147 return (dtrace_probes[id - 1]);
9151 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg)
9153 *((dtrace_id_t *)arg) = probe->dtpr_id;
9155 return (DTRACE_MATCH_DONE);
9159 * Look up a probe based on provider and one or more of module name, function
9160 * name and probe name.
9163 dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod,
9164 char *func, char *name)
9166 dtrace_probekey_t pkey;
9170 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name;
9171 pkey.dtpk_pmatch = &dtrace_match_string;
9172 pkey.dtpk_mod = mod;
9173 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul;
9174 pkey.dtpk_func = func;
9175 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul;
9176 pkey.dtpk_name = name;
9177 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul;
9178 pkey.dtpk_id = DTRACE_IDNONE;
9180 mutex_enter(&dtrace_lock);
9181 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0,
9182 dtrace_probe_lookup_match, &id);
9183 mutex_exit(&dtrace_lock);
9185 ASSERT(match == 1 || match == 0);
9186 return (match ? id : 0);
9190 * Returns the probe argument associated with the specified probe.
9193 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid)
9195 dtrace_probe_t *probe;
9198 mutex_enter(&dtrace_lock);
9200 if ((probe = dtrace_probe_lookup_id(pid)) != NULL &&
9201 probe->dtpr_provider == (dtrace_provider_t *)id)
9202 rval = probe->dtpr_arg;
9204 mutex_exit(&dtrace_lock);
9210 * Copy a probe into a probe description.
9213 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp)
9215 bzero(pdp, sizeof (dtrace_probedesc_t));
9216 pdp->dtpd_id = prp->dtpr_id;
9218 (void) strncpy(pdp->dtpd_provider,
9219 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1);
9221 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1);
9222 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1);
9223 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1);
9227 * Called to indicate that a probe -- or probes -- should be provided by a
9228 * specfied provider. If the specified description is NULL, the provider will
9229 * be told to provide all of its probes. (This is done whenever a new
9230 * consumer comes along, or whenever a retained enabling is to be matched.) If
9231 * the specified description is non-NULL, the provider is given the
9232 * opportunity to dynamically provide the specified probe, allowing providers
9233 * to support the creation of probes on-the-fly. (So-called _autocreated_
9234 * probes.) If the provider is NULL, the operations will be applied to all
9235 * providers; if the provider is non-NULL the operations will only be applied
9236 * to the specified provider. The dtrace_provider_lock must be held, and the
9237 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
9238 * will need to grab the dtrace_lock when it reenters the framework through
9239 * dtrace_probe_lookup(), dtrace_probe_create(), etc.
9242 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv)
9249 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
9253 prv = dtrace_provider;
9258 * First, call the blanket provide operation.
9260 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc);
9264 * Now call the per-module provide operation. We will grab
9265 * mod_lock to prevent the list from being modified. Note
9266 * that this also prevents the mod_busy bits from changing.
9267 * (mod_busy can only be changed with mod_lock held.)
9269 mutex_enter(&mod_lock);
9273 if (ctl->mod_busy || ctl->mod_mp == NULL)
9276 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
9278 } while ((ctl = ctl->mod_next) != &modules);
9280 mutex_exit(&mod_lock);
9282 } while (all && (prv = prv->dtpv_next) != NULL);
9287 * Iterate over each probe, and call the Framework-to-Provider API function
9291 dtrace_probe_foreach(uintptr_t offs)
9293 dtrace_provider_t *prov;
9294 void (*func)(void *, dtrace_id_t, void *);
9295 dtrace_probe_t *probe;
9296 dtrace_icookie_t cookie;
9300 * We disable interrupts to walk through the probe array. This is
9301 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
9302 * won't see stale data.
9304 cookie = dtrace_interrupt_disable();
9306 for (i = 0; i < dtrace_nprobes; i++) {
9307 if ((probe = dtrace_probes[i]) == NULL)
9310 if (probe->dtpr_ecb == NULL) {
9312 * This probe isn't enabled -- don't call the function.
9317 prov = probe->dtpr_provider;
9318 func = *((void(**)(void *, dtrace_id_t, void *))
9319 ((uintptr_t)&prov->dtpv_pops + offs));
9321 func(prov->dtpv_arg, i + 1, probe->dtpr_arg);
9324 dtrace_interrupt_enable(cookie);
9329 dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab)
9331 dtrace_probekey_t pkey;
9336 ASSERT(MUTEX_HELD(&dtrace_lock));
9337 dtrace_ecb_create_cache = NULL;
9341 * If we're passed a NULL description, we're being asked to
9342 * create an ECB with a NULL probe.
9344 (void) dtrace_ecb_create_enable(NULL, enab);
9348 dtrace_probekey(desc, &pkey);
9349 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred,
9350 &priv, &uid, &zoneid);
9352 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable,
9357 * DTrace Helper Provider Functions
9360 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr)
9362 attr->dtat_name = DOF_ATTR_NAME(dofattr);
9363 attr->dtat_data = DOF_ATTR_DATA(dofattr);
9364 attr->dtat_class = DOF_ATTR_CLASS(dofattr);
9368 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov,
9369 const dof_provider_t *dofprov, char *strtab)
9371 hprov->dthpv_provname = strtab + dofprov->dofpv_name;
9372 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider,
9373 dofprov->dofpv_provattr);
9374 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod,
9375 dofprov->dofpv_modattr);
9376 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func,
9377 dofprov->dofpv_funcattr);
9378 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name,
9379 dofprov->dofpv_nameattr);
9380 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args,
9381 dofprov->dofpv_argsattr);
9385 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
9387 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9388 dof_hdr_t *dof = (dof_hdr_t *)daddr;
9389 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
9390 dof_provider_t *provider;
9392 uint32_t *off, *enoff;
9396 dtrace_helper_provdesc_t dhpv;
9397 dtrace_helper_probedesc_t dhpb;
9398 dtrace_meta_t *meta = dtrace_meta_pid;
9399 dtrace_mops_t *mops = &meta->dtm_mops;
9402 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
9403 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9404 provider->dofpv_strtab * dof->dofh_secsize);
9405 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9406 provider->dofpv_probes * dof->dofh_secsize);
9407 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9408 provider->dofpv_prargs * dof->dofh_secsize);
9409 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9410 provider->dofpv_proffs * dof->dofh_secsize);
9412 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
9413 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset);
9414 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
9418 * See dtrace_helper_provider_validate().
9420 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
9421 provider->dofpv_prenoffs != DOF_SECT_NONE) {
9422 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9423 provider->dofpv_prenoffs * dof->dofh_secsize);
9424 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset);
9427 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
9430 * Create the provider.
9432 dtrace_dofprov2hprov(&dhpv, provider, strtab);
9434 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL)
9440 * Create the probes.
9442 for (i = 0; i < nprobes; i++) {
9443 probe = (dof_probe_t *)(uintptr_t)(daddr +
9444 prb_sec->dofs_offset + i * prb_sec->dofs_entsize);
9446 /* See the check in dtrace_helper_provider_validate(). */
9447 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN)
9450 dhpb.dthpb_mod = dhp->dofhp_mod;
9451 dhpb.dthpb_func = strtab + probe->dofpr_func;
9452 dhpb.dthpb_name = strtab + probe->dofpr_name;
9453 dhpb.dthpb_base = probe->dofpr_addr;
9454 dhpb.dthpb_offs = off + probe->dofpr_offidx;
9455 dhpb.dthpb_noffs = probe->dofpr_noffs;
9456 if (enoff != NULL) {
9457 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx;
9458 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs;
9460 dhpb.dthpb_enoffs = NULL;
9461 dhpb.dthpb_nenoffs = 0;
9463 dhpb.dthpb_args = arg + probe->dofpr_argidx;
9464 dhpb.dthpb_nargc = probe->dofpr_nargc;
9465 dhpb.dthpb_xargc = probe->dofpr_xargc;
9466 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv;
9467 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv;
9469 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb);
9474 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid)
9476 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9477 dof_hdr_t *dof = (dof_hdr_t *)daddr;
9480 ASSERT(MUTEX_HELD(&dtrace_meta_lock));
9482 for (i = 0; i < dof->dofh_secnum; i++) {
9483 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
9484 dof->dofh_secoff + i * dof->dofh_secsize);
9486 if (sec->dofs_type != DOF_SECT_PROVIDER)
9489 dtrace_helper_provide_one(dhp, sec, pid);
9493 * We may have just created probes, so we must now rematch against
9494 * any retained enablings. Note that this call will acquire both
9495 * cpu_lock and dtrace_lock; the fact that we are holding
9496 * dtrace_meta_lock now is what defines the ordering with respect to
9497 * these three locks.
9499 dtrace_enabling_matchall();
9503 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
9505 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9506 dof_hdr_t *dof = (dof_hdr_t *)daddr;
9508 dof_provider_t *provider;
9510 dtrace_helper_provdesc_t dhpv;
9511 dtrace_meta_t *meta = dtrace_meta_pid;
9512 dtrace_mops_t *mops = &meta->dtm_mops;
9514 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
9515 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9516 provider->dofpv_strtab * dof->dofh_secsize);
9518 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
9521 * Create the provider.
9523 dtrace_dofprov2hprov(&dhpv, provider, strtab);
9525 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid);
9531 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid)
9533 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9534 dof_hdr_t *dof = (dof_hdr_t *)daddr;
9537 ASSERT(MUTEX_HELD(&dtrace_meta_lock));
9539 for (i = 0; i < dof->dofh_secnum; i++) {
9540 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
9541 dof->dofh_secoff + i * dof->dofh_secsize);
9543 if (sec->dofs_type != DOF_SECT_PROVIDER)
9546 dtrace_helper_provider_remove_one(dhp, sec, pid);
9551 * DTrace Meta Provider-to-Framework API Functions
9553 * These functions implement the Meta Provider-to-Framework API, as described
9554 * in <sys/dtrace.h>.
9557 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg,
9558 dtrace_meta_provider_id_t *idp)
9560 dtrace_meta_t *meta;
9561 dtrace_helpers_t *help, *next;
9564 *idp = DTRACE_METAPROVNONE;
9567 * We strictly don't need the name, but we hold onto it for
9568 * debuggability. All hail error queues!
9571 cmn_err(CE_WARN, "failed to register meta-provider: "
9577 mops->dtms_create_probe == NULL ||
9578 mops->dtms_provide_pid == NULL ||
9579 mops->dtms_remove_pid == NULL) {
9580 cmn_err(CE_WARN, "failed to register meta-register %s: "
9581 "invalid ops", name);
9585 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP);
9586 meta->dtm_mops = *mops;
9587 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
9588 (void) strcpy(meta->dtm_name, name);
9589 meta->dtm_arg = arg;
9591 mutex_enter(&dtrace_meta_lock);
9592 mutex_enter(&dtrace_lock);
9594 if (dtrace_meta_pid != NULL) {
9595 mutex_exit(&dtrace_lock);
9596 mutex_exit(&dtrace_meta_lock);
9597 cmn_err(CE_WARN, "failed to register meta-register %s: "
9598 "user-land meta-provider exists", name);
9599 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1);
9600 kmem_free(meta, sizeof (dtrace_meta_t));
9604 dtrace_meta_pid = meta;
9605 *idp = (dtrace_meta_provider_id_t)meta;
9608 * If there are providers and probes ready to go, pass them
9609 * off to the new meta provider now.
9612 help = dtrace_deferred_pid;
9613 dtrace_deferred_pid = NULL;
9615 mutex_exit(&dtrace_lock);
9617 while (help != NULL) {
9618 for (i = 0; i < help->dthps_nprovs; i++) {
9619 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
9623 next = help->dthps_next;
9624 help->dthps_next = NULL;
9625 help->dthps_prev = NULL;
9626 help->dthps_deferred = 0;
9630 mutex_exit(&dtrace_meta_lock);
9636 dtrace_meta_unregister(dtrace_meta_provider_id_t id)
9638 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id;
9640 mutex_enter(&dtrace_meta_lock);
9641 mutex_enter(&dtrace_lock);
9643 if (old == dtrace_meta_pid) {
9644 pp = &dtrace_meta_pid;
9646 panic("attempt to unregister non-existent "
9647 "dtrace meta-provider %p\n", (void *)old);
9650 if (old->dtm_count != 0) {
9651 mutex_exit(&dtrace_lock);
9652 mutex_exit(&dtrace_meta_lock);
9658 mutex_exit(&dtrace_lock);
9659 mutex_exit(&dtrace_meta_lock);
9661 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1);
9662 kmem_free(old, sizeof (dtrace_meta_t));
9669 * DTrace DIF Object Functions
9672 dtrace_difo_err(uint_t pc, const char *format, ...)
9674 if (dtrace_err_verbose) {
9677 (void) uprintf("dtrace DIF object error: [%u]: ", pc);
9678 va_start(alist, format);
9679 (void) vuprintf(format, alist);
9683 #ifdef DTRACE_ERRDEBUG
9684 dtrace_errdebug(format);
9690 * Validate a DTrace DIF object by checking the IR instructions. The following
9691 * rules are currently enforced by dtrace_difo_validate():
9693 * 1. Each instruction must have a valid opcode
9694 * 2. Each register, string, variable, or subroutine reference must be valid
9695 * 3. No instruction can modify register %r0 (must be zero)
9696 * 4. All instruction reserved bits must be set to zero
9697 * 5. The last instruction must be a "ret" instruction
9698 * 6. All branch targets must reference a valid instruction _after_ the branch
9701 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs,
9705 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
9708 int maxglobal = -1, maxlocal = -1, maxtlocal = -1;
9710 kcheckload = cr == NULL ||
9711 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0;
9713 dp->dtdo_destructive = 0;
9715 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
9716 dif_instr_t instr = dp->dtdo_buf[pc];
9718 uint_t r1 = DIF_INSTR_R1(instr);
9719 uint_t r2 = DIF_INSTR_R2(instr);
9720 uint_t rd = DIF_INSTR_RD(instr);
9721 uint_t rs = DIF_INSTR_RS(instr);
9722 uint_t label = DIF_INSTR_LABEL(instr);
9723 uint_t v = DIF_INSTR_VAR(instr);
9724 uint_t subr = DIF_INSTR_SUBR(instr);
9725 uint_t type = DIF_INSTR_TYPE(instr);
9726 uint_t op = DIF_INSTR_OP(instr);
9744 err += efunc(pc, "invalid register %u\n", r1);
9746 err += efunc(pc, "invalid register %u\n", r2);
9748 err += efunc(pc, "invalid register %u\n", rd);
9750 err += efunc(pc, "cannot write to %r0\n");
9756 err += efunc(pc, "invalid register %u\n", r1);
9758 err += efunc(pc, "non-zero reserved bits\n");
9760 err += efunc(pc, "invalid register %u\n", rd);
9762 err += efunc(pc, "cannot write to %r0\n");
9772 err += efunc(pc, "invalid register %u\n", r1);
9774 err += efunc(pc, "non-zero reserved bits\n");
9776 err += efunc(pc, "invalid register %u\n", rd);
9778 err += efunc(pc, "cannot write to %r0\n");
9780 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op +
9781 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd);
9791 err += efunc(pc, "invalid register %u\n", r1);
9793 err += efunc(pc, "non-zero reserved bits\n");
9795 err += efunc(pc, "invalid register %u\n", rd);
9797 err += efunc(pc, "cannot write to %r0\n");
9807 err += efunc(pc, "invalid register %u\n", r1);
9809 err += efunc(pc, "non-zero reserved bits\n");
9811 err += efunc(pc, "invalid register %u\n", rd);
9813 err += efunc(pc, "cannot write to %r0\n");
9820 err += efunc(pc, "invalid register %u\n", r1);
9822 err += efunc(pc, "non-zero reserved bits\n");
9824 err += efunc(pc, "invalid register %u\n", rd);
9826 err += efunc(pc, "cannot write to 0 address\n");
9831 err += efunc(pc, "invalid register %u\n", r1);
9833 err += efunc(pc, "invalid register %u\n", r2);
9835 err += efunc(pc, "non-zero reserved bits\n");
9839 err += efunc(pc, "invalid register %u\n", r1);
9840 if (r2 != 0 || rd != 0)
9841 err += efunc(pc, "non-zero reserved bits\n");
9854 if (label >= dp->dtdo_len) {
9855 err += efunc(pc, "invalid branch target %u\n",
9859 err += efunc(pc, "backward branch to %u\n",
9864 if (r1 != 0 || r2 != 0)
9865 err += efunc(pc, "non-zero reserved bits\n");
9867 err += efunc(pc, "invalid register %u\n", rd);
9871 case DIF_OP_FLUSHTS:
9872 if (r1 != 0 || r2 != 0 || rd != 0)
9873 err += efunc(pc, "non-zero reserved bits\n");
9876 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) {
9877 err += efunc(pc, "invalid integer ref %u\n",
9878 DIF_INSTR_INTEGER(instr));
9881 err += efunc(pc, "invalid register %u\n", rd);
9883 err += efunc(pc, "cannot write to %r0\n");
9886 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) {
9887 err += efunc(pc, "invalid string ref %u\n",
9888 DIF_INSTR_STRING(instr));
9891 err += efunc(pc, "invalid register %u\n", rd);
9893 err += efunc(pc, "cannot write to %r0\n");
9897 if (r1 > DIF_VAR_ARRAY_MAX)
9898 err += efunc(pc, "invalid array %u\n", r1);
9900 err += efunc(pc, "invalid register %u\n", r2);
9902 err += efunc(pc, "invalid register %u\n", rd);
9904 err += efunc(pc, "cannot write to %r0\n");
9911 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX)
9912 err += efunc(pc, "invalid variable %u\n", v);
9914 err += efunc(pc, "invalid register %u\n", rd);
9916 err += efunc(pc, "cannot write to %r0\n");
9923 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX)
9924 err += efunc(pc, "invalid variable %u\n", v);
9926 err += efunc(pc, "invalid register %u\n", rd);
9929 if (subr > DIF_SUBR_MAX)
9930 err += efunc(pc, "invalid subr %u\n", subr);
9932 err += efunc(pc, "invalid register %u\n", rd);
9934 err += efunc(pc, "cannot write to %r0\n");
9936 if (subr == DIF_SUBR_COPYOUT ||
9937 subr == DIF_SUBR_COPYOUTSTR) {
9938 dp->dtdo_destructive = 1;
9941 if (subr == DIF_SUBR_GETF) {
9943 * If we have a getf() we need to record that
9944 * in our state. Note that our state can be
9945 * NULL if this is a helper -- but in that
9946 * case, the call to getf() is itself illegal,
9947 * and will be caught (slightly later) when
9948 * the helper is validated.
9950 if (vstate->dtvs_state != NULL)
9951 vstate->dtvs_state->dts_getf++;
9956 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF)
9957 err += efunc(pc, "invalid ref type %u\n", type);
9959 err += efunc(pc, "invalid register %u\n", r2);
9961 err += efunc(pc, "invalid register %u\n", rs);
9964 if (type != DIF_TYPE_CTF)
9965 err += efunc(pc, "invalid val type %u\n", type);
9967 err += efunc(pc, "invalid register %u\n", r2);
9969 err += efunc(pc, "invalid register %u\n", rs);
9972 err += efunc(pc, "invalid opcode %u\n",
9973 DIF_INSTR_OP(instr));
9977 if (dp->dtdo_len != 0 &&
9978 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) {
9979 err += efunc(dp->dtdo_len - 1,
9980 "expected 'ret' as last DIF instruction\n");
9983 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) {
9985 * If we're not returning by reference, the size must be either
9986 * 0 or the size of one of the base types.
9988 switch (dp->dtdo_rtype.dtdt_size) {
9990 case sizeof (uint8_t):
9991 case sizeof (uint16_t):
9992 case sizeof (uint32_t):
9993 case sizeof (uint64_t):
9997 err += efunc(dp->dtdo_len - 1, "bad return size\n");
10001 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) {
10002 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL;
10003 dtrace_diftype_t *vt, *et;
10006 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL &&
10007 v->dtdv_scope != DIFV_SCOPE_THREAD &&
10008 v->dtdv_scope != DIFV_SCOPE_LOCAL) {
10009 err += efunc(i, "unrecognized variable scope %d\n",
10014 if (v->dtdv_kind != DIFV_KIND_ARRAY &&
10015 v->dtdv_kind != DIFV_KIND_SCALAR) {
10016 err += efunc(i, "unrecognized variable type %d\n",
10021 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) {
10022 err += efunc(i, "%d exceeds variable id limit\n", id);
10026 if (id < DIF_VAR_OTHER_UBASE)
10030 * For user-defined variables, we need to check that this
10031 * definition is identical to any previous definition that we
10034 ndx = id - DIF_VAR_OTHER_UBASE;
10036 switch (v->dtdv_scope) {
10037 case DIFV_SCOPE_GLOBAL:
10038 if (maxglobal == -1 || ndx > maxglobal)
10041 if (ndx < vstate->dtvs_nglobals) {
10042 dtrace_statvar_t *svar;
10044 if ((svar = vstate->dtvs_globals[ndx]) != NULL)
10045 existing = &svar->dtsv_var;
10050 case DIFV_SCOPE_THREAD:
10051 if (maxtlocal == -1 || ndx > maxtlocal)
10054 if (ndx < vstate->dtvs_ntlocals)
10055 existing = &vstate->dtvs_tlocals[ndx];
10058 case DIFV_SCOPE_LOCAL:
10059 if (maxlocal == -1 || ndx > maxlocal)
10062 if (ndx < vstate->dtvs_nlocals) {
10063 dtrace_statvar_t *svar;
10065 if ((svar = vstate->dtvs_locals[ndx]) != NULL)
10066 existing = &svar->dtsv_var;
10072 vt = &v->dtdv_type;
10074 if (vt->dtdt_flags & DIF_TF_BYREF) {
10075 if (vt->dtdt_size == 0) {
10076 err += efunc(i, "zero-sized variable\n");
10080 if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL ||
10081 v->dtdv_scope == DIFV_SCOPE_LOCAL) &&
10082 vt->dtdt_size > dtrace_statvar_maxsize) {
10083 err += efunc(i, "oversized by-ref static\n");
10088 if (existing == NULL || existing->dtdv_id == 0)
10091 ASSERT(existing->dtdv_id == v->dtdv_id);
10092 ASSERT(existing->dtdv_scope == v->dtdv_scope);
10094 if (existing->dtdv_kind != v->dtdv_kind)
10095 err += efunc(i, "%d changed variable kind\n", id);
10097 et = &existing->dtdv_type;
10099 if (vt->dtdt_flags != et->dtdt_flags) {
10100 err += efunc(i, "%d changed variable type flags\n", id);
10104 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) {
10105 err += efunc(i, "%d changed variable type size\n", id);
10110 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
10111 dif_instr_t instr = dp->dtdo_buf[pc];
10113 uint_t v = DIF_INSTR_VAR(instr);
10114 uint_t op = DIF_INSTR_OP(instr);
10121 if (v > DIF_VAR_OTHER_UBASE + maxglobal)
10122 err += efunc(pc, "invalid variable %u\n", v);
10128 if (v > DIF_VAR_OTHER_UBASE + maxtlocal)
10129 err += efunc(pc, "invalid variable %u\n", v);
10133 if (v > DIF_VAR_OTHER_UBASE + maxlocal)
10134 err += efunc(pc, "invalid variable %u\n", v);
10145 * Validate a DTrace DIF object that it is to be used as a helper. Helpers
10146 * are much more constrained than normal DIFOs. Specifically, they may
10149 * 1. Make calls to subroutines other than copyin(), copyinstr() or
10150 * miscellaneous string routines
10151 * 2. Access DTrace variables other than the args[] array, and the
10152 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
10153 * 3. Have thread-local variables.
10154 * 4. Have dynamic variables.
10157 dtrace_difo_validate_helper(dtrace_difo_t *dp)
10159 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
10163 for (pc = 0; pc < dp->dtdo_len; pc++) {
10164 dif_instr_t instr = dp->dtdo_buf[pc];
10166 uint_t v = DIF_INSTR_VAR(instr);
10167 uint_t subr = DIF_INSTR_SUBR(instr);
10168 uint_t op = DIF_INSTR_OP(instr);
10205 case DIF_OP_ALLOCS:
10223 case DIF_OP_FLUSHTS:
10230 case DIF_OP_PUSHTR:
10231 case DIF_OP_PUSHTV:
10235 if (v >= DIF_VAR_OTHER_UBASE)
10238 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9)
10241 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID ||
10242 v == DIF_VAR_PPID || v == DIF_VAR_TID ||
10243 v == DIF_VAR_EXECARGS ||
10244 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME ||
10245 v == DIF_VAR_UID || v == DIF_VAR_GID)
10248 err += efunc(pc, "illegal variable %u\n", v);
10255 err += efunc(pc, "illegal dynamic variable load\n");
10261 err += efunc(pc, "illegal dynamic variable store\n");
10265 if (subr == DIF_SUBR_ALLOCA ||
10266 subr == DIF_SUBR_BCOPY ||
10267 subr == DIF_SUBR_COPYIN ||
10268 subr == DIF_SUBR_COPYINTO ||
10269 subr == DIF_SUBR_COPYINSTR ||
10270 subr == DIF_SUBR_INDEX ||
10271 subr == DIF_SUBR_INET_NTOA ||
10272 subr == DIF_SUBR_INET_NTOA6 ||
10273 subr == DIF_SUBR_INET_NTOP ||
10274 subr == DIF_SUBR_JSON ||
10275 subr == DIF_SUBR_LLTOSTR ||
10276 subr == DIF_SUBR_STRTOLL ||
10277 subr == DIF_SUBR_RINDEX ||
10278 subr == DIF_SUBR_STRCHR ||
10279 subr == DIF_SUBR_STRJOIN ||
10280 subr == DIF_SUBR_STRRCHR ||
10281 subr == DIF_SUBR_STRSTR ||
10282 subr == DIF_SUBR_HTONS ||
10283 subr == DIF_SUBR_HTONL ||
10284 subr == DIF_SUBR_HTONLL ||
10285 subr == DIF_SUBR_NTOHS ||
10286 subr == DIF_SUBR_NTOHL ||
10287 subr == DIF_SUBR_NTOHLL ||
10288 subr == DIF_SUBR_MEMREF)
10291 if (subr == DIF_SUBR_MEMSTR)
10295 err += efunc(pc, "invalid subr %u\n", subr);
10299 err += efunc(pc, "invalid opcode %u\n",
10300 DIF_INSTR_OP(instr));
10308 * Returns 1 if the expression in the DIF object can be cached on a per-thread
10312 dtrace_difo_cacheable(dtrace_difo_t *dp)
10319 for (i = 0; i < dp->dtdo_varlen; i++) {
10320 dtrace_difv_t *v = &dp->dtdo_vartab[i];
10322 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL)
10325 switch (v->dtdv_id) {
10326 case DIF_VAR_CURTHREAD:
10329 case DIF_VAR_EXECARGS:
10330 case DIF_VAR_EXECNAME:
10331 case DIF_VAR_ZONENAME:
10340 * This DIF object may be cacheable. Now we need to look for any
10341 * array loading instructions, any memory loading instructions, or
10342 * any stores to thread-local variables.
10344 for (i = 0; i < dp->dtdo_len; i++) {
10345 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]);
10347 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) ||
10348 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) ||
10349 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) ||
10350 op == DIF_OP_LDGA || op == DIF_OP_STTS)
10358 dtrace_difo_hold(dtrace_difo_t *dp)
10362 ASSERT(MUTEX_HELD(&dtrace_lock));
10365 ASSERT(dp->dtdo_refcnt != 0);
10368 * We need to check this DIF object for references to the variable
10369 * DIF_VAR_VTIMESTAMP.
10371 for (i = 0; i < dp->dtdo_varlen; i++) {
10372 dtrace_difv_t *v = &dp->dtdo_vartab[i];
10374 if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
10377 if (dtrace_vtime_references++ == 0)
10378 dtrace_vtime_enable();
10383 * This routine calculates the dynamic variable chunksize for a given DIF
10384 * object. The calculation is not fool-proof, and can probably be tricked by
10385 * malicious DIF -- but it works for all compiler-generated DIF. Because this
10386 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
10387 * if a dynamic variable size exceeds the chunksize.
10390 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10393 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
10394 const dif_instr_t *text = dp->dtdo_buf;
10395 uint_t pc, srd = 0;
10397 size_t size, ksize;
10400 for (pc = 0; pc < dp->dtdo_len; pc++) {
10401 dif_instr_t instr = text[pc];
10402 uint_t op = DIF_INSTR_OP(instr);
10403 uint_t rd = DIF_INSTR_RD(instr);
10404 uint_t r1 = DIF_INSTR_R1(instr);
10408 dtrace_key_t *key = tupregs;
10412 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)];
10417 key = &tupregs[DIF_DTR_NREGS];
10418 key[0].dttk_size = 0;
10419 key[1].dttk_size = 0;
10421 scope = DIFV_SCOPE_THREAD;
10428 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA)
10429 key[nkeys++].dttk_size = 0;
10431 key[nkeys++].dttk_size = 0;
10433 if (op == DIF_OP_STTAA) {
10434 scope = DIFV_SCOPE_THREAD;
10436 scope = DIFV_SCOPE_GLOBAL;
10441 case DIF_OP_PUSHTR:
10442 if (ttop == DIF_DTR_NREGS)
10445 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) {
10447 * If the register for the size of the "pushtr"
10448 * is %r0 (or the value is 0) and the type is
10449 * a string, we'll use the system-wide default
10452 tupregs[ttop++].dttk_size =
10453 dtrace_strsize_default;
10458 if (sval > LONG_MAX)
10461 tupregs[ttop++].dttk_size = sval;
10466 case DIF_OP_PUSHTV:
10467 if (ttop == DIF_DTR_NREGS)
10470 tupregs[ttop++].dttk_size = 0;
10473 case DIF_OP_FLUSHTS:
10490 * We have a dynamic variable allocation; calculate its size.
10492 for (ksize = 0, i = 0; i < nkeys; i++)
10493 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
10495 size = sizeof (dtrace_dynvar_t);
10496 size += sizeof (dtrace_key_t) * (nkeys - 1);
10500 * Now we need to determine the size of the stored data.
10502 id = DIF_INSTR_VAR(instr);
10504 for (i = 0; i < dp->dtdo_varlen; i++) {
10505 dtrace_difv_t *v = &dp->dtdo_vartab[i];
10507 if (v->dtdv_id == id && v->dtdv_scope == scope) {
10508 size += v->dtdv_type.dtdt_size;
10513 if (i == dp->dtdo_varlen)
10517 * We have the size. If this is larger than the chunk size
10518 * for our dynamic variable state, reset the chunk size.
10520 size = P2ROUNDUP(size, sizeof (uint64_t));
10523 * Before setting the chunk size, check that we're not going
10524 * to set it to a negative value...
10526 if (size > LONG_MAX)
10530 * ...and make certain that we didn't badly overflow.
10532 if (size < ksize || size < sizeof (dtrace_dynvar_t))
10535 if (size > vstate->dtvs_dynvars.dtds_chunksize)
10536 vstate->dtvs_dynvars.dtds_chunksize = size;
10541 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10543 int i, oldsvars, osz, nsz, otlocals, ntlocals;
10546 ASSERT(MUTEX_HELD(&dtrace_lock));
10547 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0);
10549 for (i = 0; i < dp->dtdo_varlen; i++) {
10550 dtrace_difv_t *v = &dp->dtdo_vartab[i];
10551 dtrace_statvar_t *svar, ***svarp = NULL;
10553 uint8_t scope = v->dtdv_scope;
10556 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
10559 id -= DIF_VAR_OTHER_UBASE;
10562 case DIFV_SCOPE_THREAD:
10563 while (id >= (otlocals = vstate->dtvs_ntlocals)) {
10564 dtrace_difv_t *tlocals;
10566 if ((ntlocals = (otlocals << 1)) == 0)
10569 osz = otlocals * sizeof (dtrace_difv_t);
10570 nsz = ntlocals * sizeof (dtrace_difv_t);
10572 tlocals = kmem_zalloc(nsz, KM_SLEEP);
10575 bcopy(vstate->dtvs_tlocals,
10577 kmem_free(vstate->dtvs_tlocals, osz);
10580 vstate->dtvs_tlocals = tlocals;
10581 vstate->dtvs_ntlocals = ntlocals;
10584 vstate->dtvs_tlocals[id] = *v;
10587 case DIFV_SCOPE_LOCAL:
10588 np = &vstate->dtvs_nlocals;
10589 svarp = &vstate->dtvs_locals;
10591 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
10592 dsize = NCPU * (v->dtdv_type.dtdt_size +
10593 sizeof (uint64_t));
10595 dsize = NCPU * sizeof (uint64_t);
10599 case DIFV_SCOPE_GLOBAL:
10600 np = &vstate->dtvs_nglobals;
10601 svarp = &vstate->dtvs_globals;
10603 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
10604 dsize = v->dtdv_type.dtdt_size +
10613 while (id >= (oldsvars = *np)) {
10614 dtrace_statvar_t **statics;
10615 int newsvars, oldsize, newsize;
10617 if ((newsvars = (oldsvars << 1)) == 0)
10620 oldsize = oldsvars * sizeof (dtrace_statvar_t *);
10621 newsize = newsvars * sizeof (dtrace_statvar_t *);
10623 statics = kmem_zalloc(newsize, KM_SLEEP);
10625 if (oldsize != 0) {
10626 bcopy(*svarp, statics, oldsize);
10627 kmem_free(*svarp, oldsize);
10634 if ((svar = (*svarp)[id]) == NULL) {
10635 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP);
10636 svar->dtsv_var = *v;
10638 if ((svar->dtsv_size = dsize) != 0) {
10639 svar->dtsv_data = (uint64_t)(uintptr_t)
10640 kmem_zalloc(dsize, KM_SLEEP);
10643 (*svarp)[id] = svar;
10646 svar->dtsv_refcnt++;
10649 dtrace_difo_chunksize(dp, vstate);
10650 dtrace_difo_hold(dp);
10653 static dtrace_difo_t *
10654 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10656 dtrace_difo_t *new;
10659 ASSERT(dp->dtdo_buf != NULL);
10660 ASSERT(dp->dtdo_refcnt != 0);
10662 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
10664 ASSERT(dp->dtdo_buf != NULL);
10665 sz = dp->dtdo_len * sizeof (dif_instr_t);
10666 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP);
10667 bcopy(dp->dtdo_buf, new->dtdo_buf, sz);
10668 new->dtdo_len = dp->dtdo_len;
10670 if (dp->dtdo_strtab != NULL) {
10671 ASSERT(dp->dtdo_strlen != 0);
10672 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP);
10673 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen);
10674 new->dtdo_strlen = dp->dtdo_strlen;
10677 if (dp->dtdo_inttab != NULL) {
10678 ASSERT(dp->dtdo_intlen != 0);
10679 sz = dp->dtdo_intlen * sizeof (uint64_t);
10680 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP);
10681 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz);
10682 new->dtdo_intlen = dp->dtdo_intlen;
10685 if (dp->dtdo_vartab != NULL) {
10686 ASSERT(dp->dtdo_varlen != 0);
10687 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t);
10688 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP);
10689 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz);
10690 new->dtdo_varlen = dp->dtdo_varlen;
10693 dtrace_difo_init(new, vstate);
10698 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10702 ASSERT(dp->dtdo_refcnt == 0);
10704 for (i = 0; i < dp->dtdo_varlen; i++) {
10705 dtrace_difv_t *v = &dp->dtdo_vartab[i];
10706 dtrace_statvar_t *svar, **svarp = NULL;
10708 uint8_t scope = v->dtdv_scope;
10712 case DIFV_SCOPE_THREAD:
10715 case DIFV_SCOPE_LOCAL:
10716 np = &vstate->dtvs_nlocals;
10717 svarp = vstate->dtvs_locals;
10720 case DIFV_SCOPE_GLOBAL:
10721 np = &vstate->dtvs_nglobals;
10722 svarp = vstate->dtvs_globals;
10729 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
10732 id -= DIF_VAR_OTHER_UBASE;
10736 ASSERT(svar != NULL);
10737 ASSERT(svar->dtsv_refcnt > 0);
10739 if (--svar->dtsv_refcnt > 0)
10742 if (svar->dtsv_size != 0) {
10743 ASSERT(svar->dtsv_data != 0);
10744 kmem_free((void *)(uintptr_t)svar->dtsv_data,
10748 kmem_free(svar, sizeof (dtrace_statvar_t));
10752 if (dp->dtdo_buf != NULL)
10753 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
10754 if (dp->dtdo_inttab != NULL)
10755 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
10756 if (dp->dtdo_strtab != NULL)
10757 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
10758 if (dp->dtdo_vartab != NULL)
10759 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
10761 kmem_free(dp, sizeof (dtrace_difo_t));
10765 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10769 ASSERT(MUTEX_HELD(&dtrace_lock));
10770 ASSERT(dp->dtdo_refcnt != 0);
10772 for (i = 0; i < dp->dtdo_varlen; i++) {
10773 dtrace_difv_t *v = &dp->dtdo_vartab[i];
10775 if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
10778 ASSERT(dtrace_vtime_references > 0);
10779 if (--dtrace_vtime_references == 0)
10780 dtrace_vtime_disable();
10783 if (--dp->dtdo_refcnt == 0)
10784 dtrace_difo_destroy(dp, vstate);
10788 * DTrace Format Functions
10791 dtrace_format_add(dtrace_state_t *state, char *str)
10794 uint16_t ndx, len = strlen(str) + 1;
10796 fmt = kmem_zalloc(len, KM_SLEEP);
10797 bcopy(str, fmt, len);
10799 for (ndx = 0; ndx < state->dts_nformats; ndx++) {
10800 if (state->dts_formats[ndx] == NULL) {
10801 state->dts_formats[ndx] = fmt;
10806 if (state->dts_nformats == USHRT_MAX) {
10808 * This is only likely if a denial-of-service attack is being
10809 * attempted. As such, it's okay to fail silently here.
10811 kmem_free(fmt, len);
10816 * For simplicity, we always resize the formats array to be exactly the
10817 * number of formats.
10819 ndx = state->dts_nformats++;
10820 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP);
10822 if (state->dts_formats != NULL) {
10824 bcopy(state->dts_formats, new, ndx * sizeof (char *));
10825 kmem_free(state->dts_formats, ndx * sizeof (char *));
10828 state->dts_formats = new;
10829 state->dts_formats[ndx] = fmt;
10835 dtrace_format_remove(dtrace_state_t *state, uint16_t format)
10839 ASSERT(state->dts_formats != NULL);
10840 ASSERT(format <= state->dts_nformats);
10841 ASSERT(state->dts_formats[format - 1] != NULL);
10843 fmt = state->dts_formats[format - 1];
10844 kmem_free(fmt, strlen(fmt) + 1);
10845 state->dts_formats[format - 1] = NULL;
10849 dtrace_format_destroy(dtrace_state_t *state)
10853 if (state->dts_nformats == 0) {
10854 ASSERT(state->dts_formats == NULL);
10858 ASSERT(state->dts_formats != NULL);
10860 for (i = 0; i < state->dts_nformats; i++) {
10861 char *fmt = state->dts_formats[i];
10866 kmem_free(fmt, strlen(fmt) + 1);
10869 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *));
10870 state->dts_nformats = 0;
10871 state->dts_formats = NULL;
10875 * DTrace Predicate Functions
10877 static dtrace_predicate_t *
10878 dtrace_predicate_create(dtrace_difo_t *dp)
10880 dtrace_predicate_t *pred;
10882 ASSERT(MUTEX_HELD(&dtrace_lock));
10883 ASSERT(dp->dtdo_refcnt != 0);
10885 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP);
10886 pred->dtp_difo = dp;
10887 pred->dtp_refcnt = 1;
10889 if (!dtrace_difo_cacheable(dp))
10892 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) {
10894 * This is only theoretically possible -- we have had 2^32
10895 * cacheable predicates on this machine. We cannot allow any
10896 * more predicates to become cacheable: as unlikely as it is,
10897 * there may be a thread caching a (now stale) predicate cache
10898 * ID. (N.B.: the temptation is being successfully resisted to
10899 * have this cmn_err() "Holy shit -- we executed this code!")
10904 pred->dtp_cacheid = dtrace_predcache_id++;
10910 dtrace_predicate_hold(dtrace_predicate_t *pred)
10912 ASSERT(MUTEX_HELD(&dtrace_lock));
10913 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0);
10914 ASSERT(pred->dtp_refcnt > 0);
10916 pred->dtp_refcnt++;
10920 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate)
10922 dtrace_difo_t *dp = pred->dtp_difo;
10924 ASSERT(MUTEX_HELD(&dtrace_lock));
10925 ASSERT(dp != NULL && dp->dtdo_refcnt != 0);
10926 ASSERT(pred->dtp_refcnt > 0);
10928 if (--pred->dtp_refcnt == 0) {
10929 dtrace_difo_release(pred->dtp_difo, vstate);
10930 kmem_free(pred, sizeof (dtrace_predicate_t));
10935 * DTrace Action Description Functions
10937 static dtrace_actdesc_t *
10938 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple,
10939 uint64_t uarg, uint64_t arg)
10941 dtrace_actdesc_t *act;
10944 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL &&
10945 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA));
10948 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP);
10949 act->dtad_kind = kind;
10950 act->dtad_ntuple = ntuple;
10951 act->dtad_uarg = uarg;
10952 act->dtad_arg = arg;
10953 act->dtad_refcnt = 1;
10959 dtrace_actdesc_hold(dtrace_actdesc_t *act)
10961 ASSERT(act->dtad_refcnt >= 1);
10962 act->dtad_refcnt++;
10966 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate)
10968 dtrace_actkind_t kind = act->dtad_kind;
10971 ASSERT(act->dtad_refcnt >= 1);
10973 if (--act->dtad_refcnt != 0)
10976 if ((dp = act->dtad_difo) != NULL)
10977 dtrace_difo_release(dp, vstate);
10979 if (DTRACEACT_ISPRINTFLIKE(kind)) {
10980 char *str = (char *)(uintptr_t)act->dtad_arg;
10983 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) ||
10984 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA));
10988 kmem_free(str, strlen(str) + 1);
10991 kmem_free(act, sizeof (dtrace_actdesc_t));
10995 * DTrace ECB Functions
10997 static dtrace_ecb_t *
10998 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe)
11001 dtrace_epid_t epid;
11003 ASSERT(MUTEX_HELD(&dtrace_lock));
11005 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP);
11006 ecb->dte_predicate = NULL;
11007 ecb->dte_probe = probe;
11010 * The default size is the size of the default action: recording
11013 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t);
11014 ecb->dte_alignment = sizeof (dtrace_epid_t);
11016 epid = state->dts_epid++;
11018 if (epid - 1 >= state->dts_necbs) {
11019 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs;
11020 int necbs = state->dts_necbs << 1;
11022 ASSERT(epid == state->dts_necbs + 1);
11025 ASSERT(oecbs == NULL);
11029 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP);
11032 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs));
11034 dtrace_membar_producer();
11035 state->dts_ecbs = ecbs;
11037 if (oecbs != NULL) {
11039 * If this state is active, we must dtrace_sync()
11040 * before we can free the old dts_ecbs array: we're
11041 * coming in hot, and there may be active ring
11042 * buffer processing (which indexes into the dts_ecbs
11043 * array) on another CPU.
11045 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
11048 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs));
11051 dtrace_membar_producer();
11052 state->dts_necbs = necbs;
11055 ecb->dte_state = state;
11057 ASSERT(state->dts_ecbs[epid - 1] == NULL);
11058 dtrace_membar_producer();
11059 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb;
11065 dtrace_ecb_enable(dtrace_ecb_t *ecb)
11067 dtrace_probe_t *probe = ecb->dte_probe;
11069 ASSERT(MUTEX_HELD(&cpu_lock));
11070 ASSERT(MUTEX_HELD(&dtrace_lock));
11071 ASSERT(ecb->dte_next == NULL);
11073 if (probe == NULL) {
11075 * This is the NULL probe -- there's nothing to do.
11080 if (probe->dtpr_ecb == NULL) {
11081 dtrace_provider_t *prov = probe->dtpr_provider;
11084 * We're the first ECB on this probe.
11086 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb;
11088 if (ecb->dte_predicate != NULL)
11089 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid;
11091 prov->dtpv_pops.dtps_enable(prov->dtpv_arg,
11092 probe->dtpr_id, probe->dtpr_arg);
11095 * This probe is already active. Swing the last pointer to
11096 * point to the new ECB, and issue a dtrace_sync() to assure
11097 * that all CPUs have seen the change.
11099 ASSERT(probe->dtpr_ecb_last != NULL);
11100 probe->dtpr_ecb_last->dte_next = ecb;
11101 probe->dtpr_ecb_last = ecb;
11102 probe->dtpr_predcache = 0;
11109 dtrace_ecb_resize(dtrace_ecb_t *ecb)
11111 dtrace_action_t *act;
11112 uint32_t curneeded = UINT32_MAX;
11113 uint32_t aggbase = UINT32_MAX;
11116 * If we record anything, we always record the dtrace_rechdr_t. (And
11117 * we always record it first.)
11119 ecb->dte_size = sizeof (dtrace_rechdr_t);
11120 ecb->dte_alignment = sizeof (dtrace_epid_t);
11122 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
11123 dtrace_recdesc_t *rec = &act->dta_rec;
11124 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1);
11126 ecb->dte_alignment = MAX(ecb->dte_alignment,
11127 rec->dtrd_alignment);
11129 if (DTRACEACT_ISAGG(act->dta_kind)) {
11130 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
11132 ASSERT(rec->dtrd_size != 0);
11133 ASSERT(agg->dtag_first != NULL);
11134 ASSERT(act->dta_prev->dta_intuple);
11135 ASSERT(aggbase != UINT32_MAX);
11136 ASSERT(curneeded != UINT32_MAX);
11138 agg->dtag_base = aggbase;
11140 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
11141 rec->dtrd_offset = curneeded;
11142 if (curneeded + rec->dtrd_size < curneeded)
11144 curneeded += rec->dtrd_size;
11145 ecb->dte_needed = MAX(ecb->dte_needed, curneeded);
11147 aggbase = UINT32_MAX;
11148 curneeded = UINT32_MAX;
11149 } else if (act->dta_intuple) {
11150 if (curneeded == UINT32_MAX) {
11152 * This is the first record in a tuple. Align
11153 * curneeded to be at offset 4 in an 8-byte
11156 ASSERT(act->dta_prev == NULL ||
11157 !act->dta_prev->dta_intuple);
11158 ASSERT3U(aggbase, ==, UINT32_MAX);
11159 curneeded = P2PHASEUP(ecb->dte_size,
11160 sizeof (uint64_t), sizeof (dtrace_aggid_t));
11162 aggbase = curneeded - sizeof (dtrace_aggid_t);
11163 ASSERT(IS_P2ALIGNED(aggbase,
11164 sizeof (uint64_t)));
11166 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
11167 rec->dtrd_offset = curneeded;
11168 if (curneeded + rec->dtrd_size < curneeded)
11170 curneeded += rec->dtrd_size;
11172 /* tuples must be followed by an aggregation */
11173 ASSERT(act->dta_prev == NULL ||
11174 !act->dta_prev->dta_intuple);
11176 ecb->dte_size = P2ROUNDUP(ecb->dte_size,
11177 rec->dtrd_alignment);
11178 rec->dtrd_offset = ecb->dte_size;
11179 if (ecb->dte_size + rec->dtrd_size < ecb->dte_size)
11181 ecb->dte_size += rec->dtrd_size;
11182 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size);
11186 if ((act = ecb->dte_action) != NULL &&
11187 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) &&
11188 ecb->dte_size == sizeof (dtrace_rechdr_t)) {
11190 * If the size is still sizeof (dtrace_rechdr_t), then all
11191 * actions store no data; set the size to 0.
11196 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t));
11197 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t)));
11198 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed,
11203 static dtrace_action_t *
11204 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
11206 dtrace_aggregation_t *agg;
11207 size_t size = sizeof (uint64_t);
11208 int ntuple = desc->dtad_ntuple;
11209 dtrace_action_t *act;
11210 dtrace_recdesc_t *frec;
11211 dtrace_aggid_t aggid;
11212 dtrace_state_t *state = ecb->dte_state;
11214 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP);
11215 agg->dtag_ecb = ecb;
11217 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind));
11219 switch (desc->dtad_kind) {
11220 case DTRACEAGG_MIN:
11221 agg->dtag_initial = INT64_MAX;
11222 agg->dtag_aggregate = dtrace_aggregate_min;
11225 case DTRACEAGG_MAX:
11226 agg->dtag_initial = INT64_MIN;
11227 agg->dtag_aggregate = dtrace_aggregate_max;
11230 case DTRACEAGG_COUNT:
11231 agg->dtag_aggregate = dtrace_aggregate_count;
11234 case DTRACEAGG_QUANTIZE:
11235 agg->dtag_aggregate = dtrace_aggregate_quantize;
11236 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) *
11240 case DTRACEAGG_LQUANTIZE: {
11241 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg);
11242 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg);
11244 agg->dtag_initial = desc->dtad_arg;
11245 agg->dtag_aggregate = dtrace_aggregate_lquantize;
11247 if (step == 0 || levels == 0)
11250 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t);
11254 case DTRACEAGG_LLQUANTIZE: {
11255 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg);
11256 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg);
11257 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg);
11258 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg);
11261 agg->dtag_initial = desc->dtad_arg;
11262 agg->dtag_aggregate = dtrace_aggregate_llquantize;
11264 if (factor < 2 || low >= high || nsteps < factor)
11268 * Now check that the number of steps evenly divides a power
11269 * of the factor. (This assures both integer bucket size and
11270 * linearity within each magnitude.)
11272 for (v = factor; v < nsteps; v *= factor)
11275 if ((v % nsteps) || (nsteps % factor))
11278 size = (dtrace_aggregate_llquantize_bucket(factor,
11279 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t);
11283 case DTRACEAGG_AVG:
11284 agg->dtag_aggregate = dtrace_aggregate_avg;
11285 size = sizeof (uint64_t) * 2;
11288 case DTRACEAGG_STDDEV:
11289 agg->dtag_aggregate = dtrace_aggregate_stddev;
11290 size = sizeof (uint64_t) * 4;
11293 case DTRACEAGG_SUM:
11294 agg->dtag_aggregate = dtrace_aggregate_sum;
11301 agg->dtag_action.dta_rec.dtrd_size = size;
11307 * We must make sure that we have enough actions for the n-tuple.
11309 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) {
11310 if (DTRACEACT_ISAGG(act->dta_kind))
11313 if (--ntuple == 0) {
11315 * This is the action with which our n-tuple begins.
11317 agg->dtag_first = act;
11323 * This n-tuple is short by ntuple elements. Return failure.
11325 ASSERT(ntuple != 0);
11327 kmem_free(agg, sizeof (dtrace_aggregation_t));
11332 * If the last action in the tuple has a size of zero, it's actually
11333 * an expression argument for the aggregating action.
11335 ASSERT(ecb->dte_action_last != NULL);
11336 act = ecb->dte_action_last;
11338 if (act->dta_kind == DTRACEACT_DIFEXPR) {
11339 ASSERT(act->dta_difo != NULL);
11341 if (act->dta_difo->dtdo_rtype.dtdt_size == 0)
11342 agg->dtag_hasarg = 1;
11346 * We need to allocate an id for this aggregation.
11349 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1,
11350 VM_BESTFIT | VM_SLEEP);
11352 aggid = alloc_unr(state->dts_aggid_arena);
11355 if (aggid - 1 >= state->dts_naggregations) {
11356 dtrace_aggregation_t **oaggs = state->dts_aggregations;
11357 dtrace_aggregation_t **aggs;
11358 int naggs = state->dts_naggregations << 1;
11359 int onaggs = state->dts_naggregations;
11361 ASSERT(aggid == state->dts_naggregations + 1);
11364 ASSERT(oaggs == NULL);
11368 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP);
11370 if (oaggs != NULL) {
11371 bcopy(oaggs, aggs, onaggs * sizeof (*aggs));
11372 kmem_free(oaggs, onaggs * sizeof (*aggs));
11375 state->dts_aggregations = aggs;
11376 state->dts_naggregations = naggs;
11379 ASSERT(state->dts_aggregations[aggid - 1] == NULL);
11380 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg;
11382 frec = &agg->dtag_first->dta_rec;
11383 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t))
11384 frec->dtrd_alignment = sizeof (dtrace_aggid_t);
11386 for (act = agg->dtag_first; act != NULL; act = act->dta_next) {
11387 ASSERT(!act->dta_intuple);
11388 act->dta_intuple = 1;
11391 return (&agg->dtag_action);
11395 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act)
11397 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
11398 dtrace_state_t *state = ecb->dte_state;
11399 dtrace_aggid_t aggid = agg->dtag_id;
11401 ASSERT(DTRACEACT_ISAGG(act->dta_kind));
11403 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1);
11405 free_unr(state->dts_aggid_arena, aggid);
11408 ASSERT(state->dts_aggregations[aggid - 1] == agg);
11409 state->dts_aggregations[aggid - 1] = NULL;
11411 kmem_free(agg, sizeof (dtrace_aggregation_t));
11415 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
11417 dtrace_action_t *action, *last;
11418 dtrace_difo_t *dp = desc->dtad_difo;
11419 uint32_t size = 0, align = sizeof (uint8_t), mask;
11420 uint16_t format = 0;
11421 dtrace_recdesc_t *rec;
11422 dtrace_state_t *state = ecb->dte_state;
11423 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize;
11424 uint64_t arg = desc->dtad_arg;
11426 ASSERT(MUTEX_HELD(&dtrace_lock));
11427 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1);
11429 if (DTRACEACT_ISAGG(desc->dtad_kind)) {
11431 * If this is an aggregating action, there must be neither
11432 * a speculate nor a commit on the action chain.
11434 dtrace_action_t *act;
11436 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
11437 if (act->dta_kind == DTRACEACT_COMMIT)
11440 if (act->dta_kind == DTRACEACT_SPECULATE)
11444 action = dtrace_ecb_aggregation_create(ecb, desc);
11446 if (action == NULL)
11449 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) ||
11450 (desc->dtad_kind == DTRACEACT_DIFEXPR &&
11451 dp != NULL && dp->dtdo_destructive)) {
11452 state->dts_destructive = 1;
11455 switch (desc->dtad_kind) {
11456 case DTRACEACT_PRINTF:
11457 case DTRACEACT_PRINTA:
11458 case DTRACEACT_SYSTEM:
11459 case DTRACEACT_FREOPEN:
11460 case DTRACEACT_DIFEXPR:
11462 * We know that our arg is a string -- turn it into a
11466 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA ||
11467 desc->dtad_kind == DTRACEACT_DIFEXPR);
11472 ASSERT(arg > KERNELBASE);
11474 format = dtrace_format_add(state,
11475 (char *)(uintptr_t)arg);
11479 case DTRACEACT_LIBACT:
11480 case DTRACEACT_TRACEMEM:
11481 case DTRACEACT_TRACEMEM_DYNSIZE:
11485 if ((size = dp->dtdo_rtype.dtdt_size) != 0)
11488 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
11489 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11492 size = opt[DTRACEOPT_STRSIZE];
11497 case DTRACEACT_STACK:
11498 if ((nframes = arg) == 0) {
11499 nframes = opt[DTRACEOPT_STACKFRAMES];
11500 ASSERT(nframes > 0);
11504 size = nframes * sizeof (pc_t);
11507 case DTRACEACT_JSTACK:
11508 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0)
11509 strsize = opt[DTRACEOPT_JSTACKSTRSIZE];
11511 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0)
11512 nframes = opt[DTRACEOPT_JSTACKFRAMES];
11514 arg = DTRACE_USTACK_ARG(nframes, strsize);
11517 case DTRACEACT_USTACK:
11518 if (desc->dtad_kind != DTRACEACT_JSTACK &&
11519 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) {
11520 strsize = DTRACE_USTACK_STRSIZE(arg);
11521 nframes = opt[DTRACEOPT_USTACKFRAMES];
11522 ASSERT(nframes > 0);
11523 arg = DTRACE_USTACK_ARG(nframes, strsize);
11527 * Save a slot for the pid.
11529 size = (nframes + 1) * sizeof (uint64_t);
11530 size += DTRACE_USTACK_STRSIZE(arg);
11531 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t)));
11535 case DTRACEACT_SYM:
11536 case DTRACEACT_MOD:
11537 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) !=
11538 sizeof (uint64_t)) ||
11539 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11543 case DTRACEACT_USYM:
11544 case DTRACEACT_UMOD:
11545 case DTRACEACT_UADDR:
11547 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) ||
11548 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11552 * We have a slot for the pid, plus a slot for the
11553 * argument. To keep things simple (aligned with
11554 * bitness-neutral sizing), we store each as a 64-bit
11557 size = 2 * sizeof (uint64_t);
11560 case DTRACEACT_STOP:
11561 case DTRACEACT_BREAKPOINT:
11562 case DTRACEACT_PANIC:
11565 case DTRACEACT_CHILL:
11566 case DTRACEACT_DISCARD:
11567 case DTRACEACT_RAISE:
11572 case DTRACEACT_EXIT:
11574 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) ||
11575 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11579 case DTRACEACT_SPECULATE:
11580 if (ecb->dte_size > sizeof (dtrace_rechdr_t))
11586 state->dts_speculates = 1;
11589 case DTRACEACT_PRINTM:
11590 size = dp->dtdo_rtype.dtdt_size;
11593 case DTRACEACT_COMMIT: {
11594 dtrace_action_t *act = ecb->dte_action;
11596 for (; act != NULL; act = act->dta_next) {
11597 if (act->dta_kind == DTRACEACT_COMMIT)
11610 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) {
11612 * If this is a data-storing action or a speculate,
11613 * we must be sure that there isn't a commit on the
11616 dtrace_action_t *act = ecb->dte_action;
11618 for (; act != NULL; act = act->dta_next) {
11619 if (act->dta_kind == DTRACEACT_COMMIT)
11624 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP);
11625 action->dta_rec.dtrd_size = size;
11628 action->dta_refcnt = 1;
11629 rec = &action->dta_rec;
11630 size = rec->dtrd_size;
11632 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) {
11633 if (!(size & mask)) {
11639 action->dta_kind = desc->dtad_kind;
11641 if ((action->dta_difo = dp) != NULL)
11642 dtrace_difo_hold(dp);
11644 rec->dtrd_action = action->dta_kind;
11645 rec->dtrd_arg = arg;
11646 rec->dtrd_uarg = desc->dtad_uarg;
11647 rec->dtrd_alignment = (uint16_t)align;
11648 rec->dtrd_format = format;
11650 if ((last = ecb->dte_action_last) != NULL) {
11651 ASSERT(ecb->dte_action != NULL);
11652 action->dta_prev = last;
11653 last->dta_next = action;
11655 ASSERT(ecb->dte_action == NULL);
11656 ecb->dte_action = action;
11659 ecb->dte_action_last = action;
11665 dtrace_ecb_action_remove(dtrace_ecb_t *ecb)
11667 dtrace_action_t *act = ecb->dte_action, *next;
11668 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate;
11672 if (act != NULL && act->dta_refcnt > 1) {
11673 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1);
11676 for (; act != NULL; act = next) {
11677 next = act->dta_next;
11678 ASSERT(next != NULL || act == ecb->dte_action_last);
11679 ASSERT(act->dta_refcnt == 1);
11681 if ((format = act->dta_rec.dtrd_format) != 0)
11682 dtrace_format_remove(ecb->dte_state, format);
11684 if ((dp = act->dta_difo) != NULL)
11685 dtrace_difo_release(dp, vstate);
11687 if (DTRACEACT_ISAGG(act->dta_kind)) {
11688 dtrace_ecb_aggregation_destroy(ecb, act);
11690 kmem_free(act, sizeof (dtrace_action_t));
11695 ecb->dte_action = NULL;
11696 ecb->dte_action_last = NULL;
11701 dtrace_ecb_disable(dtrace_ecb_t *ecb)
11704 * We disable the ECB by removing it from its probe.
11706 dtrace_ecb_t *pecb, *prev = NULL;
11707 dtrace_probe_t *probe = ecb->dte_probe;
11709 ASSERT(MUTEX_HELD(&dtrace_lock));
11711 if (probe == NULL) {
11713 * This is the NULL probe; there is nothing to disable.
11718 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) {
11724 ASSERT(pecb != NULL);
11726 if (prev == NULL) {
11727 probe->dtpr_ecb = ecb->dte_next;
11729 prev->dte_next = ecb->dte_next;
11732 if (ecb == probe->dtpr_ecb_last) {
11733 ASSERT(ecb->dte_next == NULL);
11734 probe->dtpr_ecb_last = prev;
11738 * The ECB has been disconnected from the probe; now sync to assure
11739 * that all CPUs have seen the change before returning.
11743 if (probe->dtpr_ecb == NULL) {
11745 * That was the last ECB on the probe; clear the predicate
11746 * cache ID for the probe, disable it and sync one more time
11747 * to assure that we'll never hit it again.
11749 dtrace_provider_t *prov = probe->dtpr_provider;
11751 ASSERT(ecb->dte_next == NULL);
11752 ASSERT(probe->dtpr_ecb_last == NULL);
11753 probe->dtpr_predcache = DTRACE_CACHEIDNONE;
11754 prov->dtpv_pops.dtps_disable(prov->dtpv_arg,
11755 probe->dtpr_id, probe->dtpr_arg);
11759 * There is at least one ECB remaining on the probe. If there
11760 * is _exactly_ one, set the probe's predicate cache ID to be
11761 * the predicate cache ID of the remaining ECB.
11763 ASSERT(probe->dtpr_ecb_last != NULL);
11764 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE);
11766 if (probe->dtpr_ecb == probe->dtpr_ecb_last) {
11767 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate;
11769 ASSERT(probe->dtpr_ecb->dte_next == NULL);
11772 probe->dtpr_predcache = p->dtp_cacheid;
11775 ecb->dte_next = NULL;
11780 dtrace_ecb_destroy(dtrace_ecb_t *ecb)
11782 dtrace_state_t *state = ecb->dte_state;
11783 dtrace_vstate_t *vstate = &state->dts_vstate;
11784 dtrace_predicate_t *pred;
11785 dtrace_epid_t epid = ecb->dte_epid;
11787 ASSERT(MUTEX_HELD(&dtrace_lock));
11788 ASSERT(ecb->dte_next == NULL);
11789 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb);
11791 if ((pred = ecb->dte_predicate) != NULL)
11792 dtrace_predicate_release(pred, vstate);
11794 dtrace_ecb_action_remove(ecb);
11796 ASSERT(state->dts_ecbs[epid - 1] == ecb);
11797 state->dts_ecbs[epid - 1] = NULL;
11799 kmem_free(ecb, sizeof (dtrace_ecb_t));
11802 static dtrace_ecb_t *
11803 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe,
11804 dtrace_enabling_t *enab)
11807 dtrace_predicate_t *pred;
11808 dtrace_actdesc_t *act;
11809 dtrace_provider_t *prov;
11810 dtrace_ecbdesc_t *desc = enab->dten_current;
11812 ASSERT(MUTEX_HELD(&dtrace_lock));
11813 ASSERT(state != NULL);
11815 ecb = dtrace_ecb_add(state, probe);
11816 ecb->dte_uarg = desc->dted_uarg;
11818 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) {
11819 dtrace_predicate_hold(pred);
11820 ecb->dte_predicate = pred;
11823 if (probe != NULL) {
11825 * If the provider shows more leg than the consumer is old
11826 * enough to see, we need to enable the appropriate implicit
11827 * predicate bits to prevent the ecb from activating at
11830 * Providers specifying DTRACE_PRIV_USER at register time
11831 * are stating that they need the /proc-style privilege
11832 * model to be enforced, and this is what DTRACE_COND_OWNER
11833 * and DTRACE_COND_ZONEOWNER will then do at probe time.
11835 prov = probe->dtpr_provider;
11836 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) &&
11837 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
11838 ecb->dte_cond |= DTRACE_COND_OWNER;
11840 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) &&
11841 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
11842 ecb->dte_cond |= DTRACE_COND_ZONEOWNER;
11845 * If the provider shows us kernel innards and the user
11846 * is lacking sufficient privilege, enable the
11847 * DTRACE_COND_USERMODE implicit predicate.
11849 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) &&
11850 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL))
11851 ecb->dte_cond |= DTRACE_COND_USERMODE;
11854 if (dtrace_ecb_create_cache != NULL) {
11856 * If we have a cached ecb, we'll use its action list instead
11857 * of creating our own (saving both time and space).
11859 dtrace_ecb_t *cached = dtrace_ecb_create_cache;
11860 dtrace_action_t *act = cached->dte_action;
11863 ASSERT(act->dta_refcnt > 0);
11865 ecb->dte_action = act;
11866 ecb->dte_action_last = cached->dte_action_last;
11867 ecb->dte_needed = cached->dte_needed;
11868 ecb->dte_size = cached->dte_size;
11869 ecb->dte_alignment = cached->dte_alignment;
11875 for (act = desc->dted_action; act != NULL; act = act->dtad_next) {
11876 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) {
11877 dtrace_ecb_destroy(ecb);
11882 if ((enab->dten_error = dtrace_ecb_resize(ecb)) != 0) {
11883 dtrace_ecb_destroy(ecb);
11887 return (dtrace_ecb_create_cache = ecb);
11891 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg)
11894 dtrace_enabling_t *enab = arg;
11895 dtrace_state_t *state = enab->dten_vstate->dtvs_state;
11897 ASSERT(state != NULL);
11899 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) {
11901 * This probe was created in a generation for which this
11902 * enabling has previously created ECBs; we don't want to
11903 * enable it again, so just kick out.
11905 return (DTRACE_MATCH_NEXT);
11908 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL)
11909 return (DTRACE_MATCH_DONE);
11911 dtrace_ecb_enable(ecb);
11912 return (DTRACE_MATCH_NEXT);
11915 static dtrace_ecb_t *
11916 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id)
11920 ASSERT(MUTEX_HELD(&dtrace_lock));
11922 if (id == 0 || id > state->dts_necbs)
11925 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL);
11926 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id);
11928 return (state->dts_ecbs[id - 1]);
11931 static dtrace_aggregation_t *
11932 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id)
11934 dtrace_aggregation_t *agg;
11936 ASSERT(MUTEX_HELD(&dtrace_lock));
11938 if (id == 0 || id > state->dts_naggregations)
11941 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL);
11942 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL ||
11943 agg->dtag_id == id);
11945 return (state->dts_aggregations[id - 1]);
11949 * DTrace Buffer Functions
11951 * The following functions manipulate DTrace buffers. Most of these functions
11952 * are called in the context of establishing or processing consumer state;
11953 * exceptions are explicitly noted.
11957 * Note: called from cross call context. This function switches the two
11958 * buffers on a given CPU. The atomicity of this operation is assured by
11959 * disabling interrupts while the actual switch takes place; the disabling of
11960 * interrupts serializes the execution with any execution of dtrace_probe() on
11964 dtrace_buffer_switch(dtrace_buffer_t *buf)
11966 caddr_t tomax = buf->dtb_tomax;
11967 caddr_t xamot = buf->dtb_xamot;
11968 dtrace_icookie_t cookie;
11971 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
11972 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING));
11974 cookie = dtrace_interrupt_disable();
11975 now = dtrace_gethrtime();
11976 buf->dtb_tomax = xamot;
11977 buf->dtb_xamot = tomax;
11978 buf->dtb_xamot_drops = buf->dtb_drops;
11979 buf->dtb_xamot_offset = buf->dtb_offset;
11980 buf->dtb_xamot_errors = buf->dtb_errors;
11981 buf->dtb_xamot_flags = buf->dtb_flags;
11982 buf->dtb_offset = 0;
11983 buf->dtb_drops = 0;
11984 buf->dtb_errors = 0;
11985 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED);
11986 buf->dtb_interval = now - buf->dtb_switched;
11987 buf->dtb_switched = now;
11988 dtrace_interrupt_enable(cookie);
11992 * Note: called from cross call context. This function activates a buffer
11993 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation
11994 * is guaranteed by the disabling of interrupts.
11997 dtrace_buffer_activate(dtrace_state_t *state)
11999 dtrace_buffer_t *buf;
12000 dtrace_icookie_t cookie = dtrace_interrupt_disable();
12002 buf = &state->dts_buffer[curcpu];
12004 if (buf->dtb_tomax != NULL) {
12006 * We might like to assert that the buffer is marked inactive,
12007 * but this isn't necessarily true: the buffer for the CPU
12008 * that processes the BEGIN probe has its buffer activated
12009 * manually. In this case, we take the (harmless) action
12010 * re-clearing the bit INACTIVE bit.
12012 buf->dtb_flags &= ~DTRACEBUF_INACTIVE;
12015 dtrace_interrupt_enable(cookie);
12020 * Activate the specified per-CPU buffer. This is used instead of
12021 * dtrace_buffer_activate() when APs have not yet started, i.e. when
12022 * activating anonymous state.
12025 dtrace_buffer_activate_cpu(dtrace_state_t *state, int cpu)
12028 if (state->dts_buffer[cpu].dtb_tomax != NULL)
12029 state->dts_buffer[cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
12034 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags,
12035 processorid_t cpu, int *factor)
12040 dtrace_buffer_t *buf;
12041 int allocated = 0, desired = 0;
12044 ASSERT(MUTEX_HELD(&cpu_lock));
12045 ASSERT(MUTEX_HELD(&dtrace_lock));
12049 if (size > dtrace_nonroot_maxsize &&
12050 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE))
12056 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
12059 buf = &bufs[cp->cpu_id];
12062 * If there is already a buffer allocated for this CPU, it
12063 * is only possible that this is a DR event. In this case,
12065 if (buf->dtb_tomax != NULL) {
12066 ASSERT(buf->dtb_size == size);
12070 ASSERT(buf->dtb_xamot == NULL);
12072 if ((buf->dtb_tomax = kmem_zalloc(size,
12073 KM_NOSLEEP | KM_NORMALPRI)) == NULL)
12076 buf->dtb_size = size;
12077 buf->dtb_flags = flags;
12078 buf->dtb_offset = 0;
12079 buf->dtb_drops = 0;
12081 if (flags & DTRACEBUF_NOSWITCH)
12084 if ((buf->dtb_xamot = kmem_zalloc(size,
12085 KM_NOSLEEP | KM_NORMALPRI)) == NULL)
12087 } while ((cp = cp->cpu_next) != cpu_list);
12095 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
12098 buf = &bufs[cp->cpu_id];
12101 if (buf->dtb_xamot != NULL) {
12102 ASSERT(buf->dtb_tomax != NULL);
12103 ASSERT(buf->dtb_size == size);
12104 kmem_free(buf->dtb_xamot, size);
12108 if (buf->dtb_tomax != NULL) {
12109 ASSERT(buf->dtb_size == size);
12110 kmem_free(buf->dtb_tomax, size);
12114 buf->dtb_tomax = NULL;
12115 buf->dtb_xamot = NULL;
12117 } while ((cp = cp->cpu_next) != cpu_list);
12122 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
12123 defined(__mips__) || defined(__powerpc__) || defined(__riscv)
12125 * FreeBSD isn't good at limiting the amount of memory we
12126 * ask to malloc, so let's place a limit here before trying
12127 * to do something that might well end in tears at bedtime.
12129 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1)))
12133 ASSERT(MUTEX_HELD(&dtrace_lock));
12135 if (cpu != DTRACE_CPUALL && cpu != i)
12141 * If there is already a buffer allocated for this CPU, it
12142 * is only possible that this is a DR event. In this case,
12143 * the buffer size must match our specified size.
12145 if (buf->dtb_tomax != NULL) {
12146 ASSERT(buf->dtb_size == size);
12150 ASSERT(buf->dtb_xamot == NULL);
12152 if ((buf->dtb_tomax = kmem_zalloc(size,
12153 KM_NOSLEEP | KM_NORMALPRI)) == NULL)
12156 buf->dtb_size = size;
12157 buf->dtb_flags = flags;
12158 buf->dtb_offset = 0;
12159 buf->dtb_drops = 0;
12161 if (flags & DTRACEBUF_NOSWITCH)
12164 if ((buf->dtb_xamot = kmem_zalloc(size,
12165 KM_NOSLEEP | KM_NORMALPRI)) == NULL)
12173 * Error allocating memory, so free the buffers that were
12174 * allocated before the failed allocation.
12177 if (cpu != DTRACE_CPUALL && cpu != i)
12183 if (buf->dtb_xamot != NULL) {
12184 ASSERT(buf->dtb_tomax != NULL);
12185 ASSERT(buf->dtb_size == size);
12186 kmem_free(buf->dtb_xamot, size);
12190 if (buf->dtb_tomax != NULL) {
12191 ASSERT(buf->dtb_size == size);
12192 kmem_free(buf->dtb_tomax, size);
12196 buf->dtb_tomax = NULL;
12197 buf->dtb_xamot = NULL;
12202 *factor = desired / (allocated > 0 ? allocated : 1);
12208 * Note: called from probe context. This function just increments the drop
12209 * count on a buffer. It has been made a function to allow for the
12210 * possibility of understanding the source of mysterious drop counts. (A
12211 * problem for which one may be particularly disappointed that DTrace cannot
12212 * be used to understand DTrace.)
12215 dtrace_buffer_drop(dtrace_buffer_t *buf)
12221 * Note: called from probe context. This function is called to reserve space
12222 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the
12223 * mstate. Returns the new offset in the buffer, or a negative value if an
12224 * error has occurred.
12227 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align,
12228 dtrace_state_t *state, dtrace_mstate_t *mstate)
12230 intptr_t offs = buf->dtb_offset, soffs;
12235 if (buf->dtb_flags & DTRACEBUF_INACTIVE)
12238 if ((tomax = buf->dtb_tomax) == NULL) {
12239 dtrace_buffer_drop(buf);
12243 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) {
12244 while (offs & (align - 1)) {
12246 * Assert that our alignment is off by a number which
12247 * is itself sizeof (uint32_t) aligned.
12249 ASSERT(!((align - (offs & (align - 1))) &
12250 (sizeof (uint32_t) - 1)));
12251 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
12252 offs += sizeof (uint32_t);
12255 if ((soffs = offs + needed) > buf->dtb_size) {
12256 dtrace_buffer_drop(buf);
12260 if (mstate == NULL)
12263 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs;
12264 mstate->dtms_scratch_size = buf->dtb_size - soffs;
12265 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
12270 if (buf->dtb_flags & DTRACEBUF_FILL) {
12271 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN &&
12272 (buf->dtb_flags & DTRACEBUF_FULL))
12277 total = needed + (offs & (align - 1));
12280 * For a ring buffer, life is quite a bit more complicated. Before
12281 * we can store any padding, we need to adjust our wrapping offset.
12282 * (If we've never before wrapped or we're not about to, no adjustment
12285 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) ||
12286 offs + total > buf->dtb_size) {
12287 woffs = buf->dtb_xamot_offset;
12289 if (offs + total > buf->dtb_size) {
12291 * We can't fit in the end of the buffer. First, a
12292 * sanity check that we can fit in the buffer at all.
12294 if (total > buf->dtb_size) {
12295 dtrace_buffer_drop(buf);
12300 * We're going to be storing at the top of the buffer,
12301 * so now we need to deal with the wrapped offset. We
12302 * only reset our wrapped offset to 0 if it is
12303 * currently greater than the current offset. If it
12304 * is less than the current offset, it is because a
12305 * previous allocation induced a wrap -- but the
12306 * allocation didn't subsequently take the space due
12307 * to an error or false predicate evaluation. In this
12308 * case, we'll just leave the wrapped offset alone: if
12309 * the wrapped offset hasn't been advanced far enough
12310 * for this allocation, it will be adjusted in the
12313 if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
12321 * Now we know that we're going to be storing to the
12322 * top of the buffer and that there is room for us
12323 * there. We need to clear the buffer from the current
12324 * offset to the end (there may be old gunk there).
12326 while (offs < buf->dtb_size)
12330 * We need to set our offset to zero. And because we
12331 * are wrapping, we need to set the bit indicating as
12332 * much. We can also adjust our needed space back
12333 * down to the space required by the ECB -- we know
12334 * that the top of the buffer is aligned.
12338 buf->dtb_flags |= DTRACEBUF_WRAPPED;
12341 * There is room for us in the buffer, so we simply
12342 * need to check the wrapped offset.
12344 if (woffs < offs) {
12346 * The wrapped offset is less than the offset.
12347 * This can happen if we allocated buffer space
12348 * that induced a wrap, but then we didn't
12349 * subsequently take the space due to an error
12350 * or false predicate evaluation. This is
12351 * okay; we know that _this_ allocation isn't
12352 * going to induce a wrap. We still can't
12353 * reset the wrapped offset to be zero,
12354 * however: the space may have been trashed in
12355 * the previous failed probe attempt. But at
12356 * least the wrapped offset doesn't need to
12357 * be adjusted at all...
12363 while (offs + total > woffs) {
12364 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs);
12367 if (epid == DTRACE_EPIDNONE) {
12368 size = sizeof (uint32_t);
12370 ASSERT3U(epid, <=, state->dts_necbs);
12371 ASSERT(state->dts_ecbs[epid - 1] != NULL);
12373 size = state->dts_ecbs[epid - 1]->dte_size;
12376 ASSERT(woffs + size <= buf->dtb_size);
12379 if (woffs + size == buf->dtb_size) {
12381 * We've reached the end of the buffer; we want
12382 * to set the wrapped offset to 0 and break
12383 * out. However, if the offs is 0, then we're
12384 * in a strange edge-condition: the amount of
12385 * space that we want to reserve plus the size
12386 * of the record that we're overwriting is
12387 * greater than the size of the buffer. This
12388 * is problematic because if we reserve the
12389 * space but subsequently don't consume it (due
12390 * to a failed predicate or error) the wrapped
12391 * offset will be 0 -- yet the EPID at offset 0
12392 * will not be committed. This situation is
12393 * relatively easy to deal with: if we're in
12394 * this case, the buffer is indistinguishable
12395 * from one that hasn't wrapped; we need only
12396 * finish the job by clearing the wrapped bit,
12397 * explicitly setting the offset to be 0, and
12398 * zero'ing out the old data in the buffer.
12401 buf->dtb_flags &= ~DTRACEBUF_WRAPPED;
12402 buf->dtb_offset = 0;
12405 while (woffs < buf->dtb_size)
12406 tomax[woffs++] = 0;
12417 * We have a wrapped offset. It may be that the wrapped offset
12418 * has become zero -- that's okay.
12420 buf->dtb_xamot_offset = woffs;
12425 * Now we can plow the buffer with any necessary padding.
12427 while (offs & (align - 1)) {
12429 * Assert that our alignment is off by a number which
12430 * is itself sizeof (uint32_t) aligned.
12432 ASSERT(!((align - (offs & (align - 1))) &
12433 (sizeof (uint32_t) - 1)));
12434 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
12435 offs += sizeof (uint32_t);
12438 if (buf->dtb_flags & DTRACEBUF_FILL) {
12439 if (offs + needed > buf->dtb_size - state->dts_reserve) {
12440 buf->dtb_flags |= DTRACEBUF_FULL;
12445 if (mstate == NULL)
12449 * For ring buffers and fill buffers, the scratch space is always
12450 * the inactive buffer.
12452 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot;
12453 mstate->dtms_scratch_size = buf->dtb_size;
12454 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
12460 dtrace_buffer_polish(dtrace_buffer_t *buf)
12462 ASSERT(buf->dtb_flags & DTRACEBUF_RING);
12463 ASSERT(MUTEX_HELD(&dtrace_lock));
12465 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED))
12469 * We need to polish the ring buffer. There are three cases:
12471 * - The first (and presumably most common) is that there is no gap
12472 * between the buffer offset and the wrapped offset. In this case,
12473 * there is nothing in the buffer that isn't valid data; we can
12474 * mark the buffer as polished and return.
12476 * - The second (less common than the first but still more common
12477 * than the third) is that there is a gap between the buffer offset
12478 * and the wrapped offset, and the wrapped offset is larger than the
12479 * buffer offset. This can happen because of an alignment issue, or
12480 * can happen because of a call to dtrace_buffer_reserve() that
12481 * didn't subsequently consume the buffer space. In this case,
12482 * we need to zero the data from the buffer offset to the wrapped
12485 * - The third (and least common) is that there is a gap between the
12486 * buffer offset and the wrapped offset, but the wrapped offset is
12487 * _less_ than the buffer offset. This can only happen because a
12488 * call to dtrace_buffer_reserve() induced a wrap, but the space
12489 * was not subsequently consumed. In this case, we need to zero the
12490 * space from the offset to the end of the buffer _and_ from the
12491 * top of the buffer to the wrapped offset.
12493 if (buf->dtb_offset < buf->dtb_xamot_offset) {
12494 bzero(buf->dtb_tomax + buf->dtb_offset,
12495 buf->dtb_xamot_offset - buf->dtb_offset);
12498 if (buf->dtb_offset > buf->dtb_xamot_offset) {
12499 bzero(buf->dtb_tomax + buf->dtb_offset,
12500 buf->dtb_size - buf->dtb_offset);
12501 bzero(buf->dtb_tomax, buf->dtb_xamot_offset);
12506 * This routine determines if data generated at the specified time has likely
12507 * been entirely consumed at user-level. This routine is called to determine
12508 * if an ECB on a defunct probe (but for an active enabling) can be safely
12509 * disabled and destroyed.
12512 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when)
12516 for (i = 0; i < NCPU; i++) {
12517 dtrace_buffer_t *buf = &bufs[i];
12519 if (buf->dtb_size == 0)
12522 if (buf->dtb_flags & DTRACEBUF_RING)
12525 if (!buf->dtb_switched && buf->dtb_offset != 0)
12528 if (buf->dtb_switched - buf->dtb_interval < when)
12536 dtrace_buffer_free(dtrace_buffer_t *bufs)
12540 for (i = 0; i < NCPU; i++) {
12541 dtrace_buffer_t *buf = &bufs[i];
12543 if (buf->dtb_tomax == NULL) {
12544 ASSERT(buf->dtb_xamot == NULL);
12545 ASSERT(buf->dtb_size == 0);
12549 if (buf->dtb_xamot != NULL) {
12550 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
12551 kmem_free(buf->dtb_xamot, buf->dtb_size);
12554 kmem_free(buf->dtb_tomax, buf->dtb_size);
12556 buf->dtb_tomax = NULL;
12557 buf->dtb_xamot = NULL;
12562 * DTrace Enabling Functions
12564 static dtrace_enabling_t *
12565 dtrace_enabling_create(dtrace_vstate_t *vstate)
12567 dtrace_enabling_t *enab;
12569 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP);
12570 enab->dten_vstate = vstate;
12576 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb)
12578 dtrace_ecbdesc_t **ndesc;
12579 size_t osize, nsize;
12582 * We can't add to enablings after we've enabled them, or after we've
12585 ASSERT(enab->dten_probegen == 0);
12586 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
12588 if (enab->dten_ndesc < enab->dten_maxdesc) {
12589 enab->dten_desc[enab->dten_ndesc++] = ecb;
12593 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
12595 if (enab->dten_maxdesc == 0) {
12596 enab->dten_maxdesc = 1;
12598 enab->dten_maxdesc <<= 1;
12601 ASSERT(enab->dten_ndesc < enab->dten_maxdesc);
12603 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
12604 ndesc = kmem_zalloc(nsize, KM_SLEEP);
12605 bcopy(enab->dten_desc, ndesc, osize);
12606 if (enab->dten_desc != NULL)
12607 kmem_free(enab->dten_desc, osize);
12609 enab->dten_desc = ndesc;
12610 enab->dten_desc[enab->dten_ndesc++] = ecb;
12614 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb,
12615 dtrace_probedesc_t *pd)
12617 dtrace_ecbdesc_t *new;
12618 dtrace_predicate_t *pred;
12619 dtrace_actdesc_t *act;
12622 * We're going to create a new ECB description that matches the
12623 * specified ECB in every way, but has the specified probe description.
12625 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
12627 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL)
12628 dtrace_predicate_hold(pred);
12630 for (act = ecb->dted_action; act != NULL; act = act->dtad_next)
12631 dtrace_actdesc_hold(act);
12633 new->dted_action = ecb->dted_action;
12634 new->dted_pred = ecb->dted_pred;
12635 new->dted_probe = *pd;
12636 new->dted_uarg = ecb->dted_uarg;
12638 dtrace_enabling_add(enab, new);
12642 dtrace_enabling_dump(dtrace_enabling_t *enab)
12646 for (i = 0; i < enab->dten_ndesc; i++) {
12647 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe;
12650 printf("dtrace: enabling probe %d (%s:%s:%s:%s)\n", i,
12651 desc->dtpd_provider, desc->dtpd_mod,
12652 desc->dtpd_func, desc->dtpd_name);
12654 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i,
12655 desc->dtpd_provider, desc->dtpd_mod,
12656 desc->dtpd_func, desc->dtpd_name);
12662 dtrace_enabling_destroy(dtrace_enabling_t *enab)
12665 dtrace_ecbdesc_t *ep;
12666 dtrace_vstate_t *vstate = enab->dten_vstate;
12668 ASSERT(MUTEX_HELD(&dtrace_lock));
12670 for (i = 0; i < enab->dten_ndesc; i++) {
12671 dtrace_actdesc_t *act, *next;
12672 dtrace_predicate_t *pred;
12674 ep = enab->dten_desc[i];
12676 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL)
12677 dtrace_predicate_release(pred, vstate);
12679 for (act = ep->dted_action; act != NULL; act = next) {
12680 next = act->dtad_next;
12681 dtrace_actdesc_release(act, vstate);
12684 kmem_free(ep, sizeof (dtrace_ecbdesc_t));
12687 if (enab->dten_desc != NULL)
12688 kmem_free(enab->dten_desc,
12689 enab->dten_maxdesc * sizeof (dtrace_enabling_t *));
12692 * If this was a retained enabling, decrement the dts_nretained count
12693 * and take it off of the dtrace_retained list.
12695 if (enab->dten_prev != NULL || enab->dten_next != NULL ||
12696 dtrace_retained == enab) {
12697 ASSERT(enab->dten_vstate->dtvs_state != NULL);
12698 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0);
12699 enab->dten_vstate->dtvs_state->dts_nretained--;
12700 dtrace_retained_gen++;
12703 if (enab->dten_prev == NULL) {
12704 if (dtrace_retained == enab) {
12705 dtrace_retained = enab->dten_next;
12707 if (dtrace_retained != NULL)
12708 dtrace_retained->dten_prev = NULL;
12711 ASSERT(enab != dtrace_retained);
12712 ASSERT(dtrace_retained != NULL);
12713 enab->dten_prev->dten_next = enab->dten_next;
12716 if (enab->dten_next != NULL) {
12717 ASSERT(dtrace_retained != NULL);
12718 enab->dten_next->dten_prev = enab->dten_prev;
12721 kmem_free(enab, sizeof (dtrace_enabling_t));
12725 dtrace_enabling_retain(dtrace_enabling_t *enab)
12727 dtrace_state_t *state;
12729 ASSERT(MUTEX_HELD(&dtrace_lock));
12730 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
12731 ASSERT(enab->dten_vstate != NULL);
12733 state = enab->dten_vstate->dtvs_state;
12734 ASSERT(state != NULL);
12737 * We only allow each state to retain dtrace_retain_max enablings.
12739 if (state->dts_nretained >= dtrace_retain_max)
12742 state->dts_nretained++;
12743 dtrace_retained_gen++;
12745 if (dtrace_retained == NULL) {
12746 dtrace_retained = enab;
12750 enab->dten_next = dtrace_retained;
12751 dtrace_retained->dten_prev = enab;
12752 dtrace_retained = enab;
12758 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match,
12759 dtrace_probedesc_t *create)
12761 dtrace_enabling_t *new, *enab;
12762 int found = 0, err = ENOENT;
12764 ASSERT(MUTEX_HELD(&dtrace_lock));
12765 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN);
12766 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN);
12767 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN);
12768 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN);
12770 new = dtrace_enabling_create(&state->dts_vstate);
12773 * Iterate over all retained enablings, looking for enablings that
12774 * match the specified state.
12776 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12780 * dtvs_state can only be NULL for helper enablings -- and
12781 * helper enablings can't be retained.
12783 ASSERT(enab->dten_vstate->dtvs_state != NULL);
12785 if (enab->dten_vstate->dtvs_state != state)
12789 * Now iterate over each probe description; we're looking for
12790 * an exact match to the specified probe description.
12792 for (i = 0; i < enab->dten_ndesc; i++) {
12793 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
12794 dtrace_probedesc_t *pd = &ep->dted_probe;
12796 if (strcmp(pd->dtpd_provider, match->dtpd_provider))
12799 if (strcmp(pd->dtpd_mod, match->dtpd_mod))
12802 if (strcmp(pd->dtpd_func, match->dtpd_func))
12805 if (strcmp(pd->dtpd_name, match->dtpd_name))
12809 * We have a winning probe! Add it to our growing
12813 dtrace_enabling_addlike(new, ep, create);
12817 if (!found || (err = dtrace_enabling_retain(new)) != 0) {
12818 dtrace_enabling_destroy(new);
12826 dtrace_enabling_retract(dtrace_state_t *state)
12828 dtrace_enabling_t *enab, *next;
12830 ASSERT(MUTEX_HELD(&dtrace_lock));
12833 * Iterate over all retained enablings, destroy the enablings retained
12834 * for the specified state.
12836 for (enab = dtrace_retained; enab != NULL; enab = next) {
12837 next = enab->dten_next;
12840 * dtvs_state can only be NULL for helper enablings -- and
12841 * helper enablings can't be retained.
12843 ASSERT(enab->dten_vstate->dtvs_state != NULL);
12845 if (enab->dten_vstate->dtvs_state == state) {
12846 ASSERT(state->dts_nretained > 0);
12847 dtrace_enabling_destroy(enab);
12851 ASSERT(state->dts_nretained == 0);
12855 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched)
12860 ASSERT(MUTEX_HELD(&cpu_lock));
12861 ASSERT(MUTEX_HELD(&dtrace_lock));
12863 for (i = 0; i < enab->dten_ndesc; i++) {
12864 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
12866 enab->dten_current = ep;
12867 enab->dten_error = 0;
12869 matched += dtrace_probe_enable(&ep->dted_probe, enab);
12871 if (enab->dten_error != 0) {
12873 * If we get an error half-way through enabling the
12874 * probes, we kick out -- perhaps with some number of
12875 * them enabled. Leaving enabled probes enabled may
12876 * be slightly confusing for user-level, but we expect
12877 * that no one will attempt to actually drive on in
12878 * the face of such errors. If this is an anonymous
12879 * enabling (indicated with a NULL nmatched pointer),
12880 * we cmn_err() a message. We aren't expecting to
12881 * get such an error -- such as it can exist at all,
12882 * it would be a result of corrupted DOF in the driver
12885 if (nmatched == NULL) {
12886 cmn_err(CE_WARN, "dtrace_enabling_match() "
12887 "error on %p: %d", (void *)ep,
12891 return (enab->dten_error);
12895 enab->dten_probegen = dtrace_probegen;
12896 if (nmatched != NULL)
12897 *nmatched = matched;
12903 dtrace_enabling_matchall(void)
12905 dtrace_enabling_t *enab;
12907 mutex_enter(&cpu_lock);
12908 mutex_enter(&dtrace_lock);
12911 * Iterate over all retained enablings to see if any probes match
12912 * against them. We only perform this operation on enablings for which
12913 * we have sufficient permissions by virtue of being in the global zone
12914 * or in the same zone as the DTrace client. Because we can be called
12915 * after dtrace_detach() has been called, we cannot assert that there
12916 * are retained enablings. We can safely load from dtrace_retained,
12917 * however: the taskq_destroy() at the end of dtrace_detach() will
12918 * block pending our completion.
12920 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12922 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred;
12924 if (INGLOBALZONE(curproc) ||
12925 cr != NULL && getzoneid() == crgetzoneid(cr))
12927 (void) dtrace_enabling_match(enab, NULL);
12930 mutex_exit(&dtrace_lock);
12931 mutex_exit(&cpu_lock);
12935 * If an enabling is to be enabled without having matched probes (that is, if
12936 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
12937 * enabling must be _primed_ by creating an ECB for every ECB description.
12938 * This must be done to assure that we know the number of speculations, the
12939 * number of aggregations, the minimum buffer size needed, etc. before we
12940 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually
12941 * enabling any probes, we create ECBs for every ECB decription, but with a
12942 * NULL probe -- which is exactly what this function does.
12945 dtrace_enabling_prime(dtrace_state_t *state)
12947 dtrace_enabling_t *enab;
12950 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12951 ASSERT(enab->dten_vstate->dtvs_state != NULL);
12953 if (enab->dten_vstate->dtvs_state != state)
12957 * We don't want to prime an enabling more than once, lest
12958 * we allow a malicious user to induce resource exhaustion.
12959 * (The ECBs that result from priming an enabling aren't
12960 * leaked -- but they also aren't deallocated until the
12961 * consumer state is destroyed.)
12963 if (enab->dten_primed)
12966 for (i = 0; i < enab->dten_ndesc; i++) {
12967 enab->dten_current = enab->dten_desc[i];
12968 (void) dtrace_probe_enable(NULL, enab);
12971 enab->dten_primed = 1;
12976 * Called to indicate that probes should be provided due to retained
12977 * enablings. This is implemented in terms of dtrace_probe_provide(), but it
12978 * must take an initial lap through the enabling calling the dtps_provide()
12979 * entry point explicitly to allow for autocreated probes.
12982 dtrace_enabling_provide(dtrace_provider_t *prv)
12985 dtrace_probedesc_t desc;
12986 dtrace_genid_t gen;
12988 ASSERT(MUTEX_HELD(&dtrace_lock));
12989 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
12993 prv = dtrace_provider;
12997 dtrace_enabling_t *enab;
12998 void *parg = prv->dtpv_arg;
13001 gen = dtrace_retained_gen;
13002 for (enab = dtrace_retained; enab != NULL;
13003 enab = enab->dten_next) {
13004 for (i = 0; i < enab->dten_ndesc; i++) {
13005 desc = enab->dten_desc[i]->dted_probe;
13006 mutex_exit(&dtrace_lock);
13007 prv->dtpv_pops.dtps_provide(parg, &desc);
13008 mutex_enter(&dtrace_lock);
13010 * Process the retained enablings again if
13011 * they have changed while we weren't holding
13014 if (gen != dtrace_retained_gen)
13018 } while (all && (prv = prv->dtpv_next) != NULL);
13020 mutex_exit(&dtrace_lock);
13021 dtrace_probe_provide(NULL, all ? NULL : prv);
13022 mutex_enter(&dtrace_lock);
13026 * Called to reap ECBs that are attached to probes from defunct providers.
13029 dtrace_enabling_reap(void)
13031 dtrace_provider_t *prov;
13032 dtrace_probe_t *probe;
13037 mutex_enter(&cpu_lock);
13038 mutex_enter(&dtrace_lock);
13040 for (i = 0; i < dtrace_nprobes; i++) {
13041 if ((probe = dtrace_probes[i]) == NULL)
13044 if (probe->dtpr_ecb == NULL)
13047 prov = probe->dtpr_provider;
13049 if ((when = prov->dtpv_defunct) == 0)
13053 * We have ECBs on a defunct provider: we want to reap these
13054 * ECBs to allow the provider to unregister. The destruction
13055 * of these ECBs must be done carefully: if we destroy the ECB
13056 * and the consumer later wishes to consume an EPID that
13057 * corresponds to the destroyed ECB (and if the EPID metadata
13058 * has not been previously consumed), the consumer will abort
13059 * processing on the unknown EPID. To reduce (but not, sadly,
13060 * eliminate) the possibility of this, we will only destroy an
13061 * ECB for a defunct provider if, for the state that
13062 * corresponds to the ECB:
13064 * (a) There is no speculative tracing (which can effectively
13065 * cache an EPID for an arbitrary amount of time).
13067 * (b) The principal buffers have been switched twice since the
13068 * provider became defunct.
13070 * (c) The aggregation buffers are of zero size or have been
13071 * switched twice since the provider became defunct.
13073 * We use dts_speculates to determine (a) and call a function
13074 * (dtrace_buffer_consumed()) to determine (b) and (c). Note
13075 * that as soon as we've been unable to destroy one of the ECBs
13076 * associated with the probe, we quit trying -- reaping is only
13077 * fruitful in as much as we can destroy all ECBs associated
13078 * with the defunct provider's probes.
13080 while ((ecb = probe->dtpr_ecb) != NULL) {
13081 dtrace_state_t *state = ecb->dte_state;
13082 dtrace_buffer_t *buf = state->dts_buffer;
13083 dtrace_buffer_t *aggbuf = state->dts_aggbuffer;
13085 if (state->dts_speculates)
13088 if (!dtrace_buffer_consumed(buf, when))
13091 if (!dtrace_buffer_consumed(aggbuf, when))
13094 dtrace_ecb_disable(ecb);
13095 ASSERT(probe->dtpr_ecb != ecb);
13096 dtrace_ecb_destroy(ecb);
13100 mutex_exit(&dtrace_lock);
13101 mutex_exit(&cpu_lock);
13105 * DTrace DOF Functions
13109 dtrace_dof_error(dof_hdr_t *dof, const char *str)
13111 if (dtrace_err_verbose)
13112 cmn_err(CE_WARN, "failed to process DOF: %s", str);
13114 #ifdef DTRACE_ERRDEBUG
13115 dtrace_errdebug(str);
13120 * Create DOF out of a currently enabled state. Right now, we only create
13121 * DOF containing the run-time options -- but this could be expanded to create
13122 * complete DOF representing the enabled state.
13125 dtrace_dof_create(dtrace_state_t *state)
13129 dof_optdesc_t *opt;
13130 int i, len = sizeof (dof_hdr_t) +
13131 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
13132 sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
13134 ASSERT(MUTEX_HELD(&dtrace_lock));
13136 dof = kmem_zalloc(len, KM_SLEEP);
13137 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
13138 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
13139 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
13140 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
13142 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
13143 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
13144 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
13145 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
13146 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
13147 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
13149 dof->dofh_flags = 0;
13150 dof->dofh_hdrsize = sizeof (dof_hdr_t);
13151 dof->dofh_secsize = sizeof (dof_sec_t);
13152 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
13153 dof->dofh_secoff = sizeof (dof_hdr_t);
13154 dof->dofh_loadsz = len;
13155 dof->dofh_filesz = len;
13159 * Fill in the option section header...
13161 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
13162 sec->dofs_type = DOF_SECT_OPTDESC;
13163 sec->dofs_align = sizeof (uint64_t);
13164 sec->dofs_flags = DOF_SECF_LOAD;
13165 sec->dofs_entsize = sizeof (dof_optdesc_t);
13167 opt = (dof_optdesc_t *)((uintptr_t)sec +
13168 roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
13170 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
13171 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
13173 for (i = 0; i < DTRACEOPT_MAX; i++) {
13174 opt[i].dofo_option = i;
13175 opt[i].dofo_strtab = DOF_SECIDX_NONE;
13176 opt[i].dofo_value = state->dts_options[i];
13183 dtrace_dof_copyin(uintptr_t uarg, int *errp)
13185 dof_hdr_t hdr, *dof;
13187 ASSERT(!MUTEX_HELD(&dtrace_lock));
13190 * First, we're going to copyin() the sizeof (dof_hdr_t).
13192 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) {
13193 dtrace_dof_error(NULL, "failed to copyin DOF header");
13199 * Now we'll allocate the entire DOF and copy it in -- provided
13200 * that the length isn't outrageous.
13202 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
13203 dtrace_dof_error(&hdr, "load size exceeds maximum");
13208 if (hdr.dofh_loadsz < sizeof (hdr)) {
13209 dtrace_dof_error(&hdr, "invalid load size");
13214 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP);
13216 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 ||
13217 dof->dofh_loadsz != hdr.dofh_loadsz) {
13218 kmem_free(dof, hdr.dofh_loadsz);
13228 dtrace_dof_copyin_proc(struct proc *p, uintptr_t uarg, int *errp)
13230 dof_hdr_t hdr, *dof;
13234 ASSERT(!MUTEX_HELD(&dtrace_lock));
13239 * First, we're going to copyin() the sizeof (dof_hdr_t).
13241 if (proc_readmem(td, p, uarg, &hdr, sizeof(hdr)) != sizeof(hdr)) {
13242 dtrace_dof_error(NULL, "failed to copyin DOF header");
13248 * Now we'll allocate the entire DOF and copy it in -- provided
13249 * that the length isn't outrageous.
13251 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
13252 dtrace_dof_error(&hdr, "load size exceeds maximum");
13256 loadsz = (size_t)hdr.dofh_loadsz;
13258 if (loadsz < sizeof (hdr)) {
13259 dtrace_dof_error(&hdr, "invalid load size");
13264 dof = kmem_alloc(loadsz, KM_SLEEP);
13266 if (proc_readmem(td, p, uarg, dof, loadsz) != loadsz ||
13267 dof->dofh_loadsz != loadsz) {
13268 kmem_free(dof, hdr.dofh_loadsz);
13276 static __inline uchar_t
13277 dtrace_dof_char(char c)
13298 return (c - 'A' + 10);
13305 return (c - 'a' + 10);
13307 /* Should not reach here. */
13308 return (UCHAR_MAX);
13310 #endif /* __FreeBSD__ */
13313 dtrace_dof_property(const char *name)
13317 u_char *data, *eol;
13319 size_t bytes, len, i;
13325 doffile = preload_search_by_type("dtrace_dof");
13326 if (doffile == NULL)
13329 data = preload_fetch_addr(doffile);
13330 len = preload_fetch_size(doffile);
13332 /* Look for the end of the line. All lines end in a newline. */
13333 eol = memchr(data, '\n', len);
13337 if (strncmp(name, data, strlen(name)) == 0)
13340 eol++; /* skip past the newline */
13345 /* We've found the data corresponding to the specified key. */
13347 data += strlen(name) + 1; /* skip past the '=' */
13349 if (len % 2 != 0) {
13350 dtrace_dof_error(NULL, "invalid DOF encoding length");
13354 if (bytes < sizeof(dof_hdr_t)) {
13355 dtrace_dof_error(NULL, "truncated header");
13360 * Each byte is represented by the two ASCII characters in its hex
13363 dofbuf = malloc(bytes, M_SOLARIS, M_WAITOK);
13364 for (i = 0; i < bytes; i++) {
13365 c1 = dtrace_dof_char(data[i * 2]);
13366 c2 = dtrace_dof_char(data[i * 2 + 1]);
13367 if (c1 == UCHAR_MAX || c2 == UCHAR_MAX) {
13368 dtrace_dof_error(NULL, "invalid hex char in DOF");
13371 dofbuf[i] = c1 * 16 + c2;
13374 dof = (dof_hdr_t *)dofbuf;
13375 if (bytes < dof->dofh_loadsz) {
13376 dtrace_dof_error(NULL, "truncated DOF");
13380 if (dof->dofh_loadsz >= dtrace_dof_maxsize) {
13381 dtrace_dof_error(NULL, "oversized DOF");
13388 free(dof, M_SOLARIS);
13390 #else /* __FreeBSD__ */
13393 unsigned int len, i;
13397 * Unfortunately, array of values in .conf files are always (and
13398 * only) interpreted to be integer arrays. We must read our DOF
13399 * as an integer array, and then squeeze it into a byte array.
13401 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0,
13402 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS)
13405 for (i = 0; i < len; i++)
13406 buf[i] = (uchar_t)(((int *)buf)[i]);
13408 if (len < sizeof (dof_hdr_t)) {
13409 ddi_prop_free(buf);
13410 dtrace_dof_error(NULL, "truncated header");
13414 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) {
13415 ddi_prop_free(buf);
13416 dtrace_dof_error(NULL, "truncated DOF");
13420 if (loadsz >= dtrace_dof_maxsize) {
13421 ddi_prop_free(buf);
13422 dtrace_dof_error(NULL, "oversized DOF");
13426 dof = kmem_alloc(loadsz, KM_SLEEP);
13427 bcopy(buf, dof, loadsz);
13428 ddi_prop_free(buf);
13431 #endif /* !__FreeBSD__ */
13435 dtrace_dof_destroy(dof_hdr_t *dof)
13437 kmem_free(dof, dof->dofh_loadsz);
13441 * Return the dof_sec_t pointer corresponding to a given section index. If the
13442 * index is not valid, dtrace_dof_error() is called and NULL is returned. If
13443 * a type other than DOF_SECT_NONE is specified, the header is checked against
13444 * this type and NULL is returned if the types do not match.
13447 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i)
13449 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)
13450 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize);
13452 if (i >= dof->dofh_secnum) {
13453 dtrace_dof_error(dof, "referenced section index is invalid");
13457 if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
13458 dtrace_dof_error(dof, "referenced section is not loadable");
13462 if (type != DOF_SECT_NONE && type != sec->dofs_type) {
13463 dtrace_dof_error(dof, "referenced section is the wrong type");
13470 static dtrace_probedesc_t *
13471 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc)
13473 dof_probedesc_t *probe;
13475 uintptr_t daddr = (uintptr_t)dof;
13479 if (sec->dofs_type != DOF_SECT_PROBEDESC) {
13480 dtrace_dof_error(dof, "invalid probe section");
13484 if (sec->dofs_align != sizeof (dof_secidx_t)) {
13485 dtrace_dof_error(dof, "bad alignment in probe description");
13489 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) {
13490 dtrace_dof_error(dof, "truncated probe description");
13494 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset);
13495 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab);
13497 if (strtab == NULL)
13500 str = daddr + strtab->dofs_offset;
13501 size = strtab->dofs_size;
13503 if (probe->dofp_provider >= strtab->dofs_size) {
13504 dtrace_dof_error(dof, "corrupt probe provider");
13508 (void) strncpy(desc->dtpd_provider,
13509 (char *)(str + probe->dofp_provider),
13510 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider));
13512 if (probe->dofp_mod >= strtab->dofs_size) {
13513 dtrace_dof_error(dof, "corrupt probe module");
13517 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod),
13518 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod));
13520 if (probe->dofp_func >= strtab->dofs_size) {
13521 dtrace_dof_error(dof, "corrupt probe function");
13525 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func),
13526 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func));
13528 if (probe->dofp_name >= strtab->dofs_size) {
13529 dtrace_dof_error(dof, "corrupt probe name");
13533 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name),
13534 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name));
13539 static dtrace_difo_t *
13540 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13545 dof_difohdr_t *dofd;
13546 uintptr_t daddr = (uintptr_t)dof;
13547 size_t max = dtrace_difo_maxsize;
13550 static const struct {
13558 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf),
13559 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t),
13560 sizeof (dif_instr_t), "multiple DIF sections" },
13562 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab),
13563 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t),
13564 sizeof (uint64_t), "multiple integer tables" },
13566 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab),
13567 offsetof(dtrace_difo_t, dtdo_strlen), 0,
13568 sizeof (char), "multiple string tables" },
13570 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab),
13571 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t),
13572 sizeof (uint_t), "multiple variable tables" },
13574 { DOF_SECT_NONE, 0, 0, 0, 0, NULL }
13577 if (sec->dofs_type != DOF_SECT_DIFOHDR) {
13578 dtrace_dof_error(dof, "invalid DIFO header section");
13582 if (sec->dofs_align != sizeof (dof_secidx_t)) {
13583 dtrace_dof_error(dof, "bad alignment in DIFO header");
13587 if (sec->dofs_size < sizeof (dof_difohdr_t) ||
13588 sec->dofs_size % sizeof (dof_secidx_t)) {
13589 dtrace_dof_error(dof, "bad size in DIFO header");
13593 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
13594 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1;
13596 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
13597 dp->dtdo_rtype = dofd->dofd_rtype;
13599 for (l = 0; l < n; l++) {
13604 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE,
13605 dofd->dofd_links[l])) == NULL)
13606 goto err; /* invalid section link */
13608 if (ttl + subsec->dofs_size > max) {
13609 dtrace_dof_error(dof, "exceeds maximum size");
13613 ttl += subsec->dofs_size;
13615 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) {
13616 if (subsec->dofs_type != difo[i].section)
13619 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) {
13620 dtrace_dof_error(dof, "section not loaded");
13624 if (subsec->dofs_align != difo[i].align) {
13625 dtrace_dof_error(dof, "bad alignment");
13629 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs);
13630 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs);
13632 if (*bufp != NULL) {
13633 dtrace_dof_error(dof, difo[i].msg);
13637 if (difo[i].entsize != subsec->dofs_entsize) {
13638 dtrace_dof_error(dof, "entry size mismatch");
13642 if (subsec->dofs_entsize != 0 &&
13643 (subsec->dofs_size % subsec->dofs_entsize) != 0) {
13644 dtrace_dof_error(dof, "corrupt entry size");
13648 *lenp = subsec->dofs_size;
13649 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP);
13650 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset),
13651 *bufp, subsec->dofs_size);
13653 if (subsec->dofs_entsize != 0)
13654 *lenp /= subsec->dofs_entsize;
13660 * If we encounter a loadable DIFO sub-section that is not
13661 * known to us, assume this is a broken program and fail.
13663 if (difo[i].section == DOF_SECT_NONE &&
13664 (subsec->dofs_flags & DOF_SECF_LOAD)) {
13665 dtrace_dof_error(dof, "unrecognized DIFO subsection");
13670 if (dp->dtdo_buf == NULL) {
13672 * We can't have a DIF object without DIF text.
13674 dtrace_dof_error(dof, "missing DIF text");
13679 * Before we validate the DIF object, run through the variable table
13680 * looking for the strings -- if any of their size are under, we'll set
13681 * their size to be the system-wide default string size. Note that
13682 * this should _not_ happen if the "strsize" option has been set --
13683 * in this case, the compiler should have set the size to reflect the
13684 * setting of the option.
13686 for (i = 0; i < dp->dtdo_varlen; i++) {
13687 dtrace_difv_t *v = &dp->dtdo_vartab[i];
13688 dtrace_diftype_t *t = &v->dtdv_type;
13690 if (v->dtdv_id < DIF_VAR_OTHER_UBASE)
13693 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0)
13694 t->dtdt_size = dtrace_strsize_default;
13697 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0)
13700 dtrace_difo_init(dp, vstate);
13704 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
13705 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
13706 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
13707 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
13709 kmem_free(dp, sizeof (dtrace_difo_t));
13713 static dtrace_predicate_t *
13714 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13719 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL)
13722 return (dtrace_predicate_create(dp));
13725 static dtrace_actdesc_t *
13726 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13729 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next;
13730 dof_actdesc_t *desc;
13731 dof_sec_t *difosec;
13733 uintptr_t daddr = (uintptr_t)dof;
13735 dtrace_actkind_t kind;
13737 if (sec->dofs_type != DOF_SECT_ACTDESC) {
13738 dtrace_dof_error(dof, "invalid action section");
13742 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) {
13743 dtrace_dof_error(dof, "truncated action description");
13747 if (sec->dofs_align != sizeof (uint64_t)) {
13748 dtrace_dof_error(dof, "bad alignment in action description");
13752 if (sec->dofs_size < sec->dofs_entsize) {
13753 dtrace_dof_error(dof, "section entry size exceeds total size");
13757 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) {
13758 dtrace_dof_error(dof, "bad entry size in action description");
13762 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) {
13763 dtrace_dof_error(dof, "actions exceed dtrace_actions_max");
13767 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
13768 desc = (dof_actdesc_t *)(daddr +
13769 (uintptr_t)sec->dofs_offset + offs);
13770 kind = (dtrace_actkind_t)desc->dofa_kind;
13772 if ((DTRACEACT_ISPRINTFLIKE(kind) &&
13773 (kind != DTRACEACT_PRINTA ||
13774 desc->dofa_strtab != DOF_SECIDX_NONE)) ||
13775 (kind == DTRACEACT_DIFEXPR &&
13776 desc->dofa_strtab != DOF_SECIDX_NONE)) {
13782 * The argument to these actions is an index into the
13783 * DOF string table. For printf()-like actions, this
13784 * is the format string. For print(), this is the
13785 * CTF type of the expression result.
13787 if ((strtab = dtrace_dof_sect(dof,
13788 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL)
13791 str = (char *)((uintptr_t)dof +
13792 (uintptr_t)strtab->dofs_offset);
13794 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) {
13795 if (str[i] == '\0')
13799 if (i >= strtab->dofs_size) {
13800 dtrace_dof_error(dof, "bogus format string");
13804 if (i == desc->dofa_arg) {
13805 dtrace_dof_error(dof, "empty format string");
13809 i -= desc->dofa_arg;
13810 fmt = kmem_alloc(i + 1, KM_SLEEP);
13811 bcopy(&str[desc->dofa_arg], fmt, i + 1);
13812 arg = (uint64_t)(uintptr_t)fmt;
13814 if (kind == DTRACEACT_PRINTA) {
13815 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE);
13818 arg = desc->dofa_arg;
13822 act = dtrace_actdesc_create(kind, desc->dofa_ntuple,
13823 desc->dofa_uarg, arg);
13825 if (last != NULL) {
13826 last->dtad_next = act;
13833 if (desc->dofa_difo == DOF_SECIDX_NONE)
13836 if ((difosec = dtrace_dof_sect(dof,
13837 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL)
13840 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr);
13842 if (act->dtad_difo == NULL)
13846 ASSERT(first != NULL);
13850 for (act = first; act != NULL; act = next) {
13851 next = act->dtad_next;
13852 dtrace_actdesc_release(act, vstate);
13858 static dtrace_ecbdesc_t *
13859 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13862 dtrace_ecbdesc_t *ep;
13863 dof_ecbdesc_t *ecb;
13864 dtrace_probedesc_t *desc;
13865 dtrace_predicate_t *pred = NULL;
13867 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) {
13868 dtrace_dof_error(dof, "truncated ECB description");
13872 if (sec->dofs_align != sizeof (uint64_t)) {
13873 dtrace_dof_error(dof, "bad alignment in ECB description");
13877 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset);
13878 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes);
13883 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
13884 ep->dted_uarg = ecb->dofe_uarg;
13885 desc = &ep->dted_probe;
13887 if (dtrace_dof_probedesc(dof, sec, desc) == NULL)
13890 if (ecb->dofe_pred != DOF_SECIDX_NONE) {
13891 if ((sec = dtrace_dof_sect(dof,
13892 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL)
13895 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL)
13898 ep->dted_pred.dtpdd_predicate = pred;
13901 if (ecb->dofe_actions != DOF_SECIDX_NONE) {
13902 if ((sec = dtrace_dof_sect(dof,
13903 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL)
13906 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr);
13908 if (ep->dted_action == NULL)
13916 dtrace_predicate_release(pred, vstate);
13917 kmem_free(ep, sizeof (dtrace_ecbdesc_t));
13922 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the
13923 * specified DOF. SETX relocations are computed using 'ubase', the base load
13924 * address of the object containing the DOF, and DOFREL relocations are relative
13925 * to the relocation offset within the DOF.
13928 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase,
13931 uintptr_t daddr = (uintptr_t)dof;
13932 dof_relohdr_t *dofr =
13933 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
13934 dof_sec_t *ss, *rs, *ts;
13938 if (sec->dofs_size < sizeof (dof_relohdr_t) ||
13939 sec->dofs_align != sizeof (dof_secidx_t)) {
13940 dtrace_dof_error(dof, "invalid relocation header");
13944 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab);
13945 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec);
13946 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec);
13948 if (ss == NULL || rs == NULL || ts == NULL)
13949 return (-1); /* dtrace_dof_error() has been called already */
13951 if (rs->dofs_entsize < sizeof (dof_relodesc_t) ||
13952 rs->dofs_align != sizeof (uint64_t)) {
13953 dtrace_dof_error(dof, "invalid relocation section");
13957 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset);
13958 n = rs->dofs_size / rs->dofs_entsize;
13960 for (i = 0; i < n; i++) {
13961 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset;
13963 switch (r->dofr_type) {
13964 case DOF_RELO_NONE:
13966 case DOF_RELO_SETX:
13967 case DOF_RELO_DOFREL:
13968 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset +
13969 sizeof (uint64_t) > ts->dofs_size) {
13970 dtrace_dof_error(dof, "bad relocation offset");
13974 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) {
13975 dtrace_dof_error(dof, "misaligned setx relo");
13979 if (r->dofr_type == DOF_RELO_SETX)
13980 *(uint64_t *)taddr += ubase;
13982 *(uint64_t *)taddr +=
13983 udaddr + ts->dofs_offset + r->dofr_offset;
13986 dtrace_dof_error(dof, "invalid relocation type");
13990 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize);
13997 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
13998 * header: it should be at the front of a memory region that is at least
13999 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
14000 * size. It need not be validated in any other way.
14003 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr,
14004 dtrace_enabling_t **enabp, uint64_t ubase, uint64_t udaddr, int noprobes)
14006 uint64_t len = dof->dofh_loadsz, seclen;
14007 uintptr_t daddr = (uintptr_t)dof;
14008 dtrace_ecbdesc_t *ep;
14009 dtrace_enabling_t *enab;
14012 ASSERT(MUTEX_HELD(&dtrace_lock));
14013 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t));
14016 * Check the DOF header identification bytes. In addition to checking
14017 * valid settings, we also verify that unused bits/bytes are zeroed so
14018 * we can use them later without fear of regressing existing binaries.
14020 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0],
14021 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) {
14022 dtrace_dof_error(dof, "DOF magic string mismatch");
14026 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 &&
14027 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) {
14028 dtrace_dof_error(dof, "DOF has invalid data model");
14032 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) {
14033 dtrace_dof_error(dof, "DOF encoding mismatch");
14037 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
14038 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) {
14039 dtrace_dof_error(dof, "DOF version mismatch");
14043 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) {
14044 dtrace_dof_error(dof, "DOF uses unsupported instruction set");
14048 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) {
14049 dtrace_dof_error(dof, "DOF uses too many integer registers");
14053 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) {
14054 dtrace_dof_error(dof, "DOF uses too many tuple registers");
14058 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) {
14059 if (dof->dofh_ident[i] != 0) {
14060 dtrace_dof_error(dof, "DOF has invalid ident byte set");
14065 if (dof->dofh_flags & ~DOF_FL_VALID) {
14066 dtrace_dof_error(dof, "DOF has invalid flag bits set");
14070 if (dof->dofh_secsize == 0) {
14071 dtrace_dof_error(dof, "zero section header size");
14076 * Check that the section headers don't exceed the amount of DOF
14077 * data. Note that we cast the section size and number of sections
14078 * to uint64_t's to prevent possible overflow in the multiplication.
14080 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize;
14082 if (dof->dofh_secoff > len || seclen > len ||
14083 dof->dofh_secoff + seclen > len) {
14084 dtrace_dof_error(dof, "truncated section headers");
14088 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) {
14089 dtrace_dof_error(dof, "misaligned section headers");
14093 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) {
14094 dtrace_dof_error(dof, "misaligned section size");
14099 * Take an initial pass through the section headers to be sure that
14100 * the headers don't have stray offsets. If the 'noprobes' flag is
14101 * set, do not permit sections relating to providers, probes, or args.
14103 for (i = 0; i < dof->dofh_secnum; i++) {
14104 dof_sec_t *sec = (dof_sec_t *)(daddr +
14105 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
14108 switch (sec->dofs_type) {
14109 case DOF_SECT_PROVIDER:
14110 case DOF_SECT_PROBES:
14111 case DOF_SECT_PRARGS:
14112 case DOF_SECT_PROFFS:
14113 dtrace_dof_error(dof, "illegal sections "
14119 if (DOF_SEC_ISLOADABLE(sec->dofs_type) &&
14120 !(sec->dofs_flags & DOF_SECF_LOAD)) {
14121 dtrace_dof_error(dof, "loadable section with load "
14126 if (!(sec->dofs_flags & DOF_SECF_LOAD))
14127 continue; /* just ignore non-loadable sections */
14129 if (!ISP2(sec->dofs_align)) {
14130 dtrace_dof_error(dof, "bad section alignment");
14134 if (sec->dofs_offset & (sec->dofs_align - 1)) {
14135 dtrace_dof_error(dof, "misaligned section");
14139 if (sec->dofs_offset > len || sec->dofs_size > len ||
14140 sec->dofs_offset + sec->dofs_size > len) {
14141 dtrace_dof_error(dof, "corrupt section header");
14145 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr +
14146 sec->dofs_offset + sec->dofs_size - 1) != '\0') {
14147 dtrace_dof_error(dof, "non-terminating string table");
14153 * Take a second pass through the sections and locate and perform any
14154 * relocations that are present. We do this after the first pass to
14155 * be sure that all sections have had their headers validated.
14157 for (i = 0; i < dof->dofh_secnum; i++) {
14158 dof_sec_t *sec = (dof_sec_t *)(daddr +
14159 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
14161 if (!(sec->dofs_flags & DOF_SECF_LOAD))
14162 continue; /* skip sections that are not loadable */
14164 switch (sec->dofs_type) {
14165 case DOF_SECT_URELHDR:
14166 if (dtrace_dof_relocate(dof, sec, ubase, udaddr) != 0)
14172 if ((enab = *enabp) == NULL)
14173 enab = *enabp = dtrace_enabling_create(vstate);
14175 for (i = 0; i < dof->dofh_secnum; i++) {
14176 dof_sec_t *sec = (dof_sec_t *)(daddr +
14177 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
14179 if (sec->dofs_type != DOF_SECT_ECBDESC)
14182 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) {
14183 dtrace_enabling_destroy(enab);
14188 dtrace_enabling_add(enab, ep);
14195 * Process DOF for any options. This routine assumes that the DOF has been
14196 * at least processed by dtrace_dof_slurp().
14199 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state)
14204 dof_optdesc_t *desc;
14206 for (i = 0; i < dof->dofh_secnum; i++) {
14207 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof +
14208 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
14210 if (sec->dofs_type != DOF_SECT_OPTDESC)
14213 if (sec->dofs_align != sizeof (uint64_t)) {
14214 dtrace_dof_error(dof, "bad alignment in "
14215 "option description");
14219 if ((entsize = sec->dofs_entsize) == 0) {
14220 dtrace_dof_error(dof, "zeroed option entry size");
14224 if (entsize < sizeof (dof_optdesc_t)) {
14225 dtrace_dof_error(dof, "bad option entry size");
14229 for (offs = 0; offs < sec->dofs_size; offs += entsize) {
14230 desc = (dof_optdesc_t *)((uintptr_t)dof +
14231 (uintptr_t)sec->dofs_offset + offs);
14233 if (desc->dofo_strtab != DOF_SECIDX_NONE) {
14234 dtrace_dof_error(dof, "non-zero option string");
14238 if (desc->dofo_value == DTRACEOPT_UNSET) {
14239 dtrace_dof_error(dof, "unset option");
14243 if ((rval = dtrace_state_option(state,
14244 desc->dofo_option, desc->dofo_value)) != 0) {
14245 dtrace_dof_error(dof, "rejected option");
14255 * DTrace Consumer State Functions
14258 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size)
14260 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize;
14263 dtrace_dynvar_t *dvar, *next, *start;
14266 ASSERT(MUTEX_HELD(&dtrace_lock));
14267 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL);
14269 bzero(dstate, sizeof (dtrace_dstate_t));
14271 if ((dstate->dtds_chunksize = chunksize) == 0)
14272 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE;
14274 VERIFY(dstate->dtds_chunksize < LONG_MAX);
14276 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)))
14279 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL)
14282 dstate->dtds_size = size;
14283 dstate->dtds_base = base;
14284 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP);
14285 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t));
14287 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t));
14289 if (hashsize != 1 && (hashsize & 1))
14292 dstate->dtds_hashsize = hashsize;
14293 dstate->dtds_hash = dstate->dtds_base;
14296 * Set all of our hash buckets to point to the single sink, and (if
14297 * it hasn't already been set), set the sink's hash value to be the
14298 * sink sentinel value. The sink is needed for dynamic variable
14299 * lookups to know that they have iterated over an entire, valid hash
14302 for (i = 0; i < hashsize; i++)
14303 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink;
14305 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK)
14306 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK;
14309 * Determine number of active CPUs. Divide free list evenly among
14312 start = (dtrace_dynvar_t *)
14313 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t));
14314 limit = (uintptr_t)base + size;
14316 VERIFY((uintptr_t)start < limit);
14317 VERIFY((uintptr_t)start >= (uintptr_t)base);
14319 maxper = (limit - (uintptr_t)start) / NCPU;
14320 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize;
14325 for (i = 0; i < NCPU; i++) {
14327 dstate->dtds_percpu[i].dtdsc_free = dvar = start;
14330 * If we don't even have enough chunks to make it once through
14331 * NCPUs, we're just going to allocate everything to the first
14332 * CPU. And if we're on the last CPU, we're going to allocate
14333 * whatever is left over. In either case, we set the limit to
14334 * be the limit of the dynamic variable space.
14336 if (maxper == 0 || i == NCPU - 1) {
14337 limit = (uintptr_t)base + size;
14340 limit = (uintptr_t)start + maxper;
14341 start = (dtrace_dynvar_t *)limit;
14344 VERIFY(limit <= (uintptr_t)base + size);
14347 next = (dtrace_dynvar_t *)((uintptr_t)dvar +
14348 dstate->dtds_chunksize);
14350 if ((uintptr_t)next + dstate->dtds_chunksize >= limit)
14353 VERIFY((uintptr_t)dvar >= (uintptr_t)base &&
14354 (uintptr_t)dvar <= (uintptr_t)base + size);
14355 dvar->dtdv_next = next;
14367 dtrace_dstate_fini(dtrace_dstate_t *dstate)
14369 ASSERT(MUTEX_HELD(&cpu_lock));
14371 if (dstate->dtds_base == NULL)
14374 kmem_free(dstate->dtds_base, dstate->dtds_size);
14375 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu);
14379 dtrace_vstate_fini(dtrace_vstate_t *vstate)
14382 * Logical XOR, where are you?
14384 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL));
14386 if (vstate->dtvs_nglobals > 0) {
14387 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals *
14388 sizeof (dtrace_statvar_t *));
14391 if (vstate->dtvs_ntlocals > 0) {
14392 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals *
14393 sizeof (dtrace_difv_t));
14396 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL));
14398 if (vstate->dtvs_nlocals > 0) {
14399 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals *
14400 sizeof (dtrace_statvar_t *));
14406 dtrace_state_clean(dtrace_state_t *state)
14408 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
14411 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
14412 dtrace_speculation_clean(state);
14416 dtrace_state_deadman(dtrace_state_t *state)
14422 now = dtrace_gethrtime();
14424 if (state != dtrace_anon.dta_state &&
14425 now - state->dts_laststatus >= dtrace_deadman_user)
14429 * We must be sure that dts_alive never appears to be less than the
14430 * value upon entry to dtrace_state_deadman(), and because we lack a
14431 * dtrace_cas64(), we cannot store to it atomically. We thus instead
14432 * store INT64_MAX to it, followed by a memory barrier, followed by
14433 * the new value. This assures that dts_alive never appears to be
14434 * less than its true value, regardless of the order in which the
14435 * stores to the underlying storage are issued.
14437 state->dts_alive = INT64_MAX;
14438 dtrace_membar_producer();
14439 state->dts_alive = now;
14441 #else /* !illumos */
14443 dtrace_state_clean(void *arg)
14445 dtrace_state_t *state = arg;
14446 dtrace_optval_t *opt = state->dts_options;
14448 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
14451 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
14452 dtrace_speculation_clean(state);
14454 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC,
14455 dtrace_state_clean, state);
14459 dtrace_state_deadman(void *arg)
14461 dtrace_state_t *state = arg;
14466 dtrace_debug_output();
14468 now = dtrace_gethrtime();
14470 if (state != dtrace_anon.dta_state &&
14471 now - state->dts_laststatus >= dtrace_deadman_user)
14475 * We must be sure that dts_alive never appears to be less than the
14476 * value upon entry to dtrace_state_deadman(), and because we lack a
14477 * dtrace_cas64(), we cannot store to it atomically. We thus instead
14478 * store INT64_MAX to it, followed by a memory barrier, followed by
14479 * the new value. This assures that dts_alive never appears to be
14480 * less than its true value, regardless of the order in which the
14481 * stores to the underlying storage are issued.
14483 state->dts_alive = INT64_MAX;
14484 dtrace_membar_producer();
14485 state->dts_alive = now;
14487 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC,
14488 dtrace_state_deadman, state);
14490 #endif /* illumos */
14492 static dtrace_state_t *
14494 dtrace_state_create(dev_t *devp, cred_t *cr)
14496 dtrace_state_create(struct cdev *dev, struct ucred *cred __unused)
14507 dtrace_state_t *state;
14508 dtrace_optval_t *opt;
14509 int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
14512 ASSERT(MUTEX_HELD(&dtrace_lock));
14513 ASSERT(MUTEX_HELD(&cpu_lock));
14516 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1,
14517 VM_BESTFIT | VM_SLEEP);
14519 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) {
14520 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
14524 state = ddi_get_soft_state(dtrace_softstate, minor);
14531 /* Allocate memory for the state. */
14532 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP);
14535 state->dts_epid = DTRACE_EPIDNONE + 1;
14537 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m);
14539 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1,
14540 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
14542 if (devp != NULL) {
14543 major = getemajor(*devp);
14545 major = ddi_driver_major(dtrace_devi);
14548 state->dts_dev = makedevice(major, minor);
14551 *devp = state->dts_dev;
14553 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx);
14554 state->dts_dev = dev;
14558 * We allocate NCPU buffers. On the one hand, this can be quite
14559 * a bit of memory per instance (nearly 36K on a Starcat). On the
14560 * other hand, it saves an additional memory reference in the probe
14563 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
14564 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
14567 * Allocate and initialise the per-process per-CPU random state.
14568 * SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is
14569 * assumed to be seeded at this point (if from Fortuna seed file).
14571 (void) read_random(&state->dts_rstate[0], 2 * sizeof(uint64_t));
14572 for (cpu_it = 1; cpu_it < NCPU; cpu_it++) {
14574 * Each CPU is assigned a 2^64 period, non-overlapping
14577 dtrace_xoroshiro128_plus_jump(state->dts_rstate[cpu_it-1],
14578 state->dts_rstate[cpu_it]);
14582 state->dts_cleaner = CYCLIC_NONE;
14583 state->dts_deadman = CYCLIC_NONE;
14585 callout_init(&state->dts_cleaner, 1);
14586 callout_init(&state->dts_deadman, 1);
14588 state->dts_vstate.dtvs_state = state;
14590 for (i = 0; i < DTRACEOPT_MAX; i++)
14591 state->dts_options[i] = DTRACEOPT_UNSET;
14594 * Set the default options.
14596 opt = state->dts_options;
14597 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH;
14598 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO;
14599 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default;
14600 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default;
14601 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL;
14602 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default;
14603 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default;
14604 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default;
14605 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default;
14606 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default;
14607 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default;
14608 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default;
14609 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default;
14610 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default;
14612 state->dts_activity = DTRACE_ACTIVITY_INACTIVE;
14615 * Depending on the user credentials, we set flag bits which alter probe
14616 * visibility or the amount of destructiveness allowed. In the case of
14617 * actual anonymous tracing, or the possession of all privileges, all of
14618 * the normal checks are bypassed.
14620 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
14621 state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
14622 state->dts_cred.dcr_action = DTRACE_CRA_ALL;
14625 * Set up the credentials for this instantiation. We take a
14626 * hold on the credential to prevent it from disappearing on
14627 * us; this in turn prevents the zone_t referenced by this
14628 * credential from disappearing. This means that we can
14629 * examine the credential and the zone from probe context.
14632 state->dts_cred.dcr_cred = cr;
14635 * CRA_PROC means "we have *some* privilege for dtrace" and
14636 * unlocks the use of variables like pid, zonename, etc.
14638 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) ||
14639 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
14640 state->dts_cred.dcr_action |= DTRACE_CRA_PROC;
14644 * dtrace_user allows use of syscall and profile providers.
14645 * If the user also has proc_owner and/or proc_zone, we
14646 * extend the scope to include additional visibility and
14647 * destructive power.
14649 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) {
14650 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) {
14651 state->dts_cred.dcr_visible |=
14652 DTRACE_CRV_ALLPROC;
14654 state->dts_cred.dcr_action |=
14655 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14658 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) {
14659 state->dts_cred.dcr_visible |=
14660 DTRACE_CRV_ALLZONE;
14662 state->dts_cred.dcr_action |=
14663 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14667 * If we have all privs in whatever zone this is,
14668 * we can do destructive things to processes which
14669 * have altered credentials.
14672 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
14673 cr->cr_zone->zone_privset)) {
14674 state->dts_cred.dcr_action |=
14675 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
14681 * Holding the dtrace_kernel privilege also implies that
14682 * the user has the dtrace_user privilege from a visibility
14683 * perspective. But without further privileges, some
14684 * destructive actions are not available.
14686 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) {
14688 * Make all probes in all zones visible. However,
14689 * this doesn't mean that all actions become available
14692 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL |
14693 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE;
14695 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL |
14698 * Holding proc_owner means that destructive actions
14699 * for *this* zone are allowed.
14701 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
14702 state->dts_cred.dcr_action |=
14703 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14706 * Holding proc_zone means that destructive actions
14707 * for this user/group ID in all zones is allowed.
14709 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
14710 state->dts_cred.dcr_action |=
14711 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14715 * If we have all privs in whatever zone this is,
14716 * we can do destructive things to processes which
14717 * have altered credentials.
14719 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
14720 cr->cr_zone->zone_privset)) {
14721 state->dts_cred.dcr_action |=
14722 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
14728 * Holding the dtrace_proc privilege gives control over fasttrap
14729 * and pid providers. We need to grant wider destructive
14730 * privileges in the event that the user has proc_owner and/or
14733 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
14734 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
14735 state->dts_cred.dcr_action |=
14736 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14738 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
14739 state->dts_cred.dcr_action |=
14740 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14748 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which)
14750 dtrace_optval_t *opt = state->dts_options, size;
14751 processorid_t cpu = 0;;
14752 int flags = 0, rval, factor, divisor = 1;
14754 ASSERT(MUTEX_HELD(&dtrace_lock));
14755 ASSERT(MUTEX_HELD(&cpu_lock));
14756 ASSERT(which < DTRACEOPT_MAX);
14757 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE ||
14758 (state == dtrace_anon.dta_state &&
14759 state->dts_activity == DTRACE_ACTIVITY_ACTIVE));
14761 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0)
14764 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
14765 cpu = opt[DTRACEOPT_CPU];
14767 if (which == DTRACEOPT_SPECSIZE)
14768 flags |= DTRACEBUF_NOSWITCH;
14770 if (which == DTRACEOPT_BUFSIZE) {
14771 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING)
14772 flags |= DTRACEBUF_RING;
14774 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL)
14775 flags |= DTRACEBUF_FILL;
14777 if (state != dtrace_anon.dta_state ||
14778 state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
14779 flags |= DTRACEBUF_INACTIVE;
14782 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) {
14784 * The size must be 8-byte aligned. If the size is not 8-byte
14785 * aligned, drop it down by the difference.
14787 if (size & (sizeof (uint64_t) - 1))
14788 size -= size & (sizeof (uint64_t) - 1);
14790 if (size < state->dts_reserve) {
14792 * Buffers always must be large enough to accommodate
14793 * their prereserved space. We return E2BIG instead
14794 * of ENOMEM in this case to allow for user-level
14795 * software to differentiate the cases.
14800 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor);
14802 if (rval != ENOMEM) {
14807 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
14810 for (divisor = 2; divisor < factor; divisor <<= 1)
14818 dtrace_state_buffers(dtrace_state_t *state)
14820 dtrace_speculation_t *spec = state->dts_speculations;
14823 if ((rval = dtrace_state_buffer(state, state->dts_buffer,
14824 DTRACEOPT_BUFSIZE)) != 0)
14827 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer,
14828 DTRACEOPT_AGGSIZE)) != 0)
14831 for (i = 0; i < state->dts_nspeculations; i++) {
14832 if ((rval = dtrace_state_buffer(state,
14833 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0)
14841 dtrace_state_prereserve(dtrace_state_t *state)
14844 dtrace_probe_t *probe;
14846 state->dts_reserve = 0;
14848 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
14852 * If our buffer policy is a "fill" buffer policy, we need to set the
14853 * prereserved space to be the space required by the END probes.
14855 probe = dtrace_probes[dtrace_probeid_end - 1];
14856 ASSERT(probe != NULL);
14858 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
14859 if (ecb->dte_state != state)
14862 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment;
14867 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu)
14869 dtrace_optval_t *opt = state->dts_options, sz, nspec;
14870 dtrace_speculation_t *spec;
14871 dtrace_buffer_t *buf;
14873 cyc_handler_t hdlr;
14876 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t);
14877 dtrace_icookie_t cookie;
14879 mutex_enter(&cpu_lock);
14880 mutex_enter(&dtrace_lock);
14882 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
14888 * Before we can perform any checks, we must prime all of the
14889 * retained enablings that correspond to this state.
14891 dtrace_enabling_prime(state);
14893 if (state->dts_destructive && !state->dts_cred.dcr_destructive) {
14898 dtrace_state_prereserve(state);
14901 * Now we want to do is try to allocate our speculations.
14902 * We do not automatically resize the number of speculations; if
14903 * this fails, we will fail the operation.
14905 nspec = opt[DTRACEOPT_NSPEC];
14906 ASSERT(nspec != DTRACEOPT_UNSET);
14908 if (nspec > INT_MAX) {
14913 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t),
14914 KM_NOSLEEP | KM_NORMALPRI);
14916 if (spec == NULL) {
14921 state->dts_speculations = spec;
14922 state->dts_nspeculations = (int)nspec;
14924 for (i = 0; i < nspec; i++) {
14925 if ((buf = kmem_zalloc(bufsize,
14926 KM_NOSLEEP | KM_NORMALPRI)) == NULL) {
14931 spec[i].dtsp_buffer = buf;
14934 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) {
14935 if (dtrace_anon.dta_state == NULL) {
14940 if (state->dts_necbs != 0) {
14945 state->dts_anon = dtrace_anon_grab();
14946 ASSERT(state->dts_anon != NULL);
14947 state = state->dts_anon;
14950 * We want "grabanon" to be set in the grabbed state, so we'll
14951 * copy that option value from the grabbing state into the
14954 state->dts_options[DTRACEOPT_GRABANON] =
14955 opt[DTRACEOPT_GRABANON];
14957 *cpu = dtrace_anon.dta_beganon;
14960 * If the anonymous state is active (as it almost certainly
14961 * is if the anonymous enabling ultimately matched anything),
14962 * we don't allow any further option processing -- but we
14963 * don't return failure.
14965 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
14969 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET &&
14970 opt[DTRACEOPT_AGGSIZE] != 0) {
14971 if (state->dts_aggregations == NULL) {
14973 * We're not going to create an aggregation buffer
14974 * because we don't have any ECBs that contain
14975 * aggregations -- set this option to 0.
14977 opt[DTRACEOPT_AGGSIZE] = 0;
14980 * If we have an aggregation buffer, we must also have
14981 * a buffer to use as scratch.
14983 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET ||
14984 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) {
14985 opt[DTRACEOPT_BUFSIZE] = state->dts_needed;
14990 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET &&
14991 opt[DTRACEOPT_SPECSIZE] != 0) {
14992 if (!state->dts_speculates) {
14994 * We're not going to create speculation buffers
14995 * because we don't have any ECBs that actually
14996 * speculate -- set the speculation size to 0.
14998 opt[DTRACEOPT_SPECSIZE] = 0;
15003 * The bare minimum size for any buffer that we're actually going to
15004 * do anything to is sizeof (uint64_t).
15006 sz = sizeof (uint64_t);
15008 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) ||
15009 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) ||
15010 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) {
15012 * A buffer size has been explicitly set to 0 (or to a size
15013 * that will be adjusted to 0) and we need the space -- we
15014 * need to return failure. We return ENOSPC to differentiate
15015 * it from failing to allocate a buffer due to failure to meet
15016 * the reserve (for which we return E2BIG).
15022 if ((rval = dtrace_state_buffers(state)) != 0)
15025 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET)
15026 sz = dtrace_dstate_defsize;
15029 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz);
15034 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
15036 } while (sz >>= 1);
15038 opt[DTRACEOPT_DYNVARSIZE] = sz;
15043 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max)
15044 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max;
15046 if (opt[DTRACEOPT_CLEANRATE] == 0)
15047 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
15049 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min)
15050 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min;
15052 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max)
15053 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
15055 state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
15057 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean;
15058 hdlr.cyh_arg = state;
15059 hdlr.cyh_level = CY_LOW_LEVEL;
15062 when.cyt_interval = opt[DTRACEOPT_CLEANRATE];
15064 state->dts_cleaner = cyclic_add(&hdlr, &when);
15066 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman;
15067 hdlr.cyh_arg = state;
15068 hdlr.cyh_level = CY_LOW_LEVEL;
15071 when.cyt_interval = dtrace_deadman_interval;
15073 state->dts_deadman = cyclic_add(&hdlr, &when);
15075 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC,
15076 dtrace_state_clean, state);
15077 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC,
15078 dtrace_state_deadman, state);
15081 state->dts_activity = DTRACE_ACTIVITY_WARMUP;
15084 if (state->dts_getf != 0 &&
15085 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
15087 * We don't have kernel privs but we have at least one call
15088 * to getf(); we need to bump our zone's count, and (if
15089 * this is the first enabling to have an unprivileged call
15090 * to getf()) we need to hook into closef().
15092 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++;
15094 if (dtrace_getf++ == 0) {
15095 ASSERT(dtrace_closef == NULL);
15096 dtrace_closef = dtrace_getf_barrier;
15102 * Now it's time to actually fire the BEGIN probe. We need to disable
15103 * interrupts here both to record the CPU on which we fired the BEGIN
15104 * probe (the data from this CPU will be processed first at user
15105 * level) and to manually activate the buffer for this CPU.
15107 cookie = dtrace_interrupt_disable();
15109 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE);
15110 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
15112 dtrace_probe(dtrace_probeid_begin,
15113 (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
15114 dtrace_interrupt_enable(cookie);
15116 * We may have had an exit action from a BEGIN probe; only change our
15117 * state to ACTIVE if we're still in WARMUP.
15119 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP ||
15120 state->dts_activity == DTRACE_ACTIVITY_DRAINING);
15122 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP)
15123 state->dts_activity = DTRACE_ACTIVITY_ACTIVE;
15127 * We enable anonymous tracing before APs are started, so we must
15128 * activate buffers using the current CPU.
15130 if (state == dtrace_anon.dta_state)
15131 for (int i = 0; i < NCPU; i++)
15132 dtrace_buffer_activate_cpu(state, i);
15134 dtrace_xcall(DTRACE_CPUALL,
15135 (dtrace_xcall_t)dtrace_buffer_activate, state);
15138 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
15139 * want each CPU to transition its principal buffer out of the
15140 * INACTIVE state. Doing this assures that no CPU will suddenly begin
15141 * processing an ECB halfway down a probe's ECB chain; all CPUs will
15142 * atomically transition from processing none of a state's ECBs to
15143 * processing all of them.
15145 dtrace_xcall(DTRACE_CPUALL,
15146 (dtrace_xcall_t)dtrace_buffer_activate, state);
15151 dtrace_buffer_free(state->dts_buffer);
15152 dtrace_buffer_free(state->dts_aggbuffer);
15154 if ((nspec = state->dts_nspeculations) == 0) {
15155 ASSERT(state->dts_speculations == NULL);
15159 spec = state->dts_speculations;
15160 ASSERT(spec != NULL);
15162 for (i = 0; i < state->dts_nspeculations; i++) {
15163 if ((buf = spec[i].dtsp_buffer) == NULL)
15166 dtrace_buffer_free(buf);
15167 kmem_free(buf, bufsize);
15170 kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
15171 state->dts_nspeculations = 0;
15172 state->dts_speculations = NULL;
15175 mutex_exit(&dtrace_lock);
15176 mutex_exit(&cpu_lock);
15182 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu)
15184 dtrace_icookie_t cookie;
15186 ASSERT(MUTEX_HELD(&dtrace_lock));
15188 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE &&
15189 state->dts_activity != DTRACE_ACTIVITY_DRAINING)
15193 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
15194 * to be sure that every CPU has seen it. See below for the details
15195 * on why this is done.
15197 state->dts_activity = DTRACE_ACTIVITY_DRAINING;
15201 * By this point, it is impossible for any CPU to be still processing
15202 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to
15203 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
15204 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe()
15205 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
15206 * iff we're in the END probe.
15208 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN;
15210 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN);
15213 * Finally, we can release the reserve and call the END probe. We
15214 * disable interrupts across calling the END probe to allow us to
15215 * return the CPU on which we actually called the END probe. This
15216 * allows user-land to be sure that this CPU's principal buffer is
15219 state->dts_reserve = 0;
15221 cookie = dtrace_interrupt_disable();
15223 dtrace_probe(dtrace_probeid_end,
15224 (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
15225 dtrace_interrupt_enable(cookie);
15227 state->dts_activity = DTRACE_ACTIVITY_STOPPED;
15231 if (state->dts_getf != 0 &&
15232 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
15234 * We don't have kernel privs but we have at least one call
15235 * to getf(); we need to lower our zone's count, and (if
15236 * this is the last enabling to have an unprivileged call
15237 * to getf()) we need to clear the closef() hook.
15239 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0);
15240 ASSERT(dtrace_closef == dtrace_getf_barrier);
15241 ASSERT(dtrace_getf > 0);
15243 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--;
15245 if (--dtrace_getf == 0)
15246 dtrace_closef = NULL;
15254 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option,
15255 dtrace_optval_t val)
15257 ASSERT(MUTEX_HELD(&dtrace_lock));
15259 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
15262 if (option >= DTRACEOPT_MAX)
15265 if (option != DTRACEOPT_CPU && val < 0)
15269 case DTRACEOPT_DESTRUCTIVE:
15270 if (dtrace_destructive_disallow)
15273 state->dts_cred.dcr_destructive = 1;
15276 case DTRACEOPT_BUFSIZE:
15277 case DTRACEOPT_DYNVARSIZE:
15278 case DTRACEOPT_AGGSIZE:
15279 case DTRACEOPT_SPECSIZE:
15280 case DTRACEOPT_STRSIZE:
15284 if (val >= LONG_MAX) {
15286 * If this is an otherwise negative value, set it to
15287 * the highest multiple of 128m less than LONG_MAX.
15288 * Technically, we're adjusting the size without
15289 * regard to the buffer resizing policy, but in fact,
15290 * this has no effect -- if we set the buffer size to
15291 * ~LONG_MAX and the buffer policy is ultimately set to
15292 * be "manual", the buffer allocation is guaranteed to
15293 * fail, if only because the allocation requires two
15294 * buffers. (We set the the size to the highest
15295 * multiple of 128m because it ensures that the size
15296 * will remain a multiple of a megabyte when
15297 * repeatedly halved -- all the way down to 15m.)
15299 val = LONG_MAX - (1 << 27) + 1;
15303 state->dts_options[option] = val;
15309 dtrace_state_destroy(dtrace_state_t *state)
15312 dtrace_vstate_t *vstate = &state->dts_vstate;
15314 minor_t minor = getminor(state->dts_dev);
15316 int i, bufsize = NCPU * sizeof (dtrace_buffer_t);
15317 dtrace_speculation_t *spec = state->dts_speculations;
15318 int nspec = state->dts_nspeculations;
15321 ASSERT(MUTEX_HELD(&dtrace_lock));
15322 ASSERT(MUTEX_HELD(&cpu_lock));
15325 * First, retract any retained enablings for this state.
15327 dtrace_enabling_retract(state);
15328 ASSERT(state->dts_nretained == 0);
15330 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE ||
15331 state->dts_activity == DTRACE_ACTIVITY_DRAINING) {
15333 * We have managed to come into dtrace_state_destroy() on a
15334 * hot enabling -- almost certainly because of a disorderly
15335 * shutdown of a consumer. (That is, a consumer that is
15336 * exiting without having called dtrace_stop().) In this case,
15337 * we're going to set our activity to be KILLED, and then
15338 * issue a sync to be sure that everyone is out of probe
15339 * context before we start blowing away ECBs.
15341 state->dts_activity = DTRACE_ACTIVITY_KILLED;
15346 * Release the credential hold we took in dtrace_state_create().
15348 if (state->dts_cred.dcr_cred != NULL)
15349 crfree(state->dts_cred.dcr_cred);
15352 * Now we can safely disable and destroy any enabled probes. Because
15353 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
15354 * (especially if they're all enabled), we take two passes through the
15355 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and
15356 * in the second we disable whatever is left over.
15358 for (match = DTRACE_PRIV_KERNEL; ; match = 0) {
15359 for (i = 0; i < state->dts_necbs; i++) {
15360 if ((ecb = state->dts_ecbs[i]) == NULL)
15363 if (match && ecb->dte_probe != NULL) {
15364 dtrace_probe_t *probe = ecb->dte_probe;
15365 dtrace_provider_t *prov = probe->dtpr_provider;
15367 if (!(prov->dtpv_priv.dtpp_flags & match))
15371 dtrace_ecb_disable(ecb);
15372 dtrace_ecb_destroy(ecb);
15380 * Before we free the buffers, perform one more sync to assure that
15381 * every CPU is out of probe context.
15385 dtrace_buffer_free(state->dts_buffer);
15386 dtrace_buffer_free(state->dts_aggbuffer);
15388 for (i = 0; i < nspec; i++)
15389 dtrace_buffer_free(spec[i].dtsp_buffer);
15392 if (state->dts_cleaner != CYCLIC_NONE)
15393 cyclic_remove(state->dts_cleaner);
15395 if (state->dts_deadman != CYCLIC_NONE)
15396 cyclic_remove(state->dts_deadman);
15398 callout_stop(&state->dts_cleaner);
15399 callout_drain(&state->dts_cleaner);
15400 callout_stop(&state->dts_deadman);
15401 callout_drain(&state->dts_deadman);
15404 dtrace_dstate_fini(&vstate->dtvs_dynvars);
15405 dtrace_vstate_fini(vstate);
15406 if (state->dts_ecbs != NULL)
15407 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *));
15409 if (state->dts_aggregations != NULL) {
15411 for (i = 0; i < state->dts_naggregations; i++)
15412 ASSERT(state->dts_aggregations[i] == NULL);
15414 ASSERT(state->dts_naggregations > 0);
15415 kmem_free(state->dts_aggregations,
15416 state->dts_naggregations * sizeof (dtrace_aggregation_t *));
15419 kmem_free(state->dts_buffer, bufsize);
15420 kmem_free(state->dts_aggbuffer, bufsize);
15422 for (i = 0; i < nspec; i++)
15423 kmem_free(spec[i].dtsp_buffer, bufsize);
15426 kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
15428 dtrace_format_destroy(state);
15430 if (state->dts_aggid_arena != NULL) {
15432 vmem_destroy(state->dts_aggid_arena);
15434 delete_unrhdr(state->dts_aggid_arena);
15436 state->dts_aggid_arena = NULL;
15439 ddi_soft_state_free(dtrace_softstate, minor);
15440 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
15445 * DTrace Anonymous Enabling Functions
15447 static dtrace_state_t *
15448 dtrace_anon_grab(void)
15450 dtrace_state_t *state;
15452 ASSERT(MUTEX_HELD(&dtrace_lock));
15454 if ((state = dtrace_anon.dta_state) == NULL) {
15455 ASSERT(dtrace_anon.dta_enabling == NULL);
15459 ASSERT(dtrace_anon.dta_enabling != NULL);
15460 ASSERT(dtrace_retained != NULL);
15462 dtrace_enabling_destroy(dtrace_anon.dta_enabling);
15463 dtrace_anon.dta_enabling = NULL;
15464 dtrace_anon.dta_state = NULL;
15470 dtrace_anon_property(void)
15473 dtrace_state_t *state;
15475 char c[32]; /* enough for "dof-data-" + digits */
15477 ASSERT(MUTEX_HELD(&dtrace_lock));
15478 ASSERT(MUTEX_HELD(&cpu_lock));
15480 for (i = 0; ; i++) {
15481 (void) snprintf(c, sizeof (c), "dof-data-%d", i);
15483 dtrace_err_verbose = 1;
15485 if ((dof = dtrace_dof_property(c)) == NULL) {
15486 dtrace_err_verbose = 0;
15492 * We want to create anonymous state, so we need to transition
15493 * the kernel debugger to indicate that DTrace is active. If
15494 * this fails (e.g. because the debugger has modified text in
15495 * some way), we won't continue with the processing.
15497 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
15498 cmn_err(CE_NOTE, "kernel debugger active; anonymous "
15499 "enabling ignored.");
15500 dtrace_dof_destroy(dof);
15506 * If we haven't allocated an anonymous state, we'll do so now.
15508 if ((state = dtrace_anon.dta_state) == NULL) {
15509 state = dtrace_state_create(NULL, NULL);
15510 dtrace_anon.dta_state = state;
15512 if (state == NULL) {
15514 * This basically shouldn't happen: the only
15515 * failure mode from dtrace_state_create() is a
15516 * failure of ddi_soft_state_zalloc() that
15517 * itself should never happen. Still, the
15518 * interface allows for a failure mode, and
15519 * we want to fail as gracefully as possible:
15520 * we'll emit an error message and cease
15521 * processing anonymous state in this case.
15523 cmn_err(CE_WARN, "failed to create "
15524 "anonymous state");
15525 dtrace_dof_destroy(dof);
15530 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(),
15531 &dtrace_anon.dta_enabling, 0, 0, B_TRUE);
15534 rv = dtrace_dof_options(dof, state);
15536 dtrace_err_verbose = 0;
15537 dtrace_dof_destroy(dof);
15541 * This is malformed DOF; chuck any anonymous state
15544 ASSERT(dtrace_anon.dta_enabling == NULL);
15545 dtrace_state_destroy(state);
15546 dtrace_anon.dta_state = NULL;
15550 ASSERT(dtrace_anon.dta_enabling != NULL);
15553 if (dtrace_anon.dta_enabling != NULL) {
15557 * dtrace_enabling_retain() can only fail because we are
15558 * trying to retain more enablings than are allowed -- but
15559 * we only have one anonymous enabling, and we are guaranteed
15560 * to be allowed at least one retained enabling; we assert
15561 * that dtrace_enabling_retain() returns success.
15563 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling);
15566 dtrace_enabling_dump(dtrace_anon.dta_enabling);
15571 * DTrace Helper Functions
15574 dtrace_helper_trace(dtrace_helper_action_t *helper,
15575 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where)
15577 uint32_t size, next, nnext, i;
15578 dtrace_helptrace_t *ent, *buffer;
15579 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags;
15581 if ((buffer = dtrace_helptrace_buffer) == NULL)
15584 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals);
15587 * What would a tracing framework be without its own tracing
15588 * framework? (Well, a hell of a lot simpler, for starters...)
15590 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals *
15591 sizeof (uint64_t) - sizeof (uint64_t);
15594 * Iterate until we can allocate a slot in the trace buffer.
15597 next = dtrace_helptrace_next;
15599 if (next + size < dtrace_helptrace_bufsize) {
15600 nnext = next + size;
15604 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next);
15607 * We have our slot; fill it in.
15609 if (nnext == size) {
15610 dtrace_helptrace_wrapped++;
15614 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next);
15615 ent->dtht_helper = helper;
15616 ent->dtht_where = where;
15617 ent->dtht_nlocals = vstate->dtvs_nlocals;
15619 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ?
15620 mstate->dtms_fltoffs : -1;
15621 ent->dtht_fault = DTRACE_FLAGS2FLT(flags);
15622 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval;
15624 for (i = 0; i < vstate->dtvs_nlocals; i++) {
15625 dtrace_statvar_t *svar;
15627 if ((svar = vstate->dtvs_locals[i]) == NULL)
15630 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t));
15631 ent->dtht_locals[i] =
15632 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu];
15637 dtrace_helper(int which, dtrace_mstate_t *mstate,
15638 dtrace_state_t *state, uint64_t arg0, uint64_t arg1)
15640 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
15641 uint64_t sarg0 = mstate->dtms_arg[0];
15642 uint64_t sarg1 = mstate->dtms_arg[1];
15644 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers;
15645 dtrace_helper_action_t *helper;
15646 dtrace_vstate_t *vstate;
15647 dtrace_difo_t *pred;
15648 int i, trace = dtrace_helptrace_buffer != NULL;
15650 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS);
15652 if (helpers == NULL)
15655 if ((helper = helpers->dthps_actions[which]) == NULL)
15658 vstate = &helpers->dthps_vstate;
15659 mstate->dtms_arg[0] = arg0;
15660 mstate->dtms_arg[1] = arg1;
15663 * Now iterate over each helper. If its predicate evaluates to 'true',
15664 * we'll call the corresponding actions. Note that the below calls
15665 * to dtrace_dif_emulate() may set faults in machine state. This is
15666 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow
15667 * the stored DIF offset with its own (which is the desired behavior).
15668 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
15669 * from machine state; this is okay, too.
15671 for (; helper != NULL; helper = helper->dtha_next) {
15672 if ((pred = helper->dtha_predicate) != NULL) {
15674 dtrace_helper_trace(helper, mstate, vstate, 0);
15676 if (!dtrace_dif_emulate(pred, mstate, vstate, state))
15679 if (*flags & CPU_DTRACE_FAULT)
15683 for (i = 0; i < helper->dtha_nactions; i++) {
15685 dtrace_helper_trace(helper,
15686 mstate, vstate, i + 1);
15688 rval = dtrace_dif_emulate(helper->dtha_actions[i],
15689 mstate, vstate, state);
15691 if (*flags & CPU_DTRACE_FAULT)
15697 dtrace_helper_trace(helper, mstate, vstate,
15698 DTRACE_HELPTRACE_NEXT);
15702 dtrace_helper_trace(helper, mstate, vstate,
15703 DTRACE_HELPTRACE_DONE);
15706 * Restore the arg0 that we saved upon entry.
15708 mstate->dtms_arg[0] = sarg0;
15709 mstate->dtms_arg[1] = sarg1;
15715 dtrace_helper_trace(helper, mstate, vstate,
15716 DTRACE_HELPTRACE_ERR);
15719 * Restore the arg0 that we saved upon entry.
15721 mstate->dtms_arg[0] = sarg0;
15722 mstate->dtms_arg[1] = sarg1;
15728 dtrace_helper_action_destroy(dtrace_helper_action_t *helper,
15729 dtrace_vstate_t *vstate)
15733 if (helper->dtha_predicate != NULL)
15734 dtrace_difo_release(helper->dtha_predicate, vstate);
15736 for (i = 0; i < helper->dtha_nactions; i++) {
15737 ASSERT(helper->dtha_actions[i] != NULL);
15738 dtrace_difo_release(helper->dtha_actions[i], vstate);
15741 kmem_free(helper->dtha_actions,
15742 helper->dtha_nactions * sizeof (dtrace_difo_t *));
15743 kmem_free(helper, sizeof (dtrace_helper_action_t));
15747 dtrace_helper_destroygen(dtrace_helpers_t *help, int gen)
15749 proc_t *p = curproc;
15750 dtrace_vstate_t *vstate;
15754 help = p->p_dtrace_helpers;
15756 ASSERT(MUTEX_HELD(&dtrace_lock));
15758 if (help == NULL || gen > help->dthps_generation)
15761 vstate = &help->dthps_vstate;
15763 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
15764 dtrace_helper_action_t *last = NULL, *h, *next;
15766 for (h = help->dthps_actions[i]; h != NULL; h = next) {
15767 next = h->dtha_next;
15769 if (h->dtha_generation == gen) {
15770 if (last != NULL) {
15771 last->dtha_next = next;
15773 help->dthps_actions[i] = next;
15776 dtrace_helper_action_destroy(h, vstate);
15784 * Interate until we've cleared out all helper providers with the
15785 * given generation number.
15788 dtrace_helper_provider_t *prov;
15791 * Look for a helper provider with the right generation. We
15792 * have to start back at the beginning of the list each time
15793 * because we drop dtrace_lock. It's unlikely that we'll make
15794 * more than two passes.
15796 for (i = 0; i < help->dthps_nprovs; i++) {
15797 prov = help->dthps_provs[i];
15799 if (prov->dthp_generation == gen)
15804 * If there were no matches, we're done.
15806 if (i == help->dthps_nprovs)
15810 * Move the last helper provider into this slot.
15812 help->dthps_nprovs--;
15813 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs];
15814 help->dthps_provs[help->dthps_nprovs] = NULL;
15816 mutex_exit(&dtrace_lock);
15819 * If we have a meta provider, remove this helper provider.
15821 mutex_enter(&dtrace_meta_lock);
15822 if (dtrace_meta_pid != NULL) {
15823 ASSERT(dtrace_deferred_pid == NULL);
15824 dtrace_helper_provider_remove(&prov->dthp_prov,
15827 mutex_exit(&dtrace_meta_lock);
15829 dtrace_helper_provider_destroy(prov);
15831 mutex_enter(&dtrace_lock);
15838 dtrace_helper_validate(dtrace_helper_action_t *helper)
15843 if ((dp = helper->dtha_predicate) != NULL)
15844 err += dtrace_difo_validate_helper(dp);
15846 for (i = 0; i < helper->dtha_nactions; i++)
15847 err += dtrace_difo_validate_helper(helper->dtha_actions[i]);
15853 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep,
15854 dtrace_helpers_t *help)
15856 dtrace_helper_action_t *helper, *last;
15857 dtrace_actdesc_t *act;
15858 dtrace_vstate_t *vstate;
15859 dtrace_predicate_t *pred;
15860 int count = 0, nactions = 0, i;
15862 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS)
15865 last = help->dthps_actions[which];
15866 vstate = &help->dthps_vstate;
15868 for (count = 0; last != NULL; last = last->dtha_next) {
15870 if (last->dtha_next == NULL)
15875 * If we already have dtrace_helper_actions_max helper actions for this
15876 * helper action type, we'll refuse to add a new one.
15878 if (count >= dtrace_helper_actions_max)
15881 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP);
15882 helper->dtha_generation = help->dthps_generation;
15884 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) {
15885 ASSERT(pred->dtp_difo != NULL);
15886 dtrace_difo_hold(pred->dtp_difo);
15887 helper->dtha_predicate = pred->dtp_difo;
15890 for (act = ep->dted_action; act != NULL; act = act->dtad_next) {
15891 if (act->dtad_kind != DTRACEACT_DIFEXPR)
15894 if (act->dtad_difo == NULL)
15900 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) *
15901 (helper->dtha_nactions = nactions), KM_SLEEP);
15903 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) {
15904 dtrace_difo_hold(act->dtad_difo);
15905 helper->dtha_actions[i++] = act->dtad_difo;
15908 if (!dtrace_helper_validate(helper))
15911 if (last == NULL) {
15912 help->dthps_actions[which] = helper;
15914 last->dtha_next = helper;
15917 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) {
15918 dtrace_helptrace_nlocals = vstate->dtvs_nlocals;
15919 dtrace_helptrace_next = 0;
15924 dtrace_helper_action_destroy(helper, vstate);
15929 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help,
15930 dof_helper_t *dofhp)
15932 ASSERT(MUTEX_NOT_HELD(&dtrace_lock));
15934 mutex_enter(&dtrace_meta_lock);
15935 mutex_enter(&dtrace_lock);
15937 if (!dtrace_attached() || dtrace_meta_pid == NULL) {
15939 * If the dtrace module is loaded but not attached, or if
15940 * there aren't isn't a meta provider registered to deal with
15941 * these provider descriptions, we need to postpone creating
15942 * the actual providers until later.
15945 if (help->dthps_next == NULL && help->dthps_prev == NULL &&
15946 dtrace_deferred_pid != help) {
15947 help->dthps_deferred = 1;
15948 help->dthps_pid = p->p_pid;
15949 help->dthps_next = dtrace_deferred_pid;
15950 help->dthps_prev = NULL;
15951 if (dtrace_deferred_pid != NULL)
15952 dtrace_deferred_pid->dthps_prev = help;
15953 dtrace_deferred_pid = help;
15956 mutex_exit(&dtrace_lock);
15958 } else if (dofhp != NULL) {
15960 * If the dtrace module is loaded and we have a particular
15961 * helper provider description, pass that off to the
15965 mutex_exit(&dtrace_lock);
15967 dtrace_helper_provide(dofhp, p->p_pid);
15971 * Otherwise, just pass all the helper provider descriptions
15972 * off to the meta provider.
15976 mutex_exit(&dtrace_lock);
15978 for (i = 0; i < help->dthps_nprovs; i++) {
15979 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
15984 mutex_exit(&dtrace_meta_lock);
15988 dtrace_helper_provider_add(dof_helper_t *dofhp, dtrace_helpers_t *help, int gen)
15990 dtrace_helper_provider_t *hprov, **tmp_provs;
15991 uint_t tmp_maxprovs, i;
15993 ASSERT(MUTEX_HELD(&dtrace_lock));
15994 ASSERT(help != NULL);
15997 * If we already have dtrace_helper_providers_max helper providers,
15998 * we're refuse to add a new one.
16000 if (help->dthps_nprovs >= dtrace_helper_providers_max)
16004 * Check to make sure this isn't a duplicate.
16006 for (i = 0; i < help->dthps_nprovs; i++) {
16007 if (dofhp->dofhp_addr ==
16008 help->dthps_provs[i]->dthp_prov.dofhp_addr)
16012 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP);
16013 hprov->dthp_prov = *dofhp;
16014 hprov->dthp_ref = 1;
16015 hprov->dthp_generation = gen;
16018 * Allocate a bigger table for helper providers if it's already full.
16020 if (help->dthps_maxprovs == help->dthps_nprovs) {
16021 tmp_maxprovs = help->dthps_maxprovs;
16022 tmp_provs = help->dthps_provs;
16024 if (help->dthps_maxprovs == 0)
16025 help->dthps_maxprovs = 2;
16027 help->dthps_maxprovs *= 2;
16028 if (help->dthps_maxprovs > dtrace_helper_providers_max)
16029 help->dthps_maxprovs = dtrace_helper_providers_max;
16031 ASSERT(tmp_maxprovs < help->dthps_maxprovs);
16033 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs *
16034 sizeof (dtrace_helper_provider_t *), KM_SLEEP);
16036 if (tmp_provs != NULL) {
16037 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs *
16038 sizeof (dtrace_helper_provider_t *));
16039 kmem_free(tmp_provs, tmp_maxprovs *
16040 sizeof (dtrace_helper_provider_t *));
16044 help->dthps_provs[help->dthps_nprovs] = hprov;
16045 help->dthps_nprovs++;
16051 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov)
16053 mutex_enter(&dtrace_lock);
16055 if (--hprov->dthp_ref == 0) {
16057 mutex_exit(&dtrace_lock);
16058 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof;
16059 dtrace_dof_destroy(dof);
16060 kmem_free(hprov, sizeof (dtrace_helper_provider_t));
16062 mutex_exit(&dtrace_lock);
16067 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec)
16069 uintptr_t daddr = (uintptr_t)dof;
16070 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
16071 dof_provider_t *provider;
16072 dof_probe_t *probe;
16074 char *strtab, *typestr;
16075 dof_stridx_t typeidx;
16077 uint_t nprobes, j, k;
16079 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER);
16081 if (sec->dofs_offset & (sizeof (uint_t) - 1)) {
16082 dtrace_dof_error(dof, "misaligned section offset");
16087 * The section needs to be large enough to contain the DOF provider
16088 * structure appropriate for the given version.
16090 if (sec->dofs_size <
16091 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ?
16092 offsetof(dof_provider_t, dofpv_prenoffs) :
16093 sizeof (dof_provider_t))) {
16094 dtrace_dof_error(dof, "provider section too small");
16098 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
16099 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab);
16100 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes);
16101 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs);
16102 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs);
16104 if (str_sec == NULL || prb_sec == NULL ||
16105 arg_sec == NULL || off_sec == NULL)
16110 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
16111 provider->dofpv_prenoffs != DOF_SECT_NONE &&
16112 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS,
16113 provider->dofpv_prenoffs)) == NULL)
16116 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
16118 if (provider->dofpv_name >= str_sec->dofs_size ||
16119 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) {
16120 dtrace_dof_error(dof, "invalid provider name");
16124 if (prb_sec->dofs_entsize == 0 ||
16125 prb_sec->dofs_entsize > prb_sec->dofs_size) {
16126 dtrace_dof_error(dof, "invalid entry size");
16130 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) {
16131 dtrace_dof_error(dof, "misaligned entry size");
16135 if (off_sec->dofs_entsize != sizeof (uint32_t)) {
16136 dtrace_dof_error(dof, "invalid entry size");
16140 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) {
16141 dtrace_dof_error(dof, "misaligned section offset");
16145 if (arg_sec->dofs_entsize != sizeof (uint8_t)) {
16146 dtrace_dof_error(dof, "invalid entry size");
16150 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
16152 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
16155 * Take a pass through the probes to check for errors.
16157 for (j = 0; j < nprobes; j++) {
16158 probe = (dof_probe_t *)(uintptr_t)(daddr +
16159 prb_sec->dofs_offset + j * prb_sec->dofs_entsize);
16161 if (probe->dofpr_func >= str_sec->dofs_size) {
16162 dtrace_dof_error(dof, "invalid function name");
16166 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) {
16167 dtrace_dof_error(dof, "function name too long");
16169 * Keep going if the function name is too long.
16170 * Unlike provider and probe names, we cannot reasonably
16171 * impose restrictions on function names, since they're
16172 * a property of the code being instrumented. We will
16173 * skip this probe in dtrace_helper_provide_one().
16177 if (probe->dofpr_name >= str_sec->dofs_size ||
16178 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) {
16179 dtrace_dof_error(dof, "invalid probe name");
16184 * The offset count must not wrap the index, and the offsets
16185 * must also not overflow the section's data.
16187 if (probe->dofpr_offidx + probe->dofpr_noffs <
16188 probe->dofpr_offidx ||
16189 (probe->dofpr_offidx + probe->dofpr_noffs) *
16190 off_sec->dofs_entsize > off_sec->dofs_size) {
16191 dtrace_dof_error(dof, "invalid probe offset");
16195 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) {
16197 * If there's no is-enabled offset section, make sure
16198 * there aren't any is-enabled offsets. Otherwise
16199 * perform the same checks as for probe offsets
16200 * (immediately above).
16202 if (enoff_sec == NULL) {
16203 if (probe->dofpr_enoffidx != 0 ||
16204 probe->dofpr_nenoffs != 0) {
16205 dtrace_dof_error(dof, "is-enabled "
16206 "offsets with null section");
16209 } else if (probe->dofpr_enoffidx +
16210 probe->dofpr_nenoffs < probe->dofpr_enoffidx ||
16211 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) *
16212 enoff_sec->dofs_entsize > enoff_sec->dofs_size) {
16213 dtrace_dof_error(dof, "invalid is-enabled "
16218 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) {
16219 dtrace_dof_error(dof, "zero probe and "
16220 "is-enabled offsets");
16223 } else if (probe->dofpr_noffs == 0) {
16224 dtrace_dof_error(dof, "zero probe offsets");
16228 if (probe->dofpr_argidx + probe->dofpr_xargc <
16229 probe->dofpr_argidx ||
16230 (probe->dofpr_argidx + probe->dofpr_xargc) *
16231 arg_sec->dofs_entsize > arg_sec->dofs_size) {
16232 dtrace_dof_error(dof, "invalid args");
16236 typeidx = probe->dofpr_nargv;
16237 typestr = strtab + probe->dofpr_nargv;
16238 for (k = 0; k < probe->dofpr_nargc; k++) {
16239 if (typeidx >= str_sec->dofs_size) {
16240 dtrace_dof_error(dof, "bad "
16241 "native argument type");
16245 typesz = strlen(typestr) + 1;
16246 if (typesz > DTRACE_ARGTYPELEN) {
16247 dtrace_dof_error(dof, "native "
16248 "argument type too long");
16255 typeidx = probe->dofpr_xargv;
16256 typestr = strtab + probe->dofpr_xargv;
16257 for (k = 0; k < probe->dofpr_xargc; k++) {
16258 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) {
16259 dtrace_dof_error(dof, "bad "
16260 "native argument index");
16264 if (typeidx >= str_sec->dofs_size) {
16265 dtrace_dof_error(dof, "bad "
16266 "translated argument type");
16270 typesz = strlen(typestr) + 1;
16271 if (typesz > DTRACE_ARGTYPELEN) {
16272 dtrace_dof_error(dof, "translated argument "
16286 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp, struct proc *p)
16288 dtrace_helpers_t *help;
16289 dtrace_vstate_t *vstate;
16290 dtrace_enabling_t *enab = NULL;
16291 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1;
16292 uintptr_t daddr = (uintptr_t)dof;
16294 ASSERT(MUTEX_HELD(&dtrace_lock));
16296 if ((help = p->p_dtrace_helpers) == NULL)
16297 help = dtrace_helpers_create(p);
16299 vstate = &help->dthps_vstate;
16301 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, dhp->dofhp_addr,
16302 dhp->dofhp_dof, B_FALSE)) != 0) {
16303 dtrace_dof_destroy(dof);
16308 * Look for helper providers and validate their descriptions.
16310 for (i = 0; i < dof->dofh_secnum; i++) {
16311 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
16312 dof->dofh_secoff + i * dof->dofh_secsize);
16314 if (sec->dofs_type != DOF_SECT_PROVIDER)
16317 if (dtrace_helper_provider_validate(dof, sec) != 0) {
16318 dtrace_enabling_destroy(enab);
16319 dtrace_dof_destroy(dof);
16327 * Now we need to walk through the ECB descriptions in the enabling.
16329 for (i = 0; i < enab->dten_ndesc; i++) {
16330 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
16331 dtrace_probedesc_t *desc = &ep->dted_probe;
16333 if (strcmp(desc->dtpd_provider, "dtrace") != 0)
16336 if (strcmp(desc->dtpd_mod, "helper") != 0)
16339 if (strcmp(desc->dtpd_func, "ustack") != 0)
16342 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK,
16345 * Adding this helper action failed -- we are now going
16346 * to rip out the entire generation and return failure.
16348 (void) dtrace_helper_destroygen(help,
16349 help->dthps_generation);
16350 dtrace_enabling_destroy(enab);
16351 dtrace_dof_destroy(dof);
16358 if (nhelpers < enab->dten_ndesc)
16359 dtrace_dof_error(dof, "unmatched helpers");
16361 gen = help->dthps_generation++;
16362 dtrace_enabling_destroy(enab);
16366 * Now that this is in-kernel, we change the sense of the
16367 * members: dofhp_dof denotes the in-kernel copy of the DOF
16368 * and dofhp_addr denotes the address at user-level.
16370 dhp->dofhp_addr = dhp->dofhp_dof;
16371 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof;
16373 if (dtrace_helper_provider_add(dhp, help, gen) == 0) {
16374 mutex_exit(&dtrace_lock);
16375 dtrace_helper_provider_register(p, help, dhp);
16376 mutex_enter(&dtrace_lock);
16383 dtrace_dof_destroy(dof);
16388 static dtrace_helpers_t *
16389 dtrace_helpers_create(proc_t *p)
16391 dtrace_helpers_t *help;
16393 ASSERT(MUTEX_HELD(&dtrace_lock));
16394 ASSERT(p->p_dtrace_helpers == NULL);
16396 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP);
16397 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) *
16398 DTRACE_NHELPER_ACTIONS, KM_SLEEP);
16400 p->p_dtrace_helpers = help;
16410 dtrace_helpers_destroy(proc_t *p)
16412 dtrace_helpers_t *help;
16413 dtrace_vstate_t *vstate;
16415 proc_t *p = curproc;
16419 mutex_enter(&dtrace_lock);
16421 ASSERT(p->p_dtrace_helpers != NULL);
16422 ASSERT(dtrace_helpers > 0);
16424 help = p->p_dtrace_helpers;
16425 vstate = &help->dthps_vstate;
16428 * We're now going to lose the help from this process.
16430 p->p_dtrace_helpers = NULL;
16434 * Destory the helper actions.
16436 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
16437 dtrace_helper_action_t *h, *next;
16439 for (h = help->dthps_actions[i]; h != NULL; h = next) {
16440 next = h->dtha_next;
16441 dtrace_helper_action_destroy(h, vstate);
16446 mutex_exit(&dtrace_lock);
16449 * Destroy the helper providers.
16451 if (help->dthps_maxprovs > 0) {
16452 mutex_enter(&dtrace_meta_lock);
16453 if (dtrace_meta_pid != NULL) {
16454 ASSERT(dtrace_deferred_pid == NULL);
16456 for (i = 0; i < help->dthps_nprovs; i++) {
16457 dtrace_helper_provider_remove(
16458 &help->dthps_provs[i]->dthp_prov, p->p_pid);
16461 mutex_enter(&dtrace_lock);
16462 ASSERT(help->dthps_deferred == 0 ||
16463 help->dthps_next != NULL ||
16464 help->dthps_prev != NULL ||
16465 help == dtrace_deferred_pid);
16468 * Remove the helper from the deferred list.
16470 if (help->dthps_next != NULL)
16471 help->dthps_next->dthps_prev = help->dthps_prev;
16472 if (help->dthps_prev != NULL)
16473 help->dthps_prev->dthps_next = help->dthps_next;
16474 if (dtrace_deferred_pid == help) {
16475 dtrace_deferred_pid = help->dthps_next;
16476 ASSERT(help->dthps_prev == NULL);
16479 mutex_exit(&dtrace_lock);
16482 mutex_exit(&dtrace_meta_lock);
16484 for (i = 0; i < help->dthps_nprovs; i++) {
16485 dtrace_helper_provider_destroy(help->dthps_provs[i]);
16488 kmem_free(help->dthps_provs, help->dthps_maxprovs *
16489 sizeof (dtrace_helper_provider_t *));
16492 mutex_enter(&dtrace_lock);
16494 dtrace_vstate_fini(&help->dthps_vstate);
16495 kmem_free(help->dthps_actions,
16496 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS);
16497 kmem_free(help, sizeof (dtrace_helpers_t));
16500 mutex_exit(&dtrace_lock);
16507 dtrace_helpers_duplicate(proc_t *from, proc_t *to)
16509 dtrace_helpers_t *help, *newhelp;
16510 dtrace_helper_action_t *helper, *new, *last;
16512 dtrace_vstate_t *vstate;
16513 int i, j, sz, hasprovs = 0;
16515 mutex_enter(&dtrace_lock);
16516 ASSERT(from->p_dtrace_helpers != NULL);
16517 ASSERT(dtrace_helpers > 0);
16519 help = from->p_dtrace_helpers;
16520 newhelp = dtrace_helpers_create(to);
16521 ASSERT(to->p_dtrace_helpers != NULL);
16523 newhelp->dthps_generation = help->dthps_generation;
16524 vstate = &newhelp->dthps_vstate;
16527 * Duplicate the helper actions.
16529 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
16530 if ((helper = help->dthps_actions[i]) == NULL)
16533 for (last = NULL; helper != NULL; helper = helper->dtha_next) {
16534 new = kmem_zalloc(sizeof (dtrace_helper_action_t),
16536 new->dtha_generation = helper->dtha_generation;
16538 if ((dp = helper->dtha_predicate) != NULL) {
16539 dp = dtrace_difo_duplicate(dp, vstate);
16540 new->dtha_predicate = dp;
16543 new->dtha_nactions = helper->dtha_nactions;
16544 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions;
16545 new->dtha_actions = kmem_alloc(sz, KM_SLEEP);
16547 for (j = 0; j < new->dtha_nactions; j++) {
16548 dtrace_difo_t *dp = helper->dtha_actions[j];
16550 ASSERT(dp != NULL);
16551 dp = dtrace_difo_duplicate(dp, vstate);
16552 new->dtha_actions[j] = dp;
16555 if (last != NULL) {
16556 last->dtha_next = new;
16558 newhelp->dthps_actions[i] = new;
16566 * Duplicate the helper providers and register them with the
16567 * DTrace framework.
16569 if (help->dthps_nprovs > 0) {
16570 newhelp->dthps_nprovs = help->dthps_nprovs;
16571 newhelp->dthps_maxprovs = help->dthps_nprovs;
16572 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs *
16573 sizeof (dtrace_helper_provider_t *), KM_SLEEP);
16574 for (i = 0; i < newhelp->dthps_nprovs; i++) {
16575 newhelp->dthps_provs[i] = help->dthps_provs[i];
16576 newhelp->dthps_provs[i]->dthp_ref++;
16582 mutex_exit(&dtrace_lock);
16585 dtrace_helper_provider_register(to, newhelp, NULL);
16589 * DTrace Hook Functions
16592 dtrace_module_loaded(modctl_t *ctl)
16594 dtrace_provider_t *prv;
16596 mutex_enter(&dtrace_provider_lock);
16598 mutex_enter(&mod_lock);
16602 ASSERT(ctl->mod_busy);
16606 * We're going to call each providers per-module provide operation
16607 * specifying only this module.
16609 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
16610 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
16613 mutex_exit(&mod_lock);
16615 mutex_exit(&dtrace_provider_lock);
16618 * If we have any retained enablings, we need to match against them.
16619 * Enabling probes requires that cpu_lock be held, and we cannot hold
16620 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
16621 * module. (In particular, this happens when loading scheduling
16622 * classes.) So if we have any retained enablings, we need to dispatch
16623 * our task queue to do the match for us.
16625 mutex_enter(&dtrace_lock);
16627 if (dtrace_retained == NULL) {
16628 mutex_exit(&dtrace_lock);
16632 (void) taskq_dispatch(dtrace_taskq,
16633 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP);
16635 mutex_exit(&dtrace_lock);
16638 * And now, for a little heuristic sleaze: in general, we want to
16639 * match modules as soon as they load. However, we cannot guarantee
16640 * this, because it would lead us to the lock ordering violation
16641 * outlined above. The common case, of course, is that cpu_lock is
16642 * _not_ held -- so we delay here for a clock tick, hoping that that's
16643 * long enough for the task queue to do its work. If it's not, it's
16644 * not a serious problem -- it just means that the module that we
16645 * just loaded may not be immediately instrumentable.
16652 dtrace_module_unloaded(modctl_t *ctl)
16654 dtrace_module_unloaded(modctl_t *ctl, int *error)
16657 dtrace_probe_t template, *probe, *first, *next;
16658 dtrace_provider_t *prov;
16660 char modname[DTRACE_MODNAMELEN];
16665 template.dtpr_mod = ctl->mod_modname;
16667 /* Handle the fact that ctl->filename may end in ".ko". */
16668 strlcpy(modname, ctl->filename, sizeof(modname));
16669 len = strlen(ctl->filename);
16670 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0)
16671 modname[len - 3] = '\0';
16672 template.dtpr_mod = modname;
16675 mutex_enter(&dtrace_provider_lock);
16677 mutex_enter(&mod_lock);
16679 mutex_enter(&dtrace_lock);
16682 if (ctl->nenabled > 0) {
16683 /* Don't allow unloads if a probe is enabled. */
16684 mutex_exit(&dtrace_provider_lock);
16685 mutex_exit(&dtrace_lock);
16688 "kldunload: attempt to unload module that has DTrace probes enabled\n");
16693 if (dtrace_bymod == NULL) {
16695 * The DTrace module is loaded (obviously) but not attached;
16696 * we don't have any work to do.
16698 mutex_exit(&dtrace_provider_lock);
16700 mutex_exit(&mod_lock);
16702 mutex_exit(&dtrace_lock);
16706 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template);
16707 probe != NULL; probe = probe->dtpr_nextmod) {
16708 if (probe->dtpr_ecb != NULL) {
16709 mutex_exit(&dtrace_provider_lock);
16711 mutex_exit(&mod_lock);
16713 mutex_exit(&dtrace_lock);
16716 * This shouldn't _actually_ be possible -- we're
16717 * unloading a module that has an enabled probe in it.
16718 * (It's normally up to the provider to make sure that
16719 * this can't happen.) However, because dtps_enable()
16720 * doesn't have a failure mode, there can be an
16721 * enable/unload race. Upshot: we don't want to
16722 * assert, but we're not going to disable the
16725 if (dtrace_err_verbose) {
16727 cmn_err(CE_WARN, "unloaded module '%s' had "
16728 "enabled probes", ctl->mod_modname);
16730 cmn_err(CE_WARN, "unloaded module '%s' had "
16731 "enabled probes", modname);
16741 for (first = NULL; probe != NULL; probe = next) {
16742 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe);
16744 dtrace_probes[probe->dtpr_id - 1] = NULL;
16746 next = probe->dtpr_nextmod;
16747 dtrace_hash_remove(dtrace_bymod, probe);
16748 dtrace_hash_remove(dtrace_byfunc, probe);
16749 dtrace_hash_remove(dtrace_byname, probe);
16751 if (first == NULL) {
16753 probe->dtpr_nextmod = NULL;
16755 probe->dtpr_nextmod = first;
16761 * We've removed all of the module's probes from the hash chains and
16762 * from the probe array. Now issue a dtrace_sync() to be sure that
16763 * everyone has cleared out from any probe array processing.
16767 for (probe = first; probe != NULL; probe = first) {
16768 first = probe->dtpr_nextmod;
16769 prov = probe->dtpr_provider;
16770 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
16772 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
16773 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
16774 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
16776 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1);
16778 free_unr(dtrace_arena, probe->dtpr_id);
16780 kmem_free(probe, sizeof (dtrace_probe_t));
16783 mutex_exit(&dtrace_lock);
16785 mutex_exit(&mod_lock);
16787 mutex_exit(&dtrace_provider_lock);
16792 dtrace_kld_load(void *arg __unused, linker_file_t lf)
16795 dtrace_module_loaded(lf);
16799 dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error)
16803 /* We already have an error, so don't do anything. */
16805 dtrace_module_unloaded(lf, error);
16811 dtrace_suspend(void)
16813 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend));
16817 dtrace_resume(void)
16819 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume));
16824 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu)
16826 ASSERT(MUTEX_HELD(&cpu_lock));
16827 mutex_enter(&dtrace_lock);
16831 dtrace_state_t *state;
16832 dtrace_optval_t *opt, rs, c;
16835 * For now, we only allocate a new buffer for anonymous state.
16837 if ((state = dtrace_anon.dta_state) == NULL)
16840 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
16843 opt = state->dts_options;
16844 c = opt[DTRACEOPT_CPU];
16846 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu)
16850 * Regardless of what the actual policy is, we're going to
16851 * temporarily set our resize policy to be manual. We're
16852 * also going to temporarily set our CPU option to denote
16853 * the newly configured CPU.
16855 rs = opt[DTRACEOPT_BUFRESIZE];
16856 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL;
16857 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu;
16859 (void) dtrace_state_buffers(state);
16861 opt[DTRACEOPT_BUFRESIZE] = rs;
16862 opt[DTRACEOPT_CPU] = c;
16869 * We don't free the buffer in the CPU_UNCONFIG case. (The
16870 * buffer will be freed when the consumer exits.)
16878 mutex_exit(&dtrace_lock);
16884 dtrace_cpu_setup_initial(processorid_t cpu)
16886 (void) dtrace_cpu_setup(CPU_CONFIG, cpu);
16891 dtrace_toxrange_add(uintptr_t base, uintptr_t limit)
16893 if (dtrace_toxranges >= dtrace_toxranges_max) {
16895 dtrace_toxrange_t *range;
16897 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
16900 ASSERT(dtrace_toxrange == NULL);
16901 ASSERT(dtrace_toxranges_max == 0);
16902 dtrace_toxranges_max = 1;
16904 dtrace_toxranges_max <<= 1;
16907 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
16908 range = kmem_zalloc(nsize, KM_SLEEP);
16910 if (dtrace_toxrange != NULL) {
16911 ASSERT(osize != 0);
16912 bcopy(dtrace_toxrange, range, osize);
16913 kmem_free(dtrace_toxrange, osize);
16916 dtrace_toxrange = range;
16919 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0);
16920 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0);
16922 dtrace_toxrange[dtrace_toxranges].dtt_base = base;
16923 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit;
16924 dtrace_toxranges++;
16928 dtrace_getf_barrier()
16932 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings
16933 * that contain calls to getf(), this routine will be called on every
16934 * closef() before either the underlying vnode is released or the
16935 * file_t itself is freed. By the time we are here, it is essential
16936 * that the file_t can no longer be accessed from a call to getf()
16937 * in probe context -- that assures that a dtrace_sync() can be used
16938 * to clear out any enablings referring to the old structures.
16940 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 ||
16941 kcred->cr_zone->zone_dtrace_getf != 0)
16947 * DTrace Driver Cookbook Functions
16952 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
16954 dtrace_provider_id_t id;
16955 dtrace_state_t *state = NULL;
16956 dtrace_enabling_t *enab;
16958 mutex_enter(&cpu_lock);
16959 mutex_enter(&dtrace_provider_lock);
16960 mutex_enter(&dtrace_lock);
16962 if (ddi_soft_state_init(&dtrace_softstate,
16963 sizeof (dtrace_state_t), 0) != 0) {
16964 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state");
16965 mutex_exit(&cpu_lock);
16966 mutex_exit(&dtrace_provider_lock);
16967 mutex_exit(&dtrace_lock);
16968 return (DDI_FAILURE);
16971 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR,
16972 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE ||
16973 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR,
16974 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) {
16975 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes");
16976 ddi_remove_minor_node(devi, NULL);
16977 ddi_soft_state_fini(&dtrace_softstate);
16978 mutex_exit(&cpu_lock);
16979 mutex_exit(&dtrace_provider_lock);
16980 mutex_exit(&dtrace_lock);
16981 return (DDI_FAILURE);
16984 ddi_report_dev(devi);
16985 dtrace_devi = devi;
16987 dtrace_modload = dtrace_module_loaded;
16988 dtrace_modunload = dtrace_module_unloaded;
16989 dtrace_cpu_init = dtrace_cpu_setup_initial;
16990 dtrace_helpers_cleanup = dtrace_helpers_destroy;
16991 dtrace_helpers_fork = dtrace_helpers_duplicate;
16992 dtrace_cpustart_init = dtrace_suspend;
16993 dtrace_cpustart_fini = dtrace_resume;
16994 dtrace_debugger_init = dtrace_suspend;
16995 dtrace_debugger_fini = dtrace_resume;
16997 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
16999 ASSERT(MUTEX_HELD(&cpu_lock));
17001 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1,
17002 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
17003 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE,
17004 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0,
17005 VM_SLEEP | VMC_IDENTIFIER);
17006 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri,
17009 dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
17010 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
17011 NULL, NULL, NULL, NULL, NULL, 0);
17013 ASSERT(MUTEX_HELD(&cpu_lock));
17014 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
17015 offsetof(dtrace_probe_t, dtpr_nextmod),
17016 offsetof(dtrace_probe_t, dtpr_prevmod));
17018 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
17019 offsetof(dtrace_probe_t, dtpr_nextfunc),
17020 offsetof(dtrace_probe_t, dtpr_prevfunc));
17022 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
17023 offsetof(dtrace_probe_t, dtpr_nextname),
17024 offsetof(dtrace_probe_t, dtpr_prevname));
17026 if (dtrace_retain_max < 1) {
17027 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
17028 "setting to 1", dtrace_retain_max);
17029 dtrace_retain_max = 1;
17033 * Now discover our toxic ranges.
17035 dtrace_toxic_ranges(dtrace_toxrange_add);
17038 * Before we register ourselves as a provider to our own framework,
17039 * we would like to assert that dtrace_provider is NULL -- but that's
17040 * not true if we were loaded as a dependency of a DTrace provider.
17041 * Once we've registered, we can assert that dtrace_provider is our
17044 (void) dtrace_register("dtrace", &dtrace_provider_attr,
17045 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
17047 ASSERT(dtrace_provider != NULL);
17048 ASSERT((dtrace_provider_id_t)dtrace_provider == id);
17050 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
17051 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
17052 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
17053 dtrace_provider, NULL, NULL, "END", 0, NULL);
17054 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
17055 dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
17057 dtrace_anon_property();
17058 mutex_exit(&cpu_lock);
17061 * If there are already providers, we must ask them to provide their
17062 * probes, and then match any anonymous enabling against them. Note
17063 * that there should be no other retained enablings at this time:
17064 * the only retained enablings at this time should be the anonymous
17067 if (dtrace_anon.dta_enabling != NULL) {
17068 ASSERT(dtrace_retained == dtrace_anon.dta_enabling);
17070 dtrace_enabling_provide(NULL);
17071 state = dtrace_anon.dta_state;
17074 * We couldn't hold cpu_lock across the above call to
17075 * dtrace_enabling_provide(), but we must hold it to actually
17076 * enable the probes. We have to drop all of our locks, pick
17077 * up cpu_lock, and regain our locks before matching the
17078 * retained anonymous enabling.
17080 mutex_exit(&dtrace_lock);
17081 mutex_exit(&dtrace_provider_lock);
17083 mutex_enter(&cpu_lock);
17084 mutex_enter(&dtrace_provider_lock);
17085 mutex_enter(&dtrace_lock);
17087 if ((enab = dtrace_anon.dta_enabling) != NULL)
17088 (void) dtrace_enabling_match(enab, NULL);
17090 mutex_exit(&cpu_lock);
17093 mutex_exit(&dtrace_lock);
17094 mutex_exit(&dtrace_provider_lock);
17096 if (state != NULL) {
17098 * If we created any anonymous state, set it going now.
17100 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon);
17103 return (DDI_SUCCESS);
17105 #endif /* illumos */
17108 static void dtrace_dtr(void *);
17114 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
17116 dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
17119 dtrace_state_t *state;
17125 if (getminor(*devp) == DTRACEMNRN_HELPER)
17129 * If this wasn't an open with the "helper" minor, then it must be
17130 * the "dtrace" minor.
17132 if (getminor(*devp) == DTRACEMNRN_DTRACE)
17135 cred_t *cred_p = NULL;
17136 cred_p = dev->si_cred;
17139 * If no DTRACE_PRIV_* bits are set in the credential, then the
17140 * caller lacks sufficient permission to do anything with DTrace.
17142 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid);
17143 if (priv == DTRACE_PRIV_NONE) {
17150 * Ask all providers to provide all their probes.
17152 mutex_enter(&dtrace_provider_lock);
17153 dtrace_probe_provide(NULL, NULL);
17154 mutex_exit(&dtrace_provider_lock);
17156 mutex_enter(&cpu_lock);
17157 mutex_enter(&dtrace_lock);
17159 dtrace_membar_producer();
17163 * If the kernel debugger is active (that is, if the kernel debugger
17164 * modified text in some way), we won't allow the open.
17166 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
17168 mutex_exit(&cpu_lock);
17169 mutex_exit(&dtrace_lock);
17173 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) {
17175 * If DTrace helper tracing is enabled, we need to allocate the
17176 * trace buffer and initialize the values.
17178 dtrace_helptrace_buffer =
17179 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
17180 dtrace_helptrace_next = 0;
17181 dtrace_helptrace_wrapped = 0;
17182 dtrace_helptrace_enable = 0;
17185 state = dtrace_state_create(devp, cred_p);
17187 state = dtrace_state_create(dev, NULL);
17188 devfs_set_cdevpriv(state, dtrace_dtr);
17191 mutex_exit(&cpu_lock);
17193 if (state == NULL) {
17195 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
17196 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
17200 mutex_exit(&dtrace_lock);
17204 mutex_exit(&dtrace_lock);
17212 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
17215 dtrace_dtr(void *data)
17219 minor_t minor = getminor(dev);
17220 dtrace_state_t *state;
17222 dtrace_helptrace_t *buf = NULL;
17225 if (minor == DTRACEMNRN_HELPER)
17228 state = ddi_get_soft_state(dtrace_softstate, minor);
17230 dtrace_state_t *state = data;
17233 mutex_enter(&cpu_lock);
17234 mutex_enter(&dtrace_lock);
17237 if (state->dts_anon)
17239 if (state != NULL && state->dts_anon)
17243 * There is anonymous state. Destroy that first.
17245 ASSERT(dtrace_anon.dta_state == NULL);
17246 dtrace_state_destroy(state->dts_anon);
17249 if (dtrace_helptrace_disable) {
17251 * If we have been told to disable helper tracing, set the
17252 * buffer to NULL before calling into dtrace_state_destroy();
17253 * we take advantage of its dtrace_sync() to know that no
17254 * CPU is in probe context with enabled helper tracing
17255 * after it returns.
17257 buf = dtrace_helptrace_buffer;
17258 dtrace_helptrace_buffer = NULL;
17262 dtrace_state_destroy(state);
17264 if (state != NULL) {
17265 dtrace_state_destroy(state);
17266 kmem_free(state, 0);
17269 ASSERT(dtrace_opens > 0);
17273 * Only relinquish control of the kernel debugger interface when there
17274 * are no consumers and no anonymous enablings.
17276 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
17277 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
17283 kmem_free(buf, dtrace_helptrace_bufsize);
17284 dtrace_helptrace_disable = 0;
17287 mutex_exit(&dtrace_lock);
17288 mutex_exit(&cpu_lock);
17298 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv)
17301 dof_helper_t help, *dhp = NULL;
17304 case DTRACEHIOC_ADDDOF:
17305 if (copyin((void *)arg, &help, sizeof (help)) != 0) {
17306 dtrace_dof_error(NULL, "failed to copyin DOF helper");
17311 arg = (intptr_t)help.dofhp_dof;
17314 case DTRACEHIOC_ADD: {
17315 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval);
17320 mutex_enter(&dtrace_lock);
17323 * dtrace_helper_slurp() takes responsibility for the dof --
17324 * it may free it now or it may save it and free it later.
17326 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) {
17333 mutex_exit(&dtrace_lock);
17337 case DTRACEHIOC_REMOVE: {
17338 mutex_enter(&dtrace_lock);
17339 rval = dtrace_helper_destroygen(NULL, arg);
17340 mutex_exit(&dtrace_lock);
17354 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
17356 minor_t minor = getminor(dev);
17357 dtrace_state_t *state;
17360 if (minor == DTRACEMNRN_HELPER)
17361 return (dtrace_ioctl_helper(cmd, arg, rv));
17363 state = ddi_get_soft_state(dtrace_softstate, minor);
17365 if (state->dts_anon) {
17366 ASSERT(dtrace_anon.dta_state == NULL);
17367 state = state->dts_anon;
17371 case DTRACEIOC_PROVIDER: {
17372 dtrace_providerdesc_t pvd;
17373 dtrace_provider_t *pvp;
17375 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0)
17378 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
17379 mutex_enter(&dtrace_provider_lock);
17381 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
17382 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0)
17386 mutex_exit(&dtrace_provider_lock);
17391 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t));
17392 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t));
17394 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0)
17400 case DTRACEIOC_EPROBE: {
17401 dtrace_eprobedesc_t epdesc;
17403 dtrace_action_t *act;
17409 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0)
17412 mutex_enter(&dtrace_lock);
17414 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
17415 mutex_exit(&dtrace_lock);
17419 if (ecb->dte_probe == NULL) {
17420 mutex_exit(&dtrace_lock);
17424 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
17425 epdesc.dtepd_uarg = ecb->dte_uarg;
17426 epdesc.dtepd_size = ecb->dte_size;
17428 nrecs = epdesc.dtepd_nrecs;
17429 epdesc.dtepd_nrecs = 0;
17430 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
17431 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
17434 epdesc.dtepd_nrecs++;
17438 * Now that we have the size, we need to allocate a temporary
17439 * buffer in which to store the complete description. We need
17440 * the temporary buffer to be able to drop dtrace_lock()
17441 * across the copyout(), below.
17443 size = sizeof (dtrace_eprobedesc_t) +
17444 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
17446 buf = kmem_alloc(size, KM_SLEEP);
17447 dest = (uintptr_t)buf;
17449 bcopy(&epdesc, (void *)dest, sizeof (epdesc));
17450 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
17452 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
17453 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
17459 bcopy(&act->dta_rec, (void *)dest,
17460 sizeof (dtrace_recdesc_t));
17461 dest += sizeof (dtrace_recdesc_t);
17464 mutex_exit(&dtrace_lock);
17466 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
17467 kmem_free(buf, size);
17471 kmem_free(buf, size);
17475 case DTRACEIOC_AGGDESC: {
17476 dtrace_aggdesc_t aggdesc;
17477 dtrace_action_t *act;
17478 dtrace_aggregation_t *agg;
17481 dtrace_recdesc_t *lrec;
17486 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0)
17489 mutex_enter(&dtrace_lock);
17491 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
17492 mutex_exit(&dtrace_lock);
17496 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
17498 nrecs = aggdesc.dtagd_nrecs;
17499 aggdesc.dtagd_nrecs = 0;
17501 offs = agg->dtag_base;
17502 lrec = &agg->dtag_action.dta_rec;
17503 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
17505 for (act = agg->dtag_first; ; act = act->dta_next) {
17506 ASSERT(act->dta_intuple ||
17507 DTRACEACT_ISAGG(act->dta_kind));
17510 * If this action has a record size of zero, it
17511 * denotes an argument to the aggregating action.
17512 * Because the presence of this record doesn't (or
17513 * shouldn't) affect the way the data is interpreted,
17514 * we don't copy it out to save user-level the
17515 * confusion of dealing with a zero-length record.
17517 if (act->dta_rec.dtrd_size == 0) {
17518 ASSERT(agg->dtag_hasarg);
17522 aggdesc.dtagd_nrecs++;
17524 if (act == &agg->dtag_action)
17529 * Now that we have the size, we need to allocate a temporary
17530 * buffer in which to store the complete description. We need
17531 * the temporary buffer to be able to drop dtrace_lock()
17532 * across the copyout(), below.
17534 size = sizeof (dtrace_aggdesc_t) +
17535 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
17537 buf = kmem_alloc(size, KM_SLEEP);
17538 dest = (uintptr_t)buf;
17540 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
17541 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
17543 for (act = agg->dtag_first; ; act = act->dta_next) {
17544 dtrace_recdesc_t rec = act->dta_rec;
17547 * See the comment in the above loop for why we pass
17548 * over zero-length records.
17550 if (rec.dtrd_size == 0) {
17551 ASSERT(agg->dtag_hasarg);
17558 rec.dtrd_offset -= offs;
17559 bcopy(&rec, (void *)dest, sizeof (rec));
17560 dest += sizeof (dtrace_recdesc_t);
17562 if (act == &agg->dtag_action)
17566 mutex_exit(&dtrace_lock);
17568 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
17569 kmem_free(buf, size);
17573 kmem_free(buf, size);
17577 case DTRACEIOC_ENABLE: {
17579 dtrace_enabling_t *enab = NULL;
17580 dtrace_vstate_t *vstate;
17586 * If a NULL argument has been passed, we take this as our
17587 * cue to reevaluate our enablings.
17590 dtrace_enabling_matchall();
17595 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL)
17598 mutex_enter(&cpu_lock);
17599 mutex_enter(&dtrace_lock);
17600 vstate = &state->dts_vstate;
17602 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
17603 mutex_exit(&dtrace_lock);
17604 mutex_exit(&cpu_lock);
17605 dtrace_dof_destroy(dof);
17609 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) {
17610 mutex_exit(&dtrace_lock);
17611 mutex_exit(&cpu_lock);
17612 dtrace_dof_destroy(dof);
17616 if ((rval = dtrace_dof_options(dof, state)) != 0) {
17617 dtrace_enabling_destroy(enab);
17618 mutex_exit(&dtrace_lock);
17619 mutex_exit(&cpu_lock);
17620 dtrace_dof_destroy(dof);
17624 if ((err = dtrace_enabling_match(enab, rv)) == 0) {
17625 err = dtrace_enabling_retain(enab);
17627 dtrace_enabling_destroy(enab);
17630 mutex_exit(&cpu_lock);
17631 mutex_exit(&dtrace_lock);
17632 dtrace_dof_destroy(dof);
17637 case DTRACEIOC_REPLICATE: {
17638 dtrace_repldesc_t desc;
17639 dtrace_probedesc_t *match = &desc.dtrpd_match;
17640 dtrace_probedesc_t *create = &desc.dtrpd_create;
17643 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
17646 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
17647 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
17648 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
17649 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
17651 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
17652 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
17653 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
17654 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
17656 mutex_enter(&dtrace_lock);
17657 err = dtrace_enabling_replicate(state, match, create);
17658 mutex_exit(&dtrace_lock);
17663 case DTRACEIOC_PROBEMATCH:
17664 case DTRACEIOC_PROBES: {
17665 dtrace_probe_t *probe = NULL;
17666 dtrace_probedesc_t desc;
17667 dtrace_probekey_t pkey;
17674 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
17677 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
17678 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
17679 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
17680 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0';
17683 * Before we attempt to match this probe, we want to give
17684 * all providers the opportunity to provide it.
17686 if (desc.dtpd_id == DTRACE_IDNONE) {
17687 mutex_enter(&dtrace_provider_lock);
17688 dtrace_probe_provide(&desc, NULL);
17689 mutex_exit(&dtrace_provider_lock);
17693 if (cmd == DTRACEIOC_PROBEMATCH) {
17694 dtrace_probekey(&desc, &pkey);
17695 pkey.dtpk_id = DTRACE_IDNONE;
17698 dtrace_cred2priv(cr, &priv, &uid, &zoneid);
17700 mutex_enter(&dtrace_lock);
17702 if (cmd == DTRACEIOC_PROBEMATCH) {
17703 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
17704 if ((probe = dtrace_probes[i - 1]) != NULL &&
17705 (m = dtrace_match_probe(probe, &pkey,
17706 priv, uid, zoneid)) != 0)
17711 mutex_exit(&dtrace_lock);
17716 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
17717 if ((probe = dtrace_probes[i - 1]) != NULL &&
17718 dtrace_match_priv(probe, priv, uid, zoneid))
17723 if (probe == NULL) {
17724 mutex_exit(&dtrace_lock);
17728 dtrace_probe_description(probe, &desc);
17729 mutex_exit(&dtrace_lock);
17731 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
17737 case DTRACEIOC_PROBEARG: {
17738 dtrace_argdesc_t desc;
17739 dtrace_probe_t *probe;
17740 dtrace_provider_t *prov;
17742 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
17745 if (desc.dtargd_id == DTRACE_IDNONE)
17748 if (desc.dtargd_ndx == DTRACE_ARGNONE)
17751 mutex_enter(&dtrace_provider_lock);
17752 mutex_enter(&mod_lock);
17753 mutex_enter(&dtrace_lock);
17755 if (desc.dtargd_id > dtrace_nprobes) {
17756 mutex_exit(&dtrace_lock);
17757 mutex_exit(&mod_lock);
17758 mutex_exit(&dtrace_provider_lock);
17762 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) {
17763 mutex_exit(&dtrace_lock);
17764 mutex_exit(&mod_lock);
17765 mutex_exit(&dtrace_provider_lock);
17769 mutex_exit(&dtrace_lock);
17771 prov = probe->dtpr_provider;
17773 if (prov->dtpv_pops.dtps_getargdesc == NULL) {
17775 * There isn't any typed information for this probe.
17776 * Set the argument number to DTRACE_ARGNONE.
17778 desc.dtargd_ndx = DTRACE_ARGNONE;
17780 desc.dtargd_native[0] = '\0';
17781 desc.dtargd_xlate[0] = '\0';
17782 desc.dtargd_mapping = desc.dtargd_ndx;
17784 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
17785 probe->dtpr_id, probe->dtpr_arg, &desc);
17788 mutex_exit(&mod_lock);
17789 mutex_exit(&dtrace_provider_lock);
17791 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
17797 case DTRACEIOC_GO: {
17798 processorid_t cpuid;
17799 rval = dtrace_state_go(state, &cpuid);
17804 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
17810 case DTRACEIOC_STOP: {
17811 processorid_t cpuid;
17813 mutex_enter(&dtrace_lock);
17814 rval = dtrace_state_stop(state, &cpuid);
17815 mutex_exit(&dtrace_lock);
17820 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
17826 case DTRACEIOC_DOFGET: {
17827 dof_hdr_t hdr, *dof;
17830 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0)
17833 mutex_enter(&dtrace_lock);
17834 dof = dtrace_dof_create(state);
17835 mutex_exit(&dtrace_lock);
17837 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
17838 rval = copyout(dof, (void *)arg, len);
17839 dtrace_dof_destroy(dof);
17841 return (rval == 0 ? 0 : EFAULT);
17844 case DTRACEIOC_AGGSNAP:
17845 case DTRACEIOC_BUFSNAP: {
17846 dtrace_bufdesc_t desc;
17848 dtrace_buffer_t *buf;
17850 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
17853 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU)
17856 mutex_enter(&dtrace_lock);
17858 if (cmd == DTRACEIOC_BUFSNAP) {
17859 buf = &state->dts_buffer[desc.dtbd_cpu];
17861 buf = &state->dts_aggbuffer[desc.dtbd_cpu];
17864 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
17865 size_t sz = buf->dtb_offset;
17867 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
17868 mutex_exit(&dtrace_lock);
17873 * If this buffer has already been consumed, we're
17874 * going to indicate that there's nothing left here
17877 if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
17878 mutex_exit(&dtrace_lock);
17880 desc.dtbd_size = 0;
17881 desc.dtbd_drops = 0;
17882 desc.dtbd_errors = 0;
17883 desc.dtbd_oldest = 0;
17884 sz = sizeof (desc);
17886 if (copyout(&desc, (void *)arg, sz) != 0)
17893 * If this is a ring buffer that has wrapped, we want
17894 * to copy the whole thing out.
17896 if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
17897 dtrace_buffer_polish(buf);
17898 sz = buf->dtb_size;
17901 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
17902 mutex_exit(&dtrace_lock);
17906 desc.dtbd_size = sz;
17907 desc.dtbd_drops = buf->dtb_drops;
17908 desc.dtbd_errors = buf->dtb_errors;
17909 desc.dtbd_oldest = buf->dtb_xamot_offset;
17910 desc.dtbd_timestamp = dtrace_gethrtime();
17912 mutex_exit(&dtrace_lock);
17914 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
17917 buf->dtb_flags |= DTRACEBUF_CONSUMED;
17922 if (buf->dtb_tomax == NULL) {
17923 ASSERT(buf->dtb_xamot == NULL);
17924 mutex_exit(&dtrace_lock);
17928 cached = buf->dtb_tomax;
17929 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
17931 dtrace_xcall(desc.dtbd_cpu,
17932 (dtrace_xcall_t)dtrace_buffer_switch, buf);
17934 state->dts_errors += buf->dtb_xamot_errors;
17937 * If the buffers did not actually switch, then the cross call
17938 * did not take place -- presumably because the given CPU is
17939 * not in the ready set. If this is the case, we'll return
17942 if (buf->dtb_tomax == cached) {
17943 ASSERT(buf->dtb_xamot != cached);
17944 mutex_exit(&dtrace_lock);
17948 ASSERT(cached == buf->dtb_xamot);
17951 * We have our snapshot; now copy it out.
17953 if (copyout(buf->dtb_xamot, desc.dtbd_data,
17954 buf->dtb_xamot_offset) != 0) {
17955 mutex_exit(&dtrace_lock);
17959 desc.dtbd_size = buf->dtb_xamot_offset;
17960 desc.dtbd_drops = buf->dtb_xamot_drops;
17961 desc.dtbd_errors = buf->dtb_xamot_errors;
17962 desc.dtbd_oldest = 0;
17963 desc.dtbd_timestamp = buf->dtb_switched;
17965 mutex_exit(&dtrace_lock);
17968 * Finally, copy out the buffer description.
17970 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
17976 case DTRACEIOC_CONF: {
17977 dtrace_conf_t conf;
17979 bzero(&conf, sizeof (conf));
17980 conf.dtc_difversion = DIF_VERSION;
17981 conf.dtc_difintregs = DIF_DIR_NREGS;
17982 conf.dtc_diftupregs = DIF_DTR_NREGS;
17983 conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
17985 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0)
17991 case DTRACEIOC_STATUS: {
17992 dtrace_status_t stat;
17993 dtrace_dstate_t *dstate;
17998 * See the comment in dtrace_state_deadman() for the reason
17999 * for setting dts_laststatus to INT64_MAX before setting
18000 * it to the correct value.
18002 state->dts_laststatus = INT64_MAX;
18003 dtrace_membar_producer();
18004 state->dts_laststatus = dtrace_gethrtime();
18006 bzero(&stat, sizeof (stat));
18008 mutex_enter(&dtrace_lock);
18010 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
18011 mutex_exit(&dtrace_lock);
18015 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
18016 stat.dtst_exiting = 1;
18018 nerrs = state->dts_errors;
18019 dstate = &state->dts_vstate.dtvs_dynvars;
18021 for (i = 0; i < NCPU; i++) {
18022 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
18024 stat.dtst_dyndrops += dcpu->dtdsc_drops;
18025 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
18026 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
18028 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
18029 stat.dtst_filled++;
18031 nerrs += state->dts_buffer[i].dtb_errors;
18033 for (j = 0; j < state->dts_nspeculations; j++) {
18034 dtrace_speculation_t *spec;
18035 dtrace_buffer_t *buf;
18037 spec = &state->dts_speculations[j];
18038 buf = &spec->dtsp_buffer[i];
18039 stat.dtst_specdrops += buf->dtb_xamot_drops;
18043 stat.dtst_specdrops_busy = state->dts_speculations_busy;
18044 stat.dtst_specdrops_unavail = state->dts_speculations_unavail;
18045 stat.dtst_stkstroverflows = state->dts_stkstroverflows;
18046 stat.dtst_dblerrors = state->dts_dblerrors;
18048 (state->dts_activity == DTRACE_ACTIVITY_KILLED);
18049 stat.dtst_errors = nerrs;
18051 mutex_exit(&dtrace_lock);
18053 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0)
18059 case DTRACEIOC_FORMAT: {
18060 dtrace_fmtdesc_t fmt;
18064 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0)
18067 mutex_enter(&dtrace_lock);
18069 if (fmt.dtfd_format == 0 ||
18070 fmt.dtfd_format > state->dts_nformats) {
18071 mutex_exit(&dtrace_lock);
18076 * Format strings are allocated contiguously and they are
18077 * never freed; if a format index is less than the number
18078 * of formats, we can assert that the format map is non-NULL
18079 * and that the format for the specified index is non-NULL.
18081 ASSERT(state->dts_formats != NULL);
18082 str = state->dts_formats[fmt.dtfd_format - 1];
18083 ASSERT(str != NULL);
18085 len = strlen(str) + 1;
18087 if (len > fmt.dtfd_length) {
18088 fmt.dtfd_length = len;
18090 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) {
18091 mutex_exit(&dtrace_lock);
18095 if (copyout(str, fmt.dtfd_string, len) != 0) {
18096 mutex_exit(&dtrace_lock);
18101 mutex_exit(&dtrace_lock);
18114 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
18116 dtrace_state_t *state;
18123 return (DDI_SUCCESS);
18126 return (DDI_FAILURE);
18129 mutex_enter(&cpu_lock);
18130 mutex_enter(&dtrace_provider_lock);
18131 mutex_enter(&dtrace_lock);
18133 ASSERT(dtrace_opens == 0);
18135 if (dtrace_helpers > 0) {
18136 mutex_exit(&dtrace_provider_lock);
18137 mutex_exit(&dtrace_lock);
18138 mutex_exit(&cpu_lock);
18139 return (DDI_FAILURE);
18142 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
18143 mutex_exit(&dtrace_provider_lock);
18144 mutex_exit(&dtrace_lock);
18145 mutex_exit(&cpu_lock);
18146 return (DDI_FAILURE);
18149 dtrace_provider = NULL;
18151 if ((state = dtrace_anon_grab()) != NULL) {
18153 * If there were ECBs on this state, the provider should
18154 * have not been allowed to detach; assert that there is
18157 ASSERT(state->dts_necbs == 0);
18158 dtrace_state_destroy(state);
18161 * If we're being detached with anonymous state, we need to
18162 * indicate to the kernel debugger that DTrace is now inactive.
18164 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
18167 bzero(&dtrace_anon, sizeof (dtrace_anon_t));
18168 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
18169 dtrace_cpu_init = NULL;
18170 dtrace_helpers_cleanup = NULL;
18171 dtrace_helpers_fork = NULL;
18172 dtrace_cpustart_init = NULL;
18173 dtrace_cpustart_fini = NULL;
18174 dtrace_debugger_init = NULL;
18175 dtrace_debugger_fini = NULL;
18176 dtrace_modload = NULL;
18177 dtrace_modunload = NULL;
18179 ASSERT(dtrace_getf == 0);
18180 ASSERT(dtrace_closef == NULL);
18182 mutex_exit(&cpu_lock);
18184 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *));
18185 dtrace_probes = NULL;
18186 dtrace_nprobes = 0;
18188 dtrace_hash_destroy(dtrace_bymod);
18189 dtrace_hash_destroy(dtrace_byfunc);
18190 dtrace_hash_destroy(dtrace_byname);
18191 dtrace_bymod = NULL;
18192 dtrace_byfunc = NULL;
18193 dtrace_byname = NULL;
18195 kmem_cache_destroy(dtrace_state_cache);
18196 vmem_destroy(dtrace_minor);
18197 vmem_destroy(dtrace_arena);
18199 if (dtrace_toxrange != NULL) {
18200 kmem_free(dtrace_toxrange,
18201 dtrace_toxranges_max * sizeof (dtrace_toxrange_t));
18202 dtrace_toxrange = NULL;
18203 dtrace_toxranges = 0;
18204 dtrace_toxranges_max = 0;
18207 ddi_remove_minor_node(dtrace_devi, NULL);
18208 dtrace_devi = NULL;
18210 ddi_soft_state_fini(&dtrace_softstate);
18212 ASSERT(dtrace_vtime_references == 0);
18213 ASSERT(dtrace_opens == 0);
18214 ASSERT(dtrace_retained == NULL);
18216 mutex_exit(&dtrace_lock);
18217 mutex_exit(&dtrace_provider_lock);
18220 * We don't destroy the task queue until after we have dropped our
18221 * locks (taskq_destroy() may block on running tasks). To prevent
18222 * attempting to do work after we have effectively detached but before
18223 * the task queue has been destroyed, all tasks dispatched via the
18224 * task queue must check that DTrace is still attached before
18225 * performing any operation.
18227 taskq_destroy(dtrace_taskq);
18228 dtrace_taskq = NULL;
18230 return (DDI_SUCCESS);
18237 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
18242 case DDI_INFO_DEVT2DEVINFO:
18243 *result = (void *)dtrace_devi;
18244 error = DDI_SUCCESS;
18246 case DDI_INFO_DEVT2INSTANCE:
18247 *result = (void *)0;
18248 error = DDI_SUCCESS;
18251 error = DDI_FAILURE;
18258 static struct cb_ops dtrace_cb_ops = {
18259 dtrace_open, /* open */
18260 dtrace_close, /* close */
18261 nulldev, /* strategy */
18262 nulldev, /* print */
18266 dtrace_ioctl, /* ioctl */
18267 nodev, /* devmap */
18269 nodev, /* segmap */
18270 nochpoll, /* poll */
18271 ddi_prop_op, /* cb_prop_op */
18273 D_NEW | D_MP /* Driver compatibility flag */
18276 static struct dev_ops dtrace_ops = {
18277 DEVO_REV, /* devo_rev */
18279 dtrace_info, /* get_dev_info */
18280 nulldev, /* identify */
18281 nulldev, /* probe */
18282 dtrace_attach, /* attach */
18283 dtrace_detach, /* detach */
18285 &dtrace_cb_ops, /* driver operations */
18286 NULL, /* bus operations */
18287 nodev /* dev power */
18290 static struct modldrv modldrv = {
18291 &mod_driverops, /* module type (this is a pseudo driver) */
18292 "Dynamic Tracing", /* name of module */
18293 &dtrace_ops, /* driver ops */
18296 static struct modlinkage modlinkage = {
18305 return (mod_install(&modlinkage));
18309 _info(struct modinfo *modinfop)
18311 return (mod_info(&modlinkage, modinfop));
18317 return (mod_remove(&modlinkage));
18321 static d_ioctl_t dtrace_ioctl;
18322 static d_ioctl_t dtrace_ioctl_helper;
18323 static void dtrace_load(void *);
18324 static int dtrace_unload(void);
18325 static struct cdev *dtrace_dev;
18326 static struct cdev *helper_dev;
18328 void dtrace_invop_init(void);
18329 void dtrace_invop_uninit(void);
18331 static struct cdevsw dtrace_cdevsw = {
18332 .d_version = D_VERSION,
18333 .d_ioctl = dtrace_ioctl,
18334 .d_open = dtrace_open,
18335 .d_name = "dtrace",
18338 static struct cdevsw helper_cdevsw = {
18339 .d_version = D_VERSION,
18340 .d_ioctl = dtrace_ioctl_helper,
18341 .d_name = "helper",
18344 #include <dtrace_anon.c>
18345 #include <dtrace_ioctl.c>
18346 #include <dtrace_load.c>
18347 #include <dtrace_modevent.c>
18348 #include <dtrace_sysctl.c>
18349 #include <dtrace_unload.c>
18350 #include <dtrace_vtime.c>
18351 #include <dtrace_hacks.c>
18352 #include <dtrace_isa.c>
18354 SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL);
18355 SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL);
18356 SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL);
18358 DEV_MODULE(dtrace, dtrace_modevent, NULL);
18359 MODULE_VERSION(dtrace, 1);
18360 MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1);