xref: /freebsd/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c (revision ce3adf4362fcca6a43e500b2531f0038adbfbd21)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * $FreeBSD$
22  */
23 
24 /*
25  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
26  * Copyright (c) 2012 by Delphix. All rights reserved
27  * Use is subject to license terms.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * DTrace - Dynamic Tracing for Solaris
34  *
35  * This is the implementation of the Solaris Dynamic Tracing framework
36  * (DTrace).  The user-visible interface to DTrace is described at length in
37  * the "Solaris Dynamic Tracing Guide".  The interfaces between the libdtrace
38  * library, the in-kernel DTrace framework, and the DTrace providers are
39  * described in the block comments in the <sys/dtrace.h> header file.  The
40  * internal architecture of DTrace is described in the block comments in the
41  * <sys/dtrace_impl.h> header file.  The comments contained within the DTrace
42  * implementation very much assume mastery of all of these sources; if one has
43  * an unanswered question about the implementation, one should consult them
44  * first.
45  *
46  * The functions here are ordered roughly as follows:
47  *
48  *   - Probe context functions
49  *   - Probe hashing functions
50  *   - Non-probe context utility functions
51  *   - Matching functions
52  *   - Provider-to-Framework API functions
53  *   - Probe management functions
54  *   - DIF object functions
55  *   - Format functions
56  *   - Predicate functions
57  *   - ECB functions
58  *   - Buffer functions
59  *   - Enabling functions
60  *   - DOF functions
61  *   - Anonymous enabling functions
62  *   - Consumer state functions
63  *   - Helper functions
64  *   - Hook functions
65  *   - Driver cookbook functions
66  *
67  * Each group of functions begins with a block comment labelled the "DTrace
68  * [Group] Functions", allowing one to find each block by searching forward
69  * on capital-f functions.
70  */
71 #include <sys/errno.h>
72 #if !defined(sun)
73 #include <sys/time.h>
74 #endif
75 #include <sys/stat.h>
76 #include <sys/modctl.h>
77 #include <sys/conf.h>
78 #include <sys/systm.h>
79 #if defined(sun)
80 #include <sys/ddi.h>
81 #include <sys/sunddi.h>
82 #endif
83 #include <sys/cpuvar.h>
84 #include <sys/kmem.h>
85 #if defined(sun)
86 #include <sys/strsubr.h>
87 #endif
88 #include <sys/sysmacros.h>
89 #include <sys/dtrace_impl.h>
90 #include <sys/atomic.h>
91 #include <sys/cmn_err.h>
92 #if defined(sun)
93 #include <sys/mutex_impl.h>
94 #include <sys/rwlock_impl.h>
95 #endif
96 #include <sys/ctf_api.h>
97 #if defined(sun)
98 #include <sys/panic.h>
99 #include <sys/priv_impl.h>
100 #endif
101 #include <sys/policy.h>
102 #if defined(sun)
103 #include <sys/cred_impl.h>
104 #include <sys/procfs_isa.h>
105 #endif
106 #include <sys/taskq.h>
107 #if defined(sun)
108 #include <sys/mkdev.h>
109 #include <sys/kdi.h>
110 #endif
111 #include <sys/zone.h>
112 #include <sys/socket.h>
113 #include <netinet/in.h>
114 
115 /* FreeBSD includes: */
116 #if !defined(sun)
117 #include <sys/callout.h>
118 #include <sys/ctype.h>
119 #include <sys/eventhandler.h>
120 #include <sys/limits.h>
121 #include <sys/kdb.h>
122 #include <sys/kernel.h>
123 #include <sys/malloc.h>
124 #include <sys/sysctl.h>
125 #include <sys/lock.h>
126 #include <sys/mutex.h>
127 #include <sys/rwlock.h>
128 #include <sys/sx.h>
129 #include <sys/dtrace_bsd.h>
130 #include <netinet/in.h>
131 #include "dtrace_cddl.h"
132 #include "dtrace_debug.c"
133 #endif
134 
135 /*
136  * DTrace Tunable Variables
137  *
138  * The following variables may be tuned by adding a line to /etc/system that
139  * includes both the name of the DTrace module ("dtrace") and the name of the
140  * variable.  For example:
141  *
142  *   set dtrace:dtrace_destructive_disallow = 1
143  *
144  * In general, the only variables that one should be tuning this way are those
145  * that affect system-wide DTrace behavior, and for which the default behavior
146  * is undesirable.  Most of these variables are tunable on a per-consumer
147  * basis using DTrace options, and need not be tuned on a system-wide basis.
148  * When tuning these variables, avoid pathological values; while some attempt
149  * is made to verify the integrity of these variables, they are not considered
150  * part of the supported interface to DTrace, and they are therefore not
151  * checked comprehensively.  Further, these variables should not be tuned
152  * dynamically via "mdb -kw" or other means; they should only be tuned via
153  * /etc/system.
154  */
155 int		dtrace_destructive_disallow = 0;
156 dtrace_optval_t	dtrace_nonroot_maxsize = (16 * 1024 * 1024);
157 size_t		dtrace_difo_maxsize = (256 * 1024);
158 dtrace_optval_t	dtrace_dof_maxsize = (256 * 1024);
159 size_t		dtrace_global_maxsize = (16 * 1024);
160 size_t		dtrace_actions_max = (16 * 1024);
161 size_t		dtrace_retain_max = 1024;
162 dtrace_optval_t	dtrace_helper_actions_max = 128;
163 dtrace_optval_t	dtrace_helper_providers_max = 32;
164 dtrace_optval_t	dtrace_dstate_defsize = (1 * 1024 * 1024);
165 size_t		dtrace_strsize_default = 256;
166 dtrace_optval_t	dtrace_cleanrate_default = 9900990;		/* 101 hz */
167 dtrace_optval_t	dtrace_cleanrate_min = 200000;			/* 5000 hz */
168 dtrace_optval_t	dtrace_cleanrate_max = (uint64_t)60 * NANOSEC;	/* 1/minute */
169 dtrace_optval_t	dtrace_aggrate_default = NANOSEC;		/* 1 hz */
170 dtrace_optval_t	dtrace_statusrate_default = NANOSEC;		/* 1 hz */
171 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC;	 /* 6/minute */
172 dtrace_optval_t	dtrace_switchrate_default = NANOSEC;		/* 1 hz */
173 dtrace_optval_t	dtrace_nspec_default = 1;
174 dtrace_optval_t	dtrace_specsize_default = 32 * 1024;
175 dtrace_optval_t dtrace_stackframes_default = 20;
176 dtrace_optval_t dtrace_ustackframes_default = 20;
177 dtrace_optval_t dtrace_jstackframes_default = 50;
178 dtrace_optval_t dtrace_jstackstrsize_default = 512;
179 int		dtrace_msgdsize_max = 128;
180 hrtime_t	dtrace_chill_max = 500 * (NANOSEC / MILLISEC);	/* 500 ms */
181 hrtime_t	dtrace_chill_interval = NANOSEC;		/* 1000 ms */
182 int		dtrace_devdepth_max = 32;
183 int		dtrace_err_verbose;
184 hrtime_t	dtrace_deadman_interval = NANOSEC;
185 hrtime_t	dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC;
186 hrtime_t	dtrace_deadman_user = (hrtime_t)30 * NANOSEC;
187 hrtime_t	dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC;
188 
189 /*
190  * DTrace External Variables
191  *
192  * As dtrace(7D) is a kernel module, any DTrace variables are obviously
193  * available to DTrace consumers via the backtick (`) syntax.  One of these,
194  * dtrace_zero, is made deliberately so:  it is provided as a source of
195  * well-known, zero-filled memory.  While this variable is not documented,
196  * it is used by some translators as an implementation detail.
197  */
198 const char	dtrace_zero[256] = { 0 };	/* zero-filled memory */
199 
200 /*
201  * DTrace Internal Variables
202  */
203 #if defined(sun)
204 static dev_info_t	*dtrace_devi;		/* device info */
205 #endif
206 #if defined(sun)
207 static vmem_t		*dtrace_arena;		/* probe ID arena */
208 static vmem_t		*dtrace_minor;		/* minor number arena */
209 #else
210 static taskq_t		*dtrace_taskq;		/* task queue */
211 static struct unrhdr	*dtrace_arena;		/* Probe ID number.     */
212 #endif
213 static dtrace_probe_t	**dtrace_probes;	/* array of all probes */
214 static int		dtrace_nprobes;		/* number of probes */
215 static dtrace_provider_t *dtrace_provider;	/* provider list */
216 static dtrace_meta_t	*dtrace_meta_pid;	/* user-land meta provider */
217 static int		dtrace_opens;		/* number of opens */
218 static int		dtrace_helpers;		/* number of helpers */
219 #if defined(sun)
220 static void		*dtrace_softstate;	/* softstate pointer */
221 #endif
222 static dtrace_hash_t	*dtrace_bymod;		/* probes hashed by module */
223 static dtrace_hash_t	*dtrace_byfunc;		/* probes hashed by function */
224 static dtrace_hash_t	*dtrace_byname;		/* probes hashed by name */
225 static dtrace_toxrange_t *dtrace_toxrange;	/* toxic range array */
226 static int		dtrace_toxranges;	/* number of toxic ranges */
227 static int		dtrace_toxranges_max;	/* size of toxic range array */
228 static dtrace_anon_t	dtrace_anon;		/* anonymous enabling */
229 static kmem_cache_t	*dtrace_state_cache;	/* cache for dynamic state */
230 static uint64_t		dtrace_vtime_references; /* number of vtimestamp refs */
231 static kthread_t	*dtrace_panicked;	/* panicking thread */
232 static dtrace_ecb_t	*dtrace_ecb_create_cache; /* cached created ECB */
233 static dtrace_genid_t	dtrace_probegen;	/* current probe generation */
234 static dtrace_helpers_t *dtrace_deferred_pid;	/* deferred helper list */
235 static dtrace_enabling_t *dtrace_retained;	/* list of retained enablings */
236 static dtrace_dynvar_t	dtrace_dynhash_sink;	/* end of dynamic hash chains */
237 #if !defined(sun)
238 static struct mtx	dtrace_unr_mtx;
239 MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF);
240 int		dtrace_in_probe;	/* non-zero if executing a probe */
241 #if defined(__i386__) || defined(__amd64__) || defined(__mips__) || defined(__powerpc__)
242 uintptr_t	dtrace_in_probe_addr;	/* Address of invop when already in probe */
243 #endif
244 static eventhandler_tag	dtrace_kld_load_tag;
245 static eventhandler_tag	dtrace_kld_unload_try_tag;
246 #endif
247 
248 /*
249  * DTrace Locking
250  * DTrace is protected by three (relatively coarse-grained) locks:
251  *
252  * (1) dtrace_lock is required to manipulate essentially any DTrace state,
253  *     including enabling state, probes, ECBs, consumer state, helper state,
254  *     etc.  Importantly, dtrace_lock is _not_ required when in probe context;
255  *     probe context is lock-free -- synchronization is handled via the
256  *     dtrace_sync() cross call mechanism.
257  *
258  * (2) dtrace_provider_lock is required when manipulating provider state, or
259  *     when provider state must be held constant.
260  *
261  * (3) dtrace_meta_lock is required when manipulating meta provider state, or
262  *     when meta provider state must be held constant.
263  *
264  * The lock ordering between these three locks is dtrace_meta_lock before
265  * dtrace_provider_lock before dtrace_lock.  (In particular, there are
266  * several places where dtrace_provider_lock is held by the framework as it
267  * calls into the providers -- which then call back into the framework,
268  * grabbing dtrace_lock.)
269  *
270  * There are two other locks in the mix:  mod_lock and cpu_lock.  With respect
271  * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
272  * role as a coarse-grained lock; it is acquired before both of these locks.
273  * With respect to dtrace_meta_lock, its behavior is stranger:  cpu_lock must
274  * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
275  * mod_lock is similar with respect to dtrace_provider_lock in that it must be
276  * acquired _between_ dtrace_provider_lock and dtrace_lock.
277  */
278 static kmutex_t		dtrace_lock;		/* probe state lock */
279 static kmutex_t		dtrace_provider_lock;	/* provider state lock */
280 static kmutex_t		dtrace_meta_lock;	/* meta-provider state lock */
281 
282 #if !defined(sun)
283 /* XXX FreeBSD hacks. */
284 #define cr_suid		cr_svuid
285 #define cr_sgid		cr_svgid
286 #define	ipaddr_t	in_addr_t
287 #define mod_modname	pathname
288 #define vuprintf	vprintf
289 #define ttoproc(_a)	((_a)->td_proc)
290 #define crgetzoneid(_a)	0
291 #define	NCPU		MAXCPU
292 #define SNOCD		0
293 #define CPU_ON_INTR(_a)	0
294 
295 #define PRIV_EFFECTIVE		(1 << 0)
296 #define PRIV_DTRACE_KERNEL	(1 << 1)
297 #define PRIV_DTRACE_PROC	(1 << 2)
298 #define PRIV_DTRACE_USER	(1 << 3)
299 #define PRIV_PROC_OWNER		(1 << 4)
300 #define PRIV_PROC_ZONE		(1 << 5)
301 #define PRIV_ALL		~0
302 
303 SYSCTL_NODE(_debug, OID_AUTO, dtrace, CTLFLAG_RD, 0, "DTrace Information");
304 #endif
305 
306 #if defined(sun)
307 #define curcpu	CPU->cpu_id
308 #endif
309 
310 
311 /*
312  * DTrace Provider Variables
313  *
314  * These are the variables relating to DTrace as a provider (that is, the
315  * provider of the BEGIN, END, and ERROR probes).
316  */
317 static dtrace_pattr_t	dtrace_provider_attr = {
318 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
319 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
320 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
321 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
322 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
323 };
324 
325 static void
326 dtrace_nullop(void)
327 {}
328 
329 static dtrace_pops_t	dtrace_provider_ops = {
330 	(void (*)(void *, dtrace_probedesc_t *))dtrace_nullop,
331 	(void (*)(void *, modctl_t *))dtrace_nullop,
332 	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
333 	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
334 	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
335 	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
336 	NULL,
337 	NULL,
338 	NULL,
339 	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop
340 };
341 
342 static dtrace_id_t	dtrace_probeid_begin;	/* special BEGIN probe */
343 static dtrace_id_t	dtrace_probeid_end;	/* special END probe */
344 dtrace_id_t		dtrace_probeid_error;	/* special ERROR probe */
345 
346 /*
347  * DTrace Helper Tracing Variables
348  */
349 uint32_t dtrace_helptrace_next = 0;
350 uint32_t dtrace_helptrace_nlocals;
351 char	*dtrace_helptrace_buffer;
352 int	dtrace_helptrace_bufsize = 512 * 1024;
353 
354 #ifdef DEBUG
355 int	dtrace_helptrace_enabled = 1;
356 #else
357 int	dtrace_helptrace_enabled = 0;
358 #endif
359 
360 /*
361  * DTrace Error Hashing
362  *
363  * On DEBUG kernels, DTrace will track the errors that has seen in a hash
364  * table.  This is very useful for checking coverage of tests that are
365  * expected to induce DIF or DOF processing errors, and may be useful for
366  * debugging problems in the DIF code generator or in DOF generation .  The
367  * error hash may be examined with the ::dtrace_errhash MDB dcmd.
368  */
369 #ifdef DEBUG
370 static dtrace_errhash_t	dtrace_errhash[DTRACE_ERRHASHSZ];
371 static const char *dtrace_errlast;
372 static kthread_t *dtrace_errthread;
373 static kmutex_t dtrace_errlock;
374 #endif
375 
376 /*
377  * DTrace Macros and Constants
378  *
379  * These are various macros that are useful in various spots in the
380  * implementation, along with a few random constants that have no meaning
381  * outside of the implementation.  There is no real structure to this cpp
382  * mishmash -- but is there ever?
383  */
384 #define	DTRACE_HASHSTR(hash, probe)	\
385 	dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
386 
387 #define	DTRACE_HASHNEXT(hash, probe)	\
388 	(dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
389 
390 #define	DTRACE_HASHPREV(hash, probe)	\
391 	(dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
392 
393 #define	DTRACE_HASHEQ(hash, lhs, rhs)	\
394 	(strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
395 	    *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
396 
397 #define	DTRACE_AGGHASHSIZE_SLEW		17
398 
399 #define	DTRACE_V4MAPPED_OFFSET		(sizeof (uint32_t) * 3)
400 
401 /*
402  * The key for a thread-local variable consists of the lower 61 bits of the
403  * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
404  * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
405  * equal to a variable identifier.  This is necessary (but not sufficient) to
406  * assure that global associative arrays never collide with thread-local
407  * variables.  To guarantee that they cannot collide, we must also define the
408  * order for keying dynamic variables.  That order is:
409  *
410  *   [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
411  *
412  * Because the variable-key and the tls-key are in orthogonal spaces, there is
413  * no way for a global variable key signature to match a thread-local key
414  * signature.
415  */
416 #if defined(sun)
417 #define	DTRACE_TLS_THRKEY(where) { \
418 	uint_t intr = 0; \
419 	uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \
420 	for (; actv; actv >>= 1) \
421 		intr++; \
422 	ASSERT(intr < (1 << 3)); \
423 	(where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \
424 	    (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
425 }
426 #else
427 #define	DTRACE_TLS_THRKEY(where) { \
428 	solaris_cpu_t *_c = &solaris_cpu[curcpu]; \
429 	uint_t intr = 0; \
430 	uint_t actv = _c->cpu_intr_actv; \
431 	for (; actv; actv >>= 1) \
432 		intr++; \
433 	ASSERT(intr < (1 << 3)); \
434 	(where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \
435 	    (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
436 }
437 #endif
438 
439 #define	DT_BSWAP_8(x)	((x) & 0xff)
440 #define	DT_BSWAP_16(x)	((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
441 #define	DT_BSWAP_32(x)	((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
442 #define	DT_BSWAP_64(x)	((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
443 
444 #define	DT_MASK_LO 0x00000000FFFFFFFFULL
445 
446 #define	DTRACE_STORE(type, tomax, offset, what) \
447 	*((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
448 
449 #ifndef __x86
450 #define	DTRACE_ALIGNCHECK(addr, size, flags)				\
451 	if (addr & (size - 1)) {					\
452 		*flags |= CPU_DTRACE_BADALIGN;				\
453 		cpu_core[curcpu].cpuc_dtrace_illval = addr;	\
454 		return (0);						\
455 	}
456 #else
457 #define	DTRACE_ALIGNCHECK(addr, size, flags)
458 #endif
459 
460 /*
461  * Test whether a range of memory starting at testaddr of size testsz falls
462  * within the range of memory described by addr, sz.  We take care to avoid
463  * problems with overflow and underflow of the unsigned quantities, and
464  * disallow all negative sizes.  Ranges of size 0 are allowed.
465  */
466 #define	DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
467 	((testaddr) - (baseaddr) < (basesz) && \
468 	(testaddr) + (testsz) - (baseaddr) <= (basesz) && \
469 	(testaddr) + (testsz) >= (testaddr))
470 
471 /*
472  * Test whether alloc_sz bytes will fit in the scratch region.  We isolate
473  * alloc_sz on the righthand side of the comparison in order to avoid overflow
474  * or underflow in the comparison with it.  This is simpler than the INRANGE
475  * check above, because we know that the dtms_scratch_ptr is valid in the
476  * range.  Allocations of size zero are allowed.
477  */
478 #define	DTRACE_INSCRATCH(mstate, alloc_sz) \
479 	((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
480 	(mstate)->dtms_scratch_ptr >= (alloc_sz))
481 
482 #define	DTRACE_LOADFUNC(bits)						\
483 /*CSTYLED*/								\
484 uint##bits##_t								\
485 dtrace_load##bits(uintptr_t addr)					\
486 {									\
487 	size_t size = bits / NBBY;					\
488 	/*CSTYLED*/							\
489 	uint##bits##_t rval;						\
490 	int i;								\
491 	volatile uint16_t *flags = (volatile uint16_t *)		\
492 	    &cpu_core[curcpu].cpuc_dtrace_flags;			\
493 									\
494 	DTRACE_ALIGNCHECK(addr, size, flags);				\
495 									\
496 	for (i = 0; i < dtrace_toxranges; i++) {			\
497 		if (addr >= dtrace_toxrange[i].dtt_limit)		\
498 			continue;					\
499 									\
500 		if (addr + size <= dtrace_toxrange[i].dtt_base)		\
501 			continue;					\
502 									\
503 		/*							\
504 		 * This address falls within a toxic region; return 0.	\
505 		 */							\
506 		*flags |= CPU_DTRACE_BADADDR;				\
507 		cpu_core[curcpu].cpuc_dtrace_illval = addr;		\
508 		return (0);						\
509 	}								\
510 									\
511 	*flags |= CPU_DTRACE_NOFAULT;					\
512 	/*CSTYLED*/							\
513 	rval = *((volatile uint##bits##_t *)addr);			\
514 	*flags &= ~CPU_DTRACE_NOFAULT;					\
515 									\
516 	return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0);		\
517 }
518 
519 #ifdef _LP64
520 #define	dtrace_loadptr	dtrace_load64
521 #else
522 #define	dtrace_loadptr	dtrace_load32
523 #endif
524 
525 #define	DTRACE_DYNHASH_FREE	0
526 #define	DTRACE_DYNHASH_SINK	1
527 #define	DTRACE_DYNHASH_VALID	2
528 
529 #define	DTRACE_MATCH_NEXT	0
530 #define	DTRACE_MATCH_DONE	1
531 #define	DTRACE_ANCHORED(probe)	((probe)->dtpr_func[0] != '\0')
532 #define	DTRACE_STATE_ALIGN	64
533 
534 #define	DTRACE_FLAGS2FLT(flags)						\
535 	(((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR :		\
536 	((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP :		\
537 	((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO :		\
538 	((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV :		\
539 	((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV :		\
540 	((flags) & CPU_DTRACE_TUPOFLOW) ?  DTRACEFLT_TUPOFLOW :		\
541 	((flags) & CPU_DTRACE_BADALIGN) ?  DTRACEFLT_BADALIGN :		\
542 	((flags) & CPU_DTRACE_NOSCRATCH) ?  DTRACEFLT_NOSCRATCH :	\
543 	((flags) & CPU_DTRACE_BADSTACK) ?  DTRACEFLT_BADSTACK :		\
544 	DTRACEFLT_UNKNOWN)
545 
546 #define	DTRACEACT_ISSTRING(act)						\
547 	((act)->dta_kind == DTRACEACT_DIFEXPR &&			\
548 	(act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
549 
550 /* Function prototype definitions: */
551 static size_t dtrace_strlen(const char *, size_t);
552 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id);
553 static void dtrace_enabling_provide(dtrace_provider_t *);
554 static int dtrace_enabling_match(dtrace_enabling_t *, int *);
555 static void dtrace_enabling_matchall(void);
556 static void dtrace_enabling_reap(void);
557 static dtrace_state_t *dtrace_anon_grab(void);
558 static uint64_t dtrace_helper(int, dtrace_mstate_t *,
559     dtrace_state_t *, uint64_t, uint64_t);
560 static dtrace_helpers_t *dtrace_helpers_create(proc_t *);
561 static void dtrace_buffer_drop(dtrace_buffer_t *);
562 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when);
563 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t,
564     dtrace_state_t *, dtrace_mstate_t *);
565 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t,
566     dtrace_optval_t);
567 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *);
568 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *);
569 uint16_t dtrace_load16(uintptr_t);
570 uint32_t dtrace_load32(uintptr_t);
571 uint64_t dtrace_load64(uintptr_t);
572 uint8_t dtrace_load8(uintptr_t);
573 void dtrace_dynvar_clean(dtrace_dstate_t *);
574 dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *,
575     size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *);
576 uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *);
577 
578 /*
579  * DTrace Probe Context Functions
580  *
581  * These functions are called from probe context.  Because probe context is
582  * any context in which C may be called, arbitrarily locks may be held,
583  * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
584  * As a result, functions called from probe context may only call other DTrace
585  * support functions -- they may not interact at all with the system at large.
586  * (Note that the ASSERT macro is made probe-context safe by redefining it in
587  * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
588  * loads are to be performed from probe context, they _must_ be in terms of
589  * the safe dtrace_load*() variants.
590  *
591  * Some functions in this block are not actually called from probe context;
592  * for these functions, there will be a comment above the function reading
593  * "Note:  not called from probe context."
594  */
595 void
596 dtrace_panic(const char *format, ...)
597 {
598 	va_list alist;
599 
600 	va_start(alist, format);
601 	dtrace_vpanic(format, alist);
602 	va_end(alist);
603 }
604 
605 int
606 dtrace_assfail(const char *a, const char *f, int l)
607 {
608 	dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l);
609 
610 	/*
611 	 * We just need something here that even the most clever compiler
612 	 * cannot optimize away.
613 	 */
614 	return (a[(uintptr_t)f]);
615 }
616 
617 /*
618  * Atomically increment a specified error counter from probe context.
619  */
620 static void
621 dtrace_error(uint32_t *counter)
622 {
623 	/*
624 	 * Most counters stored to in probe context are per-CPU counters.
625 	 * However, there are some error conditions that are sufficiently
626 	 * arcane that they don't merit per-CPU storage.  If these counters
627 	 * are incremented concurrently on different CPUs, scalability will be
628 	 * adversely affected -- but we don't expect them to be white-hot in a
629 	 * correctly constructed enabling...
630 	 */
631 	uint32_t oval, nval;
632 
633 	do {
634 		oval = *counter;
635 
636 		if ((nval = oval + 1) == 0) {
637 			/*
638 			 * If the counter would wrap, set it to 1 -- assuring
639 			 * that the counter is never zero when we have seen
640 			 * errors.  (The counter must be 32-bits because we
641 			 * aren't guaranteed a 64-bit compare&swap operation.)
642 			 * To save this code both the infamy of being fingered
643 			 * by a priggish news story and the indignity of being
644 			 * the target of a neo-puritan witch trial, we're
645 			 * carefully avoiding any colorful description of the
646 			 * likelihood of this condition -- but suffice it to
647 			 * say that it is only slightly more likely than the
648 			 * overflow of predicate cache IDs, as discussed in
649 			 * dtrace_predicate_create().
650 			 */
651 			nval = 1;
652 		}
653 	} while (dtrace_cas32(counter, oval, nval) != oval);
654 }
655 
656 /*
657  * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
658  * uint8_t, a uint16_t, a uint32_t and a uint64_t.
659  */
660 DTRACE_LOADFUNC(8)
661 DTRACE_LOADFUNC(16)
662 DTRACE_LOADFUNC(32)
663 DTRACE_LOADFUNC(64)
664 
665 static int
666 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate)
667 {
668 	if (dest < mstate->dtms_scratch_base)
669 		return (0);
670 
671 	if (dest + size < dest)
672 		return (0);
673 
674 	if (dest + size > mstate->dtms_scratch_ptr)
675 		return (0);
676 
677 	return (1);
678 }
679 
680 static int
681 dtrace_canstore_statvar(uint64_t addr, size_t sz,
682     dtrace_statvar_t **svars, int nsvars)
683 {
684 	int i;
685 
686 	for (i = 0; i < nsvars; i++) {
687 		dtrace_statvar_t *svar = svars[i];
688 
689 		if (svar == NULL || svar->dtsv_size == 0)
690 			continue;
691 
692 		if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size))
693 			return (1);
694 	}
695 
696 	return (0);
697 }
698 
699 /*
700  * Check to see if the address is within a memory region to which a store may
701  * be issued.  This includes the DTrace scratch areas, and any DTrace variable
702  * region.  The caller of dtrace_canstore() is responsible for performing any
703  * alignment checks that are needed before stores are actually executed.
704  */
705 static int
706 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
707     dtrace_vstate_t *vstate)
708 {
709 	/*
710 	 * First, check to see if the address is in scratch space...
711 	 */
712 	if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base,
713 	    mstate->dtms_scratch_size))
714 		return (1);
715 
716 	/*
717 	 * Now check to see if it's a dynamic variable.  This check will pick
718 	 * up both thread-local variables and any global dynamically-allocated
719 	 * variables.
720 	 */
721 	if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base,
722 	    vstate->dtvs_dynvars.dtds_size)) {
723 		dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
724 		uintptr_t base = (uintptr_t)dstate->dtds_base +
725 		    (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t));
726 		uintptr_t chunkoffs;
727 
728 		/*
729 		 * Before we assume that we can store here, we need to make
730 		 * sure that it isn't in our metadata -- storing to our
731 		 * dynamic variable metadata would corrupt our state.  For
732 		 * the range to not include any dynamic variable metadata,
733 		 * it must:
734 		 *
735 		 *	(1) Start above the hash table that is at the base of
736 		 *	the dynamic variable space
737 		 *
738 		 *	(2) Have a starting chunk offset that is beyond the
739 		 *	dtrace_dynvar_t that is at the base of every chunk
740 		 *
741 		 *	(3) Not span a chunk boundary
742 		 *
743 		 */
744 		if (addr < base)
745 			return (0);
746 
747 		chunkoffs = (addr - base) % dstate->dtds_chunksize;
748 
749 		if (chunkoffs < sizeof (dtrace_dynvar_t))
750 			return (0);
751 
752 		if (chunkoffs + sz > dstate->dtds_chunksize)
753 			return (0);
754 
755 		return (1);
756 	}
757 
758 	/*
759 	 * Finally, check the static local and global variables.  These checks
760 	 * take the longest, so we perform them last.
761 	 */
762 	if (dtrace_canstore_statvar(addr, sz,
763 	    vstate->dtvs_locals, vstate->dtvs_nlocals))
764 		return (1);
765 
766 	if (dtrace_canstore_statvar(addr, sz,
767 	    vstate->dtvs_globals, vstate->dtvs_nglobals))
768 		return (1);
769 
770 	return (0);
771 }
772 
773 
774 /*
775  * Convenience routine to check to see if the address is within a memory
776  * region in which a load may be issued given the user's privilege level;
777  * if not, it sets the appropriate error flags and loads 'addr' into the
778  * illegal value slot.
779  *
780  * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
781  * appropriate memory access protection.
782  */
783 static int
784 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
785     dtrace_vstate_t *vstate)
786 {
787 	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
788 
789 	/*
790 	 * If we hold the privilege to read from kernel memory, then
791 	 * everything is readable.
792 	 */
793 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
794 		return (1);
795 
796 	/*
797 	 * You can obviously read that which you can store.
798 	 */
799 	if (dtrace_canstore(addr, sz, mstate, vstate))
800 		return (1);
801 
802 	/*
803 	 * We're allowed to read from our own string table.
804 	 */
805 	if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab,
806 	    mstate->dtms_difo->dtdo_strlen))
807 		return (1);
808 
809 	DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
810 	*illval = addr;
811 	return (0);
812 }
813 
814 /*
815  * Convenience routine to check to see if a given string is within a memory
816  * region in which a load may be issued given the user's privilege level;
817  * this exists so that we don't need to issue unnecessary dtrace_strlen()
818  * calls in the event that the user has all privileges.
819  */
820 static int
821 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
822     dtrace_vstate_t *vstate)
823 {
824 	size_t strsz;
825 
826 	/*
827 	 * If we hold the privilege to read from kernel memory, then
828 	 * everything is readable.
829 	 */
830 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
831 		return (1);
832 
833 	strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz);
834 	if (dtrace_canload(addr, strsz, mstate, vstate))
835 		return (1);
836 
837 	return (0);
838 }
839 
840 /*
841  * Convenience routine to check to see if a given variable is within a memory
842  * region in which a load may be issued given the user's privilege level.
843  */
844 static int
845 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate,
846     dtrace_vstate_t *vstate)
847 {
848 	size_t sz;
849 	ASSERT(type->dtdt_flags & DIF_TF_BYREF);
850 
851 	/*
852 	 * If we hold the privilege to read from kernel memory, then
853 	 * everything is readable.
854 	 */
855 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
856 		return (1);
857 
858 	if (type->dtdt_kind == DIF_TYPE_STRING)
859 		sz = dtrace_strlen(src,
860 		    vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1;
861 	else
862 		sz = type->dtdt_size;
863 
864 	return (dtrace_canload((uintptr_t)src, sz, mstate, vstate));
865 }
866 
867 /*
868  * Compare two strings using safe loads.
869  */
870 static int
871 dtrace_strncmp(char *s1, char *s2, size_t limit)
872 {
873 	uint8_t c1, c2;
874 	volatile uint16_t *flags;
875 
876 	if (s1 == s2 || limit == 0)
877 		return (0);
878 
879 	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
880 
881 	do {
882 		if (s1 == NULL) {
883 			c1 = '\0';
884 		} else {
885 			c1 = dtrace_load8((uintptr_t)s1++);
886 		}
887 
888 		if (s2 == NULL) {
889 			c2 = '\0';
890 		} else {
891 			c2 = dtrace_load8((uintptr_t)s2++);
892 		}
893 
894 		if (c1 != c2)
895 			return (c1 - c2);
896 	} while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT));
897 
898 	return (0);
899 }
900 
901 /*
902  * Compute strlen(s) for a string using safe memory accesses.  The additional
903  * len parameter is used to specify a maximum length to ensure completion.
904  */
905 static size_t
906 dtrace_strlen(const char *s, size_t lim)
907 {
908 	uint_t len;
909 
910 	for (len = 0; len != lim; len++) {
911 		if (dtrace_load8((uintptr_t)s++) == '\0')
912 			break;
913 	}
914 
915 	return (len);
916 }
917 
918 /*
919  * Check if an address falls within a toxic region.
920  */
921 static int
922 dtrace_istoxic(uintptr_t kaddr, size_t size)
923 {
924 	uintptr_t taddr, tsize;
925 	int i;
926 
927 	for (i = 0; i < dtrace_toxranges; i++) {
928 		taddr = dtrace_toxrange[i].dtt_base;
929 		tsize = dtrace_toxrange[i].dtt_limit - taddr;
930 
931 		if (kaddr - taddr < tsize) {
932 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
933 			cpu_core[curcpu].cpuc_dtrace_illval = kaddr;
934 			return (1);
935 		}
936 
937 		if (taddr - kaddr < size) {
938 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
939 			cpu_core[curcpu].cpuc_dtrace_illval = taddr;
940 			return (1);
941 		}
942 	}
943 
944 	return (0);
945 }
946 
947 /*
948  * Copy src to dst using safe memory accesses.  The src is assumed to be unsafe
949  * memory specified by the DIF program.  The dst is assumed to be safe memory
950  * that we can store to directly because it is managed by DTrace.  As with
951  * standard bcopy, overlapping copies are handled properly.
952  */
953 static void
954 dtrace_bcopy(const void *src, void *dst, size_t len)
955 {
956 	if (len != 0) {
957 		uint8_t *s1 = dst;
958 		const uint8_t *s2 = src;
959 
960 		if (s1 <= s2) {
961 			do {
962 				*s1++ = dtrace_load8((uintptr_t)s2++);
963 			} while (--len != 0);
964 		} else {
965 			s2 += len;
966 			s1 += len;
967 
968 			do {
969 				*--s1 = dtrace_load8((uintptr_t)--s2);
970 			} while (--len != 0);
971 		}
972 	}
973 }
974 
975 /*
976  * Copy src to dst using safe memory accesses, up to either the specified
977  * length, or the point that a nul byte is encountered.  The src is assumed to
978  * be unsafe memory specified by the DIF program.  The dst is assumed to be
979  * safe memory that we can store to directly because it is managed by DTrace.
980  * Unlike dtrace_bcopy(), overlapping regions are not handled.
981  */
982 static void
983 dtrace_strcpy(const void *src, void *dst, size_t len)
984 {
985 	if (len != 0) {
986 		uint8_t *s1 = dst, c;
987 		const uint8_t *s2 = src;
988 
989 		do {
990 			*s1++ = c = dtrace_load8((uintptr_t)s2++);
991 		} while (--len != 0 && c != '\0');
992 	}
993 }
994 
995 /*
996  * Copy src to dst, deriving the size and type from the specified (BYREF)
997  * variable type.  The src is assumed to be unsafe memory specified by the DIF
998  * program.  The dst is assumed to be DTrace variable memory that is of the
999  * specified type; we assume that we can store to directly.
1000  */
1001 static void
1002 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type)
1003 {
1004 	ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1005 
1006 	if (type->dtdt_kind == DIF_TYPE_STRING) {
1007 		dtrace_strcpy(src, dst, type->dtdt_size);
1008 	} else {
1009 		dtrace_bcopy(src, dst, type->dtdt_size);
1010 	}
1011 }
1012 
1013 /*
1014  * Compare s1 to s2 using safe memory accesses.  The s1 data is assumed to be
1015  * unsafe memory specified by the DIF program.  The s2 data is assumed to be
1016  * safe memory that we can access directly because it is managed by DTrace.
1017  */
1018 static int
1019 dtrace_bcmp(const void *s1, const void *s2, size_t len)
1020 {
1021 	volatile uint16_t *flags;
1022 
1023 	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
1024 
1025 	if (s1 == s2)
1026 		return (0);
1027 
1028 	if (s1 == NULL || s2 == NULL)
1029 		return (1);
1030 
1031 	if (s1 != s2 && len != 0) {
1032 		const uint8_t *ps1 = s1;
1033 		const uint8_t *ps2 = s2;
1034 
1035 		do {
1036 			if (dtrace_load8((uintptr_t)ps1++) != *ps2++)
1037 				return (1);
1038 		} while (--len != 0 && !(*flags & CPU_DTRACE_FAULT));
1039 	}
1040 	return (0);
1041 }
1042 
1043 /*
1044  * Zero the specified region using a simple byte-by-byte loop.  Note that this
1045  * is for safe DTrace-managed memory only.
1046  */
1047 static void
1048 dtrace_bzero(void *dst, size_t len)
1049 {
1050 	uchar_t *cp;
1051 
1052 	for (cp = dst; len != 0; len--)
1053 		*cp++ = 0;
1054 }
1055 
1056 static void
1057 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
1058 {
1059 	uint64_t result[2];
1060 
1061 	result[0] = addend1[0] + addend2[0];
1062 	result[1] = addend1[1] + addend2[1] +
1063 	    (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
1064 
1065 	sum[0] = result[0];
1066 	sum[1] = result[1];
1067 }
1068 
1069 /*
1070  * Shift the 128-bit value in a by b. If b is positive, shift left.
1071  * If b is negative, shift right.
1072  */
1073 static void
1074 dtrace_shift_128(uint64_t *a, int b)
1075 {
1076 	uint64_t mask;
1077 
1078 	if (b == 0)
1079 		return;
1080 
1081 	if (b < 0) {
1082 		b = -b;
1083 		if (b >= 64) {
1084 			a[0] = a[1] >> (b - 64);
1085 			a[1] = 0;
1086 		} else {
1087 			a[0] >>= b;
1088 			mask = 1LL << (64 - b);
1089 			mask -= 1;
1090 			a[0] |= ((a[1] & mask) << (64 - b));
1091 			a[1] >>= b;
1092 		}
1093 	} else {
1094 		if (b >= 64) {
1095 			a[1] = a[0] << (b - 64);
1096 			a[0] = 0;
1097 		} else {
1098 			a[1] <<= b;
1099 			mask = a[0] >> (64 - b);
1100 			a[1] |= mask;
1101 			a[0] <<= b;
1102 		}
1103 	}
1104 }
1105 
1106 /*
1107  * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1108  * use native multiplication on those, and then re-combine into the
1109  * resulting 128-bit value.
1110  *
1111  * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1112  *     hi1 * hi2 << 64 +
1113  *     hi1 * lo2 << 32 +
1114  *     hi2 * lo1 << 32 +
1115  *     lo1 * lo2
1116  */
1117 static void
1118 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
1119 {
1120 	uint64_t hi1, hi2, lo1, lo2;
1121 	uint64_t tmp[2];
1122 
1123 	hi1 = factor1 >> 32;
1124 	hi2 = factor2 >> 32;
1125 
1126 	lo1 = factor1 & DT_MASK_LO;
1127 	lo2 = factor2 & DT_MASK_LO;
1128 
1129 	product[0] = lo1 * lo2;
1130 	product[1] = hi1 * hi2;
1131 
1132 	tmp[0] = hi1 * lo2;
1133 	tmp[1] = 0;
1134 	dtrace_shift_128(tmp, 32);
1135 	dtrace_add_128(product, tmp, product);
1136 
1137 	tmp[0] = hi2 * lo1;
1138 	tmp[1] = 0;
1139 	dtrace_shift_128(tmp, 32);
1140 	dtrace_add_128(product, tmp, product);
1141 }
1142 
1143 /*
1144  * This privilege check should be used by actions and subroutines to
1145  * verify that the user credentials of the process that enabled the
1146  * invoking ECB match the target credentials
1147  */
1148 static int
1149 dtrace_priv_proc_common_user(dtrace_state_t *state)
1150 {
1151 	cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1152 
1153 	/*
1154 	 * We should always have a non-NULL state cred here, since if cred
1155 	 * is null (anonymous tracing), we fast-path bypass this routine.
1156 	 */
1157 	ASSERT(s_cr != NULL);
1158 
1159 	if ((cr = CRED()) != NULL &&
1160 	    s_cr->cr_uid == cr->cr_uid &&
1161 	    s_cr->cr_uid == cr->cr_ruid &&
1162 	    s_cr->cr_uid == cr->cr_suid &&
1163 	    s_cr->cr_gid == cr->cr_gid &&
1164 	    s_cr->cr_gid == cr->cr_rgid &&
1165 	    s_cr->cr_gid == cr->cr_sgid)
1166 		return (1);
1167 
1168 	return (0);
1169 }
1170 
1171 /*
1172  * This privilege check should be used by actions and subroutines to
1173  * verify that the zone of the process that enabled the invoking ECB
1174  * matches the target credentials
1175  */
1176 static int
1177 dtrace_priv_proc_common_zone(dtrace_state_t *state)
1178 {
1179 #if defined(sun)
1180 	cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1181 
1182 	/*
1183 	 * We should always have a non-NULL state cred here, since if cred
1184 	 * is null (anonymous tracing), we fast-path bypass this routine.
1185 	 */
1186 	ASSERT(s_cr != NULL);
1187 
1188 	if ((cr = CRED()) != NULL &&
1189 	    s_cr->cr_zone == cr->cr_zone)
1190 		return (1);
1191 
1192 	return (0);
1193 #else
1194 	return (1);
1195 #endif
1196 }
1197 
1198 /*
1199  * This privilege check should be used by actions and subroutines to
1200  * verify that the process has not setuid or changed credentials.
1201  */
1202 static int
1203 dtrace_priv_proc_common_nocd(void)
1204 {
1205 	proc_t *proc;
1206 
1207 	if ((proc = ttoproc(curthread)) != NULL &&
1208 	    !(proc->p_flag & SNOCD))
1209 		return (1);
1210 
1211 	return (0);
1212 }
1213 
1214 static int
1215 dtrace_priv_proc_destructive(dtrace_state_t *state)
1216 {
1217 	int action = state->dts_cred.dcr_action;
1218 
1219 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) &&
1220 	    dtrace_priv_proc_common_zone(state) == 0)
1221 		goto bad;
1222 
1223 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) &&
1224 	    dtrace_priv_proc_common_user(state) == 0)
1225 		goto bad;
1226 
1227 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) &&
1228 	    dtrace_priv_proc_common_nocd() == 0)
1229 		goto bad;
1230 
1231 	return (1);
1232 
1233 bad:
1234 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1235 
1236 	return (0);
1237 }
1238 
1239 static int
1240 dtrace_priv_proc_control(dtrace_state_t *state)
1241 {
1242 	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL)
1243 		return (1);
1244 
1245 	if (dtrace_priv_proc_common_zone(state) &&
1246 	    dtrace_priv_proc_common_user(state) &&
1247 	    dtrace_priv_proc_common_nocd())
1248 		return (1);
1249 
1250 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1251 
1252 	return (0);
1253 }
1254 
1255 static int
1256 dtrace_priv_proc(dtrace_state_t *state)
1257 {
1258 	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC)
1259 		return (1);
1260 
1261 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1262 
1263 	return (0);
1264 }
1265 
1266 static int
1267 dtrace_priv_kernel(dtrace_state_t *state)
1268 {
1269 	if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL)
1270 		return (1);
1271 
1272 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1273 
1274 	return (0);
1275 }
1276 
1277 static int
1278 dtrace_priv_kernel_destructive(dtrace_state_t *state)
1279 {
1280 	if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE)
1281 		return (1);
1282 
1283 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1284 
1285 	return (0);
1286 }
1287 
1288 /*
1289  * Note:  not called from probe context.  This function is called
1290  * asynchronously (and at a regular interval) from outside of probe context to
1291  * clean the dirty dynamic variable lists on all CPUs.  Dynamic variable
1292  * cleaning is explained in detail in <sys/dtrace_impl.h>.
1293  */
1294 void
1295 dtrace_dynvar_clean(dtrace_dstate_t *dstate)
1296 {
1297 	dtrace_dynvar_t *dirty;
1298 	dtrace_dstate_percpu_t *dcpu;
1299 	int i, work = 0;
1300 
1301 	for (i = 0; i < NCPU; i++) {
1302 		dcpu = &dstate->dtds_percpu[i];
1303 
1304 		ASSERT(dcpu->dtdsc_rinsing == NULL);
1305 
1306 		/*
1307 		 * If the dirty list is NULL, there is no dirty work to do.
1308 		 */
1309 		if (dcpu->dtdsc_dirty == NULL)
1310 			continue;
1311 
1312 		/*
1313 		 * If the clean list is non-NULL, then we're not going to do
1314 		 * any work for this CPU -- it means that there has not been
1315 		 * a dtrace_dynvar() allocation on this CPU (or from this CPU)
1316 		 * since the last time we cleaned house.
1317 		 */
1318 		if (dcpu->dtdsc_clean != NULL)
1319 			continue;
1320 
1321 		work = 1;
1322 
1323 		/*
1324 		 * Atomically move the dirty list aside.
1325 		 */
1326 		do {
1327 			dirty = dcpu->dtdsc_dirty;
1328 
1329 			/*
1330 			 * Before we zap the dirty list, set the rinsing list.
1331 			 * (This allows for a potential assertion in
1332 			 * dtrace_dynvar():  if a free dynamic variable appears
1333 			 * on a hash chain, either the dirty list or the
1334 			 * rinsing list for some CPU must be non-NULL.)
1335 			 */
1336 			dcpu->dtdsc_rinsing = dirty;
1337 			dtrace_membar_producer();
1338 		} while (dtrace_casptr(&dcpu->dtdsc_dirty,
1339 		    dirty, NULL) != dirty);
1340 	}
1341 
1342 	if (!work) {
1343 		/*
1344 		 * We have no work to do; we can simply return.
1345 		 */
1346 		return;
1347 	}
1348 
1349 	dtrace_sync();
1350 
1351 	for (i = 0; i < NCPU; i++) {
1352 		dcpu = &dstate->dtds_percpu[i];
1353 
1354 		if (dcpu->dtdsc_rinsing == NULL)
1355 			continue;
1356 
1357 		/*
1358 		 * We are now guaranteed that no hash chain contains a pointer
1359 		 * into this dirty list; we can make it clean.
1360 		 */
1361 		ASSERT(dcpu->dtdsc_clean == NULL);
1362 		dcpu->dtdsc_clean = dcpu->dtdsc_rinsing;
1363 		dcpu->dtdsc_rinsing = NULL;
1364 	}
1365 
1366 	/*
1367 	 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1368 	 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1369 	 * This prevents a race whereby a CPU incorrectly decides that
1370 	 * the state should be something other than DTRACE_DSTATE_CLEAN
1371 	 * after dtrace_dynvar_clean() has completed.
1372 	 */
1373 	dtrace_sync();
1374 
1375 	dstate->dtds_state = DTRACE_DSTATE_CLEAN;
1376 }
1377 
1378 /*
1379  * Depending on the value of the op parameter, this function looks-up,
1380  * allocates or deallocates an arbitrarily-keyed dynamic variable.  If an
1381  * allocation is requested, this function will return a pointer to a
1382  * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1383  * variable can be allocated.  If NULL is returned, the appropriate counter
1384  * will be incremented.
1385  */
1386 dtrace_dynvar_t *
1387 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys,
1388     dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op,
1389     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1390 {
1391 	uint64_t hashval = DTRACE_DYNHASH_VALID;
1392 	dtrace_dynhash_t *hash = dstate->dtds_hash;
1393 	dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL;
1394 	processorid_t me = curcpu, cpu = me;
1395 	dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me];
1396 	size_t bucket, ksize;
1397 	size_t chunksize = dstate->dtds_chunksize;
1398 	uintptr_t kdata, lock, nstate;
1399 	uint_t i;
1400 
1401 	ASSERT(nkeys != 0);
1402 
1403 	/*
1404 	 * Hash the key.  As with aggregations, we use Jenkins' "One-at-a-time"
1405 	 * algorithm.  For the by-value portions, we perform the algorithm in
1406 	 * 16-bit chunks (as opposed to 8-bit chunks).  This speeds things up a
1407 	 * bit, and seems to have only a minute effect on distribution.  For
1408 	 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1409 	 * over each referenced byte.  It's painful to do this, but it's much
1410 	 * better than pathological hash distribution.  The efficacy of the
1411 	 * hashing algorithm (and a comparison with other algorithms) may be
1412 	 * found by running the ::dtrace_dynstat MDB dcmd.
1413 	 */
1414 	for (i = 0; i < nkeys; i++) {
1415 		if (key[i].dttk_size == 0) {
1416 			uint64_t val = key[i].dttk_value;
1417 
1418 			hashval += (val >> 48) & 0xffff;
1419 			hashval += (hashval << 10);
1420 			hashval ^= (hashval >> 6);
1421 
1422 			hashval += (val >> 32) & 0xffff;
1423 			hashval += (hashval << 10);
1424 			hashval ^= (hashval >> 6);
1425 
1426 			hashval += (val >> 16) & 0xffff;
1427 			hashval += (hashval << 10);
1428 			hashval ^= (hashval >> 6);
1429 
1430 			hashval += val & 0xffff;
1431 			hashval += (hashval << 10);
1432 			hashval ^= (hashval >> 6);
1433 		} else {
1434 			/*
1435 			 * This is incredibly painful, but it beats the hell
1436 			 * out of the alternative.
1437 			 */
1438 			uint64_t j, size = key[i].dttk_size;
1439 			uintptr_t base = (uintptr_t)key[i].dttk_value;
1440 
1441 			if (!dtrace_canload(base, size, mstate, vstate))
1442 				break;
1443 
1444 			for (j = 0; j < size; j++) {
1445 				hashval += dtrace_load8(base + j);
1446 				hashval += (hashval << 10);
1447 				hashval ^= (hashval >> 6);
1448 			}
1449 		}
1450 	}
1451 
1452 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
1453 		return (NULL);
1454 
1455 	hashval += (hashval << 3);
1456 	hashval ^= (hashval >> 11);
1457 	hashval += (hashval << 15);
1458 
1459 	/*
1460 	 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1461 	 * comes out to be one of our two sentinel hash values.  If this
1462 	 * actually happens, we set the hashval to be a value known to be a
1463 	 * non-sentinel value.
1464 	 */
1465 	if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK)
1466 		hashval = DTRACE_DYNHASH_VALID;
1467 
1468 	/*
1469 	 * Yes, it's painful to do a divide here.  If the cycle count becomes
1470 	 * important here, tricks can be pulled to reduce it.  (However, it's
1471 	 * critical that hash collisions be kept to an absolute minimum;
1472 	 * they're much more painful than a divide.)  It's better to have a
1473 	 * solution that generates few collisions and still keeps things
1474 	 * relatively simple.
1475 	 */
1476 	bucket = hashval % dstate->dtds_hashsize;
1477 
1478 	if (op == DTRACE_DYNVAR_DEALLOC) {
1479 		volatile uintptr_t *lockp = &hash[bucket].dtdh_lock;
1480 
1481 		for (;;) {
1482 			while ((lock = *lockp) & 1)
1483 				continue;
1484 
1485 			if (dtrace_casptr((volatile void *)lockp,
1486 			    (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock)
1487 				break;
1488 		}
1489 
1490 		dtrace_membar_producer();
1491 	}
1492 
1493 top:
1494 	prev = NULL;
1495 	lock = hash[bucket].dtdh_lock;
1496 
1497 	dtrace_membar_consumer();
1498 
1499 	start = hash[bucket].dtdh_chain;
1500 	ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK ||
1501 	    start->dtdv_hashval != DTRACE_DYNHASH_FREE ||
1502 	    op != DTRACE_DYNVAR_DEALLOC));
1503 
1504 	for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) {
1505 		dtrace_tuple_t *dtuple = &dvar->dtdv_tuple;
1506 		dtrace_key_t *dkey = &dtuple->dtt_key[0];
1507 
1508 		if (dvar->dtdv_hashval != hashval) {
1509 			if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) {
1510 				/*
1511 				 * We've reached the sink, and therefore the
1512 				 * end of the hash chain; we can kick out of
1513 				 * the loop knowing that we have seen a valid
1514 				 * snapshot of state.
1515 				 */
1516 				ASSERT(dvar->dtdv_next == NULL);
1517 				ASSERT(dvar == &dtrace_dynhash_sink);
1518 				break;
1519 			}
1520 
1521 			if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) {
1522 				/*
1523 				 * We've gone off the rails:  somewhere along
1524 				 * the line, one of the members of this hash
1525 				 * chain was deleted.  Note that we could also
1526 				 * detect this by simply letting this loop run
1527 				 * to completion, as we would eventually hit
1528 				 * the end of the dirty list.  However, we
1529 				 * want to avoid running the length of the
1530 				 * dirty list unnecessarily (it might be quite
1531 				 * long), so we catch this as early as
1532 				 * possible by detecting the hash marker.  In
1533 				 * this case, we simply set dvar to NULL and
1534 				 * break; the conditional after the loop will
1535 				 * send us back to top.
1536 				 */
1537 				dvar = NULL;
1538 				break;
1539 			}
1540 
1541 			goto next;
1542 		}
1543 
1544 		if (dtuple->dtt_nkeys != nkeys)
1545 			goto next;
1546 
1547 		for (i = 0; i < nkeys; i++, dkey++) {
1548 			if (dkey->dttk_size != key[i].dttk_size)
1549 				goto next; /* size or type mismatch */
1550 
1551 			if (dkey->dttk_size != 0) {
1552 				if (dtrace_bcmp(
1553 				    (void *)(uintptr_t)key[i].dttk_value,
1554 				    (void *)(uintptr_t)dkey->dttk_value,
1555 				    dkey->dttk_size))
1556 					goto next;
1557 			} else {
1558 				if (dkey->dttk_value != key[i].dttk_value)
1559 					goto next;
1560 			}
1561 		}
1562 
1563 		if (op != DTRACE_DYNVAR_DEALLOC)
1564 			return (dvar);
1565 
1566 		ASSERT(dvar->dtdv_next == NULL ||
1567 		    dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE);
1568 
1569 		if (prev != NULL) {
1570 			ASSERT(hash[bucket].dtdh_chain != dvar);
1571 			ASSERT(start != dvar);
1572 			ASSERT(prev->dtdv_next == dvar);
1573 			prev->dtdv_next = dvar->dtdv_next;
1574 		} else {
1575 			if (dtrace_casptr(&hash[bucket].dtdh_chain,
1576 			    start, dvar->dtdv_next) != start) {
1577 				/*
1578 				 * We have failed to atomically swing the
1579 				 * hash table head pointer, presumably because
1580 				 * of a conflicting allocation on another CPU.
1581 				 * We need to reread the hash chain and try
1582 				 * again.
1583 				 */
1584 				goto top;
1585 			}
1586 		}
1587 
1588 		dtrace_membar_producer();
1589 
1590 		/*
1591 		 * Now set the hash value to indicate that it's free.
1592 		 */
1593 		ASSERT(hash[bucket].dtdh_chain != dvar);
1594 		dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
1595 
1596 		dtrace_membar_producer();
1597 
1598 		/*
1599 		 * Set the next pointer to point at the dirty list, and
1600 		 * atomically swing the dirty pointer to the newly freed dvar.
1601 		 */
1602 		do {
1603 			next = dcpu->dtdsc_dirty;
1604 			dvar->dtdv_next = next;
1605 		} while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next);
1606 
1607 		/*
1608 		 * Finally, unlock this hash bucket.
1609 		 */
1610 		ASSERT(hash[bucket].dtdh_lock == lock);
1611 		ASSERT(lock & 1);
1612 		hash[bucket].dtdh_lock++;
1613 
1614 		return (NULL);
1615 next:
1616 		prev = dvar;
1617 		continue;
1618 	}
1619 
1620 	if (dvar == NULL) {
1621 		/*
1622 		 * If dvar is NULL, it is because we went off the rails:
1623 		 * one of the elements that we traversed in the hash chain
1624 		 * was deleted while we were traversing it.  In this case,
1625 		 * we assert that we aren't doing a dealloc (deallocs lock
1626 		 * the hash bucket to prevent themselves from racing with
1627 		 * one another), and retry the hash chain traversal.
1628 		 */
1629 		ASSERT(op != DTRACE_DYNVAR_DEALLOC);
1630 		goto top;
1631 	}
1632 
1633 	if (op != DTRACE_DYNVAR_ALLOC) {
1634 		/*
1635 		 * If we are not to allocate a new variable, we want to
1636 		 * return NULL now.  Before we return, check that the value
1637 		 * of the lock word hasn't changed.  If it has, we may have
1638 		 * seen an inconsistent snapshot.
1639 		 */
1640 		if (op == DTRACE_DYNVAR_NOALLOC) {
1641 			if (hash[bucket].dtdh_lock != lock)
1642 				goto top;
1643 		} else {
1644 			ASSERT(op == DTRACE_DYNVAR_DEALLOC);
1645 			ASSERT(hash[bucket].dtdh_lock == lock);
1646 			ASSERT(lock & 1);
1647 			hash[bucket].dtdh_lock++;
1648 		}
1649 
1650 		return (NULL);
1651 	}
1652 
1653 	/*
1654 	 * We need to allocate a new dynamic variable.  The size we need is the
1655 	 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
1656 	 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
1657 	 * the size of any referred-to data (dsize).  We then round the final
1658 	 * size up to the chunksize for allocation.
1659 	 */
1660 	for (ksize = 0, i = 0; i < nkeys; i++)
1661 		ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
1662 
1663 	/*
1664 	 * This should be pretty much impossible, but could happen if, say,
1665 	 * strange DIF specified the tuple.  Ideally, this should be an
1666 	 * assertion and not an error condition -- but that requires that the
1667 	 * chunksize calculation in dtrace_difo_chunksize() be absolutely
1668 	 * bullet-proof.  (That is, it must not be able to be fooled by
1669 	 * malicious DIF.)  Given the lack of backwards branches in DIF,
1670 	 * solving this would presumably not amount to solving the Halting
1671 	 * Problem -- but it still seems awfully hard.
1672 	 */
1673 	if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) +
1674 	    ksize + dsize > chunksize) {
1675 		dcpu->dtdsc_drops++;
1676 		return (NULL);
1677 	}
1678 
1679 	nstate = DTRACE_DSTATE_EMPTY;
1680 
1681 	do {
1682 retry:
1683 		free = dcpu->dtdsc_free;
1684 
1685 		if (free == NULL) {
1686 			dtrace_dynvar_t *clean = dcpu->dtdsc_clean;
1687 			void *rval;
1688 
1689 			if (clean == NULL) {
1690 				/*
1691 				 * We're out of dynamic variable space on
1692 				 * this CPU.  Unless we have tried all CPUs,
1693 				 * we'll try to allocate from a different
1694 				 * CPU.
1695 				 */
1696 				switch (dstate->dtds_state) {
1697 				case DTRACE_DSTATE_CLEAN: {
1698 					void *sp = &dstate->dtds_state;
1699 
1700 					if (++cpu >= NCPU)
1701 						cpu = 0;
1702 
1703 					if (dcpu->dtdsc_dirty != NULL &&
1704 					    nstate == DTRACE_DSTATE_EMPTY)
1705 						nstate = DTRACE_DSTATE_DIRTY;
1706 
1707 					if (dcpu->dtdsc_rinsing != NULL)
1708 						nstate = DTRACE_DSTATE_RINSING;
1709 
1710 					dcpu = &dstate->dtds_percpu[cpu];
1711 
1712 					if (cpu != me)
1713 						goto retry;
1714 
1715 					(void) dtrace_cas32(sp,
1716 					    DTRACE_DSTATE_CLEAN, nstate);
1717 
1718 					/*
1719 					 * To increment the correct bean
1720 					 * counter, take another lap.
1721 					 */
1722 					goto retry;
1723 				}
1724 
1725 				case DTRACE_DSTATE_DIRTY:
1726 					dcpu->dtdsc_dirty_drops++;
1727 					break;
1728 
1729 				case DTRACE_DSTATE_RINSING:
1730 					dcpu->dtdsc_rinsing_drops++;
1731 					break;
1732 
1733 				case DTRACE_DSTATE_EMPTY:
1734 					dcpu->dtdsc_drops++;
1735 					break;
1736 				}
1737 
1738 				DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP);
1739 				return (NULL);
1740 			}
1741 
1742 			/*
1743 			 * The clean list appears to be non-empty.  We want to
1744 			 * move the clean list to the free list; we start by
1745 			 * moving the clean pointer aside.
1746 			 */
1747 			if (dtrace_casptr(&dcpu->dtdsc_clean,
1748 			    clean, NULL) != clean) {
1749 				/*
1750 				 * We are in one of two situations:
1751 				 *
1752 				 *  (a)	The clean list was switched to the
1753 				 *	free list by another CPU.
1754 				 *
1755 				 *  (b)	The clean list was added to by the
1756 				 *	cleansing cyclic.
1757 				 *
1758 				 * In either of these situations, we can
1759 				 * just reattempt the free list allocation.
1760 				 */
1761 				goto retry;
1762 			}
1763 
1764 			ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE);
1765 
1766 			/*
1767 			 * Now we'll move the clean list to the free list.
1768 			 * It's impossible for this to fail:  the only way
1769 			 * the free list can be updated is through this
1770 			 * code path, and only one CPU can own the clean list.
1771 			 * Thus, it would only be possible for this to fail if
1772 			 * this code were racing with dtrace_dynvar_clean().
1773 			 * (That is, if dtrace_dynvar_clean() updated the clean
1774 			 * list, and we ended up racing to update the free
1775 			 * list.)  This race is prevented by the dtrace_sync()
1776 			 * in dtrace_dynvar_clean() -- which flushes the
1777 			 * owners of the clean lists out before resetting
1778 			 * the clean lists.
1779 			 */
1780 			rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean);
1781 			ASSERT(rval == NULL);
1782 			goto retry;
1783 		}
1784 
1785 		dvar = free;
1786 		new_free = dvar->dtdv_next;
1787 	} while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free);
1788 
1789 	/*
1790 	 * We have now allocated a new chunk.  We copy the tuple keys into the
1791 	 * tuple array and copy any referenced key data into the data space
1792 	 * following the tuple array.  As we do this, we relocate dttk_value
1793 	 * in the final tuple to point to the key data address in the chunk.
1794 	 */
1795 	kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys];
1796 	dvar->dtdv_data = (void *)(kdata + ksize);
1797 	dvar->dtdv_tuple.dtt_nkeys = nkeys;
1798 
1799 	for (i = 0; i < nkeys; i++) {
1800 		dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i];
1801 		size_t kesize = key[i].dttk_size;
1802 
1803 		if (kesize != 0) {
1804 			dtrace_bcopy(
1805 			    (const void *)(uintptr_t)key[i].dttk_value,
1806 			    (void *)kdata, kesize);
1807 			dkey->dttk_value = kdata;
1808 			kdata += P2ROUNDUP(kesize, sizeof (uint64_t));
1809 		} else {
1810 			dkey->dttk_value = key[i].dttk_value;
1811 		}
1812 
1813 		dkey->dttk_size = kesize;
1814 	}
1815 
1816 	ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE);
1817 	dvar->dtdv_hashval = hashval;
1818 	dvar->dtdv_next = start;
1819 
1820 	if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start)
1821 		return (dvar);
1822 
1823 	/*
1824 	 * The cas has failed.  Either another CPU is adding an element to
1825 	 * this hash chain, or another CPU is deleting an element from this
1826 	 * hash chain.  The simplest way to deal with both of these cases
1827 	 * (though not necessarily the most efficient) is to free our
1828 	 * allocated block and tail-call ourselves.  Note that the free is
1829 	 * to the dirty list and _not_ to the free list.  This is to prevent
1830 	 * races with allocators, above.
1831 	 */
1832 	dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
1833 
1834 	dtrace_membar_producer();
1835 
1836 	do {
1837 		free = dcpu->dtdsc_dirty;
1838 		dvar->dtdv_next = free;
1839 	} while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free);
1840 
1841 	return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate));
1842 }
1843 
1844 /*ARGSUSED*/
1845 static void
1846 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg)
1847 {
1848 	if ((int64_t)nval < (int64_t)*oval)
1849 		*oval = nval;
1850 }
1851 
1852 /*ARGSUSED*/
1853 static void
1854 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg)
1855 {
1856 	if ((int64_t)nval > (int64_t)*oval)
1857 		*oval = nval;
1858 }
1859 
1860 static void
1861 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr)
1862 {
1863 	int i, zero = DTRACE_QUANTIZE_ZEROBUCKET;
1864 	int64_t val = (int64_t)nval;
1865 
1866 	if (val < 0) {
1867 		for (i = 0; i < zero; i++) {
1868 			if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) {
1869 				quanta[i] += incr;
1870 				return;
1871 			}
1872 		}
1873 	} else {
1874 		for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) {
1875 			if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) {
1876 				quanta[i - 1] += incr;
1877 				return;
1878 			}
1879 		}
1880 
1881 		quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr;
1882 		return;
1883 	}
1884 
1885 	ASSERT(0);
1886 }
1887 
1888 static void
1889 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr)
1890 {
1891 	uint64_t arg = *lquanta++;
1892 	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
1893 	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
1894 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
1895 	int32_t val = (int32_t)nval, level;
1896 
1897 	ASSERT(step != 0);
1898 	ASSERT(levels != 0);
1899 
1900 	if (val < base) {
1901 		/*
1902 		 * This is an underflow.
1903 		 */
1904 		lquanta[0] += incr;
1905 		return;
1906 	}
1907 
1908 	level = (val - base) / step;
1909 
1910 	if (level < levels) {
1911 		lquanta[level + 1] += incr;
1912 		return;
1913 	}
1914 
1915 	/*
1916 	 * This is an overflow.
1917 	 */
1918 	lquanta[levels + 1] += incr;
1919 }
1920 
1921 static int
1922 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low,
1923     uint16_t high, uint16_t nsteps, int64_t value)
1924 {
1925 	int64_t this = 1, last, next;
1926 	int base = 1, order;
1927 
1928 	ASSERT(factor <= nsteps);
1929 	ASSERT(nsteps % factor == 0);
1930 
1931 	for (order = 0; order < low; order++)
1932 		this *= factor;
1933 
1934 	/*
1935 	 * If our value is less than our factor taken to the power of the
1936 	 * low order of magnitude, it goes into the zeroth bucket.
1937 	 */
1938 	if (value < (last = this))
1939 		return (0);
1940 
1941 	for (this *= factor; order <= high; order++) {
1942 		int nbuckets = this > nsteps ? nsteps : this;
1943 
1944 		if ((next = this * factor) < this) {
1945 			/*
1946 			 * We should not generally get log/linear quantizations
1947 			 * with a high magnitude that allows 64-bits to
1948 			 * overflow, but we nonetheless protect against this
1949 			 * by explicitly checking for overflow, and clamping
1950 			 * our value accordingly.
1951 			 */
1952 			value = this - 1;
1953 		}
1954 
1955 		if (value < this) {
1956 			/*
1957 			 * If our value lies within this order of magnitude,
1958 			 * determine its position by taking the offset within
1959 			 * the order of magnitude, dividing by the bucket
1960 			 * width, and adding to our (accumulated) base.
1961 			 */
1962 			return (base + (value - last) / (this / nbuckets));
1963 		}
1964 
1965 		base += nbuckets - (nbuckets / factor);
1966 		last = this;
1967 		this = next;
1968 	}
1969 
1970 	/*
1971 	 * Our value is greater than or equal to our factor taken to the
1972 	 * power of one plus the high magnitude -- return the top bucket.
1973 	 */
1974 	return (base);
1975 }
1976 
1977 static void
1978 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr)
1979 {
1980 	uint64_t arg = *llquanta++;
1981 	uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
1982 	uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
1983 	uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
1984 	uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
1985 
1986 	llquanta[dtrace_aggregate_llquantize_bucket(factor,
1987 	    low, high, nsteps, nval)] += incr;
1988 }
1989 
1990 /*ARGSUSED*/
1991 static void
1992 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg)
1993 {
1994 	data[0]++;
1995 	data[1] += nval;
1996 }
1997 
1998 /*ARGSUSED*/
1999 static void
2000 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg)
2001 {
2002 	int64_t snval = (int64_t)nval;
2003 	uint64_t tmp[2];
2004 
2005 	data[0]++;
2006 	data[1] += nval;
2007 
2008 	/*
2009 	 * What we want to say here is:
2010 	 *
2011 	 * data[2] += nval * nval;
2012 	 *
2013 	 * But given that nval is 64-bit, we could easily overflow, so
2014 	 * we do this as 128-bit arithmetic.
2015 	 */
2016 	if (snval < 0)
2017 		snval = -snval;
2018 
2019 	dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp);
2020 	dtrace_add_128(data + 2, tmp, data + 2);
2021 }
2022 
2023 /*ARGSUSED*/
2024 static void
2025 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg)
2026 {
2027 	*oval = *oval + 1;
2028 }
2029 
2030 /*ARGSUSED*/
2031 static void
2032 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg)
2033 {
2034 	*oval += nval;
2035 }
2036 
2037 /*
2038  * Aggregate given the tuple in the principal data buffer, and the aggregating
2039  * action denoted by the specified dtrace_aggregation_t.  The aggregation
2040  * buffer is specified as the buf parameter.  This routine does not return
2041  * failure; if there is no space in the aggregation buffer, the data will be
2042  * dropped, and a corresponding counter incremented.
2043  */
2044 static void
2045 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf,
2046     intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg)
2047 {
2048 	dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec;
2049 	uint32_t i, ndx, size, fsize;
2050 	uint32_t align = sizeof (uint64_t) - 1;
2051 	dtrace_aggbuffer_t *agb;
2052 	dtrace_aggkey_t *key;
2053 	uint32_t hashval = 0, limit, isstr;
2054 	caddr_t tomax, data, kdata;
2055 	dtrace_actkind_t action;
2056 	dtrace_action_t *act;
2057 	uintptr_t offs;
2058 
2059 	if (buf == NULL)
2060 		return;
2061 
2062 	if (!agg->dtag_hasarg) {
2063 		/*
2064 		 * Currently, only quantize() and lquantize() take additional
2065 		 * arguments, and they have the same semantics:  an increment
2066 		 * value that defaults to 1 when not present.  If additional
2067 		 * aggregating actions take arguments, the setting of the
2068 		 * default argument value will presumably have to become more
2069 		 * sophisticated...
2070 		 */
2071 		arg = 1;
2072 	}
2073 
2074 	action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION;
2075 	size = rec->dtrd_offset - agg->dtag_base;
2076 	fsize = size + rec->dtrd_size;
2077 
2078 	ASSERT(dbuf->dtb_tomax != NULL);
2079 	data = dbuf->dtb_tomax + offset + agg->dtag_base;
2080 
2081 	if ((tomax = buf->dtb_tomax) == NULL) {
2082 		dtrace_buffer_drop(buf);
2083 		return;
2084 	}
2085 
2086 	/*
2087 	 * The metastructure is always at the bottom of the buffer.
2088 	 */
2089 	agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size -
2090 	    sizeof (dtrace_aggbuffer_t));
2091 
2092 	if (buf->dtb_offset == 0) {
2093 		/*
2094 		 * We just kludge up approximately 1/8th of the size to be
2095 		 * buckets.  If this guess ends up being routinely
2096 		 * off-the-mark, we may need to dynamically readjust this
2097 		 * based on past performance.
2098 		 */
2099 		uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t);
2100 
2101 		if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) <
2102 		    (uintptr_t)tomax || hashsize == 0) {
2103 			/*
2104 			 * We've been given a ludicrously small buffer;
2105 			 * increment our drop count and leave.
2106 			 */
2107 			dtrace_buffer_drop(buf);
2108 			return;
2109 		}
2110 
2111 		/*
2112 		 * And now, a pathetic attempt to try to get a an odd (or
2113 		 * perchance, a prime) hash size for better hash distribution.
2114 		 */
2115 		if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3))
2116 			hashsize -= DTRACE_AGGHASHSIZE_SLEW;
2117 
2118 		agb->dtagb_hashsize = hashsize;
2119 		agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb -
2120 		    agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *));
2121 		agb->dtagb_free = (uintptr_t)agb->dtagb_hash;
2122 
2123 		for (i = 0; i < agb->dtagb_hashsize; i++)
2124 			agb->dtagb_hash[i] = NULL;
2125 	}
2126 
2127 	ASSERT(agg->dtag_first != NULL);
2128 	ASSERT(agg->dtag_first->dta_intuple);
2129 
2130 	/*
2131 	 * Calculate the hash value based on the key.  Note that we _don't_
2132 	 * include the aggid in the hashing (but we will store it as part of
2133 	 * the key).  The hashing algorithm is Bob Jenkins' "One-at-a-time"
2134 	 * algorithm: a simple, quick algorithm that has no known funnels, and
2135 	 * gets good distribution in practice.  The efficacy of the hashing
2136 	 * algorithm (and a comparison with other algorithms) may be found by
2137 	 * running the ::dtrace_aggstat MDB dcmd.
2138 	 */
2139 	for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2140 		i = act->dta_rec.dtrd_offset - agg->dtag_base;
2141 		limit = i + act->dta_rec.dtrd_size;
2142 		ASSERT(limit <= size);
2143 		isstr = DTRACEACT_ISSTRING(act);
2144 
2145 		for (; i < limit; i++) {
2146 			hashval += data[i];
2147 			hashval += (hashval << 10);
2148 			hashval ^= (hashval >> 6);
2149 
2150 			if (isstr && data[i] == '\0')
2151 				break;
2152 		}
2153 	}
2154 
2155 	hashval += (hashval << 3);
2156 	hashval ^= (hashval >> 11);
2157 	hashval += (hashval << 15);
2158 
2159 	/*
2160 	 * Yes, the divide here is expensive -- but it's generally the least
2161 	 * of the performance issues given the amount of data that we iterate
2162 	 * over to compute hash values, compare data, etc.
2163 	 */
2164 	ndx = hashval % agb->dtagb_hashsize;
2165 
2166 	for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) {
2167 		ASSERT((caddr_t)key >= tomax);
2168 		ASSERT((caddr_t)key < tomax + buf->dtb_size);
2169 
2170 		if (hashval != key->dtak_hashval || key->dtak_size != size)
2171 			continue;
2172 
2173 		kdata = key->dtak_data;
2174 		ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size);
2175 
2176 		for (act = agg->dtag_first; act->dta_intuple;
2177 		    act = act->dta_next) {
2178 			i = act->dta_rec.dtrd_offset - agg->dtag_base;
2179 			limit = i + act->dta_rec.dtrd_size;
2180 			ASSERT(limit <= size);
2181 			isstr = DTRACEACT_ISSTRING(act);
2182 
2183 			for (; i < limit; i++) {
2184 				if (kdata[i] != data[i])
2185 					goto next;
2186 
2187 				if (isstr && data[i] == '\0')
2188 					break;
2189 			}
2190 		}
2191 
2192 		if (action != key->dtak_action) {
2193 			/*
2194 			 * We are aggregating on the same value in the same
2195 			 * aggregation with two different aggregating actions.
2196 			 * (This should have been picked up in the compiler,
2197 			 * so we may be dealing with errant or devious DIF.)
2198 			 * This is an error condition; we indicate as much,
2199 			 * and return.
2200 			 */
2201 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
2202 			return;
2203 		}
2204 
2205 		/*
2206 		 * This is a hit:  we need to apply the aggregator to
2207 		 * the value at this key.
2208 		 */
2209 		agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg);
2210 		return;
2211 next:
2212 		continue;
2213 	}
2214 
2215 	/*
2216 	 * We didn't find it.  We need to allocate some zero-filled space,
2217 	 * link it into the hash table appropriately, and apply the aggregator
2218 	 * to the (zero-filled) value.
2219 	 */
2220 	offs = buf->dtb_offset;
2221 	while (offs & (align - 1))
2222 		offs += sizeof (uint32_t);
2223 
2224 	/*
2225 	 * If we don't have enough room to both allocate a new key _and_
2226 	 * its associated data, increment the drop count and return.
2227 	 */
2228 	if ((uintptr_t)tomax + offs + fsize >
2229 	    agb->dtagb_free - sizeof (dtrace_aggkey_t)) {
2230 		dtrace_buffer_drop(buf);
2231 		return;
2232 	}
2233 
2234 	/*CONSTCOND*/
2235 	ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1)));
2236 	key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t));
2237 	agb->dtagb_free -= sizeof (dtrace_aggkey_t);
2238 
2239 	key->dtak_data = kdata = tomax + offs;
2240 	buf->dtb_offset = offs + fsize;
2241 
2242 	/*
2243 	 * Now copy the data across.
2244 	 */
2245 	*((dtrace_aggid_t *)kdata) = agg->dtag_id;
2246 
2247 	for (i = sizeof (dtrace_aggid_t); i < size; i++)
2248 		kdata[i] = data[i];
2249 
2250 	/*
2251 	 * Because strings are not zeroed out by default, we need to iterate
2252 	 * looking for actions that store strings, and we need to explicitly
2253 	 * pad these strings out with zeroes.
2254 	 */
2255 	for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2256 		int nul;
2257 
2258 		if (!DTRACEACT_ISSTRING(act))
2259 			continue;
2260 
2261 		i = act->dta_rec.dtrd_offset - agg->dtag_base;
2262 		limit = i + act->dta_rec.dtrd_size;
2263 		ASSERT(limit <= size);
2264 
2265 		for (nul = 0; i < limit; i++) {
2266 			if (nul) {
2267 				kdata[i] = '\0';
2268 				continue;
2269 			}
2270 
2271 			if (data[i] != '\0')
2272 				continue;
2273 
2274 			nul = 1;
2275 		}
2276 	}
2277 
2278 	for (i = size; i < fsize; i++)
2279 		kdata[i] = 0;
2280 
2281 	key->dtak_hashval = hashval;
2282 	key->dtak_size = size;
2283 	key->dtak_action = action;
2284 	key->dtak_next = agb->dtagb_hash[ndx];
2285 	agb->dtagb_hash[ndx] = key;
2286 
2287 	/*
2288 	 * Finally, apply the aggregator.
2289 	 */
2290 	*((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial;
2291 	agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg);
2292 }
2293 
2294 /*
2295  * Given consumer state, this routine finds a speculation in the INACTIVE
2296  * state and transitions it into the ACTIVE state.  If there is no speculation
2297  * in the INACTIVE state, 0 is returned.  In this case, no error counter is
2298  * incremented -- it is up to the caller to take appropriate action.
2299  */
2300 static int
2301 dtrace_speculation(dtrace_state_t *state)
2302 {
2303 	int i = 0;
2304 	dtrace_speculation_state_t current;
2305 	uint32_t *stat = &state->dts_speculations_unavail, count;
2306 
2307 	while (i < state->dts_nspeculations) {
2308 		dtrace_speculation_t *spec = &state->dts_speculations[i];
2309 
2310 		current = spec->dtsp_state;
2311 
2312 		if (current != DTRACESPEC_INACTIVE) {
2313 			if (current == DTRACESPEC_COMMITTINGMANY ||
2314 			    current == DTRACESPEC_COMMITTING ||
2315 			    current == DTRACESPEC_DISCARDING)
2316 				stat = &state->dts_speculations_busy;
2317 			i++;
2318 			continue;
2319 		}
2320 
2321 		if (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2322 		    current, DTRACESPEC_ACTIVE) == current)
2323 			return (i + 1);
2324 	}
2325 
2326 	/*
2327 	 * We couldn't find a speculation.  If we found as much as a single
2328 	 * busy speculation buffer, we'll attribute this failure as "busy"
2329 	 * instead of "unavail".
2330 	 */
2331 	do {
2332 		count = *stat;
2333 	} while (dtrace_cas32(stat, count, count + 1) != count);
2334 
2335 	return (0);
2336 }
2337 
2338 /*
2339  * This routine commits an active speculation.  If the specified speculation
2340  * is not in a valid state to perform a commit(), this routine will silently do
2341  * nothing.  The state of the specified speculation is transitioned according
2342  * to the state transition diagram outlined in <sys/dtrace_impl.h>
2343  */
2344 static void
2345 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu,
2346     dtrace_specid_t which)
2347 {
2348 	dtrace_speculation_t *spec;
2349 	dtrace_buffer_t *src, *dest;
2350 	uintptr_t daddr, saddr, dlimit, slimit;
2351 	dtrace_speculation_state_t current, new = 0;
2352 	intptr_t offs;
2353 	uint64_t timestamp;
2354 
2355 	if (which == 0)
2356 		return;
2357 
2358 	if (which > state->dts_nspeculations) {
2359 		cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2360 		return;
2361 	}
2362 
2363 	spec = &state->dts_speculations[which - 1];
2364 	src = &spec->dtsp_buffer[cpu];
2365 	dest = &state->dts_buffer[cpu];
2366 
2367 	do {
2368 		current = spec->dtsp_state;
2369 
2370 		if (current == DTRACESPEC_COMMITTINGMANY)
2371 			break;
2372 
2373 		switch (current) {
2374 		case DTRACESPEC_INACTIVE:
2375 		case DTRACESPEC_DISCARDING:
2376 			return;
2377 
2378 		case DTRACESPEC_COMMITTING:
2379 			/*
2380 			 * This is only possible if we are (a) commit()'ing
2381 			 * without having done a prior speculate() on this CPU
2382 			 * and (b) racing with another commit() on a different
2383 			 * CPU.  There's nothing to do -- we just assert that
2384 			 * our offset is 0.
2385 			 */
2386 			ASSERT(src->dtb_offset == 0);
2387 			return;
2388 
2389 		case DTRACESPEC_ACTIVE:
2390 			new = DTRACESPEC_COMMITTING;
2391 			break;
2392 
2393 		case DTRACESPEC_ACTIVEONE:
2394 			/*
2395 			 * This speculation is active on one CPU.  If our
2396 			 * buffer offset is non-zero, we know that the one CPU
2397 			 * must be us.  Otherwise, we are committing on a
2398 			 * different CPU from the speculate(), and we must
2399 			 * rely on being asynchronously cleaned.
2400 			 */
2401 			if (src->dtb_offset != 0) {
2402 				new = DTRACESPEC_COMMITTING;
2403 				break;
2404 			}
2405 			/*FALLTHROUGH*/
2406 
2407 		case DTRACESPEC_ACTIVEMANY:
2408 			new = DTRACESPEC_COMMITTINGMANY;
2409 			break;
2410 
2411 		default:
2412 			ASSERT(0);
2413 		}
2414 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2415 	    current, new) != current);
2416 
2417 	/*
2418 	 * We have set the state to indicate that we are committing this
2419 	 * speculation.  Now reserve the necessary space in the destination
2420 	 * buffer.
2421 	 */
2422 	if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset,
2423 	    sizeof (uint64_t), state, NULL)) < 0) {
2424 		dtrace_buffer_drop(dest);
2425 		goto out;
2426 	}
2427 
2428 	/*
2429 	 * We have sufficient space to copy the speculative buffer into the
2430 	 * primary buffer.  First, modify the speculative buffer, filling
2431 	 * in the timestamp of all entries with the current time.  The data
2432 	 * must have the commit() time rather than the time it was traced,
2433 	 * so that all entries in the primary buffer are in timestamp order.
2434 	 */
2435 	timestamp = dtrace_gethrtime();
2436 	saddr = (uintptr_t)src->dtb_tomax;
2437 	slimit = saddr + src->dtb_offset;
2438 	while (saddr < slimit) {
2439 		size_t size;
2440 		dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr;
2441 
2442 		if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
2443 			saddr += sizeof (dtrace_epid_t);
2444 			continue;
2445 		}
2446 		ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs);
2447 		size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size;
2448 
2449 		ASSERT3U(saddr + size, <=, slimit);
2450 		ASSERT3U(size, >=, sizeof (dtrace_rechdr_t));
2451 		ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX);
2452 
2453 		DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp);
2454 
2455 		saddr += size;
2456 	}
2457 
2458 	/*
2459 	 * Copy the buffer across.  (Note that this is a
2460 	 * highly subobtimal bcopy(); in the unlikely event that this becomes
2461 	 * a serious performance issue, a high-performance DTrace-specific
2462 	 * bcopy() should obviously be invented.)
2463 	 */
2464 	daddr = (uintptr_t)dest->dtb_tomax + offs;
2465 	dlimit = daddr + src->dtb_offset;
2466 	saddr = (uintptr_t)src->dtb_tomax;
2467 
2468 	/*
2469 	 * First, the aligned portion.
2470 	 */
2471 	while (dlimit - daddr >= sizeof (uint64_t)) {
2472 		*((uint64_t *)daddr) = *((uint64_t *)saddr);
2473 
2474 		daddr += sizeof (uint64_t);
2475 		saddr += sizeof (uint64_t);
2476 	}
2477 
2478 	/*
2479 	 * Now any left-over bit...
2480 	 */
2481 	while (dlimit - daddr)
2482 		*((uint8_t *)daddr++) = *((uint8_t *)saddr++);
2483 
2484 	/*
2485 	 * Finally, commit the reserved space in the destination buffer.
2486 	 */
2487 	dest->dtb_offset = offs + src->dtb_offset;
2488 
2489 out:
2490 	/*
2491 	 * If we're lucky enough to be the only active CPU on this speculation
2492 	 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2493 	 */
2494 	if (current == DTRACESPEC_ACTIVE ||
2495 	    (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) {
2496 		uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state,
2497 		    DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE);
2498 
2499 		ASSERT(rval == DTRACESPEC_COMMITTING);
2500 	}
2501 
2502 	src->dtb_offset = 0;
2503 	src->dtb_xamot_drops += src->dtb_drops;
2504 	src->dtb_drops = 0;
2505 }
2506 
2507 /*
2508  * This routine discards an active speculation.  If the specified speculation
2509  * is not in a valid state to perform a discard(), this routine will silently
2510  * do nothing.  The state of the specified speculation is transitioned
2511  * according to the state transition diagram outlined in <sys/dtrace_impl.h>
2512  */
2513 static void
2514 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu,
2515     dtrace_specid_t which)
2516 {
2517 	dtrace_speculation_t *spec;
2518 	dtrace_speculation_state_t current, new = 0;
2519 	dtrace_buffer_t *buf;
2520 
2521 	if (which == 0)
2522 		return;
2523 
2524 	if (which > state->dts_nspeculations) {
2525 		cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2526 		return;
2527 	}
2528 
2529 	spec = &state->dts_speculations[which - 1];
2530 	buf = &spec->dtsp_buffer[cpu];
2531 
2532 	do {
2533 		current = spec->dtsp_state;
2534 
2535 		switch (current) {
2536 		case DTRACESPEC_INACTIVE:
2537 		case DTRACESPEC_COMMITTINGMANY:
2538 		case DTRACESPEC_COMMITTING:
2539 		case DTRACESPEC_DISCARDING:
2540 			return;
2541 
2542 		case DTRACESPEC_ACTIVE:
2543 		case DTRACESPEC_ACTIVEMANY:
2544 			new = DTRACESPEC_DISCARDING;
2545 			break;
2546 
2547 		case DTRACESPEC_ACTIVEONE:
2548 			if (buf->dtb_offset != 0) {
2549 				new = DTRACESPEC_INACTIVE;
2550 			} else {
2551 				new = DTRACESPEC_DISCARDING;
2552 			}
2553 			break;
2554 
2555 		default:
2556 			ASSERT(0);
2557 		}
2558 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2559 	    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  */
2572 static void
2573 dtrace_speculation_clean_here(dtrace_state_t *state)
2574 {
2575 	dtrace_icookie_t cookie;
2576 	processorid_t cpu = curcpu;
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 < 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 /*
2612  * Note:  not called from probe context.  This function is called
2613  * asynchronously (and at a regular interval) to clean any speculations that
2614  * are in the COMMITTINGMANY or DISCARDING states.  If it discovers that there
2615  * is work to be done, it cross calls all CPUs to perform that work;
2616  * COMMITMANY and DISCARDING speculations may not be transitioned back to the
2617  * INACTIVE state until they have been cleaned by all CPUs.
2618  */
2619 static void
2620 dtrace_speculation_clean(dtrace_state_t *state)
2621 {
2622 	int work = 0, rv;
2623 	dtrace_specid_t i;
2624 
2625 	for (i = 0; i < state->dts_nspeculations; i++) {
2626 		dtrace_speculation_t *spec = &state->dts_speculations[i];
2627 
2628 		ASSERT(!spec->dtsp_cleaning);
2629 
2630 		if (spec->dtsp_state != DTRACESPEC_DISCARDING &&
2631 		    spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2632 			continue;
2633 
2634 		work++;
2635 		spec->dtsp_cleaning = 1;
2636 	}
2637 
2638 	if (!work)
2639 		return;
2640 
2641 	dtrace_xcall(DTRACE_CPUALL,
2642 	    (dtrace_xcall_t)dtrace_speculation_clean_here, state);
2643 
2644 	/*
2645 	 * We now know that all CPUs have committed or discarded their
2646 	 * speculation buffers, as appropriate.  We can now set the state
2647 	 * to inactive.
2648 	 */
2649 	for (i = 0; i < state->dts_nspeculations; i++) {
2650 		dtrace_speculation_t *spec = &state->dts_speculations[i];
2651 		dtrace_speculation_state_t current, new;
2652 
2653 		if (!spec->dtsp_cleaning)
2654 			continue;
2655 
2656 		current = spec->dtsp_state;
2657 		ASSERT(current == DTRACESPEC_DISCARDING ||
2658 		    current == DTRACESPEC_COMMITTINGMANY);
2659 
2660 		new = DTRACESPEC_INACTIVE;
2661 
2662 		rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new);
2663 		ASSERT(rv == current);
2664 		spec->dtsp_cleaning = 0;
2665 	}
2666 }
2667 
2668 /*
2669  * Called as part of a speculate() to get the speculative buffer associated
2670  * with a given speculation.  Returns NULL if the specified speculation is not
2671  * in an ACTIVE state.  If the speculation is in the ACTIVEONE state -- and
2672  * the active CPU is not the specified CPU -- the speculation will be
2673  * atomically transitioned into the ACTIVEMANY state.
2674  */
2675 static dtrace_buffer_t *
2676 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid,
2677     dtrace_specid_t which)
2678 {
2679 	dtrace_speculation_t *spec;
2680 	dtrace_speculation_state_t current, new = 0;
2681 	dtrace_buffer_t *buf;
2682 
2683 	if (which == 0)
2684 		return (NULL);
2685 
2686 	if (which > state->dts_nspeculations) {
2687 		cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2688 		return (NULL);
2689 	}
2690 
2691 	spec = &state->dts_speculations[which - 1];
2692 	buf = &spec->dtsp_buffer[cpuid];
2693 
2694 	do {
2695 		current = spec->dtsp_state;
2696 
2697 		switch (current) {
2698 		case DTRACESPEC_INACTIVE:
2699 		case DTRACESPEC_COMMITTINGMANY:
2700 		case DTRACESPEC_DISCARDING:
2701 			return (NULL);
2702 
2703 		case DTRACESPEC_COMMITTING:
2704 			ASSERT(buf->dtb_offset == 0);
2705 			return (NULL);
2706 
2707 		case DTRACESPEC_ACTIVEONE:
2708 			/*
2709 			 * This speculation is currently active on one CPU.
2710 			 * Check the offset in the buffer; if it's non-zero,
2711 			 * that CPU must be us (and we leave the state alone).
2712 			 * If it's zero, assume that we're starting on a new
2713 			 * CPU -- and change the state to indicate that the
2714 			 * speculation is active on more than one CPU.
2715 			 */
2716 			if (buf->dtb_offset != 0)
2717 				return (buf);
2718 
2719 			new = DTRACESPEC_ACTIVEMANY;
2720 			break;
2721 
2722 		case DTRACESPEC_ACTIVEMANY:
2723 			return (buf);
2724 
2725 		case DTRACESPEC_ACTIVE:
2726 			new = DTRACESPEC_ACTIVEONE;
2727 			break;
2728 
2729 		default:
2730 			ASSERT(0);
2731 		}
2732 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2733 	    current, new) != current);
2734 
2735 	ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY);
2736 	return (buf);
2737 }
2738 
2739 /*
2740  * Return a string.  In the event that the user lacks the privilege to access
2741  * arbitrary kernel memory, we copy the string out to scratch memory so that we
2742  * don't fail access checking.
2743  *
2744  * dtrace_dif_variable() uses this routine as a helper for various
2745  * builtin values such as 'execname' and 'probefunc.'
2746  */
2747 uintptr_t
2748 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state,
2749     dtrace_mstate_t *mstate)
2750 {
2751 	uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
2752 	uintptr_t ret;
2753 	size_t strsz;
2754 
2755 	/*
2756 	 * The easy case: this probe is allowed to read all of memory, so
2757 	 * we can just return this as a vanilla pointer.
2758 	 */
2759 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
2760 		return (addr);
2761 
2762 	/*
2763 	 * This is the tougher case: we copy the string in question from
2764 	 * kernel memory into scratch memory and return it that way: this
2765 	 * ensures that we won't trip up when access checking tests the
2766 	 * BYREF return value.
2767 	 */
2768 	strsz = dtrace_strlen((char *)addr, size) + 1;
2769 
2770 	if (mstate->dtms_scratch_ptr + strsz >
2771 	    mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
2772 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
2773 		return (0);
2774 	}
2775 
2776 	dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
2777 	    strsz);
2778 	ret = mstate->dtms_scratch_ptr;
2779 	mstate->dtms_scratch_ptr += strsz;
2780 	return (ret);
2781 }
2782 
2783 /*
2784  * Return a string from a memoy address which is known to have one or
2785  * more concatenated, individually zero terminated, sub-strings.
2786  * In the event that the user lacks the privilege to access
2787  * arbitrary kernel memory, we copy the string out to scratch memory so that we
2788  * don't fail access checking.
2789  *
2790  * dtrace_dif_variable() uses this routine as a helper for various
2791  * builtin values such as 'execargs'.
2792  */
2793 static uintptr_t
2794 dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state,
2795     dtrace_mstate_t *mstate)
2796 {
2797 	char *p;
2798 	size_t i;
2799 	uintptr_t ret;
2800 
2801 	if (mstate->dtms_scratch_ptr + strsz >
2802 	    mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
2803 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
2804 		return (0);
2805 	}
2806 
2807 	dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
2808 	    strsz);
2809 
2810 	/* Replace sub-string termination characters with a space. */
2811 	for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1;
2812 	    p++, i++)
2813 		if (*p == '\0')
2814 			*p = ' ';
2815 
2816 	ret = mstate->dtms_scratch_ptr;
2817 	mstate->dtms_scratch_ptr += strsz;
2818 	return (ret);
2819 }
2820 
2821 /*
2822  * This function implements the DIF emulator's variable lookups.  The emulator
2823  * passes a reserved variable identifier and optional built-in array index.
2824  */
2825 static uint64_t
2826 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v,
2827     uint64_t ndx)
2828 {
2829 	/*
2830 	 * If we're accessing one of the uncached arguments, we'll turn this
2831 	 * into a reference in the args array.
2832 	 */
2833 	if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) {
2834 		ndx = v - DIF_VAR_ARG0;
2835 		v = DIF_VAR_ARGS;
2836 	}
2837 
2838 	switch (v) {
2839 	case DIF_VAR_ARGS:
2840 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS);
2841 		if (ndx >= sizeof (mstate->dtms_arg) /
2842 		    sizeof (mstate->dtms_arg[0])) {
2843 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
2844 			dtrace_provider_t *pv;
2845 			uint64_t val;
2846 
2847 			pv = mstate->dtms_probe->dtpr_provider;
2848 			if (pv->dtpv_pops.dtps_getargval != NULL)
2849 				val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg,
2850 				    mstate->dtms_probe->dtpr_id,
2851 				    mstate->dtms_probe->dtpr_arg, ndx, aframes);
2852 			else
2853 				val = dtrace_getarg(ndx, aframes);
2854 
2855 			/*
2856 			 * This is regrettably required to keep the compiler
2857 			 * from tail-optimizing the call to dtrace_getarg().
2858 			 * The condition always evaluates to true, but the
2859 			 * compiler has no way of figuring that out a priori.
2860 			 * (None of this would be necessary if the compiler
2861 			 * could be relied upon to _always_ tail-optimize
2862 			 * the call to dtrace_getarg() -- but it can't.)
2863 			 */
2864 			if (mstate->dtms_probe != NULL)
2865 				return (val);
2866 
2867 			ASSERT(0);
2868 		}
2869 
2870 		return (mstate->dtms_arg[ndx]);
2871 
2872 #if defined(sun)
2873 	case DIF_VAR_UREGS: {
2874 		klwp_t *lwp;
2875 
2876 		if (!dtrace_priv_proc(state))
2877 			return (0);
2878 
2879 		if ((lwp = curthread->t_lwp) == NULL) {
2880 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
2881 			cpu_core[curcpu].cpuc_dtrace_illval = NULL;
2882 			return (0);
2883 		}
2884 
2885 		return (dtrace_getreg(lwp->lwp_regs, ndx));
2886 		return (0);
2887 	}
2888 #else
2889 	case DIF_VAR_UREGS: {
2890 		struct trapframe *tframe;
2891 
2892 		if (!dtrace_priv_proc(state))
2893 			return (0);
2894 
2895 		if ((tframe = curthread->td_frame) == NULL) {
2896 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
2897 			cpu_core[curcpu].cpuc_dtrace_illval = 0;
2898 			return (0);
2899 		}
2900 
2901 		return (dtrace_getreg(tframe, ndx));
2902 	}
2903 #endif
2904 
2905 	case DIF_VAR_CURTHREAD:
2906 		if (!dtrace_priv_kernel(state))
2907 			return (0);
2908 		return ((uint64_t)(uintptr_t)curthread);
2909 
2910 	case DIF_VAR_TIMESTAMP:
2911 		if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
2912 			mstate->dtms_timestamp = dtrace_gethrtime();
2913 			mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP;
2914 		}
2915 		return (mstate->dtms_timestamp);
2916 
2917 	case DIF_VAR_VTIMESTAMP:
2918 		ASSERT(dtrace_vtime_references != 0);
2919 		return (curthread->t_dtrace_vtime);
2920 
2921 	case DIF_VAR_WALLTIMESTAMP:
2922 		if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) {
2923 			mstate->dtms_walltimestamp = dtrace_gethrestime();
2924 			mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP;
2925 		}
2926 		return (mstate->dtms_walltimestamp);
2927 
2928 #if defined(sun)
2929 	case DIF_VAR_IPL:
2930 		if (!dtrace_priv_kernel(state))
2931 			return (0);
2932 		if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) {
2933 			mstate->dtms_ipl = dtrace_getipl();
2934 			mstate->dtms_present |= DTRACE_MSTATE_IPL;
2935 		}
2936 		return (mstate->dtms_ipl);
2937 #endif
2938 
2939 	case DIF_VAR_EPID:
2940 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID);
2941 		return (mstate->dtms_epid);
2942 
2943 	case DIF_VAR_ID:
2944 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
2945 		return (mstate->dtms_probe->dtpr_id);
2946 
2947 	case DIF_VAR_STACKDEPTH:
2948 		if (!dtrace_priv_kernel(state))
2949 			return (0);
2950 		if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) {
2951 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
2952 
2953 			mstate->dtms_stackdepth = dtrace_getstackdepth(aframes);
2954 			mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH;
2955 		}
2956 		return (mstate->dtms_stackdepth);
2957 
2958 	case DIF_VAR_USTACKDEPTH:
2959 		if (!dtrace_priv_proc(state))
2960 			return (0);
2961 		if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) {
2962 			/*
2963 			 * See comment in DIF_VAR_PID.
2964 			 */
2965 			if (DTRACE_ANCHORED(mstate->dtms_probe) &&
2966 			    CPU_ON_INTR(CPU)) {
2967 				mstate->dtms_ustackdepth = 0;
2968 			} else {
2969 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2970 				mstate->dtms_ustackdepth =
2971 				    dtrace_getustackdepth();
2972 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
2973 			}
2974 			mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH;
2975 		}
2976 		return (mstate->dtms_ustackdepth);
2977 
2978 	case DIF_VAR_CALLER:
2979 		if (!dtrace_priv_kernel(state))
2980 			return (0);
2981 		if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) {
2982 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
2983 
2984 			if (!DTRACE_ANCHORED(mstate->dtms_probe)) {
2985 				/*
2986 				 * If this is an unanchored probe, we are
2987 				 * required to go through the slow path:
2988 				 * dtrace_caller() only guarantees correct
2989 				 * results for anchored probes.
2990 				 */
2991 				pc_t caller[2] = {0, 0};
2992 
2993 				dtrace_getpcstack(caller, 2, aframes,
2994 				    (uint32_t *)(uintptr_t)mstate->dtms_arg[0]);
2995 				mstate->dtms_caller = caller[1];
2996 			} else if ((mstate->dtms_caller =
2997 			    dtrace_caller(aframes)) == -1) {
2998 				/*
2999 				 * We have failed to do this the quick way;
3000 				 * we must resort to the slower approach of
3001 				 * calling dtrace_getpcstack().
3002 				 */
3003 				pc_t caller = 0;
3004 
3005 				dtrace_getpcstack(&caller, 1, aframes, NULL);
3006 				mstate->dtms_caller = caller;
3007 			}
3008 
3009 			mstate->dtms_present |= DTRACE_MSTATE_CALLER;
3010 		}
3011 		return (mstate->dtms_caller);
3012 
3013 	case DIF_VAR_UCALLER:
3014 		if (!dtrace_priv_proc(state))
3015 			return (0);
3016 
3017 		if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) {
3018 			uint64_t ustack[3];
3019 
3020 			/*
3021 			 * dtrace_getupcstack() fills in the first uint64_t
3022 			 * with the current PID.  The second uint64_t will
3023 			 * be the program counter at user-level.  The third
3024 			 * uint64_t will contain the caller, which is what
3025 			 * we're after.
3026 			 */
3027 			ustack[2] = 0;
3028 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3029 			dtrace_getupcstack(ustack, 3);
3030 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3031 			mstate->dtms_ucaller = ustack[2];
3032 			mstate->dtms_present |= DTRACE_MSTATE_UCALLER;
3033 		}
3034 
3035 		return (mstate->dtms_ucaller);
3036 
3037 	case DIF_VAR_PROBEPROV:
3038 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3039 		return (dtrace_dif_varstr(
3040 		    (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name,
3041 		    state, mstate));
3042 
3043 	case DIF_VAR_PROBEMOD:
3044 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3045 		return (dtrace_dif_varstr(
3046 		    (uintptr_t)mstate->dtms_probe->dtpr_mod,
3047 		    state, mstate));
3048 
3049 	case DIF_VAR_PROBEFUNC:
3050 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3051 		return (dtrace_dif_varstr(
3052 		    (uintptr_t)mstate->dtms_probe->dtpr_func,
3053 		    state, mstate));
3054 
3055 	case DIF_VAR_PROBENAME:
3056 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3057 		return (dtrace_dif_varstr(
3058 		    (uintptr_t)mstate->dtms_probe->dtpr_name,
3059 		    state, mstate));
3060 
3061 	case DIF_VAR_PID:
3062 		if (!dtrace_priv_proc(state))
3063 			return (0);
3064 
3065 #if defined(sun)
3066 		/*
3067 		 * Note that we are assuming that an unanchored probe is
3068 		 * always due to a high-level interrupt.  (And we're assuming
3069 		 * that there is only a single high level interrupt.)
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 		 * Further, it is always safe to dereference the p_pidp member
3078 		 * of one's own proc structure.  (These are truisms becuase
3079 		 * threads and processes don't clean up their own state --
3080 		 * they leave that task to whomever reaps them.)
3081 		 */
3082 		return ((uint64_t)curthread->t_procp->p_pidp->pid_id);
3083 #else
3084 		return ((uint64_t)curproc->p_pid);
3085 #endif
3086 
3087 	case DIF_VAR_PPID:
3088 		if (!dtrace_priv_proc(state))
3089 			return (0);
3090 
3091 #if defined(sun)
3092 		/*
3093 		 * See comment in DIF_VAR_PID.
3094 		 */
3095 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3096 			return (pid0.pid_id);
3097 
3098 		/*
3099 		 * It is always safe to dereference one's own t_procp pointer:
3100 		 * it always points to a valid, allocated proc structure.
3101 		 * (This is true because threads don't clean up their own
3102 		 * state -- they leave that task to whomever reaps them.)
3103 		 */
3104 		return ((uint64_t)curthread->t_procp->p_ppid);
3105 #else
3106 		return ((uint64_t)curproc->p_pptr->p_pid);
3107 #endif
3108 
3109 	case DIF_VAR_TID:
3110 #if defined(sun)
3111 		/*
3112 		 * See comment in DIF_VAR_PID.
3113 		 */
3114 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3115 			return (0);
3116 #endif
3117 
3118 		return ((uint64_t)curthread->t_tid);
3119 
3120 	case DIF_VAR_EXECARGS: {
3121 		struct pargs *p_args = curthread->td_proc->p_args;
3122 
3123 		if (p_args == NULL)
3124 			return(0);
3125 
3126 		return (dtrace_dif_varstrz(
3127 		    (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate));
3128 	}
3129 
3130 	case DIF_VAR_EXECNAME:
3131 #if defined(sun)
3132 		if (!dtrace_priv_proc(state))
3133 			return (0);
3134 
3135 		/*
3136 		 * See comment in DIF_VAR_PID.
3137 		 */
3138 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3139 			return ((uint64_t)(uintptr_t)p0.p_user.u_comm);
3140 
3141 		/*
3142 		 * It is always safe to dereference one's own t_procp pointer:
3143 		 * it always points to a valid, allocated proc structure.
3144 		 * (This is true because threads don't clean up their own
3145 		 * state -- they leave that task to whomever reaps them.)
3146 		 */
3147 		return (dtrace_dif_varstr(
3148 		    (uintptr_t)curthread->t_procp->p_user.u_comm,
3149 		    state, mstate));
3150 #else
3151 		return (dtrace_dif_varstr(
3152 		    (uintptr_t) curthread->td_proc->p_comm, state, mstate));
3153 #endif
3154 
3155 	case DIF_VAR_ZONENAME:
3156 #if defined(sun)
3157 		if (!dtrace_priv_proc(state))
3158 			return (0);
3159 
3160 		/*
3161 		 * See comment in DIF_VAR_PID.
3162 		 */
3163 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3164 			return ((uint64_t)(uintptr_t)p0.p_zone->zone_name);
3165 
3166 		/*
3167 		 * It is always safe to dereference one's own t_procp pointer:
3168 		 * it always points to a valid, allocated proc structure.
3169 		 * (This is true because threads don't clean up their own
3170 		 * state -- they leave that task to whomever reaps them.)
3171 		 */
3172 		return (dtrace_dif_varstr(
3173 		    (uintptr_t)curthread->t_procp->p_zone->zone_name,
3174 		    state, mstate));
3175 #else
3176 		return (0);
3177 #endif
3178 
3179 	case DIF_VAR_UID:
3180 		if (!dtrace_priv_proc(state))
3181 			return (0);
3182 
3183 #if defined(sun)
3184 		/*
3185 		 * See comment in DIF_VAR_PID.
3186 		 */
3187 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3188 			return ((uint64_t)p0.p_cred->cr_uid);
3189 #endif
3190 
3191 		/*
3192 		 * It is always safe to dereference one's own t_procp pointer:
3193 		 * it always points to a valid, allocated proc structure.
3194 		 * (This is true because threads don't clean up their own
3195 		 * state -- they leave that task to whomever reaps them.)
3196 		 *
3197 		 * Additionally, it is safe to dereference one's own process
3198 		 * credential, since this is never NULL after process birth.
3199 		 */
3200 		return ((uint64_t)curthread->t_procp->p_cred->cr_uid);
3201 
3202 	case DIF_VAR_GID:
3203 		if (!dtrace_priv_proc(state))
3204 			return (0);
3205 
3206 #if defined(sun)
3207 		/*
3208 		 * See comment in DIF_VAR_PID.
3209 		 */
3210 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3211 			return ((uint64_t)p0.p_cred->cr_gid);
3212 #endif
3213 
3214 		/*
3215 		 * It is always safe to dereference one's own t_procp pointer:
3216 		 * it always points to a valid, allocated proc structure.
3217 		 * (This is true because threads don't clean up their own
3218 		 * state -- they leave that task to whomever reaps them.)
3219 		 *
3220 		 * Additionally, it is safe to dereference one's own process
3221 		 * credential, since this is never NULL after process birth.
3222 		 */
3223 		return ((uint64_t)curthread->t_procp->p_cred->cr_gid);
3224 
3225 	case DIF_VAR_ERRNO: {
3226 #if defined(sun)
3227 		klwp_t *lwp;
3228 		if (!dtrace_priv_proc(state))
3229 			return (0);
3230 
3231 		/*
3232 		 * See comment in DIF_VAR_PID.
3233 		 */
3234 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3235 			return (0);
3236 
3237 		/*
3238 		 * It is always safe to dereference one's own t_lwp pointer in
3239 		 * the event that this pointer is non-NULL.  (This is true
3240 		 * because threads and lwps don't clean up their own state --
3241 		 * they leave that task to whomever reaps them.)
3242 		 */
3243 		if ((lwp = curthread->t_lwp) == NULL)
3244 			return (0);
3245 
3246 		return ((uint64_t)lwp->lwp_errno);
3247 #else
3248 		return (curthread->td_errno);
3249 #endif
3250 	}
3251 #if !defined(sun)
3252 	case DIF_VAR_CPU: {
3253 		return curcpu;
3254 	}
3255 #endif
3256 	default:
3257 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3258 		return (0);
3259 	}
3260 }
3261 
3262 /*
3263  * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
3264  * Notice that we don't bother validating the proper number of arguments or
3265  * their types in the tuple stack.  This isn't needed because all argument
3266  * interpretation is safe because of our load safety -- the worst that can
3267  * happen is that a bogus program can obtain bogus results.
3268  */
3269 static void
3270 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
3271     dtrace_key_t *tupregs, int nargs,
3272     dtrace_mstate_t *mstate, dtrace_state_t *state)
3273 {
3274 	volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
3275 	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
3276 	dtrace_vstate_t *vstate = &state->dts_vstate;
3277 
3278 #if defined(sun)
3279 	union {
3280 		mutex_impl_t mi;
3281 		uint64_t mx;
3282 	} m;
3283 
3284 	union {
3285 		krwlock_t ri;
3286 		uintptr_t rw;
3287 	} r;
3288 #else
3289 	struct thread *lowner;
3290 	union {
3291 		struct lock_object *li;
3292 		uintptr_t lx;
3293 	} l;
3294 #endif
3295 
3296 	switch (subr) {
3297 	case DIF_SUBR_RAND:
3298 		regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875;
3299 		break;
3300 
3301 #if defined(sun)
3302 	case DIF_SUBR_MUTEX_OWNED:
3303 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3304 		    mstate, vstate)) {
3305 			regs[rd] = 0;
3306 			break;
3307 		}
3308 
3309 		m.mx = dtrace_load64(tupregs[0].dttk_value);
3310 		if (MUTEX_TYPE_ADAPTIVE(&m.mi))
3311 			regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER;
3312 		else
3313 			regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock);
3314 		break;
3315 
3316 	case DIF_SUBR_MUTEX_OWNER:
3317 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3318 		    mstate, vstate)) {
3319 			regs[rd] = 0;
3320 			break;
3321 		}
3322 
3323 		m.mx = dtrace_load64(tupregs[0].dttk_value);
3324 		if (MUTEX_TYPE_ADAPTIVE(&m.mi) &&
3325 		    MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER)
3326 			regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi);
3327 		else
3328 			regs[rd] = 0;
3329 		break;
3330 
3331 	case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
3332 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3333 		    mstate, vstate)) {
3334 			regs[rd] = 0;
3335 			break;
3336 		}
3337 
3338 		m.mx = dtrace_load64(tupregs[0].dttk_value);
3339 		regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi);
3340 		break;
3341 
3342 	case DIF_SUBR_MUTEX_TYPE_SPIN:
3343 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3344 		    mstate, vstate)) {
3345 			regs[rd] = 0;
3346 			break;
3347 		}
3348 
3349 		m.mx = dtrace_load64(tupregs[0].dttk_value);
3350 		regs[rd] = MUTEX_TYPE_SPIN(&m.mi);
3351 		break;
3352 
3353 	case DIF_SUBR_RW_READ_HELD: {
3354 		uintptr_t tmp;
3355 
3356 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
3357 		    mstate, vstate)) {
3358 			regs[rd] = 0;
3359 			break;
3360 		}
3361 
3362 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3363 		regs[rd] = _RW_READ_HELD(&r.ri, tmp);
3364 		break;
3365 	}
3366 
3367 	case DIF_SUBR_RW_WRITE_HELD:
3368 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
3369 		    mstate, vstate)) {
3370 			regs[rd] = 0;
3371 			break;
3372 		}
3373 
3374 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3375 		regs[rd] = _RW_WRITE_HELD(&r.ri);
3376 		break;
3377 
3378 	case DIF_SUBR_RW_ISWRITER:
3379 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
3380 		    mstate, vstate)) {
3381 			regs[rd] = 0;
3382 			break;
3383 		}
3384 
3385 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3386 		regs[rd] = _RW_ISWRITER(&r.ri);
3387 		break;
3388 
3389 #else
3390 	case DIF_SUBR_MUTEX_OWNED:
3391 		if (!dtrace_canload(tupregs[0].dttk_value,
3392 			sizeof (struct lock_object), mstate, vstate)) {
3393 			regs[rd] = 0;
3394 			break;
3395 		}
3396 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
3397 		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
3398 		break;
3399 
3400 	case DIF_SUBR_MUTEX_OWNER:
3401 		if (!dtrace_canload(tupregs[0].dttk_value,
3402 			sizeof (struct lock_object), mstate, vstate)) {
3403 			regs[rd] = 0;
3404 			break;
3405 		}
3406 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
3407 		LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
3408 		regs[rd] = (uintptr_t)lowner;
3409 		break;
3410 
3411 	case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
3412 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx),
3413 		    mstate, vstate)) {
3414 			regs[rd] = 0;
3415 			break;
3416 		}
3417 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
3418 		/* XXX - should be only LC_SLEEPABLE? */
3419 		regs[rd] = (LOCK_CLASS(l.li)->lc_flags &
3420 		    (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0;
3421 		break;
3422 
3423 	case DIF_SUBR_MUTEX_TYPE_SPIN:
3424 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx),
3425 		    mstate, vstate)) {
3426 			regs[rd] = 0;
3427 			break;
3428 		}
3429 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
3430 		regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0;
3431 		break;
3432 
3433 	case DIF_SUBR_RW_READ_HELD:
3434 	case DIF_SUBR_SX_SHARED_HELD:
3435 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
3436 		    mstate, vstate)) {
3437 			regs[rd] = 0;
3438 			break;
3439 		}
3440 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
3441 		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) &&
3442 		    lowner == NULL;
3443 		break;
3444 
3445 	case DIF_SUBR_RW_WRITE_HELD:
3446 	case DIF_SUBR_SX_EXCLUSIVE_HELD:
3447 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
3448 		    mstate, vstate)) {
3449 			regs[rd] = 0;
3450 			break;
3451 		}
3452 		l.lx = dtrace_loadptr(tupregs[0].dttk_value);
3453 		LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
3454 		regs[rd] = (lowner == curthread);
3455 		break;
3456 
3457 	case DIF_SUBR_RW_ISWRITER:
3458 	case DIF_SUBR_SX_ISEXCLUSIVE:
3459 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
3460 		    mstate, vstate)) {
3461 			regs[rd] = 0;
3462 			break;
3463 		}
3464 		l.lx = dtrace_loadptr(tupregs[0].dttk_value);
3465 		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) &&
3466 		    lowner != NULL;
3467 		break;
3468 #endif /* ! defined(sun) */
3469 
3470 	case DIF_SUBR_BCOPY: {
3471 		/*
3472 		 * We need to be sure that the destination is in the scratch
3473 		 * region -- no other region is allowed.
3474 		 */
3475 		uintptr_t src = tupregs[0].dttk_value;
3476 		uintptr_t dest = tupregs[1].dttk_value;
3477 		size_t size = tupregs[2].dttk_value;
3478 
3479 		if (!dtrace_inscratch(dest, size, mstate)) {
3480 			*flags |= CPU_DTRACE_BADADDR;
3481 			*illval = regs[rd];
3482 			break;
3483 		}
3484 
3485 		if (!dtrace_canload(src, size, mstate, vstate)) {
3486 			regs[rd] = 0;
3487 			break;
3488 		}
3489 
3490 		dtrace_bcopy((void *)src, (void *)dest, size);
3491 		break;
3492 	}
3493 
3494 	case DIF_SUBR_ALLOCA:
3495 	case DIF_SUBR_COPYIN: {
3496 		uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
3497 		uint64_t size =
3498 		    tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value;
3499 		size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size;
3500 
3501 		/*
3502 		 * This action doesn't require any credential checks since
3503 		 * probes will not activate in user contexts to which the
3504 		 * enabling user does not have permissions.
3505 		 */
3506 
3507 		/*
3508 		 * Rounding up the user allocation size could have overflowed
3509 		 * a large, bogus allocation (like -1ULL) to 0.
3510 		 */
3511 		if (scratch_size < size ||
3512 		    !DTRACE_INSCRATCH(mstate, scratch_size)) {
3513 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3514 			regs[rd] = 0;
3515 			break;
3516 		}
3517 
3518 		if (subr == DIF_SUBR_COPYIN) {
3519 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3520 			dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
3521 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3522 		}
3523 
3524 		mstate->dtms_scratch_ptr += scratch_size;
3525 		regs[rd] = dest;
3526 		break;
3527 	}
3528 
3529 	case DIF_SUBR_COPYINTO: {
3530 		uint64_t size = tupregs[1].dttk_value;
3531 		uintptr_t dest = tupregs[2].dttk_value;
3532 
3533 		/*
3534 		 * This action doesn't require any credential checks since
3535 		 * probes will not activate in user contexts to which the
3536 		 * enabling user does not have permissions.
3537 		 */
3538 		if (!dtrace_inscratch(dest, size, mstate)) {
3539 			*flags |= CPU_DTRACE_BADADDR;
3540 			*illval = regs[rd];
3541 			break;
3542 		}
3543 
3544 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3545 		dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
3546 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3547 		break;
3548 	}
3549 
3550 	case DIF_SUBR_COPYINSTR: {
3551 		uintptr_t dest = mstate->dtms_scratch_ptr;
3552 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3553 
3554 		if (nargs > 1 && tupregs[1].dttk_value < size)
3555 			size = tupregs[1].dttk_value + 1;
3556 
3557 		/*
3558 		 * This action doesn't require any credential checks since
3559 		 * probes will not activate in user contexts to which the
3560 		 * enabling user does not have permissions.
3561 		 */
3562 		if (!DTRACE_INSCRATCH(mstate, size)) {
3563 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3564 			regs[rd] = 0;
3565 			break;
3566 		}
3567 
3568 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3569 		dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags);
3570 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3571 
3572 		((char *)dest)[size - 1] = '\0';
3573 		mstate->dtms_scratch_ptr += size;
3574 		regs[rd] = dest;
3575 		break;
3576 	}
3577 
3578 #if defined(sun)
3579 	case DIF_SUBR_MSGSIZE:
3580 	case DIF_SUBR_MSGDSIZE: {
3581 		uintptr_t baddr = tupregs[0].dttk_value, daddr;
3582 		uintptr_t wptr, rptr;
3583 		size_t count = 0;
3584 		int cont = 0;
3585 
3586 		while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) {
3587 
3588 			if (!dtrace_canload(baddr, sizeof (mblk_t), mstate,
3589 			    vstate)) {
3590 				regs[rd] = 0;
3591 				break;
3592 			}
3593 
3594 			wptr = dtrace_loadptr(baddr +
3595 			    offsetof(mblk_t, b_wptr));
3596 
3597 			rptr = dtrace_loadptr(baddr +
3598 			    offsetof(mblk_t, b_rptr));
3599 
3600 			if (wptr < rptr) {
3601 				*flags |= CPU_DTRACE_BADADDR;
3602 				*illval = tupregs[0].dttk_value;
3603 				break;
3604 			}
3605 
3606 			daddr = dtrace_loadptr(baddr +
3607 			    offsetof(mblk_t, b_datap));
3608 
3609 			baddr = dtrace_loadptr(baddr +
3610 			    offsetof(mblk_t, b_cont));
3611 
3612 			/*
3613 			 * We want to prevent against denial-of-service here,
3614 			 * so we're only going to search the list for
3615 			 * dtrace_msgdsize_max mblks.
3616 			 */
3617 			if (cont++ > dtrace_msgdsize_max) {
3618 				*flags |= CPU_DTRACE_ILLOP;
3619 				break;
3620 			}
3621 
3622 			if (subr == DIF_SUBR_MSGDSIZE) {
3623 				if (dtrace_load8(daddr +
3624 				    offsetof(dblk_t, db_type)) != M_DATA)
3625 					continue;
3626 			}
3627 
3628 			count += wptr - rptr;
3629 		}
3630 
3631 		if (!(*flags & CPU_DTRACE_FAULT))
3632 			regs[rd] = count;
3633 
3634 		break;
3635 	}
3636 #endif
3637 
3638 	case DIF_SUBR_PROGENYOF: {
3639 		pid_t pid = tupregs[0].dttk_value;
3640 		proc_t *p;
3641 		int rval = 0;
3642 
3643 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3644 
3645 		for (p = curthread->t_procp; p != NULL; p = p->p_parent) {
3646 #if defined(sun)
3647 			if (p->p_pidp->pid_id == pid) {
3648 #else
3649 			if (p->p_pid == pid) {
3650 #endif
3651 				rval = 1;
3652 				break;
3653 			}
3654 		}
3655 
3656 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3657 
3658 		regs[rd] = rval;
3659 		break;
3660 	}
3661 
3662 	case DIF_SUBR_SPECULATION:
3663 		regs[rd] = dtrace_speculation(state);
3664 		break;
3665 
3666 	case DIF_SUBR_COPYOUT: {
3667 		uintptr_t kaddr = tupregs[0].dttk_value;
3668 		uintptr_t uaddr = tupregs[1].dttk_value;
3669 		uint64_t size = tupregs[2].dttk_value;
3670 
3671 		if (!dtrace_destructive_disallow &&
3672 		    dtrace_priv_proc_control(state) &&
3673 		    !dtrace_istoxic(kaddr, size)) {
3674 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3675 			dtrace_copyout(kaddr, uaddr, size, flags);
3676 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3677 		}
3678 		break;
3679 	}
3680 
3681 	case DIF_SUBR_COPYOUTSTR: {
3682 		uintptr_t kaddr = tupregs[0].dttk_value;
3683 		uintptr_t uaddr = tupregs[1].dttk_value;
3684 		uint64_t size = tupregs[2].dttk_value;
3685 
3686 		if (!dtrace_destructive_disallow &&
3687 		    dtrace_priv_proc_control(state) &&
3688 		    !dtrace_istoxic(kaddr, size)) {
3689 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3690 			dtrace_copyoutstr(kaddr, uaddr, size, flags);
3691 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3692 		}
3693 		break;
3694 	}
3695 
3696 	case DIF_SUBR_STRLEN: {
3697 		size_t sz;
3698 		uintptr_t addr = (uintptr_t)tupregs[0].dttk_value;
3699 		sz = dtrace_strlen((char *)addr,
3700 		    state->dts_options[DTRACEOPT_STRSIZE]);
3701 
3702 		if (!dtrace_canload(addr, sz + 1, mstate, vstate)) {
3703 			regs[rd] = 0;
3704 			break;
3705 		}
3706 
3707 		regs[rd] = sz;
3708 
3709 		break;
3710 	}
3711 
3712 	case DIF_SUBR_STRCHR:
3713 	case DIF_SUBR_STRRCHR: {
3714 		/*
3715 		 * We're going to iterate over the string looking for the
3716 		 * specified character.  We will iterate until we have reached
3717 		 * the string length or we have found the character.  If this
3718 		 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
3719 		 * of the specified character instead of the first.
3720 		 */
3721 		uintptr_t saddr = tupregs[0].dttk_value;
3722 		uintptr_t addr = tupregs[0].dttk_value;
3723 		uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE];
3724 		char c, target = (char)tupregs[1].dttk_value;
3725 
3726 		for (regs[rd] = 0; addr < limit; addr++) {
3727 			if ((c = dtrace_load8(addr)) == target) {
3728 				regs[rd] = addr;
3729 
3730 				if (subr == DIF_SUBR_STRCHR)
3731 					break;
3732 			}
3733 
3734 			if (c == '\0')
3735 				break;
3736 		}
3737 
3738 		if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) {
3739 			regs[rd] = 0;
3740 			break;
3741 		}
3742 
3743 		break;
3744 	}
3745 
3746 	case DIF_SUBR_STRSTR:
3747 	case DIF_SUBR_INDEX:
3748 	case DIF_SUBR_RINDEX: {
3749 		/*
3750 		 * We're going to iterate over the string looking for the
3751 		 * specified string.  We will iterate until we have reached
3752 		 * the string length or we have found the string.  (Yes, this
3753 		 * is done in the most naive way possible -- but considering
3754 		 * that the string we're searching for is likely to be
3755 		 * relatively short, the complexity of Rabin-Karp or similar
3756 		 * hardly seems merited.)
3757 		 */
3758 		char *addr = (char *)(uintptr_t)tupregs[0].dttk_value;
3759 		char *substr = (char *)(uintptr_t)tupregs[1].dttk_value;
3760 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3761 		size_t len = dtrace_strlen(addr, size);
3762 		size_t sublen = dtrace_strlen(substr, size);
3763 		char *limit = addr + len, *orig = addr;
3764 		int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1;
3765 		int inc = 1;
3766 
3767 		regs[rd] = notfound;
3768 
3769 		if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) {
3770 			regs[rd] = 0;
3771 			break;
3772 		}
3773 
3774 		if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate,
3775 		    vstate)) {
3776 			regs[rd] = 0;
3777 			break;
3778 		}
3779 
3780 		/*
3781 		 * strstr() and index()/rindex() have similar semantics if
3782 		 * both strings are the empty string: strstr() returns a
3783 		 * pointer to the (empty) string, and index() and rindex()
3784 		 * both return index 0 (regardless of any position argument).
3785 		 */
3786 		if (sublen == 0 && len == 0) {
3787 			if (subr == DIF_SUBR_STRSTR)
3788 				regs[rd] = (uintptr_t)addr;
3789 			else
3790 				regs[rd] = 0;
3791 			break;
3792 		}
3793 
3794 		if (subr != DIF_SUBR_STRSTR) {
3795 			if (subr == DIF_SUBR_RINDEX) {
3796 				limit = orig - 1;
3797 				addr += len;
3798 				inc = -1;
3799 			}
3800 
3801 			/*
3802 			 * Both index() and rindex() take an optional position
3803 			 * argument that denotes the starting position.
3804 			 */
3805 			if (nargs == 3) {
3806 				int64_t pos = (int64_t)tupregs[2].dttk_value;
3807 
3808 				/*
3809 				 * If the position argument to index() is
3810 				 * negative, Perl implicitly clamps it at
3811 				 * zero.  This semantic is a little surprising
3812 				 * given the special meaning of negative
3813 				 * positions to similar Perl functions like
3814 				 * substr(), but it appears to reflect a
3815 				 * notion that index() can start from a
3816 				 * negative index and increment its way up to
3817 				 * the string.  Given this notion, Perl's
3818 				 * rindex() is at least self-consistent in
3819 				 * that it implicitly clamps positions greater
3820 				 * than the string length to be the string
3821 				 * length.  Where Perl completely loses
3822 				 * coherence, however, is when the specified
3823 				 * substring is the empty string ("").  In
3824 				 * this case, even if the position is
3825 				 * negative, rindex() returns 0 -- and even if
3826 				 * the position is greater than the length,
3827 				 * index() returns the string length.  These
3828 				 * semantics violate the notion that index()
3829 				 * should never return a value less than the
3830 				 * specified position and that rindex() should
3831 				 * never return a value greater than the
3832 				 * specified position.  (One assumes that
3833 				 * these semantics are artifacts of Perl's
3834 				 * implementation and not the results of
3835 				 * deliberate design -- it beggars belief that
3836 				 * even Larry Wall could desire such oddness.)
3837 				 * While in the abstract one would wish for
3838 				 * consistent position semantics across
3839 				 * substr(), index() and rindex() -- or at the
3840 				 * very least self-consistent position
3841 				 * semantics for index() and rindex() -- we
3842 				 * instead opt to keep with the extant Perl
3843 				 * semantics, in all their broken glory.  (Do
3844 				 * we have more desire to maintain Perl's
3845 				 * semantics than Perl does?  Probably.)
3846 				 */
3847 				if (subr == DIF_SUBR_RINDEX) {
3848 					if (pos < 0) {
3849 						if (sublen == 0)
3850 							regs[rd] = 0;
3851 						break;
3852 					}
3853 
3854 					if (pos > len)
3855 						pos = len;
3856 				} else {
3857 					if (pos < 0)
3858 						pos = 0;
3859 
3860 					if (pos >= len) {
3861 						if (sublen == 0)
3862 							regs[rd] = len;
3863 						break;
3864 					}
3865 				}
3866 
3867 				addr = orig + pos;
3868 			}
3869 		}
3870 
3871 		for (regs[rd] = notfound; addr != limit; addr += inc) {
3872 			if (dtrace_strncmp(addr, substr, sublen) == 0) {
3873 				if (subr != DIF_SUBR_STRSTR) {
3874 					/*
3875 					 * As D index() and rindex() are
3876 					 * modeled on Perl (and not on awk),
3877 					 * we return a zero-based (and not a
3878 					 * one-based) index.  (For you Perl
3879 					 * weenies: no, we're not going to add
3880 					 * $[ -- and shouldn't you be at a con
3881 					 * or something?)
3882 					 */
3883 					regs[rd] = (uintptr_t)(addr - orig);
3884 					break;
3885 				}
3886 
3887 				ASSERT(subr == DIF_SUBR_STRSTR);
3888 				regs[rd] = (uintptr_t)addr;
3889 				break;
3890 			}
3891 		}
3892 
3893 		break;
3894 	}
3895 
3896 	case DIF_SUBR_STRTOK: {
3897 		uintptr_t addr = tupregs[0].dttk_value;
3898 		uintptr_t tokaddr = tupregs[1].dttk_value;
3899 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3900 		uintptr_t limit, toklimit = tokaddr + size;
3901 		uint8_t c = 0, tokmap[32];	 /* 256 / 8 */
3902 		char *dest = (char *)mstate->dtms_scratch_ptr;
3903 		int i;
3904 
3905 		/*
3906 		 * Check both the token buffer and (later) the input buffer,
3907 		 * since both could be non-scratch addresses.
3908 		 */
3909 		if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) {
3910 			regs[rd] = 0;
3911 			break;
3912 		}
3913 
3914 		if (!DTRACE_INSCRATCH(mstate, size)) {
3915 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3916 			regs[rd] = 0;
3917 			break;
3918 		}
3919 
3920 		if (addr == 0) {
3921 			/*
3922 			 * If the address specified is NULL, we use our saved
3923 			 * strtok pointer from the mstate.  Note that this
3924 			 * means that the saved strtok pointer is _only_
3925 			 * valid within multiple enablings of the same probe --
3926 			 * it behaves like an implicit clause-local variable.
3927 			 */
3928 			addr = mstate->dtms_strtok;
3929 		} else {
3930 			/*
3931 			 * If the user-specified address is non-NULL we must
3932 			 * access check it.  This is the only time we have
3933 			 * a chance to do so, since this address may reside
3934 			 * in the string table of this clause-- future calls
3935 			 * (when we fetch addr from mstate->dtms_strtok)
3936 			 * would fail this access check.
3937 			 */
3938 			if (!dtrace_strcanload(addr, size, mstate, vstate)) {
3939 				regs[rd] = 0;
3940 				break;
3941 			}
3942 		}
3943 
3944 		/*
3945 		 * First, zero the token map, and then process the token
3946 		 * string -- setting a bit in the map for every character
3947 		 * found in the token string.
3948 		 */
3949 		for (i = 0; i < sizeof (tokmap); i++)
3950 			tokmap[i] = 0;
3951 
3952 		for (; tokaddr < toklimit; tokaddr++) {
3953 			if ((c = dtrace_load8(tokaddr)) == '\0')
3954 				break;
3955 
3956 			ASSERT((c >> 3) < sizeof (tokmap));
3957 			tokmap[c >> 3] |= (1 << (c & 0x7));
3958 		}
3959 
3960 		for (limit = addr + size; addr < limit; addr++) {
3961 			/*
3962 			 * We're looking for a character that is _not_ contained
3963 			 * in the token string.
3964 			 */
3965 			if ((c = dtrace_load8(addr)) == '\0')
3966 				break;
3967 
3968 			if (!(tokmap[c >> 3] & (1 << (c & 0x7))))
3969 				break;
3970 		}
3971 
3972 		if (c == '\0') {
3973 			/*
3974 			 * We reached the end of the string without finding
3975 			 * any character that was not in the token string.
3976 			 * We return NULL in this case, and we set the saved
3977 			 * address to NULL as well.
3978 			 */
3979 			regs[rd] = 0;
3980 			mstate->dtms_strtok = 0;
3981 			break;
3982 		}
3983 
3984 		/*
3985 		 * From here on, we're copying into the destination string.
3986 		 */
3987 		for (i = 0; addr < limit && i < size - 1; addr++) {
3988 			if ((c = dtrace_load8(addr)) == '\0')
3989 				break;
3990 
3991 			if (tokmap[c >> 3] & (1 << (c & 0x7)))
3992 				break;
3993 
3994 			ASSERT(i < size);
3995 			dest[i++] = c;
3996 		}
3997 
3998 		ASSERT(i < size);
3999 		dest[i] = '\0';
4000 		regs[rd] = (uintptr_t)dest;
4001 		mstate->dtms_scratch_ptr += size;
4002 		mstate->dtms_strtok = addr;
4003 		break;
4004 	}
4005 
4006 	case DIF_SUBR_SUBSTR: {
4007 		uintptr_t s = tupregs[0].dttk_value;
4008 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4009 		char *d = (char *)mstate->dtms_scratch_ptr;
4010 		int64_t index = (int64_t)tupregs[1].dttk_value;
4011 		int64_t remaining = (int64_t)tupregs[2].dttk_value;
4012 		size_t len = dtrace_strlen((char *)s, size);
4013 		int64_t i = 0;
4014 
4015 		if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4016 			regs[rd] = 0;
4017 			break;
4018 		}
4019 
4020 		if (!DTRACE_INSCRATCH(mstate, size)) {
4021 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4022 			regs[rd] = 0;
4023 			break;
4024 		}
4025 
4026 		if (nargs <= 2)
4027 			remaining = (int64_t)size;
4028 
4029 		if (index < 0) {
4030 			index += len;
4031 
4032 			if (index < 0 && index + remaining > 0) {
4033 				remaining += index;
4034 				index = 0;
4035 			}
4036 		}
4037 
4038 		if (index >= len || index < 0) {
4039 			remaining = 0;
4040 		} else if (remaining < 0) {
4041 			remaining += len - index;
4042 		} else if (index + remaining > size) {
4043 			remaining = size - index;
4044 		}
4045 
4046 		for (i = 0; i < remaining; i++) {
4047 			if ((d[i] = dtrace_load8(s + index + i)) == '\0')
4048 				break;
4049 		}
4050 
4051 		d[i] = '\0';
4052 
4053 		mstate->dtms_scratch_ptr += size;
4054 		regs[rd] = (uintptr_t)d;
4055 		break;
4056 	}
4057 
4058 	case DIF_SUBR_TOUPPER:
4059 	case DIF_SUBR_TOLOWER: {
4060 		uintptr_t s = tupregs[0].dttk_value;
4061 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4062 		char *dest = (char *)mstate->dtms_scratch_ptr, c;
4063 		size_t len = dtrace_strlen((char *)s, size);
4064 		char lower, upper, convert;
4065 		int64_t i;
4066 
4067 		if (subr == DIF_SUBR_TOUPPER) {
4068 			lower = 'a';
4069 			upper = 'z';
4070 			convert = 'A';
4071 		} else {
4072 			lower = 'A';
4073 			upper = 'Z';
4074 			convert = 'a';
4075 		}
4076 
4077 		if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4078 			regs[rd] = 0;
4079 			break;
4080 		}
4081 
4082 		if (!DTRACE_INSCRATCH(mstate, size)) {
4083 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4084 			regs[rd] = 0;
4085 			break;
4086 		}
4087 
4088 		for (i = 0; i < size - 1; i++) {
4089 			if ((c = dtrace_load8(s + i)) == '\0')
4090 				break;
4091 
4092 			if (c >= lower && c <= upper)
4093 				c = convert + (c - lower);
4094 
4095 			dest[i] = c;
4096 		}
4097 
4098 		ASSERT(i < size);
4099 		dest[i] = '\0';
4100 		regs[rd] = (uintptr_t)dest;
4101 		mstate->dtms_scratch_ptr += size;
4102 		break;
4103 	}
4104 
4105 #if defined(sun)
4106 	case DIF_SUBR_GETMAJOR:
4107 #ifdef _LP64
4108 		regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64;
4109 #else
4110 		regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ;
4111 #endif
4112 		break;
4113 
4114 	case DIF_SUBR_GETMINOR:
4115 #ifdef _LP64
4116 		regs[rd] = tupregs[0].dttk_value & MAXMIN64;
4117 #else
4118 		regs[rd] = tupregs[0].dttk_value & MAXMIN;
4119 #endif
4120 		break;
4121 
4122 	case DIF_SUBR_DDI_PATHNAME: {
4123 		/*
4124 		 * This one is a galactic mess.  We are going to roughly
4125 		 * emulate ddi_pathname(), but it's made more complicated
4126 		 * by the fact that we (a) want to include the minor name and
4127 		 * (b) must proceed iteratively instead of recursively.
4128 		 */
4129 		uintptr_t dest = mstate->dtms_scratch_ptr;
4130 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4131 		char *start = (char *)dest, *end = start + size - 1;
4132 		uintptr_t daddr = tupregs[0].dttk_value;
4133 		int64_t minor = (int64_t)tupregs[1].dttk_value;
4134 		char *s;
4135 		int i, len, depth = 0;
4136 
4137 		/*
4138 		 * Due to all the pointer jumping we do and context we must
4139 		 * rely upon, we just mandate that the user must have kernel
4140 		 * read privileges to use this routine.
4141 		 */
4142 		if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) {
4143 			*flags |= CPU_DTRACE_KPRIV;
4144 			*illval = daddr;
4145 			regs[rd] = 0;
4146 		}
4147 
4148 		if (!DTRACE_INSCRATCH(mstate, size)) {
4149 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4150 			regs[rd] = 0;
4151 			break;
4152 		}
4153 
4154 		*end = '\0';
4155 
4156 		/*
4157 		 * We want to have a name for the minor.  In order to do this,
4158 		 * we need to walk the minor list from the devinfo.  We want
4159 		 * to be sure that we don't infinitely walk a circular list,
4160 		 * so we check for circularity by sending a scout pointer
4161 		 * ahead two elements for every element that we iterate over;
4162 		 * if the list is circular, these will ultimately point to the
4163 		 * same element.  You may recognize this little trick as the
4164 		 * answer to a stupid interview question -- one that always
4165 		 * seems to be asked by those who had to have it laboriously
4166 		 * explained to them, and who can't even concisely describe
4167 		 * the conditions under which one would be forced to resort to
4168 		 * this technique.  Needless to say, those conditions are
4169 		 * found here -- and probably only here.  Is this the only use
4170 		 * of this infamous trick in shipping, production code?  If it
4171 		 * isn't, it probably should be...
4172 		 */
4173 		if (minor != -1) {
4174 			uintptr_t maddr = dtrace_loadptr(daddr +
4175 			    offsetof(struct dev_info, devi_minor));
4176 
4177 			uintptr_t next = offsetof(struct ddi_minor_data, next);
4178 			uintptr_t name = offsetof(struct ddi_minor_data,
4179 			    d_minor) + offsetof(struct ddi_minor, name);
4180 			uintptr_t dev = offsetof(struct ddi_minor_data,
4181 			    d_minor) + offsetof(struct ddi_minor, dev);
4182 			uintptr_t scout;
4183 
4184 			if (maddr != NULL)
4185 				scout = dtrace_loadptr(maddr + next);
4186 
4187 			while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4188 				uint64_t m;
4189 #ifdef _LP64
4190 				m = dtrace_load64(maddr + dev) & MAXMIN64;
4191 #else
4192 				m = dtrace_load32(maddr + dev) & MAXMIN;
4193 #endif
4194 				if (m != minor) {
4195 					maddr = dtrace_loadptr(maddr + next);
4196 
4197 					if (scout == NULL)
4198 						continue;
4199 
4200 					scout = dtrace_loadptr(scout + next);
4201 
4202 					if (scout == NULL)
4203 						continue;
4204 
4205 					scout = dtrace_loadptr(scout + next);
4206 
4207 					if (scout == NULL)
4208 						continue;
4209 
4210 					if (scout == maddr) {
4211 						*flags |= CPU_DTRACE_ILLOP;
4212 						break;
4213 					}
4214 
4215 					continue;
4216 				}
4217 
4218 				/*
4219 				 * We have the minor data.  Now we need to
4220 				 * copy the minor's name into the end of the
4221 				 * pathname.
4222 				 */
4223 				s = (char *)dtrace_loadptr(maddr + name);
4224 				len = dtrace_strlen(s, size);
4225 
4226 				if (*flags & CPU_DTRACE_FAULT)
4227 					break;
4228 
4229 				if (len != 0) {
4230 					if ((end -= (len + 1)) < start)
4231 						break;
4232 
4233 					*end = ':';
4234 				}
4235 
4236 				for (i = 1; i <= len; i++)
4237 					end[i] = dtrace_load8((uintptr_t)s++);
4238 				break;
4239 			}
4240 		}
4241 
4242 		while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4243 			ddi_node_state_t devi_state;
4244 
4245 			devi_state = dtrace_load32(daddr +
4246 			    offsetof(struct dev_info, devi_node_state));
4247 
4248 			if (*flags & CPU_DTRACE_FAULT)
4249 				break;
4250 
4251 			if (devi_state >= DS_INITIALIZED) {
4252 				s = (char *)dtrace_loadptr(daddr +
4253 				    offsetof(struct dev_info, devi_addr));
4254 				len = dtrace_strlen(s, size);
4255 
4256 				if (*flags & CPU_DTRACE_FAULT)
4257 					break;
4258 
4259 				if (len != 0) {
4260 					if ((end -= (len + 1)) < start)
4261 						break;
4262 
4263 					*end = '@';
4264 				}
4265 
4266 				for (i = 1; i <= len; i++)
4267 					end[i] = dtrace_load8((uintptr_t)s++);
4268 			}
4269 
4270 			/*
4271 			 * Now for the node name...
4272 			 */
4273 			s = (char *)dtrace_loadptr(daddr +
4274 			    offsetof(struct dev_info, devi_node_name));
4275 
4276 			daddr = dtrace_loadptr(daddr +
4277 			    offsetof(struct dev_info, devi_parent));
4278 
4279 			/*
4280 			 * If our parent is NULL (that is, if we're the root
4281 			 * node), we're going to use the special path
4282 			 * "devices".
4283 			 */
4284 			if (daddr == 0)
4285 				s = "devices";
4286 
4287 			len = dtrace_strlen(s, size);
4288 			if (*flags & CPU_DTRACE_FAULT)
4289 				break;
4290 
4291 			if ((end -= (len + 1)) < start)
4292 				break;
4293 
4294 			for (i = 1; i <= len; i++)
4295 				end[i] = dtrace_load8((uintptr_t)s++);
4296 			*end = '/';
4297 
4298 			if (depth++ > dtrace_devdepth_max) {
4299 				*flags |= CPU_DTRACE_ILLOP;
4300 				break;
4301 			}
4302 		}
4303 
4304 		if (end < start)
4305 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4306 
4307 		if (daddr == 0) {
4308 			regs[rd] = (uintptr_t)end;
4309 			mstate->dtms_scratch_ptr += size;
4310 		}
4311 
4312 		break;
4313 	}
4314 #endif
4315 
4316 	case DIF_SUBR_STRJOIN: {
4317 		char *d = (char *)mstate->dtms_scratch_ptr;
4318 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4319 		uintptr_t s1 = tupregs[0].dttk_value;
4320 		uintptr_t s2 = tupregs[1].dttk_value;
4321 		int i = 0;
4322 
4323 		if (!dtrace_strcanload(s1, size, mstate, vstate) ||
4324 		    !dtrace_strcanload(s2, size, mstate, vstate)) {
4325 			regs[rd] = 0;
4326 			break;
4327 		}
4328 
4329 		if (!DTRACE_INSCRATCH(mstate, size)) {
4330 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4331 			regs[rd] = 0;
4332 			break;
4333 		}
4334 
4335 		for (;;) {
4336 			if (i >= size) {
4337 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4338 				regs[rd] = 0;
4339 				break;
4340 			}
4341 
4342 			if ((d[i++] = dtrace_load8(s1++)) == '\0') {
4343 				i--;
4344 				break;
4345 			}
4346 		}
4347 
4348 		for (;;) {
4349 			if (i >= size) {
4350 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4351 				regs[rd] = 0;
4352 				break;
4353 			}
4354 
4355 			if ((d[i++] = dtrace_load8(s2++)) == '\0')
4356 				break;
4357 		}
4358 
4359 		if (i < size) {
4360 			mstate->dtms_scratch_ptr += i;
4361 			regs[rd] = (uintptr_t)d;
4362 		}
4363 
4364 		break;
4365 	}
4366 
4367 	case DIF_SUBR_LLTOSTR: {
4368 		int64_t i = (int64_t)tupregs[0].dttk_value;
4369 		uint64_t val, digit;
4370 		uint64_t size = 65;	/* enough room for 2^64 in binary */
4371 		char *end = (char *)mstate->dtms_scratch_ptr + size - 1;
4372 		int base = 10;
4373 
4374 		if (nargs > 1) {
4375 			if ((base = tupregs[1].dttk_value) <= 1 ||
4376 			    base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4377 				*flags |= CPU_DTRACE_ILLOP;
4378 				break;
4379 			}
4380 		}
4381 
4382 		val = (base == 10 && i < 0) ? i * -1 : i;
4383 
4384 		if (!DTRACE_INSCRATCH(mstate, size)) {
4385 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4386 			regs[rd] = 0;
4387 			break;
4388 		}
4389 
4390 		for (*end-- = '\0'; val; val /= base) {
4391 			if ((digit = val % base) <= '9' - '0') {
4392 				*end-- = '0' + digit;
4393 			} else {
4394 				*end-- = 'a' + (digit - ('9' - '0') - 1);
4395 			}
4396 		}
4397 
4398 		if (i == 0 && base == 16)
4399 			*end-- = '0';
4400 
4401 		if (base == 16)
4402 			*end-- = 'x';
4403 
4404 		if (i == 0 || base == 8 || base == 16)
4405 			*end-- = '0';
4406 
4407 		if (i < 0 && base == 10)
4408 			*end-- = '-';
4409 
4410 		regs[rd] = (uintptr_t)end + 1;
4411 		mstate->dtms_scratch_ptr += size;
4412 		break;
4413 	}
4414 
4415 	case DIF_SUBR_HTONS:
4416 	case DIF_SUBR_NTOHS:
4417 #if BYTE_ORDER == BIG_ENDIAN
4418 		regs[rd] = (uint16_t)tupregs[0].dttk_value;
4419 #else
4420 		regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value);
4421 #endif
4422 		break;
4423 
4424 
4425 	case DIF_SUBR_HTONL:
4426 	case DIF_SUBR_NTOHL:
4427 #if BYTE_ORDER == BIG_ENDIAN
4428 		regs[rd] = (uint32_t)tupregs[0].dttk_value;
4429 #else
4430 		regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value);
4431 #endif
4432 		break;
4433 
4434 
4435 	case DIF_SUBR_HTONLL:
4436 	case DIF_SUBR_NTOHLL:
4437 #if BYTE_ORDER == BIG_ENDIAN
4438 		regs[rd] = (uint64_t)tupregs[0].dttk_value;
4439 #else
4440 		regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value);
4441 #endif
4442 		break;
4443 
4444 
4445 	case DIF_SUBR_DIRNAME:
4446 	case DIF_SUBR_BASENAME: {
4447 		char *dest = (char *)mstate->dtms_scratch_ptr;
4448 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4449 		uintptr_t src = tupregs[0].dttk_value;
4450 		int i, j, len = dtrace_strlen((char *)src, size);
4451 		int lastbase = -1, firstbase = -1, lastdir = -1;
4452 		int start, end;
4453 
4454 		if (!dtrace_canload(src, len + 1, mstate, vstate)) {
4455 			regs[rd] = 0;
4456 			break;
4457 		}
4458 
4459 		if (!DTRACE_INSCRATCH(mstate, size)) {
4460 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4461 			regs[rd] = 0;
4462 			break;
4463 		}
4464 
4465 		/*
4466 		 * The basename and dirname for a zero-length string is
4467 		 * defined to be "."
4468 		 */
4469 		if (len == 0) {
4470 			len = 1;
4471 			src = (uintptr_t)".";
4472 		}
4473 
4474 		/*
4475 		 * Start from the back of the string, moving back toward the
4476 		 * front until we see a character that isn't a slash.  That
4477 		 * character is the last character in the basename.
4478 		 */
4479 		for (i = len - 1; i >= 0; i--) {
4480 			if (dtrace_load8(src + i) != '/')
4481 				break;
4482 		}
4483 
4484 		if (i >= 0)
4485 			lastbase = i;
4486 
4487 		/*
4488 		 * Starting from the last character in the basename, move
4489 		 * towards the front until we find a slash.  The character
4490 		 * that we processed immediately before that is the first
4491 		 * character in the basename.
4492 		 */
4493 		for (; i >= 0; i--) {
4494 			if (dtrace_load8(src + i) == '/')
4495 				break;
4496 		}
4497 
4498 		if (i >= 0)
4499 			firstbase = i + 1;
4500 
4501 		/*
4502 		 * Now keep going until we find a non-slash character.  That
4503 		 * character is the last character in the dirname.
4504 		 */
4505 		for (; i >= 0; i--) {
4506 			if (dtrace_load8(src + i) != '/')
4507 				break;
4508 		}
4509 
4510 		if (i >= 0)
4511 			lastdir = i;
4512 
4513 		ASSERT(!(lastbase == -1 && firstbase != -1));
4514 		ASSERT(!(firstbase == -1 && lastdir != -1));
4515 
4516 		if (lastbase == -1) {
4517 			/*
4518 			 * We didn't find a non-slash character.  We know that
4519 			 * the length is non-zero, so the whole string must be
4520 			 * slashes.  In either the dirname or the basename
4521 			 * case, we return '/'.
4522 			 */
4523 			ASSERT(firstbase == -1);
4524 			firstbase = lastbase = lastdir = 0;
4525 		}
4526 
4527 		if (firstbase == -1) {
4528 			/*
4529 			 * The entire string consists only of a basename
4530 			 * component.  If we're looking for dirname, we need
4531 			 * to change our string to be just "."; if we're
4532 			 * looking for a basename, we'll just set the first
4533 			 * character of the basename to be 0.
4534 			 */
4535 			if (subr == DIF_SUBR_DIRNAME) {
4536 				ASSERT(lastdir == -1);
4537 				src = (uintptr_t)".";
4538 				lastdir = 0;
4539 			} else {
4540 				firstbase = 0;
4541 			}
4542 		}
4543 
4544 		if (subr == DIF_SUBR_DIRNAME) {
4545 			if (lastdir == -1) {
4546 				/*
4547 				 * We know that we have a slash in the name --
4548 				 * or lastdir would be set to 0, above.  And
4549 				 * because lastdir is -1, we know that this
4550 				 * slash must be the first character.  (That
4551 				 * is, the full string must be of the form
4552 				 * "/basename".)  In this case, the last
4553 				 * character of the directory name is 0.
4554 				 */
4555 				lastdir = 0;
4556 			}
4557 
4558 			start = 0;
4559 			end = lastdir;
4560 		} else {
4561 			ASSERT(subr == DIF_SUBR_BASENAME);
4562 			ASSERT(firstbase != -1 && lastbase != -1);
4563 			start = firstbase;
4564 			end = lastbase;
4565 		}
4566 
4567 		for (i = start, j = 0; i <= end && j < size - 1; i++, j++)
4568 			dest[j] = dtrace_load8(src + i);
4569 
4570 		dest[j] = '\0';
4571 		regs[rd] = (uintptr_t)dest;
4572 		mstate->dtms_scratch_ptr += size;
4573 		break;
4574 	}
4575 
4576 	case DIF_SUBR_CLEANPATH: {
4577 		char *dest = (char *)mstate->dtms_scratch_ptr, c;
4578 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4579 		uintptr_t src = tupregs[0].dttk_value;
4580 		int i = 0, j = 0;
4581 
4582 		if (!dtrace_strcanload(src, size, mstate, vstate)) {
4583 			regs[rd] = 0;
4584 			break;
4585 		}
4586 
4587 		if (!DTRACE_INSCRATCH(mstate, size)) {
4588 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4589 			regs[rd] = 0;
4590 			break;
4591 		}
4592 
4593 		/*
4594 		 * Move forward, loading each character.
4595 		 */
4596 		do {
4597 			c = dtrace_load8(src + i++);
4598 next:
4599 			if (j + 5 >= size)	/* 5 = strlen("/..c\0") */
4600 				break;
4601 
4602 			if (c != '/') {
4603 				dest[j++] = c;
4604 				continue;
4605 			}
4606 
4607 			c = dtrace_load8(src + i++);
4608 
4609 			if (c == '/') {
4610 				/*
4611 				 * We have two slashes -- we can just advance
4612 				 * to the next character.
4613 				 */
4614 				goto next;
4615 			}
4616 
4617 			if (c != '.') {
4618 				/*
4619 				 * This is not "." and it's not ".." -- we can
4620 				 * just store the "/" and this character and
4621 				 * drive on.
4622 				 */
4623 				dest[j++] = '/';
4624 				dest[j++] = c;
4625 				continue;
4626 			}
4627 
4628 			c = dtrace_load8(src + i++);
4629 
4630 			if (c == '/') {
4631 				/*
4632 				 * This is a "/./" component.  We're not going
4633 				 * to store anything in the destination buffer;
4634 				 * we're just going to go to the next component.
4635 				 */
4636 				goto next;
4637 			}
4638 
4639 			if (c != '.') {
4640 				/*
4641 				 * This is not ".." -- we can just store the
4642 				 * "/." and this character and continue
4643 				 * processing.
4644 				 */
4645 				dest[j++] = '/';
4646 				dest[j++] = '.';
4647 				dest[j++] = c;
4648 				continue;
4649 			}
4650 
4651 			c = dtrace_load8(src + i++);
4652 
4653 			if (c != '/' && c != '\0') {
4654 				/*
4655 				 * This is not ".." -- it's "..[mumble]".
4656 				 * We'll store the "/.." and this character
4657 				 * and continue processing.
4658 				 */
4659 				dest[j++] = '/';
4660 				dest[j++] = '.';
4661 				dest[j++] = '.';
4662 				dest[j++] = c;
4663 				continue;
4664 			}
4665 
4666 			/*
4667 			 * This is "/../" or "/..\0".  We need to back up
4668 			 * our destination pointer until we find a "/".
4669 			 */
4670 			i--;
4671 			while (j != 0 && dest[--j] != '/')
4672 				continue;
4673 
4674 			if (c == '\0')
4675 				dest[++j] = '/';
4676 		} while (c != '\0');
4677 
4678 		dest[j] = '\0';
4679 		regs[rd] = (uintptr_t)dest;
4680 		mstate->dtms_scratch_ptr += size;
4681 		break;
4682 	}
4683 
4684 	case DIF_SUBR_INET_NTOA:
4685 	case DIF_SUBR_INET_NTOA6:
4686 	case DIF_SUBR_INET_NTOP: {
4687 		size_t size;
4688 		int af, argi, i;
4689 		char *base, *end;
4690 
4691 		if (subr == DIF_SUBR_INET_NTOP) {
4692 			af = (int)tupregs[0].dttk_value;
4693 			argi = 1;
4694 		} else {
4695 			af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6;
4696 			argi = 0;
4697 		}
4698 
4699 		if (af == AF_INET) {
4700 			ipaddr_t ip4;
4701 			uint8_t *ptr8, val;
4702 
4703 			/*
4704 			 * Safely load the IPv4 address.
4705 			 */
4706 			ip4 = dtrace_load32(tupregs[argi].dttk_value);
4707 
4708 			/*
4709 			 * Check an IPv4 string will fit in scratch.
4710 			 */
4711 			size = INET_ADDRSTRLEN;
4712 			if (!DTRACE_INSCRATCH(mstate, size)) {
4713 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4714 				regs[rd] = 0;
4715 				break;
4716 			}
4717 			base = (char *)mstate->dtms_scratch_ptr;
4718 			end = (char *)mstate->dtms_scratch_ptr + size - 1;
4719 
4720 			/*
4721 			 * Stringify as a dotted decimal quad.
4722 			 */
4723 			*end-- = '\0';
4724 			ptr8 = (uint8_t *)&ip4;
4725 			for (i = 3; i >= 0; i--) {
4726 				val = ptr8[i];
4727 
4728 				if (val == 0) {
4729 					*end-- = '0';
4730 				} else {
4731 					for (; val; val /= 10) {
4732 						*end-- = '0' + (val % 10);
4733 					}
4734 				}
4735 
4736 				if (i > 0)
4737 					*end-- = '.';
4738 			}
4739 			ASSERT(end + 1 >= base);
4740 
4741 		} else if (af == AF_INET6) {
4742 			struct in6_addr ip6;
4743 			int firstzero, tryzero, numzero, v6end;
4744 			uint16_t val;
4745 			const char digits[] = "0123456789abcdef";
4746 
4747 			/*
4748 			 * Stringify using RFC 1884 convention 2 - 16 bit
4749 			 * hexadecimal values with a zero-run compression.
4750 			 * Lower case hexadecimal digits are used.
4751 			 * 	eg, fe80::214:4fff:fe0b:76c8.
4752 			 * The IPv4 embedded form is returned for inet_ntop,
4753 			 * just the IPv4 string is returned for inet_ntoa6.
4754 			 */
4755 
4756 			/*
4757 			 * Safely load the IPv6 address.
4758 			 */
4759 			dtrace_bcopy(
4760 			    (void *)(uintptr_t)tupregs[argi].dttk_value,
4761 			    (void *)(uintptr_t)&ip6, sizeof (struct in6_addr));
4762 
4763 			/*
4764 			 * Check an IPv6 string will fit in scratch.
4765 			 */
4766 			size = INET6_ADDRSTRLEN;
4767 			if (!DTRACE_INSCRATCH(mstate, size)) {
4768 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4769 				regs[rd] = 0;
4770 				break;
4771 			}
4772 			base = (char *)mstate->dtms_scratch_ptr;
4773 			end = (char *)mstate->dtms_scratch_ptr + size - 1;
4774 			*end-- = '\0';
4775 
4776 			/*
4777 			 * Find the longest run of 16 bit zero values
4778 			 * for the single allowed zero compression - "::".
4779 			 */
4780 			firstzero = -1;
4781 			tryzero = -1;
4782 			numzero = 1;
4783 			for (i = 0; i < sizeof (struct in6_addr); i++) {
4784 #if defined(sun)
4785 				if (ip6._S6_un._S6_u8[i] == 0 &&
4786 #else
4787 				if (ip6.__u6_addr.__u6_addr8[i] == 0 &&
4788 #endif
4789 				    tryzero == -1 && i % 2 == 0) {
4790 					tryzero = i;
4791 					continue;
4792 				}
4793 
4794 				if (tryzero != -1 &&
4795 #if defined(sun)
4796 				    (ip6._S6_un._S6_u8[i] != 0 ||
4797 #else
4798 				    (ip6.__u6_addr.__u6_addr8[i] != 0 ||
4799 #endif
4800 				    i == sizeof (struct in6_addr) - 1)) {
4801 
4802 					if (i - tryzero <= numzero) {
4803 						tryzero = -1;
4804 						continue;
4805 					}
4806 
4807 					firstzero = tryzero;
4808 					numzero = i - i % 2 - tryzero;
4809 					tryzero = -1;
4810 
4811 #if defined(sun)
4812 					if (ip6._S6_un._S6_u8[i] == 0 &&
4813 #else
4814 					if (ip6.__u6_addr.__u6_addr8[i] == 0 &&
4815 #endif
4816 					    i == sizeof (struct in6_addr) - 1)
4817 						numzero += 2;
4818 				}
4819 			}
4820 			ASSERT(firstzero + numzero <= sizeof (struct in6_addr));
4821 
4822 			/*
4823 			 * Check for an IPv4 embedded address.
4824 			 */
4825 			v6end = sizeof (struct in6_addr) - 2;
4826 			if (IN6_IS_ADDR_V4MAPPED(&ip6) ||
4827 			    IN6_IS_ADDR_V4COMPAT(&ip6)) {
4828 				for (i = sizeof (struct in6_addr) - 1;
4829 				    i >= DTRACE_V4MAPPED_OFFSET; i--) {
4830 					ASSERT(end >= base);
4831 
4832 #if defined(sun)
4833 					val = ip6._S6_un._S6_u8[i];
4834 #else
4835 					val = ip6.__u6_addr.__u6_addr8[i];
4836 #endif
4837 
4838 					if (val == 0) {
4839 						*end-- = '0';
4840 					} else {
4841 						for (; val; val /= 10) {
4842 							*end-- = '0' + val % 10;
4843 						}
4844 					}
4845 
4846 					if (i > DTRACE_V4MAPPED_OFFSET)
4847 						*end-- = '.';
4848 				}
4849 
4850 				if (subr == DIF_SUBR_INET_NTOA6)
4851 					goto inetout;
4852 
4853 				/*
4854 				 * Set v6end to skip the IPv4 address that
4855 				 * we have already stringified.
4856 				 */
4857 				v6end = 10;
4858 			}
4859 
4860 			/*
4861 			 * Build the IPv6 string by working through the
4862 			 * address in reverse.
4863 			 */
4864 			for (i = v6end; i >= 0; i -= 2) {
4865 				ASSERT(end >= base);
4866 
4867 				if (i == firstzero + numzero - 2) {
4868 					*end-- = ':';
4869 					*end-- = ':';
4870 					i -= numzero - 2;
4871 					continue;
4872 				}
4873 
4874 				if (i < 14 && i != firstzero - 2)
4875 					*end-- = ':';
4876 
4877 #if defined(sun)
4878 				val = (ip6._S6_un._S6_u8[i] << 8) +
4879 				    ip6._S6_un._S6_u8[i + 1];
4880 #else
4881 				val = (ip6.__u6_addr.__u6_addr8[i] << 8) +
4882 				    ip6.__u6_addr.__u6_addr8[i + 1];
4883 #endif
4884 
4885 				if (val == 0) {
4886 					*end-- = '0';
4887 				} else {
4888 					for (; val; val /= 16) {
4889 						*end-- = digits[val % 16];
4890 					}
4891 				}
4892 			}
4893 			ASSERT(end + 1 >= base);
4894 
4895 		} else {
4896 			/*
4897 			 * The user didn't use AH_INET or AH_INET6.
4898 			 */
4899 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4900 			regs[rd] = 0;
4901 			break;
4902 		}
4903 
4904 inetout:	regs[rd] = (uintptr_t)end + 1;
4905 		mstate->dtms_scratch_ptr += size;
4906 		break;
4907 	}
4908 
4909 	case DIF_SUBR_MEMREF: {
4910 		uintptr_t size = 2 * sizeof(uintptr_t);
4911 		uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t));
4912 		size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size;
4913 
4914 		/* address and length */
4915 		memref[0] = tupregs[0].dttk_value;
4916 		memref[1] = tupregs[1].dttk_value;
4917 
4918 		regs[rd] = (uintptr_t) memref;
4919 		mstate->dtms_scratch_ptr += scratch_size;
4920 		break;
4921 	}
4922 
4923 	case DIF_SUBR_TYPEREF: {
4924 		uintptr_t size = 4 * sizeof(uintptr_t);
4925 		uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t));
4926 		size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size;
4927 
4928 		/* address, num_elements, type_str, type_len */
4929 		typeref[0] = tupregs[0].dttk_value;
4930 		typeref[1] = tupregs[1].dttk_value;
4931 		typeref[2] = tupregs[2].dttk_value;
4932 		typeref[3] = tupregs[3].dttk_value;
4933 
4934 		regs[rd] = (uintptr_t) typeref;
4935 		mstate->dtms_scratch_ptr += scratch_size;
4936 		break;
4937 	}
4938 	}
4939 }
4940 
4941 /*
4942  * Emulate the execution of DTrace IR instructions specified by the given
4943  * DIF object.  This function is deliberately void of assertions as all of
4944  * the necessary checks are handled by a call to dtrace_difo_validate().
4945  */
4946 static uint64_t
4947 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate,
4948     dtrace_vstate_t *vstate, dtrace_state_t *state)
4949 {
4950 	const dif_instr_t *text = difo->dtdo_buf;
4951 	const uint_t textlen = difo->dtdo_len;
4952 	const char *strtab = difo->dtdo_strtab;
4953 	const uint64_t *inttab = difo->dtdo_inttab;
4954 
4955 	uint64_t rval = 0;
4956 	dtrace_statvar_t *svar;
4957 	dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
4958 	dtrace_difv_t *v;
4959 	volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
4960 	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
4961 
4962 	dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
4963 	uint64_t regs[DIF_DIR_NREGS];
4964 	uint64_t *tmp;
4965 
4966 	uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0;
4967 	int64_t cc_r;
4968 	uint_t pc = 0, id, opc = 0;
4969 	uint8_t ttop = 0;
4970 	dif_instr_t instr;
4971 	uint_t r1, r2, rd;
4972 
4973 	/*
4974 	 * We stash the current DIF object into the machine state: we need it
4975 	 * for subsequent access checking.
4976 	 */
4977 	mstate->dtms_difo = difo;
4978 
4979 	regs[DIF_REG_R0] = 0; 		/* %r0 is fixed at zero */
4980 
4981 	while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) {
4982 		opc = pc;
4983 
4984 		instr = text[pc++];
4985 		r1 = DIF_INSTR_R1(instr);
4986 		r2 = DIF_INSTR_R2(instr);
4987 		rd = DIF_INSTR_RD(instr);
4988 
4989 		switch (DIF_INSTR_OP(instr)) {
4990 		case DIF_OP_OR:
4991 			regs[rd] = regs[r1] | regs[r2];
4992 			break;
4993 		case DIF_OP_XOR:
4994 			regs[rd] = regs[r1] ^ regs[r2];
4995 			break;
4996 		case DIF_OP_AND:
4997 			regs[rd] = regs[r1] & regs[r2];
4998 			break;
4999 		case DIF_OP_SLL:
5000 			regs[rd] = regs[r1] << regs[r2];
5001 			break;
5002 		case DIF_OP_SRL:
5003 			regs[rd] = regs[r1] >> regs[r2];
5004 			break;
5005 		case DIF_OP_SUB:
5006 			regs[rd] = regs[r1] - regs[r2];
5007 			break;
5008 		case DIF_OP_ADD:
5009 			regs[rd] = regs[r1] + regs[r2];
5010 			break;
5011 		case DIF_OP_MUL:
5012 			regs[rd] = regs[r1] * regs[r2];
5013 			break;
5014 		case DIF_OP_SDIV:
5015 			if (regs[r2] == 0) {
5016 				regs[rd] = 0;
5017 				*flags |= CPU_DTRACE_DIVZERO;
5018 			} else {
5019 				regs[rd] = (int64_t)regs[r1] /
5020 				    (int64_t)regs[r2];
5021 			}
5022 			break;
5023 
5024 		case DIF_OP_UDIV:
5025 			if (regs[r2] == 0) {
5026 				regs[rd] = 0;
5027 				*flags |= CPU_DTRACE_DIVZERO;
5028 			} else {
5029 				regs[rd] = regs[r1] / regs[r2];
5030 			}
5031 			break;
5032 
5033 		case DIF_OP_SREM:
5034 			if (regs[r2] == 0) {
5035 				regs[rd] = 0;
5036 				*flags |= CPU_DTRACE_DIVZERO;
5037 			} else {
5038 				regs[rd] = (int64_t)regs[r1] %
5039 				    (int64_t)regs[r2];
5040 			}
5041 			break;
5042 
5043 		case DIF_OP_UREM:
5044 			if (regs[r2] == 0) {
5045 				regs[rd] = 0;
5046 				*flags |= CPU_DTRACE_DIVZERO;
5047 			} else {
5048 				regs[rd] = regs[r1] % regs[r2];
5049 			}
5050 			break;
5051 
5052 		case DIF_OP_NOT:
5053 			regs[rd] = ~regs[r1];
5054 			break;
5055 		case DIF_OP_MOV:
5056 			regs[rd] = regs[r1];
5057 			break;
5058 		case DIF_OP_CMP:
5059 			cc_r = regs[r1] - regs[r2];
5060 			cc_n = cc_r < 0;
5061 			cc_z = cc_r == 0;
5062 			cc_v = 0;
5063 			cc_c = regs[r1] < regs[r2];
5064 			break;
5065 		case DIF_OP_TST:
5066 			cc_n = cc_v = cc_c = 0;
5067 			cc_z = regs[r1] == 0;
5068 			break;
5069 		case DIF_OP_BA:
5070 			pc = DIF_INSTR_LABEL(instr);
5071 			break;
5072 		case DIF_OP_BE:
5073 			if (cc_z)
5074 				pc = DIF_INSTR_LABEL(instr);
5075 			break;
5076 		case DIF_OP_BNE:
5077 			if (cc_z == 0)
5078 				pc = DIF_INSTR_LABEL(instr);
5079 			break;
5080 		case DIF_OP_BG:
5081 			if ((cc_z | (cc_n ^ cc_v)) == 0)
5082 				pc = DIF_INSTR_LABEL(instr);
5083 			break;
5084 		case DIF_OP_BGU:
5085 			if ((cc_c | cc_z) == 0)
5086 				pc = DIF_INSTR_LABEL(instr);
5087 			break;
5088 		case DIF_OP_BGE:
5089 			if ((cc_n ^ cc_v) == 0)
5090 				pc = DIF_INSTR_LABEL(instr);
5091 			break;
5092 		case DIF_OP_BGEU:
5093 			if (cc_c == 0)
5094 				pc = DIF_INSTR_LABEL(instr);
5095 			break;
5096 		case DIF_OP_BL:
5097 			if (cc_n ^ cc_v)
5098 				pc = DIF_INSTR_LABEL(instr);
5099 			break;
5100 		case DIF_OP_BLU:
5101 			if (cc_c)
5102 				pc = DIF_INSTR_LABEL(instr);
5103 			break;
5104 		case DIF_OP_BLE:
5105 			if (cc_z | (cc_n ^ cc_v))
5106 				pc = DIF_INSTR_LABEL(instr);
5107 			break;
5108 		case DIF_OP_BLEU:
5109 			if (cc_c | cc_z)
5110 				pc = DIF_INSTR_LABEL(instr);
5111 			break;
5112 		case DIF_OP_RLDSB:
5113 			if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) {
5114 				*flags |= CPU_DTRACE_KPRIV;
5115 				*illval = regs[r1];
5116 				break;
5117 			}
5118 			/*FALLTHROUGH*/
5119 		case DIF_OP_LDSB:
5120 			regs[rd] = (int8_t)dtrace_load8(regs[r1]);
5121 			break;
5122 		case DIF_OP_RLDSH:
5123 			if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) {
5124 				*flags |= CPU_DTRACE_KPRIV;
5125 				*illval = regs[r1];
5126 				break;
5127 			}
5128 			/*FALLTHROUGH*/
5129 		case DIF_OP_LDSH:
5130 			regs[rd] = (int16_t)dtrace_load16(regs[r1]);
5131 			break;
5132 		case DIF_OP_RLDSW:
5133 			if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) {
5134 				*flags |= CPU_DTRACE_KPRIV;
5135 				*illval = regs[r1];
5136 				break;
5137 			}
5138 			/*FALLTHROUGH*/
5139 		case DIF_OP_LDSW:
5140 			regs[rd] = (int32_t)dtrace_load32(regs[r1]);
5141 			break;
5142 		case DIF_OP_RLDUB:
5143 			if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) {
5144 				*flags |= CPU_DTRACE_KPRIV;
5145 				*illval = regs[r1];
5146 				break;
5147 			}
5148 			/*FALLTHROUGH*/
5149 		case DIF_OP_LDUB:
5150 			regs[rd] = dtrace_load8(regs[r1]);
5151 			break;
5152 		case DIF_OP_RLDUH:
5153 			if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) {
5154 				*flags |= CPU_DTRACE_KPRIV;
5155 				*illval = regs[r1];
5156 				break;
5157 			}
5158 			/*FALLTHROUGH*/
5159 		case DIF_OP_LDUH:
5160 			regs[rd] = dtrace_load16(regs[r1]);
5161 			break;
5162 		case DIF_OP_RLDUW:
5163 			if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) {
5164 				*flags |= CPU_DTRACE_KPRIV;
5165 				*illval = regs[r1];
5166 				break;
5167 			}
5168 			/*FALLTHROUGH*/
5169 		case DIF_OP_LDUW:
5170 			regs[rd] = dtrace_load32(regs[r1]);
5171 			break;
5172 		case DIF_OP_RLDX:
5173 			if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) {
5174 				*flags |= CPU_DTRACE_KPRIV;
5175 				*illval = regs[r1];
5176 				break;
5177 			}
5178 			/*FALLTHROUGH*/
5179 		case DIF_OP_LDX:
5180 			regs[rd] = dtrace_load64(regs[r1]);
5181 			break;
5182 		case DIF_OP_ULDSB:
5183 			regs[rd] = (int8_t)
5184 			    dtrace_fuword8((void *)(uintptr_t)regs[r1]);
5185 			break;
5186 		case DIF_OP_ULDSH:
5187 			regs[rd] = (int16_t)
5188 			    dtrace_fuword16((void *)(uintptr_t)regs[r1]);
5189 			break;
5190 		case DIF_OP_ULDSW:
5191 			regs[rd] = (int32_t)
5192 			    dtrace_fuword32((void *)(uintptr_t)regs[r1]);
5193 			break;
5194 		case DIF_OP_ULDUB:
5195 			regs[rd] =
5196 			    dtrace_fuword8((void *)(uintptr_t)regs[r1]);
5197 			break;
5198 		case DIF_OP_ULDUH:
5199 			regs[rd] =
5200 			    dtrace_fuword16((void *)(uintptr_t)regs[r1]);
5201 			break;
5202 		case DIF_OP_ULDUW:
5203 			regs[rd] =
5204 			    dtrace_fuword32((void *)(uintptr_t)regs[r1]);
5205 			break;
5206 		case DIF_OP_ULDX:
5207 			regs[rd] =
5208 			    dtrace_fuword64((void *)(uintptr_t)regs[r1]);
5209 			break;
5210 		case DIF_OP_RET:
5211 			rval = regs[rd];
5212 			pc = textlen;
5213 			break;
5214 		case DIF_OP_NOP:
5215 			break;
5216 		case DIF_OP_SETX:
5217 			regs[rd] = inttab[DIF_INSTR_INTEGER(instr)];
5218 			break;
5219 		case DIF_OP_SETS:
5220 			regs[rd] = (uint64_t)(uintptr_t)
5221 			    (strtab + DIF_INSTR_STRING(instr));
5222 			break;
5223 		case DIF_OP_SCMP: {
5224 			size_t sz = state->dts_options[DTRACEOPT_STRSIZE];
5225 			uintptr_t s1 = regs[r1];
5226 			uintptr_t s2 = regs[r2];
5227 
5228 			if (s1 != 0 &&
5229 			    !dtrace_strcanload(s1, sz, mstate, vstate))
5230 				break;
5231 			if (s2 != 0 &&
5232 			    !dtrace_strcanload(s2, sz, mstate, vstate))
5233 				break;
5234 
5235 			cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz);
5236 
5237 			cc_n = cc_r < 0;
5238 			cc_z = cc_r == 0;
5239 			cc_v = cc_c = 0;
5240 			break;
5241 		}
5242 		case DIF_OP_LDGA:
5243 			regs[rd] = dtrace_dif_variable(mstate, state,
5244 			    r1, regs[r2]);
5245 			break;
5246 		case DIF_OP_LDGS:
5247 			id = DIF_INSTR_VAR(instr);
5248 
5249 			if (id >= DIF_VAR_OTHER_UBASE) {
5250 				uintptr_t a;
5251 
5252 				id -= DIF_VAR_OTHER_UBASE;
5253 				svar = vstate->dtvs_globals[id];
5254 				ASSERT(svar != NULL);
5255 				v = &svar->dtsv_var;
5256 
5257 				if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) {
5258 					regs[rd] = svar->dtsv_data;
5259 					break;
5260 				}
5261 
5262 				a = (uintptr_t)svar->dtsv_data;
5263 
5264 				if (*(uint8_t *)a == UINT8_MAX) {
5265 					/*
5266 					 * If the 0th byte is set to UINT8_MAX
5267 					 * then this is to be treated as a
5268 					 * reference to a NULL variable.
5269 					 */
5270 					regs[rd] = 0;
5271 				} else {
5272 					regs[rd] = a + sizeof (uint64_t);
5273 				}
5274 
5275 				break;
5276 			}
5277 
5278 			regs[rd] = dtrace_dif_variable(mstate, state, id, 0);
5279 			break;
5280 
5281 		case DIF_OP_STGS:
5282 			id = DIF_INSTR_VAR(instr);
5283 
5284 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
5285 			id -= DIF_VAR_OTHER_UBASE;
5286 
5287 			svar = vstate->dtvs_globals[id];
5288 			ASSERT(svar != NULL);
5289 			v = &svar->dtsv_var;
5290 
5291 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5292 				uintptr_t a = (uintptr_t)svar->dtsv_data;
5293 
5294 				ASSERT(a != 0);
5295 				ASSERT(svar->dtsv_size != 0);
5296 
5297 				if (regs[rd] == 0) {
5298 					*(uint8_t *)a = UINT8_MAX;
5299 					break;
5300 				} else {
5301 					*(uint8_t *)a = 0;
5302 					a += sizeof (uint64_t);
5303 				}
5304 				if (!dtrace_vcanload(
5305 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5306 				    mstate, vstate))
5307 					break;
5308 
5309 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
5310 				    (void *)a, &v->dtdv_type);
5311 				break;
5312 			}
5313 
5314 			svar->dtsv_data = regs[rd];
5315 			break;
5316 
5317 		case DIF_OP_LDTA:
5318 			/*
5319 			 * There are no DTrace built-in thread-local arrays at
5320 			 * present.  This opcode is saved for future work.
5321 			 */
5322 			*flags |= CPU_DTRACE_ILLOP;
5323 			regs[rd] = 0;
5324 			break;
5325 
5326 		case DIF_OP_LDLS:
5327 			id = DIF_INSTR_VAR(instr);
5328 
5329 			if (id < DIF_VAR_OTHER_UBASE) {
5330 				/*
5331 				 * For now, this has no meaning.
5332 				 */
5333 				regs[rd] = 0;
5334 				break;
5335 			}
5336 
5337 			id -= DIF_VAR_OTHER_UBASE;
5338 
5339 			ASSERT(id < vstate->dtvs_nlocals);
5340 			ASSERT(vstate->dtvs_locals != NULL);
5341 
5342 			svar = vstate->dtvs_locals[id];
5343 			ASSERT(svar != NULL);
5344 			v = &svar->dtsv_var;
5345 
5346 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5347 				uintptr_t a = (uintptr_t)svar->dtsv_data;
5348 				size_t sz = v->dtdv_type.dtdt_size;
5349 
5350 				sz += sizeof (uint64_t);
5351 				ASSERT(svar->dtsv_size == NCPU * sz);
5352 				a += curcpu * sz;
5353 
5354 				if (*(uint8_t *)a == UINT8_MAX) {
5355 					/*
5356 					 * If the 0th byte is set to UINT8_MAX
5357 					 * then this is to be treated as a
5358 					 * reference to a NULL variable.
5359 					 */
5360 					regs[rd] = 0;
5361 				} else {
5362 					regs[rd] = a + sizeof (uint64_t);
5363 				}
5364 
5365 				break;
5366 			}
5367 
5368 			ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
5369 			tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
5370 			regs[rd] = tmp[curcpu];
5371 			break;
5372 
5373 		case DIF_OP_STLS:
5374 			id = DIF_INSTR_VAR(instr);
5375 
5376 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
5377 			id -= DIF_VAR_OTHER_UBASE;
5378 			ASSERT(id < vstate->dtvs_nlocals);
5379 
5380 			ASSERT(vstate->dtvs_locals != NULL);
5381 			svar = vstate->dtvs_locals[id];
5382 			ASSERT(svar != NULL);
5383 			v = &svar->dtsv_var;
5384 
5385 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5386 				uintptr_t a = (uintptr_t)svar->dtsv_data;
5387 				size_t sz = v->dtdv_type.dtdt_size;
5388 
5389 				sz += sizeof (uint64_t);
5390 				ASSERT(svar->dtsv_size == NCPU * sz);
5391 				a += curcpu * sz;
5392 
5393 				if (regs[rd] == 0) {
5394 					*(uint8_t *)a = UINT8_MAX;
5395 					break;
5396 				} else {
5397 					*(uint8_t *)a = 0;
5398 					a += sizeof (uint64_t);
5399 				}
5400 
5401 				if (!dtrace_vcanload(
5402 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5403 				    mstate, vstate))
5404 					break;
5405 
5406 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
5407 				    (void *)a, &v->dtdv_type);
5408 				break;
5409 			}
5410 
5411 			ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
5412 			tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
5413 			tmp[curcpu] = regs[rd];
5414 			break;
5415 
5416 		case DIF_OP_LDTS: {
5417 			dtrace_dynvar_t *dvar;
5418 			dtrace_key_t *key;
5419 
5420 			id = DIF_INSTR_VAR(instr);
5421 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
5422 			id -= DIF_VAR_OTHER_UBASE;
5423 			v = &vstate->dtvs_tlocals[id];
5424 
5425 			key = &tupregs[DIF_DTR_NREGS];
5426 			key[0].dttk_value = (uint64_t)id;
5427 			key[0].dttk_size = 0;
5428 			DTRACE_TLS_THRKEY(key[1].dttk_value);
5429 			key[1].dttk_size = 0;
5430 
5431 			dvar = dtrace_dynvar(dstate, 2, key,
5432 			    sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC,
5433 			    mstate, vstate);
5434 
5435 			if (dvar == NULL) {
5436 				regs[rd] = 0;
5437 				break;
5438 			}
5439 
5440 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5441 				regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
5442 			} else {
5443 				regs[rd] = *((uint64_t *)dvar->dtdv_data);
5444 			}
5445 
5446 			break;
5447 		}
5448 
5449 		case DIF_OP_STTS: {
5450 			dtrace_dynvar_t *dvar;
5451 			dtrace_key_t *key;
5452 
5453 			id = DIF_INSTR_VAR(instr);
5454 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
5455 			id -= DIF_VAR_OTHER_UBASE;
5456 
5457 			key = &tupregs[DIF_DTR_NREGS];
5458 			key[0].dttk_value = (uint64_t)id;
5459 			key[0].dttk_size = 0;
5460 			DTRACE_TLS_THRKEY(key[1].dttk_value);
5461 			key[1].dttk_size = 0;
5462 			v = &vstate->dtvs_tlocals[id];
5463 
5464 			dvar = dtrace_dynvar(dstate, 2, key,
5465 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
5466 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
5467 			    regs[rd] ? DTRACE_DYNVAR_ALLOC :
5468 			    DTRACE_DYNVAR_DEALLOC, mstate, vstate);
5469 
5470 			/*
5471 			 * Given that we're storing to thread-local data,
5472 			 * we need to flush our predicate cache.
5473 			 */
5474 			curthread->t_predcache = 0;
5475 
5476 			if (dvar == NULL)
5477 				break;
5478 
5479 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5480 				if (!dtrace_vcanload(
5481 				    (void *)(uintptr_t)regs[rd],
5482 				    &v->dtdv_type, mstate, vstate))
5483 					break;
5484 
5485 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
5486 				    dvar->dtdv_data, &v->dtdv_type);
5487 			} else {
5488 				*((uint64_t *)dvar->dtdv_data) = regs[rd];
5489 			}
5490 
5491 			break;
5492 		}
5493 
5494 		case DIF_OP_SRA:
5495 			regs[rd] = (int64_t)regs[r1] >> regs[r2];
5496 			break;
5497 
5498 		case DIF_OP_CALL:
5499 			dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd,
5500 			    regs, tupregs, ttop, mstate, state);
5501 			break;
5502 
5503 		case DIF_OP_PUSHTR:
5504 			if (ttop == DIF_DTR_NREGS) {
5505 				*flags |= CPU_DTRACE_TUPOFLOW;
5506 				break;
5507 			}
5508 
5509 			if (r1 == DIF_TYPE_STRING) {
5510 				/*
5511 				 * If this is a string type and the size is 0,
5512 				 * we'll use the system-wide default string
5513 				 * size.  Note that we are _not_ looking at
5514 				 * the value of the DTRACEOPT_STRSIZE option;
5515 				 * had this been set, we would expect to have
5516 				 * a non-zero size value in the "pushtr".
5517 				 */
5518 				tupregs[ttop].dttk_size =
5519 				    dtrace_strlen((char *)(uintptr_t)regs[rd],
5520 				    regs[r2] ? regs[r2] :
5521 				    dtrace_strsize_default) + 1;
5522 			} else {
5523 				tupregs[ttop].dttk_size = regs[r2];
5524 			}
5525 
5526 			tupregs[ttop++].dttk_value = regs[rd];
5527 			break;
5528 
5529 		case DIF_OP_PUSHTV:
5530 			if (ttop == DIF_DTR_NREGS) {
5531 				*flags |= CPU_DTRACE_TUPOFLOW;
5532 				break;
5533 			}
5534 
5535 			tupregs[ttop].dttk_value = regs[rd];
5536 			tupregs[ttop++].dttk_size = 0;
5537 			break;
5538 
5539 		case DIF_OP_POPTS:
5540 			if (ttop != 0)
5541 				ttop--;
5542 			break;
5543 
5544 		case DIF_OP_FLUSHTS:
5545 			ttop = 0;
5546 			break;
5547 
5548 		case DIF_OP_LDGAA:
5549 		case DIF_OP_LDTAA: {
5550 			dtrace_dynvar_t *dvar;
5551 			dtrace_key_t *key = tupregs;
5552 			uint_t nkeys = ttop;
5553 
5554 			id = DIF_INSTR_VAR(instr);
5555 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
5556 			id -= DIF_VAR_OTHER_UBASE;
5557 
5558 			key[nkeys].dttk_value = (uint64_t)id;
5559 			key[nkeys++].dttk_size = 0;
5560 
5561 			if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) {
5562 				DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
5563 				key[nkeys++].dttk_size = 0;
5564 				v = &vstate->dtvs_tlocals[id];
5565 			} else {
5566 				v = &vstate->dtvs_globals[id]->dtsv_var;
5567 			}
5568 
5569 			dvar = dtrace_dynvar(dstate, nkeys, key,
5570 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
5571 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
5572 			    DTRACE_DYNVAR_NOALLOC, mstate, vstate);
5573 
5574 			if (dvar == NULL) {
5575 				regs[rd] = 0;
5576 				break;
5577 			}
5578 
5579 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5580 				regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
5581 			} else {
5582 				regs[rd] = *((uint64_t *)dvar->dtdv_data);
5583 			}
5584 
5585 			break;
5586 		}
5587 
5588 		case DIF_OP_STGAA:
5589 		case DIF_OP_STTAA: {
5590 			dtrace_dynvar_t *dvar;
5591 			dtrace_key_t *key = tupregs;
5592 			uint_t nkeys = ttop;
5593 
5594 			id = DIF_INSTR_VAR(instr);
5595 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
5596 			id -= DIF_VAR_OTHER_UBASE;
5597 
5598 			key[nkeys].dttk_value = (uint64_t)id;
5599 			key[nkeys++].dttk_size = 0;
5600 
5601 			if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) {
5602 				DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
5603 				key[nkeys++].dttk_size = 0;
5604 				v = &vstate->dtvs_tlocals[id];
5605 			} else {
5606 				v = &vstate->dtvs_globals[id]->dtsv_var;
5607 			}
5608 
5609 			dvar = dtrace_dynvar(dstate, nkeys, key,
5610 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
5611 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
5612 			    regs[rd] ? DTRACE_DYNVAR_ALLOC :
5613 			    DTRACE_DYNVAR_DEALLOC, mstate, vstate);
5614 
5615 			if (dvar == NULL)
5616 				break;
5617 
5618 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5619 				if (!dtrace_vcanload(
5620 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5621 				    mstate, vstate))
5622 					break;
5623 
5624 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
5625 				    dvar->dtdv_data, &v->dtdv_type);
5626 			} else {
5627 				*((uint64_t *)dvar->dtdv_data) = regs[rd];
5628 			}
5629 
5630 			break;
5631 		}
5632 
5633 		case DIF_OP_ALLOCS: {
5634 			uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
5635 			size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1];
5636 
5637 			/*
5638 			 * Rounding up the user allocation size could have
5639 			 * overflowed large, bogus allocations (like -1ULL) to
5640 			 * 0.
5641 			 */
5642 			if (size < regs[r1] ||
5643 			    !DTRACE_INSCRATCH(mstate, size)) {
5644 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5645 				regs[rd] = 0;
5646 				break;
5647 			}
5648 
5649 			dtrace_bzero((void *) mstate->dtms_scratch_ptr, size);
5650 			mstate->dtms_scratch_ptr += size;
5651 			regs[rd] = ptr;
5652 			break;
5653 		}
5654 
5655 		case DIF_OP_COPYS:
5656 			if (!dtrace_canstore(regs[rd], regs[r2],
5657 			    mstate, vstate)) {
5658 				*flags |= CPU_DTRACE_BADADDR;
5659 				*illval = regs[rd];
5660 				break;
5661 			}
5662 
5663 			if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate))
5664 				break;
5665 
5666 			dtrace_bcopy((void *)(uintptr_t)regs[r1],
5667 			    (void *)(uintptr_t)regs[rd], (size_t)regs[r2]);
5668 			break;
5669 
5670 		case DIF_OP_STB:
5671 			if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) {
5672 				*flags |= CPU_DTRACE_BADADDR;
5673 				*illval = regs[rd];
5674 				break;
5675 			}
5676 			*((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1];
5677 			break;
5678 
5679 		case DIF_OP_STH:
5680 			if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) {
5681 				*flags |= CPU_DTRACE_BADADDR;
5682 				*illval = regs[rd];
5683 				break;
5684 			}
5685 			if (regs[rd] & 1) {
5686 				*flags |= CPU_DTRACE_BADALIGN;
5687 				*illval = regs[rd];
5688 				break;
5689 			}
5690 			*((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1];
5691 			break;
5692 
5693 		case DIF_OP_STW:
5694 			if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) {
5695 				*flags |= CPU_DTRACE_BADADDR;
5696 				*illval = regs[rd];
5697 				break;
5698 			}
5699 			if (regs[rd] & 3) {
5700 				*flags |= CPU_DTRACE_BADALIGN;
5701 				*illval = regs[rd];
5702 				break;
5703 			}
5704 			*((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1];
5705 			break;
5706 
5707 		case DIF_OP_STX:
5708 			if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) {
5709 				*flags |= CPU_DTRACE_BADADDR;
5710 				*illval = regs[rd];
5711 				break;
5712 			}
5713 			if (regs[rd] & 7) {
5714 				*flags |= CPU_DTRACE_BADALIGN;
5715 				*illval = regs[rd];
5716 				break;
5717 			}
5718 			*((uint64_t *)(uintptr_t)regs[rd]) = regs[r1];
5719 			break;
5720 		}
5721 	}
5722 
5723 	if (!(*flags & CPU_DTRACE_FAULT))
5724 		return (rval);
5725 
5726 	mstate->dtms_fltoffs = opc * sizeof (dif_instr_t);
5727 	mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS;
5728 
5729 	return (0);
5730 }
5731 
5732 static void
5733 dtrace_action_breakpoint(dtrace_ecb_t *ecb)
5734 {
5735 	dtrace_probe_t *probe = ecb->dte_probe;
5736 	dtrace_provider_t *prov = probe->dtpr_provider;
5737 	char c[DTRACE_FULLNAMELEN + 80], *str;
5738 	char *msg = "dtrace: breakpoint action at probe ";
5739 	char *ecbmsg = " (ecb ";
5740 	uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4));
5741 	uintptr_t val = (uintptr_t)ecb;
5742 	int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0;
5743 
5744 	if (dtrace_destructive_disallow)
5745 		return;
5746 
5747 	/*
5748 	 * It's impossible to be taking action on the NULL probe.
5749 	 */
5750 	ASSERT(probe != NULL);
5751 
5752 	/*
5753 	 * This is a poor man's (destitute man's?) sprintf():  we want to
5754 	 * print the provider name, module name, function name and name of
5755 	 * the probe, along with the hex address of the ECB with the breakpoint
5756 	 * action -- all of which we must place in the character buffer by
5757 	 * hand.
5758 	 */
5759 	while (*msg != '\0')
5760 		c[i++] = *msg++;
5761 
5762 	for (str = prov->dtpv_name; *str != '\0'; str++)
5763 		c[i++] = *str;
5764 	c[i++] = ':';
5765 
5766 	for (str = probe->dtpr_mod; *str != '\0'; str++)
5767 		c[i++] = *str;
5768 	c[i++] = ':';
5769 
5770 	for (str = probe->dtpr_func; *str != '\0'; str++)
5771 		c[i++] = *str;
5772 	c[i++] = ':';
5773 
5774 	for (str = probe->dtpr_name; *str != '\0'; str++)
5775 		c[i++] = *str;
5776 
5777 	while (*ecbmsg != '\0')
5778 		c[i++] = *ecbmsg++;
5779 
5780 	while (shift >= 0) {
5781 		mask = (uintptr_t)0xf << shift;
5782 
5783 		if (val >= ((uintptr_t)1 << shift))
5784 			c[i++] = "0123456789abcdef"[(val & mask) >> shift];
5785 		shift -= 4;
5786 	}
5787 
5788 	c[i++] = ')';
5789 	c[i] = '\0';
5790 
5791 #if defined(sun)
5792 	debug_enter(c);
5793 #else
5794 	kdb_enter(KDB_WHY_DTRACE, "breakpoint action");
5795 #endif
5796 }
5797 
5798 static void
5799 dtrace_action_panic(dtrace_ecb_t *ecb)
5800 {
5801 	dtrace_probe_t *probe = ecb->dte_probe;
5802 
5803 	/*
5804 	 * It's impossible to be taking action on the NULL probe.
5805 	 */
5806 	ASSERT(probe != NULL);
5807 
5808 	if (dtrace_destructive_disallow)
5809 		return;
5810 
5811 	if (dtrace_panicked != NULL)
5812 		return;
5813 
5814 	if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL)
5815 		return;
5816 
5817 	/*
5818 	 * We won the right to panic.  (We want to be sure that only one
5819 	 * thread calls panic() from dtrace_probe(), and that panic() is
5820 	 * called exactly once.)
5821 	 */
5822 	dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
5823 	    probe->dtpr_provider->dtpv_name, probe->dtpr_mod,
5824 	    probe->dtpr_func, probe->dtpr_name, (void *)ecb);
5825 }
5826 
5827 static void
5828 dtrace_action_raise(uint64_t sig)
5829 {
5830 	if (dtrace_destructive_disallow)
5831 		return;
5832 
5833 	if (sig >= NSIG) {
5834 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5835 		return;
5836 	}
5837 
5838 #if defined(sun)
5839 	/*
5840 	 * raise() has a queue depth of 1 -- we ignore all subsequent
5841 	 * invocations of the raise() action.
5842 	 */
5843 	if (curthread->t_dtrace_sig == 0)
5844 		curthread->t_dtrace_sig = (uint8_t)sig;
5845 
5846 	curthread->t_sig_check = 1;
5847 	aston(curthread);
5848 #else
5849 	struct proc *p = curproc;
5850 	PROC_LOCK(p);
5851 	kern_psignal(p, sig);
5852 	PROC_UNLOCK(p);
5853 #endif
5854 }
5855 
5856 static void
5857 dtrace_action_stop(void)
5858 {
5859 	if (dtrace_destructive_disallow)
5860 		return;
5861 
5862 #if defined(sun)
5863 	if (!curthread->t_dtrace_stop) {
5864 		curthread->t_dtrace_stop = 1;
5865 		curthread->t_sig_check = 1;
5866 		aston(curthread);
5867 	}
5868 #else
5869 	struct proc *p = curproc;
5870 	PROC_LOCK(p);
5871 	kern_psignal(p, SIGSTOP);
5872 	PROC_UNLOCK(p);
5873 #endif
5874 }
5875 
5876 static void
5877 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val)
5878 {
5879 	hrtime_t now;
5880 	volatile uint16_t *flags;
5881 #if defined(sun)
5882 	cpu_t *cpu = CPU;
5883 #else
5884 	cpu_t *cpu = &solaris_cpu[curcpu];
5885 #endif
5886 
5887 	if (dtrace_destructive_disallow)
5888 		return;
5889 
5890 	flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags;
5891 
5892 	now = dtrace_gethrtime();
5893 
5894 	if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) {
5895 		/*
5896 		 * We need to advance the mark to the current time.
5897 		 */
5898 		cpu->cpu_dtrace_chillmark = now;
5899 		cpu->cpu_dtrace_chilled = 0;
5900 	}
5901 
5902 	/*
5903 	 * Now check to see if the requested chill time would take us over
5904 	 * the maximum amount of time allowed in the chill interval.  (Or
5905 	 * worse, if the calculation itself induces overflow.)
5906 	 */
5907 	if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max ||
5908 	    cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) {
5909 		*flags |= CPU_DTRACE_ILLOP;
5910 		return;
5911 	}
5912 
5913 	while (dtrace_gethrtime() - now < val)
5914 		continue;
5915 
5916 	/*
5917 	 * Normally, we assure that the value of the variable "timestamp" does
5918 	 * not change within an ECB.  The presence of chill() represents an
5919 	 * exception to this rule, however.
5920 	 */
5921 	mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP;
5922 	cpu->cpu_dtrace_chilled += val;
5923 }
5924 
5925 static void
5926 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state,
5927     uint64_t *buf, uint64_t arg)
5928 {
5929 	int nframes = DTRACE_USTACK_NFRAMES(arg);
5930 	int strsize = DTRACE_USTACK_STRSIZE(arg);
5931 	uint64_t *pcs = &buf[1], *fps;
5932 	char *str = (char *)&pcs[nframes];
5933 	int size, offs = 0, i, j;
5934 	uintptr_t old = mstate->dtms_scratch_ptr, saved;
5935 	uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
5936 	char *sym;
5937 
5938 	/*
5939 	 * Should be taking a faster path if string space has not been
5940 	 * allocated.
5941 	 */
5942 	ASSERT(strsize != 0);
5943 
5944 	/*
5945 	 * We will first allocate some temporary space for the frame pointers.
5946 	 */
5947 	fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
5948 	size = (uintptr_t)fps - mstate->dtms_scratch_ptr +
5949 	    (nframes * sizeof (uint64_t));
5950 
5951 	if (!DTRACE_INSCRATCH(mstate, size)) {
5952 		/*
5953 		 * Not enough room for our frame pointers -- need to indicate
5954 		 * that we ran out of scratch space.
5955 		 */
5956 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5957 		return;
5958 	}
5959 
5960 	mstate->dtms_scratch_ptr += size;
5961 	saved = mstate->dtms_scratch_ptr;
5962 
5963 	/*
5964 	 * Now get a stack with both program counters and frame pointers.
5965 	 */
5966 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5967 	dtrace_getufpstack(buf, fps, nframes + 1);
5968 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5969 
5970 	/*
5971 	 * If that faulted, we're cooked.
5972 	 */
5973 	if (*flags & CPU_DTRACE_FAULT)
5974 		goto out;
5975 
5976 	/*
5977 	 * Now we want to walk up the stack, calling the USTACK helper.  For
5978 	 * each iteration, we restore the scratch pointer.
5979 	 */
5980 	for (i = 0; i < nframes; i++) {
5981 		mstate->dtms_scratch_ptr = saved;
5982 
5983 		if (offs >= strsize)
5984 			break;
5985 
5986 		sym = (char *)(uintptr_t)dtrace_helper(
5987 		    DTRACE_HELPER_ACTION_USTACK,
5988 		    mstate, state, pcs[i], fps[i]);
5989 
5990 		/*
5991 		 * If we faulted while running the helper, we're going to
5992 		 * clear the fault and null out the corresponding string.
5993 		 */
5994 		if (*flags & CPU_DTRACE_FAULT) {
5995 			*flags &= ~CPU_DTRACE_FAULT;
5996 			str[offs++] = '\0';
5997 			continue;
5998 		}
5999 
6000 		if (sym == NULL) {
6001 			str[offs++] = '\0';
6002 			continue;
6003 		}
6004 
6005 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6006 
6007 		/*
6008 		 * Now copy in the string that the helper returned to us.
6009 		 */
6010 		for (j = 0; offs + j < strsize; j++) {
6011 			if ((str[offs + j] = sym[j]) == '\0')
6012 				break;
6013 		}
6014 
6015 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6016 
6017 		offs += j + 1;
6018 	}
6019 
6020 	if (offs >= strsize) {
6021 		/*
6022 		 * If we didn't have room for all of the strings, we don't
6023 		 * abort processing -- this needn't be a fatal error -- but we
6024 		 * still want to increment a counter (dts_stkstroverflows) to
6025 		 * allow this condition to be warned about.  (If this is from
6026 		 * a jstack() action, it is easily tuned via jstackstrsize.)
6027 		 */
6028 		dtrace_error(&state->dts_stkstroverflows);
6029 	}
6030 
6031 	while (offs < strsize)
6032 		str[offs++] = '\0';
6033 
6034 out:
6035 	mstate->dtms_scratch_ptr = old;
6036 }
6037 
6038 /*
6039  * If you're looking for the epicenter of DTrace, you just found it.  This
6040  * is the function called by the provider to fire a probe -- from which all
6041  * subsequent probe-context DTrace activity emanates.
6042  */
6043 void
6044 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
6045     uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
6046 {
6047 	processorid_t cpuid;
6048 	dtrace_icookie_t cookie;
6049 	dtrace_probe_t *probe;
6050 	dtrace_mstate_t mstate;
6051 	dtrace_ecb_t *ecb;
6052 	dtrace_action_t *act;
6053 	intptr_t offs;
6054 	size_t size;
6055 	int vtime, onintr;
6056 	volatile uint16_t *flags;
6057 	hrtime_t now;
6058 
6059 	if (panicstr != NULL)
6060 		return;
6061 
6062 #if defined(sun)
6063 	/*
6064 	 * Kick out immediately if this CPU is still being born (in which case
6065 	 * curthread will be set to -1) or the current thread can't allow
6066 	 * probes in its current context.
6067 	 */
6068 	if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE))
6069 		return;
6070 #endif
6071 
6072 	cookie = dtrace_interrupt_disable();
6073 	probe = dtrace_probes[id - 1];
6074 	cpuid = curcpu;
6075 	onintr = CPU_ON_INTR(CPU);
6076 
6077 	if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE &&
6078 	    probe->dtpr_predcache == curthread->t_predcache) {
6079 		/*
6080 		 * We have hit in the predicate cache; we know that
6081 		 * this predicate would evaluate to be false.
6082 		 */
6083 		dtrace_interrupt_enable(cookie);
6084 		return;
6085 	}
6086 
6087 #if defined(sun)
6088 	if (panic_quiesce) {
6089 #else
6090 	if (panicstr != NULL) {
6091 #endif
6092 		/*
6093 		 * We don't trace anything if we're panicking.
6094 		 */
6095 		dtrace_interrupt_enable(cookie);
6096 		return;
6097 	}
6098 
6099 	now = dtrace_gethrtime();
6100 	vtime = dtrace_vtime_references != 0;
6101 
6102 	if (vtime && curthread->t_dtrace_start)
6103 		curthread->t_dtrace_vtime += now - curthread->t_dtrace_start;
6104 
6105 	mstate.dtms_difo = NULL;
6106 	mstate.dtms_probe = probe;
6107 	mstate.dtms_strtok = 0;
6108 	mstate.dtms_arg[0] = arg0;
6109 	mstate.dtms_arg[1] = arg1;
6110 	mstate.dtms_arg[2] = arg2;
6111 	mstate.dtms_arg[3] = arg3;
6112 	mstate.dtms_arg[4] = arg4;
6113 
6114 	flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags;
6115 
6116 	for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
6117 		dtrace_predicate_t *pred = ecb->dte_predicate;
6118 		dtrace_state_t *state = ecb->dte_state;
6119 		dtrace_buffer_t *buf = &state->dts_buffer[cpuid];
6120 		dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid];
6121 		dtrace_vstate_t *vstate = &state->dts_vstate;
6122 		dtrace_provider_t *prov = probe->dtpr_provider;
6123 		uint64_t tracememsize = 0;
6124 		int committed = 0;
6125 		caddr_t tomax;
6126 
6127 		/*
6128 		 * A little subtlety with the following (seemingly innocuous)
6129 		 * declaration of the automatic 'val':  by looking at the
6130 		 * code, you might think that it could be declared in the
6131 		 * action processing loop, below.  (That is, it's only used in
6132 		 * the action processing loop.)  However, it must be declared
6133 		 * out of that scope because in the case of DIF expression
6134 		 * arguments to aggregating actions, one iteration of the
6135 		 * action loop will use the last iteration's value.
6136 		 */
6137 		uint64_t val = 0;
6138 
6139 		mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE;
6140 		*flags &= ~CPU_DTRACE_ERROR;
6141 
6142 		if (prov == dtrace_provider) {
6143 			/*
6144 			 * If dtrace itself is the provider of this probe,
6145 			 * we're only going to continue processing the ECB if
6146 			 * arg0 (the dtrace_state_t) is equal to the ECB's
6147 			 * creating state.  (This prevents disjoint consumers
6148 			 * from seeing one another's metaprobes.)
6149 			 */
6150 			if (arg0 != (uint64_t)(uintptr_t)state)
6151 				continue;
6152 		}
6153 
6154 		if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) {
6155 			/*
6156 			 * We're not currently active.  If our provider isn't
6157 			 * the dtrace pseudo provider, we're not interested.
6158 			 */
6159 			if (prov != dtrace_provider)
6160 				continue;
6161 
6162 			/*
6163 			 * Now we must further check if we are in the BEGIN
6164 			 * probe.  If we are, we will only continue processing
6165 			 * if we're still in WARMUP -- if one BEGIN enabling
6166 			 * has invoked the exit() action, we don't want to
6167 			 * evaluate subsequent BEGIN enablings.
6168 			 */
6169 			if (probe->dtpr_id == dtrace_probeid_begin &&
6170 			    state->dts_activity != DTRACE_ACTIVITY_WARMUP) {
6171 				ASSERT(state->dts_activity ==
6172 				    DTRACE_ACTIVITY_DRAINING);
6173 				continue;
6174 			}
6175 		}
6176 
6177 		if (ecb->dte_cond) {
6178 			/*
6179 			 * If the dte_cond bits indicate that this
6180 			 * consumer is only allowed to see user-mode firings
6181 			 * of this probe, call the provider's dtps_usermode()
6182 			 * entry point to check that the probe was fired
6183 			 * while in a user context. Skip this ECB if that's
6184 			 * not the case.
6185 			 */
6186 			if ((ecb->dte_cond & DTRACE_COND_USERMODE) &&
6187 			    prov->dtpv_pops.dtps_usermode(prov->dtpv_arg,
6188 			    probe->dtpr_id, probe->dtpr_arg) == 0)
6189 				continue;
6190 
6191 #if defined(sun)
6192 			/*
6193 			 * This is more subtle than it looks. We have to be
6194 			 * absolutely certain that CRED() isn't going to
6195 			 * change out from under us so it's only legit to
6196 			 * examine that structure if we're in constrained
6197 			 * situations. Currently, the only times we'll this
6198 			 * check is if a non-super-user has enabled the
6199 			 * profile or syscall providers -- providers that
6200 			 * allow visibility of all processes. For the
6201 			 * profile case, the check above will ensure that
6202 			 * we're examining a user context.
6203 			 */
6204 			if (ecb->dte_cond & DTRACE_COND_OWNER) {
6205 				cred_t *cr;
6206 				cred_t *s_cr =
6207 				    ecb->dte_state->dts_cred.dcr_cred;
6208 				proc_t *proc;
6209 
6210 				ASSERT(s_cr != NULL);
6211 
6212 				if ((cr = CRED()) == NULL ||
6213 				    s_cr->cr_uid != cr->cr_uid ||
6214 				    s_cr->cr_uid != cr->cr_ruid ||
6215 				    s_cr->cr_uid != cr->cr_suid ||
6216 				    s_cr->cr_gid != cr->cr_gid ||
6217 				    s_cr->cr_gid != cr->cr_rgid ||
6218 				    s_cr->cr_gid != cr->cr_sgid ||
6219 				    (proc = ttoproc(curthread)) == NULL ||
6220 				    (proc->p_flag & SNOCD))
6221 					continue;
6222 			}
6223 
6224 			if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
6225 				cred_t *cr;
6226 				cred_t *s_cr =
6227 				    ecb->dte_state->dts_cred.dcr_cred;
6228 
6229 				ASSERT(s_cr != NULL);
6230 
6231 				if ((cr = CRED()) == NULL ||
6232 				    s_cr->cr_zone->zone_id !=
6233 				    cr->cr_zone->zone_id)
6234 					continue;
6235 			}
6236 #endif
6237 		}
6238 
6239 		if (now - state->dts_alive > dtrace_deadman_timeout) {
6240 			/*
6241 			 * We seem to be dead.  Unless we (a) have kernel
6242 			 * destructive permissions (b) have explicitly enabled
6243 			 * destructive actions and (c) destructive actions have
6244 			 * not been disabled, we're going to transition into
6245 			 * the KILLED state, from which no further processing
6246 			 * on this state will be performed.
6247 			 */
6248 			if (!dtrace_priv_kernel_destructive(state) ||
6249 			    !state->dts_cred.dcr_destructive ||
6250 			    dtrace_destructive_disallow) {
6251 				void *activity = &state->dts_activity;
6252 				dtrace_activity_t current;
6253 
6254 				do {
6255 					current = state->dts_activity;
6256 				} while (dtrace_cas32(activity, current,
6257 				    DTRACE_ACTIVITY_KILLED) != current);
6258 
6259 				continue;
6260 			}
6261 		}
6262 
6263 		if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed,
6264 		    ecb->dte_alignment, state, &mstate)) < 0)
6265 			continue;
6266 
6267 		tomax = buf->dtb_tomax;
6268 		ASSERT(tomax != NULL);
6269 
6270 		if (ecb->dte_size != 0) {
6271 			dtrace_rechdr_t dtrh;
6272 			if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
6273 				mstate.dtms_timestamp = dtrace_gethrtime();
6274 				mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP;
6275 			}
6276 			ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t));
6277 			dtrh.dtrh_epid = ecb->dte_epid;
6278 			DTRACE_RECORD_STORE_TIMESTAMP(&dtrh,
6279 			    mstate.dtms_timestamp);
6280 			*((dtrace_rechdr_t *)(tomax + offs)) = dtrh;
6281 		}
6282 
6283 		mstate.dtms_epid = ecb->dte_epid;
6284 		mstate.dtms_present |= DTRACE_MSTATE_EPID;
6285 
6286 		if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)
6287 			mstate.dtms_access = DTRACE_ACCESS_KERNEL;
6288 		else
6289 			mstate.dtms_access = 0;
6290 
6291 		if (pred != NULL) {
6292 			dtrace_difo_t *dp = pred->dtp_difo;
6293 			int rval;
6294 
6295 			rval = dtrace_dif_emulate(dp, &mstate, vstate, state);
6296 
6297 			if (!(*flags & CPU_DTRACE_ERROR) && !rval) {
6298 				dtrace_cacheid_t cid = probe->dtpr_predcache;
6299 
6300 				if (cid != DTRACE_CACHEIDNONE && !onintr) {
6301 					/*
6302 					 * Update the predicate cache...
6303 					 */
6304 					ASSERT(cid == pred->dtp_cacheid);
6305 					curthread->t_predcache = cid;
6306 				}
6307 
6308 				continue;
6309 			}
6310 		}
6311 
6312 		for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) &&
6313 		    act != NULL; act = act->dta_next) {
6314 			size_t valoffs;
6315 			dtrace_difo_t *dp;
6316 			dtrace_recdesc_t *rec = &act->dta_rec;
6317 
6318 			size = rec->dtrd_size;
6319 			valoffs = offs + rec->dtrd_offset;
6320 
6321 			if (DTRACEACT_ISAGG(act->dta_kind)) {
6322 				uint64_t v = 0xbad;
6323 				dtrace_aggregation_t *agg;
6324 
6325 				agg = (dtrace_aggregation_t *)act;
6326 
6327 				if ((dp = act->dta_difo) != NULL)
6328 					v = dtrace_dif_emulate(dp,
6329 					    &mstate, vstate, state);
6330 
6331 				if (*flags & CPU_DTRACE_ERROR)
6332 					continue;
6333 
6334 				/*
6335 				 * Note that we always pass the expression
6336 				 * value from the previous iteration of the
6337 				 * action loop.  This value will only be used
6338 				 * if there is an expression argument to the
6339 				 * aggregating action, denoted by the
6340 				 * dtag_hasarg field.
6341 				 */
6342 				dtrace_aggregate(agg, buf,
6343 				    offs, aggbuf, v, val);
6344 				continue;
6345 			}
6346 
6347 			switch (act->dta_kind) {
6348 			case DTRACEACT_STOP:
6349 				if (dtrace_priv_proc_destructive(state))
6350 					dtrace_action_stop();
6351 				continue;
6352 
6353 			case DTRACEACT_BREAKPOINT:
6354 				if (dtrace_priv_kernel_destructive(state))
6355 					dtrace_action_breakpoint(ecb);
6356 				continue;
6357 
6358 			case DTRACEACT_PANIC:
6359 				if (dtrace_priv_kernel_destructive(state))
6360 					dtrace_action_panic(ecb);
6361 				continue;
6362 
6363 			case DTRACEACT_STACK:
6364 				if (!dtrace_priv_kernel(state))
6365 					continue;
6366 
6367 				dtrace_getpcstack((pc_t *)(tomax + valoffs),
6368 				    size / sizeof (pc_t), probe->dtpr_aframes,
6369 				    DTRACE_ANCHORED(probe) ? NULL :
6370 				    (uint32_t *)arg0);
6371 				continue;
6372 
6373 			case DTRACEACT_JSTACK:
6374 			case DTRACEACT_USTACK:
6375 				if (!dtrace_priv_proc(state))
6376 					continue;
6377 
6378 				/*
6379 				 * See comment in DIF_VAR_PID.
6380 				 */
6381 				if (DTRACE_ANCHORED(mstate.dtms_probe) &&
6382 				    CPU_ON_INTR(CPU)) {
6383 					int depth = DTRACE_USTACK_NFRAMES(
6384 					    rec->dtrd_arg) + 1;
6385 
6386 					dtrace_bzero((void *)(tomax + valoffs),
6387 					    DTRACE_USTACK_STRSIZE(rec->dtrd_arg)
6388 					    + depth * sizeof (uint64_t));
6389 
6390 					continue;
6391 				}
6392 
6393 				if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 &&
6394 				    curproc->p_dtrace_helpers != NULL) {
6395 					/*
6396 					 * This is the slow path -- we have
6397 					 * allocated string space, and we're
6398 					 * getting the stack of a process that
6399 					 * has helpers.  Call into a separate
6400 					 * routine to perform this processing.
6401 					 */
6402 					dtrace_action_ustack(&mstate, state,
6403 					    (uint64_t *)(tomax + valoffs),
6404 					    rec->dtrd_arg);
6405 					continue;
6406 				}
6407 
6408 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6409 				dtrace_getupcstack((uint64_t *)
6410 				    (tomax + valoffs),
6411 				    DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1);
6412 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6413 				continue;
6414 
6415 			default:
6416 				break;
6417 			}
6418 
6419 			dp = act->dta_difo;
6420 			ASSERT(dp != NULL);
6421 
6422 			val = dtrace_dif_emulate(dp, &mstate, vstate, state);
6423 
6424 			if (*flags & CPU_DTRACE_ERROR)
6425 				continue;
6426 
6427 			switch (act->dta_kind) {
6428 			case DTRACEACT_SPECULATE: {
6429 				dtrace_rechdr_t *dtrh;
6430 
6431 				ASSERT(buf == &state->dts_buffer[cpuid]);
6432 				buf = dtrace_speculation_buffer(state,
6433 				    cpuid, val);
6434 
6435 				if (buf == NULL) {
6436 					*flags |= CPU_DTRACE_DROP;
6437 					continue;
6438 				}
6439 
6440 				offs = dtrace_buffer_reserve(buf,
6441 				    ecb->dte_needed, ecb->dte_alignment,
6442 				    state, NULL);
6443 
6444 				if (offs < 0) {
6445 					*flags |= CPU_DTRACE_DROP;
6446 					continue;
6447 				}
6448 
6449 				tomax = buf->dtb_tomax;
6450 				ASSERT(tomax != NULL);
6451 
6452 				if (ecb->dte_size == 0)
6453 					continue;
6454 
6455 				ASSERT3U(ecb->dte_size, >=,
6456 				    sizeof (dtrace_rechdr_t));
6457 				dtrh = ((void *)(tomax + offs));
6458 				dtrh->dtrh_epid = ecb->dte_epid;
6459 				/*
6460 				 * When the speculation is committed, all of
6461 				 * the records in the speculative buffer will
6462 				 * have their timestamps set to the commit
6463 				 * time.  Until then, it is set to a sentinel
6464 				 * value, for debugability.
6465 				 */
6466 				DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX);
6467 				continue;
6468 			}
6469 
6470 			case DTRACEACT_PRINTM: {
6471 				/* The DIF returns a 'memref'. */
6472 				uintptr_t *memref = (uintptr_t *)(uintptr_t) val;
6473 
6474 				/* Get the size from the memref. */
6475 				size = memref[1];
6476 
6477 				/*
6478 				 * Check if the size exceeds the allocated
6479 				 * buffer size.
6480 				 */
6481 				if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) {
6482 					/* Flag a drop! */
6483 					*flags |= CPU_DTRACE_DROP;
6484 					continue;
6485 				}
6486 
6487 				/* Store the size in the buffer first. */
6488 				DTRACE_STORE(uintptr_t, tomax,
6489 				    valoffs, size);
6490 
6491 				/*
6492 				 * Offset the buffer address to the start
6493 				 * of the data.
6494 				 */
6495 				valoffs += sizeof(uintptr_t);
6496 
6497 				/*
6498 				 * Reset to the memory address rather than
6499 				 * the memref array, then let the BYREF
6500 				 * code below do the work to store the
6501 				 * memory data in the buffer.
6502 				 */
6503 				val = memref[0];
6504 				break;
6505 			}
6506 
6507 			case DTRACEACT_PRINTT: {
6508 				/* The DIF returns a 'typeref'. */
6509 				uintptr_t *typeref = (uintptr_t *)(uintptr_t) val;
6510 				char c = '\0' + 1;
6511 				size_t s;
6512 
6513 				/*
6514 				 * Get the type string length and round it
6515 				 * up so that the data that follows is
6516 				 * aligned for easy access.
6517 				 */
6518 				size_t typs = strlen((char *) typeref[2]) + 1;
6519 				typs = roundup(typs,  sizeof(uintptr_t));
6520 
6521 				/*
6522 				 *Get the size from the typeref using the
6523 				 * number of elements and the type size.
6524 				 */
6525 				size = typeref[1] * typeref[3];
6526 
6527 				/*
6528 				 * Check if the size exceeds the allocated
6529 				 * buffer size.
6530 				 */
6531 				if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) {
6532 					/* Flag a drop! */
6533 					*flags |= CPU_DTRACE_DROP;
6534 
6535 				}
6536 
6537 				/* Store the size in the buffer first. */
6538 				DTRACE_STORE(uintptr_t, tomax,
6539 				    valoffs, size);
6540 				valoffs += sizeof(uintptr_t);
6541 
6542 				/* Store the type size in the buffer. */
6543 				DTRACE_STORE(uintptr_t, tomax,
6544 				    valoffs, typeref[3]);
6545 				valoffs += sizeof(uintptr_t);
6546 
6547 				val = typeref[2];
6548 
6549 				for (s = 0; s < typs; s++) {
6550 					if (c != '\0')
6551 						c = dtrace_load8(val++);
6552 
6553 					DTRACE_STORE(uint8_t, tomax,
6554 					    valoffs++, c);
6555 				}
6556 
6557 				/*
6558 				 * Reset to the memory address rather than
6559 				 * the typeref array, then let the BYREF
6560 				 * code below do the work to store the
6561 				 * memory data in the buffer.
6562 				 */
6563 				val = typeref[0];
6564 				break;
6565 			}
6566 
6567 			case DTRACEACT_CHILL:
6568 				if (dtrace_priv_kernel_destructive(state))
6569 					dtrace_action_chill(&mstate, val);
6570 				continue;
6571 
6572 			case DTRACEACT_RAISE:
6573 				if (dtrace_priv_proc_destructive(state))
6574 					dtrace_action_raise(val);
6575 				continue;
6576 
6577 			case DTRACEACT_COMMIT:
6578 				ASSERT(!committed);
6579 
6580 				/*
6581 				 * We need to commit our buffer state.
6582 				 */
6583 				if (ecb->dte_size)
6584 					buf->dtb_offset = offs + ecb->dte_size;
6585 				buf = &state->dts_buffer[cpuid];
6586 				dtrace_speculation_commit(state, cpuid, val);
6587 				committed = 1;
6588 				continue;
6589 
6590 			case DTRACEACT_DISCARD:
6591 				dtrace_speculation_discard(state, cpuid, val);
6592 				continue;
6593 
6594 			case DTRACEACT_DIFEXPR:
6595 			case DTRACEACT_LIBACT:
6596 			case DTRACEACT_PRINTF:
6597 			case DTRACEACT_PRINTA:
6598 			case DTRACEACT_SYSTEM:
6599 			case DTRACEACT_FREOPEN:
6600 			case DTRACEACT_TRACEMEM:
6601 				break;
6602 
6603 			case DTRACEACT_TRACEMEM_DYNSIZE:
6604 				tracememsize = val;
6605 				break;
6606 
6607 			case DTRACEACT_SYM:
6608 			case DTRACEACT_MOD:
6609 				if (!dtrace_priv_kernel(state))
6610 					continue;
6611 				break;
6612 
6613 			case DTRACEACT_USYM:
6614 			case DTRACEACT_UMOD:
6615 			case DTRACEACT_UADDR: {
6616 #if defined(sun)
6617 				struct pid *pid = curthread->t_procp->p_pidp;
6618 #endif
6619 
6620 				if (!dtrace_priv_proc(state))
6621 					continue;
6622 
6623 				DTRACE_STORE(uint64_t, tomax,
6624 #if defined(sun)
6625 				    valoffs, (uint64_t)pid->pid_id);
6626 #else
6627 				    valoffs, (uint64_t) curproc->p_pid);
6628 #endif
6629 				DTRACE_STORE(uint64_t, tomax,
6630 				    valoffs + sizeof (uint64_t), val);
6631 
6632 				continue;
6633 			}
6634 
6635 			case DTRACEACT_EXIT: {
6636 				/*
6637 				 * For the exit action, we are going to attempt
6638 				 * to atomically set our activity to be
6639 				 * draining.  If this fails (either because
6640 				 * another CPU has beat us to the exit action,
6641 				 * or because our current activity is something
6642 				 * other than ACTIVE or WARMUP), we will
6643 				 * continue.  This assures that the exit action
6644 				 * can be successfully recorded at most once
6645 				 * when we're in the ACTIVE state.  If we're
6646 				 * encountering the exit() action while in
6647 				 * COOLDOWN, however, we want to honor the new
6648 				 * status code.  (We know that we're the only
6649 				 * thread in COOLDOWN, so there is no race.)
6650 				 */
6651 				void *activity = &state->dts_activity;
6652 				dtrace_activity_t current = state->dts_activity;
6653 
6654 				if (current == DTRACE_ACTIVITY_COOLDOWN)
6655 					break;
6656 
6657 				if (current != DTRACE_ACTIVITY_WARMUP)
6658 					current = DTRACE_ACTIVITY_ACTIVE;
6659 
6660 				if (dtrace_cas32(activity, current,
6661 				    DTRACE_ACTIVITY_DRAINING) != current) {
6662 					*flags |= CPU_DTRACE_DROP;
6663 					continue;
6664 				}
6665 
6666 				break;
6667 			}
6668 
6669 			default:
6670 				ASSERT(0);
6671 			}
6672 
6673 			if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) {
6674 				uintptr_t end = valoffs + size;
6675 
6676 				if (tracememsize != 0 &&
6677 				    valoffs + tracememsize < end) {
6678 					end = valoffs + tracememsize;
6679 					tracememsize = 0;
6680 				}
6681 
6682 				if (!dtrace_vcanload((void *)(uintptr_t)val,
6683 				    &dp->dtdo_rtype, &mstate, vstate))
6684 					continue;
6685 
6686 				/*
6687 				 * If this is a string, we're going to only
6688 				 * load until we find the zero byte -- after
6689 				 * which we'll store zero bytes.
6690 				 */
6691 				if (dp->dtdo_rtype.dtdt_kind ==
6692 				    DIF_TYPE_STRING) {
6693 					char c = '\0' + 1;
6694 					int intuple = act->dta_intuple;
6695 					size_t s;
6696 
6697 					for (s = 0; s < size; s++) {
6698 						if (c != '\0')
6699 							c = dtrace_load8(val++);
6700 
6701 						DTRACE_STORE(uint8_t, tomax,
6702 						    valoffs++, c);
6703 
6704 						if (c == '\0' && intuple)
6705 							break;
6706 					}
6707 
6708 					continue;
6709 				}
6710 
6711 				while (valoffs < end) {
6712 					DTRACE_STORE(uint8_t, tomax, valoffs++,
6713 					    dtrace_load8(val++));
6714 				}
6715 
6716 				continue;
6717 			}
6718 
6719 			switch (size) {
6720 			case 0:
6721 				break;
6722 
6723 			case sizeof (uint8_t):
6724 				DTRACE_STORE(uint8_t, tomax, valoffs, val);
6725 				break;
6726 			case sizeof (uint16_t):
6727 				DTRACE_STORE(uint16_t, tomax, valoffs, val);
6728 				break;
6729 			case sizeof (uint32_t):
6730 				DTRACE_STORE(uint32_t, tomax, valoffs, val);
6731 				break;
6732 			case sizeof (uint64_t):
6733 				DTRACE_STORE(uint64_t, tomax, valoffs, val);
6734 				break;
6735 			default:
6736 				/*
6737 				 * Any other size should have been returned by
6738 				 * reference, not by value.
6739 				 */
6740 				ASSERT(0);
6741 				break;
6742 			}
6743 		}
6744 
6745 		if (*flags & CPU_DTRACE_DROP)
6746 			continue;
6747 
6748 		if (*flags & CPU_DTRACE_FAULT) {
6749 			int ndx;
6750 			dtrace_action_t *err;
6751 
6752 			buf->dtb_errors++;
6753 
6754 			if (probe->dtpr_id == dtrace_probeid_error) {
6755 				/*
6756 				 * There's nothing we can do -- we had an
6757 				 * error on the error probe.  We bump an
6758 				 * error counter to at least indicate that
6759 				 * this condition happened.
6760 				 */
6761 				dtrace_error(&state->dts_dblerrors);
6762 				continue;
6763 			}
6764 
6765 			if (vtime) {
6766 				/*
6767 				 * Before recursing on dtrace_probe(), we
6768 				 * need to explicitly clear out our start
6769 				 * time to prevent it from being accumulated
6770 				 * into t_dtrace_vtime.
6771 				 */
6772 				curthread->t_dtrace_start = 0;
6773 			}
6774 
6775 			/*
6776 			 * Iterate over the actions to figure out which action
6777 			 * we were processing when we experienced the error.
6778 			 * Note that act points _past_ the faulting action; if
6779 			 * act is ecb->dte_action, the fault was in the
6780 			 * predicate, if it's ecb->dte_action->dta_next it's
6781 			 * in action #1, and so on.
6782 			 */
6783 			for (err = ecb->dte_action, ndx = 0;
6784 			    err != act; err = err->dta_next, ndx++)
6785 				continue;
6786 
6787 			dtrace_probe_error(state, ecb->dte_epid, ndx,
6788 			    (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ?
6789 			    mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags),
6790 			    cpu_core[cpuid].cpuc_dtrace_illval);
6791 
6792 			continue;
6793 		}
6794 
6795 		if (!committed)
6796 			buf->dtb_offset = offs + ecb->dte_size;
6797 	}
6798 
6799 	if (vtime)
6800 		curthread->t_dtrace_start = dtrace_gethrtime();
6801 
6802 	dtrace_interrupt_enable(cookie);
6803 }
6804 
6805 /*
6806  * DTrace Probe Hashing Functions
6807  *
6808  * The functions in this section (and indeed, the functions in remaining
6809  * sections) are not _called_ from probe context.  (Any exceptions to this are
6810  * marked with a "Note:".)  Rather, they are called from elsewhere in the
6811  * DTrace framework to look-up probes in, add probes to and remove probes from
6812  * the DTrace probe hashes.  (Each probe is hashed by each element of the
6813  * probe tuple -- allowing for fast lookups, regardless of what was
6814  * specified.)
6815  */
6816 static uint_t
6817 dtrace_hash_str(const char *p)
6818 {
6819 	unsigned int g;
6820 	uint_t hval = 0;
6821 
6822 	while (*p) {
6823 		hval = (hval << 4) + *p++;
6824 		if ((g = (hval & 0xf0000000)) != 0)
6825 			hval ^= g >> 24;
6826 		hval &= ~g;
6827 	}
6828 	return (hval);
6829 }
6830 
6831 static dtrace_hash_t *
6832 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs)
6833 {
6834 	dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP);
6835 
6836 	hash->dth_stroffs = stroffs;
6837 	hash->dth_nextoffs = nextoffs;
6838 	hash->dth_prevoffs = prevoffs;
6839 
6840 	hash->dth_size = 1;
6841 	hash->dth_mask = hash->dth_size - 1;
6842 
6843 	hash->dth_tab = kmem_zalloc(hash->dth_size *
6844 	    sizeof (dtrace_hashbucket_t *), KM_SLEEP);
6845 
6846 	return (hash);
6847 }
6848 
6849 static void
6850 dtrace_hash_destroy(dtrace_hash_t *hash)
6851 {
6852 #ifdef DEBUG
6853 	int i;
6854 
6855 	for (i = 0; i < hash->dth_size; i++)
6856 		ASSERT(hash->dth_tab[i] == NULL);
6857 #endif
6858 
6859 	kmem_free(hash->dth_tab,
6860 	    hash->dth_size * sizeof (dtrace_hashbucket_t *));
6861 	kmem_free(hash, sizeof (dtrace_hash_t));
6862 }
6863 
6864 static void
6865 dtrace_hash_resize(dtrace_hash_t *hash)
6866 {
6867 	int size = hash->dth_size, i, ndx;
6868 	int new_size = hash->dth_size << 1;
6869 	int new_mask = new_size - 1;
6870 	dtrace_hashbucket_t **new_tab, *bucket, *next;
6871 
6872 	ASSERT((new_size & new_mask) == 0);
6873 
6874 	new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP);
6875 
6876 	for (i = 0; i < size; i++) {
6877 		for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) {
6878 			dtrace_probe_t *probe = bucket->dthb_chain;
6879 
6880 			ASSERT(probe != NULL);
6881 			ndx = DTRACE_HASHSTR(hash, probe) & new_mask;
6882 
6883 			next = bucket->dthb_next;
6884 			bucket->dthb_next = new_tab[ndx];
6885 			new_tab[ndx] = bucket;
6886 		}
6887 	}
6888 
6889 	kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *));
6890 	hash->dth_tab = new_tab;
6891 	hash->dth_size = new_size;
6892 	hash->dth_mask = new_mask;
6893 }
6894 
6895 static void
6896 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new)
6897 {
6898 	int hashval = DTRACE_HASHSTR(hash, new);
6899 	int ndx = hashval & hash->dth_mask;
6900 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
6901 	dtrace_probe_t **nextp, **prevp;
6902 
6903 	for (; bucket != NULL; bucket = bucket->dthb_next) {
6904 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new))
6905 			goto add;
6906 	}
6907 
6908 	if ((hash->dth_nbuckets >> 1) > hash->dth_size) {
6909 		dtrace_hash_resize(hash);
6910 		dtrace_hash_add(hash, new);
6911 		return;
6912 	}
6913 
6914 	bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP);
6915 	bucket->dthb_next = hash->dth_tab[ndx];
6916 	hash->dth_tab[ndx] = bucket;
6917 	hash->dth_nbuckets++;
6918 
6919 add:
6920 	nextp = DTRACE_HASHNEXT(hash, new);
6921 	ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL);
6922 	*nextp = bucket->dthb_chain;
6923 
6924 	if (bucket->dthb_chain != NULL) {
6925 		prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain);
6926 		ASSERT(*prevp == NULL);
6927 		*prevp = new;
6928 	}
6929 
6930 	bucket->dthb_chain = new;
6931 	bucket->dthb_len++;
6932 }
6933 
6934 static dtrace_probe_t *
6935 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template)
6936 {
6937 	int hashval = DTRACE_HASHSTR(hash, template);
6938 	int ndx = hashval & hash->dth_mask;
6939 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
6940 
6941 	for (; bucket != NULL; bucket = bucket->dthb_next) {
6942 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
6943 			return (bucket->dthb_chain);
6944 	}
6945 
6946 	return (NULL);
6947 }
6948 
6949 static int
6950 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template)
6951 {
6952 	int hashval = DTRACE_HASHSTR(hash, template);
6953 	int ndx = hashval & hash->dth_mask;
6954 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
6955 
6956 	for (; bucket != NULL; bucket = bucket->dthb_next) {
6957 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
6958 			return (bucket->dthb_len);
6959 	}
6960 
6961 	return (0);
6962 }
6963 
6964 static void
6965 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe)
6966 {
6967 	int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask;
6968 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
6969 
6970 	dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe);
6971 	dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe);
6972 
6973 	/*
6974 	 * Find the bucket that we're removing this probe from.
6975 	 */
6976 	for (; bucket != NULL; bucket = bucket->dthb_next) {
6977 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe))
6978 			break;
6979 	}
6980 
6981 	ASSERT(bucket != NULL);
6982 
6983 	if (*prevp == NULL) {
6984 		if (*nextp == NULL) {
6985 			/*
6986 			 * The removed probe was the only probe on this
6987 			 * bucket; we need to remove the bucket.
6988 			 */
6989 			dtrace_hashbucket_t *b = hash->dth_tab[ndx];
6990 
6991 			ASSERT(bucket->dthb_chain == probe);
6992 			ASSERT(b != NULL);
6993 
6994 			if (b == bucket) {
6995 				hash->dth_tab[ndx] = bucket->dthb_next;
6996 			} else {
6997 				while (b->dthb_next != bucket)
6998 					b = b->dthb_next;
6999 				b->dthb_next = bucket->dthb_next;
7000 			}
7001 
7002 			ASSERT(hash->dth_nbuckets > 0);
7003 			hash->dth_nbuckets--;
7004 			kmem_free(bucket, sizeof (dtrace_hashbucket_t));
7005 			return;
7006 		}
7007 
7008 		bucket->dthb_chain = *nextp;
7009 	} else {
7010 		*(DTRACE_HASHNEXT(hash, *prevp)) = *nextp;
7011 	}
7012 
7013 	if (*nextp != NULL)
7014 		*(DTRACE_HASHPREV(hash, *nextp)) = *prevp;
7015 }
7016 
7017 /*
7018  * DTrace Utility Functions
7019  *
7020  * These are random utility functions that are _not_ called from probe context.
7021  */
7022 static int
7023 dtrace_badattr(const dtrace_attribute_t *a)
7024 {
7025 	return (a->dtat_name > DTRACE_STABILITY_MAX ||
7026 	    a->dtat_data > DTRACE_STABILITY_MAX ||
7027 	    a->dtat_class > DTRACE_CLASS_MAX);
7028 }
7029 
7030 /*
7031  * Return a duplicate copy of a string.  If the specified string is NULL,
7032  * this function returns a zero-length string.
7033  */
7034 static char *
7035 dtrace_strdup(const char *str)
7036 {
7037 	char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP);
7038 
7039 	if (str != NULL)
7040 		(void) strcpy(new, str);
7041 
7042 	return (new);
7043 }
7044 
7045 #define	DTRACE_ISALPHA(c)	\
7046 	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
7047 
7048 static int
7049 dtrace_badname(const char *s)
7050 {
7051 	char c;
7052 
7053 	if (s == NULL || (c = *s++) == '\0')
7054 		return (0);
7055 
7056 	if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.')
7057 		return (1);
7058 
7059 	while ((c = *s++) != '\0') {
7060 		if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') &&
7061 		    c != '-' && c != '_' && c != '.' && c != '`')
7062 			return (1);
7063 	}
7064 
7065 	return (0);
7066 }
7067 
7068 static void
7069 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp)
7070 {
7071 	uint32_t priv;
7072 
7073 #if defined(sun)
7074 	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
7075 		/*
7076 		 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
7077 		 */
7078 		priv = DTRACE_PRIV_ALL;
7079 	} else {
7080 		*uidp = crgetuid(cr);
7081 		*zoneidp = crgetzoneid(cr);
7082 
7083 		priv = 0;
7084 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE))
7085 			priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER;
7086 		else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE))
7087 			priv |= DTRACE_PRIV_USER;
7088 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE))
7089 			priv |= DTRACE_PRIV_PROC;
7090 		if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
7091 			priv |= DTRACE_PRIV_OWNER;
7092 		if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
7093 			priv |= DTRACE_PRIV_ZONEOWNER;
7094 	}
7095 #else
7096 	priv = DTRACE_PRIV_ALL;
7097 #endif
7098 
7099 	*privp = priv;
7100 }
7101 
7102 #ifdef DTRACE_ERRDEBUG
7103 static void
7104 dtrace_errdebug(const char *str)
7105 {
7106 	int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ;
7107 	int occupied = 0;
7108 
7109 	mutex_enter(&dtrace_errlock);
7110 	dtrace_errlast = str;
7111 	dtrace_errthread = curthread;
7112 
7113 	while (occupied++ < DTRACE_ERRHASHSZ) {
7114 		if (dtrace_errhash[hval].dter_msg == str) {
7115 			dtrace_errhash[hval].dter_count++;
7116 			goto out;
7117 		}
7118 
7119 		if (dtrace_errhash[hval].dter_msg != NULL) {
7120 			hval = (hval + 1) % DTRACE_ERRHASHSZ;
7121 			continue;
7122 		}
7123 
7124 		dtrace_errhash[hval].dter_msg = str;
7125 		dtrace_errhash[hval].dter_count = 1;
7126 		goto out;
7127 	}
7128 
7129 	panic("dtrace: undersized error hash");
7130 out:
7131 	mutex_exit(&dtrace_errlock);
7132 }
7133 #endif
7134 
7135 /*
7136  * DTrace Matching Functions
7137  *
7138  * These functions are used to match groups of probes, given some elements of
7139  * a probe tuple, or some globbed expressions for elements of a probe tuple.
7140  */
7141 static int
7142 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid,
7143     zoneid_t zoneid)
7144 {
7145 	if (priv != DTRACE_PRIV_ALL) {
7146 		uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags;
7147 		uint32_t match = priv & ppriv;
7148 
7149 		/*
7150 		 * No PRIV_DTRACE_* privileges...
7151 		 */
7152 		if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER |
7153 		    DTRACE_PRIV_KERNEL)) == 0)
7154 			return (0);
7155 
7156 		/*
7157 		 * No matching bits, but there were bits to match...
7158 		 */
7159 		if (match == 0 && ppriv != 0)
7160 			return (0);
7161 
7162 		/*
7163 		 * Need to have permissions to the process, but don't...
7164 		 */
7165 		if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 &&
7166 		    uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) {
7167 			return (0);
7168 		}
7169 
7170 		/*
7171 		 * Need to be in the same zone unless we possess the
7172 		 * privilege to examine all zones.
7173 		 */
7174 		if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 &&
7175 		    zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) {
7176 			return (0);
7177 		}
7178 	}
7179 
7180 	return (1);
7181 }
7182 
7183 /*
7184  * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
7185  * consists of input pattern strings and an ops-vector to evaluate them.
7186  * This function returns >0 for match, 0 for no match, and <0 for error.
7187  */
7188 static int
7189 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp,
7190     uint32_t priv, uid_t uid, zoneid_t zoneid)
7191 {
7192 	dtrace_provider_t *pvp = prp->dtpr_provider;
7193 	int rv;
7194 
7195 	if (pvp->dtpv_defunct)
7196 		return (0);
7197 
7198 	if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0)
7199 		return (rv);
7200 
7201 	if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0)
7202 		return (rv);
7203 
7204 	if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0)
7205 		return (rv);
7206 
7207 	if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0)
7208 		return (rv);
7209 
7210 	if (dtrace_match_priv(prp, priv, uid, zoneid) == 0)
7211 		return (0);
7212 
7213 	return (rv);
7214 }
7215 
7216 /*
7217  * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
7218  * interface for matching a glob pattern 'p' to an input string 's'.  Unlike
7219  * libc's version, the kernel version only applies to 8-bit ASCII strings.
7220  * In addition, all of the recursion cases except for '*' matching have been
7221  * unwound.  For '*', we still implement recursive evaluation, but a depth
7222  * counter is maintained and matching is aborted if we recurse too deep.
7223  * The function returns 0 if no match, >0 if match, and <0 if recursion error.
7224  */
7225 static int
7226 dtrace_match_glob(const char *s, const char *p, int depth)
7227 {
7228 	const char *olds;
7229 	char s1, c;
7230 	int gs;
7231 
7232 	if (depth > DTRACE_PROBEKEY_MAXDEPTH)
7233 		return (-1);
7234 
7235 	if (s == NULL)
7236 		s = ""; /* treat NULL as empty string */
7237 
7238 top:
7239 	olds = s;
7240 	s1 = *s++;
7241 
7242 	if (p == NULL)
7243 		return (0);
7244 
7245 	if ((c = *p++) == '\0')
7246 		return (s1 == '\0');
7247 
7248 	switch (c) {
7249 	case '[': {
7250 		int ok = 0, notflag = 0;
7251 		char lc = '\0';
7252 
7253 		if (s1 == '\0')
7254 			return (0);
7255 
7256 		if (*p == '!') {
7257 			notflag = 1;
7258 			p++;
7259 		}
7260 
7261 		if ((c = *p++) == '\0')
7262 			return (0);
7263 
7264 		do {
7265 			if (c == '-' && lc != '\0' && *p != ']') {
7266 				if ((c = *p++) == '\0')
7267 					return (0);
7268 				if (c == '\\' && (c = *p++) == '\0')
7269 					return (0);
7270 
7271 				if (notflag) {
7272 					if (s1 < lc || s1 > c)
7273 						ok++;
7274 					else
7275 						return (0);
7276 				} else if (lc <= s1 && s1 <= c)
7277 					ok++;
7278 
7279 			} else if (c == '\\' && (c = *p++) == '\0')
7280 				return (0);
7281 
7282 			lc = c; /* save left-hand 'c' for next iteration */
7283 
7284 			if (notflag) {
7285 				if (s1 != c)
7286 					ok++;
7287 				else
7288 					return (0);
7289 			} else if (s1 == c)
7290 				ok++;
7291 
7292 			if ((c = *p++) == '\0')
7293 				return (0);
7294 
7295 		} while (c != ']');
7296 
7297 		if (ok)
7298 			goto top;
7299 
7300 		return (0);
7301 	}
7302 
7303 	case '\\':
7304 		if ((c = *p++) == '\0')
7305 			return (0);
7306 		/*FALLTHRU*/
7307 
7308 	default:
7309 		if (c != s1)
7310 			return (0);
7311 		/*FALLTHRU*/
7312 
7313 	case '?':
7314 		if (s1 != '\0')
7315 			goto top;
7316 		return (0);
7317 
7318 	case '*':
7319 		while (*p == '*')
7320 			p++; /* consecutive *'s are identical to a single one */
7321 
7322 		if (*p == '\0')
7323 			return (1);
7324 
7325 		for (s = olds; *s != '\0'; s++) {
7326 			if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0)
7327 				return (gs);
7328 		}
7329 
7330 		return (0);
7331 	}
7332 }
7333 
7334 /*ARGSUSED*/
7335 static int
7336 dtrace_match_string(const char *s, const char *p, int depth)
7337 {
7338 	return (s != NULL && strcmp(s, p) == 0);
7339 }
7340 
7341 /*ARGSUSED*/
7342 static int
7343 dtrace_match_nul(const char *s, const char *p, int depth)
7344 {
7345 	return (1); /* always match the empty pattern */
7346 }
7347 
7348 /*ARGSUSED*/
7349 static int
7350 dtrace_match_nonzero(const char *s, const char *p, int depth)
7351 {
7352 	return (s != NULL && s[0] != '\0');
7353 }
7354 
7355 static int
7356 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid,
7357     zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg)
7358 {
7359 	dtrace_probe_t template, *probe;
7360 	dtrace_hash_t *hash = NULL;
7361 	int len, best = INT_MAX, nmatched = 0;
7362 	dtrace_id_t i;
7363 
7364 	ASSERT(MUTEX_HELD(&dtrace_lock));
7365 
7366 	/*
7367 	 * If the probe ID is specified in the key, just lookup by ID and
7368 	 * invoke the match callback once if a matching probe is found.
7369 	 */
7370 	if (pkp->dtpk_id != DTRACE_IDNONE) {
7371 		if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL &&
7372 		    dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) {
7373 			(void) (*matched)(probe, arg);
7374 			nmatched++;
7375 		}
7376 		return (nmatched);
7377 	}
7378 
7379 	template.dtpr_mod = (char *)pkp->dtpk_mod;
7380 	template.dtpr_func = (char *)pkp->dtpk_func;
7381 	template.dtpr_name = (char *)pkp->dtpk_name;
7382 
7383 	/*
7384 	 * We want to find the most distinct of the module name, function
7385 	 * name, and name.  So for each one that is not a glob pattern or
7386 	 * empty string, we perform a lookup in the corresponding hash and
7387 	 * use the hash table with the fewest collisions to do our search.
7388 	 */
7389 	if (pkp->dtpk_mmatch == &dtrace_match_string &&
7390 	    (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) {
7391 		best = len;
7392 		hash = dtrace_bymod;
7393 	}
7394 
7395 	if (pkp->dtpk_fmatch == &dtrace_match_string &&
7396 	    (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) {
7397 		best = len;
7398 		hash = dtrace_byfunc;
7399 	}
7400 
7401 	if (pkp->dtpk_nmatch == &dtrace_match_string &&
7402 	    (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) {
7403 		best = len;
7404 		hash = dtrace_byname;
7405 	}
7406 
7407 	/*
7408 	 * If we did not select a hash table, iterate over every probe and
7409 	 * invoke our callback for each one that matches our input probe key.
7410 	 */
7411 	if (hash == NULL) {
7412 		for (i = 0; i < dtrace_nprobes; i++) {
7413 			if ((probe = dtrace_probes[i]) == NULL ||
7414 			    dtrace_match_probe(probe, pkp, priv, uid,
7415 			    zoneid) <= 0)
7416 				continue;
7417 
7418 			nmatched++;
7419 
7420 			if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT)
7421 				break;
7422 		}
7423 
7424 		return (nmatched);
7425 	}
7426 
7427 	/*
7428 	 * If we selected a hash table, iterate over each probe of the same key
7429 	 * name and invoke the callback for every probe that matches the other
7430 	 * attributes of our input probe key.
7431 	 */
7432 	for (probe = dtrace_hash_lookup(hash, &template); probe != NULL;
7433 	    probe = *(DTRACE_HASHNEXT(hash, probe))) {
7434 
7435 		if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0)
7436 			continue;
7437 
7438 		nmatched++;
7439 
7440 		if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT)
7441 			break;
7442 	}
7443 
7444 	return (nmatched);
7445 }
7446 
7447 /*
7448  * Return the function pointer dtrace_probecmp() should use to compare the
7449  * specified pattern with a string.  For NULL or empty patterns, we select
7450  * dtrace_match_nul().  For glob pattern strings, we use dtrace_match_glob().
7451  * For non-empty non-glob strings, we use dtrace_match_string().
7452  */
7453 static dtrace_probekey_f *
7454 dtrace_probekey_func(const char *p)
7455 {
7456 	char c;
7457 
7458 	if (p == NULL || *p == '\0')
7459 		return (&dtrace_match_nul);
7460 
7461 	while ((c = *p++) != '\0') {
7462 		if (c == '[' || c == '?' || c == '*' || c == '\\')
7463 			return (&dtrace_match_glob);
7464 	}
7465 
7466 	return (&dtrace_match_string);
7467 }
7468 
7469 /*
7470  * Build a probe comparison key for use with dtrace_match_probe() from the
7471  * given probe description.  By convention, a null key only matches anchored
7472  * probes: if each field is the empty string, reset dtpk_fmatch to
7473  * dtrace_match_nonzero().
7474  */
7475 static void
7476 dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp)
7477 {
7478 	pkp->dtpk_prov = pdp->dtpd_provider;
7479 	pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider);
7480 
7481 	pkp->dtpk_mod = pdp->dtpd_mod;
7482 	pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod);
7483 
7484 	pkp->dtpk_func = pdp->dtpd_func;
7485 	pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func);
7486 
7487 	pkp->dtpk_name = pdp->dtpd_name;
7488 	pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name);
7489 
7490 	pkp->dtpk_id = pdp->dtpd_id;
7491 
7492 	if (pkp->dtpk_id == DTRACE_IDNONE &&
7493 	    pkp->dtpk_pmatch == &dtrace_match_nul &&
7494 	    pkp->dtpk_mmatch == &dtrace_match_nul &&
7495 	    pkp->dtpk_fmatch == &dtrace_match_nul &&
7496 	    pkp->dtpk_nmatch == &dtrace_match_nul)
7497 		pkp->dtpk_fmatch = &dtrace_match_nonzero;
7498 }
7499 
7500 /*
7501  * DTrace Provider-to-Framework API Functions
7502  *
7503  * These functions implement much of the Provider-to-Framework API, as
7504  * described in <sys/dtrace.h>.  The parts of the API not in this section are
7505  * the functions in the API for probe management (found below), and
7506  * dtrace_probe() itself (found above).
7507  */
7508 
7509 /*
7510  * Register the calling provider with the DTrace framework.  This should
7511  * generally be called by DTrace providers in their attach(9E) entry point.
7512  */
7513 int
7514 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv,
7515     cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp)
7516 {
7517 	dtrace_provider_t *provider;
7518 
7519 	if (name == NULL || pap == NULL || pops == NULL || idp == NULL) {
7520 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7521 		    "arguments", name ? name : "<NULL>");
7522 		return (EINVAL);
7523 	}
7524 
7525 	if (name[0] == '\0' || dtrace_badname(name)) {
7526 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7527 		    "provider name", name);
7528 		return (EINVAL);
7529 	}
7530 
7531 	if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) ||
7532 	    pops->dtps_enable == NULL || pops->dtps_disable == NULL ||
7533 	    pops->dtps_destroy == NULL ||
7534 	    ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) {
7535 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7536 		    "provider ops", name);
7537 		return (EINVAL);
7538 	}
7539 
7540 	if (dtrace_badattr(&pap->dtpa_provider) ||
7541 	    dtrace_badattr(&pap->dtpa_mod) ||
7542 	    dtrace_badattr(&pap->dtpa_func) ||
7543 	    dtrace_badattr(&pap->dtpa_name) ||
7544 	    dtrace_badattr(&pap->dtpa_args)) {
7545 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7546 		    "provider attributes", name);
7547 		return (EINVAL);
7548 	}
7549 
7550 	if (priv & ~DTRACE_PRIV_ALL) {
7551 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7552 		    "privilege attributes", name);
7553 		return (EINVAL);
7554 	}
7555 
7556 	if ((priv & DTRACE_PRIV_KERNEL) &&
7557 	    (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) &&
7558 	    pops->dtps_usermode == NULL) {
7559 		cmn_err(CE_WARN, "failed to register provider '%s': need "
7560 		    "dtps_usermode() op for given privilege attributes", name);
7561 		return (EINVAL);
7562 	}
7563 
7564 	provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP);
7565 	provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7566 	(void) strcpy(provider->dtpv_name, name);
7567 
7568 	provider->dtpv_attr = *pap;
7569 	provider->dtpv_priv.dtpp_flags = priv;
7570 	if (cr != NULL) {
7571 		provider->dtpv_priv.dtpp_uid = crgetuid(cr);
7572 		provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr);
7573 	}
7574 	provider->dtpv_pops = *pops;
7575 
7576 	if (pops->dtps_provide == NULL) {
7577 		ASSERT(pops->dtps_provide_module != NULL);
7578 		provider->dtpv_pops.dtps_provide =
7579 		    (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop;
7580 	}
7581 
7582 	if (pops->dtps_provide_module == NULL) {
7583 		ASSERT(pops->dtps_provide != NULL);
7584 		provider->dtpv_pops.dtps_provide_module =
7585 		    (void (*)(void *, modctl_t *))dtrace_nullop;
7586 	}
7587 
7588 	if (pops->dtps_suspend == NULL) {
7589 		ASSERT(pops->dtps_resume == NULL);
7590 		provider->dtpv_pops.dtps_suspend =
7591 		    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
7592 		provider->dtpv_pops.dtps_resume =
7593 		    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
7594 	}
7595 
7596 	provider->dtpv_arg = arg;
7597 	*idp = (dtrace_provider_id_t)provider;
7598 
7599 	if (pops == &dtrace_provider_ops) {
7600 		ASSERT(MUTEX_HELD(&dtrace_provider_lock));
7601 		ASSERT(MUTEX_HELD(&dtrace_lock));
7602 		ASSERT(dtrace_anon.dta_enabling == NULL);
7603 
7604 		/*
7605 		 * We make sure that the DTrace provider is at the head of
7606 		 * the provider chain.
7607 		 */
7608 		provider->dtpv_next = dtrace_provider;
7609 		dtrace_provider = provider;
7610 		return (0);
7611 	}
7612 
7613 	mutex_enter(&dtrace_provider_lock);
7614 	mutex_enter(&dtrace_lock);
7615 
7616 	/*
7617 	 * If there is at least one provider registered, we'll add this
7618 	 * provider after the first provider.
7619 	 */
7620 	if (dtrace_provider != NULL) {
7621 		provider->dtpv_next = dtrace_provider->dtpv_next;
7622 		dtrace_provider->dtpv_next = provider;
7623 	} else {
7624 		dtrace_provider = provider;
7625 	}
7626 
7627 	if (dtrace_retained != NULL) {
7628 		dtrace_enabling_provide(provider);
7629 
7630 		/*
7631 		 * Now we need to call dtrace_enabling_matchall() -- which
7632 		 * will acquire cpu_lock and dtrace_lock.  We therefore need
7633 		 * to drop all of our locks before calling into it...
7634 		 */
7635 		mutex_exit(&dtrace_lock);
7636 		mutex_exit(&dtrace_provider_lock);
7637 		dtrace_enabling_matchall();
7638 
7639 		return (0);
7640 	}
7641 
7642 	mutex_exit(&dtrace_lock);
7643 	mutex_exit(&dtrace_provider_lock);
7644 
7645 	return (0);
7646 }
7647 
7648 /*
7649  * Unregister the specified provider from the DTrace framework.  This should
7650  * generally be called by DTrace providers in their detach(9E) entry point.
7651  */
7652 int
7653 dtrace_unregister(dtrace_provider_id_t id)
7654 {
7655 	dtrace_provider_t *old = (dtrace_provider_t *)id;
7656 	dtrace_provider_t *prev = NULL;
7657 	int i, self = 0, noreap = 0;
7658 	dtrace_probe_t *probe, *first = NULL;
7659 
7660 	if (old->dtpv_pops.dtps_enable ==
7661 	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) {
7662 		/*
7663 		 * If DTrace itself is the provider, we're called with locks
7664 		 * already held.
7665 		 */
7666 		ASSERT(old == dtrace_provider);
7667 #if defined(sun)
7668 		ASSERT(dtrace_devi != NULL);
7669 #endif
7670 		ASSERT(MUTEX_HELD(&dtrace_provider_lock));
7671 		ASSERT(MUTEX_HELD(&dtrace_lock));
7672 		self = 1;
7673 
7674 		if (dtrace_provider->dtpv_next != NULL) {
7675 			/*
7676 			 * There's another provider here; return failure.
7677 			 */
7678 			return (EBUSY);
7679 		}
7680 	} else {
7681 		mutex_enter(&dtrace_provider_lock);
7682 #if defined(sun)
7683 		mutex_enter(&mod_lock);
7684 #endif
7685 		mutex_enter(&dtrace_lock);
7686 	}
7687 
7688 	/*
7689 	 * If anyone has /dev/dtrace open, or if there are anonymous enabled
7690 	 * probes, we refuse to let providers slither away, unless this
7691 	 * provider has already been explicitly invalidated.
7692 	 */
7693 	if (!old->dtpv_defunct &&
7694 	    (dtrace_opens || (dtrace_anon.dta_state != NULL &&
7695 	    dtrace_anon.dta_state->dts_necbs > 0))) {
7696 		if (!self) {
7697 			mutex_exit(&dtrace_lock);
7698 #if defined(sun)
7699 			mutex_exit(&mod_lock);
7700 #endif
7701 			mutex_exit(&dtrace_provider_lock);
7702 		}
7703 		return (EBUSY);
7704 	}
7705 
7706 	/*
7707 	 * Attempt to destroy the probes associated with this provider.
7708 	 */
7709 	for (i = 0; i < dtrace_nprobes; i++) {
7710 		if ((probe = dtrace_probes[i]) == NULL)
7711 			continue;
7712 
7713 		if (probe->dtpr_provider != old)
7714 			continue;
7715 
7716 		if (probe->dtpr_ecb == NULL)
7717 			continue;
7718 
7719 		/*
7720 		 * If we are trying to unregister a defunct provider, and the
7721 		 * provider was made defunct within the interval dictated by
7722 		 * dtrace_unregister_defunct_reap, we'll (asynchronously)
7723 		 * attempt to reap our enablings.  To denote that the provider
7724 		 * should reattempt to unregister itself at some point in the
7725 		 * future, we will return a differentiable error code (EAGAIN
7726 		 * instead of EBUSY) in this case.
7727 		 */
7728 		if (dtrace_gethrtime() - old->dtpv_defunct >
7729 		    dtrace_unregister_defunct_reap)
7730 			noreap = 1;
7731 
7732 		if (!self) {
7733 			mutex_exit(&dtrace_lock);
7734 #if defined(sun)
7735 			mutex_exit(&mod_lock);
7736 #endif
7737 			mutex_exit(&dtrace_provider_lock);
7738 		}
7739 
7740 		if (noreap)
7741 			return (EBUSY);
7742 
7743 		(void) taskq_dispatch(dtrace_taskq,
7744 		    (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP);
7745 
7746 		return (EAGAIN);
7747 	}
7748 
7749 	/*
7750 	 * All of the probes for this provider are disabled; we can safely
7751 	 * remove all of them from their hash chains and from the probe array.
7752 	 */
7753 	for (i = 0; i < dtrace_nprobes; i++) {
7754 		if ((probe = dtrace_probes[i]) == NULL)
7755 			continue;
7756 
7757 		if (probe->dtpr_provider != old)
7758 			continue;
7759 
7760 		dtrace_probes[i] = NULL;
7761 
7762 		dtrace_hash_remove(dtrace_bymod, probe);
7763 		dtrace_hash_remove(dtrace_byfunc, probe);
7764 		dtrace_hash_remove(dtrace_byname, probe);
7765 
7766 		if (first == NULL) {
7767 			first = probe;
7768 			probe->dtpr_nextmod = NULL;
7769 		} else {
7770 			probe->dtpr_nextmod = first;
7771 			first = probe;
7772 		}
7773 	}
7774 
7775 	/*
7776 	 * The provider's probes have been removed from the hash chains and
7777 	 * from the probe array.  Now issue a dtrace_sync() to be sure that
7778 	 * everyone has cleared out from any probe array processing.
7779 	 */
7780 	dtrace_sync();
7781 
7782 	for (probe = first; probe != NULL; probe = first) {
7783 		first = probe->dtpr_nextmod;
7784 
7785 		old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id,
7786 		    probe->dtpr_arg);
7787 		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
7788 		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
7789 		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
7790 #if defined(sun)
7791 		vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1);
7792 #else
7793 		free_unr(dtrace_arena, probe->dtpr_id);
7794 #endif
7795 		kmem_free(probe, sizeof (dtrace_probe_t));
7796 	}
7797 
7798 	if ((prev = dtrace_provider) == old) {
7799 #if defined(sun)
7800 		ASSERT(self || dtrace_devi == NULL);
7801 		ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL);
7802 #endif
7803 		dtrace_provider = old->dtpv_next;
7804 	} else {
7805 		while (prev != NULL && prev->dtpv_next != old)
7806 			prev = prev->dtpv_next;
7807 
7808 		if (prev == NULL) {
7809 			panic("attempt to unregister non-existent "
7810 			    "dtrace provider %p\n", (void *)id);
7811 		}
7812 
7813 		prev->dtpv_next = old->dtpv_next;
7814 	}
7815 
7816 	if (!self) {
7817 		mutex_exit(&dtrace_lock);
7818 #if defined(sun)
7819 		mutex_exit(&mod_lock);
7820 #endif
7821 		mutex_exit(&dtrace_provider_lock);
7822 	}
7823 
7824 	kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1);
7825 	kmem_free(old, sizeof (dtrace_provider_t));
7826 
7827 	return (0);
7828 }
7829 
7830 /*
7831  * Invalidate the specified provider.  All subsequent probe lookups for the
7832  * specified provider will fail, but its probes will not be removed.
7833  */
7834 void
7835 dtrace_invalidate(dtrace_provider_id_t id)
7836 {
7837 	dtrace_provider_t *pvp = (dtrace_provider_t *)id;
7838 
7839 	ASSERT(pvp->dtpv_pops.dtps_enable !=
7840 	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop);
7841 
7842 	mutex_enter(&dtrace_provider_lock);
7843 	mutex_enter(&dtrace_lock);
7844 
7845 	pvp->dtpv_defunct = dtrace_gethrtime();
7846 
7847 	mutex_exit(&dtrace_lock);
7848 	mutex_exit(&dtrace_provider_lock);
7849 }
7850 
7851 /*
7852  * Indicate whether or not DTrace has attached.
7853  */
7854 int
7855 dtrace_attached(void)
7856 {
7857 	/*
7858 	 * dtrace_provider will be non-NULL iff the DTrace driver has
7859 	 * attached.  (It's non-NULL because DTrace is always itself a
7860 	 * provider.)
7861 	 */
7862 	return (dtrace_provider != NULL);
7863 }
7864 
7865 /*
7866  * Remove all the unenabled probes for the given provider.  This function is
7867  * not unlike dtrace_unregister(), except that it doesn't remove the provider
7868  * -- just as many of its associated probes as it can.
7869  */
7870 int
7871 dtrace_condense(dtrace_provider_id_t id)
7872 {
7873 	dtrace_provider_t *prov = (dtrace_provider_t *)id;
7874 	int i;
7875 	dtrace_probe_t *probe;
7876 
7877 	/*
7878 	 * Make sure this isn't the dtrace provider itself.
7879 	 */
7880 	ASSERT(prov->dtpv_pops.dtps_enable !=
7881 	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop);
7882 
7883 	mutex_enter(&dtrace_provider_lock);
7884 	mutex_enter(&dtrace_lock);
7885 
7886 	/*
7887 	 * Attempt to destroy the probes associated with this provider.
7888 	 */
7889 	for (i = 0; i < dtrace_nprobes; i++) {
7890 		if ((probe = dtrace_probes[i]) == NULL)
7891 			continue;
7892 
7893 		if (probe->dtpr_provider != prov)
7894 			continue;
7895 
7896 		if (probe->dtpr_ecb != NULL)
7897 			continue;
7898 
7899 		dtrace_probes[i] = NULL;
7900 
7901 		dtrace_hash_remove(dtrace_bymod, probe);
7902 		dtrace_hash_remove(dtrace_byfunc, probe);
7903 		dtrace_hash_remove(dtrace_byname, probe);
7904 
7905 		prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1,
7906 		    probe->dtpr_arg);
7907 		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
7908 		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
7909 		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
7910 		kmem_free(probe, sizeof (dtrace_probe_t));
7911 #if defined(sun)
7912 		vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1);
7913 #else
7914 		free_unr(dtrace_arena, i + 1);
7915 #endif
7916 	}
7917 
7918 	mutex_exit(&dtrace_lock);
7919 	mutex_exit(&dtrace_provider_lock);
7920 
7921 	return (0);
7922 }
7923 
7924 /*
7925  * DTrace Probe Management Functions
7926  *
7927  * The functions in this section perform the DTrace probe management,
7928  * including functions to create probes, look-up probes, and call into the
7929  * providers to request that probes be provided.  Some of these functions are
7930  * in the Provider-to-Framework API; these functions can be identified by the
7931  * fact that they are not declared "static".
7932  */
7933 
7934 /*
7935  * Create a probe with the specified module name, function name, and name.
7936  */
7937 dtrace_id_t
7938 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod,
7939     const char *func, const char *name, int aframes, void *arg)
7940 {
7941 	dtrace_probe_t *probe, **probes;
7942 	dtrace_provider_t *provider = (dtrace_provider_t *)prov;
7943 	dtrace_id_t id;
7944 
7945 	if (provider == dtrace_provider) {
7946 		ASSERT(MUTEX_HELD(&dtrace_lock));
7947 	} else {
7948 		mutex_enter(&dtrace_lock);
7949 	}
7950 
7951 #if defined(sun)
7952 	id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1,
7953 	    VM_BESTFIT | VM_SLEEP);
7954 #else
7955 	id = alloc_unr(dtrace_arena);
7956 #endif
7957 	probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP);
7958 
7959 	probe->dtpr_id = id;
7960 	probe->dtpr_gen = dtrace_probegen++;
7961 	probe->dtpr_mod = dtrace_strdup(mod);
7962 	probe->dtpr_func = dtrace_strdup(func);
7963 	probe->dtpr_name = dtrace_strdup(name);
7964 	probe->dtpr_arg = arg;
7965 	probe->dtpr_aframes = aframes;
7966 	probe->dtpr_provider = provider;
7967 
7968 	dtrace_hash_add(dtrace_bymod, probe);
7969 	dtrace_hash_add(dtrace_byfunc, probe);
7970 	dtrace_hash_add(dtrace_byname, probe);
7971 
7972 	if (id - 1 >= dtrace_nprobes) {
7973 		size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *);
7974 		size_t nsize = osize << 1;
7975 
7976 		if (nsize == 0) {
7977 			ASSERT(osize == 0);
7978 			ASSERT(dtrace_probes == NULL);
7979 			nsize = sizeof (dtrace_probe_t *);
7980 		}
7981 
7982 		probes = kmem_zalloc(nsize, KM_SLEEP);
7983 
7984 		if (dtrace_probes == NULL) {
7985 			ASSERT(osize == 0);
7986 			dtrace_probes = probes;
7987 			dtrace_nprobes = 1;
7988 		} else {
7989 			dtrace_probe_t **oprobes = dtrace_probes;
7990 
7991 			bcopy(oprobes, probes, osize);
7992 			dtrace_membar_producer();
7993 			dtrace_probes = probes;
7994 
7995 			dtrace_sync();
7996 
7997 			/*
7998 			 * All CPUs are now seeing the new probes array; we can
7999 			 * safely free the old array.
8000 			 */
8001 			kmem_free(oprobes, osize);
8002 			dtrace_nprobes <<= 1;
8003 		}
8004 
8005 		ASSERT(id - 1 < dtrace_nprobes);
8006 	}
8007 
8008 	ASSERT(dtrace_probes[id - 1] == NULL);
8009 	dtrace_probes[id - 1] = probe;
8010 
8011 	if (provider != dtrace_provider)
8012 		mutex_exit(&dtrace_lock);
8013 
8014 	return (id);
8015 }
8016 
8017 static dtrace_probe_t *
8018 dtrace_probe_lookup_id(dtrace_id_t id)
8019 {
8020 	ASSERT(MUTEX_HELD(&dtrace_lock));
8021 
8022 	if (id == 0 || id > dtrace_nprobes)
8023 		return (NULL);
8024 
8025 	return (dtrace_probes[id - 1]);
8026 }
8027 
8028 static int
8029 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg)
8030 {
8031 	*((dtrace_id_t *)arg) = probe->dtpr_id;
8032 
8033 	return (DTRACE_MATCH_DONE);
8034 }
8035 
8036 /*
8037  * Look up a probe based on provider and one or more of module name, function
8038  * name and probe name.
8039  */
8040 dtrace_id_t
8041 dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod,
8042     char *func, char *name)
8043 {
8044 	dtrace_probekey_t pkey;
8045 	dtrace_id_t id;
8046 	int match;
8047 
8048 	pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name;
8049 	pkey.dtpk_pmatch = &dtrace_match_string;
8050 	pkey.dtpk_mod = mod;
8051 	pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul;
8052 	pkey.dtpk_func = func;
8053 	pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul;
8054 	pkey.dtpk_name = name;
8055 	pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul;
8056 	pkey.dtpk_id = DTRACE_IDNONE;
8057 
8058 	mutex_enter(&dtrace_lock);
8059 	match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0,
8060 	    dtrace_probe_lookup_match, &id);
8061 	mutex_exit(&dtrace_lock);
8062 
8063 	ASSERT(match == 1 || match == 0);
8064 	return (match ? id : 0);
8065 }
8066 
8067 /*
8068  * Returns the probe argument associated with the specified probe.
8069  */
8070 void *
8071 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid)
8072 {
8073 	dtrace_probe_t *probe;
8074 	void *rval = NULL;
8075 
8076 	mutex_enter(&dtrace_lock);
8077 
8078 	if ((probe = dtrace_probe_lookup_id(pid)) != NULL &&
8079 	    probe->dtpr_provider == (dtrace_provider_t *)id)
8080 		rval = probe->dtpr_arg;
8081 
8082 	mutex_exit(&dtrace_lock);
8083 
8084 	return (rval);
8085 }
8086 
8087 /*
8088  * Copy a probe into a probe description.
8089  */
8090 static void
8091 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp)
8092 {
8093 	bzero(pdp, sizeof (dtrace_probedesc_t));
8094 	pdp->dtpd_id = prp->dtpr_id;
8095 
8096 	(void) strncpy(pdp->dtpd_provider,
8097 	    prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1);
8098 
8099 	(void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1);
8100 	(void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1);
8101 	(void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1);
8102 }
8103 
8104 /*
8105  * Called to indicate that a probe -- or probes -- should be provided by a
8106  * specfied provider.  If the specified description is NULL, the provider will
8107  * be told to provide all of its probes.  (This is done whenever a new
8108  * consumer comes along, or whenever a retained enabling is to be matched.) If
8109  * the specified description is non-NULL, the provider is given the
8110  * opportunity to dynamically provide the specified probe, allowing providers
8111  * to support the creation of probes on-the-fly.  (So-called _autocreated_
8112  * probes.)  If the provider is NULL, the operations will be applied to all
8113  * providers; if the provider is non-NULL the operations will only be applied
8114  * to the specified provider.  The dtrace_provider_lock must be held, and the
8115  * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
8116  * will need to grab the dtrace_lock when it reenters the framework through
8117  * dtrace_probe_lookup(), dtrace_probe_create(), etc.
8118  */
8119 static void
8120 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv)
8121 {
8122 #if defined(sun)
8123 	modctl_t *ctl;
8124 #endif
8125 	int all = 0;
8126 
8127 	ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8128 
8129 	if (prv == NULL) {
8130 		all = 1;
8131 		prv = dtrace_provider;
8132 	}
8133 
8134 	do {
8135 		/*
8136 		 * First, call the blanket provide operation.
8137 		 */
8138 		prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc);
8139 
8140 #if defined(sun)
8141 		/*
8142 		 * Now call the per-module provide operation.  We will grab
8143 		 * mod_lock to prevent the list from being modified.  Note
8144 		 * that this also prevents the mod_busy bits from changing.
8145 		 * (mod_busy can only be changed with mod_lock held.)
8146 		 */
8147 		mutex_enter(&mod_lock);
8148 
8149 		ctl = &modules;
8150 		do {
8151 			if (ctl->mod_busy || ctl->mod_mp == NULL)
8152 				continue;
8153 
8154 			prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
8155 
8156 		} while ((ctl = ctl->mod_next) != &modules);
8157 
8158 		mutex_exit(&mod_lock);
8159 #endif
8160 	} while (all && (prv = prv->dtpv_next) != NULL);
8161 }
8162 
8163 #if defined(sun)
8164 /*
8165  * Iterate over each probe, and call the Framework-to-Provider API function
8166  * denoted by offs.
8167  */
8168 static void
8169 dtrace_probe_foreach(uintptr_t offs)
8170 {
8171 	dtrace_provider_t *prov;
8172 	void (*func)(void *, dtrace_id_t, void *);
8173 	dtrace_probe_t *probe;
8174 	dtrace_icookie_t cookie;
8175 	int i;
8176 
8177 	/*
8178 	 * We disable interrupts to walk through the probe array.  This is
8179 	 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
8180 	 * won't see stale data.
8181 	 */
8182 	cookie = dtrace_interrupt_disable();
8183 
8184 	for (i = 0; i < dtrace_nprobes; i++) {
8185 		if ((probe = dtrace_probes[i]) == NULL)
8186 			continue;
8187 
8188 		if (probe->dtpr_ecb == NULL) {
8189 			/*
8190 			 * This probe isn't enabled -- don't call the function.
8191 			 */
8192 			continue;
8193 		}
8194 
8195 		prov = probe->dtpr_provider;
8196 		func = *((void(**)(void *, dtrace_id_t, void *))
8197 		    ((uintptr_t)&prov->dtpv_pops + offs));
8198 
8199 		func(prov->dtpv_arg, i + 1, probe->dtpr_arg);
8200 	}
8201 
8202 	dtrace_interrupt_enable(cookie);
8203 }
8204 #endif
8205 
8206 static int
8207 dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab)
8208 {
8209 	dtrace_probekey_t pkey;
8210 	uint32_t priv;
8211 	uid_t uid;
8212 	zoneid_t zoneid;
8213 
8214 	ASSERT(MUTEX_HELD(&dtrace_lock));
8215 	dtrace_ecb_create_cache = NULL;
8216 
8217 	if (desc == NULL) {
8218 		/*
8219 		 * If we're passed a NULL description, we're being asked to
8220 		 * create an ECB with a NULL probe.
8221 		 */
8222 		(void) dtrace_ecb_create_enable(NULL, enab);
8223 		return (0);
8224 	}
8225 
8226 	dtrace_probekey(desc, &pkey);
8227 	dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred,
8228 	    &priv, &uid, &zoneid);
8229 
8230 	return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable,
8231 	    enab));
8232 }
8233 
8234 /*
8235  * DTrace Helper Provider Functions
8236  */
8237 static void
8238 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr)
8239 {
8240 	attr->dtat_name = DOF_ATTR_NAME(dofattr);
8241 	attr->dtat_data = DOF_ATTR_DATA(dofattr);
8242 	attr->dtat_class = DOF_ATTR_CLASS(dofattr);
8243 }
8244 
8245 static void
8246 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov,
8247     const dof_provider_t *dofprov, char *strtab)
8248 {
8249 	hprov->dthpv_provname = strtab + dofprov->dofpv_name;
8250 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider,
8251 	    dofprov->dofpv_provattr);
8252 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod,
8253 	    dofprov->dofpv_modattr);
8254 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func,
8255 	    dofprov->dofpv_funcattr);
8256 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name,
8257 	    dofprov->dofpv_nameattr);
8258 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args,
8259 	    dofprov->dofpv_argsattr);
8260 }
8261 
8262 static void
8263 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
8264 {
8265 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8266 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
8267 	dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
8268 	dof_provider_t *provider;
8269 	dof_probe_t *probe;
8270 	uint32_t *off, *enoff;
8271 	uint8_t *arg;
8272 	char *strtab;
8273 	uint_t i, nprobes;
8274 	dtrace_helper_provdesc_t dhpv;
8275 	dtrace_helper_probedesc_t dhpb;
8276 	dtrace_meta_t *meta = dtrace_meta_pid;
8277 	dtrace_mops_t *mops = &meta->dtm_mops;
8278 	void *parg;
8279 
8280 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
8281 	str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8282 	    provider->dofpv_strtab * dof->dofh_secsize);
8283 	prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8284 	    provider->dofpv_probes * dof->dofh_secsize);
8285 	arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8286 	    provider->dofpv_prargs * dof->dofh_secsize);
8287 	off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8288 	    provider->dofpv_proffs * dof->dofh_secsize);
8289 
8290 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
8291 	off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset);
8292 	arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
8293 	enoff = NULL;
8294 
8295 	/*
8296 	 * See dtrace_helper_provider_validate().
8297 	 */
8298 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
8299 	    provider->dofpv_prenoffs != DOF_SECT_NONE) {
8300 		enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8301 		    provider->dofpv_prenoffs * dof->dofh_secsize);
8302 		enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset);
8303 	}
8304 
8305 	nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
8306 
8307 	/*
8308 	 * Create the provider.
8309 	 */
8310 	dtrace_dofprov2hprov(&dhpv, provider, strtab);
8311 
8312 	if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL)
8313 		return;
8314 
8315 	meta->dtm_count++;
8316 
8317 	/*
8318 	 * Create the probes.
8319 	 */
8320 	for (i = 0; i < nprobes; i++) {
8321 		probe = (dof_probe_t *)(uintptr_t)(daddr +
8322 		    prb_sec->dofs_offset + i * prb_sec->dofs_entsize);
8323 
8324 		dhpb.dthpb_mod = dhp->dofhp_mod;
8325 		dhpb.dthpb_func = strtab + probe->dofpr_func;
8326 		dhpb.dthpb_name = strtab + probe->dofpr_name;
8327 		dhpb.dthpb_base = probe->dofpr_addr;
8328 		dhpb.dthpb_offs = off + probe->dofpr_offidx;
8329 		dhpb.dthpb_noffs = probe->dofpr_noffs;
8330 		if (enoff != NULL) {
8331 			dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx;
8332 			dhpb.dthpb_nenoffs = probe->dofpr_nenoffs;
8333 		} else {
8334 			dhpb.dthpb_enoffs = NULL;
8335 			dhpb.dthpb_nenoffs = 0;
8336 		}
8337 		dhpb.dthpb_args = arg + probe->dofpr_argidx;
8338 		dhpb.dthpb_nargc = probe->dofpr_nargc;
8339 		dhpb.dthpb_xargc = probe->dofpr_xargc;
8340 		dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv;
8341 		dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv;
8342 
8343 		mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb);
8344 	}
8345 }
8346 
8347 static void
8348 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid)
8349 {
8350 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8351 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
8352 	int i;
8353 
8354 	ASSERT(MUTEX_HELD(&dtrace_meta_lock));
8355 
8356 	for (i = 0; i < dof->dofh_secnum; i++) {
8357 		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
8358 		    dof->dofh_secoff + i * dof->dofh_secsize);
8359 
8360 		if (sec->dofs_type != DOF_SECT_PROVIDER)
8361 			continue;
8362 
8363 		dtrace_helper_provide_one(dhp, sec, pid);
8364 	}
8365 
8366 	/*
8367 	 * We may have just created probes, so we must now rematch against
8368 	 * any retained enablings.  Note that this call will acquire both
8369 	 * cpu_lock and dtrace_lock; the fact that we are holding
8370 	 * dtrace_meta_lock now is what defines the ordering with respect to
8371 	 * these three locks.
8372 	 */
8373 	dtrace_enabling_matchall();
8374 }
8375 
8376 static void
8377 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
8378 {
8379 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8380 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
8381 	dof_sec_t *str_sec;
8382 	dof_provider_t *provider;
8383 	char *strtab;
8384 	dtrace_helper_provdesc_t dhpv;
8385 	dtrace_meta_t *meta = dtrace_meta_pid;
8386 	dtrace_mops_t *mops = &meta->dtm_mops;
8387 
8388 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
8389 	str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8390 	    provider->dofpv_strtab * dof->dofh_secsize);
8391 
8392 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
8393 
8394 	/*
8395 	 * Create the provider.
8396 	 */
8397 	dtrace_dofprov2hprov(&dhpv, provider, strtab);
8398 
8399 	mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid);
8400 
8401 	meta->dtm_count--;
8402 }
8403 
8404 static void
8405 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid)
8406 {
8407 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8408 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
8409 	int i;
8410 
8411 	ASSERT(MUTEX_HELD(&dtrace_meta_lock));
8412 
8413 	for (i = 0; i < dof->dofh_secnum; i++) {
8414 		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
8415 		    dof->dofh_secoff + i * dof->dofh_secsize);
8416 
8417 		if (sec->dofs_type != DOF_SECT_PROVIDER)
8418 			continue;
8419 
8420 		dtrace_helper_provider_remove_one(dhp, sec, pid);
8421 	}
8422 }
8423 
8424 /*
8425  * DTrace Meta Provider-to-Framework API Functions
8426  *
8427  * These functions implement the Meta Provider-to-Framework API, as described
8428  * in <sys/dtrace.h>.
8429  */
8430 int
8431 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg,
8432     dtrace_meta_provider_id_t *idp)
8433 {
8434 	dtrace_meta_t *meta;
8435 	dtrace_helpers_t *help, *next;
8436 	int i;
8437 
8438 	*idp = DTRACE_METAPROVNONE;
8439 
8440 	/*
8441 	 * We strictly don't need the name, but we hold onto it for
8442 	 * debuggability. All hail error queues!
8443 	 */
8444 	if (name == NULL) {
8445 		cmn_err(CE_WARN, "failed to register meta-provider: "
8446 		    "invalid name");
8447 		return (EINVAL);
8448 	}
8449 
8450 	if (mops == NULL ||
8451 	    mops->dtms_create_probe == NULL ||
8452 	    mops->dtms_provide_pid == NULL ||
8453 	    mops->dtms_remove_pid == NULL) {
8454 		cmn_err(CE_WARN, "failed to register meta-register %s: "
8455 		    "invalid ops", name);
8456 		return (EINVAL);
8457 	}
8458 
8459 	meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP);
8460 	meta->dtm_mops = *mops;
8461 	meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8462 	(void) strcpy(meta->dtm_name, name);
8463 	meta->dtm_arg = arg;
8464 
8465 	mutex_enter(&dtrace_meta_lock);
8466 	mutex_enter(&dtrace_lock);
8467 
8468 	if (dtrace_meta_pid != NULL) {
8469 		mutex_exit(&dtrace_lock);
8470 		mutex_exit(&dtrace_meta_lock);
8471 		cmn_err(CE_WARN, "failed to register meta-register %s: "
8472 		    "user-land meta-provider exists", name);
8473 		kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1);
8474 		kmem_free(meta, sizeof (dtrace_meta_t));
8475 		return (EINVAL);
8476 	}
8477 
8478 	dtrace_meta_pid = meta;
8479 	*idp = (dtrace_meta_provider_id_t)meta;
8480 
8481 	/*
8482 	 * If there are providers and probes ready to go, pass them
8483 	 * off to the new meta provider now.
8484 	 */
8485 
8486 	help = dtrace_deferred_pid;
8487 	dtrace_deferred_pid = NULL;
8488 
8489 	mutex_exit(&dtrace_lock);
8490 
8491 	while (help != NULL) {
8492 		for (i = 0; i < help->dthps_nprovs; i++) {
8493 			dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
8494 			    help->dthps_pid);
8495 		}
8496 
8497 		next = help->dthps_next;
8498 		help->dthps_next = NULL;
8499 		help->dthps_prev = NULL;
8500 		help->dthps_deferred = 0;
8501 		help = next;
8502 	}
8503 
8504 	mutex_exit(&dtrace_meta_lock);
8505 
8506 	return (0);
8507 }
8508 
8509 int
8510 dtrace_meta_unregister(dtrace_meta_provider_id_t id)
8511 {
8512 	dtrace_meta_t **pp, *old = (dtrace_meta_t *)id;
8513 
8514 	mutex_enter(&dtrace_meta_lock);
8515 	mutex_enter(&dtrace_lock);
8516 
8517 	if (old == dtrace_meta_pid) {
8518 		pp = &dtrace_meta_pid;
8519 	} else {
8520 		panic("attempt to unregister non-existent "
8521 		    "dtrace meta-provider %p\n", (void *)old);
8522 	}
8523 
8524 	if (old->dtm_count != 0) {
8525 		mutex_exit(&dtrace_lock);
8526 		mutex_exit(&dtrace_meta_lock);
8527 		return (EBUSY);
8528 	}
8529 
8530 	*pp = NULL;
8531 
8532 	mutex_exit(&dtrace_lock);
8533 	mutex_exit(&dtrace_meta_lock);
8534 
8535 	kmem_free(old->dtm_name, strlen(old->dtm_name) + 1);
8536 	kmem_free(old, sizeof (dtrace_meta_t));
8537 
8538 	return (0);
8539 }
8540 
8541 
8542 /*
8543  * DTrace DIF Object Functions
8544  */
8545 static int
8546 dtrace_difo_err(uint_t pc, const char *format, ...)
8547 {
8548 	if (dtrace_err_verbose) {
8549 		va_list alist;
8550 
8551 		(void) uprintf("dtrace DIF object error: [%u]: ", pc);
8552 		va_start(alist, format);
8553 		(void) vuprintf(format, alist);
8554 		va_end(alist);
8555 	}
8556 
8557 #ifdef DTRACE_ERRDEBUG
8558 	dtrace_errdebug(format);
8559 #endif
8560 	return (1);
8561 }
8562 
8563 /*
8564  * Validate a DTrace DIF object by checking the IR instructions.  The following
8565  * rules are currently enforced by dtrace_difo_validate():
8566  *
8567  * 1. Each instruction must have a valid opcode
8568  * 2. Each register, string, variable, or subroutine reference must be valid
8569  * 3. No instruction can modify register %r0 (must be zero)
8570  * 4. All instruction reserved bits must be set to zero
8571  * 5. The last instruction must be a "ret" instruction
8572  * 6. All branch targets must reference a valid instruction _after_ the branch
8573  */
8574 static int
8575 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs,
8576     cred_t *cr)
8577 {
8578 	int err = 0, i;
8579 	int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
8580 	int kcheckload;
8581 	uint_t pc;
8582 
8583 	kcheckload = cr == NULL ||
8584 	    (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0;
8585 
8586 	dp->dtdo_destructive = 0;
8587 
8588 	for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
8589 		dif_instr_t instr = dp->dtdo_buf[pc];
8590 
8591 		uint_t r1 = DIF_INSTR_R1(instr);
8592 		uint_t r2 = DIF_INSTR_R2(instr);
8593 		uint_t rd = DIF_INSTR_RD(instr);
8594 		uint_t rs = DIF_INSTR_RS(instr);
8595 		uint_t label = DIF_INSTR_LABEL(instr);
8596 		uint_t v = DIF_INSTR_VAR(instr);
8597 		uint_t subr = DIF_INSTR_SUBR(instr);
8598 		uint_t type = DIF_INSTR_TYPE(instr);
8599 		uint_t op = DIF_INSTR_OP(instr);
8600 
8601 		switch (op) {
8602 		case DIF_OP_OR:
8603 		case DIF_OP_XOR:
8604 		case DIF_OP_AND:
8605 		case DIF_OP_SLL:
8606 		case DIF_OP_SRL:
8607 		case DIF_OP_SRA:
8608 		case DIF_OP_SUB:
8609 		case DIF_OP_ADD:
8610 		case DIF_OP_MUL:
8611 		case DIF_OP_SDIV:
8612 		case DIF_OP_UDIV:
8613 		case DIF_OP_SREM:
8614 		case DIF_OP_UREM:
8615 		case DIF_OP_COPYS:
8616 			if (r1 >= nregs)
8617 				err += efunc(pc, "invalid register %u\n", r1);
8618 			if (r2 >= nregs)
8619 				err += efunc(pc, "invalid register %u\n", r2);
8620 			if (rd >= nregs)
8621 				err += efunc(pc, "invalid register %u\n", rd);
8622 			if (rd == 0)
8623 				err += efunc(pc, "cannot write to %r0\n");
8624 			break;
8625 		case DIF_OP_NOT:
8626 		case DIF_OP_MOV:
8627 		case DIF_OP_ALLOCS:
8628 			if (r1 >= nregs)
8629 				err += efunc(pc, "invalid register %u\n", r1);
8630 			if (r2 != 0)
8631 				err += efunc(pc, "non-zero reserved bits\n");
8632 			if (rd >= nregs)
8633 				err += efunc(pc, "invalid register %u\n", rd);
8634 			if (rd == 0)
8635 				err += efunc(pc, "cannot write to %r0\n");
8636 			break;
8637 		case DIF_OP_LDSB:
8638 		case DIF_OP_LDSH:
8639 		case DIF_OP_LDSW:
8640 		case DIF_OP_LDUB:
8641 		case DIF_OP_LDUH:
8642 		case DIF_OP_LDUW:
8643 		case DIF_OP_LDX:
8644 			if (r1 >= nregs)
8645 				err += efunc(pc, "invalid register %u\n", r1);
8646 			if (r2 != 0)
8647 				err += efunc(pc, "non-zero reserved bits\n");
8648 			if (rd >= nregs)
8649 				err += efunc(pc, "invalid register %u\n", rd);
8650 			if (rd == 0)
8651 				err += efunc(pc, "cannot write to %r0\n");
8652 			if (kcheckload)
8653 				dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op +
8654 				    DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd);
8655 			break;
8656 		case DIF_OP_RLDSB:
8657 		case DIF_OP_RLDSH:
8658 		case DIF_OP_RLDSW:
8659 		case DIF_OP_RLDUB:
8660 		case DIF_OP_RLDUH:
8661 		case DIF_OP_RLDUW:
8662 		case DIF_OP_RLDX:
8663 			if (r1 >= nregs)
8664 				err += efunc(pc, "invalid register %u\n", r1);
8665 			if (r2 != 0)
8666 				err += efunc(pc, "non-zero reserved bits\n");
8667 			if (rd >= nregs)
8668 				err += efunc(pc, "invalid register %u\n", rd);
8669 			if (rd == 0)
8670 				err += efunc(pc, "cannot write to %r0\n");
8671 			break;
8672 		case DIF_OP_ULDSB:
8673 		case DIF_OP_ULDSH:
8674 		case DIF_OP_ULDSW:
8675 		case DIF_OP_ULDUB:
8676 		case DIF_OP_ULDUH:
8677 		case DIF_OP_ULDUW:
8678 		case DIF_OP_ULDX:
8679 			if (r1 >= nregs)
8680 				err += efunc(pc, "invalid register %u\n", r1);
8681 			if (r2 != 0)
8682 				err += efunc(pc, "non-zero reserved bits\n");
8683 			if (rd >= nregs)
8684 				err += efunc(pc, "invalid register %u\n", rd);
8685 			if (rd == 0)
8686 				err += efunc(pc, "cannot write to %r0\n");
8687 			break;
8688 		case DIF_OP_STB:
8689 		case DIF_OP_STH:
8690 		case DIF_OP_STW:
8691 		case DIF_OP_STX:
8692 			if (r1 >= nregs)
8693 				err += efunc(pc, "invalid register %u\n", r1);
8694 			if (r2 != 0)
8695 				err += efunc(pc, "non-zero reserved bits\n");
8696 			if (rd >= nregs)
8697 				err += efunc(pc, "invalid register %u\n", rd);
8698 			if (rd == 0)
8699 				err += efunc(pc, "cannot write to 0 address\n");
8700 			break;
8701 		case DIF_OP_CMP:
8702 		case DIF_OP_SCMP:
8703 			if (r1 >= nregs)
8704 				err += efunc(pc, "invalid register %u\n", r1);
8705 			if (r2 >= nregs)
8706 				err += efunc(pc, "invalid register %u\n", r2);
8707 			if (rd != 0)
8708 				err += efunc(pc, "non-zero reserved bits\n");
8709 			break;
8710 		case DIF_OP_TST:
8711 			if (r1 >= nregs)
8712 				err += efunc(pc, "invalid register %u\n", r1);
8713 			if (r2 != 0 || rd != 0)
8714 				err += efunc(pc, "non-zero reserved bits\n");
8715 			break;
8716 		case DIF_OP_BA:
8717 		case DIF_OP_BE:
8718 		case DIF_OP_BNE:
8719 		case DIF_OP_BG:
8720 		case DIF_OP_BGU:
8721 		case DIF_OP_BGE:
8722 		case DIF_OP_BGEU:
8723 		case DIF_OP_BL:
8724 		case DIF_OP_BLU:
8725 		case DIF_OP_BLE:
8726 		case DIF_OP_BLEU:
8727 			if (label >= dp->dtdo_len) {
8728 				err += efunc(pc, "invalid branch target %u\n",
8729 				    label);
8730 			}
8731 			if (label <= pc) {
8732 				err += efunc(pc, "backward branch to %u\n",
8733 				    label);
8734 			}
8735 			break;
8736 		case DIF_OP_RET:
8737 			if (r1 != 0 || r2 != 0)
8738 				err += efunc(pc, "non-zero reserved bits\n");
8739 			if (rd >= nregs)
8740 				err += efunc(pc, "invalid register %u\n", rd);
8741 			break;
8742 		case DIF_OP_NOP:
8743 		case DIF_OP_POPTS:
8744 		case DIF_OP_FLUSHTS:
8745 			if (r1 != 0 || r2 != 0 || rd != 0)
8746 				err += efunc(pc, "non-zero reserved bits\n");
8747 			break;
8748 		case DIF_OP_SETX:
8749 			if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) {
8750 				err += efunc(pc, "invalid integer ref %u\n",
8751 				    DIF_INSTR_INTEGER(instr));
8752 			}
8753 			if (rd >= nregs)
8754 				err += efunc(pc, "invalid register %u\n", rd);
8755 			if (rd == 0)
8756 				err += efunc(pc, "cannot write to %r0\n");
8757 			break;
8758 		case DIF_OP_SETS:
8759 			if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) {
8760 				err += efunc(pc, "invalid string ref %u\n",
8761 				    DIF_INSTR_STRING(instr));
8762 			}
8763 			if (rd >= nregs)
8764 				err += efunc(pc, "invalid register %u\n", rd);
8765 			if (rd == 0)
8766 				err += efunc(pc, "cannot write to %r0\n");
8767 			break;
8768 		case DIF_OP_LDGA:
8769 		case DIF_OP_LDTA:
8770 			if (r1 > DIF_VAR_ARRAY_MAX)
8771 				err += efunc(pc, "invalid array %u\n", r1);
8772 			if (r2 >= nregs)
8773 				err += efunc(pc, "invalid register %u\n", r2);
8774 			if (rd >= nregs)
8775 				err += efunc(pc, "invalid register %u\n", rd);
8776 			if (rd == 0)
8777 				err += efunc(pc, "cannot write to %r0\n");
8778 			break;
8779 		case DIF_OP_LDGS:
8780 		case DIF_OP_LDTS:
8781 		case DIF_OP_LDLS:
8782 		case DIF_OP_LDGAA:
8783 		case DIF_OP_LDTAA:
8784 			if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX)
8785 				err += efunc(pc, "invalid variable %u\n", v);
8786 			if (rd >= nregs)
8787 				err += efunc(pc, "invalid register %u\n", rd);
8788 			if (rd == 0)
8789 				err += efunc(pc, "cannot write to %r0\n");
8790 			break;
8791 		case DIF_OP_STGS:
8792 		case DIF_OP_STTS:
8793 		case DIF_OP_STLS:
8794 		case DIF_OP_STGAA:
8795 		case DIF_OP_STTAA:
8796 			if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX)
8797 				err += efunc(pc, "invalid variable %u\n", v);
8798 			if (rs >= nregs)
8799 				err += efunc(pc, "invalid register %u\n", rd);
8800 			break;
8801 		case DIF_OP_CALL:
8802 			if (subr > DIF_SUBR_MAX)
8803 				err += efunc(pc, "invalid subr %u\n", subr);
8804 			if (rd >= nregs)
8805 				err += efunc(pc, "invalid register %u\n", rd);
8806 			if (rd == 0)
8807 				err += efunc(pc, "cannot write to %r0\n");
8808 
8809 			if (subr == DIF_SUBR_COPYOUT ||
8810 			    subr == DIF_SUBR_COPYOUTSTR) {
8811 				dp->dtdo_destructive = 1;
8812 			}
8813 			break;
8814 		case DIF_OP_PUSHTR:
8815 			if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF)
8816 				err += efunc(pc, "invalid ref type %u\n", type);
8817 			if (r2 >= nregs)
8818 				err += efunc(pc, "invalid register %u\n", r2);
8819 			if (rs >= nregs)
8820 				err += efunc(pc, "invalid register %u\n", rs);
8821 			break;
8822 		case DIF_OP_PUSHTV:
8823 			if (type != DIF_TYPE_CTF)
8824 				err += efunc(pc, "invalid val type %u\n", type);
8825 			if (r2 >= nregs)
8826 				err += efunc(pc, "invalid register %u\n", r2);
8827 			if (rs >= nregs)
8828 				err += efunc(pc, "invalid register %u\n", rs);
8829 			break;
8830 		default:
8831 			err += efunc(pc, "invalid opcode %u\n",
8832 			    DIF_INSTR_OP(instr));
8833 		}
8834 	}
8835 
8836 	if (dp->dtdo_len != 0 &&
8837 	    DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) {
8838 		err += efunc(dp->dtdo_len - 1,
8839 		    "expected 'ret' as last DIF instruction\n");
8840 	}
8841 
8842 	if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) {
8843 		/*
8844 		 * If we're not returning by reference, the size must be either
8845 		 * 0 or the size of one of the base types.
8846 		 */
8847 		switch (dp->dtdo_rtype.dtdt_size) {
8848 		case 0:
8849 		case sizeof (uint8_t):
8850 		case sizeof (uint16_t):
8851 		case sizeof (uint32_t):
8852 		case sizeof (uint64_t):
8853 			break;
8854 
8855 		default:
8856 			err += efunc(dp->dtdo_len - 1, "bad return size");
8857 		}
8858 	}
8859 
8860 	for (i = 0; i < dp->dtdo_varlen && err == 0; i++) {
8861 		dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL;
8862 		dtrace_diftype_t *vt, *et;
8863 		uint_t id, ndx;
8864 
8865 		if (v->dtdv_scope != DIFV_SCOPE_GLOBAL &&
8866 		    v->dtdv_scope != DIFV_SCOPE_THREAD &&
8867 		    v->dtdv_scope != DIFV_SCOPE_LOCAL) {
8868 			err += efunc(i, "unrecognized variable scope %d\n",
8869 			    v->dtdv_scope);
8870 			break;
8871 		}
8872 
8873 		if (v->dtdv_kind != DIFV_KIND_ARRAY &&
8874 		    v->dtdv_kind != DIFV_KIND_SCALAR) {
8875 			err += efunc(i, "unrecognized variable type %d\n",
8876 			    v->dtdv_kind);
8877 			break;
8878 		}
8879 
8880 		if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) {
8881 			err += efunc(i, "%d exceeds variable id limit\n", id);
8882 			break;
8883 		}
8884 
8885 		if (id < DIF_VAR_OTHER_UBASE)
8886 			continue;
8887 
8888 		/*
8889 		 * For user-defined variables, we need to check that this
8890 		 * definition is identical to any previous definition that we
8891 		 * encountered.
8892 		 */
8893 		ndx = id - DIF_VAR_OTHER_UBASE;
8894 
8895 		switch (v->dtdv_scope) {
8896 		case DIFV_SCOPE_GLOBAL:
8897 			if (ndx < vstate->dtvs_nglobals) {
8898 				dtrace_statvar_t *svar;
8899 
8900 				if ((svar = vstate->dtvs_globals[ndx]) != NULL)
8901 					existing = &svar->dtsv_var;
8902 			}
8903 
8904 			break;
8905 
8906 		case DIFV_SCOPE_THREAD:
8907 			if (ndx < vstate->dtvs_ntlocals)
8908 				existing = &vstate->dtvs_tlocals[ndx];
8909 			break;
8910 
8911 		case DIFV_SCOPE_LOCAL:
8912 			if (ndx < vstate->dtvs_nlocals) {
8913 				dtrace_statvar_t *svar;
8914 
8915 				if ((svar = vstate->dtvs_locals[ndx]) != NULL)
8916 					existing = &svar->dtsv_var;
8917 			}
8918 
8919 			break;
8920 		}
8921 
8922 		vt = &v->dtdv_type;
8923 
8924 		if (vt->dtdt_flags & DIF_TF_BYREF) {
8925 			if (vt->dtdt_size == 0) {
8926 				err += efunc(i, "zero-sized variable\n");
8927 				break;
8928 			}
8929 
8930 			if (v->dtdv_scope == DIFV_SCOPE_GLOBAL &&
8931 			    vt->dtdt_size > dtrace_global_maxsize) {
8932 				err += efunc(i, "oversized by-ref global\n");
8933 				break;
8934 			}
8935 		}
8936 
8937 		if (existing == NULL || existing->dtdv_id == 0)
8938 			continue;
8939 
8940 		ASSERT(existing->dtdv_id == v->dtdv_id);
8941 		ASSERT(existing->dtdv_scope == v->dtdv_scope);
8942 
8943 		if (existing->dtdv_kind != v->dtdv_kind)
8944 			err += efunc(i, "%d changed variable kind\n", id);
8945 
8946 		et = &existing->dtdv_type;
8947 
8948 		if (vt->dtdt_flags != et->dtdt_flags) {
8949 			err += efunc(i, "%d changed variable type flags\n", id);
8950 			break;
8951 		}
8952 
8953 		if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) {
8954 			err += efunc(i, "%d changed variable type size\n", id);
8955 			break;
8956 		}
8957 	}
8958 
8959 	return (err);
8960 }
8961 
8962 /*
8963  * Validate a DTrace DIF object that it is to be used as a helper.  Helpers
8964  * are much more constrained than normal DIFOs.  Specifically, they may
8965  * not:
8966  *
8967  * 1. Make calls to subroutines other than copyin(), copyinstr() or
8968  *    miscellaneous string routines
8969  * 2. Access DTrace variables other than the args[] array, and the
8970  *    curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
8971  * 3. Have thread-local variables.
8972  * 4. Have dynamic variables.
8973  */
8974 static int
8975 dtrace_difo_validate_helper(dtrace_difo_t *dp)
8976 {
8977 	int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
8978 	int err = 0;
8979 	uint_t pc;
8980 
8981 	for (pc = 0; pc < dp->dtdo_len; pc++) {
8982 		dif_instr_t instr = dp->dtdo_buf[pc];
8983 
8984 		uint_t v = DIF_INSTR_VAR(instr);
8985 		uint_t subr = DIF_INSTR_SUBR(instr);
8986 		uint_t op = DIF_INSTR_OP(instr);
8987 
8988 		switch (op) {
8989 		case DIF_OP_OR:
8990 		case DIF_OP_XOR:
8991 		case DIF_OP_AND:
8992 		case DIF_OP_SLL:
8993 		case DIF_OP_SRL:
8994 		case DIF_OP_SRA:
8995 		case DIF_OP_SUB:
8996 		case DIF_OP_ADD:
8997 		case DIF_OP_MUL:
8998 		case DIF_OP_SDIV:
8999 		case DIF_OP_UDIV:
9000 		case DIF_OP_SREM:
9001 		case DIF_OP_UREM:
9002 		case DIF_OP_COPYS:
9003 		case DIF_OP_NOT:
9004 		case DIF_OP_MOV:
9005 		case DIF_OP_RLDSB:
9006 		case DIF_OP_RLDSH:
9007 		case DIF_OP_RLDSW:
9008 		case DIF_OP_RLDUB:
9009 		case DIF_OP_RLDUH:
9010 		case DIF_OP_RLDUW:
9011 		case DIF_OP_RLDX:
9012 		case DIF_OP_ULDSB:
9013 		case DIF_OP_ULDSH:
9014 		case DIF_OP_ULDSW:
9015 		case DIF_OP_ULDUB:
9016 		case DIF_OP_ULDUH:
9017 		case DIF_OP_ULDUW:
9018 		case DIF_OP_ULDX:
9019 		case DIF_OP_STB:
9020 		case DIF_OP_STH:
9021 		case DIF_OP_STW:
9022 		case DIF_OP_STX:
9023 		case DIF_OP_ALLOCS:
9024 		case DIF_OP_CMP:
9025 		case DIF_OP_SCMP:
9026 		case DIF_OP_TST:
9027 		case DIF_OP_BA:
9028 		case DIF_OP_BE:
9029 		case DIF_OP_BNE:
9030 		case DIF_OP_BG:
9031 		case DIF_OP_BGU:
9032 		case DIF_OP_BGE:
9033 		case DIF_OP_BGEU:
9034 		case DIF_OP_BL:
9035 		case DIF_OP_BLU:
9036 		case DIF_OP_BLE:
9037 		case DIF_OP_BLEU:
9038 		case DIF_OP_RET:
9039 		case DIF_OP_NOP:
9040 		case DIF_OP_POPTS:
9041 		case DIF_OP_FLUSHTS:
9042 		case DIF_OP_SETX:
9043 		case DIF_OP_SETS:
9044 		case DIF_OP_LDGA:
9045 		case DIF_OP_LDLS:
9046 		case DIF_OP_STGS:
9047 		case DIF_OP_STLS:
9048 		case DIF_OP_PUSHTR:
9049 		case DIF_OP_PUSHTV:
9050 			break;
9051 
9052 		case DIF_OP_LDGS:
9053 			if (v >= DIF_VAR_OTHER_UBASE)
9054 				break;
9055 
9056 			if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9)
9057 				break;
9058 
9059 			if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID ||
9060 			    v == DIF_VAR_PPID || v == DIF_VAR_TID ||
9061 			    v == DIF_VAR_EXECARGS ||
9062 			    v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME ||
9063 			    v == DIF_VAR_UID || v == DIF_VAR_GID)
9064 				break;
9065 
9066 			err += efunc(pc, "illegal variable %u\n", v);
9067 			break;
9068 
9069 		case DIF_OP_LDTA:
9070 		case DIF_OP_LDTS:
9071 		case DIF_OP_LDGAA:
9072 		case DIF_OP_LDTAA:
9073 			err += efunc(pc, "illegal dynamic variable load\n");
9074 			break;
9075 
9076 		case DIF_OP_STTS:
9077 		case DIF_OP_STGAA:
9078 		case DIF_OP_STTAA:
9079 			err += efunc(pc, "illegal dynamic variable store\n");
9080 			break;
9081 
9082 		case DIF_OP_CALL:
9083 			if (subr == DIF_SUBR_ALLOCA ||
9084 			    subr == DIF_SUBR_BCOPY ||
9085 			    subr == DIF_SUBR_COPYIN ||
9086 			    subr == DIF_SUBR_COPYINTO ||
9087 			    subr == DIF_SUBR_COPYINSTR ||
9088 			    subr == DIF_SUBR_INDEX ||
9089 			    subr == DIF_SUBR_INET_NTOA ||
9090 			    subr == DIF_SUBR_INET_NTOA6 ||
9091 			    subr == DIF_SUBR_INET_NTOP ||
9092 			    subr == DIF_SUBR_LLTOSTR ||
9093 			    subr == DIF_SUBR_RINDEX ||
9094 			    subr == DIF_SUBR_STRCHR ||
9095 			    subr == DIF_SUBR_STRJOIN ||
9096 			    subr == DIF_SUBR_STRRCHR ||
9097 			    subr == DIF_SUBR_STRSTR ||
9098 			    subr == DIF_SUBR_HTONS ||
9099 			    subr == DIF_SUBR_HTONL ||
9100 			    subr == DIF_SUBR_HTONLL ||
9101 			    subr == DIF_SUBR_NTOHS ||
9102 			    subr == DIF_SUBR_NTOHL ||
9103 			    subr == DIF_SUBR_NTOHLL ||
9104 			    subr == DIF_SUBR_MEMREF ||
9105 			    subr == DIF_SUBR_TYPEREF)
9106 				break;
9107 
9108 			err += efunc(pc, "invalid subr %u\n", subr);
9109 			break;
9110 
9111 		default:
9112 			err += efunc(pc, "invalid opcode %u\n",
9113 			    DIF_INSTR_OP(instr));
9114 		}
9115 	}
9116 
9117 	return (err);
9118 }
9119 
9120 /*
9121  * Returns 1 if the expression in the DIF object can be cached on a per-thread
9122  * basis; 0 if not.
9123  */
9124 static int
9125 dtrace_difo_cacheable(dtrace_difo_t *dp)
9126 {
9127 	int i;
9128 
9129 	if (dp == NULL)
9130 		return (0);
9131 
9132 	for (i = 0; i < dp->dtdo_varlen; i++) {
9133 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
9134 
9135 		if (v->dtdv_scope != DIFV_SCOPE_GLOBAL)
9136 			continue;
9137 
9138 		switch (v->dtdv_id) {
9139 		case DIF_VAR_CURTHREAD:
9140 		case DIF_VAR_PID:
9141 		case DIF_VAR_TID:
9142 		case DIF_VAR_EXECARGS:
9143 		case DIF_VAR_EXECNAME:
9144 		case DIF_VAR_ZONENAME:
9145 			break;
9146 
9147 		default:
9148 			return (0);
9149 		}
9150 	}
9151 
9152 	/*
9153 	 * This DIF object may be cacheable.  Now we need to look for any
9154 	 * array loading instructions, any memory loading instructions, or
9155 	 * any stores to thread-local variables.
9156 	 */
9157 	for (i = 0; i < dp->dtdo_len; i++) {
9158 		uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]);
9159 
9160 		if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) ||
9161 		    (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) ||
9162 		    (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) ||
9163 		    op == DIF_OP_LDGA || op == DIF_OP_STTS)
9164 			return (0);
9165 	}
9166 
9167 	return (1);
9168 }
9169 
9170 static void
9171 dtrace_difo_hold(dtrace_difo_t *dp)
9172 {
9173 	int i;
9174 
9175 	ASSERT(MUTEX_HELD(&dtrace_lock));
9176 
9177 	dp->dtdo_refcnt++;
9178 	ASSERT(dp->dtdo_refcnt != 0);
9179 
9180 	/*
9181 	 * We need to check this DIF object for references to the variable
9182 	 * DIF_VAR_VTIMESTAMP.
9183 	 */
9184 	for (i = 0; i < dp->dtdo_varlen; i++) {
9185 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
9186 
9187 		if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
9188 			continue;
9189 
9190 		if (dtrace_vtime_references++ == 0)
9191 			dtrace_vtime_enable();
9192 	}
9193 }
9194 
9195 /*
9196  * This routine calculates the dynamic variable chunksize for a given DIF
9197  * object.  The calculation is not fool-proof, and can probably be tricked by
9198  * malicious DIF -- but it works for all compiler-generated DIF.  Because this
9199  * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
9200  * if a dynamic variable size exceeds the chunksize.
9201  */
9202 static void
9203 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9204 {
9205 	uint64_t sval = 0;
9206 	dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
9207 	const dif_instr_t *text = dp->dtdo_buf;
9208 	uint_t pc, srd = 0;
9209 	uint_t ttop = 0;
9210 	size_t size, ksize;
9211 	uint_t id, i;
9212 
9213 	for (pc = 0; pc < dp->dtdo_len; pc++) {
9214 		dif_instr_t instr = text[pc];
9215 		uint_t op = DIF_INSTR_OP(instr);
9216 		uint_t rd = DIF_INSTR_RD(instr);
9217 		uint_t r1 = DIF_INSTR_R1(instr);
9218 		uint_t nkeys = 0;
9219 		uchar_t scope = 0;
9220 
9221 		dtrace_key_t *key = tupregs;
9222 
9223 		switch (op) {
9224 		case DIF_OP_SETX:
9225 			sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)];
9226 			srd = rd;
9227 			continue;
9228 
9229 		case DIF_OP_STTS:
9230 			key = &tupregs[DIF_DTR_NREGS];
9231 			key[0].dttk_size = 0;
9232 			key[1].dttk_size = 0;
9233 			nkeys = 2;
9234 			scope = DIFV_SCOPE_THREAD;
9235 			break;
9236 
9237 		case DIF_OP_STGAA:
9238 		case DIF_OP_STTAA:
9239 			nkeys = ttop;
9240 
9241 			if (DIF_INSTR_OP(instr) == DIF_OP_STTAA)
9242 				key[nkeys++].dttk_size = 0;
9243 
9244 			key[nkeys++].dttk_size = 0;
9245 
9246 			if (op == DIF_OP_STTAA) {
9247 				scope = DIFV_SCOPE_THREAD;
9248 			} else {
9249 				scope = DIFV_SCOPE_GLOBAL;
9250 			}
9251 
9252 			break;
9253 
9254 		case DIF_OP_PUSHTR:
9255 			if (ttop == DIF_DTR_NREGS)
9256 				return;
9257 
9258 			if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) {
9259 				/*
9260 				 * If the register for the size of the "pushtr"
9261 				 * is %r0 (or the value is 0) and the type is
9262 				 * a string, we'll use the system-wide default
9263 				 * string size.
9264 				 */
9265 				tupregs[ttop++].dttk_size =
9266 				    dtrace_strsize_default;
9267 			} else {
9268 				if (srd == 0)
9269 					return;
9270 
9271 				tupregs[ttop++].dttk_size = sval;
9272 			}
9273 
9274 			break;
9275 
9276 		case DIF_OP_PUSHTV:
9277 			if (ttop == DIF_DTR_NREGS)
9278 				return;
9279 
9280 			tupregs[ttop++].dttk_size = 0;
9281 			break;
9282 
9283 		case DIF_OP_FLUSHTS:
9284 			ttop = 0;
9285 			break;
9286 
9287 		case DIF_OP_POPTS:
9288 			if (ttop != 0)
9289 				ttop--;
9290 			break;
9291 		}
9292 
9293 		sval = 0;
9294 		srd = 0;
9295 
9296 		if (nkeys == 0)
9297 			continue;
9298 
9299 		/*
9300 		 * We have a dynamic variable allocation; calculate its size.
9301 		 */
9302 		for (ksize = 0, i = 0; i < nkeys; i++)
9303 			ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
9304 
9305 		size = sizeof (dtrace_dynvar_t);
9306 		size += sizeof (dtrace_key_t) * (nkeys - 1);
9307 		size += ksize;
9308 
9309 		/*
9310 		 * Now we need to determine the size of the stored data.
9311 		 */
9312 		id = DIF_INSTR_VAR(instr);
9313 
9314 		for (i = 0; i < dp->dtdo_varlen; i++) {
9315 			dtrace_difv_t *v = &dp->dtdo_vartab[i];
9316 
9317 			if (v->dtdv_id == id && v->dtdv_scope == scope) {
9318 				size += v->dtdv_type.dtdt_size;
9319 				break;
9320 			}
9321 		}
9322 
9323 		if (i == dp->dtdo_varlen)
9324 			return;
9325 
9326 		/*
9327 		 * We have the size.  If this is larger than the chunk size
9328 		 * for our dynamic variable state, reset the chunk size.
9329 		 */
9330 		size = P2ROUNDUP(size, sizeof (uint64_t));
9331 
9332 		if (size > vstate->dtvs_dynvars.dtds_chunksize)
9333 			vstate->dtvs_dynvars.dtds_chunksize = size;
9334 	}
9335 }
9336 
9337 static void
9338 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9339 {
9340 	int i, oldsvars, osz, nsz, otlocals, ntlocals;
9341 	uint_t id;
9342 
9343 	ASSERT(MUTEX_HELD(&dtrace_lock));
9344 	ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0);
9345 
9346 	for (i = 0; i < dp->dtdo_varlen; i++) {
9347 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
9348 		dtrace_statvar_t *svar, ***svarp = NULL;
9349 		size_t dsize = 0;
9350 		uint8_t scope = v->dtdv_scope;
9351 		int *np = NULL;
9352 
9353 		if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
9354 			continue;
9355 
9356 		id -= DIF_VAR_OTHER_UBASE;
9357 
9358 		switch (scope) {
9359 		case DIFV_SCOPE_THREAD:
9360 			while (id >= (otlocals = vstate->dtvs_ntlocals)) {
9361 				dtrace_difv_t *tlocals;
9362 
9363 				if ((ntlocals = (otlocals << 1)) == 0)
9364 					ntlocals = 1;
9365 
9366 				osz = otlocals * sizeof (dtrace_difv_t);
9367 				nsz = ntlocals * sizeof (dtrace_difv_t);
9368 
9369 				tlocals = kmem_zalloc(nsz, KM_SLEEP);
9370 
9371 				if (osz != 0) {
9372 					bcopy(vstate->dtvs_tlocals,
9373 					    tlocals, osz);
9374 					kmem_free(vstate->dtvs_tlocals, osz);
9375 				}
9376 
9377 				vstate->dtvs_tlocals = tlocals;
9378 				vstate->dtvs_ntlocals = ntlocals;
9379 			}
9380 
9381 			vstate->dtvs_tlocals[id] = *v;
9382 			continue;
9383 
9384 		case DIFV_SCOPE_LOCAL:
9385 			np = &vstate->dtvs_nlocals;
9386 			svarp = &vstate->dtvs_locals;
9387 
9388 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
9389 				dsize = NCPU * (v->dtdv_type.dtdt_size +
9390 				    sizeof (uint64_t));
9391 			else
9392 				dsize = NCPU * sizeof (uint64_t);
9393 
9394 			break;
9395 
9396 		case DIFV_SCOPE_GLOBAL:
9397 			np = &vstate->dtvs_nglobals;
9398 			svarp = &vstate->dtvs_globals;
9399 
9400 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
9401 				dsize = v->dtdv_type.dtdt_size +
9402 				    sizeof (uint64_t);
9403 
9404 			break;
9405 
9406 		default:
9407 			ASSERT(0);
9408 		}
9409 
9410 		while (id >= (oldsvars = *np)) {
9411 			dtrace_statvar_t **statics;
9412 			int newsvars, oldsize, newsize;
9413 
9414 			if ((newsvars = (oldsvars << 1)) == 0)
9415 				newsvars = 1;
9416 
9417 			oldsize = oldsvars * sizeof (dtrace_statvar_t *);
9418 			newsize = newsvars * sizeof (dtrace_statvar_t *);
9419 
9420 			statics = kmem_zalloc(newsize, KM_SLEEP);
9421 
9422 			if (oldsize != 0) {
9423 				bcopy(*svarp, statics, oldsize);
9424 				kmem_free(*svarp, oldsize);
9425 			}
9426 
9427 			*svarp = statics;
9428 			*np = newsvars;
9429 		}
9430 
9431 		if ((svar = (*svarp)[id]) == NULL) {
9432 			svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP);
9433 			svar->dtsv_var = *v;
9434 
9435 			if ((svar->dtsv_size = dsize) != 0) {
9436 				svar->dtsv_data = (uint64_t)(uintptr_t)
9437 				    kmem_zalloc(dsize, KM_SLEEP);
9438 			}
9439 
9440 			(*svarp)[id] = svar;
9441 		}
9442 
9443 		svar->dtsv_refcnt++;
9444 	}
9445 
9446 	dtrace_difo_chunksize(dp, vstate);
9447 	dtrace_difo_hold(dp);
9448 }
9449 
9450 static dtrace_difo_t *
9451 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9452 {
9453 	dtrace_difo_t *new;
9454 	size_t sz;
9455 
9456 	ASSERT(dp->dtdo_buf != NULL);
9457 	ASSERT(dp->dtdo_refcnt != 0);
9458 
9459 	new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
9460 
9461 	ASSERT(dp->dtdo_buf != NULL);
9462 	sz = dp->dtdo_len * sizeof (dif_instr_t);
9463 	new->dtdo_buf = kmem_alloc(sz, KM_SLEEP);
9464 	bcopy(dp->dtdo_buf, new->dtdo_buf, sz);
9465 	new->dtdo_len = dp->dtdo_len;
9466 
9467 	if (dp->dtdo_strtab != NULL) {
9468 		ASSERT(dp->dtdo_strlen != 0);
9469 		new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP);
9470 		bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen);
9471 		new->dtdo_strlen = dp->dtdo_strlen;
9472 	}
9473 
9474 	if (dp->dtdo_inttab != NULL) {
9475 		ASSERT(dp->dtdo_intlen != 0);
9476 		sz = dp->dtdo_intlen * sizeof (uint64_t);
9477 		new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP);
9478 		bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz);
9479 		new->dtdo_intlen = dp->dtdo_intlen;
9480 	}
9481 
9482 	if (dp->dtdo_vartab != NULL) {
9483 		ASSERT(dp->dtdo_varlen != 0);
9484 		sz = dp->dtdo_varlen * sizeof (dtrace_difv_t);
9485 		new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP);
9486 		bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz);
9487 		new->dtdo_varlen = dp->dtdo_varlen;
9488 	}
9489 
9490 	dtrace_difo_init(new, vstate);
9491 	return (new);
9492 }
9493 
9494 static void
9495 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9496 {
9497 	int i;
9498 
9499 	ASSERT(dp->dtdo_refcnt == 0);
9500 
9501 	for (i = 0; i < dp->dtdo_varlen; i++) {
9502 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
9503 		dtrace_statvar_t *svar, **svarp = NULL;
9504 		uint_t id;
9505 		uint8_t scope = v->dtdv_scope;
9506 		int *np = NULL;
9507 
9508 		switch (scope) {
9509 		case DIFV_SCOPE_THREAD:
9510 			continue;
9511 
9512 		case DIFV_SCOPE_LOCAL:
9513 			np = &vstate->dtvs_nlocals;
9514 			svarp = vstate->dtvs_locals;
9515 			break;
9516 
9517 		case DIFV_SCOPE_GLOBAL:
9518 			np = &vstate->dtvs_nglobals;
9519 			svarp = vstate->dtvs_globals;
9520 			break;
9521 
9522 		default:
9523 			ASSERT(0);
9524 		}
9525 
9526 		if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
9527 			continue;
9528 
9529 		id -= DIF_VAR_OTHER_UBASE;
9530 		ASSERT(id < *np);
9531 
9532 		svar = svarp[id];
9533 		ASSERT(svar != NULL);
9534 		ASSERT(svar->dtsv_refcnt > 0);
9535 
9536 		if (--svar->dtsv_refcnt > 0)
9537 			continue;
9538 
9539 		if (svar->dtsv_size != 0) {
9540 			ASSERT(svar->dtsv_data != 0);
9541 			kmem_free((void *)(uintptr_t)svar->dtsv_data,
9542 			    svar->dtsv_size);
9543 		}
9544 
9545 		kmem_free(svar, sizeof (dtrace_statvar_t));
9546 		svarp[id] = NULL;
9547 	}
9548 
9549 	if (dp->dtdo_buf != NULL)
9550 		kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
9551 	if (dp->dtdo_inttab != NULL)
9552 		kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
9553 	if (dp->dtdo_strtab != NULL)
9554 		kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
9555 	if (dp->dtdo_vartab != NULL)
9556 		kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
9557 
9558 	kmem_free(dp, sizeof (dtrace_difo_t));
9559 }
9560 
9561 static void
9562 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9563 {
9564 	int i;
9565 
9566 	ASSERT(MUTEX_HELD(&dtrace_lock));
9567 	ASSERT(dp->dtdo_refcnt != 0);
9568 
9569 	for (i = 0; i < dp->dtdo_varlen; i++) {
9570 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
9571 
9572 		if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
9573 			continue;
9574 
9575 		ASSERT(dtrace_vtime_references > 0);
9576 		if (--dtrace_vtime_references == 0)
9577 			dtrace_vtime_disable();
9578 	}
9579 
9580 	if (--dp->dtdo_refcnt == 0)
9581 		dtrace_difo_destroy(dp, vstate);
9582 }
9583 
9584 /*
9585  * DTrace Format Functions
9586  */
9587 static uint16_t
9588 dtrace_format_add(dtrace_state_t *state, char *str)
9589 {
9590 	char *fmt, **new;
9591 	uint16_t ndx, len = strlen(str) + 1;
9592 
9593 	fmt = kmem_zalloc(len, KM_SLEEP);
9594 	bcopy(str, fmt, len);
9595 
9596 	for (ndx = 0; ndx < state->dts_nformats; ndx++) {
9597 		if (state->dts_formats[ndx] == NULL) {
9598 			state->dts_formats[ndx] = fmt;
9599 			return (ndx + 1);
9600 		}
9601 	}
9602 
9603 	if (state->dts_nformats == USHRT_MAX) {
9604 		/*
9605 		 * This is only likely if a denial-of-service attack is being
9606 		 * attempted.  As such, it's okay to fail silently here.
9607 		 */
9608 		kmem_free(fmt, len);
9609 		return (0);
9610 	}
9611 
9612 	/*
9613 	 * For simplicity, we always resize the formats array to be exactly the
9614 	 * number of formats.
9615 	 */
9616 	ndx = state->dts_nformats++;
9617 	new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP);
9618 
9619 	if (state->dts_formats != NULL) {
9620 		ASSERT(ndx != 0);
9621 		bcopy(state->dts_formats, new, ndx * sizeof (char *));
9622 		kmem_free(state->dts_formats, ndx * sizeof (char *));
9623 	}
9624 
9625 	state->dts_formats = new;
9626 	state->dts_formats[ndx] = fmt;
9627 
9628 	return (ndx + 1);
9629 }
9630 
9631 static void
9632 dtrace_format_remove(dtrace_state_t *state, uint16_t format)
9633 {
9634 	char *fmt;
9635 
9636 	ASSERT(state->dts_formats != NULL);
9637 	ASSERT(format <= state->dts_nformats);
9638 	ASSERT(state->dts_formats[format - 1] != NULL);
9639 
9640 	fmt = state->dts_formats[format - 1];
9641 	kmem_free(fmt, strlen(fmt) + 1);
9642 	state->dts_formats[format - 1] = NULL;
9643 }
9644 
9645 static void
9646 dtrace_format_destroy(dtrace_state_t *state)
9647 {
9648 	int i;
9649 
9650 	if (state->dts_nformats == 0) {
9651 		ASSERT(state->dts_formats == NULL);
9652 		return;
9653 	}
9654 
9655 	ASSERT(state->dts_formats != NULL);
9656 
9657 	for (i = 0; i < state->dts_nformats; i++) {
9658 		char *fmt = state->dts_formats[i];
9659 
9660 		if (fmt == NULL)
9661 			continue;
9662 
9663 		kmem_free(fmt, strlen(fmt) + 1);
9664 	}
9665 
9666 	kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *));
9667 	state->dts_nformats = 0;
9668 	state->dts_formats = NULL;
9669 }
9670 
9671 /*
9672  * DTrace Predicate Functions
9673  */
9674 static dtrace_predicate_t *
9675 dtrace_predicate_create(dtrace_difo_t *dp)
9676 {
9677 	dtrace_predicate_t *pred;
9678 
9679 	ASSERT(MUTEX_HELD(&dtrace_lock));
9680 	ASSERT(dp->dtdo_refcnt != 0);
9681 
9682 	pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP);
9683 	pred->dtp_difo = dp;
9684 	pred->dtp_refcnt = 1;
9685 
9686 	if (!dtrace_difo_cacheable(dp))
9687 		return (pred);
9688 
9689 	if (dtrace_predcache_id == DTRACE_CACHEIDNONE) {
9690 		/*
9691 		 * This is only theoretically possible -- we have had 2^32
9692 		 * cacheable predicates on this machine.  We cannot allow any
9693 		 * more predicates to become cacheable:  as unlikely as it is,
9694 		 * there may be a thread caching a (now stale) predicate cache
9695 		 * ID. (N.B.: the temptation is being successfully resisted to
9696 		 * have this cmn_err() "Holy shit -- we executed this code!")
9697 		 */
9698 		return (pred);
9699 	}
9700 
9701 	pred->dtp_cacheid = dtrace_predcache_id++;
9702 
9703 	return (pred);
9704 }
9705 
9706 static void
9707 dtrace_predicate_hold(dtrace_predicate_t *pred)
9708 {
9709 	ASSERT(MUTEX_HELD(&dtrace_lock));
9710 	ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0);
9711 	ASSERT(pred->dtp_refcnt > 0);
9712 
9713 	pred->dtp_refcnt++;
9714 }
9715 
9716 static void
9717 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate)
9718 {
9719 	dtrace_difo_t *dp = pred->dtp_difo;
9720 
9721 	ASSERT(MUTEX_HELD(&dtrace_lock));
9722 	ASSERT(dp != NULL && dp->dtdo_refcnt != 0);
9723 	ASSERT(pred->dtp_refcnt > 0);
9724 
9725 	if (--pred->dtp_refcnt == 0) {
9726 		dtrace_difo_release(pred->dtp_difo, vstate);
9727 		kmem_free(pred, sizeof (dtrace_predicate_t));
9728 	}
9729 }
9730 
9731 /*
9732  * DTrace Action Description Functions
9733  */
9734 static dtrace_actdesc_t *
9735 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple,
9736     uint64_t uarg, uint64_t arg)
9737 {
9738 	dtrace_actdesc_t *act;
9739 
9740 #if defined(sun)
9741 	ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL &&
9742 	    arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA));
9743 #endif
9744 
9745 	act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP);
9746 	act->dtad_kind = kind;
9747 	act->dtad_ntuple = ntuple;
9748 	act->dtad_uarg = uarg;
9749 	act->dtad_arg = arg;
9750 	act->dtad_refcnt = 1;
9751 
9752 	return (act);
9753 }
9754 
9755 static void
9756 dtrace_actdesc_hold(dtrace_actdesc_t *act)
9757 {
9758 	ASSERT(act->dtad_refcnt >= 1);
9759 	act->dtad_refcnt++;
9760 }
9761 
9762 static void
9763 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate)
9764 {
9765 	dtrace_actkind_t kind = act->dtad_kind;
9766 	dtrace_difo_t *dp;
9767 
9768 	ASSERT(act->dtad_refcnt >= 1);
9769 
9770 	if (--act->dtad_refcnt != 0)
9771 		return;
9772 
9773 	if ((dp = act->dtad_difo) != NULL)
9774 		dtrace_difo_release(dp, vstate);
9775 
9776 	if (DTRACEACT_ISPRINTFLIKE(kind)) {
9777 		char *str = (char *)(uintptr_t)act->dtad_arg;
9778 
9779 #if defined(sun)
9780 		ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) ||
9781 		    (str == NULL && act->dtad_kind == DTRACEACT_PRINTA));
9782 #endif
9783 
9784 		if (str != NULL)
9785 			kmem_free(str, strlen(str) + 1);
9786 	}
9787 
9788 	kmem_free(act, sizeof (dtrace_actdesc_t));
9789 }
9790 
9791 /*
9792  * DTrace ECB Functions
9793  */
9794 static dtrace_ecb_t *
9795 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe)
9796 {
9797 	dtrace_ecb_t *ecb;
9798 	dtrace_epid_t epid;
9799 
9800 	ASSERT(MUTEX_HELD(&dtrace_lock));
9801 
9802 	ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP);
9803 	ecb->dte_predicate = NULL;
9804 	ecb->dte_probe = probe;
9805 
9806 	/*
9807 	 * The default size is the size of the default action: recording
9808 	 * the header.
9809 	 */
9810 	ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t);
9811 	ecb->dte_alignment = sizeof (dtrace_epid_t);
9812 
9813 	epid = state->dts_epid++;
9814 
9815 	if (epid - 1 >= state->dts_necbs) {
9816 		dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs;
9817 		int necbs = state->dts_necbs << 1;
9818 
9819 		ASSERT(epid == state->dts_necbs + 1);
9820 
9821 		if (necbs == 0) {
9822 			ASSERT(oecbs == NULL);
9823 			necbs = 1;
9824 		}
9825 
9826 		ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP);
9827 
9828 		if (oecbs != NULL)
9829 			bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs));
9830 
9831 		dtrace_membar_producer();
9832 		state->dts_ecbs = ecbs;
9833 
9834 		if (oecbs != NULL) {
9835 			/*
9836 			 * If this state is active, we must dtrace_sync()
9837 			 * before we can free the old dts_ecbs array:  we're
9838 			 * coming in hot, and there may be active ring
9839 			 * buffer processing (which indexes into the dts_ecbs
9840 			 * array) on another CPU.
9841 			 */
9842 			if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
9843 				dtrace_sync();
9844 
9845 			kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs));
9846 		}
9847 
9848 		dtrace_membar_producer();
9849 		state->dts_necbs = necbs;
9850 	}
9851 
9852 	ecb->dte_state = state;
9853 
9854 	ASSERT(state->dts_ecbs[epid - 1] == NULL);
9855 	dtrace_membar_producer();
9856 	state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb;
9857 
9858 	return (ecb);
9859 }
9860 
9861 static void
9862 dtrace_ecb_enable(dtrace_ecb_t *ecb)
9863 {
9864 	dtrace_probe_t *probe = ecb->dte_probe;
9865 
9866 	ASSERT(MUTEX_HELD(&cpu_lock));
9867 	ASSERT(MUTEX_HELD(&dtrace_lock));
9868 	ASSERT(ecb->dte_next == NULL);
9869 
9870 	if (probe == NULL) {
9871 		/*
9872 		 * This is the NULL probe -- there's nothing to do.
9873 		 */
9874 		return;
9875 	}
9876 
9877 	if (probe->dtpr_ecb == NULL) {
9878 		dtrace_provider_t *prov = probe->dtpr_provider;
9879 
9880 		/*
9881 		 * We're the first ECB on this probe.
9882 		 */
9883 		probe->dtpr_ecb = probe->dtpr_ecb_last = ecb;
9884 
9885 		if (ecb->dte_predicate != NULL)
9886 			probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid;
9887 
9888 		prov->dtpv_pops.dtps_enable(prov->dtpv_arg,
9889 		    probe->dtpr_id, probe->dtpr_arg);
9890 	} else {
9891 		/*
9892 		 * This probe is already active.  Swing the last pointer to
9893 		 * point to the new ECB, and issue a dtrace_sync() to assure
9894 		 * that all CPUs have seen the change.
9895 		 */
9896 		ASSERT(probe->dtpr_ecb_last != NULL);
9897 		probe->dtpr_ecb_last->dte_next = ecb;
9898 		probe->dtpr_ecb_last = ecb;
9899 		probe->dtpr_predcache = 0;
9900 
9901 		dtrace_sync();
9902 	}
9903 }
9904 
9905 static void
9906 dtrace_ecb_resize(dtrace_ecb_t *ecb)
9907 {
9908 	dtrace_action_t *act;
9909 	uint32_t curneeded = UINT32_MAX;
9910 	uint32_t aggbase = UINT32_MAX;
9911 
9912 	/*
9913 	 * If we record anything, we always record the dtrace_rechdr_t.  (And
9914 	 * we always record it first.)
9915 	 */
9916 	ecb->dte_size = sizeof (dtrace_rechdr_t);
9917 	ecb->dte_alignment = sizeof (dtrace_epid_t);
9918 
9919 	for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
9920 		dtrace_recdesc_t *rec = &act->dta_rec;
9921 		ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1);
9922 
9923 		ecb->dte_alignment = MAX(ecb->dte_alignment,
9924 		    rec->dtrd_alignment);
9925 
9926 		if (DTRACEACT_ISAGG(act->dta_kind)) {
9927 			dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
9928 
9929 			ASSERT(rec->dtrd_size != 0);
9930 			ASSERT(agg->dtag_first != NULL);
9931 			ASSERT(act->dta_prev->dta_intuple);
9932 			ASSERT(aggbase != UINT32_MAX);
9933 			ASSERT(curneeded != UINT32_MAX);
9934 
9935 			agg->dtag_base = aggbase;
9936 
9937 			curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
9938 			rec->dtrd_offset = curneeded;
9939 			curneeded += rec->dtrd_size;
9940 			ecb->dte_needed = MAX(ecb->dte_needed, curneeded);
9941 
9942 			aggbase = UINT32_MAX;
9943 			curneeded = UINT32_MAX;
9944 		} else if (act->dta_intuple) {
9945 			if (curneeded == UINT32_MAX) {
9946 				/*
9947 				 * This is the first record in a tuple.  Align
9948 				 * curneeded to be at offset 4 in an 8-byte
9949 				 * aligned block.
9950 				 */
9951 				ASSERT(act->dta_prev == NULL ||
9952 				    !act->dta_prev->dta_intuple);
9953 				ASSERT3U(aggbase, ==, UINT32_MAX);
9954 				curneeded = P2PHASEUP(ecb->dte_size,
9955 				    sizeof (uint64_t), sizeof (dtrace_aggid_t));
9956 
9957 				aggbase = curneeded - sizeof (dtrace_aggid_t);
9958 				ASSERT(IS_P2ALIGNED(aggbase,
9959 				    sizeof (uint64_t)));
9960 			}
9961 			curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
9962 			rec->dtrd_offset = curneeded;
9963 			curneeded += rec->dtrd_size;
9964 		} else {
9965 			/* tuples must be followed by an aggregation */
9966 			ASSERT(act->dta_prev == NULL ||
9967 			    !act->dta_prev->dta_intuple);
9968 
9969 			ecb->dte_size = P2ROUNDUP(ecb->dte_size,
9970 			    rec->dtrd_alignment);
9971 			rec->dtrd_offset = ecb->dte_size;
9972 			ecb->dte_size += rec->dtrd_size;
9973 			ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size);
9974 		}
9975 	}
9976 
9977 	if ((act = ecb->dte_action) != NULL &&
9978 	    !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) &&
9979 	    ecb->dte_size == sizeof (dtrace_rechdr_t)) {
9980 		/*
9981 		 * If the size is still sizeof (dtrace_rechdr_t), then all
9982 		 * actions store no data; set the size to 0.
9983 		 */
9984 		ecb->dte_size = 0;
9985 	}
9986 
9987 	ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t));
9988 	ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t)));
9989 	ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed,
9990 	    ecb->dte_needed);
9991 }
9992 
9993 static dtrace_action_t *
9994 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
9995 {
9996 	dtrace_aggregation_t *agg;
9997 	size_t size = sizeof (uint64_t);
9998 	int ntuple = desc->dtad_ntuple;
9999 	dtrace_action_t *act;
10000 	dtrace_recdesc_t *frec;
10001 	dtrace_aggid_t aggid;
10002 	dtrace_state_t *state = ecb->dte_state;
10003 
10004 	agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP);
10005 	agg->dtag_ecb = ecb;
10006 
10007 	ASSERT(DTRACEACT_ISAGG(desc->dtad_kind));
10008 
10009 	switch (desc->dtad_kind) {
10010 	case DTRACEAGG_MIN:
10011 		agg->dtag_initial = INT64_MAX;
10012 		agg->dtag_aggregate = dtrace_aggregate_min;
10013 		break;
10014 
10015 	case DTRACEAGG_MAX:
10016 		agg->dtag_initial = INT64_MIN;
10017 		agg->dtag_aggregate = dtrace_aggregate_max;
10018 		break;
10019 
10020 	case DTRACEAGG_COUNT:
10021 		agg->dtag_aggregate = dtrace_aggregate_count;
10022 		break;
10023 
10024 	case DTRACEAGG_QUANTIZE:
10025 		agg->dtag_aggregate = dtrace_aggregate_quantize;
10026 		size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) *
10027 		    sizeof (uint64_t);
10028 		break;
10029 
10030 	case DTRACEAGG_LQUANTIZE: {
10031 		uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg);
10032 		uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg);
10033 
10034 		agg->dtag_initial = desc->dtad_arg;
10035 		agg->dtag_aggregate = dtrace_aggregate_lquantize;
10036 
10037 		if (step == 0 || levels == 0)
10038 			goto err;
10039 
10040 		size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t);
10041 		break;
10042 	}
10043 
10044 	case DTRACEAGG_LLQUANTIZE: {
10045 		uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg);
10046 		uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg);
10047 		uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg);
10048 		uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg);
10049 		int64_t v;
10050 
10051 		agg->dtag_initial = desc->dtad_arg;
10052 		agg->dtag_aggregate = dtrace_aggregate_llquantize;
10053 
10054 		if (factor < 2 || low >= high || nsteps < factor)
10055 			goto err;
10056 
10057 		/*
10058 		 * Now check that the number of steps evenly divides a power
10059 		 * of the factor.  (This assures both integer bucket size and
10060 		 * linearity within each magnitude.)
10061 		 */
10062 		for (v = factor; v < nsteps; v *= factor)
10063 			continue;
10064 
10065 		if ((v % nsteps) || (nsteps % factor))
10066 			goto err;
10067 
10068 		size = (dtrace_aggregate_llquantize_bucket(factor,
10069 		    low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t);
10070 		break;
10071 	}
10072 
10073 	case DTRACEAGG_AVG:
10074 		agg->dtag_aggregate = dtrace_aggregate_avg;
10075 		size = sizeof (uint64_t) * 2;
10076 		break;
10077 
10078 	case DTRACEAGG_STDDEV:
10079 		agg->dtag_aggregate = dtrace_aggregate_stddev;
10080 		size = sizeof (uint64_t) * 4;
10081 		break;
10082 
10083 	case DTRACEAGG_SUM:
10084 		agg->dtag_aggregate = dtrace_aggregate_sum;
10085 		break;
10086 
10087 	default:
10088 		goto err;
10089 	}
10090 
10091 	agg->dtag_action.dta_rec.dtrd_size = size;
10092 
10093 	if (ntuple == 0)
10094 		goto err;
10095 
10096 	/*
10097 	 * We must make sure that we have enough actions for the n-tuple.
10098 	 */
10099 	for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) {
10100 		if (DTRACEACT_ISAGG(act->dta_kind))
10101 			break;
10102 
10103 		if (--ntuple == 0) {
10104 			/*
10105 			 * This is the action with which our n-tuple begins.
10106 			 */
10107 			agg->dtag_first = act;
10108 			goto success;
10109 		}
10110 	}
10111 
10112 	/*
10113 	 * This n-tuple is short by ntuple elements.  Return failure.
10114 	 */
10115 	ASSERT(ntuple != 0);
10116 err:
10117 	kmem_free(agg, sizeof (dtrace_aggregation_t));
10118 	return (NULL);
10119 
10120 success:
10121 	/*
10122 	 * If the last action in the tuple has a size of zero, it's actually
10123 	 * an expression argument for the aggregating action.
10124 	 */
10125 	ASSERT(ecb->dte_action_last != NULL);
10126 	act = ecb->dte_action_last;
10127 
10128 	if (act->dta_kind == DTRACEACT_DIFEXPR) {
10129 		ASSERT(act->dta_difo != NULL);
10130 
10131 		if (act->dta_difo->dtdo_rtype.dtdt_size == 0)
10132 			agg->dtag_hasarg = 1;
10133 	}
10134 
10135 	/*
10136 	 * We need to allocate an id for this aggregation.
10137 	 */
10138 #if defined(sun)
10139 	aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1,
10140 	    VM_BESTFIT | VM_SLEEP);
10141 #else
10142 	aggid = alloc_unr(state->dts_aggid_arena);
10143 #endif
10144 
10145 	if (aggid - 1 >= state->dts_naggregations) {
10146 		dtrace_aggregation_t **oaggs = state->dts_aggregations;
10147 		dtrace_aggregation_t **aggs;
10148 		int naggs = state->dts_naggregations << 1;
10149 		int onaggs = state->dts_naggregations;
10150 
10151 		ASSERT(aggid == state->dts_naggregations + 1);
10152 
10153 		if (naggs == 0) {
10154 			ASSERT(oaggs == NULL);
10155 			naggs = 1;
10156 		}
10157 
10158 		aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP);
10159 
10160 		if (oaggs != NULL) {
10161 			bcopy(oaggs, aggs, onaggs * sizeof (*aggs));
10162 			kmem_free(oaggs, onaggs * sizeof (*aggs));
10163 		}
10164 
10165 		state->dts_aggregations = aggs;
10166 		state->dts_naggregations = naggs;
10167 	}
10168 
10169 	ASSERT(state->dts_aggregations[aggid - 1] == NULL);
10170 	state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg;
10171 
10172 	frec = &agg->dtag_first->dta_rec;
10173 	if (frec->dtrd_alignment < sizeof (dtrace_aggid_t))
10174 		frec->dtrd_alignment = sizeof (dtrace_aggid_t);
10175 
10176 	for (act = agg->dtag_first; act != NULL; act = act->dta_next) {
10177 		ASSERT(!act->dta_intuple);
10178 		act->dta_intuple = 1;
10179 	}
10180 
10181 	return (&agg->dtag_action);
10182 }
10183 
10184 static void
10185 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act)
10186 {
10187 	dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
10188 	dtrace_state_t *state = ecb->dte_state;
10189 	dtrace_aggid_t aggid = agg->dtag_id;
10190 
10191 	ASSERT(DTRACEACT_ISAGG(act->dta_kind));
10192 #if defined(sun)
10193 	vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1);
10194 #else
10195 	free_unr(state->dts_aggid_arena, aggid);
10196 #endif
10197 
10198 	ASSERT(state->dts_aggregations[aggid - 1] == agg);
10199 	state->dts_aggregations[aggid - 1] = NULL;
10200 
10201 	kmem_free(agg, sizeof (dtrace_aggregation_t));
10202 }
10203 
10204 static int
10205 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
10206 {
10207 	dtrace_action_t *action, *last;
10208 	dtrace_difo_t *dp = desc->dtad_difo;
10209 	uint32_t size = 0, align = sizeof (uint8_t), mask;
10210 	uint16_t format = 0;
10211 	dtrace_recdesc_t *rec;
10212 	dtrace_state_t *state = ecb->dte_state;
10213 	dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize;
10214 	uint64_t arg = desc->dtad_arg;
10215 
10216 	ASSERT(MUTEX_HELD(&dtrace_lock));
10217 	ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1);
10218 
10219 	if (DTRACEACT_ISAGG(desc->dtad_kind)) {
10220 		/*
10221 		 * If this is an aggregating action, there must be neither
10222 		 * a speculate nor a commit on the action chain.
10223 		 */
10224 		dtrace_action_t *act;
10225 
10226 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
10227 			if (act->dta_kind == DTRACEACT_COMMIT)
10228 				return (EINVAL);
10229 
10230 			if (act->dta_kind == DTRACEACT_SPECULATE)
10231 				return (EINVAL);
10232 		}
10233 
10234 		action = dtrace_ecb_aggregation_create(ecb, desc);
10235 
10236 		if (action == NULL)
10237 			return (EINVAL);
10238 	} else {
10239 		if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) ||
10240 		    (desc->dtad_kind == DTRACEACT_DIFEXPR &&
10241 		    dp != NULL && dp->dtdo_destructive)) {
10242 			state->dts_destructive = 1;
10243 		}
10244 
10245 		switch (desc->dtad_kind) {
10246 		case DTRACEACT_PRINTF:
10247 		case DTRACEACT_PRINTA:
10248 		case DTRACEACT_SYSTEM:
10249 		case DTRACEACT_FREOPEN:
10250 		case DTRACEACT_DIFEXPR:
10251 			/*
10252 			 * We know that our arg is a string -- turn it into a
10253 			 * format.
10254 			 */
10255 			if (arg == 0) {
10256 				ASSERT(desc->dtad_kind == DTRACEACT_PRINTA ||
10257 				    desc->dtad_kind == DTRACEACT_DIFEXPR);
10258 				format = 0;
10259 			} else {
10260 				ASSERT(arg != 0);
10261 #if defined(sun)
10262 				ASSERT(arg > KERNELBASE);
10263 #endif
10264 				format = dtrace_format_add(state,
10265 				    (char *)(uintptr_t)arg);
10266 			}
10267 
10268 			/*FALLTHROUGH*/
10269 		case DTRACEACT_LIBACT:
10270 		case DTRACEACT_TRACEMEM:
10271 		case DTRACEACT_TRACEMEM_DYNSIZE:
10272 			if (dp == NULL)
10273 				return (EINVAL);
10274 
10275 			if ((size = dp->dtdo_rtype.dtdt_size) != 0)
10276 				break;
10277 
10278 			if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
10279 				if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10280 					return (EINVAL);
10281 
10282 				size = opt[DTRACEOPT_STRSIZE];
10283 			}
10284 
10285 			break;
10286 
10287 		case DTRACEACT_STACK:
10288 			if ((nframes = arg) == 0) {
10289 				nframes = opt[DTRACEOPT_STACKFRAMES];
10290 				ASSERT(nframes > 0);
10291 				arg = nframes;
10292 			}
10293 
10294 			size = nframes * sizeof (pc_t);
10295 			break;
10296 
10297 		case DTRACEACT_JSTACK:
10298 			if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0)
10299 				strsize = opt[DTRACEOPT_JSTACKSTRSIZE];
10300 
10301 			if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0)
10302 				nframes = opt[DTRACEOPT_JSTACKFRAMES];
10303 
10304 			arg = DTRACE_USTACK_ARG(nframes, strsize);
10305 
10306 			/*FALLTHROUGH*/
10307 		case DTRACEACT_USTACK:
10308 			if (desc->dtad_kind != DTRACEACT_JSTACK &&
10309 			    (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) {
10310 				strsize = DTRACE_USTACK_STRSIZE(arg);
10311 				nframes = opt[DTRACEOPT_USTACKFRAMES];
10312 				ASSERT(nframes > 0);
10313 				arg = DTRACE_USTACK_ARG(nframes, strsize);
10314 			}
10315 
10316 			/*
10317 			 * Save a slot for the pid.
10318 			 */
10319 			size = (nframes + 1) * sizeof (uint64_t);
10320 			size += DTRACE_USTACK_STRSIZE(arg);
10321 			size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t)));
10322 
10323 			break;
10324 
10325 		case DTRACEACT_SYM:
10326 		case DTRACEACT_MOD:
10327 			if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) !=
10328 			    sizeof (uint64_t)) ||
10329 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10330 				return (EINVAL);
10331 			break;
10332 
10333 		case DTRACEACT_USYM:
10334 		case DTRACEACT_UMOD:
10335 		case DTRACEACT_UADDR:
10336 			if (dp == NULL ||
10337 			    (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) ||
10338 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10339 				return (EINVAL);
10340 
10341 			/*
10342 			 * We have a slot for the pid, plus a slot for the
10343 			 * argument.  To keep things simple (aligned with
10344 			 * bitness-neutral sizing), we store each as a 64-bit
10345 			 * quantity.
10346 			 */
10347 			size = 2 * sizeof (uint64_t);
10348 			break;
10349 
10350 		case DTRACEACT_STOP:
10351 		case DTRACEACT_BREAKPOINT:
10352 		case DTRACEACT_PANIC:
10353 			break;
10354 
10355 		case DTRACEACT_CHILL:
10356 		case DTRACEACT_DISCARD:
10357 		case DTRACEACT_RAISE:
10358 			if (dp == NULL)
10359 				return (EINVAL);
10360 			break;
10361 
10362 		case DTRACEACT_EXIT:
10363 			if (dp == NULL ||
10364 			    (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) ||
10365 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10366 				return (EINVAL);
10367 			break;
10368 
10369 		case DTRACEACT_SPECULATE:
10370 			if (ecb->dte_size > sizeof (dtrace_rechdr_t))
10371 				return (EINVAL);
10372 
10373 			if (dp == NULL)
10374 				return (EINVAL);
10375 
10376 			state->dts_speculates = 1;
10377 			break;
10378 
10379 		case DTRACEACT_PRINTM:
10380 		    	size = dp->dtdo_rtype.dtdt_size;
10381 			break;
10382 
10383 		case DTRACEACT_PRINTT:
10384 		    	size = dp->dtdo_rtype.dtdt_size;
10385 			break;
10386 
10387 		case DTRACEACT_COMMIT: {
10388 			dtrace_action_t *act = ecb->dte_action;
10389 
10390 			for (; act != NULL; act = act->dta_next) {
10391 				if (act->dta_kind == DTRACEACT_COMMIT)
10392 					return (EINVAL);
10393 			}
10394 
10395 			if (dp == NULL)
10396 				return (EINVAL);
10397 			break;
10398 		}
10399 
10400 		default:
10401 			return (EINVAL);
10402 		}
10403 
10404 		if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) {
10405 			/*
10406 			 * If this is a data-storing action or a speculate,
10407 			 * we must be sure that there isn't a commit on the
10408 			 * action chain.
10409 			 */
10410 			dtrace_action_t *act = ecb->dte_action;
10411 
10412 			for (; act != NULL; act = act->dta_next) {
10413 				if (act->dta_kind == DTRACEACT_COMMIT)
10414 					return (EINVAL);
10415 			}
10416 		}
10417 
10418 		action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP);
10419 		action->dta_rec.dtrd_size = size;
10420 	}
10421 
10422 	action->dta_refcnt = 1;
10423 	rec = &action->dta_rec;
10424 	size = rec->dtrd_size;
10425 
10426 	for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) {
10427 		if (!(size & mask)) {
10428 			align = mask + 1;
10429 			break;
10430 		}
10431 	}
10432 
10433 	action->dta_kind = desc->dtad_kind;
10434 
10435 	if ((action->dta_difo = dp) != NULL)
10436 		dtrace_difo_hold(dp);
10437 
10438 	rec->dtrd_action = action->dta_kind;
10439 	rec->dtrd_arg = arg;
10440 	rec->dtrd_uarg = desc->dtad_uarg;
10441 	rec->dtrd_alignment = (uint16_t)align;
10442 	rec->dtrd_format = format;
10443 
10444 	if ((last = ecb->dte_action_last) != NULL) {
10445 		ASSERT(ecb->dte_action != NULL);
10446 		action->dta_prev = last;
10447 		last->dta_next = action;
10448 	} else {
10449 		ASSERT(ecb->dte_action == NULL);
10450 		ecb->dte_action = action;
10451 	}
10452 
10453 	ecb->dte_action_last = action;
10454 
10455 	return (0);
10456 }
10457 
10458 static void
10459 dtrace_ecb_action_remove(dtrace_ecb_t *ecb)
10460 {
10461 	dtrace_action_t *act = ecb->dte_action, *next;
10462 	dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate;
10463 	dtrace_difo_t *dp;
10464 	uint16_t format;
10465 
10466 	if (act != NULL && act->dta_refcnt > 1) {
10467 		ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1);
10468 		act->dta_refcnt--;
10469 	} else {
10470 		for (; act != NULL; act = next) {
10471 			next = act->dta_next;
10472 			ASSERT(next != NULL || act == ecb->dte_action_last);
10473 			ASSERT(act->dta_refcnt == 1);
10474 
10475 			if ((format = act->dta_rec.dtrd_format) != 0)
10476 				dtrace_format_remove(ecb->dte_state, format);
10477 
10478 			if ((dp = act->dta_difo) != NULL)
10479 				dtrace_difo_release(dp, vstate);
10480 
10481 			if (DTRACEACT_ISAGG(act->dta_kind)) {
10482 				dtrace_ecb_aggregation_destroy(ecb, act);
10483 			} else {
10484 				kmem_free(act, sizeof (dtrace_action_t));
10485 			}
10486 		}
10487 	}
10488 
10489 	ecb->dte_action = NULL;
10490 	ecb->dte_action_last = NULL;
10491 	ecb->dte_size = 0;
10492 }
10493 
10494 static void
10495 dtrace_ecb_disable(dtrace_ecb_t *ecb)
10496 {
10497 	/*
10498 	 * We disable the ECB by removing it from its probe.
10499 	 */
10500 	dtrace_ecb_t *pecb, *prev = NULL;
10501 	dtrace_probe_t *probe = ecb->dte_probe;
10502 
10503 	ASSERT(MUTEX_HELD(&dtrace_lock));
10504 
10505 	if (probe == NULL) {
10506 		/*
10507 		 * This is the NULL probe; there is nothing to disable.
10508 		 */
10509 		return;
10510 	}
10511 
10512 	for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) {
10513 		if (pecb == ecb)
10514 			break;
10515 		prev = pecb;
10516 	}
10517 
10518 	ASSERT(pecb != NULL);
10519 
10520 	if (prev == NULL) {
10521 		probe->dtpr_ecb = ecb->dte_next;
10522 	} else {
10523 		prev->dte_next = ecb->dte_next;
10524 	}
10525 
10526 	if (ecb == probe->dtpr_ecb_last) {
10527 		ASSERT(ecb->dte_next == NULL);
10528 		probe->dtpr_ecb_last = prev;
10529 	}
10530 
10531 	/*
10532 	 * The ECB has been disconnected from the probe; now sync to assure
10533 	 * that all CPUs have seen the change before returning.
10534 	 */
10535 	dtrace_sync();
10536 
10537 	if (probe->dtpr_ecb == NULL) {
10538 		/*
10539 		 * That was the last ECB on the probe; clear the predicate
10540 		 * cache ID for the probe, disable it and sync one more time
10541 		 * to assure that we'll never hit it again.
10542 		 */
10543 		dtrace_provider_t *prov = probe->dtpr_provider;
10544 
10545 		ASSERT(ecb->dte_next == NULL);
10546 		ASSERT(probe->dtpr_ecb_last == NULL);
10547 		probe->dtpr_predcache = DTRACE_CACHEIDNONE;
10548 		prov->dtpv_pops.dtps_disable(prov->dtpv_arg,
10549 		    probe->dtpr_id, probe->dtpr_arg);
10550 		dtrace_sync();
10551 	} else {
10552 		/*
10553 		 * There is at least one ECB remaining on the probe.  If there
10554 		 * is _exactly_ one, set the probe's predicate cache ID to be
10555 		 * the predicate cache ID of the remaining ECB.
10556 		 */
10557 		ASSERT(probe->dtpr_ecb_last != NULL);
10558 		ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE);
10559 
10560 		if (probe->dtpr_ecb == probe->dtpr_ecb_last) {
10561 			dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate;
10562 
10563 			ASSERT(probe->dtpr_ecb->dte_next == NULL);
10564 
10565 			if (p != NULL)
10566 				probe->dtpr_predcache = p->dtp_cacheid;
10567 		}
10568 
10569 		ecb->dte_next = NULL;
10570 	}
10571 }
10572 
10573 static void
10574 dtrace_ecb_destroy(dtrace_ecb_t *ecb)
10575 {
10576 	dtrace_state_t *state = ecb->dte_state;
10577 	dtrace_vstate_t *vstate = &state->dts_vstate;
10578 	dtrace_predicate_t *pred;
10579 	dtrace_epid_t epid = ecb->dte_epid;
10580 
10581 	ASSERT(MUTEX_HELD(&dtrace_lock));
10582 	ASSERT(ecb->dte_next == NULL);
10583 	ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb);
10584 
10585 	if ((pred = ecb->dte_predicate) != NULL)
10586 		dtrace_predicate_release(pred, vstate);
10587 
10588 	dtrace_ecb_action_remove(ecb);
10589 
10590 	ASSERT(state->dts_ecbs[epid - 1] == ecb);
10591 	state->dts_ecbs[epid - 1] = NULL;
10592 
10593 	kmem_free(ecb, sizeof (dtrace_ecb_t));
10594 }
10595 
10596 static dtrace_ecb_t *
10597 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe,
10598     dtrace_enabling_t *enab)
10599 {
10600 	dtrace_ecb_t *ecb;
10601 	dtrace_predicate_t *pred;
10602 	dtrace_actdesc_t *act;
10603 	dtrace_provider_t *prov;
10604 	dtrace_ecbdesc_t *desc = enab->dten_current;
10605 
10606 	ASSERT(MUTEX_HELD(&dtrace_lock));
10607 	ASSERT(state != NULL);
10608 
10609 	ecb = dtrace_ecb_add(state, probe);
10610 	ecb->dte_uarg = desc->dted_uarg;
10611 
10612 	if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) {
10613 		dtrace_predicate_hold(pred);
10614 		ecb->dte_predicate = pred;
10615 	}
10616 
10617 	if (probe != NULL) {
10618 		/*
10619 		 * If the provider shows more leg than the consumer is old
10620 		 * enough to see, we need to enable the appropriate implicit
10621 		 * predicate bits to prevent the ecb from activating at
10622 		 * revealing times.
10623 		 *
10624 		 * Providers specifying DTRACE_PRIV_USER at register time
10625 		 * are stating that they need the /proc-style privilege
10626 		 * model to be enforced, and this is what DTRACE_COND_OWNER
10627 		 * and DTRACE_COND_ZONEOWNER will then do at probe time.
10628 		 */
10629 		prov = probe->dtpr_provider;
10630 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) &&
10631 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
10632 			ecb->dte_cond |= DTRACE_COND_OWNER;
10633 
10634 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) &&
10635 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
10636 			ecb->dte_cond |= DTRACE_COND_ZONEOWNER;
10637 
10638 		/*
10639 		 * If the provider shows us kernel innards and the user
10640 		 * is lacking sufficient privilege, enable the
10641 		 * DTRACE_COND_USERMODE implicit predicate.
10642 		 */
10643 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) &&
10644 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL))
10645 			ecb->dte_cond |= DTRACE_COND_USERMODE;
10646 	}
10647 
10648 	if (dtrace_ecb_create_cache != NULL) {
10649 		/*
10650 		 * If we have a cached ecb, we'll use its action list instead
10651 		 * of creating our own (saving both time and space).
10652 		 */
10653 		dtrace_ecb_t *cached = dtrace_ecb_create_cache;
10654 		dtrace_action_t *act = cached->dte_action;
10655 
10656 		if (act != NULL) {
10657 			ASSERT(act->dta_refcnt > 0);
10658 			act->dta_refcnt++;
10659 			ecb->dte_action = act;
10660 			ecb->dte_action_last = cached->dte_action_last;
10661 			ecb->dte_needed = cached->dte_needed;
10662 			ecb->dte_size = cached->dte_size;
10663 			ecb->dte_alignment = cached->dte_alignment;
10664 		}
10665 
10666 		return (ecb);
10667 	}
10668 
10669 	for (act = desc->dted_action; act != NULL; act = act->dtad_next) {
10670 		if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) {
10671 			dtrace_ecb_destroy(ecb);
10672 			return (NULL);
10673 		}
10674 	}
10675 
10676 	dtrace_ecb_resize(ecb);
10677 
10678 	return (dtrace_ecb_create_cache = ecb);
10679 }
10680 
10681 static int
10682 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg)
10683 {
10684 	dtrace_ecb_t *ecb;
10685 	dtrace_enabling_t *enab = arg;
10686 	dtrace_state_t *state = enab->dten_vstate->dtvs_state;
10687 
10688 	ASSERT(state != NULL);
10689 
10690 	if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) {
10691 		/*
10692 		 * This probe was created in a generation for which this
10693 		 * enabling has previously created ECBs; we don't want to
10694 		 * enable it again, so just kick out.
10695 		 */
10696 		return (DTRACE_MATCH_NEXT);
10697 	}
10698 
10699 	if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL)
10700 		return (DTRACE_MATCH_DONE);
10701 
10702 	dtrace_ecb_enable(ecb);
10703 	return (DTRACE_MATCH_NEXT);
10704 }
10705 
10706 static dtrace_ecb_t *
10707 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id)
10708 {
10709 	dtrace_ecb_t *ecb;
10710 
10711 	ASSERT(MUTEX_HELD(&dtrace_lock));
10712 
10713 	if (id == 0 || id > state->dts_necbs)
10714 		return (NULL);
10715 
10716 	ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL);
10717 	ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id);
10718 
10719 	return (state->dts_ecbs[id - 1]);
10720 }
10721 
10722 static dtrace_aggregation_t *
10723 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id)
10724 {
10725 	dtrace_aggregation_t *agg;
10726 
10727 	ASSERT(MUTEX_HELD(&dtrace_lock));
10728 
10729 	if (id == 0 || id > state->dts_naggregations)
10730 		return (NULL);
10731 
10732 	ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL);
10733 	ASSERT((agg = state->dts_aggregations[id - 1]) == NULL ||
10734 	    agg->dtag_id == id);
10735 
10736 	return (state->dts_aggregations[id - 1]);
10737 }
10738 
10739 /*
10740  * DTrace Buffer Functions
10741  *
10742  * The following functions manipulate DTrace buffers.  Most of these functions
10743  * are called in the context of establishing or processing consumer state;
10744  * exceptions are explicitly noted.
10745  */
10746 
10747 /*
10748  * Note:  called from cross call context.  This function switches the two
10749  * buffers on a given CPU.  The atomicity of this operation is assured by
10750  * disabling interrupts while the actual switch takes place; the disabling of
10751  * interrupts serializes the execution with any execution of dtrace_probe() on
10752  * the same CPU.
10753  */
10754 static void
10755 dtrace_buffer_switch(dtrace_buffer_t *buf)
10756 {
10757 	caddr_t tomax = buf->dtb_tomax;
10758 	caddr_t xamot = buf->dtb_xamot;
10759 	dtrace_icookie_t cookie;
10760 	hrtime_t now;
10761 
10762 	ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
10763 	ASSERT(!(buf->dtb_flags & DTRACEBUF_RING));
10764 
10765 	cookie = dtrace_interrupt_disable();
10766 	now = dtrace_gethrtime();
10767 	buf->dtb_tomax = xamot;
10768 	buf->dtb_xamot = tomax;
10769 	buf->dtb_xamot_drops = buf->dtb_drops;
10770 	buf->dtb_xamot_offset = buf->dtb_offset;
10771 	buf->dtb_xamot_errors = buf->dtb_errors;
10772 	buf->dtb_xamot_flags = buf->dtb_flags;
10773 	buf->dtb_offset = 0;
10774 	buf->dtb_drops = 0;
10775 	buf->dtb_errors = 0;
10776 	buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED);
10777 	buf->dtb_interval = now - buf->dtb_switched;
10778 	buf->dtb_switched = now;
10779 	dtrace_interrupt_enable(cookie);
10780 }
10781 
10782 /*
10783  * Note:  called from cross call context.  This function activates a buffer
10784  * on a CPU.  As with dtrace_buffer_switch(), the atomicity of the operation
10785  * is guaranteed by the disabling of interrupts.
10786  */
10787 static void
10788 dtrace_buffer_activate(dtrace_state_t *state)
10789 {
10790 	dtrace_buffer_t *buf;
10791 	dtrace_icookie_t cookie = dtrace_interrupt_disable();
10792 
10793 	buf = &state->dts_buffer[curcpu];
10794 
10795 	if (buf->dtb_tomax != NULL) {
10796 		/*
10797 		 * We might like to assert that the buffer is marked inactive,
10798 		 * but this isn't necessarily true:  the buffer for the CPU
10799 		 * that processes the BEGIN probe has its buffer activated
10800 		 * manually.  In this case, we take the (harmless) action
10801 		 * re-clearing the bit INACTIVE bit.
10802 		 */
10803 		buf->dtb_flags &= ~DTRACEBUF_INACTIVE;
10804 	}
10805 
10806 	dtrace_interrupt_enable(cookie);
10807 }
10808 
10809 static int
10810 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags,
10811     processorid_t cpu)
10812 {
10813 #if defined(sun)
10814 	cpu_t *cp;
10815 #endif
10816 	dtrace_buffer_t *buf;
10817 
10818 #if defined(sun)
10819 	ASSERT(MUTEX_HELD(&cpu_lock));
10820 	ASSERT(MUTEX_HELD(&dtrace_lock));
10821 
10822 	if (size > dtrace_nonroot_maxsize &&
10823 	    !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE))
10824 		return (EFBIG);
10825 
10826 	cp = cpu_list;
10827 
10828 	do {
10829 		if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
10830 			continue;
10831 
10832 		buf = &bufs[cp->cpu_id];
10833 
10834 		/*
10835 		 * If there is already a buffer allocated for this CPU, it
10836 		 * is only possible that this is a DR event.  In this case,
10837 		 */
10838 		if (buf->dtb_tomax != NULL) {
10839 			ASSERT(buf->dtb_size == size);
10840 			continue;
10841 		}
10842 
10843 		ASSERT(buf->dtb_xamot == NULL);
10844 
10845 		if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
10846 			goto err;
10847 
10848 		buf->dtb_size = size;
10849 		buf->dtb_flags = flags;
10850 		buf->dtb_offset = 0;
10851 		buf->dtb_drops = 0;
10852 
10853 		if (flags & DTRACEBUF_NOSWITCH)
10854 			continue;
10855 
10856 		if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
10857 			goto err;
10858 	} while ((cp = cp->cpu_next) != cpu_list);
10859 
10860 	return (0);
10861 
10862 err:
10863 	cp = cpu_list;
10864 
10865 	do {
10866 		if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
10867 			continue;
10868 
10869 		buf = &bufs[cp->cpu_id];
10870 
10871 		if (buf->dtb_xamot != NULL) {
10872 			ASSERT(buf->dtb_tomax != NULL);
10873 			ASSERT(buf->dtb_size == size);
10874 			kmem_free(buf->dtb_xamot, size);
10875 		}
10876 
10877 		if (buf->dtb_tomax != NULL) {
10878 			ASSERT(buf->dtb_size == size);
10879 			kmem_free(buf->dtb_tomax, size);
10880 		}
10881 
10882 		buf->dtb_tomax = NULL;
10883 		buf->dtb_xamot = NULL;
10884 		buf->dtb_size = 0;
10885 	} while ((cp = cp->cpu_next) != cpu_list);
10886 
10887 	return (ENOMEM);
10888 #else
10889 	int i;
10890 
10891 #if defined(__amd64__) || defined(__mips__) || defined(__powerpc__)
10892 	/*
10893 	 * FreeBSD isn't good at limiting the amount of memory we
10894 	 * ask to malloc, so let's place a limit here before trying
10895 	 * to do something that might well end in tears at bedtime.
10896 	 */
10897 	if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1)))
10898 		return(ENOMEM);
10899 #endif
10900 
10901 	ASSERT(MUTEX_HELD(&dtrace_lock));
10902 	CPU_FOREACH(i) {
10903 		if (cpu != DTRACE_CPUALL && cpu != i)
10904 			continue;
10905 
10906 		buf = &bufs[i];
10907 
10908 		/*
10909 		 * If there is already a buffer allocated for this CPU, it
10910 		 * is only possible that this is a DR event.  In this case,
10911 		 * the buffer size must match our specified size.
10912 		 */
10913 		if (buf->dtb_tomax != NULL) {
10914 			ASSERT(buf->dtb_size == size);
10915 			continue;
10916 		}
10917 
10918 		ASSERT(buf->dtb_xamot == NULL);
10919 
10920 		if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
10921 			goto err;
10922 
10923 		buf->dtb_size = size;
10924 		buf->dtb_flags = flags;
10925 		buf->dtb_offset = 0;
10926 		buf->dtb_drops = 0;
10927 
10928 		if (flags & DTRACEBUF_NOSWITCH)
10929 			continue;
10930 
10931 		if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
10932 			goto err;
10933 	}
10934 
10935 	return (0);
10936 
10937 err:
10938 	/*
10939 	 * Error allocating memory, so free the buffers that were
10940 	 * allocated before the failed allocation.
10941 	 */
10942 	CPU_FOREACH(i) {
10943 		if (cpu != DTRACE_CPUALL && cpu != i)
10944 			continue;
10945 
10946 		buf = &bufs[i];
10947 
10948 		if (buf->dtb_xamot != NULL) {
10949 			ASSERT(buf->dtb_tomax != NULL);
10950 			ASSERT(buf->dtb_size == size);
10951 			kmem_free(buf->dtb_xamot, size);
10952 		}
10953 
10954 		if (buf->dtb_tomax != NULL) {
10955 			ASSERT(buf->dtb_size == size);
10956 			kmem_free(buf->dtb_tomax, size);
10957 		}
10958 
10959 		buf->dtb_tomax = NULL;
10960 		buf->dtb_xamot = NULL;
10961 		buf->dtb_size = 0;
10962 
10963 	}
10964 
10965 	return (ENOMEM);
10966 #endif
10967 }
10968 
10969 /*
10970  * Note:  called from probe context.  This function just increments the drop
10971  * count on a buffer.  It has been made a function to allow for the
10972  * possibility of understanding the source of mysterious drop counts.  (A
10973  * problem for which one may be particularly disappointed that DTrace cannot
10974  * be used to understand DTrace.)
10975  */
10976 static void
10977 dtrace_buffer_drop(dtrace_buffer_t *buf)
10978 {
10979 	buf->dtb_drops++;
10980 }
10981 
10982 /*
10983  * Note:  called from probe context.  This function is called to reserve space
10984  * in a buffer.  If mstate is non-NULL, sets the scratch base and size in the
10985  * mstate.  Returns the new offset in the buffer, or a negative value if an
10986  * error has occurred.
10987  */
10988 static intptr_t
10989 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align,
10990     dtrace_state_t *state, dtrace_mstate_t *mstate)
10991 {
10992 	intptr_t offs = buf->dtb_offset, soffs;
10993 	intptr_t woffs;
10994 	caddr_t tomax;
10995 	size_t total;
10996 
10997 	if (buf->dtb_flags & DTRACEBUF_INACTIVE)
10998 		return (-1);
10999 
11000 	if ((tomax = buf->dtb_tomax) == NULL) {
11001 		dtrace_buffer_drop(buf);
11002 		return (-1);
11003 	}
11004 
11005 	if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) {
11006 		while (offs & (align - 1)) {
11007 			/*
11008 			 * Assert that our alignment is off by a number which
11009 			 * is itself sizeof (uint32_t) aligned.
11010 			 */
11011 			ASSERT(!((align - (offs & (align - 1))) &
11012 			    (sizeof (uint32_t) - 1)));
11013 			DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
11014 			offs += sizeof (uint32_t);
11015 		}
11016 
11017 		if ((soffs = offs + needed) > buf->dtb_size) {
11018 			dtrace_buffer_drop(buf);
11019 			return (-1);
11020 		}
11021 
11022 		if (mstate == NULL)
11023 			return (offs);
11024 
11025 		mstate->dtms_scratch_base = (uintptr_t)tomax + soffs;
11026 		mstate->dtms_scratch_size = buf->dtb_size - soffs;
11027 		mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
11028 
11029 		return (offs);
11030 	}
11031 
11032 	if (buf->dtb_flags & DTRACEBUF_FILL) {
11033 		if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN &&
11034 		    (buf->dtb_flags & DTRACEBUF_FULL))
11035 			return (-1);
11036 		goto out;
11037 	}
11038 
11039 	total = needed + (offs & (align - 1));
11040 
11041 	/*
11042 	 * For a ring buffer, life is quite a bit more complicated.  Before
11043 	 * we can store any padding, we need to adjust our wrapping offset.
11044 	 * (If we've never before wrapped or we're not about to, no adjustment
11045 	 * is required.)
11046 	 */
11047 	if ((buf->dtb_flags & DTRACEBUF_WRAPPED) ||
11048 	    offs + total > buf->dtb_size) {
11049 		woffs = buf->dtb_xamot_offset;
11050 
11051 		if (offs + total > buf->dtb_size) {
11052 			/*
11053 			 * We can't fit in the end of the buffer.  First, a
11054 			 * sanity check that we can fit in the buffer at all.
11055 			 */
11056 			if (total > buf->dtb_size) {
11057 				dtrace_buffer_drop(buf);
11058 				return (-1);
11059 			}
11060 
11061 			/*
11062 			 * We're going to be storing at the top of the buffer,
11063 			 * so now we need to deal with the wrapped offset.  We
11064 			 * only reset our wrapped offset to 0 if it is
11065 			 * currently greater than the current offset.  If it
11066 			 * is less than the current offset, it is because a
11067 			 * previous allocation induced a wrap -- but the
11068 			 * allocation didn't subsequently take the space due
11069 			 * to an error or false predicate evaluation.  In this
11070 			 * case, we'll just leave the wrapped offset alone: if
11071 			 * the wrapped offset hasn't been advanced far enough
11072 			 * for this allocation, it will be adjusted in the
11073 			 * lower loop.
11074 			 */
11075 			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
11076 				if (woffs >= offs)
11077 					woffs = 0;
11078 			} else {
11079 				woffs = 0;
11080 			}
11081 
11082 			/*
11083 			 * Now we know that we're going to be storing to the
11084 			 * top of the buffer and that there is room for us
11085 			 * there.  We need to clear the buffer from the current
11086 			 * offset to the end (there may be old gunk there).
11087 			 */
11088 			while (offs < buf->dtb_size)
11089 				tomax[offs++] = 0;
11090 
11091 			/*
11092 			 * We need to set our offset to zero.  And because we
11093 			 * are wrapping, we need to set the bit indicating as
11094 			 * much.  We can also adjust our needed space back
11095 			 * down to the space required by the ECB -- we know
11096 			 * that the top of the buffer is aligned.
11097 			 */
11098 			offs = 0;
11099 			total = needed;
11100 			buf->dtb_flags |= DTRACEBUF_WRAPPED;
11101 		} else {
11102 			/*
11103 			 * There is room for us in the buffer, so we simply
11104 			 * need to check the wrapped offset.
11105 			 */
11106 			if (woffs < offs) {
11107 				/*
11108 				 * The wrapped offset is less than the offset.
11109 				 * This can happen if we allocated buffer space
11110 				 * that induced a wrap, but then we didn't
11111 				 * subsequently take the space due to an error
11112 				 * or false predicate evaluation.  This is
11113 				 * okay; we know that _this_ allocation isn't
11114 				 * going to induce a wrap.  We still can't
11115 				 * reset the wrapped offset to be zero,
11116 				 * however: the space may have been trashed in
11117 				 * the previous failed probe attempt.  But at
11118 				 * least the wrapped offset doesn't need to
11119 				 * be adjusted at all...
11120 				 */
11121 				goto out;
11122 			}
11123 		}
11124 
11125 		while (offs + total > woffs) {
11126 			dtrace_epid_t epid = *(uint32_t *)(tomax + woffs);
11127 			size_t size;
11128 
11129 			if (epid == DTRACE_EPIDNONE) {
11130 				size = sizeof (uint32_t);
11131 			} else {
11132 				ASSERT3U(epid, <=, state->dts_necbs);
11133 				ASSERT(state->dts_ecbs[epid - 1] != NULL);
11134 
11135 				size = state->dts_ecbs[epid - 1]->dte_size;
11136 			}
11137 
11138 			ASSERT(woffs + size <= buf->dtb_size);
11139 			ASSERT(size != 0);
11140 
11141 			if (woffs + size == buf->dtb_size) {
11142 				/*
11143 				 * We've reached the end of the buffer; we want
11144 				 * to set the wrapped offset to 0 and break
11145 				 * out.  However, if the offs is 0, then we're
11146 				 * in a strange edge-condition:  the amount of
11147 				 * space that we want to reserve plus the size
11148 				 * of the record that we're overwriting is
11149 				 * greater than the size of the buffer.  This
11150 				 * is problematic because if we reserve the
11151 				 * space but subsequently don't consume it (due
11152 				 * to a failed predicate or error) the wrapped
11153 				 * offset will be 0 -- yet the EPID at offset 0
11154 				 * will not be committed.  This situation is
11155 				 * relatively easy to deal with:  if we're in
11156 				 * this case, the buffer is indistinguishable
11157 				 * from one that hasn't wrapped; we need only
11158 				 * finish the job by clearing the wrapped bit,
11159 				 * explicitly setting the offset to be 0, and
11160 				 * zero'ing out the old data in the buffer.
11161 				 */
11162 				if (offs == 0) {
11163 					buf->dtb_flags &= ~DTRACEBUF_WRAPPED;
11164 					buf->dtb_offset = 0;
11165 					woffs = total;
11166 
11167 					while (woffs < buf->dtb_size)
11168 						tomax[woffs++] = 0;
11169 				}
11170 
11171 				woffs = 0;
11172 				break;
11173 			}
11174 
11175 			woffs += size;
11176 		}
11177 
11178 		/*
11179 		 * We have a wrapped offset.  It may be that the wrapped offset
11180 		 * has become zero -- that's okay.
11181 		 */
11182 		buf->dtb_xamot_offset = woffs;
11183 	}
11184 
11185 out:
11186 	/*
11187 	 * Now we can plow the buffer with any necessary padding.
11188 	 */
11189 	while (offs & (align - 1)) {
11190 		/*
11191 		 * Assert that our alignment is off by a number which
11192 		 * is itself sizeof (uint32_t) aligned.
11193 		 */
11194 		ASSERT(!((align - (offs & (align - 1))) &
11195 		    (sizeof (uint32_t) - 1)));
11196 		DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
11197 		offs += sizeof (uint32_t);
11198 	}
11199 
11200 	if (buf->dtb_flags & DTRACEBUF_FILL) {
11201 		if (offs + needed > buf->dtb_size - state->dts_reserve) {
11202 			buf->dtb_flags |= DTRACEBUF_FULL;
11203 			return (-1);
11204 		}
11205 	}
11206 
11207 	if (mstate == NULL)
11208 		return (offs);
11209 
11210 	/*
11211 	 * For ring buffers and fill buffers, the scratch space is always
11212 	 * the inactive buffer.
11213 	 */
11214 	mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot;
11215 	mstate->dtms_scratch_size = buf->dtb_size;
11216 	mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
11217 
11218 	return (offs);
11219 }
11220 
11221 static void
11222 dtrace_buffer_polish(dtrace_buffer_t *buf)
11223 {
11224 	ASSERT(buf->dtb_flags & DTRACEBUF_RING);
11225 	ASSERT(MUTEX_HELD(&dtrace_lock));
11226 
11227 	if (!(buf->dtb_flags & DTRACEBUF_WRAPPED))
11228 		return;
11229 
11230 	/*
11231 	 * We need to polish the ring buffer.  There are three cases:
11232 	 *
11233 	 * - The first (and presumably most common) is that there is no gap
11234 	 *   between the buffer offset and the wrapped offset.  In this case,
11235 	 *   there is nothing in the buffer that isn't valid data; we can
11236 	 *   mark the buffer as polished and return.
11237 	 *
11238 	 * - The second (less common than the first but still more common
11239 	 *   than the third) is that there is a gap between the buffer offset
11240 	 *   and the wrapped offset, and the wrapped offset is larger than the
11241 	 *   buffer offset.  This can happen because of an alignment issue, or
11242 	 *   can happen because of a call to dtrace_buffer_reserve() that
11243 	 *   didn't subsequently consume the buffer space.  In this case,
11244 	 *   we need to zero the data from the buffer offset to the wrapped
11245 	 *   offset.
11246 	 *
11247 	 * - The third (and least common) is that there is a gap between the
11248 	 *   buffer offset and the wrapped offset, but the wrapped offset is
11249 	 *   _less_ than the buffer offset.  This can only happen because a
11250 	 *   call to dtrace_buffer_reserve() induced a wrap, but the space
11251 	 *   was not subsequently consumed.  In this case, we need to zero the
11252 	 *   space from the offset to the end of the buffer _and_ from the
11253 	 *   top of the buffer to the wrapped offset.
11254 	 */
11255 	if (buf->dtb_offset < buf->dtb_xamot_offset) {
11256 		bzero(buf->dtb_tomax + buf->dtb_offset,
11257 		    buf->dtb_xamot_offset - buf->dtb_offset);
11258 	}
11259 
11260 	if (buf->dtb_offset > buf->dtb_xamot_offset) {
11261 		bzero(buf->dtb_tomax + buf->dtb_offset,
11262 		    buf->dtb_size - buf->dtb_offset);
11263 		bzero(buf->dtb_tomax, buf->dtb_xamot_offset);
11264 	}
11265 }
11266 
11267 /*
11268  * This routine determines if data generated at the specified time has likely
11269  * been entirely consumed at user-level.  This routine is called to determine
11270  * if an ECB on a defunct probe (but for an active enabling) can be safely
11271  * disabled and destroyed.
11272  */
11273 static int
11274 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when)
11275 {
11276 	int i;
11277 
11278 	for (i = 0; i < NCPU; i++) {
11279 		dtrace_buffer_t *buf = &bufs[i];
11280 
11281 		if (buf->dtb_size == 0)
11282 			continue;
11283 
11284 		if (buf->dtb_flags & DTRACEBUF_RING)
11285 			return (0);
11286 
11287 		if (!buf->dtb_switched && buf->dtb_offset != 0)
11288 			return (0);
11289 
11290 		if (buf->dtb_switched - buf->dtb_interval < when)
11291 			return (0);
11292 	}
11293 
11294 	return (1);
11295 }
11296 
11297 static void
11298 dtrace_buffer_free(dtrace_buffer_t *bufs)
11299 {
11300 	int i;
11301 
11302 	for (i = 0; i < NCPU; i++) {
11303 		dtrace_buffer_t *buf = &bufs[i];
11304 
11305 		if (buf->dtb_tomax == NULL) {
11306 			ASSERT(buf->dtb_xamot == NULL);
11307 			ASSERT(buf->dtb_size == 0);
11308 			continue;
11309 		}
11310 
11311 		if (buf->dtb_xamot != NULL) {
11312 			ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
11313 			kmem_free(buf->dtb_xamot, buf->dtb_size);
11314 		}
11315 
11316 		kmem_free(buf->dtb_tomax, buf->dtb_size);
11317 		buf->dtb_size = 0;
11318 		buf->dtb_tomax = NULL;
11319 		buf->dtb_xamot = NULL;
11320 	}
11321 }
11322 
11323 /*
11324  * DTrace Enabling Functions
11325  */
11326 static dtrace_enabling_t *
11327 dtrace_enabling_create(dtrace_vstate_t *vstate)
11328 {
11329 	dtrace_enabling_t *enab;
11330 
11331 	enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP);
11332 	enab->dten_vstate = vstate;
11333 
11334 	return (enab);
11335 }
11336 
11337 static void
11338 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb)
11339 {
11340 	dtrace_ecbdesc_t **ndesc;
11341 	size_t osize, nsize;
11342 
11343 	/*
11344 	 * We can't add to enablings after we've enabled them, or after we've
11345 	 * retained them.
11346 	 */
11347 	ASSERT(enab->dten_probegen == 0);
11348 	ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
11349 
11350 	if (enab->dten_ndesc < enab->dten_maxdesc) {
11351 		enab->dten_desc[enab->dten_ndesc++] = ecb;
11352 		return;
11353 	}
11354 
11355 	osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
11356 
11357 	if (enab->dten_maxdesc == 0) {
11358 		enab->dten_maxdesc = 1;
11359 	} else {
11360 		enab->dten_maxdesc <<= 1;
11361 	}
11362 
11363 	ASSERT(enab->dten_ndesc < enab->dten_maxdesc);
11364 
11365 	nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
11366 	ndesc = kmem_zalloc(nsize, KM_SLEEP);
11367 	bcopy(enab->dten_desc, ndesc, osize);
11368 	if (enab->dten_desc != NULL)
11369 		kmem_free(enab->dten_desc, osize);
11370 
11371 	enab->dten_desc = ndesc;
11372 	enab->dten_desc[enab->dten_ndesc++] = ecb;
11373 }
11374 
11375 static void
11376 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb,
11377     dtrace_probedesc_t *pd)
11378 {
11379 	dtrace_ecbdesc_t *new;
11380 	dtrace_predicate_t *pred;
11381 	dtrace_actdesc_t *act;
11382 
11383 	/*
11384 	 * We're going to create a new ECB description that matches the
11385 	 * specified ECB in every way, but has the specified probe description.
11386 	 */
11387 	new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
11388 
11389 	if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL)
11390 		dtrace_predicate_hold(pred);
11391 
11392 	for (act = ecb->dted_action; act != NULL; act = act->dtad_next)
11393 		dtrace_actdesc_hold(act);
11394 
11395 	new->dted_action = ecb->dted_action;
11396 	new->dted_pred = ecb->dted_pred;
11397 	new->dted_probe = *pd;
11398 	new->dted_uarg = ecb->dted_uarg;
11399 
11400 	dtrace_enabling_add(enab, new);
11401 }
11402 
11403 static void
11404 dtrace_enabling_dump(dtrace_enabling_t *enab)
11405 {
11406 	int i;
11407 
11408 	for (i = 0; i < enab->dten_ndesc; i++) {
11409 		dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe;
11410 
11411 		cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i,
11412 		    desc->dtpd_provider, desc->dtpd_mod,
11413 		    desc->dtpd_func, desc->dtpd_name);
11414 	}
11415 }
11416 
11417 static void
11418 dtrace_enabling_destroy(dtrace_enabling_t *enab)
11419 {
11420 	int i;
11421 	dtrace_ecbdesc_t *ep;
11422 	dtrace_vstate_t *vstate = enab->dten_vstate;
11423 
11424 	ASSERT(MUTEX_HELD(&dtrace_lock));
11425 
11426 	for (i = 0; i < enab->dten_ndesc; i++) {
11427 		dtrace_actdesc_t *act, *next;
11428 		dtrace_predicate_t *pred;
11429 
11430 		ep = enab->dten_desc[i];
11431 
11432 		if ((pred = ep->dted_pred.dtpdd_predicate) != NULL)
11433 			dtrace_predicate_release(pred, vstate);
11434 
11435 		for (act = ep->dted_action; act != NULL; act = next) {
11436 			next = act->dtad_next;
11437 			dtrace_actdesc_release(act, vstate);
11438 		}
11439 
11440 		kmem_free(ep, sizeof (dtrace_ecbdesc_t));
11441 	}
11442 
11443 	if (enab->dten_desc != NULL)
11444 		kmem_free(enab->dten_desc,
11445 		    enab->dten_maxdesc * sizeof (dtrace_enabling_t *));
11446 
11447 	/*
11448 	 * If this was a retained enabling, decrement the dts_nretained count
11449 	 * and take it off of the dtrace_retained list.
11450 	 */
11451 	if (enab->dten_prev != NULL || enab->dten_next != NULL ||
11452 	    dtrace_retained == enab) {
11453 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
11454 		ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0);
11455 		enab->dten_vstate->dtvs_state->dts_nretained--;
11456 	}
11457 
11458 	if (enab->dten_prev == NULL) {
11459 		if (dtrace_retained == enab) {
11460 			dtrace_retained = enab->dten_next;
11461 
11462 			if (dtrace_retained != NULL)
11463 				dtrace_retained->dten_prev = NULL;
11464 		}
11465 	} else {
11466 		ASSERT(enab != dtrace_retained);
11467 		ASSERT(dtrace_retained != NULL);
11468 		enab->dten_prev->dten_next = enab->dten_next;
11469 	}
11470 
11471 	if (enab->dten_next != NULL) {
11472 		ASSERT(dtrace_retained != NULL);
11473 		enab->dten_next->dten_prev = enab->dten_prev;
11474 	}
11475 
11476 	kmem_free(enab, sizeof (dtrace_enabling_t));
11477 }
11478 
11479 static int
11480 dtrace_enabling_retain(dtrace_enabling_t *enab)
11481 {
11482 	dtrace_state_t *state;
11483 
11484 	ASSERT(MUTEX_HELD(&dtrace_lock));
11485 	ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
11486 	ASSERT(enab->dten_vstate != NULL);
11487 
11488 	state = enab->dten_vstate->dtvs_state;
11489 	ASSERT(state != NULL);
11490 
11491 	/*
11492 	 * We only allow each state to retain dtrace_retain_max enablings.
11493 	 */
11494 	if (state->dts_nretained >= dtrace_retain_max)
11495 		return (ENOSPC);
11496 
11497 	state->dts_nretained++;
11498 
11499 	if (dtrace_retained == NULL) {
11500 		dtrace_retained = enab;
11501 		return (0);
11502 	}
11503 
11504 	enab->dten_next = dtrace_retained;
11505 	dtrace_retained->dten_prev = enab;
11506 	dtrace_retained = enab;
11507 
11508 	return (0);
11509 }
11510 
11511 static int
11512 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match,
11513     dtrace_probedesc_t *create)
11514 {
11515 	dtrace_enabling_t *new, *enab;
11516 	int found = 0, err = ENOENT;
11517 
11518 	ASSERT(MUTEX_HELD(&dtrace_lock));
11519 	ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN);
11520 	ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN);
11521 	ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN);
11522 	ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN);
11523 
11524 	new = dtrace_enabling_create(&state->dts_vstate);
11525 
11526 	/*
11527 	 * Iterate over all retained enablings, looking for enablings that
11528 	 * match the specified state.
11529 	 */
11530 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11531 		int i;
11532 
11533 		/*
11534 		 * dtvs_state can only be NULL for helper enablings -- and
11535 		 * helper enablings can't be retained.
11536 		 */
11537 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
11538 
11539 		if (enab->dten_vstate->dtvs_state != state)
11540 			continue;
11541 
11542 		/*
11543 		 * Now iterate over each probe description; we're looking for
11544 		 * an exact match to the specified probe description.
11545 		 */
11546 		for (i = 0; i < enab->dten_ndesc; i++) {
11547 			dtrace_ecbdesc_t *ep = enab->dten_desc[i];
11548 			dtrace_probedesc_t *pd = &ep->dted_probe;
11549 
11550 			if (strcmp(pd->dtpd_provider, match->dtpd_provider))
11551 				continue;
11552 
11553 			if (strcmp(pd->dtpd_mod, match->dtpd_mod))
11554 				continue;
11555 
11556 			if (strcmp(pd->dtpd_func, match->dtpd_func))
11557 				continue;
11558 
11559 			if (strcmp(pd->dtpd_name, match->dtpd_name))
11560 				continue;
11561 
11562 			/*
11563 			 * We have a winning probe!  Add it to our growing
11564 			 * enabling.
11565 			 */
11566 			found = 1;
11567 			dtrace_enabling_addlike(new, ep, create);
11568 		}
11569 	}
11570 
11571 	if (!found || (err = dtrace_enabling_retain(new)) != 0) {
11572 		dtrace_enabling_destroy(new);
11573 		return (err);
11574 	}
11575 
11576 	return (0);
11577 }
11578 
11579 static void
11580 dtrace_enabling_retract(dtrace_state_t *state)
11581 {
11582 	dtrace_enabling_t *enab, *next;
11583 
11584 	ASSERT(MUTEX_HELD(&dtrace_lock));
11585 
11586 	/*
11587 	 * Iterate over all retained enablings, destroy the enablings retained
11588 	 * for the specified state.
11589 	 */
11590 	for (enab = dtrace_retained; enab != NULL; enab = next) {
11591 		next = enab->dten_next;
11592 
11593 		/*
11594 		 * dtvs_state can only be NULL for helper enablings -- and
11595 		 * helper enablings can't be retained.
11596 		 */
11597 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
11598 
11599 		if (enab->dten_vstate->dtvs_state == state) {
11600 			ASSERT(state->dts_nretained > 0);
11601 			dtrace_enabling_destroy(enab);
11602 		}
11603 	}
11604 
11605 	ASSERT(state->dts_nretained == 0);
11606 }
11607 
11608 static int
11609 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched)
11610 {
11611 	int i = 0;
11612 	int matched = 0;
11613 
11614 	ASSERT(MUTEX_HELD(&cpu_lock));
11615 	ASSERT(MUTEX_HELD(&dtrace_lock));
11616 
11617 	for (i = 0; i < enab->dten_ndesc; i++) {
11618 		dtrace_ecbdesc_t *ep = enab->dten_desc[i];
11619 
11620 		enab->dten_current = ep;
11621 		enab->dten_error = 0;
11622 
11623 		matched += dtrace_probe_enable(&ep->dted_probe, enab);
11624 
11625 		if (enab->dten_error != 0) {
11626 			/*
11627 			 * If we get an error half-way through enabling the
11628 			 * probes, we kick out -- perhaps with some number of
11629 			 * them enabled.  Leaving enabled probes enabled may
11630 			 * be slightly confusing for user-level, but we expect
11631 			 * that no one will attempt to actually drive on in
11632 			 * the face of such errors.  If this is an anonymous
11633 			 * enabling (indicated with a NULL nmatched pointer),
11634 			 * we cmn_err() a message.  We aren't expecting to
11635 			 * get such an error -- such as it can exist at all,
11636 			 * it would be a result of corrupted DOF in the driver
11637 			 * properties.
11638 			 */
11639 			if (nmatched == NULL) {
11640 				cmn_err(CE_WARN, "dtrace_enabling_match() "
11641 				    "error on %p: %d", (void *)ep,
11642 				    enab->dten_error);
11643 			}
11644 
11645 			return (enab->dten_error);
11646 		}
11647 	}
11648 
11649 	enab->dten_probegen = dtrace_probegen;
11650 	if (nmatched != NULL)
11651 		*nmatched = matched;
11652 
11653 	return (0);
11654 }
11655 
11656 static void
11657 dtrace_enabling_matchall(void)
11658 {
11659 	dtrace_enabling_t *enab;
11660 
11661 	mutex_enter(&cpu_lock);
11662 	mutex_enter(&dtrace_lock);
11663 
11664 	/*
11665 	 * Iterate over all retained enablings to see if any probes match
11666 	 * against them.  We only perform this operation on enablings for which
11667 	 * we have sufficient permissions by virtue of being in the global zone
11668 	 * or in the same zone as the DTrace client.  Because we can be called
11669 	 * after dtrace_detach() has been called, we cannot assert that there
11670 	 * are retained enablings.  We can safely load from dtrace_retained,
11671 	 * however:  the taskq_destroy() at the end of dtrace_detach() will
11672 	 * block pending our completion.
11673 	 */
11674 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11675 #if defined(sun)
11676 		cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred;
11677 
11678 		if (INGLOBALZONE(curproc) || getzoneid() == crgetzoneid(cr))
11679 #endif
11680 			(void) dtrace_enabling_match(enab, NULL);
11681 	}
11682 
11683 	mutex_exit(&dtrace_lock);
11684 	mutex_exit(&cpu_lock);
11685 }
11686 
11687 /*
11688  * If an enabling is to be enabled without having matched probes (that is, if
11689  * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
11690  * enabling must be _primed_ by creating an ECB for every ECB description.
11691  * This must be done to assure that we know the number of speculations, the
11692  * number of aggregations, the minimum buffer size needed, etc. before we
11693  * transition out of DTRACE_ACTIVITY_INACTIVE.  To do this without actually
11694  * enabling any probes, we create ECBs for every ECB decription, but with a
11695  * NULL probe -- which is exactly what this function does.
11696  */
11697 static void
11698 dtrace_enabling_prime(dtrace_state_t *state)
11699 {
11700 	dtrace_enabling_t *enab;
11701 	int i;
11702 
11703 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11704 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
11705 
11706 		if (enab->dten_vstate->dtvs_state != state)
11707 			continue;
11708 
11709 		/*
11710 		 * We don't want to prime an enabling more than once, lest
11711 		 * we allow a malicious user to induce resource exhaustion.
11712 		 * (The ECBs that result from priming an enabling aren't
11713 		 * leaked -- but they also aren't deallocated until the
11714 		 * consumer state is destroyed.)
11715 		 */
11716 		if (enab->dten_primed)
11717 			continue;
11718 
11719 		for (i = 0; i < enab->dten_ndesc; i++) {
11720 			enab->dten_current = enab->dten_desc[i];
11721 			(void) dtrace_probe_enable(NULL, enab);
11722 		}
11723 
11724 		enab->dten_primed = 1;
11725 	}
11726 }
11727 
11728 /*
11729  * Called to indicate that probes should be provided due to retained
11730  * enablings.  This is implemented in terms of dtrace_probe_provide(), but it
11731  * must take an initial lap through the enabling calling the dtps_provide()
11732  * entry point explicitly to allow for autocreated probes.
11733  */
11734 static void
11735 dtrace_enabling_provide(dtrace_provider_t *prv)
11736 {
11737 	int i, all = 0;
11738 	dtrace_probedesc_t desc;
11739 
11740 	ASSERT(MUTEX_HELD(&dtrace_lock));
11741 	ASSERT(MUTEX_HELD(&dtrace_provider_lock));
11742 
11743 	if (prv == NULL) {
11744 		all = 1;
11745 		prv = dtrace_provider;
11746 	}
11747 
11748 	do {
11749 		dtrace_enabling_t *enab = dtrace_retained;
11750 		void *parg = prv->dtpv_arg;
11751 
11752 		for (; enab != NULL; enab = enab->dten_next) {
11753 			for (i = 0; i < enab->dten_ndesc; i++) {
11754 				desc = enab->dten_desc[i]->dted_probe;
11755 				mutex_exit(&dtrace_lock);
11756 				prv->dtpv_pops.dtps_provide(parg, &desc);
11757 				mutex_enter(&dtrace_lock);
11758 			}
11759 		}
11760 	} while (all && (prv = prv->dtpv_next) != NULL);
11761 
11762 	mutex_exit(&dtrace_lock);
11763 	dtrace_probe_provide(NULL, all ? NULL : prv);
11764 	mutex_enter(&dtrace_lock);
11765 }
11766 
11767 /*
11768  * Called to reap ECBs that are attached to probes from defunct providers.
11769  */
11770 static void
11771 dtrace_enabling_reap(void)
11772 {
11773 	dtrace_provider_t *prov;
11774 	dtrace_probe_t *probe;
11775 	dtrace_ecb_t *ecb;
11776 	hrtime_t when;
11777 	int i;
11778 
11779 	mutex_enter(&cpu_lock);
11780 	mutex_enter(&dtrace_lock);
11781 
11782 	for (i = 0; i < dtrace_nprobes; i++) {
11783 		if ((probe = dtrace_probes[i]) == NULL)
11784 			continue;
11785 
11786 		if (probe->dtpr_ecb == NULL)
11787 			continue;
11788 
11789 		prov = probe->dtpr_provider;
11790 
11791 		if ((when = prov->dtpv_defunct) == 0)
11792 			continue;
11793 
11794 		/*
11795 		 * We have ECBs on a defunct provider:  we want to reap these
11796 		 * ECBs to allow the provider to unregister.  The destruction
11797 		 * of these ECBs must be done carefully:  if we destroy the ECB
11798 		 * and the consumer later wishes to consume an EPID that
11799 		 * corresponds to the destroyed ECB (and if the EPID metadata
11800 		 * has not been previously consumed), the consumer will abort
11801 		 * processing on the unknown EPID.  To reduce (but not, sadly,
11802 		 * eliminate) the possibility of this, we will only destroy an
11803 		 * ECB for a defunct provider if, for the state that
11804 		 * corresponds to the ECB:
11805 		 *
11806 		 *  (a)	There is no speculative tracing (which can effectively
11807 		 *	cache an EPID for an arbitrary amount of time).
11808 		 *
11809 		 *  (b)	The principal buffers have been switched twice since the
11810 		 *	provider became defunct.
11811 		 *
11812 		 *  (c)	The aggregation buffers are of zero size or have been
11813 		 *	switched twice since the provider became defunct.
11814 		 *
11815 		 * We use dts_speculates to determine (a) and call a function
11816 		 * (dtrace_buffer_consumed()) to determine (b) and (c).  Note
11817 		 * that as soon as we've been unable to destroy one of the ECBs
11818 		 * associated with the probe, we quit trying -- reaping is only
11819 		 * fruitful in as much as we can destroy all ECBs associated
11820 		 * with the defunct provider's probes.
11821 		 */
11822 		while ((ecb = probe->dtpr_ecb) != NULL) {
11823 			dtrace_state_t *state = ecb->dte_state;
11824 			dtrace_buffer_t *buf = state->dts_buffer;
11825 			dtrace_buffer_t *aggbuf = state->dts_aggbuffer;
11826 
11827 			if (state->dts_speculates)
11828 				break;
11829 
11830 			if (!dtrace_buffer_consumed(buf, when))
11831 				break;
11832 
11833 			if (!dtrace_buffer_consumed(aggbuf, when))
11834 				break;
11835 
11836 			dtrace_ecb_disable(ecb);
11837 			ASSERT(probe->dtpr_ecb != ecb);
11838 			dtrace_ecb_destroy(ecb);
11839 		}
11840 	}
11841 
11842 	mutex_exit(&dtrace_lock);
11843 	mutex_exit(&cpu_lock);
11844 }
11845 
11846 /*
11847  * DTrace DOF Functions
11848  */
11849 /*ARGSUSED*/
11850 static void
11851 dtrace_dof_error(dof_hdr_t *dof, const char *str)
11852 {
11853 	if (dtrace_err_verbose)
11854 		cmn_err(CE_WARN, "failed to process DOF: %s", str);
11855 
11856 #ifdef DTRACE_ERRDEBUG
11857 	dtrace_errdebug(str);
11858 #endif
11859 }
11860 
11861 /*
11862  * Create DOF out of a currently enabled state.  Right now, we only create
11863  * DOF containing the run-time options -- but this could be expanded to create
11864  * complete DOF representing the enabled state.
11865  */
11866 static dof_hdr_t *
11867 dtrace_dof_create(dtrace_state_t *state)
11868 {
11869 	dof_hdr_t *dof;
11870 	dof_sec_t *sec;
11871 	dof_optdesc_t *opt;
11872 	int i, len = sizeof (dof_hdr_t) +
11873 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
11874 	    sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
11875 
11876 	ASSERT(MUTEX_HELD(&dtrace_lock));
11877 
11878 	dof = kmem_zalloc(len, KM_SLEEP);
11879 	dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
11880 	dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
11881 	dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
11882 	dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
11883 
11884 	dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
11885 	dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
11886 	dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
11887 	dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
11888 	dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
11889 	dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
11890 
11891 	dof->dofh_flags = 0;
11892 	dof->dofh_hdrsize = sizeof (dof_hdr_t);
11893 	dof->dofh_secsize = sizeof (dof_sec_t);
11894 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
11895 	dof->dofh_secoff = sizeof (dof_hdr_t);
11896 	dof->dofh_loadsz = len;
11897 	dof->dofh_filesz = len;
11898 	dof->dofh_pad = 0;
11899 
11900 	/*
11901 	 * Fill in the option section header...
11902 	 */
11903 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
11904 	sec->dofs_type = DOF_SECT_OPTDESC;
11905 	sec->dofs_align = sizeof (uint64_t);
11906 	sec->dofs_flags = DOF_SECF_LOAD;
11907 	sec->dofs_entsize = sizeof (dof_optdesc_t);
11908 
11909 	opt = (dof_optdesc_t *)((uintptr_t)sec +
11910 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
11911 
11912 	sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
11913 	sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
11914 
11915 	for (i = 0; i < DTRACEOPT_MAX; i++) {
11916 		opt[i].dofo_option = i;
11917 		opt[i].dofo_strtab = DOF_SECIDX_NONE;
11918 		opt[i].dofo_value = state->dts_options[i];
11919 	}
11920 
11921 	return (dof);
11922 }
11923 
11924 static dof_hdr_t *
11925 dtrace_dof_copyin(uintptr_t uarg, int *errp)
11926 {
11927 	dof_hdr_t hdr, *dof;
11928 
11929 	ASSERT(!MUTEX_HELD(&dtrace_lock));
11930 
11931 	/*
11932 	 * First, we're going to copyin() the sizeof (dof_hdr_t).
11933 	 */
11934 	if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) {
11935 		dtrace_dof_error(NULL, "failed to copyin DOF header");
11936 		*errp = EFAULT;
11937 		return (NULL);
11938 	}
11939 
11940 	/*
11941 	 * Now we'll allocate the entire DOF and copy it in -- provided
11942 	 * that the length isn't outrageous.
11943 	 */
11944 	if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
11945 		dtrace_dof_error(&hdr, "load size exceeds maximum");
11946 		*errp = E2BIG;
11947 		return (NULL);
11948 	}
11949 
11950 	if (hdr.dofh_loadsz < sizeof (hdr)) {
11951 		dtrace_dof_error(&hdr, "invalid load size");
11952 		*errp = EINVAL;
11953 		return (NULL);
11954 	}
11955 
11956 	dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP);
11957 
11958 	if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) {
11959 		kmem_free(dof, hdr.dofh_loadsz);
11960 		*errp = EFAULT;
11961 		return (NULL);
11962 	}
11963 
11964 	return (dof);
11965 }
11966 
11967 #if !defined(sun)
11968 static __inline uchar_t
11969 dtrace_dof_char(char c) {
11970 	switch (c) {
11971 	case '0':
11972 	case '1':
11973 	case '2':
11974 	case '3':
11975 	case '4':
11976 	case '5':
11977 	case '6':
11978 	case '7':
11979 	case '8':
11980 	case '9':
11981 		return (c - '0');
11982 	case 'A':
11983 	case 'B':
11984 	case 'C':
11985 	case 'D':
11986 	case 'E':
11987 	case 'F':
11988 		return (c - 'A' + 10);
11989 	case 'a':
11990 	case 'b':
11991 	case 'c':
11992 	case 'd':
11993 	case 'e':
11994 	case 'f':
11995 		return (c - 'a' + 10);
11996 	}
11997 	/* Should not reach here. */
11998 	return (0);
11999 }
12000 #endif
12001 
12002 static dof_hdr_t *
12003 dtrace_dof_property(const char *name)
12004 {
12005 	uchar_t *buf;
12006 	uint64_t loadsz;
12007 	unsigned int len, i;
12008 	dof_hdr_t *dof;
12009 
12010 #if defined(sun)
12011 	/*
12012 	 * Unfortunately, array of values in .conf files are always (and
12013 	 * only) interpreted to be integer arrays.  We must read our DOF
12014 	 * as an integer array, and then squeeze it into a byte array.
12015 	 */
12016 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0,
12017 	    (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS)
12018 		return (NULL);
12019 
12020 	for (i = 0; i < len; i++)
12021 		buf[i] = (uchar_t)(((int *)buf)[i]);
12022 
12023 	if (len < sizeof (dof_hdr_t)) {
12024 		ddi_prop_free(buf);
12025 		dtrace_dof_error(NULL, "truncated header");
12026 		return (NULL);
12027 	}
12028 
12029 	if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) {
12030 		ddi_prop_free(buf);
12031 		dtrace_dof_error(NULL, "truncated DOF");
12032 		return (NULL);
12033 	}
12034 
12035 	if (loadsz >= dtrace_dof_maxsize) {
12036 		ddi_prop_free(buf);
12037 		dtrace_dof_error(NULL, "oversized DOF");
12038 		return (NULL);
12039 	}
12040 
12041 	dof = kmem_alloc(loadsz, KM_SLEEP);
12042 	bcopy(buf, dof, loadsz);
12043 	ddi_prop_free(buf);
12044 #else
12045 	char *p;
12046 	char *p_env;
12047 
12048 	if ((p_env = getenv(name)) == NULL)
12049 		return (NULL);
12050 
12051 	len = strlen(p_env) / 2;
12052 
12053 	buf = kmem_alloc(len, KM_SLEEP);
12054 
12055 	dof = (dof_hdr_t *) buf;
12056 
12057 	p = p_env;
12058 
12059 	for (i = 0; i < len; i++) {
12060 		buf[i] = (dtrace_dof_char(p[0]) << 4) |
12061 		     dtrace_dof_char(p[1]);
12062 		p += 2;
12063 	}
12064 
12065 	freeenv(p_env);
12066 
12067 	if (len < sizeof (dof_hdr_t)) {
12068 		kmem_free(buf, 0);
12069 		dtrace_dof_error(NULL, "truncated header");
12070 		return (NULL);
12071 	}
12072 
12073 	if (len < (loadsz = dof->dofh_loadsz)) {
12074 		kmem_free(buf, 0);
12075 		dtrace_dof_error(NULL, "truncated DOF");
12076 		return (NULL);
12077 	}
12078 
12079 	if (loadsz >= dtrace_dof_maxsize) {
12080 		kmem_free(buf, 0);
12081 		dtrace_dof_error(NULL, "oversized DOF");
12082 		return (NULL);
12083 	}
12084 #endif
12085 
12086 	return (dof);
12087 }
12088 
12089 static void
12090 dtrace_dof_destroy(dof_hdr_t *dof)
12091 {
12092 	kmem_free(dof, dof->dofh_loadsz);
12093 }
12094 
12095 /*
12096  * Return the dof_sec_t pointer corresponding to a given section index.  If the
12097  * index is not valid, dtrace_dof_error() is called and NULL is returned.  If
12098  * a type other than DOF_SECT_NONE is specified, the header is checked against
12099  * this type and NULL is returned if the types do not match.
12100  */
12101 static dof_sec_t *
12102 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i)
12103 {
12104 	dof_sec_t *sec = (dof_sec_t *)(uintptr_t)
12105 	    ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize);
12106 
12107 	if (i >= dof->dofh_secnum) {
12108 		dtrace_dof_error(dof, "referenced section index is invalid");
12109 		return (NULL);
12110 	}
12111 
12112 	if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
12113 		dtrace_dof_error(dof, "referenced section is not loadable");
12114 		return (NULL);
12115 	}
12116 
12117 	if (type != DOF_SECT_NONE && type != sec->dofs_type) {
12118 		dtrace_dof_error(dof, "referenced section is the wrong type");
12119 		return (NULL);
12120 	}
12121 
12122 	return (sec);
12123 }
12124 
12125 static dtrace_probedesc_t *
12126 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc)
12127 {
12128 	dof_probedesc_t *probe;
12129 	dof_sec_t *strtab;
12130 	uintptr_t daddr = (uintptr_t)dof;
12131 	uintptr_t str;
12132 	size_t size;
12133 
12134 	if (sec->dofs_type != DOF_SECT_PROBEDESC) {
12135 		dtrace_dof_error(dof, "invalid probe section");
12136 		return (NULL);
12137 	}
12138 
12139 	if (sec->dofs_align != sizeof (dof_secidx_t)) {
12140 		dtrace_dof_error(dof, "bad alignment in probe description");
12141 		return (NULL);
12142 	}
12143 
12144 	if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) {
12145 		dtrace_dof_error(dof, "truncated probe description");
12146 		return (NULL);
12147 	}
12148 
12149 	probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset);
12150 	strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab);
12151 
12152 	if (strtab == NULL)
12153 		return (NULL);
12154 
12155 	str = daddr + strtab->dofs_offset;
12156 	size = strtab->dofs_size;
12157 
12158 	if (probe->dofp_provider >= strtab->dofs_size) {
12159 		dtrace_dof_error(dof, "corrupt probe provider");
12160 		return (NULL);
12161 	}
12162 
12163 	(void) strncpy(desc->dtpd_provider,
12164 	    (char *)(str + probe->dofp_provider),
12165 	    MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider));
12166 
12167 	if (probe->dofp_mod >= strtab->dofs_size) {
12168 		dtrace_dof_error(dof, "corrupt probe module");
12169 		return (NULL);
12170 	}
12171 
12172 	(void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod),
12173 	    MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod));
12174 
12175 	if (probe->dofp_func >= strtab->dofs_size) {
12176 		dtrace_dof_error(dof, "corrupt probe function");
12177 		return (NULL);
12178 	}
12179 
12180 	(void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func),
12181 	    MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func));
12182 
12183 	if (probe->dofp_name >= strtab->dofs_size) {
12184 		dtrace_dof_error(dof, "corrupt probe name");
12185 		return (NULL);
12186 	}
12187 
12188 	(void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name),
12189 	    MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name));
12190 
12191 	return (desc);
12192 }
12193 
12194 static dtrace_difo_t *
12195 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12196     cred_t *cr)
12197 {
12198 	dtrace_difo_t *dp;
12199 	size_t ttl = 0;
12200 	dof_difohdr_t *dofd;
12201 	uintptr_t daddr = (uintptr_t)dof;
12202 	size_t max = dtrace_difo_maxsize;
12203 	int i, l, n;
12204 
12205 	static const struct {
12206 		int section;
12207 		int bufoffs;
12208 		int lenoffs;
12209 		int entsize;
12210 		int align;
12211 		const char *msg;
12212 	} difo[] = {
12213 		{ DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf),
12214 		offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t),
12215 		sizeof (dif_instr_t), "multiple DIF sections" },
12216 
12217 		{ DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab),
12218 		offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t),
12219 		sizeof (uint64_t), "multiple integer tables" },
12220 
12221 		{ DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab),
12222 		offsetof(dtrace_difo_t, dtdo_strlen), 0,
12223 		sizeof (char), "multiple string tables" },
12224 
12225 		{ DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab),
12226 		offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t),
12227 		sizeof (uint_t), "multiple variable tables" },
12228 
12229 		{ DOF_SECT_NONE, 0, 0, 0, 0, NULL }
12230 	};
12231 
12232 	if (sec->dofs_type != DOF_SECT_DIFOHDR) {
12233 		dtrace_dof_error(dof, "invalid DIFO header section");
12234 		return (NULL);
12235 	}
12236 
12237 	if (sec->dofs_align != sizeof (dof_secidx_t)) {
12238 		dtrace_dof_error(dof, "bad alignment in DIFO header");
12239 		return (NULL);
12240 	}
12241 
12242 	if (sec->dofs_size < sizeof (dof_difohdr_t) ||
12243 	    sec->dofs_size % sizeof (dof_secidx_t)) {
12244 		dtrace_dof_error(dof, "bad size in DIFO header");
12245 		return (NULL);
12246 	}
12247 
12248 	dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
12249 	n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1;
12250 
12251 	dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
12252 	dp->dtdo_rtype = dofd->dofd_rtype;
12253 
12254 	for (l = 0; l < n; l++) {
12255 		dof_sec_t *subsec;
12256 		void **bufp;
12257 		uint32_t *lenp;
12258 
12259 		if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE,
12260 		    dofd->dofd_links[l])) == NULL)
12261 			goto err; /* invalid section link */
12262 
12263 		if (ttl + subsec->dofs_size > max) {
12264 			dtrace_dof_error(dof, "exceeds maximum size");
12265 			goto err;
12266 		}
12267 
12268 		ttl += subsec->dofs_size;
12269 
12270 		for (i = 0; difo[i].section != DOF_SECT_NONE; i++) {
12271 			if (subsec->dofs_type != difo[i].section)
12272 				continue;
12273 
12274 			if (!(subsec->dofs_flags & DOF_SECF_LOAD)) {
12275 				dtrace_dof_error(dof, "section not loaded");
12276 				goto err;
12277 			}
12278 
12279 			if (subsec->dofs_align != difo[i].align) {
12280 				dtrace_dof_error(dof, "bad alignment");
12281 				goto err;
12282 			}
12283 
12284 			bufp = (void **)((uintptr_t)dp + difo[i].bufoffs);
12285 			lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs);
12286 
12287 			if (*bufp != NULL) {
12288 				dtrace_dof_error(dof, difo[i].msg);
12289 				goto err;
12290 			}
12291 
12292 			if (difo[i].entsize != subsec->dofs_entsize) {
12293 				dtrace_dof_error(dof, "entry size mismatch");
12294 				goto err;
12295 			}
12296 
12297 			if (subsec->dofs_entsize != 0 &&
12298 			    (subsec->dofs_size % subsec->dofs_entsize) != 0) {
12299 				dtrace_dof_error(dof, "corrupt entry size");
12300 				goto err;
12301 			}
12302 
12303 			*lenp = subsec->dofs_size;
12304 			*bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP);
12305 			bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset),
12306 			    *bufp, subsec->dofs_size);
12307 
12308 			if (subsec->dofs_entsize != 0)
12309 				*lenp /= subsec->dofs_entsize;
12310 
12311 			break;
12312 		}
12313 
12314 		/*
12315 		 * If we encounter a loadable DIFO sub-section that is not
12316 		 * known to us, assume this is a broken program and fail.
12317 		 */
12318 		if (difo[i].section == DOF_SECT_NONE &&
12319 		    (subsec->dofs_flags & DOF_SECF_LOAD)) {
12320 			dtrace_dof_error(dof, "unrecognized DIFO subsection");
12321 			goto err;
12322 		}
12323 	}
12324 
12325 	if (dp->dtdo_buf == NULL) {
12326 		/*
12327 		 * We can't have a DIF object without DIF text.
12328 		 */
12329 		dtrace_dof_error(dof, "missing DIF text");
12330 		goto err;
12331 	}
12332 
12333 	/*
12334 	 * Before we validate the DIF object, run through the variable table
12335 	 * looking for the strings -- if any of their size are under, we'll set
12336 	 * their size to be the system-wide default string size.  Note that
12337 	 * this should _not_ happen if the "strsize" option has been set --
12338 	 * in this case, the compiler should have set the size to reflect the
12339 	 * setting of the option.
12340 	 */
12341 	for (i = 0; i < dp->dtdo_varlen; i++) {
12342 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
12343 		dtrace_diftype_t *t = &v->dtdv_type;
12344 
12345 		if (v->dtdv_id < DIF_VAR_OTHER_UBASE)
12346 			continue;
12347 
12348 		if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0)
12349 			t->dtdt_size = dtrace_strsize_default;
12350 	}
12351 
12352 	if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0)
12353 		goto err;
12354 
12355 	dtrace_difo_init(dp, vstate);
12356 	return (dp);
12357 
12358 err:
12359 	kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
12360 	kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
12361 	kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
12362 	kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
12363 
12364 	kmem_free(dp, sizeof (dtrace_difo_t));
12365 	return (NULL);
12366 }
12367 
12368 static dtrace_predicate_t *
12369 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12370     cred_t *cr)
12371 {
12372 	dtrace_difo_t *dp;
12373 
12374 	if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL)
12375 		return (NULL);
12376 
12377 	return (dtrace_predicate_create(dp));
12378 }
12379 
12380 static dtrace_actdesc_t *
12381 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12382     cred_t *cr)
12383 {
12384 	dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next;
12385 	dof_actdesc_t *desc;
12386 	dof_sec_t *difosec;
12387 	size_t offs;
12388 	uintptr_t daddr = (uintptr_t)dof;
12389 	uint64_t arg;
12390 	dtrace_actkind_t kind;
12391 
12392 	if (sec->dofs_type != DOF_SECT_ACTDESC) {
12393 		dtrace_dof_error(dof, "invalid action section");
12394 		return (NULL);
12395 	}
12396 
12397 	if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) {
12398 		dtrace_dof_error(dof, "truncated action description");
12399 		return (NULL);
12400 	}
12401 
12402 	if (sec->dofs_align != sizeof (uint64_t)) {
12403 		dtrace_dof_error(dof, "bad alignment in action description");
12404 		return (NULL);
12405 	}
12406 
12407 	if (sec->dofs_size < sec->dofs_entsize) {
12408 		dtrace_dof_error(dof, "section entry size exceeds total size");
12409 		return (NULL);
12410 	}
12411 
12412 	if (sec->dofs_entsize != sizeof (dof_actdesc_t)) {
12413 		dtrace_dof_error(dof, "bad entry size in action description");
12414 		return (NULL);
12415 	}
12416 
12417 	if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) {
12418 		dtrace_dof_error(dof, "actions exceed dtrace_actions_max");
12419 		return (NULL);
12420 	}
12421 
12422 	for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
12423 		desc = (dof_actdesc_t *)(daddr +
12424 		    (uintptr_t)sec->dofs_offset + offs);
12425 		kind = (dtrace_actkind_t)desc->dofa_kind;
12426 
12427 		if ((DTRACEACT_ISPRINTFLIKE(kind) &&
12428 		    (kind != DTRACEACT_PRINTA ||
12429 		    desc->dofa_strtab != DOF_SECIDX_NONE)) ||
12430 		    (kind == DTRACEACT_DIFEXPR &&
12431 		    desc->dofa_strtab != DOF_SECIDX_NONE)) {
12432 			dof_sec_t *strtab;
12433 			char *str, *fmt;
12434 			uint64_t i;
12435 
12436 			/*
12437 			 * The argument to these actions is an index into the
12438 			 * DOF string table.  For printf()-like actions, this
12439 			 * is the format string.  For print(), this is the
12440 			 * CTF type of the expression result.
12441 			 */
12442 			if ((strtab = dtrace_dof_sect(dof,
12443 			    DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL)
12444 				goto err;
12445 
12446 			str = (char *)((uintptr_t)dof +
12447 			    (uintptr_t)strtab->dofs_offset);
12448 
12449 			for (i = desc->dofa_arg; i < strtab->dofs_size; i++) {
12450 				if (str[i] == '\0')
12451 					break;
12452 			}
12453 
12454 			if (i >= strtab->dofs_size) {
12455 				dtrace_dof_error(dof, "bogus format string");
12456 				goto err;
12457 			}
12458 
12459 			if (i == desc->dofa_arg) {
12460 				dtrace_dof_error(dof, "empty format string");
12461 				goto err;
12462 			}
12463 
12464 			i -= desc->dofa_arg;
12465 			fmt = kmem_alloc(i + 1, KM_SLEEP);
12466 			bcopy(&str[desc->dofa_arg], fmt, i + 1);
12467 			arg = (uint64_t)(uintptr_t)fmt;
12468 		} else {
12469 			if (kind == DTRACEACT_PRINTA) {
12470 				ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE);
12471 				arg = 0;
12472 			} else {
12473 				arg = desc->dofa_arg;
12474 			}
12475 		}
12476 
12477 		act = dtrace_actdesc_create(kind, desc->dofa_ntuple,
12478 		    desc->dofa_uarg, arg);
12479 
12480 		if (last != NULL) {
12481 			last->dtad_next = act;
12482 		} else {
12483 			first = act;
12484 		}
12485 
12486 		last = act;
12487 
12488 		if (desc->dofa_difo == DOF_SECIDX_NONE)
12489 			continue;
12490 
12491 		if ((difosec = dtrace_dof_sect(dof,
12492 		    DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL)
12493 			goto err;
12494 
12495 		act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr);
12496 
12497 		if (act->dtad_difo == NULL)
12498 			goto err;
12499 	}
12500 
12501 	ASSERT(first != NULL);
12502 	return (first);
12503 
12504 err:
12505 	for (act = first; act != NULL; act = next) {
12506 		next = act->dtad_next;
12507 		dtrace_actdesc_release(act, vstate);
12508 	}
12509 
12510 	return (NULL);
12511 }
12512 
12513 static dtrace_ecbdesc_t *
12514 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12515     cred_t *cr)
12516 {
12517 	dtrace_ecbdesc_t *ep;
12518 	dof_ecbdesc_t *ecb;
12519 	dtrace_probedesc_t *desc;
12520 	dtrace_predicate_t *pred = NULL;
12521 
12522 	if (sec->dofs_size < sizeof (dof_ecbdesc_t)) {
12523 		dtrace_dof_error(dof, "truncated ECB description");
12524 		return (NULL);
12525 	}
12526 
12527 	if (sec->dofs_align != sizeof (uint64_t)) {
12528 		dtrace_dof_error(dof, "bad alignment in ECB description");
12529 		return (NULL);
12530 	}
12531 
12532 	ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset);
12533 	sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes);
12534 
12535 	if (sec == NULL)
12536 		return (NULL);
12537 
12538 	ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
12539 	ep->dted_uarg = ecb->dofe_uarg;
12540 	desc = &ep->dted_probe;
12541 
12542 	if (dtrace_dof_probedesc(dof, sec, desc) == NULL)
12543 		goto err;
12544 
12545 	if (ecb->dofe_pred != DOF_SECIDX_NONE) {
12546 		if ((sec = dtrace_dof_sect(dof,
12547 		    DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL)
12548 			goto err;
12549 
12550 		if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL)
12551 			goto err;
12552 
12553 		ep->dted_pred.dtpdd_predicate = pred;
12554 	}
12555 
12556 	if (ecb->dofe_actions != DOF_SECIDX_NONE) {
12557 		if ((sec = dtrace_dof_sect(dof,
12558 		    DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL)
12559 			goto err;
12560 
12561 		ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr);
12562 
12563 		if (ep->dted_action == NULL)
12564 			goto err;
12565 	}
12566 
12567 	return (ep);
12568 
12569 err:
12570 	if (pred != NULL)
12571 		dtrace_predicate_release(pred, vstate);
12572 	kmem_free(ep, sizeof (dtrace_ecbdesc_t));
12573 	return (NULL);
12574 }
12575 
12576 /*
12577  * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the
12578  * specified DOF.  At present, this amounts to simply adding 'ubase' to the
12579  * site of any user SETX relocations to account for load object base address.
12580  * In the future, if we need other relocations, this function can be extended.
12581  */
12582 static int
12583 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase)
12584 {
12585 	uintptr_t daddr = (uintptr_t)dof;
12586 	dof_relohdr_t *dofr =
12587 	    (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
12588 	dof_sec_t *ss, *rs, *ts;
12589 	dof_relodesc_t *r;
12590 	uint_t i, n;
12591 
12592 	if (sec->dofs_size < sizeof (dof_relohdr_t) ||
12593 	    sec->dofs_align != sizeof (dof_secidx_t)) {
12594 		dtrace_dof_error(dof, "invalid relocation header");
12595 		return (-1);
12596 	}
12597 
12598 	ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab);
12599 	rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec);
12600 	ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec);
12601 
12602 	if (ss == NULL || rs == NULL || ts == NULL)
12603 		return (-1); /* dtrace_dof_error() has been called already */
12604 
12605 	if (rs->dofs_entsize < sizeof (dof_relodesc_t) ||
12606 	    rs->dofs_align != sizeof (uint64_t)) {
12607 		dtrace_dof_error(dof, "invalid relocation section");
12608 		return (-1);
12609 	}
12610 
12611 	r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset);
12612 	n = rs->dofs_size / rs->dofs_entsize;
12613 
12614 	for (i = 0; i < n; i++) {
12615 		uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset;
12616 
12617 		switch (r->dofr_type) {
12618 		case DOF_RELO_NONE:
12619 			break;
12620 		case DOF_RELO_SETX:
12621 			if (r->dofr_offset >= ts->dofs_size || r->dofr_offset +
12622 			    sizeof (uint64_t) > ts->dofs_size) {
12623 				dtrace_dof_error(dof, "bad relocation offset");
12624 				return (-1);
12625 			}
12626 
12627 			if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) {
12628 				dtrace_dof_error(dof, "misaligned setx relo");
12629 				return (-1);
12630 			}
12631 
12632 			*(uint64_t *)taddr += ubase;
12633 			break;
12634 		default:
12635 			dtrace_dof_error(dof, "invalid relocation type");
12636 			return (-1);
12637 		}
12638 
12639 		r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize);
12640 	}
12641 
12642 	return (0);
12643 }
12644 
12645 /*
12646  * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
12647  * header:  it should be at the front of a memory region that is at least
12648  * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
12649  * size.  It need not be validated in any other way.
12650  */
12651 static int
12652 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr,
12653     dtrace_enabling_t **enabp, uint64_t ubase, int noprobes)
12654 {
12655 	uint64_t len = dof->dofh_loadsz, seclen;
12656 	uintptr_t daddr = (uintptr_t)dof;
12657 	dtrace_ecbdesc_t *ep;
12658 	dtrace_enabling_t *enab;
12659 	uint_t i;
12660 
12661 	ASSERT(MUTEX_HELD(&dtrace_lock));
12662 	ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t));
12663 
12664 	/*
12665 	 * Check the DOF header identification bytes.  In addition to checking
12666 	 * valid settings, we also verify that unused bits/bytes are zeroed so
12667 	 * we can use them later without fear of regressing existing binaries.
12668 	 */
12669 	if (bcmp(&dof->dofh_ident[DOF_ID_MAG0],
12670 	    DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) {
12671 		dtrace_dof_error(dof, "DOF magic string mismatch");
12672 		return (-1);
12673 	}
12674 
12675 	if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 &&
12676 	    dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) {
12677 		dtrace_dof_error(dof, "DOF has invalid data model");
12678 		return (-1);
12679 	}
12680 
12681 	if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) {
12682 		dtrace_dof_error(dof, "DOF encoding mismatch");
12683 		return (-1);
12684 	}
12685 
12686 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
12687 	    dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) {
12688 		dtrace_dof_error(dof, "DOF version mismatch");
12689 		return (-1);
12690 	}
12691 
12692 	if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) {
12693 		dtrace_dof_error(dof, "DOF uses unsupported instruction set");
12694 		return (-1);
12695 	}
12696 
12697 	if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) {
12698 		dtrace_dof_error(dof, "DOF uses too many integer registers");
12699 		return (-1);
12700 	}
12701 
12702 	if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) {
12703 		dtrace_dof_error(dof, "DOF uses too many tuple registers");
12704 		return (-1);
12705 	}
12706 
12707 	for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) {
12708 		if (dof->dofh_ident[i] != 0) {
12709 			dtrace_dof_error(dof, "DOF has invalid ident byte set");
12710 			return (-1);
12711 		}
12712 	}
12713 
12714 	if (dof->dofh_flags & ~DOF_FL_VALID) {
12715 		dtrace_dof_error(dof, "DOF has invalid flag bits set");
12716 		return (-1);
12717 	}
12718 
12719 	if (dof->dofh_secsize == 0) {
12720 		dtrace_dof_error(dof, "zero section header size");
12721 		return (-1);
12722 	}
12723 
12724 	/*
12725 	 * Check that the section headers don't exceed the amount of DOF
12726 	 * data.  Note that we cast the section size and number of sections
12727 	 * to uint64_t's to prevent possible overflow in the multiplication.
12728 	 */
12729 	seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize;
12730 
12731 	if (dof->dofh_secoff > len || seclen > len ||
12732 	    dof->dofh_secoff + seclen > len) {
12733 		dtrace_dof_error(dof, "truncated section headers");
12734 		return (-1);
12735 	}
12736 
12737 	if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) {
12738 		dtrace_dof_error(dof, "misaligned section headers");
12739 		return (-1);
12740 	}
12741 
12742 	if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) {
12743 		dtrace_dof_error(dof, "misaligned section size");
12744 		return (-1);
12745 	}
12746 
12747 	/*
12748 	 * Take an initial pass through the section headers to be sure that
12749 	 * the headers don't have stray offsets.  If the 'noprobes' flag is
12750 	 * set, do not permit sections relating to providers, probes, or args.
12751 	 */
12752 	for (i = 0; i < dof->dofh_secnum; i++) {
12753 		dof_sec_t *sec = (dof_sec_t *)(daddr +
12754 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12755 
12756 		if (noprobes) {
12757 			switch (sec->dofs_type) {
12758 			case DOF_SECT_PROVIDER:
12759 			case DOF_SECT_PROBES:
12760 			case DOF_SECT_PRARGS:
12761 			case DOF_SECT_PROFFS:
12762 				dtrace_dof_error(dof, "illegal sections "
12763 				    "for enabling");
12764 				return (-1);
12765 			}
12766 		}
12767 
12768 		if (!(sec->dofs_flags & DOF_SECF_LOAD))
12769 			continue; /* just ignore non-loadable sections */
12770 
12771 		if (sec->dofs_align & (sec->dofs_align - 1)) {
12772 			dtrace_dof_error(dof, "bad section alignment");
12773 			return (-1);
12774 		}
12775 
12776 		if (sec->dofs_offset & (sec->dofs_align - 1)) {
12777 			dtrace_dof_error(dof, "misaligned section");
12778 			return (-1);
12779 		}
12780 
12781 		if (sec->dofs_offset > len || sec->dofs_size > len ||
12782 		    sec->dofs_offset + sec->dofs_size > len) {
12783 			dtrace_dof_error(dof, "corrupt section header");
12784 			return (-1);
12785 		}
12786 
12787 		if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr +
12788 		    sec->dofs_offset + sec->dofs_size - 1) != '\0') {
12789 			dtrace_dof_error(dof, "non-terminating string table");
12790 			return (-1);
12791 		}
12792 	}
12793 
12794 	/*
12795 	 * Take a second pass through the sections and locate and perform any
12796 	 * relocations that are present.  We do this after the first pass to
12797 	 * be sure that all sections have had their headers validated.
12798 	 */
12799 	for (i = 0; i < dof->dofh_secnum; i++) {
12800 		dof_sec_t *sec = (dof_sec_t *)(daddr +
12801 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12802 
12803 		if (!(sec->dofs_flags & DOF_SECF_LOAD))
12804 			continue; /* skip sections that are not loadable */
12805 
12806 		switch (sec->dofs_type) {
12807 		case DOF_SECT_URELHDR:
12808 			if (dtrace_dof_relocate(dof, sec, ubase) != 0)
12809 				return (-1);
12810 			break;
12811 		}
12812 	}
12813 
12814 	if ((enab = *enabp) == NULL)
12815 		enab = *enabp = dtrace_enabling_create(vstate);
12816 
12817 	for (i = 0; i < dof->dofh_secnum; i++) {
12818 		dof_sec_t *sec = (dof_sec_t *)(daddr +
12819 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12820 
12821 		if (sec->dofs_type != DOF_SECT_ECBDESC)
12822 			continue;
12823 
12824 		if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) {
12825 			dtrace_enabling_destroy(enab);
12826 			*enabp = NULL;
12827 			return (-1);
12828 		}
12829 
12830 		dtrace_enabling_add(enab, ep);
12831 	}
12832 
12833 	return (0);
12834 }
12835 
12836 /*
12837  * Process DOF for any options.  This routine assumes that the DOF has been
12838  * at least processed by dtrace_dof_slurp().
12839  */
12840 static int
12841 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state)
12842 {
12843 	int i, rval;
12844 	uint32_t entsize;
12845 	size_t offs;
12846 	dof_optdesc_t *desc;
12847 
12848 	for (i = 0; i < dof->dofh_secnum; i++) {
12849 		dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof +
12850 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12851 
12852 		if (sec->dofs_type != DOF_SECT_OPTDESC)
12853 			continue;
12854 
12855 		if (sec->dofs_align != sizeof (uint64_t)) {
12856 			dtrace_dof_error(dof, "bad alignment in "
12857 			    "option description");
12858 			return (EINVAL);
12859 		}
12860 
12861 		if ((entsize = sec->dofs_entsize) == 0) {
12862 			dtrace_dof_error(dof, "zeroed option entry size");
12863 			return (EINVAL);
12864 		}
12865 
12866 		if (entsize < sizeof (dof_optdesc_t)) {
12867 			dtrace_dof_error(dof, "bad option entry size");
12868 			return (EINVAL);
12869 		}
12870 
12871 		for (offs = 0; offs < sec->dofs_size; offs += entsize) {
12872 			desc = (dof_optdesc_t *)((uintptr_t)dof +
12873 			    (uintptr_t)sec->dofs_offset + offs);
12874 
12875 			if (desc->dofo_strtab != DOF_SECIDX_NONE) {
12876 				dtrace_dof_error(dof, "non-zero option string");
12877 				return (EINVAL);
12878 			}
12879 
12880 			if (desc->dofo_value == DTRACEOPT_UNSET) {
12881 				dtrace_dof_error(dof, "unset option");
12882 				return (EINVAL);
12883 			}
12884 
12885 			if ((rval = dtrace_state_option(state,
12886 			    desc->dofo_option, desc->dofo_value)) != 0) {
12887 				dtrace_dof_error(dof, "rejected option");
12888 				return (rval);
12889 			}
12890 		}
12891 	}
12892 
12893 	return (0);
12894 }
12895 
12896 /*
12897  * DTrace Consumer State Functions
12898  */
12899 static int
12900 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size)
12901 {
12902 	size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize;
12903 	void *base;
12904 	uintptr_t limit;
12905 	dtrace_dynvar_t *dvar, *next, *start;
12906 	int i;
12907 
12908 	ASSERT(MUTEX_HELD(&dtrace_lock));
12909 	ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL);
12910 
12911 	bzero(dstate, sizeof (dtrace_dstate_t));
12912 
12913 	if ((dstate->dtds_chunksize = chunksize) == 0)
12914 		dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE;
12915 
12916 	if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)))
12917 		size = min;
12918 
12919 	if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
12920 		return (ENOMEM);
12921 
12922 	dstate->dtds_size = size;
12923 	dstate->dtds_base = base;
12924 	dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP);
12925 	bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t));
12926 
12927 	hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t));
12928 
12929 	if (hashsize != 1 && (hashsize & 1))
12930 		hashsize--;
12931 
12932 	dstate->dtds_hashsize = hashsize;
12933 	dstate->dtds_hash = dstate->dtds_base;
12934 
12935 	/*
12936 	 * Set all of our hash buckets to point to the single sink, and (if
12937 	 * it hasn't already been set), set the sink's hash value to be the
12938 	 * sink sentinel value.  The sink is needed for dynamic variable
12939 	 * lookups to know that they have iterated over an entire, valid hash
12940 	 * chain.
12941 	 */
12942 	for (i = 0; i < hashsize; i++)
12943 		dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink;
12944 
12945 	if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK)
12946 		dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK;
12947 
12948 	/*
12949 	 * Determine number of active CPUs.  Divide free list evenly among
12950 	 * active CPUs.
12951 	 */
12952 	start = (dtrace_dynvar_t *)
12953 	    ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t));
12954 	limit = (uintptr_t)base + size;
12955 
12956 	maxper = (limit - (uintptr_t)start) / NCPU;
12957 	maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize;
12958 
12959 #if !defined(sun)
12960 	CPU_FOREACH(i) {
12961 #else
12962 	for (i = 0; i < NCPU; i++) {
12963 #endif
12964 		dstate->dtds_percpu[i].dtdsc_free = dvar = start;
12965 
12966 		/*
12967 		 * If we don't even have enough chunks to make it once through
12968 		 * NCPUs, we're just going to allocate everything to the first
12969 		 * CPU.  And if we're on the last CPU, we're going to allocate
12970 		 * whatever is left over.  In either case, we set the limit to
12971 		 * be the limit of the dynamic variable space.
12972 		 */
12973 		if (maxper == 0 || i == NCPU - 1) {
12974 			limit = (uintptr_t)base + size;
12975 			start = NULL;
12976 		} else {
12977 			limit = (uintptr_t)start + maxper;
12978 			start = (dtrace_dynvar_t *)limit;
12979 		}
12980 
12981 		ASSERT(limit <= (uintptr_t)base + size);
12982 
12983 		for (;;) {
12984 			next = (dtrace_dynvar_t *)((uintptr_t)dvar +
12985 			    dstate->dtds_chunksize);
12986 
12987 			if ((uintptr_t)next + dstate->dtds_chunksize >= limit)
12988 				break;
12989 
12990 			dvar->dtdv_next = next;
12991 			dvar = next;
12992 		}
12993 
12994 		if (maxper == 0)
12995 			break;
12996 	}
12997 
12998 	return (0);
12999 }
13000 
13001 static void
13002 dtrace_dstate_fini(dtrace_dstate_t *dstate)
13003 {
13004 	ASSERT(MUTEX_HELD(&cpu_lock));
13005 
13006 	if (dstate->dtds_base == NULL)
13007 		return;
13008 
13009 	kmem_free(dstate->dtds_base, dstate->dtds_size);
13010 	kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu);
13011 }
13012 
13013 static void
13014 dtrace_vstate_fini(dtrace_vstate_t *vstate)
13015 {
13016 	/*
13017 	 * Logical XOR, where are you?
13018 	 */
13019 	ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL));
13020 
13021 	if (vstate->dtvs_nglobals > 0) {
13022 		kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals *
13023 		    sizeof (dtrace_statvar_t *));
13024 	}
13025 
13026 	if (vstate->dtvs_ntlocals > 0) {
13027 		kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals *
13028 		    sizeof (dtrace_difv_t));
13029 	}
13030 
13031 	ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL));
13032 
13033 	if (vstate->dtvs_nlocals > 0) {
13034 		kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals *
13035 		    sizeof (dtrace_statvar_t *));
13036 	}
13037 }
13038 
13039 #if defined(sun)
13040 static void
13041 dtrace_state_clean(dtrace_state_t *state)
13042 {
13043 	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
13044 		return;
13045 
13046 	dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
13047 	dtrace_speculation_clean(state);
13048 }
13049 
13050 static void
13051 dtrace_state_deadman(dtrace_state_t *state)
13052 {
13053 	hrtime_t now;
13054 
13055 	dtrace_sync();
13056 
13057 	now = dtrace_gethrtime();
13058 
13059 	if (state != dtrace_anon.dta_state &&
13060 	    now - state->dts_laststatus >= dtrace_deadman_user)
13061 		return;
13062 
13063 	/*
13064 	 * We must be sure that dts_alive never appears to be less than the
13065 	 * value upon entry to dtrace_state_deadman(), and because we lack a
13066 	 * dtrace_cas64(), we cannot store to it atomically.  We thus instead
13067 	 * store INT64_MAX to it, followed by a memory barrier, followed by
13068 	 * the new value.  This assures that dts_alive never appears to be
13069 	 * less than its true value, regardless of the order in which the
13070 	 * stores to the underlying storage are issued.
13071 	 */
13072 	state->dts_alive = INT64_MAX;
13073 	dtrace_membar_producer();
13074 	state->dts_alive = now;
13075 }
13076 #else
13077 static void
13078 dtrace_state_clean(void *arg)
13079 {
13080 	dtrace_state_t *state = arg;
13081 	dtrace_optval_t *opt = state->dts_options;
13082 
13083 	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
13084 		return;
13085 
13086 	dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
13087 	dtrace_speculation_clean(state);
13088 
13089 	callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC,
13090 	    dtrace_state_clean, state);
13091 }
13092 
13093 static void
13094 dtrace_state_deadman(void *arg)
13095 {
13096 	dtrace_state_t *state = arg;
13097 	hrtime_t now;
13098 
13099 	dtrace_sync();
13100 
13101 	dtrace_debug_output();
13102 
13103 	now = dtrace_gethrtime();
13104 
13105 	if (state != dtrace_anon.dta_state &&
13106 	    now - state->dts_laststatus >= dtrace_deadman_user)
13107 		return;
13108 
13109 	/*
13110 	 * We must be sure that dts_alive never appears to be less than the
13111 	 * value upon entry to dtrace_state_deadman(), and because we lack a
13112 	 * dtrace_cas64(), we cannot store to it atomically.  We thus instead
13113 	 * store INT64_MAX to it, followed by a memory barrier, followed by
13114 	 * the new value.  This assures that dts_alive never appears to be
13115 	 * less than its true value, regardless of the order in which the
13116 	 * stores to the underlying storage are issued.
13117 	 */
13118 	state->dts_alive = INT64_MAX;
13119 	dtrace_membar_producer();
13120 	state->dts_alive = now;
13121 
13122 	callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC,
13123 	    dtrace_state_deadman, state);
13124 }
13125 #endif
13126 
13127 static dtrace_state_t *
13128 #if defined(sun)
13129 dtrace_state_create(dev_t *devp, cred_t *cr)
13130 #else
13131 dtrace_state_create(struct cdev *dev)
13132 #endif
13133 {
13134 #if defined(sun)
13135 	minor_t minor;
13136 	major_t major;
13137 #else
13138 	cred_t *cr = NULL;
13139 	int m = 0;
13140 #endif
13141 	char c[30];
13142 	dtrace_state_t *state;
13143 	dtrace_optval_t *opt;
13144 	int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
13145 
13146 	ASSERT(MUTEX_HELD(&dtrace_lock));
13147 	ASSERT(MUTEX_HELD(&cpu_lock));
13148 
13149 #if defined(sun)
13150 	minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1,
13151 	    VM_BESTFIT | VM_SLEEP);
13152 
13153 	if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) {
13154 		vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
13155 		return (NULL);
13156 	}
13157 
13158 	state = ddi_get_soft_state(dtrace_softstate, minor);
13159 #else
13160 	if (dev != NULL) {
13161 		cr = dev->si_cred;
13162 		m = dev2unit(dev);
13163 		}
13164 
13165 	/* Allocate memory for the state. */
13166 	state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP);
13167 #endif
13168 
13169 	state->dts_epid = DTRACE_EPIDNONE + 1;
13170 
13171 	(void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m);
13172 #if defined(sun)
13173 	state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1,
13174 	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
13175 
13176 	if (devp != NULL) {
13177 		major = getemajor(*devp);
13178 	} else {
13179 		major = ddi_driver_major(dtrace_devi);
13180 	}
13181 
13182 	state->dts_dev = makedevice(major, minor);
13183 
13184 	if (devp != NULL)
13185 		*devp = state->dts_dev;
13186 #else
13187 	state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx);
13188 	state->dts_dev = dev;
13189 #endif
13190 
13191 	/*
13192 	 * We allocate NCPU buffers.  On the one hand, this can be quite
13193 	 * a bit of memory per instance (nearly 36K on a Starcat).  On the
13194 	 * other hand, it saves an additional memory reference in the probe
13195 	 * path.
13196 	 */
13197 	state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
13198 	state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
13199 
13200 #if defined(sun)
13201 	state->dts_cleaner = CYCLIC_NONE;
13202 	state->dts_deadman = CYCLIC_NONE;
13203 #else
13204 	callout_init(&state->dts_cleaner, CALLOUT_MPSAFE);
13205 	callout_init(&state->dts_deadman, CALLOUT_MPSAFE);
13206 #endif
13207 	state->dts_vstate.dtvs_state = state;
13208 
13209 	for (i = 0; i < DTRACEOPT_MAX; i++)
13210 		state->dts_options[i] = DTRACEOPT_UNSET;
13211 
13212 	/*
13213 	 * Set the default options.
13214 	 */
13215 	opt = state->dts_options;
13216 	opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH;
13217 	opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO;
13218 	opt[DTRACEOPT_NSPEC] = dtrace_nspec_default;
13219 	opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default;
13220 	opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL;
13221 	opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default;
13222 	opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default;
13223 	opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default;
13224 	opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default;
13225 	opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default;
13226 	opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default;
13227 	opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default;
13228 	opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default;
13229 	opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default;
13230 
13231 	state->dts_activity = DTRACE_ACTIVITY_INACTIVE;
13232 
13233 	/*
13234 	 * Depending on the user credentials, we set flag bits which alter probe
13235 	 * visibility or the amount of destructiveness allowed.  In the case of
13236 	 * actual anonymous tracing, or the possession of all privileges, all of
13237 	 * the normal checks are bypassed.
13238 	 */
13239 	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
13240 		state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
13241 		state->dts_cred.dcr_action = DTRACE_CRA_ALL;
13242 	} else {
13243 		/*
13244 		 * Set up the credentials for this instantiation.  We take a
13245 		 * hold on the credential to prevent it from disappearing on
13246 		 * us; this in turn prevents the zone_t referenced by this
13247 		 * credential from disappearing.  This means that we can
13248 		 * examine the credential and the zone from probe context.
13249 		 */
13250 		crhold(cr);
13251 		state->dts_cred.dcr_cred = cr;
13252 
13253 		/*
13254 		 * CRA_PROC means "we have *some* privilege for dtrace" and
13255 		 * unlocks the use of variables like pid, zonename, etc.
13256 		 */
13257 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) ||
13258 		    PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
13259 			state->dts_cred.dcr_action |= DTRACE_CRA_PROC;
13260 		}
13261 
13262 		/*
13263 		 * dtrace_user allows use of syscall and profile providers.
13264 		 * If the user also has proc_owner and/or proc_zone, we
13265 		 * extend the scope to include additional visibility and
13266 		 * destructive power.
13267 		 */
13268 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) {
13269 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) {
13270 				state->dts_cred.dcr_visible |=
13271 				    DTRACE_CRV_ALLPROC;
13272 
13273 				state->dts_cred.dcr_action |=
13274 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13275 			}
13276 
13277 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) {
13278 				state->dts_cred.dcr_visible |=
13279 				    DTRACE_CRV_ALLZONE;
13280 
13281 				state->dts_cred.dcr_action |=
13282 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13283 			}
13284 
13285 			/*
13286 			 * If we have all privs in whatever zone this is,
13287 			 * we can do destructive things to processes which
13288 			 * have altered credentials.
13289 			 */
13290 #if defined(sun)
13291 			if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
13292 			    cr->cr_zone->zone_privset)) {
13293 				state->dts_cred.dcr_action |=
13294 				    DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
13295 			}
13296 #endif
13297 		}
13298 
13299 		/*
13300 		 * Holding the dtrace_kernel privilege also implies that
13301 		 * the user has the dtrace_user privilege from a visibility
13302 		 * perspective.  But without further privileges, some
13303 		 * destructive actions are not available.
13304 		 */
13305 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) {
13306 			/*
13307 			 * Make all probes in all zones visible.  However,
13308 			 * this doesn't mean that all actions become available
13309 			 * to all zones.
13310 			 */
13311 			state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL |
13312 			    DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE;
13313 
13314 			state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL |
13315 			    DTRACE_CRA_PROC;
13316 			/*
13317 			 * Holding proc_owner means that destructive actions
13318 			 * for *this* zone are allowed.
13319 			 */
13320 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
13321 				state->dts_cred.dcr_action |=
13322 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13323 
13324 			/*
13325 			 * Holding proc_zone means that destructive actions
13326 			 * for this user/group ID in all zones is allowed.
13327 			 */
13328 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
13329 				state->dts_cred.dcr_action |=
13330 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13331 
13332 #if defined(sun)
13333 			/*
13334 			 * If we have all privs in whatever zone this is,
13335 			 * we can do destructive things to processes which
13336 			 * have altered credentials.
13337 			 */
13338 			if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
13339 			    cr->cr_zone->zone_privset)) {
13340 				state->dts_cred.dcr_action |=
13341 				    DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
13342 			}
13343 #endif
13344 		}
13345 
13346 		/*
13347 		 * Holding the dtrace_proc privilege gives control over fasttrap
13348 		 * and pid providers.  We need to grant wider destructive
13349 		 * privileges in the event that the user has proc_owner and/or
13350 		 * proc_zone.
13351 		 */
13352 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
13353 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
13354 				state->dts_cred.dcr_action |=
13355 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13356 
13357 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
13358 				state->dts_cred.dcr_action |=
13359 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13360 		}
13361 	}
13362 
13363 	return (state);
13364 }
13365 
13366 static int
13367 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which)
13368 {
13369 	dtrace_optval_t *opt = state->dts_options, size;
13370 	processorid_t cpu = 0;;
13371 	int flags = 0, rval;
13372 
13373 	ASSERT(MUTEX_HELD(&dtrace_lock));
13374 	ASSERT(MUTEX_HELD(&cpu_lock));
13375 	ASSERT(which < DTRACEOPT_MAX);
13376 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE ||
13377 	    (state == dtrace_anon.dta_state &&
13378 	    state->dts_activity == DTRACE_ACTIVITY_ACTIVE));
13379 
13380 	if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0)
13381 		return (0);
13382 
13383 	if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
13384 		cpu = opt[DTRACEOPT_CPU];
13385 
13386 	if (which == DTRACEOPT_SPECSIZE)
13387 		flags |= DTRACEBUF_NOSWITCH;
13388 
13389 	if (which == DTRACEOPT_BUFSIZE) {
13390 		if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING)
13391 			flags |= DTRACEBUF_RING;
13392 
13393 		if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL)
13394 			flags |= DTRACEBUF_FILL;
13395 
13396 		if (state != dtrace_anon.dta_state ||
13397 		    state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
13398 			flags |= DTRACEBUF_INACTIVE;
13399 	}
13400 
13401 	for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) {
13402 		/*
13403 		 * The size must be 8-byte aligned.  If the size is not 8-byte
13404 		 * aligned, drop it down by the difference.
13405 		 */
13406 		if (size & (sizeof (uint64_t) - 1))
13407 			size -= size & (sizeof (uint64_t) - 1);
13408 
13409 		if (size < state->dts_reserve) {
13410 			/*
13411 			 * Buffers always must be large enough to accommodate
13412 			 * their prereserved space.  We return E2BIG instead
13413 			 * of ENOMEM in this case to allow for user-level
13414 			 * software to differentiate the cases.
13415 			 */
13416 			return (E2BIG);
13417 		}
13418 
13419 		rval = dtrace_buffer_alloc(buf, size, flags, cpu);
13420 
13421 		if (rval != ENOMEM) {
13422 			opt[which] = size;
13423 			return (rval);
13424 		}
13425 
13426 		if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
13427 			return (rval);
13428 	}
13429 
13430 	return (ENOMEM);
13431 }
13432 
13433 static int
13434 dtrace_state_buffers(dtrace_state_t *state)
13435 {
13436 	dtrace_speculation_t *spec = state->dts_speculations;
13437 	int rval, i;
13438 
13439 	if ((rval = dtrace_state_buffer(state, state->dts_buffer,
13440 	    DTRACEOPT_BUFSIZE)) != 0)
13441 		return (rval);
13442 
13443 	if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer,
13444 	    DTRACEOPT_AGGSIZE)) != 0)
13445 		return (rval);
13446 
13447 	for (i = 0; i < state->dts_nspeculations; i++) {
13448 		if ((rval = dtrace_state_buffer(state,
13449 		    spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0)
13450 			return (rval);
13451 	}
13452 
13453 	return (0);
13454 }
13455 
13456 static void
13457 dtrace_state_prereserve(dtrace_state_t *state)
13458 {
13459 	dtrace_ecb_t *ecb;
13460 	dtrace_probe_t *probe;
13461 
13462 	state->dts_reserve = 0;
13463 
13464 	if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
13465 		return;
13466 
13467 	/*
13468 	 * If our buffer policy is a "fill" buffer policy, we need to set the
13469 	 * prereserved space to be the space required by the END probes.
13470 	 */
13471 	probe = dtrace_probes[dtrace_probeid_end - 1];
13472 	ASSERT(probe != NULL);
13473 
13474 	for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
13475 		if (ecb->dte_state != state)
13476 			continue;
13477 
13478 		state->dts_reserve += ecb->dte_needed + ecb->dte_alignment;
13479 	}
13480 }
13481 
13482 static int
13483 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu)
13484 {
13485 	dtrace_optval_t *opt = state->dts_options, sz, nspec;
13486 	dtrace_speculation_t *spec;
13487 	dtrace_buffer_t *buf;
13488 #if defined(sun)
13489 	cyc_handler_t hdlr;
13490 	cyc_time_t when;
13491 #endif
13492 	int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t);
13493 	dtrace_icookie_t cookie;
13494 
13495 	mutex_enter(&cpu_lock);
13496 	mutex_enter(&dtrace_lock);
13497 
13498 	if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
13499 		rval = EBUSY;
13500 		goto out;
13501 	}
13502 
13503 	/*
13504 	 * Before we can perform any checks, we must prime all of the
13505 	 * retained enablings that correspond to this state.
13506 	 */
13507 	dtrace_enabling_prime(state);
13508 
13509 	if (state->dts_destructive && !state->dts_cred.dcr_destructive) {
13510 		rval = EACCES;
13511 		goto out;
13512 	}
13513 
13514 	dtrace_state_prereserve(state);
13515 
13516 	/*
13517 	 * Now we want to do is try to allocate our speculations.
13518 	 * We do not automatically resize the number of speculations; if
13519 	 * this fails, we will fail the operation.
13520 	 */
13521 	nspec = opt[DTRACEOPT_NSPEC];
13522 	ASSERT(nspec != DTRACEOPT_UNSET);
13523 
13524 	if (nspec > INT_MAX) {
13525 		rval = ENOMEM;
13526 		goto out;
13527 	}
13528 
13529 	spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP);
13530 
13531 	if (spec == NULL) {
13532 		rval = ENOMEM;
13533 		goto out;
13534 	}
13535 
13536 	state->dts_speculations = spec;
13537 	state->dts_nspeculations = (int)nspec;
13538 
13539 	for (i = 0; i < nspec; i++) {
13540 		if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) {
13541 			rval = ENOMEM;
13542 			goto err;
13543 		}
13544 
13545 		spec[i].dtsp_buffer = buf;
13546 	}
13547 
13548 	if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) {
13549 		if (dtrace_anon.dta_state == NULL) {
13550 			rval = ENOENT;
13551 			goto out;
13552 		}
13553 
13554 		if (state->dts_necbs != 0) {
13555 			rval = EALREADY;
13556 			goto out;
13557 		}
13558 
13559 		state->dts_anon = dtrace_anon_grab();
13560 		ASSERT(state->dts_anon != NULL);
13561 		state = state->dts_anon;
13562 
13563 		/*
13564 		 * We want "grabanon" to be set in the grabbed state, so we'll
13565 		 * copy that option value from the grabbing state into the
13566 		 * grabbed state.
13567 		 */
13568 		state->dts_options[DTRACEOPT_GRABANON] =
13569 		    opt[DTRACEOPT_GRABANON];
13570 
13571 		*cpu = dtrace_anon.dta_beganon;
13572 
13573 		/*
13574 		 * If the anonymous state is active (as it almost certainly
13575 		 * is if the anonymous enabling ultimately matched anything),
13576 		 * we don't allow any further option processing -- but we
13577 		 * don't return failure.
13578 		 */
13579 		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
13580 			goto out;
13581 	}
13582 
13583 	if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET &&
13584 	    opt[DTRACEOPT_AGGSIZE] != 0) {
13585 		if (state->dts_aggregations == NULL) {
13586 			/*
13587 			 * We're not going to create an aggregation buffer
13588 			 * because we don't have any ECBs that contain
13589 			 * aggregations -- set this option to 0.
13590 			 */
13591 			opt[DTRACEOPT_AGGSIZE] = 0;
13592 		} else {
13593 			/*
13594 			 * If we have an aggregation buffer, we must also have
13595 			 * a buffer to use as scratch.
13596 			 */
13597 			if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET ||
13598 			    opt[DTRACEOPT_BUFSIZE] < state->dts_needed) {
13599 				opt[DTRACEOPT_BUFSIZE] = state->dts_needed;
13600 			}
13601 		}
13602 	}
13603 
13604 	if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET &&
13605 	    opt[DTRACEOPT_SPECSIZE] != 0) {
13606 		if (!state->dts_speculates) {
13607 			/*
13608 			 * We're not going to create speculation buffers
13609 			 * because we don't have any ECBs that actually
13610 			 * speculate -- set the speculation size to 0.
13611 			 */
13612 			opt[DTRACEOPT_SPECSIZE] = 0;
13613 		}
13614 	}
13615 
13616 	/*
13617 	 * The bare minimum size for any buffer that we're actually going to
13618 	 * do anything to is sizeof (uint64_t).
13619 	 */
13620 	sz = sizeof (uint64_t);
13621 
13622 	if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) ||
13623 	    (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) ||
13624 	    (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) {
13625 		/*
13626 		 * A buffer size has been explicitly set to 0 (or to a size
13627 		 * that will be adjusted to 0) and we need the space -- we
13628 		 * need to return failure.  We return ENOSPC to differentiate
13629 		 * it from failing to allocate a buffer due to failure to meet
13630 		 * the reserve (for which we return E2BIG).
13631 		 */
13632 		rval = ENOSPC;
13633 		goto out;
13634 	}
13635 
13636 	if ((rval = dtrace_state_buffers(state)) != 0)
13637 		goto err;
13638 
13639 	if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET)
13640 		sz = dtrace_dstate_defsize;
13641 
13642 	do {
13643 		rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz);
13644 
13645 		if (rval == 0)
13646 			break;
13647 
13648 		if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
13649 			goto err;
13650 	} while (sz >>= 1);
13651 
13652 	opt[DTRACEOPT_DYNVARSIZE] = sz;
13653 
13654 	if (rval != 0)
13655 		goto err;
13656 
13657 	if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max)
13658 		opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max;
13659 
13660 	if (opt[DTRACEOPT_CLEANRATE] == 0)
13661 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
13662 
13663 	if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min)
13664 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min;
13665 
13666 	if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max)
13667 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
13668 
13669 	state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
13670 #if defined(sun)
13671 	hdlr.cyh_func = (cyc_func_t)dtrace_state_clean;
13672 	hdlr.cyh_arg = state;
13673 	hdlr.cyh_level = CY_LOW_LEVEL;
13674 
13675 	when.cyt_when = 0;
13676 	when.cyt_interval = opt[DTRACEOPT_CLEANRATE];
13677 
13678 	state->dts_cleaner = cyclic_add(&hdlr, &when);
13679 
13680 	hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman;
13681 	hdlr.cyh_arg = state;
13682 	hdlr.cyh_level = CY_LOW_LEVEL;
13683 
13684 	when.cyt_when = 0;
13685 	when.cyt_interval = dtrace_deadman_interval;
13686 
13687 	state->dts_deadman = cyclic_add(&hdlr, &when);
13688 #else
13689 	callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC,
13690 	    dtrace_state_clean, state);
13691 	callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC,
13692 	    dtrace_state_deadman, state);
13693 #endif
13694 
13695 	state->dts_activity = DTRACE_ACTIVITY_WARMUP;
13696 
13697 	/*
13698 	 * Now it's time to actually fire the BEGIN probe.  We need to disable
13699 	 * interrupts here both to record the CPU on which we fired the BEGIN
13700 	 * probe (the data from this CPU will be processed first at user
13701 	 * level) and to manually activate the buffer for this CPU.
13702 	 */
13703 	cookie = dtrace_interrupt_disable();
13704 	*cpu = curcpu;
13705 	ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE);
13706 	state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
13707 
13708 	dtrace_probe(dtrace_probeid_begin,
13709 	    (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
13710 	dtrace_interrupt_enable(cookie);
13711 	/*
13712 	 * We may have had an exit action from a BEGIN probe; only change our
13713 	 * state to ACTIVE if we're still in WARMUP.
13714 	 */
13715 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP ||
13716 	    state->dts_activity == DTRACE_ACTIVITY_DRAINING);
13717 
13718 	if (state->dts_activity == DTRACE_ACTIVITY_WARMUP)
13719 		state->dts_activity = DTRACE_ACTIVITY_ACTIVE;
13720 
13721 	/*
13722 	 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
13723 	 * want each CPU to transition its principal buffer out of the
13724 	 * INACTIVE state.  Doing this assures that no CPU will suddenly begin
13725 	 * processing an ECB halfway down a probe's ECB chain; all CPUs will
13726 	 * atomically transition from processing none of a state's ECBs to
13727 	 * processing all of them.
13728 	 */
13729 	dtrace_xcall(DTRACE_CPUALL,
13730 	    (dtrace_xcall_t)dtrace_buffer_activate, state);
13731 	goto out;
13732 
13733 err:
13734 	dtrace_buffer_free(state->dts_buffer);
13735 	dtrace_buffer_free(state->dts_aggbuffer);
13736 
13737 	if ((nspec = state->dts_nspeculations) == 0) {
13738 		ASSERT(state->dts_speculations == NULL);
13739 		goto out;
13740 	}
13741 
13742 	spec = state->dts_speculations;
13743 	ASSERT(spec != NULL);
13744 
13745 	for (i = 0; i < state->dts_nspeculations; i++) {
13746 		if ((buf = spec[i].dtsp_buffer) == NULL)
13747 			break;
13748 
13749 		dtrace_buffer_free(buf);
13750 		kmem_free(buf, bufsize);
13751 	}
13752 
13753 	kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
13754 	state->dts_nspeculations = 0;
13755 	state->dts_speculations = NULL;
13756 
13757 out:
13758 	mutex_exit(&dtrace_lock);
13759 	mutex_exit(&cpu_lock);
13760 
13761 	return (rval);
13762 }
13763 
13764 static int
13765 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu)
13766 {
13767 	dtrace_icookie_t cookie;
13768 
13769 	ASSERT(MUTEX_HELD(&dtrace_lock));
13770 
13771 	if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE &&
13772 	    state->dts_activity != DTRACE_ACTIVITY_DRAINING)
13773 		return (EINVAL);
13774 
13775 	/*
13776 	 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
13777 	 * to be sure that every CPU has seen it.  See below for the details
13778 	 * on why this is done.
13779 	 */
13780 	state->dts_activity = DTRACE_ACTIVITY_DRAINING;
13781 	dtrace_sync();
13782 
13783 	/*
13784 	 * By this point, it is impossible for any CPU to be still processing
13785 	 * with DTRACE_ACTIVITY_ACTIVE.  We can thus set our activity to
13786 	 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
13787 	 * other CPU in dtrace_buffer_reserve().  This allows dtrace_probe()
13788 	 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
13789 	 * iff we're in the END probe.
13790 	 */
13791 	state->dts_activity = DTRACE_ACTIVITY_COOLDOWN;
13792 	dtrace_sync();
13793 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN);
13794 
13795 	/*
13796 	 * Finally, we can release the reserve and call the END probe.  We
13797 	 * disable interrupts across calling the END probe to allow us to
13798 	 * return the CPU on which we actually called the END probe.  This
13799 	 * allows user-land to be sure that this CPU's principal buffer is
13800 	 * processed last.
13801 	 */
13802 	state->dts_reserve = 0;
13803 
13804 	cookie = dtrace_interrupt_disable();
13805 	*cpu = curcpu;
13806 	dtrace_probe(dtrace_probeid_end,
13807 	    (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
13808 	dtrace_interrupt_enable(cookie);
13809 
13810 	state->dts_activity = DTRACE_ACTIVITY_STOPPED;
13811 	dtrace_sync();
13812 
13813 	return (0);
13814 }
13815 
13816 static int
13817 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option,
13818     dtrace_optval_t val)
13819 {
13820 	ASSERT(MUTEX_HELD(&dtrace_lock));
13821 
13822 	if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
13823 		return (EBUSY);
13824 
13825 	if (option >= DTRACEOPT_MAX)
13826 		return (EINVAL);
13827 
13828 	if (option != DTRACEOPT_CPU && val < 0)
13829 		return (EINVAL);
13830 
13831 	switch (option) {
13832 	case DTRACEOPT_DESTRUCTIVE:
13833 		if (dtrace_destructive_disallow)
13834 			return (EACCES);
13835 
13836 		state->dts_cred.dcr_destructive = 1;
13837 		break;
13838 
13839 	case DTRACEOPT_BUFSIZE:
13840 	case DTRACEOPT_DYNVARSIZE:
13841 	case DTRACEOPT_AGGSIZE:
13842 	case DTRACEOPT_SPECSIZE:
13843 	case DTRACEOPT_STRSIZE:
13844 		if (val < 0)
13845 			return (EINVAL);
13846 
13847 		if (val >= LONG_MAX) {
13848 			/*
13849 			 * If this is an otherwise negative value, set it to
13850 			 * the highest multiple of 128m less than LONG_MAX.
13851 			 * Technically, we're adjusting the size without
13852 			 * regard to the buffer resizing policy, but in fact,
13853 			 * this has no effect -- if we set the buffer size to
13854 			 * ~LONG_MAX and the buffer policy is ultimately set to
13855 			 * be "manual", the buffer allocation is guaranteed to
13856 			 * fail, if only because the allocation requires two
13857 			 * buffers.  (We set the the size to the highest
13858 			 * multiple of 128m because it ensures that the size
13859 			 * will remain a multiple of a megabyte when
13860 			 * repeatedly halved -- all the way down to 15m.)
13861 			 */
13862 			val = LONG_MAX - (1 << 27) + 1;
13863 		}
13864 	}
13865 
13866 	state->dts_options[option] = val;
13867 
13868 	return (0);
13869 }
13870 
13871 static void
13872 dtrace_state_destroy(dtrace_state_t *state)
13873 {
13874 	dtrace_ecb_t *ecb;
13875 	dtrace_vstate_t *vstate = &state->dts_vstate;
13876 #if defined(sun)
13877 	minor_t minor = getminor(state->dts_dev);
13878 #endif
13879 	int i, bufsize = NCPU * sizeof (dtrace_buffer_t);
13880 	dtrace_speculation_t *spec = state->dts_speculations;
13881 	int nspec = state->dts_nspeculations;
13882 	uint32_t match;
13883 
13884 	ASSERT(MUTEX_HELD(&dtrace_lock));
13885 	ASSERT(MUTEX_HELD(&cpu_lock));
13886 
13887 	/*
13888 	 * First, retract any retained enablings for this state.
13889 	 */
13890 	dtrace_enabling_retract(state);
13891 	ASSERT(state->dts_nretained == 0);
13892 
13893 	if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE ||
13894 	    state->dts_activity == DTRACE_ACTIVITY_DRAINING) {
13895 		/*
13896 		 * We have managed to come into dtrace_state_destroy() on a
13897 		 * hot enabling -- almost certainly because of a disorderly
13898 		 * shutdown of a consumer.  (That is, a consumer that is
13899 		 * exiting without having called dtrace_stop().) In this case,
13900 		 * we're going to set our activity to be KILLED, and then
13901 		 * issue a sync to be sure that everyone is out of probe
13902 		 * context before we start blowing away ECBs.
13903 		 */
13904 		state->dts_activity = DTRACE_ACTIVITY_KILLED;
13905 		dtrace_sync();
13906 	}
13907 
13908 	/*
13909 	 * Release the credential hold we took in dtrace_state_create().
13910 	 */
13911 	if (state->dts_cred.dcr_cred != NULL)
13912 		crfree(state->dts_cred.dcr_cred);
13913 
13914 	/*
13915 	 * Now we can safely disable and destroy any enabled probes.  Because
13916 	 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
13917 	 * (especially if they're all enabled), we take two passes through the
13918 	 * ECBs:  in the first, we disable just DTRACE_PRIV_KERNEL probes, and
13919 	 * in the second we disable whatever is left over.
13920 	 */
13921 	for (match = DTRACE_PRIV_KERNEL; ; match = 0) {
13922 		for (i = 0; i < state->dts_necbs; i++) {
13923 			if ((ecb = state->dts_ecbs[i]) == NULL)
13924 				continue;
13925 
13926 			if (match && ecb->dte_probe != NULL) {
13927 				dtrace_probe_t *probe = ecb->dte_probe;
13928 				dtrace_provider_t *prov = probe->dtpr_provider;
13929 
13930 				if (!(prov->dtpv_priv.dtpp_flags & match))
13931 					continue;
13932 			}
13933 
13934 			dtrace_ecb_disable(ecb);
13935 			dtrace_ecb_destroy(ecb);
13936 		}
13937 
13938 		if (!match)
13939 			break;
13940 	}
13941 
13942 	/*
13943 	 * Before we free the buffers, perform one more sync to assure that
13944 	 * every CPU is out of probe context.
13945 	 */
13946 	dtrace_sync();
13947 
13948 	dtrace_buffer_free(state->dts_buffer);
13949 	dtrace_buffer_free(state->dts_aggbuffer);
13950 
13951 	for (i = 0; i < nspec; i++)
13952 		dtrace_buffer_free(spec[i].dtsp_buffer);
13953 
13954 #if defined(sun)
13955 	if (state->dts_cleaner != CYCLIC_NONE)
13956 		cyclic_remove(state->dts_cleaner);
13957 
13958 	if (state->dts_deadman != CYCLIC_NONE)
13959 		cyclic_remove(state->dts_deadman);
13960 #else
13961 	callout_stop(&state->dts_cleaner);
13962 	callout_drain(&state->dts_cleaner);
13963 	callout_stop(&state->dts_deadman);
13964 	callout_drain(&state->dts_deadman);
13965 #endif
13966 
13967 	dtrace_dstate_fini(&vstate->dtvs_dynvars);
13968 	dtrace_vstate_fini(vstate);
13969 	if (state->dts_ecbs != NULL)
13970 		kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *));
13971 
13972 	if (state->dts_aggregations != NULL) {
13973 #ifdef DEBUG
13974 		for (i = 0; i < state->dts_naggregations; i++)
13975 			ASSERT(state->dts_aggregations[i] == NULL);
13976 #endif
13977 		ASSERT(state->dts_naggregations > 0);
13978 		kmem_free(state->dts_aggregations,
13979 		    state->dts_naggregations * sizeof (dtrace_aggregation_t *));
13980 	}
13981 
13982 	kmem_free(state->dts_buffer, bufsize);
13983 	kmem_free(state->dts_aggbuffer, bufsize);
13984 
13985 	for (i = 0; i < nspec; i++)
13986 		kmem_free(spec[i].dtsp_buffer, bufsize);
13987 
13988 	if (spec != NULL)
13989 		kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
13990 
13991 	dtrace_format_destroy(state);
13992 
13993 	if (state->dts_aggid_arena != NULL) {
13994 #if defined(sun)
13995 		vmem_destroy(state->dts_aggid_arena);
13996 #else
13997 		delete_unrhdr(state->dts_aggid_arena);
13998 #endif
13999 		state->dts_aggid_arena = NULL;
14000 	}
14001 #if defined(sun)
14002 	ddi_soft_state_free(dtrace_softstate, minor);
14003 	vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
14004 #endif
14005 }
14006 
14007 /*
14008  * DTrace Anonymous Enabling Functions
14009  */
14010 static dtrace_state_t *
14011 dtrace_anon_grab(void)
14012 {
14013 	dtrace_state_t *state;
14014 
14015 	ASSERT(MUTEX_HELD(&dtrace_lock));
14016 
14017 	if ((state = dtrace_anon.dta_state) == NULL) {
14018 		ASSERT(dtrace_anon.dta_enabling == NULL);
14019 		return (NULL);
14020 	}
14021 
14022 	ASSERT(dtrace_anon.dta_enabling != NULL);
14023 	ASSERT(dtrace_retained != NULL);
14024 
14025 	dtrace_enabling_destroy(dtrace_anon.dta_enabling);
14026 	dtrace_anon.dta_enabling = NULL;
14027 	dtrace_anon.dta_state = NULL;
14028 
14029 	return (state);
14030 }
14031 
14032 static void
14033 dtrace_anon_property(void)
14034 {
14035 	int i, rv;
14036 	dtrace_state_t *state;
14037 	dof_hdr_t *dof;
14038 	char c[32];		/* enough for "dof-data-" + digits */
14039 
14040 	ASSERT(MUTEX_HELD(&dtrace_lock));
14041 	ASSERT(MUTEX_HELD(&cpu_lock));
14042 
14043 	for (i = 0; ; i++) {
14044 		(void) snprintf(c, sizeof (c), "dof-data-%d", i);
14045 
14046 		dtrace_err_verbose = 1;
14047 
14048 		if ((dof = dtrace_dof_property(c)) == NULL) {
14049 			dtrace_err_verbose = 0;
14050 			break;
14051 		}
14052 
14053 #if defined(sun)
14054 		/*
14055 		 * We want to create anonymous state, so we need to transition
14056 		 * the kernel debugger to indicate that DTrace is active.  If
14057 		 * this fails (e.g. because the debugger has modified text in
14058 		 * some way), we won't continue with the processing.
14059 		 */
14060 		if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
14061 			cmn_err(CE_NOTE, "kernel debugger active; anonymous "
14062 			    "enabling ignored.");
14063 			dtrace_dof_destroy(dof);
14064 			break;
14065 		}
14066 #endif
14067 
14068 		/*
14069 		 * If we haven't allocated an anonymous state, we'll do so now.
14070 		 */
14071 		if ((state = dtrace_anon.dta_state) == NULL) {
14072 #if defined(sun)
14073 			state = dtrace_state_create(NULL, NULL);
14074 #else
14075 			state = dtrace_state_create(NULL);
14076 #endif
14077 			dtrace_anon.dta_state = state;
14078 
14079 			if (state == NULL) {
14080 				/*
14081 				 * This basically shouldn't happen:  the only
14082 				 * failure mode from dtrace_state_create() is a
14083 				 * failure of ddi_soft_state_zalloc() that
14084 				 * itself should never happen.  Still, the
14085 				 * interface allows for a failure mode, and
14086 				 * we want to fail as gracefully as possible:
14087 				 * we'll emit an error message and cease
14088 				 * processing anonymous state in this case.
14089 				 */
14090 				cmn_err(CE_WARN, "failed to create "
14091 				    "anonymous state");
14092 				dtrace_dof_destroy(dof);
14093 				break;
14094 			}
14095 		}
14096 
14097 		rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(),
14098 		    &dtrace_anon.dta_enabling, 0, B_TRUE);
14099 
14100 		if (rv == 0)
14101 			rv = dtrace_dof_options(dof, state);
14102 
14103 		dtrace_err_verbose = 0;
14104 		dtrace_dof_destroy(dof);
14105 
14106 		if (rv != 0) {
14107 			/*
14108 			 * This is malformed DOF; chuck any anonymous state
14109 			 * that we created.
14110 			 */
14111 			ASSERT(dtrace_anon.dta_enabling == NULL);
14112 			dtrace_state_destroy(state);
14113 			dtrace_anon.dta_state = NULL;
14114 			break;
14115 		}
14116 
14117 		ASSERT(dtrace_anon.dta_enabling != NULL);
14118 	}
14119 
14120 	if (dtrace_anon.dta_enabling != NULL) {
14121 		int rval;
14122 
14123 		/*
14124 		 * dtrace_enabling_retain() can only fail because we are
14125 		 * trying to retain more enablings than are allowed -- but
14126 		 * we only have one anonymous enabling, and we are guaranteed
14127 		 * to be allowed at least one retained enabling; we assert
14128 		 * that dtrace_enabling_retain() returns success.
14129 		 */
14130 		rval = dtrace_enabling_retain(dtrace_anon.dta_enabling);
14131 		ASSERT(rval == 0);
14132 
14133 		dtrace_enabling_dump(dtrace_anon.dta_enabling);
14134 	}
14135 }
14136 
14137 /*
14138  * DTrace Helper Functions
14139  */
14140 static void
14141 dtrace_helper_trace(dtrace_helper_action_t *helper,
14142     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where)
14143 {
14144 	uint32_t size, next, nnext, i;
14145 	dtrace_helptrace_t *ent;
14146 	uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags;
14147 
14148 	if (!dtrace_helptrace_enabled)
14149 		return;
14150 
14151 	ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals);
14152 
14153 	/*
14154 	 * What would a tracing framework be without its own tracing
14155 	 * framework?  (Well, a hell of a lot simpler, for starters...)
14156 	 */
14157 	size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals *
14158 	    sizeof (uint64_t) - sizeof (uint64_t);
14159 
14160 	/*
14161 	 * Iterate until we can allocate a slot in the trace buffer.
14162 	 */
14163 	do {
14164 		next = dtrace_helptrace_next;
14165 
14166 		if (next + size < dtrace_helptrace_bufsize) {
14167 			nnext = next + size;
14168 		} else {
14169 			nnext = size;
14170 		}
14171 	} while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next);
14172 
14173 	/*
14174 	 * We have our slot; fill it in.
14175 	 */
14176 	if (nnext == size)
14177 		next = 0;
14178 
14179 	ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next];
14180 	ent->dtht_helper = helper;
14181 	ent->dtht_where = where;
14182 	ent->dtht_nlocals = vstate->dtvs_nlocals;
14183 
14184 	ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ?
14185 	    mstate->dtms_fltoffs : -1;
14186 	ent->dtht_fault = DTRACE_FLAGS2FLT(flags);
14187 	ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval;
14188 
14189 	for (i = 0; i < vstate->dtvs_nlocals; i++) {
14190 		dtrace_statvar_t *svar;
14191 
14192 		if ((svar = vstate->dtvs_locals[i]) == NULL)
14193 			continue;
14194 
14195 		ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t));
14196 		ent->dtht_locals[i] =
14197 		    ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu];
14198 	}
14199 }
14200 
14201 static uint64_t
14202 dtrace_helper(int which, dtrace_mstate_t *mstate,
14203     dtrace_state_t *state, uint64_t arg0, uint64_t arg1)
14204 {
14205 	uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
14206 	uint64_t sarg0 = mstate->dtms_arg[0];
14207 	uint64_t sarg1 = mstate->dtms_arg[1];
14208 	uint64_t rval = 0;
14209 	dtrace_helpers_t *helpers = curproc->p_dtrace_helpers;
14210 	dtrace_helper_action_t *helper;
14211 	dtrace_vstate_t *vstate;
14212 	dtrace_difo_t *pred;
14213 	int i, trace = dtrace_helptrace_enabled;
14214 
14215 	ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS);
14216 
14217 	if (helpers == NULL)
14218 		return (0);
14219 
14220 	if ((helper = helpers->dthps_actions[which]) == NULL)
14221 		return (0);
14222 
14223 	vstate = &helpers->dthps_vstate;
14224 	mstate->dtms_arg[0] = arg0;
14225 	mstate->dtms_arg[1] = arg1;
14226 
14227 	/*
14228 	 * Now iterate over each helper.  If its predicate evaluates to 'true',
14229 	 * we'll call the corresponding actions.  Note that the below calls
14230 	 * to dtrace_dif_emulate() may set faults in machine state.  This is
14231 	 * okay:  our caller (the outer dtrace_dif_emulate()) will simply plow
14232 	 * the stored DIF offset with its own (which is the desired behavior).
14233 	 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
14234 	 * from machine state; this is okay, too.
14235 	 */
14236 	for (; helper != NULL; helper = helper->dtha_next) {
14237 		if ((pred = helper->dtha_predicate) != NULL) {
14238 			if (trace)
14239 				dtrace_helper_trace(helper, mstate, vstate, 0);
14240 
14241 			if (!dtrace_dif_emulate(pred, mstate, vstate, state))
14242 				goto next;
14243 
14244 			if (*flags & CPU_DTRACE_FAULT)
14245 				goto err;
14246 		}
14247 
14248 		for (i = 0; i < helper->dtha_nactions; i++) {
14249 			if (trace)
14250 				dtrace_helper_trace(helper,
14251 				    mstate, vstate, i + 1);
14252 
14253 			rval = dtrace_dif_emulate(helper->dtha_actions[i],
14254 			    mstate, vstate, state);
14255 
14256 			if (*flags & CPU_DTRACE_FAULT)
14257 				goto err;
14258 		}
14259 
14260 next:
14261 		if (trace)
14262 			dtrace_helper_trace(helper, mstate, vstate,
14263 			    DTRACE_HELPTRACE_NEXT);
14264 	}
14265 
14266 	if (trace)
14267 		dtrace_helper_trace(helper, mstate, vstate,
14268 		    DTRACE_HELPTRACE_DONE);
14269 
14270 	/*
14271 	 * Restore the arg0 that we saved upon entry.
14272 	 */
14273 	mstate->dtms_arg[0] = sarg0;
14274 	mstate->dtms_arg[1] = sarg1;
14275 
14276 	return (rval);
14277 
14278 err:
14279 	if (trace)
14280 		dtrace_helper_trace(helper, mstate, vstate,
14281 		    DTRACE_HELPTRACE_ERR);
14282 
14283 	/*
14284 	 * Restore the arg0 that we saved upon entry.
14285 	 */
14286 	mstate->dtms_arg[0] = sarg0;
14287 	mstate->dtms_arg[1] = sarg1;
14288 
14289 	return (0);
14290 }
14291 
14292 static void
14293 dtrace_helper_action_destroy(dtrace_helper_action_t *helper,
14294     dtrace_vstate_t *vstate)
14295 {
14296 	int i;
14297 
14298 	if (helper->dtha_predicate != NULL)
14299 		dtrace_difo_release(helper->dtha_predicate, vstate);
14300 
14301 	for (i = 0; i < helper->dtha_nactions; i++) {
14302 		ASSERT(helper->dtha_actions[i] != NULL);
14303 		dtrace_difo_release(helper->dtha_actions[i], vstate);
14304 	}
14305 
14306 	kmem_free(helper->dtha_actions,
14307 	    helper->dtha_nactions * sizeof (dtrace_difo_t *));
14308 	kmem_free(helper, sizeof (dtrace_helper_action_t));
14309 }
14310 
14311 static int
14312 dtrace_helper_destroygen(int gen)
14313 {
14314 	proc_t *p = curproc;
14315 	dtrace_helpers_t *help = p->p_dtrace_helpers;
14316 	dtrace_vstate_t *vstate;
14317 	int i;
14318 
14319 	ASSERT(MUTEX_HELD(&dtrace_lock));
14320 
14321 	if (help == NULL || gen > help->dthps_generation)
14322 		return (EINVAL);
14323 
14324 	vstate = &help->dthps_vstate;
14325 
14326 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
14327 		dtrace_helper_action_t *last = NULL, *h, *next;
14328 
14329 		for (h = help->dthps_actions[i]; h != NULL; h = next) {
14330 			next = h->dtha_next;
14331 
14332 			if (h->dtha_generation == gen) {
14333 				if (last != NULL) {
14334 					last->dtha_next = next;
14335 				} else {
14336 					help->dthps_actions[i] = next;
14337 				}
14338 
14339 				dtrace_helper_action_destroy(h, vstate);
14340 			} else {
14341 				last = h;
14342 			}
14343 		}
14344 	}
14345 
14346 	/*
14347 	 * Interate until we've cleared out all helper providers with the
14348 	 * given generation number.
14349 	 */
14350 	for (;;) {
14351 		dtrace_helper_provider_t *prov;
14352 
14353 		/*
14354 		 * Look for a helper provider with the right generation. We
14355 		 * have to start back at the beginning of the list each time
14356 		 * because we drop dtrace_lock. It's unlikely that we'll make
14357 		 * more than two passes.
14358 		 */
14359 		for (i = 0; i < help->dthps_nprovs; i++) {
14360 			prov = help->dthps_provs[i];
14361 
14362 			if (prov->dthp_generation == gen)
14363 				break;
14364 		}
14365 
14366 		/*
14367 		 * If there were no matches, we're done.
14368 		 */
14369 		if (i == help->dthps_nprovs)
14370 			break;
14371 
14372 		/*
14373 		 * Move the last helper provider into this slot.
14374 		 */
14375 		help->dthps_nprovs--;
14376 		help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs];
14377 		help->dthps_provs[help->dthps_nprovs] = NULL;
14378 
14379 		mutex_exit(&dtrace_lock);
14380 
14381 		/*
14382 		 * If we have a meta provider, remove this helper provider.
14383 		 */
14384 		mutex_enter(&dtrace_meta_lock);
14385 		if (dtrace_meta_pid != NULL) {
14386 			ASSERT(dtrace_deferred_pid == NULL);
14387 			dtrace_helper_provider_remove(&prov->dthp_prov,
14388 			    p->p_pid);
14389 		}
14390 		mutex_exit(&dtrace_meta_lock);
14391 
14392 		dtrace_helper_provider_destroy(prov);
14393 
14394 		mutex_enter(&dtrace_lock);
14395 	}
14396 
14397 	return (0);
14398 }
14399 
14400 static int
14401 dtrace_helper_validate(dtrace_helper_action_t *helper)
14402 {
14403 	int err = 0, i;
14404 	dtrace_difo_t *dp;
14405 
14406 	if ((dp = helper->dtha_predicate) != NULL)
14407 		err += dtrace_difo_validate_helper(dp);
14408 
14409 	for (i = 0; i < helper->dtha_nactions; i++)
14410 		err += dtrace_difo_validate_helper(helper->dtha_actions[i]);
14411 
14412 	return (err == 0);
14413 }
14414 
14415 static int
14416 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep)
14417 {
14418 	dtrace_helpers_t *help;
14419 	dtrace_helper_action_t *helper, *last;
14420 	dtrace_actdesc_t *act;
14421 	dtrace_vstate_t *vstate;
14422 	dtrace_predicate_t *pred;
14423 	int count = 0, nactions = 0, i;
14424 
14425 	if (which < 0 || which >= DTRACE_NHELPER_ACTIONS)
14426 		return (EINVAL);
14427 
14428 	help = curproc->p_dtrace_helpers;
14429 	last = help->dthps_actions[which];
14430 	vstate = &help->dthps_vstate;
14431 
14432 	for (count = 0; last != NULL; last = last->dtha_next) {
14433 		count++;
14434 		if (last->dtha_next == NULL)
14435 			break;
14436 	}
14437 
14438 	/*
14439 	 * If we already have dtrace_helper_actions_max helper actions for this
14440 	 * helper action type, we'll refuse to add a new one.
14441 	 */
14442 	if (count >= dtrace_helper_actions_max)
14443 		return (ENOSPC);
14444 
14445 	helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP);
14446 	helper->dtha_generation = help->dthps_generation;
14447 
14448 	if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) {
14449 		ASSERT(pred->dtp_difo != NULL);
14450 		dtrace_difo_hold(pred->dtp_difo);
14451 		helper->dtha_predicate = pred->dtp_difo;
14452 	}
14453 
14454 	for (act = ep->dted_action; act != NULL; act = act->dtad_next) {
14455 		if (act->dtad_kind != DTRACEACT_DIFEXPR)
14456 			goto err;
14457 
14458 		if (act->dtad_difo == NULL)
14459 			goto err;
14460 
14461 		nactions++;
14462 	}
14463 
14464 	helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) *
14465 	    (helper->dtha_nactions = nactions), KM_SLEEP);
14466 
14467 	for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) {
14468 		dtrace_difo_hold(act->dtad_difo);
14469 		helper->dtha_actions[i++] = act->dtad_difo;
14470 	}
14471 
14472 	if (!dtrace_helper_validate(helper))
14473 		goto err;
14474 
14475 	if (last == NULL) {
14476 		help->dthps_actions[which] = helper;
14477 	} else {
14478 		last->dtha_next = helper;
14479 	}
14480 
14481 	if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) {
14482 		dtrace_helptrace_nlocals = vstate->dtvs_nlocals;
14483 		dtrace_helptrace_next = 0;
14484 	}
14485 
14486 	return (0);
14487 err:
14488 	dtrace_helper_action_destroy(helper, vstate);
14489 	return (EINVAL);
14490 }
14491 
14492 static void
14493 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help,
14494     dof_helper_t *dofhp)
14495 {
14496 	ASSERT(MUTEX_NOT_HELD(&dtrace_lock));
14497 
14498 	mutex_enter(&dtrace_meta_lock);
14499 	mutex_enter(&dtrace_lock);
14500 
14501 	if (!dtrace_attached() || dtrace_meta_pid == NULL) {
14502 		/*
14503 		 * If the dtrace module is loaded but not attached, or if
14504 		 * there aren't isn't a meta provider registered to deal with
14505 		 * these provider descriptions, we need to postpone creating
14506 		 * the actual providers until later.
14507 		 */
14508 
14509 		if (help->dthps_next == NULL && help->dthps_prev == NULL &&
14510 		    dtrace_deferred_pid != help) {
14511 			help->dthps_deferred = 1;
14512 			help->dthps_pid = p->p_pid;
14513 			help->dthps_next = dtrace_deferred_pid;
14514 			help->dthps_prev = NULL;
14515 			if (dtrace_deferred_pid != NULL)
14516 				dtrace_deferred_pid->dthps_prev = help;
14517 			dtrace_deferred_pid = help;
14518 		}
14519 
14520 		mutex_exit(&dtrace_lock);
14521 
14522 	} else if (dofhp != NULL) {
14523 		/*
14524 		 * If the dtrace module is loaded and we have a particular
14525 		 * helper provider description, pass that off to the
14526 		 * meta provider.
14527 		 */
14528 
14529 		mutex_exit(&dtrace_lock);
14530 
14531 		dtrace_helper_provide(dofhp, p->p_pid);
14532 
14533 	} else {
14534 		/*
14535 		 * Otherwise, just pass all the helper provider descriptions
14536 		 * off to the meta provider.
14537 		 */
14538 
14539 		int i;
14540 		mutex_exit(&dtrace_lock);
14541 
14542 		for (i = 0; i < help->dthps_nprovs; i++) {
14543 			dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
14544 			    p->p_pid);
14545 		}
14546 	}
14547 
14548 	mutex_exit(&dtrace_meta_lock);
14549 }
14550 
14551 static int
14552 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen)
14553 {
14554 	dtrace_helpers_t *help;
14555 	dtrace_helper_provider_t *hprov, **tmp_provs;
14556 	uint_t tmp_maxprovs, i;
14557 
14558 	ASSERT(MUTEX_HELD(&dtrace_lock));
14559 
14560 	help = curproc->p_dtrace_helpers;
14561 	ASSERT(help != NULL);
14562 
14563 	/*
14564 	 * If we already have dtrace_helper_providers_max helper providers,
14565 	 * we're refuse to add a new one.
14566 	 */
14567 	if (help->dthps_nprovs >= dtrace_helper_providers_max)
14568 		return (ENOSPC);
14569 
14570 	/*
14571 	 * Check to make sure this isn't a duplicate.
14572 	 */
14573 	for (i = 0; i < help->dthps_nprovs; i++) {
14574 		if (dofhp->dofhp_addr ==
14575 		    help->dthps_provs[i]->dthp_prov.dofhp_addr)
14576 			return (EALREADY);
14577 	}
14578 
14579 	hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP);
14580 	hprov->dthp_prov = *dofhp;
14581 	hprov->dthp_ref = 1;
14582 	hprov->dthp_generation = gen;
14583 
14584 	/*
14585 	 * Allocate a bigger table for helper providers if it's already full.
14586 	 */
14587 	if (help->dthps_maxprovs == help->dthps_nprovs) {
14588 		tmp_maxprovs = help->dthps_maxprovs;
14589 		tmp_provs = help->dthps_provs;
14590 
14591 		if (help->dthps_maxprovs == 0)
14592 			help->dthps_maxprovs = 2;
14593 		else
14594 			help->dthps_maxprovs *= 2;
14595 		if (help->dthps_maxprovs > dtrace_helper_providers_max)
14596 			help->dthps_maxprovs = dtrace_helper_providers_max;
14597 
14598 		ASSERT(tmp_maxprovs < help->dthps_maxprovs);
14599 
14600 		help->dthps_provs = kmem_zalloc(help->dthps_maxprovs *
14601 		    sizeof (dtrace_helper_provider_t *), KM_SLEEP);
14602 
14603 		if (tmp_provs != NULL) {
14604 			bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs *
14605 			    sizeof (dtrace_helper_provider_t *));
14606 			kmem_free(tmp_provs, tmp_maxprovs *
14607 			    sizeof (dtrace_helper_provider_t *));
14608 		}
14609 	}
14610 
14611 	help->dthps_provs[help->dthps_nprovs] = hprov;
14612 	help->dthps_nprovs++;
14613 
14614 	return (0);
14615 }
14616 
14617 static void
14618 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov)
14619 {
14620 	mutex_enter(&dtrace_lock);
14621 
14622 	if (--hprov->dthp_ref == 0) {
14623 		dof_hdr_t *dof;
14624 		mutex_exit(&dtrace_lock);
14625 		dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof;
14626 		dtrace_dof_destroy(dof);
14627 		kmem_free(hprov, sizeof (dtrace_helper_provider_t));
14628 	} else {
14629 		mutex_exit(&dtrace_lock);
14630 	}
14631 }
14632 
14633 static int
14634 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec)
14635 {
14636 	uintptr_t daddr = (uintptr_t)dof;
14637 	dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
14638 	dof_provider_t *provider;
14639 	dof_probe_t *probe;
14640 	uint8_t *arg;
14641 	char *strtab, *typestr;
14642 	dof_stridx_t typeidx;
14643 	size_t typesz;
14644 	uint_t nprobes, j, k;
14645 
14646 	ASSERT(sec->dofs_type == DOF_SECT_PROVIDER);
14647 
14648 	if (sec->dofs_offset & (sizeof (uint_t) - 1)) {
14649 		dtrace_dof_error(dof, "misaligned section offset");
14650 		return (-1);
14651 	}
14652 
14653 	/*
14654 	 * The section needs to be large enough to contain the DOF provider
14655 	 * structure appropriate for the given version.
14656 	 */
14657 	if (sec->dofs_size <
14658 	    ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ?
14659 	    offsetof(dof_provider_t, dofpv_prenoffs) :
14660 	    sizeof (dof_provider_t))) {
14661 		dtrace_dof_error(dof, "provider section too small");
14662 		return (-1);
14663 	}
14664 
14665 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
14666 	str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab);
14667 	prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes);
14668 	arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs);
14669 	off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs);
14670 
14671 	if (str_sec == NULL || prb_sec == NULL ||
14672 	    arg_sec == NULL || off_sec == NULL)
14673 		return (-1);
14674 
14675 	enoff_sec = NULL;
14676 
14677 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
14678 	    provider->dofpv_prenoffs != DOF_SECT_NONE &&
14679 	    (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS,
14680 	    provider->dofpv_prenoffs)) == NULL)
14681 		return (-1);
14682 
14683 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
14684 
14685 	if (provider->dofpv_name >= str_sec->dofs_size ||
14686 	    strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) {
14687 		dtrace_dof_error(dof, "invalid provider name");
14688 		return (-1);
14689 	}
14690 
14691 	if (prb_sec->dofs_entsize == 0 ||
14692 	    prb_sec->dofs_entsize > prb_sec->dofs_size) {
14693 		dtrace_dof_error(dof, "invalid entry size");
14694 		return (-1);
14695 	}
14696 
14697 	if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) {
14698 		dtrace_dof_error(dof, "misaligned entry size");
14699 		return (-1);
14700 	}
14701 
14702 	if (off_sec->dofs_entsize != sizeof (uint32_t)) {
14703 		dtrace_dof_error(dof, "invalid entry size");
14704 		return (-1);
14705 	}
14706 
14707 	if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) {
14708 		dtrace_dof_error(dof, "misaligned section offset");
14709 		return (-1);
14710 	}
14711 
14712 	if (arg_sec->dofs_entsize != sizeof (uint8_t)) {
14713 		dtrace_dof_error(dof, "invalid entry size");
14714 		return (-1);
14715 	}
14716 
14717 	arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
14718 
14719 	nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
14720 
14721 	/*
14722 	 * Take a pass through the probes to check for errors.
14723 	 */
14724 	for (j = 0; j < nprobes; j++) {
14725 		probe = (dof_probe_t *)(uintptr_t)(daddr +
14726 		    prb_sec->dofs_offset + j * prb_sec->dofs_entsize);
14727 
14728 		if (probe->dofpr_func >= str_sec->dofs_size) {
14729 			dtrace_dof_error(dof, "invalid function name");
14730 			return (-1);
14731 		}
14732 
14733 		if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) {
14734 			dtrace_dof_error(dof, "function name too long");
14735 			return (-1);
14736 		}
14737 
14738 		if (probe->dofpr_name >= str_sec->dofs_size ||
14739 		    strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) {
14740 			dtrace_dof_error(dof, "invalid probe name");
14741 			return (-1);
14742 		}
14743 
14744 		/*
14745 		 * The offset count must not wrap the index, and the offsets
14746 		 * must also not overflow the section's data.
14747 		 */
14748 		if (probe->dofpr_offidx + probe->dofpr_noffs <
14749 		    probe->dofpr_offidx ||
14750 		    (probe->dofpr_offidx + probe->dofpr_noffs) *
14751 		    off_sec->dofs_entsize > off_sec->dofs_size) {
14752 			dtrace_dof_error(dof, "invalid probe offset");
14753 			return (-1);
14754 		}
14755 
14756 		if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) {
14757 			/*
14758 			 * If there's no is-enabled offset section, make sure
14759 			 * there aren't any is-enabled offsets. Otherwise
14760 			 * perform the same checks as for probe offsets
14761 			 * (immediately above).
14762 			 */
14763 			if (enoff_sec == NULL) {
14764 				if (probe->dofpr_enoffidx != 0 ||
14765 				    probe->dofpr_nenoffs != 0) {
14766 					dtrace_dof_error(dof, "is-enabled "
14767 					    "offsets with null section");
14768 					return (-1);
14769 				}
14770 			} else if (probe->dofpr_enoffidx +
14771 			    probe->dofpr_nenoffs < probe->dofpr_enoffidx ||
14772 			    (probe->dofpr_enoffidx + probe->dofpr_nenoffs) *
14773 			    enoff_sec->dofs_entsize > enoff_sec->dofs_size) {
14774 				dtrace_dof_error(dof, "invalid is-enabled "
14775 				    "offset");
14776 				return (-1);
14777 			}
14778 
14779 			if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) {
14780 				dtrace_dof_error(dof, "zero probe and "
14781 				    "is-enabled offsets");
14782 				return (-1);
14783 			}
14784 		} else if (probe->dofpr_noffs == 0) {
14785 			dtrace_dof_error(dof, "zero probe offsets");
14786 			return (-1);
14787 		}
14788 
14789 		if (probe->dofpr_argidx + probe->dofpr_xargc <
14790 		    probe->dofpr_argidx ||
14791 		    (probe->dofpr_argidx + probe->dofpr_xargc) *
14792 		    arg_sec->dofs_entsize > arg_sec->dofs_size) {
14793 			dtrace_dof_error(dof, "invalid args");
14794 			return (-1);
14795 		}
14796 
14797 		typeidx = probe->dofpr_nargv;
14798 		typestr = strtab + probe->dofpr_nargv;
14799 		for (k = 0; k < probe->dofpr_nargc; k++) {
14800 			if (typeidx >= str_sec->dofs_size) {
14801 				dtrace_dof_error(dof, "bad "
14802 				    "native argument type");
14803 				return (-1);
14804 			}
14805 
14806 			typesz = strlen(typestr) + 1;
14807 			if (typesz > DTRACE_ARGTYPELEN) {
14808 				dtrace_dof_error(dof, "native "
14809 				    "argument type too long");
14810 				return (-1);
14811 			}
14812 			typeidx += typesz;
14813 			typestr += typesz;
14814 		}
14815 
14816 		typeidx = probe->dofpr_xargv;
14817 		typestr = strtab + probe->dofpr_xargv;
14818 		for (k = 0; k < probe->dofpr_xargc; k++) {
14819 			if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) {
14820 				dtrace_dof_error(dof, "bad "
14821 				    "native argument index");
14822 				return (-1);
14823 			}
14824 
14825 			if (typeidx >= str_sec->dofs_size) {
14826 				dtrace_dof_error(dof, "bad "
14827 				    "translated argument type");
14828 				return (-1);
14829 			}
14830 
14831 			typesz = strlen(typestr) + 1;
14832 			if (typesz > DTRACE_ARGTYPELEN) {
14833 				dtrace_dof_error(dof, "translated argument "
14834 				    "type too long");
14835 				return (-1);
14836 			}
14837 
14838 			typeidx += typesz;
14839 			typestr += typesz;
14840 		}
14841 	}
14842 
14843 	return (0);
14844 }
14845 
14846 static int
14847 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp)
14848 {
14849 	dtrace_helpers_t *help;
14850 	dtrace_vstate_t *vstate;
14851 	dtrace_enabling_t *enab = NULL;
14852 	int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1;
14853 	uintptr_t daddr = (uintptr_t)dof;
14854 
14855 	ASSERT(MUTEX_HELD(&dtrace_lock));
14856 
14857 	if ((help = curproc->p_dtrace_helpers) == NULL)
14858 		help = dtrace_helpers_create(curproc);
14859 
14860 	vstate = &help->dthps_vstate;
14861 
14862 	if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab,
14863 	    dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) {
14864 		dtrace_dof_destroy(dof);
14865 		return (rv);
14866 	}
14867 
14868 	/*
14869 	 * Look for helper providers and validate their descriptions.
14870 	 */
14871 	if (dhp != NULL) {
14872 		for (i = 0; i < dof->dofh_secnum; i++) {
14873 			dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
14874 			    dof->dofh_secoff + i * dof->dofh_secsize);
14875 
14876 			if (sec->dofs_type != DOF_SECT_PROVIDER)
14877 				continue;
14878 
14879 			if (dtrace_helper_provider_validate(dof, sec) != 0) {
14880 				dtrace_enabling_destroy(enab);
14881 				dtrace_dof_destroy(dof);
14882 				return (-1);
14883 			}
14884 
14885 			nprovs++;
14886 		}
14887 	}
14888 
14889 	/*
14890 	 * Now we need to walk through the ECB descriptions in the enabling.
14891 	 */
14892 	for (i = 0; i < enab->dten_ndesc; i++) {
14893 		dtrace_ecbdesc_t *ep = enab->dten_desc[i];
14894 		dtrace_probedesc_t *desc = &ep->dted_probe;
14895 
14896 		if (strcmp(desc->dtpd_provider, "dtrace") != 0)
14897 			continue;
14898 
14899 		if (strcmp(desc->dtpd_mod, "helper") != 0)
14900 			continue;
14901 
14902 		if (strcmp(desc->dtpd_func, "ustack") != 0)
14903 			continue;
14904 
14905 		if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK,
14906 		    ep)) != 0) {
14907 			/*
14908 			 * Adding this helper action failed -- we are now going
14909 			 * to rip out the entire generation and return failure.
14910 			 */
14911 			(void) dtrace_helper_destroygen(help->dthps_generation);
14912 			dtrace_enabling_destroy(enab);
14913 			dtrace_dof_destroy(dof);
14914 			return (-1);
14915 		}
14916 
14917 		nhelpers++;
14918 	}
14919 
14920 	if (nhelpers < enab->dten_ndesc)
14921 		dtrace_dof_error(dof, "unmatched helpers");
14922 
14923 	gen = help->dthps_generation++;
14924 	dtrace_enabling_destroy(enab);
14925 
14926 	if (dhp != NULL && nprovs > 0) {
14927 		dhp->dofhp_dof = (uint64_t)(uintptr_t)dof;
14928 		if (dtrace_helper_provider_add(dhp, gen) == 0) {
14929 			mutex_exit(&dtrace_lock);
14930 			dtrace_helper_provider_register(curproc, help, dhp);
14931 			mutex_enter(&dtrace_lock);
14932 
14933 			destroy = 0;
14934 		}
14935 	}
14936 
14937 	if (destroy)
14938 		dtrace_dof_destroy(dof);
14939 
14940 	return (gen);
14941 }
14942 
14943 static dtrace_helpers_t *
14944 dtrace_helpers_create(proc_t *p)
14945 {
14946 	dtrace_helpers_t *help;
14947 
14948 	ASSERT(MUTEX_HELD(&dtrace_lock));
14949 	ASSERT(p->p_dtrace_helpers == NULL);
14950 
14951 	help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP);
14952 	help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) *
14953 	    DTRACE_NHELPER_ACTIONS, KM_SLEEP);
14954 
14955 	p->p_dtrace_helpers = help;
14956 	dtrace_helpers++;
14957 
14958 	return (help);
14959 }
14960 
14961 #if defined(sun)
14962 static
14963 #endif
14964 void
14965 dtrace_helpers_destroy(proc_t *p)
14966 {
14967 	dtrace_helpers_t *help;
14968 	dtrace_vstate_t *vstate;
14969 #if defined(sun)
14970 	proc_t *p = curproc;
14971 #endif
14972 	int i;
14973 
14974 	mutex_enter(&dtrace_lock);
14975 
14976 	ASSERT(p->p_dtrace_helpers != NULL);
14977 	ASSERT(dtrace_helpers > 0);
14978 
14979 	help = p->p_dtrace_helpers;
14980 	vstate = &help->dthps_vstate;
14981 
14982 	/*
14983 	 * We're now going to lose the help from this process.
14984 	 */
14985 	p->p_dtrace_helpers = NULL;
14986 	dtrace_sync();
14987 
14988 	/*
14989 	 * Destory the helper actions.
14990 	 */
14991 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
14992 		dtrace_helper_action_t *h, *next;
14993 
14994 		for (h = help->dthps_actions[i]; h != NULL; h = next) {
14995 			next = h->dtha_next;
14996 			dtrace_helper_action_destroy(h, vstate);
14997 			h = next;
14998 		}
14999 	}
15000 
15001 	mutex_exit(&dtrace_lock);
15002 
15003 	/*
15004 	 * Destroy the helper providers.
15005 	 */
15006 	if (help->dthps_maxprovs > 0) {
15007 		mutex_enter(&dtrace_meta_lock);
15008 		if (dtrace_meta_pid != NULL) {
15009 			ASSERT(dtrace_deferred_pid == NULL);
15010 
15011 			for (i = 0; i < help->dthps_nprovs; i++) {
15012 				dtrace_helper_provider_remove(
15013 				    &help->dthps_provs[i]->dthp_prov, p->p_pid);
15014 			}
15015 		} else {
15016 			mutex_enter(&dtrace_lock);
15017 			ASSERT(help->dthps_deferred == 0 ||
15018 			    help->dthps_next != NULL ||
15019 			    help->dthps_prev != NULL ||
15020 			    help == dtrace_deferred_pid);
15021 
15022 			/*
15023 			 * Remove the helper from the deferred list.
15024 			 */
15025 			if (help->dthps_next != NULL)
15026 				help->dthps_next->dthps_prev = help->dthps_prev;
15027 			if (help->dthps_prev != NULL)
15028 				help->dthps_prev->dthps_next = help->dthps_next;
15029 			if (dtrace_deferred_pid == help) {
15030 				dtrace_deferred_pid = help->dthps_next;
15031 				ASSERT(help->dthps_prev == NULL);
15032 			}
15033 
15034 			mutex_exit(&dtrace_lock);
15035 		}
15036 
15037 		mutex_exit(&dtrace_meta_lock);
15038 
15039 		for (i = 0; i < help->dthps_nprovs; i++) {
15040 			dtrace_helper_provider_destroy(help->dthps_provs[i]);
15041 		}
15042 
15043 		kmem_free(help->dthps_provs, help->dthps_maxprovs *
15044 		    sizeof (dtrace_helper_provider_t *));
15045 	}
15046 
15047 	mutex_enter(&dtrace_lock);
15048 
15049 	dtrace_vstate_fini(&help->dthps_vstate);
15050 	kmem_free(help->dthps_actions,
15051 	    sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS);
15052 	kmem_free(help, sizeof (dtrace_helpers_t));
15053 
15054 	--dtrace_helpers;
15055 	mutex_exit(&dtrace_lock);
15056 }
15057 
15058 #if defined(sun)
15059 static
15060 #endif
15061 void
15062 dtrace_helpers_duplicate(proc_t *from, proc_t *to)
15063 {
15064 	dtrace_helpers_t *help, *newhelp;
15065 	dtrace_helper_action_t *helper, *new, *last;
15066 	dtrace_difo_t *dp;
15067 	dtrace_vstate_t *vstate;
15068 	int i, j, sz, hasprovs = 0;
15069 
15070 	mutex_enter(&dtrace_lock);
15071 	ASSERT(from->p_dtrace_helpers != NULL);
15072 	ASSERT(dtrace_helpers > 0);
15073 
15074 	help = from->p_dtrace_helpers;
15075 	newhelp = dtrace_helpers_create(to);
15076 	ASSERT(to->p_dtrace_helpers != NULL);
15077 
15078 	newhelp->dthps_generation = help->dthps_generation;
15079 	vstate = &newhelp->dthps_vstate;
15080 
15081 	/*
15082 	 * Duplicate the helper actions.
15083 	 */
15084 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
15085 		if ((helper = help->dthps_actions[i]) == NULL)
15086 			continue;
15087 
15088 		for (last = NULL; helper != NULL; helper = helper->dtha_next) {
15089 			new = kmem_zalloc(sizeof (dtrace_helper_action_t),
15090 			    KM_SLEEP);
15091 			new->dtha_generation = helper->dtha_generation;
15092 
15093 			if ((dp = helper->dtha_predicate) != NULL) {
15094 				dp = dtrace_difo_duplicate(dp, vstate);
15095 				new->dtha_predicate = dp;
15096 			}
15097 
15098 			new->dtha_nactions = helper->dtha_nactions;
15099 			sz = sizeof (dtrace_difo_t *) * new->dtha_nactions;
15100 			new->dtha_actions = kmem_alloc(sz, KM_SLEEP);
15101 
15102 			for (j = 0; j < new->dtha_nactions; j++) {
15103 				dtrace_difo_t *dp = helper->dtha_actions[j];
15104 
15105 				ASSERT(dp != NULL);
15106 				dp = dtrace_difo_duplicate(dp, vstate);
15107 				new->dtha_actions[j] = dp;
15108 			}
15109 
15110 			if (last != NULL) {
15111 				last->dtha_next = new;
15112 			} else {
15113 				newhelp->dthps_actions[i] = new;
15114 			}
15115 
15116 			last = new;
15117 		}
15118 	}
15119 
15120 	/*
15121 	 * Duplicate the helper providers and register them with the
15122 	 * DTrace framework.
15123 	 */
15124 	if (help->dthps_nprovs > 0) {
15125 		newhelp->dthps_nprovs = help->dthps_nprovs;
15126 		newhelp->dthps_maxprovs = help->dthps_nprovs;
15127 		newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs *
15128 		    sizeof (dtrace_helper_provider_t *), KM_SLEEP);
15129 		for (i = 0; i < newhelp->dthps_nprovs; i++) {
15130 			newhelp->dthps_provs[i] = help->dthps_provs[i];
15131 			newhelp->dthps_provs[i]->dthp_ref++;
15132 		}
15133 
15134 		hasprovs = 1;
15135 	}
15136 
15137 	mutex_exit(&dtrace_lock);
15138 
15139 	if (hasprovs)
15140 		dtrace_helper_provider_register(to, newhelp, NULL);
15141 }
15142 
15143 /*
15144  * DTrace Hook Functions
15145  */
15146 static void
15147 dtrace_module_loaded(modctl_t *ctl)
15148 {
15149 	dtrace_provider_t *prv;
15150 
15151 	mutex_enter(&dtrace_provider_lock);
15152 #if defined(sun)
15153 	mutex_enter(&mod_lock);
15154 #endif
15155 
15156 #if defined(sun)
15157 	ASSERT(ctl->mod_busy);
15158 #endif
15159 
15160 	/*
15161 	 * We're going to call each providers per-module provide operation
15162 	 * specifying only this module.
15163 	 */
15164 	for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
15165 		prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
15166 
15167 #if defined(sun)
15168 	mutex_exit(&mod_lock);
15169 #endif
15170 	mutex_exit(&dtrace_provider_lock);
15171 
15172 	/*
15173 	 * If we have any retained enablings, we need to match against them.
15174 	 * Enabling probes requires that cpu_lock be held, and we cannot hold
15175 	 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
15176 	 * module.  (In particular, this happens when loading scheduling
15177 	 * classes.)  So if we have any retained enablings, we need to dispatch
15178 	 * our task queue to do the match for us.
15179 	 */
15180 	mutex_enter(&dtrace_lock);
15181 
15182 	if (dtrace_retained == NULL) {
15183 		mutex_exit(&dtrace_lock);
15184 		return;
15185 	}
15186 
15187 	(void) taskq_dispatch(dtrace_taskq,
15188 	    (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP);
15189 
15190 	mutex_exit(&dtrace_lock);
15191 
15192 	/*
15193 	 * And now, for a little heuristic sleaze:  in general, we want to
15194 	 * match modules as soon as they load.  However, we cannot guarantee
15195 	 * this, because it would lead us to the lock ordering violation
15196 	 * outlined above.  The common case, of course, is that cpu_lock is
15197 	 * _not_ held -- so we delay here for a clock tick, hoping that that's
15198 	 * long enough for the task queue to do its work.  If it's not, it's
15199 	 * not a serious problem -- it just means that the module that we
15200 	 * just loaded may not be immediately instrumentable.
15201 	 */
15202 	delay(1);
15203 }
15204 
15205 static void
15206 #if defined(sun)
15207 dtrace_module_unloaded(modctl_t *ctl)
15208 #else
15209 dtrace_module_unloaded(modctl_t *ctl, int *error)
15210 #endif
15211 {
15212 	dtrace_probe_t template, *probe, *first, *next;
15213 	dtrace_provider_t *prov;
15214 #if !defined(sun)
15215 	char modname[DTRACE_MODNAMELEN];
15216 	size_t len;
15217 #endif
15218 
15219 #if defined(sun)
15220 	template.dtpr_mod = ctl->mod_modname;
15221 #else
15222 	/* Handle the fact that ctl->filename may end in ".ko". */
15223 	strlcpy(modname, ctl->filename, sizeof(modname));
15224 	len = strlen(ctl->filename);
15225 	if (len > 3 && strcmp(modname + len - 3, ".ko") == 0)
15226 		modname[len - 3] = '\0';
15227 	template.dtpr_mod = modname;
15228 #endif
15229 
15230 	mutex_enter(&dtrace_provider_lock);
15231 #if defined(sun)
15232 	mutex_enter(&mod_lock);
15233 #endif
15234 	mutex_enter(&dtrace_lock);
15235 
15236 #if !defined(sun)
15237 	if (ctl->nenabled > 0) {
15238 		/* Don't allow unloads if a probe is enabled. */
15239 		mutex_exit(&dtrace_provider_lock);
15240 		mutex_exit(&dtrace_lock);
15241 		*error = -1;
15242 		printf(
15243 	"kldunload: attempt to unload module that has DTrace probes enabled\n");
15244 		return;
15245 	}
15246 #endif
15247 
15248 	if (dtrace_bymod == NULL) {
15249 		/*
15250 		 * The DTrace module is loaded (obviously) but not attached;
15251 		 * we don't have any work to do.
15252 		 */
15253 		mutex_exit(&dtrace_provider_lock);
15254 #if defined(sun)
15255 		mutex_exit(&mod_lock);
15256 #endif
15257 		mutex_exit(&dtrace_lock);
15258 		return;
15259 	}
15260 
15261 	for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template);
15262 	    probe != NULL; probe = probe->dtpr_nextmod) {
15263 		if (probe->dtpr_ecb != NULL) {
15264 			mutex_exit(&dtrace_provider_lock);
15265 #if defined(sun)
15266 			mutex_exit(&mod_lock);
15267 #endif
15268 			mutex_exit(&dtrace_lock);
15269 
15270 			/*
15271 			 * This shouldn't _actually_ be possible -- we're
15272 			 * unloading a module that has an enabled probe in it.
15273 			 * (It's normally up to the provider to make sure that
15274 			 * this can't happen.)  However, because dtps_enable()
15275 			 * doesn't have a failure mode, there can be an
15276 			 * enable/unload race.  Upshot:  we don't want to
15277 			 * assert, but we're not going to disable the
15278 			 * probe, either.
15279 			 */
15280 			if (dtrace_err_verbose) {
15281 #if defined(sun)
15282 				cmn_err(CE_WARN, "unloaded module '%s' had "
15283 				    "enabled probes", ctl->mod_modname);
15284 #else
15285 				cmn_err(CE_WARN, "unloaded module '%s' had "
15286 				    "enabled probes", modname);
15287 #endif
15288 			}
15289 
15290 			return;
15291 		}
15292 	}
15293 
15294 	probe = first;
15295 
15296 	for (first = NULL; probe != NULL; probe = next) {
15297 		ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe);
15298 
15299 		dtrace_probes[probe->dtpr_id - 1] = NULL;
15300 
15301 		next = probe->dtpr_nextmod;
15302 		dtrace_hash_remove(dtrace_bymod, probe);
15303 		dtrace_hash_remove(dtrace_byfunc, probe);
15304 		dtrace_hash_remove(dtrace_byname, probe);
15305 
15306 		if (first == NULL) {
15307 			first = probe;
15308 			probe->dtpr_nextmod = NULL;
15309 		} else {
15310 			probe->dtpr_nextmod = first;
15311 			first = probe;
15312 		}
15313 	}
15314 
15315 	/*
15316 	 * We've removed all of the module's probes from the hash chains and
15317 	 * from the probe array.  Now issue a dtrace_sync() to be sure that
15318 	 * everyone has cleared out from any probe array processing.
15319 	 */
15320 	dtrace_sync();
15321 
15322 	for (probe = first; probe != NULL; probe = first) {
15323 		first = probe->dtpr_nextmod;
15324 		prov = probe->dtpr_provider;
15325 		prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
15326 		    probe->dtpr_arg);
15327 		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
15328 		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
15329 		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
15330 #if defined(sun)
15331 		vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1);
15332 #else
15333 		free_unr(dtrace_arena, probe->dtpr_id);
15334 #endif
15335 		kmem_free(probe, sizeof (dtrace_probe_t));
15336 	}
15337 
15338 	mutex_exit(&dtrace_lock);
15339 #if defined(sun)
15340 	mutex_exit(&mod_lock);
15341 #endif
15342 	mutex_exit(&dtrace_provider_lock);
15343 }
15344 
15345 #if !defined(sun)
15346 static void
15347 dtrace_kld_load(void *arg __unused, linker_file_t lf)
15348 {
15349 
15350 	dtrace_module_loaded(lf);
15351 }
15352 
15353 static void
15354 dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error)
15355 {
15356 
15357 	if (*error != 0)
15358 		/* We already have an error, so don't do anything. */
15359 		return;
15360 	dtrace_module_unloaded(lf, error);
15361 }
15362 #endif
15363 
15364 #if defined(sun)
15365 static void
15366 dtrace_suspend(void)
15367 {
15368 	dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend));
15369 }
15370 
15371 static void
15372 dtrace_resume(void)
15373 {
15374 	dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume));
15375 }
15376 #endif
15377 
15378 static int
15379 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu)
15380 {
15381 	ASSERT(MUTEX_HELD(&cpu_lock));
15382 	mutex_enter(&dtrace_lock);
15383 
15384 	switch (what) {
15385 	case CPU_CONFIG: {
15386 		dtrace_state_t *state;
15387 		dtrace_optval_t *opt, rs, c;
15388 
15389 		/*
15390 		 * For now, we only allocate a new buffer for anonymous state.
15391 		 */
15392 		if ((state = dtrace_anon.dta_state) == NULL)
15393 			break;
15394 
15395 		if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
15396 			break;
15397 
15398 		opt = state->dts_options;
15399 		c = opt[DTRACEOPT_CPU];
15400 
15401 		if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu)
15402 			break;
15403 
15404 		/*
15405 		 * Regardless of what the actual policy is, we're going to
15406 		 * temporarily set our resize policy to be manual.  We're
15407 		 * also going to temporarily set our CPU option to denote
15408 		 * the newly configured CPU.
15409 		 */
15410 		rs = opt[DTRACEOPT_BUFRESIZE];
15411 		opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL;
15412 		opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu;
15413 
15414 		(void) dtrace_state_buffers(state);
15415 
15416 		opt[DTRACEOPT_BUFRESIZE] = rs;
15417 		opt[DTRACEOPT_CPU] = c;
15418 
15419 		break;
15420 	}
15421 
15422 	case CPU_UNCONFIG:
15423 		/*
15424 		 * We don't free the buffer in the CPU_UNCONFIG case.  (The
15425 		 * buffer will be freed when the consumer exits.)
15426 		 */
15427 		break;
15428 
15429 	default:
15430 		break;
15431 	}
15432 
15433 	mutex_exit(&dtrace_lock);
15434 	return (0);
15435 }
15436 
15437 #if defined(sun)
15438 static void
15439 dtrace_cpu_setup_initial(processorid_t cpu)
15440 {
15441 	(void) dtrace_cpu_setup(CPU_CONFIG, cpu);
15442 }
15443 #endif
15444 
15445 static void
15446 dtrace_toxrange_add(uintptr_t base, uintptr_t limit)
15447 {
15448 	if (dtrace_toxranges >= dtrace_toxranges_max) {
15449 		int osize, nsize;
15450 		dtrace_toxrange_t *range;
15451 
15452 		osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
15453 
15454 		if (osize == 0) {
15455 			ASSERT(dtrace_toxrange == NULL);
15456 			ASSERT(dtrace_toxranges_max == 0);
15457 			dtrace_toxranges_max = 1;
15458 		} else {
15459 			dtrace_toxranges_max <<= 1;
15460 		}
15461 
15462 		nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
15463 		range = kmem_zalloc(nsize, KM_SLEEP);
15464 
15465 		if (dtrace_toxrange != NULL) {
15466 			ASSERT(osize != 0);
15467 			bcopy(dtrace_toxrange, range, osize);
15468 			kmem_free(dtrace_toxrange, osize);
15469 		}
15470 
15471 		dtrace_toxrange = range;
15472 	}
15473 
15474 	ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0);
15475 	ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0);
15476 
15477 	dtrace_toxrange[dtrace_toxranges].dtt_base = base;
15478 	dtrace_toxrange[dtrace_toxranges].dtt_limit = limit;
15479 	dtrace_toxranges++;
15480 }
15481 
15482 /*
15483  * DTrace Driver Cookbook Functions
15484  */
15485 #if defined(sun)
15486 /*ARGSUSED*/
15487 static int
15488 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
15489 {
15490 	dtrace_provider_id_t id;
15491 	dtrace_state_t *state = NULL;
15492 	dtrace_enabling_t *enab;
15493 
15494 	mutex_enter(&cpu_lock);
15495 	mutex_enter(&dtrace_provider_lock);
15496 	mutex_enter(&dtrace_lock);
15497 
15498 	if (ddi_soft_state_init(&dtrace_softstate,
15499 	    sizeof (dtrace_state_t), 0) != 0) {
15500 		cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state");
15501 		mutex_exit(&cpu_lock);
15502 		mutex_exit(&dtrace_provider_lock);
15503 		mutex_exit(&dtrace_lock);
15504 		return (DDI_FAILURE);
15505 	}
15506 
15507 	if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR,
15508 	    DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE ||
15509 	    ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR,
15510 	    DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) {
15511 		cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes");
15512 		ddi_remove_minor_node(devi, NULL);
15513 		ddi_soft_state_fini(&dtrace_softstate);
15514 		mutex_exit(&cpu_lock);
15515 		mutex_exit(&dtrace_provider_lock);
15516 		mutex_exit(&dtrace_lock);
15517 		return (DDI_FAILURE);
15518 	}
15519 
15520 	ddi_report_dev(devi);
15521 	dtrace_devi = devi;
15522 
15523 	dtrace_modload = dtrace_module_loaded;
15524 	dtrace_modunload = dtrace_module_unloaded;
15525 	dtrace_cpu_init = dtrace_cpu_setup_initial;
15526 	dtrace_helpers_cleanup = dtrace_helpers_destroy;
15527 	dtrace_helpers_fork = dtrace_helpers_duplicate;
15528 	dtrace_cpustart_init = dtrace_suspend;
15529 	dtrace_cpustart_fini = dtrace_resume;
15530 	dtrace_debugger_init = dtrace_suspend;
15531 	dtrace_debugger_fini = dtrace_resume;
15532 
15533 	register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
15534 
15535 	ASSERT(MUTEX_HELD(&cpu_lock));
15536 
15537 	dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1,
15538 	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
15539 	dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE,
15540 	    UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0,
15541 	    VM_SLEEP | VMC_IDENTIFIER);
15542 	dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri,
15543 	    1, INT_MAX, 0);
15544 
15545 	dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
15546 	    sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
15547 	    NULL, NULL, NULL, NULL, NULL, 0);
15548 
15549 	ASSERT(MUTEX_HELD(&cpu_lock));
15550 	dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
15551 	    offsetof(dtrace_probe_t, dtpr_nextmod),
15552 	    offsetof(dtrace_probe_t, dtpr_prevmod));
15553 
15554 	dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
15555 	    offsetof(dtrace_probe_t, dtpr_nextfunc),
15556 	    offsetof(dtrace_probe_t, dtpr_prevfunc));
15557 
15558 	dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
15559 	    offsetof(dtrace_probe_t, dtpr_nextname),
15560 	    offsetof(dtrace_probe_t, dtpr_prevname));
15561 
15562 	if (dtrace_retain_max < 1) {
15563 		cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
15564 		    "setting to 1", dtrace_retain_max);
15565 		dtrace_retain_max = 1;
15566 	}
15567 
15568 	/*
15569 	 * Now discover our toxic ranges.
15570 	 */
15571 	dtrace_toxic_ranges(dtrace_toxrange_add);
15572 
15573 	/*
15574 	 * Before we register ourselves as a provider to our own framework,
15575 	 * we would like to assert that dtrace_provider is NULL -- but that's
15576 	 * not true if we were loaded as a dependency of a DTrace provider.
15577 	 * Once we've registered, we can assert that dtrace_provider is our
15578 	 * pseudo provider.
15579 	 */
15580 	(void) dtrace_register("dtrace", &dtrace_provider_attr,
15581 	    DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
15582 
15583 	ASSERT(dtrace_provider != NULL);
15584 	ASSERT((dtrace_provider_id_t)dtrace_provider == id);
15585 
15586 	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
15587 	    dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
15588 	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
15589 	    dtrace_provider, NULL, NULL, "END", 0, NULL);
15590 	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
15591 	    dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
15592 
15593 	dtrace_anon_property();
15594 	mutex_exit(&cpu_lock);
15595 
15596 	/*
15597 	 * If DTrace helper tracing is enabled, we need to allocate the
15598 	 * trace buffer and initialize the values.
15599 	 */
15600 	if (dtrace_helptrace_enabled) {
15601 		ASSERT(dtrace_helptrace_buffer == NULL);
15602 		dtrace_helptrace_buffer =
15603 		    kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
15604 		dtrace_helptrace_next = 0;
15605 	}
15606 
15607 	/*
15608 	 * If there are already providers, we must ask them to provide their
15609 	 * probes, and then match any anonymous enabling against them.  Note
15610 	 * that there should be no other retained enablings at this time:
15611 	 * the only retained enablings at this time should be the anonymous
15612 	 * enabling.
15613 	 */
15614 	if (dtrace_anon.dta_enabling != NULL) {
15615 		ASSERT(dtrace_retained == dtrace_anon.dta_enabling);
15616 
15617 		dtrace_enabling_provide(NULL);
15618 		state = dtrace_anon.dta_state;
15619 
15620 		/*
15621 		 * We couldn't hold cpu_lock across the above call to
15622 		 * dtrace_enabling_provide(), but we must hold it to actually
15623 		 * enable the probes.  We have to drop all of our locks, pick
15624 		 * up cpu_lock, and regain our locks before matching the
15625 		 * retained anonymous enabling.
15626 		 */
15627 		mutex_exit(&dtrace_lock);
15628 		mutex_exit(&dtrace_provider_lock);
15629 
15630 		mutex_enter(&cpu_lock);
15631 		mutex_enter(&dtrace_provider_lock);
15632 		mutex_enter(&dtrace_lock);
15633 
15634 		if ((enab = dtrace_anon.dta_enabling) != NULL)
15635 			(void) dtrace_enabling_match(enab, NULL);
15636 
15637 		mutex_exit(&cpu_lock);
15638 	}
15639 
15640 	mutex_exit(&dtrace_lock);
15641 	mutex_exit(&dtrace_provider_lock);
15642 
15643 	if (state != NULL) {
15644 		/*
15645 		 * If we created any anonymous state, set it going now.
15646 		 */
15647 		(void) dtrace_state_go(state, &dtrace_anon.dta_beganon);
15648 	}
15649 
15650 	return (DDI_SUCCESS);
15651 }
15652 #endif
15653 
15654 #if !defined(sun)
15655 #if __FreeBSD_version >= 800039
15656 static void dtrace_dtr(void *);
15657 #endif
15658 #endif
15659 
15660 /*ARGSUSED*/
15661 static int
15662 #if defined(sun)
15663 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
15664 #else
15665 dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
15666 #endif
15667 {
15668 	dtrace_state_t *state;
15669 	uint32_t priv;
15670 	uid_t uid;
15671 	zoneid_t zoneid;
15672 
15673 #if defined(sun)
15674 	if (getminor(*devp) == DTRACEMNRN_HELPER)
15675 		return (0);
15676 
15677 	/*
15678 	 * If this wasn't an open with the "helper" minor, then it must be
15679 	 * the "dtrace" minor.
15680 	 */
15681 	ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE);
15682 #else
15683 	cred_t *cred_p = NULL;
15684 
15685 #if __FreeBSD_version < 800039
15686 	/*
15687 	 * The first minor device is the one that is cloned so there is
15688 	 * nothing more to do here.
15689 	 */
15690 	if (dev2unit(dev) == 0)
15691 		return 0;
15692 
15693 	/*
15694 	 * Devices are cloned, so if the DTrace state has already
15695 	 * been allocated, that means this device belongs to a
15696 	 * different client. Each client should open '/dev/dtrace'
15697 	 * to get a cloned device.
15698 	 */
15699 	if (dev->si_drv1 != NULL)
15700 		return (EBUSY);
15701 #endif
15702 
15703 	cred_p = dev->si_cred;
15704 #endif
15705 
15706 	/*
15707 	 * If no DTRACE_PRIV_* bits are set in the credential, then the
15708 	 * caller lacks sufficient permission to do anything with DTrace.
15709 	 */
15710 	dtrace_cred2priv(cred_p, &priv, &uid, &zoneid);
15711 	if (priv == DTRACE_PRIV_NONE) {
15712 #if !defined(sun)
15713 #if __FreeBSD_version < 800039
15714 		/* Destroy the cloned device. */
15715                 destroy_dev(dev);
15716 #endif
15717 #endif
15718 
15719 		return (EACCES);
15720 	}
15721 
15722 	/*
15723 	 * Ask all providers to provide all their probes.
15724 	 */
15725 	mutex_enter(&dtrace_provider_lock);
15726 	dtrace_probe_provide(NULL, NULL);
15727 	mutex_exit(&dtrace_provider_lock);
15728 
15729 	mutex_enter(&cpu_lock);
15730 	mutex_enter(&dtrace_lock);
15731 	dtrace_opens++;
15732 	dtrace_membar_producer();
15733 
15734 #if defined(sun)
15735 	/*
15736 	 * If the kernel debugger is active (that is, if the kernel debugger
15737 	 * modified text in some way), we won't allow the open.
15738 	 */
15739 	if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
15740 		dtrace_opens--;
15741 		mutex_exit(&cpu_lock);
15742 		mutex_exit(&dtrace_lock);
15743 		return (EBUSY);
15744 	}
15745 
15746 	state = dtrace_state_create(devp, cred_p);
15747 #else
15748 	state = dtrace_state_create(dev);
15749 #if __FreeBSD_version < 800039
15750 	dev->si_drv1 = state;
15751 #else
15752 	devfs_set_cdevpriv(state, dtrace_dtr);
15753 #endif
15754 	/* This code actually belongs in dtrace_attach() */
15755 	if (dtrace_opens == 1)
15756 		dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri,
15757 		    1, INT_MAX, 0);
15758 #endif
15759 
15760 	mutex_exit(&cpu_lock);
15761 
15762 	if (state == NULL) {
15763 #if defined(sun)
15764 		if (--dtrace_opens == 0)
15765 			(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
15766 #else
15767 		--dtrace_opens;
15768 #endif
15769 		mutex_exit(&dtrace_lock);
15770 #if !defined(sun)
15771 #if __FreeBSD_version < 800039
15772 		/* Destroy the cloned device. */
15773                 destroy_dev(dev);
15774 #endif
15775 #endif
15776 		return (EAGAIN);
15777 	}
15778 
15779 	mutex_exit(&dtrace_lock);
15780 
15781 	return (0);
15782 }
15783 
15784 /*ARGSUSED*/
15785 #if defined(sun)
15786 static int
15787 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
15788 #elif __FreeBSD_version < 800039
15789 static int
15790 dtrace_close(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
15791 #else
15792 static void
15793 dtrace_dtr(void *data)
15794 #endif
15795 {
15796 #if defined(sun)
15797 	minor_t minor = getminor(dev);
15798 	dtrace_state_t *state;
15799 
15800 	if (minor == DTRACEMNRN_HELPER)
15801 		return (0);
15802 
15803 	state = ddi_get_soft_state(dtrace_softstate, minor);
15804 #else
15805 #if __FreeBSD_version < 800039
15806 	dtrace_state_t *state = dev->si_drv1;
15807 
15808 	/* Check if this is not a cloned device. */
15809 	if (dev2unit(dev) == 0)
15810 		return (0);
15811 #else
15812 	dtrace_state_t *state = data;
15813 #endif
15814 
15815 #endif
15816 
15817 	mutex_enter(&cpu_lock);
15818 	mutex_enter(&dtrace_lock);
15819 
15820 	if (state != NULL) {
15821 		if (state->dts_anon) {
15822 			/*
15823 			 * There is anonymous state. Destroy that first.
15824 			 */
15825 			ASSERT(dtrace_anon.dta_state == NULL);
15826 			dtrace_state_destroy(state->dts_anon);
15827 		}
15828 
15829 		dtrace_state_destroy(state);
15830 
15831 #if !defined(sun)
15832 		kmem_free(state, 0);
15833 #if __FreeBSD_version < 800039
15834 		dev->si_drv1 = NULL;
15835 #endif
15836 #endif
15837 	}
15838 
15839 	ASSERT(dtrace_opens > 0);
15840 #if defined(sun)
15841 	if (--dtrace_opens == 0)
15842 		(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
15843 #else
15844 	--dtrace_opens;
15845 	/* This code actually belongs in dtrace_detach() */
15846 	if ((dtrace_opens == 0) && (dtrace_taskq != NULL)) {
15847 		taskq_destroy(dtrace_taskq);
15848 		dtrace_taskq = NULL;
15849 	}
15850 #endif
15851 
15852 	mutex_exit(&dtrace_lock);
15853 	mutex_exit(&cpu_lock);
15854 
15855 #if __FreeBSD_version < 800039
15856 	/* Schedule this cloned device to be destroyed. */
15857 	destroy_dev_sched(dev);
15858 #endif
15859 
15860 #if defined(sun) || __FreeBSD_version < 800039
15861 	return (0);
15862 #endif
15863 }
15864 
15865 #if defined(sun)
15866 /*ARGSUSED*/
15867 static int
15868 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv)
15869 {
15870 	int rval;
15871 	dof_helper_t help, *dhp = NULL;
15872 
15873 	switch (cmd) {
15874 	case DTRACEHIOC_ADDDOF:
15875 		if (copyin((void *)arg, &help, sizeof (help)) != 0) {
15876 			dtrace_dof_error(NULL, "failed to copyin DOF helper");
15877 			return (EFAULT);
15878 		}
15879 
15880 		dhp = &help;
15881 		arg = (intptr_t)help.dofhp_dof;
15882 		/*FALLTHROUGH*/
15883 
15884 	case DTRACEHIOC_ADD: {
15885 		dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval);
15886 
15887 		if (dof == NULL)
15888 			return (rval);
15889 
15890 		mutex_enter(&dtrace_lock);
15891 
15892 		/*
15893 		 * dtrace_helper_slurp() takes responsibility for the dof --
15894 		 * it may free it now or it may save it and free it later.
15895 		 */
15896 		if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) {
15897 			*rv = rval;
15898 			rval = 0;
15899 		} else {
15900 			rval = EINVAL;
15901 		}
15902 
15903 		mutex_exit(&dtrace_lock);
15904 		return (rval);
15905 	}
15906 
15907 	case DTRACEHIOC_REMOVE: {
15908 		mutex_enter(&dtrace_lock);
15909 		rval = dtrace_helper_destroygen(arg);
15910 		mutex_exit(&dtrace_lock);
15911 
15912 		return (rval);
15913 	}
15914 
15915 	default:
15916 		break;
15917 	}
15918 
15919 	return (ENOTTY);
15920 }
15921 
15922 /*ARGSUSED*/
15923 static int
15924 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
15925 {
15926 	minor_t minor = getminor(dev);
15927 	dtrace_state_t *state;
15928 	int rval;
15929 
15930 	if (minor == DTRACEMNRN_HELPER)
15931 		return (dtrace_ioctl_helper(cmd, arg, rv));
15932 
15933 	state = ddi_get_soft_state(dtrace_softstate, minor);
15934 
15935 	if (state->dts_anon) {
15936 		ASSERT(dtrace_anon.dta_state == NULL);
15937 		state = state->dts_anon;
15938 	}
15939 
15940 	switch (cmd) {
15941 	case DTRACEIOC_PROVIDER: {
15942 		dtrace_providerdesc_t pvd;
15943 		dtrace_provider_t *pvp;
15944 
15945 		if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0)
15946 			return (EFAULT);
15947 
15948 		pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
15949 		mutex_enter(&dtrace_provider_lock);
15950 
15951 		for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
15952 			if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0)
15953 				break;
15954 		}
15955 
15956 		mutex_exit(&dtrace_provider_lock);
15957 
15958 		if (pvp == NULL)
15959 			return (ESRCH);
15960 
15961 		bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t));
15962 		bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t));
15963 
15964 		if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0)
15965 			return (EFAULT);
15966 
15967 		return (0);
15968 	}
15969 
15970 	case DTRACEIOC_EPROBE: {
15971 		dtrace_eprobedesc_t epdesc;
15972 		dtrace_ecb_t *ecb;
15973 		dtrace_action_t *act;
15974 		void *buf;
15975 		size_t size;
15976 		uintptr_t dest;
15977 		int nrecs;
15978 
15979 		if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0)
15980 			return (EFAULT);
15981 
15982 		mutex_enter(&dtrace_lock);
15983 
15984 		if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
15985 			mutex_exit(&dtrace_lock);
15986 			return (EINVAL);
15987 		}
15988 
15989 		if (ecb->dte_probe == NULL) {
15990 			mutex_exit(&dtrace_lock);
15991 			return (EINVAL);
15992 		}
15993 
15994 		epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
15995 		epdesc.dtepd_uarg = ecb->dte_uarg;
15996 		epdesc.dtepd_size = ecb->dte_size;
15997 
15998 		nrecs = epdesc.dtepd_nrecs;
15999 		epdesc.dtepd_nrecs = 0;
16000 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
16001 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
16002 				continue;
16003 
16004 			epdesc.dtepd_nrecs++;
16005 		}
16006 
16007 		/*
16008 		 * Now that we have the size, we need to allocate a temporary
16009 		 * buffer in which to store the complete description.  We need
16010 		 * the temporary buffer to be able to drop dtrace_lock()
16011 		 * across the copyout(), below.
16012 		 */
16013 		size = sizeof (dtrace_eprobedesc_t) +
16014 		    (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
16015 
16016 		buf = kmem_alloc(size, KM_SLEEP);
16017 		dest = (uintptr_t)buf;
16018 
16019 		bcopy(&epdesc, (void *)dest, sizeof (epdesc));
16020 		dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
16021 
16022 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
16023 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
16024 				continue;
16025 
16026 			if (nrecs-- == 0)
16027 				break;
16028 
16029 			bcopy(&act->dta_rec, (void *)dest,
16030 			    sizeof (dtrace_recdesc_t));
16031 			dest += sizeof (dtrace_recdesc_t);
16032 		}
16033 
16034 		mutex_exit(&dtrace_lock);
16035 
16036 		if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
16037 			kmem_free(buf, size);
16038 			return (EFAULT);
16039 		}
16040 
16041 		kmem_free(buf, size);
16042 		return (0);
16043 	}
16044 
16045 	case DTRACEIOC_AGGDESC: {
16046 		dtrace_aggdesc_t aggdesc;
16047 		dtrace_action_t *act;
16048 		dtrace_aggregation_t *agg;
16049 		int nrecs;
16050 		uint32_t offs;
16051 		dtrace_recdesc_t *lrec;
16052 		void *buf;
16053 		size_t size;
16054 		uintptr_t dest;
16055 
16056 		if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0)
16057 			return (EFAULT);
16058 
16059 		mutex_enter(&dtrace_lock);
16060 
16061 		if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
16062 			mutex_exit(&dtrace_lock);
16063 			return (EINVAL);
16064 		}
16065 
16066 		aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
16067 
16068 		nrecs = aggdesc.dtagd_nrecs;
16069 		aggdesc.dtagd_nrecs = 0;
16070 
16071 		offs = agg->dtag_base;
16072 		lrec = &agg->dtag_action.dta_rec;
16073 		aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
16074 
16075 		for (act = agg->dtag_first; ; act = act->dta_next) {
16076 			ASSERT(act->dta_intuple ||
16077 			    DTRACEACT_ISAGG(act->dta_kind));
16078 
16079 			/*
16080 			 * If this action has a record size of zero, it
16081 			 * denotes an argument to the aggregating action.
16082 			 * Because the presence of this record doesn't (or
16083 			 * shouldn't) affect the way the data is interpreted,
16084 			 * we don't copy it out to save user-level the
16085 			 * confusion of dealing with a zero-length record.
16086 			 */
16087 			if (act->dta_rec.dtrd_size == 0) {
16088 				ASSERT(agg->dtag_hasarg);
16089 				continue;
16090 			}
16091 
16092 			aggdesc.dtagd_nrecs++;
16093 
16094 			if (act == &agg->dtag_action)
16095 				break;
16096 		}
16097 
16098 		/*
16099 		 * Now that we have the size, we need to allocate a temporary
16100 		 * buffer in which to store the complete description.  We need
16101 		 * the temporary buffer to be able to drop dtrace_lock()
16102 		 * across the copyout(), below.
16103 		 */
16104 		size = sizeof (dtrace_aggdesc_t) +
16105 		    (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
16106 
16107 		buf = kmem_alloc(size, KM_SLEEP);
16108 		dest = (uintptr_t)buf;
16109 
16110 		bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
16111 		dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
16112 
16113 		for (act = agg->dtag_first; ; act = act->dta_next) {
16114 			dtrace_recdesc_t rec = act->dta_rec;
16115 
16116 			/*
16117 			 * See the comment in the above loop for why we pass
16118 			 * over zero-length records.
16119 			 */
16120 			if (rec.dtrd_size == 0) {
16121 				ASSERT(agg->dtag_hasarg);
16122 				continue;
16123 			}
16124 
16125 			if (nrecs-- == 0)
16126 				break;
16127 
16128 			rec.dtrd_offset -= offs;
16129 			bcopy(&rec, (void *)dest, sizeof (rec));
16130 			dest += sizeof (dtrace_recdesc_t);
16131 
16132 			if (act == &agg->dtag_action)
16133 				break;
16134 		}
16135 
16136 		mutex_exit(&dtrace_lock);
16137 
16138 		if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
16139 			kmem_free(buf, size);
16140 			return (EFAULT);
16141 		}
16142 
16143 		kmem_free(buf, size);
16144 		return (0);
16145 	}
16146 
16147 	case DTRACEIOC_ENABLE: {
16148 		dof_hdr_t *dof;
16149 		dtrace_enabling_t *enab = NULL;
16150 		dtrace_vstate_t *vstate;
16151 		int err = 0;
16152 
16153 		*rv = 0;
16154 
16155 		/*
16156 		 * If a NULL argument has been passed, we take this as our
16157 		 * cue to reevaluate our enablings.
16158 		 */
16159 		if (arg == NULL) {
16160 			dtrace_enabling_matchall();
16161 
16162 			return (0);
16163 		}
16164 
16165 		if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL)
16166 			return (rval);
16167 
16168 		mutex_enter(&cpu_lock);
16169 		mutex_enter(&dtrace_lock);
16170 		vstate = &state->dts_vstate;
16171 
16172 		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
16173 			mutex_exit(&dtrace_lock);
16174 			mutex_exit(&cpu_lock);
16175 			dtrace_dof_destroy(dof);
16176 			return (EBUSY);
16177 		}
16178 
16179 		if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) {
16180 			mutex_exit(&dtrace_lock);
16181 			mutex_exit(&cpu_lock);
16182 			dtrace_dof_destroy(dof);
16183 			return (EINVAL);
16184 		}
16185 
16186 		if ((rval = dtrace_dof_options(dof, state)) != 0) {
16187 			dtrace_enabling_destroy(enab);
16188 			mutex_exit(&dtrace_lock);
16189 			mutex_exit(&cpu_lock);
16190 			dtrace_dof_destroy(dof);
16191 			return (rval);
16192 		}
16193 
16194 		if ((err = dtrace_enabling_match(enab, rv)) == 0) {
16195 			err = dtrace_enabling_retain(enab);
16196 		} else {
16197 			dtrace_enabling_destroy(enab);
16198 		}
16199 
16200 		mutex_exit(&cpu_lock);
16201 		mutex_exit(&dtrace_lock);
16202 		dtrace_dof_destroy(dof);
16203 
16204 		return (err);
16205 	}
16206 
16207 	case DTRACEIOC_REPLICATE: {
16208 		dtrace_repldesc_t desc;
16209 		dtrace_probedesc_t *match = &desc.dtrpd_match;
16210 		dtrace_probedesc_t *create = &desc.dtrpd_create;
16211 		int err;
16212 
16213 		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16214 			return (EFAULT);
16215 
16216 		match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16217 		match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16218 		match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16219 		match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16220 
16221 		create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16222 		create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16223 		create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16224 		create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16225 
16226 		mutex_enter(&dtrace_lock);
16227 		err = dtrace_enabling_replicate(state, match, create);
16228 		mutex_exit(&dtrace_lock);
16229 
16230 		return (err);
16231 	}
16232 
16233 	case DTRACEIOC_PROBEMATCH:
16234 	case DTRACEIOC_PROBES: {
16235 		dtrace_probe_t *probe = NULL;
16236 		dtrace_probedesc_t desc;
16237 		dtrace_probekey_t pkey;
16238 		dtrace_id_t i;
16239 		int m = 0;
16240 		uint32_t priv;
16241 		uid_t uid;
16242 		zoneid_t zoneid;
16243 
16244 		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16245 			return (EFAULT);
16246 
16247 		desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16248 		desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16249 		desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16250 		desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16251 
16252 		/*
16253 		 * Before we attempt to match this probe, we want to give
16254 		 * all providers the opportunity to provide it.
16255 		 */
16256 		if (desc.dtpd_id == DTRACE_IDNONE) {
16257 			mutex_enter(&dtrace_provider_lock);
16258 			dtrace_probe_provide(&desc, NULL);
16259 			mutex_exit(&dtrace_provider_lock);
16260 			desc.dtpd_id++;
16261 		}
16262 
16263 		if (cmd == DTRACEIOC_PROBEMATCH)  {
16264 			dtrace_probekey(&desc, &pkey);
16265 			pkey.dtpk_id = DTRACE_IDNONE;
16266 		}
16267 
16268 		dtrace_cred2priv(cr, &priv, &uid, &zoneid);
16269 
16270 		mutex_enter(&dtrace_lock);
16271 
16272 		if (cmd == DTRACEIOC_PROBEMATCH) {
16273 			for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
16274 				if ((probe = dtrace_probes[i - 1]) != NULL &&
16275 				    (m = dtrace_match_probe(probe, &pkey,
16276 				    priv, uid, zoneid)) != 0)
16277 					break;
16278 			}
16279 
16280 			if (m < 0) {
16281 				mutex_exit(&dtrace_lock);
16282 				return (EINVAL);
16283 			}
16284 
16285 		} else {
16286 			for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
16287 				if ((probe = dtrace_probes[i - 1]) != NULL &&
16288 				    dtrace_match_priv(probe, priv, uid, zoneid))
16289 					break;
16290 			}
16291 		}
16292 
16293 		if (probe == NULL) {
16294 			mutex_exit(&dtrace_lock);
16295 			return (ESRCH);
16296 		}
16297 
16298 		dtrace_probe_description(probe, &desc);
16299 		mutex_exit(&dtrace_lock);
16300 
16301 		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16302 			return (EFAULT);
16303 
16304 		return (0);
16305 	}
16306 
16307 	case DTRACEIOC_PROBEARG: {
16308 		dtrace_argdesc_t desc;
16309 		dtrace_probe_t *probe;
16310 		dtrace_provider_t *prov;
16311 
16312 		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16313 			return (EFAULT);
16314 
16315 		if (desc.dtargd_id == DTRACE_IDNONE)
16316 			return (EINVAL);
16317 
16318 		if (desc.dtargd_ndx == DTRACE_ARGNONE)
16319 			return (EINVAL);
16320 
16321 		mutex_enter(&dtrace_provider_lock);
16322 		mutex_enter(&mod_lock);
16323 		mutex_enter(&dtrace_lock);
16324 
16325 		if (desc.dtargd_id > dtrace_nprobes) {
16326 			mutex_exit(&dtrace_lock);
16327 			mutex_exit(&mod_lock);
16328 			mutex_exit(&dtrace_provider_lock);
16329 			return (EINVAL);
16330 		}
16331 
16332 		if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) {
16333 			mutex_exit(&dtrace_lock);
16334 			mutex_exit(&mod_lock);
16335 			mutex_exit(&dtrace_provider_lock);
16336 			return (EINVAL);
16337 		}
16338 
16339 		mutex_exit(&dtrace_lock);
16340 
16341 		prov = probe->dtpr_provider;
16342 
16343 		if (prov->dtpv_pops.dtps_getargdesc == NULL) {
16344 			/*
16345 			 * There isn't any typed information for this probe.
16346 			 * Set the argument number to DTRACE_ARGNONE.
16347 			 */
16348 			desc.dtargd_ndx = DTRACE_ARGNONE;
16349 		} else {
16350 			desc.dtargd_native[0] = '\0';
16351 			desc.dtargd_xlate[0] = '\0';
16352 			desc.dtargd_mapping = desc.dtargd_ndx;
16353 
16354 			prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
16355 			    probe->dtpr_id, probe->dtpr_arg, &desc);
16356 		}
16357 
16358 		mutex_exit(&mod_lock);
16359 		mutex_exit(&dtrace_provider_lock);
16360 
16361 		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16362 			return (EFAULT);
16363 
16364 		return (0);
16365 	}
16366 
16367 	case DTRACEIOC_GO: {
16368 		processorid_t cpuid;
16369 		rval = dtrace_state_go(state, &cpuid);
16370 
16371 		if (rval != 0)
16372 			return (rval);
16373 
16374 		if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
16375 			return (EFAULT);
16376 
16377 		return (0);
16378 	}
16379 
16380 	case DTRACEIOC_STOP: {
16381 		processorid_t cpuid;
16382 
16383 		mutex_enter(&dtrace_lock);
16384 		rval = dtrace_state_stop(state, &cpuid);
16385 		mutex_exit(&dtrace_lock);
16386 
16387 		if (rval != 0)
16388 			return (rval);
16389 
16390 		if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
16391 			return (EFAULT);
16392 
16393 		return (0);
16394 	}
16395 
16396 	case DTRACEIOC_DOFGET: {
16397 		dof_hdr_t hdr, *dof;
16398 		uint64_t len;
16399 
16400 		if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0)
16401 			return (EFAULT);
16402 
16403 		mutex_enter(&dtrace_lock);
16404 		dof = dtrace_dof_create(state);
16405 		mutex_exit(&dtrace_lock);
16406 
16407 		len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
16408 		rval = copyout(dof, (void *)arg, len);
16409 		dtrace_dof_destroy(dof);
16410 
16411 		return (rval == 0 ? 0 : EFAULT);
16412 	}
16413 
16414 	case DTRACEIOC_AGGSNAP:
16415 	case DTRACEIOC_BUFSNAP: {
16416 		dtrace_bufdesc_t desc;
16417 		caddr_t cached;
16418 		dtrace_buffer_t *buf;
16419 
16420 		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16421 			return (EFAULT);
16422 
16423 		if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU)
16424 			return (EINVAL);
16425 
16426 		mutex_enter(&dtrace_lock);
16427 
16428 		if (cmd == DTRACEIOC_BUFSNAP) {
16429 			buf = &state->dts_buffer[desc.dtbd_cpu];
16430 		} else {
16431 			buf = &state->dts_aggbuffer[desc.dtbd_cpu];
16432 		}
16433 
16434 		if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
16435 			size_t sz = buf->dtb_offset;
16436 
16437 			if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
16438 				mutex_exit(&dtrace_lock);
16439 				return (EBUSY);
16440 			}
16441 
16442 			/*
16443 			 * If this buffer has already been consumed, we're
16444 			 * going to indicate that there's nothing left here
16445 			 * to consume.
16446 			 */
16447 			if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
16448 				mutex_exit(&dtrace_lock);
16449 
16450 				desc.dtbd_size = 0;
16451 				desc.dtbd_drops = 0;
16452 				desc.dtbd_errors = 0;
16453 				desc.dtbd_oldest = 0;
16454 				sz = sizeof (desc);
16455 
16456 				if (copyout(&desc, (void *)arg, sz) != 0)
16457 					return (EFAULT);
16458 
16459 				return (0);
16460 			}
16461 
16462 			/*
16463 			 * If this is a ring buffer that has wrapped, we want
16464 			 * to copy the whole thing out.
16465 			 */
16466 			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
16467 				dtrace_buffer_polish(buf);
16468 				sz = buf->dtb_size;
16469 			}
16470 
16471 			if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
16472 				mutex_exit(&dtrace_lock);
16473 				return (EFAULT);
16474 			}
16475 
16476 			desc.dtbd_size = sz;
16477 			desc.dtbd_drops = buf->dtb_drops;
16478 			desc.dtbd_errors = buf->dtb_errors;
16479 			desc.dtbd_oldest = buf->dtb_xamot_offset;
16480 			desc.dtbd_timestamp = dtrace_gethrtime();
16481 
16482 			mutex_exit(&dtrace_lock);
16483 
16484 			if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16485 				return (EFAULT);
16486 
16487 			buf->dtb_flags |= DTRACEBUF_CONSUMED;
16488 
16489 			return (0);
16490 		}
16491 
16492 		if (buf->dtb_tomax == NULL) {
16493 			ASSERT(buf->dtb_xamot == NULL);
16494 			mutex_exit(&dtrace_lock);
16495 			return (ENOENT);
16496 		}
16497 
16498 		cached = buf->dtb_tomax;
16499 		ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
16500 
16501 		dtrace_xcall(desc.dtbd_cpu,
16502 		    (dtrace_xcall_t)dtrace_buffer_switch, buf);
16503 
16504 		state->dts_errors += buf->dtb_xamot_errors;
16505 
16506 		/*
16507 		 * If the buffers did not actually switch, then the cross call
16508 		 * did not take place -- presumably because the given CPU is
16509 		 * not in the ready set.  If this is the case, we'll return
16510 		 * ENOENT.
16511 		 */
16512 		if (buf->dtb_tomax == cached) {
16513 			ASSERT(buf->dtb_xamot != cached);
16514 			mutex_exit(&dtrace_lock);
16515 			return (ENOENT);
16516 		}
16517 
16518 		ASSERT(cached == buf->dtb_xamot);
16519 
16520 		/*
16521 		 * We have our snapshot; now copy it out.
16522 		 */
16523 		if (copyout(buf->dtb_xamot, desc.dtbd_data,
16524 		    buf->dtb_xamot_offset) != 0) {
16525 			mutex_exit(&dtrace_lock);
16526 			return (EFAULT);
16527 		}
16528 
16529 		desc.dtbd_size = buf->dtb_xamot_offset;
16530 		desc.dtbd_drops = buf->dtb_xamot_drops;
16531 		desc.dtbd_errors = buf->dtb_xamot_errors;
16532 		desc.dtbd_oldest = 0;
16533 		desc.dtbd_timestamp = buf->dtb_switched;
16534 
16535 		mutex_exit(&dtrace_lock);
16536 
16537 		/*
16538 		 * Finally, copy out the buffer description.
16539 		 */
16540 		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16541 			return (EFAULT);
16542 
16543 		return (0);
16544 	}
16545 
16546 	case DTRACEIOC_CONF: {
16547 		dtrace_conf_t conf;
16548 
16549 		bzero(&conf, sizeof (conf));
16550 		conf.dtc_difversion = DIF_VERSION;
16551 		conf.dtc_difintregs = DIF_DIR_NREGS;
16552 		conf.dtc_diftupregs = DIF_DTR_NREGS;
16553 		conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
16554 
16555 		if (copyout(&conf, (void *)arg, sizeof (conf)) != 0)
16556 			return (EFAULT);
16557 
16558 		return (0);
16559 	}
16560 
16561 	case DTRACEIOC_STATUS: {
16562 		dtrace_status_t stat;
16563 		dtrace_dstate_t *dstate;
16564 		int i, j;
16565 		uint64_t nerrs;
16566 
16567 		/*
16568 		 * See the comment in dtrace_state_deadman() for the reason
16569 		 * for setting dts_laststatus to INT64_MAX before setting
16570 		 * it to the correct value.
16571 		 */
16572 		state->dts_laststatus = INT64_MAX;
16573 		dtrace_membar_producer();
16574 		state->dts_laststatus = dtrace_gethrtime();
16575 
16576 		bzero(&stat, sizeof (stat));
16577 
16578 		mutex_enter(&dtrace_lock);
16579 
16580 		if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
16581 			mutex_exit(&dtrace_lock);
16582 			return (ENOENT);
16583 		}
16584 
16585 		if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
16586 			stat.dtst_exiting = 1;
16587 
16588 		nerrs = state->dts_errors;
16589 		dstate = &state->dts_vstate.dtvs_dynvars;
16590 
16591 		for (i = 0; i < NCPU; i++) {
16592 			dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
16593 
16594 			stat.dtst_dyndrops += dcpu->dtdsc_drops;
16595 			stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
16596 			stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
16597 
16598 			if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
16599 				stat.dtst_filled++;
16600 
16601 			nerrs += state->dts_buffer[i].dtb_errors;
16602 
16603 			for (j = 0; j < state->dts_nspeculations; j++) {
16604 				dtrace_speculation_t *spec;
16605 				dtrace_buffer_t *buf;
16606 
16607 				spec = &state->dts_speculations[j];
16608 				buf = &spec->dtsp_buffer[i];
16609 				stat.dtst_specdrops += buf->dtb_xamot_drops;
16610 			}
16611 		}
16612 
16613 		stat.dtst_specdrops_busy = state->dts_speculations_busy;
16614 		stat.dtst_specdrops_unavail = state->dts_speculations_unavail;
16615 		stat.dtst_stkstroverflows = state->dts_stkstroverflows;
16616 		stat.dtst_dblerrors = state->dts_dblerrors;
16617 		stat.dtst_killed =
16618 		    (state->dts_activity == DTRACE_ACTIVITY_KILLED);
16619 		stat.dtst_errors = nerrs;
16620 
16621 		mutex_exit(&dtrace_lock);
16622 
16623 		if (copyout(&stat, (void *)arg, sizeof (stat)) != 0)
16624 			return (EFAULT);
16625 
16626 		return (0);
16627 	}
16628 
16629 	case DTRACEIOC_FORMAT: {
16630 		dtrace_fmtdesc_t fmt;
16631 		char *str;
16632 		int len;
16633 
16634 		if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0)
16635 			return (EFAULT);
16636 
16637 		mutex_enter(&dtrace_lock);
16638 
16639 		if (fmt.dtfd_format == 0 ||
16640 		    fmt.dtfd_format > state->dts_nformats) {
16641 			mutex_exit(&dtrace_lock);
16642 			return (EINVAL);
16643 		}
16644 
16645 		/*
16646 		 * Format strings are allocated contiguously and they are
16647 		 * never freed; if a format index is less than the number
16648 		 * of formats, we can assert that the format map is non-NULL
16649 		 * and that the format for the specified index is non-NULL.
16650 		 */
16651 		ASSERT(state->dts_formats != NULL);
16652 		str = state->dts_formats[fmt.dtfd_format - 1];
16653 		ASSERT(str != NULL);
16654 
16655 		len = strlen(str) + 1;
16656 
16657 		if (len > fmt.dtfd_length) {
16658 			fmt.dtfd_length = len;
16659 
16660 			if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) {
16661 				mutex_exit(&dtrace_lock);
16662 				return (EINVAL);
16663 			}
16664 		} else {
16665 			if (copyout(str, fmt.dtfd_string, len) != 0) {
16666 				mutex_exit(&dtrace_lock);
16667 				return (EINVAL);
16668 			}
16669 		}
16670 
16671 		mutex_exit(&dtrace_lock);
16672 		return (0);
16673 	}
16674 
16675 	default:
16676 		break;
16677 	}
16678 
16679 	return (ENOTTY);
16680 }
16681 
16682 /*ARGSUSED*/
16683 static int
16684 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
16685 {
16686 	dtrace_state_t *state;
16687 
16688 	switch (cmd) {
16689 	case DDI_DETACH:
16690 		break;
16691 
16692 	case DDI_SUSPEND:
16693 		return (DDI_SUCCESS);
16694 
16695 	default:
16696 		return (DDI_FAILURE);
16697 	}
16698 
16699 	mutex_enter(&cpu_lock);
16700 	mutex_enter(&dtrace_provider_lock);
16701 	mutex_enter(&dtrace_lock);
16702 
16703 	ASSERT(dtrace_opens == 0);
16704 
16705 	if (dtrace_helpers > 0) {
16706 		mutex_exit(&dtrace_provider_lock);
16707 		mutex_exit(&dtrace_lock);
16708 		mutex_exit(&cpu_lock);
16709 		return (DDI_FAILURE);
16710 	}
16711 
16712 	if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
16713 		mutex_exit(&dtrace_provider_lock);
16714 		mutex_exit(&dtrace_lock);
16715 		mutex_exit(&cpu_lock);
16716 		return (DDI_FAILURE);
16717 	}
16718 
16719 	dtrace_provider = NULL;
16720 
16721 	if ((state = dtrace_anon_grab()) != NULL) {
16722 		/*
16723 		 * If there were ECBs on this state, the provider should
16724 		 * have not been allowed to detach; assert that there is
16725 		 * none.
16726 		 */
16727 		ASSERT(state->dts_necbs == 0);
16728 		dtrace_state_destroy(state);
16729 
16730 		/*
16731 		 * If we're being detached with anonymous state, we need to
16732 		 * indicate to the kernel debugger that DTrace is now inactive.
16733 		 */
16734 		(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
16735 	}
16736 
16737 	bzero(&dtrace_anon, sizeof (dtrace_anon_t));
16738 	unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
16739 	dtrace_cpu_init = NULL;
16740 	dtrace_helpers_cleanup = NULL;
16741 	dtrace_helpers_fork = NULL;
16742 	dtrace_cpustart_init = NULL;
16743 	dtrace_cpustart_fini = NULL;
16744 	dtrace_debugger_init = NULL;
16745 	dtrace_debugger_fini = NULL;
16746 	dtrace_modload = NULL;
16747 	dtrace_modunload = NULL;
16748 
16749 	mutex_exit(&cpu_lock);
16750 
16751 	if (dtrace_helptrace_enabled) {
16752 		kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize);
16753 		dtrace_helptrace_buffer = NULL;
16754 	}
16755 
16756 	kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *));
16757 	dtrace_probes = NULL;
16758 	dtrace_nprobes = 0;
16759 
16760 	dtrace_hash_destroy(dtrace_bymod);
16761 	dtrace_hash_destroy(dtrace_byfunc);
16762 	dtrace_hash_destroy(dtrace_byname);
16763 	dtrace_bymod = NULL;
16764 	dtrace_byfunc = NULL;
16765 	dtrace_byname = NULL;
16766 
16767 	kmem_cache_destroy(dtrace_state_cache);
16768 	vmem_destroy(dtrace_minor);
16769 	vmem_destroy(dtrace_arena);
16770 
16771 	if (dtrace_toxrange != NULL) {
16772 		kmem_free(dtrace_toxrange,
16773 		    dtrace_toxranges_max * sizeof (dtrace_toxrange_t));
16774 		dtrace_toxrange = NULL;
16775 		dtrace_toxranges = 0;
16776 		dtrace_toxranges_max = 0;
16777 	}
16778 
16779 	ddi_remove_minor_node(dtrace_devi, NULL);
16780 	dtrace_devi = NULL;
16781 
16782 	ddi_soft_state_fini(&dtrace_softstate);
16783 
16784 	ASSERT(dtrace_vtime_references == 0);
16785 	ASSERT(dtrace_opens == 0);
16786 	ASSERT(dtrace_retained == NULL);
16787 
16788 	mutex_exit(&dtrace_lock);
16789 	mutex_exit(&dtrace_provider_lock);
16790 
16791 	/*
16792 	 * We don't destroy the task queue until after we have dropped our
16793 	 * locks (taskq_destroy() may block on running tasks).  To prevent
16794 	 * attempting to do work after we have effectively detached but before
16795 	 * the task queue has been destroyed, all tasks dispatched via the
16796 	 * task queue must check that DTrace is still attached before
16797 	 * performing any operation.
16798 	 */
16799 	taskq_destroy(dtrace_taskq);
16800 	dtrace_taskq = NULL;
16801 
16802 	return (DDI_SUCCESS);
16803 }
16804 #endif
16805 
16806 #if defined(sun)
16807 /*ARGSUSED*/
16808 static int
16809 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
16810 {
16811 	int error;
16812 
16813 	switch (infocmd) {
16814 	case DDI_INFO_DEVT2DEVINFO:
16815 		*result = (void *)dtrace_devi;
16816 		error = DDI_SUCCESS;
16817 		break;
16818 	case DDI_INFO_DEVT2INSTANCE:
16819 		*result = (void *)0;
16820 		error = DDI_SUCCESS;
16821 		break;
16822 	default:
16823 		error = DDI_FAILURE;
16824 	}
16825 	return (error);
16826 }
16827 #endif
16828 
16829 #if defined(sun)
16830 static struct cb_ops dtrace_cb_ops = {
16831 	dtrace_open,		/* open */
16832 	dtrace_close,		/* close */
16833 	nulldev,		/* strategy */
16834 	nulldev,		/* print */
16835 	nodev,			/* dump */
16836 	nodev,			/* read */
16837 	nodev,			/* write */
16838 	dtrace_ioctl,		/* ioctl */
16839 	nodev,			/* devmap */
16840 	nodev,			/* mmap */
16841 	nodev,			/* segmap */
16842 	nochpoll,		/* poll */
16843 	ddi_prop_op,		/* cb_prop_op */
16844 	0,			/* streamtab  */
16845 	D_NEW | D_MP		/* Driver compatibility flag */
16846 };
16847 
16848 static struct dev_ops dtrace_ops = {
16849 	DEVO_REV,		/* devo_rev */
16850 	0,			/* refcnt */
16851 	dtrace_info,		/* get_dev_info */
16852 	nulldev,		/* identify */
16853 	nulldev,		/* probe */
16854 	dtrace_attach,		/* attach */
16855 	dtrace_detach,		/* detach */
16856 	nodev,			/* reset */
16857 	&dtrace_cb_ops,		/* driver operations */
16858 	NULL,			/* bus operations */
16859 	nodev			/* dev power */
16860 };
16861 
16862 static struct modldrv modldrv = {
16863 	&mod_driverops,		/* module type (this is a pseudo driver) */
16864 	"Dynamic Tracing",	/* name of module */
16865 	&dtrace_ops,		/* driver ops */
16866 };
16867 
16868 static struct modlinkage modlinkage = {
16869 	MODREV_1,
16870 	(void *)&modldrv,
16871 	NULL
16872 };
16873 
16874 int
16875 _init(void)
16876 {
16877 	return (mod_install(&modlinkage));
16878 }
16879 
16880 int
16881 _info(struct modinfo *modinfop)
16882 {
16883 	return (mod_info(&modlinkage, modinfop));
16884 }
16885 
16886 int
16887 _fini(void)
16888 {
16889 	return (mod_remove(&modlinkage));
16890 }
16891 #else
16892 
16893 static d_ioctl_t	dtrace_ioctl;
16894 static d_ioctl_t	dtrace_ioctl_helper;
16895 static void		dtrace_load(void *);
16896 static int		dtrace_unload(void);
16897 #if __FreeBSD_version < 800039
16898 static void		dtrace_clone(void *, struct ucred *, char *, int , struct cdev **);
16899 static struct clonedevs	*dtrace_clones;		/* Ptr to the array of cloned devices. */
16900 static eventhandler_tag	eh_tag;			/* Event handler tag. */
16901 #else
16902 static struct cdev	*dtrace_dev;
16903 static struct cdev	*helper_dev;
16904 #endif
16905 
16906 void dtrace_invop_init(void);
16907 void dtrace_invop_uninit(void);
16908 
16909 static struct cdevsw dtrace_cdevsw = {
16910 	.d_version	= D_VERSION,
16911 #if __FreeBSD_version < 800039
16912 	.d_flags	= D_TRACKCLOSE | D_NEEDMINOR,
16913 	.d_close	= dtrace_close,
16914 #endif
16915 	.d_ioctl	= dtrace_ioctl,
16916 	.d_open		= dtrace_open,
16917 	.d_name		= "dtrace",
16918 };
16919 
16920 static struct cdevsw helper_cdevsw = {
16921 	.d_version	= D_VERSION,
16922 	.d_ioctl	= dtrace_ioctl_helper,
16923 	.d_name		= "helper",
16924 };
16925 
16926 #include <dtrace_anon.c>
16927 #if __FreeBSD_version < 800039
16928 #include <dtrace_clone.c>
16929 #endif
16930 #include <dtrace_ioctl.c>
16931 #include <dtrace_load.c>
16932 #include <dtrace_modevent.c>
16933 #include <dtrace_sysctl.c>
16934 #include <dtrace_unload.c>
16935 #include <dtrace_vtime.c>
16936 #include <dtrace_hacks.c>
16937 #include <dtrace_isa.c>
16938 
16939 SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL);
16940 SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL);
16941 SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL);
16942 
16943 DEV_MODULE(dtrace, dtrace_modevent, NULL);
16944 MODULE_VERSION(dtrace, 1);
16945 MODULE_DEPEND(dtrace, cyclic, 1, 1, 1);
16946 MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1);
16947 #endif
16948