VirtualBox

source: vbox/trunk/src/VBox/ExtPacks/VBoxDTrace/onnv/uts/common/dtrace/dtrace.c@ 106921

Last change on this file since 106921 was 106921, checked in by vboxsync, 3 months ago

VBoxDTrace,VMM,IPRT: Made the ring-0 component of the DTrace extension pack compile on win.arm64. jiraref:VBP-1447

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 407.9 KB
Line 
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26/*
27 * DTrace - Dynamic Tracing for Solaris
28 *
29 * This is the implementation of the Solaris Dynamic Tracing framework
30 * (DTrace). The user-visible interface to DTrace is described at length in
31 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace
32 * library, the in-kernel DTrace framework, and the DTrace providers are
33 * described in the block comments in the <sys/dtrace.h> header file. The
34 * internal architecture of DTrace is described in the block comments in the
35 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace
36 * implementation very much assume mastery of all of these sources; if one has
37 * an unanswered question about the implementation, one should consult them
38 * first.
39 *
40 * The functions here are ordered roughly as follows:
41 *
42 * - Probe context functions
43 * - Probe hashing functions
44 * - Non-probe context utility functions
45 * - Matching functions
46 * - Provider-to-Framework API functions
47 * - Probe management functions
48 * - DIF object functions
49 * - Format functions
50 * - Predicate functions
51 * - ECB functions
52 * - Buffer functions
53 * - Enabling functions
54 * - DOF functions
55 * - Anonymous enabling functions
56 * - Consumer state functions
57 * - Helper functions
58 * - Hook functions
59 * - Driver cookbook functions
60 *
61 * Each group of functions begins with a block comment labelled the "DTrace
62 * [Group] Functions", allowing one to find each block by searching forward
63 * on capital-f functions.
64 */
65#ifndef VBOX
66#include <sys/errno.h>
67#include <sys/stat.h>
68#include <sys/modctl.h>
69#include <sys/conf.h>
70#include <sys/systm.h>
71#include <sys/ddi.h>
72#include <sys/sunddi.h>
73#include <sys/cpuvar.h>
74#include <sys/kmem.h>
75#include <sys/strsubr.h>
76#include <sys/sysmacros.h>
77#include <sys/dtrace_impl.h>
78#include <sys/atomic.h>
79#include <sys/cmn_err.h>
80#include <sys/mutex_impl.h>
81#include <sys/rwlock_impl.h>
82#include <sys/ctf_api.h>
83#include <sys/panic.h>
84#include <sys/priv_impl.h>
85#include <sys/policy.h>
86#include <sys/cred_impl.h>
87#include <sys/procfs_isa.h>
88#include <sys/taskq.h>
89#include <sys/mkdev.h>
90#include <sys/kdi.h>
91#include <sys/zone.h>
92#include <sys/socket.h>
93#include <netinet/in.h>
94
95#else /* VBOX */
96# include <sys/dtrace_impl.h>
97# include <VBox/sup.h>
98# include <iprt/assert.h>
99# include <iprt/cpuset.h>
100# include <iprt/err.h>
101# include <iprt/mem.h>
102# include <iprt/mp.h>
103# include <iprt/string.h>
104# include <iprt/process.h>
105# include <iprt/thread.h>
106# include <iprt/timer.h>
107# include <limits.h>
108
109# undef offsetof
110# define offsetof RT_OFFSETOF
111
112/*
113 * Use asm.h to implemente some of the simple stuff in dtrace_asm.s.
114 */
115# include <iprt/asm.h>
116# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
117# include <iprt/asm-amd64-x86.h>
118# elif defined(RT_ARCH_ARM64)
119# include <iprt/asm-arm.h>
120# endif
121# define dtrace_casptr(a_ppvDst, a_pvOld, a_pvNew) \
122 VBoxDtCompareAndSwapPtr((void * volatile *)a_ppvDst, a_pvOld, a_pvNew)
123DECLINLINE(void *) VBoxDtCompareAndSwapPtr(void * volatile *ppvDst, void *pvOld, void *pvNew)
124{
125 void *pvRet;
126 ASMAtomicCmpXchgExPtrVoid(ppvDst, pvNew, pvOld, &pvRet);
127 return pvRet;
128}
129
130# define dtrace_cas32(a_pu32Dst, a_pu32Old, a_pu32New) \
131 VBoxDtCompareAndSwapU32(a_pu32Dst, a_pu32Old, a_pu32New)
132DECLINLINE(uint32_t) VBoxDtCompareAndSwapU32(uint32_t volatile *pu32Dst, uint32_t u32Old, uint32_t u32New)
133{
134 uint32_t u32Ret;
135 ASMAtomicCmpXchgExU32(pu32Dst, u32New, u32Old, &u32Ret);
136 return u32Ret;
137}
138
139# define dtrace_membar_consumer() ASMReadFence()
140# define dtrace_membar_producer() ASMWriteFence()
141# define dtrace_interrupt_disable() ASMIntDisableFlags()
142# define dtrace_interrupt_enable(a_EFL) ASMSetFlags(a_EFL)
143
144/*
145 * NULL must be set to 0 or we'll end up with a billion warnings(=errors).
146 */
147# undef NULL
148# define NULL (0)
149#endif /* VBOX */
150
151/** Check if the given address is a valid kernel address.
152 * The value can be uintptr_t or uint64_t. */
153#ifndef VBOX
154# define VBDT_IS_VALID_KRNL_ADDR(a_uAddr) ((a_uAddr) >= KERNELBASE)
155#else
156# define VBDT_IS_VALID_KRNL_ADDR(a_uAddr) \
157 ( (sizeof(a_uAddr) == sizeof(uintptr_t) || (uintptr_t)(a_uAddr) == (a_uAddr)) \
158 && RTR0MemKernelIsValidAddr((void *)(uintptr_t)(a_uAddr)) )
159#endif
160
161
162/*
163 * DTrace Tunable Variables
164 *
165 * The following variables may be tuned by adding a line to /etc/system that
166 * includes both the name of the DTrace module ("dtrace") and the name of the
167 * variable. For example:
168 *
169 * set dtrace:dtrace_destructive_disallow = 1
170 *
171 * In general, the only variables that one should be tuning this way are those
172 * that affect system-wide DTrace behavior, and for which the default behavior
173 * is undesirable. Most of these variables are tunable on a per-consumer
174 * basis using DTrace options, and need not be tuned on a system-wide basis.
175 * When tuning these variables, avoid pathological values; while some attempt
176 * is made to verify the integrity of these variables, they are not considered
177 * part of the supported interface to DTrace, and they are therefore not
178 * checked comprehensively. Further, these variables should not be tuned
179 * dynamically via "mdb -kw" or other means; they should only be tuned via
180 * /etc/system.
181 */
182int dtrace_destructive_disallow = 0;
183dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024);
184size_t dtrace_difo_maxsize = (256 * 1024);
185dtrace_optval_t dtrace_dof_maxsize = (256 * 1024);
186size_t dtrace_global_maxsize = (16 * 1024);
187size_t dtrace_actions_max = (16 * 1024);
188size_t dtrace_retain_max = 1024;
189dtrace_optval_t dtrace_helper_actions_max = 32;
190dtrace_optval_t dtrace_helper_providers_max = 32;
191dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024);
192size_t dtrace_strsize_default = 256;
193dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */
194dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */
195dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */
196dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */
197dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */
198dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */
199dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */
200dtrace_optval_t dtrace_nspec_default = 1;
201dtrace_optval_t dtrace_specsize_default = 32 * 1024;
202dtrace_optval_t dtrace_stackframes_default = 20;
203dtrace_optval_t dtrace_ustackframes_default = 20;
204dtrace_optval_t dtrace_jstackframes_default = 50;
205dtrace_optval_t dtrace_jstackstrsize_default = 512;
206int dtrace_msgdsize_max = 128;
207hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */
208hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */
209int dtrace_devdepth_max = 32;
210int dtrace_err_verbose;
211hrtime_t dtrace_deadman_interval = NANOSEC;
212hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC;
213hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC;
214
215/*
216 * DTrace External Variables
217 *
218 * As dtrace(7D) is a kernel module, any DTrace variables are obviously
219 * available to DTrace consumers via the backtick (`) syntax. One of these,
220 * dtrace_zero, is made deliberately so: it is provided as a source of
221 * well-known, zero-filled memory. While this variable is not documented,
222 * it is used by some translators as an implementation detail.
223 */
224const char dtrace_zero[256] = { 0 }; /* zero-filled memory */
225
226/*
227 * DTrace Internal Variables
228 */
229#ifndef VBOX
230static dev_info_t *dtrace_devi; /* device info */
231#endif
232static vmem_t *dtrace_arena; /* probe ID arena */
233#ifndef VBOX
234static vmem_t *dtrace_minor; /* minor number arena */
235static taskq_t *dtrace_taskq; /* task queue */
236#endif
237static dtrace_probe_t **dtrace_probes; /* array of all probes */
238static VBDTTYPE(uint32_t,int) dtrace_nprobes; /* number of probes */
239static dtrace_provider_t *dtrace_provider; /* provider list */
240static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */
241static int dtrace_opens; /* number of opens */
242static int dtrace_helpers; /* number of helpers */
243#ifndef VBOX
244static void *dtrace_softstate; /* softstate pointer */
245#endif
246static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */
247static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */
248static dtrace_hash_t *dtrace_byname; /* probes hashed by name */
249static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */
250static int dtrace_toxranges; /* number of toxic ranges */
251static int dtrace_toxranges_max; /* size of toxic range array */
252static dtrace_anon_t dtrace_anon; /* anonymous enabling */
253static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */
254static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */
255#ifndef VBOX
256static kthread_t *dtrace_panicked; /* panicking thread */
257#endif
258static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */
259static dtrace_genid_t dtrace_probegen; /* current probe generation */
260static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */
261static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */
262static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */
263static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */
264static int dtrace_dynvar_failclean; /* dynvars failed to clean */
265
266/*
267 * DTrace Locking
268 * DTrace is protected by three (relatively coarse-grained) locks:
269 *
270 * (1) dtrace_lock is required to manipulate essentially any DTrace state,
271 * including enabling state, probes, ECBs, consumer state, helper state,
272 * etc. Importantly, dtrace_lock is _not_ required when in probe context;
273 * probe context is lock-free -- synchronization is handled via the
274 * dtrace_sync() cross call mechanism.
275 *
276 * (2) dtrace_provider_lock is required when manipulating provider state, or
277 * when provider state must be held constant.
278 *
279 * (3) dtrace_meta_lock is required when manipulating meta provider state, or
280 * when meta provider state must be held constant.
281 *
282 * The lock ordering between these three locks is dtrace_meta_lock before
283 * dtrace_provider_lock before dtrace_lock. (In particular, there are
284 * several places where dtrace_provider_lock is held by the framework as it
285 * calls into the providers -- which then call back into the framework,
286 * grabbing dtrace_lock.)
287 *
288 * There are two other locks in the mix: mod_lock and cpu_lock. With respect
289 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
290 * role as a coarse-grained lock; it is acquired before both of these locks.
291 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must
292 * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
293 * mod_lock is similar with respect to dtrace_provider_lock in that it must be
294 * acquired _between_ dtrace_provider_lock and dtrace_lock.
295 */
296static kmutex_t dtrace_lock; /* probe state lock */
297static kmutex_t dtrace_provider_lock; /* provider state lock */
298static kmutex_t dtrace_meta_lock; /* meta-provider state lock */
299
300/*
301 * DTrace Provider Variables
302 *
303 * These are the variables relating to DTrace as a provider (that is, the
304 * provider of the BEGIN, END, and ERROR probes).
305 */
306static dtrace_pattr_t dtrace_provider_attr = {
307{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
308{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
309{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
310{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
311{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
312};
313
314static void
315dtrace_nullop(void)
316{}
317
318static int
319dtrace_enable_nullop(void)
320{
321 return (0);
322}
323
324static dtrace_pops_t dtrace_provider_ops = {
325 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop,
326 (void (*)(void *, struct modctl *))dtrace_nullop,
327 (int (*)(void *, dtrace_id_t, void *))(uintptr_t)dtrace_enable_nullop,
328 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
329 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
330 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
331 NULL,
332 NULL,
333 NULL,
334 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop
335};
336
337static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */
338static dtrace_id_t dtrace_probeid_end; /* special END probe */
339dtrace_id_t dtrace_probeid_error; /* special ERROR probe */
340
341/*
342 * DTrace Helper Tracing Variables
343 */
344uint32_t dtrace_helptrace_next = 0;
345uint32_t dtrace_helptrace_nlocals;
346char *dtrace_helptrace_buffer;
347int dtrace_helptrace_bufsize = 512 * 1024;
348
349#ifdef DEBUG
350int dtrace_helptrace_enabled = 1;
351#else
352int dtrace_helptrace_enabled = 0;
353#endif
354
355/*
356 * DTrace Error Hashing
357 *
358 * On DEBUG kernels, DTrace will track the errors that has seen in a hash
359 * table. This is very useful for checking coverage of tests that are
360 * expected to induce DIF or DOF processing errors, and may be useful for
361 * debugging problems in the DIF code generator or in DOF generation . The
362 * error hash may be examined with the ::dtrace_errhash MDB dcmd.
363 */
364#ifdef DEBUG
365static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ];
366static const char *dtrace_errlast;
367static kthread_t *dtrace_errthread;
368static kmutex_t dtrace_errlock;
369#endif
370
371/*
372 * DTrace Macros and Constants
373 *
374 * These are various macros that are useful in various spots in the
375 * implementation, along with a few random constants that have no meaning
376 * outside of the implementation. There is no real structure to this cpp
377 * mishmash -- but is there ever?
378 */
379#define DTRACE_HASHSTR(hash, probe) \
380 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
381
382#define DTRACE_HASHNEXT(hash, probe) \
383 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
384
385#define DTRACE_HASHPREV(hash, probe) \
386 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
387
388#define DTRACE_HASHEQ(hash, lhs, rhs) \
389 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
390 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
391
392#define DTRACE_AGGHASHSIZE_SLEW 17
393
394#define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3)
395
396/*
397 * The key for a thread-local variable consists of the lower 61 bits of the
398 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
399 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
400 * equal to a variable identifier. This is necessary (but not sufficient) to
401 * assure that global associative arrays never collide with thread-local
402 * variables. To guarantee that they cannot collide, we must also define the
403 * order for keying dynamic variables. That order is:
404 *
405 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
406 *
407 * Because the variable-key and the tls-key are in orthogonal spaces, there is
408 * no way for a global variable key signature to match a thread-local key
409 * signature.
410 */
411#ifndef VBOX
412#define DTRACE_TLS_THRKEY(where) { \
413 uint_t intr = 0; \
414 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \
415 for (; actv; actv >>= 1) \
416 intr++; \
417 ASSERT(intr < (1 << 3)); \
418 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \
419 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
420}
421#else
422#define DTRACE_TLS_THRKEY(where) do { \
423 (where) = (((uintptr_t)RTThreadNativeSelf() + DIF_VARIABLE_MAX) & (RT_BIT_64(61) - 1)) \
424 | (RTThreadIsInInterrupt(NIL_RTTHREAD) ? RT_BIT_64(61) : 0); \
425} while (0)
426#endif
427
428#define DT_BSWAP_8(x) ((x) & 0xff)
429#define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
430#define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
431#define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
432
433#define DT_MASK_LO 0x00000000FFFFFFFFULL
434
435#define DTRACE_STORE(type, tomax, offset, what) \
436 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
437
438#ifndef __i386
439#define DTRACE_ALIGNCHECK(addr, size, flags) \
440 if (addr & (size - 1)) { \
441 *flags |= CPU_DTRACE_BADALIGN; \
442 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval = addr; \
443 return (0); \
444 }
445#else
446#define DTRACE_ALIGNCHECK(addr, size, flags)
447#endif
448
449/*
450 * Test whether a range of memory starting at testaddr of size testsz falls
451 * within the range of memory described by addr, sz. We take care to avoid
452 * problems with overflow and underflow of the unsigned quantities, and
453 * disallow all negative sizes. Ranges of size 0 are allowed.
454 */
455#define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
456 ((testaddr) - (baseaddr) < (basesz) && \
457 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \
458 (testaddr) + (testsz) >= (testaddr))
459
460/*
461 * Test whether alloc_sz bytes will fit in the scratch region. We isolate
462 * alloc_sz on the righthand side of the comparison in order to avoid overflow
463 * or underflow in the comparison with it. This is simpler than the INRANGE
464 * check above, because we know that the dtms_scratch_ptr is valid in the
465 * range. Allocations of size zero are allowed.
466 */
467#define DTRACE_INSCRATCH(mstate, alloc_sz) \
468 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
469 (mstate)->dtms_scratch_ptr >= (alloc_sz))
470
471#ifndef VBOX
472#define DTRACE_LOADFUNC(bits) \
473/*CSTYLED*/ \
474VBDTSTATIC uint##bits##_t \
475dtrace_load##bits(uintptr_t addr) \
476{ \
477 size_t size = bits / NBBY; \
478 /*CSTYLED*/ \
479 uint##bits##_t rval; \
480 int i; \
481 processorid_t me = VBDT_GET_CPUID(); \
482 volatile uint16_t *flags = (volatile uint16_t *) \
483 &cpu_core[me].cpuc_dtrace_flags; \
484 \
485 DTRACE_ALIGNCHECK(addr, size, flags); \
486 \
487 for (i = 0; i < dtrace_toxranges; i++) { \
488 if (addr >= dtrace_toxrange[i].dtt_limit) \
489 continue; \
490 \
491 if (addr + size <= dtrace_toxrange[i].dtt_base) \
492 continue; \
493 \
494 /* \
495 * This address falls within a toxic region; return 0. \
496 */ \
497 *flags |= CPU_DTRACE_BADADDR; \
498 cpu_core[me].cpuc_dtrace_illval = addr; \
499 return (0); \
500 } \
501 \
502 *flags |= CPU_DTRACE_NOFAULT; \
503 /*CSTYLED*/ \
504 rval = *((volatile uint##bits##_t *)addr); \
505 *flags &= ~CPU_DTRACE_NOFAULT; \
506 \
507 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \
508}
509#else /* VBOX */
510# define DTRACE_LOADFUNC(bits) \
511VBDTSTATIC uint##bits##_t \
512dtrace_load##bits(uintptr_t addr) \
513{ \
514 size_t const size = bits / NBBY; \
515 uint##bits##_t rval; \
516 processorid_t me; \
517 int i, rc; \
518 \
519 /*DTRACE_ALIGNCHECK(addr, size, flags);*/ \
520 \
521 for (i = 0; i < dtrace_toxranges; i++) { \
522 if (addr >= dtrace_toxrange[i].dtt_limit) \
523 continue; \
524 \
525 if (addr + size <= dtrace_toxrange[i].dtt_base) \
526 continue; \
527 \
528 /* \
529 * This address falls within a toxic region; return 0. \
530 */ \
531 me = VBDT_GET_CPUID(); \
532 cpu_core[me].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; \
533 cpu_core[me].cpuc_dtrace_illval = addr; \
534 return (0); \
535 } \
536 \
537 rc = RTR0MemKernelCopyFrom(&rval, (void const *)addr, size); \
538 if (RT_SUCCESS(rc)) \
539 return rval; \
540 \
541 /* \
542 * If not supported, pray it won't fault... \
543 */ \
544 if (rc == VERR_NOT_SUPPORTED) \
545 return *(uint##bits##_t const *)addr; \
546 \
547 me = VBDT_GET_CPUID(); \
548 cpu_core[me].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; \
549 cpu_core[me].cpuc_dtrace_illval = addr; \
550 return (0); \
551}
552
553#endif /* VBOX */
554
555#ifdef _LP64
556#define dtrace_loadptr dtrace_load64
557#else
558#define dtrace_loadptr dtrace_load32
559#endif
560
561#define DTRACE_DYNHASH_FREE 0
562#define DTRACE_DYNHASH_SINK 1
563#define DTRACE_DYNHASH_VALID 2
564
565#define DTRACE_MATCH_FAIL -1
566#define DTRACE_MATCH_NEXT 0
567#define DTRACE_MATCH_DONE 1
568#define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0')
569#define DTRACE_STATE_ALIGN 64
570
571#define DTRACE_FLAGS2FLT(flags) \
572 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \
573 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \
574 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \
575 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \
576 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \
577 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \
578 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \
579 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \
580 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \
581 DTRACEFLT_UNKNOWN)
582
583#define DTRACEACT_ISSTRING(act) \
584 ((act)->dta_kind == DTRACEACT_DIFEXPR && \
585 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
586
587static size_t dtrace_strlen(const char *, size_t);
588static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id);
589static void dtrace_enabling_provide(dtrace_provider_t *);
590static int dtrace_enabling_match(dtrace_enabling_t *, int *);
591static void dtrace_enabling_matchall(void);
592static dtrace_state_t *dtrace_anon_grab(void);
593#ifndef VBOX
594static uint64_t dtrace_helper(int, dtrace_mstate_t *,
595 dtrace_state_t *, uint64_t, uint64_t);
596static dtrace_helpers_t *dtrace_helpers_create(proc_t *);
597#endif
598static void dtrace_buffer_drop(dtrace_buffer_t *);
599static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t,
600 dtrace_state_t *, dtrace_mstate_t *);
601static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t,
602 dtrace_optval_t);
603static int dtrace_ecb_create_enable(dtrace_probe_t *, void *);
604#ifndef VBOX
605static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *);
606#endif
607
608/*
609 * DTrace Probe Context Functions
610 *
611 * These functions are called from probe context. Because probe context is
612 * any context in which C may be called, arbitrarily locks may be held,
613 * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
614 * As a result, functions called from probe context may only call other DTrace
615 * support functions -- they may not interact at all with the system at large.
616 * (Note that the ASSERT macro is made probe-context safe by redefining it in
617 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
618 * loads are to be performed from probe context, they _must_ be in terms of
619 * the safe dtrace_load*() variants.
620 *
621 * Some functions in this block are not actually called from probe context;
622 * for these functions, there will be a comment above the function reading
623 * "Note: not called from probe context."
624 */
625void
626dtrace_panic(const char *format, ...)
627{
628 va_list alist;
629
630 va_start(alist, format);
631 dtrace_vpanic(format, alist);
632 va_end(alist);
633}
634
635#ifndef VBOX /* We have our own assertion machinery. */
636int
637dtrace_assfail(const char *a, const char *f, int l)
638{
639 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l);
640
641 /*
642 * We just need something here that even the most clever compiler
643 * cannot optimize away.
644 */
645 return (a[(uintptr_t)f]);
646}
647#endif
648
649/*
650 * Atomically increment a specified error counter from probe context.
651 */
652static void
653dtrace_error(uint32_t *counter)
654{
655 /*
656 * Most counters stored to in probe context are per-CPU counters.
657 * However, there are some error conditions that are sufficiently
658 * arcane that they don't merit per-CPU storage. If these counters
659 * are incremented concurrently on different CPUs, scalability will be
660 * adversely affected -- but we don't expect them to be white-hot in a
661 * correctly constructed enabling...
662 */
663 uint32_t oval, nval;
664
665 do {
666 oval = *counter;
667
668 if ((nval = oval + 1) == 0) {
669 /*
670 * If the counter would wrap, set it to 1 -- assuring
671 * that the counter is never zero when we have seen
672 * errors. (The counter must be 32-bits because we
673 * aren't guaranteed a 64-bit compare&swap operation.)
674 * To save this code both the infamy of being fingered
675 * by a priggish news story and the indignity of being
676 * the target of a neo-puritan witch trial, we're
677 * carefully avoiding any colorful description of the
678 * likelihood of this condition -- but suffice it to
679 * say that it is only slightly more likely than the
680 * overflow of predicate cache IDs, as discussed in
681 * dtrace_predicate_create().
682 */
683 nval = 1;
684 }
685 } while (dtrace_cas32(counter, oval, nval) != oval);
686}
687
688/*
689 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
690 * uint8_t, a uint16_t, a uint32_t and a uint64_t.
691 */
692DTRACE_LOADFUNC(8)
693DTRACE_LOADFUNC(16)
694DTRACE_LOADFUNC(32)
695DTRACE_LOADFUNC(64)
696
697static int
698dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate)
699{
700 if (dest < mstate->dtms_scratch_base)
701 return (0);
702
703 if (dest + size < dest)
704 return (0);
705
706 if (dest + size > mstate->dtms_scratch_ptr)
707 return (0);
708
709 return (1);
710}
711
712static int
713dtrace_canstore_statvar(uint64_t addr, size_t sz,
714 dtrace_statvar_t **svars, int nsvars)
715{
716 int i;
717
718 for (i = 0; i < nsvars; i++) {
719 dtrace_statvar_t *svar = svars[i];
720
721 if (svar == NULL || svar->dtsv_size == 0)
722 continue;
723
724 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size))
725 return (1);
726 }
727
728 return (0);
729}
730
731/*
732 * Check to see if the address is within a memory region to which a store may
733 * be issued. This includes the DTrace scratch areas, and any DTrace variable
734 * region. The caller of dtrace_canstore() is responsible for performing any
735 * alignment checks that are needed before stores are actually executed.
736 */
737static int
738dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
739 dtrace_vstate_t *vstate)
740{
741 /*
742 * First, check to see if the address is in scratch space...
743 */
744 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base,
745 mstate->dtms_scratch_size))
746 return (1);
747
748 /*
749 * Now check to see if it's a dynamic variable. This check will pick
750 * up both thread-local variables and any global dynamically-allocated
751 * variables.
752 */
753 if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base,
754 vstate->dtvs_dynvars.dtds_size)) {
755 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
756 uintptr_t base = (uintptr_t)dstate->dtds_base +
757 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t));
758 uintptr_t chunkoffs;
759
760 /*
761 * Before we assume that we can store here, we need to make
762 * sure that it isn't in our metadata -- storing to our
763 * dynamic variable metadata would corrupt our state. For
764 * the range to not include any dynamic variable metadata,
765 * it must:
766 *
767 * (1) Start above the hash table that is at the base of
768 * the dynamic variable space
769 *
770 * (2) Have a starting chunk offset that is beyond the
771 * dtrace_dynvar_t that is at the base of every chunk
772 *
773 * (3) Not span a chunk boundary
774 *
775 */
776 if (addr < base)
777 return (0);
778
779 chunkoffs = (addr - base) % dstate->dtds_chunksize;
780
781 if (chunkoffs < sizeof (dtrace_dynvar_t))
782 return (0);
783
784 if (chunkoffs + sz > dstate->dtds_chunksize)
785 return (0);
786
787 return (1);
788 }
789
790 /*
791 * Finally, check the static local and global variables. These checks
792 * take the longest, so we perform them last.
793 */
794 if (dtrace_canstore_statvar(addr, sz,
795 vstate->dtvs_locals, vstate->dtvs_nlocals))
796 return (1);
797
798 if (dtrace_canstore_statvar(addr, sz,
799 vstate->dtvs_globals, vstate->dtvs_nglobals))
800 return (1);
801
802 return (0);
803}
804
805
806/*
807 * Convenience routine to check to see if the address is within a memory
808 * region in which a load may be issued given the user's privilege level;
809 * if not, it sets the appropriate error flags and loads 'addr' into the
810 * illegal value slot.
811 *
812 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
813 * appropriate memory access protection.
814 */
815static int
816dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
817 dtrace_vstate_t *vstate)
818{
819 volatile uintptr_t *illval = &cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval;
820
821 /*
822 * If we hold the privilege to read from kernel memory, then
823 * everything is readable.
824 */
825 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
826 return (1);
827
828 /*
829 * You can obviously read that which you can store.
830 */
831 if (dtrace_canstore(addr, sz, mstate, vstate))
832 return (1);
833
834 /*
835 * We're allowed to read from our own string table.
836 */
837 if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab,
838 mstate->dtms_difo->dtdo_strlen))
839 return (1);
840
841 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
842 *illval = addr;
843 return (0);
844}
845
846/*
847 * Convenience routine to check to see if a given string is within a memory
848 * region in which a load may be issued given the user's privilege level;
849 * this exists so that we don't need to issue unnecessary dtrace_strlen()
850 * calls in the event that the user has all privileges.
851 */
852static int
853dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
854 dtrace_vstate_t *vstate)
855{
856 size_t strsz;
857
858 /*
859 * If we hold the privilege to read from kernel memory, then
860 * everything is readable.
861 */
862 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
863 return (1);
864
865 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz);
866 if (dtrace_canload(addr, strsz, mstate, vstate))
867 return (1);
868
869 return (0);
870}
871
872/*
873 * Convenience routine to check to see if a given variable is within a memory
874 * region in which a load may be issued given the user's privilege level.
875 */
876static int
877dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate,
878 dtrace_vstate_t *vstate)
879{
880 size_t sz;
881 ASSERT(type->dtdt_flags & DIF_TF_BYREF);
882
883 /*
884 * If we hold the privilege to read from kernel memory, then
885 * everything is readable.
886 */
887 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
888 return (1);
889
890 if (type->dtdt_kind == DIF_TYPE_STRING)
891 sz = dtrace_strlen(src,
892 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1;
893 else
894 sz = type->dtdt_size;
895
896 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate));
897}
898
899/*
900 * Compare two strings using safe loads.
901 */
902static int
903dtrace_strncmp(char *s1, char *s2, size_t limit)
904{
905 uint8_t c1, c2;
906 volatile uint16_t *flags;
907
908 if (s1 == s2 || limit == 0)
909 return (0);
910
911 flags = (volatile uint16_t *)&cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags;
912
913 do {
914 if (s1 == NULL) {
915 c1 = '\0';
916 } else {
917 c1 = dtrace_load8((uintptr_t)s1++);
918 }
919
920 if (s2 == NULL) {
921 c2 = '\0';
922 } else {
923 c2 = dtrace_load8((uintptr_t)s2++);
924 }
925
926 if (c1 != c2)
927 return (c1 - c2);
928 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT));
929
930 return (0);
931}
932
933/*
934 * Compute strlen(s) for a string using safe memory accesses. The additional
935 * len parameter is used to specify a maximum length to ensure completion.
936 */
937static size_t
938dtrace_strlen(const char *s, size_t lim)
939{
940 uint_t len;
941
942 for (len = 0; len != lim; len++) {
943 if (dtrace_load8((uintptr_t)s++) == '\0')
944 break;
945 }
946
947 return (len);
948}
949
950/*
951 * Check if an address falls within a toxic region.
952 */
953static int
954dtrace_istoxic(uintptr_t kaddr, size_t size)
955{
956 uintptr_t taddr, tsize;
957 int i;
958
959 for (i = 0; i < dtrace_toxranges; i++) {
960 taddr = dtrace_toxrange[i].dtt_base;
961 tsize = dtrace_toxrange[i].dtt_limit - taddr;
962
963 if (kaddr - taddr < tsize) {
964 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
965 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval = kaddr;
966 return (1);
967 }
968
969 if (taddr - kaddr < size) {
970 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
971 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval = taddr;
972 return (1);
973 }
974 }
975
976 return (0);
977}
978
979/*
980 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe
981 * memory specified by the DIF program. The dst is assumed to be safe memory
982 * that we can store to directly because it is managed by DTrace. As with
983 * standard bcopy, overlapping copies are handled properly.
984 */
985static void
986dtrace_bcopy(const void *src, void *dst, size_t len)
987{
988 if (len != 0) {
989 uint8_t *s1 = dst;
990 const uint8_t *s2 = src;
991
992 if (s1 <= s2) {
993 do {
994 *s1++ = dtrace_load8((uintptr_t)s2++);
995 } while (--len != 0);
996 } else {
997 s2 += len;
998 s1 += len;
999
1000 do {
1001 *--s1 = dtrace_load8((uintptr_t)--s2);
1002 } while (--len != 0);
1003 }
1004 }
1005}
1006
1007/*
1008 * Copy src to dst using safe memory accesses, up to either the specified
1009 * length, or the point that a nul byte is encountered. The src is assumed to
1010 * be unsafe memory specified by the DIF program. The dst is assumed to be
1011 * safe memory that we can store to directly because it is managed by DTrace.
1012 * Unlike dtrace_bcopy(), overlapping regions are not handled.
1013 */
1014static void
1015dtrace_strcpy(const void *src, void *dst, size_t len)
1016{
1017 if (len != 0) {
1018 uint8_t *s1 = dst, c;
1019 const uint8_t *s2 = src;
1020
1021 do {
1022 *s1++ = c = dtrace_load8((uintptr_t)s2++);
1023 } while (--len != 0 && c != '\0');
1024 }
1025}
1026
1027/*
1028 * Copy src to dst, deriving the size and type from the specified (BYREF)
1029 * variable type. The src is assumed to be unsafe memory specified by the DIF
1030 * program. The dst is assumed to be DTrace variable memory that is of the
1031 * specified type; we assume that we can store to directly.
1032 */
1033static void
1034dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type)
1035{
1036 ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1037
1038 if (type->dtdt_kind == DIF_TYPE_STRING) {
1039 dtrace_strcpy(src, dst, type->dtdt_size);
1040 } else {
1041 dtrace_bcopy(src, dst, type->dtdt_size);
1042 }
1043}
1044
1045/*
1046 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be
1047 * unsafe memory specified by the DIF program. The s2 data is assumed to be
1048 * safe memory that we can access directly because it is managed by DTrace.
1049 */
1050static int
1051dtrace_bcmp(const void *s1, const void *s2, size_t len)
1052{
1053 volatile uint16_t *flags;
1054
1055 flags = (volatile uint16_t *)&cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags;
1056
1057 if (s1 == s2)
1058 return (0);
1059
1060 if (s1 == NULL || s2 == NULL)
1061 return (1);
1062
1063 if (s1 != s2 && len != 0) {
1064 const uint8_t *ps1 = s1;
1065 const uint8_t *ps2 = s2;
1066
1067 do {
1068 if (dtrace_load8((uintptr_t)ps1++) != *ps2++)
1069 return (1);
1070 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT));
1071 }
1072 return (0);
1073}
1074
1075/*
1076 * Zero the specified region using a simple byte-by-byte loop. Note that this
1077 * is for safe DTrace-managed memory only.
1078 */
1079static void
1080dtrace_bzero(void *dst, size_t len)
1081{
1082 uchar_t *cp;
1083
1084 for (cp = dst; len != 0; len--)
1085 *cp++ = 0;
1086}
1087
1088static void
1089dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
1090{
1091 uint64_t result[2];
1092
1093 result[0] = addend1[0] + addend2[0];
1094 result[1] = addend1[1] + addend2[1] +
1095 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
1096
1097 sum[0] = result[0];
1098 sum[1] = result[1];
1099}
1100
1101/*
1102 * Shift the 128-bit value in a by b. If b is positive, shift left.
1103 * If b is negative, shift right.
1104 */
1105static void
1106dtrace_shift_128(uint64_t *a, int b)
1107{
1108 uint64_t mask;
1109
1110 if (b == 0)
1111 return;
1112
1113 if (b < 0) {
1114 b = -b;
1115 if (b >= 64) {
1116 a[0] = a[1] >> (b - 64);
1117 a[1] = 0;
1118 } else {
1119 a[0] >>= b;
1120 mask = 1LL << (64 - b);
1121 mask -= 1;
1122 a[0] |= ((a[1] & mask) << (64 - b));
1123 a[1] >>= b;
1124 }
1125 } else {
1126 if (b >= 64) {
1127 a[1] = a[0] << (b - 64);
1128 a[0] = 0;
1129 } else {
1130 a[1] <<= b;
1131 mask = a[0] >> (64 - b);
1132 a[1] |= mask;
1133 a[0] <<= b;
1134 }
1135 }
1136}
1137
1138/*
1139 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1140 * use native multiplication on those, and then re-combine into the
1141 * resulting 128-bit value.
1142 *
1143 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1144 * hi1 * hi2 << 64 +
1145 * hi1 * lo2 << 32 +
1146 * hi2 * lo1 << 32 +
1147 * lo1 * lo2
1148 */
1149static void
1150dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
1151{
1152 uint64_t hi1, hi2, lo1, lo2;
1153 uint64_t tmp[2];
1154
1155 hi1 = factor1 >> 32;
1156 hi2 = factor2 >> 32;
1157
1158 lo1 = factor1 & DT_MASK_LO;
1159 lo2 = factor2 & DT_MASK_LO;
1160
1161 product[0] = lo1 * lo2;
1162 product[1] = hi1 * hi2;
1163
1164 tmp[0] = hi1 * lo2;
1165 tmp[1] = 0;
1166 dtrace_shift_128(tmp, 32);
1167 dtrace_add_128(product, tmp, product);
1168
1169 tmp[0] = hi2 * lo1;
1170 tmp[1] = 0;
1171 dtrace_shift_128(tmp, 32);
1172 dtrace_add_128(product, tmp, product);
1173}
1174
1175/*
1176 * This privilege check should be used by actions and subroutines to
1177 * verify that the user credentials of the process that enabled the
1178 * invoking ECB match the target credentials
1179 */
1180static int
1181dtrace_priv_proc_common_user(dtrace_state_t *state)
1182{
1183 cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1184
1185 /*
1186 * We should always have a non-NULL state cred here, since if cred
1187 * is null (anonymous tracing), we fast-path bypass this routine.
1188 */
1189 ASSERT(s_cr != NULL);
1190
1191 if ((cr = CRED()) != NULL &&
1192 s_cr->cr_uid == cr->cr_uid &&
1193 s_cr->cr_uid == cr->cr_ruid &&
1194 s_cr->cr_uid == cr->cr_suid &&
1195 s_cr->cr_gid == cr->cr_gid &&
1196 s_cr->cr_gid == cr->cr_rgid &&
1197 s_cr->cr_gid == cr->cr_sgid)
1198 return (1);
1199
1200 return (0);
1201}
1202
1203/*
1204 * This privilege check should be used by actions and subroutines to
1205 * verify that the zone of the process that enabled the invoking ECB
1206 * matches the target credentials
1207 */
1208static int
1209dtrace_priv_proc_common_zone(dtrace_state_t *state)
1210{
1211 cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1212
1213 /*
1214 * We should always have a non-NULL state cred here, since if cred
1215 * is null (anonymous tracing), we fast-path bypass this routine.
1216 */
1217 ASSERT(s_cr != NULL);
1218
1219 if ((cr = CRED()) != NULL &&
1220 s_cr->cr_zone == cr->cr_zone)
1221 return (1);
1222
1223 return (0);
1224}
1225
1226/*
1227 * This privilege check should be used by actions and subroutines to
1228 * verify that the process has not setuid or changed credentials.
1229 */
1230static int
1231dtrace_priv_proc_common_nocd(VBDTVOID)
1232{
1233#ifndef VBOX
1234 proc_t *proc;
1235
1236 if ((proc = VBDT_GET_PROC()) != NULL &&
1237 !(proc->p_flag & SNOCD))
1238 return (1);
1239
1240 return (0);
1241#else
1242 return (1);
1243#endif
1244}
1245
1246#ifndef VBOX
1247static int
1248dtrace_priv_proc_destructive(dtrace_state_t *state)
1249{
1250 int action = state->dts_cred.dcr_action;
1251
1252 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) &&
1253 dtrace_priv_proc_common_zone(state) == 0)
1254 goto bad;
1255
1256 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) &&
1257 dtrace_priv_proc_common_user(state) == 0)
1258 goto bad;
1259
1260 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) &&
1261 dtrace_priv_proc_common_nocd() == 0)
1262 goto bad;
1263
1264 return (1);
1265
1266bad:
1267 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1268
1269 return (0);
1270}
1271#endif /* !VBOX */
1272
1273static int
1274dtrace_priv_proc_control(dtrace_state_t *state)
1275{
1276 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL)
1277 return (1);
1278
1279 if (dtrace_priv_proc_common_zone(state) &&
1280 dtrace_priv_proc_common_user(state) &&
1281 dtrace_priv_proc_common_nocd())
1282 return (1);
1283
1284 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1285
1286 return (0);
1287}
1288
1289static int
1290dtrace_priv_proc(dtrace_state_t *state)
1291{
1292 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC)
1293 return (1);
1294
1295 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1296
1297 return (0);
1298}
1299
1300static int
1301dtrace_priv_kernel(dtrace_state_t *state)
1302{
1303 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL)
1304 return (1);
1305
1306 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1307
1308 return (0);
1309}
1310
1311static int
1312dtrace_priv_kernel_destructive(dtrace_state_t *state)
1313{
1314 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE)
1315 return (1);
1316
1317 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1318
1319 return (0);
1320}
1321
1322/*
1323 * Note: not called from probe context. This function is called
1324 * asynchronously (and at a regular interval) from outside of probe context to
1325 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable
1326 * cleaning is explained in detail in <sys/dtrace_impl.h>.
1327 */
1328VBDTSTATIC void
1329dtrace_dynvar_clean(dtrace_dstate_t *dstate)
1330{
1331 dtrace_dynvar_t *dirty;
1332 dtrace_dstate_percpu_t *dcpu;
1333 dtrace_dynvar_t **rinsep;
1334 int i, j, work = 0;
1335
1336 for (i = 0; i < NCPU; i++) {
1337 dcpu = &dstate->dtds_percpu[i];
1338 rinsep = &dcpu->dtdsc_rinsing;
1339
1340 /*
1341 * If the dirty list is NULL, there is no dirty work to do.
1342 */
1343 if (dcpu->dtdsc_dirty == NULL)
1344 continue;
1345
1346 if (dcpu->dtdsc_rinsing != NULL) {
1347 /*
1348 * If the rinsing list is non-NULL, then it is because
1349 * this CPU was selected to accept another CPU's
1350 * dirty list -- and since that time, dirty buffers
1351 * have accumulated. This is a highly unlikely
1352 * condition, but we choose to ignore the dirty
1353 * buffers -- they'll be picked up a future cleanse.
1354 */
1355 continue;
1356 }
1357
1358 if (dcpu->dtdsc_clean != NULL) {
1359 /*
1360 * If the clean list is non-NULL, then we're in a
1361 * situation where a CPU has done deallocations (we
1362 * have a non-NULL dirty list) but no allocations (we
1363 * also have a non-NULL clean list). We can't simply
1364 * move the dirty list into the clean list on this
1365 * CPU, yet we also don't want to allow this condition
1366 * to persist, lest a short clean list prevent a
1367 * massive dirty list from being cleaned (which in
1368 * turn could lead to otherwise avoidable dynamic
1369 * drops). To deal with this, we look for some CPU
1370 * with a NULL clean list, NULL dirty list, and NULL
1371 * rinsing list -- and then we borrow this CPU to
1372 * rinse our dirty list.
1373 */
1374 for (j = 0; j < NCPU; j++) {
1375 dtrace_dstate_percpu_t *rinser;
1376
1377 rinser = &dstate->dtds_percpu[j];
1378
1379 if (rinser->dtdsc_rinsing != NULL)
1380 continue;
1381
1382 if (rinser->dtdsc_dirty != NULL)
1383 continue;
1384
1385 if (rinser->dtdsc_clean != NULL)
1386 continue;
1387
1388 rinsep = &rinser->dtdsc_rinsing;
1389 break;
1390 }
1391
1392 if (j == NCPU) {
1393 /*
1394 * We were unable to find another CPU that
1395 * could accept this dirty list -- we are
1396 * therefore unable to clean it now.
1397 */
1398 dtrace_dynvar_failclean++;
1399 continue;
1400 }
1401 }
1402
1403 work = 1;
1404
1405 /*
1406 * Atomically move the dirty list aside.
1407 */
1408 do {
1409 dirty = dcpu->dtdsc_dirty;
1410
1411 /*
1412 * Before we zap the dirty list, set the rinsing list.
1413 * (This allows for a potential assertion in
1414 * dtrace_dynvar(): if a free dynamic variable appears
1415 * on a hash chain, either the dirty list or the
1416 * rinsing list for some CPU must be non-NULL.)
1417 */
1418 *rinsep = dirty;
1419 dtrace_membar_producer();
1420 } while (dtrace_casptr(&dcpu->dtdsc_dirty,
1421 dirty, NULL) != dirty);
1422 }
1423
1424 if (!work) {
1425 /*
1426 * We have no work to do; we can simply return.
1427 */
1428 return;
1429 }
1430
1431 dtrace_sync();
1432
1433 for (i = 0; i < NCPU; i++) {
1434 dcpu = &dstate->dtds_percpu[i];
1435
1436 if (dcpu->dtdsc_rinsing == NULL)
1437 continue;
1438
1439 /*
1440 * We are now guaranteed that no hash chain contains a pointer
1441 * into this dirty list; we can make it clean.
1442 */
1443 ASSERT(dcpu->dtdsc_clean == NULL);
1444 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing;
1445 dcpu->dtdsc_rinsing = NULL;
1446 }
1447
1448 /*
1449 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1450 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1451 * This prevents a race whereby a CPU incorrectly decides that
1452 * the state should be something other than DTRACE_DSTATE_CLEAN
1453 * after dtrace_dynvar_clean() has completed.
1454 */
1455 dtrace_sync();
1456
1457 dstate->dtds_state = DTRACE_DSTATE_CLEAN;
1458}
1459
1460/*
1461 * Depending on the value of the op parameter, this function looks-up,
1462 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an
1463 * allocation is requested, this function will return a pointer to a
1464 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1465 * variable can be allocated. If NULL is returned, the appropriate counter
1466 * will be incremented.
1467 */
1468VBDTSTATIC dtrace_dynvar_t *
1469dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys,
1470 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op,
1471 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1472{
1473 uint64_t hashval = DTRACE_DYNHASH_VALID;
1474 dtrace_dynhash_t *hash = dstate->dtds_hash;
1475 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL;
1476 processorid_t me = VBDT_GET_CPUID(), cpu = me;
1477 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me];
1478 size_t bucket, ksize;
1479 size_t chunksize = dstate->dtds_chunksize;
1480 uintptr_t kdata, lock, nstate;
1481 uint_t i;
1482
1483 ASSERT(nkeys != 0);
1484
1485 /*
1486 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time"
1487 * algorithm. For the by-value portions, we perform the algorithm in
1488 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a
1489 * bit, and seems to have only a minute effect on distribution. For
1490 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1491 * over each referenced byte. It's painful to do this, but it's much
1492 * better than pathological hash distribution. The efficacy of the
1493 * hashing algorithm (and a comparison with other algorithms) may be
1494 * found by running the ::dtrace_dynstat MDB dcmd.
1495 */
1496 for (i = 0; i < nkeys; i++) {
1497 if (key[i].dttk_size == 0) {
1498 uint64_t val = key[i].dttk_value;
1499
1500 hashval += (val >> 48) & 0xffff;
1501 hashval += (hashval << 10);
1502 hashval ^= (hashval >> 6);
1503
1504 hashval += (val >> 32) & 0xffff;
1505 hashval += (hashval << 10);
1506 hashval ^= (hashval >> 6);
1507
1508 hashval += (val >> 16) & 0xffff;
1509 hashval += (hashval << 10);
1510 hashval ^= (hashval >> 6);
1511
1512 hashval += val & 0xffff;
1513 hashval += (hashval << 10);
1514 hashval ^= (hashval >> 6);
1515 } else {
1516 /*
1517 * This is incredibly painful, but it beats the hell
1518 * out of the alternative.
1519 */
1520 uint64_t j, size = key[i].dttk_size;
1521 uintptr_t base = (uintptr_t)key[i].dttk_value;
1522
1523 if (!dtrace_canload(base, size, mstate, vstate))
1524 break;
1525
1526 for (j = 0; j < size; j++) {
1527 hashval += dtrace_load8(base + j);
1528 hashval += (hashval << 10);
1529 hashval ^= (hashval >> 6);
1530 }
1531 }
1532 }
1533
1534 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
1535 return (NULL);
1536
1537 hashval += (hashval << 3);
1538 hashval ^= (hashval >> 11);
1539 hashval += (hashval << 15);
1540
1541 /*
1542 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1543 * comes out to be one of our two sentinel hash values. If this
1544 * actually happens, we set the hashval to be a value known to be a
1545 * non-sentinel value.
1546 */
1547 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK)
1548 hashval = DTRACE_DYNHASH_VALID;
1549
1550 /*
1551 * Yes, it's painful to do a divide here. If the cycle count becomes
1552 * important here, tricks can be pulled to reduce it. (However, it's
1553 * critical that hash collisions be kept to an absolute minimum;
1554 * they're much more painful than a divide.) It's better to have a
1555 * solution that generates few collisions and still keeps things
1556 * relatively simple.
1557 */
1558 bucket = hashval % dstate->dtds_hashsize;
1559
1560 if (op == DTRACE_DYNVAR_DEALLOC) {
1561 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock;
1562
1563 for (;;) {
1564 while ((lock = *lockp) & 1)
1565 continue;
1566
1567 if (dtrace_casptr((void *)lockp,
1568 (void *)lock, (void *)(lock + 1)) == (void *)lock)
1569 break;
1570 }
1571
1572 dtrace_membar_producer();
1573 }
1574
1575top:
1576 prev = NULL;
1577 lock = hash[bucket].dtdh_lock;
1578
1579 dtrace_membar_consumer();
1580
1581 start = hash[bucket].dtdh_chain;
1582 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK ||
1583 start->dtdv_hashval != DTRACE_DYNHASH_FREE ||
1584 op != DTRACE_DYNVAR_DEALLOC));
1585
1586 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) {
1587 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple;
1588 dtrace_key_t *dkey = &dtuple->dtt_key[0];
1589
1590 if (dvar->dtdv_hashval != hashval) {
1591 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) {
1592 /*
1593 * We've reached the sink, and therefore the
1594 * end of the hash chain; we can kick out of
1595 * the loop knowing that we have seen a valid
1596 * snapshot of state.
1597 */
1598 ASSERT(dvar->dtdv_next == NULL);
1599 ASSERT(dvar == &dtrace_dynhash_sink);
1600 break;
1601 }
1602
1603 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) {
1604 /*
1605 * We've gone off the rails: somewhere along
1606 * the line, one of the members of this hash
1607 * chain was deleted. Note that we could also
1608 * detect this by simply letting this loop run
1609 * to completion, as we would eventually hit
1610 * the end of the dirty list. However, we
1611 * want to avoid running the length of the
1612 * dirty list unnecessarily (it might be quite
1613 * long), so we catch this as early as
1614 * possible by detecting the hash marker. In
1615 * this case, we simply set dvar to NULL and
1616 * break; the conditional after the loop will
1617 * send us back to top.
1618 */
1619 dvar = NULL;
1620 break;
1621 }
1622
1623 goto next;
1624 }
1625
1626 if (dtuple->dtt_nkeys != nkeys)
1627 goto next;
1628
1629 for (i = 0; i < nkeys; i++, dkey++) {
1630 if (dkey->dttk_size != key[i].dttk_size)
1631 goto next; /* size or type mismatch */
1632
1633 if (dkey->dttk_size != 0) {
1634 if (dtrace_bcmp(
1635 (void *)(uintptr_t)key[i].dttk_value,
1636 (void *)(uintptr_t)dkey->dttk_value,
1637 dkey->dttk_size))
1638 goto next;
1639 } else {
1640 if (dkey->dttk_value != key[i].dttk_value)
1641 goto next;
1642 }
1643 }
1644
1645 if (op != DTRACE_DYNVAR_DEALLOC)
1646 return (dvar);
1647
1648 ASSERT(dvar->dtdv_next == NULL ||
1649 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE);
1650
1651 if (prev != NULL) {
1652 ASSERT(hash[bucket].dtdh_chain != dvar);
1653 ASSERT(start != dvar);
1654 ASSERT(prev->dtdv_next == dvar);
1655 prev->dtdv_next = dvar->dtdv_next;
1656 } else {
1657 if (dtrace_casptr(&hash[bucket].dtdh_chain,
1658 start, dvar->dtdv_next) != start) {
1659 /*
1660 * We have failed to atomically swing the
1661 * hash table head pointer, presumably because
1662 * of a conflicting allocation on another CPU.
1663 * We need to reread the hash chain and try
1664 * again.
1665 */
1666 goto top;
1667 }
1668 }
1669
1670 dtrace_membar_producer();
1671
1672 /*
1673 * Now set the hash value to indicate that it's free.
1674 */
1675 ASSERT(hash[bucket].dtdh_chain != dvar);
1676 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
1677
1678 dtrace_membar_producer();
1679
1680 /*
1681 * Set the next pointer to point at the dirty list, and
1682 * atomically swing the dirty pointer to the newly freed dvar.
1683 */
1684 do {
1685 next = dcpu->dtdsc_dirty;
1686 dvar->dtdv_next = next;
1687 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next);
1688
1689 /*
1690 * Finally, unlock this hash bucket.
1691 */
1692 ASSERT(hash[bucket].dtdh_lock == lock);
1693 ASSERT(lock & 1);
1694 hash[bucket].dtdh_lock++;
1695
1696 return (NULL);
1697next:
1698 prev = dvar;
1699 continue;
1700 }
1701
1702 if (dvar == NULL) {
1703 /*
1704 * If dvar is NULL, it is because we went off the rails:
1705 * one of the elements that we traversed in the hash chain
1706 * was deleted while we were traversing it. In this case,
1707 * we assert that we aren't doing a dealloc (deallocs lock
1708 * the hash bucket to prevent themselves from racing with
1709 * one another), and retry the hash chain traversal.
1710 */
1711 ASSERT(op != DTRACE_DYNVAR_DEALLOC);
1712 goto top;
1713 }
1714
1715 if (op != DTRACE_DYNVAR_ALLOC) {
1716 /*
1717 * If we are not to allocate a new variable, we want to
1718 * return NULL now. Before we return, check that the value
1719 * of the lock word hasn't changed. If it has, we may have
1720 * seen an inconsistent snapshot.
1721 */
1722 if (op == DTRACE_DYNVAR_NOALLOC) {
1723 if (hash[bucket].dtdh_lock != lock)
1724 goto top;
1725 } else {
1726 ASSERT(op == DTRACE_DYNVAR_DEALLOC);
1727 ASSERT(hash[bucket].dtdh_lock == lock);
1728 ASSERT(lock & 1);
1729 hash[bucket].dtdh_lock++;
1730 }
1731
1732 return (NULL);
1733 }
1734
1735 /*
1736 * We need to allocate a new dynamic variable. The size we need is the
1737 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
1738 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
1739 * the size of any referred-to data (dsize). We then round the final
1740 * size up to the chunksize for allocation.
1741 */
1742 for (ksize = 0, i = 0; i < nkeys; i++)
1743 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
1744
1745 /*
1746 * This should be pretty much impossible, but could happen if, say,
1747 * strange DIF specified the tuple. Ideally, this should be an
1748 * assertion and not an error condition -- but that requires that the
1749 * chunksize calculation in dtrace_difo_chunksize() be absolutely
1750 * bullet-proof. (That is, it must not be able to be fooled by
1751 * malicious DIF.) Given the lack of backwards branches in DIF,
1752 * solving this would presumably not amount to solving the Halting
1753 * Problem -- but it still seems awfully hard.
1754 */
1755 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) +
1756 ksize + dsize > chunksize) {
1757 dcpu->dtdsc_drops++;
1758 return (NULL);
1759 }
1760
1761 nstate = DTRACE_DSTATE_EMPTY;
1762
1763 do {
1764retry:
1765 free = dcpu->dtdsc_free;
1766
1767 if (free == NULL) {
1768 dtrace_dynvar_t *clean = dcpu->dtdsc_clean;
1769 void *rval;
1770
1771 if (clean == NULL) {
1772 /*
1773 * We're out of dynamic variable space on
1774 * this CPU. Unless we have tried all CPUs,
1775 * we'll try to allocate from a different
1776 * CPU.
1777 */
1778 switch (dstate->dtds_state) {
1779 case DTRACE_DSTATE_CLEAN: {
1780 void *sp = &dstate->dtds_state;
1781
1782 if (++cpu >= NCPU)
1783 cpu = 0;
1784
1785 if (dcpu->dtdsc_dirty != NULL &&
1786 nstate == DTRACE_DSTATE_EMPTY)
1787 nstate = DTRACE_DSTATE_DIRTY;
1788
1789 if (dcpu->dtdsc_rinsing != NULL)
1790 nstate = DTRACE_DSTATE_RINSING;
1791
1792 dcpu = &dstate->dtds_percpu[cpu];
1793
1794 if (cpu != me)
1795 goto retry;
1796
1797 (void) dtrace_cas32(sp,
1798 DTRACE_DSTATE_CLEAN, nstate);
1799
1800 /*
1801 * To increment the correct bean
1802 * counter, take another lap.
1803 */
1804 goto retry;
1805 }
1806
1807 case DTRACE_DSTATE_DIRTY:
1808 dcpu->dtdsc_dirty_drops++;
1809 break;
1810
1811 case DTRACE_DSTATE_RINSING:
1812 dcpu->dtdsc_rinsing_drops++;
1813 break;
1814
1815 case DTRACE_DSTATE_EMPTY:
1816 dcpu->dtdsc_drops++;
1817 break;
1818 }
1819
1820 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP);
1821 return (NULL);
1822 }
1823
1824 /*
1825 * The clean list appears to be non-empty. We want to
1826 * move the clean list to the free list; we start by
1827 * moving the clean pointer aside.
1828 */
1829 if (dtrace_casptr(&dcpu->dtdsc_clean,
1830 clean, NULL) != clean) {
1831 /*
1832 * We are in one of two situations:
1833 *
1834 * (a) The clean list was switched to the
1835 * free list by another CPU.
1836 *
1837 * (b) The clean list was added to by the
1838 * cleansing cyclic.
1839 *
1840 * In either of these situations, we can
1841 * just reattempt the free list allocation.
1842 */
1843 goto retry;
1844 }
1845
1846 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE);
1847
1848 /*
1849 * Now we'll move the clean list to our free list.
1850 * It's impossible for this to fail: the only way
1851 * the free list can be updated is through this
1852 * code path, and only one CPU can own the clean list.
1853 * Thus, it would only be possible for this to fail if
1854 * this code were racing with dtrace_dynvar_clean().
1855 * (That is, if dtrace_dynvar_clean() updated the clean
1856 * list, and we ended up racing to update the free
1857 * list.) This race is prevented by the dtrace_sync()
1858 * in dtrace_dynvar_clean() -- which flushes the
1859 * owners of the clean lists out before resetting
1860 * the clean lists.
1861 */
1862 dcpu = &dstate->dtds_percpu[me];
1863 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean);
1864 ASSERT(rval == NULL);
1865 goto retry;
1866 }
1867
1868 dvar = free;
1869 new_free = dvar->dtdv_next;
1870 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free);
1871
1872 /*
1873 * We have now allocated a new chunk. We copy the tuple keys into the
1874 * tuple array and copy any referenced key data into the data space
1875 * following the tuple array. As we do this, we relocate dttk_value
1876 * in the final tuple to point to the key data address in the chunk.
1877 */
1878 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys];
1879 dvar->dtdv_data = (void *)(kdata + ksize);
1880 dvar->dtdv_tuple.dtt_nkeys = nkeys;
1881
1882 for (i = 0; i < nkeys; i++) {
1883 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i];
1884 size_t kesize = key[i].dttk_size;
1885
1886 if (kesize != 0) {
1887 dtrace_bcopy(
1888 (const void *)(uintptr_t)key[i].dttk_value,
1889 (void *)kdata, kesize);
1890 dkey->dttk_value = kdata;
1891 kdata += P2ROUNDUP(kesize, sizeof (uint64_t));
1892 } else {
1893 dkey->dttk_value = key[i].dttk_value;
1894 }
1895
1896 dkey->dttk_size = kesize;
1897 }
1898
1899 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE);
1900 dvar->dtdv_hashval = hashval;
1901 dvar->dtdv_next = start;
1902
1903 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start)
1904 return (dvar);
1905
1906 /*
1907 * The cas has failed. Either another CPU is adding an element to
1908 * this hash chain, or another CPU is deleting an element from this
1909 * hash chain. The simplest way to deal with both of these cases
1910 * (though not necessarily the most efficient) is to free our
1911 * allocated block and tail-call ourselves. Note that the free is
1912 * to the dirty list and _not_ to the free list. This is to prevent
1913 * races with allocators, above.
1914 */
1915 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
1916
1917 dtrace_membar_producer();
1918
1919 do {
1920 free = dcpu->dtdsc_dirty;
1921 dvar->dtdv_next = free;
1922 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free);
1923
1924 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate));
1925}
1926
1927/*ARGSUSED*/
1928static void
1929dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg)
1930{
1931 RT_NOREF_PV(arg);
1932 if ((int64_t)nval < (int64_t)*oval)
1933 *oval = nval;
1934}
1935
1936/*ARGSUSED*/
1937static void
1938dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg)
1939{
1940 RT_NOREF_PV(arg);
1941 if ((int64_t)nval > (int64_t)*oval)
1942 *oval = nval;
1943}
1944
1945static void
1946dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr)
1947{
1948 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET;
1949 int64_t val = (int64_t)nval;
1950
1951 if (val < 0) {
1952 for (i = 0; i < zero; i++) {
1953 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) {
1954 quanta[i] += incr;
1955 return;
1956 }
1957 }
1958 } else {
1959 for (i = zero + 1; i < VBDTCAST(int)DTRACE_QUANTIZE_NBUCKETS; i++) {
1960 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) {
1961 quanta[i - 1] += incr;
1962 return;
1963 }
1964 }
1965
1966 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr;
1967 return;
1968 }
1969
1970#ifndef VBOX
1971 ASSERT(0);
1972#else
1973 AssertFatalFailed();
1974#endif
1975}
1976
1977static void
1978dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr)
1979{
1980 uint64_t arg = *lquanta++;
1981 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
1982 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
1983 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
1984 int32_t val = (int32_t)nval, level;
1985
1986 ASSERT(step != 0);
1987 ASSERT(levels != 0);
1988
1989 if (val < base) {
1990 /*
1991 * This is an underflow.
1992 */
1993 lquanta[0] += incr;
1994 return;
1995 }
1996
1997 level = (val - base) / step;
1998
1999 if (level < levels) {
2000 lquanta[level + 1] += incr;
2001 return;
2002 }
2003
2004 /*
2005 * This is an overflow.
2006 */
2007 lquanta[levels + 1] += incr;
2008}
2009
2010/*ARGSUSED*/
2011static void
2012dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg)
2013{
2014 RT_NOREF_PV(arg);
2015 data[0]++;
2016 data[1] += nval;
2017}
2018
2019/*ARGSUSED*/
2020static void
2021dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg)
2022{
2023 int64_t snval = (int64_t)nval;
2024 uint64_t tmp[2];
2025 RT_NOREF_PV(arg);
2026
2027 data[0]++;
2028 data[1] += nval;
2029
2030 /*
2031 * What we want to say here is:
2032 *
2033 * data[2] += nval * nval;
2034 *
2035 * But given that nval is 64-bit, we could easily overflow, so
2036 * we do this as 128-bit arithmetic.
2037 */
2038 if (snval < 0)
2039 snval = -snval;
2040
2041 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp);
2042 dtrace_add_128(data + 2, tmp, data + 2);
2043}
2044
2045/*ARGSUSED*/
2046static void
2047dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg)
2048{
2049 RT_NOREF_PV(arg); RT_NOREF_PV(nval);
2050
2051 *oval = *oval + 1;
2052}
2053
2054/*ARGSUSED*/
2055static void
2056dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg)
2057{
2058 RT_NOREF_PV(arg);
2059 *oval += nval;
2060}
2061
2062/*
2063 * Aggregate given the tuple in the principal data buffer, and the aggregating
2064 * action denoted by the specified dtrace_aggregation_t. The aggregation
2065 * buffer is specified as the buf parameter. This routine does not return
2066 * failure; if there is no space in the aggregation buffer, the data will be
2067 * dropped, and a corresponding counter incremented.
2068 */
2069static void
2070dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf,
2071 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg)
2072{
2073 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec;
2074 uint32_t i, ndx, size, fsize;
2075 uint32_t align = sizeof (uint64_t) - 1;
2076 dtrace_aggbuffer_t *agb;
2077 dtrace_aggkey_t *key;
2078 uint32_t hashval = 0, limit, isstr;
2079 caddr_t tomax, data, kdata;
2080 dtrace_actkind_t action;
2081 dtrace_action_t *act;
2082 uintptr_t offs;
2083
2084 if (buf == NULL)
2085 return;
2086
2087 if (!agg->dtag_hasarg) {
2088 /*
2089 * Currently, only quantize() and lquantize() take additional
2090 * arguments, and they have the same semantics: an increment
2091 * value that defaults to 1 when not present. If additional
2092 * aggregating actions take arguments, the setting of the
2093 * default argument value will presumably have to become more
2094 * sophisticated...
2095 */
2096 arg = 1;
2097 }
2098
2099 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION;
2100 size = rec->dtrd_offset - agg->dtag_base;
2101 fsize = size + rec->dtrd_size;
2102
2103 ASSERT(dbuf->dtb_tomax != NULL);
2104 data = dbuf->dtb_tomax + offset + agg->dtag_base;
2105
2106 if ((tomax = buf->dtb_tomax) == NULL) {
2107 dtrace_buffer_drop(buf);
2108 return;
2109 }
2110
2111 /*
2112 * The metastructure is always at the bottom of the buffer.
2113 */
2114 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size -
2115 sizeof (dtrace_aggbuffer_t));
2116
2117 if (buf->dtb_offset == 0) {
2118 /*
2119 * We just kludge up approximately 1/8th of the size to be
2120 * buckets. If this guess ends up being routinely
2121 * off-the-mark, we may need to dynamically readjust this
2122 * based on past performance.
2123 */
2124 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t);
2125
2126 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) <
2127 (uintptr_t)tomax || hashsize == 0) {
2128 /*
2129 * We've been given a ludicrously small buffer;
2130 * increment our drop count and leave.
2131 */
2132 dtrace_buffer_drop(buf);
2133 return;
2134 }
2135
2136 /*
2137 * And now, a pathetic attempt to try to get a an odd (or
2138 * perchance, a prime) hash size for better hash distribution.
2139 */
2140 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3))
2141 hashsize -= DTRACE_AGGHASHSIZE_SLEW;
2142
2143 agb->dtagb_hashsize = hashsize;
2144 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb -
2145 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *));
2146 agb->dtagb_free = (uintptr_t)agb->dtagb_hash;
2147
2148 for (i = 0; i < agb->dtagb_hashsize; i++)
2149 agb->dtagb_hash[i] = NULL;
2150 }
2151
2152 ASSERT(agg->dtag_first != NULL);
2153 ASSERT(agg->dtag_first->dta_intuple);
2154
2155 /*
2156 * Calculate the hash value based on the key. Note that we _don't_
2157 * include the aggid in the hashing (but we will store it as part of
2158 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time"
2159 * algorithm: a simple, quick algorithm that has no known funnels, and
2160 * gets good distribution in practice. The efficacy of the hashing
2161 * algorithm (and a comparison with other algorithms) may be found by
2162 * running the ::dtrace_aggstat MDB dcmd.
2163 */
2164 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2165 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2166 limit = i + act->dta_rec.dtrd_size;
2167 ASSERT(limit <= size);
2168 isstr = DTRACEACT_ISSTRING(act);
2169
2170 for (; i < limit; i++) {
2171 hashval += data[i];
2172 hashval += (hashval << 10);
2173 hashval ^= (hashval >> 6);
2174
2175 if (isstr && data[i] == '\0')
2176 break;
2177 }
2178 }
2179
2180 hashval += (hashval << 3);
2181 hashval ^= (hashval >> 11);
2182 hashval += (hashval << 15);
2183
2184 /*
2185 * Yes, the divide here is expensive -- but it's generally the least
2186 * of the performance issues given the amount of data that we iterate
2187 * over to compute hash values, compare data, etc.
2188 */
2189 ndx = hashval % agb->dtagb_hashsize;
2190
2191 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) {
2192 ASSERT((caddr_t)key >= tomax);
2193 ASSERT((caddr_t)key < tomax + buf->dtb_size);
2194
2195 if (hashval != key->dtak_hashval || key->dtak_size != size)
2196 continue;
2197
2198 kdata = key->dtak_data;
2199 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size);
2200
2201 for (act = agg->dtag_first; act->dta_intuple;
2202 act = act->dta_next) {
2203 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2204 limit = i + act->dta_rec.dtrd_size;
2205 ASSERT(limit <= size);
2206 isstr = DTRACEACT_ISSTRING(act);
2207
2208 for (; i < limit; i++) {
2209 if (kdata[i] != data[i])
2210 goto next;
2211
2212 if (isstr && data[i] == '\0')
2213 break;
2214 }
2215 }
2216
2217 if (action != key->dtak_action) {
2218 /*
2219 * We are aggregating on the same value in the same
2220 * aggregation with two different aggregating actions.
2221 * (This should have been picked up in the compiler,
2222 * so we may be dealing with errant or devious DIF.)
2223 * This is an error condition; we indicate as much,
2224 * and return.
2225 */
2226 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
2227 return;
2228 }
2229
2230 /*
2231 * This is a hit: we need to apply the aggregator to
2232 * the value at this key.
2233 */
2234 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg);
2235 return;
2236next:
2237 continue;
2238 }
2239
2240 /*
2241 * We didn't find it. We need to allocate some zero-filled space,
2242 * link it into the hash table appropriately, and apply the aggregator
2243 * to the (zero-filled) value.
2244 */
2245 offs = buf->dtb_offset;
2246 while (offs & (align - 1))
2247 offs += sizeof (uint32_t);
2248
2249 /*
2250 * If we don't have enough room to both allocate a new key _and_
2251 * its associated data, increment the drop count and return.
2252 */
2253 if ((uintptr_t)tomax + offs + fsize >
2254 agb->dtagb_free - sizeof (dtrace_aggkey_t)) {
2255 dtrace_buffer_drop(buf);
2256 return;
2257 }
2258
2259 /*CONSTCOND*/
2260 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1)));
2261 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t));
2262 agb->dtagb_free -= sizeof (dtrace_aggkey_t);
2263
2264 key->dtak_data = kdata = tomax + offs;
2265 buf->dtb_offset = offs + fsize;
2266
2267 /*
2268 * Now copy the data across.
2269 */
2270 *((dtrace_aggid_t *)kdata) = agg->dtag_id;
2271
2272 for (i = sizeof (dtrace_aggid_t); i < size; i++)
2273 kdata[i] = data[i];
2274
2275 /*
2276 * Because strings are not zeroed out by default, we need to iterate
2277 * looking for actions that store strings, and we need to explicitly
2278 * pad these strings out with zeroes.
2279 */
2280 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2281 int nul;
2282
2283 if (!DTRACEACT_ISSTRING(act))
2284 continue;
2285
2286 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2287 limit = i + act->dta_rec.dtrd_size;
2288 ASSERT(limit <= size);
2289
2290 for (nul = 0; i < limit; i++) {
2291 if (nul) {
2292 kdata[i] = '\0';
2293 continue;
2294 }
2295
2296 if (data[i] != '\0')
2297 continue;
2298
2299 nul = 1;
2300 }
2301 }
2302
2303 for (i = size; i < fsize; i++)
2304 kdata[i] = 0;
2305
2306 key->dtak_hashval = hashval;
2307 key->dtak_size = size;
2308 key->dtak_action = action;
2309 key->dtak_next = agb->dtagb_hash[ndx];
2310 agb->dtagb_hash[ndx] = key;
2311
2312 /*
2313 * Finally, apply the aggregator.
2314 */
2315 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial;
2316 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg);
2317}
2318
2319/*
2320 * Given consumer state, this routine finds a speculation in the INACTIVE
2321 * state and transitions it into the ACTIVE state. If there is no speculation
2322 * in the INACTIVE state, 0 is returned. In this case, no error counter is
2323 * incremented -- it is up to the caller to take appropriate action.
2324 */
2325static int
2326dtrace_speculation(dtrace_state_t *state)
2327{
2328 int i = 0;
2329 dtrace_speculation_state_t current;
2330 uint32_t *stat = &state->dts_speculations_unavail, count;
2331
2332 while (i < state->dts_nspeculations) {
2333 dtrace_speculation_t *spec = &state->dts_speculations[i];
2334
2335 current = spec->dtsp_state;
2336
2337 if (current != DTRACESPEC_INACTIVE) {
2338 if (current == DTRACESPEC_COMMITTINGMANY ||
2339 current == DTRACESPEC_COMMITTING ||
2340 current == DTRACESPEC_DISCARDING)
2341 stat = &state->dts_speculations_busy;
2342 i++;
2343 continue;
2344 }
2345
2346 if ( (dtrace_speculation_state_t)dtrace_cas32((uint32_t *)&spec->dtsp_state, current, DTRACESPEC_ACTIVE)
2347 == current)
2348 return (i + 1);
2349 }
2350
2351 /*
2352 * We couldn't find a speculation. If we found as much as a single
2353 * busy speculation buffer, we'll attribute this failure as "busy"
2354 * instead of "unavail".
2355 */
2356 do {
2357 count = *stat;
2358 } while (dtrace_cas32(stat, count, count + 1) != count);
2359
2360 return (0);
2361}
2362
2363/*
2364 * This routine commits an active speculation. If the specified speculation
2365 * is not in a valid state to perform a commit(), this routine will silently do
2366 * nothing. The state of the specified speculation is transitioned according
2367 * to the state transition diagram outlined in <sys/dtrace_impl.h>
2368 */
2369static void
2370dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu,
2371 dtrace_specid_t which)
2372{
2373 dtrace_speculation_t *spec;
2374 dtrace_buffer_t *src, *dest;
2375 uintptr_t daddr, saddr, dlimit;
2376 dtrace_speculation_state_t current, new VBDTUNASS(-1);
2377 intptr_t offs;
2378
2379 if (which == 0)
2380 return;
2381
2382 if (which > VBDTCAST(unsigned)state->dts_nspeculations) {
2383 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2384 return;
2385 }
2386
2387 spec = &state->dts_speculations[which - 1];
2388 src = &spec->dtsp_buffer[cpu];
2389 dest = &state->dts_buffer[cpu];
2390
2391 do {
2392 current = spec->dtsp_state;
2393
2394 if (current == DTRACESPEC_COMMITTINGMANY)
2395 break;
2396
2397 switch (current) {
2398 case DTRACESPEC_INACTIVE:
2399 case DTRACESPEC_DISCARDING:
2400 return;
2401
2402 case DTRACESPEC_COMMITTING:
2403 /*
2404 * This is only possible if we are (a) commit()'ing
2405 * without having done a prior speculate() on this CPU
2406 * and (b) racing with another commit() on a different
2407 * CPU. There's nothing to do -- we just assert that
2408 * our offset is 0.
2409 */
2410 ASSERT(src->dtb_offset == 0);
2411 return;
2412
2413 case DTRACESPEC_ACTIVE:
2414 new = DTRACESPEC_COMMITTING;
2415 break;
2416
2417 case DTRACESPEC_ACTIVEONE:
2418 /*
2419 * This speculation is active on one CPU. If our
2420 * buffer offset is non-zero, we know that the one CPU
2421 * must be us. Otherwise, we are committing on a
2422 * different CPU from the speculate(), and we must
2423 * rely on being asynchronously cleaned.
2424 */
2425 if (src->dtb_offset != 0) {
2426 new = DTRACESPEC_COMMITTING;
2427 break;
2428 }
2429 RT_FALL_THRU();
2430
2431 case DTRACESPEC_ACTIVEMANY:
2432 new = DTRACESPEC_COMMITTINGMANY;
2433 break;
2434
2435 default:
2436#ifndef VBOX
2437 ASSERT(0);
2438#else
2439 AssertFatalMsgFailed(("%d\n", current));
2440#endif
2441 }
2442 } while ((dtrace_speculation_state_t)dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new) != current);
2443
2444 /*
2445 * We have set the state to indicate that we are committing this
2446 * speculation. Now reserve the necessary space in the destination
2447 * buffer.
2448 */
2449 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset,
2450 sizeof (uint64_t), state, NULL)) < 0) {
2451 dtrace_buffer_drop(dest);
2452 goto out;
2453 }
2454
2455 /*
2456 * We have the space; copy the buffer across. (Note that this is a
2457 * highly subobtimal bcopy(); in the unlikely event that this becomes
2458 * a serious performance issue, a high-performance DTrace-specific
2459 * bcopy() should obviously be invented.)
2460 */
2461 daddr = (uintptr_t)dest->dtb_tomax + offs;
2462 dlimit = daddr + src->dtb_offset;
2463 saddr = (uintptr_t)src->dtb_tomax;
2464
2465 /*
2466 * First, the aligned portion.
2467 */
2468 while (dlimit - daddr >= sizeof (uint64_t)) {
2469 *((uint64_t *)daddr) = *((uint64_t *)saddr);
2470
2471 daddr += sizeof (uint64_t);
2472 saddr += sizeof (uint64_t);
2473 }
2474
2475 /*
2476 * Now any left-over bit...
2477 */
2478 while (dlimit - daddr)
2479 *((uint8_t *)daddr++) = *((uint8_t *)saddr++);
2480
2481 /*
2482 * Finally, commit the reserved space in the destination buffer.
2483 */
2484 dest->dtb_offset = offs + src->dtb_offset;
2485
2486out:
2487 /*
2488 * If we're lucky enough to be the only active CPU on this speculation
2489 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2490 */
2491 if (current == DTRACESPEC_ACTIVE ||
2492 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) {
2493 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state,
2494 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE);
2495
2496 ASSERT(rval == DTRACESPEC_COMMITTING); NOREF(rval);
2497 }
2498
2499 src->dtb_offset = 0;
2500 src->dtb_xamot_drops += src->dtb_drops;
2501 src->dtb_drops = 0;
2502}
2503
2504/*
2505 * This routine discards an active speculation. If the specified speculation
2506 * is not in a valid state to perform a discard(), this routine will silently
2507 * do nothing. The state of the specified speculation is transitioned
2508 * according to the state transition diagram outlined in <sys/dtrace_impl.h>
2509 */
2510static void
2511dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu,
2512 dtrace_specid_t which)
2513{
2514 dtrace_speculation_t *spec;
2515 dtrace_speculation_state_t current, new;
2516 dtrace_buffer_t *buf;
2517
2518 if (which == 0)
2519 return;
2520
2521 if (which > VBDTCAST(unsigned)state->dts_nspeculations) {
2522 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2523 return;
2524 }
2525
2526 spec = &state->dts_speculations[which - 1];
2527 buf = &spec->dtsp_buffer[cpu];
2528
2529 do {
2530 current = spec->dtsp_state;
2531
2532 switch (current) {
2533 case DTRACESPEC_INACTIVE:
2534 case DTRACESPEC_COMMITTINGMANY:
2535 case DTRACESPEC_COMMITTING:
2536 case DTRACESPEC_DISCARDING:
2537 return;
2538
2539 case DTRACESPEC_ACTIVE:
2540 case DTRACESPEC_ACTIVEMANY:
2541 new = DTRACESPEC_DISCARDING;
2542 break;
2543
2544 case DTRACESPEC_ACTIVEONE:
2545 if (buf->dtb_offset != 0) {
2546 new = DTRACESPEC_INACTIVE;
2547 } else {
2548 new = DTRACESPEC_DISCARDING;
2549 }
2550 break;
2551
2552 default:
2553#ifndef VBOX
2554 ASSERT(0);
2555#else
2556 AssertFatalMsgFailed(("%d\n", current));
2557#endif
2558 }
2559 } while ((dtrace_speculation_state_t)dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new) != current);
2560
2561 buf->dtb_offset = 0;
2562 buf->dtb_drops = 0;
2563}
2564
2565/*
2566 * Note: not called from probe context. This function is called
2567 * asynchronously from cross call context to clean any speculations that are
2568 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be
2569 * transitioned back to the INACTIVE state until all CPUs have cleaned the
2570 * speculation.
2571 */
2572static void
2573dtrace_speculation_clean_here(dtrace_state_t *state)
2574{
2575 dtrace_icookie_t cookie;
2576 processorid_t cpu = VBDT_GET_CPUID();
2577 dtrace_buffer_t *dest = &state->dts_buffer[cpu];
2578 dtrace_specid_t i;
2579
2580 cookie = dtrace_interrupt_disable();
2581
2582 if (dest->dtb_tomax == NULL) {
2583 dtrace_interrupt_enable(cookie);
2584 return;
2585 }
2586
2587 for (i = 0; i < VBDTCAST(unsigned)state->dts_nspeculations; i++) {
2588 dtrace_speculation_t *spec = &state->dts_speculations[i];
2589 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu];
2590
2591 if (src->dtb_tomax == NULL)
2592 continue;
2593
2594 if (spec->dtsp_state == DTRACESPEC_DISCARDING) {
2595 src->dtb_offset = 0;
2596 continue;
2597 }
2598
2599 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2600 continue;
2601
2602 if (src->dtb_offset == 0)
2603 continue;
2604
2605 dtrace_speculation_commit(state, cpu, i + 1);
2606 }
2607
2608 dtrace_interrupt_enable(cookie);
2609}
2610
2611#ifdef VBOX
2612/** */
2613static DECLCALLBACK(void) dtrace_speculation_clean_here_wrapper(RTCPUID idCpu, void *pvUser1, void *pvUser2)
2614{
2615 dtrace_speculation_clean_here((dtrace_state_t *)pvUser1);
2616 NOREF(pvUser2); NOREF(idCpu);
2617}
2618#endif
2619
2620/*
2621 * Note: not called from probe context. This function is called
2622 * asynchronously (and at a regular interval) to clean any speculations that
2623 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there
2624 * is work to be done, it cross calls all CPUs to perform that work;
2625 * COMMITMANY and DISCARDING speculations may not be transitioned back to the
2626 * INACTIVE state until they have been cleaned by all CPUs.
2627 */
2628static void
2629dtrace_speculation_clean(dtrace_state_t *state)
2630{
2631 int work = 0, rv;
2632 dtrace_specid_t i;
2633
2634 for (i = 0; i < VBDTCAST(unsigned)state->dts_nspeculations; i++) {
2635 dtrace_speculation_t *spec = &state->dts_speculations[i];
2636
2637 ASSERT(!spec->dtsp_cleaning);
2638
2639 if (spec->dtsp_state != DTRACESPEC_DISCARDING &&
2640 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2641 continue;
2642
2643 work++;
2644 spec->dtsp_cleaning = 1;
2645 }
2646
2647 if (!work)
2648 return;
2649
2650#ifndef VBOX
2651 dtrace_xcall(DTRACE_CPUALL,
2652 (dtrace_xcall_t)dtrace_speculation_clean_here, state);
2653#else
2654 RTMpOnAll(dtrace_speculation_clean_here_wrapper, state, NULL);
2655#endif
2656
2657 /*
2658 * We now know that all CPUs have committed or discarded their
2659 * speculation buffers, as appropriate. We can now set the state
2660 * to inactive.
2661 */
2662 for (i = 0; i < VBDTCAST(unsigned)state->dts_nspeculations; i++) {
2663 dtrace_speculation_t *spec = &state->dts_speculations[i];
2664 dtrace_speculation_state_t current, new;
2665
2666 if (!spec->dtsp_cleaning)
2667 continue;
2668
2669 current = spec->dtsp_state;
2670 ASSERT(current == DTRACESPEC_DISCARDING ||
2671 current == DTRACESPEC_COMMITTINGMANY);
2672
2673 new = DTRACESPEC_INACTIVE;
2674
2675 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new);
2676 ASSERT(VBDTCAST(dtrace_speculation_state_t)rv == current);
2677 spec->dtsp_cleaning = 0;
2678 }
2679}
2680
2681/*
2682 * Called as part of a speculate() to get the speculative buffer associated
2683 * with a given speculation. Returns NULL if the specified speculation is not
2684 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and
2685 * the active CPU is not the specified CPU -- the speculation will be
2686 * atomically transitioned into the ACTIVEMANY state.
2687 */
2688static dtrace_buffer_t *
2689dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid,
2690 dtrace_specid_t which)
2691{
2692 dtrace_speculation_t *spec;
2693 dtrace_speculation_state_t current, new VBDTUNASS(-1);
2694 dtrace_buffer_t *buf;
2695
2696 if (which == 0)
2697 return (NULL);
2698
2699 if (which > VBDTCAST(unsigned)state->dts_nspeculations) {
2700 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2701 return (NULL);
2702 }
2703
2704 spec = &state->dts_speculations[which - 1];
2705 buf = &spec->dtsp_buffer[cpuid];
2706
2707 do {
2708 current = spec->dtsp_state;
2709
2710 switch (current) {
2711 case DTRACESPEC_INACTIVE:
2712 case DTRACESPEC_COMMITTINGMANY:
2713 case DTRACESPEC_DISCARDING:
2714 return (NULL);
2715
2716 case DTRACESPEC_COMMITTING:
2717 ASSERT(buf->dtb_offset == 0);
2718 return (NULL);
2719
2720 case DTRACESPEC_ACTIVEONE:
2721 /*
2722 * This speculation is currently active on one CPU.
2723 * Check the offset in the buffer; if it's non-zero,
2724 * that CPU must be us (and we leave the state alone).
2725 * If it's zero, assume that we're starting on a new
2726 * CPU -- and change the state to indicate that the
2727 * speculation is active on more than one CPU.
2728 */
2729 if (buf->dtb_offset != 0)
2730 return (buf);
2731
2732 new = DTRACESPEC_ACTIVEMANY;
2733 break;
2734
2735 case DTRACESPEC_ACTIVEMANY:
2736 return (buf);
2737
2738 case DTRACESPEC_ACTIVE:
2739 new = DTRACESPEC_ACTIVEONE;
2740 break;
2741
2742 default:
2743#ifndef VBOX
2744 ASSERT(0);
2745#else
2746 AssertFatalMsgFailed(("%d\n", current));
2747#endif
2748 }
2749 } while ((dtrace_speculation_state_t)dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new) != current);
2750
2751 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY);
2752 return (buf);
2753}
2754
2755/*
2756 * Return a string. In the event that the user lacks the privilege to access
2757 * arbitrary kernel memory, we copy the string out to scratch memory so that we
2758 * don't fail access checking.
2759 *
2760 * dtrace_dif_variable() uses this routine as a helper for various
2761 * builtin values such as 'execname' and 'probefunc.'
2762 */
2763VBDTSTATIC uintptr_t
2764dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state,
2765 dtrace_mstate_t *mstate)
2766{
2767 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
2768 uintptr_t ret;
2769 size_t strsz;
2770
2771 /*
2772 * The easy case: this probe is allowed to read all of memory, so
2773 * we can just return this as a vanilla pointer.
2774 */
2775 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
2776 return (addr);
2777
2778 /*
2779 * This is the tougher case: we copy the string in question from
2780 * kernel memory into scratch memory and return it that way: this
2781 * ensures that we won't trip up when access checking tests the
2782 * BYREF return value.
2783 */
2784 strsz = dtrace_strlen((char *)addr, size) + 1;
2785
2786 if (mstate->dtms_scratch_ptr + strsz >
2787 mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
2788 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
2789 return (NULL);
2790 }
2791
2792 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
2793 strsz);
2794 ret = mstate->dtms_scratch_ptr;
2795 mstate->dtms_scratch_ptr += strsz;
2796 return (ret);
2797}
2798
2799/*
2800 * This function implements the DIF emulator's variable lookups. The emulator
2801 * passes a reserved variable identifier and optional built-in array index.
2802 */
2803static uint64_t
2804dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v,
2805 uint64_t ndx)
2806{
2807 /*
2808 * If we're accessing one of the uncached arguments, we'll turn this
2809 * into a reference in the args array.
2810 */
2811 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) {
2812 ndx = v - DIF_VAR_ARG0;
2813 v = DIF_VAR_ARGS;
2814 }
2815
2816 switch (v) {
2817 case DIF_VAR_ARGS:
2818 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS);
2819 if (ndx >= sizeof (mstate->dtms_arg) /
2820 sizeof (mstate->dtms_arg[0])) {
2821 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
2822 dtrace_provider_t *pv;
2823 uint64_t val;
2824
2825 pv = mstate->dtms_probe->dtpr_provider;
2826 if (pv->dtpv_pops.dtps_getargval != NULL)
2827 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg,
2828 mstate->dtms_probe->dtpr_id,
2829 mstate->dtms_probe->dtpr_arg, ndx, aframes);
2830 else
2831 val = dtrace_getarg(ndx, aframes);
2832
2833 /*
2834 * This is regrettably required to keep the compiler
2835 * from tail-optimizing the call to dtrace_getarg().
2836 * The condition always evaluates to true, but the
2837 * compiler has no way of figuring that out a priori.
2838 * (None of this would be necessary if the compiler
2839 * could be relied upon to _always_ tail-optimize
2840 * the call to dtrace_getarg() -- but it can't.)
2841 */
2842 if (mstate->dtms_probe != NULL)
2843 return (val);
2844
2845#ifndef VBOX
2846 ASSERT(0);
2847#else
2848 AssertFatalFailed();
2849#endif
2850 }
2851
2852 return (mstate->dtms_arg[ndx]);
2853
2854 case DIF_VAR_UREGS: {
2855#ifndef VBOX
2856 klwp_t *lwp;
2857
2858 if (!dtrace_priv_proc(state))
2859 return (0);
2860
2861 if ((lwp = curthread->t_lwp) == NULL) {
2862 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
2863 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval = NULL;
2864 return (0);
2865 }
2866
2867 return (dtrace_getreg(lwp->lwp_regs, ndx));
2868#else
2869 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2870 return (0);
2871#endif
2872 }
2873
2874 case DIF_VAR_CURTHREAD:
2875 if (!dtrace_priv_kernel(state))
2876 return (0);
2877#ifndef VBOX
2878 return ((uint64_t)(uintptr_t)curthread);
2879#else
2880 return ((uintptr_t)RTThreadNativeSelf());
2881#endif
2882
2883 case DIF_VAR_TIMESTAMP:
2884 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
2885 mstate->dtms_timestamp = dtrace_gethrtime();
2886 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP;
2887 }
2888 return (mstate->dtms_timestamp);
2889
2890 case DIF_VAR_VTIMESTAMP:
2891#ifndef VBOX
2892 ASSERT(dtrace_vtime_references != 0);
2893 return (curthread->t_dtrace_vtime);
2894#else
2895 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2896 return (0);
2897#endif
2898
2899 case DIF_VAR_WALLTIMESTAMP:
2900 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) {
2901 mstate->dtms_walltimestamp = dtrace_gethrestime();
2902 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP;
2903 }
2904 return (mstate->dtms_walltimestamp);
2905
2906 case DIF_VAR_IPL:
2907 if (!dtrace_priv_kernel(state))
2908 return (0);
2909 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) {
2910 mstate->dtms_ipl = dtrace_getipl();
2911 mstate->dtms_present |= DTRACE_MSTATE_IPL;
2912 }
2913 return (mstate->dtms_ipl);
2914
2915 case DIF_VAR_EPID:
2916 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID);
2917 return (mstate->dtms_epid);
2918
2919 case DIF_VAR_ID:
2920 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
2921 return (mstate->dtms_probe->dtpr_id);
2922
2923 case DIF_VAR_STACKDEPTH:
2924 if (!dtrace_priv_kernel(state))
2925 return (0);
2926 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) {
2927 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
2928
2929 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes);
2930 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH;
2931 }
2932 return (mstate->dtms_stackdepth);
2933
2934 case DIF_VAR_USTACKDEPTH:
2935 if (!dtrace_priv_proc(state))
2936 return (0);
2937 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) {
2938 /*
2939 * See comment in DIF_VAR_PID.
2940 */
2941 if (DTRACE_ANCHORED(mstate->dtms_probe) &&
2942 CPU_ON_INTR(CPU)) {
2943 mstate->dtms_ustackdepth = 0;
2944 } else {
2945 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2946 mstate->dtms_ustackdepth =
2947 dtrace_getustackdepth();
2948 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
2949 }
2950 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH;
2951 }
2952 return (mstate->dtms_ustackdepth);
2953
2954 case DIF_VAR_CALLER:
2955 if (!dtrace_priv_kernel(state))
2956 return (0);
2957 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) {
2958 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
2959
2960 if (!DTRACE_ANCHORED(mstate->dtms_probe)) {
2961 /*
2962 * If this is an unanchored probe, we are
2963 * required to go through the slow path:
2964 * dtrace_caller() only guarantees correct
2965 * results for anchored probes.
2966 */
2967 pc_t caller[2];
2968
2969 dtrace_getpcstack(caller, 2, aframes,
2970 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]);
2971 mstate->dtms_caller = caller[1];
2972 } else if ((mstate->dtms_caller =
2973 dtrace_caller(aframes)) == VBDTCAST(uintptr_t)-1) {
2974 /*
2975 * We have failed to do this the quick way;
2976 * we must resort to the slower approach of
2977 * calling dtrace_getpcstack().
2978 */
2979 pc_t caller;
2980
2981 dtrace_getpcstack(&caller, 1, aframes, NULL);
2982 mstate->dtms_caller = caller;
2983 }
2984
2985 mstate->dtms_present |= DTRACE_MSTATE_CALLER;
2986 }
2987 return (mstate->dtms_caller);
2988
2989 case DIF_VAR_UCALLER:
2990 if (!dtrace_priv_proc(state))
2991 return (0);
2992
2993 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) {
2994 uint64_t ustack[3];
2995
2996 /*
2997 * dtrace_getupcstack() fills in the first uint64_t
2998 * with the current PID. The second uint64_t will
2999 * be the program counter at user-level. The third
3000 * uint64_t will contain the caller, which is what
3001 * we're after.
3002 */
3003 ustack[2] = NULL;
3004 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3005 dtrace_getupcstack(ustack, 3);
3006 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3007 mstate->dtms_ucaller = ustack[2];
3008 mstate->dtms_present |= DTRACE_MSTATE_UCALLER;
3009 }
3010
3011 return (mstate->dtms_ucaller);
3012
3013 case DIF_VAR_PROBEPROV:
3014 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3015 return (dtrace_dif_varstr(
3016 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name,
3017 state, mstate));
3018
3019 case DIF_VAR_PROBEMOD:
3020 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3021 return (dtrace_dif_varstr(
3022 (uintptr_t)mstate->dtms_probe->dtpr_mod,
3023 state, mstate));
3024
3025 case DIF_VAR_PROBEFUNC:
3026 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3027 return (dtrace_dif_varstr(
3028 (uintptr_t)mstate->dtms_probe->dtpr_func,
3029 state, mstate));
3030
3031 case DIF_VAR_PROBENAME:
3032 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3033 return (dtrace_dif_varstr(
3034 (uintptr_t)mstate->dtms_probe->dtpr_name,
3035 state, mstate));
3036
3037 case DIF_VAR_PID:
3038 if (!dtrace_priv_proc(state))
3039 return (0);
3040
3041#ifndef VBOX
3042 /*
3043 * Note that we are assuming that an unanchored probe is
3044 * always due to a high-level interrupt. (And we're assuming
3045 * that there is only a single high level interrupt.)
3046 */
3047 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3048 return (pid0.pid_id);
3049
3050 /*
3051 * It is always safe to dereference one's own t_procp pointer:
3052 * it always points to a valid, allocated proc structure.
3053 * Further, it is always safe to dereference the p_pidp member
3054 * of one's own proc structure. (These are truisms becuase
3055 * threads and processes don't clean up their own state --
3056 * they leave that task to whomever reaps them.)
3057 */
3058 return ((uint64_t)curthread->t_procp->p_pidp->pid_id);
3059#else
3060 return (RTProcSelf());
3061#endif
3062
3063 case DIF_VAR_PPID:
3064 if (!dtrace_priv_proc(state))
3065 return (0);
3066
3067#ifndef VBOX
3068 /*
3069 * See comment in DIF_VAR_PID.
3070 */
3071 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3072 return (pid0.pid_id);
3073
3074 /*
3075 * It is always safe to dereference one's own t_procp pointer:
3076 * it always points to a valid, allocated proc structure.
3077 * (This is true because threads don't clean up their own
3078 * state -- they leave that task to whomever reaps them.)
3079 */
3080 return ((uint64_t)curthread->t_procp->p_ppid);
3081#else
3082 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3083 return (0); /** @todo parent pid? */
3084#endif
3085
3086 case DIF_VAR_TID:
3087#ifndef VBOX
3088 /*
3089 * See comment in DIF_VAR_PID.
3090 */
3091 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3092 return (0);
3093
3094 return ((uint64_t)curthread->t_tid);
3095#else
3096 return (RTThreadNativeSelf()); /** @todo proper tid? */
3097#endif
3098
3099 case DIF_VAR_EXECNAME:
3100 if (!dtrace_priv_proc(state))
3101 return (0);
3102
3103#ifndef VBOX
3104 /*
3105 * See comment in DIF_VAR_PID.
3106 */
3107 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3108 return ((uint64_t)(uintptr_t)p0.p_user.u_comm);
3109
3110 /*
3111 * It is always safe to dereference one's own t_procp pointer:
3112 * it always points to a valid, allocated proc structure.
3113 * (This is true because threads don't clean up their own
3114 * state -- they leave that task to whomever reaps them.)
3115 */
3116 return (dtrace_dif_varstr(
3117 (uintptr_t)curthread->t_procp->p_user.u_comm,
3118 state, mstate));
3119#else
3120 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3121 return (0); /** @todo execname */
3122#endif
3123
3124 case DIF_VAR_ZONENAME:
3125 if (!dtrace_priv_proc(state))
3126 return (0);
3127
3128#ifndef VBOX
3129 /*
3130 * See comment in DIF_VAR_PID.
3131 */
3132 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3133 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name);
3134
3135 /*
3136 * It is always safe to dereference one's own t_procp pointer:
3137 * it always points to a valid, allocated proc structure.
3138 * (This is true because threads don't clean up their own
3139 * state -- they leave that task to whomever reaps them.)
3140 */
3141 return (dtrace_dif_varstr(
3142 (uintptr_t)curthread->t_procp->p_zone->zone_name,
3143 state, mstate));
3144#else
3145 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3146 return (0);
3147#endif
3148
3149 case DIF_VAR_UID:
3150 if (!dtrace_priv_proc(state))
3151 return (0);
3152
3153#ifndef VBOX
3154 /*
3155 * See comment in DIF_VAR_PID.
3156 */
3157 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3158 return ((uint64_t)p0.p_cred->cr_uid);
3159
3160 /*
3161 * It is always safe to dereference one's own t_procp pointer:
3162 * it always points to a valid, allocated proc structure.
3163 * (This is true because threads don't clean up their own
3164 * state -- they leave that task to whomever reaps them.)
3165 *
3166 * Additionally, it is safe to dereference one's own process
3167 * credential, since this is never NULL after process birth.
3168 */
3169 return ((uint64_t)curthread->t_procp->p_cred->cr_uid);
3170#else
3171 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3172 return (0);
3173#endif
3174
3175 case DIF_VAR_GID:
3176 if (!dtrace_priv_proc(state))
3177 return (0);
3178
3179#ifndef VBOX
3180 /*
3181 * See comment in DIF_VAR_PID.
3182 */
3183 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3184 return ((uint64_t)p0.p_cred->cr_gid);
3185
3186 /*
3187 * It is always safe to dereference one's own t_procp pointer:
3188 * it always points to a valid, allocated proc structure.
3189 * (This is true because threads don't clean up their own
3190 * state -- they leave that task to whomever reaps them.)
3191 *
3192 * Additionally, it is safe to dereference one's own process
3193 * credential, since this is never NULL after process birth.
3194 */
3195 return ((uint64_t)curthread->t_procp->p_cred->cr_gid);
3196#else
3197 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3198 return (0);
3199#endif
3200
3201 case DIF_VAR_ERRNO: {
3202#ifndef VBOX
3203 klwp_t *lwp;
3204#endif
3205 if (!dtrace_priv_proc(state))
3206 return (0);
3207
3208#ifndef VBOX
3209 /*
3210 * See comment in DIF_VAR_PID.
3211 */
3212 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3213 return (0);
3214
3215 /*
3216 * It is always safe to dereference one's own t_lwp pointer in
3217 * the event that this pointer is non-NULL. (This is true
3218 * because threads and lwps don't clean up their own state --
3219 * they leave that task to whomever reaps them.)
3220 */
3221 if ((lwp = curthread->t_lwp) == NULL)
3222 return (0);
3223
3224 return ((uint64_t)lwp->lwp_errno);
3225#else
3226 cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3227 return (0);
3228#endif
3229 }
3230 default:
3231 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3232 return (0);
3233 }
3234}
3235
3236/*
3237 * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
3238 * Notice that we don't bother validating the proper number of arguments or
3239 * their types in the tuple stack. This isn't needed because all argument
3240 * interpretation is safe because of our load safety -- the worst that can
3241 * happen is that a bogus program can obtain bogus results.
3242 */
3243static void
3244dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
3245 dtrace_key_t *tupregs, int nargs,
3246 dtrace_mstate_t *mstate, dtrace_state_t *state)
3247{
3248 volatile uint16_t *flags = &cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags;
3249 volatile uintptr_t *illval = &cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval;
3250 dtrace_vstate_t *vstate = &state->dts_vstate;
3251
3252#ifndef VBOX
3253 union {
3254 mutex_impl_t mi;
3255 uint64_t mx;
3256 } m;
3257
3258 union {
3259 krwlock_t ri;
3260 uintptr_t rw;
3261 } r;
3262#endif
3263
3264 switch (subr) {
3265 case DIF_SUBR_RAND:
3266 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875;
3267 break;
3268
3269 case DIF_SUBR_MUTEX_OWNED:
3270#ifndef VBOX
3271 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3272 mstate, vstate)) {
3273 regs[rd] = NULL;
3274 break;
3275 }
3276
3277 m.mx = dtrace_load64(tupregs[0].dttk_value);
3278 if (MUTEX_TYPE_ADAPTIVE(&m.mi))
3279 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER;
3280 else
3281 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock);
3282#else
3283 regs[rd] = 0;
3284 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3285#endif
3286 break;
3287
3288 case DIF_SUBR_MUTEX_OWNER:
3289#ifndef VBOX
3290 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3291 mstate, vstate)) {
3292 regs[rd] = NULL;
3293 break;
3294 }
3295
3296 m.mx = dtrace_load64(tupregs[0].dttk_value);
3297 if (MUTEX_TYPE_ADAPTIVE(&m.mi) &&
3298 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER)
3299 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi);
3300 else
3301 regs[rd] = 0;
3302#else
3303 regs[rd] = 0;
3304 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3305#endif
3306 break;
3307
3308 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
3309#ifndef VBOX
3310 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3311 mstate, vstate)) {
3312 regs[rd] = NULL;
3313 break;
3314 }
3315
3316 m.mx = dtrace_load64(tupregs[0].dttk_value);
3317 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi);
3318#else
3319 regs[rd] = 0;
3320 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3321#endif
3322 break;
3323
3324 case DIF_SUBR_MUTEX_TYPE_SPIN:
3325#ifndef VBOX
3326 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3327 mstate, vstate)) {
3328 regs[rd] = NULL;
3329 break;
3330 }
3331
3332 m.mx = dtrace_load64(tupregs[0].dttk_value);
3333 regs[rd] = MUTEX_TYPE_SPIN(&m.mi);
3334#else
3335 regs[rd] = 0;
3336 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3337#endif
3338 break;
3339
3340 case DIF_SUBR_RW_READ_HELD: {
3341#ifndef VBOX
3342 uintptr_t tmp;
3343
3344 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
3345 mstate, vstate)) {
3346 regs[rd] = NULL;
3347 break;
3348 }
3349
3350 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3351 regs[rd] = _RW_READ_HELD(&r.ri, tmp);
3352#else
3353 regs[rd] = 0;
3354 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3355#endif
3356 break;
3357 }
3358
3359 case DIF_SUBR_RW_WRITE_HELD:
3360#ifndef VBOX
3361 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
3362 mstate, vstate)) {
3363 regs[rd] = NULL;
3364 break;
3365 }
3366
3367 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3368 regs[rd] = _RW_WRITE_HELD(&r.ri);
3369#else
3370 regs[rd] = 0;
3371 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3372#endif
3373 break;
3374
3375 case DIF_SUBR_RW_ISWRITER:
3376#ifndef VBOX
3377 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
3378 mstate, vstate)) {
3379 regs[rd] = NULL;
3380 break;
3381 }
3382
3383 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3384 regs[rd] = _RW_ISWRITER(&r.ri);
3385#else
3386 regs[rd] = 0;
3387 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3388#endif
3389 break;
3390
3391 case DIF_SUBR_BCOPY: {
3392 /*
3393 * We need to be sure that the destination is in the scratch
3394 * region -- no other region is allowed.
3395 */
3396 uintptr_t src = tupregs[0].dttk_value;
3397 uintptr_t dest = tupregs[1].dttk_value;
3398 size_t size = tupregs[2].dttk_value;
3399
3400 if (!dtrace_inscratch(dest, size, mstate)) {
3401 *flags |= CPU_DTRACE_BADADDR;
3402 *illval = regs[rd];
3403 break;
3404 }
3405
3406 if (!dtrace_canload(src, size, mstate, vstate)) {
3407 regs[rd] = NULL;
3408 break;
3409 }
3410
3411 dtrace_bcopy((void *)src, (void *)dest, size);
3412 break;
3413 }
3414
3415 case DIF_SUBR_ALLOCA:
3416 case DIF_SUBR_COPYIN: {
3417 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
3418 uint64_t size =
3419 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value;
3420 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size;
3421
3422 /*
3423 * This action doesn't require any credential checks since
3424 * probes will not activate in user contexts to which the
3425 * enabling user does not have permissions.
3426 */
3427
3428 /*
3429 * Rounding up the user allocation size could have overflowed
3430 * a large, bogus allocation (like -1ULL) to 0.
3431 */
3432 if (scratch_size < size ||
3433 !DTRACE_INSCRATCH(mstate, scratch_size)) {
3434 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3435 regs[rd] = NULL;
3436 break;
3437 }
3438
3439 if (subr == DIF_SUBR_COPYIN) {
3440 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3441 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
3442 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3443 }
3444
3445 mstate->dtms_scratch_ptr += scratch_size;
3446 regs[rd] = dest;
3447 break;
3448 }
3449
3450 case DIF_SUBR_COPYINTO: {
3451 uint64_t size = tupregs[1].dttk_value;
3452 uintptr_t dest = tupregs[2].dttk_value;
3453
3454 /*
3455 * This action doesn't require any credential checks since
3456 * probes will not activate in user contexts to which the
3457 * enabling user does not have permissions.
3458 */
3459 if (!dtrace_inscratch(dest, size, mstate)) {
3460 *flags |= CPU_DTRACE_BADADDR;
3461 *illval = regs[rd];
3462 break;
3463 }
3464
3465 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3466 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
3467 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3468 break;
3469 }
3470
3471 case DIF_SUBR_COPYINSTR: {
3472 uintptr_t dest = mstate->dtms_scratch_ptr;
3473 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3474
3475 if (nargs > 1 && tupregs[1].dttk_value < size)
3476 size = tupregs[1].dttk_value + 1;
3477
3478 /*
3479 * This action doesn't require any credential checks since
3480 * probes will not activate in user contexts to which the
3481 * enabling user does not have permissions.
3482 */
3483 if (!DTRACE_INSCRATCH(mstate, size)) {
3484 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3485 regs[rd] = NULL;
3486 break;
3487 }
3488
3489 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3490 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags);
3491 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3492
3493 ((char *)dest)[size - 1] = '\0';
3494 mstate->dtms_scratch_ptr += size;
3495 regs[rd] = dest;
3496 break;
3497 }
3498
3499 case DIF_SUBR_MSGSIZE:
3500 case DIF_SUBR_MSGDSIZE: {
3501#ifndef VBOX
3502 uintptr_t baddr = tupregs[0].dttk_value, daddr;
3503 uintptr_t wptr, rptr;
3504 size_t count = 0;
3505 int cont = 0;
3506
3507 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
3508
3509 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate,
3510 vstate)) {
3511 regs[rd] = NULL;
3512 break;
3513 }
3514
3515 wptr = dtrace_loadptr(baddr +
3516 offsetof(mblk_t, b_wptr));
3517
3518 rptr = dtrace_loadptr(baddr +
3519 offsetof(mblk_t, b_rptr));
3520
3521 if (wptr < rptr) {
3522 *flags |= CPU_DTRACE_BADADDR;
3523 *illval = tupregs[0].dttk_value;
3524 break;
3525 }
3526
3527 daddr = dtrace_loadptr(baddr +
3528 offsetof(mblk_t, b_datap));
3529
3530 baddr = dtrace_loadptr(baddr +
3531 offsetof(mblk_t, b_cont));
3532
3533 /*
3534 * We want to prevent against denial-of-service here,
3535 * so we're only going to search the list for
3536 * dtrace_msgdsize_max mblks.
3537 */
3538 if (cont++ > dtrace_msgdsize_max) {
3539 *flags |= CPU_DTRACE_ILLOP;
3540 break;
3541 }
3542
3543 if (subr == DIF_SUBR_MSGDSIZE) {
3544 if (dtrace_load8(daddr +
3545 offsetof(dblk_t, db_type)) != M_DATA)
3546 continue;
3547 }
3548
3549 count += wptr - rptr;
3550 }
3551
3552 if (!(*flags & CPU_DTRACE_FAULT))
3553 regs[rd] = count;
3554
3555#else
3556 regs[rd] = 0;
3557 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3558#endif
3559 break;
3560 }
3561
3562 case DIF_SUBR_PROGENYOF: {
3563#ifndef VBOX
3564 pid_t pid = tupregs[0].dttk_value;
3565 proc_t *p;
3566 int rval = 0;
3567
3568 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3569
3570 for (p = curthread->t_procp; p != NULL; p = p->p_parent) {
3571 if (p->p_pidp->pid_id == pid) {
3572 rval = 1;
3573 break;
3574 }
3575 }
3576
3577 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3578
3579 regs[rd] = rval;
3580#else
3581 regs[rd] = 0;
3582 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3583#endif
3584 break;
3585 }
3586
3587 case DIF_SUBR_SPECULATION:
3588 regs[rd] = dtrace_speculation(state);
3589 break;
3590
3591 case DIF_SUBR_COPYOUT: {
3592 uintptr_t kaddr = tupregs[0].dttk_value;
3593 uintptr_t uaddr = tupregs[1].dttk_value;
3594 uint64_t size = tupregs[2].dttk_value;
3595
3596 if (!dtrace_destructive_disallow &&
3597 dtrace_priv_proc_control(state) &&
3598 !dtrace_istoxic(kaddr, size)) {
3599 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3600 dtrace_copyout(kaddr, uaddr, size, flags);
3601 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3602 }
3603 break;
3604 }
3605
3606 case DIF_SUBR_COPYOUTSTR: {
3607 uintptr_t kaddr = tupregs[0].dttk_value;
3608 uintptr_t uaddr = tupregs[1].dttk_value;
3609 uint64_t size = tupregs[2].dttk_value;
3610
3611 if (!dtrace_destructive_disallow &&
3612 dtrace_priv_proc_control(state) &&
3613 !dtrace_istoxic(kaddr, size)) {
3614 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3615 dtrace_copyoutstr(kaddr, uaddr, size, flags);
3616 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3617 }
3618 break;
3619 }
3620
3621 case DIF_SUBR_STRLEN: {
3622 size_t sz;
3623 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value;
3624 sz = dtrace_strlen((char *)addr,
3625 state->dts_options[DTRACEOPT_STRSIZE]);
3626
3627 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) {
3628 regs[rd] = NULL;
3629 break;
3630 }
3631
3632 regs[rd] = sz;
3633
3634 break;
3635 }
3636
3637 case DIF_SUBR_STRCHR:
3638 case DIF_SUBR_STRRCHR: {
3639 /*
3640 * We're going to iterate over the string looking for the
3641 * specified character. We will iterate until we have reached
3642 * the string length or we have found the character. If this
3643 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
3644 * of the specified character instead of the first.
3645 */
3646 uintptr_t saddr = tupregs[0].dttk_value;
3647 uintptr_t addr = tupregs[0].dttk_value;
3648 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE];
3649 char c, target = (char)tupregs[1].dttk_value;
3650
3651 for (regs[rd] = NULL; addr < limit; addr++) {
3652 if ((c = dtrace_load8(addr)) == target) {
3653 regs[rd] = addr;
3654
3655 if (subr == DIF_SUBR_STRCHR)
3656 break;
3657 }
3658
3659 if (c == '\0')
3660 break;
3661 }
3662
3663 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) {
3664 regs[rd] = NULL;
3665 break;
3666 }
3667
3668 break;
3669 }
3670
3671 case DIF_SUBR_STRSTR:
3672 case DIF_SUBR_INDEX:
3673 case DIF_SUBR_RINDEX: {
3674 /*
3675 * We're going to iterate over the string looking for the
3676 * specified string. We will iterate until we have reached
3677 * the string length or we have found the string. (Yes, this
3678 * is done in the most naive way possible -- but considering
3679 * that the string we're searching for is likely to be
3680 * relatively short, the complexity of Rabin-Karp or similar
3681 * hardly seems merited.)
3682 */
3683 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value;
3684 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value;
3685 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3686 size_t len = dtrace_strlen(addr, size);
3687 size_t sublen = dtrace_strlen(substr, size);
3688 char *limit = addr + len, *orig = addr;
3689 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1;
3690 int inc = 1;
3691
3692 regs[rd] = notfound;
3693
3694 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) {
3695 regs[rd] = NULL;
3696 break;
3697 }
3698
3699 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate,
3700 vstate)) {
3701 regs[rd] = NULL;
3702 break;
3703 }
3704
3705 /*
3706 * strstr() and index()/rindex() have similar semantics if
3707 * both strings are the empty string: strstr() returns a
3708 * pointer to the (empty) string, and index() and rindex()
3709 * both return index 0 (regardless of any position argument).
3710 */
3711 if (sublen == 0 && len == 0) {
3712 if (subr == DIF_SUBR_STRSTR)
3713 regs[rd] = (uintptr_t)addr;
3714 else
3715 regs[rd] = 0;
3716 break;
3717 }
3718
3719 if (subr != DIF_SUBR_STRSTR) {
3720 if (subr == DIF_SUBR_RINDEX) {
3721 limit = orig - 1;
3722 addr += len;
3723 inc = -1;
3724 }
3725
3726 /*
3727 * Both index() and rindex() take an optional position
3728 * argument that denotes the starting position.
3729 */
3730 if (nargs == 3) {
3731 int64_t pos = (int64_t)tupregs[2].dttk_value;
3732
3733 /*
3734 * If the position argument to index() is
3735 * negative, Perl implicitly clamps it at
3736 * zero. This semantic is a little surprising
3737 * given the special meaning of negative
3738 * positions to similar Perl functions like
3739 * substr(), but it appears to reflect a
3740 * notion that index() can start from a
3741 * negative index and increment its way up to
3742 * the string. Given this notion, Perl's
3743 * rindex() is at least self-consistent in
3744 * that it implicitly clamps positions greater
3745 * than the string length to be the string
3746 * length. Where Perl completely loses
3747 * coherence, however, is when the specified
3748 * substring is the empty string (""). In
3749 * this case, even if the position is
3750 * negative, rindex() returns 0 -- and even if
3751 * the position is greater than the length,
3752 * index() returns the string length. These
3753 * semantics violate the notion that index()
3754 * should never return a value less than the
3755 * specified position and that rindex() should
3756 * never return a value greater than the
3757 * specified position. (One assumes that
3758 * these semantics are artifacts of Perl's
3759 * implementation and not the results of
3760 * deliberate design -- it beggars belief that
3761 * even Larry Wall could desire such oddness.)
3762 * While in the abstract one would wish for
3763 * consistent position semantics across
3764 * substr(), index() and rindex() -- or at the
3765 * very least self-consistent position
3766 * semantics for index() and rindex() -- we
3767 * instead opt to keep with the extant Perl
3768 * semantics, in all their broken glory. (Do
3769 * we have more desire to maintain Perl's
3770 * semantics than Perl does? Probably.)
3771 */
3772 if (subr == DIF_SUBR_RINDEX) {
3773 if (pos < 0) {
3774 if (sublen == 0)
3775 regs[rd] = 0;
3776 break;
3777 }
3778
3779 if (VBDTCAST(uint64_t)pos > len)
3780 pos = len;
3781 } else {
3782 if (pos < 0)
3783 pos = 0;
3784
3785 if (VBDTCAST(uint64_t)pos >= len) {
3786 if (sublen == 0)
3787 regs[rd] = len;
3788 break;
3789 }
3790 }
3791
3792 addr = orig + pos;
3793 }
3794 }
3795
3796 for (regs[rd] = notfound; addr != limit; addr += inc) {
3797 if (dtrace_strncmp(addr, substr, sublen) == 0) {
3798 if (subr != DIF_SUBR_STRSTR) {
3799 /*
3800 * As D index() and rindex() are
3801 * modeled on Perl (and not on awk),
3802 * we return a zero-based (and not a
3803 * one-based) index. (For you Perl
3804 * weenies: no, we're not going to add
3805 * $[ -- and shouldn't you be at a con
3806 * or something?)
3807 */
3808 regs[rd] = (uintptr_t)(addr - orig);
3809 break;
3810 }
3811
3812 ASSERT(subr == DIF_SUBR_STRSTR);
3813 regs[rd] = (uintptr_t)addr;
3814 break;
3815 }
3816 }
3817
3818 break;
3819 }
3820
3821 case DIF_SUBR_STRTOK: {
3822 uintptr_t addr = tupregs[0].dttk_value;
3823 uintptr_t tokaddr = tupregs[1].dttk_value;
3824 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3825 uintptr_t limit, toklimit = tokaddr + size;
3826 uint8_t c VBDTUNASS(0), tokmap[32]; /* 256 / 8 */
3827 char *dest = (char *)mstate->dtms_scratch_ptr;
3828 VBDTTYPE(unsigned,int) i;
3829
3830 /*
3831 * Check both the token buffer and (later) the input buffer,
3832 * since both could be non-scratch addresses.
3833 */
3834 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) {
3835 regs[rd] = NULL;
3836 break;
3837 }
3838
3839 if (!DTRACE_INSCRATCH(mstate, size)) {
3840 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3841 regs[rd] = NULL;
3842 break;
3843 }
3844
3845 if (addr == NULL) {
3846 /*
3847 * If the address specified is NULL, we use our saved
3848 * strtok pointer from the mstate. Note that this
3849 * means that the saved strtok pointer is _only_
3850 * valid within multiple enablings of the same probe --
3851 * it behaves like an implicit clause-local variable.
3852 */
3853 addr = mstate->dtms_strtok;
3854 } else {
3855 /*
3856 * If the user-specified address is non-NULL we must
3857 * access check it. This is the only time we have
3858 * a chance to do so, since this address may reside
3859 * in the string table of this clause-- future calls
3860 * (when we fetch addr from mstate->dtms_strtok)
3861 * would fail this access check.
3862 */
3863 if (!dtrace_strcanload(addr, size, mstate, vstate)) {
3864 regs[rd] = NULL;
3865 break;
3866 }
3867 }
3868
3869 /*
3870 * First, zero the token map, and then process the token
3871 * string -- setting a bit in the map for every character
3872 * found in the token string.
3873 */
3874 for (i = 0; i < sizeof (tokmap); i++)
3875 tokmap[i] = 0;
3876
3877 for (; tokaddr < toklimit; tokaddr++) {
3878 if ((c = dtrace_load8(tokaddr)) == '\0')
3879 break;
3880
3881 ASSERT((c >> 3) < sizeof (tokmap));
3882 tokmap[c >> 3] |= (1 << (c & 0x7));
3883 }
3884
3885 for (limit = addr + size; addr < limit; addr++) {
3886 /*
3887 * We're looking for a character that is _not_ contained
3888 * in the token string.
3889 */
3890 if ((c = dtrace_load8(addr)) == '\0')
3891 break;
3892
3893 if (!(tokmap[c >> 3] & (1 << (c & 0x7))))
3894 break;
3895 }
3896
3897 if (c == '\0') {
3898 /*
3899 * We reached the end of the string without finding
3900 * any character that was not in the token string.
3901 * We return NULL in this case, and we set the saved
3902 * address to NULL as well.
3903 */
3904 regs[rd] = NULL;
3905 mstate->dtms_strtok = NULL;
3906 break;
3907 }
3908
3909 /*
3910 * From here on, we're copying into the destination string.
3911 */
3912 for (i = 0; addr < limit && i < size - 1; addr++) {
3913 if ((c = dtrace_load8(addr)) == '\0')
3914 break;
3915
3916 if (tokmap[c >> 3] & (1 << (c & 0x7)))
3917 break;
3918
3919 ASSERT(i < size);
3920 dest[i++] = c;
3921 }
3922
3923 ASSERT(i < size);
3924 dest[i] = '\0';
3925 regs[rd] = (uintptr_t)dest;
3926 mstate->dtms_scratch_ptr += size;
3927 mstate->dtms_strtok = addr;
3928 break;
3929 }
3930
3931 case DIF_SUBR_SUBSTR: {
3932 uintptr_t s = tupregs[0].dttk_value;
3933 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3934 char *d = (char *)mstate->dtms_scratch_ptr;
3935 int64_t index = (int64_t)tupregs[1].dttk_value;
3936 int64_t remaining = (int64_t)tupregs[2].dttk_value;
3937 size_t len = dtrace_strlen((char *)s, size);
3938 int64_t i;
3939
3940 if (!dtrace_canload(s, len + 1, mstate, vstate)) {
3941 regs[rd] = NULL;
3942 break;
3943 }
3944
3945 if (!DTRACE_INSCRATCH(mstate, size)) {
3946 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3947 regs[rd] = NULL;
3948 break;
3949 }
3950
3951 if (nargs <= 2)
3952 remaining = (int64_t)size;
3953
3954 if (index < 0) {
3955 index += len;
3956
3957 if (index < 0 && index + remaining > 0) {
3958 remaining += index;
3959 index = 0;
3960 }
3961 }
3962
3963 if (VBDTCAST(uint64_t)index >= len || index < 0) {
3964 remaining = 0;
3965 } else if (remaining < 0) {
3966 remaining += len - index;
3967 } else if (VBDTCAST(uint64_t)index + remaining > size) {
3968 remaining = size - index;
3969 }
3970
3971 for (i = 0; i < remaining; i++) {
3972 if ((d[i] = dtrace_load8(s + index + i)) == '\0')
3973 break;
3974 }
3975
3976 d[i] = '\0';
3977
3978 mstate->dtms_scratch_ptr += size;
3979 regs[rd] = (uintptr_t)d;
3980 break;
3981 }
3982
3983 case DIF_SUBR_GETMAJOR:
3984#ifndef VBOX
3985#ifdef _LP64
3986 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64;
3987#else
3988 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ;
3989#endif
3990#else
3991 regs[rd] = 0;
3992 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3993#endif
3994 break;
3995
3996 case DIF_SUBR_GETMINOR:
3997#ifndef VBOX
3998#ifdef _LP64
3999 regs[rd] = tupregs[0].dttk_value & MAXMIN64;
4000#else
4001 regs[rd] = tupregs[0].dttk_value & MAXMIN;
4002#endif
4003#else
4004 regs[rd] = 0;
4005 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4006#endif
4007 break;
4008
4009 case DIF_SUBR_DDI_PATHNAME: {
4010#ifndef VBOX
4011 /*
4012 * This one is a galactic mess. We are going to roughly
4013 * emulate ddi_pathname(), but it's made more complicated
4014 * by the fact that we (a) want to include the minor name and
4015 * (b) must proceed iteratively instead of recursively.
4016 */
4017 uintptr_t dest = mstate->dtms_scratch_ptr;
4018 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4019 char *start = (char *)dest, *end = start + size - 1;
4020 uintptr_t daddr = tupregs[0].dttk_value;
4021 int64_t minor = (int64_t)tupregs[1].dttk_value;
4022 char *s;
4023 int i, len, depth = 0;
4024
4025 /*
4026 * Due to all the pointer jumping we do and context we must
4027 * rely upon, we just mandate that the user must have kernel
4028 * read privileges to use this routine.
4029 */
4030 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) {
4031 *flags |= CPU_DTRACE_KPRIV;
4032 *illval = daddr;
4033 regs[rd] = NULL;
4034 }
4035
4036 if (!DTRACE_INSCRATCH(mstate, size)) {
4037 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4038 regs[rd] = NULL;
4039 break;
4040 }
4041
4042 *end = '\0';
4043
4044 /*
4045 * We want to have a name for the minor. In order to do this,
4046 * we need to walk the minor list from the devinfo. We want
4047 * to be sure that we don't infinitely walk a circular list,
4048 * so we check for circularity by sending a scout pointer
4049 * ahead two elements for every element that we iterate over;
4050 * if the list is circular, these will ultimately point to the
4051 * same element. You may recognize this little trick as the
4052 * answer to a stupid interview question -- one that always
4053 * seems to be asked by those who had to have it laboriously
4054 * explained to them, and who can't even concisely describe
4055 * the conditions under which one would be forced to resort to
4056 * this technique. Needless to say, those conditions are
4057 * found here -- and probably only here. Is this the only use
4058 * of this infamous trick in shipping, production code? If it
4059 * isn't, it probably should be...
4060 */
4061 if (minor != -1) {
4062 uintptr_t maddr = dtrace_loadptr(daddr +
4063 offsetof(struct dev_info, devi_minor));
4064
4065 uintptr_t next = offsetof(struct ddi_minor_data, next);
4066 uintptr_t name = offsetof(struct ddi_minor_data,
4067 d_minor) + offsetof(struct ddi_minor, name);
4068 uintptr_t dev = offsetof(struct ddi_minor_data,
4069 d_minor) + offsetof(struct ddi_minor, dev);
4070 uintptr_t scout;
4071
4072 if (maddr != NULL)
4073 scout = dtrace_loadptr(maddr + next);
4074
4075 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4076 uint64_t m;
4077#ifdef _LP64
4078 m = dtrace_load64(maddr + dev) & MAXMIN64;
4079#else
4080 m = dtrace_load32(maddr + dev) & MAXMIN;
4081#endif
4082 if (m != minor) {
4083 maddr = dtrace_loadptr(maddr + next);
4084
4085 if (scout == NULL)
4086 continue;
4087
4088 scout = dtrace_loadptr(scout + next);
4089
4090 if (scout == NULL)
4091 continue;
4092
4093 scout = dtrace_loadptr(scout + next);
4094
4095 if (scout == NULL)
4096 continue;
4097
4098 if (scout == maddr) {
4099 *flags |= CPU_DTRACE_ILLOP;
4100 break;
4101 }
4102
4103 continue;
4104 }
4105
4106 /*
4107 * We have the minor data. Now we need to
4108 * copy the minor's name into the end of the
4109 * pathname.
4110 */
4111 s = (char *)dtrace_loadptr(maddr + name);
4112 len = dtrace_strlen(s, size);
4113
4114 if (*flags & CPU_DTRACE_FAULT)
4115 break;
4116
4117 if (len != 0) {
4118 if ((end -= (len + 1)) < start)
4119 break;
4120
4121 *end = ':';
4122 }
4123
4124 for (i = 1; i <= len; i++)
4125 end[i] = dtrace_load8((uintptr_t)s++);
4126 break;
4127 }
4128 }
4129
4130 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4131 ddi_node_state_t devi_state;
4132
4133 devi_state = dtrace_load32(daddr +
4134 offsetof(struct dev_info, devi_node_state));
4135
4136 if (*flags & CPU_DTRACE_FAULT)
4137 break;
4138
4139 if (devi_state >= DS_INITIALIZED) {
4140 s = (char *)dtrace_loadptr(daddr +
4141 offsetof(struct dev_info, devi_addr));
4142 len = dtrace_strlen(s, size);
4143
4144 if (*flags & CPU_DTRACE_FAULT)
4145 break;
4146
4147 if (len != 0) {
4148 if ((end -= (len + 1)) < start)
4149 break;
4150
4151 *end = '@';
4152 }
4153
4154 for (i = 1; i <= len; i++)
4155 end[i] = dtrace_load8((uintptr_t)s++);
4156 }
4157
4158 /*
4159 * Now for the node name...
4160 */
4161 s = (char *)dtrace_loadptr(daddr +
4162 offsetof(struct dev_info, devi_node_name));
4163
4164 daddr = dtrace_loadptr(daddr +
4165 offsetof(struct dev_info, devi_parent));
4166
4167 /*
4168 * If our parent is NULL (that is, if we're the root
4169 * node), we're going to use the special path
4170 * "devices".
4171 */
4172 if (daddr == NULL)
4173 s = "devices";
4174
4175 len = dtrace_strlen(s, size);
4176 if (*flags & CPU_DTRACE_FAULT)
4177 break;
4178
4179 if ((end -= (len + 1)) < start)
4180 break;
4181
4182 for (i = 1; i <= len; i++)
4183 end[i] = dtrace_load8((uintptr_t)s++);
4184 *end = '/';
4185
4186 if (depth++ > dtrace_devdepth_max) {
4187 *flags |= CPU_DTRACE_ILLOP;
4188 break;
4189 }
4190 }
4191
4192 if (end < start)
4193 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4194
4195 if (daddr == NULL) {
4196 regs[rd] = (uintptr_t)end;
4197 mstate->dtms_scratch_ptr += size;
4198 }
4199
4200#else
4201 regs[rd] = 0;
4202 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4203#endif
4204 break;
4205 }
4206
4207 case DIF_SUBR_STRJOIN: {
4208 char *d = (char *)mstate->dtms_scratch_ptr;
4209 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4210 uintptr_t s1 = tupregs[0].dttk_value;
4211 uintptr_t s2 = tupregs[1].dttk_value;
4212 VBDTTYPE(unsigned,int) i = 0;
4213
4214 if (!dtrace_strcanload(s1, size, mstate, vstate) ||
4215 !dtrace_strcanload(s2, size, mstate, vstate)) {
4216 regs[rd] = NULL;
4217 break;
4218 }
4219
4220 if (!DTRACE_INSCRATCH(mstate, size)) {
4221 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4222 regs[rd] = NULL;
4223 break;
4224 }
4225
4226 for (;;) {
4227 if (i >= size) {
4228 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4229 regs[rd] = NULL;
4230 break;
4231 }
4232
4233 if ((d[i++] = dtrace_load8(s1++)) == '\0') {
4234 i--;
4235 break;
4236 }
4237 }
4238
4239 for (;;) {
4240 if (i >= size) {
4241 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4242 regs[rd] = NULL;
4243 break;
4244 }
4245
4246 if ((d[i++] = dtrace_load8(s2++)) == '\0')
4247 break;
4248 }
4249
4250 if (i < size) {
4251 mstate->dtms_scratch_ptr += i;
4252 regs[rd] = (uintptr_t)d;
4253 }
4254
4255 break;
4256 }
4257
4258 case DIF_SUBR_LLTOSTR: {
4259 int64_t i = (int64_t)tupregs[0].dttk_value;
4260 int64_t val = i < 0 ? i * -1 : i;
4261 uint64_t size = 22; /* enough room for 2^64 in decimal */
4262 char *end = (char *)mstate->dtms_scratch_ptr + size - 1;
4263
4264 if (!DTRACE_INSCRATCH(mstate, size)) {
4265 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4266 regs[rd] = NULL;
4267 break;
4268 }
4269
4270 for (*end-- = '\0'; val; val /= 10)
4271 *end-- = '0' + (val % 10);
4272
4273 if (i == 0)
4274 *end-- = '0';
4275
4276 if (i < 0)
4277 *end-- = '-';
4278
4279 regs[rd] = (uintptr_t)end + 1;
4280 mstate->dtms_scratch_ptr += size;
4281 break;
4282 }
4283
4284 case DIF_SUBR_HTONS:
4285 case DIF_SUBR_NTOHS:
4286#ifdef _BIG_ENDIAN
4287 regs[rd] = (uint16_t)tupregs[0].dttk_value;
4288#else
4289 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value);
4290#endif
4291 break;
4292
4293
4294 case DIF_SUBR_HTONL:
4295 case DIF_SUBR_NTOHL:
4296#ifdef _BIG_ENDIAN
4297 regs[rd] = (uint32_t)tupregs[0].dttk_value;
4298#else
4299 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value);
4300#endif
4301 break;
4302
4303
4304 case DIF_SUBR_HTONLL:
4305 case DIF_SUBR_NTOHLL:
4306#ifdef _BIG_ENDIAN
4307 regs[rd] = (uint64_t)tupregs[0].dttk_value;
4308#else
4309 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value);
4310#endif
4311 break;
4312
4313
4314 case DIF_SUBR_DIRNAME:
4315 case DIF_SUBR_BASENAME: {
4316 char *dest = (char *)mstate->dtms_scratch_ptr;
4317 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4318 uintptr_t src = tupregs[0].dttk_value;
4319 int i, j, len = VBDTCAST(int)dtrace_strlen((char *)src, size);
4320 int lastbase = -1, firstbase = -1, lastdir = -1;
4321 int start, end;
4322
4323 if (!dtrace_canload(src, len + 1, mstate, vstate)) {
4324 regs[rd] = NULL;
4325 break;
4326 }
4327
4328 if (!DTRACE_INSCRATCH(mstate, size)) {
4329 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4330 regs[rd] = NULL;
4331 break;
4332 }
4333
4334 /*
4335 * The basename and dirname for a zero-length string is
4336 * defined to be "."
4337 */
4338 if (len == 0) {
4339 len = 1;
4340 src = (uintptr_t)".";
4341 }
4342
4343 /*
4344 * Start from the back of the string, moving back toward the
4345 * front until we see a character that isn't a slash. That
4346 * character is the last character in the basename.
4347 */
4348 for (i = len - 1; i >= 0; i--) {
4349 if (dtrace_load8(src + i) != '/')
4350 break;
4351 }
4352
4353 if (i >= 0)
4354 lastbase = i;
4355
4356 /*
4357 * Starting from the last character in the basename, move
4358 * towards the front until we find a slash. The character
4359 * that we processed immediately before that is the first
4360 * character in the basename.
4361 */
4362 for (; i >= 0; i--) {
4363 if (dtrace_load8(src + i) == '/')
4364 break;
4365 }
4366
4367 if (i >= 0)
4368 firstbase = i + 1;
4369
4370 /*
4371 * Now keep going until we find a non-slash character. That
4372 * character is the last character in the dirname.
4373 */
4374 for (; i >= 0; i--) {
4375 if (dtrace_load8(src + i) != '/')
4376 break;
4377 }
4378
4379 if (i >= 0)
4380 lastdir = i;
4381
4382 ASSERT(!(lastbase == -1 && firstbase != -1));
4383 ASSERT(!(firstbase == -1 && lastdir != -1));
4384
4385 if (lastbase == -1) {
4386 /*
4387 * We didn't find a non-slash character. We know that
4388 * the length is non-zero, so the whole string must be
4389 * slashes. In either the dirname or the basename
4390 * case, we return '/'.
4391 */
4392 ASSERT(firstbase == -1);
4393 firstbase = lastbase = lastdir = 0;
4394 }
4395
4396 if (firstbase == -1) {
4397 /*
4398 * The entire string consists only of a basename
4399 * component. If we're looking for dirname, we need
4400 * to change our string to be just "."; if we're
4401 * looking for a basename, we'll just set the first
4402 * character of the basename to be 0.
4403 */
4404 if (subr == DIF_SUBR_DIRNAME) {
4405 ASSERT(lastdir == -1);
4406 src = (uintptr_t)".";
4407 lastdir = 0;
4408 } else {
4409 firstbase = 0;
4410 }
4411 }
4412
4413 if (subr == DIF_SUBR_DIRNAME) {
4414 if (lastdir == -1) {
4415 /*
4416 * We know that we have a slash in the name --
4417 * or lastdir would be set to 0, above. And
4418 * because lastdir is -1, we know that this
4419 * slash must be the first character. (That
4420 * is, the full string must be of the form
4421 * "/basename".) In this case, the last
4422 * character of the directory name is 0.
4423 */
4424 lastdir = 0;
4425 }
4426
4427 start = 0;
4428 end = lastdir;
4429 } else {
4430 ASSERT(subr == DIF_SUBR_BASENAME);
4431 ASSERT(firstbase != -1 && lastbase != -1);
4432 start = firstbase;
4433 end = lastbase;
4434 }
4435
4436 for (i = start, j = 0; i <= end && VBDTCAST(unsigned)j < size - 1; i++, j++)
4437 dest[j] = dtrace_load8(src + i);
4438
4439 dest[j] = '\0';
4440 regs[rd] = (uintptr_t)dest;
4441 mstate->dtms_scratch_ptr += size;
4442 break;
4443 }
4444
4445 case DIF_SUBR_CLEANPATH: {
4446 char *dest = (char *)mstate->dtms_scratch_ptr, c;
4447 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4448 uintptr_t src = tupregs[0].dttk_value;
4449 int i = 0, j = 0;
4450
4451 if (!dtrace_strcanload(src, size, mstate, vstate)) {
4452 regs[rd] = NULL;
4453 break;
4454 }
4455
4456 if (!DTRACE_INSCRATCH(mstate, size)) {
4457 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4458 regs[rd] = NULL;
4459 break;
4460 }
4461
4462 /*
4463 * Move forward, loading each character.
4464 */
4465 do {
4466 c = dtrace_load8(src + i++);
4467next:
4468 if (j + 5 >= VBDTCAST(int64_t)size) /* 5 = strlen("/..c\0") */
4469 break;
4470
4471 if (c != '/') {
4472 dest[j++] = c;
4473 continue;
4474 }
4475
4476 c = dtrace_load8(src + i++);
4477
4478 if (c == '/') {
4479 /*
4480 * We have two slashes -- we can just advance
4481 * to the next character.
4482 */
4483 goto next;
4484 }
4485
4486 if (c != '.') {
4487 /*
4488 * This is not "." and it's not ".." -- we can
4489 * just store the "/" and this character and
4490 * drive on.
4491 */
4492 dest[j++] = '/';
4493 dest[j++] = c;
4494 continue;
4495 }
4496
4497 c = dtrace_load8(src + i++);
4498
4499 if (c == '/') {
4500 /*
4501 * This is a "/./" component. We're not going
4502 * to store anything in the destination buffer;
4503 * we're just going to go to the next component.
4504 */
4505 goto next;
4506 }
4507
4508 if (c != '.') {
4509 /*
4510 * This is not ".." -- we can just store the
4511 * "/." and this character and continue
4512 * processing.
4513 */
4514 dest[j++] = '/';
4515 dest[j++] = '.';
4516 dest[j++] = c;
4517 continue;
4518 }
4519
4520 c = dtrace_load8(src + i++);
4521
4522 if (c != '/' && c != '\0') {
4523 /*
4524 * This is not ".." -- it's "..[mumble]".
4525 * We'll store the "/.." and this character
4526 * and continue processing.
4527 */
4528 dest[j++] = '/';
4529 dest[j++] = '.';
4530 dest[j++] = '.';
4531 dest[j++] = c;
4532 continue;
4533 }
4534
4535 /*
4536 * This is "/../" or "/..\0". We need to back up
4537 * our destination pointer until we find a "/".
4538 */
4539 i--;
4540 while (j != 0 && dest[--j] != '/')
4541 continue;
4542
4543 if (c == '\0')
4544 dest[++j] = '/';
4545 } while (c != '\0');
4546
4547 dest[j] = '\0';
4548 regs[rd] = (uintptr_t)dest;
4549 mstate->dtms_scratch_ptr += size;
4550 break;
4551 }
4552
4553 case DIF_SUBR_INET_NTOA:
4554 case DIF_SUBR_INET_NTOA6:
4555 case DIF_SUBR_INET_NTOP: {
4556#ifndef VBOX
4557 size_t size;
4558 int af, argi, i;
4559 char *base, *end;
4560
4561 if (subr == DIF_SUBR_INET_NTOP) {
4562 af = (int)tupregs[0].dttk_value;
4563 argi = 1;
4564 } else {
4565 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6;
4566 argi = 0;
4567 }
4568
4569 if (af == AF_INET) {
4570 ipaddr_t ip4;
4571 uint8_t *ptr8, val;
4572
4573 /*
4574 * Safely load the IPv4 address.
4575 */
4576 ip4 = dtrace_load32(tupregs[argi].dttk_value);
4577
4578 /*
4579 * Check an IPv4 string will fit in scratch.
4580 */
4581 size = INET_ADDRSTRLEN;
4582 if (!DTRACE_INSCRATCH(mstate, size)) {
4583 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4584 regs[rd] = NULL;
4585 break;
4586 }
4587 base = (char *)mstate->dtms_scratch_ptr;
4588 end = (char *)mstate->dtms_scratch_ptr + size - 1;
4589
4590 /*
4591 * Stringify as a dotted decimal quad.
4592 */
4593 *end-- = '\0';
4594 ptr8 = (uint8_t *)&ip4;
4595 for (i = 3; i >= 0; i--) {
4596 val = ptr8[i];
4597
4598 if (val == 0) {
4599 *end-- = '0';
4600 } else {
4601 for (; val; val /= 10) {
4602 *end-- = '0' + (val % 10);
4603 }
4604 }
4605
4606 if (i > 0)
4607 *end-- = '.';
4608 }
4609 ASSERT(end + 1 >= base);
4610
4611 } else if (af == AF_INET6) {
4612 struct in6_addr ip6;
4613 int firstzero, tryzero, numzero, v6end;
4614 uint16_t val;
4615 const char digits[] = "0123456789abcdef";
4616
4617 /*
4618 * Stringify using RFC 1884 convention 2 - 16 bit
4619 * hexadecimal values with a zero-run compression.
4620 * Lower case hexadecimal digits are used.
4621 * eg, fe80::214:4fff:fe0b:76c8.
4622 * The IPv4 embedded form is returned for inet_ntop,
4623 * just the IPv4 string is returned for inet_ntoa6.
4624 */
4625
4626 /*
4627 * Safely load the IPv6 address.
4628 */
4629 dtrace_bcopy(
4630 (void *)(uintptr_t)tupregs[argi].dttk_value,
4631 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr));
4632
4633 /*
4634 * Check an IPv6 string will fit in scratch.
4635 */
4636 size = INET6_ADDRSTRLEN;
4637 if (!DTRACE_INSCRATCH(mstate, size)) {
4638 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4639 regs[rd] = NULL;
4640 break;
4641 }
4642 base = (char *)mstate->dtms_scratch_ptr;
4643 end = (char *)mstate->dtms_scratch_ptr + size - 1;
4644 *end-- = '\0';
4645
4646 /*
4647 * Find the longest run of 16 bit zero values
4648 * for the single allowed zero compression - "::".
4649 */
4650 firstzero = -1;
4651 tryzero = -1;
4652 numzero = 1;
4653 for (i = 0; i < sizeof (struct in6_addr); i++) {
4654 if (ip6._S6_un._S6_u8[i] == 0 &&
4655 tryzero == -1 && i % 2 == 0) {
4656 tryzero = i;
4657 continue;
4658 }
4659
4660 if (tryzero != -1 &&
4661 (ip6._S6_un._S6_u8[i] != 0 ||
4662 i == sizeof (struct in6_addr) - 1)) {
4663
4664 if (i - tryzero <= numzero) {
4665 tryzero = -1;
4666 continue;
4667 }
4668
4669 firstzero = tryzero;
4670 numzero = i - i % 2 - tryzero;
4671 tryzero = -1;
4672
4673 if (ip6._S6_un._S6_u8[i] == 0 &&
4674 i == sizeof (struct in6_addr) - 1)
4675 numzero += 2;
4676 }
4677 }
4678 ASSERT(firstzero + numzero <= sizeof (struct in6_addr));
4679
4680 /*
4681 * Check for an IPv4 embedded address.
4682 */
4683 v6end = sizeof (struct in6_addr) - 2;
4684 if (IN6_IS_ADDR_V4MAPPED(&ip6) ||
4685 IN6_IS_ADDR_V4COMPAT(&ip6)) {
4686 for (i = sizeof (struct in6_addr) - 1;
4687 i >= DTRACE_V4MAPPED_OFFSET; i--) {
4688 ASSERT(end >= base);
4689
4690 val = ip6._S6_un._S6_u8[i];
4691
4692 if (val == 0) {
4693 *end-- = '0';
4694 } else {
4695 for (; val; val /= 10) {
4696 *end-- = '0' + val % 10;
4697 }
4698 }
4699
4700 if (i > DTRACE_V4MAPPED_OFFSET)
4701 *end-- = '.';
4702 }
4703
4704 if (subr == DIF_SUBR_INET_NTOA6)
4705 goto inetout;
4706
4707 /*
4708 * Set v6end to skip the IPv4 address that
4709 * we have already stringified.
4710 */
4711 v6end = 10;
4712 }
4713
4714 /*
4715 * Build the IPv6 string by working through the
4716 * address in reverse.
4717 */
4718 for (i = v6end; i >= 0; i -= 2) {
4719 ASSERT(end >= base);
4720
4721 if (i == firstzero + numzero - 2) {
4722 *end-- = ':';
4723 *end-- = ':';
4724 i -= numzero - 2;
4725 continue;
4726 }
4727
4728 if (i < 14 && i != firstzero - 2)
4729 *end-- = ':';
4730
4731 val = (ip6._S6_un._S6_u8[i] << 8) +
4732 ip6._S6_un._S6_u8[i + 1];
4733
4734 if (val == 0) {
4735 *end-- = '0';
4736 } else {
4737 for (; val; val /= 16) {
4738 *end-- = digits[val % 16];
4739 }
4740 }
4741 }
4742 ASSERT(end + 1 >= base);
4743
4744 } else {
4745 /*
4746 * The user didn't use AH_INET or AH_INET6.
4747 */
4748 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4749 regs[rd] = NULL;
4750 break;
4751 }
4752
4753inetout: regs[rd] = (uintptr_t)end + 1;
4754 mstate->dtms_scratch_ptr += size;
4755#else /* VBOX */
4756 regs[rd] = 0;
4757 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4758#endif /* VBOX */
4759 break;
4760 }
4761
4762 }
4763}
4764
4765/*
4766 * Emulate the execution of DTrace IR instructions specified by the given
4767 * DIF object. This function is deliberately void of assertions as all of
4768 * the necessary checks are handled by a call to dtrace_difo_validate().
4769 */
4770static uint64_t
4771dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate,
4772 dtrace_vstate_t *vstate, dtrace_state_t *state)
4773{
4774 const dif_instr_t *text = difo->dtdo_buf;
4775 const uint_t textlen = difo->dtdo_len;
4776 const char *strtab = difo->dtdo_strtab;
4777 const uint64_t *inttab = difo->dtdo_inttab;
4778
4779 uint64_t rval = 0;
4780 dtrace_statvar_t *svar;
4781 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
4782 dtrace_difv_t *v;
4783 volatile uint16_t *flags = &cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags;
4784 volatile uintptr_t *illval = &cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval;
4785
4786 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
4787 uint64_t regs[DIF_DIR_NREGS];
4788 uint64_t *tmp;
4789
4790 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0;
4791 int64_t cc_r;
4792 uint_t pc = 0, id, opc VBDTUNASS(0);
4793 uint8_t ttop = 0;
4794 dif_instr_t instr;
4795 uint_t r1, r2, rd;
4796
4797 /*
4798 * We stash the current DIF object into the machine state: we need it
4799 * for subsequent access checking.
4800 */
4801 mstate->dtms_difo = difo;
4802
4803 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */
4804
4805 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) {
4806 opc = pc;
4807
4808 instr = text[pc++];
4809 r1 = DIF_INSTR_R1(instr);
4810 r2 = DIF_INSTR_R2(instr);
4811 rd = DIF_INSTR_RD(instr);
4812
4813 switch (DIF_INSTR_OP(instr)) {
4814 case DIF_OP_OR:
4815 regs[rd] = regs[r1] | regs[r2];
4816 break;
4817 case DIF_OP_XOR:
4818 regs[rd] = regs[r1] ^ regs[r2];
4819 break;
4820 case DIF_OP_AND:
4821 regs[rd] = regs[r1] & regs[r2];
4822 break;
4823 case DIF_OP_SLL:
4824 regs[rd] = regs[r1] << regs[r2];
4825 break;
4826 case DIF_OP_SRL:
4827 regs[rd] = regs[r1] >> regs[r2];
4828 break;
4829 case DIF_OP_SUB:
4830 regs[rd] = regs[r1] - regs[r2];
4831 break;
4832 case DIF_OP_ADD:
4833 regs[rd] = regs[r1] + regs[r2];
4834 break;
4835 case DIF_OP_MUL:
4836 regs[rd] = regs[r1] * regs[r2];
4837 break;
4838 case DIF_OP_SDIV:
4839 if (regs[r2] == 0) {
4840 regs[rd] = 0;
4841 *flags |= CPU_DTRACE_DIVZERO;
4842 } else {
4843 regs[rd] = (int64_t)regs[r1] /
4844 (int64_t)regs[r2];
4845 }
4846 break;
4847
4848 case DIF_OP_UDIV:
4849 if (regs[r2] == 0) {
4850 regs[rd] = 0;
4851 *flags |= CPU_DTRACE_DIVZERO;
4852 } else {
4853 regs[rd] = regs[r1] / regs[r2];
4854 }
4855 break;
4856
4857 case DIF_OP_SREM:
4858 if (regs[r2] == 0) {
4859 regs[rd] = 0;
4860 *flags |= CPU_DTRACE_DIVZERO;
4861 } else {
4862 regs[rd] = (int64_t)regs[r1] %
4863 (int64_t)regs[r2];
4864 }
4865 break;
4866
4867 case DIF_OP_UREM:
4868 if (regs[r2] == 0) {
4869 regs[rd] = 0;
4870 *flags |= CPU_DTRACE_DIVZERO;
4871 } else {
4872 regs[rd] = regs[r1] % regs[r2];
4873 }
4874 break;
4875
4876 case DIF_OP_NOT:
4877 regs[rd] = ~regs[r1];
4878 break;
4879 case DIF_OP_MOV:
4880 regs[rd] = regs[r1];
4881 break;
4882 case DIF_OP_CMP:
4883 cc_r = regs[r1] - regs[r2];
4884 cc_n = cc_r < 0;
4885 cc_z = cc_r == 0;
4886 cc_v = 0;
4887 cc_c = regs[r1] < regs[r2];
4888 break;
4889 case DIF_OP_TST:
4890 cc_n = cc_v = cc_c = 0;
4891 cc_z = regs[r1] == 0;
4892 break;
4893 case DIF_OP_BA:
4894 pc = DIF_INSTR_LABEL(instr);
4895 break;
4896 case DIF_OP_BE:
4897 if (cc_z)
4898 pc = DIF_INSTR_LABEL(instr);
4899 break;
4900 case DIF_OP_BNE:
4901 if (cc_z == 0)
4902 pc = DIF_INSTR_LABEL(instr);
4903 break;
4904 case DIF_OP_BG:
4905 if ((cc_z | (cc_n ^ cc_v)) == 0)
4906 pc = DIF_INSTR_LABEL(instr);
4907 break;
4908 case DIF_OP_BGU:
4909 if ((cc_c | cc_z) == 0)
4910 pc = DIF_INSTR_LABEL(instr);
4911 break;
4912 case DIF_OP_BGE:
4913 if ((cc_n ^ cc_v) == 0)
4914 pc = DIF_INSTR_LABEL(instr);
4915 break;
4916 case DIF_OP_BGEU:
4917 if (cc_c == 0)
4918 pc = DIF_INSTR_LABEL(instr);
4919 break;
4920 case DIF_OP_BL:
4921 if (cc_n ^ cc_v)
4922 pc = DIF_INSTR_LABEL(instr);
4923 break;
4924 case DIF_OP_BLU:
4925 if (cc_c)
4926 pc = DIF_INSTR_LABEL(instr);
4927 break;
4928 case DIF_OP_BLE:
4929 if (cc_z | (cc_n ^ cc_v))
4930 pc = DIF_INSTR_LABEL(instr);
4931 break;
4932 case DIF_OP_BLEU:
4933 if (cc_c | cc_z)
4934 pc = DIF_INSTR_LABEL(instr);
4935 break;
4936 case DIF_OP_RLDSB:
4937 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) {
4938 *flags |= CPU_DTRACE_KPRIV;
4939 *illval = regs[r1];
4940 break;
4941 }
4942 RT_FALL_THRU();
4943 case DIF_OP_LDSB:
4944 regs[rd] = (int8_t)dtrace_load8(regs[r1]);
4945 break;
4946 case DIF_OP_RLDSH:
4947 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) {
4948 *flags |= CPU_DTRACE_KPRIV;
4949 *illval = regs[r1];
4950 break;
4951 }
4952 RT_FALL_THRU();
4953 case DIF_OP_LDSH:
4954 regs[rd] = (int16_t)dtrace_load16(regs[r1]);
4955 break;
4956 case DIF_OP_RLDSW:
4957 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) {
4958 *flags |= CPU_DTRACE_KPRIV;
4959 *illval = regs[r1];
4960 break;
4961 }
4962 RT_FALL_THRU();
4963 case DIF_OP_LDSW:
4964 regs[rd] = (int32_t)dtrace_load32(regs[r1]);
4965 break;
4966 case DIF_OP_RLDUB:
4967 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) {
4968 *flags |= CPU_DTRACE_KPRIV;
4969 *illval = regs[r1];
4970 break;
4971 }
4972 RT_FALL_THRU();
4973 case DIF_OP_LDUB:
4974 regs[rd] = dtrace_load8(regs[r1]);
4975 break;
4976 case DIF_OP_RLDUH:
4977 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) {
4978 *flags |= CPU_DTRACE_KPRIV;
4979 *illval = regs[r1];
4980 break;
4981 }
4982 RT_FALL_THRU();
4983 case DIF_OP_LDUH:
4984 regs[rd] = dtrace_load16(regs[r1]);
4985 break;
4986 case DIF_OP_RLDUW:
4987 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) {
4988 *flags |= CPU_DTRACE_KPRIV;
4989 *illval = regs[r1];
4990 break;
4991 }
4992 RT_FALL_THRU();
4993 case DIF_OP_LDUW:
4994 regs[rd] = dtrace_load32(regs[r1]);
4995 break;
4996 case DIF_OP_RLDX:
4997 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) {
4998 *flags |= CPU_DTRACE_KPRIV;
4999 *illval = regs[r1];
5000 break;
5001 }
5002 RT_FALL_THRU();
5003 case DIF_OP_LDX:
5004 regs[rd] = dtrace_load64(regs[r1]);
5005 break;
5006 case DIF_OP_ULDSB:
5007 regs[rd] = (int8_t)
5008 dtrace_fuword8((void *)(uintptr_t)regs[r1]);
5009 break;
5010 case DIF_OP_ULDSH:
5011 regs[rd] = (int16_t)
5012 dtrace_fuword16((void *)(uintptr_t)regs[r1]);
5013 break;
5014 case DIF_OP_ULDSW:
5015 regs[rd] = (int32_t)
5016 dtrace_fuword32((void *)(uintptr_t)regs[r1]);
5017 break;
5018 case DIF_OP_ULDUB:
5019 regs[rd] =
5020 dtrace_fuword8((void *)(uintptr_t)regs[r1]);
5021 break;
5022 case DIF_OP_ULDUH:
5023 regs[rd] =
5024 dtrace_fuword16((void *)(uintptr_t)regs[r1]);
5025 break;
5026 case DIF_OP_ULDUW:
5027 regs[rd] =
5028 dtrace_fuword32((void *)(uintptr_t)regs[r1]);
5029 break;
5030 case DIF_OP_ULDX:
5031 regs[rd] =
5032 dtrace_fuword64((void *)(uintptr_t)regs[r1]);
5033 break;
5034 case DIF_OP_RET:
5035 rval = regs[rd];
5036 pc = textlen;
5037 break;
5038 case DIF_OP_NOP:
5039 break;
5040 case DIF_OP_SETX:
5041 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)];
5042 break;
5043 case DIF_OP_SETS:
5044 regs[rd] = (uint64_t)(uintptr_t)
5045 (strtab + DIF_INSTR_STRING(instr));
5046 break;
5047 case DIF_OP_SCMP: {
5048 size_t sz = state->dts_options[DTRACEOPT_STRSIZE];
5049 uintptr_t s1 = regs[r1];
5050 uintptr_t s2 = regs[r2];
5051
5052 if (s1 != NULL &&
5053 !dtrace_strcanload(s1, sz, mstate, vstate))
5054 break;
5055 if (s2 != NULL &&
5056 !dtrace_strcanload(s2, sz, mstate, vstate))
5057 break;
5058
5059 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz);
5060
5061 cc_n = cc_r < 0;
5062 cc_z = cc_r == 0;
5063 cc_v = cc_c = 0;
5064 break;
5065 }
5066 case DIF_OP_LDGA:
5067 regs[rd] = dtrace_dif_variable(mstate, state,
5068 r1, regs[r2]);
5069 break;
5070 case DIF_OP_LDGS:
5071 id = DIF_INSTR_VAR(instr);
5072
5073 if (id >= DIF_VAR_OTHER_UBASE) {
5074 uintptr_t a;
5075
5076 id -= DIF_VAR_OTHER_UBASE;
5077 svar = vstate->dtvs_globals[id];
5078 ASSERT(svar != NULL);
5079 v = &svar->dtsv_var;
5080
5081 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) {
5082 regs[rd] = svar->dtsv_data;
5083 break;
5084 }
5085
5086 a = (uintptr_t)svar->dtsv_data;
5087
5088 if (*(uint8_t *)a == UINT8_MAX) {
5089 /*
5090 * If the 0th byte is set to UINT8_MAX
5091 * then this is to be treated as a
5092 * reference to a NULL variable.
5093 */
5094 regs[rd] = NULL;
5095 } else {
5096 regs[rd] = a + sizeof (uint64_t);
5097 }
5098
5099 break;
5100 }
5101
5102 regs[rd] = dtrace_dif_variable(mstate, state, id, 0);
5103 break;
5104
5105 case DIF_OP_STGS:
5106 id = DIF_INSTR_VAR(instr);
5107
5108 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5109 id -= DIF_VAR_OTHER_UBASE;
5110
5111 svar = vstate->dtvs_globals[id];
5112 ASSERT(svar != NULL);
5113 v = &svar->dtsv_var;
5114
5115 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5116 uintptr_t a = (uintptr_t)svar->dtsv_data;
5117
5118 ASSERT(a != NULL);
5119 ASSERT(svar->dtsv_size != 0);
5120
5121 if (regs[rd] == NULL) {
5122 *(uint8_t *)a = UINT8_MAX;
5123 break;
5124 } else {
5125 *(uint8_t *)a = 0;
5126 a += sizeof (uint64_t);
5127 }
5128 if (!dtrace_vcanload(
5129 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5130 mstate, vstate))
5131 break;
5132
5133 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5134 (void *)a, &v->dtdv_type);
5135 break;
5136 }
5137
5138 svar->dtsv_data = regs[rd];
5139 break;
5140
5141 case DIF_OP_LDTA:
5142 /*
5143 * There are no DTrace built-in thread-local arrays at
5144 * present. This opcode is saved for future work.
5145 */
5146 *flags |= CPU_DTRACE_ILLOP;
5147 regs[rd] = 0;
5148 break;
5149
5150 case DIF_OP_LDLS:
5151 id = DIF_INSTR_VAR(instr);
5152
5153 if (id < DIF_VAR_OTHER_UBASE) {
5154 /*
5155 * For now, this has no meaning.
5156 */
5157 regs[rd] = 0;
5158 break;
5159 }
5160
5161 id -= DIF_VAR_OTHER_UBASE;
5162
5163 ASSERT(VBDTCAST(int64_t)id < vstate->dtvs_nlocals);
5164 ASSERT(vstate->dtvs_locals != NULL);
5165
5166 svar = vstate->dtvs_locals[id];
5167 ASSERT(svar != NULL);
5168 v = &svar->dtsv_var;
5169
5170 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5171 uintptr_t a = (uintptr_t)svar->dtsv_data;
5172 size_t sz = v->dtdv_type.dtdt_size;
5173
5174 sz += sizeof (uint64_t);
5175 ASSERT(svar->dtsv_size == NCPU * sz);
5176 a += VBDT_GET_CPUID() * sz;
5177
5178 if (*(uint8_t *)a == UINT8_MAX) {
5179 /*
5180 * If the 0th byte is set to UINT8_MAX
5181 * then this is to be treated as a
5182 * reference to a NULL variable.
5183 */
5184 regs[rd] = NULL;
5185 } else {
5186 regs[rd] = a + sizeof (uint64_t);
5187 }
5188
5189 break;
5190 }
5191
5192 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
5193 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
5194 regs[rd] = tmp[VBDT_GET_CPUID()];
5195 break;
5196
5197 case DIF_OP_STLS:
5198 id = DIF_INSTR_VAR(instr);
5199
5200 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5201 id -= DIF_VAR_OTHER_UBASE;
5202 ASSERT(VBDTCAST(int64_t)id < vstate->dtvs_nlocals);
5203
5204 ASSERT(vstate->dtvs_locals != NULL);
5205 svar = vstate->dtvs_locals[id];
5206 ASSERT(svar != NULL);
5207 v = &svar->dtsv_var;
5208
5209 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5210 uintptr_t a = (uintptr_t)svar->dtsv_data;
5211 size_t sz = v->dtdv_type.dtdt_size;
5212
5213 sz += sizeof (uint64_t);
5214 ASSERT(svar->dtsv_size == NCPU * sz);
5215 a += VBDT_GET_CPUID() * sz;
5216
5217 if (regs[rd] == NULL) {
5218 *(uint8_t *)a = UINT8_MAX;
5219 break;
5220 } else {
5221 *(uint8_t *)a = 0;
5222 a += sizeof (uint64_t);
5223 }
5224
5225 if (!dtrace_vcanload(
5226 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5227 mstate, vstate))
5228 break;
5229
5230 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5231 (void *)a, &v->dtdv_type);
5232 break;
5233 }
5234
5235 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
5236 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
5237 tmp[VBDT_GET_CPUID()] = regs[rd];
5238 break;
5239
5240 case DIF_OP_LDTS: {
5241 dtrace_dynvar_t *dvar;
5242 dtrace_key_t *key;
5243
5244 id = DIF_INSTR_VAR(instr);
5245 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5246 id -= DIF_VAR_OTHER_UBASE;
5247 v = &vstate->dtvs_tlocals[id];
5248
5249 key = &tupregs[DIF_DTR_NREGS];
5250 key[0].dttk_value = (uint64_t)id;
5251 key[0].dttk_size = 0;
5252 DTRACE_TLS_THRKEY(key[1].dttk_value);
5253 key[1].dttk_size = 0;
5254
5255 dvar = dtrace_dynvar(dstate, 2, key,
5256 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC,
5257 mstate, vstate);
5258
5259 if (dvar == NULL) {
5260 regs[rd] = 0;
5261 break;
5262 }
5263
5264 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5265 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
5266 } else {
5267 regs[rd] = *((uint64_t *)dvar->dtdv_data);
5268 }
5269
5270 break;
5271 }
5272
5273 case DIF_OP_STTS: {
5274 dtrace_dynvar_t *dvar;
5275 dtrace_key_t *key;
5276
5277 id = DIF_INSTR_VAR(instr);
5278 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5279 id -= DIF_VAR_OTHER_UBASE;
5280
5281 key = &tupregs[DIF_DTR_NREGS];
5282 key[0].dttk_value = (uint64_t)id;
5283 key[0].dttk_size = 0;
5284 DTRACE_TLS_THRKEY(key[1].dttk_value);
5285 key[1].dttk_size = 0;
5286 v = &vstate->dtvs_tlocals[id];
5287
5288 dvar = dtrace_dynvar(dstate, 2, key,
5289 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
5290 v->dtdv_type.dtdt_size : sizeof (uint64_t),
5291 regs[rd] ? DTRACE_DYNVAR_ALLOC :
5292 DTRACE_DYNVAR_DEALLOC, mstate, vstate);
5293
5294 /*
5295 * Given that we're storing to thread-local data,
5296 * we need to flush our predicate cache.
5297 */
5298 curthread->t_predcache = NULL;
5299
5300 if (dvar == NULL)
5301 break;
5302
5303 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5304 if (!dtrace_vcanload(
5305 (void *)(uintptr_t)regs[rd],
5306 &v->dtdv_type, mstate, vstate))
5307 break;
5308
5309 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5310 dvar->dtdv_data, &v->dtdv_type);
5311 } else {
5312 *((uint64_t *)dvar->dtdv_data) = regs[rd];
5313 }
5314
5315 break;
5316 }
5317
5318 case DIF_OP_SRA:
5319 regs[rd] = (int64_t)regs[r1] >> regs[r2];
5320 break;
5321
5322 case DIF_OP_CALL:
5323 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd,
5324 regs, tupregs, ttop, mstate, state);
5325 break;
5326
5327 case DIF_OP_PUSHTR:
5328 if (ttop == DIF_DTR_NREGS) {
5329 *flags |= CPU_DTRACE_TUPOFLOW;
5330 break;
5331 }
5332
5333 if (r1 == DIF_TYPE_STRING) {
5334 /*
5335 * If this is a string type and the size is 0,
5336 * we'll use the system-wide default string
5337 * size. Note that we are _not_ looking at
5338 * the value of the DTRACEOPT_STRSIZE option;
5339 * had this been set, we would expect to have
5340 * a non-zero size value in the "pushtr".
5341 */
5342 tupregs[ttop].dttk_size =
5343 dtrace_strlen((char *)(uintptr_t)regs[rd],
5344 regs[r2] ? regs[r2] :
5345 dtrace_strsize_default) + 1;
5346 } else {
5347 tupregs[ttop].dttk_size = regs[r2];
5348 }
5349
5350 tupregs[ttop++].dttk_value = regs[rd];
5351 break;
5352
5353 case DIF_OP_PUSHTV:
5354 if (ttop == DIF_DTR_NREGS) {
5355 *flags |= CPU_DTRACE_TUPOFLOW;
5356 break;
5357 }
5358
5359 tupregs[ttop].dttk_value = regs[rd];
5360 tupregs[ttop++].dttk_size = 0;
5361 break;
5362
5363 case DIF_OP_POPTS:
5364 if (ttop != 0)
5365 ttop--;
5366 break;
5367
5368 case DIF_OP_FLUSHTS:
5369 ttop = 0;
5370 break;
5371
5372 case DIF_OP_LDGAA:
5373 case DIF_OP_LDTAA: {
5374 dtrace_dynvar_t *dvar;
5375 dtrace_key_t *key = tupregs;
5376 uint_t nkeys = ttop;
5377
5378 id = DIF_INSTR_VAR(instr);
5379 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5380 id -= DIF_VAR_OTHER_UBASE;
5381
5382 key[nkeys].dttk_value = (uint64_t)id;
5383 key[nkeys++].dttk_size = 0;
5384
5385 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) {
5386 DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
5387 key[nkeys++].dttk_size = 0;
5388 v = &vstate->dtvs_tlocals[id];
5389 } else {
5390 v = &vstate->dtvs_globals[id]->dtsv_var;
5391 }
5392
5393 dvar = dtrace_dynvar(dstate, nkeys, key,
5394 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
5395 v->dtdv_type.dtdt_size : sizeof (uint64_t),
5396 DTRACE_DYNVAR_NOALLOC, mstate, vstate);
5397
5398 if (dvar == NULL) {
5399 regs[rd] = 0;
5400 break;
5401 }
5402
5403 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5404 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
5405 } else {
5406 regs[rd] = *((uint64_t *)dvar->dtdv_data);
5407 }
5408
5409 break;
5410 }
5411
5412 case DIF_OP_STGAA:
5413 case DIF_OP_STTAA: {
5414 dtrace_dynvar_t *dvar;
5415 dtrace_key_t *key = tupregs;
5416 uint_t nkeys = ttop;
5417
5418 id = DIF_INSTR_VAR(instr);
5419 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5420 id -= DIF_VAR_OTHER_UBASE;
5421
5422 key[nkeys].dttk_value = (uint64_t)id;
5423 key[nkeys++].dttk_size = 0;
5424
5425 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) {
5426 DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
5427 key[nkeys++].dttk_size = 0;
5428 v = &vstate->dtvs_tlocals[id];
5429 } else {
5430 v = &vstate->dtvs_globals[id]->dtsv_var;
5431 }
5432
5433 dvar = dtrace_dynvar(dstate, nkeys, key,
5434 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
5435 v->dtdv_type.dtdt_size : sizeof (uint64_t),
5436 regs[rd] ? DTRACE_DYNVAR_ALLOC :
5437 DTRACE_DYNVAR_DEALLOC, mstate, vstate);
5438
5439 if (dvar == NULL)
5440 break;
5441
5442 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5443 if (!dtrace_vcanload(
5444 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5445 mstate, vstate))
5446 break;
5447
5448 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5449 dvar->dtdv_data, &v->dtdv_type);
5450 } else {
5451 *((uint64_t *)dvar->dtdv_data) = regs[rd];
5452 }
5453
5454 break;
5455 }
5456
5457 case DIF_OP_ALLOCS: {
5458 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
5459 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1];
5460
5461 /*
5462 * Rounding up the user allocation size could have
5463 * overflowed large, bogus allocations (like -1ULL) to
5464 * 0.
5465 */
5466 if (size < regs[r1] ||
5467 !DTRACE_INSCRATCH(mstate, size)) {
5468 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5469 regs[rd] = NULL;
5470 break;
5471 }
5472
5473 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size);
5474 mstate->dtms_scratch_ptr += size;
5475 regs[rd] = ptr;
5476 break;
5477 }
5478
5479 case DIF_OP_COPYS:
5480 if (!dtrace_canstore(regs[rd], regs[r2],
5481 mstate, vstate)) {
5482 *flags |= CPU_DTRACE_BADADDR;
5483 *illval = regs[rd];
5484 break;
5485 }
5486
5487 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate))
5488 break;
5489
5490 dtrace_bcopy((void *)(uintptr_t)regs[r1],
5491 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]);
5492 break;
5493
5494 case DIF_OP_STB:
5495 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) {
5496 *flags |= CPU_DTRACE_BADADDR;
5497 *illval = regs[rd];
5498 break;
5499 }
5500 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1];
5501 break;
5502
5503 case DIF_OP_STH:
5504 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) {
5505 *flags |= CPU_DTRACE_BADADDR;
5506 *illval = regs[rd];
5507 break;
5508 }
5509 if (regs[rd] & 1) {
5510 *flags |= CPU_DTRACE_BADALIGN;
5511 *illval = regs[rd];
5512 break;
5513 }
5514 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1];
5515 break;
5516
5517 case DIF_OP_STW:
5518 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) {
5519 *flags |= CPU_DTRACE_BADADDR;
5520 *illval = regs[rd];
5521 break;
5522 }
5523 if (regs[rd] & 3) {
5524 *flags |= CPU_DTRACE_BADALIGN;
5525 *illval = regs[rd];
5526 break;
5527 }
5528 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1];
5529 break;
5530
5531 case DIF_OP_STX:
5532 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) {
5533 *flags |= CPU_DTRACE_BADADDR;
5534 *illval = regs[rd];
5535 break;
5536 }
5537 if (regs[rd] & 7) {
5538 *flags |= CPU_DTRACE_BADALIGN;
5539 *illval = regs[rd];
5540 break;
5541 }
5542 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1];
5543 break;
5544 }
5545 }
5546
5547 if (!(*flags & CPU_DTRACE_FAULT))
5548 return (rval);
5549
5550 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t);
5551 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS;
5552
5553 return (0);
5554}
5555
5556#ifndef VBOX /* no destructive stuff */
5557
5558static void
5559dtrace_action_breakpoint(dtrace_ecb_t *ecb)
5560{
5561 dtrace_probe_t *probe = ecb->dte_probe;
5562 dtrace_provider_t *prov = probe->dtpr_provider;
5563 char c[DTRACE_FULLNAMELEN + 80], *str;
5564 char *msg = "dtrace: breakpoint action at probe ";
5565 char *ecbmsg = " (ecb ";
5566 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4));
5567 uintptr_t val = (uintptr_t)ecb;
5568 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0;
5569
5570 if (dtrace_destructive_disallow)
5571 return;
5572
5573 /*
5574 * It's impossible to be taking action on the NULL probe.
5575 */
5576 ASSERT(probe != NULL);
5577
5578 /*
5579 * This is a poor man's (destitute man's?) sprintf(): we want to
5580 * print the provider name, module name, function name and name of
5581 * the probe, along with the hex address of the ECB with the breakpoint
5582 * action -- all of which we must place in the character buffer by
5583 * hand.
5584 */
5585 while (*msg != '\0')
5586 c[i++] = *msg++;
5587
5588 for (str = prov->dtpv_name; *str != '\0'; str++)
5589 c[i++] = *str;
5590 c[i++] = ':';
5591
5592 for (str = probe->dtpr_mod; *str != '\0'; str++)
5593 c[i++] = *str;
5594 c[i++] = ':';
5595
5596 for (str = probe->dtpr_func; *str != '\0'; str++)
5597 c[i++] = *str;
5598 c[i++] = ':';
5599
5600 for (str = probe->dtpr_name; *str != '\0'; str++)
5601 c[i++] = *str;
5602
5603 while (*ecbmsg != '\0')
5604 c[i++] = *ecbmsg++;
5605
5606 while (shift >= 0) {
5607 mask = (uintptr_t)0xf << shift;
5608
5609 if (val >= ((uintptr_t)1 << shift))
5610 c[i++] = "0123456789abcdef"[(val & mask) >> shift];
5611 shift -= 4;
5612 }
5613
5614 c[i++] = ')';
5615 c[i] = '\0';
5616
5617 debug_enter(c);
5618}
5619
5620static void
5621dtrace_action_panic(dtrace_ecb_t *ecb)
5622{
5623 dtrace_probe_t *probe = ecb->dte_probe;
5624
5625 /*
5626 * It's impossible to be taking action on the NULL probe.
5627 */
5628 ASSERT(probe != NULL);
5629
5630 if (dtrace_destructive_disallow)
5631 return;
5632
5633 if (dtrace_panicked != NULL)
5634 return;
5635
5636 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL)
5637 return;
5638
5639 /*
5640 * We won the right to panic. (We want to be sure that only one
5641 * thread calls panic() from dtrace_probe(), and that panic() is
5642 * called exactly once.)
5643 */
5644 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
5645 probe->dtpr_provider->dtpv_name, probe->dtpr_mod,
5646 probe->dtpr_func, probe->dtpr_name, (void *)ecb);
5647}
5648
5649static void
5650dtrace_action_raise(uint64_t sig)
5651{
5652 if (dtrace_destructive_disallow)
5653 return;
5654
5655 if (sig >= NSIG) {
5656 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5657 return;
5658 }
5659
5660 /*
5661 * raise() has a queue depth of 1 -- we ignore all subsequent
5662 * invocations of the raise() action.
5663 */
5664 if (curthread->t_dtrace_sig == 0)
5665 curthread->t_dtrace_sig = (uint8_t)sig;
5666
5667 curthread->t_sig_check = 1;
5668 aston(curthread);
5669}
5670
5671static void
5672dtrace_action_stop(void)
5673{
5674 if (dtrace_destructive_disallow)
5675 return;
5676
5677 if (!curthread->t_dtrace_stop) {
5678 curthread->t_dtrace_stop = 1;
5679 curthread->t_sig_check = 1;
5680 aston(curthread);
5681 }
5682}
5683
5684static void
5685dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val)
5686{
5687 hrtime_t now;
5688 volatile uint16_t *flags;
5689 cpu_t *cpu = CPU;
5690
5691 if (dtrace_destructive_disallow)
5692 return;
5693
5694 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags;
5695
5696 now = dtrace_gethrtime();
5697
5698 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) {
5699 /*
5700 * We need to advance the mark to the current time.
5701 */
5702 cpu->cpu_dtrace_chillmark = now;
5703 cpu->cpu_dtrace_chilled = 0;
5704 }
5705
5706 /*
5707 * Now check to see if the requested chill time would take us over
5708 * the maximum amount of time allowed in the chill interval. (Or
5709 * worse, if the calculation itself induces overflow.)
5710 */
5711 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max ||
5712 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) {
5713 *flags |= CPU_DTRACE_ILLOP;
5714 return;
5715 }
5716
5717 while (dtrace_gethrtime() - now < val)
5718 continue;
5719
5720 /*
5721 * Normally, we assure that the value of the variable "timestamp" does
5722 * not change within an ECB. The presence of chill() represents an
5723 * exception to this rule, however.
5724 */
5725 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP;
5726 cpu->cpu_dtrace_chilled += val;
5727}
5728
5729#endif /* !VBOX */
5730
5731#ifndef VBOX
5732static void
5733dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state,
5734 uint64_t *buf, uint64_t arg)
5735{
5736 int nframes = DTRACE_USTACK_NFRAMES(arg);
5737 int strsize = DTRACE_USTACK_STRSIZE(arg);
5738 uint64_t *pcs = &buf[1], *fps;
5739 char *str = (char *)&pcs[nframes];
5740 int size, offs = 0, i, j;
5741 uintptr_t old = mstate->dtms_scratch_ptr, saved;
5742#ifndef VBOX
5743 uint16_t *flags = &cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags;
5744#else
5745 uint16_t volatile *flags = &cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags;
5746#endif
5747 char *sym;
5748
5749 /*
5750 * Should be taking a faster path if string space has not been
5751 * allocated.
5752 */
5753 ASSERT(strsize != 0);
5754
5755 /*
5756 * We will first allocate some temporary space for the frame pointers.
5757 */
5758 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
5759 size = (uintptr_t)fps - mstate->dtms_scratch_ptr +
5760 (nframes * sizeof (uint64_t));
5761
5762 if (!DTRACE_INSCRATCH(mstate, VBDTCAST(unsigned)size)) {
5763 /*
5764 * Not enough room for our frame pointers -- need to indicate
5765 * that we ran out of scratch space.
5766 */
5767 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5768 return;
5769 }
5770
5771 mstate->dtms_scratch_ptr += size;
5772 saved = mstate->dtms_scratch_ptr;
5773
5774 /*
5775 * Now get a stack with both program counters and frame pointers.
5776 */
5777 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5778 dtrace_getufpstack(buf, fps, nframes + 1);
5779 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5780
5781 /*
5782 * If that faulted, we're cooked.
5783 */
5784 if (*flags & CPU_DTRACE_FAULT)
5785 goto out;
5786
5787 /*
5788 * Now we want to walk up the stack, calling the USTACK helper. For
5789 * each iteration, we restore the scratch pointer.
5790 */
5791 for (i = 0; i < nframes; i++) {
5792 mstate->dtms_scratch_ptr = saved;
5793
5794 if (offs >= strsize)
5795 break;
5796
5797#ifndef VBOX
5798 sym = (char *)(uintptr_t)dtrace_helper(
5799 DTRACE_HELPER_ACTION_USTACK,
5800 mstate, state, pcs[i], fps[i]);
5801#else
5802 sym = NULL;
5803#endif
5804
5805 /*
5806 * If we faulted while running the helper, we're going to
5807 * clear the fault and null out the corresponding string.
5808 */
5809 if (*flags & CPU_DTRACE_FAULT) {
5810 *flags &= ~CPU_DTRACE_FAULT;
5811 str[offs++] = '\0';
5812 continue;
5813 }
5814
5815 if (sym == NULL) {
5816 str[offs++] = '\0';
5817 continue;
5818 }
5819
5820 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5821
5822 /*
5823 * Now copy in the string that the helper returned to us.
5824 */
5825 for (j = 0; offs + j < strsize; j++) {
5826 if ((str[offs + j] = sym[j]) == '\0')
5827 break;
5828 }
5829
5830 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5831
5832 offs += j + 1;
5833 }
5834
5835 if (offs >= strsize) {
5836 /*
5837 * If we didn't have room for all of the strings, we don't
5838 * abort processing -- this needn't be a fatal error -- but we
5839 * still want to increment a counter (dts_stkstroverflows) to
5840 * allow this condition to be warned about. (If this is from
5841 * a jstack() action, it is easily tuned via jstackstrsize.)
5842 */
5843 dtrace_error(&state->dts_stkstroverflows);
5844 }
5845
5846 while (offs < strsize)
5847 str[offs++] = '\0';
5848
5849out:
5850 mstate->dtms_scratch_ptr = old;
5851}
5852#endif /* !VBOX */
5853
5854#ifdef VBOX
5855extern void dtrace_probe6(dtrace_id_t, uintptr_t arg0, uintptr_t arg1,
5856 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4, uintptr_t arg5);
5857# define dtrace_probe_error(a1, a2, a3, a4, a5, a6) \
5858 dtrace_probe6(dtrace_probeid_error, (uintptr_t)a1, a2, a3, a4, a5, a6)
5859#endif
5860
5861/*
5862 * If you're looking for the epicenter of DTrace, you just found it. This
5863 * is the function called by the provider to fire a probe -- from which all
5864 * subsequent probe-context DTrace activity emanates.
5865 */
5866void
5867dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
5868 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
5869{
5870 processorid_t cpuid;
5871 dtrace_icookie_t cookie;
5872 dtrace_probe_t *probe;
5873 dtrace_mstate_t mstate;
5874 dtrace_ecb_t *ecb;
5875 dtrace_action_t *act;
5876 intptr_t offs;
5877 size_t size;
5878 int vtime, onintr;
5879 volatile uint16_t *flags;
5880 hrtime_t now;
5881
5882#ifndef VBOX
5883 /*
5884 * Kick out immediately if this CPU is still being born (in which case
5885 * curthread will be set to -1) or the current thread can't allow
5886 * probes in its current context.
5887 */
5888 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE))
5889 return;
5890#endif
5891
5892 cookie = dtrace_interrupt_disable();
5893 probe = dtrace_probes[id - 1];
5894 cpuid = VBDT_GET_CPUID();
5895 onintr = CPU_ON_INTR(CPU);
5896
5897 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE &&
5898 probe->dtpr_predcache == curthread->t_predcache) {
5899 /*
5900 * We have hit in the predicate cache; we know that
5901 * this predicate would evaluate to be false.
5902 */
5903 dtrace_interrupt_enable(cookie);
5904 return;
5905 }
5906
5907#ifndef VBOX
5908 if (panic_quiesce) {
5909 /*
5910 * We don't trace anything if we're panicking.
5911 */
5912 dtrace_interrupt_enable(cookie);
5913 return;
5914 }
5915#endif
5916
5917 now = dtrace_gethrtime();
5918 vtime = dtrace_vtime_references != 0;
5919
5920 if (vtime && curthread->t_dtrace_start)
5921 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start;
5922
5923 mstate.dtms_difo = NULL;
5924 mstate.dtms_probe = probe;
5925 mstate.dtms_strtok = NULL;
5926 mstate.dtms_arg[0] = arg0;
5927 mstate.dtms_arg[1] = arg1;
5928 mstate.dtms_arg[2] = arg2;
5929 mstate.dtms_arg[3] = arg3;
5930 mstate.dtms_arg[4] = arg4;
5931
5932 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags;
5933
5934 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
5935 dtrace_predicate_t *pred = ecb->dte_predicate;
5936 dtrace_state_t *state = ecb->dte_state;
5937 dtrace_buffer_t *buf = &state->dts_buffer[cpuid];
5938 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid];
5939 dtrace_vstate_t *vstate = &state->dts_vstate;
5940 dtrace_provider_t *prov = probe->dtpr_provider;
5941 int committed = 0;
5942 caddr_t tomax;
5943
5944 /*
5945 * A little subtlety with the following (seemingly innocuous)
5946 * declaration of the automatic 'val': by looking at the
5947 * code, you might think that it could be declared in the
5948 * action processing loop, below. (That is, it's only used in
5949 * the action processing loop.) However, it must be declared
5950 * out of that scope because in the case of DIF expression
5951 * arguments to aggregating actions, one iteration of the
5952 * action loop will use the last iteration's value.
5953 */
5954#ifdef lint
5955 uint64_t val = 0;
5956#else
5957 uint64_t val VBDTUNASS(0);
5958#endif
5959
5960 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE;
5961 *flags &= ~CPU_DTRACE_ERROR;
5962
5963 if (prov == dtrace_provider) {
5964 /*
5965 * If dtrace itself is the provider of this probe,
5966 * we're only going to continue processing the ECB if
5967 * arg0 (the dtrace_state_t) is equal to the ECB's
5968 * creating state. (This prevents disjoint consumers
5969 * from seeing one another's metaprobes.)
5970 */
5971 if (arg0 != (uint64_t)(uintptr_t)state)
5972 continue;
5973 }
5974
5975 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) {
5976 /*
5977 * We're not currently active. If our provider isn't
5978 * the dtrace pseudo provider, we're not interested.
5979 */
5980 if (prov != dtrace_provider)
5981 continue;
5982
5983 /*
5984 * Now we must further check if we are in the BEGIN
5985 * probe. If we are, we will only continue processing
5986 * if we're still in WARMUP -- if one BEGIN enabling
5987 * has invoked the exit() action, we don't want to
5988 * evaluate subsequent BEGIN enablings.
5989 */
5990 if (probe->dtpr_id == dtrace_probeid_begin &&
5991 state->dts_activity != DTRACE_ACTIVITY_WARMUP) {
5992 ASSERT(state->dts_activity ==
5993 DTRACE_ACTIVITY_DRAINING);
5994 continue;
5995 }
5996 }
5997
5998 if (ecb->dte_cond) {
5999 /*
6000 * If the dte_cond bits indicate that this
6001 * consumer is only allowed to see user-mode firings
6002 * of this probe, call the provider's dtps_usermode()
6003 * entry point to check that the probe was fired
6004 * while in a user context. Skip this ECB if that's
6005 * not the case.
6006 */
6007 if ((ecb->dte_cond & DTRACE_COND_USERMODE) &&
6008 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg,
6009 probe->dtpr_id, probe->dtpr_arg) == 0)
6010 continue;
6011
6012 /*
6013 * This is more subtle than it looks. We have to be
6014 * absolutely certain that CRED() isn't going to
6015 * change out from under us so it's only legit to
6016 * examine that structure if we're in constrained
6017 * situations. Currently, the only times we'll this
6018 * check is if a non-super-user has enabled the
6019 * profile or syscall providers -- providers that
6020 * allow visibility of all processes. For the
6021 * profile case, the check above will ensure that
6022 * we're examining a user context.
6023 */
6024 if (ecb->dte_cond & DTRACE_COND_OWNER) {
6025 cred_t *cr;
6026 cred_t *s_cr =
6027 ecb->dte_state->dts_cred.dcr_cred;
6028#ifndef VBOX
6029 proc_t *proc;
6030#endif
6031
6032 ASSERT(s_cr != NULL);
6033
6034 if ((cr = CRED()) == NULL ||
6035 s_cr->cr_uid != cr->cr_uid ||
6036 s_cr->cr_uid != cr->cr_ruid ||
6037 s_cr->cr_uid != cr->cr_suid ||
6038 s_cr->cr_gid != cr->cr_gid ||
6039 s_cr->cr_gid != cr->cr_rgid ||
6040 s_cr->cr_gid != cr->cr_sgid ||
6041#ifndef VBOX
6042 (proc = VBDT_GET_PROC()) == NULL ||
6043 (proc->p_flag & SNOCD))
6044#else
6045 0)
6046
6047#endif
6048 continue;
6049 }
6050
6051#ifndef VBOX
6052 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
6053 cred_t *cr;
6054 cred_t *s_cr =
6055 ecb->dte_state->dts_cred.dcr_cred;
6056
6057 ASSERT(s_cr != NULL);
6058
6059 if ((cr = CRED()) == NULL ||
6060 s_cr->cr_zone->zone_id !=
6061 cr->cr_zone->zone_id)
6062 continue;
6063 }
6064#endif
6065 }
6066
6067 if (now - state->dts_alive > dtrace_deadman_timeout) {
6068 /*
6069 * We seem to be dead. Unless we (a) have kernel
6070 * destructive permissions (b) have expicitly enabled
6071 * destructive actions and (c) destructive actions have
6072 * not been disabled, we're going to transition into
6073 * the KILLED state, from which no further processing
6074 * on this state will be performed.
6075 */
6076 if (!dtrace_priv_kernel_destructive(state) ||
6077 !state->dts_cred.dcr_destructive ||
6078 dtrace_destructive_disallow) {
6079 void *activity = &state->dts_activity;
6080 dtrace_activity_t current;
6081
6082 do {
6083 current = state->dts_activity;
6084 } while ( (dtrace_activity_t)dtrace_cas32(activity, current, DTRACE_ACTIVITY_KILLED)
6085 != current);
6086
6087 continue;
6088 }
6089 }
6090
6091 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed,
6092 ecb->dte_alignment, state, &mstate)) < 0)
6093 continue;
6094
6095 tomax = buf->dtb_tomax;
6096 ASSERT(tomax != NULL);
6097
6098 if (ecb->dte_size != 0)
6099 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid);
6100
6101 mstate.dtms_epid = ecb->dte_epid;
6102 mstate.dtms_present |= DTRACE_MSTATE_EPID;
6103
6104 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)
6105 mstate.dtms_access = DTRACE_ACCESS_KERNEL;
6106 else
6107 mstate.dtms_access = 0;
6108
6109 if (pred != NULL) {
6110 dtrace_difo_t *dp = pred->dtp_difo;
6111 int rval;
6112
6113 rval = dtrace_dif_emulate(dp, &mstate, vstate, state);
6114
6115 if (!(*flags & CPU_DTRACE_ERROR) && !rval) {
6116 dtrace_cacheid_t cid = probe->dtpr_predcache;
6117
6118 if (cid != DTRACE_CACHEIDNONE && !onintr) {
6119 /*
6120 * Update the predicate cache...
6121 */
6122 ASSERT(cid == pred->dtp_cacheid);
6123 curthread->t_predcache = cid;
6124 }
6125
6126 continue;
6127 }
6128 }
6129
6130 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) &&
6131 act != NULL; act = act->dta_next) {
6132 size_t valoffs;
6133 dtrace_difo_t *dp;
6134 dtrace_recdesc_t *rec = &act->dta_rec;
6135
6136 size = rec->dtrd_size;
6137 valoffs = offs + rec->dtrd_offset;
6138
6139 if (DTRACEACT_ISAGG(act->dta_kind)) {
6140 uint64_t v = 0xbad;
6141 dtrace_aggregation_t *agg;
6142
6143 agg = (dtrace_aggregation_t *)act;
6144
6145 if ((dp = act->dta_difo) != NULL)
6146 v = dtrace_dif_emulate(dp,
6147 &mstate, vstate, state);
6148
6149 if (*flags & CPU_DTRACE_ERROR)
6150 continue;
6151
6152 /*
6153 * Note that we always pass the expression
6154 * value from the previous iteration of the
6155 * action loop. This value will only be used
6156 * if there is an expression argument to the
6157 * aggregating action, denoted by the
6158 * dtag_hasarg field.
6159 */
6160 dtrace_aggregate(agg, buf,
6161 offs, aggbuf, v, val);
6162 continue;
6163 }
6164
6165 switch (act->dta_kind) {
6166 case DTRACEACT_STOP:
6167#ifndef VBOX
6168 if (dtrace_priv_proc_destructive(state))
6169 dtrace_action_stop();
6170#else
6171 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6172#endif
6173 continue;
6174
6175 case DTRACEACT_BREAKPOINT:
6176#ifndef VBOX
6177 if (dtrace_priv_kernel_destructive(state))
6178 dtrace_action_breakpoint(ecb);
6179#else
6180 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6181#endif
6182 continue;
6183
6184 case DTRACEACT_PANIC:
6185#ifndef VBOX
6186 if (dtrace_priv_kernel_destructive(state))
6187 dtrace_action_panic(ecb);
6188#endif
6189 continue;
6190
6191 case DTRACEACT_STACK:
6192 if (!dtrace_priv_kernel(state))
6193 continue;
6194
6195 dtrace_getpcstack((pc_t *)(tomax + valoffs),
6196 VBDTCAST(int)(size / sizeof (pc_t)), probe->dtpr_aframes,
6197 DTRACE_ANCHORED(probe) ? NULL :
6198 (uint32_t *)arg0);
6199
6200 continue;
6201
6202 case DTRACEACT_JSTACK:
6203 case DTRACEACT_USTACK:
6204 if (!dtrace_priv_proc(state))
6205 continue;
6206
6207 /*
6208 * See comment in DIF_VAR_PID.
6209 */
6210 if (DTRACE_ANCHORED(mstate.dtms_probe) &&
6211 CPU_ON_INTR(CPU)) {
6212 int depth = DTRACE_USTACK_NFRAMES(
6213 rec->dtrd_arg) + 1;
6214
6215 dtrace_bzero((void *)(tomax + valoffs),
6216 DTRACE_USTACK_STRSIZE(rec->dtrd_arg)
6217 + depth * sizeof (uint64_t));
6218
6219 continue;
6220 }
6221
6222#ifndef VBOX /* no helpers */
6223 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 &&
6224 curproc->p_dtrace_helpers != NULL) {
6225 /*
6226 * This is the slow path -- we have
6227 * allocated string space, and we're
6228 * getting the stack of a process that
6229 * has helpers. Call into a separate
6230 * routine to perform this processing.
6231 */
6232 dtrace_action_ustack(&mstate, state,
6233 (uint64_t *)(tomax + valoffs),
6234 rec->dtrd_arg);
6235 continue;
6236 }
6237#endif
6238
6239 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6240 dtrace_getupcstack((uint64_t *)
6241 (tomax + valoffs),
6242 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1);
6243 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6244 continue;
6245
6246 default:
6247 break;
6248 }
6249
6250 dp = act->dta_difo;
6251 ASSERT(dp != NULL);
6252
6253 val = dtrace_dif_emulate(dp, &mstate, vstate, state);
6254
6255 if (*flags & CPU_DTRACE_ERROR)
6256 continue;
6257
6258 switch (act->dta_kind) {
6259 case DTRACEACT_SPECULATE:
6260 ASSERT(buf == &state->dts_buffer[cpuid]);
6261 buf = dtrace_speculation_buffer(state,
6262 cpuid, val);
6263
6264 if (buf == NULL) {
6265 *flags |= CPU_DTRACE_DROP;
6266 continue;
6267 }
6268
6269 offs = dtrace_buffer_reserve(buf,
6270 ecb->dte_needed, ecb->dte_alignment,
6271 state, NULL);
6272
6273 if (offs < 0) {
6274 *flags |= CPU_DTRACE_DROP;
6275 continue;
6276 }
6277
6278 tomax = buf->dtb_tomax;
6279 ASSERT(tomax != NULL);
6280
6281 if (ecb->dte_size != 0)
6282 DTRACE_STORE(uint32_t, tomax, offs,
6283 ecb->dte_epid);
6284 continue;
6285
6286 case DTRACEACT_CHILL:
6287#ifndef VBOX
6288 if (dtrace_priv_kernel_destructive(state))
6289 dtrace_action_chill(&mstate, val);
6290#else
6291 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6292#endif
6293 continue;
6294
6295 case DTRACEACT_RAISE:
6296#ifndef VBOX
6297 if (dtrace_priv_proc_destructive(state))
6298 dtrace_action_raise(val);
6299#else
6300 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6301#endif
6302 continue;
6303
6304 case DTRACEACT_COMMIT:
6305 ASSERT(!committed);
6306
6307 /*
6308 * We need to commit our buffer state.
6309 */
6310 if (ecb->dte_size)
6311 buf->dtb_offset = offs + ecb->dte_size;
6312 buf = &state->dts_buffer[cpuid];
6313 dtrace_speculation_commit(state, cpuid, val);
6314 committed = 1;
6315 continue;
6316
6317 case DTRACEACT_DISCARD:
6318 dtrace_speculation_discard(state, cpuid, val);
6319 continue;
6320
6321 case DTRACEACT_DIFEXPR:
6322 case DTRACEACT_LIBACT:
6323 case DTRACEACT_PRINTF:
6324 case DTRACEACT_PRINTA:
6325 case DTRACEACT_SYSTEM:
6326 case DTRACEACT_FREOPEN:
6327 break;
6328
6329 case DTRACEACT_SYM:
6330 case DTRACEACT_MOD:
6331 if (!dtrace_priv_kernel(state))
6332 continue;
6333 break;
6334
6335 case DTRACEACT_USYM:
6336 case DTRACEACT_UMOD:
6337 case DTRACEACT_UADDR: {
6338#ifndef VBOX
6339 struct pid *pid = curthread->t_procp->p_pidp;
6340
6341 if (!dtrace_priv_proc(state))
6342 continue;
6343
6344 DTRACE_STORE(uint64_t, tomax,
6345 valoffs, (uint64_t)pid->pid_id);
6346 DTRACE_STORE(uint64_t, tomax,
6347 valoffs + sizeof (uint64_t), val);
6348#else
6349 DTRACE_CPUFLAG_SET(CPU_DTRACE_UPRIV);
6350#endif
6351 continue;
6352 }
6353
6354 case DTRACEACT_EXIT: {
6355 /*
6356 * For the exit action, we are going to attempt
6357 * to atomically set our activity to be
6358 * draining. If this fails (either because
6359 * another CPU has beat us to the exit action,
6360 * or because our current activity is something
6361 * other than ACTIVE or WARMUP), we will
6362 * continue. This assures that the exit action
6363 * can be successfully recorded at most once
6364 * when we're in the ACTIVE state. If we're
6365 * encountering the exit() action while in
6366 * COOLDOWN, however, we want to honor the new
6367 * status code. (We know that we're the only
6368 * thread in COOLDOWN, so there is no race.)
6369 */
6370 void *activity = &state->dts_activity;
6371 dtrace_activity_t current = state->dts_activity;
6372
6373 if (current == DTRACE_ACTIVITY_COOLDOWN)
6374 break;
6375
6376 if (current != DTRACE_ACTIVITY_WARMUP)
6377 current = DTRACE_ACTIVITY_ACTIVE;
6378
6379 if ( (dtrace_activity_t)dtrace_cas32(activity, current, DTRACE_ACTIVITY_DRAINING)
6380 != current) {
6381 *flags |= CPU_DTRACE_DROP;
6382 continue;
6383 }
6384
6385 break;
6386 }
6387
6388 default:
6389#ifndef VBOX
6390 ASSERT(0);
6391#else
6392 AssertFatalMsgFailed(("%d\n", act->dta_kind));
6393#endif
6394 }
6395
6396 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) {
6397 uintptr_t end = valoffs + size;
6398
6399 if (!dtrace_vcanload((void *)(uintptr_t)val,
6400 &dp->dtdo_rtype, &mstate, vstate))
6401 continue;
6402
6403 /*
6404 * If this is a string, we're going to only
6405 * load until we find the zero byte -- after
6406 * which we'll store zero bytes.
6407 */
6408 if (dp->dtdo_rtype.dtdt_kind ==
6409 DIF_TYPE_STRING) {
6410 char c = '\0' + 1;
6411 int intuple = act->dta_intuple;
6412 size_t s;
6413
6414 for (s = 0; s < size; s++) {
6415 if (c != '\0')
6416 c = dtrace_load8(val++);
6417
6418 DTRACE_STORE(uint8_t, tomax,
6419 valoffs++, c);
6420
6421 if (c == '\0' && intuple)
6422 break;
6423 }
6424
6425 continue;
6426 }
6427
6428 while (valoffs < end) {
6429 DTRACE_STORE(uint8_t, tomax, valoffs++,
6430 dtrace_load8(val++));
6431 }
6432
6433 continue;
6434 }
6435
6436 switch (size) {
6437 case 0:
6438 break;
6439
6440 case sizeof (uint8_t):
6441 DTRACE_STORE(uint8_t, tomax, valoffs, val);
6442 break;
6443 case sizeof (uint16_t):
6444 DTRACE_STORE(uint16_t, tomax, valoffs, val);
6445 break;
6446 case sizeof (uint32_t):
6447 DTRACE_STORE(uint32_t, tomax, valoffs, val);
6448 break;
6449 case sizeof (uint64_t):
6450 DTRACE_STORE(uint64_t, tomax, valoffs, val);
6451 break;
6452 default:
6453 /*
6454 * Any other size should have been returned by
6455 * reference, not by value.
6456 */
6457#ifndef VBOX
6458 ASSERT(0);
6459#else
6460 AssertFatalMsgFailed(("%zu\n", size));
6461#endif
6462 break;
6463 }
6464 }
6465
6466 if (*flags & CPU_DTRACE_DROP)
6467 continue;
6468
6469 if (*flags & CPU_DTRACE_FAULT) {
6470 int ndx;
6471 dtrace_action_t *err;
6472
6473 buf->dtb_errors++;
6474
6475 if (probe->dtpr_id == dtrace_probeid_error) {
6476 /*
6477 * There's nothing we can do -- we had an
6478 * error on the error probe. We bump an
6479 * error counter to at least indicate that
6480 * this condition happened.
6481 */
6482 dtrace_error(&state->dts_dblerrors);
6483 continue;
6484 }
6485
6486 if (vtime) {
6487 /*
6488 * Before recursing on dtrace_probe(), we
6489 * need to explicitly clear out our start
6490 * time to prevent it from being accumulated
6491 * into t_dtrace_vtime.
6492 */
6493 curthread->t_dtrace_start = 0;
6494 }
6495
6496 /*
6497 * Iterate over the actions to figure out which action
6498 * we were processing when we experienced the error.
6499 * Note that act points _past_ the faulting action; if
6500 * act is ecb->dte_action, the fault was in the
6501 * predicate, if it's ecb->dte_action->dta_next it's
6502 * in action #1, and so on.
6503 */
6504 for (err = ecb->dte_action, ndx = 0;
6505 err != act; err = err->dta_next, ndx++)
6506 continue;
6507
6508 dtrace_probe_error(state, ecb->dte_epid, ndx,
6509 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ?
6510 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags),
6511 cpu_core[cpuid].cpuc_dtrace_illval);
6512
6513 continue;
6514 }
6515
6516 if (!committed)
6517 buf->dtb_offset = offs + ecb->dte_size;
6518 }
6519
6520 if (vtime)
6521 curthread->t_dtrace_start = dtrace_gethrtime();
6522
6523 dtrace_interrupt_enable(cookie);
6524}
6525
6526/*
6527 * DTrace Probe Hashing Functions
6528 *
6529 * The functions in this section (and indeed, the functions in remaining
6530 * sections) are not _called_ from probe context. (Any exceptions to this are
6531 * marked with a "Note:".) Rather, they are called from elsewhere in the
6532 * DTrace framework to look-up probes in, add probes to and remove probes from
6533 * the DTrace probe hashes. (Each probe is hashed by each element of the
6534 * probe tuple -- allowing for fast lookups, regardless of what was
6535 * specified.)
6536 */
6537static uint_t
6538dtrace_hash_str(char *p)
6539{
6540 unsigned int g;
6541 uint_t hval = 0;
6542
6543 while (*p) {
6544 hval = (hval << 4) + *p++;
6545 if ((g = (hval & 0xf0000000)) != 0)
6546 hval ^= g >> 24;
6547 hval &= ~g;
6548 }
6549 return (hval);
6550}
6551
6552static dtrace_hash_t *
6553dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs)
6554{
6555 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP);
6556
6557 hash->dth_stroffs = stroffs;
6558 hash->dth_nextoffs = nextoffs;
6559 hash->dth_prevoffs = prevoffs;
6560
6561 hash->dth_size = 1;
6562 hash->dth_mask = hash->dth_size - 1;
6563
6564 hash->dth_tab = kmem_zalloc(hash->dth_size *
6565 sizeof (dtrace_hashbucket_t *), KM_SLEEP);
6566
6567 return (hash);
6568}
6569
6570static void
6571dtrace_hash_destroy(dtrace_hash_t *hash)
6572{
6573#ifdef DEBUG
6574 int i;
6575
6576 for (i = 0; i < hash->dth_size; i++)
6577 ASSERT(hash->dth_tab[i] == NULL);
6578#endif
6579
6580 kmem_free(hash->dth_tab,
6581 hash->dth_size * sizeof (dtrace_hashbucket_t *));
6582 kmem_free(hash, sizeof (dtrace_hash_t));
6583}
6584
6585static void
6586dtrace_hash_resize(dtrace_hash_t *hash)
6587{
6588 int size = hash->dth_size, i, ndx;
6589 int new_size = hash->dth_size << 1;
6590 int new_mask = new_size - 1;
6591 dtrace_hashbucket_t **new_tab, *bucket, *next;
6592
6593 ASSERT((new_size & new_mask) == 0);
6594
6595 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP);
6596
6597 for (i = 0; i < size; i++) {
6598 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) {
6599 dtrace_probe_t *probe = bucket->dthb_chain;
6600
6601 ASSERT(probe != NULL);
6602 ndx = DTRACE_HASHSTR(hash, probe) & new_mask;
6603
6604 next = bucket->dthb_next;
6605 bucket->dthb_next = new_tab[ndx];
6606 new_tab[ndx] = bucket;
6607 }
6608 }
6609
6610 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *));
6611 hash->dth_tab = new_tab;
6612 hash->dth_size = new_size;
6613 hash->dth_mask = new_mask;
6614}
6615
6616static void
6617dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new)
6618{
6619 int hashval = DTRACE_HASHSTR(hash, new);
6620 int ndx = hashval & hash->dth_mask;
6621 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
6622 dtrace_probe_t **nextp, **prevp;
6623
6624 for (; bucket != NULL; bucket = bucket->dthb_next) {
6625 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new))
6626 goto add;
6627 }
6628
6629 if ((hash->dth_nbuckets >> 1) > hash->dth_size) {
6630 dtrace_hash_resize(hash);
6631 dtrace_hash_add(hash, new);
6632 return;
6633 }
6634
6635 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP);
6636 bucket->dthb_next = hash->dth_tab[ndx];
6637 hash->dth_tab[ndx] = bucket;
6638 hash->dth_nbuckets++;
6639
6640add:
6641 nextp = DTRACE_HASHNEXT(hash, new);
6642 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL);
6643 *nextp = bucket->dthb_chain;
6644
6645 if (bucket->dthb_chain != NULL) {
6646 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain);
6647 ASSERT(*prevp == NULL);
6648 *prevp = new;
6649 }
6650
6651 bucket->dthb_chain = new;
6652 bucket->dthb_len++;
6653}
6654
6655static dtrace_probe_t *
6656dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template)
6657{
6658 int hashval = DTRACE_HASHSTR(hash, template);
6659 int ndx = hashval & hash->dth_mask;
6660 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
6661
6662 for (; bucket != NULL; bucket = bucket->dthb_next) {
6663 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
6664 return (bucket->dthb_chain);
6665 }
6666
6667 return (NULL);
6668}
6669
6670static int
6671dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template)
6672{
6673 int hashval = DTRACE_HASHSTR(hash, template);
6674 int ndx = hashval & hash->dth_mask;
6675 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
6676
6677 for (; bucket != NULL; bucket = bucket->dthb_next) {
6678 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
6679 return (bucket->dthb_len);
6680 }
6681
6682 return (NULL);
6683}
6684
6685static void
6686dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe)
6687{
6688 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask;
6689 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
6690
6691 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe);
6692 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe);
6693
6694 /*
6695 * Find the bucket that we're removing this probe from.
6696 */
6697 for (; bucket != NULL; bucket = bucket->dthb_next) {
6698 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe))
6699 break;
6700 }
6701
6702 ASSERT(bucket != NULL);
6703
6704 if (*prevp == NULL) {
6705 if (*nextp == NULL) {
6706 /*
6707 * The removed probe was the only probe on this
6708 * bucket; we need to remove the bucket.
6709 */
6710 dtrace_hashbucket_t *b = hash->dth_tab[ndx];
6711
6712 ASSERT(bucket->dthb_chain == probe);
6713 ASSERT(b != NULL);
6714
6715 if (b == bucket) {
6716 hash->dth_tab[ndx] = bucket->dthb_next;
6717 } else {
6718 while (b->dthb_next != bucket)
6719 b = b->dthb_next;
6720 b->dthb_next = bucket->dthb_next;
6721 }
6722
6723 ASSERT(hash->dth_nbuckets > 0);
6724 hash->dth_nbuckets--;
6725 kmem_free(bucket, sizeof (dtrace_hashbucket_t));
6726 return;
6727 }
6728
6729 bucket->dthb_chain = *nextp;
6730 } else {
6731 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp;
6732 }
6733
6734 if (*nextp != NULL)
6735 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp;
6736}
6737
6738/*
6739 * DTrace Utility Functions
6740 *
6741 * These are random utility functions that are _not_ called from probe context.
6742 */
6743static int
6744dtrace_badattr(const dtrace_attribute_t *a)
6745{
6746 return (a->dtat_name > DTRACE_STABILITY_MAX ||
6747 a->dtat_data > DTRACE_STABILITY_MAX ||
6748 a->dtat_class > DTRACE_CLASS_MAX);
6749}
6750
6751/*
6752 * Return a duplicate copy of a string. If the specified string is NULL,
6753 * this function returns a zero-length string.
6754 */
6755static char *
6756dtrace_strdup(const char *str)
6757{
6758 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP);
6759
6760 if (str != NULL)
6761 (void) strcpy(new, str);
6762
6763 return (new);
6764}
6765
6766#define DTRACE_ISALPHA(c) \
6767 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
6768
6769static int
6770dtrace_badname(const char *s)
6771{
6772 char c;
6773
6774 if (s == NULL || (c = *s++) == '\0')
6775 return (0);
6776
6777 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.')
6778 return (1);
6779
6780 while ((c = *s++) != '\0') {
6781 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') &&
6782 c != '-' && c != '_' && c != '.' && c != '`')
6783 return (1);
6784 }
6785
6786 return (0);
6787}
6788
6789static void
6790dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp)
6791{
6792 uint32_t priv;
6793
6794 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
6795 /*
6796 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
6797 */
6798 priv = DTRACE_PRIV_ALL;
6799#ifdef VBOX
6800 *uidp = UINT32_MAX;
6801 *zoneidp = 0;
6802#endif
6803 } else {
6804 *uidp = crgetuid(cr);
6805 *zoneidp = crgetzoneid(cr);
6806
6807 priv = 0;
6808 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE))
6809 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER;
6810 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE))
6811 priv |= DTRACE_PRIV_USER;
6812 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE))
6813 priv |= DTRACE_PRIV_PROC;
6814 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
6815 priv |= DTRACE_PRIV_OWNER;
6816 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
6817 priv |= DTRACE_PRIV_ZONEOWNER;
6818 }
6819
6820 *privp = priv;
6821}
6822
6823#ifdef DTRACE_ERRDEBUG
6824static void
6825dtrace_errdebug(const char *str)
6826{
6827 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ;
6828 int occupied = 0;
6829
6830 mutex_enter(&dtrace_errlock);
6831 dtrace_errlast = str;
6832 dtrace_errthread = curthread;
6833
6834 while (occupied++ < DTRACE_ERRHASHSZ) {
6835 if (dtrace_errhash[hval].dter_msg == str) {
6836 dtrace_errhash[hval].dter_count++;
6837 goto out;
6838 }
6839
6840 if (dtrace_errhash[hval].dter_msg != NULL) {
6841 hval = (hval + 1) % DTRACE_ERRHASHSZ;
6842 continue;
6843 }
6844
6845 dtrace_errhash[hval].dter_msg = str;
6846 dtrace_errhash[hval].dter_count = 1;
6847 goto out;
6848 }
6849
6850 panic("dtrace: undersized error hash");
6851out:
6852 mutex_exit(&dtrace_errlock);
6853}
6854#endif
6855
6856/*
6857 * DTrace Matching Functions
6858 *
6859 * These functions are used to match groups of probes, given some elements of
6860 * a probe tuple, or some globbed expressions for elements of a probe tuple.
6861 */
6862static int
6863dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid,
6864 zoneid_t zoneid)
6865{
6866 if (priv != DTRACE_PRIV_ALL) {
6867 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags;
6868 uint32_t match = priv & ppriv;
6869
6870 /*
6871 * No PRIV_DTRACE_* privileges...
6872 */
6873 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER |
6874 DTRACE_PRIV_KERNEL)) == 0)
6875 return (0);
6876
6877 /*
6878 * No matching bits, but there were bits to match...
6879 */
6880 if (match == 0 && ppriv != 0)
6881 return (0);
6882
6883 /*
6884 * Need to have permissions to the process, but don't...
6885 */
6886 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 &&
6887 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) {
6888 return (0);
6889 }
6890
6891 /*
6892 * Need to be in the same zone unless we possess the
6893 * privilege to examine all zones.
6894 */
6895 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 &&
6896 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) {
6897 return (0);
6898 }
6899 }
6900
6901 return (1);
6902}
6903
6904/*
6905 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
6906 * consists of input pattern strings and an ops-vector to evaluate them.
6907 * This function returns >0 for match, 0 for no match, and <0 for error.
6908 */
6909static int
6910dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp,
6911 uint32_t priv, uid_t uid, zoneid_t zoneid)
6912{
6913 dtrace_provider_t *pvp = prp->dtpr_provider;
6914 int rv;
6915
6916 if (pvp->dtpv_defunct)
6917 return (0);
6918
6919 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0)
6920 return (rv);
6921
6922 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0)
6923 return (rv);
6924
6925 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0)
6926 return (rv);
6927
6928 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0)
6929 return (rv);
6930
6931 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0)
6932 return (0);
6933
6934 return (rv);
6935}
6936
6937/*
6938 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
6939 * interface for matching a glob pattern 'p' to an input string 's'. Unlike
6940 * libc's version, the kernel version only applies to 8-bit ASCII strings.
6941 * In addition, all of the recursion cases except for '*' matching have been
6942 * unwound. For '*', we still implement recursive evaluation, but a depth
6943 * counter is maintained and matching is aborted if we recurse too deep.
6944 * The function returns 0 if no match, >0 if match, and <0 if recursion error.
6945 */
6946static int
6947dtrace_match_glob(const char *s, const char *p, int depth)
6948{
6949 const char *olds;
6950 char s1, c;
6951 int gs;
6952
6953 if (depth > DTRACE_PROBEKEY_MAXDEPTH)
6954 return (-1);
6955
6956 if (s == NULL)
6957 s = ""; /* treat NULL as empty string */
6958
6959top:
6960 olds = s;
6961 s1 = *s++;
6962
6963 if (p == NULL)
6964 return (0);
6965
6966 if ((c = *p++) == '\0')
6967 return (s1 == '\0');
6968
6969 switch (c) {
6970 case '[': {
6971 int ok = 0, notflag = 0;
6972 char lc = '\0';
6973
6974 if (s1 == '\0')
6975 return (0);
6976
6977 if (*p == '!') {
6978 notflag = 1;
6979 p++;
6980 }
6981
6982 if ((c = *p++) == '\0')
6983 return (0);
6984
6985 do {
6986 if (c == '-' && lc != '\0' && *p != ']') {
6987 if ((c = *p++) == '\0')
6988 return (0);
6989 if (c == '\\' && (c = *p++) == '\0')
6990 return (0);
6991
6992 if (notflag) {
6993 if (s1 < lc || s1 > c)
6994 ok++;
6995 else
6996 return (0);
6997 } else if (lc <= s1 && s1 <= c)
6998 ok++;
6999
7000 } else if (c == '\\' && (c = *p++) == '\0')
7001 return (0);
7002
7003 lc = c; /* save left-hand 'c' for next iteration */
7004
7005 if (notflag) {
7006 if (s1 != c)
7007 ok++;
7008 else
7009 return (0);
7010 } else if (s1 == c)
7011 ok++;
7012
7013 if ((c = *p++) == '\0')
7014 return (0);
7015
7016 } while (c != ']');
7017
7018 if (ok)
7019 goto top;
7020
7021 return (0);
7022 }
7023
7024 case '\\':
7025 if ((c = *p++) == '\0')
7026 return (0);
7027 RT_FALL_THRU();
7028
7029 default:
7030 if (c != s1)
7031 return (0);
7032 RT_FALL_THRU();
7033
7034 case '?':
7035 if (s1 != '\0')
7036 goto top;
7037 return (0);
7038
7039 case '*':
7040 while (*p == '*')
7041 p++; /* consecutive *'s are identical to a single one */
7042
7043 if (*p == '\0')
7044 return (1);
7045
7046 for (s = olds; *s != '\0'; s++) {
7047 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0)
7048 return (gs);
7049 }
7050
7051 return (0);
7052 }
7053}
7054
7055/*ARGSUSED*/
7056static int
7057dtrace_match_string(const char *s, const char *p, int depth)
7058{
7059 RT_NOREF_PV(depth);
7060 return (s != NULL && strcmp(s, p) == 0);
7061}
7062
7063/*ARGSUSED*/
7064static int
7065dtrace_match_nul(const char *s, const char *p, int depth)
7066{
7067 RT_NOREF_PV(s); RT_NOREF_PV(p); RT_NOREF_PV(depth);
7068 return (1); /* always match the empty pattern */
7069}
7070
7071/*ARGSUSED*/
7072static int
7073dtrace_match_nonzero(const char *s, const char *p, int depth)
7074{
7075 RT_NOREF_PV(p); RT_NOREF_PV(depth);
7076 return (s != NULL && s[0] != '\0');
7077}
7078
7079static int
7080dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid,
7081 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg)
7082{
7083 dtrace_probe_t template, *probe;
7084 dtrace_hash_t *hash = NULL;
7085 int len, rc, best = INT_MAX, nmatched = 0;
7086 dtrace_id_t i;
7087
7088 ASSERT(MUTEX_HELD(&dtrace_lock));
7089
7090 /*
7091 * If the probe ID is specified in the key, just lookup by ID and
7092 * invoke the match callback once if a matching probe is found.
7093 */
7094 if (pkp->dtpk_id != DTRACE_IDNONE) {
7095 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL &&
7096 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) {
7097 if ((*matched)(probe, arg) == DTRACE_MATCH_FAIL)
7098 return (DTRACE_MATCH_FAIL);
7099 nmatched++;
7100 }
7101 return (nmatched);
7102 }
7103
7104 template.dtpr_mod = (char *)pkp->dtpk_mod;
7105 template.dtpr_func = (char *)pkp->dtpk_func;
7106 template.dtpr_name = (char *)pkp->dtpk_name;
7107
7108 /*
7109 * We want to find the most distinct of the module name, function
7110 * name, and name. So for each one that is not a glob pattern or
7111 * empty string, we perform a lookup in the corresponding hash and
7112 * use the hash table with the fewest collisions to do our search.
7113 */
7114 if (pkp->dtpk_mmatch == &dtrace_match_string &&
7115 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) {
7116 best = len;
7117 hash = dtrace_bymod;
7118 }
7119
7120 if (pkp->dtpk_fmatch == &dtrace_match_string &&
7121 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) {
7122 best = len;
7123 hash = dtrace_byfunc;
7124 }
7125
7126 if (pkp->dtpk_nmatch == &dtrace_match_string &&
7127 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) {
7128 best = len;
7129 hash = dtrace_byname;
7130 }
7131
7132 /*
7133 * If we did not select a hash table, iterate over every probe and
7134 * invoke our callback for each one that matches our input probe key.
7135 */
7136 if (hash == NULL) {
7137 for (i = 0; i < VBDTCAST(dtrace_id_t)dtrace_nprobes; i++) {
7138 if ((probe = dtrace_probes[i]) == NULL ||
7139 dtrace_match_probe(probe, pkp, priv, uid,
7140 zoneid) <= 0)
7141 continue;
7142
7143 nmatched++;
7144
7145 if ((rc = (*matched)(probe, arg)) !=
7146 DTRACE_MATCH_NEXT) {
7147 if (rc == DTRACE_MATCH_FAIL)
7148 return (DTRACE_MATCH_FAIL);
7149 break;
7150 }
7151 }
7152
7153 return (nmatched);
7154 }
7155
7156 /*
7157 * If we selected a hash table, iterate over each probe of the same key
7158 * name and invoke the callback for every probe that matches the other
7159 * attributes of our input probe key.
7160 */
7161 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL;
7162 probe = *(DTRACE_HASHNEXT(hash, probe))) {
7163
7164 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0)
7165 continue;
7166
7167 nmatched++;
7168
7169 if ((rc = (*matched)(probe, arg)) != DTRACE_MATCH_NEXT) {
7170 if (rc == DTRACE_MATCH_FAIL)
7171 return (DTRACE_MATCH_FAIL);
7172 break;
7173 }
7174 }
7175
7176 return (nmatched);
7177}
7178
7179/*
7180 * Return the function pointer dtrace_probecmp() should use to compare the
7181 * specified pattern with a string. For NULL or empty patterns, we select
7182 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob().
7183 * For non-empty non-glob strings, we use dtrace_match_string().
7184 */
7185static dtrace_probekey_f *
7186dtrace_probekey_func(const char *p)
7187{
7188 char c;
7189
7190 if (p == NULL || *p == '\0')
7191 return (&dtrace_match_nul);
7192
7193 while ((c = *p++) != '\0') {
7194 if (c == '[' || c == '?' || c == '*' || c == '\\')
7195 return (&dtrace_match_glob);
7196 }
7197
7198 return (&dtrace_match_string);
7199}
7200
7201/*
7202 * Build a probe comparison key for use with dtrace_match_probe() from the
7203 * given probe description. By convention, a null key only matches anchored
7204 * probes: if each field is the empty string, reset dtpk_fmatch to
7205 * dtrace_match_nonzero().
7206 */
7207static void
7208dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp)
7209{
7210 pkp->dtpk_prov = pdp->dtpd_provider;
7211 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider);
7212
7213 pkp->dtpk_mod = pdp->dtpd_mod;
7214 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod);
7215
7216 pkp->dtpk_func = pdp->dtpd_func;
7217 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func);
7218
7219 pkp->dtpk_name = pdp->dtpd_name;
7220 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name);
7221
7222 pkp->dtpk_id = pdp->dtpd_id;
7223
7224 if (pkp->dtpk_id == DTRACE_IDNONE &&
7225 pkp->dtpk_pmatch == &dtrace_match_nul &&
7226 pkp->dtpk_mmatch == &dtrace_match_nul &&
7227 pkp->dtpk_fmatch == &dtrace_match_nul &&
7228 pkp->dtpk_nmatch == &dtrace_match_nul)
7229 pkp->dtpk_fmatch = &dtrace_match_nonzero;
7230}
7231
7232/*
7233 * DTrace Provider-to-Framework API Functions
7234 *
7235 * These functions implement much of the Provider-to-Framework API, as
7236 * described in <sys/dtrace.h>. The parts of the API not in this section are
7237 * the functions in the API for probe management (found below), and
7238 * dtrace_probe() itself (found above).
7239 */
7240
7241/*
7242 * Register the calling provider with the DTrace framework. This should
7243 * generally be called by DTrace providers in their attach(9E) entry point.
7244 */
7245int
7246dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv,
7247 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp)
7248{
7249 dtrace_provider_t *provider;
7250
7251 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) {
7252 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7253 "arguments", name ? name : "<NULL>");
7254 return (EINVAL);
7255 }
7256
7257 if (name[0] == '\0' || dtrace_badname(name)) {
7258 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7259 "provider name", name);
7260 return (EINVAL);
7261 }
7262
7263 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) ||
7264 pops->dtps_enable == NULL || pops->dtps_disable == NULL ||
7265 pops->dtps_destroy == NULL ||
7266 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) {
7267 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7268 "provider ops", name);
7269 return (EINVAL);
7270 }
7271
7272 if (dtrace_badattr(&pap->dtpa_provider) ||
7273 dtrace_badattr(&pap->dtpa_mod) ||
7274 dtrace_badattr(&pap->dtpa_func) ||
7275 dtrace_badattr(&pap->dtpa_name) ||
7276 dtrace_badattr(&pap->dtpa_args)) {
7277 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7278 "provider attributes", name);
7279 return (EINVAL);
7280 }
7281
7282 if (priv & ~DTRACE_PRIV_ALL) {
7283 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7284 "privilege attributes", name);
7285 return (EINVAL);
7286 }
7287
7288 if ((priv & DTRACE_PRIV_KERNEL) &&
7289 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) &&
7290 pops->dtps_usermode == NULL) {
7291 cmn_err(CE_WARN, "failed to register provider '%s': need "
7292 "dtps_usermode() op for given privilege attributes", name);
7293 return (EINVAL);
7294 }
7295
7296 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP);
7297 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7298 (void) strcpy(provider->dtpv_name, name);
7299
7300 provider->dtpv_attr = *pap;
7301 provider->dtpv_priv.dtpp_flags = priv;
7302 if (cr != NULL) {
7303 provider->dtpv_priv.dtpp_uid = crgetuid(cr);
7304 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr);
7305 }
7306 provider->dtpv_pops = *pops;
7307
7308 if (pops->dtps_provide == NULL) {
7309 ASSERT(pops->dtps_provide_module != NULL);
7310 provider->dtpv_pops.dtps_provide =
7311 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop;
7312 }
7313
7314 if (pops->dtps_provide_module == NULL) {
7315 ASSERT(pops->dtps_provide != NULL);
7316 provider->dtpv_pops.dtps_provide_module =
7317 (void (*)(void *, struct modctl *))dtrace_nullop;
7318 }
7319
7320 if (pops->dtps_suspend == NULL) {
7321 ASSERT(pops->dtps_resume == NULL);
7322 provider->dtpv_pops.dtps_suspend =
7323 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
7324 provider->dtpv_pops.dtps_resume =
7325 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
7326 }
7327
7328 provider->dtpv_arg = arg;
7329 *idp = (dtrace_provider_id_t)provider;
7330
7331 if (pops == &dtrace_provider_ops) {
7332 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
7333 ASSERT(MUTEX_HELD(&dtrace_lock));
7334 ASSERT(dtrace_anon.dta_enabling == NULL);
7335
7336 /*
7337 * We make sure that the DTrace provider is at the head of
7338 * the provider chain.
7339 */
7340 provider->dtpv_next = dtrace_provider;
7341 dtrace_provider = provider;
7342 return (0);
7343 }
7344
7345 mutex_enter(&dtrace_provider_lock);
7346 mutex_enter(&dtrace_lock);
7347
7348 /*
7349 * If there is at least one provider registered, we'll add this
7350 * provider after the first provider.
7351 */
7352 if (dtrace_provider != NULL) {
7353 provider->dtpv_next = dtrace_provider->dtpv_next;
7354 dtrace_provider->dtpv_next = provider;
7355 } else {
7356 dtrace_provider = provider;
7357 }
7358
7359 if (dtrace_retained != NULL) {
7360 dtrace_enabling_provide(provider);
7361
7362 /*
7363 * Now we need to call dtrace_enabling_matchall() -- which
7364 * will acquire cpu_lock and dtrace_lock. We therefore need
7365 * to drop all of our locks before calling into it...
7366 */
7367 mutex_exit(&dtrace_lock);
7368 mutex_exit(&dtrace_provider_lock);
7369 dtrace_enabling_matchall();
7370
7371 return (0);
7372 }
7373
7374 mutex_exit(&dtrace_lock);
7375 mutex_exit(&dtrace_provider_lock);
7376
7377 return (0);
7378}
7379
7380/*
7381 * Unregister the specified provider from the DTrace framework. This should
7382 * generally be called by DTrace providers in their detach(9E) entry point.
7383 */
7384int
7385dtrace_unregister(dtrace_provider_id_t id)
7386{
7387 dtrace_provider_t *old = (dtrace_provider_t *)id;
7388 dtrace_provider_t *prev = NULL;
7389 VBDTTYPE(uint32_t,int) i, self = 0;
7390 dtrace_probe_t *probe, *first = NULL;
7391
7392 if (old->dtpv_pops.dtps_enable ==
7393 (int (*)(void *, dtrace_id_t, void *))(uintptr_t)dtrace_enable_nullop) {
7394 /*
7395 * If DTrace itself is the provider, we're called with locks
7396 * already held.
7397 */
7398 ASSERT(old == dtrace_provider);
7399#ifndef VBOX
7400 ASSERT(dtrace_devi != NULL);
7401#endif
7402 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
7403 ASSERT(MUTEX_HELD(&dtrace_lock));
7404 self = 1;
7405
7406 if (dtrace_provider->dtpv_next != NULL) {
7407 /*
7408 * There's another provider here; return failure.
7409 */
7410 return (EBUSY);
7411 }
7412 } else {
7413 mutex_enter(&dtrace_provider_lock);
7414 mutex_enter(&mod_lock);
7415 mutex_enter(&dtrace_lock);
7416 }
7417
7418 /*
7419 * If anyone has /dev/dtrace open, or if there are anonymous enabled
7420 * probes, we refuse to let providers slither away, unless this
7421 * provider has already been explicitly invalidated.
7422 */
7423 if (!old->dtpv_defunct &&
7424 (dtrace_opens || (dtrace_anon.dta_state != NULL &&
7425 dtrace_anon.dta_state->dts_necbs > 0))) {
7426 if (!self) {
7427 mutex_exit(&dtrace_lock);
7428 mutex_exit(&mod_lock);
7429 mutex_exit(&dtrace_provider_lock);
7430 }
7431 return (EBUSY);
7432 }
7433
7434 /*
7435 * Attempt to destroy the probes associated with this provider.
7436 */
7437 for (i = 0; i < dtrace_nprobes; i++) {
7438 if ((probe = dtrace_probes[i]) == NULL)
7439 continue;
7440
7441 if (probe->dtpr_provider != old)
7442 continue;
7443
7444 if (probe->dtpr_ecb == NULL)
7445 continue;
7446
7447 /*
7448 * We have at least one ECB; we can't remove this provider.
7449 */
7450 if (!self) {
7451 mutex_exit(&dtrace_lock);
7452 mutex_exit(&mod_lock);
7453 mutex_exit(&dtrace_provider_lock);
7454 }
7455 return (EBUSY);
7456 }
7457
7458 /*
7459 * All of the probes for this provider are disabled; we can safely
7460 * remove all of them from their hash chains and from the probe array.
7461 */
7462 for (i = 0; i < dtrace_nprobes; i++) {
7463 if ((probe = dtrace_probes[i]) == NULL)
7464 continue;
7465
7466 if (probe->dtpr_provider != old)
7467 continue;
7468
7469 dtrace_probes[i] = NULL;
7470
7471 dtrace_hash_remove(dtrace_bymod, probe);
7472 dtrace_hash_remove(dtrace_byfunc, probe);
7473 dtrace_hash_remove(dtrace_byname, probe);
7474
7475 if (first == NULL) {
7476 first = probe;
7477 probe->dtpr_nextmod = NULL;
7478 } else {
7479 probe->dtpr_nextmod = first;
7480 first = probe;
7481 }
7482 }
7483
7484 /*
7485 * The provider's probes have been removed from the hash chains and
7486 * from the probe array. Now issue a dtrace_sync() to be sure that
7487 * everyone has cleared out from any probe array processing.
7488 */
7489 dtrace_sync();
7490
7491 for (probe = first; probe != NULL; probe = first) {
7492 first = probe->dtpr_nextmod;
7493
7494 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id,
7495 probe->dtpr_arg);
7496 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
7497 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
7498 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
7499 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1);
7500 kmem_free(probe, sizeof (dtrace_probe_t));
7501 }
7502
7503 if ((prev = dtrace_provider) == old) {
7504#ifndef VBOX
7505 ASSERT(self || dtrace_devi == NULL);
7506 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL);
7507#endif
7508 dtrace_provider = old->dtpv_next;
7509 } else {
7510 while (prev != NULL && prev->dtpv_next != old)
7511 prev = prev->dtpv_next;
7512
7513 if (prev == NULL) {
7514 panic("attempt to unregister non-existent "
7515 "dtrace provider %p\n", (void *)id);
7516 }
7517
7518 prev->dtpv_next = old->dtpv_next;
7519 }
7520
7521 if (!self) {
7522 mutex_exit(&dtrace_lock);
7523 mutex_exit(&mod_lock);
7524 mutex_exit(&dtrace_provider_lock);
7525 }
7526
7527 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1);
7528 kmem_free(old, sizeof (dtrace_provider_t));
7529
7530 return (0);
7531}
7532
7533/*
7534 * Invalidate the specified provider. All subsequent probe lookups for the
7535 * specified provider will fail, but its probes will not be removed.
7536 */
7537void
7538dtrace_invalidate(dtrace_provider_id_t id)
7539{
7540 dtrace_provider_t *pvp = (dtrace_provider_t *)id;
7541
7542 ASSERT(pvp->dtpv_pops.dtps_enable !=
7543 (int (*)(void *, dtrace_id_t, void *))(uintptr_t)dtrace_enable_nullop);
7544
7545 mutex_enter(&dtrace_provider_lock);
7546 mutex_enter(&dtrace_lock);
7547
7548 pvp->dtpv_defunct = 1;
7549
7550 mutex_exit(&dtrace_lock);
7551 mutex_exit(&dtrace_provider_lock);
7552}
7553
7554/*
7555 * Indicate whether or not DTrace has attached.
7556 */
7557int
7558dtrace_attached(void)
7559{
7560 /*
7561 * dtrace_provider will be non-NULL iff the DTrace driver has
7562 * attached. (It's non-NULL because DTrace is always itself a
7563 * provider.)
7564 */
7565 return (dtrace_provider != NULL);
7566}
7567
7568/*
7569 * Remove all the unenabled probes for the given provider. This function is
7570 * not unlike dtrace_unregister(), except that it doesn't remove the provider
7571 * -- just as many of its associated probes as it can.
7572 */
7573int
7574dtrace_condense(dtrace_provider_id_t id)
7575{
7576 dtrace_provider_t *prov = (dtrace_provider_t *)id;
7577 VBDTTYPE(uint32_t,int) i;
7578 dtrace_probe_t *probe;
7579
7580 /*
7581 * Make sure this isn't the dtrace provider itself.
7582 */
7583 ASSERT(prov->dtpv_pops.dtps_enable !=
7584 (int (*)(void *, dtrace_id_t, void *))(uintptr_t)dtrace_enable_nullop);
7585
7586 mutex_enter(&dtrace_provider_lock);
7587 mutex_enter(&dtrace_lock);
7588
7589 /*
7590 * Attempt to destroy the probes associated with this provider.
7591 */
7592 for (i = 0; i < dtrace_nprobes; i++) {
7593 if ((probe = dtrace_probes[i]) == NULL)
7594 continue;
7595
7596 if (probe->dtpr_provider != prov)
7597 continue;
7598
7599 if (probe->dtpr_ecb != NULL)
7600 continue;
7601
7602 dtrace_probes[i] = NULL;
7603
7604 dtrace_hash_remove(dtrace_bymod, probe);
7605 dtrace_hash_remove(dtrace_byfunc, probe);
7606 dtrace_hash_remove(dtrace_byname, probe);
7607
7608 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1,
7609 probe->dtpr_arg);
7610 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
7611 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
7612 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
7613 kmem_free(probe, sizeof (dtrace_probe_t));
7614 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1);
7615 }
7616
7617 mutex_exit(&dtrace_lock);
7618 mutex_exit(&dtrace_provider_lock);
7619
7620 return (0);
7621}
7622
7623/*
7624 * DTrace Probe Management Functions
7625 *
7626 * The functions in this section perform the DTrace probe management,
7627 * including functions to create probes, look-up probes, and call into the
7628 * providers to request that probes be provided. Some of these functions are
7629 * in the Provider-to-Framework API; these functions can be identified by the
7630 * fact that they are not declared "static".
7631 */
7632
7633/*
7634 * Create a probe with the specified module name, function name, and name.
7635 */
7636dtrace_id_t
7637dtrace_probe_create(dtrace_provider_id_t prov, const char *mod,
7638 const char *func, const char *name, int aframes, void *arg)
7639{
7640 dtrace_probe_t *probe, **probes;
7641 dtrace_provider_t *provider = (dtrace_provider_t *)prov;
7642 dtrace_id_t id;
7643
7644 if (provider == dtrace_provider) {
7645 ASSERT(MUTEX_HELD(&dtrace_lock));
7646 } else {
7647 mutex_enter(&dtrace_lock);
7648 }
7649
7650 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1,
7651 VM_BESTFIT | VM_SLEEP);
7652 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP);
7653
7654 probe->dtpr_id = id;
7655 probe->dtpr_gen = dtrace_probegen++;
7656 probe->dtpr_mod = dtrace_strdup(mod);
7657 probe->dtpr_func = dtrace_strdup(func);
7658 probe->dtpr_name = dtrace_strdup(name);
7659 probe->dtpr_arg = arg;
7660 probe->dtpr_aframes = aframes;
7661 probe->dtpr_provider = provider;
7662
7663 dtrace_hash_add(dtrace_bymod, probe);
7664 dtrace_hash_add(dtrace_byfunc, probe);
7665 dtrace_hash_add(dtrace_byname, probe);
7666
7667 if (id - 1 >= dtrace_nprobes) {
7668 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *);
7669 size_t nsize = osize << 1;
7670
7671 if (nsize == 0) {
7672 ASSERT(osize == 0);
7673 ASSERT(dtrace_probes == NULL);
7674 nsize = sizeof (dtrace_probe_t *);
7675 }
7676
7677 probes = kmem_zalloc(nsize, KM_SLEEP);
7678
7679 if (dtrace_probes == NULL) {
7680 ASSERT(osize == 0);
7681 dtrace_probes = probes;
7682 dtrace_nprobes = 1;
7683 } else {
7684 dtrace_probe_t **oprobes = dtrace_probes;
7685
7686 bcopy(oprobes, probes, osize);
7687 dtrace_membar_producer();
7688 dtrace_probes = probes;
7689
7690 dtrace_sync();
7691
7692 /*
7693 * All CPUs are now seeing the new probes array; we can
7694 * safely free the old array.
7695 */
7696 kmem_free(oprobes, osize);
7697 dtrace_nprobes <<= 1;
7698 }
7699
7700 ASSERT(id - 1 < dtrace_nprobes);
7701 }
7702
7703 ASSERT(dtrace_probes[id - 1] == NULL);
7704 dtrace_probes[id - 1] = probe;
7705
7706 if (provider != dtrace_provider)
7707 mutex_exit(&dtrace_lock);
7708
7709 return (id);
7710}
7711
7712static dtrace_probe_t *
7713dtrace_probe_lookup_id(dtrace_id_t id)
7714{
7715 ASSERT(MUTEX_HELD(&dtrace_lock));
7716
7717 if (id == 0 || id > dtrace_nprobes)
7718 return (NULL);
7719
7720 return (dtrace_probes[id - 1]);
7721}
7722
7723static int
7724dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg)
7725{
7726 *((dtrace_id_t *)arg) = probe->dtpr_id;
7727
7728 return (DTRACE_MATCH_DONE);
7729}
7730
7731/*
7732 * Look up a probe based on provider and one or more of module name, function
7733 * name and probe name.
7734 */
7735dtrace_id_t
7736dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod,
7737 const char *func, const char *name)
7738{
7739 dtrace_probekey_t pkey;
7740 dtrace_id_t id;
7741 int match;
7742
7743 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name;
7744 pkey.dtpk_pmatch = &dtrace_match_string;
7745 pkey.dtpk_mod = mod;
7746 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul;
7747 pkey.dtpk_func = func;
7748 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul;
7749 pkey.dtpk_name = name;
7750 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul;
7751 pkey.dtpk_id = DTRACE_IDNONE;
7752
7753 mutex_enter(&dtrace_lock);
7754 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0,
7755 dtrace_probe_lookup_match, &id);
7756 mutex_exit(&dtrace_lock);
7757
7758 ASSERT(match == 1 || match == 0);
7759 return (match ? id : 0);
7760}
7761
7762/*
7763 * Returns the probe argument associated with the specified probe.
7764 */
7765void *
7766dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid)
7767{
7768 dtrace_probe_t *probe;
7769 void *rval = NULL;
7770
7771 mutex_enter(&dtrace_lock);
7772
7773 if ((probe = dtrace_probe_lookup_id(pid)) != NULL &&
7774 probe->dtpr_provider == (dtrace_provider_t *)id)
7775 rval = probe->dtpr_arg;
7776
7777 mutex_exit(&dtrace_lock);
7778
7779 return (rval);
7780}
7781
7782/*
7783 * Copy a probe into a probe description.
7784 */
7785static void
7786dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp)
7787{
7788 bzero(pdp, sizeof (dtrace_probedesc_t));
7789 pdp->dtpd_id = prp->dtpr_id;
7790
7791 (void) strncpy(pdp->dtpd_provider,
7792 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1);
7793
7794 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1);
7795 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1);
7796 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1);
7797}
7798
7799/*
7800 * Called to indicate that a probe -- or probes -- should be provided by a
7801 * specfied provider. If the specified description is NULL, the provider will
7802 * be told to provide all of its probes. (This is done whenever a new
7803 * consumer comes along, or whenever a retained enabling is to be matched.) If
7804 * the specified description is non-NULL, the provider is given the
7805 * opportunity to dynamically provide the specified probe, allowing providers
7806 * to support the creation of probes on-the-fly. (So-called _autocreated_
7807 * probes.) If the provider is NULL, the operations will be applied to all
7808 * providers; if the provider is non-NULL the operations will only be applied
7809 * to the specified provider. The dtrace_provider_lock must be held, and the
7810 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
7811 * will need to grab the dtrace_lock when it reenters the framework through
7812 * dtrace_probe_lookup(), dtrace_probe_create(), etc.
7813 */
7814static void
7815dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv)
7816{
7817#ifndef VBOX
7818 struct modctl *ctl;
7819#endif
7820 int all = 0;
7821
7822 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
7823
7824 if (prv == NULL) {
7825 all = 1;
7826 prv = dtrace_provider;
7827 }
7828
7829 do {
7830 /*
7831 * First, call the blanket provide operation.
7832 */
7833 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc);
7834
7835#ifndef VBOX
7836 /*
7837 * Now call the per-module provide operation. We will grab
7838 * mod_lock to prevent the list from being modified. Note
7839 * that this also prevents the mod_busy bits from changing.
7840 * (mod_busy can only be changed with mod_lock held.)
7841 */
7842 mutex_enter(&mod_lock);
7843
7844 ctl = &modules;
7845 do {
7846 if (ctl->mod_busy || ctl->mod_mp == NULL)
7847 continue;
7848
7849 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
7850
7851 } while ((ctl = ctl->mod_next) != &modules);
7852
7853 mutex_exit(&mod_lock);
7854#endif
7855 } while (all && (prv = prv->dtpv_next) != NULL);
7856}
7857
7858#ifndef VBOX
7859/*
7860 * Iterate over each probe, and call the Framework-to-Provider API function
7861 * denoted by offs.
7862 */
7863static void
7864dtrace_probe_foreach(uintptr_t offs)
7865{
7866 dtrace_provider_t *prov;
7867 void (*func)(void *, dtrace_id_t, void *);
7868 dtrace_probe_t *probe;
7869 dtrace_icookie_t cookie;
7870 VBDTTYPE(uint32_t,int) i;
7871
7872 /*
7873 * We disable interrupts to walk through the probe array. This is
7874 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
7875 * won't see stale data.
7876 */
7877 cookie = dtrace_interrupt_disable();
7878
7879 for (i = 0; i < dtrace_nprobes; i++) {
7880 if ((probe = dtrace_probes[i]) == NULL)
7881 continue;
7882
7883 if (probe->dtpr_ecb == NULL) {
7884 /*
7885 * This probe isn't enabled -- don't call the function.
7886 */
7887 continue;
7888 }
7889
7890 prov = probe->dtpr_provider;
7891 func = *((void(**)(void *, dtrace_id_t, void *))
7892 ((uintptr_t)&prov->dtpv_pops + offs));
7893
7894 func(prov->dtpv_arg, i + 1, probe->dtpr_arg);
7895 }
7896
7897 dtrace_interrupt_enable(cookie);
7898}
7899#endif /* !VBOX */
7900
7901static int
7902dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab)
7903{
7904 dtrace_probekey_t pkey;
7905 uint32_t priv;
7906 uid_t uid;
7907 zoneid_t zoneid;
7908
7909 ASSERT(MUTEX_HELD(&dtrace_lock));
7910 dtrace_ecb_create_cache = NULL;
7911
7912 if (desc == NULL) {
7913 /*
7914 * If we're passed a NULL description, we're being asked to
7915 * create an ECB with a NULL probe.
7916 */
7917 (void) dtrace_ecb_create_enable(NULL, enab);
7918 return (0);
7919 }
7920
7921 dtrace_probekey(desc, &pkey);
7922 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred,
7923 &priv, &uid, &zoneid);
7924
7925 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable,
7926 enab));
7927}
7928
7929/*
7930 * DTrace Helper Provider Functions
7931 */
7932static void
7933dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr)
7934{
7935 attr->dtat_name = DOF_ATTR_NAME(dofattr);
7936 attr->dtat_data = DOF_ATTR_DATA(dofattr);
7937 attr->dtat_class = DOF_ATTR_CLASS(dofattr);
7938}
7939
7940static void
7941dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov,
7942 const dof_provider_t *dofprov, char *strtab)
7943{
7944 hprov->dthpv_provname = strtab + dofprov->dofpv_name;
7945 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider,
7946 dofprov->dofpv_provattr);
7947 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod,
7948 dofprov->dofpv_modattr);
7949 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func,
7950 dofprov->dofpv_funcattr);
7951 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name,
7952 dofprov->dofpv_nameattr);
7953 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args,
7954 dofprov->dofpv_argsattr);
7955}
7956
7957static void
7958dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
7959{
7960 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
7961 dof_hdr_t *dof = (dof_hdr_t *)daddr;
7962 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
7963 dof_provider_t *provider;
7964 dof_probe_t *probe;
7965 uint32_t *off, *enoff;
7966 uint8_t *arg;
7967 char *strtab;
7968 uint_t i, nprobes;
7969 dtrace_helper_provdesc_t dhpv;
7970 dtrace_helper_probedesc_t dhpb;
7971 dtrace_meta_t *meta = dtrace_meta_pid;
7972 dtrace_mops_t *mops = &meta->dtm_mops;
7973 void *parg;
7974
7975 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
7976 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
7977 provider->dofpv_strtab * dof->dofh_secsize);
7978 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
7979 provider->dofpv_probes * dof->dofh_secsize);
7980 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
7981 provider->dofpv_prargs * dof->dofh_secsize);
7982 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
7983 provider->dofpv_proffs * dof->dofh_secsize);
7984
7985 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
7986 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset);
7987 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
7988 enoff = NULL;
7989
7990 /*
7991 * See dtrace_helper_provider_validate().
7992 */
7993 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
7994 provider->dofpv_prenoffs != DOF_SECT_NONE) {
7995 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
7996 provider->dofpv_prenoffs * dof->dofh_secsize);
7997 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset);
7998 }
7999
8000 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
8001
8002 /*
8003 * Create the provider.
8004 */
8005 dtrace_dofprov2hprov(&dhpv, provider, strtab);
8006
8007 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL)
8008 return;
8009
8010 meta->dtm_count++;
8011
8012 /*
8013 * Create the probes.
8014 */
8015 for (i = 0; i < nprobes; i++) {
8016 probe = (dof_probe_t *)(uintptr_t)(daddr +
8017 prb_sec->dofs_offset + i * prb_sec->dofs_entsize);
8018
8019 dhpb.dthpb_mod = dhp->dofhp_mod;
8020 dhpb.dthpb_func = strtab + probe->dofpr_func;
8021 dhpb.dthpb_name = strtab + probe->dofpr_name;
8022 dhpb.dthpb_base = probe->dofpr_addr;
8023 dhpb.dthpb_offs = off + probe->dofpr_offidx;
8024 dhpb.dthpb_noffs = probe->dofpr_noffs;
8025 if (enoff != NULL) {
8026 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx;
8027 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs;
8028 } else {
8029 dhpb.dthpb_enoffs = NULL;
8030 dhpb.dthpb_nenoffs = 0;
8031 }
8032 dhpb.dthpb_args = arg + probe->dofpr_argidx;
8033 dhpb.dthpb_nargc = probe->dofpr_nargc;
8034 dhpb.dthpb_xargc = probe->dofpr_xargc;
8035 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv;
8036 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv;
8037
8038 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb);
8039 }
8040}
8041
8042static void
8043dtrace_helper_provide(dof_helper_t *dhp, pid_t pid)
8044{
8045 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8046 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8047 VBDTTYPE(uint32_t,int) i;
8048
8049 ASSERT(MUTEX_HELD(&dtrace_meta_lock));
8050
8051 for (i = 0; i < dof->dofh_secnum; i++) {
8052 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
8053 dof->dofh_secoff + i * dof->dofh_secsize);
8054
8055 if (sec->dofs_type != DOF_SECT_PROVIDER)
8056 continue;
8057
8058 dtrace_helper_provide_one(dhp, sec, pid);
8059 }
8060
8061 /*
8062 * We may have just created probes, so we must now rematch against
8063 * any retained enablings. Note that this call will acquire both
8064 * cpu_lock and dtrace_lock; the fact that we are holding
8065 * dtrace_meta_lock now is what defines the ordering with respect to
8066 * these three locks.
8067 */
8068 dtrace_enabling_matchall();
8069}
8070
8071#ifndef VBOX
8072
8073static void
8074dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
8075{
8076 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8077 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8078 dof_sec_t *str_sec;
8079 dof_provider_t *provider;
8080 char *strtab;
8081 dtrace_helper_provdesc_t dhpv;
8082 dtrace_meta_t *meta = dtrace_meta_pid;
8083 dtrace_mops_t *mops = &meta->dtm_mops;
8084
8085 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
8086 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8087 provider->dofpv_strtab * dof->dofh_secsize);
8088
8089 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
8090
8091 /*
8092 * Create the provider.
8093 */
8094 dtrace_dofprov2hprov(&dhpv, provider, strtab);
8095
8096 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid);
8097
8098 meta->dtm_count--;
8099}
8100
8101static void
8102dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid)
8103{
8104 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8105 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8106 VBDTTYPE(uint32_t,int) i;
8107
8108 ASSERT(MUTEX_HELD(&dtrace_meta_lock));
8109
8110 for (i = 0; i < dof->dofh_secnum; i++) {
8111 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
8112 dof->dofh_secoff + i * dof->dofh_secsize);
8113
8114 if (sec->dofs_type != DOF_SECT_PROVIDER)
8115 continue;
8116
8117 dtrace_helper_provider_remove_one(dhp, sec, pid);
8118 }
8119}
8120
8121#endif /* !VBOX */
8122
8123/*
8124 * DTrace Meta Provider-to-Framework API Functions
8125 *
8126 * These functions implement the Meta Provider-to-Framework API, as described
8127 * in <sys/dtrace.h>.
8128 */
8129int
8130dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg,
8131 dtrace_meta_provider_id_t *idp)
8132{
8133 dtrace_meta_t *meta;
8134 dtrace_helpers_t *help, *next;
8135 VBDTTYPE(uint32_t,int) i;
8136
8137 *idp = DTRACE_METAPROVNONE;
8138
8139 /*
8140 * We strictly don't need the name, but we hold onto it for
8141 * debuggability. All hail error queues!
8142 */
8143 if (name == NULL) {
8144 cmn_err(CE_WARN, "failed to register meta-provider: "
8145 "invalid name");
8146 return (EINVAL);
8147 }
8148
8149 if (mops == NULL ||
8150 mops->dtms_create_probe == NULL ||
8151 mops->dtms_provide_pid == NULL ||
8152 mops->dtms_remove_pid == NULL) {
8153 cmn_err(CE_WARN, "failed to register meta-register %s: "
8154 "invalid ops", name);
8155 return (EINVAL);
8156 }
8157
8158 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP);
8159 meta->dtm_mops = *mops;
8160 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8161 (void) strcpy(meta->dtm_name, name);
8162 meta->dtm_arg = arg;
8163
8164 mutex_enter(&dtrace_meta_lock);
8165 mutex_enter(&dtrace_lock);
8166
8167 if (dtrace_meta_pid != NULL) {
8168 mutex_exit(&dtrace_lock);
8169 mutex_exit(&dtrace_meta_lock);
8170 cmn_err(CE_WARN, "failed to register meta-register %s: "
8171 "user-land meta-provider exists", name);
8172 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1);
8173 kmem_free(meta, sizeof (dtrace_meta_t));
8174 return (EINVAL);
8175 }
8176
8177 dtrace_meta_pid = meta;
8178 *idp = (dtrace_meta_provider_id_t)meta;
8179
8180 /*
8181 * If there are providers and probes ready to go, pass them
8182 * off to the new meta provider now.
8183 */
8184
8185 help = dtrace_deferred_pid;
8186 dtrace_deferred_pid = NULL;
8187
8188 mutex_exit(&dtrace_lock);
8189
8190 while (help != NULL) {
8191 for (i = 0; i < help->dthps_nprovs; i++) {
8192 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
8193 help->dthps_pid);
8194 }
8195
8196 next = help->dthps_next;
8197 help->dthps_next = NULL;
8198 help->dthps_prev = NULL;
8199 help->dthps_deferred = 0;
8200 help = next;
8201 }
8202
8203 mutex_exit(&dtrace_meta_lock);
8204
8205 return (0);
8206}
8207
8208int
8209dtrace_meta_unregister(dtrace_meta_provider_id_t id)
8210{
8211 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id;
8212
8213 mutex_enter(&dtrace_meta_lock);
8214 mutex_enter(&dtrace_lock);
8215
8216 if (old == dtrace_meta_pid) {
8217 pp = &dtrace_meta_pid;
8218 } else {
8219 panic("attempt to unregister non-existent "
8220 "dtrace meta-provider %p\n", (void *)old);
8221#ifdef VBOX
8222 return EINVAL;
8223#endif
8224 }
8225
8226 if (old->dtm_count != 0) {
8227 mutex_exit(&dtrace_lock);
8228 mutex_exit(&dtrace_meta_lock);
8229 return (EBUSY);
8230 }
8231
8232 *pp = NULL;
8233
8234 mutex_exit(&dtrace_lock);
8235 mutex_exit(&dtrace_meta_lock);
8236
8237 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1);
8238 kmem_free(old, sizeof (dtrace_meta_t));
8239
8240 return (0);
8241}
8242
8243
8244/*
8245 * DTrace DIF Object Functions
8246 */
8247static int
8248dtrace_difo_err(uint_t pc, const char *format, ...)
8249{
8250 if (dtrace_err_verbose) {
8251 va_list alist;
8252
8253 (void) uprintf("dtrace DIF object error: [%u]: ", pc);
8254 va_start(alist, format);
8255 (void) vuprintf(format, alist);
8256 va_end(alist);
8257 }
8258
8259#ifdef DTRACE_ERRDEBUG
8260 dtrace_errdebug(format);
8261#endif
8262 return (1);
8263}
8264
8265/*
8266 * Validate a DTrace DIF object by checking the IR instructions. The following
8267 * rules are currently enforced by dtrace_difo_validate():
8268 *
8269 * 1. Each instruction must have a valid opcode
8270 * 2. Each register, string, variable, or subroutine reference must be valid
8271 * 3. No instruction can modify register %r0 (must be zero)
8272 * 4. All instruction reserved bits must be set to zero
8273 * 5. The last instruction must be a "ret" instruction
8274 * 6. All branch targets must reference a valid instruction _after_ the branch
8275 */
8276static int
8277dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs,
8278 cred_t *cr)
8279{
8280#ifndef VBOX
8281 int err = 0, i;
8282#else
8283 int err = 0;
8284 uint_t i;
8285#endif
8286 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
8287 int kcheckload;
8288 uint_t pc;
8289
8290 kcheckload = cr == NULL ||
8291 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0;
8292
8293 dp->dtdo_destructive = 0;
8294
8295 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
8296 dif_instr_t instr = dp->dtdo_buf[pc];
8297
8298 uint_t r1 = DIF_INSTR_R1(instr);
8299 uint_t r2 = DIF_INSTR_R2(instr);
8300 uint_t rd = DIF_INSTR_RD(instr);
8301 uint_t rs = DIF_INSTR_RS(instr);
8302 uint_t label = DIF_INSTR_LABEL(instr);
8303 uint_t v = DIF_INSTR_VAR(instr);
8304 uint_t subr = DIF_INSTR_SUBR(instr);
8305 uint_t type = DIF_INSTR_TYPE(instr);
8306 uint_t op = DIF_INSTR_OP(instr);
8307
8308 switch (op) {
8309 case DIF_OP_OR:
8310 case DIF_OP_XOR:
8311 case DIF_OP_AND:
8312 case DIF_OP_SLL:
8313 case DIF_OP_SRL:
8314 case DIF_OP_SRA:
8315 case DIF_OP_SUB:
8316 case DIF_OP_ADD:
8317 case DIF_OP_MUL:
8318 case DIF_OP_SDIV:
8319 case DIF_OP_UDIV:
8320 case DIF_OP_SREM:
8321 case DIF_OP_UREM:
8322 case DIF_OP_COPYS:
8323 if (r1 >= nregs)
8324 err += efunc(pc, "invalid register %u\n", r1);
8325 if (r2 >= nregs)
8326 err += efunc(pc, "invalid register %u\n", r2);
8327 if (rd >= nregs)
8328 err += efunc(pc, "invalid register %u\n", rd);
8329 if (rd == 0)
8330 err += efunc(pc, "cannot write to %r0\n");
8331 break;
8332 case DIF_OP_NOT:
8333 case DIF_OP_MOV:
8334 case DIF_OP_ALLOCS:
8335 if (r1 >= nregs)
8336 err += efunc(pc, "invalid register %u\n", r1);
8337 if (r2 != 0)
8338 err += efunc(pc, "non-zero reserved bits\n");
8339 if (rd >= nregs)
8340 err += efunc(pc, "invalid register %u\n", rd);
8341 if (rd == 0)
8342 err += efunc(pc, "cannot write to %r0\n");
8343 break;
8344 case DIF_OP_LDSB:
8345 case DIF_OP_LDSH:
8346 case DIF_OP_LDSW:
8347 case DIF_OP_LDUB:
8348 case DIF_OP_LDUH:
8349 case DIF_OP_LDUW:
8350 case DIF_OP_LDX:
8351 if (r1 >= nregs)
8352 err += efunc(pc, "invalid register %u\n", r1);
8353 if (r2 != 0)
8354 err += efunc(pc, "non-zero reserved bits\n");
8355 if (rd >= nregs)
8356 err += efunc(pc, "invalid register %u\n", rd);
8357 if (rd == 0)
8358 err += efunc(pc, "cannot write to %r0\n");
8359 if (kcheckload)
8360 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op +
8361 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd);
8362 break;
8363 case DIF_OP_RLDSB:
8364 case DIF_OP_RLDSH:
8365 case DIF_OP_RLDSW:
8366 case DIF_OP_RLDUB:
8367 case DIF_OP_RLDUH:
8368 case DIF_OP_RLDUW:
8369 case DIF_OP_RLDX:
8370 if (r1 >= nregs)
8371 err += efunc(pc, "invalid register %u\n", r1);
8372 if (r2 != 0)
8373 err += efunc(pc, "non-zero reserved bits\n");
8374 if (rd >= nregs)
8375 err += efunc(pc, "invalid register %u\n", rd);
8376 if (rd == 0)
8377 err += efunc(pc, "cannot write to %r0\n");
8378 break;
8379 case DIF_OP_ULDSB:
8380 case DIF_OP_ULDSH:
8381 case DIF_OP_ULDSW:
8382 case DIF_OP_ULDUB:
8383 case DIF_OP_ULDUH:
8384 case DIF_OP_ULDUW:
8385 case DIF_OP_ULDX:
8386 if (r1 >= nregs)
8387 err += efunc(pc, "invalid register %u\n", r1);
8388 if (r2 != 0)
8389 err += efunc(pc, "non-zero reserved bits\n");
8390 if (rd >= nregs)
8391 err += efunc(pc, "invalid register %u\n", rd);
8392 if (rd == 0)
8393 err += efunc(pc, "cannot write to %r0\n");
8394 break;
8395 case DIF_OP_STB:
8396 case DIF_OP_STH:
8397 case DIF_OP_STW:
8398 case DIF_OP_STX:
8399 if (r1 >= nregs)
8400 err += efunc(pc, "invalid register %u\n", r1);
8401 if (r2 != 0)
8402 err += efunc(pc, "non-zero reserved bits\n");
8403 if (rd >= nregs)
8404 err += efunc(pc, "invalid register %u\n", rd);
8405 if (rd == 0)
8406 err += efunc(pc, "cannot write to 0 address\n");
8407 break;
8408 case DIF_OP_CMP:
8409 case DIF_OP_SCMP:
8410 if (r1 >= nregs)
8411 err += efunc(pc, "invalid register %u\n", r1);
8412 if (r2 >= nregs)
8413 err += efunc(pc, "invalid register %u\n", r2);
8414 if (rd != 0)
8415 err += efunc(pc, "non-zero reserved bits\n");
8416 break;
8417 case DIF_OP_TST:
8418 if (r1 >= nregs)
8419 err += efunc(pc, "invalid register %u\n", r1);
8420 if (r2 != 0 || rd != 0)
8421 err += efunc(pc, "non-zero reserved bits\n");
8422 break;
8423 case DIF_OP_BA:
8424 case DIF_OP_BE:
8425 case DIF_OP_BNE:
8426 case DIF_OP_BG:
8427 case DIF_OP_BGU:
8428 case DIF_OP_BGE:
8429 case DIF_OP_BGEU:
8430 case DIF_OP_BL:
8431 case DIF_OP_BLU:
8432 case DIF_OP_BLE:
8433 case DIF_OP_BLEU:
8434 if (label >= dp->dtdo_len) {
8435 err += efunc(pc, "invalid branch target %u\n",
8436 label);
8437 }
8438 if (label <= pc) {
8439 err += efunc(pc, "backward branch to %u\n",
8440 label);
8441 }
8442 break;
8443 case DIF_OP_RET:
8444 if (r1 != 0 || r2 != 0)
8445 err += efunc(pc, "non-zero reserved bits\n");
8446 if (rd >= nregs)
8447 err += efunc(pc, "invalid register %u\n", rd);
8448 break;
8449 case DIF_OP_NOP:
8450 case DIF_OP_POPTS:
8451 case DIF_OP_FLUSHTS:
8452 if (r1 != 0 || r2 != 0 || rd != 0)
8453 err += efunc(pc, "non-zero reserved bits\n");
8454 break;
8455 case DIF_OP_SETX:
8456 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) {
8457 err += efunc(pc, "invalid integer ref %u\n",
8458 DIF_INSTR_INTEGER(instr));
8459 }
8460 if (rd >= nregs)
8461 err += efunc(pc, "invalid register %u\n", rd);
8462 if (rd == 0)
8463 err += efunc(pc, "cannot write to %r0\n");
8464 break;
8465 case DIF_OP_SETS:
8466 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) {
8467 err += efunc(pc, "invalid string ref %u\n",
8468 DIF_INSTR_STRING(instr));
8469 }
8470 if (rd >= nregs)
8471 err += efunc(pc, "invalid register %u\n", rd);
8472 if (rd == 0)
8473 err += efunc(pc, "cannot write to %r0\n");
8474 break;
8475 case DIF_OP_LDGA:
8476 case DIF_OP_LDTA:
8477 if (r1 > DIF_VAR_ARRAY_MAX)
8478 err += efunc(pc, "invalid array %u\n", r1);
8479 if (r2 >= nregs)
8480 err += efunc(pc, "invalid register %u\n", r2);
8481 if (rd >= nregs)
8482 err += efunc(pc, "invalid register %u\n", rd);
8483 if (rd == 0)
8484 err += efunc(pc, "cannot write to %r0\n");
8485 break;
8486 case DIF_OP_LDGS:
8487 case DIF_OP_LDTS:
8488 case DIF_OP_LDLS:
8489 case DIF_OP_LDGAA:
8490 case DIF_OP_LDTAA:
8491 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX)
8492 err += efunc(pc, "invalid variable %u\n", v);
8493 if (rd >= nregs)
8494 err += efunc(pc, "invalid register %u\n", rd);
8495 if (rd == 0)
8496 err += efunc(pc, "cannot write to %r0\n");
8497 break;
8498 case DIF_OP_STGS:
8499 case DIF_OP_STTS:
8500 case DIF_OP_STLS:
8501 case DIF_OP_STGAA:
8502 case DIF_OP_STTAA:
8503 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX)
8504 err += efunc(pc, "invalid variable %u\n", v);
8505 if (rs >= nregs)
8506 err += efunc(pc, "invalid register %u\n", rd);
8507 break;
8508 case DIF_OP_CALL:
8509 if (subr > DIF_SUBR_MAX)
8510 err += efunc(pc, "invalid subr %u\n", subr);
8511 if (rd >= nregs)
8512 err += efunc(pc, "invalid register %u\n", rd);
8513 if (rd == 0)
8514 err += efunc(pc, "cannot write to %r0\n");
8515
8516 if (subr == DIF_SUBR_COPYOUT ||
8517 subr == DIF_SUBR_COPYOUTSTR) {
8518 dp->dtdo_destructive = 1;
8519 }
8520 break;
8521 case DIF_OP_PUSHTR:
8522 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF)
8523 err += efunc(pc, "invalid ref type %u\n", type);
8524 if (r2 >= nregs)
8525 err += efunc(pc, "invalid register %u\n", r2);
8526 if (rs >= nregs)
8527 err += efunc(pc, "invalid register %u\n", rs);
8528 break;
8529 case DIF_OP_PUSHTV:
8530 if (type != DIF_TYPE_CTF)
8531 err += efunc(pc, "invalid val type %u\n", type);
8532 if (r2 >= nregs)
8533 err += efunc(pc, "invalid register %u\n", r2);
8534 if (rs >= nregs)
8535 err += efunc(pc, "invalid register %u\n", rs);
8536 break;
8537 default:
8538 err += efunc(pc, "invalid opcode %u\n",
8539 DIF_INSTR_OP(instr));
8540 }
8541 }
8542
8543 if (dp->dtdo_len != 0 &&
8544 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) {
8545 err += efunc(dp->dtdo_len - 1,
8546 "expected 'ret' as last DIF instruction\n");
8547 }
8548
8549 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) {
8550 /*
8551 * If we're not returning by reference, the size must be either
8552 * 0 or the size of one of the base types.
8553 */
8554 switch (dp->dtdo_rtype.dtdt_size) {
8555 case 0:
8556 case sizeof (uint8_t):
8557 case sizeof (uint16_t):
8558 case sizeof (uint32_t):
8559 case sizeof (uint64_t):
8560 break;
8561
8562 default:
8563 err += efunc(dp->dtdo_len - 1, "bad return size\n");
8564 }
8565 }
8566
8567 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) {
8568 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL;
8569 dtrace_diftype_t *vt, *et;
8570 uint_t id, ndx;
8571
8572 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL &&
8573 v->dtdv_scope != DIFV_SCOPE_THREAD &&
8574 v->dtdv_scope != DIFV_SCOPE_LOCAL) {
8575 err += efunc(i, "unrecognized variable scope %d\n",
8576 v->dtdv_scope);
8577 break;
8578 }
8579
8580 if (v->dtdv_kind != DIFV_KIND_ARRAY &&
8581 v->dtdv_kind != DIFV_KIND_SCALAR) {
8582 err += efunc(i, "unrecognized variable type %d\n",
8583 v->dtdv_kind);
8584 break;
8585 }
8586
8587 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) {
8588 err += efunc(i, "%d exceeds variable id limit\n", id);
8589 break;
8590 }
8591
8592 if (id < DIF_VAR_OTHER_UBASE)
8593 continue;
8594
8595 /*
8596 * For user-defined variables, we need to check that this
8597 * definition is identical to any previous definition that we
8598 * encountered.
8599 */
8600 ndx = id - DIF_VAR_OTHER_UBASE;
8601
8602 switch (v->dtdv_scope) {
8603 case DIFV_SCOPE_GLOBAL:
8604 if (VBDTCAST(int64_t)ndx < vstate->dtvs_nglobals) {
8605 dtrace_statvar_t *svar;
8606
8607 if ((svar = vstate->dtvs_globals[ndx]) != NULL)
8608 existing = &svar->dtsv_var;
8609 }
8610
8611 break;
8612
8613 case DIFV_SCOPE_THREAD:
8614 if (VBDTCAST(int64_t)ndx < vstate->dtvs_ntlocals)
8615 existing = &vstate->dtvs_tlocals[ndx];
8616 break;
8617
8618 case DIFV_SCOPE_LOCAL:
8619 if (VBDTCAST(int64_t)ndx < vstate->dtvs_nlocals) {
8620 dtrace_statvar_t *svar;
8621
8622 if ((svar = vstate->dtvs_locals[ndx]) != NULL)
8623 existing = &svar->dtsv_var;
8624 }
8625
8626 break;
8627 }
8628
8629 vt = &v->dtdv_type;
8630
8631 if (vt->dtdt_flags & DIF_TF_BYREF) {
8632 if (vt->dtdt_size == 0) {
8633 err += efunc(i, "zero-sized variable\n");
8634 break;
8635 }
8636
8637 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL &&
8638 vt->dtdt_size > dtrace_global_maxsize) {
8639 err += efunc(i, "oversized by-ref global\n");
8640 break;
8641 }
8642 }
8643
8644 if (existing == NULL || existing->dtdv_id == 0)
8645 continue;
8646
8647 ASSERT(existing->dtdv_id == v->dtdv_id);
8648 ASSERT(existing->dtdv_scope == v->dtdv_scope);
8649
8650 if (existing->dtdv_kind != v->dtdv_kind)
8651 err += efunc(i, "%d changed variable kind\n", id);
8652
8653 et = &existing->dtdv_type;
8654
8655 if (vt->dtdt_flags != et->dtdt_flags) {
8656 err += efunc(i, "%d changed variable type flags\n", id);
8657 break;
8658 }
8659
8660 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) {
8661 err += efunc(i, "%d changed variable type size\n", id);
8662 break;
8663 }
8664 }
8665
8666 return (err);
8667}
8668
8669#ifndef VBOX
8670/*
8671 * Validate a DTrace DIF object that it is to be used as a helper. Helpers
8672 * are much more constrained than normal DIFOs. Specifically, they may
8673 * not:
8674 *
8675 * 1. Make calls to subroutines other than copyin(), copyinstr() or
8676 * miscellaneous string routines
8677 * 2. Access DTrace variables other than the args[] array, and the
8678 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
8679 * 3. Have thread-local variables.
8680 * 4. Have dynamic variables.
8681 */
8682static int
8683dtrace_difo_validate_helper(dtrace_difo_t *dp)
8684{
8685 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
8686 int err = 0;
8687 uint_t pc;
8688
8689 for (pc = 0; pc < dp->dtdo_len; pc++) {
8690 dif_instr_t instr = dp->dtdo_buf[pc];
8691
8692 uint_t v = DIF_INSTR_VAR(instr);
8693 uint_t subr = DIF_INSTR_SUBR(instr);
8694 uint_t op = DIF_INSTR_OP(instr);
8695
8696 switch (op) {
8697 case DIF_OP_OR:
8698 case DIF_OP_XOR:
8699 case DIF_OP_AND:
8700 case DIF_OP_SLL:
8701 case DIF_OP_SRL:
8702 case DIF_OP_SRA:
8703 case DIF_OP_SUB:
8704 case DIF_OP_ADD:
8705 case DIF_OP_MUL:
8706 case DIF_OP_SDIV:
8707 case DIF_OP_UDIV:
8708 case DIF_OP_SREM:
8709 case DIF_OP_UREM:
8710 case DIF_OP_COPYS:
8711 case DIF_OP_NOT:
8712 case DIF_OP_MOV:
8713 case DIF_OP_RLDSB:
8714 case DIF_OP_RLDSH:
8715 case DIF_OP_RLDSW:
8716 case DIF_OP_RLDUB:
8717 case DIF_OP_RLDUH:
8718 case DIF_OP_RLDUW:
8719 case DIF_OP_RLDX:
8720 case DIF_OP_ULDSB:
8721 case DIF_OP_ULDSH:
8722 case DIF_OP_ULDSW:
8723 case DIF_OP_ULDUB:
8724 case DIF_OP_ULDUH:
8725 case DIF_OP_ULDUW:
8726 case DIF_OP_ULDX:
8727 case DIF_OP_STB:
8728 case DIF_OP_STH:
8729 case DIF_OP_STW:
8730 case DIF_OP_STX:
8731 case DIF_OP_ALLOCS:
8732 case DIF_OP_CMP:
8733 case DIF_OP_SCMP:
8734 case DIF_OP_TST:
8735 case DIF_OP_BA:
8736 case DIF_OP_BE:
8737 case DIF_OP_BNE:
8738 case DIF_OP_BG:
8739 case DIF_OP_BGU:
8740 case DIF_OP_BGE:
8741 case DIF_OP_BGEU:
8742 case DIF_OP_BL:
8743 case DIF_OP_BLU:
8744 case DIF_OP_BLE:
8745 case DIF_OP_BLEU:
8746 case DIF_OP_RET:
8747 case DIF_OP_NOP:
8748 case DIF_OP_POPTS:
8749 case DIF_OP_FLUSHTS:
8750 case DIF_OP_SETX:
8751 case DIF_OP_SETS:
8752 case DIF_OP_LDGA:
8753 case DIF_OP_LDLS:
8754 case DIF_OP_STGS:
8755 case DIF_OP_STLS:
8756 case DIF_OP_PUSHTR:
8757 case DIF_OP_PUSHTV:
8758 break;
8759
8760 case DIF_OP_LDGS:
8761 if (v >= DIF_VAR_OTHER_UBASE)
8762 break;
8763
8764 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9)
8765 break;
8766
8767 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID ||
8768 v == DIF_VAR_PPID || v == DIF_VAR_TID ||
8769 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME ||
8770 v == DIF_VAR_UID || v == DIF_VAR_GID)
8771 break;
8772
8773 err += efunc(pc, "illegal variable %u\n", v);
8774 break;
8775
8776 case DIF_OP_LDTA:
8777 case DIF_OP_LDTS:
8778 case DIF_OP_LDGAA:
8779 case DIF_OP_LDTAA:
8780 err += efunc(pc, "illegal dynamic variable load\n");
8781 break;
8782
8783 case DIF_OP_STTS:
8784 case DIF_OP_STGAA:
8785 case DIF_OP_STTAA:
8786 err += efunc(pc, "illegal dynamic variable store\n");
8787 break;
8788
8789 case DIF_OP_CALL:
8790 if (subr == DIF_SUBR_ALLOCA ||
8791 subr == DIF_SUBR_BCOPY ||
8792 subr == DIF_SUBR_COPYIN ||
8793 subr == DIF_SUBR_COPYINTO ||
8794 subr == DIF_SUBR_COPYINSTR ||
8795 subr == DIF_SUBR_INDEX ||
8796 subr == DIF_SUBR_INET_NTOA ||
8797 subr == DIF_SUBR_INET_NTOA6 ||
8798 subr == DIF_SUBR_INET_NTOP ||
8799 subr == DIF_SUBR_LLTOSTR ||
8800 subr == DIF_SUBR_RINDEX ||
8801 subr == DIF_SUBR_STRCHR ||
8802 subr == DIF_SUBR_STRJOIN ||
8803 subr == DIF_SUBR_STRRCHR ||
8804 subr == DIF_SUBR_STRSTR ||
8805 subr == DIF_SUBR_HTONS ||
8806 subr == DIF_SUBR_HTONL ||
8807 subr == DIF_SUBR_HTONLL ||
8808 subr == DIF_SUBR_NTOHS ||
8809 subr == DIF_SUBR_NTOHL ||
8810 subr == DIF_SUBR_NTOHLL)
8811 break;
8812
8813 err += efunc(pc, "invalid subr %u\n", subr);
8814 break;
8815
8816 default:
8817 err += efunc(pc, "invalid opcode %u\n",
8818 DIF_INSTR_OP(instr));
8819 }
8820 }
8821
8822 return (err);
8823}
8824#endif /* !VBOX */
8825
8826/*
8827 * Returns 1 if the expression in the DIF object can be cached on a per-thread
8828 * basis; 0 if not.
8829 */
8830static int
8831dtrace_difo_cacheable(dtrace_difo_t *dp)
8832{
8833 VBDTTYPE(uint_t,int) i;
8834
8835 if (dp == NULL)
8836 return (0);
8837
8838 for (i = 0; i < dp->dtdo_varlen; i++) {
8839 dtrace_difv_t *v = &dp->dtdo_vartab[i];
8840
8841 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL)
8842 continue;
8843
8844 switch (v->dtdv_id) {
8845 case DIF_VAR_CURTHREAD:
8846 case DIF_VAR_PID:
8847 case DIF_VAR_TID:
8848 case DIF_VAR_EXECNAME:
8849 case DIF_VAR_ZONENAME:
8850 break;
8851
8852 default:
8853 return (0);
8854 }
8855 }
8856
8857 /*
8858 * This DIF object may be cacheable. Now we need to look for any
8859 * array loading instructions, any memory loading instructions, or
8860 * any stores to thread-local variables.
8861 */
8862 for (i = 0; i < dp->dtdo_len; i++) {
8863 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]);
8864
8865 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) ||
8866 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) ||
8867 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) ||
8868 op == DIF_OP_LDGA || op == DIF_OP_STTS)
8869 return (0);
8870 }
8871
8872 return (1);
8873}
8874
8875static void
8876dtrace_difo_hold(dtrace_difo_t *dp)
8877{
8878#ifndef VBOX
8879 VBDTTYPE(uint_t,int) i;
8880#endif
8881
8882 ASSERT(MUTEX_HELD(&dtrace_lock));
8883
8884 dp->dtdo_refcnt++;
8885 ASSERT(dp->dtdo_refcnt != 0);
8886
8887#ifndef VBOX
8888 /*
8889 * We need to check this DIF object for references to the variable
8890 * DIF_VAR_VTIMESTAMP.
8891 */
8892 for (i = 0; i < dp->dtdo_varlen; i++) {
8893 dtrace_difv_t *v = &dp->dtdo_vartab[i];
8894
8895 if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
8896 continue;
8897
8898 if (dtrace_vtime_references++ == 0)
8899 dtrace_vtime_enable();
8900 }
8901#endif
8902}
8903
8904/*
8905 * This routine calculates the dynamic variable chunksize for a given DIF
8906 * object. The calculation is not fool-proof, and can probably be tricked by
8907 * malicious DIF -- but it works for all compiler-generated DIF. Because this
8908 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
8909 * if a dynamic variable size exceeds the chunksize.
8910 */
8911static void
8912dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
8913{
8914 uint64_t sval VBDTGCC(0);
8915 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
8916 const dif_instr_t *text = dp->dtdo_buf;
8917 uint_t pc, srd = 0;
8918 uint_t ttop = 0;
8919 size_t size, ksize;
8920 uint_t id, i;
8921
8922 for (pc = 0; pc < dp->dtdo_len; pc++) {
8923 dif_instr_t instr = text[pc];
8924 uint_t op = DIF_INSTR_OP(instr);
8925 uint_t rd = DIF_INSTR_RD(instr);
8926 uint_t r1 = DIF_INSTR_R1(instr);
8927 uint_t nkeys = 0;
8928 uchar_t scope VBDTGCC(0);
8929
8930 dtrace_key_t *key = tupregs;
8931
8932 switch (op) {
8933 case DIF_OP_SETX:
8934 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)];
8935 srd = rd;
8936 continue;
8937
8938 case DIF_OP_STTS:
8939 key = &tupregs[DIF_DTR_NREGS];
8940 key[0].dttk_size = 0;
8941 key[1].dttk_size = 0;
8942 nkeys = 2;
8943 scope = DIFV_SCOPE_THREAD;
8944 break;
8945
8946 case DIF_OP_STGAA:
8947 case DIF_OP_STTAA:
8948 nkeys = ttop;
8949
8950 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA)
8951 key[nkeys++].dttk_size = 0;
8952
8953 key[nkeys++].dttk_size = 0;
8954
8955 if (op == DIF_OP_STTAA) {
8956 scope = DIFV_SCOPE_THREAD;
8957 } else {
8958 scope = DIFV_SCOPE_GLOBAL;
8959 }
8960
8961 break;
8962
8963 case DIF_OP_PUSHTR:
8964 if (ttop == DIF_DTR_NREGS)
8965 return;
8966
8967 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) {
8968 /*
8969 * If the register for the size of the "pushtr"
8970 * is %r0 (or the value is 0) and the type is
8971 * a string, we'll use the system-wide default
8972 * string size.
8973 */
8974 tupregs[ttop++].dttk_size =
8975 dtrace_strsize_default;
8976 } else {
8977 if (srd == 0)
8978 return;
8979
8980 tupregs[ttop++].dttk_size = sval;
8981 }
8982
8983 break;
8984
8985 case DIF_OP_PUSHTV:
8986 if (ttop == DIF_DTR_NREGS)
8987 return;
8988
8989 tupregs[ttop++].dttk_size = 0;
8990 break;
8991
8992 case DIF_OP_FLUSHTS:
8993 ttop = 0;
8994 break;
8995
8996 case DIF_OP_POPTS:
8997 if (ttop != 0)
8998 ttop--;
8999 break;
9000 }
9001
9002 sval = 0;
9003 srd = 0;
9004
9005 if (nkeys == 0)
9006 continue;
9007
9008 /*
9009 * We have a dynamic variable allocation; calculate its size.
9010 */
9011 for (ksize = 0, i = 0; i < nkeys; i++)
9012 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
9013
9014 size = sizeof (dtrace_dynvar_t);
9015 size += sizeof (dtrace_key_t) * (nkeys - 1);
9016 size += ksize;
9017
9018 /*
9019 * Now we need to determine the size of the stored data.
9020 */
9021 id = DIF_INSTR_VAR(instr);
9022
9023 for (i = 0; i < dp->dtdo_varlen; i++) {
9024 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9025
9026 if (v->dtdv_id == id && v->dtdv_scope == scope) {
9027 size += v->dtdv_type.dtdt_size;
9028 break;
9029 }
9030 }
9031
9032 if (i == dp->dtdo_varlen)
9033 return;
9034
9035 /*
9036 * We have the size. If this is larger than the chunk size
9037 * for our dynamic variable state, reset the chunk size.
9038 */
9039 size = P2ROUNDUP(size, sizeof (uint64_t));
9040
9041 if (size > vstate->dtvs_dynvars.dtds_chunksize)
9042 vstate->dtvs_dynvars.dtds_chunksize = size;
9043 }
9044}
9045
9046static void
9047dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9048{
9049#ifndef VBOX
9050 int i, oldsvars, osz, nsz, otlocals, ntlocals;
9051#else
9052 int oldsvars, osz, nsz, otlocals, ntlocals;
9053 uint_t i;
9054#endif
9055 uint_t id;
9056
9057 ASSERT(MUTEX_HELD(&dtrace_lock));
9058 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0);
9059
9060 for (i = 0; i < dp->dtdo_varlen; i++) {
9061 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9062 dtrace_statvar_t *svar, ***svarp;
9063 size_t dsize = 0;
9064 uint8_t scope = v->dtdv_scope;
9065 int *np;
9066
9067 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
9068 continue;
9069
9070 id -= DIF_VAR_OTHER_UBASE;
9071
9072 switch (scope) {
9073 case DIFV_SCOPE_THREAD:
9074 while (VBDTCAST(int64_t)id >= (otlocals = vstate->dtvs_ntlocals)) {
9075 dtrace_difv_t *tlocals;
9076
9077 if ((ntlocals = (otlocals << 1)) == 0)
9078 ntlocals = 1;
9079
9080 osz = otlocals * sizeof (dtrace_difv_t);
9081 nsz = ntlocals * sizeof (dtrace_difv_t);
9082
9083 tlocals = kmem_zalloc(nsz, KM_SLEEP);
9084
9085 if (osz != 0) {
9086 bcopy(vstate->dtvs_tlocals,
9087 tlocals, osz);
9088 kmem_free(vstate->dtvs_tlocals, osz);
9089 }
9090
9091 vstate->dtvs_tlocals = tlocals;
9092 vstate->dtvs_ntlocals = ntlocals;
9093 }
9094
9095 vstate->dtvs_tlocals[id] = *v;
9096 continue;
9097
9098 case DIFV_SCOPE_LOCAL:
9099 np = &vstate->dtvs_nlocals;
9100 svarp = &vstate->dtvs_locals;
9101
9102 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
9103 dsize = NCPU * (v->dtdv_type.dtdt_size +
9104 sizeof (uint64_t));
9105 else
9106 dsize = NCPU * sizeof (uint64_t);
9107
9108 break;
9109
9110 case DIFV_SCOPE_GLOBAL:
9111 np = &vstate->dtvs_nglobals;
9112 svarp = &vstate->dtvs_globals;
9113
9114 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
9115 dsize = v->dtdv_type.dtdt_size +
9116 sizeof (uint64_t);
9117
9118 break;
9119
9120 default:
9121#ifndef VBOX
9122 ASSERT(0);
9123#else
9124 AssertFatalMsgFailed(("%d\n", scope));
9125#endif
9126 }
9127
9128 while (VBDTCAST(int64_t)id >= (oldsvars = *np)) {
9129 dtrace_statvar_t **statics;
9130 int newsvars, oldsize, newsize;
9131
9132 if ((newsvars = (oldsvars << 1)) == 0)
9133 newsvars = 1;
9134
9135 oldsize = oldsvars * sizeof (dtrace_statvar_t *);
9136 newsize = newsvars * sizeof (dtrace_statvar_t *);
9137
9138 statics = kmem_zalloc(newsize, KM_SLEEP);
9139
9140 if (oldsize != 0) {
9141 bcopy(*svarp, statics, oldsize);
9142 kmem_free(*svarp, oldsize);
9143 }
9144
9145 *svarp = statics;
9146 *np = newsvars;
9147 }
9148
9149 if ((svar = (*svarp)[id]) == NULL) {
9150 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP);
9151 svar->dtsv_var = *v;
9152
9153 if ((svar->dtsv_size = dsize) != 0) {
9154 svar->dtsv_data = (uint64_t)(uintptr_t)
9155 kmem_zalloc(dsize, KM_SLEEP);
9156 }
9157
9158 (*svarp)[id] = svar;
9159 }
9160
9161 svar->dtsv_refcnt++;
9162 }
9163
9164 dtrace_difo_chunksize(dp, vstate);
9165 dtrace_difo_hold(dp);
9166}
9167
9168#ifndef VBOX
9169static dtrace_difo_t *
9170dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9171{
9172 dtrace_difo_t *new;
9173 size_t sz;
9174
9175 ASSERT(dp->dtdo_buf != NULL);
9176 ASSERT(dp->dtdo_refcnt != 0);
9177
9178 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
9179
9180 ASSERT(dp->dtdo_buf != NULL);
9181 sz = dp->dtdo_len * sizeof (dif_instr_t);
9182 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP);
9183 bcopy(dp->dtdo_buf, new->dtdo_buf, sz);
9184 new->dtdo_len = dp->dtdo_len;
9185
9186 if (dp->dtdo_strtab != NULL) {
9187 ASSERT(dp->dtdo_strlen != 0);
9188 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP);
9189 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen);
9190 new->dtdo_strlen = dp->dtdo_strlen;
9191 }
9192
9193 if (dp->dtdo_inttab != NULL) {
9194 ASSERT(dp->dtdo_intlen != 0);
9195 sz = dp->dtdo_intlen * sizeof (uint64_t);
9196 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP);
9197 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz);
9198 new->dtdo_intlen = dp->dtdo_intlen;
9199 }
9200
9201 if (dp->dtdo_vartab != NULL) {
9202 ASSERT(dp->dtdo_varlen != 0);
9203 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t);
9204 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP);
9205 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz);
9206 new->dtdo_varlen = dp->dtdo_varlen;
9207 }
9208
9209 dtrace_difo_init(new, vstate);
9210 return (new);
9211}
9212#endif
9213
9214static void
9215dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9216{
9217 VBDTTYPE(uint_t,int) i;
9218
9219 ASSERT(dp->dtdo_refcnt == 0);
9220
9221 for (i = 0; i < dp->dtdo_varlen; i++) {
9222 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9223 dtrace_statvar_t *svar, **svarp;
9224 uint_t id;
9225 uint8_t scope = v->dtdv_scope;
9226 int *np;
9227
9228 switch (scope) {
9229 case DIFV_SCOPE_THREAD:
9230 continue;
9231
9232 case DIFV_SCOPE_LOCAL:
9233 np = &vstate->dtvs_nlocals;
9234 svarp = vstate->dtvs_locals;
9235 break;
9236
9237 case DIFV_SCOPE_GLOBAL:
9238 np = &vstate->dtvs_nglobals;
9239 svarp = vstate->dtvs_globals;
9240 break;
9241
9242 default:
9243#ifndef VBOX
9244 ASSERT(0);
9245#else
9246 AssertFatalMsgFailed(("%d\n", scope));
9247#endif
9248 }
9249
9250 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
9251 continue;
9252
9253 id -= DIF_VAR_OTHER_UBASE;
9254 ASSERT(VBDTCAST(int64_t)id < *np);
9255
9256 svar = svarp[id];
9257 ASSERT(svar != NULL);
9258 ASSERT(svar->dtsv_refcnt > 0);
9259
9260 if (--svar->dtsv_refcnt > 0)
9261 continue;
9262
9263 if (svar->dtsv_size != 0) {
9264 ASSERT(svar->dtsv_data != NULL);
9265 kmem_free((void *)(uintptr_t)svar->dtsv_data,
9266 svar->dtsv_size);
9267 }
9268
9269 kmem_free(svar, sizeof (dtrace_statvar_t));
9270 svarp[id] = NULL;
9271 }
9272
9273 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
9274 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
9275 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
9276 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
9277
9278 kmem_free(dp, sizeof (dtrace_difo_t));
9279}
9280
9281static void
9282dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9283{
9284#ifndef VBOX
9285 VBDTTYPE(uint_t,int) i;
9286#endif
9287
9288 ASSERT(MUTEX_HELD(&dtrace_lock));
9289 ASSERT(dp->dtdo_refcnt != 0);
9290
9291#ifndef VBOX
9292 for (i = 0; i < dp->dtdo_varlen; i++) {
9293 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9294
9295 if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
9296 continue;
9297
9298 ASSERT(dtrace_vtime_references > 0);
9299 if (--dtrace_vtime_references == 0)
9300 dtrace_vtime_disable();
9301 }
9302#endif
9303
9304 if (--dp->dtdo_refcnt == 0)
9305 dtrace_difo_destroy(dp, vstate);
9306}
9307
9308/*
9309 * DTrace Format Functions
9310 */
9311static uint16_t
9312dtrace_format_add(dtrace_state_t *state, char *str)
9313{
9314 char *fmt, **new;
9315 uint16_t ndx, len = VBDTCAST(uint16_t)strlen(str) + 1;
9316
9317 fmt = kmem_zalloc(len, KM_SLEEP);
9318 bcopy(str, fmt, len);
9319
9320 for (ndx = 0; ndx < state->dts_nformats; ndx++) {
9321 if (state->dts_formats[ndx] == NULL) {
9322 state->dts_formats[ndx] = fmt;
9323 return (ndx + 1);
9324 }
9325 }
9326
9327 if (state->dts_nformats == USHRT_MAX) {
9328 /*
9329 * This is only likely if a denial-of-service attack is being
9330 * attempted. As such, it's okay to fail silently here.
9331 */
9332 kmem_free(fmt, len);
9333 return (0);
9334 }
9335
9336 /*
9337 * For simplicity, we always resize the formats array to be exactly the
9338 * number of formats.
9339 */
9340 ndx = state->dts_nformats++;
9341 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP);
9342
9343 if (state->dts_formats != NULL) {
9344 ASSERT(ndx != 0);
9345 bcopy(state->dts_formats, new, ndx * sizeof (char *));
9346 kmem_free(state->dts_formats, ndx * sizeof (char *));
9347 }
9348
9349 state->dts_formats = new;
9350 state->dts_formats[ndx] = fmt;
9351
9352 return (ndx + 1);
9353}
9354
9355static void
9356dtrace_format_remove(dtrace_state_t *state, uint16_t format)
9357{
9358 char *fmt;
9359
9360 ASSERT(state->dts_formats != NULL);
9361 ASSERT(format <= state->dts_nformats);
9362 ASSERT(state->dts_formats[format - 1] != NULL);
9363
9364 fmt = state->dts_formats[format - 1];
9365 kmem_free(fmt, strlen(fmt) + 1);
9366 state->dts_formats[format - 1] = NULL;
9367}
9368
9369static void
9370dtrace_format_destroy(dtrace_state_t *state)
9371{
9372 int i;
9373
9374 if (state->dts_nformats == 0) {
9375 ASSERT(state->dts_formats == NULL);
9376 return;
9377 }
9378
9379 ASSERT(state->dts_formats != NULL);
9380
9381 for (i = 0; i < state->dts_nformats; i++) {
9382 char *fmt = state->dts_formats[i];
9383
9384 if (fmt == NULL)
9385 continue;
9386
9387 kmem_free(fmt, strlen(fmt) + 1);
9388 }
9389
9390 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *));
9391 state->dts_nformats = 0;
9392 state->dts_formats = NULL;
9393}
9394
9395/*
9396 * DTrace Predicate Functions
9397 */
9398static dtrace_predicate_t *
9399dtrace_predicate_create(dtrace_difo_t *dp)
9400{
9401 dtrace_predicate_t *pred;
9402
9403 ASSERT(MUTEX_HELD(&dtrace_lock));
9404 ASSERT(dp->dtdo_refcnt != 0);
9405
9406 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP);
9407 pred->dtp_difo = dp;
9408 pred->dtp_refcnt = 1;
9409
9410 if (!dtrace_difo_cacheable(dp))
9411 return (pred);
9412
9413 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) {
9414 /*
9415 * This is only theoretically possible -- we have had 2^32
9416 * cacheable predicates on this machine. We cannot allow any
9417 * more predicates to become cacheable: as unlikely as it is,
9418 * there may be a thread caching a (now stale) predicate cache
9419 * ID. (N.B.: the temptation is being successfully resisted to
9420 * have this cmn_err() "Holy shit -- we executed this code!")
9421 */
9422 return (pred);
9423 }
9424
9425 pred->dtp_cacheid = dtrace_predcache_id++;
9426
9427 return (pred);
9428}
9429
9430static void
9431dtrace_predicate_hold(dtrace_predicate_t *pred)
9432{
9433 ASSERT(MUTEX_HELD(&dtrace_lock));
9434 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0);
9435 ASSERT(pred->dtp_refcnt > 0);
9436
9437 pred->dtp_refcnt++;
9438}
9439
9440static void
9441dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate)
9442{
9443#ifdef VBOX_STRICT
9444 dtrace_difo_t *dp = pred->dtp_difo;
9445#endif
9446
9447 ASSERT(MUTEX_HELD(&dtrace_lock));
9448 ASSERT(dp != NULL && dp->dtdo_refcnt != 0);
9449 ASSERT(pred->dtp_refcnt > 0);
9450
9451 if (--pred->dtp_refcnt == 0) {
9452 dtrace_difo_release(pred->dtp_difo, vstate);
9453 kmem_free(pred, sizeof (dtrace_predicate_t));
9454 }
9455}
9456
9457/*
9458 * DTrace Action Description Functions
9459 */
9460static dtrace_actdesc_t *
9461dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple,
9462 uint64_t uarg, uint64_t arg)
9463{
9464 dtrace_actdesc_t *act;
9465
9466 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL &&
9467 VBDT_IS_VALID_KRNL_ADDR(arg)) || (arg == NULL && kind == DTRACEACT_PRINTA));
9468
9469 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP);
9470 act->dtad_kind = kind;
9471 act->dtad_ntuple = ntuple;
9472 act->dtad_uarg = uarg;
9473 act->dtad_arg = arg;
9474 act->dtad_refcnt = 1;
9475
9476 return (act);
9477}
9478
9479static void
9480dtrace_actdesc_hold(dtrace_actdesc_t *act)
9481{
9482 ASSERT(act->dtad_refcnt >= 1);
9483 act->dtad_refcnt++;
9484}
9485
9486static void
9487dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate)
9488{
9489 dtrace_actkind_t kind = act->dtad_kind;
9490 dtrace_difo_t *dp;
9491
9492 ASSERT(act->dtad_refcnt >= 1);
9493
9494 if (--act->dtad_refcnt != 0)
9495 return;
9496
9497 if ((dp = act->dtad_difo) != NULL)
9498 dtrace_difo_release(dp, vstate);
9499
9500 if (DTRACEACT_ISPRINTFLIKE(kind)) {
9501 char *str = (char *)(uintptr_t)act->dtad_arg;
9502
9503 ASSERT((str != NULL && VBDT_IS_VALID_KRNL_ADDR((uintptr_t)str)) ||
9504 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA));
9505
9506 if (str != NULL)
9507 kmem_free(str, strlen(str) + 1);
9508 }
9509
9510 kmem_free(act, sizeof (dtrace_actdesc_t));
9511}
9512
9513/*
9514 * DTrace ECB Functions
9515 */
9516static dtrace_ecb_t *
9517dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe)
9518{
9519 dtrace_ecb_t *ecb;
9520 dtrace_epid_t epid;
9521
9522 ASSERT(MUTEX_HELD(&dtrace_lock));
9523
9524 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP);
9525 ecb->dte_predicate = NULL;
9526 ecb->dte_probe = probe;
9527
9528 /*
9529 * The default size is the size of the default action: recording
9530 * the epid.
9531 */
9532 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t);
9533 ecb->dte_alignment = sizeof (dtrace_epid_t);
9534
9535 epid = state->dts_epid++;
9536
9537 if (VBDTCAST(int64_t)epid - 1 >= state->dts_necbs) {
9538 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs;
9539 int necbs = state->dts_necbs << 1;
9540
9541 ASSERT(epid == VBDTCAST(dtrace_epid_t)state->dts_necbs + 1);
9542
9543 if (necbs == 0) {
9544 ASSERT(oecbs == NULL);
9545 necbs = 1;
9546 }
9547
9548 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP);
9549
9550 if (oecbs != NULL)
9551 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs));
9552
9553 dtrace_membar_producer();
9554 state->dts_ecbs = ecbs;
9555
9556 if (oecbs != NULL) {
9557 /*
9558 * If this state is active, we must dtrace_sync()
9559 * before we can free the old dts_ecbs array: we're
9560 * coming in hot, and there may be active ring
9561 * buffer processing (which indexes into the dts_ecbs
9562 * array) on another CPU.
9563 */
9564 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
9565 dtrace_sync();
9566
9567 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs));
9568 }
9569
9570 dtrace_membar_producer();
9571 state->dts_necbs = necbs;
9572 }
9573
9574 ecb->dte_state = state;
9575
9576 ASSERT(state->dts_ecbs[epid - 1] == NULL);
9577 dtrace_membar_producer();
9578 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb;
9579
9580 return (ecb);
9581}
9582
9583static int
9584dtrace_ecb_enable(dtrace_ecb_t *ecb)
9585{
9586 dtrace_probe_t *probe = ecb->dte_probe;
9587
9588 ASSERT(MUTEX_HELD(&cpu_lock));
9589 ASSERT(MUTEX_HELD(&dtrace_lock));
9590 ASSERT(ecb->dte_next == NULL);
9591
9592 if (probe == NULL) {
9593 /*
9594 * This is the NULL probe -- there's nothing to do.
9595 */
9596 return (0);
9597 }
9598
9599 if (probe->dtpr_ecb == NULL) {
9600 dtrace_provider_t *prov = probe->dtpr_provider;
9601
9602 /*
9603 * We're the first ECB on this probe.
9604 */
9605 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb;
9606
9607 if (ecb->dte_predicate != NULL)
9608 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid;
9609
9610 return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg,
9611 probe->dtpr_id, probe->dtpr_arg));
9612 } else {
9613 /*
9614 * This probe is already active. Swing the last pointer to
9615 * point to the new ECB, and issue a dtrace_sync() to assure
9616 * that all CPUs have seen the change.
9617 */
9618 ASSERT(probe->dtpr_ecb_last != NULL);
9619 probe->dtpr_ecb_last->dte_next = ecb;
9620 probe->dtpr_ecb_last = ecb;
9621 probe->dtpr_predcache = 0;
9622
9623 dtrace_sync();
9624 return (0);
9625 }
9626}
9627
9628static void
9629dtrace_ecb_resize(dtrace_ecb_t *ecb)
9630{
9631 uint32_t maxalign = sizeof (dtrace_epid_t);
9632 uint32_t align = sizeof (uint8_t), offs, diff;
9633 dtrace_action_t *act;
9634 int wastuple = 0;
9635 uint32_t aggbase = UINT32_MAX;
9636 dtrace_state_t *state = ecb->dte_state;
9637
9638 /*
9639 * If we record anything, we always record the epid. (And we always
9640 * record it first.)
9641 */
9642 offs = sizeof (dtrace_epid_t);
9643 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t);
9644
9645 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
9646 dtrace_recdesc_t *rec = &act->dta_rec;
9647
9648 if ((align = rec->dtrd_alignment) > maxalign)
9649 maxalign = align;
9650
9651 if (!wastuple && act->dta_intuple) {
9652 /*
9653 * This is the first record in a tuple. Align the
9654 * offset to be at offset 4 in an 8-byte aligned
9655 * block.
9656 */
9657 diff = offs + sizeof (dtrace_aggid_t);
9658
9659 if ((diff = (diff & (sizeof (uint64_t) - 1))))
9660 offs += sizeof (uint64_t) - diff;
9661
9662 aggbase = offs - sizeof (dtrace_aggid_t);
9663 ASSERT(!(aggbase & (sizeof (uint64_t) - 1)));
9664 }
9665
9666 /*LINTED*/
9667 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) {
9668 /*
9669 * The current offset is not properly aligned; align it.
9670 */
9671 offs += align - diff;
9672 }
9673
9674 rec->dtrd_offset = offs;
9675
9676 if (offs + rec->dtrd_size > ecb->dte_needed) {
9677 ecb->dte_needed = offs + rec->dtrd_size;
9678
9679 if (ecb->dte_needed > state->dts_needed)
9680 state->dts_needed = ecb->dte_needed;
9681 }
9682
9683 if (DTRACEACT_ISAGG(act->dta_kind)) {
9684 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
9685 dtrace_action_t *first = agg->dtag_first, *prev;
9686
9687 ASSERT(rec->dtrd_size != 0 && first != NULL);
9688 ASSERT(wastuple);
9689 ASSERT(aggbase != UINT32_MAX);
9690
9691 agg->dtag_base = aggbase;
9692
9693 while ((prev = first->dta_prev) != NULL &&
9694 DTRACEACT_ISAGG(prev->dta_kind)) {
9695 agg = (dtrace_aggregation_t *)prev;
9696 first = agg->dtag_first;
9697 }
9698
9699 if (prev != NULL) {
9700 offs = prev->dta_rec.dtrd_offset +
9701 prev->dta_rec.dtrd_size;
9702 } else {
9703 offs = sizeof (dtrace_epid_t);
9704 }
9705 wastuple = 0;
9706 } else {
9707 if (!act->dta_intuple)
9708 ecb->dte_size = offs + rec->dtrd_size;
9709
9710 offs += rec->dtrd_size;
9711 }
9712
9713 wastuple = act->dta_intuple;
9714 }
9715
9716 if ((act = ecb->dte_action) != NULL &&
9717 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) &&
9718 ecb->dte_size == sizeof (dtrace_epid_t)) {
9719 /*
9720 * If the size is still sizeof (dtrace_epid_t), then all
9721 * actions store no data; set the size to 0.
9722 */
9723 ecb->dte_alignment = maxalign;
9724 ecb->dte_size = 0;
9725
9726 /*
9727 * If the needed space is still sizeof (dtrace_epid_t), then
9728 * all actions need no additional space; set the needed
9729 * size to 0.
9730 */
9731 if (ecb->dte_needed == sizeof (dtrace_epid_t))
9732 ecb->dte_needed = 0;
9733
9734 return;
9735 }
9736
9737 /*
9738 * Set our alignment, and make sure that the dte_size and dte_needed
9739 * are aligned to the size of an EPID.
9740 */
9741 ecb->dte_alignment = maxalign;
9742 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) &
9743 ~(sizeof (dtrace_epid_t) - 1);
9744 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) &
9745 ~(sizeof (dtrace_epid_t) - 1);
9746 ASSERT(ecb->dte_size <= ecb->dte_needed);
9747}
9748
9749static dtrace_action_t *
9750dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
9751{
9752 dtrace_aggregation_t *agg;
9753 size_t size = sizeof (uint64_t);
9754 int ntuple = desc->dtad_ntuple;
9755 dtrace_action_t *act;
9756 dtrace_recdesc_t *frec;
9757 dtrace_aggid_t aggid;
9758 dtrace_state_t *state = ecb->dte_state;
9759
9760 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP);
9761 agg->dtag_ecb = ecb;
9762
9763 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind));
9764
9765 switch (desc->dtad_kind) {
9766 case DTRACEAGG_MIN:
9767 agg->dtag_initial = INT64_MAX;
9768 agg->dtag_aggregate = dtrace_aggregate_min;
9769 break;
9770
9771 case DTRACEAGG_MAX:
9772 agg->dtag_initial = (uint64_t)INT64_MIN;
9773 agg->dtag_aggregate = dtrace_aggregate_max;
9774 break;
9775
9776 case DTRACEAGG_COUNT:
9777 agg->dtag_aggregate = dtrace_aggregate_count;
9778 break;
9779
9780 case DTRACEAGG_QUANTIZE:
9781 agg->dtag_aggregate = dtrace_aggregate_quantize;
9782 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) *
9783 sizeof (uint64_t);
9784 break;
9785
9786 case DTRACEAGG_LQUANTIZE: {
9787 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg);
9788 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg);
9789
9790 agg->dtag_initial = desc->dtad_arg;
9791 agg->dtag_aggregate = dtrace_aggregate_lquantize;
9792
9793 if (step == 0 || levels == 0)
9794 goto err;
9795
9796 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t);
9797 break;
9798 }
9799
9800 case DTRACEAGG_AVG:
9801 agg->dtag_aggregate = dtrace_aggregate_avg;
9802 size = sizeof (uint64_t) * 2;
9803 break;
9804
9805 case DTRACEAGG_STDDEV:
9806 agg->dtag_aggregate = dtrace_aggregate_stddev;
9807 size = sizeof (uint64_t) * 4;
9808 break;
9809
9810 case DTRACEAGG_SUM:
9811 agg->dtag_aggregate = dtrace_aggregate_sum;
9812 break;
9813
9814 default:
9815 goto err;
9816 }
9817
9818 agg->dtag_action.dta_rec.dtrd_size = VBDTCAST(uint32_t)size;
9819
9820 if (ntuple == 0)
9821 goto err;
9822
9823 /*
9824 * We must make sure that we have enough actions for the n-tuple.
9825 */
9826 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) {
9827 if (DTRACEACT_ISAGG(act->dta_kind))
9828 break;
9829
9830 if (--ntuple == 0) {
9831 /*
9832 * This is the action with which our n-tuple begins.
9833 */
9834 agg->dtag_first = act;
9835 goto success;
9836 }
9837 }
9838
9839 /*
9840 * This n-tuple is short by ntuple elements. Return failure.
9841 */
9842 ASSERT(ntuple != 0);
9843err:
9844 kmem_free(agg, sizeof (dtrace_aggregation_t));
9845 return (NULL);
9846
9847success:
9848 /*
9849 * If the last action in the tuple has a size of zero, it's actually
9850 * an expression argument for the aggregating action.
9851 */
9852 ASSERT(ecb->dte_action_last != NULL);
9853 act = ecb->dte_action_last;
9854
9855 if (act->dta_kind == DTRACEACT_DIFEXPR) {
9856 ASSERT(act->dta_difo != NULL);
9857
9858 if (act->dta_difo->dtdo_rtype.dtdt_size == 0)
9859 agg->dtag_hasarg = 1;
9860 }
9861
9862 /*
9863 * We need to allocate an id for this aggregation.
9864 */
9865 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1,
9866 VM_BESTFIT | VM_SLEEP);
9867
9868 if (VBDTCAST(int64_t)aggid - 1 >= state->dts_naggregations) {
9869 dtrace_aggregation_t **oaggs = state->dts_aggregations;
9870 dtrace_aggregation_t **aggs;
9871 int naggs = state->dts_naggregations << 1;
9872 int onaggs = state->dts_naggregations;
9873
9874 ASSERT(aggid == VBDTCAST(dtrace_aggid_t)state->dts_naggregations + 1);
9875
9876 if (naggs == 0) {
9877 ASSERT(oaggs == NULL);
9878 naggs = 1;
9879 }
9880
9881 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP);
9882
9883 if (oaggs != NULL) {
9884 bcopy(oaggs, aggs, onaggs * sizeof (*aggs));
9885 kmem_free(oaggs, onaggs * sizeof (*aggs));
9886 }
9887
9888 state->dts_aggregations = aggs;
9889 state->dts_naggregations = naggs;
9890 }
9891
9892 ASSERT(state->dts_aggregations[aggid - 1] == NULL);
9893 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg;
9894
9895 frec = &agg->dtag_first->dta_rec;
9896 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t))
9897 frec->dtrd_alignment = sizeof (dtrace_aggid_t);
9898
9899 for (act = agg->dtag_first; act != NULL; act = act->dta_next) {
9900 ASSERT(!act->dta_intuple);
9901 act->dta_intuple = 1;
9902 }
9903
9904 return (&agg->dtag_action);
9905}
9906
9907static void
9908dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act)
9909{
9910 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
9911 dtrace_state_t *state = ecb->dte_state;
9912 dtrace_aggid_t aggid = agg->dtag_id;
9913
9914 ASSERT(DTRACEACT_ISAGG(act->dta_kind));
9915 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1);
9916
9917 ASSERT(state->dts_aggregations[aggid - 1] == agg);
9918 state->dts_aggregations[aggid - 1] = NULL;
9919
9920 kmem_free(agg, sizeof (dtrace_aggregation_t));
9921}
9922
9923static int
9924dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
9925{
9926 dtrace_action_t *action, *last;
9927 dtrace_difo_t *dp = desc->dtad_difo;
9928 uint32_t size = 0, align = sizeof (uint8_t), mask;
9929 uint16_t format = 0;
9930 dtrace_recdesc_t *rec;
9931 dtrace_state_t *state = ecb->dte_state;
9932 dtrace_optval_t *opt = state->dts_options, nframes VBDTUNASS(0), strsize;
9933 uint64_t arg = desc->dtad_arg;
9934
9935 ASSERT(MUTEX_HELD(&dtrace_lock));
9936 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1);
9937
9938 if (DTRACEACT_ISAGG(desc->dtad_kind)) {
9939 /*
9940 * If this is an aggregating action, there must be neither
9941 * a speculate nor a commit on the action chain.
9942 */
9943 dtrace_action_t *act;
9944
9945 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
9946 if (act->dta_kind == DTRACEACT_COMMIT)
9947 return (EINVAL);
9948
9949 if (act->dta_kind == DTRACEACT_SPECULATE)
9950 return (EINVAL);
9951 }
9952
9953 action = dtrace_ecb_aggregation_create(ecb, desc);
9954
9955 if (action == NULL)
9956 return (EINVAL);
9957 } else {
9958 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) ||
9959 (desc->dtad_kind == DTRACEACT_DIFEXPR &&
9960 dp != NULL && dp->dtdo_destructive)) {
9961 state->dts_destructive = 1;
9962 }
9963
9964 switch (desc->dtad_kind) {
9965 case DTRACEACT_PRINTF:
9966 case DTRACEACT_PRINTA:
9967 case DTRACEACT_SYSTEM:
9968 case DTRACEACT_FREOPEN:
9969 /*
9970 * We know that our arg is a string -- turn it into a
9971 * format.
9972 */
9973 if (arg == NULL) {
9974 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA);
9975 format = 0;
9976 } else {
9977 ASSERT(arg != NULL);
9978 ASSERT(VBDT_IS_VALID_KRNL_ADDR(arg));
9979 format = dtrace_format_add(state,
9980 (char *)(uintptr_t)arg);
9981 }
9982
9983 RT_FALL_THRU();
9984 case DTRACEACT_LIBACT:
9985 case DTRACEACT_DIFEXPR:
9986 if (dp == NULL)
9987 return (EINVAL);
9988
9989 if ((size = dp->dtdo_rtype.dtdt_size) != 0)
9990 break;
9991
9992 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
9993 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
9994 return (EINVAL);
9995
9996 size = opt[DTRACEOPT_STRSIZE];
9997 }
9998
9999 break;
10000
10001 case DTRACEACT_STACK:
10002 if ((nframes = arg) == 0) {
10003 nframes = opt[DTRACEOPT_STACKFRAMES];
10004 ASSERT(nframes > 0);
10005 arg = nframes;
10006 }
10007
10008 size = VBDTCAST(uint32_t)(nframes * sizeof (pc_t));
10009 break;
10010
10011 case DTRACEACT_JSTACK:
10012 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0)
10013 strsize = opt[DTRACEOPT_JSTACKSTRSIZE];
10014
10015 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0)
10016 nframes = opt[DTRACEOPT_JSTACKFRAMES];
10017
10018 arg = DTRACE_USTACK_ARG(nframes, strsize);
10019
10020 RT_FALL_THRU();
10021 case DTRACEACT_USTACK:
10022 if (desc->dtad_kind != DTRACEACT_JSTACK &&
10023 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) {
10024 strsize = DTRACE_USTACK_STRSIZE(arg);
10025 nframes = opt[DTRACEOPT_USTACKFRAMES];
10026 ASSERT(nframes > 0);
10027 arg = DTRACE_USTACK_ARG(nframes, strsize);
10028 }
10029
10030 /*
10031 * Save a slot for the pid.
10032 */
10033 size = VBDTCAST(uint32_t)((nframes + 1) * sizeof (uint64_t));
10034 size += DTRACE_USTACK_STRSIZE(arg);
10035 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t)));
10036
10037 break;
10038
10039 case DTRACEACT_SYM:
10040 case DTRACEACT_MOD:
10041 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) !=
10042 sizeof (uint64_t)) ||
10043 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10044 return (EINVAL);
10045 break;
10046
10047 case DTRACEACT_USYM:
10048 case DTRACEACT_UMOD:
10049 case DTRACEACT_UADDR:
10050 if (dp == NULL ||
10051 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) ||
10052 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10053 return (EINVAL);
10054
10055 /*
10056 * We have a slot for the pid, plus a slot for the
10057 * argument. To keep things simple (aligned with
10058 * bitness-neutral sizing), we store each as a 64-bit
10059 * quantity.
10060 */
10061 size = 2 * sizeof (uint64_t);
10062 break;
10063
10064 case DTRACEACT_STOP:
10065 case DTRACEACT_BREAKPOINT:
10066 case DTRACEACT_PANIC:
10067 break;
10068
10069 case DTRACEACT_CHILL:
10070 case DTRACEACT_DISCARD:
10071 case DTRACEACT_RAISE:
10072 if (dp == NULL)
10073 return (EINVAL);
10074 break;
10075
10076 case DTRACEACT_EXIT:
10077 if (dp == NULL ||
10078 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) ||
10079 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10080 return (EINVAL);
10081 break;
10082
10083 case DTRACEACT_SPECULATE:
10084 if (ecb->dte_size > sizeof (dtrace_epid_t))
10085 return (EINVAL);
10086
10087 if (dp == NULL)
10088 return (EINVAL);
10089
10090 state->dts_speculates = 1;
10091 break;
10092
10093 case DTRACEACT_COMMIT: {
10094 dtrace_action_t *act = ecb->dte_action;
10095
10096 for (; act != NULL; act = act->dta_next) {
10097 if (act->dta_kind == DTRACEACT_COMMIT)
10098 return (EINVAL);
10099 }
10100
10101 if (dp == NULL)
10102 return (EINVAL);
10103 break;
10104 }
10105
10106 default:
10107 return (EINVAL);
10108 }
10109
10110 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) {
10111 /*
10112 * If this is a data-storing action or a speculate,
10113 * we must be sure that there isn't a commit on the
10114 * action chain.
10115 */
10116 dtrace_action_t *act = ecb->dte_action;
10117
10118 for (; act != NULL; act = act->dta_next) {
10119 if (act->dta_kind == DTRACEACT_COMMIT)
10120 return (EINVAL);
10121 }
10122 }
10123
10124 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP);
10125 action->dta_rec.dtrd_size = size;
10126 }
10127
10128 action->dta_refcnt = 1;
10129 rec = &action->dta_rec;
10130 size = rec->dtrd_size;
10131
10132 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) {
10133 if (!(size & mask)) {
10134 align = mask + 1;
10135 break;
10136 }
10137 }
10138
10139 action->dta_kind = desc->dtad_kind;
10140
10141 if ((action->dta_difo = dp) != NULL)
10142 dtrace_difo_hold(dp);
10143
10144 rec->dtrd_action = action->dta_kind;
10145 rec->dtrd_arg = arg;
10146 rec->dtrd_uarg = desc->dtad_uarg;
10147 rec->dtrd_alignment = (uint16_t)align;
10148 rec->dtrd_format = format;
10149
10150 if ((last = ecb->dte_action_last) != NULL) {
10151 ASSERT(ecb->dte_action != NULL);
10152 action->dta_prev = last;
10153 last->dta_next = action;
10154 } else {
10155 ASSERT(ecb->dte_action == NULL);
10156 ecb->dte_action = action;
10157 }
10158
10159 ecb->dte_action_last = action;
10160
10161 return (0);
10162}
10163
10164static void
10165dtrace_ecb_action_remove(dtrace_ecb_t *ecb)
10166{
10167 dtrace_action_t *act = ecb->dte_action, *next;
10168 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate;
10169 dtrace_difo_t *dp;
10170 uint16_t format;
10171
10172 if (act != NULL && act->dta_refcnt > 1) {
10173 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1);
10174 act->dta_refcnt--;
10175 } else {
10176 for (; act != NULL; act = next) {
10177 next = act->dta_next;
10178 ASSERT(next != NULL || act == ecb->dte_action_last);
10179 ASSERT(act->dta_refcnt == 1);
10180
10181 if ((format = act->dta_rec.dtrd_format) != 0)
10182 dtrace_format_remove(ecb->dte_state, format);
10183
10184 if ((dp = act->dta_difo) != NULL)
10185 dtrace_difo_release(dp, vstate);
10186
10187 if (DTRACEACT_ISAGG(act->dta_kind)) {
10188 dtrace_ecb_aggregation_destroy(ecb, act);
10189 } else {
10190 kmem_free(act, sizeof (dtrace_action_t));
10191 }
10192 }
10193 }
10194
10195 ecb->dte_action = NULL;
10196 ecb->dte_action_last = NULL;
10197 ecb->dte_size = sizeof (dtrace_epid_t);
10198}
10199
10200static void
10201dtrace_ecb_disable(dtrace_ecb_t *ecb)
10202{
10203 /*
10204 * We disable the ECB by removing it from its probe.
10205 */
10206 dtrace_ecb_t *pecb, *prev = NULL;
10207 dtrace_probe_t *probe = ecb->dte_probe;
10208
10209 ASSERT(MUTEX_HELD(&dtrace_lock));
10210
10211 if (probe == NULL) {
10212 /*
10213 * This is the NULL probe; there is nothing to disable.
10214 */
10215 return;
10216 }
10217
10218 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) {
10219 if (pecb == ecb)
10220 break;
10221 prev = pecb;
10222 }
10223
10224 ASSERT(pecb != NULL);
10225
10226 if (prev == NULL) {
10227 probe->dtpr_ecb = ecb->dte_next;
10228 } else {
10229 prev->dte_next = ecb->dte_next;
10230 }
10231
10232 if (ecb == probe->dtpr_ecb_last) {
10233 ASSERT(ecb->dte_next == NULL);
10234 probe->dtpr_ecb_last = prev;
10235 }
10236
10237 /*
10238 * The ECB has been disconnected from the probe; now sync to assure
10239 * that all CPUs have seen the change before returning.
10240 */
10241 dtrace_sync();
10242
10243 if (probe->dtpr_ecb == NULL) {
10244 /*
10245 * That was the last ECB on the probe; clear the predicate
10246 * cache ID for the probe, disable it and sync one more time
10247 * to assure that we'll never hit it again.
10248 */
10249 dtrace_provider_t *prov = probe->dtpr_provider;
10250
10251 ASSERT(ecb->dte_next == NULL);
10252 ASSERT(probe->dtpr_ecb_last == NULL);
10253 probe->dtpr_predcache = DTRACE_CACHEIDNONE;
10254 prov->dtpv_pops.dtps_disable(prov->dtpv_arg,
10255 probe->dtpr_id, probe->dtpr_arg);
10256 dtrace_sync();
10257 } else {
10258 /*
10259 * There is at least one ECB remaining on the probe. If there
10260 * is _exactly_ one, set the probe's predicate cache ID to be
10261 * the predicate cache ID of the remaining ECB.
10262 */
10263 ASSERT(probe->dtpr_ecb_last != NULL);
10264 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE);
10265
10266 if (probe->dtpr_ecb == probe->dtpr_ecb_last) {
10267 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate;
10268
10269 ASSERT(probe->dtpr_ecb->dte_next == NULL);
10270
10271 if (p != NULL)
10272 probe->dtpr_predcache = p->dtp_cacheid;
10273 }
10274
10275 ecb->dte_next = NULL;
10276 }
10277}
10278
10279static void
10280dtrace_ecb_destroy(dtrace_ecb_t *ecb)
10281{
10282 dtrace_state_t *state = ecb->dte_state;
10283 dtrace_vstate_t *vstate = &state->dts_vstate;
10284 dtrace_predicate_t *pred;
10285 dtrace_epid_t epid = ecb->dte_epid;
10286
10287 ASSERT(MUTEX_HELD(&dtrace_lock));
10288 ASSERT(ecb->dte_next == NULL);
10289 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb);
10290
10291 if ((pred = ecb->dte_predicate) != NULL)
10292 dtrace_predicate_release(pred, vstate);
10293
10294 dtrace_ecb_action_remove(ecb);
10295
10296 ASSERT(state->dts_ecbs[epid - 1] == ecb);
10297 state->dts_ecbs[epid - 1] = NULL;
10298
10299 kmem_free(ecb, sizeof (dtrace_ecb_t));
10300}
10301
10302static dtrace_ecb_t *
10303dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe,
10304 dtrace_enabling_t *enab)
10305{
10306 dtrace_ecb_t *ecb;
10307 dtrace_predicate_t *pred;
10308 dtrace_actdesc_t *act;
10309 dtrace_provider_t *prov;
10310 dtrace_ecbdesc_t *desc = enab->dten_current;
10311
10312 ASSERT(MUTEX_HELD(&dtrace_lock));
10313 ASSERT(state != NULL);
10314
10315 ecb = dtrace_ecb_add(state, probe);
10316 ecb->dte_uarg = desc->dted_uarg;
10317
10318 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) {
10319 dtrace_predicate_hold(pred);
10320 ecb->dte_predicate = pred;
10321 }
10322
10323 if (probe != NULL) {
10324 /*
10325 * If the provider shows more leg than the consumer is old
10326 * enough to see, we need to enable the appropriate implicit
10327 * predicate bits to prevent the ecb from activating at
10328 * revealing times.
10329 *
10330 * Providers specifying DTRACE_PRIV_USER at register time
10331 * are stating that they need the /proc-style privilege
10332 * model to be enforced, and this is what DTRACE_COND_OWNER
10333 * and DTRACE_COND_ZONEOWNER will then do at probe time.
10334 */
10335 prov = probe->dtpr_provider;
10336 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) &&
10337 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
10338 ecb->dte_cond |= DTRACE_COND_OWNER;
10339
10340 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) &&
10341 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
10342 ecb->dte_cond |= DTRACE_COND_ZONEOWNER;
10343
10344 /*
10345 * If the provider shows us kernel innards and the user
10346 * is lacking sufficient privilege, enable the
10347 * DTRACE_COND_USERMODE implicit predicate.
10348 */
10349 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) &&
10350 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL))
10351 ecb->dte_cond |= DTRACE_COND_USERMODE;
10352 }
10353
10354 if (dtrace_ecb_create_cache != NULL) {
10355 /*
10356 * If we have a cached ecb, we'll use its action list instead
10357 * of creating our own (saving both time and space).
10358 */
10359 dtrace_ecb_t *cached = dtrace_ecb_create_cache;
10360 dtrace_action_t *act2 = cached->dte_action;
10361
10362 if (act2 != NULL) {
10363 ASSERT(act2->dta_refcnt > 0);
10364 act2->dta_refcnt++;
10365 ecb->dte_action = act2;
10366 ecb->dte_action_last = cached->dte_action_last;
10367 ecb->dte_needed = cached->dte_needed;
10368 ecb->dte_size = cached->dte_size;
10369 ecb->dte_alignment = cached->dte_alignment;
10370 }
10371
10372 return (ecb);
10373 }
10374
10375 for (act = desc->dted_action; act != NULL; act = act->dtad_next) {
10376 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) {
10377 dtrace_ecb_destroy(ecb);
10378 return (NULL);
10379 }
10380 }
10381
10382 dtrace_ecb_resize(ecb);
10383
10384 return (dtrace_ecb_create_cache = ecb);
10385}
10386
10387static int
10388dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg)
10389{
10390 dtrace_ecb_t *ecb;
10391 dtrace_enabling_t *enab = arg;
10392 dtrace_state_t *state = enab->dten_vstate->dtvs_state;
10393
10394 ASSERT(state != NULL);
10395
10396 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) {
10397 /*
10398 * This probe was created in a generation for which this
10399 * enabling has previously created ECBs; we don't want to
10400 * enable it again, so just kick out.
10401 */
10402 return (DTRACE_MATCH_NEXT);
10403 }
10404
10405 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL)
10406 return (DTRACE_MATCH_DONE);
10407
10408 if (dtrace_ecb_enable(ecb) < 0)
10409 return (DTRACE_MATCH_FAIL);
10410
10411 return (DTRACE_MATCH_NEXT);
10412}
10413
10414static dtrace_ecb_t *
10415dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id)
10416{
10417 dtrace_ecb_t *ecb; NOREF(ecb);
10418
10419 ASSERT(MUTEX_HELD(&dtrace_lock));
10420
10421 if (id == 0 || VBDTCAST(int64_t)id > state->dts_necbs)
10422 return (NULL);
10423
10424 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL);
10425 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id);
10426
10427 return (state->dts_ecbs[id - 1]);
10428}
10429
10430static dtrace_aggregation_t *
10431dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id)
10432{
10433 dtrace_aggregation_t *agg; NOREF(agg);
10434
10435 ASSERT(MUTEX_HELD(&dtrace_lock));
10436
10437 if (id == 0 || VBDTCAST(int64_t)id > state->dts_naggregations)
10438 return (NULL);
10439
10440 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL);
10441 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL ||
10442 agg->dtag_id == id);
10443
10444 return (state->dts_aggregations[id - 1]);
10445}
10446
10447/*
10448 * DTrace Buffer Functions
10449 *
10450 * The following functions manipulate DTrace buffers. Most of these functions
10451 * are called in the context of establishing or processing consumer state;
10452 * exceptions are explicitly noted.
10453 */
10454
10455/*
10456 * Note: called from cross call context. This function switches the two
10457 * buffers on a given CPU. The atomicity of this operation is assured by
10458 * disabling interrupts while the actual switch takes place; the disabling of
10459 * interrupts serializes the execution with any execution of dtrace_probe() on
10460 * the same CPU.
10461 */
10462static void
10463dtrace_buffer_switch(dtrace_buffer_t *buf)
10464{
10465 caddr_t tomax = buf->dtb_tomax;
10466 caddr_t xamot = buf->dtb_xamot;
10467 dtrace_icookie_t cookie;
10468
10469 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
10470 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING));
10471
10472 cookie = dtrace_interrupt_disable();
10473 buf->dtb_tomax = xamot;
10474 buf->dtb_xamot = tomax;
10475 buf->dtb_xamot_drops = buf->dtb_drops;
10476 buf->dtb_xamot_offset = buf->dtb_offset;
10477 buf->dtb_xamot_errors = buf->dtb_errors;
10478 buf->dtb_xamot_flags = buf->dtb_flags;
10479 buf->dtb_offset = 0;
10480 buf->dtb_drops = 0;
10481 buf->dtb_errors = 0;
10482 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED);
10483 dtrace_interrupt_enable(cookie);
10484}
10485
10486#ifdef VBOX
10487static DECLCALLBACK(void) dtrace_buffer_switch_wrapper(RTCPUID idCpu, void *pvUser1, void *pvUser2)
10488{
10489 dtrace_buffer_switch((dtrace_buffer_t *)pvUser1);
10490 NOREF(pvUser2); NOREF(idCpu);
10491}
10492#endif
10493
10494/*
10495 * Note: called from cross call context. This function activates a buffer
10496 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation
10497 * is guaranteed by the disabling of interrupts.
10498 */
10499static void
10500dtrace_buffer_activate(dtrace_state_t *state)
10501{
10502 dtrace_buffer_t *buf;
10503 dtrace_icookie_t cookie = dtrace_interrupt_disable();
10504
10505 buf = &state->dts_buffer[VBDT_GET_CPUID()];
10506
10507 if (buf->dtb_tomax != NULL) {
10508 /*
10509 * We might like to assert that the buffer is marked inactive,
10510 * but this isn't necessarily true: the buffer for the CPU
10511 * that processes the BEGIN probe has its buffer activated
10512 * manually. In this case, we take the (harmless) action
10513 * re-clearing the bit INACTIVE bit.
10514 */
10515 buf->dtb_flags &= ~DTRACEBUF_INACTIVE;
10516 }
10517
10518 dtrace_interrupt_enable(cookie);
10519}
10520
10521#ifdef VBOX
10522static DECLCALLBACK(void) dtrace_buffer_activate_wrapper(RTCPUID idCpu, void *pvUser1, void *pvUser2)
10523{
10524 dtrace_buffer_activate((dtrace_state_t *)pvUser1);
10525 NOREF(pvUser2); NOREF(idCpu);
10526}
10527#endif
10528
10529static int
10530dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags,
10531 processorid_t cpu)
10532{
10533#ifndef VBOX
10534 cpu_t *cp;
10535#else
10536 RTCPUSET CpuSet;
10537 unsigned iCpu;
10538#endif
10539 dtrace_buffer_t *buf;
10540
10541 ASSERT(MUTEX_HELD(&cpu_lock));
10542 ASSERT(MUTEX_HELD(&dtrace_lock));
10543
10544 if (VBDTCAST(int64_t)size > dtrace_nonroot_maxsize
10545#ifndef VBOX
10546 && !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)
10547#endif
10548 )
10549 return (EFBIG);
10550
10551#ifndef VBOX
10552 cp = cpu_list;
10553#else
10554 RTMpGetSet(&CpuSet);
10555#endif
10556
10557#ifndef VBOX
10558 do {
10559 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
10560 continue;
10561
10562 buf = &bufs[cp->cpu_id];
10563#else
10564 for (iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++) {
10565 if ( !RTCpuSetIsMember(&CpuSet, iCpu)
10566 || (cpu != (processorid_t)DTRACE_CPUALL && cpu != iCpu))
10567 continue;
10568
10569 buf = &bufs[iCpu];
10570#endif
10571
10572 /*
10573 * If there is already a buffer allocated for this CPU, it
10574 * is only possible that this is a DR event. In this case,
10575 * the buffer size must match our specified size.
10576 */
10577 if (buf->dtb_tomax != NULL) {
10578 ASSERT(buf->dtb_size == size);
10579 continue;
10580 }
10581
10582 ASSERT(buf->dtb_xamot == NULL);
10583
10584 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
10585 goto err;
10586
10587 buf->dtb_size = size;
10588 buf->dtb_flags = flags;
10589 buf->dtb_offset = 0;
10590 buf->dtb_drops = 0;
10591
10592 if (flags & DTRACEBUF_NOSWITCH)
10593 continue;
10594
10595 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
10596 goto err;
10597#ifndef VBOX
10598 } while ((cp = cp->cpu_next) != cpu_list);
10599#else
10600 }
10601#endif
10602
10603 return (0);
10604
10605err:
10606#ifndef VBOX
10607 cp = cpu_list;
10608
10609 do {
10610 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
10611 continue;
10612
10613 buf = &bufs[cp->cpu_id];
10614#else
10615 for (iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++) {
10616 if ( !RTCpuSetIsMember(&CpuSet, iCpu)
10617 || (cpu != (processorid_t)DTRACE_CPUALL && cpu != iCpu))
10618 continue;
10619
10620 buf = &bufs[iCpu];
10621#endif
10622
10623 if (buf->dtb_xamot != NULL) {
10624 ASSERT(buf->dtb_tomax != NULL);
10625 ASSERT(buf->dtb_size == size);
10626 kmem_free(buf->dtb_xamot, size);
10627 }
10628
10629 if (buf->dtb_tomax != NULL) {
10630 ASSERT(buf->dtb_size == size);
10631 kmem_free(buf->dtb_tomax, size);
10632 }
10633
10634 buf->dtb_tomax = NULL;
10635 buf->dtb_xamot = NULL;
10636 buf->dtb_size = 0;
10637#ifndef VBOX
10638 } while ((cp = cp->cpu_next) != cpu_list);
10639#else
10640 }
10641#endif
10642
10643 return (ENOMEM);
10644}
10645
10646/*
10647 * Note: called from probe context. This function just increments the drop
10648 * count on a buffer. It has been made a function to allow for the
10649 * possibility of understanding the source of mysterious drop counts. (A
10650 * problem for which one may be particularly disappointed that DTrace cannot
10651 * be used to understand DTrace.)
10652 */
10653static void
10654dtrace_buffer_drop(dtrace_buffer_t *buf)
10655{
10656 buf->dtb_drops++;
10657}
10658
10659/*
10660 * Note: called from probe context. This function is called to reserve space
10661 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the
10662 * mstate. Returns the new offset in the buffer, or a negative value if an
10663 * error has occurred.
10664 */
10665static intptr_t
10666dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align,
10667 dtrace_state_t *state, dtrace_mstate_t *mstate)
10668{
10669 intptr_t offs = buf->dtb_offset, soffs;
10670 intptr_t woffs;
10671 caddr_t tomax;
10672 size_t total;
10673
10674 if (buf->dtb_flags & DTRACEBUF_INACTIVE)
10675 return (-1);
10676
10677 if ((tomax = buf->dtb_tomax) == NULL) {
10678 dtrace_buffer_drop(buf);
10679 return (-1);
10680 }
10681
10682 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) {
10683 while (offs & (align - 1)) {
10684 /*
10685 * Assert that our alignment is off by a number which
10686 * is itself sizeof (uint32_t) aligned.
10687 */
10688 ASSERT(!((align - (offs & (align - 1))) &
10689 (sizeof (uint32_t) - 1)));
10690 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
10691 offs += sizeof (uint32_t);
10692 }
10693
10694 if (VBDTCAST(uintptr_t)(soffs = offs + needed) > buf->dtb_size) {
10695 dtrace_buffer_drop(buf);
10696 return (-1);
10697 }
10698
10699 if (mstate == NULL)
10700 return (offs);
10701
10702 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs;
10703 mstate->dtms_scratch_size = buf->dtb_size - soffs;
10704 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
10705
10706 return (offs);
10707 }
10708
10709 if (buf->dtb_flags & DTRACEBUF_FILL) {
10710 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN &&
10711 (buf->dtb_flags & DTRACEBUF_FULL))
10712 return (-1);
10713 goto out;
10714 }
10715
10716 total = needed + (offs & (align - 1));
10717
10718 /*
10719 * For a ring buffer, life is quite a bit more complicated. Before
10720 * we can store any padding, we need to adjust our wrapping offset.
10721 * (If we've never before wrapped or we're not about to, no adjustment
10722 * is required.)
10723 */
10724 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) ||
10725 offs + total > buf->dtb_size) {
10726 woffs = buf->dtb_xamot_offset;
10727
10728 if (offs + total > buf->dtb_size) {
10729 /*
10730 * We can't fit in the end of the buffer. First, a
10731 * sanity check that we can fit in the buffer at all.
10732 */
10733 if (total > buf->dtb_size) {
10734 dtrace_buffer_drop(buf);
10735 return (-1);
10736 }
10737
10738 /*
10739 * We're going to be storing at the top of the buffer,
10740 * so now we need to deal with the wrapped offset. We
10741 * only reset our wrapped offset to 0 if it is
10742 * currently greater than the current offset. If it
10743 * is less than the current offset, it is because a
10744 * previous allocation induced a wrap -- but the
10745 * allocation didn't subsequently take the space due
10746 * to an error or false predicate evaluation. In this
10747 * case, we'll just leave the wrapped offset alone: if
10748 * the wrapped offset hasn't been advanced far enough
10749 * for this allocation, it will be adjusted in the
10750 * lower loop.
10751 */
10752 if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
10753 if (woffs >= offs)
10754 woffs = 0;
10755 } else {
10756 woffs = 0;
10757 }
10758
10759 /*
10760 * Now we know that we're going to be storing to the
10761 * top of the buffer and that there is room for us
10762 * there. We need to clear the buffer from the current
10763 * offset to the end (there may be old gunk there).
10764 */
10765 while (VBDTCAST(uintptr_t)offs < buf->dtb_size)
10766 tomax[offs++] = 0;
10767
10768 /*
10769 * We need to set our offset to zero. And because we
10770 * are wrapping, we need to set the bit indicating as
10771 * much. We can also adjust our needed space back
10772 * down to the space required by the ECB -- we know
10773 * that the top of the buffer is aligned.
10774 */
10775 offs = 0;
10776 total = needed;
10777 buf->dtb_flags |= DTRACEBUF_WRAPPED;
10778 } else {
10779 /*
10780 * There is room for us in the buffer, so we simply
10781 * need to check the wrapped offset.
10782 */
10783 if (woffs < offs) {
10784 /*
10785 * The wrapped offset is less than the offset.
10786 * This can happen if we allocated buffer space
10787 * that induced a wrap, but then we didn't
10788 * subsequently take the space due to an error
10789 * or false predicate evaluation. This is
10790 * okay; we know that _this_ allocation isn't
10791 * going to induce a wrap. We still can't
10792 * reset the wrapped offset to be zero,
10793 * however: the space may have been trashed in
10794 * the previous failed probe attempt. But at
10795 * least the wrapped offset doesn't need to
10796 * be adjusted at all...
10797 */
10798 goto out;
10799 }
10800 }
10801
10802 while (VBDTCAST(uintptr_t)offs + total > VBDTCAST(uintptr_t)woffs) {
10803 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs);
10804 size_t size;
10805
10806 if (epid == DTRACE_EPIDNONE) {
10807 size = sizeof (uint32_t);
10808 } else {
10809 ASSERT(VBDTCAST(int64_t)epid <= state->dts_necbs);
10810 ASSERT(state->dts_ecbs[epid - 1] != NULL);
10811
10812 size = state->dts_ecbs[epid - 1]->dte_size;
10813 }
10814
10815 ASSERT(woffs + size <= buf->dtb_size);
10816 ASSERT(size != 0);
10817
10818 if (woffs + size == buf->dtb_size) {
10819 /*
10820 * We've reached the end of the buffer; we want
10821 * to set the wrapped offset to 0 and break
10822 * out. However, if the offs is 0, then we're
10823 * in a strange edge-condition: the amount of
10824 * space that we want to reserve plus the size
10825 * of the record that we're overwriting is
10826 * greater than the size of the buffer. This
10827 * is problematic because if we reserve the
10828 * space but subsequently don't consume it (due
10829 * to a failed predicate or error) the wrapped
10830 * offset will be 0 -- yet the EPID at offset 0
10831 * will not be committed. This situation is
10832 * relatively easy to deal with: if we're in
10833 * this case, the buffer is indistinguishable
10834 * from one that hasn't wrapped; we need only
10835 * finish the job by clearing the wrapped bit,
10836 * explicitly setting the offset to be 0, and
10837 * zero'ing out the old data in the buffer.
10838 */
10839 if (offs == 0) {
10840 buf->dtb_flags &= ~DTRACEBUF_WRAPPED;
10841 buf->dtb_offset = 0;
10842 woffs = total;
10843
10844 while (VBDTCAST(uintptr_t)woffs < buf->dtb_size)
10845 tomax[woffs++] = 0;
10846 }
10847
10848 woffs = 0;
10849 break;
10850 }
10851
10852 woffs += size;
10853 }
10854
10855 /*
10856 * We have a wrapped offset. It may be that the wrapped offset
10857 * has become zero -- that's okay.
10858 */
10859 buf->dtb_xamot_offset = woffs;
10860 }
10861
10862out:
10863 /*
10864 * Now we can plow the buffer with any necessary padding.
10865 */
10866 while (offs & (align - 1)) {
10867 /*
10868 * Assert that our alignment is off by a number which
10869 * is itself sizeof (uint32_t) aligned.
10870 */
10871 ASSERT(!((align - (offs & (align - 1))) &
10872 (sizeof (uint32_t) - 1)));
10873 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
10874 offs += sizeof (uint32_t);
10875 }
10876
10877 if (buf->dtb_flags & DTRACEBUF_FILL) {
10878 if (offs + needed > buf->dtb_size - state->dts_reserve) {
10879 buf->dtb_flags |= DTRACEBUF_FULL;
10880 return (-1);
10881 }
10882 }
10883
10884 if (mstate == NULL)
10885 return (offs);
10886
10887 /*
10888 * For ring buffers and fill buffers, the scratch space is always
10889 * the inactive buffer.
10890 */
10891 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot;
10892 mstate->dtms_scratch_size = buf->dtb_size;
10893 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
10894
10895 return (offs);
10896}
10897
10898static void
10899dtrace_buffer_polish(dtrace_buffer_t *buf)
10900{
10901 ASSERT(buf->dtb_flags & DTRACEBUF_RING);
10902 ASSERT(MUTEX_HELD(&dtrace_lock));
10903
10904 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED))
10905 return;
10906
10907 /*
10908 * We need to polish the ring buffer. There are three cases:
10909 *
10910 * - The first (and presumably most common) is that there is no gap
10911 * between the buffer offset and the wrapped offset. In this case,
10912 * there is nothing in the buffer that isn't valid data; we can
10913 * mark the buffer as polished and return.
10914 *
10915 * - The second (less common than the first but still more common
10916 * than the third) is that there is a gap between the buffer offset
10917 * and the wrapped offset, and the wrapped offset is larger than the
10918 * buffer offset. This can happen because of an alignment issue, or
10919 * can happen because of a call to dtrace_buffer_reserve() that
10920 * didn't subsequently consume the buffer space. In this case,
10921 * we need to zero the data from the buffer offset to the wrapped
10922 * offset.
10923 *
10924 * - The third (and least common) is that there is a gap between the
10925 * buffer offset and the wrapped offset, but the wrapped offset is
10926 * _less_ than the buffer offset. This can only happen because a
10927 * call to dtrace_buffer_reserve() induced a wrap, but the space
10928 * was not subsequently consumed. In this case, we need to zero the
10929 * space from the offset to the end of the buffer _and_ from the
10930 * top of the buffer to the wrapped offset.
10931 */
10932 if (buf->dtb_offset < buf->dtb_xamot_offset) {
10933 bzero(buf->dtb_tomax + buf->dtb_offset,
10934 buf->dtb_xamot_offset - buf->dtb_offset);
10935 }
10936
10937 if (buf->dtb_offset > buf->dtb_xamot_offset) {
10938 bzero(buf->dtb_tomax + buf->dtb_offset,
10939 buf->dtb_size - buf->dtb_offset);
10940 bzero(buf->dtb_tomax, buf->dtb_xamot_offset);
10941 }
10942}
10943
10944static void
10945dtrace_buffer_free(dtrace_buffer_t *bufs)
10946{
10947 int i;
10948
10949 for (i = 0; i < NCPU; i++) {
10950 dtrace_buffer_t *buf = &bufs[i];
10951
10952 if (buf->dtb_tomax == NULL) {
10953 ASSERT(buf->dtb_xamot == NULL);
10954 ASSERT(buf->dtb_size == 0);
10955 continue;
10956 }
10957
10958 if (buf->dtb_xamot != NULL) {
10959 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
10960 kmem_free(buf->dtb_xamot, buf->dtb_size);
10961 }
10962
10963 kmem_free(buf->dtb_tomax, buf->dtb_size);
10964 buf->dtb_size = 0;
10965 buf->dtb_tomax = NULL;
10966 buf->dtb_xamot = NULL;
10967 }
10968}
10969
10970/*
10971 * DTrace Enabling Functions
10972 */
10973static dtrace_enabling_t *
10974dtrace_enabling_create(dtrace_vstate_t *vstate)
10975{
10976 dtrace_enabling_t *enab;
10977
10978 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP);
10979 enab->dten_vstate = vstate;
10980
10981 return (enab);
10982}
10983
10984static void
10985dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb)
10986{
10987 dtrace_ecbdesc_t **ndesc;
10988 size_t osize, nsize;
10989
10990 /*
10991 * We can't add to enablings after we've enabled them, or after we've
10992 * retained them.
10993 */
10994 ASSERT(enab->dten_probegen == 0);
10995 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
10996
10997 if (enab->dten_ndesc < enab->dten_maxdesc) {
10998 enab->dten_desc[enab->dten_ndesc++] = ecb;
10999 return;
11000 }
11001
11002 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
11003
11004 if (enab->dten_maxdesc == 0) {
11005 enab->dten_maxdesc = 1;
11006 } else {
11007 enab->dten_maxdesc <<= 1;
11008 }
11009
11010 ASSERT(enab->dten_ndesc < enab->dten_maxdesc);
11011
11012 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
11013 ndesc = kmem_zalloc(nsize, KM_SLEEP);
11014 bcopy(enab->dten_desc, ndesc, osize);
11015 kmem_free(enab->dten_desc, osize);
11016
11017 enab->dten_desc = ndesc;
11018 enab->dten_desc[enab->dten_ndesc++] = ecb;
11019}
11020
11021static void
11022dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb,
11023 dtrace_probedesc_t *pd)
11024{
11025 dtrace_ecbdesc_t *new;
11026 dtrace_predicate_t *pred;
11027 dtrace_actdesc_t *act;
11028
11029 /*
11030 * We're going to create a new ECB description that matches the
11031 * specified ECB in every way, but has the specified probe description.
11032 */
11033 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
11034
11035 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL)
11036 dtrace_predicate_hold(pred);
11037
11038 for (act = ecb->dted_action; act != NULL; act = act->dtad_next)
11039 dtrace_actdesc_hold(act);
11040
11041 new->dted_action = ecb->dted_action;
11042 new->dted_pred = ecb->dted_pred;
11043 new->dted_probe = *pd;
11044 new->dted_uarg = ecb->dted_uarg;
11045
11046 dtrace_enabling_add(enab, new);
11047}
11048
11049#ifndef VBOX
11050static void
11051dtrace_enabling_dump(dtrace_enabling_t *enab)
11052{
11053 int i;
11054
11055 for (i = 0; i < enab->dten_ndesc; i++) {
11056 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe;
11057
11058 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i,
11059 desc->dtpd_provider, desc->dtpd_mod,
11060 desc->dtpd_func, desc->dtpd_name);
11061 }
11062}
11063#endif /* !VBOX */
11064
11065static void
11066dtrace_enabling_destroy(dtrace_enabling_t *enab)
11067{
11068 int i;
11069 dtrace_ecbdesc_t *ep;
11070 dtrace_vstate_t *vstate = enab->dten_vstate;
11071
11072 ASSERT(MUTEX_HELD(&dtrace_lock));
11073
11074 for (i = 0; i < enab->dten_ndesc; i++) {
11075 dtrace_actdesc_t *act, *next;
11076 dtrace_predicate_t *pred;
11077
11078 ep = enab->dten_desc[i];
11079
11080 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL)
11081 dtrace_predicate_release(pred, vstate);
11082
11083 for (act = ep->dted_action; act != NULL; act = next) {
11084 next = act->dtad_next;
11085 dtrace_actdesc_release(act, vstate);
11086 }
11087
11088 kmem_free(ep, sizeof (dtrace_ecbdesc_t));
11089 }
11090
11091 kmem_free(enab->dten_desc,
11092 enab->dten_maxdesc * sizeof (dtrace_enabling_t *));
11093
11094 /*
11095 * If this was a retained enabling, decrement the dts_nretained count
11096 * and take it off of the dtrace_retained list.
11097 */
11098 if (enab->dten_prev != NULL || enab->dten_next != NULL ||
11099 dtrace_retained == enab) {
11100 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11101 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0);
11102 enab->dten_vstate->dtvs_state->dts_nretained--;
11103 dtrace_retained_gen++;
11104 }
11105
11106 if (enab->dten_prev == NULL) {
11107 if (dtrace_retained == enab) {
11108 dtrace_retained = enab->dten_next;
11109
11110 if (dtrace_retained != NULL)
11111 dtrace_retained->dten_prev = NULL;
11112 }
11113 } else {
11114 ASSERT(enab != dtrace_retained);
11115 ASSERT(dtrace_retained != NULL);
11116 enab->dten_prev->dten_next = enab->dten_next;
11117 }
11118
11119 if (enab->dten_next != NULL) {
11120 ASSERT(dtrace_retained != NULL);
11121 enab->dten_next->dten_prev = enab->dten_prev;
11122 }
11123
11124 kmem_free(enab, sizeof (dtrace_enabling_t));
11125}
11126
11127static int
11128dtrace_enabling_retain(dtrace_enabling_t *enab)
11129{
11130 dtrace_state_t *state;
11131
11132 ASSERT(MUTEX_HELD(&dtrace_lock));
11133 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
11134 ASSERT(enab->dten_vstate != NULL);
11135
11136 state = enab->dten_vstate->dtvs_state;
11137 ASSERT(state != NULL);
11138
11139 /*
11140 * We only allow each state to retain dtrace_retain_max enablings.
11141 */
11142 if (state->dts_nretained >= dtrace_retain_max)
11143 return (ENOSPC);
11144
11145 state->dts_nretained++;
11146 dtrace_retained_gen++;
11147
11148 if (dtrace_retained == NULL) {
11149 dtrace_retained = enab;
11150 return (0);
11151 }
11152
11153 enab->dten_next = dtrace_retained;
11154 dtrace_retained->dten_prev = enab;
11155 dtrace_retained = enab;
11156
11157 return (0);
11158}
11159
11160static int
11161dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match,
11162 dtrace_probedesc_t *create)
11163{
11164 dtrace_enabling_t *new, *enab;
11165 int found = 0, err = ENOENT;
11166
11167 ASSERT(MUTEX_HELD(&dtrace_lock));
11168 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN);
11169 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN);
11170 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN);
11171 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN);
11172
11173 new = dtrace_enabling_create(&state->dts_vstate);
11174
11175 /*
11176 * Iterate over all retained enablings, looking for enablings that
11177 * match the specified state.
11178 */
11179 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11180 int i;
11181
11182 /*
11183 * dtvs_state can only be NULL for helper enablings -- and
11184 * helper enablings can't be retained.
11185 */
11186 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11187
11188 if (enab->dten_vstate->dtvs_state != state)
11189 continue;
11190
11191 /*
11192 * Now iterate over each probe description; we're looking for
11193 * an exact match to the specified probe description.
11194 */
11195 for (i = 0; i < enab->dten_ndesc; i++) {
11196 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
11197 dtrace_probedesc_t *pd = &ep->dted_probe;
11198
11199 if (strcmp(pd->dtpd_provider, match->dtpd_provider))
11200 continue;
11201
11202 if (strcmp(pd->dtpd_mod, match->dtpd_mod))
11203 continue;
11204
11205 if (strcmp(pd->dtpd_func, match->dtpd_func))
11206 continue;
11207
11208 if (strcmp(pd->dtpd_name, match->dtpd_name))
11209 continue;
11210
11211 /*
11212 * We have a winning probe! Add it to our growing
11213 * enabling.
11214 */
11215 found = 1;
11216 dtrace_enabling_addlike(new, ep, create);
11217 }
11218 }
11219
11220 if (!found || (err = dtrace_enabling_retain(new)) != 0) {
11221 dtrace_enabling_destroy(new);
11222 return (err);
11223 }
11224
11225 return (0);
11226}
11227
11228static void
11229dtrace_enabling_retract(dtrace_state_t *state)
11230{
11231 dtrace_enabling_t *enab, *next;
11232
11233 ASSERT(MUTEX_HELD(&dtrace_lock));
11234
11235 /*
11236 * Iterate over all retained enablings, destroy the enablings retained
11237 * for the specified state.
11238 */
11239 for (enab = dtrace_retained; enab != NULL; enab = next) {
11240 next = enab->dten_next;
11241
11242 /*
11243 * dtvs_state can only be NULL for helper enablings -- and
11244 * helper enablings can't be retained.
11245 */
11246 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11247
11248 if (enab->dten_vstate->dtvs_state == state) {
11249 ASSERT(state->dts_nretained > 0);
11250 dtrace_enabling_destroy(enab);
11251 }
11252 }
11253
11254 ASSERT(state->dts_nretained == 0);
11255}
11256
11257static int
11258dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched)
11259{
11260 int i = 0;
11261 int total_matched = 0, matched = 0;
11262
11263 ASSERT(MUTEX_HELD(&cpu_lock));
11264 ASSERT(MUTEX_HELD(&dtrace_lock));
11265
11266 for (i = 0; i < enab->dten_ndesc; i++) {
11267 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
11268
11269 enab->dten_current = ep;
11270 enab->dten_error = 0;
11271
11272 /*
11273 * If a provider failed to enable a probe then get out and
11274 * let the consumer know we failed.
11275 */
11276 if ((matched = dtrace_probe_enable(&ep->dted_probe, enab)) < 0)
11277 return (EBUSY);
11278
11279 total_matched += matched;
11280
11281 if (enab->dten_error != 0) {
11282 /*
11283 * If we get an error half-way through enabling the
11284 * probes, we kick out -- perhaps with some number of
11285 * them enabled. Leaving enabled probes enabled may
11286 * be slightly confusing for user-level, but we expect
11287 * that no one will attempt to actually drive on in
11288 * the face of such errors. If this is an anonymous
11289 * enabling (indicated with a NULL nmatched pointer),
11290 * we cmn_err() a message. We aren't expecting to
11291 * get such an error -- such as it can exist at all,
11292 * it would be a result of corrupted DOF in the driver
11293 * properties.
11294 */
11295 if (nmatched == NULL) {
11296 cmn_err(CE_WARN, "dtrace_enabling_match() "
11297 "error on %p: %d", (void *)ep,
11298 enab->dten_error);
11299 }
11300
11301 return (enab->dten_error);
11302 }
11303 }
11304
11305 enab->dten_probegen = dtrace_probegen;
11306 if (nmatched != NULL)
11307 *nmatched = total_matched;
11308
11309 return (0);
11310}
11311
11312static void
11313dtrace_enabling_matchall(void)
11314{
11315 dtrace_enabling_t *enab;
11316
11317 mutex_enter(&cpu_lock);
11318 mutex_enter(&dtrace_lock);
11319
11320 /*
11321 * Iterate over all retained enablings to see if any probes match
11322 * against them. We only perform this operation on enablings for which
11323 * we have sufficient permissions by virtue of being in the global zone
11324 * or in the same zone as the DTrace client. Because we can be called
11325 * after dtrace_detach() has been called, we cannot assert that there
11326 * are retained enablings. We can safely load from dtrace_retained,
11327 * however: the taskq_destroy() at the end of dtrace_detach() will
11328 * block pending our completion.
11329 */
11330 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11331#ifndef VBOX
11332 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred;
11333
11334 if (INGLOBALZONE(curproc) ||
11335 cr != NULL && getzoneid() == crgetzoneid(cr))
11336#endif
11337 (void) dtrace_enabling_match(enab, NULL);
11338 }
11339
11340 mutex_exit(&dtrace_lock);
11341 mutex_exit(&cpu_lock);
11342}
11343
11344/*
11345 * If an enabling is to be enabled without having matched probes (that is, if
11346 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
11347 * enabling must be _primed_ by creating an ECB for every ECB description.
11348 * This must be done to assure that we know the number of speculations, the
11349 * number of aggregations, the minimum buffer size needed, etc. before we
11350 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually
11351 * enabling any probes, we create ECBs for every ECB decription, but with a
11352 * NULL probe -- which is exactly what this function does.
11353 */
11354static void
11355dtrace_enabling_prime(dtrace_state_t *state)
11356{
11357 dtrace_enabling_t *enab;
11358 int i;
11359
11360 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11361 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11362
11363 if (enab->dten_vstate->dtvs_state != state)
11364 continue;
11365
11366 /*
11367 * We don't want to prime an enabling more than once, lest
11368 * we allow a malicious user to induce resource exhaustion.
11369 * (The ECBs that result from priming an enabling aren't
11370 * leaked -- but they also aren't deallocated until the
11371 * consumer state is destroyed.)
11372 */
11373 if (enab->dten_primed)
11374 continue;
11375
11376 for (i = 0; i < enab->dten_ndesc; i++) {
11377 enab->dten_current = enab->dten_desc[i];
11378 (void) dtrace_probe_enable(NULL, enab);
11379 }
11380
11381 enab->dten_primed = 1;
11382 }
11383}
11384
11385/*
11386 * Called to indicate that probes should be provided due to retained
11387 * enablings. This is implemented in terms of dtrace_probe_provide(), but it
11388 * must take an initial lap through the enabling calling the dtps_provide()
11389 * entry point explicitly to allow for autocreated probes.
11390 */
11391static void
11392dtrace_enabling_provide(dtrace_provider_t *prv)
11393{
11394 int i, all = 0;
11395 dtrace_probedesc_t desc;
11396 dtrace_genid_t gen;
11397
11398 ASSERT(MUTEX_HELD(&dtrace_lock));
11399 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
11400
11401 if (prv == NULL) {
11402 all = 1;
11403 prv = dtrace_provider;
11404 }
11405
11406 do {
11407 dtrace_enabling_t *enab;
11408 void *parg = prv->dtpv_arg;
11409
11410retry:
11411 gen = dtrace_retained_gen;
11412 for (enab = dtrace_retained; enab != NULL;
11413 enab = enab->dten_next) {
11414 for (i = 0; i < enab->dten_ndesc; i++) {
11415 desc = enab->dten_desc[i]->dted_probe;
11416 mutex_exit(&dtrace_lock);
11417 prv->dtpv_pops.dtps_provide(parg, &desc);
11418 mutex_enter(&dtrace_lock);
11419 /*
11420 * Process the retained enablings again if
11421 * they have changed while we weren't holding
11422 * dtrace_lock.
11423 */
11424 if (gen != dtrace_retained_gen)
11425 goto retry;
11426 }
11427 }
11428 } while (all && (prv = prv->dtpv_next) != NULL);
11429
11430 mutex_exit(&dtrace_lock);
11431 dtrace_probe_provide(NULL, all ? NULL : prv);
11432 mutex_enter(&dtrace_lock);
11433}
11434
11435/*
11436 * DTrace DOF Functions
11437 */
11438/*ARGSUSED*/
11439static void
11440dtrace_dof_error(dof_hdr_t *dof, const char *str)
11441{
11442 RT_NOREF_PV(dof);
11443
11444 if (dtrace_err_verbose)
11445 cmn_err(CE_WARN, "failed to process DOF: %s", str);
11446
11447#ifdef DTRACE_ERRDEBUG
11448 dtrace_errdebug(str);
11449#endif
11450}
11451
11452/*
11453 * Create DOF out of a currently enabled state. Right now, we only create
11454 * DOF containing the run-time options -- but this could be expanded to create
11455 * complete DOF representing the enabled state.
11456 */
11457static dof_hdr_t *
11458dtrace_dof_create(dtrace_state_t *state)
11459{
11460 dof_hdr_t *dof;
11461 dof_sec_t *sec;
11462 dof_optdesc_t *opt;
11463 int i, len = sizeof (dof_hdr_t) +
11464 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
11465 sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
11466
11467 ASSERT(MUTEX_HELD(&dtrace_lock));
11468
11469 dof = kmem_zalloc(len, KM_SLEEP);
11470 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
11471 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
11472 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
11473 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
11474
11475 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
11476 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
11477 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
11478 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
11479 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
11480 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
11481
11482 dof->dofh_flags = 0;
11483 dof->dofh_hdrsize = sizeof (dof_hdr_t);
11484 dof->dofh_secsize = sizeof (dof_sec_t);
11485 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
11486 dof->dofh_secoff = sizeof (dof_hdr_t);
11487 dof->dofh_loadsz = len;
11488 dof->dofh_filesz = len;
11489 dof->dofh_pad = 0;
11490
11491 /*
11492 * Fill in the option section header...
11493 */
11494 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
11495 sec->dofs_type = DOF_SECT_OPTDESC;
11496 sec->dofs_align = sizeof (uint64_t);
11497 sec->dofs_flags = DOF_SECF_LOAD;
11498 sec->dofs_entsize = sizeof (dof_optdesc_t);
11499
11500 opt = (dof_optdesc_t *)((uintptr_t)sec +
11501 roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
11502
11503 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
11504 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
11505
11506 for (i = 0; i < DTRACEOPT_MAX; i++) {
11507 opt[i].dofo_option = i;
11508 opt[i].dofo_strtab = DOF_SECIDX_NONE;
11509 opt[i].dofo_value = state->dts_options[i];
11510 }
11511
11512 return (dof);
11513}
11514
11515static dof_hdr_t *
11516dtrace_dof_copyin(uintptr_t uarg, int *errp)
11517{
11518 dof_hdr_t hdr, *dof;
11519
11520 ASSERT(!MUTEX_HELD(&dtrace_lock));
11521
11522 /*
11523 * First, we're going to copyin() the sizeof (dof_hdr_t).
11524 */
11525 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) {
11526 dtrace_dof_error(NULL, "failed to copyin DOF header");
11527 *errp = EFAULT;
11528 return (NULL);
11529 }
11530
11531 /*
11532 * Now we'll allocate the entire DOF and copy it in -- provided
11533 * that the length isn't outrageous.
11534 */
11535 if (hdr.dofh_loadsz >= VBDTCAST(uint64_t)dtrace_dof_maxsize) {
11536 dtrace_dof_error(&hdr, "load size exceeds maximum");
11537 *errp = E2BIG;
11538 return (NULL);
11539 }
11540
11541 if (hdr.dofh_loadsz < sizeof (hdr)) {
11542 dtrace_dof_error(&hdr, "invalid load size");
11543 *errp = EINVAL;
11544 return (NULL);
11545 }
11546
11547 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP);
11548
11549 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 ||
11550 dof->dofh_loadsz != hdr.dofh_loadsz) {
11551 kmem_free(dof, hdr.dofh_loadsz);
11552 *errp = EFAULT;
11553 return (NULL);
11554 }
11555
11556 return (dof);
11557}
11558
11559#ifndef VBOX
11560static dof_hdr_t *
11561dtrace_dof_property(const char *name)
11562{
11563#ifndef VBOX
11564 uchar_t *buf;
11565 uint64_t loadsz;
11566 unsigned int len, i;
11567 dof_hdr_t *dof;
11568
11569 /*
11570 * Unfortunately, array of values in .conf files are always (and
11571 * only) interpreted to be integer arrays. We must read our DOF
11572 * as an integer array, and then squeeze it into a byte array.
11573 */
11574 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0,
11575 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS)
11576 return (NULL);
11577
11578 for (i = 0; i < len; i++)
11579 buf[i] = (uchar_t)(((int *)buf)[i]);
11580
11581 if (len < sizeof (dof_hdr_t)) {
11582 ddi_prop_free(buf);
11583 dtrace_dof_error(NULL, "truncated header");
11584 return (NULL);
11585 }
11586
11587 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) {
11588 ddi_prop_free(buf);
11589 dtrace_dof_error(NULL, "truncated DOF");
11590 return (NULL);
11591 }
11592
11593 if (loadsz >= dtrace_dof_maxsize) {
11594 ddi_prop_free(buf);
11595 dtrace_dof_error(NULL, "oversized DOF");
11596 return (NULL);
11597 }
11598
11599 dof = kmem_alloc(loadsz, KM_SLEEP);
11600 bcopy(buf, dof, loadsz);
11601 ddi_prop_free(buf);
11602
11603 return (dof);
11604#else /* VBOX */
11605 RT_NOREF_PV(name);
11606 return (NULL);
11607#endif /* VBOX */
11608}
11609#endif /* !VBOX */
11610
11611static void
11612dtrace_dof_destroy(dof_hdr_t *dof)
11613{
11614 kmem_free(dof, dof->dofh_loadsz);
11615}
11616
11617/*
11618 * Return the dof_sec_t pointer corresponding to a given section index. If the
11619 * index is not valid, dtrace_dof_error() is called and NULL is returned. If
11620 * a type other than DOF_SECT_NONE is specified, the header is checked against
11621 * this type and NULL is returned if the types do not match.
11622 */
11623static dof_sec_t *
11624dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i)
11625{
11626 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)
11627 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize);
11628
11629 if (i >= dof->dofh_secnum) {
11630 dtrace_dof_error(dof, "referenced section index is invalid");
11631 return (NULL);
11632 }
11633
11634 if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
11635 dtrace_dof_error(dof, "referenced section is not loadable");
11636 return (NULL);
11637 }
11638
11639 if (type != DOF_SECT_NONE && type != sec->dofs_type) {
11640 dtrace_dof_error(dof, "referenced section is the wrong type");
11641 return (NULL);
11642 }
11643
11644 return (sec);
11645}
11646
11647static dtrace_probedesc_t *
11648dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc)
11649{
11650 dof_probedesc_t *probe;
11651 dof_sec_t *strtab;
11652 uintptr_t daddr = (uintptr_t)dof;
11653 uintptr_t str;
11654 size_t size;
11655
11656 if (sec->dofs_type != DOF_SECT_PROBEDESC) {
11657 dtrace_dof_error(dof, "invalid probe section");
11658 return (NULL);
11659 }
11660
11661 if (sec->dofs_align != sizeof (dof_secidx_t)) {
11662 dtrace_dof_error(dof, "bad alignment in probe description");
11663 return (NULL);
11664 }
11665
11666 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) {
11667 dtrace_dof_error(dof, "truncated probe description");
11668 return (NULL);
11669 }
11670
11671 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset);
11672 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab);
11673
11674 if (strtab == NULL)
11675 return (NULL);
11676
11677 str = daddr + strtab->dofs_offset;
11678 size = strtab->dofs_size;
11679
11680 if (probe->dofp_provider >= strtab->dofs_size) {
11681 dtrace_dof_error(dof, "corrupt probe provider");
11682 return (NULL);
11683 }
11684
11685 (void) strncpy(desc->dtpd_provider,
11686 (char *)(str + probe->dofp_provider),
11687 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider));
11688
11689 if (probe->dofp_mod >= strtab->dofs_size) {
11690 dtrace_dof_error(dof, "corrupt probe module");
11691 return (NULL);
11692 }
11693
11694 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod),
11695 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod));
11696
11697 if (probe->dofp_func >= strtab->dofs_size) {
11698 dtrace_dof_error(dof, "corrupt probe function");
11699 return (NULL);
11700 }
11701
11702 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func),
11703 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func));
11704
11705 if (probe->dofp_name >= strtab->dofs_size) {
11706 dtrace_dof_error(dof, "corrupt probe name");
11707 return (NULL);
11708 }
11709
11710 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name),
11711 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name));
11712
11713 return (desc);
11714}
11715
11716static dtrace_difo_t *
11717dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
11718 cred_t *cr)
11719{
11720 dtrace_difo_t *dp;
11721 size_t ttl = 0;
11722 dof_difohdr_t *dofd;
11723 uintptr_t daddr = (uintptr_t)dof;
11724 size_t max = dtrace_difo_maxsize;
11725 int i, l, n;
11726
11727 static const struct {
11728 int section;
11729 int bufoffs;
11730 int lenoffs;
11731 int entsize;
11732 int align;
11733 const char *msg;
11734 } difo[] = {
11735 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf),
11736 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t),
11737 sizeof (dif_instr_t), "multiple DIF sections" },
11738
11739 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab),
11740 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t),
11741 sizeof (uint64_t), "multiple integer tables" },
11742
11743 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab),
11744 offsetof(dtrace_difo_t, dtdo_strlen), 0,
11745 sizeof (char), "multiple string tables" },
11746
11747 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab),
11748 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t),
11749 sizeof (uint_t), "multiple variable tables" },
11750
11751 { DOF_SECT_NONE, 0, 0, 0, NULL }
11752 };
11753
11754 if (sec->dofs_type != DOF_SECT_DIFOHDR) {
11755 dtrace_dof_error(dof, "invalid DIFO header section");
11756 return (NULL);
11757 }
11758
11759 if (sec->dofs_align != sizeof (dof_secidx_t)) {
11760 dtrace_dof_error(dof, "bad alignment in DIFO header");
11761 return (NULL);
11762 }
11763
11764 if (sec->dofs_size < sizeof (dof_difohdr_t) ||
11765 sec->dofs_size % sizeof (dof_secidx_t)) {
11766 dtrace_dof_error(dof, "bad size in DIFO header");
11767 return (NULL);
11768 }
11769
11770 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
11771 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1;
11772
11773 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
11774 dp->dtdo_rtype = dofd->dofd_rtype;
11775
11776 for (l = 0; l < n; l++) {
11777 dof_sec_t *subsec;
11778 void **bufp;
11779 uint32_t *lenp;
11780
11781 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE,
11782 dofd->dofd_links[l])) == NULL)
11783 goto err; /* invalid section link */
11784
11785 if (ttl + subsec->dofs_size > max) {
11786 dtrace_dof_error(dof, "exceeds maximum size");
11787 goto err;
11788 }
11789
11790 ttl += subsec->dofs_size;
11791
11792 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) {
11793 if (subsec->dofs_type != VBDTCAST(uint32_t)difo[i].section)
11794 continue;
11795
11796 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) {
11797 dtrace_dof_error(dof, "section not loaded");
11798 goto err;
11799 }
11800
11801 if (subsec->dofs_align != VBDTCAST(uint32_t)difo[i].align) {
11802 dtrace_dof_error(dof, "bad alignment");
11803 goto err;
11804 }
11805
11806 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs);
11807 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs);
11808
11809 if (*bufp != NULL) {
11810 dtrace_dof_error(dof, difo[i].msg);
11811 goto err;
11812 }
11813
11814 if (VBDTCAST(uint32_t)difo[i].entsize != subsec->dofs_entsize) {
11815 dtrace_dof_error(dof, "entry size mismatch");
11816 goto err;
11817 }
11818
11819 if (subsec->dofs_entsize != 0 &&
11820 (subsec->dofs_size % subsec->dofs_entsize) != 0) {
11821 dtrace_dof_error(dof, "corrupt entry size");
11822 goto err;
11823 }
11824
11825 *lenp = subsec->dofs_size;
11826 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP);
11827 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset),
11828 *bufp, subsec->dofs_size);
11829
11830 if (subsec->dofs_entsize != 0)
11831 *lenp /= subsec->dofs_entsize;
11832
11833 break;
11834 }
11835
11836 /*
11837 * If we encounter a loadable DIFO sub-section that is not
11838 * known to us, assume this is a broken program and fail.
11839 */
11840 if (difo[i].section == DOF_SECT_NONE &&
11841 (subsec->dofs_flags & DOF_SECF_LOAD)) {
11842 dtrace_dof_error(dof, "unrecognized DIFO subsection");
11843 goto err;
11844 }
11845 }
11846
11847 if (dp->dtdo_buf == NULL) {
11848 /*
11849 * We can't have a DIF object without DIF text.
11850 */
11851 dtrace_dof_error(dof, "missing DIF text");
11852 goto err;
11853 }
11854
11855 /*
11856 * Before we validate the DIF object, run through the variable table
11857 * looking for the strings -- if any of their size are under, we'll set
11858 * their size to be the system-wide default string size. Note that
11859 * this should _not_ happen if the "strsize" option has been set --
11860 * in this case, the compiler should have set the size to reflect the
11861 * setting of the option.
11862 */
11863 for (i = 0; VBDTCAST(unsigned)i < dp->dtdo_varlen; i++) {
11864 dtrace_difv_t *v = &dp->dtdo_vartab[i];
11865 dtrace_diftype_t *t = &v->dtdv_type;
11866
11867 if (v->dtdv_id < DIF_VAR_OTHER_UBASE)
11868 continue;
11869
11870 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0)
11871 t->dtdt_size = VBDTCAST(uint32_t)dtrace_strsize_default;
11872 }
11873
11874 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0)
11875 goto err;
11876
11877 dtrace_difo_init(dp, vstate);
11878 return (dp);
11879
11880err:
11881 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
11882 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
11883 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
11884 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
11885
11886 kmem_free(dp, sizeof (dtrace_difo_t));
11887 return (NULL);
11888}
11889
11890static dtrace_predicate_t *
11891dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
11892 cred_t *cr)
11893{
11894 dtrace_difo_t *dp;
11895
11896 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL)
11897 return (NULL);
11898
11899 return (dtrace_predicate_create(dp));
11900}
11901
11902static dtrace_actdesc_t *
11903dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
11904 cred_t *cr)
11905{
11906 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next;
11907 dof_actdesc_t *desc;
11908 dof_sec_t *difosec;
11909 size_t offs;
11910 uintptr_t daddr = (uintptr_t)dof;
11911 uint64_t arg;
11912 dtrace_actkind_t kind;
11913
11914 if (sec->dofs_type != DOF_SECT_ACTDESC) {
11915 dtrace_dof_error(dof, "invalid action section");
11916 return (NULL);
11917 }
11918
11919 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) {
11920 dtrace_dof_error(dof, "truncated action description");
11921 return (NULL);
11922 }
11923
11924 if (sec->dofs_align != sizeof (uint64_t)) {
11925 dtrace_dof_error(dof, "bad alignment in action description");
11926 return (NULL);
11927 }
11928
11929 if (sec->dofs_size < sec->dofs_entsize) {
11930 dtrace_dof_error(dof, "section entry size exceeds total size");
11931 return (NULL);
11932 }
11933
11934 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) {
11935 dtrace_dof_error(dof, "bad entry size in action description");
11936 return (NULL);
11937 }
11938
11939 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) {
11940 dtrace_dof_error(dof, "actions exceed dtrace_actions_max");
11941 return (NULL);
11942 }
11943
11944 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
11945 desc = (dof_actdesc_t *)(daddr +
11946 (uintptr_t)sec->dofs_offset + offs);
11947 kind = (dtrace_actkind_t)desc->dofa_kind;
11948
11949 if (DTRACEACT_ISPRINTFLIKE(kind) &&
11950 (kind != DTRACEACT_PRINTA ||
11951 desc->dofa_strtab != DOF_SECIDX_NONE)) {
11952 dof_sec_t *strtab;
11953 char *str, *fmt;
11954 uint64_t i;
11955
11956 /*
11957 * printf()-like actions must have a format string.
11958 */
11959 if ((strtab = dtrace_dof_sect(dof,
11960 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL)
11961 goto err;
11962
11963 str = (char *)((uintptr_t)dof +
11964 (uintptr_t)strtab->dofs_offset);
11965
11966 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) {
11967 if (str[i] == '\0')
11968 break;
11969 }
11970
11971 if (i >= strtab->dofs_size) {
11972 dtrace_dof_error(dof, "bogus format string");
11973 goto err;
11974 }
11975
11976 if (i == desc->dofa_arg) {
11977 dtrace_dof_error(dof, "empty format string");
11978 goto err;
11979 }
11980
11981 i -= desc->dofa_arg;
11982 fmt = kmem_alloc(i + 1, KM_SLEEP);
11983 bcopy(&str[desc->dofa_arg], fmt, i + 1);
11984 arg = (uint64_t)(uintptr_t)fmt;
11985 } else {
11986 if (kind == DTRACEACT_PRINTA) {
11987 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE);
11988 arg = 0;
11989 } else {
11990 arg = desc->dofa_arg;
11991 }
11992 }
11993
11994 act = dtrace_actdesc_create(kind, desc->dofa_ntuple,
11995 desc->dofa_uarg, arg);
11996
11997 if (last != NULL) {
11998 last->dtad_next = act;
11999 } else {
12000 first = act;
12001 }
12002
12003 last = act;
12004
12005 if (desc->dofa_difo == DOF_SECIDX_NONE)
12006 continue;
12007
12008 if ((difosec = dtrace_dof_sect(dof,
12009 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL)
12010 goto err;
12011
12012 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr);
12013
12014 if (act->dtad_difo == NULL)
12015 goto err;
12016 }
12017
12018 ASSERT(first != NULL);
12019 return (first);
12020
12021err:
12022 for (act = first; act != NULL; act = next) {
12023 next = act->dtad_next;
12024 dtrace_actdesc_release(act, vstate);
12025 }
12026
12027 return (NULL);
12028}
12029
12030static dtrace_ecbdesc_t *
12031dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12032 cred_t *cr)
12033{
12034 dtrace_ecbdesc_t *ep;
12035 dof_ecbdesc_t *ecb;
12036 dtrace_probedesc_t *desc;
12037 dtrace_predicate_t *pred = NULL;
12038
12039 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) {
12040 dtrace_dof_error(dof, "truncated ECB description");
12041 return (NULL);
12042 }
12043
12044 if (sec->dofs_align != sizeof (uint64_t)) {
12045 dtrace_dof_error(dof, "bad alignment in ECB description");
12046 return (NULL);
12047 }
12048
12049 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset);
12050 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes);
12051
12052 if (sec == NULL)
12053 return (NULL);
12054
12055 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
12056 ep->dted_uarg = ecb->dofe_uarg;
12057 desc = &ep->dted_probe;
12058
12059 if (dtrace_dof_probedesc(dof, sec, desc) == NULL)
12060 goto err;
12061
12062 if (ecb->dofe_pred != DOF_SECIDX_NONE) {
12063 if ((sec = dtrace_dof_sect(dof,
12064 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL)
12065 goto err;
12066
12067 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL)
12068 goto err;
12069
12070 ep->dted_pred.dtpdd_predicate = pred;
12071 }
12072
12073 if (ecb->dofe_actions != DOF_SECIDX_NONE) {
12074 if ((sec = dtrace_dof_sect(dof,
12075 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL)
12076 goto err;
12077
12078 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr);
12079
12080 if (ep->dted_action == NULL)
12081 goto err;
12082 }
12083
12084 return (ep);
12085
12086err:
12087 if (pred != NULL)
12088 dtrace_predicate_release(pred, vstate);
12089 kmem_free(ep, sizeof (dtrace_ecbdesc_t));
12090 return (NULL);
12091}
12092
12093/*
12094 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the
12095 * specified DOF. At present, this amounts to simply adding 'ubase' to the
12096 * site of any user SETX relocations to account for load object base address.
12097 * In the future, if we need other relocations, this function can be extended.
12098 */
12099static int
12100dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase)
12101{
12102 uintptr_t daddr = (uintptr_t)dof;
12103 dof_relohdr_t *dofr =
12104 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
12105 dof_sec_t *ss, *rs, *ts;
12106 dof_relodesc_t *r;
12107 uint_t i, n;
12108
12109 if (sec->dofs_size < sizeof (dof_relohdr_t) ||
12110 sec->dofs_align != sizeof (dof_secidx_t)) {
12111 dtrace_dof_error(dof, "invalid relocation header");
12112 return (-1);
12113 }
12114
12115 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab);
12116 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec);
12117 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec);
12118
12119 if (ss == NULL || rs == NULL || ts == NULL)
12120 return (-1); /* dtrace_dof_error() has been called already */
12121
12122 if (rs->dofs_entsize < sizeof (dof_relodesc_t) ||
12123 rs->dofs_align != sizeof (uint64_t)) {
12124 dtrace_dof_error(dof, "invalid relocation section");
12125 return (-1);
12126 }
12127
12128 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset);
12129 n = rs->dofs_size / rs->dofs_entsize;
12130
12131 for (i = 0; i < n; i++) {
12132 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset;
12133
12134 switch (r->dofr_type) {
12135 case DOF_RELO_NONE:
12136 break;
12137 case DOF_RELO_SETX:
12138 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset +
12139 sizeof (uint64_t) > ts->dofs_size) {
12140 dtrace_dof_error(dof, "bad relocation offset");
12141 return (-1);
12142 }
12143
12144 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) {
12145 dtrace_dof_error(dof, "misaligned setx relo");
12146 return (-1);
12147 }
12148
12149 *(uint64_t *)taddr += ubase;
12150 break;
12151 default:
12152 dtrace_dof_error(dof, "invalid relocation type");
12153 return (-1);
12154 }
12155
12156 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize);
12157 }
12158
12159 return (0);
12160}
12161
12162/*
12163 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
12164 * header: it should be at the front of a memory region that is at least
12165 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
12166 * size. It need not be validated in any other way.
12167 */
12168static int
12169dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr,
12170 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes)
12171{
12172 uint64_t len = dof->dofh_loadsz, seclen;
12173 uintptr_t daddr = (uintptr_t)dof;
12174 dtrace_ecbdesc_t *ep;
12175 dtrace_enabling_t *enab;
12176 uint_t i;
12177
12178 ASSERT(MUTEX_HELD(&dtrace_lock));
12179 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t));
12180
12181 /*
12182 * Check the DOF header identification bytes. In addition to checking
12183 * valid settings, we also verify that unused bits/bytes are zeroed so
12184 * we can use them later without fear of regressing existing binaries.
12185 */
12186 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0],
12187 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) {
12188 dtrace_dof_error(dof, "DOF magic string mismatch");
12189 return (-1);
12190 }
12191
12192 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 &&
12193 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) {
12194 dtrace_dof_error(dof, "DOF has invalid data model");
12195 return (-1);
12196 }
12197
12198 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) {
12199 dtrace_dof_error(dof, "DOF encoding mismatch");
12200 return (-1);
12201 }
12202
12203 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
12204 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) {
12205 dtrace_dof_error(dof, "DOF version mismatch");
12206 return (-1);
12207 }
12208
12209 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) {
12210 dtrace_dof_error(dof, "DOF uses unsupported instruction set");
12211 return (-1);
12212 }
12213
12214 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) {
12215 dtrace_dof_error(dof, "DOF uses too many integer registers");
12216 return (-1);
12217 }
12218
12219 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) {
12220 dtrace_dof_error(dof, "DOF uses too many tuple registers");
12221 return (-1);
12222 }
12223
12224 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) {
12225 if (dof->dofh_ident[i] != 0) {
12226 dtrace_dof_error(dof, "DOF has invalid ident byte set");
12227 return (-1);
12228 }
12229 }
12230
12231 if (dof->dofh_flags & ~DOF_FL_VALID) {
12232 dtrace_dof_error(dof, "DOF has invalid flag bits set");
12233 return (-1);
12234 }
12235
12236 if (dof->dofh_secsize == 0) {
12237 dtrace_dof_error(dof, "zero section header size");
12238 return (-1);
12239 }
12240
12241 /*
12242 * Check that the section headers don't exceed the amount of DOF
12243 * data. Note that we cast the section size and number of sections
12244 * to uint64_t's to prevent possible overflow in the multiplication.
12245 */
12246 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize;
12247
12248 if (dof->dofh_secoff > len || seclen > len ||
12249 dof->dofh_secoff + seclen > len) {
12250 dtrace_dof_error(dof, "truncated section headers");
12251 return (-1);
12252 }
12253
12254 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) {
12255 dtrace_dof_error(dof, "misaligned section headers");
12256 return (-1);
12257 }
12258
12259 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) {
12260 dtrace_dof_error(dof, "misaligned section size");
12261 return (-1);
12262 }
12263
12264 /*
12265 * Take an initial pass through the section headers to be sure that
12266 * the headers don't have stray offsets. If the 'noprobes' flag is
12267 * set, do not permit sections relating to providers, probes, or args.
12268 */
12269 for (i = 0; i < dof->dofh_secnum; i++) {
12270 dof_sec_t *sec = (dof_sec_t *)(daddr +
12271 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12272
12273 if (noprobes) {
12274 switch (sec->dofs_type) {
12275 case DOF_SECT_PROVIDER:
12276 case DOF_SECT_PROBES:
12277 case DOF_SECT_PRARGS:
12278 case DOF_SECT_PROFFS:
12279 dtrace_dof_error(dof, "illegal sections "
12280 "for enabling");
12281 return (-1);
12282 }
12283 }
12284
12285 if (DOF_SEC_ISLOADABLE(sec->dofs_type) &&
12286 !(sec->dofs_flags & DOF_SECF_LOAD)) {
12287 dtrace_dof_error(dof, "loadable section with load "
12288 "flag unset");
12289 return (-1);
12290 }
12291
12292 if (!(sec->dofs_flags & DOF_SECF_LOAD))
12293 continue; /* just ignore non-loadable sections */
12294
12295 if (sec->dofs_align & (sec->dofs_align - 1)) {
12296 dtrace_dof_error(dof, "bad section alignment");
12297 return (-1);
12298 }
12299
12300 if (sec->dofs_offset & (sec->dofs_align - 1)) {
12301 dtrace_dof_error(dof, "misaligned section");
12302 return (-1);
12303 }
12304
12305 if (sec->dofs_offset > len || sec->dofs_size > len ||
12306 sec->dofs_offset + sec->dofs_size > len) {
12307 dtrace_dof_error(dof, "corrupt section header");
12308 return (-1);
12309 }
12310
12311 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr +
12312 sec->dofs_offset + sec->dofs_size - 1) != '\0') {
12313 dtrace_dof_error(dof, "non-terminating string table");
12314 return (-1);
12315 }
12316 }
12317
12318 /*
12319 * Take a second pass through the sections and locate and perform any
12320 * relocations that are present. We do this after the first pass to
12321 * be sure that all sections have had their headers validated.
12322 */
12323 for (i = 0; i < dof->dofh_secnum; i++) {
12324 dof_sec_t *sec = (dof_sec_t *)(daddr +
12325 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12326
12327 if (!(sec->dofs_flags & DOF_SECF_LOAD))
12328 continue; /* skip sections that are not loadable */
12329
12330 switch (sec->dofs_type) {
12331 case DOF_SECT_URELHDR:
12332 if (dtrace_dof_relocate(dof, sec, ubase) != 0)
12333 return (-1);
12334 break;
12335 }
12336 }
12337
12338 if ((enab = *enabp) == NULL)
12339 enab = *enabp = dtrace_enabling_create(vstate);
12340
12341 for (i = 0; i < dof->dofh_secnum; i++) {
12342 dof_sec_t *sec = (dof_sec_t *)(daddr +
12343 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12344
12345 if (sec->dofs_type != DOF_SECT_ECBDESC)
12346 continue;
12347
12348 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) {
12349 dtrace_enabling_destroy(enab);
12350 *enabp = NULL;
12351 return (-1);
12352 }
12353
12354 dtrace_enabling_add(enab, ep);
12355 }
12356
12357 return (0);
12358}
12359
12360/*
12361 * Process DOF for any options. This routine assumes that the DOF has been
12362 * at least processed by dtrace_dof_slurp().
12363 */
12364static int
12365dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state)
12366{
12367 int i, rval;
12368 uint32_t entsize;
12369 size_t offs;
12370 dof_optdesc_t *desc;
12371
12372 for (i = 0; VBDTCAST(unsigned)i < dof->dofh_secnum; i++) {
12373 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof +
12374 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12375
12376 if (sec->dofs_type != DOF_SECT_OPTDESC)
12377 continue;
12378
12379 if (sec->dofs_align != sizeof (uint64_t)) {
12380 dtrace_dof_error(dof, "bad alignment in "
12381 "option description");
12382 return (EINVAL);
12383 }
12384
12385 if ((entsize = sec->dofs_entsize) == 0) {
12386 dtrace_dof_error(dof, "zeroed option entry size");
12387 return (EINVAL);
12388 }
12389
12390 if (entsize < sizeof (dof_optdesc_t)) {
12391 dtrace_dof_error(dof, "bad option entry size");
12392 return (EINVAL);
12393 }
12394
12395 for (offs = 0; offs < sec->dofs_size; offs += entsize) {
12396 desc = (dof_optdesc_t *)((uintptr_t)dof +
12397 (uintptr_t)sec->dofs_offset + offs);
12398
12399 if (desc->dofo_strtab != DOF_SECIDX_NONE) {
12400 dtrace_dof_error(dof, "non-zero option string");
12401 return (EINVAL);
12402 }
12403
12404 if (desc->dofo_value == VBDTCAST(uint64_t)DTRACEOPT_UNSET) {
12405 dtrace_dof_error(dof, "unset option");
12406 return (EINVAL);
12407 }
12408
12409 if ((rval = dtrace_state_option(state,
12410 desc->dofo_option, desc->dofo_value)) != 0) {
12411 dtrace_dof_error(dof, "rejected option");
12412 return (rval);
12413 }
12414 }
12415 }
12416
12417 return (0);
12418}
12419
12420/*
12421 * DTrace Consumer State Functions
12422 */
12423VBDTSTATIC int
12424dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size)
12425{
12426 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize;
12427 void *base;
12428 uintptr_t limit;
12429 dtrace_dynvar_t *dvar, *next, *start;
12430 VBDTTYPE(size_t,int) i;
12431
12432 ASSERT(MUTEX_HELD(&dtrace_lock));
12433 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL);
12434
12435 bzero(dstate, sizeof (dtrace_dstate_t));
12436
12437 if ((dstate->dtds_chunksize = chunksize) == 0)
12438 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE;
12439
12440 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)))
12441 size = min;
12442
12443 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
12444 return (ENOMEM);
12445
12446 dstate->dtds_size = size;
12447 dstate->dtds_base = base;
12448 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP);
12449 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t));
12450
12451 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t));
12452
12453 if (hashsize != 1 && (hashsize & 1))
12454 hashsize--;
12455
12456 dstate->dtds_hashsize = hashsize;
12457 dstate->dtds_hash = dstate->dtds_base;
12458
12459 /*
12460 * Set all of our hash buckets to point to the single sink, and (if
12461 * it hasn't already been set), set the sink's hash value to be the
12462 * sink sentinel value. The sink is needed for dynamic variable
12463 * lookups to know that they have iterated over an entire, valid hash
12464 * chain.
12465 */
12466 for (i = 0; i < hashsize; i++)
12467 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink;
12468
12469 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK)
12470 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK;
12471
12472 /*
12473 * Determine number of active CPUs. Divide free list evenly among
12474 * active CPUs.
12475 */
12476 start = (dtrace_dynvar_t *)
12477 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t));
12478 limit = (uintptr_t)base + size;
12479
12480 maxper = (limit - (uintptr_t)start) / NCPU;
12481 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize;
12482
12483 for (i = 0; i < NCPU; i++) {
12484 dstate->dtds_percpu[i].dtdsc_free = dvar = start;
12485
12486 /*
12487 * If we don't even have enough chunks to make it once through
12488 * NCPUs, we're just going to allocate everything to the first
12489 * CPU. And if we're on the last CPU, we're going to allocate
12490 * whatever is left over. In either case, we set the limit to
12491 * be the limit of the dynamic variable space.
12492 */
12493 if (maxper == 0 || i == NCPU - 1) {
12494 limit = (uintptr_t)base + size;
12495 start = NULL;
12496 } else {
12497 limit = (uintptr_t)start + maxper;
12498 start = (dtrace_dynvar_t *)limit;
12499 }
12500
12501 ASSERT(limit <= (uintptr_t)base + size);
12502
12503 for (;;) {
12504 next = (dtrace_dynvar_t *)((uintptr_t)dvar +
12505 dstate->dtds_chunksize);
12506
12507 if ((uintptr_t)next + dstate->dtds_chunksize >= limit)
12508 break;
12509
12510 dvar->dtdv_next = next;
12511 dvar = next;
12512 }
12513
12514 if (maxper == 0)
12515 break;
12516 }
12517
12518 return (0);
12519}
12520
12521VBDTSTATIC void
12522dtrace_dstate_fini(dtrace_dstate_t *dstate)
12523{
12524 ASSERT(MUTEX_HELD(&cpu_lock));
12525
12526 if (dstate->dtds_base == NULL)
12527 return;
12528
12529 kmem_free(dstate->dtds_base, dstate->dtds_size);
12530 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu);
12531}
12532
12533static void
12534dtrace_vstate_fini(dtrace_vstate_t *vstate)
12535{
12536 /*
12537 * Logical XOR, where are you?
12538 */
12539 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL));
12540
12541 if (vstate->dtvs_nglobals > 0) {
12542 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals *
12543 sizeof (dtrace_statvar_t *));
12544 }
12545
12546 if (vstate->dtvs_ntlocals > 0) {
12547 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals *
12548 sizeof (dtrace_difv_t));
12549 }
12550
12551 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL));
12552
12553 if (vstate->dtvs_nlocals > 0) {
12554 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals *
12555 sizeof (dtrace_statvar_t *));
12556 }
12557}
12558
12559static void
12560dtrace_state_clean(dtrace_state_t *state)
12561{
12562 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
12563 return;
12564
12565 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
12566 dtrace_speculation_clean(state);
12567}
12568#ifdef VBOX
12569static DECLCALLBACK(void) dtrace_state_clean_timer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
12570{
12571 dtrace_state_clean((dtrace_state_t *)pvUser);
12572 NOREF(pTimer); NOREF(iTick);
12573}
12574#endif
12575
12576static void
12577dtrace_state_deadman(dtrace_state_t *state)
12578{
12579 hrtime_t now;
12580
12581 dtrace_sync();
12582
12583 now = dtrace_gethrtime();
12584
12585 if (state != dtrace_anon.dta_state &&
12586 now - state->dts_laststatus >= dtrace_deadman_user)
12587 return;
12588
12589 /*
12590 * We must be sure that dts_alive never appears to be less than the
12591 * value upon entry to dtrace_state_deadman(), and because we lack a
12592 * dtrace_cas64(), we cannot store to it atomically. We thus instead
12593 * store INT64_MAX to it, followed by a memory barrier, followed by
12594 * the new value. This assures that dts_alive never appears to be
12595 * less than its true value, regardless of the order in which the
12596 * stores to the underlying storage are issued.
12597 */
12598 state->dts_alive = INT64_MAX;
12599 dtrace_membar_producer();
12600 state->dts_alive = now;
12601}
12602
12603#ifdef VBOX
12604static DECLCALLBACK(void) dtrace_state_deadman_timer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
12605{
12606 dtrace_state_deadman((dtrace_state_t *)pvUser);
12607 NOREF(pTimer); NOREF(iTick);
12608}
12609#endif
12610
12611VBDTSTATIC dtrace_state_t *
12612#ifdef VBOX
12613dtrace_state_create(cred_t *cr)
12614#else
12615dtrace_state_create(dev_t *devp, cred_t *cr)
12616#endif
12617{
12618#ifndef VBOX
12619 minor_t minor;
12620 major_t major;
12621#endif
12622 char c[30];
12623 dtrace_state_t *state;
12624 dtrace_optval_t *opt;
12625 int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
12626
12627 ASSERT(MUTEX_HELD(&dtrace_lock));
12628 ASSERT(MUTEX_HELD(&cpu_lock));
12629
12630#ifndef VBOX
12631 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1,
12632 VM_BESTFIT | VM_SLEEP);
12633
12634 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) {
12635 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
12636 return (NULL);
12637 }
12638
12639 state = ddi_get_soft_state(dtrace_softstate, minor);
12640#else
12641 state = kmem_zalloc(sizeof (*state), KM_SLEEP);
12642 if (!state) {
12643 return (NULL);
12644 }
12645#endif
12646 state->dts_epid = DTRACE_EPIDNONE + 1;
12647
12648#ifndef VBOX
12649 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor);
12650#else
12651 (void) snprintf(c, sizeof (c), "dtrace_aggid_%p", state);
12652#endif
12653#ifndef VBOX /* Avoid idProbe = UINT32_MAX as it is used as invalid value by VTG. */
12654 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1,
12655 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
12656#else
12657 state->dts_aggid_arena = vmem_create(c, (void *)(uintptr_t)1, _1G, 1,
12658 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
12659#endif
12660
12661#ifndef VBOX
12662 if (devp != NULL) {
12663 major = getemajor(*devp);
12664 } else {
12665 major = ddi_driver_major(dtrace_devi);
12666 }
12667
12668 state->dts_dev = makedevice(major, minor);
12669
12670 if (devp != NULL)
12671 *devp = state->dts_dev;
12672#endif
12673
12674 /*
12675 * We allocate NCPU buffers. On the one hand, this can be quite
12676 * a bit of memory per instance (nearly 36K on a Starcat). On the
12677 * other hand, it saves an additional memory reference in the probe
12678 * path.
12679 */
12680 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
12681 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
12682 state->dts_cleaner = CYCLIC_NONE;
12683 state->dts_deadman = CYCLIC_NONE;
12684 state->dts_vstate.dtvs_state = state;
12685
12686 for (i = 0; i < DTRACEOPT_MAX; i++)
12687 state->dts_options[i] = DTRACEOPT_UNSET;
12688
12689 /*
12690 * Set the default options.
12691 */
12692 opt = state->dts_options;
12693 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH;
12694 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO;
12695 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default;
12696 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default;
12697 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL;
12698 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default;
12699 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default;
12700 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default;
12701 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default;
12702 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default;
12703 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default;
12704 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default;
12705 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default;
12706 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default;
12707
12708 state->dts_activity = DTRACE_ACTIVITY_INACTIVE;
12709
12710 /*
12711 * Depending on the user credentials, we set flag bits which alter probe
12712 * visibility or the amount of destructiveness allowed. In the case of
12713 * actual anonymous tracing, or the possession of all privileges, all of
12714 * the normal checks are bypassed.
12715 */
12716 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
12717 state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
12718 state->dts_cred.dcr_action = DTRACE_CRA_ALL;
12719 } else {
12720 /*
12721 * Set up the credentials for this instantiation. We take a
12722 * hold on the credential to prevent it from disappearing on
12723 * us; this in turn prevents the zone_t referenced by this
12724 * credential from disappearing. This means that we can
12725 * examine the credential and the zone from probe context.
12726 */
12727 crhold(cr);
12728 state->dts_cred.dcr_cred = cr;
12729
12730 /*
12731 * CRA_PROC means "we have *some* privilege for dtrace" and
12732 * unlocks the use of variables like pid, zonename, etc.
12733 */
12734 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) ||
12735 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
12736 state->dts_cred.dcr_action |= DTRACE_CRA_PROC;
12737 }
12738
12739 /*
12740 * dtrace_user allows use of syscall and profile providers.
12741 * If the user also has proc_owner and/or proc_zone, we
12742 * extend the scope to include additional visibility and
12743 * destructive power.
12744 */
12745 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) {
12746 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) {
12747 state->dts_cred.dcr_visible |=
12748 DTRACE_CRV_ALLPROC;
12749
12750 state->dts_cred.dcr_action |=
12751 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
12752 }
12753
12754 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) {
12755 state->dts_cred.dcr_visible |=
12756 DTRACE_CRV_ALLZONE;
12757
12758 state->dts_cred.dcr_action |=
12759 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
12760 }
12761
12762 /*
12763 * If we have all privs in whatever zone this is,
12764 * we can do destructive things to processes which
12765 * have altered credentials.
12766 */
12767 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
12768 cr->cr_zone->zone_privset)) {
12769 state->dts_cred.dcr_action |=
12770 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
12771 }
12772 }
12773
12774 /*
12775 * Holding the dtrace_kernel privilege also implies that
12776 * the user has the dtrace_user privilege from a visibility
12777 * perspective. But without further privileges, some
12778 * destructive actions are not available.
12779 */
12780 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) {
12781 /*
12782 * Make all probes in all zones visible. However,
12783 * this doesn't mean that all actions become available
12784 * to all zones.
12785 */
12786 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL |
12787 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE;
12788
12789 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL |
12790 DTRACE_CRA_PROC;
12791 /*
12792 * Holding proc_owner means that destructive actions
12793 * for *this* zone are allowed.
12794 */
12795 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
12796 state->dts_cred.dcr_action |=
12797 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
12798
12799 /*
12800 * Holding proc_zone means that destructive actions
12801 * for this user/group ID in all zones is allowed.
12802 */
12803 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
12804 state->dts_cred.dcr_action |=
12805 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
12806
12807 /*
12808 * If we have all privs in whatever zone this is,
12809 * we can do destructive things to processes which
12810 * have altered credentials.
12811 */
12812 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
12813 cr->cr_zone->zone_privset)) {
12814 state->dts_cred.dcr_action |=
12815 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
12816 }
12817 }
12818
12819 /*
12820 * Holding the dtrace_proc privilege gives control over fasttrap
12821 * and pid providers. We need to grant wider destructive
12822 * privileges in the event that the user has proc_owner and/or
12823 * proc_zone.
12824 */
12825 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
12826 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
12827 state->dts_cred.dcr_action |=
12828 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
12829
12830 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
12831 state->dts_cred.dcr_action |=
12832 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
12833 }
12834 }
12835
12836 return (state);
12837}
12838
12839static int
12840dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which)
12841{
12842 dtrace_optval_t *opt = state->dts_options, size;
12843 processorid_t cpu VBDTUNASS((processorid_t)DTRACE_CPUALL);
12844 int flags = 0, rval;
12845
12846 ASSERT(MUTEX_HELD(&dtrace_lock));
12847 ASSERT(MUTEX_HELD(&cpu_lock));
12848 ASSERT(which < DTRACEOPT_MAX);
12849 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE ||
12850 (state == dtrace_anon.dta_state &&
12851 state->dts_activity == DTRACE_ACTIVITY_ACTIVE));
12852
12853 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0)
12854 return (0);
12855
12856 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
12857 cpu = opt[DTRACEOPT_CPU];
12858
12859 if (which == DTRACEOPT_SPECSIZE)
12860 flags |= DTRACEBUF_NOSWITCH;
12861
12862 if (which == DTRACEOPT_BUFSIZE) {
12863 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING)
12864 flags |= DTRACEBUF_RING;
12865
12866 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL)
12867 flags |= DTRACEBUF_FILL;
12868
12869 if (state != dtrace_anon.dta_state ||
12870 state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
12871 flags |= DTRACEBUF_INACTIVE;
12872 }
12873
12874 for (size = opt[which]; size >= VBDTCAST(dtrace_optval_t)sizeof (uint64_t); size >>= 1) {
12875 /*
12876 * The size must be 8-byte aligned. If the size is not 8-byte
12877 * aligned, drop it down by the difference.
12878 */
12879 if (size & (sizeof (uint64_t) - 1))
12880 size -= size & (sizeof (uint64_t) - 1);
12881
12882 if (size < state->dts_reserve) {
12883 /*
12884 * Buffers always must be large enough to accommodate
12885 * their prereserved space. We return E2BIG instead
12886 * of ENOMEM in this case to allow for user-level
12887 * software to differentiate the cases.
12888 */
12889 return (E2BIG);
12890 }
12891
12892 rval = dtrace_buffer_alloc(buf, size, flags, cpu);
12893
12894 if (rval != ENOMEM) {
12895 opt[which] = size;
12896 return (rval);
12897 }
12898
12899 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
12900 return (rval);
12901 }
12902
12903 return (ENOMEM);
12904}
12905
12906static int
12907dtrace_state_buffers(dtrace_state_t *state)
12908{
12909 dtrace_speculation_t *spec = state->dts_speculations;
12910 int rval, i;
12911
12912 if ((rval = dtrace_state_buffer(state, state->dts_buffer,
12913 DTRACEOPT_BUFSIZE)) != 0)
12914 return (rval);
12915
12916 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer,
12917 DTRACEOPT_AGGSIZE)) != 0)
12918 return (rval);
12919
12920 for (i = 0; i < state->dts_nspeculations; i++) {
12921 if ((rval = dtrace_state_buffer(state,
12922 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0)
12923 return (rval);
12924 }
12925
12926 return (0);
12927}
12928
12929static void
12930dtrace_state_prereserve(dtrace_state_t *state)
12931{
12932 dtrace_ecb_t *ecb;
12933 dtrace_probe_t *probe;
12934
12935 state->dts_reserve = 0;
12936
12937 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
12938 return;
12939
12940 /*
12941 * If our buffer policy is a "fill" buffer policy, we need to set the
12942 * prereserved space to be the space required by the END probes.
12943 */
12944 probe = dtrace_probes[dtrace_probeid_end - 1];
12945 ASSERT(probe != NULL);
12946
12947 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
12948 if (ecb->dte_state != state)
12949 continue;
12950
12951 state->dts_reserve += VBDTCAST(uint32_t)ecb->dte_needed + ecb->dte_alignment;
12952 }
12953}
12954
12955static int
12956dtrace_state_go(dtrace_state_t *state, processorid_t *cpu)
12957{
12958 dtrace_optval_t *opt = state->dts_options, sz, nspec;
12959 dtrace_speculation_t *spec;
12960 dtrace_buffer_t *buf;
12961#ifndef VBOX
12962 cyc_handler_t hdlr;
12963 cyc_time_t when;
12964#endif
12965 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t);
12966 dtrace_icookie_t cookie;
12967
12968 mutex_enter(&cpu_lock);
12969 mutex_enter(&dtrace_lock);
12970
12971 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
12972 rval = EBUSY;
12973 goto out;
12974 }
12975
12976 /*
12977 * Before we can perform any checks, we must prime all of the
12978 * retained enablings that correspond to this state.
12979 */
12980 dtrace_enabling_prime(state);
12981
12982 if (state->dts_destructive && !state->dts_cred.dcr_destructive) {
12983 rval = EACCES;
12984 goto out;
12985 }
12986
12987 dtrace_state_prereserve(state);
12988
12989 /*
12990 * Now we want to do is try to allocate our speculations.
12991 * We do not automatically resize the number of speculations; if
12992 * this fails, we will fail the operation.
12993 */
12994 nspec = opt[DTRACEOPT_NSPEC];
12995 ASSERT(nspec != DTRACEOPT_UNSET);
12996
12997 if (nspec > INT_MAX) {
12998 rval = ENOMEM;
12999 goto out;
13000 }
13001
13002 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP);
13003
13004 if (spec == NULL) {
13005 rval = ENOMEM;
13006 goto out;
13007 }
13008
13009 state->dts_speculations = spec;
13010 state->dts_nspeculations = (int)nspec;
13011
13012 for (i = 0; i < nspec; i++) {
13013 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) {
13014 rval = ENOMEM;
13015 goto err;
13016 }
13017
13018 spec[i].dtsp_buffer = buf;
13019 }
13020
13021 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) {
13022 if (dtrace_anon.dta_state == NULL) {
13023 rval = ENOENT;
13024 goto out;
13025 }
13026
13027 if (state->dts_necbs != 0) {
13028 rval = EALREADY;
13029 goto out;
13030 }
13031
13032 state->dts_anon = dtrace_anon_grab();
13033 ASSERT(state->dts_anon != NULL);
13034 state = state->dts_anon;
13035
13036 /*
13037 * We want "grabanon" to be set in the grabbed state, so we'll
13038 * copy that option value from the grabbing state into the
13039 * grabbed state.
13040 */
13041 state->dts_options[DTRACEOPT_GRABANON] =
13042 opt[DTRACEOPT_GRABANON];
13043
13044 *cpu = dtrace_anon.dta_beganon;
13045
13046 /*
13047 * If the anonymous state is active (as it almost certainly
13048 * is if the anonymous enabling ultimately matched anything),
13049 * we don't allow any further option processing -- but we
13050 * don't return failure.
13051 */
13052 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
13053 goto out;
13054 }
13055
13056 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET &&
13057 opt[DTRACEOPT_AGGSIZE] != 0) {
13058 if (state->dts_aggregations == NULL) {
13059 /*
13060 * We're not going to create an aggregation buffer
13061 * because we don't have any ECBs that contain
13062 * aggregations -- set this option to 0.
13063 */
13064 opt[DTRACEOPT_AGGSIZE] = 0;
13065 } else {
13066 /*
13067 * If we have an aggregation buffer, we must also have
13068 * a buffer to use as scratch.
13069 */
13070 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET ||
13071 opt[DTRACEOPT_BUFSIZE] < VBDTCAST(dtrace_optval_t)state->dts_needed) {
13072 opt[DTRACEOPT_BUFSIZE] = state->dts_needed;
13073 }
13074 }
13075 }
13076
13077 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET &&
13078 opt[DTRACEOPT_SPECSIZE] != 0) {
13079 if (!state->dts_speculates) {
13080 /*
13081 * We're not going to create speculation buffers
13082 * because we don't have any ECBs that actually
13083 * speculate -- set the speculation size to 0.
13084 */
13085 opt[DTRACEOPT_SPECSIZE] = 0;
13086 }
13087 }
13088
13089 /*
13090 * The bare minimum size for any buffer that we're actually going to
13091 * do anything to is sizeof (uint64_t).
13092 */
13093 sz = sizeof (uint64_t);
13094
13095 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) ||
13096 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) ||
13097 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) {
13098 /*
13099 * A buffer size has been explicitly set to 0 (or to a size
13100 * that will be adjusted to 0) and we need the space -- we
13101 * need to return failure. We return ENOSPC to differentiate
13102 * it from failing to allocate a buffer due to failure to meet
13103 * the reserve (for which we return E2BIG).
13104 */
13105 rval = ENOSPC;
13106 goto out;
13107 }
13108
13109 if ((rval = dtrace_state_buffers(state)) != 0)
13110 goto err;
13111
13112 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET)
13113 sz = dtrace_dstate_defsize;
13114
13115 do {
13116 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz);
13117
13118 if (rval == 0)
13119 break;
13120
13121 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
13122 goto err;
13123 } while (sz >>= 1);
13124
13125 opt[DTRACEOPT_DYNVARSIZE] = sz;
13126
13127 if (rval != 0)
13128 goto err;
13129
13130 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max)
13131 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max;
13132
13133 if (opt[DTRACEOPT_CLEANRATE] == 0)
13134 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
13135
13136 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min)
13137 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min;
13138
13139 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max)
13140 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
13141
13142#ifndef VBOX
13143 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean;
13144 hdlr.cyh_arg = state;
13145 hdlr.cyh_level = CY_LOW_LEVEL;
13146
13147 when.cyt_when = 0;
13148 when.cyt_interval = opt[DTRACEOPT_CLEANRATE];
13149
13150 state->dts_cleaner = cyclic_add(&hdlr, &when);
13151
13152 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman;
13153 hdlr.cyh_arg = state;
13154 hdlr.cyh_level = CY_LOW_LEVEL;
13155
13156 when.cyt_when = 0;
13157 when.cyt_interval = dtrace_deadman_interval;
13158
13159 state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
13160 state->dts_deadman = cyclic_add(&hdlr, &when);
13161#else /* VBOX */
13162
13163 rval = RTTimerCreateEx(&state->dts_cleaner, opt[DTRACEOPT_CLEANRATE],
13164 RTTIMER_FLAGS_CPU_ANY, dtrace_state_clean_timer, state);
13165 if (RT_FAILURE(rval)) {
13166 rval = RTErrConvertToErrno(rval);
13167 goto err;
13168 }
13169
13170 state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
13171 rval = RTTimerCreateEx(&state->dts_deadman, dtrace_deadman_interval,
13172 RTTIMER_FLAGS_CPU_ANY, dtrace_state_deadman_timer, state);
13173 if (RT_FAILURE(rval)) {
13174 RTTimerDestroy(state->dts_cleaner);
13175 state->dts_cleaner = CYCLIC_NONE;
13176 state->dts_deadman = CYCLIC_NONE;
13177 rval = RTErrConvertToErrno(rval);
13178 goto err;
13179 }
13180
13181 rval = RTTimerStart(state->dts_cleaner, 0);
13182 if (RT_SUCCESS(rval))
13183 rval = RTTimerStart(state->dts_deadman, 0);
13184 if (RT_FAILURE(rval)) {
13185 rval = RTErrConvertToErrno(rval);
13186 goto err;
13187 }
13188#endif /* VBOX */
13189
13190 state->dts_activity = DTRACE_ACTIVITY_WARMUP;
13191
13192 /*
13193 * Now it's time to actually fire the BEGIN probe. We need to disable
13194 * interrupts here both to record the CPU on which we fired the BEGIN
13195 * probe (the data from this CPU will be processed first at user
13196 * level) and to manually activate the buffer for this CPU.
13197 */
13198 cookie = dtrace_interrupt_disable();
13199 *cpu = VBDT_GET_CPUID();
13200 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE);
13201 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
13202
13203 dtrace_probe(dtrace_probeid_begin,
13204 (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
13205 dtrace_interrupt_enable(cookie);
13206 /*
13207 * We may have had an exit action from a BEGIN probe; only change our
13208 * state to ACTIVE if we're still in WARMUP.
13209 */
13210 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP ||
13211 state->dts_activity == DTRACE_ACTIVITY_DRAINING);
13212
13213 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP)
13214 state->dts_activity = DTRACE_ACTIVITY_ACTIVE;
13215
13216 /*
13217 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
13218 * want each CPU to transition its principal buffer out of the
13219 * INACTIVE state. Doing this assures that no CPU will suddenly begin
13220 * processing an ECB halfway down a probe's ECB chain; all CPUs will
13221 * atomically transition from processing none of a state's ECBs to
13222 * processing all of them.
13223 */
13224#ifndef VBOX
13225 dtrace_xcall(DTRACE_CPUALL,
13226 (dtrace_xcall_t)dtrace_buffer_activate, state);
13227#else
13228 RTMpOnAll(dtrace_buffer_activate_wrapper, state, NULL);
13229#endif
13230 goto out;
13231
13232err:
13233 dtrace_buffer_free(state->dts_buffer);
13234 dtrace_buffer_free(state->dts_aggbuffer);
13235
13236 if ((nspec = state->dts_nspeculations) == 0) {
13237 ASSERT(state->dts_speculations == NULL);
13238 goto out;
13239 }
13240
13241 spec = state->dts_speculations;
13242 ASSERT(spec != NULL);
13243
13244 for (i = 0; i < state->dts_nspeculations; i++) {
13245 if ((buf = spec[i].dtsp_buffer) == NULL)
13246 break;
13247
13248 dtrace_buffer_free(buf);
13249 kmem_free(buf, bufsize);
13250 }
13251
13252 kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
13253 state->dts_nspeculations = 0;
13254 state->dts_speculations = NULL;
13255
13256out:
13257 mutex_exit(&dtrace_lock);
13258 mutex_exit(&cpu_lock);
13259
13260 return (rval);
13261}
13262
13263static int
13264dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu)
13265{
13266 dtrace_icookie_t cookie;
13267
13268 ASSERT(MUTEX_HELD(&dtrace_lock));
13269
13270 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE &&
13271 state->dts_activity != DTRACE_ACTIVITY_DRAINING)
13272 return (EINVAL);
13273
13274 /*
13275 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
13276 * to be sure that every CPU has seen it. See below for the details
13277 * on why this is done.
13278 */
13279 state->dts_activity = DTRACE_ACTIVITY_DRAINING;
13280 dtrace_sync();
13281
13282 /*
13283 * By this point, it is impossible for any CPU to be still processing
13284 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to
13285 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
13286 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe()
13287 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
13288 * iff we're in the END probe.
13289 */
13290 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN;
13291 dtrace_sync();
13292 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN);
13293
13294 /*
13295 * Finally, we can release the reserve and call the END probe. We
13296 * disable interrupts across calling the END probe to allow us to
13297 * return the CPU on which we actually called the END probe. This
13298 * allows user-land to be sure that this CPU's principal buffer is
13299 * processed last.
13300 */
13301 state->dts_reserve = 0;
13302
13303 cookie = dtrace_interrupt_disable();
13304 *cpu = VBDT_GET_CPUID();
13305 dtrace_probe(dtrace_probeid_end,
13306 (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
13307 dtrace_interrupt_enable(cookie);
13308
13309 state->dts_activity = DTRACE_ACTIVITY_STOPPED;
13310 dtrace_sync();
13311
13312 return (0);
13313}
13314
13315static int
13316dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option,
13317 dtrace_optval_t val)
13318{
13319 ASSERT(MUTEX_HELD(&dtrace_lock));
13320
13321 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
13322 return (EBUSY);
13323
13324 if (option >= DTRACEOPT_MAX)
13325 return (EINVAL);
13326
13327 if (option != DTRACEOPT_CPU && val < 0)
13328 return (EINVAL);
13329
13330 switch (option) {
13331 case DTRACEOPT_DESTRUCTIVE:
13332 if (dtrace_destructive_disallow)
13333 return (EACCES);
13334
13335 state->dts_cred.dcr_destructive = 1;
13336 break;
13337
13338 case DTRACEOPT_BUFSIZE:
13339 case DTRACEOPT_DYNVARSIZE:
13340 case DTRACEOPT_AGGSIZE:
13341 case DTRACEOPT_SPECSIZE:
13342 case DTRACEOPT_STRSIZE:
13343 if (val < 0)
13344 return (EINVAL);
13345
13346 if (val >= LONG_MAX) {
13347 /*
13348 * If this is an otherwise negative value, set it to
13349 * the highest multiple of 128m less than LONG_MAX.
13350 * Technically, we're adjusting the size without
13351 * regard to the buffer resizing policy, but in fact,
13352 * this has no effect -- if we set the buffer size to
13353 * ~LONG_MAX and the buffer policy is ultimately set to
13354 * be "manual", the buffer allocation is guaranteed to
13355 * fail, if only because the allocation requires two
13356 * buffers. (We set the the size to the highest
13357 * multiple of 128m because it ensures that the size
13358 * will remain a multiple of a megabyte when
13359 * repeatedly halved -- all the way down to 15m.)
13360 */
13361 val = LONG_MAX - (1 << 27) + 1;
13362 }
13363 }
13364
13365 state->dts_options[option] = val;
13366
13367 return (0);
13368}
13369
13370static void
13371dtrace_state_destroy(dtrace_state_t *state)
13372{
13373 dtrace_ecb_t *ecb;
13374 dtrace_vstate_t *vstate = &state->dts_vstate;
13375#ifndef VBOX
13376 minor_t minor = getminor(state->dts_dev);
13377#endif
13378 int i, bufsize = NCPU * sizeof (dtrace_buffer_t);
13379 dtrace_speculation_t *spec = state->dts_speculations;
13380 int nspec = state->dts_nspeculations;
13381 uint32_t match;
13382
13383 ASSERT(MUTEX_HELD(&dtrace_lock));
13384 ASSERT(MUTEX_HELD(&cpu_lock));
13385
13386 /*
13387 * First, retract any retained enablings for this state.
13388 */
13389 dtrace_enabling_retract(state);
13390 ASSERT(state->dts_nretained == 0);
13391
13392 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE ||
13393 state->dts_activity == DTRACE_ACTIVITY_DRAINING) {
13394 /*
13395 * We have managed to come into dtrace_state_destroy() on a
13396 * hot enabling -- almost certainly because of a disorderly
13397 * shutdown of a consumer. (That is, a consumer that is
13398 * exiting without having called dtrace_stop().) In this case,
13399 * we're going to set our activity to be KILLED, and then
13400 * issue a sync to be sure that everyone is out of probe
13401 * context before we start blowing away ECBs.
13402 */
13403 state->dts_activity = DTRACE_ACTIVITY_KILLED;
13404 dtrace_sync();
13405 }
13406
13407 /*
13408 * Release the credential hold we took in dtrace_state_create().
13409 */
13410 if (state->dts_cred.dcr_cred != NULL)
13411 crfree(state->dts_cred.dcr_cred);
13412
13413 /*
13414 * Now we can safely disable and destroy any enabled probes. Because
13415 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
13416 * (especially if they're all enabled), we take two passes through the
13417 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and
13418 * in the second we disable whatever is left over.
13419 */
13420 for (match = DTRACE_PRIV_KERNEL; ; match = 0) {
13421 for (i = 0; i < state->dts_necbs; i++) {
13422 if ((ecb = state->dts_ecbs[i]) == NULL)
13423 continue;
13424
13425 if (match && ecb->dte_probe != NULL) {
13426 dtrace_probe_t *probe = ecb->dte_probe;
13427 dtrace_provider_t *prov = probe->dtpr_provider;
13428
13429 if (!(prov->dtpv_priv.dtpp_flags & match))
13430 continue;
13431 }
13432
13433 dtrace_ecb_disable(ecb);
13434 dtrace_ecb_destroy(ecb);
13435 }
13436
13437 if (!match)
13438 break;
13439 }
13440
13441 /*
13442 * Before we free the buffers, perform one more sync to assure that
13443 * every CPU is out of probe context.
13444 */
13445 dtrace_sync();
13446
13447 dtrace_buffer_free(state->dts_buffer);
13448 dtrace_buffer_free(state->dts_aggbuffer);
13449
13450 for (i = 0; i < nspec; i++)
13451 dtrace_buffer_free(spec[i].dtsp_buffer);
13452
13453 if (state->dts_cleaner != CYCLIC_NONE)
13454 cyclic_remove(state->dts_cleaner);
13455
13456 if (state->dts_deadman != CYCLIC_NONE)
13457 cyclic_remove(state->dts_deadman);
13458
13459 dtrace_dstate_fini(&vstate->dtvs_dynvars);
13460 dtrace_vstate_fini(vstate);
13461 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *));
13462
13463 if (state->dts_aggregations != NULL) {
13464#ifdef DEBUG
13465 for (i = 0; i < state->dts_naggregations; i++)
13466 ASSERT(state->dts_aggregations[i] == NULL);
13467#endif
13468 ASSERT(state->dts_naggregations > 0);
13469 kmem_free(state->dts_aggregations,
13470 state->dts_naggregations * sizeof (dtrace_aggregation_t *));
13471 }
13472
13473 kmem_free(state->dts_buffer, bufsize);
13474 kmem_free(state->dts_aggbuffer, bufsize);
13475
13476 for (i = 0; i < nspec; i++)
13477 kmem_free(spec[i].dtsp_buffer, bufsize);
13478
13479 kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
13480
13481 dtrace_format_destroy(state);
13482
13483 vmem_destroy(state->dts_aggid_arena);
13484#ifndef VBOX
13485 ddi_soft_state_free(dtrace_softstate, minor);
13486 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
13487#else
13488 kmem_free(state, sizeof (*state));
13489#endif
13490}
13491
13492/*
13493 * DTrace Anonymous Enabling Functions
13494 */
13495static dtrace_state_t *
13496dtrace_anon_grab(void)
13497{
13498 dtrace_state_t *state;
13499
13500 ASSERT(MUTEX_HELD(&dtrace_lock));
13501
13502 if ((state = dtrace_anon.dta_state) == NULL) {
13503 ASSERT(dtrace_anon.dta_enabling == NULL);
13504 return (NULL);
13505 }
13506
13507 ASSERT(dtrace_anon.dta_enabling != NULL);
13508 ASSERT(dtrace_retained != NULL);
13509
13510 dtrace_enabling_destroy(dtrace_anon.dta_enabling);
13511 dtrace_anon.dta_enabling = NULL;
13512 dtrace_anon.dta_state = NULL;
13513
13514 return (state);
13515}
13516
13517#ifndef VBOX
13518static void
13519dtrace_anon_property(void)
13520{
13521 int i, rv;
13522 dtrace_state_t *state;
13523 dof_hdr_t *dof;
13524 char c[32]; /* enough for "dof-data-" + digits */
13525
13526 ASSERT(MUTEX_HELD(&dtrace_lock));
13527 ASSERT(MUTEX_HELD(&cpu_lock));
13528
13529 for (i = 0; ; i++) {
13530 (void) snprintf(c, sizeof (c), "dof-data-%d", i);
13531
13532 dtrace_err_verbose = 1;
13533
13534 if ((dof = dtrace_dof_property(c)) == NULL) {
13535 dtrace_err_verbose = 0;
13536 break;
13537 }
13538
13539#ifndef VBOX
13540 /*
13541 * We want to create anonymous state, so we need to transition
13542 * the kernel debugger to indicate that DTrace is active. If
13543 * this fails (e.g. because the debugger has modified text in
13544 * some way), we won't continue with the processing.
13545 */
13546 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
13547 cmn_err(CE_NOTE, "kernel debugger active; anonymous "
13548 "enabling ignored.");
13549 dtrace_dof_destroy(dof);
13550 break;
13551 }
13552#endif
13553
13554 /*
13555 * If we haven't allocated an anonymous state, we'll do so now.
13556 */
13557 if ((state = dtrace_anon.dta_state) == NULL) {
13558 state = dtrace_state_create(NULL, NULL);
13559 dtrace_anon.dta_state = state;
13560
13561 if (state == NULL) {
13562 /*
13563 * This basically shouldn't happen: the only
13564 * failure mode from dtrace_state_create() is a
13565 * failure of ddi_soft_state_zalloc() that
13566 * itself should never happen. Still, the
13567 * interface allows for a failure mode, and
13568 * we want to fail as gracefully as possible:
13569 * we'll emit an error message and cease
13570 * processing anonymous state in this case.
13571 */
13572 cmn_err(CE_WARN, "failed to create "
13573 "anonymous state");
13574 dtrace_dof_destroy(dof);
13575 break;
13576 }
13577 }
13578
13579 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(),
13580 &dtrace_anon.dta_enabling, 0, B_TRUE);
13581
13582 if (rv == 0)
13583 rv = dtrace_dof_options(dof, state);
13584
13585 dtrace_err_verbose = 0;
13586 dtrace_dof_destroy(dof);
13587
13588 if (rv != 0) {
13589 /*
13590 * This is malformed DOF; chuck any anonymous state
13591 * that we created.
13592 */
13593 ASSERT(dtrace_anon.dta_enabling == NULL);
13594 dtrace_state_destroy(state);
13595 dtrace_anon.dta_state = NULL;
13596 break;
13597 }
13598
13599 ASSERT(dtrace_anon.dta_enabling != NULL);
13600 }
13601
13602 if (dtrace_anon.dta_enabling != NULL) {
13603 int rval;
13604
13605 /*
13606 * dtrace_enabling_retain() can only fail because we are
13607 * trying to retain more enablings than are allowed -- but
13608 * we only have one anonymous enabling, and we are guaranteed
13609 * to be allowed at least one retained enabling; we assert
13610 * that dtrace_enabling_retain() returns success.
13611 */
13612 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling);
13613 ASSERT(rval == 0);
13614
13615 dtrace_enabling_dump(dtrace_anon.dta_enabling);
13616 }
13617}
13618#endif /* !VBOX */
13619
13620/*
13621 * DTrace Helper Functions
13622 */
13623#ifndef VBOX /* No helper stuff */
13624static void
13625dtrace_helper_trace(dtrace_helper_action_t *helper,
13626 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where)
13627{
13628 uint32_t size, next, nnext, i;
13629 dtrace_helptrace_t *ent;
13630 uint16_t flags = cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags;
13631
13632 if (!dtrace_helptrace_enabled)
13633 return;
13634
13635 ASSERT(vstate->dtvs_nlocals <= VBDTCAST(int32_t)dtrace_helptrace_nlocals);
13636
13637 /*
13638 * What would a tracing framework be without its own tracing
13639 * framework? (Well, a hell of a lot simpler, for starters...)
13640 */
13641 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals *
13642 sizeof (uint64_t) - sizeof (uint64_t);
13643
13644 /*
13645 * Iterate until we can allocate a slot in the trace buffer.
13646 */
13647 do {
13648 next = dtrace_helptrace_next;
13649
13650 if (next + size < VBDTCAST(unsigned)dtrace_helptrace_bufsize) {
13651 nnext = next + size;
13652 } else {
13653 nnext = size;
13654 }
13655 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next);
13656
13657 /*
13658 * We have our slot; fill it in.
13659 */
13660 if (nnext == size)
13661 next = 0;
13662
13663 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next];
13664 ent->dtht_helper = helper;
13665 ent->dtht_where = where;
13666 ent->dtht_nlocals = vstate->dtvs_nlocals;
13667
13668 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ?
13669 mstate->dtms_fltoffs : -1;
13670 ent->dtht_fault = DTRACE_FLAGS2FLT(flags);
13671 ent->dtht_illval = cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval;
13672
13673 for (i = 0; VBDTCAST(int32_t)i < vstate->dtvs_nlocals; i++) {
13674 dtrace_statvar_t *svar;
13675
13676 if ((svar = vstate->dtvs_locals[i]) == NULL)
13677 continue;
13678
13679 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t));
13680 ent->dtht_locals[i] =
13681 ((uint64_t *)(uintptr_t)svar->dtsv_data)[VBDT_GET_CPUID()];
13682 }
13683}
13684
13685static uint64_t
13686dtrace_helper(int which, dtrace_mstate_t *mstate,
13687 dtrace_state_t *state, uint64_t arg0, uint64_t arg1)
13688{
13689 VBDTTYPE(uint16_t volatile *, uint16_t *)flags = &cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_flags;
13690 uint64_t sarg0 = mstate->dtms_arg[0];
13691 uint64_t sarg1 = mstate->dtms_arg[1];
13692 uint64_t rval VBDTUNASS(666);
13693 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers;
13694 dtrace_helper_action_t *helper;
13695 dtrace_vstate_t *vstate;
13696 dtrace_difo_t *pred;
13697 int i, trace = dtrace_helptrace_enabled;
13698
13699 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS);
13700
13701 if (helpers == NULL)
13702 return (0);
13703
13704 if ((helper = helpers->dthps_actions[which]) == NULL)
13705 return (0);
13706
13707 vstate = &helpers->dthps_vstate;
13708 mstate->dtms_arg[0] = arg0;
13709 mstate->dtms_arg[1] = arg1;
13710
13711 /*
13712 * Now iterate over each helper. If its predicate evaluates to 'true',
13713 * we'll call the corresponding actions. Note that the below calls
13714 * to dtrace_dif_emulate() may set faults in machine state. This is
13715 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow
13716 * the stored DIF offset with its own (which is the desired behavior).
13717 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
13718 * from machine state; this is okay, too.
13719 */
13720 for (; helper != NULL; helper = helper->dtha_next) {
13721 if ((pred = helper->dtha_predicate) != NULL) {
13722 if (trace)
13723 dtrace_helper_trace(helper, mstate, vstate, 0);
13724
13725 if (!dtrace_dif_emulate(pred, mstate, vstate, state))
13726 goto next;
13727
13728 if (*flags & CPU_DTRACE_FAULT)
13729 goto err;
13730 }
13731
13732 for (i = 0; i < helper->dtha_nactions; i++) {
13733 if (trace)
13734 dtrace_helper_trace(helper,
13735 mstate, vstate, i + 1);
13736
13737 rval = dtrace_dif_emulate(helper->dtha_actions[i],
13738 mstate, vstate, state);
13739
13740 if (*flags & CPU_DTRACE_FAULT)
13741 goto err;
13742 }
13743
13744next:
13745 if (trace)
13746 dtrace_helper_trace(helper, mstate, vstate,
13747 DTRACE_HELPTRACE_NEXT);
13748 }
13749
13750 if (trace)
13751 dtrace_helper_trace(helper, mstate, vstate,
13752 DTRACE_HELPTRACE_DONE);
13753
13754 /*
13755 * Restore the arg0 that we saved upon entry.
13756 */
13757 mstate->dtms_arg[0] = sarg0;
13758 mstate->dtms_arg[1] = sarg1;
13759
13760 return (rval);
13761
13762err:
13763 if (trace)
13764 dtrace_helper_trace(helper, mstate, vstate,
13765 DTRACE_HELPTRACE_ERR);
13766
13767 /*
13768 * Restore the arg0 that we saved upon entry.
13769 */
13770 mstate->dtms_arg[0] = sarg0;
13771 mstate->dtms_arg[1] = sarg1;
13772
13773 return (NULL);
13774}
13775
13776static void
13777dtrace_helper_action_destroy(dtrace_helper_action_t *helper,
13778 dtrace_vstate_t *vstate)
13779{
13780 int i;
13781
13782 if (helper->dtha_predicate != NULL)
13783 dtrace_difo_release(helper->dtha_predicate, vstate);
13784
13785 for (i = 0; i < helper->dtha_nactions; i++) {
13786 ASSERT(helper->dtha_actions[i] != NULL);
13787 dtrace_difo_release(helper->dtha_actions[i], vstate);
13788 }
13789
13790 kmem_free(helper->dtha_actions,
13791 helper->dtha_nactions * sizeof (dtrace_difo_t *));
13792 kmem_free(helper, sizeof (dtrace_helper_action_t));
13793}
13794
13795static int
13796dtrace_helper_destroygen(int gen)
13797{
13798 proc_t *p = curproc;
13799 dtrace_helpers_t *help = p->p_dtrace_helpers;
13800 dtrace_vstate_t *vstate;
13801 VBDTTYPE(uint_t,int) i;
13802
13803 ASSERT(MUTEX_HELD(&dtrace_lock));
13804
13805 if (help == NULL || gen > help->dthps_generation)
13806 return (EINVAL);
13807
13808 vstate = &help->dthps_vstate;
13809
13810 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
13811 dtrace_helper_action_t *last = NULL, *h, *next;
13812
13813 for (h = help->dthps_actions[i]; h != NULL; h = next) {
13814 next = h->dtha_next;
13815
13816 if (h->dtha_generation == gen) {
13817 if (last != NULL) {
13818 last->dtha_next = next;
13819 } else {
13820 help->dthps_actions[i] = next;
13821 }
13822
13823 dtrace_helper_action_destroy(h, vstate);
13824 } else {
13825 last = h;
13826 }
13827 }
13828 }
13829
13830 /*
13831 * Interate until we've cleared out all helper providers with the
13832 * given generation number.
13833 */
13834 for (;;) {
13835 dtrace_helper_provider_t *prov VBDTGCC(NULL);
13836
13837 /*
13838 * Look for a helper provider with the right generation. We
13839 * have to start back at the beginning of the list each time
13840 * because we drop dtrace_lock. It's unlikely that we'll make
13841 * more than two passes.
13842 */
13843 for (i = 0; i < help->dthps_nprovs; i++) {
13844 prov = help->dthps_provs[i];
13845
13846 if (prov->dthp_generation == gen)
13847 break;
13848 }
13849
13850 /*
13851 * If there were no matches, we're done.
13852 */
13853 if (i == help->dthps_nprovs)
13854 break;
13855
13856 /*
13857 * Move the last helper provider into this slot.
13858 */
13859 help->dthps_nprovs--;
13860 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs];
13861 help->dthps_provs[help->dthps_nprovs] = NULL;
13862
13863 mutex_exit(&dtrace_lock);
13864
13865 /*
13866 * If we have a meta provider, remove this helper provider.
13867 */
13868 mutex_enter(&dtrace_meta_lock);
13869 if (dtrace_meta_pid != NULL) {
13870 ASSERT(dtrace_deferred_pid == NULL);
13871 dtrace_helper_provider_remove(&prov->dthp_prov,
13872 p->p_pid);
13873 }
13874 mutex_exit(&dtrace_meta_lock);
13875
13876 dtrace_helper_provider_destroy(prov);
13877
13878 mutex_enter(&dtrace_lock);
13879 }
13880
13881 return (0);
13882}
13883
13884static int
13885dtrace_helper_validate(dtrace_helper_action_t *helper)
13886{
13887 int err = 0, i;
13888 dtrace_difo_t *dp;
13889
13890 if ((dp = helper->dtha_predicate) != NULL)
13891 err += dtrace_difo_validate_helper(dp);
13892
13893 for (i = 0; i < helper->dtha_nactions; i++)
13894 err += dtrace_difo_validate_helper(helper->dtha_actions[i]);
13895
13896 return (err == 0);
13897}
13898
13899static int
13900dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep)
13901{
13902 dtrace_helpers_t *help;
13903 dtrace_helper_action_t *helper, *last;
13904 dtrace_actdesc_t *act;
13905 dtrace_vstate_t *vstate;
13906 dtrace_predicate_t *pred;
13907 int count = 0, nactions = 0, i;
13908
13909 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS)
13910 return (EINVAL);
13911
13912 help = curproc->p_dtrace_helpers;
13913 last = help->dthps_actions[which];
13914 vstate = &help->dthps_vstate;
13915
13916 for (count = 0; last != NULL; last = last->dtha_next) {
13917 count++;
13918 if (last->dtha_next == NULL)
13919 break;
13920 }
13921
13922 /*
13923 * If we already have dtrace_helper_actions_max helper actions for this
13924 * helper action type, we'll refuse to add a new one.
13925 */
13926 if (count >= dtrace_helper_actions_max)
13927 return (ENOSPC);
13928
13929 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP);
13930 helper->dtha_generation = help->dthps_generation;
13931
13932 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) {
13933 ASSERT(pred->dtp_difo != NULL);
13934 dtrace_difo_hold(pred->dtp_difo);
13935 helper->dtha_predicate = pred->dtp_difo;
13936 }
13937
13938 for (act = ep->dted_action; act != NULL; act = act->dtad_next) {
13939 if (act->dtad_kind != DTRACEACT_DIFEXPR)
13940 goto err;
13941
13942 if (act->dtad_difo == NULL)
13943 goto err;
13944
13945 nactions++;
13946 }
13947
13948 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) *
13949 (helper->dtha_nactions = nactions), KM_SLEEP);
13950
13951 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) {
13952 dtrace_difo_hold(act->dtad_difo);
13953 helper->dtha_actions[i++] = act->dtad_difo;
13954 }
13955
13956 if (!dtrace_helper_validate(helper))
13957 goto err;
13958
13959 if (last == NULL) {
13960 help->dthps_actions[which] = helper;
13961 } else {
13962 last->dtha_next = helper;
13963 }
13964
13965 if (vstate->dtvs_nlocals > VBDTCAST(int32_t)dtrace_helptrace_nlocals) {
13966 dtrace_helptrace_nlocals = vstate->dtvs_nlocals;
13967 dtrace_helptrace_next = 0;
13968 }
13969
13970 return (0);
13971err:
13972 dtrace_helper_action_destroy(helper, vstate);
13973 return (EINVAL);
13974}
13975
13976static void
13977dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help,
13978 dof_helper_t *dofhp)
13979{
13980 ASSERT(MUTEX_NOT_HELD(&dtrace_lock));
13981
13982 mutex_enter(&dtrace_meta_lock);
13983 mutex_enter(&dtrace_lock);
13984
13985 if (!dtrace_attached() || dtrace_meta_pid == NULL) {
13986 /*
13987 * If the dtrace module is loaded but not attached, or if
13988 * there aren't isn't a meta provider registered to deal with
13989 * these provider descriptions, we need to postpone creating
13990 * the actual providers until later.
13991 */
13992
13993 if (help->dthps_next == NULL && help->dthps_prev == NULL &&
13994 dtrace_deferred_pid != help) {
13995 help->dthps_deferred = 1;
13996 help->dthps_pid = p->p_pid;
13997 help->dthps_next = dtrace_deferred_pid;
13998 help->dthps_prev = NULL;
13999 if (dtrace_deferred_pid != NULL)
14000 dtrace_deferred_pid->dthps_prev = help;
14001 dtrace_deferred_pid = help;
14002 }
14003
14004 mutex_exit(&dtrace_lock);
14005
14006 } else if (dofhp != NULL) {
14007 /*
14008 * If the dtrace module is loaded and we have a particular
14009 * helper provider description, pass that off to the
14010 * meta provider.
14011 */
14012
14013 mutex_exit(&dtrace_lock);
14014
14015 dtrace_helper_provide(dofhp, p->p_pid);
14016
14017 } else {
14018 /*
14019 * Otherwise, just pass all the helper provider descriptions
14020 * off to the meta provider.
14021 */
14022
14023 VBDTTYPE(uint_t,int) i;
14024 mutex_exit(&dtrace_lock);
14025
14026 for (i = 0; i < help->dthps_nprovs; i++) {
14027 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
14028 p->p_pid);
14029 }
14030 }
14031
14032 mutex_exit(&dtrace_meta_lock);
14033}
14034
14035static int
14036dtrace_helper_provider_add(dof_helper_t *dofhp, int gen)
14037{
14038 dtrace_helpers_t *help;
14039 dtrace_helper_provider_t *hprov, **tmp_provs;
14040 uint_t tmp_maxprovs, i;
14041
14042 ASSERT(MUTEX_HELD(&dtrace_lock));
14043
14044 help = curproc->p_dtrace_helpers;
14045 ASSERT(help != NULL);
14046
14047 /*
14048 * If we already have dtrace_helper_providers_max helper providers,
14049 * we're refuse to add a new one.
14050 */
14051 if (help->dthps_nprovs >= dtrace_helper_providers_max)
14052 return (ENOSPC);
14053
14054 /*
14055 * Check to make sure this isn't a duplicate.
14056 */
14057 for (i = 0; i < help->dthps_nprovs; i++) {
14058 if (dofhp->dofhp_addr ==
14059 help->dthps_provs[i]->dthp_prov.dofhp_addr)
14060 return (EALREADY);
14061 }
14062
14063 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP);
14064 hprov->dthp_prov = *dofhp;
14065 hprov->dthp_ref = 1;
14066 hprov->dthp_generation = gen;
14067
14068 /*
14069 * Allocate a bigger table for helper providers if it's already full.
14070 */
14071 if (help->dthps_maxprovs == help->dthps_nprovs) {
14072 tmp_maxprovs = help->dthps_maxprovs;
14073 tmp_provs = help->dthps_provs;
14074
14075 if (help->dthps_maxprovs == 0)
14076 help->dthps_maxprovs = 2;
14077 else
14078 help->dthps_maxprovs *= 2;
14079 if (help->dthps_maxprovs > dtrace_helper_providers_max)
14080 help->dthps_maxprovs = dtrace_helper_providers_max;
14081
14082 ASSERT(tmp_maxprovs < help->dthps_maxprovs);
14083
14084 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs *
14085 sizeof (dtrace_helper_provider_t *), KM_SLEEP);
14086
14087 if (tmp_provs != NULL) {
14088 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs *
14089 sizeof (dtrace_helper_provider_t *));
14090 kmem_free(tmp_provs, tmp_maxprovs *
14091 sizeof (dtrace_helper_provider_t *));
14092 }
14093 }
14094
14095 help->dthps_provs[help->dthps_nprovs] = hprov;
14096 help->dthps_nprovs++;
14097
14098 return (0);
14099}
14100
14101static void
14102dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov)
14103{
14104 mutex_enter(&dtrace_lock);
14105
14106 if (--hprov->dthp_ref == 0) {
14107 dof_hdr_t *dof;
14108 mutex_exit(&dtrace_lock);
14109 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof;
14110 dtrace_dof_destroy(dof);
14111 kmem_free(hprov, sizeof (dtrace_helper_provider_t));
14112 } else {
14113 mutex_exit(&dtrace_lock);
14114 }
14115}
14116
14117static int
14118dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec)
14119{
14120 uintptr_t daddr = (uintptr_t)dof;
14121 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
14122 dof_provider_t *provider;
14123 dof_probe_t *probe;
14124 uint8_t *arg;
14125 char *strtab, *typestr;
14126 dof_stridx_t typeidx;
14127 size_t typesz;
14128 uint_t nprobes, j, k;
14129
14130 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER);
14131
14132 if (sec->dofs_offset & (sizeof (uint_t) - 1)) {
14133 dtrace_dof_error(dof, "misaligned section offset");
14134 return (-1);
14135 }
14136
14137 /*
14138 * The section needs to be large enough to contain the DOF provider
14139 * structure appropriate for the given version.
14140 */
14141 if (sec->dofs_size <
14142 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ?
14143 offsetof(dof_provider_t, dofpv_prenoffs) :
14144 sizeof (dof_provider_t))) {
14145 dtrace_dof_error(dof, "provider section too small");
14146 return (-1);
14147 }
14148
14149 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
14150 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab);
14151 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes);
14152 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs);
14153 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs);
14154
14155 if (str_sec == NULL || prb_sec == NULL ||
14156 arg_sec == NULL || off_sec == NULL)
14157 return (-1);
14158
14159 enoff_sec = NULL;
14160
14161 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
14162 provider->dofpv_prenoffs != DOF_SECT_NONE &&
14163 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS,
14164 provider->dofpv_prenoffs)) == NULL)
14165 return (-1);
14166
14167 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
14168
14169 if (provider->dofpv_name >= str_sec->dofs_size ||
14170 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) {
14171 dtrace_dof_error(dof, "invalid provider name");
14172 return (-1);
14173 }
14174
14175 if (prb_sec->dofs_entsize == 0 ||
14176 prb_sec->dofs_entsize > prb_sec->dofs_size) {
14177 dtrace_dof_error(dof, "invalid entry size");
14178 return (-1);
14179 }
14180
14181 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) {
14182 dtrace_dof_error(dof, "misaligned entry size");
14183 return (-1);
14184 }
14185
14186 if (off_sec->dofs_entsize != sizeof (uint32_t)) {
14187 dtrace_dof_error(dof, "invalid entry size");
14188 return (-1);
14189 }
14190
14191 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) {
14192 dtrace_dof_error(dof, "misaligned section offset");
14193 return (-1);
14194 }
14195
14196 if (arg_sec->dofs_entsize != sizeof (uint8_t)) {
14197 dtrace_dof_error(dof, "invalid entry size");
14198 return (-1);
14199 }
14200
14201 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
14202
14203 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
14204
14205 /*
14206 * Take a pass through the probes to check for errors.
14207 */
14208 for (j = 0; j < nprobes; j++) {
14209 probe = (dof_probe_t *)(uintptr_t)(daddr +
14210 prb_sec->dofs_offset + j * prb_sec->dofs_entsize);
14211
14212 if (probe->dofpr_func >= str_sec->dofs_size) {
14213 dtrace_dof_error(dof, "invalid function name");
14214 return (-1);
14215 }
14216
14217 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) {
14218 dtrace_dof_error(dof, "function name too long");
14219 return (-1);
14220 }
14221
14222 if (probe->dofpr_name >= str_sec->dofs_size ||
14223 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) {
14224 dtrace_dof_error(dof, "invalid probe name");
14225 return (-1);
14226 }
14227
14228 /*
14229 * The offset count must not wrap the index, and the offsets
14230 * must also not overflow the section's data.
14231 */
14232 if (probe->dofpr_offidx + probe->dofpr_noffs <
14233 probe->dofpr_offidx ||
14234 (probe->dofpr_offidx + probe->dofpr_noffs) *
14235 off_sec->dofs_entsize > off_sec->dofs_size) {
14236 dtrace_dof_error(dof, "invalid probe offset");
14237 return (-1);
14238 }
14239
14240 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) {
14241 /*
14242 * If there's no is-enabled offset section, make sure
14243 * there aren't any is-enabled offsets. Otherwise
14244 * perform the same checks as for probe offsets
14245 * (immediately above).
14246 */
14247 if (enoff_sec == NULL) {
14248 if (probe->dofpr_enoffidx != 0 ||
14249 probe->dofpr_nenoffs != 0) {
14250 dtrace_dof_error(dof, "is-enabled "
14251 "offsets with null section");
14252 return (-1);
14253 }
14254 } else if (probe->dofpr_enoffidx +
14255 probe->dofpr_nenoffs < probe->dofpr_enoffidx ||
14256 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) *
14257 enoff_sec->dofs_entsize > enoff_sec->dofs_size) {
14258 dtrace_dof_error(dof, "invalid is-enabled "
14259 "offset");
14260 return (-1);
14261 }
14262
14263 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) {
14264 dtrace_dof_error(dof, "zero probe and "
14265 "is-enabled offsets");
14266 return (-1);
14267 }
14268 } else if (probe->dofpr_noffs == 0) {
14269 dtrace_dof_error(dof, "zero probe offsets");
14270 return (-1);
14271 }
14272
14273 if (probe->dofpr_argidx + probe->dofpr_xargc <
14274 probe->dofpr_argidx ||
14275 (probe->dofpr_argidx + probe->dofpr_xargc) *
14276 arg_sec->dofs_entsize > arg_sec->dofs_size) {
14277 dtrace_dof_error(dof, "invalid args");
14278 return (-1);
14279 }
14280
14281 typeidx = probe->dofpr_nargv;
14282 typestr = strtab + probe->dofpr_nargv;
14283 for (k = 0; k < probe->dofpr_nargc; k++) {
14284 if (typeidx >= str_sec->dofs_size) {
14285 dtrace_dof_error(dof, "bad "
14286 "native argument type");
14287 return (-1);
14288 }
14289
14290 typesz = strlen(typestr) + 1;
14291 if (typesz > DTRACE_ARGTYPELEN) {
14292 dtrace_dof_error(dof, "native "
14293 "argument type too long");
14294 return (-1);
14295 }
14296 typeidx += VBDTCAST(dof_stridx_t)typesz;
14297 typestr += typesz;
14298 }
14299
14300 typeidx = probe->dofpr_xargv;
14301 typestr = strtab + probe->dofpr_xargv;
14302 for (k = 0; k < probe->dofpr_xargc; k++) {
14303 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) {
14304 dtrace_dof_error(dof, "bad "
14305 "native argument index");
14306 return (-1);
14307 }
14308
14309 if (typeidx >= str_sec->dofs_size) {
14310 dtrace_dof_error(dof, "bad "
14311 "translated argument type");
14312 return (-1);
14313 }
14314
14315 typesz = strlen(typestr) + 1;
14316 if (typesz > DTRACE_ARGTYPELEN) {
14317 dtrace_dof_error(dof, "translated argument "
14318 "type too long");
14319 return (-1);
14320 }
14321
14322 typeidx += VBDTCAST(dof_stridx_t)typesz;
14323 typestr += typesz;
14324 }
14325 }
14326
14327 return (0);
14328}
14329
14330static int
14331dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp)
14332{
14333 dtrace_helpers_t *help;
14334 dtrace_vstate_t *vstate;
14335 dtrace_enabling_t *enab = NULL;
14336 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1;
14337 uintptr_t daddr = (uintptr_t)dof;
14338
14339 ASSERT(MUTEX_HELD(&dtrace_lock));
14340
14341 if ((help = curproc->p_dtrace_helpers) == NULL)
14342 help = dtrace_helpers_create(curproc);
14343
14344 vstate = &help->dthps_vstate;
14345
14346 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab,
14347 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) {
14348 dtrace_dof_destroy(dof);
14349 return (rv);
14350 }
14351
14352 /*
14353 * Look for helper providers and validate their descriptions.
14354 */
14355 if (dhp != NULL) {
14356 for (i = 0; i < VBDTCAST(int)dof->dofh_secnum; i++) {
14357 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
14358 dof->dofh_secoff + i * dof->dofh_secsize);
14359
14360 if (sec->dofs_type != DOF_SECT_PROVIDER)
14361 continue;
14362
14363 if (dtrace_helper_provider_validate(dof, sec) != 0) {
14364 dtrace_enabling_destroy(enab);
14365 dtrace_dof_destroy(dof);
14366 return (-1);
14367 }
14368
14369 nprovs++;
14370 }
14371 }
14372
14373 /*
14374 * Now we need to walk through the ECB descriptions in the enabling.
14375 */
14376 for (i = 0; i < enab->dten_ndesc; i++) {
14377 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
14378 dtrace_probedesc_t *desc = &ep->dted_probe;
14379
14380 if (strcmp(desc->dtpd_provider, "dtrace") != 0)
14381 continue;
14382
14383 if (strcmp(desc->dtpd_mod, "helper") != 0)
14384 continue;
14385
14386 if (strcmp(desc->dtpd_func, "ustack") != 0)
14387 continue;
14388
14389 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK,
14390 ep)) != 0) {
14391 /*
14392 * Adding this helper action failed -- we are now going
14393 * to rip out the entire generation and return failure.
14394 */
14395 (void) dtrace_helper_destroygen(help->dthps_generation);
14396 dtrace_enabling_destroy(enab);
14397 dtrace_dof_destroy(dof);
14398 return (-1);
14399 }
14400
14401 nhelpers++;
14402 }
14403
14404 if (nhelpers < enab->dten_ndesc)
14405 dtrace_dof_error(dof, "unmatched helpers");
14406
14407 gen = help->dthps_generation++;
14408 dtrace_enabling_destroy(enab);
14409
14410 if (dhp != NULL && nprovs > 0) {
14411 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof;
14412 if (dtrace_helper_provider_add(dhp, gen) == 0) {
14413 mutex_exit(&dtrace_lock);
14414 dtrace_helper_provider_register(curproc, help, dhp);
14415 mutex_enter(&dtrace_lock);
14416
14417 destroy = 0;
14418 }
14419 }
14420
14421 if (destroy)
14422 dtrace_dof_destroy(dof);
14423
14424 return (gen);
14425}
14426
14427static dtrace_helpers_t *
14428dtrace_helpers_create(proc_t *p)
14429{
14430 dtrace_helpers_t *help;
14431
14432 ASSERT(MUTEX_HELD(&dtrace_lock));
14433 ASSERT(p->p_dtrace_helpers == NULL);
14434
14435 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP);
14436 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) *
14437 DTRACE_NHELPER_ACTIONS, KM_SLEEP);
14438
14439 p->p_dtrace_helpers = help;
14440 dtrace_helpers++;
14441
14442 return (help);
14443}
14444
14445static void
14446dtrace_helpers_destroy(void)
14447{
14448 dtrace_helpers_t *help;
14449 dtrace_vstate_t *vstate;
14450 proc_t *p = curproc;
14451 VBDTTYPE(uint_t, int) i;
14452
14453 mutex_enter(&dtrace_lock);
14454
14455 ASSERT(p->p_dtrace_helpers != NULL);
14456 ASSERT(dtrace_helpers > 0);
14457
14458 help = p->p_dtrace_helpers;
14459 vstate = &help->dthps_vstate;
14460
14461 /*
14462 * We're now going to lose the help from this process.
14463 */
14464 p->p_dtrace_helpers = NULL;
14465 dtrace_sync();
14466
14467 /*
14468 * Destory the helper actions.
14469 */
14470 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
14471 dtrace_helper_action_t *h, *next;
14472
14473 for (h = help->dthps_actions[i]; h != NULL; h = next) {
14474 next = h->dtha_next;
14475 dtrace_helper_action_destroy(h, vstate);
14476 h = next;
14477 }
14478 }
14479
14480 mutex_exit(&dtrace_lock);
14481
14482 /*
14483 * Destroy the helper providers.
14484 */
14485 if (help->dthps_maxprovs > 0) {
14486 mutex_enter(&dtrace_meta_lock);
14487 if (dtrace_meta_pid != NULL) {
14488 ASSERT(dtrace_deferred_pid == NULL);
14489
14490 for (i = 0; i < help->dthps_nprovs; i++) {
14491 dtrace_helper_provider_remove(
14492 &help->dthps_provs[i]->dthp_prov, p->p_pid);
14493 }
14494 } else {
14495 mutex_enter(&dtrace_lock);
14496 ASSERT(help->dthps_deferred == 0 ||
14497 help->dthps_next != NULL ||
14498 help->dthps_prev != NULL ||
14499 help == dtrace_deferred_pid);
14500
14501 /*
14502 * Remove the helper from the deferred list.
14503 */
14504 if (help->dthps_next != NULL)
14505 help->dthps_next->dthps_prev = help->dthps_prev;
14506 if (help->dthps_prev != NULL)
14507 help->dthps_prev->dthps_next = help->dthps_next;
14508 if (dtrace_deferred_pid == help) {
14509 dtrace_deferred_pid = help->dthps_next;
14510 ASSERT(help->dthps_prev == NULL);
14511 }
14512
14513 mutex_exit(&dtrace_lock);
14514 }
14515
14516 mutex_exit(&dtrace_meta_lock);
14517
14518 for (i = 0; i < help->dthps_nprovs; i++) {
14519 dtrace_helper_provider_destroy(help->dthps_provs[i]);
14520 }
14521
14522 kmem_free(help->dthps_provs, help->dthps_maxprovs *
14523 sizeof (dtrace_helper_provider_t *));
14524 }
14525
14526 mutex_enter(&dtrace_lock);
14527
14528 dtrace_vstate_fini(&help->dthps_vstate);
14529 kmem_free(help->dthps_actions,
14530 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS);
14531 kmem_free(help, sizeof (dtrace_helpers_t));
14532
14533 --dtrace_helpers;
14534 mutex_exit(&dtrace_lock);
14535}
14536
14537static void
14538dtrace_helpers_duplicate(proc_t *from, proc_t *to)
14539{
14540 dtrace_helpers_t *help, *newhelp;
14541 dtrace_helper_action_t *helper, *new, *last;
14542 dtrace_difo_t *dp;
14543 dtrace_vstate_t *vstate;
14544 int i, j, sz, hasprovs = 0;
14545
14546 mutex_enter(&dtrace_lock);
14547 ASSERT(from->p_dtrace_helpers != NULL);
14548 ASSERT(dtrace_helpers > 0);
14549
14550 help = from->p_dtrace_helpers;
14551 newhelp = dtrace_helpers_create(to);
14552 ASSERT(to->p_dtrace_helpers != NULL);
14553
14554 newhelp->dthps_generation = help->dthps_generation;
14555 vstate = &newhelp->dthps_vstate;
14556
14557 /*
14558 * Duplicate the helper actions.
14559 */
14560 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
14561 if ((helper = help->dthps_actions[i]) == NULL)
14562 continue;
14563
14564 for (last = NULL; helper != NULL; helper = helper->dtha_next) {
14565 new = kmem_zalloc(sizeof (dtrace_helper_action_t),
14566 KM_SLEEP);
14567 new->dtha_generation = helper->dtha_generation;
14568
14569 if ((dp = helper->dtha_predicate) != NULL) {
14570 dp = dtrace_difo_duplicate(dp, vstate);
14571 new->dtha_predicate = dp;
14572 }
14573
14574 new->dtha_nactions = helper->dtha_nactions;
14575 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions;
14576 new->dtha_actions = kmem_alloc(sz, KM_SLEEP);
14577
14578 for (j = 0; j < new->dtha_nactions; j++) {
14579 dtrace_difo_t *dp2 = helper->dtha_actions[j];
14580
14581 ASSERT(dp2 != NULL);
14582 dp2 = dtrace_difo_duplicate(dp2, vstate);
14583 new->dtha_actions[j] = dp2;
14584 }
14585
14586 if (last != NULL) {
14587 last->dtha_next = new;
14588 } else {
14589 newhelp->dthps_actions[i] = new;
14590 }
14591
14592 last = new;
14593 }
14594 }
14595
14596 /*
14597 * Duplicate the helper providers and register them with the
14598 * DTrace framework.
14599 */
14600 if (help->dthps_nprovs > 0) {
14601 newhelp->dthps_nprovs = help->dthps_nprovs;
14602 newhelp->dthps_maxprovs = help->dthps_nprovs;
14603 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs *
14604 sizeof (dtrace_helper_provider_t *), KM_SLEEP);
14605 for (i = 0; i < VBDTCAST(int)newhelp->dthps_nprovs; i++) {
14606 newhelp->dthps_provs[i] = help->dthps_provs[i];
14607 newhelp->dthps_provs[i]->dthp_ref++;
14608 }
14609
14610 hasprovs = 1;
14611 }
14612
14613 mutex_exit(&dtrace_lock);
14614
14615 if (hasprovs)
14616 dtrace_helper_provider_register(to, newhelp, NULL);
14617}
14618
14619/*
14620 * DTrace Hook Functions
14621 */
14622static void
14623dtrace_module_loaded(struct modctl *ctl)
14624{
14625 dtrace_provider_t *prv;
14626
14627 mutex_enter(&dtrace_provider_lock);
14628 mutex_enter(&mod_lock);
14629
14630 ASSERT(ctl->mod_busy);
14631
14632 /*
14633 * We're going to call each providers per-module provide operation
14634 * specifying only this module.
14635 */
14636 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
14637 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
14638
14639 mutex_exit(&mod_lock);
14640 mutex_exit(&dtrace_provider_lock);
14641
14642 /*
14643 * If we have any retained enablings, we need to match against them.
14644 * Enabling probes requires that cpu_lock be held, and we cannot hold
14645 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
14646 * module. (In particular, this happens when loading scheduling
14647 * classes.) So if we have any retained enablings, we need to dispatch
14648 * our task queue to do the match for us.
14649 */
14650 mutex_enter(&dtrace_lock);
14651
14652 if (dtrace_retained == NULL) {
14653 mutex_exit(&dtrace_lock);
14654 return;
14655 }
14656
14657 (void) taskq_dispatch(dtrace_taskq,
14658 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP);
14659
14660 mutex_exit(&dtrace_lock);
14661
14662 /*
14663 * And now, for a little heuristic sleaze: in general, we want to
14664 * match modules as soon as they load. However, we cannot guarantee
14665 * this, because it would lead us to the lock ordering violation
14666 * outlined above. The common case, of course, is that cpu_lock is
14667 * _not_ held -- so we delay here for a clock tick, hoping that that's
14668 * long enough for the task queue to do its work. If it's not, it's
14669 * not a serious problem -- it just means that the module that we
14670 * just loaded may not be immediately instrumentable.
14671 */
14672 delay(1);
14673}
14674
14675static void
14676dtrace_module_unloaded(struct modctl *ctl)
14677{
14678 dtrace_probe_t template, *probe, *first, *next;
14679 dtrace_provider_t *prov;
14680
14681 template.dtpr_mod = ctl->mod_modname;
14682
14683 mutex_enter(&dtrace_provider_lock);
14684 mutex_enter(&mod_lock);
14685 mutex_enter(&dtrace_lock);
14686
14687 if (dtrace_bymod == NULL) {
14688 /*
14689 * The DTrace module is loaded (obviously) but not attached;
14690 * we don't have any work to do.
14691 */
14692 mutex_exit(&dtrace_provider_lock);
14693 mutex_exit(&mod_lock);
14694 mutex_exit(&dtrace_lock);
14695 return;
14696 }
14697
14698 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template);
14699 probe != NULL; probe = probe->dtpr_nextmod) {
14700 if (probe->dtpr_ecb != NULL) {
14701 mutex_exit(&dtrace_provider_lock);
14702 mutex_exit(&mod_lock);
14703 mutex_exit(&dtrace_lock);
14704
14705 /*
14706 * This shouldn't _actually_ be possible -- we're
14707 * unloading a module that has an enabled probe in it.
14708 * (It's normally up to the provider to make sure that
14709 * this can't happen.) However, because dtps_enable()
14710 * doesn't have a failure mode, there can be an
14711 * enable/unload race. Upshot: we don't want to
14712 * assert, but we're not going to disable the
14713 * probe, either.
14714 */
14715 if (dtrace_err_verbose) {
14716 cmn_err(CE_WARN, "unloaded module '%s' had "
14717 "enabled probes", ctl->mod_modname);
14718 }
14719
14720 return;
14721 }
14722 }
14723
14724 probe = first;
14725
14726 for (first = NULL; probe != NULL; probe = next) {
14727 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe);
14728
14729 dtrace_probes[probe->dtpr_id - 1] = NULL;
14730
14731 next = probe->dtpr_nextmod;
14732 dtrace_hash_remove(dtrace_bymod, probe);
14733 dtrace_hash_remove(dtrace_byfunc, probe);
14734 dtrace_hash_remove(dtrace_byname, probe);
14735
14736 if (first == NULL) {
14737 first = probe;
14738 probe->dtpr_nextmod = NULL;
14739 } else {
14740 probe->dtpr_nextmod = first;
14741 first = probe;
14742 }
14743 }
14744
14745 /*
14746 * We've removed all of the module's probes from the hash chains and
14747 * from the probe array. Now issue a dtrace_sync() to be sure that
14748 * everyone has cleared out from any probe array processing.
14749 */
14750 dtrace_sync();
14751
14752 for (probe = first; probe != NULL; probe = first) {
14753 first = probe->dtpr_nextmod;
14754 prov = probe->dtpr_provider;
14755 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
14756 probe->dtpr_arg);
14757 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
14758 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
14759 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
14760 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1);
14761 kmem_free(probe, sizeof (dtrace_probe_t));
14762 }
14763
14764 mutex_exit(&dtrace_lock);
14765 mutex_exit(&mod_lock);
14766 mutex_exit(&dtrace_provider_lock);
14767}
14768
14769VBDTSTATIC void
14770dtrace_suspend(void)
14771{
14772 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend));
14773}
14774
14775VBDTSTATIC void
14776dtrace_resume(void)
14777{
14778 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume));
14779}
14780
14781#endif /* !VBOX */
14782
14783#ifdef VBOX
14784typedef enum {
14785 CPU_INVALID,
14786 CPU_CONFIG,
14787 CPU_UNCONFIG
14788} cpu_setup_t;
14789#endif
14790
14791#ifndef VBOX
14792
14793static int
14794dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu)
14795{
14796 ASSERT(MUTEX_HELD(&cpu_lock));
14797 mutex_enter(&dtrace_lock);
14798
14799 switch (what) {
14800 case CPU_CONFIG: {
14801 dtrace_state_t *state;
14802 dtrace_optval_t *opt, rs, c;
14803
14804 /*
14805 * For now, we only allocate a new buffer for anonymous state.
14806 */
14807 if ((state = dtrace_anon.dta_state) == NULL)
14808 break;
14809
14810 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
14811 break;
14812
14813 opt = state->dts_options;
14814 c = opt[DTRACEOPT_CPU];
14815
14816 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu)
14817 break;
14818
14819 /*
14820 * Regardless of what the actual policy is, we're going to
14821 * temporarily set our resize policy to be manual. We're
14822 * also going to temporarily set our CPU option to denote
14823 * the newly configured CPU.
14824 */
14825 rs = opt[DTRACEOPT_BUFRESIZE];
14826 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL;
14827 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu;
14828
14829 (void) dtrace_state_buffers(state);
14830
14831 opt[DTRACEOPT_BUFRESIZE] = rs;
14832 opt[DTRACEOPT_CPU] = c;
14833
14834 break;
14835 }
14836
14837 case CPU_UNCONFIG:
14838 /*
14839 * We don't free the buffer in the CPU_UNCONFIG case. (The
14840 * buffer will be freed when the consumer exits.)
14841 */
14842 break;
14843
14844 default:
14845 break;
14846 }
14847
14848 mutex_exit(&dtrace_lock);
14849 return (0);
14850}
14851
14852static void
14853dtrace_cpu_setup_initial(processorid_t cpu)
14854{
14855 (void) dtrace_cpu_setup(CPU_CONFIG, cpu);
14856}
14857
14858#endif /* !VBOX */
14859
14860static void
14861dtrace_toxrange_add(uintptr_t base, uintptr_t limit)
14862{
14863 if (dtrace_toxranges >= dtrace_toxranges_max) {
14864 int osize, nsize;
14865 dtrace_toxrange_t *range;
14866
14867 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
14868
14869 if (osize == 0) {
14870 ASSERT(dtrace_toxrange == NULL);
14871 ASSERT(dtrace_toxranges_max == 0);
14872 dtrace_toxranges_max = 1;
14873 } else {
14874 dtrace_toxranges_max <<= 1;
14875 }
14876
14877 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
14878 range = kmem_zalloc(nsize, KM_SLEEP);
14879
14880 if (dtrace_toxrange != NULL) {
14881 ASSERT(osize != 0);
14882 bcopy(dtrace_toxrange, range, osize);
14883 kmem_free(dtrace_toxrange, osize);
14884 }
14885
14886 dtrace_toxrange = range;
14887 }
14888
14889 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL);
14890 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL);
14891
14892 dtrace_toxrange[dtrace_toxranges].dtt_base = base;
14893 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit;
14894 dtrace_toxranges++;
14895}
14896
14897/*
14898 * DTrace Driver Cookbook Functions
14899 */
14900#ifdef VBOX
14901int dtrace_attach(void)
14902#else
14903/*ARGSUSED*/
14904static int
14905dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
14906#endif
14907{
14908 dtrace_provider_id_t id;
14909 dtrace_state_t *state = NULL;
14910 dtrace_enabling_t *enab;
14911
14912#ifdef VBOX
14913 if ( VBoxDtMutexInit(&dtrace_lock)
14914 || VBoxDtMutexInit(&dtrace_provider_lock)
14915 || VBoxDtMutexInit(&dtrace_meta_lock)
14916# ifdef DEBUG
14917 || VBoxDtMutexInit(&dtrace_errlock)
14918# endif
14919 )
14920 return (DDI_FAILURE);
14921#endif
14922
14923 mutex_enter(&cpu_lock);
14924 mutex_enter(&dtrace_provider_lock);
14925 mutex_enter(&dtrace_lock);
14926
14927#ifndef VBOX
14928 if (ddi_soft_state_init(&dtrace_softstate,
14929 sizeof (dtrace_state_t), 0) != 0) {
14930 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state");
14931 mutex_exit(&cpu_lock);
14932 mutex_exit(&dtrace_provider_lock);
14933 mutex_exit(&dtrace_lock);
14934 return (DDI_FAILURE);
14935 }
14936
14937 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR,
14938 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE ||
14939 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR,
14940 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) {
14941 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes");
14942 ddi_remove_minor_node(devi, NULL);
14943 ddi_soft_state_fini(&dtrace_softstate);
14944 mutex_exit(&cpu_lock);
14945 mutex_exit(&dtrace_provider_lock);
14946 mutex_exit(&dtrace_lock);
14947 return (DDI_FAILURE);
14948 }
14949
14950 ddi_report_dev(devi);
14951 dtrace_devi = devi;
14952
14953 dtrace_modload = dtrace_module_loaded;
14954 dtrace_modunload = dtrace_module_unloaded;
14955 dtrace_cpu_init = dtrace_cpu_setup_initial;
14956 dtrace_helpers_cleanup = dtrace_helpers_destroy;
14957 dtrace_helpers_fork = dtrace_helpers_duplicate;
14958 dtrace_cpustart_init = dtrace_suspend;
14959 dtrace_cpustart_fini = dtrace_resume;
14960 dtrace_debugger_init = dtrace_suspend;
14961 dtrace_debugger_fini = dtrace_resume;
14962
14963 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
14964#else
14965 /** @todo some of these hooks needs checking out! */
14966#endif
14967
14968 ASSERT(MUTEX_HELD(&cpu_lock));
14969
14970#ifndef VBOX /* Reduce the area a bit just to be sure our vmem fake doesn't blow up. */
14971 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1,
14972 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
14973#else
14974 dtrace_arena = vmem_create("dtrace", (void *)(uintptr_t)1, UINT32_MAX - 16, 1,
14975 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
14976#endif
14977#ifndef VBOX
14978 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE,
14979 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0,
14980 VM_SLEEP | VMC_IDENTIFIER);
14981 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri,
14982 1, INT_MAX, 0);
14983#endif
14984
14985 dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
14986 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
14987 NULL, NULL, NULL, NULL, NULL, 0);
14988
14989 ASSERT(MUTEX_HELD(&cpu_lock));
14990 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
14991 offsetof(dtrace_probe_t, dtpr_nextmod),
14992 offsetof(dtrace_probe_t, dtpr_prevmod));
14993
14994 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
14995 offsetof(dtrace_probe_t, dtpr_nextfunc),
14996 offsetof(dtrace_probe_t, dtpr_prevfunc));
14997
14998 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
14999 offsetof(dtrace_probe_t, dtpr_nextname),
15000 offsetof(dtrace_probe_t, dtpr_prevname));
15001
15002 if (dtrace_retain_max < 1) {
15003 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
15004 "setting to 1", dtrace_retain_max);
15005 dtrace_retain_max = 1;
15006 }
15007
15008 /*
15009 * Now discover our toxic ranges.
15010 */
15011 dtrace_toxic_ranges(dtrace_toxrange_add);
15012
15013 /*
15014 * Before we register ourselves as a provider to our own framework,
15015 * we would like to assert that dtrace_provider is NULL -- but that's
15016 * not true if we were loaded as a dependency of a DTrace provider.
15017 * Once we've registered, we can assert that dtrace_provider is our
15018 * pseudo provider.
15019 */
15020 (void) dtrace_register("dtrace", &dtrace_provider_attr,
15021 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
15022
15023 ASSERT(dtrace_provider != NULL);
15024 ASSERT((dtrace_provider_id_t)dtrace_provider == id);
15025
15026 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
15027 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
15028 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
15029 dtrace_provider, NULL, NULL, "END", 0, NULL);
15030 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
15031 dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
15032
15033#ifndef VBOX
15034 dtrace_anon_property();
15035#endif
15036 mutex_exit(&cpu_lock);
15037
15038 /*
15039 * If DTrace helper tracing is enabled, we need to allocate the
15040 * trace buffer and initialize the values.
15041 */
15042 if (dtrace_helptrace_enabled) {
15043 ASSERT(dtrace_helptrace_buffer == NULL);
15044 dtrace_helptrace_buffer =
15045 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
15046 dtrace_helptrace_next = 0;
15047 }
15048
15049 /*
15050 * If there are already providers, we must ask them to provide their
15051 * probes, and then match any anonymous enabling against them. Note
15052 * that there should be no other retained enablings at this time:
15053 * the only retained enablings at this time should be the anonymous
15054 * enabling.
15055 */
15056 if (dtrace_anon.dta_enabling != NULL) {
15057 ASSERT(dtrace_retained == dtrace_anon.dta_enabling);
15058
15059 dtrace_enabling_provide(NULL);
15060 state = dtrace_anon.dta_state;
15061
15062 /*
15063 * We couldn't hold cpu_lock across the above call to
15064 * dtrace_enabling_provide(), but we must hold it to actually
15065 * enable the probes. We have to drop all of our locks, pick
15066 * up cpu_lock, and regain our locks before matching the
15067 * retained anonymous enabling.
15068 */
15069 mutex_exit(&dtrace_lock);
15070 mutex_exit(&dtrace_provider_lock);
15071
15072 mutex_enter(&cpu_lock);
15073 mutex_enter(&dtrace_provider_lock);
15074 mutex_enter(&dtrace_lock);
15075
15076 if ((enab = dtrace_anon.dta_enabling) != NULL)
15077 (void) dtrace_enabling_match(enab, NULL);
15078
15079 mutex_exit(&cpu_lock);
15080 }
15081
15082 mutex_exit(&dtrace_lock);
15083 mutex_exit(&dtrace_provider_lock);
15084
15085 if (state != NULL) {
15086 /*
15087 * If we created any anonymous state, set it going now.
15088 */
15089 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon);
15090 }
15091
15092 return (DDI_SUCCESS);
15093}
15094
15095#ifdef VBOX
15096int dtrace_open(dtrace_state_t **ppState, cred_t *cred_p)
15097#else
15098/*ARGSUSED*/
15099static int
15100dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
15101#endif
15102{
15103 dtrace_state_t *state;
15104 uint32_t priv;
15105 uid_t uid;
15106 zoneid_t zoneid;
15107
15108#ifndef VBOX
15109 if (getminor(*devp) == DTRACEMNRN_HELPER)
15110 return (0);
15111
15112 /*
15113 * If this wasn't an open with the "helper" minor, then it must be
15114 * the "dtrace" minor.
15115 */
15116 if (getminor(*devp) != DTRACEMNRN_DTRACE)
15117 return (ENXIO);
15118#endif /* !VBOX */
15119
15120 /*
15121 * If no DTRACE_PRIV_* bits are set in the credential, then the
15122 * caller lacks sufficient permission to do anything with DTrace.
15123 */
15124 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid);
15125 if (priv == DTRACE_PRIV_NONE)
15126 return (EACCES);
15127
15128 /*
15129 * Ask all providers to provide all their probes.
15130 */
15131 mutex_enter(&dtrace_provider_lock);
15132 dtrace_probe_provide(NULL, NULL);
15133 mutex_exit(&dtrace_provider_lock);
15134
15135 mutex_enter(&cpu_lock);
15136 mutex_enter(&dtrace_lock);
15137 dtrace_opens++;
15138 dtrace_membar_producer();
15139
15140#ifndef VBOX
15141 /*
15142 * If the kernel debugger is active (that is, if the kernel debugger
15143 * modified text in some way), we won't allow the open.
15144 */
15145 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
15146 dtrace_opens--;
15147 mutex_exit(&cpu_lock);
15148 mutex_exit(&dtrace_lock);
15149 return (EBUSY);
15150 }
15151#endif
15152
15153#ifndef VBOX
15154 state = dtrace_state_create(devp, cred_p);
15155#else
15156 state = dtrace_state_create(cred_p);
15157#endif
15158 mutex_exit(&cpu_lock);
15159
15160 if (state == NULL) {
15161#ifndef VBOX
15162 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
15163 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
15164#else
15165 dtrace_opens--;
15166#endif
15167 mutex_exit(&dtrace_lock);
15168 return (EAGAIN);
15169 }
15170
15171 mutex_exit(&dtrace_lock);
15172
15173#ifdef VBOX
15174 *ppState = state;
15175#endif
15176 return (0);
15177}
15178
15179#ifdef VBOX
15180int dtrace_close(dtrace_state_t *state)
15181#else
15182/*ARGSUSED*/
15183static int
15184dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
15185#endif
15186{
15187#ifndef VBOX
15188 minor_t minor = getminor(dev);
15189 dtrace_state_t *state;
15190
15191 if (minor == DTRACEMNRN_HELPER)
15192 return (0);
15193
15194 state = ddi_get_soft_state(dtrace_softstate, minor);
15195#endif
15196
15197 mutex_enter(&cpu_lock);
15198 mutex_enter(&dtrace_lock);
15199
15200 if (state->dts_anon) {
15201 /*
15202 * There is anonymous state. Destroy that first.
15203 */
15204 ASSERT(dtrace_anon.dta_state == NULL);
15205 dtrace_state_destroy(state->dts_anon);
15206 }
15207
15208 dtrace_state_destroy(state);
15209 ASSERT(dtrace_opens > 0);
15210
15211#ifndef VBOX
15212 /*
15213 * Only relinquish control of the kernel debugger interface when there
15214 * are no consumers and no anonymous enablings.
15215 */
15216 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
15217 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
15218#else
15219 dtrace_opens--;
15220#endif
15221
15222 mutex_exit(&dtrace_lock);
15223 mutex_exit(&cpu_lock);
15224
15225 return (0);
15226}
15227
15228#ifndef VBOX
15229/*ARGSUSED*/
15230static int
15231dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv)
15232{
15233 int rval;
15234 dof_helper_t help, *dhp = NULL;
15235
15236 switch (cmd) {
15237 case DTRACEHIOC_ADDDOF:
15238 if (copyin((void *)arg, &help, sizeof (help)) != 0) {
15239 dtrace_dof_error(NULL, "failed to copyin DOF helper");
15240 return (EFAULT);
15241 }
15242
15243 dhp = &help;
15244 arg = (intptr_t)help.dofhp_dof;
15245 RT_FALL_THRU();
15246
15247 case DTRACEHIOC_ADD: {
15248 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval);
15249
15250 if (dof == NULL)
15251 return (rval);
15252
15253 mutex_enter(&dtrace_lock);
15254
15255 /*
15256 * dtrace_helper_slurp() takes responsibility for the dof --
15257 * it may free it now or it may save it and free it later.
15258 */
15259 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) {
15260 *rv = rval;
15261 rval = 0;
15262 } else {
15263 rval = EINVAL;
15264 }
15265
15266 mutex_exit(&dtrace_lock);
15267 return (rval);
15268 }
15269
15270 case DTRACEHIOC_REMOVE: {
15271 mutex_enter(&dtrace_lock);
15272 rval = dtrace_helper_destroygen(arg);
15273 mutex_exit(&dtrace_lock);
15274
15275 return (rval);
15276 }
15277
15278 default:
15279 break;
15280 }
15281
15282 return (ENOTTY);
15283}
15284#endif /* !VBOX */
15285
15286#ifdef VBOX
15287int dtrace_ioctl(dtrace_state_t *state, int cmd, intptr_t arg, int32_t *rv)
15288#else
15289/*ARGSUSED*/
15290static int
15291dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
15292#endif
15293{
15294#ifndef VBOX
15295 minor_t minor = getminor(dev);
15296 dtrace_state_t *state;
15297#endif
15298 int rval;
15299
15300#ifndef VBOX
15301 if (minor == DTRACEMNRN_HELPER)
15302 return (dtrace_ioctl_helper(cmd, arg, rv));
15303
15304 state = ddi_get_soft_state(dtrace_softstate, minor);
15305#endif
15306
15307 if (state->dts_anon) {
15308 ASSERT(dtrace_anon.dta_state == NULL);
15309 state = state->dts_anon;
15310 }
15311
15312 switch (cmd) {
15313 case DTRACEIOC_PROVIDER: {
15314 dtrace_providerdesc_t pvd;
15315 dtrace_provider_t *pvp;
15316
15317 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0)
15318 return (EFAULT);
15319
15320 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
15321 mutex_enter(&dtrace_provider_lock);
15322
15323 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
15324 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0)
15325 break;
15326 }
15327
15328 mutex_exit(&dtrace_provider_lock);
15329
15330 if (pvp == NULL)
15331 return (ESRCH);
15332
15333 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t));
15334 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t));
15335 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0)
15336 return (EFAULT);
15337
15338 return (0);
15339 }
15340
15341 case DTRACEIOC_EPROBE: {
15342 dtrace_eprobedesc_t epdesc;
15343 dtrace_ecb_t *ecb;
15344 dtrace_action_t *act;
15345 void *buf;
15346 size_t size;
15347 uintptr_t dest;
15348 int nrecs;
15349
15350 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0)
15351 return (EFAULT);
15352
15353 mutex_enter(&dtrace_lock);
15354
15355 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
15356 mutex_exit(&dtrace_lock);
15357 return (EINVAL);
15358 }
15359
15360 if (ecb->dte_probe == NULL) {
15361 mutex_exit(&dtrace_lock);
15362 return (EINVAL);
15363 }
15364
15365 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
15366 epdesc.dtepd_uarg = ecb->dte_uarg;
15367 epdesc.dtepd_size = VBDTCAST(uint32_t)ecb->dte_size;
15368
15369 nrecs = epdesc.dtepd_nrecs;
15370 epdesc.dtepd_nrecs = 0;
15371 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
15372 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
15373 continue;
15374
15375 epdesc.dtepd_nrecs++;
15376 }
15377
15378 /*
15379 * Now that we have the size, we need to allocate a temporary
15380 * buffer in which to store the complete description. We need
15381 * the temporary buffer to be able to drop dtrace_lock()
15382 * across the copyout(), below.
15383 */
15384 size = sizeof (dtrace_eprobedesc_t) +
15385 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
15386
15387 buf = kmem_alloc(size, KM_SLEEP);
15388 dest = (uintptr_t)buf;
15389
15390 bcopy(&epdesc, (void *)dest, sizeof (epdesc));
15391 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
15392
15393 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
15394 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
15395 continue;
15396
15397 if (nrecs-- == 0)
15398 break;
15399
15400 bcopy(&act->dta_rec, (void *)dest,
15401 sizeof (dtrace_recdesc_t));
15402 dest += sizeof (dtrace_recdesc_t);
15403 }
15404
15405 mutex_exit(&dtrace_lock);
15406
15407 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
15408 kmem_free(buf, size);
15409 return (EFAULT);
15410 }
15411
15412 kmem_free(buf, size);
15413 return (0);
15414 }
15415
15416 case DTRACEIOC_AGGDESC: {
15417 dtrace_aggdesc_t aggdesc;
15418 dtrace_action_t *act;
15419 dtrace_aggregation_t *agg;
15420 int nrecs;
15421 uint32_t offs;
15422 dtrace_recdesc_t *lrec;
15423 void *buf;
15424 size_t size;
15425 uintptr_t dest;
15426
15427 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0)
15428 return (EFAULT);
15429
15430 mutex_enter(&dtrace_lock);
15431
15432 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
15433 mutex_exit(&dtrace_lock);
15434 return (EINVAL);
15435 }
15436
15437 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
15438
15439 nrecs = aggdesc.dtagd_nrecs;
15440 aggdesc.dtagd_nrecs = 0;
15441
15442 offs = agg->dtag_base;
15443 lrec = &agg->dtag_action.dta_rec;
15444 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
15445
15446 for (act = agg->dtag_first; ; act = act->dta_next) {
15447 ASSERT(act->dta_intuple ||
15448 DTRACEACT_ISAGG(act->dta_kind));
15449
15450 /*
15451 * If this action has a record size of zero, it
15452 * denotes an argument to the aggregating action.
15453 * Because the presence of this record doesn't (or
15454 * shouldn't) affect the way the data is interpreted,
15455 * we don't copy it out to save user-level the
15456 * confusion of dealing with a zero-length record.
15457 */
15458 if (act->dta_rec.dtrd_size == 0) {
15459 ASSERT(agg->dtag_hasarg);
15460 continue;
15461 }
15462
15463 aggdesc.dtagd_nrecs++;
15464
15465 if (act == &agg->dtag_action)
15466 break;
15467 }
15468
15469 /*
15470 * Now that we have the size, we need to allocate a temporary
15471 * buffer in which to store the complete description. We need
15472 * the temporary buffer to be able to drop dtrace_lock()
15473 * across the copyout(), below.
15474 */
15475 size = sizeof (dtrace_aggdesc_t) +
15476 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
15477
15478 buf = kmem_alloc(size, KM_SLEEP);
15479 dest = (uintptr_t)buf;
15480
15481 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
15482 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
15483
15484 for (act = agg->dtag_first; ; act = act->dta_next) {
15485 dtrace_recdesc_t rec = act->dta_rec;
15486
15487 /*
15488 * See the comment in the above loop for why we pass
15489 * over zero-length records.
15490 */
15491 if (rec.dtrd_size == 0) {
15492 ASSERT(agg->dtag_hasarg);
15493 continue;
15494 }
15495
15496 if (nrecs-- == 0)
15497 break;
15498
15499 rec.dtrd_offset -= offs;
15500 bcopy(&rec, (void *)dest, sizeof (rec));
15501 dest += sizeof (dtrace_recdesc_t);
15502
15503 if (act == &agg->dtag_action)
15504 break;
15505 }
15506
15507 mutex_exit(&dtrace_lock);
15508
15509 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
15510 kmem_free(buf, size);
15511 return (EFAULT);
15512 }
15513
15514 kmem_free(buf, size);
15515 return (0);
15516 }
15517
15518 case DTRACEIOC_ENABLE: {
15519 dof_hdr_t *dof;
15520 dtrace_enabling_t *enab = NULL;
15521 dtrace_vstate_t *vstate;
15522 int err = 0;
15523#ifdef VBOX
15524 cred_t *cr = CRED();
15525#endif
15526
15527 *rv = 0;
15528
15529 /*
15530 * If a NULL argument has been passed, we take this as our
15531 * cue to reevaluate our enablings.
15532 */
15533 if (arg == NULL) {
15534 dtrace_enabling_matchall();
15535
15536 return (0);
15537 }
15538
15539 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL)
15540 return (rval);
15541
15542 mutex_enter(&cpu_lock);
15543 mutex_enter(&dtrace_lock);
15544 vstate = &state->dts_vstate;
15545
15546 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
15547 mutex_exit(&dtrace_lock);
15548 mutex_exit(&cpu_lock);
15549 dtrace_dof_destroy(dof);
15550 return (EBUSY);
15551 }
15552
15553 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) {
15554 mutex_exit(&dtrace_lock);
15555 mutex_exit(&cpu_lock);
15556 dtrace_dof_destroy(dof);
15557 return (EINVAL);
15558 }
15559
15560 if ((rval = dtrace_dof_options(dof, state)) != 0) {
15561 dtrace_enabling_destroy(enab);
15562 mutex_exit(&dtrace_lock);
15563 mutex_exit(&cpu_lock);
15564 dtrace_dof_destroy(dof);
15565 return (rval);
15566 }
15567
15568 if ((err = dtrace_enabling_match(enab, rv)) == 0) {
15569 err = dtrace_enabling_retain(enab);
15570 } else {
15571 dtrace_enabling_destroy(enab);
15572 }
15573
15574 mutex_exit(&cpu_lock);
15575 mutex_exit(&dtrace_lock);
15576 dtrace_dof_destroy(dof);
15577
15578 return (err);
15579 }
15580
15581 case DTRACEIOC_REPLICATE: {
15582 dtrace_repldesc_t desc;
15583 dtrace_probedesc_t *match = &desc.dtrpd_match;
15584 dtrace_probedesc_t *create = &desc.dtrpd_create;
15585 int err;
15586
15587 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
15588 return (EFAULT);
15589
15590 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
15591 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
15592 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
15593 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
15594
15595 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
15596 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
15597 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
15598 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
15599
15600 mutex_enter(&dtrace_lock);
15601 err = dtrace_enabling_replicate(state, match, create);
15602 mutex_exit(&dtrace_lock);
15603
15604 return (err);
15605 }
15606
15607 case DTRACEIOC_PROBEMATCH:
15608 case DTRACEIOC_PROBES: {
15609 dtrace_probe_t *probe = NULL;
15610 dtrace_probedesc_t desc;
15611 dtrace_probekey_t pkey;
15612 dtrace_id_t i;
15613 int m = 0;
15614 uint32_t priv;
15615 uid_t uid;
15616 zoneid_t zoneid;
15617#ifdef VBOX
15618 cred_t *cr = CRED();
15619#endif
15620
15621 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
15622 return (EFAULT);
15623
15624 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
15625 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
15626 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
15627 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0';
15628
15629 /*
15630 * Before we attempt to match this probe, we want to give
15631 * all providers the opportunity to provide it.
15632 */
15633 if (desc.dtpd_id == DTRACE_IDNONE) {
15634 mutex_enter(&dtrace_provider_lock);
15635 dtrace_probe_provide(&desc, NULL);
15636 mutex_exit(&dtrace_provider_lock);
15637 desc.dtpd_id++;
15638 }
15639
15640 if (cmd == DTRACEIOC_PROBEMATCH) {
15641 dtrace_probekey(&desc, &pkey);
15642 pkey.dtpk_id = DTRACE_IDNONE;
15643 }
15644
15645 dtrace_cred2priv(cr, &priv, &uid, &zoneid);
15646
15647 mutex_enter(&dtrace_lock);
15648
15649 if (cmd == DTRACEIOC_PROBEMATCH) {
15650 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
15651 if ((probe = dtrace_probes[i - 1]) != NULL &&
15652 (m = dtrace_match_probe(probe, &pkey,
15653 priv, uid, zoneid)) != 0)
15654 break;
15655 }
15656
15657 if (m < 0) {
15658 mutex_exit(&dtrace_lock);
15659 return (EINVAL);
15660 }
15661
15662 } else {
15663 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
15664 if ((probe = dtrace_probes[i - 1]) != NULL &&
15665 dtrace_match_priv(probe, priv, uid, zoneid))
15666 break;
15667 }
15668 }
15669
15670 if (probe == NULL) {
15671 mutex_exit(&dtrace_lock);
15672 return (ESRCH);
15673 }
15674
15675 dtrace_probe_description(probe, &desc);
15676 mutex_exit(&dtrace_lock);
15677
15678 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
15679 return (EFAULT);
15680
15681 return (0);
15682 }
15683
15684 case DTRACEIOC_PROBEARG: {
15685 dtrace_argdesc_t desc;
15686 dtrace_probe_t *probe;
15687 dtrace_provider_t *prov;
15688
15689 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
15690 return (EFAULT);
15691
15692 if (desc.dtargd_id == DTRACE_IDNONE)
15693 return (EINVAL);
15694
15695 if (desc.dtargd_ndx == DTRACE_ARGNONE)
15696 return (EINVAL);
15697
15698 mutex_enter(&dtrace_provider_lock);
15699 mutex_enter(&mod_lock);
15700 mutex_enter(&dtrace_lock);
15701
15702 if (desc.dtargd_id > dtrace_nprobes) {
15703 mutex_exit(&dtrace_lock);
15704 mutex_exit(&mod_lock);
15705 mutex_exit(&dtrace_provider_lock);
15706 return (EINVAL);
15707 }
15708
15709 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) {
15710 mutex_exit(&dtrace_lock);
15711 mutex_exit(&mod_lock);
15712 mutex_exit(&dtrace_provider_lock);
15713 return (EINVAL);
15714 }
15715
15716 mutex_exit(&dtrace_lock);
15717
15718 prov = probe->dtpr_provider;
15719
15720 if (prov->dtpv_pops.dtps_getargdesc == NULL) {
15721 /*
15722 * There isn't any typed information for this probe.
15723 * Set the argument number to DTRACE_ARGNONE.
15724 */
15725 desc.dtargd_ndx = DTRACE_ARGNONE;
15726 } else {
15727 desc.dtargd_native[0] = '\0';
15728 desc.dtargd_xlate[0] = '\0';
15729 desc.dtargd_mapping = desc.dtargd_ndx;
15730
15731 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
15732 probe->dtpr_id, probe->dtpr_arg, &desc);
15733 }
15734
15735 mutex_exit(&mod_lock);
15736 mutex_exit(&dtrace_provider_lock);
15737
15738 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
15739 return (EFAULT);
15740
15741 return (0);
15742 }
15743
15744 case DTRACEIOC_GO: {
15745 processorid_t cpuid;
15746 rval = dtrace_state_go(state, &cpuid);
15747
15748 if (rval != 0)
15749 return (rval);
15750
15751 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
15752 return (EFAULT);
15753
15754 return (0);
15755 }
15756
15757 case DTRACEIOC_STOP: {
15758 processorid_t cpuid;
15759
15760 mutex_enter(&dtrace_lock);
15761 rval = dtrace_state_stop(state, &cpuid);
15762 mutex_exit(&dtrace_lock);
15763
15764 if (rval != 0)
15765 return (rval);
15766
15767 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
15768 return (EFAULT);
15769
15770 return (0);
15771 }
15772
15773 case DTRACEIOC_DOFGET: {
15774 dof_hdr_t hdr, *dof;
15775 uint64_t len;
15776
15777 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0)
15778 return (EFAULT);
15779
15780 mutex_enter(&dtrace_lock);
15781 dof = dtrace_dof_create(state);
15782 mutex_exit(&dtrace_lock);
15783
15784 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
15785 rval = copyout(dof, (void *)arg, len);
15786 dtrace_dof_destroy(dof);
15787
15788 return (rval == 0 ? 0 : EFAULT);
15789 }
15790
15791 case DTRACEIOC_AGGSNAP:
15792 case DTRACEIOC_BUFSNAP: {
15793 dtrace_bufdesc_t desc;
15794 caddr_t cached;
15795 dtrace_buffer_t *buf;
15796
15797 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
15798 return (EFAULT);
15799
15800 if (/*VBox value is is unsigned: desc.dtbd_cpu < 0 ||*/ desc.dtbd_cpu >= NCPU)
15801 return (EINVAL);
15802
15803 mutex_enter(&dtrace_lock);
15804
15805 if (cmd == DTRACEIOC_BUFSNAP) {
15806 buf = &state->dts_buffer[desc.dtbd_cpu];
15807 } else {
15808 buf = &state->dts_aggbuffer[desc.dtbd_cpu];
15809 }
15810
15811 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
15812 size_t sz = buf->dtb_offset;
15813
15814 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
15815 mutex_exit(&dtrace_lock);
15816 return (EBUSY);
15817 }
15818
15819 /*
15820 * If this buffer has already been consumed, we're
15821 * going to indicate that there's nothing left here
15822 * to consume.
15823 */
15824 if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
15825 mutex_exit(&dtrace_lock);
15826
15827 desc.dtbd_size = 0;
15828 desc.dtbd_drops = 0;
15829 desc.dtbd_errors = 0;
15830 desc.dtbd_oldest = 0;
15831 sz = sizeof (desc);
15832
15833 if (copyout(&desc, (void *)arg, sz) != 0)
15834 return (EFAULT);
15835
15836 return (0);
15837 }
15838
15839 /*
15840 * If this is a ring buffer that has wrapped, we want
15841 * to copy the whole thing out.
15842 */
15843 if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
15844 dtrace_buffer_polish(buf);
15845 sz = buf->dtb_size;
15846 }
15847
15848 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
15849 mutex_exit(&dtrace_lock);
15850 return (EFAULT);
15851 }
15852
15853 desc.dtbd_size = sz;
15854 desc.dtbd_drops = buf->dtb_drops;
15855 desc.dtbd_errors = buf->dtb_errors;
15856 desc.dtbd_oldest = buf->dtb_xamot_offset;
15857
15858 mutex_exit(&dtrace_lock);
15859
15860 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
15861 return (EFAULT);
15862
15863 buf->dtb_flags |= DTRACEBUF_CONSUMED;
15864
15865 return (0);
15866 }
15867
15868 if (buf->dtb_tomax == NULL) {
15869 ASSERT(buf->dtb_xamot == NULL);
15870 mutex_exit(&dtrace_lock);
15871 return (ENOENT);
15872 }
15873
15874 cached = buf->dtb_tomax;
15875 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
15876
15877#ifndef VBOX
15878 dtrace_xcall(desc.dtbd_cpu,
15879 (dtrace_xcall_t)dtrace_buffer_switch, buf);
15880#else
15881 if (desc.dtbd_cpu == DTRACE_CPUALL)
15882 RTMpOnAll(dtrace_buffer_switch_wrapper, buf, NULL);
15883 else
15884 RTMpOnSpecific(desc.dtbd_cpu, dtrace_buffer_switch_wrapper, buf, NULL);
15885#endif
15886
15887 state->dts_errors += buf->dtb_xamot_errors;
15888
15889 /*
15890 * If the buffers did not actually switch, then the cross call
15891 * did not take place -- presumably because the given CPU is
15892 * not in the ready set. If this is the case, we'll return
15893 * ENOENT.
15894 */
15895 if (buf->dtb_tomax == cached) {
15896 ASSERT(buf->dtb_xamot != cached);
15897 mutex_exit(&dtrace_lock);
15898 return (ENOENT);
15899 }
15900
15901 ASSERT(cached == buf->dtb_xamot);
15902
15903 /*
15904 * We have our snapshot; now copy it out.
15905 */
15906 if (copyout(buf->dtb_xamot, desc.dtbd_data,
15907 buf->dtb_xamot_offset) != 0) {
15908 mutex_exit(&dtrace_lock);
15909 return (EFAULT);
15910 }
15911
15912 desc.dtbd_size = buf->dtb_xamot_offset;
15913 desc.dtbd_drops = buf->dtb_xamot_drops;
15914 desc.dtbd_errors = buf->dtb_xamot_errors;
15915 desc.dtbd_oldest = 0;
15916
15917 mutex_exit(&dtrace_lock);
15918
15919 /*
15920 * Finally, copy out the buffer description.
15921 */
15922 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
15923 return (EFAULT);
15924
15925 return (0);
15926 }
15927
15928 case DTRACEIOC_CONF: {
15929 dtrace_conf_t conf;
15930
15931 bzero(&conf, sizeof (conf));
15932 conf.dtc_difversion = DIF_VERSION;
15933 conf.dtc_difintregs = DIF_DIR_NREGS;
15934 conf.dtc_diftupregs = DIF_DTR_NREGS;
15935 conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
15936
15937 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0)
15938 return (EFAULT);
15939
15940 return (0);
15941 }
15942
15943 case DTRACEIOC_STATUS: {
15944 dtrace_status_t stat;
15945 dtrace_dstate_t *dstate;
15946 int i, j;
15947 uint64_t nerrs;
15948
15949 /*
15950 * See the comment in dtrace_state_deadman() for the reason
15951 * for setting dts_laststatus to INT64_MAX before setting
15952 * it to the correct value.
15953 */
15954 state->dts_laststatus = INT64_MAX;
15955 dtrace_membar_producer();
15956 state->dts_laststatus = dtrace_gethrtime();
15957
15958 bzero(&stat, sizeof (stat));
15959
15960 mutex_enter(&dtrace_lock);
15961
15962 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
15963 mutex_exit(&dtrace_lock);
15964 return (ENOENT);
15965 }
15966
15967 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
15968 stat.dtst_exiting = 1;
15969
15970 nerrs = state->dts_errors;
15971 dstate = &state->dts_vstate.dtvs_dynvars;
15972
15973 for (i = 0; i < NCPU; i++) {
15974 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
15975
15976 stat.dtst_dyndrops += dcpu->dtdsc_drops;
15977 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
15978 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
15979
15980 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
15981 stat.dtst_filled++;
15982
15983 nerrs += state->dts_buffer[i].dtb_errors;
15984
15985 for (j = 0; j < state->dts_nspeculations; j++) {
15986 dtrace_speculation_t *spec;
15987 dtrace_buffer_t *buf;
15988
15989 spec = &state->dts_speculations[j];
15990 buf = &spec->dtsp_buffer[i];
15991 stat.dtst_specdrops += buf->dtb_xamot_drops;
15992 }
15993 }
15994
15995 stat.dtst_specdrops_busy = state->dts_speculations_busy;
15996 stat.dtst_specdrops_unavail = state->dts_speculations_unavail;
15997 stat.dtst_stkstroverflows = state->dts_stkstroverflows;
15998 stat.dtst_dblerrors = state->dts_dblerrors;
15999 stat.dtst_killed =
16000 (state->dts_activity == DTRACE_ACTIVITY_KILLED);
16001 stat.dtst_errors = nerrs;
16002
16003 mutex_exit(&dtrace_lock);
16004
16005 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0)
16006 return (EFAULT);
16007
16008 return (0);
16009 }
16010
16011 case DTRACEIOC_FORMAT: {
16012 dtrace_fmtdesc_t fmt;
16013 char *str;
16014 int len;
16015
16016 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0)
16017 return (EFAULT);
16018
16019 mutex_enter(&dtrace_lock);
16020
16021 if (fmt.dtfd_format == 0 ||
16022 fmt.dtfd_format > state->dts_nformats) {
16023 mutex_exit(&dtrace_lock);
16024 return (EINVAL);
16025 }
16026
16027 /*
16028 * Format strings are allocated contiguously and they are
16029 * never freed; if a format index is less than the number
16030 * of formats, we can assert that the format map is non-NULL
16031 * and that the format for the specified index is non-NULL.
16032 */
16033 ASSERT(state->dts_formats != NULL);
16034 str = state->dts_formats[fmt.dtfd_format - 1];
16035 ASSERT(str != NULL);
16036
16037 len = VBDTCAST(int)strlen(str) + 1;
16038
16039 if (len > fmt.dtfd_length) {
16040 fmt.dtfd_length = len;
16041
16042 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) {
16043 mutex_exit(&dtrace_lock);
16044 return (EINVAL);
16045 }
16046 } else {
16047 if (copyout(str, fmt.dtfd_string, len) != 0) {
16048 mutex_exit(&dtrace_lock);
16049 return (EINVAL);
16050 }
16051 }
16052
16053 mutex_exit(&dtrace_lock);
16054 return (0);
16055 }
16056
16057 default:
16058 break;
16059 }
16060
16061 return (ENOTTY);
16062}
16063
16064#ifdef VBOX
16065int dtrace_detach(void)
16066#else
16067/*ARGSUSED*/
16068static int
16069dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
16070#endif
16071{
16072 dtrace_state_t *state;
16073
16074#ifndef VBOX
16075 switch (cmd) {
16076 case DDI_DETACH:
16077 break;
16078
16079 case DDI_SUSPEND:
16080 return (DDI_SUCCESS);
16081
16082 default:
16083 return (DDI_FAILURE);
16084 }
16085#endif
16086
16087 mutex_enter(&cpu_lock);
16088 mutex_enter(&dtrace_provider_lock);
16089 mutex_enter(&dtrace_lock);
16090
16091 ASSERT(dtrace_opens == 0);
16092
16093 if (dtrace_helpers > 0) {
16094 mutex_exit(&dtrace_provider_lock);
16095 mutex_exit(&dtrace_lock);
16096 mutex_exit(&cpu_lock);
16097 return (DDI_FAILURE);
16098 }
16099
16100 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
16101 mutex_exit(&dtrace_provider_lock);
16102 mutex_exit(&dtrace_lock);
16103 mutex_exit(&cpu_lock);
16104 return (DDI_FAILURE);
16105 }
16106
16107 dtrace_provider = NULL;
16108
16109 if ((state = dtrace_anon_grab()) != NULL) {
16110 /*
16111 * If there were ECBs on this state, the provider should
16112 * have not been allowed to detach; assert that there is
16113 * none.
16114 */
16115 ASSERT(state->dts_necbs == 0);
16116 dtrace_state_destroy(state);
16117
16118#ifndef VBOX
16119 /*
16120 * If we're being detached with anonymous state, we need to
16121 * indicate to the kernel debugger that DTrace is now inactive.
16122 */
16123 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
16124#endif
16125 }
16126
16127 bzero(&dtrace_anon, sizeof (dtrace_anon_t));
16128#ifndef VBOX /** @todo CPU hooks */
16129 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
16130 dtrace_cpu_init = NULL;
16131 dtrace_helpers_cleanup = NULL;
16132 dtrace_helpers_fork = NULL;
16133 dtrace_cpustart_init = NULL;
16134 dtrace_cpustart_fini = NULL;
16135 dtrace_debugger_init = NULL;
16136 dtrace_debugger_fini = NULL;
16137 dtrace_modload = NULL;
16138 dtrace_modunload = NULL;
16139#endif
16140
16141 mutex_exit(&cpu_lock);
16142
16143 if (dtrace_helptrace_enabled) {
16144 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize);
16145 dtrace_helptrace_buffer = NULL;
16146 }
16147
16148 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *));
16149 dtrace_probes = NULL;
16150 dtrace_nprobes = 0;
16151
16152 dtrace_hash_destroy(dtrace_bymod);
16153 dtrace_hash_destroy(dtrace_byfunc);
16154 dtrace_hash_destroy(dtrace_byname);
16155 dtrace_bymod = NULL;
16156 dtrace_byfunc = NULL;
16157 dtrace_byname = NULL;
16158
16159 kmem_cache_destroy(dtrace_state_cache);
16160#ifndef VBOX
16161 vmem_destroy(dtrace_minor);
16162#endif
16163 vmem_destroy(dtrace_arena);
16164
16165 if (dtrace_toxrange != NULL) {
16166 kmem_free(dtrace_toxrange,
16167 dtrace_toxranges_max * sizeof (dtrace_toxrange_t));
16168 dtrace_toxrange = NULL;
16169 dtrace_toxranges = 0;
16170 dtrace_toxranges_max = 0;
16171 }
16172
16173#ifndef VBOX
16174 ddi_remove_minor_node(dtrace_devi, NULL);
16175 dtrace_devi = NULL;
16176
16177 ddi_soft_state_fini(&dtrace_softstate);
16178#endif
16179
16180 ASSERT(dtrace_vtime_references == 0);
16181 ASSERT(dtrace_opens == 0);
16182 ASSERT(dtrace_retained == NULL);
16183
16184 mutex_exit(&dtrace_lock);
16185 mutex_exit(&dtrace_provider_lock);
16186#ifdef VBOX
16187 VBoxDtMutexDelete(&dtrace_lock);
16188 VBoxDtMutexDelete(&dtrace_provider_lock);
16189 VBoxDtMutexDelete(&dtrace_meta_lock);
16190# ifdef DEBUG
16191 VBoxDtMutexDelete(&dtrace_errlock);
16192# endif
16193#endif
16194
16195 /*
16196 * We don't destroy the task queue until after we have dropped our
16197 * locks (taskq_destroy() may block on running tasks). To prevent
16198 * attempting to do work after we have effectively detached but before
16199 * the task queue has been destroyed, all tasks dispatched via the
16200 * task queue must check that DTrace is still attached before
16201 * performing any operation.
16202 */
16203#ifndef VBOX
16204 taskq_destroy(dtrace_taskq);
16205 dtrace_taskq = NULL;
16206#endif
16207
16208 return (DDI_SUCCESS);
16209}
16210
16211#ifndef VBOX
16212/*ARGSUSED*/
16213static int
16214dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
16215{
16216 int error;
16217
16218 switch (infocmd) {
16219 case DDI_INFO_DEVT2DEVINFO:
16220 *result = (void *)dtrace_devi;
16221 error = DDI_SUCCESS;
16222 break;
16223 case DDI_INFO_DEVT2INSTANCE:
16224 *result = (void *)0;
16225 error = DDI_SUCCESS;
16226 break;
16227 default:
16228 error = DDI_FAILURE;
16229 }
16230 return (error);
16231}
16232
16233static struct cb_ops dtrace_cb_ops = {
16234 dtrace_open, /* open */
16235 dtrace_close, /* close */
16236 nulldev, /* strategy */
16237 nulldev, /* print */
16238 nodev, /* dump */
16239 nodev, /* read */
16240 nodev, /* write */
16241 dtrace_ioctl, /* ioctl */
16242 nodev, /* devmap */
16243 nodev, /* mmap */
16244 nodev, /* segmap */
16245 nochpoll, /* poll */
16246 ddi_prop_op, /* cb_prop_op */
16247 0, /* streamtab */
16248 D_NEW | D_MP /* Driver compatibility flag */
16249};
16250
16251static struct dev_ops dtrace_ops = {
16252 DEVO_REV, /* devo_rev */
16253 0, /* refcnt */
16254 dtrace_info, /* get_dev_info */
16255 nulldev, /* identify */
16256 nulldev, /* probe */
16257 dtrace_attach, /* attach */
16258 dtrace_detach, /* detach */
16259 nodev, /* reset */
16260 &dtrace_cb_ops, /* driver operations */
16261 NULL, /* bus operations */
16262 nodev, /* dev power */
16263 ddi_quiesce_not_needed, /* quiesce */
16264};
16265
16266static struct modldrv modldrv = {
16267 &mod_driverops, /* module type (this is a pseudo driver) */
16268 "Dynamic Tracing", /* name of module */
16269 &dtrace_ops, /* driver ops */
16270};
16271
16272static struct modlinkage modlinkage = {
16273 MODREV_1,
16274 (void *)&modldrv,
16275 NULL
16276};
16277
16278int
16279_init(void)
16280{
16281 return (mod_install(&modlinkage));
16282}
16283
16284int
16285_info(struct modinfo *modinfop)
16286{
16287 return (mod_info(&modlinkage, modinfop));
16288}
16289
16290int
16291_fini(void)
16292{
16293 return (mod_remove(&modlinkage));
16294}
16295
16296#endif /* !VBOX */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette